gdb/dwarf2read.c: Sanity check DW_AT_sibling values.
[deliverable/binutils-gdb.git] / gdb / dwarf2read.c
CommitLineData
c906108c 1/* DWARF 2 debugging format support for GDB.
917c78fc 2
28e7fd62 3 Copyright (C) 1994-2013 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
SS
31#include "defs.h"
32#include "bfd.h"
80626a55 33#include "elf-bfd.h"
c906108c
SS
34#include "symtab.h"
35#include "gdbtypes.h"
c906108c 36#include "objfiles.h"
fa8f86ff 37#include "dwarf2.h"
c906108c
SS
38#include "buildsym.h"
39#include "demangle.h"
50f182aa 40#include "gdb-demangle.h"
c906108c 41#include "expression.h"
d5166ae1 42#include "filenames.h" /* for DOSish file names */
2e276125 43#include "macrotab.h"
c906108c
SS
44#include "language.h"
45#include "complaints.h"
357e46e7 46#include "bcache.h"
4c2df51b
DJ
47#include "dwarf2expr.h"
48#include "dwarf2loc.h"
9219021c 49#include "cp-support.h"
72bf9492 50#include "hashtab.h"
ae038cb0
DJ
51#include "command.h"
52#include "gdbcmd.h"
edb3359d 53#include "block.h"
ff013f42 54#include "addrmap.h"
94af9270
KS
55#include "typeprint.h"
56#include "jv-lang.h"
ccefe4c4 57#include "psympriv.h"
9291a0cd
TT
58#include "exceptions.h"
59#include "gdb_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"
4c2df51b 73
c906108c
SS
74#include <fcntl.h>
75#include "gdb_string.h"
4bdf3d34 76#include "gdb_assert.h"
c906108c 77#include <sys/types.h>
d8151005 78
34eaf542
TT
79typedef struct symbol *symbolp;
80DEF_VEC_P (symbolp);
81
45cfd468
DE
82/* When non-zero, print basic high level tracing messages.
83 This is in contrast to the low level DIE reading of dwarf2_die_debug. */
84static int dwarf2_read_debug = 0;
85
d97bc12b 86/* When non-zero, dump DIEs after they are read in. */
ccce17b0 87static unsigned int dwarf2_die_debug = 0;
d97bc12b 88
900e11f9
JK
89/* When non-zero, cross-check physname against demangler. */
90static int check_physname = 0;
91
481860b3 92/* When non-zero, do not reject deprecated .gdb_index sections. */
e615022a 93static int use_deprecated_index_sections = 0;
481860b3 94
6502dd73
DJ
95static const struct objfile_data *dwarf2_objfile_data_key;
96
f1e6e072
TT
97/* The "aclass" indices for various kinds of computed DWARF symbols. */
98
99static int dwarf2_locexpr_index;
100static int dwarf2_loclist_index;
101static int dwarf2_locexpr_block_index;
102static int dwarf2_loclist_block_index;
103
73869dc2
DE
104/* A descriptor for dwarf sections.
105
106 S.ASECTION, SIZE are typically initialized when the objfile is first
107 scanned. BUFFER, READIN are filled in later when the section is read.
108 If the section contained compressed data then SIZE is updated to record
109 the uncompressed size of the section.
110
111 DWP file format V2 introduces a wrinkle that is easiest to handle by
112 creating the concept of virtual sections contained within a real section.
113 In DWP V2 the sections of the input DWO files are concatenated together
114 into one section, but section offsets are kept relative to the original
115 input section.
116 If this is a virtual dwp-v2 section, S.CONTAINING_SECTION is a backlink to
117 the real section this "virtual" section is contained in, and BUFFER,SIZE
118 describe the virtual section. */
119
dce234bc
PP
120struct dwarf2_section_info
121{
73869dc2
DE
122 union
123 {
e5aa3347 124 /* If this is a real section, the bfd section. */
73869dc2
DE
125 asection *asection;
126 /* If this is a virtual section, pointer to the containing ("real")
e5aa3347 127 section. */
73869dc2
DE
128 struct dwarf2_section_info *containing_section;
129 } s;
19ac8c2e 130 /* Pointer to section data, only valid if readin. */
d521ce57 131 const gdb_byte *buffer;
73869dc2 132 /* The size of the section, real or virtual. */
dce234bc 133 bfd_size_type size;
73869dc2
DE
134 /* If this is a virtual section, the offset in the real section.
135 Only valid if is_virtual. */
136 bfd_size_type virtual_offset;
be391dca 137 /* True if we have tried to read this section. */
73869dc2
DE
138 char readin;
139 /* True if this is a virtual section, False otherwise.
140 This specifies which of s.asection and s.containing_section to use. */
141 char is_virtual;
dce234bc
PP
142};
143
8b70b953
TT
144typedef struct dwarf2_section_info dwarf2_section_info_def;
145DEF_VEC_O (dwarf2_section_info_def);
146
9291a0cd
TT
147/* All offsets in the index are of this type. It must be
148 architecture-independent. */
149typedef uint32_t offset_type;
150
151DEF_VEC_I (offset_type);
152
156942c7
DE
153/* Ensure only legit values are used. */
154#define DW2_GDB_INDEX_SYMBOL_STATIC_SET_VALUE(cu_index, value) \
155 do { \
156 gdb_assert ((unsigned int) (value) <= 1); \
157 GDB_INDEX_SYMBOL_STATIC_SET_VALUE((cu_index), (value)); \
158 } while (0)
159
160/* Ensure only legit values are used. */
161#define DW2_GDB_INDEX_SYMBOL_KIND_SET_VALUE(cu_index, value) \
162 do { \
163 gdb_assert ((value) >= GDB_INDEX_SYMBOL_KIND_TYPE \
164 && (value) <= GDB_INDEX_SYMBOL_KIND_OTHER); \
165 GDB_INDEX_SYMBOL_KIND_SET_VALUE((cu_index), (value)); \
166 } while (0)
167
168/* Ensure we don't use more than the alloted nuber of bits for the CU. */
169#define DW2_GDB_INDEX_CU_SET_VALUE(cu_index, value) \
170 do { \
171 gdb_assert (((value) & ~GDB_INDEX_CU_MASK) == 0); \
172 GDB_INDEX_CU_SET_VALUE((cu_index), (value)); \
173 } while (0)
174
9291a0cd
TT
175/* A description of the mapped index. The file format is described in
176 a comment by the code that writes the index. */
177struct mapped_index
178{
559a7a62
JK
179 /* Index data format version. */
180 int version;
181
9291a0cd
TT
182 /* The total length of the buffer. */
183 off_t total_size;
b11b1f88 184
9291a0cd
TT
185 /* A pointer to the address table data. */
186 const gdb_byte *address_table;
b11b1f88 187
9291a0cd
TT
188 /* Size of the address table data in bytes. */
189 offset_type address_table_size;
b11b1f88 190
3876f04e
DE
191 /* The symbol table, implemented as a hash table. */
192 const offset_type *symbol_table;
b11b1f88 193
9291a0cd 194 /* Size in slots, each slot is 2 offset_types. */
3876f04e 195 offset_type symbol_table_slots;
b11b1f88 196
9291a0cd
TT
197 /* A pointer to the constant pool. */
198 const char *constant_pool;
199};
200
95554aad
TT
201typedef struct dwarf2_per_cu_data *dwarf2_per_cu_ptr;
202DEF_VEC_P (dwarf2_per_cu_ptr);
203
9cdd5dbd
DE
204/* Collection of data recorded per objfile.
205 This hangs off of dwarf2_objfile_data_key. */
206
6502dd73
DJ
207struct dwarf2_per_objfile
208{
dce234bc
PP
209 struct dwarf2_section_info info;
210 struct dwarf2_section_info abbrev;
211 struct dwarf2_section_info line;
dce234bc
PP
212 struct dwarf2_section_info loc;
213 struct dwarf2_section_info macinfo;
cf2c3c16 214 struct dwarf2_section_info macro;
dce234bc
PP
215 struct dwarf2_section_info str;
216 struct dwarf2_section_info ranges;
3019eac3 217 struct dwarf2_section_info addr;
dce234bc
PP
218 struct dwarf2_section_info frame;
219 struct dwarf2_section_info eh_frame;
9291a0cd 220 struct dwarf2_section_info gdb_index;
ae038cb0 221
8b70b953
TT
222 VEC (dwarf2_section_info_def) *types;
223
be391dca
TT
224 /* Back link. */
225 struct objfile *objfile;
226
d467dd73 227 /* Table of all the compilation units. This is used to locate
10b3939b 228 the target compilation unit of a particular reference. */
ae038cb0
DJ
229 struct dwarf2_per_cu_data **all_comp_units;
230
231 /* The number of compilation units in ALL_COMP_UNITS. */
232 int n_comp_units;
233
1fd400ff 234 /* The number of .debug_types-related CUs. */
d467dd73 235 int n_type_units;
1fd400ff 236
a2ce51a0
DE
237 /* The .debug_types-related CUs (TUs).
238 This is stored in malloc space because we may realloc it. */
b4dd5633 239 struct signatured_type **all_type_units;
1fd400ff 240
f4dc4d17
DE
241 /* The number of entries in all_type_unit_groups. */
242 int n_type_unit_groups;
243
244 /* Table of type unit groups.
245 This exists to make it easy to iterate over all CUs and TU groups. */
246 struct type_unit_group **all_type_unit_groups;
247
248 /* Table of struct type_unit_group objects.
249 The hash key is the DW_AT_stmt_list value. */
250 htab_t type_unit_groups;
72dca2f5 251
348e048f
DE
252 /* A table mapping .debug_types signatures to its signatured_type entry.
253 This is NULL if the .debug_types section hasn't been read in yet. */
254 htab_t signatured_types;
255
f4dc4d17
DE
256 /* Type unit statistics, to see how well the scaling improvements
257 are doing. */
258 struct tu_stats
259 {
260 int nr_uniq_abbrev_tables;
261 int nr_symtabs;
262 int nr_symtab_sharers;
263 int nr_stmt_less_type_units;
264 } tu_stats;
265
266 /* A chain of compilation units that are currently read in, so that
267 they can be freed later. */
268 struct dwarf2_per_cu_data *read_in_chain;
269
3019eac3
DE
270 /* A table mapping DW_AT_dwo_name values to struct dwo_file objects.
271 This is NULL if the table hasn't been allocated yet. */
272 htab_t dwo_files;
273
80626a55
DE
274 /* Non-zero if we've check for whether there is a DWP file. */
275 int dwp_checked;
276
277 /* The DWP file if there is one, or NULL. */
278 struct dwp_file *dwp_file;
279
36586728
TT
280 /* The shared '.dwz' file, if one exists. This is used when the
281 original data was compressed using 'dwz -m'. */
282 struct dwz_file *dwz_file;
283
72dca2f5
FR
284 /* A flag indicating wether this objfile has a section loaded at a
285 VMA of 0. */
286 int has_section_at_zero;
9291a0cd 287
ae2de4f8
DE
288 /* True if we are using the mapped index,
289 or we are faking it for OBJF_READNOW's sake. */
9291a0cd
TT
290 unsigned char using_index;
291
ae2de4f8 292 /* The mapped index, or NULL if .gdb_index is missing or not being used. */
9291a0cd 293 struct mapped_index *index_table;
98bfdba5 294
7b9f3c50 295 /* When using index_table, this keeps track of all quick_file_names entries.
56e64610
DE
296 TUs typically share line table entries with a CU, so we maintain a
297 separate table of all line table entries to support the sharing.
298 Note that while there can be way more TUs than CUs, we've already
299 sorted all the TUs into "type unit groups", grouped by their
300 DW_AT_stmt_list value. Therefore the only sharing done here is with a
301 CU and its associated TU group if there is one. */
7b9f3c50
DE
302 htab_t quick_file_names_table;
303
98bfdba5
PA
304 /* Set during partial symbol reading, to prevent queueing of full
305 symbols. */
306 int reading_partial_symbols;
673bfd45 307
dee91e82 308 /* Table mapping type DIEs to their struct type *.
673bfd45 309 This is NULL if not allocated yet.
02142a6c 310 The mapping is done via (CU/TU + DIE offset) -> type. */
dee91e82 311 htab_t die_type_hash;
95554aad
TT
312
313 /* The CUs we recently read. */
314 VEC (dwarf2_per_cu_ptr) *just_read_cus;
6502dd73
DJ
315};
316
317static struct dwarf2_per_objfile *dwarf2_per_objfile;
c906108c 318
251d32d9 319/* Default names of the debugging sections. */
c906108c 320
233a11ab
CS
321/* Note that if the debugging section has been compressed, it might
322 have a name like .zdebug_info. */
323
9cdd5dbd
DE
324static const struct dwarf2_debug_sections dwarf2_elf_names =
325{
251d32d9
TG
326 { ".debug_info", ".zdebug_info" },
327 { ".debug_abbrev", ".zdebug_abbrev" },
328 { ".debug_line", ".zdebug_line" },
329 { ".debug_loc", ".zdebug_loc" },
330 { ".debug_macinfo", ".zdebug_macinfo" },
cf2c3c16 331 { ".debug_macro", ".zdebug_macro" },
251d32d9
TG
332 { ".debug_str", ".zdebug_str" },
333 { ".debug_ranges", ".zdebug_ranges" },
334 { ".debug_types", ".zdebug_types" },
3019eac3 335 { ".debug_addr", ".zdebug_addr" },
251d32d9
TG
336 { ".debug_frame", ".zdebug_frame" },
337 { ".eh_frame", NULL },
24d3216f
TT
338 { ".gdb_index", ".zgdb_index" },
339 23
251d32d9 340};
c906108c 341
80626a55 342/* List of DWO/DWP sections. */
3019eac3 343
80626a55 344static const struct dwop_section_names
3019eac3
DE
345{
346 struct dwarf2_section_names abbrev_dwo;
347 struct dwarf2_section_names info_dwo;
348 struct dwarf2_section_names line_dwo;
349 struct dwarf2_section_names loc_dwo;
09262596
DE
350 struct dwarf2_section_names macinfo_dwo;
351 struct dwarf2_section_names macro_dwo;
3019eac3
DE
352 struct dwarf2_section_names str_dwo;
353 struct dwarf2_section_names str_offsets_dwo;
354 struct dwarf2_section_names types_dwo;
80626a55
DE
355 struct dwarf2_section_names cu_index;
356 struct dwarf2_section_names tu_index;
3019eac3 357}
80626a55 358dwop_section_names =
3019eac3
DE
359{
360 { ".debug_abbrev.dwo", ".zdebug_abbrev.dwo" },
361 { ".debug_info.dwo", ".zdebug_info.dwo" },
362 { ".debug_line.dwo", ".zdebug_line.dwo" },
363 { ".debug_loc.dwo", ".zdebug_loc.dwo" },
09262596
DE
364 { ".debug_macinfo.dwo", ".zdebug_macinfo.dwo" },
365 { ".debug_macro.dwo", ".zdebug_macro.dwo" },
3019eac3
DE
366 { ".debug_str.dwo", ".zdebug_str.dwo" },
367 { ".debug_str_offsets.dwo", ".zdebug_str_offsets.dwo" },
368 { ".debug_types.dwo", ".zdebug_types.dwo" },
80626a55
DE
369 { ".debug_cu_index", ".zdebug_cu_index" },
370 { ".debug_tu_index", ".zdebug_tu_index" },
3019eac3
DE
371};
372
c906108c
SS
373/* local data types */
374
107d2387
AC
375/* The data in a compilation unit header, after target2host
376 translation, looks like this. */
c906108c 377struct comp_unit_head
a738430d 378{
c764a876 379 unsigned int length;
a738430d 380 short version;
a738430d
MK
381 unsigned char addr_size;
382 unsigned char signed_addr_p;
b64f50a1 383 sect_offset abbrev_offset;
57349743 384
a738430d
MK
385 /* Size of file offsets; either 4 or 8. */
386 unsigned int offset_size;
57349743 387
a738430d
MK
388 /* Size of the length field; either 4 or 12. */
389 unsigned int initial_length_size;
57349743 390
a738430d
MK
391 /* Offset to the first byte of this compilation unit header in the
392 .debug_info section, for resolving relative reference dies. */
b64f50a1 393 sect_offset offset;
57349743 394
d00adf39
DE
395 /* Offset to first die in this cu from the start of the cu.
396 This will be the first byte following the compilation unit header. */
b64f50a1 397 cu_offset first_die_offset;
a738430d 398};
c906108c 399
3da10d80
KS
400/* Type used for delaying computation of method physnames.
401 See comments for compute_delayed_physnames. */
402struct delayed_method_info
403{
404 /* The type to which the method is attached, i.e., its parent class. */
405 struct type *type;
406
407 /* The index of the method in the type's function fieldlists. */
408 int fnfield_index;
409
410 /* The index of the method in the fieldlist. */
411 int index;
412
413 /* The name of the DIE. */
414 const char *name;
415
416 /* The DIE associated with this method. */
417 struct die_info *die;
418};
419
420typedef struct delayed_method_info delayed_method_info;
421DEF_VEC_O (delayed_method_info);
422
e7c27a73
DJ
423/* Internal state when decoding a particular compilation unit. */
424struct dwarf2_cu
425{
426 /* The objfile containing this compilation unit. */
427 struct objfile *objfile;
428
d00adf39 429 /* The header of the compilation unit. */
e7c27a73 430 struct comp_unit_head header;
e142c38c 431
d00adf39
DE
432 /* Base address of this compilation unit. */
433 CORE_ADDR base_address;
434
435 /* Non-zero if base_address has been set. */
436 int base_known;
437
e142c38c
DJ
438 /* The language we are debugging. */
439 enum language language;
440 const struct language_defn *language_defn;
441
b0f35d58
DL
442 const char *producer;
443
e142c38c
DJ
444 /* The generic symbol table building routines have separate lists for
445 file scope symbols and all all other scopes (local scopes). So
446 we need to select the right one to pass to add_symbol_to_list().
447 We do it by keeping a pointer to the correct list in list_in_scope.
448
449 FIXME: The original dwarf code just treated the file scope as the
450 first local scope, and all other local scopes as nested local
451 scopes, and worked fine. Check to see if we really need to
452 distinguish these in buildsym.c. */
453 struct pending **list_in_scope;
454
433df2d4
DE
455 /* The abbrev table for this CU.
456 Normally this points to the abbrev table in the objfile.
457 But if DWO_UNIT is non-NULL this is the abbrev table in the DWO file. */
458 struct abbrev_table *abbrev_table;
72bf9492 459
b64f50a1
JK
460 /* Hash table holding all the loaded partial DIEs
461 with partial_die->offset.SECT_OFF as hash. */
72bf9492
DJ
462 htab_t partial_dies;
463
464 /* Storage for things with the same lifetime as this read-in compilation
465 unit, including partial DIEs. */
466 struct obstack comp_unit_obstack;
467
ae038cb0
DJ
468 /* When multiple dwarf2_cu structures are living in memory, this field
469 chains them all together, so that they can be released efficiently.
470 We will probably also want a generation counter so that most-recently-used
471 compilation units are cached... */
472 struct dwarf2_per_cu_data *read_in_chain;
473
69d751e3 474 /* Backlink to our per_cu entry. */
ae038cb0
DJ
475 struct dwarf2_per_cu_data *per_cu;
476
477 /* How many compilation units ago was this CU last referenced? */
478 int last_used;
479
b64f50a1
JK
480 /* A hash table of DIE cu_offset for following references with
481 die_info->offset.sect_off as hash. */
51545339 482 htab_t die_hash;
10b3939b
DJ
483
484 /* Full DIEs if read in. */
485 struct die_info *dies;
486
487 /* A set of pointers to dwarf2_per_cu_data objects for compilation
488 units referenced by this one. Only set during full symbol processing;
489 partial symbol tables do not have dependencies. */
490 htab_t dependencies;
491
cb1df416
DJ
492 /* Header data from the line table, during full symbol processing. */
493 struct line_header *line_header;
494
3da10d80
KS
495 /* A list of methods which need to have physnames computed
496 after all type information has been read. */
497 VEC (delayed_method_info) *method_list;
498
96408a79
SA
499 /* To be copied to symtab->call_site_htab. */
500 htab_t call_site_htab;
501
034e5797
DE
502 /* Non-NULL if this CU came from a DWO file.
503 There is an invariant here that is important to remember:
504 Except for attributes copied from the top level DIE in the "main"
505 (or "stub") file in preparation for reading the DWO file
506 (e.g., DW_AT_GNU_addr_base), we KISS: there is only *one* CU.
507 Either there isn't a DWO file (in which case this is NULL and the point
508 is moot), or there is and either we're not going to read it (in which
509 case this is NULL) or there is and we are reading it (in which case this
510 is non-NULL). */
3019eac3
DE
511 struct dwo_unit *dwo_unit;
512
513 /* The DW_AT_addr_base attribute if present, zero otherwise
514 (zero is a valid value though).
515 Note this value comes from the stub CU/TU's DIE. */
516 ULONGEST addr_base;
517
2e3cf129
DE
518 /* The DW_AT_ranges_base attribute if present, zero otherwise
519 (zero is a valid value though).
520 Note this value comes from the stub CU/TU's DIE.
521 Also note that the value is zero in the non-DWO case so this value can
ab435259
DE
522 be used without needing to know whether DWO files are in use or not.
523 N.B. This does not apply to DW_AT_ranges appearing in
524 DW_TAG_compile_unit dies. This is a bit of a wart, consider if ever
525 DW_AT_ranges appeared in the DW_TAG_compile_unit of DWO DIEs: then
526 DW_AT_ranges_base *would* have to be applied, and we'd have to care
527 whether the DW_AT_ranges attribute came from the skeleton or DWO. */
2e3cf129
DE
528 ULONGEST ranges_base;
529
ae038cb0
DJ
530 /* Mark used when releasing cached dies. */
531 unsigned int mark : 1;
532
8be455d7
JK
533 /* This CU references .debug_loc. See the symtab->locations_valid field.
534 This test is imperfect as there may exist optimized debug code not using
535 any location list and still facing inlining issues if handled as
536 unoptimized code. For a future better test see GCC PR other/32998. */
8be455d7 537 unsigned int has_loclist : 1;
ba919b58 538
1b80a9fa
JK
539 /* These cache the results for producer_is_* fields. CHECKED_PRODUCER is set
540 if all the producer_is_* fields are valid. This information is cached
541 because profiling CU expansion showed excessive time spent in
542 producer_is_gxx_lt_4_6. */
ba919b58
TT
543 unsigned int checked_producer : 1;
544 unsigned int producer_is_gxx_lt_4_6 : 1;
1b80a9fa 545 unsigned int producer_is_gcc_lt_4_3 : 1;
685b1105 546 unsigned int producer_is_icc : 1;
4d4ec4e5
TT
547
548 /* When set, the file that we're processing is known to have
549 debugging info for C++ namespaces. GCC 3.3.x did not produce
550 this information, but later versions do. */
551
552 unsigned int processing_has_namespace_info : 1;
e7c27a73
DJ
553};
554
10b3939b
DJ
555/* Persistent data held for a compilation unit, even when not
556 processing it. We put a pointer to this structure in the
28dee7f5 557 read_symtab_private field of the psymtab. */
10b3939b 558
ae038cb0
DJ
559struct dwarf2_per_cu_data
560{
36586728 561 /* The start offset and length of this compilation unit.
45452591 562 NOTE: Unlike comp_unit_head.length, this length includes
3019eac3
DE
563 initial_length_size.
564 If the DIE refers to a DWO file, this is always of the original die,
565 not the DWO file. */
b64f50a1 566 sect_offset offset;
36586728 567 unsigned int length;
ae038cb0
DJ
568
569 /* Flag indicating this compilation unit will be read in before
570 any of the current compilation units are processed. */
c764a876 571 unsigned int queued : 1;
ae038cb0 572
0d99eb77
DE
573 /* This flag will be set when reading partial DIEs if we need to load
574 absolutely all DIEs for this compilation unit, instead of just the ones
575 we think are interesting. It gets set if we look for a DIE in the
5afb4e99
DJ
576 hash table and don't find it. */
577 unsigned int load_all_dies : 1;
578
0186c6a7
DE
579 /* Non-zero if this CU is from .debug_types.
580 Struct dwarf2_per_cu_data is contained in struct signatured_type iff
581 this is non-zero. */
3019eac3
DE
582 unsigned int is_debug_types : 1;
583
36586728
TT
584 /* Non-zero if this CU is from the .dwz file. */
585 unsigned int is_dwz : 1;
586
a2ce51a0
DE
587 /* Non-zero if reading a TU directly from a DWO file, bypassing the stub.
588 This flag is only valid if is_debug_types is true.
589 We can't read a CU directly from a DWO file: There are required
590 attributes in the stub. */
591 unsigned int reading_dwo_directly : 1;
592
7ee85ab1
DE
593 /* Non-zero if the TU has been read.
594 This is used to assist the "Stay in DWO Optimization" for Fission:
595 When reading a DWO, it's faster to read TUs from the DWO instead of
596 fetching them from random other DWOs (due to comdat folding).
597 If the TU has already been read, the optimization is unnecessary
598 (and unwise - we don't want to change where gdb thinks the TU lives
599 "midflight").
600 This flag is only valid if is_debug_types is true. */
601 unsigned int tu_read : 1;
602
3019eac3
DE
603 /* The section this CU/TU lives in.
604 If the DIE refers to a DWO file, this is always the original die,
605 not the DWO file. */
8a0459fd 606 struct dwarf2_section_info *section;
348e048f 607
17ea53c3
JK
608 /* Set to non-NULL iff this CU is currently loaded. When it gets freed out
609 of the CU cache it gets reset to NULL again. */
ae038cb0 610 struct dwarf2_cu *cu;
1c379e20 611
9cdd5dbd
DE
612 /* The corresponding objfile.
613 Normally we can get the objfile from dwarf2_per_objfile.
614 However we can enter this file with just a "per_cu" handle. */
9291a0cd
TT
615 struct objfile *objfile;
616
617 /* When using partial symbol tables, the 'psymtab' field is active.
618 Otherwise the 'quick' field is active. */
619 union
620 {
621 /* The partial symbol table associated with this compilation unit,
95554aad 622 or NULL for unread partial units. */
9291a0cd
TT
623 struct partial_symtab *psymtab;
624
625 /* Data needed by the "quick" functions. */
626 struct dwarf2_per_cu_quick_data *quick;
627 } v;
95554aad 628
796a7ff8
DE
629 /* The CUs we import using DW_TAG_imported_unit. This is filled in
630 while reading psymtabs, used to compute the psymtab dependencies,
631 and then cleared. Then it is filled in again while reading full
632 symbols, and only deleted when the objfile is destroyed.
633
634 This is also used to work around a difference between the way gold
635 generates .gdb_index version <=7 and the way gdb does. Arguably this
636 is a gold bug. For symbols coming from TUs, gold records in the index
637 the CU that includes the TU instead of the TU itself. This breaks
638 dw2_lookup_symbol: It assumes that if the index says symbol X lives
639 in CU/TU Y, then one need only expand Y and a subsequent lookup in Y
640 will find X. Alas TUs live in their own symtab, so after expanding CU Y
641 we need to look in TU Z to find X. Fortunately, this is akin to
642 DW_TAG_imported_unit, so we just use the same mechanism: For
643 .gdb_index version <=7 this also records the TUs that the CU referred
644 to. Concurrently with this change gdb was modified to emit version 8
69d751e3
DE
645 indices so we only pay a price for gold generated indices.
646 http://sourceware.org/bugzilla/show_bug.cgi?id=15021. */
796a7ff8 647 VEC (dwarf2_per_cu_ptr) *imported_symtabs;
ae038cb0
DJ
648};
649
348e048f
DE
650/* Entry in the signatured_types hash table. */
651
652struct signatured_type
653{
42e7ad6c 654 /* The "per_cu" object of this type.
ac9ec31b 655 This struct is used iff per_cu.is_debug_types.
42e7ad6c
DE
656 N.B.: This is the first member so that it's easy to convert pointers
657 between them. */
658 struct dwarf2_per_cu_data per_cu;
659
3019eac3 660 /* The type's signature. */
348e048f
DE
661 ULONGEST signature;
662
3019eac3 663 /* Offset in the TU of the type's DIE, as read from the TU header.
c88ee1f0
DE
664 If this TU is a DWO stub and the definition lives in a DWO file
665 (specified by DW_AT_GNU_dwo_name), this value is unusable. */
3019eac3
DE
666 cu_offset type_offset_in_tu;
667
668 /* Offset in the section of the type's DIE.
669 If the definition lives in a DWO file, this is the offset in the
670 .debug_types.dwo section.
671 The value is zero until the actual value is known.
672 Zero is otherwise not a valid section offset. */
673 sect_offset type_offset_in_section;
0186c6a7
DE
674
675 /* Type units are grouped by their DW_AT_stmt_list entry so that they
676 can share them. This points to the containing symtab. */
677 struct type_unit_group *type_unit_group;
ac9ec31b
DE
678
679 /* The type.
680 The first time we encounter this type we fully read it in and install it
681 in the symbol tables. Subsequent times we only need the type. */
682 struct type *type;
a2ce51a0
DE
683
684 /* Containing DWO unit.
685 This field is valid iff per_cu.reading_dwo_directly. */
686 struct dwo_unit *dwo_unit;
348e048f
DE
687};
688
0186c6a7
DE
689typedef struct signatured_type *sig_type_ptr;
690DEF_VEC_P (sig_type_ptr);
691
094b34ac
DE
692/* A struct that can be used as a hash key for tables based on DW_AT_stmt_list.
693 This includes type_unit_group and quick_file_names. */
694
695struct stmt_list_hash
696{
697 /* The DWO unit this table is from or NULL if there is none. */
698 struct dwo_unit *dwo_unit;
699
700 /* Offset in .debug_line or .debug_line.dwo. */
701 sect_offset line_offset;
702};
703
f4dc4d17
DE
704/* Each element of dwarf2_per_objfile->type_unit_groups is a pointer to
705 an object of this type. */
706
707struct type_unit_group
708{
0186c6a7 709 /* dwarf2read.c's main "handle" on a TU symtab.
f4dc4d17
DE
710 To simplify things we create an artificial CU that "includes" all the
711 type units using this stmt_list so that the rest of the code still has
712 a "per_cu" handle on the symtab.
713 This PER_CU is recognized by having no section. */
8a0459fd 714#define IS_TYPE_UNIT_GROUP(per_cu) ((per_cu)->section == NULL)
094b34ac
DE
715 struct dwarf2_per_cu_data per_cu;
716
0186c6a7
DE
717 /* The TUs that share this DW_AT_stmt_list entry.
718 This is added to while parsing type units to build partial symtabs,
719 and is deleted afterwards and not used again. */
720 VEC (sig_type_ptr) *tus;
f4dc4d17
DE
721
722 /* The primary symtab.
094b34ac
DE
723 Type units in a group needn't all be defined in the same source file,
724 so we create an essentially anonymous symtab as the primary symtab. */
f4dc4d17
DE
725 struct symtab *primary_symtab;
726
094b34ac
DE
727 /* The data used to construct the hash key. */
728 struct stmt_list_hash hash;
f4dc4d17
DE
729
730 /* The number of symtabs from the line header.
731 The value here must match line_header.num_file_names. */
732 unsigned int num_symtabs;
733
734 /* The symbol tables for this TU (obtained from the files listed in
735 DW_AT_stmt_list).
736 WARNING: The order of entries here must match the order of entries
737 in the line header. After the first TU using this type_unit_group, the
738 line header for the subsequent TUs is recreated from this. This is done
739 because we need to use the same symtabs for each TU using the same
740 DW_AT_stmt_list value. Also note that symtabs may be repeated here,
741 there's no guarantee the line header doesn't have duplicate entries. */
742 struct symtab **symtabs;
743};
744
73869dc2 745/* These sections are what may appear in a (real or virtual) DWO file. */
3019eac3
DE
746
747struct dwo_sections
748{
749 struct dwarf2_section_info abbrev;
3019eac3
DE
750 struct dwarf2_section_info line;
751 struct dwarf2_section_info loc;
09262596
DE
752 struct dwarf2_section_info macinfo;
753 struct dwarf2_section_info macro;
3019eac3
DE
754 struct dwarf2_section_info str;
755 struct dwarf2_section_info str_offsets;
80626a55
DE
756 /* In the case of a virtual DWO file, these two are unused. */
757 struct dwarf2_section_info info;
3019eac3
DE
758 VEC (dwarf2_section_info_def) *types;
759};
760
c88ee1f0 761/* CUs/TUs in DWP/DWO files. */
3019eac3
DE
762
763struct dwo_unit
764{
765 /* Backlink to the containing struct dwo_file. */
766 struct dwo_file *dwo_file;
767
768 /* The "id" that distinguishes this CU/TU.
769 .debug_info calls this "dwo_id", .debug_types calls this "signature".
770 Since signatures came first, we stick with it for consistency. */
771 ULONGEST signature;
772
773 /* The section this CU/TU lives in, in the DWO file. */
8a0459fd 774 struct dwarf2_section_info *section;
3019eac3 775
19ac8c2e 776 /* Same as dwarf2_per_cu_data:{offset,length} but in the DWO section. */
3019eac3
DE
777 sect_offset offset;
778 unsigned int length;
779
780 /* For types, offset in the type's DIE of the type defined by this TU. */
781 cu_offset type_offset_in_tu;
782};
783
73869dc2
DE
784/* include/dwarf2.h defines the DWP section codes.
785 It defines a max value but it doesn't define a min value, which we
786 use for error checking, so provide one. */
787
788enum dwp_v2_section_ids
789{
790 DW_SECT_MIN = 1
791};
792
80626a55 793/* Data for one DWO file.
57d63ce2
DE
794
795 This includes virtual DWO files (a virtual DWO file is a DWO file as it
796 appears in a DWP file). DWP files don't really have DWO files per se -
797 comdat folding of types "loses" the DWO file they came from, and from
798 a high level view DWP files appear to contain a mass of random types.
799 However, to maintain consistency with the non-DWP case we pretend DWP
800 files contain virtual DWO files, and we assign each TU with one virtual
801 DWO file (generally based on the line and abbrev section offsets -
802 a heuristic that seems to work in practice). */
3019eac3
DE
803
804struct dwo_file
805{
0ac5b59e 806 /* The DW_AT_GNU_dwo_name attribute.
80626a55
DE
807 For virtual DWO files the name is constructed from the section offsets
808 of abbrev,line,loc,str_offsets so that we combine virtual DWO files
809 from related CU+TUs. */
0ac5b59e
DE
810 const char *dwo_name;
811
812 /* The DW_AT_comp_dir attribute. */
813 const char *comp_dir;
3019eac3 814
80626a55
DE
815 /* The bfd, when the file is open. Otherwise this is NULL.
816 This is unused(NULL) for virtual DWO files where we use dwp_file.dbfd. */
817 bfd *dbfd;
3019eac3 818
73869dc2
DE
819 /* The sections that make up this DWO file.
820 Remember that for virtual DWO files in DWP V2, these are virtual
821 sections (for lack of a better name). */
3019eac3
DE
822 struct dwo_sections sections;
823
19c3d4c9
DE
824 /* The CU in the file.
825 We only support one because having more than one requires hacking the
826 dwo_name of each to match, which is highly unlikely to happen.
827 Doing this means all TUs can share comp_dir: We also assume that
828 DW_AT_comp_dir across all TUs in a DWO file will be identical. */
829 struct dwo_unit *cu;
3019eac3
DE
830
831 /* Table of TUs in the file.
832 Each element is a struct dwo_unit. */
833 htab_t tus;
834};
835
80626a55
DE
836/* These sections are what may appear in a DWP file. */
837
838struct dwp_sections
839{
73869dc2 840 /* These are used by both DWP version 1 and 2. */
80626a55
DE
841 struct dwarf2_section_info str;
842 struct dwarf2_section_info cu_index;
843 struct dwarf2_section_info tu_index;
73869dc2
DE
844
845 /* These are only used by DWP version 2 files.
846 In DWP version 1 the .debug_info.dwo, .debug_types.dwo, and other
847 sections are referenced by section number, and are not recorded here.
848 In DWP version 2 there is at most one copy of all these sections, each
849 section being (effectively) comprised of the concatenation of all of the
850 individual sections that exist in the version 1 format.
851 To keep the code simple we treat each of these concatenated pieces as a
852 section itself (a virtual section?). */
853 struct dwarf2_section_info abbrev;
854 struct dwarf2_section_info info;
855 struct dwarf2_section_info line;
856 struct dwarf2_section_info loc;
857 struct dwarf2_section_info macinfo;
858 struct dwarf2_section_info macro;
859 struct dwarf2_section_info str_offsets;
860 struct dwarf2_section_info types;
80626a55
DE
861};
862
73869dc2
DE
863/* These sections are what may appear in a virtual DWO file in DWP version 1.
864 A virtual DWO file is a DWO file as it appears in a DWP file. */
80626a55 865
73869dc2 866struct virtual_v1_dwo_sections
80626a55
DE
867{
868 struct dwarf2_section_info abbrev;
869 struct dwarf2_section_info line;
870 struct dwarf2_section_info loc;
871 struct dwarf2_section_info macinfo;
872 struct dwarf2_section_info macro;
873 struct dwarf2_section_info str_offsets;
874 /* Each DWP hash table entry records one CU or one TU.
8a0459fd 875 That is recorded here, and copied to dwo_unit.section. */
80626a55
DE
876 struct dwarf2_section_info info_or_types;
877};
878
73869dc2
DE
879/* Similar to virtual_v1_dwo_sections, but for DWP version 2.
880 In version 2, the sections of the DWO files are concatenated together
881 and stored in one section of that name. Thus each ELF section contains
882 several "virtual" sections. */
883
884struct virtual_v2_dwo_sections
885{
886 bfd_size_type abbrev_offset;
887 bfd_size_type abbrev_size;
888
889 bfd_size_type line_offset;
890 bfd_size_type line_size;
891
892 bfd_size_type loc_offset;
893 bfd_size_type loc_size;
894
895 bfd_size_type macinfo_offset;
896 bfd_size_type macinfo_size;
897
898 bfd_size_type macro_offset;
899 bfd_size_type macro_size;
900
901 bfd_size_type str_offsets_offset;
902 bfd_size_type str_offsets_size;
903
904 /* Each DWP hash table entry records one CU or one TU.
905 That is recorded here, and copied to dwo_unit.section. */
906 bfd_size_type info_or_types_offset;
907 bfd_size_type info_or_types_size;
908};
909
80626a55
DE
910/* Contents of DWP hash tables. */
911
912struct dwp_hash_table
913{
73869dc2 914 uint32_t version, nr_columns;
80626a55 915 uint32_t nr_units, nr_slots;
73869dc2
DE
916 const gdb_byte *hash_table, *unit_table;
917 union
918 {
919 struct
920 {
921 const gdb_byte *indices;
922 } v1;
923 struct
924 {
925 /* This is indexed by column number and gives the id of the section
926 in that column. */
927#define MAX_NR_V2_DWO_SECTIONS \
928 (1 /* .debug_info or .debug_types */ \
929 + 1 /* .debug_abbrev */ \
930 + 1 /* .debug_line */ \
931 + 1 /* .debug_loc */ \
932 + 1 /* .debug_str_offsets */ \
933 + 1 /* .debug_macro or .debug_macinfo */)
934 int section_ids[MAX_NR_V2_DWO_SECTIONS];
935 const gdb_byte *offsets;
936 const gdb_byte *sizes;
937 } v2;
938 } section_pool;
80626a55
DE
939};
940
941/* Data for one DWP file. */
942
943struct dwp_file
944{
945 /* Name of the file. */
946 const char *name;
947
73869dc2
DE
948 /* File format version. */
949 int version;
950
93417882 951 /* The bfd. */
80626a55
DE
952 bfd *dbfd;
953
954 /* Section info for this file. */
955 struct dwp_sections sections;
956
57d63ce2 957 /* Table of CUs in the file. */
80626a55
DE
958 const struct dwp_hash_table *cus;
959
960 /* Table of TUs in the file. */
961 const struct dwp_hash_table *tus;
962
19ac8c2e
DE
963 /* Tables of loaded CUs/TUs. Each entry is a struct dwo_unit *. */
964 htab_t loaded_cus;
965 htab_t loaded_tus;
80626a55 966
73869dc2
DE
967 /* Table to map ELF section numbers to their sections.
968 This is only needed for the DWP V1 file format. */
80626a55
DE
969 unsigned int num_sections;
970 asection **elf_sections;
971};
972
36586728
TT
973/* This represents a '.dwz' file. */
974
975struct dwz_file
976{
977 /* A dwz file can only contain a few sections. */
978 struct dwarf2_section_info abbrev;
979 struct dwarf2_section_info info;
980 struct dwarf2_section_info str;
981 struct dwarf2_section_info line;
982 struct dwarf2_section_info macro;
2ec9a5e0 983 struct dwarf2_section_info gdb_index;
36586728
TT
984
985 /* The dwz's BFD. */
986 bfd *dwz_bfd;
987};
988
0963b4bd
MS
989/* Struct used to pass misc. parameters to read_die_and_children, et
990 al. which are used for both .debug_info and .debug_types dies.
991 All parameters here are unchanging for the life of the call. This
dee91e82 992 struct exists to abstract away the constant parameters of die reading. */
93311388
DE
993
994struct die_reader_specs
995{
a32a8923 996 /* The bfd of die_section. */
93311388
DE
997 bfd* abfd;
998
999 /* The CU of the DIE we are parsing. */
1000 struct dwarf2_cu *cu;
1001
80626a55 1002 /* Non-NULL if reading a DWO file (including one packaged into a DWP). */
3019eac3
DE
1003 struct dwo_file *dwo_file;
1004
dee91e82 1005 /* The section the die comes from.
3019eac3 1006 This is either .debug_info or .debug_types, or the .dwo variants. */
dee91e82
DE
1007 struct dwarf2_section_info *die_section;
1008
1009 /* die_section->buffer. */
d521ce57 1010 const gdb_byte *buffer;
f664829e
DE
1011
1012 /* The end of the buffer. */
1013 const gdb_byte *buffer_end;
a2ce51a0
DE
1014
1015 /* The value of the DW_AT_comp_dir attribute. */
1016 const char *comp_dir;
93311388
DE
1017};
1018
fd820528 1019/* Type of function passed to init_cutu_and_read_dies, et.al. */
dee91e82 1020typedef void (die_reader_func_ftype) (const struct die_reader_specs *reader,
d521ce57 1021 const gdb_byte *info_ptr,
dee91e82
DE
1022 struct die_info *comp_unit_die,
1023 int has_children,
1024 void *data);
1025
debd256d
JB
1026/* The line number information for a compilation unit (found in the
1027 .debug_line section) begins with a "statement program header",
1028 which contains the following information. */
1029struct line_header
1030{
1031 unsigned int total_length;
1032 unsigned short version;
1033 unsigned int header_length;
1034 unsigned char minimum_instruction_length;
2dc7f7b3 1035 unsigned char maximum_ops_per_instruction;
debd256d
JB
1036 unsigned char default_is_stmt;
1037 int line_base;
1038 unsigned char line_range;
1039 unsigned char opcode_base;
1040
1041 /* standard_opcode_lengths[i] is the number of operands for the
1042 standard opcode whose value is i. This means that
1043 standard_opcode_lengths[0] is unused, and the last meaningful
1044 element is standard_opcode_lengths[opcode_base - 1]. */
1045 unsigned char *standard_opcode_lengths;
1046
1047 /* The include_directories table. NOTE! These strings are not
1048 allocated with xmalloc; instead, they are pointers into
1049 debug_line_buffer. If you try to free them, `free' will get
1050 indigestion. */
1051 unsigned int num_include_dirs, include_dirs_size;
d521ce57 1052 const char **include_dirs;
debd256d
JB
1053
1054 /* The file_names table. NOTE! These strings are not allocated
1055 with xmalloc; instead, they are pointers into debug_line_buffer.
1056 Don't try to free them directly. */
1057 unsigned int num_file_names, file_names_size;
1058 struct file_entry
c906108c 1059 {
d521ce57 1060 const char *name;
debd256d
JB
1061 unsigned int dir_index;
1062 unsigned int mod_time;
1063 unsigned int length;
aaa75496 1064 int included_p; /* Non-zero if referenced by the Line Number Program. */
cb1df416 1065 struct symtab *symtab; /* The associated symbol table, if any. */
debd256d
JB
1066 } *file_names;
1067
1068 /* The start and end of the statement program following this
6502dd73 1069 header. These point into dwarf2_per_objfile->line_buffer. */
d521ce57 1070 const gdb_byte *statement_program_start, *statement_program_end;
debd256d 1071};
c906108c
SS
1072
1073/* When we construct a partial symbol table entry we only
0963b4bd 1074 need this much information. */
c906108c
SS
1075struct partial_die_info
1076 {
72bf9492 1077 /* Offset of this DIE. */
b64f50a1 1078 sect_offset offset;
72bf9492
DJ
1079
1080 /* DWARF-2 tag for this DIE. */
1081 ENUM_BITFIELD(dwarf_tag) tag : 16;
1082
72bf9492
DJ
1083 /* Assorted flags describing the data found in this DIE. */
1084 unsigned int has_children : 1;
1085 unsigned int is_external : 1;
1086 unsigned int is_declaration : 1;
1087 unsigned int has_type : 1;
1088 unsigned int has_specification : 1;
1089 unsigned int has_pc_info : 1;
481860b3 1090 unsigned int may_be_inlined : 1;
72bf9492
DJ
1091
1092 /* Flag set if the SCOPE field of this structure has been
1093 computed. */
1094 unsigned int scope_set : 1;
1095
fa4028e9
JB
1096 /* Flag set if the DIE has a byte_size attribute. */
1097 unsigned int has_byte_size : 1;
1098
98bfdba5
PA
1099 /* Flag set if any of the DIE's children are template arguments. */
1100 unsigned int has_template_arguments : 1;
1101
abc72ce4
DE
1102 /* Flag set if fixup_partial_die has been called on this die. */
1103 unsigned int fixup_called : 1;
1104
36586728
TT
1105 /* Flag set if DW_TAG_imported_unit uses DW_FORM_GNU_ref_alt. */
1106 unsigned int is_dwz : 1;
1107
1108 /* Flag set if spec_offset uses DW_FORM_GNU_ref_alt. */
1109 unsigned int spec_is_dwz : 1;
1110
72bf9492 1111 /* The name of this DIE. Normally the value of DW_AT_name, but
94af9270 1112 sometimes a default name for unnamed DIEs. */
15d034d0 1113 const char *name;
72bf9492 1114
abc72ce4
DE
1115 /* The linkage name, if present. */
1116 const char *linkage_name;
1117
72bf9492
DJ
1118 /* The scope to prepend to our children. This is generally
1119 allocated on the comp_unit_obstack, so will disappear
1120 when this compilation unit leaves the cache. */
15d034d0 1121 const char *scope;
72bf9492 1122
95554aad
TT
1123 /* Some data associated with the partial DIE. The tag determines
1124 which field is live. */
1125 union
1126 {
1127 /* The location description associated with this DIE, if any. */
1128 struct dwarf_block *locdesc;
1129 /* The offset of an import, for DW_TAG_imported_unit. */
1130 sect_offset offset;
1131 } d;
72bf9492
DJ
1132
1133 /* If HAS_PC_INFO, the PC range associated with this DIE. */
c906108c
SS
1134 CORE_ADDR lowpc;
1135 CORE_ADDR highpc;
72bf9492 1136
93311388 1137 /* Pointer into the info_buffer (or types_buffer) pointing at the target of
72bf9492 1138 DW_AT_sibling, if any. */
abc72ce4
DE
1139 /* NOTE: This member isn't strictly necessary, read_partial_die could
1140 return DW_AT_sibling values to its caller load_partial_dies. */
d521ce57 1141 const gdb_byte *sibling;
72bf9492
DJ
1142
1143 /* If HAS_SPECIFICATION, the offset of the DIE referred to by
1144 DW_AT_specification (or DW_AT_abstract_origin or
1145 DW_AT_extension). */
b64f50a1 1146 sect_offset spec_offset;
72bf9492
DJ
1147
1148 /* Pointers to this DIE's parent, first child, and next sibling,
1149 if any. */
1150 struct partial_die_info *die_parent, *die_child, *die_sibling;
c906108c
SS
1151 };
1152
0963b4bd 1153/* This data structure holds the information of an abbrev. */
c906108c
SS
1154struct abbrev_info
1155 {
1156 unsigned int number; /* number identifying abbrev */
1157 enum dwarf_tag tag; /* dwarf tag */
f3dd6933
DJ
1158 unsigned short has_children; /* boolean */
1159 unsigned short num_attrs; /* number of attributes */
c906108c
SS
1160 struct attr_abbrev *attrs; /* an array of attribute descriptions */
1161 struct abbrev_info *next; /* next in chain */
1162 };
1163
1164struct attr_abbrev
1165 {
9d25dd43
DE
1166 ENUM_BITFIELD(dwarf_attribute) name : 16;
1167 ENUM_BITFIELD(dwarf_form) form : 16;
c906108c
SS
1168 };
1169
433df2d4
DE
1170/* Size of abbrev_table.abbrev_hash_table. */
1171#define ABBREV_HASH_SIZE 121
1172
1173/* Top level data structure to contain an abbreviation table. */
1174
1175struct abbrev_table
1176{
f4dc4d17
DE
1177 /* Where the abbrev table came from.
1178 This is used as a sanity check when the table is used. */
433df2d4
DE
1179 sect_offset offset;
1180
1181 /* Storage for the abbrev table. */
1182 struct obstack abbrev_obstack;
1183
1184 /* Hash table of abbrevs.
1185 This is an array of size ABBREV_HASH_SIZE allocated in abbrev_obstack.
1186 It could be statically allocated, but the previous code didn't so we
1187 don't either. */
1188 struct abbrev_info **abbrevs;
1189};
1190
0963b4bd 1191/* Attributes have a name and a value. */
b60c80d6
DJ
1192struct attribute
1193 {
9d25dd43 1194 ENUM_BITFIELD(dwarf_attribute) name : 16;
8285870a
JK
1195 ENUM_BITFIELD(dwarf_form) form : 15;
1196
1197 /* Has DW_STRING already been updated by dwarf2_canonicalize_name? This
1198 field should be in u.str (existing only for DW_STRING) but it is kept
1199 here for better struct attribute alignment. */
1200 unsigned int string_is_canonical : 1;
1201
b60c80d6
DJ
1202 union
1203 {
15d034d0 1204 const char *str;
b60c80d6 1205 struct dwarf_block *blk;
43bbcdc2
PH
1206 ULONGEST unsnd;
1207 LONGEST snd;
b60c80d6 1208 CORE_ADDR addr;
ac9ec31b 1209 ULONGEST signature;
b60c80d6
DJ
1210 }
1211 u;
1212 };
1213
0963b4bd 1214/* This data structure holds a complete die structure. */
c906108c
SS
1215struct die_info
1216 {
76815b17
DE
1217 /* DWARF-2 tag for this DIE. */
1218 ENUM_BITFIELD(dwarf_tag) tag : 16;
1219
1220 /* Number of attributes */
98bfdba5
PA
1221 unsigned char num_attrs;
1222
1223 /* True if we're presently building the full type name for the
1224 type derived from this DIE. */
1225 unsigned char building_fullname : 1;
76815b17
DE
1226
1227 /* Abbrev number */
1228 unsigned int abbrev;
1229
93311388 1230 /* Offset in .debug_info or .debug_types section. */
b64f50a1 1231 sect_offset offset;
78ba4af6
JB
1232
1233 /* The dies in a compilation unit form an n-ary tree. PARENT
1234 points to this die's parent; CHILD points to the first child of
1235 this node; and all the children of a given node are chained
4950bc1c 1236 together via their SIBLING fields. */
639d11d3
DC
1237 struct die_info *child; /* Its first child, if any. */
1238 struct die_info *sibling; /* Its next sibling, if any. */
1239 struct die_info *parent; /* Its parent, if any. */
c906108c 1240
b60c80d6
DJ
1241 /* An array of attributes, with NUM_ATTRS elements. There may be
1242 zero, but it's not common and zero-sized arrays are not
1243 sufficiently portable C. */
1244 struct attribute attrs[1];
c906108c
SS
1245 };
1246
0963b4bd 1247/* Get at parts of an attribute structure. */
c906108c
SS
1248
1249#define DW_STRING(attr) ((attr)->u.str)
8285870a 1250#define DW_STRING_IS_CANONICAL(attr) ((attr)->string_is_canonical)
c906108c
SS
1251#define DW_UNSND(attr) ((attr)->u.unsnd)
1252#define DW_BLOCK(attr) ((attr)->u.blk)
1253#define DW_SND(attr) ((attr)->u.snd)
1254#define DW_ADDR(attr) ((attr)->u.addr)
ac9ec31b 1255#define DW_SIGNATURE(attr) ((attr)->u.signature)
c906108c 1256
0963b4bd 1257/* Blocks are a bunch of untyped bytes. */
c906108c
SS
1258struct dwarf_block
1259 {
56eb65bd 1260 size_t size;
1d6edc3c
JK
1261
1262 /* Valid only if SIZE is not zero. */
d521ce57 1263 const gdb_byte *data;
c906108c
SS
1264 };
1265
c906108c
SS
1266#ifndef ATTR_ALLOC_CHUNK
1267#define ATTR_ALLOC_CHUNK 4
1268#endif
1269
c906108c
SS
1270/* Allocate fields for structs, unions and enums in this size. */
1271#ifndef DW_FIELD_ALLOC_CHUNK
1272#define DW_FIELD_ALLOC_CHUNK 4
1273#endif
1274
c906108c
SS
1275/* FIXME: We might want to set this from BFD via bfd_arch_bits_per_byte,
1276 but this would require a corresponding change in unpack_field_as_long
1277 and friends. */
1278static int bits_per_byte = 8;
1279
1280/* The routines that read and process dies for a C struct or C++ class
1281 pass lists of data member fields and lists of member function fields
1282 in an instance of a field_info structure, as defined below. */
1283struct field_info
c5aa993b 1284 {
0963b4bd 1285 /* List of data member and baseclasses fields. */
c5aa993b
JM
1286 struct nextfield
1287 {
1288 struct nextfield *next;
1289 int accessibility;
1290 int virtuality;
1291 struct field field;
1292 }
7d0ccb61 1293 *fields, *baseclasses;
c906108c 1294
7d0ccb61 1295 /* Number of fields (including baseclasses). */
c5aa993b 1296 int nfields;
c906108c 1297
c5aa993b
JM
1298 /* Number of baseclasses. */
1299 int nbaseclasses;
c906108c 1300
c5aa993b
JM
1301 /* Set if the accesibility of one of the fields is not public. */
1302 int non_public_fields;
c906108c 1303
c5aa993b
JM
1304 /* Member function fields array, entries are allocated in the order they
1305 are encountered in the object file. */
1306 struct nextfnfield
1307 {
1308 struct nextfnfield *next;
1309 struct fn_field fnfield;
1310 }
1311 *fnfields;
c906108c 1312
c5aa993b
JM
1313 /* Member function fieldlist array, contains name of possibly overloaded
1314 member function, number of overloaded member functions and a pointer
1315 to the head of the member function field chain. */
1316 struct fnfieldlist
1317 {
15d034d0 1318 const char *name;
c5aa993b
JM
1319 int length;
1320 struct nextfnfield *head;
1321 }
1322 *fnfieldlists;
c906108c 1323
c5aa993b
JM
1324 /* Number of entries in the fnfieldlists array. */
1325 int nfnfields;
98751a41
JK
1326
1327 /* typedefs defined inside this class. TYPEDEF_FIELD_LIST contains head of
1328 a NULL terminated list of TYPEDEF_FIELD_LIST_COUNT elements. */
1329 struct typedef_field_list
1330 {
1331 struct typedef_field field;
1332 struct typedef_field_list *next;
1333 }
1334 *typedef_field_list;
1335 unsigned typedef_field_list_count;
c5aa993b 1336 };
c906108c 1337
10b3939b
DJ
1338/* One item on the queue of compilation units to read in full symbols
1339 for. */
1340struct dwarf2_queue_item
1341{
1342 struct dwarf2_per_cu_data *per_cu;
95554aad 1343 enum language pretend_language;
10b3939b
DJ
1344 struct dwarf2_queue_item *next;
1345};
1346
1347/* The current queue. */
1348static struct dwarf2_queue_item *dwarf2_queue, *dwarf2_queue_tail;
1349
ae038cb0
DJ
1350/* Loaded secondary compilation units are kept in memory until they
1351 have not been referenced for the processing of this many
1352 compilation units. Set this to zero to disable caching. Cache
1353 sizes of up to at least twenty will improve startup time for
1354 typical inter-CU-reference binaries, at an obvious memory cost. */
1355static int dwarf2_max_cache_age = 5;
920d2a44
AC
1356static void
1357show_dwarf2_max_cache_age (struct ui_file *file, int from_tty,
1358 struct cmd_list_element *c, const char *value)
1359{
3e43a32a
MS
1360 fprintf_filtered (file, _("The upper bound on the age of cached "
1361 "dwarf2 compilation units is %s.\n"),
920d2a44
AC
1362 value);
1363}
4390d890 1364\f
c906108c
SS
1365/* local function prototypes */
1366
a32a8923
DE
1367static const char *get_section_name (const struct dwarf2_section_info *);
1368
1369static const char *get_section_file_name (const struct dwarf2_section_info *);
1370
4efb68b1 1371static void dwarf2_locate_sections (bfd *, asection *, void *);
c906108c 1372
918dd910
JK
1373static void dwarf2_find_base_address (struct die_info *die,
1374 struct dwarf2_cu *cu);
1375
0018ea6f
DE
1376static struct partial_symtab *create_partial_symtab
1377 (struct dwarf2_per_cu_data *per_cu, const char *name);
1378
c67a9c90 1379static void dwarf2_build_psymtabs_hard (struct objfile *);
c906108c 1380
72bf9492
DJ
1381static void scan_partial_symbols (struct partial_die_info *,
1382 CORE_ADDR *, CORE_ADDR *,
5734ee8b 1383 int, struct dwarf2_cu *);
c906108c 1384
72bf9492
DJ
1385static void add_partial_symbol (struct partial_die_info *,
1386 struct dwarf2_cu *);
63d06c5c 1387
72bf9492
DJ
1388static void add_partial_namespace (struct partial_die_info *pdi,
1389 CORE_ADDR *lowpc, CORE_ADDR *highpc,
5734ee8b 1390 int need_pc, struct dwarf2_cu *cu);
63d06c5c 1391
5d7cb8df
JK
1392static void add_partial_module (struct partial_die_info *pdi, CORE_ADDR *lowpc,
1393 CORE_ADDR *highpc, int need_pc,
1394 struct dwarf2_cu *cu);
1395
72bf9492
DJ
1396static void add_partial_enumeration (struct partial_die_info *enum_pdi,
1397 struct dwarf2_cu *cu);
91c24f0a 1398
bc30ff58
JB
1399static void add_partial_subprogram (struct partial_die_info *pdi,
1400 CORE_ADDR *lowpc, CORE_ADDR *highpc,
5734ee8b 1401 int need_pc, struct dwarf2_cu *cu);
bc30ff58 1402
257e7a09
YQ
1403static void dwarf2_read_symtab (struct partial_symtab *,
1404 struct objfile *);
c906108c 1405
a14ed312 1406static void psymtab_to_symtab_1 (struct partial_symtab *);
c906108c 1407
433df2d4
DE
1408static struct abbrev_info *abbrev_table_lookup_abbrev
1409 (const struct abbrev_table *, unsigned int);
1410
1411static struct abbrev_table *abbrev_table_read_table
1412 (struct dwarf2_section_info *, sect_offset);
1413
1414static void abbrev_table_free (struct abbrev_table *);
1415
f4dc4d17
DE
1416static void abbrev_table_free_cleanup (void *);
1417
dee91e82
DE
1418static void dwarf2_read_abbrevs (struct dwarf2_cu *,
1419 struct dwarf2_section_info *);
c906108c 1420
f3dd6933 1421static void dwarf2_free_abbrev_table (void *);
c906108c 1422
d521ce57 1423static unsigned int peek_abbrev_code (bfd *, const gdb_byte *);
6caca83c 1424
dee91e82 1425static struct partial_die_info *load_partial_dies
d521ce57 1426 (const struct die_reader_specs *, const gdb_byte *, int);
72bf9492 1427
d521ce57
TT
1428static const gdb_byte *read_partial_die (const struct die_reader_specs *,
1429 struct partial_die_info *,
1430 struct abbrev_info *,
1431 unsigned int,
1432 const gdb_byte *);
c906108c 1433
36586728 1434static struct partial_die_info *find_partial_die (sect_offset, int,
10b3939b 1435 struct dwarf2_cu *);
72bf9492
DJ
1436
1437static void fixup_partial_die (struct partial_die_info *,
1438 struct dwarf2_cu *);
1439
d521ce57
TT
1440static const gdb_byte *read_attribute (const struct die_reader_specs *,
1441 struct attribute *, struct attr_abbrev *,
1442 const gdb_byte *);
a8329558 1443
a1855c1d 1444static unsigned int read_1_byte (bfd *, const gdb_byte *);
c906108c 1445
a1855c1d 1446static int read_1_signed_byte (bfd *, const gdb_byte *);
c906108c 1447
a1855c1d 1448static unsigned int read_2_bytes (bfd *, const gdb_byte *);
c906108c 1449
a1855c1d 1450static unsigned int read_4_bytes (bfd *, const gdb_byte *);
c906108c 1451
a1855c1d 1452static ULONGEST read_8_bytes (bfd *, const gdb_byte *);
c906108c 1453
d521ce57 1454static CORE_ADDR read_address (bfd *, const gdb_byte *ptr, struct dwarf2_cu *,
891d2f0b 1455 unsigned int *);
c906108c 1456
d521ce57 1457static LONGEST read_initial_length (bfd *, const gdb_byte *, unsigned int *);
c764a876
DE
1458
1459static LONGEST read_checked_initial_length_and_offset
d521ce57 1460 (bfd *, const gdb_byte *, const struct comp_unit_head *,
c764a876 1461 unsigned int *, unsigned int *);
613e1657 1462
d521ce57
TT
1463static LONGEST read_offset (bfd *, const gdb_byte *,
1464 const struct comp_unit_head *,
c764a876
DE
1465 unsigned int *);
1466
d521ce57 1467static LONGEST read_offset_1 (bfd *, const gdb_byte *, unsigned int);
613e1657 1468
f4dc4d17
DE
1469static sect_offset read_abbrev_offset (struct dwarf2_section_info *,
1470 sect_offset);
1471
d521ce57 1472static const gdb_byte *read_n_bytes (bfd *, const gdb_byte *, unsigned int);
c906108c 1473
d521ce57 1474static const char *read_direct_string (bfd *, const gdb_byte *, unsigned int *);
c906108c 1475
d521ce57
TT
1476static const char *read_indirect_string (bfd *, const gdb_byte *,
1477 const struct comp_unit_head *,
1478 unsigned int *);
4bdf3d34 1479
d521ce57 1480static const char *read_indirect_string_from_dwz (struct dwz_file *, LONGEST);
36586728 1481
d521ce57 1482static ULONGEST read_unsigned_leb128 (bfd *, const gdb_byte *, unsigned int *);
c906108c 1483
d521ce57 1484static LONGEST read_signed_leb128 (bfd *, const gdb_byte *, unsigned int *);
c906108c 1485
d521ce57
TT
1486static CORE_ADDR read_addr_index_from_leb128 (struct dwarf2_cu *,
1487 const gdb_byte *,
3019eac3
DE
1488 unsigned int *);
1489
d521ce57
TT
1490static const char *read_str_index (const struct die_reader_specs *reader,
1491 struct dwarf2_cu *cu, ULONGEST str_index);
3019eac3 1492
e142c38c 1493static void set_cu_language (unsigned int, struct dwarf2_cu *);
c906108c 1494
e142c38c
DJ
1495static struct attribute *dwarf2_attr (struct die_info *, unsigned int,
1496 struct dwarf2_cu *);
c906108c 1497
348e048f 1498static struct attribute *dwarf2_attr_no_follow (struct die_info *,
45e58e77 1499 unsigned int);
348e048f 1500
05cf31d1
JB
1501static int dwarf2_flag_true_p (struct die_info *die, unsigned name,
1502 struct dwarf2_cu *cu);
1503
e142c38c 1504static int die_is_declaration (struct die_info *, struct dwarf2_cu *cu);
3ca72b44 1505
e142c38c 1506static struct die_info *die_specification (struct die_info *die,
f2f0e013 1507 struct dwarf2_cu **);
63d06c5c 1508
debd256d
JB
1509static void free_line_header (struct line_header *lh);
1510
3019eac3
DE
1511static struct line_header *dwarf_decode_line_header (unsigned int offset,
1512 struct dwarf2_cu *cu);
debd256d 1513
f3f5162e
DE
1514static void dwarf_decode_lines (struct line_header *, const char *,
1515 struct dwarf2_cu *, struct partial_symtab *,
1516 int);
c906108c 1517
d521ce57 1518static void dwarf2_start_subfile (const char *, const char *, const char *);
c906108c 1519
f4dc4d17 1520static void dwarf2_start_symtab (struct dwarf2_cu *,
15d034d0 1521 const char *, const char *, CORE_ADDR);
f4dc4d17 1522
a14ed312 1523static struct symbol *new_symbol (struct die_info *, struct type *,
e7c27a73 1524 struct dwarf2_cu *);
c906108c 1525
34eaf542
TT
1526static struct symbol *new_symbol_full (struct die_info *, struct type *,
1527 struct dwarf2_cu *, struct symbol *);
1528
ff39bb5e 1529static void dwarf2_const_value (const struct attribute *, struct symbol *,
e7c27a73 1530 struct dwarf2_cu *);
c906108c 1531
ff39bb5e 1532static void dwarf2_const_value_attr (const struct attribute *attr,
98bfdba5
PA
1533 struct type *type,
1534 const char *name,
1535 struct obstack *obstack,
12df843f 1536 struct dwarf2_cu *cu, LONGEST *value,
d521ce57 1537 const gdb_byte **bytes,
98bfdba5 1538 struct dwarf2_locexpr_baton **baton);
2df3850c 1539
e7c27a73 1540static struct type *die_type (struct die_info *, struct dwarf2_cu *);
c906108c 1541
b4ba55a1
JB
1542static int need_gnat_info (struct dwarf2_cu *);
1543
3e43a32a
MS
1544static struct type *die_descriptive_type (struct die_info *,
1545 struct dwarf2_cu *);
b4ba55a1
JB
1546
1547static void set_descriptive_type (struct type *, struct die_info *,
1548 struct dwarf2_cu *);
1549
e7c27a73
DJ
1550static struct type *die_containing_type (struct die_info *,
1551 struct dwarf2_cu *);
c906108c 1552
ff39bb5e 1553static struct type *lookup_die_type (struct die_info *, const struct attribute *,
673bfd45 1554 struct dwarf2_cu *);
c906108c 1555
f792889a 1556static struct type *read_type_die (struct die_info *, struct dwarf2_cu *);
c906108c 1557
673bfd45
DE
1558static struct type *read_type_die_1 (struct die_info *, struct dwarf2_cu *);
1559
0d5cff50 1560static const char *determine_prefix (struct die_info *die, struct dwarf2_cu *);
63d06c5c 1561
6e70227d 1562static char *typename_concat (struct obstack *obs, const char *prefix,
f55ee35c
JK
1563 const char *suffix, int physname,
1564 struct dwarf2_cu *cu);
63d06c5c 1565
e7c27a73 1566static void read_file_scope (struct die_info *, struct dwarf2_cu *);
c906108c 1567
348e048f
DE
1568static void read_type_unit_scope (struct die_info *, struct dwarf2_cu *);
1569
e7c27a73 1570static void read_func_scope (struct die_info *, struct dwarf2_cu *);
c906108c 1571
e7c27a73 1572static void read_lexical_block_scope (struct die_info *, struct dwarf2_cu *);
c906108c 1573
96408a79
SA
1574static void read_call_site_scope (struct die_info *die, struct dwarf2_cu *cu);
1575
ff013f42
JK
1576static int dwarf2_ranges_read (unsigned, CORE_ADDR *, CORE_ADDR *,
1577 struct dwarf2_cu *, struct partial_symtab *);
1578
a14ed312 1579static int dwarf2_get_pc_bounds (struct die_info *,
d85a05f0
DJ
1580 CORE_ADDR *, CORE_ADDR *, struct dwarf2_cu *,
1581 struct partial_symtab *);
c906108c 1582
fae299cd
DC
1583static void get_scope_pc_bounds (struct die_info *,
1584 CORE_ADDR *, CORE_ADDR *,
1585 struct dwarf2_cu *);
1586
801e3a5b
JB
1587static void dwarf2_record_block_ranges (struct die_info *, struct block *,
1588 CORE_ADDR, struct dwarf2_cu *);
1589
a14ed312 1590static void dwarf2_add_field (struct field_info *, struct die_info *,
e7c27a73 1591 struct dwarf2_cu *);
c906108c 1592
a14ed312 1593static void dwarf2_attach_fields_to_type (struct field_info *,
e7c27a73 1594 struct type *, struct dwarf2_cu *);
c906108c 1595
a14ed312 1596static void dwarf2_add_member_fn (struct field_info *,
e26fb1d7 1597 struct die_info *, struct type *,
e7c27a73 1598 struct dwarf2_cu *);
c906108c 1599
a14ed312 1600static void dwarf2_attach_fn_fields_to_type (struct field_info *,
3e43a32a
MS
1601 struct type *,
1602 struct dwarf2_cu *);
c906108c 1603
134d01f1 1604static void process_structure_scope (struct die_info *, struct dwarf2_cu *);
c906108c 1605
e7c27a73 1606static void read_common_block (struct die_info *, struct dwarf2_cu *);
c906108c 1607
e7c27a73 1608static void read_namespace (struct die_info *die, struct dwarf2_cu *);
d9fa45fe 1609
5d7cb8df
JK
1610static void read_module (struct die_info *die, struct dwarf2_cu *cu);
1611
27aa8d6a
SW
1612static void read_import_statement (struct die_info *die, struct dwarf2_cu *);
1613
f55ee35c
JK
1614static struct type *read_module_type (struct die_info *die,
1615 struct dwarf2_cu *cu);
1616
38d518c9 1617static const char *namespace_name (struct die_info *die,
e142c38c 1618 int *is_anonymous, struct dwarf2_cu *);
38d518c9 1619
134d01f1 1620static void process_enumeration_scope (struct die_info *, struct dwarf2_cu *);
c906108c 1621
e7c27a73 1622static CORE_ADDR decode_locdesc (struct dwarf_block *, struct dwarf2_cu *);
c906108c 1623
6e70227d 1624static enum dwarf_array_dim_ordering read_array_order (struct die_info *,
7ca2d3a3
DL
1625 struct dwarf2_cu *);
1626
bf6af496 1627static struct die_info *read_die_and_siblings_1
d521ce57 1628 (const struct die_reader_specs *, const gdb_byte *, const gdb_byte **,
bf6af496 1629 struct die_info *);
639d11d3 1630
dee91e82 1631static struct die_info *read_die_and_siblings (const struct die_reader_specs *,
d521ce57
TT
1632 const gdb_byte *info_ptr,
1633 const gdb_byte **new_info_ptr,
639d11d3
DC
1634 struct die_info *parent);
1635
d521ce57
TT
1636static const gdb_byte *read_full_die_1 (const struct die_reader_specs *,
1637 struct die_info **, const gdb_byte *,
1638 int *, int);
3019eac3 1639
d521ce57
TT
1640static const gdb_byte *read_full_die (const struct die_reader_specs *,
1641 struct die_info **, const gdb_byte *,
1642 int *);
93311388 1643
e7c27a73 1644static void process_die (struct die_info *, struct dwarf2_cu *);
c906108c 1645
15d034d0
TT
1646static const char *dwarf2_canonicalize_name (const char *, struct dwarf2_cu *,
1647 struct obstack *);
71c25dea 1648
15d034d0 1649static const char *dwarf2_name (struct die_info *die, struct dwarf2_cu *);
9219021c 1650
15d034d0 1651static const char *dwarf2_full_name (const char *name,
98bfdba5
PA
1652 struct die_info *die,
1653 struct dwarf2_cu *cu);
1654
ca69b9e6
DE
1655static const char *dwarf2_physname (const char *name, struct die_info *die,
1656 struct dwarf2_cu *cu);
1657
e142c38c 1658static struct die_info *dwarf2_extension (struct die_info *die,
f2f0e013 1659 struct dwarf2_cu **);
9219021c 1660
f39c6ffd 1661static const char *dwarf_tag_name (unsigned int);
c906108c 1662
f39c6ffd 1663static const char *dwarf_attr_name (unsigned int);
c906108c 1664
f39c6ffd 1665static const char *dwarf_form_name (unsigned int);
c906108c 1666
a14ed312 1667static char *dwarf_bool_name (unsigned int);
c906108c 1668
f39c6ffd 1669static const char *dwarf_type_encoding_name (unsigned int);
c906108c 1670
f9aca02d 1671static struct die_info *sibling_die (struct die_info *);
c906108c 1672
d97bc12b
DE
1673static void dump_die_shallow (struct ui_file *, int indent, struct die_info *);
1674
1675static void dump_die_for_error (struct die_info *);
1676
1677static void dump_die_1 (struct ui_file *, int level, int max_level,
1678 struct die_info *);
c906108c 1679
d97bc12b 1680/*static*/ void dump_die (struct die_info *, int max_level);
c906108c 1681
51545339 1682static void store_in_ref_table (struct die_info *,
10b3939b 1683 struct dwarf2_cu *);
c906108c 1684
ff39bb5e 1685static sect_offset dwarf2_get_ref_die_offset (const struct attribute *);
c906108c 1686
ff39bb5e 1687static LONGEST dwarf2_get_attr_constant_value (const struct attribute *, int);
a02abb62 1688
348e048f 1689static struct die_info *follow_die_ref_or_sig (struct die_info *,
ff39bb5e 1690 const struct attribute *,
348e048f
DE
1691 struct dwarf2_cu **);
1692
10b3939b 1693static struct die_info *follow_die_ref (struct die_info *,
ff39bb5e 1694 const struct attribute *,
f2f0e013 1695 struct dwarf2_cu **);
c906108c 1696
348e048f 1697static struct die_info *follow_die_sig (struct die_info *,
ff39bb5e 1698 const struct attribute *,
348e048f
DE
1699 struct dwarf2_cu **);
1700
ac9ec31b
DE
1701static struct type *get_signatured_type (struct die_info *, ULONGEST,
1702 struct dwarf2_cu *);
1703
1704static struct type *get_DW_AT_signature_type (struct die_info *,
ff39bb5e 1705 const struct attribute *,
ac9ec31b
DE
1706 struct dwarf2_cu *);
1707
e5fe5e75 1708static void load_full_type_unit (struct dwarf2_per_cu_data *per_cu);
348e048f 1709
52dc124a 1710static void read_signatured_type (struct signatured_type *);
348e048f 1711
f4dc4d17 1712static struct type_unit_group *get_type_unit_group
ff39bb5e 1713 (struct dwarf2_cu *, const struct attribute *);
f4dc4d17
DE
1714
1715static void build_type_unit_groups (die_reader_func_ftype *, void *);
1716
c906108c
SS
1717/* memory allocation interface */
1718
7b5a2f43 1719static struct dwarf_block *dwarf_alloc_block (struct dwarf2_cu *);
c906108c 1720
b60c80d6 1721static struct die_info *dwarf_alloc_die (struct dwarf2_cu *, int);
c906108c 1722
09262596 1723static void dwarf_decode_macros (struct dwarf2_cu *, unsigned int,
15d034d0 1724 const char *, int);
2e276125 1725
6e5a29e1 1726static int attr_form_is_block (const struct attribute *);
8e19ed76 1727
6e5a29e1 1728static int attr_form_is_section_offset (const struct attribute *);
3690dd37 1729
6e5a29e1 1730static int attr_form_is_constant (const struct attribute *);
3690dd37 1731
6e5a29e1 1732static int attr_form_is_ref (const struct attribute *);
7771576e 1733
8cf6f0b1
TT
1734static void fill_in_loclist_baton (struct dwarf2_cu *cu,
1735 struct dwarf2_loclist_baton *baton,
ff39bb5e 1736 const struct attribute *attr);
8cf6f0b1 1737
ff39bb5e 1738static void dwarf2_symbol_mark_computed (const struct attribute *attr,
93e7bd98 1739 struct symbol *sym,
f1e6e072
TT
1740 struct dwarf2_cu *cu,
1741 int is_block);
4c2df51b 1742
d521ce57
TT
1743static const gdb_byte *skip_one_die (const struct die_reader_specs *reader,
1744 const gdb_byte *info_ptr,
1745 struct abbrev_info *abbrev);
4bb7a0a7 1746
72bf9492
DJ
1747static void free_stack_comp_unit (void *);
1748
72bf9492
DJ
1749static hashval_t partial_die_hash (const void *item);
1750
1751static int partial_die_eq (const void *item_lhs, const void *item_rhs);
1752
ae038cb0 1753static struct dwarf2_per_cu_data *dwarf2_find_containing_comp_unit
36586728 1754 (sect_offset offset, unsigned int offset_in_dwz, struct objfile *objfile);
ae038cb0 1755
9816fde3 1756static void init_one_comp_unit (struct dwarf2_cu *cu,
23745b47 1757 struct dwarf2_per_cu_data *per_cu);
9816fde3
JK
1758
1759static void prepare_one_comp_unit (struct dwarf2_cu *cu,
95554aad
TT
1760 struct die_info *comp_unit_die,
1761 enum language pretend_language);
93311388 1762
68dc6402 1763static void free_heap_comp_unit (void *);
ae038cb0
DJ
1764
1765static void free_cached_comp_units (void *);
1766
1767static void age_cached_comp_units (void);
1768
dee91e82 1769static void free_one_cached_comp_unit (struct dwarf2_per_cu_data *);
ae038cb0 1770
f792889a
DJ
1771static struct type *set_die_type (struct die_info *, struct type *,
1772 struct dwarf2_cu *);
1c379e20 1773
ae038cb0
DJ
1774static void create_all_comp_units (struct objfile *);
1775
0e50663e 1776static int create_all_type_units (struct objfile *);
1fd400ff 1777
95554aad
TT
1778static void load_full_comp_unit (struct dwarf2_per_cu_data *,
1779 enum language);
10b3939b 1780
95554aad
TT
1781static void process_full_comp_unit (struct dwarf2_per_cu_data *,
1782 enum language);
10b3939b 1783
f4dc4d17
DE
1784static void process_full_type_unit (struct dwarf2_per_cu_data *,
1785 enum language);
1786
10b3939b
DJ
1787static void dwarf2_add_dependence (struct dwarf2_cu *,
1788 struct dwarf2_per_cu_data *);
1789
ae038cb0
DJ
1790static void dwarf2_mark (struct dwarf2_cu *);
1791
1792static void dwarf2_clear_marks (struct dwarf2_per_cu_data *);
1793
b64f50a1 1794static struct type *get_die_type_at_offset (sect_offset,
ac9ec31b 1795 struct dwarf2_per_cu_data *);
673bfd45 1796
f792889a 1797static struct type *get_die_type (struct die_info *die, struct dwarf2_cu *cu);
72019c9c 1798
9291a0cd
TT
1799static void dwarf2_release_queue (void *dummy);
1800
95554aad
TT
1801static void queue_comp_unit (struct dwarf2_per_cu_data *per_cu,
1802 enum language pretend_language);
1803
a0f42c21 1804static void process_queue (void);
9291a0cd
TT
1805
1806static void find_file_and_directory (struct die_info *die,
1807 struct dwarf2_cu *cu,
15d034d0 1808 const char **name, const char **comp_dir);
9291a0cd
TT
1809
1810static char *file_full_name (int file, struct line_header *lh,
1811 const char *comp_dir);
1812
d521ce57 1813static const gdb_byte *read_and_check_comp_unit_head
36586728
TT
1814 (struct comp_unit_head *header,
1815 struct dwarf2_section_info *section,
d521ce57 1816 struct dwarf2_section_info *abbrev_section, const gdb_byte *info_ptr,
36586728
TT
1817 int is_debug_types_section);
1818
fd820528 1819static void init_cutu_and_read_dies
f4dc4d17
DE
1820 (struct dwarf2_per_cu_data *this_cu, struct abbrev_table *abbrev_table,
1821 int use_existing_cu, int keep,
3019eac3
DE
1822 die_reader_func_ftype *die_reader_func, void *data);
1823
dee91e82
DE
1824static void init_cutu_and_read_dies_simple
1825 (struct dwarf2_per_cu_data *this_cu,
1826 die_reader_func_ftype *die_reader_func, void *data);
9291a0cd 1827
673bfd45 1828static htab_t allocate_signatured_type_table (struct objfile *objfile);
1fd400ff 1829
3019eac3
DE
1830static htab_t allocate_dwo_unit_table (struct objfile *objfile);
1831
57d63ce2
DE
1832static struct dwo_unit *lookup_dwo_unit_in_dwp
1833 (struct dwp_file *dwp_file, const char *comp_dir,
1834 ULONGEST signature, int is_debug_types);
a2ce51a0
DE
1835
1836static struct dwp_file *get_dwp_file (void);
1837
3019eac3 1838static struct dwo_unit *lookup_dwo_comp_unit
a1855c1d 1839 (struct dwarf2_per_cu_data *, const char *, const char *, ULONGEST);
3019eac3
DE
1840
1841static struct dwo_unit *lookup_dwo_type_unit
a1855c1d 1842 (struct signatured_type *, const char *, const char *);
3019eac3 1843
89e63ee4
DE
1844static void queue_and_load_all_dwo_tus (struct dwarf2_per_cu_data *);
1845
3019eac3
DE
1846static void free_dwo_file_cleanup (void *);
1847
95554aad
TT
1848static void process_cu_includes (void);
1849
1b80a9fa 1850static void check_producer (struct dwarf2_cu *cu);
4390d890
DE
1851\f
1852/* Various complaints about symbol reading that don't abort the process. */
1853
1854static void
1855dwarf2_statement_list_fits_in_line_number_section_complaint (void)
1856{
1857 complaint (&symfile_complaints,
1858 _("statement list doesn't fit in .debug_line section"));
1859}
1860
1861static void
1862dwarf2_debug_line_missing_file_complaint (void)
1863{
1864 complaint (&symfile_complaints,
1865 _(".debug_line section has line data without a file"));
1866}
1867
1868static void
1869dwarf2_debug_line_missing_end_sequence_complaint (void)
1870{
1871 complaint (&symfile_complaints,
1872 _(".debug_line section has line "
1873 "program sequence without an end"));
1874}
1875
1876static void
1877dwarf2_complex_location_expr_complaint (void)
1878{
1879 complaint (&symfile_complaints, _("location expression too complex"));
1880}
1881
1882static void
1883dwarf2_const_value_length_mismatch_complaint (const char *arg1, int arg2,
1884 int arg3)
1885{
1886 complaint (&symfile_complaints,
1887 _("const value length mismatch for '%s', got %d, expected %d"),
1888 arg1, arg2, arg3);
1889}
1890
1891static void
1892dwarf2_section_buffer_overflow_complaint (struct dwarf2_section_info *section)
1893{
1894 complaint (&symfile_complaints,
1895 _("debug info runs off end of %s section"
1896 " [in module %s]"),
a32a8923
DE
1897 get_section_name (section),
1898 get_section_file_name (section));
4390d890 1899}
1b80a9fa 1900
4390d890
DE
1901static void
1902dwarf2_macro_malformed_definition_complaint (const char *arg1)
1903{
1904 complaint (&symfile_complaints,
1905 _("macro debug info contains a "
1906 "malformed macro definition:\n`%s'"),
1907 arg1);
1908}
1909
1910static void
1911dwarf2_invalid_attrib_class_complaint (const char *arg1, const char *arg2)
1912{
1913 complaint (&symfile_complaints,
1914 _("invalid attribute class or form for '%s' in '%s'"),
1915 arg1, arg2);
1916}
1917\f
9291a0cd
TT
1918#if WORDS_BIGENDIAN
1919
1920/* Convert VALUE between big- and little-endian. */
1921static offset_type
1922byte_swap (offset_type value)
1923{
1924 offset_type result;
1925
1926 result = (value & 0xff) << 24;
1927 result |= (value & 0xff00) << 8;
1928 result |= (value & 0xff0000) >> 8;
1929 result |= (value & 0xff000000) >> 24;
1930 return result;
1931}
1932
1933#define MAYBE_SWAP(V) byte_swap (V)
1934
1935#else
1936#define MAYBE_SWAP(V) (V)
1937#endif /* WORDS_BIGENDIAN */
1938
1939/* The suffix for an index file. */
1940#define INDEX_SUFFIX ".gdb-index"
1941
c906108c 1942/* Try to locate the sections we need for DWARF 2 debugging
251d32d9
TG
1943 information and return true if we have enough to do something.
1944 NAMES points to the dwarf2 section names, or is NULL if the standard
1945 ELF names are used. */
c906108c
SS
1946
1947int
251d32d9
TG
1948dwarf2_has_info (struct objfile *objfile,
1949 const struct dwarf2_debug_sections *names)
c906108c 1950{
be391dca
TT
1951 dwarf2_per_objfile = objfile_data (objfile, dwarf2_objfile_data_key);
1952 if (!dwarf2_per_objfile)
1953 {
1954 /* Initialize per-objfile state. */
1955 struct dwarf2_per_objfile *data
1956 = obstack_alloc (&objfile->objfile_obstack, sizeof (*data));
9a619af0 1957
be391dca
TT
1958 memset (data, 0, sizeof (*data));
1959 set_objfile_data (objfile, dwarf2_objfile_data_key, data);
1960 dwarf2_per_objfile = data;
6502dd73 1961
251d32d9
TG
1962 bfd_map_over_sections (objfile->obfd, dwarf2_locate_sections,
1963 (void *) names);
be391dca
TT
1964 dwarf2_per_objfile->objfile = objfile;
1965 }
73869dc2
DE
1966 return (!dwarf2_per_objfile->info.is_virtual
1967 && dwarf2_per_objfile->info.s.asection != NULL
1968 && !dwarf2_per_objfile->abbrev.is_virtual
1969 && dwarf2_per_objfile->abbrev.s.asection != NULL);
1970}
1971
1972/* Return the containing section of virtual section SECTION. */
1973
1974static struct dwarf2_section_info *
1975get_containing_section (const struct dwarf2_section_info *section)
1976{
1977 gdb_assert (section->is_virtual);
1978 return section->s.containing_section;
c906108c
SS
1979}
1980
a32a8923
DE
1981/* Return the bfd owner of SECTION. */
1982
1983static struct bfd *
1984get_section_bfd_owner (const struct dwarf2_section_info *section)
1985{
73869dc2
DE
1986 if (section->is_virtual)
1987 {
1988 section = get_containing_section (section);
1989 gdb_assert (!section->is_virtual);
1990 }
1991 return section->s.asection->owner;
a32a8923
DE
1992}
1993
1994/* Return the bfd section of SECTION.
1995 Returns NULL if the section is not present. */
1996
1997static asection *
1998get_section_bfd_section (const struct dwarf2_section_info *section)
1999{
73869dc2
DE
2000 if (section->is_virtual)
2001 {
2002 section = get_containing_section (section);
2003 gdb_assert (!section->is_virtual);
2004 }
2005 return section->s.asection;
a32a8923
DE
2006}
2007
2008/* Return the name of SECTION. */
2009
2010static const char *
2011get_section_name (const struct dwarf2_section_info *section)
2012{
2013 asection *sectp = get_section_bfd_section (section);
2014
2015 gdb_assert (sectp != NULL);
2016 return bfd_section_name (get_section_bfd_owner (section), sectp);
2017}
2018
2019/* Return the name of the file SECTION is in. */
2020
2021static const char *
2022get_section_file_name (const struct dwarf2_section_info *section)
2023{
2024 bfd *abfd = get_section_bfd_owner (section);
2025
2026 return bfd_get_filename (abfd);
2027}
2028
2029/* Return the id of SECTION.
2030 Returns 0 if SECTION doesn't exist. */
2031
2032static int
2033get_section_id (const struct dwarf2_section_info *section)
2034{
2035 asection *sectp = get_section_bfd_section (section);
2036
2037 if (sectp == NULL)
2038 return 0;
2039 return sectp->id;
2040}
2041
2042/* Return the flags of SECTION.
73869dc2 2043 SECTION (or containing section if this is a virtual section) must exist. */
a32a8923
DE
2044
2045static int
2046get_section_flags (const struct dwarf2_section_info *section)
2047{
2048 asection *sectp = get_section_bfd_section (section);
2049
2050 gdb_assert (sectp != NULL);
2051 return bfd_get_section_flags (sectp->owner, sectp);
2052}
2053
251d32d9
TG
2054/* When loading sections, we look either for uncompressed section or for
2055 compressed section names. */
233a11ab
CS
2056
2057static int
251d32d9
TG
2058section_is_p (const char *section_name,
2059 const struct dwarf2_section_names *names)
233a11ab 2060{
251d32d9
TG
2061 if (names->normal != NULL
2062 && strcmp (section_name, names->normal) == 0)
2063 return 1;
2064 if (names->compressed != NULL
2065 && strcmp (section_name, names->compressed) == 0)
2066 return 1;
2067 return 0;
233a11ab
CS
2068}
2069
c906108c
SS
2070/* This function is mapped across the sections and remembers the
2071 offset and size of each of the debugging sections we are interested
2072 in. */
2073
2074static void
251d32d9 2075dwarf2_locate_sections (bfd *abfd, asection *sectp, void *vnames)
c906108c 2076{
251d32d9 2077 const struct dwarf2_debug_sections *names;
dc7650b8 2078 flagword aflag = bfd_get_section_flags (abfd, sectp);
251d32d9
TG
2079
2080 if (vnames == NULL)
2081 names = &dwarf2_elf_names;
2082 else
2083 names = (const struct dwarf2_debug_sections *) vnames;
2084
dc7650b8
JK
2085 if ((aflag & SEC_HAS_CONTENTS) == 0)
2086 {
2087 }
2088 else if (section_is_p (sectp->name, &names->info))
c906108c 2089 {
73869dc2 2090 dwarf2_per_objfile->info.s.asection = sectp;
dce234bc 2091 dwarf2_per_objfile->info.size = bfd_get_section_size (sectp);
c906108c 2092 }
251d32d9 2093 else if (section_is_p (sectp->name, &names->abbrev))
c906108c 2094 {
73869dc2 2095 dwarf2_per_objfile->abbrev.s.asection = sectp;
dce234bc 2096 dwarf2_per_objfile->abbrev.size = bfd_get_section_size (sectp);
c906108c 2097 }
251d32d9 2098 else if (section_is_p (sectp->name, &names->line))
c906108c 2099 {
73869dc2 2100 dwarf2_per_objfile->line.s.asection = sectp;
dce234bc 2101 dwarf2_per_objfile->line.size = bfd_get_section_size (sectp);
c906108c 2102 }
251d32d9 2103 else if (section_is_p (sectp->name, &names->loc))
c906108c 2104 {
73869dc2 2105 dwarf2_per_objfile->loc.s.asection = sectp;
dce234bc 2106 dwarf2_per_objfile->loc.size = bfd_get_section_size (sectp);
c906108c 2107 }
251d32d9 2108 else if (section_is_p (sectp->name, &names->macinfo))
c906108c 2109 {
73869dc2 2110 dwarf2_per_objfile->macinfo.s.asection = sectp;
dce234bc 2111 dwarf2_per_objfile->macinfo.size = bfd_get_section_size (sectp);
c906108c 2112 }
cf2c3c16
TT
2113 else if (section_is_p (sectp->name, &names->macro))
2114 {
73869dc2 2115 dwarf2_per_objfile->macro.s.asection = sectp;
cf2c3c16
TT
2116 dwarf2_per_objfile->macro.size = bfd_get_section_size (sectp);
2117 }
251d32d9 2118 else if (section_is_p (sectp->name, &names->str))
c906108c 2119 {
73869dc2 2120 dwarf2_per_objfile->str.s.asection = sectp;
dce234bc 2121 dwarf2_per_objfile->str.size = bfd_get_section_size (sectp);
c906108c 2122 }
3019eac3
DE
2123 else if (section_is_p (sectp->name, &names->addr))
2124 {
73869dc2 2125 dwarf2_per_objfile->addr.s.asection = sectp;
3019eac3
DE
2126 dwarf2_per_objfile->addr.size = bfd_get_section_size (sectp);
2127 }
251d32d9 2128 else if (section_is_p (sectp->name, &names->frame))
b6af0555 2129 {
73869dc2 2130 dwarf2_per_objfile->frame.s.asection = sectp;
dce234bc 2131 dwarf2_per_objfile->frame.size = bfd_get_section_size (sectp);
b6af0555 2132 }
251d32d9 2133 else if (section_is_p (sectp->name, &names->eh_frame))
b6af0555 2134 {
73869dc2 2135 dwarf2_per_objfile->eh_frame.s.asection = sectp;
dc7650b8 2136 dwarf2_per_objfile->eh_frame.size = bfd_get_section_size (sectp);
b6af0555 2137 }
251d32d9 2138 else if (section_is_p (sectp->name, &names->ranges))
af34e669 2139 {
73869dc2 2140 dwarf2_per_objfile->ranges.s.asection = sectp;
dce234bc 2141 dwarf2_per_objfile->ranges.size = bfd_get_section_size (sectp);
af34e669 2142 }
251d32d9 2143 else if (section_is_p (sectp->name, &names->types))
348e048f 2144 {
8b70b953
TT
2145 struct dwarf2_section_info type_section;
2146
2147 memset (&type_section, 0, sizeof (type_section));
73869dc2 2148 type_section.s.asection = sectp;
8b70b953
TT
2149 type_section.size = bfd_get_section_size (sectp);
2150
2151 VEC_safe_push (dwarf2_section_info_def, dwarf2_per_objfile->types,
2152 &type_section);
348e048f 2153 }
251d32d9 2154 else if (section_is_p (sectp->name, &names->gdb_index))
9291a0cd 2155 {
73869dc2 2156 dwarf2_per_objfile->gdb_index.s.asection = sectp;
9291a0cd
TT
2157 dwarf2_per_objfile->gdb_index.size = bfd_get_section_size (sectp);
2158 }
dce234bc 2159
72dca2f5
FR
2160 if ((bfd_get_section_flags (abfd, sectp) & SEC_LOAD)
2161 && bfd_section_vma (abfd, sectp) == 0)
2162 dwarf2_per_objfile->has_section_at_zero = 1;
c906108c
SS
2163}
2164
fceca515
DE
2165/* A helper function that decides whether a section is empty,
2166 or not present. */
9e0ac564
TT
2167
2168static int
19ac8c2e 2169dwarf2_section_empty_p (const struct dwarf2_section_info *section)
9e0ac564 2170{
73869dc2
DE
2171 if (section->is_virtual)
2172 return section->size == 0;
2173 return section->s.asection == NULL || section->size == 0;
9e0ac564
TT
2174}
2175
3019eac3
DE
2176/* Read the contents of the section INFO.
2177 OBJFILE is the main object file, but not necessarily the file where
a32a8923
DE
2178 the section comes from. E.g., for DWO files the bfd of INFO is the bfd
2179 of the DWO file.
dce234bc 2180 If the section is compressed, uncompress it before returning. */
c906108c 2181
dce234bc
PP
2182static void
2183dwarf2_read_section (struct objfile *objfile, struct dwarf2_section_info *info)
c906108c 2184{
a32a8923 2185 asection *sectp;
3019eac3 2186 bfd *abfd;
dce234bc 2187 gdb_byte *buf, *retbuf;
c906108c 2188
be391dca
TT
2189 if (info->readin)
2190 return;
dce234bc 2191 info->buffer = NULL;
be391dca 2192 info->readin = 1;
188dd5d6 2193
9e0ac564 2194 if (dwarf2_section_empty_p (info))
dce234bc 2195 return;
c906108c 2196
a32a8923 2197 sectp = get_section_bfd_section (info);
3019eac3 2198
73869dc2
DE
2199 /* If this is a virtual section we need to read in the real one first. */
2200 if (info->is_virtual)
2201 {
2202 struct dwarf2_section_info *containing_section =
2203 get_containing_section (info);
2204
2205 gdb_assert (sectp != NULL);
2206 if ((sectp->flags & SEC_RELOC) != 0)
2207 {
2208 error (_("Dwarf Error: DWP format V2 with relocations is not"
2209 " supported in section %s [in module %s]"),
2210 get_section_name (info), get_section_file_name (info));
2211 }
2212 dwarf2_read_section (objfile, containing_section);
2213 /* Other code should have already caught virtual sections that don't
2214 fit. */
2215 gdb_assert (info->virtual_offset + info->size
2216 <= containing_section->size);
2217 /* If the real section is empty or there was a problem reading the
2218 section we shouldn't get here. */
2219 gdb_assert (containing_section->buffer != NULL);
2220 info->buffer = containing_section->buffer + info->virtual_offset;
2221 return;
2222 }
2223
4bf44c1c
TT
2224 /* If the section has relocations, we must read it ourselves.
2225 Otherwise we attach it to the BFD. */
2226 if ((sectp->flags & SEC_RELOC) == 0)
dce234bc 2227 {
d521ce57 2228 info->buffer = gdb_bfd_map_section (sectp, &info->size);
4bf44c1c 2229 return;
dce234bc 2230 }
dce234bc 2231
4bf44c1c
TT
2232 buf = obstack_alloc (&objfile->objfile_obstack, info->size);
2233 info->buffer = buf;
dce234bc
PP
2234
2235 /* When debugging .o files, we may need to apply relocations; see
2236 http://sourceware.org/ml/gdb-patches/2002-04/msg00136.html .
2237 We never compress sections in .o files, so we only need to
2238 try this when the section is not compressed. */
ac8035ab 2239 retbuf = symfile_relocate_debug_section (objfile, sectp, buf);
dce234bc
PP
2240 if (retbuf != NULL)
2241 {
2242 info->buffer = retbuf;
2243 return;
2244 }
2245
a32a8923
DE
2246 abfd = get_section_bfd_owner (info);
2247 gdb_assert (abfd != NULL);
2248
dce234bc
PP
2249 if (bfd_seek (abfd, sectp->filepos, SEEK_SET) != 0
2250 || bfd_bread (buf, info->size, abfd) != info->size)
19ac8c2e
DE
2251 {
2252 error (_("Dwarf Error: Can't read DWARF data"
2253 " in section %s [in module %s]"),
2254 bfd_section_name (abfd, sectp), bfd_get_filename (abfd));
2255 }
dce234bc
PP
2256}
2257
9e0ac564
TT
2258/* A helper function that returns the size of a section in a safe way.
2259 If you are positive that the section has been read before using the
2260 size, then it is safe to refer to the dwarf2_section_info object's
2261 "size" field directly. In other cases, you must call this
2262 function, because for compressed sections the size field is not set
2263 correctly until the section has been read. */
2264
2265static bfd_size_type
2266dwarf2_section_size (struct objfile *objfile,
2267 struct dwarf2_section_info *info)
2268{
2269 if (!info->readin)
2270 dwarf2_read_section (objfile, info);
2271 return info->size;
2272}
2273
dce234bc 2274/* Fill in SECTP, BUFP and SIZEP with section info, given OBJFILE and
0963b4bd 2275 SECTION_NAME. */
af34e669 2276
dce234bc 2277void
3017a003
TG
2278dwarf2_get_section_info (struct objfile *objfile,
2279 enum dwarf2_section_enum sect,
d521ce57 2280 asection **sectp, const gdb_byte **bufp,
dce234bc
PP
2281 bfd_size_type *sizep)
2282{
2283 struct dwarf2_per_objfile *data
2284 = objfile_data (objfile, dwarf2_objfile_data_key);
2285 struct dwarf2_section_info *info;
a3b2a86b
TT
2286
2287 /* We may see an objfile without any DWARF, in which case we just
2288 return nothing. */
2289 if (data == NULL)
2290 {
2291 *sectp = NULL;
2292 *bufp = NULL;
2293 *sizep = 0;
2294 return;
2295 }
3017a003
TG
2296 switch (sect)
2297 {
2298 case DWARF2_DEBUG_FRAME:
2299 info = &data->frame;
2300 break;
2301 case DWARF2_EH_FRAME:
2302 info = &data->eh_frame;
2303 break;
2304 default:
2305 gdb_assert_not_reached ("unexpected section");
2306 }
dce234bc 2307
9e0ac564 2308 dwarf2_read_section (objfile, info);
dce234bc 2309
a32a8923 2310 *sectp = get_section_bfd_section (info);
dce234bc
PP
2311 *bufp = info->buffer;
2312 *sizep = info->size;
2313}
2314
36586728
TT
2315/* A helper function to find the sections for a .dwz file. */
2316
2317static void
2318locate_dwz_sections (bfd *abfd, asection *sectp, void *arg)
2319{
2320 struct dwz_file *dwz_file = arg;
2321
2322 /* Note that we only support the standard ELF names, because .dwz
2323 is ELF-only (at the time of writing). */
2324 if (section_is_p (sectp->name, &dwarf2_elf_names.abbrev))
2325 {
73869dc2 2326 dwz_file->abbrev.s.asection = sectp;
36586728
TT
2327 dwz_file->abbrev.size = bfd_get_section_size (sectp);
2328 }
2329 else if (section_is_p (sectp->name, &dwarf2_elf_names.info))
2330 {
73869dc2 2331 dwz_file->info.s.asection = sectp;
36586728
TT
2332 dwz_file->info.size = bfd_get_section_size (sectp);
2333 }
2334 else if (section_is_p (sectp->name, &dwarf2_elf_names.str))
2335 {
73869dc2 2336 dwz_file->str.s.asection = sectp;
36586728
TT
2337 dwz_file->str.size = bfd_get_section_size (sectp);
2338 }
2339 else if (section_is_p (sectp->name, &dwarf2_elf_names.line))
2340 {
73869dc2 2341 dwz_file->line.s.asection = sectp;
36586728
TT
2342 dwz_file->line.size = bfd_get_section_size (sectp);
2343 }
2344 else if (section_is_p (sectp->name, &dwarf2_elf_names.macro))
2345 {
73869dc2 2346 dwz_file->macro.s.asection = sectp;
36586728
TT
2347 dwz_file->macro.size = bfd_get_section_size (sectp);
2348 }
2ec9a5e0
TT
2349 else if (section_is_p (sectp->name, &dwarf2_elf_names.gdb_index))
2350 {
73869dc2 2351 dwz_file->gdb_index.s.asection = sectp;
2ec9a5e0
TT
2352 dwz_file->gdb_index.size = bfd_get_section_size (sectp);
2353 }
36586728
TT
2354}
2355
4db1a1dc
TT
2356/* Open the separate '.dwz' debug file, if needed. Return NULL if
2357 there is no .gnu_debugaltlink section in the file. Error if there
2358 is such a section but the file cannot be found. */
36586728
TT
2359
2360static struct dwz_file *
2361dwarf2_get_dwz_file (void)
2362{
4db1a1dc
TT
2363 bfd *dwz_bfd;
2364 char *data;
36586728
TT
2365 struct cleanup *cleanup;
2366 const char *filename;
2367 struct dwz_file *result;
acd13123 2368 bfd_size_type buildid_len_arg;
dc294be5
TT
2369 size_t buildid_len;
2370 bfd_byte *buildid;
36586728
TT
2371
2372 if (dwarf2_per_objfile->dwz_file != NULL)
2373 return dwarf2_per_objfile->dwz_file;
2374
4db1a1dc
TT
2375 bfd_set_error (bfd_error_no_error);
2376 data = bfd_get_alt_debug_link_info (dwarf2_per_objfile->objfile->obfd,
acd13123 2377 &buildid_len_arg, &buildid);
4db1a1dc
TT
2378 if (data == NULL)
2379 {
2380 if (bfd_get_error () == bfd_error_no_error)
2381 return NULL;
2382 error (_("could not read '.gnu_debugaltlink' section: %s"),
2383 bfd_errmsg (bfd_get_error ()));
2384 }
36586728 2385 cleanup = make_cleanup (xfree, data);
dc294be5 2386 make_cleanup (xfree, buildid);
36586728 2387
acd13123
TT
2388 buildid_len = (size_t) buildid_len_arg;
2389
f9d83a0b 2390 filename = (const char *) data;
36586728
TT
2391 if (!IS_ABSOLUTE_PATH (filename))
2392 {
4262abfb 2393 char *abs = gdb_realpath (objfile_name (dwarf2_per_objfile->objfile));
36586728
TT
2394 char *rel;
2395
2396 make_cleanup (xfree, abs);
2397 abs = ldirname (abs);
2398 make_cleanup (xfree, abs);
2399
2400 rel = concat (abs, SLASH_STRING, filename, (char *) NULL);
2401 make_cleanup (xfree, rel);
2402 filename = rel;
2403 }
2404
dc294be5
TT
2405 /* First try the file name given in the section. If that doesn't
2406 work, try to use the build-id instead. */
36586728 2407 dwz_bfd = gdb_bfd_open (filename, gnutarget, -1);
dc294be5 2408 if (dwz_bfd != NULL)
36586728 2409 {
dc294be5
TT
2410 if (!build_id_verify (dwz_bfd, buildid_len, buildid))
2411 {
2412 gdb_bfd_unref (dwz_bfd);
2413 dwz_bfd = NULL;
2414 }
36586728
TT
2415 }
2416
dc294be5
TT
2417 if (dwz_bfd == NULL)
2418 dwz_bfd = build_id_to_debug_bfd (buildid_len, buildid);
2419
2420 if (dwz_bfd == NULL)
2421 error (_("could not find '.gnu_debugaltlink' file for %s"),
2422 objfile_name (dwarf2_per_objfile->objfile));
2423
36586728
TT
2424 result = OBSTACK_ZALLOC (&dwarf2_per_objfile->objfile->objfile_obstack,
2425 struct dwz_file);
2426 result->dwz_bfd = dwz_bfd;
2427
2428 bfd_map_over_sections (dwz_bfd, locate_dwz_sections, result);
2429
2430 do_cleanups (cleanup);
2431
8d2cc612 2432 dwarf2_per_objfile->dwz_file = result;
36586728
TT
2433 return result;
2434}
9291a0cd 2435\f
7b9f3c50
DE
2436/* DWARF quick_symbols_functions support. */
2437
2438/* TUs can share .debug_line entries, and there can be a lot more TUs than
2439 unique line tables, so we maintain a separate table of all .debug_line
2440 derived entries to support the sharing.
2441 All the quick functions need is the list of file names. We discard the
2442 line_header when we're done and don't need to record it here. */
2443struct quick_file_names
2444{
094b34ac
DE
2445 /* The data used to construct the hash key. */
2446 struct stmt_list_hash hash;
7b9f3c50
DE
2447
2448 /* The number of entries in file_names, real_names. */
2449 unsigned int num_file_names;
2450
2451 /* The file names from the line table, after being run through
2452 file_full_name. */
2453 const char **file_names;
2454
2455 /* The file names from the line table after being run through
2456 gdb_realpath. These are computed lazily. */
2457 const char **real_names;
2458};
2459
2460/* When using the index (and thus not using psymtabs), each CU has an
2461 object of this type. This is used to hold information needed by
2462 the various "quick" methods. */
2463struct dwarf2_per_cu_quick_data
2464{
2465 /* The file table. This can be NULL if there was no file table
2466 or it's currently not read in.
2467 NOTE: This points into dwarf2_per_objfile->quick_file_names_table. */
2468 struct quick_file_names *file_names;
2469
2470 /* The corresponding symbol table. This is NULL if symbols for this
2471 CU have not yet been read. */
2472 struct symtab *symtab;
2473
2474 /* A temporary mark bit used when iterating over all CUs in
2475 expand_symtabs_matching. */
2476 unsigned int mark : 1;
2477
2478 /* True if we've tried to read the file table and found there isn't one.
2479 There will be no point in trying to read it again next time. */
2480 unsigned int no_file_data : 1;
2481};
2482
094b34ac
DE
2483/* Utility hash function for a stmt_list_hash. */
2484
2485static hashval_t
2486hash_stmt_list_entry (const struct stmt_list_hash *stmt_list_hash)
2487{
2488 hashval_t v = 0;
2489
2490 if (stmt_list_hash->dwo_unit != NULL)
2491 v += (uintptr_t) stmt_list_hash->dwo_unit->dwo_file;
2492 v += stmt_list_hash->line_offset.sect_off;
2493 return v;
2494}
2495
2496/* Utility equality function for a stmt_list_hash. */
2497
2498static int
2499eq_stmt_list_entry (const struct stmt_list_hash *lhs,
2500 const struct stmt_list_hash *rhs)
2501{
2502 if ((lhs->dwo_unit != NULL) != (rhs->dwo_unit != NULL))
2503 return 0;
2504 if (lhs->dwo_unit != NULL
2505 && lhs->dwo_unit->dwo_file != rhs->dwo_unit->dwo_file)
2506 return 0;
2507
2508 return lhs->line_offset.sect_off == rhs->line_offset.sect_off;
2509}
2510
7b9f3c50
DE
2511/* Hash function for a quick_file_names. */
2512
2513static hashval_t
2514hash_file_name_entry (const void *e)
2515{
2516 const struct quick_file_names *file_data = e;
2517
094b34ac 2518 return hash_stmt_list_entry (&file_data->hash);
7b9f3c50
DE
2519}
2520
2521/* Equality function for a quick_file_names. */
2522
2523static int
2524eq_file_name_entry (const void *a, const void *b)
2525{
2526 const struct quick_file_names *ea = a;
2527 const struct quick_file_names *eb = b;
2528
094b34ac 2529 return eq_stmt_list_entry (&ea->hash, &eb->hash);
7b9f3c50
DE
2530}
2531
2532/* Delete function for a quick_file_names. */
2533
2534static void
2535delete_file_name_entry (void *e)
2536{
2537 struct quick_file_names *file_data = e;
2538 int i;
2539
2540 for (i = 0; i < file_data->num_file_names; ++i)
2541 {
2542 xfree ((void*) file_data->file_names[i]);
2543 if (file_data->real_names)
2544 xfree ((void*) file_data->real_names[i]);
2545 }
2546
2547 /* The space for the struct itself lives on objfile_obstack,
2548 so we don't free it here. */
2549}
2550
2551/* Create a quick_file_names hash table. */
2552
2553static htab_t
2554create_quick_file_names_table (unsigned int nr_initial_entries)
2555{
2556 return htab_create_alloc (nr_initial_entries,
2557 hash_file_name_entry, eq_file_name_entry,
2558 delete_file_name_entry, xcalloc, xfree);
2559}
9291a0cd 2560
918dd910
JK
2561/* Read in PER_CU->CU. This function is unrelated to symtabs, symtab would
2562 have to be created afterwards. You should call age_cached_comp_units after
2563 processing PER_CU->CU. dw2_setup must have been already called. */
2564
2565static void
2566load_cu (struct dwarf2_per_cu_data *per_cu)
2567{
3019eac3 2568 if (per_cu->is_debug_types)
e5fe5e75 2569 load_full_type_unit (per_cu);
918dd910 2570 else
95554aad 2571 load_full_comp_unit (per_cu, language_minimal);
918dd910 2572
918dd910 2573 gdb_assert (per_cu->cu != NULL);
2dc860c0
DE
2574
2575 dwarf2_find_base_address (per_cu->cu->dies, per_cu->cu);
918dd910
JK
2576}
2577
a0f42c21 2578/* Read in the symbols for PER_CU. */
2fdf6df6 2579
9291a0cd 2580static void
a0f42c21 2581dw2_do_instantiate_symtab (struct dwarf2_per_cu_data *per_cu)
9291a0cd
TT
2582{
2583 struct cleanup *back_to;
2584
f4dc4d17
DE
2585 /* Skip type_unit_groups, reading the type units they contain
2586 is handled elsewhere. */
2587 if (IS_TYPE_UNIT_GROUP (per_cu))
2588 return;
2589
9291a0cd
TT
2590 back_to = make_cleanup (dwarf2_release_queue, NULL);
2591
95554aad
TT
2592 if (dwarf2_per_objfile->using_index
2593 ? per_cu->v.quick->symtab == NULL
2594 : (per_cu->v.psymtab == NULL || !per_cu->v.psymtab->readin))
2595 {
2596 queue_comp_unit (per_cu, language_minimal);
2597 load_cu (per_cu);
89e63ee4
DE
2598
2599 /* If we just loaded a CU from a DWO, and we're working with an index
2600 that may badly handle TUs, load all the TUs in that DWO as well.
2601 http://sourceware.org/bugzilla/show_bug.cgi?id=15021 */
2602 if (!per_cu->is_debug_types
2603 && per_cu->cu->dwo_unit != NULL
2604 && dwarf2_per_objfile->index_table != NULL
2605 && dwarf2_per_objfile->index_table->version <= 7
2606 /* DWP files aren't supported yet. */
2607 && get_dwp_file () == NULL)
2608 queue_and_load_all_dwo_tus (per_cu);
95554aad 2609 }
9291a0cd 2610
a0f42c21 2611 process_queue ();
9291a0cd
TT
2612
2613 /* Age the cache, releasing compilation units that have not
2614 been used recently. */
2615 age_cached_comp_units ();
2616
2617 do_cleanups (back_to);
2618}
2619
2620/* Ensure that the symbols for PER_CU have been read in. OBJFILE is
2621 the objfile from which this CU came. Returns the resulting symbol
2622 table. */
2fdf6df6 2623
9291a0cd 2624static struct symtab *
a0f42c21 2625dw2_instantiate_symtab (struct dwarf2_per_cu_data *per_cu)
9291a0cd 2626{
95554aad 2627 gdb_assert (dwarf2_per_objfile->using_index);
9291a0cd
TT
2628 if (!per_cu->v.quick->symtab)
2629 {
2630 struct cleanup *back_to = make_cleanup (free_cached_comp_units, NULL);
2631 increment_reading_symtab ();
a0f42c21 2632 dw2_do_instantiate_symtab (per_cu);
95554aad 2633 process_cu_includes ();
9291a0cd
TT
2634 do_cleanups (back_to);
2635 }
2636 return per_cu->v.quick->symtab;
2637}
2638
f4dc4d17
DE
2639/* Return the CU given its index.
2640
2641 This is intended for loops like:
2642
2643 for (i = 0; i < (dwarf2_per_objfile->n_comp_units
2644 + dwarf2_per_objfile->n_type_units); ++i)
2645 {
2646 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
2647
2648 ...;
2649 }
2650*/
2fdf6df6 2651
1fd400ff
TT
2652static struct dwarf2_per_cu_data *
2653dw2_get_cu (int index)
2654{
2655 if (index >= dwarf2_per_objfile->n_comp_units)
2656 {
f4dc4d17 2657 index -= dwarf2_per_objfile->n_comp_units;
094b34ac
DE
2658 gdb_assert (index < dwarf2_per_objfile->n_type_units);
2659 return &dwarf2_per_objfile->all_type_units[index]->per_cu;
f4dc4d17
DE
2660 }
2661
2662 return dwarf2_per_objfile->all_comp_units[index];
2663}
2664
2665/* Return the primary CU given its index.
2666 The difference between this function and dw2_get_cu is in the handling
2667 of type units (TUs). Here we return the type_unit_group object.
2668
2669 This is intended for loops like:
2670
2671 for (i = 0; i < (dwarf2_per_objfile->n_comp_units
2672 + dwarf2_per_objfile->n_type_unit_groups); ++i)
2673 {
2674 struct dwarf2_per_cu_data *per_cu = dw2_get_primary_cu (i);
2675
2676 ...;
2677 }
2678*/
2679
2680static struct dwarf2_per_cu_data *
2681dw2_get_primary_cu (int index)
2682{
2683 if (index >= dwarf2_per_objfile->n_comp_units)
2684 {
1fd400ff 2685 index -= dwarf2_per_objfile->n_comp_units;
094b34ac
DE
2686 gdb_assert (index < dwarf2_per_objfile->n_type_unit_groups);
2687 return &dwarf2_per_objfile->all_type_unit_groups[index]->per_cu;
1fd400ff 2688 }
f4dc4d17 2689
1fd400ff
TT
2690 return dwarf2_per_objfile->all_comp_units[index];
2691}
2692
2ec9a5e0
TT
2693/* A helper for create_cus_from_index that handles a given list of
2694 CUs. */
2fdf6df6 2695
74a0d9f6 2696static void
2ec9a5e0
TT
2697create_cus_from_index_list (struct objfile *objfile,
2698 const gdb_byte *cu_list, offset_type n_elements,
2699 struct dwarf2_section_info *section,
2700 int is_dwz,
2701 int base_offset)
9291a0cd
TT
2702{
2703 offset_type i;
9291a0cd 2704
2ec9a5e0 2705 for (i = 0; i < n_elements; i += 2)
9291a0cd
TT
2706 {
2707 struct dwarf2_per_cu_data *the_cu;
2708 ULONGEST offset, length;
2709
74a0d9f6
JK
2710 gdb_static_assert (sizeof (ULONGEST) >= 8);
2711 offset = extract_unsigned_integer (cu_list, 8, BFD_ENDIAN_LITTLE);
2712 length = extract_unsigned_integer (cu_list + 8, 8, BFD_ENDIAN_LITTLE);
9291a0cd
TT
2713 cu_list += 2 * 8;
2714
2715 the_cu = OBSTACK_ZALLOC (&objfile->objfile_obstack,
2716 struct dwarf2_per_cu_data);
b64f50a1 2717 the_cu->offset.sect_off = offset;
9291a0cd
TT
2718 the_cu->length = length;
2719 the_cu->objfile = objfile;
8a0459fd 2720 the_cu->section = section;
9291a0cd
TT
2721 the_cu->v.quick = OBSTACK_ZALLOC (&objfile->objfile_obstack,
2722 struct dwarf2_per_cu_quick_data);
2ec9a5e0
TT
2723 the_cu->is_dwz = is_dwz;
2724 dwarf2_per_objfile->all_comp_units[base_offset + i / 2] = the_cu;
9291a0cd 2725 }
9291a0cd
TT
2726}
2727
2ec9a5e0 2728/* Read the CU list from the mapped index, and use it to create all
74a0d9f6 2729 the CU objects for this objfile. */
2ec9a5e0 2730
74a0d9f6 2731static void
2ec9a5e0
TT
2732create_cus_from_index (struct objfile *objfile,
2733 const gdb_byte *cu_list, offset_type cu_list_elements,
2734 const gdb_byte *dwz_list, offset_type dwz_elements)
2735{
2736 struct dwz_file *dwz;
2737
2738 dwarf2_per_objfile->n_comp_units = (cu_list_elements + dwz_elements) / 2;
2739 dwarf2_per_objfile->all_comp_units
2740 = obstack_alloc (&objfile->objfile_obstack,
2741 dwarf2_per_objfile->n_comp_units
2742 * sizeof (struct dwarf2_per_cu_data *));
2743
74a0d9f6
JK
2744 create_cus_from_index_list (objfile, cu_list, cu_list_elements,
2745 &dwarf2_per_objfile->info, 0, 0);
2ec9a5e0
TT
2746
2747 if (dwz_elements == 0)
74a0d9f6 2748 return;
2ec9a5e0
TT
2749
2750 dwz = dwarf2_get_dwz_file ();
74a0d9f6
JK
2751 create_cus_from_index_list (objfile, dwz_list, dwz_elements, &dwz->info, 1,
2752 cu_list_elements / 2);
2ec9a5e0
TT
2753}
2754
1fd400ff 2755/* Create the signatured type hash table from the index. */
673bfd45 2756
74a0d9f6 2757static void
673bfd45 2758create_signatured_type_table_from_index (struct objfile *objfile,
8b70b953 2759 struct dwarf2_section_info *section,
673bfd45
DE
2760 const gdb_byte *bytes,
2761 offset_type elements)
1fd400ff
TT
2762{
2763 offset_type i;
673bfd45 2764 htab_t sig_types_hash;
1fd400ff 2765
d467dd73
DE
2766 dwarf2_per_objfile->n_type_units = elements / 3;
2767 dwarf2_per_objfile->all_type_units
a2ce51a0
DE
2768 = xmalloc (dwarf2_per_objfile->n_type_units
2769 * sizeof (struct signatured_type *));
1fd400ff 2770
673bfd45 2771 sig_types_hash = allocate_signatured_type_table (objfile);
1fd400ff
TT
2772
2773 for (i = 0; i < elements; i += 3)
2774 {
52dc124a
DE
2775 struct signatured_type *sig_type;
2776 ULONGEST offset, type_offset_in_tu, signature;
1fd400ff
TT
2777 void **slot;
2778
74a0d9f6
JK
2779 gdb_static_assert (sizeof (ULONGEST) >= 8);
2780 offset = extract_unsigned_integer (bytes, 8, BFD_ENDIAN_LITTLE);
2781 type_offset_in_tu = extract_unsigned_integer (bytes + 8, 8,
2782 BFD_ENDIAN_LITTLE);
1fd400ff
TT
2783 signature = extract_unsigned_integer (bytes + 16, 8, BFD_ENDIAN_LITTLE);
2784 bytes += 3 * 8;
2785
52dc124a 2786 sig_type = OBSTACK_ZALLOC (&objfile->objfile_obstack,
1fd400ff 2787 struct signatured_type);
52dc124a 2788 sig_type->signature = signature;
3019eac3
DE
2789 sig_type->type_offset_in_tu.cu_off = type_offset_in_tu;
2790 sig_type->per_cu.is_debug_types = 1;
8a0459fd 2791 sig_type->per_cu.section = section;
52dc124a
DE
2792 sig_type->per_cu.offset.sect_off = offset;
2793 sig_type->per_cu.objfile = objfile;
2794 sig_type->per_cu.v.quick
1fd400ff
TT
2795 = OBSTACK_ZALLOC (&objfile->objfile_obstack,
2796 struct dwarf2_per_cu_quick_data);
2797
52dc124a
DE
2798 slot = htab_find_slot (sig_types_hash, sig_type, INSERT);
2799 *slot = sig_type;
1fd400ff 2800
b4dd5633 2801 dwarf2_per_objfile->all_type_units[i / 3] = sig_type;
1fd400ff
TT
2802 }
2803
673bfd45 2804 dwarf2_per_objfile->signatured_types = sig_types_hash;
1fd400ff
TT
2805}
2806
9291a0cd
TT
2807/* Read the address map data from the mapped index, and use it to
2808 populate the objfile's psymtabs_addrmap. */
2fdf6df6 2809
9291a0cd
TT
2810static void
2811create_addrmap_from_index (struct objfile *objfile, struct mapped_index *index)
2812{
2813 const gdb_byte *iter, *end;
2814 struct obstack temp_obstack;
2815 struct addrmap *mutable_map;
2816 struct cleanup *cleanup;
2817 CORE_ADDR baseaddr;
2818
2819 obstack_init (&temp_obstack);
2820 cleanup = make_cleanup_obstack_free (&temp_obstack);
2821 mutable_map = addrmap_create_mutable (&temp_obstack);
2822
2823 iter = index->address_table;
2824 end = iter + index->address_table_size;
2825
2826 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
2827
2828 while (iter < end)
2829 {
2830 ULONGEST hi, lo, cu_index;
2831 lo = extract_unsigned_integer (iter, 8, BFD_ENDIAN_LITTLE);
2832 iter += 8;
2833 hi = extract_unsigned_integer (iter, 8, BFD_ENDIAN_LITTLE);
2834 iter += 8;
2835 cu_index = extract_unsigned_integer (iter, 4, BFD_ENDIAN_LITTLE);
2836 iter += 4;
f652bce2 2837
24a55014 2838 if (lo > hi)
f652bce2 2839 {
24a55014
DE
2840 complaint (&symfile_complaints,
2841 _(".gdb_index address table has invalid range (%s - %s)"),
c0cd8254 2842 hex_string (lo), hex_string (hi));
24a55014 2843 continue;
f652bce2 2844 }
24a55014
DE
2845
2846 if (cu_index >= dwarf2_per_objfile->n_comp_units)
f652bce2
DE
2847 {
2848 complaint (&symfile_complaints,
2849 _(".gdb_index address table has invalid CU number %u"),
2850 (unsigned) cu_index);
24a55014 2851 continue;
f652bce2 2852 }
24a55014
DE
2853
2854 addrmap_set_empty (mutable_map, lo + baseaddr, hi + baseaddr - 1,
2855 dw2_get_cu (cu_index));
9291a0cd
TT
2856 }
2857
2858 objfile->psymtabs_addrmap = addrmap_create_fixed (mutable_map,
2859 &objfile->objfile_obstack);
2860 do_cleanups (cleanup);
2861}
2862
59d7bcaf
JK
2863/* The hash function for strings in the mapped index. This is the same as
2864 SYMBOL_HASH_NEXT, but we keep a separate copy to maintain control over the
2865 implementation. This is necessary because the hash function is tied to the
2866 format of the mapped index file. The hash values do not have to match with
559a7a62
JK
2867 SYMBOL_HASH_NEXT.
2868
2869 Use INT_MAX for INDEX_VERSION if you generate the current index format. */
2fdf6df6 2870
9291a0cd 2871static hashval_t
559a7a62 2872mapped_index_string_hash (int index_version, const void *p)
9291a0cd
TT
2873{
2874 const unsigned char *str = (const unsigned char *) p;
2875 hashval_t r = 0;
2876 unsigned char c;
2877
2878 while ((c = *str++) != 0)
559a7a62
JK
2879 {
2880 if (index_version >= 5)
2881 c = tolower (c);
2882 r = r * 67 + c - 113;
2883 }
9291a0cd
TT
2884
2885 return r;
2886}
2887
2888/* Find a slot in the mapped index INDEX for the object named NAME.
2889 If NAME is found, set *VEC_OUT to point to the CU vector in the
2890 constant pool and return 1. If NAME cannot be found, return 0. */
2fdf6df6 2891
9291a0cd
TT
2892static int
2893find_slot_in_mapped_hash (struct mapped_index *index, const char *name,
2894 offset_type **vec_out)
2895{
0cf03b49
JK
2896 struct cleanup *back_to = make_cleanup (null_cleanup, 0);
2897 offset_type hash;
9291a0cd 2898 offset_type slot, step;
559a7a62 2899 int (*cmp) (const char *, const char *);
9291a0cd 2900
0cf03b49
JK
2901 if (current_language->la_language == language_cplus
2902 || current_language->la_language == language_java
2903 || current_language->la_language == language_fortran)
2904 {
2905 /* NAME is already canonical. Drop any qualifiers as .gdb_index does
2906 not contain any. */
2907 const char *paren = strchr (name, '(');
2908
2909 if (paren)
2910 {
2911 char *dup;
2912
2913 dup = xmalloc (paren - name + 1);
2914 memcpy (dup, name, paren - name);
2915 dup[paren - name] = 0;
2916
2917 make_cleanup (xfree, dup);
2918 name = dup;
2919 }
2920 }
2921
559a7a62 2922 /* Index version 4 did not support case insensitive searches. But the
feea76c2 2923 indices for case insensitive languages are built in lowercase, therefore
559a7a62
JK
2924 simulate our NAME being searched is also lowercased. */
2925 hash = mapped_index_string_hash ((index->version == 4
2926 && case_sensitivity == case_sensitive_off
2927 ? 5 : index->version),
2928 name);
2929
3876f04e
DE
2930 slot = hash & (index->symbol_table_slots - 1);
2931 step = ((hash * 17) & (index->symbol_table_slots - 1)) | 1;
559a7a62 2932 cmp = (case_sensitivity == case_sensitive_on ? strcmp : strcasecmp);
9291a0cd
TT
2933
2934 for (;;)
2935 {
2936 /* Convert a slot number to an offset into the table. */
2937 offset_type i = 2 * slot;
2938 const char *str;
3876f04e 2939 if (index->symbol_table[i] == 0 && index->symbol_table[i + 1] == 0)
0cf03b49
JK
2940 {
2941 do_cleanups (back_to);
2942 return 0;
2943 }
9291a0cd 2944
3876f04e 2945 str = index->constant_pool + MAYBE_SWAP (index->symbol_table[i]);
559a7a62 2946 if (!cmp (name, str))
9291a0cd
TT
2947 {
2948 *vec_out = (offset_type *) (index->constant_pool
3876f04e 2949 + MAYBE_SWAP (index->symbol_table[i + 1]));
0cf03b49 2950 do_cleanups (back_to);
9291a0cd
TT
2951 return 1;
2952 }
2953
3876f04e 2954 slot = (slot + step) & (index->symbol_table_slots - 1);
9291a0cd
TT
2955 }
2956}
2957
2ec9a5e0
TT
2958/* A helper function that reads the .gdb_index from SECTION and fills
2959 in MAP. FILENAME is the name of the file containing the section;
2960 it is used for error reporting. DEPRECATED_OK is nonzero if it is
2961 ok to use deprecated sections.
2962
2963 CU_LIST, CU_LIST_ELEMENTS, TYPES_LIST, and TYPES_LIST_ELEMENTS are
2964 out parameters that are filled in with information about the CU and
2965 TU lists in the section.
2966
2967 Returns 1 if all went well, 0 otherwise. */
2fdf6df6 2968
9291a0cd 2969static int
2ec9a5e0
TT
2970read_index_from_section (struct objfile *objfile,
2971 const char *filename,
2972 int deprecated_ok,
2973 struct dwarf2_section_info *section,
2974 struct mapped_index *map,
2975 const gdb_byte **cu_list,
2976 offset_type *cu_list_elements,
2977 const gdb_byte **types_list,
2978 offset_type *types_list_elements)
9291a0cd 2979{
948f8e3d 2980 const gdb_byte *addr;
2ec9a5e0 2981 offset_type version;
b3b272e1 2982 offset_type *metadata;
1fd400ff 2983 int i;
9291a0cd 2984
2ec9a5e0 2985 if (dwarf2_section_empty_p (section))
9291a0cd 2986 return 0;
82430852
JK
2987
2988 /* Older elfutils strip versions could keep the section in the main
2989 executable while splitting it for the separate debug info file. */
a32a8923 2990 if ((get_section_flags (section) & SEC_HAS_CONTENTS) == 0)
82430852
JK
2991 return 0;
2992
2ec9a5e0 2993 dwarf2_read_section (objfile, section);
9291a0cd 2994
2ec9a5e0 2995 addr = section->buffer;
9291a0cd 2996 /* Version check. */
1fd400ff 2997 version = MAYBE_SWAP (*(offset_type *) addr);
987d643c 2998 /* Versions earlier than 3 emitted every copy of a psymbol. This
a6e293d1 2999 causes the index to behave very poorly for certain requests. Version 3
831adc1f 3000 contained incomplete addrmap. So, it seems better to just ignore such
481860b3 3001 indices. */
831adc1f 3002 if (version < 4)
481860b3
GB
3003 {
3004 static int warning_printed = 0;
3005 if (!warning_printed)
3006 {
3007 warning (_("Skipping obsolete .gdb_index section in %s."),
2ec9a5e0 3008 filename);
481860b3
GB
3009 warning_printed = 1;
3010 }
3011 return 0;
3012 }
3013 /* Index version 4 uses a different hash function than index version
3014 5 and later.
3015
3016 Versions earlier than 6 did not emit psymbols for inlined
3017 functions. Using these files will cause GDB not to be able to
3018 set breakpoints on inlined functions by name, so we ignore these
e615022a
DE
3019 indices unless the user has done
3020 "set use-deprecated-index-sections on". */
2ec9a5e0 3021 if (version < 6 && !deprecated_ok)
481860b3
GB
3022 {
3023 static int warning_printed = 0;
3024 if (!warning_printed)
3025 {
e615022a
DE
3026 warning (_("\
3027Skipping deprecated .gdb_index section in %s.\n\
3028Do \"set use-deprecated-index-sections on\" before the file is read\n\
3029to use the section anyway."),
2ec9a5e0 3030 filename);
481860b3
GB
3031 warning_printed = 1;
3032 }
3033 return 0;
3034 }
796a7ff8
DE
3035 /* Version 7 indices generated by gold refer to the CU for a symbol instead
3036 of the TU (for symbols coming from TUs). It's just a performance bug, and
3037 we can't distinguish gdb-generated indices from gold-generated ones, so
3038 nothing to do here. */
3039
481860b3 3040 /* Indexes with higher version than the one supported by GDB may be no
594e8718 3041 longer backward compatible. */
796a7ff8 3042 if (version > 8)
594e8718 3043 return 0;
9291a0cd 3044
559a7a62 3045 map->version = version;
2ec9a5e0 3046 map->total_size = section->size;
9291a0cd
TT
3047
3048 metadata = (offset_type *) (addr + sizeof (offset_type));
1fd400ff
TT
3049
3050 i = 0;
2ec9a5e0
TT
3051 *cu_list = addr + MAYBE_SWAP (metadata[i]);
3052 *cu_list_elements = ((MAYBE_SWAP (metadata[i + 1]) - MAYBE_SWAP (metadata[i]))
3053 / 8);
1fd400ff
TT
3054 ++i;
3055
2ec9a5e0
TT
3056 *types_list = addr + MAYBE_SWAP (metadata[i]);
3057 *types_list_elements = ((MAYBE_SWAP (metadata[i + 1])
3058 - MAYBE_SWAP (metadata[i]))
3059 / 8);
987d643c 3060 ++i;
1fd400ff
TT
3061
3062 map->address_table = addr + MAYBE_SWAP (metadata[i]);
3063 map->address_table_size = (MAYBE_SWAP (metadata[i + 1])
3064 - MAYBE_SWAP (metadata[i]));
3065 ++i;
3066
3876f04e
DE
3067 map->symbol_table = (offset_type *) (addr + MAYBE_SWAP (metadata[i]));
3068 map->symbol_table_slots = ((MAYBE_SWAP (metadata[i + 1])
3069 - MAYBE_SWAP (metadata[i]))
3070 / (2 * sizeof (offset_type)));
1fd400ff 3071 ++i;
9291a0cd 3072
f9d83a0b 3073 map->constant_pool = (char *) (addr + MAYBE_SWAP (metadata[i]));
1fd400ff 3074
2ec9a5e0
TT
3075 return 1;
3076}
3077
3078
3079/* Read the index file. If everything went ok, initialize the "quick"
3080 elements of all the CUs and return 1. Otherwise, return 0. */
3081
3082static int
3083dwarf2_read_index (struct objfile *objfile)
3084{
3085 struct mapped_index local_map, *map;
3086 const gdb_byte *cu_list, *types_list, *dwz_list = NULL;
3087 offset_type cu_list_elements, types_list_elements, dwz_list_elements = 0;
4db1a1dc 3088 struct dwz_file *dwz;
2ec9a5e0 3089
4262abfb 3090 if (!read_index_from_section (objfile, objfile_name (objfile),
2ec9a5e0
TT
3091 use_deprecated_index_sections,
3092 &dwarf2_per_objfile->gdb_index, &local_map,
3093 &cu_list, &cu_list_elements,
3094 &types_list, &types_list_elements))
3095 return 0;
3096
0fefef59 3097 /* Don't use the index if it's empty. */
2ec9a5e0 3098 if (local_map.symbol_table_slots == 0)
0fefef59
DE
3099 return 0;
3100
2ec9a5e0
TT
3101 /* If there is a .dwz file, read it so we can get its CU list as
3102 well. */
4db1a1dc
TT
3103 dwz = dwarf2_get_dwz_file ();
3104 if (dwz != NULL)
2ec9a5e0 3105 {
2ec9a5e0
TT
3106 struct mapped_index dwz_map;
3107 const gdb_byte *dwz_types_ignore;
3108 offset_type dwz_types_elements_ignore;
3109
3110 if (!read_index_from_section (objfile, bfd_get_filename (dwz->dwz_bfd),
3111 1,
3112 &dwz->gdb_index, &dwz_map,
3113 &dwz_list, &dwz_list_elements,
3114 &dwz_types_ignore,
3115 &dwz_types_elements_ignore))
3116 {
3117 warning (_("could not read '.gdb_index' section from %s; skipping"),
3118 bfd_get_filename (dwz->dwz_bfd));
3119 return 0;
3120 }
3121 }
3122
74a0d9f6
JK
3123 create_cus_from_index (objfile, cu_list, cu_list_elements, dwz_list,
3124 dwz_list_elements);
1fd400ff 3125
8b70b953
TT
3126 if (types_list_elements)
3127 {
3128 struct dwarf2_section_info *section;
3129
3130 /* We can only handle a single .debug_types when we have an
3131 index. */
3132 if (VEC_length (dwarf2_section_info_def, dwarf2_per_objfile->types) != 1)
3133 return 0;
3134
3135 section = VEC_index (dwarf2_section_info_def,
3136 dwarf2_per_objfile->types, 0);
3137
74a0d9f6
JK
3138 create_signatured_type_table_from_index (objfile, section, types_list,
3139 types_list_elements);
8b70b953 3140 }
9291a0cd 3141
2ec9a5e0
TT
3142 create_addrmap_from_index (objfile, &local_map);
3143
3144 map = obstack_alloc (&objfile->objfile_obstack, sizeof (struct mapped_index));
3145 *map = local_map;
9291a0cd
TT
3146
3147 dwarf2_per_objfile->index_table = map;
3148 dwarf2_per_objfile->using_index = 1;
7b9f3c50
DE
3149 dwarf2_per_objfile->quick_file_names_table =
3150 create_quick_file_names_table (dwarf2_per_objfile->n_comp_units);
9291a0cd
TT
3151
3152 return 1;
3153}
3154
3155/* A helper for the "quick" functions which sets the global
3156 dwarf2_per_objfile according to OBJFILE. */
2fdf6df6 3157
9291a0cd
TT
3158static void
3159dw2_setup (struct objfile *objfile)
3160{
3161 dwarf2_per_objfile = objfile_data (objfile, dwarf2_objfile_data_key);
3162 gdb_assert (dwarf2_per_objfile);
3163}
3164
dee91e82 3165/* die_reader_func for dw2_get_file_names. */
2fdf6df6 3166
dee91e82
DE
3167static void
3168dw2_get_file_names_reader (const struct die_reader_specs *reader,
d521ce57 3169 const gdb_byte *info_ptr,
dee91e82
DE
3170 struct die_info *comp_unit_die,
3171 int has_children,
3172 void *data)
9291a0cd 3173{
dee91e82
DE
3174 struct dwarf2_cu *cu = reader->cu;
3175 struct dwarf2_per_cu_data *this_cu = cu->per_cu;
3176 struct objfile *objfile = dwarf2_per_objfile->objfile;
094b34ac 3177 struct dwarf2_per_cu_data *lh_cu;
7b9f3c50 3178 struct line_header *lh;
9291a0cd 3179 struct attribute *attr;
dee91e82 3180 int i;
15d034d0 3181 const char *name, *comp_dir;
7b9f3c50
DE
3182 void **slot;
3183 struct quick_file_names *qfn;
3184 unsigned int line_offset;
9291a0cd 3185
0186c6a7
DE
3186 gdb_assert (! this_cu->is_debug_types);
3187
07261596
TT
3188 /* Our callers never want to match partial units -- instead they
3189 will match the enclosing full CU. */
3190 if (comp_unit_die->tag == DW_TAG_partial_unit)
3191 {
3192 this_cu->v.quick->no_file_data = 1;
3193 return;
3194 }
3195
0186c6a7 3196 lh_cu = this_cu;
7b9f3c50
DE
3197 lh = NULL;
3198 slot = NULL;
3199 line_offset = 0;
dee91e82
DE
3200
3201 attr = dwarf2_attr (comp_unit_die, DW_AT_stmt_list, cu);
9291a0cd
TT
3202 if (attr)
3203 {
7b9f3c50
DE
3204 struct quick_file_names find_entry;
3205
3206 line_offset = DW_UNSND (attr);
3207
3208 /* We may have already read in this line header (TU line header sharing).
3209 If we have we're done. */
094b34ac
DE
3210 find_entry.hash.dwo_unit = cu->dwo_unit;
3211 find_entry.hash.line_offset.sect_off = line_offset;
7b9f3c50
DE
3212 slot = htab_find_slot (dwarf2_per_objfile->quick_file_names_table,
3213 &find_entry, INSERT);
3214 if (*slot != NULL)
3215 {
094b34ac 3216 lh_cu->v.quick->file_names = *slot;
dee91e82 3217 return;
7b9f3c50
DE
3218 }
3219
3019eac3 3220 lh = dwarf_decode_line_header (line_offset, cu);
9291a0cd
TT
3221 }
3222 if (lh == NULL)
3223 {
094b34ac 3224 lh_cu->v.quick->no_file_data = 1;
dee91e82 3225 return;
9291a0cd
TT
3226 }
3227
7b9f3c50 3228 qfn = obstack_alloc (&objfile->objfile_obstack, sizeof (*qfn));
094b34ac
DE
3229 qfn->hash.dwo_unit = cu->dwo_unit;
3230 qfn->hash.line_offset.sect_off = line_offset;
7b9f3c50
DE
3231 gdb_assert (slot != NULL);
3232 *slot = qfn;
9291a0cd 3233
dee91e82 3234 find_file_and_directory (comp_unit_die, cu, &name, &comp_dir);
9291a0cd 3235
7b9f3c50
DE
3236 qfn->num_file_names = lh->num_file_names;
3237 qfn->file_names = obstack_alloc (&objfile->objfile_obstack,
3238 lh->num_file_names * sizeof (char *));
9291a0cd 3239 for (i = 0; i < lh->num_file_names; ++i)
7b9f3c50
DE
3240 qfn->file_names[i] = file_full_name (i + 1, lh, comp_dir);
3241 qfn->real_names = NULL;
9291a0cd 3242
7b9f3c50 3243 free_line_header (lh);
7b9f3c50 3244
094b34ac 3245 lh_cu->v.quick->file_names = qfn;
dee91e82
DE
3246}
3247
3248/* A helper for the "quick" functions which attempts to read the line
3249 table for THIS_CU. */
3250
3251static struct quick_file_names *
e4a48d9d 3252dw2_get_file_names (struct dwarf2_per_cu_data *this_cu)
dee91e82 3253{
0186c6a7
DE
3254 /* This should never be called for TUs. */
3255 gdb_assert (! this_cu->is_debug_types);
3256 /* Nor type unit groups. */
3257 gdb_assert (! IS_TYPE_UNIT_GROUP (this_cu));
f4dc4d17 3258
dee91e82
DE
3259 if (this_cu->v.quick->file_names != NULL)
3260 return this_cu->v.quick->file_names;
3261 /* If we know there is no line data, no point in looking again. */
3262 if (this_cu->v.quick->no_file_data)
3263 return NULL;
3264
0186c6a7 3265 init_cutu_and_read_dies_simple (this_cu, dw2_get_file_names_reader, NULL);
dee91e82
DE
3266
3267 if (this_cu->v.quick->no_file_data)
3268 return NULL;
3269 return this_cu->v.quick->file_names;
9291a0cd
TT
3270}
3271
3272/* A helper for the "quick" functions which computes and caches the
7b9f3c50 3273 real path for a given file name from the line table. */
2fdf6df6 3274
9291a0cd 3275static const char *
7b9f3c50
DE
3276dw2_get_real_path (struct objfile *objfile,
3277 struct quick_file_names *qfn, int index)
9291a0cd 3278{
7b9f3c50
DE
3279 if (qfn->real_names == NULL)
3280 qfn->real_names = OBSTACK_CALLOC (&objfile->objfile_obstack,
fd0a4d76 3281 qfn->num_file_names, char *);
9291a0cd 3282
7b9f3c50
DE
3283 if (qfn->real_names[index] == NULL)
3284 qfn->real_names[index] = gdb_realpath (qfn->file_names[index]);
9291a0cd 3285
7b9f3c50 3286 return qfn->real_names[index];
9291a0cd
TT
3287}
3288
3289static struct symtab *
3290dw2_find_last_source_symtab (struct objfile *objfile)
3291{
3292 int index;
ae2de4f8 3293
9291a0cd
TT
3294 dw2_setup (objfile);
3295 index = dwarf2_per_objfile->n_comp_units - 1;
a0f42c21 3296 return dw2_instantiate_symtab (dw2_get_cu (index));
9291a0cd
TT
3297}
3298
7b9f3c50
DE
3299/* Traversal function for dw2_forget_cached_source_info. */
3300
3301static int
3302dw2_free_cached_file_names (void **slot, void *info)
9291a0cd 3303{
7b9f3c50 3304 struct quick_file_names *file_data = (struct quick_file_names *) *slot;
9291a0cd 3305
7b9f3c50 3306 if (file_data->real_names)
9291a0cd 3307 {
7b9f3c50 3308 int i;
9291a0cd 3309
7b9f3c50 3310 for (i = 0; i < file_data->num_file_names; ++i)
9291a0cd 3311 {
7b9f3c50
DE
3312 xfree ((void*) file_data->real_names[i]);
3313 file_data->real_names[i] = NULL;
9291a0cd
TT
3314 }
3315 }
7b9f3c50
DE
3316
3317 return 1;
3318}
3319
3320static void
3321dw2_forget_cached_source_info (struct objfile *objfile)
3322{
3323 dw2_setup (objfile);
3324
3325 htab_traverse_noresize (dwarf2_per_objfile->quick_file_names_table,
3326 dw2_free_cached_file_names, NULL);
9291a0cd
TT
3327}
3328
f8eba3c6
TT
3329/* Helper function for dw2_map_symtabs_matching_filename that expands
3330 the symtabs and calls the iterator. */
3331
3332static int
3333dw2_map_expand_apply (struct objfile *objfile,
3334 struct dwarf2_per_cu_data *per_cu,
f5b95b50 3335 const char *name, const char *real_path,
f8eba3c6
TT
3336 int (*callback) (struct symtab *, void *),
3337 void *data)
3338{
3339 struct symtab *last_made = objfile->symtabs;
3340
3341 /* Don't visit already-expanded CUs. */
3342 if (per_cu->v.quick->symtab)
3343 return 0;
3344
3345 /* This may expand more than one symtab, and we want to iterate over
3346 all of them. */
a0f42c21 3347 dw2_instantiate_symtab (per_cu);
f8eba3c6 3348
f5b95b50 3349 return iterate_over_some_symtabs (name, real_path, callback, data,
f8eba3c6
TT
3350 objfile->symtabs, last_made);
3351}
3352
3353/* Implementation of the map_symtabs_matching_filename method. */
3354
9291a0cd 3355static int
f8eba3c6 3356dw2_map_symtabs_matching_filename (struct objfile *objfile, const char *name,
f5b95b50 3357 const char *real_path,
f8eba3c6
TT
3358 int (*callback) (struct symtab *, void *),
3359 void *data)
9291a0cd
TT
3360{
3361 int i;
c011a4f4 3362 const char *name_basename = lbasename (name);
9291a0cd
TT
3363
3364 dw2_setup (objfile);
ae2de4f8 3365
848e3e78
DE
3366 /* The rule is CUs specify all the files, including those used by
3367 any TU, so there's no need to scan TUs here. */
f4dc4d17 3368
848e3e78 3369 for (i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
9291a0cd
TT
3370 {
3371 int j;
f4dc4d17 3372 struct dwarf2_per_cu_data *per_cu = dw2_get_primary_cu (i);
7b9f3c50 3373 struct quick_file_names *file_data;
9291a0cd 3374
3d7bb9d9 3375 /* We only need to look at symtabs not already expanded. */
e254ef6a 3376 if (per_cu->v.quick->symtab)
9291a0cd
TT
3377 continue;
3378
e4a48d9d 3379 file_data = dw2_get_file_names (per_cu);
7b9f3c50 3380 if (file_data == NULL)
9291a0cd
TT
3381 continue;
3382
7b9f3c50 3383 for (j = 0; j < file_data->num_file_names; ++j)
9291a0cd 3384 {
7b9f3c50 3385 const char *this_name = file_data->file_names[j];
da235a7c 3386 const char *this_real_name;
9291a0cd 3387
af529f8f 3388 if (compare_filenames_for_search (this_name, name))
9291a0cd 3389 {
f5b95b50 3390 if (dw2_map_expand_apply (objfile, per_cu, name, real_path,
f8eba3c6
TT
3391 callback, data))
3392 return 1;
288e77a7 3393 continue;
4aac40c8 3394 }
9291a0cd 3395
c011a4f4
DE
3396 /* Before we invoke realpath, which can get expensive when many
3397 files are involved, do a quick comparison of the basenames. */
3398 if (! basenames_may_differ
3399 && FILENAME_CMP (lbasename (this_name), name_basename) != 0)
3400 continue;
3401
da235a7c
JK
3402 this_real_name = dw2_get_real_path (objfile, file_data, j);
3403 if (compare_filenames_for_search (this_real_name, name))
9291a0cd 3404 {
da235a7c
JK
3405 if (dw2_map_expand_apply (objfile, per_cu, name, real_path,
3406 callback, data))
3407 return 1;
288e77a7 3408 continue;
da235a7c 3409 }
9291a0cd 3410
da235a7c
JK
3411 if (real_path != NULL)
3412 {
af529f8f
JK
3413 gdb_assert (IS_ABSOLUTE_PATH (real_path));
3414 gdb_assert (IS_ABSOLUTE_PATH (name));
7b9f3c50 3415 if (this_real_name != NULL
af529f8f 3416 && FILENAME_CMP (real_path, this_real_name) == 0)
9291a0cd 3417 {
f5b95b50 3418 if (dw2_map_expand_apply (objfile, per_cu, name, real_path,
f8eba3c6
TT
3419 callback, data))
3420 return 1;
288e77a7 3421 continue;
9291a0cd
TT
3422 }
3423 }
3424 }
3425 }
3426
9291a0cd
TT
3427 return 0;
3428}
3429
da51c347
DE
3430/* Struct used to manage iterating over all CUs looking for a symbol. */
3431
3432struct dw2_symtab_iterator
9291a0cd 3433{
da51c347
DE
3434 /* The internalized form of .gdb_index. */
3435 struct mapped_index *index;
3436 /* If non-zero, only look for symbols that match BLOCK_INDEX. */
3437 int want_specific_block;
3438 /* One of GLOBAL_BLOCK or STATIC_BLOCK.
3439 Unused if !WANT_SPECIFIC_BLOCK. */
3440 int block_index;
3441 /* The kind of symbol we're looking for. */
3442 domain_enum domain;
3443 /* The list of CUs from the index entry of the symbol,
3444 or NULL if not found. */
3445 offset_type *vec;
3446 /* The next element in VEC to look at. */
3447 int next;
3448 /* The number of elements in VEC, or zero if there is no match. */
3449 int length;
3450};
9291a0cd 3451
da51c347
DE
3452/* Initialize the index symtab iterator ITER.
3453 If WANT_SPECIFIC_BLOCK is non-zero, only look for symbols
3454 in block BLOCK_INDEX. Otherwise BLOCK_INDEX is ignored. */
2fdf6df6 3455
9291a0cd 3456static void
da51c347
DE
3457dw2_symtab_iter_init (struct dw2_symtab_iterator *iter,
3458 struct mapped_index *index,
3459 int want_specific_block,
3460 int block_index,
3461 domain_enum domain,
3462 const char *name)
3463{
3464 iter->index = index;
3465 iter->want_specific_block = want_specific_block;
3466 iter->block_index = block_index;
3467 iter->domain = domain;
3468 iter->next = 0;
3469
3470 if (find_slot_in_mapped_hash (index, name, &iter->vec))
3471 iter->length = MAYBE_SWAP (*iter->vec);
3472 else
3473 {
3474 iter->vec = NULL;
3475 iter->length = 0;
3476 }
3477}
3478
3479/* Return the next matching CU or NULL if there are no more. */
3480
3481static struct dwarf2_per_cu_data *
3482dw2_symtab_iter_next (struct dw2_symtab_iterator *iter)
3483{
3484 for ( ; iter->next < iter->length; ++iter->next)
3485 {
3486 offset_type cu_index_and_attrs =
3487 MAYBE_SWAP (iter->vec[iter->next + 1]);
3488 offset_type cu_index = GDB_INDEX_CU_VALUE (cu_index_and_attrs);
3190f0c6 3489 struct dwarf2_per_cu_data *per_cu;
da51c347
DE
3490 int want_static = iter->block_index != GLOBAL_BLOCK;
3491 /* This value is only valid for index versions >= 7. */
3492 int is_static = GDB_INDEX_SYMBOL_STATIC_VALUE (cu_index_and_attrs);
3493 gdb_index_symbol_kind symbol_kind =
3494 GDB_INDEX_SYMBOL_KIND_VALUE (cu_index_and_attrs);
3495 /* Only check the symbol attributes if they're present.
3496 Indices prior to version 7 don't record them,
3497 and indices >= 7 may elide them for certain symbols
3498 (gold does this). */
3499 int attrs_valid =
3500 (iter->index->version >= 7
3501 && symbol_kind != GDB_INDEX_SYMBOL_KIND_NONE);
3502
3190f0c6
DE
3503 /* Don't crash on bad data. */
3504 if (cu_index >= (dwarf2_per_objfile->n_comp_units
3505 + dwarf2_per_objfile->n_type_units))
3506 {
3507 complaint (&symfile_complaints,
3508 _(".gdb_index entry has bad CU index"
4262abfb
JK
3509 " [in module %s]"),
3510 objfile_name (dwarf2_per_objfile->objfile));
3190f0c6
DE
3511 continue;
3512 }
3513
3514 per_cu = dw2_get_cu (cu_index);
3515
da51c347
DE
3516 /* Skip if already read in. */
3517 if (per_cu->v.quick->symtab)
3518 continue;
3519
3520 if (attrs_valid
3521 && iter->want_specific_block
3522 && want_static != is_static)
3523 continue;
3524
3525 /* Only check the symbol's kind if it has one. */
3526 if (attrs_valid)
3527 {
3528 switch (iter->domain)
3529 {
3530 case VAR_DOMAIN:
3531 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_VARIABLE
3532 && symbol_kind != GDB_INDEX_SYMBOL_KIND_FUNCTION
3533 /* Some types are also in VAR_DOMAIN. */
3534 && symbol_kind != GDB_INDEX_SYMBOL_KIND_TYPE)
3535 continue;
3536 break;
3537 case STRUCT_DOMAIN:
3538 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_TYPE)
3539 continue;
3540 break;
3541 case LABEL_DOMAIN:
3542 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_OTHER)
3543 continue;
3544 break;
3545 default:
3546 break;
3547 }
3548 }
3549
3550 ++iter->next;
3551 return per_cu;
3552 }
3553
3554 return NULL;
3555}
3556
3557static struct symtab *
3558dw2_lookup_symbol (struct objfile *objfile, int block_index,
3559 const char *name, domain_enum domain)
9291a0cd 3560{
da51c347 3561 struct symtab *stab_best = NULL;
156942c7
DE
3562 struct mapped_index *index;
3563
9291a0cd
TT
3564 dw2_setup (objfile);
3565
156942c7
DE
3566 index = dwarf2_per_objfile->index_table;
3567
da51c347 3568 /* index is NULL if OBJF_READNOW. */
156942c7 3569 if (index)
9291a0cd 3570 {
da51c347
DE
3571 struct dw2_symtab_iterator iter;
3572 struct dwarf2_per_cu_data *per_cu;
3573
3574 dw2_symtab_iter_init (&iter, index, 1, block_index, domain, name);
9291a0cd 3575
da51c347 3576 while ((per_cu = dw2_symtab_iter_next (&iter)) != NULL)
9291a0cd 3577 {
da51c347
DE
3578 struct symbol *sym = NULL;
3579 struct symtab *stab = dw2_instantiate_symtab (per_cu);
3580
3581 /* Some caution must be observed with overloaded functions
3582 and methods, since the index will not contain any overload
3583 information (but NAME might contain it). */
3584 if (stab->primary)
9291a0cd 3585 {
da51c347
DE
3586 struct blockvector *bv = BLOCKVECTOR (stab);
3587 struct block *block = BLOCKVECTOR_BLOCK (bv, block_index);
156942c7 3588
da51c347
DE
3589 sym = lookup_block_symbol (block, name, domain);
3590 }
1fd400ff 3591
da51c347
DE
3592 if (sym && strcmp_iw (SYMBOL_SEARCH_NAME (sym), name) == 0)
3593 {
3594 if (!TYPE_IS_OPAQUE (SYMBOL_TYPE (sym)))
3595 return stab;
3596
3597 stab_best = stab;
9291a0cd 3598 }
da51c347
DE
3599
3600 /* Keep looking through other CUs. */
9291a0cd
TT
3601 }
3602 }
9291a0cd 3603
da51c347 3604 return stab_best;
9291a0cd
TT
3605}
3606
3607static void
3608dw2_print_stats (struct objfile *objfile)
3609{
e4a48d9d 3610 int i, total, count;
9291a0cd
TT
3611
3612 dw2_setup (objfile);
e4a48d9d 3613 total = dwarf2_per_objfile->n_comp_units + dwarf2_per_objfile->n_type_units;
9291a0cd 3614 count = 0;
e4a48d9d 3615 for (i = 0; i < total; ++i)
9291a0cd 3616 {
e254ef6a 3617 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
9291a0cd 3618
e254ef6a 3619 if (!per_cu->v.quick->symtab)
9291a0cd
TT
3620 ++count;
3621 }
e4a48d9d 3622 printf_filtered (_(" Number of read CUs: %d\n"), total - count);
9291a0cd
TT
3623 printf_filtered (_(" Number of unread CUs: %d\n"), count);
3624}
3625
779bd270
DE
3626/* This dumps minimal information about the index.
3627 It is called via "mt print objfiles".
3628 One use is to verify .gdb_index has been loaded by the
3629 gdb.dwarf2/gdb-index.exp testcase. */
3630
9291a0cd
TT
3631static void
3632dw2_dump (struct objfile *objfile)
3633{
779bd270
DE
3634 dw2_setup (objfile);
3635 gdb_assert (dwarf2_per_objfile->using_index);
3636 printf_filtered (".gdb_index:");
3637 if (dwarf2_per_objfile->index_table != NULL)
3638 {
3639 printf_filtered (" version %d\n",
3640 dwarf2_per_objfile->index_table->version);
3641 }
3642 else
3643 printf_filtered (" faked for \"readnow\"\n");
3644 printf_filtered ("\n");
9291a0cd
TT
3645}
3646
3647static void
3189cb12
DE
3648dw2_relocate (struct objfile *objfile,
3649 const struct section_offsets *new_offsets,
3650 const struct section_offsets *delta)
9291a0cd
TT
3651{
3652 /* There's nothing to relocate here. */
3653}
3654
3655static void
3656dw2_expand_symtabs_for_function (struct objfile *objfile,
3657 const char *func_name)
3658{
da51c347
DE
3659 struct mapped_index *index;
3660
3661 dw2_setup (objfile);
3662
3663 index = dwarf2_per_objfile->index_table;
3664
3665 /* index is NULL if OBJF_READNOW. */
3666 if (index)
3667 {
3668 struct dw2_symtab_iterator iter;
3669 struct dwarf2_per_cu_data *per_cu;
3670
3671 /* Note: It doesn't matter what we pass for block_index here. */
3672 dw2_symtab_iter_init (&iter, index, 0, GLOBAL_BLOCK, VAR_DOMAIN,
3673 func_name);
3674
3675 while ((per_cu = dw2_symtab_iter_next (&iter)) != NULL)
3676 dw2_instantiate_symtab (per_cu);
3677 }
9291a0cd
TT
3678}
3679
3680static void
3681dw2_expand_all_symtabs (struct objfile *objfile)
3682{
3683 int i;
3684
3685 dw2_setup (objfile);
1fd400ff
TT
3686
3687 for (i = 0; i < (dwarf2_per_objfile->n_comp_units
d467dd73 3688 + dwarf2_per_objfile->n_type_units); ++i)
9291a0cd 3689 {
e254ef6a 3690 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
9291a0cd 3691
a0f42c21 3692 dw2_instantiate_symtab (per_cu);
9291a0cd
TT
3693 }
3694}
3695
3696static void
652a8996
JK
3697dw2_expand_symtabs_with_fullname (struct objfile *objfile,
3698 const char *fullname)
9291a0cd
TT
3699{
3700 int i;
3701
3702 dw2_setup (objfile);
d4637a04
DE
3703
3704 /* We don't need to consider type units here.
3705 This is only called for examining code, e.g. expand_line_sal.
3706 There can be an order of magnitude (or more) more type units
3707 than comp units, and we avoid them if we can. */
3708
3709 for (i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
9291a0cd
TT
3710 {
3711 int j;
e254ef6a 3712 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
7b9f3c50 3713 struct quick_file_names *file_data;
9291a0cd 3714
3d7bb9d9 3715 /* We only need to look at symtabs not already expanded. */
e254ef6a 3716 if (per_cu->v.quick->symtab)
9291a0cd
TT
3717 continue;
3718
e4a48d9d 3719 file_data = dw2_get_file_names (per_cu);
7b9f3c50 3720 if (file_data == NULL)
9291a0cd
TT
3721 continue;
3722
7b9f3c50 3723 for (j = 0; j < file_data->num_file_names; ++j)
9291a0cd 3724 {
652a8996
JK
3725 const char *this_fullname = file_data->file_names[j];
3726
3727 if (filename_cmp (this_fullname, fullname) == 0)
9291a0cd 3728 {
a0f42c21 3729 dw2_instantiate_symtab (per_cu);
9291a0cd
TT
3730 break;
3731 }
3732 }
3733 }
3734}
3735
9291a0cd 3736static void
ade7ed9e
DE
3737dw2_map_matching_symbols (struct objfile *objfile,
3738 const char * name, domain_enum namespace,
3739 int global,
40658b94
PH
3740 int (*callback) (struct block *,
3741 struct symbol *, void *),
2edb89d3
JK
3742 void *data, symbol_compare_ftype *match,
3743 symbol_compare_ftype *ordered_compare)
9291a0cd 3744{
40658b94 3745 /* Currently unimplemented; used for Ada. The function can be called if the
a9e6a4bb
JK
3746 current language is Ada for a non-Ada objfile using GNU index. As Ada
3747 does not look for non-Ada symbols this function should just return. */
9291a0cd
TT
3748}
3749
3750static void
f8eba3c6
TT
3751dw2_expand_symtabs_matching
3752 (struct objfile *objfile,
fbd9ab74 3753 int (*file_matcher) (const char *, void *, int basenames),
e078317b 3754 int (*name_matcher) (const char *, void *),
f8eba3c6
TT
3755 enum search_domain kind,
3756 void *data)
9291a0cd
TT
3757{
3758 int i;
3759 offset_type iter;
4b5246aa 3760 struct mapped_index *index;
9291a0cd
TT
3761
3762 dw2_setup (objfile);
ae2de4f8
DE
3763
3764 /* index_table is NULL if OBJF_READNOW. */
9291a0cd
TT
3765 if (!dwarf2_per_objfile->index_table)
3766 return;
4b5246aa 3767 index = dwarf2_per_objfile->index_table;
9291a0cd 3768
7b08b9eb 3769 if (file_matcher != NULL)
24c79950
TT
3770 {
3771 struct cleanup *cleanup;
3772 htab_t visited_found, visited_not_found;
3773
3774 visited_found = htab_create_alloc (10,
3775 htab_hash_pointer, htab_eq_pointer,
3776 NULL, xcalloc, xfree);
3777 cleanup = make_cleanup_htab_delete (visited_found);
3778 visited_not_found = htab_create_alloc (10,
3779 htab_hash_pointer, htab_eq_pointer,
3780 NULL, xcalloc, xfree);
3781 make_cleanup_htab_delete (visited_not_found);
3782
848e3e78
DE
3783 /* The rule is CUs specify all the files, including those used by
3784 any TU, so there's no need to scan TUs here. */
3785
3786 for (i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
24c79950
TT
3787 {
3788 int j;
f4dc4d17 3789 struct dwarf2_per_cu_data *per_cu = dw2_get_primary_cu (i);
24c79950
TT
3790 struct quick_file_names *file_data;
3791 void **slot;
7b08b9eb 3792
24c79950 3793 per_cu->v.quick->mark = 0;
3d7bb9d9 3794
24c79950
TT
3795 /* We only need to look at symtabs not already expanded. */
3796 if (per_cu->v.quick->symtab)
3797 continue;
7b08b9eb 3798
e4a48d9d 3799 file_data = dw2_get_file_names (per_cu);
24c79950
TT
3800 if (file_data == NULL)
3801 continue;
7b08b9eb 3802
24c79950
TT
3803 if (htab_find (visited_not_found, file_data) != NULL)
3804 continue;
3805 else if (htab_find (visited_found, file_data) != NULL)
3806 {
3807 per_cu->v.quick->mark = 1;
3808 continue;
3809 }
3810
3811 for (j = 0; j < file_data->num_file_names; ++j)
3812 {
da235a7c
JK
3813 const char *this_real_name;
3814
fbd9ab74 3815 if (file_matcher (file_data->file_names[j], data, 0))
24c79950
TT
3816 {
3817 per_cu->v.quick->mark = 1;
3818 break;
3819 }
da235a7c
JK
3820
3821 /* Before we invoke realpath, which can get expensive when many
3822 files are involved, do a quick comparison of the basenames. */
3823 if (!basenames_may_differ
3824 && !file_matcher (lbasename (file_data->file_names[j]),
3825 data, 1))
3826 continue;
3827
3828 this_real_name = dw2_get_real_path (objfile, file_data, j);
3829 if (file_matcher (this_real_name, data, 0))
3830 {
3831 per_cu->v.quick->mark = 1;
3832 break;
3833 }
24c79950
TT
3834 }
3835
3836 slot = htab_find_slot (per_cu->v.quick->mark
3837 ? visited_found
3838 : visited_not_found,
3839 file_data, INSERT);
3840 *slot = file_data;
3841 }
3842
3843 do_cleanups (cleanup);
3844 }
9291a0cd 3845
3876f04e 3846 for (iter = 0; iter < index->symbol_table_slots; ++iter)
9291a0cd
TT
3847 {
3848 offset_type idx = 2 * iter;
3849 const char *name;
3850 offset_type *vec, vec_len, vec_idx;
3851
3876f04e 3852 if (index->symbol_table[idx] == 0 && index->symbol_table[idx + 1] == 0)
9291a0cd
TT
3853 continue;
3854
3876f04e 3855 name = index->constant_pool + MAYBE_SWAP (index->symbol_table[idx]);
9291a0cd 3856
e078317b 3857 if (! (*name_matcher) (name, data))
9291a0cd
TT
3858 continue;
3859
3860 /* The name was matched, now expand corresponding CUs that were
3861 marked. */
4b5246aa 3862 vec = (offset_type *) (index->constant_pool
3876f04e 3863 + MAYBE_SWAP (index->symbol_table[idx + 1]));
9291a0cd
TT
3864 vec_len = MAYBE_SWAP (vec[0]);
3865 for (vec_idx = 0; vec_idx < vec_len; ++vec_idx)
3866 {
e254ef6a 3867 struct dwarf2_per_cu_data *per_cu;
156942c7
DE
3868 offset_type cu_index_and_attrs = MAYBE_SWAP (vec[vec_idx + 1]);
3869 gdb_index_symbol_kind symbol_kind =
3870 GDB_INDEX_SYMBOL_KIND_VALUE (cu_index_and_attrs);
3871 int cu_index = GDB_INDEX_CU_VALUE (cu_index_and_attrs);
3190f0c6
DE
3872 /* Only check the symbol attributes if they're present.
3873 Indices prior to version 7 don't record them,
3874 and indices >= 7 may elide them for certain symbols
3875 (gold does this). */
3876 int attrs_valid =
3877 (index->version >= 7
3878 && symbol_kind != GDB_INDEX_SYMBOL_KIND_NONE);
3879
3880 /* Only check the symbol's kind if it has one. */
3881 if (attrs_valid)
156942c7
DE
3882 {
3883 switch (kind)
3884 {
3885 case VARIABLES_DOMAIN:
3886 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_VARIABLE)
3887 continue;
3888 break;
3889 case FUNCTIONS_DOMAIN:
3890 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_FUNCTION)
3891 continue;
3892 break;
3893 case TYPES_DOMAIN:
3894 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_TYPE)
3895 continue;
3896 break;
3897 default:
3898 break;
3899 }
3900 }
3901
3190f0c6
DE
3902 /* Don't crash on bad data. */
3903 if (cu_index >= (dwarf2_per_objfile->n_comp_units
3904 + dwarf2_per_objfile->n_type_units))
3905 {
3906 complaint (&symfile_complaints,
3907 _(".gdb_index entry has bad CU index"
4262abfb 3908 " [in module %s]"), objfile_name (objfile));
3190f0c6
DE
3909 continue;
3910 }
3911
156942c7 3912 per_cu = dw2_get_cu (cu_index);
7b08b9eb 3913 if (file_matcher == NULL || per_cu->v.quick->mark)
a0f42c21 3914 dw2_instantiate_symtab (per_cu);
9291a0cd
TT
3915 }
3916 }
3917}
3918
9703b513
TT
3919/* A helper for dw2_find_pc_sect_symtab which finds the most specific
3920 symtab. */
3921
3922static struct symtab *
3923recursively_find_pc_sect_symtab (struct symtab *symtab, CORE_ADDR pc)
3924{
3925 int i;
3926
3927 if (BLOCKVECTOR (symtab) != NULL
3928 && blockvector_contains_pc (BLOCKVECTOR (symtab), pc))
3929 return symtab;
3930
a3ec0bb1
DE
3931 if (symtab->includes == NULL)
3932 return NULL;
3933
9703b513
TT
3934 for (i = 0; symtab->includes[i]; ++i)
3935 {
a3ec0bb1 3936 struct symtab *s = symtab->includes[i];
9703b513
TT
3937
3938 s = recursively_find_pc_sect_symtab (s, pc);
3939 if (s != NULL)
3940 return s;
3941 }
3942
3943 return NULL;
3944}
3945
9291a0cd
TT
3946static struct symtab *
3947dw2_find_pc_sect_symtab (struct objfile *objfile,
3948 struct minimal_symbol *msymbol,
3949 CORE_ADDR pc,
3950 struct obj_section *section,
3951 int warn_if_readin)
3952{
3953 struct dwarf2_per_cu_data *data;
9703b513 3954 struct symtab *result;
9291a0cd
TT
3955
3956 dw2_setup (objfile);
3957
3958 if (!objfile->psymtabs_addrmap)
3959 return NULL;
3960
3961 data = addrmap_find (objfile->psymtabs_addrmap, pc);
3962 if (!data)
3963 return NULL;
3964
3965 if (warn_if_readin && data->v.quick->symtab)
abebb8b0 3966 warning (_("(Internal error: pc %s in read in CU, but not in symtab.)"),
9291a0cd
TT
3967 paddress (get_objfile_arch (objfile), pc));
3968
9703b513
TT
3969 result = recursively_find_pc_sect_symtab (dw2_instantiate_symtab (data), pc);
3970 gdb_assert (result != NULL);
3971 return result;
9291a0cd
TT
3972}
3973
9291a0cd 3974static void
44b13c5a 3975dw2_map_symbol_filenames (struct objfile *objfile, symbol_filename_ftype *fun,
74e2f255 3976 void *data, int need_fullname)
9291a0cd
TT
3977{
3978 int i;
24c79950
TT
3979 struct cleanup *cleanup;
3980 htab_t visited = htab_create_alloc (10, htab_hash_pointer, htab_eq_pointer,
3981 NULL, xcalloc, xfree);
9291a0cd 3982
24c79950 3983 cleanup = make_cleanup_htab_delete (visited);
9291a0cd 3984 dw2_setup (objfile);
ae2de4f8 3985
848e3e78
DE
3986 /* The rule is CUs specify all the files, including those used by
3987 any TU, so there's no need to scan TUs here.
3988 We can ignore file names coming from already-expanded CUs. */
f4dc4d17 3989
848e3e78 3990 for (i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
24c79950
TT
3991 {
3992 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
3993
3994 if (per_cu->v.quick->symtab)
3995 {
3996 void **slot = htab_find_slot (visited, per_cu->v.quick->file_names,
3997 INSERT);
3998
3999 *slot = per_cu->v.quick->file_names;
4000 }
4001 }
4002
848e3e78 4003 for (i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
9291a0cd
TT
4004 {
4005 int j;
f4dc4d17 4006 struct dwarf2_per_cu_data *per_cu = dw2_get_primary_cu (i);
7b9f3c50 4007 struct quick_file_names *file_data;
24c79950 4008 void **slot;
9291a0cd 4009
3d7bb9d9 4010 /* We only need to look at symtabs not already expanded. */
e254ef6a 4011 if (per_cu->v.quick->symtab)
9291a0cd
TT
4012 continue;
4013
e4a48d9d 4014 file_data = dw2_get_file_names (per_cu);
7b9f3c50 4015 if (file_data == NULL)
9291a0cd
TT
4016 continue;
4017
24c79950
TT
4018 slot = htab_find_slot (visited, file_data, INSERT);
4019 if (*slot)
4020 {
4021 /* Already visited. */
4022 continue;
4023 }
4024 *slot = file_data;
4025
7b9f3c50 4026 for (j = 0; j < file_data->num_file_names; ++j)
9291a0cd 4027 {
74e2f255
DE
4028 const char *this_real_name;
4029
4030 if (need_fullname)
4031 this_real_name = dw2_get_real_path (objfile, file_data, j);
4032 else
4033 this_real_name = NULL;
7b9f3c50 4034 (*fun) (file_data->file_names[j], this_real_name, data);
9291a0cd
TT
4035 }
4036 }
24c79950
TT
4037
4038 do_cleanups (cleanup);
9291a0cd
TT
4039}
4040
4041static int
4042dw2_has_symbols (struct objfile *objfile)
4043{
4044 return 1;
4045}
4046
4047const struct quick_symbol_functions dwarf2_gdb_index_functions =
4048{
4049 dw2_has_symbols,
4050 dw2_find_last_source_symtab,
4051 dw2_forget_cached_source_info,
f8eba3c6 4052 dw2_map_symtabs_matching_filename,
9291a0cd 4053 dw2_lookup_symbol,
9291a0cd
TT
4054 dw2_print_stats,
4055 dw2_dump,
4056 dw2_relocate,
4057 dw2_expand_symtabs_for_function,
4058 dw2_expand_all_symtabs,
652a8996 4059 dw2_expand_symtabs_with_fullname,
40658b94 4060 dw2_map_matching_symbols,
9291a0cd
TT
4061 dw2_expand_symtabs_matching,
4062 dw2_find_pc_sect_symtab,
9291a0cd
TT
4063 dw2_map_symbol_filenames
4064};
4065
4066/* Initialize for reading DWARF for this objfile. Return 0 if this
4067 file will use psymtabs, or 1 if using the GNU index. */
4068
4069int
4070dwarf2_initialize_objfile (struct objfile *objfile)
4071{
4072 /* If we're about to read full symbols, don't bother with the
4073 indices. In this case we also don't care if some other debug
4074 format is making psymtabs, because they are all about to be
4075 expanded anyway. */
4076 if ((objfile->flags & OBJF_READNOW))
4077 {
4078 int i;
4079
4080 dwarf2_per_objfile->using_index = 1;
4081 create_all_comp_units (objfile);
0e50663e 4082 create_all_type_units (objfile);
7b9f3c50
DE
4083 dwarf2_per_objfile->quick_file_names_table =
4084 create_quick_file_names_table (dwarf2_per_objfile->n_comp_units);
9291a0cd 4085
1fd400ff 4086 for (i = 0; i < (dwarf2_per_objfile->n_comp_units
d467dd73 4087 + dwarf2_per_objfile->n_type_units); ++i)
9291a0cd 4088 {
e254ef6a 4089 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
9291a0cd 4090
e254ef6a
DE
4091 per_cu->v.quick = OBSTACK_ZALLOC (&objfile->objfile_obstack,
4092 struct dwarf2_per_cu_quick_data);
9291a0cd
TT
4093 }
4094
4095 /* Return 1 so that gdb sees the "quick" functions. However,
4096 these functions will be no-ops because we will have expanded
4097 all symtabs. */
4098 return 1;
4099 }
4100
4101 if (dwarf2_read_index (objfile))
4102 return 1;
4103
9291a0cd
TT
4104 return 0;
4105}
4106
4107\f
4108
dce234bc
PP
4109/* Build a partial symbol table. */
4110
4111void
f29dff0a 4112dwarf2_build_psymtabs (struct objfile *objfile)
dce234bc 4113{
c9bf0622
TT
4114 volatile struct gdb_exception except;
4115
f29dff0a 4116 if (objfile->global_psymbols.size == 0 && objfile->static_psymbols.size == 0)
c906108c
SS
4117 {
4118 init_psymbol_list (objfile, 1024);
4119 }
4120
c9bf0622
TT
4121 TRY_CATCH (except, RETURN_MASK_ERROR)
4122 {
4123 /* This isn't really ideal: all the data we allocate on the
4124 objfile's obstack is still uselessly kept around. However,
4125 freeing it seems unsafe. */
4126 struct cleanup *cleanups = make_cleanup_discard_psymtabs (objfile);
4127
4128 dwarf2_build_psymtabs_hard (objfile);
4129 discard_cleanups (cleanups);
4130 }
4131 if (except.reason < 0)
4132 exception_print (gdb_stderr, except);
c906108c 4133}
c906108c 4134
1ce1cefd
DE
4135/* Return the total length of the CU described by HEADER. */
4136
4137static unsigned int
4138get_cu_length (const struct comp_unit_head *header)
4139{
4140 return header->initial_length_size + header->length;
4141}
4142
45452591
DE
4143/* Return TRUE if OFFSET is within CU_HEADER. */
4144
4145static inline int
b64f50a1 4146offset_in_cu_p (const struct comp_unit_head *cu_header, sect_offset offset)
45452591 4147{
b64f50a1 4148 sect_offset bottom = { cu_header->offset.sect_off };
1ce1cefd 4149 sect_offset top = { cu_header->offset.sect_off + get_cu_length (cu_header) };
9a619af0 4150
b64f50a1 4151 return (offset.sect_off >= bottom.sect_off && offset.sect_off < top.sect_off);
45452591
DE
4152}
4153
3b80fe9b
DE
4154/* Find the base address of the compilation unit for range lists and
4155 location lists. It will normally be specified by DW_AT_low_pc.
4156 In DWARF-3 draft 4, the base address could be overridden by
4157 DW_AT_entry_pc. It's been removed, but GCC still uses this for
4158 compilation units with discontinuous ranges. */
4159
4160static void
4161dwarf2_find_base_address (struct die_info *die, struct dwarf2_cu *cu)
4162{
4163 struct attribute *attr;
4164
4165 cu->base_known = 0;
4166 cu->base_address = 0;
4167
4168 attr = dwarf2_attr (die, DW_AT_entry_pc, cu);
4169 if (attr)
4170 {
4171 cu->base_address = DW_ADDR (attr);
4172 cu->base_known = 1;
4173 }
4174 else
4175 {
4176 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
4177 if (attr)
4178 {
4179 cu->base_address = DW_ADDR (attr);
4180 cu->base_known = 1;
4181 }
4182 }
4183}
4184
93311388
DE
4185/* Read in the comp unit header information from the debug_info at info_ptr.
4186 NOTE: This leaves members offset, first_die_offset to be filled in
4187 by the caller. */
107d2387 4188
d521ce57 4189static const gdb_byte *
107d2387 4190read_comp_unit_head (struct comp_unit_head *cu_header,
d521ce57 4191 const gdb_byte *info_ptr, bfd *abfd)
107d2387
AC
4192{
4193 int signed_addr;
891d2f0b 4194 unsigned int bytes_read;
c764a876
DE
4195
4196 cu_header->length = read_initial_length (abfd, info_ptr, &bytes_read);
4197 cu_header->initial_length_size = bytes_read;
4198 cu_header->offset_size = (bytes_read == 4) ? 4 : 8;
613e1657 4199 info_ptr += bytes_read;
107d2387
AC
4200 cu_header->version = read_2_bytes (abfd, info_ptr);
4201 info_ptr += 2;
b64f50a1
JK
4202 cu_header->abbrev_offset.sect_off = read_offset (abfd, info_ptr, cu_header,
4203 &bytes_read);
613e1657 4204 info_ptr += bytes_read;
107d2387
AC
4205 cu_header->addr_size = read_1_byte (abfd, info_ptr);
4206 info_ptr += 1;
4207 signed_addr = bfd_get_sign_extend_vma (abfd);
4208 if (signed_addr < 0)
8e65ff28 4209 internal_error (__FILE__, __LINE__,
e2e0b3e5 4210 _("read_comp_unit_head: dwarf from non elf file"));
107d2387 4211 cu_header->signed_addr_p = signed_addr;
c764a876 4212
107d2387
AC
4213 return info_ptr;
4214}
4215
36586728
TT
4216/* Helper function that returns the proper abbrev section for
4217 THIS_CU. */
4218
4219static struct dwarf2_section_info *
4220get_abbrev_section_for_cu (struct dwarf2_per_cu_data *this_cu)
4221{
4222 struct dwarf2_section_info *abbrev;
4223
4224 if (this_cu->is_dwz)
4225 abbrev = &dwarf2_get_dwz_file ()->abbrev;
4226 else
4227 abbrev = &dwarf2_per_objfile->abbrev;
4228
4229 return abbrev;
4230}
4231
9ff913ba
DE
4232/* Subroutine of read_and_check_comp_unit_head and
4233 read_and_check_type_unit_head to simplify them.
4234 Perform various error checking on the header. */
4235
4236static void
4237error_check_comp_unit_head (struct comp_unit_head *header,
4bdcc0c1
DE
4238 struct dwarf2_section_info *section,
4239 struct dwarf2_section_info *abbrev_section)
9ff913ba 4240{
a32a8923
DE
4241 bfd *abfd = get_section_bfd_owner (section);
4242 const char *filename = get_section_file_name (section);
9ff913ba
DE
4243
4244 if (header->version != 2 && header->version != 3 && header->version != 4)
4245 error (_("Dwarf Error: wrong version in compilation unit header "
4246 "(is %d, should be 2, 3, or 4) [in module %s]"), header->version,
4247 filename);
4248
b64f50a1 4249 if (header->abbrev_offset.sect_off
36586728 4250 >= dwarf2_section_size (dwarf2_per_objfile->objfile, abbrev_section))
9ff913ba
DE
4251 error (_("Dwarf Error: bad offset (0x%lx) in compilation unit header "
4252 "(offset 0x%lx + 6) [in module %s]"),
b64f50a1 4253 (long) header->abbrev_offset.sect_off, (long) header->offset.sect_off,
9ff913ba
DE
4254 filename);
4255
4256 /* Cast to unsigned long to use 64-bit arithmetic when possible to
4257 avoid potential 32-bit overflow. */
1ce1cefd 4258 if (((unsigned long) header->offset.sect_off + get_cu_length (header))
9ff913ba
DE
4259 > section->size)
4260 error (_("Dwarf Error: bad length (0x%lx) in compilation unit header "
4261 "(offset 0x%lx + 0) [in module %s]"),
b64f50a1 4262 (long) header->length, (long) header->offset.sect_off,
9ff913ba
DE
4263 filename);
4264}
4265
4266/* Read in a CU/TU header and perform some basic error checking.
4267 The contents of the header are stored in HEADER.
4268 The result is a pointer to the start of the first DIE. */
adabb602 4269
d521ce57 4270static const gdb_byte *
9ff913ba
DE
4271read_and_check_comp_unit_head (struct comp_unit_head *header,
4272 struct dwarf2_section_info *section,
4bdcc0c1 4273 struct dwarf2_section_info *abbrev_section,
d521ce57 4274 const gdb_byte *info_ptr,
9ff913ba 4275 int is_debug_types_section)
72bf9492 4276{
d521ce57 4277 const gdb_byte *beg_of_comp_unit = info_ptr;
a32a8923 4278 bfd *abfd = get_section_bfd_owner (section);
72bf9492 4279
b64f50a1 4280 header->offset.sect_off = beg_of_comp_unit - section->buffer;
adabb602 4281
72bf9492
DJ
4282 info_ptr = read_comp_unit_head (header, info_ptr, abfd);
4283
460c1c54
CC
4284 /* If we're reading a type unit, skip over the signature and
4285 type_offset fields. */
b0df02fd 4286 if (is_debug_types_section)
460c1c54
CC
4287 info_ptr += 8 /*signature*/ + header->offset_size;
4288
b64f50a1 4289 header->first_die_offset.cu_off = info_ptr - beg_of_comp_unit;
adabb602 4290
4bdcc0c1 4291 error_check_comp_unit_head (header, section, abbrev_section);
72bf9492
DJ
4292
4293 return info_ptr;
4294}
4295
348e048f
DE
4296/* Read in the types comp unit header information from .debug_types entry at
4297 types_ptr. The result is a pointer to one past the end of the header. */
4298
d521ce57 4299static const gdb_byte *
9ff913ba
DE
4300read_and_check_type_unit_head (struct comp_unit_head *header,
4301 struct dwarf2_section_info *section,
4bdcc0c1 4302 struct dwarf2_section_info *abbrev_section,
d521ce57 4303 const gdb_byte *info_ptr,
dee91e82
DE
4304 ULONGEST *signature,
4305 cu_offset *type_offset_in_tu)
348e048f 4306{
d521ce57 4307 const gdb_byte *beg_of_comp_unit = info_ptr;
a32a8923 4308 bfd *abfd = get_section_bfd_owner (section);
348e048f 4309
b64f50a1 4310 header->offset.sect_off = beg_of_comp_unit - section->buffer;
348e048f 4311
9ff913ba 4312 info_ptr = read_comp_unit_head (header, info_ptr, abfd);
348e048f 4313
9ff913ba
DE
4314 /* If we're reading a type unit, skip over the signature and
4315 type_offset fields. */
4316 if (signature != NULL)
4317 *signature = read_8_bytes (abfd, info_ptr);
4318 info_ptr += 8;
dee91e82
DE
4319 if (type_offset_in_tu != NULL)
4320 type_offset_in_tu->cu_off = read_offset_1 (abfd, info_ptr,
4321 header->offset_size);
9ff913ba
DE
4322 info_ptr += header->offset_size;
4323
b64f50a1 4324 header->first_die_offset.cu_off = info_ptr - beg_of_comp_unit;
348e048f 4325
4bdcc0c1 4326 error_check_comp_unit_head (header, section, abbrev_section);
9ff913ba
DE
4327
4328 return info_ptr;
348e048f
DE
4329}
4330
f4dc4d17
DE
4331/* Fetch the abbreviation table offset from a comp or type unit header. */
4332
4333static sect_offset
4334read_abbrev_offset (struct dwarf2_section_info *section,
4335 sect_offset offset)
4336{
a32a8923 4337 bfd *abfd = get_section_bfd_owner (section);
d521ce57 4338 const gdb_byte *info_ptr;
f4dc4d17
DE
4339 unsigned int length, initial_length_size, offset_size;
4340 sect_offset abbrev_offset;
4341
4342 dwarf2_read_section (dwarf2_per_objfile->objfile, section);
4343 info_ptr = section->buffer + offset.sect_off;
4344 length = read_initial_length (abfd, info_ptr, &initial_length_size);
4345 offset_size = initial_length_size == 4 ? 4 : 8;
4346 info_ptr += initial_length_size + 2 /*version*/;
4347 abbrev_offset.sect_off = read_offset_1 (abfd, info_ptr, offset_size);
4348 return abbrev_offset;
4349}
4350
aaa75496
JB
4351/* Allocate a new partial symtab for file named NAME and mark this new
4352 partial symtab as being an include of PST. */
4353
4354static void
d521ce57 4355dwarf2_create_include_psymtab (const char *name, struct partial_symtab *pst,
aaa75496
JB
4356 struct objfile *objfile)
4357{
4358 struct partial_symtab *subpst = allocate_psymtab (name, objfile);
4359
fbd9ab74
JK
4360 if (!IS_ABSOLUTE_PATH (subpst->filename))
4361 {
4362 /* It shares objfile->objfile_obstack. */
4363 subpst->dirname = pst->dirname;
4364 }
4365
aaa75496
JB
4366 subpst->section_offsets = pst->section_offsets;
4367 subpst->textlow = 0;
4368 subpst->texthigh = 0;
4369
4370 subpst->dependencies = (struct partial_symtab **)
4371 obstack_alloc (&objfile->objfile_obstack,
4372 sizeof (struct partial_symtab *));
4373 subpst->dependencies[0] = pst;
4374 subpst->number_of_dependencies = 1;
4375
4376 subpst->globals_offset = 0;
4377 subpst->n_global_syms = 0;
4378 subpst->statics_offset = 0;
4379 subpst->n_static_syms = 0;
4380 subpst->symtab = NULL;
4381 subpst->read_symtab = pst->read_symtab;
4382 subpst->readin = 0;
4383
4384 /* No private part is necessary for include psymtabs. This property
4385 can be used to differentiate between such include psymtabs and
10b3939b 4386 the regular ones. */
58a9656e 4387 subpst->read_symtab_private = NULL;
aaa75496
JB
4388}
4389
4390/* Read the Line Number Program data and extract the list of files
4391 included by the source file represented by PST. Build an include
d85a05f0 4392 partial symtab for each of these included files. */
aaa75496
JB
4393
4394static void
4395dwarf2_build_include_psymtabs (struct dwarf2_cu *cu,
dee91e82
DE
4396 struct die_info *die,
4397 struct partial_symtab *pst)
aaa75496 4398{
d85a05f0
DJ
4399 struct line_header *lh = NULL;
4400 struct attribute *attr;
aaa75496 4401
d85a05f0
DJ
4402 attr = dwarf2_attr (die, DW_AT_stmt_list, cu);
4403 if (attr)
3019eac3 4404 lh = dwarf_decode_line_header (DW_UNSND (attr), cu);
aaa75496
JB
4405 if (lh == NULL)
4406 return; /* No linetable, so no includes. */
4407
c6da4cef 4408 /* NOTE: pst->dirname is DW_AT_comp_dir (if present). */
f3f5162e 4409 dwarf_decode_lines (lh, pst->dirname, cu, pst, 1);
aaa75496
JB
4410
4411 free_line_header (lh);
4412}
4413
348e048f 4414static hashval_t
52dc124a 4415hash_signatured_type (const void *item)
348e048f 4416{
52dc124a 4417 const struct signatured_type *sig_type = item;
9a619af0 4418
348e048f 4419 /* This drops the top 32 bits of the signature, but is ok for a hash. */
52dc124a 4420 return sig_type->signature;
348e048f
DE
4421}
4422
4423static int
52dc124a 4424eq_signatured_type (const void *item_lhs, const void *item_rhs)
348e048f
DE
4425{
4426 const struct signatured_type *lhs = item_lhs;
4427 const struct signatured_type *rhs = item_rhs;
9a619af0 4428
348e048f
DE
4429 return lhs->signature == rhs->signature;
4430}
4431
1fd400ff
TT
4432/* Allocate a hash table for signatured types. */
4433
4434static htab_t
673bfd45 4435allocate_signatured_type_table (struct objfile *objfile)
1fd400ff
TT
4436{
4437 return htab_create_alloc_ex (41,
52dc124a
DE
4438 hash_signatured_type,
4439 eq_signatured_type,
1fd400ff
TT
4440 NULL,
4441 &objfile->objfile_obstack,
4442 hashtab_obstack_allocate,
4443 dummy_obstack_deallocate);
4444}
4445
d467dd73 4446/* A helper function to add a signatured type CU to a table. */
1fd400ff
TT
4447
4448static int
d467dd73 4449add_signatured_type_cu_to_table (void **slot, void *datum)
1fd400ff
TT
4450{
4451 struct signatured_type *sigt = *slot;
b4dd5633 4452 struct signatured_type ***datap = datum;
1fd400ff 4453
b4dd5633 4454 **datap = sigt;
1fd400ff
TT
4455 ++*datap;
4456
4457 return 1;
4458}
4459
c88ee1f0
DE
4460/* Create the hash table of all entries in the .debug_types
4461 (or .debug_types.dwo) section(s).
4462 If reading a DWO file, then DWO_FILE is a pointer to the DWO file object,
4463 otherwise it is NULL.
4464
4465 The result is a pointer to the hash table or NULL if there are no types.
4466
4467 Note: This function processes DWO files only, not DWP files. */
348e048f 4468
3019eac3
DE
4469static htab_t
4470create_debug_types_hash_table (struct dwo_file *dwo_file,
4471 VEC (dwarf2_section_info_def) *types)
348e048f 4472{
3019eac3 4473 struct objfile *objfile = dwarf2_per_objfile->objfile;
8b70b953 4474 htab_t types_htab = NULL;
8b70b953
TT
4475 int ix;
4476 struct dwarf2_section_info *section;
4bdcc0c1 4477 struct dwarf2_section_info *abbrev_section;
348e048f 4478
3019eac3
DE
4479 if (VEC_empty (dwarf2_section_info_def, types))
4480 return NULL;
348e048f 4481
4bdcc0c1
DE
4482 abbrev_section = (dwo_file != NULL
4483 ? &dwo_file->sections.abbrev
4484 : &dwarf2_per_objfile->abbrev);
4485
09406207
DE
4486 if (dwarf2_read_debug)
4487 fprintf_unfiltered (gdb_stdlog, "Reading .debug_types%s for %s:\n",
4488 dwo_file ? ".dwo" : "",
a32a8923 4489 get_section_file_name (abbrev_section));
09406207 4490
8b70b953 4491 for (ix = 0;
3019eac3 4492 VEC_iterate (dwarf2_section_info_def, types, ix, section);
8b70b953
TT
4493 ++ix)
4494 {
3019eac3 4495 bfd *abfd;
d521ce57 4496 const gdb_byte *info_ptr, *end_ptr;
348e048f 4497
8b70b953
TT
4498 dwarf2_read_section (objfile, section);
4499 info_ptr = section->buffer;
348e048f 4500
8b70b953
TT
4501 if (info_ptr == NULL)
4502 continue;
348e048f 4503
3019eac3 4504 /* We can't set abfd until now because the section may be empty or
a32a8923
DE
4505 not present, in which case the bfd is unknown. */
4506 abfd = get_section_bfd_owner (section);
3019eac3 4507
dee91e82
DE
4508 /* We don't use init_cutu_and_read_dies_simple, or some such, here
4509 because we don't need to read any dies: the signature is in the
4510 header. */
8b70b953
TT
4511
4512 end_ptr = info_ptr + section->size;
4513 while (info_ptr < end_ptr)
4514 {
b64f50a1 4515 sect_offset offset;
3019eac3 4516 cu_offset type_offset_in_tu;
8b70b953 4517 ULONGEST signature;
52dc124a 4518 struct signatured_type *sig_type;
3019eac3 4519 struct dwo_unit *dwo_tu;
8b70b953 4520 void **slot;
d521ce57 4521 const gdb_byte *ptr = info_ptr;
9ff913ba 4522 struct comp_unit_head header;
dee91e82 4523 unsigned int length;
348e048f 4524
b64f50a1 4525 offset.sect_off = ptr - section->buffer;
348e048f 4526
8b70b953 4527 /* We need to read the type's signature in order to build the hash
9ff913ba 4528 table, but we don't need anything else just yet. */
348e048f 4529
4bdcc0c1
DE
4530 ptr = read_and_check_type_unit_head (&header, section,
4531 abbrev_section, ptr,
3019eac3 4532 &signature, &type_offset_in_tu);
6caca83c 4533
1ce1cefd 4534 length = get_cu_length (&header);
dee91e82 4535
6caca83c 4536 /* Skip dummy type units. */
dee91e82
DE
4537 if (ptr >= info_ptr + length
4538 || peek_abbrev_code (abfd, ptr) == 0)
6caca83c 4539 {
1ce1cefd 4540 info_ptr += length;
6caca83c
CC
4541 continue;
4542 }
8b70b953 4543
0349ea22
DE
4544 if (types_htab == NULL)
4545 {
4546 if (dwo_file)
4547 types_htab = allocate_dwo_unit_table (objfile);
4548 else
4549 types_htab = allocate_signatured_type_table (objfile);
4550 }
4551
3019eac3
DE
4552 if (dwo_file)
4553 {
4554 sig_type = NULL;
4555 dwo_tu = OBSTACK_ZALLOC (&objfile->objfile_obstack,
4556 struct dwo_unit);
4557 dwo_tu->dwo_file = dwo_file;
4558 dwo_tu->signature = signature;
4559 dwo_tu->type_offset_in_tu = type_offset_in_tu;
8a0459fd 4560 dwo_tu->section = section;
3019eac3
DE
4561 dwo_tu->offset = offset;
4562 dwo_tu->length = length;
4563 }
4564 else
4565 {
4566 /* N.B.: type_offset is not usable if this type uses a DWO file.
4567 The real type_offset is in the DWO file. */
4568 dwo_tu = NULL;
4569 sig_type = OBSTACK_ZALLOC (&objfile->objfile_obstack,
4570 struct signatured_type);
4571 sig_type->signature = signature;
4572 sig_type->type_offset_in_tu = type_offset_in_tu;
4573 sig_type->per_cu.objfile = objfile;
4574 sig_type->per_cu.is_debug_types = 1;
8a0459fd 4575 sig_type->per_cu.section = section;
3019eac3
DE
4576 sig_type->per_cu.offset = offset;
4577 sig_type->per_cu.length = length;
4578 }
8b70b953 4579
3019eac3
DE
4580 slot = htab_find_slot (types_htab,
4581 dwo_file ? (void*) dwo_tu : (void *) sig_type,
4582 INSERT);
8b70b953
TT
4583 gdb_assert (slot != NULL);
4584 if (*slot != NULL)
4585 {
3019eac3
DE
4586 sect_offset dup_offset;
4587
4588 if (dwo_file)
4589 {
4590 const struct dwo_unit *dup_tu = *slot;
4591
4592 dup_offset = dup_tu->offset;
4593 }
4594 else
4595 {
4596 const struct signatured_type *dup_tu = *slot;
4597
4598 dup_offset = dup_tu->per_cu.offset;
4599 }
b3c8eb43 4600
8b70b953 4601 complaint (&symfile_complaints,
c88ee1f0 4602 _("debug type entry at offset 0x%x is duplicate to"
4031ecc5 4603 " the entry at offset 0x%x, signature %s"),
3019eac3 4604 offset.sect_off, dup_offset.sect_off,
4031ecc5 4605 hex_string (signature));
8b70b953 4606 }
3019eac3 4607 *slot = dwo_file ? (void *) dwo_tu : (void *) sig_type;
348e048f 4608
09406207 4609 if (dwarf2_read_debug)
4031ecc5 4610 fprintf_unfiltered (gdb_stdlog, " offset 0x%x, signature %s\n",
b64f50a1 4611 offset.sect_off,
4031ecc5 4612 hex_string (signature));
348e048f 4613
dee91e82 4614 info_ptr += length;
8b70b953 4615 }
348e048f
DE
4616 }
4617
3019eac3
DE
4618 return types_htab;
4619}
4620
4621/* Create the hash table of all entries in the .debug_types section,
4622 and initialize all_type_units.
4623 The result is zero if there is an error (e.g. missing .debug_types section),
4624 otherwise non-zero. */
4625
4626static int
4627create_all_type_units (struct objfile *objfile)
4628{
4629 htab_t types_htab;
b4dd5633 4630 struct signatured_type **iter;
3019eac3
DE
4631
4632 types_htab = create_debug_types_hash_table (NULL, dwarf2_per_objfile->types);
4633 if (types_htab == NULL)
4634 {
4635 dwarf2_per_objfile->signatured_types = NULL;
4636 return 0;
4637 }
4638
348e048f
DE
4639 dwarf2_per_objfile->signatured_types = types_htab;
4640
d467dd73
DE
4641 dwarf2_per_objfile->n_type_units = htab_elements (types_htab);
4642 dwarf2_per_objfile->all_type_units
a2ce51a0
DE
4643 = xmalloc (dwarf2_per_objfile->n_type_units
4644 * sizeof (struct signatured_type *));
d467dd73
DE
4645 iter = &dwarf2_per_objfile->all_type_units[0];
4646 htab_traverse_noresize (types_htab, add_signatured_type_cu_to_table, &iter);
4647 gdb_assert (iter - &dwarf2_per_objfile->all_type_units[0]
4648 == dwarf2_per_objfile->n_type_units);
1fd400ff 4649
348e048f
DE
4650 return 1;
4651}
4652
a2ce51a0
DE
4653/* Subroutine of lookup_dwo_signatured_type and lookup_dwp_signatured_type.
4654 Fill in SIG_ENTRY with DWO_ENTRY. */
4655
4656static void
4657fill_in_sig_entry_from_dwo_entry (struct objfile *objfile,
4658 struct signatured_type *sig_entry,
4659 struct dwo_unit *dwo_entry)
4660{
7ee85ab1 4661 /* Make sure we're not clobbering something we don't expect to. */
a2ce51a0
DE
4662 gdb_assert (! sig_entry->per_cu.queued);
4663 gdb_assert (sig_entry->per_cu.cu == NULL);
4664 gdb_assert (sig_entry->per_cu.v.quick != NULL);
4665 gdb_assert (sig_entry->per_cu.v.quick->symtab == NULL);
4666 gdb_assert (sig_entry->signature == dwo_entry->signature);
4667 gdb_assert (sig_entry->type_offset_in_section.sect_off == 0);
4668 gdb_assert (sig_entry->type_unit_group == NULL);
7ee85ab1
DE
4669 gdb_assert (sig_entry->dwo_unit == NULL);
4670
4671 sig_entry->per_cu.section = dwo_entry->section;
4672 sig_entry->per_cu.offset = dwo_entry->offset;
4673 sig_entry->per_cu.length = dwo_entry->length;
4674 sig_entry->per_cu.reading_dwo_directly = 1;
4675 sig_entry->per_cu.objfile = objfile;
a2ce51a0
DE
4676 sig_entry->type_offset_in_tu = dwo_entry->type_offset_in_tu;
4677 sig_entry->dwo_unit = dwo_entry;
4678}
4679
4680/* Subroutine of lookup_signatured_type.
7ee85ab1
DE
4681 If we haven't read the TU yet, create the signatured_type data structure
4682 for a TU to be read in directly from a DWO file, bypassing the stub.
4683 This is the "Stay in DWO Optimization": When there is no DWP file and we're
4684 using .gdb_index, then when reading a CU we want to stay in the DWO file
4685 containing that CU. Otherwise we could end up reading several other DWO
4686 files (due to comdat folding) to process the transitive closure of all the
4687 mentioned TUs, and that can be slow. The current DWO file will have every
4688 type signature that it needs.
a2ce51a0
DE
4689 We only do this for .gdb_index because in the psymtab case we already have
4690 to read all the DWOs to build the type unit groups. */
4691
4692static struct signatured_type *
4693lookup_dwo_signatured_type (struct dwarf2_cu *cu, ULONGEST sig)
4694{
4695 struct objfile *objfile = dwarf2_per_objfile->objfile;
4696 struct dwo_file *dwo_file;
4697 struct dwo_unit find_dwo_entry, *dwo_entry;
4698 struct signatured_type find_sig_entry, *sig_entry;
4699
4700 gdb_assert (cu->dwo_unit && dwarf2_per_objfile->using_index);
4701
4702 /* Note: cu->dwo_unit is the dwo_unit that references this TU, not the
4703 dwo_unit of the TU itself. */
4704 dwo_file = cu->dwo_unit->dwo_file;
4705
4706 /* We only ever need to read in one copy of a signatured type.
4707 Just use the global signatured_types array. If this is the first time
4708 we're reading this type, replace the recorded data from .gdb_index with
4709 this TU. */
4710
4711 if (dwarf2_per_objfile->signatured_types == NULL)
4712 return NULL;
4713 find_sig_entry.signature = sig;
4714 sig_entry = htab_find (dwarf2_per_objfile->signatured_types, &find_sig_entry);
4715 if (sig_entry == NULL)
4716 return NULL;
7ee85ab1
DE
4717
4718 /* We can get here with the TU already read, *or* in the process of being
4719 read. Don't reassign it if that's the case. Also note that if the TU is
4720 already being read, it may not have come from a DWO, the program may be
4721 a mix of Fission-compiled code and non-Fission-compiled code. */
a2ce51a0 4722 /* Have we already tried to read this TU? */
7ee85ab1 4723 if (sig_entry->per_cu.tu_read)
a2ce51a0
DE
4724 return sig_entry;
4725
4726 /* Ok, this is the first time we're reading this TU. */
4727 if (dwo_file->tus == NULL)
4728 return NULL;
4729 find_dwo_entry.signature = sig;
4730 dwo_entry = htab_find (dwo_file->tus, &find_dwo_entry);
4731 if (dwo_entry == NULL)
4732 return NULL;
4733
4734 fill_in_sig_entry_from_dwo_entry (objfile, sig_entry, dwo_entry);
89e63ee4 4735 sig_entry->per_cu.tu_read = 1;
a2ce51a0
DE
4736 return sig_entry;
4737}
4738
4739/* Subroutine of lookup_dwp_signatured_type.
4740 Add an entry for signature SIG to dwarf2_per_objfile->signatured_types. */
4741
4742static struct signatured_type *
4743add_type_unit (ULONGEST sig)
4744{
4745 struct objfile *objfile = dwarf2_per_objfile->objfile;
4746 int n_type_units = dwarf2_per_objfile->n_type_units;
4747 struct signatured_type *sig_type;
4748 void **slot;
4749
4750 ++n_type_units;
4751 dwarf2_per_objfile->all_type_units =
4752 xrealloc (dwarf2_per_objfile->all_type_units,
4753 n_type_units * sizeof (struct signatured_type *));
4754 dwarf2_per_objfile->n_type_units = n_type_units;
4755 sig_type = OBSTACK_ZALLOC (&objfile->objfile_obstack,
4756 struct signatured_type);
4757 dwarf2_per_objfile->all_type_units[n_type_units - 1] = sig_type;
4758 sig_type->signature = sig;
4759 sig_type->per_cu.is_debug_types = 1;
4760 sig_type->per_cu.v.quick =
4761 OBSTACK_ZALLOC (&objfile->objfile_obstack,
4762 struct dwarf2_per_cu_quick_data);
4763 slot = htab_find_slot (dwarf2_per_objfile->signatured_types,
4764 sig_type, INSERT);
4765 gdb_assert (*slot == NULL);
4766 *slot = sig_type;
4767 /* The rest of sig_type must be filled in by the caller. */
4768 return sig_type;
4769}
4770
4771/* Subroutine of lookup_signatured_type.
4772 Look up the type for signature SIG, and if we can't find SIG in .gdb_index
4773 then try the DWP file.
4774 Normally this "can't happen", but if there's a bug in signature
4775 generation and/or the DWP file is built incorrectly, it can happen.
4776 Using the type directly from the DWP file means we don't have the stub
4777 which has some useful attributes (e.g., DW_AT_comp_dir), but they're
4778 not critical. [Eventually the stub may go away for type units anyway.] */
4779
4780static struct signatured_type *
4781lookup_dwp_signatured_type (struct dwarf2_cu *cu, ULONGEST sig)
4782{
4783 struct objfile *objfile = dwarf2_per_objfile->objfile;
4784 struct dwp_file *dwp_file = get_dwp_file ();
4785 struct dwo_unit *dwo_entry;
4786 struct signatured_type find_sig_entry, *sig_entry;
4787
4788 gdb_assert (cu->dwo_unit && dwarf2_per_objfile->using_index);
4789 gdb_assert (dwp_file != NULL);
4790
4791 if (dwarf2_per_objfile->signatured_types != NULL)
4792 {
4793 find_sig_entry.signature = sig;
4794 sig_entry = htab_find (dwarf2_per_objfile->signatured_types,
4795 &find_sig_entry);
4796 if (sig_entry != NULL)
4797 return sig_entry;
4798 }
4799
4800 /* This is the "shouldn't happen" case.
4801 Try the DWP file and hope for the best. */
4802 if (dwp_file->tus == NULL)
4803 return NULL;
57d63ce2
DE
4804 dwo_entry = lookup_dwo_unit_in_dwp (dwp_file, NULL,
4805 sig, 1 /* is_debug_types */);
a2ce51a0
DE
4806 if (dwo_entry == NULL)
4807 return NULL;
4808
4809 sig_entry = add_type_unit (sig);
4810 fill_in_sig_entry_from_dwo_entry (objfile, sig_entry, dwo_entry);
4811
4812 /* The caller will signal a complaint if we return NULL.
4813 Here we don't return NULL but we still want to complain. */
4814 complaint (&symfile_complaints,
4815 _("Bad type signature %s referenced by %s at 0x%x,"
4816 " coping by using copy in DWP [in module %s]"),
4817 hex_string (sig),
4818 cu->per_cu->is_debug_types ? "TU" : "CU",
4819 cu->per_cu->offset.sect_off,
4262abfb 4820 objfile_name (objfile));
a2ce51a0
DE
4821
4822 return sig_entry;
4823}
4824
380bca97 4825/* Lookup a signature based type for DW_FORM_ref_sig8.
5a8b3f62
DE
4826 Returns NULL if signature SIG is not present in the table.
4827 It is up to the caller to complain about this. */
348e048f
DE
4828
4829static struct signatured_type *
a2ce51a0 4830lookup_signatured_type (struct dwarf2_cu *cu, ULONGEST sig)
348e048f 4831{
a2ce51a0
DE
4832 if (cu->dwo_unit
4833 && dwarf2_per_objfile->using_index)
4834 {
4835 /* We're in a DWO/DWP file, and we're using .gdb_index.
4836 These cases require special processing. */
4837 if (get_dwp_file () == NULL)
4838 return lookup_dwo_signatured_type (cu, sig);
4839 else
4840 return lookup_dwp_signatured_type (cu, sig);
4841 }
4842 else
4843 {
4844 struct signatured_type find_entry, *entry;
348e048f 4845
a2ce51a0
DE
4846 if (dwarf2_per_objfile->signatured_types == NULL)
4847 return NULL;
4848 find_entry.signature = sig;
4849 entry = htab_find (dwarf2_per_objfile->signatured_types, &find_entry);
4850 return entry;
4851 }
348e048f 4852}
42e7ad6c
DE
4853\f
4854/* Low level DIE reading support. */
348e048f 4855
d85a05f0
DJ
4856/* Initialize a die_reader_specs struct from a dwarf2_cu struct. */
4857
4858static void
4859init_cu_die_reader (struct die_reader_specs *reader,
dee91e82 4860 struct dwarf2_cu *cu,
3019eac3
DE
4861 struct dwarf2_section_info *section,
4862 struct dwo_file *dwo_file)
d85a05f0 4863{
fceca515 4864 gdb_assert (section->readin && section->buffer != NULL);
a32a8923 4865 reader->abfd = get_section_bfd_owner (section);
d85a05f0 4866 reader->cu = cu;
3019eac3 4867 reader->dwo_file = dwo_file;
dee91e82
DE
4868 reader->die_section = section;
4869 reader->buffer = section->buffer;
f664829e 4870 reader->buffer_end = section->buffer + section->size;
a2ce51a0 4871 reader->comp_dir = NULL;
d85a05f0
DJ
4872}
4873
b0c7bfa9
DE
4874/* Subroutine of init_cutu_and_read_dies to simplify it.
4875 Read in the rest of a CU/TU top level DIE from DWO_UNIT.
4876 There's just a lot of work to do, and init_cutu_and_read_dies is big enough
4877 already.
4878
4879 STUB_COMP_UNIT_DIE is for the stub DIE, we copy over certain attributes
4880 from it to the DIE in the DWO. If NULL we are skipping the stub.
a2ce51a0
DE
4881 STUB_COMP_DIR is similar to STUB_COMP_UNIT_DIE: When reading a TU directly
4882 from the DWO file, bypassing the stub, it contains the DW_AT_comp_dir
4883 attribute of the referencing CU. Exactly one of STUB_COMP_UNIT_DIE and
4884 COMP_DIR must be non-NULL.
b0c7bfa9
DE
4885 *RESULT_READER,*RESULT_INFO_PTR,*RESULT_COMP_UNIT_DIE,*RESULT_HAS_CHILDREN
4886 are filled in with the info of the DIE from the DWO file.
4887 ABBREV_TABLE_PROVIDED is non-zero if the caller of init_cutu_and_read_dies
4888 provided an abbrev table to use.
4889 The result is non-zero if a valid (non-dummy) DIE was found. */
4890
4891static int
4892read_cutu_die_from_dwo (struct dwarf2_per_cu_data *this_cu,
4893 struct dwo_unit *dwo_unit,
4894 int abbrev_table_provided,
4895 struct die_info *stub_comp_unit_die,
a2ce51a0 4896 const char *stub_comp_dir,
b0c7bfa9 4897 struct die_reader_specs *result_reader,
d521ce57 4898 const gdb_byte **result_info_ptr,
b0c7bfa9
DE
4899 struct die_info **result_comp_unit_die,
4900 int *result_has_children)
4901{
4902 struct objfile *objfile = dwarf2_per_objfile->objfile;
4903 struct dwarf2_cu *cu = this_cu->cu;
4904 struct dwarf2_section_info *section;
4905 bfd *abfd;
d521ce57 4906 const gdb_byte *begin_info_ptr, *info_ptr;
b0c7bfa9
DE
4907 const char *comp_dir_string;
4908 ULONGEST signature; /* Or dwo_id. */
4909 struct attribute *comp_dir, *stmt_list, *low_pc, *high_pc, *ranges;
4910 int i,num_extra_attrs;
4911 struct dwarf2_section_info *dwo_abbrev_section;
4912 struct attribute *attr;
a2ce51a0 4913 struct attribute comp_dir_attr;
b0c7bfa9
DE
4914 struct die_info *comp_unit_die;
4915
a2ce51a0
DE
4916 /* Both can't be provided. */
4917 gdb_assert (! (stub_comp_unit_die && stub_comp_dir));
4918
b0c7bfa9
DE
4919 /* These attributes aren't processed until later:
4920 DW_AT_stmt_list, DW_AT_low_pc, DW_AT_high_pc, DW_AT_ranges.
4921 However, the attribute is found in the stub which we won't have later.
4922 In order to not impose this complication on the rest of the code,
4923 we read them here and copy them to the DWO CU/TU die. */
4924
4925 stmt_list = NULL;
4926 low_pc = NULL;
4927 high_pc = NULL;
4928 ranges = NULL;
4929 comp_dir = NULL;
4930
4931 if (stub_comp_unit_die != NULL)
4932 {
4933 /* For TUs in DWO files, the DW_AT_stmt_list attribute lives in the
4934 DWO file. */
4935 if (! this_cu->is_debug_types)
4936 stmt_list = dwarf2_attr (stub_comp_unit_die, DW_AT_stmt_list, cu);
4937 low_pc = dwarf2_attr (stub_comp_unit_die, DW_AT_low_pc, cu);
4938 high_pc = dwarf2_attr (stub_comp_unit_die, DW_AT_high_pc, cu);
4939 ranges = dwarf2_attr (stub_comp_unit_die, DW_AT_ranges, cu);
4940 comp_dir = dwarf2_attr (stub_comp_unit_die, DW_AT_comp_dir, cu);
4941
4942 /* There should be a DW_AT_addr_base attribute here (if needed).
4943 We need the value before we can process DW_FORM_GNU_addr_index. */
4944 cu->addr_base = 0;
4945 attr = dwarf2_attr (stub_comp_unit_die, DW_AT_GNU_addr_base, cu);
4946 if (attr)
4947 cu->addr_base = DW_UNSND (attr);
4948
4949 /* There should be a DW_AT_ranges_base attribute here (if needed).
4950 We need the value before we can process DW_AT_ranges. */
4951 cu->ranges_base = 0;
4952 attr = dwarf2_attr (stub_comp_unit_die, DW_AT_GNU_ranges_base, cu);
4953 if (attr)
4954 cu->ranges_base = DW_UNSND (attr);
4955 }
a2ce51a0
DE
4956 else if (stub_comp_dir != NULL)
4957 {
4958 /* Reconstruct the comp_dir attribute to simplify the code below. */
4959 comp_dir = (struct attribute *)
4960 obstack_alloc (&cu->comp_unit_obstack, sizeof (*comp_dir));
4961 comp_dir->name = DW_AT_comp_dir;
4962 comp_dir->form = DW_FORM_string;
4963 DW_STRING_IS_CANONICAL (comp_dir) = 0;
4964 DW_STRING (comp_dir) = stub_comp_dir;
4965 }
b0c7bfa9
DE
4966
4967 /* Set up for reading the DWO CU/TU. */
4968 cu->dwo_unit = dwo_unit;
4969 section = dwo_unit->section;
4970 dwarf2_read_section (objfile, section);
a32a8923 4971 abfd = get_section_bfd_owner (section);
b0c7bfa9
DE
4972 begin_info_ptr = info_ptr = section->buffer + dwo_unit->offset.sect_off;
4973 dwo_abbrev_section = &dwo_unit->dwo_file->sections.abbrev;
4974 init_cu_die_reader (result_reader, cu, section, dwo_unit->dwo_file);
4975
4976 if (this_cu->is_debug_types)
4977 {
4978 ULONGEST header_signature;
4979 cu_offset type_offset_in_tu;
4980 struct signatured_type *sig_type = (struct signatured_type *) this_cu;
4981
4982 info_ptr = read_and_check_type_unit_head (&cu->header, section,
4983 dwo_abbrev_section,
4984 info_ptr,
4985 &header_signature,
4986 &type_offset_in_tu);
a2ce51a0
DE
4987 /* This is not an assert because it can be caused by bad debug info. */
4988 if (sig_type->signature != header_signature)
4989 {
4990 error (_("Dwarf Error: signature mismatch %s vs %s while reading"
4991 " TU at offset 0x%x [in module %s]"),
4992 hex_string (sig_type->signature),
4993 hex_string (header_signature),
4994 dwo_unit->offset.sect_off,
4995 bfd_get_filename (abfd));
4996 }
b0c7bfa9
DE
4997 gdb_assert (dwo_unit->offset.sect_off == cu->header.offset.sect_off);
4998 /* For DWOs coming from DWP files, we don't know the CU length
4999 nor the type's offset in the TU until now. */
5000 dwo_unit->length = get_cu_length (&cu->header);
5001 dwo_unit->type_offset_in_tu = type_offset_in_tu;
5002
5003 /* Establish the type offset that can be used to lookup the type.
5004 For DWO files, we don't know it until now. */
5005 sig_type->type_offset_in_section.sect_off =
5006 dwo_unit->offset.sect_off + dwo_unit->type_offset_in_tu.cu_off;
5007 }
5008 else
5009 {
5010 info_ptr = read_and_check_comp_unit_head (&cu->header, section,
5011 dwo_abbrev_section,
5012 info_ptr, 0);
5013 gdb_assert (dwo_unit->offset.sect_off == cu->header.offset.sect_off);
5014 /* For DWOs coming from DWP files, we don't know the CU length
5015 until now. */
5016 dwo_unit->length = get_cu_length (&cu->header);
5017 }
5018
02142a6c
DE
5019 /* Replace the CU's original abbrev table with the DWO's.
5020 Reminder: We can't read the abbrev table until we've read the header. */
b0c7bfa9
DE
5021 if (abbrev_table_provided)
5022 {
5023 /* Don't free the provided abbrev table, the caller of
5024 init_cutu_and_read_dies owns it. */
5025 dwarf2_read_abbrevs (cu, dwo_abbrev_section);
02142a6c 5026 /* Ensure the DWO abbrev table gets freed. */
b0c7bfa9
DE
5027 make_cleanup (dwarf2_free_abbrev_table, cu);
5028 }
5029 else
5030 {
5031 dwarf2_free_abbrev_table (cu);
5032 dwarf2_read_abbrevs (cu, dwo_abbrev_section);
02142a6c 5033 /* Leave any existing abbrev table cleanup as is. */
b0c7bfa9
DE
5034 }
5035
5036 /* Read in the die, but leave space to copy over the attributes
5037 from the stub. This has the benefit of simplifying the rest of
5038 the code - all the work to maintain the illusion of a single
5039 DW_TAG_{compile,type}_unit DIE is done here. */
5040 num_extra_attrs = ((stmt_list != NULL)
5041 + (low_pc != NULL)
5042 + (high_pc != NULL)
5043 + (ranges != NULL)
5044 + (comp_dir != NULL));
5045 info_ptr = read_full_die_1 (result_reader, result_comp_unit_die, info_ptr,
5046 result_has_children, num_extra_attrs);
5047
5048 /* Copy over the attributes from the stub to the DIE we just read in. */
5049 comp_unit_die = *result_comp_unit_die;
5050 i = comp_unit_die->num_attrs;
5051 if (stmt_list != NULL)
5052 comp_unit_die->attrs[i++] = *stmt_list;
5053 if (low_pc != NULL)
5054 comp_unit_die->attrs[i++] = *low_pc;
5055 if (high_pc != NULL)
5056 comp_unit_die->attrs[i++] = *high_pc;
5057 if (ranges != NULL)
5058 comp_unit_die->attrs[i++] = *ranges;
5059 if (comp_dir != NULL)
5060 comp_unit_die->attrs[i++] = *comp_dir;
5061 comp_unit_die->num_attrs += num_extra_attrs;
5062
bf6af496
DE
5063 if (dwarf2_die_debug)
5064 {
5065 fprintf_unfiltered (gdb_stdlog,
5066 "Read die from %s@0x%x of %s:\n",
a32a8923 5067 get_section_name (section),
bf6af496
DE
5068 (unsigned) (begin_info_ptr - section->buffer),
5069 bfd_get_filename (abfd));
5070 dump_die (comp_unit_die, dwarf2_die_debug);
5071 }
5072
a2ce51a0
DE
5073 /* Save the comp_dir attribute. If there is no DWP file then we'll read
5074 TUs by skipping the stub and going directly to the entry in the DWO file.
5075 However, skipping the stub means we won't get DW_AT_comp_dir, so we have
5076 to get it via circuitous means. Blech. */
5077 if (comp_dir != NULL)
5078 result_reader->comp_dir = DW_STRING (comp_dir);
5079
b0c7bfa9
DE
5080 /* Skip dummy compilation units. */
5081 if (info_ptr >= begin_info_ptr + dwo_unit->length
5082 || peek_abbrev_code (abfd, info_ptr) == 0)
5083 return 0;
5084
5085 *result_info_ptr = info_ptr;
5086 return 1;
5087}
5088
5089/* Subroutine of init_cutu_and_read_dies to simplify it.
5090 Look up the DWO unit specified by COMP_UNIT_DIE of THIS_CU.
6a506a2d 5091 Returns NULL if the specified DWO unit cannot be found. */
b0c7bfa9
DE
5092
5093static struct dwo_unit *
5094lookup_dwo_unit (struct dwarf2_per_cu_data *this_cu,
5095 struct die_info *comp_unit_die)
5096{
5097 struct dwarf2_cu *cu = this_cu->cu;
5098 struct attribute *attr;
5099 ULONGEST signature;
5100 struct dwo_unit *dwo_unit;
5101 const char *comp_dir, *dwo_name;
5102
a2ce51a0
DE
5103 gdb_assert (cu != NULL);
5104
b0c7bfa9
DE
5105 /* Yeah, we look dwo_name up again, but it simplifies the code. */
5106 attr = dwarf2_attr (comp_unit_die, DW_AT_GNU_dwo_name, cu);
5107 gdb_assert (attr != NULL);
5108 dwo_name = DW_STRING (attr);
5109 comp_dir = NULL;
5110 attr = dwarf2_attr (comp_unit_die, DW_AT_comp_dir, cu);
5111 if (attr)
5112 comp_dir = DW_STRING (attr);
5113
5114 if (this_cu->is_debug_types)
5115 {
5116 struct signatured_type *sig_type;
5117
5118 /* Since this_cu is the first member of struct signatured_type,
5119 we can go from a pointer to one to a pointer to the other. */
5120 sig_type = (struct signatured_type *) this_cu;
5121 signature = sig_type->signature;
5122 dwo_unit = lookup_dwo_type_unit (sig_type, dwo_name, comp_dir);
5123 }
5124 else
5125 {
5126 struct attribute *attr;
5127
5128 attr = dwarf2_attr (comp_unit_die, DW_AT_GNU_dwo_id, cu);
5129 if (! attr)
5130 error (_("Dwarf Error: missing dwo_id for dwo_name %s"
5131 " [in module %s]"),
4262abfb 5132 dwo_name, objfile_name (this_cu->objfile));
b0c7bfa9
DE
5133 signature = DW_UNSND (attr);
5134 dwo_unit = lookup_dwo_comp_unit (this_cu, dwo_name, comp_dir,
5135 signature);
5136 }
5137
b0c7bfa9
DE
5138 return dwo_unit;
5139}
5140
a2ce51a0
DE
5141/* Subroutine of init_cutu_and_read_dies to simplify it.
5142 Read a TU directly from a DWO file, bypassing the stub. */
5143
5144static void
5145init_tu_and_read_dwo_dies (struct dwarf2_per_cu_data *this_cu, int keep,
5146 die_reader_func_ftype *die_reader_func,
5147 void *data)
5148{
5149 struct dwarf2_cu *cu;
5150 struct signatured_type *sig_type;
5151 struct cleanup *cleanups, *free_cu_cleanup;
5152 struct die_reader_specs reader;
5153 const gdb_byte *info_ptr;
5154 struct die_info *comp_unit_die;
5155 int has_children;
5156
5157 /* Verify we can do the following downcast, and that we have the
5158 data we need. */
5159 gdb_assert (this_cu->is_debug_types && this_cu->reading_dwo_directly);
5160 sig_type = (struct signatured_type *) this_cu;
5161 gdb_assert (sig_type->dwo_unit != NULL);
5162
5163 cleanups = make_cleanup (null_cleanup, NULL);
5164
5165 gdb_assert (this_cu->cu == NULL);
5166 cu = xmalloc (sizeof (*cu));
5167 init_one_comp_unit (cu, this_cu);
5168 /* If an error occurs while loading, release our storage. */
5169 free_cu_cleanup = make_cleanup (free_heap_comp_unit, cu);
5170
5171 if (read_cutu_die_from_dwo (this_cu, sig_type->dwo_unit,
5172 0 /* abbrev_table_provided */,
5173 NULL /* stub_comp_unit_die */,
5174 sig_type->dwo_unit->dwo_file->comp_dir,
5175 &reader, &info_ptr,
5176 &comp_unit_die, &has_children) == 0)
5177 {
5178 /* Dummy die. */
5179 do_cleanups (cleanups);
5180 return;
5181 }
5182
5183 /* All the "real" work is done here. */
5184 die_reader_func (&reader, info_ptr, comp_unit_die, has_children, data);
5185
5186 /* This duplicates some code in init_cutu_and_read_dies,
5187 but the alternative is making the latter more complex.
5188 This function is only for the special case of using DWO files directly:
5189 no point in overly complicating the general case just to handle this. */
5190 if (keep)
5191 {
5192 /* We've successfully allocated this compilation unit. Let our
5193 caller clean it up when finished with it. */
5194 discard_cleanups (free_cu_cleanup);
5195
5196 /* We can only discard free_cu_cleanup and all subsequent cleanups.
5197 So we have to manually free the abbrev table. */
5198 dwarf2_free_abbrev_table (cu);
5199
5200 /* Link this CU into read_in_chain. */
5201 this_cu->cu->read_in_chain = dwarf2_per_objfile->read_in_chain;
5202 dwarf2_per_objfile->read_in_chain = this_cu;
5203 }
5204 else
5205 do_cleanups (free_cu_cleanup);
5206
5207 do_cleanups (cleanups);
5208}
5209
fd820528 5210/* Initialize a CU (or TU) and read its DIEs.
3019eac3 5211 If the CU defers to a DWO file, read the DWO file as well.
dee91e82 5212
f4dc4d17
DE
5213 ABBREV_TABLE, if non-NULL, is the abbreviation table to use.
5214 Otherwise the table specified in the comp unit header is read in and used.
5215 This is an optimization for when we already have the abbrev table.
5216
dee91e82
DE
5217 If USE_EXISTING_CU is non-zero, and THIS_CU->cu is non-NULL, then use it.
5218 Otherwise, a new CU is allocated with xmalloc.
5219
5220 If KEEP is non-zero, then if we allocated a dwarf2_cu we add it to
5221 read_in_chain. Otherwise the dwarf2_cu data is freed at the end.
5222
5223 WARNING: If THIS_CU is a "dummy CU" (used as filler by the incremental
fd820528 5224 linker) then DIE_READER_FUNC will not get called. */
aaa75496 5225
70221824 5226static void
fd820528 5227init_cutu_and_read_dies (struct dwarf2_per_cu_data *this_cu,
f4dc4d17 5228 struct abbrev_table *abbrev_table,
fd820528
DE
5229 int use_existing_cu, int keep,
5230 die_reader_func_ftype *die_reader_func,
5231 void *data)
c906108c 5232{
dee91e82 5233 struct objfile *objfile = dwarf2_per_objfile->objfile;
8a0459fd 5234 struct dwarf2_section_info *section = this_cu->section;
a32a8923 5235 bfd *abfd = get_section_bfd_owner (section);
dee91e82 5236 struct dwarf2_cu *cu;
d521ce57 5237 const gdb_byte *begin_info_ptr, *info_ptr;
dee91e82 5238 struct die_reader_specs reader;
d85a05f0 5239 struct die_info *comp_unit_die;
dee91e82 5240 int has_children;
d85a05f0 5241 struct attribute *attr;
365156ad 5242 struct cleanup *cleanups, *free_cu_cleanup = NULL;
dee91e82 5243 struct signatured_type *sig_type = NULL;
4bdcc0c1 5244 struct dwarf2_section_info *abbrev_section;
42e7ad6c
DE
5245 /* Non-zero if CU currently points to a DWO file and we need to
5246 reread it. When this happens we need to reread the skeleton die
a2ce51a0 5247 before we can reread the DWO file (this only applies to CUs, not TUs). */
42e7ad6c 5248 int rereading_dwo_cu = 0;
c906108c 5249
09406207
DE
5250 if (dwarf2_die_debug)
5251 fprintf_unfiltered (gdb_stdlog, "Reading %s unit at offset 0x%x\n",
5252 this_cu->is_debug_types ? "type" : "comp",
5253 this_cu->offset.sect_off);
5254
dee91e82
DE
5255 if (use_existing_cu)
5256 gdb_assert (keep);
23745b47 5257
a2ce51a0
DE
5258 /* If we're reading a TU directly from a DWO file, including a virtual DWO
5259 file (instead of going through the stub), short-circuit all of this. */
5260 if (this_cu->reading_dwo_directly)
5261 {
5262 /* Narrow down the scope of possibilities to have to understand. */
5263 gdb_assert (this_cu->is_debug_types);
5264 gdb_assert (abbrev_table == NULL);
5265 gdb_assert (!use_existing_cu);
5266 init_tu_and_read_dwo_dies (this_cu, keep, die_reader_func, data);
5267 return;
5268 }
5269
dee91e82
DE
5270 cleanups = make_cleanup (null_cleanup, NULL);
5271
5272 /* This is cheap if the section is already read in. */
5273 dwarf2_read_section (objfile, section);
5274
5275 begin_info_ptr = info_ptr = section->buffer + this_cu->offset.sect_off;
36586728
TT
5276
5277 abbrev_section = get_abbrev_section_for_cu (this_cu);
dee91e82
DE
5278
5279 if (use_existing_cu && this_cu->cu != NULL)
5280 {
5281 cu = this_cu->cu;
42e7ad6c
DE
5282
5283 /* If this CU is from a DWO file we need to start over, we need to
5284 refetch the attributes from the skeleton CU.
5285 This could be optimized by retrieving those attributes from when we
5286 were here the first time: the previous comp_unit_die was stored in
5287 comp_unit_obstack. But there's no data yet that we need this
5288 optimization. */
5289 if (cu->dwo_unit != NULL)
5290 rereading_dwo_cu = 1;
dee91e82
DE
5291 }
5292 else
5293 {
5294 /* If !use_existing_cu, this_cu->cu must be NULL. */
5295 gdb_assert (this_cu->cu == NULL);
5296
5297 cu = xmalloc (sizeof (*cu));
5298 init_one_comp_unit (cu, this_cu);
5299
5300 /* If an error occurs while loading, release our storage. */
365156ad 5301 free_cu_cleanup = make_cleanup (free_heap_comp_unit, cu);
42e7ad6c 5302 }
dee91e82 5303
b0c7bfa9 5304 /* Get the header. */
42e7ad6c
DE
5305 if (cu->header.first_die_offset.cu_off != 0 && ! rereading_dwo_cu)
5306 {
5307 /* We already have the header, there's no need to read it in again. */
5308 info_ptr += cu->header.first_die_offset.cu_off;
5309 }
5310 else
5311 {
3019eac3 5312 if (this_cu->is_debug_types)
dee91e82
DE
5313 {
5314 ULONGEST signature;
42e7ad6c 5315 cu_offset type_offset_in_tu;
dee91e82 5316
4bdcc0c1
DE
5317 info_ptr = read_and_check_type_unit_head (&cu->header, section,
5318 abbrev_section, info_ptr,
42e7ad6c
DE
5319 &signature,
5320 &type_offset_in_tu);
dee91e82 5321
42e7ad6c
DE
5322 /* Since per_cu is the first member of struct signatured_type,
5323 we can go from a pointer to one to a pointer to the other. */
5324 sig_type = (struct signatured_type *) this_cu;
5325 gdb_assert (sig_type->signature == signature);
5326 gdb_assert (sig_type->type_offset_in_tu.cu_off
5327 == type_offset_in_tu.cu_off);
dee91e82
DE
5328 gdb_assert (this_cu->offset.sect_off == cu->header.offset.sect_off);
5329
42e7ad6c
DE
5330 /* LENGTH has not been set yet for type units if we're
5331 using .gdb_index. */
1ce1cefd 5332 this_cu->length = get_cu_length (&cu->header);
3019eac3
DE
5333
5334 /* Establish the type offset that can be used to lookup the type. */
5335 sig_type->type_offset_in_section.sect_off =
5336 this_cu->offset.sect_off + sig_type->type_offset_in_tu.cu_off;
dee91e82
DE
5337 }
5338 else
5339 {
4bdcc0c1
DE
5340 info_ptr = read_and_check_comp_unit_head (&cu->header, section,
5341 abbrev_section,
5342 info_ptr, 0);
dee91e82
DE
5343
5344 gdb_assert (this_cu->offset.sect_off == cu->header.offset.sect_off);
1ce1cefd 5345 gdb_assert (this_cu->length == get_cu_length (&cu->header));
dee91e82
DE
5346 }
5347 }
10b3939b 5348
6caca83c 5349 /* Skip dummy compilation units. */
dee91e82 5350 if (info_ptr >= begin_info_ptr + this_cu->length
6caca83c
CC
5351 || peek_abbrev_code (abfd, info_ptr) == 0)
5352 {
dee91e82 5353 do_cleanups (cleanups);
21b2bd31 5354 return;
6caca83c
CC
5355 }
5356
433df2d4
DE
5357 /* If we don't have them yet, read the abbrevs for this compilation unit.
5358 And if we need to read them now, make sure they're freed when we're
42e7ad6c
DE
5359 done. Note that it's important that if the CU had an abbrev table
5360 on entry we don't free it when we're done: Somewhere up the call stack
5361 it may be in use. */
f4dc4d17
DE
5362 if (abbrev_table != NULL)
5363 {
5364 gdb_assert (cu->abbrev_table == NULL);
5365 gdb_assert (cu->header.abbrev_offset.sect_off
5366 == abbrev_table->offset.sect_off);
5367 cu->abbrev_table = abbrev_table;
5368 }
5369 else if (cu->abbrev_table == NULL)
dee91e82 5370 {
4bdcc0c1 5371 dwarf2_read_abbrevs (cu, abbrev_section);
dee91e82
DE
5372 make_cleanup (dwarf2_free_abbrev_table, cu);
5373 }
42e7ad6c
DE
5374 else if (rereading_dwo_cu)
5375 {
5376 dwarf2_free_abbrev_table (cu);
5377 dwarf2_read_abbrevs (cu, abbrev_section);
5378 }
af703f96 5379
dee91e82 5380 /* Read the top level CU/TU die. */
3019eac3 5381 init_cu_die_reader (&reader, cu, section, NULL);
dee91e82 5382 info_ptr = read_full_die (&reader, &comp_unit_die, info_ptr, &has_children);
93311388 5383
b0c7bfa9
DE
5384 /* If we are in a DWO stub, process it and then read in the "real" CU/TU
5385 from the DWO file.
5386 Note that if USE_EXISTING_OK != 0, and THIS_CU->cu already contains a
5387 DWO CU, that this test will fail (the attribute will not be present). */
3019eac3
DE
5388 attr = dwarf2_attr (comp_unit_die, DW_AT_GNU_dwo_name, cu);
5389 if (attr)
5390 {
3019eac3 5391 struct dwo_unit *dwo_unit;
b0c7bfa9 5392 struct die_info *dwo_comp_unit_die;
3019eac3
DE
5393
5394 if (has_children)
6a506a2d
DE
5395 {
5396 complaint (&symfile_complaints,
5397 _("compilation unit with DW_AT_GNU_dwo_name"
5398 " has children (offset 0x%x) [in module %s]"),
5399 this_cu->offset.sect_off, bfd_get_filename (abfd));
5400 }
b0c7bfa9 5401 dwo_unit = lookup_dwo_unit (this_cu, comp_unit_die);
6a506a2d 5402 if (dwo_unit != NULL)
3019eac3 5403 {
6a506a2d
DE
5404 if (read_cutu_die_from_dwo (this_cu, dwo_unit,
5405 abbrev_table != NULL,
a2ce51a0 5406 comp_unit_die, NULL,
6a506a2d
DE
5407 &reader, &info_ptr,
5408 &dwo_comp_unit_die, &has_children) == 0)
5409 {
5410 /* Dummy die. */
5411 do_cleanups (cleanups);
5412 return;
5413 }
5414 comp_unit_die = dwo_comp_unit_die;
5415 }
5416 else
5417 {
5418 /* Yikes, we couldn't find the rest of the DIE, we only have
5419 the stub. A complaint has already been logged. There's
5420 not much more we can do except pass on the stub DIE to
5421 die_reader_func. We don't want to throw an error on bad
5422 debug info. */
3019eac3
DE
5423 }
5424 }
5425
b0c7bfa9 5426 /* All of the above is setup for this call. Yikes. */
dee91e82
DE
5427 die_reader_func (&reader, info_ptr, comp_unit_die, has_children, data);
5428
b0c7bfa9 5429 /* Done, clean up. */
365156ad 5430 if (free_cu_cleanup != NULL)
348e048f 5431 {
365156ad
TT
5432 if (keep)
5433 {
5434 /* We've successfully allocated this compilation unit. Let our
5435 caller clean it up when finished with it. */
5436 discard_cleanups (free_cu_cleanup);
dee91e82 5437
365156ad
TT
5438 /* We can only discard free_cu_cleanup and all subsequent cleanups.
5439 So we have to manually free the abbrev table. */
5440 dwarf2_free_abbrev_table (cu);
dee91e82 5441
365156ad
TT
5442 /* Link this CU into read_in_chain. */
5443 this_cu->cu->read_in_chain = dwarf2_per_objfile->read_in_chain;
5444 dwarf2_per_objfile->read_in_chain = this_cu;
5445 }
5446 else
5447 do_cleanups (free_cu_cleanup);
348e048f 5448 }
365156ad
TT
5449
5450 do_cleanups (cleanups);
dee91e82
DE
5451}
5452
3019eac3
DE
5453/* Read CU/TU THIS_CU in section SECTION,
5454 but do not follow DW_AT_GNU_dwo_name if present.
80626a55
DE
5455 DWOP_FILE, if non-NULL, is the DWO/DWP file to read (the caller is assumed
5456 to have already done the lookup to find the DWO/DWP file).
dee91e82
DE
5457
5458 The caller is required to fill in THIS_CU->section, THIS_CU->offset, and
3019eac3 5459 THIS_CU->is_debug_types, but nothing else.
dee91e82
DE
5460
5461 We fill in THIS_CU->length.
5462
5463 WARNING: If THIS_CU is a "dummy CU" (used as filler by the incremental
5464 linker) then DIE_READER_FUNC will not get called.
5465
5466 THIS_CU->cu is always freed when done.
3019eac3
DE
5467 This is done in order to not leave THIS_CU->cu in a state where we have
5468 to care whether it refers to the "main" CU or the DWO CU. */
dee91e82
DE
5469
5470static void
5471init_cutu_and_read_dies_no_follow (struct dwarf2_per_cu_data *this_cu,
5472 struct dwarf2_section_info *abbrev_section,
3019eac3 5473 struct dwo_file *dwo_file,
dee91e82
DE
5474 die_reader_func_ftype *die_reader_func,
5475 void *data)
5476{
5477 struct objfile *objfile = dwarf2_per_objfile->objfile;
8a0459fd 5478 struct dwarf2_section_info *section = this_cu->section;
a32a8923 5479 bfd *abfd = get_section_bfd_owner (section);
dee91e82 5480 struct dwarf2_cu cu;
d521ce57 5481 const gdb_byte *begin_info_ptr, *info_ptr;
dee91e82
DE
5482 struct die_reader_specs reader;
5483 struct cleanup *cleanups;
5484 struct die_info *comp_unit_die;
5485 int has_children;
5486
09406207
DE
5487 if (dwarf2_die_debug)
5488 fprintf_unfiltered (gdb_stdlog, "Reading %s unit at offset 0x%x\n",
5489 this_cu->is_debug_types ? "type" : "comp",
5490 this_cu->offset.sect_off);
5491
dee91e82
DE
5492 gdb_assert (this_cu->cu == NULL);
5493
dee91e82
DE
5494 /* This is cheap if the section is already read in. */
5495 dwarf2_read_section (objfile, section);
5496
5497 init_one_comp_unit (&cu, this_cu);
5498
5499 cleanups = make_cleanup (free_stack_comp_unit, &cu);
5500
5501 begin_info_ptr = info_ptr = section->buffer + this_cu->offset.sect_off;
4bdcc0c1
DE
5502 info_ptr = read_and_check_comp_unit_head (&cu.header, section,
5503 abbrev_section, info_ptr,
3019eac3 5504 this_cu->is_debug_types);
dee91e82 5505
1ce1cefd 5506 this_cu->length = get_cu_length (&cu.header);
dee91e82
DE
5507
5508 /* Skip dummy compilation units. */
5509 if (info_ptr >= begin_info_ptr + this_cu->length
5510 || peek_abbrev_code (abfd, info_ptr) == 0)
c906108c 5511 {
dee91e82 5512 do_cleanups (cleanups);
21b2bd31 5513 return;
93311388 5514 }
72bf9492 5515
dee91e82
DE
5516 dwarf2_read_abbrevs (&cu, abbrev_section);
5517 make_cleanup (dwarf2_free_abbrev_table, &cu);
5518
3019eac3 5519 init_cu_die_reader (&reader, &cu, section, dwo_file);
dee91e82
DE
5520 info_ptr = read_full_die (&reader, &comp_unit_die, info_ptr, &has_children);
5521
5522 die_reader_func (&reader, info_ptr, comp_unit_die, has_children, data);
5523
5524 do_cleanups (cleanups);
5525}
5526
3019eac3
DE
5527/* Read a CU/TU, except that this does not look for DW_AT_GNU_dwo_name and
5528 does not lookup the specified DWO file.
5529 This cannot be used to read DWO files.
dee91e82
DE
5530
5531 THIS_CU->cu is always freed when done.
3019eac3
DE
5532 This is done in order to not leave THIS_CU->cu in a state where we have
5533 to care whether it refers to the "main" CU or the DWO CU.
5534 We can revisit this if the data shows there's a performance issue. */
dee91e82
DE
5535
5536static void
5537init_cutu_and_read_dies_simple (struct dwarf2_per_cu_data *this_cu,
5538 die_reader_func_ftype *die_reader_func,
5539 void *data)
5540{
5541 init_cutu_and_read_dies_no_follow (this_cu,
36586728 5542 get_abbrev_section_for_cu (this_cu),
3019eac3 5543 NULL,
dee91e82
DE
5544 die_reader_func, data);
5545}
0018ea6f
DE
5546\f
5547/* Type Unit Groups.
dee91e82 5548
0018ea6f
DE
5549 Type Unit Groups are a way to collapse the set of all TUs (type units) into
5550 a more manageable set. The grouping is done by DW_AT_stmt_list entry
5551 so that all types coming from the same compilation (.o file) are grouped
5552 together. A future step could be to put the types in the same symtab as
5553 the CU the types ultimately came from. */
ff013f42 5554
f4dc4d17
DE
5555static hashval_t
5556hash_type_unit_group (const void *item)
5557{
094b34ac 5558 const struct type_unit_group *tu_group = item;
f4dc4d17 5559
094b34ac 5560 return hash_stmt_list_entry (&tu_group->hash);
f4dc4d17 5561}
348e048f
DE
5562
5563static int
f4dc4d17 5564eq_type_unit_group (const void *item_lhs, const void *item_rhs)
348e048f 5565{
f4dc4d17
DE
5566 const struct type_unit_group *lhs = item_lhs;
5567 const struct type_unit_group *rhs = item_rhs;
348e048f 5568
094b34ac 5569 return eq_stmt_list_entry (&lhs->hash, &rhs->hash);
f4dc4d17 5570}
348e048f 5571
f4dc4d17
DE
5572/* Allocate a hash table for type unit groups. */
5573
5574static htab_t
5575allocate_type_unit_groups_table (void)
5576{
5577 return htab_create_alloc_ex (3,
5578 hash_type_unit_group,
5579 eq_type_unit_group,
5580 NULL,
5581 &dwarf2_per_objfile->objfile->objfile_obstack,
5582 hashtab_obstack_allocate,
5583 dummy_obstack_deallocate);
5584}
dee91e82 5585
f4dc4d17
DE
5586/* Type units that don't have DW_AT_stmt_list are grouped into their own
5587 partial symtabs. We combine several TUs per psymtab to not let the size
5588 of any one psymtab grow too big. */
5589#define NO_STMT_LIST_TYPE_UNIT_PSYMTAB (1 << 31)
5590#define NO_STMT_LIST_TYPE_UNIT_PSYMTAB_SIZE 10
dee91e82 5591
094b34ac 5592/* Helper routine for get_type_unit_group.
f4dc4d17
DE
5593 Create the type_unit_group object used to hold one or more TUs. */
5594
5595static struct type_unit_group *
094b34ac 5596create_type_unit_group (struct dwarf2_cu *cu, sect_offset line_offset_struct)
f4dc4d17
DE
5597{
5598 struct objfile *objfile = dwarf2_per_objfile->objfile;
094b34ac 5599 struct dwarf2_per_cu_data *per_cu;
f4dc4d17 5600 struct type_unit_group *tu_group;
f4dc4d17
DE
5601
5602 tu_group = OBSTACK_ZALLOC (&objfile->objfile_obstack,
5603 struct type_unit_group);
094b34ac 5604 per_cu = &tu_group->per_cu;
f4dc4d17 5605 per_cu->objfile = objfile;
f4dc4d17 5606
094b34ac
DE
5607 if (dwarf2_per_objfile->using_index)
5608 {
5609 per_cu->v.quick = OBSTACK_ZALLOC (&objfile->objfile_obstack,
5610 struct dwarf2_per_cu_quick_data);
094b34ac
DE
5611 }
5612 else
5613 {
5614 unsigned int line_offset = line_offset_struct.sect_off;
5615 struct partial_symtab *pst;
5616 char *name;
5617
5618 /* Give the symtab a useful name for debug purposes. */
5619 if ((line_offset & NO_STMT_LIST_TYPE_UNIT_PSYMTAB) != 0)
5620 name = xstrprintf ("<type_units_%d>",
5621 (line_offset & ~NO_STMT_LIST_TYPE_UNIT_PSYMTAB));
5622 else
5623 name = xstrprintf ("<type_units_at_0x%x>", line_offset);
5624
5625 pst = create_partial_symtab (per_cu, name);
5626 pst->anonymous = 1;
f4dc4d17 5627
094b34ac
DE
5628 xfree (name);
5629 }
f4dc4d17 5630
094b34ac
DE
5631 tu_group->hash.dwo_unit = cu->dwo_unit;
5632 tu_group->hash.line_offset = line_offset_struct;
f4dc4d17
DE
5633
5634 return tu_group;
5635}
5636
094b34ac
DE
5637/* Look up the type_unit_group for type unit CU, and create it if necessary.
5638 STMT_LIST is a DW_AT_stmt_list attribute. */
f4dc4d17
DE
5639
5640static struct type_unit_group *
ff39bb5e 5641get_type_unit_group (struct dwarf2_cu *cu, const struct attribute *stmt_list)
f4dc4d17
DE
5642{
5643 struct tu_stats *tu_stats = &dwarf2_per_objfile->tu_stats;
5644 struct type_unit_group *tu_group;
5645 void **slot;
5646 unsigned int line_offset;
5647 struct type_unit_group type_unit_group_for_lookup;
5648
5649 if (dwarf2_per_objfile->type_unit_groups == NULL)
5650 {
5651 dwarf2_per_objfile->type_unit_groups =
5652 allocate_type_unit_groups_table ();
5653 }
5654
5655 /* Do we need to create a new group, or can we use an existing one? */
5656
5657 if (stmt_list)
5658 {
5659 line_offset = DW_UNSND (stmt_list);
5660 ++tu_stats->nr_symtab_sharers;
5661 }
5662 else
5663 {
5664 /* Ugh, no stmt_list. Rare, but we have to handle it.
5665 We can do various things here like create one group per TU or
5666 spread them over multiple groups to split up the expansion work.
5667 To avoid worst case scenarios (too many groups or too large groups)
5668 we, umm, group them in bunches. */
5669 line_offset = (NO_STMT_LIST_TYPE_UNIT_PSYMTAB
5670 | (tu_stats->nr_stmt_less_type_units
5671 / NO_STMT_LIST_TYPE_UNIT_PSYMTAB_SIZE));
5672 ++tu_stats->nr_stmt_less_type_units;
5673 }
5674
094b34ac
DE
5675 type_unit_group_for_lookup.hash.dwo_unit = cu->dwo_unit;
5676 type_unit_group_for_lookup.hash.line_offset.sect_off = line_offset;
f4dc4d17
DE
5677 slot = htab_find_slot (dwarf2_per_objfile->type_unit_groups,
5678 &type_unit_group_for_lookup, INSERT);
5679 if (*slot != NULL)
5680 {
5681 tu_group = *slot;
5682 gdb_assert (tu_group != NULL);
5683 }
5684 else
5685 {
5686 sect_offset line_offset_struct;
5687
5688 line_offset_struct.sect_off = line_offset;
094b34ac 5689 tu_group = create_type_unit_group (cu, line_offset_struct);
f4dc4d17
DE
5690 *slot = tu_group;
5691 ++tu_stats->nr_symtabs;
5692 }
5693
5694 return tu_group;
5695}
5696
5697/* Struct used to sort TUs by their abbreviation table offset. */
5698
5699struct tu_abbrev_offset
5700{
5701 struct signatured_type *sig_type;
5702 sect_offset abbrev_offset;
5703};
5704
5705/* Helper routine for build_type_unit_groups, passed to qsort. */
5706
5707static int
5708sort_tu_by_abbrev_offset (const void *ap, const void *bp)
5709{
5710 const struct tu_abbrev_offset * const *a = ap;
5711 const struct tu_abbrev_offset * const *b = bp;
5712 unsigned int aoff = (*a)->abbrev_offset.sect_off;
5713 unsigned int boff = (*b)->abbrev_offset.sect_off;
5714
5715 return (aoff > boff) - (aoff < boff);
5716}
5717
5718/* A helper function to add a type_unit_group to a table. */
5719
5720static int
5721add_type_unit_group_to_table (void **slot, void *datum)
5722{
5723 struct type_unit_group *tu_group = *slot;
5724 struct type_unit_group ***datap = datum;
5725
5726 **datap = tu_group;
5727 ++*datap;
5728
5729 return 1;
5730}
5731
5732/* Efficiently read all the type units, calling init_cutu_and_read_dies on
5733 each one passing FUNC,DATA.
5734
5735 The efficiency is because we sort TUs by the abbrev table they use and
5736 only read each abbrev table once. In one program there are 200K TUs
5737 sharing 8K abbrev tables.
5738
5739 The main purpose of this function is to support building the
5740 dwarf2_per_objfile->type_unit_groups table.
5741 TUs typically share the DW_AT_stmt_list of the CU they came from, so we
5742 can collapse the search space by grouping them by stmt_list.
5743 The savings can be significant, in the same program from above the 200K TUs
5744 share 8K stmt_list tables.
5745
5746 FUNC is expected to call get_type_unit_group, which will create the
5747 struct type_unit_group if necessary and add it to
5748 dwarf2_per_objfile->type_unit_groups. */
5749
5750static void
5751build_type_unit_groups (die_reader_func_ftype *func, void *data)
5752{
5753 struct objfile *objfile = dwarf2_per_objfile->objfile;
5754 struct tu_stats *tu_stats = &dwarf2_per_objfile->tu_stats;
5755 struct cleanup *cleanups;
5756 struct abbrev_table *abbrev_table;
5757 sect_offset abbrev_offset;
5758 struct tu_abbrev_offset *sorted_by_abbrev;
5759 struct type_unit_group **iter;
5760 int i;
5761
5762 /* It's up to the caller to not call us multiple times. */
5763 gdb_assert (dwarf2_per_objfile->type_unit_groups == NULL);
5764
5765 if (dwarf2_per_objfile->n_type_units == 0)
5766 return;
5767
5768 /* TUs typically share abbrev tables, and there can be way more TUs than
5769 abbrev tables. Sort by abbrev table to reduce the number of times we
5770 read each abbrev table in.
5771 Alternatives are to punt or to maintain a cache of abbrev tables.
5772 This is simpler and efficient enough for now.
5773
5774 Later we group TUs by their DW_AT_stmt_list value (as this defines the
5775 symtab to use). Typically TUs with the same abbrev offset have the same
5776 stmt_list value too so in practice this should work well.
5777
5778 The basic algorithm here is:
5779
5780 sort TUs by abbrev table
5781 for each TU with same abbrev table:
5782 read abbrev table if first user
5783 read TU top level DIE
5784 [IWBN if DWO skeletons had DW_AT_stmt_list]
5785 call FUNC */
5786
5787 if (dwarf2_read_debug)
5788 fprintf_unfiltered (gdb_stdlog, "Building type unit groups ...\n");
5789
5790 /* Sort in a separate table to maintain the order of all_type_units
5791 for .gdb_index: TU indices directly index all_type_units. */
5792 sorted_by_abbrev = XNEWVEC (struct tu_abbrev_offset,
5793 dwarf2_per_objfile->n_type_units);
5794 for (i = 0; i < dwarf2_per_objfile->n_type_units; ++i)
5795 {
5796 struct signatured_type *sig_type = dwarf2_per_objfile->all_type_units[i];
5797
5798 sorted_by_abbrev[i].sig_type = sig_type;
5799 sorted_by_abbrev[i].abbrev_offset =
8a0459fd 5800 read_abbrev_offset (sig_type->per_cu.section,
f4dc4d17
DE
5801 sig_type->per_cu.offset);
5802 }
5803 cleanups = make_cleanup (xfree, sorted_by_abbrev);
5804 qsort (sorted_by_abbrev, dwarf2_per_objfile->n_type_units,
5805 sizeof (struct tu_abbrev_offset), sort_tu_by_abbrev_offset);
5806
094b34ac
DE
5807 /* Note: In the .gdb_index case, get_type_unit_group may have already been
5808 called any number of times, so we don't reset tu_stats here. */
5809
f4dc4d17
DE
5810 abbrev_offset.sect_off = ~(unsigned) 0;
5811 abbrev_table = NULL;
5812 make_cleanup (abbrev_table_free_cleanup, &abbrev_table);
5813
5814 for (i = 0; i < dwarf2_per_objfile->n_type_units; ++i)
5815 {
5816 const struct tu_abbrev_offset *tu = &sorted_by_abbrev[i];
5817
5818 /* Switch to the next abbrev table if necessary. */
5819 if (abbrev_table == NULL
5820 || tu->abbrev_offset.sect_off != abbrev_offset.sect_off)
5821 {
5822 if (abbrev_table != NULL)
5823 {
5824 abbrev_table_free (abbrev_table);
5825 /* Reset to NULL in case abbrev_table_read_table throws
5826 an error: abbrev_table_free_cleanup will get called. */
5827 abbrev_table = NULL;
5828 }
5829 abbrev_offset = tu->abbrev_offset;
5830 abbrev_table =
5831 abbrev_table_read_table (&dwarf2_per_objfile->abbrev,
5832 abbrev_offset);
5833 ++tu_stats->nr_uniq_abbrev_tables;
5834 }
5835
5836 init_cutu_and_read_dies (&tu->sig_type->per_cu, abbrev_table, 0, 0,
5837 func, data);
5838 }
5839
a2ce51a0
DE
5840 /* type_unit_groups can be NULL if there is an error in the debug info.
5841 Just create an empty table so the rest of gdb doesn't have to watch
5842 for this error case. */
5843 if (dwarf2_per_objfile->type_unit_groups == NULL)
5844 {
5845 dwarf2_per_objfile->type_unit_groups =
5846 allocate_type_unit_groups_table ();
5847 dwarf2_per_objfile->n_type_unit_groups = 0;
5848 }
5849
f4dc4d17
DE
5850 /* Create a vector of pointers to primary type units to make it easy to
5851 iterate over them and CUs. See dw2_get_primary_cu. */
5852 dwarf2_per_objfile->n_type_unit_groups =
5853 htab_elements (dwarf2_per_objfile->type_unit_groups);
5854 dwarf2_per_objfile->all_type_unit_groups =
5855 obstack_alloc (&objfile->objfile_obstack,
5856 dwarf2_per_objfile->n_type_unit_groups
5857 * sizeof (struct type_unit_group *));
5858 iter = &dwarf2_per_objfile->all_type_unit_groups[0];
5859 htab_traverse_noresize (dwarf2_per_objfile->type_unit_groups,
5860 add_type_unit_group_to_table, &iter);
5861 gdb_assert (iter - &dwarf2_per_objfile->all_type_unit_groups[0]
5862 == dwarf2_per_objfile->n_type_unit_groups);
5863
5864 do_cleanups (cleanups);
5865
5866 if (dwarf2_read_debug)
5867 {
5868 fprintf_unfiltered (gdb_stdlog, "Done building type unit groups:\n");
5869 fprintf_unfiltered (gdb_stdlog, " %d TUs\n",
5870 dwarf2_per_objfile->n_type_units);
5871 fprintf_unfiltered (gdb_stdlog, " %d uniq abbrev tables\n",
5872 tu_stats->nr_uniq_abbrev_tables);
5873 fprintf_unfiltered (gdb_stdlog, " %d symtabs from stmt_list entries\n",
5874 tu_stats->nr_symtabs);
5875 fprintf_unfiltered (gdb_stdlog, " %d symtab sharers\n",
5876 tu_stats->nr_symtab_sharers);
5877 fprintf_unfiltered (gdb_stdlog, " %d type units without a stmt_list\n",
5878 tu_stats->nr_stmt_less_type_units);
5879 }
5880}
0018ea6f
DE
5881\f
5882/* Partial symbol tables. */
5883
5884/* Create a psymtab named NAME and assign it to PER_CU.
5885
5886 The caller must fill in the following details:
5887 dirname, textlow, texthigh. */
5888
5889static struct partial_symtab *
5890create_partial_symtab (struct dwarf2_per_cu_data *per_cu, const char *name)
5891{
5892 struct objfile *objfile = per_cu->objfile;
5893 struct partial_symtab *pst;
5894
5895 pst = start_psymtab_common (objfile, objfile->section_offsets,
5896 name, 0,
5897 objfile->global_psymbols.next,
5898 objfile->static_psymbols.next);
5899
5900 pst->psymtabs_addrmap_supported = 1;
5901
5902 /* This is the glue that links PST into GDB's symbol API. */
5903 pst->read_symtab_private = per_cu;
5904 pst->read_symtab = dwarf2_read_symtab;
5905 per_cu->v.psymtab = pst;
5906
5907 return pst;
5908}
5909
b93601f3
TT
5910/* The DATA object passed to process_psymtab_comp_unit_reader has this
5911 type. */
5912
5913struct process_psymtab_comp_unit_data
5914{
5915 /* True if we are reading a DW_TAG_partial_unit. */
5916
5917 int want_partial_unit;
5918
5919 /* The "pretend" language that is used if the CU doesn't declare a
5920 language. */
5921
5922 enum language pretend_language;
5923};
5924
0018ea6f
DE
5925/* die_reader_func for process_psymtab_comp_unit. */
5926
5927static void
5928process_psymtab_comp_unit_reader (const struct die_reader_specs *reader,
d521ce57 5929 const gdb_byte *info_ptr,
0018ea6f
DE
5930 struct die_info *comp_unit_die,
5931 int has_children,
5932 void *data)
5933{
5934 struct dwarf2_cu *cu = reader->cu;
5935 struct objfile *objfile = cu->objfile;
5936 struct dwarf2_per_cu_data *per_cu = cu->per_cu;
5937 struct attribute *attr;
5938 CORE_ADDR baseaddr;
5939 CORE_ADDR best_lowpc = 0, best_highpc = 0;
5940 struct partial_symtab *pst;
5941 int has_pc_info;
5942 const char *filename;
b93601f3 5943 struct process_psymtab_comp_unit_data *info = data;
0018ea6f 5944
b93601f3 5945 if (comp_unit_die->tag == DW_TAG_partial_unit && !info->want_partial_unit)
0018ea6f
DE
5946 return;
5947
5948 gdb_assert (! per_cu->is_debug_types);
5949
b93601f3 5950 prepare_one_comp_unit (cu, comp_unit_die, info->pretend_language);
0018ea6f
DE
5951
5952 cu->list_in_scope = &file_symbols;
5953
5954 /* Allocate a new partial symbol table structure. */
5955 attr = dwarf2_attr (comp_unit_die, DW_AT_name, cu);
5956 if (attr == NULL || !DW_STRING (attr))
5957 filename = "";
5958 else
5959 filename = DW_STRING (attr);
5960
5961 pst = create_partial_symtab (per_cu, filename);
5962
5963 /* This must be done before calling dwarf2_build_include_psymtabs. */
5964 attr = dwarf2_attr (comp_unit_die, DW_AT_comp_dir, cu);
5965 if (attr != NULL)
5966 pst->dirname = DW_STRING (attr);
5967
5968 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
5969
5970 dwarf2_find_base_address (comp_unit_die, cu);
5971
5972 /* Possibly set the default values of LOWPC and HIGHPC from
5973 `DW_AT_ranges'. */
5974 has_pc_info = dwarf2_get_pc_bounds (comp_unit_die, &best_lowpc,
5975 &best_highpc, cu, pst);
5976 if (has_pc_info == 1 && best_lowpc < best_highpc)
5977 /* Store the contiguous range if it is not empty; it can be empty for
5978 CUs with no code. */
5979 addrmap_set_empty (objfile->psymtabs_addrmap,
5980 best_lowpc + baseaddr,
5981 best_highpc + baseaddr - 1, pst);
5982
5983 /* Check if comp unit has_children.
5984 If so, read the rest of the partial symbols from this comp unit.
5985 If not, there's no more debug_info for this comp unit. */
5986 if (has_children)
5987 {
5988 struct partial_die_info *first_die;
5989 CORE_ADDR lowpc, highpc;
5990
5991 lowpc = ((CORE_ADDR) -1);
5992 highpc = ((CORE_ADDR) 0);
5993
5994 first_die = load_partial_dies (reader, info_ptr, 1);
5995
5996 scan_partial_symbols (first_die, &lowpc, &highpc,
5997 ! has_pc_info, cu);
5998
5999 /* If we didn't find a lowpc, set it to highpc to avoid
6000 complaints from `maint check'. */
6001 if (lowpc == ((CORE_ADDR) -1))
6002 lowpc = highpc;
6003
6004 /* If the compilation unit didn't have an explicit address range,
6005 then use the information extracted from its child dies. */
6006 if (! has_pc_info)
6007 {
6008 best_lowpc = lowpc;
6009 best_highpc = highpc;
6010 }
6011 }
6012 pst->textlow = best_lowpc + baseaddr;
6013 pst->texthigh = best_highpc + baseaddr;
6014
6015 pst->n_global_syms = objfile->global_psymbols.next -
6016 (objfile->global_psymbols.list + pst->globals_offset);
6017 pst->n_static_syms = objfile->static_psymbols.next -
6018 (objfile->static_psymbols.list + pst->statics_offset);
6019 sort_pst_symbols (objfile, pst);
6020
6021 if (!VEC_empty (dwarf2_per_cu_ptr, cu->per_cu->imported_symtabs))
6022 {
6023 int i;
6024 int len = VEC_length (dwarf2_per_cu_ptr, cu->per_cu->imported_symtabs);
6025 struct dwarf2_per_cu_data *iter;
6026
6027 /* Fill in 'dependencies' here; we fill in 'users' in a
6028 post-pass. */
6029 pst->number_of_dependencies = len;
6030 pst->dependencies = obstack_alloc (&objfile->objfile_obstack,
6031 len * sizeof (struct symtab *));
6032 for (i = 0;
6033 VEC_iterate (dwarf2_per_cu_ptr, cu->per_cu->imported_symtabs,
6034 i, iter);
6035 ++i)
6036 pst->dependencies[i] = iter->v.psymtab;
6037
6038 VEC_free (dwarf2_per_cu_ptr, cu->per_cu->imported_symtabs);
6039 }
6040
6041 /* Get the list of files included in the current compilation unit,
6042 and build a psymtab for each of them. */
6043 dwarf2_build_include_psymtabs (cu, comp_unit_die, pst);
6044
6045 if (dwarf2_read_debug)
6046 {
6047 struct gdbarch *gdbarch = get_objfile_arch (objfile);
6048
6049 fprintf_unfiltered (gdb_stdlog,
6050 "Psymtab for %s unit @0x%x: %s - %s"
6051 ", %d global, %d static syms\n",
6052 per_cu->is_debug_types ? "type" : "comp",
6053 per_cu->offset.sect_off,
6054 paddress (gdbarch, pst->textlow),
6055 paddress (gdbarch, pst->texthigh),
6056 pst->n_global_syms, pst->n_static_syms);
6057 }
6058}
6059
6060/* Subroutine of dwarf2_build_psymtabs_hard to simplify it.
6061 Process compilation unit THIS_CU for a psymtab. */
6062
6063static void
6064process_psymtab_comp_unit (struct dwarf2_per_cu_data *this_cu,
b93601f3
TT
6065 int want_partial_unit,
6066 enum language pretend_language)
0018ea6f 6067{
b93601f3
TT
6068 struct process_psymtab_comp_unit_data info;
6069
0018ea6f
DE
6070 /* If this compilation unit was already read in, free the
6071 cached copy in order to read it in again. This is
6072 necessary because we skipped some symbols when we first
6073 read in the compilation unit (see load_partial_dies).
6074 This problem could be avoided, but the benefit is unclear. */
6075 if (this_cu->cu != NULL)
6076 free_one_cached_comp_unit (this_cu);
6077
6078 gdb_assert (! this_cu->is_debug_types);
b93601f3
TT
6079 info.want_partial_unit = want_partial_unit;
6080 info.pretend_language = pretend_language;
0018ea6f
DE
6081 init_cutu_and_read_dies (this_cu, NULL, 0, 0,
6082 process_psymtab_comp_unit_reader,
b93601f3 6083 &info);
0018ea6f
DE
6084
6085 /* Age out any secondary CUs. */
6086 age_cached_comp_units ();
6087}
f4dc4d17
DE
6088
6089/* Reader function for build_type_psymtabs. */
6090
6091static void
6092build_type_psymtabs_reader (const struct die_reader_specs *reader,
d521ce57 6093 const gdb_byte *info_ptr,
f4dc4d17
DE
6094 struct die_info *type_unit_die,
6095 int has_children,
6096 void *data)
6097{
6098 struct objfile *objfile = dwarf2_per_objfile->objfile;
6099 struct dwarf2_cu *cu = reader->cu;
6100 struct dwarf2_per_cu_data *per_cu = cu->per_cu;
0186c6a7 6101 struct signatured_type *sig_type;
f4dc4d17
DE
6102 struct type_unit_group *tu_group;
6103 struct attribute *attr;
6104 struct partial_die_info *first_die;
6105 CORE_ADDR lowpc, highpc;
6106 struct partial_symtab *pst;
6107
6108 gdb_assert (data == NULL);
0186c6a7
DE
6109 gdb_assert (per_cu->is_debug_types);
6110 sig_type = (struct signatured_type *) per_cu;
f4dc4d17
DE
6111
6112 if (! has_children)
6113 return;
6114
6115 attr = dwarf2_attr_no_follow (type_unit_die, DW_AT_stmt_list);
094b34ac 6116 tu_group = get_type_unit_group (cu, attr);
f4dc4d17 6117
0186c6a7 6118 VEC_safe_push (sig_type_ptr, tu_group->tus, sig_type);
f4dc4d17
DE
6119
6120 prepare_one_comp_unit (cu, type_unit_die, language_minimal);
6121 cu->list_in_scope = &file_symbols;
6122 pst = create_partial_symtab (per_cu, "");
6123 pst->anonymous = 1;
6124
6125 first_die = load_partial_dies (reader, info_ptr, 1);
6126
6127 lowpc = (CORE_ADDR) -1;
6128 highpc = (CORE_ADDR) 0;
6129 scan_partial_symbols (first_die, &lowpc, &highpc, 0, cu);
6130
6131 pst->n_global_syms = objfile->global_psymbols.next -
6132 (objfile->global_psymbols.list + pst->globals_offset);
6133 pst->n_static_syms = objfile->static_psymbols.next -
6134 (objfile->static_psymbols.list + pst->statics_offset);
5c80ed9d 6135 sort_pst_symbols (objfile, pst);
f4dc4d17
DE
6136}
6137
6138/* Traversal function for build_type_psymtabs. */
6139
6140static int
6141build_type_psymtab_dependencies (void **slot, void *info)
6142{
6143 struct objfile *objfile = dwarf2_per_objfile->objfile;
6144 struct type_unit_group *tu_group = (struct type_unit_group *) *slot;
094b34ac 6145 struct dwarf2_per_cu_data *per_cu = &tu_group->per_cu;
f4dc4d17 6146 struct partial_symtab *pst = per_cu->v.psymtab;
0186c6a7
DE
6147 int len = VEC_length (sig_type_ptr, tu_group->tus);
6148 struct signatured_type *iter;
f4dc4d17
DE
6149 int i;
6150
6151 gdb_assert (len > 0);
0186c6a7 6152 gdb_assert (IS_TYPE_UNIT_GROUP (per_cu));
f4dc4d17
DE
6153
6154 pst->number_of_dependencies = len;
6155 pst->dependencies = obstack_alloc (&objfile->objfile_obstack,
6156 len * sizeof (struct psymtab *));
6157 for (i = 0;
0186c6a7 6158 VEC_iterate (sig_type_ptr, tu_group->tus, i, iter);
f4dc4d17
DE
6159 ++i)
6160 {
0186c6a7
DE
6161 gdb_assert (iter->per_cu.is_debug_types);
6162 pst->dependencies[i] = iter->per_cu.v.psymtab;
796a7ff8 6163 iter->type_unit_group = tu_group;
f4dc4d17
DE
6164 }
6165
0186c6a7 6166 VEC_free (sig_type_ptr, tu_group->tus);
348e048f
DE
6167
6168 return 1;
6169}
6170
6171/* Subroutine of dwarf2_build_psymtabs_hard to simplify it.
6172 Build partial symbol tables for the .debug_types comp-units. */
6173
6174static void
6175build_type_psymtabs (struct objfile *objfile)
6176{
0e50663e 6177 if (! create_all_type_units (objfile))
348e048f
DE
6178 return;
6179
f4dc4d17
DE
6180 build_type_unit_groups (build_type_psymtabs_reader, NULL);
6181
6182 /* Now that all TUs have been processed we can fill in the dependencies. */
6183 htab_traverse_noresize (dwarf2_per_objfile->type_unit_groups,
6184 build_type_psymtab_dependencies, NULL);
348e048f
DE
6185}
6186
60606b2c
TT
6187/* A cleanup function that clears objfile's psymtabs_addrmap field. */
6188
6189static void
6190psymtabs_addrmap_cleanup (void *o)
6191{
6192 struct objfile *objfile = o;
ec61707d 6193
60606b2c
TT
6194 objfile->psymtabs_addrmap = NULL;
6195}
6196
95554aad
TT
6197/* Compute the 'user' field for each psymtab in OBJFILE. */
6198
6199static void
6200set_partial_user (struct objfile *objfile)
6201{
6202 int i;
6203
6204 for (i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
6205 {
6206 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
6207 struct partial_symtab *pst = per_cu->v.psymtab;
6208 int j;
6209
36586728
TT
6210 if (pst == NULL)
6211 continue;
6212
95554aad
TT
6213 for (j = 0; j < pst->number_of_dependencies; ++j)
6214 {
6215 /* Set the 'user' field only if it is not already set. */
6216 if (pst->dependencies[j]->user == NULL)
6217 pst->dependencies[j]->user = pst;
6218 }
6219 }
6220}
6221
93311388
DE
6222/* Build the partial symbol table by doing a quick pass through the
6223 .debug_info and .debug_abbrev sections. */
72bf9492 6224
93311388 6225static void
c67a9c90 6226dwarf2_build_psymtabs_hard (struct objfile *objfile)
93311388 6227{
60606b2c
TT
6228 struct cleanup *back_to, *addrmap_cleanup;
6229 struct obstack temp_obstack;
21b2bd31 6230 int i;
93311388 6231
45cfd468
DE
6232 if (dwarf2_read_debug)
6233 {
6234 fprintf_unfiltered (gdb_stdlog, "Building psymtabs of objfile %s ...\n",
4262abfb 6235 objfile_name (objfile));
45cfd468
DE
6236 }
6237
98bfdba5
PA
6238 dwarf2_per_objfile->reading_partial_symbols = 1;
6239
be391dca 6240 dwarf2_read_section (objfile, &dwarf2_per_objfile->info);
91c24f0a 6241
93311388
DE
6242 /* Any cached compilation units will be linked by the per-objfile
6243 read_in_chain. Make sure to free them when we're done. */
6244 back_to = make_cleanup (free_cached_comp_units, NULL);
72bf9492 6245
348e048f
DE
6246 build_type_psymtabs (objfile);
6247
93311388 6248 create_all_comp_units (objfile);
c906108c 6249
60606b2c
TT
6250 /* Create a temporary address map on a temporary obstack. We later
6251 copy this to the final obstack. */
6252 obstack_init (&temp_obstack);
6253 make_cleanup_obstack_free (&temp_obstack);
6254 objfile->psymtabs_addrmap = addrmap_create_mutable (&temp_obstack);
6255 addrmap_cleanup = make_cleanup (psymtabs_addrmap_cleanup, objfile);
72bf9492 6256
21b2bd31 6257 for (i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
93311388 6258 {
21b2bd31 6259 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
aaa75496 6260
b93601f3 6261 process_psymtab_comp_unit (per_cu, 0, language_minimal);
c906108c 6262 }
ff013f42 6263
95554aad
TT
6264 set_partial_user (objfile);
6265
ff013f42
JK
6266 objfile->psymtabs_addrmap = addrmap_create_fixed (objfile->psymtabs_addrmap,
6267 &objfile->objfile_obstack);
60606b2c 6268 discard_cleanups (addrmap_cleanup);
ff013f42 6269
ae038cb0 6270 do_cleanups (back_to);
45cfd468
DE
6271
6272 if (dwarf2_read_debug)
6273 fprintf_unfiltered (gdb_stdlog, "Done building psymtabs of %s\n",
4262abfb 6274 objfile_name (objfile));
ae038cb0
DJ
6275}
6276
3019eac3 6277/* die_reader_func for load_partial_comp_unit. */
ae038cb0
DJ
6278
6279static void
dee91e82 6280load_partial_comp_unit_reader (const struct die_reader_specs *reader,
d521ce57 6281 const gdb_byte *info_ptr,
dee91e82
DE
6282 struct die_info *comp_unit_die,
6283 int has_children,
6284 void *data)
ae038cb0 6285{
dee91e82 6286 struct dwarf2_cu *cu = reader->cu;
ae038cb0 6287
95554aad 6288 prepare_one_comp_unit (cu, comp_unit_die, language_minimal);
ae038cb0 6289
ae038cb0
DJ
6290 /* Check if comp unit has_children.
6291 If so, read the rest of the partial symbols from this comp unit.
0963b4bd 6292 If not, there's no more debug_info for this comp unit. */
d85a05f0 6293 if (has_children)
dee91e82
DE
6294 load_partial_dies (reader, info_ptr, 0);
6295}
98bfdba5 6296
dee91e82
DE
6297/* Load the partial DIEs for a secondary CU into memory.
6298 This is also used when rereading a primary CU with load_all_dies. */
c5b7e1cb 6299
dee91e82
DE
6300static void
6301load_partial_comp_unit (struct dwarf2_per_cu_data *this_cu)
6302{
f4dc4d17
DE
6303 init_cutu_and_read_dies (this_cu, NULL, 1, 1,
6304 load_partial_comp_unit_reader, NULL);
ae038cb0
DJ
6305}
6306
ae038cb0 6307static void
36586728
TT
6308read_comp_units_from_section (struct objfile *objfile,
6309 struct dwarf2_section_info *section,
6310 unsigned int is_dwz,
6311 int *n_allocated,
6312 int *n_comp_units,
6313 struct dwarf2_per_cu_data ***all_comp_units)
ae038cb0 6314{
d521ce57 6315 const gdb_byte *info_ptr;
a32a8923 6316 bfd *abfd = get_section_bfd_owner (section);
be391dca 6317
bf6af496
DE
6318 if (dwarf2_read_debug)
6319 fprintf_unfiltered (gdb_stdlog, "Reading %s for %s\n",
a32a8923
DE
6320 get_section_name (section),
6321 get_section_file_name (section));
bf6af496 6322
36586728 6323 dwarf2_read_section (objfile, section);
ae038cb0 6324
36586728 6325 info_ptr = section->buffer;
6e70227d 6326
36586728 6327 while (info_ptr < section->buffer + section->size)
ae038cb0 6328 {
c764a876 6329 unsigned int length, initial_length_size;
ae038cb0 6330 struct dwarf2_per_cu_data *this_cu;
b64f50a1 6331 sect_offset offset;
ae038cb0 6332
36586728 6333 offset.sect_off = info_ptr - section->buffer;
ae038cb0
DJ
6334
6335 /* Read just enough information to find out where the next
6336 compilation unit is. */
36586728 6337 length = read_initial_length (abfd, info_ptr, &initial_length_size);
ae038cb0
DJ
6338
6339 /* Save the compilation unit for later lookup. */
6340 this_cu = obstack_alloc (&objfile->objfile_obstack,
6341 sizeof (struct dwarf2_per_cu_data));
6342 memset (this_cu, 0, sizeof (*this_cu));
6343 this_cu->offset = offset;
c764a876 6344 this_cu->length = length + initial_length_size;
36586728 6345 this_cu->is_dwz = is_dwz;
9291a0cd 6346 this_cu->objfile = objfile;
8a0459fd 6347 this_cu->section = section;
ae038cb0 6348
36586728 6349 if (*n_comp_units == *n_allocated)
ae038cb0 6350 {
36586728
TT
6351 *n_allocated *= 2;
6352 *all_comp_units = xrealloc (*all_comp_units,
6353 *n_allocated
6354 * sizeof (struct dwarf2_per_cu_data *));
ae038cb0 6355 }
36586728
TT
6356 (*all_comp_units)[*n_comp_units] = this_cu;
6357 ++*n_comp_units;
ae038cb0
DJ
6358
6359 info_ptr = info_ptr + this_cu->length;
6360 }
36586728
TT
6361}
6362
6363/* Create a list of all compilation units in OBJFILE.
6364 This is only done for -readnow and building partial symtabs. */
6365
6366static void
6367create_all_comp_units (struct objfile *objfile)
6368{
6369 int n_allocated;
6370 int n_comp_units;
6371 struct dwarf2_per_cu_data **all_comp_units;
4db1a1dc 6372 struct dwz_file *dwz;
36586728
TT
6373
6374 n_comp_units = 0;
6375 n_allocated = 10;
6376 all_comp_units = xmalloc (n_allocated
6377 * sizeof (struct dwarf2_per_cu_data *));
6378
6379 read_comp_units_from_section (objfile, &dwarf2_per_objfile->info, 0,
6380 &n_allocated, &n_comp_units, &all_comp_units);
6381
4db1a1dc
TT
6382 dwz = dwarf2_get_dwz_file ();
6383 if (dwz != NULL)
6384 read_comp_units_from_section (objfile, &dwz->info, 1,
6385 &n_allocated, &n_comp_units,
6386 &all_comp_units);
ae038cb0
DJ
6387
6388 dwarf2_per_objfile->all_comp_units
6389 = obstack_alloc (&objfile->objfile_obstack,
6390 n_comp_units * sizeof (struct dwarf2_per_cu_data *));
6391 memcpy (dwarf2_per_objfile->all_comp_units, all_comp_units,
6392 n_comp_units * sizeof (struct dwarf2_per_cu_data *));
6393 xfree (all_comp_units);
6394 dwarf2_per_objfile->n_comp_units = n_comp_units;
c906108c
SS
6395}
6396
5734ee8b
DJ
6397/* Process all loaded DIEs for compilation unit CU, starting at
6398 FIRST_DIE. The caller should pass NEED_PC == 1 if the compilation
6399 unit DIE did not have PC info (DW_AT_low_pc and DW_AT_high_pc, or
6400 DW_AT_ranges). If NEED_PC is set, then this function will set
6401 *LOWPC and *HIGHPC to the lowest and highest PC values found in CU
6402 and record the covered ranges in the addrmap. */
c906108c 6403
72bf9492
DJ
6404static void
6405scan_partial_symbols (struct partial_die_info *first_die, CORE_ADDR *lowpc,
5734ee8b 6406 CORE_ADDR *highpc, int need_pc, struct dwarf2_cu *cu)
c906108c 6407{
72bf9492 6408 struct partial_die_info *pdi;
c906108c 6409
91c24f0a
DC
6410 /* Now, march along the PDI's, descending into ones which have
6411 interesting children but skipping the children of the other ones,
6412 until we reach the end of the compilation unit. */
c906108c 6413
72bf9492 6414 pdi = first_die;
91c24f0a 6415
72bf9492
DJ
6416 while (pdi != NULL)
6417 {
6418 fixup_partial_die (pdi, cu);
c906108c 6419
f55ee35c 6420 /* Anonymous namespaces or modules have no name but have interesting
91c24f0a
DC
6421 children, so we need to look at them. Ditto for anonymous
6422 enums. */
933c6fe4 6423
72bf9492 6424 if (pdi->name != NULL || pdi->tag == DW_TAG_namespace
95554aad
TT
6425 || pdi->tag == DW_TAG_module || pdi->tag == DW_TAG_enumeration_type
6426 || pdi->tag == DW_TAG_imported_unit)
c906108c 6427 {
72bf9492 6428 switch (pdi->tag)
c906108c
SS
6429 {
6430 case DW_TAG_subprogram:
5734ee8b 6431 add_partial_subprogram (pdi, lowpc, highpc, need_pc, cu);
c906108c 6432 break;
72929c62 6433 case DW_TAG_constant:
c906108c
SS
6434 case DW_TAG_variable:
6435 case DW_TAG_typedef:
91c24f0a 6436 case DW_TAG_union_type:
72bf9492 6437 if (!pdi->is_declaration)
63d06c5c 6438 {
72bf9492 6439 add_partial_symbol (pdi, cu);
63d06c5c
DC
6440 }
6441 break;
c906108c 6442 case DW_TAG_class_type:
680b30c7 6443 case DW_TAG_interface_type:
c906108c 6444 case DW_TAG_structure_type:
72bf9492 6445 if (!pdi->is_declaration)
c906108c 6446 {
72bf9492 6447 add_partial_symbol (pdi, cu);
c906108c
SS
6448 }
6449 break;
91c24f0a 6450 case DW_TAG_enumeration_type:
72bf9492
DJ
6451 if (!pdi->is_declaration)
6452 add_partial_enumeration (pdi, cu);
c906108c
SS
6453 break;
6454 case DW_TAG_base_type:
a02abb62 6455 case DW_TAG_subrange_type:
c906108c 6456 /* File scope base type definitions are added to the partial
c5aa993b 6457 symbol table. */
72bf9492 6458 add_partial_symbol (pdi, cu);
c906108c 6459 break;
d9fa45fe 6460 case DW_TAG_namespace:
5734ee8b 6461 add_partial_namespace (pdi, lowpc, highpc, need_pc, cu);
91c24f0a 6462 break;
5d7cb8df
JK
6463 case DW_TAG_module:
6464 add_partial_module (pdi, lowpc, highpc, need_pc, cu);
6465 break;
95554aad
TT
6466 case DW_TAG_imported_unit:
6467 {
6468 struct dwarf2_per_cu_data *per_cu;
6469
f4dc4d17
DE
6470 /* For now we don't handle imported units in type units. */
6471 if (cu->per_cu->is_debug_types)
6472 {
6473 error (_("Dwarf Error: DW_TAG_imported_unit is not"
6474 " supported in type units [in module %s]"),
4262abfb 6475 objfile_name (cu->objfile));
f4dc4d17
DE
6476 }
6477
95554aad 6478 per_cu = dwarf2_find_containing_comp_unit (pdi->d.offset,
36586728 6479 pdi->is_dwz,
95554aad
TT
6480 cu->objfile);
6481
6482 /* Go read the partial unit, if needed. */
6483 if (per_cu->v.psymtab == NULL)
b93601f3 6484 process_psymtab_comp_unit (per_cu, 1, cu->language);
95554aad 6485
f4dc4d17 6486 VEC_safe_push (dwarf2_per_cu_ptr,
796a7ff8 6487 cu->per_cu->imported_symtabs, per_cu);
95554aad
TT
6488 }
6489 break;
c906108c
SS
6490 default:
6491 break;
6492 }
6493 }
6494
72bf9492
DJ
6495 /* If the die has a sibling, skip to the sibling. */
6496
6497 pdi = pdi->die_sibling;
6498 }
6499}
6500
6501/* Functions used to compute the fully scoped name of a partial DIE.
91c24f0a 6502
72bf9492 6503 Normally, this is simple. For C++, the parent DIE's fully scoped
987504bb
JJ
6504 name is concatenated with "::" and the partial DIE's name. For
6505 Java, the same thing occurs except that "." is used instead of "::".
72bf9492
DJ
6506 Enumerators are an exception; they use the scope of their parent
6507 enumeration type, i.e. the name of the enumeration type is not
6508 prepended to the enumerator.
91c24f0a 6509
72bf9492
DJ
6510 There are two complexities. One is DW_AT_specification; in this
6511 case "parent" means the parent of the target of the specification,
6512 instead of the direct parent of the DIE. The other is compilers
6513 which do not emit DW_TAG_namespace; in this case we try to guess
6514 the fully qualified name of structure types from their members'
6515 linkage names. This must be done using the DIE's children rather
6516 than the children of any DW_AT_specification target. We only need
6517 to do this for structures at the top level, i.e. if the target of
6518 any DW_AT_specification (if any; otherwise the DIE itself) does not
6519 have a parent. */
6520
6521/* Compute the scope prefix associated with PDI's parent, in
6522 compilation unit CU. The result will be allocated on CU's
6523 comp_unit_obstack, or a copy of the already allocated PDI->NAME
6524 field. NULL is returned if no prefix is necessary. */
15d034d0 6525static const char *
72bf9492
DJ
6526partial_die_parent_scope (struct partial_die_info *pdi,
6527 struct dwarf2_cu *cu)
6528{
15d034d0 6529 const char *grandparent_scope;
72bf9492 6530 struct partial_die_info *parent, *real_pdi;
91c24f0a 6531
72bf9492
DJ
6532 /* We need to look at our parent DIE; if we have a DW_AT_specification,
6533 then this means the parent of the specification DIE. */
6534
6535 real_pdi = pdi;
72bf9492 6536 while (real_pdi->has_specification)
36586728
TT
6537 real_pdi = find_partial_die (real_pdi->spec_offset,
6538 real_pdi->spec_is_dwz, cu);
72bf9492
DJ
6539
6540 parent = real_pdi->die_parent;
6541 if (parent == NULL)
6542 return NULL;
6543
6544 if (parent->scope_set)
6545 return parent->scope;
6546
6547 fixup_partial_die (parent, cu);
6548
10b3939b 6549 grandparent_scope = partial_die_parent_scope (parent, cu);
72bf9492 6550
acebe513
UW
6551 /* GCC 4.0 and 4.1 had a bug (PR c++/28460) where they generated bogus
6552 DW_TAG_namespace DIEs with a name of "::" for the global namespace.
6553 Work around this problem here. */
6554 if (cu->language == language_cplus
6e70227d 6555 && parent->tag == DW_TAG_namespace
acebe513
UW
6556 && strcmp (parent->name, "::") == 0
6557 && grandparent_scope == NULL)
6558 {
6559 parent->scope = NULL;
6560 parent->scope_set = 1;
6561 return NULL;
6562 }
6563
9c6c53f7
SA
6564 if (pdi->tag == DW_TAG_enumerator)
6565 /* Enumerators should not get the name of the enumeration as a prefix. */
6566 parent->scope = grandparent_scope;
6567 else if (parent->tag == DW_TAG_namespace
f55ee35c 6568 || parent->tag == DW_TAG_module
72bf9492
DJ
6569 || parent->tag == DW_TAG_structure_type
6570 || parent->tag == DW_TAG_class_type
680b30c7 6571 || parent->tag == DW_TAG_interface_type
ceeb3d5a
TT
6572 || parent->tag == DW_TAG_union_type
6573 || parent->tag == DW_TAG_enumeration_type)
72bf9492
DJ
6574 {
6575 if (grandparent_scope == NULL)
6576 parent->scope = parent->name;
6577 else
3e43a32a
MS
6578 parent->scope = typename_concat (&cu->comp_unit_obstack,
6579 grandparent_scope,
f55ee35c 6580 parent->name, 0, cu);
72bf9492 6581 }
72bf9492
DJ
6582 else
6583 {
6584 /* FIXME drow/2004-04-01: What should we be doing with
6585 function-local names? For partial symbols, we should probably be
6586 ignoring them. */
6587 complaint (&symfile_complaints,
e2e0b3e5 6588 _("unhandled containing DIE tag %d for DIE at %d"),
b64f50a1 6589 parent->tag, pdi->offset.sect_off);
72bf9492 6590 parent->scope = grandparent_scope;
c906108c
SS
6591 }
6592
72bf9492
DJ
6593 parent->scope_set = 1;
6594 return parent->scope;
6595}
6596
6597/* Return the fully scoped name associated with PDI, from compilation unit
6598 CU. The result will be allocated with malloc. */
4568ecf9 6599
72bf9492
DJ
6600static char *
6601partial_die_full_name (struct partial_die_info *pdi,
6602 struct dwarf2_cu *cu)
6603{
15d034d0 6604 const char *parent_scope;
72bf9492 6605
98bfdba5
PA
6606 /* If this is a template instantiation, we can not work out the
6607 template arguments from partial DIEs. So, unfortunately, we have
6608 to go through the full DIEs. At least any work we do building
6609 types here will be reused if full symbols are loaded later. */
6610 if (pdi->has_template_arguments)
6611 {
6612 fixup_partial_die (pdi, cu);
6613
6614 if (pdi->name != NULL && strchr (pdi->name, '<') == NULL)
6615 {
6616 struct die_info *die;
6617 struct attribute attr;
6618 struct dwarf2_cu *ref_cu = cu;
6619
b64f50a1 6620 /* DW_FORM_ref_addr is using section offset. */
98bfdba5
PA
6621 attr.name = 0;
6622 attr.form = DW_FORM_ref_addr;
4568ecf9 6623 attr.u.unsnd = pdi->offset.sect_off;
98bfdba5
PA
6624 die = follow_die_ref (NULL, &attr, &ref_cu);
6625
6626 return xstrdup (dwarf2_full_name (NULL, die, ref_cu));
6627 }
6628 }
6629
72bf9492
DJ
6630 parent_scope = partial_die_parent_scope (pdi, cu);
6631 if (parent_scope == NULL)
6632 return NULL;
6633 else
f55ee35c 6634 return typename_concat (NULL, parent_scope, pdi->name, 0, cu);
c906108c
SS
6635}
6636
6637static void
72bf9492 6638add_partial_symbol (struct partial_die_info *pdi, struct dwarf2_cu *cu)
c906108c 6639{
e7c27a73 6640 struct objfile *objfile = cu->objfile;
c906108c 6641 CORE_ADDR addr = 0;
15d034d0 6642 const char *actual_name = NULL;
e142c38c 6643 CORE_ADDR baseaddr;
15d034d0 6644 char *built_actual_name;
e142c38c
DJ
6645
6646 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
c906108c 6647
15d034d0
TT
6648 built_actual_name = partial_die_full_name (pdi, cu);
6649 if (built_actual_name != NULL)
6650 actual_name = built_actual_name;
63d06c5c 6651
72bf9492
DJ
6652 if (actual_name == NULL)
6653 actual_name = pdi->name;
6654
c906108c
SS
6655 switch (pdi->tag)
6656 {
6657 case DW_TAG_subprogram:
2cfa0c8d 6658 if (pdi->is_external || cu->language == language_ada)
c906108c 6659 {
2cfa0c8d
JB
6660 /* brobecker/2007-12-26: Normally, only "external" DIEs are part
6661 of the global scope. But in Ada, we want to be able to access
6662 nested procedures globally. So all Ada subprograms are stored
6663 in the global scope. */
f47fb265 6664 /* prim_record_minimal_symbol (actual_name, pdi->lowpc + baseaddr,
c5aa993b 6665 mst_text, objfile); */
f47fb265 6666 add_psymbol_to_list (actual_name, strlen (actual_name),
15d034d0 6667 built_actual_name != NULL,
f47fb265
MS
6668 VAR_DOMAIN, LOC_BLOCK,
6669 &objfile->global_psymbols,
6670 0, pdi->lowpc + baseaddr,
6671 cu->language, objfile);
c906108c
SS
6672 }
6673 else
6674 {
f47fb265 6675 /* prim_record_minimal_symbol (actual_name, pdi->lowpc + baseaddr,
c5aa993b 6676 mst_file_text, objfile); */
f47fb265 6677 add_psymbol_to_list (actual_name, strlen (actual_name),
15d034d0 6678 built_actual_name != NULL,
f47fb265
MS
6679 VAR_DOMAIN, LOC_BLOCK,
6680 &objfile->static_psymbols,
6681 0, pdi->lowpc + baseaddr,
6682 cu->language, objfile);
c906108c
SS
6683 }
6684 break;
72929c62
JB
6685 case DW_TAG_constant:
6686 {
6687 struct psymbol_allocation_list *list;
6688
6689 if (pdi->is_external)
6690 list = &objfile->global_psymbols;
6691 else
6692 list = &objfile->static_psymbols;
f47fb265 6693 add_psymbol_to_list (actual_name, strlen (actual_name),
15d034d0 6694 built_actual_name != NULL, VAR_DOMAIN, LOC_STATIC,
f47fb265 6695 list, 0, 0, cu->language, objfile);
72929c62
JB
6696 }
6697 break;
c906108c 6698 case DW_TAG_variable:
95554aad
TT
6699 if (pdi->d.locdesc)
6700 addr = decode_locdesc (pdi->d.locdesc, cu);
caac4577 6701
95554aad 6702 if (pdi->d.locdesc
caac4577
JG
6703 && addr == 0
6704 && !dwarf2_per_objfile->has_section_at_zero)
6705 {
6706 /* A global or static variable may also have been stripped
6707 out by the linker if unused, in which case its address
6708 will be nullified; do not add such variables into partial
6709 symbol table then. */
6710 }
6711 else if (pdi->is_external)
c906108c
SS
6712 {
6713 /* Global Variable.
6714 Don't enter into the minimal symbol tables as there is
6715 a minimal symbol table entry from the ELF symbols already.
6716 Enter into partial symbol table if it has a location
6717 descriptor or a type.
6718 If the location descriptor is missing, new_symbol will create
6719 a LOC_UNRESOLVED symbol, the address of the variable will then
6720 be determined from the minimal symbol table whenever the variable
6721 is referenced.
6722 The address for the partial symbol table entry is not
6723 used by GDB, but it comes in handy for debugging partial symbol
6724 table building. */
6725
95554aad 6726 if (pdi->d.locdesc || pdi->has_type)
f47fb265 6727 add_psymbol_to_list (actual_name, strlen (actual_name),
15d034d0 6728 built_actual_name != NULL,
f47fb265
MS
6729 VAR_DOMAIN, LOC_STATIC,
6730 &objfile->global_psymbols,
6731 0, addr + baseaddr,
6732 cu->language, objfile);
c906108c
SS
6733 }
6734 else
6735 {
0963b4bd 6736 /* Static Variable. Skip symbols without location descriptors. */
95554aad 6737 if (pdi->d.locdesc == NULL)
decbce07 6738 {
15d034d0 6739 xfree (built_actual_name);
decbce07
MS
6740 return;
6741 }
f47fb265 6742 /* prim_record_minimal_symbol (actual_name, addr + baseaddr,
c5aa993b 6743 mst_file_data, objfile); */
f47fb265 6744 add_psymbol_to_list (actual_name, strlen (actual_name),
15d034d0 6745 built_actual_name != NULL,
f47fb265
MS
6746 VAR_DOMAIN, LOC_STATIC,
6747 &objfile->static_psymbols,
6748 0, addr + baseaddr,
6749 cu->language, objfile);
c906108c
SS
6750 }
6751 break;
6752 case DW_TAG_typedef:
6753 case DW_TAG_base_type:
a02abb62 6754 case DW_TAG_subrange_type:
38d518c9 6755 add_psymbol_to_list (actual_name, strlen (actual_name),
15d034d0 6756 built_actual_name != NULL,
176620f1 6757 VAR_DOMAIN, LOC_TYPEDEF,
c906108c 6758 &objfile->static_psymbols,
e142c38c 6759 0, (CORE_ADDR) 0, cu->language, objfile);
c906108c 6760 break;
72bf9492
DJ
6761 case DW_TAG_namespace:
6762 add_psymbol_to_list (actual_name, strlen (actual_name),
15d034d0 6763 built_actual_name != NULL,
72bf9492
DJ
6764 VAR_DOMAIN, LOC_TYPEDEF,
6765 &objfile->global_psymbols,
6766 0, (CORE_ADDR) 0, cu->language, objfile);
6767 break;
c906108c 6768 case DW_TAG_class_type:
680b30c7 6769 case DW_TAG_interface_type:
c906108c
SS
6770 case DW_TAG_structure_type:
6771 case DW_TAG_union_type:
6772 case DW_TAG_enumeration_type:
fa4028e9
JB
6773 /* Skip external references. The DWARF standard says in the section
6774 about "Structure, Union, and Class Type Entries": "An incomplete
6775 structure, union or class type is represented by a structure,
6776 union or class entry that does not have a byte size attribute
6777 and that has a DW_AT_declaration attribute." */
6778 if (!pdi->has_byte_size && pdi->is_declaration)
decbce07 6779 {
15d034d0 6780 xfree (built_actual_name);
decbce07
MS
6781 return;
6782 }
fa4028e9 6783
63d06c5c
DC
6784 /* NOTE: carlton/2003-10-07: See comment in new_symbol about
6785 static vs. global. */
38d518c9 6786 add_psymbol_to_list (actual_name, strlen (actual_name),
15d034d0 6787 built_actual_name != NULL,
176620f1 6788 STRUCT_DOMAIN, LOC_TYPEDEF,
987504bb
JJ
6789 (cu->language == language_cplus
6790 || cu->language == language_java)
63d06c5c
DC
6791 ? &objfile->global_psymbols
6792 : &objfile->static_psymbols,
e142c38c 6793 0, (CORE_ADDR) 0, cu->language, objfile);
c906108c 6794
c906108c
SS
6795 break;
6796 case DW_TAG_enumerator:
38d518c9 6797 add_psymbol_to_list (actual_name, strlen (actual_name),
15d034d0 6798 built_actual_name != NULL,
176620f1 6799 VAR_DOMAIN, LOC_CONST,
987504bb
JJ
6800 (cu->language == language_cplus
6801 || cu->language == language_java)
f6fe98ef
DJ
6802 ? &objfile->global_psymbols
6803 : &objfile->static_psymbols,
e142c38c 6804 0, (CORE_ADDR) 0, cu->language, objfile);
c906108c
SS
6805 break;
6806 default:
6807 break;
6808 }
5c4e30ca 6809
15d034d0 6810 xfree (built_actual_name);
c906108c
SS
6811}
6812
5c4e30ca
DC
6813/* Read a partial die corresponding to a namespace; also, add a symbol
6814 corresponding to that namespace to the symbol table. NAMESPACE is
6815 the name of the enclosing namespace. */
91c24f0a 6816
72bf9492
DJ
6817static void
6818add_partial_namespace (struct partial_die_info *pdi,
91c24f0a 6819 CORE_ADDR *lowpc, CORE_ADDR *highpc,
5734ee8b 6820 int need_pc, struct dwarf2_cu *cu)
91c24f0a 6821{
72bf9492 6822 /* Add a symbol for the namespace. */
e7c27a73 6823
72bf9492 6824 add_partial_symbol (pdi, cu);
5c4e30ca
DC
6825
6826 /* Now scan partial symbols in that namespace. */
6827
91c24f0a 6828 if (pdi->has_children)
5734ee8b 6829 scan_partial_symbols (pdi->die_child, lowpc, highpc, need_pc, cu);
91c24f0a
DC
6830}
6831
5d7cb8df
JK
6832/* Read a partial die corresponding to a Fortran module. */
6833
6834static void
6835add_partial_module (struct partial_die_info *pdi, CORE_ADDR *lowpc,
6836 CORE_ADDR *highpc, int need_pc, struct dwarf2_cu *cu)
6837{
f55ee35c 6838 /* Now scan partial symbols in that module. */
5d7cb8df
JK
6839
6840 if (pdi->has_children)
6841 scan_partial_symbols (pdi->die_child, lowpc, highpc, need_pc, cu);
6842}
6843
bc30ff58
JB
6844/* Read a partial die corresponding to a subprogram and create a partial
6845 symbol for that subprogram. When the CU language allows it, this
6846 routine also defines a partial symbol for each nested subprogram
6847 that this subprogram contains.
6e70227d 6848
bc30ff58
JB
6849 DIE my also be a lexical block, in which case we simply search
6850 recursively for suprograms defined inside that lexical block.
6851 Again, this is only performed when the CU language allows this
6852 type of definitions. */
6853
6854static void
6855add_partial_subprogram (struct partial_die_info *pdi,
6856 CORE_ADDR *lowpc, CORE_ADDR *highpc,
5734ee8b 6857 int need_pc, struct dwarf2_cu *cu)
bc30ff58
JB
6858{
6859 if (pdi->tag == DW_TAG_subprogram)
6860 {
6861 if (pdi->has_pc_info)
6862 {
6863 if (pdi->lowpc < *lowpc)
6864 *lowpc = pdi->lowpc;
6865 if (pdi->highpc > *highpc)
6866 *highpc = pdi->highpc;
5734ee8b
DJ
6867 if (need_pc)
6868 {
6869 CORE_ADDR baseaddr;
6870 struct objfile *objfile = cu->objfile;
6871
6872 baseaddr = ANOFFSET (objfile->section_offsets,
6873 SECT_OFF_TEXT (objfile));
6874 addrmap_set_empty (objfile->psymtabs_addrmap,
01637564
DE
6875 pdi->lowpc + baseaddr,
6876 pdi->highpc - 1 + baseaddr,
9291a0cd 6877 cu->per_cu->v.psymtab);
5734ee8b 6878 }
481860b3
GB
6879 }
6880
6881 if (pdi->has_pc_info || (!pdi->is_external && pdi->may_be_inlined))
6882 {
bc30ff58 6883 if (!pdi->is_declaration)
e8d05480
JB
6884 /* Ignore subprogram DIEs that do not have a name, they are
6885 illegal. Do not emit a complaint at this point, we will
6886 do so when we convert this psymtab into a symtab. */
6887 if (pdi->name)
6888 add_partial_symbol (pdi, cu);
bc30ff58
JB
6889 }
6890 }
6e70227d 6891
bc30ff58
JB
6892 if (! pdi->has_children)
6893 return;
6894
6895 if (cu->language == language_ada)
6896 {
6897 pdi = pdi->die_child;
6898 while (pdi != NULL)
6899 {
6900 fixup_partial_die (pdi, cu);
6901 if (pdi->tag == DW_TAG_subprogram
6902 || pdi->tag == DW_TAG_lexical_block)
5734ee8b 6903 add_partial_subprogram (pdi, lowpc, highpc, need_pc, cu);
bc30ff58
JB
6904 pdi = pdi->die_sibling;
6905 }
6906 }
6907}
6908
91c24f0a
DC
6909/* Read a partial die corresponding to an enumeration type. */
6910
72bf9492
DJ
6911static void
6912add_partial_enumeration (struct partial_die_info *enum_pdi,
6913 struct dwarf2_cu *cu)
91c24f0a 6914{
72bf9492 6915 struct partial_die_info *pdi;
91c24f0a
DC
6916
6917 if (enum_pdi->name != NULL)
72bf9492
DJ
6918 add_partial_symbol (enum_pdi, cu);
6919
6920 pdi = enum_pdi->die_child;
6921 while (pdi)
91c24f0a 6922 {
72bf9492 6923 if (pdi->tag != DW_TAG_enumerator || pdi->name == NULL)
e2e0b3e5 6924 complaint (&symfile_complaints, _("malformed enumerator DIE ignored"));
91c24f0a 6925 else
72bf9492
DJ
6926 add_partial_symbol (pdi, cu);
6927 pdi = pdi->die_sibling;
91c24f0a 6928 }
91c24f0a
DC
6929}
6930
6caca83c
CC
6931/* Return the initial uleb128 in the die at INFO_PTR. */
6932
6933static unsigned int
d521ce57 6934peek_abbrev_code (bfd *abfd, const gdb_byte *info_ptr)
6caca83c
CC
6935{
6936 unsigned int bytes_read;
6937
6938 return read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
6939}
6940
4bb7a0a7
DJ
6941/* Read the initial uleb128 in the die at INFO_PTR in compilation unit CU.
6942 Return the corresponding abbrev, or NULL if the number is zero (indicating
6943 an empty DIE). In either case *BYTES_READ will be set to the length of
6944 the initial number. */
6945
6946static struct abbrev_info *
d521ce57 6947peek_die_abbrev (const gdb_byte *info_ptr, unsigned int *bytes_read,
891d2f0b 6948 struct dwarf2_cu *cu)
4bb7a0a7
DJ
6949{
6950 bfd *abfd = cu->objfile->obfd;
6951 unsigned int abbrev_number;
6952 struct abbrev_info *abbrev;
6953
6954 abbrev_number = read_unsigned_leb128 (abfd, info_ptr, bytes_read);
6955
6956 if (abbrev_number == 0)
6957 return NULL;
6958
433df2d4 6959 abbrev = abbrev_table_lookup_abbrev (cu->abbrev_table, abbrev_number);
4bb7a0a7
DJ
6960 if (!abbrev)
6961 {
3e43a32a
MS
6962 error (_("Dwarf Error: Could not find abbrev number %d [in module %s]"),
6963 abbrev_number, bfd_get_filename (abfd));
4bb7a0a7
DJ
6964 }
6965
6966 return abbrev;
6967}
6968
93311388
DE
6969/* Scan the debug information for CU starting at INFO_PTR in buffer BUFFER.
6970 Returns a pointer to the end of a series of DIEs, terminated by an empty
4bb7a0a7
DJ
6971 DIE. Any children of the skipped DIEs will also be skipped. */
6972
d521ce57
TT
6973static const gdb_byte *
6974skip_children (const struct die_reader_specs *reader, const gdb_byte *info_ptr)
4bb7a0a7 6975{
dee91e82 6976 struct dwarf2_cu *cu = reader->cu;
4bb7a0a7
DJ
6977 struct abbrev_info *abbrev;
6978 unsigned int bytes_read;
6979
6980 while (1)
6981 {
6982 abbrev = peek_die_abbrev (info_ptr, &bytes_read, cu);
6983 if (abbrev == NULL)
6984 return info_ptr + bytes_read;
6985 else
dee91e82 6986 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
4bb7a0a7
DJ
6987 }
6988}
6989
93311388
DE
6990/* Scan the debug information for CU starting at INFO_PTR in buffer BUFFER.
6991 INFO_PTR should point just after the initial uleb128 of a DIE, and the
4bb7a0a7
DJ
6992 abbrev corresponding to that skipped uleb128 should be passed in
6993 ABBREV. Returns a pointer to this DIE's sibling, skipping any
6994 children. */
6995
d521ce57
TT
6996static const gdb_byte *
6997skip_one_die (const struct die_reader_specs *reader, const gdb_byte *info_ptr,
dee91e82 6998 struct abbrev_info *abbrev)
4bb7a0a7
DJ
6999{
7000 unsigned int bytes_read;
7001 struct attribute attr;
dee91e82
DE
7002 bfd *abfd = reader->abfd;
7003 struct dwarf2_cu *cu = reader->cu;
d521ce57 7004 const gdb_byte *buffer = reader->buffer;
f664829e 7005 const gdb_byte *buffer_end = reader->buffer_end;
d521ce57 7006 const gdb_byte *start_info_ptr = info_ptr;
4bb7a0a7
DJ
7007 unsigned int form, i;
7008
7009 for (i = 0; i < abbrev->num_attrs; i++)
7010 {
7011 /* The only abbrev we care about is DW_AT_sibling. */
7012 if (abbrev->attrs[i].name == DW_AT_sibling)
7013 {
dee91e82 7014 read_attribute (reader, &attr, &abbrev->attrs[i], info_ptr);
4bb7a0a7 7015 if (attr.form == DW_FORM_ref_addr)
3e43a32a
MS
7016 complaint (&symfile_complaints,
7017 _("ignoring absolute DW_AT_sibling"));
4bb7a0a7 7018 else
b9502d3f
WN
7019 {
7020 unsigned int off = dwarf2_get_ref_die_offset (&attr).sect_off;
7021 const gdb_byte *sibling_ptr = buffer + off;
7022
7023 if (sibling_ptr < info_ptr)
7024 complaint (&symfile_complaints,
7025 _("DW_AT_sibling points backwards"));
7026 else
7027 return sibling_ptr;
7028 }
4bb7a0a7
DJ
7029 }
7030
7031 /* If it isn't DW_AT_sibling, skip this attribute. */
7032 form = abbrev->attrs[i].form;
7033 skip_attribute:
7034 switch (form)
7035 {
4bb7a0a7 7036 case DW_FORM_ref_addr:
ae411497
TT
7037 /* In DWARF 2, DW_FORM_ref_addr is address sized; in DWARF 3
7038 and later it is offset sized. */
7039 if (cu->header.version == 2)
7040 info_ptr += cu->header.addr_size;
7041 else
7042 info_ptr += cu->header.offset_size;
7043 break;
36586728
TT
7044 case DW_FORM_GNU_ref_alt:
7045 info_ptr += cu->header.offset_size;
7046 break;
ae411497 7047 case DW_FORM_addr:
4bb7a0a7
DJ
7048 info_ptr += cu->header.addr_size;
7049 break;
7050 case DW_FORM_data1:
7051 case DW_FORM_ref1:
7052 case DW_FORM_flag:
7053 info_ptr += 1;
7054 break;
2dc7f7b3
TT
7055 case DW_FORM_flag_present:
7056 break;
4bb7a0a7
DJ
7057 case DW_FORM_data2:
7058 case DW_FORM_ref2:
7059 info_ptr += 2;
7060 break;
7061 case DW_FORM_data4:
7062 case DW_FORM_ref4:
7063 info_ptr += 4;
7064 break;
7065 case DW_FORM_data8:
7066 case DW_FORM_ref8:
55f1336d 7067 case DW_FORM_ref_sig8:
4bb7a0a7
DJ
7068 info_ptr += 8;
7069 break;
7070 case DW_FORM_string:
9b1c24c8 7071 read_direct_string (abfd, info_ptr, &bytes_read);
4bb7a0a7
DJ
7072 info_ptr += bytes_read;
7073 break;
2dc7f7b3 7074 case DW_FORM_sec_offset:
4bb7a0a7 7075 case DW_FORM_strp:
36586728 7076 case DW_FORM_GNU_strp_alt:
4bb7a0a7
DJ
7077 info_ptr += cu->header.offset_size;
7078 break;
2dc7f7b3 7079 case DW_FORM_exprloc:
4bb7a0a7
DJ
7080 case DW_FORM_block:
7081 info_ptr += read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
7082 info_ptr += bytes_read;
7083 break;
7084 case DW_FORM_block1:
7085 info_ptr += 1 + read_1_byte (abfd, info_ptr);
7086 break;
7087 case DW_FORM_block2:
7088 info_ptr += 2 + read_2_bytes (abfd, info_ptr);
7089 break;
7090 case DW_FORM_block4:
7091 info_ptr += 4 + read_4_bytes (abfd, info_ptr);
7092 break;
7093 case DW_FORM_sdata:
7094 case DW_FORM_udata:
7095 case DW_FORM_ref_udata:
3019eac3
DE
7096 case DW_FORM_GNU_addr_index:
7097 case DW_FORM_GNU_str_index:
d521ce57 7098 info_ptr = safe_skip_leb128 (info_ptr, buffer_end);
4bb7a0a7
DJ
7099 break;
7100 case DW_FORM_indirect:
7101 form = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
7102 info_ptr += bytes_read;
7103 /* We need to continue parsing from here, so just go back to
7104 the top. */
7105 goto skip_attribute;
7106
7107 default:
3e43a32a
MS
7108 error (_("Dwarf Error: Cannot handle %s "
7109 "in DWARF reader [in module %s]"),
4bb7a0a7
DJ
7110 dwarf_form_name (form),
7111 bfd_get_filename (abfd));
7112 }
7113 }
7114
7115 if (abbrev->has_children)
dee91e82 7116 return skip_children (reader, info_ptr);
4bb7a0a7
DJ
7117 else
7118 return info_ptr;
7119}
7120
93311388 7121/* Locate ORIG_PDI's sibling.
dee91e82 7122 INFO_PTR should point to the start of the next DIE after ORIG_PDI. */
91c24f0a 7123
d521ce57 7124static const gdb_byte *
dee91e82
DE
7125locate_pdi_sibling (const struct die_reader_specs *reader,
7126 struct partial_die_info *orig_pdi,
d521ce57 7127 const gdb_byte *info_ptr)
91c24f0a
DC
7128{
7129 /* Do we know the sibling already? */
72bf9492 7130
91c24f0a
DC
7131 if (orig_pdi->sibling)
7132 return orig_pdi->sibling;
7133
7134 /* Are there any children to deal with? */
7135
7136 if (!orig_pdi->has_children)
7137 return info_ptr;
7138
4bb7a0a7 7139 /* Skip the children the long way. */
91c24f0a 7140
dee91e82 7141 return skip_children (reader, info_ptr);
91c24f0a
DC
7142}
7143
257e7a09 7144/* Expand this partial symbol table into a full symbol table. SELF is
442e4d9c 7145 not NULL. */
c906108c
SS
7146
7147static void
257e7a09
YQ
7148dwarf2_read_symtab (struct partial_symtab *self,
7149 struct objfile *objfile)
c906108c 7150{
257e7a09 7151 if (self->readin)
c906108c 7152 {
442e4d9c 7153 warning (_("bug: psymtab for %s is already read in."),
257e7a09 7154 self->filename);
442e4d9c
YQ
7155 }
7156 else
7157 {
7158 if (info_verbose)
c906108c 7159 {
442e4d9c 7160 printf_filtered (_("Reading in symbols for %s..."),
257e7a09 7161 self->filename);
442e4d9c 7162 gdb_flush (gdb_stdout);
c906108c 7163 }
c906108c 7164
442e4d9c
YQ
7165 /* Restore our global data. */
7166 dwarf2_per_objfile = objfile_data (objfile, dwarf2_objfile_data_key);
10b3939b 7167
442e4d9c
YQ
7168 /* If this psymtab is constructed from a debug-only objfile, the
7169 has_section_at_zero flag will not necessarily be correct. We
7170 can get the correct value for this flag by looking at the data
7171 associated with the (presumably stripped) associated objfile. */
7172 if (objfile->separate_debug_objfile_backlink)
7173 {
7174 struct dwarf2_per_objfile *dpo_backlink
7175 = objfile_data (objfile->separate_debug_objfile_backlink,
7176 dwarf2_objfile_data_key);
9a619af0 7177
442e4d9c
YQ
7178 dwarf2_per_objfile->has_section_at_zero
7179 = dpo_backlink->has_section_at_zero;
7180 }
b2ab525c 7181
442e4d9c 7182 dwarf2_per_objfile->reading_partial_symbols = 0;
98bfdba5 7183
257e7a09 7184 psymtab_to_symtab_1 (self);
c906108c 7185
442e4d9c
YQ
7186 /* Finish up the debug error message. */
7187 if (info_verbose)
7188 printf_filtered (_("done.\n"));
c906108c 7189 }
95554aad
TT
7190
7191 process_cu_includes ();
c906108c 7192}
9cdd5dbd
DE
7193\f
7194/* Reading in full CUs. */
c906108c 7195
10b3939b
DJ
7196/* Add PER_CU to the queue. */
7197
7198static void
95554aad
TT
7199queue_comp_unit (struct dwarf2_per_cu_data *per_cu,
7200 enum language pretend_language)
10b3939b
DJ
7201{
7202 struct dwarf2_queue_item *item;
7203
7204 per_cu->queued = 1;
7205 item = xmalloc (sizeof (*item));
7206 item->per_cu = per_cu;
95554aad 7207 item->pretend_language = pretend_language;
10b3939b
DJ
7208 item->next = NULL;
7209
7210 if (dwarf2_queue == NULL)
7211 dwarf2_queue = item;
7212 else
7213 dwarf2_queue_tail->next = item;
7214
7215 dwarf2_queue_tail = item;
7216}
7217
89e63ee4
DE
7218/* If PER_CU is not yet queued, add it to the queue.
7219 If DEPENDENT_CU is non-NULL, it has a reference to PER_CU so add a
7220 dependency.
0907af0c 7221 The result is non-zero if PER_CU was queued, otherwise the result is zero
69d751e3
DE
7222 meaning either PER_CU is already queued or it is already loaded.
7223
7224 N.B. There is an invariant here that if a CU is queued then it is loaded.
7225 The caller is required to load PER_CU if we return non-zero. */
0907af0c
DE
7226
7227static int
89e63ee4 7228maybe_queue_comp_unit (struct dwarf2_cu *dependent_cu,
0907af0c
DE
7229 struct dwarf2_per_cu_data *per_cu,
7230 enum language pretend_language)
7231{
7232 /* We may arrive here during partial symbol reading, if we need full
7233 DIEs to process an unusual case (e.g. template arguments). Do
7234 not queue PER_CU, just tell our caller to load its DIEs. */
7235 if (dwarf2_per_objfile->reading_partial_symbols)
7236 {
7237 if (per_cu->cu == NULL || per_cu->cu->dies == NULL)
7238 return 1;
7239 return 0;
7240 }
7241
7242 /* Mark the dependence relation so that we don't flush PER_CU
7243 too early. */
89e63ee4
DE
7244 if (dependent_cu != NULL)
7245 dwarf2_add_dependence (dependent_cu, per_cu);
0907af0c
DE
7246
7247 /* If it's already on the queue, we have nothing to do. */
7248 if (per_cu->queued)
7249 return 0;
7250
7251 /* If the compilation unit is already loaded, just mark it as
7252 used. */
7253 if (per_cu->cu != NULL)
7254 {
7255 per_cu->cu->last_used = 0;
7256 return 0;
7257 }
7258
7259 /* Add it to the queue. */
7260 queue_comp_unit (per_cu, pretend_language);
7261
7262 return 1;
7263}
7264
10b3939b
DJ
7265/* Process the queue. */
7266
7267static void
a0f42c21 7268process_queue (void)
10b3939b
DJ
7269{
7270 struct dwarf2_queue_item *item, *next_item;
7271
45cfd468
DE
7272 if (dwarf2_read_debug)
7273 {
7274 fprintf_unfiltered (gdb_stdlog,
7275 "Expanding one or more symtabs of objfile %s ...\n",
4262abfb 7276 objfile_name (dwarf2_per_objfile->objfile));
45cfd468
DE
7277 }
7278
03dd20cc
DJ
7279 /* The queue starts out with one item, but following a DIE reference
7280 may load a new CU, adding it to the end of the queue. */
10b3939b
DJ
7281 for (item = dwarf2_queue; item != NULL; dwarf2_queue = item = next_item)
7282 {
9291a0cd
TT
7283 if (dwarf2_per_objfile->using_index
7284 ? !item->per_cu->v.quick->symtab
7285 : (item->per_cu->v.psymtab && !item->per_cu->v.psymtab->readin))
f4dc4d17
DE
7286 {
7287 struct dwarf2_per_cu_data *per_cu = item->per_cu;
247f5c4f 7288 char buf[100];
f4dc4d17 7289
247f5c4f 7290 if (per_cu->is_debug_types)
f4dc4d17 7291 {
247f5c4f
DE
7292 struct signatured_type *sig_type =
7293 (struct signatured_type *) per_cu;
7294
7295 sprintf (buf, "TU %s at offset 0x%x",
7296 hex_string (sig_type->signature), per_cu->offset.sect_off);
f4dc4d17 7297 }
247f5c4f
DE
7298 else
7299 sprintf (buf, "CU at offset 0x%x", per_cu->offset.sect_off);
7300
7301 if (dwarf2_read_debug)
7302 fprintf_unfiltered (gdb_stdlog, "Expanding symtab of %s\n", buf);
f4dc4d17
DE
7303
7304 if (per_cu->is_debug_types)
7305 process_full_type_unit (per_cu, item->pretend_language);
7306 else
7307 process_full_comp_unit (per_cu, item->pretend_language);
7308
7309 if (dwarf2_read_debug)
247f5c4f 7310 fprintf_unfiltered (gdb_stdlog, "Done expanding %s\n", buf);
f4dc4d17 7311 }
10b3939b
DJ
7312
7313 item->per_cu->queued = 0;
7314 next_item = item->next;
7315 xfree (item);
7316 }
7317
7318 dwarf2_queue_tail = NULL;
45cfd468
DE
7319
7320 if (dwarf2_read_debug)
7321 {
7322 fprintf_unfiltered (gdb_stdlog, "Done expanding symtabs of %s.\n",
4262abfb 7323 objfile_name (dwarf2_per_objfile->objfile));
45cfd468 7324 }
10b3939b
DJ
7325}
7326
7327/* Free all allocated queue entries. This function only releases anything if
7328 an error was thrown; if the queue was processed then it would have been
7329 freed as we went along. */
7330
7331static void
7332dwarf2_release_queue (void *dummy)
7333{
7334 struct dwarf2_queue_item *item, *last;
7335
7336 item = dwarf2_queue;
7337 while (item)
7338 {
7339 /* Anything still marked queued is likely to be in an
7340 inconsistent state, so discard it. */
7341 if (item->per_cu->queued)
7342 {
7343 if (item->per_cu->cu != NULL)
dee91e82 7344 free_one_cached_comp_unit (item->per_cu);
10b3939b
DJ
7345 item->per_cu->queued = 0;
7346 }
7347
7348 last = item;
7349 item = item->next;
7350 xfree (last);
7351 }
7352
7353 dwarf2_queue = dwarf2_queue_tail = NULL;
7354}
7355
7356/* Read in full symbols for PST, and anything it depends on. */
7357
c906108c 7358static void
fba45db2 7359psymtab_to_symtab_1 (struct partial_symtab *pst)
c906108c 7360{
10b3939b 7361 struct dwarf2_per_cu_data *per_cu;
aaa75496
JB
7362 int i;
7363
95554aad
TT
7364 if (pst->readin)
7365 return;
7366
aaa75496 7367 for (i = 0; i < pst->number_of_dependencies; i++)
95554aad
TT
7368 if (!pst->dependencies[i]->readin
7369 && pst->dependencies[i]->user == NULL)
aaa75496
JB
7370 {
7371 /* Inform about additional files that need to be read in. */
7372 if (info_verbose)
7373 {
a3f17187 7374 /* FIXME: i18n: Need to make this a single string. */
aaa75496
JB
7375 fputs_filtered (" ", gdb_stdout);
7376 wrap_here ("");
7377 fputs_filtered ("and ", gdb_stdout);
7378 wrap_here ("");
7379 printf_filtered ("%s...", pst->dependencies[i]->filename);
0963b4bd 7380 wrap_here (""); /* Flush output. */
aaa75496
JB
7381 gdb_flush (gdb_stdout);
7382 }
7383 psymtab_to_symtab_1 (pst->dependencies[i]);
7384 }
7385
e38df1d0 7386 per_cu = pst->read_symtab_private;
10b3939b
DJ
7387
7388 if (per_cu == NULL)
aaa75496
JB
7389 {
7390 /* It's an include file, no symbols to read for it.
7391 Everything is in the parent symtab. */
7392 pst->readin = 1;
7393 return;
7394 }
c906108c 7395
a0f42c21 7396 dw2_do_instantiate_symtab (per_cu);
10b3939b
DJ
7397}
7398
dee91e82
DE
7399/* Trivial hash function for die_info: the hash value of a DIE
7400 is its offset in .debug_info for this objfile. */
10b3939b 7401
dee91e82
DE
7402static hashval_t
7403die_hash (const void *item)
10b3939b 7404{
dee91e82 7405 const struct die_info *die = item;
6502dd73 7406
dee91e82
DE
7407 return die->offset.sect_off;
7408}
63d06c5c 7409
dee91e82
DE
7410/* Trivial comparison function for die_info structures: two DIEs
7411 are equal if they have the same offset. */
98bfdba5 7412
dee91e82
DE
7413static int
7414die_eq (const void *item_lhs, const void *item_rhs)
7415{
7416 const struct die_info *die_lhs = item_lhs;
7417 const struct die_info *die_rhs = item_rhs;
c906108c 7418
dee91e82
DE
7419 return die_lhs->offset.sect_off == die_rhs->offset.sect_off;
7420}
c906108c 7421
dee91e82
DE
7422/* die_reader_func for load_full_comp_unit.
7423 This is identical to read_signatured_type_reader,
7424 but is kept separate for now. */
c906108c 7425
dee91e82
DE
7426static void
7427load_full_comp_unit_reader (const struct die_reader_specs *reader,
d521ce57 7428 const gdb_byte *info_ptr,
dee91e82
DE
7429 struct die_info *comp_unit_die,
7430 int has_children,
7431 void *data)
7432{
7433 struct dwarf2_cu *cu = reader->cu;
95554aad 7434 enum language *language_ptr = data;
6caca83c 7435
dee91e82
DE
7436 gdb_assert (cu->die_hash == NULL);
7437 cu->die_hash =
7438 htab_create_alloc_ex (cu->header.length / 12,
7439 die_hash,
7440 die_eq,
7441 NULL,
7442 &cu->comp_unit_obstack,
7443 hashtab_obstack_allocate,
7444 dummy_obstack_deallocate);
e142c38c 7445
dee91e82
DE
7446 if (has_children)
7447 comp_unit_die->child = read_die_and_siblings (reader, info_ptr,
7448 &info_ptr, comp_unit_die);
7449 cu->dies = comp_unit_die;
7450 /* comp_unit_die is not stored in die_hash, no need. */
10b3939b
DJ
7451
7452 /* We try not to read any attributes in this function, because not
9cdd5dbd 7453 all CUs needed for references have been loaded yet, and symbol
10b3939b 7454 table processing isn't initialized. But we have to set the CU language,
dee91e82
DE
7455 or we won't be able to build types correctly.
7456 Similarly, if we do not read the producer, we can not apply
7457 producer-specific interpretation. */
95554aad 7458 prepare_one_comp_unit (cu, cu->dies, *language_ptr);
dee91e82 7459}
10b3939b 7460
dee91e82 7461/* Load the DIEs associated with PER_CU into memory. */
a6c727b2 7462
dee91e82 7463static void
95554aad
TT
7464load_full_comp_unit (struct dwarf2_per_cu_data *this_cu,
7465 enum language pretend_language)
dee91e82 7466{
3019eac3 7467 gdb_assert (! this_cu->is_debug_types);
c5b7e1cb 7468
f4dc4d17
DE
7469 init_cutu_and_read_dies (this_cu, NULL, 1, 1,
7470 load_full_comp_unit_reader, &pretend_language);
10b3939b
DJ
7471}
7472
3da10d80
KS
7473/* Add a DIE to the delayed physname list. */
7474
7475static void
7476add_to_method_list (struct type *type, int fnfield_index, int index,
7477 const char *name, struct die_info *die,
7478 struct dwarf2_cu *cu)
7479{
7480 struct delayed_method_info mi;
7481 mi.type = type;
7482 mi.fnfield_index = fnfield_index;
7483 mi.index = index;
7484 mi.name = name;
7485 mi.die = die;
7486 VEC_safe_push (delayed_method_info, cu->method_list, &mi);
7487}
7488
7489/* A cleanup for freeing the delayed method list. */
7490
7491static void
7492free_delayed_list (void *ptr)
7493{
7494 struct dwarf2_cu *cu = (struct dwarf2_cu *) ptr;
7495 if (cu->method_list != NULL)
7496 {
7497 VEC_free (delayed_method_info, cu->method_list);
7498 cu->method_list = NULL;
7499 }
7500}
7501
7502/* Compute the physnames of any methods on the CU's method list.
7503
7504 The computation of method physnames is delayed in order to avoid the
7505 (bad) condition that one of the method's formal parameters is of an as yet
7506 incomplete type. */
7507
7508static void
7509compute_delayed_physnames (struct dwarf2_cu *cu)
7510{
7511 int i;
7512 struct delayed_method_info *mi;
7513 for (i = 0; VEC_iterate (delayed_method_info, cu->method_list, i, mi) ; ++i)
7514 {
1d06ead6 7515 const char *physname;
3da10d80
KS
7516 struct fn_fieldlist *fn_flp
7517 = &TYPE_FN_FIELDLIST (mi->type, mi->fnfield_index);
7d455152 7518 physname = dwarf2_physname (mi->name, mi->die, cu);
3da10d80
KS
7519 fn_flp->fn_fields[mi->index].physname = physname ? physname : "";
7520 }
7521}
7522
a766d390
DE
7523/* Go objects should be embedded in a DW_TAG_module DIE,
7524 and it's not clear if/how imported objects will appear.
7525 To keep Go support simple until that's worked out,
7526 go back through what we've read and create something usable.
7527 We could do this while processing each DIE, and feels kinda cleaner,
7528 but that way is more invasive.
7529 This is to, for example, allow the user to type "p var" or "b main"
7530 without having to specify the package name, and allow lookups
7531 of module.object to work in contexts that use the expression
7532 parser. */
7533
7534static void
7535fixup_go_packaging (struct dwarf2_cu *cu)
7536{
7537 char *package_name = NULL;
7538 struct pending *list;
7539 int i;
7540
7541 for (list = global_symbols; list != NULL; list = list->next)
7542 {
7543 for (i = 0; i < list->nsyms; ++i)
7544 {
7545 struct symbol *sym = list->symbol[i];
7546
7547 if (SYMBOL_LANGUAGE (sym) == language_go
7548 && SYMBOL_CLASS (sym) == LOC_BLOCK)
7549 {
7550 char *this_package_name = go_symbol_package_name (sym);
7551
7552 if (this_package_name == NULL)
7553 continue;
7554 if (package_name == NULL)
7555 package_name = this_package_name;
7556 else
7557 {
7558 if (strcmp (package_name, this_package_name) != 0)
7559 complaint (&symfile_complaints,
7560 _("Symtab %s has objects from two different Go packages: %s and %s"),
210bbc17 7561 (SYMBOL_SYMTAB (sym)
05cba821 7562 ? symtab_to_filename_for_display (SYMBOL_SYMTAB (sym))
4262abfb 7563 : objfile_name (cu->objfile)),
a766d390
DE
7564 this_package_name, package_name);
7565 xfree (this_package_name);
7566 }
7567 }
7568 }
7569 }
7570
7571 if (package_name != NULL)
7572 {
7573 struct objfile *objfile = cu->objfile;
10f0c4bb
TT
7574 const char *saved_package_name = obstack_copy0 (&objfile->objfile_obstack,
7575 package_name,
7576 strlen (package_name));
a766d390 7577 struct type *type = init_type (TYPE_CODE_MODULE, 0, 0,
86f62fd7 7578 saved_package_name, objfile);
a766d390
DE
7579 struct symbol *sym;
7580
7581 TYPE_TAG_NAME (type) = TYPE_NAME (type);
7582
e623cf5d 7583 sym = allocate_symbol (objfile);
f85f34ed 7584 SYMBOL_SET_LANGUAGE (sym, language_go, &objfile->objfile_obstack);
86f62fd7
TT
7585 SYMBOL_SET_NAMES (sym, saved_package_name,
7586 strlen (saved_package_name), 0, objfile);
a766d390
DE
7587 /* This is not VAR_DOMAIN because we want a way to ensure a lookup of,
7588 e.g., "main" finds the "main" module and not C's main(). */
7589 SYMBOL_DOMAIN (sym) = STRUCT_DOMAIN;
f1e6e072 7590 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
a766d390
DE
7591 SYMBOL_TYPE (sym) = type;
7592
7593 add_symbol_to_list (sym, &global_symbols);
7594
7595 xfree (package_name);
7596 }
7597}
7598
95554aad
TT
7599/* Return the symtab for PER_CU. This works properly regardless of
7600 whether we're using the index or psymtabs. */
7601
7602static struct symtab *
7603get_symtab (struct dwarf2_per_cu_data *per_cu)
7604{
7605 return (dwarf2_per_objfile->using_index
7606 ? per_cu->v.quick->symtab
7607 : per_cu->v.psymtab->symtab);
7608}
7609
7610/* A helper function for computing the list of all symbol tables
7611 included by PER_CU. */
7612
7613static void
ec94af83
DE
7614recursively_compute_inclusions (VEC (symtab_ptr) **result,
7615 htab_t all_children, htab_t all_type_symtabs,
f9125b6c
TT
7616 struct dwarf2_per_cu_data *per_cu,
7617 struct symtab *immediate_parent)
95554aad
TT
7618{
7619 void **slot;
7620 int ix;
ec94af83 7621 struct symtab *symtab;
95554aad
TT
7622 struct dwarf2_per_cu_data *iter;
7623
7624 slot = htab_find_slot (all_children, per_cu, INSERT);
7625 if (*slot != NULL)
7626 {
7627 /* This inclusion and its children have been processed. */
7628 return;
7629 }
7630
7631 *slot = per_cu;
7632 /* Only add a CU if it has a symbol table. */
ec94af83
DE
7633 symtab = get_symtab (per_cu);
7634 if (symtab != NULL)
7635 {
7636 /* If this is a type unit only add its symbol table if we haven't
7637 seen it yet (type unit per_cu's can share symtabs). */
7638 if (per_cu->is_debug_types)
7639 {
7640 slot = htab_find_slot (all_type_symtabs, symtab, INSERT);
7641 if (*slot == NULL)
7642 {
7643 *slot = symtab;
7644 VEC_safe_push (symtab_ptr, *result, symtab);
f9125b6c
TT
7645 if (symtab->user == NULL)
7646 symtab->user = immediate_parent;
ec94af83
DE
7647 }
7648 }
7649 else
f9125b6c
TT
7650 {
7651 VEC_safe_push (symtab_ptr, *result, symtab);
7652 if (symtab->user == NULL)
7653 symtab->user = immediate_parent;
7654 }
ec94af83 7655 }
95554aad
TT
7656
7657 for (ix = 0;
796a7ff8 7658 VEC_iterate (dwarf2_per_cu_ptr, per_cu->imported_symtabs, ix, iter);
95554aad 7659 ++ix)
ec94af83
DE
7660 {
7661 recursively_compute_inclusions (result, all_children,
f9125b6c 7662 all_type_symtabs, iter, symtab);
ec94af83 7663 }
95554aad
TT
7664}
7665
7666/* Compute the symtab 'includes' fields for the symtab related to
7667 PER_CU. */
7668
7669static void
7670compute_symtab_includes (struct dwarf2_per_cu_data *per_cu)
7671{
f4dc4d17
DE
7672 gdb_assert (! per_cu->is_debug_types);
7673
796a7ff8 7674 if (!VEC_empty (dwarf2_per_cu_ptr, per_cu->imported_symtabs))
95554aad
TT
7675 {
7676 int ix, len;
ec94af83
DE
7677 struct dwarf2_per_cu_data *per_cu_iter;
7678 struct symtab *symtab_iter;
7679 VEC (symtab_ptr) *result_symtabs = NULL;
7680 htab_t all_children, all_type_symtabs;
95554aad
TT
7681 struct symtab *symtab = get_symtab (per_cu);
7682
7683 /* If we don't have a symtab, we can just skip this case. */
7684 if (symtab == NULL)
7685 return;
7686
7687 all_children = htab_create_alloc (1, htab_hash_pointer, htab_eq_pointer,
7688 NULL, xcalloc, xfree);
ec94af83
DE
7689 all_type_symtabs = htab_create_alloc (1, htab_hash_pointer, htab_eq_pointer,
7690 NULL, xcalloc, xfree);
95554aad
TT
7691
7692 for (ix = 0;
796a7ff8 7693 VEC_iterate (dwarf2_per_cu_ptr, per_cu->imported_symtabs,
ec94af83 7694 ix, per_cu_iter);
95554aad 7695 ++ix)
ec94af83
DE
7696 {
7697 recursively_compute_inclusions (&result_symtabs, all_children,
f9125b6c
TT
7698 all_type_symtabs, per_cu_iter,
7699 symtab);
ec94af83 7700 }
95554aad 7701
ec94af83
DE
7702 /* Now we have a transitive closure of all the included symtabs. */
7703 len = VEC_length (symtab_ptr, result_symtabs);
95554aad
TT
7704 symtab->includes
7705 = obstack_alloc (&dwarf2_per_objfile->objfile->objfile_obstack,
7706 (len + 1) * sizeof (struct symtab *));
7707 for (ix = 0;
ec94af83 7708 VEC_iterate (symtab_ptr, result_symtabs, ix, symtab_iter);
95554aad 7709 ++ix)
ec94af83 7710 symtab->includes[ix] = symtab_iter;
95554aad
TT
7711 symtab->includes[len] = NULL;
7712
ec94af83 7713 VEC_free (symtab_ptr, result_symtabs);
95554aad 7714 htab_delete (all_children);
ec94af83 7715 htab_delete (all_type_symtabs);
95554aad
TT
7716 }
7717}
7718
7719/* Compute the 'includes' field for the symtabs of all the CUs we just
7720 read. */
7721
7722static void
7723process_cu_includes (void)
7724{
7725 int ix;
7726 struct dwarf2_per_cu_data *iter;
7727
7728 for (ix = 0;
7729 VEC_iterate (dwarf2_per_cu_ptr, dwarf2_per_objfile->just_read_cus,
7730 ix, iter);
7731 ++ix)
f4dc4d17
DE
7732 {
7733 if (! iter->is_debug_types)
7734 compute_symtab_includes (iter);
7735 }
95554aad
TT
7736
7737 VEC_free (dwarf2_per_cu_ptr, dwarf2_per_objfile->just_read_cus);
7738}
7739
9cdd5dbd 7740/* Generate full symbol information for PER_CU, whose DIEs have
10b3939b
DJ
7741 already been loaded into memory. */
7742
7743static void
95554aad
TT
7744process_full_comp_unit (struct dwarf2_per_cu_data *per_cu,
7745 enum language pretend_language)
10b3939b 7746{
10b3939b 7747 struct dwarf2_cu *cu = per_cu->cu;
9291a0cd 7748 struct objfile *objfile = per_cu->objfile;
10b3939b
DJ
7749 CORE_ADDR lowpc, highpc;
7750 struct symtab *symtab;
3da10d80 7751 struct cleanup *back_to, *delayed_list_cleanup;
10b3939b 7752 CORE_ADDR baseaddr;
4359dff1 7753 struct block *static_block;
10b3939b
DJ
7754
7755 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
7756
10b3939b
DJ
7757 buildsym_init ();
7758 back_to = make_cleanup (really_free_pendings, NULL);
3da10d80 7759 delayed_list_cleanup = make_cleanup (free_delayed_list, cu);
10b3939b
DJ
7760
7761 cu->list_in_scope = &file_symbols;
c906108c 7762
95554aad
TT
7763 cu->language = pretend_language;
7764 cu->language_defn = language_def (cu->language);
7765
c906108c 7766 /* Do line number decoding in read_file_scope () */
10b3939b 7767 process_die (cu->dies, cu);
c906108c 7768
a766d390
DE
7769 /* For now fudge the Go package. */
7770 if (cu->language == language_go)
7771 fixup_go_packaging (cu);
7772
3da10d80
KS
7773 /* Now that we have processed all the DIEs in the CU, all the types
7774 should be complete, and it should now be safe to compute all of the
7775 physnames. */
7776 compute_delayed_physnames (cu);
7777 do_cleanups (delayed_list_cleanup);
7778
fae299cd
DC
7779 /* Some compilers don't define a DW_AT_high_pc attribute for the
7780 compilation unit. If the DW_AT_high_pc is missing, synthesize
7781 it, by scanning the DIE's below the compilation unit. */
10b3939b 7782 get_scope_pc_bounds (cu->dies, &lowpc, &highpc, cu);
c906108c 7783
36586728 7784 static_block
ff546935 7785 = end_symtab_get_static_block (highpc + baseaddr, objfile, 0, 1);
4359dff1
JK
7786
7787 /* If the comp unit has DW_AT_ranges, it may have discontiguous ranges.
7788 Also, DW_AT_ranges may record ranges not belonging to any child DIEs
7789 (such as virtual method tables). Record the ranges in STATIC_BLOCK's
7790 addrmap to help ensure it has an accurate map of pc values belonging to
7791 this comp unit. */
7792 dwarf2_record_block_ranges (cu->dies, static_block, baseaddr, cu);
7793
7794 symtab = end_symtab_from_static_block (static_block, objfile,
7795 SECT_OFF_TEXT (objfile), 0);
c906108c 7796
8be455d7 7797 if (symtab != NULL)
c906108c 7798 {
df15bd07 7799 int gcc_4_minor = producer_is_gcc_ge_4 (cu->producer);
4632c0d0 7800
8be455d7
JK
7801 /* Set symtab language to language from DW_AT_language. If the
7802 compilation is from a C file generated by language preprocessors, do
7803 not set the language if it was already deduced by start_subfile. */
7804 if (!(cu->language == language_c && symtab->language != language_c))
7805 symtab->language = cu->language;
7806
7807 /* GCC-4.0 has started to support -fvar-tracking. GCC-3.x still can
7808 produce DW_AT_location with location lists but it can be possibly
ab260dad
JK
7809 invalid without -fvar-tracking. Still up to GCC-4.4.x incl. 4.4.0
7810 there were bugs in prologue debug info, fixed later in GCC-4.5
7811 by "unwind info for epilogues" patch (which is not directly related).
8be455d7
JK
7812
7813 For -gdwarf-4 type units LOCATIONS_VALID indication is fortunately not
7814 needed, it would be wrong due to missing DW_AT_producer there.
7815
7816 Still one can confuse GDB by using non-standard GCC compilation
7817 options - this waits on GCC PR other/32998 (-frecord-gcc-switches).
7818 */
ab260dad 7819 if (cu->has_loclist && gcc_4_minor >= 5)
8be455d7 7820 symtab->locations_valid = 1;
e0d00bc7
JK
7821
7822 if (gcc_4_minor >= 5)
7823 symtab->epilogue_unwind_valid = 1;
96408a79
SA
7824
7825 symtab->call_site_htab = cu->call_site_htab;
c906108c 7826 }
9291a0cd
TT
7827
7828 if (dwarf2_per_objfile->using_index)
7829 per_cu->v.quick->symtab = symtab;
7830 else
7831 {
7832 struct partial_symtab *pst = per_cu->v.psymtab;
7833 pst->symtab = symtab;
7834 pst->readin = 1;
7835 }
c906108c 7836
95554aad
TT
7837 /* Push it for inclusion processing later. */
7838 VEC_safe_push (dwarf2_per_cu_ptr, dwarf2_per_objfile->just_read_cus, per_cu);
7839
c906108c 7840 do_cleanups (back_to);
f4dc4d17 7841}
45cfd468 7842
f4dc4d17
DE
7843/* Generate full symbol information for type unit PER_CU, whose DIEs have
7844 already been loaded into memory. */
7845
7846static void
7847process_full_type_unit (struct dwarf2_per_cu_data *per_cu,
7848 enum language pretend_language)
7849{
7850 struct dwarf2_cu *cu = per_cu->cu;
7851 struct objfile *objfile = per_cu->objfile;
7852 struct symtab *symtab;
7853 struct cleanup *back_to, *delayed_list_cleanup;
0186c6a7
DE
7854 struct signatured_type *sig_type;
7855
7856 gdb_assert (per_cu->is_debug_types);
7857 sig_type = (struct signatured_type *) per_cu;
f4dc4d17
DE
7858
7859 buildsym_init ();
7860 back_to = make_cleanup (really_free_pendings, NULL);
7861 delayed_list_cleanup = make_cleanup (free_delayed_list, cu);
7862
7863 cu->list_in_scope = &file_symbols;
7864
7865 cu->language = pretend_language;
7866 cu->language_defn = language_def (cu->language);
7867
7868 /* The symbol tables are set up in read_type_unit_scope. */
7869 process_die (cu->dies, cu);
7870
7871 /* For now fudge the Go package. */
7872 if (cu->language == language_go)
7873 fixup_go_packaging (cu);
7874
7875 /* Now that we have processed all the DIEs in the CU, all the types
7876 should be complete, and it should now be safe to compute all of the
7877 physnames. */
7878 compute_delayed_physnames (cu);
7879 do_cleanups (delayed_list_cleanup);
7880
7881 /* TUs share symbol tables.
7882 If this is the first TU to use this symtab, complete the construction
094b34ac
DE
7883 of it with end_expandable_symtab. Otherwise, complete the addition of
7884 this TU's symbols to the existing symtab. */
0186c6a7 7885 if (sig_type->type_unit_group->primary_symtab == NULL)
45cfd468 7886 {
f4dc4d17 7887 symtab = end_expandable_symtab (0, objfile, SECT_OFF_TEXT (objfile));
0186c6a7 7888 sig_type->type_unit_group->primary_symtab = symtab;
f4dc4d17
DE
7889
7890 if (symtab != NULL)
7891 {
7892 /* Set symtab language to language from DW_AT_language. If the
7893 compilation is from a C file generated by language preprocessors,
7894 do not set the language if it was already deduced by
7895 start_subfile. */
7896 if (!(cu->language == language_c && symtab->language != language_c))
7897 symtab->language = cu->language;
7898 }
7899 }
7900 else
7901 {
7902 augment_type_symtab (objfile,
0186c6a7
DE
7903 sig_type->type_unit_group->primary_symtab);
7904 symtab = sig_type->type_unit_group->primary_symtab;
f4dc4d17
DE
7905 }
7906
7907 if (dwarf2_per_objfile->using_index)
7908 per_cu->v.quick->symtab = symtab;
7909 else
7910 {
7911 struct partial_symtab *pst = per_cu->v.psymtab;
7912 pst->symtab = symtab;
7913 pst->readin = 1;
45cfd468 7914 }
f4dc4d17
DE
7915
7916 do_cleanups (back_to);
c906108c
SS
7917}
7918
95554aad
TT
7919/* Process an imported unit DIE. */
7920
7921static void
7922process_imported_unit_die (struct die_info *die, struct dwarf2_cu *cu)
7923{
7924 struct attribute *attr;
7925
f4dc4d17
DE
7926 /* For now we don't handle imported units in type units. */
7927 if (cu->per_cu->is_debug_types)
7928 {
7929 error (_("Dwarf Error: DW_TAG_imported_unit is not"
7930 " supported in type units [in module %s]"),
4262abfb 7931 objfile_name (cu->objfile));
f4dc4d17
DE
7932 }
7933
95554aad
TT
7934 attr = dwarf2_attr (die, DW_AT_import, cu);
7935 if (attr != NULL)
7936 {
7937 struct dwarf2_per_cu_data *per_cu;
7938 struct symtab *imported_symtab;
7939 sect_offset offset;
36586728 7940 int is_dwz;
95554aad
TT
7941
7942 offset = dwarf2_get_ref_die_offset (attr);
36586728
TT
7943 is_dwz = (attr->form == DW_FORM_GNU_ref_alt || cu->per_cu->is_dwz);
7944 per_cu = dwarf2_find_containing_comp_unit (offset, is_dwz, cu->objfile);
95554aad 7945
69d751e3 7946 /* If necessary, add it to the queue and load its DIEs. */
95554aad
TT
7947 if (maybe_queue_comp_unit (cu, per_cu, cu->language))
7948 load_full_comp_unit (per_cu, cu->language);
7949
796a7ff8 7950 VEC_safe_push (dwarf2_per_cu_ptr, cu->per_cu->imported_symtabs,
95554aad
TT
7951 per_cu);
7952 }
7953}
7954
c906108c
SS
7955/* Process a die and its children. */
7956
7957static void
e7c27a73 7958process_die (struct die_info *die, struct dwarf2_cu *cu)
c906108c
SS
7959{
7960 switch (die->tag)
7961 {
7962 case DW_TAG_padding:
7963 break;
7964 case DW_TAG_compile_unit:
95554aad 7965 case DW_TAG_partial_unit:
e7c27a73 7966 read_file_scope (die, cu);
c906108c 7967 break;
348e048f
DE
7968 case DW_TAG_type_unit:
7969 read_type_unit_scope (die, cu);
7970 break;
c906108c 7971 case DW_TAG_subprogram:
c906108c 7972 case DW_TAG_inlined_subroutine:
edb3359d 7973 read_func_scope (die, cu);
c906108c
SS
7974 break;
7975 case DW_TAG_lexical_block:
14898363
L
7976 case DW_TAG_try_block:
7977 case DW_TAG_catch_block:
e7c27a73 7978 read_lexical_block_scope (die, cu);
c906108c 7979 break;
96408a79
SA
7980 case DW_TAG_GNU_call_site:
7981 read_call_site_scope (die, cu);
7982 break;
c906108c 7983 case DW_TAG_class_type:
680b30c7 7984 case DW_TAG_interface_type:
c906108c
SS
7985 case DW_TAG_structure_type:
7986 case DW_TAG_union_type:
134d01f1 7987 process_structure_scope (die, cu);
c906108c
SS
7988 break;
7989 case DW_TAG_enumeration_type:
134d01f1 7990 process_enumeration_scope (die, cu);
c906108c 7991 break;
134d01f1 7992
f792889a
DJ
7993 /* These dies have a type, but processing them does not create
7994 a symbol or recurse to process the children. Therefore we can
7995 read them on-demand through read_type_die. */
c906108c 7996 case DW_TAG_subroutine_type:
72019c9c 7997 case DW_TAG_set_type:
c906108c 7998 case DW_TAG_array_type:
c906108c 7999 case DW_TAG_pointer_type:
c906108c 8000 case DW_TAG_ptr_to_member_type:
c906108c 8001 case DW_TAG_reference_type:
c906108c 8002 case DW_TAG_string_type:
c906108c 8003 break;
134d01f1 8004
c906108c 8005 case DW_TAG_base_type:
a02abb62 8006 case DW_TAG_subrange_type:
cb249c71 8007 case DW_TAG_typedef:
134d01f1
DJ
8008 /* Add a typedef symbol for the type definition, if it has a
8009 DW_AT_name. */
f792889a 8010 new_symbol (die, read_type_die (die, cu), cu);
a02abb62 8011 break;
c906108c 8012 case DW_TAG_common_block:
e7c27a73 8013 read_common_block (die, cu);
c906108c
SS
8014 break;
8015 case DW_TAG_common_inclusion:
8016 break;
d9fa45fe 8017 case DW_TAG_namespace:
4d4ec4e5 8018 cu->processing_has_namespace_info = 1;
e7c27a73 8019 read_namespace (die, cu);
d9fa45fe 8020 break;
5d7cb8df 8021 case DW_TAG_module:
4d4ec4e5 8022 cu->processing_has_namespace_info = 1;
5d7cb8df
JK
8023 read_module (die, cu);
8024 break;
d9fa45fe
DC
8025 case DW_TAG_imported_declaration:
8026 case DW_TAG_imported_module:
4d4ec4e5 8027 cu->processing_has_namespace_info = 1;
27aa8d6a
SW
8028 if (die->child != NULL && (die->tag == DW_TAG_imported_declaration
8029 || cu->language != language_fortran))
8030 complaint (&symfile_complaints, _("Tag '%s' has unexpected children"),
8031 dwarf_tag_name (die->tag));
8032 read_import_statement (die, cu);
d9fa45fe 8033 break;
95554aad
TT
8034
8035 case DW_TAG_imported_unit:
8036 process_imported_unit_die (die, cu);
8037 break;
8038
c906108c 8039 default:
e7c27a73 8040 new_symbol (die, NULL, cu);
c906108c
SS
8041 break;
8042 }
8043}
ca69b9e6
DE
8044\f
8045/* DWARF name computation. */
c906108c 8046
94af9270
KS
8047/* A helper function for dwarf2_compute_name which determines whether DIE
8048 needs to have the name of the scope prepended to the name listed in the
8049 die. */
8050
8051static int
8052die_needs_namespace (struct die_info *die, struct dwarf2_cu *cu)
8053{
1c809c68
TT
8054 struct attribute *attr;
8055
94af9270
KS
8056 switch (die->tag)
8057 {
8058 case DW_TAG_namespace:
8059 case DW_TAG_typedef:
8060 case DW_TAG_class_type:
8061 case DW_TAG_interface_type:
8062 case DW_TAG_structure_type:
8063 case DW_TAG_union_type:
8064 case DW_TAG_enumeration_type:
8065 case DW_TAG_enumerator:
8066 case DW_TAG_subprogram:
8067 case DW_TAG_member:
8068 return 1;
8069
8070 case DW_TAG_variable:
c2b0a229 8071 case DW_TAG_constant:
94af9270
KS
8072 /* We only need to prefix "globally" visible variables. These include
8073 any variable marked with DW_AT_external or any variable that
8074 lives in a namespace. [Variables in anonymous namespaces
8075 require prefixing, but they are not DW_AT_external.] */
8076
8077 if (dwarf2_attr (die, DW_AT_specification, cu))
8078 {
8079 struct dwarf2_cu *spec_cu = cu;
9a619af0 8080
94af9270
KS
8081 return die_needs_namespace (die_specification (die, &spec_cu),
8082 spec_cu);
8083 }
8084
1c809c68 8085 attr = dwarf2_attr (die, DW_AT_external, cu);
f55ee35c
JK
8086 if (attr == NULL && die->parent->tag != DW_TAG_namespace
8087 && die->parent->tag != DW_TAG_module)
1c809c68
TT
8088 return 0;
8089 /* A variable in a lexical block of some kind does not need a
8090 namespace, even though in C++ such variables may be external
8091 and have a mangled name. */
8092 if (die->parent->tag == DW_TAG_lexical_block
8093 || die->parent->tag == DW_TAG_try_block
1054b214
TT
8094 || die->parent->tag == DW_TAG_catch_block
8095 || die->parent->tag == DW_TAG_subprogram)
1c809c68
TT
8096 return 0;
8097 return 1;
94af9270
KS
8098
8099 default:
8100 return 0;
8101 }
8102}
8103
98bfdba5
PA
8104/* Retrieve the last character from a mem_file. */
8105
8106static void
8107do_ui_file_peek_last (void *object, const char *buffer, long length)
8108{
8109 char *last_char_p = (char *) object;
8110
8111 if (length > 0)
8112 *last_char_p = buffer[length - 1];
8113}
8114
94af9270 8115/* Compute the fully qualified name of DIE in CU. If PHYSNAME is nonzero,
a766d390
DE
8116 compute the physname for the object, which include a method's:
8117 - formal parameters (C++/Java),
8118 - receiver type (Go),
8119 - return type (Java).
8120
8121 The term "physname" is a bit confusing.
8122 For C++, for example, it is the demangled name.
8123 For Go, for example, it's the mangled name.
94af9270 8124
af6b7be1
JB
8125 For Ada, return the DIE's linkage name rather than the fully qualified
8126 name. PHYSNAME is ignored..
8127
94af9270
KS
8128 The result is allocated on the objfile_obstack and canonicalized. */
8129
8130static const char *
15d034d0
TT
8131dwarf2_compute_name (const char *name,
8132 struct die_info *die, struct dwarf2_cu *cu,
94af9270
KS
8133 int physname)
8134{
bb5ed363
DE
8135 struct objfile *objfile = cu->objfile;
8136
94af9270
KS
8137 if (name == NULL)
8138 name = dwarf2_name (die, cu);
8139
f55ee35c
JK
8140 /* For Fortran GDB prefers DW_AT_*linkage_name if present but otherwise
8141 compute it by typename_concat inside GDB. */
8142 if (cu->language == language_ada
8143 || (cu->language == language_fortran && physname))
8144 {
8145 /* For Ada unit, we prefer the linkage name over the name, as
8146 the former contains the exported name, which the user expects
8147 to be able to reference. Ideally, we want the user to be able
8148 to reference this entity using either natural or linkage name,
8149 but we haven't started looking at this enhancement yet. */
8150 struct attribute *attr;
8151
8152 attr = dwarf2_attr (die, DW_AT_linkage_name, cu);
8153 if (attr == NULL)
8154 attr = dwarf2_attr (die, DW_AT_MIPS_linkage_name, cu);
8155 if (attr && DW_STRING (attr))
8156 return DW_STRING (attr);
8157 }
8158
94af9270
KS
8159 /* These are the only languages we know how to qualify names in. */
8160 if (name != NULL
f55ee35c
JK
8161 && (cu->language == language_cplus || cu->language == language_java
8162 || cu->language == language_fortran))
94af9270
KS
8163 {
8164 if (die_needs_namespace (die, cu))
8165 {
8166 long length;
0d5cff50 8167 const char *prefix;
94af9270
KS
8168 struct ui_file *buf;
8169
8170 prefix = determine_prefix (die, cu);
8171 buf = mem_fileopen ();
8172 if (*prefix != '\0')
8173 {
f55ee35c
JK
8174 char *prefixed_name = typename_concat (NULL, prefix, name,
8175 physname, cu);
9a619af0 8176
94af9270
KS
8177 fputs_unfiltered (prefixed_name, buf);
8178 xfree (prefixed_name);
8179 }
8180 else
62d5b8da 8181 fputs_unfiltered (name, buf);
94af9270 8182
98bfdba5
PA
8183 /* Template parameters may be specified in the DIE's DW_AT_name, or
8184 as children with DW_TAG_template_type_param or
8185 DW_TAG_value_type_param. If the latter, add them to the name
8186 here. If the name already has template parameters, then
8187 skip this step; some versions of GCC emit both, and
8188 it is more efficient to use the pre-computed name.
8189
8190 Something to keep in mind about this process: it is very
8191 unlikely, or in some cases downright impossible, to produce
8192 something that will match the mangled name of a function.
8193 If the definition of the function has the same debug info,
8194 we should be able to match up with it anyway. But fallbacks
8195 using the minimal symbol, for instance to find a method
8196 implemented in a stripped copy of libstdc++, will not work.
8197 If we do not have debug info for the definition, we will have to
8198 match them up some other way.
8199
8200 When we do name matching there is a related problem with function
8201 templates; two instantiated function templates are allowed to
8202 differ only by their return types, which we do not add here. */
8203
8204 if (cu->language == language_cplus && strchr (name, '<') == NULL)
8205 {
8206 struct attribute *attr;
8207 struct die_info *child;
8208 int first = 1;
8209
8210 die->building_fullname = 1;
8211
8212 for (child = die->child; child != NULL; child = child->sibling)
8213 {
8214 struct type *type;
12df843f 8215 LONGEST value;
d521ce57 8216 const gdb_byte *bytes;
98bfdba5
PA
8217 struct dwarf2_locexpr_baton *baton;
8218 struct value *v;
8219
8220 if (child->tag != DW_TAG_template_type_param
8221 && child->tag != DW_TAG_template_value_param)
8222 continue;
8223
8224 if (first)
8225 {
8226 fputs_unfiltered ("<", buf);
8227 first = 0;
8228 }
8229 else
8230 fputs_unfiltered (", ", buf);
8231
8232 attr = dwarf2_attr (child, DW_AT_type, cu);
8233 if (attr == NULL)
8234 {
8235 complaint (&symfile_complaints,
8236 _("template parameter missing DW_AT_type"));
8237 fputs_unfiltered ("UNKNOWN_TYPE", buf);
8238 continue;
8239 }
8240 type = die_type (child, cu);
8241
8242 if (child->tag == DW_TAG_template_type_param)
8243 {
79d43c61 8244 c_print_type (type, "", buf, -1, 0, &type_print_raw_options);
98bfdba5
PA
8245 continue;
8246 }
8247
8248 attr = dwarf2_attr (child, DW_AT_const_value, cu);
8249 if (attr == NULL)
8250 {
8251 complaint (&symfile_complaints,
3e43a32a
MS
8252 _("template parameter missing "
8253 "DW_AT_const_value"));
98bfdba5
PA
8254 fputs_unfiltered ("UNKNOWN_VALUE", buf);
8255 continue;
8256 }
8257
8258 dwarf2_const_value_attr (attr, type, name,
8259 &cu->comp_unit_obstack, cu,
8260 &value, &bytes, &baton);
8261
8262 if (TYPE_NOSIGN (type))
8263 /* GDB prints characters as NUMBER 'CHAR'. If that's
8264 changed, this can use value_print instead. */
8265 c_printchar (value, type, buf);
8266 else
8267 {
8268 struct value_print_options opts;
8269
8270 if (baton != NULL)
8271 v = dwarf2_evaluate_loc_desc (type, NULL,
8272 baton->data,
8273 baton->size,
8274 baton->per_cu);
8275 else if (bytes != NULL)
8276 {
8277 v = allocate_value (type);
8278 memcpy (value_contents_writeable (v), bytes,
8279 TYPE_LENGTH (type));
8280 }
8281 else
8282 v = value_from_longest (type, value);
8283
3e43a32a
MS
8284 /* Specify decimal so that we do not depend on
8285 the radix. */
98bfdba5
PA
8286 get_formatted_print_options (&opts, 'd');
8287 opts.raw = 1;
8288 value_print (v, buf, &opts);
8289 release_value (v);
8290 value_free (v);
8291 }
8292 }
8293
8294 die->building_fullname = 0;
8295
8296 if (!first)
8297 {
8298 /* Close the argument list, with a space if necessary
8299 (nested templates). */
8300 char last_char = '\0';
8301 ui_file_put (buf, do_ui_file_peek_last, &last_char);
8302 if (last_char == '>')
8303 fputs_unfiltered (" >", buf);
8304 else
8305 fputs_unfiltered (">", buf);
8306 }
8307 }
8308
94af9270
KS
8309 /* For Java and C++ methods, append formal parameter type
8310 information, if PHYSNAME. */
6e70227d 8311
94af9270
KS
8312 if (physname && die->tag == DW_TAG_subprogram
8313 && (cu->language == language_cplus
8314 || cu->language == language_java))
8315 {
8316 struct type *type = read_type_die (die, cu);
8317
79d43c61
TT
8318 c_type_print_args (type, buf, 1, cu->language,
8319 &type_print_raw_options);
94af9270
KS
8320
8321 if (cu->language == language_java)
8322 {
8323 /* For java, we must append the return type to method
0963b4bd 8324 names. */
94af9270
KS
8325 if (die->tag == DW_TAG_subprogram)
8326 java_print_type (TYPE_TARGET_TYPE (type), "", buf,
79d43c61 8327 0, 0, &type_print_raw_options);
94af9270
KS
8328 }
8329 else if (cu->language == language_cplus)
8330 {
60430eff
DJ
8331 /* Assume that an artificial first parameter is
8332 "this", but do not crash if it is not. RealView
8333 marks unnamed (and thus unused) parameters as
8334 artificial; there is no way to differentiate
8335 the two cases. */
94af9270
KS
8336 if (TYPE_NFIELDS (type) > 0
8337 && TYPE_FIELD_ARTIFICIAL (type, 0)
60430eff 8338 && TYPE_CODE (TYPE_FIELD_TYPE (type, 0)) == TYPE_CODE_PTR
3e43a32a
MS
8339 && TYPE_CONST (TYPE_TARGET_TYPE (TYPE_FIELD_TYPE (type,
8340 0))))
94af9270
KS
8341 fputs_unfiltered (" const", buf);
8342 }
8343 }
8344
bb5ed363 8345 name = ui_file_obsavestring (buf, &objfile->objfile_obstack,
94af9270
KS
8346 &length);
8347 ui_file_delete (buf);
8348
8349 if (cu->language == language_cplus)
8350 {
15d034d0 8351 const char *cname
94af9270 8352 = dwarf2_canonicalize_name (name, cu,
bb5ed363 8353 &objfile->objfile_obstack);
9a619af0 8354
94af9270
KS
8355 if (cname != NULL)
8356 name = cname;
8357 }
8358 }
8359 }
8360
8361 return name;
8362}
8363
0114d602
DJ
8364/* Return the fully qualified name of DIE, based on its DW_AT_name.
8365 If scope qualifiers are appropriate they will be added. The result
8366 will be allocated on the objfile_obstack, or NULL if the DIE does
94af9270
KS
8367 not have a name. NAME may either be from a previous call to
8368 dwarf2_name or NULL.
8369
0963b4bd 8370 The output string will be canonicalized (if C++/Java). */
0114d602
DJ
8371
8372static const char *
15d034d0 8373dwarf2_full_name (const char *name, struct die_info *die, struct dwarf2_cu *cu)
0114d602 8374{
94af9270
KS
8375 return dwarf2_compute_name (name, die, cu, 0);
8376}
0114d602 8377
94af9270
KS
8378/* Construct a physname for the given DIE in CU. NAME may either be
8379 from a previous call to dwarf2_name or NULL. The result will be
8380 allocated on the objfile_objstack or NULL if the DIE does not have a
8381 name.
0114d602 8382
94af9270 8383 The output string will be canonicalized (if C++/Java). */
0114d602 8384
94af9270 8385static const char *
15d034d0 8386dwarf2_physname (const char *name, struct die_info *die, struct dwarf2_cu *cu)
94af9270 8387{
bb5ed363 8388 struct objfile *objfile = cu->objfile;
900e11f9
JK
8389 struct attribute *attr;
8390 const char *retval, *mangled = NULL, *canon = NULL;
8391 struct cleanup *back_to;
8392 int need_copy = 1;
8393
8394 /* In this case dwarf2_compute_name is just a shortcut not building anything
8395 on its own. */
8396 if (!die_needs_namespace (die, cu))
8397 return dwarf2_compute_name (name, die, cu, 1);
8398
8399 back_to = make_cleanup (null_cleanup, NULL);
8400
8401 attr = dwarf2_attr (die, DW_AT_linkage_name, cu);
8402 if (!attr)
8403 attr = dwarf2_attr (die, DW_AT_MIPS_linkage_name, cu);
8404
8405 /* DW_AT_linkage_name is missing in some cases - depend on what GDB
8406 has computed. */
8407 if (attr && DW_STRING (attr))
8408 {
8409 char *demangled;
8410
8411 mangled = DW_STRING (attr);
8412
8413 /* Use DMGL_RET_DROP for C++ template functions to suppress their return
8414 type. It is easier for GDB users to search for such functions as
8415 `name(params)' than `long name(params)'. In such case the minimal
8416 symbol names do not match the full symbol names but for template
8417 functions there is never a need to look up their definition from their
8418 declaration so the only disadvantage remains the minimal symbol
8419 variant `long name(params)' does not have the proper inferior type.
8420 */
8421
a766d390
DE
8422 if (cu->language == language_go)
8423 {
8424 /* This is a lie, but we already lie to the caller new_symbol_full.
8425 new_symbol_full assumes we return the mangled name.
8426 This just undoes that lie until things are cleaned up. */
8427 demangled = NULL;
8428 }
8429 else
8430 {
8de20a37
TT
8431 demangled = gdb_demangle (mangled,
8432 (DMGL_PARAMS | DMGL_ANSI
8433 | (cu->language == language_java
8434 ? DMGL_JAVA | DMGL_RET_POSTFIX
8435 : DMGL_RET_DROP)));
a766d390 8436 }
900e11f9
JK
8437 if (demangled)
8438 {
8439 make_cleanup (xfree, demangled);
8440 canon = demangled;
8441 }
8442 else
8443 {
8444 canon = mangled;
8445 need_copy = 0;
8446 }
8447 }
8448
8449 if (canon == NULL || check_physname)
8450 {
8451 const char *physname = dwarf2_compute_name (name, die, cu, 1);
8452
8453 if (canon != NULL && strcmp (physname, canon) != 0)
8454 {
8455 /* It may not mean a bug in GDB. The compiler could also
8456 compute DW_AT_linkage_name incorrectly. But in such case
8457 GDB would need to be bug-to-bug compatible. */
8458
8459 complaint (&symfile_complaints,
8460 _("Computed physname <%s> does not match demangled <%s> "
8461 "(from linkage <%s>) - DIE at 0x%x [in module %s]"),
4262abfb
JK
8462 physname, canon, mangled, die->offset.sect_off,
8463 objfile_name (objfile));
900e11f9
JK
8464
8465 /* Prefer DW_AT_linkage_name (in the CANON form) - when it
8466 is available here - over computed PHYSNAME. It is safer
8467 against both buggy GDB and buggy compilers. */
8468
8469 retval = canon;
8470 }
8471 else
8472 {
8473 retval = physname;
8474 need_copy = 0;
8475 }
8476 }
8477 else
8478 retval = canon;
8479
8480 if (need_copy)
10f0c4bb 8481 retval = obstack_copy0 (&objfile->objfile_obstack, retval, strlen (retval));
900e11f9
JK
8482
8483 do_cleanups (back_to);
8484 return retval;
0114d602
DJ
8485}
8486
27aa8d6a
SW
8487/* Read the import statement specified by the given die and record it. */
8488
8489static void
8490read_import_statement (struct die_info *die, struct dwarf2_cu *cu)
8491{
bb5ed363 8492 struct objfile *objfile = cu->objfile;
27aa8d6a 8493 struct attribute *import_attr;
32019081 8494 struct die_info *imported_die, *child_die;
de4affc9 8495 struct dwarf2_cu *imported_cu;
27aa8d6a 8496 const char *imported_name;
794684b6 8497 const char *imported_name_prefix;
13387711
SW
8498 const char *canonical_name;
8499 const char *import_alias;
8500 const char *imported_declaration = NULL;
794684b6 8501 const char *import_prefix;
32019081
JK
8502 VEC (const_char_ptr) *excludes = NULL;
8503 struct cleanup *cleanups;
13387711 8504
27aa8d6a
SW
8505 import_attr = dwarf2_attr (die, DW_AT_import, cu);
8506 if (import_attr == NULL)
8507 {
8508 complaint (&symfile_complaints, _("Tag '%s' has no DW_AT_import"),
8509 dwarf_tag_name (die->tag));
8510 return;
8511 }
8512
de4affc9
CC
8513 imported_cu = cu;
8514 imported_die = follow_die_ref_or_sig (die, import_attr, &imported_cu);
8515 imported_name = dwarf2_name (imported_die, imported_cu);
27aa8d6a
SW
8516 if (imported_name == NULL)
8517 {
8518 /* GCC bug: https://bugzilla.redhat.com/show_bug.cgi?id=506524
8519
8520 The import in the following code:
8521 namespace A
8522 {
8523 typedef int B;
8524 }
8525
8526 int main ()
8527 {
8528 using A::B;
8529 B b;
8530 return b;
8531 }
8532
8533 ...
8534 <2><51>: Abbrev Number: 3 (DW_TAG_imported_declaration)
8535 <52> DW_AT_decl_file : 1
8536 <53> DW_AT_decl_line : 6
8537 <54> DW_AT_import : <0x75>
8538 <2><58>: Abbrev Number: 4 (DW_TAG_typedef)
8539 <59> DW_AT_name : B
8540 <5b> DW_AT_decl_file : 1
8541 <5c> DW_AT_decl_line : 2
8542 <5d> DW_AT_type : <0x6e>
8543 ...
8544 <1><75>: Abbrev Number: 7 (DW_TAG_base_type)
8545 <76> DW_AT_byte_size : 4
8546 <77> DW_AT_encoding : 5 (signed)
8547
8548 imports the wrong die ( 0x75 instead of 0x58 ).
8549 This case will be ignored until the gcc bug is fixed. */
8550 return;
8551 }
8552
82856980
SW
8553 /* Figure out the local name after import. */
8554 import_alias = dwarf2_name (die, cu);
27aa8d6a 8555
794684b6
SW
8556 /* Figure out where the statement is being imported to. */
8557 import_prefix = determine_prefix (die, cu);
8558
8559 /* Figure out what the scope of the imported die is and prepend it
8560 to the name of the imported die. */
de4affc9 8561 imported_name_prefix = determine_prefix (imported_die, imported_cu);
794684b6 8562
f55ee35c
JK
8563 if (imported_die->tag != DW_TAG_namespace
8564 && imported_die->tag != DW_TAG_module)
794684b6 8565 {
13387711
SW
8566 imported_declaration = imported_name;
8567 canonical_name = imported_name_prefix;
794684b6 8568 }
13387711 8569 else if (strlen (imported_name_prefix) > 0)
12aaed36
TT
8570 canonical_name = obconcat (&objfile->objfile_obstack,
8571 imported_name_prefix, "::", imported_name,
8572 (char *) NULL);
13387711
SW
8573 else
8574 canonical_name = imported_name;
794684b6 8575
32019081
JK
8576 cleanups = make_cleanup (VEC_cleanup (const_char_ptr), &excludes);
8577
8578 if (die->tag == DW_TAG_imported_module && cu->language == language_fortran)
8579 for (child_die = die->child; child_die && child_die->tag;
8580 child_die = sibling_die (child_die))
8581 {
8582 /* DWARF-4: A Fortran use statement with a “rename list” may be
8583 represented by an imported module entry with an import attribute
8584 referring to the module and owned entries corresponding to those
8585 entities that are renamed as part of being imported. */
8586
8587 if (child_die->tag != DW_TAG_imported_declaration)
8588 {
8589 complaint (&symfile_complaints,
8590 _("child DW_TAG_imported_declaration expected "
8591 "- DIE at 0x%x [in module %s]"),
4262abfb 8592 child_die->offset.sect_off, objfile_name (objfile));
32019081
JK
8593 continue;
8594 }
8595
8596 import_attr = dwarf2_attr (child_die, DW_AT_import, cu);
8597 if (import_attr == NULL)
8598 {
8599 complaint (&symfile_complaints, _("Tag '%s' has no DW_AT_import"),
8600 dwarf_tag_name (child_die->tag));
8601 continue;
8602 }
8603
8604 imported_cu = cu;
8605 imported_die = follow_die_ref_or_sig (child_die, import_attr,
8606 &imported_cu);
8607 imported_name = dwarf2_name (imported_die, imported_cu);
8608 if (imported_name == NULL)
8609 {
8610 complaint (&symfile_complaints,
8611 _("child DW_TAG_imported_declaration has unknown "
8612 "imported name - DIE at 0x%x [in module %s]"),
4262abfb 8613 child_die->offset.sect_off, objfile_name (objfile));
32019081
JK
8614 continue;
8615 }
8616
8617 VEC_safe_push (const_char_ptr, excludes, imported_name);
8618
8619 process_die (child_die, cu);
8620 }
8621
c0cc3a76
SW
8622 cp_add_using_directive (import_prefix,
8623 canonical_name,
8624 import_alias,
13387711 8625 imported_declaration,
32019081 8626 excludes,
12aaed36 8627 0,
bb5ed363 8628 &objfile->objfile_obstack);
32019081
JK
8629
8630 do_cleanups (cleanups);
27aa8d6a
SW
8631}
8632
f4dc4d17 8633/* Cleanup function for handle_DW_AT_stmt_list. */
ae2de4f8 8634
cb1df416
DJ
8635static void
8636free_cu_line_header (void *arg)
8637{
8638 struct dwarf2_cu *cu = arg;
8639
8640 free_line_header (cu->line_header);
8641 cu->line_header = NULL;
8642}
8643
1b80a9fa
JK
8644/* Check for possibly missing DW_AT_comp_dir with relative .debug_line
8645 directory paths. GCC SVN r127613 (new option -fdebug-prefix-map) fixed
8646 this, it was first present in GCC release 4.3.0. */
8647
8648static int
8649producer_is_gcc_lt_4_3 (struct dwarf2_cu *cu)
8650{
8651 if (!cu->checked_producer)
8652 check_producer (cu);
8653
8654 return cu->producer_is_gcc_lt_4_3;
8655}
8656
9291a0cd
TT
8657static void
8658find_file_and_directory (struct die_info *die, struct dwarf2_cu *cu,
15d034d0 8659 const char **name, const char **comp_dir)
9291a0cd
TT
8660{
8661 struct attribute *attr;
8662
8663 *name = NULL;
8664 *comp_dir = NULL;
8665
8666 /* Find the filename. Do not use dwarf2_name here, since the filename
8667 is not a source language identifier. */
8668 attr = dwarf2_attr (die, DW_AT_name, cu);
8669 if (attr)
8670 {
8671 *name = DW_STRING (attr);
8672 }
8673
8674 attr = dwarf2_attr (die, DW_AT_comp_dir, cu);
8675 if (attr)
8676 *comp_dir = DW_STRING (attr);
1b80a9fa
JK
8677 else if (producer_is_gcc_lt_4_3 (cu) && *name != NULL
8678 && IS_ABSOLUTE_PATH (*name))
9291a0cd 8679 {
15d034d0
TT
8680 char *d = ldirname (*name);
8681
8682 *comp_dir = d;
8683 if (d != NULL)
8684 make_cleanup (xfree, d);
9291a0cd
TT
8685 }
8686 if (*comp_dir != NULL)
8687 {
8688 /* Irix 6.2 native cc prepends <machine>.: to the compilation
8689 directory, get rid of it. */
8690 char *cp = strchr (*comp_dir, ':');
8691
8692 if (cp && cp != *comp_dir && cp[-1] == '.' && cp[1] == '/')
8693 *comp_dir = cp + 1;
8694 }
8695
8696 if (*name == NULL)
8697 *name = "<unknown>";
8698}
8699
f4dc4d17
DE
8700/* Handle DW_AT_stmt_list for a compilation unit.
8701 DIE is the DW_TAG_compile_unit die for CU.
f3f5162e
DE
8702 COMP_DIR is the compilation directory.
8703 WANT_LINE_INFO is non-zero if the pc/line-number mapping is needed. */
2ab95328
TT
8704
8705static void
8706handle_DW_AT_stmt_list (struct die_info *die, struct dwarf2_cu *cu,
b385a60d 8707 const char *comp_dir) /* ARI: editCase function */
2ab95328
TT
8708{
8709 struct attribute *attr;
2ab95328 8710
f4dc4d17
DE
8711 gdb_assert (! cu->per_cu->is_debug_types);
8712
2ab95328
TT
8713 attr = dwarf2_attr (die, DW_AT_stmt_list, cu);
8714 if (attr)
8715 {
8716 unsigned int line_offset = DW_UNSND (attr);
8717 struct line_header *line_header
3019eac3 8718 = dwarf_decode_line_header (line_offset, cu);
2ab95328
TT
8719
8720 if (line_header)
dee91e82
DE
8721 {
8722 cu->line_header = line_header;
8723 make_cleanup (free_cu_line_header, cu);
f4dc4d17 8724 dwarf_decode_lines (line_header, comp_dir, cu, NULL, 1);
dee91e82 8725 }
2ab95328
TT
8726 }
8727}
8728
95554aad 8729/* Process DW_TAG_compile_unit or DW_TAG_partial_unit. */
ae2de4f8 8730
c906108c 8731static void
e7c27a73 8732read_file_scope (struct die_info *die, struct dwarf2_cu *cu)
c906108c 8733{
dee91e82 8734 struct objfile *objfile = dwarf2_per_objfile->objfile;
debd256d 8735 struct cleanup *back_to = make_cleanup (null_cleanup, 0);
2acceee2 8736 CORE_ADDR lowpc = ((CORE_ADDR) -1);
c906108c
SS
8737 CORE_ADDR highpc = ((CORE_ADDR) 0);
8738 struct attribute *attr;
15d034d0
TT
8739 const char *name = NULL;
8740 const char *comp_dir = NULL;
c906108c
SS
8741 struct die_info *child_die;
8742 bfd *abfd = objfile->obfd;
e142c38c 8743 CORE_ADDR baseaddr;
6e70227d 8744
e142c38c 8745 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
c906108c 8746
fae299cd 8747 get_scope_pc_bounds (die, &lowpc, &highpc, cu);
c906108c
SS
8748
8749 /* If we didn't find a lowpc, set it to highpc to avoid complaints
8750 from finish_block. */
2acceee2 8751 if (lowpc == ((CORE_ADDR) -1))
c906108c
SS
8752 lowpc = highpc;
8753 lowpc += baseaddr;
8754 highpc += baseaddr;
8755
9291a0cd 8756 find_file_and_directory (die, cu, &name, &comp_dir);
e1024ff1 8757
95554aad 8758 prepare_one_comp_unit (cu, die, cu->language);
303b6f5d 8759
f4b8a18d
KW
8760 /* The XLCL doesn't generate DW_LANG_OpenCL because this attribute is not
8761 standardised yet. As a workaround for the language detection we fall
8762 back to the DW_AT_producer string. */
8763 if (cu->producer && strstr (cu->producer, "IBM XL C for OpenCL") != NULL)
8764 cu->language = language_opencl;
8765
3019eac3
DE
8766 /* Similar hack for Go. */
8767 if (cu->producer && strstr (cu->producer, "GNU Go ") != NULL)
8768 set_cu_language (DW_LANG_Go, cu);
8769
f4dc4d17 8770 dwarf2_start_symtab (cu, name, comp_dir, lowpc);
3019eac3
DE
8771
8772 /* Decode line number information if present. We do this before
8773 processing child DIEs, so that the line header table is available
8774 for DW_AT_decl_file. */
f4dc4d17 8775 handle_DW_AT_stmt_list (die, cu, comp_dir);
3019eac3
DE
8776
8777 /* Process all dies in compilation unit. */
8778 if (die->child != NULL)
8779 {
8780 child_die = die->child;
8781 while (child_die && child_die->tag)
8782 {
8783 process_die (child_die, cu);
8784 child_die = sibling_die (child_die);
8785 }
8786 }
8787
8788 /* Decode macro information, if present. Dwarf 2 macro information
8789 refers to information in the line number info statement program
8790 header, so we can only read it if we've read the header
8791 successfully. */
8792 attr = dwarf2_attr (die, DW_AT_GNU_macros, cu);
8793 if (attr && cu->line_header)
8794 {
8795 if (dwarf2_attr (die, DW_AT_macro_info, cu))
8796 complaint (&symfile_complaints,
8797 _("CU refers to both DW_AT_GNU_macros and DW_AT_macro_info"));
8798
09262596 8799 dwarf_decode_macros (cu, DW_UNSND (attr), comp_dir, 1);
3019eac3
DE
8800 }
8801 else
8802 {
8803 attr = dwarf2_attr (die, DW_AT_macro_info, cu);
8804 if (attr && cu->line_header)
8805 {
8806 unsigned int macro_offset = DW_UNSND (attr);
8807
09262596 8808 dwarf_decode_macros (cu, macro_offset, comp_dir, 0);
3019eac3
DE
8809 }
8810 }
8811
8812 do_cleanups (back_to);
8813}
8814
f4dc4d17
DE
8815/* TU version of handle_DW_AT_stmt_list for read_type_unit_scope.
8816 Create the set of symtabs used by this TU, or if this TU is sharing
8817 symtabs with another TU and the symtabs have already been created
8818 then restore those symtabs in the line header.
8819 We don't need the pc/line-number mapping for type units. */
3019eac3
DE
8820
8821static void
f4dc4d17 8822setup_type_unit_groups (struct die_info *die, struct dwarf2_cu *cu)
3019eac3 8823{
f4dc4d17
DE
8824 struct objfile *objfile = dwarf2_per_objfile->objfile;
8825 struct dwarf2_per_cu_data *per_cu = cu->per_cu;
8826 struct type_unit_group *tu_group;
8827 int first_time;
8828 struct line_header *lh;
3019eac3 8829 struct attribute *attr;
f4dc4d17 8830 unsigned int i, line_offset;
0186c6a7 8831 struct signatured_type *sig_type;
3019eac3 8832
f4dc4d17 8833 gdb_assert (per_cu->is_debug_types);
0186c6a7 8834 sig_type = (struct signatured_type *) per_cu;
3019eac3 8835
f4dc4d17 8836 attr = dwarf2_attr (die, DW_AT_stmt_list, cu);
3019eac3 8837
f4dc4d17 8838 /* If we're using .gdb_index (includes -readnow) then
74e04d1c 8839 per_cu->type_unit_group may not have been set up yet. */
0186c6a7
DE
8840 if (sig_type->type_unit_group == NULL)
8841 sig_type->type_unit_group = get_type_unit_group (cu, attr);
8842 tu_group = sig_type->type_unit_group;
f4dc4d17
DE
8843
8844 /* If we've already processed this stmt_list there's no real need to
8845 do it again, we could fake it and just recreate the part we need
8846 (file name,index -> symtab mapping). If data shows this optimization
8847 is useful we can do it then. */
8848 first_time = tu_group->primary_symtab == NULL;
8849
8850 /* We have to handle the case of both a missing DW_AT_stmt_list or bad
8851 debug info. */
8852 lh = NULL;
8853 if (attr != NULL)
3019eac3 8854 {
f4dc4d17
DE
8855 line_offset = DW_UNSND (attr);
8856 lh = dwarf_decode_line_header (line_offset, cu);
8857 }
8858 if (lh == NULL)
8859 {
8860 if (first_time)
8861 dwarf2_start_symtab (cu, "", NULL, 0);
8862 else
8863 {
8864 gdb_assert (tu_group->symtabs == NULL);
8865 restart_symtab (0);
8866 }
8867 /* Note: The primary symtab will get allocated at the end. */
8868 return;
3019eac3
DE
8869 }
8870
f4dc4d17
DE
8871 cu->line_header = lh;
8872 make_cleanup (free_cu_line_header, cu);
3019eac3 8873
f4dc4d17
DE
8874 if (first_time)
8875 {
8876 dwarf2_start_symtab (cu, "", NULL, 0);
3019eac3 8877
f4dc4d17
DE
8878 tu_group->num_symtabs = lh->num_file_names;
8879 tu_group->symtabs = XNEWVEC (struct symtab *, lh->num_file_names);
3019eac3 8880
f4dc4d17
DE
8881 for (i = 0; i < lh->num_file_names; ++i)
8882 {
d521ce57 8883 const char *dir = NULL;
f4dc4d17 8884 struct file_entry *fe = &lh->file_names[i];
3019eac3 8885
f4dc4d17
DE
8886 if (fe->dir_index)
8887 dir = lh->include_dirs[fe->dir_index - 1];
8888 dwarf2_start_subfile (fe->name, dir, NULL);
3019eac3 8889
f4dc4d17
DE
8890 /* Note: We don't have to watch for the main subfile here, type units
8891 don't have DW_AT_name. */
3019eac3 8892
f4dc4d17
DE
8893 if (current_subfile->symtab == NULL)
8894 {
8895 /* NOTE: start_subfile will recognize when it's been passed
8896 a file it has already seen. So we can't assume there's a
8897 simple mapping from lh->file_names to subfiles,
8898 lh->file_names may contain dups. */
8899 current_subfile->symtab = allocate_symtab (current_subfile->name,
8900 objfile);
8901 }
8902
8903 fe->symtab = current_subfile->symtab;
8904 tu_group->symtabs[i] = fe->symtab;
8905 }
8906 }
8907 else
3019eac3 8908 {
f4dc4d17
DE
8909 restart_symtab (0);
8910
8911 for (i = 0; i < lh->num_file_names; ++i)
8912 {
8913 struct file_entry *fe = &lh->file_names[i];
8914
8915 fe->symtab = tu_group->symtabs[i];
8916 }
3019eac3
DE
8917 }
8918
f4dc4d17
DE
8919 /* The main symtab is allocated last. Type units don't have DW_AT_name
8920 so they don't have a "real" (so to speak) symtab anyway.
8921 There is later code that will assign the main symtab to all symbols
8922 that don't have one. We need to handle the case of a symbol with a
8923 missing symtab (DW_AT_decl_file) anyway. */
8924}
3019eac3 8925
f4dc4d17
DE
8926/* Process DW_TAG_type_unit.
8927 For TUs we want to skip the first top level sibling if it's not the
8928 actual type being defined by this TU. In this case the first top
8929 level sibling is there to provide context only. */
3019eac3 8930
f4dc4d17
DE
8931static void
8932read_type_unit_scope (struct die_info *die, struct dwarf2_cu *cu)
8933{
8934 struct die_info *child_die;
3019eac3 8935
f4dc4d17
DE
8936 prepare_one_comp_unit (cu, die, language_minimal);
8937
8938 /* Initialize (or reinitialize) the machinery for building symtabs.
8939 We do this before processing child DIEs, so that the line header table
8940 is available for DW_AT_decl_file. */
8941 setup_type_unit_groups (die, cu);
8942
8943 if (die->child != NULL)
8944 {
8945 child_die = die->child;
8946 while (child_die && child_die->tag)
8947 {
8948 process_die (child_die, cu);
8949 child_die = sibling_die (child_die);
8950 }
8951 }
3019eac3
DE
8952}
8953\f
80626a55
DE
8954/* DWO/DWP files.
8955
8956 http://gcc.gnu.org/wiki/DebugFission
8957 http://gcc.gnu.org/wiki/DebugFissionDWP
8958
8959 To simplify handling of both DWO files ("object" files with the DWARF info)
8960 and DWP files (a file with the DWOs packaged up into one file), we treat
8961 DWP files as having a collection of virtual DWO files. */
3019eac3
DE
8962
8963static hashval_t
8964hash_dwo_file (const void *item)
8965{
8966 const struct dwo_file *dwo_file = item;
a2ce51a0 8967 hashval_t hash;
3019eac3 8968
a2ce51a0
DE
8969 hash = htab_hash_string (dwo_file->dwo_name);
8970 if (dwo_file->comp_dir != NULL)
8971 hash += htab_hash_string (dwo_file->comp_dir);
8972 return hash;
3019eac3
DE
8973}
8974
8975static int
8976eq_dwo_file (const void *item_lhs, const void *item_rhs)
8977{
8978 const struct dwo_file *lhs = item_lhs;
8979 const struct dwo_file *rhs = item_rhs;
8980
a2ce51a0
DE
8981 if (strcmp (lhs->dwo_name, rhs->dwo_name) != 0)
8982 return 0;
8983 if (lhs->comp_dir == NULL || rhs->comp_dir == NULL)
8984 return lhs->comp_dir == rhs->comp_dir;
8985 return strcmp (lhs->comp_dir, rhs->comp_dir) == 0;
3019eac3
DE
8986}
8987
8988/* Allocate a hash table for DWO files. */
8989
8990static htab_t
8991allocate_dwo_file_hash_table (void)
8992{
8993 struct objfile *objfile = dwarf2_per_objfile->objfile;
8994
8995 return htab_create_alloc_ex (41,
8996 hash_dwo_file,
8997 eq_dwo_file,
8998 NULL,
8999 &objfile->objfile_obstack,
9000 hashtab_obstack_allocate,
9001 dummy_obstack_deallocate);
9002}
9003
80626a55
DE
9004/* Lookup DWO file DWO_NAME. */
9005
9006static void **
0ac5b59e 9007lookup_dwo_file_slot (const char *dwo_name, const char *comp_dir)
80626a55
DE
9008{
9009 struct dwo_file find_entry;
9010 void **slot;
9011
9012 if (dwarf2_per_objfile->dwo_files == NULL)
9013 dwarf2_per_objfile->dwo_files = allocate_dwo_file_hash_table ();
9014
9015 memset (&find_entry, 0, sizeof (find_entry));
0ac5b59e
DE
9016 find_entry.dwo_name = dwo_name;
9017 find_entry.comp_dir = comp_dir;
80626a55
DE
9018 slot = htab_find_slot (dwarf2_per_objfile->dwo_files, &find_entry, INSERT);
9019
9020 return slot;
9021}
9022
3019eac3
DE
9023static hashval_t
9024hash_dwo_unit (const void *item)
9025{
9026 const struct dwo_unit *dwo_unit = item;
9027
9028 /* This drops the top 32 bits of the id, but is ok for a hash. */
9029 return dwo_unit->signature;
9030}
9031
9032static int
9033eq_dwo_unit (const void *item_lhs, const void *item_rhs)
9034{
9035 const struct dwo_unit *lhs = item_lhs;
9036 const struct dwo_unit *rhs = item_rhs;
9037
9038 /* The signature is assumed to be unique within the DWO file.
9039 So while object file CU dwo_id's always have the value zero,
9040 that's OK, assuming each object file DWO file has only one CU,
9041 and that's the rule for now. */
9042 return lhs->signature == rhs->signature;
9043}
9044
9045/* Allocate a hash table for DWO CUs,TUs.
9046 There is one of these tables for each of CUs,TUs for each DWO file. */
9047
9048static htab_t
9049allocate_dwo_unit_table (struct objfile *objfile)
9050{
9051 /* Start out with a pretty small number.
9052 Generally DWO files contain only one CU and maybe some TUs. */
9053 return htab_create_alloc_ex (3,
9054 hash_dwo_unit,
9055 eq_dwo_unit,
9056 NULL,
9057 &objfile->objfile_obstack,
9058 hashtab_obstack_allocate,
9059 dummy_obstack_deallocate);
9060}
9061
80626a55 9062/* Structure used to pass data to create_dwo_debug_info_hash_table_reader. */
3019eac3 9063
19c3d4c9 9064struct create_dwo_cu_data
3019eac3
DE
9065{
9066 struct dwo_file *dwo_file;
19c3d4c9 9067 struct dwo_unit dwo_unit;
3019eac3
DE
9068};
9069
19c3d4c9 9070/* die_reader_func for create_dwo_cu. */
3019eac3
DE
9071
9072static void
19c3d4c9
DE
9073create_dwo_cu_reader (const struct die_reader_specs *reader,
9074 const gdb_byte *info_ptr,
9075 struct die_info *comp_unit_die,
9076 int has_children,
9077 void *datap)
3019eac3
DE
9078{
9079 struct dwarf2_cu *cu = reader->cu;
9080 struct objfile *objfile = dwarf2_per_objfile->objfile;
9081 sect_offset offset = cu->per_cu->offset;
8a0459fd 9082 struct dwarf2_section_info *section = cu->per_cu->section;
19c3d4c9 9083 struct create_dwo_cu_data *data = datap;
3019eac3 9084 struct dwo_file *dwo_file = data->dwo_file;
19c3d4c9 9085 struct dwo_unit *dwo_unit = &data->dwo_unit;
3019eac3 9086 struct attribute *attr;
3019eac3
DE
9087
9088 attr = dwarf2_attr (comp_unit_die, DW_AT_GNU_dwo_id, cu);
9089 if (attr == NULL)
9090 {
19c3d4c9
DE
9091 complaint (&symfile_complaints,
9092 _("Dwarf Error: debug entry at offset 0x%x is missing"
9093 " its dwo_id [in module %s]"),
9094 offset.sect_off, dwo_file->dwo_name);
3019eac3
DE
9095 return;
9096 }
9097
3019eac3
DE
9098 dwo_unit->dwo_file = dwo_file;
9099 dwo_unit->signature = DW_UNSND (attr);
8a0459fd 9100 dwo_unit->section = section;
3019eac3
DE
9101 dwo_unit->offset = offset;
9102 dwo_unit->length = cu->per_cu->length;
9103
09406207 9104 if (dwarf2_read_debug)
4031ecc5
DE
9105 fprintf_unfiltered (gdb_stdlog, " offset 0x%x, dwo_id %s\n",
9106 offset.sect_off, hex_string (dwo_unit->signature));
3019eac3
DE
9107}
9108
19c3d4c9
DE
9109/* Create the dwo_unit for the lone CU in DWO_FILE.
9110 Note: This function processes DWO files only, not DWP files. */
3019eac3 9111
19c3d4c9
DE
9112static struct dwo_unit *
9113create_dwo_cu (struct dwo_file *dwo_file)
3019eac3
DE
9114{
9115 struct objfile *objfile = dwarf2_per_objfile->objfile;
9116 struct dwarf2_section_info *section = &dwo_file->sections.info;
9117 bfd *abfd;
9118 htab_t cu_htab;
d521ce57 9119 const gdb_byte *info_ptr, *end_ptr;
19c3d4c9
DE
9120 struct create_dwo_cu_data create_dwo_cu_data;
9121 struct dwo_unit *dwo_unit;
3019eac3
DE
9122
9123 dwarf2_read_section (objfile, section);
9124 info_ptr = section->buffer;
9125
9126 if (info_ptr == NULL)
9127 return NULL;
9128
9129 /* We can't set abfd until now because the section may be empty or
9130 not present, in which case section->asection will be NULL. */
a32a8923 9131 abfd = get_section_bfd_owner (section);
3019eac3 9132
09406207 9133 if (dwarf2_read_debug)
19c3d4c9
DE
9134 {
9135 fprintf_unfiltered (gdb_stdlog, "Reading %s for %s:\n",
a32a8923
DE
9136 get_section_name (section),
9137 get_section_file_name (section));
19c3d4c9 9138 }
3019eac3 9139
19c3d4c9
DE
9140 create_dwo_cu_data.dwo_file = dwo_file;
9141 dwo_unit = NULL;
3019eac3
DE
9142
9143 end_ptr = info_ptr + section->size;
9144 while (info_ptr < end_ptr)
9145 {
9146 struct dwarf2_per_cu_data per_cu;
9147
19c3d4c9
DE
9148 memset (&create_dwo_cu_data.dwo_unit, 0,
9149 sizeof (create_dwo_cu_data.dwo_unit));
3019eac3
DE
9150 memset (&per_cu, 0, sizeof (per_cu));
9151 per_cu.objfile = objfile;
9152 per_cu.is_debug_types = 0;
9153 per_cu.offset.sect_off = info_ptr - section->buffer;
8a0459fd 9154 per_cu.section = section;
3019eac3
DE
9155
9156 init_cutu_and_read_dies_no_follow (&per_cu,
9157 &dwo_file->sections.abbrev,
9158 dwo_file,
19c3d4c9
DE
9159 create_dwo_cu_reader,
9160 &create_dwo_cu_data);
9161
9162 if (create_dwo_cu_data.dwo_unit.dwo_file != NULL)
9163 {
9164 /* If we've already found one, complain. We only support one
9165 because having more than one requires hacking the dwo_name of
9166 each to match, which is highly unlikely to happen. */
9167 if (dwo_unit != NULL)
9168 {
9169 complaint (&symfile_complaints,
9170 _("Multiple CUs in DWO file %s [in module %s]"),
4262abfb 9171 dwo_file->dwo_name, objfile_name (objfile));
19c3d4c9
DE
9172 break;
9173 }
9174
9175 dwo_unit = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct dwo_unit);
9176 *dwo_unit = create_dwo_cu_data.dwo_unit;
9177 }
3019eac3
DE
9178
9179 info_ptr += per_cu.length;
9180 }
9181
19c3d4c9 9182 return dwo_unit;
3019eac3
DE
9183}
9184
80626a55
DE
9185/* DWP file .debug_{cu,tu}_index section format:
9186 [ref: http://gcc.gnu.org/wiki/DebugFissionDWP]
9187
d2415c6c
DE
9188 DWP Version 1:
9189
80626a55
DE
9190 Both index sections have the same format, and serve to map a 64-bit
9191 signature to a set of section numbers. Each section begins with a header,
9192 followed by a hash table of 64-bit signatures, a parallel table of 32-bit
9193 indexes, and a pool of 32-bit section numbers. The index sections will be
9194 aligned at 8-byte boundaries in the file.
9195
d2415c6c
DE
9196 The index section header consists of:
9197
9198 V, 32 bit version number
9199 -, 32 bits unused
9200 N, 32 bit number of compilation units or type units in the index
9201 M, 32 bit number of slots in the hash table
80626a55 9202
d2415c6c 9203 Numbers are recorded using the byte order of the application binary.
80626a55 9204
d2415c6c
DE
9205 The hash table begins at offset 16 in the section, and consists of an array
9206 of M 64-bit slots. Each slot contains a 64-bit signature (using the byte
9207 order of the application binary). Unused slots in the hash table are 0.
9208 (We rely on the extreme unlikeliness of a signature being exactly 0.)
80626a55 9209
d2415c6c
DE
9210 The parallel table begins immediately after the hash table
9211 (at offset 16 + 8 * M from the beginning of the section), and consists of an
9212 array of 32-bit indexes (using the byte order of the application binary),
9213 corresponding 1-1 with slots in the hash table. Each entry in the parallel
9214 table contains a 32-bit index into the pool of section numbers. For unused
9215 hash table slots, the corresponding entry in the parallel table will be 0.
80626a55 9216
73869dc2
DE
9217 The pool of section numbers begins immediately following the hash table
9218 (at offset 16 + 12 * M from the beginning of the section). The pool of
9219 section numbers consists of an array of 32-bit words (using the byte order
9220 of the application binary). Each item in the array is indexed starting
9221 from 0. The hash table entry provides the index of the first section
9222 number in the set. Additional section numbers in the set follow, and the
9223 set is terminated by a 0 entry (section number 0 is not used in ELF).
9224
9225 In each set of section numbers, the .debug_info.dwo or .debug_types.dwo
9226 section must be the first entry in the set, and the .debug_abbrev.dwo must
9227 be the second entry. Other members of the set may follow in any order.
9228
9229 ---
9230
9231 DWP Version 2:
9232
9233 DWP Version 2 combines all the .debug_info, etc. sections into one,
9234 and the entries in the index tables are now offsets into these sections.
9235 CU offsets begin at 0. TU offsets begin at the size of the .debug_info
9236 section.
9237
9238 Index Section Contents:
9239 Header
9240 Hash Table of Signatures dwp_hash_table.hash_table
9241 Parallel Table of Indices dwp_hash_table.unit_table
9242 Table of Section Offsets dwp_hash_table.v2.{section_ids,offsets}
9243 Table of Section Sizes dwp_hash_table.v2.sizes
9244
9245 The index section header consists of:
9246
9247 V, 32 bit version number
9248 L, 32 bit number of columns in the table of section offsets
9249 N, 32 bit number of compilation units or type units in the index
9250 M, 32 bit number of slots in the hash table
9251
9252 Numbers are recorded using the byte order of the application binary.
9253
9254 The hash table has the same format as version 1.
9255 The parallel table of indices has the same format as version 1,
9256 except that the entries are origin-1 indices into the table of sections
9257 offsets and the table of section sizes.
9258
9259 The table of offsets begins immediately following the parallel table
9260 (at offset 16 + 12 * M from the beginning of the section). The table is
9261 a two-dimensional array of 32-bit words (using the byte order of the
9262 application binary), with L columns and N+1 rows, in row-major order.
9263 Each row in the array is indexed starting from 0. The first row provides
9264 a key to the remaining rows: each column in this row provides an identifier
9265 for a debug section, and the offsets in the same column of subsequent rows
9266 refer to that section. The section identifiers are:
9267
9268 DW_SECT_INFO 1 .debug_info.dwo
9269 DW_SECT_TYPES 2 .debug_types.dwo
9270 DW_SECT_ABBREV 3 .debug_abbrev.dwo
9271 DW_SECT_LINE 4 .debug_line.dwo
9272 DW_SECT_LOC 5 .debug_loc.dwo
9273 DW_SECT_STR_OFFSETS 6 .debug_str_offsets.dwo
9274 DW_SECT_MACINFO 7 .debug_macinfo.dwo
9275 DW_SECT_MACRO 8 .debug_macro.dwo
9276
9277 The offsets provided by the CU and TU index sections are the base offsets
9278 for the contributions made by each CU or TU to the corresponding section
9279 in the package file. Each CU and TU header contains an abbrev_offset
9280 field, used to find the abbreviations table for that CU or TU within the
9281 contribution to the .debug_abbrev.dwo section for that CU or TU, and should
9282 be interpreted as relative to the base offset given in the index section.
9283 Likewise, offsets into .debug_line.dwo from DW_AT_stmt_list attributes
9284 should be interpreted as relative to the base offset for .debug_line.dwo,
9285 and offsets into other debug sections obtained from DWARF attributes should
9286 also be interpreted as relative to the corresponding base offset.
9287
9288 The table of sizes begins immediately following the table of offsets.
9289 Like the table of offsets, it is a two-dimensional array of 32-bit words,
9290 with L columns and N rows, in row-major order. Each row in the array is
9291 indexed starting from 1 (row 0 is shared by the two tables).
9292
9293 ---
9294
9295 Hash table lookup is handled the same in version 1 and 2:
9296
9297 We assume that N and M will not exceed 2^32 - 1.
9298 The size of the hash table, M, must be 2^k such that 2^k > 3*N/2.
9299
d2415c6c
DE
9300 Given a 64-bit compilation unit signature or a type signature S, an entry
9301 in the hash table is located as follows:
80626a55 9302
d2415c6c
DE
9303 1) Calculate a primary hash H = S & MASK(k), where MASK(k) is a mask with
9304 the low-order k bits all set to 1.
80626a55 9305
d2415c6c 9306 2) Calculate a secondary hash H' = (((S >> 32) & MASK(k)) | 1).
80626a55 9307
d2415c6c
DE
9308 3) If the hash table entry at index H matches the signature, use that
9309 entry. If the hash table entry at index H is unused (all zeroes),
9310 terminate the search: the signature is not present in the table.
80626a55 9311
d2415c6c 9312 4) Let H = (H + H') modulo M. Repeat at Step 3.
80626a55 9313
d2415c6c 9314 Because M > N and H' and M are relatively prime, the search is guaranteed
73869dc2 9315 to stop at an unused slot or find the match. */
80626a55
DE
9316
9317/* Create a hash table to map DWO IDs to their CU/TU entry in
9318 .debug_{info,types}.dwo in DWP_FILE.
9319 Returns NULL if there isn't one.
9320 Note: This function processes DWP files only, not DWO files. */
9321
9322static struct dwp_hash_table *
9323create_dwp_hash_table (struct dwp_file *dwp_file, int is_debug_types)
9324{
9325 struct objfile *objfile = dwarf2_per_objfile->objfile;
9326 bfd *dbfd = dwp_file->dbfd;
948f8e3d 9327 const gdb_byte *index_ptr, *index_end;
80626a55 9328 struct dwarf2_section_info *index;
73869dc2 9329 uint32_t version, nr_columns, nr_units, nr_slots;
80626a55
DE
9330 struct dwp_hash_table *htab;
9331
9332 if (is_debug_types)
9333 index = &dwp_file->sections.tu_index;
9334 else
9335 index = &dwp_file->sections.cu_index;
9336
9337 if (dwarf2_section_empty_p (index))
9338 return NULL;
9339 dwarf2_read_section (objfile, index);
9340
9341 index_ptr = index->buffer;
9342 index_end = index_ptr + index->size;
9343
9344 version = read_4_bytes (dbfd, index_ptr);
73869dc2
DE
9345 index_ptr += 4;
9346 if (version == 2)
9347 nr_columns = read_4_bytes (dbfd, index_ptr);
9348 else
9349 nr_columns = 0;
9350 index_ptr += 4;
80626a55
DE
9351 nr_units = read_4_bytes (dbfd, index_ptr);
9352 index_ptr += 4;
9353 nr_slots = read_4_bytes (dbfd, index_ptr);
9354 index_ptr += 4;
9355
73869dc2 9356 if (version != 1 && version != 2)
80626a55 9357 {
21aa081e 9358 error (_("Dwarf Error: unsupported DWP file version (%s)"
80626a55 9359 " [in module %s]"),
21aa081e 9360 pulongest (version), dwp_file->name);
80626a55
DE
9361 }
9362 if (nr_slots != (nr_slots & -nr_slots))
9363 {
21aa081e 9364 error (_("Dwarf Error: number of slots in DWP hash table (%s)"
80626a55 9365 " is not power of 2 [in module %s]"),
21aa081e 9366 pulongest (nr_slots), dwp_file->name);
80626a55
DE
9367 }
9368
9369 htab = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct dwp_hash_table);
73869dc2
DE
9370 htab->version = version;
9371 htab->nr_columns = nr_columns;
80626a55
DE
9372 htab->nr_units = nr_units;
9373 htab->nr_slots = nr_slots;
9374 htab->hash_table = index_ptr;
9375 htab->unit_table = htab->hash_table + sizeof (uint64_t) * nr_slots;
73869dc2
DE
9376
9377 /* Exit early if the table is empty. */
9378 if (nr_slots == 0 || nr_units == 0
9379 || (version == 2 && nr_columns == 0))
9380 {
9381 /* All must be zero. */
9382 if (nr_slots != 0 || nr_units != 0
9383 || (version == 2 && nr_columns != 0))
9384 {
9385 complaint (&symfile_complaints,
9386 _("Empty DWP but nr_slots,nr_units,nr_columns not"
9387 " all zero [in modules %s]"),
9388 dwp_file->name);
9389 }
9390 return htab;
9391 }
9392
9393 if (version == 1)
9394 {
9395 htab->section_pool.v1.indices =
9396 htab->unit_table + sizeof (uint32_t) * nr_slots;
9397 /* It's harder to decide whether the section is too small in v1.
9398 V1 is deprecated anyway so we punt. */
9399 }
9400 else
9401 {
9402 const gdb_byte *ids_ptr = htab->unit_table + sizeof (uint32_t) * nr_slots;
9403 int *ids = htab->section_pool.v2.section_ids;
9404 /* Reverse map for error checking. */
9405 int ids_seen[DW_SECT_MAX + 1];
9406 int i;
9407
9408 if (nr_columns < 2)
9409 {
9410 error (_("Dwarf Error: bad DWP hash table, too few columns"
9411 " in section table [in module %s]"),
9412 dwp_file->name);
9413 }
9414 if (nr_columns > MAX_NR_V2_DWO_SECTIONS)
9415 {
9416 error (_("Dwarf Error: bad DWP hash table, too many columns"
9417 " in section table [in module %s]"),
9418 dwp_file->name);
9419 }
9420 memset (ids, 255, (DW_SECT_MAX + 1) * sizeof (int32_t));
9421 memset (ids_seen, 255, (DW_SECT_MAX + 1) * sizeof (int32_t));
9422 for (i = 0; i < nr_columns; ++i)
9423 {
9424 int id = read_4_bytes (dbfd, ids_ptr + i * sizeof (uint32_t));
9425
9426 if (id < DW_SECT_MIN || id > DW_SECT_MAX)
9427 {
9428 error (_("Dwarf Error: bad DWP hash table, bad section id %d"
9429 " in section table [in module %s]"),
9430 id, dwp_file->name);
9431 }
9432 if (ids_seen[id] != -1)
9433 {
9434 error (_("Dwarf Error: bad DWP hash table, duplicate section"
9435 " id %d in section table [in module %s]"),
9436 id, dwp_file->name);
9437 }
9438 ids_seen[id] = i;
9439 ids[i] = id;
9440 }
9441 /* Must have exactly one info or types section. */
9442 if (((ids_seen[DW_SECT_INFO] != -1)
9443 + (ids_seen[DW_SECT_TYPES] != -1))
9444 != 1)
9445 {
9446 error (_("Dwarf Error: bad DWP hash table, missing/duplicate"
9447 " DWO info/types section [in module %s]"),
9448 dwp_file->name);
9449 }
9450 /* Must have an abbrev section. */
9451 if (ids_seen[DW_SECT_ABBREV] == -1)
9452 {
9453 error (_("Dwarf Error: bad DWP hash table, missing DWO abbrev"
9454 " section [in module %s]"),
9455 dwp_file->name);
9456 }
9457 htab->section_pool.v2.offsets = ids_ptr + sizeof (uint32_t) * nr_columns;
9458 htab->section_pool.v2.sizes =
9459 htab->section_pool.v2.offsets + (sizeof (uint32_t)
9460 * nr_units * nr_columns);
9461 if ((htab->section_pool.v2.sizes + (sizeof (uint32_t)
9462 * nr_units * nr_columns))
9463 > index_end)
9464 {
9465 error (_("Dwarf Error: DWP index section is corrupt (too small)"
9466 " [in module %s]"),
9467 dwp_file->name);
9468 }
9469 }
80626a55
DE
9470
9471 return htab;
9472}
9473
9474/* Update SECTIONS with the data from SECTP.
9475
9476 This function is like the other "locate" section routines that are
9477 passed to bfd_map_over_sections, but in this context the sections to
73869dc2 9478 read comes from the DWP V1 hash table, not the full ELF section table.
80626a55
DE
9479
9480 The result is non-zero for success, or zero if an error was found. */
9481
9482static int
73869dc2
DE
9483locate_v1_virtual_dwo_sections (asection *sectp,
9484 struct virtual_v1_dwo_sections *sections)
80626a55
DE
9485{
9486 const struct dwop_section_names *names = &dwop_section_names;
9487
9488 if (section_is_p (sectp->name, &names->abbrev_dwo))
9489 {
9490 /* There can be only one. */
73869dc2 9491 if (sections->abbrev.s.asection != NULL)
80626a55 9492 return 0;
73869dc2 9493 sections->abbrev.s.asection = sectp;
80626a55
DE
9494 sections->abbrev.size = bfd_get_section_size (sectp);
9495 }
9496 else if (section_is_p (sectp->name, &names->info_dwo)
9497 || section_is_p (sectp->name, &names->types_dwo))
9498 {
9499 /* There can be only one. */
73869dc2 9500 if (sections->info_or_types.s.asection != NULL)
80626a55 9501 return 0;
73869dc2 9502 sections->info_or_types.s.asection = sectp;
80626a55
DE
9503 sections->info_or_types.size = bfd_get_section_size (sectp);
9504 }
9505 else if (section_is_p (sectp->name, &names->line_dwo))
9506 {
9507 /* There can be only one. */
73869dc2 9508 if (sections->line.s.asection != NULL)
80626a55 9509 return 0;
73869dc2 9510 sections->line.s.asection = sectp;
80626a55
DE
9511 sections->line.size = bfd_get_section_size (sectp);
9512 }
9513 else if (section_is_p (sectp->name, &names->loc_dwo))
9514 {
9515 /* There can be only one. */
73869dc2 9516 if (sections->loc.s.asection != NULL)
80626a55 9517 return 0;
73869dc2 9518 sections->loc.s.asection = sectp;
80626a55
DE
9519 sections->loc.size = bfd_get_section_size (sectp);
9520 }
9521 else if (section_is_p (sectp->name, &names->macinfo_dwo))
9522 {
9523 /* There can be only one. */
73869dc2 9524 if (sections->macinfo.s.asection != NULL)
80626a55 9525 return 0;
73869dc2 9526 sections->macinfo.s.asection = sectp;
80626a55
DE
9527 sections->macinfo.size = bfd_get_section_size (sectp);
9528 }
9529 else if (section_is_p (sectp->name, &names->macro_dwo))
9530 {
9531 /* There can be only one. */
73869dc2 9532 if (sections->macro.s.asection != NULL)
80626a55 9533 return 0;
73869dc2 9534 sections->macro.s.asection = sectp;
80626a55
DE
9535 sections->macro.size = bfd_get_section_size (sectp);
9536 }
9537 else if (section_is_p (sectp->name, &names->str_offsets_dwo))
9538 {
9539 /* There can be only one. */
73869dc2 9540 if (sections->str_offsets.s.asection != NULL)
80626a55 9541 return 0;
73869dc2 9542 sections->str_offsets.s.asection = sectp;
80626a55
DE
9543 sections->str_offsets.size = bfd_get_section_size (sectp);
9544 }
9545 else
9546 {
9547 /* No other kind of section is valid. */
9548 return 0;
9549 }
9550
9551 return 1;
9552}
9553
73869dc2
DE
9554/* Create a dwo_unit object for the DWO unit with signature SIGNATURE.
9555 UNIT_INDEX is the index of the DWO unit in the DWP hash table.
9556 COMP_DIR is the DW_AT_comp_dir attribute of the referencing CU.
9557 This is for DWP version 1 files. */
80626a55
DE
9558
9559static struct dwo_unit *
73869dc2
DE
9560create_dwo_unit_in_dwp_v1 (struct dwp_file *dwp_file,
9561 uint32_t unit_index,
9562 const char *comp_dir,
9563 ULONGEST signature, int is_debug_types)
80626a55
DE
9564{
9565 struct objfile *objfile = dwarf2_per_objfile->objfile;
73869dc2
DE
9566 const struct dwp_hash_table *dwp_htab =
9567 is_debug_types ? dwp_file->tus : dwp_file->cus;
80626a55
DE
9568 bfd *dbfd = dwp_file->dbfd;
9569 const char *kind = is_debug_types ? "TU" : "CU";
9570 struct dwo_file *dwo_file;
9571 struct dwo_unit *dwo_unit;
73869dc2 9572 struct virtual_v1_dwo_sections sections;
80626a55
DE
9573 void **dwo_file_slot;
9574 char *virtual_dwo_name;
9575 struct dwarf2_section_info *cutu;
9576 struct cleanup *cleanups;
9577 int i;
9578
73869dc2
DE
9579 gdb_assert (dwp_file->version == 1);
9580
80626a55
DE
9581 if (dwarf2_read_debug)
9582 {
73869dc2 9583 fprintf_unfiltered (gdb_stdlog, "Reading %s %s/%s in DWP V1 file: %s\n",
80626a55 9584 kind,
73869dc2 9585 pulongest (unit_index), hex_string (signature),
80626a55
DE
9586 dwp_file->name);
9587 }
9588
19ac8c2e 9589 /* Fetch the sections of this DWO unit.
80626a55
DE
9590 Put a limit on the number of sections we look for so that bad data
9591 doesn't cause us to loop forever. */
9592
73869dc2 9593#define MAX_NR_V1_DWO_SECTIONS \
80626a55
DE
9594 (1 /* .debug_info or .debug_types */ \
9595 + 1 /* .debug_abbrev */ \
9596 + 1 /* .debug_line */ \
9597 + 1 /* .debug_loc */ \
9598 + 1 /* .debug_str_offsets */ \
19ac8c2e 9599 + 1 /* .debug_macro or .debug_macinfo */ \
80626a55
DE
9600 + 1 /* trailing zero */)
9601
9602 memset (&sections, 0, sizeof (sections));
9603 cleanups = make_cleanup (null_cleanup, 0);
9604
73869dc2 9605 for (i = 0; i < MAX_NR_V1_DWO_SECTIONS; ++i)
80626a55
DE
9606 {
9607 asection *sectp;
9608 uint32_t section_nr =
9609 read_4_bytes (dbfd,
73869dc2
DE
9610 dwp_htab->section_pool.v1.indices
9611 + (unit_index + i) * sizeof (uint32_t));
80626a55
DE
9612
9613 if (section_nr == 0)
9614 break;
9615 if (section_nr >= dwp_file->num_sections)
9616 {
9617 error (_("Dwarf Error: bad DWP hash table, section number too large"
9618 " [in module %s]"),
9619 dwp_file->name);
9620 }
9621
9622 sectp = dwp_file->elf_sections[section_nr];
73869dc2 9623 if (! locate_v1_virtual_dwo_sections (sectp, &sections))
80626a55
DE
9624 {
9625 error (_("Dwarf Error: bad DWP hash table, invalid section found"
9626 " [in module %s]"),
9627 dwp_file->name);
9628 }
9629 }
9630
9631 if (i < 2
a32a8923
DE
9632 || dwarf2_section_empty_p (&sections.info_or_types)
9633 || dwarf2_section_empty_p (&sections.abbrev))
80626a55
DE
9634 {
9635 error (_("Dwarf Error: bad DWP hash table, missing DWO sections"
9636 " [in module %s]"),
9637 dwp_file->name);
9638 }
73869dc2 9639 if (i == MAX_NR_V1_DWO_SECTIONS)
80626a55
DE
9640 {
9641 error (_("Dwarf Error: bad DWP hash table, too many DWO sections"
9642 " [in module %s]"),
9643 dwp_file->name);
9644 }
9645
9646 /* It's easier for the rest of the code if we fake a struct dwo_file and
9647 have dwo_unit "live" in that. At least for now.
9648
9649 The DWP file can be made up of a random collection of CUs and TUs.
c766f7ec 9650 However, for each CU + set of TUs that came from the same original DWO
57d63ce2
DE
9651 file, we can combine them back into a virtual DWO file to save space
9652 (fewer struct dwo_file objects to allocate). Remember that for really
80626a55
DE
9653 large apps there can be on the order of 8K CUs and 200K TUs, or more. */
9654
2792b94d
PM
9655 virtual_dwo_name =
9656 xstrprintf ("virtual-dwo/%d-%d-%d-%d",
a32a8923
DE
9657 get_section_id (&sections.abbrev),
9658 get_section_id (&sections.line),
9659 get_section_id (&sections.loc),
9660 get_section_id (&sections.str_offsets));
80626a55
DE
9661 make_cleanup (xfree, virtual_dwo_name);
9662 /* Can we use an existing virtual DWO file? */
0ac5b59e 9663 dwo_file_slot = lookup_dwo_file_slot (virtual_dwo_name, comp_dir);
80626a55
DE
9664 /* Create one if necessary. */
9665 if (*dwo_file_slot == NULL)
9666 {
9667 if (dwarf2_read_debug)
9668 {
9669 fprintf_unfiltered (gdb_stdlog, "Creating virtual DWO: %s\n",
9670 virtual_dwo_name);
9671 }
9672 dwo_file = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct dwo_file);
0ac5b59e
DE
9673 dwo_file->dwo_name = obstack_copy0 (&objfile->objfile_obstack,
9674 virtual_dwo_name,
9675 strlen (virtual_dwo_name));
9676 dwo_file->comp_dir = comp_dir;
80626a55
DE
9677 dwo_file->sections.abbrev = sections.abbrev;
9678 dwo_file->sections.line = sections.line;
9679 dwo_file->sections.loc = sections.loc;
9680 dwo_file->sections.macinfo = sections.macinfo;
9681 dwo_file->sections.macro = sections.macro;
9682 dwo_file->sections.str_offsets = sections.str_offsets;
9683 /* The "str" section is global to the entire DWP file. */
9684 dwo_file->sections.str = dwp_file->sections.str;
57d63ce2 9685 /* The info or types section is assigned below to dwo_unit,
80626a55
DE
9686 there's no need to record it in dwo_file.
9687 Also, we can't simply record type sections in dwo_file because
9688 we record a pointer into the vector in dwo_unit. As we collect more
9689 types we'll grow the vector and eventually have to reallocate space
57d63ce2
DE
9690 for it, invalidating all copies of pointers into the previous
9691 contents. */
80626a55
DE
9692 *dwo_file_slot = dwo_file;
9693 }
9694 else
9695 {
9696 if (dwarf2_read_debug)
9697 {
9698 fprintf_unfiltered (gdb_stdlog, "Using existing virtual DWO: %s\n",
9699 virtual_dwo_name);
9700 }
9701 dwo_file = *dwo_file_slot;
9702 }
9703 do_cleanups (cleanups);
9704
9705 dwo_unit = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct dwo_unit);
9706 dwo_unit->dwo_file = dwo_file;
9707 dwo_unit->signature = signature;
8a0459fd
DE
9708 dwo_unit->section = obstack_alloc (&objfile->objfile_obstack,
9709 sizeof (struct dwarf2_section_info));
9710 *dwo_unit->section = sections.info_or_types;
57d63ce2 9711 /* dwo_unit->{offset,length,type_offset_in_tu} are set later. */
80626a55
DE
9712
9713 return dwo_unit;
9714}
9715
73869dc2
DE
9716/* Subroutine of create_dwo_unit_in_dwp_v2 to simplify it.
9717 Given a pointer to the containing section SECTION, and OFFSET,SIZE of the
9718 piece within that section used by a TU/CU, return a virtual section
9719 of just that piece. */
9720
9721static struct dwarf2_section_info
9722create_dwp_v2_section (struct dwarf2_section_info *section,
9723 bfd_size_type offset, bfd_size_type size)
9724{
9725 struct dwarf2_section_info result;
9726 asection *sectp;
9727
9728 gdb_assert (section != NULL);
9729 gdb_assert (!section->is_virtual);
9730
9731 memset (&result, 0, sizeof (result));
9732 result.s.containing_section = section;
9733 result.is_virtual = 1;
9734
9735 if (size == 0)
9736 return result;
9737
9738 sectp = get_section_bfd_section (section);
9739
9740 /* Flag an error if the piece denoted by OFFSET,SIZE is outside the
9741 bounds of the real section. This is a pretty-rare event, so just
9742 flag an error (easier) instead of a warning and trying to cope. */
9743 if (sectp == NULL
9744 || offset + size > bfd_get_section_size (sectp))
9745 {
9746 bfd *abfd = sectp->owner;
9747
9748 error (_("Dwarf Error: Bad DWP V2 section info, doesn't fit"
9749 " in section %s [in module %s]"),
9750 sectp ? bfd_section_name (abfd, sectp) : "<unknown>",
9751 objfile_name (dwarf2_per_objfile->objfile));
9752 }
9753
9754 result.virtual_offset = offset;
9755 result.size = size;
9756 return result;
9757}
9758
9759/* Create a dwo_unit object for the DWO unit with signature SIGNATURE.
9760 UNIT_INDEX is the index of the DWO unit in the DWP hash table.
9761 COMP_DIR is the DW_AT_comp_dir attribute of the referencing CU.
9762 This is for DWP version 2 files. */
9763
9764static struct dwo_unit *
9765create_dwo_unit_in_dwp_v2 (struct dwp_file *dwp_file,
9766 uint32_t unit_index,
9767 const char *comp_dir,
9768 ULONGEST signature, int is_debug_types)
9769{
9770 struct objfile *objfile = dwarf2_per_objfile->objfile;
9771 const struct dwp_hash_table *dwp_htab =
9772 is_debug_types ? dwp_file->tus : dwp_file->cus;
9773 bfd *dbfd = dwp_file->dbfd;
9774 const char *kind = is_debug_types ? "TU" : "CU";
9775 struct dwo_file *dwo_file;
9776 struct dwo_unit *dwo_unit;
9777 struct virtual_v2_dwo_sections sections;
9778 void **dwo_file_slot;
9779 char *virtual_dwo_name;
9780 struct dwarf2_section_info *cutu;
9781 struct cleanup *cleanups;
9782 int i;
9783
9784 gdb_assert (dwp_file->version == 2);
9785
9786 if (dwarf2_read_debug)
9787 {
9788 fprintf_unfiltered (gdb_stdlog, "Reading %s %s/%s in DWP V2 file: %s\n",
9789 kind,
9790 pulongest (unit_index), hex_string (signature),
9791 dwp_file->name);
9792 }
9793
9794 /* Fetch the section offsets of this DWO unit. */
9795
9796 memset (&sections, 0, sizeof (sections));
9797 cleanups = make_cleanup (null_cleanup, 0);
9798
9799 for (i = 0; i < dwp_htab->nr_columns; ++i)
9800 {
9801 uint32_t offset = read_4_bytes (dbfd,
9802 dwp_htab->section_pool.v2.offsets
9803 + (((unit_index - 1) * dwp_htab->nr_columns
9804 + i)
9805 * sizeof (uint32_t)));
9806 uint32_t size = read_4_bytes (dbfd,
9807 dwp_htab->section_pool.v2.sizes
9808 + (((unit_index - 1) * dwp_htab->nr_columns
9809 + i)
9810 * sizeof (uint32_t)));
9811
9812 switch (dwp_htab->section_pool.v2.section_ids[i])
9813 {
9814 case DW_SECT_INFO:
9815 case DW_SECT_TYPES:
9816 sections.info_or_types_offset = offset;
9817 sections.info_or_types_size = size;
9818 break;
9819 case DW_SECT_ABBREV:
9820 sections.abbrev_offset = offset;
9821 sections.abbrev_size = size;
9822 break;
9823 case DW_SECT_LINE:
9824 sections.line_offset = offset;
9825 sections.line_size = size;
9826 break;
9827 case DW_SECT_LOC:
9828 sections.loc_offset = offset;
9829 sections.loc_size = size;
9830 break;
9831 case DW_SECT_STR_OFFSETS:
9832 sections.str_offsets_offset = offset;
9833 sections.str_offsets_size = size;
9834 break;
9835 case DW_SECT_MACINFO:
9836 sections.macinfo_offset = offset;
9837 sections.macinfo_size = size;
9838 break;
9839 case DW_SECT_MACRO:
9840 sections.macro_offset = offset;
9841 sections.macro_size = size;
9842 break;
9843 }
9844 }
9845
9846 /* It's easier for the rest of the code if we fake a struct dwo_file and
9847 have dwo_unit "live" in that. At least for now.
9848
9849 The DWP file can be made up of a random collection of CUs and TUs.
9850 However, for each CU + set of TUs that came from the same original DWO
9851 file, we can combine them back into a virtual DWO file to save space
9852 (fewer struct dwo_file objects to allocate). Remember that for really
9853 large apps there can be on the order of 8K CUs and 200K TUs, or more. */
9854
9855 virtual_dwo_name =
9856 xstrprintf ("virtual-dwo/%ld-%ld-%ld-%ld",
9857 (long) (sections.abbrev_size ? sections.abbrev_offset : 0),
9858 (long) (sections.line_size ? sections.line_offset : 0),
9859 (long) (sections.loc_size ? sections.loc_offset : 0),
9860 (long) (sections.str_offsets_size
9861 ? sections.str_offsets_offset : 0));
9862 make_cleanup (xfree, virtual_dwo_name);
9863 /* Can we use an existing virtual DWO file? */
9864 dwo_file_slot = lookup_dwo_file_slot (virtual_dwo_name, comp_dir);
9865 /* Create one if necessary. */
9866 if (*dwo_file_slot == NULL)
9867 {
9868 if (dwarf2_read_debug)
9869 {
9870 fprintf_unfiltered (gdb_stdlog, "Creating virtual DWO: %s\n",
9871 virtual_dwo_name);
9872 }
9873 dwo_file = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct dwo_file);
9874 dwo_file->dwo_name = obstack_copy0 (&objfile->objfile_obstack,
9875 virtual_dwo_name,
9876 strlen (virtual_dwo_name));
9877 dwo_file->comp_dir = comp_dir;
9878 dwo_file->sections.abbrev =
9879 create_dwp_v2_section (&dwp_file->sections.abbrev,
9880 sections.abbrev_offset, sections.abbrev_size);
9881 dwo_file->sections.line =
9882 create_dwp_v2_section (&dwp_file->sections.line,
9883 sections.line_offset, sections.line_size);
9884 dwo_file->sections.loc =
9885 create_dwp_v2_section (&dwp_file->sections.loc,
9886 sections.loc_offset, sections.loc_size);
9887 dwo_file->sections.macinfo =
9888 create_dwp_v2_section (&dwp_file->sections.macinfo,
9889 sections.macinfo_offset, sections.macinfo_size);
9890 dwo_file->sections.macro =
9891 create_dwp_v2_section (&dwp_file->sections.macro,
9892 sections.macro_offset, sections.macro_size);
9893 dwo_file->sections.str_offsets =
9894 create_dwp_v2_section (&dwp_file->sections.str_offsets,
9895 sections.str_offsets_offset,
9896 sections.str_offsets_size);
9897 /* The "str" section is global to the entire DWP file. */
9898 dwo_file->sections.str = dwp_file->sections.str;
9899 /* The info or types section is assigned below to dwo_unit,
9900 there's no need to record it in dwo_file.
9901 Also, we can't simply record type sections in dwo_file because
9902 we record a pointer into the vector in dwo_unit. As we collect more
9903 types we'll grow the vector and eventually have to reallocate space
9904 for it, invalidating all copies of pointers into the previous
9905 contents. */
9906 *dwo_file_slot = dwo_file;
9907 }
9908 else
9909 {
9910 if (dwarf2_read_debug)
9911 {
9912 fprintf_unfiltered (gdb_stdlog, "Using existing virtual DWO: %s\n",
9913 virtual_dwo_name);
9914 }
9915 dwo_file = *dwo_file_slot;
9916 }
9917 do_cleanups (cleanups);
9918
9919 dwo_unit = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct dwo_unit);
9920 dwo_unit->dwo_file = dwo_file;
9921 dwo_unit->signature = signature;
9922 dwo_unit->section = obstack_alloc (&objfile->objfile_obstack,
9923 sizeof (struct dwarf2_section_info));
9924 *dwo_unit->section = create_dwp_v2_section (is_debug_types
9925 ? &dwp_file->sections.types
9926 : &dwp_file->sections.info,
9927 sections.info_or_types_offset,
9928 sections.info_or_types_size);
9929 /* dwo_unit->{offset,length,type_offset_in_tu} are set later. */
9930
9931 return dwo_unit;
9932}
9933
57d63ce2
DE
9934/* Lookup the DWO unit with SIGNATURE in DWP_FILE.
9935 Returns NULL if the signature isn't found. */
80626a55
DE
9936
9937static struct dwo_unit *
57d63ce2
DE
9938lookup_dwo_unit_in_dwp (struct dwp_file *dwp_file, const char *comp_dir,
9939 ULONGEST signature, int is_debug_types)
80626a55 9940{
57d63ce2
DE
9941 const struct dwp_hash_table *dwp_htab =
9942 is_debug_types ? dwp_file->tus : dwp_file->cus;
80626a55 9943 bfd *dbfd = dwp_file->dbfd;
57d63ce2 9944 uint32_t mask = dwp_htab->nr_slots - 1;
80626a55
DE
9945 uint32_t hash = signature & mask;
9946 uint32_t hash2 = ((signature >> 32) & mask) | 1;
9947 unsigned int i;
9948 void **slot;
9949 struct dwo_unit find_dwo_cu, *dwo_cu;
9950
9951 memset (&find_dwo_cu, 0, sizeof (find_dwo_cu));
9952 find_dwo_cu.signature = signature;
19ac8c2e
DE
9953 slot = htab_find_slot (is_debug_types
9954 ? dwp_file->loaded_tus
9955 : dwp_file->loaded_cus,
9956 &find_dwo_cu, INSERT);
80626a55
DE
9957
9958 if (*slot != NULL)
9959 return *slot;
9960
9961 /* Use a for loop so that we don't loop forever on bad debug info. */
57d63ce2 9962 for (i = 0; i < dwp_htab->nr_slots; ++i)
80626a55
DE
9963 {
9964 ULONGEST signature_in_table;
9965
9966 signature_in_table =
57d63ce2 9967 read_8_bytes (dbfd, dwp_htab->hash_table + hash * sizeof (uint64_t));
80626a55
DE
9968 if (signature_in_table == signature)
9969 {
57d63ce2
DE
9970 uint32_t unit_index =
9971 read_4_bytes (dbfd,
9972 dwp_htab->unit_table + hash * sizeof (uint32_t));
80626a55 9973
73869dc2
DE
9974 if (dwp_file->version == 1)
9975 {
9976 *slot = create_dwo_unit_in_dwp_v1 (dwp_file, unit_index,
9977 comp_dir, signature,
9978 is_debug_types);
9979 }
9980 else
9981 {
9982 *slot = create_dwo_unit_in_dwp_v2 (dwp_file, unit_index,
9983 comp_dir, signature,
9984 is_debug_types);
9985 }
80626a55
DE
9986 return *slot;
9987 }
9988 if (signature_in_table == 0)
9989 return NULL;
9990 hash = (hash + hash2) & mask;
9991 }
9992
9993 error (_("Dwarf Error: bad DWP hash table, lookup didn't terminate"
9994 " [in module %s]"),
9995 dwp_file->name);
9996}
9997
ab5088bf 9998/* Subroutine of open_dwo_file,open_dwp_file to simplify them.
3019eac3
DE
9999 Open the file specified by FILE_NAME and hand it off to BFD for
10000 preliminary analysis. Return a newly initialized bfd *, which
10001 includes a canonicalized copy of FILE_NAME.
80626a55 10002 If IS_DWP is TRUE, we're opening a DWP file, otherwise a DWO file.
6ac97d4c
DE
10003 SEARCH_CWD is true if the current directory is to be searched.
10004 It will be searched before debug-file-directory.
10005 If unable to find/open the file, return NULL.
3019eac3
DE
10006 NOTE: This function is derived from symfile_bfd_open. */
10007
10008static bfd *
6ac97d4c 10009try_open_dwop_file (const char *file_name, int is_dwp, int search_cwd)
3019eac3
DE
10010{
10011 bfd *sym_bfd;
80626a55 10012 int desc, flags;
3019eac3 10013 char *absolute_name;
9c02c129
DE
10014 /* Blech. OPF_TRY_CWD_FIRST also disables searching the path list if
10015 FILE_NAME contains a '/'. So we can't use it. Instead prepend "."
10016 to debug_file_directory. */
10017 char *search_path;
10018 static const char dirname_separator_string[] = { DIRNAME_SEPARATOR, '\0' };
10019
6ac97d4c
DE
10020 if (search_cwd)
10021 {
10022 if (*debug_file_directory != '\0')
10023 search_path = concat (".", dirname_separator_string,
10024 debug_file_directory, NULL);
10025 else
10026 search_path = xstrdup (".");
10027 }
9c02c129 10028 else
6ac97d4c 10029 search_path = xstrdup (debug_file_directory);
3019eac3 10030
492c0ab7 10031 flags = OPF_RETURN_REALPATH;
80626a55
DE
10032 if (is_dwp)
10033 flags |= OPF_SEARCH_IN_PATH;
9c02c129 10034 desc = openp (search_path, flags, file_name,
3019eac3 10035 O_RDONLY | O_BINARY, &absolute_name);
9c02c129 10036 xfree (search_path);
3019eac3
DE
10037 if (desc < 0)
10038 return NULL;
10039
bb397797 10040 sym_bfd = gdb_bfd_open (absolute_name, gnutarget, desc);
a4453b7e 10041 xfree (absolute_name);
9c02c129
DE
10042 if (sym_bfd == NULL)
10043 return NULL;
3019eac3
DE
10044 bfd_set_cacheable (sym_bfd, 1);
10045
10046 if (!bfd_check_format (sym_bfd, bfd_object))
10047 {
cbb099e8 10048 gdb_bfd_unref (sym_bfd); /* This also closes desc. */
3019eac3
DE
10049 return NULL;
10050 }
10051
3019eac3
DE
10052 return sym_bfd;
10053}
10054
ab5088bf 10055/* Try to open DWO file FILE_NAME.
3019eac3
DE
10056 COMP_DIR is the DW_AT_comp_dir attribute.
10057 The result is the bfd handle of the file.
10058 If there is a problem finding or opening the file, return NULL.
10059 Upon success, the canonicalized path of the file is stored in the bfd,
10060 same as symfile_bfd_open. */
10061
10062static bfd *
ab5088bf 10063open_dwo_file (const char *file_name, const char *comp_dir)
3019eac3
DE
10064{
10065 bfd *abfd;
3019eac3 10066
80626a55 10067 if (IS_ABSOLUTE_PATH (file_name))
6ac97d4c 10068 return try_open_dwop_file (file_name, 0 /*is_dwp*/, 0 /*search_cwd*/);
3019eac3
DE
10069
10070 /* Before trying the search path, try DWO_NAME in COMP_DIR. */
10071
10072 if (comp_dir != NULL)
10073 {
80626a55 10074 char *path_to_try = concat (comp_dir, SLASH_STRING, file_name, NULL);
3019eac3
DE
10075
10076 /* NOTE: If comp_dir is a relative path, this will also try the
10077 search path, which seems useful. */
6ac97d4c 10078 abfd = try_open_dwop_file (path_to_try, 0 /*is_dwp*/, 1 /*search_cwd*/);
3019eac3
DE
10079 xfree (path_to_try);
10080 if (abfd != NULL)
10081 return abfd;
10082 }
10083
10084 /* That didn't work, try debug-file-directory, which, despite its name,
10085 is a list of paths. */
10086
10087 if (*debug_file_directory == '\0')
10088 return NULL;
10089
6ac97d4c 10090 return try_open_dwop_file (file_name, 0 /*is_dwp*/, 1 /*search_cwd*/);
3019eac3
DE
10091}
10092
80626a55
DE
10093/* This function is mapped across the sections and remembers the offset and
10094 size of each of the DWO debugging sections we are interested in. */
10095
10096static void
10097dwarf2_locate_dwo_sections (bfd *abfd, asection *sectp, void *dwo_sections_ptr)
10098{
10099 struct dwo_sections *dwo_sections = dwo_sections_ptr;
10100 const struct dwop_section_names *names = &dwop_section_names;
10101
10102 if (section_is_p (sectp->name, &names->abbrev_dwo))
10103 {
73869dc2 10104 dwo_sections->abbrev.s.asection = sectp;
80626a55
DE
10105 dwo_sections->abbrev.size = bfd_get_section_size (sectp);
10106 }
10107 else if (section_is_p (sectp->name, &names->info_dwo))
10108 {
73869dc2 10109 dwo_sections->info.s.asection = sectp;
80626a55
DE
10110 dwo_sections->info.size = bfd_get_section_size (sectp);
10111 }
10112 else if (section_is_p (sectp->name, &names->line_dwo))
10113 {
73869dc2 10114 dwo_sections->line.s.asection = sectp;
80626a55
DE
10115 dwo_sections->line.size = bfd_get_section_size (sectp);
10116 }
10117 else if (section_is_p (sectp->name, &names->loc_dwo))
10118 {
73869dc2 10119 dwo_sections->loc.s.asection = sectp;
80626a55
DE
10120 dwo_sections->loc.size = bfd_get_section_size (sectp);
10121 }
10122 else if (section_is_p (sectp->name, &names->macinfo_dwo))
10123 {
73869dc2 10124 dwo_sections->macinfo.s.asection = sectp;
80626a55
DE
10125 dwo_sections->macinfo.size = bfd_get_section_size (sectp);
10126 }
10127 else if (section_is_p (sectp->name, &names->macro_dwo))
10128 {
73869dc2 10129 dwo_sections->macro.s.asection = sectp;
80626a55
DE
10130 dwo_sections->macro.size = bfd_get_section_size (sectp);
10131 }
10132 else if (section_is_p (sectp->name, &names->str_dwo))
10133 {
73869dc2 10134 dwo_sections->str.s.asection = sectp;
80626a55
DE
10135 dwo_sections->str.size = bfd_get_section_size (sectp);
10136 }
10137 else if (section_is_p (sectp->name, &names->str_offsets_dwo))
10138 {
73869dc2 10139 dwo_sections->str_offsets.s.asection = sectp;
80626a55
DE
10140 dwo_sections->str_offsets.size = bfd_get_section_size (sectp);
10141 }
10142 else if (section_is_p (sectp->name, &names->types_dwo))
10143 {
10144 struct dwarf2_section_info type_section;
10145
10146 memset (&type_section, 0, sizeof (type_section));
73869dc2 10147 type_section.s.asection = sectp;
80626a55
DE
10148 type_section.size = bfd_get_section_size (sectp);
10149 VEC_safe_push (dwarf2_section_info_def, dwo_sections->types,
10150 &type_section);
10151 }
10152}
10153
ab5088bf 10154/* Initialize the use of the DWO file specified by DWO_NAME and referenced
19c3d4c9 10155 by PER_CU. This is for the non-DWP case.
80626a55 10156 The result is NULL if DWO_NAME can't be found. */
3019eac3
DE
10157
10158static struct dwo_file *
0ac5b59e
DE
10159open_and_init_dwo_file (struct dwarf2_per_cu_data *per_cu,
10160 const char *dwo_name, const char *comp_dir)
3019eac3
DE
10161{
10162 struct objfile *objfile = dwarf2_per_objfile->objfile;
80626a55
DE
10163 struct dwo_file *dwo_file;
10164 bfd *dbfd;
3019eac3
DE
10165 struct cleanup *cleanups;
10166
ab5088bf 10167 dbfd = open_dwo_file (dwo_name, comp_dir);
80626a55
DE
10168 if (dbfd == NULL)
10169 {
10170 if (dwarf2_read_debug)
10171 fprintf_unfiltered (gdb_stdlog, "DWO file not found: %s\n", dwo_name);
10172 return NULL;
10173 }
10174 dwo_file = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct dwo_file);
0ac5b59e
DE
10175 dwo_file->dwo_name = dwo_name;
10176 dwo_file->comp_dir = comp_dir;
80626a55 10177 dwo_file->dbfd = dbfd;
3019eac3
DE
10178
10179 cleanups = make_cleanup (free_dwo_file_cleanup, dwo_file);
10180
80626a55 10181 bfd_map_over_sections (dbfd, dwarf2_locate_dwo_sections, &dwo_file->sections);
3019eac3 10182
19c3d4c9 10183 dwo_file->cu = create_dwo_cu (dwo_file);
3019eac3
DE
10184
10185 dwo_file->tus = create_debug_types_hash_table (dwo_file,
10186 dwo_file->sections.types);
10187
10188 discard_cleanups (cleanups);
10189
80626a55
DE
10190 if (dwarf2_read_debug)
10191 fprintf_unfiltered (gdb_stdlog, "DWO file found: %s\n", dwo_name);
10192
3019eac3
DE
10193 return dwo_file;
10194}
10195
80626a55 10196/* This function is mapped across the sections and remembers the offset and
73869dc2
DE
10197 size of each of the DWP debugging sections common to version 1 and 2 that
10198 we are interested in. */
3019eac3 10199
80626a55 10200static void
73869dc2
DE
10201dwarf2_locate_common_dwp_sections (bfd *abfd, asection *sectp,
10202 void *dwp_file_ptr)
3019eac3 10203{
80626a55
DE
10204 struct dwp_file *dwp_file = dwp_file_ptr;
10205 const struct dwop_section_names *names = &dwop_section_names;
10206 unsigned int elf_section_nr = elf_section_data (sectp)->this_idx;
3019eac3 10207
80626a55 10208 /* Record the ELF section number for later lookup: this is what the
73869dc2 10209 .debug_cu_index,.debug_tu_index tables use in DWP V1. */
80626a55
DE
10210 gdb_assert (elf_section_nr < dwp_file->num_sections);
10211 dwp_file->elf_sections[elf_section_nr] = sectp;
3019eac3 10212
80626a55
DE
10213 /* Look for specific sections that we need. */
10214 if (section_is_p (sectp->name, &names->str_dwo))
10215 {
73869dc2 10216 dwp_file->sections.str.s.asection = sectp;
80626a55
DE
10217 dwp_file->sections.str.size = bfd_get_section_size (sectp);
10218 }
10219 else if (section_is_p (sectp->name, &names->cu_index))
10220 {
73869dc2 10221 dwp_file->sections.cu_index.s.asection = sectp;
80626a55
DE
10222 dwp_file->sections.cu_index.size = bfd_get_section_size (sectp);
10223 }
10224 else if (section_is_p (sectp->name, &names->tu_index))
10225 {
73869dc2 10226 dwp_file->sections.tu_index.s.asection = sectp;
80626a55
DE
10227 dwp_file->sections.tu_index.size = bfd_get_section_size (sectp);
10228 }
10229}
3019eac3 10230
73869dc2
DE
10231/* This function is mapped across the sections and remembers the offset and
10232 size of each of the DWP version 2 debugging sections that we are interested
10233 in. This is split into a separate function because we don't know if we
10234 have version 1 or 2 until we parse the cu_index/tu_index sections. */
10235
10236static void
10237dwarf2_locate_v2_dwp_sections (bfd *abfd, asection *sectp, void *dwp_file_ptr)
10238{
10239 struct dwp_file *dwp_file = dwp_file_ptr;
10240 const struct dwop_section_names *names = &dwop_section_names;
10241 unsigned int elf_section_nr = elf_section_data (sectp)->this_idx;
10242
10243 /* Record the ELF section number for later lookup: this is what the
10244 .debug_cu_index,.debug_tu_index tables use in DWP V1. */
10245 gdb_assert (elf_section_nr < dwp_file->num_sections);
10246 dwp_file->elf_sections[elf_section_nr] = sectp;
10247
10248 /* Look for specific sections that we need. */
10249 if (section_is_p (sectp->name, &names->abbrev_dwo))
10250 {
10251 dwp_file->sections.abbrev.s.asection = sectp;
10252 dwp_file->sections.abbrev.size = bfd_get_section_size (sectp);
10253 }
10254 else if (section_is_p (sectp->name, &names->info_dwo))
10255 {
10256 dwp_file->sections.info.s.asection = sectp;
10257 dwp_file->sections.info.size = bfd_get_section_size (sectp);
10258 }
10259 else if (section_is_p (sectp->name, &names->line_dwo))
10260 {
10261 dwp_file->sections.line.s.asection = sectp;
10262 dwp_file->sections.line.size = bfd_get_section_size (sectp);
10263 }
10264 else if (section_is_p (sectp->name, &names->loc_dwo))
10265 {
10266 dwp_file->sections.loc.s.asection = sectp;
10267 dwp_file->sections.loc.size = bfd_get_section_size (sectp);
10268 }
10269 else if (section_is_p (sectp->name, &names->macinfo_dwo))
10270 {
10271 dwp_file->sections.macinfo.s.asection = sectp;
10272 dwp_file->sections.macinfo.size = bfd_get_section_size (sectp);
10273 }
10274 else if (section_is_p (sectp->name, &names->macro_dwo))
10275 {
10276 dwp_file->sections.macro.s.asection = sectp;
10277 dwp_file->sections.macro.size = bfd_get_section_size (sectp);
10278 }
10279 else if (section_is_p (sectp->name, &names->str_offsets_dwo))
10280 {
10281 dwp_file->sections.str_offsets.s.asection = sectp;
10282 dwp_file->sections.str_offsets.size = bfd_get_section_size (sectp);
10283 }
10284 else if (section_is_p (sectp->name, &names->types_dwo))
10285 {
10286 dwp_file->sections.types.s.asection = sectp;
10287 dwp_file->sections.types.size = bfd_get_section_size (sectp);
10288 }
10289}
10290
80626a55 10291/* Hash function for dwp_file loaded CUs/TUs. */
3019eac3 10292
80626a55
DE
10293static hashval_t
10294hash_dwp_loaded_cutus (const void *item)
10295{
10296 const struct dwo_unit *dwo_unit = item;
3019eac3 10297
80626a55
DE
10298 /* This drops the top 32 bits of the signature, but is ok for a hash. */
10299 return dwo_unit->signature;
3019eac3
DE
10300}
10301
80626a55 10302/* Equality function for dwp_file loaded CUs/TUs. */
3019eac3 10303
80626a55
DE
10304static int
10305eq_dwp_loaded_cutus (const void *a, const void *b)
3019eac3 10306{
80626a55
DE
10307 const struct dwo_unit *dua = a;
10308 const struct dwo_unit *dub = b;
3019eac3 10309
80626a55
DE
10310 return dua->signature == dub->signature;
10311}
3019eac3 10312
80626a55 10313/* Allocate a hash table for dwp_file loaded CUs/TUs. */
3019eac3 10314
80626a55
DE
10315static htab_t
10316allocate_dwp_loaded_cutus_table (struct objfile *objfile)
10317{
10318 return htab_create_alloc_ex (3,
10319 hash_dwp_loaded_cutus,
10320 eq_dwp_loaded_cutus,
10321 NULL,
10322 &objfile->objfile_obstack,
10323 hashtab_obstack_allocate,
10324 dummy_obstack_deallocate);
10325}
3019eac3 10326
ab5088bf
DE
10327/* Try to open DWP file FILE_NAME.
10328 The result is the bfd handle of the file.
10329 If there is a problem finding or opening the file, return NULL.
10330 Upon success, the canonicalized path of the file is stored in the bfd,
10331 same as symfile_bfd_open. */
10332
10333static bfd *
10334open_dwp_file (const char *file_name)
10335{
6ac97d4c
DE
10336 bfd *abfd;
10337
10338 abfd = try_open_dwop_file (file_name, 1 /*is_dwp*/, 1 /*search_cwd*/);
10339 if (abfd != NULL)
10340 return abfd;
10341
10342 /* Work around upstream bug 15652.
10343 http://sourceware.org/bugzilla/show_bug.cgi?id=15652
10344 [Whether that's a "bug" is debatable, but it is getting in our way.]
10345 We have no real idea where the dwp file is, because gdb's realpath-ing
10346 of the executable's path may have discarded the needed info.
10347 [IWBN if the dwp file name was recorded in the executable, akin to
10348 .gnu_debuglink, but that doesn't exist yet.]
10349 Strip the directory from FILE_NAME and search again. */
10350 if (*debug_file_directory != '\0')
10351 {
10352 /* Don't implicitly search the current directory here.
10353 If the user wants to search "." to handle this case,
10354 it must be added to debug-file-directory. */
10355 return try_open_dwop_file (lbasename (file_name), 1 /*is_dwp*/,
10356 0 /*search_cwd*/);
10357 }
10358
10359 return NULL;
ab5088bf
DE
10360}
10361
80626a55
DE
10362/* Initialize the use of the DWP file for the current objfile.
10363 By convention the name of the DWP file is ${objfile}.dwp.
10364 The result is NULL if it can't be found. */
a766d390 10365
80626a55 10366static struct dwp_file *
ab5088bf 10367open_and_init_dwp_file (void)
80626a55
DE
10368{
10369 struct objfile *objfile = dwarf2_per_objfile->objfile;
10370 struct dwp_file *dwp_file;
10371 char *dwp_name;
10372 bfd *dbfd;
10373 struct cleanup *cleanups;
10374
82bf32bc
JK
10375 /* Try to find first .dwp for the binary file before any symbolic links
10376 resolving. */
10377 dwp_name = xstrprintf ("%s.dwp", objfile->original_name);
80626a55
DE
10378 cleanups = make_cleanup (xfree, dwp_name);
10379
ab5088bf 10380 dbfd = open_dwp_file (dwp_name);
82bf32bc
JK
10381 if (dbfd == NULL
10382 && strcmp (objfile->original_name, objfile_name (objfile)) != 0)
10383 {
10384 /* Try to find .dwp for the binary file after gdb_realpath resolving. */
10385 dwp_name = xstrprintf ("%s.dwp", objfile_name (objfile));
10386 make_cleanup (xfree, dwp_name);
10387 dbfd = open_dwp_file (dwp_name);
10388 }
10389
80626a55
DE
10390 if (dbfd == NULL)
10391 {
10392 if (dwarf2_read_debug)
10393 fprintf_unfiltered (gdb_stdlog, "DWP file not found: %s\n", dwp_name);
10394 do_cleanups (cleanups);
10395 return NULL;
3019eac3 10396 }
80626a55 10397 dwp_file = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct dwp_file);
93417882 10398 dwp_file->name = bfd_get_filename (dbfd);
80626a55
DE
10399 dwp_file->dbfd = dbfd;
10400 do_cleanups (cleanups);
c906108c 10401
80626a55
DE
10402 /* +1: section 0 is unused */
10403 dwp_file->num_sections = bfd_count_sections (dbfd) + 1;
10404 dwp_file->elf_sections =
10405 OBSTACK_CALLOC (&objfile->objfile_obstack,
10406 dwp_file->num_sections, asection *);
10407
73869dc2 10408 bfd_map_over_sections (dbfd, dwarf2_locate_common_dwp_sections, dwp_file);
80626a55
DE
10409
10410 dwp_file->cus = create_dwp_hash_table (dwp_file, 0);
10411
10412 dwp_file->tus = create_dwp_hash_table (dwp_file, 1);
10413
73869dc2
DE
10414 /* The DWP file version is stored in the hash table. Oh well. */
10415 if (dwp_file->cus->version != dwp_file->tus->version)
10416 {
10417 /* Technically speaking, we should try to limp along, but this is
10418 pretty bizarre. */
10419 error (_("Dwarf Error: DWP file CU version %d doesn't match"
10420 " TU version %d [in DWP file %s]"),
10421 dwp_file->cus->version, dwp_file->tus->version, dwp_name);
10422 }
10423 dwp_file->version = dwp_file->cus->version;
10424
10425 if (dwp_file->version == 2)
10426 bfd_map_over_sections (dbfd, dwarf2_locate_v2_dwp_sections, dwp_file);
10427
19ac8c2e
DE
10428 dwp_file->loaded_cus = allocate_dwp_loaded_cutus_table (objfile);
10429 dwp_file->loaded_tus = allocate_dwp_loaded_cutus_table (objfile);
80626a55 10430
80626a55
DE
10431 if (dwarf2_read_debug)
10432 {
10433 fprintf_unfiltered (gdb_stdlog, "DWP file found: %s\n", dwp_file->name);
10434 fprintf_unfiltered (gdb_stdlog,
21aa081e
PA
10435 " %s CUs, %s TUs\n",
10436 pulongest (dwp_file->cus ? dwp_file->cus->nr_units : 0),
10437 pulongest (dwp_file->tus ? dwp_file->tus->nr_units : 0));
80626a55
DE
10438 }
10439
10440 return dwp_file;
3019eac3 10441}
c906108c 10442
ab5088bf
DE
10443/* Wrapper around open_and_init_dwp_file, only open it once. */
10444
10445static struct dwp_file *
10446get_dwp_file (void)
10447{
10448 if (! dwarf2_per_objfile->dwp_checked)
10449 {
10450 dwarf2_per_objfile->dwp_file = open_and_init_dwp_file ();
10451 dwarf2_per_objfile->dwp_checked = 1;
10452 }
10453 return dwarf2_per_objfile->dwp_file;
10454}
10455
80626a55
DE
10456/* Subroutine of lookup_dwo_comp_unit, lookup_dwo_type_unit.
10457 Look up the CU/TU with signature SIGNATURE, either in DWO file DWO_NAME
10458 or in the DWP file for the objfile, referenced by THIS_UNIT.
3019eac3 10459 If non-NULL, comp_dir is the DW_AT_comp_dir attribute.
80626a55
DE
10460 IS_DEBUG_TYPES is non-zero if reading a TU, otherwise read a CU.
10461
10462 This is called, for example, when wanting to read a variable with a
10463 complex location. Therefore we don't want to do file i/o for every call.
10464 Therefore we don't want to look for a DWO file on every call.
10465 Therefore we first see if we've already seen SIGNATURE in a DWP file,
10466 then we check if we've already seen DWO_NAME, and only THEN do we check
10467 for a DWO file.
10468
1c658ad5 10469 The result is a pointer to the dwo_unit object or NULL if we didn't find it
80626a55 10470 (dwo_id mismatch or couldn't find the DWO/DWP file). */
debd256d 10471
3019eac3 10472static struct dwo_unit *
80626a55
DE
10473lookup_dwo_cutu (struct dwarf2_per_cu_data *this_unit,
10474 const char *dwo_name, const char *comp_dir,
10475 ULONGEST signature, int is_debug_types)
3019eac3
DE
10476{
10477 struct objfile *objfile = dwarf2_per_objfile->objfile;
80626a55
DE
10478 const char *kind = is_debug_types ? "TU" : "CU";
10479 void **dwo_file_slot;
3019eac3 10480 struct dwo_file *dwo_file;
80626a55 10481 struct dwp_file *dwp_file;
cb1df416 10482
6a506a2d
DE
10483 /* First see if there's a DWP file.
10484 If we have a DWP file but didn't find the DWO inside it, don't
10485 look for the original DWO file. It makes gdb behave differently
10486 depending on whether one is debugging in the build tree. */
cf2c3c16 10487
ab5088bf 10488 dwp_file = get_dwp_file ();
80626a55 10489 if (dwp_file != NULL)
cf2c3c16 10490 {
80626a55
DE
10491 const struct dwp_hash_table *dwp_htab =
10492 is_debug_types ? dwp_file->tus : dwp_file->cus;
10493
10494 if (dwp_htab != NULL)
10495 {
10496 struct dwo_unit *dwo_cutu =
57d63ce2
DE
10497 lookup_dwo_unit_in_dwp (dwp_file, comp_dir,
10498 signature, is_debug_types);
80626a55
DE
10499
10500 if (dwo_cutu != NULL)
10501 {
10502 if (dwarf2_read_debug)
10503 {
10504 fprintf_unfiltered (gdb_stdlog,
10505 "Virtual DWO %s %s found: @%s\n",
10506 kind, hex_string (signature),
10507 host_address_to_string (dwo_cutu));
10508 }
10509 return dwo_cutu;
10510 }
10511 }
10512 }
6a506a2d 10513 else
80626a55 10514 {
6a506a2d 10515 /* No DWP file, look for the DWO file. */
80626a55 10516
6a506a2d
DE
10517 dwo_file_slot = lookup_dwo_file_slot (dwo_name, comp_dir);
10518 if (*dwo_file_slot == NULL)
80626a55 10519 {
6a506a2d
DE
10520 /* Read in the file and build a table of the CUs/TUs it contains. */
10521 *dwo_file_slot = open_and_init_dwo_file (this_unit, dwo_name, comp_dir);
19c3d4c9 10522 }
6a506a2d
DE
10523 /* NOTE: This will be NULL if unable to open the file. */
10524 dwo_file = *dwo_file_slot;
3019eac3 10525
6a506a2d 10526 if (dwo_file != NULL)
19c3d4c9 10527 {
6a506a2d
DE
10528 struct dwo_unit *dwo_cutu = NULL;
10529
10530 if (is_debug_types && dwo_file->tus)
10531 {
10532 struct dwo_unit find_dwo_cutu;
10533
10534 memset (&find_dwo_cutu, 0, sizeof (find_dwo_cutu));
10535 find_dwo_cutu.signature = signature;
10536 dwo_cutu = htab_find (dwo_file->tus, &find_dwo_cutu);
10537 }
10538 else if (!is_debug_types && dwo_file->cu)
80626a55 10539 {
6a506a2d
DE
10540 if (signature == dwo_file->cu->signature)
10541 dwo_cutu = dwo_file->cu;
10542 }
10543
10544 if (dwo_cutu != NULL)
10545 {
10546 if (dwarf2_read_debug)
10547 {
10548 fprintf_unfiltered (gdb_stdlog, "DWO %s %s(%s) found: @%s\n",
10549 kind, dwo_name, hex_string (signature),
10550 host_address_to_string (dwo_cutu));
10551 }
10552 return dwo_cutu;
80626a55
DE
10553 }
10554 }
2e276125 10555 }
9cdd5dbd 10556
80626a55
DE
10557 /* We didn't find it. This could mean a dwo_id mismatch, or
10558 someone deleted the DWO/DWP file, or the search path isn't set up
10559 correctly to find the file. */
10560
10561 if (dwarf2_read_debug)
10562 {
10563 fprintf_unfiltered (gdb_stdlog, "DWO %s %s(%s) not found\n",
10564 kind, dwo_name, hex_string (signature));
10565 }
3019eac3 10566
6656a72d
DE
10567 /* This is a warning and not a complaint because it can be caused by
10568 pilot error (e.g., user accidentally deleting the DWO). */
10569 warning (_("Could not find DWO %s %s(%s) referenced by %s at offset 0x%x"
10570 " [in module %s]"),
10571 kind, dwo_name, hex_string (signature),
10572 this_unit->is_debug_types ? "TU" : "CU",
4262abfb 10573 this_unit->offset.sect_off, objfile_name (objfile));
3019eac3 10574 return NULL;
5fb290d7
DJ
10575}
10576
80626a55
DE
10577/* Lookup the DWO CU DWO_NAME/SIGNATURE referenced from THIS_CU.
10578 See lookup_dwo_cutu_unit for details. */
10579
10580static struct dwo_unit *
10581lookup_dwo_comp_unit (struct dwarf2_per_cu_data *this_cu,
10582 const char *dwo_name, const char *comp_dir,
10583 ULONGEST signature)
10584{
10585 return lookup_dwo_cutu (this_cu, dwo_name, comp_dir, signature, 0);
10586}
10587
10588/* Lookup the DWO TU DWO_NAME/SIGNATURE referenced from THIS_TU.
10589 See lookup_dwo_cutu_unit for details. */
10590
10591static struct dwo_unit *
10592lookup_dwo_type_unit (struct signatured_type *this_tu,
10593 const char *dwo_name, const char *comp_dir)
10594{
10595 return lookup_dwo_cutu (&this_tu->per_cu, dwo_name, comp_dir, this_tu->signature, 1);
10596}
10597
89e63ee4
DE
10598/* Traversal function for queue_and_load_all_dwo_tus. */
10599
10600static int
10601queue_and_load_dwo_tu (void **slot, void *info)
10602{
10603 struct dwo_unit *dwo_unit = (struct dwo_unit *) *slot;
10604 struct dwarf2_per_cu_data *per_cu = (struct dwarf2_per_cu_data *) info;
10605 ULONGEST signature = dwo_unit->signature;
10606 struct signatured_type *sig_type =
10607 lookup_dwo_signatured_type (per_cu->cu, signature);
10608
10609 if (sig_type != NULL)
10610 {
10611 struct dwarf2_per_cu_data *sig_cu = &sig_type->per_cu;
10612
10613 /* We pass NULL for DEPENDENT_CU because we don't yet know if there's
10614 a real dependency of PER_CU on SIG_TYPE. That is detected later
10615 while processing PER_CU. */
10616 if (maybe_queue_comp_unit (NULL, sig_cu, per_cu->cu->language))
10617 load_full_type_unit (sig_cu);
10618 VEC_safe_push (dwarf2_per_cu_ptr, per_cu->imported_symtabs, sig_cu);
10619 }
10620
10621 return 1;
10622}
10623
10624/* Queue all TUs contained in the DWO of PER_CU to be read in.
10625 The DWO may have the only definition of the type, though it may not be
10626 referenced anywhere in PER_CU. Thus we have to load *all* its TUs.
10627 http://sourceware.org/bugzilla/show_bug.cgi?id=15021 */
10628
10629static void
10630queue_and_load_all_dwo_tus (struct dwarf2_per_cu_data *per_cu)
10631{
10632 struct dwo_unit *dwo_unit;
10633 struct dwo_file *dwo_file;
10634
10635 gdb_assert (!per_cu->is_debug_types);
10636 gdb_assert (get_dwp_file () == NULL);
10637 gdb_assert (per_cu->cu != NULL);
10638
10639 dwo_unit = per_cu->cu->dwo_unit;
10640 gdb_assert (dwo_unit != NULL);
10641
10642 dwo_file = dwo_unit->dwo_file;
10643 if (dwo_file->tus != NULL)
10644 htab_traverse_noresize (dwo_file->tus, queue_and_load_dwo_tu, per_cu);
10645}
10646
3019eac3
DE
10647/* Free all resources associated with DWO_FILE.
10648 Close the DWO file and munmap the sections.
10649 All memory should be on the objfile obstack. */
348e048f
DE
10650
10651static void
3019eac3 10652free_dwo_file (struct dwo_file *dwo_file, struct objfile *objfile)
348e048f 10653{
3019eac3
DE
10654 int ix;
10655 struct dwarf2_section_info *section;
348e048f 10656
5c6fa7ab 10657 /* Note: dbfd is NULL for virtual DWO files. */
80626a55 10658 gdb_bfd_unref (dwo_file->dbfd);
348e048f 10659
3019eac3
DE
10660 VEC_free (dwarf2_section_info_def, dwo_file->sections.types);
10661}
348e048f 10662
3019eac3 10663/* Wrapper for free_dwo_file for use in cleanups. */
348e048f 10664
3019eac3
DE
10665static void
10666free_dwo_file_cleanup (void *arg)
10667{
10668 struct dwo_file *dwo_file = (struct dwo_file *) arg;
10669 struct objfile *objfile = dwarf2_per_objfile->objfile;
348e048f 10670
3019eac3
DE
10671 free_dwo_file (dwo_file, objfile);
10672}
348e048f 10673
3019eac3 10674/* Traversal function for free_dwo_files. */
2ab95328 10675
3019eac3
DE
10676static int
10677free_dwo_file_from_slot (void **slot, void *info)
10678{
10679 struct dwo_file *dwo_file = (struct dwo_file *) *slot;
10680 struct objfile *objfile = (struct objfile *) info;
348e048f 10681
3019eac3 10682 free_dwo_file (dwo_file, objfile);
348e048f 10683
3019eac3
DE
10684 return 1;
10685}
348e048f 10686
3019eac3 10687/* Free all resources associated with DWO_FILES. */
348e048f 10688
3019eac3
DE
10689static void
10690free_dwo_files (htab_t dwo_files, struct objfile *objfile)
10691{
10692 htab_traverse_noresize (dwo_files, free_dwo_file_from_slot, objfile);
348e048f 10693}
3019eac3
DE
10694\f
10695/* Read in various DIEs. */
348e048f 10696
d389af10
JK
10697/* qsort helper for inherit_abstract_dies. */
10698
10699static int
10700unsigned_int_compar (const void *ap, const void *bp)
10701{
10702 unsigned int a = *(unsigned int *) ap;
10703 unsigned int b = *(unsigned int *) bp;
10704
10705 return (a > b) - (b > a);
10706}
10707
10708/* DW_AT_abstract_origin inherits whole DIEs (not just their attributes).
3e43a32a
MS
10709 Inherit only the children of the DW_AT_abstract_origin DIE not being
10710 already referenced by DW_AT_abstract_origin from the children of the
10711 current DIE. */
d389af10
JK
10712
10713static void
10714inherit_abstract_dies (struct die_info *die, struct dwarf2_cu *cu)
10715{
10716 struct die_info *child_die;
10717 unsigned die_children_count;
10718 /* CU offsets which were referenced by children of the current DIE. */
b64f50a1
JK
10719 sect_offset *offsets;
10720 sect_offset *offsets_end, *offsetp;
d389af10
JK
10721 /* Parent of DIE - referenced by DW_AT_abstract_origin. */
10722 struct die_info *origin_die;
10723 /* Iterator of the ORIGIN_DIE children. */
10724 struct die_info *origin_child_die;
10725 struct cleanup *cleanups;
10726 struct attribute *attr;
cd02d79d
PA
10727 struct dwarf2_cu *origin_cu;
10728 struct pending **origin_previous_list_in_scope;
d389af10
JK
10729
10730 attr = dwarf2_attr (die, DW_AT_abstract_origin, cu);
10731 if (!attr)
10732 return;
10733
cd02d79d
PA
10734 /* Note that following die references may follow to a die in a
10735 different cu. */
10736
10737 origin_cu = cu;
10738 origin_die = follow_die_ref (die, attr, &origin_cu);
10739
10740 /* We're inheriting ORIGIN's children into the scope we'd put DIE's
10741 symbols in. */
10742 origin_previous_list_in_scope = origin_cu->list_in_scope;
10743 origin_cu->list_in_scope = cu->list_in_scope;
10744
edb3359d
DJ
10745 if (die->tag != origin_die->tag
10746 && !(die->tag == DW_TAG_inlined_subroutine
10747 && origin_die->tag == DW_TAG_subprogram))
d389af10
JK
10748 complaint (&symfile_complaints,
10749 _("DIE 0x%x and its abstract origin 0x%x have different tags"),
b64f50a1 10750 die->offset.sect_off, origin_die->offset.sect_off);
d389af10
JK
10751
10752 child_die = die->child;
10753 die_children_count = 0;
10754 while (child_die && child_die->tag)
10755 {
10756 child_die = sibling_die (child_die);
10757 die_children_count++;
10758 }
10759 offsets = xmalloc (sizeof (*offsets) * die_children_count);
10760 cleanups = make_cleanup (xfree, offsets);
10761
10762 offsets_end = offsets;
10763 child_die = die->child;
10764 while (child_die && child_die->tag)
10765 {
c38f313d
DJ
10766 /* For each CHILD_DIE, find the corresponding child of
10767 ORIGIN_DIE. If there is more than one layer of
10768 DW_AT_abstract_origin, follow them all; there shouldn't be,
10769 but GCC versions at least through 4.4 generate this (GCC PR
10770 40573). */
10771 struct die_info *child_origin_die = child_die;
cd02d79d 10772 struct dwarf2_cu *child_origin_cu = cu;
9a619af0 10773
c38f313d
DJ
10774 while (1)
10775 {
cd02d79d
PA
10776 attr = dwarf2_attr (child_origin_die, DW_AT_abstract_origin,
10777 child_origin_cu);
c38f313d
DJ
10778 if (attr == NULL)
10779 break;
cd02d79d
PA
10780 child_origin_die = follow_die_ref (child_origin_die, attr,
10781 &child_origin_cu);
c38f313d
DJ
10782 }
10783
d389af10
JK
10784 /* According to DWARF3 3.3.8.2 #3 new entries without their abstract
10785 counterpart may exist. */
c38f313d 10786 if (child_origin_die != child_die)
d389af10 10787 {
edb3359d
DJ
10788 if (child_die->tag != child_origin_die->tag
10789 && !(child_die->tag == DW_TAG_inlined_subroutine
10790 && child_origin_die->tag == DW_TAG_subprogram))
d389af10
JK
10791 complaint (&symfile_complaints,
10792 _("Child DIE 0x%x and its abstract origin 0x%x have "
b64f50a1
JK
10793 "different tags"), child_die->offset.sect_off,
10794 child_origin_die->offset.sect_off);
c38f313d
DJ
10795 if (child_origin_die->parent != origin_die)
10796 complaint (&symfile_complaints,
10797 _("Child DIE 0x%x and its abstract origin 0x%x have "
b64f50a1
JK
10798 "different parents"), child_die->offset.sect_off,
10799 child_origin_die->offset.sect_off);
c38f313d
DJ
10800 else
10801 *offsets_end++ = child_origin_die->offset;
d389af10
JK
10802 }
10803 child_die = sibling_die (child_die);
10804 }
10805 qsort (offsets, offsets_end - offsets, sizeof (*offsets),
10806 unsigned_int_compar);
10807 for (offsetp = offsets + 1; offsetp < offsets_end; offsetp++)
b64f50a1 10808 if (offsetp[-1].sect_off == offsetp->sect_off)
3e43a32a
MS
10809 complaint (&symfile_complaints,
10810 _("Multiple children of DIE 0x%x refer "
10811 "to DIE 0x%x as their abstract origin"),
b64f50a1 10812 die->offset.sect_off, offsetp->sect_off);
d389af10
JK
10813
10814 offsetp = offsets;
10815 origin_child_die = origin_die->child;
10816 while (origin_child_die && origin_child_die->tag)
10817 {
10818 /* Is ORIGIN_CHILD_DIE referenced by any of the DIE children? */
b64f50a1
JK
10819 while (offsetp < offsets_end
10820 && offsetp->sect_off < origin_child_die->offset.sect_off)
d389af10 10821 offsetp++;
b64f50a1
JK
10822 if (offsetp >= offsets_end
10823 || offsetp->sect_off > origin_child_die->offset.sect_off)
d389af10
JK
10824 {
10825 /* Found that ORIGIN_CHILD_DIE is really not referenced. */
cd02d79d 10826 process_die (origin_child_die, origin_cu);
d389af10
JK
10827 }
10828 origin_child_die = sibling_die (origin_child_die);
10829 }
cd02d79d 10830 origin_cu->list_in_scope = origin_previous_list_in_scope;
d389af10
JK
10831
10832 do_cleanups (cleanups);
10833}
10834
c906108c 10835static void
e7c27a73 10836read_func_scope (struct die_info *die, struct dwarf2_cu *cu)
c906108c 10837{
e7c27a73 10838 struct objfile *objfile = cu->objfile;
52f0bd74 10839 struct context_stack *new;
c906108c
SS
10840 CORE_ADDR lowpc;
10841 CORE_ADDR highpc;
10842 struct die_info *child_die;
edb3359d 10843 struct attribute *attr, *call_line, *call_file;
15d034d0 10844 const char *name;
e142c38c 10845 CORE_ADDR baseaddr;
801e3a5b 10846 struct block *block;
edb3359d 10847 int inlined_func = (die->tag == DW_TAG_inlined_subroutine);
34eaf542
TT
10848 VEC (symbolp) *template_args = NULL;
10849 struct template_symbol *templ_func = NULL;
edb3359d
DJ
10850
10851 if (inlined_func)
10852 {
10853 /* If we do not have call site information, we can't show the
10854 caller of this inlined function. That's too confusing, so
10855 only use the scope for local variables. */
10856 call_line = dwarf2_attr (die, DW_AT_call_line, cu);
10857 call_file = dwarf2_attr (die, DW_AT_call_file, cu);
10858 if (call_line == NULL || call_file == NULL)
10859 {
10860 read_lexical_block_scope (die, cu);
10861 return;
10862 }
10863 }
c906108c 10864
e142c38c
DJ
10865 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
10866
94af9270 10867 name = dwarf2_name (die, cu);
c906108c 10868
e8d05480
JB
10869 /* Ignore functions with missing or empty names. These are actually
10870 illegal according to the DWARF standard. */
10871 if (name == NULL)
10872 {
10873 complaint (&symfile_complaints,
b64f50a1
JK
10874 _("missing name for subprogram DIE at %d"),
10875 die->offset.sect_off);
e8d05480
JB
10876 return;
10877 }
10878
10879 /* Ignore functions with missing or invalid low and high pc attributes. */
10880 if (!dwarf2_get_pc_bounds (die, &lowpc, &highpc, cu, NULL))
10881 {
ae4d0c03
PM
10882 attr = dwarf2_attr (die, DW_AT_external, cu);
10883 if (!attr || !DW_UNSND (attr))
10884 complaint (&symfile_complaints,
3e43a32a
MS
10885 _("cannot get low and high bounds "
10886 "for subprogram DIE at %d"),
b64f50a1 10887 die->offset.sect_off);
e8d05480
JB
10888 return;
10889 }
c906108c
SS
10890
10891 lowpc += baseaddr;
10892 highpc += baseaddr;
10893
34eaf542
TT
10894 /* If we have any template arguments, then we must allocate a
10895 different sort of symbol. */
10896 for (child_die = die->child; child_die; child_die = sibling_die (child_die))
10897 {
10898 if (child_die->tag == DW_TAG_template_type_param
10899 || child_die->tag == DW_TAG_template_value_param)
10900 {
e623cf5d 10901 templ_func = allocate_template_symbol (objfile);
34eaf542
TT
10902 templ_func->base.is_cplus_template_function = 1;
10903 break;
10904 }
10905 }
10906
c906108c 10907 new = push_context (0, lowpc);
34eaf542
TT
10908 new->name = new_symbol_full (die, read_type_die (die, cu), cu,
10909 (struct symbol *) templ_func);
4c2df51b 10910
4cecd739
DJ
10911 /* If there is a location expression for DW_AT_frame_base, record
10912 it. */
e142c38c 10913 attr = dwarf2_attr (die, DW_AT_frame_base, cu);
4c2df51b 10914 if (attr)
f1e6e072 10915 dwarf2_symbol_mark_computed (attr, new->name, cu, 1);
4c2df51b 10916
e142c38c 10917 cu->list_in_scope = &local_symbols;
c906108c 10918
639d11d3 10919 if (die->child != NULL)
c906108c 10920 {
639d11d3 10921 child_die = die->child;
c906108c
SS
10922 while (child_die && child_die->tag)
10923 {
34eaf542
TT
10924 if (child_die->tag == DW_TAG_template_type_param
10925 || child_die->tag == DW_TAG_template_value_param)
10926 {
10927 struct symbol *arg = new_symbol (child_die, NULL, cu);
10928
f1078f66
DJ
10929 if (arg != NULL)
10930 VEC_safe_push (symbolp, template_args, arg);
34eaf542
TT
10931 }
10932 else
10933 process_die (child_die, cu);
c906108c
SS
10934 child_die = sibling_die (child_die);
10935 }
10936 }
10937
d389af10
JK
10938 inherit_abstract_dies (die, cu);
10939
4a811a97
UW
10940 /* If we have a DW_AT_specification, we might need to import using
10941 directives from the context of the specification DIE. See the
10942 comment in determine_prefix. */
10943 if (cu->language == language_cplus
10944 && dwarf2_attr (die, DW_AT_specification, cu))
10945 {
10946 struct dwarf2_cu *spec_cu = cu;
10947 struct die_info *spec_die = die_specification (die, &spec_cu);
10948
10949 while (spec_die)
10950 {
10951 child_die = spec_die->child;
10952 while (child_die && child_die->tag)
10953 {
10954 if (child_die->tag == DW_TAG_imported_module)
10955 process_die (child_die, spec_cu);
10956 child_die = sibling_die (child_die);
10957 }
10958
10959 /* In some cases, GCC generates specification DIEs that
10960 themselves contain DW_AT_specification attributes. */
10961 spec_die = die_specification (spec_die, &spec_cu);
10962 }
10963 }
10964
c906108c
SS
10965 new = pop_context ();
10966 /* Make a block for the local symbols within. */
801e3a5b
JB
10967 block = finish_block (new->name, &local_symbols, new->old_blocks,
10968 lowpc, highpc, objfile);
10969
df8a16a1 10970 /* For C++, set the block's scope. */
195a3f6c 10971 if ((cu->language == language_cplus || cu->language == language_fortran)
4d4ec4e5 10972 && cu->processing_has_namespace_info)
195a3f6c
TT
10973 block_set_scope (block, determine_prefix (die, cu),
10974 &objfile->objfile_obstack);
df8a16a1 10975
801e3a5b
JB
10976 /* If we have address ranges, record them. */
10977 dwarf2_record_block_ranges (die, block, baseaddr, cu);
6e70227d 10978
34eaf542
TT
10979 /* Attach template arguments to function. */
10980 if (! VEC_empty (symbolp, template_args))
10981 {
10982 gdb_assert (templ_func != NULL);
10983
10984 templ_func->n_template_arguments = VEC_length (symbolp, template_args);
10985 templ_func->template_arguments
10986 = obstack_alloc (&objfile->objfile_obstack,
10987 (templ_func->n_template_arguments
10988 * sizeof (struct symbol *)));
10989 memcpy (templ_func->template_arguments,
10990 VEC_address (symbolp, template_args),
10991 (templ_func->n_template_arguments * sizeof (struct symbol *)));
10992 VEC_free (symbolp, template_args);
10993 }
10994
208d8187
JB
10995 /* In C++, we can have functions nested inside functions (e.g., when
10996 a function declares a class that has methods). This means that
10997 when we finish processing a function scope, we may need to go
10998 back to building a containing block's symbol lists. */
10999 local_symbols = new->locals;
27aa8d6a 11000 using_directives = new->using_directives;
208d8187 11001
921e78cf
JB
11002 /* If we've finished processing a top-level function, subsequent
11003 symbols go in the file symbol list. */
11004 if (outermost_context_p ())
e142c38c 11005 cu->list_in_scope = &file_symbols;
c906108c
SS
11006}
11007
11008/* Process all the DIES contained within a lexical block scope. Start
11009 a new scope, process the dies, and then close the scope. */
11010
11011static void
e7c27a73 11012read_lexical_block_scope (struct die_info *die, struct dwarf2_cu *cu)
c906108c 11013{
e7c27a73 11014 struct objfile *objfile = cu->objfile;
52f0bd74 11015 struct context_stack *new;
c906108c
SS
11016 CORE_ADDR lowpc, highpc;
11017 struct die_info *child_die;
e142c38c
DJ
11018 CORE_ADDR baseaddr;
11019
11020 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
c906108c
SS
11021
11022 /* Ignore blocks with missing or invalid low and high pc attributes. */
af34e669
DJ
11023 /* ??? Perhaps consider discontiguous blocks defined by DW_AT_ranges
11024 as multiple lexical blocks? Handling children in a sane way would
6e70227d 11025 be nasty. Might be easier to properly extend generic blocks to
af34e669 11026 describe ranges. */
d85a05f0 11027 if (!dwarf2_get_pc_bounds (die, &lowpc, &highpc, cu, NULL))
c906108c
SS
11028 return;
11029 lowpc += baseaddr;
11030 highpc += baseaddr;
11031
11032 push_context (0, lowpc);
639d11d3 11033 if (die->child != NULL)
c906108c 11034 {
639d11d3 11035 child_die = die->child;
c906108c
SS
11036 while (child_die && child_die->tag)
11037 {
e7c27a73 11038 process_die (child_die, cu);
c906108c
SS
11039 child_die = sibling_die (child_die);
11040 }
11041 }
11042 new = pop_context ();
11043
8540c487 11044 if (local_symbols != NULL || using_directives != NULL)
c906108c 11045 {
801e3a5b
JB
11046 struct block *block
11047 = finish_block (0, &local_symbols, new->old_blocks, new->start_addr,
11048 highpc, objfile);
11049
11050 /* Note that recording ranges after traversing children, as we
11051 do here, means that recording a parent's ranges entails
11052 walking across all its children's ranges as they appear in
11053 the address map, which is quadratic behavior.
11054
11055 It would be nicer to record the parent's ranges before
11056 traversing its children, simply overriding whatever you find
11057 there. But since we don't even decide whether to create a
11058 block until after we've traversed its children, that's hard
11059 to do. */
11060 dwarf2_record_block_ranges (die, block, baseaddr, cu);
c906108c
SS
11061 }
11062 local_symbols = new->locals;
27aa8d6a 11063 using_directives = new->using_directives;
c906108c
SS
11064}
11065
96408a79
SA
11066/* Read in DW_TAG_GNU_call_site and insert it to CU->call_site_htab. */
11067
11068static void
11069read_call_site_scope (struct die_info *die, struct dwarf2_cu *cu)
11070{
11071 struct objfile *objfile = cu->objfile;
11072 struct gdbarch *gdbarch = get_objfile_arch (objfile);
11073 CORE_ADDR pc, baseaddr;
11074 struct attribute *attr;
11075 struct call_site *call_site, call_site_local;
11076 void **slot;
11077 int nparams;
11078 struct die_info *child_die;
11079
11080 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
11081
11082 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
11083 if (!attr)
11084 {
11085 complaint (&symfile_complaints,
11086 _("missing DW_AT_low_pc for DW_TAG_GNU_call_site "
11087 "DIE 0x%x [in module %s]"),
4262abfb 11088 die->offset.sect_off, objfile_name (objfile));
96408a79
SA
11089 return;
11090 }
11091 pc = DW_ADDR (attr) + baseaddr;
11092
11093 if (cu->call_site_htab == NULL)
11094 cu->call_site_htab = htab_create_alloc_ex (16, core_addr_hash, core_addr_eq,
11095 NULL, &objfile->objfile_obstack,
11096 hashtab_obstack_allocate, NULL);
11097 call_site_local.pc = pc;
11098 slot = htab_find_slot (cu->call_site_htab, &call_site_local, INSERT);
11099 if (*slot != NULL)
11100 {
11101 complaint (&symfile_complaints,
11102 _("Duplicate PC %s for DW_TAG_GNU_call_site "
11103 "DIE 0x%x [in module %s]"),
4262abfb
JK
11104 paddress (gdbarch, pc), die->offset.sect_off,
11105 objfile_name (objfile));
96408a79
SA
11106 return;
11107 }
11108
11109 /* Count parameters at the caller. */
11110
11111 nparams = 0;
11112 for (child_die = die->child; child_die && child_die->tag;
11113 child_die = sibling_die (child_die))
11114 {
11115 if (child_die->tag != DW_TAG_GNU_call_site_parameter)
11116 {
11117 complaint (&symfile_complaints,
11118 _("Tag %d is not DW_TAG_GNU_call_site_parameter in "
11119 "DW_TAG_GNU_call_site child DIE 0x%x [in module %s]"),
4262abfb
JK
11120 child_die->tag, child_die->offset.sect_off,
11121 objfile_name (objfile));
96408a79
SA
11122 continue;
11123 }
11124
11125 nparams++;
11126 }
11127
11128 call_site = obstack_alloc (&objfile->objfile_obstack,
11129 (sizeof (*call_site)
11130 + (sizeof (*call_site->parameter)
11131 * (nparams - 1))));
11132 *slot = call_site;
11133 memset (call_site, 0, sizeof (*call_site) - sizeof (*call_site->parameter));
11134 call_site->pc = pc;
11135
11136 if (dwarf2_flag_true_p (die, DW_AT_GNU_tail_call, cu))
11137 {
11138 struct die_info *func_die;
11139
11140 /* Skip also over DW_TAG_inlined_subroutine. */
11141 for (func_die = die->parent;
11142 func_die && func_die->tag != DW_TAG_subprogram
11143 && func_die->tag != DW_TAG_subroutine_type;
11144 func_die = func_die->parent);
11145
11146 /* DW_AT_GNU_all_call_sites is a superset
11147 of DW_AT_GNU_all_tail_call_sites. */
11148 if (func_die
11149 && !dwarf2_flag_true_p (func_die, DW_AT_GNU_all_call_sites, cu)
11150 && !dwarf2_flag_true_p (func_die, DW_AT_GNU_all_tail_call_sites, cu))
11151 {
11152 /* TYPE_TAIL_CALL_LIST is not interesting in functions where it is
11153 not complete. But keep CALL_SITE for look ups via call_site_htab,
11154 both the initial caller containing the real return address PC and
11155 the final callee containing the current PC of a chain of tail
11156 calls do not need to have the tail call list complete. But any
11157 function candidate for a virtual tail call frame searched via
11158 TYPE_TAIL_CALL_LIST must have the tail call list complete to be
11159 determined unambiguously. */
11160 }
11161 else
11162 {
11163 struct type *func_type = NULL;
11164
11165 if (func_die)
11166 func_type = get_die_type (func_die, cu);
11167 if (func_type != NULL)
11168 {
11169 gdb_assert (TYPE_CODE (func_type) == TYPE_CODE_FUNC);
11170
11171 /* Enlist this call site to the function. */
11172 call_site->tail_call_next = TYPE_TAIL_CALL_LIST (func_type);
11173 TYPE_TAIL_CALL_LIST (func_type) = call_site;
11174 }
11175 else
11176 complaint (&symfile_complaints,
11177 _("Cannot find function owning DW_TAG_GNU_call_site "
11178 "DIE 0x%x [in module %s]"),
4262abfb 11179 die->offset.sect_off, objfile_name (objfile));
96408a79
SA
11180 }
11181 }
11182
11183 attr = dwarf2_attr (die, DW_AT_GNU_call_site_target, cu);
11184 if (attr == NULL)
11185 attr = dwarf2_attr (die, DW_AT_abstract_origin, cu);
11186 SET_FIELD_DWARF_BLOCK (call_site->target, NULL);
11187 if (!attr || (attr_form_is_block (attr) && DW_BLOCK (attr)->size == 0))
11188 /* Keep NULL DWARF_BLOCK. */;
11189 else if (attr_form_is_block (attr))
11190 {
11191 struct dwarf2_locexpr_baton *dlbaton;
11192
11193 dlbaton = obstack_alloc (&objfile->objfile_obstack, sizeof (*dlbaton));
11194 dlbaton->data = DW_BLOCK (attr)->data;
11195 dlbaton->size = DW_BLOCK (attr)->size;
11196 dlbaton->per_cu = cu->per_cu;
11197
11198 SET_FIELD_DWARF_BLOCK (call_site->target, dlbaton);
11199 }
7771576e 11200 else if (attr_form_is_ref (attr))
96408a79 11201 {
96408a79
SA
11202 struct dwarf2_cu *target_cu = cu;
11203 struct die_info *target_die;
11204
ac9ec31b 11205 target_die = follow_die_ref (die, attr, &target_cu);
96408a79
SA
11206 gdb_assert (target_cu->objfile == objfile);
11207 if (die_is_declaration (target_die, target_cu))
11208 {
9112db09
JK
11209 const char *target_physname = NULL;
11210 struct attribute *target_attr;
11211
11212 /* Prefer the mangled name; otherwise compute the demangled one. */
11213 target_attr = dwarf2_attr (target_die, DW_AT_linkage_name, target_cu);
11214 if (target_attr == NULL)
11215 target_attr = dwarf2_attr (target_die, DW_AT_MIPS_linkage_name,
11216 target_cu);
11217 if (target_attr != NULL && DW_STRING (target_attr) != NULL)
11218 target_physname = DW_STRING (target_attr);
11219 else
11220 target_physname = dwarf2_physname (NULL, target_die, target_cu);
96408a79
SA
11221 if (target_physname == NULL)
11222 complaint (&symfile_complaints,
11223 _("DW_AT_GNU_call_site_target target DIE has invalid "
11224 "physname, for referencing DIE 0x%x [in module %s]"),
4262abfb 11225 die->offset.sect_off, objfile_name (objfile));
96408a79 11226 else
7d455152 11227 SET_FIELD_PHYSNAME (call_site->target, target_physname);
96408a79
SA
11228 }
11229 else
11230 {
11231 CORE_ADDR lowpc;
11232
11233 /* DW_AT_entry_pc should be preferred. */
11234 if (!dwarf2_get_pc_bounds (target_die, &lowpc, NULL, target_cu, NULL))
11235 complaint (&symfile_complaints,
11236 _("DW_AT_GNU_call_site_target target DIE has invalid "
11237 "low pc, for referencing DIE 0x%x [in module %s]"),
4262abfb 11238 die->offset.sect_off, objfile_name (objfile));
96408a79
SA
11239 else
11240 SET_FIELD_PHYSADDR (call_site->target, lowpc + baseaddr);
11241 }
11242 }
11243 else
11244 complaint (&symfile_complaints,
11245 _("DW_TAG_GNU_call_site DW_AT_GNU_call_site_target is neither "
11246 "block nor reference, for DIE 0x%x [in module %s]"),
4262abfb 11247 die->offset.sect_off, objfile_name (objfile));
96408a79
SA
11248
11249 call_site->per_cu = cu->per_cu;
11250
11251 for (child_die = die->child;
11252 child_die && child_die->tag;
11253 child_die = sibling_die (child_die))
11254 {
96408a79 11255 struct call_site_parameter *parameter;
1788b2d3 11256 struct attribute *loc, *origin;
96408a79
SA
11257
11258 if (child_die->tag != DW_TAG_GNU_call_site_parameter)
11259 {
11260 /* Already printed the complaint above. */
11261 continue;
11262 }
11263
11264 gdb_assert (call_site->parameter_count < nparams);
11265 parameter = &call_site->parameter[call_site->parameter_count];
11266
1788b2d3
JK
11267 /* DW_AT_location specifies the register number or DW_AT_abstract_origin
11268 specifies DW_TAG_formal_parameter. Value of the data assumed for the
11269 register is contained in DW_AT_GNU_call_site_value. */
96408a79 11270
24c5c679 11271 loc = dwarf2_attr (child_die, DW_AT_location, cu);
1788b2d3 11272 origin = dwarf2_attr (child_die, DW_AT_abstract_origin, cu);
7771576e 11273 if (loc == NULL && origin != NULL && attr_form_is_ref (origin))
1788b2d3
JK
11274 {
11275 sect_offset offset;
11276
11277 parameter->kind = CALL_SITE_PARAMETER_PARAM_OFFSET;
11278 offset = dwarf2_get_ref_die_offset (origin);
d76b7dbc
JK
11279 if (!offset_in_cu_p (&cu->header, offset))
11280 {
11281 /* As DW_OP_GNU_parameter_ref uses CU-relative offset this
11282 binding can be done only inside one CU. Such referenced DIE
11283 therefore cannot be even moved to DW_TAG_partial_unit. */
11284 complaint (&symfile_complaints,
11285 _("DW_AT_abstract_origin offset is not in CU for "
11286 "DW_TAG_GNU_call_site child DIE 0x%x "
11287 "[in module %s]"),
4262abfb 11288 child_die->offset.sect_off, objfile_name (objfile));
d76b7dbc
JK
11289 continue;
11290 }
1788b2d3
JK
11291 parameter->u.param_offset.cu_off = (offset.sect_off
11292 - cu->header.offset.sect_off);
11293 }
11294 else if (loc == NULL || origin != NULL || !attr_form_is_block (loc))
96408a79
SA
11295 {
11296 complaint (&symfile_complaints,
11297 _("No DW_FORM_block* DW_AT_location for "
11298 "DW_TAG_GNU_call_site child DIE 0x%x [in module %s]"),
4262abfb 11299 child_die->offset.sect_off, objfile_name (objfile));
96408a79
SA
11300 continue;
11301 }
24c5c679 11302 else
96408a79 11303 {
24c5c679
JK
11304 parameter->u.dwarf_reg = dwarf_block_to_dwarf_reg
11305 (DW_BLOCK (loc)->data, &DW_BLOCK (loc)->data[DW_BLOCK (loc)->size]);
11306 if (parameter->u.dwarf_reg != -1)
11307 parameter->kind = CALL_SITE_PARAMETER_DWARF_REG;
11308 else if (dwarf_block_to_sp_offset (gdbarch, DW_BLOCK (loc)->data,
11309 &DW_BLOCK (loc)->data[DW_BLOCK (loc)->size],
11310 &parameter->u.fb_offset))
11311 parameter->kind = CALL_SITE_PARAMETER_FB_OFFSET;
11312 else
11313 {
11314 complaint (&symfile_complaints,
11315 _("Only single DW_OP_reg or DW_OP_fbreg is supported "
11316 "for DW_FORM_block* DW_AT_location is supported for "
11317 "DW_TAG_GNU_call_site child DIE 0x%x "
11318 "[in module %s]"),
4262abfb 11319 child_die->offset.sect_off, objfile_name (objfile));
24c5c679
JK
11320 continue;
11321 }
96408a79
SA
11322 }
11323
11324 attr = dwarf2_attr (child_die, DW_AT_GNU_call_site_value, cu);
11325 if (!attr_form_is_block (attr))
11326 {
11327 complaint (&symfile_complaints,
11328 _("No DW_FORM_block* DW_AT_GNU_call_site_value for "
11329 "DW_TAG_GNU_call_site child DIE 0x%x [in module %s]"),
4262abfb 11330 child_die->offset.sect_off, objfile_name (objfile));
96408a79
SA
11331 continue;
11332 }
11333 parameter->value = DW_BLOCK (attr)->data;
11334 parameter->value_size = DW_BLOCK (attr)->size;
11335
11336 /* Parameters are not pre-cleared by memset above. */
11337 parameter->data_value = NULL;
11338 parameter->data_value_size = 0;
11339 call_site->parameter_count++;
11340
11341 attr = dwarf2_attr (child_die, DW_AT_GNU_call_site_data_value, cu);
11342 if (attr)
11343 {
11344 if (!attr_form_is_block (attr))
11345 complaint (&symfile_complaints,
11346 _("No DW_FORM_block* DW_AT_GNU_call_site_data_value for "
11347 "DW_TAG_GNU_call_site child DIE 0x%x [in module %s]"),
4262abfb 11348 child_die->offset.sect_off, objfile_name (objfile));
96408a79
SA
11349 else
11350 {
11351 parameter->data_value = DW_BLOCK (attr)->data;
11352 parameter->data_value_size = DW_BLOCK (attr)->size;
11353 }
11354 }
11355 }
11356}
11357
43039443 11358/* Get low and high pc attributes from DW_AT_ranges attribute value OFFSET.
ff013f42
JK
11359 Return 1 if the attributes are present and valid, otherwise, return 0.
11360 If RANGES_PST is not NULL we should setup `objfile->psymtabs_addrmap'. */
43039443
JK
11361
11362static int
11363dwarf2_ranges_read (unsigned offset, CORE_ADDR *low_return,
ff013f42
JK
11364 CORE_ADDR *high_return, struct dwarf2_cu *cu,
11365 struct partial_symtab *ranges_pst)
43039443
JK
11366{
11367 struct objfile *objfile = cu->objfile;
11368 struct comp_unit_head *cu_header = &cu->header;
11369 bfd *obfd = objfile->obfd;
11370 unsigned int addr_size = cu_header->addr_size;
11371 CORE_ADDR mask = ~(~(CORE_ADDR)1 << (addr_size * 8 - 1));
11372 /* Base address selection entry. */
11373 CORE_ADDR base;
11374 int found_base;
11375 unsigned int dummy;
d521ce57 11376 const gdb_byte *buffer;
43039443
JK
11377 CORE_ADDR marker;
11378 int low_set;
11379 CORE_ADDR low = 0;
11380 CORE_ADDR high = 0;
ff013f42 11381 CORE_ADDR baseaddr;
43039443 11382
d00adf39
DE
11383 found_base = cu->base_known;
11384 base = cu->base_address;
43039443 11385
be391dca 11386 dwarf2_read_section (objfile, &dwarf2_per_objfile->ranges);
dce234bc 11387 if (offset >= dwarf2_per_objfile->ranges.size)
43039443
JK
11388 {
11389 complaint (&symfile_complaints,
11390 _("Offset %d out of bounds for DW_AT_ranges attribute"),
11391 offset);
11392 return 0;
11393 }
dce234bc 11394 buffer = dwarf2_per_objfile->ranges.buffer + offset;
43039443
JK
11395
11396 /* Read in the largest possible address. */
11397 marker = read_address (obfd, buffer, cu, &dummy);
11398 if ((marker & mask) == mask)
11399 {
11400 /* If we found the largest possible address, then
11401 read the base address. */
11402 base = read_address (obfd, buffer + addr_size, cu, &dummy);
11403 buffer += 2 * addr_size;
11404 offset += 2 * addr_size;
11405 found_base = 1;
11406 }
11407
11408 low_set = 0;
11409
e7030f15 11410 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
ff013f42 11411
43039443
JK
11412 while (1)
11413 {
11414 CORE_ADDR range_beginning, range_end;
11415
11416 range_beginning = read_address (obfd, buffer, cu, &dummy);
11417 buffer += addr_size;
11418 range_end = read_address (obfd, buffer, cu, &dummy);
11419 buffer += addr_size;
11420 offset += 2 * addr_size;
11421
11422 /* An end of list marker is a pair of zero addresses. */
11423 if (range_beginning == 0 && range_end == 0)
11424 /* Found the end of list entry. */
11425 break;
11426
11427 /* Each base address selection entry is a pair of 2 values.
11428 The first is the largest possible address, the second is
11429 the base address. Check for a base address here. */
11430 if ((range_beginning & mask) == mask)
11431 {
11432 /* If we found the largest possible address, then
11433 read the base address. */
11434 base = read_address (obfd, buffer + addr_size, cu, &dummy);
11435 found_base = 1;
11436 continue;
11437 }
11438
11439 if (!found_base)
11440 {
11441 /* We have no valid base address for the ranges
11442 data. */
11443 complaint (&symfile_complaints,
11444 _("Invalid .debug_ranges data (no base address)"));
11445 return 0;
11446 }
11447
9277c30c
UW
11448 if (range_beginning > range_end)
11449 {
11450 /* Inverted range entries are invalid. */
11451 complaint (&symfile_complaints,
11452 _("Invalid .debug_ranges data (inverted range)"));
11453 return 0;
11454 }
11455
11456 /* Empty range entries have no effect. */
11457 if (range_beginning == range_end)
11458 continue;
11459
43039443
JK
11460 range_beginning += base;
11461 range_end += base;
11462
01093045
DE
11463 /* A not-uncommon case of bad debug info.
11464 Don't pollute the addrmap with bad data. */
11465 if (range_beginning + baseaddr == 0
11466 && !dwarf2_per_objfile->has_section_at_zero)
11467 {
11468 complaint (&symfile_complaints,
11469 _(".debug_ranges entry has start address of zero"
4262abfb 11470 " [in module %s]"), objfile_name (objfile));
01093045
DE
11471 continue;
11472 }
11473
9277c30c 11474 if (ranges_pst != NULL)
ff013f42 11475 addrmap_set_empty (objfile->psymtabs_addrmap,
3e43a32a
MS
11476 range_beginning + baseaddr,
11477 range_end - 1 + baseaddr,
ff013f42
JK
11478 ranges_pst);
11479
43039443
JK
11480 /* FIXME: This is recording everything as a low-high
11481 segment of consecutive addresses. We should have a
11482 data structure for discontiguous block ranges
11483 instead. */
11484 if (! low_set)
11485 {
11486 low = range_beginning;
11487 high = range_end;
11488 low_set = 1;
11489 }
11490 else
11491 {
11492 if (range_beginning < low)
11493 low = range_beginning;
11494 if (range_end > high)
11495 high = range_end;
11496 }
11497 }
11498
11499 if (! low_set)
11500 /* If the first entry is an end-of-list marker, the range
11501 describes an empty scope, i.e. no instructions. */
11502 return 0;
11503
11504 if (low_return)
11505 *low_return = low;
11506 if (high_return)
11507 *high_return = high;
11508 return 1;
11509}
11510
af34e669
DJ
11511/* Get low and high pc attributes from a die. Return 1 if the attributes
11512 are present and valid, otherwise, return 0. Return -1 if the range is
11513 discontinuous, i.e. derived from DW_AT_ranges information. */
380bca97 11514
c906108c 11515static int
af34e669 11516dwarf2_get_pc_bounds (struct die_info *die, CORE_ADDR *lowpc,
d85a05f0
DJ
11517 CORE_ADDR *highpc, struct dwarf2_cu *cu,
11518 struct partial_symtab *pst)
c906108c
SS
11519{
11520 struct attribute *attr;
91da1414 11521 struct attribute *attr_high;
af34e669
DJ
11522 CORE_ADDR low = 0;
11523 CORE_ADDR high = 0;
11524 int ret = 0;
c906108c 11525
91da1414
MW
11526 attr_high = dwarf2_attr (die, DW_AT_high_pc, cu);
11527 if (attr_high)
af34e669 11528 {
e142c38c 11529 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
af34e669 11530 if (attr)
91da1414
MW
11531 {
11532 low = DW_ADDR (attr);
3019eac3
DE
11533 if (attr_high->form == DW_FORM_addr
11534 || attr_high->form == DW_FORM_GNU_addr_index)
91da1414
MW
11535 high = DW_ADDR (attr_high);
11536 else
11537 high = low + DW_UNSND (attr_high);
11538 }
af34e669
DJ
11539 else
11540 /* Found high w/o low attribute. */
11541 return 0;
11542
11543 /* Found consecutive range of addresses. */
11544 ret = 1;
11545 }
c906108c 11546 else
af34e669 11547 {
e142c38c 11548 attr = dwarf2_attr (die, DW_AT_ranges, cu);
af34e669
DJ
11549 if (attr != NULL)
11550 {
ab435259
DE
11551 /* DW_AT_ranges_base does not apply to DIEs from the DWO skeleton.
11552 We take advantage of the fact that DW_AT_ranges does not appear
11553 in DW_TAG_compile_unit of DWO files. */
11554 int need_ranges_base = die->tag != DW_TAG_compile_unit;
11555 unsigned int ranges_offset = (DW_UNSND (attr)
11556 + (need_ranges_base
11557 ? cu->ranges_base
11558 : 0));
2e3cf129 11559
af34e669 11560 /* Value of the DW_AT_ranges attribute is the offset in the
a604369a 11561 .debug_ranges section. */
2e3cf129 11562 if (!dwarf2_ranges_read (ranges_offset, &low, &high, cu, pst))
af34e669 11563 return 0;
43039443 11564 /* Found discontinuous range of addresses. */
af34e669
DJ
11565 ret = -1;
11566 }
11567 }
c906108c 11568
9373cf26
JK
11569 /* read_partial_die has also the strict LOW < HIGH requirement. */
11570 if (high <= low)
c906108c
SS
11571 return 0;
11572
11573 /* When using the GNU linker, .gnu.linkonce. sections are used to
11574 eliminate duplicate copies of functions and vtables and such.
11575 The linker will arbitrarily choose one and discard the others.
11576 The AT_*_pc values for such functions refer to local labels in
11577 these sections. If the section from that file was discarded, the
11578 labels are not in the output, so the relocs get a value of 0.
11579 If this is a discarded function, mark the pc bounds as invalid,
11580 so that GDB will ignore it. */
72dca2f5 11581 if (low == 0 && !dwarf2_per_objfile->has_section_at_zero)
c906108c
SS
11582 return 0;
11583
11584 *lowpc = low;
96408a79
SA
11585 if (highpc)
11586 *highpc = high;
af34e669 11587 return ret;
c906108c
SS
11588}
11589
b084d499
JB
11590/* Assuming that DIE represents a subprogram DIE or a lexical block, get
11591 its low and high PC addresses. Do nothing if these addresses could not
11592 be determined. Otherwise, set LOWPC to the low address if it is smaller,
11593 and HIGHPC to the high address if greater than HIGHPC. */
11594
11595static void
11596dwarf2_get_subprogram_pc_bounds (struct die_info *die,
11597 CORE_ADDR *lowpc, CORE_ADDR *highpc,
11598 struct dwarf2_cu *cu)
11599{
11600 CORE_ADDR low, high;
11601 struct die_info *child = die->child;
11602
d85a05f0 11603 if (dwarf2_get_pc_bounds (die, &low, &high, cu, NULL))
b084d499
JB
11604 {
11605 *lowpc = min (*lowpc, low);
11606 *highpc = max (*highpc, high);
11607 }
11608
11609 /* If the language does not allow nested subprograms (either inside
11610 subprograms or lexical blocks), we're done. */
11611 if (cu->language != language_ada)
11612 return;
6e70227d 11613
b084d499
JB
11614 /* Check all the children of the given DIE. If it contains nested
11615 subprograms, then check their pc bounds. Likewise, we need to
11616 check lexical blocks as well, as they may also contain subprogram
11617 definitions. */
11618 while (child && child->tag)
11619 {
11620 if (child->tag == DW_TAG_subprogram
11621 || child->tag == DW_TAG_lexical_block)
11622 dwarf2_get_subprogram_pc_bounds (child, lowpc, highpc, cu);
11623 child = sibling_die (child);
11624 }
11625}
11626
fae299cd
DC
11627/* Get the low and high pc's represented by the scope DIE, and store
11628 them in *LOWPC and *HIGHPC. If the correct values can't be
11629 determined, set *LOWPC to -1 and *HIGHPC to 0. */
11630
11631static void
11632get_scope_pc_bounds (struct die_info *die,
11633 CORE_ADDR *lowpc, CORE_ADDR *highpc,
11634 struct dwarf2_cu *cu)
11635{
11636 CORE_ADDR best_low = (CORE_ADDR) -1;
11637 CORE_ADDR best_high = (CORE_ADDR) 0;
11638 CORE_ADDR current_low, current_high;
11639
d85a05f0 11640 if (dwarf2_get_pc_bounds (die, &current_low, &current_high, cu, NULL))
fae299cd
DC
11641 {
11642 best_low = current_low;
11643 best_high = current_high;
11644 }
11645 else
11646 {
11647 struct die_info *child = die->child;
11648
11649 while (child && child->tag)
11650 {
11651 switch (child->tag) {
11652 case DW_TAG_subprogram:
b084d499 11653 dwarf2_get_subprogram_pc_bounds (child, &best_low, &best_high, cu);
fae299cd
DC
11654 break;
11655 case DW_TAG_namespace:
f55ee35c 11656 case DW_TAG_module:
fae299cd
DC
11657 /* FIXME: carlton/2004-01-16: Should we do this for
11658 DW_TAG_class_type/DW_TAG_structure_type, too? I think
11659 that current GCC's always emit the DIEs corresponding
11660 to definitions of methods of classes as children of a
11661 DW_TAG_compile_unit or DW_TAG_namespace (as opposed to
11662 the DIEs giving the declarations, which could be
11663 anywhere). But I don't see any reason why the
11664 standards says that they have to be there. */
11665 get_scope_pc_bounds (child, &current_low, &current_high, cu);
11666
11667 if (current_low != ((CORE_ADDR) -1))
11668 {
11669 best_low = min (best_low, current_low);
11670 best_high = max (best_high, current_high);
11671 }
11672 break;
11673 default:
0963b4bd 11674 /* Ignore. */
fae299cd
DC
11675 break;
11676 }
11677
11678 child = sibling_die (child);
11679 }
11680 }
11681
11682 *lowpc = best_low;
11683 *highpc = best_high;
11684}
11685
801e3a5b
JB
11686/* Record the address ranges for BLOCK, offset by BASEADDR, as given
11687 in DIE. */
380bca97 11688
801e3a5b
JB
11689static void
11690dwarf2_record_block_ranges (struct die_info *die, struct block *block,
11691 CORE_ADDR baseaddr, struct dwarf2_cu *cu)
11692{
bb5ed363 11693 struct objfile *objfile = cu->objfile;
801e3a5b 11694 struct attribute *attr;
91da1414 11695 struct attribute *attr_high;
801e3a5b 11696
91da1414
MW
11697 attr_high = dwarf2_attr (die, DW_AT_high_pc, cu);
11698 if (attr_high)
801e3a5b 11699 {
801e3a5b
JB
11700 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
11701 if (attr)
11702 {
11703 CORE_ADDR low = DW_ADDR (attr);
91da1414 11704 CORE_ADDR high;
3019eac3
DE
11705 if (attr_high->form == DW_FORM_addr
11706 || attr_high->form == DW_FORM_GNU_addr_index)
91da1414
MW
11707 high = DW_ADDR (attr_high);
11708 else
11709 high = low + DW_UNSND (attr_high);
9a619af0 11710
801e3a5b
JB
11711 record_block_range (block, baseaddr + low, baseaddr + high - 1);
11712 }
11713 }
11714
11715 attr = dwarf2_attr (die, DW_AT_ranges, cu);
11716 if (attr)
11717 {
bb5ed363 11718 bfd *obfd = objfile->obfd;
ab435259
DE
11719 /* DW_AT_ranges_base does not apply to DIEs from the DWO skeleton.
11720 We take advantage of the fact that DW_AT_ranges does not appear
11721 in DW_TAG_compile_unit of DWO files. */
11722 int need_ranges_base = die->tag != DW_TAG_compile_unit;
801e3a5b
JB
11723
11724 /* The value of the DW_AT_ranges attribute is the offset of the
11725 address range list in the .debug_ranges section. */
ab435259
DE
11726 unsigned long offset = (DW_UNSND (attr)
11727 + (need_ranges_base ? cu->ranges_base : 0));
d62bfeaf 11728 const gdb_byte *buffer;
801e3a5b
JB
11729
11730 /* For some target architectures, but not others, the
11731 read_address function sign-extends the addresses it returns.
11732 To recognize base address selection entries, we need a
11733 mask. */
11734 unsigned int addr_size = cu->header.addr_size;
11735 CORE_ADDR base_select_mask = ~(~(CORE_ADDR)1 << (addr_size * 8 - 1));
11736
11737 /* The base address, to which the next pair is relative. Note
11738 that this 'base' is a DWARF concept: most entries in a range
11739 list are relative, to reduce the number of relocs against the
11740 debugging information. This is separate from this function's
11741 'baseaddr' argument, which GDB uses to relocate debugging
11742 information from a shared library based on the address at
11743 which the library was loaded. */
d00adf39
DE
11744 CORE_ADDR base = cu->base_address;
11745 int base_known = cu->base_known;
801e3a5b 11746
d62bfeaf 11747 dwarf2_read_section (objfile, &dwarf2_per_objfile->ranges);
dce234bc 11748 if (offset >= dwarf2_per_objfile->ranges.size)
801e3a5b
JB
11749 {
11750 complaint (&symfile_complaints,
11751 _("Offset %lu out of bounds for DW_AT_ranges attribute"),
11752 offset);
11753 return;
11754 }
d62bfeaf 11755 buffer = dwarf2_per_objfile->ranges.buffer + offset;
801e3a5b
JB
11756
11757 for (;;)
11758 {
11759 unsigned int bytes_read;
11760 CORE_ADDR start, end;
11761
11762 start = read_address (obfd, buffer, cu, &bytes_read);
11763 buffer += bytes_read;
11764 end = read_address (obfd, buffer, cu, &bytes_read);
11765 buffer += bytes_read;
11766
11767 /* Did we find the end of the range list? */
11768 if (start == 0 && end == 0)
11769 break;
11770
11771 /* Did we find a base address selection entry? */
11772 else if ((start & base_select_mask) == base_select_mask)
11773 {
11774 base = end;
11775 base_known = 1;
11776 }
11777
11778 /* We found an ordinary address range. */
11779 else
11780 {
11781 if (!base_known)
11782 {
11783 complaint (&symfile_complaints,
3e43a32a
MS
11784 _("Invalid .debug_ranges data "
11785 "(no base address)"));
801e3a5b
JB
11786 return;
11787 }
11788
9277c30c
UW
11789 if (start > end)
11790 {
11791 /* Inverted range entries are invalid. */
11792 complaint (&symfile_complaints,
11793 _("Invalid .debug_ranges data "
11794 "(inverted range)"));
11795 return;
11796 }
11797
11798 /* Empty range entries have no effect. */
11799 if (start == end)
11800 continue;
11801
01093045
DE
11802 start += base + baseaddr;
11803 end += base + baseaddr;
11804
11805 /* A not-uncommon case of bad debug info.
11806 Don't pollute the addrmap with bad data. */
11807 if (start == 0 && !dwarf2_per_objfile->has_section_at_zero)
11808 {
11809 complaint (&symfile_complaints,
11810 _(".debug_ranges entry has start address of zero"
4262abfb 11811 " [in module %s]"), objfile_name (objfile));
01093045
DE
11812 continue;
11813 }
11814
11815 record_block_range (block, start, end - 1);
801e3a5b
JB
11816 }
11817 }
11818 }
11819}
11820
685b1105
JK
11821/* Check whether the producer field indicates either of GCC < 4.6, or the
11822 Intel C/C++ compiler, and cache the result in CU. */
60d5a603 11823
685b1105
JK
11824static void
11825check_producer (struct dwarf2_cu *cu)
60d5a603
JK
11826{
11827 const char *cs;
11828 int major, minor, release;
11829
11830 if (cu->producer == NULL)
11831 {
11832 /* For unknown compilers expect their behavior is DWARF version
11833 compliant.
11834
11835 GCC started to support .debug_types sections by -gdwarf-4 since
11836 gcc-4.5.x. As the .debug_types sections are missing DW_AT_producer
11837 for their space efficiency GDB cannot workaround gcc-4.5.x -gdwarf-4
11838 combination. gcc-4.5.x -gdwarf-4 binaries have DW_AT_accessibility
11839 interpreted incorrectly by GDB now - GCC PR debug/48229. */
60d5a603 11840 }
685b1105 11841 else if (strncmp (cu->producer, "GNU ", strlen ("GNU ")) == 0)
60d5a603 11842 {
685b1105
JK
11843 /* Skip any identifier after "GNU " - such as "C++" or "Java". */
11844
ba919b58
TT
11845 cs = &cu->producer[strlen ("GNU ")];
11846 while (*cs && !isdigit (*cs))
11847 cs++;
11848 if (sscanf (cs, "%d.%d.%d", &major, &minor, &release) != 3)
11849 {
11850 /* Not recognized as GCC. */
11851 }
11852 else
1b80a9fa
JK
11853 {
11854 cu->producer_is_gxx_lt_4_6 = major < 4 || (major == 4 && minor < 6);
11855 cu->producer_is_gcc_lt_4_3 = major < 4 || (major == 4 && minor < 3);
11856 }
685b1105
JK
11857 }
11858 else if (strncmp (cu->producer, "Intel(R) C", strlen ("Intel(R) C")) == 0)
11859 cu->producer_is_icc = 1;
11860 else
11861 {
11862 /* For other non-GCC compilers, expect their behavior is DWARF version
11863 compliant. */
60d5a603
JK
11864 }
11865
ba919b58 11866 cu->checked_producer = 1;
685b1105 11867}
ba919b58 11868
685b1105
JK
11869/* Check for GCC PR debug/45124 fix which is not present in any G++ version up
11870 to 4.5.any while it is present already in G++ 4.6.0 - the PR has been fixed
11871 during 4.6.0 experimental. */
11872
11873static int
11874producer_is_gxx_lt_4_6 (struct dwarf2_cu *cu)
11875{
11876 if (!cu->checked_producer)
11877 check_producer (cu);
11878
11879 return cu->producer_is_gxx_lt_4_6;
60d5a603
JK
11880}
11881
11882/* Return the default accessibility type if it is not overriden by
11883 DW_AT_accessibility. */
11884
11885static enum dwarf_access_attribute
11886dwarf2_default_access_attribute (struct die_info *die, struct dwarf2_cu *cu)
11887{
11888 if (cu->header.version < 3 || producer_is_gxx_lt_4_6 (cu))
11889 {
11890 /* The default DWARF 2 accessibility for members is public, the default
11891 accessibility for inheritance is private. */
11892
11893 if (die->tag != DW_TAG_inheritance)
11894 return DW_ACCESS_public;
11895 else
11896 return DW_ACCESS_private;
11897 }
11898 else
11899 {
11900 /* DWARF 3+ defines the default accessibility a different way. The same
11901 rules apply now for DW_TAG_inheritance as for the members and it only
11902 depends on the container kind. */
11903
11904 if (die->parent->tag == DW_TAG_class_type)
11905 return DW_ACCESS_private;
11906 else
11907 return DW_ACCESS_public;
11908 }
11909}
11910
74ac6d43
TT
11911/* Look for DW_AT_data_member_location. Set *OFFSET to the byte
11912 offset. If the attribute was not found return 0, otherwise return
11913 1. If it was found but could not properly be handled, set *OFFSET
11914 to 0. */
11915
11916static int
11917handle_data_member_location (struct die_info *die, struct dwarf2_cu *cu,
11918 LONGEST *offset)
11919{
11920 struct attribute *attr;
11921
11922 attr = dwarf2_attr (die, DW_AT_data_member_location, cu);
11923 if (attr != NULL)
11924 {
11925 *offset = 0;
11926
11927 /* Note that we do not check for a section offset first here.
11928 This is because DW_AT_data_member_location is new in DWARF 4,
11929 so if we see it, we can assume that a constant form is really
11930 a constant and not a section offset. */
11931 if (attr_form_is_constant (attr))
11932 *offset = dwarf2_get_attr_constant_value (attr, 0);
11933 else if (attr_form_is_section_offset (attr))
11934 dwarf2_complex_location_expr_complaint ();
11935 else if (attr_form_is_block (attr))
11936 *offset = decode_locdesc (DW_BLOCK (attr), cu);
11937 else
11938 dwarf2_complex_location_expr_complaint ();
11939
11940 return 1;
11941 }
11942
11943 return 0;
11944}
11945
c906108c
SS
11946/* Add an aggregate field to the field list. */
11947
11948static void
107d2387 11949dwarf2_add_field (struct field_info *fip, struct die_info *die,
e7c27a73 11950 struct dwarf2_cu *cu)
6e70227d 11951{
e7c27a73 11952 struct objfile *objfile = cu->objfile;
5e2b427d 11953 struct gdbarch *gdbarch = get_objfile_arch (objfile);
c906108c
SS
11954 struct nextfield *new_field;
11955 struct attribute *attr;
11956 struct field *fp;
15d034d0 11957 const char *fieldname = "";
c906108c
SS
11958
11959 /* Allocate a new field list entry and link it in. */
11960 new_field = (struct nextfield *) xmalloc (sizeof (struct nextfield));
b8c9b27d 11961 make_cleanup (xfree, new_field);
c906108c 11962 memset (new_field, 0, sizeof (struct nextfield));
7d0ccb61
DJ
11963
11964 if (die->tag == DW_TAG_inheritance)
11965 {
11966 new_field->next = fip->baseclasses;
11967 fip->baseclasses = new_field;
11968 }
11969 else
11970 {
11971 new_field->next = fip->fields;
11972 fip->fields = new_field;
11973 }
c906108c
SS
11974 fip->nfields++;
11975
e142c38c 11976 attr = dwarf2_attr (die, DW_AT_accessibility, cu);
c906108c
SS
11977 if (attr)
11978 new_field->accessibility = DW_UNSND (attr);
60d5a603
JK
11979 else
11980 new_field->accessibility = dwarf2_default_access_attribute (die, cu);
c906108c
SS
11981 if (new_field->accessibility != DW_ACCESS_public)
11982 fip->non_public_fields = 1;
60d5a603 11983
e142c38c 11984 attr = dwarf2_attr (die, DW_AT_virtuality, cu);
c906108c
SS
11985 if (attr)
11986 new_field->virtuality = DW_UNSND (attr);
60d5a603
JK
11987 else
11988 new_field->virtuality = DW_VIRTUALITY_none;
c906108c
SS
11989
11990 fp = &new_field->field;
a9a9bd0f 11991
e142c38c 11992 if (die->tag == DW_TAG_member && ! die_is_declaration (die, cu))
c906108c 11993 {
74ac6d43
TT
11994 LONGEST offset;
11995
a9a9bd0f 11996 /* Data member other than a C++ static data member. */
6e70227d 11997
c906108c 11998 /* Get type of field. */
e7c27a73 11999 fp->type = die_type (die, cu);
c906108c 12000
d6a843b5 12001 SET_FIELD_BITPOS (*fp, 0);
01ad7f36 12002
c906108c 12003 /* Get bit size of field (zero if none). */
e142c38c 12004 attr = dwarf2_attr (die, DW_AT_bit_size, cu);
c906108c
SS
12005 if (attr)
12006 {
12007 FIELD_BITSIZE (*fp) = DW_UNSND (attr);
12008 }
12009 else
12010 {
12011 FIELD_BITSIZE (*fp) = 0;
12012 }
12013
12014 /* Get bit offset of field. */
74ac6d43
TT
12015 if (handle_data_member_location (die, cu, &offset))
12016 SET_FIELD_BITPOS (*fp, offset * bits_per_byte);
e142c38c 12017 attr = dwarf2_attr (die, DW_AT_bit_offset, cu);
c906108c
SS
12018 if (attr)
12019 {
5e2b427d 12020 if (gdbarch_bits_big_endian (gdbarch))
c906108c
SS
12021 {
12022 /* For big endian bits, the DW_AT_bit_offset gives the
c5aa993b
JM
12023 additional bit offset from the MSB of the containing
12024 anonymous object to the MSB of the field. We don't
12025 have to do anything special since we don't need to
12026 know the size of the anonymous object. */
f41f5e61 12027 SET_FIELD_BITPOS (*fp, FIELD_BITPOS (*fp) + DW_UNSND (attr));
c906108c
SS
12028 }
12029 else
12030 {
12031 /* For little endian bits, compute the bit offset to the
c5aa993b
JM
12032 MSB of the anonymous object, subtract off the number of
12033 bits from the MSB of the field to the MSB of the
12034 object, and then subtract off the number of bits of
12035 the field itself. The result is the bit offset of
12036 the LSB of the field. */
c906108c
SS
12037 int anonymous_size;
12038 int bit_offset = DW_UNSND (attr);
12039
e142c38c 12040 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
c906108c
SS
12041 if (attr)
12042 {
12043 /* The size of the anonymous object containing
12044 the bit field is explicit, so use the
12045 indicated size (in bytes). */
12046 anonymous_size = DW_UNSND (attr);
12047 }
12048 else
12049 {
12050 /* The size of the anonymous object containing
12051 the bit field must be inferred from the type
12052 attribute of the data member containing the
12053 bit field. */
12054 anonymous_size = TYPE_LENGTH (fp->type);
12055 }
f41f5e61
PA
12056 SET_FIELD_BITPOS (*fp,
12057 (FIELD_BITPOS (*fp)
12058 + anonymous_size * bits_per_byte
12059 - bit_offset - FIELD_BITSIZE (*fp)));
c906108c
SS
12060 }
12061 }
12062
12063 /* Get name of field. */
39cbfefa
DJ
12064 fieldname = dwarf2_name (die, cu);
12065 if (fieldname == NULL)
12066 fieldname = "";
d8151005
DJ
12067
12068 /* The name is already allocated along with this objfile, so we don't
12069 need to duplicate it for the type. */
12070 fp->name = fieldname;
c906108c
SS
12071
12072 /* Change accessibility for artificial fields (e.g. virtual table
c5aa993b 12073 pointer or virtual base class pointer) to private. */
e142c38c 12074 if (dwarf2_attr (die, DW_AT_artificial, cu))
c906108c 12075 {
d48cc9dd 12076 FIELD_ARTIFICIAL (*fp) = 1;
c906108c
SS
12077 new_field->accessibility = DW_ACCESS_private;
12078 fip->non_public_fields = 1;
12079 }
12080 }
a9a9bd0f 12081 else if (die->tag == DW_TAG_member || die->tag == DW_TAG_variable)
c906108c 12082 {
a9a9bd0f
DC
12083 /* C++ static member. */
12084
12085 /* NOTE: carlton/2002-11-05: It should be a DW_TAG_member that
12086 is a declaration, but all versions of G++ as of this writing
12087 (so through at least 3.2.1) incorrectly generate
12088 DW_TAG_variable tags. */
6e70227d 12089
ff355380 12090 const char *physname;
c906108c 12091
a9a9bd0f 12092 /* Get name of field. */
39cbfefa
DJ
12093 fieldname = dwarf2_name (die, cu);
12094 if (fieldname == NULL)
c906108c
SS
12095 return;
12096
254e6b9e 12097 attr = dwarf2_attr (die, DW_AT_const_value, cu);
3863f96c
DE
12098 if (attr
12099 /* Only create a symbol if this is an external value.
12100 new_symbol checks this and puts the value in the global symbol
12101 table, which we want. If it is not external, new_symbol
12102 will try to put the value in cu->list_in_scope which is wrong. */
12103 && dwarf2_flag_true_p (die, DW_AT_external, cu))
254e6b9e
DE
12104 {
12105 /* A static const member, not much different than an enum as far as
12106 we're concerned, except that we can support more types. */
12107 new_symbol (die, NULL, cu);
12108 }
12109
2df3850c 12110 /* Get physical name. */
ff355380 12111 physname = dwarf2_physname (fieldname, die, cu);
c906108c 12112
d8151005
DJ
12113 /* The name is already allocated along with this objfile, so we don't
12114 need to duplicate it for the type. */
12115 SET_FIELD_PHYSNAME (*fp, physname ? physname : "");
e7c27a73 12116 FIELD_TYPE (*fp) = die_type (die, cu);
d8151005 12117 FIELD_NAME (*fp) = fieldname;
c906108c
SS
12118 }
12119 else if (die->tag == DW_TAG_inheritance)
12120 {
74ac6d43 12121 LONGEST offset;
d4b96c9a 12122
74ac6d43
TT
12123 /* C++ base class field. */
12124 if (handle_data_member_location (die, cu, &offset))
12125 SET_FIELD_BITPOS (*fp, offset * bits_per_byte);
c906108c 12126 FIELD_BITSIZE (*fp) = 0;
e7c27a73 12127 FIELD_TYPE (*fp) = die_type (die, cu);
c906108c
SS
12128 FIELD_NAME (*fp) = type_name_no_tag (fp->type);
12129 fip->nbaseclasses++;
12130 }
12131}
12132
98751a41
JK
12133/* Add a typedef defined in the scope of the FIP's class. */
12134
12135static void
12136dwarf2_add_typedef (struct field_info *fip, struct die_info *die,
12137 struct dwarf2_cu *cu)
6e70227d 12138{
98751a41 12139 struct objfile *objfile = cu->objfile;
98751a41
JK
12140 struct typedef_field_list *new_field;
12141 struct attribute *attr;
12142 struct typedef_field *fp;
12143 char *fieldname = "";
12144
12145 /* Allocate a new field list entry and link it in. */
12146 new_field = xzalloc (sizeof (*new_field));
12147 make_cleanup (xfree, new_field);
12148
12149 gdb_assert (die->tag == DW_TAG_typedef);
12150
12151 fp = &new_field->field;
12152
12153 /* Get name of field. */
12154 fp->name = dwarf2_name (die, cu);
12155 if (fp->name == NULL)
12156 return;
12157
12158 fp->type = read_type_die (die, cu);
12159
12160 new_field->next = fip->typedef_field_list;
12161 fip->typedef_field_list = new_field;
12162 fip->typedef_field_list_count++;
12163}
12164
c906108c
SS
12165/* Create the vector of fields, and attach it to the type. */
12166
12167static void
fba45db2 12168dwarf2_attach_fields_to_type (struct field_info *fip, struct type *type,
e7c27a73 12169 struct dwarf2_cu *cu)
c906108c
SS
12170{
12171 int nfields = fip->nfields;
12172
12173 /* Record the field count, allocate space for the array of fields,
12174 and create blank accessibility bitfields if necessary. */
12175 TYPE_NFIELDS (type) = nfields;
12176 TYPE_FIELDS (type) = (struct field *)
12177 TYPE_ALLOC (type, sizeof (struct field) * nfields);
12178 memset (TYPE_FIELDS (type), 0, sizeof (struct field) * nfields);
12179
b4ba55a1 12180 if (fip->non_public_fields && cu->language != language_ada)
c906108c
SS
12181 {
12182 ALLOCATE_CPLUS_STRUCT_TYPE (type);
12183
12184 TYPE_FIELD_PRIVATE_BITS (type) =
12185 (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
12186 B_CLRALL (TYPE_FIELD_PRIVATE_BITS (type), nfields);
12187
12188 TYPE_FIELD_PROTECTED_BITS (type) =
12189 (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
12190 B_CLRALL (TYPE_FIELD_PROTECTED_BITS (type), nfields);
12191
774b6a14
TT
12192 TYPE_FIELD_IGNORE_BITS (type) =
12193 (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
12194 B_CLRALL (TYPE_FIELD_IGNORE_BITS (type), nfields);
c906108c
SS
12195 }
12196
12197 /* If the type has baseclasses, allocate and clear a bit vector for
12198 TYPE_FIELD_VIRTUAL_BITS. */
b4ba55a1 12199 if (fip->nbaseclasses && cu->language != language_ada)
c906108c
SS
12200 {
12201 int num_bytes = B_BYTES (fip->nbaseclasses);
fe1b8b76 12202 unsigned char *pointer;
c906108c
SS
12203
12204 ALLOCATE_CPLUS_STRUCT_TYPE (type);
fe1b8b76
JB
12205 pointer = TYPE_ALLOC (type, num_bytes);
12206 TYPE_FIELD_VIRTUAL_BITS (type) = pointer;
c906108c
SS
12207 B_CLRALL (TYPE_FIELD_VIRTUAL_BITS (type), fip->nbaseclasses);
12208 TYPE_N_BASECLASSES (type) = fip->nbaseclasses;
12209 }
12210
3e43a32a
MS
12211 /* Copy the saved-up fields into the field vector. Start from the head of
12212 the list, adding to the tail of the field array, so that they end up in
12213 the same order in the array in which they were added to the list. */
c906108c
SS
12214 while (nfields-- > 0)
12215 {
7d0ccb61
DJ
12216 struct nextfield *fieldp;
12217
12218 if (fip->fields)
12219 {
12220 fieldp = fip->fields;
12221 fip->fields = fieldp->next;
12222 }
12223 else
12224 {
12225 fieldp = fip->baseclasses;
12226 fip->baseclasses = fieldp->next;
12227 }
12228
12229 TYPE_FIELD (type, nfields) = fieldp->field;
12230 switch (fieldp->accessibility)
c906108c 12231 {
c5aa993b 12232 case DW_ACCESS_private:
b4ba55a1
JB
12233 if (cu->language != language_ada)
12234 SET_TYPE_FIELD_PRIVATE (type, nfields);
c5aa993b 12235 break;
c906108c 12236
c5aa993b 12237 case DW_ACCESS_protected:
b4ba55a1
JB
12238 if (cu->language != language_ada)
12239 SET_TYPE_FIELD_PROTECTED (type, nfields);
c5aa993b 12240 break;
c906108c 12241
c5aa993b
JM
12242 case DW_ACCESS_public:
12243 break;
c906108c 12244
c5aa993b
JM
12245 default:
12246 /* Unknown accessibility. Complain and treat it as public. */
12247 {
e2e0b3e5 12248 complaint (&symfile_complaints, _("unsupported accessibility %d"),
7d0ccb61 12249 fieldp->accessibility);
c5aa993b
JM
12250 }
12251 break;
c906108c
SS
12252 }
12253 if (nfields < fip->nbaseclasses)
12254 {
7d0ccb61 12255 switch (fieldp->virtuality)
c906108c 12256 {
c5aa993b
JM
12257 case DW_VIRTUALITY_virtual:
12258 case DW_VIRTUALITY_pure_virtual:
b4ba55a1 12259 if (cu->language == language_ada)
a73c6dcd 12260 error (_("unexpected virtuality in component of Ada type"));
c5aa993b
JM
12261 SET_TYPE_FIELD_VIRTUAL (type, nfields);
12262 break;
c906108c
SS
12263 }
12264 }
c906108c
SS
12265 }
12266}
12267
7d27a96d
TT
12268/* Return true if this member function is a constructor, false
12269 otherwise. */
12270
12271static int
12272dwarf2_is_constructor (struct die_info *die, struct dwarf2_cu *cu)
12273{
12274 const char *fieldname;
12275 const char *typename;
12276 int len;
12277
12278 if (die->parent == NULL)
12279 return 0;
12280
12281 if (die->parent->tag != DW_TAG_structure_type
12282 && die->parent->tag != DW_TAG_union_type
12283 && die->parent->tag != DW_TAG_class_type)
12284 return 0;
12285
12286 fieldname = dwarf2_name (die, cu);
12287 typename = dwarf2_name (die->parent, cu);
12288 if (fieldname == NULL || typename == NULL)
12289 return 0;
12290
12291 len = strlen (fieldname);
12292 return (strncmp (fieldname, typename, len) == 0
12293 && (typename[len] == '\0' || typename[len] == '<'));
12294}
12295
c906108c
SS
12296/* Add a member function to the proper fieldlist. */
12297
12298static void
107d2387 12299dwarf2_add_member_fn (struct field_info *fip, struct die_info *die,
e7c27a73 12300 struct type *type, struct dwarf2_cu *cu)
c906108c 12301{
e7c27a73 12302 struct objfile *objfile = cu->objfile;
c906108c
SS
12303 struct attribute *attr;
12304 struct fnfieldlist *flp;
12305 int i;
12306 struct fn_field *fnp;
15d034d0 12307 const char *fieldname;
c906108c 12308 struct nextfnfield *new_fnfield;
f792889a 12309 struct type *this_type;
60d5a603 12310 enum dwarf_access_attribute accessibility;
c906108c 12311
b4ba55a1 12312 if (cu->language == language_ada)
a73c6dcd 12313 error (_("unexpected member function in Ada type"));
b4ba55a1 12314
2df3850c 12315 /* Get name of member function. */
39cbfefa
DJ
12316 fieldname = dwarf2_name (die, cu);
12317 if (fieldname == NULL)
2df3850c 12318 return;
c906108c 12319
c906108c
SS
12320 /* Look up member function name in fieldlist. */
12321 for (i = 0; i < fip->nfnfields; i++)
12322 {
27bfe10e 12323 if (strcmp (fip->fnfieldlists[i].name, fieldname) == 0)
c906108c
SS
12324 break;
12325 }
12326
12327 /* Create new list element if necessary. */
12328 if (i < fip->nfnfields)
12329 flp = &fip->fnfieldlists[i];
12330 else
12331 {
12332 if ((fip->nfnfields % DW_FIELD_ALLOC_CHUNK) == 0)
12333 {
12334 fip->fnfieldlists = (struct fnfieldlist *)
12335 xrealloc (fip->fnfieldlists,
12336 (fip->nfnfields + DW_FIELD_ALLOC_CHUNK)
c5aa993b 12337 * sizeof (struct fnfieldlist));
c906108c 12338 if (fip->nfnfields == 0)
c13c43fd 12339 make_cleanup (free_current_contents, &fip->fnfieldlists);
c906108c
SS
12340 }
12341 flp = &fip->fnfieldlists[fip->nfnfields];
12342 flp->name = fieldname;
12343 flp->length = 0;
12344 flp->head = NULL;
3da10d80 12345 i = fip->nfnfields++;
c906108c
SS
12346 }
12347
12348 /* Create a new member function field and chain it to the field list
0963b4bd 12349 entry. */
c906108c 12350 new_fnfield = (struct nextfnfield *) xmalloc (sizeof (struct nextfnfield));
b8c9b27d 12351 make_cleanup (xfree, new_fnfield);
c906108c
SS
12352 memset (new_fnfield, 0, sizeof (struct nextfnfield));
12353 new_fnfield->next = flp->head;
12354 flp->head = new_fnfield;
12355 flp->length++;
12356
12357 /* Fill in the member function field info. */
12358 fnp = &new_fnfield->fnfield;
3da10d80
KS
12359
12360 /* Delay processing of the physname until later. */
12361 if (cu->language == language_cplus || cu->language == language_java)
12362 {
12363 add_to_method_list (type, i, flp->length - 1, fieldname,
12364 die, cu);
12365 }
12366 else
12367 {
1d06ead6 12368 const char *physname = dwarf2_physname (fieldname, die, cu);
3da10d80
KS
12369 fnp->physname = physname ? physname : "";
12370 }
12371
c906108c 12372 fnp->type = alloc_type (objfile);
f792889a
DJ
12373 this_type = read_type_die (die, cu);
12374 if (this_type && TYPE_CODE (this_type) == TYPE_CODE_FUNC)
c906108c 12375 {
f792889a 12376 int nparams = TYPE_NFIELDS (this_type);
c906108c 12377
f792889a 12378 /* TYPE is the domain of this method, and THIS_TYPE is the type
e26fb1d7
DC
12379 of the method itself (TYPE_CODE_METHOD). */
12380 smash_to_method_type (fnp->type, type,
f792889a
DJ
12381 TYPE_TARGET_TYPE (this_type),
12382 TYPE_FIELDS (this_type),
12383 TYPE_NFIELDS (this_type),
12384 TYPE_VARARGS (this_type));
c906108c
SS
12385
12386 /* Handle static member functions.
c5aa993b 12387 Dwarf2 has no clean way to discern C++ static and non-static
0963b4bd
MS
12388 member functions. G++ helps GDB by marking the first
12389 parameter for non-static member functions (which is the this
12390 pointer) as artificial. We obtain this information from
12391 read_subroutine_type via TYPE_FIELD_ARTIFICIAL. */
f792889a 12392 if (nparams == 0 || TYPE_FIELD_ARTIFICIAL (this_type, 0) == 0)
c906108c
SS
12393 fnp->voffset = VOFFSET_STATIC;
12394 }
12395 else
e2e0b3e5 12396 complaint (&symfile_complaints, _("member function type missing for '%s'"),
3da10d80 12397 dwarf2_full_name (fieldname, die, cu));
c906108c
SS
12398
12399 /* Get fcontext from DW_AT_containing_type if present. */
e142c38c 12400 if (dwarf2_attr (die, DW_AT_containing_type, cu) != NULL)
e7c27a73 12401 fnp->fcontext = die_containing_type (die, cu);
c906108c 12402
3e43a32a
MS
12403 /* dwarf2 doesn't have stubbed physical names, so the setting of is_const and
12404 is_volatile is irrelevant, as it is needed by gdb_mangle_name only. */
c906108c
SS
12405
12406 /* Get accessibility. */
e142c38c 12407 attr = dwarf2_attr (die, DW_AT_accessibility, cu);
c906108c 12408 if (attr)
60d5a603
JK
12409 accessibility = DW_UNSND (attr);
12410 else
12411 accessibility = dwarf2_default_access_attribute (die, cu);
12412 switch (accessibility)
c906108c 12413 {
60d5a603
JK
12414 case DW_ACCESS_private:
12415 fnp->is_private = 1;
12416 break;
12417 case DW_ACCESS_protected:
12418 fnp->is_protected = 1;
12419 break;
c906108c
SS
12420 }
12421
b02dede2 12422 /* Check for artificial methods. */
e142c38c 12423 attr = dwarf2_attr (die, DW_AT_artificial, cu);
b02dede2
DJ
12424 if (attr && DW_UNSND (attr) != 0)
12425 fnp->is_artificial = 1;
12426
7d27a96d
TT
12427 fnp->is_constructor = dwarf2_is_constructor (die, cu);
12428
0d564a31 12429 /* Get index in virtual function table if it is a virtual member
aec5aa8b
TT
12430 function. For older versions of GCC, this is an offset in the
12431 appropriate virtual table, as specified by DW_AT_containing_type.
12432 For everyone else, it is an expression to be evaluated relative
0d564a31
DJ
12433 to the object address. */
12434
e142c38c 12435 attr = dwarf2_attr (die, DW_AT_vtable_elem_location, cu);
aec5aa8b 12436 if (attr)
8e19ed76 12437 {
aec5aa8b 12438 if (attr_form_is_block (attr) && DW_BLOCK (attr)->size > 0)
8e19ed76 12439 {
aec5aa8b
TT
12440 if (DW_BLOCK (attr)->data[0] == DW_OP_constu)
12441 {
12442 /* Old-style GCC. */
12443 fnp->voffset = decode_locdesc (DW_BLOCK (attr), cu) + 2;
12444 }
12445 else if (DW_BLOCK (attr)->data[0] == DW_OP_deref
12446 || (DW_BLOCK (attr)->size > 1
12447 && DW_BLOCK (attr)->data[0] == DW_OP_deref_size
12448 && DW_BLOCK (attr)->data[1] == cu->header.addr_size))
12449 {
12450 struct dwarf_block blk;
12451 int offset;
12452
12453 offset = (DW_BLOCK (attr)->data[0] == DW_OP_deref
12454 ? 1 : 2);
12455 blk.size = DW_BLOCK (attr)->size - offset;
12456 blk.data = DW_BLOCK (attr)->data + offset;
12457 fnp->voffset = decode_locdesc (DW_BLOCK (attr), cu);
12458 if ((fnp->voffset % cu->header.addr_size) != 0)
12459 dwarf2_complex_location_expr_complaint ();
12460 else
12461 fnp->voffset /= cu->header.addr_size;
12462 fnp->voffset += 2;
12463 }
12464 else
12465 dwarf2_complex_location_expr_complaint ();
12466
12467 if (!fnp->fcontext)
12468 fnp->fcontext = TYPE_TARGET_TYPE (TYPE_FIELD_TYPE (this_type, 0));
12469 }
3690dd37 12470 else if (attr_form_is_section_offset (attr))
8e19ed76 12471 {
4d3c2250 12472 dwarf2_complex_location_expr_complaint ();
8e19ed76
PS
12473 }
12474 else
12475 {
4d3c2250
KB
12476 dwarf2_invalid_attrib_class_complaint ("DW_AT_vtable_elem_location",
12477 fieldname);
8e19ed76 12478 }
0d564a31 12479 }
d48cc9dd
DJ
12480 else
12481 {
12482 attr = dwarf2_attr (die, DW_AT_virtuality, cu);
12483 if (attr && DW_UNSND (attr))
12484 {
12485 /* GCC does this, as of 2008-08-25; PR debug/37237. */
12486 complaint (&symfile_complaints,
3e43a32a
MS
12487 _("Member function \"%s\" (offset %d) is virtual "
12488 "but the vtable offset is not specified"),
b64f50a1 12489 fieldname, die->offset.sect_off);
9655fd1a 12490 ALLOCATE_CPLUS_STRUCT_TYPE (type);
d48cc9dd
DJ
12491 TYPE_CPLUS_DYNAMIC (type) = 1;
12492 }
12493 }
c906108c
SS
12494}
12495
12496/* Create the vector of member function fields, and attach it to the type. */
12497
12498static void
fba45db2 12499dwarf2_attach_fn_fields_to_type (struct field_info *fip, struct type *type,
e7c27a73 12500 struct dwarf2_cu *cu)
c906108c
SS
12501{
12502 struct fnfieldlist *flp;
c906108c
SS
12503 int i;
12504
b4ba55a1 12505 if (cu->language == language_ada)
a73c6dcd 12506 error (_("unexpected member functions in Ada type"));
b4ba55a1 12507
c906108c
SS
12508 ALLOCATE_CPLUS_STRUCT_TYPE (type);
12509 TYPE_FN_FIELDLISTS (type) = (struct fn_fieldlist *)
12510 TYPE_ALLOC (type, sizeof (struct fn_fieldlist) * fip->nfnfields);
12511
12512 for (i = 0, flp = fip->fnfieldlists; i < fip->nfnfields; i++, flp++)
12513 {
12514 struct nextfnfield *nfp = flp->head;
12515 struct fn_fieldlist *fn_flp = &TYPE_FN_FIELDLIST (type, i);
12516 int k;
12517
12518 TYPE_FN_FIELDLIST_NAME (type, i) = flp->name;
12519 TYPE_FN_FIELDLIST_LENGTH (type, i) = flp->length;
12520 fn_flp->fn_fields = (struct fn_field *)
12521 TYPE_ALLOC (type, sizeof (struct fn_field) * flp->length);
12522 for (k = flp->length; (k--, nfp); nfp = nfp->next)
c5aa993b 12523 fn_flp->fn_fields[k] = nfp->fnfield;
c906108c
SS
12524 }
12525
12526 TYPE_NFN_FIELDS (type) = fip->nfnfields;
c906108c
SS
12527}
12528
1168df01
JB
12529/* Returns non-zero if NAME is the name of a vtable member in CU's
12530 language, zero otherwise. */
12531static int
12532is_vtable_name (const char *name, struct dwarf2_cu *cu)
12533{
12534 static const char vptr[] = "_vptr";
987504bb 12535 static const char vtable[] = "vtable";
1168df01 12536
987504bb
JJ
12537 /* Look for the C++ and Java forms of the vtable. */
12538 if ((cu->language == language_java
12539 && strncmp (name, vtable, sizeof (vtable) - 1) == 0)
12540 || (strncmp (name, vptr, sizeof (vptr) - 1) == 0
12541 && is_cplus_marker (name[sizeof (vptr) - 1])))
1168df01
JB
12542 return 1;
12543
12544 return 0;
12545}
12546
c0dd20ea 12547/* GCC outputs unnamed structures that are really pointers to member
0b92b5bb
TT
12548 functions, with the ABI-specified layout. If TYPE describes
12549 such a structure, smash it into a member function type.
61049d3b
DJ
12550
12551 GCC shouldn't do this; it should just output pointer to member DIEs.
12552 This is GCC PR debug/28767. */
c0dd20ea 12553
0b92b5bb
TT
12554static void
12555quirk_gcc_member_function_pointer (struct type *type, struct objfile *objfile)
c0dd20ea 12556{
0b92b5bb 12557 struct type *pfn_type, *domain_type, *new_type;
c0dd20ea
DJ
12558
12559 /* Check for a structure with no name and two children. */
0b92b5bb
TT
12560 if (TYPE_CODE (type) != TYPE_CODE_STRUCT || TYPE_NFIELDS (type) != 2)
12561 return;
c0dd20ea
DJ
12562
12563 /* Check for __pfn and __delta members. */
0b92b5bb
TT
12564 if (TYPE_FIELD_NAME (type, 0) == NULL
12565 || strcmp (TYPE_FIELD_NAME (type, 0), "__pfn") != 0
12566 || TYPE_FIELD_NAME (type, 1) == NULL
12567 || strcmp (TYPE_FIELD_NAME (type, 1), "__delta") != 0)
12568 return;
c0dd20ea
DJ
12569
12570 /* Find the type of the method. */
0b92b5bb 12571 pfn_type = TYPE_FIELD_TYPE (type, 0);
c0dd20ea
DJ
12572 if (pfn_type == NULL
12573 || TYPE_CODE (pfn_type) != TYPE_CODE_PTR
12574 || TYPE_CODE (TYPE_TARGET_TYPE (pfn_type)) != TYPE_CODE_FUNC)
0b92b5bb 12575 return;
c0dd20ea
DJ
12576
12577 /* Look for the "this" argument. */
12578 pfn_type = TYPE_TARGET_TYPE (pfn_type);
12579 if (TYPE_NFIELDS (pfn_type) == 0
0b92b5bb 12580 /* || TYPE_FIELD_TYPE (pfn_type, 0) == NULL */
c0dd20ea 12581 || TYPE_CODE (TYPE_FIELD_TYPE (pfn_type, 0)) != TYPE_CODE_PTR)
0b92b5bb 12582 return;
c0dd20ea
DJ
12583
12584 domain_type = TYPE_TARGET_TYPE (TYPE_FIELD_TYPE (pfn_type, 0));
0b92b5bb
TT
12585 new_type = alloc_type (objfile);
12586 smash_to_method_type (new_type, domain_type, TYPE_TARGET_TYPE (pfn_type),
c0dd20ea
DJ
12587 TYPE_FIELDS (pfn_type), TYPE_NFIELDS (pfn_type),
12588 TYPE_VARARGS (pfn_type));
0b92b5bb 12589 smash_to_methodptr_type (type, new_type);
c0dd20ea 12590}
1168df01 12591
685b1105
JK
12592/* Return non-zero if the CU's PRODUCER string matches the Intel C/C++ compiler
12593 (icc). */
12594
12595static int
12596producer_is_icc (struct dwarf2_cu *cu)
12597{
12598 if (!cu->checked_producer)
12599 check_producer (cu);
12600
12601 return cu->producer_is_icc;
12602}
12603
c906108c 12604/* Called when we find the DIE that starts a structure or union scope
c767944b
DJ
12605 (definition) to create a type for the structure or union. Fill in
12606 the type's name and general properties; the members will not be
3d1d5ea3 12607 processed until process_structure_scope.
c906108c 12608
c767944b
DJ
12609 NOTE: we need to call these functions regardless of whether or not the
12610 DIE has a DW_AT_name attribute, since it might be an anonymous
c906108c
SS
12611 structure or union. This gets the type entered into our set of
12612 user defined types.
12613
12614 However, if the structure is incomplete (an opaque struct/union)
12615 then suppress creating a symbol table entry for it since gdb only
12616 wants to find the one with the complete definition. Note that if
12617 it is complete, we just call new_symbol, which does it's own
12618 checking about whether the struct/union is anonymous or not (and
12619 suppresses creating a symbol table entry itself). */
12620
f792889a 12621static struct type *
134d01f1 12622read_structure_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 12623{
e7c27a73 12624 struct objfile *objfile = cu->objfile;
c906108c
SS
12625 struct type *type;
12626 struct attribute *attr;
15d034d0 12627 const char *name;
c906108c 12628
348e048f
DE
12629 /* If the definition of this type lives in .debug_types, read that type.
12630 Don't follow DW_AT_specification though, that will take us back up
12631 the chain and we want to go down. */
45e58e77 12632 attr = dwarf2_attr_no_follow (die, DW_AT_signature);
348e048f
DE
12633 if (attr)
12634 {
ac9ec31b 12635 type = get_DW_AT_signature_type (die, attr, cu);
9dc481d3 12636
ac9ec31b 12637 /* The type's CU may not be the same as CU.
02142a6c 12638 Ensure TYPE is recorded with CU in die_type_hash. */
348e048f
DE
12639 return set_die_type (die, type, cu);
12640 }
12641
c0dd20ea 12642 type = alloc_type (objfile);
c906108c 12643 INIT_CPLUS_SPECIFIC (type);
93311388 12644
39cbfefa
DJ
12645 name = dwarf2_name (die, cu);
12646 if (name != NULL)
c906108c 12647 {
987504bb
JJ
12648 if (cu->language == language_cplus
12649 || cu->language == language_java)
63d06c5c 12650 {
15d034d0 12651 const char *full_name = dwarf2_full_name (name, die, cu);
3da10d80
KS
12652
12653 /* dwarf2_full_name might have already finished building the DIE's
12654 type. If so, there is no need to continue. */
12655 if (get_die_type (die, cu) != NULL)
12656 return get_die_type (die, cu);
12657
12658 TYPE_TAG_NAME (type) = full_name;
94af9270
KS
12659 if (die->tag == DW_TAG_structure_type
12660 || die->tag == DW_TAG_class_type)
12661 TYPE_NAME (type) = TYPE_TAG_NAME (type);
63d06c5c
DC
12662 }
12663 else
12664 {
d8151005
DJ
12665 /* The name is already allocated along with this objfile, so
12666 we don't need to duplicate it for the type. */
7d455152 12667 TYPE_TAG_NAME (type) = name;
94af9270
KS
12668 if (die->tag == DW_TAG_class_type)
12669 TYPE_NAME (type) = TYPE_TAG_NAME (type);
63d06c5c 12670 }
c906108c
SS
12671 }
12672
12673 if (die->tag == DW_TAG_structure_type)
12674 {
12675 TYPE_CODE (type) = TYPE_CODE_STRUCT;
12676 }
12677 else if (die->tag == DW_TAG_union_type)
12678 {
12679 TYPE_CODE (type) = TYPE_CODE_UNION;
12680 }
12681 else
12682 {
c906108c
SS
12683 TYPE_CODE (type) = TYPE_CODE_CLASS;
12684 }
12685
0cc2414c
TT
12686 if (cu->language == language_cplus && die->tag == DW_TAG_class_type)
12687 TYPE_DECLARED_CLASS (type) = 1;
12688
e142c38c 12689 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
c906108c
SS
12690 if (attr)
12691 {
12692 TYPE_LENGTH (type) = DW_UNSND (attr);
12693 }
12694 else
12695 {
12696 TYPE_LENGTH (type) = 0;
12697 }
12698
685b1105
JK
12699 if (producer_is_icc (cu))
12700 {
12701 /* ICC does not output the required DW_AT_declaration
12702 on incomplete types, but gives them a size of zero. */
12703 }
12704 else
12705 TYPE_STUB_SUPPORTED (type) = 1;
12706
dc718098 12707 if (die_is_declaration (die, cu))
876cecd0 12708 TYPE_STUB (type) = 1;
a6c727b2
DJ
12709 else if (attr == NULL && die->child == NULL
12710 && producer_is_realview (cu->producer))
12711 /* RealView does not output the required DW_AT_declaration
12712 on incomplete types. */
12713 TYPE_STUB (type) = 1;
dc718098 12714
c906108c
SS
12715 /* We need to add the type field to the die immediately so we don't
12716 infinitely recurse when dealing with pointers to the structure
0963b4bd 12717 type within the structure itself. */
1c379e20 12718 set_die_type (die, type, cu);
c906108c 12719
7e314c57
JK
12720 /* set_die_type should be already done. */
12721 set_descriptive_type (type, die, cu);
12722
c767944b
DJ
12723 return type;
12724}
12725
12726/* Finish creating a structure or union type, including filling in
12727 its members and creating a symbol for it. */
12728
12729static void
12730process_structure_scope (struct die_info *die, struct dwarf2_cu *cu)
12731{
12732 struct objfile *objfile = cu->objfile;
12733 struct die_info *child_die = die->child;
12734 struct type *type;
12735
12736 type = get_die_type (die, cu);
12737 if (type == NULL)
12738 type = read_structure_type (die, cu);
12739
e142c38c 12740 if (die->child != NULL && ! die_is_declaration (die, cu))
c906108c
SS
12741 {
12742 struct field_info fi;
12743 struct die_info *child_die;
34eaf542 12744 VEC (symbolp) *template_args = NULL;
c767944b 12745 struct cleanup *back_to = make_cleanup (null_cleanup, 0);
c906108c
SS
12746
12747 memset (&fi, 0, sizeof (struct field_info));
12748
639d11d3 12749 child_die = die->child;
c906108c
SS
12750
12751 while (child_die && child_die->tag)
12752 {
a9a9bd0f
DC
12753 if (child_die->tag == DW_TAG_member
12754 || child_die->tag == DW_TAG_variable)
c906108c 12755 {
a9a9bd0f
DC
12756 /* NOTE: carlton/2002-11-05: A C++ static data member
12757 should be a DW_TAG_member that is a declaration, but
12758 all versions of G++ as of this writing (so through at
12759 least 3.2.1) incorrectly generate DW_TAG_variable
12760 tags for them instead. */
e7c27a73 12761 dwarf2_add_field (&fi, child_die, cu);
c906108c 12762 }
8713b1b1 12763 else if (child_die->tag == DW_TAG_subprogram)
c906108c 12764 {
0963b4bd 12765 /* C++ member function. */
e7c27a73 12766 dwarf2_add_member_fn (&fi, child_die, type, cu);
c906108c
SS
12767 }
12768 else if (child_die->tag == DW_TAG_inheritance)
12769 {
12770 /* C++ base class field. */
e7c27a73 12771 dwarf2_add_field (&fi, child_die, cu);
c906108c 12772 }
98751a41
JK
12773 else if (child_die->tag == DW_TAG_typedef)
12774 dwarf2_add_typedef (&fi, child_die, cu);
34eaf542
TT
12775 else if (child_die->tag == DW_TAG_template_type_param
12776 || child_die->tag == DW_TAG_template_value_param)
12777 {
12778 struct symbol *arg = new_symbol (child_die, NULL, cu);
12779
f1078f66
DJ
12780 if (arg != NULL)
12781 VEC_safe_push (symbolp, template_args, arg);
34eaf542
TT
12782 }
12783
c906108c
SS
12784 child_die = sibling_die (child_die);
12785 }
12786
34eaf542
TT
12787 /* Attach template arguments to type. */
12788 if (! VEC_empty (symbolp, template_args))
12789 {
12790 ALLOCATE_CPLUS_STRUCT_TYPE (type);
12791 TYPE_N_TEMPLATE_ARGUMENTS (type)
12792 = VEC_length (symbolp, template_args);
12793 TYPE_TEMPLATE_ARGUMENTS (type)
12794 = obstack_alloc (&objfile->objfile_obstack,
12795 (TYPE_N_TEMPLATE_ARGUMENTS (type)
12796 * sizeof (struct symbol *)));
12797 memcpy (TYPE_TEMPLATE_ARGUMENTS (type),
12798 VEC_address (symbolp, template_args),
12799 (TYPE_N_TEMPLATE_ARGUMENTS (type)
12800 * sizeof (struct symbol *)));
12801 VEC_free (symbolp, template_args);
12802 }
12803
c906108c
SS
12804 /* Attach fields and member functions to the type. */
12805 if (fi.nfields)
e7c27a73 12806 dwarf2_attach_fields_to_type (&fi, type, cu);
c906108c
SS
12807 if (fi.nfnfields)
12808 {
e7c27a73 12809 dwarf2_attach_fn_fields_to_type (&fi, type, cu);
c906108c 12810
c5aa993b 12811 /* Get the type which refers to the base class (possibly this
c906108c 12812 class itself) which contains the vtable pointer for the current
0d564a31
DJ
12813 class from the DW_AT_containing_type attribute. This use of
12814 DW_AT_containing_type is a GNU extension. */
c906108c 12815
e142c38c 12816 if (dwarf2_attr (die, DW_AT_containing_type, cu) != NULL)
c906108c 12817 {
e7c27a73 12818 struct type *t = die_containing_type (die, cu);
c906108c
SS
12819
12820 TYPE_VPTR_BASETYPE (type) = t;
12821 if (type == t)
12822 {
c906108c
SS
12823 int i;
12824
12825 /* Our own class provides vtbl ptr. */
12826 for (i = TYPE_NFIELDS (t) - 1;
12827 i >= TYPE_N_BASECLASSES (t);
12828 --i)
12829 {
0d5cff50 12830 const char *fieldname = TYPE_FIELD_NAME (t, i);
c906108c 12831
1168df01 12832 if (is_vtable_name (fieldname, cu))
c906108c
SS
12833 {
12834 TYPE_VPTR_FIELDNO (type) = i;
12835 break;
12836 }
12837 }
12838
12839 /* Complain if virtual function table field not found. */
12840 if (i < TYPE_N_BASECLASSES (t))
4d3c2250 12841 complaint (&symfile_complaints,
3e43a32a
MS
12842 _("virtual function table pointer "
12843 "not found when defining class '%s'"),
4d3c2250
KB
12844 TYPE_TAG_NAME (type) ? TYPE_TAG_NAME (type) :
12845 "");
c906108c
SS
12846 }
12847 else
12848 {
12849 TYPE_VPTR_FIELDNO (type) = TYPE_VPTR_FIELDNO (t);
12850 }
12851 }
f6235d4c
EZ
12852 else if (cu->producer
12853 && strncmp (cu->producer,
12854 "IBM(R) XL C/C++ Advanced Edition", 32) == 0)
12855 {
12856 /* The IBM XLC compiler does not provide direct indication
12857 of the containing type, but the vtable pointer is
12858 always named __vfp. */
12859
12860 int i;
12861
12862 for (i = TYPE_NFIELDS (type) - 1;
12863 i >= TYPE_N_BASECLASSES (type);
12864 --i)
12865 {
12866 if (strcmp (TYPE_FIELD_NAME (type, i), "__vfp") == 0)
12867 {
12868 TYPE_VPTR_FIELDNO (type) = i;
12869 TYPE_VPTR_BASETYPE (type) = type;
12870 break;
12871 }
12872 }
12873 }
c906108c 12874 }
98751a41
JK
12875
12876 /* Copy fi.typedef_field_list linked list elements content into the
12877 allocated array TYPE_TYPEDEF_FIELD_ARRAY (type). */
12878 if (fi.typedef_field_list)
12879 {
12880 int i = fi.typedef_field_list_count;
12881
a0d7a4ff 12882 ALLOCATE_CPLUS_STRUCT_TYPE (type);
98751a41
JK
12883 TYPE_TYPEDEF_FIELD_ARRAY (type)
12884 = TYPE_ALLOC (type, sizeof (TYPE_TYPEDEF_FIELD (type, 0)) * i);
12885 TYPE_TYPEDEF_FIELD_COUNT (type) = i;
12886
12887 /* Reverse the list order to keep the debug info elements order. */
12888 while (--i >= 0)
12889 {
12890 struct typedef_field *dest, *src;
6e70227d 12891
98751a41
JK
12892 dest = &TYPE_TYPEDEF_FIELD (type, i);
12893 src = &fi.typedef_field_list->field;
12894 fi.typedef_field_list = fi.typedef_field_list->next;
12895 *dest = *src;
12896 }
12897 }
c767944b
DJ
12898
12899 do_cleanups (back_to);
eb2a6f42
TT
12900
12901 if (HAVE_CPLUS_STRUCT (type))
12902 TYPE_CPLUS_REALLY_JAVA (type) = cu->language == language_java;
c906108c 12903 }
63d06c5c 12904
bb5ed363 12905 quirk_gcc_member_function_pointer (type, objfile);
0b92b5bb 12906
90aeadfc
DC
12907 /* NOTE: carlton/2004-03-16: GCC 3.4 (or at least one of its
12908 snapshots) has been known to create a die giving a declaration
12909 for a class that has, as a child, a die giving a definition for a
12910 nested class. So we have to process our children even if the
12911 current die is a declaration. Normally, of course, a declaration
12912 won't have any children at all. */
134d01f1 12913
90aeadfc
DC
12914 while (child_die != NULL && child_die->tag)
12915 {
12916 if (child_die->tag == DW_TAG_member
12917 || child_die->tag == DW_TAG_variable
34eaf542
TT
12918 || child_die->tag == DW_TAG_inheritance
12919 || child_die->tag == DW_TAG_template_value_param
12920 || child_die->tag == DW_TAG_template_type_param)
134d01f1 12921 {
90aeadfc 12922 /* Do nothing. */
134d01f1 12923 }
90aeadfc
DC
12924 else
12925 process_die (child_die, cu);
134d01f1 12926
90aeadfc 12927 child_die = sibling_die (child_die);
134d01f1
DJ
12928 }
12929
fa4028e9
JB
12930 /* Do not consider external references. According to the DWARF standard,
12931 these DIEs are identified by the fact that they have no byte_size
12932 attribute, and a declaration attribute. */
12933 if (dwarf2_attr (die, DW_AT_byte_size, cu) != NULL
12934 || !die_is_declaration (die, cu))
c767944b 12935 new_symbol (die, type, cu);
134d01f1
DJ
12936}
12937
12938/* Given a DW_AT_enumeration_type die, set its type. We do not
12939 complete the type's fields yet, or create any symbols. */
c906108c 12940
f792889a 12941static struct type *
134d01f1 12942read_enumeration_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 12943{
e7c27a73 12944 struct objfile *objfile = cu->objfile;
c906108c 12945 struct type *type;
c906108c 12946 struct attribute *attr;
0114d602 12947 const char *name;
134d01f1 12948
348e048f
DE
12949 /* If the definition of this type lives in .debug_types, read that type.
12950 Don't follow DW_AT_specification though, that will take us back up
12951 the chain and we want to go down. */
45e58e77 12952 attr = dwarf2_attr_no_follow (die, DW_AT_signature);
348e048f
DE
12953 if (attr)
12954 {
ac9ec31b 12955 type = get_DW_AT_signature_type (die, attr, cu);
9dc481d3 12956
ac9ec31b 12957 /* The type's CU may not be the same as CU.
02142a6c 12958 Ensure TYPE is recorded with CU in die_type_hash. */
348e048f
DE
12959 return set_die_type (die, type, cu);
12960 }
12961
c906108c
SS
12962 type = alloc_type (objfile);
12963
12964 TYPE_CODE (type) = TYPE_CODE_ENUM;
94af9270 12965 name = dwarf2_full_name (NULL, die, cu);
39cbfefa 12966 if (name != NULL)
7d455152 12967 TYPE_TAG_NAME (type) = name;
c906108c 12968
e142c38c 12969 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
c906108c
SS
12970 if (attr)
12971 {
12972 TYPE_LENGTH (type) = DW_UNSND (attr);
12973 }
12974 else
12975 {
12976 TYPE_LENGTH (type) = 0;
12977 }
12978
137033e9
JB
12979 /* The enumeration DIE can be incomplete. In Ada, any type can be
12980 declared as private in the package spec, and then defined only
12981 inside the package body. Such types are known as Taft Amendment
12982 Types. When another package uses such a type, an incomplete DIE
12983 may be generated by the compiler. */
02eb380e 12984 if (die_is_declaration (die, cu))
876cecd0 12985 TYPE_STUB (type) = 1;
02eb380e 12986
f792889a 12987 return set_die_type (die, type, cu);
134d01f1
DJ
12988}
12989
12990/* Given a pointer to a die which begins an enumeration, process all
12991 the dies that define the members of the enumeration, and create the
12992 symbol for the enumeration type.
12993
12994 NOTE: We reverse the order of the element list. */
12995
12996static void
12997process_enumeration_scope (struct die_info *die, struct dwarf2_cu *cu)
12998{
f792889a 12999 struct type *this_type;
134d01f1 13000
f792889a
DJ
13001 this_type = get_die_type (die, cu);
13002 if (this_type == NULL)
13003 this_type = read_enumeration_type (die, cu);
9dc481d3 13004
639d11d3 13005 if (die->child != NULL)
c906108c 13006 {
9dc481d3
DE
13007 struct die_info *child_die;
13008 struct symbol *sym;
13009 struct field *fields = NULL;
13010 int num_fields = 0;
13011 int unsigned_enum = 1;
15d034d0 13012 const char *name;
cafec441
TT
13013 int flag_enum = 1;
13014 ULONGEST mask = 0;
9dc481d3 13015
639d11d3 13016 child_die = die->child;
c906108c
SS
13017 while (child_die && child_die->tag)
13018 {
13019 if (child_die->tag != DW_TAG_enumerator)
13020 {
e7c27a73 13021 process_die (child_die, cu);
c906108c
SS
13022 }
13023 else
13024 {
39cbfefa
DJ
13025 name = dwarf2_name (child_die, cu);
13026 if (name)
c906108c 13027 {
f792889a 13028 sym = new_symbol (child_die, this_type, cu);
c906108c 13029 if (SYMBOL_VALUE (sym) < 0)
cafec441
TT
13030 {
13031 unsigned_enum = 0;
13032 flag_enum = 0;
13033 }
13034 else if ((mask & SYMBOL_VALUE (sym)) != 0)
13035 flag_enum = 0;
13036 else
13037 mask |= SYMBOL_VALUE (sym);
c906108c
SS
13038
13039 if ((num_fields % DW_FIELD_ALLOC_CHUNK) == 0)
13040 {
13041 fields = (struct field *)
13042 xrealloc (fields,
13043 (num_fields + DW_FIELD_ALLOC_CHUNK)
c5aa993b 13044 * sizeof (struct field));
c906108c
SS
13045 }
13046
3567439c 13047 FIELD_NAME (fields[num_fields]) = SYMBOL_LINKAGE_NAME (sym);
c906108c 13048 FIELD_TYPE (fields[num_fields]) = NULL;
14e75d8e 13049 SET_FIELD_ENUMVAL (fields[num_fields], SYMBOL_VALUE (sym));
c906108c
SS
13050 FIELD_BITSIZE (fields[num_fields]) = 0;
13051
13052 num_fields++;
13053 }
13054 }
13055
13056 child_die = sibling_die (child_die);
13057 }
13058
13059 if (num_fields)
13060 {
f792889a
DJ
13061 TYPE_NFIELDS (this_type) = num_fields;
13062 TYPE_FIELDS (this_type) = (struct field *)
13063 TYPE_ALLOC (this_type, sizeof (struct field) * num_fields);
13064 memcpy (TYPE_FIELDS (this_type), fields,
c906108c 13065 sizeof (struct field) * num_fields);
b8c9b27d 13066 xfree (fields);
c906108c
SS
13067 }
13068 if (unsigned_enum)
876cecd0 13069 TYPE_UNSIGNED (this_type) = 1;
cafec441
TT
13070 if (flag_enum)
13071 TYPE_FLAG_ENUM (this_type) = 1;
c906108c 13072 }
134d01f1 13073
6c83ed52
TT
13074 /* If we are reading an enum from a .debug_types unit, and the enum
13075 is a declaration, and the enum is not the signatured type in the
13076 unit, then we do not want to add a symbol for it. Adding a
13077 symbol would in some cases obscure the true definition of the
13078 enum, giving users an incomplete type when the definition is
13079 actually available. Note that we do not want to do this for all
13080 enums which are just declarations, because C++0x allows forward
13081 enum declarations. */
3019eac3 13082 if (cu->per_cu->is_debug_types
6c83ed52
TT
13083 && die_is_declaration (die, cu))
13084 {
52dc124a 13085 struct signatured_type *sig_type;
6c83ed52 13086
c0f78cd4 13087 sig_type = (struct signatured_type *) cu->per_cu;
3019eac3
DE
13088 gdb_assert (sig_type->type_offset_in_section.sect_off != 0);
13089 if (sig_type->type_offset_in_section.sect_off != die->offset.sect_off)
6c83ed52
TT
13090 return;
13091 }
13092
f792889a 13093 new_symbol (die, this_type, cu);
c906108c
SS
13094}
13095
13096/* Extract all information from a DW_TAG_array_type DIE and put it in
13097 the DIE's type field. For now, this only handles one dimensional
13098 arrays. */
13099
f792889a 13100static struct type *
e7c27a73 13101read_array_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 13102{
e7c27a73 13103 struct objfile *objfile = cu->objfile;
c906108c 13104 struct die_info *child_die;
7e314c57 13105 struct type *type;
c906108c
SS
13106 struct type *element_type, *range_type, *index_type;
13107 struct type **range_types = NULL;
13108 struct attribute *attr;
13109 int ndim = 0;
13110 struct cleanup *back_to;
15d034d0 13111 const char *name;
c906108c 13112
e7c27a73 13113 element_type = die_type (die, cu);
c906108c 13114
7e314c57
JK
13115 /* The die_type call above may have already set the type for this DIE. */
13116 type = get_die_type (die, cu);
13117 if (type)
13118 return type;
13119
c906108c
SS
13120 /* Irix 6.2 native cc creates array types without children for
13121 arrays with unspecified length. */
639d11d3 13122 if (die->child == NULL)
c906108c 13123 {
46bf5051 13124 index_type = objfile_type (objfile)->builtin_int;
c906108c 13125 range_type = create_range_type (NULL, index_type, 0, -1);
f792889a
DJ
13126 type = create_array_type (NULL, element_type, range_type);
13127 return set_die_type (die, type, cu);
c906108c
SS
13128 }
13129
13130 back_to = make_cleanup (null_cleanup, NULL);
639d11d3 13131 child_die = die->child;
c906108c
SS
13132 while (child_die && child_die->tag)
13133 {
13134 if (child_die->tag == DW_TAG_subrange_type)
13135 {
f792889a 13136 struct type *child_type = read_type_die (child_die, cu);
9a619af0 13137
f792889a 13138 if (child_type != NULL)
a02abb62 13139 {
0963b4bd
MS
13140 /* The range type was succesfully read. Save it for the
13141 array type creation. */
a02abb62
JB
13142 if ((ndim % DW_FIELD_ALLOC_CHUNK) == 0)
13143 {
13144 range_types = (struct type **)
13145 xrealloc (range_types, (ndim + DW_FIELD_ALLOC_CHUNK)
13146 * sizeof (struct type *));
13147 if (ndim == 0)
13148 make_cleanup (free_current_contents, &range_types);
13149 }
f792889a 13150 range_types[ndim++] = child_type;
a02abb62 13151 }
c906108c
SS
13152 }
13153 child_die = sibling_die (child_die);
13154 }
13155
13156 /* Dwarf2 dimensions are output from left to right, create the
13157 necessary array types in backwards order. */
7ca2d3a3 13158
c906108c 13159 type = element_type;
7ca2d3a3
DL
13160
13161 if (read_array_order (die, cu) == DW_ORD_col_major)
13162 {
13163 int i = 0;
9a619af0 13164
7ca2d3a3
DL
13165 while (i < ndim)
13166 type = create_array_type (NULL, type, range_types[i++]);
13167 }
13168 else
13169 {
13170 while (ndim-- > 0)
13171 type = create_array_type (NULL, type, range_types[ndim]);
13172 }
c906108c 13173
f5f8a009
EZ
13174 /* Understand Dwarf2 support for vector types (like they occur on
13175 the PowerPC w/ AltiVec). Gcc just adds another attribute to the
13176 array type. This is not part of the Dwarf2/3 standard yet, but a
13177 custom vendor extension. The main difference between a regular
13178 array and the vector variant is that vectors are passed by value
13179 to functions. */
e142c38c 13180 attr = dwarf2_attr (die, DW_AT_GNU_vector, cu);
f5f8a009 13181 if (attr)
ea37ba09 13182 make_vector_type (type);
f5f8a009 13183
dbc98a8b
KW
13184 /* The DIE may have DW_AT_byte_size set. For example an OpenCL
13185 implementation may choose to implement triple vectors using this
13186 attribute. */
13187 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
13188 if (attr)
13189 {
13190 if (DW_UNSND (attr) >= TYPE_LENGTH (type))
13191 TYPE_LENGTH (type) = DW_UNSND (attr);
13192 else
3e43a32a
MS
13193 complaint (&symfile_complaints,
13194 _("DW_AT_byte_size for array type smaller "
13195 "than the total size of elements"));
dbc98a8b
KW
13196 }
13197
39cbfefa
DJ
13198 name = dwarf2_name (die, cu);
13199 if (name)
13200 TYPE_NAME (type) = name;
6e70227d 13201
0963b4bd 13202 /* Install the type in the die. */
7e314c57
JK
13203 set_die_type (die, type, cu);
13204
13205 /* set_die_type should be already done. */
b4ba55a1
JB
13206 set_descriptive_type (type, die, cu);
13207
c906108c
SS
13208 do_cleanups (back_to);
13209
7e314c57 13210 return type;
c906108c
SS
13211}
13212
7ca2d3a3 13213static enum dwarf_array_dim_ordering
6e70227d 13214read_array_order (struct die_info *die, struct dwarf2_cu *cu)
7ca2d3a3
DL
13215{
13216 struct attribute *attr;
13217
13218 attr = dwarf2_attr (die, DW_AT_ordering, cu);
13219
13220 if (attr) return DW_SND (attr);
13221
0963b4bd
MS
13222 /* GNU F77 is a special case, as at 08/2004 array type info is the
13223 opposite order to the dwarf2 specification, but data is still
13224 laid out as per normal fortran.
7ca2d3a3 13225
0963b4bd
MS
13226 FIXME: dsl/2004-8-20: If G77 is ever fixed, this will also need
13227 version checking. */
7ca2d3a3 13228
905e0470
PM
13229 if (cu->language == language_fortran
13230 && cu->producer && strstr (cu->producer, "GNU F77"))
7ca2d3a3
DL
13231 {
13232 return DW_ORD_row_major;
13233 }
13234
6e70227d 13235 switch (cu->language_defn->la_array_ordering)
7ca2d3a3
DL
13236 {
13237 case array_column_major:
13238 return DW_ORD_col_major;
13239 case array_row_major:
13240 default:
13241 return DW_ORD_row_major;
13242 };
13243}
13244
72019c9c 13245/* Extract all information from a DW_TAG_set_type DIE and put it in
0963b4bd 13246 the DIE's type field. */
72019c9c 13247
f792889a 13248static struct type *
72019c9c
GM
13249read_set_type (struct die_info *die, struct dwarf2_cu *cu)
13250{
7e314c57
JK
13251 struct type *domain_type, *set_type;
13252 struct attribute *attr;
f792889a 13253
7e314c57
JK
13254 domain_type = die_type (die, cu);
13255
13256 /* The die_type call above may have already set the type for this DIE. */
13257 set_type = get_die_type (die, cu);
13258 if (set_type)
13259 return set_type;
13260
13261 set_type = create_set_type (NULL, domain_type);
13262
13263 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
d09039dd
PM
13264 if (attr)
13265 TYPE_LENGTH (set_type) = DW_UNSND (attr);
7e314c57 13266
f792889a 13267 return set_die_type (die, set_type, cu);
72019c9c 13268}
7ca2d3a3 13269
0971de02
TT
13270/* A helper for read_common_block that creates a locexpr baton.
13271 SYM is the symbol which we are marking as computed.
13272 COMMON_DIE is the DIE for the common block.
13273 COMMON_LOC is the location expression attribute for the common
13274 block itself.
13275 MEMBER_LOC is the location expression attribute for the particular
13276 member of the common block that we are processing.
13277 CU is the CU from which the above come. */
13278
13279static void
13280mark_common_block_symbol_computed (struct symbol *sym,
13281 struct die_info *common_die,
13282 struct attribute *common_loc,
13283 struct attribute *member_loc,
13284 struct dwarf2_cu *cu)
13285{
13286 struct objfile *objfile = dwarf2_per_objfile->objfile;
13287 struct dwarf2_locexpr_baton *baton;
13288 gdb_byte *ptr;
13289 unsigned int cu_off;
13290 enum bfd_endian byte_order = gdbarch_byte_order (get_objfile_arch (objfile));
13291 LONGEST offset = 0;
13292
13293 gdb_assert (common_loc && member_loc);
13294 gdb_assert (attr_form_is_block (common_loc));
13295 gdb_assert (attr_form_is_block (member_loc)
13296 || attr_form_is_constant (member_loc));
13297
13298 baton = obstack_alloc (&objfile->objfile_obstack,
13299 sizeof (struct dwarf2_locexpr_baton));
13300 baton->per_cu = cu->per_cu;
13301 gdb_assert (baton->per_cu);
13302
13303 baton->size = 5 /* DW_OP_call4 */ + 1 /* DW_OP_plus */;
13304
13305 if (attr_form_is_constant (member_loc))
13306 {
13307 offset = dwarf2_get_attr_constant_value (member_loc, 0);
13308 baton->size += 1 /* DW_OP_addr */ + cu->header.addr_size;
13309 }
13310 else
13311 baton->size += DW_BLOCK (member_loc)->size;
13312
13313 ptr = obstack_alloc (&objfile->objfile_obstack, baton->size);
13314 baton->data = ptr;
13315
13316 *ptr++ = DW_OP_call4;
13317 cu_off = common_die->offset.sect_off - cu->per_cu->offset.sect_off;
13318 store_unsigned_integer (ptr, 4, byte_order, cu_off);
13319 ptr += 4;
13320
13321 if (attr_form_is_constant (member_loc))
13322 {
13323 *ptr++ = DW_OP_addr;
13324 store_unsigned_integer (ptr, cu->header.addr_size, byte_order, offset);
13325 ptr += cu->header.addr_size;
13326 }
13327 else
13328 {
13329 /* We have to copy the data here, because DW_OP_call4 will only
13330 use a DW_AT_location attribute. */
13331 memcpy (ptr, DW_BLOCK (member_loc)->data, DW_BLOCK (member_loc)->size);
13332 ptr += DW_BLOCK (member_loc)->size;
13333 }
13334
13335 *ptr++ = DW_OP_plus;
13336 gdb_assert (ptr - baton->data == baton->size);
13337
0971de02 13338 SYMBOL_LOCATION_BATON (sym) = baton;
f1e6e072 13339 SYMBOL_ACLASS_INDEX (sym) = dwarf2_locexpr_index;
0971de02
TT
13340}
13341
4357ac6c
TT
13342/* Create appropriate locally-scoped variables for all the
13343 DW_TAG_common_block entries. Also create a struct common_block
13344 listing all such variables for `info common'. COMMON_BLOCK_DOMAIN
13345 is used to sepate the common blocks name namespace from regular
13346 variable names. */
c906108c
SS
13347
13348static void
e7c27a73 13349read_common_block (struct die_info *die, struct dwarf2_cu *cu)
c906108c 13350{
0971de02
TT
13351 struct attribute *attr;
13352
13353 attr = dwarf2_attr (die, DW_AT_location, cu);
13354 if (attr)
13355 {
13356 /* Support the .debug_loc offsets. */
13357 if (attr_form_is_block (attr))
13358 {
13359 /* Ok. */
13360 }
13361 else if (attr_form_is_section_offset (attr))
13362 {
13363 dwarf2_complex_location_expr_complaint ();
13364 attr = NULL;
13365 }
13366 else
13367 {
13368 dwarf2_invalid_attrib_class_complaint ("DW_AT_location",
13369 "common block member");
13370 attr = NULL;
13371 }
13372 }
13373
639d11d3 13374 if (die->child != NULL)
c906108c 13375 {
4357ac6c
TT
13376 struct objfile *objfile = cu->objfile;
13377 struct die_info *child_die;
13378 size_t n_entries = 0, size;
13379 struct common_block *common_block;
13380 struct symbol *sym;
74ac6d43 13381
4357ac6c
TT
13382 for (child_die = die->child;
13383 child_die && child_die->tag;
13384 child_die = sibling_die (child_die))
13385 ++n_entries;
13386
13387 size = (sizeof (struct common_block)
13388 + (n_entries - 1) * sizeof (struct symbol *));
13389 common_block = obstack_alloc (&objfile->objfile_obstack, size);
13390 memset (common_block->contents, 0, n_entries * sizeof (struct symbol *));
13391 common_block->n_entries = 0;
13392
13393 for (child_die = die->child;
13394 child_die && child_die->tag;
13395 child_die = sibling_die (child_die))
13396 {
13397 /* Create the symbol in the DW_TAG_common_block block in the current
13398 symbol scope. */
e7c27a73 13399 sym = new_symbol (child_die, NULL, cu);
0971de02
TT
13400 if (sym != NULL)
13401 {
13402 struct attribute *member_loc;
13403
13404 common_block->contents[common_block->n_entries++] = sym;
13405
13406 member_loc = dwarf2_attr (child_die, DW_AT_data_member_location,
13407 cu);
13408 if (member_loc)
13409 {
13410 /* GDB has handled this for a long time, but it is
13411 not specified by DWARF. It seems to have been
13412 emitted by gfortran at least as recently as:
13413 http://gcc.gnu.org/bugzilla/show_bug.cgi?id=23057. */
13414 complaint (&symfile_complaints,
13415 _("Variable in common block has "
13416 "DW_AT_data_member_location "
13417 "- DIE at 0x%x [in module %s]"),
4262abfb
JK
13418 child_die->offset.sect_off,
13419 objfile_name (cu->objfile));
0971de02
TT
13420
13421 if (attr_form_is_section_offset (member_loc))
13422 dwarf2_complex_location_expr_complaint ();
13423 else if (attr_form_is_constant (member_loc)
13424 || attr_form_is_block (member_loc))
13425 {
13426 if (attr)
13427 mark_common_block_symbol_computed (sym, die, attr,
13428 member_loc, cu);
13429 }
13430 else
13431 dwarf2_complex_location_expr_complaint ();
13432 }
13433 }
c906108c 13434 }
4357ac6c
TT
13435
13436 sym = new_symbol (die, objfile_type (objfile)->builtin_void, cu);
13437 SYMBOL_VALUE_COMMON_BLOCK (sym) = common_block;
c906108c
SS
13438 }
13439}
13440
0114d602 13441/* Create a type for a C++ namespace. */
d9fa45fe 13442
0114d602
DJ
13443static struct type *
13444read_namespace_type (struct die_info *die, struct dwarf2_cu *cu)
d9fa45fe 13445{
e7c27a73 13446 struct objfile *objfile = cu->objfile;
0114d602 13447 const char *previous_prefix, *name;
9219021c 13448 int is_anonymous;
0114d602
DJ
13449 struct type *type;
13450
13451 /* For extensions, reuse the type of the original namespace. */
13452 if (dwarf2_attr (die, DW_AT_extension, cu) != NULL)
13453 {
13454 struct die_info *ext_die;
13455 struct dwarf2_cu *ext_cu = cu;
9a619af0 13456
0114d602
DJ
13457 ext_die = dwarf2_extension (die, &ext_cu);
13458 type = read_type_die (ext_die, ext_cu);
9dc481d3
DE
13459
13460 /* EXT_CU may not be the same as CU.
02142a6c 13461 Ensure TYPE is recorded with CU in die_type_hash. */
0114d602
DJ
13462 return set_die_type (die, type, cu);
13463 }
9219021c 13464
e142c38c 13465 name = namespace_name (die, &is_anonymous, cu);
9219021c
DC
13466
13467 /* Now build the name of the current namespace. */
13468
0114d602
DJ
13469 previous_prefix = determine_prefix (die, cu);
13470 if (previous_prefix[0] != '\0')
13471 name = typename_concat (&objfile->objfile_obstack,
f55ee35c 13472 previous_prefix, name, 0, cu);
0114d602
DJ
13473
13474 /* Create the type. */
13475 type = init_type (TYPE_CODE_NAMESPACE, 0, 0, NULL,
13476 objfile);
abee88f2 13477 TYPE_NAME (type) = name;
0114d602
DJ
13478 TYPE_TAG_NAME (type) = TYPE_NAME (type);
13479
60531b24 13480 return set_die_type (die, type, cu);
0114d602
DJ
13481}
13482
13483/* Read a C++ namespace. */
13484
13485static void
13486read_namespace (struct die_info *die, struct dwarf2_cu *cu)
13487{
13488 struct objfile *objfile = cu->objfile;
0114d602 13489 int is_anonymous;
9219021c 13490
5c4e30ca
DC
13491 /* Add a symbol associated to this if we haven't seen the namespace
13492 before. Also, add a using directive if it's an anonymous
13493 namespace. */
9219021c 13494
f2f0e013 13495 if (dwarf2_attr (die, DW_AT_extension, cu) == NULL)
5c4e30ca
DC
13496 {
13497 struct type *type;
13498
0114d602 13499 type = read_type_die (die, cu);
e7c27a73 13500 new_symbol (die, type, cu);
5c4e30ca 13501
e8e80198 13502 namespace_name (die, &is_anonymous, cu);
5c4e30ca 13503 if (is_anonymous)
0114d602
DJ
13504 {
13505 const char *previous_prefix = determine_prefix (die, cu);
9a619af0 13506
c0cc3a76 13507 cp_add_using_directive (previous_prefix, TYPE_NAME (type), NULL,
12aaed36 13508 NULL, NULL, 0, &objfile->objfile_obstack);
0114d602 13509 }
5c4e30ca 13510 }
9219021c 13511
639d11d3 13512 if (die->child != NULL)
d9fa45fe 13513 {
639d11d3 13514 struct die_info *child_die = die->child;
6e70227d 13515
d9fa45fe
DC
13516 while (child_die && child_die->tag)
13517 {
e7c27a73 13518 process_die (child_die, cu);
d9fa45fe
DC
13519 child_die = sibling_die (child_die);
13520 }
13521 }
38d518c9
EZ
13522}
13523
f55ee35c
JK
13524/* Read a Fortran module as type. This DIE can be only a declaration used for
13525 imported module. Still we need that type as local Fortran "use ... only"
13526 declaration imports depend on the created type in determine_prefix. */
13527
13528static struct type *
13529read_module_type (struct die_info *die, struct dwarf2_cu *cu)
13530{
13531 struct objfile *objfile = cu->objfile;
15d034d0 13532 const char *module_name;
f55ee35c
JK
13533 struct type *type;
13534
13535 module_name = dwarf2_name (die, cu);
13536 if (!module_name)
3e43a32a
MS
13537 complaint (&symfile_complaints,
13538 _("DW_TAG_module has no name, offset 0x%x"),
b64f50a1 13539 die->offset.sect_off);
f55ee35c
JK
13540 type = init_type (TYPE_CODE_MODULE, 0, 0, module_name, objfile);
13541
13542 /* determine_prefix uses TYPE_TAG_NAME. */
13543 TYPE_TAG_NAME (type) = TYPE_NAME (type);
13544
13545 return set_die_type (die, type, cu);
13546}
13547
5d7cb8df
JK
13548/* Read a Fortran module. */
13549
13550static void
13551read_module (struct die_info *die, struct dwarf2_cu *cu)
13552{
13553 struct die_info *child_die = die->child;
13554
5d7cb8df
JK
13555 while (child_die && child_die->tag)
13556 {
13557 process_die (child_die, cu);
13558 child_die = sibling_die (child_die);
13559 }
13560}
13561
38d518c9
EZ
13562/* Return the name of the namespace represented by DIE. Set
13563 *IS_ANONYMOUS to tell whether or not the namespace is an anonymous
13564 namespace. */
13565
13566static const char *
e142c38c 13567namespace_name (struct die_info *die, int *is_anonymous, struct dwarf2_cu *cu)
38d518c9
EZ
13568{
13569 struct die_info *current_die;
13570 const char *name = NULL;
13571
13572 /* Loop through the extensions until we find a name. */
13573
13574 for (current_die = die;
13575 current_die != NULL;
f2f0e013 13576 current_die = dwarf2_extension (die, &cu))
38d518c9 13577 {
e142c38c 13578 name = dwarf2_name (current_die, cu);
38d518c9
EZ
13579 if (name != NULL)
13580 break;
13581 }
13582
13583 /* Is it an anonymous namespace? */
13584
13585 *is_anonymous = (name == NULL);
13586 if (*is_anonymous)
2b1dbab0 13587 name = CP_ANONYMOUS_NAMESPACE_STR;
38d518c9
EZ
13588
13589 return name;
d9fa45fe
DC
13590}
13591
c906108c
SS
13592/* Extract all information from a DW_TAG_pointer_type DIE and add to
13593 the user defined type vector. */
13594
f792889a 13595static struct type *
e7c27a73 13596read_tag_pointer_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 13597{
5e2b427d 13598 struct gdbarch *gdbarch = get_objfile_arch (cu->objfile);
e7c27a73 13599 struct comp_unit_head *cu_header = &cu->header;
c906108c 13600 struct type *type;
8b2dbe47
KB
13601 struct attribute *attr_byte_size;
13602 struct attribute *attr_address_class;
13603 int byte_size, addr_class;
7e314c57
JK
13604 struct type *target_type;
13605
13606 target_type = die_type (die, cu);
c906108c 13607
7e314c57
JK
13608 /* The die_type call above may have already set the type for this DIE. */
13609 type = get_die_type (die, cu);
13610 if (type)
13611 return type;
13612
13613 type = lookup_pointer_type (target_type);
8b2dbe47 13614
e142c38c 13615 attr_byte_size = dwarf2_attr (die, DW_AT_byte_size, cu);
8b2dbe47
KB
13616 if (attr_byte_size)
13617 byte_size = DW_UNSND (attr_byte_size);
c906108c 13618 else
8b2dbe47
KB
13619 byte_size = cu_header->addr_size;
13620
e142c38c 13621 attr_address_class = dwarf2_attr (die, DW_AT_address_class, cu);
8b2dbe47
KB
13622 if (attr_address_class)
13623 addr_class = DW_UNSND (attr_address_class);
13624 else
13625 addr_class = DW_ADDR_none;
13626
13627 /* If the pointer size or address class is different than the
13628 default, create a type variant marked as such and set the
13629 length accordingly. */
13630 if (TYPE_LENGTH (type) != byte_size || addr_class != DW_ADDR_none)
c906108c 13631 {
5e2b427d 13632 if (gdbarch_address_class_type_flags_p (gdbarch))
8b2dbe47
KB
13633 {
13634 int type_flags;
13635
849957d9 13636 type_flags = gdbarch_address_class_type_flags
5e2b427d 13637 (gdbarch, byte_size, addr_class);
876cecd0
TT
13638 gdb_assert ((type_flags & ~TYPE_INSTANCE_FLAG_ADDRESS_CLASS_ALL)
13639 == 0);
8b2dbe47
KB
13640 type = make_type_with_address_space (type, type_flags);
13641 }
13642 else if (TYPE_LENGTH (type) != byte_size)
13643 {
3e43a32a
MS
13644 complaint (&symfile_complaints,
13645 _("invalid pointer size %d"), byte_size);
8b2dbe47 13646 }
6e70227d 13647 else
9a619af0
MS
13648 {
13649 /* Should we also complain about unhandled address classes? */
13650 }
c906108c 13651 }
8b2dbe47
KB
13652
13653 TYPE_LENGTH (type) = byte_size;
f792889a 13654 return set_die_type (die, type, cu);
c906108c
SS
13655}
13656
13657/* Extract all information from a DW_TAG_ptr_to_member_type DIE and add to
13658 the user defined type vector. */
13659
f792889a 13660static struct type *
e7c27a73 13661read_tag_ptr_to_member_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c
SS
13662{
13663 struct type *type;
13664 struct type *to_type;
13665 struct type *domain;
13666
e7c27a73
DJ
13667 to_type = die_type (die, cu);
13668 domain = die_containing_type (die, cu);
0d5de010 13669
7e314c57
JK
13670 /* The calls above may have already set the type for this DIE. */
13671 type = get_die_type (die, cu);
13672 if (type)
13673 return type;
13674
0d5de010
DJ
13675 if (TYPE_CODE (check_typedef (to_type)) == TYPE_CODE_METHOD)
13676 type = lookup_methodptr_type (to_type);
7078baeb
TT
13677 else if (TYPE_CODE (check_typedef (to_type)) == TYPE_CODE_FUNC)
13678 {
13679 struct type *new_type = alloc_type (cu->objfile);
13680
13681 smash_to_method_type (new_type, domain, TYPE_TARGET_TYPE (to_type),
13682 TYPE_FIELDS (to_type), TYPE_NFIELDS (to_type),
13683 TYPE_VARARGS (to_type));
13684 type = lookup_methodptr_type (new_type);
13685 }
0d5de010
DJ
13686 else
13687 type = lookup_memberptr_type (to_type, domain);
c906108c 13688
f792889a 13689 return set_die_type (die, type, cu);
c906108c
SS
13690}
13691
13692/* Extract all information from a DW_TAG_reference_type DIE and add to
13693 the user defined type vector. */
13694
f792889a 13695static struct type *
e7c27a73 13696read_tag_reference_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 13697{
e7c27a73 13698 struct comp_unit_head *cu_header = &cu->header;
7e314c57 13699 struct type *type, *target_type;
c906108c
SS
13700 struct attribute *attr;
13701
7e314c57
JK
13702 target_type = die_type (die, cu);
13703
13704 /* The die_type call above may have already set the type for this DIE. */
13705 type = get_die_type (die, cu);
13706 if (type)
13707 return type;
13708
13709 type = lookup_reference_type (target_type);
e142c38c 13710 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
c906108c
SS
13711 if (attr)
13712 {
13713 TYPE_LENGTH (type) = DW_UNSND (attr);
13714 }
13715 else
13716 {
107d2387 13717 TYPE_LENGTH (type) = cu_header->addr_size;
c906108c 13718 }
f792889a 13719 return set_die_type (die, type, cu);
c906108c
SS
13720}
13721
f792889a 13722static struct type *
e7c27a73 13723read_tag_const_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 13724{
f792889a 13725 struct type *base_type, *cv_type;
c906108c 13726
e7c27a73 13727 base_type = die_type (die, cu);
7e314c57
JK
13728
13729 /* The die_type call above may have already set the type for this DIE. */
13730 cv_type = get_die_type (die, cu);
13731 if (cv_type)
13732 return cv_type;
13733
2f608a3a
KW
13734 /* In case the const qualifier is applied to an array type, the element type
13735 is so qualified, not the array type (section 6.7.3 of C99). */
13736 if (TYPE_CODE (base_type) == TYPE_CODE_ARRAY)
13737 {
13738 struct type *el_type, *inner_array;
13739
13740 base_type = copy_type (base_type);
13741 inner_array = base_type;
13742
13743 while (TYPE_CODE (TYPE_TARGET_TYPE (inner_array)) == TYPE_CODE_ARRAY)
13744 {
13745 TYPE_TARGET_TYPE (inner_array) =
13746 copy_type (TYPE_TARGET_TYPE (inner_array));
13747 inner_array = TYPE_TARGET_TYPE (inner_array);
13748 }
13749
13750 el_type = TYPE_TARGET_TYPE (inner_array);
13751 TYPE_TARGET_TYPE (inner_array) =
13752 make_cv_type (1, TYPE_VOLATILE (el_type), el_type, NULL);
13753
13754 return set_die_type (die, base_type, cu);
13755 }
13756
f792889a
DJ
13757 cv_type = make_cv_type (1, TYPE_VOLATILE (base_type), base_type, 0);
13758 return set_die_type (die, cv_type, cu);
c906108c
SS
13759}
13760
f792889a 13761static struct type *
e7c27a73 13762read_tag_volatile_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 13763{
f792889a 13764 struct type *base_type, *cv_type;
c906108c 13765
e7c27a73 13766 base_type = die_type (die, cu);
7e314c57
JK
13767
13768 /* The die_type call above may have already set the type for this DIE. */
13769 cv_type = get_die_type (die, cu);
13770 if (cv_type)
13771 return cv_type;
13772
f792889a
DJ
13773 cv_type = make_cv_type (TYPE_CONST (base_type), 1, base_type, 0);
13774 return set_die_type (die, cv_type, cu);
c906108c
SS
13775}
13776
06d66ee9
TT
13777/* Handle DW_TAG_restrict_type. */
13778
13779static struct type *
13780read_tag_restrict_type (struct die_info *die, struct dwarf2_cu *cu)
13781{
13782 struct type *base_type, *cv_type;
13783
13784 base_type = die_type (die, cu);
13785
13786 /* The die_type call above may have already set the type for this DIE. */
13787 cv_type = get_die_type (die, cu);
13788 if (cv_type)
13789 return cv_type;
13790
13791 cv_type = make_restrict_type (base_type);
13792 return set_die_type (die, cv_type, cu);
13793}
13794
c906108c
SS
13795/* Extract all information from a DW_TAG_string_type DIE and add to
13796 the user defined type vector. It isn't really a user defined type,
13797 but it behaves like one, with other DIE's using an AT_user_def_type
13798 attribute to reference it. */
13799
f792889a 13800static struct type *
e7c27a73 13801read_tag_string_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 13802{
e7c27a73 13803 struct objfile *objfile = cu->objfile;
3b7538c0 13804 struct gdbarch *gdbarch = get_objfile_arch (objfile);
c906108c
SS
13805 struct type *type, *range_type, *index_type, *char_type;
13806 struct attribute *attr;
13807 unsigned int length;
13808
e142c38c 13809 attr = dwarf2_attr (die, DW_AT_string_length, cu);
c906108c
SS
13810 if (attr)
13811 {
13812 length = DW_UNSND (attr);
13813 }
13814 else
13815 {
0963b4bd 13816 /* Check for the DW_AT_byte_size attribute. */
e142c38c 13817 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
b21b22e0
PS
13818 if (attr)
13819 {
13820 length = DW_UNSND (attr);
13821 }
13822 else
13823 {
13824 length = 1;
13825 }
c906108c 13826 }
6ccb9162 13827
46bf5051 13828 index_type = objfile_type (objfile)->builtin_int;
c906108c 13829 range_type = create_range_type (NULL, index_type, 1, length);
3b7538c0
UW
13830 char_type = language_string_char_type (cu->language_defn, gdbarch);
13831 type = create_string_type (NULL, char_type, range_type);
6ccb9162 13832
f792889a 13833 return set_die_type (die, type, cu);
c906108c
SS
13834}
13835
4d804846
JB
13836/* Assuming that DIE corresponds to a function, returns nonzero
13837 if the function is prototyped. */
13838
13839static int
13840prototyped_function_p (struct die_info *die, struct dwarf2_cu *cu)
13841{
13842 struct attribute *attr;
13843
13844 attr = dwarf2_attr (die, DW_AT_prototyped, cu);
13845 if (attr && (DW_UNSND (attr) != 0))
13846 return 1;
13847
13848 /* The DWARF standard implies that the DW_AT_prototyped attribute
13849 is only meaninful for C, but the concept also extends to other
13850 languages that allow unprototyped functions (Eg: Objective C).
13851 For all other languages, assume that functions are always
13852 prototyped. */
13853 if (cu->language != language_c
13854 && cu->language != language_objc
13855 && cu->language != language_opencl)
13856 return 1;
13857
13858 /* RealView does not emit DW_AT_prototyped. We can not distinguish
13859 prototyped and unprototyped functions; default to prototyped,
13860 since that is more common in modern code (and RealView warns
13861 about unprototyped functions). */
13862 if (producer_is_realview (cu->producer))
13863 return 1;
13864
13865 return 0;
13866}
13867
c906108c
SS
13868/* Handle DIES due to C code like:
13869
13870 struct foo
c5aa993b
JM
13871 {
13872 int (*funcp)(int a, long l);
13873 int b;
13874 };
c906108c 13875
0963b4bd 13876 ('funcp' generates a DW_TAG_subroutine_type DIE). */
c906108c 13877
f792889a 13878static struct type *
e7c27a73 13879read_subroutine_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 13880{
bb5ed363 13881 struct objfile *objfile = cu->objfile;
0963b4bd
MS
13882 struct type *type; /* Type that this function returns. */
13883 struct type *ftype; /* Function that returns above type. */
c906108c
SS
13884 struct attribute *attr;
13885
e7c27a73 13886 type = die_type (die, cu);
7e314c57
JK
13887
13888 /* The die_type call above may have already set the type for this DIE. */
13889 ftype = get_die_type (die, cu);
13890 if (ftype)
13891 return ftype;
13892
0c8b41f1 13893 ftype = lookup_function_type (type);
c906108c 13894
4d804846 13895 if (prototyped_function_p (die, cu))
a6c727b2 13896 TYPE_PROTOTYPED (ftype) = 1;
c906108c 13897
c055b101
CV
13898 /* Store the calling convention in the type if it's available in
13899 the subroutine die. Otherwise set the calling convention to
13900 the default value DW_CC_normal. */
13901 attr = dwarf2_attr (die, DW_AT_calling_convention, cu);
54fcddd0
UW
13902 if (attr)
13903 TYPE_CALLING_CONVENTION (ftype) = DW_UNSND (attr);
13904 else if (cu->producer && strstr (cu->producer, "IBM XL C for OpenCL"))
13905 TYPE_CALLING_CONVENTION (ftype) = DW_CC_GDB_IBM_OpenCL;
13906 else
13907 TYPE_CALLING_CONVENTION (ftype) = DW_CC_normal;
76c10ea2
GM
13908
13909 /* We need to add the subroutine type to the die immediately so
13910 we don't infinitely recurse when dealing with parameters
0963b4bd 13911 declared as the same subroutine type. */
76c10ea2 13912 set_die_type (die, ftype, cu);
6e70227d 13913
639d11d3 13914 if (die->child != NULL)
c906108c 13915 {
bb5ed363 13916 struct type *void_type = objfile_type (objfile)->builtin_void;
c906108c 13917 struct die_info *child_die;
8072405b 13918 int nparams, iparams;
c906108c
SS
13919
13920 /* Count the number of parameters.
13921 FIXME: GDB currently ignores vararg functions, but knows about
13922 vararg member functions. */
8072405b 13923 nparams = 0;
639d11d3 13924 child_die = die->child;
c906108c
SS
13925 while (child_die && child_die->tag)
13926 {
13927 if (child_die->tag == DW_TAG_formal_parameter)
13928 nparams++;
13929 else if (child_die->tag == DW_TAG_unspecified_parameters)
876cecd0 13930 TYPE_VARARGS (ftype) = 1;
c906108c
SS
13931 child_die = sibling_die (child_die);
13932 }
13933
13934 /* Allocate storage for parameters and fill them in. */
13935 TYPE_NFIELDS (ftype) = nparams;
13936 TYPE_FIELDS (ftype) = (struct field *)
ae5a43e0 13937 TYPE_ZALLOC (ftype, nparams * sizeof (struct field));
c906108c 13938
8072405b
JK
13939 /* TYPE_FIELD_TYPE must never be NULL. Pre-fill the array to ensure it
13940 even if we error out during the parameters reading below. */
13941 for (iparams = 0; iparams < nparams; iparams++)
13942 TYPE_FIELD_TYPE (ftype, iparams) = void_type;
13943
13944 iparams = 0;
639d11d3 13945 child_die = die->child;
c906108c
SS
13946 while (child_die && child_die->tag)
13947 {
13948 if (child_die->tag == DW_TAG_formal_parameter)
13949 {
3ce3b1ba
PA
13950 struct type *arg_type;
13951
13952 /* DWARF version 2 has no clean way to discern C++
13953 static and non-static member functions. G++ helps
13954 GDB by marking the first parameter for non-static
13955 member functions (which is the this pointer) as
13956 artificial. We pass this information to
13957 dwarf2_add_member_fn via TYPE_FIELD_ARTIFICIAL.
13958
13959 DWARF version 3 added DW_AT_object_pointer, which GCC
13960 4.5 does not yet generate. */
e142c38c 13961 attr = dwarf2_attr (child_die, DW_AT_artificial, cu);
c906108c
SS
13962 if (attr)
13963 TYPE_FIELD_ARTIFICIAL (ftype, iparams) = DW_UNSND (attr);
13964 else
418835cc
KS
13965 {
13966 TYPE_FIELD_ARTIFICIAL (ftype, iparams) = 0;
13967
13968 /* GCC/43521: In java, the formal parameter
13969 "this" is sometimes not marked with DW_AT_artificial. */
13970 if (cu->language == language_java)
13971 {
13972 const char *name = dwarf2_name (child_die, cu);
9a619af0 13973
418835cc
KS
13974 if (name && !strcmp (name, "this"))
13975 TYPE_FIELD_ARTIFICIAL (ftype, iparams) = 1;
13976 }
13977 }
3ce3b1ba
PA
13978 arg_type = die_type (child_die, cu);
13979
13980 /* RealView does not mark THIS as const, which the testsuite
13981 expects. GCC marks THIS as const in method definitions,
13982 but not in the class specifications (GCC PR 43053). */
13983 if (cu->language == language_cplus && !TYPE_CONST (arg_type)
13984 && TYPE_FIELD_ARTIFICIAL (ftype, iparams))
13985 {
13986 int is_this = 0;
13987 struct dwarf2_cu *arg_cu = cu;
13988 const char *name = dwarf2_name (child_die, cu);
13989
13990 attr = dwarf2_attr (die, DW_AT_object_pointer, cu);
13991 if (attr)
13992 {
13993 /* If the compiler emits this, use it. */
13994 if (follow_die_ref (die, attr, &arg_cu) == child_die)
13995 is_this = 1;
13996 }
13997 else if (name && strcmp (name, "this") == 0)
13998 /* Function definitions will have the argument names. */
13999 is_this = 1;
14000 else if (name == NULL && iparams == 0)
14001 /* Declarations may not have the names, so like
14002 elsewhere in GDB, assume an artificial first
14003 argument is "this". */
14004 is_this = 1;
14005
14006 if (is_this)
14007 arg_type = make_cv_type (1, TYPE_VOLATILE (arg_type),
14008 arg_type, 0);
14009 }
14010
14011 TYPE_FIELD_TYPE (ftype, iparams) = arg_type;
c906108c
SS
14012 iparams++;
14013 }
14014 child_die = sibling_die (child_die);
14015 }
14016 }
14017
76c10ea2 14018 return ftype;
c906108c
SS
14019}
14020
f792889a 14021static struct type *
e7c27a73 14022read_typedef (struct die_info *die, struct dwarf2_cu *cu)
c906108c 14023{
e7c27a73 14024 struct objfile *objfile = cu->objfile;
0114d602 14025 const char *name = NULL;
3c8e0968 14026 struct type *this_type, *target_type;
c906108c 14027
94af9270 14028 name = dwarf2_full_name (NULL, die, cu);
f792889a 14029 this_type = init_type (TYPE_CODE_TYPEDEF, 0,
0114d602 14030 TYPE_FLAG_TARGET_STUB, NULL, objfile);
abee88f2 14031 TYPE_NAME (this_type) = name;
f792889a 14032 set_die_type (die, this_type, cu);
3c8e0968
DE
14033 target_type = die_type (die, cu);
14034 if (target_type != this_type)
14035 TYPE_TARGET_TYPE (this_type) = target_type;
14036 else
14037 {
14038 /* Self-referential typedefs are, it seems, not allowed by the DWARF
14039 spec and cause infinite loops in GDB. */
14040 complaint (&symfile_complaints,
14041 _("Self-referential DW_TAG_typedef "
14042 "- DIE at 0x%x [in module %s]"),
4262abfb 14043 die->offset.sect_off, objfile_name (objfile));
3c8e0968
DE
14044 TYPE_TARGET_TYPE (this_type) = NULL;
14045 }
f792889a 14046 return this_type;
c906108c
SS
14047}
14048
14049/* Find a representation of a given base type and install
14050 it in the TYPE field of the die. */
14051
f792889a 14052static struct type *
e7c27a73 14053read_base_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 14054{
e7c27a73 14055 struct objfile *objfile = cu->objfile;
c906108c
SS
14056 struct type *type;
14057 struct attribute *attr;
14058 int encoding = 0, size = 0;
15d034d0 14059 const char *name;
6ccb9162
UW
14060 enum type_code code = TYPE_CODE_INT;
14061 int type_flags = 0;
14062 struct type *target_type = NULL;
c906108c 14063
e142c38c 14064 attr = dwarf2_attr (die, DW_AT_encoding, cu);
c906108c
SS
14065 if (attr)
14066 {
14067 encoding = DW_UNSND (attr);
14068 }
e142c38c 14069 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
c906108c
SS
14070 if (attr)
14071 {
14072 size = DW_UNSND (attr);
14073 }
39cbfefa 14074 name = dwarf2_name (die, cu);
6ccb9162 14075 if (!name)
c906108c 14076 {
6ccb9162
UW
14077 complaint (&symfile_complaints,
14078 _("DW_AT_name missing from DW_TAG_base_type"));
c906108c 14079 }
6ccb9162
UW
14080
14081 switch (encoding)
c906108c 14082 {
6ccb9162
UW
14083 case DW_ATE_address:
14084 /* Turn DW_ATE_address into a void * pointer. */
14085 code = TYPE_CODE_PTR;
14086 type_flags |= TYPE_FLAG_UNSIGNED;
14087 target_type = init_type (TYPE_CODE_VOID, 1, 0, NULL, objfile);
14088 break;
14089 case DW_ATE_boolean:
14090 code = TYPE_CODE_BOOL;
14091 type_flags |= TYPE_FLAG_UNSIGNED;
14092 break;
14093 case DW_ATE_complex_float:
14094 code = TYPE_CODE_COMPLEX;
14095 target_type = init_type (TYPE_CODE_FLT, size / 2, 0, NULL, objfile);
14096 break;
14097 case DW_ATE_decimal_float:
14098 code = TYPE_CODE_DECFLOAT;
14099 break;
14100 case DW_ATE_float:
14101 code = TYPE_CODE_FLT;
14102 break;
14103 case DW_ATE_signed:
14104 break;
14105 case DW_ATE_unsigned:
14106 type_flags |= TYPE_FLAG_UNSIGNED;
3b2b8fea
TT
14107 if (cu->language == language_fortran
14108 && name
14109 && strncmp (name, "character(", sizeof ("character(") - 1) == 0)
14110 code = TYPE_CODE_CHAR;
6ccb9162
UW
14111 break;
14112 case DW_ATE_signed_char:
6e70227d 14113 if (cu->language == language_ada || cu->language == language_m2
3b2b8fea
TT
14114 || cu->language == language_pascal
14115 || cu->language == language_fortran)
6ccb9162
UW
14116 code = TYPE_CODE_CHAR;
14117 break;
14118 case DW_ATE_unsigned_char:
868a0084 14119 if (cu->language == language_ada || cu->language == language_m2
3b2b8fea
TT
14120 || cu->language == language_pascal
14121 || cu->language == language_fortran)
6ccb9162
UW
14122 code = TYPE_CODE_CHAR;
14123 type_flags |= TYPE_FLAG_UNSIGNED;
14124 break;
75079b2b
TT
14125 case DW_ATE_UTF:
14126 /* We just treat this as an integer and then recognize the
14127 type by name elsewhere. */
14128 break;
14129
6ccb9162
UW
14130 default:
14131 complaint (&symfile_complaints, _("unsupported DW_AT_encoding: '%s'"),
14132 dwarf_type_encoding_name (encoding));
14133 break;
c906108c 14134 }
6ccb9162 14135
0114d602
DJ
14136 type = init_type (code, size, type_flags, NULL, objfile);
14137 TYPE_NAME (type) = name;
6ccb9162
UW
14138 TYPE_TARGET_TYPE (type) = target_type;
14139
0114d602 14140 if (name && strcmp (name, "char") == 0)
876cecd0 14141 TYPE_NOSIGN (type) = 1;
0114d602 14142
f792889a 14143 return set_die_type (die, type, cu);
c906108c
SS
14144}
14145
a02abb62
JB
14146/* Read the given DW_AT_subrange DIE. */
14147
f792889a 14148static struct type *
a02abb62
JB
14149read_subrange_type (struct die_info *die, struct dwarf2_cu *cu)
14150{
4c9ad8c2 14151 struct type *base_type, *orig_base_type;
a02abb62
JB
14152 struct type *range_type;
14153 struct attribute *attr;
4fae6e18
JK
14154 LONGEST low, high;
14155 int low_default_is_valid;
15d034d0 14156 const char *name;
43bbcdc2 14157 LONGEST negative_mask;
e77813c8 14158
4c9ad8c2
TT
14159 orig_base_type = die_type (die, cu);
14160 /* If ORIG_BASE_TYPE is a typedef, it will not be TYPE_UNSIGNED,
14161 whereas the real type might be. So, we use ORIG_BASE_TYPE when
14162 creating the range type, but we use the result of check_typedef
14163 when examining properties of the type. */
14164 base_type = check_typedef (orig_base_type);
a02abb62 14165
7e314c57
JK
14166 /* The die_type call above may have already set the type for this DIE. */
14167 range_type = get_die_type (die, cu);
14168 if (range_type)
14169 return range_type;
14170
4fae6e18
JK
14171 /* Set LOW_DEFAULT_IS_VALID if current language and DWARF version allow
14172 omitting DW_AT_lower_bound. */
14173 switch (cu->language)
6e70227d 14174 {
4fae6e18
JK
14175 case language_c:
14176 case language_cplus:
14177 low = 0;
14178 low_default_is_valid = 1;
14179 break;
14180 case language_fortran:
14181 low = 1;
14182 low_default_is_valid = 1;
14183 break;
14184 case language_d:
14185 case language_java:
14186 case language_objc:
14187 low = 0;
14188 low_default_is_valid = (cu->header.version >= 4);
14189 break;
14190 case language_ada:
14191 case language_m2:
14192 case language_pascal:
a02abb62 14193 low = 1;
4fae6e18
JK
14194 low_default_is_valid = (cu->header.version >= 4);
14195 break;
14196 default:
14197 low = 0;
14198 low_default_is_valid = 0;
14199 break;
a02abb62
JB
14200 }
14201
dd5e6932
DJ
14202 /* FIXME: For variable sized arrays either of these could be
14203 a variable rather than a constant value. We'll allow it,
14204 but we don't know how to handle it. */
e142c38c 14205 attr = dwarf2_attr (die, DW_AT_lower_bound, cu);
a02abb62 14206 if (attr)
4fae6e18
JK
14207 low = dwarf2_get_attr_constant_value (attr, low);
14208 else if (!low_default_is_valid)
14209 complaint (&symfile_complaints, _("Missing DW_AT_lower_bound "
14210 "- DIE at 0x%x [in module %s]"),
4262abfb 14211 die->offset.sect_off, objfile_name (cu->objfile));
a02abb62 14212
e142c38c 14213 attr = dwarf2_attr (die, DW_AT_upper_bound, cu);
a02abb62 14214 if (attr)
6e70227d 14215 {
7771576e 14216 if (attr_form_is_block (attr) || attr_form_is_ref (attr))
a02abb62
JB
14217 {
14218 /* GCC encodes arrays with unspecified or dynamic length
e77813c8 14219 with a DW_FORM_block1 attribute or a reference attribute.
a02abb62
JB
14220 FIXME: GDB does not yet know how to handle dynamic
14221 arrays properly, treat them as arrays with unspecified
14222 length for now.
14223
14224 FIXME: jimb/2003-09-22: GDB does not really know
14225 how to handle arrays of unspecified length
14226 either; we just represent them as zero-length
14227 arrays. Choose an appropriate upper bound given
14228 the lower bound we've computed above. */
14229 high = low - 1;
14230 }
14231 else
14232 high = dwarf2_get_attr_constant_value (attr, 1);
14233 }
e77813c8
PM
14234 else
14235 {
14236 attr = dwarf2_attr (die, DW_AT_count, cu);
14237 if (attr)
14238 {
14239 int count = dwarf2_get_attr_constant_value (attr, 1);
14240 high = low + count - 1;
14241 }
c2ff108b
JK
14242 else
14243 {
14244 /* Unspecified array length. */
14245 high = low - 1;
14246 }
e77813c8
PM
14247 }
14248
14249 /* Dwarf-2 specifications explicitly allows to create subrange types
14250 without specifying a base type.
14251 In that case, the base type must be set to the type of
14252 the lower bound, upper bound or count, in that order, if any of these
14253 three attributes references an object that has a type.
14254 If no base type is found, the Dwarf-2 specifications say that
14255 a signed integer type of size equal to the size of an address should
14256 be used.
14257 For the following C code: `extern char gdb_int [];'
14258 GCC produces an empty range DIE.
14259 FIXME: muller/2010-05-28: Possible references to object for low bound,
0963b4bd 14260 high bound or count are not yet handled by this code. */
e77813c8
PM
14261 if (TYPE_CODE (base_type) == TYPE_CODE_VOID)
14262 {
14263 struct objfile *objfile = cu->objfile;
14264 struct gdbarch *gdbarch = get_objfile_arch (objfile);
14265 int addr_size = gdbarch_addr_bit (gdbarch) /8;
14266 struct type *int_type = objfile_type (objfile)->builtin_int;
14267
14268 /* Test "int", "long int", and "long long int" objfile types,
14269 and select the first one having a size above or equal to the
14270 architecture address size. */
14271 if (int_type && TYPE_LENGTH (int_type) >= addr_size)
14272 base_type = int_type;
14273 else
14274 {
14275 int_type = objfile_type (objfile)->builtin_long;
14276 if (int_type && TYPE_LENGTH (int_type) >= addr_size)
14277 base_type = int_type;
14278 else
14279 {
14280 int_type = objfile_type (objfile)->builtin_long_long;
14281 if (int_type && TYPE_LENGTH (int_type) >= addr_size)
14282 base_type = int_type;
14283 }
14284 }
14285 }
a02abb62 14286
6e70227d 14287 negative_mask =
43bbcdc2
PH
14288 (LONGEST) -1 << (TYPE_LENGTH (base_type) * TARGET_CHAR_BIT - 1);
14289 if (!TYPE_UNSIGNED (base_type) && (low & negative_mask))
14290 low |= negative_mask;
14291 if (!TYPE_UNSIGNED (base_type) && (high & negative_mask))
14292 high |= negative_mask;
14293
4c9ad8c2 14294 range_type = create_range_type (NULL, orig_base_type, low, high);
a02abb62 14295
bbb0eef6
JK
14296 /* Mark arrays with dynamic length at least as an array of unspecified
14297 length. GDB could check the boundary but before it gets implemented at
14298 least allow accessing the array elements. */
d48323d8 14299 if (attr && attr_form_is_block (attr))
bbb0eef6
JK
14300 TYPE_HIGH_BOUND_UNDEFINED (range_type) = 1;
14301
c2ff108b
JK
14302 /* Ada expects an empty array on no boundary attributes. */
14303 if (attr == NULL && cu->language != language_ada)
14304 TYPE_HIGH_BOUND_UNDEFINED (range_type) = 1;
14305
39cbfefa
DJ
14306 name = dwarf2_name (die, cu);
14307 if (name)
14308 TYPE_NAME (range_type) = name;
6e70227d 14309
e142c38c 14310 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
a02abb62
JB
14311 if (attr)
14312 TYPE_LENGTH (range_type) = DW_UNSND (attr);
14313
7e314c57
JK
14314 set_die_type (die, range_type, cu);
14315
14316 /* set_die_type should be already done. */
b4ba55a1
JB
14317 set_descriptive_type (range_type, die, cu);
14318
7e314c57 14319 return range_type;
a02abb62 14320}
6e70227d 14321
f792889a 14322static struct type *
81a17f79
JB
14323read_unspecified_type (struct die_info *die, struct dwarf2_cu *cu)
14324{
14325 struct type *type;
81a17f79 14326
81a17f79
JB
14327 /* For now, we only support the C meaning of an unspecified type: void. */
14328
0114d602
DJ
14329 type = init_type (TYPE_CODE_VOID, 0, 0, NULL, cu->objfile);
14330 TYPE_NAME (type) = dwarf2_name (die, cu);
81a17f79 14331
f792889a 14332 return set_die_type (die, type, cu);
81a17f79 14333}
a02abb62 14334
639d11d3
DC
14335/* Read a single die and all its descendents. Set the die's sibling
14336 field to NULL; set other fields in the die correctly, and set all
14337 of the descendents' fields correctly. Set *NEW_INFO_PTR to the
14338 location of the info_ptr after reading all of those dies. PARENT
14339 is the parent of the die in question. */
14340
14341static struct die_info *
dee91e82 14342read_die_and_children (const struct die_reader_specs *reader,
d521ce57
TT
14343 const gdb_byte *info_ptr,
14344 const gdb_byte **new_info_ptr,
dee91e82 14345 struct die_info *parent)
639d11d3
DC
14346{
14347 struct die_info *die;
d521ce57 14348 const gdb_byte *cur_ptr;
639d11d3
DC
14349 int has_children;
14350
bf6af496 14351 cur_ptr = read_full_die_1 (reader, &die, info_ptr, &has_children, 0);
1d325ec1
DJ
14352 if (die == NULL)
14353 {
14354 *new_info_ptr = cur_ptr;
14355 return NULL;
14356 }
93311388 14357 store_in_ref_table (die, reader->cu);
639d11d3
DC
14358
14359 if (has_children)
bf6af496 14360 die->child = read_die_and_siblings_1 (reader, cur_ptr, new_info_ptr, die);
639d11d3
DC
14361 else
14362 {
14363 die->child = NULL;
14364 *new_info_ptr = cur_ptr;
14365 }
14366
14367 die->sibling = NULL;
14368 die->parent = parent;
14369 return die;
14370}
14371
14372/* Read a die, all of its descendents, and all of its siblings; set
14373 all of the fields of all of the dies correctly. Arguments are as
14374 in read_die_and_children. */
14375
14376static struct die_info *
bf6af496 14377read_die_and_siblings_1 (const struct die_reader_specs *reader,
d521ce57
TT
14378 const gdb_byte *info_ptr,
14379 const gdb_byte **new_info_ptr,
bf6af496 14380 struct die_info *parent)
639d11d3
DC
14381{
14382 struct die_info *first_die, *last_sibling;
d521ce57 14383 const gdb_byte *cur_ptr;
639d11d3 14384
c906108c 14385 cur_ptr = info_ptr;
639d11d3
DC
14386 first_die = last_sibling = NULL;
14387
14388 while (1)
c906108c 14389 {
639d11d3 14390 struct die_info *die
dee91e82 14391 = read_die_and_children (reader, cur_ptr, &cur_ptr, parent);
639d11d3 14392
1d325ec1 14393 if (die == NULL)
c906108c 14394 {
639d11d3
DC
14395 *new_info_ptr = cur_ptr;
14396 return first_die;
c906108c 14397 }
1d325ec1
DJ
14398
14399 if (!first_die)
14400 first_die = die;
c906108c 14401 else
1d325ec1
DJ
14402 last_sibling->sibling = die;
14403
14404 last_sibling = die;
c906108c 14405 }
c906108c
SS
14406}
14407
bf6af496
DE
14408/* Read a die, all of its descendents, and all of its siblings; set
14409 all of the fields of all of the dies correctly. Arguments are as
14410 in read_die_and_children.
14411 This the main entry point for reading a DIE and all its children. */
14412
14413static struct die_info *
14414read_die_and_siblings (const struct die_reader_specs *reader,
d521ce57
TT
14415 const gdb_byte *info_ptr,
14416 const gdb_byte **new_info_ptr,
bf6af496
DE
14417 struct die_info *parent)
14418{
14419 struct die_info *die = read_die_and_siblings_1 (reader, info_ptr,
14420 new_info_ptr, parent);
14421
14422 if (dwarf2_die_debug)
14423 {
14424 fprintf_unfiltered (gdb_stdlog,
14425 "Read die from %s@0x%x of %s:\n",
a32a8923 14426 get_section_name (reader->die_section),
bf6af496
DE
14427 (unsigned) (info_ptr - reader->die_section->buffer),
14428 bfd_get_filename (reader->abfd));
14429 dump_die (die, dwarf2_die_debug);
14430 }
14431
14432 return die;
14433}
14434
3019eac3
DE
14435/* Read a die and all its attributes, leave space for NUM_EXTRA_ATTRS
14436 attributes.
14437 The caller is responsible for filling in the extra attributes
14438 and updating (*DIEP)->num_attrs.
14439 Set DIEP to point to a newly allocated die with its information,
14440 except for its child, sibling, and parent fields.
14441 Set HAS_CHILDREN to tell whether the die has children or not. */
93311388 14442
d521ce57 14443static const gdb_byte *
3019eac3 14444read_full_die_1 (const struct die_reader_specs *reader,
d521ce57 14445 struct die_info **diep, const gdb_byte *info_ptr,
3019eac3 14446 int *has_children, int num_extra_attrs)
93311388 14447{
b64f50a1
JK
14448 unsigned int abbrev_number, bytes_read, i;
14449 sect_offset offset;
93311388
DE
14450 struct abbrev_info *abbrev;
14451 struct die_info *die;
14452 struct dwarf2_cu *cu = reader->cu;
14453 bfd *abfd = reader->abfd;
14454
b64f50a1 14455 offset.sect_off = info_ptr - reader->buffer;
93311388
DE
14456 abbrev_number = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
14457 info_ptr += bytes_read;
14458 if (!abbrev_number)
14459 {
14460 *diep = NULL;
14461 *has_children = 0;
14462 return info_ptr;
14463 }
14464
433df2d4 14465 abbrev = abbrev_table_lookup_abbrev (cu->abbrev_table, abbrev_number);
93311388 14466 if (!abbrev)
348e048f
DE
14467 error (_("Dwarf Error: could not find abbrev number %d [in module %s]"),
14468 abbrev_number,
14469 bfd_get_filename (abfd));
14470
3019eac3 14471 die = dwarf_alloc_die (cu, abbrev->num_attrs + num_extra_attrs);
93311388
DE
14472 die->offset = offset;
14473 die->tag = abbrev->tag;
14474 die->abbrev = abbrev_number;
14475
3019eac3
DE
14476 /* Make the result usable.
14477 The caller needs to update num_attrs after adding the extra
14478 attributes. */
93311388
DE
14479 die->num_attrs = abbrev->num_attrs;
14480
14481 for (i = 0; i < abbrev->num_attrs; ++i)
dee91e82
DE
14482 info_ptr = read_attribute (reader, &die->attrs[i], &abbrev->attrs[i],
14483 info_ptr);
93311388
DE
14484
14485 *diep = die;
14486 *has_children = abbrev->has_children;
14487 return info_ptr;
14488}
14489
3019eac3
DE
14490/* Read a die and all its attributes.
14491 Set DIEP to point to a newly allocated die with its information,
14492 except for its child, sibling, and parent fields.
14493 Set HAS_CHILDREN to tell whether the die has children or not. */
14494
d521ce57 14495static const gdb_byte *
3019eac3 14496read_full_die (const struct die_reader_specs *reader,
d521ce57 14497 struct die_info **diep, const gdb_byte *info_ptr,
3019eac3
DE
14498 int *has_children)
14499{
d521ce57 14500 const gdb_byte *result;
bf6af496
DE
14501
14502 result = read_full_die_1 (reader, diep, info_ptr, has_children, 0);
14503
14504 if (dwarf2_die_debug)
14505 {
14506 fprintf_unfiltered (gdb_stdlog,
14507 "Read die from %s@0x%x of %s:\n",
a32a8923 14508 get_section_name (reader->die_section),
bf6af496
DE
14509 (unsigned) (info_ptr - reader->die_section->buffer),
14510 bfd_get_filename (reader->abfd));
14511 dump_die (*diep, dwarf2_die_debug);
14512 }
14513
14514 return result;
3019eac3 14515}
433df2d4
DE
14516\f
14517/* Abbreviation tables.
3019eac3 14518
433df2d4 14519 In DWARF version 2, the description of the debugging information is
c906108c
SS
14520 stored in a separate .debug_abbrev section. Before we read any
14521 dies from a section we read in all abbreviations and install them
433df2d4
DE
14522 in a hash table. */
14523
14524/* Allocate space for a struct abbrev_info object in ABBREV_TABLE. */
14525
14526static struct abbrev_info *
14527abbrev_table_alloc_abbrev (struct abbrev_table *abbrev_table)
14528{
14529 struct abbrev_info *abbrev;
14530
14531 abbrev = (struct abbrev_info *)
14532 obstack_alloc (&abbrev_table->abbrev_obstack, sizeof (struct abbrev_info));
14533 memset (abbrev, 0, sizeof (struct abbrev_info));
14534 return abbrev;
14535}
14536
14537/* Add an abbreviation to the table. */
c906108c
SS
14538
14539static void
433df2d4
DE
14540abbrev_table_add_abbrev (struct abbrev_table *abbrev_table,
14541 unsigned int abbrev_number,
14542 struct abbrev_info *abbrev)
14543{
14544 unsigned int hash_number;
14545
14546 hash_number = abbrev_number % ABBREV_HASH_SIZE;
14547 abbrev->next = abbrev_table->abbrevs[hash_number];
14548 abbrev_table->abbrevs[hash_number] = abbrev;
14549}
dee91e82 14550
433df2d4
DE
14551/* Look up an abbrev in the table.
14552 Returns NULL if the abbrev is not found. */
14553
14554static struct abbrev_info *
14555abbrev_table_lookup_abbrev (const struct abbrev_table *abbrev_table,
14556 unsigned int abbrev_number)
c906108c 14557{
433df2d4
DE
14558 unsigned int hash_number;
14559 struct abbrev_info *abbrev;
14560
14561 hash_number = abbrev_number % ABBREV_HASH_SIZE;
14562 abbrev = abbrev_table->abbrevs[hash_number];
14563
14564 while (abbrev)
14565 {
14566 if (abbrev->number == abbrev_number)
14567 return abbrev;
14568 abbrev = abbrev->next;
14569 }
14570 return NULL;
14571}
14572
14573/* Read in an abbrev table. */
14574
14575static struct abbrev_table *
14576abbrev_table_read_table (struct dwarf2_section_info *section,
14577 sect_offset offset)
14578{
14579 struct objfile *objfile = dwarf2_per_objfile->objfile;
a32a8923 14580 bfd *abfd = get_section_bfd_owner (section);
433df2d4 14581 struct abbrev_table *abbrev_table;
d521ce57 14582 const gdb_byte *abbrev_ptr;
c906108c
SS
14583 struct abbrev_info *cur_abbrev;
14584 unsigned int abbrev_number, bytes_read, abbrev_name;
433df2d4 14585 unsigned int abbrev_form;
f3dd6933
DJ
14586 struct attr_abbrev *cur_attrs;
14587 unsigned int allocated_attrs;
c906108c 14588
433df2d4 14589 abbrev_table = XMALLOC (struct abbrev_table);
f4dc4d17 14590 abbrev_table->offset = offset;
433df2d4
DE
14591 obstack_init (&abbrev_table->abbrev_obstack);
14592 abbrev_table->abbrevs = obstack_alloc (&abbrev_table->abbrev_obstack,
14593 (ABBREV_HASH_SIZE
14594 * sizeof (struct abbrev_info *)));
14595 memset (abbrev_table->abbrevs, 0,
14596 ABBREV_HASH_SIZE * sizeof (struct abbrev_info *));
c906108c 14597
433df2d4
DE
14598 dwarf2_read_section (objfile, section);
14599 abbrev_ptr = section->buffer + offset.sect_off;
c906108c
SS
14600 abbrev_number = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
14601 abbrev_ptr += bytes_read;
14602
f3dd6933
DJ
14603 allocated_attrs = ATTR_ALLOC_CHUNK;
14604 cur_attrs = xmalloc (allocated_attrs * sizeof (struct attr_abbrev));
6e70227d 14605
0963b4bd 14606 /* Loop until we reach an abbrev number of 0. */
c906108c
SS
14607 while (abbrev_number)
14608 {
433df2d4 14609 cur_abbrev = abbrev_table_alloc_abbrev (abbrev_table);
c906108c
SS
14610
14611 /* read in abbrev header */
14612 cur_abbrev->number = abbrev_number;
14613 cur_abbrev->tag = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
14614 abbrev_ptr += bytes_read;
14615 cur_abbrev->has_children = read_1_byte (abfd, abbrev_ptr);
14616 abbrev_ptr += 1;
14617
14618 /* now read in declarations */
14619 abbrev_name = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
14620 abbrev_ptr += bytes_read;
14621 abbrev_form = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
14622 abbrev_ptr += bytes_read;
14623 while (abbrev_name)
14624 {
f3dd6933 14625 if (cur_abbrev->num_attrs == allocated_attrs)
c906108c 14626 {
f3dd6933
DJ
14627 allocated_attrs += ATTR_ALLOC_CHUNK;
14628 cur_attrs
14629 = xrealloc (cur_attrs, (allocated_attrs
14630 * sizeof (struct attr_abbrev)));
c906108c 14631 }
ae038cb0 14632
f3dd6933
DJ
14633 cur_attrs[cur_abbrev->num_attrs].name = abbrev_name;
14634 cur_attrs[cur_abbrev->num_attrs++].form = abbrev_form;
c906108c
SS
14635 abbrev_name = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
14636 abbrev_ptr += bytes_read;
14637 abbrev_form = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
14638 abbrev_ptr += bytes_read;
14639 }
14640
433df2d4 14641 cur_abbrev->attrs = obstack_alloc (&abbrev_table->abbrev_obstack,
f3dd6933
DJ
14642 (cur_abbrev->num_attrs
14643 * sizeof (struct attr_abbrev)));
14644 memcpy (cur_abbrev->attrs, cur_attrs,
14645 cur_abbrev->num_attrs * sizeof (struct attr_abbrev));
14646
433df2d4 14647 abbrev_table_add_abbrev (abbrev_table, abbrev_number, cur_abbrev);
c906108c
SS
14648
14649 /* Get next abbreviation.
14650 Under Irix6 the abbreviations for a compilation unit are not
c5aa993b
JM
14651 always properly terminated with an abbrev number of 0.
14652 Exit loop if we encounter an abbreviation which we have
14653 already read (which means we are about to read the abbreviations
14654 for the next compile unit) or if the end of the abbreviation
14655 table is reached. */
433df2d4 14656 if ((unsigned int) (abbrev_ptr - section->buffer) >= section->size)
c906108c
SS
14657 break;
14658 abbrev_number = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
14659 abbrev_ptr += bytes_read;
433df2d4 14660 if (abbrev_table_lookup_abbrev (abbrev_table, abbrev_number) != NULL)
c906108c
SS
14661 break;
14662 }
f3dd6933
DJ
14663
14664 xfree (cur_attrs);
433df2d4 14665 return abbrev_table;
c906108c
SS
14666}
14667
433df2d4 14668/* Free the resources held by ABBREV_TABLE. */
c906108c 14669
c906108c 14670static void
433df2d4 14671abbrev_table_free (struct abbrev_table *abbrev_table)
c906108c 14672{
433df2d4
DE
14673 obstack_free (&abbrev_table->abbrev_obstack, NULL);
14674 xfree (abbrev_table);
c906108c
SS
14675}
14676
f4dc4d17
DE
14677/* Same as abbrev_table_free but as a cleanup.
14678 We pass in a pointer to the pointer to the table so that we can
14679 set the pointer to NULL when we're done. It also simplifies
14680 build_type_unit_groups. */
14681
14682static void
14683abbrev_table_free_cleanup (void *table_ptr)
14684{
14685 struct abbrev_table **abbrev_table_ptr = table_ptr;
14686
14687 if (*abbrev_table_ptr != NULL)
14688 abbrev_table_free (*abbrev_table_ptr);
14689 *abbrev_table_ptr = NULL;
14690}
14691
433df2d4
DE
14692/* Read the abbrev table for CU from ABBREV_SECTION. */
14693
14694static void
14695dwarf2_read_abbrevs (struct dwarf2_cu *cu,
14696 struct dwarf2_section_info *abbrev_section)
c906108c 14697{
433df2d4
DE
14698 cu->abbrev_table =
14699 abbrev_table_read_table (abbrev_section, cu->header.abbrev_offset);
14700}
c906108c 14701
433df2d4 14702/* Release the memory used by the abbrev table for a compilation unit. */
c906108c 14703
433df2d4
DE
14704static void
14705dwarf2_free_abbrev_table (void *ptr_to_cu)
14706{
14707 struct dwarf2_cu *cu = ptr_to_cu;
c906108c 14708
a2ce51a0
DE
14709 if (cu->abbrev_table != NULL)
14710 abbrev_table_free (cu->abbrev_table);
433df2d4
DE
14711 /* Set this to NULL so that we SEGV if we try to read it later,
14712 and also because free_comp_unit verifies this is NULL. */
14713 cu->abbrev_table = NULL;
14714}
14715\f
72bf9492
DJ
14716/* Returns nonzero if TAG represents a type that we might generate a partial
14717 symbol for. */
14718
14719static int
14720is_type_tag_for_partial (int tag)
14721{
14722 switch (tag)
14723 {
14724#if 0
14725 /* Some types that would be reasonable to generate partial symbols for,
14726 that we don't at present. */
14727 case DW_TAG_array_type:
14728 case DW_TAG_file_type:
14729 case DW_TAG_ptr_to_member_type:
14730 case DW_TAG_set_type:
14731 case DW_TAG_string_type:
14732 case DW_TAG_subroutine_type:
14733#endif
14734 case DW_TAG_base_type:
14735 case DW_TAG_class_type:
680b30c7 14736 case DW_TAG_interface_type:
72bf9492
DJ
14737 case DW_TAG_enumeration_type:
14738 case DW_TAG_structure_type:
14739 case DW_TAG_subrange_type:
14740 case DW_TAG_typedef:
14741 case DW_TAG_union_type:
14742 return 1;
14743 default:
14744 return 0;
14745 }
14746}
14747
14748/* Load all DIEs that are interesting for partial symbols into memory. */
14749
14750static struct partial_die_info *
dee91e82 14751load_partial_dies (const struct die_reader_specs *reader,
d521ce57 14752 const gdb_byte *info_ptr, int building_psymtab)
72bf9492 14753{
dee91e82 14754 struct dwarf2_cu *cu = reader->cu;
bb5ed363 14755 struct objfile *objfile = cu->objfile;
72bf9492
DJ
14756 struct partial_die_info *part_die;
14757 struct partial_die_info *parent_die, *last_die, *first_die = NULL;
14758 struct abbrev_info *abbrev;
14759 unsigned int bytes_read;
5afb4e99 14760 unsigned int load_all = 0;
72bf9492
DJ
14761 int nesting_level = 1;
14762
14763 parent_die = NULL;
14764 last_die = NULL;
14765
7adf1e79
DE
14766 gdb_assert (cu->per_cu != NULL);
14767 if (cu->per_cu->load_all_dies)
5afb4e99
DJ
14768 load_all = 1;
14769
72bf9492
DJ
14770 cu->partial_dies
14771 = htab_create_alloc_ex (cu->header.length / 12,
14772 partial_die_hash,
14773 partial_die_eq,
14774 NULL,
14775 &cu->comp_unit_obstack,
14776 hashtab_obstack_allocate,
14777 dummy_obstack_deallocate);
14778
14779 part_die = obstack_alloc (&cu->comp_unit_obstack,
14780 sizeof (struct partial_die_info));
14781
14782 while (1)
14783 {
14784 abbrev = peek_die_abbrev (info_ptr, &bytes_read, cu);
14785
14786 /* A NULL abbrev means the end of a series of children. */
14787 if (abbrev == NULL)
14788 {
14789 if (--nesting_level == 0)
14790 {
14791 /* PART_DIE was probably the last thing allocated on the
14792 comp_unit_obstack, so we could call obstack_free
14793 here. We don't do that because the waste is small,
14794 and will be cleaned up when we're done with this
14795 compilation unit. This way, we're also more robust
14796 against other users of the comp_unit_obstack. */
14797 return first_die;
14798 }
14799 info_ptr += bytes_read;
14800 last_die = parent_die;
14801 parent_die = parent_die->die_parent;
14802 continue;
14803 }
14804
98bfdba5
PA
14805 /* Check for template arguments. We never save these; if
14806 they're seen, we just mark the parent, and go on our way. */
14807 if (parent_die != NULL
14808 && cu->language == language_cplus
14809 && (abbrev->tag == DW_TAG_template_type_param
14810 || abbrev->tag == DW_TAG_template_value_param))
14811 {
14812 parent_die->has_template_arguments = 1;
14813
14814 if (!load_all)
14815 {
14816 /* We don't need a partial DIE for the template argument. */
dee91e82 14817 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
98bfdba5
PA
14818 continue;
14819 }
14820 }
14821
0d99eb77 14822 /* We only recurse into c++ subprograms looking for template arguments.
98bfdba5
PA
14823 Skip their other children. */
14824 if (!load_all
14825 && cu->language == language_cplus
14826 && parent_die != NULL
14827 && parent_die->tag == DW_TAG_subprogram)
14828 {
dee91e82 14829 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
98bfdba5
PA
14830 continue;
14831 }
14832
5afb4e99
DJ
14833 /* Check whether this DIE is interesting enough to save. Normally
14834 we would not be interested in members here, but there may be
14835 later variables referencing them via DW_AT_specification (for
14836 static members). */
14837 if (!load_all
14838 && !is_type_tag_for_partial (abbrev->tag)
72929c62 14839 && abbrev->tag != DW_TAG_constant
72bf9492
DJ
14840 && abbrev->tag != DW_TAG_enumerator
14841 && abbrev->tag != DW_TAG_subprogram
bc30ff58 14842 && abbrev->tag != DW_TAG_lexical_block
72bf9492 14843 && abbrev->tag != DW_TAG_variable
5afb4e99 14844 && abbrev->tag != DW_TAG_namespace
f55ee35c 14845 && abbrev->tag != DW_TAG_module
95554aad
TT
14846 && abbrev->tag != DW_TAG_member
14847 && abbrev->tag != DW_TAG_imported_unit)
72bf9492
DJ
14848 {
14849 /* Otherwise we skip to the next sibling, if any. */
dee91e82 14850 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
72bf9492
DJ
14851 continue;
14852 }
14853
dee91e82
DE
14854 info_ptr = read_partial_die (reader, part_die, abbrev, bytes_read,
14855 info_ptr);
72bf9492
DJ
14856
14857 /* This two-pass algorithm for processing partial symbols has a
14858 high cost in cache pressure. Thus, handle some simple cases
14859 here which cover the majority of C partial symbols. DIEs
14860 which neither have specification tags in them, nor could have
14861 specification tags elsewhere pointing at them, can simply be
14862 processed and discarded.
14863
14864 This segment is also optional; scan_partial_symbols and
14865 add_partial_symbol will handle these DIEs if we chain
14866 them in normally. When compilers which do not emit large
14867 quantities of duplicate debug information are more common,
14868 this code can probably be removed. */
14869
14870 /* Any complete simple types at the top level (pretty much all
14871 of them, for a language without namespaces), can be processed
14872 directly. */
14873 if (parent_die == NULL
14874 && part_die->has_specification == 0
14875 && part_die->is_declaration == 0
d8228535 14876 && ((part_die->tag == DW_TAG_typedef && !part_die->has_children)
72bf9492
DJ
14877 || part_die->tag == DW_TAG_base_type
14878 || part_die->tag == DW_TAG_subrange_type))
14879 {
14880 if (building_psymtab && part_die->name != NULL)
04a679b8 14881 add_psymbol_to_list (part_die->name, strlen (part_die->name), 0,
72bf9492 14882 VAR_DOMAIN, LOC_TYPEDEF,
bb5ed363
DE
14883 &objfile->static_psymbols,
14884 0, (CORE_ADDR) 0, cu->language, objfile);
dee91e82 14885 info_ptr = locate_pdi_sibling (reader, part_die, info_ptr);
72bf9492
DJ
14886 continue;
14887 }
14888
d8228535
JK
14889 /* The exception for DW_TAG_typedef with has_children above is
14890 a workaround of GCC PR debug/47510. In the case of this complaint
14891 type_name_no_tag_or_error will error on such types later.
14892
14893 GDB skipped children of DW_TAG_typedef by the shortcut above and then
14894 it could not find the child DIEs referenced later, this is checked
14895 above. In correct DWARF DW_TAG_typedef should have no children. */
14896
14897 if (part_die->tag == DW_TAG_typedef && part_die->has_children)
14898 complaint (&symfile_complaints,
14899 _("DW_TAG_typedef has childen - GCC PR debug/47510 bug "
14900 "- DIE at 0x%x [in module %s]"),
4262abfb 14901 part_die->offset.sect_off, objfile_name (objfile));
d8228535 14902
72bf9492
DJ
14903 /* If we're at the second level, and we're an enumerator, and
14904 our parent has no specification (meaning possibly lives in a
14905 namespace elsewhere), then we can add the partial symbol now
14906 instead of queueing it. */
14907 if (part_die->tag == DW_TAG_enumerator
14908 && parent_die != NULL
14909 && parent_die->die_parent == NULL
14910 && parent_die->tag == DW_TAG_enumeration_type
14911 && parent_die->has_specification == 0)
14912 {
14913 if (part_die->name == NULL)
3e43a32a
MS
14914 complaint (&symfile_complaints,
14915 _("malformed enumerator DIE ignored"));
72bf9492 14916 else if (building_psymtab)
04a679b8 14917 add_psymbol_to_list (part_die->name, strlen (part_die->name), 0,
72bf9492 14918 VAR_DOMAIN, LOC_CONST,
987504bb
JJ
14919 (cu->language == language_cplus
14920 || cu->language == language_java)
bb5ed363
DE
14921 ? &objfile->global_psymbols
14922 : &objfile->static_psymbols,
14923 0, (CORE_ADDR) 0, cu->language, objfile);
72bf9492 14924
dee91e82 14925 info_ptr = locate_pdi_sibling (reader, part_die, info_ptr);
72bf9492
DJ
14926 continue;
14927 }
14928
14929 /* We'll save this DIE so link it in. */
14930 part_die->die_parent = parent_die;
14931 part_die->die_sibling = NULL;
14932 part_die->die_child = NULL;
14933
14934 if (last_die && last_die == parent_die)
14935 last_die->die_child = part_die;
14936 else if (last_die)
14937 last_die->die_sibling = part_die;
14938
14939 last_die = part_die;
14940
14941 if (first_die == NULL)
14942 first_die = part_die;
14943
14944 /* Maybe add the DIE to the hash table. Not all DIEs that we
14945 find interesting need to be in the hash table, because we
14946 also have the parent/sibling/child chains; only those that we
14947 might refer to by offset later during partial symbol reading.
14948
14949 For now this means things that might have be the target of a
14950 DW_AT_specification, DW_AT_abstract_origin, or
14951 DW_AT_extension. DW_AT_extension will refer only to
14952 namespaces; DW_AT_abstract_origin refers to functions (and
14953 many things under the function DIE, but we do not recurse
14954 into function DIEs during partial symbol reading) and
14955 possibly variables as well; DW_AT_specification refers to
14956 declarations. Declarations ought to have the DW_AT_declaration
14957 flag. It happens that GCC forgets to put it in sometimes, but
14958 only for functions, not for types.
14959
14960 Adding more things than necessary to the hash table is harmless
14961 except for the performance cost. Adding too few will result in
5afb4e99
DJ
14962 wasted time in find_partial_die, when we reread the compilation
14963 unit with load_all_dies set. */
72bf9492 14964
5afb4e99 14965 if (load_all
72929c62 14966 || abbrev->tag == DW_TAG_constant
5afb4e99 14967 || abbrev->tag == DW_TAG_subprogram
72bf9492
DJ
14968 || abbrev->tag == DW_TAG_variable
14969 || abbrev->tag == DW_TAG_namespace
14970 || part_die->is_declaration)
14971 {
14972 void **slot;
14973
14974 slot = htab_find_slot_with_hash (cu->partial_dies, part_die,
b64f50a1 14975 part_die->offset.sect_off, INSERT);
72bf9492
DJ
14976 *slot = part_die;
14977 }
14978
14979 part_die = obstack_alloc (&cu->comp_unit_obstack,
14980 sizeof (struct partial_die_info));
14981
14982 /* For some DIEs we want to follow their children (if any). For C
bc30ff58 14983 we have no reason to follow the children of structures; for other
98bfdba5
PA
14984 languages we have to, so that we can get at method physnames
14985 to infer fully qualified class names, for DW_AT_specification,
14986 and for C++ template arguments. For C++, we also look one level
14987 inside functions to find template arguments (if the name of the
14988 function does not already contain the template arguments).
bc30ff58
JB
14989
14990 For Ada, we need to scan the children of subprograms and lexical
14991 blocks as well because Ada allows the definition of nested
14992 entities that could be interesting for the debugger, such as
14993 nested subprograms for instance. */
72bf9492 14994 if (last_die->has_children
5afb4e99
DJ
14995 && (load_all
14996 || last_die->tag == DW_TAG_namespace
f55ee35c 14997 || last_die->tag == DW_TAG_module
72bf9492 14998 || last_die->tag == DW_TAG_enumeration_type
98bfdba5
PA
14999 || (cu->language == language_cplus
15000 && last_die->tag == DW_TAG_subprogram
15001 && (last_die->name == NULL
15002 || strchr (last_die->name, '<') == NULL))
72bf9492
DJ
15003 || (cu->language != language_c
15004 && (last_die->tag == DW_TAG_class_type
680b30c7 15005 || last_die->tag == DW_TAG_interface_type
72bf9492 15006 || last_die->tag == DW_TAG_structure_type
bc30ff58
JB
15007 || last_die->tag == DW_TAG_union_type))
15008 || (cu->language == language_ada
15009 && (last_die->tag == DW_TAG_subprogram
15010 || last_die->tag == DW_TAG_lexical_block))))
72bf9492
DJ
15011 {
15012 nesting_level++;
15013 parent_die = last_die;
15014 continue;
15015 }
15016
15017 /* Otherwise we skip to the next sibling, if any. */
dee91e82 15018 info_ptr = locate_pdi_sibling (reader, last_die, info_ptr);
72bf9492
DJ
15019
15020 /* Back to the top, do it again. */
15021 }
15022}
15023
c906108c
SS
15024/* Read a minimal amount of information into the minimal die structure. */
15025
d521ce57 15026static const gdb_byte *
dee91e82
DE
15027read_partial_die (const struct die_reader_specs *reader,
15028 struct partial_die_info *part_die,
15029 struct abbrev_info *abbrev, unsigned int abbrev_len,
d521ce57 15030 const gdb_byte *info_ptr)
c906108c 15031{
dee91e82 15032 struct dwarf2_cu *cu = reader->cu;
bb5ed363 15033 struct objfile *objfile = cu->objfile;
d521ce57 15034 const gdb_byte *buffer = reader->buffer;
fa238c03 15035 unsigned int i;
c906108c 15036 struct attribute attr;
c5aa993b 15037 int has_low_pc_attr = 0;
c906108c 15038 int has_high_pc_attr = 0;
91da1414 15039 int high_pc_relative = 0;
c906108c 15040
72bf9492 15041 memset (part_die, 0, sizeof (struct partial_die_info));
c906108c 15042
b64f50a1 15043 part_die->offset.sect_off = info_ptr - buffer;
72bf9492
DJ
15044
15045 info_ptr += abbrev_len;
15046
15047 if (abbrev == NULL)
15048 return info_ptr;
15049
c906108c
SS
15050 part_die->tag = abbrev->tag;
15051 part_die->has_children = abbrev->has_children;
c906108c
SS
15052
15053 for (i = 0; i < abbrev->num_attrs; ++i)
15054 {
dee91e82 15055 info_ptr = read_attribute (reader, &attr, &abbrev->attrs[i], info_ptr);
c906108c
SS
15056
15057 /* Store the data if it is of an attribute we want to keep in a
c5aa993b 15058 partial symbol table. */
c906108c
SS
15059 switch (attr.name)
15060 {
15061 case DW_AT_name:
71c25dea
TT
15062 switch (part_die->tag)
15063 {
15064 case DW_TAG_compile_unit:
95554aad 15065 case DW_TAG_partial_unit:
348e048f 15066 case DW_TAG_type_unit:
71c25dea
TT
15067 /* Compilation units have a DW_AT_name that is a filename, not
15068 a source language identifier. */
15069 case DW_TAG_enumeration_type:
15070 case DW_TAG_enumerator:
15071 /* These tags always have simple identifiers already; no need
15072 to canonicalize them. */
15073 part_die->name = DW_STRING (&attr);
15074 break;
15075 default:
15076 part_die->name
15077 = dwarf2_canonicalize_name (DW_STRING (&attr), cu,
bb5ed363 15078 &objfile->objfile_obstack);
71c25dea
TT
15079 break;
15080 }
c906108c 15081 break;
31ef98ae 15082 case DW_AT_linkage_name:
c906108c 15083 case DW_AT_MIPS_linkage_name:
31ef98ae
TT
15084 /* Note that both forms of linkage name might appear. We
15085 assume they will be the same, and we only store the last
15086 one we see. */
94af9270
KS
15087 if (cu->language == language_ada)
15088 part_die->name = DW_STRING (&attr);
abc72ce4 15089 part_die->linkage_name = DW_STRING (&attr);
c906108c
SS
15090 break;
15091 case DW_AT_low_pc:
15092 has_low_pc_attr = 1;
15093 part_die->lowpc = DW_ADDR (&attr);
15094 break;
15095 case DW_AT_high_pc:
15096 has_high_pc_attr = 1;
3019eac3
DE
15097 if (attr.form == DW_FORM_addr
15098 || attr.form == DW_FORM_GNU_addr_index)
91da1414
MW
15099 part_die->highpc = DW_ADDR (&attr);
15100 else
15101 {
15102 high_pc_relative = 1;
15103 part_die->highpc = DW_UNSND (&attr);
15104 }
c906108c
SS
15105 break;
15106 case DW_AT_location:
0963b4bd 15107 /* Support the .debug_loc offsets. */
8e19ed76
PS
15108 if (attr_form_is_block (&attr))
15109 {
95554aad 15110 part_die->d.locdesc = DW_BLOCK (&attr);
8e19ed76 15111 }
3690dd37 15112 else if (attr_form_is_section_offset (&attr))
8e19ed76 15113 {
4d3c2250 15114 dwarf2_complex_location_expr_complaint ();
8e19ed76
PS
15115 }
15116 else
15117 {
4d3c2250
KB
15118 dwarf2_invalid_attrib_class_complaint ("DW_AT_location",
15119 "partial symbol information");
8e19ed76 15120 }
c906108c 15121 break;
c906108c
SS
15122 case DW_AT_external:
15123 part_die->is_external = DW_UNSND (&attr);
15124 break;
15125 case DW_AT_declaration:
15126 part_die->is_declaration = DW_UNSND (&attr);
15127 break;
15128 case DW_AT_type:
15129 part_die->has_type = 1;
15130 break;
15131 case DW_AT_abstract_origin:
15132 case DW_AT_specification:
72bf9492
DJ
15133 case DW_AT_extension:
15134 part_die->has_specification = 1;
c764a876 15135 part_die->spec_offset = dwarf2_get_ref_die_offset (&attr);
36586728
TT
15136 part_die->spec_is_dwz = (attr.form == DW_FORM_GNU_ref_alt
15137 || cu->per_cu->is_dwz);
c906108c
SS
15138 break;
15139 case DW_AT_sibling:
15140 /* Ignore absolute siblings, they might point outside of
15141 the current compile unit. */
15142 if (attr.form == DW_FORM_ref_addr)
3e43a32a
MS
15143 complaint (&symfile_complaints,
15144 _("ignoring absolute DW_AT_sibling"));
c906108c 15145 else
b9502d3f
WN
15146 {
15147 unsigned int off = dwarf2_get_ref_die_offset (&attr).sect_off;
15148 const gdb_byte *sibling_ptr = buffer + off;
15149
15150 if (sibling_ptr < info_ptr)
15151 complaint (&symfile_complaints,
15152 _("DW_AT_sibling points backwards"));
15153 else
15154 part_die->sibling = sibling_ptr;
15155 }
c906108c 15156 break;
fa4028e9
JB
15157 case DW_AT_byte_size:
15158 part_die->has_byte_size = 1;
15159 break;
68511cec
CES
15160 case DW_AT_calling_convention:
15161 /* DWARF doesn't provide a way to identify a program's source-level
15162 entry point. DW_AT_calling_convention attributes are only meant
15163 to describe functions' calling conventions.
15164
15165 However, because it's a necessary piece of information in
15166 Fortran, and because DW_CC_program is the only piece of debugging
15167 information whose definition refers to a 'main program' at all,
15168 several compilers have begun marking Fortran main programs with
15169 DW_CC_program --- even when those functions use the standard
15170 calling conventions.
15171
15172 So until DWARF specifies a way to provide this information and
15173 compilers pick up the new representation, we'll support this
15174 practice. */
15175 if (DW_UNSND (&attr) == DW_CC_program
15176 && cu->language == language_fortran)
01f8c46d
JK
15177 {
15178 set_main_name (part_die->name);
15179
15180 /* As this DIE has a static linkage the name would be difficult
15181 to look up later. */
15182 language_of_main = language_fortran;
15183 }
68511cec 15184 break;
481860b3
GB
15185 case DW_AT_inline:
15186 if (DW_UNSND (&attr) == DW_INL_inlined
15187 || DW_UNSND (&attr) == DW_INL_declared_inlined)
15188 part_die->may_be_inlined = 1;
15189 break;
95554aad
TT
15190
15191 case DW_AT_import:
15192 if (part_die->tag == DW_TAG_imported_unit)
36586728
TT
15193 {
15194 part_die->d.offset = dwarf2_get_ref_die_offset (&attr);
15195 part_die->is_dwz = (attr.form == DW_FORM_GNU_ref_alt
15196 || cu->per_cu->is_dwz);
15197 }
95554aad
TT
15198 break;
15199
c906108c
SS
15200 default:
15201 break;
15202 }
15203 }
15204
91da1414
MW
15205 if (high_pc_relative)
15206 part_die->highpc += part_die->lowpc;
15207
9373cf26
JK
15208 if (has_low_pc_attr && has_high_pc_attr)
15209 {
15210 /* When using the GNU linker, .gnu.linkonce. sections are used to
15211 eliminate duplicate copies of functions and vtables and such.
15212 The linker will arbitrarily choose one and discard the others.
15213 The AT_*_pc values for such functions refer to local labels in
15214 these sections. If the section from that file was discarded, the
15215 labels are not in the output, so the relocs get a value of 0.
15216 If this is a discarded function, mark the pc bounds as invalid,
15217 so that GDB will ignore it. */
15218 if (part_die->lowpc == 0 && !dwarf2_per_objfile->has_section_at_zero)
15219 {
bb5ed363 15220 struct gdbarch *gdbarch = get_objfile_arch (objfile);
9373cf26
JK
15221
15222 complaint (&symfile_complaints,
15223 _("DW_AT_low_pc %s is zero "
15224 "for DIE at 0x%x [in module %s]"),
15225 paddress (gdbarch, part_die->lowpc),
4262abfb 15226 part_die->offset.sect_off, objfile_name (objfile));
9373cf26
JK
15227 }
15228 /* dwarf2_get_pc_bounds has also the strict low < high requirement. */
15229 else if (part_die->lowpc >= part_die->highpc)
15230 {
bb5ed363 15231 struct gdbarch *gdbarch = get_objfile_arch (objfile);
9373cf26
JK
15232
15233 complaint (&symfile_complaints,
15234 _("DW_AT_low_pc %s is not < DW_AT_high_pc %s "
15235 "for DIE at 0x%x [in module %s]"),
15236 paddress (gdbarch, part_die->lowpc),
15237 paddress (gdbarch, part_die->highpc),
4262abfb 15238 part_die->offset.sect_off, objfile_name (objfile));
9373cf26
JK
15239 }
15240 else
15241 part_die->has_pc_info = 1;
15242 }
85cbf3d3 15243
c906108c
SS
15244 return info_ptr;
15245}
15246
72bf9492
DJ
15247/* Find a cached partial DIE at OFFSET in CU. */
15248
15249static struct partial_die_info *
b64f50a1 15250find_partial_die_in_comp_unit (sect_offset offset, struct dwarf2_cu *cu)
72bf9492
DJ
15251{
15252 struct partial_die_info *lookup_die = NULL;
15253 struct partial_die_info part_die;
15254
15255 part_die.offset = offset;
b64f50a1
JK
15256 lookup_die = htab_find_with_hash (cu->partial_dies, &part_die,
15257 offset.sect_off);
72bf9492 15258
72bf9492
DJ
15259 return lookup_die;
15260}
15261
348e048f
DE
15262/* Find a partial DIE at OFFSET, which may or may not be in CU,
15263 except in the case of .debug_types DIEs which do not reference
15264 outside their CU (they do however referencing other types via
55f1336d 15265 DW_FORM_ref_sig8). */
72bf9492
DJ
15266
15267static struct partial_die_info *
36586728 15268find_partial_die (sect_offset offset, int offset_in_dwz, struct dwarf2_cu *cu)
72bf9492 15269{
bb5ed363 15270 struct objfile *objfile = cu->objfile;
5afb4e99
DJ
15271 struct dwarf2_per_cu_data *per_cu = NULL;
15272 struct partial_die_info *pd = NULL;
72bf9492 15273
36586728
TT
15274 if (offset_in_dwz == cu->per_cu->is_dwz
15275 && offset_in_cu_p (&cu->header, offset))
5afb4e99
DJ
15276 {
15277 pd = find_partial_die_in_comp_unit (offset, cu);
15278 if (pd != NULL)
15279 return pd;
0d99eb77
DE
15280 /* We missed recording what we needed.
15281 Load all dies and try again. */
15282 per_cu = cu->per_cu;
5afb4e99 15283 }
0d99eb77
DE
15284 else
15285 {
15286 /* TUs don't reference other CUs/TUs (except via type signatures). */
3019eac3 15287 if (cu->per_cu->is_debug_types)
0d99eb77
DE
15288 {
15289 error (_("Dwarf Error: Type Unit at offset 0x%lx contains"
15290 " external reference to offset 0x%lx [in module %s].\n"),
15291 (long) cu->header.offset.sect_off, (long) offset.sect_off,
15292 bfd_get_filename (objfile->obfd));
15293 }
36586728
TT
15294 per_cu = dwarf2_find_containing_comp_unit (offset, offset_in_dwz,
15295 objfile);
72bf9492 15296
0d99eb77
DE
15297 if (per_cu->cu == NULL || per_cu->cu->partial_dies == NULL)
15298 load_partial_comp_unit (per_cu);
ae038cb0 15299
0d99eb77
DE
15300 per_cu->cu->last_used = 0;
15301 pd = find_partial_die_in_comp_unit (offset, per_cu->cu);
15302 }
5afb4e99 15303
dee91e82
DE
15304 /* If we didn't find it, and not all dies have been loaded,
15305 load them all and try again. */
15306
5afb4e99
DJ
15307 if (pd == NULL && per_cu->load_all_dies == 0)
15308 {
5afb4e99 15309 per_cu->load_all_dies = 1;
fd820528
DE
15310
15311 /* This is nasty. When we reread the DIEs, somewhere up the call chain
15312 THIS_CU->cu may already be in use. So we can't just free it and
15313 replace its DIEs with the ones we read in. Instead, we leave those
15314 DIEs alone (which can still be in use, e.g. in scan_partial_symbols),
15315 and clobber THIS_CU->cu->partial_dies with the hash table for the new
15316 set. */
dee91e82 15317 load_partial_comp_unit (per_cu);
5afb4e99
DJ
15318
15319 pd = find_partial_die_in_comp_unit (offset, per_cu->cu);
15320 }
15321
15322 if (pd == NULL)
15323 internal_error (__FILE__, __LINE__,
3e43a32a
MS
15324 _("could not find partial DIE 0x%x "
15325 "in cache [from module %s]\n"),
b64f50a1 15326 offset.sect_off, bfd_get_filename (objfile->obfd));
5afb4e99 15327 return pd;
72bf9492
DJ
15328}
15329
abc72ce4
DE
15330/* See if we can figure out if the class lives in a namespace. We do
15331 this by looking for a member function; its demangled name will
15332 contain namespace info, if there is any. */
15333
15334static void
15335guess_partial_die_structure_name (struct partial_die_info *struct_pdi,
15336 struct dwarf2_cu *cu)
15337{
15338 /* NOTE: carlton/2003-10-07: Getting the info this way changes
15339 what template types look like, because the demangler
15340 frequently doesn't give the same name as the debug info. We
15341 could fix this by only using the demangled name to get the
15342 prefix (but see comment in read_structure_type). */
15343
15344 struct partial_die_info *real_pdi;
15345 struct partial_die_info *child_pdi;
15346
15347 /* If this DIE (this DIE's specification, if any) has a parent, then
15348 we should not do this. We'll prepend the parent's fully qualified
15349 name when we create the partial symbol. */
15350
15351 real_pdi = struct_pdi;
15352 while (real_pdi->has_specification)
36586728
TT
15353 real_pdi = find_partial_die (real_pdi->spec_offset,
15354 real_pdi->spec_is_dwz, cu);
abc72ce4
DE
15355
15356 if (real_pdi->die_parent != NULL)
15357 return;
15358
15359 for (child_pdi = struct_pdi->die_child;
15360 child_pdi != NULL;
15361 child_pdi = child_pdi->die_sibling)
15362 {
15363 if (child_pdi->tag == DW_TAG_subprogram
15364 && child_pdi->linkage_name != NULL)
15365 {
15366 char *actual_class_name
15367 = language_class_name_from_physname (cu->language_defn,
15368 child_pdi->linkage_name);
15369 if (actual_class_name != NULL)
15370 {
15371 struct_pdi->name
10f0c4bb
TT
15372 = obstack_copy0 (&cu->objfile->objfile_obstack,
15373 actual_class_name,
15374 strlen (actual_class_name));
abc72ce4
DE
15375 xfree (actual_class_name);
15376 }
15377 break;
15378 }
15379 }
15380}
15381
72bf9492
DJ
15382/* Adjust PART_DIE before generating a symbol for it. This function
15383 may set the is_external flag or change the DIE's name. */
15384
15385static void
15386fixup_partial_die (struct partial_die_info *part_die,
15387 struct dwarf2_cu *cu)
15388{
abc72ce4
DE
15389 /* Once we've fixed up a die, there's no point in doing so again.
15390 This also avoids a memory leak if we were to call
15391 guess_partial_die_structure_name multiple times. */
15392 if (part_die->fixup_called)
15393 return;
15394
72bf9492
DJ
15395 /* If we found a reference attribute and the DIE has no name, try
15396 to find a name in the referred to DIE. */
15397
15398 if (part_die->name == NULL && part_die->has_specification)
15399 {
15400 struct partial_die_info *spec_die;
72bf9492 15401
36586728
TT
15402 spec_die = find_partial_die (part_die->spec_offset,
15403 part_die->spec_is_dwz, cu);
72bf9492 15404
10b3939b 15405 fixup_partial_die (spec_die, cu);
72bf9492
DJ
15406
15407 if (spec_die->name)
15408 {
15409 part_die->name = spec_die->name;
15410
15411 /* Copy DW_AT_external attribute if it is set. */
15412 if (spec_die->is_external)
15413 part_die->is_external = spec_die->is_external;
15414 }
15415 }
15416
15417 /* Set default names for some unnamed DIEs. */
72bf9492
DJ
15418
15419 if (part_die->name == NULL && part_die->tag == DW_TAG_namespace)
2b1dbab0 15420 part_die->name = CP_ANONYMOUS_NAMESPACE_STR;
72bf9492 15421
abc72ce4
DE
15422 /* If there is no parent die to provide a namespace, and there are
15423 children, see if we can determine the namespace from their linkage
122d1940 15424 name. */
abc72ce4 15425 if (cu->language == language_cplus
8b70b953 15426 && !VEC_empty (dwarf2_section_info_def, dwarf2_per_objfile->types)
abc72ce4
DE
15427 && part_die->die_parent == NULL
15428 && part_die->has_children
15429 && (part_die->tag == DW_TAG_class_type
15430 || part_die->tag == DW_TAG_structure_type
15431 || part_die->tag == DW_TAG_union_type))
15432 guess_partial_die_structure_name (part_die, cu);
15433
53832f31
TT
15434 /* GCC might emit a nameless struct or union that has a linkage
15435 name. See http://gcc.gnu.org/bugzilla/show_bug.cgi?id=47510. */
15436 if (part_die->name == NULL
96408a79
SA
15437 && (part_die->tag == DW_TAG_class_type
15438 || part_die->tag == DW_TAG_interface_type
15439 || part_die->tag == DW_TAG_structure_type
15440 || part_die->tag == DW_TAG_union_type)
53832f31
TT
15441 && part_die->linkage_name != NULL)
15442 {
15443 char *demangled;
15444
8de20a37 15445 demangled = gdb_demangle (part_die->linkage_name, DMGL_TYPES);
53832f31
TT
15446 if (demangled)
15447 {
96408a79
SA
15448 const char *base;
15449
15450 /* Strip any leading namespaces/classes, keep only the base name.
15451 DW_AT_name for named DIEs does not contain the prefixes. */
15452 base = strrchr (demangled, ':');
15453 if (base && base > demangled && base[-1] == ':')
15454 base++;
15455 else
15456 base = demangled;
15457
10f0c4bb
TT
15458 part_die->name = obstack_copy0 (&cu->objfile->objfile_obstack,
15459 base, strlen (base));
53832f31
TT
15460 xfree (demangled);
15461 }
15462 }
15463
abc72ce4 15464 part_die->fixup_called = 1;
72bf9492
DJ
15465}
15466
a8329558 15467/* Read an attribute value described by an attribute form. */
c906108c 15468
d521ce57 15469static const gdb_byte *
dee91e82
DE
15470read_attribute_value (const struct die_reader_specs *reader,
15471 struct attribute *attr, unsigned form,
d521ce57 15472 const gdb_byte *info_ptr)
c906108c 15473{
dee91e82
DE
15474 struct dwarf2_cu *cu = reader->cu;
15475 bfd *abfd = reader->abfd;
e7c27a73 15476 struct comp_unit_head *cu_header = &cu->header;
c906108c
SS
15477 unsigned int bytes_read;
15478 struct dwarf_block *blk;
15479
a8329558
KW
15480 attr->form = form;
15481 switch (form)
c906108c 15482 {
c906108c 15483 case DW_FORM_ref_addr:
ae411497 15484 if (cu->header.version == 2)
4568ecf9 15485 DW_UNSND (attr) = read_address (abfd, info_ptr, cu, &bytes_read);
ae411497 15486 else
4568ecf9
DE
15487 DW_UNSND (attr) = read_offset (abfd, info_ptr,
15488 &cu->header, &bytes_read);
ae411497
TT
15489 info_ptr += bytes_read;
15490 break;
36586728
TT
15491 case DW_FORM_GNU_ref_alt:
15492 DW_UNSND (attr) = read_offset (abfd, info_ptr, &cu->header, &bytes_read);
15493 info_ptr += bytes_read;
15494 break;
ae411497 15495 case DW_FORM_addr:
e7c27a73 15496 DW_ADDR (attr) = read_address (abfd, info_ptr, cu, &bytes_read);
107d2387 15497 info_ptr += bytes_read;
c906108c
SS
15498 break;
15499 case DW_FORM_block2:
7b5a2f43 15500 blk = dwarf_alloc_block (cu);
c906108c
SS
15501 blk->size = read_2_bytes (abfd, info_ptr);
15502 info_ptr += 2;
15503 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
15504 info_ptr += blk->size;
15505 DW_BLOCK (attr) = blk;
15506 break;
15507 case DW_FORM_block4:
7b5a2f43 15508 blk = dwarf_alloc_block (cu);
c906108c
SS
15509 blk->size = read_4_bytes (abfd, info_ptr);
15510 info_ptr += 4;
15511 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
15512 info_ptr += blk->size;
15513 DW_BLOCK (attr) = blk;
15514 break;
15515 case DW_FORM_data2:
15516 DW_UNSND (attr) = read_2_bytes (abfd, info_ptr);
15517 info_ptr += 2;
15518 break;
15519 case DW_FORM_data4:
15520 DW_UNSND (attr) = read_4_bytes (abfd, info_ptr);
15521 info_ptr += 4;
15522 break;
15523 case DW_FORM_data8:
15524 DW_UNSND (attr) = read_8_bytes (abfd, info_ptr);
15525 info_ptr += 8;
15526 break;
2dc7f7b3
TT
15527 case DW_FORM_sec_offset:
15528 DW_UNSND (attr) = read_offset (abfd, info_ptr, &cu->header, &bytes_read);
15529 info_ptr += bytes_read;
15530 break;
c906108c 15531 case DW_FORM_string:
9b1c24c8 15532 DW_STRING (attr) = read_direct_string (abfd, info_ptr, &bytes_read);
8285870a 15533 DW_STRING_IS_CANONICAL (attr) = 0;
c906108c
SS
15534 info_ptr += bytes_read;
15535 break;
4bdf3d34 15536 case DW_FORM_strp:
36586728
TT
15537 if (!cu->per_cu->is_dwz)
15538 {
15539 DW_STRING (attr) = read_indirect_string (abfd, info_ptr, cu_header,
15540 &bytes_read);
15541 DW_STRING_IS_CANONICAL (attr) = 0;
15542 info_ptr += bytes_read;
15543 break;
15544 }
15545 /* FALLTHROUGH */
15546 case DW_FORM_GNU_strp_alt:
15547 {
15548 struct dwz_file *dwz = dwarf2_get_dwz_file ();
15549 LONGEST str_offset = read_offset (abfd, info_ptr, cu_header,
15550 &bytes_read);
15551
15552 DW_STRING (attr) = read_indirect_string_from_dwz (dwz, str_offset);
15553 DW_STRING_IS_CANONICAL (attr) = 0;
15554 info_ptr += bytes_read;
15555 }
4bdf3d34 15556 break;
2dc7f7b3 15557 case DW_FORM_exprloc:
c906108c 15558 case DW_FORM_block:
7b5a2f43 15559 blk = dwarf_alloc_block (cu);
c906108c
SS
15560 blk->size = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
15561 info_ptr += bytes_read;
15562 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
15563 info_ptr += blk->size;
15564 DW_BLOCK (attr) = blk;
15565 break;
15566 case DW_FORM_block1:
7b5a2f43 15567 blk = dwarf_alloc_block (cu);
c906108c
SS
15568 blk->size = read_1_byte (abfd, info_ptr);
15569 info_ptr += 1;
15570 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
15571 info_ptr += blk->size;
15572 DW_BLOCK (attr) = blk;
15573 break;
15574 case DW_FORM_data1:
15575 DW_UNSND (attr) = read_1_byte (abfd, info_ptr);
15576 info_ptr += 1;
15577 break;
15578 case DW_FORM_flag:
15579 DW_UNSND (attr) = read_1_byte (abfd, info_ptr);
15580 info_ptr += 1;
15581 break;
2dc7f7b3
TT
15582 case DW_FORM_flag_present:
15583 DW_UNSND (attr) = 1;
15584 break;
c906108c
SS
15585 case DW_FORM_sdata:
15586 DW_SND (attr) = read_signed_leb128 (abfd, info_ptr, &bytes_read);
15587 info_ptr += bytes_read;
15588 break;
15589 case DW_FORM_udata:
15590 DW_UNSND (attr) = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
15591 info_ptr += bytes_read;
15592 break;
15593 case DW_FORM_ref1:
4568ecf9
DE
15594 DW_UNSND (attr) = (cu->header.offset.sect_off
15595 + read_1_byte (abfd, info_ptr));
c906108c
SS
15596 info_ptr += 1;
15597 break;
15598 case DW_FORM_ref2:
4568ecf9
DE
15599 DW_UNSND (attr) = (cu->header.offset.sect_off
15600 + read_2_bytes (abfd, info_ptr));
c906108c
SS
15601 info_ptr += 2;
15602 break;
15603 case DW_FORM_ref4:
4568ecf9
DE
15604 DW_UNSND (attr) = (cu->header.offset.sect_off
15605 + read_4_bytes (abfd, info_ptr));
c906108c
SS
15606 info_ptr += 4;
15607 break;
613e1657 15608 case DW_FORM_ref8:
4568ecf9
DE
15609 DW_UNSND (attr) = (cu->header.offset.sect_off
15610 + read_8_bytes (abfd, info_ptr));
613e1657
KB
15611 info_ptr += 8;
15612 break;
55f1336d 15613 case DW_FORM_ref_sig8:
ac9ec31b 15614 DW_SIGNATURE (attr) = read_8_bytes (abfd, info_ptr);
348e048f
DE
15615 info_ptr += 8;
15616 break;
c906108c 15617 case DW_FORM_ref_udata:
4568ecf9
DE
15618 DW_UNSND (attr) = (cu->header.offset.sect_off
15619 + read_unsigned_leb128 (abfd, info_ptr, &bytes_read));
c906108c
SS
15620 info_ptr += bytes_read;
15621 break;
c906108c 15622 case DW_FORM_indirect:
a8329558
KW
15623 form = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
15624 info_ptr += bytes_read;
dee91e82 15625 info_ptr = read_attribute_value (reader, attr, form, info_ptr);
a8329558 15626 break;
3019eac3
DE
15627 case DW_FORM_GNU_addr_index:
15628 if (reader->dwo_file == NULL)
15629 {
15630 /* For now flag a hard error.
15631 Later we can turn this into a complaint. */
15632 error (_("Dwarf Error: %s found in non-DWO CU [in module %s]"),
15633 dwarf_form_name (form),
15634 bfd_get_filename (abfd));
15635 }
15636 DW_ADDR (attr) = read_addr_index_from_leb128 (cu, info_ptr, &bytes_read);
15637 info_ptr += bytes_read;
15638 break;
15639 case DW_FORM_GNU_str_index:
15640 if (reader->dwo_file == NULL)
15641 {
15642 /* For now flag a hard error.
15643 Later we can turn this into a complaint if warranted. */
15644 error (_("Dwarf Error: %s found in non-DWO CU [in module %s]"),
15645 dwarf_form_name (form),
15646 bfd_get_filename (abfd));
15647 }
15648 {
15649 ULONGEST str_index =
15650 read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
15651
15652 DW_STRING (attr) = read_str_index (reader, cu, str_index);
15653 DW_STRING_IS_CANONICAL (attr) = 0;
15654 info_ptr += bytes_read;
15655 }
15656 break;
c906108c 15657 default:
8a3fe4f8 15658 error (_("Dwarf Error: Cannot handle %s in DWARF reader [in module %s]"),
659b0389
ML
15659 dwarf_form_name (form),
15660 bfd_get_filename (abfd));
c906108c 15661 }
28e94949 15662
36586728 15663 /* Super hack. */
7771576e 15664 if (cu->per_cu->is_dwz && attr_form_is_ref (attr))
36586728
TT
15665 attr->form = DW_FORM_GNU_ref_alt;
15666
28e94949
JB
15667 /* We have seen instances where the compiler tried to emit a byte
15668 size attribute of -1 which ended up being encoded as an unsigned
15669 0xffffffff. Although 0xffffffff is technically a valid size value,
15670 an object of this size seems pretty unlikely so we can relatively
15671 safely treat these cases as if the size attribute was invalid and
15672 treat them as zero by default. */
15673 if (attr->name == DW_AT_byte_size
15674 && form == DW_FORM_data4
15675 && DW_UNSND (attr) >= 0xffffffff)
01c66ae6
JB
15676 {
15677 complaint
15678 (&symfile_complaints,
43bbcdc2
PH
15679 _("Suspicious DW_AT_byte_size value treated as zero instead of %s"),
15680 hex_string (DW_UNSND (attr)));
01c66ae6
JB
15681 DW_UNSND (attr) = 0;
15682 }
28e94949 15683
c906108c
SS
15684 return info_ptr;
15685}
15686
a8329558
KW
15687/* Read an attribute described by an abbreviated attribute. */
15688
d521ce57 15689static const gdb_byte *
dee91e82
DE
15690read_attribute (const struct die_reader_specs *reader,
15691 struct attribute *attr, struct attr_abbrev *abbrev,
d521ce57 15692 const gdb_byte *info_ptr)
a8329558
KW
15693{
15694 attr->name = abbrev->name;
dee91e82 15695 return read_attribute_value (reader, attr, abbrev->form, info_ptr);
a8329558
KW
15696}
15697
0963b4bd 15698/* Read dwarf information from a buffer. */
c906108c
SS
15699
15700static unsigned int
a1855c1d 15701read_1_byte (bfd *abfd, const gdb_byte *buf)
c906108c 15702{
fe1b8b76 15703 return bfd_get_8 (abfd, buf);
c906108c
SS
15704}
15705
15706static int
a1855c1d 15707read_1_signed_byte (bfd *abfd, const gdb_byte *buf)
c906108c 15708{
fe1b8b76 15709 return bfd_get_signed_8 (abfd, buf);
c906108c
SS
15710}
15711
15712static unsigned int
a1855c1d 15713read_2_bytes (bfd *abfd, const gdb_byte *buf)
c906108c 15714{
fe1b8b76 15715 return bfd_get_16 (abfd, buf);
c906108c
SS
15716}
15717
21ae7a4d 15718static int
a1855c1d 15719read_2_signed_bytes (bfd *abfd, const gdb_byte *buf)
21ae7a4d
JK
15720{
15721 return bfd_get_signed_16 (abfd, buf);
15722}
15723
c906108c 15724static unsigned int
a1855c1d 15725read_4_bytes (bfd *abfd, const gdb_byte *buf)
c906108c 15726{
fe1b8b76 15727 return bfd_get_32 (abfd, buf);
c906108c
SS
15728}
15729
21ae7a4d 15730static int
a1855c1d 15731read_4_signed_bytes (bfd *abfd, const gdb_byte *buf)
21ae7a4d
JK
15732{
15733 return bfd_get_signed_32 (abfd, buf);
15734}
15735
93311388 15736static ULONGEST
a1855c1d 15737read_8_bytes (bfd *abfd, const gdb_byte *buf)
c906108c 15738{
fe1b8b76 15739 return bfd_get_64 (abfd, buf);
c906108c
SS
15740}
15741
15742static CORE_ADDR
d521ce57 15743read_address (bfd *abfd, const gdb_byte *buf, struct dwarf2_cu *cu,
891d2f0b 15744 unsigned int *bytes_read)
c906108c 15745{
e7c27a73 15746 struct comp_unit_head *cu_header = &cu->header;
c906108c
SS
15747 CORE_ADDR retval = 0;
15748
107d2387 15749 if (cu_header->signed_addr_p)
c906108c 15750 {
107d2387
AC
15751 switch (cu_header->addr_size)
15752 {
15753 case 2:
fe1b8b76 15754 retval = bfd_get_signed_16 (abfd, buf);
107d2387
AC
15755 break;
15756 case 4:
fe1b8b76 15757 retval = bfd_get_signed_32 (abfd, buf);
107d2387
AC
15758 break;
15759 case 8:
fe1b8b76 15760 retval = bfd_get_signed_64 (abfd, buf);
107d2387
AC
15761 break;
15762 default:
8e65ff28 15763 internal_error (__FILE__, __LINE__,
e2e0b3e5 15764 _("read_address: bad switch, signed [in module %s]"),
659b0389 15765 bfd_get_filename (abfd));
107d2387
AC
15766 }
15767 }
15768 else
15769 {
15770 switch (cu_header->addr_size)
15771 {
15772 case 2:
fe1b8b76 15773 retval = bfd_get_16 (abfd, buf);
107d2387
AC
15774 break;
15775 case 4:
fe1b8b76 15776 retval = bfd_get_32 (abfd, buf);
107d2387
AC
15777 break;
15778 case 8:
fe1b8b76 15779 retval = bfd_get_64 (abfd, buf);
107d2387
AC
15780 break;
15781 default:
8e65ff28 15782 internal_error (__FILE__, __LINE__,
a73c6dcd
MS
15783 _("read_address: bad switch, "
15784 "unsigned [in module %s]"),
659b0389 15785 bfd_get_filename (abfd));
107d2387 15786 }
c906108c 15787 }
64367e0a 15788
107d2387
AC
15789 *bytes_read = cu_header->addr_size;
15790 return retval;
c906108c
SS
15791}
15792
f7ef9339 15793/* Read the initial length from a section. The (draft) DWARF 3
613e1657
KB
15794 specification allows the initial length to take up either 4 bytes
15795 or 12 bytes. If the first 4 bytes are 0xffffffff, then the next 8
15796 bytes describe the length and all offsets will be 8 bytes in length
15797 instead of 4.
15798
f7ef9339
KB
15799 An older, non-standard 64-bit format is also handled by this
15800 function. The older format in question stores the initial length
15801 as an 8-byte quantity without an escape value. Lengths greater
15802 than 2^32 aren't very common which means that the initial 4 bytes
15803 is almost always zero. Since a length value of zero doesn't make
15804 sense for the 32-bit format, this initial zero can be considered to
15805 be an escape value which indicates the presence of the older 64-bit
15806 format. As written, the code can't detect (old format) lengths
917c78fc
MK
15807 greater than 4GB. If it becomes necessary to handle lengths
15808 somewhat larger than 4GB, we could allow other small values (such
15809 as the non-sensical values of 1, 2, and 3) to also be used as
15810 escape values indicating the presence of the old format.
f7ef9339 15811
917c78fc
MK
15812 The value returned via bytes_read should be used to increment the
15813 relevant pointer after calling read_initial_length().
c764a876 15814
613e1657
KB
15815 [ Note: read_initial_length() and read_offset() are based on the
15816 document entitled "DWARF Debugging Information Format", revision
f7ef9339 15817 3, draft 8, dated November 19, 2001. This document was obtained
613e1657
KB
15818 from:
15819
f7ef9339 15820 http://reality.sgiweb.org/davea/dwarf3-draft8-011125.pdf
6e70227d 15821
613e1657
KB
15822 This document is only a draft and is subject to change. (So beware.)
15823
f7ef9339 15824 Details regarding the older, non-standard 64-bit format were
917c78fc
MK
15825 determined empirically by examining 64-bit ELF files produced by
15826 the SGI toolchain on an IRIX 6.5 machine.
f7ef9339
KB
15827
15828 - Kevin, July 16, 2002
613e1657
KB
15829 ] */
15830
15831static LONGEST
d521ce57 15832read_initial_length (bfd *abfd, const gdb_byte *buf, unsigned int *bytes_read)
613e1657 15833{
fe1b8b76 15834 LONGEST length = bfd_get_32 (abfd, buf);
613e1657 15835
dd373385 15836 if (length == 0xffffffff)
613e1657 15837 {
fe1b8b76 15838 length = bfd_get_64 (abfd, buf + 4);
613e1657 15839 *bytes_read = 12;
613e1657 15840 }
dd373385 15841 else if (length == 0)
f7ef9339 15842 {
dd373385 15843 /* Handle the (non-standard) 64-bit DWARF2 format used by IRIX. */
fe1b8b76 15844 length = bfd_get_64 (abfd, buf);
f7ef9339 15845 *bytes_read = 8;
f7ef9339 15846 }
613e1657
KB
15847 else
15848 {
15849 *bytes_read = 4;
613e1657
KB
15850 }
15851
c764a876
DE
15852 return length;
15853}
dd373385 15854
c764a876
DE
15855/* Cover function for read_initial_length.
15856 Returns the length of the object at BUF, and stores the size of the
15857 initial length in *BYTES_READ and stores the size that offsets will be in
15858 *OFFSET_SIZE.
15859 If the initial length size is not equivalent to that specified in
15860 CU_HEADER then issue a complaint.
15861 This is useful when reading non-comp-unit headers. */
dd373385 15862
c764a876 15863static LONGEST
d521ce57 15864read_checked_initial_length_and_offset (bfd *abfd, const gdb_byte *buf,
c764a876
DE
15865 const struct comp_unit_head *cu_header,
15866 unsigned int *bytes_read,
15867 unsigned int *offset_size)
15868{
15869 LONGEST length = read_initial_length (abfd, buf, bytes_read);
15870
15871 gdb_assert (cu_header->initial_length_size == 4
15872 || cu_header->initial_length_size == 8
15873 || cu_header->initial_length_size == 12);
15874
15875 if (cu_header->initial_length_size != *bytes_read)
15876 complaint (&symfile_complaints,
15877 _("intermixed 32-bit and 64-bit DWARF sections"));
dd373385 15878
c764a876 15879 *offset_size = (*bytes_read == 4) ? 4 : 8;
dd373385 15880 return length;
613e1657
KB
15881}
15882
15883/* Read an offset from the data stream. The size of the offset is
917c78fc 15884 given by cu_header->offset_size. */
613e1657
KB
15885
15886static LONGEST
d521ce57
TT
15887read_offset (bfd *abfd, const gdb_byte *buf,
15888 const struct comp_unit_head *cu_header,
891d2f0b 15889 unsigned int *bytes_read)
c764a876
DE
15890{
15891 LONGEST offset = read_offset_1 (abfd, buf, cu_header->offset_size);
9a619af0 15892
c764a876
DE
15893 *bytes_read = cu_header->offset_size;
15894 return offset;
15895}
15896
15897/* Read an offset from the data stream. */
15898
15899static LONGEST
d521ce57 15900read_offset_1 (bfd *abfd, const gdb_byte *buf, unsigned int offset_size)
613e1657
KB
15901{
15902 LONGEST retval = 0;
15903
c764a876 15904 switch (offset_size)
613e1657
KB
15905 {
15906 case 4:
fe1b8b76 15907 retval = bfd_get_32 (abfd, buf);
613e1657
KB
15908 break;
15909 case 8:
fe1b8b76 15910 retval = bfd_get_64 (abfd, buf);
613e1657
KB
15911 break;
15912 default:
8e65ff28 15913 internal_error (__FILE__, __LINE__,
c764a876 15914 _("read_offset_1: bad switch [in module %s]"),
659b0389 15915 bfd_get_filename (abfd));
613e1657
KB
15916 }
15917
917c78fc 15918 return retval;
613e1657
KB
15919}
15920
d521ce57
TT
15921static const gdb_byte *
15922read_n_bytes (bfd *abfd, const gdb_byte *buf, unsigned int size)
c906108c
SS
15923{
15924 /* If the size of a host char is 8 bits, we can return a pointer
15925 to the buffer, otherwise we have to copy the data to a buffer
15926 allocated on the temporary obstack. */
4bdf3d34 15927 gdb_assert (HOST_CHAR_BIT == 8);
c906108c 15928 return buf;
c906108c
SS
15929}
15930
d521ce57
TT
15931static const char *
15932read_direct_string (bfd *abfd, const gdb_byte *buf,
15933 unsigned int *bytes_read_ptr)
c906108c
SS
15934{
15935 /* If the size of a host char is 8 bits, we can return a pointer
15936 to the string, otherwise we have to copy the string to a buffer
15937 allocated on the temporary obstack. */
4bdf3d34 15938 gdb_assert (HOST_CHAR_BIT == 8);
c906108c
SS
15939 if (*buf == '\0')
15940 {
15941 *bytes_read_ptr = 1;
15942 return NULL;
15943 }
d521ce57
TT
15944 *bytes_read_ptr = strlen ((const char *) buf) + 1;
15945 return (const char *) buf;
4bdf3d34
JJ
15946}
15947
d521ce57 15948static const char *
cf2c3c16 15949read_indirect_string_at_offset (bfd *abfd, LONGEST str_offset)
4bdf3d34 15950{
be391dca 15951 dwarf2_read_section (dwarf2_per_objfile->objfile, &dwarf2_per_objfile->str);
dce234bc 15952 if (dwarf2_per_objfile->str.buffer == NULL)
cf2c3c16
TT
15953 error (_("DW_FORM_strp used without .debug_str section [in module %s]"),
15954 bfd_get_filename (abfd));
dce234bc 15955 if (str_offset >= dwarf2_per_objfile->str.size)
cf2c3c16
TT
15956 error (_("DW_FORM_strp pointing outside of "
15957 ".debug_str section [in module %s]"),
15958 bfd_get_filename (abfd));
4bdf3d34 15959 gdb_assert (HOST_CHAR_BIT == 8);
dce234bc 15960 if (dwarf2_per_objfile->str.buffer[str_offset] == '\0')
4bdf3d34 15961 return NULL;
d521ce57 15962 return (const char *) (dwarf2_per_objfile->str.buffer + str_offset);
c906108c
SS
15963}
15964
36586728
TT
15965/* Read a string at offset STR_OFFSET in the .debug_str section from
15966 the .dwz file DWZ. Throw an error if the offset is too large. If
15967 the string consists of a single NUL byte, return NULL; otherwise
15968 return a pointer to the string. */
15969
d521ce57 15970static const char *
36586728
TT
15971read_indirect_string_from_dwz (struct dwz_file *dwz, LONGEST str_offset)
15972{
15973 dwarf2_read_section (dwarf2_per_objfile->objfile, &dwz->str);
15974
15975 if (dwz->str.buffer == NULL)
15976 error (_("DW_FORM_GNU_strp_alt used without .debug_str "
15977 "section [in module %s]"),
15978 bfd_get_filename (dwz->dwz_bfd));
15979 if (str_offset >= dwz->str.size)
15980 error (_("DW_FORM_GNU_strp_alt pointing outside of "
15981 ".debug_str section [in module %s]"),
15982 bfd_get_filename (dwz->dwz_bfd));
15983 gdb_assert (HOST_CHAR_BIT == 8);
15984 if (dwz->str.buffer[str_offset] == '\0')
15985 return NULL;
d521ce57 15986 return (const char *) (dwz->str.buffer + str_offset);
36586728
TT
15987}
15988
d521ce57
TT
15989static const char *
15990read_indirect_string (bfd *abfd, const gdb_byte *buf,
cf2c3c16
TT
15991 const struct comp_unit_head *cu_header,
15992 unsigned int *bytes_read_ptr)
15993{
15994 LONGEST str_offset = read_offset (abfd, buf, cu_header, bytes_read_ptr);
15995
15996 return read_indirect_string_at_offset (abfd, str_offset);
15997}
15998
12df843f 15999static ULONGEST
d521ce57
TT
16000read_unsigned_leb128 (bfd *abfd, const gdb_byte *buf,
16001 unsigned int *bytes_read_ptr)
c906108c 16002{
12df843f 16003 ULONGEST result;
ce5d95e1 16004 unsigned int num_read;
c906108c
SS
16005 int i, shift;
16006 unsigned char byte;
16007
16008 result = 0;
16009 shift = 0;
16010 num_read = 0;
16011 i = 0;
16012 while (1)
16013 {
fe1b8b76 16014 byte = bfd_get_8 (abfd, buf);
c906108c
SS
16015 buf++;
16016 num_read++;
12df843f 16017 result |= ((ULONGEST) (byte & 127) << shift);
c906108c
SS
16018 if ((byte & 128) == 0)
16019 {
16020 break;
16021 }
16022 shift += 7;
16023 }
16024 *bytes_read_ptr = num_read;
16025 return result;
16026}
16027
12df843f 16028static LONGEST
d521ce57
TT
16029read_signed_leb128 (bfd *abfd, const gdb_byte *buf,
16030 unsigned int *bytes_read_ptr)
c906108c 16031{
12df843f 16032 LONGEST result;
77e0b926 16033 int i, shift, num_read;
c906108c
SS
16034 unsigned char byte;
16035
16036 result = 0;
16037 shift = 0;
c906108c
SS
16038 num_read = 0;
16039 i = 0;
16040 while (1)
16041 {
fe1b8b76 16042 byte = bfd_get_8 (abfd, buf);
c906108c
SS
16043 buf++;
16044 num_read++;
12df843f 16045 result |= ((LONGEST) (byte & 127) << shift);
c906108c
SS
16046 shift += 7;
16047 if ((byte & 128) == 0)
16048 {
16049 break;
16050 }
16051 }
77e0b926 16052 if ((shift < 8 * sizeof (result)) && (byte & 0x40))
12df843f 16053 result |= -(((LONGEST) 1) << shift);
c906108c
SS
16054 *bytes_read_ptr = num_read;
16055 return result;
16056}
16057
3019eac3
DE
16058/* Given index ADDR_INDEX in .debug_addr, fetch the value.
16059 ADDR_BASE is the DW_AT_GNU_addr_base attribute or zero.
16060 ADDR_SIZE is the size of addresses from the CU header. */
16061
16062static CORE_ADDR
16063read_addr_index_1 (unsigned int addr_index, ULONGEST addr_base, int addr_size)
16064{
16065 struct objfile *objfile = dwarf2_per_objfile->objfile;
16066 bfd *abfd = objfile->obfd;
16067 const gdb_byte *info_ptr;
16068
16069 dwarf2_read_section (objfile, &dwarf2_per_objfile->addr);
16070 if (dwarf2_per_objfile->addr.buffer == NULL)
16071 error (_("DW_FORM_addr_index used without .debug_addr section [in module %s]"),
4262abfb 16072 objfile_name (objfile));
3019eac3
DE
16073 if (addr_base + addr_index * addr_size >= dwarf2_per_objfile->addr.size)
16074 error (_("DW_FORM_addr_index pointing outside of "
16075 ".debug_addr section [in module %s]"),
4262abfb 16076 objfile_name (objfile));
3019eac3
DE
16077 info_ptr = (dwarf2_per_objfile->addr.buffer
16078 + addr_base + addr_index * addr_size);
16079 if (addr_size == 4)
16080 return bfd_get_32 (abfd, info_ptr);
16081 else
16082 return bfd_get_64 (abfd, info_ptr);
16083}
16084
16085/* Given index ADDR_INDEX in .debug_addr, fetch the value. */
16086
16087static CORE_ADDR
16088read_addr_index (struct dwarf2_cu *cu, unsigned int addr_index)
16089{
16090 return read_addr_index_1 (addr_index, cu->addr_base, cu->header.addr_size);
16091}
16092
16093/* Given a pointer to an leb128 value, fetch the value from .debug_addr. */
16094
16095static CORE_ADDR
d521ce57 16096read_addr_index_from_leb128 (struct dwarf2_cu *cu, const gdb_byte *info_ptr,
3019eac3
DE
16097 unsigned int *bytes_read)
16098{
16099 bfd *abfd = cu->objfile->obfd;
16100 unsigned int addr_index = read_unsigned_leb128 (abfd, info_ptr, bytes_read);
16101
16102 return read_addr_index (cu, addr_index);
16103}
16104
16105/* Data structure to pass results from dwarf2_read_addr_index_reader
16106 back to dwarf2_read_addr_index. */
16107
16108struct dwarf2_read_addr_index_data
16109{
16110 ULONGEST addr_base;
16111 int addr_size;
16112};
16113
16114/* die_reader_func for dwarf2_read_addr_index. */
16115
16116static void
16117dwarf2_read_addr_index_reader (const struct die_reader_specs *reader,
d521ce57 16118 const gdb_byte *info_ptr,
3019eac3
DE
16119 struct die_info *comp_unit_die,
16120 int has_children,
16121 void *data)
16122{
16123 struct dwarf2_cu *cu = reader->cu;
16124 struct dwarf2_read_addr_index_data *aidata =
16125 (struct dwarf2_read_addr_index_data *) data;
16126
16127 aidata->addr_base = cu->addr_base;
16128 aidata->addr_size = cu->header.addr_size;
16129}
16130
16131/* Given an index in .debug_addr, fetch the value.
16132 NOTE: This can be called during dwarf expression evaluation,
16133 long after the debug information has been read, and thus per_cu->cu
16134 may no longer exist. */
16135
16136CORE_ADDR
16137dwarf2_read_addr_index (struct dwarf2_per_cu_data *per_cu,
16138 unsigned int addr_index)
16139{
16140 struct objfile *objfile = per_cu->objfile;
16141 struct dwarf2_cu *cu = per_cu->cu;
16142 ULONGEST addr_base;
16143 int addr_size;
16144
16145 /* This is intended to be called from outside this file. */
16146 dw2_setup (objfile);
16147
16148 /* We need addr_base and addr_size.
16149 If we don't have PER_CU->cu, we have to get it.
16150 Nasty, but the alternative is storing the needed info in PER_CU,
16151 which at this point doesn't seem justified: it's not clear how frequently
16152 it would get used and it would increase the size of every PER_CU.
16153 Entry points like dwarf2_per_cu_addr_size do a similar thing
16154 so we're not in uncharted territory here.
16155 Alas we need to be a bit more complicated as addr_base is contained
16156 in the DIE.
16157
16158 We don't need to read the entire CU(/TU).
16159 We just need the header and top level die.
a1b64ce1 16160
3019eac3 16161 IWBN to use the aging mechanism to let us lazily later discard the CU.
a1b64ce1 16162 For now we skip this optimization. */
3019eac3
DE
16163
16164 if (cu != NULL)
16165 {
16166 addr_base = cu->addr_base;
16167 addr_size = cu->header.addr_size;
16168 }
16169 else
16170 {
16171 struct dwarf2_read_addr_index_data aidata;
16172
a1b64ce1
DE
16173 /* Note: We can't use init_cutu_and_read_dies_simple here,
16174 we need addr_base. */
16175 init_cutu_and_read_dies (per_cu, NULL, 0, 0,
16176 dwarf2_read_addr_index_reader, &aidata);
3019eac3
DE
16177 addr_base = aidata.addr_base;
16178 addr_size = aidata.addr_size;
16179 }
16180
16181 return read_addr_index_1 (addr_index, addr_base, addr_size);
16182}
16183
57d63ce2
DE
16184/* Given a DW_FORM_GNU_str_index, fetch the string.
16185 This is only used by the Fission support. */
3019eac3 16186
d521ce57 16187static const char *
3019eac3
DE
16188read_str_index (const struct die_reader_specs *reader,
16189 struct dwarf2_cu *cu, ULONGEST str_index)
16190{
16191 struct objfile *objfile = dwarf2_per_objfile->objfile;
4262abfb 16192 const char *dwo_name = objfile_name (objfile);
3019eac3 16193 bfd *abfd = objfile->obfd;
73869dc2
DE
16194 struct dwarf2_section_info *str_section = &reader->dwo_file->sections.str;
16195 struct dwarf2_section_info *str_offsets_section =
16196 &reader->dwo_file->sections.str_offsets;
d521ce57 16197 const gdb_byte *info_ptr;
3019eac3 16198 ULONGEST str_offset;
57d63ce2 16199 static const char form_name[] = "DW_FORM_GNU_str_index";
3019eac3 16200
73869dc2
DE
16201 dwarf2_read_section (objfile, str_section);
16202 dwarf2_read_section (objfile, str_offsets_section);
16203 if (str_section->buffer == NULL)
57d63ce2 16204 error (_("%s used without .debug_str.dwo section"
3019eac3 16205 " in CU at offset 0x%lx [in module %s]"),
57d63ce2 16206 form_name, (long) cu->header.offset.sect_off, dwo_name);
73869dc2 16207 if (str_offsets_section->buffer == NULL)
57d63ce2 16208 error (_("%s used without .debug_str_offsets.dwo section"
3019eac3 16209 " in CU at offset 0x%lx [in module %s]"),
57d63ce2 16210 form_name, (long) cu->header.offset.sect_off, dwo_name);
73869dc2 16211 if (str_index * cu->header.offset_size >= str_offsets_section->size)
57d63ce2 16212 error (_("%s pointing outside of .debug_str_offsets.dwo"
3019eac3 16213 " section in CU at offset 0x%lx [in module %s]"),
57d63ce2 16214 form_name, (long) cu->header.offset.sect_off, dwo_name);
73869dc2 16215 info_ptr = (str_offsets_section->buffer
3019eac3
DE
16216 + str_index * cu->header.offset_size);
16217 if (cu->header.offset_size == 4)
16218 str_offset = bfd_get_32 (abfd, info_ptr);
16219 else
16220 str_offset = bfd_get_64 (abfd, info_ptr);
73869dc2 16221 if (str_offset >= str_section->size)
57d63ce2 16222 error (_("Offset from %s pointing outside of"
3019eac3 16223 " .debug_str.dwo section in CU at offset 0x%lx [in module %s]"),
57d63ce2 16224 form_name, (long) cu->header.offset.sect_off, dwo_name);
73869dc2 16225 return (const char *) (str_section->buffer + str_offset);
3019eac3
DE
16226}
16227
3019eac3
DE
16228/* Return the length of an LEB128 number in BUF. */
16229
16230static int
16231leb128_size (const gdb_byte *buf)
16232{
16233 const gdb_byte *begin = buf;
16234 gdb_byte byte;
16235
16236 while (1)
16237 {
16238 byte = *buf++;
16239 if ((byte & 128) == 0)
16240 return buf - begin;
16241 }
16242}
16243
c906108c 16244static void
e142c38c 16245set_cu_language (unsigned int lang, struct dwarf2_cu *cu)
c906108c
SS
16246{
16247 switch (lang)
16248 {
16249 case DW_LANG_C89:
76bee0cc 16250 case DW_LANG_C99:
c906108c 16251 case DW_LANG_C:
d1be3247 16252 case DW_LANG_UPC:
e142c38c 16253 cu->language = language_c;
c906108c
SS
16254 break;
16255 case DW_LANG_C_plus_plus:
e142c38c 16256 cu->language = language_cplus;
c906108c 16257 break;
6aecb9c2
JB
16258 case DW_LANG_D:
16259 cu->language = language_d;
16260 break;
c906108c
SS
16261 case DW_LANG_Fortran77:
16262 case DW_LANG_Fortran90:
b21b22e0 16263 case DW_LANG_Fortran95:
e142c38c 16264 cu->language = language_fortran;
c906108c 16265 break;
a766d390
DE
16266 case DW_LANG_Go:
16267 cu->language = language_go;
16268 break;
c906108c 16269 case DW_LANG_Mips_Assembler:
e142c38c 16270 cu->language = language_asm;
c906108c 16271 break;
bebd888e 16272 case DW_LANG_Java:
e142c38c 16273 cu->language = language_java;
bebd888e 16274 break;
c906108c 16275 case DW_LANG_Ada83:
8aaf0b47 16276 case DW_LANG_Ada95:
bc5f45f8
JB
16277 cu->language = language_ada;
16278 break;
72019c9c
GM
16279 case DW_LANG_Modula2:
16280 cu->language = language_m2;
16281 break;
fe8e67fd
PM
16282 case DW_LANG_Pascal83:
16283 cu->language = language_pascal;
16284 break;
22566fbd
DJ
16285 case DW_LANG_ObjC:
16286 cu->language = language_objc;
16287 break;
c906108c
SS
16288 case DW_LANG_Cobol74:
16289 case DW_LANG_Cobol85:
c906108c 16290 default:
e142c38c 16291 cu->language = language_minimal;
c906108c
SS
16292 break;
16293 }
e142c38c 16294 cu->language_defn = language_def (cu->language);
c906108c
SS
16295}
16296
16297/* Return the named attribute or NULL if not there. */
16298
16299static struct attribute *
e142c38c 16300dwarf2_attr (struct die_info *die, unsigned int name, struct dwarf2_cu *cu)
c906108c 16301{
a48e046c 16302 for (;;)
c906108c 16303 {
a48e046c
TT
16304 unsigned int i;
16305 struct attribute *spec = NULL;
16306
16307 for (i = 0; i < die->num_attrs; ++i)
16308 {
16309 if (die->attrs[i].name == name)
16310 return &die->attrs[i];
16311 if (die->attrs[i].name == DW_AT_specification
16312 || die->attrs[i].name == DW_AT_abstract_origin)
16313 spec = &die->attrs[i];
16314 }
16315
16316 if (!spec)
16317 break;
c906108c 16318
f2f0e013 16319 die = follow_die_ref (die, spec, &cu);
f2f0e013 16320 }
c5aa993b 16321
c906108c
SS
16322 return NULL;
16323}
16324
348e048f
DE
16325/* Return the named attribute or NULL if not there,
16326 but do not follow DW_AT_specification, etc.
16327 This is for use in contexts where we're reading .debug_types dies.
16328 Following DW_AT_specification, DW_AT_abstract_origin will take us
16329 back up the chain, and we want to go down. */
16330
16331static struct attribute *
45e58e77 16332dwarf2_attr_no_follow (struct die_info *die, unsigned int name)
348e048f
DE
16333{
16334 unsigned int i;
16335
16336 for (i = 0; i < die->num_attrs; ++i)
16337 if (die->attrs[i].name == name)
16338 return &die->attrs[i];
16339
16340 return NULL;
16341}
16342
05cf31d1
JB
16343/* Return non-zero iff the attribute NAME is defined for the given DIE,
16344 and holds a non-zero value. This function should only be used for
2dc7f7b3 16345 DW_FORM_flag or DW_FORM_flag_present attributes. */
05cf31d1
JB
16346
16347static int
16348dwarf2_flag_true_p (struct die_info *die, unsigned name, struct dwarf2_cu *cu)
16349{
16350 struct attribute *attr = dwarf2_attr (die, name, cu);
16351
16352 return (attr && DW_UNSND (attr));
16353}
16354
3ca72b44 16355static int
e142c38c 16356die_is_declaration (struct die_info *die, struct dwarf2_cu *cu)
3ca72b44 16357{
05cf31d1
JB
16358 /* A DIE is a declaration if it has a DW_AT_declaration attribute
16359 which value is non-zero. However, we have to be careful with
16360 DIEs having a DW_AT_specification attribute, because dwarf2_attr()
16361 (via dwarf2_flag_true_p) follows this attribute. So we may
16362 end up accidently finding a declaration attribute that belongs
16363 to a different DIE referenced by the specification attribute,
16364 even though the given DIE does not have a declaration attribute. */
16365 return (dwarf2_flag_true_p (die, DW_AT_declaration, cu)
16366 && dwarf2_attr (die, DW_AT_specification, cu) == NULL);
3ca72b44
AC
16367}
16368
63d06c5c 16369/* Return the die giving the specification for DIE, if there is
f2f0e013 16370 one. *SPEC_CU is the CU containing DIE on input, and the CU
edb3359d
DJ
16371 containing the return value on output. If there is no
16372 specification, but there is an abstract origin, that is
16373 returned. */
63d06c5c
DC
16374
16375static struct die_info *
f2f0e013 16376die_specification (struct die_info *die, struct dwarf2_cu **spec_cu)
63d06c5c 16377{
f2f0e013
DJ
16378 struct attribute *spec_attr = dwarf2_attr (die, DW_AT_specification,
16379 *spec_cu);
63d06c5c 16380
edb3359d
DJ
16381 if (spec_attr == NULL)
16382 spec_attr = dwarf2_attr (die, DW_AT_abstract_origin, *spec_cu);
16383
63d06c5c
DC
16384 if (spec_attr == NULL)
16385 return NULL;
16386 else
f2f0e013 16387 return follow_die_ref (die, spec_attr, spec_cu);
63d06c5c 16388}
c906108c 16389
debd256d 16390/* Free the line_header structure *LH, and any arrays and strings it
ae2de4f8
DE
16391 refers to.
16392 NOTE: This is also used as a "cleanup" function. */
16393
debd256d
JB
16394static void
16395free_line_header (struct line_header *lh)
16396{
16397 if (lh->standard_opcode_lengths)
a8bc7b56 16398 xfree (lh->standard_opcode_lengths);
debd256d
JB
16399
16400 /* Remember that all the lh->file_names[i].name pointers are
16401 pointers into debug_line_buffer, and don't need to be freed. */
16402 if (lh->file_names)
a8bc7b56 16403 xfree (lh->file_names);
debd256d
JB
16404
16405 /* Similarly for the include directory names. */
16406 if (lh->include_dirs)
a8bc7b56 16407 xfree (lh->include_dirs);
debd256d 16408
a8bc7b56 16409 xfree (lh);
debd256d
JB
16410}
16411
debd256d 16412/* Add an entry to LH's include directory table. */
ae2de4f8 16413
debd256d 16414static void
d521ce57 16415add_include_dir (struct line_header *lh, const char *include_dir)
c906108c 16416{
debd256d
JB
16417 /* Grow the array if necessary. */
16418 if (lh->include_dirs_size == 0)
c5aa993b 16419 {
debd256d
JB
16420 lh->include_dirs_size = 1; /* for testing */
16421 lh->include_dirs = xmalloc (lh->include_dirs_size
16422 * sizeof (*lh->include_dirs));
16423 }
16424 else if (lh->num_include_dirs >= lh->include_dirs_size)
16425 {
16426 lh->include_dirs_size *= 2;
16427 lh->include_dirs = xrealloc (lh->include_dirs,
16428 (lh->include_dirs_size
16429 * sizeof (*lh->include_dirs)));
c5aa993b 16430 }
c906108c 16431
debd256d
JB
16432 lh->include_dirs[lh->num_include_dirs++] = include_dir;
16433}
6e70227d 16434
debd256d 16435/* Add an entry to LH's file name table. */
ae2de4f8 16436
debd256d
JB
16437static void
16438add_file_name (struct line_header *lh,
d521ce57 16439 const char *name,
debd256d
JB
16440 unsigned int dir_index,
16441 unsigned int mod_time,
16442 unsigned int length)
16443{
16444 struct file_entry *fe;
16445
16446 /* Grow the array if necessary. */
16447 if (lh->file_names_size == 0)
16448 {
16449 lh->file_names_size = 1; /* for testing */
16450 lh->file_names = xmalloc (lh->file_names_size
16451 * sizeof (*lh->file_names));
16452 }
16453 else if (lh->num_file_names >= lh->file_names_size)
16454 {
16455 lh->file_names_size *= 2;
16456 lh->file_names = xrealloc (lh->file_names,
16457 (lh->file_names_size
16458 * sizeof (*lh->file_names)));
16459 }
16460
16461 fe = &lh->file_names[lh->num_file_names++];
16462 fe->name = name;
16463 fe->dir_index = dir_index;
16464 fe->mod_time = mod_time;
16465 fe->length = length;
aaa75496 16466 fe->included_p = 0;
cb1df416 16467 fe->symtab = NULL;
debd256d 16468}
6e70227d 16469
36586728
TT
16470/* A convenience function to find the proper .debug_line section for a
16471 CU. */
16472
16473static struct dwarf2_section_info *
16474get_debug_line_section (struct dwarf2_cu *cu)
16475{
16476 struct dwarf2_section_info *section;
16477
16478 /* For TUs in DWO files, the DW_AT_stmt_list attribute lives in the
16479 DWO file. */
16480 if (cu->dwo_unit && cu->per_cu->is_debug_types)
16481 section = &cu->dwo_unit->dwo_file->sections.line;
16482 else if (cu->per_cu->is_dwz)
16483 {
16484 struct dwz_file *dwz = dwarf2_get_dwz_file ();
16485
16486 section = &dwz->line;
16487 }
16488 else
16489 section = &dwarf2_per_objfile->line;
16490
16491 return section;
16492}
16493
debd256d 16494/* Read the statement program header starting at OFFSET in
3019eac3 16495 .debug_line, or .debug_line.dwo. Return a pointer
6502dd73 16496 to a struct line_header, allocated using xmalloc.
debd256d
JB
16497
16498 NOTE: the strings in the include directory and file name tables of
3019eac3
DE
16499 the returned object point into the dwarf line section buffer,
16500 and must not be freed. */
ae2de4f8 16501
debd256d 16502static struct line_header *
3019eac3 16503dwarf_decode_line_header (unsigned int offset, struct dwarf2_cu *cu)
debd256d
JB
16504{
16505 struct cleanup *back_to;
16506 struct line_header *lh;
d521ce57 16507 const gdb_byte *line_ptr;
c764a876 16508 unsigned int bytes_read, offset_size;
debd256d 16509 int i;
d521ce57 16510 const char *cur_dir, *cur_file;
3019eac3
DE
16511 struct dwarf2_section_info *section;
16512 bfd *abfd;
16513
36586728 16514 section = get_debug_line_section (cu);
3019eac3
DE
16515 dwarf2_read_section (dwarf2_per_objfile->objfile, section);
16516 if (section->buffer == NULL)
debd256d 16517 {
3019eac3
DE
16518 if (cu->dwo_unit && cu->per_cu->is_debug_types)
16519 complaint (&symfile_complaints, _("missing .debug_line.dwo section"));
16520 else
16521 complaint (&symfile_complaints, _("missing .debug_line section"));
debd256d
JB
16522 return 0;
16523 }
16524
fceca515
DE
16525 /* We can't do this until we know the section is non-empty.
16526 Only then do we know we have such a section. */
a32a8923 16527 abfd = get_section_bfd_owner (section);
fceca515 16528
a738430d
MK
16529 /* Make sure that at least there's room for the total_length field.
16530 That could be 12 bytes long, but we're just going to fudge that. */
3019eac3 16531 if (offset + 4 >= section->size)
debd256d 16532 {
4d3c2250 16533 dwarf2_statement_list_fits_in_line_number_section_complaint ();
debd256d
JB
16534 return 0;
16535 }
16536
16537 lh = xmalloc (sizeof (*lh));
16538 memset (lh, 0, sizeof (*lh));
16539 back_to = make_cleanup ((make_cleanup_ftype *) free_line_header,
16540 (void *) lh);
16541
3019eac3 16542 line_ptr = section->buffer + offset;
debd256d 16543
a738430d 16544 /* Read in the header. */
6e70227d 16545 lh->total_length =
c764a876
DE
16546 read_checked_initial_length_and_offset (abfd, line_ptr, &cu->header,
16547 &bytes_read, &offset_size);
debd256d 16548 line_ptr += bytes_read;
3019eac3 16549 if (line_ptr + lh->total_length > (section->buffer + section->size))
debd256d 16550 {
4d3c2250 16551 dwarf2_statement_list_fits_in_line_number_section_complaint ();
2f324bf6 16552 do_cleanups (back_to);
debd256d
JB
16553 return 0;
16554 }
16555 lh->statement_program_end = line_ptr + lh->total_length;
16556 lh->version = read_2_bytes (abfd, line_ptr);
16557 line_ptr += 2;
c764a876
DE
16558 lh->header_length = read_offset_1 (abfd, line_ptr, offset_size);
16559 line_ptr += offset_size;
debd256d
JB
16560 lh->minimum_instruction_length = read_1_byte (abfd, line_ptr);
16561 line_ptr += 1;
2dc7f7b3
TT
16562 if (lh->version >= 4)
16563 {
16564 lh->maximum_ops_per_instruction = read_1_byte (abfd, line_ptr);
16565 line_ptr += 1;
16566 }
16567 else
16568 lh->maximum_ops_per_instruction = 1;
16569
16570 if (lh->maximum_ops_per_instruction == 0)
16571 {
16572 lh->maximum_ops_per_instruction = 1;
16573 complaint (&symfile_complaints,
3e43a32a
MS
16574 _("invalid maximum_ops_per_instruction "
16575 "in `.debug_line' section"));
2dc7f7b3
TT
16576 }
16577
debd256d
JB
16578 lh->default_is_stmt = read_1_byte (abfd, line_ptr);
16579 line_ptr += 1;
16580 lh->line_base = read_1_signed_byte (abfd, line_ptr);
16581 line_ptr += 1;
16582 lh->line_range = read_1_byte (abfd, line_ptr);
16583 line_ptr += 1;
16584 lh->opcode_base = read_1_byte (abfd, line_ptr);
16585 line_ptr += 1;
16586 lh->standard_opcode_lengths
fe1b8b76 16587 = xmalloc (lh->opcode_base * sizeof (lh->standard_opcode_lengths[0]));
debd256d
JB
16588
16589 lh->standard_opcode_lengths[0] = 1; /* This should never be used anyway. */
16590 for (i = 1; i < lh->opcode_base; ++i)
16591 {
16592 lh->standard_opcode_lengths[i] = read_1_byte (abfd, line_ptr);
16593 line_ptr += 1;
16594 }
16595
a738430d 16596 /* Read directory table. */
9b1c24c8 16597 while ((cur_dir = read_direct_string (abfd, line_ptr, &bytes_read)) != NULL)
debd256d
JB
16598 {
16599 line_ptr += bytes_read;
16600 add_include_dir (lh, cur_dir);
16601 }
16602 line_ptr += bytes_read;
16603
a738430d 16604 /* Read file name table. */
9b1c24c8 16605 while ((cur_file = read_direct_string (abfd, line_ptr, &bytes_read)) != NULL)
debd256d
JB
16606 {
16607 unsigned int dir_index, mod_time, length;
16608
16609 line_ptr += bytes_read;
16610 dir_index = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
16611 line_ptr += bytes_read;
16612 mod_time = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
16613 line_ptr += bytes_read;
16614 length = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
16615 line_ptr += bytes_read;
16616
16617 add_file_name (lh, cur_file, dir_index, mod_time, length);
16618 }
16619 line_ptr += bytes_read;
6e70227d 16620 lh->statement_program_start = line_ptr;
debd256d 16621
3019eac3 16622 if (line_ptr > (section->buffer + section->size))
4d3c2250 16623 complaint (&symfile_complaints,
3e43a32a
MS
16624 _("line number info header doesn't "
16625 "fit in `.debug_line' section"));
debd256d
JB
16626
16627 discard_cleanups (back_to);
16628 return lh;
16629}
c906108c 16630
c6da4cef
DE
16631/* Subroutine of dwarf_decode_lines to simplify it.
16632 Return the file name of the psymtab for included file FILE_INDEX
16633 in line header LH of PST.
16634 COMP_DIR is the compilation directory (DW_AT_comp_dir) or NULL if unknown.
16635 If space for the result is malloc'd, it will be freed by a cleanup.
1ed59174
JK
16636 Returns NULL if FILE_INDEX should be ignored, i.e., it is pst->filename.
16637
16638 The function creates dangling cleanup registration. */
c6da4cef 16639
d521ce57 16640static const char *
c6da4cef
DE
16641psymtab_include_file_name (const struct line_header *lh, int file_index,
16642 const struct partial_symtab *pst,
16643 const char *comp_dir)
16644{
16645 const struct file_entry fe = lh->file_names [file_index];
d521ce57
TT
16646 const char *include_name = fe.name;
16647 const char *include_name_to_compare = include_name;
16648 const char *dir_name = NULL;
72b9f47f
TT
16649 const char *pst_filename;
16650 char *copied_name = NULL;
c6da4cef
DE
16651 int file_is_pst;
16652
16653 if (fe.dir_index)
16654 dir_name = lh->include_dirs[fe.dir_index - 1];
16655
16656 if (!IS_ABSOLUTE_PATH (include_name)
16657 && (dir_name != NULL || comp_dir != NULL))
16658 {
16659 /* Avoid creating a duplicate psymtab for PST.
16660 We do this by comparing INCLUDE_NAME and PST_FILENAME.
16661 Before we do the comparison, however, we need to account
16662 for DIR_NAME and COMP_DIR.
16663 First prepend dir_name (if non-NULL). If we still don't
16664 have an absolute path prepend comp_dir (if non-NULL).
16665 However, the directory we record in the include-file's
16666 psymtab does not contain COMP_DIR (to match the
16667 corresponding symtab(s)).
16668
16669 Example:
16670
16671 bash$ cd /tmp
16672 bash$ gcc -g ./hello.c
16673 include_name = "hello.c"
16674 dir_name = "."
16675 DW_AT_comp_dir = comp_dir = "/tmp"
16676 DW_AT_name = "./hello.c" */
16677
16678 if (dir_name != NULL)
16679 {
d521ce57
TT
16680 char *tem = concat (dir_name, SLASH_STRING,
16681 include_name, (char *)NULL);
16682
16683 make_cleanup (xfree, tem);
16684 include_name = tem;
c6da4cef 16685 include_name_to_compare = include_name;
c6da4cef
DE
16686 }
16687 if (!IS_ABSOLUTE_PATH (include_name) && comp_dir != NULL)
16688 {
d521ce57
TT
16689 char *tem = concat (comp_dir, SLASH_STRING,
16690 include_name, (char *)NULL);
16691
16692 make_cleanup (xfree, tem);
16693 include_name_to_compare = tem;
c6da4cef
DE
16694 }
16695 }
16696
16697 pst_filename = pst->filename;
16698 if (!IS_ABSOLUTE_PATH (pst_filename) && pst->dirname != NULL)
16699 {
72b9f47f
TT
16700 copied_name = concat (pst->dirname, SLASH_STRING,
16701 pst_filename, (char *)NULL);
16702 pst_filename = copied_name;
c6da4cef
DE
16703 }
16704
1e3fad37 16705 file_is_pst = FILENAME_CMP (include_name_to_compare, pst_filename) == 0;
c6da4cef 16706
72b9f47f
TT
16707 if (copied_name != NULL)
16708 xfree (copied_name);
c6da4cef
DE
16709
16710 if (file_is_pst)
16711 return NULL;
16712 return include_name;
16713}
16714
c91513d8
PP
16715/* Ignore this record_line request. */
16716
16717static void
16718noop_record_line (struct subfile *subfile, int line, CORE_ADDR pc)
16719{
16720 return;
16721}
16722
f3f5162e
DE
16723/* Subroutine of dwarf_decode_lines to simplify it.
16724 Process the line number information in LH. */
debd256d 16725
c906108c 16726static void
f3f5162e
DE
16727dwarf_decode_lines_1 (struct line_header *lh, const char *comp_dir,
16728 struct dwarf2_cu *cu, struct partial_symtab *pst)
c906108c 16729{
d521ce57
TT
16730 const gdb_byte *line_ptr, *extended_end;
16731 const gdb_byte *line_end;
a8c50c1f 16732 unsigned int bytes_read, extended_len;
c906108c 16733 unsigned char op_code, extended_op, adj_opcode;
e142c38c
DJ
16734 CORE_ADDR baseaddr;
16735 struct objfile *objfile = cu->objfile;
f3f5162e 16736 bfd *abfd = objfile->obfd;
fbf65064 16737 struct gdbarch *gdbarch = get_objfile_arch (objfile);
aaa75496 16738 const int decode_for_pst_p = (pst != NULL);
f3f5162e 16739 struct subfile *last_subfile = NULL;
c91513d8
PP
16740 void (*p_record_line) (struct subfile *subfile, int line, CORE_ADDR pc)
16741 = record_line;
e142c38c
DJ
16742
16743 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
c906108c 16744
debd256d
JB
16745 line_ptr = lh->statement_program_start;
16746 line_end = lh->statement_program_end;
c906108c
SS
16747
16748 /* Read the statement sequences until there's nothing left. */
16749 while (line_ptr < line_end)
16750 {
16751 /* state machine registers */
16752 CORE_ADDR address = 0;
16753 unsigned int file = 1;
16754 unsigned int line = 1;
16755 unsigned int column = 0;
debd256d 16756 int is_stmt = lh->default_is_stmt;
c906108c
SS
16757 int basic_block = 0;
16758 int end_sequence = 0;
fbf65064 16759 CORE_ADDR addr;
2dc7f7b3 16760 unsigned char op_index = 0;
c906108c 16761
aaa75496 16762 if (!decode_for_pst_p && lh->num_file_names >= file)
c906108c 16763 {
aaa75496 16764 /* Start a subfile for the current file of the state machine. */
debd256d
JB
16765 /* lh->include_dirs and lh->file_names are 0-based, but the
16766 directory and file name numbers in the statement program
16767 are 1-based. */
16768 struct file_entry *fe = &lh->file_names[file - 1];
d521ce57 16769 const char *dir = NULL;
a738430d 16770
debd256d
JB
16771 if (fe->dir_index)
16772 dir = lh->include_dirs[fe->dir_index - 1];
4f1520fb
FR
16773
16774 dwarf2_start_subfile (fe->name, dir, comp_dir);
c906108c
SS
16775 }
16776
a738430d 16777 /* Decode the table. */
c5aa993b 16778 while (!end_sequence)
c906108c
SS
16779 {
16780 op_code = read_1_byte (abfd, line_ptr);
16781 line_ptr += 1;
59205f5a
JB
16782 if (line_ptr > line_end)
16783 {
16784 dwarf2_debug_line_missing_end_sequence_complaint ();
16785 break;
16786 }
9aa1fe7e 16787
debd256d 16788 if (op_code >= lh->opcode_base)
6e70227d 16789 {
a738430d 16790 /* Special operand. */
debd256d 16791 adj_opcode = op_code - lh->opcode_base;
2dc7f7b3
TT
16792 address += (((op_index + (adj_opcode / lh->line_range))
16793 / lh->maximum_ops_per_instruction)
16794 * lh->minimum_instruction_length);
16795 op_index = ((op_index + (adj_opcode / lh->line_range))
16796 % lh->maximum_ops_per_instruction);
debd256d 16797 line += lh->line_base + (adj_opcode % lh->line_range);
59205f5a 16798 if (lh->num_file_names < file || file == 0)
25e43795 16799 dwarf2_debug_line_missing_file_complaint ();
2dc7f7b3
TT
16800 /* For now we ignore lines not starting on an
16801 instruction boundary. */
16802 else if (op_index == 0)
25e43795
DJ
16803 {
16804 lh->file_names[file - 1].included_p = 1;
ca5f395d 16805 if (!decode_for_pst_p && is_stmt)
fbf65064
UW
16806 {
16807 if (last_subfile != current_subfile)
16808 {
16809 addr = gdbarch_addr_bits_remove (gdbarch, address);
16810 if (last_subfile)
c91513d8 16811 (*p_record_line) (last_subfile, 0, addr);
fbf65064
UW
16812 last_subfile = current_subfile;
16813 }
25e43795 16814 /* Append row to matrix using current values. */
7019d805 16815 addr = gdbarch_addr_bits_remove (gdbarch, address);
c91513d8 16816 (*p_record_line) (current_subfile, line, addr);
366da635 16817 }
25e43795 16818 }
ca5f395d 16819 basic_block = 0;
9aa1fe7e
GK
16820 }
16821 else switch (op_code)
c906108c
SS
16822 {
16823 case DW_LNS_extended_op:
3e43a32a
MS
16824 extended_len = read_unsigned_leb128 (abfd, line_ptr,
16825 &bytes_read);
473b7be6 16826 line_ptr += bytes_read;
a8c50c1f 16827 extended_end = line_ptr + extended_len;
c906108c
SS
16828 extended_op = read_1_byte (abfd, line_ptr);
16829 line_ptr += 1;
16830 switch (extended_op)
16831 {
16832 case DW_LNE_end_sequence:
c91513d8 16833 p_record_line = record_line;
c906108c 16834 end_sequence = 1;
c906108c
SS
16835 break;
16836 case DW_LNE_set_address:
e7c27a73 16837 address = read_address (abfd, line_ptr, cu, &bytes_read);
c91513d8
PP
16838
16839 if (address == 0 && !dwarf2_per_objfile->has_section_at_zero)
16840 {
16841 /* This line table is for a function which has been
16842 GCd by the linker. Ignore it. PR gdb/12528 */
16843
16844 long line_offset
36586728 16845 = line_ptr - get_debug_line_section (cu)->buffer;
c91513d8
PP
16846
16847 complaint (&symfile_complaints,
16848 _(".debug_line address at offset 0x%lx is 0 "
16849 "[in module %s]"),
4262abfb 16850 line_offset, objfile_name (objfile));
c91513d8
PP
16851 p_record_line = noop_record_line;
16852 }
16853
2dc7f7b3 16854 op_index = 0;
107d2387
AC
16855 line_ptr += bytes_read;
16856 address += baseaddr;
c906108c
SS
16857 break;
16858 case DW_LNE_define_file:
debd256d 16859 {
d521ce57 16860 const char *cur_file;
debd256d 16861 unsigned int dir_index, mod_time, length;
6e70227d 16862
3e43a32a
MS
16863 cur_file = read_direct_string (abfd, line_ptr,
16864 &bytes_read);
debd256d
JB
16865 line_ptr += bytes_read;
16866 dir_index =
16867 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
16868 line_ptr += bytes_read;
16869 mod_time =
16870 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
16871 line_ptr += bytes_read;
16872 length =
16873 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
16874 line_ptr += bytes_read;
16875 add_file_name (lh, cur_file, dir_index, mod_time, length);
16876 }
c906108c 16877 break;
d0c6ba3d
CC
16878 case DW_LNE_set_discriminator:
16879 /* The discriminator is not interesting to the debugger;
16880 just ignore it. */
16881 line_ptr = extended_end;
16882 break;
c906108c 16883 default:
4d3c2250 16884 complaint (&symfile_complaints,
e2e0b3e5 16885 _("mangled .debug_line section"));
debd256d 16886 return;
c906108c 16887 }
a8c50c1f
DJ
16888 /* Make sure that we parsed the extended op correctly. If e.g.
16889 we expected a different address size than the producer used,
16890 we may have read the wrong number of bytes. */
16891 if (line_ptr != extended_end)
16892 {
16893 complaint (&symfile_complaints,
16894 _("mangled .debug_line section"));
16895 return;
16896 }
c906108c
SS
16897 break;
16898 case DW_LNS_copy:
59205f5a 16899 if (lh->num_file_names < file || file == 0)
25e43795
DJ
16900 dwarf2_debug_line_missing_file_complaint ();
16901 else
366da635 16902 {
25e43795 16903 lh->file_names[file - 1].included_p = 1;
ca5f395d 16904 if (!decode_for_pst_p && is_stmt)
fbf65064
UW
16905 {
16906 if (last_subfile != current_subfile)
16907 {
16908 addr = gdbarch_addr_bits_remove (gdbarch, address);
16909 if (last_subfile)
c91513d8 16910 (*p_record_line) (last_subfile, 0, addr);
fbf65064
UW
16911 last_subfile = current_subfile;
16912 }
7019d805 16913 addr = gdbarch_addr_bits_remove (gdbarch, address);
c91513d8 16914 (*p_record_line) (current_subfile, line, addr);
fbf65064 16915 }
366da635 16916 }
c906108c
SS
16917 basic_block = 0;
16918 break;
16919 case DW_LNS_advance_pc:
2dc7f7b3
TT
16920 {
16921 CORE_ADDR adjust
16922 = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
16923
16924 address += (((op_index + adjust)
16925 / lh->maximum_ops_per_instruction)
16926 * lh->minimum_instruction_length);
16927 op_index = ((op_index + adjust)
16928 % lh->maximum_ops_per_instruction);
16929 line_ptr += bytes_read;
16930 }
c906108c
SS
16931 break;
16932 case DW_LNS_advance_line:
16933 line += read_signed_leb128 (abfd, line_ptr, &bytes_read);
16934 line_ptr += bytes_read;
16935 break;
16936 case DW_LNS_set_file:
debd256d 16937 {
a738430d
MK
16938 /* The arrays lh->include_dirs and lh->file_names are
16939 0-based, but the directory and file name numbers in
16940 the statement program are 1-based. */
debd256d 16941 struct file_entry *fe;
d521ce57 16942 const char *dir = NULL;
a738430d 16943
debd256d
JB
16944 file = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
16945 line_ptr += bytes_read;
59205f5a 16946 if (lh->num_file_names < file || file == 0)
25e43795
DJ
16947 dwarf2_debug_line_missing_file_complaint ();
16948 else
16949 {
16950 fe = &lh->file_names[file - 1];
16951 if (fe->dir_index)
16952 dir = lh->include_dirs[fe->dir_index - 1];
16953 if (!decode_for_pst_p)
16954 {
16955 last_subfile = current_subfile;
16956 dwarf2_start_subfile (fe->name, dir, comp_dir);
16957 }
16958 }
debd256d 16959 }
c906108c
SS
16960 break;
16961 case DW_LNS_set_column:
16962 column = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
16963 line_ptr += bytes_read;
16964 break;
16965 case DW_LNS_negate_stmt:
16966 is_stmt = (!is_stmt);
16967 break;
16968 case DW_LNS_set_basic_block:
16969 basic_block = 1;
16970 break;
c2c6d25f
JM
16971 /* Add to the address register of the state machine the
16972 address increment value corresponding to special opcode
a738430d
MK
16973 255. I.e., this value is scaled by the minimum
16974 instruction length since special opcode 255 would have
b021a221 16975 scaled the increment. */
c906108c 16976 case DW_LNS_const_add_pc:
2dc7f7b3
TT
16977 {
16978 CORE_ADDR adjust = (255 - lh->opcode_base) / lh->line_range;
16979
16980 address += (((op_index + adjust)
16981 / lh->maximum_ops_per_instruction)
16982 * lh->minimum_instruction_length);
16983 op_index = ((op_index + adjust)
16984 % lh->maximum_ops_per_instruction);
16985 }
c906108c
SS
16986 break;
16987 case DW_LNS_fixed_advance_pc:
16988 address += read_2_bytes (abfd, line_ptr);
2dc7f7b3 16989 op_index = 0;
c906108c
SS
16990 line_ptr += 2;
16991 break;
9aa1fe7e 16992 default:
a738430d
MK
16993 {
16994 /* Unknown standard opcode, ignore it. */
9aa1fe7e 16995 int i;
a738430d 16996
debd256d 16997 for (i = 0; i < lh->standard_opcode_lengths[op_code]; i++)
9aa1fe7e
GK
16998 {
16999 (void) read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
17000 line_ptr += bytes_read;
17001 }
17002 }
c906108c
SS
17003 }
17004 }
59205f5a
JB
17005 if (lh->num_file_names < file || file == 0)
17006 dwarf2_debug_line_missing_file_complaint ();
17007 else
17008 {
17009 lh->file_names[file - 1].included_p = 1;
17010 if (!decode_for_pst_p)
fbf65064
UW
17011 {
17012 addr = gdbarch_addr_bits_remove (gdbarch, address);
c91513d8 17013 (*p_record_line) (current_subfile, 0, addr);
fbf65064 17014 }
59205f5a 17015 }
c906108c 17016 }
f3f5162e
DE
17017}
17018
17019/* Decode the Line Number Program (LNP) for the given line_header
17020 structure and CU. The actual information extracted and the type
17021 of structures created from the LNP depends on the value of PST.
17022
17023 1. If PST is NULL, then this procedure uses the data from the program
17024 to create all necessary symbol tables, and their linetables.
17025
17026 2. If PST is not NULL, this procedure reads the program to determine
17027 the list of files included by the unit represented by PST, and
17028 builds all the associated partial symbol tables.
17029
17030 COMP_DIR is the compilation directory (DW_AT_comp_dir) or NULL if unknown.
17031 It is used for relative paths in the line table.
17032 NOTE: When processing partial symtabs (pst != NULL),
17033 comp_dir == pst->dirname.
17034
17035 NOTE: It is important that psymtabs have the same file name (via strcmp)
17036 as the corresponding symtab. Since COMP_DIR is not used in the name of the
17037 symtab we don't use it in the name of the psymtabs we create.
17038 E.g. expand_line_sal requires this when finding psymtabs to expand.
17039 A good testcase for this is mb-inline.exp. */
17040
17041static void
17042dwarf_decode_lines (struct line_header *lh, const char *comp_dir,
17043 struct dwarf2_cu *cu, struct partial_symtab *pst,
17044 int want_line_info)
17045{
17046 struct objfile *objfile = cu->objfile;
17047 const int decode_for_pst_p = (pst != NULL);
17048 struct subfile *first_subfile = current_subfile;
17049
17050 if (want_line_info)
17051 dwarf_decode_lines_1 (lh, comp_dir, cu, pst);
aaa75496
JB
17052
17053 if (decode_for_pst_p)
17054 {
17055 int file_index;
17056
17057 /* Now that we're done scanning the Line Header Program, we can
17058 create the psymtab of each included file. */
17059 for (file_index = 0; file_index < lh->num_file_names; file_index++)
17060 if (lh->file_names[file_index].included_p == 1)
17061 {
d521ce57 17062 const char *include_name =
c6da4cef
DE
17063 psymtab_include_file_name (lh, file_index, pst, comp_dir);
17064 if (include_name != NULL)
aaa75496
JB
17065 dwarf2_create_include_psymtab (include_name, pst, objfile);
17066 }
17067 }
cb1df416
DJ
17068 else
17069 {
17070 /* Make sure a symtab is created for every file, even files
17071 which contain only variables (i.e. no code with associated
17072 line numbers). */
cb1df416 17073 int i;
cb1df416
DJ
17074
17075 for (i = 0; i < lh->num_file_names; i++)
17076 {
d521ce57 17077 const char *dir = NULL;
f3f5162e 17078 struct file_entry *fe;
9a619af0 17079
cb1df416
DJ
17080 fe = &lh->file_names[i];
17081 if (fe->dir_index)
17082 dir = lh->include_dirs[fe->dir_index - 1];
17083 dwarf2_start_subfile (fe->name, dir, comp_dir);
17084
17085 /* Skip the main file; we don't need it, and it must be
17086 allocated last, so that it will show up before the
17087 non-primary symtabs in the objfile's symtab list. */
17088 if (current_subfile == first_subfile)
17089 continue;
17090
17091 if (current_subfile->symtab == NULL)
17092 current_subfile->symtab = allocate_symtab (current_subfile->name,
bb5ed363 17093 objfile);
cb1df416
DJ
17094 fe->symtab = current_subfile->symtab;
17095 }
17096 }
c906108c
SS
17097}
17098
17099/* Start a subfile for DWARF. FILENAME is the name of the file and
17100 DIRNAME the name of the source directory which contains FILENAME
4f1520fb
FR
17101 or NULL if not known. COMP_DIR is the compilation directory for the
17102 linetable's compilation unit or NULL if not known.
c906108c
SS
17103 This routine tries to keep line numbers from identical absolute and
17104 relative file names in a common subfile.
17105
17106 Using the `list' example from the GDB testsuite, which resides in
17107 /srcdir and compiling it with Irix6.2 cc in /compdir using a filename
17108 of /srcdir/list0.c yields the following debugging information for list0.c:
17109
c5aa993b
JM
17110 DW_AT_name: /srcdir/list0.c
17111 DW_AT_comp_dir: /compdir
357e46e7 17112 files.files[0].name: list0.h
c5aa993b 17113 files.files[0].dir: /srcdir
357e46e7 17114 files.files[1].name: list0.c
c5aa993b 17115 files.files[1].dir: /srcdir
c906108c
SS
17116
17117 The line number information for list0.c has to end up in a single
4f1520fb
FR
17118 subfile, so that `break /srcdir/list0.c:1' works as expected.
17119 start_subfile will ensure that this happens provided that we pass the
17120 concatenation of files.files[1].dir and files.files[1].name as the
17121 subfile's name. */
c906108c
SS
17122
17123static void
d521ce57 17124dwarf2_start_subfile (const char *filename, const char *dirname,
3e43a32a 17125 const char *comp_dir)
c906108c 17126{
d521ce57 17127 char *copy = NULL;
4f1520fb
FR
17128
17129 /* While reading the DIEs, we call start_symtab(DW_AT_name, DW_AT_comp_dir).
17130 `start_symtab' will always pass the contents of DW_AT_comp_dir as
17131 second argument to start_subfile. To be consistent, we do the
17132 same here. In order not to lose the line information directory,
17133 we concatenate it to the filename when it makes sense.
17134 Note that the Dwarf3 standard says (speaking of filenames in line
17135 information): ``The directory index is ignored for file names
17136 that represent full path names''. Thus ignoring dirname in the
17137 `else' branch below isn't an issue. */
c906108c 17138
d5166ae1 17139 if (!IS_ABSOLUTE_PATH (filename) && dirname != NULL)
d521ce57
TT
17140 {
17141 copy = concat (dirname, SLASH_STRING, filename, (char *)NULL);
17142 filename = copy;
17143 }
c906108c 17144
d521ce57 17145 start_subfile (filename, comp_dir);
4f1520fb 17146
d521ce57
TT
17147 if (copy != NULL)
17148 xfree (copy);
c906108c
SS
17149}
17150
f4dc4d17
DE
17151/* Start a symtab for DWARF.
17152 NAME, COMP_DIR, LOW_PC are passed to start_symtab. */
17153
17154static void
17155dwarf2_start_symtab (struct dwarf2_cu *cu,
15d034d0 17156 const char *name, const char *comp_dir, CORE_ADDR low_pc)
f4dc4d17
DE
17157{
17158 start_symtab (name, comp_dir, low_pc);
17159 record_debugformat ("DWARF 2");
17160 record_producer (cu->producer);
17161
17162 /* We assume that we're processing GCC output. */
17163 processing_gcc_compilation = 2;
17164
4d4ec4e5 17165 cu->processing_has_namespace_info = 0;
f4dc4d17
DE
17166}
17167
4c2df51b
DJ
17168static void
17169var_decode_location (struct attribute *attr, struct symbol *sym,
e7c27a73 17170 struct dwarf2_cu *cu)
4c2df51b 17171{
e7c27a73
DJ
17172 struct objfile *objfile = cu->objfile;
17173 struct comp_unit_head *cu_header = &cu->header;
17174
4c2df51b
DJ
17175 /* NOTE drow/2003-01-30: There used to be a comment and some special
17176 code here to turn a symbol with DW_AT_external and a
17177 SYMBOL_VALUE_ADDRESS of 0 into a LOC_UNRESOLVED symbol. This was
17178 necessary for platforms (maybe Alpha, certainly PowerPC GNU/Linux
17179 with some versions of binutils) where shared libraries could have
17180 relocations against symbols in their debug information - the
17181 minimal symbol would have the right address, but the debug info
17182 would not. It's no longer necessary, because we will explicitly
17183 apply relocations when we read in the debug information now. */
17184
17185 /* A DW_AT_location attribute with no contents indicates that a
17186 variable has been optimized away. */
17187 if (attr_form_is_block (attr) && DW_BLOCK (attr)->size == 0)
17188 {
f1e6e072 17189 SYMBOL_ACLASS_INDEX (sym) = LOC_OPTIMIZED_OUT;
4c2df51b
DJ
17190 return;
17191 }
17192
17193 /* Handle one degenerate form of location expression specially, to
17194 preserve GDB's previous behavior when section offsets are
3019eac3
DE
17195 specified. If this is just a DW_OP_addr or DW_OP_GNU_addr_index
17196 then mark this symbol as LOC_STATIC. */
4c2df51b
DJ
17197
17198 if (attr_form_is_block (attr)
3019eac3
DE
17199 && ((DW_BLOCK (attr)->data[0] == DW_OP_addr
17200 && DW_BLOCK (attr)->size == 1 + cu_header->addr_size)
17201 || (DW_BLOCK (attr)->data[0] == DW_OP_GNU_addr_index
17202 && (DW_BLOCK (attr)->size
17203 == 1 + leb128_size (&DW_BLOCK (attr)->data[1])))))
4c2df51b 17204 {
891d2f0b 17205 unsigned int dummy;
4c2df51b 17206
3019eac3
DE
17207 if (DW_BLOCK (attr)->data[0] == DW_OP_addr)
17208 SYMBOL_VALUE_ADDRESS (sym) =
17209 read_address (objfile->obfd, DW_BLOCK (attr)->data + 1, cu, &dummy);
17210 else
17211 SYMBOL_VALUE_ADDRESS (sym) =
17212 read_addr_index_from_leb128 (cu, DW_BLOCK (attr)->data + 1, &dummy);
f1e6e072 17213 SYMBOL_ACLASS_INDEX (sym) = LOC_STATIC;
4c2df51b
DJ
17214 fixup_symbol_section (sym, objfile);
17215 SYMBOL_VALUE_ADDRESS (sym) += ANOFFSET (objfile->section_offsets,
17216 SYMBOL_SECTION (sym));
4c2df51b
DJ
17217 return;
17218 }
17219
17220 /* NOTE drow/2002-01-30: It might be worthwhile to have a static
17221 expression evaluator, and use LOC_COMPUTED only when necessary
17222 (i.e. when the value of a register or memory location is
17223 referenced, or a thread-local block, etc.). Then again, it might
17224 not be worthwhile. I'm assuming that it isn't unless performance
17225 or memory numbers show me otherwise. */
17226
f1e6e072 17227 dwarf2_symbol_mark_computed (attr, sym, cu, 0);
8be455d7 17228
f1e6e072 17229 if (SYMBOL_COMPUTED_OPS (sym)->location_has_loclist)
8be455d7 17230 cu->has_loclist = 1;
4c2df51b
DJ
17231}
17232
c906108c
SS
17233/* Given a pointer to a DWARF information entry, figure out if we need
17234 to make a symbol table entry for it, and if so, create a new entry
17235 and return a pointer to it.
17236 If TYPE is NULL, determine symbol type from the die, otherwise
34eaf542
TT
17237 used the passed type.
17238 If SPACE is not NULL, use it to hold the new symbol. If it is
17239 NULL, allocate a new symbol on the objfile's obstack. */
c906108c
SS
17240
17241static struct symbol *
34eaf542
TT
17242new_symbol_full (struct die_info *die, struct type *type, struct dwarf2_cu *cu,
17243 struct symbol *space)
c906108c 17244{
e7c27a73 17245 struct objfile *objfile = cu->objfile;
c906108c 17246 struct symbol *sym = NULL;
15d034d0 17247 const char *name;
c906108c
SS
17248 struct attribute *attr = NULL;
17249 struct attribute *attr2 = NULL;
e142c38c 17250 CORE_ADDR baseaddr;
e37fd15a
SW
17251 struct pending **list_to_add = NULL;
17252
edb3359d 17253 int inlined_func = (die->tag == DW_TAG_inlined_subroutine);
e142c38c
DJ
17254
17255 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
c906108c 17256
94af9270 17257 name = dwarf2_name (die, cu);
c906108c
SS
17258 if (name)
17259 {
94af9270 17260 const char *linkagename;
34eaf542 17261 int suppress_add = 0;
94af9270 17262
34eaf542
TT
17263 if (space)
17264 sym = space;
17265 else
e623cf5d 17266 sym = allocate_symbol (objfile);
c906108c 17267 OBJSTAT (objfile, n_syms++);
2de7ced7
DJ
17268
17269 /* Cache this symbol's name and the name's demangled form (if any). */
f85f34ed 17270 SYMBOL_SET_LANGUAGE (sym, cu->language, &objfile->objfile_obstack);
94af9270
KS
17271 linkagename = dwarf2_physname (name, die, cu);
17272 SYMBOL_SET_NAMES (sym, linkagename, strlen (linkagename), 0, objfile);
c906108c 17273
f55ee35c
JK
17274 /* Fortran does not have mangling standard and the mangling does differ
17275 between gfortran, iFort etc. */
17276 if (cu->language == language_fortran
b250c185 17277 && symbol_get_demangled_name (&(sym->ginfo)) == NULL)
29df156d 17278 symbol_set_demangled_name (&(sym->ginfo),
cfc594ee 17279 dwarf2_full_name (name, die, cu),
29df156d 17280 NULL);
f55ee35c 17281
c906108c 17282 /* Default assumptions.
c5aa993b 17283 Use the passed type or decode it from the die. */
176620f1 17284 SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
f1e6e072 17285 SYMBOL_ACLASS_INDEX (sym) = LOC_OPTIMIZED_OUT;
c906108c
SS
17286 if (type != NULL)
17287 SYMBOL_TYPE (sym) = type;
17288 else
e7c27a73 17289 SYMBOL_TYPE (sym) = die_type (die, cu);
edb3359d
DJ
17290 attr = dwarf2_attr (die,
17291 inlined_func ? DW_AT_call_line : DW_AT_decl_line,
17292 cu);
c906108c
SS
17293 if (attr)
17294 {
17295 SYMBOL_LINE (sym) = DW_UNSND (attr);
17296 }
cb1df416 17297
edb3359d
DJ
17298 attr = dwarf2_attr (die,
17299 inlined_func ? DW_AT_call_file : DW_AT_decl_file,
17300 cu);
cb1df416
DJ
17301 if (attr)
17302 {
17303 int file_index = DW_UNSND (attr);
9a619af0 17304
cb1df416
DJ
17305 if (cu->line_header == NULL
17306 || file_index > cu->line_header->num_file_names)
17307 complaint (&symfile_complaints,
17308 _("file index out of range"));
1c3d648d 17309 else if (file_index > 0)
cb1df416
DJ
17310 {
17311 struct file_entry *fe;
9a619af0 17312
cb1df416
DJ
17313 fe = &cu->line_header->file_names[file_index - 1];
17314 SYMBOL_SYMTAB (sym) = fe->symtab;
17315 }
17316 }
17317
c906108c
SS
17318 switch (die->tag)
17319 {
17320 case DW_TAG_label:
e142c38c 17321 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
c906108c
SS
17322 if (attr)
17323 {
17324 SYMBOL_VALUE_ADDRESS (sym) = DW_ADDR (attr) + baseaddr;
17325 }
0f5238ed
TT
17326 SYMBOL_TYPE (sym) = objfile_type (objfile)->builtin_core_addr;
17327 SYMBOL_DOMAIN (sym) = LABEL_DOMAIN;
f1e6e072 17328 SYMBOL_ACLASS_INDEX (sym) = LOC_LABEL;
0f5238ed 17329 add_symbol_to_list (sym, cu->list_in_scope);
c906108c
SS
17330 break;
17331 case DW_TAG_subprogram:
17332 /* SYMBOL_BLOCK_VALUE (sym) will be filled in later by
17333 finish_block. */
f1e6e072 17334 SYMBOL_ACLASS_INDEX (sym) = LOC_BLOCK;
e142c38c 17335 attr2 = dwarf2_attr (die, DW_AT_external, cu);
2cfa0c8d
JB
17336 if ((attr2 && (DW_UNSND (attr2) != 0))
17337 || cu->language == language_ada)
c906108c 17338 {
2cfa0c8d
JB
17339 /* Subprograms marked external are stored as a global symbol.
17340 Ada subprograms, whether marked external or not, are always
17341 stored as a global symbol, because we want to be able to
17342 access them globally. For instance, we want to be able
17343 to break on a nested subprogram without having to
17344 specify the context. */
e37fd15a 17345 list_to_add = &global_symbols;
c906108c
SS
17346 }
17347 else
17348 {
e37fd15a 17349 list_to_add = cu->list_in_scope;
c906108c
SS
17350 }
17351 break;
edb3359d
DJ
17352 case DW_TAG_inlined_subroutine:
17353 /* SYMBOL_BLOCK_VALUE (sym) will be filled in later by
17354 finish_block. */
f1e6e072 17355 SYMBOL_ACLASS_INDEX (sym) = LOC_BLOCK;
edb3359d 17356 SYMBOL_INLINED (sym) = 1;
481860b3 17357 list_to_add = cu->list_in_scope;
edb3359d 17358 break;
34eaf542
TT
17359 case DW_TAG_template_value_param:
17360 suppress_add = 1;
17361 /* Fall through. */
72929c62 17362 case DW_TAG_constant:
c906108c 17363 case DW_TAG_variable:
254e6b9e 17364 case DW_TAG_member:
0963b4bd
MS
17365 /* Compilation with minimal debug info may result in
17366 variables with missing type entries. Change the
17367 misleading `void' type to something sensible. */
c906108c 17368 if (TYPE_CODE (SYMBOL_TYPE (sym)) == TYPE_CODE_VOID)
64c50499 17369 SYMBOL_TYPE (sym)
46bf5051 17370 = objfile_type (objfile)->nodebug_data_symbol;
64c50499 17371
e142c38c 17372 attr = dwarf2_attr (die, DW_AT_const_value, cu);
254e6b9e
DE
17373 /* In the case of DW_TAG_member, we should only be called for
17374 static const members. */
17375 if (die->tag == DW_TAG_member)
17376 {
3863f96c
DE
17377 /* dwarf2_add_field uses die_is_declaration,
17378 so we do the same. */
254e6b9e
DE
17379 gdb_assert (die_is_declaration (die, cu));
17380 gdb_assert (attr);
17381 }
c906108c
SS
17382 if (attr)
17383 {
e7c27a73 17384 dwarf2_const_value (attr, sym, cu);
e142c38c 17385 attr2 = dwarf2_attr (die, DW_AT_external, cu);
e37fd15a 17386 if (!suppress_add)
34eaf542
TT
17387 {
17388 if (attr2 && (DW_UNSND (attr2) != 0))
e37fd15a 17389 list_to_add = &global_symbols;
34eaf542 17390 else
e37fd15a 17391 list_to_add = cu->list_in_scope;
34eaf542 17392 }
c906108c
SS
17393 break;
17394 }
e142c38c 17395 attr = dwarf2_attr (die, DW_AT_location, cu);
c906108c
SS
17396 if (attr)
17397 {
e7c27a73 17398 var_decode_location (attr, sym, cu);
e142c38c 17399 attr2 = dwarf2_attr (die, DW_AT_external, cu);
4357ac6c
TT
17400
17401 /* Fortran explicitly imports any global symbols to the local
17402 scope by DW_TAG_common_block. */
17403 if (cu->language == language_fortran && die->parent
17404 && die->parent->tag == DW_TAG_common_block)
17405 attr2 = NULL;
17406
caac4577
JG
17407 if (SYMBOL_CLASS (sym) == LOC_STATIC
17408 && SYMBOL_VALUE_ADDRESS (sym) == 0
17409 && !dwarf2_per_objfile->has_section_at_zero)
17410 {
17411 /* When a static variable is eliminated by the linker,
17412 the corresponding debug information is not stripped
17413 out, but the variable address is set to null;
17414 do not add such variables into symbol table. */
17415 }
17416 else if (attr2 && (DW_UNSND (attr2) != 0))
1c809c68 17417 {
f55ee35c
JK
17418 /* Workaround gfortran PR debug/40040 - it uses
17419 DW_AT_location for variables in -fPIC libraries which may
17420 get overriden by other libraries/executable and get
17421 a different address. Resolve it by the minimal symbol
17422 which may come from inferior's executable using copy
17423 relocation. Make this workaround only for gfortran as for
17424 other compilers GDB cannot guess the minimal symbol
17425 Fortran mangling kind. */
17426 if (cu->language == language_fortran && die->parent
17427 && die->parent->tag == DW_TAG_module
17428 && cu->producer
17429 && strncmp (cu->producer, "GNU Fortran ", 12) == 0)
f1e6e072 17430 SYMBOL_ACLASS_INDEX (sym) = LOC_UNRESOLVED;
f55ee35c 17431
1c809c68
TT
17432 /* A variable with DW_AT_external is never static,
17433 but it may be block-scoped. */
17434 list_to_add = (cu->list_in_scope == &file_symbols
17435 ? &global_symbols : cu->list_in_scope);
1c809c68 17436 }
c906108c 17437 else
e37fd15a 17438 list_to_add = cu->list_in_scope;
c906108c
SS
17439 }
17440 else
17441 {
17442 /* We do not know the address of this symbol.
c5aa993b
JM
17443 If it is an external symbol and we have type information
17444 for it, enter the symbol as a LOC_UNRESOLVED symbol.
17445 The address of the variable will then be determined from
17446 the minimal symbol table whenever the variable is
17447 referenced. */
e142c38c 17448 attr2 = dwarf2_attr (die, DW_AT_external, cu);
0971de02
TT
17449
17450 /* Fortran explicitly imports any global symbols to the local
17451 scope by DW_TAG_common_block. */
17452 if (cu->language == language_fortran && die->parent
17453 && die->parent->tag == DW_TAG_common_block)
17454 {
17455 /* SYMBOL_CLASS doesn't matter here because
17456 read_common_block is going to reset it. */
17457 if (!suppress_add)
17458 list_to_add = cu->list_in_scope;
17459 }
17460 else if (attr2 && (DW_UNSND (attr2) != 0)
17461 && dwarf2_attr (die, DW_AT_type, cu) != NULL)
c906108c 17462 {
0fe7935b
DJ
17463 /* A variable with DW_AT_external is never static, but it
17464 may be block-scoped. */
17465 list_to_add = (cu->list_in_scope == &file_symbols
17466 ? &global_symbols : cu->list_in_scope);
17467
f1e6e072 17468 SYMBOL_ACLASS_INDEX (sym) = LOC_UNRESOLVED;
c906108c 17469 }
442ddf59
JK
17470 else if (!die_is_declaration (die, cu))
17471 {
17472 /* Use the default LOC_OPTIMIZED_OUT class. */
17473 gdb_assert (SYMBOL_CLASS (sym) == LOC_OPTIMIZED_OUT);
e37fd15a
SW
17474 if (!suppress_add)
17475 list_to_add = cu->list_in_scope;
442ddf59 17476 }
c906108c
SS
17477 }
17478 break;
17479 case DW_TAG_formal_parameter:
edb3359d
DJ
17480 /* If we are inside a function, mark this as an argument. If
17481 not, we might be looking at an argument to an inlined function
17482 when we do not have enough information to show inlined frames;
17483 pretend it's a local variable in that case so that the user can
17484 still see it. */
17485 if (context_stack_depth > 0
17486 && context_stack[context_stack_depth - 1].name != NULL)
17487 SYMBOL_IS_ARGUMENT (sym) = 1;
e142c38c 17488 attr = dwarf2_attr (die, DW_AT_location, cu);
c906108c
SS
17489 if (attr)
17490 {
e7c27a73 17491 var_decode_location (attr, sym, cu);
c906108c 17492 }
e142c38c 17493 attr = dwarf2_attr (die, DW_AT_const_value, cu);
c906108c
SS
17494 if (attr)
17495 {
e7c27a73 17496 dwarf2_const_value (attr, sym, cu);
c906108c 17497 }
f346a30d 17498
e37fd15a 17499 list_to_add = cu->list_in_scope;
c906108c
SS
17500 break;
17501 case DW_TAG_unspecified_parameters:
17502 /* From varargs functions; gdb doesn't seem to have any
17503 interest in this information, so just ignore it for now.
17504 (FIXME?) */
17505 break;
34eaf542
TT
17506 case DW_TAG_template_type_param:
17507 suppress_add = 1;
17508 /* Fall through. */
c906108c 17509 case DW_TAG_class_type:
680b30c7 17510 case DW_TAG_interface_type:
c906108c
SS
17511 case DW_TAG_structure_type:
17512 case DW_TAG_union_type:
72019c9c 17513 case DW_TAG_set_type:
c906108c 17514 case DW_TAG_enumeration_type:
f1e6e072 17515 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
176620f1 17516 SYMBOL_DOMAIN (sym) = STRUCT_DOMAIN;
c906108c 17517
63d06c5c 17518 {
987504bb 17519 /* NOTE: carlton/2003-11-10: C++ and Java class symbols shouldn't
63d06c5c
DC
17520 really ever be static objects: otherwise, if you try
17521 to, say, break of a class's method and you're in a file
17522 which doesn't mention that class, it won't work unless
17523 the check for all static symbols in lookup_symbol_aux
17524 saves you. See the OtherFileClass tests in
17525 gdb.c++/namespace.exp. */
17526
e37fd15a 17527 if (!suppress_add)
34eaf542 17528 {
34eaf542
TT
17529 list_to_add = (cu->list_in_scope == &file_symbols
17530 && (cu->language == language_cplus
17531 || cu->language == language_java)
17532 ? &global_symbols : cu->list_in_scope);
63d06c5c 17533
64382290
TT
17534 /* The semantics of C++ state that "struct foo {
17535 ... }" also defines a typedef for "foo". A Java
17536 class declaration also defines a typedef for the
17537 class. */
17538 if (cu->language == language_cplus
17539 || cu->language == language_java
17540 || cu->language == language_ada)
17541 {
17542 /* The symbol's name is already allocated along
17543 with this objfile, so we don't need to
17544 duplicate it for the type. */
17545 if (TYPE_NAME (SYMBOL_TYPE (sym)) == 0)
17546 TYPE_NAME (SYMBOL_TYPE (sym)) = SYMBOL_SEARCH_NAME (sym);
17547 }
63d06c5c
DC
17548 }
17549 }
c906108c
SS
17550 break;
17551 case DW_TAG_typedef:
f1e6e072 17552 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
63d06c5c 17553 SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
e37fd15a 17554 list_to_add = cu->list_in_scope;
63d06c5c 17555 break;
c906108c 17556 case DW_TAG_base_type:
a02abb62 17557 case DW_TAG_subrange_type:
f1e6e072 17558 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
176620f1 17559 SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
e37fd15a 17560 list_to_add = cu->list_in_scope;
c906108c
SS
17561 break;
17562 case DW_TAG_enumerator:
e142c38c 17563 attr = dwarf2_attr (die, DW_AT_const_value, cu);
c906108c
SS
17564 if (attr)
17565 {
e7c27a73 17566 dwarf2_const_value (attr, sym, cu);
c906108c 17567 }
63d06c5c
DC
17568 {
17569 /* NOTE: carlton/2003-11-10: See comment above in the
17570 DW_TAG_class_type, etc. block. */
17571
e142c38c 17572 list_to_add = (cu->list_in_scope == &file_symbols
987504bb
JJ
17573 && (cu->language == language_cplus
17574 || cu->language == language_java)
e142c38c 17575 ? &global_symbols : cu->list_in_scope);
63d06c5c 17576 }
c906108c 17577 break;
5c4e30ca 17578 case DW_TAG_namespace:
f1e6e072 17579 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
e37fd15a 17580 list_to_add = &global_symbols;
5c4e30ca 17581 break;
4357ac6c 17582 case DW_TAG_common_block:
f1e6e072 17583 SYMBOL_ACLASS_INDEX (sym) = LOC_COMMON_BLOCK;
4357ac6c
TT
17584 SYMBOL_DOMAIN (sym) = COMMON_BLOCK_DOMAIN;
17585 add_symbol_to_list (sym, cu->list_in_scope);
17586 break;
c906108c
SS
17587 default:
17588 /* Not a tag we recognize. Hopefully we aren't processing
17589 trash data, but since we must specifically ignore things
17590 we don't recognize, there is nothing else we should do at
0963b4bd 17591 this point. */
e2e0b3e5 17592 complaint (&symfile_complaints, _("unsupported tag: '%s'"),
4d3c2250 17593 dwarf_tag_name (die->tag));
c906108c
SS
17594 break;
17595 }
df8a16a1 17596
e37fd15a
SW
17597 if (suppress_add)
17598 {
17599 sym->hash_next = objfile->template_symbols;
17600 objfile->template_symbols = sym;
17601 list_to_add = NULL;
17602 }
17603
17604 if (list_to_add != NULL)
17605 add_symbol_to_list (sym, list_to_add);
17606
df8a16a1
DJ
17607 /* For the benefit of old versions of GCC, check for anonymous
17608 namespaces based on the demangled name. */
4d4ec4e5 17609 if (!cu->processing_has_namespace_info
94af9270 17610 && cu->language == language_cplus)
a10964d1 17611 cp_scan_for_anonymous_namespaces (sym, objfile);
c906108c
SS
17612 }
17613 return (sym);
17614}
17615
34eaf542
TT
17616/* A wrapper for new_symbol_full that always allocates a new symbol. */
17617
17618static struct symbol *
17619new_symbol (struct die_info *die, struct type *type, struct dwarf2_cu *cu)
17620{
17621 return new_symbol_full (die, type, cu, NULL);
17622}
17623
98bfdba5
PA
17624/* Given an attr with a DW_FORM_dataN value in host byte order,
17625 zero-extend it as appropriate for the symbol's type. The DWARF
17626 standard (v4) is not entirely clear about the meaning of using
17627 DW_FORM_dataN for a constant with a signed type, where the type is
17628 wider than the data. The conclusion of a discussion on the DWARF
17629 list was that this is unspecified. We choose to always zero-extend
17630 because that is the interpretation long in use by GCC. */
c906108c 17631
98bfdba5 17632static gdb_byte *
ff39bb5e 17633dwarf2_const_value_data (const struct attribute *attr, struct obstack *obstack,
12df843f 17634 struct dwarf2_cu *cu, LONGEST *value, int bits)
c906108c 17635{
e7c27a73 17636 struct objfile *objfile = cu->objfile;
e17a4113
UW
17637 enum bfd_endian byte_order = bfd_big_endian (objfile->obfd) ?
17638 BFD_ENDIAN_BIG : BFD_ENDIAN_LITTLE;
98bfdba5
PA
17639 LONGEST l = DW_UNSND (attr);
17640
17641 if (bits < sizeof (*value) * 8)
17642 {
17643 l &= ((LONGEST) 1 << bits) - 1;
17644 *value = l;
17645 }
17646 else if (bits == sizeof (*value) * 8)
17647 *value = l;
17648 else
17649 {
17650 gdb_byte *bytes = obstack_alloc (obstack, bits / 8);
17651 store_unsigned_integer (bytes, bits / 8, byte_order, l);
17652 return bytes;
17653 }
17654
17655 return NULL;
17656}
17657
17658/* Read a constant value from an attribute. Either set *VALUE, or if
17659 the value does not fit in *VALUE, set *BYTES - either already
17660 allocated on the objfile obstack, or newly allocated on OBSTACK,
17661 or, set *BATON, if we translated the constant to a location
17662 expression. */
17663
17664static void
ff39bb5e 17665dwarf2_const_value_attr (const struct attribute *attr, struct type *type,
98bfdba5
PA
17666 const char *name, struct obstack *obstack,
17667 struct dwarf2_cu *cu,
d521ce57 17668 LONGEST *value, const gdb_byte **bytes,
98bfdba5
PA
17669 struct dwarf2_locexpr_baton **baton)
17670{
17671 struct objfile *objfile = cu->objfile;
17672 struct comp_unit_head *cu_header = &cu->header;
c906108c 17673 struct dwarf_block *blk;
98bfdba5
PA
17674 enum bfd_endian byte_order = (bfd_big_endian (objfile->obfd) ?
17675 BFD_ENDIAN_BIG : BFD_ENDIAN_LITTLE);
17676
17677 *value = 0;
17678 *bytes = NULL;
17679 *baton = NULL;
c906108c
SS
17680
17681 switch (attr->form)
17682 {
17683 case DW_FORM_addr:
3019eac3 17684 case DW_FORM_GNU_addr_index:
ac56253d 17685 {
ac56253d
TT
17686 gdb_byte *data;
17687
98bfdba5
PA
17688 if (TYPE_LENGTH (type) != cu_header->addr_size)
17689 dwarf2_const_value_length_mismatch_complaint (name,
ac56253d 17690 cu_header->addr_size,
98bfdba5 17691 TYPE_LENGTH (type));
ac56253d
TT
17692 /* Symbols of this form are reasonably rare, so we just
17693 piggyback on the existing location code rather than writing
17694 a new implementation of symbol_computed_ops. */
7919a973 17695 *baton = obstack_alloc (obstack, sizeof (struct dwarf2_locexpr_baton));
98bfdba5
PA
17696 (*baton)->per_cu = cu->per_cu;
17697 gdb_assert ((*baton)->per_cu);
ac56253d 17698
98bfdba5 17699 (*baton)->size = 2 + cu_header->addr_size;
7919a973 17700 data = obstack_alloc (obstack, (*baton)->size);
98bfdba5 17701 (*baton)->data = data;
ac56253d
TT
17702
17703 data[0] = DW_OP_addr;
17704 store_unsigned_integer (&data[1], cu_header->addr_size,
17705 byte_order, DW_ADDR (attr));
17706 data[cu_header->addr_size + 1] = DW_OP_stack_value;
ac56253d 17707 }
c906108c 17708 break;
4ac36638 17709 case DW_FORM_string:
93b5768b 17710 case DW_FORM_strp:
3019eac3 17711 case DW_FORM_GNU_str_index:
36586728 17712 case DW_FORM_GNU_strp_alt:
98bfdba5
PA
17713 /* DW_STRING is already allocated on the objfile obstack, point
17714 directly to it. */
d521ce57 17715 *bytes = (const gdb_byte *) DW_STRING (attr);
93b5768b 17716 break;
c906108c
SS
17717 case DW_FORM_block1:
17718 case DW_FORM_block2:
17719 case DW_FORM_block4:
17720 case DW_FORM_block:
2dc7f7b3 17721 case DW_FORM_exprloc:
c906108c 17722 blk = DW_BLOCK (attr);
98bfdba5
PA
17723 if (TYPE_LENGTH (type) != blk->size)
17724 dwarf2_const_value_length_mismatch_complaint (name, blk->size,
17725 TYPE_LENGTH (type));
17726 *bytes = blk->data;
c906108c 17727 break;
2df3850c
JM
17728
17729 /* The DW_AT_const_value attributes are supposed to carry the
17730 symbol's value "represented as it would be on the target
17731 architecture." By the time we get here, it's already been
17732 converted to host endianness, so we just need to sign- or
17733 zero-extend it as appropriate. */
17734 case DW_FORM_data1:
3aef2284 17735 *bytes = dwarf2_const_value_data (attr, obstack, cu, value, 8);
2df3850c 17736 break;
c906108c 17737 case DW_FORM_data2:
3aef2284 17738 *bytes = dwarf2_const_value_data (attr, obstack, cu, value, 16);
2df3850c 17739 break;
c906108c 17740 case DW_FORM_data4:
3aef2284 17741 *bytes = dwarf2_const_value_data (attr, obstack, cu, value, 32);
2df3850c 17742 break;
c906108c 17743 case DW_FORM_data8:
3aef2284 17744 *bytes = dwarf2_const_value_data (attr, obstack, cu, value, 64);
2df3850c
JM
17745 break;
17746
c906108c 17747 case DW_FORM_sdata:
98bfdba5 17748 *value = DW_SND (attr);
2df3850c
JM
17749 break;
17750
c906108c 17751 case DW_FORM_udata:
98bfdba5 17752 *value = DW_UNSND (attr);
c906108c 17753 break;
2df3850c 17754
c906108c 17755 default:
4d3c2250 17756 complaint (&symfile_complaints,
e2e0b3e5 17757 _("unsupported const value attribute form: '%s'"),
4d3c2250 17758 dwarf_form_name (attr->form));
98bfdba5 17759 *value = 0;
c906108c
SS
17760 break;
17761 }
17762}
17763
2df3850c 17764
98bfdba5
PA
17765/* Copy constant value from an attribute to a symbol. */
17766
2df3850c 17767static void
ff39bb5e 17768dwarf2_const_value (const struct attribute *attr, struct symbol *sym,
98bfdba5 17769 struct dwarf2_cu *cu)
2df3850c 17770{
98bfdba5
PA
17771 struct objfile *objfile = cu->objfile;
17772 struct comp_unit_head *cu_header = &cu->header;
12df843f 17773 LONGEST value;
d521ce57 17774 const gdb_byte *bytes;
98bfdba5 17775 struct dwarf2_locexpr_baton *baton;
2df3850c 17776
98bfdba5
PA
17777 dwarf2_const_value_attr (attr, SYMBOL_TYPE (sym),
17778 SYMBOL_PRINT_NAME (sym),
17779 &objfile->objfile_obstack, cu,
17780 &value, &bytes, &baton);
2df3850c 17781
98bfdba5
PA
17782 if (baton != NULL)
17783 {
98bfdba5 17784 SYMBOL_LOCATION_BATON (sym) = baton;
f1e6e072 17785 SYMBOL_ACLASS_INDEX (sym) = dwarf2_locexpr_index;
98bfdba5
PA
17786 }
17787 else if (bytes != NULL)
17788 {
17789 SYMBOL_VALUE_BYTES (sym) = bytes;
f1e6e072 17790 SYMBOL_ACLASS_INDEX (sym) = LOC_CONST_BYTES;
98bfdba5
PA
17791 }
17792 else
17793 {
17794 SYMBOL_VALUE (sym) = value;
f1e6e072 17795 SYMBOL_ACLASS_INDEX (sym) = LOC_CONST;
98bfdba5 17796 }
2df3850c
JM
17797}
17798
c906108c
SS
17799/* Return the type of the die in question using its DW_AT_type attribute. */
17800
17801static struct type *
e7c27a73 17802die_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 17803{
c906108c 17804 struct attribute *type_attr;
c906108c 17805
e142c38c 17806 type_attr = dwarf2_attr (die, DW_AT_type, cu);
c906108c
SS
17807 if (!type_attr)
17808 {
17809 /* A missing DW_AT_type represents a void type. */
46bf5051 17810 return objfile_type (cu->objfile)->builtin_void;
c906108c 17811 }
348e048f 17812
673bfd45 17813 return lookup_die_type (die, type_attr, cu);
c906108c
SS
17814}
17815
b4ba55a1
JB
17816/* True iff CU's producer generates GNAT Ada auxiliary information
17817 that allows to find parallel types through that information instead
17818 of having to do expensive parallel lookups by type name. */
17819
17820static int
17821need_gnat_info (struct dwarf2_cu *cu)
17822{
17823 /* FIXME: brobecker/2010-10-12: As of now, only the AdaCore version
17824 of GNAT produces this auxiliary information, without any indication
17825 that it is produced. Part of enhancing the FSF version of GNAT
17826 to produce that information will be to put in place an indicator
17827 that we can use in order to determine whether the descriptive type
17828 info is available or not. One suggestion that has been made is
17829 to use a new attribute, attached to the CU die. For now, assume
17830 that the descriptive type info is not available. */
17831 return 0;
17832}
17833
b4ba55a1
JB
17834/* Return the auxiliary type of the die in question using its
17835 DW_AT_GNAT_descriptive_type attribute. Returns NULL if the
17836 attribute is not present. */
17837
17838static struct type *
17839die_descriptive_type (struct die_info *die, struct dwarf2_cu *cu)
17840{
b4ba55a1 17841 struct attribute *type_attr;
b4ba55a1
JB
17842
17843 type_attr = dwarf2_attr (die, DW_AT_GNAT_descriptive_type, cu);
17844 if (!type_attr)
17845 return NULL;
17846
673bfd45 17847 return lookup_die_type (die, type_attr, cu);
b4ba55a1
JB
17848}
17849
17850/* If DIE has a descriptive_type attribute, then set the TYPE's
17851 descriptive type accordingly. */
17852
17853static void
17854set_descriptive_type (struct type *type, struct die_info *die,
17855 struct dwarf2_cu *cu)
17856{
17857 struct type *descriptive_type = die_descriptive_type (die, cu);
17858
17859 if (descriptive_type)
17860 {
17861 ALLOCATE_GNAT_AUX_TYPE (type);
17862 TYPE_DESCRIPTIVE_TYPE (type) = descriptive_type;
17863 }
17864}
17865
c906108c
SS
17866/* Return the containing type of the die in question using its
17867 DW_AT_containing_type attribute. */
17868
17869static struct type *
e7c27a73 17870die_containing_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 17871{
c906108c 17872 struct attribute *type_attr;
c906108c 17873
e142c38c 17874 type_attr = dwarf2_attr (die, DW_AT_containing_type, cu);
33ac96f0
JK
17875 if (!type_attr)
17876 error (_("Dwarf Error: Problem turning containing type into gdb type "
4262abfb 17877 "[in module %s]"), objfile_name (cu->objfile));
33ac96f0 17878
673bfd45 17879 return lookup_die_type (die, type_attr, cu);
c906108c
SS
17880}
17881
ac9ec31b
DE
17882/* Return an error marker type to use for the ill formed type in DIE/CU. */
17883
17884static struct type *
17885build_error_marker_type (struct dwarf2_cu *cu, struct die_info *die)
17886{
17887 struct objfile *objfile = dwarf2_per_objfile->objfile;
17888 char *message, *saved;
17889
17890 message = xstrprintf (_("<unknown type in %s, CU 0x%x, DIE 0x%x>"),
4262abfb 17891 objfile_name (objfile),
ac9ec31b
DE
17892 cu->header.offset.sect_off,
17893 die->offset.sect_off);
17894 saved = obstack_copy0 (&objfile->objfile_obstack,
17895 message, strlen (message));
17896 xfree (message);
17897
17898 return init_type (TYPE_CODE_ERROR, 0, 0, saved, objfile);
17899}
17900
673bfd45 17901/* Look up the type of DIE in CU using its type attribute ATTR.
ac9ec31b
DE
17902 ATTR must be one of: DW_AT_type, DW_AT_GNAT_descriptive_type,
17903 DW_AT_containing_type.
673bfd45
DE
17904 If there is no type substitute an error marker. */
17905
c906108c 17906static struct type *
ff39bb5e 17907lookup_die_type (struct die_info *die, const struct attribute *attr,
673bfd45 17908 struct dwarf2_cu *cu)
c906108c 17909{
bb5ed363 17910 struct objfile *objfile = cu->objfile;
f792889a
DJ
17911 struct type *this_type;
17912
ac9ec31b
DE
17913 gdb_assert (attr->name == DW_AT_type
17914 || attr->name == DW_AT_GNAT_descriptive_type
17915 || attr->name == DW_AT_containing_type);
17916
673bfd45
DE
17917 /* First see if we have it cached. */
17918
36586728
TT
17919 if (attr->form == DW_FORM_GNU_ref_alt)
17920 {
17921 struct dwarf2_per_cu_data *per_cu;
17922 sect_offset offset = dwarf2_get_ref_die_offset (attr);
17923
17924 per_cu = dwarf2_find_containing_comp_unit (offset, 1, cu->objfile);
17925 this_type = get_die_type_at_offset (offset, per_cu);
17926 }
7771576e 17927 else if (attr_form_is_ref (attr))
673bfd45 17928 {
b64f50a1 17929 sect_offset offset = dwarf2_get_ref_die_offset (attr);
673bfd45
DE
17930
17931 this_type = get_die_type_at_offset (offset, cu->per_cu);
17932 }
55f1336d 17933 else if (attr->form == DW_FORM_ref_sig8)
673bfd45 17934 {
ac9ec31b 17935 ULONGEST signature = DW_SIGNATURE (attr);
673bfd45 17936
ac9ec31b 17937 return get_signatured_type (die, signature, cu);
673bfd45
DE
17938 }
17939 else
17940 {
ac9ec31b
DE
17941 complaint (&symfile_complaints,
17942 _("Dwarf Error: Bad type attribute %s in DIE"
17943 " at 0x%x [in module %s]"),
17944 dwarf_attr_name (attr->name), die->offset.sect_off,
4262abfb 17945 objfile_name (objfile));
ac9ec31b 17946 return build_error_marker_type (cu, die);
673bfd45
DE
17947 }
17948
17949 /* If not cached we need to read it in. */
17950
17951 if (this_type == NULL)
17952 {
ac9ec31b 17953 struct die_info *type_die = NULL;
673bfd45
DE
17954 struct dwarf2_cu *type_cu = cu;
17955
7771576e 17956 if (attr_form_is_ref (attr))
ac9ec31b
DE
17957 type_die = follow_die_ref (die, attr, &type_cu);
17958 if (type_die == NULL)
17959 return build_error_marker_type (cu, die);
17960 /* If we find the type now, it's probably because the type came
3019eac3
DE
17961 from an inter-CU reference and the type's CU got expanded before
17962 ours. */
ac9ec31b 17963 this_type = read_type_die (type_die, type_cu);
673bfd45
DE
17964 }
17965
17966 /* If we still don't have a type use an error marker. */
17967
17968 if (this_type == NULL)
ac9ec31b 17969 return build_error_marker_type (cu, die);
673bfd45 17970
f792889a 17971 return this_type;
c906108c
SS
17972}
17973
673bfd45
DE
17974/* Return the type in DIE, CU.
17975 Returns NULL for invalid types.
17976
02142a6c 17977 This first does a lookup in die_type_hash,
673bfd45
DE
17978 and only reads the die in if necessary.
17979
17980 NOTE: This can be called when reading in partial or full symbols. */
17981
f792889a 17982static struct type *
e7c27a73 17983read_type_die (struct die_info *die, struct dwarf2_cu *cu)
c906108c 17984{
f792889a
DJ
17985 struct type *this_type;
17986
17987 this_type = get_die_type (die, cu);
17988 if (this_type)
17989 return this_type;
17990
673bfd45
DE
17991 return read_type_die_1 (die, cu);
17992}
17993
17994/* Read the type in DIE, CU.
17995 Returns NULL for invalid types. */
17996
17997static struct type *
17998read_type_die_1 (struct die_info *die, struct dwarf2_cu *cu)
17999{
18000 struct type *this_type = NULL;
18001
c906108c
SS
18002 switch (die->tag)
18003 {
18004 case DW_TAG_class_type:
680b30c7 18005 case DW_TAG_interface_type:
c906108c
SS
18006 case DW_TAG_structure_type:
18007 case DW_TAG_union_type:
f792889a 18008 this_type = read_structure_type (die, cu);
c906108c
SS
18009 break;
18010 case DW_TAG_enumeration_type:
f792889a 18011 this_type = read_enumeration_type (die, cu);
c906108c
SS
18012 break;
18013 case DW_TAG_subprogram:
18014 case DW_TAG_subroutine_type:
edb3359d 18015 case DW_TAG_inlined_subroutine:
f792889a 18016 this_type = read_subroutine_type (die, cu);
c906108c
SS
18017 break;
18018 case DW_TAG_array_type:
f792889a 18019 this_type = read_array_type (die, cu);
c906108c 18020 break;
72019c9c 18021 case DW_TAG_set_type:
f792889a 18022 this_type = read_set_type (die, cu);
72019c9c 18023 break;
c906108c 18024 case DW_TAG_pointer_type:
f792889a 18025 this_type = read_tag_pointer_type (die, cu);
c906108c
SS
18026 break;
18027 case DW_TAG_ptr_to_member_type:
f792889a 18028 this_type = read_tag_ptr_to_member_type (die, cu);
c906108c
SS
18029 break;
18030 case DW_TAG_reference_type:
f792889a 18031 this_type = read_tag_reference_type (die, cu);
c906108c
SS
18032 break;
18033 case DW_TAG_const_type:
f792889a 18034 this_type = read_tag_const_type (die, cu);
c906108c
SS
18035 break;
18036 case DW_TAG_volatile_type:
f792889a 18037 this_type = read_tag_volatile_type (die, cu);
c906108c 18038 break;
06d66ee9
TT
18039 case DW_TAG_restrict_type:
18040 this_type = read_tag_restrict_type (die, cu);
18041 break;
c906108c 18042 case DW_TAG_string_type:
f792889a 18043 this_type = read_tag_string_type (die, cu);
c906108c
SS
18044 break;
18045 case DW_TAG_typedef:
f792889a 18046 this_type = read_typedef (die, cu);
c906108c 18047 break;
a02abb62 18048 case DW_TAG_subrange_type:
f792889a 18049 this_type = read_subrange_type (die, cu);
a02abb62 18050 break;
c906108c 18051 case DW_TAG_base_type:
f792889a 18052 this_type = read_base_type (die, cu);
c906108c 18053 break;
81a17f79 18054 case DW_TAG_unspecified_type:
f792889a 18055 this_type = read_unspecified_type (die, cu);
81a17f79 18056 break;
0114d602
DJ
18057 case DW_TAG_namespace:
18058 this_type = read_namespace_type (die, cu);
18059 break;
f55ee35c
JK
18060 case DW_TAG_module:
18061 this_type = read_module_type (die, cu);
18062 break;
c906108c 18063 default:
3e43a32a
MS
18064 complaint (&symfile_complaints,
18065 _("unexpected tag in read_type_die: '%s'"),
4d3c2250 18066 dwarf_tag_name (die->tag));
c906108c
SS
18067 break;
18068 }
63d06c5c 18069
f792889a 18070 return this_type;
63d06c5c
DC
18071}
18072
abc72ce4
DE
18073/* See if we can figure out if the class lives in a namespace. We do
18074 this by looking for a member function; its demangled name will
18075 contain namespace info, if there is any.
18076 Return the computed name or NULL.
18077 Space for the result is allocated on the objfile's obstack.
18078 This is the full-die version of guess_partial_die_structure_name.
18079 In this case we know DIE has no useful parent. */
18080
18081static char *
18082guess_full_die_structure_name (struct die_info *die, struct dwarf2_cu *cu)
18083{
18084 struct die_info *spec_die;
18085 struct dwarf2_cu *spec_cu;
18086 struct die_info *child;
18087
18088 spec_cu = cu;
18089 spec_die = die_specification (die, &spec_cu);
18090 if (spec_die != NULL)
18091 {
18092 die = spec_die;
18093 cu = spec_cu;
18094 }
18095
18096 for (child = die->child;
18097 child != NULL;
18098 child = child->sibling)
18099 {
18100 if (child->tag == DW_TAG_subprogram)
18101 {
18102 struct attribute *attr;
18103
18104 attr = dwarf2_attr (child, DW_AT_linkage_name, cu);
18105 if (attr == NULL)
18106 attr = dwarf2_attr (child, DW_AT_MIPS_linkage_name, cu);
18107 if (attr != NULL)
18108 {
18109 char *actual_name
18110 = language_class_name_from_physname (cu->language_defn,
18111 DW_STRING (attr));
18112 char *name = NULL;
18113
18114 if (actual_name != NULL)
18115 {
15d034d0 18116 const char *die_name = dwarf2_name (die, cu);
abc72ce4
DE
18117
18118 if (die_name != NULL
18119 && strcmp (die_name, actual_name) != 0)
18120 {
18121 /* Strip off the class name from the full name.
18122 We want the prefix. */
18123 int die_name_len = strlen (die_name);
18124 int actual_name_len = strlen (actual_name);
18125
18126 /* Test for '::' as a sanity check. */
18127 if (actual_name_len > die_name_len + 2
3e43a32a
MS
18128 && actual_name[actual_name_len
18129 - die_name_len - 1] == ':')
abc72ce4 18130 name =
10f0c4bb
TT
18131 obstack_copy0 (&cu->objfile->objfile_obstack,
18132 actual_name,
18133 actual_name_len - die_name_len - 2);
abc72ce4
DE
18134 }
18135 }
18136 xfree (actual_name);
18137 return name;
18138 }
18139 }
18140 }
18141
18142 return NULL;
18143}
18144
96408a79
SA
18145/* GCC might emit a nameless typedef that has a linkage name. Determine the
18146 prefix part in such case. See
18147 http://gcc.gnu.org/bugzilla/show_bug.cgi?id=47510. */
18148
18149static char *
18150anonymous_struct_prefix (struct die_info *die, struct dwarf2_cu *cu)
18151{
18152 struct attribute *attr;
18153 char *base;
18154
18155 if (die->tag != DW_TAG_class_type && die->tag != DW_TAG_interface_type
18156 && die->tag != DW_TAG_structure_type && die->tag != DW_TAG_union_type)
18157 return NULL;
18158
18159 attr = dwarf2_attr (die, DW_AT_name, cu);
18160 if (attr != NULL && DW_STRING (attr) != NULL)
18161 return NULL;
18162
18163 attr = dwarf2_attr (die, DW_AT_linkage_name, cu);
18164 if (attr == NULL)
18165 attr = dwarf2_attr (die, DW_AT_MIPS_linkage_name, cu);
18166 if (attr == NULL || DW_STRING (attr) == NULL)
18167 return NULL;
18168
18169 /* dwarf2_name had to be already called. */
18170 gdb_assert (DW_STRING_IS_CANONICAL (attr));
18171
18172 /* Strip the base name, keep any leading namespaces/classes. */
18173 base = strrchr (DW_STRING (attr), ':');
18174 if (base == NULL || base == DW_STRING (attr) || base[-1] != ':')
18175 return "";
18176
10f0c4bb
TT
18177 return obstack_copy0 (&cu->objfile->objfile_obstack,
18178 DW_STRING (attr), &base[-1] - DW_STRING (attr));
96408a79
SA
18179}
18180
fdde2d81 18181/* Return the name of the namespace/class that DIE is defined within,
0114d602 18182 or "" if we can't tell. The caller should not xfree the result.
fdde2d81 18183
0114d602
DJ
18184 For example, if we're within the method foo() in the following
18185 code:
18186
18187 namespace N {
18188 class C {
18189 void foo () {
18190 }
18191 };
18192 }
18193
18194 then determine_prefix on foo's die will return "N::C". */
fdde2d81 18195
0d5cff50 18196static const char *
e142c38c 18197determine_prefix (struct die_info *die, struct dwarf2_cu *cu)
63d06c5c 18198{
0114d602
DJ
18199 struct die_info *parent, *spec_die;
18200 struct dwarf2_cu *spec_cu;
18201 struct type *parent_type;
96408a79 18202 char *retval;
63d06c5c 18203
f55ee35c
JK
18204 if (cu->language != language_cplus && cu->language != language_java
18205 && cu->language != language_fortran)
0114d602
DJ
18206 return "";
18207
96408a79
SA
18208 retval = anonymous_struct_prefix (die, cu);
18209 if (retval)
18210 return retval;
18211
0114d602
DJ
18212 /* We have to be careful in the presence of DW_AT_specification.
18213 For example, with GCC 3.4, given the code
18214
18215 namespace N {
18216 void foo() {
18217 // Definition of N::foo.
18218 }
18219 }
18220
18221 then we'll have a tree of DIEs like this:
18222
18223 1: DW_TAG_compile_unit
18224 2: DW_TAG_namespace // N
18225 3: DW_TAG_subprogram // declaration of N::foo
18226 4: DW_TAG_subprogram // definition of N::foo
18227 DW_AT_specification // refers to die #3
18228
18229 Thus, when processing die #4, we have to pretend that we're in
18230 the context of its DW_AT_specification, namely the contex of die
18231 #3. */
18232 spec_cu = cu;
18233 spec_die = die_specification (die, &spec_cu);
18234 if (spec_die == NULL)
18235 parent = die->parent;
18236 else
63d06c5c 18237 {
0114d602
DJ
18238 parent = spec_die->parent;
18239 cu = spec_cu;
63d06c5c 18240 }
0114d602
DJ
18241
18242 if (parent == NULL)
18243 return "";
98bfdba5
PA
18244 else if (parent->building_fullname)
18245 {
18246 const char *name;
18247 const char *parent_name;
18248
18249 /* It has been seen on RealView 2.2 built binaries,
18250 DW_TAG_template_type_param types actually _defined_ as
18251 children of the parent class:
18252
18253 enum E {};
18254 template class <class Enum> Class{};
18255 Class<enum E> class_e;
18256
18257 1: DW_TAG_class_type (Class)
18258 2: DW_TAG_enumeration_type (E)
18259 3: DW_TAG_enumerator (enum1:0)
18260 3: DW_TAG_enumerator (enum2:1)
18261 ...
18262 2: DW_TAG_template_type_param
18263 DW_AT_type DW_FORM_ref_udata (E)
18264
18265 Besides being broken debug info, it can put GDB into an
18266 infinite loop. Consider:
18267
18268 When we're building the full name for Class<E>, we'll start
18269 at Class, and go look over its template type parameters,
18270 finding E. We'll then try to build the full name of E, and
18271 reach here. We're now trying to build the full name of E,
18272 and look over the parent DIE for containing scope. In the
18273 broken case, if we followed the parent DIE of E, we'd again
18274 find Class, and once again go look at its template type
18275 arguments, etc., etc. Simply don't consider such parent die
18276 as source-level parent of this die (it can't be, the language
18277 doesn't allow it), and break the loop here. */
18278 name = dwarf2_name (die, cu);
18279 parent_name = dwarf2_name (parent, cu);
18280 complaint (&symfile_complaints,
18281 _("template param type '%s' defined within parent '%s'"),
18282 name ? name : "<unknown>",
18283 parent_name ? parent_name : "<unknown>");
18284 return "";
18285 }
63d06c5c 18286 else
0114d602
DJ
18287 switch (parent->tag)
18288 {
63d06c5c 18289 case DW_TAG_namespace:
0114d602 18290 parent_type = read_type_die (parent, cu);
acebe513
UW
18291 /* GCC 4.0 and 4.1 had a bug (PR c++/28460) where they generated bogus
18292 DW_TAG_namespace DIEs with a name of "::" for the global namespace.
18293 Work around this problem here. */
18294 if (cu->language == language_cplus
18295 && strcmp (TYPE_TAG_NAME (parent_type), "::") == 0)
18296 return "";
0114d602
DJ
18297 /* We give a name to even anonymous namespaces. */
18298 return TYPE_TAG_NAME (parent_type);
63d06c5c 18299 case DW_TAG_class_type:
680b30c7 18300 case DW_TAG_interface_type:
63d06c5c 18301 case DW_TAG_structure_type:
0114d602 18302 case DW_TAG_union_type:
f55ee35c 18303 case DW_TAG_module:
0114d602
DJ
18304 parent_type = read_type_die (parent, cu);
18305 if (TYPE_TAG_NAME (parent_type) != NULL)
18306 return TYPE_TAG_NAME (parent_type);
18307 else
18308 /* An anonymous structure is only allowed non-static data
18309 members; no typedefs, no member functions, et cetera.
18310 So it does not need a prefix. */
18311 return "";
abc72ce4 18312 case DW_TAG_compile_unit:
95554aad 18313 case DW_TAG_partial_unit:
abc72ce4
DE
18314 /* gcc-4.5 -gdwarf-4 can drop the enclosing namespace. Cope. */
18315 if (cu->language == language_cplus
8b70b953 18316 && !VEC_empty (dwarf2_section_info_def, dwarf2_per_objfile->types)
abc72ce4
DE
18317 && die->child != NULL
18318 && (die->tag == DW_TAG_class_type
18319 || die->tag == DW_TAG_structure_type
18320 || die->tag == DW_TAG_union_type))
18321 {
18322 char *name = guess_full_die_structure_name (die, cu);
18323 if (name != NULL)
18324 return name;
18325 }
18326 return "";
63d06c5c 18327 default:
8176b9b8 18328 return determine_prefix (parent, cu);
63d06c5c 18329 }
63d06c5c
DC
18330}
18331
3e43a32a
MS
18332/* Return a newly-allocated string formed by concatenating PREFIX and SUFFIX
18333 with appropriate separator. If PREFIX or SUFFIX is NULL or empty, then
18334 simply copy the SUFFIX or PREFIX, respectively. If OBS is non-null, perform
18335 an obconcat, otherwise allocate storage for the result. The CU argument is
18336 used to determine the language and hence, the appropriate separator. */
987504bb 18337
f55ee35c 18338#define MAX_SEP_LEN 7 /* strlen ("__") + strlen ("_MOD_") */
63d06c5c
DC
18339
18340static char *
f55ee35c
JK
18341typename_concat (struct obstack *obs, const char *prefix, const char *suffix,
18342 int physname, struct dwarf2_cu *cu)
63d06c5c 18343{
f55ee35c 18344 const char *lead = "";
5c315b68 18345 const char *sep;
63d06c5c 18346
3e43a32a
MS
18347 if (suffix == NULL || suffix[0] == '\0'
18348 || prefix == NULL || prefix[0] == '\0')
987504bb
JJ
18349 sep = "";
18350 else if (cu->language == language_java)
18351 sep = ".";
f55ee35c
JK
18352 else if (cu->language == language_fortran && physname)
18353 {
18354 /* This is gfortran specific mangling. Normally DW_AT_linkage_name or
18355 DW_AT_MIPS_linkage_name is preferred and used instead. */
18356
18357 lead = "__";
18358 sep = "_MOD_";
18359 }
987504bb
JJ
18360 else
18361 sep = "::";
63d06c5c 18362
6dd47d34
DE
18363 if (prefix == NULL)
18364 prefix = "";
18365 if (suffix == NULL)
18366 suffix = "";
18367
987504bb
JJ
18368 if (obs == NULL)
18369 {
3e43a32a
MS
18370 char *retval
18371 = xmalloc (strlen (prefix) + MAX_SEP_LEN + strlen (suffix) + 1);
9a619af0 18372
f55ee35c
JK
18373 strcpy (retval, lead);
18374 strcat (retval, prefix);
6dd47d34
DE
18375 strcat (retval, sep);
18376 strcat (retval, suffix);
63d06c5c
DC
18377 return retval;
18378 }
987504bb
JJ
18379 else
18380 {
18381 /* We have an obstack. */
f55ee35c 18382 return obconcat (obs, lead, prefix, sep, suffix, (char *) NULL);
987504bb 18383 }
63d06c5c
DC
18384}
18385
c906108c
SS
18386/* Return sibling of die, NULL if no sibling. */
18387
f9aca02d 18388static struct die_info *
fba45db2 18389sibling_die (struct die_info *die)
c906108c 18390{
639d11d3 18391 return die->sibling;
c906108c
SS
18392}
18393
71c25dea
TT
18394/* Get name of a die, return NULL if not found. */
18395
15d034d0
TT
18396static const char *
18397dwarf2_canonicalize_name (const char *name, struct dwarf2_cu *cu,
71c25dea
TT
18398 struct obstack *obstack)
18399{
18400 if (name && cu->language == language_cplus)
18401 {
18402 char *canon_name = cp_canonicalize_string (name);
18403
18404 if (canon_name != NULL)
18405 {
18406 if (strcmp (canon_name, name) != 0)
10f0c4bb 18407 name = obstack_copy0 (obstack, canon_name, strlen (canon_name));
71c25dea
TT
18408 xfree (canon_name);
18409 }
18410 }
18411
18412 return name;
c906108c
SS
18413}
18414
9219021c
DC
18415/* Get name of a die, return NULL if not found. */
18416
15d034d0 18417static const char *
e142c38c 18418dwarf2_name (struct die_info *die, struct dwarf2_cu *cu)
9219021c
DC
18419{
18420 struct attribute *attr;
18421
e142c38c 18422 attr = dwarf2_attr (die, DW_AT_name, cu);
53832f31
TT
18423 if ((!attr || !DW_STRING (attr))
18424 && die->tag != DW_TAG_class_type
18425 && die->tag != DW_TAG_interface_type
18426 && die->tag != DW_TAG_structure_type
18427 && die->tag != DW_TAG_union_type)
71c25dea
TT
18428 return NULL;
18429
18430 switch (die->tag)
18431 {
18432 case DW_TAG_compile_unit:
95554aad 18433 case DW_TAG_partial_unit:
71c25dea
TT
18434 /* Compilation units have a DW_AT_name that is a filename, not
18435 a source language identifier. */
18436 case DW_TAG_enumeration_type:
18437 case DW_TAG_enumerator:
18438 /* These tags always have simple identifiers already; no need
18439 to canonicalize them. */
18440 return DW_STRING (attr);
907af001 18441
418835cc
KS
18442 case DW_TAG_subprogram:
18443 /* Java constructors will all be named "<init>", so return
18444 the class name when we see this special case. */
18445 if (cu->language == language_java
18446 && DW_STRING (attr) != NULL
18447 && strcmp (DW_STRING (attr), "<init>") == 0)
18448 {
18449 struct dwarf2_cu *spec_cu = cu;
18450 struct die_info *spec_die;
18451
18452 /* GCJ will output '<init>' for Java constructor names.
18453 For this special case, return the name of the parent class. */
18454
18455 /* GCJ may output suprogram DIEs with AT_specification set.
18456 If so, use the name of the specified DIE. */
18457 spec_die = die_specification (die, &spec_cu);
18458 if (spec_die != NULL)
18459 return dwarf2_name (spec_die, spec_cu);
18460
18461 do
18462 {
18463 die = die->parent;
18464 if (die->tag == DW_TAG_class_type)
18465 return dwarf2_name (die, cu);
18466 }
95554aad
TT
18467 while (die->tag != DW_TAG_compile_unit
18468 && die->tag != DW_TAG_partial_unit);
418835cc 18469 }
907af001
UW
18470 break;
18471
18472 case DW_TAG_class_type:
18473 case DW_TAG_interface_type:
18474 case DW_TAG_structure_type:
18475 case DW_TAG_union_type:
18476 /* Some GCC versions emit spurious DW_AT_name attributes for unnamed
18477 structures or unions. These were of the form "._%d" in GCC 4.1,
18478 or simply "<anonymous struct>" or "<anonymous union>" in GCC 4.3
18479 and GCC 4.4. We work around this problem by ignoring these. */
53832f31
TT
18480 if (attr && DW_STRING (attr)
18481 && (strncmp (DW_STRING (attr), "._", 2) == 0
18482 || strncmp (DW_STRING (attr), "<anonymous", 10) == 0))
907af001 18483 return NULL;
53832f31
TT
18484
18485 /* GCC might emit a nameless typedef that has a linkage name. See
18486 http://gcc.gnu.org/bugzilla/show_bug.cgi?id=47510. */
18487 if (!attr || DW_STRING (attr) == NULL)
18488 {
df5c6c50 18489 char *demangled = NULL;
53832f31
TT
18490
18491 attr = dwarf2_attr (die, DW_AT_linkage_name, cu);
18492 if (attr == NULL)
18493 attr = dwarf2_attr (die, DW_AT_MIPS_linkage_name, cu);
18494
18495 if (attr == NULL || DW_STRING (attr) == NULL)
18496 return NULL;
18497
df5c6c50
JK
18498 /* Avoid demangling DW_STRING (attr) the second time on a second
18499 call for the same DIE. */
18500 if (!DW_STRING_IS_CANONICAL (attr))
8de20a37 18501 demangled = gdb_demangle (DW_STRING (attr), DMGL_TYPES);
53832f31
TT
18502
18503 if (demangled)
18504 {
96408a79
SA
18505 char *base;
18506
53832f31 18507 /* FIXME: we already did this for the partial symbol... */
10f0c4bb
TT
18508 DW_STRING (attr) = obstack_copy0 (&cu->objfile->objfile_obstack,
18509 demangled, strlen (demangled));
53832f31
TT
18510 DW_STRING_IS_CANONICAL (attr) = 1;
18511 xfree (demangled);
96408a79
SA
18512
18513 /* Strip any leading namespaces/classes, keep only the base name.
18514 DW_AT_name for named DIEs does not contain the prefixes. */
18515 base = strrchr (DW_STRING (attr), ':');
18516 if (base && base > DW_STRING (attr) && base[-1] == ':')
18517 return &base[1];
18518 else
18519 return DW_STRING (attr);
53832f31
TT
18520 }
18521 }
907af001
UW
18522 break;
18523
71c25dea 18524 default:
907af001
UW
18525 break;
18526 }
18527
18528 if (!DW_STRING_IS_CANONICAL (attr))
18529 {
18530 DW_STRING (attr)
18531 = dwarf2_canonicalize_name (DW_STRING (attr), cu,
18532 &cu->objfile->objfile_obstack);
18533 DW_STRING_IS_CANONICAL (attr) = 1;
71c25dea 18534 }
907af001 18535 return DW_STRING (attr);
9219021c
DC
18536}
18537
18538/* Return the die that this die in an extension of, or NULL if there
f2f0e013
DJ
18539 is none. *EXT_CU is the CU containing DIE on input, and the CU
18540 containing the return value on output. */
9219021c
DC
18541
18542static struct die_info *
f2f0e013 18543dwarf2_extension (struct die_info *die, struct dwarf2_cu **ext_cu)
9219021c
DC
18544{
18545 struct attribute *attr;
9219021c 18546
f2f0e013 18547 attr = dwarf2_attr (die, DW_AT_extension, *ext_cu);
9219021c
DC
18548 if (attr == NULL)
18549 return NULL;
18550
f2f0e013 18551 return follow_die_ref (die, attr, ext_cu);
9219021c
DC
18552}
18553
c906108c
SS
18554/* Convert a DIE tag into its string name. */
18555
f39c6ffd 18556static const char *
aa1ee363 18557dwarf_tag_name (unsigned tag)
c906108c 18558{
f39c6ffd
TT
18559 const char *name = get_DW_TAG_name (tag);
18560
18561 if (name == NULL)
18562 return "DW_TAG_<unknown>";
18563
18564 return name;
c906108c
SS
18565}
18566
18567/* Convert a DWARF attribute code into its string name. */
18568
f39c6ffd 18569static const char *
aa1ee363 18570dwarf_attr_name (unsigned attr)
c906108c 18571{
f39c6ffd
TT
18572 const char *name;
18573
c764a876 18574#ifdef MIPS /* collides with DW_AT_HP_block_index */
f39c6ffd
TT
18575 if (attr == DW_AT_MIPS_fde)
18576 return "DW_AT_MIPS_fde";
18577#else
18578 if (attr == DW_AT_HP_block_index)
18579 return "DW_AT_HP_block_index";
c764a876 18580#endif
f39c6ffd
TT
18581
18582 name = get_DW_AT_name (attr);
18583
18584 if (name == NULL)
18585 return "DW_AT_<unknown>";
18586
18587 return name;
c906108c
SS
18588}
18589
18590/* Convert a DWARF value form code into its string name. */
18591
f39c6ffd 18592static const char *
aa1ee363 18593dwarf_form_name (unsigned form)
c906108c 18594{
f39c6ffd
TT
18595 const char *name = get_DW_FORM_name (form);
18596
18597 if (name == NULL)
18598 return "DW_FORM_<unknown>";
18599
18600 return name;
c906108c
SS
18601}
18602
18603static char *
fba45db2 18604dwarf_bool_name (unsigned mybool)
c906108c
SS
18605{
18606 if (mybool)
18607 return "TRUE";
18608 else
18609 return "FALSE";
18610}
18611
18612/* Convert a DWARF type code into its string name. */
18613
f39c6ffd 18614static const char *
aa1ee363 18615dwarf_type_encoding_name (unsigned enc)
c906108c 18616{
f39c6ffd 18617 const char *name = get_DW_ATE_name (enc);
c906108c 18618
f39c6ffd
TT
18619 if (name == NULL)
18620 return "DW_ATE_<unknown>";
c906108c 18621
f39c6ffd 18622 return name;
c906108c 18623}
c906108c 18624
f9aca02d 18625static void
d97bc12b 18626dump_die_shallow (struct ui_file *f, int indent, struct die_info *die)
c906108c
SS
18627{
18628 unsigned int i;
18629
d97bc12b
DE
18630 print_spaces (indent, f);
18631 fprintf_unfiltered (f, "Die: %s (abbrev %d, offset 0x%x)\n",
b64f50a1 18632 dwarf_tag_name (die->tag), die->abbrev, die->offset.sect_off);
d97bc12b
DE
18633
18634 if (die->parent != NULL)
18635 {
18636 print_spaces (indent, f);
18637 fprintf_unfiltered (f, " parent at offset: 0x%x\n",
b64f50a1 18638 die->parent->offset.sect_off);
d97bc12b
DE
18639 }
18640
18641 print_spaces (indent, f);
18642 fprintf_unfiltered (f, " has children: %s\n",
639d11d3 18643 dwarf_bool_name (die->child != NULL));
c906108c 18644
d97bc12b
DE
18645 print_spaces (indent, f);
18646 fprintf_unfiltered (f, " attributes:\n");
18647
c906108c
SS
18648 for (i = 0; i < die->num_attrs; ++i)
18649 {
d97bc12b
DE
18650 print_spaces (indent, f);
18651 fprintf_unfiltered (f, " %s (%s) ",
c906108c
SS
18652 dwarf_attr_name (die->attrs[i].name),
18653 dwarf_form_name (die->attrs[i].form));
d97bc12b 18654
c906108c
SS
18655 switch (die->attrs[i].form)
18656 {
c906108c 18657 case DW_FORM_addr:
3019eac3 18658 case DW_FORM_GNU_addr_index:
d97bc12b 18659 fprintf_unfiltered (f, "address: ");
5af949e3 18660 fputs_filtered (hex_string (DW_ADDR (&die->attrs[i])), f);
c906108c
SS
18661 break;
18662 case DW_FORM_block2:
18663 case DW_FORM_block4:
18664 case DW_FORM_block:
18665 case DW_FORM_block1:
56eb65bd
SP
18666 fprintf_unfiltered (f, "block: size %s",
18667 pulongest (DW_BLOCK (&die->attrs[i])->size));
c906108c 18668 break;
2dc7f7b3 18669 case DW_FORM_exprloc:
56eb65bd
SP
18670 fprintf_unfiltered (f, "expression: size %s",
18671 pulongest (DW_BLOCK (&die->attrs[i])->size));
2dc7f7b3 18672 break;
4568ecf9
DE
18673 case DW_FORM_ref_addr:
18674 fprintf_unfiltered (f, "ref address: ");
18675 fputs_filtered (hex_string (DW_UNSND (&die->attrs[i])), f);
18676 break;
36586728
TT
18677 case DW_FORM_GNU_ref_alt:
18678 fprintf_unfiltered (f, "alt ref address: ");
18679 fputs_filtered (hex_string (DW_UNSND (&die->attrs[i])), f);
18680 break;
10b3939b
DJ
18681 case DW_FORM_ref1:
18682 case DW_FORM_ref2:
18683 case DW_FORM_ref4:
4568ecf9
DE
18684 case DW_FORM_ref8:
18685 case DW_FORM_ref_udata:
d97bc12b 18686 fprintf_unfiltered (f, "constant ref: 0x%lx (adjusted)",
4568ecf9 18687 (long) (DW_UNSND (&die->attrs[i])));
10b3939b 18688 break;
c906108c
SS
18689 case DW_FORM_data1:
18690 case DW_FORM_data2:
18691 case DW_FORM_data4:
ce5d95e1 18692 case DW_FORM_data8:
c906108c
SS
18693 case DW_FORM_udata:
18694 case DW_FORM_sdata:
43bbcdc2
PH
18695 fprintf_unfiltered (f, "constant: %s",
18696 pulongest (DW_UNSND (&die->attrs[i])));
c906108c 18697 break;
2dc7f7b3
TT
18698 case DW_FORM_sec_offset:
18699 fprintf_unfiltered (f, "section offset: %s",
18700 pulongest (DW_UNSND (&die->attrs[i])));
18701 break;
55f1336d 18702 case DW_FORM_ref_sig8:
ac9ec31b
DE
18703 fprintf_unfiltered (f, "signature: %s",
18704 hex_string (DW_SIGNATURE (&die->attrs[i])));
348e048f 18705 break;
c906108c 18706 case DW_FORM_string:
4bdf3d34 18707 case DW_FORM_strp:
3019eac3 18708 case DW_FORM_GNU_str_index:
36586728 18709 case DW_FORM_GNU_strp_alt:
8285870a 18710 fprintf_unfiltered (f, "string: \"%s\" (%s canonicalized)",
c906108c 18711 DW_STRING (&die->attrs[i])
8285870a
JK
18712 ? DW_STRING (&die->attrs[i]) : "",
18713 DW_STRING_IS_CANONICAL (&die->attrs[i]) ? "is" : "not");
c906108c
SS
18714 break;
18715 case DW_FORM_flag:
18716 if (DW_UNSND (&die->attrs[i]))
d97bc12b 18717 fprintf_unfiltered (f, "flag: TRUE");
c906108c 18718 else
d97bc12b 18719 fprintf_unfiltered (f, "flag: FALSE");
c906108c 18720 break;
2dc7f7b3
TT
18721 case DW_FORM_flag_present:
18722 fprintf_unfiltered (f, "flag: TRUE");
18723 break;
a8329558 18724 case DW_FORM_indirect:
0963b4bd
MS
18725 /* The reader will have reduced the indirect form to
18726 the "base form" so this form should not occur. */
3e43a32a
MS
18727 fprintf_unfiltered (f,
18728 "unexpected attribute form: DW_FORM_indirect");
a8329558 18729 break;
c906108c 18730 default:
d97bc12b 18731 fprintf_unfiltered (f, "unsupported attribute form: %d.",
c5aa993b 18732 die->attrs[i].form);
d97bc12b 18733 break;
c906108c 18734 }
d97bc12b 18735 fprintf_unfiltered (f, "\n");
c906108c
SS
18736 }
18737}
18738
f9aca02d 18739static void
d97bc12b 18740dump_die_for_error (struct die_info *die)
c906108c 18741{
d97bc12b
DE
18742 dump_die_shallow (gdb_stderr, 0, die);
18743}
18744
18745static void
18746dump_die_1 (struct ui_file *f, int level, int max_level, struct die_info *die)
18747{
18748 int indent = level * 4;
18749
18750 gdb_assert (die != NULL);
18751
18752 if (level >= max_level)
18753 return;
18754
18755 dump_die_shallow (f, indent, die);
18756
18757 if (die->child != NULL)
c906108c 18758 {
d97bc12b
DE
18759 print_spaces (indent, f);
18760 fprintf_unfiltered (f, " Children:");
18761 if (level + 1 < max_level)
18762 {
18763 fprintf_unfiltered (f, "\n");
18764 dump_die_1 (f, level + 1, max_level, die->child);
18765 }
18766 else
18767 {
3e43a32a
MS
18768 fprintf_unfiltered (f,
18769 " [not printed, max nesting level reached]\n");
d97bc12b
DE
18770 }
18771 }
18772
18773 if (die->sibling != NULL && level > 0)
18774 {
18775 dump_die_1 (f, level, max_level, die->sibling);
c906108c
SS
18776 }
18777}
18778
d97bc12b
DE
18779/* This is called from the pdie macro in gdbinit.in.
18780 It's not static so gcc will keep a copy callable from gdb. */
18781
18782void
18783dump_die (struct die_info *die, int max_level)
18784{
18785 dump_die_1 (gdb_stdlog, 0, max_level, die);
18786}
18787
f9aca02d 18788static void
51545339 18789store_in_ref_table (struct die_info *die, struct dwarf2_cu *cu)
c906108c 18790{
51545339 18791 void **slot;
c906108c 18792
b64f50a1
JK
18793 slot = htab_find_slot_with_hash (cu->die_hash, die, die->offset.sect_off,
18794 INSERT);
51545339
DJ
18795
18796 *slot = die;
c906108c
SS
18797}
18798
b64f50a1
JK
18799/* Return DIE offset of ATTR. Return 0 with complaint if ATTR is not of the
18800 required kind. */
18801
18802static sect_offset
ff39bb5e 18803dwarf2_get_ref_die_offset (const struct attribute *attr)
93311388 18804{
4568ecf9 18805 sect_offset retval = { DW_UNSND (attr) };
b64f50a1 18806
7771576e 18807 if (attr_form_is_ref (attr))
b64f50a1 18808 return retval;
93311388 18809
b64f50a1 18810 retval.sect_off = 0;
93311388
DE
18811 complaint (&symfile_complaints,
18812 _("unsupported die ref attribute form: '%s'"),
18813 dwarf_form_name (attr->form));
b64f50a1 18814 return retval;
c906108c
SS
18815}
18816
43bbcdc2
PH
18817/* Return the constant value held by ATTR. Return DEFAULT_VALUE if
18818 * the value held by the attribute is not constant. */
a02abb62 18819
43bbcdc2 18820static LONGEST
ff39bb5e 18821dwarf2_get_attr_constant_value (const struct attribute *attr, int default_value)
a02abb62
JB
18822{
18823 if (attr->form == DW_FORM_sdata)
18824 return DW_SND (attr);
18825 else if (attr->form == DW_FORM_udata
18826 || attr->form == DW_FORM_data1
18827 || attr->form == DW_FORM_data2
18828 || attr->form == DW_FORM_data4
18829 || attr->form == DW_FORM_data8)
18830 return DW_UNSND (attr);
18831 else
18832 {
3e43a32a
MS
18833 complaint (&symfile_complaints,
18834 _("Attribute value is not a constant (%s)"),
a02abb62
JB
18835 dwarf_form_name (attr->form));
18836 return default_value;
18837 }
18838}
18839
348e048f
DE
18840/* Follow reference or signature attribute ATTR of SRC_DIE.
18841 On entry *REF_CU is the CU of SRC_DIE.
18842 On exit *REF_CU is the CU of the result. */
18843
18844static struct die_info *
ff39bb5e 18845follow_die_ref_or_sig (struct die_info *src_die, const struct attribute *attr,
348e048f
DE
18846 struct dwarf2_cu **ref_cu)
18847{
18848 struct die_info *die;
18849
7771576e 18850 if (attr_form_is_ref (attr))
348e048f 18851 die = follow_die_ref (src_die, attr, ref_cu);
55f1336d 18852 else if (attr->form == DW_FORM_ref_sig8)
348e048f
DE
18853 die = follow_die_sig (src_die, attr, ref_cu);
18854 else
18855 {
18856 dump_die_for_error (src_die);
18857 error (_("Dwarf Error: Expected reference attribute [in module %s]"),
4262abfb 18858 objfile_name ((*ref_cu)->objfile));
348e048f
DE
18859 }
18860
18861 return die;
03dd20cc
DJ
18862}
18863
5c631832 18864/* Follow reference OFFSET.
673bfd45
DE
18865 On entry *REF_CU is the CU of the source die referencing OFFSET.
18866 On exit *REF_CU is the CU of the result.
18867 Returns NULL if OFFSET is invalid. */
f504f079 18868
f9aca02d 18869static struct die_info *
36586728
TT
18870follow_die_offset (sect_offset offset, int offset_in_dwz,
18871 struct dwarf2_cu **ref_cu)
c906108c 18872{
10b3939b 18873 struct die_info temp_die;
f2f0e013 18874 struct dwarf2_cu *target_cu, *cu = *ref_cu;
10b3939b 18875
348e048f
DE
18876 gdb_assert (cu->per_cu != NULL);
18877
98bfdba5
PA
18878 target_cu = cu;
18879
3019eac3 18880 if (cu->per_cu->is_debug_types)
348e048f
DE
18881 {
18882 /* .debug_types CUs cannot reference anything outside their CU.
18883 If they need to, they have to reference a signatured type via
55f1336d 18884 DW_FORM_ref_sig8. */
348e048f 18885 if (! offset_in_cu_p (&cu->header, offset))
5c631832 18886 return NULL;
348e048f 18887 }
36586728
TT
18888 else if (offset_in_dwz != cu->per_cu->is_dwz
18889 || ! offset_in_cu_p (&cu->header, offset))
10b3939b
DJ
18890 {
18891 struct dwarf2_per_cu_data *per_cu;
9a619af0 18892
36586728
TT
18893 per_cu = dwarf2_find_containing_comp_unit (offset, offset_in_dwz,
18894 cu->objfile);
03dd20cc
DJ
18895
18896 /* If necessary, add it to the queue and load its DIEs. */
95554aad
TT
18897 if (maybe_queue_comp_unit (cu, per_cu, cu->language))
18898 load_full_comp_unit (per_cu, cu->language);
03dd20cc 18899
10b3939b
DJ
18900 target_cu = per_cu->cu;
18901 }
98bfdba5
PA
18902 else if (cu->dies == NULL)
18903 {
18904 /* We're loading full DIEs during partial symbol reading. */
18905 gdb_assert (dwarf2_per_objfile->reading_partial_symbols);
95554aad 18906 load_full_comp_unit (cu->per_cu, language_minimal);
98bfdba5 18907 }
c906108c 18908
f2f0e013 18909 *ref_cu = target_cu;
51545339 18910 temp_die.offset = offset;
b64f50a1 18911 return htab_find_with_hash (target_cu->die_hash, &temp_die, offset.sect_off);
5c631832 18912}
10b3939b 18913
5c631832
JK
18914/* Follow reference attribute ATTR of SRC_DIE.
18915 On entry *REF_CU is the CU of SRC_DIE.
18916 On exit *REF_CU is the CU of the result. */
18917
18918static struct die_info *
ff39bb5e 18919follow_die_ref (struct die_info *src_die, const struct attribute *attr,
5c631832
JK
18920 struct dwarf2_cu **ref_cu)
18921{
b64f50a1 18922 sect_offset offset = dwarf2_get_ref_die_offset (attr);
5c631832
JK
18923 struct dwarf2_cu *cu = *ref_cu;
18924 struct die_info *die;
18925
36586728
TT
18926 die = follow_die_offset (offset,
18927 (attr->form == DW_FORM_GNU_ref_alt
18928 || cu->per_cu->is_dwz),
18929 ref_cu);
5c631832
JK
18930 if (!die)
18931 error (_("Dwarf Error: Cannot find DIE at 0x%x referenced from DIE "
18932 "at 0x%x [in module %s]"),
4262abfb
JK
18933 offset.sect_off, src_die->offset.sect_off,
18934 objfile_name (cu->objfile));
348e048f 18935
5c631832
JK
18936 return die;
18937}
18938
d83e736b
JK
18939/* Return DWARF block referenced by DW_AT_location of DIE at OFFSET at PER_CU.
18940 Returned value is intended for DW_OP_call*. Returned
18941 dwarf2_locexpr_baton->data has lifetime of PER_CU->OBJFILE. */
5c631832
JK
18942
18943struct dwarf2_locexpr_baton
8b9737bf
TT
18944dwarf2_fetch_die_loc_sect_off (sect_offset offset,
18945 struct dwarf2_per_cu_data *per_cu,
18946 CORE_ADDR (*get_frame_pc) (void *baton),
18947 void *baton)
5c631832 18948{
918dd910 18949 struct dwarf2_cu *cu;
5c631832
JK
18950 struct die_info *die;
18951 struct attribute *attr;
18952 struct dwarf2_locexpr_baton retval;
18953
8cf6f0b1
TT
18954 dw2_setup (per_cu->objfile);
18955
918dd910
JK
18956 if (per_cu->cu == NULL)
18957 load_cu (per_cu);
18958 cu = per_cu->cu;
18959
36586728 18960 die = follow_die_offset (offset, per_cu->is_dwz, &cu);
5c631832
JK
18961 if (!die)
18962 error (_("Dwarf Error: Cannot find DIE at 0x%x referenced in module %s"),
4262abfb 18963 offset.sect_off, objfile_name (per_cu->objfile));
5c631832
JK
18964
18965 attr = dwarf2_attr (die, DW_AT_location, cu);
18966 if (!attr)
18967 {
e103e986
JK
18968 /* DWARF: "If there is no such attribute, then there is no effect.".
18969 DATA is ignored if SIZE is 0. */
5c631832 18970
e103e986 18971 retval.data = NULL;
5c631832
JK
18972 retval.size = 0;
18973 }
8cf6f0b1
TT
18974 else if (attr_form_is_section_offset (attr))
18975 {
18976 struct dwarf2_loclist_baton loclist_baton;
18977 CORE_ADDR pc = (*get_frame_pc) (baton);
18978 size_t size;
18979
18980 fill_in_loclist_baton (cu, &loclist_baton, attr);
18981
18982 retval.data = dwarf2_find_location_expression (&loclist_baton,
18983 &size, pc);
18984 retval.size = size;
18985 }
5c631832
JK
18986 else
18987 {
18988 if (!attr_form_is_block (attr))
18989 error (_("Dwarf Error: DIE at 0x%x referenced in module %s "
18990 "is neither DW_FORM_block* nor DW_FORM_exprloc"),
4262abfb 18991 offset.sect_off, objfile_name (per_cu->objfile));
5c631832
JK
18992
18993 retval.data = DW_BLOCK (attr)->data;
18994 retval.size = DW_BLOCK (attr)->size;
18995 }
18996 retval.per_cu = cu->per_cu;
918dd910 18997
918dd910
JK
18998 age_cached_comp_units ();
18999
5c631832 19000 return retval;
348e048f
DE
19001}
19002
8b9737bf
TT
19003/* Like dwarf2_fetch_die_loc_sect_off, but take a CU
19004 offset. */
19005
19006struct dwarf2_locexpr_baton
19007dwarf2_fetch_die_loc_cu_off (cu_offset offset_in_cu,
19008 struct dwarf2_per_cu_data *per_cu,
19009 CORE_ADDR (*get_frame_pc) (void *baton),
19010 void *baton)
19011{
19012 sect_offset offset = { per_cu->offset.sect_off + offset_in_cu.cu_off };
19013
19014 return dwarf2_fetch_die_loc_sect_off (offset, per_cu, get_frame_pc, baton);
19015}
19016
b6807d98
TT
19017/* Write a constant of a given type as target-ordered bytes into
19018 OBSTACK. */
19019
19020static const gdb_byte *
19021write_constant_as_bytes (struct obstack *obstack,
19022 enum bfd_endian byte_order,
19023 struct type *type,
19024 ULONGEST value,
19025 LONGEST *len)
19026{
19027 gdb_byte *result;
19028
19029 *len = TYPE_LENGTH (type);
19030 result = obstack_alloc (obstack, *len);
19031 store_unsigned_integer (result, *len, byte_order, value);
19032
19033 return result;
19034}
19035
19036/* If the DIE at OFFSET in PER_CU has a DW_AT_const_value, return a
19037 pointer to the constant bytes and set LEN to the length of the
19038 data. If memory is needed, allocate it on OBSTACK. If the DIE
19039 does not have a DW_AT_const_value, return NULL. */
19040
19041const gdb_byte *
19042dwarf2_fetch_constant_bytes (sect_offset offset,
19043 struct dwarf2_per_cu_data *per_cu,
19044 struct obstack *obstack,
19045 LONGEST *len)
19046{
19047 struct dwarf2_cu *cu;
19048 struct die_info *die;
19049 struct attribute *attr;
19050 const gdb_byte *result = NULL;
19051 struct type *type;
19052 LONGEST value;
19053 enum bfd_endian byte_order;
19054
19055 dw2_setup (per_cu->objfile);
19056
19057 if (per_cu->cu == NULL)
19058 load_cu (per_cu);
19059 cu = per_cu->cu;
19060
19061 die = follow_die_offset (offset, per_cu->is_dwz, &cu);
19062 if (!die)
19063 error (_("Dwarf Error: Cannot find DIE at 0x%x referenced in module %s"),
4262abfb 19064 offset.sect_off, objfile_name (per_cu->objfile));
b6807d98
TT
19065
19066
19067 attr = dwarf2_attr (die, DW_AT_const_value, cu);
19068 if (attr == NULL)
19069 return NULL;
19070
19071 byte_order = (bfd_big_endian (per_cu->objfile->obfd)
19072 ? BFD_ENDIAN_BIG : BFD_ENDIAN_LITTLE);
19073
19074 switch (attr->form)
19075 {
19076 case DW_FORM_addr:
19077 case DW_FORM_GNU_addr_index:
19078 {
19079 gdb_byte *tem;
19080
19081 *len = cu->header.addr_size;
19082 tem = obstack_alloc (obstack, *len);
19083 store_unsigned_integer (tem, *len, byte_order, DW_ADDR (attr));
19084 result = tem;
19085 }
19086 break;
19087 case DW_FORM_string:
19088 case DW_FORM_strp:
19089 case DW_FORM_GNU_str_index:
19090 case DW_FORM_GNU_strp_alt:
19091 /* DW_STRING is already allocated on the objfile obstack, point
19092 directly to it. */
19093 result = (const gdb_byte *) DW_STRING (attr);
19094 *len = strlen (DW_STRING (attr));
19095 break;
19096 case DW_FORM_block1:
19097 case DW_FORM_block2:
19098 case DW_FORM_block4:
19099 case DW_FORM_block:
19100 case DW_FORM_exprloc:
19101 result = DW_BLOCK (attr)->data;
19102 *len = DW_BLOCK (attr)->size;
19103 break;
19104
19105 /* The DW_AT_const_value attributes are supposed to carry the
19106 symbol's value "represented as it would be on the target
19107 architecture." By the time we get here, it's already been
19108 converted to host endianness, so we just need to sign- or
19109 zero-extend it as appropriate. */
19110 case DW_FORM_data1:
19111 type = die_type (die, cu);
19112 result = dwarf2_const_value_data (attr, obstack, cu, &value, 8);
19113 if (result == NULL)
19114 result = write_constant_as_bytes (obstack, byte_order,
19115 type, value, len);
19116 break;
19117 case DW_FORM_data2:
19118 type = die_type (die, cu);
19119 result = dwarf2_const_value_data (attr, obstack, cu, &value, 16);
19120 if (result == NULL)
19121 result = write_constant_as_bytes (obstack, byte_order,
19122 type, value, len);
19123 break;
19124 case DW_FORM_data4:
19125 type = die_type (die, cu);
19126 result = dwarf2_const_value_data (attr, obstack, cu, &value, 32);
19127 if (result == NULL)
19128 result = write_constant_as_bytes (obstack, byte_order,
19129 type, value, len);
19130 break;
19131 case DW_FORM_data8:
19132 type = die_type (die, cu);
19133 result = dwarf2_const_value_data (attr, obstack, cu, &value, 64);
19134 if (result == NULL)
19135 result = write_constant_as_bytes (obstack, byte_order,
19136 type, value, len);
19137 break;
19138
19139 case DW_FORM_sdata:
19140 type = die_type (die, cu);
19141 result = write_constant_as_bytes (obstack, byte_order,
19142 type, DW_SND (attr), len);
19143 break;
19144
19145 case DW_FORM_udata:
19146 type = die_type (die, cu);
19147 result = write_constant_as_bytes (obstack, byte_order,
19148 type, DW_UNSND (attr), len);
19149 break;
19150
19151 default:
19152 complaint (&symfile_complaints,
19153 _("unsupported const value attribute form: '%s'"),
19154 dwarf_form_name (attr->form));
19155 break;
19156 }
19157
19158 return result;
19159}
19160
8a9b8146
TT
19161/* Return the type of the DIE at DIE_OFFSET in the CU named by
19162 PER_CU. */
19163
19164struct type *
b64f50a1 19165dwarf2_get_die_type (cu_offset die_offset,
8a9b8146
TT
19166 struct dwarf2_per_cu_data *per_cu)
19167{
b64f50a1
JK
19168 sect_offset die_offset_sect;
19169
8a9b8146 19170 dw2_setup (per_cu->objfile);
b64f50a1
JK
19171
19172 die_offset_sect.sect_off = per_cu->offset.sect_off + die_offset.cu_off;
19173 return get_die_type_at_offset (die_offset_sect, per_cu);
8a9b8146
TT
19174}
19175
ac9ec31b 19176/* Follow type unit SIG_TYPE referenced by SRC_DIE.
348e048f 19177 On entry *REF_CU is the CU of SRC_DIE.
ac9ec31b
DE
19178 On exit *REF_CU is the CU of the result.
19179 Returns NULL if the referenced DIE isn't found. */
348e048f
DE
19180
19181static struct die_info *
ac9ec31b
DE
19182follow_die_sig_1 (struct die_info *src_die, struct signatured_type *sig_type,
19183 struct dwarf2_cu **ref_cu)
348e048f
DE
19184{
19185 struct objfile *objfile = (*ref_cu)->objfile;
19186 struct die_info temp_die;
348e048f
DE
19187 struct dwarf2_cu *sig_cu;
19188 struct die_info *die;
19189
ac9ec31b
DE
19190 /* While it might be nice to assert sig_type->type == NULL here,
19191 we can get here for DW_AT_imported_declaration where we need
19192 the DIE not the type. */
348e048f
DE
19193
19194 /* If necessary, add it to the queue and load its DIEs. */
19195
95554aad 19196 if (maybe_queue_comp_unit (*ref_cu, &sig_type->per_cu, language_minimal))
a0f42c21 19197 read_signatured_type (sig_type);
348e048f 19198
348e048f 19199 sig_cu = sig_type->per_cu.cu;
69d751e3 19200 gdb_assert (sig_cu != NULL);
3019eac3
DE
19201 gdb_assert (sig_type->type_offset_in_section.sect_off != 0);
19202 temp_die.offset = sig_type->type_offset_in_section;
b64f50a1
JK
19203 die = htab_find_with_hash (sig_cu->die_hash, &temp_die,
19204 temp_die.offset.sect_off);
348e048f
DE
19205 if (die)
19206 {
796a7ff8
DE
19207 /* For .gdb_index version 7 keep track of included TUs.
19208 http://sourceware.org/bugzilla/show_bug.cgi?id=15021. */
19209 if (dwarf2_per_objfile->index_table != NULL
19210 && dwarf2_per_objfile->index_table->version <= 7)
19211 {
19212 VEC_safe_push (dwarf2_per_cu_ptr,
19213 (*ref_cu)->per_cu->imported_symtabs,
19214 sig_cu->per_cu);
19215 }
19216
348e048f
DE
19217 *ref_cu = sig_cu;
19218 return die;
19219 }
19220
ac9ec31b
DE
19221 return NULL;
19222}
19223
19224/* Follow signatured type referenced by ATTR in SRC_DIE.
19225 On entry *REF_CU is the CU of SRC_DIE.
19226 On exit *REF_CU is the CU of the result.
19227 The result is the DIE of the type.
19228 If the referenced type cannot be found an error is thrown. */
19229
19230static struct die_info *
ff39bb5e 19231follow_die_sig (struct die_info *src_die, const struct attribute *attr,
ac9ec31b
DE
19232 struct dwarf2_cu **ref_cu)
19233{
19234 ULONGEST signature = DW_SIGNATURE (attr);
19235 struct signatured_type *sig_type;
19236 struct die_info *die;
19237
19238 gdb_assert (attr->form == DW_FORM_ref_sig8);
19239
a2ce51a0 19240 sig_type = lookup_signatured_type (*ref_cu, signature);
ac9ec31b
DE
19241 /* sig_type will be NULL if the signatured type is missing from
19242 the debug info. */
19243 if (sig_type == NULL)
19244 {
19245 error (_("Dwarf Error: Cannot find signatured DIE %s referenced"
19246 " from DIE at 0x%x [in module %s]"),
19247 hex_string (signature), src_die->offset.sect_off,
4262abfb 19248 objfile_name ((*ref_cu)->objfile));
ac9ec31b
DE
19249 }
19250
19251 die = follow_die_sig_1 (src_die, sig_type, ref_cu);
19252 if (die == NULL)
19253 {
19254 dump_die_for_error (src_die);
19255 error (_("Dwarf Error: Problem reading signatured DIE %s referenced"
19256 " from DIE at 0x%x [in module %s]"),
19257 hex_string (signature), src_die->offset.sect_off,
4262abfb 19258 objfile_name ((*ref_cu)->objfile));
ac9ec31b
DE
19259 }
19260
19261 return die;
19262}
19263
19264/* Get the type specified by SIGNATURE referenced in DIE/CU,
19265 reading in and processing the type unit if necessary. */
19266
19267static struct type *
19268get_signatured_type (struct die_info *die, ULONGEST signature,
19269 struct dwarf2_cu *cu)
19270{
19271 struct signatured_type *sig_type;
19272 struct dwarf2_cu *type_cu;
19273 struct die_info *type_die;
19274 struct type *type;
19275
a2ce51a0 19276 sig_type = lookup_signatured_type (cu, signature);
ac9ec31b
DE
19277 /* sig_type will be NULL if the signatured type is missing from
19278 the debug info. */
19279 if (sig_type == NULL)
19280 {
19281 complaint (&symfile_complaints,
19282 _("Dwarf Error: Cannot find signatured DIE %s referenced"
19283 " from DIE at 0x%x [in module %s]"),
19284 hex_string (signature), die->offset.sect_off,
4262abfb 19285 objfile_name (dwarf2_per_objfile->objfile));
ac9ec31b
DE
19286 return build_error_marker_type (cu, die);
19287 }
19288
19289 /* If we already know the type we're done. */
19290 if (sig_type->type != NULL)
19291 return sig_type->type;
19292
19293 type_cu = cu;
19294 type_die = follow_die_sig_1 (die, sig_type, &type_cu);
19295 if (type_die != NULL)
19296 {
19297 /* N.B. We need to call get_die_type to ensure only one type for this DIE
19298 is created. This is important, for example, because for c++ classes
19299 we need TYPE_NAME set which is only done by new_symbol. Blech. */
19300 type = read_type_die (type_die, type_cu);
19301 if (type == NULL)
19302 {
19303 complaint (&symfile_complaints,
19304 _("Dwarf Error: Cannot build signatured type %s"
19305 " referenced from DIE at 0x%x [in module %s]"),
19306 hex_string (signature), die->offset.sect_off,
4262abfb 19307 objfile_name (dwarf2_per_objfile->objfile));
ac9ec31b
DE
19308 type = build_error_marker_type (cu, die);
19309 }
19310 }
19311 else
19312 {
19313 complaint (&symfile_complaints,
19314 _("Dwarf Error: Problem reading signatured DIE %s referenced"
19315 " from DIE at 0x%x [in module %s]"),
19316 hex_string (signature), die->offset.sect_off,
4262abfb 19317 objfile_name (dwarf2_per_objfile->objfile));
ac9ec31b
DE
19318 type = build_error_marker_type (cu, die);
19319 }
19320 sig_type->type = type;
19321
19322 return type;
19323}
19324
19325/* Get the type specified by the DW_AT_signature ATTR in DIE/CU,
19326 reading in and processing the type unit if necessary. */
19327
19328static struct type *
ff39bb5e 19329get_DW_AT_signature_type (struct die_info *die, const struct attribute *attr,
b385a60d 19330 struct dwarf2_cu *cu) /* ARI: editCase function */
ac9ec31b
DE
19331{
19332 /* Yes, DW_AT_signature can use a non-ref_sig8 reference. */
7771576e 19333 if (attr_form_is_ref (attr))
ac9ec31b
DE
19334 {
19335 struct dwarf2_cu *type_cu = cu;
19336 struct die_info *type_die = follow_die_ref (die, attr, &type_cu);
19337
19338 return read_type_die (type_die, type_cu);
19339 }
19340 else if (attr->form == DW_FORM_ref_sig8)
19341 {
19342 return get_signatured_type (die, DW_SIGNATURE (attr), cu);
19343 }
19344 else
19345 {
19346 complaint (&symfile_complaints,
19347 _("Dwarf Error: DW_AT_signature has bad form %s in DIE"
19348 " at 0x%x [in module %s]"),
19349 dwarf_form_name (attr->form), die->offset.sect_off,
4262abfb 19350 objfile_name (dwarf2_per_objfile->objfile));
ac9ec31b
DE
19351 return build_error_marker_type (cu, die);
19352 }
348e048f
DE
19353}
19354
e5fe5e75 19355/* Load the DIEs associated with type unit PER_CU into memory. */
348e048f
DE
19356
19357static void
e5fe5e75 19358load_full_type_unit (struct dwarf2_per_cu_data *per_cu)
348e048f 19359{
52dc124a 19360 struct signatured_type *sig_type;
348e048f 19361
f4dc4d17
DE
19362 /* Caller is responsible for ensuring type_unit_groups don't get here. */
19363 gdb_assert (! IS_TYPE_UNIT_GROUP (per_cu));
19364
6721b2ec
DE
19365 /* We have the per_cu, but we need the signatured_type.
19366 Fortunately this is an easy translation. */
19367 gdb_assert (per_cu->is_debug_types);
19368 sig_type = (struct signatured_type *) per_cu;
348e048f 19369
6721b2ec 19370 gdb_assert (per_cu->cu == NULL);
348e048f 19371
52dc124a 19372 read_signatured_type (sig_type);
348e048f 19373
6721b2ec 19374 gdb_assert (per_cu->cu != NULL);
348e048f
DE
19375}
19376
dee91e82
DE
19377/* die_reader_func for read_signatured_type.
19378 This is identical to load_full_comp_unit_reader,
19379 but is kept separate for now. */
348e048f
DE
19380
19381static void
dee91e82 19382read_signatured_type_reader (const struct die_reader_specs *reader,
d521ce57 19383 const gdb_byte *info_ptr,
dee91e82
DE
19384 struct die_info *comp_unit_die,
19385 int has_children,
19386 void *data)
348e048f 19387{
dee91e82 19388 struct dwarf2_cu *cu = reader->cu;
348e048f 19389
dee91e82
DE
19390 gdb_assert (cu->die_hash == NULL);
19391 cu->die_hash =
19392 htab_create_alloc_ex (cu->header.length / 12,
19393 die_hash,
19394 die_eq,
19395 NULL,
19396 &cu->comp_unit_obstack,
19397 hashtab_obstack_allocate,
19398 dummy_obstack_deallocate);
348e048f 19399
dee91e82
DE
19400 if (has_children)
19401 comp_unit_die->child = read_die_and_siblings (reader, info_ptr,
19402 &info_ptr, comp_unit_die);
19403 cu->dies = comp_unit_die;
19404 /* comp_unit_die is not stored in die_hash, no need. */
348e048f
DE
19405
19406 /* We try not to read any attributes in this function, because not
9cdd5dbd 19407 all CUs needed for references have been loaded yet, and symbol
348e048f 19408 table processing isn't initialized. But we have to set the CU language,
dee91e82
DE
19409 or we won't be able to build types correctly.
19410 Similarly, if we do not read the producer, we can not apply
19411 producer-specific interpretation. */
95554aad 19412 prepare_one_comp_unit (cu, cu->dies, language_minimal);
dee91e82 19413}
348e048f 19414
3019eac3
DE
19415/* Read in a signatured type and build its CU and DIEs.
19416 If the type is a stub for the real type in a DWO file,
19417 read in the real type from the DWO file as well. */
dee91e82
DE
19418
19419static void
19420read_signatured_type (struct signatured_type *sig_type)
19421{
19422 struct dwarf2_per_cu_data *per_cu = &sig_type->per_cu;
348e048f 19423
3019eac3 19424 gdb_assert (per_cu->is_debug_types);
dee91e82 19425 gdb_assert (per_cu->cu == NULL);
348e048f 19426
f4dc4d17
DE
19427 init_cutu_and_read_dies (per_cu, NULL, 0, 1,
19428 read_signatured_type_reader, NULL);
7ee85ab1 19429 sig_type->per_cu.tu_read = 1;
c906108c
SS
19430}
19431
c906108c
SS
19432/* Decode simple location descriptions.
19433 Given a pointer to a dwarf block that defines a location, compute
19434 the location and return the value.
19435
4cecd739
DJ
19436 NOTE drow/2003-11-18: This function is called in two situations
19437 now: for the address of static or global variables (partial symbols
19438 only) and for offsets into structures which are expected to be
19439 (more or less) constant. The partial symbol case should go away,
19440 and only the constant case should remain. That will let this
19441 function complain more accurately. A few special modes are allowed
19442 without complaint for global variables (for instance, global
19443 register values and thread-local values).
c906108c
SS
19444
19445 A location description containing no operations indicates that the
4cecd739 19446 object is optimized out. The return value is 0 for that case.
6b992462
DJ
19447 FIXME drow/2003-11-16: No callers check for this case any more; soon all
19448 callers will only want a very basic result and this can become a
21ae7a4d
JK
19449 complaint.
19450
19451 Note that stack[0] is unused except as a default error return. */
c906108c
SS
19452
19453static CORE_ADDR
e7c27a73 19454decode_locdesc (struct dwarf_block *blk, struct dwarf2_cu *cu)
c906108c 19455{
e7c27a73 19456 struct objfile *objfile = cu->objfile;
56eb65bd
SP
19457 size_t i;
19458 size_t size = blk->size;
d521ce57 19459 const gdb_byte *data = blk->data;
21ae7a4d
JK
19460 CORE_ADDR stack[64];
19461 int stacki;
19462 unsigned int bytes_read, unsnd;
19463 gdb_byte op;
c906108c 19464
21ae7a4d
JK
19465 i = 0;
19466 stacki = 0;
19467 stack[stacki] = 0;
19468 stack[++stacki] = 0;
19469
19470 while (i < size)
19471 {
19472 op = data[i++];
19473 switch (op)
19474 {
19475 case DW_OP_lit0:
19476 case DW_OP_lit1:
19477 case DW_OP_lit2:
19478 case DW_OP_lit3:
19479 case DW_OP_lit4:
19480 case DW_OP_lit5:
19481 case DW_OP_lit6:
19482 case DW_OP_lit7:
19483 case DW_OP_lit8:
19484 case DW_OP_lit9:
19485 case DW_OP_lit10:
19486 case DW_OP_lit11:
19487 case DW_OP_lit12:
19488 case DW_OP_lit13:
19489 case DW_OP_lit14:
19490 case DW_OP_lit15:
19491 case DW_OP_lit16:
19492 case DW_OP_lit17:
19493 case DW_OP_lit18:
19494 case DW_OP_lit19:
19495 case DW_OP_lit20:
19496 case DW_OP_lit21:
19497 case DW_OP_lit22:
19498 case DW_OP_lit23:
19499 case DW_OP_lit24:
19500 case DW_OP_lit25:
19501 case DW_OP_lit26:
19502 case DW_OP_lit27:
19503 case DW_OP_lit28:
19504 case DW_OP_lit29:
19505 case DW_OP_lit30:
19506 case DW_OP_lit31:
19507 stack[++stacki] = op - DW_OP_lit0;
19508 break;
f1bea926 19509
21ae7a4d
JK
19510 case DW_OP_reg0:
19511 case DW_OP_reg1:
19512 case DW_OP_reg2:
19513 case DW_OP_reg3:
19514 case DW_OP_reg4:
19515 case DW_OP_reg5:
19516 case DW_OP_reg6:
19517 case DW_OP_reg7:
19518 case DW_OP_reg8:
19519 case DW_OP_reg9:
19520 case DW_OP_reg10:
19521 case DW_OP_reg11:
19522 case DW_OP_reg12:
19523 case DW_OP_reg13:
19524 case DW_OP_reg14:
19525 case DW_OP_reg15:
19526 case DW_OP_reg16:
19527 case DW_OP_reg17:
19528 case DW_OP_reg18:
19529 case DW_OP_reg19:
19530 case DW_OP_reg20:
19531 case DW_OP_reg21:
19532 case DW_OP_reg22:
19533 case DW_OP_reg23:
19534 case DW_OP_reg24:
19535 case DW_OP_reg25:
19536 case DW_OP_reg26:
19537 case DW_OP_reg27:
19538 case DW_OP_reg28:
19539 case DW_OP_reg29:
19540 case DW_OP_reg30:
19541 case DW_OP_reg31:
19542 stack[++stacki] = op - DW_OP_reg0;
19543 if (i < size)
19544 dwarf2_complex_location_expr_complaint ();
19545 break;
c906108c 19546
21ae7a4d
JK
19547 case DW_OP_regx:
19548 unsnd = read_unsigned_leb128 (NULL, (data + i), &bytes_read);
19549 i += bytes_read;
19550 stack[++stacki] = unsnd;
19551 if (i < size)
19552 dwarf2_complex_location_expr_complaint ();
19553 break;
c906108c 19554
21ae7a4d
JK
19555 case DW_OP_addr:
19556 stack[++stacki] = read_address (objfile->obfd, &data[i],
19557 cu, &bytes_read);
19558 i += bytes_read;
19559 break;
d53d4ac5 19560
21ae7a4d
JK
19561 case DW_OP_const1u:
19562 stack[++stacki] = read_1_byte (objfile->obfd, &data[i]);
19563 i += 1;
19564 break;
19565
19566 case DW_OP_const1s:
19567 stack[++stacki] = read_1_signed_byte (objfile->obfd, &data[i]);
19568 i += 1;
19569 break;
19570
19571 case DW_OP_const2u:
19572 stack[++stacki] = read_2_bytes (objfile->obfd, &data[i]);
19573 i += 2;
19574 break;
19575
19576 case DW_OP_const2s:
19577 stack[++stacki] = read_2_signed_bytes (objfile->obfd, &data[i]);
19578 i += 2;
19579 break;
d53d4ac5 19580
21ae7a4d
JK
19581 case DW_OP_const4u:
19582 stack[++stacki] = read_4_bytes (objfile->obfd, &data[i]);
19583 i += 4;
19584 break;
19585
19586 case DW_OP_const4s:
19587 stack[++stacki] = read_4_signed_bytes (objfile->obfd, &data[i]);
19588 i += 4;
19589 break;
19590
585861ea
JK
19591 case DW_OP_const8u:
19592 stack[++stacki] = read_8_bytes (objfile->obfd, &data[i]);
19593 i += 8;
19594 break;
19595
21ae7a4d
JK
19596 case DW_OP_constu:
19597 stack[++stacki] = read_unsigned_leb128 (NULL, (data + i),
19598 &bytes_read);
19599 i += bytes_read;
19600 break;
19601
19602 case DW_OP_consts:
19603 stack[++stacki] = read_signed_leb128 (NULL, (data + i), &bytes_read);
19604 i += bytes_read;
19605 break;
19606
19607 case DW_OP_dup:
19608 stack[stacki + 1] = stack[stacki];
19609 stacki++;
19610 break;
19611
19612 case DW_OP_plus:
19613 stack[stacki - 1] += stack[stacki];
19614 stacki--;
19615 break;
19616
19617 case DW_OP_plus_uconst:
19618 stack[stacki] += read_unsigned_leb128 (NULL, (data + i),
19619 &bytes_read);
19620 i += bytes_read;
19621 break;
19622
19623 case DW_OP_minus:
19624 stack[stacki - 1] -= stack[stacki];
19625 stacki--;
19626 break;
19627
19628 case DW_OP_deref:
19629 /* If we're not the last op, then we definitely can't encode
19630 this using GDB's address_class enum. This is valid for partial
19631 global symbols, although the variable's address will be bogus
19632 in the psymtab. */
19633 if (i < size)
19634 dwarf2_complex_location_expr_complaint ();
19635 break;
19636
19637 case DW_OP_GNU_push_tls_address:
19638 /* The top of the stack has the offset from the beginning
19639 of the thread control block at which the variable is located. */
19640 /* Nothing should follow this operator, so the top of stack would
19641 be returned. */
19642 /* This is valid for partial global symbols, but the variable's
585861ea
JK
19643 address will be bogus in the psymtab. Make it always at least
19644 non-zero to not look as a variable garbage collected by linker
19645 which have DW_OP_addr 0. */
21ae7a4d
JK
19646 if (i < size)
19647 dwarf2_complex_location_expr_complaint ();
585861ea 19648 stack[stacki]++;
21ae7a4d
JK
19649 break;
19650
19651 case DW_OP_GNU_uninit:
19652 break;
19653
3019eac3 19654 case DW_OP_GNU_addr_index:
49f6c839 19655 case DW_OP_GNU_const_index:
3019eac3
DE
19656 stack[++stacki] = read_addr_index_from_leb128 (cu, &data[i],
19657 &bytes_read);
19658 i += bytes_read;
19659 break;
19660
21ae7a4d
JK
19661 default:
19662 {
f39c6ffd 19663 const char *name = get_DW_OP_name (op);
21ae7a4d
JK
19664
19665 if (name)
19666 complaint (&symfile_complaints, _("unsupported stack op: '%s'"),
19667 name);
19668 else
19669 complaint (&symfile_complaints, _("unsupported stack op: '%02x'"),
19670 op);
19671 }
19672
19673 return (stack[stacki]);
d53d4ac5 19674 }
3c6e0cb3 19675
21ae7a4d
JK
19676 /* Enforce maximum stack depth of SIZE-1 to avoid writing
19677 outside of the allocated space. Also enforce minimum>0. */
19678 if (stacki >= ARRAY_SIZE (stack) - 1)
19679 {
19680 complaint (&symfile_complaints,
19681 _("location description stack overflow"));
19682 return 0;
19683 }
19684
19685 if (stacki <= 0)
19686 {
19687 complaint (&symfile_complaints,
19688 _("location description stack underflow"));
19689 return 0;
19690 }
19691 }
19692 return (stack[stacki]);
c906108c
SS
19693}
19694
19695/* memory allocation interface */
19696
c906108c 19697static struct dwarf_block *
7b5a2f43 19698dwarf_alloc_block (struct dwarf2_cu *cu)
c906108c
SS
19699{
19700 struct dwarf_block *blk;
19701
19702 blk = (struct dwarf_block *)
7b5a2f43 19703 obstack_alloc (&cu->comp_unit_obstack, sizeof (struct dwarf_block));
c906108c
SS
19704 return (blk);
19705}
19706
c906108c 19707static struct die_info *
b60c80d6 19708dwarf_alloc_die (struct dwarf2_cu *cu, int num_attrs)
c906108c
SS
19709{
19710 struct die_info *die;
b60c80d6
DJ
19711 size_t size = sizeof (struct die_info);
19712
19713 if (num_attrs > 1)
19714 size += (num_attrs - 1) * sizeof (struct attribute);
c906108c 19715
b60c80d6 19716 die = (struct die_info *) obstack_alloc (&cu->comp_unit_obstack, size);
c906108c
SS
19717 memset (die, 0, sizeof (struct die_info));
19718 return (die);
19719}
2e276125
JB
19720
19721\f
19722/* Macro support. */
19723
233d95b5
JK
19724/* Return file name relative to the compilation directory of file number I in
19725 *LH's file name table. The result is allocated using xmalloc; the caller is
2e276125 19726 responsible for freeing it. */
233d95b5 19727
2e276125 19728static char *
233d95b5 19729file_file_name (int file, struct line_header *lh)
2e276125 19730{
6a83a1e6
EZ
19731 /* Is the file number a valid index into the line header's file name
19732 table? Remember that file numbers start with one, not zero. */
19733 if (1 <= file && file <= lh->num_file_names)
19734 {
19735 struct file_entry *fe = &lh->file_names[file - 1];
6e70227d 19736
233d95b5 19737 if (IS_ABSOLUTE_PATH (fe->name) || fe->dir_index == 0)
6a83a1e6 19738 return xstrdup (fe->name);
233d95b5
JK
19739 return concat (lh->include_dirs[fe->dir_index - 1], SLASH_STRING,
19740 fe->name, NULL);
6a83a1e6 19741 }
2e276125
JB
19742 else
19743 {
6a83a1e6
EZ
19744 /* The compiler produced a bogus file number. We can at least
19745 record the macro definitions made in the file, even if we
19746 won't be able to find the file by name. */
19747 char fake_name[80];
9a619af0 19748
8c042590
PM
19749 xsnprintf (fake_name, sizeof (fake_name),
19750 "<bad macro file number %d>", file);
2e276125 19751
6e70227d 19752 complaint (&symfile_complaints,
6a83a1e6
EZ
19753 _("bad file number in macro information (%d)"),
19754 file);
2e276125 19755
6a83a1e6 19756 return xstrdup (fake_name);
2e276125
JB
19757 }
19758}
19759
233d95b5
JK
19760/* Return the full name of file number I in *LH's file name table.
19761 Use COMP_DIR as the name of the current directory of the
19762 compilation. The result is allocated using xmalloc; the caller is
19763 responsible for freeing it. */
19764static char *
19765file_full_name (int file, struct line_header *lh, const char *comp_dir)
19766{
19767 /* Is the file number a valid index into the line header's file name
19768 table? Remember that file numbers start with one, not zero. */
19769 if (1 <= file && file <= lh->num_file_names)
19770 {
19771 char *relative = file_file_name (file, lh);
19772
19773 if (IS_ABSOLUTE_PATH (relative) || comp_dir == NULL)
19774 return relative;
19775 return reconcat (relative, comp_dir, SLASH_STRING, relative, NULL);
19776 }
19777 else
19778 return file_file_name (file, lh);
19779}
19780
2e276125
JB
19781
19782static struct macro_source_file *
19783macro_start_file (int file, int line,
19784 struct macro_source_file *current_file,
19785 const char *comp_dir,
19786 struct line_header *lh, struct objfile *objfile)
19787{
233d95b5
JK
19788 /* File name relative to the compilation directory of this source file. */
19789 char *file_name = file_file_name (file, lh);
2e276125 19790
2e276125 19791 if (! current_file)
abc9d0dc 19792 {
fc474241
DE
19793 /* Note: We don't create a macro table for this compilation unit
19794 at all until we actually get a filename. */
19795 struct macro_table *macro_table = get_macro_table (objfile, comp_dir);
19796
abc9d0dc
TT
19797 /* If we have no current file, then this must be the start_file
19798 directive for the compilation unit's main source file. */
fc474241
DE
19799 current_file = macro_set_main (macro_table, file_name);
19800 macro_define_special (macro_table);
abc9d0dc 19801 }
2e276125 19802 else
233d95b5 19803 current_file = macro_include (current_file, line, file_name);
2e276125 19804
233d95b5 19805 xfree (file_name);
6e70227d 19806
2e276125
JB
19807 return current_file;
19808}
19809
19810
19811/* Copy the LEN characters at BUF to a xmalloc'ed block of memory,
19812 followed by a null byte. */
19813static char *
19814copy_string (const char *buf, int len)
19815{
19816 char *s = xmalloc (len + 1);
9a619af0 19817
2e276125
JB
19818 memcpy (s, buf, len);
19819 s[len] = '\0';
2e276125
JB
19820 return s;
19821}
19822
19823
19824static const char *
19825consume_improper_spaces (const char *p, const char *body)
19826{
19827 if (*p == ' ')
19828 {
4d3c2250 19829 complaint (&symfile_complaints,
3e43a32a
MS
19830 _("macro definition contains spaces "
19831 "in formal argument list:\n`%s'"),
4d3c2250 19832 body);
2e276125
JB
19833
19834 while (*p == ' ')
19835 p++;
19836 }
19837
19838 return p;
19839}
19840
19841
19842static void
19843parse_macro_definition (struct macro_source_file *file, int line,
19844 const char *body)
19845{
19846 const char *p;
19847
19848 /* The body string takes one of two forms. For object-like macro
19849 definitions, it should be:
19850
19851 <macro name> " " <definition>
19852
19853 For function-like macro definitions, it should be:
19854
19855 <macro name> "() " <definition>
19856 or
19857 <macro name> "(" <arg name> ( "," <arg name> ) * ") " <definition>
19858
19859 Spaces may appear only where explicitly indicated, and in the
19860 <definition>.
19861
19862 The Dwarf 2 spec says that an object-like macro's name is always
19863 followed by a space, but versions of GCC around March 2002 omit
6e70227d 19864 the space when the macro's definition is the empty string.
2e276125
JB
19865
19866 The Dwarf 2 spec says that there should be no spaces between the
19867 formal arguments in a function-like macro's formal argument list,
19868 but versions of GCC around March 2002 include spaces after the
19869 commas. */
19870
19871
19872 /* Find the extent of the macro name. The macro name is terminated
19873 by either a space or null character (for an object-like macro) or
19874 an opening paren (for a function-like macro). */
19875 for (p = body; *p; p++)
19876 if (*p == ' ' || *p == '(')
19877 break;
19878
19879 if (*p == ' ' || *p == '\0')
19880 {
19881 /* It's an object-like macro. */
19882 int name_len = p - body;
19883 char *name = copy_string (body, name_len);
19884 const char *replacement;
19885
19886 if (*p == ' ')
19887 replacement = body + name_len + 1;
19888 else
19889 {
4d3c2250 19890 dwarf2_macro_malformed_definition_complaint (body);
2e276125
JB
19891 replacement = body + name_len;
19892 }
6e70227d 19893
2e276125
JB
19894 macro_define_object (file, line, name, replacement);
19895
19896 xfree (name);
19897 }
19898 else if (*p == '(')
19899 {
19900 /* It's a function-like macro. */
19901 char *name = copy_string (body, p - body);
19902 int argc = 0;
19903 int argv_size = 1;
19904 char **argv = xmalloc (argv_size * sizeof (*argv));
19905
19906 p++;
19907
19908 p = consume_improper_spaces (p, body);
19909
19910 /* Parse the formal argument list. */
19911 while (*p && *p != ')')
19912 {
19913 /* Find the extent of the current argument name. */
19914 const char *arg_start = p;
19915
19916 while (*p && *p != ',' && *p != ')' && *p != ' ')
19917 p++;
19918
19919 if (! *p || p == arg_start)
4d3c2250 19920 dwarf2_macro_malformed_definition_complaint (body);
2e276125
JB
19921 else
19922 {
19923 /* Make sure argv has room for the new argument. */
19924 if (argc >= argv_size)
19925 {
19926 argv_size *= 2;
19927 argv = xrealloc (argv, argv_size * sizeof (*argv));
19928 }
19929
19930 argv[argc++] = copy_string (arg_start, p - arg_start);
19931 }
19932
19933 p = consume_improper_spaces (p, body);
19934
19935 /* Consume the comma, if present. */
19936 if (*p == ',')
19937 {
19938 p++;
19939
19940 p = consume_improper_spaces (p, body);
19941 }
19942 }
19943
19944 if (*p == ')')
19945 {
19946 p++;
19947
19948 if (*p == ' ')
19949 /* Perfectly formed definition, no complaints. */
19950 macro_define_function (file, line, name,
6e70227d 19951 argc, (const char **) argv,
2e276125
JB
19952 p + 1);
19953 else if (*p == '\0')
19954 {
19955 /* Complain, but do define it. */
4d3c2250 19956 dwarf2_macro_malformed_definition_complaint (body);
2e276125 19957 macro_define_function (file, line, name,
6e70227d 19958 argc, (const char **) argv,
2e276125
JB
19959 p);
19960 }
19961 else
19962 /* Just complain. */
4d3c2250 19963 dwarf2_macro_malformed_definition_complaint (body);
2e276125
JB
19964 }
19965 else
19966 /* Just complain. */
4d3c2250 19967 dwarf2_macro_malformed_definition_complaint (body);
2e276125
JB
19968
19969 xfree (name);
19970 {
19971 int i;
19972
19973 for (i = 0; i < argc; i++)
19974 xfree (argv[i]);
19975 }
19976 xfree (argv);
19977 }
19978 else
4d3c2250 19979 dwarf2_macro_malformed_definition_complaint (body);
2e276125
JB
19980}
19981
cf2c3c16
TT
19982/* Skip some bytes from BYTES according to the form given in FORM.
19983 Returns the new pointer. */
2e276125 19984
d521ce57
TT
19985static const gdb_byte *
19986skip_form_bytes (bfd *abfd, const gdb_byte *bytes, const gdb_byte *buffer_end,
cf2c3c16
TT
19987 enum dwarf_form form,
19988 unsigned int offset_size,
19989 struct dwarf2_section_info *section)
2e276125 19990{
cf2c3c16 19991 unsigned int bytes_read;
2e276125 19992
cf2c3c16 19993 switch (form)
2e276125 19994 {
cf2c3c16
TT
19995 case DW_FORM_data1:
19996 case DW_FORM_flag:
19997 ++bytes;
19998 break;
19999
20000 case DW_FORM_data2:
20001 bytes += 2;
20002 break;
20003
20004 case DW_FORM_data4:
20005 bytes += 4;
20006 break;
20007
20008 case DW_FORM_data8:
20009 bytes += 8;
20010 break;
20011
20012 case DW_FORM_string:
20013 read_direct_string (abfd, bytes, &bytes_read);
20014 bytes += bytes_read;
20015 break;
20016
20017 case DW_FORM_sec_offset:
20018 case DW_FORM_strp:
36586728 20019 case DW_FORM_GNU_strp_alt:
cf2c3c16
TT
20020 bytes += offset_size;
20021 break;
20022
20023 case DW_FORM_block:
20024 bytes += read_unsigned_leb128 (abfd, bytes, &bytes_read);
20025 bytes += bytes_read;
20026 break;
20027
20028 case DW_FORM_block1:
20029 bytes += 1 + read_1_byte (abfd, bytes);
20030 break;
20031 case DW_FORM_block2:
20032 bytes += 2 + read_2_bytes (abfd, bytes);
20033 break;
20034 case DW_FORM_block4:
20035 bytes += 4 + read_4_bytes (abfd, bytes);
20036 break;
20037
20038 case DW_FORM_sdata:
20039 case DW_FORM_udata:
3019eac3
DE
20040 case DW_FORM_GNU_addr_index:
20041 case DW_FORM_GNU_str_index:
d521ce57 20042 bytes = gdb_skip_leb128 (bytes, buffer_end);
f664829e
DE
20043 if (bytes == NULL)
20044 {
20045 dwarf2_section_buffer_overflow_complaint (section);
20046 return NULL;
20047 }
cf2c3c16
TT
20048 break;
20049
20050 default:
20051 {
20052 complain:
20053 complaint (&symfile_complaints,
20054 _("invalid form 0x%x in `%s'"),
a32a8923 20055 form, get_section_name (section));
cf2c3c16
TT
20056 return NULL;
20057 }
2e276125
JB
20058 }
20059
cf2c3c16
TT
20060 return bytes;
20061}
757a13d0 20062
cf2c3c16
TT
20063/* A helper for dwarf_decode_macros that handles skipping an unknown
20064 opcode. Returns an updated pointer to the macro data buffer; or,
20065 on error, issues a complaint and returns NULL. */
757a13d0 20066
d521ce57 20067static const gdb_byte *
cf2c3c16 20068skip_unknown_opcode (unsigned int opcode,
d521ce57
TT
20069 const gdb_byte **opcode_definitions,
20070 const gdb_byte *mac_ptr, const gdb_byte *mac_end,
cf2c3c16
TT
20071 bfd *abfd,
20072 unsigned int offset_size,
20073 struct dwarf2_section_info *section)
20074{
20075 unsigned int bytes_read, i;
20076 unsigned long arg;
d521ce57 20077 const gdb_byte *defn;
2e276125 20078
cf2c3c16 20079 if (opcode_definitions[opcode] == NULL)
2e276125 20080 {
cf2c3c16
TT
20081 complaint (&symfile_complaints,
20082 _("unrecognized DW_MACFINO opcode 0x%x"),
20083 opcode);
20084 return NULL;
20085 }
2e276125 20086
cf2c3c16
TT
20087 defn = opcode_definitions[opcode];
20088 arg = read_unsigned_leb128 (abfd, defn, &bytes_read);
20089 defn += bytes_read;
2e276125 20090
cf2c3c16
TT
20091 for (i = 0; i < arg; ++i)
20092 {
f664829e
DE
20093 mac_ptr = skip_form_bytes (abfd, mac_ptr, mac_end, defn[i], offset_size,
20094 section);
cf2c3c16
TT
20095 if (mac_ptr == NULL)
20096 {
20097 /* skip_form_bytes already issued the complaint. */
20098 return NULL;
20099 }
20100 }
757a13d0 20101
cf2c3c16
TT
20102 return mac_ptr;
20103}
757a13d0 20104
cf2c3c16
TT
20105/* A helper function which parses the header of a macro section.
20106 If the macro section is the extended (for now called "GNU") type,
20107 then this updates *OFFSET_SIZE. Returns a pointer to just after
20108 the header, or issues a complaint and returns NULL on error. */
757a13d0 20109
d521ce57
TT
20110static const gdb_byte *
20111dwarf_parse_macro_header (const gdb_byte **opcode_definitions,
cf2c3c16 20112 bfd *abfd,
d521ce57 20113 const gdb_byte *mac_ptr,
cf2c3c16
TT
20114 unsigned int *offset_size,
20115 int section_is_gnu)
20116{
20117 memset (opcode_definitions, 0, 256 * sizeof (gdb_byte *));
757a13d0 20118
cf2c3c16
TT
20119 if (section_is_gnu)
20120 {
20121 unsigned int version, flags;
757a13d0 20122
cf2c3c16
TT
20123 version = read_2_bytes (abfd, mac_ptr);
20124 if (version != 4)
20125 {
20126 complaint (&symfile_complaints,
20127 _("unrecognized version `%d' in .debug_macro section"),
20128 version);
20129 return NULL;
20130 }
20131 mac_ptr += 2;
757a13d0 20132
cf2c3c16
TT
20133 flags = read_1_byte (abfd, mac_ptr);
20134 ++mac_ptr;
20135 *offset_size = (flags & 1) ? 8 : 4;
757a13d0 20136
cf2c3c16
TT
20137 if ((flags & 2) != 0)
20138 /* We don't need the line table offset. */
20139 mac_ptr += *offset_size;
757a13d0 20140
cf2c3c16
TT
20141 /* Vendor opcode descriptions. */
20142 if ((flags & 4) != 0)
20143 {
20144 unsigned int i, count;
757a13d0 20145
cf2c3c16
TT
20146 count = read_1_byte (abfd, mac_ptr);
20147 ++mac_ptr;
20148 for (i = 0; i < count; ++i)
20149 {
20150 unsigned int opcode, bytes_read;
20151 unsigned long arg;
20152
20153 opcode = read_1_byte (abfd, mac_ptr);
20154 ++mac_ptr;
20155 opcode_definitions[opcode] = mac_ptr;
20156 arg = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
20157 mac_ptr += bytes_read;
20158 mac_ptr += arg;
20159 }
757a13d0 20160 }
cf2c3c16 20161 }
757a13d0 20162
cf2c3c16
TT
20163 return mac_ptr;
20164}
757a13d0 20165
cf2c3c16 20166/* A helper for dwarf_decode_macros that handles the GNU extensions,
8fc3fc34 20167 including DW_MACRO_GNU_transparent_include. */
cf2c3c16
TT
20168
20169static void
d521ce57
TT
20170dwarf_decode_macro_bytes (bfd *abfd,
20171 const gdb_byte *mac_ptr, const gdb_byte *mac_end,
cf2c3c16 20172 struct macro_source_file *current_file,
15d034d0 20173 struct line_header *lh, const char *comp_dir,
cf2c3c16 20174 struct dwarf2_section_info *section,
36586728 20175 int section_is_gnu, int section_is_dwz,
cf2c3c16 20176 unsigned int offset_size,
8fc3fc34
TT
20177 struct objfile *objfile,
20178 htab_t include_hash)
cf2c3c16
TT
20179{
20180 enum dwarf_macro_record_type macinfo_type;
20181 int at_commandline;
d521ce57 20182 const gdb_byte *opcode_definitions[256];
757a13d0 20183
cf2c3c16
TT
20184 mac_ptr = dwarf_parse_macro_header (opcode_definitions, abfd, mac_ptr,
20185 &offset_size, section_is_gnu);
20186 if (mac_ptr == NULL)
20187 {
20188 /* We already issued a complaint. */
20189 return;
20190 }
757a13d0
JK
20191
20192 /* Determines if GDB is still before first DW_MACINFO_start_file. If true
20193 GDB is still reading the definitions from command line. First
20194 DW_MACINFO_start_file will need to be ignored as it was already executed
20195 to create CURRENT_FILE for the main source holding also the command line
20196 definitions. On first met DW_MACINFO_start_file this flag is reset to
20197 normally execute all the remaining DW_MACINFO_start_file macinfos. */
20198
20199 at_commandline = 1;
20200
20201 do
20202 {
20203 /* Do we at least have room for a macinfo type byte? */
20204 if (mac_ptr >= mac_end)
20205 {
f664829e 20206 dwarf2_section_buffer_overflow_complaint (section);
757a13d0
JK
20207 break;
20208 }
20209
20210 macinfo_type = read_1_byte (abfd, mac_ptr);
20211 mac_ptr++;
20212
cf2c3c16
TT
20213 /* Note that we rely on the fact that the corresponding GNU and
20214 DWARF constants are the same. */
757a13d0
JK
20215 switch (macinfo_type)
20216 {
20217 /* A zero macinfo type indicates the end of the macro
20218 information. */
20219 case 0:
20220 break;
2e276125 20221
cf2c3c16
TT
20222 case DW_MACRO_GNU_define:
20223 case DW_MACRO_GNU_undef:
20224 case DW_MACRO_GNU_define_indirect:
20225 case DW_MACRO_GNU_undef_indirect:
36586728
TT
20226 case DW_MACRO_GNU_define_indirect_alt:
20227 case DW_MACRO_GNU_undef_indirect_alt:
2e276125 20228 {
891d2f0b 20229 unsigned int bytes_read;
2e276125 20230 int line;
d521ce57 20231 const char *body;
cf2c3c16 20232 int is_define;
2e276125 20233
cf2c3c16
TT
20234 line = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
20235 mac_ptr += bytes_read;
20236
20237 if (macinfo_type == DW_MACRO_GNU_define
20238 || macinfo_type == DW_MACRO_GNU_undef)
20239 {
20240 body = read_direct_string (abfd, mac_ptr, &bytes_read);
20241 mac_ptr += bytes_read;
20242 }
20243 else
20244 {
20245 LONGEST str_offset;
20246
20247 str_offset = read_offset_1 (abfd, mac_ptr, offset_size);
20248 mac_ptr += offset_size;
2e276125 20249
36586728 20250 if (macinfo_type == DW_MACRO_GNU_define_indirect_alt
f7a35f02
TT
20251 || macinfo_type == DW_MACRO_GNU_undef_indirect_alt
20252 || section_is_dwz)
36586728
TT
20253 {
20254 struct dwz_file *dwz = dwarf2_get_dwz_file ();
20255
20256 body = read_indirect_string_from_dwz (dwz, str_offset);
20257 }
20258 else
20259 body = read_indirect_string_at_offset (abfd, str_offset);
cf2c3c16
TT
20260 }
20261
20262 is_define = (macinfo_type == DW_MACRO_GNU_define
36586728
TT
20263 || macinfo_type == DW_MACRO_GNU_define_indirect
20264 || macinfo_type == DW_MACRO_GNU_define_indirect_alt);
2e276125 20265 if (! current_file)
757a13d0
JK
20266 {
20267 /* DWARF violation as no main source is present. */
20268 complaint (&symfile_complaints,
20269 _("debug info with no main source gives macro %s "
20270 "on line %d: %s"),
cf2c3c16
TT
20271 is_define ? _("definition") : _("undefinition"),
20272 line, body);
757a13d0
JK
20273 break;
20274 }
3e43a32a
MS
20275 if ((line == 0 && !at_commandline)
20276 || (line != 0 && at_commandline))
4d3c2250 20277 complaint (&symfile_complaints,
757a13d0
JK
20278 _("debug info gives %s macro %s with %s line %d: %s"),
20279 at_commandline ? _("command-line") : _("in-file"),
cf2c3c16 20280 is_define ? _("definition") : _("undefinition"),
757a13d0
JK
20281 line == 0 ? _("zero") : _("non-zero"), line, body);
20282
cf2c3c16 20283 if (is_define)
757a13d0 20284 parse_macro_definition (current_file, line, body);
cf2c3c16
TT
20285 else
20286 {
20287 gdb_assert (macinfo_type == DW_MACRO_GNU_undef
36586728
TT
20288 || macinfo_type == DW_MACRO_GNU_undef_indirect
20289 || macinfo_type == DW_MACRO_GNU_undef_indirect_alt);
cf2c3c16
TT
20290 macro_undef (current_file, line, body);
20291 }
2e276125
JB
20292 }
20293 break;
20294
cf2c3c16 20295 case DW_MACRO_GNU_start_file:
2e276125 20296 {
891d2f0b 20297 unsigned int bytes_read;
2e276125
JB
20298 int line, file;
20299
20300 line = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
20301 mac_ptr += bytes_read;
20302 file = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
20303 mac_ptr += bytes_read;
20304
3e43a32a
MS
20305 if ((line == 0 && !at_commandline)
20306 || (line != 0 && at_commandline))
757a13d0
JK
20307 complaint (&symfile_complaints,
20308 _("debug info gives source %d included "
20309 "from %s at %s line %d"),
20310 file, at_commandline ? _("command-line") : _("file"),
20311 line == 0 ? _("zero") : _("non-zero"), line);
20312
20313 if (at_commandline)
20314 {
cf2c3c16
TT
20315 /* This DW_MACRO_GNU_start_file was executed in the
20316 pass one. */
757a13d0
JK
20317 at_commandline = 0;
20318 }
20319 else
20320 current_file = macro_start_file (file, line,
20321 current_file, comp_dir,
cf2c3c16 20322 lh, objfile);
2e276125
JB
20323 }
20324 break;
20325
cf2c3c16 20326 case DW_MACRO_GNU_end_file:
2e276125 20327 if (! current_file)
4d3c2250 20328 complaint (&symfile_complaints,
3e43a32a
MS
20329 _("macro debug info has an unmatched "
20330 "`close_file' directive"));
2e276125
JB
20331 else
20332 {
20333 current_file = current_file->included_by;
20334 if (! current_file)
20335 {
cf2c3c16 20336 enum dwarf_macro_record_type next_type;
2e276125
JB
20337
20338 /* GCC circa March 2002 doesn't produce the zero
20339 type byte marking the end of the compilation
20340 unit. Complain if it's not there, but exit no
20341 matter what. */
20342
20343 /* Do we at least have room for a macinfo type byte? */
20344 if (mac_ptr >= mac_end)
20345 {
f664829e 20346 dwarf2_section_buffer_overflow_complaint (section);
2e276125
JB
20347 return;
20348 }
20349
20350 /* We don't increment mac_ptr here, so this is just
20351 a look-ahead. */
20352 next_type = read_1_byte (abfd, mac_ptr);
20353 if (next_type != 0)
4d3c2250 20354 complaint (&symfile_complaints,
3e43a32a
MS
20355 _("no terminating 0-type entry for "
20356 "macros in `.debug_macinfo' section"));
2e276125
JB
20357
20358 return;
20359 }
20360 }
20361 break;
20362
cf2c3c16 20363 case DW_MACRO_GNU_transparent_include:
36586728 20364 case DW_MACRO_GNU_transparent_include_alt:
cf2c3c16
TT
20365 {
20366 LONGEST offset;
8fc3fc34 20367 void **slot;
a036ba48
TT
20368 bfd *include_bfd = abfd;
20369 struct dwarf2_section_info *include_section = section;
20370 struct dwarf2_section_info alt_section;
d521ce57 20371 const gdb_byte *include_mac_end = mac_end;
a036ba48 20372 int is_dwz = section_is_dwz;
d521ce57 20373 const gdb_byte *new_mac_ptr;
cf2c3c16
TT
20374
20375 offset = read_offset_1 (abfd, mac_ptr, offset_size);
20376 mac_ptr += offset_size;
20377
a036ba48
TT
20378 if (macinfo_type == DW_MACRO_GNU_transparent_include_alt)
20379 {
20380 struct dwz_file *dwz = dwarf2_get_dwz_file ();
20381
20382 dwarf2_read_section (dwarf2_per_objfile->objfile,
20383 &dwz->macro);
20384
a036ba48 20385 include_section = &dwz->macro;
a32a8923 20386 include_bfd = get_section_bfd_owner (include_section);
a036ba48
TT
20387 include_mac_end = dwz->macro.buffer + dwz->macro.size;
20388 is_dwz = 1;
20389 }
20390
20391 new_mac_ptr = include_section->buffer + offset;
20392 slot = htab_find_slot (include_hash, new_mac_ptr, INSERT);
20393
8fc3fc34
TT
20394 if (*slot != NULL)
20395 {
20396 /* This has actually happened; see
20397 http://sourceware.org/bugzilla/show_bug.cgi?id=13568. */
20398 complaint (&symfile_complaints,
20399 _("recursive DW_MACRO_GNU_transparent_include in "
20400 ".debug_macro section"));
20401 }
20402 else
20403 {
d521ce57 20404 *slot = (void *) new_mac_ptr;
36586728 20405
a036ba48 20406 dwarf_decode_macro_bytes (include_bfd, new_mac_ptr,
36586728 20407 include_mac_end, current_file,
8fc3fc34 20408 lh, comp_dir,
36586728 20409 section, section_is_gnu, is_dwz,
8fc3fc34
TT
20410 offset_size, objfile, include_hash);
20411
d521ce57 20412 htab_remove_elt (include_hash, (void *) new_mac_ptr);
8fc3fc34 20413 }
cf2c3c16
TT
20414 }
20415 break;
20416
2e276125 20417 case DW_MACINFO_vendor_ext:
cf2c3c16
TT
20418 if (!section_is_gnu)
20419 {
20420 unsigned int bytes_read;
20421 int constant;
2e276125 20422
cf2c3c16
TT
20423 constant = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
20424 mac_ptr += bytes_read;
20425 read_direct_string (abfd, mac_ptr, &bytes_read);
20426 mac_ptr += bytes_read;
2e276125 20427
cf2c3c16
TT
20428 /* We don't recognize any vendor extensions. */
20429 break;
20430 }
20431 /* FALLTHROUGH */
20432
20433 default:
20434 mac_ptr = skip_unknown_opcode (macinfo_type, opcode_definitions,
f664829e 20435 mac_ptr, mac_end, abfd, offset_size,
cf2c3c16
TT
20436 section);
20437 if (mac_ptr == NULL)
20438 return;
20439 break;
2e276125 20440 }
757a13d0 20441 } while (macinfo_type != 0);
2e276125 20442}
8e19ed76 20443
cf2c3c16 20444static void
09262596 20445dwarf_decode_macros (struct dwarf2_cu *cu, unsigned int offset,
15d034d0 20446 const char *comp_dir, int section_is_gnu)
cf2c3c16 20447{
bb5ed363 20448 struct objfile *objfile = dwarf2_per_objfile->objfile;
09262596
DE
20449 struct line_header *lh = cu->line_header;
20450 bfd *abfd;
d521ce57 20451 const gdb_byte *mac_ptr, *mac_end;
cf2c3c16
TT
20452 struct macro_source_file *current_file = 0;
20453 enum dwarf_macro_record_type macinfo_type;
20454 unsigned int offset_size = cu->header.offset_size;
d521ce57 20455 const gdb_byte *opcode_definitions[256];
8fc3fc34
TT
20456 struct cleanup *cleanup;
20457 htab_t include_hash;
20458 void **slot;
09262596
DE
20459 struct dwarf2_section_info *section;
20460 const char *section_name;
20461
20462 if (cu->dwo_unit != NULL)
20463 {
20464 if (section_is_gnu)
20465 {
20466 section = &cu->dwo_unit->dwo_file->sections.macro;
20467 section_name = ".debug_macro.dwo";
20468 }
20469 else
20470 {
20471 section = &cu->dwo_unit->dwo_file->sections.macinfo;
20472 section_name = ".debug_macinfo.dwo";
20473 }
20474 }
20475 else
20476 {
20477 if (section_is_gnu)
20478 {
20479 section = &dwarf2_per_objfile->macro;
20480 section_name = ".debug_macro";
20481 }
20482 else
20483 {
20484 section = &dwarf2_per_objfile->macinfo;
20485 section_name = ".debug_macinfo";
20486 }
20487 }
cf2c3c16 20488
bb5ed363 20489 dwarf2_read_section (objfile, section);
cf2c3c16
TT
20490 if (section->buffer == NULL)
20491 {
fceca515 20492 complaint (&symfile_complaints, _("missing %s section"), section_name);
cf2c3c16
TT
20493 return;
20494 }
a32a8923 20495 abfd = get_section_bfd_owner (section);
cf2c3c16
TT
20496
20497 /* First pass: Find the name of the base filename.
20498 This filename is needed in order to process all macros whose definition
20499 (or undefinition) comes from the command line. These macros are defined
20500 before the first DW_MACINFO_start_file entry, and yet still need to be
20501 associated to the base file.
20502
20503 To determine the base file name, we scan the macro definitions until we
20504 reach the first DW_MACINFO_start_file entry. We then initialize
20505 CURRENT_FILE accordingly so that any macro definition found before the
20506 first DW_MACINFO_start_file can still be associated to the base file. */
20507
20508 mac_ptr = section->buffer + offset;
20509 mac_end = section->buffer + section->size;
20510
20511 mac_ptr = dwarf_parse_macro_header (opcode_definitions, abfd, mac_ptr,
20512 &offset_size, section_is_gnu);
20513 if (mac_ptr == NULL)
20514 {
20515 /* We already issued a complaint. */
20516 return;
20517 }
20518
20519 do
20520 {
20521 /* Do we at least have room for a macinfo type byte? */
20522 if (mac_ptr >= mac_end)
20523 {
20524 /* Complaint is printed during the second pass as GDB will probably
20525 stop the first pass earlier upon finding
20526 DW_MACINFO_start_file. */
20527 break;
20528 }
20529
20530 macinfo_type = read_1_byte (abfd, mac_ptr);
20531 mac_ptr++;
20532
20533 /* Note that we rely on the fact that the corresponding GNU and
20534 DWARF constants are the same. */
20535 switch (macinfo_type)
20536 {
20537 /* A zero macinfo type indicates the end of the macro
20538 information. */
20539 case 0:
20540 break;
20541
20542 case DW_MACRO_GNU_define:
20543 case DW_MACRO_GNU_undef:
20544 /* Only skip the data by MAC_PTR. */
20545 {
20546 unsigned int bytes_read;
20547
20548 read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
20549 mac_ptr += bytes_read;
20550 read_direct_string (abfd, mac_ptr, &bytes_read);
20551 mac_ptr += bytes_read;
20552 }
20553 break;
20554
20555 case DW_MACRO_GNU_start_file:
20556 {
20557 unsigned int bytes_read;
20558 int line, file;
20559
20560 line = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
20561 mac_ptr += bytes_read;
20562 file = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
20563 mac_ptr += bytes_read;
20564
20565 current_file = macro_start_file (file, line, current_file,
bb5ed363 20566 comp_dir, lh, objfile);
cf2c3c16
TT
20567 }
20568 break;
20569
20570 case DW_MACRO_GNU_end_file:
20571 /* No data to skip by MAC_PTR. */
20572 break;
20573
20574 case DW_MACRO_GNU_define_indirect:
20575 case DW_MACRO_GNU_undef_indirect:
f7a35f02
TT
20576 case DW_MACRO_GNU_define_indirect_alt:
20577 case DW_MACRO_GNU_undef_indirect_alt:
cf2c3c16
TT
20578 {
20579 unsigned int bytes_read;
20580
20581 read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
20582 mac_ptr += bytes_read;
20583 mac_ptr += offset_size;
20584 }
20585 break;
20586
20587 case DW_MACRO_GNU_transparent_include:
f7a35f02 20588 case DW_MACRO_GNU_transparent_include_alt:
cf2c3c16
TT
20589 /* Note that, according to the spec, a transparent include
20590 chain cannot call DW_MACRO_GNU_start_file. So, we can just
20591 skip this opcode. */
20592 mac_ptr += offset_size;
20593 break;
20594
20595 case DW_MACINFO_vendor_ext:
20596 /* Only skip the data by MAC_PTR. */
20597 if (!section_is_gnu)
20598 {
20599 unsigned int bytes_read;
20600
20601 read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
20602 mac_ptr += bytes_read;
20603 read_direct_string (abfd, mac_ptr, &bytes_read);
20604 mac_ptr += bytes_read;
20605 }
20606 /* FALLTHROUGH */
20607
20608 default:
20609 mac_ptr = skip_unknown_opcode (macinfo_type, opcode_definitions,
f664829e 20610 mac_ptr, mac_end, abfd, offset_size,
cf2c3c16
TT
20611 section);
20612 if (mac_ptr == NULL)
20613 return;
20614 break;
20615 }
20616 } while (macinfo_type != 0 && current_file == NULL);
20617
20618 /* Second pass: Process all entries.
20619
20620 Use the AT_COMMAND_LINE flag to determine whether we are still processing
20621 command-line macro definitions/undefinitions. This flag is unset when we
20622 reach the first DW_MACINFO_start_file entry. */
20623
8fc3fc34
TT
20624 include_hash = htab_create_alloc (1, htab_hash_pointer, htab_eq_pointer,
20625 NULL, xcalloc, xfree);
20626 cleanup = make_cleanup_htab_delete (include_hash);
20627 mac_ptr = section->buffer + offset;
20628 slot = htab_find_slot (include_hash, mac_ptr, INSERT);
d521ce57 20629 *slot = (void *) mac_ptr;
8fc3fc34 20630 dwarf_decode_macro_bytes (abfd, mac_ptr, mac_end,
36586728
TT
20631 current_file, lh, comp_dir, section,
20632 section_is_gnu, 0,
8fc3fc34
TT
20633 offset_size, objfile, include_hash);
20634 do_cleanups (cleanup);
cf2c3c16
TT
20635}
20636
8e19ed76 20637/* Check if the attribute's form is a DW_FORM_block*
0963b4bd 20638 if so return true else false. */
380bca97 20639
8e19ed76 20640static int
6e5a29e1 20641attr_form_is_block (const struct attribute *attr)
8e19ed76
PS
20642{
20643 return (attr == NULL ? 0 :
20644 attr->form == DW_FORM_block1
20645 || attr->form == DW_FORM_block2
20646 || attr->form == DW_FORM_block4
2dc7f7b3
TT
20647 || attr->form == DW_FORM_block
20648 || attr->form == DW_FORM_exprloc);
8e19ed76 20649}
4c2df51b 20650
c6a0999f
JB
20651/* Return non-zero if ATTR's value is a section offset --- classes
20652 lineptr, loclistptr, macptr or rangelistptr --- or zero, otherwise.
20653 You may use DW_UNSND (attr) to retrieve such offsets.
20654
20655 Section 7.5.4, "Attribute Encodings", explains that no attribute
20656 may have a value that belongs to more than one of these classes; it
20657 would be ambiguous if we did, because we use the same forms for all
20658 of them. */
380bca97 20659
3690dd37 20660static int
6e5a29e1 20661attr_form_is_section_offset (const struct attribute *attr)
3690dd37
JB
20662{
20663 return (attr->form == DW_FORM_data4
2dc7f7b3
TT
20664 || attr->form == DW_FORM_data8
20665 || attr->form == DW_FORM_sec_offset);
3690dd37
JB
20666}
20667
3690dd37
JB
20668/* Return non-zero if ATTR's value falls in the 'constant' class, or
20669 zero otherwise. When this function returns true, you can apply
20670 dwarf2_get_attr_constant_value to it.
20671
20672 However, note that for some attributes you must check
20673 attr_form_is_section_offset before using this test. DW_FORM_data4
20674 and DW_FORM_data8 are members of both the constant class, and of
20675 the classes that contain offsets into other debug sections
20676 (lineptr, loclistptr, macptr or rangelistptr). The DWARF spec says
20677 that, if an attribute's can be either a constant or one of the
20678 section offset classes, DW_FORM_data4 and DW_FORM_data8 should be
20679 taken as section offsets, not constants. */
380bca97 20680
3690dd37 20681static int
6e5a29e1 20682attr_form_is_constant (const struct attribute *attr)
3690dd37
JB
20683{
20684 switch (attr->form)
20685 {
20686 case DW_FORM_sdata:
20687 case DW_FORM_udata:
20688 case DW_FORM_data1:
20689 case DW_FORM_data2:
20690 case DW_FORM_data4:
20691 case DW_FORM_data8:
20692 return 1;
20693 default:
20694 return 0;
20695 }
20696}
20697
7771576e
SA
20698
20699/* DW_ADDR is always stored already as sect_offset; despite for the forms
20700 besides DW_FORM_ref_addr it is stored as cu_offset in the DWARF file. */
20701
20702static int
6e5a29e1 20703attr_form_is_ref (const struct attribute *attr)
7771576e
SA
20704{
20705 switch (attr->form)
20706 {
20707 case DW_FORM_ref_addr:
20708 case DW_FORM_ref1:
20709 case DW_FORM_ref2:
20710 case DW_FORM_ref4:
20711 case DW_FORM_ref8:
20712 case DW_FORM_ref_udata:
20713 case DW_FORM_GNU_ref_alt:
20714 return 1;
20715 default:
20716 return 0;
20717 }
20718}
20719
3019eac3
DE
20720/* Return the .debug_loc section to use for CU.
20721 For DWO files use .debug_loc.dwo. */
20722
20723static struct dwarf2_section_info *
20724cu_debug_loc_section (struct dwarf2_cu *cu)
20725{
20726 if (cu->dwo_unit)
20727 return &cu->dwo_unit->dwo_file->sections.loc;
20728 return &dwarf2_per_objfile->loc;
20729}
20730
8cf6f0b1
TT
20731/* A helper function that fills in a dwarf2_loclist_baton. */
20732
20733static void
20734fill_in_loclist_baton (struct dwarf2_cu *cu,
20735 struct dwarf2_loclist_baton *baton,
ff39bb5e 20736 const struct attribute *attr)
8cf6f0b1 20737{
3019eac3
DE
20738 struct dwarf2_section_info *section = cu_debug_loc_section (cu);
20739
20740 dwarf2_read_section (dwarf2_per_objfile->objfile, section);
8cf6f0b1
TT
20741
20742 baton->per_cu = cu->per_cu;
20743 gdb_assert (baton->per_cu);
20744 /* We don't know how long the location list is, but make sure we
20745 don't run off the edge of the section. */
3019eac3
DE
20746 baton->size = section->size - DW_UNSND (attr);
20747 baton->data = section->buffer + DW_UNSND (attr);
8cf6f0b1 20748 baton->base_address = cu->base_address;
f664829e 20749 baton->from_dwo = cu->dwo_unit != NULL;
8cf6f0b1
TT
20750}
20751
4c2df51b 20752static void
ff39bb5e 20753dwarf2_symbol_mark_computed (const struct attribute *attr, struct symbol *sym,
f1e6e072 20754 struct dwarf2_cu *cu, int is_block)
4c2df51b 20755{
bb5ed363 20756 struct objfile *objfile = dwarf2_per_objfile->objfile;
3019eac3 20757 struct dwarf2_section_info *section = cu_debug_loc_section (cu);
bb5ed363 20758
3690dd37 20759 if (attr_form_is_section_offset (attr)
3019eac3 20760 /* .debug_loc{,.dwo} may not exist at all, or the offset may be outside
99bcc461
DJ
20761 the section. If so, fall through to the complaint in the
20762 other branch. */
3019eac3 20763 && DW_UNSND (attr) < dwarf2_section_size (objfile, section))
4c2df51b 20764 {
0d53c4c4 20765 struct dwarf2_loclist_baton *baton;
4c2df51b 20766
bb5ed363 20767 baton = obstack_alloc (&objfile->objfile_obstack,
0d53c4c4 20768 sizeof (struct dwarf2_loclist_baton));
4c2df51b 20769
8cf6f0b1 20770 fill_in_loclist_baton (cu, baton, attr);
be391dca 20771
d00adf39 20772 if (cu->base_known == 0)
0d53c4c4 20773 complaint (&symfile_complaints,
3e43a32a
MS
20774 _("Location list used without "
20775 "specifying the CU base address."));
4c2df51b 20776
f1e6e072
TT
20777 SYMBOL_ACLASS_INDEX (sym) = (is_block
20778 ? dwarf2_loclist_block_index
20779 : dwarf2_loclist_index);
0d53c4c4
DJ
20780 SYMBOL_LOCATION_BATON (sym) = baton;
20781 }
20782 else
20783 {
20784 struct dwarf2_locexpr_baton *baton;
20785
bb5ed363 20786 baton = obstack_alloc (&objfile->objfile_obstack,
0d53c4c4 20787 sizeof (struct dwarf2_locexpr_baton));
ae0d2f24
UW
20788 baton->per_cu = cu->per_cu;
20789 gdb_assert (baton->per_cu);
0d53c4c4
DJ
20790
20791 if (attr_form_is_block (attr))
20792 {
20793 /* Note that we're just copying the block's data pointer
20794 here, not the actual data. We're still pointing into the
6502dd73
DJ
20795 info_buffer for SYM's objfile; right now we never release
20796 that buffer, but when we do clean up properly this may
20797 need to change. */
0d53c4c4
DJ
20798 baton->size = DW_BLOCK (attr)->size;
20799 baton->data = DW_BLOCK (attr)->data;
20800 }
20801 else
20802 {
20803 dwarf2_invalid_attrib_class_complaint ("location description",
20804 SYMBOL_NATURAL_NAME (sym));
20805 baton->size = 0;
0d53c4c4 20806 }
6e70227d 20807
f1e6e072
TT
20808 SYMBOL_ACLASS_INDEX (sym) = (is_block
20809 ? dwarf2_locexpr_block_index
20810 : dwarf2_locexpr_index);
0d53c4c4
DJ
20811 SYMBOL_LOCATION_BATON (sym) = baton;
20812 }
4c2df51b 20813}
6502dd73 20814
9aa1f1e3
TT
20815/* Return the OBJFILE associated with the compilation unit CU. If CU
20816 came from a separate debuginfo file, then the master objfile is
20817 returned. */
ae0d2f24
UW
20818
20819struct objfile *
20820dwarf2_per_cu_objfile (struct dwarf2_per_cu_data *per_cu)
20821{
9291a0cd 20822 struct objfile *objfile = per_cu->objfile;
ae0d2f24
UW
20823
20824 /* Return the master objfile, so that we can report and look up the
20825 correct file containing this variable. */
20826 if (objfile->separate_debug_objfile_backlink)
20827 objfile = objfile->separate_debug_objfile_backlink;
20828
20829 return objfile;
20830}
20831
96408a79
SA
20832/* Return comp_unit_head for PER_CU, either already available in PER_CU->CU
20833 (CU_HEADERP is unused in such case) or prepare a temporary copy at
20834 CU_HEADERP first. */
20835
20836static const struct comp_unit_head *
20837per_cu_header_read_in (struct comp_unit_head *cu_headerp,
20838 struct dwarf2_per_cu_data *per_cu)
20839{
d521ce57 20840 const gdb_byte *info_ptr;
96408a79
SA
20841
20842 if (per_cu->cu)
20843 return &per_cu->cu->header;
20844
8a0459fd 20845 info_ptr = per_cu->section->buffer + per_cu->offset.sect_off;
96408a79
SA
20846
20847 memset (cu_headerp, 0, sizeof (*cu_headerp));
0bc3a05c 20848 read_comp_unit_head (cu_headerp, info_ptr, per_cu->objfile->obfd);
96408a79
SA
20849
20850 return cu_headerp;
20851}
20852
ae0d2f24
UW
20853/* Return the address size given in the compilation unit header for CU. */
20854
98714339 20855int
ae0d2f24
UW
20856dwarf2_per_cu_addr_size (struct dwarf2_per_cu_data *per_cu)
20857{
96408a79
SA
20858 struct comp_unit_head cu_header_local;
20859 const struct comp_unit_head *cu_headerp;
c471e790 20860
96408a79
SA
20861 cu_headerp = per_cu_header_read_in (&cu_header_local, per_cu);
20862
20863 return cu_headerp->addr_size;
ae0d2f24
UW
20864}
20865
9eae7c52
TT
20866/* Return the offset size given in the compilation unit header for CU. */
20867
20868int
20869dwarf2_per_cu_offset_size (struct dwarf2_per_cu_data *per_cu)
20870{
96408a79
SA
20871 struct comp_unit_head cu_header_local;
20872 const struct comp_unit_head *cu_headerp;
9c6c53f7 20873
96408a79
SA
20874 cu_headerp = per_cu_header_read_in (&cu_header_local, per_cu);
20875
20876 return cu_headerp->offset_size;
20877}
20878
20879/* See its dwarf2loc.h declaration. */
20880
20881int
20882dwarf2_per_cu_ref_addr_size (struct dwarf2_per_cu_data *per_cu)
20883{
20884 struct comp_unit_head cu_header_local;
20885 const struct comp_unit_head *cu_headerp;
20886
20887 cu_headerp = per_cu_header_read_in (&cu_header_local, per_cu);
20888
20889 if (cu_headerp->version == 2)
20890 return cu_headerp->addr_size;
20891 else
20892 return cu_headerp->offset_size;
181cebd4
JK
20893}
20894
9aa1f1e3
TT
20895/* Return the text offset of the CU. The returned offset comes from
20896 this CU's objfile. If this objfile came from a separate debuginfo
20897 file, then the offset may be different from the corresponding
20898 offset in the parent objfile. */
20899
20900CORE_ADDR
20901dwarf2_per_cu_text_offset (struct dwarf2_per_cu_data *per_cu)
20902{
bb3fa9d0 20903 struct objfile *objfile = per_cu->objfile;
9aa1f1e3
TT
20904
20905 return ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
20906}
20907
348e048f
DE
20908/* Locate the .debug_info compilation unit from CU's objfile which contains
20909 the DIE at OFFSET. Raises an error on failure. */
ae038cb0
DJ
20910
20911static struct dwarf2_per_cu_data *
b64f50a1 20912dwarf2_find_containing_comp_unit (sect_offset offset,
36586728 20913 unsigned int offset_in_dwz,
ae038cb0
DJ
20914 struct objfile *objfile)
20915{
20916 struct dwarf2_per_cu_data *this_cu;
20917 int low, high;
36586728 20918 const sect_offset *cu_off;
ae038cb0 20919
ae038cb0
DJ
20920 low = 0;
20921 high = dwarf2_per_objfile->n_comp_units - 1;
20922 while (high > low)
20923 {
36586728 20924 struct dwarf2_per_cu_data *mid_cu;
ae038cb0 20925 int mid = low + (high - low) / 2;
9a619af0 20926
36586728
TT
20927 mid_cu = dwarf2_per_objfile->all_comp_units[mid];
20928 cu_off = &mid_cu->offset;
20929 if (mid_cu->is_dwz > offset_in_dwz
20930 || (mid_cu->is_dwz == offset_in_dwz
20931 && cu_off->sect_off >= offset.sect_off))
ae038cb0
DJ
20932 high = mid;
20933 else
20934 low = mid + 1;
20935 }
20936 gdb_assert (low == high);
36586728
TT
20937 this_cu = dwarf2_per_objfile->all_comp_units[low];
20938 cu_off = &this_cu->offset;
20939 if (this_cu->is_dwz != offset_in_dwz || cu_off->sect_off > offset.sect_off)
ae038cb0 20940 {
36586728 20941 if (low == 0 || this_cu->is_dwz != offset_in_dwz)
8a3fe4f8
AC
20942 error (_("Dwarf Error: could not find partial DIE containing "
20943 "offset 0x%lx [in module %s]"),
b64f50a1 20944 (long) offset.sect_off, bfd_get_filename (objfile->obfd));
10b3939b 20945
b64f50a1
JK
20946 gdb_assert (dwarf2_per_objfile->all_comp_units[low-1]->offset.sect_off
20947 <= offset.sect_off);
ae038cb0
DJ
20948 return dwarf2_per_objfile->all_comp_units[low-1];
20949 }
20950 else
20951 {
20952 this_cu = dwarf2_per_objfile->all_comp_units[low];
20953 if (low == dwarf2_per_objfile->n_comp_units - 1
b64f50a1
JK
20954 && offset.sect_off >= this_cu->offset.sect_off + this_cu->length)
20955 error (_("invalid dwarf2 offset %u"), offset.sect_off);
20956 gdb_assert (offset.sect_off < this_cu->offset.sect_off + this_cu->length);
ae038cb0
DJ
20957 return this_cu;
20958 }
20959}
20960
23745b47 20961/* Initialize dwarf2_cu CU, owned by PER_CU. */
93311388 20962
9816fde3 20963static void
23745b47 20964init_one_comp_unit (struct dwarf2_cu *cu, struct dwarf2_per_cu_data *per_cu)
93311388 20965{
9816fde3 20966 memset (cu, 0, sizeof (*cu));
23745b47
DE
20967 per_cu->cu = cu;
20968 cu->per_cu = per_cu;
20969 cu->objfile = per_cu->objfile;
93311388 20970 obstack_init (&cu->comp_unit_obstack);
9816fde3
JK
20971}
20972
20973/* Initialize basic fields of dwarf_cu CU according to DIE COMP_UNIT_DIE. */
20974
20975static void
95554aad
TT
20976prepare_one_comp_unit (struct dwarf2_cu *cu, struct die_info *comp_unit_die,
20977 enum language pretend_language)
9816fde3
JK
20978{
20979 struct attribute *attr;
20980
20981 /* Set the language we're debugging. */
20982 attr = dwarf2_attr (comp_unit_die, DW_AT_language, cu);
20983 if (attr)
20984 set_cu_language (DW_UNSND (attr), cu);
20985 else
9cded63f 20986 {
95554aad 20987 cu->language = pretend_language;
9cded63f
TT
20988 cu->language_defn = language_def (cu->language);
20989 }
dee91e82
DE
20990
20991 attr = dwarf2_attr (comp_unit_die, DW_AT_producer, cu);
20992 if (attr)
20993 cu->producer = DW_STRING (attr);
93311388
DE
20994}
20995
ae038cb0
DJ
20996/* Release one cached compilation unit, CU. We unlink it from the tree
20997 of compilation units, but we don't remove it from the read_in_chain;
93311388
DE
20998 the caller is responsible for that.
20999 NOTE: DATA is a void * because this function is also used as a
21000 cleanup routine. */
ae038cb0
DJ
21001
21002static void
68dc6402 21003free_heap_comp_unit (void *data)
ae038cb0
DJ
21004{
21005 struct dwarf2_cu *cu = data;
21006
23745b47
DE
21007 gdb_assert (cu->per_cu != NULL);
21008 cu->per_cu->cu = NULL;
ae038cb0
DJ
21009 cu->per_cu = NULL;
21010
21011 obstack_free (&cu->comp_unit_obstack, NULL);
21012
21013 xfree (cu);
21014}
21015
72bf9492 21016/* This cleanup function is passed the address of a dwarf2_cu on the stack
ae038cb0 21017 when we're finished with it. We can't free the pointer itself, but be
dee91e82 21018 sure to unlink it from the cache. Also release any associated storage. */
72bf9492
DJ
21019
21020static void
21021free_stack_comp_unit (void *data)
21022{
21023 struct dwarf2_cu *cu = data;
21024
23745b47
DE
21025 gdb_assert (cu->per_cu != NULL);
21026 cu->per_cu->cu = NULL;
21027 cu->per_cu = NULL;
21028
72bf9492
DJ
21029 obstack_free (&cu->comp_unit_obstack, NULL);
21030 cu->partial_dies = NULL;
ae038cb0
DJ
21031}
21032
21033/* Free all cached compilation units. */
21034
21035static void
21036free_cached_comp_units (void *data)
21037{
21038 struct dwarf2_per_cu_data *per_cu, **last_chain;
21039
21040 per_cu = dwarf2_per_objfile->read_in_chain;
21041 last_chain = &dwarf2_per_objfile->read_in_chain;
21042 while (per_cu != NULL)
21043 {
21044 struct dwarf2_per_cu_data *next_cu;
21045
21046 next_cu = per_cu->cu->read_in_chain;
21047
68dc6402 21048 free_heap_comp_unit (per_cu->cu);
ae038cb0
DJ
21049 *last_chain = next_cu;
21050
21051 per_cu = next_cu;
21052 }
21053}
21054
21055/* Increase the age counter on each cached compilation unit, and free
21056 any that are too old. */
21057
21058static void
21059age_cached_comp_units (void)
21060{
21061 struct dwarf2_per_cu_data *per_cu, **last_chain;
21062
21063 dwarf2_clear_marks (dwarf2_per_objfile->read_in_chain);
21064 per_cu = dwarf2_per_objfile->read_in_chain;
21065 while (per_cu != NULL)
21066 {
21067 per_cu->cu->last_used ++;
21068 if (per_cu->cu->last_used <= dwarf2_max_cache_age)
21069 dwarf2_mark (per_cu->cu);
21070 per_cu = per_cu->cu->read_in_chain;
21071 }
21072
21073 per_cu = dwarf2_per_objfile->read_in_chain;
21074 last_chain = &dwarf2_per_objfile->read_in_chain;
21075 while (per_cu != NULL)
21076 {
21077 struct dwarf2_per_cu_data *next_cu;
21078
21079 next_cu = per_cu->cu->read_in_chain;
21080
21081 if (!per_cu->cu->mark)
21082 {
68dc6402 21083 free_heap_comp_unit (per_cu->cu);
ae038cb0
DJ
21084 *last_chain = next_cu;
21085 }
21086 else
21087 last_chain = &per_cu->cu->read_in_chain;
21088
21089 per_cu = next_cu;
21090 }
21091}
21092
21093/* Remove a single compilation unit from the cache. */
21094
21095static void
dee91e82 21096free_one_cached_comp_unit (struct dwarf2_per_cu_data *target_per_cu)
ae038cb0
DJ
21097{
21098 struct dwarf2_per_cu_data *per_cu, **last_chain;
21099
21100 per_cu = dwarf2_per_objfile->read_in_chain;
21101 last_chain = &dwarf2_per_objfile->read_in_chain;
21102 while (per_cu != NULL)
21103 {
21104 struct dwarf2_per_cu_data *next_cu;
21105
21106 next_cu = per_cu->cu->read_in_chain;
21107
dee91e82 21108 if (per_cu == target_per_cu)
ae038cb0 21109 {
68dc6402 21110 free_heap_comp_unit (per_cu->cu);
dee91e82 21111 per_cu->cu = NULL;
ae038cb0
DJ
21112 *last_chain = next_cu;
21113 break;
21114 }
21115 else
21116 last_chain = &per_cu->cu->read_in_chain;
21117
21118 per_cu = next_cu;
21119 }
21120}
21121
fe3e1990
DJ
21122/* Release all extra memory associated with OBJFILE. */
21123
21124void
21125dwarf2_free_objfile (struct objfile *objfile)
21126{
21127 dwarf2_per_objfile = objfile_data (objfile, dwarf2_objfile_data_key);
21128
21129 if (dwarf2_per_objfile == NULL)
21130 return;
21131
21132 /* Cached DIE trees use xmalloc and the comp_unit_obstack. */
21133 free_cached_comp_units (NULL);
21134
7b9f3c50
DE
21135 if (dwarf2_per_objfile->quick_file_names_table)
21136 htab_delete (dwarf2_per_objfile->quick_file_names_table);
9291a0cd 21137
fe3e1990
DJ
21138 /* Everything else should be on the objfile obstack. */
21139}
21140
dee91e82
DE
21141/* A set of CU "per_cu" pointer, DIE offset, and GDB type pointer.
21142 We store these in a hash table separate from the DIEs, and preserve them
21143 when the DIEs are flushed out of cache.
21144
21145 The CU "per_cu" pointer is needed because offset alone is not enough to
3019eac3 21146 uniquely identify the type. A file may have multiple .debug_types sections,
c88ee1f0
DE
21147 or the type may come from a DWO file. Furthermore, while it's more logical
21148 to use per_cu->section+offset, with Fission the section with the data is in
21149 the DWO file but we don't know that section at the point we need it.
21150 We have to use something in dwarf2_per_cu_data (or the pointer to it)
21151 because we can enter the lookup routine, get_die_type_at_offset, from
21152 outside this file, and thus won't necessarily have PER_CU->cu.
21153 Fortunately, PER_CU is stable for the life of the objfile. */
1c379e20 21154
dee91e82 21155struct dwarf2_per_cu_offset_and_type
1c379e20 21156{
dee91e82 21157 const struct dwarf2_per_cu_data *per_cu;
b64f50a1 21158 sect_offset offset;
1c379e20
DJ
21159 struct type *type;
21160};
21161
dee91e82 21162/* Hash function for a dwarf2_per_cu_offset_and_type. */
1c379e20
DJ
21163
21164static hashval_t
dee91e82 21165per_cu_offset_and_type_hash (const void *item)
1c379e20 21166{
dee91e82 21167 const struct dwarf2_per_cu_offset_and_type *ofs = item;
9a619af0 21168
dee91e82 21169 return (uintptr_t) ofs->per_cu + ofs->offset.sect_off;
1c379e20
DJ
21170}
21171
dee91e82 21172/* Equality function for a dwarf2_per_cu_offset_and_type. */
1c379e20
DJ
21173
21174static int
dee91e82 21175per_cu_offset_and_type_eq (const void *item_lhs, const void *item_rhs)
1c379e20 21176{
dee91e82
DE
21177 const struct dwarf2_per_cu_offset_and_type *ofs_lhs = item_lhs;
21178 const struct dwarf2_per_cu_offset_and_type *ofs_rhs = item_rhs;
9a619af0 21179
dee91e82
DE
21180 return (ofs_lhs->per_cu == ofs_rhs->per_cu
21181 && ofs_lhs->offset.sect_off == ofs_rhs->offset.sect_off);
1c379e20
DJ
21182}
21183
21184/* Set the type associated with DIE to TYPE. Save it in CU's hash
7e314c57
JK
21185 table if necessary. For convenience, return TYPE.
21186
21187 The DIEs reading must have careful ordering to:
21188 * Not cause infite loops trying to read in DIEs as a prerequisite for
21189 reading current DIE.
21190 * Not trying to dereference contents of still incompletely read in types
21191 while reading in other DIEs.
21192 * Enable referencing still incompletely read in types just by a pointer to
21193 the type without accessing its fields.
21194
21195 Therefore caller should follow these rules:
21196 * Try to fetch any prerequisite types we may need to build this DIE type
21197 before building the type and calling set_die_type.
e71ec853 21198 * After building type call set_die_type for current DIE as soon as
7e314c57
JK
21199 possible before fetching more types to complete the current type.
21200 * Make the type as complete as possible before fetching more types. */
1c379e20 21201
f792889a 21202static struct type *
1c379e20
DJ
21203set_die_type (struct die_info *die, struct type *type, struct dwarf2_cu *cu)
21204{
dee91e82 21205 struct dwarf2_per_cu_offset_and_type **slot, ofs;
673bfd45 21206 struct objfile *objfile = cu->objfile;
1c379e20 21207
b4ba55a1
JB
21208 /* For Ada types, make sure that the gnat-specific data is always
21209 initialized (if not already set). There are a few types where
21210 we should not be doing so, because the type-specific area is
21211 already used to hold some other piece of info (eg: TYPE_CODE_FLT
21212 where the type-specific area is used to store the floatformat).
21213 But this is not a problem, because the gnat-specific information
21214 is actually not needed for these types. */
21215 if (need_gnat_info (cu)
21216 && TYPE_CODE (type) != TYPE_CODE_FUNC
21217 && TYPE_CODE (type) != TYPE_CODE_FLT
21218 && !HAVE_GNAT_AUX_INFO (type))
21219 INIT_GNAT_SPECIFIC (type);
21220
dee91e82 21221 if (dwarf2_per_objfile->die_type_hash == NULL)
f792889a 21222 {
dee91e82
DE
21223 dwarf2_per_objfile->die_type_hash =
21224 htab_create_alloc_ex (127,
21225 per_cu_offset_and_type_hash,
21226 per_cu_offset_and_type_eq,
21227 NULL,
21228 &objfile->objfile_obstack,
21229 hashtab_obstack_allocate,
21230 dummy_obstack_deallocate);
f792889a 21231 }
1c379e20 21232
dee91e82 21233 ofs.per_cu = cu->per_cu;
1c379e20
DJ
21234 ofs.offset = die->offset;
21235 ofs.type = type;
dee91e82
DE
21236 slot = (struct dwarf2_per_cu_offset_and_type **)
21237 htab_find_slot (dwarf2_per_objfile->die_type_hash, &ofs, INSERT);
7e314c57
JK
21238 if (*slot)
21239 complaint (&symfile_complaints,
21240 _("A problem internal to GDB: DIE 0x%x has type already set"),
b64f50a1 21241 die->offset.sect_off);
673bfd45 21242 *slot = obstack_alloc (&objfile->objfile_obstack, sizeof (**slot));
1c379e20 21243 **slot = ofs;
f792889a 21244 return type;
1c379e20
DJ
21245}
21246
02142a6c
DE
21247/* Look up the type for the die at OFFSET in PER_CU in die_type_hash,
21248 or return NULL if the die does not have a saved type. */
1c379e20
DJ
21249
21250static struct type *
b64f50a1 21251get_die_type_at_offset (sect_offset offset,
673bfd45 21252 struct dwarf2_per_cu_data *per_cu)
1c379e20 21253{
dee91e82 21254 struct dwarf2_per_cu_offset_and_type *slot, ofs;
f792889a 21255
dee91e82 21256 if (dwarf2_per_objfile->die_type_hash == NULL)
f792889a 21257 return NULL;
1c379e20 21258
dee91e82 21259 ofs.per_cu = per_cu;
673bfd45 21260 ofs.offset = offset;
dee91e82 21261 slot = htab_find (dwarf2_per_objfile->die_type_hash, &ofs);
1c379e20
DJ
21262 if (slot)
21263 return slot->type;
21264 else
21265 return NULL;
21266}
21267
02142a6c 21268/* Look up the type for DIE in CU in die_type_hash,
673bfd45
DE
21269 or return NULL if DIE does not have a saved type. */
21270
21271static struct type *
21272get_die_type (struct die_info *die, struct dwarf2_cu *cu)
21273{
21274 return get_die_type_at_offset (die->offset, cu->per_cu);
21275}
21276
10b3939b
DJ
21277/* Add a dependence relationship from CU to REF_PER_CU. */
21278
21279static void
21280dwarf2_add_dependence (struct dwarf2_cu *cu,
21281 struct dwarf2_per_cu_data *ref_per_cu)
21282{
21283 void **slot;
21284
21285 if (cu->dependencies == NULL)
21286 cu->dependencies
21287 = htab_create_alloc_ex (5, htab_hash_pointer, htab_eq_pointer,
21288 NULL, &cu->comp_unit_obstack,
21289 hashtab_obstack_allocate,
21290 dummy_obstack_deallocate);
21291
21292 slot = htab_find_slot (cu->dependencies, ref_per_cu, INSERT);
21293 if (*slot == NULL)
21294 *slot = ref_per_cu;
21295}
1c379e20 21296
f504f079
DE
21297/* Subroutine of dwarf2_mark to pass to htab_traverse.
21298 Set the mark field in every compilation unit in the
ae038cb0
DJ
21299 cache that we must keep because we are keeping CU. */
21300
10b3939b
DJ
21301static int
21302dwarf2_mark_helper (void **slot, void *data)
21303{
21304 struct dwarf2_per_cu_data *per_cu;
21305
21306 per_cu = (struct dwarf2_per_cu_data *) *slot;
d07ed419
JK
21307
21308 /* cu->dependencies references may not yet have been ever read if QUIT aborts
21309 reading of the chain. As such dependencies remain valid it is not much
21310 useful to track and undo them during QUIT cleanups. */
21311 if (per_cu->cu == NULL)
21312 return 1;
21313
10b3939b
DJ
21314 if (per_cu->cu->mark)
21315 return 1;
21316 per_cu->cu->mark = 1;
21317
21318 if (per_cu->cu->dependencies != NULL)
21319 htab_traverse (per_cu->cu->dependencies, dwarf2_mark_helper, NULL);
21320
21321 return 1;
21322}
21323
f504f079
DE
21324/* Set the mark field in CU and in every other compilation unit in the
21325 cache that we must keep because we are keeping CU. */
21326
ae038cb0
DJ
21327static void
21328dwarf2_mark (struct dwarf2_cu *cu)
21329{
21330 if (cu->mark)
21331 return;
21332 cu->mark = 1;
10b3939b
DJ
21333 if (cu->dependencies != NULL)
21334 htab_traverse (cu->dependencies, dwarf2_mark_helper, NULL);
ae038cb0
DJ
21335}
21336
21337static void
21338dwarf2_clear_marks (struct dwarf2_per_cu_data *per_cu)
21339{
21340 while (per_cu)
21341 {
21342 per_cu->cu->mark = 0;
21343 per_cu = per_cu->cu->read_in_chain;
21344 }
72bf9492
DJ
21345}
21346
72bf9492
DJ
21347/* Trivial hash function for partial_die_info: the hash value of a DIE
21348 is its offset in .debug_info for this objfile. */
21349
21350static hashval_t
21351partial_die_hash (const void *item)
21352{
21353 const struct partial_die_info *part_die = item;
9a619af0 21354
b64f50a1 21355 return part_die->offset.sect_off;
72bf9492
DJ
21356}
21357
21358/* Trivial comparison function for partial_die_info structures: two DIEs
21359 are equal if they have the same offset. */
21360
21361static int
21362partial_die_eq (const void *item_lhs, const void *item_rhs)
21363{
21364 const struct partial_die_info *part_die_lhs = item_lhs;
21365 const struct partial_die_info *part_die_rhs = item_rhs;
9a619af0 21366
b64f50a1 21367 return part_die_lhs->offset.sect_off == part_die_rhs->offset.sect_off;
72bf9492
DJ
21368}
21369
ae038cb0
DJ
21370static struct cmd_list_element *set_dwarf2_cmdlist;
21371static struct cmd_list_element *show_dwarf2_cmdlist;
21372
21373static void
21374set_dwarf2_cmd (char *args, int from_tty)
21375{
21376 help_list (set_dwarf2_cmdlist, "maintenance set dwarf2 ", -1, gdb_stdout);
21377}
21378
21379static void
21380show_dwarf2_cmd (char *args, int from_tty)
6e70227d 21381{
ae038cb0
DJ
21382 cmd_show_list (show_dwarf2_cmdlist, from_tty, "");
21383}
21384
4bf44c1c 21385/* Free data associated with OBJFILE, if necessary. */
dce234bc
PP
21386
21387static void
c1bd65d0 21388dwarf2_per_objfile_free (struct objfile *objfile, void *d)
dce234bc
PP
21389{
21390 struct dwarf2_per_objfile *data = d;
8b70b953 21391 int ix;
8b70b953 21392
626f2d1c
TT
21393 /* Make sure we don't accidentally use dwarf2_per_objfile while
21394 cleaning up. */
21395 dwarf2_per_objfile = NULL;
21396
59b0c7c1
JB
21397 for (ix = 0; ix < data->n_comp_units; ++ix)
21398 VEC_free (dwarf2_per_cu_ptr, data->all_comp_units[ix]->imported_symtabs);
796a7ff8 21399
59b0c7c1 21400 for (ix = 0; ix < data->n_type_units; ++ix)
796a7ff8 21401 VEC_free (dwarf2_per_cu_ptr,
59b0c7c1
JB
21402 data->all_type_units[ix]->per_cu.imported_symtabs);
21403 xfree (data->all_type_units);
95554aad 21404
8b70b953 21405 VEC_free (dwarf2_section_info_def, data->types);
3019eac3
DE
21406
21407 if (data->dwo_files)
21408 free_dwo_files (data->dwo_files, objfile);
5c6fa7ab
DE
21409 if (data->dwp_file)
21410 gdb_bfd_unref (data->dwp_file->dbfd);
36586728
TT
21411
21412 if (data->dwz_file && data->dwz_file->dwz_bfd)
21413 gdb_bfd_unref (data->dwz_file->dwz_bfd);
9291a0cd
TT
21414}
21415
21416\f
ae2de4f8 21417/* The "save gdb-index" command. */
9291a0cd
TT
21418
21419/* The contents of the hash table we create when building the string
21420 table. */
21421struct strtab_entry
21422{
21423 offset_type offset;
21424 const char *str;
21425};
21426
559a7a62
JK
21427/* Hash function for a strtab_entry.
21428
21429 Function is used only during write_hash_table so no index format backward
21430 compatibility is needed. */
b89be57b 21431
9291a0cd
TT
21432static hashval_t
21433hash_strtab_entry (const void *e)
21434{
21435 const struct strtab_entry *entry = e;
559a7a62 21436 return mapped_index_string_hash (INT_MAX, entry->str);
9291a0cd
TT
21437}
21438
21439/* Equality function for a strtab_entry. */
b89be57b 21440
9291a0cd
TT
21441static int
21442eq_strtab_entry (const void *a, const void *b)
21443{
21444 const struct strtab_entry *ea = a;
21445 const struct strtab_entry *eb = b;
21446 return !strcmp (ea->str, eb->str);
21447}
21448
21449/* Create a strtab_entry hash table. */
b89be57b 21450
9291a0cd
TT
21451static htab_t
21452create_strtab (void)
21453{
21454 return htab_create_alloc (100, hash_strtab_entry, eq_strtab_entry,
21455 xfree, xcalloc, xfree);
21456}
21457
21458/* Add a string to the constant pool. Return the string's offset in
21459 host order. */
b89be57b 21460
9291a0cd
TT
21461static offset_type
21462add_string (htab_t table, struct obstack *cpool, const char *str)
21463{
21464 void **slot;
21465 struct strtab_entry entry;
21466 struct strtab_entry *result;
21467
21468 entry.str = str;
21469 slot = htab_find_slot (table, &entry, INSERT);
21470 if (*slot)
21471 result = *slot;
21472 else
21473 {
21474 result = XNEW (struct strtab_entry);
21475 result->offset = obstack_object_size (cpool);
21476 result->str = str;
21477 obstack_grow_str0 (cpool, str);
21478 *slot = result;
21479 }
21480 return result->offset;
21481}
21482
21483/* An entry in the symbol table. */
21484struct symtab_index_entry
21485{
21486 /* The name of the symbol. */
21487 const char *name;
21488 /* The offset of the name in the constant pool. */
21489 offset_type index_offset;
21490 /* A sorted vector of the indices of all the CUs that hold an object
21491 of this name. */
21492 VEC (offset_type) *cu_indices;
21493};
21494
21495/* The symbol table. This is a power-of-2-sized hash table. */
21496struct mapped_symtab
21497{
21498 offset_type n_elements;
21499 offset_type size;
21500 struct symtab_index_entry **data;
21501};
21502
21503/* Hash function for a symtab_index_entry. */
b89be57b 21504
9291a0cd
TT
21505static hashval_t
21506hash_symtab_entry (const void *e)
21507{
21508 const struct symtab_index_entry *entry = e;
21509 return iterative_hash (VEC_address (offset_type, entry->cu_indices),
21510 sizeof (offset_type) * VEC_length (offset_type,
21511 entry->cu_indices),
21512 0);
21513}
21514
21515/* Equality function for a symtab_index_entry. */
b89be57b 21516
9291a0cd
TT
21517static int
21518eq_symtab_entry (const void *a, const void *b)
21519{
21520 const struct symtab_index_entry *ea = a;
21521 const struct symtab_index_entry *eb = b;
21522 int len = VEC_length (offset_type, ea->cu_indices);
21523 if (len != VEC_length (offset_type, eb->cu_indices))
21524 return 0;
21525 return !memcmp (VEC_address (offset_type, ea->cu_indices),
21526 VEC_address (offset_type, eb->cu_indices),
21527 sizeof (offset_type) * len);
21528}
21529
21530/* Destroy a symtab_index_entry. */
b89be57b 21531
9291a0cd
TT
21532static void
21533delete_symtab_entry (void *p)
21534{
21535 struct symtab_index_entry *entry = p;
21536 VEC_free (offset_type, entry->cu_indices);
21537 xfree (entry);
21538}
21539
21540/* Create a hash table holding symtab_index_entry objects. */
b89be57b 21541
9291a0cd 21542static htab_t
3876f04e 21543create_symbol_hash_table (void)
9291a0cd
TT
21544{
21545 return htab_create_alloc (100, hash_symtab_entry, eq_symtab_entry,
21546 delete_symtab_entry, xcalloc, xfree);
21547}
21548
21549/* Create a new mapped symtab object. */
b89be57b 21550
9291a0cd
TT
21551static struct mapped_symtab *
21552create_mapped_symtab (void)
21553{
21554 struct mapped_symtab *symtab = XNEW (struct mapped_symtab);
21555 symtab->n_elements = 0;
21556 symtab->size = 1024;
21557 symtab->data = XCNEWVEC (struct symtab_index_entry *, symtab->size);
21558 return symtab;
21559}
21560
21561/* Destroy a mapped_symtab. */
b89be57b 21562
9291a0cd
TT
21563static void
21564cleanup_mapped_symtab (void *p)
21565{
21566 struct mapped_symtab *symtab = p;
21567 /* The contents of the array are freed when the other hash table is
21568 destroyed. */
21569 xfree (symtab->data);
21570 xfree (symtab);
21571}
21572
21573/* Find a slot in SYMTAB for the symbol NAME. Returns a pointer to
559a7a62
JK
21574 the slot.
21575
21576 Function is used only during write_hash_table so no index format backward
21577 compatibility is needed. */
b89be57b 21578
9291a0cd
TT
21579static struct symtab_index_entry **
21580find_slot (struct mapped_symtab *symtab, const char *name)
21581{
559a7a62 21582 offset_type index, step, hash = mapped_index_string_hash (INT_MAX, name);
9291a0cd
TT
21583
21584 index = hash & (symtab->size - 1);
21585 step = ((hash * 17) & (symtab->size - 1)) | 1;
21586
21587 for (;;)
21588 {
21589 if (!symtab->data[index] || !strcmp (name, symtab->data[index]->name))
21590 return &symtab->data[index];
21591 index = (index + step) & (symtab->size - 1);
21592 }
21593}
21594
21595/* Expand SYMTAB's hash table. */
b89be57b 21596
9291a0cd
TT
21597static void
21598hash_expand (struct mapped_symtab *symtab)
21599{
21600 offset_type old_size = symtab->size;
21601 offset_type i;
21602 struct symtab_index_entry **old_entries = symtab->data;
21603
21604 symtab->size *= 2;
21605 symtab->data = XCNEWVEC (struct symtab_index_entry *, symtab->size);
21606
21607 for (i = 0; i < old_size; ++i)
21608 {
21609 if (old_entries[i])
21610 {
21611 struct symtab_index_entry **slot = find_slot (symtab,
21612 old_entries[i]->name);
21613 *slot = old_entries[i];
21614 }
21615 }
21616
21617 xfree (old_entries);
21618}
21619
156942c7
DE
21620/* Add an entry to SYMTAB. NAME is the name of the symbol.
21621 CU_INDEX is the index of the CU in which the symbol appears.
21622 IS_STATIC is one if the symbol is static, otherwise zero (global). */
b89be57b 21623
9291a0cd
TT
21624static void
21625add_index_entry (struct mapped_symtab *symtab, const char *name,
156942c7 21626 int is_static, gdb_index_symbol_kind kind,
9291a0cd
TT
21627 offset_type cu_index)
21628{
21629 struct symtab_index_entry **slot;
156942c7 21630 offset_type cu_index_and_attrs;
9291a0cd
TT
21631
21632 ++symtab->n_elements;
21633 if (4 * symtab->n_elements / 3 >= symtab->size)
21634 hash_expand (symtab);
21635
21636 slot = find_slot (symtab, name);
21637 if (!*slot)
21638 {
21639 *slot = XNEW (struct symtab_index_entry);
21640 (*slot)->name = name;
156942c7 21641 /* index_offset is set later. */
9291a0cd
TT
21642 (*slot)->cu_indices = NULL;
21643 }
156942c7
DE
21644
21645 cu_index_and_attrs = 0;
21646 DW2_GDB_INDEX_CU_SET_VALUE (cu_index_and_attrs, cu_index);
21647 DW2_GDB_INDEX_SYMBOL_STATIC_SET_VALUE (cu_index_and_attrs, is_static);
21648 DW2_GDB_INDEX_SYMBOL_KIND_SET_VALUE (cu_index_and_attrs, kind);
21649
21650 /* We don't want to record an index value twice as we want to avoid the
21651 duplication.
21652 We process all global symbols and then all static symbols
21653 (which would allow us to avoid the duplication by only having to check
21654 the last entry pushed), but a symbol could have multiple kinds in one CU.
21655 To keep things simple we don't worry about the duplication here and
21656 sort and uniqufy the list after we've processed all symbols. */
21657 VEC_safe_push (offset_type, (*slot)->cu_indices, cu_index_and_attrs);
21658}
21659
21660/* qsort helper routine for uniquify_cu_indices. */
21661
21662static int
21663offset_type_compare (const void *ap, const void *bp)
21664{
21665 offset_type a = *(offset_type *) ap;
21666 offset_type b = *(offset_type *) bp;
21667
21668 return (a > b) - (b > a);
21669}
21670
21671/* Sort and remove duplicates of all symbols' cu_indices lists. */
21672
21673static void
21674uniquify_cu_indices (struct mapped_symtab *symtab)
21675{
21676 int i;
21677
21678 for (i = 0; i < symtab->size; ++i)
21679 {
21680 struct symtab_index_entry *entry = symtab->data[i];
21681
21682 if (entry
21683 && entry->cu_indices != NULL)
21684 {
21685 unsigned int next_to_insert, next_to_check;
21686 offset_type last_value;
21687
21688 qsort (VEC_address (offset_type, entry->cu_indices),
21689 VEC_length (offset_type, entry->cu_indices),
21690 sizeof (offset_type), offset_type_compare);
21691
21692 last_value = VEC_index (offset_type, entry->cu_indices, 0);
21693 next_to_insert = 1;
21694 for (next_to_check = 1;
21695 next_to_check < VEC_length (offset_type, entry->cu_indices);
21696 ++next_to_check)
21697 {
21698 if (VEC_index (offset_type, entry->cu_indices, next_to_check)
21699 != last_value)
21700 {
21701 last_value = VEC_index (offset_type, entry->cu_indices,
21702 next_to_check);
21703 VEC_replace (offset_type, entry->cu_indices, next_to_insert,
21704 last_value);
21705 ++next_to_insert;
21706 }
21707 }
21708 VEC_truncate (offset_type, entry->cu_indices, next_to_insert);
21709 }
21710 }
9291a0cd
TT
21711}
21712
21713/* Add a vector of indices to the constant pool. */
b89be57b 21714
9291a0cd 21715static offset_type
3876f04e 21716add_indices_to_cpool (htab_t symbol_hash_table, struct obstack *cpool,
9291a0cd
TT
21717 struct symtab_index_entry *entry)
21718{
21719 void **slot;
21720
3876f04e 21721 slot = htab_find_slot (symbol_hash_table, entry, INSERT);
9291a0cd
TT
21722 if (!*slot)
21723 {
21724 offset_type len = VEC_length (offset_type, entry->cu_indices);
21725 offset_type val = MAYBE_SWAP (len);
21726 offset_type iter;
21727 int i;
21728
21729 *slot = entry;
21730 entry->index_offset = obstack_object_size (cpool);
21731
21732 obstack_grow (cpool, &val, sizeof (val));
21733 for (i = 0;
21734 VEC_iterate (offset_type, entry->cu_indices, i, iter);
21735 ++i)
21736 {
21737 val = MAYBE_SWAP (iter);
21738 obstack_grow (cpool, &val, sizeof (val));
21739 }
21740 }
21741 else
21742 {
21743 struct symtab_index_entry *old_entry = *slot;
21744 entry->index_offset = old_entry->index_offset;
21745 entry = old_entry;
21746 }
21747 return entry->index_offset;
21748}
21749
21750/* Write the mapped hash table SYMTAB to the obstack OUTPUT, with
21751 constant pool entries going into the obstack CPOOL. */
b89be57b 21752
9291a0cd
TT
21753static void
21754write_hash_table (struct mapped_symtab *symtab,
21755 struct obstack *output, struct obstack *cpool)
21756{
21757 offset_type i;
3876f04e 21758 htab_t symbol_hash_table;
9291a0cd
TT
21759 htab_t str_table;
21760
3876f04e 21761 symbol_hash_table = create_symbol_hash_table ();
9291a0cd 21762 str_table = create_strtab ();
3876f04e 21763
9291a0cd
TT
21764 /* We add all the index vectors to the constant pool first, to
21765 ensure alignment is ok. */
21766 for (i = 0; i < symtab->size; ++i)
21767 {
21768 if (symtab->data[i])
3876f04e 21769 add_indices_to_cpool (symbol_hash_table, cpool, symtab->data[i]);
9291a0cd
TT
21770 }
21771
21772 /* Now write out the hash table. */
21773 for (i = 0; i < symtab->size; ++i)
21774 {
21775 offset_type str_off, vec_off;
21776
21777 if (symtab->data[i])
21778 {
21779 str_off = add_string (str_table, cpool, symtab->data[i]->name);
21780 vec_off = symtab->data[i]->index_offset;
21781 }
21782 else
21783 {
21784 /* While 0 is a valid constant pool index, it is not valid
21785 to have 0 for both offsets. */
21786 str_off = 0;
21787 vec_off = 0;
21788 }
21789
21790 str_off = MAYBE_SWAP (str_off);
21791 vec_off = MAYBE_SWAP (vec_off);
21792
21793 obstack_grow (output, &str_off, sizeof (str_off));
21794 obstack_grow (output, &vec_off, sizeof (vec_off));
21795 }
21796
21797 htab_delete (str_table);
3876f04e 21798 htab_delete (symbol_hash_table);
9291a0cd
TT
21799}
21800
0a5429f6
DE
21801/* Struct to map psymtab to CU index in the index file. */
21802struct psymtab_cu_index_map
21803{
21804 struct partial_symtab *psymtab;
21805 unsigned int cu_index;
21806};
21807
21808static hashval_t
21809hash_psymtab_cu_index (const void *item)
21810{
21811 const struct psymtab_cu_index_map *map = item;
21812
21813 return htab_hash_pointer (map->psymtab);
21814}
21815
21816static int
21817eq_psymtab_cu_index (const void *item_lhs, const void *item_rhs)
21818{
21819 const struct psymtab_cu_index_map *lhs = item_lhs;
21820 const struct psymtab_cu_index_map *rhs = item_rhs;
21821
21822 return lhs->psymtab == rhs->psymtab;
21823}
21824
21825/* Helper struct for building the address table. */
21826struct addrmap_index_data
21827{
21828 struct objfile *objfile;
21829 struct obstack *addr_obstack;
21830 htab_t cu_index_htab;
21831
21832 /* Non-zero if the previous_* fields are valid.
21833 We can't write an entry until we see the next entry (since it is only then
21834 that we know the end of the entry). */
21835 int previous_valid;
21836 /* Index of the CU in the table of all CUs in the index file. */
21837 unsigned int previous_cu_index;
0963b4bd 21838 /* Start address of the CU. */
0a5429f6
DE
21839 CORE_ADDR previous_cu_start;
21840};
21841
21842/* Write an address entry to OBSTACK. */
b89be57b 21843
9291a0cd 21844static void
0a5429f6
DE
21845add_address_entry (struct objfile *objfile, struct obstack *obstack,
21846 CORE_ADDR start, CORE_ADDR end, unsigned int cu_index)
9291a0cd 21847{
0a5429f6 21848 offset_type cu_index_to_write;
948f8e3d 21849 gdb_byte addr[8];
9291a0cd
TT
21850 CORE_ADDR baseaddr;
21851
21852 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
21853
0a5429f6
DE
21854 store_unsigned_integer (addr, 8, BFD_ENDIAN_LITTLE, start - baseaddr);
21855 obstack_grow (obstack, addr, 8);
21856 store_unsigned_integer (addr, 8, BFD_ENDIAN_LITTLE, end - baseaddr);
21857 obstack_grow (obstack, addr, 8);
21858 cu_index_to_write = MAYBE_SWAP (cu_index);
21859 obstack_grow (obstack, &cu_index_to_write, sizeof (offset_type));
21860}
21861
21862/* Worker function for traversing an addrmap to build the address table. */
21863
21864static int
21865add_address_entry_worker (void *datap, CORE_ADDR start_addr, void *obj)
21866{
21867 struct addrmap_index_data *data = datap;
21868 struct partial_symtab *pst = obj;
0a5429f6
DE
21869
21870 if (data->previous_valid)
21871 add_address_entry (data->objfile, data->addr_obstack,
21872 data->previous_cu_start, start_addr,
21873 data->previous_cu_index);
21874
21875 data->previous_cu_start = start_addr;
21876 if (pst != NULL)
21877 {
21878 struct psymtab_cu_index_map find_map, *map;
21879 find_map.psymtab = pst;
21880 map = htab_find (data->cu_index_htab, &find_map);
21881 gdb_assert (map != NULL);
21882 data->previous_cu_index = map->cu_index;
21883 data->previous_valid = 1;
21884 }
21885 else
21886 data->previous_valid = 0;
21887
21888 return 0;
21889}
21890
21891/* Write OBJFILE's address map to OBSTACK.
21892 CU_INDEX_HTAB is used to map addrmap entries to their CU indices
21893 in the index file. */
21894
21895static void
21896write_address_map (struct objfile *objfile, struct obstack *obstack,
21897 htab_t cu_index_htab)
21898{
21899 struct addrmap_index_data addrmap_index_data;
21900
21901 /* When writing the address table, we have to cope with the fact that
21902 the addrmap iterator only provides the start of a region; we have to
21903 wait until the next invocation to get the start of the next region. */
21904
21905 addrmap_index_data.objfile = objfile;
21906 addrmap_index_data.addr_obstack = obstack;
21907 addrmap_index_data.cu_index_htab = cu_index_htab;
21908 addrmap_index_data.previous_valid = 0;
21909
21910 addrmap_foreach (objfile->psymtabs_addrmap, add_address_entry_worker,
21911 &addrmap_index_data);
21912
21913 /* It's highly unlikely the last entry (end address = 0xff...ff)
21914 is valid, but we should still handle it.
21915 The end address is recorded as the start of the next region, but that
21916 doesn't work here. To cope we pass 0xff...ff, this is a rare situation
21917 anyway. */
21918 if (addrmap_index_data.previous_valid)
21919 add_address_entry (objfile, obstack,
21920 addrmap_index_data.previous_cu_start, (CORE_ADDR) -1,
21921 addrmap_index_data.previous_cu_index);
9291a0cd
TT
21922}
21923
156942c7
DE
21924/* Return the symbol kind of PSYM. */
21925
21926static gdb_index_symbol_kind
21927symbol_kind (struct partial_symbol *psym)
21928{
21929 domain_enum domain = PSYMBOL_DOMAIN (psym);
21930 enum address_class aclass = PSYMBOL_CLASS (psym);
21931
21932 switch (domain)
21933 {
21934 case VAR_DOMAIN:
21935 switch (aclass)
21936 {
21937 case LOC_BLOCK:
21938 return GDB_INDEX_SYMBOL_KIND_FUNCTION;
21939 case LOC_TYPEDEF:
21940 return GDB_INDEX_SYMBOL_KIND_TYPE;
21941 case LOC_COMPUTED:
21942 case LOC_CONST_BYTES:
21943 case LOC_OPTIMIZED_OUT:
21944 case LOC_STATIC:
21945 return GDB_INDEX_SYMBOL_KIND_VARIABLE;
21946 case LOC_CONST:
21947 /* Note: It's currently impossible to recognize psyms as enum values
21948 short of reading the type info. For now punt. */
21949 return GDB_INDEX_SYMBOL_KIND_VARIABLE;
21950 default:
21951 /* There are other LOC_FOO values that one might want to classify
21952 as variables, but dwarf2read.c doesn't currently use them. */
21953 return GDB_INDEX_SYMBOL_KIND_OTHER;
21954 }
21955 case STRUCT_DOMAIN:
21956 return GDB_INDEX_SYMBOL_KIND_TYPE;
21957 default:
21958 return GDB_INDEX_SYMBOL_KIND_OTHER;
21959 }
21960}
21961
9291a0cd 21962/* Add a list of partial symbols to SYMTAB. */
b89be57b 21963
9291a0cd
TT
21964static void
21965write_psymbols (struct mapped_symtab *symtab,
987d643c 21966 htab_t psyms_seen,
9291a0cd
TT
21967 struct partial_symbol **psymp,
21968 int count,
987d643c
TT
21969 offset_type cu_index,
21970 int is_static)
9291a0cd
TT
21971{
21972 for (; count-- > 0; ++psymp)
21973 {
156942c7
DE
21974 struct partial_symbol *psym = *psymp;
21975 void **slot;
987d643c 21976
156942c7 21977 if (SYMBOL_LANGUAGE (psym) == language_ada)
9291a0cd 21978 error (_("Ada is not currently supported by the index"));
987d643c 21979
987d643c 21980 /* Only add a given psymbol once. */
156942c7 21981 slot = htab_find_slot (psyms_seen, psym, INSERT);
987d643c
TT
21982 if (!*slot)
21983 {
156942c7
DE
21984 gdb_index_symbol_kind kind = symbol_kind (psym);
21985
21986 *slot = psym;
21987 add_index_entry (symtab, SYMBOL_SEARCH_NAME (psym),
21988 is_static, kind, cu_index);
987d643c 21989 }
9291a0cd
TT
21990 }
21991}
21992
21993/* Write the contents of an ("unfinished") obstack to FILE. Throw an
21994 exception if there is an error. */
b89be57b 21995
9291a0cd
TT
21996static void
21997write_obstack (FILE *file, struct obstack *obstack)
21998{
21999 if (fwrite (obstack_base (obstack), 1, obstack_object_size (obstack),
22000 file)
22001 != obstack_object_size (obstack))
22002 error (_("couldn't data write to file"));
22003}
22004
22005/* Unlink a file if the argument is not NULL. */
b89be57b 22006
9291a0cd
TT
22007static void
22008unlink_if_set (void *p)
22009{
22010 char **filename = p;
22011 if (*filename)
22012 unlink (*filename);
22013}
22014
1fd400ff
TT
22015/* A helper struct used when iterating over debug_types. */
22016struct signatured_type_index_data
22017{
22018 struct objfile *objfile;
22019 struct mapped_symtab *symtab;
22020 struct obstack *types_list;
987d643c 22021 htab_t psyms_seen;
1fd400ff
TT
22022 int cu_index;
22023};
22024
22025/* A helper function that writes a single signatured_type to an
22026 obstack. */
b89be57b 22027
1fd400ff
TT
22028static int
22029write_one_signatured_type (void **slot, void *d)
22030{
22031 struct signatured_type_index_data *info = d;
22032 struct signatured_type *entry = (struct signatured_type *) *slot;
0186c6a7 22033 struct partial_symtab *psymtab = entry->per_cu.v.psymtab;
1fd400ff
TT
22034 gdb_byte val[8];
22035
22036 write_psymbols (info->symtab,
987d643c 22037 info->psyms_seen,
3e43a32a
MS
22038 info->objfile->global_psymbols.list
22039 + psymtab->globals_offset,
987d643c
TT
22040 psymtab->n_global_syms, info->cu_index,
22041 0);
1fd400ff 22042 write_psymbols (info->symtab,
987d643c 22043 info->psyms_seen,
3e43a32a
MS
22044 info->objfile->static_psymbols.list
22045 + psymtab->statics_offset,
987d643c
TT
22046 psymtab->n_static_syms, info->cu_index,
22047 1);
1fd400ff 22048
b64f50a1
JK
22049 store_unsigned_integer (val, 8, BFD_ENDIAN_LITTLE,
22050 entry->per_cu.offset.sect_off);
1fd400ff 22051 obstack_grow (info->types_list, val, 8);
3019eac3
DE
22052 store_unsigned_integer (val, 8, BFD_ENDIAN_LITTLE,
22053 entry->type_offset_in_tu.cu_off);
1fd400ff
TT
22054 obstack_grow (info->types_list, val, 8);
22055 store_unsigned_integer (val, 8, BFD_ENDIAN_LITTLE, entry->signature);
22056 obstack_grow (info->types_list, val, 8);
22057
22058 ++info->cu_index;
22059
22060 return 1;
22061}
22062
95554aad
TT
22063/* Recurse into all "included" dependencies and write their symbols as
22064 if they appeared in this psymtab. */
22065
22066static void
22067recursively_write_psymbols (struct objfile *objfile,
22068 struct partial_symtab *psymtab,
22069 struct mapped_symtab *symtab,
22070 htab_t psyms_seen,
22071 offset_type cu_index)
22072{
22073 int i;
22074
22075 for (i = 0; i < psymtab->number_of_dependencies; ++i)
22076 if (psymtab->dependencies[i]->user != NULL)
22077 recursively_write_psymbols (objfile, psymtab->dependencies[i],
22078 symtab, psyms_seen, cu_index);
22079
22080 write_psymbols (symtab,
22081 psyms_seen,
22082 objfile->global_psymbols.list + psymtab->globals_offset,
22083 psymtab->n_global_syms, cu_index,
22084 0);
22085 write_psymbols (symtab,
22086 psyms_seen,
22087 objfile->static_psymbols.list + psymtab->statics_offset,
22088 psymtab->n_static_syms, cu_index,
22089 1);
22090}
22091
9291a0cd 22092/* Create an index file for OBJFILE in the directory DIR. */
b89be57b 22093
9291a0cd
TT
22094static void
22095write_psymtabs_to_index (struct objfile *objfile, const char *dir)
22096{
22097 struct cleanup *cleanup;
22098 char *filename, *cleanup_filename;
1fd400ff
TT
22099 struct obstack contents, addr_obstack, constant_pool, symtab_obstack;
22100 struct obstack cu_list, types_cu_list;
9291a0cd
TT
22101 int i;
22102 FILE *out_file;
22103 struct mapped_symtab *symtab;
22104 offset_type val, size_of_contents, total_len;
22105 struct stat st;
987d643c 22106 htab_t psyms_seen;
0a5429f6
DE
22107 htab_t cu_index_htab;
22108 struct psymtab_cu_index_map *psymtab_cu_index_map;
9291a0cd 22109
9291a0cd
TT
22110 if (dwarf2_per_objfile->using_index)
22111 error (_("Cannot use an index to create the index"));
22112
8b70b953
TT
22113 if (VEC_length (dwarf2_section_info_def, dwarf2_per_objfile->types) > 1)
22114 error (_("Cannot make an index when the file has multiple .debug_types sections"));
22115
260b681b
DE
22116 if (!objfile->psymtabs || !objfile->psymtabs_addrmap)
22117 return;
22118
4262abfb
JK
22119 if (stat (objfile_name (objfile), &st) < 0)
22120 perror_with_name (objfile_name (objfile));
9291a0cd 22121
4262abfb 22122 filename = concat (dir, SLASH_STRING, lbasename (objfile_name (objfile)),
9291a0cd
TT
22123 INDEX_SUFFIX, (char *) NULL);
22124 cleanup = make_cleanup (xfree, filename);
22125
614c279d 22126 out_file = gdb_fopen_cloexec (filename, "wb");
9291a0cd
TT
22127 if (!out_file)
22128 error (_("Can't open `%s' for writing"), filename);
22129
22130 cleanup_filename = filename;
22131 make_cleanup (unlink_if_set, &cleanup_filename);
22132
22133 symtab = create_mapped_symtab ();
22134 make_cleanup (cleanup_mapped_symtab, symtab);
22135
22136 obstack_init (&addr_obstack);
22137 make_cleanup_obstack_free (&addr_obstack);
22138
22139 obstack_init (&cu_list);
22140 make_cleanup_obstack_free (&cu_list);
22141
1fd400ff
TT
22142 obstack_init (&types_cu_list);
22143 make_cleanup_obstack_free (&types_cu_list);
22144
987d643c
TT
22145 psyms_seen = htab_create_alloc (100, htab_hash_pointer, htab_eq_pointer,
22146 NULL, xcalloc, xfree);
96408a79 22147 make_cleanup_htab_delete (psyms_seen);
987d643c 22148
0a5429f6
DE
22149 /* While we're scanning CU's create a table that maps a psymtab pointer
22150 (which is what addrmap records) to its index (which is what is recorded
22151 in the index file). This will later be needed to write the address
22152 table. */
22153 cu_index_htab = htab_create_alloc (100,
22154 hash_psymtab_cu_index,
22155 eq_psymtab_cu_index,
22156 NULL, xcalloc, xfree);
96408a79 22157 make_cleanup_htab_delete (cu_index_htab);
0a5429f6
DE
22158 psymtab_cu_index_map = (struct psymtab_cu_index_map *)
22159 xmalloc (sizeof (struct psymtab_cu_index_map)
22160 * dwarf2_per_objfile->n_comp_units);
22161 make_cleanup (xfree, psymtab_cu_index_map);
22162
22163 /* The CU list is already sorted, so we don't need to do additional
1fd400ff
TT
22164 work here. Also, the debug_types entries do not appear in
22165 all_comp_units, but only in their own hash table. */
9291a0cd
TT
22166 for (i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
22167 {
3e43a32a
MS
22168 struct dwarf2_per_cu_data *per_cu
22169 = dwarf2_per_objfile->all_comp_units[i];
e254ef6a 22170 struct partial_symtab *psymtab = per_cu->v.psymtab;
9291a0cd 22171 gdb_byte val[8];
0a5429f6
DE
22172 struct psymtab_cu_index_map *map;
22173 void **slot;
9291a0cd 22174
92fac807
JK
22175 /* CU of a shared file from 'dwz -m' may be unused by this main file.
22176 It may be referenced from a local scope but in such case it does not
22177 need to be present in .gdb_index. */
22178 if (psymtab == NULL)
22179 continue;
22180
95554aad
TT
22181 if (psymtab->user == NULL)
22182 recursively_write_psymbols (objfile, psymtab, symtab, psyms_seen, i);
9291a0cd 22183
0a5429f6
DE
22184 map = &psymtab_cu_index_map[i];
22185 map->psymtab = psymtab;
22186 map->cu_index = i;
22187 slot = htab_find_slot (cu_index_htab, map, INSERT);
22188 gdb_assert (slot != NULL);
22189 gdb_assert (*slot == NULL);
22190 *slot = map;
9291a0cd 22191
b64f50a1
JK
22192 store_unsigned_integer (val, 8, BFD_ENDIAN_LITTLE,
22193 per_cu->offset.sect_off);
9291a0cd 22194 obstack_grow (&cu_list, val, 8);
e254ef6a 22195 store_unsigned_integer (val, 8, BFD_ENDIAN_LITTLE, per_cu->length);
9291a0cd
TT
22196 obstack_grow (&cu_list, val, 8);
22197 }
22198
0a5429f6
DE
22199 /* Dump the address map. */
22200 write_address_map (objfile, &addr_obstack, cu_index_htab);
22201
1fd400ff
TT
22202 /* Write out the .debug_type entries, if any. */
22203 if (dwarf2_per_objfile->signatured_types)
22204 {
22205 struct signatured_type_index_data sig_data;
22206
22207 sig_data.objfile = objfile;
22208 sig_data.symtab = symtab;
22209 sig_data.types_list = &types_cu_list;
987d643c 22210 sig_data.psyms_seen = psyms_seen;
1fd400ff
TT
22211 sig_data.cu_index = dwarf2_per_objfile->n_comp_units;
22212 htab_traverse_noresize (dwarf2_per_objfile->signatured_types,
22213 write_one_signatured_type, &sig_data);
22214 }
22215
156942c7
DE
22216 /* Now that we've processed all symbols we can shrink their cu_indices
22217 lists. */
22218 uniquify_cu_indices (symtab);
22219
9291a0cd
TT
22220 obstack_init (&constant_pool);
22221 make_cleanup_obstack_free (&constant_pool);
22222 obstack_init (&symtab_obstack);
22223 make_cleanup_obstack_free (&symtab_obstack);
22224 write_hash_table (symtab, &symtab_obstack, &constant_pool);
22225
22226 obstack_init (&contents);
22227 make_cleanup_obstack_free (&contents);
1fd400ff 22228 size_of_contents = 6 * sizeof (offset_type);
9291a0cd
TT
22229 total_len = size_of_contents;
22230
22231 /* The version number. */
796a7ff8 22232 val = MAYBE_SWAP (8);
9291a0cd
TT
22233 obstack_grow (&contents, &val, sizeof (val));
22234
22235 /* The offset of the CU list from the start of the file. */
22236 val = MAYBE_SWAP (total_len);
22237 obstack_grow (&contents, &val, sizeof (val));
22238 total_len += obstack_object_size (&cu_list);
22239
1fd400ff
TT
22240 /* The offset of the types CU list from the start of the file. */
22241 val = MAYBE_SWAP (total_len);
22242 obstack_grow (&contents, &val, sizeof (val));
22243 total_len += obstack_object_size (&types_cu_list);
22244
9291a0cd
TT
22245 /* The offset of the address table from the start of the file. */
22246 val = MAYBE_SWAP (total_len);
22247 obstack_grow (&contents, &val, sizeof (val));
22248 total_len += obstack_object_size (&addr_obstack);
22249
22250 /* The offset of the symbol table from the start of the file. */
22251 val = MAYBE_SWAP (total_len);
22252 obstack_grow (&contents, &val, sizeof (val));
22253 total_len += obstack_object_size (&symtab_obstack);
22254
22255 /* The offset of the constant pool from the start of the file. */
22256 val = MAYBE_SWAP (total_len);
22257 obstack_grow (&contents, &val, sizeof (val));
22258 total_len += obstack_object_size (&constant_pool);
22259
22260 gdb_assert (obstack_object_size (&contents) == size_of_contents);
22261
22262 write_obstack (out_file, &contents);
22263 write_obstack (out_file, &cu_list);
1fd400ff 22264 write_obstack (out_file, &types_cu_list);
9291a0cd
TT
22265 write_obstack (out_file, &addr_obstack);
22266 write_obstack (out_file, &symtab_obstack);
22267 write_obstack (out_file, &constant_pool);
22268
22269 fclose (out_file);
22270
22271 /* We want to keep the file, so we set cleanup_filename to NULL
22272 here. See unlink_if_set. */
22273 cleanup_filename = NULL;
22274
22275 do_cleanups (cleanup);
22276}
22277
90476074
TT
22278/* Implementation of the `save gdb-index' command.
22279
22280 Note that the file format used by this command is documented in the
22281 GDB manual. Any changes here must be documented there. */
11570e71 22282
9291a0cd
TT
22283static void
22284save_gdb_index_command (char *arg, int from_tty)
22285{
22286 struct objfile *objfile;
22287
22288 if (!arg || !*arg)
96d19272 22289 error (_("usage: save gdb-index DIRECTORY"));
9291a0cd
TT
22290
22291 ALL_OBJFILES (objfile)
22292 {
22293 struct stat st;
22294
22295 /* If the objfile does not correspond to an actual file, skip it. */
4262abfb 22296 if (stat (objfile_name (objfile), &st) < 0)
9291a0cd
TT
22297 continue;
22298
22299 dwarf2_per_objfile = objfile_data (objfile, dwarf2_objfile_data_key);
22300 if (dwarf2_per_objfile)
22301 {
22302 volatile struct gdb_exception except;
22303
22304 TRY_CATCH (except, RETURN_MASK_ERROR)
22305 {
22306 write_psymtabs_to_index (objfile, arg);
22307 }
22308 if (except.reason < 0)
22309 exception_fprintf (gdb_stderr, except,
22310 _("Error while writing index for `%s': "),
4262abfb 22311 objfile_name (objfile));
9291a0cd
TT
22312 }
22313 }
dce234bc
PP
22314}
22315
9291a0cd
TT
22316\f
22317
9eae7c52
TT
22318int dwarf2_always_disassemble;
22319
22320static void
22321show_dwarf2_always_disassemble (struct ui_file *file, int from_tty,
22322 struct cmd_list_element *c, const char *value)
22323{
3e43a32a
MS
22324 fprintf_filtered (file,
22325 _("Whether to always disassemble "
22326 "DWARF expressions is %s.\n"),
9eae7c52
TT
22327 value);
22328}
22329
900e11f9
JK
22330static void
22331show_check_physname (struct ui_file *file, int from_tty,
22332 struct cmd_list_element *c, const char *value)
22333{
22334 fprintf_filtered (file,
22335 _("Whether to check \"physname\" is %s.\n"),
22336 value);
22337}
22338
6502dd73
DJ
22339void _initialize_dwarf2_read (void);
22340
22341void
22342_initialize_dwarf2_read (void)
22343{
96d19272
JK
22344 struct cmd_list_element *c;
22345
dce234bc 22346 dwarf2_objfile_data_key
c1bd65d0 22347 = register_objfile_data_with_cleanup (NULL, dwarf2_per_objfile_free);
ae038cb0 22348
1bedd215
AC
22349 add_prefix_cmd ("dwarf2", class_maintenance, set_dwarf2_cmd, _("\
22350Set DWARF 2 specific variables.\n\
22351Configure DWARF 2 variables such as the cache size"),
ae038cb0
DJ
22352 &set_dwarf2_cmdlist, "maintenance set dwarf2 ",
22353 0/*allow-unknown*/, &maintenance_set_cmdlist);
22354
1bedd215
AC
22355 add_prefix_cmd ("dwarf2", class_maintenance, show_dwarf2_cmd, _("\
22356Show DWARF 2 specific variables\n\
22357Show DWARF 2 variables such as the cache size"),
ae038cb0
DJ
22358 &show_dwarf2_cmdlist, "maintenance show dwarf2 ",
22359 0/*allow-unknown*/, &maintenance_show_cmdlist);
22360
22361 add_setshow_zinteger_cmd ("max-cache-age", class_obscure,
7915a72c
AC
22362 &dwarf2_max_cache_age, _("\
22363Set the upper bound on the age of cached dwarf2 compilation units."), _("\
22364Show the upper bound on the age of cached dwarf2 compilation units."), _("\
22365A higher limit means that cached compilation units will be stored\n\
22366in memory longer, and more total memory will be used. Zero disables\n\
22367caching, which can slow down startup."),
2c5b56ce 22368 NULL,
920d2a44 22369 show_dwarf2_max_cache_age,
2c5b56ce 22370 &set_dwarf2_cmdlist,
ae038cb0 22371 &show_dwarf2_cmdlist);
d97bc12b 22372
9eae7c52
TT
22373 add_setshow_boolean_cmd ("always-disassemble", class_obscure,
22374 &dwarf2_always_disassemble, _("\
22375Set whether `info address' always disassembles DWARF expressions."), _("\
22376Show whether `info address' always disassembles DWARF expressions."), _("\
22377When enabled, DWARF expressions are always printed in an assembly-like\n\
22378syntax. When disabled, expressions will be printed in a more\n\
22379conversational style, when possible."),
22380 NULL,
22381 show_dwarf2_always_disassemble,
22382 &set_dwarf2_cmdlist,
22383 &show_dwarf2_cmdlist);
22384
45cfd468
DE
22385 add_setshow_boolean_cmd ("dwarf2-read", no_class, &dwarf2_read_debug, _("\
22386Set debugging of the dwarf2 reader."), _("\
22387Show debugging of the dwarf2 reader."), _("\
22388When enabled, debugging messages are printed during dwarf2 reading\n\
22389and symtab expansion."),
22390 NULL,
22391 NULL,
22392 &setdebuglist, &showdebuglist);
22393
ccce17b0 22394 add_setshow_zuinteger_cmd ("dwarf2-die", no_class, &dwarf2_die_debug, _("\
d97bc12b
DE
22395Set debugging of the dwarf2 DIE reader."), _("\
22396Show debugging of the dwarf2 DIE reader."), _("\
22397When enabled (non-zero), DIEs are dumped after they are read in.\n\
22398The value is the maximum depth to print."),
ccce17b0
YQ
22399 NULL,
22400 NULL,
22401 &setdebuglist, &showdebuglist);
9291a0cd 22402
900e11f9
JK
22403 add_setshow_boolean_cmd ("check-physname", no_class, &check_physname, _("\
22404Set cross-checking of \"physname\" code against demangler."), _("\
22405Show cross-checking of \"physname\" code against demangler."), _("\
22406When enabled, GDB's internal \"physname\" code is checked against\n\
22407the demangler."),
22408 NULL, show_check_physname,
22409 &setdebuglist, &showdebuglist);
22410
e615022a
DE
22411 add_setshow_boolean_cmd ("use-deprecated-index-sections",
22412 no_class, &use_deprecated_index_sections, _("\
22413Set whether to use deprecated gdb_index sections."), _("\
22414Show whether to use deprecated gdb_index sections."), _("\
22415When enabled, deprecated .gdb_index sections are used anyway.\n\
22416Normally they are ignored either because of a missing feature or\n\
22417performance issue.\n\
22418Warning: This option must be enabled before gdb reads the file."),
22419 NULL,
22420 NULL,
22421 &setlist, &showlist);
22422
96d19272 22423 c = add_cmd ("gdb-index", class_files, save_gdb_index_command,
11570e71 22424 _("\
fc1a9d6e 22425Save a gdb-index file.\n\
11570e71 22426Usage: save gdb-index DIRECTORY"),
96d19272
JK
22427 &save_cmdlist);
22428 set_cmd_completer (c, filename_completer);
f1e6e072
TT
22429
22430 dwarf2_locexpr_index = register_symbol_computed_impl (LOC_COMPUTED,
22431 &dwarf2_locexpr_funcs);
22432 dwarf2_loclist_index = register_symbol_computed_impl (LOC_COMPUTED,
22433 &dwarf2_loclist_funcs);
22434
22435 dwarf2_locexpr_block_index = register_symbol_block_impl (LOC_BLOCK,
22436 &dwarf2_block_frame_base_locexpr_funcs);
22437 dwarf2_loclist_block_index = register_symbol_block_impl (LOC_BLOCK,
22438 &dwarf2_block_frame_base_loclist_funcs);
6502dd73 22439}
This page took 3.230077 seconds and 4 git commands to generate.