Properly check indicies bigger than 4Gb
[deliverable/binutils-gdb.git] / gdb / dwarf2read.c
CommitLineData
c906108c 1/* DWARF 2 debugging format support for GDB.
917c78fc 2
0b302171 3 Copyright (C) 1994-2012 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"
4c2df51b 70
c906108c
SS
71#include <fcntl.h>
72#include "gdb_string.h"
4bdf3d34 73#include "gdb_assert.h"
c906108c 74#include <sys/types.h>
d8151005 75
34eaf542
TT
76typedef struct symbol *symbolp;
77DEF_VEC_P (symbolp);
78
45cfd468
DE
79/* When non-zero, print basic high level tracing messages.
80 This is in contrast to the low level DIE reading of dwarf2_die_debug. */
81static int dwarf2_read_debug = 0;
82
d97bc12b 83/* When non-zero, dump DIEs after they are read in. */
ccce17b0 84static unsigned int dwarf2_die_debug = 0;
d97bc12b 85
900e11f9
JK
86/* When non-zero, cross-check physname against demangler. */
87static int check_physname = 0;
88
481860b3 89/* When non-zero, do not reject deprecated .gdb_index sections. */
e615022a 90static int use_deprecated_index_sections = 0;
481860b3 91
df8a16a1
DJ
92/* When set, the file that we're processing is known to have debugging
93 info for C++ namespaces. GCC 3.3.x did not produce this information,
94 but later versions do. */
95
96static int processing_has_namespace_info;
97
6502dd73
DJ
98static const struct objfile_data *dwarf2_objfile_data_key;
99
dce234bc
PP
100struct dwarf2_section_info
101{
102 asection *asection;
103 gdb_byte *buffer;
104 bfd_size_type size;
be391dca
TT
105 /* True if we have tried to read this section. */
106 int readin;
dce234bc
PP
107};
108
8b70b953
TT
109typedef struct dwarf2_section_info dwarf2_section_info_def;
110DEF_VEC_O (dwarf2_section_info_def);
111
9291a0cd
TT
112/* All offsets in the index are of this type. It must be
113 architecture-independent. */
114typedef uint32_t offset_type;
115
116DEF_VEC_I (offset_type);
117
156942c7
DE
118/* Ensure only legit values are used. */
119#define DW2_GDB_INDEX_SYMBOL_STATIC_SET_VALUE(cu_index, value) \
120 do { \
121 gdb_assert ((unsigned int) (value) <= 1); \
122 GDB_INDEX_SYMBOL_STATIC_SET_VALUE((cu_index), (value)); \
123 } while (0)
124
125/* Ensure only legit values are used. */
126#define DW2_GDB_INDEX_SYMBOL_KIND_SET_VALUE(cu_index, value) \
127 do { \
128 gdb_assert ((value) >= GDB_INDEX_SYMBOL_KIND_TYPE \
129 && (value) <= GDB_INDEX_SYMBOL_KIND_OTHER); \
130 GDB_INDEX_SYMBOL_KIND_SET_VALUE((cu_index), (value)); \
131 } while (0)
132
133/* Ensure we don't use more than the alloted nuber of bits for the CU. */
134#define DW2_GDB_INDEX_CU_SET_VALUE(cu_index, value) \
135 do { \
136 gdb_assert (((value) & ~GDB_INDEX_CU_MASK) == 0); \
137 GDB_INDEX_CU_SET_VALUE((cu_index), (value)); \
138 } while (0)
139
9291a0cd
TT
140/* A description of the mapped index. The file format is described in
141 a comment by the code that writes the index. */
142struct mapped_index
143{
559a7a62
JK
144 /* Index data format version. */
145 int version;
146
9291a0cd
TT
147 /* The total length of the buffer. */
148 off_t total_size;
b11b1f88 149
9291a0cd
TT
150 /* A pointer to the address table data. */
151 const gdb_byte *address_table;
b11b1f88 152
9291a0cd
TT
153 /* Size of the address table data in bytes. */
154 offset_type address_table_size;
b11b1f88 155
3876f04e
DE
156 /* The symbol table, implemented as a hash table. */
157 const offset_type *symbol_table;
b11b1f88 158
9291a0cd 159 /* Size in slots, each slot is 2 offset_types. */
3876f04e 160 offset_type symbol_table_slots;
b11b1f88 161
9291a0cd
TT
162 /* A pointer to the constant pool. */
163 const char *constant_pool;
164};
165
95554aad
TT
166typedef struct dwarf2_per_cu_data *dwarf2_per_cu_ptr;
167DEF_VEC_P (dwarf2_per_cu_ptr);
168
9cdd5dbd
DE
169/* Collection of data recorded per objfile.
170 This hangs off of dwarf2_objfile_data_key. */
171
6502dd73
DJ
172struct dwarf2_per_objfile
173{
dce234bc
PP
174 struct dwarf2_section_info info;
175 struct dwarf2_section_info abbrev;
176 struct dwarf2_section_info line;
dce234bc
PP
177 struct dwarf2_section_info loc;
178 struct dwarf2_section_info macinfo;
cf2c3c16 179 struct dwarf2_section_info macro;
dce234bc
PP
180 struct dwarf2_section_info str;
181 struct dwarf2_section_info ranges;
3019eac3 182 struct dwarf2_section_info addr;
dce234bc
PP
183 struct dwarf2_section_info frame;
184 struct dwarf2_section_info eh_frame;
9291a0cd 185 struct dwarf2_section_info gdb_index;
ae038cb0 186
8b70b953
TT
187 VEC (dwarf2_section_info_def) *types;
188
be391dca
TT
189 /* Back link. */
190 struct objfile *objfile;
191
d467dd73 192 /* Table of all the compilation units. This is used to locate
10b3939b 193 the target compilation unit of a particular reference. */
ae038cb0
DJ
194 struct dwarf2_per_cu_data **all_comp_units;
195
196 /* The number of compilation units in ALL_COMP_UNITS. */
197 int n_comp_units;
198
1fd400ff 199 /* The number of .debug_types-related CUs. */
d467dd73 200 int n_type_units;
1fd400ff 201
d467dd73 202 /* The .debug_types-related CUs (TUs). */
b4dd5633 203 struct signatured_type **all_type_units;
1fd400ff 204
f4dc4d17
DE
205 /* The number of entries in all_type_unit_groups. */
206 int n_type_unit_groups;
207
208 /* Table of type unit groups.
209 This exists to make it easy to iterate over all CUs and TU groups. */
210 struct type_unit_group **all_type_unit_groups;
211
212 /* Table of struct type_unit_group objects.
213 The hash key is the DW_AT_stmt_list value. */
214 htab_t type_unit_groups;
72dca2f5 215
348e048f
DE
216 /* A table mapping .debug_types signatures to its signatured_type entry.
217 This is NULL if the .debug_types section hasn't been read in yet. */
218 htab_t signatured_types;
219
f4dc4d17
DE
220 /* Type unit statistics, to see how well the scaling improvements
221 are doing. */
222 struct tu_stats
223 {
224 int nr_uniq_abbrev_tables;
225 int nr_symtabs;
226 int nr_symtab_sharers;
227 int nr_stmt_less_type_units;
228 } tu_stats;
229
230 /* A chain of compilation units that are currently read in, so that
231 they can be freed later. */
232 struct dwarf2_per_cu_data *read_in_chain;
233
3019eac3
DE
234 /* A table mapping DW_AT_dwo_name values to struct dwo_file objects.
235 This is NULL if the table hasn't been allocated yet. */
236 htab_t dwo_files;
237
80626a55
DE
238 /* Non-zero if we've check for whether there is a DWP file. */
239 int dwp_checked;
240
241 /* The DWP file if there is one, or NULL. */
242 struct dwp_file *dwp_file;
243
36586728
TT
244 /* The shared '.dwz' file, if one exists. This is used when the
245 original data was compressed using 'dwz -m'. */
246 struct dwz_file *dwz_file;
247
72dca2f5
FR
248 /* A flag indicating wether this objfile has a section loaded at a
249 VMA of 0. */
250 int has_section_at_zero;
9291a0cd 251
ae2de4f8
DE
252 /* True if we are using the mapped index,
253 or we are faking it for OBJF_READNOW's sake. */
9291a0cd
TT
254 unsigned char using_index;
255
ae2de4f8 256 /* The mapped index, or NULL if .gdb_index is missing or not being used. */
9291a0cd 257 struct mapped_index *index_table;
98bfdba5 258
7b9f3c50 259 /* When using index_table, this keeps track of all quick_file_names entries.
56e64610
DE
260 TUs typically share line table entries with a CU, so we maintain a
261 separate table of all line table entries to support the sharing.
262 Note that while there can be way more TUs than CUs, we've already
263 sorted all the TUs into "type unit groups", grouped by their
264 DW_AT_stmt_list value. Therefore the only sharing done here is with a
265 CU and its associated TU group if there is one. */
7b9f3c50
DE
266 htab_t quick_file_names_table;
267
98bfdba5
PA
268 /* Set during partial symbol reading, to prevent queueing of full
269 symbols. */
270 int reading_partial_symbols;
673bfd45 271
dee91e82 272 /* Table mapping type DIEs to their struct type *.
673bfd45 273 This is NULL if not allocated yet.
dee91e82
DE
274 The mapping is done via (CU/TU signature + DIE offset) -> type. */
275 htab_t die_type_hash;
95554aad
TT
276
277 /* The CUs we recently read. */
278 VEC (dwarf2_per_cu_ptr) *just_read_cus;
6502dd73
DJ
279};
280
281static struct dwarf2_per_objfile *dwarf2_per_objfile;
c906108c 282
251d32d9 283/* Default names of the debugging sections. */
c906108c 284
233a11ab
CS
285/* Note that if the debugging section has been compressed, it might
286 have a name like .zdebug_info. */
287
9cdd5dbd
DE
288static const struct dwarf2_debug_sections dwarf2_elf_names =
289{
251d32d9
TG
290 { ".debug_info", ".zdebug_info" },
291 { ".debug_abbrev", ".zdebug_abbrev" },
292 { ".debug_line", ".zdebug_line" },
293 { ".debug_loc", ".zdebug_loc" },
294 { ".debug_macinfo", ".zdebug_macinfo" },
cf2c3c16 295 { ".debug_macro", ".zdebug_macro" },
251d32d9
TG
296 { ".debug_str", ".zdebug_str" },
297 { ".debug_ranges", ".zdebug_ranges" },
298 { ".debug_types", ".zdebug_types" },
3019eac3 299 { ".debug_addr", ".zdebug_addr" },
251d32d9
TG
300 { ".debug_frame", ".zdebug_frame" },
301 { ".eh_frame", NULL },
24d3216f
TT
302 { ".gdb_index", ".zgdb_index" },
303 23
251d32d9 304};
c906108c 305
80626a55 306/* List of DWO/DWP sections. */
3019eac3 307
80626a55 308static const struct dwop_section_names
3019eac3
DE
309{
310 struct dwarf2_section_names abbrev_dwo;
311 struct dwarf2_section_names info_dwo;
312 struct dwarf2_section_names line_dwo;
313 struct dwarf2_section_names loc_dwo;
09262596
DE
314 struct dwarf2_section_names macinfo_dwo;
315 struct dwarf2_section_names macro_dwo;
3019eac3
DE
316 struct dwarf2_section_names str_dwo;
317 struct dwarf2_section_names str_offsets_dwo;
318 struct dwarf2_section_names types_dwo;
80626a55
DE
319 struct dwarf2_section_names cu_index;
320 struct dwarf2_section_names tu_index;
3019eac3 321}
80626a55 322dwop_section_names =
3019eac3
DE
323{
324 { ".debug_abbrev.dwo", ".zdebug_abbrev.dwo" },
325 { ".debug_info.dwo", ".zdebug_info.dwo" },
326 { ".debug_line.dwo", ".zdebug_line.dwo" },
327 { ".debug_loc.dwo", ".zdebug_loc.dwo" },
09262596
DE
328 { ".debug_macinfo.dwo", ".zdebug_macinfo.dwo" },
329 { ".debug_macro.dwo", ".zdebug_macro.dwo" },
3019eac3
DE
330 { ".debug_str.dwo", ".zdebug_str.dwo" },
331 { ".debug_str_offsets.dwo", ".zdebug_str_offsets.dwo" },
332 { ".debug_types.dwo", ".zdebug_types.dwo" },
80626a55
DE
333 { ".debug_cu_index", ".zdebug_cu_index" },
334 { ".debug_tu_index", ".zdebug_tu_index" },
3019eac3
DE
335};
336
c906108c
SS
337/* local data types */
338
107d2387
AC
339/* The data in a compilation unit header, after target2host
340 translation, looks like this. */
c906108c 341struct comp_unit_head
a738430d 342{
c764a876 343 unsigned int length;
a738430d 344 short version;
a738430d
MK
345 unsigned char addr_size;
346 unsigned char signed_addr_p;
b64f50a1 347 sect_offset abbrev_offset;
57349743 348
a738430d
MK
349 /* Size of file offsets; either 4 or 8. */
350 unsigned int offset_size;
57349743 351
a738430d
MK
352 /* Size of the length field; either 4 or 12. */
353 unsigned int initial_length_size;
57349743 354
a738430d
MK
355 /* Offset to the first byte of this compilation unit header in the
356 .debug_info section, for resolving relative reference dies. */
b64f50a1 357 sect_offset offset;
57349743 358
d00adf39
DE
359 /* Offset to first die in this cu from the start of the cu.
360 This will be the first byte following the compilation unit header. */
b64f50a1 361 cu_offset first_die_offset;
a738430d 362};
c906108c 363
3da10d80
KS
364/* Type used for delaying computation of method physnames.
365 See comments for compute_delayed_physnames. */
366struct delayed_method_info
367{
368 /* The type to which the method is attached, i.e., its parent class. */
369 struct type *type;
370
371 /* The index of the method in the type's function fieldlists. */
372 int fnfield_index;
373
374 /* The index of the method in the fieldlist. */
375 int index;
376
377 /* The name of the DIE. */
378 const char *name;
379
380 /* The DIE associated with this method. */
381 struct die_info *die;
382};
383
384typedef struct delayed_method_info delayed_method_info;
385DEF_VEC_O (delayed_method_info);
386
e7c27a73
DJ
387/* Internal state when decoding a particular compilation unit. */
388struct dwarf2_cu
389{
390 /* The objfile containing this compilation unit. */
391 struct objfile *objfile;
392
d00adf39 393 /* The header of the compilation unit. */
e7c27a73 394 struct comp_unit_head header;
e142c38c 395
d00adf39
DE
396 /* Base address of this compilation unit. */
397 CORE_ADDR base_address;
398
399 /* Non-zero if base_address has been set. */
400 int base_known;
401
e142c38c
DJ
402 /* The language we are debugging. */
403 enum language language;
404 const struct language_defn *language_defn;
405
b0f35d58
DL
406 const char *producer;
407
e142c38c
DJ
408 /* The generic symbol table building routines have separate lists for
409 file scope symbols and all all other scopes (local scopes). So
410 we need to select the right one to pass to add_symbol_to_list().
411 We do it by keeping a pointer to the correct list in list_in_scope.
412
413 FIXME: The original dwarf code just treated the file scope as the
414 first local scope, and all other local scopes as nested local
415 scopes, and worked fine. Check to see if we really need to
416 distinguish these in buildsym.c. */
417 struct pending **list_in_scope;
418
433df2d4
DE
419 /* The abbrev table for this CU.
420 Normally this points to the abbrev table in the objfile.
421 But if DWO_UNIT is non-NULL this is the abbrev table in the DWO file. */
422 struct abbrev_table *abbrev_table;
72bf9492 423
b64f50a1
JK
424 /* Hash table holding all the loaded partial DIEs
425 with partial_die->offset.SECT_OFF as hash. */
72bf9492
DJ
426 htab_t partial_dies;
427
428 /* Storage for things with the same lifetime as this read-in compilation
429 unit, including partial DIEs. */
430 struct obstack comp_unit_obstack;
431
ae038cb0
DJ
432 /* When multiple dwarf2_cu structures are living in memory, this field
433 chains them all together, so that they can be released efficiently.
434 We will probably also want a generation counter so that most-recently-used
435 compilation units are cached... */
436 struct dwarf2_per_cu_data *read_in_chain;
437
438 /* Backchain to our per_cu entry if the tree has been built. */
439 struct dwarf2_per_cu_data *per_cu;
440
441 /* How many compilation units ago was this CU last referenced? */
442 int last_used;
443
b64f50a1
JK
444 /* A hash table of DIE cu_offset for following references with
445 die_info->offset.sect_off as hash. */
51545339 446 htab_t die_hash;
10b3939b
DJ
447
448 /* Full DIEs if read in. */
449 struct die_info *dies;
450
451 /* A set of pointers to dwarf2_per_cu_data objects for compilation
452 units referenced by this one. Only set during full symbol processing;
453 partial symbol tables do not have dependencies. */
454 htab_t dependencies;
455
cb1df416
DJ
456 /* Header data from the line table, during full symbol processing. */
457 struct line_header *line_header;
458
3da10d80
KS
459 /* A list of methods which need to have physnames computed
460 after all type information has been read. */
461 VEC (delayed_method_info) *method_list;
462
96408a79
SA
463 /* To be copied to symtab->call_site_htab. */
464 htab_t call_site_htab;
465
034e5797
DE
466 /* Non-NULL if this CU came from a DWO file.
467 There is an invariant here that is important to remember:
468 Except for attributes copied from the top level DIE in the "main"
469 (or "stub") file in preparation for reading the DWO file
470 (e.g., DW_AT_GNU_addr_base), we KISS: there is only *one* CU.
471 Either there isn't a DWO file (in which case this is NULL and the point
472 is moot), or there is and either we're not going to read it (in which
473 case this is NULL) or there is and we are reading it (in which case this
474 is non-NULL). */
3019eac3
DE
475 struct dwo_unit *dwo_unit;
476
477 /* The DW_AT_addr_base attribute if present, zero otherwise
478 (zero is a valid value though).
479 Note this value comes from the stub CU/TU's DIE. */
480 ULONGEST addr_base;
481
2e3cf129
DE
482 /* The DW_AT_ranges_base attribute if present, zero otherwise
483 (zero is a valid value though).
484 Note this value comes from the stub CU/TU's DIE.
485 Also note that the value is zero in the non-DWO case so this value can
486 be used without needing to know whether DWO files are in use or not. */
487 ULONGEST ranges_base;
488
ae038cb0
DJ
489 /* Mark used when releasing cached dies. */
490 unsigned int mark : 1;
491
8be455d7
JK
492 /* This CU references .debug_loc. See the symtab->locations_valid field.
493 This test is imperfect as there may exist optimized debug code not using
494 any location list and still facing inlining issues if handled as
495 unoptimized code. For a future better test see GCC PR other/32998. */
8be455d7 496 unsigned int has_loclist : 1;
ba919b58 497
1b80a9fa
JK
498 /* These cache the results for producer_is_* fields. CHECKED_PRODUCER is set
499 if all the producer_is_* fields are valid. This information is cached
500 because profiling CU expansion showed excessive time spent in
501 producer_is_gxx_lt_4_6. */
ba919b58
TT
502 unsigned int checked_producer : 1;
503 unsigned int producer_is_gxx_lt_4_6 : 1;
1b80a9fa 504 unsigned int producer_is_gcc_lt_4_3 : 1;
685b1105 505 unsigned int producer_is_icc : 1;
e7c27a73
DJ
506};
507
10b3939b
DJ
508/* Persistent data held for a compilation unit, even when not
509 processing it. We put a pointer to this structure in the
28dee7f5 510 read_symtab_private field of the psymtab. */
10b3939b 511
ae038cb0
DJ
512struct dwarf2_per_cu_data
513{
36586728 514 /* The start offset and length of this compilation unit.
45452591 515 NOTE: Unlike comp_unit_head.length, this length includes
3019eac3
DE
516 initial_length_size.
517 If the DIE refers to a DWO file, this is always of the original die,
518 not the DWO file. */
b64f50a1 519 sect_offset offset;
36586728 520 unsigned int length;
ae038cb0
DJ
521
522 /* Flag indicating this compilation unit will be read in before
523 any of the current compilation units are processed. */
c764a876 524 unsigned int queued : 1;
ae038cb0 525
0d99eb77
DE
526 /* This flag will be set when reading partial DIEs if we need to load
527 absolutely all DIEs for this compilation unit, instead of just the ones
528 we think are interesting. It gets set if we look for a DIE in the
5afb4e99
DJ
529 hash table and don't find it. */
530 unsigned int load_all_dies : 1;
531
3019eac3
DE
532 /* Non-zero if this CU is from .debug_types. */
533 unsigned int is_debug_types : 1;
534
36586728
TT
535 /* Non-zero if this CU is from the .dwz file. */
536 unsigned int is_dwz : 1;
537
3019eac3
DE
538 /* The section this CU/TU lives in.
539 If the DIE refers to a DWO file, this is always the original die,
540 not the DWO file. */
541 struct dwarf2_section_info *info_or_types_section;
348e048f 542
17ea53c3
JK
543 /* Set to non-NULL iff this CU is currently loaded. When it gets freed out
544 of the CU cache it gets reset to NULL again. */
ae038cb0 545 struct dwarf2_cu *cu;
1c379e20 546
9cdd5dbd
DE
547 /* The corresponding objfile.
548 Normally we can get the objfile from dwarf2_per_objfile.
549 However we can enter this file with just a "per_cu" handle. */
9291a0cd
TT
550 struct objfile *objfile;
551
552 /* When using partial symbol tables, the 'psymtab' field is active.
553 Otherwise the 'quick' field is active. */
554 union
555 {
556 /* The partial symbol table associated with this compilation unit,
95554aad 557 or NULL for unread partial units. */
9291a0cd
TT
558 struct partial_symtab *psymtab;
559
560 /* Data needed by the "quick" functions. */
561 struct dwarf2_per_cu_quick_data *quick;
562 } v;
95554aad 563
f4dc4d17
DE
564 union
565 {
566 /* The CUs we import using DW_TAG_imported_unit. This is filled in
567 while reading psymtabs, used to compute the psymtab dependencies,
568 and then cleared. Then it is filled in again while reading full
569 symbols, and only deleted when the objfile is destroyed. */
570 VEC (dwarf2_per_cu_ptr) *imported_symtabs;
571
572 /* Type units are grouped by their DW_AT_stmt_list entry so that they
573 can share them. If this is a TU, this points to the containing
574 symtab. */
575 struct type_unit_group *type_unit_group;
576 } s;
ae038cb0
DJ
577};
578
348e048f
DE
579/* Entry in the signatured_types hash table. */
580
581struct signatured_type
582{
42e7ad6c
DE
583 /* The "per_cu" object of this type.
584 N.B.: This is the first member so that it's easy to convert pointers
585 between them. */
586 struct dwarf2_per_cu_data per_cu;
587
3019eac3 588 /* The type's signature. */
348e048f
DE
589 ULONGEST signature;
590
3019eac3
DE
591 /* Offset in the TU of the type's DIE, as read from the TU header.
592 If the definition lives in a DWO file, this value is unusable. */
593 cu_offset type_offset_in_tu;
594
595 /* Offset in the section of the type's DIE.
596 If the definition lives in a DWO file, this is the offset in the
597 .debug_types.dwo section.
598 The value is zero until the actual value is known.
599 Zero is otherwise not a valid section offset. */
600 sect_offset type_offset_in_section;
348e048f
DE
601};
602
094b34ac
DE
603/* A struct that can be used as a hash key for tables based on DW_AT_stmt_list.
604 This includes type_unit_group and quick_file_names. */
605
606struct stmt_list_hash
607{
608 /* The DWO unit this table is from or NULL if there is none. */
609 struct dwo_unit *dwo_unit;
610
611 /* Offset in .debug_line or .debug_line.dwo. */
612 sect_offset line_offset;
613};
614
f4dc4d17
DE
615/* Each element of dwarf2_per_objfile->type_unit_groups is a pointer to
616 an object of this type. */
617
618struct type_unit_group
619{
620 /* dwarf2read.c's main "handle" on the symtab.
621 To simplify things we create an artificial CU that "includes" all the
622 type units using this stmt_list so that the rest of the code still has
623 a "per_cu" handle on the symtab.
624 This PER_CU is recognized by having no section. */
625#define IS_TYPE_UNIT_GROUP(per_cu) ((per_cu)->info_or_types_section == NULL)
094b34ac
DE
626 struct dwarf2_per_cu_data per_cu;
627
628 union
629 {
630 /* The TUs that share this DW_AT_stmt_list entry.
631 This is added to while parsing type units to build partial symtabs,
632 and is deleted afterwards and not used again. */
633 VEC (dwarf2_per_cu_ptr) *tus;
f4dc4d17 634
094b34ac
DE
635 /* When reading the line table in "quick" functions, we need a real TU.
636 Any will do, we know they all share the same DW_AT_stmt_list entry.
637 For simplicity's sake, we pick the first one. */
638 struct dwarf2_per_cu_data *first_tu;
639 } t;
f4dc4d17
DE
640
641 /* The primary symtab.
094b34ac
DE
642 Type units in a group needn't all be defined in the same source file,
643 so we create an essentially anonymous symtab as the primary symtab. */
f4dc4d17
DE
644 struct symtab *primary_symtab;
645
094b34ac
DE
646 /* The data used to construct the hash key. */
647 struct stmt_list_hash hash;
f4dc4d17
DE
648
649 /* The number of symtabs from the line header.
650 The value here must match line_header.num_file_names. */
651 unsigned int num_symtabs;
652
653 /* The symbol tables for this TU (obtained from the files listed in
654 DW_AT_stmt_list).
655 WARNING: The order of entries here must match the order of entries
656 in the line header. After the first TU using this type_unit_group, the
657 line header for the subsequent TUs is recreated from this. This is done
658 because we need to use the same symtabs for each TU using the same
659 DW_AT_stmt_list value. Also note that symtabs may be repeated here,
660 there's no guarantee the line header doesn't have duplicate entries. */
661 struct symtab **symtabs;
662};
663
80626a55 664/* These sections are what may appear in a DWO file. */
3019eac3
DE
665
666struct dwo_sections
667{
668 struct dwarf2_section_info abbrev;
3019eac3
DE
669 struct dwarf2_section_info line;
670 struct dwarf2_section_info loc;
09262596
DE
671 struct dwarf2_section_info macinfo;
672 struct dwarf2_section_info macro;
3019eac3
DE
673 struct dwarf2_section_info str;
674 struct dwarf2_section_info str_offsets;
80626a55
DE
675 /* In the case of a virtual DWO file, these two are unused. */
676 struct dwarf2_section_info info;
3019eac3
DE
677 VEC (dwarf2_section_info_def) *types;
678};
679
680/* Common bits of DWO CUs/TUs. */
681
682struct dwo_unit
683{
684 /* Backlink to the containing struct dwo_file. */
685 struct dwo_file *dwo_file;
686
687 /* The "id" that distinguishes this CU/TU.
688 .debug_info calls this "dwo_id", .debug_types calls this "signature".
689 Since signatures came first, we stick with it for consistency. */
690 ULONGEST signature;
691
692 /* The section this CU/TU lives in, in the DWO file. */
693 struct dwarf2_section_info *info_or_types_section;
694
695 /* Same as dwarf2_per_cu_data:{offset,length} but for the DWO section. */
696 sect_offset offset;
697 unsigned int length;
698
699 /* For types, offset in the type's DIE of the type defined by this TU. */
700 cu_offset type_offset_in_tu;
701};
702
80626a55
DE
703/* Data for one DWO file.
704 This includes virtual DWO files that have been packaged into a
705 DWP file. */
3019eac3
DE
706
707struct dwo_file
708{
80626a55
DE
709 /* The DW_AT_GNU_dwo_name attribute. This is the hash key.
710 For virtual DWO files the name is constructed from the section offsets
711 of abbrev,line,loc,str_offsets so that we combine virtual DWO files
712 from related CU+TUs. */
713 const char *name;
3019eac3 714
80626a55
DE
715 /* The bfd, when the file is open. Otherwise this is NULL.
716 This is unused(NULL) for virtual DWO files where we use dwp_file.dbfd. */
717 bfd *dbfd;
3019eac3
DE
718
719 /* Section info for this file. */
720 struct dwo_sections sections;
721
722 /* Table of CUs in the file.
723 Each element is a struct dwo_unit. */
724 htab_t cus;
725
726 /* Table of TUs in the file.
727 Each element is a struct dwo_unit. */
728 htab_t tus;
729};
730
80626a55
DE
731/* These sections are what may appear in a DWP file. */
732
733struct dwp_sections
734{
735 struct dwarf2_section_info str;
736 struct dwarf2_section_info cu_index;
737 struct dwarf2_section_info tu_index;
738 /* The .debug_info.dwo, .debug_types.dwo, and other sections are referenced
739 by section number. We don't need to record them here. */
740};
741
742/* These sections are what may appear in a virtual DWO file. */
743
744struct virtual_dwo_sections
745{
746 struct dwarf2_section_info abbrev;
747 struct dwarf2_section_info line;
748 struct dwarf2_section_info loc;
749 struct dwarf2_section_info macinfo;
750 struct dwarf2_section_info macro;
751 struct dwarf2_section_info str_offsets;
752 /* Each DWP hash table entry records one CU or one TU.
753 That is recorded here, and copied to dwo_unit.info_or_types_section. */
754 struct dwarf2_section_info info_or_types;
755};
756
757/* Contents of DWP hash tables. */
758
759struct dwp_hash_table
760{
761 uint32_t nr_units, nr_slots;
762 const gdb_byte *hash_table, *unit_table, *section_pool;
763};
764
765/* Data for one DWP file. */
766
767struct dwp_file
768{
769 /* Name of the file. */
770 const char *name;
771
772 /* The bfd, when the file is open. Otherwise this is NULL. */
773 bfd *dbfd;
774
775 /* Section info for this file. */
776 struct dwp_sections sections;
777
778 /* Table of CUs in the file. */
779 const struct dwp_hash_table *cus;
780
781 /* Table of TUs in the file. */
782 const struct dwp_hash_table *tus;
783
784 /* Table of loaded CUs/TUs. Each entry is a struct dwo_unit *. */
785 htab_t loaded_cutus;
786
787 /* Table to map ELF section numbers to their sections. */
788 unsigned int num_sections;
789 asection **elf_sections;
790};
791
36586728
TT
792/* This represents a '.dwz' file. */
793
794struct dwz_file
795{
796 /* A dwz file can only contain a few sections. */
797 struct dwarf2_section_info abbrev;
798 struct dwarf2_section_info info;
799 struct dwarf2_section_info str;
800 struct dwarf2_section_info line;
801 struct dwarf2_section_info macro;
2ec9a5e0 802 struct dwarf2_section_info gdb_index;
36586728
TT
803
804 /* The dwz's BFD. */
805 bfd *dwz_bfd;
806};
807
0963b4bd
MS
808/* Struct used to pass misc. parameters to read_die_and_children, et
809 al. which are used for both .debug_info and .debug_types dies.
810 All parameters here are unchanging for the life of the call. This
dee91e82 811 struct exists to abstract away the constant parameters of die reading. */
93311388
DE
812
813struct die_reader_specs
814{
dee91e82 815 /* die_section->asection->owner. */
93311388
DE
816 bfd* abfd;
817
818 /* The CU of the DIE we are parsing. */
819 struct dwarf2_cu *cu;
820
80626a55 821 /* Non-NULL if reading a DWO file (including one packaged into a DWP). */
3019eac3
DE
822 struct dwo_file *dwo_file;
823
dee91e82 824 /* The section the die comes from.
3019eac3 825 This is either .debug_info or .debug_types, or the .dwo variants. */
dee91e82
DE
826 struct dwarf2_section_info *die_section;
827
828 /* die_section->buffer. */
829 gdb_byte *buffer;
f664829e
DE
830
831 /* The end of the buffer. */
832 const gdb_byte *buffer_end;
93311388
DE
833};
834
fd820528 835/* Type of function passed to init_cutu_and_read_dies, et.al. */
dee91e82
DE
836typedef void (die_reader_func_ftype) (const struct die_reader_specs *reader,
837 gdb_byte *info_ptr,
838 struct die_info *comp_unit_die,
839 int has_children,
840 void *data);
841
debd256d
JB
842/* The line number information for a compilation unit (found in the
843 .debug_line section) begins with a "statement program header",
844 which contains the following information. */
845struct line_header
846{
847 unsigned int total_length;
848 unsigned short version;
849 unsigned int header_length;
850 unsigned char minimum_instruction_length;
2dc7f7b3 851 unsigned char maximum_ops_per_instruction;
debd256d
JB
852 unsigned char default_is_stmt;
853 int line_base;
854 unsigned char line_range;
855 unsigned char opcode_base;
856
857 /* standard_opcode_lengths[i] is the number of operands for the
858 standard opcode whose value is i. This means that
859 standard_opcode_lengths[0] is unused, and the last meaningful
860 element is standard_opcode_lengths[opcode_base - 1]. */
861 unsigned char *standard_opcode_lengths;
862
863 /* The include_directories table. NOTE! These strings are not
864 allocated with xmalloc; instead, they are pointers into
865 debug_line_buffer. If you try to free them, `free' will get
866 indigestion. */
867 unsigned int num_include_dirs, include_dirs_size;
868 char **include_dirs;
869
870 /* The file_names table. NOTE! These strings are not allocated
871 with xmalloc; instead, they are pointers into debug_line_buffer.
872 Don't try to free them directly. */
873 unsigned int num_file_names, file_names_size;
874 struct file_entry
c906108c 875 {
debd256d
JB
876 char *name;
877 unsigned int dir_index;
878 unsigned int mod_time;
879 unsigned int length;
aaa75496 880 int included_p; /* Non-zero if referenced by the Line Number Program. */
cb1df416 881 struct symtab *symtab; /* The associated symbol table, if any. */
debd256d
JB
882 } *file_names;
883
884 /* The start and end of the statement program following this
6502dd73 885 header. These point into dwarf2_per_objfile->line_buffer. */
fe1b8b76 886 gdb_byte *statement_program_start, *statement_program_end;
debd256d 887};
c906108c
SS
888
889/* When we construct a partial symbol table entry we only
0963b4bd 890 need this much information. */
c906108c
SS
891struct partial_die_info
892 {
72bf9492 893 /* Offset of this DIE. */
b64f50a1 894 sect_offset offset;
72bf9492
DJ
895
896 /* DWARF-2 tag for this DIE. */
897 ENUM_BITFIELD(dwarf_tag) tag : 16;
898
72bf9492
DJ
899 /* Assorted flags describing the data found in this DIE. */
900 unsigned int has_children : 1;
901 unsigned int is_external : 1;
902 unsigned int is_declaration : 1;
903 unsigned int has_type : 1;
904 unsigned int has_specification : 1;
905 unsigned int has_pc_info : 1;
481860b3 906 unsigned int may_be_inlined : 1;
72bf9492
DJ
907
908 /* Flag set if the SCOPE field of this structure has been
909 computed. */
910 unsigned int scope_set : 1;
911
fa4028e9
JB
912 /* Flag set if the DIE has a byte_size attribute. */
913 unsigned int has_byte_size : 1;
914
98bfdba5
PA
915 /* Flag set if any of the DIE's children are template arguments. */
916 unsigned int has_template_arguments : 1;
917
abc72ce4
DE
918 /* Flag set if fixup_partial_die has been called on this die. */
919 unsigned int fixup_called : 1;
920
36586728
TT
921 /* Flag set if DW_TAG_imported_unit uses DW_FORM_GNU_ref_alt. */
922 unsigned int is_dwz : 1;
923
924 /* Flag set if spec_offset uses DW_FORM_GNU_ref_alt. */
925 unsigned int spec_is_dwz : 1;
926
72bf9492 927 /* The name of this DIE. Normally the value of DW_AT_name, but
94af9270 928 sometimes a default name for unnamed DIEs. */
c906108c 929 char *name;
72bf9492 930
abc72ce4
DE
931 /* The linkage name, if present. */
932 const char *linkage_name;
933
72bf9492
DJ
934 /* The scope to prepend to our children. This is generally
935 allocated on the comp_unit_obstack, so will disappear
936 when this compilation unit leaves the cache. */
937 char *scope;
938
95554aad
TT
939 /* Some data associated with the partial DIE. The tag determines
940 which field is live. */
941 union
942 {
943 /* The location description associated with this DIE, if any. */
944 struct dwarf_block *locdesc;
945 /* The offset of an import, for DW_TAG_imported_unit. */
946 sect_offset offset;
947 } d;
72bf9492
DJ
948
949 /* If HAS_PC_INFO, the PC range associated with this DIE. */
c906108c
SS
950 CORE_ADDR lowpc;
951 CORE_ADDR highpc;
72bf9492 952
93311388 953 /* Pointer into the info_buffer (or types_buffer) pointing at the target of
72bf9492 954 DW_AT_sibling, if any. */
abc72ce4
DE
955 /* NOTE: This member isn't strictly necessary, read_partial_die could
956 return DW_AT_sibling values to its caller load_partial_dies. */
fe1b8b76 957 gdb_byte *sibling;
72bf9492
DJ
958
959 /* If HAS_SPECIFICATION, the offset of the DIE referred to by
960 DW_AT_specification (or DW_AT_abstract_origin or
961 DW_AT_extension). */
b64f50a1 962 sect_offset spec_offset;
72bf9492
DJ
963
964 /* Pointers to this DIE's parent, first child, and next sibling,
965 if any. */
966 struct partial_die_info *die_parent, *die_child, *die_sibling;
c906108c
SS
967 };
968
0963b4bd 969/* This data structure holds the information of an abbrev. */
c906108c
SS
970struct abbrev_info
971 {
972 unsigned int number; /* number identifying abbrev */
973 enum dwarf_tag tag; /* dwarf tag */
f3dd6933
DJ
974 unsigned short has_children; /* boolean */
975 unsigned short num_attrs; /* number of attributes */
c906108c
SS
976 struct attr_abbrev *attrs; /* an array of attribute descriptions */
977 struct abbrev_info *next; /* next in chain */
978 };
979
980struct attr_abbrev
981 {
9d25dd43
DE
982 ENUM_BITFIELD(dwarf_attribute) name : 16;
983 ENUM_BITFIELD(dwarf_form) form : 16;
c906108c
SS
984 };
985
433df2d4
DE
986/* Size of abbrev_table.abbrev_hash_table. */
987#define ABBREV_HASH_SIZE 121
988
989/* Top level data structure to contain an abbreviation table. */
990
991struct abbrev_table
992{
f4dc4d17
DE
993 /* Where the abbrev table came from.
994 This is used as a sanity check when the table is used. */
433df2d4
DE
995 sect_offset offset;
996
997 /* Storage for the abbrev table. */
998 struct obstack abbrev_obstack;
999
1000 /* Hash table of abbrevs.
1001 This is an array of size ABBREV_HASH_SIZE allocated in abbrev_obstack.
1002 It could be statically allocated, but the previous code didn't so we
1003 don't either. */
1004 struct abbrev_info **abbrevs;
1005};
1006
0963b4bd 1007/* Attributes have a name and a value. */
b60c80d6
DJ
1008struct attribute
1009 {
9d25dd43 1010 ENUM_BITFIELD(dwarf_attribute) name : 16;
8285870a
JK
1011 ENUM_BITFIELD(dwarf_form) form : 15;
1012
1013 /* Has DW_STRING already been updated by dwarf2_canonicalize_name? This
1014 field should be in u.str (existing only for DW_STRING) but it is kept
1015 here for better struct attribute alignment. */
1016 unsigned int string_is_canonical : 1;
1017
b60c80d6
DJ
1018 union
1019 {
1020 char *str;
1021 struct dwarf_block *blk;
43bbcdc2
PH
1022 ULONGEST unsnd;
1023 LONGEST snd;
b60c80d6 1024 CORE_ADDR addr;
348e048f 1025 struct signatured_type *signatured_type;
b60c80d6
DJ
1026 }
1027 u;
1028 };
1029
0963b4bd 1030/* This data structure holds a complete die structure. */
c906108c
SS
1031struct die_info
1032 {
76815b17
DE
1033 /* DWARF-2 tag for this DIE. */
1034 ENUM_BITFIELD(dwarf_tag) tag : 16;
1035
1036 /* Number of attributes */
98bfdba5
PA
1037 unsigned char num_attrs;
1038
1039 /* True if we're presently building the full type name for the
1040 type derived from this DIE. */
1041 unsigned char building_fullname : 1;
76815b17
DE
1042
1043 /* Abbrev number */
1044 unsigned int abbrev;
1045
93311388 1046 /* Offset in .debug_info or .debug_types section. */
b64f50a1 1047 sect_offset offset;
78ba4af6
JB
1048
1049 /* The dies in a compilation unit form an n-ary tree. PARENT
1050 points to this die's parent; CHILD points to the first child of
1051 this node; and all the children of a given node are chained
4950bc1c 1052 together via their SIBLING fields. */
639d11d3
DC
1053 struct die_info *child; /* Its first child, if any. */
1054 struct die_info *sibling; /* Its next sibling, if any. */
1055 struct die_info *parent; /* Its parent, if any. */
c906108c 1056
b60c80d6
DJ
1057 /* An array of attributes, with NUM_ATTRS elements. There may be
1058 zero, but it's not common and zero-sized arrays are not
1059 sufficiently portable C. */
1060 struct attribute attrs[1];
c906108c
SS
1061 };
1062
0963b4bd 1063/* Get at parts of an attribute structure. */
c906108c
SS
1064
1065#define DW_STRING(attr) ((attr)->u.str)
8285870a 1066#define DW_STRING_IS_CANONICAL(attr) ((attr)->string_is_canonical)
c906108c
SS
1067#define DW_UNSND(attr) ((attr)->u.unsnd)
1068#define DW_BLOCK(attr) ((attr)->u.blk)
1069#define DW_SND(attr) ((attr)->u.snd)
1070#define DW_ADDR(attr) ((attr)->u.addr)
348e048f 1071#define DW_SIGNATURED_TYPE(attr) ((attr)->u.signatured_type)
c906108c 1072
0963b4bd 1073/* Blocks are a bunch of untyped bytes. */
c906108c
SS
1074struct dwarf_block
1075 {
56eb65bd 1076 size_t size;
1d6edc3c
JK
1077
1078 /* Valid only if SIZE is not zero. */
fe1b8b76 1079 gdb_byte *data;
c906108c
SS
1080 };
1081
c906108c
SS
1082#ifndef ATTR_ALLOC_CHUNK
1083#define ATTR_ALLOC_CHUNK 4
1084#endif
1085
c906108c
SS
1086/* Allocate fields for structs, unions and enums in this size. */
1087#ifndef DW_FIELD_ALLOC_CHUNK
1088#define DW_FIELD_ALLOC_CHUNK 4
1089#endif
1090
c906108c
SS
1091/* FIXME: We might want to set this from BFD via bfd_arch_bits_per_byte,
1092 but this would require a corresponding change in unpack_field_as_long
1093 and friends. */
1094static int bits_per_byte = 8;
1095
1096/* The routines that read and process dies for a C struct or C++ class
1097 pass lists of data member fields and lists of member function fields
1098 in an instance of a field_info structure, as defined below. */
1099struct field_info
c5aa993b 1100 {
0963b4bd 1101 /* List of data member and baseclasses fields. */
c5aa993b
JM
1102 struct nextfield
1103 {
1104 struct nextfield *next;
1105 int accessibility;
1106 int virtuality;
1107 struct field field;
1108 }
7d0ccb61 1109 *fields, *baseclasses;
c906108c 1110
7d0ccb61 1111 /* Number of fields (including baseclasses). */
c5aa993b 1112 int nfields;
c906108c 1113
c5aa993b
JM
1114 /* Number of baseclasses. */
1115 int nbaseclasses;
c906108c 1116
c5aa993b
JM
1117 /* Set if the accesibility of one of the fields is not public. */
1118 int non_public_fields;
c906108c 1119
c5aa993b
JM
1120 /* Member function fields array, entries are allocated in the order they
1121 are encountered in the object file. */
1122 struct nextfnfield
1123 {
1124 struct nextfnfield *next;
1125 struct fn_field fnfield;
1126 }
1127 *fnfields;
c906108c 1128
c5aa993b
JM
1129 /* Member function fieldlist array, contains name of possibly overloaded
1130 member function, number of overloaded member functions and a pointer
1131 to the head of the member function field chain. */
1132 struct fnfieldlist
1133 {
1134 char *name;
1135 int length;
1136 struct nextfnfield *head;
1137 }
1138 *fnfieldlists;
c906108c 1139
c5aa993b
JM
1140 /* Number of entries in the fnfieldlists array. */
1141 int nfnfields;
98751a41
JK
1142
1143 /* typedefs defined inside this class. TYPEDEF_FIELD_LIST contains head of
1144 a NULL terminated list of TYPEDEF_FIELD_LIST_COUNT elements. */
1145 struct typedef_field_list
1146 {
1147 struct typedef_field field;
1148 struct typedef_field_list *next;
1149 }
1150 *typedef_field_list;
1151 unsigned typedef_field_list_count;
c5aa993b 1152 };
c906108c 1153
10b3939b
DJ
1154/* One item on the queue of compilation units to read in full symbols
1155 for. */
1156struct dwarf2_queue_item
1157{
1158 struct dwarf2_per_cu_data *per_cu;
95554aad 1159 enum language pretend_language;
10b3939b
DJ
1160 struct dwarf2_queue_item *next;
1161};
1162
1163/* The current queue. */
1164static struct dwarf2_queue_item *dwarf2_queue, *dwarf2_queue_tail;
1165
ae038cb0
DJ
1166/* Loaded secondary compilation units are kept in memory until they
1167 have not been referenced for the processing of this many
1168 compilation units. Set this to zero to disable caching. Cache
1169 sizes of up to at least twenty will improve startup time for
1170 typical inter-CU-reference binaries, at an obvious memory cost. */
1171static int dwarf2_max_cache_age = 5;
920d2a44
AC
1172static void
1173show_dwarf2_max_cache_age (struct ui_file *file, int from_tty,
1174 struct cmd_list_element *c, const char *value)
1175{
3e43a32a
MS
1176 fprintf_filtered (file, _("The upper bound on the age of cached "
1177 "dwarf2 compilation units is %s.\n"),
920d2a44
AC
1178 value);
1179}
1180
ae038cb0 1181
0963b4bd 1182/* Various complaints about symbol reading that don't abort the process. */
c906108c 1183
4d3c2250
KB
1184static void
1185dwarf2_statement_list_fits_in_line_number_section_complaint (void)
2e276125 1186{
4d3c2250 1187 complaint (&symfile_complaints,
e2e0b3e5 1188 _("statement list doesn't fit in .debug_line section"));
4d3c2250
KB
1189}
1190
25e43795
DJ
1191static void
1192dwarf2_debug_line_missing_file_complaint (void)
1193{
1194 complaint (&symfile_complaints,
1195 _(".debug_line section has line data without a file"));
1196}
1197
59205f5a
JB
1198static void
1199dwarf2_debug_line_missing_end_sequence_complaint (void)
1200{
1201 complaint (&symfile_complaints,
3e43a32a
MS
1202 _(".debug_line section has line "
1203 "program sequence without an end"));
59205f5a
JB
1204}
1205
4d3c2250
KB
1206static void
1207dwarf2_complex_location_expr_complaint (void)
2e276125 1208{
e2e0b3e5 1209 complaint (&symfile_complaints, _("location expression too complex"));
4d3c2250
KB
1210}
1211
4d3c2250
KB
1212static void
1213dwarf2_const_value_length_mismatch_complaint (const char *arg1, int arg2,
1214 int arg3)
2e276125 1215{
4d3c2250 1216 complaint (&symfile_complaints,
3e43a32a
MS
1217 _("const value length mismatch for '%s', got %d, expected %d"),
1218 arg1, arg2, arg3);
4d3c2250
KB
1219}
1220
1221static void
f664829e 1222dwarf2_section_buffer_overflow_complaint (struct dwarf2_section_info *section)
2e276125 1223{
4d3c2250 1224 complaint (&symfile_complaints,
f664829e
DE
1225 _("debug info runs off end of %s section"
1226 " [in module %s]"),
1227 section->asection->name,
1228 bfd_get_filename (section->asection->owner));
4d3c2250
KB
1229}
1230
1231static void
1232dwarf2_macro_malformed_definition_complaint (const char *arg1)
8e19ed76 1233{
4d3c2250 1234 complaint (&symfile_complaints,
3e43a32a
MS
1235 _("macro debug info contains a "
1236 "malformed macro definition:\n`%s'"),
4d3c2250
KB
1237 arg1);
1238}
1239
1240static void
1241dwarf2_invalid_attrib_class_complaint (const char *arg1, const char *arg2)
8b2dbe47 1242{
4d3c2250 1243 complaint (&symfile_complaints,
3e43a32a
MS
1244 _("invalid attribute class or form for '%s' in '%s'"),
1245 arg1, arg2);
4d3c2250 1246}
c906108c 1247
c906108c
SS
1248/* local function prototypes */
1249
4efb68b1 1250static void dwarf2_locate_sections (bfd *, asection *, void *);
c906108c 1251
aaa75496
JB
1252static void dwarf2_create_include_psymtab (char *, struct partial_symtab *,
1253 struct objfile *);
1254
918dd910
JK
1255static void dwarf2_find_base_address (struct die_info *die,
1256 struct dwarf2_cu *cu);
1257
c67a9c90 1258static void dwarf2_build_psymtabs_hard (struct objfile *);
c906108c 1259
72bf9492
DJ
1260static void scan_partial_symbols (struct partial_die_info *,
1261 CORE_ADDR *, CORE_ADDR *,
5734ee8b 1262 int, struct dwarf2_cu *);
c906108c 1263
72bf9492
DJ
1264static void add_partial_symbol (struct partial_die_info *,
1265 struct dwarf2_cu *);
63d06c5c 1266
72bf9492
DJ
1267static void add_partial_namespace (struct partial_die_info *pdi,
1268 CORE_ADDR *lowpc, CORE_ADDR *highpc,
5734ee8b 1269 int need_pc, struct dwarf2_cu *cu);
63d06c5c 1270
5d7cb8df
JK
1271static void add_partial_module (struct partial_die_info *pdi, CORE_ADDR *lowpc,
1272 CORE_ADDR *highpc, int need_pc,
1273 struct dwarf2_cu *cu);
1274
72bf9492
DJ
1275static void add_partial_enumeration (struct partial_die_info *enum_pdi,
1276 struct dwarf2_cu *cu);
91c24f0a 1277
bc30ff58
JB
1278static void add_partial_subprogram (struct partial_die_info *pdi,
1279 CORE_ADDR *lowpc, CORE_ADDR *highpc,
5734ee8b 1280 int need_pc, struct dwarf2_cu *cu);
bc30ff58 1281
a14ed312 1282static void dwarf2_psymtab_to_symtab (struct partial_symtab *);
c906108c 1283
a14ed312 1284static void psymtab_to_symtab_1 (struct partial_symtab *);
c906108c 1285
433df2d4
DE
1286static struct abbrev_info *abbrev_table_lookup_abbrev
1287 (const struct abbrev_table *, unsigned int);
1288
1289static struct abbrev_table *abbrev_table_read_table
1290 (struct dwarf2_section_info *, sect_offset);
1291
1292static void abbrev_table_free (struct abbrev_table *);
1293
f4dc4d17
DE
1294static void abbrev_table_free_cleanup (void *);
1295
dee91e82
DE
1296static void dwarf2_read_abbrevs (struct dwarf2_cu *,
1297 struct dwarf2_section_info *);
c906108c 1298
f3dd6933 1299static void dwarf2_free_abbrev_table (void *);
c906108c 1300
6caca83c
CC
1301static unsigned int peek_abbrev_code (bfd *, gdb_byte *);
1302
dee91e82
DE
1303static struct partial_die_info *load_partial_dies
1304 (const struct die_reader_specs *, gdb_byte *, int);
72bf9492 1305
dee91e82
DE
1306static gdb_byte *read_partial_die (const struct die_reader_specs *,
1307 struct partial_die_info *,
1308 struct abbrev_info *,
1309 unsigned int,
1310 gdb_byte *);
c906108c 1311
36586728 1312static struct partial_die_info *find_partial_die (sect_offset, int,
10b3939b 1313 struct dwarf2_cu *);
72bf9492
DJ
1314
1315static void fixup_partial_die (struct partial_die_info *,
1316 struct dwarf2_cu *);
1317
dee91e82
DE
1318static gdb_byte *read_attribute (const struct die_reader_specs *,
1319 struct attribute *, struct attr_abbrev *,
1320 gdb_byte *);
a8329558 1321
a1855c1d 1322static unsigned int read_1_byte (bfd *, const gdb_byte *);
c906108c 1323
a1855c1d 1324static int read_1_signed_byte (bfd *, const gdb_byte *);
c906108c 1325
a1855c1d 1326static unsigned int read_2_bytes (bfd *, const gdb_byte *);
c906108c 1327
a1855c1d 1328static unsigned int read_4_bytes (bfd *, const gdb_byte *);
c906108c 1329
a1855c1d 1330static ULONGEST read_8_bytes (bfd *, const gdb_byte *);
c906108c 1331
fe1b8b76 1332static CORE_ADDR read_address (bfd *, gdb_byte *ptr, struct dwarf2_cu *,
891d2f0b 1333 unsigned int *);
c906108c 1334
c764a876
DE
1335static LONGEST read_initial_length (bfd *, gdb_byte *, unsigned int *);
1336
1337static LONGEST read_checked_initial_length_and_offset
1338 (bfd *, gdb_byte *, const struct comp_unit_head *,
1339 unsigned int *, unsigned int *);
613e1657 1340
fe1b8b76 1341static LONGEST read_offset (bfd *, gdb_byte *, const struct comp_unit_head *,
c764a876
DE
1342 unsigned int *);
1343
1344static LONGEST read_offset_1 (bfd *, gdb_byte *, unsigned int);
613e1657 1345
f4dc4d17
DE
1346static sect_offset read_abbrev_offset (struct dwarf2_section_info *,
1347 sect_offset);
1348
fe1b8b76 1349static gdb_byte *read_n_bytes (bfd *, gdb_byte *, unsigned int);
c906108c 1350
9b1c24c8 1351static char *read_direct_string (bfd *, gdb_byte *, unsigned int *);
c906108c 1352
fe1b8b76
JB
1353static char *read_indirect_string (bfd *, gdb_byte *,
1354 const struct comp_unit_head *,
1355 unsigned int *);
4bdf3d34 1356
36586728
TT
1357static char *read_indirect_string_from_dwz (struct dwz_file *, LONGEST);
1358
12df843f 1359static ULONGEST read_unsigned_leb128 (bfd *, gdb_byte *, unsigned int *);
c906108c 1360
12df843f 1361static LONGEST read_signed_leb128 (bfd *, gdb_byte *, unsigned int *);
c906108c 1362
3019eac3
DE
1363static CORE_ADDR read_addr_index_from_leb128 (struct dwarf2_cu *, gdb_byte *,
1364 unsigned int *);
1365
1366static char *read_str_index (const struct die_reader_specs *reader,
1367 struct dwarf2_cu *cu, ULONGEST str_index);
1368
e142c38c 1369static void set_cu_language (unsigned int, struct dwarf2_cu *);
c906108c 1370
e142c38c
DJ
1371static struct attribute *dwarf2_attr (struct die_info *, unsigned int,
1372 struct dwarf2_cu *);
c906108c 1373
348e048f 1374static struct attribute *dwarf2_attr_no_follow (struct die_info *,
45e58e77 1375 unsigned int);
348e048f 1376
05cf31d1
JB
1377static int dwarf2_flag_true_p (struct die_info *die, unsigned name,
1378 struct dwarf2_cu *cu);
1379
e142c38c 1380static int die_is_declaration (struct die_info *, struct dwarf2_cu *cu);
3ca72b44 1381
e142c38c 1382static struct die_info *die_specification (struct die_info *die,
f2f0e013 1383 struct dwarf2_cu **);
63d06c5c 1384
debd256d
JB
1385static void free_line_header (struct line_header *lh);
1386
aaa75496
JB
1387static void add_file_name (struct line_header *, char *, unsigned int,
1388 unsigned int, unsigned int);
1389
3019eac3
DE
1390static struct line_header *dwarf_decode_line_header (unsigned int offset,
1391 struct dwarf2_cu *cu);
debd256d 1392
f3f5162e
DE
1393static void dwarf_decode_lines (struct line_header *, const char *,
1394 struct dwarf2_cu *, struct partial_symtab *,
1395 int);
c906108c 1396
72b9f47f 1397static void dwarf2_start_subfile (char *, const char *, const char *);
c906108c 1398
f4dc4d17
DE
1399static void dwarf2_start_symtab (struct dwarf2_cu *,
1400 char *, char *, CORE_ADDR);
1401
a14ed312 1402static struct symbol *new_symbol (struct die_info *, struct type *,
e7c27a73 1403 struct dwarf2_cu *);
c906108c 1404
34eaf542
TT
1405static struct symbol *new_symbol_full (struct die_info *, struct type *,
1406 struct dwarf2_cu *, struct symbol *);
1407
a14ed312 1408static void dwarf2_const_value (struct attribute *, struct symbol *,
e7c27a73 1409 struct dwarf2_cu *);
c906108c 1410
98bfdba5
PA
1411static void dwarf2_const_value_attr (struct attribute *attr,
1412 struct type *type,
1413 const char *name,
1414 struct obstack *obstack,
12df843f 1415 struct dwarf2_cu *cu, LONGEST *value,
98bfdba5
PA
1416 gdb_byte **bytes,
1417 struct dwarf2_locexpr_baton **baton);
2df3850c 1418
e7c27a73 1419static struct type *die_type (struct die_info *, struct dwarf2_cu *);
c906108c 1420
b4ba55a1
JB
1421static int need_gnat_info (struct dwarf2_cu *);
1422
3e43a32a
MS
1423static struct type *die_descriptive_type (struct die_info *,
1424 struct dwarf2_cu *);
b4ba55a1
JB
1425
1426static void set_descriptive_type (struct type *, struct die_info *,
1427 struct dwarf2_cu *);
1428
e7c27a73
DJ
1429static struct type *die_containing_type (struct die_info *,
1430 struct dwarf2_cu *);
c906108c 1431
673bfd45
DE
1432static struct type *lookup_die_type (struct die_info *, struct attribute *,
1433 struct dwarf2_cu *);
c906108c 1434
f792889a 1435static struct type *read_type_die (struct die_info *, struct dwarf2_cu *);
c906108c 1436
673bfd45
DE
1437static struct type *read_type_die_1 (struct die_info *, struct dwarf2_cu *);
1438
0d5cff50 1439static const char *determine_prefix (struct die_info *die, struct dwarf2_cu *);
63d06c5c 1440
6e70227d 1441static char *typename_concat (struct obstack *obs, const char *prefix,
f55ee35c
JK
1442 const char *suffix, int physname,
1443 struct dwarf2_cu *cu);
63d06c5c 1444
e7c27a73 1445static void read_file_scope (struct die_info *, struct dwarf2_cu *);
c906108c 1446
348e048f
DE
1447static void read_type_unit_scope (struct die_info *, struct dwarf2_cu *);
1448
e7c27a73 1449static void read_func_scope (struct die_info *, struct dwarf2_cu *);
c906108c 1450
e7c27a73 1451static void read_lexical_block_scope (struct die_info *, struct dwarf2_cu *);
c906108c 1452
96408a79
SA
1453static void read_call_site_scope (struct die_info *die, struct dwarf2_cu *cu);
1454
ff013f42
JK
1455static int dwarf2_ranges_read (unsigned, CORE_ADDR *, CORE_ADDR *,
1456 struct dwarf2_cu *, struct partial_symtab *);
1457
a14ed312 1458static int dwarf2_get_pc_bounds (struct die_info *,
d85a05f0
DJ
1459 CORE_ADDR *, CORE_ADDR *, struct dwarf2_cu *,
1460 struct partial_symtab *);
c906108c 1461
fae299cd
DC
1462static void get_scope_pc_bounds (struct die_info *,
1463 CORE_ADDR *, CORE_ADDR *,
1464 struct dwarf2_cu *);
1465
801e3a5b
JB
1466static void dwarf2_record_block_ranges (struct die_info *, struct block *,
1467 CORE_ADDR, struct dwarf2_cu *);
1468
a14ed312 1469static void dwarf2_add_field (struct field_info *, struct die_info *,
e7c27a73 1470 struct dwarf2_cu *);
c906108c 1471
a14ed312 1472static void dwarf2_attach_fields_to_type (struct field_info *,
e7c27a73 1473 struct type *, struct dwarf2_cu *);
c906108c 1474
a14ed312 1475static void dwarf2_add_member_fn (struct field_info *,
e26fb1d7 1476 struct die_info *, struct type *,
e7c27a73 1477 struct dwarf2_cu *);
c906108c 1478
a14ed312 1479static void dwarf2_attach_fn_fields_to_type (struct field_info *,
3e43a32a
MS
1480 struct type *,
1481 struct dwarf2_cu *);
c906108c 1482
134d01f1 1483static void process_structure_scope (struct die_info *, struct dwarf2_cu *);
c906108c 1484
e7c27a73 1485static void read_common_block (struct die_info *, struct dwarf2_cu *);
c906108c 1486
e7c27a73 1487static void read_namespace (struct die_info *die, struct dwarf2_cu *);
d9fa45fe 1488
5d7cb8df
JK
1489static void read_module (struct die_info *die, struct dwarf2_cu *cu);
1490
27aa8d6a
SW
1491static void read_import_statement (struct die_info *die, struct dwarf2_cu *);
1492
f55ee35c
JK
1493static struct type *read_module_type (struct die_info *die,
1494 struct dwarf2_cu *cu);
1495
38d518c9 1496static const char *namespace_name (struct die_info *die,
e142c38c 1497 int *is_anonymous, struct dwarf2_cu *);
38d518c9 1498
134d01f1 1499static void process_enumeration_scope (struct die_info *, struct dwarf2_cu *);
c906108c 1500
e7c27a73 1501static CORE_ADDR decode_locdesc (struct dwarf_block *, struct dwarf2_cu *);
c906108c 1502
6e70227d 1503static enum dwarf_array_dim_ordering read_array_order (struct die_info *,
7ca2d3a3
DL
1504 struct dwarf2_cu *);
1505
dee91e82 1506static struct die_info *read_die_and_children (const struct die_reader_specs *,
93311388 1507 gdb_byte *info_ptr,
fe1b8b76 1508 gdb_byte **new_info_ptr,
639d11d3
DC
1509 struct die_info *parent);
1510
dee91e82 1511static struct die_info *read_die_and_siblings (const struct die_reader_specs *,
93311388 1512 gdb_byte *info_ptr,
fe1b8b76 1513 gdb_byte **new_info_ptr,
639d11d3
DC
1514 struct die_info *parent);
1515
3019eac3
DE
1516static gdb_byte *read_full_die_1 (const struct die_reader_specs *,
1517 struct die_info **, gdb_byte *, int *, int);
1518
dee91e82
DE
1519static gdb_byte *read_full_die (const struct die_reader_specs *,
1520 struct die_info **, gdb_byte *, int *);
93311388 1521
e7c27a73 1522static void process_die (struct die_info *, struct dwarf2_cu *);
c906108c 1523
71c25dea
TT
1524static char *dwarf2_canonicalize_name (char *, struct dwarf2_cu *,
1525 struct obstack *);
1526
e142c38c 1527static char *dwarf2_name (struct die_info *die, struct dwarf2_cu *);
9219021c 1528
98bfdba5
PA
1529static const char *dwarf2_full_name (char *name,
1530 struct die_info *die,
1531 struct dwarf2_cu *cu);
1532
e142c38c 1533static struct die_info *dwarf2_extension (struct die_info *die,
f2f0e013 1534 struct dwarf2_cu **);
9219021c 1535
f39c6ffd 1536static const char *dwarf_tag_name (unsigned int);
c906108c 1537
f39c6ffd 1538static const char *dwarf_attr_name (unsigned int);
c906108c 1539
f39c6ffd 1540static const char *dwarf_form_name (unsigned int);
c906108c 1541
a14ed312 1542static char *dwarf_bool_name (unsigned int);
c906108c 1543
f39c6ffd 1544static const char *dwarf_type_encoding_name (unsigned int);
c906108c 1545
f9aca02d 1546static struct die_info *sibling_die (struct die_info *);
c906108c 1547
d97bc12b
DE
1548static void dump_die_shallow (struct ui_file *, int indent, struct die_info *);
1549
1550static void dump_die_for_error (struct die_info *);
1551
1552static void dump_die_1 (struct ui_file *, int level, int max_level,
1553 struct die_info *);
c906108c 1554
d97bc12b 1555/*static*/ void dump_die (struct die_info *, int max_level);
c906108c 1556
51545339 1557static void store_in_ref_table (struct die_info *,
10b3939b 1558 struct dwarf2_cu *);
c906108c 1559
93311388
DE
1560static int is_ref_attr (struct attribute *);
1561
b64f50a1 1562static sect_offset dwarf2_get_ref_die_offset (struct attribute *);
c906108c 1563
43bbcdc2 1564static LONGEST dwarf2_get_attr_constant_value (struct attribute *, int);
a02abb62 1565
348e048f
DE
1566static struct die_info *follow_die_ref_or_sig (struct die_info *,
1567 struct attribute *,
1568 struct dwarf2_cu **);
1569
10b3939b
DJ
1570static struct die_info *follow_die_ref (struct die_info *,
1571 struct attribute *,
f2f0e013 1572 struct dwarf2_cu **);
c906108c 1573
348e048f
DE
1574static struct die_info *follow_die_sig (struct die_info *,
1575 struct attribute *,
1576 struct dwarf2_cu **);
1577
6c83ed52
TT
1578static struct signatured_type *lookup_signatured_type_at_offset
1579 (struct objfile *objfile,
b64f50a1 1580 struct dwarf2_section_info *section, sect_offset offset);
6c83ed52 1581
e5fe5e75 1582static void load_full_type_unit (struct dwarf2_per_cu_data *per_cu);
348e048f 1583
52dc124a 1584static void read_signatured_type (struct signatured_type *);
348e048f 1585
f4dc4d17 1586static struct type_unit_group *get_type_unit_group
094b34ac 1587 (struct dwarf2_cu *, struct attribute *);
f4dc4d17
DE
1588
1589static void build_type_unit_groups (die_reader_func_ftype *, void *);
1590
c906108c
SS
1591/* memory allocation interface */
1592
7b5a2f43 1593static struct dwarf_block *dwarf_alloc_block (struct dwarf2_cu *);
c906108c 1594
b60c80d6 1595static struct die_info *dwarf_alloc_die (struct dwarf2_cu *, int);
c906108c 1596
09262596
DE
1597static void dwarf_decode_macros (struct dwarf2_cu *, unsigned int,
1598 char *, int);
2e276125 1599
8e19ed76
PS
1600static int attr_form_is_block (struct attribute *);
1601
3690dd37
JB
1602static int attr_form_is_section_offset (struct attribute *);
1603
1604static int attr_form_is_constant (struct attribute *);
1605
8cf6f0b1
TT
1606static void fill_in_loclist_baton (struct dwarf2_cu *cu,
1607 struct dwarf2_loclist_baton *baton,
1608 struct attribute *attr);
1609
93e7bd98
DJ
1610static void dwarf2_symbol_mark_computed (struct attribute *attr,
1611 struct symbol *sym,
1612 struct dwarf2_cu *cu);
4c2df51b 1613
dee91e82
DE
1614static gdb_byte *skip_one_die (const struct die_reader_specs *reader,
1615 gdb_byte *info_ptr,
1616 struct abbrev_info *abbrev);
4bb7a0a7 1617
72bf9492
DJ
1618static void free_stack_comp_unit (void *);
1619
72bf9492
DJ
1620static hashval_t partial_die_hash (const void *item);
1621
1622static int partial_die_eq (const void *item_lhs, const void *item_rhs);
1623
ae038cb0 1624static struct dwarf2_per_cu_data *dwarf2_find_containing_comp_unit
36586728 1625 (sect_offset offset, unsigned int offset_in_dwz, struct objfile *objfile);
ae038cb0 1626
9816fde3 1627static void init_one_comp_unit (struct dwarf2_cu *cu,
23745b47 1628 struct dwarf2_per_cu_data *per_cu);
9816fde3
JK
1629
1630static void prepare_one_comp_unit (struct dwarf2_cu *cu,
95554aad
TT
1631 struct die_info *comp_unit_die,
1632 enum language pretend_language);
93311388 1633
68dc6402 1634static void free_heap_comp_unit (void *);
ae038cb0
DJ
1635
1636static void free_cached_comp_units (void *);
1637
1638static void age_cached_comp_units (void);
1639
dee91e82 1640static void free_one_cached_comp_unit (struct dwarf2_per_cu_data *);
ae038cb0 1641
f792889a
DJ
1642static struct type *set_die_type (struct die_info *, struct type *,
1643 struct dwarf2_cu *);
1c379e20 1644
ae038cb0
DJ
1645static void create_all_comp_units (struct objfile *);
1646
0e50663e 1647static int create_all_type_units (struct objfile *);
1fd400ff 1648
95554aad
TT
1649static void load_full_comp_unit (struct dwarf2_per_cu_data *,
1650 enum language);
10b3939b 1651
95554aad
TT
1652static void process_full_comp_unit (struct dwarf2_per_cu_data *,
1653 enum language);
10b3939b 1654
f4dc4d17
DE
1655static void process_full_type_unit (struct dwarf2_per_cu_data *,
1656 enum language);
1657
10b3939b
DJ
1658static void dwarf2_add_dependence (struct dwarf2_cu *,
1659 struct dwarf2_per_cu_data *);
1660
ae038cb0
DJ
1661static void dwarf2_mark (struct dwarf2_cu *);
1662
1663static void dwarf2_clear_marks (struct dwarf2_per_cu_data *);
1664
b64f50a1 1665static struct type *get_die_type_at_offset (sect_offset,
673bfd45
DE
1666 struct dwarf2_per_cu_data *per_cu);
1667
f792889a 1668static struct type *get_die_type (struct die_info *die, struct dwarf2_cu *cu);
72019c9c 1669
9291a0cd
TT
1670static void dwarf2_release_queue (void *dummy);
1671
95554aad
TT
1672static void queue_comp_unit (struct dwarf2_per_cu_data *per_cu,
1673 enum language pretend_language);
1674
1675static int maybe_queue_comp_unit (struct dwarf2_cu *this_cu,
1676 struct dwarf2_per_cu_data *per_cu,
1677 enum language pretend_language);
9291a0cd 1678
a0f42c21 1679static void process_queue (void);
9291a0cd
TT
1680
1681static void find_file_and_directory (struct die_info *die,
1682 struct dwarf2_cu *cu,
1683 char **name, char **comp_dir);
1684
1685static char *file_full_name (int file, struct line_header *lh,
1686 const char *comp_dir);
1687
36586728
TT
1688static gdb_byte *read_and_check_comp_unit_head
1689 (struct comp_unit_head *header,
1690 struct dwarf2_section_info *section,
1691 struct dwarf2_section_info *abbrev_section, gdb_byte *info_ptr,
1692 int is_debug_types_section);
1693
fd820528 1694static void init_cutu_and_read_dies
f4dc4d17
DE
1695 (struct dwarf2_per_cu_data *this_cu, struct abbrev_table *abbrev_table,
1696 int use_existing_cu, int keep,
3019eac3
DE
1697 die_reader_func_ftype *die_reader_func, void *data);
1698
dee91e82
DE
1699static void init_cutu_and_read_dies_simple
1700 (struct dwarf2_per_cu_data *this_cu,
1701 die_reader_func_ftype *die_reader_func, void *data);
9291a0cd 1702
673bfd45 1703static htab_t allocate_signatured_type_table (struct objfile *objfile);
1fd400ff 1704
3019eac3
DE
1705static htab_t allocate_dwo_unit_table (struct objfile *objfile);
1706
1707static struct dwo_unit *lookup_dwo_comp_unit
a1855c1d 1708 (struct dwarf2_per_cu_data *, const char *, const char *, ULONGEST);
3019eac3
DE
1709
1710static struct dwo_unit *lookup_dwo_type_unit
a1855c1d 1711 (struct signatured_type *, const char *, const char *);
3019eac3
DE
1712
1713static void free_dwo_file_cleanup (void *);
1714
95554aad
TT
1715static void process_cu_includes (void);
1716
1b80a9fa
JK
1717static void check_producer (struct dwarf2_cu *cu);
1718
9291a0cd
TT
1719#if WORDS_BIGENDIAN
1720
1721/* Convert VALUE between big- and little-endian. */
1722static offset_type
1723byte_swap (offset_type value)
1724{
1725 offset_type result;
1726
1727 result = (value & 0xff) << 24;
1728 result |= (value & 0xff00) << 8;
1729 result |= (value & 0xff0000) >> 8;
1730 result |= (value & 0xff000000) >> 24;
1731 return result;
1732}
1733
1734#define MAYBE_SWAP(V) byte_swap (V)
1735
1736#else
1737#define MAYBE_SWAP(V) (V)
1738#endif /* WORDS_BIGENDIAN */
1739
1740/* The suffix for an index file. */
1741#define INDEX_SUFFIX ".gdb-index"
1742
3da10d80
KS
1743static const char *dwarf2_physname (char *name, struct die_info *die,
1744 struct dwarf2_cu *cu);
1745
c906108c 1746/* Try to locate the sections we need for DWARF 2 debugging
251d32d9
TG
1747 information and return true if we have enough to do something.
1748 NAMES points to the dwarf2 section names, or is NULL if the standard
1749 ELF names are used. */
c906108c
SS
1750
1751int
251d32d9
TG
1752dwarf2_has_info (struct objfile *objfile,
1753 const struct dwarf2_debug_sections *names)
c906108c 1754{
be391dca
TT
1755 dwarf2_per_objfile = objfile_data (objfile, dwarf2_objfile_data_key);
1756 if (!dwarf2_per_objfile)
1757 {
1758 /* Initialize per-objfile state. */
1759 struct dwarf2_per_objfile *data
1760 = obstack_alloc (&objfile->objfile_obstack, sizeof (*data));
9a619af0 1761
be391dca
TT
1762 memset (data, 0, sizeof (*data));
1763 set_objfile_data (objfile, dwarf2_objfile_data_key, data);
1764 dwarf2_per_objfile = data;
6502dd73 1765
251d32d9
TG
1766 bfd_map_over_sections (objfile->obfd, dwarf2_locate_sections,
1767 (void *) names);
be391dca
TT
1768 dwarf2_per_objfile->objfile = objfile;
1769 }
1770 return (dwarf2_per_objfile->info.asection != NULL
1771 && dwarf2_per_objfile->abbrev.asection != NULL);
c906108c
SS
1772}
1773
251d32d9
TG
1774/* When loading sections, we look either for uncompressed section or for
1775 compressed section names. */
233a11ab
CS
1776
1777static int
251d32d9
TG
1778section_is_p (const char *section_name,
1779 const struct dwarf2_section_names *names)
233a11ab 1780{
251d32d9
TG
1781 if (names->normal != NULL
1782 && strcmp (section_name, names->normal) == 0)
1783 return 1;
1784 if (names->compressed != NULL
1785 && strcmp (section_name, names->compressed) == 0)
1786 return 1;
1787 return 0;
233a11ab
CS
1788}
1789
c906108c
SS
1790/* This function is mapped across the sections and remembers the
1791 offset and size of each of the debugging sections we are interested
1792 in. */
1793
1794static void
251d32d9 1795dwarf2_locate_sections (bfd *abfd, asection *sectp, void *vnames)
c906108c 1796{
251d32d9 1797 const struct dwarf2_debug_sections *names;
dc7650b8 1798 flagword aflag = bfd_get_section_flags (abfd, sectp);
251d32d9
TG
1799
1800 if (vnames == NULL)
1801 names = &dwarf2_elf_names;
1802 else
1803 names = (const struct dwarf2_debug_sections *) vnames;
1804
dc7650b8
JK
1805 if ((aflag & SEC_HAS_CONTENTS) == 0)
1806 {
1807 }
1808 else if (section_is_p (sectp->name, &names->info))
c906108c 1809 {
dce234bc
PP
1810 dwarf2_per_objfile->info.asection = sectp;
1811 dwarf2_per_objfile->info.size = bfd_get_section_size (sectp);
c906108c 1812 }
251d32d9 1813 else if (section_is_p (sectp->name, &names->abbrev))
c906108c 1814 {
dce234bc
PP
1815 dwarf2_per_objfile->abbrev.asection = sectp;
1816 dwarf2_per_objfile->abbrev.size = bfd_get_section_size (sectp);
c906108c 1817 }
251d32d9 1818 else if (section_is_p (sectp->name, &names->line))
c906108c 1819 {
dce234bc
PP
1820 dwarf2_per_objfile->line.asection = sectp;
1821 dwarf2_per_objfile->line.size = bfd_get_section_size (sectp);
c906108c 1822 }
251d32d9 1823 else if (section_is_p (sectp->name, &names->loc))
c906108c 1824 {
dce234bc
PP
1825 dwarf2_per_objfile->loc.asection = sectp;
1826 dwarf2_per_objfile->loc.size = bfd_get_section_size (sectp);
c906108c 1827 }
251d32d9 1828 else if (section_is_p (sectp->name, &names->macinfo))
c906108c 1829 {
dce234bc
PP
1830 dwarf2_per_objfile->macinfo.asection = sectp;
1831 dwarf2_per_objfile->macinfo.size = bfd_get_section_size (sectp);
c906108c 1832 }
cf2c3c16
TT
1833 else if (section_is_p (sectp->name, &names->macro))
1834 {
1835 dwarf2_per_objfile->macro.asection = sectp;
1836 dwarf2_per_objfile->macro.size = bfd_get_section_size (sectp);
1837 }
251d32d9 1838 else if (section_is_p (sectp->name, &names->str))
c906108c 1839 {
dce234bc
PP
1840 dwarf2_per_objfile->str.asection = sectp;
1841 dwarf2_per_objfile->str.size = bfd_get_section_size (sectp);
c906108c 1842 }
3019eac3
DE
1843 else if (section_is_p (sectp->name, &names->addr))
1844 {
1845 dwarf2_per_objfile->addr.asection = sectp;
1846 dwarf2_per_objfile->addr.size = bfd_get_section_size (sectp);
1847 }
251d32d9 1848 else if (section_is_p (sectp->name, &names->frame))
b6af0555 1849 {
dce234bc
PP
1850 dwarf2_per_objfile->frame.asection = sectp;
1851 dwarf2_per_objfile->frame.size = bfd_get_section_size (sectp);
b6af0555 1852 }
251d32d9 1853 else if (section_is_p (sectp->name, &names->eh_frame))
b6af0555 1854 {
dc7650b8
JK
1855 dwarf2_per_objfile->eh_frame.asection = sectp;
1856 dwarf2_per_objfile->eh_frame.size = bfd_get_section_size (sectp);
b6af0555 1857 }
251d32d9 1858 else if (section_is_p (sectp->name, &names->ranges))
af34e669 1859 {
dce234bc
PP
1860 dwarf2_per_objfile->ranges.asection = sectp;
1861 dwarf2_per_objfile->ranges.size = bfd_get_section_size (sectp);
af34e669 1862 }
251d32d9 1863 else if (section_is_p (sectp->name, &names->types))
348e048f 1864 {
8b70b953
TT
1865 struct dwarf2_section_info type_section;
1866
1867 memset (&type_section, 0, sizeof (type_section));
1868 type_section.asection = sectp;
1869 type_section.size = bfd_get_section_size (sectp);
1870
1871 VEC_safe_push (dwarf2_section_info_def, dwarf2_per_objfile->types,
1872 &type_section);
348e048f 1873 }
251d32d9 1874 else if (section_is_p (sectp->name, &names->gdb_index))
9291a0cd
TT
1875 {
1876 dwarf2_per_objfile->gdb_index.asection = sectp;
1877 dwarf2_per_objfile->gdb_index.size = bfd_get_section_size (sectp);
1878 }
dce234bc 1879
72dca2f5
FR
1880 if ((bfd_get_section_flags (abfd, sectp) & SEC_LOAD)
1881 && bfd_section_vma (abfd, sectp) == 0)
1882 dwarf2_per_objfile->has_section_at_zero = 1;
c906108c
SS
1883}
1884
fceca515
DE
1885/* A helper function that decides whether a section is empty,
1886 or not present. */
9e0ac564
TT
1887
1888static int
1889dwarf2_section_empty_p (struct dwarf2_section_info *info)
1890{
1891 return info->asection == NULL || info->size == 0;
1892}
1893
3019eac3
DE
1894/* Read the contents of the section INFO.
1895 OBJFILE is the main object file, but not necessarily the file where
1896 the section comes from. E.g., for DWO files INFO->asection->owner
1897 is the bfd of the DWO file.
dce234bc 1898 If the section is compressed, uncompress it before returning. */
c906108c 1899
dce234bc
PP
1900static void
1901dwarf2_read_section (struct objfile *objfile, struct dwarf2_section_info *info)
c906108c 1902{
dce234bc 1903 asection *sectp = info->asection;
3019eac3 1904 bfd *abfd;
dce234bc
PP
1905 gdb_byte *buf, *retbuf;
1906 unsigned char header[4];
c906108c 1907
be391dca
TT
1908 if (info->readin)
1909 return;
dce234bc 1910 info->buffer = NULL;
be391dca 1911 info->readin = 1;
188dd5d6 1912
9e0ac564 1913 if (dwarf2_section_empty_p (info))
dce234bc 1914 return;
c906108c 1915
3019eac3
DE
1916 abfd = sectp->owner;
1917
4bf44c1c
TT
1918 /* If the section has relocations, we must read it ourselves.
1919 Otherwise we attach it to the BFD. */
1920 if ((sectp->flags & SEC_RELOC) == 0)
dce234bc 1921 {
4bf44c1c 1922 const gdb_byte *bytes = gdb_bfd_map_section (sectp, &info->size);
dce234bc 1923
4bf44c1c
TT
1924 /* We have to cast away const here for historical reasons.
1925 Fixing dwarf2read to be const-correct would be quite nice. */
1926 info->buffer = (gdb_byte *) bytes;
1927 return;
dce234bc 1928 }
dce234bc 1929
4bf44c1c
TT
1930 buf = obstack_alloc (&objfile->objfile_obstack, info->size);
1931 info->buffer = buf;
dce234bc
PP
1932
1933 /* When debugging .o files, we may need to apply relocations; see
1934 http://sourceware.org/ml/gdb-patches/2002-04/msg00136.html .
1935 We never compress sections in .o files, so we only need to
1936 try this when the section is not compressed. */
ac8035ab 1937 retbuf = symfile_relocate_debug_section (objfile, sectp, buf);
dce234bc
PP
1938 if (retbuf != NULL)
1939 {
1940 info->buffer = retbuf;
1941 return;
1942 }
1943
1944 if (bfd_seek (abfd, sectp->filepos, SEEK_SET) != 0
1945 || bfd_bread (buf, info->size, abfd) != info->size)
1946 error (_("Dwarf Error: Can't read DWARF data from '%s'"),
1947 bfd_get_filename (abfd));
1948}
1949
9e0ac564
TT
1950/* A helper function that returns the size of a section in a safe way.
1951 If you are positive that the section has been read before using the
1952 size, then it is safe to refer to the dwarf2_section_info object's
1953 "size" field directly. In other cases, you must call this
1954 function, because for compressed sections the size field is not set
1955 correctly until the section has been read. */
1956
1957static bfd_size_type
1958dwarf2_section_size (struct objfile *objfile,
1959 struct dwarf2_section_info *info)
1960{
1961 if (!info->readin)
1962 dwarf2_read_section (objfile, info);
1963 return info->size;
1964}
1965
dce234bc 1966/* Fill in SECTP, BUFP and SIZEP with section info, given OBJFILE and
0963b4bd 1967 SECTION_NAME. */
af34e669 1968
dce234bc 1969void
3017a003
TG
1970dwarf2_get_section_info (struct objfile *objfile,
1971 enum dwarf2_section_enum sect,
dce234bc
PP
1972 asection **sectp, gdb_byte **bufp,
1973 bfd_size_type *sizep)
1974{
1975 struct dwarf2_per_objfile *data
1976 = objfile_data (objfile, dwarf2_objfile_data_key);
1977 struct dwarf2_section_info *info;
a3b2a86b
TT
1978
1979 /* We may see an objfile without any DWARF, in which case we just
1980 return nothing. */
1981 if (data == NULL)
1982 {
1983 *sectp = NULL;
1984 *bufp = NULL;
1985 *sizep = 0;
1986 return;
1987 }
3017a003
TG
1988 switch (sect)
1989 {
1990 case DWARF2_DEBUG_FRAME:
1991 info = &data->frame;
1992 break;
1993 case DWARF2_EH_FRAME:
1994 info = &data->eh_frame;
1995 break;
1996 default:
1997 gdb_assert_not_reached ("unexpected section");
1998 }
dce234bc 1999
9e0ac564 2000 dwarf2_read_section (objfile, info);
dce234bc
PP
2001
2002 *sectp = info->asection;
2003 *bufp = info->buffer;
2004 *sizep = info->size;
2005}
2006
36586728
TT
2007/* A helper function to find the sections for a .dwz file. */
2008
2009static void
2010locate_dwz_sections (bfd *abfd, asection *sectp, void *arg)
2011{
2012 struct dwz_file *dwz_file = arg;
2013
2014 /* Note that we only support the standard ELF names, because .dwz
2015 is ELF-only (at the time of writing). */
2016 if (section_is_p (sectp->name, &dwarf2_elf_names.abbrev))
2017 {
2018 dwz_file->abbrev.asection = sectp;
2019 dwz_file->abbrev.size = bfd_get_section_size (sectp);
2020 }
2021 else if (section_is_p (sectp->name, &dwarf2_elf_names.info))
2022 {
2023 dwz_file->info.asection = sectp;
2024 dwz_file->info.size = bfd_get_section_size (sectp);
2025 }
2026 else if (section_is_p (sectp->name, &dwarf2_elf_names.str))
2027 {
2028 dwz_file->str.asection = sectp;
2029 dwz_file->str.size = bfd_get_section_size (sectp);
2030 }
2031 else if (section_is_p (sectp->name, &dwarf2_elf_names.line))
2032 {
2033 dwz_file->line.asection = sectp;
2034 dwz_file->line.size = bfd_get_section_size (sectp);
2035 }
2036 else if (section_is_p (sectp->name, &dwarf2_elf_names.macro))
2037 {
2038 dwz_file->macro.asection = sectp;
2039 dwz_file->macro.size = bfd_get_section_size (sectp);
2040 }
2ec9a5e0
TT
2041 else if (section_is_p (sectp->name, &dwarf2_elf_names.gdb_index))
2042 {
2043 dwz_file->gdb_index.asection = sectp;
2044 dwz_file->gdb_index.size = bfd_get_section_size (sectp);
2045 }
36586728
TT
2046}
2047
2048/* Open the separate '.dwz' debug file, if needed. Error if the file
2049 cannot be found. */
2050
2051static struct dwz_file *
2052dwarf2_get_dwz_file (void)
2053{
2054 bfd *abfd, *dwz_bfd;
2055 asection *section;
2056 gdb_byte *data;
2057 struct cleanup *cleanup;
2058 const char *filename;
2059 struct dwz_file *result;
2060
2061 if (dwarf2_per_objfile->dwz_file != NULL)
2062 return dwarf2_per_objfile->dwz_file;
2063
2064 abfd = dwarf2_per_objfile->objfile->obfd;
2065 section = bfd_get_section_by_name (abfd, ".gnu_debugaltlink");
2066 if (section == NULL)
2067 error (_("could not find '.gnu_debugaltlink' section"));
2068 if (!bfd_malloc_and_get_section (abfd, section, &data))
2069 error (_("could not read '.gnu_debugaltlink' section: %s"),
2070 bfd_errmsg (bfd_get_error ()));
2071 cleanup = make_cleanup (xfree, data);
2072
2073 filename = data;
2074 if (!IS_ABSOLUTE_PATH (filename))
2075 {
2076 char *abs = gdb_realpath (dwarf2_per_objfile->objfile->name);
2077 char *rel;
2078
2079 make_cleanup (xfree, abs);
2080 abs = ldirname (abs);
2081 make_cleanup (xfree, abs);
2082
2083 rel = concat (abs, SLASH_STRING, filename, (char *) NULL);
2084 make_cleanup (xfree, rel);
2085 filename = rel;
2086 }
2087
2088 /* The format is just a NUL-terminated file name, followed by the
2089 build-id. For now, though, we ignore the build-id. */
2090 dwz_bfd = gdb_bfd_open (filename, gnutarget, -1);
2091 if (dwz_bfd == NULL)
2092 error (_("could not read '%s': %s"), filename,
2093 bfd_errmsg (bfd_get_error ()));
2094
2095 if (!bfd_check_format (dwz_bfd, bfd_object))
2096 {
2097 gdb_bfd_unref (dwz_bfd);
2098 error (_("file '%s' was not usable: %s"), filename,
2099 bfd_errmsg (bfd_get_error ()));
2100 }
2101
2102 result = OBSTACK_ZALLOC (&dwarf2_per_objfile->objfile->objfile_obstack,
2103 struct dwz_file);
2104 result->dwz_bfd = dwz_bfd;
2105
2106 bfd_map_over_sections (dwz_bfd, locate_dwz_sections, result);
2107
2108 do_cleanups (cleanup);
2109
2110 return result;
2111}
9291a0cd 2112\f
7b9f3c50
DE
2113/* DWARF quick_symbols_functions support. */
2114
2115/* TUs can share .debug_line entries, and there can be a lot more TUs than
2116 unique line tables, so we maintain a separate table of all .debug_line
2117 derived entries to support the sharing.
2118 All the quick functions need is the list of file names. We discard the
2119 line_header when we're done and don't need to record it here. */
2120struct quick_file_names
2121{
094b34ac
DE
2122 /* The data used to construct the hash key. */
2123 struct stmt_list_hash hash;
7b9f3c50
DE
2124
2125 /* The number of entries in file_names, real_names. */
2126 unsigned int num_file_names;
2127
2128 /* The file names from the line table, after being run through
2129 file_full_name. */
2130 const char **file_names;
2131
2132 /* The file names from the line table after being run through
2133 gdb_realpath. These are computed lazily. */
2134 const char **real_names;
2135};
2136
2137/* When using the index (and thus not using psymtabs), each CU has an
2138 object of this type. This is used to hold information needed by
2139 the various "quick" methods. */
2140struct dwarf2_per_cu_quick_data
2141{
2142 /* The file table. This can be NULL if there was no file table
2143 or it's currently not read in.
2144 NOTE: This points into dwarf2_per_objfile->quick_file_names_table. */
2145 struct quick_file_names *file_names;
2146
2147 /* The corresponding symbol table. This is NULL if symbols for this
2148 CU have not yet been read. */
2149 struct symtab *symtab;
2150
2151 /* A temporary mark bit used when iterating over all CUs in
2152 expand_symtabs_matching. */
2153 unsigned int mark : 1;
2154
2155 /* True if we've tried to read the file table and found there isn't one.
2156 There will be no point in trying to read it again next time. */
2157 unsigned int no_file_data : 1;
2158};
2159
094b34ac
DE
2160/* Utility hash function for a stmt_list_hash. */
2161
2162static hashval_t
2163hash_stmt_list_entry (const struct stmt_list_hash *stmt_list_hash)
2164{
2165 hashval_t v = 0;
2166
2167 if (stmt_list_hash->dwo_unit != NULL)
2168 v += (uintptr_t) stmt_list_hash->dwo_unit->dwo_file;
2169 v += stmt_list_hash->line_offset.sect_off;
2170 return v;
2171}
2172
2173/* Utility equality function for a stmt_list_hash. */
2174
2175static int
2176eq_stmt_list_entry (const struct stmt_list_hash *lhs,
2177 const struct stmt_list_hash *rhs)
2178{
2179 if ((lhs->dwo_unit != NULL) != (rhs->dwo_unit != NULL))
2180 return 0;
2181 if (lhs->dwo_unit != NULL
2182 && lhs->dwo_unit->dwo_file != rhs->dwo_unit->dwo_file)
2183 return 0;
2184
2185 return lhs->line_offset.sect_off == rhs->line_offset.sect_off;
2186}
2187
7b9f3c50
DE
2188/* Hash function for a quick_file_names. */
2189
2190static hashval_t
2191hash_file_name_entry (const void *e)
2192{
2193 const struct quick_file_names *file_data = e;
2194
094b34ac 2195 return hash_stmt_list_entry (&file_data->hash);
7b9f3c50
DE
2196}
2197
2198/* Equality function for a quick_file_names. */
2199
2200static int
2201eq_file_name_entry (const void *a, const void *b)
2202{
2203 const struct quick_file_names *ea = a;
2204 const struct quick_file_names *eb = b;
2205
094b34ac 2206 return eq_stmt_list_entry (&ea->hash, &eb->hash);
7b9f3c50
DE
2207}
2208
2209/* Delete function for a quick_file_names. */
2210
2211static void
2212delete_file_name_entry (void *e)
2213{
2214 struct quick_file_names *file_data = e;
2215 int i;
2216
2217 for (i = 0; i < file_data->num_file_names; ++i)
2218 {
2219 xfree ((void*) file_data->file_names[i]);
2220 if (file_data->real_names)
2221 xfree ((void*) file_data->real_names[i]);
2222 }
2223
2224 /* The space for the struct itself lives on objfile_obstack,
2225 so we don't free it here. */
2226}
2227
2228/* Create a quick_file_names hash table. */
2229
2230static htab_t
2231create_quick_file_names_table (unsigned int nr_initial_entries)
2232{
2233 return htab_create_alloc (nr_initial_entries,
2234 hash_file_name_entry, eq_file_name_entry,
2235 delete_file_name_entry, xcalloc, xfree);
2236}
9291a0cd 2237
918dd910
JK
2238/* Read in PER_CU->CU. This function is unrelated to symtabs, symtab would
2239 have to be created afterwards. You should call age_cached_comp_units after
2240 processing PER_CU->CU. dw2_setup must have been already called. */
2241
2242static void
2243load_cu (struct dwarf2_per_cu_data *per_cu)
2244{
3019eac3 2245 if (per_cu->is_debug_types)
e5fe5e75 2246 load_full_type_unit (per_cu);
918dd910 2247 else
95554aad 2248 load_full_comp_unit (per_cu, language_minimal);
918dd910 2249
918dd910 2250 gdb_assert (per_cu->cu != NULL);
2dc860c0
DE
2251
2252 dwarf2_find_base_address (per_cu->cu->dies, per_cu->cu);
918dd910
JK
2253}
2254
a0f42c21 2255/* Read in the symbols for PER_CU. */
2fdf6df6 2256
9291a0cd 2257static void
a0f42c21 2258dw2_do_instantiate_symtab (struct dwarf2_per_cu_data *per_cu)
9291a0cd
TT
2259{
2260 struct cleanup *back_to;
2261
f4dc4d17
DE
2262 /* Skip type_unit_groups, reading the type units they contain
2263 is handled elsewhere. */
2264 if (IS_TYPE_UNIT_GROUP (per_cu))
2265 return;
2266
9291a0cd
TT
2267 back_to = make_cleanup (dwarf2_release_queue, NULL);
2268
95554aad
TT
2269 if (dwarf2_per_objfile->using_index
2270 ? per_cu->v.quick->symtab == NULL
2271 : (per_cu->v.psymtab == NULL || !per_cu->v.psymtab->readin))
2272 {
2273 queue_comp_unit (per_cu, language_minimal);
2274 load_cu (per_cu);
2275 }
9291a0cd 2276
a0f42c21 2277 process_queue ();
9291a0cd
TT
2278
2279 /* Age the cache, releasing compilation units that have not
2280 been used recently. */
2281 age_cached_comp_units ();
2282
2283 do_cleanups (back_to);
2284}
2285
2286/* Ensure that the symbols for PER_CU have been read in. OBJFILE is
2287 the objfile from which this CU came. Returns the resulting symbol
2288 table. */
2fdf6df6 2289
9291a0cd 2290static struct symtab *
a0f42c21 2291dw2_instantiate_symtab (struct dwarf2_per_cu_data *per_cu)
9291a0cd 2292{
95554aad 2293 gdb_assert (dwarf2_per_objfile->using_index);
9291a0cd
TT
2294 if (!per_cu->v.quick->symtab)
2295 {
2296 struct cleanup *back_to = make_cleanup (free_cached_comp_units, NULL);
2297 increment_reading_symtab ();
a0f42c21 2298 dw2_do_instantiate_symtab (per_cu);
95554aad 2299 process_cu_includes ();
9291a0cd
TT
2300 do_cleanups (back_to);
2301 }
2302 return per_cu->v.quick->symtab;
2303}
2304
f4dc4d17
DE
2305/* Return the CU given its index.
2306
2307 This is intended for loops like:
2308
2309 for (i = 0; i < (dwarf2_per_objfile->n_comp_units
2310 + dwarf2_per_objfile->n_type_units); ++i)
2311 {
2312 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
2313
2314 ...;
2315 }
2316*/
2fdf6df6 2317
1fd400ff
TT
2318static struct dwarf2_per_cu_data *
2319dw2_get_cu (int index)
2320{
2321 if (index >= dwarf2_per_objfile->n_comp_units)
2322 {
f4dc4d17 2323 index -= dwarf2_per_objfile->n_comp_units;
094b34ac
DE
2324 gdb_assert (index < dwarf2_per_objfile->n_type_units);
2325 return &dwarf2_per_objfile->all_type_units[index]->per_cu;
f4dc4d17
DE
2326 }
2327
2328 return dwarf2_per_objfile->all_comp_units[index];
2329}
2330
2331/* Return the primary CU given its index.
2332 The difference between this function and dw2_get_cu is in the handling
2333 of type units (TUs). Here we return the type_unit_group object.
2334
2335 This is intended for loops like:
2336
2337 for (i = 0; i < (dwarf2_per_objfile->n_comp_units
2338 + dwarf2_per_objfile->n_type_unit_groups); ++i)
2339 {
2340 struct dwarf2_per_cu_data *per_cu = dw2_get_primary_cu (i);
2341
2342 ...;
2343 }
2344*/
2345
2346static struct dwarf2_per_cu_data *
2347dw2_get_primary_cu (int index)
2348{
2349 if (index >= dwarf2_per_objfile->n_comp_units)
2350 {
1fd400ff 2351 index -= dwarf2_per_objfile->n_comp_units;
094b34ac
DE
2352 gdb_assert (index < dwarf2_per_objfile->n_type_unit_groups);
2353 return &dwarf2_per_objfile->all_type_unit_groups[index]->per_cu;
1fd400ff 2354 }
f4dc4d17 2355
1fd400ff
TT
2356 return dwarf2_per_objfile->all_comp_units[index];
2357}
2358
9291a0cd
TT
2359/* A helper function that knows how to read a 64-bit value in a way
2360 that doesn't make gdb die. Returns 1 if the conversion went ok, 0
2361 otherwise. */
2fdf6df6 2362
9291a0cd
TT
2363static int
2364extract_cu_value (const char *bytes, ULONGEST *result)
2365{
2366 if (sizeof (ULONGEST) < 8)
2367 {
2368 int i;
2369
2370 /* Ignore the upper 4 bytes if they are all zero. */
2371 for (i = 0; i < 4; ++i)
2372 if (bytes[i + 4] != 0)
2373 return 0;
2374
2375 *result = extract_unsigned_integer (bytes, 4, BFD_ENDIAN_LITTLE);
2376 }
2377 else
2378 *result = extract_unsigned_integer (bytes, 8, BFD_ENDIAN_LITTLE);
2379 return 1;
2380}
2381
2ec9a5e0
TT
2382/* A helper for create_cus_from_index that handles a given list of
2383 CUs. */
2fdf6df6 2384
9291a0cd 2385static int
2ec9a5e0
TT
2386create_cus_from_index_list (struct objfile *objfile,
2387 const gdb_byte *cu_list, offset_type n_elements,
2388 struct dwarf2_section_info *section,
2389 int is_dwz,
2390 int base_offset)
9291a0cd
TT
2391{
2392 offset_type i;
9291a0cd 2393
2ec9a5e0 2394 for (i = 0; i < n_elements; i += 2)
9291a0cd
TT
2395 {
2396 struct dwarf2_per_cu_data *the_cu;
2397 ULONGEST offset, length;
2398
2399 if (!extract_cu_value (cu_list, &offset)
2400 || !extract_cu_value (cu_list + 8, &length))
2401 return 0;
2402 cu_list += 2 * 8;
2403
2404 the_cu = OBSTACK_ZALLOC (&objfile->objfile_obstack,
2405 struct dwarf2_per_cu_data);
b64f50a1 2406 the_cu->offset.sect_off = offset;
9291a0cd
TT
2407 the_cu->length = length;
2408 the_cu->objfile = objfile;
2ec9a5e0 2409 the_cu->info_or_types_section = section;
9291a0cd
TT
2410 the_cu->v.quick = OBSTACK_ZALLOC (&objfile->objfile_obstack,
2411 struct dwarf2_per_cu_quick_data);
2ec9a5e0
TT
2412 the_cu->is_dwz = is_dwz;
2413 dwarf2_per_objfile->all_comp_units[base_offset + i / 2] = the_cu;
9291a0cd
TT
2414 }
2415
2416 return 1;
2417}
2418
2ec9a5e0
TT
2419/* Read the CU list from the mapped index, and use it to create all
2420 the CU objects for this objfile. Return 0 if something went wrong,
2421 1 if everything went ok. */
2422
2423static int
2424create_cus_from_index (struct objfile *objfile,
2425 const gdb_byte *cu_list, offset_type cu_list_elements,
2426 const gdb_byte *dwz_list, offset_type dwz_elements)
2427{
2428 struct dwz_file *dwz;
2429
2430 dwarf2_per_objfile->n_comp_units = (cu_list_elements + dwz_elements) / 2;
2431 dwarf2_per_objfile->all_comp_units
2432 = obstack_alloc (&objfile->objfile_obstack,
2433 dwarf2_per_objfile->n_comp_units
2434 * sizeof (struct dwarf2_per_cu_data *));
2435
2436 if (!create_cus_from_index_list (objfile, cu_list, cu_list_elements,
2437 &dwarf2_per_objfile->info, 0, 0))
2438 return 0;
2439
2440 if (dwz_elements == 0)
2441 return 1;
2442
2443 dwz = dwarf2_get_dwz_file ();
2444 return create_cus_from_index_list (objfile, dwz_list, dwz_elements,
2445 &dwz->info, 1, cu_list_elements / 2);
2446}
2447
1fd400ff 2448/* Create the signatured type hash table from the index. */
673bfd45 2449
1fd400ff 2450static int
673bfd45 2451create_signatured_type_table_from_index (struct objfile *objfile,
8b70b953 2452 struct dwarf2_section_info *section,
673bfd45
DE
2453 const gdb_byte *bytes,
2454 offset_type elements)
1fd400ff
TT
2455{
2456 offset_type i;
673bfd45 2457 htab_t sig_types_hash;
1fd400ff 2458
d467dd73
DE
2459 dwarf2_per_objfile->n_type_units = elements / 3;
2460 dwarf2_per_objfile->all_type_units
1fd400ff 2461 = obstack_alloc (&objfile->objfile_obstack,
d467dd73 2462 dwarf2_per_objfile->n_type_units
b4dd5633 2463 * sizeof (struct signatured_type *));
1fd400ff 2464
673bfd45 2465 sig_types_hash = allocate_signatured_type_table (objfile);
1fd400ff
TT
2466
2467 for (i = 0; i < elements; i += 3)
2468 {
52dc124a
DE
2469 struct signatured_type *sig_type;
2470 ULONGEST offset, type_offset_in_tu, signature;
1fd400ff
TT
2471 void **slot;
2472
2473 if (!extract_cu_value (bytes, &offset)
52dc124a 2474 || !extract_cu_value (bytes + 8, &type_offset_in_tu))
1fd400ff
TT
2475 return 0;
2476 signature = extract_unsigned_integer (bytes + 16, 8, BFD_ENDIAN_LITTLE);
2477 bytes += 3 * 8;
2478
52dc124a 2479 sig_type = OBSTACK_ZALLOC (&objfile->objfile_obstack,
1fd400ff 2480 struct signatured_type);
52dc124a 2481 sig_type->signature = signature;
3019eac3
DE
2482 sig_type->type_offset_in_tu.cu_off = type_offset_in_tu;
2483 sig_type->per_cu.is_debug_types = 1;
2484 sig_type->per_cu.info_or_types_section = section;
52dc124a
DE
2485 sig_type->per_cu.offset.sect_off = offset;
2486 sig_type->per_cu.objfile = objfile;
2487 sig_type->per_cu.v.quick
1fd400ff
TT
2488 = OBSTACK_ZALLOC (&objfile->objfile_obstack,
2489 struct dwarf2_per_cu_quick_data);
2490
52dc124a
DE
2491 slot = htab_find_slot (sig_types_hash, sig_type, INSERT);
2492 *slot = sig_type;
1fd400ff 2493
b4dd5633 2494 dwarf2_per_objfile->all_type_units[i / 3] = sig_type;
1fd400ff
TT
2495 }
2496
673bfd45 2497 dwarf2_per_objfile->signatured_types = sig_types_hash;
1fd400ff
TT
2498
2499 return 1;
2500}
2501
9291a0cd
TT
2502/* Read the address map data from the mapped index, and use it to
2503 populate the objfile's psymtabs_addrmap. */
2fdf6df6 2504
9291a0cd
TT
2505static void
2506create_addrmap_from_index (struct objfile *objfile, struct mapped_index *index)
2507{
2508 const gdb_byte *iter, *end;
2509 struct obstack temp_obstack;
2510 struct addrmap *mutable_map;
2511 struct cleanup *cleanup;
2512 CORE_ADDR baseaddr;
2513
2514 obstack_init (&temp_obstack);
2515 cleanup = make_cleanup_obstack_free (&temp_obstack);
2516 mutable_map = addrmap_create_mutable (&temp_obstack);
2517
2518 iter = index->address_table;
2519 end = iter + index->address_table_size;
2520
2521 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
2522
2523 while (iter < end)
2524 {
2525 ULONGEST hi, lo, cu_index;
2526 lo = extract_unsigned_integer (iter, 8, BFD_ENDIAN_LITTLE);
2527 iter += 8;
2528 hi = extract_unsigned_integer (iter, 8, BFD_ENDIAN_LITTLE);
2529 iter += 8;
2530 cu_index = extract_unsigned_integer (iter, 4, BFD_ENDIAN_LITTLE);
2531 iter += 4;
2532
2533 addrmap_set_empty (mutable_map, lo + baseaddr, hi + baseaddr - 1,
1fd400ff 2534 dw2_get_cu (cu_index));
9291a0cd
TT
2535 }
2536
2537 objfile->psymtabs_addrmap = addrmap_create_fixed (mutable_map,
2538 &objfile->objfile_obstack);
2539 do_cleanups (cleanup);
2540}
2541
59d7bcaf
JK
2542/* The hash function for strings in the mapped index. This is the same as
2543 SYMBOL_HASH_NEXT, but we keep a separate copy to maintain control over the
2544 implementation. This is necessary because the hash function is tied to the
2545 format of the mapped index file. The hash values do not have to match with
559a7a62
JK
2546 SYMBOL_HASH_NEXT.
2547
2548 Use INT_MAX for INDEX_VERSION if you generate the current index format. */
2fdf6df6 2549
9291a0cd 2550static hashval_t
559a7a62 2551mapped_index_string_hash (int index_version, const void *p)
9291a0cd
TT
2552{
2553 const unsigned char *str = (const unsigned char *) p;
2554 hashval_t r = 0;
2555 unsigned char c;
2556
2557 while ((c = *str++) != 0)
559a7a62
JK
2558 {
2559 if (index_version >= 5)
2560 c = tolower (c);
2561 r = r * 67 + c - 113;
2562 }
9291a0cd
TT
2563
2564 return r;
2565}
2566
2567/* Find a slot in the mapped index INDEX for the object named NAME.
2568 If NAME is found, set *VEC_OUT to point to the CU vector in the
2569 constant pool and return 1. If NAME cannot be found, return 0. */
2fdf6df6 2570
9291a0cd
TT
2571static int
2572find_slot_in_mapped_hash (struct mapped_index *index, const char *name,
2573 offset_type **vec_out)
2574{
0cf03b49
JK
2575 struct cleanup *back_to = make_cleanup (null_cleanup, 0);
2576 offset_type hash;
9291a0cd 2577 offset_type slot, step;
559a7a62 2578 int (*cmp) (const char *, const char *);
9291a0cd 2579
0cf03b49
JK
2580 if (current_language->la_language == language_cplus
2581 || current_language->la_language == language_java
2582 || current_language->la_language == language_fortran)
2583 {
2584 /* NAME is already canonical. Drop any qualifiers as .gdb_index does
2585 not contain any. */
2586 const char *paren = strchr (name, '(');
2587
2588 if (paren)
2589 {
2590 char *dup;
2591
2592 dup = xmalloc (paren - name + 1);
2593 memcpy (dup, name, paren - name);
2594 dup[paren - name] = 0;
2595
2596 make_cleanup (xfree, dup);
2597 name = dup;
2598 }
2599 }
2600
559a7a62 2601 /* Index version 4 did not support case insensitive searches. But the
feea76c2 2602 indices for case insensitive languages are built in lowercase, therefore
559a7a62
JK
2603 simulate our NAME being searched is also lowercased. */
2604 hash = mapped_index_string_hash ((index->version == 4
2605 && case_sensitivity == case_sensitive_off
2606 ? 5 : index->version),
2607 name);
2608
3876f04e
DE
2609 slot = hash & (index->symbol_table_slots - 1);
2610 step = ((hash * 17) & (index->symbol_table_slots - 1)) | 1;
559a7a62 2611 cmp = (case_sensitivity == case_sensitive_on ? strcmp : strcasecmp);
9291a0cd
TT
2612
2613 for (;;)
2614 {
2615 /* Convert a slot number to an offset into the table. */
2616 offset_type i = 2 * slot;
2617 const char *str;
3876f04e 2618 if (index->symbol_table[i] == 0 && index->symbol_table[i + 1] == 0)
0cf03b49
JK
2619 {
2620 do_cleanups (back_to);
2621 return 0;
2622 }
9291a0cd 2623
3876f04e 2624 str = index->constant_pool + MAYBE_SWAP (index->symbol_table[i]);
559a7a62 2625 if (!cmp (name, str))
9291a0cd
TT
2626 {
2627 *vec_out = (offset_type *) (index->constant_pool
3876f04e 2628 + MAYBE_SWAP (index->symbol_table[i + 1]));
0cf03b49 2629 do_cleanups (back_to);
9291a0cd
TT
2630 return 1;
2631 }
2632
3876f04e 2633 slot = (slot + step) & (index->symbol_table_slots - 1);
9291a0cd
TT
2634 }
2635}
2636
2ec9a5e0
TT
2637/* A helper function that reads the .gdb_index from SECTION and fills
2638 in MAP. FILENAME is the name of the file containing the section;
2639 it is used for error reporting. DEPRECATED_OK is nonzero if it is
2640 ok to use deprecated sections.
2641
2642 CU_LIST, CU_LIST_ELEMENTS, TYPES_LIST, and TYPES_LIST_ELEMENTS are
2643 out parameters that are filled in with information about the CU and
2644 TU lists in the section.
2645
2646 Returns 1 if all went well, 0 otherwise. */
2fdf6df6 2647
9291a0cd 2648static int
2ec9a5e0
TT
2649read_index_from_section (struct objfile *objfile,
2650 const char *filename,
2651 int deprecated_ok,
2652 struct dwarf2_section_info *section,
2653 struct mapped_index *map,
2654 const gdb_byte **cu_list,
2655 offset_type *cu_list_elements,
2656 const gdb_byte **types_list,
2657 offset_type *types_list_elements)
9291a0cd 2658{
9291a0cd 2659 char *addr;
2ec9a5e0 2660 offset_type version;
b3b272e1 2661 offset_type *metadata;
1fd400ff 2662 int i;
9291a0cd 2663
2ec9a5e0 2664 if (dwarf2_section_empty_p (section))
9291a0cd 2665 return 0;
82430852
JK
2666
2667 /* Older elfutils strip versions could keep the section in the main
2668 executable while splitting it for the separate debug info file. */
2ec9a5e0 2669 if ((bfd_get_file_flags (section->asection) & SEC_HAS_CONTENTS) == 0)
82430852
JK
2670 return 0;
2671
2ec9a5e0 2672 dwarf2_read_section (objfile, section);
9291a0cd 2673
2ec9a5e0 2674 addr = section->buffer;
9291a0cd 2675 /* Version check. */
1fd400ff 2676 version = MAYBE_SWAP (*(offset_type *) addr);
987d643c 2677 /* Versions earlier than 3 emitted every copy of a psymbol. This
a6e293d1 2678 causes the index to behave very poorly for certain requests. Version 3
831adc1f 2679 contained incomplete addrmap. So, it seems better to just ignore such
481860b3 2680 indices. */
831adc1f 2681 if (version < 4)
481860b3
GB
2682 {
2683 static int warning_printed = 0;
2684 if (!warning_printed)
2685 {
2686 warning (_("Skipping obsolete .gdb_index section in %s."),
2ec9a5e0 2687 filename);
481860b3
GB
2688 warning_printed = 1;
2689 }
2690 return 0;
2691 }
2692 /* Index version 4 uses a different hash function than index version
2693 5 and later.
2694
2695 Versions earlier than 6 did not emit psymbols for inlined
2696 functions. Using these files will cause GDB not to be able to
2697 set breakpoints on inlined functions by name, so we ignore these
e615022a
DE
2698 indices unless the user has done
2699 "set use-deprecated-index-sections on". */
2ec9a5e0 2700 if (version < 6 && !deprecated_ok)
481860b3
GB
2701 {
2702 static int warning_printed = 0;
2703 if (!warning_printed)
2704 {
e615022a
DE
2705 warning (_("\
2706Skipping deprecated .gdb_index section in %s.\n\
2707Do \"set use-deprecated-index-sections on\" before the file is read\n\
2708to use the section anyway."),
2ec9a5e0 2709 filename);
481860b3
GB
2710 warning_printed = 1;
2711 }
2712 return 0;
2713 }
2714 /* Indexes with higher version than the one supported by GDB may be no
594e8718 2715 longer backward compatible. */
156942c7 2716 if (version > 7)
594e8718 2717 return 0;
9291a0cd 2718
559a7a62 2719 map->version = version;
2ec9a5e0 2720 map->total_size = section->size;
9291a0cd
TT
2721
2722 metadata = (offset_type *) (addr + sizeof (offset_type));
1fd400ff
TT
2723
2724 i = 0;
2ec9a5e0
TT
2725 *cu_list = addr + MAYBE_SWAP (metadata[i]);
2726 *cu_list_elements = ((MAYBE_SWAP (metadata[i + 1]) - MAYBE_SWAP (metadata[i]))
2727 / 8);
1fd400ff
TT
2728 ++i;
2729
2ec9a5e0
TT
2730 *types_list = addr + MAYBE_SWAP (metadata[i]);
2731 *types_list_elements = ((MAYBE_SWAP (metadata[i + 1])
2732 - MAYBE_SWAP (metadata[i]))
2733 / 8);
987d643c 2734 ++i;
1fd400ff
TT
2735
2736 map->address_table = addr + MAYBE_SWAP (metadata[i]);
2737 map->address_table_size = (MAYBE_SWAP (metadata[i + 1])
2738 - MAYBE_SWAP (metadata[i]));
2739 ++i;
2740
3876f04e
DE
2741 map->symbol_table = (offset_type *) (addr + MAYBE_SWAP (metadata[i]));
2742 map->symbol_table_slots = ((MAYBE_SWAP (metadata[i + 1])
2743 - MAYBE_SWAP (metadata[i]))
2744 / (2 * sizeof (offset_type)));
1fd400ff 2745 ++i;
9291a0cd 2746
1fd400ff
TT
2747 map->constant_pool = addr + MAYBE_SWAP (metadata[i]);
2748
2ec9a5e0
TT
2749 return 1;
2750}
2751
2752
2753/* Read the index file. If everything went ok, initialize the "quick"
2754 elements of all the CUs and return 1. Otherwise, return 0. */
2755
2756static int
2757dwarf2_read_index (struct objfile *objfile)
2758{
2759 struct mapped_index local_map, *map;
2760 const gdb_byte *cu_list, *types_list, *dwz_list = NULL;
2761 offset_type cu_list_elements, types_list_elements, dwz_list_elements = 0;
2762
2763 if (!read_index_from_section (objfile, objfile->name,
2764 use_deprecated_index_sections,
2765 &dwarf2_per_objfile->gdb_index, &local_map,
2766 &cu_list, &cu_list_elements,
2767 &types_list, &types_list_elements))
2768 return 0;
2769
0fefef59 2770 /* Don't use the index if it's empty. */
2ec9a5e0 2771 if (local_map.symbol_table_slots == 0)
0fefef59
DE
2772 return 0;
2773
2ec9a5e0
TT
2774 /* If there is a .dwz file, read it so we can get its CU list as
2775 well. */
2776 if (bfd_get_section_by_name (objfile->obfd, ".gnu_debugaltlink") != NULL)
2777 {
2778 struct dwz_file *dwz = dwarf2_get_dwz_file ();
2779 struct mapped_index dwz_map;
2780 const gdb_byte *dwz_types_ignore;
2781 offset_type dwz_types_elements_ignore;
2782
2783 if (!read_index_from_section (objfile, bfd_get_filename (dwz->dwz_bfd),
2784 1,
2785 &dwz->gdb_index, &dwz_map,
2786 &dwz_list, &dwz_list_elements,
2787 &dwz_types_ignore,
2788 &dwz_types_elements_ignore))
2789 {
2790 warning (_("could not read '.gdb_index' section from %s; skipping"),
2791 bfd_get_filename (dwz->dwz_bfd));
2792 return 0;
2793 }
2794 }
2795
2796 if (!create_cus_from_index (objfile, cu_list, cu_list_elements,
2797 dwz_list, dwz_list_elements))
1fd400ff
TT
2798 return 0;
2799
8b70b953
TT
2800 if (types_list_elements)
2801 {
2802 struct dwarf2_section_info *section;
2803
2804 /* We can only handle a single .debug_types when we have an
2805 index. */
2806 if (VEC_length (dwarf2_section_info_def, dwarf2_per_objfile->types) != 1)
2807 return 0;
2808
2809 section = VEC_index (dwarf2_section_info_def,
2810 dwarf2_per_objfile->types, 0);
2811
2812 if (!create_signatured_type_table_from_index (objfile, section,
2813 types_list,
2814 types_list_elements))
2815 return 0;
2816 }
9291a0cd 2817
2ec9a5e0
TT
2818 create_addrmap_from_index (objfile, &local_map);
2819
2820 map = obstack_alloc (&objfile->objfile_obstack, sizeof (struct mapped_index));
2821 *map = local_map;
9291a0cd
TT
2822
2823 dwarf2_per_objfile->index_table = map;
2824 dwarf2_per_objfile->using_index = 1;
7b9f3c50
DE
2825 dwarf2_per_objfile->quick_file_names_table =
2826 create_quick_file_names_table (dwarf2_per_objfile->n_comp_units);
9291a0cd
TT
2827
2828 return 1;
2829}
2830
2831/* A helper for the "quick" functions which sets the global
2832 dwarf2_per_objfile according to OBJFILE. */
2fdf6df6 2833
9291a0cd
TT
2834static void
2835dw2_setup (struct objfile *objfile)
2836{
2837 dwarf2_per_objfile = objfile_data (objfile, dwarf2_objfile_data_key);
2838 gdb_assert (dwarf2_per_objfile);
2839}
2840
f4dc4d17
DE
2841/* Reader function for dw2_build_type_unit_groups. */
2842
2843static void
2844dw2_build_type_unit_groups_reader (const struct die_reader_specs *reader,
2845 gdb_byte *info_ptr,
2846 struct die_info *type_unit_die,
2847 int has_children,
2848 void *data)
2849{
2850 struct dwarf2_cu *cu = reader->cu;
f4dc4d17
DE
2851 struct attribute *attr;
2852 struct type_unit_group *tu_group;
2853
2854 gdb_assert (data == NULL);
2855
2856 if (! has_children)
2857 return;
2858
2859 attr = dwarf2_attr_no_follow (type_unit_die, DW_AT_stmt_list);
2860 /* Call this for its side-effect of creating the associated
2861 struct type_unit_group if it doesn't already exist. */
094b34ac 2862 tu_group = get_type_unit_group (cu, attr);
f4dc4d17
DE
2863}
2864
2865/* Build dwarf2_per_objfile->type_unit_groups.
2866 This function may be called multiple times. */
2867
2868static void
2869dw2_build_type_unit_groups (void)
2870{
2871 if (dwarf2_per_objfile->type_unit_groups == NULL)
2872 build_type_unit_groups (dw2_build_type_unit_groups_reader, NULL);
2873}
2874
dee91e82 2875/* die_reader_func for dw2_get_file_names. */
2fdf6df6 2876
dee91e82
DE
2877static void
2878dw2_get_file_names_reader (const struct die_reader_specs *reader,
2879 gdb_byte *info_ptr,
2880 struct die_info *comp_unit_die,
2881 int has_children,
2882 void *data)
9291a0cd 2883{
dee91e82
DE
2884 struct dwarf2_cu *cu = reader->cu;
2885 struct dwarf2_per_cu_data *this_cu = cu->per_cu;
2886 struct objfile *objfile = dwarf2_per_objfile->objfile;
094b34ac 2887 struct dwarf2_per_cu_data *lh_cu;
7b9f3c50 2888 struct line_header *lh;
9291a0cd 2889 struct attribute *attr;
dee91e82 2890 int i;
9291a0cd 2891 char *name, *comp_dir;
7b9f3c50
DE
2892 void **slot;
2893 struct quick_file_names *qfn;
2894 unsigned int line_offset;
9291a0cd 2895
07261596
TT
2896 /* Our callers never want to match partial units -- instead they
2897 will match the enclosing full CU. */
2898 if (comp_unit_die->tag == DW_TAG_partial_unit)
2899 {
2900 this_cu->v.quick->no_file_data = 1;
2901 return;
2902 }
2903
094b34ac
DE
2904 /* If we're reading the line header for TUs, store it in the "per_cu"
2905 for tu_group. */
2906 if (this_cu->is_debug_types)
2907 {
2908 struct type_unit_group *tu_group = data;
2909
2910 gdb_assert (tu_group != NULL);
2911 lh_cu = &tu_group->per_cu;
2912 }
2913 else
2914 lh_cu = this_cu;
2915
7b9f3c50
DE
2916 lh = NULL;
2917 slot = NULL;
2918 line_offset = 0;
dee91e82
DE
2919
2920 attr = dwarf2_attr (comp_unit_die, DW_AT_stmt_list, cu);
9291a0cd
TT
2921 if (attr)
2922 {
7b9f3c50
DE
2923 struct quick_file_names find_entry;
2924
2925 line_offset = DW_UNSND (attr);
2926
2927 /* We may have already read in this line header (TU line header sharing).
2928 If we have we're done. */
094b34ac
DE
2929 find_entry.hash.dwo_unit = cu->dwo_unit;
2930 find_entry.hash.line_offset.sect_off = line_offset;
7b9f3c50
DE
2931 slot = htab_find_slot (dwarf2_per_objfile->quick_file_names_table,
2932 &find_entry, INSERT);
2933 if (*slot != NULL)
2934 {
094b34ac 2935 lh_cu->v.quick->file_names = *slot;
dee91e82 2936 return;
7b9f3c50
DE
2937 }
2938
3019eac3 2939 lh = dwarf_decode_line_header (line_offset, cu);
9291a0cd
TT
2940 }
2941 if (lh == NULL)
2942 {
094b34ac 2943 lh_cu->v.quick->no_file_data = 1;
dee91e82 2944 return;
9291a0cd
TT
2945 }
2946
7b9f3c50 2947 qfn = obstack_alloc (&objfile->objfile_obstack, sizeof (*qfn));
094b34ac
DE
2948 qfn->hash.dwo_unit = cu->dwo_unit;
2949 qfn->hash.line_offset.sect_off = line_offset;
7b9f3c50
DE
2950 gdb_assert (slot != NULL);
2951 *slot = qfn;
9291a0cd 2952
dee91e82 2953 find_file_and_directory (comp_unit_die, cu, &name, &comp_dir);
9291a0cd 2954
7b9f3c50
DE
2955 qfn->num_file_names = lh->num_file_names;
2956 qfn->file_names = obstack_alloc (&objfile->objfile_obstack,
2957 lh->num_file_names * sizeof (char *));
9291a0cd 2958 for (i = 0; i < lh->num_file_names; ++i)
7b9f3c50
DE
2959 qfn->file_names[i] = file_full_name (i + 1, lh, comp_dir);
2960 qfn->real_names = NULL;
9291a0cd 2961
7b9f3c50 2962 free_line_header (lh);
7b9f3c50 2963
094b34ac 2964 lh_cu->v.quick->file_names = qfn;
dee91e82
DE
2965}
2966
2967/* A helper for the "quick" functions which attempts to read the line
2968 table for THIS_CU. */
2969
2970static struct quick_file_names *
2971dw2_get_file_names (struct objfile *objfile,
2972 struct dwarf2_per_cu_data *this_cu)
2973{
f4dc4d17
DE
2974 /* For TUs this should only be called on the parent group. */
2975 if (this_cu->is_debug_types)
2976 gdb_assert (IS_TYPE_UNIT_GROUP (this_cu));
2977
dee91e82
DE
2978 if (this_cu->v.quick->file_names != NULL)
2979 return this_cu->v.quick->file_names;
2980 /* If we know there is no line data, no point in looking again. */
2981 if (this_cu->v.quick->no_file_data)
2982 return NULL;
2983
3019eac3
DE
2984 /* If DWO files are in use, we can still find the DW_AT_stmt_list attribute
2985 in the stub for CUs, there's is no need to lookup the DWO file.
2986 However, that's not the case for TUs where DW_AT_stmt_list lives in the
2987 DWO file. */
2988 if (this_cu->is_debug_types)
094b34ac
DE
2989 {
2990 struct type_unit_group *tu_group = this_cu->s.type_unit_group;
2991
2992 init_cutu_and_read_dies (tu_group->t.first_tu, NULL, 0, 0,
2993 dw2_get_file_names_reader, tu_group);
2994 }
3019eac3
DE
2995 else
2996 init_cutu_and_read_dies_simple (this_cu, dw2_get_file_names_reader, NULL);
dee91e82
DE
2997
2998 if (this_cu->v.quick->no_file_data)
2999 return NULL;
3000 return this_cu->v.quick->file_names;
9291a0cd
TT
3001}
3002
3003/* A helper for the "quick" functions which computes and caches the
7b9f3c50 3004 real path for a given file name from the line table. */
2fdf6df6 3005
9291a0cd 3006static const char *
7b9f3c50
DE
3007dw2_get_real_path (struct objfile *objfile,
3008 struct quick_file_names *qfn, int index)
9291a0cd 3009{
7b9f3c50
DE
3010 if (qfn->real_names == NULL)
3011 qfn->real_names = OBSTACK_CALLOC (&objfile->objfile_obstack,
3012 qfn->num_file_names, sizeof (char *));
9291a0cd 3013
7b9f3c50
DE
3014 if (qfn->real_names[index] == NULL)
3015 qfn->real_names[index] = gdb_realpath (qfn->file_names[index]);
9291a0cd 3016
7b9f3c50 3017 return qfn->real_names[index];
9291a0cd
TT
3018}
3019
3020static struct symtab *
3021dw2_find_last_source_symtab (struct objfile *objfile)
3022{
3023 int index;
ae2de4f8 3024
9291a0cd
TT
3025 dw2_setup (objfile);
3026 index = dwarf2_per_objfile->n_comp_units - 1;
a0f42c21 3027 return dw2_instantiate_symtab (dw2_get_cu (index));
9291a0cd
TT
3028}
3029
7b9f3c50
DE
3030/* Traversal function for dw2_forget_cached_source_info. */
3031
3032static int
3033dw2_free_cached_file_names (void **slot, void *info)
9291a0cd 3034{
7b9f3c50 3035 struct quick_file_names *file_data = (struct quick_file_names *) *slot;
9291a0cd 3036
7b9f3c50 3037 if (file_data->real_names)
9291a0cd 3038 {
7b9f3c50 3039 int i;
9291a0cd 3040
7b9f3c50 3041 for (i = 0; i < file_data->num_file_names; ++i)
9291a0cd 3042 {
7b9f3c50
DE
3043 xfree ((void*) file_data->real_names[i]);
3044 file_data->real_names[i] = NULL;
9291a0cd
TT
3045 }
3046 }
7b9f3c50
DE
3047
3048 return 1;
3049}
3050
3051static void
3052dw2_forget_cached_source_info (struct objfile *objfile)
3053{
3054 dw2_setup (objfile);
3055
3056 htab_traverse_noresize (dwarf2_per_objfile->quick_file_names_table,
3057 dw2_free_cached_file_names, NULL);
9291a0cd
TT
3058}
3059
f8eba3c6
TT
3060/* Helper function for dw2_map_symtabs_matching_filename that expands
3061 the symtabs and calls the iterator. */
3062
3063static int
3064dw2_map_expand_apply (struct objfile *objfile,
3065 struct dwarf2_per_cu_data *per_cu,
3066 const char *name,
3067 const char *full_path, const char *real_path,
3068 int (*callback) (struct symtab *, void *),
3069 void *data)
3070{
3071 struct symtab *last_made = objfile->symtabs;
3072
3073 /* Don't visit already-expanded CUs. */
3074 if (per_cu->v.quick->symtab)
3075 return 0;
3076
3077 /* This may expand more than one symtab, and we want to iterate over
3078 all of them. */
a0f42c21 3079 dw2_instantiate_symtab (per_cu);
f8eba3c6
TT
3080
3081 return iterate_over_some_symtabs (name, full_path, real_path, callback, data,
3082 objfile->symtabs, last_made);
3083}
3084
3085/* Implementation of the map_symtabs_matching_filename method. */
3086
9291a0cd 3087static int
f8eba3c6
TT
3088dw2_map_symtabs_matching_filename (struct objfile *objfile, const char *name,
3089 const char *full_path, const char *real_path,
3090 int (*callback) (struct symtab *, void *),
3091 void *data)
9291a0cd
TT
3092{
3093 int i;
c011a4f4 3094 const char *name_basename = lbasename (name);
4aac40c8
TT
3095 int name_len = strlen (name);
3096 int is_abs = IS_ABSOLUTE_PATH (name);
9291a0cd
TT
3097
3098 dw2_setup (objfile);
ae2de4f8 3099
f4dc4d17
DE
3100 dw2_build_type_unit_groups ();
3101
1fd400ff 3102 for (i = 0; i < (dwarf2_per_objfile->n_comp_units
f4dc4d17 3103 + dwarf2_per_objfile->n_type_unit_groups); ++i)
9291a0cd
TT
3104 {
3105 int j;
f4dc4d17 3106 struct dwarf2_per_cu_data *per_cu = dw2_get_primary_cu (i);
7b9f3c50 3107 struct quick_file_names *file_data;
9291a0cd 3108
3d7bb9d9 3109 /* We only need to look at symtabs not already expanded. */
e254ef6a 3110 if (per_cu->v.quick->symtab)
9291a0cd
TT
3111 continue;
3112
7b9f3c50
DE
3113 file_data = dw2_get_file_names (objfile, per_cu);
3114 if (file_data == NULL)
9291a0cd
TT
3115 continue;
3116
7b9f3c50 3117 for (j = 0; j < file_data->num_file_names; ++j)
9291a0cd 3118 {
7b9f3c50 3119 const char *this_name = file_data->file_names[j];
9291a0cd 3120
4aac40c8
TT
3121 if (FILENAME_CMP (name, this_name) == 0
3122 || (!is_abs && compare_filenames_for_search (this_name,
3123 name, name_len)))
9291a0cd 3124 {
f8eba3c6
TT
3125 if (dw2_map_expand_apply (objfile, per_cu,
3126 name, full_path, real_path,
3127 callback, data))
3128 return 1;
4aac40c8 3129 }
9291a0cd 3130
c011a4f4
DE
3131 /* Before we invoke realpath, which can get expensive when many
3132 files are involved, do a quick comparison of the basenames. */
3133 if (! basenames_may_differ
3134 && FILENAME_CMP (lbasename (this_name), name_basename) != 0)
3135 continue;
3136
9291a0cd
TT
3137 if (full_path != NULL)
3138 {
7b9f3c50
DE
3139 const char *this_real_name = dw2_get_real_path (objfile,
3140 file_data, j);
9291a0cd 3141
7b9f3c50 3142 if (this_real_name != NULL
4aac40c8
TT
3143 && (FILENAME_CMP (full_path, this_real_name) == 0
3144 || (!is_abs
3145 && compare_filenames_for_search (this_real_name,
3146 name, name_len))))
9291a0cd 3147 {
f8eba3c6
TT
3148 if (dw2_map_expand_apply (objfile, per_cu,
3149 name, full_path, real_path,
3150 callback, data))
3151 return 1;
9291a0cd
TT
3152 }
3153 }
3154
3155 if (real_path != NULL)
3156 {
7b9f3c50
DE
3157 const char *this_real_name = dw2_get_real_path (objfile,
3158 file_data, j);
9291a0cd 3159
7b9f3c50 3160 if (this_real_name != NULL
4aac40c8
TT
3161 && (FILENAME_CMP (real_path, this_real_name) == 0
3162 || (!is_abs
3163 && compare_filenames_for_search (this_real_name,
3164 name, name_len))))
9291a0cd 3165 {
f8eba3c6
TT
3166 if (dw2_map_expand_apply (objfile, per_cu,
3167 name, full_path, real_path,
3168 callback, data))
3169 return 1;
9291a0cd
TT
3170 }
3171 }
3172 }
3173 }
3174
9291a0cd
TT
3175 return 0;
3176}
3177
3178static struct symtab *
3179dw2_lookup_symbol (struct objfile *objfile, int block_index,
3180 const char *name, domain_enum domain)
3181{
774b6a14 3182 /* We do all the work in the pre_expand_symtabs_matching hook
9291a0cd
TT
3183 instead. */
3184 return NULL;
3185}
3186
3187/* A helper function that expands all symtabs that hold an object
156942c7
DE
3188 named NAME. If WANT_SPECIFIC_BLOCK is non-zero, only look for
3189 symbols in block BLOCK_KIND. */
2fdf6df6 3190
9291a0cd 3191static void
156942c7
DE
3192dw2_do_expand_symtabs_matching (struct objfile *objfile,
3193 int want_specific_block,
3194 enum block_enum block_kind,
3195 const char *name, domain_enum domain)
9291a0cd 3196{
156942c7
DE
3197 struct mapped_index *index;
3198
9291a0cd
TT
3199 dw2_setup (objfile);
3200
156942c7
DE
3201 index = dwarf2_per_objfile->index_table;
3202
ae2de4f8 3203 /* index_table is NULL if OBJF_READNOW. */
156942c7 3204 if (index)
9291a0cd
TT
3205 {
3206 offset_type *vec;
3207
156942c7 3208 if (find_slot_in_mapped_hash (index, name, &vec))
9291a0cd
TT
3209 {
3210 offset_type i, len = MAYBE_SWAP (*vec);
3211 for (i = 0; i < len; ++i)
3212 {
156942c7
DE
3213 offset_type cu_index_and_attrs = MAYBE_SWAP (vec[i + 1]);
3214 offset_type cu_index = GDB_INDEX_CU_VALUE (cu_index_and_attrs);
e254ef6a 3215 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (cu_index);
156942c7
DE
3216 int want_static = block_kind != GLOBAL_BLOCK;
3217 /* This value is only valid for index versions >= 7. */
3218 int is_static = GDB_INDEX_SYMBOL_STATIC_VALUE (cu_index_and_attrs);
3219 gdb_index_symbol_kind symbol_kind =
3220 GDB_INDEX_SYMBOL_KIND_VALUE (cu_index_and_attrs);
83a788b8
DE
3221 /* Only check the symbol attributes if they're present.
3222 Indices prior to version 7 don't record them,
3223 and indices >= 7 may elide them for certain symbols
3224 (gold does this). */
3225 int attrs_valid =
3226 (index->version >= 7
3227 && symbol_kind != GDB_INDEX_SYMBOL_KIND_NONE);
3228
3229 if (attrs_valid
3230 && want_specific_block
156942c7
DE
3231 && want_static != is_static)
3232 continue;
3233
83a788b8
DE
3234 /* Only check the symbol's kind if it has one. */
3235 if (attrs_valid)
156942c7
DE
3236 {
3237 switch (domain)
3238 {
3239 case VAR_DOMAIN:
3240 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_VARIABLE
3241 && symbol_kind != GDB_INDEX_SYMBOL_KIND_FUNCTION
3242 /* Some types are also in VAR_DOMAIN. */
3243 && symbol_kind != GDB_INDEX_SYMBOL_KIND_TYPE)
3244 continue;
3245 break;
3246 case STRUCT_DOMAIN:
3247 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_TYPE)
3248 continue;
3249 break;
3250 case LABEL_DOMAIN:
3251 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_OTHER)
3252 continue;
3253 break;
3254 default:
3255 break;
3256 }
3257 }
1fd400ff 3258
a0f42c21 3259 dw2_instantiate_symtab (per_cu);
9291a0cd
TT
3260 }
3261 }
3262 }
3263}
3264
774b6a14
TT
3265static void
3266dw2_pre_expand_symtabs_matching (struct objfile *objfile,
8903c50d 3267 enum block_enum block_kind, const char *name,
774b6a14 3268 domain_enum domain)
9291a0cd 3269{
156942c7 3270 dw2_do_expand_symtabs_matching (objfile, 1, block_kind, name, domain);
9291a0cd
TT
3271}
3272
3273static void
3274dw2_print_stats (struct objfile *objfile)
3275{
3276 int i, count;
3277
3278 dw2_setup (objfile);
3279 count = 0;
1fd400ff 3280 for (i = 0; i < (dwarf2_per_objfile->n_comp_units
d467dd73 3281 + dwarf2_per_objfile->n_type_units); ++i)
9291a0cd 3282 {
e254ef6a 3283 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
9291a0cd 3284
e254ef6a 3285 if (!per_cu->v.quick->symtab)
9291a0cd
TT
3286 ++count;
3287 }
3288 printf_filtered (_(" Number of unread CUs: %d\n"), count);
3289}
3290
3291static void
3292dw2_dump (struct objfile *objfile)
3293{
3294 /* Nothing worth printing. */
3295}
3296
3297static void
3298dw2_relocate (struct objfile *objfile, struct section_offsets *new_offsets,
3299 struct section_offsets *delta)
3300{
3301 /* There's nothing to relocate here. */
3302}
3303
3304static void
3305dw2_expand_symtabs_for_function (struct objfile *objfile,
3306 const char *func_name)
3307{
156942c7
DE
3308 /* Note: It doesn't matter what we pass for block_kind here. */
3309 dw2_do_expand_symtabs_matching (objfile, 0, GLOBAL_BLOCK, func_name,
3310 VAR_DOMAIN);
9291a0cd
TT
3311}
3312
3313static void
3314dw2_expand_all_symtabs (struct objfile *objfile)
3315{
3316 int i;
3317
3318 dw2_setup (objfile);
1fd400ff
TT
3319
3320 for (i = 0; i < (dwarf2_per_objfile->n_comp_units
d467dd73 3321 + dwarf2_per_objfile->n_type_units); ++i)
9291a0cd 3322 {
e254ef6a 3323 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
9291a0cd 3324
a0f42c21 3325 dw2_instantiate_symtab (per_cu);
9291a0cd
TT
3326 }
3327}
3328
3329static void
3330dw2_expand_symtabs_with_filename (struct objfile *objfile,
3331 const char *filename)
3332{
3333 int i;
3334
3335 dw2_setup (objfile);
d4637a04
DE
3336
3337 /* We don't need to consider type units here.
3338 This is only called for examining code, e.g. expand_line_sal.
3339 There can be an order of magnitude (or more) more type units
3340 than comp units, and we avoid them if we can. */
3341
3342 for (i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
9291a0cd
TT
3343 {
3344 int j;
e254ef6a 3345 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
7b9f3c50 3346 struct quick_file_names *file_data;
9291a0cd 3347
3d7bb9d9 3348 /* We only need to look at symtabs not already expanded. */
e254ef6a 3349 if (per_cu->v.quick->symtab)
9291a0cd
TT
3350 continue;
3351
7b9f3c50
DE
3352 file_data = dw2_get_file_names (objfile, per_cu);
3353 if (file_data == NULL)
9291a0cd
TT
3354 continue;
3355
7b9f3c50 3356 for (j = 0; j < file_data->num_file_names; ++j)
9291a0cd 3357 {
7b9f3c50 3358 const char *this_name = file_data->file_names[j];
1ef75ecc 3359 if (FILENAME_CMP (this_name, filename) == 0)
9291a0cd 3360 {
a0f42c21 3361 dw2_instantiate_symtab (per_cu);
9291a0cd
TT
3362 break;
3363 }
3364 }
3365 }
3366}
3367
356d9f9d
TT
3368/* A helper function for dw2_find_symbol_file that finds the primary
3369 file name for a given CU. This is a die_reader_func. */
3370
3371static void
3372dw2_get_primary_filename_reader (const struct die_reader_specs *reader,
3373 gdb_byte *info_ptr,
3374 struct die_info *comp_unit_die,
3375 int has_children,
3376 void *data)
3377{
3378 const char **result_ptr = data;
3379 struct dwarf2_cu *cu = reader->cu;
3380 struct attribute *attr;
3381
3382 attr = dwarf2_attr (comp_unit_die, DW_AT_name, cu);
3383 if (attr == NULL)
3384 *result_ptr = NULL;
3385 else
3386 *result_ptr = DW_STRING (attr);
3387}
3388
dd786858 3389static const char *
9291a0cd
TT
3390dw2_find_symbol_file (struct objfile *objfile, const char *name)
3391{
e254ef6a 3392 struct dwarf2_per_cu_data *per_cu;
9291a0cd 3393 offset_type *vec;
356d9f9d 3394 const char *filename;
9291a0cd
TT
3395
3396 dw2_setup (objfile);
3397
ae2de4f8 3398 /* index_table is NULL if OBJF_READNOW. */
9291a0cd 3399 if (!dwarf2_per_objfile->index_table)
96408a79
SA
3400 {
3401 struct symtab *s;
3402
d790cf0a
DE
3403 ALL_OBJFILE_PRIMARY_SYMTABS (objfile, s)
3404 {
3405 struct blockvector *bv = BLOCKVECTOR (s);
3406 const struct block *block = BLOCKVECTOR_BLOCK (bv, GLOBAL_BLOCK);
3407 struct symbol *sym = lookup_block_symbol (block, name, VAR_DOMAIN);
3408
3409 if (sym)
210bbc17 3410 return SYMBOL_SYMTAB (sym)->filename;
d790cf0a 3411 }
96408a79
SA
3412 return NULL;
3413 }
9291a0cd
TT
3414
3415 if (!find_slot_in_mapped_hash (dwarf2_per_objfile->index_table,
3416 name, &vec))
3417 return NULL;
3418
3419 /* Note that this just looks at the very first one named NAME -- but
3420 actually we are looking for a function. find_main_filename
3421 should be rewritten so that it doesn't require a custom hook. It
3422 could just use the ordinary symbol tables. */
3423 /* vec[0] is the length, which must always be >0. */
156942c7 3424 per_cu = dw2_get_cu (GDB_INDEX_CU_VALUE (MAYBE_SWAP (vec[1])));
9291a0cd 3425
356d9f9d
TT
3426 if (per_cu->v.quick->symtab != NULL)
3427 return per_cu->v.quick->symtab->filename;
3428
f4dc4d17
DE
3429 init_cutu_and_read_dies (per_cu, NULL, 0, 0,
3430 dw2_get_primary_filename_reader, &filename);
9291a0cd 3431
356d9f9d 3432 return filename;
9291a0cd
TT
3433}
3434
3435static void
40658b94
PH
3436dw2_map_matching_symbols (const char * name, domain_enum namespace,
3437 struct objfile *objfile, int global,
3438 int (*callback) (struct block *,
3439 struct symbol *, void *),
2edb89d3
JK
3440 void *data, symbol_compare_ftype *match,
3441 symbol_compare_ftype *ordered_compare)
9291a0cd 3442{
40658b94 3443 /* Currently unimplemented; used for Ada. The function can be called if the
a9e6a4bb
JK
3444 current language is Ada for a non-Ada objfile using GNU index. As Ada
3445 does not look for non-Ada symbols this function should just return. */
9291a0cd
TT
3446}
3447
3448static void
f8eba3c6
TT
3449dw2_expand_symtabs_matching
3450 (struct objfile *objfile,
3451 int (*file_matcher) (const char *, void *),
e078317b 3452 int (*name_matcher) (const char *, void *),
f8eba3c6
TT
3453 enum search_domain kind,
3454 void *data)
9291a0cd
TT
3455{
3456 int i;
3457 offset_type iter;
4b5246aa 3458 struct mapped_index *index;
9291a0cd
TT
3459
3460 dw2_setup (objfile);
ae2de4f8
DE
3461
3462 /* index_table is NULL if OBJF_READNOW. */
9291a0cd
TT
3463 if (!dwarf2_per_objfile->index_table)
3464 return;
4b5246aa 3465 index = dwarf2_per_objfile->index_table;
9291a0cd 3466
7b08b9eb 3467 if (file_matcher != NULL)
24c79950
TT
3468 {
3469 struct cleanup *cleanup;
3470 htab_t visited_found, visited_not_found;
3471
f4dc4d17
DE
3472 dw2_build_type_unit_groups ();
3473
24c79950
TT
3474 visited_found = htab_create_alloc (10,
3475 htab_hash_pointer, htab_eq_pointer,
3476 NULL, xcalloc, xfree);
3477 cleanup = make_cleanup_htab_delete (visited_found);
3478 visited_not_found = htab_create_alloc (10,
3479 htab_hash_pointer, htab_eq_pointer,
3480 NULL, xcalloc, xfree);
3481 make_cleanup_htab_delete (visited_not_found);
3482
3483 for (i = 0; i < (dwarf2_per_objfile->n_comp_units
f4dc4d17 3484 + dwarf2_per_objfile->n_type_unit_groups); ++i)
24c79950
TT
3485 {
3486 int j;
f4dc4d17 3487 struct dwarf2_per_cu_data *per_cu = dw2_get_primary_cu (i);
24c79950
TT
3488 struct quick_file_names *file_data;
3489 void **slot;
7b08b9eb 3490
24c79950 3491 per_cu->v.quick->mark = 0;
3d7bb9d9 3492
24c79950
TT
3493 /* We only need to look at symtabs not already expanded. */
3494 if (per_cu->v.quick->symtab)
3495 continue;
7b08b9eb 3496
24c79950
TT
3497 file_data = dw2_get_file_names (objfile, per_cu);
3498 if (file_data == NULL)
3499 continue;
7b08b9eb 3500
24c79950
TT
3501 if (htab_find (visited_not_found, file_data) != NULL)
3502 continue;
3503 else if (htab_find (visited_found, file_data) != NULL)
3504 {
3505 per_cu->v.quick->mark = 1;
3506 continue;
3507 }
3508
3509 for (j = 0; j < file_data->num_file_names; ++j)
3510 {
3511 if (file_matcher (file_data->file_names[j], data))
3512 {
3513 per_cu->v.quick->mark = 1;
3514 break;
3515 }
3516 }
3517
3518 slot = htab_find_slot (per_cu->v.quick->mark
3519 ? visited_found
3520 : visited_not_found,
3521 file_data, INSERT);
3522 *slot = file_data;
3523 }
3524
3525 do_cleanups (cleanup);
3526 }
9291a0cd 3527
3876f04e 3528 for (iter = 0; iter < index->symbol_table_slots; ++iter)
9291a0cd
TT
3529 {
3530 offset_type idx = 2 * iter;
3531 const char *name;
3532 offset_type *vec, vec_len, vec_idx;
3533
3876f04e 3534 if (index->symbol_table[idx] == 0 && index->symbol_table[idx + 1] == 0)
9291a0cd
TT
3535 continue;
3536
3876f04e 3537 name = index->constant_pool + MAYBE_SWAP (index->symbol_table[idx]);
9291a0cd 3538
e078317b 3539 if (! (*name_matcher) (name, data))
9291a0cd
TT
3540 continue;
3541
3542 /* The name was matched, now expand corresponding CUs that were
3543 marked. */
4b5246aa 3544 vec = (offset_type *) (index->constant_pool
3876f04e 3545 + MAYBE_SWAP (index->symbol_table[idx + 1]));
9291a0cd
TT
3546 vec_len = MAYBE_SWAP (vec[0]);
3547 for (vec_idx = 0; vec_idx < vec_len; ++vec_idx)
3548 {
e254ef6a 3549 struct dwarf2_per_cu_data *per_cu;
156942c7
DE
3550 offset_type cu_index_and_attrs = MAYBE_SWAP (vec[vec_idx + 1]);
3551 gdb_index_symbol_kind symbol_kind =
3552 GDB_INDEX_SYMBOL_KIND_VALUE (cu_index_and_attrs);
3553 int cu_index = GDB_INDEX_CU_VALUE (cu_index_and_attrs);
3554
3555 /* Don't crash on bad data. */
3556 if (cu_index >= (dwarf2_per_objfile->n_comp_units
667e0a4b 3557 + dwarf2_per_objfile->n_type_units))
156942c7 3558 continue;
1fd400ff 3559
156942c7
DE
3560 /* Only check the symbol's kind if it has one.
3561 Indices prior to version 7 don't record it. */
3562 if (index->version >= 7)
3563 {
3564 switch (kind)
3565 {
3566 case VARIABLES_DOMAIN:
3567 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_VARIABLE)
3568 continue;
3569 break;
3570 case FUNCTIONS_DOMAIN:
3571 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_FUNCTION)
3572 continue;
3573 break;
3574 case TYPES_DOMAIN:
3575 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_TYPE)
3576 continue;
3577 break;
3578 default:
3579 break;
3580 }
3581 }
3582
3583 per_cu = dw2_get_cu (cu_index);
7b08b9eb 3584 if (file_matcher == NULL || per_cu->v.quick->mark)
a0f42c21 3585 dw2_instantiate_symtab (per_cu);
9291a0cd
TT
3586 }
3587 }
3588}
3589
9703b513
TT
3590/* A helper for dw2_find_pc_sect_symtab which finds the most specific
3591 symtab. */
3592
3593static struct symtab *
3594recursively_find_pc_sect_symtab (struct symtab *symtab, CORE_ADDR pc)
3595{
3596 int i;
3597
3598 if (BLOCKVECTOR (symtab) != NULL
3599 && blockvector_contains_pc (BLOCKVECTOR (symtab), pc))
3600 return symtab;
3601
a3ec0bb1
DE
3602 if (symtab->includes == NULL)
3603 return NULL;
3604
9703b513
TT
3605 for (i = 0; symtab->includes[i]; ++i)
3606 {
a3ec0bb1 3607 struct symtab *s = symtab->includes[i];
9703b513
TT
3608
3609 s = recursively_find_pc_sect_symtab (s, pc);
3610 if (s != NULL)
3611 return s;
3612 }
3613
3614 return NULL;
3615}
3616
9291a0cd
TT
3617static struct symtab *
3618dw2_find_pc_sect_symtab (struct objfile *objfile,
3619 struct minimal_symbol *msymbol,
3620 CORE_ADDR pc,
3621 struct obj_section *section,
3622 int warn_if_readin)
3623{
3624 struct dwarf2_per_cu_data *data;
9703b513 3625 struct symtab *result;
9291a0cd
TT
3626
3627 dw2_setup (objfile);
3628
3629 if (!objfile->psymtabs_addrmap)
3630 return NULL;
3631
3632 data = addrmap_find (objfile->psymtabs_addrmap, pc);
3633 if (!data)
3634 return NULL;
3635
3636 if (warn_if_readin && data->v.quick->symtab)
abebb8b0 3637 warning (_("(Internal error: pc %s in read in CU, but not in symtab.)"),
9291a0cd
TT
3638 paddress (get_objfile_arch (objfile), pc));
3639
9703b513
TT
3640 result = recursively_find_pc_sect_symtab (dw2_instantiate_symtab (data), pc);
3641 gdb_assert (result != NULL);
3642 return result;
9291a0cd
TT
3643}
3644
9291a0cd 3645static void
44b13c5a 3646dw2_map_symbol_filenames (struct objfile *objfile, symbol_filename_ftype *fun,
74e2f255 3647 void *data, int need_fullname)
9291a0cd
TT
3648{
3649 int i;
24c79950
TT
3650 struct cleanup *cleanup;
3651 htab_t visited = htab_create_alloc (10, htab_hash_pointer, htab_eq_pointer,
3652 NULL, xcalloc, xfree);
9291a0cd 3653
24c79950 3654 cleanup = make_cleanup_htab_delete (visited);
9291a0cd 3655 dw2_setup (objfile);
ae2de4f8 3656
f4dc4d17
DE
3657 dw2_build_type_unit_groups ();
3658
24c79950
TT
3659 /* We can ignore file names coming from already-expanded CUs. */
3660 for (i = 0; i < (dwarf2_per_objfile->n_comp_units
3661 + dwarf2_per_objfile->n_type_units); ++i)
3662 {
3663 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
3664
3665 if (per_cu->v.quick->symtab)
3666 {
3667 void **slot = htab_find_slot (visited, per_cu->v.quick->file_names,
3668 INSERT);
3669
3670 *slot = per_cu->v.quick->file_names;
3671 }
3672 }
3673
1fd400ff 3674 for (i = 0; i < (dwarf2_per_objfile->n_comp_units
f4dc4d17 3675 + dwarf2_per_objfile->n_type_unit_groups); ++i)
9291a0cd
TT
3676 {
3677 int j;
f4dc4d17 3678 struct dwarf2_per_cu_data *per_cu = dw2_get_primary_cu (i);
7b9f3c50 3679 struct quick_file_names *file_data;
24c79950 3680 void **slot;
9291a0cd 3681
3d7bb9d9 3682 /* We only need to look at symtabs not already expanded. */
e254ef6a 3683 if (per_cu->v.quick->symtab)
9291a0cd
TT
3684 continue;
3685
7b9f3c50
DE
3686 file_data = dw2_get_file_names (objfile, per_cu);
3687 if (file_data == NULL)
9291a0cd
TT
3688 continue;
3689
24c79950
TT
3690 slot = htab_find_slot (visited, file_data, INSERT);
3691 if (*slot)
3692 {
3693 /* Already visited. */
3694 continue;
3695 }
3696 *slot = file_data;
3697
7b9f3c50 3698 for (j = 0; j < file_data->num_file_names; ++j)
9291a0cd 3699 {
74e2f255
DE
3700 const char *this_real_name;
3701
3702 if (need_fullname)
3703 this_real_name = dw2_get_real_path (objfile, file_data, j);
3704 else
3705 this_real_name = NULL;
7b9f3c50 3706 (*fun) (file_data->file_names[j], this_real_name, data);
9291a0cd
TT
3707 }
3708 }
24c79950
TT
3709
3710 do_cleanups (cleanup);
9291a0cd
TT
3711}
3712
3713static int
3714dw2_has_symbols (struct objfile *objfile)
3715{
3716 return 1;
3717}
3718
3719const struct quick_symbol_functions dwarf2_gdb_index_functions =
3720{
3721 dw2_has_symbols,
3722 dw2_find_last_source_symtab,
3723 dw2_forget_cached_source_info,
f8eba3c6 3724 dw2_map_symtabs_matching_filename,
9291a0cd 3725 dw2_lookup_symbol,
774b6a14 3726 dw2_pre_expand_symtabs_matching,
9291a0cd
TT
3727 dw2_print_stats,
3728 dw2_dump,
3729 dw2_relocate,
3730 dw2_expand_symtabs_for_function,
3731 dw2_expand_all_symtabs,
3732 dw2_expand_symtabs_with_filename,
3733 dw2_find_symbol_file,
40658b94 3734 dw2_map_matching_symbols,
9291a0cd
TT
3735 dw2_expand_symtabs_matching,
3736 dw2_find_pc_sect_symtab,
9291a0cd
TT
3737 dw2_map_symbol_filenames
3738};
3739
3740/* Initialize for reading DWARF for this objfile. Return 0 if this
3741 file will use psymtabs, or 1 if using the GNU index. */
3742
3743int
3744dwarf2_initialize_objfile (struct objfile *objfile)
3745{
3746 /* If we're about to read full symbols, don't bother with the
3747 indices. In this case we also don't care if some other debug
3748 format is making psymtabs, because they are all about to be
3749 expanded anyway. */
3750 if ((objfile->flags & OBJF_READNOW))
3751 {
3752 int i;
3753
3754 dwarf2_per_objfile->using_index = 1;
3755 create_all_comp_units (objfile);
0e50663e 3756 create_all_type_units (objfile);
7b9f3c50
DE
3757 dwarf2_per_objfile->quick_file_names_table =
3758 create_quick_file_names_table (dwarf2_per_objfile->n_comp_units);
9291a0cd 3759
1fd400ff 3760 for (i = 0; i < (dwarf2_per_objfile->n_comp_units
d467dd73 3761 + dwarf2_per_objfile->n_type_units); ++i)
9291a0cd 3762 {
e254ef6a 3763 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
9291a0cd 3764
e254ef6a
DE
3765 per_cu->v.quick = OBSTACK_ZALLOC (&objfile->objfile_obstack,
3766 struct dwarf2_per_cu_quick_data);
9291a0cd
TT
3767 }
3768
3769 /* Return 1 so that gdb sees the "quick" functions. However,
3770 these functions will be no-ops because we will have expanded
3771 all symtabs. */
3772 return 1;
3773 }
3774
3775 if (dwarf2_read_index (objfile))
3776 return 1;
3777
9291a0cd
TT
3778 return 0;
3779}
3780
3781\f
3782
dce234bc
PP
3783/* Build a partial symbol table. */
3784
3785void
f29dff0a 3786dwarf2_build_psymtabs (struct objfile *objfile)
dce234bc 3787{
f29dff0a 3788 if (objfile->global_psymbols.size == 0 && objfile->static_psymbols.size == 0)
c906108c
SS
3789 {
3790 init_psymbol_list (objfile, 1024);
3791 }
3792
d146bf1e 3793 dwarf2_build_psymtabs_hard (objfile);
c906108c 3794}
c906108c 3795
1ce1cefd
DE
3796/* Return the total length of the CU described by HEADER. */
3797
3798static unsigned int
3799get_cu_length (const struct comp_unit_head *header)
3800{
3801 return header->initial_length_size + header->length;
3802}
3803
45452591
DE
3804/* Return TRUE if OFFSET is within CU_HEADER. */
3805
3806static inline int
b64f50a1 3807offset_in_cu_p (const struct comp_unit_head *cu_header, sect_offset offset)
45452591 3808{
b64f50a1 3809 sect_offset bottom = { cu_header->offset.sect_off };
1ce1cefd 3810 sect_offset top = { cu_header->offset.sect_off + get_cu_length (cu_header) };
9a619af0 3811
b64f50a1 3812 return (offset.sect_off >= bottom.sect_off && offset.sect_off < top.sect_off);
45452591
DE
3813}
3814
3b80fe9b
DE
3815/* Find the base address of the compilation unit for range lists and
3816 location lists. It will normally be specified by DW_AT_low_pc.
3817 In DWARF-3 draft 4, the base address could be overridden by
3818 DW_AT_entry_pc. It's been removed, but GCC still uses this for
3819 compilation units with discontinuous ranges. */
3820
3821static void
3822dwarf2_find_base_address (struct die_info *die, struct dwarf2_cu *cu)
3823{
3824 struct attribute *attr;
3825
3826 cu->base_known = 0;
3827 cu->base_address = 0;
3828
3829 attr = dwarf2_attr (die, DW_AT_entry_pc, cu);
3830 if (attr)
3831 {
3832 cu->base_address = DW_ADDR (attr);
3833 cu->base_known = 1;
3834 }
3835 else
3836 {
3837 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
3838 if (attr)
3839 {
3840 cu->base_address = DW_ADDR (attr);
3841 cu->base_known = 1;
3842 }
3843 }
3844}
3845
93311388
DE
3846/* Read in the comp unit header information from the debug_info at info_ptr.
3847 NOTE: This leaves members offset, first_die_offset to be filled in
3848 by the caller. */
107d2387 3849
fe1b8b76 3850static gdb_byte *
107d2387 3851read_comp_unit_head (struct comp_unit_head *cu_header,
fe1b8b76 3852 gdb_byte *info_ptr, bfd *abfd)
107d2387
AC
3853{
3854 int signed_addr;
891d2f0b 3855 unsigned int bytes_read;
c764a876
DE
3856
3857 cu_header->length = read_initial_length (abfd, info_ptr, &bytes_read);
3858 cu_header->initial_length_size = bytes_read;
3859 cu_header->offset_size = (bytes_read == 4) ? 4 : 8;
613e1657 3860 info_ptr += bytes_read;
107d2387
AC
3861 cu_header->version = read_2_bytes (abfd, info_ptr);
3862 info_ptr += 2;
b64f50a1
JK
3863 cu_header->abbrev_offset.sect_off = read_offset (abfd, info_ptr, cu_header,
3864 &bytes_read);
613e1657 3865 info_ptr += bytes_read;
107d2387
AC
3866 cu_header->addr_size = read_1_byte (abfd, info_ptr);
3867 info_ptr += 1;
3868 signed_addr = bfd_get_sign_extend_vma (abfd);
3869 if (signed_addr < 0)
8e65ff28 3870 internal_error (__FILE__, __LINE__,
e2e0b3e5 3871 _("read_comp_unit_head: dwarf from non elf file"));
107d2387 3872 cu_header->signed_addr_p = signed_addr;
c764a876 3873
107d2387
AC
3874 return info_ptr;
3875}
3876
36586728
TT
3877/* Helper function that returns the proper abbrev section for
3878 THIS_CU. */
3879
3880static struct dwarf2_section_info *
3881get_abbrev_section_for_cu (struct dwarf2_per_cu_data *this_cu)
3882{
3883 struct dwarf2_section_info *abbrev;
3884
3885 if (this_cu->is_dwz)
3886 abbrev = &dwarf2_get_dwz_file ()->abbrev;
3887 else
3888 abbrev = &dwarf2_per_objfile->abbrev;
3889
3890 return abbrev;
3891}
3892
9ff913ba
DE
3893/* Subroutine of read_and_check_comp_unit_head and
3894 read_and_check_type_unit_head to simplify them.
3895 Perform various error checking on the header. */
3896
3897static void
3898error_check_comp_unit_head (struct comp_unit_head *header,
4bdcc0c1
DE
3899 struct dwarf2_section_info *section,
3900 struct dwarf2_section_info *abbrev_section)
9ff913ba
DE
3901{
3902 bfd *abfd = section->asection->owner;
3903 const char *filename = bfd_get_filename (abfd);
3904
3905 if (header->version != 2 && header->version != 3 && header->version != 4)
3906 error (_("Dwarf Error: wrong version in compilation unit header "
3907 "(is %d, should be 2, 3, or 4) [in module %s]"), header->version,
3908 filename);
3909
b64f50a1 3910 if (header->abbrev_offset.sect_off
36586728 3911 >= dwarf2_section_size (dwarf2_per_objfile->objfile, abbrev_section))
9ff913ba
DE
3912 error (_("Dwarf Error: bad offset (0x%lx) in compilation unit header "
3913 "(offset 0x%lx + 6) [in module %s]"),
b64f50a1 3914 (long) header->abbrev_offset.sect_off, (long) header->offset.sect_off,
9ff913ba
DE
3915 filename);
3916
3917 /* Cast to unsigned long to use 64-bit arithmetic when possible to
3918 avoid potential 32-bit overflow. */
1ce1cefd 3919 if (((unsigned long) header->offset.sect_off + get_cu_length (header))
9ff913ba
DE
3920 > section->size)
3921 error (_("Dwarf Error: bad length (0x%lx) in compilation unit header "
3922 "(offset 0x%lx + 0) [in module %s]"),
b64f50a1 3923 (long) header->length, (long) header->offset.sect_off,
9ff913ba
DE
3924 filename);
3925}
3926
3927/* Read in a CU/TU header and perform some basic error checking.
3928 The contents of the header are stored in HEADER.
3929 The result is a pointer to the start of the first DIE. */
adabb602 3930
fe1b8b76 3931static gdb_byte *
9ff913ba
DE
3932read_and_check_comp_unit_head (struct comp_unit_head *header,
3933 struct dwarf2_section_info *section,
4bdcc0c1 3934 struct dwarf2_section_info *abbrev_section,
9ff913ba
DE
3935 gdb_byte *info_ptr,
3936 int is_debug_types_section)
72bf9492 3937{
fe1b8b76 3938 gdb_byte *beg_of_comp_unit = info_ptr;
9ff913ba 3939 bfd *abfd = section->asection->owner;
72bf9492 3940
b64f50a1 3941 header->offset.sect_off = beg_of_comp_unit - section->buffer;
adabb602 3942
72bf9492
DJ
3943 info_ptr = read_comp_unit_head (header, info_ptr, abfd);
3944
460c1c54
CC
3945 /* If we're reading a type unit, skip over the signature and
3946 type_offset fields. */
b0df02fd 3947 if (is_debug_types_section)
460c1c54
CC
3948 info_ptr += 8 /*signature*/ + header->offset_size;
3949
b64f50a1 3950 header->first_die_offset.cu_off = info_ptr - beg_of_comp_unit;
adabb602 3951
4bdcc0c1 3952 error_check_comp_unit_head (header, section, abbrev_section);
72bf9492
DJ
3953
3954 return info_ptr;
3955}
3956
348e048f
DE
3957/* Read in the types comp unit header information from .debug_types entry at
3958 types_ptr. The result is a pointer to one past the end of the header. */
3959
3960static gdb_byte *
9ff913ba
DE
3961read_and_check_type_unit_head (struct comp_unit_head *header,
3962 struct dwarf2_section_info *section,
4bdcc0c1 3963 struct dwarf2_section_info *abbrev_section,
9ff913ba 3964 gdb_byte *info_ptr,
dee91e82
DE
3965 ULONGEST *signature,
3966 cu_offset *type_offset_in_tu)
348e048f 3967{
9ff913ba
DE
3968 gdb_byte *beg_of_comp_unit = info_ptr;
3969 bfd *abfd = section->asection->owner;
348e048f 3970
b64f50a1 3971 header->offset.sect_off = beg_of_comp_unit - section->buffer;
348e048f 3972
9ff913ba 3973 info_ptr = read_comp_unit_head (header, info_ptr, abfd);
348e048f 3974
9ff913ba
DE
3975 /* If we're reading a type unit, skip over the signature and
3976 type_offset fields. */
3977 if (signature != NULL)
3978 *signature = read_8_bytes (abfd, info_ptr);
3979 info_ptr += 8;
dee91e82
DE
3980 if (type_offset_in_tu != NULL)
3981 type_offset_in_tu->cu_off = read_offset_1 (abfd, info_ptr,
3982 header->offset_size);
9ff913ba
DE
3983 info_ptr += header->offset_size;
3984
b64f50a1 3985 header->first_die_offset.cu_off = info_ptr - beg_of_comp_unit;
348e048f 3986
4bdcc0c1 3987 error_check_comp_unit_head (header, section, abbrev_section);
9ff913ba
DE
3988
3989 return info_ptr;
348e048f
DE
3990}
3991
f4dc4d17
DE
3992/* Fetch the abbreviation table offset from a comp or type unit header. */
3993
3994static sect_offset
3995read_abbrev_offset (struct dwarf2_section_info *section,
3996 sect_offset offset)
3997{
3998 bfd *abfd = section->asection->owner;
3999 gdb_byte *info_ptr;
4000 unsigned int length, initial_length_size, offset_size;
4001 sect_offset abbrev_offset;
4002
4003 dwarf2_read_section (dwarf2_per_objfile->objfile, section);
4004 info_ptr = section->buffer + offset.sect_off;
4005 length = read_initial_length (abfd, info_ptr, &initial_length_size);
4006 offset_size = initial_length_size == 4 ? 4 : 8;
4007 info_ptr += initial_length_size + 2 /*version*/;
4008 abbrev_offset.sect_off = read_offset_1 (abfd, info_ptr, offset_size);
4009 return abbrev_offset;
4010}
4011
aaa75496
JB
4012/* Allocate a new partial symtab for file named NAME and mark this new
4013 partial symtab as being an include of PST. */
4014
4015static void
4016dwarf2_create_include_psymtab (char *name, struct partial_symtab *pst,
4017 struct objfile *objfile)
4018{
4019 struct partial_symtab *subpst = allocate_psymtab (name, objfile);
4020
4021 subpst->section_offsets = pst->section_offsets;
4022 subpst->textlow = 0;
4023 subpst->texthigh = 0;
4024
4025 subpst->dependencies = (struct partial_symtab **)
4026 obstack_alloc (&objfile->objfile_obstack,
4027 sizeof (struct partial_symtab *));
4028 subpst->dependencies[0] = pst;
4029 subpst->number_of_dependencies = 1;
4030
4031 subpst->globals_offset = 0;
4032 subpst->n_global_syms = 0;
4033 subpst->statics_offset = 0;
4034 subpst->n_static_syms = 0;
4035 subpst->symtab = NULL;
4036 subpst->read_symtab = pst->read_symtab;
4037 subpst->readin = 0;
4038
4039 /* No private part is necessary for include psymtabs. This property
4040 can be used to differentiate between such include psymtabs and
10b3939b 4041 the regular ones. */
58a9656e 4042 subpst->read_symtab_private = NULL;
aaa75496
JB
4043}
4044
4045/* Read the Line Number Program data and extract the list of files
4046 included by the source file represented by PST. Build an include
d85a05f0 4047 partial symtab for each of these included files. */
aaa75496
JB
4048
4049static void
4050dwarf2_build_include_psymtabs (struct dwarf2_cu *cu,
dee91e82
DE
4051 struct die_info *die,
4052 struct partial_symtab *pst)
aaa75496 4053{
d85a05f0
DJ
4054 struct line_header *lh = NULL;
4055 struct attribute *attr;
aaa75496 4056
d85a05f0
DJ
4057 attr = dwarf2_attr (die, DW_AT_stmt_list, cu);
4058 if (attr)
3019eac3 4059 lh = dwarf_decode_line_header (DW_UNSND (attr), cu);
aaa75496
JB
4060 if (lh == NULL)
4061 return; /* No linetable, so no includes. */
4062
c6da4cef 4063 /* NOTE: pst->dirname is DW_AT_comp_dir (if present). */
f3f5162e 4064 dwarf_decode_lines (lh, pst->dirname, cu, pst, 1);
aaa75496
JB
4065
4066 free_line_header (lh);
4067}
4068
348e048f 4069static hashval_t
52dc124a 4070hash_signatured_type (const void *item)
348e048f 4071{
52dc124a 4072 const struct signatured_type *sig_type = item;
9a619af0 4073
348e048f 4074 /* This drops the top 32 bits of the signature, but is ok for a hash. */
52dc124a 4075 return sig_type->signature;
348e048f
DE
4076}
4077
4078static int
52dc124a 4079eq_signatured_type (const void *item_lhs, const void *item_rhs)
348e048f
DE
4080{
4081 const struct signatured_type *lhs = item_lhs;
4082 const struct signatured_type *rhs = item_rhs;
9a619af0 4083
348e048f
DE
4084 return lhs->signature == rhs->signature;
4085}
4086
1fd400ff
TT
4087/* Allocate a hash table for signatured types. */
4088
4089static htab_t
673bfd45 4090allocate_signatured_type_table (struct objfile *objfile)
1fd400ff
TT
4091{
4092 return htab_create_alloc_ex (41,
52dc124a
DE
4093 hash_signatured_type,
4094 eq_signatured_type,
1fd400ff
TT
4095 NULL,
4096 &objfile->objfile_obstack,
4097 hashtab_obstack_allocate,
4098 dummy_obstack_deallocate);
4099}
4100
d467dd73 4101/* A helper function to add a signatured type CU to a table. */
1fd400ff
TT
4102
4103static int
d467dd73 4104add_signatured_type_cu_to_table (void **slot, void *datum)
1fd400ff
TT
4105{
4106 struct signatured_type *sigt = *slot;
b4dd5633 4107 struct signatured_type ***datap = datum;
1fd400ff 4108
b4dd5633 4109 **datap = sigt;
1fd400ff
TT
4110 ++*datap;
4111
4112 return 1;
4113}
4114
3019eac3 4115/* Create the hash table of all entries in the .debug_types section.
80626a55
DE
4116 DWO_FILE is a pointer to the DWO file for .debug_types.dwo,
4117 NULL otherwise.
4118 Note: This function processes DWO files only, not DWP files.
3019eac3
DE
4119 The result is a pointer to the hash table or NULL if there are
4120 no types. */
348e048f 4121
3019eac3
DE
4122static htab_t
4123create_debug_types_hash_table (struct dwo_file *dwo_file,
4124 VEC (dwarf2_section_info_def) *types)
348e048f 4125{
3019eac3 4126 struct objfile *objfile = dwarf2_per_objfile->objfile;
8b70b953 4127 htab_t types_htab = NULL;
8b70b953
TT
4128 int ix;
4129 struct dwarf2_section_info *section;
4bdcc0c1 4130 struct dwarf2_section_info *abbrev_section;
348e048f 4131
3019eac3
DE
4132 if (VEC_empty (dwarf2_section_info_def, types))
4133 return NULL;
348e048f 4134
4bdcc0c1
DE
4135 abbrev_section = (dwo_file != NULL
4136 ? &dwo_file->sections.abbrev
4137 : &dwarf2_per_objfile->abbrev);
4138
09406207
DE
4139 if (dwarf2_read_debug)
4140 fprintf_unfiltered (gdb_stdlog, "Reading .debug_types%s for %s:\n",
4141 dwo_file ? ".dwo" : "",
4142 bfd_get_filename (abbrev_section->asection->owner));
4143
8b70b953 4144 for (ix = 0;
3019eac3 4145 VEC_iterate (dwarf2_section_info_def, types, ix, section);
8b70b953
TT
4146 ++ix)
4147 {
3019eac3 4148 bfd *abfd;
8b70b953 4149 gdb_byte *info_ptr, *end_ptr;
36586728 4150 struct dwarf2_section_info *abbrev_section;
348e048f 4151
8b70b953
TT
4152 dwarf2_read_section (objfile, section);
4153 info_ptr = section->buffer;
348e048f 4154
8b70b953
TT
4155 if (info_ptr == NULL)
4156 continue;
348e048f 4157
3019eac3
DE
4158 /* We can't set abfd until now because the section may be empty or
4159 not present, in which case section->asection will be NULL. */
4160 abfd = section->asection->owner;
4161
36586728
TT
4162 if (dwo_file)
4163 abbrev_section = &dwo_file->sections.abbrev;
4164 else
4165 abbrev_section = &dwarf2_per_objfile->abbrev;
4166
8b70b953 4167 if (types_htab == NULL)
3019eac3
DE
4168 {
4169 if (dwo_file)
4170 types_htab = allocate_dwo_unit_table (objfile);
4171 else
4172 types_htab = allocate_signatured_type_table (objfile);
4173 }
348e048f 4174
dee91e82
DE
4175 /* We don't use init_cutu_and_read_dies_simple, or some such, here
4176 because we don't need to read any dies: the signature is in the
4177 header. */
8b70b953
TT
4178
4179 end_ptr = info_ptr + section->size;
4180 while (info_ptr < end_ptr)
4181 {
b64f50a1 4182 sect_offset offset;
3019eac3 4183 cu_offset type_offset_in_tu;
8b70b953 4184 ULONGEST signature;
52dc124a 4185 struct signatured_type *sig_type;
3019eac3 4186 struct dwo_unit *dwo_tu;
8b70b953
TT
4187 void **slot;
4188 gdb_byte *ptr = info_ptr;
9ff913ba 4189 struct comp_unit_head header;
dee91e82 4190 unsigned int length;
348e048f 4191
b64f50a1 4192 offset.sect_off = ptr - section->buffer;
348e048f 4193
8b70b953 4194 /* We need to read the type's signature in order to build the hash
9ff913ba 4195 table, but we don't need anything else just yet. */
348e048f 4196
4bdcc0c1
DE
4197 ptr = read_and_check_type_unit_head (&header, section,
4198 abbrev_section, ptr,
3019eac3 4199 &signature, &type_offset_in_tu);
6caca83c 4200
1ce1cefd 4201 length = get_cu_length (&header);
dee91e82 4202
6caca83c 4203 /* Skip dummy type units. */
dee91e82
DE
4204 if (ptr >= info_ptr + length
4205 || peek_abbrev_code (abfd, ptr) == 0)
6caca83c 4206 {
1ce1cefd 4207 info_ptr += length;
6caca83c
CC
4208 continue;
4209 }
8b70b953 4210
3019eac3
DE
4211 if (dwo_file)
4212 {
4213 sig_type = NULL;
4214 dwo_tu = OBSTACK_ZALLOC (&objfile->objfile_obstack,
4215 struct dwo_unit);
4216 dwo_tu->dwo_file = dwo_file;
4217 dwo_tu->signature = signature;
4218 dwo_tu->type_offset_in_tu = type_offset_in_tu;
4219 dwo_tu->info_or_types_section = section;
4220 dwo_tu->offset = offset;
4221 dwo_tu->length = length;
4222 }
4223 else
4224 {
4225 /* N.B.: type_offset is not usable if this type uses a DWO file.
4226 The real type_offset is in the DWO file. */
4227 dwo_tu = NULL;
4228 sig_type = OBSTACK_ZALLOC (&objfile->objfile_obstack,
4229 struct signatured_type);
4230 sig_type->signature = signature;
4231 sig_type->type_offset_in_tu = type_offset_in_tu;
4232 sig_type->per_cu.objfile = objfile;
4233 sig_type->per_cu.is_debug_types = 1;
4234 sig_type->per_cu.info_or_types_section = section;
4235 sig_type->per_cu.offset = offset;
4236 sig_type->per_cu.length = length;
4237 }
8b70b953 4238
3019eac3
DE
4239 slot = htab_find_slot (types_htab,
4240 dwo_file ? (void*) dwo_tu : (void *) sig_type,
4241 INSERT);
8b70b953
TT
4242 gdb_assert (slot != NULL);
4243 if (*slot != NULL)
4244 {
3019eac3
DE
4245 sect_offset dup_offset;
4246
4247 if (dwo_file)
4248 {
4249 const struct dwo_unit *dup_tu = *slot;
4250
4251 dup_offset = dup_tu->offset;
4252 }
4253 else
4254 {
4255 const struct signatured_type *dup_tu = *slot;
4256
4257 dup_offset = dup_tu->per_cu.offset;
4258 }
b3c8eb43 4259
8b70b953
TT
4260 complaint (&symfile_complaints,
4261 _("debug type entry at offset 0x%x is duplicate to the "
4262 "entry at offset 0x%x, signature 0x%s"),
3019eac3 4263 offset.sect_off, dup_offset.sect_off,
8b70b953 4264 phex (signature, sizeof (signature)));
8b70b953 4265 }
3019eac3 4266 *slot = dwo_file ? (void *) dwo_tu : (void *) sig_type;
348e048f 4267
09406207 4268 if (dwarf2_read_debug)
8b70b953 4269 fprintf_unfiltered (gdb_stdlog, " offset 0x%x, signature 0x%s\n",
b64f50a1
JK
4270 offset.sect_off,
4271 phex (signature, sizeof (signature)));
348e048f 4272
dee91e82 4273 info_ptr += length;
8b70b953 4274 }
348e048f
DE
4275 }
4276
3019eac3
DE
4277 return types_htab;
4278}
4279
4280/* Create the hash table of all entries in the .debug_types section,
4281 and initialize all_type_units.
4282 The result is zero if there is an error (e.g. missing .debug_types section),
4283 otherwise non-zero. */
4284
4285static int
4286create_all_type_units (struct objfile *objfile)
4287{
4288 htab_t types_htab;
b4dd5633 4289 struct signatured_type **iter;
3019eac3
DE
4290
4291 types_htab = create_debug_types_hash_table (NULL, dwarf2_per_objfile->types);
4292 if (types_htab == NULL)
4293 {
4294 dwarf2_per_objfile->signatured_types = NULL;
4295 return 0;
4296 }
4297
348e048f
DE
4298 dwarf2_per_objfile->signatured_types = types_htab;
4299
d467dd73
DE
4300 dwarf2_per_objfile->n_type_units = htab_elements (types_htab);
4301 dwarf2_per_objfile->all_type_units
1fd400ff 4302 = obstack_alloc (&objfile->objfile_obstack,
d467dd73 4303 dwarf2_per_objfile->n_type_units
b4dd5633 4304 * sizeof (struct signatured_type *));
d467dd73
DE
4305 iter = &dwarf2_per_objfile->all_type_units[0];
4306 htab_traverse_noresize (types_htab, add_signatured_type_cu_to_table, &iter);
4307 gdb_assert (iter - &dwarf2_per_objfile->all_type_units[0]
4308 == dwarf2_per_objfile->n_type_units);
1fd400ff 4309
348e048f
DE
4310 return 1;
4311}
4312
380bca97 4313/* Lookup a signature based type for DW_FORM_ref_sig8.
e319fa28 4314 Returns NULL if signature SIG is not present in the table. */
348e048f
DE
4315
4316static struct signatured_type *
e319fa28 4317lookup_signatured_type (ULONGEST sig)
348e048f
DE
4318{
4319 struct signatured_type find_entry, *entry;
4320
4321 if (dwarf2_per_objfile->signatured_types == NULL)
4322 {
4323 complaint (&symfile_complaints,
55f1336d 4324 _("missing `.debug_types' section for DW_FORM_ref_sig8 die"));
dcc07052 4325 return NULL;
348e048f
DE
4326 }
4327
4328 find_entry.signature = sig;
4329 entry = htab_find (dwarf2_per_objfile->signatured_types, &find_entry);
4330 return entry;
4331}
42e7ad6c
DE
4332\f
4333/* Low level DIE reading support. */
348e048f 4334
d85a05f0
DJ
4335/* Initialize a die_reader_specs struct from a dwarf2_cu struct. */
4336
4337static void
4338init_cu_die_reader (struct die_reader_specs *reader,
dee91e82 4339 struct dwarf2_cu *cu,
3019eac3
DE
4340 struct dwarf2_section_info *section,
4341 struct dwo_file *dwo_file)
d85a05f0 4342{
fceca515 4343 gdb_assert (section->readin && section->buffer != NULL);
dee91e82 4344 reader->abfd = section->asection->owner;
d85a05f0 4345 reader->cu = cu;
3019eac3 4346 reader->dwo_file = dwo_file;
dee91e82
DE
4347 reader->die_section = section;
4348 reader->buffer = section->buffer;
f664829e 4349 reader->buffer_end = section->buffer + section->size;
d85a05f0
DJ
4350}
4351
fd820528 4352/* Initialize a CU (or TU) and read its DIEs.
3019eac3 4353 If the CU defers to a DWO file, read the DWO file as well.
dee91e82 4354
f4dc4d17
DE
4355 ABBREV_TABLE, if non-NULL, is the abbreviation table to use.
4356 Otherwise the table specified in the comp unit header is read in and used.
4357 This is an optimization for when we already have the abbrev table.
4358
dee91e82
DE
4359 If USE_EXISTING_CU is non-zero, and THIS_CU->cu is non-NULL, then use it.
4360 Otherwise, a new CU is allocated with xmalloc.
4361
4362 If KEEP is non-zero, then if we allocated a dwarf2_cu we add it to
4363 read_in_chain. Otherwise the dwarf2_cu data is freed at the end.
4364
4365 WARNING: If THIS_CU is a "dummy CU" (used as filler by the incremental
fd820528 4366 linker) then DIE_READER_FUNC will not get called. */
aaa75496 4367
70221824 4368static void
fd820528 4369init_cutu_and_read_dies (struct dwarf2_per_cu_data *this_cu,
f4dc4d17 4370 struct abbrev_table *abbrev_table,
fd820528
DE
4371 int use_existing_cu, int keep,
4372 die_reader_func_ftype *die_reader_func,
4373 void *data)
c906108c 4374{
dee91e82 4375 struct objfile *objfile = dwarf2_per_objfile->objfile;
3019eac3
DE
4376 struct dwarf2_section_info *section = this_cu->info_or_types_section;
4377 bfd *abfd = section->asection->owner;
dee91e82
DE
4378 struct dwarf2_cu *cu;
4379 gdb_byte *begin_info_ptr, *info_ptr;
4380 struct die_reader_specs reader;
d85a05f0 4381 struct die_info *comp_unit_die;
dee91e82 4382 int has_children;
d85a05f0 4383 struct attribute *attr;
dee91e82
DE
4384 struct cleanup *cleanups, *free_cu_cleanup = NULL;
4385 struct signatured_type *sig_type = NULL;
4bdcc0c1 4386 struct dwarf2_section_info *abbrev_section;
42e7ad6c
DE
4387 /* Non-zero if CU currently points to a DWO file and we need to
4388 reread it. When this happens we need to reread the skeleton die
4389 before we can reread the DWO file. */
4390 int rereading_dwo_cu = 0;
c906108c 4391
09406207
DE
4392 if (dwarf2_die_debug)
4393 fprintf_unfiltered (gdb_stdlog, "Reading %s unit at offset 0x%x\n",
4394 this_cu->is_debug_types ? "type" : "comp",
4395 this_cu->offset.sect_off);
4396
dee91e82
DE
4397 if (use_existing_cu)
4398 gdb_assert (keep);
23745b47 4399
dee91e82
DE
4400 cleanups = make_cleanup (null_cleanup, NULL);
4401
4402 /* This is cheap if the section is already read in. */
4403 dwarf2_read_section (objfile, section);
4404
4405 begin_info_ptr = info_ptr = section->buffer + this_cu->offset.sect_off;
36586728
TT
4406
4407 abbrev_section = get_abbrev_section_for_cu (this_cu);
dee91e82
DE
4408
4409 if (use_existing_cu && this_cu->cu != NULL)
4410 {
4411 cu = this_cu->cu;
42e7ad6c
DE
4412
4413 /* If this CU is from a DWO file we need to start over, we need to
4414 refetch the attributes from the skeleton CU.
4415 This could be optimized by retrieving those attributes from when we
4416 were here the first time: the previous comp_unit_die was stored in
4417 comp_unit_obstack. But there's no data yet that we need this
4418 optimization. */
4419 if (cu->dwo_unit != NULL)
4420 rereading_dwo_cu = 1;
dee91e82
DE
4421 }
4422 else
4423 {
4424 /* If !use_existing_cu, this_cu->cu must be NULL. */
4425 gdb_assert (this_cu->cu == NULL);
4426
4427 cu = xmalloc (sizeof (*cu));
4428 init_one_comp_unit (cu, this_cu);
4429
4430 /* If an error occurs while loading, release our storage. */
4431 free_cu_cleanup = make_cleanup (free_heap_comp_unit, cu);
42e7ad6c 4432 }
dee91e82 4433
42e7ad6c
DE
4434 if (cu->header.first_die_offset.cu_off != 0 && ! rereading_dwo_cu)
4435 {
4436 /* We already have the header, there's no need to read it in again. */
4437 info_ptr += cu->header.first_die_offset.cu_off;
4438 }
4439 else
4440 {
3019eac3 4441 if (this_cu->is_debug_types)
dee91e82
DE
4442 {
4443 ULONGEST signature;
42e7ad6c 4444 cu_offset type_offset_in_tu;
dee91e82 4445
4bdcc0c1
DE
4446 info_ptr = read_and_check_type_unit_head (&cu->header, section,
4447 abbrev_section, info_ptr,
42e7ad6c
DE
4448 &signature,
4449 &type_offset_in_tu);
dee91e82 4450
42e7ad6c
DE
4451 /* Since per_cu is the first member of struct signatured_type,
4452 we can go from a pointer to one to a pointer to the other. */
4453 sig_type = (struct signatured_type *) this_cu;
4454 gdb_assert (sig_type->signature == signature);
4455 gdb_assert (sig_type->type_offset_in_tu.cu_off
4456 == type_offset_in_tu.cu_off);
dee91e82
DE
4457 gdb_assert (this_cu->offset.sect_off == cu->header.offset.sect_off);
4458
42e7ad6c
DE
4459 /* LENGTH has not been set yet for type units if we're
4460 using .gdb_index. */
1ce1cefd 4461 this_cu->length = get_cu_length (&cu->header);
3019eac3
DE
4462
4463 /* Establish the type offset that can be used to lookup the type. */
4464 sig_type->type_offset_in_section.sect_off =
4465 this_cu->offset.sect_off + sig_type->type_offset_in_tu.cu_off;
dee91e82
DE
4466 }
4467 else
4468 {
4bdcc0c1
DE
4469 info_ptr = read_and_check_comp_unit_head (&cu->header, section,
4470 abbrev_section,
4471 info_ptr, 0);
dee91e82
DE
4472
4473 gdb_assert (this_cu->offset.sect_off == cu->header.offset.sect_off);
1ce1cefd 4474 gdb_assert (this_cu->length == get_cu_length (&cu->header));
dee91e82
DE
4475 }
4476 }
10b3939b 4477
6caca83c 4478 /* Skip dummy compilation units. */
dee91e82 4479 if (info_ptr >= begin_info_ptr + this_cu->length
6caca83c
CC
4480 || peek_abbrev_code (abfd, info_ptr) == 0)
4481 {
dee91e82 4482 do_cleanups (cleanups);
21b2bd31 4483 return;
6caca83c
CC
4484 }
4485
433df2d4
DE
4486 /* If we don't have them yet, read the abbrevs for this compilation unit.
4487 And if we need to read them now, make sure they're freed when we're
42e7ad6c
DE
4488 done. Note that it's important that if the CU had an abbrev table
4489 on entry we don't free it when we're done: Somewhere up the call stack
4490 it may be in use. */
f4dc4d17
DE
4491 if (abbrev_table != NULL)
4492 {
4493 gdb_assert (cu->abbrev_table == NULL);
4494 gdb_assert (cu->header.abbrev_offset.sect_off
4495 == abbrev_table->offset.sect_off);
4496 cu->abbrev_table = abbrev_table;
4497 }
4498 else if (cu->abbrev_table == NULL)
dee91e82 4499 {
4bdcc0c1 4500 dwarf2_read_abbrevs (cu, abbrev_section);
dee91e82
DE
4501 make_cleanup (dwarf2_free_abbrev_table, cu);
4502 }
42e7ad6c
DE
4503 else if (rereading_dwo_cu)
4504 {
4505 dwarf2_free_abbrev_table (cu);
4506 dwarf2_read_abbrevs (cu, abbrev_section);
4507 }
af703f96 4508
dee91e82 4509 /* Read the top level CU/TU die. */
3019eac3 4510 init_cu_die_reader (&reader, cu, section, NULL);
dee91e82 4511 info_ptr = read_full_die (&reader, &comp_unit_die, info_ptr, &has_children);
93311388 4512
3019eac3
DE
4513 /* If we have a DWO stub, process it and then read in the DWO file.
4514 Note that if USE_EXISTING_OK != 0, and THIS_CU->cu already contains
4515 a DWO CU, that this test will fail. */
4516 attr = dwarf2_attr (comp_unit_die, DW_AT_GNU_dwo_name, cu);
4517 if (attr)
4518 {
4519 char *dwo_name = DW_STRING (attr);
42e7ad6c 4520 const char *comp_dir_string;
3019eac3
DE
4521 struct dwo_unit *dwo_unit;
4522 ULONGEST signature; /* Or dwo_id. */
42e7ad6c 4523 struct attribute *comp_dir, *stmt_list, *low_pc, *high_pc, *ranges;
3019eac3 4524 int i,num_extra_attrs;
4bdcc0c1 4525 struct dwarf2_section_info *dwo_abbrev_section;
3019eac3
DE
4526
4527 if (has_children)
4528 error (_("Dwarf Error: compilation unit with DW_AT_GNU_dwo_name"
4529 " has children (offset 0x%x) [in module %s]"),
4530 this_cu->offset.sect_off, bfd_get_filename (abfd));
4531
4532 /* These attributes aren't processed until later:
4533 DW_AT_stmt_list, DW_AT_low_pc, DW_AT_high_pc, DW_AT_ranges.
4534 However, the attribute is found in the stub which we won't have later.
4535 In order to not impose this complication on the rest of the code,
4536 we read them here and copy them to the DWO CU/TU die. */
3019eac3
DE
4537
4538 /* For TUs in DWO files, the DW_AT_stmt_list attribute lives in the
4539 DWO file. */
42e7ad6c 4540 stmt_list = NULL;
3019eac3
DE
4541 if (! this_cu->is_debug_types)
4542 stmt_list = dwarf2_attr (comp_unit_die, DW_AT_stmt_list, cu);
4543 low_pc = dwarf2_attr (comp_unit_die, DW_AT_low_pc, cu);
4544 high_pc = dwarf2_attr (comp_unit_die, DW_AT_high_pc, cu);
4545 ranges = dwarf2_attr (comp_unit_die, DW_AT_ranges, cu);
42e7ad6c 4546 comp_dir = dwarf2_attr (comp_unit_die, DW_AT_comp_dir, cu);
3019eac3
DE
4547
4548 /* There should be a DW_AT_addr_base attribute here (if needed).
4549 We need the value before we can process DW_FORM_GNU_addr_index. */
4550 cu->addr_base = 0;
3019eac3
DE
4551 attr = dwarf2_attr (comp_unit_die, DW_AT_GNU_addr_base, cu);
4552 if (attr)
2e3cf129
DE
4553 cu->addr_base = DW_UNSND (attr);
4554
4555 /* There should be a DW_AT_ranges_base attribute here (if needed).
4556 We need the value before we can process DW_AT_ranges. */
4557 cu->ranges_base = 0;
4558 attr = dwarf2_attr (comp_unit_die, DW_AT_GNU_ranges_base, cu);
4559 if (attr)
4560 cu->ranges_base = DW_UNSND (attr);
3019eac3
DE
4561
4562 if (this_cu->is_debug_types)
4563 {
4564 gdb_assert (sig_type != NULL);
4565 signature = sig_type->signature;
4566 }
4567 else
4568 {
4569 attr = dwarf2_attr (comp_unit_die, DW_AT_GNU_dwo_id, cu);
4570 if (! attr)
4571 error (_("Dwarf Error: missing dwo_id [in module %s]"),
4572 dwo_name);
4573 signature = DW_UNSND (attr);
4574 }
4575
4576 /* We may need the comp_dir in order to find the DWO file. */
42e7ad6c
DE
4577 comp_dir_string = NULL;
4578 if (comp_dir)
4579 comp_dir_string = DW_STRING (comp_dir);
3019eac3
DE
4580
4581 if (this_cu->is_debug_types)
42e7ad6c 4582 dwo_unit = lookup_dwo_type_unit (sig_type, dwo_name, comp_dir_string);
3019eac3 4583 else
42e7ad6c 4584 dwo_unit = lookup_dwo_comp_unit (this_cu, dwo_name, comp_dir_string,
3019eac3
DE
4585 signature);
4586
4587 if (dwo_unit == NULL)
4588 {
4589 error (_("Dwarf Error: CU at offset 0x%x references unknown DWO"
4590 " with ID %s [in module %s]"),
4591 this_cu->offset.sect_off,
4592 phex (signature, sizeof (signature)),
4593 objfile->name);
4594 }
4595
4596 /* Set up for reading the DWO CU/TU. */
4597 cu->dwo_unit = dwo_unit;
4598 section = dwo_unit->info_or_types_section;
80626a55 4599 dwarf2_read_section (objfile, section);
3019eac3 4600 begin_info_ptr = info_ptr = section->buffer + dwo_unit->offset.sect_off;
4bdcc0c1 4601 dwo_abbrev_section = &dwo_unit->dwo_file->sections.abbrev;
3019eac3
DE
4602 init_cu_die_reader (&reader, cu, section, dwo_unit->dwo_file);
4603
4604 if (this_cu->is_debug_types)
4605 {
4606 ULONGEST signature;
80626a55 4607 cu_offset type_offset_in_tu;
3019eac3 4608
4bdcc0c1
DE
4609 info_ptr = read_and_check_type_unit_head (&cu->header, section,
4610 dwo_abbrev_section,
4611 info_ptr,
80626a55
DE
4612 &signature,
4613 &type_offset_in_tu);
3019eac3
DE
4614 gdb_assert (sig_type->signature == signature);
4615 gdb_assert (dwo_unit->offset.sect_off == cu->header.offset.sect_off);
80626a55
DE
4616 /* For DWOs coming from DWP files, we don't know the CU length
4617 nor the type's offset in the TU until now. */
4618 dwo_unit->length = get_cu_length (&cu->header);
4619 dwo_unit->type_offset_in_tu = type_offset_in_tu;
3019eac3
DE
4620
4621 /* Establish the type offset that can be used to lookup the type.
4622 For DWO files, we don't know it until now. */
4623 sig_type->type_offset_in_section.sect_off =
4624 dwo_unit->offset.sect_off + dwo_unit->type_offset_in_tu.cu_off;
4625 }
4626 else
4627 {
4bdcc0c1
DE
4628 info_ptr = read_and_check_comp_unit_head (&cu->header, section,
4629 dwo_abbrev_section,
4630 info_ptr, 0);
3019eac3 4631 gdb_assert (dwo_unit->offset.sect_off == cu->header.offset.sect_off);
80626a55
DE
4632 /* For DWOs coming from DWP files, we don't know the CU length
4633 until now. */
4634 dwo_unit->length = get_cu_length (&cu->header);
3019eac3
DE
4635 }
4636
4637 /* Discard the original CU's abbrev table, and read the DWO's. */
f4dc4d17
DE
4638 if (abbrev_table == NULL)
4639 {
4640 dwarf2_free_abbrev_table (cu);
4641 dwarf2_read_abbrevs (cu, dwo_abbrev_section);
4642 }
4643 else
4644 {
4645 dwarf2_read_abbrevs (cu, dwo_abbrev_section);
4646 make_cleanup (dwarf2_free_abbrev_table, cu);
4647 }
3019eac3
DE
4648
4649 /* Read in the die, but leave space to copy over the attributes
4650 from the stub. This has the benefit of simplifying the rest of
4651 the code - all the real work is done here. */
4652 num_extra_attrs = ((stmt_list != NULL)
4653 + (low_pc != NULL)
4654 + (high_pc != NULL)
42e7ad6c
DE
4655 + (ranges != NULL)
4656 + (comp_dir != NULL));
3019eac3
DE
4657 info_ptr = read_full_die_1 (&reader, &comp_unit_die, info_ptr,
4658 &has_children, num_extra_attrs);
4659
4660 /* Copy over the attributes from the stub to the DWO die. */
4661 i = comp_unit_die->num_attrs;
4662 if (stmt_list != NULL)
4663 comp_unit_die->attrs[i++] = *stmt_list;
4664 if (low_pc != NULL)
4665 comp_unit_die->attrs[i++] = *low_pc;
4666 if (high_pc != NULL)
4667 comp_unit_die->attrs[i++] = *high_pc;
4668 if (ranges != NULL)
4669 comp_unit_die->attrs[i++] = *ranges;
42e7ad6c
DE
4670 if (comp_dir != NULL)
4671 comp_unit_die->attrs[i++] = *comp_dir;
3019eac3
DE
4672 comp_unit_die->num_attrs += num_extra_attrs;
4673
4674 /* Skip dummy compilation units. */
4675 if (info_ptr >= begin_info_ptr + dwo_unit->length
4676 || peek_abbrev_code (abfd, info_ptr) == 0)
4677 {
4678 do_cleanups (cleanups);
4679 return;
4680 }
4681 }
4682
dee91e82
DE
4683 die_reader_func (&reader, info_ptr, comp_unit_die, has_children, data);
4684
4685 if (free_cu_cleanup != NULL)
348e048f 4686 {
dee91e82
DE
4687 if (keep)
4688 {
4689 /* We've successfully allocated this compilation unit. Let our
4690 caller clean it up when finished with it. */
4691 discard_cleanups (free_cu_cleanup);
4692
4693 /* We can only discard free_cu_cleanup and all subsequent cleanups.
4694 So we have to manually free the abbrev table. */
4695 dwarf2_free_abbrev_table (cu);
4696
4697 /* Link this CU into read_in_chain. */
4698 this_cu->cu->read_in_chain = dwarf2_per_objfile->read_in_chain;
4699 dwarf2_per_objfile->read_in_chain = this_cu;
4700 }
4701 else
4702 do_cleanups (free_cu_cleanup);
348e048f 4703 }
dee91e82
DE
4704
4705 do_cleanups (cleanups);
4706}
4707
3019eac3
DE
4708/* Read CU/TU THIS_CU in section SECTION,
4709 but do not follow DW_AT_GNU_dwo_name if present.
80626a55
DE
4710 DWOP_FILE, if non-NULL, is the DWO/DWP file to read (the caller is assumed
4711 to have already done the lookup to find the DWO/DWP file).
dee91e82
DE
4712
4713 The caller is required to fill in THIS_CU->section, THIS_CU->offset, and
3019eac3 4714 THIS_CU->is_debug_types, but nothing else.
dee91e82
DE
4715
4716 We fill in THIS_CU->length.
4717
4718 WARNING: If THIS_CU is a "dummy CU" (used as filler by the incremental
4719 linker) then DIE_READER_FUNC will not get called.
4720
4721 THIS_CU->cu is always freed when done.
3019eac3
DE
4722 This is done in order to not leave THIS_CU->cu in a state where we have
4723 to care whether it refers to the "main" CU or the DWO CU. */
dee91e82
DE
4724
4725static void
4726init_cutu_and_read_dies_no_follow (struct dwarf2_per_cu_data *this_cu,
4727 struct dwarf2_section_info *abbrev_section,
3019eac3 4728 struct dwo_file *dwo_file,
dee91e82
DE
4729 die_reader_func_ftype *die_reader_func,
4730 void *data)
4731{
4732 struct objfile *objfile = dwarf2_per_objfile->objfile;
3019eac3
DE
4733 struct dwarf2_section_info *section = this_cu->info_or_types_section;
4734 bfd *abfd = section->asection->owner;
dee91e82
DE
4735 struct dwarf2_cu cu;
4736 gdb_byte *begin_info_ptr, *info_ptr;
4737 struct die_reader_specs reader;
4738 struct cleanup *cleanups;
4739 struct die_info *comp_unit_die;
4740 int has_children;
4741
09406207
DE
4742 if (dwarf2_die_debug)
4743 fprintf_unfiltered (gdb_stdlog, "Reading %s unit at offset 0x%x\n",
4744 this_cu->is_debug_types ? "type" : "comp",
4745 this_cu->offset.sect_off);
4746
dee91e82
DE
4747 gdb_assert (this_cu->cu == NULL);
4748
dee91e82
DE
4749 /* This is cheap if the section is already read in. */
4750 dwarf2_read_section (objfile, section);
4751
4752 init_one_comp_unit (&cu, this_cu);
4753
4754 cleanups = make_cleanup (free_stack_comp_unit, &cu);
4755
4756 begin_info_ptr = info_ptr = section->buffer + this_cu->offset.sect_off;
4bdcc0c1
DE
4757 info_ptr = read_and_check_comp_unit_head (&cu.header, section,
4758 abbrev_section, info_ptr,
3019eac3 4759 this_cu->is_debug_types);
dee91e82 4760
1ce1cefd 4761 this_cu->length = get_cu_length (&cu.header);
dee91e82
DE
4762
4763 /* Skip dummy compilation units. */
4764 if (info_ptr >= begin_info_ptr + this_cu->length
4765 || peek_abbrev_code (abfd, info_ptr) == 0)
c906108c 4766 {
dee91e82 4767 do_cleanups (cleanups);
21b2bd31 4768 return;
93311388 4769 }
72bf9492 4770
dee91e82
DE
4771 dwarf2_read_abbrevs (&cu, abbrev_section);
4772 make_cleanup (dwarf2_free_abbrev_table, &cu);
4773
3019eac3 4774 init_cu_die_reader (&reader, &cu, section, dwo_file);
dee91e82
DE
4775 info_ptr = read_full_die (&reader, &comp_unit_die, info_ptr, &has_children);
4776
4777 die_reader_func (&reader, info_ptr, comp_unit_die, has_children, data);
4778
4779 do_cleanups (cleanups);
4780}
4781
3019eac3
DE
4782/* Read a CU/TU, except that this does not look for DW_AT_GNU_dwo_name and
4783 does not lookup the specified DWO file.
4784 This cannot be used to read DWO files.
dee91e82
DE
4785
4786 THIS_CU->cu is always freed when done.
3019eac3
DE
4787 This is done in order to not leave THIS_CU->cu in a state where we have
4788 to care whether it refers to the "main" CU or the DWO CU.
4789 We can revisit this if the data shows there's a performance issue. */
dee91e82
DE
4790
4791static void
4792init_cutu_and_read_dies_simple (struct dwarf2_per_cu_data *this_cu,
4793 die_reader_func_ftype *die_reader_func,
4794 void *data)
4795{
4796 init_cutu_and_read_dies_no_follow (this_cu,
36586728 4797 get_abbrev_section_for_cu (this_cu),
3019eac3 4798 NULL,
dee91e82
DE
4799 die_reader_func, data);
4800}
4801
f4dc4d17
DE
4802/* Create a psymtab named NAME and assign it to PER_CU.
4803
4804 The caller must fill in the following details:
4805 dirname, textlow, texthigh. */
4806
4807static struct partial_symtab *
4808create_partial_symtab (struct dwarf2_per_cu_data *per_cu, const char *name)
4809{
4810 struct objfile *objfile = per_cu->objfile;
4811 struct partial_symtab *pst;
4812
4813 pst = start_psymtab_common (objfile, objfile->section_offsets,
4814 name, 0,
4815 objfile->global_psymbols.next,
4816 objfile->static_psymbols.next);
4817
4818 pst->psymtabs_addrmap_supported = 1;
4819
4820 /* This is the glue that links PST into GDB's symbol API. */
4821 pst->read_symtab_private = per_cu;
4822 pst->read_symtab = dwarf2_psymtab_to_symtab;
4823 per_cu->v.psymtab = pst;
4824
4825 return pst;
4826}
4827
dee91e82
DE
4828/* die_reader_func for process_psymtab_comp_unit. */
4829
4830static void
4831process_psymtab_comp_unit_reader (const struct die_reader_specs *reader,
4832 gdb_byte *info_ptr,
4833 struct die_info *comp_unit_die,
4834 int has_children,
4835 void *data)
4836{
4837 struct dwarf2_cu *cu = reader->cu;
4838 struct objfile *objfile = cu->objfile;
4839 struct dwarf2_per_cu_data *per_cu = cu->per_cu;
dee91e82
DE
4840 struct attribute *attr;
4841 CORE_ADDR baseaddr;
4842 CORE_ADDR best_lowpc = 0, best_highpc = 0;
4843 struct partial_symtab *pst;
4844 int has_pc_info;
4845 const char *filename;
95554aad 4846 int *want_partial_unit_ptr = data;
dee91e82 4847
95554aad
TT
4848 if (comp_unit_die->tag == DW_TAG_partial_unit
4849 && (want_partial_unit_ptr == NULL
4850 || !*want_partial_unit_ptr))
dee91e82
DE
4851 return;
4852
f4dc4d17
DE
4853 gdb_assert (! per_cu->is_debug_types);
4854
95554aad 4855 prepare_one_comp_unit (cu, comp_unit_die, language_minimal);
dee91e82
DE
4856
4857 cu->list_in_scope = &file_symbols;
c906108c 4858
93311388 4859 /* Allocate a new partial symbol table structure. */
dee91e82 4860 attr = dwarf2_attr (comp_unit_die, DW_AT_name, cu);
3e2a0cee
TT
4861 if (attr == NULL || !DW_STRING (attr))
4862 filename = "";
4863 else
4864 filename = DW_STRING (attr);
72bf9492 4865
f4dc4d17
DE
4866 pst = create_partial_symtab (per_cu, filename);
4867
4868 /* This must be done before calling dwarf2_build_include_psymtabs. */
dee91e82 4869 attr = dwarf2_attr (comp_unit_die, DW_AT_comp_dir, cu);
d85a05f0
DJ
4870 if (attr != NULL)
4871 pst->dirname = DW_STRING (attr);
72bf9492 4872
93311388 4873 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
e7c27a73 4874
dee91e82 4875 dwarf2_find_base_address (comp_unit_die, cu);
d85a05f0 4876
93311388
DE
4877 /* Possibly set the default values of LOWPC and HIGHPC from
4878 `DW_AT_ranges'. */
d85a05f0 4879 has_pc_info = dwarf2_get_pc_bounds (comp_unit_die, &best_lowpc,
dee91e82 4880 &best_highpc, cu, pst);
d85a05f0 4881 if (has_pc_info == 1 && best_lowpc < best_highpc)
93311388
DE
4882 /* Store the contiguous range if it is not empty; it can be empty for
4883 CUs with no code. */
4884 addrmap_set_empty (objfile->psymtabs_addrmap,
d85a05f0
DJ
4885 best_lowpc + baseaddr,
4886 best_highpc + baseaddr - 1, pst);
93311388
DE
4887
4888 /* Check if comp unit has_children.
4889 If so, read the rest of the partial symbols from this comp unit.
0963b4bd 4890 If not, there's no more debug_info for this comp unit. */
d85a05f0 4891 if (has_children)
93311388
DE
4892 {
4893 struct partial_die_info *first_die;
4894 CORE_ADDR lowpc, highpc;
31ffec48 4895
93311388
DE
4896 lowpc = ((CORE_ADDR) -1);
4897 highpc = ((CORE_ADDR) 0);
c906108c 4898
dee91e82 4899 first_die = load_partial_dies (reader, info_ptr, 1);
c906108c 4900
93311388 4901 scan_partial_symbols (first_die, &lowpc, &highpc,
dee91e82 4902 ! has_pc_info, cu);
57c22c6c 4903
93311388
DE
4904 /* If we didn't find a lowpc, set it to highpc to avoid
4905 complaints from `maint check'. */
4906 if (lowpc == ((CORE_ADDR) -1))
4907 lowpc = highpc;
10b3939b 4908
93311388
DE
4909 /* If the compilation unit didn't have an explicit address range,
4910 then use the information extracted from its child dies. */
d85a05f0 4911 if (! has_pc_info)
93311388 4912 {
d85a05f0
DJ
4913 best_lowpc = lowpc;
4914 best_highpc = highpc;
93311388
DE
4915 }
4916 }
d85a05f0
DJ
4917 pst->textlow = best_lowpc + baseaddr;
4918 pst->texthigh = best_highpc + baseaddr;
c906108c 4919
93311388
DE
4920 pst->n_global_syms = objfile->global_psymbols.next -
4921 (objfile->global_psymbols.list + pst->globals_offset);
4922 pst->n_static_syms = objfile->static_psymbols.next -
4923 (objfile->static_psymbols.list + pst->statics_offset);
4924 sort_pst_symbols (pst);
c906108c 4925
f4dc4d17 4926 if (!VEC_empty (dwarf2_per_cu_ptr, cu->per_cu->s.imported_symtabs))
95554aad
TT
4927 {
4928 int i;
f4dc4d17 4929 int len = VEC_length (dwarf2_per_cu_ptr, cu->per_cu->s.imported_symtabs);
95554aad
TT
4930 struct dwarf2_per_cu_data *iter;
4931
4932 /* Fill in 'dependencies' here; we fill in 'users' in a
4933 post-pass. */
4934 pst->number_of_dependencies = len;
4935 pst->dependencies = obstack_alloc (&objfile->objfile_obstack,
4936 len * sizeof (struct symtab *));
4937 for (i = 0;
f4dc4d17 4938 VEC_iterate (dwarf2_per_cu_ptr, cu->per_cu->s.imported_symtabs,
95554aad
TT
4939 i, iter);
4940 ++i)
4941 pst->dependencies[i] = iter->v.psymtab;
4942
f4dc4d17 4943 VEC_free (dwarf2_per_cu_ptr, cu->per_cu->s.imported_symtabs);
95554aad
TT
4944 }
4945
f4dc4d17
DE
4946 /* Get the list of files included in the current compilation unit,
4947 and build a psymtab for each of them. */
4948 dwarf2_build_include_psymtabs (cu, comp_unit_die, pst);
09406207
DE
4949
4950 if (dwarf2_read_debug)
4951 {
4952 struct gdbarch *gdbarch = get_objfile_arch (objfile);
4953
4954 fprintf_unfiltered (gdb_stdlog,
844226d6 4955 "Psymtab for %s unit @0x%x: %s - %s"
09406207
DE
4956 ", %d global, %d static syms\n",
4957 per_cu->is_debug_types ? "type" : "comp",
4958 per_cu->offset.sect_off,
4959 paddress (gdbarch, pst->textlow),
4960 paddress (gdbarch, pst->texthigh),
4961 pst->n_global_syms, pst->n_static_syms);
4962 }
dee91e82 4963}
ae038cb0 4964
dee91e82
DE
4965/* Subroutine of dwarf2_build_psymtabs_hard to simplify it.
4966 Process compilation unit THIS_CU for a psymtab. */
4967
4968static void
95554aad
TT
4969process_psymtab_comp_unit (struct dwarf2_per_cu_data *this_cu,
4970 int want_partial_unit)
dee91e82
DE
4971{
4972 /* If this compilation unit was already read in, free the
4973 cached copy in order to read it in again. This is
4974 necessary because we skipped some symbols when we first
4975 read in the compilation unit (see load_partial_dies).
4976 This problem could be avoided, but the benefit is unclear. */
4977 if (this_cu->cu != NULL)
4978 free_one_cached_comp_unit (this_cu);
4979
3019eac3 4980 gdb_assert (! this_cu->is_debug_types);
f4dc4d17
DE
4981 init_cutu_and_read_dies (this_cu, NULL, 0, 0,
4982 process_psymtab_comp_unit_reader,
95554aad 4983 &want_partial_unit);
dee91e82
DE
4984
4985 /* Age out any secondary CUs. */
4986 age_cached_comp_units ();
93311388 4987}
ff013f42 4988
f4dc4d17
DE
4989static hashval_t
4990hash_type_unit_group (const void *item)
4991{
094b34ac 4992 const struct type_unit_group *tu_group = item;
f4dc4d17 4993
094b34ac 4994 return hash_stmt_list_entry (&tu_group->hash);
f4dc4d17 4995}
348e048f
DE
4996
4997static int
f4dc4d17 4998eq_type_unit_group (const void *item_lhs, const void *item_rhs)
348e048f 4999{
f4dc4d17
DE
5000 const struct type_unit_group *lhs = item_lhs;
5001 const struct type_unit_group *rhs = item_rhs;
348e048f 5002
094b34ac 5003 return eq_stmt_list_entry (&lhs->hash, &rhs->hash);
f4dc4d17 5004}
348e048f 5005
f4dc4d17
DE
5006/* Allocate a hash table for type unit groups. */
5007
5008static htab_t
5009allocate_type_unit_groups_table (void)
5010{
5011 return htab_create_alloc_ex (3,
5012 hash_type_unit_group,
5013 eq_type_unit_group,
5014 NULL,
5015 &dwarf2_per_objfile->objfile->objfile_obstack,
5016 hashtab_obstack_allocate,
5017 dummy_obstack_deallocate);
5018}
dee91e82 5019
f4dc4d17
DE
5020/* Type units that don't have DW_AT_stmt_list are grouped into their own
5021 partial symtabs. We combine several TUs per psymtab to not let the size
5022 of any one psymtab grow too big. */
5023#define NO_STMT_LIST_TYPE_UNIT_PSYMTAB (1 << 31)
5024#define NO_STMT_LIST_TYPE_UNIT_PSYMTAB_SIZE 10
dee91e82 5025
094b34ac 5026/* Helper routine for get_type_unit_group.
f4dc4d17
DE
5027 Create the type_unit_group object used to hold one or more TUs. */
5028
5029static struct type_unit_group *
094b34ac 5030create_type_unit_group (struct dwarf2_cu *cu, sect_offset line_offset_struct)
f4dc4d17
DE
5031{
5032 struct objfile *objfile = dwarf2_per_objfile->objfile;
094b34ac 5033 struct dwarf2_per_cu_data *per_cu;
f4dc4d17 5034 struct type_unit_group *tu_group;
f4dc4d17
DE
5035
5036 tu_group = OBSTACK_ZALLOC (&objfile->objfile_obstack,
5037 struct type_unit_group);
094b34ac 5038 per_cu = &tu_group->per_cu;
f4dc4d17
DE
5039 per_cu->objfile = objfile;
5040 per_cu->is_debug_types = 1;
5041 per_cu->s.type_unit_group = tu_group;
5042
094b34ac
DE
5043 if (dwarf2_per_objfile->using_index)
5044 {
5045 per_cu->v.quick = OBSTACK_ZALLOC (&objfile->objfile_obstack,
5046 struct dwarf2_per_cu_quick_data);
5047 tu_group->t.first_tu = cu->per_cu;
5048 }
5049 else
5050 {
5051 unsigned int line_offset = line_offset_struct.sect_off;
5052 struct partial_symtab *pst;
5053 char *name;
5054
5055 /* Give the symtab a useful name for debug purposes. */
5056 if ((line_offset & NO_STMT_LIST_TYPE_UNIT_PSYMTAB) != 0)
5057 name = xstrprintf ("<type_units_%d>",
5058 (line_offset & ~NO_STMT_LIST_TYPE_UNIT_PSYMTAB));
5059 else
5060 name = xstrprintf ("<type_units_at_0x%x>", line_offset);
5061
5062 pst = create_partial_symtab (per_cu, name);
5063 pst->anonymous = 1;
f4dc4d17 5064
094b34ac
DE
5065 xfree (name);
5066 }
f4dc4d17 5067
094b34ac
DE
5068 tu_group->hash.dwo_unit = cu->dwo_unit;
5069 tu_group->hash.line_offset = line_offset_struct;
f4dc4d17
DE
5070
5071 return tu_group;
5072}
5073
094b34ac
DE
5074/* Look up the type_unit_group for type unit CU, and create it if necessary.
5075 STMT_LIST is a DW_AT_stmt_list attribute. */
f4dc4d17
DE
5076
5077static struct type_unit_group *
094b34ac 5078get_type_unit_group (struct dwarf2_cu *cu, struct attribute *stmt_list)
f4dc4d17
DE
5079{
5080 struct tu_stats *tu_stats = &dwarf2_per_objfile->tu_stats;
5081 struct type_unit_group *tu_group;
5082 void **slot;
5083 unsigned int line_offset;
5084 struct type_unit_group type_unit_group_for_lookup;
5085
5086 if (dwarf2_per_objfile->type_unit_groups == NULL)
5087 {
5088 dwarf2_per_objfile->type_unit_groups =
5089 allocate_type_unit_groups_table ();
5090 }
5091
5092 /* Do we need to create a new group, or can we use an existing one? */
5093
5094 if (stmt_list)
5095 {
5096 line_offset = DW_UNSND (stmt_list);
5097 ++tu_stats->nr_symtab_sharers;
5098 }
5099 else
5100 {
5101 /* Ugh, no stmt_list. Rare, but we have to handle it.
5102 We can do various things here like create one group per TU or
5103 spread them over multiple groups to split up the expansion work.
5104 To avoid worst case scenarios (too many groups or too large groups)
5105 we, umm, group them in bunches. */
5106 line_offset = (NO_STMT_LIST_TYPE_UNIT_PSYMTAB
5107 | (tu_stats->nr_stmt_less_type_units
5108 / NO_STMT_LIST_TYPE_UNIT_PSYMTAB_SIZE));
5109 ++tu_stats->nr_stmt_less_type_units;
5110 }
5111
094b34ac
DE
5112 type_unit_group_for_lookup.hash.dwo_unit = cu->dwo_unit;
5113 type_unit_group_for_lookup.hash.line_offset.sect_off = line_offset;
f4dc4d17
DE
5114 slot = htab_find_slot (dwarf2_per_objfile->type_unit_groups,
5115 &type_unit_group_for_lookup, INSERT);
5116 if (*slot != NULL)
5117 {
5118 tu_group = *slot;
5119 gdb_assert (tu_group != NULL);
5120 }
5121 else
5122 {
5123 sect_offset line_offset_struct;
5124
5125 line_offset_struct.sect_off = line_offset;
094b34ac 5126 tu_group = create_type_unit_group (cu, line_offset_struct);
f4dc4d17
DE
5127 *slot = tu_group;
5128 ++tu_stats->nr_symtabs;
5129 }
5130
5131 return tu_group;
5132}
5133
5134/* Struct used to sort TUs by their abbreviation table offset. */
5135
5136struct tu_abbrev_offset
5137{
5138 struct signatured_type *sig_type;
5139 sect_offset abbrev_offset;
5140};
5141
5142/* Helper routine for build_type_unit_groups, passed to qsort. */
5143
5144static int
5145sort_tu_by_abbrev_offset (const void *ap, const void *bp)
5146{
5147 const struct tu_abbrev_offset * const *a = ap;
5148 const struct tu_abbrev_offset * const *b = bp;
5149 unsigned int aoff = (*a)->abbrev_offset.sect_off;
5150 unsigned int boff = (*b)->abbrev_offset.sect_off;
5151
5152 return (aoff > boff) - (aoff < boff);
5153}
5154
5155/* A helper function to add a type_unit_group to a table. */
5156
5157static int
5158add_type_unit_group_to_table (void **slot, void *datum)
5159{
5160 struct type_unit_group *tu_group = *slot;
5161 struct type_unit_group ***datap = datum;
5162
5163 **datap = tu_group;
5164 ++*datap;
5165
5166 return 1;
5167}
5168
5169/* Efficiently read all the type units, calling init_cutu_and_read_dies on
5170 each one passing FUNC,DATA.
5171
5172 The efficiency is because we sort TUs by the abbrev table they use and
5173 only read each abbrev table once. In one program there are 200K TUs
5174 sharing 8K abbrev tables.
5175
5176 The main purpose of this function is to support building the
5177 dwarf2_per_objfile->type_unit_groups table.
5178 TUs typically share the DW_AT_stmt_list of the CU they came from, so we
5179 can collapse the search space by grouping them by stmt_list.
5180 The savings can be significant, in the same program from above the 200K TUs
5181 share 8K stmt_list tables.
5182
5183 FUNC is expected to call get_type_unit_group, which will create the
5184 struct type_unit_group if necessary and add it to
5185 dwarf2_per_objfile->type_unit_groups. */
5186
5187static void
5188build_type_unit_groups (die_reader_func_ftype *func, void *data)
5189{
5190 struct objfile *objfile = dwarf2_per_objfile->objfile;
5191 struct tu_stats *tu_stats = &dwarf2_per_objfile->tu_stats;
5192 struct cleanup *cleanups;
5193 struct abbrev_table *abbrev_table;
5194 sect_offset abbrev_offset;
5195 struct tu_abbrev_offset *sorted_by_abbrev;
5196 struct type_unit_group **iter;
5197 int i;
5198
5199 /* It's up to the caller to not call us multiple times. */
5200 gdb_assert (dwarf2_per_objfile->type_unit_groups == NULL);
5201
5202 if (dwarf2_per_objfile->n_type_units == 0)
5203 return;
5204
5205 /* TUs typically share abbrev tables, and there can be way more TUs than
5206 abbrev tables. Sort by abbrev table to reduce the number of times we
5207 read each abbrev table in.
5208 Alternatives are to punt or to maintain a cache of abbrev tables.
5209 This is simpler and efficient enough for now.
5210
5211 Later we group TUs by their DW_AT_stmt_list value (as this defines the
5212 symtab to use). Typically TUs with the same abbrev offset have the same
5213 stmt_list value too so in practice this should work well.
5214
5215 The basic algorithm here is:
5216
5217 sort TUs by abbrev table
5218 for each TU with same abbrev table:
5219 read abbrev table if first user
5220 read TU top level DIE
5221 [IWBN if DWO skeletons had DW_AT_stmt_list]
5222 call FUNC */
5223
5224 if (dwarf2_read_debug)
5225 fprintf_unfiltered (gdb_stdlog, "Building type unit groups ...\n");
5226
5227 /* Sort in a separate table to maintain the order of all_type_units
5228 for .gdb_index: TU indices directly index all_type_units. */
5229 sorted_by_abbrev = XNEWVEC (struct tu_abbrev_offset,
5230 dwarf2_per_objfile->n_type_units);
5231 for (i = 0; i < dwarf2_per_objfile->n_type_units; ++i)
5232 {
5233 struct signatured_type *sig_type = dwarf2_per_objfile->all_type_units[i];
5234
5235 sorted_by_abbrev[i].sig_type = sig_type;
5236 sorted_by_abbrev[i].abbrev_offset =
5237 read_abbrev_offset (sig_type->per_cu.info_or_types_section,
5238 sig_type->per_cu.offset);
5239 }
5240 cleanups = make_cleanup (xfree, sorted_by_abbrev);
5241 qsort (sorted_by_abbrev, dwarf2_per_objfile->n_type_units,
5242 sizeof (struct tu_abbrev_offset), sort_tu_by_abbrev_offset);
5243
094b34ac
DE
5244 /* Note: In the .gdb_index case, get_type_unit_group may have already been
5245 called any number of times, so we don't reset tu_stats here. */
5246
f4dc4d17
DE
5247 abbrev_offset.sect_off = ~(unsigned) 0;
5248 abbrev_table = NULL;
5249 make_cleanup (abbrev_table_free_cleanup, &abbrev_table);
5250
5251 for (i = 0; i < dwarf2_per_objfile->n_type_units; ++i)
5252 {
5253 const struct tu_abbrev_offset *tu = &sorted_by_abbrev[i];
5254
5255 /* Switch to the next abbrev table if necessary. */
5256 if (abbrev_table == NULL
5257 || tu->abbrev_offset.sect_off != abbrev_offset.sect_off)
5258 {
5259 if (abbrev_table != NULL)
5260 {
5261 abbrev_table_free (abbrev_table);
5262 /* Reset to NULL in case abbrev_table_read_table throws
5263 an error: abbrev_table_free_cleanup will get called. */
5264 abbrev_table = NULL;
5265 }
5266 abbrev_offset = tu->abbrev_offset;
5267 abbrev_table =
5268 abbrev_table_read_table (&dwarf2_per_objfile->abbrev,
5269 abbrev_offset);
5270 ++tu_stats->nr_uniq_abbrev_tables;
5271 }
5272
5273 init_cutu_and_read_dies (&tu->sig_type->per_cu, abbrev_table, 0, 0,
5274 func, data);
5275 }
5276
5277 /* Create a vector of pointers to primary type units to make it easy to
5278 iterate over them and CUs. See dw2_get_primary_cu. */
5279 dwarf2_per_objfile->n_type_unit_groups =
5280 htab_elements (dwarf2_per_objfile->type_unit_groups);
5281 dwarf2_per_objfile->all_type_unit_groups =
5282 obstack_alloc (&objfile->objfile_obstack,
5283 dwarf2_per_objfile->n_type_unit_groups
5284 * sizeof (struct type_unit_group *));
5285 iter = &dwarf2_per_objfile->all_type_unit_groups[0];
5286 htab_traverse_noresize (dwarf2_per_objfile->type_unit_groups,
5287 add_type_unit_group_to_table, &iter);
5288 gdb_assert (iter - &dwarf2_per_objfile->all_type_unit_groups[0]
5289 == dwarf2_per_objfile->n_type_unit_groups);
5290
5291 do_cleanups (cleanups);
5292
5293 if (dwarf2_read_debug)
5294 {
5295 fprintf_unfiltered (gdb_stdlog, "Done building type unit groups:\n");
5296 fprintf_unfiltered (gdb_stdlog, " %d TUs\n",
5297 dwarf2_per_objfile->n_type_units);
5298 fprintf_unfiltered (gdb_stdlog, " %d uniq abbrev tables\n",
5299 tu_stats->nr_uniq_abbrev_tables);
5300 fprintf_unfiltered (gdb_stdlog, " %d symtabs from stmt_list entries\n",
5301 tu_stats->nr_symtabs);
5302 fprintf_unfiltered (gdb_stdlog, " %d symtab sharers\n",
5303 tu_stats->nr_symtab_sharers);
5304 fprintf_unfiltered (gdb_stdlog, " %d type units without a stmt_list\n",
5305 tu_stats->nr_stmt_less_type_units);
5306 }
5307}
5308
5309/* Reader function for build_type_psymtabs. */
5310
5311static void
5312build_type_psymtabs_reader (const struct die_reader_specs *reader,
5313 gdb_byte *info_ptr,
5314 struct die_info *type_unit_die,
5315 int has_children,
5316 void *data)
5317{
5318 struct objfile *objfile = dwarf2_per_objfile->objfile;
5319 struct dwarf2_cu *cu = reader->cu;
5320 struct dwarf2_per_cu_data *per_cu = cu->per_cu;
5321 struct type_unit_group *tu_group;
5322 struct attribute *attr;
5323 struct partial_die_info *first_die;
5324 CORE_ADDR lowpc, highpc;
5325 struct partial_symtab *pst;
5326
5327 gdb_assert (data == NULL);
5328
5329 if (! has_children)
5330 return;
5331
5332 attr = dwarf2_attr_no_follow (type_unit_die, DW_AT_stmt_list);
094b34ac 5333 tu_group = get_type_unit_group (cu, attr);
f4dc4d17 5334
094b34ac 5335 VEC_safe_push (dwarf2_per_cu_ptr, tu_group->t.tus, per_cu);
f4dc4d17
DE
5336
5337 prepare_one_comp_unit (cu, type_unit_die, language_minimal);
5338 cu->list_in_scope = &file_symbols;
5339 pst = create_partial_symtab (per_cu, "");
5340 pst->anonymous = 1;
5341
5342 first_die = load_partial_dies (reader, info_ptr, 1);
5343
5344 lowpc = (CORE_ADDR) -1;
5345 highpc = (CORE_ADDR) 0;
5346 scan_partial_symbols (first_die, &lowpc, &highpc, 0, cu);
5347
5348 pst->n_global_syms = objfile->global_psymbols.next -
5349 (objfile->global_psymbols.list + pst->globals_offset);
5350 pst->n_static_syms = objfile->static_psymbols.next -
5351 (objfile->static_psymbols.list + pst->statics_offset);
5352 sort_pst_symbols (pst);
5353}
5354
5355/* Traversal function for build_type_psymtabs. */
5356
5357static int
5358build_type_psymtab_dependencies (void **slot, void *info)
5359{
5360 struct objfile *objfile = dwarf2_per_objfile->objfile;
5361 struct type_unit_group *tu_group = (struct type_unit_group *) *slot;
094b34ac 5362 struct dwarf2_per_cu_data *per_cu = &tu_group->per_cu;
f4dc4d17 5363 struct partial_symtab *pst = per_cu->v.psymtab;
094b34ac 5364 int len = VEC_length (dwarf2_per_cu_ptr, tu_group->t.tus);
f4dc4d17
DE
5365 struct dwarf2_per_cu_data *iter;
5366 int i;
5367
5368 gdb_assert (len > 0);
5369
5370 pst->number_of_dependencies = len;
5371 pst->dependencies = obstack_alloc (&objfile->objfile_obstack,
5372 len * sizeof (struct psymtab *));
5373 for (i = 0;
094b34ac 5374 VEC_iterate (dwarf2_per_cu_ptr, tu_group->t.tus, i, iter);
f4dc4d17
DE
5375 ++i)
5376 {
5377 pst->dependencies[i] = iter->v.psymtab;
5378 iter->s.type_unit_group = tu_group;
5379 }
5380
094b34ac 5381 VEC_free (dwarf2_per_cu_ptr, tu_group->t.tus);
348e048f
DE
5382
5383 return 1;
5384}
5385
5386/* Subroutine of dwarf2_build_psymtabs_hard to simplify it.
5387 Build partial symbol tables for the .debug_types comp-units. */
5388
5389static void
5390build_type_psymtabs (struct objfile *objfile)
5391{
0e50663e 5392 if (! create_all_type_units (objfile))
348e048f
DE
5393 return;
5394
f4dc4d17
DE
5395 build_type_unit_groups (build_type_psymtabs_reader, NULL);
5396
5397 /* Now that all TUs have been processed we can fill in the dependencies. */
5398 htab_traverse_noresize (dwarf2_per_objfile->type_unit_groups,
5399 build_type_psymtab_dependencies, NULL);
348e048f
DE
5400}
5401
60606b2c
TT
5402/* A cleanup function that clears objfile's psymtabs_addrmap field. */
5403
5404static void
5405psymtabs_addrmap_cleanup (void *o)
5406{
5407 struct objfile *objfile = o;
ec61707d 5408
60606b2c
TT
5409 objfile->psymtabs_addrmap = NULL;
5410}
5411
95554aad
TT
5412/* Compute the 'user' field for each psymtab in OBJFILE. */
5413
5414static void
5415set_partial_user (struct objfile *objfile)
5416{
5417 int i;
5418
5419 for (i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
5420 {
5421 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
5422 struct partial_symtab *pst = per_cu->v.psymtab;
5423 int j;
5424
36586728
TT
5425 if (pst == NULL)
5426 continue;
5427
95554aad
TT
5428 for (j = 0; j < pst->number_of_dependencies; ++j)
5429 {
5430 /* Set the 'user' field only if it is not already set. */
5431 if (pst->dependencies[j]->user == NULL)
5432 pst->dependencies[j]->user = pst;
5433 }
5434 }
5435}
5436
93311388
DE
5437/* Build the partial symbol table by doing a quick pass through the
5438 .debug_info and .debug_abbrev sections. */
72bf9492 5439
93311388 5440static void
c67a9c90 5441dwarf2_build_psymtabs_hard (struct objfile *objfile)
93311388 5442{
60606b2c
TT
5443 struct cleanup *back_to, *addrmap_cleanup;
5444 struct obstack temp_obstack;
21b2bd31 5445 int i;
93311388 5446
45cfd468
DE
5447 if (dwarf2_read_debug)
5448 {
5449 fprintf_unfiltered (gdb_stdlog, "Building psymtabs of objfile %s ...\n",
5450 objfile->name);
5451 }
5452
98bfdba5
PA
5453 dwarf2_per_objfile->reading_partial_symbols = 1;
5454
be391dca 5455 dwarf2_read_section (objfile, &dwarf2_per_objfile->info);
91c24f0a 5456
93311388
DE
5457 /* Any cached compilation units will be linked by the per-objfile
5458 read_in_chain. Make sure to free them when we're done. */
5459 back_to = make_cleanup (free_cached_comp_units, NULL);
72bf9492 5460
348e048f
DE
5461 build_type_psymtabs (objfile);
5462
93311388 5463 create_all_comp_units (objfile);
c906108c 5464
60606b2c
TT
5465 /* Create a temporary address map on a temporary obstack. We later
5466 copy this to the final obstack. */
5467 obstack_init (&temp_obstack);
5468 make_cleanup_obstack_free (&temp_obstack);
5469 objfile->psymtabs_addrmap = addrmap_create_mutable (&temp_obstack);
5470 addrmap_cleanup = make_cleanup (psymtabs_addrmap_cleanup, objfile);
72bf9492 5471
21b2bd31 5472 for (i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
93311388 5473 {
21b2bd31 5474 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
aaa75496 5475
95554aad 5476 process_psymtab_comp_unit (per_cu, 0);
c906108c 5477 }
ff013f42 5478
95554aad
TT
5479 set_partial_user (objfile);
5480
ff013f42
JK
5481 objfile->psymtabs_addrmap = addrmap_create_fixed (objfile->psymtabs_addrmap,
5482 &objfile->objfile_obstack);
60606b2c 5483 discard_cleanups (addrmap_cleanup);
ff013f42 5484
ae038cb0 5485 do_cleanups (back_to);
45cfd468
DE
5486
5487 if (dwarf2_read_debug)
5488 fprintf_unfiltered (gdb_stdlog, "Done building psymtabs of %s\n",
5489 objfile->name);
ae038cb0
DJ
5490}
5491
3019eac3 5492/* die_reader_func for load_partial_comp_unit. */
ae038cb0
DJ
5493
5494static void
dee91e82
DE
5495load_partial_comp_unit_reader (const struct die_reader_specs *reader,
5496 gdb_byte *info_ptr,
5497 struct die_info *comp_unit_die,
5498 int has_children,
5499 void *data)
ae038cb0 5500{
dee91e82 5501 struct dwarf2_cu *cu = reader->cu;
ae038cb0 5502
95554aad 5503 prepare_one_comp_unit (cu, comp_unit_die, language_minimal);
ae038cb0 5504
ae038cb0
DJ
5505 /* Check if comp unit has_children.
5506 If so, read the rest of the partial symbols from this comp unit.
0963b4bd 5507 If not, there's no more debug_info for this comp unit. */
d85a05f0 5508 if (has_children)
dee91e82
DE
5509 load_partial_dies (reader, info_ptr, 0);
5510}
98bfdba5 5511
dee91e82
DE
5512/* Load the partial DIEs for a secondary CU into memory.
5513 This is also used when rereading a primary CU with load_all_dies. */
c5b7e1cb 5514
dee91e82
DE
5515static void
5516load_partial_comp_unit (struct dwarf2_per_cu_data *this_cu)
5517{
f4dc4d17
DE
5518 init_cutu_and_read_dies (this_cu, NULL, 1, 1,
5519 load_partial_comp_unit_reader, NULL);
ae038cb0
DJ
5520}
5521
ae038cb0 5522static void
36586728
TT
5523read_comp_units_from_section (struct objfile *objfile,
5524 struct dwarf2_section_info *section,
5525 unsigned int is_dwz,
5526 int *n_allocated,
5527 int *n_comp_units,
5528 struct dwarf2_per_cu_data ***all_comp_units)
ae038cb0 5529{
be391dca 5530 gdb_byte *info_ptr;
36586728 5531 bfd *abfd = section->asection->owner;
be391dca 5532
36586728 5533 dwarf2_read_section (objfile, section);
ae038cb0 5534
36586728 5535 info_ptr = section->buffer;
6e70227d 5536
36586728 5537 while (info_ptr < section->buffer + section->size)
ae038cb0 5538 {
c764a876 5539 unsigned int length, initial_length_size;
ae038cb0 5540 struct dwarf2_per_cu_data *this_cu;
b64f50a1 5541 sect_offset offset;
ae038cb0 5542
36586728 5543 offset.sect_off = info_ptr - section->buffer;
ae038cb0
DJ
5544
5545 /* Read just enough information to find out where the next
5546 compilation unit is. */
36586728 5547 length = read_initial_length (abfd, info_ptr, &initial_length_size);
ae038cb0
DJ
5548
5549 /* Save the compilation unit for later lookup. */
5550 this_cu = obstack_alloc (&objfile->objfile_obstack,
5551 sizeof (struct dwarf2_per_cu_data));
5552 memset (this_cu, 0, sizeof (*this_cu));
5553 this_cu->offset = offset;
c764a876 5554 this_cu->length = length + initial_length_size;
36586728 5555 this_cu->is_dwz = is_dwz;
9291a0cd 5556 this_cu->objfile = objfile;
36586728 5557 this_cu->info_or_types_section = section;
ae038cb0 5558
36586728 5559 if (*n_comp_units == *n_allocated)
ae038cb0 5560 {
36586728
TT
5561 *n_allocated *= 2;
5562 *all_comp_units = xrealloc (*all_comp_units,
5563 *n_allocated
5564 * sizeof (struct dwarf2_per_cu_data *));
ae038cb0 5565 }
36586728
TT
5566 (*all_comp_units)[*n_comp_units] = this_cu;
5567 ++*n_comp_units;
ae038cb0
DJ
5568
5569 info_ptr = info_ptr + this_cu->length;
5570 }
36586728
TT
5571}
5572
5573/* Create a list of all compilation units in OBJFILE.
5574 This is only done for -readnow and building partial symtabs. */
5575
5576static void
5577create_all_comp_units (struct objfile *objfile)
5578{
5579 int n_allocated;
5580 int n_comp_units;
5581 struct dwarf2_per_cu_data **all_comp_units;
5582
5583 n_comp_units = 0;
5584 n_allocated = 10;
5585 all_comp_units = xmalloc (n_allocated
5586 * sizeof (struct dwarf2_per_cu_data *));
5587
5588 read_comp_units_from_section (objfile, &dwarf2_per_objfile->info, 0,
5589 &n_allocated, &n_comp_units, &all_comp_units);
5590
5591 if (bfd_get_section_by_name (objfile->obfd, ".gnu_debugaltlink") != NULL)
5592 {
5593 struct dwz_file *dwz = dwarf2_get_dwz_file ();
5594
5595 read_comp_units_from_section (objfile, &dwz->info, 1,
5596 &n_allocated, &n_comp_units,
5597 &all_comp_units);
5598 }
ae038cb0
DJ
5599
5600 dwarf2_per_objfile->all_comp_units
5601 = obstack_alloc (&objfile->objfile_obstack,
5602 n_comp_units * sizeof (struct dwarf2_per_cu_data *));
5603 memcpy (dwarf2_per_objfile->all_comp_units, all_comp_units,
5604 n_comp_units * sizeof (struct dwarf2_per_cu_data *));
5605 xfree (all_comp_units);
5606 dwarf2_per_objfile->n_comp_units = n_comp_units;
c906108c
SS
5607}
5608
5734ee8b
DJ
5609/* Process all loaded DIEs for compilation unit CU, starting at
5610 FIRST_DIE. The caller should pass NEED_PC == 1 if the compilation
5611 unit DIE did not have PC info (DW_AT_low_pc and DW_AT_high_pc, or
5612 DW_AT_ranges). If NEED_PC is set, then this function will set
5613 *LOWPC and *HIGHPC to the lowest and highest PC values found in CU
5614 and record the covered ranges in the addrmap. */
c906108c 5615
72bf9492
DJ
5616static void
5617scan_partial_symbols (struct partial_die_info *first_die, CORE_ADDR *lowpc,
5734ee8b 5618 CORE_ADDR *highpc, int need_pc, struct dwarf2_cu *cu)
c906108c 5619{
72bf9492 5620 struct partial_die_info *pdi;
c906108c 5621
91c24f0a
DC
5622 /* Now, march along the PDI's, descending into ones which have
5623 interesting children but skipping the children of the other ones,
5624 until we reach the end of the compilation unit. */
c906108c 5625
72bf9492 5626 pdi = first_die;
91c24f0a 5627
72bf9492
DJ
5628 while (pdi != NULL)
5629 {
5630 fixup_partial_die (pdi, cu);
c906108c 5631
f55ee35c 5632 /* Anonymous namespaces or modules have no name but have interesting
91c24f0a
DC
5633 children, so we need to look at them. Ditto for anonymous
5634 enums. */
933c6fe4 5635
72bf9492 5636 if (pdi->name != NULL || pdi->tag == DW_TAG_namespace
95554aad
TT
5637 || pdi->tag == DW_TAG_module || pdi->tag == DW_TAG_enumeration_type
5638 || pdi->tag == DW_TAG_imported_unit)
c906108c 5639 {
72bf9492 5640 switch (pdi->tag)
c906108c
SS
5641 {
5642 case DW_TAG_subprogram:
5734ee8b 5643 add_partial_subprogram (pdi, lowpc, highpc, need_pc, cu);
c906108c 5644 break;
72929c62 5645 case DW_TAG_constant:
c906108c
SS
5646 case DW_TAG_variable:
5647 case DW_TAG_typedef:
91c24f0a 5648 case DW_TAG_union_type:
72bf9492 5649 if (!pdi->is_declaration)
63d06c5c 5650 {
72bf9492 5651 add_partial_symbol (pdi, cu);
63d06c5c
DC
5652 }
5653 break;
c906108c 5654 case DW_TAG_class_type:
680b30c7 5655 case DW_TAG_interface_type:
c906108c 5656 case DW_TAG_structure_type:
72bf9492 5657 if (!pdi->is_declaration)
c906108c 5658 {
72bf9492 5659 add_partial_symbol (pdi, cu);
c906108c
SS
5660 }
5661 break;
91c24f0a 5662 case DW_TAG_enumeration_type:
72bf9492
DJ
5663 if (!pdi->is_declaration)
5664 add_partial_enumeration (pdi, cu);
c906108c
SS
5665 break;
5666 case DW_TAG_base_type:
a02abb62 5667 case DW_TAG_subrange_type:
c906108c 5668 /* File scope base type definitions are added to the partial
c5aa993b 5669 symbol table. */
72bf9492 5670 add_partial_symbol (pdi, cu);
c906108c 5671 break;
d9fa45fe 5672 case DW_TAG_namespace:
5734ee8b 5673 add_partial_namespace (pdi, lowpc, highpc, need_pc, cu);
91c24f0a 5674 break;
5d7cb8df
JK
5675 case DW_TAG_module:
5676 add_partial_module (pdi, lowpc, highpc, need_pc, cu);
5677 break;
95554aad
TT
5678 case DW_TAG_imported_unit:
5679 {
5680 struct dwarf2_per_cu_data *per_cu;
5681
f4dc4d17
DE
5682 /* For now we don't handle imported units in type units. */
5683 if (cu->per_cu->is_debug_types)
5684 {
5685 error (_("Dwarf Error: DW_TAG_imported_unit is not"
5686 " supported in type units [in module %s]"),
5687 cu->objfile->name);
5688 }
5689
95554aad 5690 per_cu = dwarf2_find_containing_comp_unit (pdi->d.offset,
36586728 5691 pdi->is_dwz,
95554aad
TT
5692 cu->objfile);
5693
5694 /* Go read the partial unit, if needed. */
5695 if (per_cu->v.psymtab == NULL)
5696 process_psymtab_comp_unit (per_cu, 1);
5697
f4dc4d17
DE
5698 VEC_safe_push (dwarf2_per_cu_ptr,
5699 cu->per_cu->s.imported_symtabs, per_cu);
95554aad
TT
5700 }
5701 break;
c906108c
SS
5702 default:
5703 break;
5704 }
5705 }
5706
72bf9492
DJ
5707 /* If the die has a sibling, skip to the sibling. */
5708
5709 pdi = pdi->die_sibling;
5710 }
5711}
5712
5713/* Functions used to compute the fully scoped name of a partial DIE.
91c24f0a 5714
72bf9492 5715 Normally, this is simple. For C++, the parent DIE's fully scoped
987504bb
JJ
5716 name is concatenated with "::" and the partial DIE's name. For
5717 Java, the same thing occurs except that "." is used instead of "::".
72bf9492
DJ
5718 Enumerators are an exception; they use the scope of their parent
5719 enumeration type, i.e. the name of the enumeration type is not
5720 prepended to the enumerator.
91c24f0a 5721
72bf9492
DJ
5722 There are two complexities. One is DW_AT_specification; in this
5723 case "parent" means the parent of the target of the specification,
5724 instead of the direct parent of the DIE. The other is compilers
5725 which do not emit DW_TAG_namespace; in this case we try to guess
5726 the fully qualified name of structure types from their members'
5727 linkage names. This must be done using the DIE's children rather
5728 than the children of any DW_AT_specification target. We only need
5729 to do this for structures at the top level, i.e. if the target of
5730 any DW_AT_specification (if any; otherwise the DIE itself) does not
5731 have a parent. */
5732
5733/* Compute the scope prefix associated with PDI's parent, in
5734 compilation unit CU. The result will be allocated on CU's
5735 comp_unit_obstack, or a copy of the already allocated PDI->NAME
5736 field. NULL is returned if no prefix is necessary. */
5737static char *
5738partial_die_parent_scope (struct partial_die_info *pdi,
5739 struct dwarf2_cu *cu)
5740{
5741 char *grandparent_scope;
5742 struct partial_die_info *parent, *real_pdi;
91c24f0a 5743
72bf9492
DJ
5744 /* We need to look at our parent DIE; if we have a DW_AT_specification,
5745 then this means the parent of the specification DIE. */
5746
5747 real_pdi = pdi;
72bf9492 5748 while (real_pdi->has_specification)
36586728
TT
5749 real_pdi = find_partial_die (real_pdi->spec_offset,
5750 real_pdi->spec_is_dwz, cu);
72bf9492
DJ
5751
5752 parent = real_pdi->die_parent;
5753 if (parent == NULL)
5754 return NULL;
5755
5756 if (parent->scope_set)
5757 return parent->scope;
5758
5759 fixup_partial_die (parent, cu);
5760
10b3939b 5761 grandparent_scope = partial_die_parent_scope (parent, cu);
72bf9492 5762
acebe513
UW
5763 /* GCC 4.0 and 4.1 had a bug (PR c++/28460) where they generated bogus
5764 DW_TAG_namespace DIEs with a name of "::" for the global namespace.
5765 Work around this problem here. */
5766 if (cu->language == language_cplus
6e70227d 5767 && parent->tag == DW_TAG_namespace
acebe513
UW
5768 && strcmp (parent->name, "::") == 0
5769 && grandparent_scope == NULL)
5770 {
5771 parent->scope = NULL;
5772 parent->scope_set = 1;
5773 return NULL;
5774 }
5775
9c6c53f7
SA
5776 if (pdi->tag == DW_TAG_enumerator)
5777 /* Enumerators should not get the name of the enumeration as a prefix. */
5778 parent->scope = grandparent_scope;
5779 else if (parent->tag == DW_TAG_namespace
f55ee35c 5780 || parent->tag == DW_TAG_module
72bf9492
DJ
5781 || parent->tag == DW_TAG_structure_type
5782 || parent->tag == DW_TAG_class_type
680b30c7 5783 || parent->tag == DW_TAG_interface_type
ceeb3d5a
TT
5784 || parent->tag == DW_TAG_union_type
5785 || parent->tag == DW_TAG_enumeration_type)
72bf9492
DJ
5786 {
5787 if (grandparent_scope == NULL)
5788 parent->scope = parent->name;
5789 else
3e43a32a
MS
5790 parent->scope = typename_concat (&cu->comp_unit_obstack,
5791 grandparent_scope,
f55ee35c 5792 parent->name, 0, cu);
72bf9492 5793 }
72bf9492
DJ
5794 else
5795 {
5796 /* FIXME drow/2004-04-01: What should we be doing with
5797 function-local names? For partial symbols, we should probably be
5798 ignoring them. */
5799 complaint (&symfile_complaints,
e2e0b3e5 5800 _("unhandled containing DIE tag %d for DIE at %d"),
b64f50a1 5801 parent->tag, pdi->offset.sect_off);
72bf9492 5802 parent->scope = grandparent_scope;
c906108c
SS
5803 }
5804
72bf9492
DJ
5805 parent->scope_set = 1;
5806 return parent->scope;
5807}
5808
5809/* Return the fully scoped name associated with PDI, from compilation unit
5810 CU. The result will be allocated with malloc. */
4568ecf9 5811
72bf9492
DJ
5812static char *
5813partial_die_full_name (struct partial_die_info *pdi,
5814 struct dwarf2_cu *cu)
5815{
5816 char *parent_scope;
5817
98bfdba5
PA
5818 /* If this is a template instantiation, we can not work out the
5819 template arguments from partial DIEs. So, unfortunately, we have
5820 to go through the full DIEs. At least any work we do building
5821 types here will be reused if full symbols are loaded later. */
5822 if (pdi->has_template_arguments)
5823 {
5824 fixup_partial_die (pdi, cu);
5825
5826 if (pdi->name != NULL && strchr (pdi->name, '<') == NULL)
5827 {
5828 struct die_info *die;
5829 struct attribute attr;
5830 struct dwarf2_cu *ref_cu = cu;
5831
b64f50a1 5832 /* DW_FORM_ref_addr is using section offset. */
98bfdba5
PA
5833 attr.name = 0;
5834 attr.form = DW_FORM_ref_addr;
4568ecf9 5835 attr.u.unsnd = pdi->offset.sect_off;
98bfdba5
PA
5836 die = follow_die_ref (NULL, &attr, &ref_cu);
5837
5838 return xstrdup (dwarf2_full_name (NULL, die, ref_cu));
5839 }
5840 }
5841
72bf9492
DJ
5842 parent_scope = partial_die_parent_scope (pdi, cu);
5843 if (parent_scope == NULL)
5844 return NULL;
5845 else
f55ee35c 5846 return typename_concat (NULL, parent_scope, pdi->name, 0, cu);
c906108c
SS
5847}
5848
5849static void
72bf9492 5850add_partial_symbol (struct partial_die_info *pdi, struct dwarf2_cu *cu)
c906108c 5851{
e7c27a73 5852 struct objfile *objfile = cu->objfile;
c906108c 5853 CORE_ADDR addr = 0;
decbce07 5854 char *actual_name = NULL;
e142c38c 5855 CORE_ADDR baseaddr;
72bf9492 5856 int built_actual_name = 0;
e142c38c
DJ
5857
5858 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
c906108c 5859
94af9270
KS
5860 actual_name = partial_die_full_name (pdi, cu);
5861 if (actual_name)
5862 built_actual_name = 1;
63d06c5c 5863
72bf9492
DJ
5864 if (actual_name == NULL)
5865 actual_name = pdi->name;
5866
c906108c
SS
5867 switch (pdi->tag)
5868 {
5869 case DW_TAG_subprogram:
2cfa0c8d 5870 if (pdi->is_external || cu->language == language_ada)
c906108c 5871 {
2cfa0c8d
JB
5872 /* brobecker/2007-12-26: Normally, only "external" DIEs are part
5873 of the global scope. But in Ada, we want to be able to access
5874 nested procedures globally. So all Ada subprograms are stored
5875 in the global scope. */
f47fb265 5876 /* prim_record_minimal_symbol (actual_name, pdi->lowpc + baseaddr,
c5aa993b 5877 mst_text, objfile); */
f47fb265
MS
5878 add_psymbol_to_list (actual_name, strlen (actual_name),
5879 built_actual_name,
5880 VAR_DOMAIN, LOC_BLOCK,
5881 &objfile->global_psymbols,
5882 0, pdi->lowpc + baseaddr,
5883 cu->language, objfile);
c906108c
SS
5884 }
5885 else
5886 {
f47fb265 5887 /* prim_record_minimal_symbol (actual_name, pdi->lowpc + baseaddr,
c5aa993b 5888 mst_file_text, objfile); */
f47fb265
MS
5889 add_psymbol_to_list (actual_name, strlen (actual_name),
5890 built_actual_name,
5891 VAR_DOMAIN, LOC_BLOCK,
5892 &objfile->static_psymbols,
5893 0, pdi->lowpc + baseaddr,
5894 cu->language, objfile);
c906108c
SS
5895 }
5896 break;
72929c62
JB
5897 case DW_TAG_constant:
5898 {
5899 struct psymbol_allocation_list *list;
5900
5901 if (pdi->is_external)
5902 list = &objfile->global_psymbols;
5903 else
5904 list = &objfile->static_psymbols;
f47fb265
MS
5905 add_psymbol_to_list (actual_name, strlen (actual_name),
5906 built_actual_name, VAR_DOMAIN, LOC_STATIC,
5907 list, 0, 0, cu->language, objfile);
72929c62
JB
5908 }
5909 break;
c906108c 5910 case DW_TAG_variable:
95554aad
TT
5911 if (pdi->d.locdesc)
5912 addr = decode_locdesc (pdi->d.locdesc, cu);
caac4577 5913
95554aad 5914 if (pdi->d.locdesc
caac4577
JG
5915 && addr == 0
5916 && !dwarf2_per_objfile->has_section_at_zero)
5917 {
5918 /* A global or static variable may also have been stripped
5919 out by the linker if unused, in which case its address
5920 will be nullified; do not add such variables into partial
5921 symbol table then. */
5922 }
5923 else if (pdi->is_external)
c906108c
SS
5924 {
5925 /* Global Variable.
5926 Don't enter into the minimal symbol tables as there is
5927 a minimal symbol table entry from the ELF symbols already.
5928 Enter into partial symbol table if it has a location
5929 descriptor or a type.
5930 If the location descriptor is missing, new_symbol will create
5931 a LOC_UNRESOLVED symbol, the address of the variable will then
5932 be determined from the minimal symbol table whenever the variable
5933 is referenced.
5934 The address for the partial symbol table entry is not
5935 used by GDB, but it comes in handy for debugging partial symbol
5936 table building. */
5937
95554aad 5938 if (pdi->d.locdesc || pdi->has_type)
f47fb265
MS
5939 add_psymbol_to_list (actual_name, strlen (actual_name),
5940 built_actual_name,
5941 VAR_DOMAIN, LOC_STATIC,
5942 &objfile->global_psymbols,
5943 0, addr + baseaddr,
5944 cu->language, objfile);
c906108c
SS
5945 }
5946 else
5947 {
0963b4bd 5948 /* Static Variable. Skip symbols without location descriptors. */
95554aad 5949 if (pdi->d.locdesc == NULL)
decbce07
MS
5950 {
5951 if (built_actual_name)
5952 xfree (actual_name);
5953 return;
5954 }
f47fb265 5955 /* prim_record_minimal_symbol (actual_name, addr + baseaddr,
c5aa993b 5956 mst_file_data, objfile); */
f47fb265
MS
5957 add_psymbol_to_list (actual_name, strlen (actual_name),
5958 built_actual_name,
5959 VAR_DOMAIN, LOC_STATIC,
5960 &objfile->static_psymbols,
5961 0, addr + baseaddr,
5962 cu->language, objfile);
c906108c
SS
5963 }
5964 break;
5965 case DW_TAG_typedef:
5966 case DW_TAG_base_type:
a02abb62 5967 case DW_TAG_subrange_type:
38d518c9 5968 add_psymbol_to_list (actual_name, strlen (actual_name),
04a679b8 5969 built_actual_name,
176620f1 5970 VAR_DOMAIN, LOC_TYPEDEF,
c906108c 5971 &objfile->static_psymbols,
e142c38c 5972 0, (CORE_ADDR) 0, cu->language, objfile);
c906108c 5973 break;
72bf9492
DJ
5974 case DW_TAG_namespace:
5975 add_psymbol_to_list (actual_name, strlen (actual_name),
04a679b8 5976 built_actual_name,
72bf9492
DJ
5977 VAR_DOMAIN, LOC_TYPEDEF,
5978 &objfile->global_psymbols,
5979 0, (CORE_ADDR) 0, cu->language, objfile);
5980 break;
c906108c 5981 case DW_TAG_class_type:
680b30c7 5982 case DW_TAG_interface_type:
c906108c
SS
5983 case DW_TAG_structure_type:
5984 case DW_TAG_union_type:
5985 case DW_TAG_enumeration_type:
fa4028e9
JB
5986 /* Skip external references. The DWARF standard says in the section
5987 about "Structure, Union, and Class Type Entries": "An incomplete
5988 structure, union or class type is represented by a structure,
5989 union or class entry that does not have a byte size attribute
5990 and that has a DW_AT_declaration attribute." */
5991 if (!pdi->has_byte_size && pdi->is_declaration)
decbce07
MS
5992 {
5993 if (built_actual_name)
5994 xfree (actual_name);
5995 return;
5996 }
fa4028e9 5997
63d06c5c
DC
5998 /* NOTE: carlton/2003-10-07: See comment in new_symbol about
5999 static vs. global. */
38d518c9 6000 add_psymbol_to_list (actual_name, strlen (actual_name),
04a679b8 6001 built_actual_name,
176620f1 6002 STRUCT_DOMAIN, LOC_TYPEDEF,
987504bb
JJ
6003 (cu->language == language_cplus
6004 || cu->language == language_java)
63d06c5c
DC
6005 ? &objfile->global_psymbols
6006 : &objfile->static_psymbols,
e142c38c 6007 0, (CORE_ADDR) 0, cu->language, objfile);
c906108c 6008
c906108c
SS
6009 break;
6010 case DW_TAG_enumerator:
38d518c9 6011 add_psymbol_to_list (actual_name, strlen (actual_name),
04a679b8 6012 built_actual_name,
176620f1 6013 VAR_DOMAIN, LOC_CONST,
987504bb
JJ
6014 (cu->language == language_cplus
6015 || cu->language == language_java)
f6fe98ef
DJ
6016 ? &objfile->global_psymbols
6017 : &objfile->static_psymbols,
e142c38c 6018 0, (CORE_ADDR) 0, cu->language, objfile);
c906108c
SS
6019 break;
6020 default:
6021 break;
6022 }
5c4e30ca 6023
72bf9492
DJ
6024 if (built_actual_name)
6025 xfree (actual_name);
c906108c
SS
6026}
6027
5c4e30ca
DC
6028/* Read a partial die corresponding to a namespace; also, add a symbol
6029 corresponding to that namespace to the symbol table. NAMESPACE is
6030 the name of the enclosing namespace. */
91c24f0a 6031
72bf9492
DJ
6032static void
6033add_partial_namespace (struct partial_die_info *pdi,
91c24f0a 6034 CORE_ADDR *lowpc, CORE_ADDR *highpc,
5734ee8b 6035 int need_pc, struct dwarf2_cu *cu)
91c24f0a 6036{
72bf9492 6037 /* Add a symbol for the namespace. */
e7c27a73 6038
72bf9492 6039 add_partial_symbol (pdi, cu);
5c4e30ca
DC
6040
6041 /* Now scan partial symbols in that namespace. */
6042
91c24f0a 6043 if (pdi->has_children)
5734ee8b 6044 scan_partial_symbols (pdi->die_child, lowpc, highpc, need_pc, cu);
91c24f0a
DC
6045}
6046
5d7cb8df
JK
6047/* Read a partial die corresponding to a Fortran module. */
6048
6049static void
6050add_partial_module (struct partial_die_info *pdi, CORE_ADDR *lowpc,
6051 CORE_ADDR *highpc, int need_pc, struct dwarf2_cu *cu)
6052{
f55ee35c 6053 /* Now scan partial symbols in that module. */
5d7cb8df
JK
6054
6055 if (pdi->has_children)
6056 scan_partial_symbols (pdi->die_child, lowpc, highpc, need_pc, cu);
6057}
6058
bc30ff58
JB
6059/* Read a partial die corresponding to a subprogram and create a partial
6060 symbol for that subprogram. When the CU language allows it, this
6061 routine also defines a partial symbol for each nested subprogram
6062 that this subprogram contains.
6e70227d 6063
bc30ff58
JB
6064 DIE my also be a lexical block, in which case we simply search
6065 recursively for suprograms defined inside that lexical block.
6066 Again, this is only performed when the CU language allows this
6067 type of definitions. */
6068
6069static void
6070add_partial_subprogram (struct partial_die_info *pdi,
6071 CORE_ADDR *lowpc, CORE_ADDR *highpc,
5734ee8b 6072 int need_pc, struct dwarf2_cu *cu)
bc30ff58
JB
6073{
6074 if (pdi->tag == DW_TAG_subprogram)
6075 {
6076 if (pdi->has_pc_info)
6077 {
6078 if (pdi->lowpc < *lowpc)
6079 *lowpc = pdi->lowpc;
6080 if (pdi->highpc > *highpc)
6081 *highpc = pdi->highpc;
5734ee8b
DJ
6082 if (need_pc)
6083 {
6084 CORE_ADDR baseaddr;
6085 struct objfile *objfile = cu->objfile;
6086
6087 baseaddr = ANOFFSET (objfile->section_offsets,
6088 SECT_OFF_TEXT (objfile));
6089 addrmap_set_empty (objfile->psymtabs_addrmap,
01637564
DE
6090 pdi->lowpc + baseaddr,
6091 pdi->highpc - 1 + baseaddr,
9291a0cd 6092 cu->per_cu->v.psymtab);
5734ee8b 6093 }
481860b3
GB
6094 }
6095
6096 if (pdi->has_pc_info || (!pdi->is_external && pdi->may_be_inlined))
6097 {
bc30ff58 6098 if (!pdi->is_declaration)
e8d05480
JB
6099 /* Ignore subprogram DIEs that do not have a name, they are
6100 illegal. Do not emit a complaint at this point, we will
6101 do so when we convert this psymtab into a symtab. */
6102 if (pdi->name)
6103 add_partial_symbol (pdi, cu);
bc30ff58
JB
6104 }
6105 }
6e70227d 6106
bc30ff58
JB
6107 if (! pdi->has_children)
6108 return;
6109
6110 if (cu->language == language_ada)
6111 {
6112 pdi = pdi->die_child;
6113 while (pdi != NULL)
6114 {
6115 fixup_partial_die (pdi, cu);
6116 if (pdi->tag == DW_TAG_subprogram
6117 || pdi->tag == DW_TAG_lexical_block)
5734ee8b 6118 add_partial_subprogram (pdi, lowpc, highpc, need_pc, cu);
bc30ff58
JB
6119 pdi = pdi->die_sibling;
6120 }
6121 }
6122}
6123
91c24f0a
DC
6124/* Read a partial die corresponding to an enumeration type. */
6125
72bf9492
DJ
6126static void
6127add_partial_enumeration (struct partial_die_info *enum_pdi,
6128 struct dwarf2_cu *cu)
91c24f0a 6129{
72bf9492 6130 struct partial_die_info *pdi;
91c24f0a
DC
6131
6132 if (enum_pdi->name != NULL)
72bf9492
DJ
6133 add_partial_symbol (enum_pdi, cu);
6134
6135 pdi = enum_pdi->die_child;
6136 while (pdi)
91c24f0a 6137 {
72bf9492 6138 if (pdi->tag != DW_TAG_enumerator || pdi->name == NULL)
e2e0b3e5 6139 complaint (&symfile_complaints, _("malformed enumerator DIE ignored"));
91c24f0a 6140 else
72bf9492
DJ
6141 add_partial_symbol (pdi, cu);
6142 pdi = pdi->die_sibling;
91c24f0a 6143 }
91c24f0a
DC
6144}
6145
6caca83c
CC
6146/* Return the initial uleb128 in the die at INFO_PTR. */
6147
6148static unsigned int
6149peek_abbrev_code (bfd *abfd, gdb_byte *info_ptr)
6150{
6151 unsigned int bytes_read;
6152
6153 return read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
6154}
6155
4bb7a0a7
DJ
6156/* Read the initial uleb128 in the die at INFO_PTR in compilation unit CU.
6157 Return the corresponding abbrev, or NULL if the number is zero (indicating
6158 an empty DIE). In either case *BYTES_READ will be set to the length of
6159 the initial number. */
6160
6161static struct abbrev_info *
fe1b8b76 6162peek_die_abbrev (gdb_byte *info_ptr, unsigned int *bytes_read,
891d2f0b 6163 struct dwarf2_cu *cu)
4bb7a0a7
DJ
6164{
6165 bfd *abfd = cu->objfile->obfd;
6166 unsigned int abbrev_number;
6167 struct abbrev_info *abbrev;
6168
6169 abbrev_number = read_unsigned_leb128 (abfd, info_ptr, bytes_read);
6170
6171 if (abbrev_number == 0)
6172 return NULL;
6173
433df2d4 6174 abbrev = abbrev_table_lookup_abbrev (cu->abbrev_table, abbrev_number);
4bb7a0a7
DJ
6175 if (!abbrev)
6176 {
3e43a32a
MS
6177 error (_("Dwarf Error: Could not find abbrev number %d [in module %s]"),
6178 abbrev_number, bfd_get_filename (abfd));
4bb7a0a7
DJ
6179 }
6180
6181 return abbrev;
6182}
6183
93311388
DE
6184/* Scan the debug information for CU starting at INFO_PTR in buffer BUFFER.
6185 Returns a pointer to the end of a series of DIEs, terminated by an empty
4bb7a0a7
DJ
6186 DIE. Any children of the skipped DIEs will also be skipped. */
6187
fe1b8b76 6188static gdb_byte *
dee91e82 6189skip_children (const struct die_reader_specs *reader, gdb_byte *info_ptr)
4bb7a0a7 6190{
dee91e82 6191 struct dwarf2_cu *cu = reader->cu;
4bb7a0a7
DJ
6192 struct abbrev_info *abbrev;
6193 unsigned int bytes_read;
6194
6195 while (1)
6196 {
6197 abbrev = peek_die_abbrev (info_ptr, &bytes_read, cu);
6198 if (abbrev == NULL)
6199 return info_ptr + bytes_read;
6200 else
dee91e82 6201 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
4bb7a0a7
DJ
6202 }
6203}
6204
93311388
DE
6205/* Scan the debug information for CU starting at INFO_PTR in buffer BUFFER.
6206 INFO_PTR should point just after the initial uleb128 of a DIE, and the
4bb7a0a7
DJ
6207 abbrev corresponding to that skipped uleb128 should be passed in
6208 ABBREV. Returns a pointer to this DIE's sibling, skipping any
6209 children. */
6210
fe1b8b76 6211static gdb_byte *
dee91e82
DE
6212skip_one_die (const struct die_reader_specs *reader, gdb_byte *info_ptr,
6213 struct abbrev_info *abbrev)
4bb7a0a7
DJ
6214{
6215 unsigned int bytes_read;
6216 struct attribute attr;
dee91e82
DE
6217 bfd *abfd = reader->abfd;
6218 struct dwarf2_cu *cu = reader->cu;
6219 gdb_byte *buffer = reader->buffer;
f664829e
DE
6220 const gdb_byte *buffer_end = reader->buffer_end;
6221 gdb_byte *start_info_ptr = info_ptr;
4bb7a0a7
DJ
6222 unsigned int form, i;
6223
6224 for (i = 0; i < abbrev->num_attrs; i++)
6225 {
6226 /* The only abbrev we care about is DW_AT_sibling. */
6227 if (abbrev->attrs[i].name == DW_AT_sibling)
6228 {
dee91e82 6229 read_attribute (reader, &attr, &abbrev->attrs[i], info_ptr);
4bb7a0a7 6230 if (attr.form == DW_FORM_ref_addr)
3e43a32a
MS
6231 complaint (&symfile_complaints,
6232 _("ignoring absolute DW_AT_sibling"));
4bb7a0a7 6233 else
b64f50a1 6234 return buffer + dwarf2_get_ref_die_offset (&attr).sect_off;
4bb7a0a7
DJ
6235 }
6236
6237 /* If it isn't DW_AT_sibling, skip this attribute. */
6238 form = abbrev->attrs[i].form;
6239 skip_attribute:
6240 switch (form)
6241 {
4bb7a0a7 6242 case DW_FORM_ref_addr:
ae411497
TT
6243 /* In DWARF 2, DW_FORM_ref_addr is address sized; in DWARF 3
6244 and later it is offset sized. */
6245 if (cu->header.version == 2)
6246 info_ptr += cu->header.addr_size;
6247 else
6248 info_ptr += cu->header.offset_size;
6249 break;
36586728
TT
6250 case DW_FORM_GNU_ref_alt:
6251 info_ptr += cu->header.offset_size;
6252 break;
ae411497 6253 case DW_FORM_addr:
4bb7a0a7
DJ
6254 info_ptr += cu->header.addr_size;
6255 break;
6256 case DW_FORM_data1:
6257 case DW_FORM_ref1:
6258 case DW_FORM_flag:
6259 info_ptr += 1;
6260 break;
2dc7f7b3
TT
6261 case DW_FORM_flag_present:
6262 break;
4bb7a0a7
DJ
6263 case DW_FORM_data2:
6264 case DW_FORM_ref2:
6265 info_ptr += 2;
6266 break;
6267 case DW_FORM_data4:
6268 case DW_FORM_ref4:
6269 info_ptr += 4;
6270 break;
6271 case DW_FORM_data8:
6272 case DW_FORM_ref8:
55f1336d 6273 case DW_FORM_ref_sig8:
4bb7a0a7
DJ
6274 info_ptr += 8;
6275 break;
6276 case DW_FORM_string:
9b1c24c8 6277 read_direct_string (abfd, info_ptr, &bytes_read);
4bb7a0a7
DJ
6278 info_ptr += bytes_read;
6279 break;
2dc7f7b3 6280 case DW_FORM_sec_offset:
4bb7a0a7 6281 case DW_FORM_strp:
36586728 6282 case DW_FORM_GNU_strp_alt:
4bb7a0a7
DJ
6283 info_ptr += cu->header.offset_size;
6284 break;
2dc7f7b3 6285 case DW_FORM_exprloc:
4bb7a0a7
DJ
6286 case DW_FORM_block:
6287 info_ptr += read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
6288 info_ptr += bytes_read;
6289 break;
6290 case DW_FORM_block1:
6291 info_ptr += 1 + read_1_byte (abfd, info_ptr);
6292 break;
6293 case DW_FORM_block2:
6294 info_ptr += 2 + read_2_bytes (abfd, info_ptr);
6295 break;
6296 case DW_FORM_block4:
6297 info_ptr += 4 + read_4_bytes (abfd, info_ptr);
6298 break;
6299 case DW_FORM_sdata:
6300 case DW_FORM_udata:
6301 case DW_FORM_ref_udata:
3019eac3
DE
6302 case DW_FORM_GNU_addr_index:
6303 case DW_FORM_GNU_str_index:
f664829e 6304 info_ptr = (gdb_byte *) safe_skip_leb128 (info_ptr, buffer_end);
4bb7a0a7
DJ
6305 break;
6306 case DW_FORM_indirect:
6307 form = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
6308 info_ptr += bytes_read;
6309 /* We need to continue parsing from here, so just go back to
6310 the top. */
6311 goto skip_attribute;
6312
6313 default:
3e43a32a
MS
6314 error (_("Dwarf Error: Cannot handle %s "
6315 "in DWARF reader [in module %s]"),
4bb7a0a7
DJ
6316 dwarf_form_name (form),
6317 bfd_get_filename (abfd));
6318 }
6319 }
6320
6321 if (abbrev->has_children)
dee91e82 6322 return skip_children (reader, info_ptr);
4bb7a0a7
DJ
6323 else
6324 return info_ptr;
6325}
6326
93311388 6327/* Locate ORIG_PDI's sibling.
dee91e82 6328 INFO_PTR should point to the start of the next DIE after ORIG_PDI. */
91c24f0a 6329
fe1b8b76 6330static gdb_byte *
dee91e82
DE
6331locate_pdi_sibling (const struct die_reader_specs *reader,
6332 struct partial_die_info *orig_pdi,
6333 gdb_byte *info_ptr)
91c24f0a
DC
6334{
6335 /* Do we know the sibling already? */
72bf9492 6336
91c24f0a
DC
6337 if (orig_pdi->sibling)
6338 return orig_pdi->sibling;
6339
6340 /* Are there any children to deal with? */
6341
6342 if (!orig_pdi->has_children)
6343 return info_ptr;
6344
4bb7a0a7 6345 /* Skip the children the long way. */
91c24f0a 6346
dee91e82 6347 return skip_children (reader, info_ptr);
91c24f0a
DC
6348}
6349
c906108c
SS
6350/* Expand this partial symbol table into a full symbol table. */
6351
6352static void
fba45db2 6353dwarf2_psymtab_to_symtab (struct partial_symtab *pst)
c906108c 6354{
c906108c
SS
6355 if (pst != NULL)
6356 {
6357 if (pst->readin)
6358 {
3e43a32a
MS
6359 warning (_("bug: psymtab for %s is already read in."),
6360 pst->filename);
c906108c
SS
6361 }
6362 else
6363 {
6364 if (info_verbose)
6365 {
3e43a32a
MS
6366 printf_filtered (_("Reading in symbols for %s..."),
6367 pst->filename);
c906108c
SS
6368 gdb_flush (gdb_stdout);
6369 }
6370
10b3939b
DJ
6371 /* Restore our global data. */
6372 dwarf2_per_objfile = objfile_data (pst->objfile,
6373 dwarf2_objfile_data_key);
6374
b2ab525c
KB
6375 /* If this psymtab is constructed from a debug-only objfile, the
6376 has_section_at_zero flag will not necessarily be correct. We
6377 can get the correct value for this flag by looking at the data
6378 associated with the (presumably stripped) associated objfile. */
6379 if (pst->objfile->separate_debug_objfile_backlink)
6380 {
6381 struct dwarf2_per_objfile *dpo_backlink
6382 = objfile_data (pst->objfile->separate_debug_objfile_backlink,
6383 dwarf2_objfile_data_key);
9a619af0 6384
b2ab525c
KB
6385 dwarf2_per_objfile->has_section_at_zero
6386 = dpo_backlink->has_section_at_zero;
6387 }
6388
98bfdba5
PA
6389 dwarf2_per_objfile->reading_partial_symbols = 0;
6390
c906108c
SS
6391 psymtab_to_symtab_1 (pst);
6392
6393 /* Finish up the debug error message. */
6394 if (info_verbose)
a3f17187 6395 printf_filtered (_("done.\n"));
c906108c
SS
6396 }
6397 }
95554aad
TT
6398
6399 process_cu_includes ();
c906108c 6400}
9cdd5dbd
DE
6401\f
6402/* Reading in full CUs. */
c906108c 6403
10b3939b
DJ
6404/* Add PER_CU to the queue. */
6405
6406static void
95554aad
TT
6407queue_comp_unit (struct dwarf2_per_cu_data *per_cu,
6408 enum language pretend_language)
10b3939b
DJ
6409{
6410 struct dwarf2_queue_item *item;
6411
6412 per_cu->queued = 1;
6413 item = xmalloc (sizeof (*item));
6414 item->per_cu = per_cu;
95554aad 6415 item->pretend_language = pretend_language;
10b3939b
DJ
6416 item->next = NULL;
6417
6418 if (dwarf2_queue == NULL)
6419 dwarf2_queue = item;
6420 else
6421 dwarf2_queue_tail->next = item;
6422
6423 dwarf2_queue_tail = item;
6424}
6425
0907af0c
DE
6426/* THIS_CU has a reference to PER_CU. If necessary, load the new compilation
6427 unit and add it to our queue.
6428 The result is non-zero if PER_CU was queued, otherwise the result is zero
6429 meaning either PER_CU is already queued or it is already loaded. */
6430
6431static int
6432maybe_queue_comp_unit (struct dwarf2_cu *this_cu,
6433 struct dwarf2_per_cu_data *per_cu,
6434 enum language pretend_language)
6435{
6436 /* We may arrive here during partial symbol reading, if we need full
6437 DIEs to process an unusual case (e.g. template arguments). Do
6438 not queue PER_CU, just tell our caller to load its DIEs. */
6439 if (dwarf2_per_objfile->reading_partial_symbols)
6440 {
6441 if (per_cu->cu == NULL || per_cu->cu->dies == NULL)
6442 return 1;
6443 return 0;
6444 }
6445
6446 /* Mark the dependence relation so that we don't flush PER_CU
6447 too early. */
6448 dwarf2_add_dependence (this_cu, per_cu);
6449
6450 /* If it's already on the queue, we have nothing to do. */
6451 if (per_cu->queued)
6452 return 0;
6453
6454 /* If the compilation unit is already loaded, just mark it as
6455 used. */
6456 if (per_cu->cu != NULL)
6457 {
6458 per_cu->cu->last_used = 0;
6459 return 0;
6460 }
6461
6462 /* Add it to the queue. */
6463 queue_comp_unit (per_cu, pretend_language);
6464
6465 return 1;
6466}
6467
10b3939b
DJ
6468/* Process the queue. */
6469
6470static void
a0f42c21 6471process_queue (void)
10b3939b
DJ
6472{
6473 struct dwarf2_queue_item *item, *next_item;
6474
45cfd468
DE
6475 if (dwarf2_read_debug)
6476 {
6477 fprintf_unfiltered (gdb_stdlog,
6478 "Expanding one or more symtabs of objfile %s ...\n",
6479 dwarf2_per_objfile->objfile->name);
6480 }
6481
03dd20cc
DJ
6482 /* The queue starts out with one item, but following a DIE reference
6483 may load a new CU, adding it to the end of the queue. */
10b3939b
DJ
6484 for (item = dwarf2_queue; item != NULL; dwarf2_queue = item = next_item)
6485 {
9291a0cd
TT
6486 if (dwarf2_per_objfile->using_index
6487 ? !item->per_cu->v.quick->symtab
6488 : (item->per_cu->v.psymtab && !item->per_cu->v.psymtab->readin))
f4dc4d17
DE
6489 {
6490 struct dwarf2_per_cu_data *per_cu = item->per_cu;
6491
6492 if (dwarf2_read_debug)
6493 {
6494 fprintf_unfiltered (gdb_stdlog,
6495 "Expanding symtab of %s at offset 0x%x\n",
6496 per_cu->is_debug_types ? "TU" : "CU",
6497 per_cu->offset.sect_off);
6498 }
6499
6500 if (per_cu->is_debug_types)
6501 process_full_type_unit (per_cu, item->pretend_language);
6502 else
6503 process_full_comp_unit (per_cu, item->pretend_language);
6504
6505 if (dwarf2_read_debug)
6506 {
6507 fprintf_unfiltered (gdb_stdlog,
6508 "Done expanding %s at offset 0x%x\n",
6509 per_cu->is_debug_types ? "TU" : "CU",
6510 per_cu->offset.sect_off);
6511 }
6512 }
10b3939b
DJ
6513
6514 item->per_cu->queued = 0;
6515 next_item = item->next;
6516 xfree (item);
6517 }
6518
6519 dwarf2_queue_tail = NULL;
45cfd468
DE
6520
6521 if (dwarf2_read_debug)
6522 {
6523 fprintf_unfiltered (gdb_stdlog, "Done expanding symtabs of %s.\n",
6524 dwarf2_per_objfile->objfile->name);
6525 }
10b3939b
DJ
6526}
6527
6528/* Free all allocated queue entries. This function only releases anything if
6529 an error was thrown; if the queue was processed then it would have been
6530 freed as we went along. */
6531
6532static void
6533dwarf2_release_queue (void *dummy)
6534{
6535 struct dwarf2_queue_item *item, *last;
6536
6537 item = dwarf2_queue;
6538 while (item)
6539 {
6540 /* Anything still marked queued is likely to be in an
6541 inconsistent state, so discard it. */
6542 if (item->per_cu->queued)
6543 {
6544 if (item->per_cu->cu != NULL)
dee91e82 6545 free_one_cached_comp_unit (item->per_cu);
10b3939b
DJ
6546 item->per_cu->queued = 0;
6547 }
6548
6549 last = item;
6550 item = item->next;
6551 xfree (last);
6552 }
6553
6554 dwarf2_queue = dwarf2_queue_tail = NULL;
6555}
6556
6557/* Read in full symbols for PST, and anything it depends on. */
6558
c906108c 6559static void
fba45db2 6560psymtab_to_symtab_1 (struct partial_symtab *pst)
c906108c 6561{
10b3939b 6562 struct dwarf2_per_cu_data *per_cu;
aaa75496
JB
6563 int i;
6564
95554aad
TT
6565 if (pst->readin)
6566 return;
6567
aaa75496 6568 for (i = 0; i < pst->number_of_dependencies; i++)
95554aad
TT
6569 if (!pst->dependencies[i]->readin
6570 && pst->dependencies[i]->user == NULL)
aaa75496
JB
6571 {
6572 /* Inform about additional files that need to be read in. */
6573 if (info_verbose)
6574 {
a3f17187 6575 /* FIXME: i18n: Need to make this a single string. */
aaa75496
JB
6576 fputs_filtered (" ", gdb_stdout);
6577 wrap_here ("");
6578 fputs_filtered ("and ", gdb_stdout);
6579 wrap_here ("");
6580 printf_filtered ("%s...", pst->dependencies[i]->filename);
0963b4bd 6581 wrap_here (""); /* Flush output. */
aaa75496
JB
6582 gdb_flush (gdb_stdout);
6583 }
6584 psymtab_to_symtab_1 (pst->dependencies[i]);
6585 }
6586
e38df1d0 6587 per_cu = pst->read_symtab_private;
10b3939b
DJ
6588
6589 if (per_cu == NULL)
aaa75496
JB
6590 {
6591 /* It's an include file, no symbols to read for it.
6592 Everything is in the parent symtab. */
6593 pst->readin = 1;
6594 return;
6595 }
c906108c 6596
a0f42c21 6597 dw2_do_instantiate_symtab (per_cu);
10b3939b
DJ
6598}
6599
dee91e82
DE
6600/* Trivial hash function for die_info: the hash value of a DIE
6601 is its offset in .debug_info for this objfile. */
10b3939b 6602
dee91e82
DE
6603static hashval_t
6604die_hash (const void *item)
10b3939b 6605{
dee91e82 6606 const struct die_info *die = item;
6502dd73 6607
dee91e82
DE
6608 return die->offset.sect_off;
6609}
63d06c5c 6610
dee91e82
DE
6611/* Trivial comparison function for die_info structures: two DIEs
6612 are equal if they have the same offset. */
98bfdba5 6613
dee91e82
DE
6614static int
6615die_eq (const void *item_lhs, const void *item_rhs)
6616{
6617 const struct die_info *die_lhs = item_lhs;
6618 const struct die_info *die_rhs = item_rhs;
c906108c 6619
dee91e82
DE
6620 return die_lhs->offset.sect_off == die_rhs->offset.sect_off;
6621}
c906108c 6622
dee91e82
DE
6623/* die_reader_func for load_full_comp_unit.
6624 This is identical to read_signatured_type_reader,
6625 but is kept separate for now. */
c906108c 6626
dee91e82
DE
6627static void
6628load_full_comp_unit_reader (const struct die_reader_specs *reader,
6629 gdb_byte *info_ptr,
6630 struct die_info *comp_unit_die,
6631 int has_children,
6632 void *data)
6633{
6634 struct dwarf2_cu *cu = reader->cu;
95554aad 6635 enum language *language_ptr = data;
6caca83c 6636
dee91e82
DE
6637 gdb_assert (cu->die_hash == NULL);
6638 cu->die_hash =
6639 htab_create_alloc_ex (cu->header.length / 12,
6640 die_hash,
6641 die_eq,
6642 NULL,
6643 &cu->comp_unit_obstack,
6644 hashtab_obstack_allocate,
6645 dummy_obstack_deallocate);
e142c38c 6646
dee91e82
DE
6647 if (has_children)
6648 comp_unit_die->child = read_die_and_siblings (reader, info_ptr,
6649 &info_ptr, comp_unit_die);
6650 cu->dies = comp_unit_die;
6651 /* comp_unit_die is not stored in die_hash, no need. */
10b3939b
DJ
6652
6653 /* We try not to read any attributes in this function, because not
9cdd5dbd 6654 all CUs needed for references have been loaded yet, and symbol
10b3939b 6655 table processing isn't initialized. But we have to set the CU language,
dee91e82
DE
6656 or we won't be able to build types correctly.
6657 Similarly, if we do not read the producer, we can not apply
6658 producer-specific interpretation. */
95554aad 6659 prepare_one_comp_unit (cu, cu->dies, *language_ptr);
dee91e82 6660}
10b3939b 6661
dee91e82 6662/* Load the DIEs associated with PER_CU into memory. */
a6c727b2 6663
dee91e82 6664static void
95554aad
TT
6665load_full_comp_unit (struct dwarf2_per_cu_data *this_cu,
6666 enum language pretend_language)
dee91e82 6667{
3019eac3 6668 gdb_assert (! this_cu->is_debug_types);
c5b7e1cb 6669
f4dc4d17
DE
6670 init_cutu_and_read_dies (this_cu, NULL, 1, 1,
6671 load_full_comp_unit_reader, &pretend_language);
10b3939b
DJ
6672}
6673
3da10d80
KS
6674/* Add a DIE to the delayed physname list. */
6675
6676static void
6677add_to_method_list (struct type *type, int fnfield_index, int index,
6678 const char *name, struct die_info *die,
6679 struct dwarf2_cu *cu)
6680{
6681 struct delayed_method_info mi;
6682 mi.type = type;
6683 mi.fnfield_index = fnfield_index;
6684 mi.index = index;
6685 mi.name = name;
6686 mi.die = die;
6687 VEC_safe_push (delayed_method_info, cu->method_list, &mi);
6688}
6689
6690/* A cleanup for freeing the delayed method list. */
6691
6692static void
6693free_delayed_list (void *ptr)
6694{
6695 struct dwarf2_cu *cu = (struct dwarf2_cu *) ptr;
6696 if (cu->method_list != NULL)
6697 {
6698 VEC_free (delayed_method_info, cu->method_list);
6699 cu->method_list = NULL;
6700 }
6701}
6702
6703/* Compute the physnames of any methods on the CU's method list.
6704
6705 The computation of method physnames is delayed in order to avoid the
6706 (bad) condition that one of the method's formal parameters is of an as yet
6707 incomplete type. */
6708
6709static void
6710compute_delayed_physnames (struct dwarf2_cu *cu)
6711{
6712 int i;
6713 struct delayed_method_info *mi;
6714 for (i = 0; VEC_iterate (delayed_method_info, cu->method_list, i, mi) ; ++i)
6715 {
1d06ead6 6716 const char *physname;
3da10d80
KS
6717 struct fn_fieldlist *fn_flp
6718 = &TYPE_FN_FIELDLIST (mi->type, mi->fnfield_index);
1d06ead6 6719 physname = dwarf2_physname ((char *) mi->name, mi->die, cu);
3da10d80
KS
6720 fn_flp->fn_fields[mi->index].physname = physname ? physname : "";
6721 }
6722}
6723
a766d390
DE
6724/* Go objects should be embedded in a DW_TAG_module DIE,
6725 and it's not clear if/how imported objects will appear.
6726 To keep Go support simple until that's worked out,
6727 go back through what we've read and create something usable.
6728 We could do this while processing each DIE, and feels kinda cleaner,
6729 but that way is more invasive.
6730 This is to, for example, allow the user to type "p var" or "b main"
6731 without having to specify the package name, and allow lookups
6732 of module.object to work in contexts that use the expression
6733 parser. */
6734
6735static void
6736fixup_go_packaging (struct dwarf2_cu *cu)
6737{
6738 char *package_name = NULL;
6739 struct pending *list;
6740 int i;
6741
6742 for (list = global_symbols; list != NULL; list = list->next)
6743 {
6744 for (i = 0; i < list->nsyms; ++i)
6745 {
6746 struct symbol *sym = list->symbol[i];
6747
6748 if (SYMBOL_LANGUAGE (sym) == language_go
6749 && SYMBOL_CLASS (sym) == LOC_BLOCK)
6750 {
6751 char *this_package_name = go_symbol_package_name (sym);
6752
6753 if (this_package_name == NULL)
6754 continue;
6755 if (package_name == NULL)
6756 package_name = this_package_name;
6757 else
6758 {
6759 if (strcmp (package_name, this_package_name) != 0)
6760 complaint (&symfile_complaints,
6761 _("Symtab %s has objects from two different Go packages: %s and %s"),
210bbc17
TT
6762 (SYMBOL_SYMTAB (sym)
6763 && SYMBOL_SYMTAB (sym)->filename
6764 ? SYMBOL_SYMTAB (sym)->filename
a766d390
DE
6765 : cu->objfile->name),
6766 this_package_name, package_name);
6767 xfree (this_package_name);
6768 }
6769 }
6770 }
6771 }
6772
6773 if (package_name != NULL)
6774 {
6775 struct objfile *objfile = cu->objfile;
6776 struct type *type = init_type (TYPE_CODE_MODULE, 0, 0,
6777 package_name, objfile);
6778 struct symbol *sym;
6779
6780 TYPE_TAG_NAME (type) = TYPE_NAME (type);
6781
6782 sym = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct symbol);
6783 SYMBOL_SET_LANGUAGE (sym, language_go);
6784 SYMBOL_SET_NAMES (sym, package_name, strlen (package_name), 1, objfile);
6785 /* This is not VAR_DOMAIN because we want a way to ensure a lookup of,
6786 e.g., "main" finds the "main" module and not C's main(). */
6787 SYMBOL_DOMAIN (sym) = STRUCT_DOMAIN;
6788 SYMBOL_CLASS (sym) = LOC_TYPEDEF;
6789 SYMBOL_TYPE (sym) = type;
6790
6791 add_symbol_to_list (sym, &global_symbols);
6792
6793 xfree (package_name);
6794 }
6795}
6796
95554aad
TT
6797static void compute_symtab_includes (struct dwarf2_per_cu_data *per_cu);
6798
6799/* Return the symtab for PER_CU. This works properly regardless of
6800 whether we're using the index or psymtabs. */
6801
6802static struct symtab *
6803get_symtab (struct dwarf2_per_cu_data *per_cu)
6804{
6805 return (dwarf2_per_objfile->using_index
6806 ? per_cu->v.quick->symtab
6807 : per_cu->v.psymtab->symtab);
6808}
6809
6810/* A helper function for computing the list of all symbol tables
6811 included by PER_CU. */
6812
6813static void
6814recursively_compute_inclusions (VEC (dwarf2_per_cu_ptr) **result,
6815 htab_t all_children,
6816 struct dwarf2_per_cu_data *per_cu)
6817{
6818 void **slot;
6819 int ix;
6820 struct dwarf2_per_cu_data *iter;
6821
6822 slot = htab_find_slot (all_children, per_cu, INSERT);
6823 if (*slot != NULL)
6824 {
6825 /* This inclusion and its children have been processed. */
6826 return;
6827 }
6828
6829 *slot = per_cu;
6830 /* Only add a CU if it has a symbol table. */
6831 if (get_symtab (per_cu) != NULL)
6832 VEC_safe_push (dwarf2_per_cu_ptr, *result, per_cu);
6833
6834 for (ix = 0;
f4dc4d17 6835 VEC_iterate (dwarf2_per_cu_ptr, per_cu->s.imported_symtabs, ix, iter);
95554aad
TT
6836 ++ix)
6837 recursively_compute_inclusions (result, all_children, iter);
6838}
6839
6840/* Compute the symtab 'includes' fields for the symtab related to
6841 PER_CU. */
6842
6843static void
6844compute_symtab_includes (struct dwarf2_per_cu_data *per_cu)
6845{
f4dc4d17
DE
6846 gdb_assert (! per_cu->is_debug_types);
6847
6848 if (!VEC_empty (dwarf2_per_cu_ptr, per_cu->s.imported_symtabs))
95554aad
TT
6849 {
6850 int ix, len;
6851 struct dwarf2_per_cu_data *iter;
6852 VEC (dwarf2_per_cu_ptr) *result_children = NULL;
6853 htab_t all_children;
6854 struct symtab *symtab = get_symtab (per_cu);
6855
6856 /* If we don't have a symtab, we can just skip this case. */
6857 if (symtab == NULL)
6858 return;
6859
6860 all_children = htab_create_alloc (1, htab_hash_pointer, htab_eq_pointer,
6861 NULL, xcalloc, xfree);
6862
6863 for (ix = 0;
f4dc4d17 6864 VEC_iterate (dwarf2_per_cu_ptr, per_cu->s.imported_symtabs,
95554aad
TT
6865 ix, iter);
6866 ++ix)
6867 recursively_compute_inclusions (&result_children, all_children, iter);
6868
6869 /* Now we have a transitive closure of all the included CUs, so
6870 we can convert it to a list of symtabs. */
6871 len = VEC_length (dwarf2_per_cu_ptr, result_children);
6872 symtab->includes
6873 = obstack_alloc (&dwarf2_per_objfile->objfile->objfile_obstack,
6874 (len + 1) * sizeof (struct symtab *));
6875 for (ix = 0;
6876 VEC_iterate (dwarf2_per_cu_ptr, result_children, ix, iter);
6877 ++ix)
6878 symtab->includes[ix] = get_symtab (iter);
6879 symtab->includes[len] = NULL;
6880
6881 VEC_free (dwarf2_per_cu_ptr, result_children);
6882 htab_delete (all_children);
6883 }
6884}
6885
6886/* Compute the 'includes' field for the symtabs of all the CUs we just
6887 read. */
6888
6889static void
6890process_cu_includes (void)
6891{
6892 int ix;
6893 struct dwarf2_per_cu_data *iter;
6894
6895 for (ix = 0;
6896 VEC_iterate (dwarf2_per_cu_ptr, dwarf2_per_objfile->just_read_cus,
6897 ix, iter);
6898 ++ix)
f4dc4d17
DE
6899 {
6900 if (! iter->is_debug_types)
6901 compute_symtab_includes (iter);
6902 }
95554aad
TT
6903
6904 VEC_free (dwarf2_per_cu_ptr, dwarf2_per_objfile->just_read_cus);
6905}
6906
9cdd5dbd 6907/* Generate full symbol information for PER_CU, whose DIEs have
10b3939b
DJ
6908 already been loaded into memory. */
6909
6910static void
95554aad
TT
6911process_full_comp_unit (struct dwarf2_per_cu_data *per_cu,
6912 enum language pretend_language)
10b3939b 6913{
10b3939b 6914 struct dwarf2_cu *cu = per_cu->cu;
9291a0cd 6915 struct objfile *objfile = per_cu->objfile;
10b3939b
DJ
6916 CORE_ADDR lowpc, highpc;
6917 struct symtab *symtab;
3da10d80 6918 struct cleanup *back_to, *delayed_list_cleanup;
10b3939b 6919 CORE_ADDR baseaddr;
4359dff1 6920 struct block *static_block;
10b3939b
DJ
6921
6922 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
6923
10b3939b
DJ
6924 buildsym_init ();
6925 back_to = make_cleanup (really_free_pendings, NULL);
3da10d80 6926 delayed_list_cleanup = make_cleanup (free_delayed_list, cu);
10b3939b
DJ
6927
6928 cu->list_in_scope = &file_symbols;
c906108c 6929
95554aad
TT
6930 cu->language = pretend_language;
6931 cu->language_defn = language_def (cu->language);
6932
c906108c 6933 /* Do line number decoding in read_file_scope () */
10b3939b 6934 process_die (cu->dies, cu);
c906108c 6935
a766d390
DE
6936 /* For now fudge the Go package. */
6937 if (cu->language == language_go)
6938 fixup_go_packaging (cu);
6939
3da10d80
KS
6940 /* Now that we have processed all the DIEs in the CU, all the types
6941 should be complete, and it should now be safe to compute all of the
6942 physnames. */
6943 compute_delayed_physnames (cu);
6944 do_cleanups (delayed_list_cleanup);
6945
fae299cd
DC
6946 /* Some compilers don't define a DW_AT_high_pc attribute for the
6947 compilation unit. If the DW_AT_high_pc is missing, synthesize
6948 it, by scanning the DIE's below the compilation unit. */
10b3939b 6949 get_scope_pc_bounds (cu->dies, &lowpc, &highpc, cu);
c906108c 6950
36586728
TT
6951 static_block
6952 = end_symtab_get_static_block (highpc + baseaddr, objfile, 0,
6953 per_cu->s.imported_symtabs != NULL);
4359dff1
JK
6954
6955 /* If the comp unit has DW_AT_ranges, it may have discontiguous ranges.
6956 Also, DW_AT_ranges may record ranges not belonging to any child DIEs
6957 (such as virtual method tables). Record the ranges in STATIC_BLOCK's
6958 addrmap to help ensure it has an accurate map of pc values belonging to
6959 this comp unit. */
6960 dwarf2_record_block_ranges (cu->dies, static_block, baseaddr, cu);
6961
6962 symtab = end_symtab_from_static_block (static_block, objfile,
6963 SECT_OFF_TEXT (objfile), 0);
c906108c 6964
8be455d7 6965 if (symtab != NULL)
c906108c 6966 {
df15bd07 6967 int gcc_4_minor = producer_is_gcc_ge_4 (cu->producer);
4632c0d0 6968
8be455d7
JK
6969 /* Set symtab language to language from DW_AT_language. If the
6970 compilation is from a C file generated by language preprocessors, do
6971 not set the language if it was already deduced by start_subfile. */
6972 if (!(cu->language == language_c && symtab->language != language_c))
6973 symtab->language = cu->language;
6974
6975 /* GCC-4.0 has started to support -fvar-tracking. GCC-3.x still can
6976 produce DW_AT_location with location lists but it can be possibly
ab260dad
JK
6977 invalid without -fvar-tracking. Still up to GCC-4.4.x incl. 4.4.0
6978 there were bugs in prologue debug info, fixed later in GCC-4.5
6979 by "unwind info for epilogues" patch (which is not directly related).
8be455d7
JK
6980
6981 For -gdwarf-4 type units LOCATIONS_VALID indication is fortunately not
6982 needed, it would be wrong due to missing DW_AT_producer there.
6983
6984 Still one can confuse GDB by using non-standard GCC compilation
6985 options - this waits on GCC PR other/32998 (-frecord-gcc-switches).
6986 */
ab260dad 6987 if (cu->has_loclist && gcc_4_minor >= 5)
8be455d7 6988 symtab->locations_valid = 1;
e0d00bc7
JK
6989
6990 if (gcc_4_minor >= 5)
6991 symtab->epilogue_unwind_valid = 1;
96408a79
SA
6992
6993 symtab->call_site_htab = cu->call_site_htab;
c906108c 6994 }
9291a0cd
TT
6995
6996 if (dwarf2_per_objfile->using_index)
6997 per_cu->v.quick->symtab = symtab;
6998 else
6999 {
7000 struct partial_symtab *pst = per_cu->v.psymtab;
7001 pst->symtab = symtab;
7002 pst->readin = 1;
7003 }
c906108c 7004
95554aad
TT
7005 /* Push it for inclusion processing later. */
7006 VEC_safe_push (dwarf2_per_cu_ptr, dwarf2_per_objfile->just_read_cus, per_cu);
7007
c906108c 7008 do_cleanups (back_to);
f4dc4d17 7009}
45cfd468 7010
f4dc4d17
DE
7011/* Generate full symbol information for type unit PER_CU, whose DIEs have
7012 already been loaded into memory. */
7013
7014static void
7015process_full_type_unit (struct dwarf2_per_cu_data *per_cu,
7016 enum language pretend_language)
7017{
7018 struct dwarf2_cu *cu = per_cu->cu;
7019 struct objfile *objfile = per_cu->objfile;
7020 struct symtab *symtab;
7021 struct cleanup *back_to, *delayed_list_cleanup;
7022
7023 buildsym_init ();
7024 back_to = make_cleanup (really_free_pendings, NULL);
7025 delayed_list_cleanup = make_cleanup (free_delayed_list, cu);
7026
7027 cu->list_in_scope = &file_symbols;
7028
7029 cu->language = pretend_language;
7030 cu->language_defn = language_def (cu->language);
7031
7032 /* The symbol tables are set up in read_type_unit_scope. */
7033 process_die (cu->dies, cu);
7034
7035 /* For now fudge the Go package. */
7036 if (cu->language == language_go)
7037 fixup_go_packaging (cu);
7038
7039 /* Now that we have processed all the DIEs in the CU, all the types
7040 should be complete, and it should now be safe to compute all of the
7041 physnames. */
7042 compute_delayed_physnames (cu);
7043 do_cleanups (delayed_list_cleanup);
7044
7045 /* TUs share symbol tables.
7046 If this is the first TU to use this symtab, complete the construction
094b34ac
DE
7047 of it with end_expandable_symtab. Otherwise, complete the addition of
7048 this TU's symbols to the existing symtab. */
f4dc4d17 7049 if (per_cu->s.type_unit_group->primary_symtab == NULL)
45cfd468 7050 {
f4dc4d17
DE
7051 symtab = end_expandable_symtab (0, objfile, SECT_OFF_TEXT (objfile));
7052 per_cu->s.type_unit_group->primary_symtab = symtab;
7053
7054 if (symtab != NULL)
7055 {
7056 /* Set symtab language to language from DW_AT_language. If the
7057 compilation is from a C file generated by language preprocessors,
7058 do not set the language if it was already deduced by
7059 start_subfile. */
7060 if (!(cu->language == language_c && symtab->language != language_c))
7061 symtab->language = cu->language;
7062 }
7063 }
7064 else
7065 {
7066 augment_type_symtab (objfile,
7067 per_cu->s.type_unit_group->primary_symtab);
7068 symtab = per_cu->s.type_unit_group->primary_symtab;
7069 }
7070
7071 if (dwarf2_per_objfile->using_index)
7072 per_cu->v.quick->symtab = symtab;
7073 else
7074 {
7075 struct partial_symtab *pst = per_cu->v.psymtab;
7076 pst->symtab = symtab;
7077 pst->readin = 1;
45cfd468 7078 }
f4dc4d17
DE
7079
7080 do_cleanups (back_to);
c906108c
SS
7081}
7082
95554aad
TT
7083/* Process an imported unit DIE. */
7084
7085static void
7086process_imported_unit_die (struct die_info *die, struct dwarf2_cu *cu)
7087{
7088 struct attribute *attr;
7089
f4dc4d17
DE
7090 /* For now we don't handle imported units in type units. */
7091 if (cu->per_cu->is_debug_types)
7092 {
7093 error (_("Dwarf Error: DW_TAG_imported_unit is not"
7094 " supported in type units [in module %s]"),
7095 cu->objfile->name);
7096 }
7097
95554aad
TT
7098 attr = dwarf2_attr (die, DW_AT_import, cu);
7099 if (attr != NULL)
7100 {
7101 struct dwarf2_per_cu_data *per_cu;
7102 struct symtab *imported_symtab;
7103 sect_offset offset;
36586728 7104 int is_dwz;
95554aad
TT
7105
7106 offset = dwarf2_get_ref_die_offset (attr);
36586728
TT
7107 is_dwz = (attr->form == DW_FORM_GNU_ref_alt || cu->per_cu->is_dwz);
7108 per_cu = dwarf2_find_containing_comp_unit (offset, is_dwz, cu->objfile);
95554aad
TT
7109
7110 /* Queue the unit, if needed. */
7111 if (maybe_queue_comp_unit (cu, per_cu, cu->language))
7112 load_full_comp_unit (per_cu, cu->language);
7113
f4dc4d17 7114 VEC_safe_push (dwarf2_per_cu_ptr, cu->per_cu->s.imported_symtabs,
95554aad
TT
7115 per_cu);
7116 }
7117}
7118
c906108c
SS
7119/* Process a die and its children. */
7120
7121static void
e7c27a73 7122process_die (struct die_info *die, struct dwarf2_cu *cu)
c906108c
SS
7123{
7124 switch (die->tag)
7125 {
7126 case DW_TAG_padding:
7127 break;
7128 case DW_TAG_compile_unit:
95554aad 7129 case DW_TAG_partial_unit:
e7c27a73 7130 read_file_scope (die, cu);
c906108c 7131 break;
348e048f
DE
7132 case DW_TAG_type_unit:
7133 read_type_unit_scope (die, cu);
7134 break;
c906108c 7135 case DW_TAG_subprogram:
c906108c 7136 case DW_TAG_inlined_subroutine:
edb3359d 7137 read_func_scope (die, cu);
c906108c
SS
7138 break;
7139 case DW_TAG_lexical_block:
14898363
L
7140 case DW_TAG_try_block:
7141 case DW_TAG_catch_block:
e7c27a73 7142 read_lexical_block_scope (die, cu);
c906108c 7143 break;
96408a79
SA
7144 case DW_TAG_GNU_call_site:
7145 read_call_site_scope (die, cu);
7146 break;
c906108c 7147 case DW_TAG_class_type:
680b30c7 7148 case DW_TAG_interface_type:
c906108c
SS
7149 case DW_TAG_structure_type:
7150 case DW_TAG_union_type:
134d01f1 7151 process_structure_scope (die, cu);
c906108c
SS
7152 break;
7153 case DW_TAG_enumeration_type:
134d01f1 7154 process_enumeration_scope (die, cu);
c906108c 7155 break;
134d01f1 7156
f792889a
DJ
7157 /* These dies have a type, but processing them does not create
7158 a symbol or recurse to process the children. Therefore we can
7159 read them on-demand through read_type_die. */
c906108c 7160 case DW_TAG_subroutine_type:
72019c9c 7161 case DW_TAG_set_type:
c906108c 7162 case DW_TAG_array_type:
c906108c 7163 case DW_TAG_pointer_type:
c906108c 7164 case DW_TAG_ptr_to_member_type:
c906108c 7165 case DW_TAG_reference_type:
c906108c 7166 case DW_TAG_string_type:
c906108c 7167 break;
134d01f1 7168
c906108c 7169 case DW_TAG_base_type:
a02abb62 7170 case DW_TAG_subrange_type:
cb249c71 7171 case DW_TAG_typedef:
134d01f1
DJ
7172 /* Add a typedef symbol for the type definition, if it has a
7173 DW_AT_name. */
f792889a 7174 new_symbol (die, read_type_die (die, cu), cu);
a02abb62 7175 break;
c906108c 7176 case DW_TAG_common_block:
e7c27a73 7177 read_common_block (die, cu);
c906108c
SS
7178 break;
7179 case DW_TAG_common_inclusion:
7180 break;
d9fa45fe 7181 case DW_TAG_namespace:
63d06c5c 7182 processing_has_namespace_info = 1;
e7c27a73 7183 read_namespace (die, cu);
d9fa45fe 7184 break;
5d7cb8df 7185 case DW_TAG_module:
f55ee35c 7186 processing_has_namespace_info = 1;
5d7cb8df
JK
7187 read_module (die, cu);
7188 break;
d9fa45fe
DC
7189 case DW_TAG_imported_declaration:
7190 case DW_TAG_imported_module:
63d06c5c 7191 processing_has_namespace_info = 1;
27aa8d6a
SW
7192 if (die->child != NULL && (die->tag == DW_TAG_imported_declaration
7193 || cu->language != language_fortran))
7194 complaint (&symfile_complaints, _("Tag '%s' has unexpected children"),
7195 dwarf_tag_name (die->tag));
7196 read_import_statement (die, cu);
d9fa45fe 7197 break;
95554aad
TT
7198
7199 case DW_TAG_imported_unit:
7200 process_imported_unit_die (die, cu);
7201 break;
7202
c906108c 7203 default:
e7c27a73 7204 new_symbol (die, NULL, cu);
c906108c
SS
7205 break;
7206 }
7207}
7208
94af9270
KS
7209/* A helper function for dwarf2_compute_name which determines whether DIE
7210 needs to have the name of the scope prepended to the name listed in the
7211 die. */
7212
7213static int
7214die_needs_namespace (struct die_info *die, struct dwarf2_cu *cu)
7215{
1c809c68
TT
7216 struct attribute *attr;
7217
94af9270
KS
7218 switch (die->tag)
7219 {
7220 case DW_TAG_namespace:
7221 case DW_TAG_typedef:
7222 case DW_TAG_class_type:
7223 case DW_TAG_interface_type:
7224 case DW_TAG_structure_type:
7225 case DW_TAG_union_type:
7226 case DW_TAG_enumeration_type:
7227 case DW_TAG_enumerator:
7228 case DW_TAG_subprogram:
7229 case DW_TAG_member:
7230 return 1;
7231
7232 case DW_TAG_variable:
c2b0a229 7233 case DW_TAG_constant:
94af9270
KS
7234 /* We only need to prefix "globally" visible variables. These include
7235 any variable marked with DW_AT_external or any variable that
7236 lives in a namespace. [Variables in anonymous namespaces
7237 require prefixing, but they are not DW_AT_external.] */
7238
7239 if (dwarf2_attr (die, DW_AT_specification, cu))
7240 {
7241 struct dwarf2_cu *spec_cu = cu;
9a619af0 7242
94af9270
KS
7243 return die_needs_namespace (die_specification (die, &spec_cu),
7244 spec_cu);
7245 }
7246
1c809c68 7247 attr = dwarf2_attr (die, DW_AT_external, cu);
f55ee35c
JK
7248 if (attr == NULL && die->parent->tag != DW_TAG_namespace
7249 && die->parent->tag != DW_TAG_module)
1c809c68
TT
7250 return 0;
7251 /* A variable in a lexical block of some kind does not need a
7252 namespace, even though in C++ such variables may be external
7253 and have a mangled name. */
7254 if (die->parent->tag == DW_TAG_lexical_block
7255 || die->parent->tag == DW_TAG_try_block
1054b214
TT
7256 || die->parent->tag == DW_TAG_catch_block
7257 || die->parent->tag == DW_TAG_subprogram)
1c809c68
TT
7258 return 0;
7259 return 1;
94af9270
KS
7260
7261 default:
7262 return 0;
7263 }
7264}
7265
98bfdba5
PA
7266/* Retrieve the last character from a mem_file. */
7267
7268static void
7269do_ui_file_peek_last (void *object, const char *buffer, long length)
7270{
7271 char *last_char_p = (char *) object;
7272
7273 if (length > 0)
7274 *last_char_p = buffer[length - 1];
7275}
7276
94af9270 7277/* Compute the fully qualified name of DIE in CU. If PHYSNAME is nonzero,
a766d390
DE
7278 compute the physname for the object, which include a method's:
7279 - formal parameters (C++/Java),
7280 - receiver type (Go),
7281 - return type (Java).
7282
7283 The term "physname" is a bit confusing.
7284 For C++, for example, it is the demangled name.
7285 For Go, for example, it's the mangled name.
94af9270 7286
af6b7be1
JB
7287 For Ada, return the DIE's linkage name rather than the fully qualified
7288 name. PHYSNAME is ignored..
7289
94af9270
KS
7290 The result is allocated on the objfile_obstack and canonicalized. */
7291
7292static const char *
7293dwarf2_compute_name (char *name, struct die_info *die, struct dwarf2_cu *cu,
7294 int physname)
7295{
bb5ed363
DE
7296 struct objfile *objfile = cu->objfile;
7297
94af9270
KS
7298 if (name == NULL)
7299 name = dwarf2_name (die, cu);
7300
f55ee35c
JK
7301 /* For Fortran GDB prefers DW_AT_*linkage_name if present but otherwise
7302 compute it by typename_concat inside GDB. */
7303 if (cu->language == language_ada
7304 || (cu->language == language_fortran && physname))
7305 {
7306 /* For Ada unit, we prefer the linkage name over the name, as
7307 the former contains the exported name, which the user expects
7308 to be able to reference. Ideally, we want the user to be able
7309 to reference this entity using either natural or linkage name,
7310 but we haven't started looking at this enhancement yet. */
7311 struct attribute *attr;
7312
7313 attr = dwarf2_attr (die, DW_AT_linkage_name, cu);
7314 if (attr == NULL)
7315 attr = dwarf2_attr (die, DW_AT_MIPS_linkage_name, cu);
7316 if (attr && DW_STRING (attr))
7317 return DW_STRING (attr);
7318 }
7319
94af9270
KS
7320 /* These are the only languages we know how to qualify names in. */
7321 if (name != NULL
f55ee35c
JK
7322 && (cu->language == language_cplus || cu->language == language_java
7323 || cu->language == language_fortran))
94af9270
KS
7324 {
7325 if (die_needs_namespace (die, cu))
7326 {
7327 long length;
0d5cff50 7328 const char *prefix;
94af9270
KS
7329 struct ui_file *buf;
7330
7331 prefix = determine_prefix (die, cu);
7332 buf = mem_fileopen ();
7333 if (*prefix != '\0')
7334 {
f55ee35c
JK
7335 char *prefixed_name = typename_concat (NULL, prefix, name,
7336 physname, cu);
9a619af0 7337
94af9270
KS
7338 fputs_unfiltered (prefixed_name, buf);
7339 xfree (prefixed_name);
7340 }
7341 else
62d5b8da 7342 fputs_unfiltered (name, buf);
94af9270 7343
98bfdba5
PA
7344 /* Template parameters may be specified in the DIE's DW_AT_name, or
7345 as children with DW_TAG_template_type_param or
7346 DW_TAG_value_type_param. If the latter, add them to the name
7347 here. If the name already has template parameters, then
7348 skip this step; some versions of GCC emit both, and
7349 it is more efficient to use the pre-computed name.
7350
7351 Something to keep in mind about this process: it is very
7352 unlikely, or in some cases downright impossible, to produce
7353 something that will match the mangled name of a function.
7354 If the definition of the function has the same debug info,
7355 we should be able to match up with it anyway. But fallbacks
7356 using the minimal symbol, for instance to find a method
7357 implemented in a stripped copy of libstdc++, will not work.
7358 If we do not have debug info for the definition, we will have to
7359 match them up some other way.
7360
7361 When we do name matching there is a related problem with function
7362 templates; two instantiated function templates are allowed to
7363 differ only by their return types, which we do not add here. */
7364
7365 if (cu->language == language_cplus && strchr (name, '<') == NULL)
7366 {
7367 struct attribute *attr;
7368 struct die_info *child;
7369 int first = 1;
7370
7371 die->building_fullname = 1;
7372
7373 for (child = die->child; child != NULL; child = child->sibling)
7374 {
7375 struct type *type;
12df843f 7376 LONGEST value;
98bfdba5
PA
7377 gdb_byte *bytes;
7378 struct dwarf2_locexpr_baton *baton;
7379 struct value *v;
7380
7381 if (child->tag != DW_TAG_template_type_param
7382 && child->tag != DW_TAG_template_value_param)
7383 continue;
7384
7385 if (first)
7386 {
7387 fputs_unfiltered ("<", buf);
7388 first = 0;
7389 }
7390 else
7391 fputs_unfiltered (", ", buf);
7392
7393 attr = dwarf2_attr (child, DW_AT_type, cu);
7394 if (attr == NULL)
7395 {
7396 complaint (&symfile_complaints,
7397 _("template parameter missing DW_AT_type"));
7398 fputs_unfiltered ("UNKNOWN_TYPE", buf);
7399 continue;
7400 }
7401 type = die_type (child, cu);
7402
7403 if (child->tag == DW_TAG_template_type_param)
7404 {
79d43c61 7405 c_print_type (type, "", buf, -1, 0, &type_print_raw_options);
98bfdba5
PA
7406 continue;
7407 }
7408
7409 attr = dwarf2_attr (child, DW_AT_const_value, cu);
7410 if (attr == NULL)
7411 {
7412 complaint (&symfile_complaints,
3e43a32a
MS
7413 _("template parameter missing "
7414 "DW_AT_const_value"));
98bfdba5
PA
7415 fputs_unfiltered ("UNKNOWN_VALUE", buf);
7416 continue;
7417 }
7418
7419 dwarf2_const_value_attr (attr, type, name,
7420 &cu->comp_unit_obstack, cu,
7421 &value, &bytes, &baton);
7422
7423 if (TYPE_NOSIGN (type))
7424 /* GDB prints characters as NUMBER 'CHAR'. If that's
7425 changed, this can use value_print instead. */
7426 c_printchar (value, type, buf);
7427 else
7428 {
7429 struct value_print_options opts;
7430
7431 if (baton != NULL)
7432 v = dwarf2_evaluate_loc_desc (type, NULL,
7433 baton->data,
7434 baton->size,
7435 baton->per_cu);
7436 else if (bytes != NULL)
7437 {
7438 v = allocate_value (type);
7439 memcpy (value_contents_writeable (v), bytes,
7440 TYPE_LENGTH (type));
7441 }
7442 else
7443 v = value_from_longest (type, value);
7444
3e43a32a
MS
7445 /* Specify decimal so that we do not depend on
7446 the radix. */
98bfdba5
PA
7447 get_formatted_print_options (&opts, 'd');
7448 opts.raw = 1;
7449 value_print (v, buf, &opts);
7450 release_value (v);
7451 value_free (v);
7452 }
7453 }
7454
7455 die->building_fullname = 0;
7456
7457 if (!first)
7458 {
7459 /* Close the argument list, with a space if necessary
7460 (nested templates). */
7461 char last_char = '\0';
7462 ui_file_put (buf, do_ui_file_peek_last, &last_char);
7463 if (last_char == '>')
7464 fputs_unfiltered (" >", buf);
7465 else
7466 fputs_unfiltered (">", buf);
7467 }
7468 }
7469
94af9270
KS
7470 /* For Java and C++ methods, append formal parameter type
7471 information, if PHYSNAME. */
6e70227d 7472
94af9270
KS
7473 if (physname && die->tag == DW_TAG_subprogram
7474 && (cu->language == language_cplus
7475 || cu->language == language_java))
7476 {
7477 struct type *type = read_type_die (die, cu);
7478
79d43c61
TT
7479 c_type_print_args (type, buf, 1, cu->language,
7480 &type_print_raw_options);
94af9270
KS
7481
7482 if (cu->language == language_java)
7483 {
7484 /* For java, we must append the return type to method
0963b4bd 7485 names. */
94af9270
KS
7486 if (die->tag == DW_TAG_subprogram)
7487 java_print_type (TYPE_TARGET_TYPE (type), "", buf,
79d43c61 7488 0, 0, &type_print_raw_options);
94af9270
KS
7489 }
7490 else if (cu->language == language_cplus)
7491 {
60430eff
DJ
7492 /* Assume that an artificial first parameter is
7493 "this", but do not crash if it is not. RealView
7494 marks unnamed (and thus unused) parameters as
7495 artificial; there is no way to differentiate
7496 the two cases. */
94af9270
KS
7497 if (TYPE_NFIELDS (type) > 0
7498 && TYPE_FIELD_ARTIFICIAL (type, 0)
60430eff 7499 && TYPE_CODE (TYPE_FIELD_TYPE (type, 0)) == TYPE_CODE_PTR
3e43a32a
MS
7500 && TYPE_CONST (TYPE_TARGET_TYPE (TYPE_FIELD_TYPE (type,
7501 0))))
94af9270
KS
7502 fputs_unfiltered (" const", buf);
7503 }
7504 }
7505
bb5ed363 7506 name = ui_file_obsavestring (buf, &objfile->objfile_obstack,
94af9270
KS
7507 &length);
7508 ui_file_delete (buf);
7509
7510 if (cu->language == language_cplus)
7511 {
7512 char *cname
7513 = dwarf2_canonicalize_name (name, cu,
bb5ed363 7514 &objfile->objfile_obstack);
9a619af0 7515
94af9270
KS
7516 if (cname != NULL)
7517 name = cname;
7518 }
7519 }
7520 }
7521
7522 return name;
7523}
7524
0114d602
DJ
7525/* Return the fully qualified name of DIE, based on its DW_AT_name.
7526 If scope qualifiers are appropriate they will be added. The result
7527 will be allocated on the objfile_obstack, or NULL if the DIE does
94af9270
KS
7528 not have a name. NAME may either be from a previous call to
7529 dwarf2_name or NULL.
7530
0963b4bd 7531 The output string will be canonicalized (if C++/Java). */
0114d602
DJ
7532
7533static const char *
94af9270 7534dwarf2_full_name (char *name, struct die_info *die, struct dwarf2_cu *cu)
0114d602 7535{
94af9270
KS
7536 return dwarf2_compute_name (name, die, cu, 0);
7537}
0114d602 7538
94af9270
KS
7539/* Construct a physname for the given DIE in CU. NAME may either be
7540 from a previous call to dwarf2_name or NULL. The result will be
7541 allocated on the objfile_objstack or NULL if the DIE does not have a
7542 name.
0114d602 7543
94af9270 7544 The output string will be canonicalized (if C++/Java). */
0114d602 7545
94af9270
KS
7546static const char *
7547dwarf2_physname (char *name, struct die_info *die, struct dwarf2_cu *cu)
7548{
bb5ed363 7549 struct objfile *objfile = cu->objfile;
900e11f9
JK
7550 struct attribute *attr;
7551 const char *retval, *mangled = NULL, *canon = NULL;
7552 struct cleanup *back_to;
7553 int need_copy = 1;
7554
7555 /* In this case dwarf2_compute_name is just a shortcut not building anything
7556 on its own. */
7557 if (!die_needs_namespace (die, cu))
7558 return dwarf2_compute_name (name, die, cu, 1);
7559
7560 back_to = make_cleanup (null_cleanup, NULL);
7561
7562 attr = dwarf2_attr (die, DW_AT_linkage_name, cu);
7563 if (!attr)
7564 attr = dwarf2_attr (die, DW_AT_MIPS_linkage_name, cu);
7565
7566 /* DW_AT_linkage_name is missing in some cases - depend on what GDB
7567 has computed. */
7568 if (attr && DW_STRING (attr))
7569 {
7570 char *demangled;
7571
7572 mangled = DW_STRING (attr);
7573
7574 /* Use DMGL_RET_DROP for C++ template functions to suppress their return
7575 type. It is easier for GDB users to search for such functions as
7576 `name(params)' than `long name(params)'. In such case the minimal
7577 symbol names do not match the full symbol names but for template
7578 functions there is never a need to look up their definition from their
7579 declaration so the only disadvantage remains the minimal symbol
7580 variant `long name(params)' does not have the proper inferior type.
7581 */
7582
a766d390
DE
7583 if (cu->language == language_go)
7584 {
7585 /* This is a lie, but we already lie to the caller new_symbol_full.
7586 new_symbol_full assumes we return the mangled name.
7587 This just undoes that lie until things are cleaned up. */
7588 demangled = NULL;
7589 }
7590 else
7591 {
7592 demangled = cplus_demangle (mangled,
7593 (DMGL_PARAMS | DMGL_ANSI
7594 | (cu->language == language_java
7595 ? DMGL_JAVA | DMGL_RET_POSTFIX
7596 : DMGL_RET_DROP)));
7597 }
900e11f9
JK
7598 if (demangled)
7599 {
7600 make_cleanup (xfree, demangled);
7601 canon = demangled;
7602 }
7603 else
7604 {
7605 canon = mangled;
7606 need_copy = 0;
7607 }
7608 }
7609
7610 if (canon == NULL || check_physname)
7611 {
7612 const char *physname = dwarf2_compute_name (name, die, cu, 1);
7613
7614 if (canon != NULL && strcmp (physname, canon) != 0)
7615 {
7616 /* It may not mean a bug in GDB. The compiler could also
7617 compute DW_AT_linkage_name incorrectly. But in such case
7618 GDB would need to be bug-to-bug compatible. */
7619
7620 complaint (&symfile_complaints,
7621 _("Computed physname <%s> does not match demangled <%s> "
7622 "(from linkage <%s>) - DIE at 0x%x [in module %s]"),
b64f50a1 7623 physname, canon, mangled, die->offset.sect_off, objfile->name);
900e11f9
JK
7624
7625 /* Prefer DW_AT_linkage_name (in the CANON form) - when it
7626 is available here - over computed PHYSNAME. It is safer
7627 against both buggy GDB and buggy compilers. */
7628
7629 retval = canon;
7630 }
7631 else
7632 {
7633 retval = physname;
7634 need_copy = 0;
7635 }
7636 }
7637 else
7638 retval = canon;
7639
7640 if (need_copy)
7641 retval = obsavestring (retval, strlen (retval),
bb5ed363 7642 &objfile->objfile_obstack);
900e11f9
JK
7643
7644 do_cleanups (back_to);
7645 return retval;
0114d602
DJ
7646}
7647
27aa8d6a
SW
7648/* Read the import statement specified by the given die and record it. */
7649
7650static void
7651read_import_statement (struct die_info *die, struct dwarf2_cu *cu)
7652{
bb5ed363 7653 struct objfile *objfile = cu->objfile;
27aa8d6a 7654 struct attribute *import_attr;
32019081 7655 struct die_info *imported_die, *child_die;
de4affc9 7656 struct dwarf2_cu *imported_cu;
27aa8d6a 7657 const char *imported_name;
794684b6 7658 const char *imported_name_prefix;
13387711
SW
7659 const char *canonical_name;
7660 const char *import_alias;
7661 const char *imported_declaration = NULL;
794684b6 7662 const char *import_prefix;
32019081
JK
7663 VEC (const_char_ptr) *excludes = NULL;
7664 struct cleanup *cleanups;
13387711
SW
7665
7666 char *temp;
27aa8d6a
SW
7667
7668 import_attr = dwarf2_attr (die, DW_AT_import, cu);
7669 if (import_attr == NULL)
7670 {
7671 complaint (&symfile_complaints, _("Tag '%s' has no DW_AT_import"),
7672 dwarf_tag_name (die->tag));
7673 return;
7674 }
7675
de4affc9
CC
7676 imported_cu = cu;
7677 imported_die = follow_die_ref_or_sig (die, import_attr, &imported_cu);
7678 imported_name = dwarf2_name (imported_die, imported_cu);
27aa8d6a
SW
7679 if (imported_name == NULL)
7680 {
7681 /* GCC bug: https://bugzilla.redhat.com/show_bug.cgi?id=506524
7682
7683 The import in the following code:
7684 namespace A
7685 {
7686 typedef int B;
7687 }
7688
7689 int main ()
7690 {
7691 using A::B;
7692 B b;
7693 return b;
7694 }
7695
7696 ...
7697 <2><51>: Abbrev Number: 3 (DW_TAG_imported_declaration)
7698 <52> DW_AT_decl_file : 1
7699 <53> DW_AT_decl_line : 6
7700 <54> DW_AT_import : <0x75>
7701 <2><58>: Abbrev Number: 4 (DW_TAG_typedef)
7702 <59> DW_AT_name : B
7703 <5b> DW_AT_decl_file : 1
7704 <5c> DW_AT_decl_line : 2
7705 <5d> DW_AT_type : <0x6e>
7706 ...
7707 <1><75>: Abbrev Number: 7 (DW_TAG_base_type)
7708 <76> DW_AT_byte_size : 4
7709 <77> DW_AT_encoding : 5 (signed)
7710
7711 imports the wrong die ( 0x75 instead of 0x58 ).
7712 This case will be ignored until the gcc bug is fixed. */
7713 return;
7714 }
7715
82856980
SW
7716 /* Figure out the local name after import. */
7717 import_alias = dwarf2_name (die, cu);
27aa8d6a 7718
794684b6
SW
7719 /* Figure out where the statement is being imported to. */
7720 import_prefix = determine_prefix (die, cu);
7721
7722 /* Figure out what the scope of the imported die is and prepend it
7723 to the name of the imported die. */
de4affc9 7724 imported_name_prefix = determine_prefix (imported_die, imported_cu);
794684b6 7725
f55ee35c
JK
7726 if (imported_die->tag != DW_TAG_namespace
7727 && imported_die->tag != DW_TAG_module)
794684b6 7728 {
13387711
SW
7729 imported_declaration = imported_name;
7730 canonical_name = imported_name_prefix;
794684b6 7731 }
13387711 7732 else if (strlen (imported_name_prefix) > 0)
794684b6 7733 {
13387711
SW
7734 temp = alloca (strlen (imported_name_prefix)
7735 + 2 + strlen (imported_name) + 1);
7736 strcpy (temp, imported_name_prefix);
7737 strcat (temp, "::");
7738 strcat (temp, imported_name);
7739 canonical_name = temp;
794684b6 7740 }
13387711
SW
7741 else
7742 canonical_name = imported_name;
794684b6 7743
32019081
JK
7744 cleanups = make_cleanup (VEC_cleanup (const_char_ptr), &excludes);
7745
7746 if (die->tag == DW_TAG_imported_module && cu->language == language_fortran)
7747 for (child_die = die->child; child_die && child_die->tag;
7748 child_die = sibling_die (child_die))
7749 {
7750 /* DWARF-4: A Fortran use statement with a “rename list” may be
7751 represented by an imported module entry with an import attribute
7752 referring to the module and owned entries corresponding to those
7753 entities that are renamed as part of being imported. */
7754
7755 if (child_die->tag != DW_TAG_imported_declaration)
7756 {
7757 complaint (&symfile_complaints,
7758 _("child DW_TAG_imported_declaration expected "
7759 "- DIE at 0x%x [in module %s]"),
b64f50a1 7760 child_die->offset.sect_off, objfile->name);
32019081
JK
7761 continue;
7762 }
7763
7764 import_attr = dwarf2_attr (child_die, DW_AT_import, cu);
7765 if (import_attr == NULL)
7766 {
7767 complaint (&symfile_complaints, _("Tag '%s' has no DW_AT_import"),
7768 dwarf_tag_name (child_die->tag));
7769 continue;
7770 }
7771
7772 imported_cu = cu;
7773 imported_die = follow_die_ref_or_sig (child_die, import_attr,
7774 &imported_cu);
7775 imported_name = dwarf2_name (imported_die, imported_cu);
7776 if (imported_name == NULL)
7777 {
7778 complaint (&symfile_complaints,
7779 _("child DW_TAG_imported_declaration has unknown "
7780 "imported name - DIE at 0x%x [in module %s]"),
b64f50a1 7781 child_die->offset.sect_off, objfile->name);
32019081
JK
7782 continue;
7783 }
7784
7785 VEC_safe_push (const_char_ptr, excludes, imported_name);
7786
7787 process_die (child_die, cu);
7788 }
7789
c0cc3a76
SW
7790 cp_add_using_directive (import_prefix,
7791 canonical_name,
7792 import_alias,
13387711 7793 imported_declaration,
32019081 7794 excludes,
bb5ed363 7795 &objfile->objfile_obstack);
32019081
JK
7796
7797 do_cleanups (cleanups);
27aa8d6a
SW
7798}
7799
f4dc4d17 7800/* Cleanup function for handle_DW_AT_stmt_list. */
ae2de4f8 7801
cb1df416
DJ
7802static void
7803free_cu_line_header (void *arg)
7804{
7805 struct dwarf2_cu *cu = arg;
7806
7807 free_line_header (cu->line_header);
7808 cu->line_header = NULL;
7809}
7810
1b80a9fa
JK
7811/* Check for possibly missing DW_AT_comp_dir with relative .debug_line
7812 directory paths. GCC SVN r127613 (new option -fdebug-prefix-map) fixed
7813 this, it was first present in GCC release 4.3.0. */
7814
7815static int
7816producer_is_gcc_lt_4_3 (struct dwarf2_cu *cu)
7817{
7818 if (!cu->checked_producer)
7819 check_producer (cu);
7820
7821 return cu->producer_is_gcc_lt_4_3;
7822}
7823
9291a0cd
TT
7824static void
7825find_file_and_directory (struct die_info *die, struct dwarf2_cu *cu,
7826 char **name, char **comp_dir)
7827{
7828 struct attribute *attr;
7829
7830 *name = NULL;
7831 *comp_dir = NULL;
7832
7833 /* Find the filename. Do not use dwarf2_name here, since the filename
7834 is not a source language identifier. */
7835 attr = dwarf2_attr (die, DW_AT_name, cu);
7836 if (attr)
7837 {
7838 *name = DW_STRING (attr);
7839 }
7840
7841 attr = dwarf2_attr (die, DW_AT_comp_dir, cu);
7842 if (attr)
7843 *comp_dir = DW_STRING (attr);
1b80a9fa
JK
7844 else if (producer_is_gcc_lt_4_3 (cu) && *name != NULL
7845 && IS_ABSOLUTE_PATH (*name))
9291a0cd
TT
7846 {
7847 *comp_dir = ldirname (*name);
7848 if (*comp_dir != NULL)
7849 make_cleanup (xfree, *comp_dir);
7850 }
7851 if (*comp_dir != NULL)
7852 {
7853 /* Irix 6.2 native cc prepends <machine>.: to the compilation
7854 directory, get rid of it. */
7855 char *cp = strchr (*comp_dir, ':');
7856
7857 if (cp && cp != *comp_dir && cp[-1] == '.' && cp[1] == '/')
7858 *comp_dir = cp + 1;
7859 }
7860
7861 if (*name == NULL)
7862 *name = "<unknown>";
7863}
7864
f4dc4d17
DE
7865/* Handle DW_AT_stmt_list for a compilation unit.
7866 DIE is the DW_TAG_compile_unit die for CU.
f3f5162e
DE
7867 COMP_DIR is the compilation directory.
7868 WANT_LINE_INFO is non-zero if the pc/line-number mapping is needed. */
2ab95328
TT
7869
7870static void
7871handle_DW_AT_stmt_list (struct die_info *die, struct dwarf2_cu *cu,
f4dc4d17 7872 const char *comp_dir)
2ab95328
TT
7873{
7874 struct attribute *attr;
2ab95328 7875
f4dc4d17
DE
7876 gdb_assert (! cu->per_cu->is_debug_types);
7877
2ab95328
TT
7878 attr = dwarf2_attr (die, DW_AT_stmt_list, cu);
7879 if (attr)
7880 {
7881 unsigned int line_offset = DW_UNSND (attr);
7882 struct line_header *line_header
3019eac3 7883 = dwarf_decode_line_header (line_offset, cu);
2ab95328
TT
7884
7885 if (line_header)
dee91e82
DE
7886 {
7887 cu->line_header = line_header;
7888 make_cleanup (free_cu_line_header, cu);
f4dc4d17 7889 dwarf_decode_lines (line_header, comp_dir, cu, NULL, 1);
dee91e82 7890 }
2ab95328
TT
7891 }
7892}
7893
95554aad 7894/* Process DW_TAG_compile_unit or DW_TAG_partial_unit. */
ae2de4f8 7895
c906108c 7896static void
e7c27a73 7897read_file_scope (struct die_info *die, struct dwarf2_cu *cu)
c906108c 7898{
dee91e82 7899 struct objfile *objfile = dwarf2_per_objfile->objfile;
debd256d 7900 struct cleanup *back_to = make_cleanup (null_cleanup, 0);
2acceee2 7901 CORE_ADDR lowpc = ((CORE_ADDR) -1);
c906108c
SS
7902 CORE_ADDR highpc = ((CORE_ADDR) 0);
7903 struct attribute *attr;
e1024ff1 7904 char *name = NULL;
c906108c
SS
7905 char *comp_dir = NULL;
7906 struct die_info *child_die;
7907 bfd *abfd = objfile->obfd;
e142c38c 7908 CORE_ADDR baseaddr;
6e70227d 7909
e142c38c 7910 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
c906108c 7911
fae299cd 7912 get_scope_pc_bounds (die, &lowpc, &highpc, cu);
c906108c
SS
7913
7914 /* If we didn't find a lowpc, set it to highpc to avoid complaints
7915 from finish_block. */
2acceee2 7916 if (lowpc == ((CORE_ADDR) -1))
c906108c
SS
7917 lowpc = highpc;
7918 lowpc += baseaddr;
7919 highpc += baseaddr;
7920
9291a0cd 7921 find_file_and_directory (die, cu, &name, &comp_dir);
e1024ff1 7922
95554aad 7923 prepare_one_comp_unit (cu, die, cu->language);
303b6f5d 7924
f4b8a18d
KW
7925 /* The XLCL doesn't generate DW_LANG_OpenCL because this attribute is not
7926 standardised yet. As a workaround for the language detection we fall
7927 back to the DW_AT_producer string. */
7928 if (cu->producer && strstr (cu->producer, "IBM XL C for OpenCL") != NULL)
7929 cu->language = language_opencl;
7930
3019eac3
DE
7931 /* Similar hack for Go. */
7932 if (cu->producer && strstr (cu->producer, "GNU Go ") != NULL)
7933 set_cu_language (DW_LANG_Go, cu);
7934
f4dc4d17 7935 dwarf2_start_symtab (cu, name, comp_dir, lowpc);
3019eac3
DE
7936
7937 /* Decode line number information if present. We do this before
7938 processing child DIEs, so that the line header table is available
7939 for DW_AT_decl_file. */
f4dc4d17 7940 handle_DW_AT_stmt_list (die, cu, comp_dir);
3019eac3
DE
7941
7942 /* Process all dies in compilation unit. */
7943 if (die->child != NULL)
7944 {
7945 child_die = die->child;
7946 while (child_die && child_die->tag)
7947 {
7948 process_die (child_die, cu);
7949 child_die = sibling_die (child_die);
7950 }
7951 }
7952
7953 /* Decode macro information, if present. Dwarf 2 macro information
7954 refers to information in the line number info statement program
7955 header, so we can only read it if we've read the header
7956 successfully. */
7957 attr = dwarf2_attr (die, DW_AT_GNU_macros, cu);
7958 if (attr && cu->line_header)
7959 {
7960 if (dwarf2_attr (die, DW_AT_macro_info, cu))
7961 complaint (&symfile_complaints,
7962 _("CU refers to both DW_AT_GNU_macros and DW_AT_macro_info"));
7963
09262596 7964 dwarf_decode_macros (cu, DW_UNSND (attr), comp_dir, 1);
3019eac3
DE
7965 }
7966 else
7967 {
7968 attr = dwarf2_attr (die, DW_AT_macro_info, cu);
7969 if (attr && cu->line_header)
7970 {
7971 unsigned int macro_offset = DW_UNSND (attr);
7972
09262596 7973 dwarf_decode_macros (cu, macro_offset, comp_dir, 0);
3019eac3
DE
7974 }
7975 }
7976
7977 do_cleanups (back_to);
7978}
7979
f4dc4d17
DE
7980/* TU version of handle_DW_AT_stmt_list for read_type_unit_scope.
7981 Create the set of symtabs used by this TU, or if this TU is sharing
7982 symtabs with another TU and the symtabs have already been created
7983 then restore those symtabs in the line header.
7984 We don't need the pc/line-number mapping for type units. */
3019eac3
DE
7985
7986static void
f4dc4d17 7987setup_type_unit_groups (struct die_info *die, struct dwarf2_cu *cu)
3019eac3 7988{
f4dc4d17
DE
7989 struct objfile *objfile = dwarf2_per_objfile->objfile;
7990 struct dwarf2_per_cu_data *per_cu = cu->per_cu;
7991 struct type_unit_group *tu_group;
7992 int first_time;
7993 struct line_header *lh;
3019eac3 7994 struct attribute *attr;
f4dc4d17 7995 unsigned int i, line_offset;
3019eac3 7996
f4dc4d17 7997 gdb_assert (per_cu->is_debug_types);
3019eac3 7998
f4dc4d17 7999 attr = dwarf2_attr (die, DW_AT_stmt_list, cu);
3019eac3 8000
f4dc4d17
DE
8001 /* If we're using .gdb_index (includes -readnow) then
8002 per_cu->s.type_unit_group may not have been set up yet. */
8003 if (per_cu->s.type_unit_group == NULL)
094b34ac 8004 per_cu->s.type_unit_group = get_type_unit_group (cu, attr);
f4dc4d17
DE
8005 tu_group = per_cu->s.type_unit_group;
8006
8007 /* If we've already processed this stmt_list there's no real need to
8008 do it again, we could fake it and just recreate the part we need
8009 (file name,index -> symtab mapping). If data shows this optimization
8010 is useful we can do it then. */
8011 first_time = tu_group->primary_symtab == NULL;
8012
8013 /* We have to handle the case of both a missing DW_AT_stmt_list or bad
8014 debug info. */
8015 lh = NULL;
8016 if (attr != NULL)
3019eac3 8017 {
f4dc4d17
DE
8018 line_offset = DW_UNSND (attr);
8019 lh = dwarf_decode_line_header (line_offset, cu);
8020 }
8021 if (lh == NULL)
8022 {
8023 if (first_time)
8024 dwarf2_start_symtab (cu, "", NULL, 0);
8025 else
8026 {
8027 gdb_assert (tu_group->symtabs == NULL);
8028 restart_symtab (0);
8029 }
8030 /* Note: The primary symtab will get allocated at the end. */
8031 return;
3019eac3
DE
8032 }
8033
f4dc4d17
DE
8034 cu->line_header = lh;
8035 make_cleanup (free_cu_line_header, cu);
3019eac3 8036
f4dc4d17
DE
8037 if (first_time)
8038 {
8039 dwarf2_start_symtab (cu, "", NULL, 0);
3019eac3 8040
f4dc4d17
DE
8041 tu_group->num_symtabs = lh->num_file_names;
8042 tu_group->symtabs = XNEWVEC (struct symtab *, lh->num_file_names);
3019eac3 8043
f4dc4d17
DE
8044 for (i = 0; i < lh->num_file_names; ++i)
8045 {
8046 char *dir = NULL;
8047 struct file_entry *fe = &lh->file_names[i];
3019eac3 8048
f4dc4d17
DE
8049 if (fe->dir_index)
8050 dir = lh->include_dirs[fe->dir_index - 1];
8051 dwarf2_start_subfile (fe->name, dir, NULL);
3019eac3 8052
f4dc4d17
DE
8053 /* Note: We don't have to watch for the main subfile here, type units
8054 don't have DW_AT_name. */
3019eac3 8055
f4dc4d17
DE
8056 if (current_subfile->symtab == NULL)
8057 {
8058 /* NOTE: start_subfile will recognize when it's been passed
8059 a file it has already seen. So we can't assume there's a
8060 simple mapping from lh->file_names to subfiles,
8061 lh->file_names may contain dups. */
8062 current_subfile->symtab = allocate_symtab (current_subfile->name,
8063 objfile);
8064 }
8065
8066 fe->symtab = current_subfile->symtab;
8067 tu_group->symtabs[i] = fe->symtab;
8068 }
8069 }
8070 else
3019eac3 8071 {
f4dc4d17
DE
8072 restart_symtab (0);
8073
8074 for (i = 0; i < lh->num_file_names; ++i)
8075 {
8076 struct file_entry *fe = &lh->file_names[i];
8077
8078 fe->symtab = tu_group->symtabs[i];
8079 }
3019eac3
DE
8080 }
8081
f4dc4d17
DE
8082 /* The main symtab is allocated last. Type units don't have DW_AT_name
8083 so they don't have a "real" (so to speak) symtab anyway.
8084 There is later code that will assign the main symtab to all symbols
8085 that don't have one. We need to handle the case of a symbol with a
8086 missing symtab (DW_AT_decl_file) anyway. */
8087}
3019eac3 8088
f4dc4d17
DE
8089/* Process DW_TAG_type_unit.
8090 For TUs we want to skip the first top level sibling if it's not the
8091 actual type being defined by this TU. In this case the first top
8092 level sibling is there to provide context only. */
3019eac3 8093
f4dc4d17
DE
8094static void
8095read_type_unit_scope (struct die_info *die, struct dwarf2_cu *cu)
8096{
8097 struct die_info *child_die;
3019eac3 8098
f4dc4d17
DE
8099 prepare_one_comp_unit (cu, die, language_minimal);
8100
8101 /* Initialize (or reinitialize) the machinery for building symtabs.
8102 We do this before processing child DIEs, so that the line header table
8103 is available for DW_AT_decl_file. */
8104 setup_type_unit_groups (die, cu);
8105
8106 if (die->child != NULL)
8107 {
8108 child_die = die->child;
8109 while (child_die && child_die->tag)
8110 {
8111 process_die (child_die, cu);
8112 child_die = sibling_die (child_die);
8113 }
8114 }
3019eac3
DE
8115}
8116\f
80626a55
DE
8117/* DWO/DWP files.
8118
8119 http://gcc.gnu.org/wiki/DebugFission
8120 http://gcc.gnu.org/wiki/DebugFissionDWP
8121
8122 To simplify handling of both DWO files ("object" files with the DWARF info)
8123 and DWP files (a file with the DWOs packaged up into one file), we treat
8124 DWP files as having a collection of virtual DWO files. */
3019eac3
DE
8125
8126static hashval_t
8127hash_dwo_file (const void *item)
8128{
8129 const struct dwo_file *dwo_file = item;
8130
80626a55 8131 return htab_hash_string (dwo_file->name);
3019eac3
DE
8132}
8133
8134static int
8135eq_dwo_file (const void *item_lhs, const void *item_rhs)
8136{
8137 const struct dwo_file *lhs = item_lhs;
8138 const struct dwo_file *rhs = item_rhs;
8139
80626a55 8140 return strcmp (lhs->name, rhs->name) == 0;
3019eac3
DE
8141}
8142
8143/* Allocate a hash table for DWO files. */
8144
8145static htab_t
8146allocate_dwo_file_hash_table (void)
8147{
8148 struct objfile *objfile = dwarf2_per_objfile->objfile;
8149
8150 return htab_create_alloc_ex (41,
8151 hash_dwo_file,
8152 eq_dwo_file,
8153 NULL,
8154 &objfile->objfile_obstack,
8155 hashtab_obstack_allocate,
8156 dummy_obstack_deallocate);
8157}
8158
80626a55
DE
8159/* Lookup DWO file DWO_NAME. */
8160
8161static void **
8162lookup_dwo_file_slot (const char *dwo_name)
8163{
8164 struct dwo_file find_entry;
8165 void **slot;
8166
8167 if (dwarf2_per_objfile->dwo_files == NULL)
8168 dwarf2_per_objfile->dwo_files = allocate_dwo_file_hash_table ();
8169
8170 memset (&find_entry, 0, sizeof (find_entry));
8171 find_entry.name = dwo_name;
8172 slot = htab_find_slot (dwarf2_per_objfile->dwo_files, &find_entry, INSERT);
8173
8174 return slot;
8175}
8176
3019eac3
DE
8177static hashval_t
8178hash_dwo_unit (const void *item)
8179{
8180 const struct dwo_unit *dwo_unit = item;
8181
8182 /* This drops the top 32 bits of the id, but is ok for a hash. */
8183 return dwo_unit->signature;
8184}
8185
8186static int
8187eq_dwo_unit (const void *item_lhs, const void *item_rhs)
8188{
8189 const struct dwo_unit *lhs = item_lhs;
8190 const struct dwo_unit *rhs = item_rhs;
8191
8192 /* The signature is assumed to be unique within the DWO file.
8193 So while object file CU dwo_id's always have the value zero,
8194 that's OK, assuming each object file DWO file has only one CU,
8195 and that's the rule for now. */
8196 return lhs->signature == rhs->signature;
8197}
8198
8199/* Allocate a hash table for DWO CUs,TUs.
8200 There is one of these tables for each of CUs,TUs for each DWO file. */
8201
8202static htab_t
8203allocate_dwo_unit_table (struct objfile *objfile)
8204{
8205 /* Start out with a pretty small number.
8206 Generally DWO files contain only one CU and maybe some TUs. */
8207 return htab_create_alloc_ex (3,
8208 hash_dwo_unit,
8209 eq_dwo_unit,
8210 NULL,
8211 &objfile->objfile_obstack,
8212 hashtab_obstack_allocate,
8213 dummy_obstack_deallocate);
8214}
8215
80626a55 8216/* Structure used to pass data to create_dwo_debug_info_hash_table_reader. */
3019eac3
DE
8217
8218struct create_dwo_info_table_data
8219{
8220 struct dwo_file *dwo_file;
8221 htab_t cu_htab;
8222};
8223
80626a55 8224/* die_reader_func for create_dwo_debug_info_hash_table. */
3019eac3
DE
8225
8226static void
80626a55
DE
8227create_dwo_debug_info_hash_table_reader (const struct die_reader_specs *reader,
8228 gdb_byte *info_ptr,
8229 struct die_info *comp_unit_die,
8230 int has_children,
8231 void *datap)
3019eac3
DE
8232{
8233 struct dwarf2_cu *cu = reader->cu;
8234 struct objfile *objfile = dwarf2_per_objfile->objfile;
8235 sect_offset offset = cu->per_cu->offset;
8236 struct dwarf2_section_info *section = cu->per_cu->info_or_types_section;
8237 struct create_dwo_info_table_data *data = datap;
8238 struct dwo_file *dwo_file = data->dwo_file;
8239 htab_t cu_htab = data->cu_htab;
8240 void **slot;
8241 struct attribute *attr;
8242 struct dwo_unit *dwo_unit;
8243
8244 attr = dwarf2_attr (comp_unit_die, DW_AT_GNU_dwo_id, cu);
8245 if (attr == NULL)
8246 {
8247 error (_("Dwarf Error: debug entry at offset 0x%x is missing"
8248 " its dwo_id [in module %s]"),
80626a55 8249 offset.sect_off, dwo_file->name);
3019eac3
DE
8250 return;
8251 }
8252
8253 dwo_unit = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct dwo_unit);
8254 dwo_unit->dwo_file = dwo_file;
8255 dwo_unit->signature = DW_UNSND (attr);
8256 dwo_unit->info_or_types_section = section;
8257 dwo_unit->offset = offset;
8258 dwo_unit->length = cu->per_cu->length;
8259
8260 slot = htab_find_slot (cu_htab, dwo_unit, INSERT);
8261 gdb_assert (slot != NULL);
8262 if (*slot != NULL)
8263 {
8264 const struct dwo_unit *dup_dwo_unit = *slot;
8265
8266 complaint (&symfile_complaints,
8267 _("debug entry at offset 0x%x is duplicate to the entry at"
8268 " offset 0x%x, dwo_id 0x%s [in module %s]"),
8269 offset.sect_off, dup_dwo_unit->offset.sect_off,
8270 phex (dwo_unit->signature, sizeof (dwo_unit->signature)),
80626a55 8271 dwo_file->name);
3019eac3
DE
8272 }
8273 else
8274 *slot = dwo_unit;
8275
09406207 8276 if (dwarf2_read_debug)
3019eac3
DE
8277 fprintf_unfiltered (gdb_stdlog, " offset 0x%x, dwo_id 0x%s\n",
8278 offset.sect_off,
8279 phex (dwo_unit->signature,
8280 sizeof (dwo_unit->signature)));
8281}
8282
80626a55
DE
8283/* Create a hash table to map DWO IDs to their CU entry in
8284 .debug_info.dwo in DWO_FILE.
8285 Note: This function processes DWO files only, not DWP files. */
3019eac3
DE
8286
8287static htab_t
80626a55 8288create_dwo_debug_info_hash_table (struct dwo_file *dwo_file)
3019eac3
DE
8289{
8290 struct objfile *objfile = dwarf2_per_objfile->objfile;
8291 struct dwarf2_section_info *section = &dwo_file->sections.info;
8292 bfd *abfd;
8293 htab_t cu_htab;
8294 gdb_byte *info_ptr, *end_ptr;
8295 struct create_dwo_info_table_data create_dwo_info_table_data;
8296
8297 dwarf2_read_section (objfile, section);
8298 info_ptr = section->buffer;
8299
8300 if (info_ptr == NULL)
8301 return NULL;
8302
8303 /* We can't set abfd until now because the section may be empty or
8304 not present, in which case section->asection will be NULL. */
8305 abfd = section->asection->owner;
8306
09406207 8307 if (dwarf2_read_debug)
3019eac3
DE
8308 fprintf_unfiltered (gdb_stdlog, "Reading .debug_info.dwo for %s:\n",
8309 bfd_get_filename (abfd));
8310
8311 cu_htab = allocate_dwo_unit_table (objfile);
8312
8313 create_dwo_info_table_data.dwo_file = dwo_file;
8314 create_dwo_info_table_data.cu_htab = cu_htab;
8315
8316 end_ptr = info_ptr + section->size;
8317 while (info_ptr < end_ptr)
8318 {
8319 struct dwarf2_per_cu_data per_cu;
8320
8321 memset (&per_cu, 0, sizeof (per_cu));
8322 per_cu.objfile = objfile;
8323 per_cu.is_debug_types = 0;
8324 per_cu.offset.sect_off = info_ptr - section->buffer;
8325 per_cu.info_or_types_section = section;
8326
8327 init_cutu_and_read_dies_no_follow (&per_cu,
8328 &dwo_file->sections.abbrev,
8329 dwo_file,
80626a55 8330 create_dwo_debug_info_hash_table_reader,
3019eac3
DE
8331 &create_dwo_info_table_data);
8332
8333 info_ptr += per_cu.length;
8334 }
8335
8336 return cu_htab;
8337}
8338
80626a55
DE
8339/* DWP file .debug_{cu,tu}_index section format:
8340 [ref: http://gcc.gnu.org/wiki/DebugFissionDWP]
8341
8342 Both index sections have the same format, and serve to map a 64-bit
8343 signature to a set of section numbers. Each section begins with a header,
8344 followed by a hash table of 64-bit signatures, a parallel table of 32-bit
8345 indexes, and a pool of 32-bit section numbers. The index sections will be
8346 aligned at 8-byte boundaries in the file.
8347
8348 The index section header contains two unsigned 32-bit values (using the
8349 byte order of the application binary):
8350
8351 N, the number of compilation units or type units in the index
8352 M, the number of slots in the hash table
8353
8354 (We assume that N and M will not exceed 2^32 - 1.)
8355
8356 The size of the hash table, M, must be 2^k such that 2^k > 3*N/2.
8357
8358 The hash table begins at offset 8 in the section, and consists of an array
8359 of M 64-bit slots. Each slot contains a 64-bit signature (using the byte
8360 order of the application binary). Unused slots in the hash table are 0.
8361 (We rely on the extreme unlikeliness of a signature being exactly 0.)
8362
8363 The parallel table begins immediately after the hash table
8364 (at offset 8 + 8 * M from the beginning of the section), and consists of an
8365 array of 32-bit indexes (using the byte order of the application binary),
8366 corresponding 1-1 with slots in the hash table. Each entry in the parallel
8367 table contains a 32-bit index into the pool of section numbers. For unused
8368 hash table slots, the corresponding entry in the parallel table will be 0.
8369
8370 Given a 64-bit compilation unit signature or a type signature S, an entry
8371 in the hash table is located as follows:
8372
8373 1) Calculate a primary hash H = S & MASK(k), where MASK(k) is a mask with
8374 the low-order k bits all set to 1.
8375
8376 2) Calculate a secondary hash H' = (((S >> 32) & MASK(k)) | 1).
8377
8378 3) If the hash table entry at index H matches the signature, use that
8379 entry. If the hash table entry at index H is unused (all zeroes),
8380 terminate the search: the signature is not present in the table.
8381
8382 4) Let H = (H + H') modulo M. Repeat at Step 3.
8383
8384 Because M > N and H' and M are relatively prime, the search is guaranteed
8385 to stop at an unused slot or find the match.
8386
8387 The pool of section numbers begins immediately following the hash table
8388 (at offset 8 + 12 * M from the beginning of the section). The pool of
8389 section numbers consists of an array of 32-bit words (using the byte order
8390 of the application binary). Each item in the array is indexed starting
8391 from 0. The hash table entry provides the index of the first section
8392 number in the set. Additional section numbers in the set follow, and the
8393 set is terminated by a 0 entry (section number 0 is not used in ELF).
8394
8395 In each set of section numbers, the .debug_info.dwo or .debug_types.dwo
8396 section must be the first entry in the set, and the .debug_abbrev.dwo must
8397 be the second entry. Other members of the set may follow in any order. */
8398
8399/* Create a hash table to map DWO IDs to their CU/TU entry in
8400 .debug_{info,types}.dwo in DWP_FILE.
8401 Returns NULL if there isn't one.
8402 Note: This function processes DWP files only, not DWO files. */
8403
8404static struct dwp_hash_table *
8405create_dwp_hash_table (struct dwp_file *dwp_file, int is_debug_types)
8406{
8407 struct objfile *objfile = dwarf2_per_objfile->objfile;
8408 bfd *dbfd = dwp_file->dbfd;
8409 char *index_ptr, *index_end;
8410 struct dwarf2_section_info *index;
8411 uint32_t version, nr_units, nr_slots;
8412 struct dwp_hash_table *htab;
8413
8414 if (is_debug_types)
8415 index = &dwp_file->sections.tu_index;
8416 else
8417 index = &dwp_file->sections.cu_index;
8418
8419 if (dwarf2_section_empty_p (index))
8420 return NULL;
8421 dwarf2_read_section (objfile, index);
8422
8423 index_ptr = index->buffer;
8424 index_end = index_ptr + index->size;
8425
8426 version = read_4_bytes (dbfd, index_ptr);
8427 index_ptr += 8; /* Skip the unused word. */
8428 nr_units = read_4_bytes (dbfd, index_ptr);
8429 index_ptr += 4;
8430 nr_slots = read_4_bytes (dbfd, index_ptr);
8431 index_ptr += 4;
8432
8433 if (version != 1)
8434 {
8435 error (_("Dwarf Error: unsupported DWP file version (%u)"
8436 " [in module %s]"),
8437 version, dwp_file->name);
8438 }
8439 if (nr_slots != (nr_slots & -nr_slots))
8440 {
8441 error (_("Dwarf Error: number of slots in DWP hash table (%u)"
8442 " is not power of 2 [in module %s]"),
8443 nr_slots, dwp_file->name);
8444 }
8445
8446 htab = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct dwp_hash_table);
8447 htab->nr_units = nr_units;
8448 htab->nr_slots = nr_slots;
8449 htab->hash_table = index_ptr;
8450 htab->unit_table = htab->hash_table + sizeof (uint64_t) * nr_slots;
8451 htab->section_pool = htab->unit_table + sizeof (uint32_t) * nr_slots;
8452
8453 return htab;
8454}
8455
8456/* Update SECTIONS with the data from SECTP.
8457
8458 This function is like the other "locate" section routines that are
8459 passed to bfd_map_over_sections, but in this context the sections to
8460 read comes from the DWP hash table, not the full ELF section table.
8461
8462 The result is non-zero for success, or zero if an error was found. */
8463
8464static int
8465locate_virtual_dwo_sections (asection *sectp,
8466 struct virtual_dwo_sections *sections)
8467{
8468 const struct dwop_section_names *names = &dwop_section_names;
8469
8470 if (section_is_p (sectp->name, &names->abbrev_dwo))
8471 {
8472 /* There can be only one. */
8473 if (sections->abbrev.asection != NULL)
8474 return 0;
8475 sections->abbrev.asection = sectp;
8476 sections->abbrev.size = bfd_get_section_size (sectp);
8477 }
8478 else if (section_is_p (sectp->name, &names->info_dwo)
8479 || section_is_p (sectp->name, &names->types_dwo))
8480 {
8481 /* There can be only one. */
8482 if (sections->info_or_types.asection != NULL)
8483 return 0;
8484 sections->info_or_types.asection = sectp;
8485 sections->info_or_types.size = bfd_get_section_size (sectp);
8486 }
8487 else if (section_is_p (sectp->name, &names->line_dwo))
8488 {
8489 /* There can be only one. */
8490 if (sections->line.asection != NULL)
8491 return 0;
8492 sections->line.asection = sectp;
8493 sections->line.size = bfd_get_section_size (sectp);
8494 }
8495 else if (section_is_p (sectp->name, &names->loc_dwo))
8496 {
8497 /* There can be only one. */
8498 if (sections->loc.asection != NULL)
8499 return 0;
8500 sections->loc.asection = sectp;
8501 sections->loc.size = bfd_get_section_size (sectp);
8502 }
8503 else if (section_is_p (sectp->name, &names->macinfo_dwo))
8504 {
8505 /* There can be only one. */
8506 if (sections->macinfo.asection != NULL)
8507 return 0;
8508 sections->macinfo.asection = sectp;
8509 sections->macinfo.size = bfd_get_section_size (sectp);
8510 }
8511 else if (section_is_p (sectp->name, &names->macro_dwo))
8512 {
8513 /* There can be only one. */
8514 if (sections->macro.asection != NULL)
8515 return 0;
8516 sections->macro.asection = sectp;
8517 sections->macro.size = bfd_get_section_size (sectp);
8518 }
8519 else if (section_is_p (sectp->name, &names->str_offsets_dwo))
8520 {
8521 /* There can be only one. */
8522 if (sections->str_offsets.asection != NULL)
8523 return 0;
8524 sections->str_offsets.asection = sectp;
8525 sections->str_offsets.size = bfd_get_section_size (sectp);
8526 }
8527 else
8528 {
8529 /* No other kind of section is valid. */
8530 return 0;
8531 }
8532
8533 return 1;
8534}
8535
8536/* Create a dwo_unit object for the DWO with signature SIGNATURE.
8537 HTAB is the hash table from the DWP file.
8538 SECTION_INDEX is the index of the DWO in HTAB. */
8539
8540static struct dwo_unit *
8541create_dwo_in_dwp (struct dwp_file *dwp_file,
8542 const struct dwp_hash_table *htab,
8543 uint32_t section_index,
8544 ULONGEST signature, int is_debug_types)
8545{
8546 struct objfile *objfile = dwarf2_per_objfile->objfile;
8547 bfd *dbfd = dwp_file->dbfd;
8548 const char *kind = is_debug_types ? "TU" : "CU";
8549 struct dwo_file *dwo_file;
8550 struct dwo_unit *dwo_unit;
8551 struct virtual_dwo_sections sections;
8552 void **dwo_file_slot;
8553 char *virtual_dwo_name;
8554 struct dwarf2_section_info *cutu;
8555 struct cleanup *cleanups;
8556 int i;
8557
8558 if (dwarf2_read_debug)
8559 {
8560 fprintf_unfiltered (gdb_stdlog, "Reading %s %u/0x%s in DWP file: %s\n",
8561 kind,
8562 section_index, phex (signature, sizeof (signature)),
8563 dwp_file->name);
8564 }
8565
8566 /* Fetch the sections of this DWO.
8567 Put a limit on the number of sections we look for so that bad data
8568 doesn't cause us to loop forever. */
8569
8570#define MAX_NR_DWO_SECTIONS \
8571 (1 /* .debug_info or .debug_types */ \
8572 + 1 /* .debug_abbrev */ \
8573 + 1 /* .debug_line */ \
8574 + 1 /* .debug_loc */ \
8575 + 1 /* .debug_str_offsets */ \
8576 + 1 /* .debug_macro */ \
8577 + 1 /* .debug_macinfo */ \
8578 + 1 /* trailing zero */)
8579
8580 memset (&sections, 0, sizeof (sections));
8581 cleanups = make_cleanup (null_cleanup, 0);
8582
8583 for (i = 0; i < MAX_NR_DWO_SECTIONS; ++i)
8584 {
8585 asection *sectp;
8586 uint32_t section_nr =
8587 read_4_bytes (dbfd,
8588 htab->section_pool
8589 + (section_index + i) * sizeof (uint32_t));
8590
8591 if (section_nr == 0)
8592 break;
8593 if (section_nr >= dwp_file->num_sections)
8594 {
8595 error (_("Dwarf Error: bad DWP hash table, section number too large"
8596 " [in module %s]"),
8597 dwp_file->name);
8598 }
8599
8600 sectp = dwp_file->elf_sections[section_nr];
8601 if (! locate_virtual_dwo_sections (sectp, &sections))
8602 {
8603 error (_("Dwarf Error: bad DWP hash table, invalid section found"
8604 " [in module %s]"),
8605 dwp_file->name);
8606 }
8607 }
8608
8609 if (i < 2
8610 || sections.info_or_types.asection == NULL
8611 || sections.abbrev.asection == NULL)
8612 {
8613 error (_("Dwarf Error: bad DWP hash table, missing DWO sections"
8614 " [in module %s]"),
8615 dwp_file->name);
8616 }
8617 if (i == MAX_NR_DWO_SECTIONS)
8618 {
8619 error (_("Dwarf Error: bad DWP hash table, too many DWO sections"
8620 " [in module %s]"),
8621 dwp_file->name);
8622 }
8623
8624 /* It's easier for the rest of the code if we fake a struct dwo_file and
8625 have dwo_unit "live" in that. At least for now.
8626
8627 The DWP file can be made up of a random collection of CUs and TUs.
8628 However, for each CU + set of TUs that came from the same original
8629 DWO file, we want combine them back into a virtual DWO file to save space
8630 (fewer struct dwo_file objects to allocated). Remember that for really
8631 large apps there can be on the order of 8K CUs and 200K TUs, or more. */
8632
2792b94d
PM
8633 virtual_dwo_name =
8634 xstrprintf ("virtual-dwo/%d-%d-%d-%d",
8635 sections.abbrev.asection ? sections.abbrev.asection->id : 0,
8636 sections.line.asection ? sections.line.asection->id : 0,
8637 sections.loc.asection ? sections.loc.asection->id : 0,
8638 (sections.str_offsets.asection
8639 ? sections.str_offsets.asection->id
8640 : 0));
80626a55
DE
8641 make_cleanup (xfree, virtual_dwo_name);
8642 /* Can we use an existing virtual DWO file? */
8643 dwo_file_slot = lookup_dwo_file_slot (virtual_dwo_name);
8644 /* Create one if necessary. */
8645 if (*dwo_file_slot == NULL)
8646 {
8647 if (dwarf2_read_debug)
8648 {
8649 fprintf_unfiltered (gdb_stdlog, "Creating virtual DWO: %s\n",
8650 virtual_dwo_name);
8651 }
8652 dwo_file = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct dwo_file);
8653 dwo_file->name = obstack_copy0 (&objfile->objfile_obstack,
8654 virtual_dwo_name,
8655 strlen (virtual_dwo_name));
8656 dwo_file->sections.abbrev = sections.abbrev;
8657 dwo_file->sections.line = sections.line;
8658 dwo_file->sections.loc = sections.loc;
8659 dwo_file->sections.macinfo = sections.macinfo;
8660 dwo_file->sections.macro = sections.macro;
8661 dwo_file->sections.str_offsets = sections.str_offsets;
8662 /* The "str" section is global to the entire DWP file. */
8663 dwo_file->sections.str = dwp_file->sections.str;
8664 /* The info or types section is assigned later to dwo_unit,
8665 there's no need to record it in dwo_file.
8666 Also, we can't simply record type sections in dwo_file because
8667 we record a pointer into the vector in dwo_unit. As we collect more
8668 types we'll grow the vector and eventually have to reallocate space
8669 for it, invalidating all the pointers into the current copy. */
8670 *dwo_file_slot = dwo_file;
8671 }
8672 else
8673 {
8674 if (dwarf2_read_debug)
8675 {
8676 fprintf_unfiltered (gdb_stdlog, "Using existing virtual DWO: %s\n",
8677 virtual_dwo_name);
8678 }
8679 dwo_file = *dwo_file_slot;
8680 }
8681 do_cleanups (cleanups);
8682
8683 dwo_unit = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct dwo_unit);
8684 dwo_unit->dwo_file = dwo_file;
8685 dwo_unit->signature = signature;
8686 dwo_unit->info_or_types_section =
8687 obstack_alloc (&objfile->objfile_obstack,
8688 sizeof (struct dwarf2_section_info));
8689 *dwo_unit->info_or_types_section = sections.info_or_types;
8690 /* offset, length, type_offset_in_tu are set later. */
8691
8692 return dwo_unit;
8693}
8694
8695/* Lookup the DWO with SIGNATURE in DWP_FILE. */
8696
8697static struct dwo_unit *
8698lookup_dwo_in_dwp (struct dwp_file *dwp_file,
8699 const struct dwp_hash_table *htab,
8700 ULONGEST signature, int is_debug_types)
8701{
8702 bfd *dbfd = dwp_file->dbfd;
8703 uint32_t mask = htab->nr_slots - 1;
8704 uint32_t hash = signature & mask;
8705 uint32_t hash2 = ((signature >> 32) & mask) | 1;
8706 unsigned int i;
8707 void **slot;
8708 struct dwo_unit find_dwo_cu, *dwo_cu;
8709
8710 memset (&find_dwo_cu, 0, sizeof (find_dwo_cu));
8711 find_dwo_cu.signature = signature;
8712 slot = htab_find_slot (dwp_file->loaded_cutus, &find_dwo_cu, INSERT);
8713
8714 if (*slot != NULL)
8715 return *slot;
8716
8717 /* Use a for loop so that we don't loop forever on bad debug info. */
8718 for (i = 0; i < htab->nr_slots; ++i)
8719 {
8720 ULONGEST signature_in_table;
8721
8722 signature_in_table =
8723 read_8_bytes (dbfd, htab->hash_table + hash * sizeof (uint64_t));
8724 if (signature_in_table == signature)
8725 {
8726 uint32_t section_index =
8727 read_4_bytes (dbfd, htab->unit_table + hash * sizeof (uint32_t));
8728
8729 *slot = create_dwo_in_dwp (dwp_file, htab, section_index,
8730 signature, is_debug_types);
8731 return *slot;
8732 }
8733 if (signature_in_table == 0)
8734 return NULL;
8735 hash = (hash + hash2) & mask;
8736 }
8737
8738 error (_("Dwarf Error: bad DWP hash table, lookup didn't terminate"
8739 " [in module %s]"),
8740 dwp_file->name);
8741}
8742
8743/* Subroutine of open_dwop_file to simplify it.
3019eac3
DE
8744 Open the file specified by FILE_NAME and hand it off to BFD for
8745 preliminary analysis. Return a newly initialized bfd *, which
8746 includes a canonicalized copy of FILE_NAME.
80626a55 8747 If IS_DWP is TRUE, we're opening a DWP file, otherwise a DWO file.
3019eac3
DE
8748 In case of trouble, return NULL.
8749 NOTE: This function is derived from symfile_bfd_open. */
8750
8751static bfd *
80626a55 8752try_open_dwop_file (const char *file_name, int is_dwp)
3019eac3
DE
8753{
8754 bfd *sym_bfd;
80626a55 8755 int desc, flags;
3019eac3 8756 char *absolute_name;
3019eac3 8757
80626a55
DE
8758 flags = OPF_TRY_CWD_FIRST;
8759 if (is_dwp)
8760 flags |= OPF_SEARCH_IN_PATH;
8761 desc = openp (debug_file_directory, flags, file_name,
3019eac3
DE
8762 O_RDONLY | O_BINARY, &absolute_name);
8763 if (desc < 0)
8764 return NULL;
8765
bb397797 8766 sym_bfd = gdb_bfd_open (absolute_name, gnutarget, desc);
3019eac3
DE
8767 if (!sym_bfd)
8768 {
3019eac3
DE
8769 xfree (absolute_name);
8770 return NULL;
8771 }
a4453b7e 8772 xfree (absolute_name);
3019eac3
DE
8773 bfd_set_cacheable (sym_bfd, 1);
8774
8775 if (!bfd_check_format (sym_bfd, bfd_object))
8776 {
cbb099e8 8777 gdb_bfd_unref (sym_bfd); /* This also closes desc. */
3019eac3
DE
8778 return NULL;
8779 }
8780
3019eac3
DE
8781 return sym_bfd;
8782}
8783
80626a55 8784/* Try to open DWO/DWP file FILE_NAME.
3019eac3 8785 COMP_DIR is the DW_AT_comp_dir attribute.
80626a55 8786 If IS_DWP is TRUE, we're opening a DWP file, otherwise a DWO file.
3019eac3
DE
8787 The result is the bfd handle of the file.
8788 If there is a problem finding or opening the file, return NULL.
8789 Upon success, the canonicalized path of the file is stored in the bfd,
8790 same as symfile_bfd_open. */
8791
8792static bfd *
80626a55 8793open_dwop_file (const char *file_name, const char *comp_dir, int is_dwp)
3019eac3
DE
8794{
8795 bfd *abfd;
3019eac3 8796
80626a55
DE
8797 if (IS_ABSOLUTE_PATH (file_name))
8798 return try_open_dwop_file (file_name, is_dwp);
3019eac3
DE
8799
8800 /* Before trying the search path, try DWO_NAME in COMP_DIR. */
8801
8802 if (comp_dir != NULL)
8803 {
80626a55 8804 char *path_to_try = concat (comp_dir, SLASH_STRING, file_name, NULL);
3019eac3
DE
8805
8806 /* NOTE: If comp_dir is a relative path, this will also try the
8807 search path, which seems useful. */
80626a55 8808 abfd = try_open_dwop_file (path_to_try, is_dwp);
3019eac3
DE
8809 xfree (path_to_try);
8810 if (abfd != NULL)
8811 return abfd;
8812 }
8813
8814 /* That didn't work, try debug-file-directory, which, despite its name,
8815 is a list of paths. */
8816
8817 if (*debug_file_directory == '\0')
8818 return NULL;
8819
80626a55 8820 return try_open_dwop_file (file_name, is_dwp);
3019eac3
DE
8821}
8822
80626a55
DE
8823/* This function is mapped across the sections and remembers the offset and
8824 size of each of the DWO debugging sections we are interested in. */
8825
8826static void
8827dwarf2_locate_dwo_sections (bfd *abfd, asection *sectp, void *dwo_sections_ptr)
8828{
8829 struct dwo_sections *dwo_sections = dwo_sections_ptr;
8830 const struct dwop_section_names *names = &dwop_section_names;
8831
8832 if (section_is_p (sectp->name, &names->abbrev_dwo))
8833 {
8834 dwo_sections->abbrev.asection = sectp;
8835 dwo_sections->abbrev.size = bfd_get_section_size (sectp);
8836 }
8837 else if (section_is_p (sectp->name, &names->info_dwo))
8838 {
8839 dwo_sections->info.asection = sectp;
8840 dwo_sections->info.size = bfd_get_section_size (sectp);
8841 }
8842 else if (section_is_p (sectp->name, &names->line_dwo))
8843 {
8844 dwo_sections->line.asection = sectp;
8845 dwo_sections->line.size = bfd_get_section_size (sectp);
8846 }
8847 else if (section_is_p (sectp->name, &names->loc_dwo))
8848 {
8849 dwo_sections->loc.asection = sectp;
8850 dwo_sections->loc.size = bfd_get_section_size (sectp);
8851 }
8852 else if (section_is_p (sectp->name, &names->macinfo_dwo))
8853 {
8854 dwo_sections->macinfo.asection = sectp;
8855 dwo_sections->macinfo.size = bfd_get_section_size (sectp);
8856 }
8857 else if (section_is_p (sectp->name, &names->macro_dwo))
8858 {
8859 dwo_sections->macro.asection = sectp;
8860 dwo_sections->macro.size = bfd_get_section_size (sectp);
8861 }
8862 else if (section_is_p (sectp->name, &names->str_dwo))
8863 {
8864 dwo_sections->str.asection = sectp;
8865 dwo_sections->str.size = bfd_get_section_size (sectp);
8866 }
8867 else if (section_is_p (sectp->name, &names->str_offsets_dwo))
8868 {
8869 dwo_sections->str_offsets.asection = sectp;
8870 dwo_sections->str_offsets.size = bfd_get_section_size (sectp);
8871 }
8872 else if (section_is_p (sectp->name, &names->types_dwo))
8873 {
8874 struct dwarf2_section_info type_section;
8875
8876 memset (&type_section, 0, sizeof (type_section));
8877 type_section.asection = sectp;
8878 type_section.size = bfd_get_section_size (sectp);
8879 VEC_safe_push (dwarf2_section_info_def, dwo_sections->types,
8880 &type_section);
8881 }
8882}
8883
8884/* Initialize the use of the DWO file specified by DWO_NAME.
8885 The result is NULL if DWO_NAME can't be found. */
3019eac3
DE
8886
8887static struct dwo_file *
80626a55 8888open_and_init_dwo_file (const char *dwo_name, const char *comp_dir)
3019eac3
DE
8889{
8890 struct objfile *objfile = dwarf2_per_objfile->objfile;
80626a55
DE
8891 struct dwo_file *dwo_file;
8892 bfd *dbfd;
3019eac3
DE
8893 struct cleanup *cleanups;
8894
80626a55
DE
8895 dbfd = open_dwop_file (dwo_name, comp_dir, 0);
8896 if (dbfd == NULL)
8897 {
8898 if (dwarf2_read_debug)
8899 fprintf_unfiltered (gdb_stdlog, "DWO file not found: %s\n", dwo_name);
8900 return NULL;
8901 }
8902 dwo_file = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct dwo_file);
8903 dwo_file->name = obstack_copy0 (&objfile->objfile_obstack,
8904 dwo_name, strlen (dwo_name));
8905 dwo_file->dbfd = dbfd;
3019eac3
DE
8906
8907 cleanups = make_cleanup (free_dwo_file_cleanup, dwo_file);
8908
80626a55 8909 bfd_map_over_sections (dbfd, dwarf2_locate_dwo_sections, &dwo_file->sections);
3019eac3 8910
80626a55 8911 dwo_file->cus = create_dwo_debug_info_hash_table (dwo_file);
3019eac3
DE
8912
8913 dwo_file->tus = create_debug_types_hash_table (dwo_file,
8914 dwo_file->sections.types);
8915
8916 discard_cleanups (cleanups);
8917
80626a55
DE
8918 if (dwarf2_read_debug)
8919 fprintf_unfiltered (gdb_stdlog, "DWO file found: %s\n", dwo_name);
8920
3019eac3
DE
8921 return dwo_file;
8922}
8923
80626a55
DE
8924/* This function is mapped across the sections and remembers the offset and
8925 size of each of the DWP debugging sections we are interested in. */
3019eac3 8926
80626a55
DE
8927static void
8928dwarf2_locate_dwp_sections (bfd *abfd, asection *sectp, void *dwp_file_ptr)
3019eac3 8929{
80626a55
DE
8930 struct dwp_file *dwp_file = dwp_file_ptr;
8931 const struct dwop_section_names *names = &dwop_section_names;
8932 unsigned int elf_section_nr = elf_section_data (sectp)->this_idx;
3019eac3 8933
80626a55
DE
8934 /* Record the ELF section number for later lookup: this is what the
8935 .debug_cu_index,.debug_tu_index tables use. */
8936 gdb_assert (elf_section_nr < dwp_file->num_sections);
8937 dwp_file->elf_sections[elf_section_nr] = sectp;
3019eac3 8938
80626a55
DE
8939 /* Look for specific sections that we need. */
8940 if (section_is_p (sectp->name, &names->str_dwo))
8941 {
8942 dwp_file->sections.str.asection = sectp;
8943 dwp_file->sections.str.size = bfd_get_section_size (sectp);
8944 }
8945 else if (section_is_p (sectp->name, &names->cu_index))
8946 {
8947 dwp_file->sections.cu_index.asection = sectp;
8948 dwp_file->sections.cu_index.size = bfd_get_section_size (sectp);
8949 }
8950 else if (section_is_p (sectp->name, &names->tu_index))
8951 {
8952 dwp_file->sections.tu_index.asection = sectp;
8953 dwp_file->sections.tu_index.size = bfd_get_section_size (sectp);
8954 }
8955}
3019eac3 8956
80626a55 8957/* Hash function for dwp_file loaded CUs/TUs. */
3019eac3 8958
80626a55
DE
8959static hashval_t
8960hash_dwp_loaded_cutus (const void *item)
8961{
8962 const struct dwo_unit *dwo_unit = item;
3019eac3 8963
80626a55
DE
8964 /* This drops the top 32 bits of the signature, but is ok for a hash. */
8965 return dwo_unit->signature;
3019eac3
DE
8966}
8967
80626a55 8968/* Equality function for dwp_file loaded CUs/TUs. */
3019eac3 8969
80626a55
DE
8970static int
8971eq_dwp_loaded_cutus (const void *a, const void *b)
3019eac3 8972{
80626a55
DE
8973 const struct dwo_unit *dua = a;
8974 const struct dwo_unit *dub = b;
3019eac3 8975
80626a55
DE
8976 return dua->signature == dub->signature;
8977}
3019eac3 8978
80626a55 8979/* Allocate a hash table for dwp_file loaded CUs/TUs. */
3019eac3 8980
80626a55
DE
8981static htab_t
8982allocate_dwp_loaded_cutus_table (struct objfile *objfile)
8983{
8984 return htab_create_alloc_ex (3,
8985 hash_dwp_loaded_cutus,
8986 eq_dwp_loaded_cutus,
8987 NULL,
8988 &objfile->objfile_obstack,
8989 hashtab_obstack_allocate,
8990 dummy_obstack_deallocate);
8991}
3019eac3 8992
80626a55
DE
8993/* Initialize the use of the DWP file for the current objfile.
8994 By convention the name of the DWP file is ${objfile}.dwp.
8995 The result is NULL if it can't be found. */
a766d390 8996
80626a55
DE
8997static struct dwp_file *
8998open_and_init_dwp_file (const char *comp_dir)
8999{
9000 struct objfile *objfile = dwarf2_per_objfile->objfile;
9001 struct dwp_file *dwp_file;
9002 char *dwp_name;
9003 bfd *dbfd;
9004 struct cleanup *cleanups;
9005
2792b94d 9006 dwp_name = xstrprintf ("%s.dwp", dwarf2_per_objfile->objfile->name);
80626a55
DE
9007 cleanups = make_cleanup (xfree, dwp_name);
9008
9009 dbfd = open_dwop_file (dwp_name, comp_dir, 1);
9010 if (dbfd == NULL)
9011 {
9012 if (dwarf2_read_debug)
9013 fprintf_unfiltered (gdb_stdlog, "DWP file not found: %s\n", dwp_name);
9014 do_cleanups (cleanups);
9015 return NULL;
3019eac3 9016 }
80626a55
DE
9017 dwp_file = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct dwp_file);
9018 dwp_file->name = obstack_copy0 (&objfile->objfile_obstack,
9019 dwp_name, strlen (dwp_name));
9020 dwp_file->dbfd = dbfd;
9021 do_cleanups (cleanups);
c906108c 9022
80626a55 9023 cleanups = make_cleanup (free_dwo_file_cleanup, dwp_file);
df8a16a1 9024
80626a55
DE
9025 /* +1: section 0 is unused */
9026 dwp_file->num_sections = bfd_count_sections (dbfd) + 1;
9027 dwp_file->elf_sections =
9028 OBSTACK_CALLOC (&objfile->objfile_obstack,
9029 dwp_file->num_sections, asection *);
9030
9031 bfd_map_over_sections (dbfd, dwarf2_locate_dwp_sections, dwp_file);
9032
9033 dwp_file->cus = create_dwp_hash_table (dwp_file, 0);
9034
9035 dwp_file->tus = create_dwp_hash_table (dwp_file, 1);
9036
9037 dwp_file->loaded_cutus = allocate_dwp_loaded_cutus_table (objfile);
9038
9039 discard_cleanups (cleanups);
9040
9041 if (dwarf2_read_debug)
9042 {
9043 fprintf_unfiltered (gdb_stdlog, "DWP file found: %s\n", dwp_file->name);
9044 fprintf_unfiltered (gdb_stdlog,
9045 " %u CUs, %u TUs\n",
9046 dwp_file->cus ? dwp_file->cus->nr_units : 0,
9047 dwp_file->tus ? dwp_file->tus->nr_units : 0);
9048 }
9049
9050 return dwp_file;
3019eac3 9051}
c906108c 9052
80626a55
DE
9053/* Subroutine of lookup_dwo_comp_unit, lookup_dwo_type_unit.
9054 Look up the CU/TU with signature SIGNATURE, either in DWO file DWO_NAME
9055 or in the DWP file for the objfile, referenced by THIS_UNIT.
3019eac3 9056 If non-NULL, comp_dir is the DW_AT_comp_dir attribute.
80626a55
DE
9057 IS_DEBUG_TYPES is non-zero if reading a TU, otherwise read a CU.
9058
9059 This is called, for example, when wanting to read a variable with a
9060 complex location. Therefore we don't want to do file i/o for every call.
9061 Therefore we don't want to look for a DWO file on every call.
9062 Therefore we first see if we've already seen SIGNATURE in a DWP file,
9063 then we check if we've already seen DWO_NAME, and only THEN do we check
9064 for a DWO file.
9065
1c658ad5 9066 The result is a pointer to the dwo_unit object or NULL if we didn't find it
80626a55 9067 (dwo_id mismatch or couldn't find the DWO/DWP file). */
debd256d 9068
3019eac3 9069static struct dwo_unit *
80626a55
DE
9070lookup_dwo_cutu (struct dwarf2_per_cu_data *this_unit,
9071 const char *dwo_name, const char *comp_dir,
9072 ULONGEST signature, int is_debug_types)
3019eac3
DE
9073{
9074 struct objfile *objfile = dwarf2_per_objfile->objfile;
80626a55
DE
9075 const char *kind = is_debug_types ? "TU" : "CU";
9076 void **dwo_file_slot;
3019eac3 9077 struct dwo_file *dwo_file;
80626a55 9078 struct dwp_file *dwp_file;
cb1df416 9079
80626a55 9080 /* Have we already read SIGNATURE from a DWP file? */
cf2c3c16 9081
80626a55
DE
9082 if (! dwarf2_per_objfile->dwp_checked)
9083 {
9084 dwarf2_per_objfile->dwp_file = open_and_init_dwp_file (comp_dir);
9085 dwarf2_per_objfile->dwp_checked = 1;
9086 }
9087 dwp_file = dwarf2_per_objfile->dwp_file;
3019eac3 9088
80626a55 9089 if (dwp_file != NULL)
cf2c3c16 9090 {
80626a55
DE
9091 const struct dwp_hash_table *dwp_htab =
9092 is_debug_types ? dwp_file->tus : dwp_file->cus;
9093
9094 if (dwp_htab != NULL)
9095 {
9096 struct dwo_unit *dwo_cutu =
9097 lookup_dwo_in_dwp (dwp_file, dwp_htab, signature, is_debug_types);
9098
9099 if (dwo_cutu != NULL)
9100 {
9101 if (dwarf2_read_debug)
9102 {
9103 fprintf_unfiltered (gdb_stdlog,
9104 "Virtual DWO %s %s found: @%s\n",
9105 kind, hex_string (signature),
9106 host_address_to_string (dwo_cutu));
9107 }
9108 return dwo_cutu;
9109 }
9110 }
9111 }
9112
9113 /* Have we already seen DWO_NAME? */
9114
9115 dwo_file_slot = lookup_dwo_file_slot (dwo_name);
9116 if (*dwo_file_slot == NULL)
9117 {
9118 /* Read in the file and build a table of the DWOs it contains. */
9119 *dwo_file_slot = open_and_init_dwo_file (dwo_name, comp_dir);
9120 }
9121 /* NOTE: This will be NULL if unable to open the file. */
9122 dwo_file = *dwo_file_slot;
9123
9124 if (dwo_file != NULL)
9125 {
9126 htab_t htab = is_debug_types ? dwo_file->tus : dwo_file->cus;
9127
9128 if (htab != NULL)
9129 {
9130 struct dwo_unit find_dwo_cutu, *dwo_cutu;
9a619af0 9131
80626a55
DE
9132 memset (&find_dwo_cutu, 0, sizeof (find_dwo_cutu));
9133 find_dwo_cutu.signature = signature;
9134 dwo_cutu = htab_find (htab, &find_dwo_cutu);
3019eac3 9135
80626a55
DE
9136 if (dwo_cutu != NULL)
9137 {
9138 if (dwarf2_read_debug)
9139 {
9140 fprintf_unfiltered (gdb_stdlog, "DWO %s %s(%s) found: @%s\n",
9141 kind, dwo_name, hex_string (signature),
9142 host_address_to_string (dwo_cutu));
9143 }
9144 return dwo_cutu;
9145 }
9146 }
2e276125 9147 }
9cdd5dbd 9148
80626a55
DE
9149 /* We didn't find it. This could mean a dwo_id mismatch, or
9150 someone deleted the DWO/DWP file, or the search path isn't set up
9151 correctly to find the file. */
9152
9153 if (dwarf2_read_debug)
9154 {
9155 fprintf_unfiltered (gdb_stdlog, "DWO %s %s(%s) not found\n",
9156 kind, dwo_name, hex_string (signature));
9157 }
3019eac3
DE
9158
9159 complaint (&symfile_complaints,
80626a55 9160 _("Could not find DWO CU referenced by CU at offset 0x%x"
3019eac3 9161 " [in module %s]"),
80626a55 9162 this_unit->offset.sect_off, objfile->name);
3019eac3 9163 return NULL;
5fb290d7
DJ
9164}
9165
80626a55
DE
9166/* Lookup the DWO CU DWO_NAME/SIGNATURE referenced from THIS_CU.
9167 See lookup_dwo_cutu_unit for details. */
9168
9169static struct dwo_unit *
9170lookup_dwo_comp_unit (struct dwarf2_per_cu_data *this_cu,
9171 const char *dwo_name, const char *comp_dir,
9172 ULONGEST signature)
9173{
9174 return lookup_dwo_cutu (this_cu, dwo_name, comp_dir, signature, 0);
9175}
9176
9177/* Lookup the DWO TU DWO_NAME/SIGNATURE referenced from THIS_TU.
9178 See lookup_dwo_cutu_unit for details. */
9179
9180static struct dwo_unit *
9181lookup_dwo_type_unit (struct signatured_type *this_tu,
9182 const char *dwo_name, const char *comp_dir)
9183{
9184 return lookup_dwo_cutu (&this_tu->per_cu, dwo_name, comp_dir, this_tu->signature, 1);
9185}
9186
3019eac3
DE
9187/* Free all resources associated with DWO_FILE.
9188 Close the DWO file and munmap the sections.
9189 All memory should be on the objfile obstack. */
348e048f
DE
9190
9191static void
3019eac3 9192free_dwo_file (struct dwo_file *dwo_file, struct objfile *objfile)
348e048f 9193{
3019eac3
DE
9194 int ix;
9195 struct dwarf2_section_info *section;
348e048f 9196
80626a55
DE
9197 gdb_assert (dwo_file->dbfd != objfile->obfd);
9198 gdb_bfd_unref (dwo_file->dbfd);
348e048f 9199
3019eac3
DE
9200 VEC_free (dwarf2_section_info_def, dwo_file->sections.types);
9201}
348e048f 9202
3019eac3 9203/* Wrapper for free_dwo_file for use in cleanups. */
348e048f 9204
3019eac3
DE
9205static void
9206free_dwo_file_cleanup (void *arg)
9207{
9208 struct dwo_file *dwo_file = (struct dwo_file *) arg;
9209 struct objfile *objfile = dwarf2_per_objfile->objfile;
348e048f 9210
3019eac3
DE
9211 free_dwo_file (dwo_file, objfile);
9212}
348e048f 9213
3019eac3 9214/* Traversal function for free_dwo_files. */
2ab95328 9215
3019eac3
DE
9216static int
9217free_dwo_file_from_slot (void **slot, void *info)
9218{
9219 struct dwo_file *dwo_file = (struct dwo_file *) *slot;
9220 struct objfile *objfile = (struct objfile *) info;
348e048f 9221
3019eac3 9222 free_dwo_file (dwo_file, objfile);
348e048f 9223
3019eac3
DE
9224 return 1;
9225}
348e048f 9226
3019eac3 9227/* Free all resources associated with DWO_FILES. */
348e048f 9228
3019eac3
DE
9229static void
9230free_dwo_files (htab_t dwo_files, struct objfile *objfile)
9231{
9232 htab_traverse_noresize (dwo_files, free_dwo_file_from_slot, objfile);
348e048f 9233}
3019eac3
DE
9234\f
9235/* Read in various DIEs. */
348e048f 9236
d389af10
JK
9237/* qsort helper for inherit_abstract_dies. */
9238
9239static int
9240unsigned_int_compar (const void *ap, const void *bp)
9241{
9242 unsigned int a = *(unsigned int *) ap;
9243 unsigned int b = *(unsigned int *) bp;
9244
9245 return (a > b) - (b > a);
9246}
9247
9248/* DW_AT_abstract_origin inherits whole DIEs (not just their attributes).
3e43a32a
MS
9249 Inherit only the children of the DW_AT_abstract_origin DIE not being
9250 already referenced by DW_AT_abstract_origin from the children of the
9251 current DIE. */
d389af10
JK
9252
9253static void
9254inherit_abstract_dies (struct die_info *die, struct dwarf2_cu *cu)
9255{
9256 struct die_info *child_die;
9257 unsigned die_children_count;
9258 /* CU offsets which were referenced by children of the current DIE. */
b64f50a1
JK
9259 sect_offset *offsets;
9260 sect_offset *offsets_end, *offsetp;
d389af10
JK
9261 /* Parent of DIE - referenced by DW_AT_abstract_origin. */
9262 struct die_info *origin_die;
9263 /* Iterator of the ORIGIN_DIE children. */
9264 struct die_info *origin_child_die;
9265 struct cleanup *cleanups;
9266 struct attribute *attr;
cd02d79d
PA
9267 struct dwarf2_cu *origin_cu;
9268 struct pending **origin_previous_list_in_scope;
d389af10
JK
9269
9270 attr = dwarf2_attr (die, DW_AT_abstract_origin, cu);
9271 if (!attr)
9272 return;
9273
cd02d79d
PA
9274 /* Note that following die references may follow to a die in a
9275 different cu. */
9276
9277 origin_cu = cu;
9278 origin_die = follow_die_ref (die, attr, &origin_cu);
9279
9280 /* We're inheriting ORIGIN's children into the scope we'd put DIE's
9281 symbols in. */
9282 origin_previous_list_in_scope = origin_cu->list_in_scope;
9283 origin_cu->list_in_scope = cu->list_in_scope;
9284
edb3359d
DJ
9285 if (die->tag != origin_die->tag
9286 && !(die->tag == DW_TAG_inlined_subroutine
9287 && origin_die->tag == DW_TAG_subprogram))
d389af10
JK
9288 complaint (&symfile_complaints,
9289 _("DIE 0x%x and its abstract origin 0x%x have different tags"),
b64f50a1 9290 die->offset.sect_off, origin_die->offset.sect_off);
d389af10
JK
9291
9292 child_die = die->child;
9293 die_children_count = 0;
9294 while (child_die && child_die->tag)
9295 {
9296 child_die = sibling_die (child_die);
9297 die_children_count++;
9298 }
9299 offsets = xmalloc (sizeof (*offsets) * die_children_count);
9300 cleanups = make_cleanup (xfree, offsets);
9301
9302 offsets_end = offsets;
9303 child_die = die->child;
9304 while (child_die && child_die->tag)
9305 {
c38f313d
DJ
9306 /* For each CHILD_DIE, find the corresponding child of
9307 ORIGIN_DIE. If there is more than one layer of
9308 DW_AT_abstract_origin, follow them all; there shouldn't be,
9309 but GCC versions at least through 4.4 generate this (GCC PR
9310 40573). */
9311 struct die_info *child_origin_die = child_die;
cd02d79d 9312 struct dwarf2_cu *child_origin_cu = cu;
9a619af0 9313
c38f313d
DJ
9314 while (1)
9315 {
cd02d79d
PA
9316 attr = dwarf2_attr (child_origin_die, DW_AT_abstract_origin,
9317 child_origin_cu);
c38f313d
DJ
9318 if (attr == NULL)
9319 break;
cd02d79d
PA
9320 child_origin_die = follow_die_ref (child_origin_die, attr,
9321 &child_origin_cu);
c38f313d
DJ
9322 }
9323
d389af10
JK
9324 /* According to DWARF3 3.3.8.2 #3 new entries without their abstract
9325 counterpart may exist. */
c38f313d 9326 if (child_origin_die != child_die)
d389af10 9327 {
edb3359d
DJ
9328 if (child_die->tag != child_origin_die->tag
9329 && !(child_die->tag == DW_TAG_inlined_subroutine
9330 && child_origin_die->tag == DW_TAG_subprogram))
d389af10
JK
9331 complaint (&symfile_complaints,
9332 _("Child DIE 0x%x and its abstract origin 0x%x have "
b64f50a1
JK
9333 "different tags"), child_die->offset.sect_off,
9334 child_origin_die->offset.sect_off);
c38f313d
DJ
9335 if (child_origin_die->parent != origin_die)
9336 complaint (&symfile_complaints,
9337 _("Child DIE 0x%x and its abstract origin 0x%x have "
b64f50a1
JK
9338 "different parents"), child_die->offset.sect_off,
9339 child_origin_die->offset.sect_off);
c38f313d
DJ
9340 else
9341 *offsets_end++ = child_origin_die->offset;
d389af10
JK
9342 }
9343 child_die = sibling_die (child_die);
9344 }
9345 qsort (offsets, offsets_end - offsets, sizeof (*offsets),
9346 unsigned_int_compar);
9347 for (offsetp = offsets + 1; offsetp < offsets_end; offsetp++)
b64f50a1 9348 if (offsetp[-1].sect_off == offsetp->sect_off)
3e43a32a
MS
9349 complaint (&symfile_complaints,
9350 _("Multiple children of DIE 0x%x refer "
9351 "to DIE 0x%x as their abstract origin"),
b64f50a1 9352 die->offset.sect_off, offsetp->sect_off);
d389af10
JK
9353
9354 offsetp = offsets;
9355 origin_child_die = origin_die->child;
9356 while (origin_child_die && origin_child_die->tag)
9357 {
9358 /* Is ORIGIN_CHILD_DIE referenced by any of the DIE children? */
b64f50a1
JK
9359 while (offsetp < offsets_end
9360 && offsetp->sect_off < origin_child_die->offset.sect_off)
d389af10 9361 offsetp++;
b64f50a1
JK
9362 if (offsetp >= offsets_end
9363 || offsetp->sect_off > origin_child_die->offset.sect_off)
d389af10
JK
9364 {
9365 /* Found that ORIGIN_CHILD_DIE is really not referenced. */
cd02d79d 9366 process_die (origin_child_die, origin_cu);
d389af10
JK
9367 }
9368 origin_child_die = sibling_die (origin_child_die);
9369 }
cd02d79d 9370 origin_cu->list_in_scope = origin_previous_list_in_scope;
d389af10
JK
9371
9372 do_cleanups (cleanups);
9373}
9374
c906108c 9375static void
e7c27a73 9376read_func_scope (struct die_info *die, struct dwarf2_cu *cu)
c906108c 9377{
e7c27a73 9378 struct objfile *objfile = cu->objfile;
52f0bd74 9379 struct context_stack *new;
c906108c
SS
9380 CORE_ADDR lowpc;
9381 CORE_ADDR highpc;
9382 struct die_info *child_die;
edb3359d 9383 struct attribute *attr, *call_line, *call_file;
c906108c 9384 char *name;
e142c38c 9385 CORE_ADDR baseaddr;
801e3a5b 9386 struct block *block;
edb3359d 9387 int inlined_func = (die->tag == DW_TAG_inlined_subroutine);
34eaf542
TT
9388 VEC (symbolp) *template_args = NULL;
9389 struct template_symbol *templ_func = NULL;
edb3359d
DJ
9390
9391 if (inlined_func)
9392 {
9393 /* If we do not have call site information, we can't show the
9394 caller of this inlined function. That's too confusing, so
9395 only use the scope for local variables. */
9396 call_line = dwarf2_attr (die, DW_AT_call_line, cu);
9397 call_file = dwarf2_attr (die, DW_AT_call_file, cu);
9398 if (call_line == NULL || call_file == NULL)
9399 {
9400 read_lexical_block_scope (die, cu);
9401 return;
9402 }
9403 }
c906108c 9404
e142c38c
DJ
9405 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
9406
94af9270 9407 name = dwarf2_name (die, cu);
c906108c 9408
e8d05480
JB
9409 /* Ignore functions with missing or empty names. These are actually
9410 illegal according to the DWARF standard. */
9411 if (name == NULL)
9412 {
9413 complaint (&symfile_complaints,
b64f50a1
JK
9414 _("missing name for subprogram DIE at %d"),
9415 die->offset.sect_off);
e8d05480
JB
9416 return;
9417 }
9418
9419 /* Ignore functions with missing or invalid low and high pc attributes. */
9420 if (!dwarf2_get_pc_bounds (die, &lowpc, &highpc, cu, NULL))
9421 {
ae4d0c03
PM
9422 attr = dwarf2_attr (die, DW_AT_external, cu);
9423 if (!attr || !DW_UNSND (attr))
9424 complaint (&symfile_complaints,
3e43a32a
MS
9425 _("cannot get low and high bounds "
9426 "for subprogram DIE at %d"),
b64f50a1 9427 die->offset.sect_off);
e8d05480
JB
9428 return;
9429 }
c906108c
SS
9430
9431 lowpc += baseaddr;
9432 highpc += baseaddr;
9433
34eaf542
TT
9434 /* If we have any template arguments, then we must allocate a
9435 different sort of symbol. */
9436 for (child_die = die->child; child_die; child_die = sibling_die (child_die))
9437 {
9438 if (child_die->tag == DW_TAG_template_type_param
9439 || child_die->tag == DW_TAG_template_value_param)
9440 {
9441 templ_func = OBSTACK_ZALLOC (&objfile->objfile_obstack,
9442 struct template_symbol);
9443 templ_func->base.is_cplus_template_function = 1;
9444 break;
9445 }
9446 }
9447
c906108c 9448 new = push_context (0, lowpc);
34eaf542
TT
9449 new->name = new_symbol_full (die, read_type_die (die, cu), cu,
9450 (struct symbol *) templ_func);
4c2df51b 9451
4cecd739
DJ
9452 /* If there is a location expression for DW_AT_frame_base, record
9453 it. */
e142c38c 9454 attr = dwarf2_attr (die, DW_AT_frame_base, cu);
4c2df51b 9455 if (attr)
c034e007
AC
9456 /* FIXME: cagney/2004-01-26: The DW_AT_frame_base's location
9457 expression is being recorded directly in the function's symbol
9458 and not in a separate frame-base object. I guess this hack is
9459 to avoid adding some sort of frame-base adjunct/annex to the
9460 function's symbol :-(. The problem with doing this is that it
9461 results in a function symbol with a location expression that
9462 has nothing to do with the location of the function, ouch! The
9463 relationship should be: a function's symbol has-a frame base; a
9464 frame-base has-a location expression. */
e7c27a73 9465 dwarf2_symbol_mark_computed (attr, new->name, cu);
4c2df51b 9466
e142c38c 9467 cu->list_in_scope = &local_symbols;
c906108c 9468
639d11d3 9469 if (die->child != NULL)
c906108c 9470 {
639d11d3 9471 child_die = die->child;
c906108c
SS
9472 while (child_die && child_die->tag)
9473 {
34eaf542
TT
9474 if (child_die->tag == DW_TAG_template_type_param
9475 || child_die->tag == DW_TAG_template_value_param)
9476 {
9477 struct symbol *arg = new_symbol (child_die, NULL, cu);
9478
f1078f66
DJ
9479 if (arg != NULL)
9480 VEC_safe_push (symbolp, template_args, arg);
34eaf542
TT
9481 }
9482 else
9483 process_die (child_die, cu);
c906108c
SS
9484 child_die = sibling_die (child_die);
9485 }
9486 }
9487
d389af10
JK
9488 inherit_abstract_dies (die, cu);
9489
4a811a97
UW
9490 /* If we have a DW_AT_specification, we might need to import using
9491 directives from the context of the specification DIE. See the
9492 comment in determine_prefix. */
9493 if (cu->language == language_cplus
9494 && dwarf2_attr (die, DW_AT_specification, cu))
9495 {
9496 struct dwarf2_cu *spec_cu = cu;
9497 struct die_info *spec_die = die_specification (die, &spec_cu);
9498
9499 while (spec_die)
9500 {
9501 child_die = spec_die->child;
9502 while (child_die && child_die->tag)
9503 {
9504 if (child_die->tag == DW_TAG_imported_module)
9505 process_die (child_die, spec_cu);
9506 child_die = sibling_die (child_die);
9507 }
9508
9509 /* In some cases, GCC generates specification DIEs that
9510 themselves contain DW_AT_specification attributes. */
9511 spec_die = die_specification (spec_die, &spec_cu);
9512 }
9513 }
9514
c906108c
SS
9515 new = pop_context ();
9516 /* Make a block for the local symbols within. */
801e3a5b
JB
9517 block = finish_block (new->name, &local_symbols, new->old_blocks,
9518 lowpc, highpc, objfile);
9519
df8a16a1 9520 /* For C++, set the block's scope. */
f55ee35c 9521 if (cu->language == language_cplus || cu->language == language_fortran)
df8a16a1 9522 cp_set_block_scope (new->name, block, &objfile->objfile_obstack,
0114d602 9523 determine_prefix (die, cu),
df8a16a1
DJ
9524 processing_has_namespace_info);
9525
801e3a5b
JB
9526 /* If we have address ranges, record them. */
9527 dwarf2_record_block_ranges (die, block, baseaddr, cu);
6e70227d 9528
34eaf542
TT
9529 /* Attach template arguments to function. */
9530 if (! VEC_empty (symbolp, template_args))
9531 {
9532 gdb_assert (templ_func != NULL);
9533
9534 templ_func->n_template_arguments = VEC_length (symbolp, template_args);
9535 templ_func->template_arguments
9536 = obstack_alloc (&objfile->objfile_obstack,
9537 (templ_func->n_template_arguments
9538 * sizeof (struct symbol *)));
9539 memcpy (templ_func->template_arguments,
9540 VEC_address (symbolp, template_args),
9541 (templ_func->n_template_arguments * sizeof (struct symbol *)));
9542 VEC_free (symbolp, template_args);
9543 }
9544
208d8187
JB
9545 /* In C++, we can have functions nested inside functions (e.g., when
9546 a function declares a class that has methods). This means that
9547 when we finish processing a function scope, we may need to go
9548 back to building a containing block's symbol lists. */
9549 local_symbols = new->locals;
27aa8d6a 9550 using_directives = new->using_directives;
208d8187 9551
921e78cf
JB
9552 /* If we've finished processing a top-level function, subsequent
9553 symbols go in the file symbol list. */
9554 if (outermost_context_p ())
e142c38c 9555 cu->list_in_scope = &file_symbols;
c906108c
SS
9556}
9557
9558/* Process all the DIES contained within a lexical block scope. Start
9559 a new scope, process the dies, and then close the scope. */
9560
9561static void
e7c27a73 9562read_lexical_block_scope (struct die_info *die, struct dwarf2_cu *cu)
c906108c 9563{
e7c27a73 9564 struct objfile *objfile = cu->objfile;
52f0bd74 9565 struct context_stack *new;
c906108c
SS
9566 CORE_ADDR lowpc, highpc;
9567 struct die_info *child_die;
e142c38c
DJ
9568 CORE_ADDR baseaddr;
9569
9570 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
c906108c
SS
9571
9572 /* Ignore blocks with missing or invalid low and high pc attributes. */
af34e669
DJ
9573 /* ??? Perhaps consider discontiguous blocks defined by DW_AT_ranges
9574 as multiple lexical blocks? Handling children in a sane way would
6e70227d 9575 be nasty. Might be easier to properly extend generic blocks to
af34e669 9576 describe ranges. */
d85a05f0 9577 if (!dwarf2_get_pc_bounds (die, &lowpc, &highpc, cu, NULL))
c906108c
SS
9578 return;
9579 lowpc += baseaddr;
9580 highpc += baseaddr;
9581
9582 push_context (0, lowpc);
639d11d3 9583 if (die->child != NULL)
c906108c 9584 {
639d11d3 9585 child_die = die->child;
c906108c
SS
9586 while (child_die && child_die->tag)
9587 {
e7c27a73 9588 process_die (child_die, cu);
c906108c
SS
9589 child_die = sibling_die (child_die);
9590 }
9591 }
9592 new = pop_context ();
9593
8540c487 9594 if (local_symbols != NULL || using_directives != NULL)
c906108c 9595 {
801e3a5b
JB
9596 struct block *block
9597 = finish_block (0, &local_symbols, new->old_blocks, new->start_addr,
9598 highpc, objfile);
9599
9600 /* Note that recording ranges after traversing children, as we
9601 do here, means that recording a parent's ranges entails
9602 walking across all its children's ranges as they appear in
9603 the address map, which is quadratic behavior.
9604
9605 It would be nicer to record the parent's ranges before
9606 traversing its children, simply overriding whatever you find
9607 there. But since we don't even decide whether to create a
9608 block until after we've traversed its children, that's hard
9609 to do. */
9610 dwarf2_record_block_ranges (die, block, baseaddr, cu);
c906108c
SS
9611 }
9612 local_symbols = new->locals;
27aa8d6a 9613 using_directives = new->using_directives;
c906108c
SS
9614}
9615
96408a79
SA
9616/* Read in DW_TAG_GNU_call_site and insert it to CU->call_site_htab. */
9617
9618static void
9619read_call_site_scope (struct die_info *die, struct dwarf2_cu *cu)
9620{
9621 struct objfile *objfile = cu->objfile;
9622 struct gdbarch *gdbarch = get_objfile_arch (objfile);
9623 CORE_ADDR pc, baseaddr;
9624 struct attribute *attr;
9625 struct call_site *call_site, call_site_local;
9626 void **slot;
9627 int nparams;
9628 struct die_info *child_die;
9629
9630 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
9631
9632 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
9633 if (!attr)
9634 {
9635 complaint (&symfile_complaints,
9636 _("missing DW_AT_low_pc for DW_TAG_GNU_call_site "
9637 "DIE 0x%x [in module %s]"),
b64f50a1 9638 die->offset.sect_off, objfile->name);
96408a79
SA
9639 return;
9640 }
9641 pc = DW_ADDR (attr) + baseaddr;
9642
9643 if (cu->call_site_htab == NULL)
9644 cu->call_site_htab = htab_create_alloc_ex (16, core_addr_hash, core_addr_eq,
9645 NULL, &objfile->objfile_obstack,
9646 hashtab_obstack_allocate, NULL);
9647 call_site_local.pc = pc;
9648 slot = htab_find_slot (cu->call_site_htab, &call_site_local, INSERT);
9649 if (*slot != NULL)
9650 {
9651 complaint (&symfile_complaints,
9652 _("Duplicate PC %s for DW_TAG_GNU_call_site "
9653 "DIE 0x%x [in module %s]"),
b64f50a1 9654 paddress (gdbarch, pc), die->offset.sect_off, objfile->name);
96408a79
SA
9655 return;
9656 }
9657
9658 /* Count parameters at the caller. */
9659
9660 nparams = 0;
9661 for (child_die = die->child; child_die && child_die->tag;
9662 child_die = sibling_die (child_die))
9663 {
9664 if (child_die->tag != DW_TAG_GNU_call_site_parameter)
9665 {
9666 complaint (&symfile_complaints,
9667 _("Tag %d is not DW_TAG_GNU_call_site_parameter in "
9668 "DW_TAG_GNU_call_site child DIE 0x%x [in module %s]"),
b64f50a1 9669 child_die->tag, child_die->offset.sect_off, objfile->name);
96408a79
SA
9670 continue;
9671 }
9672
9673 nparams++;
9674 }
9675
9676 call_site = obstack_alloc (&objfile->objfile_obstack,
9677 (sizeof (*call_site)
9678 + (sizeof (*call_site->parameter)
9679 * (nparams - 1))));
9680 *slot = call_site;
9681 memset (call_site, 0, sizeof (*call_site) - sizeof (*call_site->parameter));
9682 call_site->pc = pc;
9683
9684 if (dwarf2_flag_true_p (die, DW_AT_GNU_tail_call, cu))
9685 {
9686 struct die_info *func_die;
9687
9688 /* Skip also over DW_TAG_inlined_subroutine. */
9689 for (func_die = die->parent;
9690 func_die && func_die->tag != DW_TAG_subprogram
9691 && func_die->tag != DW_TAG_subroutine_type;
9692 func_die = func_die->parent);
9693
9694 /* DW_AT_GNU_all_call_sites is a superset
9695 of DW_AT_GNU_all_tail_call_sites. */
9696 if (func_die
9697 && !dwarf2_flag_true_p (func_die, DW_AT_GNU_all_call_sites, cu)
9698 && !dwarf2_flag_true_p (func_die, DW_AT_GNU_all_tail_call_sites, cu))
9699 {
9700 /* TYPE_TAIL_CALL_LIST is not interesting in functions where it is
9701 not complete. But keep CALL_SITE for look ups via call_site_htab,
9702 both the initial caller containing the real return address PC and
9703 the final callee containing the current PC of a chain of tail
9704 calls do not need to have the tail call list complete. But any
9705 function candidate for a virtual tail call frame searched via
9706 TYPE_TAIL_CALL_LIST must have the tail call list complete to be
9707 determined unambiguously. */
9708 }
9709 else
9710 {
9711 struct type *func_type = NULL;
9712
9713 if (func_die)
9714 func_type = get_die_type (func_die, cu);
9715 if (func_type != NULL)
9716 {
9717 gdb_assert (TYPE_CODE (func_type) == TYPE_CODE_FUNC);
9718
9719 /* Enlist this call site to the function. */
9720 call_site->tail_call_next = TYPE_TAIL_CALL_LIST (func_type);
9721 TYPE_TAIL_CALL_LIST (func_type) = call_site;
9722 }
9723 else
9724 complaint (&symfile_complaints,
9725 _("Cannot find function owning DW_TAG_GNU_call_site "
9726 "DIE 0x%x [in module %s]"),
b64f50a1 9727 die->offset.sect_off, objfile->name);
96408a79
SA
9728 }
9729 }
9730
9731 attr = dwarf2_attr (die, DW_AT_GNU_call_site_target, cu);
9732 if (attr == NULL)
9733 attr = dwarf2_attr (die, DW_AT_abstract_origin, cu);
9734 SET_FIELD_DWARF_BLOCK (call_site->target, NULL);
9735 if (!attr || (attr_form_is_block (attr) && DW_BLOCK (attr)->size == 0))
9736 /* Keep NULL DWARF_BLOCK. */;
9737 else if (attr_form_is_block (attr))
9738 {
9739 struct dwarf2_locexpr_baton *dlbaton;
9740
9741 dlbaton = obstack_alloc (&objfile->objfile_obstack, sizeof (*dlbaton));
9742 dlbaton->data = DW_BLOCK (attr)->data;
9743 dlbaton->size = DW_BLOCK (attr)->size;
9744 dlbaton->per_cu = cu->per_cu;
9745
9746 SET_FIELD_DWARF_BLOCK (call_site->target, dlbaton);
9747 }
9748 else if (is_ref_attr (attr))
9749 {
96408a79
SA
9750 struct dwarf2_cu *target_cu = cu;
9751 struct die_info *target_die;
9752
9753 target_die = follow_die_ref_or_sig (die, attr, &target_cu);
9754 gdb_assert (target_cu->objfile == objfile);
9755 if (die_is_declaration (target_die, target_cu))
9756 {
9757 const char *target_physname;
9758
9759 target_physname = dwarf2_physname (NULL, target_die, target_cu);
9760 if (target_physname == NULL)
9761 complaint (&symfile_complaints,
9762 _("DW_AT_GNU_call_site_target target DIE has invalid "
9763 "physname, for referencing DIE 0x%x [in module %s]"),
b64f50a1 9764 die->offset.sect_off, objfile->name);
96408a79
SA
9765 else
9766 SET_FIELD_PHYSNAME (call_site->target, (char *) target_physname);
9767 }
9768 else
9769 {
9770 CORE_ADDR lowpc;
9771
9772 /* DW_AT_entry_pc should be preferred. */
9773 if (!dwarf2_get_pc_bounds (target_die, &lowpc, NULL, target_cu, NULL))
9774 complaint (&symfile_complaints,
9775 _("DW_AT_GNU_call_site_target target DIE has invalid "
9776 "low pc, for referencing DIE 0x%x [in module %s]"),
b64f50a1 9777 die->offset.sect_off, objfile->name);
96408a79
SA
9778 else
9779 SET_FIELD_PHYSADDR (call_site->target, lowpc + baseaddr);
9780 }
9781 }
9782 else
9783 complaint (&symfile_complaints,
9784 _("DW_TAG_GNU_call_site DW_AT_GNU_call_site_target is neither "
9785 "block nor reference, for DIE 0x%x [in module %s]"),
b64f50a1 9786 die->offset.sect_off, objfile->name);
96408a79
SA
9787
9788 call_site->per_cu = cu->per_cu;
9789
9790 for (child_die = die->child;
9791 child_die && child_die->tag;
9792 child_die = sibling_die (child_die))
9793 {
96408a79 9794 struct call_site_parameter *parameter;
1788b2d3 9795 struct attribute *loc, *origin;
96408a79
SA
9796
9797 if (child_die->tag != DW_TAG_GNU_call_site_parameter)
9798 {
9799 /* Already printed the complaint above. */
9800 continue;
9801 }
9802
9803 gdb_assert (call_site->parameter_count < nparams);
9804 parameter = &call_site->parameter[call_site->parameter_count];
9805
1788b2d3
JK
9806 /* DW_AT_location specifies the register number or DW_AT_abstract_origin
9807 specifies DW_TAG_formal_parameter. Value of the data assumed for the
9808 register is contained in DW_AT_GNU_call_site_value. */
96408a79 9809
24c5c679 9810 loc = dwarf2_attr (child_die, DW_AT_location, cu);
1788b2d3
JK
9811 origin = dwarf2_attr (child_die, DW_AT_abstract_origin, cu);
9812 if (loc == NULL && origin != NULL && is_ref_attr (origin))
9813 {
9814 sect_offset offset;
9815
9816 parameter->kind = CALL_SITE_PARAMETER_PARAM_OFFSET;
9817 offset = dwarf2_get_ref_die_offset (origin);
d76b7dbc
JK
9818 if (!offset_in_cu_p (&cu->header, offset))
9819 {
9820 /* As DW_OP_GNU_parameter_ref uses CU-relative offset this
9821 binding can be done only inside one CU. Such referenced DIE
9822 therefore cannot be even moved to DW_TAG_partial_unit. */
9823 complaint (&symfile_complaints,
9824 _("DW_AT_abstract_origin offset is not in CU for "
9825 "DW_TAG_GNU_call_site child DIE 0x%x "
9826 "[in module %s]"),
9827 child_die->offset.sect_off, objfile->name);
9828 continue;
9829 }
1788b2d3
JK
9830 parameter->u.param_offset.cu_off = (offset.sect_off
9831 - cu->header.offset.sect_off);
9832 }
9833 else if (loc == NULL || origin != NULL || !attr_form_is_block (loc))
96408a79
SA
9834 {
9835 complaint (&symfile_complaints,
9836 _("No DW_FORM_block* DW_AT_location for "
9837 "DW_TAG_GNU_call_site child DIE 0x%x [in module %s]"),
b64f50a1 9838 child_die->offset.sect_off, objfile->name);
96408a79
SA
9839 continue;
9840 }
24c5c679 9841 else
96408a79 9842 {
24c5c679
JK
9843 parameter->u.dwarf_reg = dwarf_block_to_dwarf_reg
9844 (DW_BLOCK (loc)->data, &DW_BLOCK (loc)->data[DW_BLOCK (loc)->size]);
9845 if (parameter->u.dwarf_reg != -1)
9846 parameter->kind = CALL_SITE_PARAMETER_DWARF_REG;
9847 else if (dwarf_block_to_sp_offset (gdbarch, DW_BLOCK (loc)->data,
9848 &DW_BLOCK (loc)->data[DW_BLOCK (loc)->size],
9849 &parameter->u.fb_offset))
9850 parameter->kind = CALL_SITE_PARAMETER_FB_OFFSET;
9851 else
9852 {
9853 complaint (&symfile_complaints,
9854 _("Only single DW_OP_reg or DW_OP_fbreg is supported "
9855 "for DW_FORM_block* DW_AT_location is supported for "
9856 "DW_TAG_GNU_call_site child DIE 0x%x "
9857 "[in module %s]"),
9858 child_die->offset.sect_off, objfile->name);
9859 continue;
9860 }
96408a79
SA
9861 }
9862
9863 attr = dwarf2_attr (child_die, DW_AT_GNU_call_site_value, cu);
9864 if (!attr_form_is_block (attr))
9865 {
9866 complaint (&symfile_complaints,
9867 _("No DW_FORM_block* DW_AT_GNU_call_site_value for "
9868 "DW_TAG_GNU_call_site child DIE 0x%x [in module %s]"),
b64f50a1 9869 child_die->offset.sect_off, objfile->name);
96408a79
SA
9870 continue;
9871 }
9872 parameter->value = DW_BLOCK (attr)->data;
9873 parameter->value_size = DW_BLOCK (attr)->size;
9874
9875 /* Parameters are not pre-cleared by memset above. */
9876 parameter->data_value = NULL;
9877 parameter->data_value_size = 0;
9878 call_site->parameter_count++;
9879
9880 attr = dwarf2_attr (child_die, DW_AT_GNU_call_site_data_value, cu);
9881 if (attr)
9882 {
9883 if (!attr_form_is_block (attr))
9884 complaint (&symfile_complaints,
9885 _("No DW_FORM_block* DW_AT_GNU_call_site_data_value for "
9886 "DW_TAG_GNU_call_site child DIE 0x%x [in module %s]"),
b64f50a1 9887 child_die->offset.sect_off, objfile->name);
96408a79
SA
9888 else
9889 {
9890 parameter->data_value = DW_BLOCK (attr)->data;
9891 parameter->data_value_size = DW_BLOCK (attr)->size;
9892 }
9893 }
9894 }
9895}
9896
43039443 9897/* Get low and high pc attributes from DW_AT_ranges attribute value OFFSET.
ff013f42
JK
9898 Return 1 if the attributes are present and valid, otherwise, return 0.
9899 If RANGES_PST is not NULL we should setup `objfile->psymtabs_addrmap'. */
43039443
JK
9900
9901static int
9902dwarf2_ranges_read (unsigned offset, CORE_ADDR *low_return,
ff013f42
JK
9903 CORE_ADDR *high_return, struct dwarf2_cu *cu,
9904 struct partial_symtab *ranges_pst)
43039443
JK
9905{
9906 struct objfile *objfile = cu->objfile;
9907 struct comp_unit_head *cu_header = &cu->header;
9908 bfd *obfd = objfile->obfd;
9909 unsigned int addr_size = cu_header->addr_size;
9910 CORE_ADDR mask = ~(~(CORE_ADDR)1 << (addr_size * 8 - 1));
9911 /* Base address selection entry. */
9912 CORE_ADDR base;
9913 int found_base;
9914 unsigned int dummy;
9915 gdb_byte *buffer;
9916 CORE_ADDR marker;
9917 int low_set;
9918 CORE_ADDR low = 0;
9919 CORE_ADDR high = 0;
ff013f42 9920 CORE_ADDR baseaddr;
43039443 9921
d00adf39
DE
9922 found_base = cu->base_known;
9923 base = cu->base_address;
43039443 9924
be391dca 9925 dwarf2_read_section (objfile, &dwarf2_per_objfile->ranges);
dce234bc 9926 if (offset >= dwarf2_per_objfile->ranges.size)
43039443
JK
9927 {
9928 complaint (&symfile_complaints,
9929 _("Offset %d out of bounds for DW_AT_ranges attribute"),
9930 offset);
9931 return 0;
9932 }
dce234bc 9933 buffer = dwarf2_per_objfile->ranges.buffer + offset;
43039443
JK
9934
9935 /* Read in the largest possible address. */
9936 marker = read_address (obfd, buffer, cu, &dummy);
9937 if ((marker & mask) == mask)
9938 {
9939 /* If we found the largest possible address, then
9940 read the base address. */
9941 base = read_address (obfd, buffer + addr_size, cu, &dummy);
9942 buffer += 2 * addr_size;
9943 offset += 2 * addr_size;
9944 found_base = 1;
9945 }
9946
9947 low_set = 0;
9948
e7030f15 9949 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
ff013f42 9950
43039443
JK
9951 while (1)
9952 {
9953 CORE_ADDR range_beginning, range_end;
9954
9955 range_beginning = read_address (obfd, buffer, cu, &dummy);
9956 buffer += addr_size;
9957 range_end = read_address (obfd, buffer, cu, &dummy);
9958 buffer += addr_size;
9959 offset += 2 * addr_size;
9960
9961 /* An end of list marker is a pair of zero addresses. */
9962 if (range_beginning == 0 && range_end == 0)
9963 /* Found the end of list entry. */
9964 break;
9965
9966 /* Each base address selection entry is a pair of 2 values.
9967 The first is the largest possible address, the second is
9968 the base address. Check for a base address here. */
9969 if ((range_beginning & mask) == mask)
9970 {
9971 /* If we found the largest possible address, then
9972 read the base address. */
9973 base = read_address (obfd, buffer + addr_size, cu, &dummy);
9974 found_base = 1;
9975 continue;
9976 }
9977
9978 if (!found_base)
9979 {
9980 /* We have no valid base address for the ranges
9981 data. */
9982 complaint (&symfile_complaints,
9983 _("Invalid .debug_ranges data (no base address)"));
9984 return 0;
9985 }
9986
9277c30c
UW
9987 if (range_beginning > range_end)
9988 {
9989 /* Inverted range entries are invalid. */
9990 complaint (&symfile_complaints,
9991 _("Invalid .debug_ranges data (inverted range)"));
9992 return 0;
9993 }
9994
9995 /* Empty range entries have no effect. */
9996 if (range_beginning == range_end)
9997 continue;
9998
43039443
JK
9999 range_beginning += base;
10000 range_end += base;
10001
01093045
DE
10002 /* A not-uncommon case of bad debug info.
10003 Don't pollute the addrmap with bad data. */
10004 if (range_beginning + baseaddr == 0
10005 && !dwarf2_per_objfile->has_section_at_zero)
10006 {
10007 complaint (&symfile_complaints,
10008 _(".debug_ranges entry has start address of zero"
10009 " [in module %s]"), objfile->name);
10010 continue;
10011 }
10012
9277c30c 10013 if (ranges_pst != NULL)
ff013f42 10014 addrmap_set_empty (objfile->psymtabs_addrmap,
3e43a32a
MS
10015 range_beginning + baseaddr,
10016 range_end - 1 + baseaddr,
ff013f42
JK
10017 ranges_pst);
10018
43039443
JK
10019 /* FIXME: This is recording everything as a low-high
10020 segment of consecutive addresses. We should have a
10021 data structure for discontiguous block ranges
10022 instead. */
10023 if (! low_set)
10024 {
10025 low = range_beginning;
10026 high = range_end;
10027 low_set = 1;
10028 }
10029 else
10030 {
10031 if (range_beginning < low)
10032 low = range_beginning;
10033 if (range_end > high)
10034 high = range_end;
10035 }
10036 }
10037
10038 if (! low_set)
10039 /* If the first entry is an end-of-list marker, the range
10040 describes an empty scope, i.e. no instructions. */
10041 return 0;
10042
10043 if (low_return)
10044 *low_return = low;
10045 if (high_return)
10046 *high_return = high;
10047 return 1;
10048}
10049
af34e669
DJ
10050/* Get low and high pc attributes from a die. Return 1 if the attributes
10051 are present and valid, otherwise, return 0. Return -1 if the range is
10052 discontinuous, i.e. derived from DW_AT_ranges information. */
380bca97 10053
c906108c 10054static int
af34e669 10055dwarf2_get_pc_bounds (struct die_info *die, CORE_ADDR *lowpc,
d85a05f0
DJ
10056 CORE_ADDR *highpc, struct dwarf2_cu *cu,
10057 struct partial_symtab *pst)
c906108c
SS
10058{
10059 struct attribute *attr;
91da1414 10060 struct attribute *attr_high;
af34e669
DJ
10061 CORE_ADDR low = 0;
10062 CORE_ADDR high = 0;
10063 int ret = 0;
c906108c 10064
91da1414
MW
10065 attr_high = dwarf2_attr (die, DW_AT_high_pc, cu);
10066 if (attr_high)
af34e669 10067 {
e142c38c 10068 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
af34e669 10069 if (attr)
91da1414
MW
10070 {
10071 low = DW_ADDR (attr);
3019eac3
DE
10072 if (attr_high->form == DW_FORM_addr
10073 || attr_high->form == DW_FORM_GNU_addr_index)
91da1414
MW
10074 high = DW_ADDR (attr_high);
10075 else
10076 high = low + DW_UNSND (attr_high);
10077 }
af34e669
DJ
10078 else
10079 /* Found high w/o low attribute. */
10080 return 0;
10081
10082 /* Found consecutive range of addresses. */
10083 ret = 1;
10084 }
c906108c 10085 else
af34e669 10086 {
e142c38c 10087 attr = dwarf2_attr (die, DW_AT_ranges, cu);
af34e669
DJ
10088 if (attr != NULL)
10089 {
2e3cf129
DE
10090 unsigned int ranges_offset = DW_UNSND (attr) + cu->ranges_base;
10091
af34e669 10092 /* Value of the DW_AT_ranges attribute is the offset in the
a604369a 10093 .debug_ranges section. */
2e3cf129 10094 if (!dwarf2_ranges_read (ranges_offset, &low, &high, cu, pst))
af34e669 10095 return 0;
43039443 10096 /* Found discontinuous range of addresses. */
af34e669
DJ
10097 ret = -1;
10098 }
10099 }
c906108c 10100
9373cf26
JK
10101 /* read_partial_die has also the strict LOW < HIGH requirement. */
10102 if (high <= low)
c906108c
SS
10103 return 0;
10104
10105 /* When using the GNU linker, .gnu.linkonce. sections are used to
10106 eliminate duplicate copies of functions and vtables and such.
10107 The linker will arbitrarily choose one and discard the others.
10108 The AT_*_pc values for such functions refer to local labels in
10109 these sections. If the section from that file was discarded, the
10110 labels are not in the output, so the relocs get a value of 0.
10111 If this is a discarded function, mark the pc bounds as invalid,
10112 so that GDB will ignore it. */
72dca2f5 10113 if (low == 0 && !dwarf2_per_objfile->has_section_at_zero)
c906108c
SS
10114 return 0;
10115
10116 *lowpc = low;
96408a79
SA
10117 if (highpc)
10118 *highpc = high;
af34e669 10119 return ret;
c906108c
SS
10120}
10121
b084d499
JB
10122/* Assuming that DIE represents a subprogram DIE or a lexical block, get
10123 its low and high PC addresses. Do nothing if these addresses could not
10124 be determined. Otherwise, set LOWPC to the low address if it is smaller,
10125 and HIGHPC to the high address if greater than HIGHPC. */
10126
10127static void
10128dwarf2_get_subprogram_pc_bounds (struct die_info *die,
10129 CORE_ADDR *lowpc, CORE_ADDR *highpc,
10130 struct dwarf2_cu *cu)
10131{
10132 CORE_ADDR low, high;
10133 struct die_info *child = die->child;
10134
d85a05f0 10135 if (dwarf2_get_pc_bounds (die, &low, &high, cu, NULL))
b084d499
JB
10136 {
10137 *lowpc = min (*lowpc, low);
10138 *highpc = max (*highpc, high);
10139 }
10140
10141 /* If the language does not allow nested subprograms (either inside
10142 subprograms or lexical blocks), we're done. */
10143 if (cu->language != language_ada)
10144 return;
6e70227d 10145
b084d499
JB
10146 /* Check all the children of the given DIE. If it contains nested
10147 subprograms, then check their pc bounds. Likewise, we need to
10148 check lexical blocks as well, as they may also contain subprogram
10149 definitions. */
10150 while (child && child->tag)
10151 {
10152 if (child->tag == DW_TAG_subprogram
10153 || child->tag == DW_TAG_lexical_block)
10154 dwarf2_get_subprogram_pc_bounds (child, lowpc, highpc, cu);
10155 child = sibling_die (child);
10156 }
10157}
10158
fae299cd
DC
10159/* Get the low and high pc's represented by the scope DIE, and store
10160 them in *LOWPC and *HIGHPC. If the correct values can't be
10161 determined, set *LOWPC to -1 and *HIGHPC to 0. */
10162
10163static void
10164get_scope_pc_bounds (struct die_info *die,
10165 CORE_ADDR *lowpc, CORE_ADDR *highpc,
10166 struct dwarf2_cu *cu)
10167{
10168 CORE_ADDR best_low = (CORE_ADDR) -1;
10169 CORE_ADDR best_high = (CORE_ADDR) 0;
10170 CORE_ADDR current_low, current_high;
10171
d85a05f0 10172 if (dwarf2_get_pc_bounds (die, &current_low, &current_high, cu, NULL))
fae299cd
DC
10173 {
10174 best_low = current_low;
10175 best_high = current_high;
10176 }
10177 else
10178 {
10179 struct die_info *child = die->child;
10180
10181 while (child && child->tag)
10182 {
10183 switch (child->tag) {
10184 case DW_TAG_subprogram:
b084d499 10185 dwarf2_get_subprogram_pc_bounds (child, &best_low, &best_high, cu);
fae299cd
DC
10186 break;
10187 case DW_TAG_namespace:
f55ee35c 10188 case DW_TAG_module:
fae299cd
DC
10189 /* FIXME: carlton/2004-01-16: Should we do this for
10190 DW_TAG_class_type/DW_TAG_structure_type, too? I think
10191 that current GCC's always emit the DIEs corresponding
10192 to definitions of methods of classes as children of a
10193 DW_TAG_compile_unit or DW_TAG_namespace (as opposed to
10194 the DIEs giving the declarations, which could be
10195 anywhere). But I don't see any reason why the
10196 standards says that they have to be there. */
10197 get_scope_pc_bounds (child, &current_low, &current_high, cu);
10198
10199 if (current_low != ((CORE_ADDR) -1))
10200 {
10201 best_low = min (best_low, current_low);
10202 best_high = max (best_high, current_high);
10203 }
10204 break;
10205 default:
0963b4bd 10206 /* Ignore. */
fae299cd
DC
10207 break;
10208 }
10209
10210 child = sibling_die (child);
10211 }
10212 }
10213
10214 *lowpc = best_low;
10215 *highpc = best_high;
10216}
10217
801e3a5b
JB
10218/* Record the address ranges for BLOCK, offset by BASEADDR, as given
10219 in DIE. */
380bca97 10220
801e3a5b
JB
10221static void
10222dwarf2_record_block_ranges (struct die_info *die, struct block *block,
10223 CORE_ADDR baseaddr, struct dwarf2_cu *cu)
10224{
bb5ed363 10225 struct objfile *objfile = cu->objfile;
801e3a5b 10226 struct attribute *attr;
91da1414 10227 struct attribute *attr_high;
801e3a5b 10228
91da1414
MW
10229 attr_high = dwarf2_attr (die, DW_AT_high_pc, cu);
10230 if (attr_high)
801e3a5b 10231 {
801e3a5b
JB
10232 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
10233 if (attr)
10234 {
10235 CORE_ADDR low = DW_ADDR (attr);
91da1414 10236 CORE_ADDR high;
3019eac3
DE
10237 if (attr_high->form == DW_FORM_addr
10238 || attr_high->form == DW_FORM_GNU_addr_index)
91da1414
MW
10239 high = DW_ADDR (attr_high);
10240 else
10241 high = low + DW_UNSND (attr_high);
9a619af0 10242
801e3a5b
JB
10243 record_block_range (block, baseaddr + low, baseaddr + high - 1);
10244 }
10245 }
10246
10247 attr = dwarf2_attr (die, DW_AT_ranges, cu);
10248 if (attr)
10249 {
bb5ed363 10250 bfd *obfd = objfile->obfd;
801e3a5b
JB
10251
10252 /* The value of the DW_AT_ranges attribute is the offset of the
10253 address range list in the .debug_ranges section. */
2e3cf129 10254 unsigned long offset = DW_UNSND (attr) + cu->ranges_base;
dce234bc 10255 gdb_byte *buffer = dwarf2_per_objfile->ranges.buffer + offset;
801e3a5b
JB
10256
10257 /* For some target architectures, but not others, the
10258 read_address function sign-extends the addresses it returns.
10259 To recognize base address selection entries, we need a
10260 mask. */
10261 unsigned int addr_size = cu->header.addr_size;
10262 CORE_ADDR base_select_mask = ~(~(CORE_ADDR)1 << (addr_size * 8 - 1));
10263
10264 /* The base address, to which the next pair is relative. Note
10265 that this 'base' is a DWARF concept: most entries in a range
10266 list are relative, to reduce the number of relocs against the
10267 debugging information. This is separate from this function's
10268 'baseaddr' argument, which GDB uses to relocate debugging
10269 information from a shared library based on the address at
10270 which the library was loaded. */
d00adf39
DE
10271 CORE_ADDR base = cu->base_address;
10272 int base_known = cu->base_known;
801e3a5b 10273
be391dca 10274 gdb_assert (dwarf2_per_objfile->ranges.readin);
dce234bc 10275 if (offset >= dwarf2_per_objfile->ranges.size)
801e3a5b
JB
10276 {
10277 complaint (&symfile_complaints,
10278 _("Offset %lu out of bounds for DW_AT_ranges attribute"),
10279 offset);
10280 return;
10281 }
10282
10283 for (;;)
10284 {
10285 unsigned int bytes_read;
10286 CORE_ADDR start, end;
10287
10288 start = read_address (obfd, buffer, cu, &bytes_read);
10289 buffer += bytes_read;
10290 end = read_address (obfd, buffer, cu, &bytes_read);
10291 buffer += bytes_read;
10292
10293 /* Did we find the end of the range list? */
10294 if (start == 0 && end == 0)
10295 break;
10296
10297 /* Did we find a base address selection entry? */
10298 else if ((start & base_select_mask) == base_select_mask)
10299 {
10300 base = end;
10301 base_known = 1;
10302 }
10303
10304 /* We found an ordinary address range. */
10305 else
10306 {
10307 if (!base_known)
10308 {
10309 complaint (&symfile_complaints,
3e43a32a
MS
10310 _("Invalid .debug_ranges data "
10311 "(no base address)"));
801e3a5b
JB
10312 return;
10313 }
10314
9277c30c
UW
10315 if (start > end)
10316 {
10317 /* Inverted range entries are invalid. */
10318 complaint (&symfile_complaints,
10319 _("Invalid .debug_ranges data "
10320 "(inverted range)"));
10321 return;
10322 }
10323
10324 /* Empty range entries have no effect. */
10325 if (start == end)
10326 continue;
10327
01093045
DE
10328 start += base + baseaddr;
10329 end += base + baseaddr;
10330
10331 /* A not-uncommon case of bad debug info.
10332 Don't pollute the addrmap with bad data. */
10333 if (start == 0 && !dwarf2_per_objfile->has_section_at_zero)
10334 {
10335 complaint (&symfile_complaints,
10336 _(".debug_ranges entry has start address of zero"
10337 " [in module %s]"), objfile->name);
10338 continue;
10339 }
10340
10341 record_block_range (block, start, end - 1);
801e3a5b
JB
10342 }
10343 }
10344 }
10345}
10346
685b1105
JK
10347/* Check whether the producer field indicates either of GCC < 4.6, or the
10348 Intel C/C++ compiler, and cache the result in CU. */
60d5a603 10349
685b1105
JK
10350static void
10351check_producer (struct dwarf2_cu *cu)
60d5a603
JK
10352{
10353 const char *cs;
10354 int major, minor, release;
10355
10356 if (cu->producer == NULL)
10357 {
10358 /* For unknown compilers expect their behavior is DWARF version
10359 compliant.
10360
10361 GCC started to support .debug_types sections by -gdwarf-4 since
10362 gcc-4.5.x. As the .debug_types sections are missing DW_AT_producer
10363 for their space efficiency GDB cannot workaround gcc-4.5.x -gdwarf-4
10364 combination. gcc-4.5.x -gdwarf-4 binaries have DW_AT_accessibility
10365 interpreted incorrectly by GDB now - GCC PR debug/48229. */
60d5a603 10366 }
685b1105 10367 else if (strncmp (cu->producer, "GNU ", strlen ("GNU ")) == 0)
60d5a603 10368 {
685b1105
JK
10369 /* Skip any identifier after "GNU " - such as "C++" or "Java". */
10370
ba919b58
TT
10371 cs = &cu->producer[strlen ("GNU ")];
10372 while (*cs && !isdigit (*cs))
10373 cs++;
10374 if (sscanf (cs, "%d.%d.%d", &major, &minor, &release) != 3)
10375 {
10376 /* Not recognized as GCC. */
10377 }
10378 else
1b80a9fa
JK
10379 {
10380 cu->producer_is_gxx_lt_4_6 = major < 4 || (major == 4 && minor < 6);
10381 cu->producer_is_gcc_lt_4_3 = major < 4 || (major == 4 && minor < 3);
10382 }
685b1105
JK
10383 }
10384 else if (strncmp (cu->producer, "Intel(R) C", strlen ("Intel(R) C")) == 0)
10385 cu->producer_is_icc = 1;
10386 else
10387 {
10388 /* For other non-GCC compilers, expect their behavior is DWARF version
10389 compliant. */
60d5a603
JK
10390 }
10391
ba919b58 10392 cu->checked_producer = 1;
685b1105 10393}
ba919b58 10394
685b1105
JK
10395/* Check for GCC PR debug/45124 fix which is not present in any G++ version up
10396 to 4.5.any while it is present already in G++ 4.6.0 - the PR has been fixed
10397 during 4.6.0 experimental. */
10398
10399static int
10400producer_is_gxx_lt_4_6 (struct dwarf2_cu *cu)
10401{
10402 if (!cu->checked_producer)
10403 check_producer (cu);
10404
10405 return cu->producer_is_gxx_lt_4_6;
60d5a603
JK
10406}
10407
10408/* Return the default accessibility type if it is not overriden by
10409 DW_AT_accessibility. */
10410
10411static enum dwarf_access_attribute
10412dwarf2_default_access_attribute (struct die_info *die, struct dwarf2_cu *cu)
10413{
10414 if (cu->header.version < 3 || producer_is_gxx_lt_4_6 (cu))
10415 {
10416 /* The default DWARF 2 accessibility for members is public, the default
10417 accessibility for inheritance is private. */
10418
10419 if (die->tag != DW_TAG_inheritance)
10420 return DW_ACCESS_public;
10421 else
10422 return DW_ACCESS_private;
10423 }
10424 else
10425 {
10426 /* DWARF 3+ defines the default accessibility a different way. The same
10427 rules apply now for DW_TAG_inheritance as for the members and it only
10428 depends on the container kind. */
10429
10430 if (die->parent->tag == DW_TAG_class_type)
10431 return DW_ACCESS_private;
10432 else
10433 return DW_ACCESS_public;
10434 }
10435}
10436
74ac6d43
TT
10437/* Look for DW_AT_data_member_location. Set *OFFSET to the byte
10438 offset. If the attribute was not found return 0, otherwise return
10439 1. If it was found but could not properly be handled, set *OFFSET
10440 to 0. */
10441
10442static int
10443handle_data_member_location (struct die_info *die, struct dwarf2_cu *cu,
10444 LONGEST *offset)
10445{
10446 struct attribute *attr;
10447
10448 attr = dwarf2_attr (die, DW_AT_data_member_location, cu);
10449 if (attr != NULL)
10450 {
10451 *offset = 0;
10452
10453 /* Note that we do not check for a section offset first here.
10454 This is because DW_AT_data_member_location is new in DWARF 4,
10455 so if we see it, we can assume that a constant form is really
10456 a constant and not a section offset. */
10457 if (attr_form_is_constant (attr))
10458 *offset = dwarf2_get_attr_constant_value (attr, 0);
10459 else if (attr_form_is_section_offset (attr))
10460 dwarf2_complex_location_expr_complaint ();
10461 else if (attr_form_is_block (attr))
10462 *offset = decode_locdesc (DW_BLOCK (attr), cu);
10463 else
10464 dwarf2_complex_location_expr_complaint ();
10465
10466 return 1;
10467 }
10468
10469 return 0;
10470}
10471
c906108c
SS
10472/* Add an aggregate field to the field list. */
10473
10474static void
107d2387 10475dwarf2_add_field (struct field_info *fip, struct die_info *die,
e7c27a73 10476 struct dwarf2_cu *cu)
6e70227d 10477{
e7c27a73 10478 struct objfile *objfile = cu->objfile;
5e2b427d 10479 struct gdbarch *gdbarch = get_objfile_arch (objfile);
c906108c
SS
10480 struct nextfield *new_field;
10481 struct attribute *attr;
10482 struct field *fp;
10483 char *fieldname = "";
10484
10485 /* Allocate a new field list entry and link it in. */
10486 new_field = (struct nextfield *) xmalloc (sizeof (struct nextfield));
b8c9b27d 10487 make_cleanup (xfree, new_field);
c906108c 10488 memset (new_field, 0, sizeof (struct nextfield));
7d0ccb61
DJ
10489
10490 if (die->tag == DW_TAG_inheritance)
10491 {
10492 new_field->next = fip->baseclasses;
10493 fip->baseclasses = new_field;
10494 }
10495 else
10496 {
10497 new_field->next = fip->fields;
10498 fip->fields = new_field;
10499 }
c906108c
SS
10500 fip->nfields++;
10501
e142c38c 10502 attr = dwarf2_attr (die, DW_AT_accessibility, cu);
c906108c
SS
10503 if (attr)
10504 new_field->accessibility = DW_UNSND (attr);
60d5a603
JK
10505 else
10506 new_field->accessibility = dwarf2_default_access_attribute (die, cu);
c906108c
SS
10507 if (new_field->accessibility != DW_ACCESS_public)
10508 fip->non_public_fields = 1;
60d5a603 10509
e142c38c 10510 attr = dwarf2_attr (die, DW_AT_virtuality, cu);
c906108c
SS
10511 if (attr)
10512 new_field->virtuality = DW_UNSND (attr);
60d5a603
JK
10513 else
10514 new_field->virtuality = DW_VIRTUALITY_none;
c906108c
SS
10515
10516 fp = &new_field->field;
a9a9bd0f 10517
e142c38c 10518 if (die->tag == DW_TAG_member && ! die_is_declaration (die, cu))
c906108c 10519 {
74ac6d43
TT
10520 LONGEST offset;
10521
a9a9bd0f 10522 /* Data member other than a C++ static data member. */
6e70227d 10523
c906108c 10524 /* Get type of field. */
e7c27a73 10525 fp->type = die_type (die, cu);
c906108c 10526
d6a843b5 10527 SET_FIELD_BITPOS (*fp, 0);
01ad7f36 10528
c906108c 10529 /* Get bit size of field (zero if none). */
e142c38c 10530 attr = dwarf2_attr (die, DW_AT_bit_size, cu);
c906108c
SS
10531 if (attr)
10532 {
10533 FIELD_BITSIZE (*fp) = DW_UNSND (attr);
10534 }
10535 else
10536 {
10537 FIELD_BITSIZE (*fp) = 0;
10538 }
10539
10540 /* Get bit offset of field. */
74ac6d43
TT
10541 if (handle_data_member_location (die, cu, &offset))
10542 SET_FIELD_BITPOS (*fp, offset * bits_per_byte);
e142c38c 10543 attr = dwarf2_attr (die, DW_AT_bit_offset, cu);
c906108c
SS
10544 if (attr)
10545 {
5e2b427d 10546 if (gdbarch_bits_big_endian (gdbarch))
c906108c
SS
10547 {
10548 /* For big endian bits, the DW_AT_bit_offset gives the
c5aa993b
JM
10549 additional bit offset from the MSB of the containing
10550 anonymous object to the MSB of the field. We don't
10551 have to do anything special since we don't need to
10552 know the size of the anonymous object. */
f41f5e61 10553 SET_FIELD_BITPOS (*fp, FIELD_BITPOS (*fp) + DW_UNSND (attr));
c906108c
SS
10554 }
10555 else
10556 {
10557 /* For little endian bits, compute the bit offset to the
c5aa993b
JM
10558 MSB of the anonymous object, subtract off the number of
10559 bits from the MSB of the field to the MSB of the
10560 object, and then subtract off the number of bits of
10561 the field itself. The result is the bit offset of
10562 the LSB of the field. */
c906108c
SS
10563 int anonymous_size;
10564 int bit_offset = DW_UNSND (attr);
10565
e142c38c 10566 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
c906108c
SS
10567 if (attr)
10568 {
10569 /* The size of the anonymous object containing
10570 the bit field is explicit, so use the
10571 indicated size (in bytes). */
10572 anonymous_size = DW_UNSND (attr);
10573 }
10574 else
10575 {
10576 /* The size of the anonymous object containing
10577 the bit field must be inferred from the type
10578 attribute of the data member containing the
10579 bit field. */
10580 anonymous_size = TYPE_LENGTH (fp->type);
10581 }
f41f5e61
PA
10582 SET_FIELD_BITPOS (*fp,
10583 (FIELD_BITPOS (*fp)
10584 + anonymous_size * bits_per_byte
10585 - bit_offset - FIELD_BITSIZE (*fp)));
c906108c
SS
10586 }
10587 }
10588
10589 /* Get name of field. */
39cbfefa
DJ
10590 fieldname = dwarf2_name (die, cu);
10591 if (fieldname == NULL)
10592 fieldname = "";
d8151005
DJ
10593
10594 /* The name is already allocated along with this objfile, so we don't
10595 need to duplicate it for the type. */
10596 fp->name = fieldname;
c906108c
SS
10597
10598 /* Change accessibility for artificial fields (e.g. virtual table
c5aa993b 10599 pointer or virtual base class pointer) to private. */
e142c38c 10600 if (dwarf2_attr (die, DW_AT_artificial, cu))
c906108c 10601 {
d48cc9dd 10602 FIELD_ARTIFICIAL (*fp) = 1;
c906108c
SS
10603 new_field->accessibility = DW_ACCESS_private;
10604 fip->non_public_fields = 1;
10605 }
10606 }
a9a9bd0f 10607 else if (die->tag == DW_TAG_member || die->tag == DW_TAG_variable)
c906108c 10608 {
a9a9bd0f
DC
10609 /* C++ static member. */
10610
10611 /* NOTE: carlton/2002-11-05: It should be a DW_TAG_member that
10612 is a declaration, but all versions of G++ as of this writing
10613 (so through at least 3.2.1) incorrectly generate
10614 DW_TAG_variable tags. */
6e70227d 10615
ff355380 10616 const char *physname;
c906108c 10617
a9a9bd0f 10618 /* Get name of field. */
39cbfefa
DJ
10619 fieldname = dwarf2_name (die, cu);
10620 if (fieldname == NULL)
c906108c
SS
10621 return;
10622
254e6b9e 10623 attr = dwarf2_attr (die, DW_AT_const_value, cu);
3863f96c
DE
10624 if (attr
10625 /* Only create a symbol if this is an external value.
10626 new_symbol checks this and puts the value in the global symbol
10627 table, which we want. If it is not external, new_symbol
10628 will try to put the value in cu->list_in_scope which is wrong. */
10629 && dwarf2_flag_true_p (die, DW_AT_external, cu))
254e6b9e
DE
10630 {
10631 /* A static const member, not much different than an enum as far as
10632 we're concerned, except that we can support more types. */
10633 new_symbol (die, NULL, cu);
10634 }
10635
2df3850c 10636 /* Get physical name. */
ff355380 10637 physname = dwarf2_physname (fieldname, die, cu);
c906108c 10638
d8151005
DJ
10639 /* The name is already allocated along with this objfile, so we don't
10640 need to duplicate it for the type. */
10641 SET_FIELD_PHYSNAME (*fp, physname ? physname : "");
e7c27a73 10642 FIELD_TYPE (*fp) = die_type (die, cu);
d8151005 10643 FIELD_NAME (*fp) = fieldname;
c906108c
SS
10644 }
10645 else if (die->tag == DW_TAG_inheritance)
10646 {
74ac6d43 10647 LONGEST offset;
d4b96c9a 10648
74ac6d43
TT
10649 /* C++ base class field. */
10650 if (handle_data_member_location (die, cu, &offset))
10651 SET_FIELD_BITPOS (*fp, offset * bits_per_byte);
c906108c 10652 FIELD_BITSIZE (*fp) = 0;
e7c27a73 10653 FIELD_TYPE (*fp) = die_type (die, cu);
c906108c
SS
10654 FIELD_NAME (*fp) = type_name_no_tag (fp->type);
10655 fip->nbaseclasses++;
10656 }
10657}
10658
98751a41
JK
10659/* Add a typedef defined in the scope of the FIP's class. */
10660
10661static void
10662dwarf2_add_typedef (struct field_info *fip, struct die_info *die,
10663 struct dwarf2_cu *cu)
6e70227d 10664{
98751a41 10665 struct objfile *objfile = cu->objfile;
98751a41
JK
10666 struct typedef_field_list *new_field;
10667 struct attribute *attr;
10668 struct typedef_field *fp;
10669 char *fieldname = "";
10670
10671 /* Allocate a new field list entry and link it in. */
10672 new_field = xzalloc (sizeof (*new_field));
10673 make_cleanup (xfree, new_field);
10674
10675 gdb_assert (die->tag == DW_TAG_typedef);
10676
10677 fp = &new_field->field;
10678
10679 /* Get name of field. */
10680 fp->name = dwarf2_name (die, cu);
10681 if (fp->name == NULL)
10682 return;
10683
10684 fp->type = read_type_die (die, cu);
10685
10686 new_field->next = fip->typedef_field_list;
10687 fip->typedef_field_list = new_field;
10688 fip->typedef_field_list_count++;
10689}
10690
c906108c
SS
10691/* Create the vector of fields, and attach it to the type. */
10692
10693static void
fba45db2 10694dwarf2_attach_fields_to_type (struct field_info *fip, struct type *type,
e7c27a73 10695 struct dwarf2_cu *cu)
c906108c
SS
10696{
10697 int nfields = fip->nfields;
10698
10699 /* Record the field count, allocate space for the array of fields,
10700 and create blank accessibility bitfields if necessary. */
10701 TYPE_NFIELDS (type) = nfields;
10702 TYPE_FIELDS (type) = (struct field *)
10703 TYPE_ALLOC (type, sizeof (struct field) * nfields);
10704 memset (TYPE_FIELDS (type), 0, sizeof (struct field) * nfields);
10705
b4ba55a1 10706 if (fip->non_public_fields && cu->language != language_ada)
c906108c
SS
10707 {
10708 ALLOCATE_CPLUS_STRUCT_TYPE (type);
10709
10710 TYPE_FIELD_PRIVATE_BITS (type) =
10711 (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
10712 B_CLRALL (TYPE_FIELD_PRIVATE_BITS (type), nfields);
10713
10714 TYPE_FIELD_PROTECTED_BITS (type) =
10715 (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
10716 B_CLRALL (TYPE_FIELD_PROTECTED_BITS (type), nfields);
10717
774b6a14
TT
10718 TYPE_FIELD_IGNORE_BITS (type) =
10719 (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
10720 B_CLRALL (TYPE_FIELD_IGNORE_BITS (type), nfields);
c906108c
SS
10721 }
10722
10723 /* If the type has baseclasses, allocate and clear a bit vector for
10724 TYPE_FIELD_VIRTUAL_BITS. */
b4ba55a1 10725 if (fip->nbaseclasses && cu->language != language_ada)
c906108c
SS
10726 {
10727 int num_bytes = B_BYTES (fip->nbaseclasses);
fe1b8b76 10728 unsigned char *pointer;
c906108c
SS
10729
10730 ALLOCATE_CPLUS_STRUCT_TYPE (type);
fe1b8b76
JB
10731 pointer = TYPE_ALLOC (type, num_bytes);
10732 TYPE_FIELD_VIRTUAL_BITS (type) = pointer;
c906108c
SS
10733 B_CLRALL (TYPE_FIELD_VIRTUAL_BITS (type), fip->nbaseclasses);
10734 TYPE_N_BASECLASSES (type) = fip->nbaseclasses;
10735 }
10736
3e43a32a
MS
10737 /* Copy the saved-up fields into the field vector. Start from the head of
10738 the list, adding to the tail of the field array, so that they end up in
10739 the same order in the array in which they were added to the list. */
c906108c
SS
10740 while (nfields-- > 0)
10741 {
7d0ccb61
DJ
10742 struct nextfield *fieldp;
10743
10744 if (fip->fields)
10745 {
10746 fieldp = fip->fields;
10747 fip->fields = fieldp->next;
10748 }
10749 else
10750 {
10751 fieldp = fip->baseclasses;
10752 fip->baseclasses = fieldp->next;
10753 }
10754
10755 TYPE_FIELD (type, nfields) = fieldp->field;
10756 switch (fieldp->accessibility)
c906108c 10757 {
c5aa993b 10758 case DW_ACCESS_private:
b4ba55a1
JB
10759 if (cu->language != language_ada)
10760 SET_TYPE_FIELD_PRIVATE (type, nfields);
c5aa993b 10761 break;
c906108c 10762
c5aa993b 10763 case DW_ACCESS_protected:
b4ba55a1
JB
10764 if (cu->language != language_ada)
10765 SET_TYPE_FIELD_PROTECTED (type, nfields);
c5aa993b 10766 break;
c906108c 10767
c5aa993b
JM
10768 case DW_ACCESS_public:
10769 break;
c906108c 10770
c5aa993b
JM
10771 default:
10772 /* Unknown accessibility. Complain and treat it as public. */
10773 {
e2e0b3e5 10774 complaint (&symfile_complaints, _("unsupported accessibility %d"),
7d0ccb61 10775 fieldp->accessibility);
c5aa993b
JM
10776 }
10777 break;
c906108c
SS
10778 }
10779 if (nfields < fip->nbaseclasses)
10780 {
7d0ccb61 10781 switch (fieldp->virtuality)
c906108c 10782 {
c5aa993b
JM
10783 case DW_VIRTUALITY_virtual:
10784 case DW_VIRTUALITY_pure_virtual:
b4ba55a1 10785 if (cu->language == language_ada)
a73c6dcd 10786 error (_("unexpected virtuality in component of Ada type"));
c5aa993b
JM
10787 SET_TYPE_FIELD_VIRTUAL (type, nfields);
10788 break;
c906108c
SS
10789 }
10790 }
c906108c
SS
10791 }
10792}
10793
c906108c
SS
10794/* Add a member function to the proper fieldlist. */
10795
10796static void
107d2387 10797dwarf2_add_member_fn (struct field_info *fip, struct die_info *die,
e7c27a73 10798 struct type *type, struct dwarf2_cu *cu)
c906108c 10799{
e7c27a73 10800 struct objfile *objfile = cu->objfile;
c906108c
SS
10801 struct attribute *attr;
10802 struct fnfieldlist *flp;
10803 int i;
10804 struct fn_field *fnp;
10805 char *fieldname;
c906108c 10806 struct nextfnfield *new_fnfield;
f792889a 10807 struct type *this_type;
60d5a603 10808 enum dwarf_access_attribute accessibility;
c906108c 10809
b4ba55a1 10810 if (cu->language == language_ada)
a73c6dcd 10811 error (_("unexpected member function in Ada type"));
b4ba55a1 10812
2df3850c 10813 /* Get name of member function. */
39cbfefa
DJ
10814 fieldname = dwarf2_name (die, cu);
10815 if (fieldname == NULL)
2df3850c 10816 return;
c906108c 10817
c906108c
SS
10818 /* Look up member function name in fieldlist. */
10819 for (i = 0; i < fip->nfnfields; i++)
10820 {
27bfe10e 10821 if (strcmp (fip->fnfieldlists[i].name, fieldname) == 0)
c906108c
SS
10822 break;
10823 }
10824
10825 /* Create new list element if necessary. */
10826 if (i < fip->nfnfields)
10827 flp = &fip->fnfieldlists[i];
10828 else
10829 {
10830 if ((fip->nfnfields % DW_FIELD_ALLOC_CHUNK) == 0)
10831 {
10832 fip->fnfieldlists = (struct fnfieldlist *)
10833 xrealloc (fip->fnfieldlists,
10834 (fip->nfnfields + DW_FIELD_ALLOC_CHUNK)
c5aa993b 10835 * sizeof (struct fnfieldlist));
c906108c 10836 if (fip->nfnfields == 0)
c13c43fd 10837 make_cleanup (free_current_contents, &fip->fnfieldlists);
c906108c
SS
10838 }
10839 flp = &fip->fnfieldlists[fip->nfnfields];
10840 flp->name = fieldname;
10841 flp->length = 0;
10842 flp->head = NULL;
3da10d80 10843 i = fip->nfnfields++;
c906108c
SS
10844 }
10845
10846 /* Create a new member function field and chain it to the field list
0963b4bd 10847 entry. */
c906108c 10848 new_fnfield = (struct nextfnfield *) xmalloc (sizeof (struct nextfnfield));
b8c9b27d 10849 make_cleanup (xfree, new_fnfield);
c906108c
SS
10850 memset (new_fnfield, 0, sizeof (struct nextfnfield));
10851 new_fnfield->next = flp->head;
10852 flp->head = new_fnfield;
10853 flp->length++;
10854
10855 /* Fill in the member function field info. */
10856 fnp = &new_fnfield->fnfield;
3da10d80
KS
10857
10858 /* Delay processing of the physname until later. */
10859 if (cu->language == language_cplus || cu->language == language_java)
10860 {
10861 add_to_method_list (type, i, flp->length - 1, fieldname,
10862 die, cu);
10863 }
10864 else
10865 {
1d06ead6 10866 const char *physname = dwarf2_physname (fieldname, die, cu);
3da10d80
KS
10867 fnp->physname = physname ? physname : "";
10868 }
10869
c906108c 10870 fnp->type = alloc_type (objfile);
f792889a
DJ
10871 this_type = read_type_die (die, cu);
10872 if (this_type && TYPE_CODE (this_type) == TYPE_CODE_FUNC)
c906108c 10873 {
f792889a 10874 int nparams = TYPE_NFIELDS (this_type);
c906108c 10875
f792889a 10876 /* TYPE is the domain of this method, and THIS_TYPE is the type
e26fb1d7
DC
10877 of the method itself (TYPE_CODE_METHOD). */
10878 smash_to_method_type (fnp->type, type,
f792889a
DJ
10879 TYPE_TARGET_TYPE (this_type),
10880 TYPE_FIELDS (this_type),
10881 TYPE_NFIELDS (this_type),
10882 TYPE_VARARGS (this_type));
c906108c
SS
10883
10884 /* Handle static member functions.
c5aa993b 10885 Dwarf2 has no clean way to discern C++ static and non-static
0963b4bd
MS
10886 member functions. G++ helps GDB by marking the first
10887 parameter for non-static member functions (which is the this
10888 pointer) as artificial. We obtain this information from
10889 read_subroutine_type via TYPE_FIELD_ARTIFICIAL. */
f792889a 10890 if (nparams == 0 || TYPE_FIELD_ARTIFICIAL (this_type, 0) == 0)
c906108c
SS
10891 fnp->voffset = VOFFSET_STATIC;
10892 }
10893 else
e2e0b3e5 10894 complaint (&symfile_complaints, _("member function type missing for '%s'"),
3da10d80 10895 dwarf2_full_name (fieldname, die, cu));
c906108c
SS
10896
10897 /* Get fcontext from DW_AT_containing_type if present. */
e142c38c 10898 if (dwarf2_attr (die, DW_AT_containing_type, cu) != NULL)
e7c27a73 10899 fnp->fcontext = die_containing_type (die, cu);
c906108c 10900
3e43a32a
MS
10901 /* dwarf2 doesn't have stubbed physical names, so the setting of is_const and
10902 is_volatile is irrelevant, as it is needed by gdb_mangle_name only. */
c906108c
SS
10903
10904 /* Get accessibility. */
e142c38c 10905 attr = dwarf2_attr (die, DW_AT_accessibility, cu);
c906108c 10906 if (attr)
60d5a603
JK
10907 accessibility = DW_UNSND (attr);
10908 else
10909 accessibility = dwarf2_default_access_attribute (die, cu);
10910 switch (accessibility)
c906108c 10911 {
60d5a603
JK
10912 case DW_ACCESS_private:
10913 fnp->is_private = 1;
10914 break;
10915 case DW_ACCESS_protected:
10916 fnp->is_protected = 1;
10917 break;
c906108c
SS
10918 }
10919
b02dede2 10920 /* Check for artificial methods. */
e142c38c 10921 attr = dwarf2_attr (die, DW_AT_artificial, cu);
b02dede2
DJ
10922 if (attr && DW_UNSND (attr) != 0)
10923 fnp->is_artificial = 1;
10924
0d564a31 10925 /* Get index in virtual function table if it is a virtual member
aec5aa8b
TT
10926 function. For older versions of GCC, this is an offset in the
10927 appropriate virtual table, as specified by DW_AT_containing_type.
10928 For everyone else, it is an expression to be evaluated relative
0d564a31
DJ
10929 to the object address. */
10930
e142c38c 10931 attr = dwarf2_attr (die, DW_AT_vtable_elem_location, cu);
aec5aa8b 10932 if (attr)
8e19ed76 10933 {
aec5aa8b 10934 if (attr_form_is_block (attr) && DW_BLOCK (attr)->size > 0)
8e19ed76 10935 {
aec5aa8b
TT
10936 if (DW_BLOCK (attr)->data[0] == DW_OP_constu)
10937 {
10938 /* Old-style GCC. */
10939 fnp->voffset = decode_locdesc (DW_BLOCK (attr), cu) + 2;
10940 }
10941 else if (DW_BLOCK (attr)->data[0] == DW_OP_deref
10942 || (DW_BLOCK (attr)->size > 1
10943 && DW_BLOCK (attr)->data[0] == DW_OP_deref_size
10944 && DW_BLOCK (attr)->data[1] == cu->header.addr_size))
10945 {
10946 struct dwarf_block blk;
10947 int offset;
10948
10949 offset = (DW_BLOCK (attr)->data[0] == DW_OP_deref
10950 ? 1 : 2);
10951 blk.size = DW_BLOCK (attr)->size - offset;
10952 blk.data = DW_BLOCK (attr)->data + offset;
10953 fnp->voffset = decode_locdesc (DW_BLOCK (attr), cu);
10954 if ((fnp->voffset % cu->header.addr_size) != 0)
10955 dwarf2_complex_location_expr_complaint ();
10956 else
10957 fnp->voffset /= cu->header.addr_size;
10958 fnp->voffset += 2;
10959 }
10960 else
10961 dwarf2_complex_location_expr_complaint ();
10962
10963 if (!fnp->fcontext)
10964 fnp->fcontext = TYPE_TARGET_TYPE (TYPE_FIELD_TYPE (this_type, 0));
10965 }
3690dd37 10966 else if (attr_form_is_section_offset (attr))
8e19ed76 10967 {
4d3c2250 10968 dwarf2_complex_location_expr_complaint ();
8e19ed76
PS
10969 }
10970 else
10971 {
4d3c2250
KB
10972 dwarf2_invalid_attrib_class_complaint ("DW_AT_vtable_elem_location",
10973 fieldname);
8e19ed76 10974 }
0d564a31 10975 }
d48cc9dd
DJ
10976 else
10977 {
10978 attr = dwarf2_attr (die, DW_AT_virtuality, cu);
10979 if (attr && DW_UNSND (attr))
10980 {
10981 /* GCC does this, as of 2008-08-25; PR debug/37237. */
10982 complaint (&symfile_complaints,
3e43a32a
MS
10983 _("Member function \"%s\" (offset %d) is virtual "
10984 "but the vtable offset is not specified"),
b64f50a1 10985 fieldname, die->offset.sect_off);
9655fd1a 10986 ALLOCATE_CPLUS_STRUCT_TYPE (type);
d48cc9dd
DJ
10987 TYPE_CPLUS_DYNAMIC (type) = 1;
10988 }
10989 }
c906108c
SS
10990}
10991
10992/* Create the vector of member function fields, and attach it to the type. */
10993
10994static void
fba45db2 10995dwarf2_attach_fn_fields_to_type (struct field_info *fip, struct type *type,
e7c27a73 10996 struct dwarf2_cu *cu)
c906108c
SS
10997{
10998 struct fnfieldlist *flp;
c906108c
SS
10999 int i;
11000
b4ba55a1 11001 if (cu->language == language_ada)
a73c6dcd 11002 error (_("unexpected member functions in Ada type"));
b4ba55a1 11003
c906108c
SS
11004 ALLOCATE_CPLUS_STRUCT_TYPE (type);
11005 TYPE_FN_FIELDLISTS (type) = (struct fn_fieldlist *)
11006 TYPE_ALLOC (type, sizeof (struct fn_fieldlist) * fip->nfnfields);
11007
11008 for (i = 0, flp = fip->fnfieldlists; i < fip->nfnfields; i++, flp++)
11009 {
11010 struct nextfnfield *nfp = flp->head;
11011 struct fn_fieldlist *fn_flp = &TYPE_FN_FIELDLIST (type, i);
11012 int k;
11013
11014 TYPE_FN_FIELDLIST_NAME (type, i) = flp->name;
11015 TYPE_FN_FIELDLIST_LENGTH (type, i) = flp->length;
11016 fn_flp->fn_fields = (struct fn_field *)
11017 TYPE_ALLOC (type, sizeof (struct fn_field) * flp->length);
11018 for (k = flp->length; (k--, nfp); nfp = nfp->next)
c5aa993b 11019 fn_flp->fn_fields[k] = nfp->fnfield;
c906108c
SS
11020 }
11021
11022 TYPE_NFN_FIELDS (type) = fip->nfnfields;
c906108c
SS
11023}
11024
1168df01
JB
11025/* Returns non-zero if NAME is the name of a vtable member in CU's
11026 language, zero otherwise. */
11027static int
11028is_vtable_name (const char *name, struct dwarf2_cu *cu)
11029{
11030 static const char vptr[] = "_vptr";
987504bb 11031 static const char vtable[] = "vtable";
1168df01 11032
987504bb
JJ
11033 /* Look for the C++ and Java forms of the vtable. */
11034 if ((cu->language == language_java
11035 && strncmp (name, vtable, sizeof (vtable) - 1) == 0)
11036 || (strncmp (name, vptr, sizeof (vptr) - 1) == 0
11037 && is_cplus_marker (name[sizeof (vptr) - 1])))
1168df01
JB
11038 return 1;
11039
11040 return 0;
11041}
11042
c0dd20ea 11043/* GCC outputs unnamed structures that are really pointers to member
0b92b5bb
TT
11044 functions, with the ABI-specified layout. If TYPE describes
11045 such a structure, smash it into a member function type.
61049d3b
DJ
11046
11047 GCC shouldn't do this; it should just output pointer to member DIEs.
11048 This is GCC PR debug/28767. */
c0dd20ea 11049
0b92b5bb
TT
11050static void
11051quirk_gcc_member_function_pointer (struct type *type, struct objfile *objfile)
c0dd20ea 11052{
0b92b5bb 11053 struct type *pfn_type, *domain_type, *new_type;
c0dd20ea
DJ
11054
11055 /* Check for a structure with no name and two children. */
0b92b5bb
TT
11056 if (TYPE_CODE (type) != TYPE_CODE_STRUCT || TYPE_NFIELDS (type) != 2)
11057 return;
c0dd20ea
DJ
11058
11059 /* Check for __pfn and __delta members. */
0b92b5bb
TT
11060 if (TYPE_FIELD_NAME (type, 0) == NULL
11061 || strcmp (TYPE_FIELD_NAME (type, 0), "__pfn") != 0
11062 || TYPE_FIELD_NAME (type, 1) == NULL
11063 || strcmp (TYPE_FIELD_NAME (type, 1), "__delta") != 0)
11064 return;
c0dd20ea
DJ
11065
11066 /* Find the type of the method. */
0b92b5bb 11067 pfn_type = TYPE_FIELD_TYPE (type, 0);
c0dd20ea
DJ
11068 if (pfn_type == NULL
11069 || TYPE_CODE (pfn_type) != TYPE_CODE_PTR
11070 || TYPE_CODE (TYPE_TARGET_TYPE (pfn_type)) != TYPE_CODE_FUNC)
0b92b5bb 11071 return;
c0dd20ea
DJ
11072
11073 /* Look for the "this" argument. */
11074 pfn_type = TYPE_TARGET_TYPE (pfn_type);
11075 if (TYPE_NFIELDS (pfn_type) == 0
0b92b5bb 11076 /* || TYPE_FIELD_TYPE (pfn_type, 0) == NULL */
c0dd20ea 11077 || TYPE_CODE (TYPE_FIELD_TYPE (pfn_type, 0)) != TYPE_CODE_PTR)
0b92b5bb 11078 return;
c0dd20ea
DJ
11079
11080 domain_type = TYPE_TARGET_TYPE (TYPE_FIELD_TYPE (pfn_type, 0));
0b92b5bb
TT
11081 new_type = alloc_type (objfile);
11082 smash_to_method_type (new_type, domain_type, TYPE_TARGET_TYPE (pfn_type),
c0dd20ea
DJ
11083 TYPE_FIELDS (pfn_type), TYPE_NFIELDS (pfn_type),
11084 TYPE_VARARGS (pfn_type));
0b92b5bb 11085 smash_to_methodptr_type (type, new_type);
c0dd20ea 11086}
1168df01 11087
685b1105
JK
11088/* Return non-zero if the CU's PRODUCER string matches the Intel C/C++ compiler
11089 (icc). */
11090
11091static int
11092producer_is_icc (struct dwarf2_cu *cu)
11093{
11094 if (!cu->checked_producer)
11095 check_producer (cu);
11096
11097 return cu->producer_is_icc;
11098}
11099
c906108c 11100/* Called when we find the DIE that starts a structure or union scope
c767944b
DJ
11101 (definition) to create a type for the structure or union. Fill in
11102 the type's name and general properties; the members will not be
11103 processed until process_structure_type.
c906108c 11104
c767944b
DJ
11105 NOTE: we need to call these functions regardless of whether or not the
11106 DIE has a DW_AT_name attribute, since it might be an anonymous
c906108c
SS
11107 structure or union. This gets the type entered into our set of
11108 user defined types.
11109
11110 However, if the structure is incomplete (an opaque struct/union)
11111 then suppress creating a symbol table entry for it since gdb only
11112 wants to find the one with the complete definition. Note that if
11113 it is complete, we just call new_symbol, which does it's own
11114 checking about whether the struct/union is anonymous or not (and
11115 suppresses creating a symbol table entry itself). */
11116
f792889a 11117static struct type *
134d01f1 11118read_structure_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 11119{
e7c27a73 11120 struct objfile *objfile = cu->objfile;
c906108c
SS
11121 struct type *type;
11122 struct attribute *attr;
39cbfefa 11123 char *name;
c906108c 11124
348e048f
DE
11125 /* If the definition of this type lives in .debug_types, read that type.
11126 Don't follow DW_AT_specification though, that will take us back up
11127 the chain and we want to go down. */
45e58e77 11128 attr = dwarf2_attr_no_follow (die, DW_AT_signature);
348e048f
DE
11129 if (attr)
11130 {
11131 struct dwarf2_cu *type_cu = cu;
11132 struct die_info *type_die = follow_die_ref_or_sig (die, attr, &type_cu);
9a619af0 11133
348e048f
DE
11134 /* We could just recurse on read_structure_type, but we need to call
11135 get_die_type to ensure only one type for this DIE is created.
11136 This is important, for example, because for c++ classes we need
11137 TYPE_NAME set which is only done by new_symbol. Blech. */
11138 type = read_type_die (type_die, type_cu);
9dc481d3
DE
11139
11140 /* TYPE_CU may not be the same as CU.
11141 Ensure TYPE is recorded in CU's type_hash table. */
348e048f
DE
11142 return set_die_type (die, type, cu);
11143 }
11144
c0dd20ea 11145 type = alloc_type (objfile);
c906108c 11146 INIT_CPLUS_SPECIFIC (type);
93311388 11147
39cbfefa
DJ
11148 name = dwarf2_name (die, cu);
11149 if (name != NULL)
c906108c 11150 {
987504bb
JJ
11151 if (cu->language == language_cplus
11152 || cu->language == language_java)
63d06c5c 11153 {
3da10d80
KS
11154 char *full_name = (char *) dwarf2_full_name (name, die, cu);
11155
11156 /* dwarf2_full_name might have already finished building the DIE's
11157 type. If so, there is no need to continue. */
11158 if (get_die_type (die, cu) != NULL)
11159 return get_die_type (die, cu);
11160
11161 TYPE_TAG_NAME (type) = full_name;
94af9270
KS
11162 if (die->tag == DW_TAG_structure_type
11163 || die->tag == DW_TAG_class_type)
11164 TYPE_NAME (type) = TYPE_TAG_NAME (type);
63d06c5c
DC
11165 }
11166 else
11167 {
d8151005
DJ
11168 /* The name is already allocated along with this objfile, so
11169 we don't need to duplicate it for the type. */
94af9270
KS
11170 TYPE_TAG_NAME (type) = (char *) name;
11171 if (die->tag == DW_TAG_class_type)
11172 TYPE_NAME (type) = TYPE_TAG_NAME (type);
63d06c5c 11173 }
c906108c
SS
11174 }
11175
11176 if (die->tag == DW_TAG_structure_type)
11177 {
11178 TYPE_CODE (type) = TYPE_CODE_STRUCT;
11179 }
11180 else if (die->tag == DW_TAG_union_type)
11181 {
11182 TYPE_CODE (type) = TYPE_CODE_UNION;
11183 }
11184 else
11185 {
c906108c
SS
11186 TYPE_CODE (type) = TYPE_CODE_CLASS;
11187 }
11188
0cc2414c
TT
11189 if (cu->language == language_cplus && die->tag == DW_TAG_class_type)
11190 TYPE_DECLARED_CLASS (type) = 1;
11191
e142c38c 11192 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
c906108c
SS
11193 if (attr)
11194 {
11195 TYPE_LENGTH (type) = DW_UNSND (attr);
11196 }
11197 else
11198 {
11199 TYPE_LENGTH (type) = 0;
11200 }
11201
685b1105
JK
11202 if (producer_is_icc (cu))
11203 {
11204 /* ICC does not output the required DW_AT_declaration
11205 on incomplete types, but gives them a size of zero. */
11206 }
11207 else
11208 TYPE_STUB_SUPPORTED (type) = 1;
11209
dc718098 11210 if (die_is_declaration (die, cu))
876cecd0 11211 TYPE_STUB (type) = 1;
a6c727b2
DJ
11212 else if (attr == NULL && die->child == NULL
11213 && producer_is_realview (cu->producer))
11214 /* RealView does not output the required DW_AT_declaration
11215 on incomplete types. */
11216 TYPE_STUB (type) = 1;
dc718098 11217
c906108c
SS
11218 /* We need to add the type field to the die immediately so we don't
11219 infinitely recurse when dealing with pointers to the structure
0963b4bd 11220 type within the structure itself. */
1c379e20 11221 set_die_type (die, type, cu);
c906108c 11222
7e314c57
JK
11223 /* set_die_type should be already done. */
11224 set_descriptive_type (type, die, cu);
11225
c767944b
DJ
11226 return type;
11227}
11228
11229/* Finish creating a structure or union type, including filling in
11230 its members and creating a symbol for it. */
11231
11232static void
11233process_structure_scope (struct die_info *die, struct dwarf2_cu *cu)
11234{
11235 struct objfile *objfile = cu->objfile;
11236 struct die_info *child_die = die->child;
11237 struct type *type;
11238
11239 type = get_die_type (die, cu);
11240 if (type == NULL)
11241 type = read_structure_type (die, cu);
11242
e142c38c 11243 if (die->child != NULL && ! die_is_declaration (die, cu))
c906108c
SS
11244 {
11245 struct field_info fi;
11246 struct die_info *child_die;
34eaf542 11247 VEC (symbolp) *template_args = NULL;
c767944b 11248 struct cleanup *back_to = make_cleanup (null_cleanup, 0);
c906108c
SS
11249
11250 memset (&fi, 0, sizeof (struct field_info));
11251
639d11d3 11252 child_die = die->child;
c906108c
SS
11253
11254 while (child_die && child_die->tag)
11255 {
a9a9bd0f
DC
11256 if (child_die->tag == DW_TAG_member
11257 || child_die->tag == DW_TAG_variable)
c906108c 11258 {
a9a9bd0f
DC
11259 /* NOTE: carlton/2002-11-05: A C++ static data member
11260 should be a DW_TAG_member that is a declaration, but
11261 all versions of G++ as of this writing (so through at
11262 least 3.2.1) incorrectly generate DW_TAG_variable
11263 tags for them instead. */
e7c27a73 11264 dwarf2_add_field (&fi, child_die, cu);
c906108c 11265 }
8713b1b1 11266 else if (child_die->tag == DW_TAG_subprogram)
c906108c 11267 {
0963b4bd 11268 /* C++ member function. */
e7c27a73 11269 dwarf2_add_member_fn (&fi, child_die, type, cu);
c906108c
SS
11270 }
11271 else if (child_die->tag == DW_TAG_inheritance)
11272 {
11273 /* C++ base class field. */
e7c27a73 11274 dwarf2_add_field (&fi, child_die, cu);
c906108c 11275 }
98751a41
JK
11276 else if (child_die->tag == DW_TAG_typedef)
11277 dwarf2_add_typedef (&fi, child_die, cu);
34eaf542
TT
11278 else if (child_die->tag == DW_TAG_template_type_param
11279 || child_die->tag == DW_TAG_template_value_param)
11280 {
11281 struct symbol *arg = new_symbol (child_die, NULL, cu);
11282
f1078f66
DJ
11283 if (arg != NULL)
11284 VEC_safe_push (symbolp, template_args, arg);
34eaf542
TT
11285 }
11286
c906108c
SS
11287 child_die = sibling_die (child_die);
11288 }
11289
34eaf542
TT
11290 /* Attach template arguments to type. */
11291 if (! VEC_empty (symbolp, template_args))
11292 {
11293 ALLOCATE_CPLUS_STRUCT_TYPE (type);
11294 TYPE_N_TEMPLATE_ARGUMENTS (type)
11295 = VEC_length (symbolp, template_args);
11296 TYPE_TEMPLATE_ARGUMENTS (type)
11297 = obstack_alloc (&objfile->objfile_obstack,
11298 (TYPE_N_TEMPLATE_ARGUMENTS (type)
11299 * sizeof (struct symbol *)));
11300 memcpy (TYPE_TEMPLATE_ARGUMENTS (type),
11301 VEC_address (symbolp, template_args),
11302 (TYPE_N_TEMPLATE_ARGUMENTS (type)
11303 * sizeof (struct symbol *)));
11304 VEC_free (symbolp, template_args);
11305 }
11306
c906108c
SS
11307 /* Attach fields and member functions to the type. */
11308 if (fi.nfields)
e7c27a73 11309 dwarf2_attach_fields_to_type (&fi, type, cu);
c906108c
SS
11310 if (fi.nfnfields)
11311 {
e7c27a73 11312 dwarf2_attach_fn_fields_to_type (&fi, type, cu);
c906108c 11313
c5aa993b 11314 /* Get the type which refers to the base class (possibly this
c906108c 11315 class itself) which contains the vtable pointer for the current
0d564a31
DJ
11316 class from the DW_AT_containing_type attribute. This use of
11317 DW_AT_containing_type is a GNU extension. */
c906108c 11318
e142c38c 11319 if (dwarf2_attr (die, DW_AT_containing_type, cu) != NULL)
c906108c 11320 {
e7c27a73 11321 struct type *t = die_containing_type (die, cu);
c906108c
SS
11322
11323 TYPE_VPTR_BASETYPE (type) = t;
11324 if (type == t)
11325 {
c906108c
SS
11326 int i;
11327
11328 /* Our own class provides vtbl ptr. */
11329 for (i = TYPE_NFIELDS (t) - 1;
11330 i >= TYPE_N_BASECLASSES (t);
11331 --i)
11332 {
0d5cff50 11333 const char *fieldname = TYPE_FIELD_NAME (t, i);
c906108c 11334
1168df01 11335 if (is_vtable_name (fieldname, cu))
c906108c
SS
11336 {
11337 TYPE_VPTR_FIELDNO (type) = i;
11338 break;
11339 }
11340 }
11341
11342 /* Complain if virtual function table field not found. */
11343 if (i < TYPE_N_BASECLASSES (t))
4d3c2250 11344 complaint (&symfile_complaints,
3e43a32a
MS
11345 _("virtual function table pointer "
11346 "not found when defining class '%s'"),
4d3c2250
KB
11347 TYPE_TAG_NAME (type) ? TYPE_TAG_NAME (type) :
11348 "");
c906108c
SS
11349 }
11350 else
11351 {
11352 TYPE_VPTR_FIELDNO (type) = TYPE_VPTR_FIELDNO (t);
11353 }
11354 }
f6235d4c
EZ
11355 else if (cu->producer
11356 && strncmp (cu->producer,
11357 "IBM(R) XL C/C++ Advanced Edition", 32) == 0)
11358 {
11359 /* The IBM XLC compiler does not provide direct indication
11360 of the containing type, but the vtable pointer is
11361 always named __vfp. */
11362
11363 int i;
11364
11365 for (i = TYPE_NFIELDS (type) - 1;
11366 i >= TYPE_N_BASECLASSES (type);
11367 --i)
11368 {
11369 if (strcmp (TYPE_FIELD_NAME (type, i), "__vfp") == 0)
11370 {
11371 TYPE_VPTR_FIELDNO (type) = i;
11372 TYPE_VPTR_BASETYPE (type) = type;
11373 break;
11374 }
11375 }
11376 }
c906108c 11377 }
98751a41
JK
11378
11379 /* Copy fi.typedef_field_list linked list elements content into the
11380 allocated array TYPE_TYPEDEF_FIELD_ARRAY (type). */
11381 if (fi.typedef_field_list)
11382 {
11383 int i = fi.typedef_field_list_count;
11384
a0d7a4ff 11385 ALLOCATE_CPLUS_STRUCT_TYPE (type);
98751a41
JK
11386 TYPE_TYPEDEF_FIELD_ARRAY (type)
11387 = TYPE_ALLOC (type, sizeof (TYPE_TYPEDEF_FIELD (type, 0)) * i);
11388 TYPE_TYPEDEF_FIELD_COUNT (type) = i;
11389
11390 /* Reverse the list order to keep the debug info elements order. */
11391 while (--i >= 0)
11392 {
11393 struct typedef_field *dest, *src;
6e70227d 11394
98751a41
JK
11395 dest = &TYPE_TYPEDEF_FIELD (type, i);
11396 src = &fi.typedef_field_list->field;
11397 fi.typedef_field_list = fi.typedef_field_list->next;
11398 *dest = *src;
11399 }
11400 }
c767944b
DJ
11401
11402 do_cleanups (back_to);
eb2a6f42
TT
11403
11404 if (HAVE_CPLUS_STRUCT (type))
11405 TYPE_CPLUS_REALLY_JAVA (type) = cu->language == language_java;
c906108c 11406 }
63d06c5c 11407
bb5ed363 11408 quirk_gcc_member_function_pointer (type, objfile);
0b92b5bb 11409
90aeadfc
DC
11410 /* NOTE: carlton/2004-03-16: GCC 3.4 (or at least one of its
11411 snapshots) has been known to create a die giving a declaration
11412 for a class that has, as a child, a die giving a definition for a
11413 nested class. So we have to process our children even if the
11414 current die is a declaration. Normally, of course, a declaration
11415 won't have any children at all. */
134d01f1 11416
90aeadfc
DC
11417 while (child_die != NULL && child_die->tag)
11418 {
11419 if (child_die->tag == DW_TAG_member
11420 || child_die->tag == DW_TAG_variable
34eaf542
TT
11421 || child_die->tag == DW_TAG_inheritance
11422 || child_die->tag == DW_TAG_template_value_param
11423 || child_die->tag == DW_TAG_template_type_param)
134d01f1 11424 {
90aeadfc 11425 /* Do nothing. */
134d01f1 11426 }
90aeadfc
DC
11427 else
11428 process_die (child_die, cu);
134d01f1 11429
90aeadfc 11430 child_die = sibling_die (child_die);
134d01f1
DJ
11431 }
11432
fa4028e9
JB
11433 /* Do not consider external references. According to the DWARF standard,
11434 these DIEs are identified by the fact that they have no byte_size
11435 attribute, and a declaration attribute. */
11436 if (dwarf2_attr (die, DW_AT_byte_size, cu) != NULL
11437 || !die_is_declaration (die, cu))
c767944b 11438 new_symbol (die, type, cu);
134d01f1
DJ
11439}
11440
11441/* Given a DW_AT_enumeration_type die, set its type. We do not
11442 complete the type's fields yet, or create any symbols. */
c906108c 11443
f792889a 11444static struct type *
134d01f1 11445read_enumeration_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 11446{
e7c27a73 11447 struct objfile *objfile = cu->objfile;
c906108c 11448 struct type *type;
c906108c 11449 struct attribute *attr;
0114d602 11450 const char *name;
134d01f1 11451
348e048f
DE
11452 /* If the definition of this type lives in .debug_types, read that type.
11453 Don't follow DW_AT_specification though, that will take us back up
11454 the chain and we want to go down. */
45e58e77 11455 attr = dwarf2_attr_no_follow (die, DW_AT_signature);
348e048f
DE
11456 if (attr)
11457 {
11458 struct dwarf2_cu *type_cu = cu;
11459 struct die_info *type_die = follow_die_ref_or_sig (die, attr, &type_cu);
9a619af0 11460
348e048f 11461 type = read_type_die (type_die, type_cu);
9dc481d3
DE
11462
11463 /* TYPE_CU may not be the same as CU.
11464 Ensure TYPE is recorded in CU's type_hash table. */
348e048f
DE
11465 return set_die_type (die, type, cu);
11466 }
11467
c906108c
SS
11468 type = alloc_type (objfile);
11469
11470 TYPE_CODE (type) = TYPE_CODE_ENUM;
94af9270 11471 name = dwarf2_full_name (NULL, die, cu);
39cbfefa 11472 if (name != NULL)
0114d602 11473 TYPE_TAG_NAME (type) = (char *) name;
c906108c 11474
e142c38c 11475 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
c906108c
SS
11476 if (attr)
11477 {
11478 TYPE_LENGTH (type) = DW_UNSND (attr);
11479 }
11480 else
11481 {
11482 TYPE_LENGTH (type) = 0;
11483 }
11484
137033e9
JB
11485 /* The enumeration DIE can be incomplete. In Ada, any type can be
11486 declared as private in the package spec, and then defined only
11487 inside the package body. Such types are known as Taft Amendment
11488 Types. When another package uses such a type, an incomplete DIE
11489 may be generated by the compiler. */
02eb380e 11490 if (die_is_declaration (die, cu))
876cecd0 11491 TYPE_STUB (type) = 1;
02eb380e 11492
f792889a 11493 return set_die_type (die, type, cu);
134d01f1
DJ
11494}
11495
11496/* Given a pointer to a die which begins an enumeration, process all
11497 the dies that define the members of the enumeration, and create the
11498 symbol for the enumeration type.
11499
11500 NOTE: We reverse the order of the element list. */
11501
11502static void
11503process_enumeration_scope (struct die_info *die, struct dwarf2_cu *cu)
11504{
f792889a 11505 struct type *this_type;
134d01f1 11506
f792889a
DJ
11507 this_type = get_die_type (die, cu);
11508 if (this_type == NULL)
11509 this_type = read_enumeration_type (die, cu);
9dc481d3 11510
639d11d3 11511 if (die->child != NULL)
c906108c 11512 {
9dc481d3
DE
11513 struct die_info *child_die;
11514 struct symbol *sym;
11515 struct field *fields = NULL;
11516 int num_fields = 0;
11517 int unsigned_enum = 1;
11518 char *name;
cafec441
TT
11519 int flag_enum = 1;
11520 ULONGEST mask = 0;
9dc481d3 11521
639d11d3 11522 child_die = die->child;
c906108c
SS
11523 while (child_die && child_die->tag)
11524 {
11525 if (child_die->tag != DW_TAG_enumerator)
11526 {
e7c27a73 11527 process_die (child_die, cu);
c906108c
SS
11528 }
11529 else
11530 {
39cbfefa
DJ
11531 name = dwarf2_name (child_die, cu);
11532 if (name)
c906108c 11533 {
f792889a 11534 sym = new_symbol (child_die, this_type, cu);
c906108c 11535 if (SYMBOL_VALUE (sym) < 0)
cafec441
TT
11536 {
11537 unsigned_enum = 0;
11538 flag_enum = 0;
11539 }
11540 else if ((mask & SYMBOL_VALUE (sym)) != 0)
11541 flag_enum = 0;
11542 else
11543 mask |= SYMBOL_VALUE (sym);
c906108c
SS
11544
11545 if ((num_fields % DW_FIELD_ALLOC_CHUNK) == 0)
11546 {
11547 fields = (struct field *)
11548 xrealloc (fields,
11549 (num_fields + DW_FIELD_ALLOC_CHUNK)
c5aa993b 11550 * sizeof (struct field));
c906108c
SS
11551 }
11552
3567439c 11553 FIELD_NAME (fields[num_fields]) = SYMBOL_LINKAGE_NAME (sym);
c906108c 11554 FIELD_TYPE (fields[num_fields]) = NULL;
14e75d8e 11555 SET_FIELD_ENUMVAL (fields[num_fields], SYMBOL_VALUE (sym));
c906108c
SS
11556 FIELD_BITSIZE (fields[num_fields]) = 0;
11557
11558 num_fields++;
11559 }
11560 }
11561
11562 child_die = sibling_die (child_die);
11563 }
11564
11565 if (num_fields)
11566 {
f792889a
DJ
11567 TYPE_NFIELDS (this_type) = num_fields;
11568 TYPE_FIELDS (this_type) = (struct field *)
11569 TYPE_ALLOC (this_type, sizeof (struct field) * num_fields);
11570 memcpy (TYPE_FIELDS (this_type), fields,
c906108c 11571 sizeof (struct field) * num_fields);
b8c9b27d 11572 xfree (fields);
c906108c
SS
11573 }
11574 if (unsigned_enum)
876cecd0 11575 TYPE_UNSIGNED (this_type) = 1;
cafec441
TT
11576 if (flag_enum)
11577 TYPE_FLAG_ENUM (this_type) = 1;
c906108c 11578 }
134d01f1 11579
6c83ed52
TT
11580 /* If we are reading an enum from a .debug_types unit, and the enum
11581 is a declaration, and the enum is not the signatured type in the
11582 unit, then we do not want to add a symbol for it. Adding a
11583 symbol would in some cases obscure the true definition of the
11584 enum, giving users an incomplete type when the definition is
11585 actually available. Note that we do not want to do this for all
11586 enums which are just declarations, because C++0x allows forward
11587 enum declarations. */
3019eac3 11588 if (cu->per_cu->is_debug_types
6c83ed52
TT
11589 && die_is_declaration (die, cu))
11590 {
52dc124a 11591 struct signatured_type *sig_type;
6c83ed52 11592
52dc124a 11593 sig_type
6c83ed52 11594 = lookup_signatured_type_at_offset (dwarf2_per_objfile->objfile,
3019eac3 11595 cu->per_cu->info_or_types_section,
6c83ed52 11596 cu->per_cu->offset);
3019eac3
DE
11597 gdb_assert (sig_type->type_offset_in_section.sect_off != 0);
11598 if (sig_type->type_offset_in_section.sect_off != die->offset.sect_off)
6c83ed52
TT
11599 return;
11600 }
11601
f792889a 11602 new_symbol (die, this_type, cu);
c906108c
SS
11603}
11604
11605/* Extract all information from a DW_TAG_array_type DIE and put it in
11606 the DIE's type field. For now, this only handles one dimensional
11607 arrays. */
11608
f792889a 11609static struct type *
e7c27a73 11610read_array_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 11611{
e7c27a73 11612 struct objfile *objfile = cu->objfile;
c906108c 11613 struct die_info *child_die;
7e314c57 11614 struct type *type;
c906108c
SS
11615 struct type *element_type, *range_type, *index_type;
11616 struct type **range_types = NULL;
11617 struct attribute *attr;
11618 int ndim = 0;
11619 struct cleanup *back_to;
39cbfefa 11620 char *name;
c906108c 11621
e7c27a73 11622 element_type = die_type (die, cu);
c906108c 11623
7e314c57
JK
11624 /* The die_type call above may have already set the type for this DIE. */
11625 type = get_die_type (die, cu);
11626 if (type)
11627 return type;
11628
c906108c
SS
11629 /* Irix 6.2 native cc creates array types without children for
11630 arrays with unspecified length. */
639d11d3 11631 if (die->child == NULL)
c906108c 11632 {
46bf5051 11633 index_type = objfile_type (objfile)->builtin_int;
c906108c 11634 range_type = create_range_type (NULL, index_type, 0, -1);
f792889a
DJ
11635 type = create_array_type (NULL, element_type, range_type);
11636 return set_die_type (die, type, cu);
c906108c
SS
11637 }
11638
11639 back_to = make_cleanup (null_cleanup, NULL);
639d11d3 11640 child_die = die->child;
c906108c
SS
11641 while (child_die && child_die->tag)
11642 {
11643 if (child_die->tag == DW_TAG_subrange_type)
11644 {
f792889a 11645 struct type *child_type = read_type_die (child_die, cu);
9a619af0 11646
f792889a 11647 if (child_type != NULL)
a02abb62 11648 {
0963b4bd
MS
11649 /* The range type was succesfully read. Save it for the
11650 array type creation. */
a02abb62
JB
11651 if ((ndim % DW_FIELD_ALLOC_CHUNK) == 0)
11652 {
11653 range_types = (struct type **)
11654 xrealloc (range_types, (ndim + DW_FIELD_ALLOC_CHUNK)
11655 * sizeof (struct type *));
11656 if (ndim == 0)
11657 make_cleanup (free_current_contents, &range_types);
11658 }
f792889a 11659 range_types[ndim++] = child_type;
a02abb62 11660 }
c906108c
SS
11661 }
11662 child_die = sibling_die (child_die);
11663 }
11664
11665 /* Dwarf2 dimensions are output from left to right, create the
11666 necessary array types in backwards order. */
7ca2d3a3 11667
c906108c 11668 type = element_type;
7ca2d3a3
DL
11669
11670 if (read_array_order (die, cu) == DW_ORD_col_major)
11671 {
11672 int i = 0;
9a619af0 11673
7ca2d3a3
DL
11674 while (i < ndim)
11675 type = create_array_type (NULL, type, range_types[i++]);
11676 }
11677 else
11678 {
11679 while (ndim-- > 0)
11680 type = create_array_type (NULL, type, range_types[ndim]);
11681 }
c906108c 11682
f5f8a009
EZ
11683 /* Understand Dwarf2 support for vector types (like they occur on
11684 the PowerPC w/ AltiVec). Gcc just adds another attribute to the
11685 array type. This is not part of the Dwarf2/3 standard yet, but a
11686 custom vendor extension. The main difference between a regular
11687 array and the vector variant is that vectors are passed by value
11688 to functions. */
e142c38c 11689 attr = dwarf2_attr (die, DW_AT_GNU_vector, cu);
f5f8a009 11690 if (attr)
ea37ba09 11691 make_vector_type (type);
f5f8a009 11692
dbc98a8b
KW
11693 /* The DIE may have DW_AT_byte_size set. For example an OpenCL
11694 implementation may choose to implement triple vectors using this
11695 attribute. */
11696 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
11697 if (attr)
11698 {
11699 if (DW_UNSND (attr) >= TYPE_LENGTH (type))
11700 TYPE_LENGTH (type) = DW_UNSND (attr);
11701 else
3e43a32a
MS
11702 complaint (&symfile_complaints,
11703 _("DW_AT_byte_size for array type smaller "
11704 "than the total size of elements"));
dbc98a8b
KW
11705 }
11706
39cbfefa
DJ
11707 name = dwarf2_name (die, cu);
11708 if (name)
11709 TYPE_NAME (type) = name;
6e70227d 11710
0963b4bd 11711 /* Install the type in the die. */
7e314c57
JK
11712 set_die_type (die, type, cu);
11713
11714 /* set_die_type should be already done. */
b4ba55a1
JB
11715 set_descriptive_type (type, die, cu);
11716
c906108c
SS
11717 do_cleanups (back_to);
11718
7e314c57 11719 return type;
c906108c
SS
11720}
11721
7ca2d3a3 11722static enum dwarf_array_dim_ordering
6e70227d 11723read_array_order (struct die_info *die, struct dwarf2_cu *cu)
7ca2d3a3
DL
11724{
11725 struct attribute *attr;
11726
11727 attr = dwarf2_attr (die, DW_AT_ordering, cu);
11728
11729 if (attr) return DW_SND (attr);
11730
0963b4bd
MS
11731 /* GNU F77 is a special case, as at 08/2004 array type info is the
11732 opposite order to the dwarf2 specification, but data is still
11733 laid out as per normal fortran.
7ca2d3a3 11734
0963b4bd
MS
11735 FIXME: dsl/2004-8-20: If G77 is ever fixed, this will also need
11736 version checking. */
7ca2d3a3 11737
905e0470
PM
11738 if (cu->language == language_fortran
11739 && cu->producer && strstr (cu->producer, "GNU F77"))
7ca2d3a3
DL
11740 {
11741 return DW_ORD_row_major;
11742 }
11743
6e70227d 11744 switch (cu->language_defn->la_array_ordering)
7ca2d3a3
DL
11745 {
11746 case array_column_major:
11747 return DW_ORD_col_major;
11748 case array_row_major:
11749 default:
11750 return DW_ORD_row_major;
11751 };
11752}
11753
72019c9c 11754/* Extract all information from a DW_TAG_set_type DIE and put it in
0963b4bd 11755 the DIE's type field. */
72019c9c 11756
f792889a 11757static struct type *
72019c9c
GM
11758read_set_type (struct die_info *die, struct dwarf2_cu *cu)
11759{
7e314c57
JK
11760 struct type *domain_type, *set_type;
11761 struct attribute *attr;
f792889a 11762
7e314c57
JK
11763 domain_type = die_type (die, cu);
11764
11765 /* The die_type call above may have already set the type for this DIE. */
11766 set_type = get_die_type (die, cu);
11767 if (set_type)
11768 return set_type;
11769
11770 set_type = create_set_type (NULL, domain_type);
11771
11772 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
d09039dd
PM
11773 if (attr)
11774 TYPE_LENGTH (set_type) = DW_UNSND (attr);
7e314c57 11775
f792889a 11776 return set_die_type (die, set_type, cu);
72019c9c 11777}
7ca2d3a3 11778
0971de02
TT
11779/* A helper for read_common_block that creates a locexpr baton.
11780 SYM is the symbol which we are marking as computed.
11781 COMMON_DIE is the DIE for the common block.
11782 COMMON_LOC is the location expression attribute for the common
11783 block itself.
11784 MEMBER_LOC is the location expression attribute for the particular
11785 member of the common block that we are processing.
11786 CU is the CU from which the above come. */
11787
11788static void
11789mark_common_block_symbol_computed (struct symbol *sym,
11790 struct die_info *common_die,
11791 struct attribute *common_loc,
11792 struct attribute *member_loc,
11793 struct dwarf2_cu *cu)
11794{
11795 struct objfile *objfile = dwarf2_per_objfile->objfile;
11796 struct dwarf2_locexpr_baton *baton;
11797 gdb_byte *ptr;
11798 unsigned int cu_off;
11799 enum bfd_endian byte_order = gdbarch_byte_order (get_objfile_arch (objfile));
11800 LONGEST offset = 0;
11801
11802 gdb_assert (common_loc && member_loc);
11803 gdb_assert (attr_form_is_block (common_loc));
11804 gdb_assert (attr_form_is_block (member_loc)
11805 || attr_form_is_constant (member_loc));
11806
11807 baton = obstack_alloc (&objfile->objfile_obstack,
11808 sizeof (struct dwarf2_locexpr_baton));
11809 baton->per_cu = cu->per_cu;
11810 gdb_assert (baton->per_cu);
11811
11812 baton->size = 5 /* DW_OP_call4 */ + 1 /* DW_OP_plus */;
11813
11814 if (attr_form_is_constant (member_loc))
11815 {
11816 offset = dwarf2_get_attr_constant_value (member_loc, 0);
11817 baton->size += 1 /* DW_OP_addr */ + cu->header.addr_size;
11818 }
11819 else
11820 baton->size += DW_BLOCK (member_loc)->size;
11821
11822 ptr = obstack_alloc (&objfile->objfile_obstack, baton->size);
11823 baton->data = ptr;
11824
11825 *ptr++ = DW_OP_call4;
11826 cu_off = common_die->offset.sect_off - cu->per_cu->offset.sect_off;
11827 store_unsigned_integer (ptr, 4, byte_order, cu_off);
11828 ptr += 4;
11829
11830 if (attr_form_is_constant (member_loc))
11831 {
11832 *ptr++ = DW_OP_addr;
11833 store_unsigned_integer (ptr, cu->header.addr_size, byte_order, offset);
11834 ptr += cu->header.addr_size;
11835 }
11836 else
11837 {
11838 /* We have to copy the data here, because DW_OP_call4 will only
11839 use a DW_AT_location attribute. */
11840 memcpy (ptr, DW_BLOCK (member_loc)->data, DW_BLOCK (member_loc)->size);
11841 ptr += DW_BLOCK (member_loc)->size;
11842 }
11843
11844 *ptr++ = DW_OP_plus;
11845 gdb_assert (ptr - baton->data == baton->size);
11846
11847 SYMBOL_COMPUTED_OPS (sym) = &dwarf2_locexpr_funcs;
11848 SYMBOL_LOCATION_BATON (sym) = baton;
11849 SYMBOL_CLASS (sym) = LOC_COMPUTED;
11850}
11851
4357ac6c
TT
11852/* Create appropriate locally-scoped variables for all the
11853 DW_TAG_common_block entries. Also create a struct common_block
11854 listing all such variables for `info common'. COMMON_BLOCK_DOMAIN
11855 is used to sepate the common blocks name namespace from regular
11856 variable names. */
c906108c
SS
11857
11858static void
e7c27a73 11859read_common_block (struct die_info *die, struct dwarf2_cu *cu)
c906108c 11860{
0971de02
TT
11861 struct attribute *attr;
11862
11863 attr = dwarf2_attr (die, DW_AT_location, cu);
11864 if (attr)
11865 {
11866 /* Support the .debug_loc offsets. */
11867 if (attr_form_is_block (attr))
11868 {
11869 /* Ok. */
11870 }
11871 else if (attr_form_is_section_offset (attr))
11872 {
11873 dwarf2_complex_location_expr_complaint ();
11874 attr = NULL;
11875 }
11876 else
11877 {
11878 dwarf2_invalid_attrib_class_complaint ("DW_AT_location",
11879 "common block member");
11880 attr = NULL;
11881 }
11882 }
11883
639d11d3 11884 if (die->child != NULL)
c906108c 11885 {
4357ac6c
TT
11886 struct objfile *objfile = cu->objfile;
11887 struct die_info *child_die;
11888 size_t n_entries = 0, size;
11889 struct common_block *common_block;
11890 struct symbol *sym;
74ac6d43 11891
4357ac6c
TT
11892 for (child_die = die->child;
11893 child_die && child_die->tag;
11894 child_die = sibling_die (child_die))
11895 ++n_entries;
11896
11897 size = (sizeof (struct common_block)
11898 + (n_entries - 1) * sizeof (struct symbol *));
11899 common_block = obstack_alloc (&objfile->objfile_obstack, size);
11900 memset (common_block->contents, 0, n_entries * sizeof (struct symbol *));
11901 common_block->n_entries = 0;
11902
11903 for (child_die = die->child;
11904 child_die && child_die->tag;
11905 child_die = sibling_die (child_die))
11906 {
11907 /* Create the symbol in the DW_TAG_common_block block in the current
11908 symbol scope. */
e7c27a73 11909 sym = new_symbol (child_die, NULL, cu);
0971de02
TT
11910 if (sym != NULL)
11911 {
11912 struct attribute *member_loc;
11913
11914 common_block->contents[common_block->n_entries++] = sym;
11915
11916 member_loc = dwarf2_attr (child_die, DW_AT_data_member_location,
11917 cu);
11918 if (member_loc)
11919 {
11920 /* GDB has handled this for a long time, but it is
11921 not specified by DWARF. It seems to have been
11922 emitted by gfortran at least as recently as:
11923 http://gcc.gnu.org/bugzilla/show_bug.cgi?id=23057. */
11924 complaint (&symfile_complaints,
11925 _("Variable in common block has "
11926 "DW_AT_data_member_location "
11927 "- DIE at 0x%x [in module %s]"),
11928 child_die->offset.sect_off, cu->objfile->name);
11929
11930 if (attr_form_is_section_offset (member_loc))
11931 dwarf2_complex_location_expr_complaint ();
11932 else if (attr_form_is_constant (member_loc)
11933 || attr_form_is_block (member_loc))
11934 {
11935 if (attr)
11936 mark_common_block_symbol_computed (sym, die, attr,
11937 member_loc, cu);
11938 }
11939 else
11940 dwarf2_complex_location_expr_complaint ();
11941 }
11942 }
c906108c 11943 }
4357ac6c
TT
11944
11945 sym = new_symbol (die, objfile_type (objfile)->builtin_void, cu);
11946 SYMBOL_VALUE_COMMON_BLOCK (sym) = common_block;
c906108c
SS
11947 }
11948}
11949
0114d602 11950/* Create a type for a C++ namespace. */
d9fa45fe 11951
0114d602
DJ
11952static struct type *
11953read_namespace_type (struct die_info *die, struct dwarf2_cu *cu)
d9fa45fe 11954{
e7c27a73 11955 struct objfile *objfile = cu->objfile;
0114d602 11956 const char *previous_prefix, *name;
9219021c 11957 int is_anonymous;
0114d602
DJ
11958 struct type *type;
11959
11960 /* For extensions, reuse the type of the original namespace. */
11961 if (dwarf2_attr (die, DW_AT_extension, cu) != NULL)
11962 {
11963 struct die_info *ext_die;
11964 struct dwarf2_cu *ext_cu = cu;
9a619af0 11965
0114d602
DJ
11966 ext_die = dwarf2_extension (die, &ext_cu);
11967 type = read_type_die (ext_die, ext_cu);
9dc481d3
DE
11968
11969 /* EXT_CU may not be the same as CU.
11970 Ensure TYPE is recorded in CU's type_hash table. */
0114d602
DJ
11971 return set_die_type (die, type, cu);
11972 }
9219021c 11973
e142c38c 11974 name = namespace_name (die, &is_anonymous, cu);
9219021c
DC
11975
11976 /* Now build the name of the current namespace. */
11977
0114d602
DJ
11978 previous_prefix = determine_prefix (die, cu);
11979 if (previous_prefix[0] != '\0')
11980 name = typename_concat (&objfile->objfile_obstack,
f55ee35c 11981 previous_prefix, name, 0, cu);
0114d602
DJ
11982
11983 /* Create the type. */
11984 type = init_type (TYPE_CODE_NAMESPACE, 0, 0, NULL,
11985 objfile);
11986 TYPE_NAME (type) = (char *) name;
11987 TYPE_TAG_NAME (type) = TYPE_NAME (type);
11988
60531b24 11989 return set_die_type (die, type, cu);
0114d602
DJ
11990}
11991
11992/* Read a C++ namespace. */
11993
11994static void
11995read_namespace (struct die_info *die, struct dwarf2_cu *cu)
11996{
11997 struct objfile *objfile = cu->objfile;
0114d602 11998 int is_anonymous;
9219021c 11999
5c4e30ca
DC
12000 /* Add a symbol associated to this if we haven't seen the namespace
12001 before. Also, add a using directive if it's an anonymous
12002 namespace. */
9219021c 12003
f2f0e013 12004 if (dwarf2_attr (die, DW_AT_extension, cu) == NULL)
5c4e30ca
DC
12005 {
12006 struct type *type;
12007
0114d602 12008 type = read_type_die (die, cu);
e7c27a73 12009 new_symbol (die, type, cu);
5c4e30ca 12010
e8e80198 12011 namespace_name (die, &is_anonymous, cu);
5c4e30ca 12012 if (is_anonymous)
0114d602
DJ
12013 {
12014 const char *previous_prefix = determine_prefix (die, cu);
9a619af0 12015
c0cc3a76 12016 cp_add_using_directive (previous_prefix, TYPE_NAME (type), NULL,
32019081 12017 NULL, NULL, &objfile->objfile_obstack);
0114d602 12018 }
5c4e30ca 12019 }
9219021c 12020
639d11d3 12021 if (die->child != NULL)
d9fa45fe 12022 {
639d11d3 12023 struct die_info *child_die = die->child;
6e70227d 12024
d9fa45fe
DC
12025 while (child_die && child_die->tag)
12026 {
e7c27a73 12027 process_die (child_die, cu);
d9fa45fe
DC
12028 child_die = sibling_die (child_die);
12029 }
12030 }
38d518c9
EZ
12031}
12032
f55ee35c
JK
12033/* Read a Fortran module as type. This DIE can be only a declaration used for
12034 imported module. Still we need that type as local Fortran "use ... only"
12035 declaration imports depend on the created type in determine_prefix. */
12036
12037static struct type *
12038read_module_type (struct die_info *die, struct dwarf2_cu *cu)
12039{
12040 struct objfile *objfile = cu->objfile;
12041 char *module_name;
12042 struct type *type;
12043
12044 module_name = dwarf2_name (die, cu);
12045 if (!module_name)
3e43a32a
MS
12046 complaint (&symfile_complaints,
12047 _("DW_TAG_module has no name, offset 0x%x"),
b64f50a1 12048 die->offset.sect_off);
f55ee35c
JK
12049 type = init_type (TYPE_CODE_MODULE, 0, 0, module_name, objfile);
12050
12051 /* determine_prefix uses TYPE_TAG_NAME. */
12052 TYPE_TAG_NAME (type) = TYPE_NAME (type);
12053
12054 return set_die_type (die, type, cu);
12055}
12056
5d7cb8df
JK
12057/* Read a Fortran module. */
12058
12059static void
12060read_module (struct die_info *die, struct dwarf2_cu *cu)
12061{
12062 struct die_info *child_die = die->child;
12063
5d7cb8df
JK
12064 while (child_die && child_die->tag)
12065 {
12066 process_die (child_die, cu);
12067 child_die = sibling_die (child_die);
12068 }
12069}
12070
38d518c9
EZ
12071/* Return the name of the namespace represented by DIE. Set
12072 *IS_ANONYMOUS to tell whether or not the namespace is an anonymous
12073 namespace. */
12074
12075static const char *
e142c38c 12076namespace_name (struct die_info *die, int *is_anonymous, struct dwarf2_cu *cu)
38d518c9
EZ
12077{
12078 struct die_info *current_die;
12079 const char *name = NULL;
12080
12081 /* Loop through the extensions until we find a name. */
12082
12083 for (current_die = die;
12084 current_die != NULL;
f2f0e013 12085 current_die = dwarf2_extension (die, &cu))
38d518c9 12086 {
e142c38c 12087 name = dwarf2_name (current_die, cu);
38d518c9
EZ
12088 if (name != NULL)
12089 break;
12090 }
12091
12092 /* Is it an anonymous namespace? */
12093
12094 *is_anonymous = (name == NULL);
12095 if (*is_anonymous)
2b1dbab0 12096 name = CP_ANONYMOUS_NAMESPACE_STR;
38d518c9
EZ
12097
12098 return name;
d9fa45fe
DC
12099}
12100
c906108c
SS
12101/* Extract all information from a DW_TAG_pointer_type DIE and add to
12102 the user defined type vector. */
12103
f792889a 12104static struct type *
e7c27a73 12105read_tag_pointer_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 12106{
5e2b427d 12107 struct gdbarch *gdbarch = get_objfile_arch (cu->objfile);
e7c27a73 12108 struct comp_unit_head *cu_header = &cu->header;
c906108c 12109 struct type *type;
8b2dbe47
KB
12110 struct attribute *attr_byte_size;
12111 struct attribute *attr_address_class;
12112 int byte_size, addr_class;
7e314c57
JK
12113 struct type *target_type;
12114
12115 target_type = die_type (die, cu);
c906108c 12116
7e314c57
JK
12117 /* The die_type call above may have already set the type for this DIE. */
12118 type = get_die_type (die, cu);
12119 if (type)
12120 return type;
12121
12122 type = lookup_pointer_type (target_type);
8b2dbe47 12123
e142c38c 12124 attr_byte_size = dwarf2_attr (die, DW_AT_byte_size, cu);
8b2dbe47
KB
12125 if (attr_byte_size)
12126 byte_size = DW_UNSND (attr_byte_size);
c906108c 12127 else
8b2dbe47
KB
12128 byte_size = cu_header->addr_size;
12129
e142c38c 12130 attr_address_class = dwarf2_attr (die, DW_AT_address_class, cu);
8b2dbe47
KB
12131 if (attr_address_class)
12132 addr_class = DW_UNSND (attr_address_class);
12133 else
12134 addr_class = DW_ADDR_none;
12135
12136 /* If the pointer size or address class is different than the
12137 default, create a type variant marked as such and set the
12138 length accordingly. */
12139 if (TYPE_LENGTH (type) != byte_size || addr_class != DW_ADDR_none)
c906108c 12140 {
5e2b427d 12141 if (gdbarch_address_class_type_flags_p (gdbarch))
8b2dbe47
KB
12142 {
12143 int type_flags;
12144
849957d9 12145 type_flags = gdbarch_address_class_type_flags
5e2b427d 12146 (gdbarch, byte_size, addr_class);
876cecd0
TT
12147 gdb_assert ((type_flags & ~TYPE_INSTANCE_FLAG_ADDRESS_CLASS_ALL)
12148 == 0);
8b2dbe47
KB
12149 type = make_type_with_address_space (type, type_flags);
12150 }
12151 else if (TYPE_LENGTH (type) != byte_size)
12152 {
3e43a32a
MS
12153 complaint (&symfile_complaints,
12154 _("invalid pointer size %d"), byte_size);
8b2dbe47 12155 }
6e70227d 12156 else
9a619af0
MS
12157 {
12158 /* Should we also complain about unhandled address classes? */
12159 }
c906108c 12160 }
8b2dbe47
KB
12161
12162 TYPE_LENGTH (type) = byte_size;
f792889a 12163 return set_die_type (die, type, cu);
c906108c
SS
12164}
12165
12166/* Extract all information from a DW_TAG_ptr_to_member_type DIE and add to
12167 the user defined type vector. */
12168
f792889a 12169static struct type *
e7c27a73 12170read_tag_ptr_to_member_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c
SS
12171{
12172 struct type *type;
12173 struct type *to_type;
12174 struct type *domain;
12175
e7c27a73
DJ
12176 to_type = die_type (die, cu);
12177 domain = die_containing_type (die, cu);
0d5de010 12178
7e314c57
JK
12179 /* The calls above may have already set the type for this DIE. */
12180 type = get_die_type (die, cu);
12181 if (type)
12182 return type;
12183
0d5de010
DJ
12184 if (TYPE_CODE (check_typedef (to_type)) == TYPE_CODE_METHOD)
12185 type = lookup_methodptr_type (to_type);
12186 else
12187 type = lookup_memberptr_type (to_type, domain);
c906108c 12188
f792889a 12189 return set_die_type (die, type, cu);
c906108c
SS
12190}
12191
12192/* Extract all information from a DW_TAG_reference_type DIE and add to
12193 the user defined type vector. */
12194
f792889a 12195static struct type *
e7c27a73 12196read_tag_reference_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 12197{
e7c27a73 12198 struct comp_unit_head *cu_header = &cu->header;
7e314c57 12199 struct type *type, *target_type;
c906108c
SS
12200 struct attribute *attr;
12201
7e314c57
JK
12202 target_type = die_type (die, cu);
12203
12204 /* The die_type call above may have already set the type for this DIE. */
12205 type = get_die_type (die, cu);
12206 if (type)
12207 return type;
12208
12209 type = lookup_reference_type (target_type);
e142c38c 12210 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
c906108c
SS
12211 if (attr)
12212 {
12213 TYPE_LENGTH (type) = DW_UNSND (attr);
12214 }
12215 else
12216 {
107d2387 12217 TYPE_LENGTH (type) = cu_header->addr_size;
c906108c 12218 }
f792889a 12219 return set_die_type (die, type, cu);
c906108c
SS
12220}
12221
f792889a 12222static struct type *
e7c27a73 12223read_tag_const_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 12224{
f792889a 12225 struct type *base_type, *cv_type;
c906108c 12226
e7c27a73 12227 base_type = die_type (die, cu);
7e314c57
JK
12228
12229 /* The die_type call above may have already set the type for this DIE. */
12230 cv_type = get_die_type (die, cu);
12231 if (cv_type)
12232 return cv_type;
12233
2f608a3a
KW
12234 /* In case the const qualifier is applied to an array type, the element type
12235 is so qualified, not the array type (section 6.7.3 of C99). */
12236 if (TYPE_CODE (base_type) == TYPE_CODE_ARRAY)
12237 {
12238 struct type *el_type, *inner_array;
12239
12240 base_type = copy_type (base_type);
12241 inner_array = base_type;
12242
12243 while (TYPE_CODE (TYPE_TARGET_TYPE (inner_array)) == TYPE_CODE_ARRAY)
12244 {
12245 TYPE_TARGET_TYPE (inner_array) =
12246 copy_type (TYPE_TARGET_TYPE (inner_array));
12247 inner_array = TYPE_TARGET_TYPE (inner_array);
12248 }
12249
12250 el_type = TYPE_TARGET_TYPE (inner_array);
12251 TYPE_TARGET_TYPE (inner_array) =
12252 make_cv_type (1, TYPE_VOLATILE (el_type), el_type, NULL);
12253
12254 return set_die_type (die, base_type, cu);
12255 }
12256
f792889a
DJ
12257 cv_type = make_cv_type (1, TYPE_VOLATILE (base_type), base_type, 0);
12258 return set_die_type (die, cv_type, cu);
c906108c
SS
12259}
12260
f792889a 12261static struct type *
e7c27a73 12262read_tag_volatile_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 12263{
f792889a 12264 struct type *base_type, *cv_type;
c906108c 12265
e7c27a73 12266 base_type = die_type (die, cu);
7e314c57
JK
12267
12268 /* The die_type call above may have already set the type for this DIE. */
12269 cv_type = get_die_type (die, cu);
12270 if (cv_type)
12271 return cv_type;
12272
f792889a
DJ
12273 cv_type = make_cv_type (TYPE_CONST (base_type), 1, base_type, 0);
12274 return set_die_type (die, cv_type, cu);
c906108c
SS
12275}
12276
12277/* Extract all information from a DW_TAG_string_type DIE and add to
12278 the user defined type vector. It isn't really a user defined type,
12279 but it behaves like one, with other DIE's using an AT_user_def_type
12280 attribute to reference it. */
12281
f792889a 12282static struct type *
e7c27a73 12283read_tag_string_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 12284{
e7c27a73 12285 struct objfile *objfile = cu->objfile;
3b7538c0 12286 struct gdbarch *gdbarch = get_objfile_arch (objfile);
c906108c
SS
12287 struct type *type, *range_type, *index_type, *char_type;
12288 struct attribute *attr;
12289 unsigned int length;
12290
e142c38c 12291 attr = dwarf2_attr (die, DW_AT_string_length, cu);
c906108c
SS
12292 if (attr)
12293 {
12294 length = DW_UNSND (attr);
12295 }
12296 else
12297 {
0963b4bd 12298 /* Check for the DW_AT_byte_size attribute. */
e142c38c 12299 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
b21b22e0
PS
12300 if (attr)
12301 {
12302 length = DW_UNSND (attr);
12303 }
12304 else
12305 {
12306 length = 1;
12307 }
c906108c 12308 }
6ccb9162 12309
46bf5051 12310 index_type = objfile_type (objfile)->builtin_int;
c906108c 12311 range_type = create_range_type (NULL, index_type, 1, length);
3b7538c0
UW
12312 char_type = language_string_char_type (cu->language_defn, gdbarch);
12313 type = create_string_type (NULL, char_type, range_type);
6ccb9162 12314
f792889a 12315 return set_die_type (die, type, cu);
c906108c
SS
12316}
12317
12318/* Handle DIES due to C code like:
12319
12320 struct foo
c5aa993b
JM
12321 {
12322 int (*funcp)(int a, long l);
12323 int b;
12324 };
c906108c 12325
0963b4bd 12326 ('funcp' generates a DW_TAG_subroutine_type DIE). */
c906108c 12327
f792889a 12328static struct type *
e7c27a73 12329read_subroutine_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 12330{
bb5ed363 12331 struct objfile *objfile = cu->objfile;
0963b4bd
MS
12332 struct type *type; /* Type that this function returns. */
12333 struct type *ftype; /* Function that returns above type. */
c906108c
SS
12334 struct attribute *attr;
12335
e7c27a73 12336 type = die_type (die, cu);
7e314c57
JK
12337
12338 /* The die_type call above may have already set the type for this DIE. */
12339 ftype = get_die_type (die, cu);
12340 if (ftype)
12341 return ftype;
12342
0c8b41f1 12343 ftype = lookup_function_type (type);
c906108c 12344
5b8101ae 12345 /* All functions in C++, Pascal and Java have prototypes. */
e142c38c 12346 attr = dwarf2_attr (die, DW_AT_prototyped, cu);
c906108c 12347 if ((attr && (DW_UNSND (attr) != 0))
987504bb 12348 || cu->language == language_cplus
5b8101ae
PM
12349 || cu->language == language_java
12350 || cu->language == language_pascal)
876cecd0 12351 TYPE_PROTOTYPED (ftype) = 1;
a6c727b2
DJ
12352 else if (producer_is_realview (cu->producer))
12353 /* RealView does not emit DW_AT_prototyped. We can not
12354 distinguish prototyped and unprototyped functions; default to
12355 prototyped, since that is more common in modern code (and
12356 RealView warns about unprototyped functions). */
12357 TYPE_PROTOTYPED (ftype) = 1;
c906108c 12358
c055b101
CV
12359 /* Store the calling convention in the type if it's available in
12360 the subroutine die. Otherwise set the calling convention to
12361 the default value DW_CC_normal. */
12362 attr = dwarf2_attr (die, DW_AT_calling_convention, cu);
54fcddd0
UW
12363 if (attr)
12364 TYPE_CALLING_CONVENTION (ftype) = DW_UNSND (attr);
12365 else if (cu->producer && strstr (cu->producer, "IBM XL C for OpenCL"))
12366 TYPE_CALLING_CONVENTION (ftype) = DW_CC_GDB_IBM_OpenCL;
12367 else
12368 TYPE_CALLING_CONVENTION (ftype) = DW_CC_normal;
76c10ea2
GM
12369
12370 /* We need to add the subroutine type to the die immediately so
12371 we don't infinitely recurse when dealing with parameters
0963b4bd 12372 declared as the same subroutine type. */
76c10ea2 12373 set_die_type (die, ftype, cu);
6e70227d 12374
639d11d3 12375 if (die->child != NULL)
c906108c 12376 {
bb5ed363 12377 struct type *void_type = objfile_type (objfile)->builtin_void;
c906108c 12378 struct die_info *child_die;
8072405b 12379 int nparams, iparams;
c906108c
SS
12380
12381 /* Count the number of parameters.
12382 FIXME: GDB currently ignores vararg functions, but knows about
12383 vararg member functions. */
8072405b 12384 nparams = 0;
639d11d3 12385 child_die = die->child;
c906108c
SS
12386 while (child_die && child_die->tag)
12387 {
12388 if (child_die->tag == DW_TAG_formal_parameter)
12389 nparams++;
12390 else if (child_die->tag == DW_TAG_unspecified_parameters)
876cecd0 12391 TYPE_VARARGS (ftype) = 1;
c906108c
SS
12392 child_die = sibling_die (child_die);
12393 }
12394
12395 /* Allocate storage for parameters and fill them in. */
12396 TYPE_NFIELDS (ftype) = nparams;
12397 TYPE_FIELDS (ftype) = (struct field *)
ae5a43e0 12398 TYPE_ZALLOC (ftype, nparams * sizeof (struct field));
c906108c 12399
8072405b
JK
12400 /* TYPE_FIELD_TYPE must never be NULL. Pre-fill the array to ensure it
12401 even if we error out during the parameters reading below. */
12402 for (iparams = 0; iparams < nparams; iparams++)
12403 TYPE_FIELD_TYPE (ftype, iparams) = void_type;
12404
12405 iparams = 0;
639d11d3 12406 child_die = die->child;
c906108c
SS
12407 while (child_die && child_die->tag)
12408 {
12409 if (child_die->tag == DW_TAG_formal_parameter)
12410 {
3ce3b1ba
PA
12411 struct type *arg_type;
12412
12413 /* DWARF version 2 has no clean way to discern C++
12414 static and non-static member functions. G++ helps
12415 GDB by marking the first parameter for non-static
12416 member functions (which is the this pointer) as
12417 artificial. We pass this information to
12418 dwarf2_add_member_fn via TYPE_FIELD_ARTIFICIAL.
12419
12420 DWARF version 3 added DW_AT_object_pointer, which GCC
12421 4.5 does not yet generate. */
e142c38c 12422 attr = dwarf2_attr (child_die, DW_AT_artificial, cu);
c906108c
SS
12423 if (attr)
12424 TYPE_FIELD_ARTIFICIAL (ftype, iparams) = DW_UNSND (attr);
12425 else
418835cc
KS
12426 {
12427 TYPE_FIELD_ARTIFICIAL (ftype, iparams) = 0;
12428
12429 /* GCC/43521: In java, the formal parameter
12430 "this" is sometimes not marked with DW_AT_artificial. */
12431 if (cu->language == language_java)
12432 {
12433 const char *name = dwarf2_name (child_die, cu);
9a619af0 12434
418835cc
KS
12435 if (name && !strcmp (name, "this"))
12436 TYPE_FIELD_ARTIFICIAL (ftype, iparams) = 1;
12437 }
12438 }
3ce3b1ba
PA
12439 arg_type = die_type (child_die, cu);
12440
12441 /* RealView does not mark THIS as const, which the testsuite
12442 expects. GCC marks THIS as const in method definitions,
12443 but not in the class specifications (GCC PR 43053). */
12444 if (cu->language == language_cplus && !TYPE_CONST (arg_type)
12445 && TYPE_FIELD_ARTIFICIAL (ftype, iparams))
12446 {
12447 int is_this = 0;
12448 struct dwarf2_cu *arg_cu = cu;
12449 const char *name = dwarf2_name (child_die, cu);
12450
12451 attr = dwarf2_attr (die, DW_AT_object_pointer, cu);
12452 if (attr)
12453 {
12454 /* If the compiler emits this, use it. */
12455 if (follow_die_ref (die, attr, &arg_cu) == child_die)
12456 is_this = 1;
12457 }
12458 else if (name && strcmp (name, "this") == 0)
12459 /* Function definitions will have the argument names. */
12460 is_this = 1;
12461 else if (name == NULL && iparams == 0)
12462 /* Declarations may not have the names, so like
12463 elsewhere in GDB, assume an artificial first
12464 argument is "this". */
12465 is_this = 1;
12466
12467 if (is_this)
12468 arg_type = make_cv_type (1, TYPE_VOLATILE (arg_type),
12469 arg_type, 0);
12470 }
12471
12472 TYPE_FIELD_TYPE (ftype, iparams) = arg_type;
c906108c
SS
12473 iparams++;
12474 }
12475 child_die = sibling_die (child_die);
12476 }
12477 }
12478
76c10ea2 12479 return ftype;
c906108c
SS
12480}
12481
f792889a 12482static struct type *
e7c27a73 12483read_typedef (struct die_info *die, struct dwarf2_cu *cu)
c906108c 12484{
e7c27a73 12485 struct objfile *objfile = cu->objfile;
0114d602 12486 const char *name = NULL;
3c8e0968 12487 struct type *this_type, *target_type;
c906108c 12488
94af9270 12489 name = dwarf2_full_name (NULL, die, cu);
f792889a 12490 this_type = init_type (TYPE_CODE_TYPEDEF, 0,
0114d602
DJ
12491 TYPE_FLAG_TARGET_STUB, NULL, objfile);
12492 TYPE_NAME (this_type) = (char *) name;
f792889a 12493 set_die_type (die, this_type, cu);
3c8e0968
DE
12494 target_type = die_type (die, cu);
12495 if (target_type != this_type)
12496 TYPE_TARGET_TYPE (this_type) = target_type;
12497 else
12498 {
12499 /* Self-referential typedefs are, it seems, not allowed by the DWARF
12500 spec and cause infinite loops in GDB. */
12501 complaint (&symfile_complaints,
12502 _("Self-referential DW_TAG_typedef "
12503 "- DIE at 0x%x [in module %s]"),
b64f50a1 12504 die->offset.sect_off, objfile->name);
3c8e0968
DE
12505 TYPE_TARGET_TYPE (this_type) = NULL;
12506 }
f792889a 12507 return this_type;
c906108c
SS
12508}
12509
12510/* Find a representation of a given base type and install
12511 it in the TYPE field of the die. */
12512
f792889a 12513static struct type *
e7c27a73 12514read_base_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 12515{
e7c27a73 12516 struct objfile *objfile = cu->objfile;
c906108c
SS
12517 struct type *type;
12518 struct attribute *attr;
12519 int encoding = 0, size = 0;
39cbfefa 12520 char *name;
6ccb9162
UW
12521 enum type_code code = TYPE_CODE_INT;
12522 int type_flags = 0;
12523 struct type *target_type = NULL;
c906108c 12524
e142c38c 12525 attr = dwarf2_attr (die, DW_AT_encoding, cu);
c906108c
SS
12526 if (attr)
12527 {
12528 encoding = DW_UNSND (attr);
12529 }
e142c38c 12530 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
c906108c
SS
12531 if (attr)
12532 {
12533 size = DW_UNSND (attr);
12534 }
39cbfefa 12535 name = dwarf2_name (die, cu);
6ccb9162 12536 if (!name)
c906108c 12537 {
6ccb9162
UW
12538 complaint (&symfile_complaints,
12539 _("DW_AT_name missing from DW_TAG_base_type"));
c906108c 12540 }
6ccb9162
UW
12541
12542 switch (encoding)
c906108c 12543 {
6ccb9162
UW
12544 case DW_ATE_address:
12545 /* Turn DW_ATE_address into a void * pointer. */
12546 code = TYPE_CODE_PTR;
12547 type_flags |= TYPE_FLAG_UNSIGNED;
12548 target_type = init_type (TYPE_CODE_VOID, 1, 0, NULL, objfile);
12549 break;
12550 case DW_ATE_boolean:
12551 code = TYPE_CODE_BOOL;
12552 type_flags |= TYPE_FLAG_UNSIGNED;
12553 break;
12554 case DW_ATE_complex_float:
12555 code = TYPE_CODE_COMPLEX;
12556 target_type = init_type (TYPE_CODE_FLT, size / 2, 0, NULL, objfile);
12557 break;
12558 case DW_ATE_decimal_float:
12559 code = TYPE_CODE_DECFLOAT;
12560 break;
12561 case DW_ATE_float:
12562 code = TYPE_CODE_FLT;
12563 break;
12564 case DW_ATE_signed:
12565 break;
12566 case DW_ATE_unsigned:
12567 type_flags |= TYPE_FLAG_UNSIGNED;
3b2b8fea
TT
12568 if (cu->language == language_fortran
12569 && name
12570 && strncmp (name, "character(", sizeof ("character(") - 1) == 0)
12571 code = TYPE_CODE_CHAR;
6ccb9162
UW
12572 break;
12573 case DW_ATE_signed_char:
6e70227d 12574 if (cu->language == language_ada || cu->language == language_m2
3b2b8fea
TT
12575 || cu->language == language_pascal
12576 || cu->language == language_fortran)
6ccb9162
UW
12577 code = TYPE_CODE_CHAR;
12578 break;
12579 case DW_ATE_unsigned_char:
868a0084 12580 if (cu->language == language_ada || cu->language == language_m2
3b2b8fea
TT
12581 || cu->language == language_pascal
12582 || cu->language == language_fortran)
6ccb9162
UW
12583 code = TYPE_CODE_CHAR;
12584 type_flags |= TYPE_FLAG_UNSIGNED;
12585 break;
75079b2b
TT
12586 case DW_ATE_UTF:
12587 /* We just treat this as an integer and then recognize the
12588 type by name elsewhere. */
12589 break;
12590
6ccb9162
UW
12591 default:
12592 complaint (&symfile_complaints, _("unsupported DW_AT_encoding: '%s'"),
12593 dwarf_type_encoding_name (encoding));
12594 break;
c906108c 12595 }
6ccb9162 12596
0114d602
DJ
12597 type = init_type (code, size, type_flags, NULL, objfile);
12598 TYPE_NAME (type) = name;
6ccb9162
UW
12599 TYPE_TARGET_TYPE (type) = target_type;
12600
0114d602 12601 if (name && strcmp (name, "char") == 0)
876cecd0 12602 TYPE_NOSIGN (type) = 1;
0114d602 12603
f792889a 12604 return set_die_type (die, type, cu);
c906108c
SS
12605}
12606
a02abb62
JB
12607/* Read the given DW_AT_subrange DIE. */
12608
f792889a 12609static struct type *
a02abb62
JB
12610read_subrange_type (struct die_info *die, struct dwarf2_cu *cu)
12611{
12612 struct type *base_type;
12613 struct type *range_type;
12614 struct attribute *attr;
4fae6e18
JK
12615 LONGEST low, high;
12616 int low_default_is_valid;
39cbfefa 12617 char *name;
43bbcdc2 12618 LONGEST negative_mask;
e77813c8 12619
a02abb62 12620 base_type = die_type (die, cu);
953ac07e
JK
12621 /* Preserve BASE_TYPE's original type, just set its LENGTH. */
12622 check_typedef (base_type);
a02abb62 12623
7e314c57
JK
12624 /* The die_type call above may have already set the type for this DIE. */
12625 range_type = get_die_type (die, cu);
12626 if (range_type)
12627 return range_type;
12628
4fae6e18
JK
12629 /* Set LOW_DEFAULT_IS_VALID if current language and DWARF version allow
12630 omitting DW_AT_lower_bound. */
12631 switch (cu->language)
6e70227d 12632 {
4fae6e18
JK
12633 case language_c:
12634 case language_cplus:
12635 low = 0;
12636 low_default_is_valid = 1;
12637 break;
12638 case language_fortran:
12639 low = 1;
12640 low_default_is_valid = 1;
12641 break;
12642 case language_d:
12643 case language_java:
12644 case language_objc:
12645 low = 0;
12646 low_default_is_valid = (cu->header.version >= 4);
12647 break;
12648 case language_ada:
12649 case language_m2:
12650 case language_pascal:
a02abb62 12651 low = 1;
4fae6e18
JK
12652 low_default_is_valid = (cu->header.version >= 4);
12653 break;
12654 default:
12655 low = 0;
12656 low_default_is_valid = 0;
12657 break;
a02abb62
JB
12658 }
12659
dd5e6932
DJ
12660 /* FIXME: For variable sized arrays either of these could be
12661 a variable rather than a constant value. We'll allow it,
12662 but we don't know how to handle it. */
e142c38c 12663 attr = dwarf2_attr (die, DW_AT_lower_bound, cu);
a02abb62 12664 if (attr)
4fae6e18
JK
12665 low = dwarf2_get_attr_constant_value (attr, low);
12666 else if (!low_default_is_valid)
12667 complaint (&symfile_complaints, _("Missing DW_AT_lower_bound "
12668 "- DIE at 0x%x [in module %s]"),
12669 die->offset.sect_off, cu->objfile->name);
a02abb62 12670
e142c38c 12671 attr = dwarf2_attr (die, DW_AT_upper_bound, cu);
a02abb62 12672 if (attr)
6e70227d 12673 {
d48323d8 12674 if (attr_form_is_block (attr) || is_ref_attr (attr))
a02abb62
JB
12675 {
12676 /* GCC encodes arrays with unspecified or dynamic length
e77813c8 12677 with a DW_FORM_block1 attribute or a reference attribute.
a02abb62
JB
12678 FIXME: GDB does not yet know how to handle dynamic
12679 arrays properly, treat them as arrays with unspecified
12680 length for now.
12681
12682 FIXME: jimb/2003-09-22: GDB does not really know
12683 how to handle arrays of unspecified length
12684 either; we just represent them as zero-length
12685 arrays. Choose an appropriate upper bound given
12686 the lower bound we've computed above. */
12687 high = low - 1;
12688 }
12689 else
12690 high = dwarf2_get_attr_constant_value (attr, 1);
12691 }
e77813c8
PM
12692 else
12693 {
12694 attr = dwarf2_attr (die, DW_AT_count, cu);
12695 if (attr)
12696 {
12697 int count = dwarf2_get_attr_constant_value (attr, 1);
12698 high = low + count - 1;
12699 }
c2ff108b
JK
12700 else
12701 {
12702 /* Unspecified array length. */
12703 high = low - 1;
12704 }
e77813c8
PM
12705 }
12706
12707 /* Dwarf-2 specifications explicitly allows to create subrange types
12708 without specifying a base type.
12709 In that case, the base type must be set to the type of
12710 the lower bound, upper bound or count, in that order, if any of these
12711 three attributes references an object that has a type.
12712 If no base type is found, the Dwarf-2 specifications say that
12713 a signed integer type of size equal to the size of an address should
12714 be used.
12715 For the following C code: `extern char gdb_int [];'
12716 GCC produces an empty range DIE.
12717 FIXME: muller/2010-05-28: Possible references to object for low bound,
0963b4bd 12718 high bound or count are not yet handled by this code. */
e77813c8
PM
12719 if (TYPE_CODE (base_type) == TYPE_CODE_VOID)
12720 {
12721 struct objfile *objfile = cu->objfile;
12722 struct gdbarch *gdbarch = get_objfile_arch (objfile);
12723 int addr_size = gdbarch_addr_bit (gdbarch) /8;
12724 struct type *int_type = objfile_type (objfile)->builtin_int;
12725
12726 /* Test "int", "long int", and "long long int" objfile types,
12727 and select the first one having a size above or equal to the
12728 architecture address size. */
12729 if (int_type && TYPE_LENGTH (int_type) >= addr_size)
12730 base_type = int_type;
12731 else
12732 {
12733 int_type = objfile_type (objfile)->builtin_long;
12734 if (int_type && TYPE_LENGTH (int_type) >= addr_size)
12735 base_type = int_type;
12736 else
12737 {
12738 int_type = objfile_type (objfile)->builtin_long_long;
12739 if (int_type && TYPE_LENGTH (int_type) >= addr_size)
12740 base_type = int_type;
12741 }
12742 }
12743 }
a02abb62 12744
6e70227d 12745 negative_mask =
43bbcdc2
PH
12746 (LONGEST) -1 << (TYPE_LENGTH (base_type) * TARGET_CHAR_BIT - 1);
12747 if (!TYPE_UNSIGNED (base_type) && (low & negative_mask))
12748 low |= negative_mask;
12749 if (!TYPE_UNSIGNED (base_type) && (high & negative_mask))
12750 high |= negative_mask;
12751
a02abb62
JB
12752 range_type = create_range_type (NULL, base_type, low, high);
12753
bbb0eef6
JK
12754 /* Mark arrays with dynamic length at least as an array of unspecified
12755 length. GDB could check the boundary but before it gets implemented at
12756 least allow accessing the array elements. */
d48323d8 12757 if (attr && attr_form_is_block (attr))
bbb0eef6
JK
12758 TYPE_HIGH_BOUND_UNDEFINED (range_type) = 1;
12759
c2ff108b
JK
12760 /* Ada expects an empty array on no boundary attributes. */
12761 if (attr == NULL && cu->language != language_ada)
12762 TYPE_HIGH_BOUND_UNDEFINED (range_type) = 1;
12763
39cbfefa
DJ
12764 name = dwarf2_name (die, cu);
12765 if (name)
12766 TYPE_NAME (range_type) = name;
6e70227d 12767
e142c38c 12768 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
a02abb62
JB
12769 if (attr)
12770 TYPE_LENGTH (range_type) = DW_UNSND (attr);
12771
7e314c57
JK
12772 set_die_type (die, range_type, cu);
12773
12774 /* set_die_type should be already done. */
b4ba55a1
JB
12775 set_descriptive_type (range_type, die, cu);
12776
7e314c57 12777 return range_type;
a02abb62 12778}
6e70227d 12779
f792889a 12780static struct type *
81a17f79
JB
12781read_unspecified_type (struct die_info *die, struct dwarf2_cu *cu)
12782{
12783 struct type *type;
81a17f79 12784
81a17f79
JB
12785 /* For now, we only support the C meaning of an unspecified type: void. */
12786
0114d602
DJ
12787 type = init_type (TYPE_CODE_VOID, 0, 0, NULL, cu->objfile);
12788 TYPE_NAME (type) = dwarf2_name (die, cu);
81a17f79 12789
f792889a 12790 return set_die_type (die, type, cu);
81a17f79 12791}
a02abb62 12792
639d11d3
DC
12793/* Read a single die and all its descendents. Set the die's sibling
12794 field to NULL; set other fields in the die correctly, and set all
12795 of the descendents' fields correctly. Set *NEW_INFO_PTR to the
12796 location of the info_ptr after reading all of those dies. PARENT
12797 is the parent of the die in question. */
12798
12799static struct die_info *
dee91e82
DE
12800read_die_and_children (const struct die_reader_specs *reader,
12801 gdb_byte *info_ptr,
12802 gdb_byte **new_info_ptr,
12803 struct die_info *parent)
639d11d3
DC
12804{
12805 struct die_info *die;
fe1b8b76 12806 gdb_byte *cur_ptr;
639d11d3
DC
12807 int has_children;
12808
93311388 12809 cur_ptr = read_full_die (reader, &die, info_ptr, &has_children);
1d325ec1
DJ
12810 if (die == NULL)
12811 {
12812 *new_info_ptr = cur_ptr;
12813 return NULL;
12814 }
93311388 12815 store_in_ref_table (die, reader->cu);
639d11d3
DC
12816
12817 if (has_children)
348e048f 12818 die->child = read_die_and_siblings (reader, cur_ptr, new_info_ptr, die);
639d11d3
DC
12819 else
12820 {
12821 die->child = NULL;
12822 *new_info_ptr = cur_ptr;
12823 }
12824
12825 die->sibling = NULL;
12826 die->parent = parent;
12827 return die;
12828}
12829
12830/* Read a die, all of its descendents, and all of its siblings; set
12831 all of the fields of all of the dies correctly. Arguments are as
12832 in read_die_and_children. */
12833
12834static struct die_info *
93311388
DE
12835read_die_and_siblings (const struct die_reader_specs *reader,
12836 gdb_byte *info_ptr,
fe1b8b76 12837 gdb_byte **new_info_ptr,
639d11d3
DC
12838 struct die_info *parent)
12839{
12840 struct die_info *first_die, *last_sibling;
fe1b8b76 12841 gdb_byte *cur_ptr;
639d11d3 12842
c906108c 12843 cur_ptr = info_ptr;
639d11d3
DC
12844 first_die = last_sibling = NULL;
12845
12846 while (1)
c906108c 12847 {
639d11d3 12848 struct die_info *die
dee91e82 12849 = read_die_and_children (reader, cur_ptr, &cur_ptr, parent);
639d11d3 12850
1d325ec1 12851 if (die == NULL)
c906108c 12852 {
639d11d3
DC
12853 *new_info_ptr = cur_ptr;
12854 return first_die;
c906108c 12855 }
1d325ec1
DJ
12856
12857 if (!first_die)
12858 first_die = die;
c906108c 12859 else
1d325ec1
DJ
12860 last_sibling->sibling = die;
12861
12862 last_sibling = die;
c906108c 12863 }
c906108c
SS
12864}
12865
3019eac3
DE
12866/* Read a die and all its attributes, leave space for NUM_EXTRA_ATTRS
12867 attributes.
12868 The caller is responsible for filling in the extra attributes
12869 and updating (*DIEP)->num_attrs.
12870 Set DIEP to point to a newly allocated die with its information,
12871 except for its child, sibling, and parent fields.
12872 Set HAS_CHILDREN to tell whether the die has children or not. */
93311388
DE
12873
12874static gdb_byte *
3019eac3
DE
12875read_full_die_1 (const struct die_reader_specs *reader,
12876 struct die_info **diep, gdb_byte *info_ptr,
12877 int *has_children, int num_extra_attrs)
93311388 12878{
b64f50a1
JK
12879 unsigned int abbrev_number, bytes_read, i;
12880 sect_offset offset;
93311388
DE
12881 struct abbrev_info *abbrev;
12882 struct die_info *die;
12883 struct dwarf2_cu *cu = reader->cu;
12884 bfd *abfd = reader->abfd;
12885
b64f50a1 12886 offset.sect_off = info_ptr - reader->buffer;
93311388
DE
12887 abbrev_number = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
12888 info_ptr += bytes_read;
12889 if (!abbrev_number)
12890 {
12891 *diep = NULL;
12892 *has_children = 0;
12893 return info_ptr;
12894 }
12895
433df2d4 12896 abbrev = abbrev_table_lookup_abbrev (cu->abbrev_table, abbrev_number);
93311388 12897 if (!abbrev)
348e048f
DE
12898 error (_("Dwarf Error: could not find abbrev number %d [in module %s]"),
12899 abbrev_number,
12900 bfd_get_filename (abfd));
12901
3019eac3 12902 die = dwarf_alloc_die (cu, abbrev->num_attrs + num_extra_attrs);
93311388
DE
12903 die->offset = offset;
12904 die->tag = abbrev->tag;
12905 die->abbrev = abbrev_number;
12906
3019eac3
DE
12907 /* Make the result usable.
12908 The caller needs to update num_attrs after adding the extra
12909 attributes. */
93311388
DE
12910 die->num_attrs = abbrev->num_attrs;
12911
12912 for (i = 0; i < abbrev->num_attrs; ++i)
dee91e82
DE
12913 info_ptr = read_attribute (reader, &die->attrs[i], &abbrev->attrs[i],
12914 info_ptr);
93311388
DE
12915
12916 *diep = die;
12917 *has_children = abbrev->has_children;
12918 return info_ptr;
12919}
12920
3019eac3
DE
12921/* Read a die and all its attributes.
12922 Set DIEP to point to a newly allocated die with its information,
12923 except for its child, sibling, and parent fields.
12924 Set HAS_CHILDREN to tell whether the die has children or not. */
12925
12926static gdb_byte *
12927read_full_die (const struct die_reader_specs *reader,
12928 struct die_info **diep, gdb_byte *info_ptr,
12929 int *has_children)
12930{
12931 return read_full_die_1 (reader, diep, info_ptr, has_children, 0);
12932}
433df2d4
DE
12933\f
12934/* Abbreviation tables.
3019eac3 12935
433df2d4 12936 In DWARF version 2, the description of the debugging information is
c906108c
SS
12937 stored in a separate .debug_abbrev section. Before we read any
12938 dies from a section we read in all abbreviations and install them
433df2d4
DE
12939 in a hash table. */
12940
12941/* Allocate space for a struct abbrev_info object in ABBREV_TABLE. */
12942
12943static struct abbrev_info *
12944abbrev_table_alloc_abbrev (struct abbrev_table *abbrev_table)
12945{
12946 struct abbrev_info *abbrev;
12947
12948 abbrev = (struct abbrev_info *)
12949 obstack_alloc (&abbrev_table->abbrev_obstack, sizeof (struct abbrev_info));
12950 memset (abbrev, 0, sizeof (struct abbrev_info));
12951 return abbrev;
12952}
12953
12954/* Add an abbreviation to the table. */
c906108c
SS
12955
12956static void
433df2d4
DE
12957abbrev_table_add_abbrev (struct abbrev_table *abbrev_table,
12958 unsigned int abbrev_number,
12959 struct abbrev_info *abbrev)
12960{
12961 unsigned int hash_number;
12962
12963 hash_number = abbrev_number % ABBREV_HASH_SIZE;
12964 abbrev->next = abbrev_table->abbrevs[hash_number];
12965 abbrev_table->abbrevs[hash_number] = abbrev;
12966}
dee91e82 12967
433df2d4
DE
12968/* Look up an abbrev in the table.
12969 Returns NULL if the abbrev is not found. */
12970
12971static struct abbrev_info *
12972abbrev_table_lookup_abbrev (const struct abbrev_table *abbrev_table,
12973 unsigned int abbrev_number)
c906108c 12974{
433df2d4
DE
12975 unsigned int hash_number;
12976 struct abbrev_info *abbrev;
12977
12978 hash_number = abbrev_number % ABBREV_HASH_SIZE;
12979 abbrev = abbrev_table->abbrevs[hash_number];
12980
12981 while (abbrev)
12982 {
12983 if (abbrev->number == abbrev_number)
12984 return abbrev;
12985 abbrev = abbrev->next;
12986 }
12987 return NULL;
12988}
12989
12990/* Read in an abbrev table. */
12991
12992static struct abbrev_table *
12993abbrev_table_read_table (struct dwarf2_section_info *section,
12994 sect_offset offset)
12995{
12996 struct objfile *objfile = dwarf2_per_objfile->objfile;
12997 bfd *abfd = section->asection->owner;
12998 struct abbrev_table *abbrev_table;
fe1b8b76 12999 gdb_byte *abbrev_ptr;
c906108c
SS
13000 struct abbrev_info *cur_abbrev;
13001 unsigned int abbrev_number, bytes_read, abbrev_name;
433df2d4 13002 unsigned int abbrev_form;
f3dd6933
DJ
13003 struct attr_abbrev *cur_attrs;
13004 unsigned int allocated_attrs;
c906108c 13005
433df2d4 13006 abbrev_table = XMALLOC (struct abbrev_table);
f4dc4d17 13007 abbrev_table->offset = offset;
433df2d4
DE
13008 obstack_init (&abbrev_table->abbrev_obstack);
13009 abbrev_table->abbrevs = obstack_alloc (&abbrev_table->abbrev_obstack,
13010 (ABBREV_HASH_SIZE
13011 * sizeof (struct abbrev_info *)));
13012 memset (abbrev_table->abbrevs, 0,
13013 ABBREV_HASH_SIZE * sizeof (struct abbrev_info *));
c906108c 13014
433df2d4
DE
13015 dwarf2_read_section (objfile, section);
13016 abbrev_ptr = section->buffer + offset.sect_off;
c906108c
SS
13017 abbrev_number = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
13018 abbrev_ptr += bytes_read;
13019
f3dd6933
DJ
13020 allocated_attrs = ATTR_ALLOC_CHUNK;
13021 cur_attrs = xmalloc (allocated_attrs * sizeof (struct attr_abbrev));
6e70227d 13022
0963b4bd 13023 /* Loop until we reach an abbrev number of 0. */
c906108c
SS
13024 while (abbrev_number)
13025 {
433df2d4 13026 cur_abbrev = abbrev_table_alloc_abbrev (abbrev_table);
c906108c
SS
13027
13028 /* read in abbrev header */
13029 cur_abbrev->number = abbrev_number;
13030 cur_abbrev->tag = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
13031 abbrev_ptr += bytes_read;
13032 cur_abbrev->has_children = read_1_byte (abfd, abbrev_ptr);
13033 abbrev_ptr += 1;
13034
13035 /* now read in declarations */
13036 abbrev_name = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
13037 abbrev_ptr += bytes_read;
13038 abbrev_form = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
13039 abbrev_ptr += bytes_read;
13040 while (abbrev_name)
13041 {
f3dd6933 13042 if (cur_abbrev->num_attrs == allocated_attrs)
c906108c 13043 {
f3dd6933
DJ
13044 allocated_attrs += ATTR_ALLOC_CHUNK;
13045 cur_attrs
13046 = xrealloc (cur_attrs, (allocated_attrs
13047 * sizeof (struct attr_abbrev)));
c906108c 13048 }
ae038cb0 13049
f3dd6933
DJ
13050 cur_attrs[cur_abbrev->num_attrs].name = abbrev_name;
13051 cur_attrs[cur_abbrev->num_attrs++].form = abbrev_form;
c906108c
SS
13052 abbrev_name = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
13053 abbrev_ptr += bytes_read;
13054 abbrev_form = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
13055 abbrev_ptr += bytes_read;
13056 }
13057
433df2d4 13058 cur_abbrev->attrs = obstack_alloc (&abbrev_table->abbrev_obstack,
f3dd6933
DJ
13059 (cur_abbrev->num_attrs
13060 * sizeof (struct attr_abbrev)));
13061 memcpy (cur_abbrev->attrs, cur_attrs,
13062 cur_abbrev->num_attrs * sizeof (struct attr_abbrev));
13063
433df2d4 13064 abbrev_table_add_abbrev (abbrev_table, abbrev_number, cur_abbrev);
c906108c
SS
13065
13066 /* Get next abbreviation.
13067 Under Irix6 the abbreviations for a compilation unit are not
c5aa993b
JM
13068 always properly terminated with an abbrev number of 0.
13069 Exit loop if we encounter an abbreviation which we have
13070 already read (which means we are about to read the abbreviations
13071 for the next compile unit) or if the end of the abbreviation
13072 table is reached. */
433df2d4 13073 if ((unsigned int) (abbrev_ptr - section->buffer) >= section->size)
c906108c
SS
13074 break;
13075 abbrev_number = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
13076 abbrev_ptr += bytes_read;
433df2d4 13077 if (abbrev_table_lookup_abbrev (abbrev_table, abbrev_number) != NULL)
c906108c
SS
13078 break;
13079 }
f3dd6933
DJ
13080
13081 xfree (cur_attrs);
433df2d4 13082 return abbrev_table;
c906108c
SS
13083}
13084
433df2d4 13085/* Free the resources held by ABBREV_TABLE. */
c906108c 13086
c906108c 13087static void
433df2d4 13088abbrev_table_free (struct abbrev_table *abbrev_table)
c906108c 13089{
433df2d4
DE
13090 obstack_free (&abbrev_table->abbrev_obstack, NULL);
13091 xfree (abbrev_table);
c906108c
SS
13092}
13093
f4dc4d17
DE
13094/* Same as abbrev_table_free but as a cleanup.
13095 We pass in a pointer to the pointer to the table so that we can
13096 set the pointer to NULL when we're done. It also simplifies
13097 build_type_unit_groups. */
13098
13099static void
13100abbrev_table_free_cleanup (void *table_ptr)
13101{
13102 struct abbrev_table **abbrev_table_ptr = table_ptr;
13103
13104 if (*abbrev_table_ptr != NULL)
13105 abbrev_table_free (*abbrev_table_ptr);
13106 *abbrev_table_ptr = NULL;
13107}
13108
433df2d4
DE
13109/* Read the abbrev table for CU from ABBREV_SECTION. */
13110
13111static void
13112dwarf2_read_abbrevs (struct dwarf2_cu *cu,
13113 struct dwarf2_section_info *abbrev_section)
c906108c 13114{
433df2d4
DE
13115 cu->abbrev_table =
13116 abbrev_table_read_table (abbrev_section, cu->header.abbrev_offset);
13117}
c906108c 13118
433df2d4 13119/* Release the memory used by the abbrev table for a compilation unit. */
c906108c 13120
433df2d4
DE
13121static void
13122dwarf2_free_abbrev_table (void *ptr_to_cu)
13123{
13124 struct dwarf2_cu *cu = ptr_to_cu;
c906108c 13125
433df2d4
DE
13126 abbrev_table_free (cu->abbrev_table);
13127 /* Set this to NULL so that we SEGV if we try to read it later,
13128 and also because free_comp_unit verifies this is NULL. */
13129 cu->abbrev_table = NULL;
13130}
13131\f
72bf9492
DJ
13132/* Returns nonzero if TAG represents a type that we might generate a partial
13133 symbol for. */
13134
13135static int
13136is_type_tag_for_partial (int tag)
13137{
13138 switch (tag)
13139 {
13140#if 0
13141 /* Some types that would be reasonable to generate partial symbols for,
13142 that we don't at present. */
13143 case DW_TAG_array_type:
13144 case DW_TAG_file_type:
13145 case DW_TAG_ptr_to_member_type:
13146 case DW_TAG_set_type:
13147 case DW_TAG_string_type:
13148 case DW_TAG_subroutine_type:
13149#endif
13150 case DW_TAG_base_type:
13151 case DW_TAG_class_type:
680b30c7 13152 case DW_TAG_interface_type:
72bf9492
DJ
13153 case DW_TAG_enumeration_type:
13154 case DW_TAG_structure_type:
13155 case DW_TAG_subrange_type:
13156 case DW_TAG_typedef:
13157 case DW_TAG_union_type:
13158 return 1;
13159 default:
13160 return 0;
13161 }
13162}
13163
13164/* Load all DIEs that are interesting for partial symbols into memory. */
13165
13166static struct partial_die_info *
dee91e82
DE
13167load_partial_dies (const struct die_reader_specs *reader,
13168 gdb_byte *info_ptr, int building_psymtab)
72bf9492 13169{
dee91e82 13170 struct dwarf2_cu *cu = reader->cu;
bb5ed363 13171 struct objfile *objfile = cu->objfile;
72bf9492
DJ
13172 struct partial_die_info *part_die;
13173 struct partial_die_info *parent_die, *last_die, *first_die = NULL;
13174 struct abbrev_info *abbrev;
13175 unsigned int bytes_read;
5afb4e99 13176 unsigned int load_all = 0;
72bf9492
DJ
13177 int nesting_level = 1;
13178
13179 parent_die = NULL;
13180 last_die = NULL;
13181
7adf1e79
DE
13182 gdb_assert (cu->per_cu != NULL);
13183 if (cu->per_cu->load_all_dies)
5afb4e99
DJ
13184 load_all = 1;
13185
72bf9492
DJ
13186 cu->partial_dies
13187 = htab_create_alloc_ex (cu->header.length / 12,
13188 partial_die_hash,
13189 partial_die_eq,
13190 NULL,
13191 &cu->comp_unit_obstack,
13192 hashtab_obstack_allocate,
13193 dummy_obstack_deallocate);
13194
13195 part_die = obstack_alloc (&cu->comp_unit_obstack,
13196 sizeof (struct partial_die_info));
13197
13198 while (1)
13199 {
13200 abbrev = peek_die_abbrev (info_ptr, &bytes_read, cu);
13201
13202 /* A NULL abbrev means the end of a series of children. */
13203 if (abbrev == NULL)
13204 {
13205 if (--nesting_level == 0)
13206 {
13207 /* PART_DIE was probably the last thing allocated on the
13208 comp_unit_obstack, so we could call obstack_free
13209 here. We don't do that because the waste is small,
13210 and will be cleaned up when we're done with this
13211 compilation unit. This way, we're also more robust
13212 against other users of the comp_unit_obstack. */
13213 return first_die;
13214 }
13215 info_ptr += bytes_read;
13216 last_die = parent_die;
13217 parent_die = parent_die->die_parent;
13218 continue;
13219 }
13220
98bfdba5
PA
13221 /* Check for template arguments. We never save these; if
13222 they're seen, we just mark the parent, and go on our way. */
13223 if (parent_die != NULL
13224 && cu->language == language_cplus
13225 && (abbrev->tag == DW_TAG_template_type_param
13226 || abbrev->tag == DW_TAG_template_value_param))
13227 {
13228 parent_die->has_template_arguments = 1;
13229
13230 if (!load_all)
13231 {
13232 /* We don't need a partial DIE for the template argument. */
dee91e82 13233 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
98bfdba5
PA
13234 continue;
13235 }
13236 }
13237
0d99eb77 13238 /* We only recurse into c++ subprograms looking for template arguments.
98bfdba5
PA
13239 Skip their other children. */
13240 if (!load_all
13241 && cu->language == language_cplus
13242 && parent_die != NULL
13243 && parent_die->tag == DW_TAG_subprogram)
13244 {
dee91e82 13245 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
98bfdba5
PA
13246 continue;
13247 }
13248
5afb4e99
DJ
13249 /* Check whether this DIE is interesting enough to save. Normally
13250 we would not be interested in members here, but there may be
13251 later variables referencing them via DW_AT_specification (for
13252 static members). */
13253 if (!load_all
13254 && !is_type_tag_for_partial (abbrev->tag)
72929c62 13255 && abbrev->tag != DW_TAG_constant
72bf9492
DJ
13256 && abbrev->tag != DW_TAG_enumerator
13257 && abbrev->tag != DW_TAG_subprogram
bc30ff58 13258 && abbrev->tag != DW_TAG_lexical_block
72bf9492 13259 && abbrev->tag != DW_TAG_variable
5afb4e99 13260 && abbrev->tag != DW_TAG_namespace
f55ee35c 13261 && abbrev->tag != DW_TAG_module
95554aad
TT
13262 && abbrev->tag != DW_TAG_member
13263 && abbrev->tag != DW_TAG_imported_unit)
72bf9492
DJ
13264 {
13265 /* Otherwise we skip to the next sibling, if any. */
dee91e82 13266 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
72bf9492
DJ
13267 continue;
13268 }
13269
dee91e82
DE
13270 info_ptr = read_partial_die (reader, part_die, abbrev, bytes_read,
13271 info_ptr);
72bf9492
DJ
13272
13273 /* This two-pass algorithm for processing partial symbols has a
13274 high cost in cache pressure. Thus, handle some simple cases
13275 here which cover the majority of C partial symbols. DIEs
13276 which neither have specification tags in them, nor could have
13277 specification tags elsewhere pointing at them, can simply be
13278 processed and discarded.
13279
13280 This segment is also optional; scan_partial_symbols and
13281 add_partial_symbol will handle these DIEs if we chain
13282 them in normally. When compilers which do not emit large
13283 quantities of duplicate debug information are more common,
13284 this code can probably be removed. */
13285
13286 /* Any complete simple types at the top level (pretty much all
13287 of them, for a language without namespaces), can be processed
13288 directly. */
13289 if (parent_die == NULL
13290 && part_die->has_specification == 0
13291 && part_die->is_declaration == 0
d8228535 13292 && ((part_die->tag == DW_TAG_typedef && !part_die->has_children)
72bf9492
DJ
13293 || part_die->tag == DW_TAG_base_type
13294 || part_die->tag == DW_TAG_subrange_type))
13295 {
13296 if (building_psymtab && part_die->name != NULL)
04a679b8 13297 add_psymbol_to_list (part_die->name, strlen (part_die->name), 0,
72bf9492 13298 VAR_DOMAIN, LOC_TYPEDEF,
bb5ed363
DE
13299 &objfile->static_psymbols,
13300 0, (CORE_ADDR) 0, cu->language, objfile);
dee91e82 13301 info_ptr = locate_pdi_sibling (reader, part_die, info_ptr);
72bf9492
DJ
13302 continue;
13303 }
13304
d8228535
JK
13305 /* The exception for DW_TAG_typedef with has_children above is
13306 a workaround of GCC PR debug/47510. In the case of this complaint
13307 type_name_no_tag_or_error will error on such types later.
13308
13309 GDB skipped children of DW_TAG_typedef by the shortcut above and then
13310 it could not find the child DIEs referenced later, this is checked
13311 above. In correct DWARF DW_TAG_typedef should have no children. */
13312
13313 if (part_die->tag == DW_TAG_typedef && part_die->has_children)
13314 complaint (&symfile_complaints,
13315 _("DW_TAG_typedef has childen - GCC PR debug/47510 bug "
13316 "- DIE at 0x%x [in module %s]"),
b64f50a1 13317 part_die->offset.sect_off, objfile->name);
d8228535 13318
72bf9492
DJ
13319 /* If we're at the second level, and we're an enumerator, and
13320 our parent has no specification (meaning possibly lives in a
13321 namespace elsewhere), then we can add the partial symbol now
13322 instead of queueing it. */
13323 if (part_die->tag == DW_TAG_enumerator
13324 && parent_die != NULL
13325 && parent_die->die_parent == NULL
13326 && parent_die->tag == DW_TAG_enumeration_type
13327 && parent_die->has_specification == 0)
13328 {
13329 if (part_die->name == NULL)
3e43a32a
MS
13330 complaint (&symfile_complaints,
13331 _("malformed enumerator DIE ignored"));
72bf9492 13332 else if (building_psymtab)
04a679b8 13333 add_psymbol_to_list (part_die->name, strlen (part_die->name), 0,
72bf9492 13334 VAR_DOMAIN, LOC_CONST,
987504bb
JJ
13335 (cu->language == language_cplus
13336 || cu->language == language_java)
bb5ed363
DE
13337 ? &objfile->global_psymbols
13338 : &objfile->static_psymbols,
13339 0, (CORE_ADDR) 0, cu->language, objfile);
72bf9492 13340
dee91e82 13341 info_ptr = locate_pdi_sibling (reader, part_die, info_ptr);
72bf9492
DJ
13342 continue;
13343 }
13344
13345 /* We'll save this DIE so link it in. */
13346 part_die->die_parent = parent_die;
13347 part_die->die_sibling = NULL;
13348 part_die->die_child = NULL;
13349
13350 if (last_die && last_die == parent_die)
13351 last_die->die_child = part_die;
13352 else if (last_die)
13353 last_die->die_sibling = part_die;
13354
13355 last_die = part_die;
13356
13357 if (first_die == NULL)
13358 first_die = part_die;
13359
13360 /* Maybe add the DIE to the hash table. Not all DIEs that we
13361 find interesting need to be in the hash table, because we
13362 also have the parent/sibling/child chains; only those that we
13363 might refer to by offset later during partial symbol reading.
13364
13365 For now this means things that might have be the target of a
13366 DW_AT_specification, DW_AT_abstract_origin, or
13367 DW_AT_extension. DW_AT_extension will refer only to
13368 namespaces; DW_AT_abstract_origin refers to functions (and
13369 many things under the function DIE, but we do not recurse
13370 into function DIEs during partial symbol reading) and
13371 possibly variables as well; DW_AT_specification refers to
13372 declarations. Declarations ought to have the DW_AT_declaration
13373 flag. It happens that GCC forgets to put it in sometimes, but
13374 only for functions, not for types.
13375
13376 Adding more things than necessary to the hash table is harmless
13377 except for the performance cost. Adding too few will result in
5afb4e99
DJ
13378 wasted time in find_partial_die, when we reread the compilation
13379 unit with load_all_dies set. */
72bf9492 13380
5afb4e99 13381 if (load_all
72929c62 13382 || abbrev->tag == DW_TAG_constant
5afb4e99 13383 || abbrev->tag == DW_TAG_subprogram
72bf9492
DJ
13384 || abbrev->tag == DW_TAG_variable
13385 || abbrev->tag == DW_TAG_namespace
13386 || part_die->is_declaration)
13387 {
13388 void **slot;
13389
13390 slot = htab_find_slot_with_hash (cu->partial_dies, part_die,
b64f50a1 13391 part_die->offset.sect_off, INSERT);
72bf9492
DJ
13392 *slot = part_die;
13393 }
13394
13395 part_die = obstack_alloc (&cu->comp_unit_obstack,
13396 sizeof (struct partial_die_info));
13397
13398 /* For some DIEs we want to follow their children (if any). For C
bc30ff58 13399 we have no reason to follow the children of structures; for other
98bfdba5
PA
13400 languages we have to, so that we can get at method physnames
13401 to infer fully qualified class names, for DW_AT_specification,
13402 and for C++ template arguments. For C++, we also look one level
13403 inside functions to find template arguments (if the name of the
13404 function does not already contain the template arguments).
bc30ff58
JB
13405
13406 For Ada, we need to scan the children of subprograms and lexical
13407 blocks as well because Ada allows the definition of nested
13408 entities that could be interesting for the debugger, such as
13409 nested subprograms for instance. */
72bf9492 13410 if (last_die->has_children
5afb4e99
DJ
13411 && (load_all
13412 || last_die->tag == DW_TAG_namespace
f55ee35c 13413 || last_die->tag == DW_TAG_module
72bf9492 13414 || last_die->tag == DW_TAG_enumeration_type
98bfdba5
PA
13415 || (cu->language == language_cplus
13416 && last_die->tag == DW_TAG_subprogram
13417 && (last_die->name == NULL
13418 || strchr (last_die->name, '<') == NULL))
72bf9492
DJ
13419 || (cu->language != language_c
13420 && (last_die->tag == DW_TAG_class_type
680b30c7 13421 || last_die->tag == DW_TAG_interface_type
72bf9492 13422 || last_die->tag == DW_TAG_structure_type
bc30ff58
JB
13423 || last_die->tag == DW_TAG_union_type))
13424 || (cu->language == language_ada
13425 && (last_die->tag == DW_TAG_subprogram
13426 || last_die->tag == DW_TAG_lexical_block))))
72bf9492
DJ
13427 {
13428 nesting_level++;
13429 parent_die = last_die;
13430 continue;
13431 }
13432
13433 /* Otherwise we skip to the next sibling, if any. */
dee91e82 13434 info_ptr = locate_pdi_sibling (reader, last_die, info_ptr);
72bf9492
DJ
13435
13436 /* Back to the top, do it again. */
13437 }
13438}
13439
c906108c
SS
13440/* Read a minimal amount of information into the minimal die structure. */
13441
fe1b8b76 13442static gdb_byte *
dee91e82
DE
13443read_partial_die (const struct die_reader_specs *reader,
13444 struct partial_die_info *part_die,
13445 struct abbrev_info *abbrev, unsigned int abbrev_len,
13446 gdb_byte *info_ptr)
c906108c 13447{
dee91e82 13448 struct dwarf2_cu *cu = reader->cu;
bb5ed363 13449 struct objfile *objfile = cu->objfile;
dee91e82 13450 gdb_byte *buffer = reader->buffer;
fa238c03 13451 unsigned int i;
c906108c 13452 struct attribute attr;
c5aa993b 13453 int has_low_pc_attr = 0;
c906108c 13454 int has_high_pc_attr = 0;
91da1414 13455 int high_pc_relative = 0;
c906108c 13456
72bf9492 13457 memset (part_die, 0, sizeof (struct partial_die_info));
c906108c 13458
b64f50a1 13459 part_die->offset.sect_off = info_ptr - buffer;
72bf9492
DJ
13460
13461 info_ptr += abbrev_len;
13462
13463 if (abbrev == NULL)
13464 return info_ptr;
13465
c906108c
SS
13466 part_die->tag = abbrev->tag;
13467 part_die->has_children = abbrev->has_children;
c906108c
SS
13468
13469 for (i = 0; i < abbrev->num_attrs; ++i)
13470 {
dee91e82 13471 info_ptr = read_attribute (reader, &attr, &abbrev->attrs[i], info_ptr);
c906108c
SS
13472
13473 /* Store the data if it is of an attribute we want to keep in a
c5aa993b 13474 partial symbol table. */
c906108c
SS
13475 switch (attr.name)
13476 {
13477 case DW_AT_name:
71c25dea
TT
13478 switch (part_die->tag)
13479 {
13480 case DW_TAG_compile_unit:
95554aad 13481 case DW_TAG_partial_unit:
348e048f 13482 case DW_TAG_type_unit:
71c25dea
TT
13483 /* Compilation units have a DW_AT_name that is a filename, not
13484 a source language identifier. */
13485 case DW_TAG_enumeration_type:
13486 case DW_TAG_enumerator:
13487 /* These tags always have simple identifiers already; no need
13488 to canonicalize them. */
13489 part_die->name = DW_STRING (&attr);
13490 break;
13491 default:
13492 part_die->name
13493 = dwarf2_canonicalize_name (DW_STRING (&attr), cu,
bb5ed363 13494 &objfile->objfile_obstack);
71c25dea
TT
13495 break;
13496 }
c906108c 13497 break;
31ef98ae 13498 case DW_AT_linkage_name:
c906108c 13499 case DW_AT_MIPS_linkage_name:
31ef98ae
TT
13500 /* Note that both forms of linkage name might appear. We
13501 assume they will be the same, and we only store the last
13502 one we see. */
94af9270
KS
13503 if (cu->language == language_ada)
13504 part_die->name = DW_STRING (&attr);
abc72ce4 13505 part_die->linkage_name = DW_STRING (&attr);
c906108c
SS
13506 break;
13507 case DW_AT_low_pc:
13508 has_low_pc_attr = 1;
13509 part_die->lowpc = DW_ADDR (&attr);
13510 break;
13511 case DW_AT_high_pc:
13512 has_high_pc_attr = 1;
3019eac3
DE
13513 if (attr.form == DW_FORM_addr
13514 || attr.form == DW_FORM_GNU_addr_index)
91da1414
MW
13515 part_die->highpc = DW_ADDR (&attr);
13516 else
13517 {
13518 high_pc_relative = 1;
13519 part_die->highpc = DW_UNSND (&attr);
13520 }
c906108c
SS
13521 break;
13522 case DW_AT_location:
0963b4bd 13523 /* Support the .debug_loc offsets. */
8e19ed76
PS
13524 if (attr_form_is_block (&attr))
13525 {
95554aad 13526 part_die->d.locdesc = DW_BLOCK (&attr);
8e19ed76 13527 }
3690dd37 13528 else if (attr_form_is_section_offset (&attr))
8e19ed76 13529 {
4d3c2250 13530 dwarf2_complex_location_expr_complaint ();
8e19ed76
PS
13531 }
13532 else
13533 {
4d3c2250
KB
13534 dwarf2_invalid_attrib_class_complaint ("DW_AT_location",
13535 "partial symbol information");
8e19ed76 13536 }
c906108c 13537 break;
c906108c
SS
13538 case DW_AT_external:
13539 part_die->is_external = DW_UNSND (&attr);
13540 break;
13541 case DW_AT_declaration:
13542 part_die->is_declaration = DW_UNSND (&attr);
13543 break;
13544 case DW_AT_type:
13545 part_die->has_type = 1;
13546 break;
13547 case DW_AT_abstract_origin:
13548 case DW_AT_specification:
72bf9492
DJ
13549 case DW_AT_extension:
13550 part_die->has_specification = 1;
c764a876 13551 part_die->spec_offset = dwarf2_get_ref_die_offset (&attr);
36586728
TT
13552 part_die->spec_is_dwz = (attr.form == DW_FORM_GNU_ref_alt
13553 || cu->per_cu->is_dwz);
c906108c
SS
13554 break;
13555 case DW_AT_sibling:
13556 /* Ignore absolute siblings, they might point outside of
13557 the current compile unit. */
13558 if (attr.form == DW_FORM_ref_addr)
3e43a32a
MS
13559 complaint (&symfile_complaints,
13560 _("ignoring absolute DW_AT_sibling"));
c906108c 13561 else
b64f50a1 13562 part_die->sibling = buffer + dwarf2_get_ref_die_offset (&attr).sect_off;
c906108c 13563 break;
fa4028e9
JB
13564 case DW_AT_byte_size:
13565 part_die->has_byte_size = 1;
13566 break;
68511cec
CES
13567 case DW_AT_calling_convention:
13568 /* DWARF doesn't provide a way to identify a program's source-level
13569 entry point. DW_AT_calling_convention attributes are only meant
13570 to describe functions' calling conventions.
13571
13572 However, because it's a necessary piece of information in
13573 Fortran, and because DW_CC_program is the only piece of debugging
13574 information whose definition refers to a 'main program' at all,
13575 several compilers have begun marking Fortran main programs with
13576 DW_CC_program --- even when those functions use the standard
13577 calling conventions.
13578
13579 So until DWARF specifies a way to provide this information and
13580 compilers pick up the new representation, we'll support this
13581 practice. */
13582 if (DW_UNSND (&attr) == DW_CC_program
13583 && cu->language == language_fortran)
01f8c46d
JK
13584 {
13585 set_main_name (part_die->name);
13586
13587 /* As this DIE has a static linkage the name would be difficult
13588 to look up later. */
13589 language_of_main = language_fortran;
13590 }
68511cec 13591 break;
481860b3
GB
13592 case DW_AT_inline:
13593 if (DW_UNSND (&attr) == DW_INL_inlined
13594 || DW_UNSND (&attr) == DW_INL_declared_inlined)
13595 part_die->may_be_inlined = 1;
13596 break;
95554aad
TT
13597
13598 case DW_AT_import:
13599 if (part_die->tag == DW_TAG_imported_unit)
36586728
TT
13600 {
13601 part_die->d.offset = dwarf2_get_ref_die_offset (&attr);
13602 part_die->is_dwz = (attr.form == DW_FORM_GNU_ref_alt
13603 || cu->per_cu->is_dwz);
13604 }
95554aad
TT
13605 break;
13606
c906108c
SS
13607 default:
13608 break;
13609 }
13610 }
13611
91da1414
MW
13612 if (high_pc_relative)
13613 part_die->highpc += part_die->lowpc;
13614
9373cf26
JK
13615 if (has_low_pc_attr && has_high_pc_attr)
13616 {
13617 /* When using the GNU linker, .gnu.linkonce. sections are used to
13618 eliminate duplicate copies of functions and vtables and such.
13619 The linker will arbitrarily choose one and discard the others.
13620 The AT_*_pc values for such functions refer to local labels in
13621 these sections. If the section from that file was discarded, the
13622 labels are not in the output, so the relocs get a value of 0.
13623 If this is a discarded function, mark the pc bounds as invalid,
13624 so that GDB will ignore it. */
13625 if (part_die->lowpc == 0 && !dwarf2_per_objfile->has_section_at_zero)
13626 {
bb5ed363 13627 struct gdbarch *gdbarch = get_objfile_arch (objfile);
9373cf26
JK
13628
13629 complaint (&symfile_complaints,
13630 _("DW_AT_low_pc %s is zero "
13631 "for DIE at 0x%x [in module %s]"),
13632 paddress (gdbarch, part_die->lowpc),
b64f50a1 13633 part_die->offset.sect_off, objfile->name);
9373cf26
JK
13634 }
13635 /* dwarf2_get_pc_bounds has also the strict low < high requirement. */
13636 else if (part_die->lowpc >= part_die->highpc)
13637 {
bb5ed363 13638 struct gdbarch *gdbarch = get_objfile_arch (objfile);
9373cf26
JK
13639
13640 complaint (&symfile_complaints,
13641 _("DW_AT_low_pc %s is not < DW_AT_high_pc %s "
13642 "for DIE at 0x%x [in module %s]"),
13643 paddress (gdbarch, part_die->lowpc),
13644 paddress (gdbarch, part_die->highpc),
b64f50a1 13645 part_die->offset.sect_off, objfile->name);
9373cf26
JK
13646 }
13647 else
13648 part_die->has_pc_info = 1;
13649 }
85cbf3d3 13650
c906108c
SS
13651 return info_ptr;
13652}
13653
72bf9492
DJ
13654/* Find a cached partial DIE at OFFSET in CU. */
13655
13656static struct partial_die_info *
b64f50a1 13657find_partial_die_in_comp_unit (sect_offset offset, struct dwarf2_cu *cu)
72bf9492
DJ
13658{
13659 struct partial_die_info *lookup_die = NULL;
13660 struct partial_die_info part_die;
13661
13662 part_die.offset = offset;
b64f50a1
JK
13663 lookup_die = htab_find_with_hash (cu->partial_dies, &part_die,
13664 offset.sect_off);
72bf9492 13665
72bf9492
DJ
13666 return lookup_die;
13667}
13668
348e048f
DE
13669/* Find a partial DIE at OFFSET, which may or may not be in CU,
13670 except in the case of .debug_types DIEs which do not reference
13671 outside their CU (they do however referencing other types via
55f1336d 13672 DW_FORM_ref_sig8). */
72bf9492
DJ
13673
13674static struct partial_die_info *
36586728 13675find_partial_die (sect_offset offset, int offset_in_dwz, struct dwarf2_cu *cu)
72bf9492 13676{
bb5ed363 13677 struct objfile *objfile = cu->objfile;
5afb4e99
DJ
13678 struct dwarf2_per_cu_data *per_cu = NULL;
13679 struct partial_die_info *pd = NULL;
72bf9492 13680
36586728
TT
13681 if (offset_in_dwz == cu->per_cu->is_dwz
13682 && offset_in_cu_p (&cu->header, offset))
5afb4e99
DJ
13683 {
13684 pd = find_partial_die_in_comp_unit (offset, cu);
13685 if (pd != NULL)
13686 return pd;
0d99eb77
DE
13687 /* We missed recording what we needed.
13688 Load all dies and try again. */
13689 per_cu = cu->per_cu;
5afb4e99 13690 }
0d99eb77
DE
13691 else
13692 {
13693 /* TUs don't reference other CUs/TUs (except via type signatures). */
3019eac3 13694 if (cu->per_cu->is_debug_types)
0d99eb77
DE
13695 {
13696 error (_("Dwarf Error: Type Unit at offset 0x%lx contains"
13697 " external reference to offset 0x%lx [in module %s].\n"),
13698 (long) cu->header.offset.sect_off, (long) offset.sect_off,
13699 bfd_get_filename (objfile->obfd));
13700 }
36586728
TT
13701 per_cu = dwarf2_find_containing_comp_unit (offset, offset_in_dwz,
13702 objfile);
72bf9492 13703
0d99eb77
DE
13704 if (per_cu->cu == NULL || per_cu->cu->partial_dies == NULL)
13705 load_partial_comp_unit (per_cu);
ae038cb0 13706
0d99eb77
DE
13707 per_cu->cu->last_used = 0;
13708 pd = find_partial_die_in_comp_unit (offset, per_cu->cu);
13709 }
5afb4e99 13710
dee91e82
DE
13711 /* If we didn't find it, and not all dies have been loaded,
13712 load them all and try again. */
13713
5afb4e99
DJ
13714 if (pd == NULL && per_cu->load_all_dies == 0)
13715 {
5afb4e99 13716 per_cu->load_all_dies = 1;
fd820528
DE
13717
13718 /* This is nasty. When we reread the DIEs, somewhere up the call chain
13719 THIS_CU->cu may already be in use. So we can't just free it and
13720 replace its DIEs with the ones we read in. Instead, we leave those
13721 DIEs alone (which can still be in use, e.g. in scan_partial_symbols),
13722 and clobber THIS_CU->cu->partial_dies with the hash table for the new
13723 set. */
dee91e82 13724 load_partial_comp_unit (per_cu);
5afb4e99
DJ
13725
13726 pd = find_partial_die_in_comp_unit (offset, per_cu->cu);
13727 }
13728
13729 if (pd == NULL)
13730 internal_error (__FILE__, __LINE__,
3e43a32a
MS
13731 _("could not find partial DIE 0x%x "
13732 "in cache [from module %s]\n"),
b64f50a1 13733 offset.sect_off, bfd_get_filename (objfile->obfd));
5afb4e99 13734 return pd;
72bf9492
DJ
13735}
13736
abc72ce4
DE
13737/* See if we can figure out if the class lives in a namespace. We do
13738 this by looking for a member function; its demangled name will
13739 contain namespace info, if there is any. */
13740
13741static void
13742guess_partial_die_structure_name (struct partial_die_info *struct_pdi,
13743 struct dwarf2_cu *cu)
13744{
13745 /* NOTE: carlton/2003-10-07: Getting the info this way changes
13746 what template types look like, because the demangler
13747 frequently doesn't give the same name as the debug info. We
13748 could fix this by only using the demangled name to get the
13749 prefix (but see comment in read_structure_type). */
13750
13751 struct partial_die_info *real_pdi;
13752 struct partial_die_info *child_pdi;
13753
13754 /* If this DIE (this DIE's specification, if any) has a parent, then
13755 we should not do this. We'll prepend the parent's fully qualified
13756 name when we create the partial symbol. */
13757
13758 real_pdi = struct_pdi;
13759 while (real_pdi->has_specification)
36586728
TT
13760 real_pdi = find_partial_die (real_pdi->spec_offset,
13761 real_pdi->spec_is_dwz, cu);
abc72ce4
DE
13762
13763 if (real_pdi->die_parent != NULL)
13764 return;
13765
13766 for (child_pdi = struct_pdi->die_child;
13767 child_pdi != NULL;
13768 child_pdi = child_pdi->die_sibling)
13769 {
13770 if (child_pdi->tag == DW_TAG_subprogram
13771 && child_pdi->linkage_name != NULL)
13772 {
13773 char *actual_class_name
13774 = language_class_name_from_physname (cu->language_defn,
13775 child_pdi->linkage_name);
13776 if (actual_class_name != NULL)
13777 {
13778 struct_pdi->name
13779 = obsavestring (actual_class_name,
13780 strlen (actual_class_name),
13781 &cu->objfile->objfile_obstack);
13782 xfree (actual_class_name);
13783 }
13784 break;
13785 }
13786 }
13787}
13788
72bf9492
DJ
13789/* Adjust PART_DIE before generating a symbol for it. This function
13790 may set the is_external flag or change the DIE's name. */
13791
13792static void
13793fixup_partial_die (struct partial_die_info *part_die,
13794 struct dwarf2_cu *cu)
13795{
abc72ce4
DE
13796 /* Once we've fixed up a die, there's no point in doing so again.
13797 This also avoids a memory leak if we were to call
13798 guess_partial_die_structure_name multiple times. */
13799 if (part_die->fixup_called)
13800 return;
13801
72bf9492
DJ
13802 /* If we found a reference attribute and the DIE has no name, try
13803 to find a name in the referred to DIE. */
13804
13805 if (part_die->name == NULL && part_die->has_specification)
13806 {
13807 struct partial_die_info *spec_die;
72bf9492 13808
36586728
TT
13809 spec_die = find_partial_die (part_die->spec_offset,
13810 part_die->spec_is_dwz, cu);
72bf9492 13811
10b3939b 13812 fixup_partial_die (spec_die, cu);
72bf9492
DJ
13813
13814 if (spec_die->name)
13815 {
13816 part_die->name = spec_die->name;
13817
13818 /* Copy DW_AT_external attribute if it is set. */
13819 if (spec_die->is_external)
13820 part_die->is_external = spec_die->is_external;
13821 }
13822 }
13823
13824 /* Set default names for some unnamed DIEs. */
72bf9492
DJ
13825
13826 if (part_die->name == NULL && part_die->tag == DW_TAG_namespace)
2b1dbab0 13827 part_die->name = CP_ANONYMOUS_NAMESPACE_STR;
72bf9492 13828
abc72ce4
DE
13829 /* If there is no parent die to provide a namespace, and there are
13830 children, see if we can determine the namespace from their linkage
122d1940 13831 name. */
abc72ce4 13832 if (cu->language == language_cplus
8b70b953 13833 && !VEC_empty (dwarf2_section_info_def, dwarf2_per_objfile->types)
abc72ce4
DE
13834 && part_die->die_parent == NULL
13835 && part_die->has_children
13836 && (part_die->tag == DW_TAG_class_type
13837 || part_die->tag == DW_TAG_structure_type
13838 || part_die->tag == DW_TAG_union_type))
13839 guess_partial_die_structure_name (part_die, cu);
13840
53832f31
TT
13841 /* GCC might emit a nameless struct or union that has a linkage
13842 name. See http://gcc.gnu.org/bugzilla/show_bug.cgi?id=47510. */
13843 if (part_die->name == NULL
96408a79
SA
13844 && (part_die->tag == DW_TAG_class_type
13845 || part_die->tag == DW_TAG_interface_type
13846 || part_die->tag == DW_TAG_structure_type
13847 || part_die->tag == DW_TAG_union_type)
53832f31
TT
13848 && part_die->linkage_name != NULL)
13849 {
13850 char *demangled;
13851
13852 demangled = cplus_demangle (part_die->linkage_name, DMGL_TYPES);
13853 if (demangled)
13854 {
96408a79
SA
13855 const char *base;
13856
13857 /* Strip any leading namespaces/classes, keep only the base name.
13858 DW_AT_name for named DIEs does not contain the prefixes. */
13859 base = strrchr (demangled, ':');
13860 if (base && base > demangled && base[-1] == ':')
13861 base++;
13862 else
13863 base = demangled;
13864
13865 part_die->name = obsavestring (base, strlen (base),
53832f31
TT
13866 &cu->objfile->objfile_obstack);
13867 xfree (demangled);
13868 }
13869 }
13870
abc72ce4 13871 part_die->fixup_called = 1;
72bf9492
DJ
13872}
13873
a8329558 13874/* Read an attribute value described by an attribute form. */
c906108c 13875
fe1b8b76 13876static gdb_byte *
dee91e82
DE
13877read_attribute_value (const struct die_reader_specs *reader,
13878 struct attribute *attr, unsigned form,
13879 gdb_byte *info_ptr)
c906108c 13880{
dee91e82
DE
13881 struct dwarf2_cu *cu = reader->cu;
13882 bfd *abfd = reader->abfd;
e7c27a73 13883 struct comp_unit_head *cu_header = &cu->header;
c906108c
SS
13884 unsigned int bytes_read;
13885 struct dwarf_block *blk;
13886
a8329558
KW
13887 attr->form = form;
13888 switch (form)
c906108c 13889 {
c906108c 13890 case DW_FORM_ref_addr:
ae411497 13891 if (cu->header.version == 2)
4568ecf9 13892 DW_UNSND (attr) = read_address (abfd, info_ptr, cu, &bytes_read);
ae411497 13893 else
4568ecf9
DE
13894 DW_UNSND (attr) = read_offset (abfd, info_ptr,
13895 &cu->header, &bytes_read);
ae411497
TT
13896 info_ptr += bytes_read;
13897 break;
36586728
TT
13898 case DW_FORM_GNU_ref_alt:
13899 DW_UNSND (attr) = read_offset (abfd, info_ptr, &cu->header, &bytes_read);
13900 info_ptr += bytes_read;
13901 break;
ae411497 13902 case DW_FORM_addr:
e7c27a73 13903 DW_ADDR (attr) = read_address (abfd, info_ptr, cu, &bytes_read);
107d2387 13904 info_ptr += bytes_read;
c906108c
SS
13905 break;
13906 case DW_FORM_block2:
7b5a2f43 13907 blk = dwarf_alloc_block (cu);
c906108c
SS
13908 blk->size = read_2_bytes (abfd, info_ptr);
13909 info_ptr += 2;
13910 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
13911 info_ptr += blk->size;
13912 DW_BLOCK (attr) = blk;
13913 break;
13914 case DW_FORM_block4:
7b5a2f43 13915 blk = dwarf_alloc_block (cu);
c906108c
SS
13916 blk->size = read_4_bytes (abfd, info_ptr);
13917 info_ptr += 4;
13918 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
13919 info_ptr += blk->size;
13920 DW_BLOCK (attr) = blk;
13921 break;
13922 case DW_FORM_data2:
13923 DW_UNSND (attr) = read_2_bytes (abfd, info_ptr);
13924 info_ptr += 2;
13925 break;
13926 case DW_FORM_data4:
13927 DW_UNSND (attr) = read_4_bytes (abfd, info_ptr);
13928 info_ptr += 4;
13929 break;
13930 case DW_FORM_data8:
13931 DW_UNSND (attr) = read_8_bytes (abfd, info_ptr);
13932 info_ptr += 8;
13933 break;
2dc7f7b3
TT
13934 case DW_FORM_sec_offset:
13935 DW_UNSND (attr) = read_offset (abfd, info_ptr, &cu->header, &bytes_read);
13936 info_ptr += bytes_read;
13937 break;
c906108c 13938 case DW_FORM_string:
9b1c24c8 13939 DW_STRING (attr) = read_direct_string (abfd, info_ptr, &bytes_read);
8285870a 13940 DW_STRING_IS_CANONICAL (attr) = 0;
c906108c
SS
13941 info_ptr += bytes_read;
13942 break;
4bdf3d34 13943 case DW_FORM_strp:
36586728
TT
13944 if (!cu->per_cu->is_dwz)
13945 {
13946 DW_STRING (attr) = read_indirect_string (abfd, info_ptr, cu_header,
13947 &bytes_read);
13948 DW_STRING_IS_CANONICAL (attr) = 0;
13949 info_ptr += bytes_read;
13950 break;
13951 }
13952 /* FALLTHROUGH */
13953 case DW_FORM_GNU_strp_alt:
13954 {
13955 struct dwz_file *dwz = dwarf2_get_dwz_file ();
13956 LONGEST str_offset = read_offset (abfd, info_ptr, cu_header,
13957 &bytes_read);
13958
13959 DW_STRING (attr) = read_indirect_string_from_dwz (dwz, str_offset);
13960 DW_STRING_IS_CANONICAL (attr) = 0;
13961 info_ptr += bytes_read;
13962 }
4bdf3d34 13963 break;
2dc7f7b3 13964 case DW_FORM_exprloc:
c906108c 13965 case DW_FORM_block:
7b5a2f43 13966 blk = dwarf_alloc_block (cu);
c906108c
SS
13967 blk->size = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
13968 info_ptr += bytes_read;
13969 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
13970 info_ptr += blk->size;
13971 DW_BLOCK (attr) = blk;
13972 break;
13973 case DW_FORM_block1:
7b5a2f43 13974 blk = dwarf_alloc_block (cu);
c906108c
SS
13975 blk->size = read_1_byte (abfd, info_ptr);
13976 info_ptr += 1;
13977 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
13978 info_ptr += blk->size;
13979 DW_BLOCK (attr) = blk;
13980 break;
13981 case DW_FORM_data1:
13982 DW_UNSND (attr) = read_1_byte (abfd, info_ptr);
13983 info_ptr += 1;
13984 break;
13985 case DW_FORM_flag:
13986 DW_UNSND (attr) = read_1_byte (abfd, info_ptr);
13987 info_ptr += 1;
13988 break;
2dc7f7b3
TT
13989 case DW_FORM_flag_present:
13990 DW_UNSND (attr) = 1;
13991 break;
c906108c
SS
13992 case DW_FORM_sdata:
13993 DW_SND (attr) = read_signed_leb128 (abfd, info_ptr, &bytes_read);
13994 info_ptr += bytes_read;
13995 break;
13996 case DW_FORM_udata:
13997 DW_UNSND (attr) = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
13998 info_ptr += bytes_read;
13999 break;
14000 case DW_FORM_ref1:
4568ecf9
DE
14001 DW_UNSND (attr) = (cu->header.offset.sect_off
14002 + read_1_byte (abfd, info_ptr));
c906108c
SS
14003 info_ptr += 1;
14004 break;
14005 case DW_FORM_ref2:
4568ecf9
DE
14006 DW_UNSND (attr) = (cu->header.offset.sect_off
14007 + read_2_bytes (abfd, info_ptr));
c906108c
SS
14008 info_ptr += 2;
14009 break;
14010 case DW_FORM_ref4:
4568ecf9
DE
14011 DW_UNSND (attr) = (cu->header.offset.sect_off
14012 + read_4_bytes (abfd, info_ptr));
c906108c
SS
14013 info_ptr += 4;
14014 break;
613e1657 14015 case DW_FORM_ref8:
4568ecf9
DE
14016 DW_UNSND (attr) = (cu->header.offset.sect_off
14017 + read_8_bytes (abfd, info_ptr));
613e1657
KB
14018 info_ptr += 8;
14019 break;
55f1336d 14020 case DW_FORM_ref_sig8:
348e048f
DE
14021 /* Convert the signature to something we can record in DW_UNSND
14022 for later lookup.
14023 NOTE: This is NULL if the type wasn't found. */
14024 DW_SIGNATURED_TYPE (attr) =
e319fa28 14025 lookup_signatured_type (read_8_bytes (abfd, info_ptr));
348e048f
DE
14026 info_ptr += 8;
14027 break;
c906108c 14028 case DW_FORM_ref_udata:
4568ecf9
DE
14029 DW_UNSND (attr) = (cu->header.offset.sect_off
14030 + read_unsigned_leb128 (abfd, info_ptr, &bytes_read));
c906108c
SS
14031 info_ptr += bytes_read;
14032 break;
c906108c 14033 case DW_FORM_indirect:
a8329558
KW
14034 form = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
14035 info_ptr += bytes_read;
dee91e82 14036 info_ptr = read_attribute_value (reader, attr, form, info_ptr);
a8329558 14037 break;
3019eac3
DE
14038 case DW_FORM_GNU_addr_index:
14039 if (reader->dwo_file == NULL)
14040 {
14041 /* For now flag a hard error.
14042 Later we can turn this into a complaint. */
14043 error (_("Dwarf Error: %s found in non-DWO CU [in module %s]"),
14044 dwarf_form_name (form),
14045 bfd_get_filename (abfd));
14046 }
14047 DW_ADDR (attr) = read_addr_index_from_leb128 (cu, info_ptr, &bytes_read);
14048 info_ptr += bytes_read;
14049 break;
14050 case DW_FORM_GNU_str_index:
14051 if (reader->dwo_file == NULL)
14052 {
14053 /* For now flag a hard error.
14054 Later we can turn this into a complaint if warranted. */
14055 error (_("Dwarf Error: %s found in non-DWO CU [in module %s]"),
14056 dwarf_form_name (form),
14057 bfd_get_filename (abfd));
14058 }
14059 {
14060 ULONGEST str_index =
14061 read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
14062
14063 DW_STRING (attr) = read_str_index (reader, cu, str_index);
14064 DW_STRING_IS_CANONICAL (attr) = 0;
14065 info_ptr += bytes_read;
14066 }
14067 break;
c906108c 14068 default:
8a3fe4f8 14069 error (_("Dwarf Error: Cannot handle %s in DWARF reader [in module %s]"),
659b0389
ML
14070 dwarf_form_name (form),
14071 bfd_get_filename (abfd));
c906108c 14072 }
28e94949 14073
36586728
TT
14074 /* Super hack. */
14075 if (cu->per_cu->is_dwz && is_ref_attr (attr))
14076 attr->form = DW_FORM_GNU_ref_alt;
14077
28e94949
JB
14078 /* We have seen instances where the compiler tried to emit a byte
14079 size attribute of -1 which ended up being encoded as an unsigned
14080 0xffffffff. Although 0xffffffff is technically a valid size value,
14081 an object of this size seems pretty unlikely so we can relatively
14082 safely treat these cases as if the size attribute was invalid and
14083 treat them as zero by default. */
14084 if (attr->name == DW_AT_byte_size
14085 && form == DW_FORM_data4
14086 && DW_UNSND (attr) >= 0xffffffff)
01c66ae6
JB
14087 {
14088 complaint
14089 (&symfile_complaints,
43bbcdc2
PH
14090 _("Suspicious DW_AT_byte_size value treated as zero instead of %s"),
14091 hex_string (DW_UNSND (attr)));
01c66ae6
JB
14092 DW_UNSND (attr) = 0;
14093 }
28e94949 14094
c906108c
SS
14095 return info_ptr;
14096}
14097
a8329558
KW
14098/* Read an attribute described by an abbreviated attribute. */
14099
fe1b8b76 14100static gdb_byte *
dee91e82
DE
14101read_attribute (const struct die_reader_specs *reader,
14102 struct attribute *attr, struct attr_abbrev *abbrev,
14103 gdb_byte *info_ptr)
a8329558
KW
14104{
14105 attr->name = abbrev->name;
dee91e82 14106 return read_attribute_value (reader, attr, abbrev->form, info_ptr);
a8329558
KW
14107}
14108
0963b4bd 14109/* Read dwarf information from a buffer. */
c906108c
SS
14110
14111static unsigned int
a1855c1d 14112read_1_byte (bfd *abfd, const gdb_byte *buf)
c906108c 14113{
fe1b8b76 14114 return bfd_get_8 (abfd, buf);
c906108c
SS
14115}
14116
14117static int
a1855c1d 14118read_1_signed_byte (bfd *abfd, const gdb_byte *buf)
c906108c 14119{
fe1b8b76 14120 return bfd_get_signed_8 (abfd, buf);
c906108c
SS
14121}
14122
14123static unsigned int
a1855c1d 14124read_2_bytes (bfd *abfd, const gdb_byte *buf)
c906108c 14125{
fe1b8b76 14126 return bfd_get_16 (abfd, buf);
c906108c
SS
14127}
14128
21ae7a4d 14129static int
a1855c1d 14130read_2_signed_bytes (bfd *abfd, const gdb_byte *buf)
21ae7a4d
JK
14131{
14132 return bfd_get_signed_16 (abfd, buf);
14133}
14134
c906108c 14135static unsigned int
a1855c1d 14136read_4_bytes (bfd *abfd, const gdb_byte *buf)
c906108c 14137{
fe1b8b76 14138 return bfd_get_32 (abfd, buf);
c906108c
SS
14139}
14140
21ae7a4d 14141static int
a1855c1d 14142read_4_signed_bytes (bfd *abfd, const gdb_byte *buf)
21ae7a4d
JK
14143{
14144 return bfd_get_signed_32 (abfd, buf);
14145}
14146
93311388 14147static ULONGEST
a1855c1d 14148read_8_bytes (bfd *abfd, const gdb_byte *buf)
c906108c 14149{
fe1b8b76 14150 return bfd_get_64 (abfd, buf);
c906108c
SS
14151}
14152
14153static CORE_ADDR
fe1b8b76 14154read_address (bfd *abfd, gdb_byte *buf, struct dwarf2_cu *cu,
891d2f0b 14155 unsigned int *bytes_read)
c906108c 14156{
e7c27a73 14157 struct comp_unit_head *cu_header = &cu->header;
c906108c
SS
14158 CORE_ADDR retval = 0;
14159
107d2387 14160 if (cu_header->signed_addr_p)
c906108c 14161 {
107d2387
AC
14162 switch (cu_header->addr_size)
14163 {
14164 case 2:
fe1b8b76 14165 retval = bfd_get_signed_16 (abfd, buf);
107d2387
AC
14166 break;
14167 case 4:
fe1b8b76 14168 retval = bfd_get_signed_32 (abfd, buf);
107d2387
AC
14169 break;
14170 case 8:
fe1b8b76 14171 retval = bfd_get_signed_64 (abfd, buf);
107d2387
AC
14172 break;
14173 default:
8e65ff28 14174 internal_error (__FILE__, __LINE__,
e2e0b3e5 14175 _("read_address: bad switch, signed [in module %s]"),
659b0389 14176 bfd_get_filename (abfd));
107d2387
AC
14177 }
14178 }
14179 else
14180 {
14181 switch (cu_header->addr_size)
14182 {
14183 case 2:
fe1b8b76 14184 retval = bfd_get_16 (abfd, buf);
107d2387
AC
14185 break;
14186 case 4:
fe1b8b76 14187 retval = bfd_get_32 (abfd, buf);
107d2387
AC
14188 break;
14189 case 8:
fe1b8b76 14190 retval = bfd_get_64 (abfd, buf);
107d2387
AC
14191 break;
14192 default:
8e65ff28 14193 internal_error (__FILE__, __LINE__,
a73c6dcd
MS
14194 _("read_address: bad switch, "
14195 "unsigned [in module %s]"),
659b0389 14196 bfd_get_filename (abfd));
107d2387 14197 }
c906108c 14198 }
64367e0a 14199
107d2387
AC
14200 *bytes_read = cu_header->addr_size;
14201 return retval;
c906108c
SS
14202}
14203
f7ef9339 14204/* Read the initial length from a section. The (draft) DWARF 3
613e1657
KB
14205 specification allows the initial length to take up either 4 bytes
14206 or 12 bytes. If the first 4 bytes are 0xffffffff, then the next 8
14207 bytes describe the length and all offsets will be 8 bytes in length
14208 instead of 4.
14209
f7ef9339
KB
14210 An older, non-standard 64-bit format is also handled by this
14211 function. The older format in question stores the initial length
14212 as an 8-byte quantity without an escape value. Lengths greater
14213 than 2^32 aren't very common which means that the initial 4 bytes
14214 is almost always zero. Since a length value of zero doesn't make
14215 sense for the 32-bit format, this initial zero can be considered to
14216 be an escape value which indicates the presence of the older 64-bit
14217 format. As written, the code can't detect (old format) lengths
917c78fc
MK
14218 greater than 4GB. If it becomes necessary to handle lengths
14219 somewhat larger than 4GB, we could allow other small values (such
14220 as the non-sensical values of 1, 2, and 3) to also be used as
14221 escape values indicating the presence of the old format.
f7ef9339 14222
917c78fc
MK
14223 The value returned via bytes_read should be used to increment the
14224 relevant pointer after calling read_initial_length().
c764a876 14225
613e1657
KB
14226 [ Note: read_initial_length() and read_offset() are based on the
14227 document entitled "DWARF Debugging Information Format", revision
f7ef9339 14228 3, draft 8, dated November 19, 2001. This document was obtained
613e1657
KB
14229 from:
14230
f7ef9339 14231 http://reality.sgiweb.org/davea/dwarf3-draft8-011125.pdf
6e70227d 14232
613e1657
KB
14233 This document is only a draft and is subject to change. (So beware.)
14234
f7ef9339 14235 Details regarding the older, non-standard 64-bit format were
917c78fc
MK
14236 determined empirically by examining 64-bit ELF files produced by
14237 the SGI toolchain on an IRIX 6.5 machine.
f7ef9339
KB
14238
14239 - Kevin, July 16, 2002
613e1657
KB
14240 ] */
14241
14242static LONGEST
c764a876 14243read_initial_length (bfd *abfd, gdb_byte *buf, unsigned int *bytes_read)
613e1657 14244{
fe1b8b76 14245 LONGEST length = bfd_get_32 (abfd, buf);
613e1657 14246
dd373385 14247 if (length == 0xffffffff)
613e1657 14248 {
fe1b8b76 14249 length = bfd_get_64 (abfd, buf + 4);
613e1657 14250 *bytes_read = 12;
613e1657 14251 }
dd373385 14252 else if (length == 0)
f7ef9339 14253 {
dd373385 14254 /* Handle the (non-standard) 64-bit DWARF2 format used by IRIX. */
fe1b8b76 14255 length = bfd_get_64 (abfd, buf);
f7ef9339 14256 *bytes_read = 8;
f7ef9339 14257 }
613e1657
KB
14258 else
14259 {
14260 *bytes_read = 4;
613e1657
KB
14261 }
14262
c764a876
DE
14263 return length;
14264}
dd373385 14265
c764a876
DE
14266/* Cover function for read_initial_length.
14267 Returns the length of the object at BUF, and stores the size of the
14268 initial length in *BYTES_READ and stores the size that offsets will be in
14269 *OFFSET_SIZE.
14270 If the initial length size is not equivalent to that specified in
14271 CU_HEADER then issue a complaint.
14272 This is useful when reading non-comp-unit headers. */
dd373385 14273
c764a876
DE
14274static LONGEST
14275read_checked_initial_length_and_offset (bfd *abfd, gdb_byte *buf,
14276 const struct comp_unit_head *cu_header,
14277 unsigned int *bytes_read,
14278 unsigned int *offset_size)
14279{
14280 LONGEST length = read_initial_length (abfd, buf, bytes_read);
14281
14282 gdb_assert (cu_header->initial_length_size == 4
14283 || cu_header->initial_length_size == 8
14284 || cu_header->initial_length_size == 12);
14285
14286 if (cu_header->initial_length_size != *bytes_read)
14287 complaint (&symfile_complaints,
14288 _("intermixed 32-bit and 64-bit DWARF sections"));
dd373385 14289
c764a876 14290 *offset_size = (*bytes_read == 4) ? 4 : 8;
dd373385 14291 return length;
613e1657
KB
14292}
14293
14294/* Read an offset from the data stream. The size of the offset is
917c78fc 14295 given by cu_header->offset_size. */
613e1657
KB
14296
14297static LONGEST
fe1b8b76 14298read_offset (bfd *abfd, gdb_byte *buf, const struct comp_unit_head *cu_header,
891d2f0b 14299 unsigned int *bytes_read)
c764a876
DE
14300{
14301 LONGEST offset = read_offset_1 (abfd, buf, cu_header->offset_size);
9a619af0 14302
c764a876
DE
14303 *bytes_read = cu_header->offset_size;
14304 return offset;
14305}
14306
14307/* Read an offset from the data stream. */
14308
14309static LONGEST
14310read_offset_1 (bfd *abfd, gdb_byte *buf, unsigned int offset_size)
613e1657
KB
14311{
14312 LONGEST retval = 0;
14313
c764a876 14314 switch (offset_size)
613e1657
KB
14315 {
14316 case 4:
fe1b8b76 14317 retval = bfd_get_32 (abfd, buf);
613e1657
KB
14318 break;
14319 case 8:
fe1b8b76 14320 retval = bfd_get_64 (abfd, buf);
613e1657
KB
14321 break;
14322 default:
8e65ff28 14323 internal_error (__FILE__, __LINE__,
c764a876 14324 _("read_offset_1: bad switch [in module %s]"),
659b0389 14325 bfd_get_filename (abfd));
613e1657
KB
14326 }
14327
917c78fc 14328 return retval;
613e1657
KB
14329}
14330
fe1b8b76
JB
14331static gdb_byte *
14332read_n_bytes (bfd *abfd, gdb_byte *buf, unsigned int size)
c906108c
SS
14333{
14334 /* If the size of a host char is 8 bits, we can return a pointer
14335 to the buffer, otherwise we have to copy the data to a buffer
14336 allocated on the temporary obstack. */
4bdf3d34 14337 gdb_assert (HOST_CHAR_BIT == 8);
c906108c 14338 return buf;
c906108c
SS
14339}
14340
14341static char *
9b1c24c8 14342read_direct_string (bfd *abfd, gdb_byte *buf, unsigned int *bytes_read_ptr)
c906108c
SS
14343{
14344 /* If the size of a host char is 8 bits, we can return a pointer
14345 to the string, otherwise we have to copy the string to a buffer
14346 allocated on the temporary obstack. */
4bdf3d34 14347 gdb_assert (HOST_CHAR_BIT == 8);
c906108c
SS
14348 if (*buf == '\0')
14349 {
14350 *bytes_read_ptr = 1;
14351 return NULL;
14352 }
fe1b8b76
JB
14353 *bytes_read_ptr = strlen ((char *) buf) + 1;
14354 return (char *) buf;
4bdf3d34
JJ
14355}
14356
14357static char *
cf2c3c16 14358read_indirect_string_at_offset (bfd *abfd, LONGEST str_offset)
4bdf3d34 14359{
be391dca 14360 dwarf2_read_section (dwarf2_per_objfile->objfile, &dwarf2_per_objfile->str);
dce234bc 14361 if (dwarf2_per_objfile->str.buffer == NULL)
cf2c3c16
TT
14362 error (_("DW_FORM_strp used without .debug_str section [in module %s]"),
14363 bfd_get_filename (abfd));
dce234bc 14364 if (str_offset >= dwarf2_per_objfile->str.size)
cf2c3c16
TT
14365 error (_("DW_FORM_strp pointing outside of "
14366 ".debug_str section [in module %s]"),
14367 bfd_get_filename (abfd));
4bdf3d34 14368 gdb_assert (HOST_CHAR_BIT == 8);
dce234bc 14369 if (dwarf2_per_objfile->str.buffer[str_offset] == '\0')
4bdf3d34 14370 return NULL;
dce234bc 14371 return (char *) (dwarf2_per_objfile->str.buffer + str_offset);
c906108c
SS
14372}
14373
36586728
TT
14374/* Read a string at offset STR_OFFSET in the .debug_str section from
14375 the .dwz file DWZ. Throw an error if the offset is too large. If
14376 the string consists of a single NUL byte, return NULL; otherwise
14377 return a pointer to the string. */
14378
14379static char *
14380read_indirect_string_from_dwz (struct dwz_file *dwz, LONGEST str_offset)
14381{
14382 dwarf2_read_section (dwarf2_per_objfile->objfile, &dwz->str);
14383
14384 if (dwz->str.buffer == NULL)
14385 error (_("DW_FORM_GNU_strp_alt used without .debug_str "
14386 "section [in module %s]"),
14387 bfd_get_filename (dwz->dwz_bfd));
14388 if (str_offset >= dwz->str.size)
14389 error (_("DW_FORM_GNU_strp_alt pointing outside of "
14390 ".debug_str section [in module %s]"),
14391 bfd_get_filename (dwz->dwz_bfd));
14392 gdb_assert (HOST_CHAR_BIT == 8);
14393 if (dwz->str.buffer[str_offset] == '\0')
14394 return NULL;
14395 return (char *) (dwz->str.buffer + str_offset);
14396}
14397
cf2c3c16
TT
14398static char *
14399read_indirect_string (bfd *abfd, gdb_byte *buf,
14400 const struct comp_unit_head *cu_header,
14401 unsigned int *bytes_read_ptr)
14402{
14403 LONGEST str_offset = read_offset (abfd, buf, cu_header, bytes_read_ptr);
14404
14405 return read_indirect_string_at_offset (abfd, str_offset);
14406}
14407
12df843f 14408static ULONGEST
fe1b8b76 14409read_unsigned_leb128 (bfd *abfd, gdb_byte *buf, unsigned int *bytes_read_ptr)
c906108c 14410{
12df843f 14411 ULONGEST result;
ce5d95e1 14412 unsigned int num_read;
c906108c
SS
14413 int i, shift;
14414 unsigned char byte;
14415
14416 result = 0;
14417 shift = 0;
14418 num_read = 0;
14419 i = 0;
14420 while (1)
14421 {
fe1b8b76 14422 byte = bfd_get_8 (abfd, buf);
c906108c
SS
14423 buf++;
14424 num_read++;
12df843f 14425 result |= ((ULONGEST) (byte & 127) << shift);
c906108c
SS
14426 if ((byte & 128) == 0)
14427 {
14428 break;
14429 }
14430 shift += 7;
14431 }
14432 *bytes_read_ptr = num_read;
14433 return result;
14434}
14435
12df843f 14436static LONGEST
fe1b8b76 14437read_signed_leb128 (bfd *abfd, gdb_byte *buf, unsigned int *bytes_read_ptr)
c906108c 14438{
12df843f 14439 LONGEST result;
77e0b926 14440 int i, shift, num_read;
c906108c
SS
14441 unsigned char byte;
14442
14443 result = 0;
14444 shift = 0;
c906108c
SS
14445 num_read = 0;
14446 i = 0;
14447 while (1)
14448 {
fe1b8b76 14449 byte = bfd_get_8 (abfd, buf);
c906108c
SS
14450 buf++;
14451 num_read++;
12df843f 14452 result |= ((LONGEST) (byte & 127) << shift);
c906108c
SS
14453 shift += 7;
14454 if ((byte & 128) == 0)
14455 {
14456 break;
14457 }
14458 }
77e0b926 14459 if ((shift < 8 * sizeof (result)) && (byte & 0x40))
12df843f 14460 result |= -(((LONGEST) 1) << shift);
c906108c
SS
14461 *bytes_read_ptr = num_read;
14462 return result;
14463}
14464
3019eac3
DE
14465/* Given index ADDR_INDEX in .debug_addr, fetch the value.
14466 ADDR_BASE is the DW_AT_GNU_addr_base attribute or zero.
14467 ADDR_SIZE is the size of addresses from the CU header. */
14468
14469static CORE_ADDR
14470read_addr_index_1 (unsigned int addr_index, ULONGEST addr_base, int addr_size)
14471{
14472 struct objfile *objfile = dwarf2_per_objfile->objfile;
14473 bfd *abfd = objfile->obfd;
14474 const gdb_byte *info_ptr;
14475
14476 dwarf2_read_section (objfile, &dwarf2_per_objfile->addr);
14477 if (dwarf2_per_objfile->addr.buffer == NULL)
14478 error (_("DW_FORM_addr_index used without .debug_addr section [in module %s]"),
14479 objfile->name);
14480 if (addr_base + addr_index * addr_size >= dwarf2_per_objfile->addr.size)
14481 error (_("DW_FORM_addr_index pointing outside of "
14482 ".debug_addr section [in module %s]"),
14483 objfile->name);
14484 info_ptr = (dwarf2_per_objfile->addr.buffer
14485 + addr_base + addr_index * addr_size);
14486 if (addr_size == 4)
14487 return bfd_get_32 (abfd, info_ptr);
14488 else
14489 return bfd_get_64 (abfd, info_ptr);
14490}
14491
14492/* Given index ADDR_INDEX in .debug_addr, fetch the value. */
14493
14494static CORE_ADDR
14495read_addr_index (struct dwarf2_cu *cu, unsigned int addr_index)
14496{
14497 return read_addr_index_1 (addr_index, cu->addr_base, cu->header.addr_size);
14498}
14499
14500/* Given a pointer to an leb128 value, fetch the value from .debug_addr. */
14501
14502static CORE_ADDR
14503read_addr_index_from_leb128 (struct dwarf2_cu *cu, gdb_byte *info_ptr,
14504 unsigned int *bytes_read)
14505{
14506 bfd *abfd = cu->objfile->obfd;
14507 unsigned int addr_index = read_unsigned_leb128 (abfd, info_ptr, bytes_read);
14508
14509 return read_addr_index (cu, addr_index);
14510}
14511
14512/* Data structure to pass results from dwarf2_read_addr_index_reader
14513 back to dwarf2_read_addr_index. */
14514
14515struct dwarf2_read_addr_index_data
14516{
14517 ULONGEST addr_base;
14518 int addr_size;
14519};
14520
14521/* die_reader_func for dwarf2_read_addr_index. */
14522
14523static void
14524dwarf2_read_addr_index_reader (const struct die_reader_specs *reader,
14525 gdb_byte *info_ptr,
14526 struct die_info *comp_unit_die,
14527 int has_children,
14528 void *data)
14529{
14530 struct dwarf2_cu *cu = reader->cu;
14531 struct dwarf2_read_addr_index_data *aidata =
14532 (struct dwarf2_read_addr_index_data *) data;
14533
14534 aidata->addr_base = cu->addr_base;
14535 aidata->addr_size = cu->header.addr_size;
14536}
14537
14538/* Given an index in .debug_addr, fetch the value.
14539 NOTE: This can be called during dwarf expression evaluation,
14540 long after the debug information has been read, and thus per_cu->cu
14541 may no longer exist. */
14542
14543CORE_ADDR
14544dwarf2_read_addr_index (struct dwarf2_per_cu_data *per_cu,
14545 unsigned int addr_index)
14546{
14547 struct objfile *objfile = per_cu->objfile;
14548 struct dwarf2_cu *cu = per_cu->cu;
14549 ULONGEST addr_base;
14550 int addr_size;
14551
14552 /* This is intended to be called from outside this file. */
14553 dw2_setup (objfile);
14554
14555 /* We need addr_base and addr_size.
14556 If we don't have PER_CU->cu, we have to get it.
14557 Nasty, but the alternative is storing the needed info in PER_CU,
14558 which at this point doesn't seem justified: it's not clear how frequently
14559 it would get used and it would increase the size of every PER_CU.
14560 Entry points like dwarf2_per_cu_addr_size do a similar thing
14561 so we're not in uncharted territory here.
14562 Alas we need to be a bit more complicated as addr_base is contained
14563 in the DIE.
14564
14565 We don't need to read the entire CU(/TU).
14566 We just need the header and top level die.
a1b64ce1 14567
3019eac3 14568 IWBN to use the aging mechanism to let us lazily later discard the CU.
a1b64ce1 14569 For now we skip this optimization. */
3019eac3
DE
14570
14571 if (cu != NULL)
14572 {
14573 addr_base = cu->addr_base;
14574 addr_size = cu->header.addr_size;
14575 }
14576 else
14577 {
14578 struct dwarf2_read_addr_index_data aidata;
14579
a1b64ce1
DE
14580 /* Note: We can't use init_cutu_and_read_dies_simple here,
14581 we need addr_base. */
14582 init_cutu_and_read_dies (per_cu, NULL, 0, 0,
14583 dwarf2_read_addr_index_reader, &aidata);
3019eac3
DE
14584 addr_base = aidata.addr_base;
14585 addr_size = aidata.addr_size;
14586 }
14587
14588 return read_addr_index_1 (addr_index, addr_base, addr_size);
14589}
14590
14591/* Given a DW_AT_str_index, fetch the string. */
14592
14593static char *
14594read_str_index (const struct die_reader_specs *reader,
14595 struct dwarf2_cu *cu, ULONGEST str_index)
14596{
14597 struct objfile *objfile = dwarf2_per_objfile->objfile;
14598 const char *dwo_name = objfile->name;
14599 bfd *abfd = objfile->obfd;
14600 struct dwo_sections *sections = &reader->dwo_file->sections;
14601 gdb_byte *info_ptr;
14602 ULONGEST str_offset;
14603
14604 dwarf2_read_section (objfile, &sections->str);
14605 dwarf2_read_section (objfile, &sections->str_offsets);
14606 if (sections->str.buffer == NULL)
14607 error (_("DW_FORM_str_index used without .debug_str.dwo section"
14608 " in CU at offset 0x%lx [in module %s]"),
14609 (long) cu->header.offset.sect_off, dwo_name);
14610 if (sections->str_offsets.buffer == NULL)
14611 error (_("DW_FORM_str_index used without .debug_str_offsets.dwo section"
14612 " in CU at offset 0x%lx [in module %s]"),
14613 (long) cu->header.offset.sect_off, dwo_name);
14614 if (str_index * cu->header.offset_size >= sections->str_offsets.size)
14615 error (_("DW_FORM_str_index pointing outside of .debug_str_offsets.dwo"
14616 " section in CU at offset 0x%lx [in module %s]"),
14617 (long) cu->header.offset.sect_off, dwo_name);
14618 info_ptr = (sections->str_offsets.buffer
14619 + str_index * cu->header.offset_size);
14620 if (cu->header.offset_size == 4)
14621 str_offset = bfd_get_32 (abfd, info_ptr);
14622 else
14623 str_offset = bfd_get_64 (abfd, info_ptr);
14624 if (str_offset >= sections->str.size)
14625 error (_("Offset from DW_FORM_str_index pointing outside of"
14626 " .debug_str.dwo section in CU at offset 0x%lx [in module %s]"),
14627 (long) cu->header.offset.sect_off, dwo_name);
14628 return (char *) (sections->str.buffer + str_offset);
14629}
14630
3019eac3
DE
14631/* Return the length of an LEB128 number in BUF. */
14632
14633static int
14634leb128_size (const gdb_byte *buf)
14635{
14636 const gdb_byte *begin = buf;
14637 gdb_byte byte;
14638
14639 while (1)
14640 {
14641 byte = *buf++;
14642 if ((byte & 128) == 0)
14643 return buf - begin;
14644 }
14645}
14646
c906108c 14647static void
e142c38c 14648set_cu_language (unsigned int lang, struct dwarf2_cu *cu)
c906108c
SS
14649{
14650 switch (lang)
14651 {
14652 case DW_LANG_C89:
76bee0cc 14653 case DW_LANG_C99:
c906108c 14654 case DW_LANG_C:
e142c38c 14655 cu->language = language_c;
c906108c
SS
14656 break;
14657 case DW_LANG_C_plus_plus:
e142c38c 14658 cu->language = language_cplus;
c906108c 14659 break;
6aecb9c2
JB
14660 case DW_LANG_D:
14661 cu->language = language_d;
14662 break;
c906108c
SS
14663 case DW_LANG_Fortran77:
14664 case DW_LANG_Fortran90:
b21b22e0 14665 case DW_LANG_Fortran95:
e142c38c 14666 cu->language = language_fortran;
c906108c 14667 break;
a766d390
DE
14668 case DW_LANG_Go:
14669 cu->language = language_go;
14670 break;
c906108c 14671 case DW_LANG_Mips_Assembler:
e142c38c 14672 cu->language = language_asm;
c906108c 14673 break;
bebd888e 14674 case DW_LANG_Java:
e142c38c 14675 cu->language = language_java;
bebd888e 14676 break;
c906108c 14677 case DW_LANG_Ada83:
8aaf0b47 14678 case DW_LANG_Ada95:
bc5f45f8
JB
14679 cu->language = language_ada;
14680 break;
72019c9c
GM
14681 case DW_LANG_Modula2:
14682 cu->language = language_m2;
14683 break;
fe8e67fd
PM
14684 case DW_LANG_Pascal83:
14685 cu->language = language_pascal;
14686 break;
22566fbd
DJ
14687 case DW_LANG_ObjC:
14688 cu->language = language_objc;
14689 break;
c906108c
SS
14690 case DW_LANG_Cobol74:
14691 case DW_LANG_Cobol85:
c906108c 14692 default:
e142c38c 14693 cu->language = language_minimal;
c906108c
SS
14694 break;
14695 }
e142c38c 14696 cu->language_defn = language_def (cu->language);
c906108c
SS
14697}
14698
14699/* Return the named attribute or NULL if not there. */
14700
14701static struct attribute *
e142c38c 14702dwarf2_attr (struct die_info *die, unsigned int name, struct dwarf2_cu *cu)
c906108c 14703{
a48e046c 14704 for (;;)
c906108c 14705 {
a48e046c
TT
14706 unsigned int i;
14707 struct attribute *spec = NULL;
14708
14709 for (i = 0; i < die->num_attrs; ++i)
14710 {
14711 if (die->attrs[i].name == name)
14712 return &die->attrs[i];
14713 if (die->attrs[i].name == DW_AT_specification
14714 || die->attrs[i].name == DW_AT_abstract_origin)
14715 spec = &die->attrs[i];
14716 }
14717
14718 if (!spec)
14719 break;
c906108c 14720
f2f0e013 14721 die = follow_die_ref (die, spec, &cu);
f2f0e013 14722 }
c5aa993b 14723
c906108c
SS
14724 return NULL;
14725}
14726
348e048f
DE
14727/* Return the named attribute or NULL if not there,
14728 but do not follow DW_AT_specification, etc.
14729 This is for use in contexts where we're reading .debug_types dies.
14730 Following DW_AT_specification, DW_AT_abstract_origin will take us
14731 back up the chain, and we want to go down. */
14732
14733static struct attribute *
45e58e77 14734dwarf2_attr_no_follow (struct die_info *die, unsigned int name)
348e048f
DE
14735{
14736 unsigned int i;
14737
14738 for (i = 0; i < die->num_attrs; ++i)
14739 if (die->attrs[i].name == name)
14740 return &die->attrs[i];
14741
14742 return NULL;
14743}
14744
05cf31d1
JB
14745/* Return non-zero iff the attribute NAME is defined for the given DIE,
14746 and holds a non-zero value. This function should only be used for
2dc7f7b3 14747 DW_FORM_flag or DW_FORM_flag_present attributes. */
05cf31d1
JB
14748
14749static int
14750dwarf2_flag_true_p (struct die_info *die, unsigned name, struct dwarf2_cu *cu)
14751{
14752 struct attribute *attr = dwarf2_attr (die, name, cu);
14753
14754 return (attr && DW_UNSND (attr));
14755}
14756
3ca72b44 14757static int
e142c38c 14758die_is_declaration (struct die_info *die, struct dwarf2_cu *cu)
3ca72b44 14759{
05cf31d1
JB
14760 /* A DIE is a declaration if it has a DW_AT_declaration attribute
14761 which value is non-zero. However, we have to be careful with
14762 DIEs having a DW_AT_specification attribute, because dwarf2_attr()
14763 (via dwarf2_flag_true_p) follows this attribute. So we may
14764 end up accidently finding a declaration attribute that belongs
14765 to a different DIE referenced by the specification attribute,
14766 even though the given DIE does not have a declaration attribute. */
14767 return (dwarf2_flag_true_p (die, DW_AT_declaration, cu)
14768 && dwarf2_attr (die, DW_AT_specification, cu) == NULL);
3ca72b44
AC
14769}
14770
63d06c5c 14771/* Return the die giving the specification for DIE, if there is
f2f0e013 14772 one. *SPEC_CU is the CU containing DIE on input, and the CU
edb3359d
DJ
14773 containing the return value on output. If there is no
14774 specification, but there is an abstract origin, that is
14775 returned. */
63d06c5c
DC
14776
14777static struct die_info *
f2f0e013 14778die_specification (struct die_info *die, struct dwarf2_cu **spec_cu)
63d06c5c 14779{
f2f0e013
DJ
14780 struct attribute *spec_attr = dwarf2_attr (die, DW_AT_specification,
14781 *spec_cu);
63d06c5c 14782
edb3359d
DJ
14783 if (spec_attr == NULL)
14784 spec_attr = dwarf2_attr (die, DW_AT_abstract_origin, *spec_cu);
14785
63d06c5c
DC
14786 if (spec_attr == NULL)
14787 return NULL;
14788 else
f2f0e013 14789 return follow_die_ref (die, spec_attr, spec_cu);
63d06c5c 14790}
c906108c 14791
debd256d 14792/* Free the line_header structure *LH, and any arrays and strings it
ae2de4f8
DE
14793 refers to.
14794 NOTE: This is also used as a "cleanup" function. */
14795
debd256d
JB
14796static void
14797free_line_header (struct line_header *lh)
14798{
14799 if (lh->standard_opcode_lengths)
a8bc7b56 14800 xfree (lh->standard_opcode_lengths);
debd256d
JB
14801
14802 /* Remember that all the lh->file_names[i].name pointers are
14803 pointers into debug_line_buffer, and don't need to be freed. */
14804 if (lh->file_names)
a8bc7b56 14805 xfree (lh->file_names);
debd256d
JB
14806
14807 /* Similarly for the include directory names. */
14808 if (lh->include_dirs)
a8bc7b56 14809 xfree (lh->include_dirs);
debd256d 14810
a8bc7b56 14811 xfree (lh);
debd256d
JB
14812}
14813
debd256d 14814/* Add an entry to LH's include directory table. */
ae2de4f8 14815
debd256d
JB
14816static void
14817add_include_dir (struct line_header *lh, char *include_dir)
c906108c 14818{
debd256d
JB
14819 /* Grow the array if necessary. */
14820 if (lh->include_dirs_size == 0)
c5aa993b 14821 {
debd256d
JB
14822 lh->include_dirs_size = 1; /* for testing */
14823 lh->include_dirs = xmalloc (lh->include_dirs_size
14824 * sizeof (*lh->include_dirs));
14825 }
14826 else if (lh->num_include_dirs >= lh->include_dirs_size)
14827 {
14828 lh->include_dirs_size *= 2;
14829 lh->include_dirs = xrealloc (lh->include_dirs,
14830 (lh->include_dirs_size
14831 * sizeof (*lh->include_dirs)));
c5aa993b 14832 }
c906108c 14833
debd256d
JB
14834 lh->include_dirs[lh->num_include_dirs++] = include_dir;
14835}
6e70227d 14836
debd256d 14837/* Add an entry to LH's file name table. */
ae2de4f8 14838
debd256d
JB
14839static void
14840add_file_name (struct line_header *lh,
14841 char *name,
14842 unsigned int dir_index,
14843 unsigned int mod_time,
14844 unsigned int length)
14845{
14846 struct file_entry *fe;
14847
14848 /* Grow the array if necessary. */
14849 if (lh->file_names_size == 0)
14850 {
14851 lh->file_names_size = 1; /* for testing */
14852 lh->file_names = xmalloc (lh->file_names_size
14853 * sizeof (*lh->file_names));
14854 }
14855 else if (lh->num_file_names >= lh->file_names_size)
14856 {
14857 lh->file_names_size *= 2;
14858 lh->file_names = xrealloc (lh->file_names,
14859 (lh->file_names_size
14860 * sizeof (*lh->file_names)));
14861 }
14862
14863 fe = &lh->file_names[lh->num_file_names++];
14864 fe->name = name;
14865 fe->dir_index = dir_index;
14866 fe->mod_time = mod_time;
14867 fe->length = length;
aaa75496 14868 fe->included_p = 0;
cb1df416 14869 fe->symtab = NULL;
debd256d 14870}
6e70227d 14871
36586728
TT
14872/* A convenience function to find the proper .debug_line section for a
14873 CU. */
14874
14875static struct dwarf2_section_info *
14876get_debug_line_section (struct dwarf2_cu *cu)
14877{
14878 struct dwarf2_section_info *section;
14879
14880 /* For TUs in DWO files, the DW_AT_stmt_list attribute lives in the
14881 DWO file. */
14882 if (cu->dwo_unit && cu->per_cu->is_debug_types)
14883 section = &cu->dwo_unit->dwo_file->sections.line;
14884 else if (cu->per_cu->is_dwz)
14885 {
14886 struct dwz_file *dwz = dwarf2_get_dwz_file ();
14887
14888 section = &dwz->line;
14889 }
14890 else
14891 section = &dwarf2_per_objfile->line;
14892
14893 return section;
14894}
14895
debd256d 14896/* Read the statement program header starting at OFFSET in
3019eac3 14897 .debug_line, or .debug_line.dwo. Return a pointer
6502dd73 14898 to a struct line_header, allocated using xmalloc.
debd256d
JB
14899
14900 NOTE: the strings in the include directory and file name tables of
3019eac3
DE
14901 the returned object point into the dwarf line section buffer,
14902 and must not be freed. */
ae2de4f8 14903
debd256d 14904static struct line_header *
3019eac3 14905dwarf_decode_line_header (unsigned int offset, struct dwarf2_cu *cu)
debd256d
JB
14906{
14907 struct cleanup *back_to;
14908 struct line_header *lh;
fe1b8b76 14909 gdb_byte *line_ptr;
c764a876 14910 unsigned int bytes_read, offset_size;
debd256d
JB
14911 int i;
14912 char *cur_dir, *cur_file;
3019eac3
DE
14913 struct dwarf2_section_info *section;
14914 bfd *abfd;
14915
36586728 14916 section = get_debug_line_section (cu);
3019eac3
DE
14917 dwarf2_read_section (dwarf2_per_objfile->objfile, section);
14918 if (section->buffer == NULL)
debd256d 14919 {
3019eac3
DE
14920 if (cu->dwo_unit && cu->per_cu->is_debug_types)
14921 complaint (&symfile_complaints, _("missing .debug_line.dwo section"));
14922 else
14923 complaint (&symfile_complaints, _("missing .debug_line section"));
debd256d
JB
14924 return 0;
14925 }
14926
fceca515
DE
14927 /* We can't do this until we know the section is non-empty.
14928 Only then do we know we have such a section. */
14929 abfd = section->asection->owner;
14930
a738430d
MK
14931 /* Make sure that at least there's room for the total_length field.
14932 That could be 12 bytes long, but we're just going to fudge that. */
3019eac3 14933 if (offset + 4 >= section->size)
debd256d 14934 {
4d3c2250 14935 dwarf2_statement_list_fits_in_line_number_section_complaint ();
debd256d
JB
14936 return 0;
14937 }
14938
14939 lh = xmalloc (sizeof (*lh));
14940 memset (lh, 0, sizeof (*lh));
14941 back_to = make_cleanup ((make_cleanup_ftype *) free_line_header,
14942 (void *) lh);
14943
3019eac3 14944 line_ptr = section->buffer + offset;
debd256d 14945
a738430d 14946 /* Read in the header. */
6e70227d 14947 lh->total_length =
c764a876
DE
14948 read_checked_initial_length_and_offset (abfd, line_ptr, &cu->header,
14949 &bytes_read, &offset_size);
debd256d 14950 line_ptr += bytes_read;
3019eac3 14951 if (line_ptr + lh->total_length > (section->buffer + section->size))
debd256d 14952 {
4d3c2250 14953 dwarf2_statement_list_fits_in_line_number_section_complaint ();
debd256d
JB
14954 return 0;
14955 }
14956 lh->statement_program_end = line_ptr + lh->total_length;
14957 lh->version = read_2_bytes (abfd, line_ptr);
14958 line_ptr += 2;
c764a876
DE
14959 lh->header_length = read_offset_1 (abfd, line_ptr, offset_size);
14960 line_ptr += offset_size;
debd256d
JB
14961 lh->minimum_instruction_length = read_1_byte (abfd, line_ptr);
14962 line_ptr += 1;
2dc7f7b3
TT
14963 if (lh->version >= 4)
14964 {
14965 lh->maximum_ops_per_instruction = read_1_byte (abfd, line_ptr);
14966 line_ptr += 1;
14967 }
14968 else
14969 lh->maximum_ops_per_instruction = 1;
14970
14971 if (lh->maximum_ops_per_instruction == 0)
14972 {
14973 lh->maximum_ops_per_instruction = 1;
14974 complaint (&symfile_complaints,
3e43a32a
MS
14975 _("invalid maximum_ops_per_instruction "
14976 "in `.debug_line' section"));
2dc7f7b3
TT
14977 }
14978
debd256d
JB
14979 lh->default_is_stmt = read_1_byte (abfd, line_ptr);
14980 line_ptr += 1;
14981 lh->line_base = read_1_signed_byte (abfd, line_ptr);
14982 line_ptr += 1;
14983 lh->line_range = read_1_byte (abfd, line_ptr);
14984 line_ptr += 1;
14985 lh->opcode_base = read_1_byte (abfd, line_ptr);
14986 line_ptr += 1;
14987 lh->standard_opcode_lengths
fe1b8b76 14988 = xmalloc (lh->opcode_base * sizeof (lh->standard_opcode_lengths[0]));
debd256d
JB
14989
14990 lh->standard_opcode_lengths[0] = 1; /* This should never be used anyway. */
14991 for (i = 1; i < lh->opcode_base; ++i)
14992 {
14993 lh->standard_opcode_lengths[i] = read_1_byte (abfd, line_ptr);
14994 line_ptr += 1;
14995 }
14996
a738430d 14997 /* Read directory table. */
9b1c24c8 14998 while ((cur_dir = read_direct_string (abfd, line_ptr, &bytes_read)) != NULL)
debd256d
JB
14999 {
15000 line_ptr += bytes_read;
15001 add_include_dir (lh, cur_dir);
15002 }
15003 line_ptr += bytes_read;
15004
a738430d 15005 /* Read file name table. */
9b1c24c8 15006 while ((cur_file = read_direct_string (abfd, line_ptr, &bytes_read)) != NULL)
debd256d
JB
15007 {
15008 unsigned int dir_index, mod_time, length;
15009
15010 line_ptr += bytes_read;
15011 dir_index = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
15012 line_ptr += bytes_read;
15013 mod_time = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
15014 line_ptr += bytes_read;
15015 length = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
15016 line_ptr += bytes_read;
15017
15018 add_file_name (lh, cur_file, dir_index, mod_time, length);
15019 }
15020 line_ptr += bytes_read;
6e70227d 15021 lh->statement_program_start = line_ptr;
debd256d 15022
3019eac3 15023 if (line_ptr > (section->buffer + section->size))
4d3c2250 15024 complaint (&symfile_complaints,
3e43a32a
MS
15025 _("line number info header doesn't "
15026 "fit in `.debug_line' section"));
debd256d
JB
15027
15028 discard_cleanups (back_to);
15029 return lh;
15030}
c906108c 15031
c6da4cef
DE
15032/* Subroutine of dwarf_decode_lines to simplify it.
15033 Return the file name of the psymtab for included file FILE_INDEX
15034 in line header LH of PST.
15035 COMP_DIR is the compilation directory (DW_AT_comp_dir) or NULL if unknown.
15036 If space for the result is malloc'd, it will be freed by a cleanup.
15037 Returns NULL if FILE_INDEX should be ignored, i.e., it is pst->filename. */
15038
15039static char *
15040psymtab_include_file_name (const struct line_header *lh, int file_index,
15041 const struct partial_symtab *pst,
15042 const char *comp_dir)
15043{
15044 const struct file_entry fe = lh->file_names [file_index];
15045 char *include_name = fe.name;
15046 char *include_name_to_compare = include_name;
15047 char *dir_name = NULL;
72b9f47f
TT
15048 const char *pst_filename;
15049 char *copied_name = NULL;
c6da4cef
DE
15050 int file_is_pst;
15051
15052 if (fe.dir_index)
15053 dir_name = lh->include_dirs[fe.dir_index - 1];
15054
15055 if (!IS_ABSOLUTE_PATH (include_name)
15056 && (dir_name != NULL || comp_dir != NULL))
15057 {
15058 /* Avoid creating a duplicate psymtab for PST.
15059 We do this by comparing INCLUDE_NAME and PST_FILENAME.
15060 Before we do the comparison, however, we need to account
15061 for DIR_NAME and COMP_DIR.
15062 First prepend dir_name (if non-NULL). If we still don't
15063 have an absolute path prepend comp_dir (if non-NULL).
15064 However, the directory we record in the include-file's
15065 psymtab does not contain COMP_DIR (to match the
15066 corresponding symtab(s)).
15067
15068 Example:
15069
15070 bash$ cd /tmp
15071 bash$ gcc -g ./hello.c
15072 include_name = "hello.c"
15073 dir_name = "."
15074 DW_AT_comp_dir = comp_dir = "/tmp"
15075 DW_AT_name = "./hello.c" */
15076
15077 if (dir_name != NULL)
15078 {
15079 include_name = concat (dir_name, SLASH_STRING,
15080 include_name, (char *)NULL);
15081 include_name_to_compare = include_name;
15082 make_cleanup (xfree, include_name);
15083 }
15084 if (!IS_ABSOLUTE_PATH (include_name) && comp_dir != NULL)
15085 {
15086 include_name_to_compare = concat (comp_dir, SLASH_STRING,
15087 include_name, (char *)NULL);
15088 }
15089 }
15090
15091 pst_filename = pst->filename;
15092 if (!IS_ABSOLUTE_PATH (pst_filename) && pst->dirname != NULL)
15093 {
72b9f47f
TT
15094 copied_name = concat (pst->dirname, SLASH_STRING,
15095 pst_filename, (char *)NULL);
15096 pst_filename = copied_name;
c6da4cef
DE
15097 }
15098
1e3fad37 15099 file_is_pst = FILENAME_CMP (include_name_to_compare, pst_filename) == 0;
c6da4cef
DE
15100
15101 if (include_name_to_compare != include_name)
15102 xfree (include_name_to_compare);
72b9f47f
TT
15103 if (copied_name != NULL)
15104 xfree (copied_name);
c6da4cef
DE
15105
15106 if (file_is_pst)
15107 return NULL;
15108 return include_name;
15109}
15110
c91513d8
PP
15111/* Ignore this record_line request. */
15112
15113static void
15114noop_record_line (struct subfile *subfile, int line, CORE_ADDR pc)
15115{
15116 return;
15117}
15118
f3f5162e
DE
15119/* Subroutine of dwarf_decode_lines to simplify it.
15120 Process the line number information in LH. */
debd256d 15121
c906108c 15122static void
f3f5162e
DE
15123dwarf_decode_lines_1 (struct line_header *lh, const char *comp_dir,
15124 struct dwarf2_cu *cu, struct partial_symtab *pst)
c906108c 15125{
a8c50c1f 15126 gdb_byte *line_ptr, *extended_end;
fe1b8b76 15127 gdb_byte *line_end;
a8c50c1f 15128 unsigned int bytes_read, extended_len;
c906108c 15129 unsigned char op_code, extended_op, adj_opcode;
e142c38c
DJ
15130 CORE_ADDR baseaddr;
15131 struct objfile *objfile = cu->objfile;
f3f5162e 15132 bfd *abfd = objfile->obfd;
fbf65064 15133 struct gdbarch *gdbarch = get_objfile_arch (objfile);
aaa75496 15134 const int decode_for_pst_p = (pst != NULL);
f3f5162e 15135 struct subfile *last_subfile = NULL;
c91513d8
PP
15136 void (*p_record_line) (struct subfile *subfile, int line, CORE_ADDR pc)
15137 = record_line;
e142c38c
DJ
15138
15139 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
c906108c 15140
debd256d
JB
15141 line_ptr = lh->statement_program_start;
15142 line_end = lh->statement_program_end;
c906108c
SS
15143
15144 /* Read the statement sequences until there's nothing left. */
15145 while (line_ptr < line_end)
15146 {
15147 /* state machine registers */
15148 CORE_ADDR address = 0;
15149 unsigned int file = 1;
15150 unsigned int line = 1;
15151 unsigned int column = 0;
debd256d 15152 int is_stmt = lh->default_is_stmt;
c906108c
SS
15153 int basic_block = 0;
15154 int end_sequence = 0;
fbf65064 15155 CORE_ADDR addr;
2dc7f7b3 15156 unsigned char op_index = 0;
c906108c 15157
aaa75496 15158 if (!decode_for_pst_p && lh->num_file_names >= file)
c906108c 15159 {
aaa75496 15160 /* Start a subfile for the current file of the state machine. */
debd256d
JB
15161 /* lh->include_dirs and lh->file_names are 0-based, but the
15162 directory and file name numbers in the statement program
15163 are 1-based. */
15164 struct file_entry *fe = &lh->file_names[file - 1];
4f1520fb 15165 char *dir = NULL;
a738430d 15166
debd256d
JB
15167 if (fe->dir_index)
15168 dir = lh->include_dirs[fe->dir_index - 1];
4f1520fb
FR
15169
15170 dwarf2_start_subfile (fe->name, dir, comp_dir);
c906108c
SS
15171 }
15172
a738430d 15173 /* Decode the table. */
c5aa993b 15174 while (!end_sequence)
c906108c
SS
15175 {
15176 op_code = read_1_byte (abfd, line_ptr);
15177 line_ptr += 1;
59205f5a
JB
15178 if (line_ptr > line_end)
15179 {
15180 dwarf2_debug_line_missing_end_sequence_complaint ();
15181 break;
15182 }
9aa1fe7e 15183
debd256d 15184 if (op_code >= lh->opcode_base)
6e70227d 15185 {
a738430d 15186 /* Special operand. */
debd256d 15187 adj_opcode = op_code - lh->opcode_base;
2dc7f7b3
TT
15188 address += (((op_index + (adj_opcode / lh->line_range))
15189 / lh->maximum_ops_per_instruction)
15190 * lh->minimum_instruction_length);
15191 op_index = ((op_index + (adj_opcode / lh->line_range))
15192 % lh->maximum_ops_per_instruction);
debd256d 15193 line += lh->line_base + (adj_opcode % lh->line_range);
59205f5a 15194 if (lh->num_file_names < file || file == 0)
25e43795 15195 dwarf2_debug_line_missing_file_complaint ();
2dc7f7b3
TT
15196 /* For now we ignore lines not starting on an
15197 instruction boundary. */
15198 else if (op_index == 0)
25e43795
DJ
15199 {
15200 lh->file_names[file - 1].included_p = 1;
ca5f395d 15201 if (!decode_for_pst_p && is_stmt)
fbf65064
UW
15202 {
15203 if (last_subfile != current_subfile)
15204 {
15205 addr = gdbarch_addr_bits_remove (gdbarch, address);
15206 if (last_subfile)
c91513d8 15207 (*p_record_line) (last_subfile, 0, addr);
fbf65064
UW
15208 last_subfile = current_subfile;
15209 }
25e43795 15210 /* Append row to matrix using current values. */
7019d805 15211 addr = gdbarch_addr_bits_remove (gdbarch, address);
c91513d8 15212 (*p_record_line) (current_subfile, line, addr);
366da635 15213 }
25e43795 15214 }
ca5f395d 15215 basic_block = 0;
9aa1fe7e
GK
15216 }
15217 else switch (op_code)
c906108c
SS
15218 {
15219 case DW_LNS_extended_op:
3e43a32a
MS
15220 extended_len = read_unsigned_leb128 (abfd, line_ptr,
15221 &bytes_read);
473b7be6 15222 line_ptr += bytes_read;
a8c50c1f 15223 extended_end = line_ptr + extended_len;
c906108c
SS
15224 extended_op = read_1_byte (abfd, line_ptr);
15225 line_ptr += 1;
15226 switch (extended_op)
15227 {
15228 case DW_LNE_end_sequence:
c91513d8 15229 p_record_line = record_line;
c906108c 15230 end_sequence = 1;
c906108c
SS
15231 break;
15232 case DW_LNE_set_address:
e7c27a73 15233 address = read_address (abfd, line_ptr, cu, &bytes_read);
c91513d8
PP
15234
15235 if (address == 0 && !dwarf2_per_objfile->has_section_at_zero)
15236 {
15237 /* This line table is for a function which has been
15238 GCd by the linker. Ignore it. PR gdb/12528 */
15239
15240 long line_offset
36586728 15241 = line_ptr - get_debug_line_section (cu)->buffer;
c91513d8
PP
15242
15243 complaint (&symfile_complaints,
15244 _(".debug_line address at offset 0x%lx is 0 "
15245 "[in module %s]"),
bb5ed363 15246 line_offset, objfile->name);
c91513d8
PP
15247 p_record_line = noop_record_line;
15248 }
15249
2dc7f7b3 15250 op_index = 0;
107d2387
AC
15251 line_ptr += bytes_read;
15252 address += baseaddr;
c906108c
SS
15253 break;
15254 case DW_LNE_define_file:
debd256d
JB
15255 {
15256 char *cur_file;
15257 unsigned int dir_index, mod_time, length;
6e70227d 15258
3e43a32a
MS
15259 cur_file = read_direct_string (abfd, line_ptr,
15260 &bytes_read);
debd256d
JB
15261 line_ptr += bytes_read;
15262 dir_index =
15263 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
15264 line_ptr += bytes_read;
15265 mod_time =
15266 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
15267 line_ptr += bytes_read;
15268 length =
15269 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
15270 line_ptr += bytes_read;
15271 add_file_name (lh, cur_file, dir_index, mod_time, length);
15272 }
c906108c 15273 break;
d0c6ba3d
CC
15274 case DW_LNE_set_discriminator:
15275 /* The discriminator is not interesting to the debugger;
15276 just ignore it. */
15277 line_ptr = extended_end;
15278 break;
c906108c 15279 default:
4d3c2250 15280 complaint (&symfile_complaints,
e2e0b3e5 15281 _("mangled .debug_line section"));
debd256d 15282 return;
c906108c 15283 }
a8c50c1f
DJ
15284 /* Make sure that we parsed the extended op correctly. If e.g.
15285 we expected a different address size than the producer used,
15286 we may have read the wrong number of bytes. */
15287 if (line_ptr != extended_end)
15288 {
15289 complaint (&symfile_complaints,
15290 _("mangled .debug_line section"));
15291 return;
15292 }
c906108c
SS
15293 break;
15294 case DW_LNS_copy:
59205f5a 15295 if (lh->num_file_names < file || file == 0)
25e43795
DJ
15296 dwarf2_debug_line_missing_file_complaint ();
15297 else
366da635 15298 {
25e43795 15299 lh->file_names[file - 1].included_p = 1;
ca5f395d 15300 if (!decode_for_pst_p && is_stmt)
fbf65064
UW
15301 {
15302 if (last_subfile != current_subfile)
15303 {
15304 addr = gdbarch_addr_bits_remove (gdbarch, address);
15305 if (last_subfile)
c91513d8 15306 (*p_record_line) (last_subfile, 0, addr);
fbf65064
UW
15307 last_subfile = current_subfile;
15308 }
7019d805 15309 addr = gdbarch_addr_bits_remove (gdbarch, address);
c91513d8 15310 (*p_record_line) (current_subfile, line, addr);
fbf65064 15311 }
366da635 15312 }
c906108c
SS
15313 basic_block = 0;
15314 break;
15315 case DW_LNS_advance_pc:
2dc7f7b3
TT
15316 {
15317 CORE_ADDR adjust
15318 = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
15319
15320 address += (((op_index + adjust)
15321 / lh->maximum_ops_per_instruction)
15322 * lh->minimum_instruction_length);
15323 op_index = ((op_index + adjust)
15324 % lh->maximum_ops_per_instruction);
15325 line_ptr += bytes_read;
15326 }
c906108c
SS
15327 break;
15328 case DW_LNS_advance_line:
15329 line += read_signed_leb128 (abfd, line_ptr, &bytes_read);
15330 line_ptr += bytes_read;
15331 break;
15332 case DW_LNS_set_file:
debd256d 15333 {
a738430d
MK
15334 /* The arrays lh->include_dirs and lh->file_names are
15335 0-based, but the directory and file name numbers in
15336 the statement program are 1-based. */
debd256d 15337 struct file_entry *fe;
4f1520fb 15338 char *dir = NULL;
a738430d 15339
debd256d
JB
15340 file = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
15341 line_ptr += bytes_read;
59205f5a 15342 if (lh->num_file_names < file || file == 0)
25e43795
DJ
15343 dwarf2_debug_line_missing_file_complaint ();
15344 else
15345 {
15346 fe = &lh->file_names[file - 1];
15347 if (fe->dir_index)
15348 dir = lh->include_dirs[fe->dir_index - 1];
15349 if (!decode_for_pst_p)
15350 {
15351 last_subfile = current_subfile;
15352 dwarf2_start_subfile (fe->name, dir, comp_dir);
15353 }
15354 }
debd256d 15355 }
c906108c
SS
15356 break;
15357 case DW_LNS_set_column:
15358 column = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
15359 line_ptr += bytes_read;
15360 break;
15361 case DW_LNS_negate_stmt:
15362 is_stmt = (!is_stmt);
15363 break;
15364 case DW_LNS_set_basic_block:
15365 basic_block = 1;
15366 break;
c2c6d25f
JM
15367 /* Add to the address register of the state machine the
15368 address increment value corresponding to special opcode
a738430d
MK
15369 255. I.e., this value is scaled by the minimum
15370 instruction length since special opcode 255 would have
b021a221 15371 scaled the increment. */
c906108c 15372 case DW_LNS_const_add_pc:
2dc7f7b3
TT
15373 {
15374 CORE_ADDR adjust = (255 - lh->opcode_base) / lh->line_range;
15375
15376 address += (((op_index + adjust)
15377 / lh->maximum_ops_per_instruction)
15378 * lh->minimum_instruction_length);
15379 op_index = ((op_index + adjust)
15380 % lh->maximum_ops_per_instruction);
15381 }
c906108c
SS
15382 break;
15383 case DW_LNS_fixed_advance_pc:
15384 address += read_2_bytes (abfd, line_ptr);
2dc7f7b3 15385 op_index = 0;
c906108c
SS
15386 line_ptr += 2;
15387 break;
9aa1fe7e 15388 default:
a738430d
MK
15389 {
15390 /* Unknown standard opcode, ignore it. */
9aa1fe7e 15391 int i;
a738430d 15392
debd256d 15393 for (i = 0; i < lh->standard_opcode_lengths[op_code]; i++)
9aa1fe7e
GK
15394 {
15395 (void) read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
15396 line_ptr += bytes_read;
15397 }
15398 }
c906108c
SS
15399 }
15400 }
59205f5a
JB
15401 if (lh->num_file_names < file || file == 0)
15402 dwarf2_debug_line_missing_file_complaint ();
15403 else
15404 {
15405 lh->file_names[file - 1].included_p = 1;
15406 if (!decode_for_pst_p)
fbf65064
UW
15407 {
15408 addr = gdbarch_addr_bits_remove (gdbarch, address);
c91513d8 15409 (*p_record_line) (current_subfile, 0, addr);
fbf65064 15410 }
59205f5a 15411 }
c906108c 15412 }
f3f5162e
DE
15413}
15414
15415/* Decode the Line Number Program (LNP) for the given line_header
15416 structure and CU. The actual information extracted and the type
15417 of structures created from the LNP depends on the value of PST.
15418
15419 1. If PST is NULL, then this procedure uses the data from the program
15420 to create all necessary symbol tables, and their linetables.
15421
15422 2. If PST is not NULL, this procedure reads the program to determine
15423 the list of files included by the unit represented by PST, and
15424 builds all the associated partial symbol tables.
15425
15426 COMP_DIR is the compilation directory (DW_AT_comp_dir) or NULL if unknown.
15427 It is used for relative paths in the line table.
15428 NOTE: When processing partial symtabs (pst != NULL),
15429 comp_dir == pst->dirname.
15430
15431 NOTE: It is important that psymtabs have the same file name (via strcmp)
15432 as the corresponding symtab. Since COMP_DIR is not used in the name of the
15433 symtab we don't use it in the name of the psymtabs we create.
15434 E.g. expand_line_sal requires this when finding psymtabs to expand.
15435 A good testcase for this is mb-inline.exp. */
15436
15437static void
15438dwarf_decode_lines (struct line_header *lh, const char *comp_dir,
15439 struct dwarf2_cu *cu, struct partial_symtab *pst,
15440 int want_line_info)
15441{
15442 struct objfile *objfile = cu->objfile;
15443 const int decode_for_pst_p = (pst != NULL);
15444 struct subfile *first_subfile = current_subfile;
15445
15446 if (want_line_info)
15447 dwarf_decode_lines_1 (lh, comp_dir, cu, pst);
aaa75496
JB
15448
15449 if (decode_for_pst_p)
15450 {
15451 int file_index;
15452
15453 /* Now that we're done scanning the Line Header Program, we can
15454 create the psymtab of each included file. */
15455 for (file_index = 0; file_index < lh->num_file_names; file_index++)
15456 if (lh->file_names[file_index].included_p == 1)
15457 {
c6da4cef
DE
15458 char *include_name =
15459 psymtab_include_file_name (lh, file_index, pst, comp_dir);
15460 if (include_name != NULL)
aaa75496
JB
15461 dwarf2_create_include_psymtab (include_name, pst, objfile);
15462 }
15463 }
cb1df416
DJ
15464 else
15465 {
15466 /* Make sure a symtab is created for every file, even files
15467 which contain only variables (i.e. no code with associated
15468 line numbers). */
cb1df416 15469 int i;
cb1df416
DJ
15470
15471 for (i = 0; i < lh->num_file_names; i++)
15472 {
15473 char *dir = NULL;
f3f5162e 15474 struct file_entry *fe;
9a619af0 15475
cb1df416
DJ
15476 fe = &lh->file_names[i];
15477 if (fe->dir_index)
15478 dir = lh->include_dirs[fe->dir_index - 1];
15479 dwarf2_start_subfile (fe->name, dir, comp_dir);
15480
15481 /* Skip the main file; we don't need it, and it must be
15482 allocated last, so that it will show up before the
15483 non-primary symtabs in the objfile's symtab list. */
15484 if (current_subfile == first_subfile)
15485 continue;
15486
15487 if (current_subfile->symtab == NULL)
15488 current_subfile->symtab = allocate_symtab (current_subfile->name,
bb5ed363 15489 objfile);
cb1df416
DJ
15490 fe->symtab = current_subfile->symtab;
15491 }
15492 }
c906108c
SS
15493}
15494
15495/* Start a subfile for DWARF. FILENAME is the name of the file and
15496 DIRNAME the name of the source directory which contains FILENAME
4f1520fb
FR
15497 or NULL if not known. COMP_DIR is the compilation directory for the
15498 linetable's compilation unit or NULL if not known.
c906108c
SS
15499 This routine tries to keep line numbers from identical absolute and
15500 relative file names in a common subfile.
15501
15502 Using the `list' example from the GDB testsuite, which resides in
15503 /srcdir and compiling it with Irix6.2 cc in /compdir using a filename
15504 of /srcdir/list0.c yields the following debugging information for list0.c:
15505
c5aa993b
JM
15506 DW_AT_name: /srcdir/list0.c
15507 DW_AT_comp_dir: /compdir
357e46e7 15508 files.files[0].name: list0.h
c5aa993b 15509 files.files[0].dir: /srcdir
357e46e7 15510 files.files[1].name: list0.c
c5aa993b 15511 files.files[1].dir: /srcdir
c906108c
SS
15512
15513 The line number information for list0.c has to end up in a single
4f1520fb
FR
15514 subfile, so that `break /srcdir/list0.c:1' works as expected.
15515 start_subfile will ensure that this happens provided that we pass the
15516 concatenation of files.files[1].dir and files.files[1].name as the
15517 subfile's name. */
c906108c
SS
15518
15519static void
3e43a32a
MS
15520dwarf2_start_subfile (char *filename, const char *dirname,
15521 const char *comp_dir)
c906108c 15522{
4f1520fb
FR
15523 char *fullname;
15524
15525 /* While reading the DIEs, we call start_symtab(DW_AT_name, DW_AT_comp_dir).
15526 `start_symtab' will always pass the contents of DW_AT_comp_dir as
15527 second argument to start_subfile. To be consistent, we do the
15528 same here. In order not to lose the line information directory,
15529 we concatenate it to the filename when it makes sense.
15530 Note that the Dwarf3 standard says (speaking of filenames in line
15531 information): ``The directory index is ignored for file names
15532 that represent full path names''. Thus ignoring dirname in the
15533 `else' branch below isn't an issue. */
c906108c 15534
d5166ae1 15535 if (!IS_ABSOLUTE_PATH (filename) && dirname != NULL)
4f1520fb
FR
15536 fullname = concat (dirname, SLASH_STRING, filename, (char *)NULL);
15537 else
15538 fullname = filename;
c906108c 15539
4f1520fb
FR
15540 start_subfile (fullname, comp_dir);
15541
15542 if (fullname != filename)
15543 xfree (fullname);
c906108c
SS
15544}
15545
f4dc4d17
DE
15546/* Start a symtab for DWARF.
15547 NAME, COMP_DIR, LOW_PC are passed to start_symtab. */
15548
15549static void
15550dwarf2_start_symtab (struct dwarf2_cu *cu,
15551 char *name, char *comp_dir, CORE_ADDR low_pc)
15552{
15553 start_symtab (name, comp_dir, low_pc);
15554 record_debugformat ("DWARF 2");
15555 record_producer (cu->producer);
15556
15557 /* We assume that we're processing GCC output. */
15558 processing_gcc_compilation = 2;
15559
15560 processing_has_namespace_info = 0;
15561}
15562
4c2df51b
DJ
15563static void
15564var_decode_location (struct attribute *attr, struct symbol *sym,
e7c27a73 15565 struct dwarf2_cu *cu)
4c2df51b 15566{
e7c27a73
DJ
15567 struct objfile *objfile = cu->objfile;
15568 struct comp_unit_head *cu_header = &cu->header;
15569
4c2df51b
DJ
15570 /* NOTE drow/2003-01-30: There used to be a comment and some special
15571 code here to turn a symbol with DW_AT_external and a
15572 SYMBOL_VALUE_ADDRESS of 0 into a LOC_UNRESOLVED symbol. This was
15573 necessary for platforms (maybe Alpha, certainly PowerPC GNU/Linux
15574 with some versions of binutils) where shared libraries could have
15575 relocations against symbols in their debug information - the
15576 minimal symbol would have the right address, but the debug info
15577 would not. It's no longer necessary, because we will explicitly
15578 apply relocations when we read in the debug information now. */
15579
15580 /* A DW_AT_location attribute with no contents indicates that a
15581 variable has been optimized away. */
15582 if (attr_form_is_block (attr) && DW_BLOCK (attr)->size == 0)
15583 {
15584 SYMBOL_CLASS (sym) = LOC_OPTIMIZED_OUT;
15585 return;
15586 }
15587
15588 /* Handle one degenerate form of location expression specially, to
15589 preserve GDB's previous behavior when section offsets are
3019eac3
DE
15590 specified. If this is just a DW_OP_addr or DW_OP_GNU_addr_index
15591 then mark this symbol as LOC_STATIC. */
4c2df51b
DJ
15592
15593 if (attr_form_is_block (attr)
3019eac3
DE
15594 && ((DW_BLOCK (attr)->data[0] == DW_OP_addr
15595 && DW_BLOCK (attr)->size == 1 + cu_header->addr_size)
15596 || (DW_BLOCK (attr)->data[0] == DW_OP_GNU_addr_index
15597 && (DW_BLOCK (attr)->size
15598 == 1 + leb128_size (&DW_BLOCK (attr)->data[1])))))
4c2df51b 15599 {
891d2f0b 15600 unsigned int dummy;
4c2df51b 15601
3019eac3
DE
15602 if (DW_BLOCK (attr)->data[0] == DW_OP_addr)
15603 SYMBOL_VALUE_ADDRESS (sym) =
15604 read_address (objfile->obfd, DW_BLOCK (attr)->data + 1, cu, &dummy);
15605 else
15606 SYMBOL_VALUE_ADDRESS (sym) =
15607 read_addr_index_from_leb128 (cu, DW_BLOCK (attr)->data + 1, &dummy);
907fc202 15608 SYMBOL_CLASS (sym) = LOC_STATIC;
4c2df51b
DJ
15609 fixup_symbol_section (sym, objfile);
15610 SYMBOL_VALUE_ADDRESS (sym) += ANOFFSET (objfile->section_offsets,
15611 SYMBOL_SECTION (sym));
4c2df51b
DJ
15612 return;
15613 }
15614
15615 /* NOTE drow/2002-01-30: It might be worthwhile to have a static
15616 expression evaluator, and use LOC_COMPUTED only when necessary
15617 (i.e. when the value of a register or memory location is
15618 referenced, or a thread-local block, etc.). Then again, it might
15619 not be worthwhile. I'm assuming that it isn't unless performance
15620 or memory numbers show me otherwise. */
15621
e7c27a73 15622 dwarf2_symbol_mark_computed (attr, sym, cu);
4c2df51b 15623 SYMBOL_CLASS (sym) = LOC_COMPUTED;
8be455d7
JK
15624
15625 if (SYMBOL_COMPUTED_OPS (sym) == &dwarf2_loclist_funcs)
15626 cu->has_loclist = 1;
4c2df51b
DJ
15627}
15628
c906108c
SS
15629/* Given a pointer to a DWARF information entry, figure out if we need
15630 to make a symbol table entry for it, and if so, create a new entry
15631 and return a pointer to it.
15632 If TYPE is NULL, determine symbol type from the die, otherwise
34eaf542
TT
15633 used the passed type.
15634 If SPACE is not NULL, use it to hold the new symbol. If it is
15635 NULL, allocate a new symbol on the objfile's obstack. */
c906108c
SS
15636
15637static struct symbol *
34eaf542
TT
15638new_symbol_full (struct die_info *die, struct type *type, struct dwarf2_cu *cu,
15639 struct symbol *space)
c906108c 15640{
e7c27a73 15641 struct objfile *objfile = cu->objfile;
c906108c
SS
15642 struct symbol *sym = NULL;
15643 char *name;
15644 struct attribute *attr = NULL;
15645 struct attribute *attr2 = NULL;
e142c38c 15646 CORE_ADDR baseaddr;
e37fd15a
SW
15647 struct pending **list_to_add = NULL;
15648
edb3359d 15649 int inlined_func = (die->tag == DW_TAG_inlined_subroutine);
e142c38c
DJ
15650
15651 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
c906108c 15652
94af9270 15653 name = dwarf2_name (die, cu);
c906108c
SS
15654 if (name)
15655 {
94af9270 15656 const char *linkagename;
34eaf542 15657 int suppress_add = 0;
94af9270 15658
34eaf542
TT
15659 if (space)
15660 sym = space;
15661 else
15662 sym = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct symbol);
c906108c 15663 OBJSTAT (objfile, n_syms++);
2de7ced7
DJ
15664
15665 /* Cache this symbol's name and the name's demangled form (if any). */
33e5013e 15666 SYMBOL_SET_LANGUAGE (sym, cu->language);
94af9270
KS
15667 linkagename = dwarf2_physname (name, die, cu);
15668 SYMBOL_SET_NAMES (sym, linkagename, strlen (linkagename), 0, objfile);
c906108c 15669
f55ee35c
JK
15670 /* Fortran does not have mangling standard and the mangling does differ
15671 between gfortran, iFort etc. */
15672 if (cu->language == language_fortran
b250c185 15673 && symbol_get_demangled_name (&(sym->ginfo)) == NULL)
29df156d
SW
15674 symbol_set_demangled_name (&(sym->ginfo),
15675 (char *) dwarf2_full_name (name, die, cu),
15676 NULL);
f55ee35c 15677
c906108c 15678 /* Default assumptions.
c5aa993b 15679 Use the passed type or decode it from the die. */
176620f1 15680 SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
875dc2fc 15681 SYMBOL_CLASS (sym) = LOC_OPTIMIZED_OUT;
c906108c
SS
15682 if (type != NULL)
15683 SYMBOL_TYPE (sym) = type;
15684 else
e7c27a73 15685 SYMBOL_TYPE (sym) = die_type (die, cu);
edb3359d
DJ
15686 attr = dwarf2_attr (die,
15687 inlined_func ? DW_AT_call_line : DW_AT_decl_line,
15688 cu);
c906108c
SS
15689 if (attr)
15690 {
15691 SYMBOL_LINE (sym) = DW_UNSND (attr);
15692 }
cb1df416 15693
edb3359d
DJ
15694 attr = dwarf2_attr (die,
15695 inlined_func ? DW_AT_call_file : DW_AT_decl_file,
15696 cu);
cb1df416
DJ
15697 if (attr)
15698 {
15699 int file_index = DW_UNSND (attr);
9a619af0 15700
cb1df416
DJ
15701 if (cu->line_header == NULL
15702 || file_index > cu->line_header->num_file_names)
15703 complaint (&symfile_complaints,
15704 _("file index out of range"));
1c3d648d 15705 else if (file_index > 0)
cb1df416
DJ
15706 {
15707 struct file_entry *fe;
9a619af0 15708
cb1df416
DJ
15709 fe = &cu->line_header->file_names[file_index - 1];
15710 SYMBOL_SYMTAB (sym) = fe->symtab;
15711 }
15712 }
15713
c906108c
SS
15714 switch (die->tag)
15715 {
15716 case DW_TAG_label:
e142c38c 15717 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
c906108c
SS
15718 if (attr)
15719 {
15720 SYMBOL_VALUE_ADDRESS (sym) = DW_ADDR (attr) + baseaddr;
15721 }
0f5238ed
TT
15722 SYMBOL_TYPE (sym) = objfile_type (objfile)->builtin_core_addr;
15723 SYMBOL_DOMAIN (sym) = LABEL_DOMAIN;
c906108c 15724 SYMBOL_CLASS (sym) = LOC_LABEL;
0f5238ed 15725 add_symbol_to_list (sym, cu->list_in_scope);
c906108c
SS
15726 break;
15727 case DW_TAG_subprogram:
15728 /* SYMBOL_BLOCK_VALUE (sym) will be filled in later by
15729 finish_block. */
15730 SYMBOL_CLASS (sym) = LOC_BLOCK;
e142c38c 15731 attr2 = dwarf2_attr (die, DW_AT_external, cu);
2cfa0c8d
JB
15732 if ((attr2 && (DW_UNSND (attr2) != 0))
15733 || cu->language == language_ada)
c906108c 15734 {
2cfa0c8d
JB
15735 /* Subprograms marked external are stored as a global symbol.
15736 Ada subprograms, whether marked external or not, are always
15737 stored as a global symbol, because we want to be able to
15738 access them globally. For instance, we want to be able
15739 to break on a nested subprogram without having to
15740 specify the context. */
e37fd15a 15741 list_to_add = &global_symbols;
c906108c
SS
15742 }
15743 else
15744 {
e37fd15a 15745 list_to_add = cu->list_in_scope;
c906108c
SS
15746 }
15747 break;
edb3359d
DJ
15748 case DW_TAG_inlined_subroutine:
15749 /* SYMBOL_BLOCK_VALUE (sym) will be filled in later by
15750 finish_block. */
15751 SYMBOL_CLASS (sym) = LOC_BLOCK;
15752 SYMBOL_INLINED (sym) = 1;
481860b3 15753 list_to_add = cu->list_in_scope;
edb3359d 15754 break;
34eaf542
TT
15755 case DW_TAG_template_value_param:
15756 suppress_add = 1;
15757 /* Fall through. */
72929c62 15758 case DW_TAG_constant:
c906108c 15759 case DW_TAG_variable:
254e6b9e 15760 case DW_TAG_member:
0963b4bd
MS
15761 /* Compilation with minimal debug info may result in
15762 variables with missing type entries. Change the
15763 misleading `void' type to something sensible. */
c906108c 15764 if (TYPE_CODE (SYMBOL_TYPE (sym)) == TYPE_CODE_VOID)
64c50499 15765 SYMBOL_TYPE (sym)
46bf5051 15766 = objfile_type (objfile)->nodebug_data_symbol;
64c50499 15767
e142c38c 15768 attr = dwarf2_attr (die, DW_AT_const_value, cu);
254e6b9e
DE
15769 /* In the case of DW_TAG_member, we should only be called for
15770 static const members. */
15771 if (die->tag == DW_TAG_member)
15772 {
3863f96c
DE
15773 /* dwarf2_add_field uses die_is_declaration,
15774 so we do the same. */
254e6b9e
DE
15775 gdb_assert (die_is_declaration (die, cu));
15776 gdb_assert (attr);
15777 }
c906108c
SS
15778 if (attr)
15779 {
e7c27a73 15780 dwarf2_const_value (attr, sym, cu);
e142c38c 15781 attr2 = dwarf2_attr (die, DW_AT_external, cu);
e37fd15a 15782 if (!suppress_add)
34eaf542
TT
15783 {
15784 if (attr2 && (DW_UNSND (attr2) != 0))
e37fd15a 15785 list_to_add = &global_symbols;
34eaf542 15786 else
e37fd15a 15787 list_to_add = cu->list_in_scope;
34eaf542 15788 }
c906108c
SS
15789 break;
15790 }
e142c38c 15791 attr = dwarf2_attr (die, DW_AT_location, cu);
c906108c
SS
15792 if (attr)
15793 {
e7c27a73 15794 var_decode_location (attr, sym, cu);
e142c38c 15795 attr2 = dwarf2_attr (die, DW_AT_external, cu);
4357ac6c
TT
15796
15797 /* Fortran explicitly imports any global symbols to the local
15798 scope by DW_TAG_common_block. */
15799 if (cu->language == language_fortran && die->parent
15800 && die->parent->tag == DW_TAG_common_block)
15801 attr2 = NULL;
15802
caac4577
JG
15803 if (SYMBOL_CLASS (sym) == LOC_STATIC
15804 && SYMBOL_VALUE_ADDRESS (sym) == 0
15805 && !dwarf2_per_objfile->has_section_at_zero)
15806 {
15807 /* When a static variable is eliminated by the linker,
15808 the corresponding debug information is not stripped
15809 out, but the variable address is set to null;
15810 do not add such variables into symbol table. */
15811 }
15812 else if (attr2 && (DW_UNSND (attr2) != 0))
1c809c68 15813 {
f55ee35c
JK
15814 /* Workaround gfortran PR debug/40040 - it uses
15815 DW_AT_location for variables in -fPIC libraries which may
15816 get overriden by other libraries/executable and get
15817 a different address. Resolve it by the minimal symbol
15818 which may come from inferior's executable using copy
15819 relocation. Make this workaround only for gfortran as for
15820 other compilers GDB cannot guess the minimal symbol
15821 Fortran mangling kind. */
15822 if (cu->language == language_fortran && die->parent
15823 && die->parent->tag == DW_TAG_module
15824 && cu->producer
15825 && strncmp (cu->producer, "GNU Fortran ", 12) == 0)
15826 SYMBOL_CLASS (sym) = LOC_UNRESOLVED;
15827
1c809c68
TT
15828 /* A variable with DW_AT_external is never static,
15829 but it may be block-scoped. */
15830 list_to_add = (cu->list_in_scope == &file_symbols
15831 ? &global_symbols : cu->list_in_scope);
1c809c68 15832 }
c906108c 15833 else
e37fd15a 15834 list_to_add = cu->list_in_scope;
c906108c
SS
15835 }
15836 else
15837 {
15838 /* We do not know the address of this symbol.
c5aa993b
JM
15839 If it is an external symbol and we have type information
15840 for it, enter the symbol as a LOC_UNRESOLVED symbol.
15841 The address of the variable will then be determined from
15842 the minimal symbol table whenever the variable is
15843 referenced. */
e142c38c 15844 attr2 = dwarf2_attr (die, DW_AT_external, cu);
0971de02
TT
15845
15846 /* Fortran explicitly imports any global symbols to the local
15847 scope by DW_TAG_common_block. */
15848 if (cu->language == language_fortran && die->parent
15849 && die->parent->tag == DW_TAG_common_block)
15850 {
15851 /* SYMBOL_CLASS doesn't matter here because
15852 read_common_block is going to reset it. */
15853 if (!suppress_add)
15854 list_to_add = cu->list_in_scope;
15855 }
15856 else if (attr2 && (DW_UNSND (attr2) != 0)
15857 && dwarf2_attr (die, DW_AT_type, cu) != NULL)
c906108c 15858 {
0fe7935b
DJ
15859 /* A variable with DW_AT_external is never static, but it
15860 may be block-scoped. */
15861 list_to_add = (cu->list_in_scope == &file_symbols
15862 ? &global_symbols : cu->list_in_scope);
15863
c906108c 15864 SYMBOL_CLASS (sym) = LOC_UNRESOLVED;
c906108c 15865 }
442ddf59
JK
15866 else if (!die_is_declaration (die, cu))
15867 {
15868 /* Use the default LOC_OPTIMIZED_OUT class. */
15869 gdb_assert (SYMBOL_CLASS (sym) == LOC_OPTIMIZED_OUT);
e37fd15a
SW
15870 if (!suppress_add)
15871 list_to_add = cu->list_in_scope;
442ddf59 15872 }
c906108c
SS
15873 }
15874 break;
15875 case DW_TAG_formal_parameter:
edb3359d
DJ
15876 /* If we are inside a function, mark this as an argument. If
15877 not, we might be looking at an argument to an inlined function
15878 when we do not have enough information to show inlined frames;
15879 pretend it's a local variable in that case so that the user can
15880 still see it. */
15881 if (context_stack_depth > 0
15882 && context_stack[context_stack_depth - 1].name != NULL)
15883 SYMBOL_IS_ARGUMENT (sym) = 1;
e142c38c 15884 attr = dwarf2_attr (die, DW_AT_location, cu);
c906108c
SS
15885 if (attr)
15886 {
e7c27a73 15887 var_decode_location (attr, sym, cu);
c906108c 15888 }
e142c38c 15889 attr = dwarf2_attr (die, DW_AT_const_value, cu);
c906108c
SS
15890 if (attr)
15891 {
e7c27a73 15892 dwarf2_const_value (attr, sym, cu);
c906108c 15893 }
f346a30d 15894
e37fd15a 15895 list_to_add = cu->list_in_scope;
c906108c
SS
15896 break;
15897 case DW_TAG_unspecified_parameters:
15898 /* From varargs functions; gdb doesn't seem to have any
15899 interest in this information, so just ignore it for now.
15900 (FIXME?) */
15901 break;
34eaf542
TT
15902 case DW_TAG_template_type_param:
15903 suppress_add = 1;
15904 /* Fall through. */
c906108c 15905 case DW_TAG_class_type:
680b30c7 15906 case DW_TAG_interface_type:
c906108c
SS
15907 case DW_TAG_structure_type:
15908 case DW_TAG_union_type:
72019c9c 15909 case DW_TAG_set_type:
c906108c
SS
15910 case DW_TAG_enumeration_type:
15911 SYMBOL_CLASS (sym) = LOC_TYPEDEF;
176620f1 15912 SYMBOL_DOMAIN (sym) = STRUCT_DOMAIN;
c906108c 15913
63d06c5c 15914 {
987504bb 15915 /* NOTE: carlton/2003-11-10: C++ and Java class symbols shouldn't
63d06c5c
DC
15916 really ever be static objects: otherwise, if you try
15917 to, say, break of a class's method and you're in a file
15918 which doesn't mention that class, it won't work unless
15919 the check for all static symbols in lookup_symbol_aux
15920 saves you. See the OtherFileClass tests in
15921 gdb.c++/namespace.exp. */
15922
e37fd15a 15923 if (!suppress_add)
34eaf542 15924 {
34eaf542
TT
15925 list_to_add = (cu->list_in_scope == &file_symbols
15926 && (cu->language == language_cplus
15927 || cu->language == language_java)
15928 ? &global_symbols : cu->list_in_scope);
63d06c5c 15929
64382290
TT
15930 /* The semantics of C++ state that "struct foo {
15931 ... }" also defines a typedef for "foo". A Java
15932 class declaration also defines a typedef for the
15933 class. */
15934 if (cu->language == language_cplus
15935 || cu->language == language_java
15936 || cu->language == language_ada)
15937 {
15938 /* The symbol's name is already allocated along
15939 with this objfile, so we don't need to
15940 duplicate it for the type. */
15941 if (TYPE_NAME (SYMBOL_TYPE (sym)) == 0)
15942 TYPE_NAME (SYMBOL_TYPE (sym)) = SYMBOL_SEARCH_NAME (sym);
15943 }
63d06c5c
DC
15944 }
15945 }
c906108c
SS
15946 break;
15947 case DW_TAG_typedef:
63d06c5c
DC
15948 SYMBOL_CLASS (sym) = LOC_TYPEDEF;
15949 SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
e37fd15a 15950 list_to_add = cu->list_in_scope;
63d06c5c 15951 break;
c906108c 15952 case DW_TAG_base_type:
a02abb62 15953 case DW_TAG_subrange_type:
c906108c 15954 SYMBOL_CLASS (sym) = LOC_TYPEDEF;
176620f1 15955 SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
e37fd15a 15956 list_to_add = cu->list_in_scope;
c906108c
SS
15957 break;
15958 case DW_TAG_enumerator:
e142c38c 15959 attr = dwarf2_attr (die, DW_AT_const_value, cu);
c906108c
SS
15960 if (attr)
15961 {
e7c27a73 15962 dwarf2_const_value (attr, sym, cu);
c906108c 15963 }
63d06c5c
DC
15964 {
15965 /* NOTE: carlton/2003-11-10: See comment above in the
15966 DW_TAG_class_type, etc. block. */
15967
e142c38c 15968 list_to_add = (cu->list_in_scope == &file_symbols
987504bb
JJ
15969 && (cu->language == language_cplus
15970 || cu->language == language_java)
e142c38c 15971 ? &global_symbols : cu->list_in_scope);
63d06c5c 15972 }
c906108c 15973 break;
5c4e30ca
DC
15974 case DW_TAG_namespace:
15975 SYMBOL_CLASS (sym) = LOC_TYPEDEF;
e37fd15a 15976 list_to_add = &global_symbols;
5c4e30ca 15977 break;
4357ac6c
TT
15978 case DW_TAG_common_block:
15979 SYMBOL_CLASS (sym) = LOC_STATIC;
15980 SYMBOL_DOMAIN (sym) = COMMON_BLOCK_DOMAIN;
15981 add_symbol_to_list (sym, cu->list_in_scope);
15982 break;
c906108c
SS
15983 default:
15984 /* Not a tag we recognize. Hopefully we aren't processing
15985 trash data, but since we must specifically ignore things
15986 we don't recognize, there is nothing else we should do at
0963b4bd 15987 this point. */
e2e0b3e5 15988 complaint (&symfile_complaints, _("unsupported tag: '%s'"),
4d3c2250 15989 dwarf_tag_name (die->tag));
c906108c
SS
15990 break;
15991 }
df8a16a1 15992
e37fd15a
SW
15993 if (suppress_add)
15994 {
15995 sym->hash_next = objfile->template_symbols;
15996 objfile->template_symbols = sym;
15997 list_to_add = NULL;
15998 }
15999
16000 if (list_to_add != NULL)
16001 add_symbol_to_list (sym, list_to_add);
16002
df8a16a1
DJ
16003 /* For the benefit of old versions of GCC, check for anonymous
16004 namespaces based on the demangled name. */
16005 if (!processing_has_namespace_info
94af9270 16006 && cu->language == language_cplus)
a10964d1 16007 cp_scan_for_anonymous_namespaces (sym, objfile);
c906108c
SS
16008 }
16009 return (sym);
16010}
16011
34eaf542
TT
16012/* A wrapper for new_symbol_full that always allocates a new symbol. */
16013
16014static struct symbol *
16015new_symbol (struct die_info *die, struct type *type, struct dwarf2_cu *cu)
16016{
16017 return new_symbol_full (die, type, cu, NULL);
16018}
16019
98bfdba5
PA
16020/* Given an attr with a DW_FORM_dataN value in host byte order,
16021 zero-extend it as appropriate for the symbol's type. The DWARF
16022 standard (v4) is not entirely clear about the meaning of using
16023 DW_FORM_dataN for a constant with a signed type, where the type is
16024 wider than the data. The conclusion of a discussion on the DWARF
16025 list was that this is unspecified. We choose to always zero-extend
16026 because that is the interpretation long in use by GCC. */
c906108c 16027
98bfdba5
PA
16028static gdb_byte *
16029dwarf2_const_value_data (struct attribute *attr, struct type *type,
16030 const char *name, struct obstack *obstack,
12df843f 16031 struct dwarf2_cu *cu, LONGEST *value, int bits)
c906108c 16032{
e7c27a73 16033 struct objfile *objfile = cu->objfile;
e17a4113
UW
16034 enum bfd_endian byte_order = bfd_big_endian (objfile->obfd) ?
16035 BFD_ENDIAN_BIG : BFD_ENDIAN_LITTLE;
98bfdba5
PA
16036 LONGEST l = DW_UNSND (attr);
16037
16038 if (bits < sizeof (*value) * 8)
16039 {
16040 l &= ((LONGEST) 1 << bits) - 1;
16041 *value = l;
16042 }
16043 else if (bits == sizeof (*value) * 8)
16044 *value = l;
16045 else
16046 {
16047 gdb_byte *bytes = obstack_alloc (obstack, bits / 8);
16048 store_unsigned_integer (bytes, bits / 8, byte_order, l);
16049 return bytes;
16050 }
16051
16052 return NULL;
16053}
16054
16055/* Read a constant value from an attribute. Either set *VALUE, or if
16056 the value does not fit in *VALUE, set *BYTES - either already
16057 allocated on the objfile obstack, or newly allocated on OBSTACK,
16058 or, set *BATON, if we translated the constant to a location
16059 expression. */
16060
16061static void
16062dwarf2_const_value_attr (struct attribute *attr, struct type *type,
16063 const char *name, struct obstack *obstack,
16064 struct dwarf2_cu *cu,
12df843f 16065 LONGEST *value, gdb_byte **bytes,
98bfdba5
PA
16066 struct dwarf2_locexpr_baton **baton)
16067{
16068 struct objfile *objfile = cu->objfile;
16069 struct comp_unit_head *cu_header = &cu->header;
c906108c 16070 struct dwarf_block *blk;
98bfdba5
PA
16071 enum bfd_endian byte_order = (bfd_big_endian (objfile->obfd) ?
16072 BFD_ENDIAN_BIG : BFD_ENDIAN_LITTLE);
16073
16074 *value = 0;
16075 *bytes = NULL;
16076 *baton = NULL;
c906108c
SS
16077
16078 switch (attr->form)
16079 {
16080 case DW_FORM_addr:
3019eac3 16081 case DW_FORM_GNU_addr_index:
ac56253d 16082 {
ac56253d
TT
16083 gdb_byte *data;
16084
98bfdba5
PA
16085 if (TYPE_LENGTH (type) != cu_header->addr_size)
16086 dwarf2_const_value_length_mismatch_complaint (name,
ac56253d 16087 cu_header->addr_size,
98bfdba5 16088 TYPE_LENGTH (type));
ac56253d
TT
16089 /* Symbols of this form are reasonably rare, so we just
16090 piggyback on the existing location code rather than writing
16091 a new implementation of symbol_computed_ops. */
98bfdba5
PA
16092 *baton = obstack_alloc (&objfile->objfile_obstack,
16093 sizeof (struct dwarf2_locexpr_baton));
16094 (*baton)->per_cu = cu->per_cu;
16095 gdb_assert ((*baton)->per_cu);
ac56253d 16096
98bfdba5
PA
16097 (*baton)->size = 2 + cu_header->addr_size;
16098 data = obstack_alloc (&objfile->objfile_obstack, (*baton)->size);
16099 (*baton)->data = data;
ac56253d
TT
16100
16101 data[0] = DW_OP_addr;
16102 store_unsigned_integer (&data[1], cu_header->addr_size,
16103 byte_order, DW_ADDR (attr));
16104 data[cu_header->addr_size + 1] = DW_OP_stack_value;
ac56253d 16105 }
c906108c 16106 break;
4ac36638 16107 case DW_FORM_string:
93b5768b 16108 case DW_FORM_strp:
3019eac3 16109 case DW_FORM_GNU_str_index:
36586728 16110 case DW_FORM_GNU_strp_alt:
98bfdba5
PA
16111 /* DW_STRING is already allocated on the objfile obstack, point
16112 directly to it. */
16113 *bytes = (gdb_byte *) DW_STRING (attr);
93b5768b 16114 break;
c906108c
SS
16115 case DW_FORM_block1:
16116 case DW_FORM_block2:
16117 case DW_FORM_block4:
16118 case DW_FORM_block:
2dc7f7b3 16119 case DW_FORM_exprloc:
c906108c 16120 blk = DW_BLOCK (attr);
98bfdba5
PA
16121 if (TYPE_LENGTH (type) != blk->size)
16122 dwarf2_const_value_length_mismatch_complaint (name, blk->size,
16123 TYPE_LENGTH (type));
16124 *bytes = blk->data;
c906108c 16125 break;
2df3850c
JM
16126
16127 /* The DW_AT_const_value attributes are supposed to carry the
16128 symbol's value "represented as it would be on the target
16129 architecture." By the time we get here, it's already been
16130 converted to host endianness, so we just need to sign- or
16131 zero-extend it as appropriate. */
16132 case DW_FORM_data1:
3e43a32a
MS
16133 *bytes = dwarf2_const_value_data (attr, type, name,
16134 obstack, cu, value, 8);
2df3850c 16135 break;
c906108c 16136 case DW_FORM_data2:
3e43a32a
MS
16137 *bytes = dwarf2_const_value_data (attr, type, name,
16138 obstack, cu, value, 16);
2df3850c 16139 break;
c906108c 16140 case DW_FORM_data4:
3e43a32a
MS
16141 *bytes = dwarf2_const_value_data (attr, type, name,
16142 obstack, cu, value, 32);
2df3850c 16143 break;
c906108c 16144 case DW_FORM_data8:
3e43a32a
MS
16145 *bytes = dwarf2_const_value_data (attr, type, name,
16146 obstack, cu, value, 64);
2df3850c
JM
16147 break;
16148
c906108c 16149 case DW_FORM_sdata:
98bfdba5 16150 *value = DW_SND (attr);
2df3850c
JM
16151 break;
16152
c906108c 16153 case DW_FORM_udata:
98bfdba5 16154 *value = DW_UNSND (attr);
c906108c 16155 break;
2df3850c 16156
c906108c 16157 default:
4d3c2250 16158 complaint (&symfile_complaints,
e2e0b3e5 16159 _("unsupported const value attribute form: '%s'"),
4d3c2250 16160 dwarf_form_name (attr->form));
98bfdba5 16161 *value = 0;
c906108c
SS
16162 break;
16163 }
16164}
16165
2df3850c 16166
98bfdba5
PA
16167/* Copy constant value from an attribute to a symbol. */
16168
2df3850c 16169static void
98bfdba5
PA
16170dwarf2_const_value (struct attribute *attr, struct symbol *sym,
16171 struct dwarf2_cu *cu)
2df3850c 16172{
98bfdba5
PA
16173 struct objfile *objfile = cu->objfile;
16174 struct comp_unit_head *cu_header = &cu->header;
12df843f 16175 LONGEST value;
98bfdba5
PA
16176 gdb_byte *bytes;
16177 struct dwarf2_locexpr_baton *baton;
2df3850c 16178
98bfdba5
PA
16179 dwarf2_const_value_attr (attr, SYMBOL_TYPE (sym),
16180 SYMBOL_PRINT_NAME (sym),
16181 &objfile->objfile_obstack, cu,
16182 &value, &bytes, &baton);
2df3850c 16183
98bfdba5
PA
16184 if (baton != NULL)
16185 {
16186 SYMBOL_COMPUTED_OPS (sym) = &dwarf2_locexpr_funcs;
16187 SYMBOL_LOCATION_BATON (sym) = baton;
16188 SYMBOL_CLASS (sym) = LOC_COMPUTED;
16189 }
16190 else if (bytes != NULL)
16191 {
16192 SYMBOL_VALUE_BYTES (sym) = bytes;
16193 SYMBOL_CLASS (sym) = LOC_CONST_BYTES;
16194 }
16195 else
16196 {
16197 SYMBOL_VALUE (sym) = value;
16198 SYMBOL_CLASS (sym) = LOC_CONST;
16199 }
2df3850c
JM
16200}
16201
c906108c
SS
16202/* Return the type of the die in question using its DW_AT_type attribute. */
16203
16204static struct type *
e7c27a73 16205die_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 16206{
c906108c 16207 struct attribute *type_attr;
c906108c 16208
e142c38c 16209 type_attr = dwarf2_attr (die, DW_AT_type, cu);
c906108c
SS
16210 if (!type_attr)
16211 {
16212 /* A missing DW_AT_type represents a void type. */
46bf5051 16213 return objfile_type (cu->objfile)->builtin_void;
c906108c 16214 }
348e048f 16215
673bfd45 16216 return lookup_die_type (die, type_attr, cu);
c906108c
SS
16217}
16218
b4ba55a1
JB
16219/* True iff CU's producer generates GNAT Ada auxiliary information
16220 that allows to find parallel types through that information instead
16221 of having to do expensive parallel lookups by type name. */
16222
16223static int
16224need_gnat_info (struct dwarf2_cu *cu)
16225{
16226 /* FIXME: brobecker/2010-10-12: As of now, only the AdaCore version
16227 of GNAT produces this auxiliary information, without any indication
16228 that it is produced. Part of enhancing the FSF version of GNAT
16229 to produce that information will be to put in place an indicator
16230 that we can use in order to determine whether the descriptive type
16231 info is available or not. One suggestion that has been made is
16232 to use a new attribute, attached to the CU die. For now, assume
16233 that the descriptive type info is not available. */
16234 return 0;
16235}
16236
b4ba55a1
JB
16237/* Return the auxiliary type of the die in question using its
16238 DW_AT_GNAT_descriptive_type attribute. Returns NULL if the
16239 attribute is not present. */
16240
16241static struct type *
16242die_descriptive_type (struct die_info *die, struct dwarf2_cu *cu)
16243{
b4ba55a1 16244 struct attribute *type_attr;
b4ba55a1
JB
16245
16246 type_attr = dwarf2_attr (die, DW_AT_GNAT_descriptive_type, cu);
16247 if (!type_attr)
16248 return NULL;
16249
673bfd45 16250 return lookup_die_type (die, type_attr, cu);
b4ba55a1
JB
16251}
16252
16253/* If DIE has a descriptive_type attribute, then set the TYPE's
16254 descriptive type accordingly. */
16255
16256static void
16257set_descriptive_type (struct type *type, struct die_info *die,
16258 struct dwarf2_cu *cu)
16259{
16260 struct type *descriptive_type = die_descriptive_type (die, cu);
16261
16262 if (descriptive_type)
16263 {
16264 ALLOCATE_GNAT_AUX_TYPE (type);
16265 TYPE_DESCRIPTIVE_TYPE (type) = descriptive_type;
16266 }
16267}
16268
c906108c
SS
16269/* Return the containing type of the die in question using its
16270 DW_AT_containing_type attribute. */
16271
16272static struct type *
e7c27a73 16273die_containing_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 16274{
c906108c 16275 struct attribute *type_attr;
c906108c 16276
e142c38c 16277 type_attr = dwarf2_attr (die, DW_AT_containing_type, cu);
33ac96f0
JK
16278 if (!type_attr)
16279 error (_("Dwarf Error: Problem turning containing type into gdb type "
16280 "[in module %s]"), cu->objfile->name);
16281
673bfd45 16282 return lookup_die_type (die, type_attr, cu);
c906108c
SS
16283}
16284
673bfd45
DE
16285/* Look up the type of DIE in CU using its type attribute ATTR.
16286 If there is no type substitute an error marker. */
16287
c906108c 16288static struct type *
673bfd45
DE
16289lookup_die_type (struct die_info *die, struct attribute *attr,
16290 struct dwarf2_cu *cu)
c906108c 16291{
bb5ed363 16292 struct objfile *objfile = cu->objfile;
f792889a
DJ
16293 struct type *this_type;
16294
673bfd45
DE
16295 /* First see if we have it cached. */
16296
36586728
TT
16297 if (attr->form == DW_FORM_GNU_ref_alt)
16298 {
16299 struct dwarf2_per_cu_data *per_cu;
16300 sect_offset offset = dwarf2_get_ref_die_offset (attr);
16301
16302 per_cu = dwarf2_find_containing_comp_unit (offset, 1, cu->objfile);
16303 this_type = get_die_type_at_offset (offset, per_cu);
16304 }
16305 else if (is_ref_attr (attr))
673bfd45 16306 {
b64f50a1 16307 sect_offset offset = dwarf2_get_ref_die_offset (attr);
673bfd45
DE
16308
16309 this_type = get_die_type_at_offset (offset, cu->per_cu);
16310 }
55f1336d 16311 else if (attr->form == DW_FORM_ref_sig8)
673bfd45
DE
16312 {
16313 struct signatured_type *sig_type = DW_SIGNATURED_TYPE (attr);
673bfd45
DE
16314
16315 /* sig_type will be NULL if the signatured type is missing from
16316 the debug info. */
16317 if (sig_type == NULL)
16318 error (_("Dwarf Error: Cannot find signatured DIE referenced from DIE "
16319 "at 0x%x [in module %s]"),
b64f50a1 16320 die->offset.sect_off, objfile->name);
673bfd45 16321
3019eac3
DE
16322 gdb_assert (sig_type->per_cu.is_debug_types);
16323 /* If we haven't filled in type_offset_in_section yet, then we
16324 haven't read the type in yet. */
16325 this_type = NULL;
16326 if (sig_type->type_offset_in_section.sect_off != 0)
16327 {
16328 this_type =
16329 get_die_type_at_offset (sig_type->type_offset_in_section,
16330 &sig_type->per_cu);
16331 }
673bfd45
DE
16332 }
16333 else
16334 {
16335 dump_die_for_error (die);
16336 error (_("Dwarf Error: Bad type attribute %s [in module %s]"),
bb5ed363 16337 dwarf_attr_name (attr->name), objfile->name);
673bfd45
DE
16338 }
16339
16340 /* If not cached we need to read it in. */
16341
16342 if (this_type == NULL)
16343 {
16344 struct die_info *type_die;
16345 struct dwarf2_cu *type_cu = cu;
16346
16347 type_die = follow_die_ref_or_sig (die, attr, &type_cu);
3019eac3
DE
16348 /* If we found the type now, it's probably because the type came
16349 from an inter-CU reference and the type's CU got expanded before
16350 ours. */
16351 this_type = get_die_type (type_die, type_cu);
16352 if (this_type == NULL)
16353 this_type = read_type_die_1 (type_die, type_cu);
673bfd45
DE
16354 }
16355
16356 /* If we still don't have a type use an error marker. */
16357
16358 if (this_type == NULL)
c906108c 16359 {
b00fdb78
TT
16360 char *message, *saved;
16361
16362 /* read_type_die already issued a complaint. */
16363 message = xstrprintf (_("<unknown type in %s, CU 0x%x, DIE 0x%x>"),
bb5ed363 16364 objfile->name,
b64f50a1
JK
16365 cu->header.offset.sect_off,
16366 die->offset.sect_off);
bb5ed363 16367 saved = obstack_copy0 (&objfile->objfile_obstack,
b00fdb78
TT
16368 message, strlen (message));
16369 xfree (message);
16370
bb5ed363 16371 this_type = init_type (TYPE_CODE_ERROR, 0, 0, saved, objfile);
c906108c 16372 }
673bfd45 16373
f792889a 16374 return this_type;
c906108c
SS
16375}
16376
673bfd45
DE
16377/* Return the type in DIE, CU.
16378 Returns NULL for invalid types.
16379
16380 This first does a lookup in the appropriate type_hash table,
16381 and only reads the die in if necessary.
16382
16383 NOTE: This can be called when reading in partial or full symbols. */
16384
f792889a 16385static struct type *
e7c27a73 16386read_type_die (struct die_info *die, struct dwarf2_cu *cu)
c906108c 16387{
f792889a
DJ
16388 struct type *this_type;
16389
16390 this_type = get_die_type (die, cu);
16391 if (this_type)
16392 return this_type;
16393
673bfd45
DE
16394 return read_type_die_1 (die, cu);
16395}
16396
16397/* Read the type in DIE, CU.
16398 Returns NULL for invalid types. */
16399
16400static struct type *
16401read_type_die_1 (struct die_info *die, struct dwarf2_cu *cu)
16402{
16403 struct type *this_type = NULL;
16404
c906108c
SS
16405 switch (die->tag)
16406 {
16407 case DW_TAG_class_type:
680b30c7 16408 case DW_TAG_interface_type:
c906108c
SS
16409 case DW_TAG_structure_type:
16410 case DW_TAG_union_type:
f792889a 16411 this_type = read_structure_type (die, cu);
c906108c
SS
16412 break;
16413 case DW_TAG_enumeration_type:
f792889a 16414 this_type = read_enumeration_type (die, cu);
c906108c
SS
16415 break;
16416 case DW_TAG_subprogram:
16417 case DW_TAG_subroutine_type:
edb3359d 16418 case DW_TAG_inlined_subroutine:
f792889a 16419 this_type = read_subroutine_type (die, cu);
c906108c
SS
16420 break;
16421 case DW_TAG_array_type:
f792889a 16422 this_type = read_array_type (die, cu);
c906108c 16423 break;
72019c9c 16424 case DW_TAG_set_type:
f792889a 16425 this_type = read_set_type (die, cu);
72019c9c 16426 break;
c906108c 16427 case DW_TAG_pointer_type:
f792889a 16428 this_type = read_tag_pointer_type (die, cu);
c906108c
SS
16429 break;
16430 case DW_TAG_ptr_to_member_type:
f792889a 16431 this_type = read_tag_ptr_to_member_type (die, cu);
c906108c
SS
16432 break;
16433 case DW_TAG_reference_type:
f792889a 16434 this_type = read_tag_reference_type (die, cu);
c906108c
SS
16435 break;
16436 case DW_TAG_const_type:
f792889a 16437 this_type = read_tag_const_type (die, cu);
c906108c
SS
16438 break;
16439 case DW_TAG_volatile_type:
f792889a 16440 this_type = read_tag_volatile_type (die, cu);
c906108c
SS
16441 break;
16442 case DW_TAG_string_type:
f792889a 16443 this_type = read_tag_string_type (die, cu);
c906108c
SS
16444 break;
16445 case DW_TAG_typedef:
f792889a 16446 this_type = read_typedef (die, cu);
c906108c 16447 break;
a02abb62 16448 case DW_TAG_subrange_type:
f792889a 16449 this_type = read_subrange_type (die, cu);
a02abb62 16450 break;
c906108c 16451 case DW_TAG_base_type:
f792889a 16452 this_type = read_base_type (die, cu);
c906108c 16453 break;
81a17f79 16454 case DW_TAG_unspecified_type:
f792889a 16455 this_type = read_unspecified_type (die, cu);
81a17f79 16456 break;
0114d602
DJ
16457 case DW_TAG_namespace:
16458 this_type = read_namespace_type (die, cu);
16459 break;
f55ee35c
JK
16460 case DW_TAG_module:
16461 this_type = read_module_type (die, cu);
16462 break;
c906108c 16463 default:
3e43a32a
MS
16464 complaint (&symfile_complaints,
16465 _("unexpected tag in read_type_die: '%s'"),
4d3c2250 16466 dwarf_tag_name (die->tag));
c906108c
SS
16467 break;
16468 }
63d06c5c 16469
f792889a 16470 return this_type;
63d06c5c
DC
16471}
16472
abc72ce4
DE
16473/* See if we can figure out if the class lives in a namespace. We do
16474 this by looking for a member function; its demangled name will
16475 contain namespace info, if there is any.
16476 Return the computed name or NULL.
16477 Space for the result is allocated on the objfile's obstack.
16478 This is the full-die version of guess_partial_die_structure_name.
16479 In this case we know DIE has no useful parent. */
16480
16481static char *
16482guess_full_die_structure_name (struct die_info *die, struct dwarf2_cu *cu)
16483{
16484 struct die_info *spec_die;
16485 struct dwarf2_cu *spec_cu;
16486 struct die_info *child;
16487
16488 spec_cu = cu;
16489 spec_die = die_specification (die, &spec_cu);
16490 if (spec_die != NULL)
16491 {
16492 die = spec_die;
16493 cu = spec_cu;
16494 }
16495
16496 for (child = die->child;
16497 child != NULL;
16498 child = child->sibling)
16499 {
16500 if (child->tag == DW_TAG_subprogram)
16501 {
16502 struct attribute *attr;
16503
16504 attr = dwarf2_attr (child, DW_AT_linkage_name, cu);
16505 if (attr == NULL)
16506 attr = dwarf2_attr (child, DW_AT_MIPS_linkage_name, cu);
16507 if (attr != NULL)
16508 {
16509 char *actual_name
16510 = language_class_name_from_physname (cu->language_defn,
16511 DW_STRING (attr));
16512 char *name = NULL;
16513
16514 if (actual_name != NULL)
16515 {
16516 char *die_name = dwarf2_name (die, cu);
16517
16518 if (die_name != NULL
16519 && strcmp (die_name, actual_name) != 0)
16520 {
16521 /* Strip off the class name from the full name.
16522 We want the prefix. */
16523 int die_name_len = strlen (die_name);
16524 int actual_name_len = strlen (actual_name);
16525
16526 /* Test for '::' as a sanity check. */
16527 if (actual_name_len > die_name_len + 2
3e43a32a
MS
16528 && actual_name[actual_name_len
16529 - die_name_len - 1] == ':')
abc72ce4
DE
16530 name =
16531 obsavestring (actual_name,
16532 actual_name_len - die_name_len - 2,
16533 &cu->objfile->objfile_obstack);
16534 }
16535 }
16536 xfree (actual_name);
16537 return name;
16538 }
16539 }
16540 }
16541
16542 return NULL;
16543}
16544
96408a79
SA
16545/* GCC might emit a nameless typedef that has a linkage name. Determine the
16546 prefix part in such case. See
16547 http://gcc.gnu.org/bugzilla/show_bug.cgi?id=47510. */
16548
16549static char *
16550anonymous_struct_prefix (struct die_info *die, struct dwarf2_cu *cu)
16551{
16552 struct attribute *attr;
16553 char *base;
16554
16555 if (die->tag != DW_TAG_class_type && die->tag != DW_TAG_interface_type
16556 && die->tag != DW_TAG_structure_type && die->tag != DW_TAG_union_type)
16557 return NULL;
16558
16559 attr = dwarf2_attr (die, DW_AT_name, cu);
16560 if (attr != NULL && DW_STRING (attr) != NULL)
16561 return NULL;
16562
16563 attr = dwarf2_attr (die, DW_AT_linkage_name, cu);
16564 if (attr == NULL)
16565 attr = dwarf2_attr (die, DW_AT_MIPS_linkage_name, cu);
16566 if (attr == NULL || DW_STRING (attr) == NULL)
16567 return NULL;
16568
16569 /* dwarf2_name had to be already called. */
16570 gdb_assert (DW_STRING_IS_CANONICAL (attr));
16571
16572 /* Strip the base name, keep any leading namespaces/classes. */
16573 base = strrchr (DW_STRING (attr), ':');
16574 if (base == NULL || base == DW_STRING (attr) || base[-1] != ':')
16575 return "";
16576
16577 return obsavestring (DW_STRING (attr), &base[-1] - DW_STRING (attr),
16578 &cu->objfile->objfile_obstack);
16579}
16580
fdde2d81 16581/* Return the name of the namespace/class that DIE is defined within,
0114d602 16582 or "" if we can't tell. The caller should not xfree the result.
fdde2d81 16583
0114d602
DJ
16584 For example, if we're within the method foo() in the following
16585 code:
16586
16587 namespace N {
16588 class C {
16589 void foo () {
16590 }
16591 };
16592 }
16593
16594 then determine_prefix on foo's die will return "N::C". */
fdde2d81 16595
0d5cff50 16596static const char *
e142c38c 16597determine_prefix (struct die_info *die, struct dwarf2_cu *cu)
63d06c5c 16598{
0114d602
DJ
16599 struct die_info *parent, *spec_die;
16600 struct dwarf2_cu *spec_cu;
16601 struct type *parent_type;
96408a79 16602 char *retval;
63d06c5c 16603
f55ee35c
JK
16604 if (cu->language != language_cplus && cu->language != language_java
16605 && cu->language != language_fortran)
0114d602
DJ
16606 return "";
16607
96408a79
SA
16608 retval = anonymous_struct_prefix (die, cu);
16609 if (retval)
16610 return retval;
16611
0114d602
DJ
16612 /* We have to be careful in the presence of DW_AT_specification.
16613 For example, with GCC 3.4, given the code
16614
16615 namespace N {
16616 void foo() {
16617 // Definition of N::foo.
16618 }
16619 }
16620
16621 then we'll have a tree of DIEs like this:
16622
16623 1: DW_TAG_compile_unit
16624 2: DW_TAG_namespace // N
16625 3: DW_TAG_subprogram // declaration of N::foo
16626 4: DW_TAG_subprogram // definition of N::foo
16627 DW_AT_specification // refers to die #3
16628
16629 Thus, when processing die #4, we have to pretend that we're in
16630 the context of its DW_AT_specification, namely the contex of die
16631 #3. */
16632 spec_cu = cu;
16633 spec_die = die_specification (die, &spec_cu);
16634 if (spec_die == NULL)
16635 parent = die->parent;
16636 else
63d06c5c 16637 {
0114d602
DJ
16638 parent = spec_die->parent;
16639 cu = spec_cu;
63d06c5c 16640 }
0114d602
DJ
16641
16642 if (parent == NULL)
16643 return "";
98bfdba5
PA
16644 else if (parent->building_fullname)
16645 {
16646 const char *name;
16647 const char *parent_name;
16648
16649 /* It has been seen on RealView 2.2 built binaries,
16650 DW_TAG_template_type_param types actually _defined_ as
16651 children of the parent class:
16652
16653 enum E {};
16654 template class <class Enum> Class{};
16655 Class<enum E> class_e;
16656
16657 1: DW_TAG_class_type (Class)
16658 2: DW_TAG_enumeration_type (E)
16659 3: DW_TAG_enumerator (enum1:0)
16660 3: DW_TAG_enumerator (enum2:1)
16661 ...
16662 2: DW_TAG_template_type_param
16663 DW_AT_type DW_FORM_ref_udata (E)
16664
16665 Besides being broken debug info, it can put GDB into an
16666 infinite loop. Consider:
16667
16668 When we're building the full name for Class<E>, we'll start
16669 at Class, and go look over its template type parameters,
16670 finding E. We'll then try to build the full name of E, and
16671 reach here. We're now trying to build the full name of E,
16672 and look over the parent DIE for containing scope. In the
16673 broken case, if we followed the parent DIE of E, we'd again
16674 find Class, and once again go look at its template type
16675 arguments, etc., etc. Simply don't consider such parent die
16676 as source-level parent of this die (it can't be, the language
16677 doesn't allow it), and break the loop here. */
16678 name = dwarf2_name (die, cu);
16679 parent_name = dwarf2_name (parent, cu);
16680 complaint (&symfile_complaints,
16681 _("template param type '%s' defined within parent '%s'"),
16682 name ? name : "<unknown>",
16683 parent_name ? parent_name : "<unknown>");
16684 return "";
16685 }
63d06c5c 16686 else
0114d602
DJ
16687 switch (parent->tag)
16688 {
63d06c5c 16689 case DW_TAG_namespace:
0114d602 16690 parent_type = read_type_die (parent, cu);
acebe513
UW
16691 /* GCC 4.0 and 4.1 had a bug (PR c++/28460) where they generated bogus
16692 DW_TAG_namespace DIEs with a name of "::" for the global namespace.
16693 Work around this problem here. */
16694 if (cu->language == language_cplus
16695 && strcmp (TYPE_TAG_NAME (parent_type), "::") == 0)
16696 return "";
0114d602
DJ
16697 /* We give a name to even anonymous namespaces. */
16698 return TYPE_TAG_NAME (parent_type);
63d06c5c 16699 case DW_TAG_class_type:
680b30c7 16700 case DW_TAG_interface_type:
63d06c5c 16701 case DW_TAG_structure_type:
0114d602 16702 case DW_TAG_union_type:
f55ee35c 16703 case DW_TAG_module:
0114d602
DJ
16704 parent_type = read_type_die (parent, cu);
16705 if (TYPE_TAG_NAME (parent_type) != NULL)
16706 return TYPE_TAG_NAME (parent_type);
16707 else
16708 /* An anonymous structure is only allowed non-static data
16709 members; no typedefs, no member functions, et cetera.
16710 So it does not need a prefix. */
16711 return "";
abc72ce4 16712 case DW_TAG_compile_unit:
95554aad 16713 case DW_TAG_partial_unit:
abc72ce4
DE
16714 /* gcc-4.5 -gdwarf-4 can drop the enclosing namespace. Cope. */
16715 if (cu->language == language_cplus
8b70b953 16716 && !VEC_empty (dwarf2_section_info_def, dwarf2_per_objfile->types)
abc72ce4
DE
16717 && die->child != NULL
16718 && (die->tag == DW_TAG_class_type
16719 || die->tag == DW_TAG_structure_type
16720 || die->tag == DW_TAG_union_type))
16721 {
16722 char *name = guess_full_die_structure_name (die, cu);
16723 if (name != NULL)
16724 return name;
16725 }
16726 return "";
63d06c5c 16727 default:
8176b9b8 16728 return determine_prefix (parent, cu);
63d06c5c 16729 }
63d06c5c
DC
16730}
16731
3e43a32a
MS
16732/* Return a newly-allocated string formed by concatenating PREFIX and SUFFIX
16733 with appropriate separator. If PREFIX or SUFFIX is NULL or empty, then
16734 simply copy the SUFFIX or PREFIX, respectively. If OBS is non-null, perform
16735 an obconcat, otherwise allocate storage for the result. The CU argument is
16736 used to determine the language and hence, the appropriate separator. */
987504bb 16737
f55ee35c 16738#define MAX_SEP_LEN 7 /* strlen ("__") + strlen ("_MOD_") */
63d06c5c
DC
16739
16740static char *
f55ee35c
JK
16741typename_concat (struct obstack *obs, const char *prefix, const char *suffix,
16742 int physname, struct dwarf2_cu *cu)
63d06c5c 16743{
f55ee35c 16744 const char *lead = "";
5c315b68 16745 const char *sep;
63d06c5c 16746
3e43a32a
MS
16747 if (suffix == NULL || suffix[0] == '\0'
16748 || prefix == NULL || prefix[0] == '\0')
987504bb
JJ
16749 sep = "";
16750 else if (cu->language == language_java)
16751 sep = ".";
f55ee35c
JK
16752 else if (cu->language == language_fortran && physname)
16753 {
16754 /* This is gfortran specific mangling. Normally DW_AT_linkage_name or
16755 DW_AT_MIPS_linkage_name is preferred and used instead. */
16756
16757 lead = "__";
16758 sep = "_MOD_";
16759 }
987504bb
JJ
16760 else
16761 sep = "::";
63d06c5c 16762
6dd47d34
DE
16763 if (prefix == NULL)
16764 prefix = "";
16765 if (suffix == NULL)
16766 suffix = "";
16767
987504bb
JJ
16768 if (obs == NULL)
16769 {
3e43a32a
MS
16770 char *retval
16771 = xmalloc (strlen (prefix) + MAX_SEP_LEN + strlen (suffix) + 1);
9a619af0 16772
f55ee35c
JK
16773 strcpy (retval, lead);
16774 strcat (retval, prefix);
6dd47d34
DE
16775 strcat (retval, sep);
16776 strcat (retval, suffix);
63d06c5c
DC
16777 return retval;
16778 }
987504bb
JJ
16779 else
16780 {
16781 /* We have an obstack. */
f55ee35c 16782 return obconcat (obs, lead, prefix, sep, suffix, (char *) NULL);
987504bb 16783 }
63d06c5c
DC
16784}
16785
c906108c
SS
16786/* Return sibling of die, NULL if no sibling. */
16787
f9aca02d 16788static struct die_info *
fba45db2 16789sibling_die (struct die_info *die)
c906108c 16790{
639d11d3 16791 return die->sibling;
c906108c
SS
16792}
16793
71c25dea
TT
16794/* Get name of a die, return NULL if not found. */
16795
16796static char *
16797dwarf2_canonicalize_name (char *name, struct dwarf2_cu *cu,
16798 struct obstack *obstack)
16799{
16800 if (name && cu->language == language_cplus)
16801 {
16802 char *canon_name = cp_canonicalize_string (name);
16803
16804 if (canon_name != NULL)
16805 {
16806 if (strcmp (canon_name, name) != 0)
16807 name = obsavestring (canon_name, strlen (canon_name),
16808 obstack);
16809 xfree (canon_name);
16810 }
16811 }
16812
16813 return name;
c906108c
SS
16814}
16815
9219021c
DC
16816/* Get name of a die, return NULL if not found. */
16817
16818static char *
e142c38c 16819dwarf2_name (struct die_info *die, struct dwarf2_cu *cu)
9219021c
DC
16820{
16821 struct attribute *attr;
16822
e142c38c 16823 attr = dwarf2_attr (die, DW_AT_name, cu);
53832f31
TT
16824 if ((!attr || !DW_STRING (attr))
16825 && die->tag != DW_TAG_class_type
16826 && die->tag != DW_TAG_interface_type
16827 && die->tag != DW_TAG_structure_type
16828 && die->tag != DW_TAG_union_type)
71c25dea
TT
16829 return NULL;
16830
16831 switch (die->tag)
16832 {
16833 case DW_TAG_compile_unit:
95554aad 16834 case DW_TAG_partial_unit:
71c25dea
TT
16835 /* Compilation units have a DW_AT_name that is a filename, not
16836 a source language identifier. */
16837 case DW_TAG_enumeration_type:
16838 case DW_TAG_enumerator:
16839 /* These tags always have simple identifiers already; no need
16840 to canonicalize them. */
16841 return DW_STRING (attr);
907af001 16842
418835cc
KS
16843 case DW_TAG_subprogram:
16844 /* Java constructors will all be named "<init>", so return
16845 the class name when we see this special case. */
16846 if (cu->language == language_java
16847 && DW_STRING (attr) != NULL
16848 && strcmp (DW_STRING (attr), "<init>") == 0)
16849 {
16850 struct dwarf2_cu *spec_cu = cu;
16851 struct die_info *spec_die;
16852
16853 /* GCJ will output '<init>' for Java constructor names.
16854 For this special case, return the name of the parent class. */
16855
16856 /* GCJ may output suprogram DIEs with AT_specification set.
16857 If so, use the name of the specified DIE. */
16858 spec_die = die_specification (die, &spec_cu);
16859 if (spec_die != NULL)
16860 return dwarf2_name (spec_die, spec_cu);
16861
16862 do
16863 {
16864 die = die->parent;
16865 if (die->tag == DW_TAG_class_type)
16866 return dwarf2_name (die, cu);
16867 }
95554aad
TT
16868 while (die->tag != DW_TAG_compile_unit
16869 && die->tag != DW_TAG_partial_unit);
418835cc 16870 }
907af001
UW
16871 break;
16872
16873 case DW_TAG_class_type:
16874 case DW_TAG_interface_type:
16875 case DW_TAG_structure_type:
16876 case DW_TAG_union_type:
16877 /* Some GCC versions emit spurious DW_AT_name attributes for unnamed
16878 structures or unions. These were of the form "._%d" in GCC 4.1,
16879 or simply "<anonymous struct>" or "<anonymous union>" in GCC 4.3
16880 and GCC 4.4. We work around this problem by ignoring these. */
53832f31
TT
16881 if (attr && DW_STRING (attr)
16882 && (strncmp (DW_STRING (attr), "._", 2) == 0
16883 || strncmp (DW_STRING (attr), "<anonymous", 10) == 0))
907af001 16884 return NULL;
53832f31
TT
16885
16886 /* GCC might emit a nameless typedef that has a linkage name. See
16887 http://gcc.gnu.org/bugzilla/show_bug.cgi?id=47510. */
16888 if (!attr || DW_STRING (attr) == NULL)
16889 {
df5c6c50 16890 char *demangled = NULL;
53832f31
TT
16891
16892 attr = dwarf2_attr (die, DW_AT_linkage_name, cu);
16893 if (attr == NULL)
16894 attr = dwarf2_attr (die, DW_AT_MIPS_linkage_name, cu);
16895
16896 if (attr == NULL || DW_STRING (attr) == NULL)
16897 return NULL;
16898
df5c6c50
JK
16899 /* Avoid demangling DW_STRING (attr) the second time on a second
16900 call for the same DIE. */
16901 if (!DW_STRING_IS_CANONICAL (attr))
16902 demangled = cplus_demangle (DW_STRING (attr), DMGL_TYPES);
53832f31
TT
16903
16904 if (demangled)
16905 {
96408a79
SA
16906 char *base;
16907
53832f31 16908 /* FIXME: we already did this for the partial symbol... */
96408a79
SA
16909 DW_STRING (attr) = obsavestring (demangled, strlen (demangled),
16910 &cu->objfile->objfile_obstack);
53832f31
TT
16911 DW_STRING_IS_CANONICAL (attr) = 1;
16912 xfree (demangled);
96408a79
SA
16913
16914 /* Strip any leading namespaces/classes, keep only the base name.
16915 DW_AT_name for named DIEs does not contain the prefixes. */
16916 base = strrchr (DW_STRING (attr), ':');
16917 if (base && base > DW_STRING (attr) && base[-1] == ':')
16918 return &base[1];
16919 else
16920 return DW_STRING (attr);
53832f31
TT
16921 }
16922 }
907af001
UW
16923 break;
16924
71c25dea 16925 default:
907af001
UW
16926 break;
16927 }
16928
16929 if (!DW_STRING_IS_CANONICAL (attr))
16930 {
16931 DW_STRING (attr)
16932 = dwarf2_canonicalize_name (DW_STRING (attr), cu,
16933 &cu->objfile->objfile_obstack);
16934 DW_STRING_IS_CANONICAL (attr) = 1;
71c25dea 16935 }
907af001 16936 return DW_STRING (attr);
9219021c
DC
16937}
16938
16939/* Return the die that this die in an extension of, or NULL if there
f2f0e013
DJ
16940 is none. *EXT_CU is the CU containing DIE on input, and the CU
16941 containing the return value on output. */
9219021c
DC
16942
16943static struct die_info *
f2f0e013 16944dwarf2_extension (struct die_info *die, struct dwarf2_cu **ext_cu)
9219021c
DC
16945{
16946 struct attribute *attr;
9219021c 16947
f2f0e013 16948 attr = dwarf2_attr (die, DW_AT_extension, *ext_cu);
9219021c
DC
16949 if (attr == NULL)
16950 return NULL;
16951
f2f0e013 16952 return follow_die_ref (die, attr, ext_cu);
9219021c
DC
16953}
16954
c906108c
SS
16955/* Convert a DIE tag into its string name. */
16956
f39c6ffd 16957static const char *
aa1ee363 16958dwarf_tag_name (unsigned tag)
c906108c 16959{
f39c6ffd
TT
16960 const char *name = get_DW_TAG_name (tag);
16961
16962 if (name == NULL)
16963 return "DW_TAG_<unknown>";
16964
16965 return name;
c906108c
SS
16966}
16967
16968/* Convert a DWARF attribute code into its string name. */
16969
f39c6ffd 16970static const char *
aa1ee363 16971dwarf_attr_name (unsigned attr)
c906108c 16972{
f39c6ffd
TT
16973 const char *name;
16974
c764a876 16975#ifdef MIPS /* collides with DW_AT_HP_block_index */
f39c6ffd
TT
16976 if (attr == DW_AT_MIPS_fde)
16977 return "DW_AT_MIPS_fde";
16978#else
16979 if (attr == DW_AT_HP_block_index)
16980 return "DW_AT_HP_block_index";
c764a876 16981#endif
f39c6ffd
TT
16982
16983 name = get_DW_AT_name (attr);
16984
16985 if (name == NULL)
16986 return "DW_AT_<unknown>";
16987
16988 return name;
c906108c
SS
16989}
16990
16991/* Convert a DWARF value form code into its string name. */
16992
f39c6ffd 16993static const char *
aa1ee363 16994dwarf_form_name (unsigned form)
c906108c 16995{
f39c6ffd
TT
16996 const char *name = get_DW_FORM_name (form);
16997
16998 if (name == NULL)
16999 return "DW_FORM_<unknown>";
17000
17001 return name;
c906108c
SS
17002}
17003
17004static char *
fba45db2 17005dwarf_bool_name (unsigned mybool)
c906108c
SS
17006{
17007 if (mybool)
17008 return "TRUE";
17009 else
17010 return "FALSE";
17011}
17012
17013/* Convert a DWARF type code into its string name. */
17014
f39c6ffd 17015static const char *
aa1ee363 17016dwarf_type_encoding_name (unsigned enc)
c906108c 17017{
f39c6ffd 17018 const char *name = get_DW_ATE_name (enc);
c906108c 17019
f39c6ffd
TT
17020 if (name == NULL)
17021 return "DW_ATE_<unknown>";
c906108c 17022
f39c6ffd 17023 return name;
c906108c 17024}
c906108c 17025
f9aca02d 17026static void
d97bc12b 17027dump_die_shallow (struct ui_file *f, int indent, struct die_info *die)
c906108c
SS
17028{
17029 unsigned int i;
17030
d97bc12b
DE
17031 print_spaces (indent, f);
17032 fprintf_unfiltered (f, "Die: %s (abbrev %d, offset 0x%x)\n",
b64f50a1 17033 dwarf_tag_name (die->tag), die->abbrev, die->offset.sect_off);
d97bc12b
DE
17034
17035 if (die->parent != NULL)
17036 {
17037 print_spaces (indent, f);
17038 fprintf_unfiltered (f, " parent at offset: 0x%x\n",
b64f50a1 17039 die->parent->offset.sect_off);
d97bc12b
DE
17040 }
17041
17042 print_spaces (indent, f);
17043 fprintf_unfiltered (f, " has children: %s\n",
639d11d3 17044 dwarf_bool_name (die->child != NULL));
c906108c 17045
d97bc12b
DE
17046 print_spaces (indent, f);
17047 fprintf_unfiltered (f, " attributes:\n");
17048
c906108c
SS
17049 for (i = 0; i < die->num_attrs; ++i)
17050 {
d97bc12b
DE
17051 print_spaces (indent, f);
17052 fprintf_unfiltered (f, " %s (%s) ",
c906108c
SS
17053 dwarf_attr_name (die->attrs[i].name),
17054 dwarf_form_name (die->attrs[i].form));
d97bc12b 17055
c906108c
SS
17056 switch (die->attrs[i].form)
17057 {
c906108c 17058 case DW_FORM_addr:
3019eac3 17059 case DW_FORM_GNU_addr_index:
d97bc12b 17060 fprintf_unfiltered (f, "address: ");
5af949e3 17061 fputs_filtered (hex_string (DW_ADDR (&die->attrs[i])), f);
c906108c
SS
17062 break;
17063 case DW_FORM_block2:
17064 case DW_FORM_block4:
17065 case DW_FORM_block:
17066 case DW_FORM_block1:
56eb65bd
SP
17067 fprintf_unfiltered (f, "block: size %s",
17068 pulongest (DW_BLOCK (&die->attrs[i])->size));
c906108c 17069 break;
2dc7f7b3 17070 case DW_FORM_exprloc:
56eb65bd
SP
17071 fprintf_unfiltered (f, "expression: size %s",
17072 pulongest (DW_BLOCK (&die->attrs[i])->size));
2dc7f7b3 17073 break;
4568ecf9
DE
17074 case DW_FORM_ref_addr:
17075 fprintf_unfiltered (f, "ref address: ");
17076 fputs_filtered (hex_string (DW_UNSND (&die->attrs[i])), f);
17077 break;
36586728
TT
17078 case DW_FORM_GNU_ref_alt:
17079 fprintf_unfiltered (f, "alt ref address: ");
17080 fputs_filtered (hex_string (DW_UNSND (&die->attrs[i])), f);
17081 break;
10b3939b
DJ
17082 case DW_FORM_ref1:
17083 case DW_FORM_ref2:
17084 case DW_FORM_ref4:
4568ecf9
DE
17085 case DW_FORM_ref8:
17086 case DW_FORM_ref_udata:
d97bc12b 17087 fprintf_unfiltered (f, "constant ref: 0x%lx (adjusted)",
4568ecf9 17088 (long) (DW_UNSND (&die->attrs[i])));
10b3939b 17089 break;
c906108c
SS
17090 case DW_FORM_data1:
17091 case DW_FORM_data2:
17092 case DW_FORM_data4:
ce5d95e1 17093 case DW_FORM_data8:
c906108c
SS
17094 case DW_FORM_udata:
17095 case DW_FORM_sdata:
43bbcdc2
PH
17096 fprintf_unfiltered (f, "constant: %s",
17097 pulongest (DW_UNSND (&die->attrs[i])));
c906108c 17098 break;
2dc7f7b3
TT
17099 case DW_FORM_sec_offset:
17100 fprintf_unfiltered (f, "section offset: %s",
17101 pulongest (DW_UNSND (&die->attrs[i])));
17102 break;
55f1336d 17103 case DW_FORM_ref_sig8:
348e048f
DE
17104 if (DW_SIGNATURED_TYPE (&die->attrs[i]) != NULL)
17105 fprintf_unfiltered (f, "signatured type, offset: 0x%x",
b64f50a1 17106 DW_SIGNATURED_TYPE (&die->attrs[i])->per_cu.offset.sect_off);
348e048f
DE
17107 else
17108 fprintf_unfiltered (f, "signatured type, offset: unknown");
17109 break;
c906108c 17110 case DW_FORM_string:
4bdf3d34 17111 case DW_FORM_strp:
3019eac3 17112 case DW_FORM_GNU_str_index:
36586728 17113 case DW_FORM_GNU_strp_alt:
8285870a 17114 fprintf_unfiltered (f, "string: \"%s\" (%s canonicalized)",
c906108c 17115 DW_STRING (&die->attrs[i])
8285870a
JK
17116 ? DW_STRING (&die->attrs[i]) : "",
17117 DW_STRING_IS_CANONICAL (&die->attrs[i]) ? "is" : "not");
c906108c
SS
17118 break;
17119 case DW_FORM_flag:
17120 if (DW_UNSND (&die->attrs[i]))
d97bc12b 17121 fprintf_unfiltered (f, "flag: TRUE");
c906108c 17122 else
d97bc12b 17123 fprintf_unfiltered (f, "flag: FALSE");
c906108c 17124 break;
2dc7f7b3
TT
17125 case DW_FORM_flag_present:
17126 fprintf_unfiltered (f, "flag: TRUE");
17127 break;
a8329558 17128 case DW_FORM_indirect:
0963b4bd
MS
17129 /* The reader will have reduced the indirect form to
17130 the "base form" so this form should not occur. */
3e43a32a
MS
17131 fprintf_unfiltered (f,
17132 "unexpected attribute form: DW_FORM_indirect");
a8329558 17133 break;
c906108c 17134 default:
d97bc12b 17135 fprintf_unfiltered (f, "unsupported attribute form: %d.",
c5aa993b 17136 die->attrs[i].form);
d97bc12b 17137 break;
c906108c 17138 }
d97bc12b 17139 fprintf_unfiltered (f, "\n");
c906108c
SS
17140 }
17141}
17142
f9aca02d 17143static void
d97bc12b 17144dump_die_for_error (struct die_info *die)
c906108c 17145{
d97bc12b
DE
17146 dump_die_shallow (gdb_stderr, 0, die);
17147}
17148
17149static void
17150dump_die_1 (struct ui_file *f, int level, int max_level, struct die_info *die)
17151{
17152 int indent = level * 4;
17153
17154 gdb_assert (die != NULL);
17155
17156 if (level >= max_level)
17157 return;
17158
17159 dump_die_shallow (f, indent, die);
17160
17161 if (die->child != NULL)
c906108c 17162 {
d97bc12b
DE
17163 print_spaces (indent, f);
17164 fprintf_unfiltered (f, " Children:");
17165 if (level + 1 < max_level)
17166 {
17167 fprintf_unfiltered (f, "\n");
17168 dump_die_1 (f, level + 1, max_level, die->child);
17169 }
17170 else
17171 {
3e43a32a
MS
17172 fprintf_unfiltered (f,
17173 " [not printed, max nesting level reached]\n");
d97bc12b
DE
17174 }
17175 }
17176
17177 if (die->sibling != NULL && level > 0)
17178 {
17179 dump_die_1 (f, level, max_level, die->sibling);
c906108c
SS
17180 }
17181}
17182
d97bc12b
DE
17183/* This is called from the pdie macro in gdbinit.in.
17184 It's not static so gcc will keep a copy callable from gdb. */
17185
17186void
17187dump_die (struct die_info *die, int max_level)
17188{
17189 dump_die_1 (gdb_stdlog, 0, max_level, die);
17190}
17191
f9aca02d 17192static void
51545339 17193store_in_ref_table (struct die_info *die, struct dwarf2_cu *cu)
c906108c 17194{
51545339 17195 void **slot;
c906108c 17196
b64f50a1
JK
17197 slot = htab_find_slot_with_hash (cu->die_hash, die, die->offset.sect_off,
17198 INSERT);
51545339
DJ
17199
17200 *slot = die;
c906108c
SS
17201}
17202
b64f50a1
JK
17203/* DW_ADDR is always stored already as sect_offset; despite for the forms
17204 besides DW_FORM_ref_addr it is stored as cu_offset in the DWARF file. */
17205
93311388
DE
17206static int
17207is_ref_attr (struct attribute *attr)
c906108c 17208{
c906108c
SS
17209 switch (attr->form)
17210 {
17211 case DW_FORM_ref_addr:
c906108c
SS
17212 case DW_FORM_ref1:
17213 case DW_FORM_ref2:
17214 case DW_FORM_ref4:
613e1657 17215 case DW_FORM_ref8:
c906108c 17216 case DW_FORM_ref_udata:
36586728 17217 case DW_FORM_GNU_ref_alt:
93311388 17218 return 1;
c906108c 17219 default:
93311388 17220 return 0;
c906108c 17221 }
93311388
DE
17222}
17223
b64f50a1
JK
17224/* Return DIE offset of ATTR. Return 0 with complaint if ATTR is not of the
17225 required kind. */
17226
17227static sect_offset
93311388
DE
17228dwarf2_get_ref_die_offset (struct attribute *attr)
17229{
4568ecf9 17230 sect_offset retval = { DW_UNSND (attr) };
b64f50a1 17231
93311388 17232 if (is_ref_attr (attr))
b64f50a1 17233 return retval;
93311388 17234
b64f50a1 17235 retval.sect_off = 0;
93311388
DE
17236 complaint (&symfile_complaints,
17237 _("unsupported die ref attribute form: '%s'"),
17238 dwarf_form_name (attr->form));
b64f50a1 17239 return retval;
c906108c
SS
17240}
17241
43bbcdc2
PH
17242/* Return the constant value held by ATTR. Return DEFAULT_VALUE if
17243 * the value held by the attribute is not constant. */
a02abb62 17244
43bbcdc2 17245static LONGEST
a02abb62
JB
17246dwarf2_get_attr_constant_value (struct attribute *attr, int default_value)
17247{
17248 if (attr->form == DW_FORM_sdata)
17249 return DW_SND (attr);
17250 else if (attr->form == DW_FORM_udata
17251 || attr->form == DW_FORM_data1
17252 || attr->form == DW_FORM_data2
17253 || attr->form == DW_FORM_data4
17254 || attr->form == DW_FORM_data8)
17255 return DW_UNSND (attr);
17256 else
17257 {
3e43a32a
MS
17258 complaint (&symfile_complaints,
17259 _("Attribute value is not a constant (%s)"),
a02abb62
JB
17260 dwarf_form_name (attr->form));
17261 return default_value;
17262 }
17263}
17264
348e048f
DE
17265/* Follow reference or signature attribute ATTR of SRC_DIE.
17266 On entry *REF_CU is the CU of SRC_DIE.
17267 On exit *REF_CU is the CU of the result. */
17268
17269static struct die_info *
17270follow_die_ref_or_sig (struct die_info *src_die, struct attribute *attr,
17271 struct dwarf2_cu **ref_cu)
17272{
17273 struct die_info *die;
17274
17275 if (is_ref_attr (attr))
17276 die = follow_die_ref (src_die, attr, ref_cu);
55f1336d 17277 else if (attr->form == DW_FORM_ref_sig8)
348e048f
DE
17278 die = follow_die_sig (src_die, attr, ref_cu);
17279 else
17280 {
17281 dump_die_for_error (src_die);
17282 error (_("Dwarf Error: Expected reference attribute [in module %s]"),
17283 (*ref_cu)->objfile->name);
17284 }
17285
17286 return die;
03dd20cc
DJ
17287}
17288
5c631832 17289/* Follow reference OFFSET.
673bfd45
DE
17290 On entry *REF_CU is the CU of the source die referencing OFFSET.
17291 On exit *REF_CU is the CU of the result.
17292 Returns NULL if OFFSET is invalid. */
f504f079 17293
f9aca02d 17294static struct die_info *
36586728
TT
17295follow_die_offset (sect_offset offset, int offset_in_dwz,
17296 struct dwarf2_cu **ref_cu)
c906108c 17297{
10b3939b 17298 struct die_info temp_die;
f2f0e013 17299 struct dwarf2_cu *target_cu, *cu = *ref_cu;
10b3939b 17300
348e048f
DE
17301 gdb_assert (cu->per_cu != NULL);
17302
98bfdba5
PA
17303 target_cu = cu;
17304
3019eac3 17305 if (cu->per_cu->is_debug_types)
348e048f
DE
17306 {
17307 /* .debug_types CUs cannot reference anything outside their CU.
17308 If they need to, they have to reference a signatured type via
55f1336d 17309 DW_FORM_ref_sig8. */
348e048f 17310 if (! offset_in_cu_p (&cu->header, offset))
5c631832 17311 return NULL;
348e048f 17312 }
36586728
TT
17313 else if (offset_in_dwz != cu->per_cu->is_dwz
17314 || ! offset_in_cu_p (&cu->header, offset))
10b3939b
DJ
17315 {
17316 struct dwarf2_per_cu_data *per_cu;
9a619af0 17317
36586728
TT
17318 per_cu = dwarf2_find_containing_comp_unit (offset, offset_in_dwz,
17319 cu->objfile);
03dd20cc
DJ
17320
17321 /* If necessary, add it to the queue and load its DIEs. */
95554aad
TT
17322 if (maybe_queue_comp_unit (cu, per_cu, cu->language))
17323 load_full_comp_unit (per_cu, cu->language);
03dd20cc 17324
10b3939b
DJ
17325 target_cu = per_cu->cu;
17326 }
98bfdba5
PA
17327 else if (cu->dies == NULL)
17328 {
17329 /* We're loading full DIEs during partial symbol reading. */
17330 gdb_assert (dwarf2_per_objfile->reading_partial_symbols);
95554aad 17331 load_full_comp_unit (cu->per_cu, language_minimal);
98bfdba5 17332 }
c906108c 17333
f2f0e013 17334 *ref_cu = target_cu;
51545339 17335 temp_die.offset = offset;
b64f50a1 17336 return htab_find_with_hash (target_cu->die_hash, &temp_die, offset.sect_off);
5c631832 17337}
10b3939b 17338
5c631832
JK
17339/* Follow reference attribute ATTR of SRC_DIE.
17340 On entry *REF_CU is the CU of SRC_DIE.
17341 On exit *REF_CU is the CU of the result. */
17342
17343static struct die_info *
17344follow_die_ref (struct die_info *src_die, struct attribute *attr,
17345 struct dwarf2_cu **ref_cu)
17346{
b64f50a1 17347 sect_offset offset = dwarf2_get_ref_die_offset (attr);
5c631832
JK
17348 struct dwarf2_cu *cu = *ref_cu;
17349 struct die_info *die;
17350
36586728
TT
17351 die = follow_die_offset (offset,
17352 (attr->form == DW_FORM_GNU_ref_alt
17353 || cu->per_cu->is_dwz),
17354 ref_cu);
5c631832
JK
17355 if (!die)
17356 error (_("Dwarf Error: Cannot find DIE at 0x%x referenced from DIE "
17357 "at 0x%x [in module %s]"),
b64f50a1 17358 offset.sect_off, src_die->offset.sect_off, cu->objfile->name);
348e048f 17359
5c631832
JK
17360 return die;
17361}
17362
d83e736b
JK
17363/* Return DWARF block referenced by DW_AT_location of DIE at OFFSET at PER_CU.
17364 Returned value is intended for DW_OP_call*. Returned
17365 dwarf2_locexpr_baton->data has lifetime of PER_CU->OBJFILE. */
5c631832
JK
17366
17367struct dwarf2_locexpr_baton
b64f50a1 17368dwarf2_fetch_die_location_block (cu_offset offset_in_cu,
8cf6f0b1
TT
17369 struct dwarf2_per_cu_data *per_cu,
17370 CORE_ADDR (*get_frame_pc) (void *baton),
17371 void *baton)
5c631832 17372{
b64f50a1 17373 sect_offset offset = { per_cu->offset.sect_off + offset_in_cu.cu_off };
918dd910 17374 struct dwarf2_cu *cu;
5c631832
JK
17375 struct die_info *die;
17376 struct attribute *attr;
17377 struct dwarf2_locexpr_baton retval;
17378
8cf6f0b1
TT
17379 dw2_setup (per_cu->objfile);
17380
918dd910
JK
17381 if (per_cu->cu == NULL)
17382 load_cu (per_cu);
17383 cu = per_cu->cu;
17384
36586728 17385 die = follow_die_offset (offset, per_cu->is_dwz, &cu);
5c631832
JK
17386 if (!die)
17387 error (_("Dwarf Error: Cannot find DIE at 0x%x referenced in module %s"),
b64f50a1 17388 offset.sect_off, per_cu->objfile->name);
5c631832
JK
17389
17390 attr = dwarf2_attr (die, DW_AT_location, cu);
17391 if (!attr)
17392 {
e103e986
JK
17393 /* DWARF: "If there is no such attribute, then there is no effect.".
17394 DATA is ignored if SIZE is 0. */
5c631832 17395
e103e986 17396 retval.data = NULL;
5c631832
JK
17397 retval.size = 0;
17398 }
8cf6f0b1
TT
17399 else if (attr_form_is_section_offset (attr))
17400 {
17401 struct dwarf2_loclist_baton loclist_baton;
17402 CORE_ADDR pc = (*get_frame_pc) (baton);
17403 size_t size;
17404
17405 fill_in_loclist_baton (cu, &loclist_baton, attr);
17406
17407 retval.data = dwarf2_find_location_expression (&loclist_baton,
17408 &size, pc);
17409 retval.size = size;
17410 }
5c631832
JK
17411 else
17412 {
17413 if (!attr_form_is_block (attr))
17414 error (_("Dwarf Error: DIE at 0x%x referenced in module %s "
17415 "is neither DW_FORM_block* nor DW_FORM_exprloc"),
b64f50a1 17416 offset.sect_off, per_cu->objfile->name);
5c631832
JK
17417
17418 retval.data = DW_BLOCK (attr)->data;
17419 retval.size = DW_BLOCK (attr)->size;
17420 }
17421 retval.per_cu = cu->per_cu;
918dd910 17422
918dd910
JK
17423 age_cached_comp_units ();
17424
5c631832 17425 return retval;
348e048f
DE
17426}
17427
8a9b8146
TT
17428/* Return the type of the DIE at DIE_OFFSET in the CU named by
17429 PER_CU. */
17430
17431struct type *
b64f50a1 17432dwarf2_get_die_type (cu_offset die_offset,
8a9b8146
TT
17433 struct dwarf2_per_cu_data *per_cu)
17434{
b64f50a1
JK
17435 sect_offset die_offset_sect;
17436
8a9b8146 17437 dw2_setup (per_cu->objfile);
b64f50a1
JK
17438
17439 die_offset_sect.sect_off = per_cu->offset.sect_off + die_offset.cu_off;
17440 return get_die_type_at_offset (die_offset_sect, per_cu);
8a9b8146
TT
17441}
17442
348e048f
DE
17443/* Follow the signature attribute ATTR in SRC_DIE.
17444 On entry *REF_CU is the CU of SRC_DIE.
17445 On exit *REF_CU is the CU of the result. */
17446
17447static struct die_info *
17448follow_die_sig (struct die_info *src_die, struct attribute *attr,
17449 struct dwarf2_cu **ref_cu)
17450{
17451 struct objfile *objfile = (*ref_cu)->objfile;
17452 struct die_info temp_die;
17453 struct signatured_type *sig_type = DW_SIGNATURED_TYPE (attr);
17454 struct dwarf2_cu *sig_cu;
17455 struct die_info *die;
17456
17457 /* sig_type will be NULL if the signatured type is missing from
17458 the debug info. */
17459 if (sig_type == NULL)
17460 error (_("Dwarf Error: Cannot find signatured DIE referenced from DIE "
17461 "at 0x%x [in module %s]"),
b64f50a1 17462 src_die->offset.sect_off, objfile->name);
348e048f
DE
17463
17464 /* If necessary, add it to the queue and load its DIEs. */
17465
95554aad 17466 if (maybe_queue_comp_unit (*ref_cu, &sig_type->per_cu, language_minimal))
a0f42c21 17467 read_signatured_type (sig_type);
348e048f
DE
17468
17469 gdb_assert (sig_type->per_cu.cu != NULL);
17470
17471 sig_cu = sig_type->per_cu.cu;
3019eac3
DE
17472 gdb_assert (sig_type->type_offset_in_section.sect_off != 0);
17473 temp_die.offset = sig_type->type_offset_in_section;
b64f50a1
JK
17474 die = htab_find_with_hash (sig_cu->die_hash, &temp_die,
17475 temp_die.offset.sect_off);
348e048f
DE
17476 if (die)
17477 {
17478 *ref_cu = sig_cu;
17479 return die;
17480 }
17481
3e43a32a
MS
17482 error (_("Dwarf Error: Cannot find signatured DIE at 0x%x referenced "
17483 "from DIE at 0x%x [in module %s]"),
b64f50a1 17484 temp_die.offset.sect_off, src_die->offset.sect_off, objfile->name);
348e048f
DE
17485}
17486
17487/* Given an offset of a signatured type, return its signatured_type. */
17488
17489static struct signatured_type *
8b70b953
TT
17490lookup_signatured_type_at_offset (struct objfile *objfile,
17491 struct dwarf2_section_info *section,
b64f50a1 17492 sect_offset offset)
348e048f 17493{
b64f50a1 17494 gdb_byte *info_ptr = section->buffer + offset.sect_off;
348e048f
DE
17495 unsigned int length, initial_length_size;
17496 unsigned int sig_offset;
52dc124a 17497 struct signatured_type find_entry, *sig_type;
348e048f
DE
17498
17499 length = read_initial_length (objfile->obfd, info_ptr, &initial_length_size);
17500 sig_offset = (initial_length_size
17501 + 2 /*version*/
17502 + (initial_length_size == 4 ? 4 : 8) /*debug_abbrev_offset*/
17503 + 1 /*address_size*/);
17504 find_entry.signature = bfd_get_64 (objfile->obfd, info_ptr + sig_offset);
52dc124a 17505 sig_type = htab_find (dwarf2_per_objfile->signatured_types, &find_entry);
348e048f
DE
17506
17507 /* This is only used to lookup previously recorded types.
17508 If we didn't find it, it's our bug. */
52dc124a
DE
17509 gdb_assert (sig_type != NULL);
17510 gdb_assert (offset.sect_off == sig_type->per_cu.offset.sect_off);
348e048f 17511
52dc124a 17512 return sig_type;
348e048f
DE
17513}
17514
e5fe5e75 17515/* Load the DIEs associated with type unit PER_CU into memory. */
348e048f
DE
17516
17517static void
e5fe5e75 17518load_full_type_unit (struct dwarf2_per_cu_data *per_cu)
348e048f 17519{
52dc124a 17520 struct signatured_type *sig_type;
348e048f 17521
f4dc4d17
DE
17522 /* Caller is responsible for ensuring type_unit_groups don't get here. */
17523 gdb_assert (! IS_TYPE_UNIT_GROUP (per_cu));
17524
6721b2ec
DE
17525 /* We have the per_cu, but we need the signatured_type.
17526 Fortunately this is an easy translation. */
17527 gdb_assert (per_cu->is_debug_types);
17528 sig_type = (struct signatured_type *) per_cu;
348e048f 17529
6721b2ec 17530 gdb_assert (per_cu->cu == NULL);
348e048f 17531
52dc124a 17532 read_signatured_type (sig_type);
348e048f 17533
6721b2ec 17534 gdb_assert (per_cu->cu != NULL);
348e048f
DE
17535}
17536
dee91e82
DE
17537/* die_reader_func for read_signatured_type.
17538 This is identical to load_full_comp_unit_reader,
17539 but is kept separate for now. */
348e048f
DE
17540
17541static void
dee91e82
DE
17542read_signatured_type_reader (const struct die_reader_specs *reader,
17543 gdb_byte *info_ptr,
17544 struct die_info *comp_unit_die,
17545 int has_children,
17546 void *data)
348e048f 17547{
dee91e82 17548 struct dwarf2_cu *cu = reader->cu;
348e048f 17549
dee91e82
DE
17550 gdb_assert (cu->die_hash == NULL);
17551 cu->die_hash =
17552 htab_create_alloc_ex (cu->header.length / 12,
17553 die_hash,
17554 die_eq,
17555 NULL,
17556 &cu->comp_unit_obstack,
17557 hashtab_obstack_allocate,
17558 dummy_obstack_deallocate);
348e048f 17559
dee91e82
DE
17560 if (has_children)
17561 comp_unit_die->child = read_die_and_siblings (reader, info_ptr,
17562 &info_ptr, comp_unit_die);
17563 cu->dies = comp_unit_die;
17564 /* comp_unit_die is not stored in die_hash, no need. */
348e048f
DE
17565
17566 /* We try not to read any attributes in this function, because not
9cdd5dbd 17567 all CUs needed for references have been loaded yet, and symbol
348e048f 17568 table processing isn't initialized. But we have to set the CU language,
dee91e82
DE
17569 or we won't be able to build types correctly.
17570 Similarly, if we do not read the producer, we can not apply
17571 producer-specific interpretation. */
95554aad 17572 prepare_one_comp_unit (cu, cu->dies, language_minimal);
dee91e82 17573}
348e048f 17574
3019eac3
DE
17575/* Read in a signatured type and build its CU and DIEs.
17576 If the type is a stub for the real type in a DWO file,
17577 read in the real type from the DWO file as well. */
dee91e82
DE
17578
17579static void
17580read_signatured_type (struct signatured_type *sig_type)
17581{
17582 struct dwarf2_per_cu_data *per_cu = &sig_type->per_cu;
348e048f 17583
3019eac3 17584 gdb_assert (per_cu->is_debug_types);
dee91e82 17585 gdb_assert (per_cu->cu == NULL);
348e048f 17586
f4dc4d17
DE
17587 init_cutu_and_read_dies (per_cu, NULL, 0, 1,
17588 read_signatured_type_reader, NULL);
c906108c
SS
17589}
17590
c906108c
SS
17591/* Decode simple location descriptions.
17592 Given a pointer to a dwarf block that defines a location, compute
17593 the location and return the value.
17594
4cecd739
DJ
17595 NOTE drow/2003-11-18: This function is called in two situations
17596 now: for the address of static or global variables (partial symbols
17597 only) and for offsets into structures which are expected to be
17598 (more or less) constant. The partial symbol case should go away,
17599 and only the constant case should remain. That will let this
17600 function complain more accurately. A few special modes are allowed
17601 without complaint for global variables (for instance, global
17602 register values and thread-local values).
c906108c
SS
17603
17604 A location description containing no operations indicates that the
4cecd739 17605 object is optimized out. The return value is 0 for that case.
6b992462
DJ
17606 FIXME drow/2003-11-16: No callers check for this case any more; soon all
17607 callers will only want a very basic result and this can become a
21ae7a4d
JK
17608 complaint.
17609
17610 Note that stack[0] is unused except as a default error return. */
c906108c
SS
17611
17612static CORE_ADDR
e7c27a73 17613decode_locdesc (struct dwarf_block *blk, struct dwarf2_cu *cu)
c906108c 17614{
e7c27a73 17615 struct objfile *objfile = cu->objfile;
56eb65bd
SP
17616 size_t i;
17617 size_t size = blk->size;
21ae7a4d
JK
17618 gdb_byte *data = blk->data;
17619 CORE_ADDR stack[64];
17620 int stacki;
17621 unsigned int bytes_read, unsnd;
17622 gdb_byte op;
c906108c 17623
21ae7a4d
JK
17624 i = 0;
17625 stacki = 0;
17626 stack[stacki] = 0;
17627 stack[++stacki] = 0;
17628
17629 while (i < size)
17630 {
17631 op = data[i++];
17632 switch (op)
17633 {
17634 case DW_OP_lit0:
17635 case DW_OP_lit1:
17636 case DW_OP_lit2:
17637 case DW_OP_lit3:
17638 case DW_OP_lit4:
17639 case DW_OP_lit5:
17640 case DW_OP_lit6:
17641 case DW_OP_lit7:
17642 case DW_OP_lit8:
17643 case DW_OP_lit9:
17644 case DW_OP_lit10:
17645 case DW_OP_lit11:
17646 case DW_OP_lit12:
17647 case DW_OP_lit13:
17648 case DW_OP_lit14:
17649 case DW_OP_lit15:
17650 case DW_OP_lit16:
17651 case DW_OP_lit17:
17652 case DW_OP_lit18:
17653 case DW_OP_lit19:
17654 case DW_OP_lit20:
17655 case DW_OP_lit21:
17656 case DW_OP_lit22:
17657 case DW_OP_lit23:
17658 case DW_OP_lit24:
17659 case DW_OP_lit25:
17660 case DW_OP_lit26:
17661 case DW_OP_lit27:
17662 case DW_OP_lit28:
17663 case DW_OP_lit29:
17664 case DW_OP_lit30:
17665 case DW_OP_lit31:
17666 stack[++stacki] = op - DW_OP_lit0;
17667 break;
f1bea926 17668
21ae7a4d
JK
17669 case DW_OP_reg0:
17670 case DW_OP_reg1:
17671 case DW_OP_reg2:
17672 case DW_OP_reg3:
17673 case DW_OP_reg4:
17674 case DW_OP_reg5:
17675 case DW_OP_reg6:
17676 case DW_OP_reg7:
17677 case DW_OP_reg8:
17678 case DW_OP_reg9:
17679 case DW_OP_reg10:
17680 case DW_OP_reg11:
17681 case DW_OP_reg12:
17682 case DW_OP_reg13:
17683 case DW_OP_reg14:
17684 case DW_OP_reg15:
17685 case DW_OP_reg16:
17686 case DW_OP_reg17:
17687 case DW_OP_reg18:
17688 case DW_OP_reg19:
17689 case DW_OP_reg20:
17690 case DW_OP_reg21:
17691 case DW_OP_reg22:
17692 case DW_OP_reg23:
17693 case DW_OP_reg24:
17694 case DW_OP_reg25:
17695 case DW_OP_reg26:
17696 case DW_OP_reg27:
17697 case DW_OP_reg28:
17698 case DW_OP_reg29:
17699 case DW_OP_reg30:
17700 case DW_OP_reg31:
17701 stack[++stacki] = op - DW_OP_reg0;
17702 if (i < size)
17703 dwarf2_complex_location_expr_complaint ();
17704 break;
c906108c 17705
21ae7a4d
JK
17706 case DW_OP_regx:
17707 unsnd = read_unsigned_leb128 (NULL, (data + i), &bytes_read);
17708 i += bytes_read;
17709 stack[++stacki] = unsnd;
17710 if (i < size)
17711 dwarf2_complex_location_expr_complaint ();
17712 break;
c906108c 17713
21ae7a4d
JK
17714 case DW_OP_addr:
17715 stack[++stacki] = read_address (objfile->obfd, &data[i],
17716 cu, &bytes_read);
17717 i += bytes_read;
17718 break;
d53d4ac5 17719
21ae7a4d
JK
17720 case DW_OP_const1u:
17721 stack[++stacki] = read_1_byte (objfile->obfd, &data[i]);
17722 i += 1;
17723 break;
17724
17725 case DW_OP_const1s:
17726 stack[++stacki] = read_1_signed_byte (objfile->obfd, &data[i]);
17727 i += 1;
17728 break;
17729
17730 case DW_OP_const2u:
17731 stack[++stacki] = read_2_bytes (objfile->obfd, &data[i]);
17732 i += 2;
17733 break;
17734
17735 case DW_OP_const2s:
17736 stack[++stacki] = read_2_signed_bytes (objfile->obfd, &data[i]);
17737 i += 2;
17738 break;
d53d4ac5 17739
21ae7a4d
JK
17740 case DW_OP_const4u:
17741 stack[++stacki] = read_4_bytes (objfile->obfd, &data[i]);
17742 i += 4;
17743 break;
17744
17745 case DW_OP_const4s:
17746 stack[++stacki] = read_4_signed_bytes (objfile->obfd, &data[i]);
17747 i += 4;
17748 break;
17749
585861ea
JK
17750 case DW_OP_const8u:
17751 stack[++stacki] = read_8_bytes (objfile->obfd, &data[i]);
17752 i += 8;
17753 break;
17754
21ae7a4d
JK
17755 case DW_OP_constu:
17756 stack[++stacki] = read_unsigned_leb128 (NULL, (data + i),
17757 &bytes_read);
17758 i += bytes_read;
17759 break;
17760
17761 case DW_OP_consts:
17762 stack[++stacki] = read_signed_leb128 (NULL, (data + i), &bytes_read);
17763 i += bytes_read;
17764 break;
17765
17766 case DW_OP_dup:
17767 stack[stacki + 1] = stack[stacki];
17768 stacki++;
17769 break;
17770
17771 case DW_OP_plus:
17772 stack[stacki - 1] += stack[stacki];
17773 stacki--;
17774 break;
17775
17776 case DW_OP_plus_uconst:
17777 stack[stacki] += read_unsigned_leb128 (NULL, (data + i),
17778 &bytes_read);
17779 i += bytes_read;
17780 break;
17781
17782 case DW_OP_minus:
17783 stack[stacki - 1] -= stack[stacki];
17784 stacki--;
17785 break;
17786
17787 case DW_OP_deref:
17788 /* If we're not the last op, then we definitely can't encode
17789 this using GDB's address_class enum. This is valid for partial
17790 global symbols, although the variable's address will be bogus
17791 in the psymtab. */
17792 if (i < size)
17793 dwarf2_complex_location_expr_complaint ();
17794 break;
17795
17796 case DW_OP_GNU_push_tls_address:
17797 /* The top of the stack has the offset from the beginning
17798 of the thread control block at which the variable is located. */
17799 /* Nothing should follow this operator, so the top of stack would
17800 be returned. */
17801 /* This is valid for partial global symbols, but the variable's
585861ea
JK
17802 address will be bogus in the psymtab. Make it always at least
17803 non-zero to not look as a variable garbage collected by linker
17804 which have DW_OP_addr 0. */
21ae7a4d
JK
17805 if (i < size)
17806 dwarf2_complex_location_expr_complaint ();
585861ea 17807 stack[stacki]++;
21ae7a4d
JK
17808 break;
17809
17810 case DW_OP_GNU_uninit:
17811 break;
17812
3019eac3 17813 case DW_OP_GNU_addr_index:
49f6c839 17814 case DW_OP_GNU_const_index:
3019eac3
DE
17815 stack[++stacki] = read_addr_index_from_leb128 (cu, &data[i],
17816 &bytes_read);
17817 i += bytes_read;
17818 break;
17819
21ae7a4d
JK
17820 default:
17821 {
f39c6ffd 17822 const char *name = get_DW_OP_name (op);
21ae7a4d
JK
17823
17824 if (name)
17825 complaint (&symfile_complaints, _("unsupported stack op: '%s'"),
17826 name);
17827 else
17828 complaint (&symfile_complaints, _("unsupported stack op: '%02x'"),
17829 op);
17830 }
17831
17832 return (stack[stacki]);
d53d4ac5 17833 }
3c6e0cb3 17834
21ae7a4d
JK
17835 /* Enforce maximum stack depth of SIZE-1 to avoid writing
17836 outside of the allocated space. Also enforce minimum>0. */
17837 if (stacki >= ARRAY_SIZE (stack) - 1)
17838 {
17839 complaint (&symfile_complaints,
17840 _("location description stack overflow"));
17841 return 0;
17842 }
17843
17844 if (stacki <= 0)
17845 {
17846 complaint (&symfile_complaints,
17847 _("location description stack underflow"));
17848 return 0;
17849 }
17850 }
17851 return (stack[stacki]);
c906108c
SS
17852}
17853
17854/* memory allocation interface */
17855
c906108c 17856static struct dwarf_block *
7b5a2f43 17857dwarf_alloc_block (struct dwarf2_cu *cu)
c906108c
SS
17858{
17859 struct dwarf_block *blk;
17860
17861 blk = (struct dwarf_block *)
7b5a2f43 17862 obstack_alloc (&cu->comp_unit_obstack, sizeof (struct dwarf_block));
c906108c
SS
17863 return (blk);
17864}
17865
c906108c 17866static struct die_info *
b60c80d6 17867dwarf_alloc_die (struct dwarf2_cu *cu, int num_attrs)
c906108c
SS
17868{
17869 struct die_info *die;
b60c80d6
DJ
17870 size_t size = sizeof (struct die_info);
17871
17872 if (num_attrs > 1)
17873 size += (num_attrs - 1) * sizeof (struct attribute);
c906108c 17874
b60c80d6 17875 die = (struct die_info *) obstack_alloc (&cu->comp_unit_obstack, size);
c906108c
SS
17876 memset (die, 0, sizeof (struct die_info));
17877 return (die);
17878}
2e276125
JB
17879
17880\f
17881/* Macro support. */
17882
2e276125
JB
17883/* Return the full name of file number I in *LH's file name table.
17884 Use COMP_DIR as the name of the current directory of the
17885 compilation. The result is allocated using xmalloc; the caller is
17886 responsible for freeing it. */
17887static char *
17888file_full_name (int file, struct line_header *lh, const char *comp_dir)
17889{
6a83a1e6
EZ
17890 /* Is the file number a valid index into the line header's file name
17891 table? Remember that file numbers start with one, not zero. */
17892 if (1 <= file && file <= lh->num_file_names)
17893 {
17894 struct file_entry *fe = &lh->file_names[file - 1];
6e70227d 17895
6a83a1e6
EZ
17896 if (IS_ABSOLUTE_PATH (fe->name))
17897 return xstrdup (fe->name);
17898 else
17899 {
17900 const char *dir;
17901 int dir_len;
17902 char *full_name;
17903
17904 if (fe->dir_index)
17905 dir = lh->include_dirs[fe->dir_index - 1];
17906 else
17907 dir = comp_dir;
17908
17909 if (dir)
17910 {
17911 dir_len = strlen (dir);
17912 full_name = xmalloc (dir_len + 1 + strlen (fe->name) + 1);
17913 strcpy (full_name, dir);
17914 full_name[dir_len] = '/';
17915 strcpy (full_name + dir_len + 1, fe->name);
17916 return full_name;
17917 }
17918 else
17919 return xstrdup (fe->name);
17920 }
17921 }
2e276125
JB
17922 else
17923 {
6a83a1e6
EZ
17924 /* The compiler produced a bogus file number. We can at least
17925 record the macro definitions made in the file, even if we
17926 won't be able to find the file by name. */
17927 char fake_name[80];
9a619af0 17928
8c042590
PM
17929 xsnprintf (fake_name, sizeof (fake_name),
17930 "<bad macro file number %d>", file);
2e276125 17931
6e70227d 17932 complaint (&symfile_complaints,
6a83a1e6
EZ
17933 _("bad file number in macro information (%d)"),
17934 file);
2e276125 17935
6a83a1e6 17936 return xstrdup (fake_name);
2e276125
JB
17937 }
17938}
17939
17940
17941static struct macro_source_file *
17942macro_start_file (int file, int line,
17943 struct macro_source_file *current_file,
17944 const char *comp_dir,
17945 struct line_header *lh, struct objfile *objfile)
17946{
17947 /* The full name of this source file. */
17948 char *full_name = file_full_name (file, lh, comp_dir);
17949
17950 /* We don't create a macro table for this compilation unit
17951 at all until we actually get a filename. */
17952 if (! pending_macros)
6532ff36
TT
17953 pending_macros = new_macro_table (&objfile->per_bfd->storage_obstack,
17954 objfile->per_bfd->macro_cache);
2e276125
JB
17955
17956 if (! current_file)
abc9d0dc
TT
17957 {
17958 /* If we have no current file, then this must be the start_file
17959 directive for the compilation unit's main source file. */
17960 current_file = macro_set_main (pending_macros, full_name);
17961 macro_define_special (pending_macros);
17962 }
2e276125
JB
17963 else
17964 current_file = macro_include (current_file, line, full_name);
17965
17966 xfree (full_name);
6e70227d 17967
2e276125
JB
17968 return current_file;
17969}
17970
17971
17972/* Copy the LEN characters at BUF to a xmalloc'ed block of memory,
17973 followed by a null byte. */
17974static char *
17975copy_string (const char *buf, int len)
17976{
17977 char *s = xmalloc (len + 1);
9a619af0 17978
2e276125
JB
17979 memcpy (s, buf, len);
17980 s[len] = '\0';
2e276125
JB
17981 return s;
17982}
17983
17984
17985static const char *
17986consume_improper_spaces (const char *p, const char *body)
17987{
17988 if (*p == ' ')
17989 {
4d3c2250 17990 complaint (&symfile_complaints,
3e43a32a
MS
17991 _("macro definition contains spaces "
17992 "in formal argument list:\n`%s'"),
4d3c2250 17993 body);
2e276125
JB
17994
17995 while (*p == ' ')
17996 p++;
17997 }
17998
17999 return p;
18000}
18001
18002
18003static void
18004parse_macro_definition (struct macro_source_file *file, int line,
18005 const char *body)
18006{
18007 const char *p;
18008
18009 /* The body string takes one of two forms. For object-like macro
18010 definitions, it should be:
18011
18012 <macro name> " " <definition>
18013
18014 For function-like macro definitions, it should be:
18015
18016 <macro name> "() " <definition>
18017 or
18018 <macro name> "(" <arg name> ( "," <arg name> ) * ") " <definition>
18019
18020 Spaces may appear only where explicitly indicated, and in the
18021 <definition>.
18022
18023 The Dwarf 2 spec says that an object-like macro's name is always
18024 followed by a space, but versions of GCC around March 2002 omit
6e70227d 18025 the space when the macro's definition is the empty string.
2e276125
JB
18026
18027 The Dwarf 2 spec says that there should be no spaces between the
18028 formal arguments in a function-like macro's formal argument list,
18029 but versions of GCC around March 2002 include spaces after the
18030 commas. */
18031
18032
18033 /* Find the extent of the macro name. The macro name is terminated
18034 by either a space or null character (for an object-like macro) or
18035 an opening paren (for a function-like macro). */
18036 for (p = body; *p; p++)
18037 if (*p == ' ' || *p == '(')
18038 break;
18039
18040 if (*p == ' ' || *p == '\0')
18041 {
18042 /* It's an object-like macro. */
18043 int name_len = p - body;
18044 char *name = copy_string (body, name_len);
18045 const char *replacement;
18046
18047 if (*p == ' ')
18048 replacement = body + name_len + 1;
18049 else
18050 {
4d3c2250 18051 dwarf2_macro_malformed_definition_complaint (body);
2e276125
JB
18052 replacement = body + name_len;
18053 }
6e70227d 18054
2e276125
JB
18055 macro_define_object (file, line, name, replacement);
18056
18057 xfree (name);
18058 }
18059 else if (*p == '(')
18060 {
18061 /* It's a function-like macro. */
18062 char *name = copy_string (body, p - body);
18063 int argc = 0;
18064 int argv_size = 1;
18065 char **argv = xmalloc (argv_size * sizeof (*argv));
18066
18067 p++;
18068
18069 p = consume_improper_spaces (p, body);
18070
18071 /* Parse the formal argument list. */
18072 while (*p && *p != ')')
18073 {
18074 /* Find the extent of the current argument name. */
18075 const char *arg_start = p;
18076
18077 while (*p && *p != ',' && *p != ')' && *p != ' ')
18078 p++;
18079
18080 if (! *p || p == arg_start)
4d3c2250 18081 dwarf2_macro_malformed_definition_complaint (body);
2e276125
JB
18082 else
18083 {
18084 /* Make sure argv has room for the new argument. */
18085 if (argc >= argv_size)
18086 {
18087 argv_size *= 2;
18088 argv = xrealloc (argv, argv_size * sizeof (*argv));
18089 }
18090
18091 argv[argc++] = copy_string (arg_start, p - arg_start);
18092 }
18093
18094 p = consume_improper_spaces (p, body);
18095
18096 /* Consume the comma, if present. */
18097 if (*p == ',')
18098 {
18099 p++;
18100
18101 p = consume_improper_spaces (p, body);
18102 }
18103 }
18104
18105 if (*p == ')')
18106 {
18107 p++;
18108
18109 if (*p == ' ')
18110 /* Perfectly formed definition, no complaints. */
18111 macro_define_function (file, line, name,
6e70227d 18112 argc, (const char **) argv,
2e276125
JB
18113 p + 1);
18114 else if (*p == '\0')
18115 {
18116 /* Complain, but do define it. */
4d3c2250 18117 dwarf2_macro_malformed_definition_complaint (body);
2e276125 18118 macro_define_function (file, line, name,
6e70227d 18119 argc, (const char **) argv,
2e276125
JB
18120 p);
18121 }
18122 else
18123 /* Just complain. */
4d3c2250 18124 dwarf2_macro_malformed_definition_complaint (body);
2e276125
JB
18125 }
18126 else
18127 /* Just complain. */
4d3c2250 18128 dwarf2_macro_malformed_definition_complaint (body);
2e276125
JB
18129
18130 xfree (name);
18131 {
18132 int i;
18133
18134 for (i = 0; i < argc; i++)
18135 xfree (argv[i]);
18136 }
18137 xfree (argv);
18138 }
18139 else
4d3c2250 18140 dwarf2_macro_malformed_definition_complaint (body);
2e276125
JB
18141}
18142
cf2c3c16
TT
18143/* Skip some bytes from BYTES according to the form given in FORM.
18144 Returns the new pointer. */
2e276125 18145
cf2c3c16 18146static gdb_byte *
f664829e 18147skip_form_bytes (bfd *abfd, gdb_byte *bytes, gdb_byte *buffer_end,
cf2c3c16
TT
18148 enum dwarf_form form,
18149 unsigned int offset_size,
18150 struct dwarf2_section_info *section)
2e276125 18151{
cf2c3c16 18152 unsigned int bytes_read;
2e276125 18153
cf2c3c16 18154 switch (form)
2e276125 18155 {
cf2c3c16
TT
18156 case DW_FORM_data1:
18157 case DW_FORM_flag:
18158 ++bytes;
18159 break;
18160
18161 case DW_FORM_data2:
18162 bytes += 2;
18163 break;
18164
18165 case DW_FORM_data4:
18166 bytes += 4;
18167 break;
18168
18169 case DW_FORM_data8:
18170 bytes += 8;
18171 break;
18172
18173 case DW_FORM_string:
18174 read_direct_string (abfd, bytes, &bytes_read);
18175 bytes += bytes_read;
18176 break;
18177
18178 case DW_FORM_sec_offset:
18179 case DW_FORM_strp:
36586728 18180 case DW_FORM_GNU_strp_alt:
cf2c3c16
TT
18181 bytes += offset_size;
18182 break;
18183
18184 case DW_FORM_block:
18185 bytes += read_unsigned_leb128 (abfd, bytes, &bytes_read);
18186 bytes += bytes_read;
18187 break;
18188
18189 case DW_FORM_block1:
18190 bytes += 1 + read_1_byte (abfd, bytes);
18191 break;
18192 case DW_FORM_block2:
18193 bytes += 2 + read_2_bytes (abfd, bytes);
18194 break;
18195 case DW_FORM_block4:
18196 bytes += 4 + read_4_bytes (abfd, bytes);
18197 break;
18198
18199 case DW_FORM_sdata:
18200 case DW_FORM_udata:
3019eac3
DE
18201 case DW_FORM_GNU_addr_index:
18202 case DW_FORM_GNU_str_index:
f664829e
DE
18203 bytes = (gdb_byte *) gdb_skip_leb128 (bytes, buffer_end);
18204 if (bytes == NULL)
18205 {
18206 dwarf2_section_buffer_overflow_complaint (section);
18207 return NULL;
18208 }
cf2c3c16
TT
18209 break;
18210
18211 default:
18212 {
18213 complain:
18214 complaint (&symfile_complaints,
18215 _("invalid form 0x%x in `%s'"),
18216 form,
18217 section->asection->name);
18218 return NULL;
18219 }
2e276125
JB
18220 }
18221
cf2c3c16
TT
18222 return bytes;
18223}
757a13d0 18224
cf2c3c16
TT
18225/* A helper for dwarf_decode_macros that handles skipping an unknown
18226 opcode. Returns an updated pointer to the macro data buffer; or,
18227 on error, issues a complaint and returns NULL. */
757a13d0 18228
cf2c3c16
TT
18229static gdb_byte *
18230skip_unknown_opcode (unsigned int opcode,
18231 gdb_byte **opcode_definitions,
f664829e 18232 gdb_byte *mac_ptr, gdb_byte *mac_end,
cf2c3c16
TT
18233 bfd *abfd,
18234 unsigned int offset_size,
18235 struct dwarf2_section_info *section)
18236{
18237 unsigned int bytes_read, i;
18238 unsigned long arg;
18239 gdb_byte *defn;
2e276125 18240
cf2c3c16 18241 if (opcode_definitions[opcode] == NULL)
2e276125 18242 {
cf2c3c16
TT
18243 complaint (&symfile_complaints,
18244 _("unrecognized DW_MACFINO opcode 0x%x"),
18245 opcode);
18246 return NULL;
18247 }
2e276125 18248
cf2c3c16
TT
18249 defn = opcode_definitions[opcode];
18250 arg = read_unsigned_leb128 (abfd, defn, &bytes_read);
18251 defn += bytes_read;
2e276125 18252
cf2c3c16
TT
18253 for (i = 0; i < arg; ++i)
18254 {
f664829e
DE
18255 mac_ptr = skip_form_bytes (abfd, mac_ptr, mac_end, defn[i], offset_size,
18256 section);
cf2c3c16
TT
18257 if (mac_ptr == NULL)
18258 {
18259 /* skip_form_bytes already issued the complaint. */
18260 return NULL;
18261 }
18262 }
757a13d0 18263
cf2c3c16
TT
18264 return mac_ptr;
18265}
757a13d0 18266
cf2c3c16
TT
18267/* A helper function which parses the header of a macro section.
18268 If the macro section is the extended (for now called "GNU") type,
18269 then this updates *OFFSET_SIZE. Returns a pointer to just after
18270 the header, or issues a complaint and returns NULL on error. */
757a13d0 18271
cf2c3c16
TT
18272static gdb_byte *
18273dwarf_parse_macro_header (gdb_byte **opcode_definitions,
18274 bfd *abfd,
18275 gdb_byte *mac_ptr,
18276 unsigned int *offset_size,
18277 int section_is_gnu)
18278{
18279 memset (opcode_definitions, 0, 256 * sizeof (gdb_byte *));
757a13d0 18280
cf2c3c16
TT
18281 if (section_is_gnu)
18282 {
18283 unsigned int version, flags;
757a13d0 18284
cf2c3c16
TT
18285 version = read_2_bytes (abfd, mac_ptr);
18286 if (version != 4)
18287 {
18288 complaint (&symfile_complaints,
18289 _("unrecognized version `%d' in .debug_macro section"),
18290 version);
18291 return NULL;
18292 }
18293 mac_ptr += 2;
757a13d0 18294
cf2c3c16
TT
18295 flags = read_1_byte (abfd, mac_ptr);
18296 ++mac_ptr;
18297 *offset_size = (flags & 1) ? 8 : 4;
757a13d0 18298
cf2c3c16
TT
18299 if ((flags & 2) != 0)
18300 /* We don't need the line table offset. */
18301 mac_ptr += *offset_size;
757a13d0 18302
cf2c3c16
TT
18303 /* Vendor opcode descriptions. */
18304 if ((flags & 4) != 0)
18305 {
18306 unsigned int i, count;
757a13d0 18307
cf2c3c16
TT
18308 count = read_1_byte (abfd, mac_ptr);
18309 ++mac_ptr;
18310 for (i = 0; i < count; ++i)
18311 {
18312 unsigned int opcode, bytes_read;
18313 unsigned long arg;
18314
18315 opcode = read_1_byte (abfd, mac_ptr);
18316 ++mac_ptr;
18317 opcode_definitions[opcode] = mac_ptr;
18318 arg = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
18319 mac_ptr += bytes_read;
18320 mac_ptr += arg;
18321 }
757a13d0 18322 }
cf2c3c16 18323 }
757a13d0 18324
cf2c3c16
TT
18325 return mac_ptr;
18326}
757a13d0 18327
cf2c3c16 18328/* A helper for dwarf_decode_macros that handles the GNU extensions,
8fc3fc34 18329 including DW_MACRO_GNU_transparent_include. */
cf2c3c16
TT
18330
18331static void
18332dwarf_decode_macro_bytes (bfd *abfd, gdb_byte *mac_ptr, gdb_byte *mac_end,
18333 struct macro_source_file *current_file,
18334 struct line_header *lh, char *comp_dir,
18335 struct dwarf2_section_info *section,
36586728 18336 int section_is_gnu, int section_is_dwz,
cf2c3c16 18337 unsigned int offset_size,
8fc3fc34
TT
18338 struct objfile *objfile,
18339 htab_t include_hash)
cf2c3c16
TT
18340{
18341 enum dwarf_macro_record_type macinfo_type;
18342 int at_commandline;
18343 gdb_byte *opcode_definitions[256];
757a13d0 18344
cf2c3c16
TT
18345 mac_ptr = dwarf_parse_macro_header (opcode_definitions, abfd, mac_ptr,
18346 &offset_size, section_is_gnu);
18347 if (mac_ptr == NULL)
18348 {
18349 /* We already issued a complaint. */
18350 return;
18351 }
757a13d0
JK
18352
18353 /* Determines if GDB is still before first DW_MACINFO_start_file. If true
18354 GDB is still reading the definitions from command line. First
18355 DW_MACINFO_start_file will need to be ignored as it was already executed
18356 to create CURRENT_FILE for the main source holding also the command line
18357 definitions. On first met DW_MACINFO_start_file this flag is reset to
18358 normally execute all the remaining DW_MACINFO_start_file macinfos. */
18359
18360 at_commandline = 1;
18361
18362 do
18363 {
18364 /* Do we at least have room for a macinfo type byte? */
18365 if (mac_ptr >= mac_end)
18366 {
f664829e 18367 dwarf2_section_buffer_overflow_complaint (section);
757a13d0
JK
18368 break;
18369 }
18370
18371 macinfo_type = read_1_byte (abfd, mac_ptr);
18372 mac_ptr++;
18373
cf2c3c16
TT
18374 /* Note that we rely on the fact that the corresponding GNU and
18375 DWARF constants are the same. */
757a13d0
JK
18376 switch (macinfo_type)
18377 {
18378 /* A zero macinfo type indicates the end of the macro
18379 information. */
18380 case 0:
18381 break;
2e276125 18382
cf2c3c16
TT
18383 case DW_MACRO_GNU_define:
18384 case DW_MACRO_GNU_undef:
18385 case DW_MACRO_GNU_define_indirect:
18386 case DW_MACRO_GNU_undef_indirect:
36586728
TT
18387 case DW_MACRO_GNU_define_indirect_alt:
18388 case DW_MACRO_GNU_undef_indirect_alt:
2e276125 18389 {
891d2f0b 18390 unsigned int bytes_read;
2e276125
JB
18391 int line;
18392 char *body;
cf2c3c16 18393 int is_define;
2e276125 18394
cf2c3c16
TT
18395 line = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
18396 mac_ptr += bytes_read;
18397
18398 if (macinfo_type == DW_MACRO_GNU_define
18399 || macinfo_type == DW_MACRO_GNU_undef)
18400 {
18401 body = read_direct_string (abfd, mac_ptr, &bytes_read);
18402 mac_ptr += bytes_read;
18403 }
18404 else
18405 {
18406 LONGEST str_offset;
18407
18408 str_offset = read_offset_1 (abfd, mac_ptr, offset_size);
18409 mac_ptr += offset_size;
2e276125 18410
36586728 18411 if (macinfo_type == DW_MACRO_GNU_define_indirect_alt
f7a35f02
TT
18412 || macinfo_type == DW_MACRO_GNU_undef_indirect_alt
18413 || section_is_dwz)
36586728
TT
18414 {
18415 struct dwz_file *dwz = dwarf2_get_dwz_file ();
18416
18417 body = read_indirect_string_from_dwz (dwz, str_offset);
18418 }
18419 else
18420 body = read_indirect_string_at_offset (abfd, str_offset);
cf2c3c16
TT
18421 }
18422
18423 is_define = (macinfo_type == DW_MACRO_GNU_define
36586728
TT
18424 || macinfo_type == DW_MACRO_GNU_define_indirect
18425 || macinfo_type == DW_MACRO_GNU_define_indirect_alt);
2e276125 18426 if (! current_file)
757a13d0
JK
18427 {
18428 /* DWARF violation as no main source is present. */
18429 complaint (&symfile_complaints,
18430 _("debug info with no main source gives macro %s "
18431 "on line %d: %s"),
cf2c3c16
TT
18432 is_define ? _("definition") : _("undefinition"),
18433 line, body);
757a13d0
JK
18434 break;
18435 }
3e43a32a
MS
18436 if ((line == 0 && !at_commandline)
18437 || (line != 0 && at_commandline))
4d3c2250 18438 complaint (&symfile_complaints,
757a13d0
JK
18439 _("debug info gives %s macro %s with %s line %d: %s"),
18440 at_commandline ? _("command-line") : _("in-file"),
cf2c3c16 18441 is_define ? _("definition") : _("undefinition"),
757a13d0
JK
18442 line == 0 ? _("zero") : _("non-zero"), line, body);
18443
cf2c3c16 18444 if (is_define)
757a13d0 18445 parse_macro_definition (current_file, line, body);
cf2c3c16
TT
18446 else
18447 {
18448 gdb_assert (macinfo_type == DW_MACRO_GNU_undef
36586728
TT
18449 || macinfo_type == DW_MACRO_GNU_undef_indirect
18450 || macinfo_type == DW_MACRO_GNU_undef_indirect_alt);
cf2c3c16
TT
18451 macro_undef (current_file, line, body);
18452 }
2e276125
JB
18453 }
18454 break;
18455
cf2c3c16 18456 case DW_MACRO_GNU_start_file:
2e276125 18457 {
891d2f0b 18458 unsigned int bytes_read;
2e276125
JB
18459 int line, file;
18460
18461 line = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
18462 mac_ptr += bytes_read;
18463 file = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
18464 mac_ptr += bytes_read;
18465
3e43a32a
MS
18466 if ((line == 0 && !at_commandline)
18467 || (line != 0 && at_commandline))
757a13d0
JK
18468 complaint (&symfile_complaints,
18469 _("debug info gives source %d included "
18470 "from %s at %s line %d"),
18471 file, at_commandline ? _("command-line") : _("file"),
18472 line == 0 ? _("zero") : _("non-zero"), line);
18473
18474 if (at_commandline)
18475 {
cf2c3c16
TT
18476 /* This DW_MACRO_GNU_start_file was executed in the
18477 pass one. */
757a13d0
JK
18478 at_commandline = 0;
18479 }
18480 else
18481 current_file = macro_start_file (file, line,
18482 current_file, comp_dir,
cf2c3c16 18483 lh, objfile);
2e276125
JB
18484 }
18485 break;
18486
cf2c3c16 18487 case DW_MACRO_GNU_end_file:
2e276125 18488 if (! current_file)
4d3c2250 18489 complaint (&symfile_complaints,
3e43a32a
MS
18490 _("macro debug info has an unmatched "
18491 "`close_file' directive"));
2e276125
JB
18492 else
18493 {
18494 current_file = current_file->included_by;
18495 if (! current_file)
18496 {
cf2c3c16 18497 enum dwarf_macro_record_type next_type;
2e276125
JB
18498
18499 /* GCC circa March 2002 doesn't produce the zero
18500 type byte marking the end of the compilation
18501 unit. Complain if it's not there, but exit no
18502 matter what. */
18503
18504 /* Do we at least have room for a macinfo type byte? */
18505 if (mac_ptr >= mac_end)
18506 {
f664829e 18507 dwarf2_section_buffer_overflow_complaint (section);
2e276125
JB
18508 return;
18509 }
18510
18511 /* We don't increment mac_ptr here, so this is just
18512 a look-ahead. */
18513 next_type = read_1_byte (abfd, mac_ptr);
18514 if (next_type != 0)
4d3c2250 18515 complaint (&symfile_complaints,
3e43a32a
MS
18516 _("no terminating 0-type entry for "
18517 "macros in `.debug_macinfo' section"));
2e276125
JB
18518
18519 return;
18520 }
18521 }
18522 break;
18523
cf2c3c16 18524 case DW_MACRO_GNU_transparent_include:
36586728 18525 case DW_MACRO_GNU_transparent_include_alt:
cf2c3c16
TT
18526 {
18527 LONGEST offset;
8fc3fc34 18528 void **slot;
a036ba48
TT
18529 bfd *include_bfd = abfd;
18530 struct dwarf2_section_info *include_section = section;
18531 struct dwarf2_section_info alt_section;
18532 gdb_byte *include_mac_end = mac_end;
18533 int is_dwz = section_is_dwz;
18534 gdb_byte *new_mac_ptr;
cf2c3c16
TT
18535
18536 offset = read_offset_1 (abfd, mac_ptr, offset_size);
18537 mac_ptr += offset_size;
18538
a036ba48
TT
18539 if (macinfo_type == DW_MACRO_GNU_transparent_include_alt)
18540 {
18541 struct dwz_file *dwz = dwarf2_get_dwz_file ();
18542
18543 dwarf2_read_section (dwarf2_per_objfile->objfile,
18544 &dwz->macro);
18545
18546 include_bfd = dwz->macro.asection->owner;
18547 include_section = &dwz->macro;
18548 include_mac_end = dwz->macro.buffer + dwz->macro.size;
18549 is_dwz = 1;
18550 }
18551
18552 new_mac_ptr = include_section->buffer + offset;
18553 slot = htab_find_slot (include_hash, new_mac_ptr, INSERT);
18554
8fc3fc34
TT
18555 if (*slot != NULL)
18556 {
18557 /* This has actually happened; see
18558 http://sourceware.org/bugzilla/show_bug.cgi?id=13568. */
18559 complaint (&symfile_complaints,
18560 _("recursive DW_MACRO_GNU_transparent_include in "
18561 ".debug_macro section"));
18562 }
18563 else
18564 {
a036ba48 18565 *slot = new_mac_ptr;
36586728 18566
a036ba48 18567 dwarf_decode_macro_bytes (include_bfd, new_mac_ptr,
36586728 18568 include_mac_end, current_file,
8fc3fc34 18569 lh, comp_dir,
36586728 18570 section, section_is_gnu, is_dwz,
8fc3fc34
TT
18571 offset_size, objfile, include_hash);
18572
a036ba48 18573 htab_remove_elt (include_hash, new_mac_ptr);
8fc3fc34 18574 }
cf2c3c16
TT
18575 }
18576 break;
18577
2e276125 18578 case DW_MACINFO_vendor_ext:
cf2c3c16
TT
18579 if (!section_is_gnu)
18580 {
18581 unsigned int bytes_read;
18582 int constant;
2e276125 18583
cf2c3c16
TT
18584 constant = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
18585 mac_ptr += bytes_read;
18586 read_direct_string (abfd, mac_ptr, &bytes_read);
18587 mac_ptr += bytes_read;
2e276125 18588
cf2c3c16
TT
18589 /* We don't recognize any vendor extensions. */
18590 break;
18591 }
18592 /* FALLTHROUGH */
18593
18594 default:
18595 mac_ptr = skip_unknown_opcode (macinfo_type, opcode_definitions,
f664829e 18596 mac_ptr, mac_end, abfd, offset_size,
cf2c3c16
TT
18597 section);
18598 if (mac_ptr == NULL)
18599 return;
18600 break;
2e276125 18601 }
757a13d0 18602 } while (macinfo_type != 0);
2e276125 18603}
8e19ed76 18604
cf2c3c16 18605static void
09262596
DE
18606dwarf_decode_macros (struct dwarf2_cu *cu, unsigned int offset,
18607 char *comp_dir, int section_is_gnu)
cf2c3c16 18608{
bb5ed363 18609 struct objfile *objfile = dwarf2_per_objfile->objfile;
09262596
DE
18610 struct line_header *lh = cu->line_header;
18611 bfd *abfd;
cf2c3c16
TT
18612 gdb_byte *mac_ptr, *mac_end;
18613 struct macro_source_file *current_file = 0;
18614 enum dwarf_macro_record_type macinfo_type;
18615 unsigned int offset_size = cu->header.offset_size;
18616 gdb_byte *opcode_definitions[256];
8fc3fc34
TT
18617 struct cleanup *cleanup;
18618 htab_t include_hash;
18619 void **slot;
09262596
DE
18620 struct dwarf2_section_info *section;
18621 const char *section_name;
18622
18623 if (cu->dwo_unit != NULL)
18624 {
18625 if (section_is_gnu)
18626 {
18627 section = &cu->dwo_unit->dwo_file->sections.macro;
18628 section_name = ".debug_macro.dwo";
18629 }
18630 else
18631 {
18632 section = &cu->dwo_unit->dwo_file->sections.macinfo;
18633 section_name = ".debug_macinfo.dwo";
18634 }
18635 }
18636 else
18637 {
18638 if (section_is_gnu)
18639 {
18640 section = &dwarf2_per_objfile->macro;
18641 section_name = ".debug_macro";
18642 }
18643 else
18644 {
18645 section = &dwarf2_per_objfile->macinfo;
18646 section_name = ".debug_macinfo";
18647 }
18648 }
cf2c3c16 18649
bb5ed363 18650 dwarf2_read_section (objfile, section);
cf2c3c16
TT
18651 if (section->buffer == NULL)
18652 {
fceca515 18653 complaint (&symfile_complaints, _("missing %s section"), section_name);
cf2c3c16
TT
18654 return;
18655 }
09262596 18656 abfd = section->asection->owner;
cf2c3c16
TT
18657
18658 /* First pass: Find the name of the base filename.
18659 This filename is needed in order to process all macros whose definition
18660 (or undefinition) comes from the command line. These macros are defined
18661 before the first DW_MACINFO_start_file entry, and yet still need to be
18662 associated to the base file.
18663
18664 To determine the base file name, we scan the macro definitions until we
18665 reach the first DW_MACINFO_start_file entry. We then initialize
18666 CURRENT_FILE accordingly so that any macro definition found before the
18667 first DW_MACINFO_start_file can still be associated to the base file. */
18668
18669 mac_ptr = section->buffer + offset;
18670 mac_end = section->buffer + section->size;
18671
18672 mac_ptr = dwarf_parse_macro_header (opcode_definitions, abfd, mac_ptr,
18673 &offset_size, section_is_gnu);
18674 if (mac_ptr == NULL)
18675 {
18676 /* We already issued a complaint. */
18677 return;
18678 }
18679
18680 do
18681 {
18682 /* Do we at least have room for a macinfo type byte? */
18683 if (mac_ptr >= mac_end)
18684 {
18685 /* Complaint is printed during the second pass as GDB will probably
18686 stop the first pass earlier upon finding
18687 DW_MACINFO_start_file. */
18688 break;
18689 }
18690
18691 macinfo_type = read_1_byte (abfd, mac_ptr);
18692 mac_ptr++;
18693
18694 /* Note that we rely on the fact that the corresponding GNU and
18695 DWARF constants are the same. */
18696 switch (macinfo_type)
18697 {
18698 /* A zero macinfo type indicates the end of the macro
18699 information. */
18700 case 0:
18701 break;
18702
18703 case DW_MACRO_GNU_define:
18704 case DW_MACRO_GNU_undef:
18705 /* Only skip the data by MAC_PTR. */
18706 {
18707 unsigned int bytes_read;
18708
18709 read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
18710 mac_ptr += bytes_read;
18711 read_direct_string (abfd, mac_ptr, &bytes_read);
18712 mac_ptr += bytes_read;
18713 }
18714 break;
18715
18716 case DW_MACRO_GNU_start_file:
18717 {
18718 unsigned int bytes_read;
18719 int line, file;
18720
18721 line = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
18722 mac_ptr += bytes_read;
18723 file = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
18724 mac_ptr += bytes_read;
18725
18726 current_file = macro_start_file (file, line, current_file,
bb5ed363 18727 comp_dir, lh, objfile);
cf2c3c16
TT
18728 }
18729 break;
18730
18731 case DW_MACRO_GNU_end_file:
18732 /* No data to skip by MAC_PTR. */
18733 break;
18734
18735 case DW_MACRO_GNU_define_indirect:
18736 case DW_MACRO_GNU_undef_indirect:
f7a35f02
TT
18737 case DW_MACRO_GNU_define_indirect_alt:
18738 case DW_MACRO_GNU_undef_indirect_alt:
cf2c3c16
TT
18739 {
18740 unsigned int bytes_read;
18741
18742 read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
18743 mac_ptr += bytes_read;
18744 mac_ptr += offset_size;
18745 }
18746 break;
18747
18748 case DW_MACRO_GNU_transparent_include:
f7a35f02 18749 case DW_MACRO_GNU_transparent_include_alt:
cf2c3c16
TT
18750 /* Note that, according to the spec, a transparent include
18751 chain cannot call DW_MACRO_GNU_start_file. So, we can just
18752 skip this opcode. */
18753 mac_ptr += offset_size;
18754 break;
18755
18756 case DW_MACINFO_vendor_ext:
18757 /* Only skip the data by MAC_PTR. */
18758 if (!section_is_gnu)
18759 {
18760 unsigned int bytes_read;
18761
18762 read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
18763 mac_ptr += bytes_read;
18764 read_direct_string (abfd, mac_ptr, &bytes_read);
18765 mac_ptr += bytes_read;
18766 }
18767 /* FALLTHROUGH */
18768
18769 default:
18770 mac_ptr = skip_unknown_opcode (macinfo_type, opcode_definitions,
f664829e 18771 mac_ptr, mac_end, abfd, offset_size,
cf2c3c16
TT
18772 section);
18773 if (mac_ptr == NULL)
18774 return;
18775 break;
18776 }
18777 } while (macinfo_type != 0 && current_file == NULL);
18778
18779 /* Second pass: Process all entries.
18780
18781 Use the AT_COMMAND_LINE flag to determine whether we are still processing
18782 command-line macro definitions/undefinitions. This flag is unset when we
18783 reach the first DW_MACINFO_start_file entry. */
18784
8fc3fc34
TT
18785 include_hash = htab_create_alloc (1, htab_hash_pointer, htab_eq_pointer,
18786 NULL, xcalloc, xfree);
18787 cleanup = make_cleanup_htab_delete (include_hash);
18788 mac_ptr = section->buffer + offset;
18789 slot = htab_find_slot (include_hash, mac_ptr, INSERT);
18790 *slot = mac_ptr;
18791 dwarf_decode_macro_bytes (abfd, mac_ptr, mac_end,
36586728
TT
18792 current_file, lh, comp_dir, section,
18793 section_is_gnu, 0,
8fc3fc34
TT
18794 offset_size, objfile, include_hash);
18795 do_cleanups (cleanup);
cf2c3c16
TT
18796}
18797
8e19ed76 18798/* Check if the attribute's form is a DW_FORM_block*
0963b4bd 18799 if so return true else false. */
380bca97 18800
8e19ed76
PS
18801static int
18802attr_form_is_block (struct attribute *attr)
18803{
18804 return (attr == NULL ? 0 :
18805 attr->form == DW_FORM_block1
18806 || attr->form == DW_FORM_block2
18807 || attr->form == DW_FORM_block4
2dc7f7b3
TT
18808 || attr->form == DW_FORM_block
18809 || attr->form == DW_FORM_exprloc);
8e19ed76 18810}
4c2df51b 18811
c6a0999f
JB
18812/* Return non-zero if ATTR's value is a section offset --- classes
18813 lineptr, loclistptr, macptr or rangelistptr --- or zero, otherwise.
18814 You may use DW_UNSND (attr) to retrieve such offsets.
18815
18816 Section 7.5.4, "Attribute Encodings", explains that no attribute
18817 may have a value that belongs to more than one of these classes; it
18818 would be ambiguous if we did, because we use the same forms for all
18819 of them. */
380bca97 18820
3690dd37
JB
18821static int
18822attr_form_is_section_offset (struct attribute *attr)
18823{
18824 return (attr->form == DW_FORM_data4
2dc7f7b3
TT
18825 || attr->form == DW_FORM_data8
18826 || attr->form == DW_FORM_sec_offset);
3690dd37
JB
18827}
18828
3690dd37
JB
18829/* Return non-zero if ATTR's value falls in the 'constant' class, or
18830 zero otherwise. When this function returns true, you can apply
18831 dwarf2_get_attr_constant_value to it.
18832
18833 However, note that for some attributes you must check
18834 attr_form_is_section_offset before using this test. DW_FORM_data4
18835 and DW_FORM_data8 are members of both the constant class, and of
18836 the classes that contain offsets into other debug sections
18837 (lineptr, loclistptr, macptr or rangelistptr). The DWARF spec says
18838 that, if an attribute's can be either a constant or one of the
18839 section offset classes, DW_FORM_data4 and DW_FORM_data8 should be
18840 taken as section offsets, not constants. */
380bca97 18841
3690dd37
JB
18842static int
18843attr_form_is_constant (struct attribute *attr)
18844{
18845 switch (attr->form)
18846 {
18847 case DW_FORM_sdata:
18848 case DW_FORM_udata:
18849 case DW_FORM_data1:
18850 case DW_FORM_data2:
18851 case DW_FORM_data4:
18852 case DW_FORM_data8:
18853 return 1;
18854 default:
18855 return 0;
18856 }
18857}
18858
3019eac3
DE
18859/* Return the .debug_loc section to use for CU.
18860 For DWO files use .debug_loc.dwo. */
18861
18862static struct dwarf2_section_info *
18863cu_debug_loc_section (struct dwarf2_cu *cu)
18864{
18865 if (cu->dwo_unit)
18866 return &cu->dwo_unit->dwo_file->sections.loc;
18867 return &dwarf2_per_objfile->loc;
18868}
18869
8cf6f0b1
TT
18870/* A helper function that fills in a dwarf2_loclist_baton. */
18871
18872static void
18873fill_in_loclist_baton (struct dwarf2_cu *cu,
18874 struct dwarf2_loclist_baton *baton,
18875 struct attribute *attr)
18876{
3019eac3
DE
18877 struct dwarf2_section_info *section = cu_debug_loc_section (cu);
18878
18879 dwarf2_read_section (dwarf2_per_objfile->objfile, section);
8cf6f0b1
TT
18880
18881 baton->per_cu = cu->per_cu;
18882 gdb_assert (baton->per_cu);
18883 /* We don't know how long the location list is, but make sure we
18884 don't run off the edge of the section. */
3019eac3
DE
18885 baton->size = section->size - DW_UNSND (attr);
18886 baton->data = section->buffer + DW_UNSND (attr);
8cf6f0b1 18887 baton->base_address = cu->base_address;
f664829e 18888 baton->from_dwo = cu->dwo_unit != NULL;
8cf6f0b1
TT
18889}
18890
4c2df51b
DJ
18891static void
18892dwarf2_symbol_mark_computed (struct attribute *attr, struct symbol *sym,
e7c27a73 18893 struct dwarf2_cu *cu)
4c2df51b 18894{
bb5ed363 18895 struct objfile *objfile = dwarf2_per_objfile->objfile;
3019eac3 18896 struct dwarf2_section_info *section = cu_debug_loc_section (cu);
bb5ed363 18897
3690dd37 18898 if (attr_form_is_section_offset (attr)
3019eac3 18899 /* .debug_loc{,.dwo} may not exist at all, or the offset may be outside
99bcc461
DJ
18900 the section. If so, fall through to the complaint in the
18901 other branch. */
3019eac3 18902 && DW_UNSND (attr) < dwarf2_section_size (objfile, section))
4c2df51b 18903 {
0d53c4c4 18904 struct dwarf2_loclist_baton *baton;
4c2df51b 18905
bb5ed363 18906 baton = obstack_alloc (&objfile->objfile_obstack,
0d53c4c4 18907 sizeof (struct dwarf2_loclist_baton));
4c2df51b 18908
8cf6f0b1 18909 fill_in_loclist_baton (cu, baton, attr);
be391dca 18910
d00adf39 18911 if (cu->base_known == 0)
0d53c4c4 18912 complaint (&symfile_complaints,
3e43a32a
MS
18913 _("Location list used without "
18914 "specifying the CU base address."));
4c2df51b 18915
768a979c 18916 SYMBOL_COMPUTED_OPS (sym) = &dwarf2_loclist_funcs;
0d53c4c4
DJ
18917 SYMBOL_LOCATION_BATON (sym) = baton;
18918 }
18919 else
18920 {
18921 struct dwarf2_locexpr_baton *baton;
18922
bb5ed363 18923 baton = obstack_alloc (&objfile->objfile_obstack,
0d53c4c4 18924 sizeof (struct dwarf2_locexpr_baton));
ae0d2f24
UW
18925 baton->per_cu = cu->per_cu;
18926 gdb_assert (baton->per_cu);
0d53c4c4
DJ
18927
18928 if (attr_form_is_block (attr))
18929 {
18930 /* Note that we're just copying the block's data pointer
18931 here, not the actual data. We're still pointing into the
6502dd73
DJ
18932 info_buffer for SYM's objfile; right now we never release
18933 that buffer, but when we do clean up properly this may
18934 need to change. */
0d53c4c4
DJ
18935 baton->size = DW_BLOCK (attr)->size;
18936 baton->data = DW_BLOCK (attr)->data;
18937 }
18938 else
18939 {
18940 dwarf2_invalid_attrib_class_complaint ("location description",
18941 SYMBOL_NATURAL_NAME (sym));
18942 baton->size = 0;
0d53c4c4 18943 }
6e70227d 18944
768a979c 18945 SYMBOL_COMPUTED_OPS (sym) = &dwarf2_locexpr_funcs;
0d53c4c4
DJ
18946 SYMBOL_LOCATION_BATON (sym) = baton;
18947 }
4c2df51b 18948}
6502dd73 18949
9aa1f1e3
TT
18950/* Return the OBJFILE associated with the compilation unit CU. If CU
18951 came from a separate debuginfo file, then the master objfile is
18952 returned. */
ae0d2f24
UW
18953
18954struct objfile *
18955dwarf2_per_cu_objfile (struct dwarf2_per_cu_data *per_cu)
18956{
9291a0cd 18957 struct objfile *objfile = per_cu->objfile;
ae0d2f24
UW
18958
18959 /* Return the master objfile, so that we can report and look up the
18960 correct file containing this variable. */
18961 if (objfile->separate_debug_objfile_backlink)
18962 objfile = objfile->separate_debug_objfile_backlink;
18963
18964 return objfile;
18965}
18966
96408a79
SA
18967/* Return comp_unit_head for PER_CU, either already available in PER_CU->CU
18968 (CU_HEADERP is unused in such case) or prepare a temporary copy at
18969 CU_HEADERP first. */
18970
18971static const struct comp_unit_head *
18972per_cu_header_read_in (struct comp_unit_head *cu_headerp,
18973 struct dwarf2_per_cu_data *per_cu)
18974{
96408a79
SA
18975 gdb_byte *info_ptr;
18976
18977 if (per_cu->cu)
18978 return &per_cu->cu->header;
18979
0bc3a05c 18980 info_ptr = per_cu->info_or_types_section->buffer + per_cu->offset.sect_off;
96408a79
SA
18981
18982 memset (cu_headerp, 0, sizeof (*cu_headerp));
0bc3a05c 18983 read_comp_unit_head (cu_headerp, info_ptr, per_cu->objfile->obfd);
96408a79
SA
18984
18985 return cu_headerp;
18986}
18987
ae0d2f24
UW
18988/* Return the address size given in the compilation unit header for CU. */
18989
98714339 18990int
ae0d2f24
UW
18991dwarf2_per_cu_addr_size (struct dwarf2_per_cu_data *per_cu)
18992{
96408a79
SA
18993 struct comp_unit_head cu_header_local;
18994 const struct comp_unit_head *cu_headerp;
c471e790 18995
96408a79
SA
18996 cu_headerp = per_cu_header_read_in (&cu_header_local, per_cu);
18997
18998 return cu_headerp->addr_size;
ae0d2f24
UW
18999}
19000
9eae7c52
TT
19001/* Return the offset size given in the compilation unit header for CU. */
19002
19003int
19004dwarf2_per_cu_offset_size (struct dwarf2_per_cu_data *per_cu)
19005{
96408a79
SA
19006 struct comp_unit_head cu_header_local;
19007 const struct comp_unit_head *cu_headerp;
9c6c53f7 19008
96408a79
SA
19009 cu_headerp = per_cu_header_read_in (&cu_header_local, per_cu);
19010
19011 return cu_headerp->offset_size;
19012}
19013
19014/* See its dwarf2loc.h declaration. */
19015
19016int
19017dwarf2_per_cu_ref_addr_size (struct dwarf2_per_cu_data *per_cu)
19018{
19019 struct comp_unit_head cu_header_local;
19020 const struct comp_unit_head *cu_headerp;
19021
19022 cu_headerp = per_cu_header_read_in (&cu_header_local, per_cu);
19023
19024 if (cu_headerp->version == 2)
19025 return cu_headerp->addr_size;
19026 else
19027 return cu_headerp->offset_size;
181cebd4
JK
19028}
19029
9aa1f1e3
TT
19030/* Return the text offset of the CU. The returned offset comes from
19031 this CU's objfile. If this objfile came from a separate debuginfo
19032 file, then the offset may be different from the corresponding
19033 offset in the parent objfile. */
19034
19035CORE_ADDR
19036dwarf2_per_cu_text_offset (struct dwarf2_per_cu_data *per_cu)
19037{
bb3fa9d0 19038 struct objfile *objfile = per_cu->objfile;
9aa1f1e3
TT
19039
19040 return ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
19041}
19042
348e048f
DE
19043/* Locate the .debug_info compilation unit from CU's objfile which contains
19044 the DIE at OFFSET. Raises an error on failure. */
ae038cb0
DJ
19045
19046static struct dwarf2_per_cu_data *
b64f50a1 19047dwarf2_find_containing_comp_unit (sect_offset offset,
36586728 19048 unsigned int offset_in_dwz,
ae038cb0
DJ
19049 struct objfile *objfile)
19050{
19051 struct dwarf2_per_cu_data *this_cu;
19052 int low, high;
36586728 19053 const sect_offset *cu_off;
ae038cb0 19054
ae038cb0
DJ
19055 low = 0;
19056 high = dwarf2_per_objfile->n_comp_units - 1;
19057 while (high > low)
19058 {
36586728 19059 struct dwarf2_per_cu_data *mid_cu;
ae038cb0 19060 int mid = low + (high - low) / 2;
9a619af0 19061
36586728
TT
19062 mid_cu = dwarf2_per_objfile->all_comp_units[mid];
19063 cu_off = &mid_cu->offset;
19064 if (mid_cu->is_dwz > offset_in_dwz
19065 || (mid_cu->is_dwz == offset_in_dwz
19066 && cu_off->sect_off >= offset.sect_off))
ae038cb0
DJ
19067 high = mid;
19068 else
19069 low = mid + 1;
19070 }
19071 gdb_assert (low == high);
36586728
TT
19072 this_cu = dwarf2_per_objfile->all_comp_units[low];
19073 cu_off = &this_cu->offset;
19074 if (this_cu->is_dwz != offset_in_dwz || cu_off->sect_off > offset.sect_off)
ae038cb0 19075 {
36586728 19076 if (low == 0 || this_cu->is_dwz != offset_in_dwz)
8a3fe4f8
AC
19077 error (_("Dwarf Error: could not find partial DIE containing "
19078 "offset 0x%lx [in module %s]"),
b64f50a1 19079 (long) offset.sect_off, bfd_get_filename (objfile->obfd));
10b3939b 19080
b64f50a1
JK
19081 gdb_assert (dwarf2_per_objfile->all_comp_units[low-1]->offset.sect_off
19082 <= offset.sect_off);
ae038cb0
DJ
19083 return dwarf2_per_objfile->all_comp_units[low-1];
19084 }
19085 else
19086 {
19087 this_cu = dwarf2_per_objfile->all_comp_units[low];
19088 if (low == dwarf2_per_objfile->n_comp_units - 1
b64f50a1
JK
19089 && offset.sect_off >= this_cu->offset.sect_off + this_cu->length)
19090 error (_("invalid dwarf2 offset %u"), offset.sect_off);
19091 gdb_assert (offset.sect_off < this_cu->offset.sect_off + this_cu->length);
ae038cb0
DJ
19092 return this_cu;
19093 }
19094}
19095
23745b47 19096/* Initialize dwarf2_cu CU, owned by PER_CU. */
93311388 19097
9816fde3 19098static void
23745b47 19099init_one_comp_unit (struct dwarf2_cu *cu, struct dwarf2_per_cu_data *per_cu)
93311388 19100{
9816fde3 19101 memset (cu, 0, sizeof (*cu));
23745b47
DE
19102 per_cu->cu = cu;
19103 cu->per_cu = per_cu;
19104 cu->objfile = per_cu->objfile;
93311388 19105 obstack_init (&cu->comp_unit_obstack);
9816fde3
JK
19106}
19107
19108/* Initialize basic fields of dwarf_cu CU according to DIE COMP_UNIT_DIE. */
19109
19110static void
95554aad
TT
19111prepare_one_comp_unit (struct dwarf2_cu *cu, struct die_info *comp_unit_die,
19112 enum language pretend_language)
9816fde3
JK
19113{
19114 struct attribute *attr;
19115
19116 /* Set the language we're debugging. */
19117 attr = dwarf2_attr (comp_unit_die, DW_AT_language, cu);
19118 if (attr)
19119 set_cu_language (DW_UNSND (attr), cu);
19120 else
9cded63f 19121 {
95554aad 19122 cu->language = pretend_language;
9cded63f
TT
19123 cu->language_defn = language_def (cu->language);
19124 }
dee91e82
DE
19125
19126 attr = dwarf2_attr (comp_unit_die, DW_AT_producer, cu);
19127 if (attr)
19128 cu->producer = DW_STRING (attr);
93311388
DE
19129}
19130
ae038cb0
DJ
19131/* Release one cached compilation unit, CU. We unlink it from the tree
19132 of compilation units, but we don't remove it from the read_in_chain;
93311388
DE
19133 the caller is responsible for that.
19134 NOTE: DATA is a void * because this function is also used as a
19135 cleanup routine. */
ae038cb0
DJ
19136
19137static void
68dc6402 19138free_heap_comp_unit (void *data)
ae038cb0
DJ
19139{
19140 struct dwarf2_cu *cu = data;
19141
23745b47
DE
19142 gdb_assert (cu->per_cu != NULL);
19143 cu->per_cu->cu = NULL;
ae038cb0
DJ
19144 cu->per_cu = NULL;
19145
19146 obstack_free (&cu->comp_unit_obstack, NULL);
19147
19148 xfree (cu);
19149}
19150
72bf9492 19151/* This cleanup function is passed the address of a dwarf2_cu on the stack
ae038cb0 19152 when we're finished with it. We can't free the pointer itself, but be
dee91e82 19153 sure to unlink it from the cache. Also release any associated storage. */
72bf9492
DJ
19154
19155static void
19156free_stack_comp_unit (void *data)
19157{
19158 struct dwarf2_cu *cu = data;
19159
23745b47
DE
19160 gdb_assert (cu->per_cu != NULL);
19161 cu->per_cu->cu = NULL;
19162 cu->per_cu = NULL;
19163
72bf9492
DJ
19164 obstack_free (&cu->comp_unit_obstack, NULL);
19165 cu->partial_dies = NULL;
ae038cb0
DJ
19166}
19167
19168/* Free all cached compilation units. */
19169
19170static void
19171free_cached_comp_units (void *data)
19172{
19173 struct dwarf2_per_cu_data *per_cu, **last_chain;
19174
19175 per_cu = dwarf2_per_objfile->read_in_chain;
19176 last_chain = &dwarf2_per_objfile->read_in_chain;
19177 while (per_cu != NULL)
19178 {
19179 struct dwarf2_per_cu_data *next_cu;
19180
19181 next_cu = per_cu->cu->read_in_chain;
19182
68dc6402 19183 free_heap_comp_unit (per_cu->cu);
ae038cb0
DJ
19184 *last_chain = next_cu;
19185
19186 per_cu = next_cu;
19187 }
19188}
19189
19190/* Increase the age counter on each cached compilation unit, and free
19191 any that are too old. */
19192
19193static void
19194age_cached_comp_units (void)
19195{
19196 struct dwarf2_per_cu_data *per_cu, **last_chain;
19197
19198 dwarf2_clear_marks (dwarf2_per_objfile->read_in_chain);
19199 per_cu = dwarf2_per_objfile->read_in_chain;
19200 while (per_cu != NULL)
19201 {
19202 per_cu->cu->last_used ++;
19203 if (per_cu->cu->last_used <= dwarf2_max_cache_age)
19204 dwarf2_mark (per_cu->cu);
19205 per_cu = per_cu->cu->read_in_chain;
19206 }
19207
19208 per_cu = dwarf2_per_objfile->read_in_chain;
19209 last_chain = &dwarf2_per_objfile->read_in_chain;
19210 while (per_cu != NULL)
19211 {
19212 struct dwarf2_per_cu_data *next_cu;
19213
19214 next_cu = per_cu->cu->read_in_chain;
19215
19216 if (!per_cu->cu->mark)
19217 {
68dc6402 19218 free_heap_comp_unit (per_cu->cu);
ae038cb0
DJ
19219 *last_chain = next_cu;
19220 }
19221 else
19222 last_chain = &per_cu->cu->read_in_chain;
19223
19224 per_cu = next_cu;
19225 }
19226}
19227
19228/* Remove a single compilation unit from the cache. */
19229
19230static void
dee91e82 19231free_one_cached_comp_unit (struct dwarf2_per_cu_data *target_per_cu)
ae038cb0
DJ
19232{
19233 struct dwarf2_per_cu_data *per_cu, **last_chain;
19234
19235 per_cu = dwarf2_per_objfile->read_in_chain;
19236 last_chain = &dwarf2_per_objfile->read_in_chain;
19237 while (per_cu != NULL)
19238 {
19239 struct dwarf2_per_cu_data *next_cu;
19240
19241 next_cu = per_cu->cu->read_in_chain;
19242
dee91e82 19243 if (per_cu == target_per_cu)
ae038cb0 19244 {
68dc6402 19245 free_heap_comp_unit (per_cu->cu);
dee91e82 19246 per_cu->cu = NULL;
ae038cb0
DJ
19247 *last_chain = next_cu;
19248 break;
19249 }
19250 else
19251 last_chain = &per_cu->cu->read_in_chain;
19252
19253 per_cu = next_cu;
19254 }
19255}
19256
fe3e1990
DJ
19257/* Release all extra memory associated with OBJFILE. */
19258
19259void
19260dwarf2_free_objfile (struct objfile *objfile)
19261{
19262 dwarf2_per_objfile = objfile_data (objfile, dwarf2_objfile_data_key);
19263
19264 if (dwarf2_per_objfile == NULL)
19265 return;
19266
19267 /* Cached DIE trees use xmalloc and the comp_unit_obstack. */
19268 free_cached_comp_units (NULL);
19269
7b9f3c50
DE
19270 if (dwarf2_per_objfile->quick_file_names_table)
19271 htab_delete (dwarf2_per_objfile->quick_file_names_table);
9291a0cd 19272
fe3e1990
DJ
19273 /* Everything else should be on the objfile obstack. */
19274}
19275
dee91e82
DE
19276/* A set of CU "per_cu" pointer, DIE offset, and GDB type pointer.
19277 We store these in a hash table separate from the DIEs, and preserve them
19278 when the DIEs are flushed out of cache.
19279
19280 The CU "per_cu" pointer is needed because offset alone is not enough to
3019eac3
DE
19281 uniquely identify the type. A file may have multiple .debug_types sections,
19282 or the type may come from a DWO file. We have to use something in
19283 dwarf2_per_cu_data (or the pointer to it) because we can enter the lookup
19284 routine, get_die_type_at_offset, from outside this file, and thus won't
19285 necessarily have PER_CU->cu. Fortunately, PER_CU is stable for the life
19286 of the objfile. */
1c379e20 19287
dee91e82 19288struct dwarf2_per_cu_offset_and_type
1c379e20 19289{
dee91e82 19290 const struct dwarf2_per_cu_data *per_cu;
b64f50a1 19291 sect_offset offset;
1c379e20
DJ
19292 struct type *type;
19293};
19294
dee91e82 19295/* Hash function for a dwarf2_per_cu_offset_and_type. */
1c379e20
DJ
19296
19297static hashval_t
dee91e82 19298per_cu_offset_and_type_hash (const void *item)
1c379e20 19299{
dee91e82 19300 const struct dwarf2_per_cu_offset_and_type *ofs = item;
9a619af0 19301
dee91e82 19302 return (uintptr_t) ofs->per_cu + ofs->offset.sect_off;
1c379e20
DJ
19303}
19304
dee91e82 19305/* Equality function for a dwarf2_per_cu_offset_and_type. */
1c379e20
DJ
19306
19307static int
dee91e82 19308per_cu_offset_and_type_eq (const void *item_lhs, const void *item_rhs)
1c379e20 19309{
dee91e82
DE
19310 const struct dwarf2_per_cu_offset_and_type *ofs_lhs = item_lhs;
19311 const struct dwarf2_per_cu_offset_and_type *ofs_rhs = item_rhs;
9a619af0 19312
dee91e82
DE
19313 return (ofs_lhs->per_cu == ofs_rhs->per_cu
19314 && ofs_lhs->offset.sect_off == ofs_rhs->offset.sect_off);
1c379e20
DJ
19315}
19316
19317/* Set the type associated with DIE to TYPE. Save it in CU's hash
7e314c57
JK
19318 table if necessary. For convenience, return TYPE.
19319
19320 The DIEs reading must have careful ordering to:
19321 * Not cause infite loops trying to read in DIEs as a prerequisite for
19322 reading current DIE.
19323 * Not trying to dereference contents of still incompletely read in types
19324 while reading in other DIEs.
19325 * Enable referencing still incompletely read in types just by a pointer to
19326 the type without accessing its fields.
19327
19328 Therefore caller should follow these rules:
19329 * Try to fetch any prerequisite types we may need to build this DIE type
19330 before building the type and calling set_die_type.
e71ec853 19331 * After building type call set_die_type for current DIE as soon as
7e314c57
JK
19332 possible before fetching more types to complete the current type.
19333 * Make the type as complete as possible before fetching more types. */
1c379e20 19334
f792889a 19335static struct type *
1c379e20
DJ
19336set_die_type (struct die_info *die, struct type *type, struct dwarf2_cu *cu)
19337{
dee91e82 19338 struct dwarf2_per_cu_offset_and_type **slot, ofs;
673bfd45 19339 struct objfile *objfile = cu->objfile;
1c379e20 19340
b4ba55a1
JB
19341 /* For Ada types, make sure that the gnat-specific data is always
19342 initialized (if not already set). There are a few types where
19343 we should not be doing so, because the type-specific area is
19344 already used to hold some other piece of info (eg: TYPE_CODE_FLT
19345 where the type-specific area is used to store the floatformat).
19346 But this is not a problem, because the gnat-specific information
19347 is actually not needed for these types. */
19348 if (need_gnat_info (cu)
19349 && TYPE_CODE (type) != TYPE_CODE_FUNC
19350 && TYPE_CODE (type) != TYPE_CODE_FLT
19351 && !HAVE_GNAT_AUX_INFO (type))
19352 INIT_GNAT_SPECIFIC (type);
19353
dee91e82 19354 if (dwarf2_per_objfile->die_type_hash == NULL)
f792889a 19355 {
dee91e82
DE
19356 dwarf2_per_objfile->die_type_hash =
19357 htab_create_alloc_ex (127,
19358 per_cu_offset_and_type_hash,
19359 per_cu_offset_and_type_eq,
19360 NULL,
19361 &objfile->objfile_obstack,
19362 hashtab_obstack_allocate,
19363 dummy_obstack_deallocate);
f792889a 19364 }
1c379e20 19365
dee91e82 19366 ofs.per_cu = cu->per_cu;
1c379e20
DJ
19367 ofs.offset = die->offset;
19368 ofs.type = type;
dee91e82
DE
19369 slot = (struct dwarf2_per_cu_offset_and_type **)
19370 htab_find_slot (dwarf2_per_objfile->die_type_hash, &ofs, INSERT);
7e314c57
JK
19371 if (*slot)
19372 complaint (&symfile_complaints,
19373 _("A problem internal to GDB: DIE 0x%x has type already set"),
b64f50a1 19374 die->offset.sect_off);
673bfd45 19375 *slot = obstack_alloc (&objfile->objfile_obstack, sizeof (**slot));
1c379e20 19376 **slot = ofs;
f792889a 19377 return type;
1c379e20
DJ
19378}
19379
380bca97 19380/* Look up the type for the die at OFFSET in the appropriate type_hash
673bfd45 19381 table, or return NULL if the die does not have a saved type. */
1c379e20
DJ
19382
19383static struct type *
b64f50a1 19384get_die_type_at_offset (sect_offset offset,
673bfd45 19385 struct dwarf2_per_cu_data *per_cu)
1c379e20 19386{
dee91e82 19387 struct dwarf2_per_cu_offset_and_type *slot, ofs;
f792889a 19388
dee91e82 19389 if (dwarf2_per_objfile->die_type_hash == NULL)
f792889a 19390 return NULL;
1c379e20 19391
dee91e82 19392 ofs.per_cu = per_cu;
673bfd45 19393 ofs.offset = offset;
dee91e82 19394 slot = htab_find (dwarf2_per_objfile->die_type_hash, &ofs);
1c379e20
DJ
19395 if (slot)
19396 return slot->type;
19397 else
19398 return NULL;
19399}
19400
673bfd45
DE
19401/* Look up the type for DIE in the appropriate type_hash table,
19402 or return NULL if DIE does not have a saved type. */
19403
19404static struct type *
19405get_die_type (struct die_info *die, struct dwarf2_cu *cu)
19406{
19407 return get_die_type_at_offset (die->offset, cu->per_cu);
19408}
19409
10b3939b
DJ
19410/* Add a dependence relationship from CU to REF_PER_CU. */
19411
19412static void
19413dwarf2_add_dependence (struct dwarf2_cu *cu,
19414 struct dwarf2_per_cu_data *ref_per_cu)
19415{
19416 void **slot;
19417
19418 if (cu->dependencies == NULL)
19419 cu->dependencies
19420 = htab_create_alloc_ex (5, htab_hash_pointer, htab_eq_pointer,
19421 NULL, &cu->comp_unit_obstack,
19422 hashtab_obstack_allocate,
19423 dummy_obstack_deallocate);
19424
19425 slot = htab_find_slot (cu->dependencies, ref_per_cu, INSERT);
19426 if (*slot == NULL)
19427 *slot = ref_per_cu;
19428}
1c379e20 19429
f504f079
DE
19430/* Subroutine of dwarf2_mark to pass to htab_traverse.
19431 Set the mark field in every compilation unit in the
ae038cb0
DJ
19432 cache that we must keep because we are keeping CU. */
19433
10b3939b
DJ
19434static int
19435dwarf2_mark_helper (void **slot, void *data)
19436{
19437 struct dwarf2_per_cu_data *per_cu;
19438
19439 per_cu = (struct dwarf2_per_cu_data *) *slot;
d07ed419
JK
19440
19441 /* cu->dependencies references may not yet have been ever read if QUIT aborts
19442 reading of the chain. As such dependencies remain valid it is not much
19443 useful to track and undo them during QUIT cleanups. */
19444 if (per_cu->cu == NULL)
19445 return 1;
19446
10b3939b
DJ
19447 if (per_cu->cu->mark)
19448 return 1;
19449 per_cu->cu->mark = 1;
19450
19451 if (per_cu->cu->dependencies != NULL)
19452 htab_traverse (per_cu->cu->dependencies, dwarf2_mark_helper, NULL);
19453
19454 return 1;
19455}
19456
f504f079
DE
19457/* Set the mark field in CU and in every other compilation unit in the
19458 cache that we must keep because we are keeping CU. */
19459
ae038cb0
DJ
19460static void
19461dwarf2_mark (struct dwarf2_cu *cu)
19462{
19463 if (cu->mark)
19464 return;
19465 cu->mark = 1;
10b3939b
DJ
19466 if (cu->dependencies != NULL)
19467 htab_traverse (cu->dependencies, dwarf2_mark_helper, NULL);
ae038cb0
DJ
19468}
19469
19470static void
19471dwarf2_clear_marks (struct dwarf2_per_cu_data *per_cu)
19472{
19473 while (per_cu)
19474 {
19475 per_cu->cu->mark = 0;
19476 per_cu = per_cu->cu->read_in_chain;
19477 }
72bf9492
DJ
19478}
19479
72bf9492
DJ
19480/* Trivial hash function for partial_die_info: the hash value of a DIE
19481 is its offset in .debug_info for this objfile. */
19482
19483static hashval_t
19484partial_die_hash (const void *item)
19485{
19486 const struct partial_die_info *part_die = item;
9a619af0 19487
b64f50a1 19488 return part_die->offset.sect_off;
72bf9492
DJ
19489}
19490
19491/* Trivial comparison function for partial_die_info structures: two DIEs
19492 are equal if they have the same offset. */
19493
19494static int
19495partial_die_eq (const void *item_lhs, const void *item_rhs)
19496{
19497 const struct partial_die_info *part_die_lhs = item_lhs;
19498 const struct partial_die_info *part_die_rhs = item_rhs;
9a619af0 19499
b64f50a1 19500 return part_die_lhs->offset.sect_off == part_die_rhs->offset.sect_off;
72bf9492
DJ
19501}
19502
ae038cb0
DJ
19503static struct cmd_list_element *set_dwarf2_cmdlist;
19504static struct cmd_list_element *show_dwarf2_cmdlist;
19505
19506static void
19507set_dwarf2_cmd (char *args, int from_tty)
19508{
19509 help_list (set_dwarf2_cmdlist, "maintenance set dwarf2 ", -1, gdb_stdout);
19510}
19511
19512static void
19513show_dwarf2_cmd (char *args, int from_tty)
6e70227d 19514{
ae038cb0
DJ
19515 cmd_show_list (show_dwarf2_cmdlist, from_tty, "");
19516}
19517
4bf44c1c 19518/* Free data associated with OBJFILE, if necessary. */
dce234bc
PP
19519
19520static void
c1bd65d0 19521dwarf2_per_objfile_free (struct objfile *objfile, void *d)
dce234bc
PP
19522{
19523 struct dwarf2_per_objfile *data = d;
8b70b953 19524 int ix;
8b70b953 19525
95554aad
TT
19526 for (ix = 0; ix < dwarf2_per_objfile->n_comp_units; ++ix)
19527 VEC_free (dwarf2_per_cu_ptr,
f4dc4d17 19528 dwarf2_per_objfile->all_comp_units[ix]->s.imported_symtabs);
95554aad 19529
8b70b953 19530 VEC_free (dwarf2_section_info_def, data->types);
3019eac3
DE
19531
19532 if (data->dwo_files)
19533 free_dwo_files (data->dwo_files, objfile);
36586728
TT
19534
19535 if (data->dwz_file && data->dwz_file->dwz_bfd)
19536 gdb_bfd_unref (data->dwz_file->dwz_bfd);
9291a0cd
TT
19537}
19538
19539\f
ae2de4f8 19540/* The "save gdb-index" command. */
9291a0cd
TT
19541
19542/* The contents of the hash table we create when building the string
19543 table. */
19544struct strtab_entry
19545{
19546 offset_type offset;
19547 const char *str;
19548};
19549
559a7a62
JK
19550/* Hash function for a strtab_entry.
19551
19552 Function is used only during write_hash_table so no index format backward
19553 compatibility is needed. */
b89be57b 19554
9291a0cd
TT
19555static hashval_t
19556hash_strtab_entry (const void *e)
19557{
19558 const struct strtab_entry *entry = e;
559a7a62 19559 return mapped_index_string_hash (INT_MAX, entry->str);
9291a0cd
TT
19560}
19561
19562/* Equality function for a strtab_entry. */
b89be57b 19563
9291a0cd
TT
19564static int
19565eq_strtab_entry (const void *a, const void *b)
19566{
19567 const struct strtab_entry *ea = a;
19568 const struct strtab_entry *eb = b;
19569 return !strcmp (ea->str, eb->str);
19570}
19571
19572/* Create a strtab_entry hash table. */
b89be57b 19573
9291a0cd
TT
19574static htab_t
19575create_strtab (void)
19576{
19577 return htab_create_alloc (100, hash_strtab_entry, eq_strtab_entry,
19578 xfree, xcalloc, xfree);
19579}
19580
19581/* Add a string to the constant pool. Return the string's offset in
19582 host order. */
b89be57b 19583
9291a0cd
TT
19584static offset_type
19585add_string (htab_t table, struct obstack *cpool, const char *str)
19586{
19587 void **slot;
19588 struct strtab_entry entry;
19589 struct strtab_entry *result;
19590
19591 entry.str = str;
19592 slot = htab_find_slot (table, &entry, INSERT);
19593 if (*slot)
19594 result = *slot;
19595 else
19596 {
19597 result = XNEW (struct strtab_entry);
19598 result->offset = obstack_object_size (cpool);
19599 result->str = str;
19600 obstack_grow_str0 (cpool, str);
19601 *slot = result;
19602 }
19603 return result->offset;
19604}
19605
19606/* An entry in the symbol table. */
19607struct symtab_index_entry
19608{
19609 /* The name of the symbol. */
19610 const char *name;
19611 /* The offset of the name in the constant pool. */
19612 offset_type index_offset;
19613 /* A sorted vector of the indices of all the CUs that hold an object
19614 of this name. */
19615 VEC (offset_type) *cu_indices;
19616};
19617
19618/* The symbol table. This is a power-of-2-sized hash table. */
19619struct mapped_symtab
19620{
19621 offset_type n_elements;
19622 offset_type size;
19623 struct symtab_index_entry **data;
19624};
19625
19626/* Hash function for a symtab_index_entry. */
b89be57b 19627
9291a0cd
TT
19628static hashval_t
19629hash_symtab_entry (const void *e)
19630{
19631 const struct symtab_index_entry *entry = e;
19632 return iterative_hash (VEC_address (offset_type, entry->cu_indices),
19633 sizeof (offset_type) * VEC_length (offset_type,
19634 entry->cu_indices),
19635 0);
19636}
19637
19638/* Equality function for a symtab_index_entry. */
b89be57b 19639
9291a0cd
TT
19640static int
19641eq_symtab_entry (const void *a, const void *b)
19642{
19643 const struct symtab_index_entry *ea = a;
19644 const struct symtab_index_entry *eb = b;
19645 int len = VEC_length (offset_type, ea->cu_indices);
19646 if (len != VEC_length (offset_type, eb->cu_indices))
19647 return 0;
19648 return !memcmp (VEC_address (offset_type, ea->cu_indices),
19649 VEC_address (offset_type, eb->cu_indices),
19650 sizeof (offset_type) * len);
19651}
19652
19653/* Destroy a symtab_index_entry. */
b89be57b 19654
9291a0cd
TT
19655static void
19656delete_symtab_entry (void *p)
19657{
19658 struct symtab_index_entry *entry = p;
19659 VEC_free (offset_type, entry->cu_indices);
19660 xfree (entry);
19661}
19662
19663/* Create a hash table holding symtab_index_entry objects. */
b89be57b 19664
9291a0cd 19665static htab_t
3876f04e 19666create_symbol_hash_table (void)
9291a0cd
TT
19667{
19668 return htab_create_alloc (100, hash_symtab_entry, eq_symtab_entry,
19669 delete_symtab_entry, xcalloc, xfree);
19670}
19671
19672/* Create a new mapped symtab object. */
b89be57b 19673
9291a0cd
TT
19674static struct mapped_symtab *
19675create_mapped_symtab (void)
19676{
19677 struct mapped_symtab *symtab = XNEW (struct mapped_symtab);
19678 symtab->n_elements = 0;
19679 symtab->size = 1024;
19680 symtab->data = XCNEWVEC (struct symtab_index_entry *, symtab->size);
19681 return symtab;
19682}
19683
19684/* Destroy a mapped_symtab. */
b89be57b 19685
9291a0cd
TT
19686static void
19687cleanup_mapped_symtab (void *p)
19688{
19689 struct mapped_symtab *symtab = p;
19690 /* The contents of the array are freed when the other hash table is
19691 destroyed. */
19692 xfree (symtab->data);
19693 xfree (symtab);
19694}
19695
19696/* Find a slot in SYMTAB for the symbol NAME. Returns a pointer to
559a7a62
JK
19697 the slot.
19698
19699 Function is used only during write_hash_table so no index format backward
19700 compatibility is needed. */
b89be57b 19701
9291a0cd
TT
19702static struct symtab_index_entry **
19703find_slot (struct mapped_symtab *symtab, const char *name)
19704{
559a7a62 19705 offset_type index, step, hash = mapped_index_string_hash (INT_MAX, name);
9291a0cd
TT
19706
19707 index = hash & (symtab->size - 1);
19708 step = ((hash * 17) & (symtab->size - 1)) | 1;
19709
19710 for (;;)
19711 {
19712 if (!symtab->data[index] || !strcmp (name, symtab->data[index]->name))
19713 return &symtab->data[index];
19714 index = (index + step) & (symtab->size - 1);
19715 }
19716}
19717
19718/* Expand SYMTAB's hash table. */
b89be57b 19719
9291a0cd
TT
19720static void
19721hash_expand (struct mapped_symtab *symtab)
19722{
19723 offset_type old_size = symtab->size;
19724 offset_type i;
19725 struct symtab_index_entry **old_entries = symtab->data;
19726
19727 symtab->size *= 2;
19728 symtab->data = XCNEWVEC (struct symtab_index_entry *, symtab->size);
19729
19730 for (i = 0; i < old_size; ++i)
19731 {
19732 if (old_entries[i])
19733 {
19734 struct symtab_index_entry **slot = find_slot (symtab,
19735 old_entries[i]->name);
19736 *slot = old_entries[i];
19737 }
19738 }
19739
19740 xfree (old_entries);
19741}
19742
156942c7
DE
19743/* Add an entry to SYMTAB. NAME is the name of the symbol.
19744 CU_INDEX is the index of the CU in which the symbol appears.
19745 IS_STATIC is one if the symbol is static, otherwise zero (global). */
b89be57b 19746
9291a0cd
TT
19747static void
19748add_index_entry (struct mapped_symtab *symtab, const char *name,
156942c7 19749 int is_static, gdb_index_symbol_kind kind,
9291a0cd
TT
19750 offset_type cu_index)
19751{
19752 struct symtab_index_entry **slot;
156942c7 19753 offset_type cu_index_and_attrs;
9291a0cd
TT
19754
19755 ++symtab->n_elements;
19756 if (4 * symtab->n_elements / 3 >= symtab->size)
19757 hash_expand (symtab);
19758
19759 slot = find_slot (symtab, name);
19760 if (!*slot)
19761 {
19762 *slot = XNEW (struct symtab_index_entry);
19763 (*slot)->name = name;
156942c7 19764 /* index_offset is set later. */
9291a0cd
TT
19765 (*slot)->cu_indices = NULL;
19766 }
156942c7
DE
19767
19768 cu_index_and_attrs = 0;
19769 DW2_GDB_INDEX_CU_SET_VALUE (cu_index_and_attrs, cu_index);
19770 DW2_GDB_INDEX_SYMBOL_STATIC_SET_VALUE (cu_index_and_attrs, is_static);
19771 DW2_GDB_INDEX_SYMBOL_KIND_SET_VALUE (cu_index_and_attrs, kind);
19772
19773 /* We don't want to record an index value twice as we want to avoid the
19774 duplication.
19775 We process all global symbols and then all static symbols
19776 (which would allow us to avoid the duplication by only having to check
19777 the last entry pushed), but a symbol could have multiple kinds in one CU.
19778 To keep things simple we don't worry about the duplication here and
19779 sort and uniqufy the list after we've processed all symbols. */
19780 VEC_safe_push (offset_type, (*slot)->cu_indices, cu_index_and_attrs);
19781}
19782
19783/* qsort helper routine for uniquify_cu_indices. */
19784
19785static int
19786offset_type_compare (const void *ap, const void *bp)
19787{
19788 offset_type a = *(offset_type *) ap;
19789 offset_type b = *(offset_type *) bp;
19790
19791 return (a > b) - (b > a);
19792}
19793
19794/* Sort and remove duplicates of all symbols' cu_indices lists. */
19795
19796static void
19797uniquify_cu_indices (struct mapped_symtab *symtab)
19798{
19799 int i;
19800
19801 for (i = 0; i < symtab->size; ++i)
19802 {
19803 struct symtab_index_entry *entry = symtab->data[i];
19804
19805 if (entry
19806 && entry->cu_indices != NULL)
19807 {
19808 unsigned int next_to_insert, next_to_check;
19809 offset_type last_value;
19810
19811 qsort (VEC_address (offset_type, entry->cu_indices),
19812 VEC_length (offset_type, entry->cu_indices),
19813 sizeof (offset_type), offset_type_compare);
19814
19815 last_value = VEC_index (offset_type, entry->cu_indices, 0);
19816 next_to_insert = 1;
19817 for (next_to_check = 1;
19818 next_to_check < VEC_length (offset_type, entry->cu_indices);
19819 ++next_to_check)
19820 {
19821 if (VEC_index (offset_type, entry->cu_indices, next_to_check)
19822 != last_value)
19823 {
19824 last_value = VEC_index (offset_type, entry->cu_indices,
19825 next_to_check);
19826 VEC_replace (offset_type, entry->cu_indices, next_to_insert,
19827 last_value);
19828 ++next_to_insert;
19829 }
19830 }
19831 VEC_truncate (offset_type, entry->cu_indices, next_to_insert);
19832 }
19833 }
9291a0cd
TT
19834}
19835
19836/* Add a vector of indices to the constant pool. */
b89be57b 19837
9291a0cd 19838static offset_type
3876f04e 19839add_indices_to_cpool (htab_t symbol_hash_table, struct obstack *cpool,
9291a0cd
TT
19840 struct symtab_index_entry *entry)
19841{
19842 void **slot;
19843
3876f04e 19844 slot = htab_find_slot (symbol_hash_table, entry, INSERT);
9291a0cd
TT
19845 if (!*slot)
19846 {
19847 offset_type len = VEC_length (offset_type, entry->cu_indices);
19848 offset_type val = MAYBE_SWAP (len);
19849 offset_type iter;
19850 int i;
19851
19852 *slot = entry;
19853 entry->index_offset = obstack_object_size (cpool);
19854
19855 obstack_grow (cpool, &val, sizeof (val));
19856 for (i = 0;
19857 VEC_iterate (offset_type, entry->cu_indices, i, iter);
19858 ++i)
19859 {
19860 val = MAYBE_SWAP (iter);
19861 obstack_grow (cpool, &val, sizeof (val));
19862 }
19863 }
19864 else
19865 {
19866 struct symtab_index_entry *old_entry = *slot;
19867 entry->index_offset = old_entry->index_offset;
19868 entry = old_entry;
19869 }
19870 return entry->index_offset;
19871}
19872
19873/* Write the mapped hash table SYMTAB to the obstack OUTPUT, with
19874 constant pool entries going into the obstack CPOOL. */
b89be57b 19875
9291a0cd
TT
19876static void
19877write_hash_table (struct mapped_symtab *symtab,
19878 struct obstack *output, struct obstack *cpool)
19879{
19880 offset_type i;
3876f04e 19881 htab_t symbol_hash_table;
9291a0cd
TT
19882 htab_t str_table;
19883
3876f04e 19884 symbol_hash_table = create_symbol_hash_table ();
9291a0cd 19885 str_table = create_strtab ();
3876f04e 19886
9291a0cd
TT
19887 /* We add all the index vectors to the constant pool first, to
19888 ensure alignment is ok. */
19889 for (i = 0; i < symtab->size; ++i)
19890 {
19891 if (symtab->data[i])
3876f04e 19892 add_indices_to_cpool (symbol_hash_table, cpool, symtab->data[i]);
9291a0cd
TT
19893 }
19894
19895 /* Now write out the hash table. */
19896 for (i = 0; i < symtab->size; ++i)
19897 {
19898 offset_type str_off, vec_off;
19899
19900 if (symtab->data[i])
19901 {
19902 str_off = add_string (str_table, cpool, symtab->data[i]->name);
19903 vec_off = symtab->data[i]->index_offset;
19904 }
19905 else
19906 {
19907 /* While 0 is a valid constant pool index, it is not valid
19908 to have 0 for both offsets. */
19909 str_off = 0;
19910 vec_off = 0;
19911 }
19912
19913 str_off = MAYBE_SWAP (str_off);
19914 vec_off = MAYBE_SWAP (vec_off);
19915
19916 obstack_grow (output, &str_off, sizeof (str_off));
19917 obstack_grow (output, &vec_off, sizeof (vec_off));
19918 }
19919
19920 htab_delete (str_table);
3876f04e 19921 htab_delete (symbol_hash_table);
9291a0cd
TT
19922}
19923
0a5429f6
DE
19924/* Struct to map psymtab to CU index in the index file. */
19925struct psymtab_cu_index_map
19926{
19927 struct partial_symtab *psymtab;
19928 unsigned int cu_index;
19929};
19930
19931static hashval_t
19932hash_psymtab_cu_index (const void *item)
19933{
19934 const struct psymtab_cu_index_map *map = item;
19935
19936 return htab_hash_pointer (map->psymtab);
19937}
19938
19939static int
19940eq_psymtab_cu_index (const void *item_lhs, const void *item_rhs)
19941{
19942 const struct psymtab_cu_index_map *lhs = item_lhs;
19943 const struct psymtab_cu_index_map *rhs = item_rhs;
19944
19945 return lhs->psymtab == rhs->psymtab;
19946}
19947
19948/* Helper struct for building the address table. */
19949struct addrmap_index_data
19950{
19951 struct objfile *objfile;
19952 struct obstack *addr_obstack;
19953 htab_t cu_index_htab;
19954
19955 /* Non-zero if the previous_* fields are valid.
19956 We can't write an entry until we see the next entry (since it is only then
19957 that we know the end of the entry). */
19958 int previous_valid;
19959 /* Index of the CU in the table of all CUs in the index file. */
19960 unsigned int previous_cu_index;
0963b4bd 19961 /* Start address of the CU. */
0a5429f6
DE
19962 CORE_ADDR previous_cu_start;
19963};
19964
19965/* Write an address entry to OBSTACK. */
b89be57b 19966
9291a0cd 19967static void
0a5429f6
DE
19968add_address_entry (struct objfile *objfile, struct obstack *obstack,
19969 CORE_ADDR start, CORE_ADDR end, unsigned int cu_index)
9291a0cd 19970{
0a5429f6 19971 offset_type cu_index_to_write;
9291a0cd
TT
19972 char addr[8];
19973 CORE_ADDR baseaddr;
19974
19975 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
19976
0a5429f6
DE
19977 store_unsigned_integer (addr, 8, BFD_ENDIAN_LITTLE, start - baseaddr);
19978 obstack_grow (obstack, addr, 8);
19979 store_unsigned_integer (addr, 8, BFD_ENDIAN_LITTLE, end - baseaddr);
19980 obstack_grow (obstack, addr, 8);
19981 cu_index_to_write = MAYBE_SWAP (cu_index);
19982 obstack_grow (obstack, &cu_index_to_write, sizeof (offset_type));
19983}
19984
19985/* Worker function for traversing an addrmap to build the address table. */
19986
19987static int
19988add_address_entry_worker (void *datap, CORE_ADDR start_addr, void *obj)
19989{
19990 struct addrmap_index_data *data = datap;
19991 struct partial_symtab *pst = obj;
0a5429f6
DE
19992
19993 if (data->previous_valid)
19994 add_address_entry (data->objfile, data->addr_obstack,
19995 data->previous_cu_start, start_addr,
19996 data->previous_cu_index);
19997
19998 data->previous_cu_start = start_addr;
19999 if (pst != NULL)
20000 {
20001 struct psymtab_cu_index_map find_map, *map;
20002 find_map.psymtab = pst;
20003 map = htab_find (data->cu_index_htab, &find_map);
20004 gdb_assert (map != NULL);
20005 data->previous_cu_index = map->cu_index;
20006 data->previous_valid = 1;
20007 }
20008 else
20009 data->previous_valid = 0;
20010
20011 return 0;
20012}
20013
20014/* Write OBJFILE's address map to OBSTACK.
20015 CU_INDEX_HTAB is used to map addrmap entries to their CU indices
20016 in the index file. */
20017
20018static void
20019write_address_map (struct objfile *objfile, struct obstack *obstack,
20020 htab_t cu_index_htab)
20021{
20022 struct addrmap_index_data addrmap_index_data;
20023
20024 /* When writing the address table, we have to cope with the fact that
20025 the addrmap iterator only provides the start of a region; we have to
20026 wait until the next invocation to get the start of the next region. */
20027
20028 addrmap_index_data.objfile = objfile;
20029 addrmap_index_data.addr_obstack = obstack;
20030 addrmap_index_data.cu_index_htab = cu_index_htab;
20031 addrmap_index_data.previous_valid = 0;
20032
20033 addrmap_foreach (objfile->psymtabs_addrmap, add_address_entry_worker,
20034 &addrmap_index_data);
20035
20036 /* It's highly unlikely the last entry (end address = 0xff...ff)
20037 is valid, but we should still handle it.
20038 The end address is recorded as the start of the next region, but that
20039 doesn't work here. To cope we pass 0xff...ff, this is a rare situation
20040 anyway. */
20041 if (addrmap_index_data.previous_valid)
20042 add_address_entry (objfile, obstack,
20043 addrmap_index_data.previous_cu_start, (CORE_ADDR) -1,
20044 addrmap_index_data.previous_cu_index);
9291a0cd
TT
20045}
20046
156942c7
DE
20047/* Return the symbol kind of PSYM. */
20048
20049static gdb_index_symbol_kind
20050symbol_kind (struct partial_symbol *psym)
20051{
20052 domain_enum domain = PSYMBOL_DOMAIN (psym);
20053 enum address_class aclass = PSYMBOL_CLASS (psym);
20054
20055 switch (domain)
20056 {
20057 case VAR_DOMAIN:
20058 switch (aclass)
20059 {
20060 case LOC_BLOCK:
20061 return GDB_INDEX_SYMBOL_KIND_FUNCTION;
20062 case LOC_TYPEDEF:
20063 return GDB_INDEX_SYMBOL_KIND_TYPE;
20064 case LOC_COMPUTED:
20065 case LOC_CONST_BYTES:
20066 case LOC_OPTIMIZED_OUT:
20067 case LOC_STATIC:
20068 return GDB_INDEX_SYMBOL_KIND_VARIABLE;
20069 case LOC_CONST:
20070 /* Note: It's currently impossible to recognize psyms as enum values
20071 short of reading the type info. For now punt. */
20072 return GDB_INDEX_SYMBOL_KIND_VARIABLE;
20073 default:
20074 /* There are other LOC_FOO values that one might want to classify
20075 as variables, but dwarf2read.c doesn't currently use them. */
20076 return GDB_INDEX_SYMBOL_KIND_OTHER;
20077 }
20078 case STRUCT_DOMAIN:
20079 return GDB_INDEX_SYMBOL_KIND_TYPE;
20080 default:
20081 return GDB_INDEX_SYMBOL_KIND_OTHER;
20082 }
20083}
20084
9291a0cd 20085/* Add a list of partial symbols to SYMTAB. */
b89be57b 20086
9291a0cd
TT
20087static void
20088write_psymbols (struct mapped_symtab *symtab,
987d643c 20089 htab_t psyms_seen,
9291a0cd
TT
20090 struct partial_symbol **psymp,
20091 int count,
987d643c
TT
20092 offset_type cu_index,
20093 int is_static)
9291a0cd
TT
20094{
20095 for (; count-- > 0; ++psymp)
20096 {
156942c7
DE
20097 struct partial_symbol *psym = *psymp;
20098 void **slot;
987d643c 20099
156942c7 20100 if (SYMBOL_LANGUAGE (psym) == language_ada)
9291a0cd 20101 error (_("Ada is not currently supported by the index"));
987d643c 20102
987d643c 20103 /* Only add a given psymbol once. */
156942c7 20104 slot = htab_find_slot (psyms_seen, psym, INSERT);
987d643c
TT
20105 if (!*slot)
20106 {
156942c7
DE
20107 gdb_index_symbol_kind kind = symbol_kind (psym);
20108
20109 *slot = psym;
20110 add_index_entry (symtab, SYMBOL_SEARCH_NAME (psym),
20111 is_static, kind, cu_index);
987d643c 20112 }
9291a0cd
TT
20113 }
20114}
20115
20116/* Write the contents of an ("unfinished") obstack to FILE. Throw an
20117 exception if there is an error. */
b89be57b 20118
9291a0cd
TT
20119static void
20120write_obstack (FILE *file, struct obstack *obstack)
20121{
20122 if (fwrite (obstack_base (obstack), 1, obstack_object_size (obstack),
20123 file)
20124 != obstack_object_size (obstack))
20125 error (_("couldn't data write to file"));
20126}
20127
20128/* Unlink a file if the argument is not NULL. */
b89be57b 20129
9291a0cd
TT
20130static void
20131unlink_if_set (void *p)
20132{
20133 char **filename = p;
20134 if (*filename)
20135 unlink (*filename);
20136}
20137
1fd400ff
TT
20138/* A helper struct used when iterating over debug_types. */
20139struct signatured_type_index_data
20140{
20141 struct objfile *objfile;
20142 struct mapped_symtab *symtab;
20143 struct obstack *types_list;
987d643c 20144 htab_t psyms_seen;
1fd400ff
TT
20145 int cu_index;
20146};
20147
20148/* A helper function that writes a single signatured_type to an
20149 obstack. */
b89be57b 20150
1fd400ff
TT
20151static int
20152write_one_signatured_type (void **slot, void *d)
20153{
20154 struct signatured_type_index_data *info = d;
20155 struct signatured_type *entry = (struct signatured_type *) *slot;
e254ef6a
DE
20156 struct dwarf2_per_cu_data *per_cu = &entry->per_cu;
20157 struct partial_symtab *psymtab = per_cu->v.psymtab;
1fd400ff
TT
20158 gdb_byte val[8];
20159
20160 write_psymbols (info->symtab,
987d643c 20161 info->psyms_seen,
3e43a32a
MS
20162 info->objfile->global_psymbols.list
20163 + psymtab->globals_offset,
987d643c
TT
20164 psymtab->n_global_syms, info->cu_index,
20165 0);
1fd400ff 20166 write_psymbols (info->symtab,
987d643c 20167 info->psyms_seen,
3e43a32a
MS
20168 info->objfile->static_psymbols.list
20169 + psymtab->statics_offset,
987d643c
TT
20170 psymtab->n_static_syms, info->cu_index,
20171 1);
1fd400ff 20172
b64f50a1
JK
20173 store_unsigned_integer (val, 8, BFD_ENDIAN_LITTLE,
20174 entry->per_cu.offset.sect_off);
1fd400ff 20175 obstack_grow (info->types_list, val, 8);
3019eac3
DE
20176 store_unsigned_integer (val, 8, BFD_ENDIAN_LITTLE,
20177 entry->type_offset_in_tu.cu_off);
1fd400ff
TT
20178 obstack_grow (info->types_list, val, 8);
20179 store_unsigned_integer (val, 8, BFD_ENDIAN_LITTLE, entry->signature);
20180 obstack_grow (info->types_list, val, 8);
20181
20182 ++info->cu_index;
20183
20184 return 1;
20185}
20186
95554aad
TT
20187/* Recurse into all "included" dependencies and write their symbols as
20188 if they appeared in this psymtab. */
20189
20190static void
20191recursively_write_psymbols (struct objfile *objfile,
20192 struct partial_symtab *psymtab,
20193 struct mapped_symtab *symtab,
20194 htab_t psyms_seen,
20195 offset_type cu_index)
20196{
20197 int i;
20198
20199 for (i = 0; i < psymtab->number_of_dependencies; ++i)
20200 if (psymtab->dependencies[i]->user != NULL)
20201 recursively_write_psymbols (objfile, psymtab->dependencies[i],
20202 symtab, psyms_seen, cu_index);
20203
20204 write_psymbols (symtab,
20205 psyms_seen,
20206 objfile->global_psymbols.list + psymtab->globals_offset,
20207 psymtab->n_global_syms, cu_index,
20208 0);
20209 write_psymbols (symtab,
20210 psyms_seen,
20211 objfile->static_psymbols.list + psymtab->statics_offset,
20212 psymtab->n_static_syms, cu_index,
20213 1);
20214}
20215
9291a0cd 20216/* Create an index file for OBJFILE in the directory DIR. */
b89be57b 20217
9291a0cd
TT
20218static void
20219write_psymtabs_to_index (struct objfile *objfile, const char *dir)
20220{
20221 struct cleanup *cleanup;
20222 char *filename, *cleanup_filename;
1fd400ff
TT
20223 struct obstack contents, addr_obstack, constant_pool, symtab_obstack;
20224 struct obstack cu_list, types_cu_list;
9291a0cd
TT
20225 int i;
20226 FILE *out_file;
20227 struct mapped_symtab *symtab;
20228 offset_type val, size_of_contents, total_len;
20229 struct stat st;
987d643c 20230 htab_t psyms_seen;
0a5429f6
DE
20231 htab_t cu_index_htab;
20232 struct psymtab_cu_index_map *psymtab_cu_index_map;
9291a0cd 20233
b4f2f049 20234 if (!objfile->psymtabs || !objfile->psymtabs_addrmap)
9291a0cd 20235 return;
b4f2f049 20236
9291a0cd
TT
20237 if (dwarf2_per_objfile->using_index)
20238 error (_("Cannot use an index to create the index"));
20239
8b70b953
TT
20240 if (VEC_length (dwarf2_section_info_def, dwarf2_per_objfile->types) > 1)
20241 error (_("Cannot make an index when the file has multiple .debug_types sections"));
20242
9291a0cd 20243 if (stat (objfile->name, &st) < 0)
7e17e088 20244 perror_with_name (objfile->name);
9291a0cd
TT
20245
20246 filename = concat (dir, SLASH_STRING, lbasename (objfile->name),
20247 INDEX_SUFFIX, (char *) NULL);
20248 cleanup = make_cleanup (xfree, filename);
20249
20250 out_file = fopen (filename, "wb");
20251 if (!out_file)
20252 error (_("Can't open `%s' for writing"), filename);
20253
20254 cleanup_filename = filename;
20255 make_cleanup (unlink_if_set, &cleanup_filename);
20256
20257 symtab = create_mapped_symtab ();
20258 make_cleanup (cleanup_mapped_symtab, symtab);
20259
20260 obstack_init (&addr_obstack);
20261 make_cleanup_obstack_free (&addr_obstack);
20262
20263 obstack_init (&cu_list);
20264 make_cleanup_obstack_free (&cu_list);
20265
1fd400ff
TT
20266 obstack_init (&types_cu_list);
20267 make_cleanup_obstack_free (&types_cu_list);
20268
987d643c
TT
20269 psyms_seen = htab_create_alloc (100, htab_hash_pointer, htab_eq_pointer,
20270 NULL, xcalloc, xfree);
96408a79 20271 make_cleanup_htab_delete (psyms_seen);
987d643c 20272
0a5429f6
DE
20273 /* While we're scanning CU's create a table that maps a psymtab pointer
20274 (which is what addrmap records) to its index (which is what is recorded
20275 in the index file). This will later be needed to write the address
20276 table. */
20277 cu_index_htab = htab_create_alloc (100,
20278 hash_psymtab_cu_index,
20279 eq_psymtab_cu_index,
20280 NULL, xcalloc, xfree);
96408a79 20281 make_cleanup_htab_delete (cu_index_htab);
0a5429f6
DE
20282 psymtab_cu_index_map = (struct psymtab_cu_index_map *)
20283 xmalloc (sizeof (struct psymtab_cu_index_map)
20284 * dwarf2_per_objfile->n_comp_units);
20285 make_cleanup (xfree, psymtab_cu_index_map);
20286
20287 /* The CU list is already sorted, so we don't need to do additional
1fd400ff
TT
20288 work here. Also, the debug_types entries do not appear in
20289 all_comp_units, but only in their own hash table. */
9291a0cd
TT
20290 for (i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
20291 {
3e43a32a
MS
20292 struct dwarf2_per_cu_data *per_cu
20293 = dwarf2_per_objfile->all_comp_units[i];
e254ef6a 20294 struct partial_symtab *psymtab = per_cu->v.psymtab;
9291a0cd 20295 gdb_byte val[8];
0a5429f6
DE
20296 struct psymtab_cu_index_map *map;
20297 void **slot;
9291a0cd 20298
95554aad
TT
20299 if (psymtab->user == NULL)
20300 recursively_write_psymbols (objfile, psymtab, symtab, psyms_seen, i);
9291a0cd 20301
0a5429f6
DE
20302 map = &psymtab_cu_index_map[i];
20303 map->psymtab = psymtab;
20304 map->cu_index = i;
20305 slot = htab_find_slot (cu_index_htab, map, INSERT);
20306 gdb_assert (slot != NULL);
20307 gdb_assert (*slot == NULL);
20308 *slot = map;
9291a0cd 20309
b64f50a1
JK
20310 store_unsigned_integer (val, 8, BFD_ENDIAN_LITTLE,
20311 per_cu->offset.sect_off);
9291a0cd 20312 obstack_grow (&cu_list, val, 8);
e254ef6a 20313 store_unsigned_integer (val, 8, BFD_ENDIAN_LITTLE, per_cu->length);
9291a0cd
TT
20314 obstack_grow (&cu_list, val, 8);
20315 }
20316
0a5429f6
DE
20317 /* Dump the address map. */
20318 write_address_map (objfile, &addr_obstack, cu_index_htab);
20319
1fd400ff
TT
20320 /* Write out the .debug_type entries, if any. */
20321 if (dwarf2_per_objfile->signatured_types)
20322 {
20323 struct signatured_type_index_data sig_data;
20324
20325 sig_data.objfile = objfile;
20326 sig_data.symtab = symtab;
20327 sig_data.types_list = &types_cu_list;
987d643c 20328 sig_data.psyms_seen = psyms_seen;
1fd400ff
TT
20329 sig_data.cu_index = dwarf2_per_objfile->n_comp_units;
20330 htab_traverse_noresize (dwarf2_per_objfile->signatured_types,
20331 write_one_signatured_type, &sig_data);
20332 }
20333
156942c7
DE
20334 /* Now that we've processed all symbols we can shrink their cu_indices
20335 lists. */
20336 uniquify_cu_indices (symtab);
20337
9291a0cd
TT
20338 obstack_init (&constant_pool);
20339 make_cleanup_obstack_free (&constant_pool);
20340 obstack_init (&symtab_obstack);
20341 make_cleanup_obstack_free (&symtab_obstack);
20342 write_hash_table (symtab, &symtab_obstack, &constant_pool);
20343
20344 obstack_init (&contents);
20345 make_cleanup_obstack_free (&contents);
1fd400ff 20346 size_of_contents = 6 * sizeof (offset_type);
9291a0cd
TT
20347 total_len = size_of_contents;
20348
20349 /* The version number. */
156942c7 20350 val = MAYBE_SWAP (7);
9291a0cd
TT
20351 obstack_grow (&contents, &val, sizeof (val));
20352
20353 /* The offset of the CU list from the start of the file. */
20354 val = MAYBE_SWAP (total_len);
20355 obstack_grow (&contents, &val, sizeof (val));
20356 total_len += obstack_object_size (&cu_list);
20357
1fd400ff
TT
20358 /* The offset of the types CU list from the start of the file. */
20359 val = MAYBE_SWAP (total_len);
20360 obstack_grow (&contents, &val, sizeof (val));
20361 total_len += obstack_object_size (&types_cu_list);
20362
9291a0cd
TT
20363 /* The offset of the address table from the start of the file. */
20364 val = MAYBE_SWAP (total_len);
20365 obstack_grow (&contents, &val, sizeof (val));
20366 total_len += obstack_object_size (&addr_obstack);
20367
20368 /* The offset of the symbol table from the start of the file. */
20369 val = MAYBE_SWAP (total_len);
20370 obstack_grow (&contents, &val, sizeof (val));
20371 total_len += obstack_object_size (&symtab_obstack);
20372
20373 /* The offset of the constant pool from the start of the file. */
20374 val = MAYBE_SWAP (total_len);
20375 obstack_grow (&contents, &val, sizeof (val));
20376 total_len += obstack_object_size (&constant_pool);
20377
20378 gdb_assert (obstack_object_size (&contents) == size_of_contents);
20379
20380 write_obstack (out_file, &contents);
20381 write_obstack (out_file, &cu_list);
1fd400ff 20382 write_obstack (out_file, &types_cu_list);
9291a0cd
TT
20383 write_obstack (out_file, &addr_obstack);
20384 write_obstack (out_file, &symtab_obstack);
20385 write_obstack (out_file, &constant_pool);
20386
20387 fclose (out_file);
20388
20389 /* We want to keep the file, so we set cleanup_filename to NULL
20390 here. See unlink_if_set. */
20391 cleanup_filename = NULL;
20392
20393 do_cleanups (cleanup);
20394}
20395
90476074
TT
20396/* Implementation of the `save gdb-index' command.
20397
20398 Note that the file format used by this command is documented in the
20399 GDB manual. Any changes here must be documented there. */
11570e71 20400
9291a0cd
TT
20401static void
20402save_gdb_index_command (char *arg, int from_tty)
20403{
20404 struct objfile *objfile;
20405
20406 if (!arg || !*arg)
96d19272 20407 error (_("usage: save gdb-index DIRECTORY"));
9291a0cd
TT
20408
20409 ALL_OBJFILES (objfile)
20410 {
20411 struct stat st;
20412
20413 /* If the objfile does not correspond to an actual file, skip it. */
20414 if (stat (objfile->name, &st) < 0)
20415 continue;
20416
20417 dwarf2_per_objfile = objfile_data (objfile, dwarf2_objfile_data_key);
20418 if (dwarf2_per_objfile)
20419 {
20420 volatile struct gdb_exception except;
20421
20422 TRY_CATCH (except, RETURN_MASK_ERROR)
20423 {
20424 write_psymtabs_to_index (objfile, arg);
20425 }
20426 if (except.reason < 0)
20427 exception_fprintf (gdb_stderr, except,
20428 _("Error while writing index for `%s': "),
20429 objfile->name);
20430 }
20431 }
dce234bc
PP
20432}
20433
9291a0cd
TT
20434\f
20435
9eae7c52
TT
20436int dwarf2_always_disassemble;
20437
20438static void
20439show_dwarf2_always_disassemble (struct ui_file *file, int from_tty,
20440 struct cmd_list_element *c, const char *value)
20441{
3e43a32a
MS
20442 fprintf_filtered (file,
20443 _("Whether to always disassemble "
20444 "DWARF expressions is %s.\n"),
9eae7c52
TT
20445 value);
20446}
20447
900e11f9
JK
20448static void
20449show_check_physname (struct ui_file *file, int from_tty,
20450 struct cmd_list_element *c, const char *value)
20451{
20452 fprintf_filtered (file,
20453 _("Whether to check \"physname\" is %s.\n"),
20454 value);
20455}
20456
6502dd73
DJ
20457void _initialize_dwarf2_read (void);
20458
20459void
20460_initialize_dwarf2_read (void)
20461{
96d19272
JK
20462 struct cmd_list_element *c;
20463
dce234bc 20464 dwarf2_objfile_data_key
c1bd65d0 20465 = register_objfile_data_with_cleanup (NULL, dwarf2_per_objfile_free);
ae038cb0 20466
1bedd215
AC
20467 add_prefix_cmd ("dwarf2", class_maintenance, set_dwarf2_cmd, _("\
20468Set DWARF 2 specific variables.\n\
20469Configure DWARF 2 variables such as the cache size"),
ae038cb0
DJ
20470 &set_dwarf2_cmdlist, "maintenance set dwarf2 ",
20471 0/*allow-unknown*/, &maintenance_set_cmdlist);
20472
1bedd215
AC
20473 add_prefix_cmd ("dwarf2", class_maintenance, show_dwarf2_cmd, _("\
20474Show DWARF 2 specific variables\n\
20475Show DWARF 2 variables such as the cache size"),
ae038cb0
DJ
20476 &show_dwarf2_cmdlist, "maintenance show dwarf2 ",
20477 0/*allow-unknown*/, &maintenance_show_cmdlist);
20478
20479 add_setshow_zinteger_cmd ("max-cache-age", class_obscure,
7915a72c
AC
20480 &dwarf2_max_cache_age, _("\
20481Set the upper bound on the age of cached dwarf2 compilation units."), _("\
20482Show the upper bound on the age of cached dwarf2 compilation units."), _("\
20483A higher limit means that cached compilation units will be stored\n\
20484in memory longer, and more total memory will be used. Zero disables\n\
20485caching, which can slow down startup."),
2c5b56ce 20486 NULL,
920d2a44 20487 show_dwarf2_max_cache_age,
2c5b56ce 20488 &set_dwarf2_cmdlist,
ae038cb0 20489 &show_dwarf2_cmdlist);
d97bc12b 20490
9eae7c52
TT
20491 add_setshow_boolean_cmd ("always-disassemble", class_obscure,
20492 &dwarf2_always_disassemble, _("\
20493Set whether `info address' always disassembles DWARF expressions."), _("\
20494Show whether `info address' always disassembles DWARF expressions."), _("\
20495When enabled, DWARF expressions are always printed in an assembly-like\n\
20496syntax. When disabled, expressions will be printed in a more\n\
20497conversational style, when possible."),
20498 NULL,
20499 show_dwarf2_always_disassemble,
20500 &set_dwarf2_cmdlist,
20501 &show_dwarf2_cmdlist);
20502
45cfd468
DE
20503 add_setshow_boolean_cmd ("dwarf2-read", no_class, &dwarf2_read_debug, _("\
20504Set debugging of the dwarf2 reader."), _("\
20505Show debugging of the dwarf2 reader."), _("\
20506When enabled, debugging messages are printed during dwarf2 reading\n\
20507and symtab expansion."),
20508 NULL,
20509 NULL,
20510 &setdebuglist, &showdebuglist);
20511
ccce17b0 20512 add_setshow_zuinteger_cmd ("dwarf2-die", no_class, &dwarf2_die_debug, _("\
d97bc12b
DE
20513Set debugging of the dwarf2 DIE reader."), _("\
20514Show debugging of the dwarf2 DIE reader."), _("\
20515When enabled (non-zero), DIEs are dumped after they are read in.\n\
20516The value is the maximum depth to print."),
ccce17b0
YQ
20517 NULL,
20518 NULL,
20519 &setdebuglist, &showdebuglist);
9291a0cd 20520
900e11f9
JK
20521 add_setshow_boolean_cmd ("check-physname", no_class, &check_physname, _("\
20522Set cross-checking of \"physname\" code against demangler."), _("\
20523Show cross-checking of \"physname\" code against demangler."), _("\
20524When enabled, GDB's internal \"physname\" code is checked against\n\
20525the demangler."),
20526 NULL, show_check_physname,
20527 &setdebuglist, &showdebuglist);
20528
e615022a
DE
20529 add_setshow_boolean_cmd ("use-deprecated-index-sections",
20530 no_class, &use_deprecated_index_sections, _("\
20531Set whether to use deprecated gdb_index sections."), _("\
20532Show whether to use deprecated gdb_index sections."), _("\
20533When enabled, deprecated .gdb_index sections are used anyway.\n\
20534Normally they are ignored either because of a missing feature or\n\
20535performance issue.\n\
20536Warning: This option must be enabled before gdb reads the file."),
20537 NULL,
20538 NULL,
20539 &setlist, &showlist);
20540
96d19272 20541 c = add_cmd ("gdb-index", class_files, save_gdb_index_command,
11570e71 20542 _("\
fc1a9d6e 20543Save a gdb-index file.\n\
11570e71 20544Usage: save gdb-index DIRECTORY"),
96d19272
JK
20545 &save_cmdlist);
20546 set_cmd_completer (c, filename_completer);
6502dd73 20547}
This page took 2.711888 seconds and 4 git commands to generate.