libiberty/md5: fix strict alias warnings
[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"
c906108c
SS
33#include "symtab.h"
34#include "gdbtypes.h"
c906108c 35#include "objfiles.h"
fa8f86ff 36#include "dwarf2.h"
c906108c
SS
37#include "buildsym.h"
38#include "demangle.h"
50f182aa 39#include "gdb-demangle.h"
c906108c 40#include "expression.h"
d5166ae1 41#include "filenames.h" /* for DOSish file names */
2e276125 42#include "macrotab.h"
c906108c
SS
43#include "language.h"
44#include "complaints.h"
357e46e7 45#include "bcache.h"
4c2df51b
DJ
46#include "dwarf2expr.h"
47#include "dwarf2loc.h"
9219021c 48#include "cp-support.h"
72bf9492 49#include "hashtab.h"
ae038cb0
DJ
50#include "command.h"
51#include "gdbcmd.h"
edb3359d 52#include "block.h"
ff013f42 53#include "addrmap.h"
94af9270
KS
54#include "typeprint.h"
55#include "jv-lang.h"
ccefe4c4 56#include "psympriv.h"
9291a0cd
TT
57#include "exceptions.h"
58#include "gdb_stat.h"
96d19272 59#include "completer.h"
34eaf542 60#include "vec.h"
98bfdba5 61#include "c-lang.h"
a766d390 62#include "go-lang.h"
98bfdba5 63#include "valprint.h"
3019eac3 64#include "gdbcore.h" /* for gnutarget */
156942c7 65#include "gdb/gdb-index.h"
60d5a603 66#include <ctype.h>
cbb099e8 67#include "gdb_bfd.h"
4c2df51b 68
c906108c
SS
69#include <fcntl.h>
70#include "gdb_string.h"
4bdf3d34 71#include "gdb_assert.h"
c906108c 72#include <sys/types.h>
d8151005 73
34eaf542
TT
74typedef struct symbol *symbolp;
75DEF_VEC_P (symbolp);
76
45cfd468
DE
77/* When non-zero, print basic high level tracing messages.
78 This is in contrast to the low level DIE reading of dwarf2_die_debug. */
79static int dwarf2_read_debug = 0;
80
d97bc12b
DE
81/* When non-zero, dump DIEs after they are read in. */
82static int dwarf2_die_debug = 0;
83
900e11f9
JK
84/* When non-zero, cross-check physname against demangler. */
85static int check_physname = 0;
86
481860b3 87/* When non-zero, do not reject deprecated .gdb_index sections. */
e615022a 88static int use_deprecated_index_sections = 0;
481860b3 89
df8a16a1
DJ
90/* When set, the file that we're processing is known to have debugging
91 info for C++ namespaces. GCC 3.3.x did not produce this information,
92 but later versions do. */
93
94static int processing_has_namespace_info;
95
6502dd73
DJ
96static const struct objfile_data *dwarf2_objfile_data_key;
97
dce234bc
PP
98struct dwarf2_section_info
99{
100 asection *asection;
101 gdb_byte *buffer;
102 bfd_size_type size;
be391dca
TT
103 /* True if we have tried to read this section. */
104 int readin;
dce234bc
PP
105};
106
8b70b953
TT
107typedef struct dwarf2_section_info dwarf2_section_info_def;
108DEF_VEC_O (dwarf2_section_info_def);
109
9291a0cd
TT
110/* All offsets in the index are of this type. It must be
111 architecture-independent. */
112typedef uint32_t offset_type;
113
114DEF_VEC_I (offset_type);
115
156942c7
DE
116/* Ensure only legit values are used. */
117#define DW2_GDB_INDEX_SYMBOL_STATIC_SET_VALUE(cu_index, value) \
118 do { \
119 gdb_assert ((unsigned int) (value) <= 1); \
120 GDB_INDEX_SYMBOL_STATIC_SET_VALUE((cu_index), (value)); \
121 } while (0)
122
123/* Ensure only legit values are used. */
124#define DW2_GDB_INDEX_SYMBOL_KIND_SET_VALUE(cu_index, value) \
125 do { \
126 gdb_assert ((value) >= GDB_INDEX_SYMBOL_KIND_TYPE \
127 && (value) <= GDB_INDEX_SYMBOL_KIND_OTHER); \
128 GDB_INDEX_SYMBOL_KIND_SET_VALUE((cu_index), (value)); \
129 } while (0)
130
131/* Ensure we don't use more than the alloted nuber of bits for the CU. */
132#define DW2_GDB_INDEX_CU_SET_VALUE(cu_index, value) \
133 do { \
134 gdb_assert (((value) & ~GDB_INDEX_CU_MASK) == 0); \
135 GDB_INDEX_CU_SET_VALUE((cu_index), (value)); \
136 } while (0)
137
9291a0cd
TT
138/* A description of the mapped index. The file format is described in
139 a comment by the code that writes the index. */
140struct mapped_index
141{
559a7a62
JK
142 /* Index data format version. */
143 int version;
144
9291a0cd
TT
145 /* The total length of the buffer. */
146 off_t total_size;
b11b1f88 147
9291a0cd
TT
148 /* A pointer to the address table data. */
149 const gdb_byte *address_table;
b11b1f88 150
9291a0cd
TT
151 /* Size of the address table data in bytes. */
152 offset_type address_table_size;
b11b1f88 153
3876f04e
DE
154 /* The symbol table, implemented as a hash table. */
155 const offset_type *symbol_table;
b11b1f88 156
9291a0cd 157 /* Size in slots, each slot is 2 offset_types. */
3876f04e 158 offset_type symbol_table_slots;
b11b1f88 159
9291a0cd
TT
160 /* A pointer to the constant pool. */
161 const char *constant_pool;
162};
163
95554aad
TT
164typedef struct dwarf2_per_cu_data *dwarf2_per_cu_ptr;
165DEF_VEC_P (dwarf2_per_cu_ptr);
166
9cdd5dbd
DE
167/* Collection of data recorded per objfile.
168 This hangs off of dwarf2_objfile_data_key. */
169
6502dd73
DJ
170struct dwarf2_per_objfile
171{
dce234bc
PP
172 struct dwarf2_section_info info;
173 struct dwarf2_section_info abbrev;
174 struct dwarf2_section_info line;
dce234bc
PP
175 struct dwarf2_section_info loc;
176 struct dwarf2_section_info macinfo;
cf2c3c16 177 struct dwarf2_section_info macro;
dce234bc
PP
178 struct dwarf2_section_info str;
179 struct dwarf2_section_info ranges;
3019eac3 180 struct dwarf2_section_info addr;
dce234bc
PP
181 struct dwarf2_section_info frame;
182 struct dwarf2_section_info eh_frame;
9291a0cd 183 struct dwarf2_section_info gdb_index;
ae038cb0 184
8b70b953
TT
185 VEC (dwarf2_section_info_def) *types;
186
be391dca
TT
187 /* Back link. */
188 struct objfile *objfile;
189
d467dd73 190 /* Table of all the compilation units. This is used to locate
10b3939b 191 the target compilation unit of a particular reference. */
ae038cb0
DJ
192 struct dwarf2_per_cu_data **all_comp_units;
193
194 /* The number of compilation units in ALL_COMP_UNITS. */
195 int n_comp_units;
196
1fd400ff 197 /* The number of .debug_types-related CUs. */
d467dd73 198 int n_type_units;
1fd400ff 199
d467dd73 200 /* The .debug_types-related CUs (TUs). */
b4dd5633 201 struct signatured_type **all_type_units;
1fd400ff 202
f4dc4d17
DE
203 /* The number of entries in all_type_unit_groups. */
204 int n_type_unit_groups;
205
206 /* Table of type unit groups.
207 This exists to make it easy to iterate over all CUs and TU groups. */
208 struct type_unit_group **all_type_unit_groups;
209
210 /* Table of struct type_unit_group objects.
211 The hash key is the DW_AT_stmt_list value. */
212 htab_t type_unit_groups;
72dca2f5 213
348e048f
DE
214 /* A table mapping .debug_types signatures to its signatured_type entry.
215 This is NULL if the .debug_types section hasn't been read in yet. */
216 htab_t signatured_types;
217
f4dc4d17
DE
218 /* Type unit statistics, to see how well the scaling improvements
219 are doing. */
220 struct tu_stats
221 {
222 int nr_uniq_abbrev_tables;
223 int nr_symtabs;
224 int nr_symtab_sharers;
225 int nr_stmt_less_type_units;
226 } tu_stats;
227
228 /* A chain of compilation units that are currently read in, so that
229 they can be freed later. */
230 struct dwarf2_per_cu_data *read_in_chain;
231
3019eac3
DE
232 /* A table mapping DW_AT_dwo_name values to struct dwo_file objects.
233 This is NULL if the table hasn't been allocated yet. */
234 htab_t dwo_files;
235
36586728
TT
236 /* The shared '.dwz' file, if one exists. This is used when the
237 original data was compressed using 'dwz -m'. */
238 struct dwz_file *dwz_file;
239
72dca2f5
FR
240 /* A flag indicating wether this objfile has a section loaded at a
241 VMA of 0. */
242 int has_section_at_zero;
9291a0cd 243
ae2de4f8
DE
244 /* True if we are using the mapped index,
245 or we are faking it for OBJF_READNOW's sake. */
9291a0cd
TT
246 unsigned char using_index;
247
ae2de4f8 248 /* The mapped index, or NULL if .gdb_index is missing or not being used. */
9291a0cd 249 struct mapped_index *index_table;
98bfdba5 250
7b9f3c50
DE
251 /* When using index_table, this keeps track of all quick_file_names entries.
252 TUs can share line table entries with CUs or other TUs, and there can be
253 a lot more TUs than unique line tables, so we maintain a separate table
254 of all line table entries to support the sharing. */
255 htab_t quick_file_names_table;
256
98bfdba5
PA
257 /* Set during partial symbol reading, to prevent queueing of full
258 symbols. */
259 int reading_partial_symbols;
673bfd45 260
dee91e82 261 /* Table mapping type DIEs to their struct type *.
673bfd45 262 This is NULL if not allocated yet.
dee91e82
DE
263 The mapping is done via (CU/TU signature + DIE offset) -> type. */
264 htab_t die_type_hash;
95554aad
TT
265
266 /* The CUs we recently read. */
267 VEC (dwarf2_per_cu_ptr) *just_read_cus;
6502dd73
DJ
268};
269
270static struct dwarf2_per_objfile *dwarf2_per_objfile;
c906108c 271
251d32d9 272/* Default names of the debugging sections. */
c906108c 273
233a11ab
CS
274/* Note that if the debugging section has been compressed, it might
275 have a name like .zdebug_info. */
276
9cdd5dbd
DE
277static const struct dwarf2_debug_sections dwarf2_elf_names =
278{
251d32d9
TG
279 { ".debug_info", ".zdebug_info" },
280 { ".debug_abbrev", ".zdebug_abbrev" },
281 { ".debug_line", ".zdebug_line" },
282 { ".debug_loc", ".zdebug_loc" },
283 { ".debug_macinfo", ".zdebug_macinfo" },
cf2c3c16 284 { ".debug_macro", ".zdebug_macro" },
251d32d9
TG
285 { ".debug_str", ".zdebug_str" },
286 { ".debug_ranges", ".zdebug_ranges" },
287 { ".debug_types", ".zdebug_types" },
3019eac3 288 { ".debug_addr", ".zdebug_addr" },
251d32d9
TG
289 { ".debug_frame", ".zdebug_frame" },
290 { ".eh_frame", NULL },
24d3216f
TT
291 { ".gdb_index", ".zgdb_index" },
292 23
251d32d9 293};
c906108c 294
3019eac3
DE
295/* List of DWO sections. */
296
297static const struct dwo_section_names
298{
299 struct dwarf2_section_names abbrev_dwo;
300 struct dwarf2_section_names info_dwo;
301 struct dwarf2_section_names line_dwo;
302 struct dwarf2_section_names loc_dwo;
09262596
DE
303 struct dwarf2_section_names macinfo_dwo;
304 struct dwarf2_section_names macro_dwo;
3019eac3
DE
305 struct dwarf2_section_names str_dwo;
306 struct dwarf2_section_names str_offsets_dwo;
307 struct dwarf2_section_names types_dwo;
308}
309dwo_section_names =
310{
311 { ".debug_abbrev.dwo", ".zdebug_abbrev.dwo" },
312 { ".debug_info.dwo", ".zdebug_info.dwo" },
313 { ".debug_line.dwo", ".zdebug_line.dwo" },
314 { ".debug_loc.dwo", ".zdebug_loc.dwo" },
09262596
DE
315 { ".debug_macinfo.dwo", ".zdebug_macinfo.dwo" },
316 { ".debug_macro.dwo", ".zdebug_macro.dwo" },
3019eac3
DE
317 { ".debug_str.dwo", ".zdebug_str.dwo" },
318 { ".debug_str_offsets.dwo", ".zdebug_str_offsets.dwo" },
319 { ".debug_types.dwo", ".zdebug_types.dwo" },
320};
321
c906108c
SS
322/* local data types */
323
107d2387
AC
324/* The data in a compilation unit header, after target2host
325 translation, looks like this. */
c906108c 326struct comp_unit_head
a738430d 327{
c764a876 328 unsigned int length;
a738430d 329 short version;
a738430d
MK
330 unsigned char addr_size;
331 unsigned char signed_addr_p;
b64f50a1 332 sect_offset abbrev_offset;
57349743 333
a738430d
MK
334 /* Size of file offsets; either 4 or 8. */
335 unsigned int offset_size;
57349743 336
a738430d
MK
337 /* Size of the length field; either 4 or 12. */
338 unsigned int initial_length_size;
57349743 339
a738430d
MK
340 /* Offset to the first byte of this compilation unit header in the
341 .debug_info section, for resolving relative reference dies. */
b64f50a1 342 sect_offset offset;
57349743 343
d00adf39
DE
344 /* Offset to first die in this cu from the start of the cu.
345 This will be the first byte following the compilation unit header. */
b64f50a1 346 cu_offset first_die_offset;
a738430d 347};
c906108c 348
3da10d80
KS
349/* Type used for delaying computation of method physnames.
350 See comments for compute_delayed_physnames. */
351struct delayed_method_info
352{
353 /* The type to which the method is attached, i.e., its parent class. */
354 struct type *type;
355
356 /* The index of the method in the type's function fieldlists. */
357 int fnfield_index;
358
359 /* The index of the method in the fieldlist. */
360 int index;
361
362 /* The name of the DIE. */
363 const char *name;
364
365 /* The DIE associated with this method. */
366 struct die_info *die;
367};
368
369typedef struct delayed_method_info delayed_method_info;
370DEF_VEC_O (delayed_method_info);
371
e7c27a73
DJ
372/* Internal state when decoding a particular compilation unit. */
373struct dwarf2_cu
374{
375 /* The objfile containing this compilation unit. */
376 struct objfile *objfile;
377
d00adf39 378 /* The header of the compilation unit. */
e7c27a73 379 struct comp_unit_head header;
e142c38c 380
d00adf39
DE
381 /* Base address of this compilation unit. */
382 CORE_ADDR base_address;
383
384 /* Non-zero if base_address has been set. */
385 int base_known;
386
e142c38c
DJ
387 /* The language we are debugging. */
388 enum language language;
389 const struct language_defn *language_defn;
390
b0f35d58
DL
391 const char *producer;
392
e142c38c
DJ
393 /* The generic symbol table building routines have separate lists for
394 file scope symbols and all all other scopes (local scopes). So
395 we need to select the right one to pass to add_symbol_to_list().
396 We do it by keeping a pointer to the correct list in list_in_scope.
397
398 FIXME: The original dwarf code just treated the file scope as the
399 first local scope, and all other local scopes as nested local
400 scopes, and worked fine. Check to see if we really need to
401 distinguish these in buildsym.c. */
402 struct pending **list_in_scope;
403
433df2d4
DE
404 /* The abbrev table for this CU.
405 Normally this points to the abbrev table in the objfile.
406 But if DWO_UNIT is non-NULL this is the abbrev table in the DWO file. */
407 struct abbrev_table *abbrev_table;
72bf9492 408
b64f50a1
JK
409 /* Hash table holding all the loaded partial DIEs
410 with partial_die->offset.SECT_OFF as hash. */
72bf9492
DJ
411 htab_t partial_dies;
412
413 /* Storage for things with the same lifetime as this read-in compilation
414 unit, including partial DIEs. */
415 struct obstack comp_unit_obstack;
416
ae038cb0
DJ
417 /* When multiple dwarf2_cu structures are living in memory, this field
418 chains them all together, so that they can be released efficiently.
419 We will probably also want a generation counter so that most-recently-used
420 compilation units are cached... */
421 struct dwarf2_per_cu_data *read_in_chain;
422
423 /* Backchain to our per_cu entry if the tree has been built. */
424 struct dwarf2_per_cu_data *per_cu;
425
426 /* How many compilation units ago was this CU last referenced? */
427 int last_used;
428
b64f50a1
JK
429 /* A hash table of DIE cu_offset for following references with
430 die_info->offset.sect_off as hash. */
51545339 431 htab_t die_hash;
10b3939b
DJ
432
433 /* Full DIEs if read in. */
434 struct die_info *dies;
435
436 /* A set of pointers to dwarf2_per_cu_data objects for compilation
437 units referenced by this one. Only set during full symbol processing;
438 partial symbol tables do not have dependencies. */
439 htab_t dependencies;
440
cb1df416
DJ
441 /* Header data from the line table, during full symbol processing. */
442 struct line_header *line_header;
443
3da10d80
KS
444 /* A list of methods which need to have physnames computed
445 after all type information has been read. */
446 VEC (delayed_method_info) *method_list;
447
96408a79
SA
448 /* To be copied to symtab->call_site_htab. */
449 htab_t call_site_htab;
450
034e5797
DE
451 /* Non-NULL if this CU came from a DWO file.
452 There is an invariant here that is important to remember:
453 Except for attributes copied from the top level DIE in the "main"
454 (or "stub") file in preparation for reading the DWO file
455 (e.g., DW_AT_GNU_addr_base), we KISS: there is only *one* CU.
456 Either there isn't a DWO file (in which case this is NULL and the point
457 is moot), or there is and either we're not going to read it (in which
458 case this is NULL) or there is and we are reading it (in which case this
459 is non-NULL). */
3019eac3
DE
460 struct dwo_unit *dwo_unit;
461
462 /* The DW_AT_addr_base attribute if present, zero otherwise
463 (zero is a valid value though).
464 Note this value comes from the stub CU/TU's DIE. */
465 ULONGEST addr_base;
466
2e3cf129
DE
467 /* The DW_AT_ranges_base attribute if present, zero otherwise
468 (zero is a valid value though).
469 Note this value comes from the stub CU/TU's DIE.
470 Also note that the value is zero in the non-DWO case so this value can
471 be used without needing to know whether DWO files are in use or not. */
472 ULONGEST ranges_base;
473
ae038cb0
DJ
474 /* Mark used when releasing cached dies. */
475 unsigned int mark : 1;
476
8be455d7
JK
477 /* This CU references .debug_loc. See the symtab->locations_valid field.
478 This test is imperfect as there may exist optimized debug code not using
479 any location list and still facing inlining issues if handled as
480 unoptimized code. For a future better test see GCC PR other/32998. */
8be455d7 481 unsigned int has_loclist : 1;
ba919b58 482
685b1105
JK
483 /* These cache the results for producer_is_gxx_lt_4_6 and producer_is_icc.
484 CHECKED_PRODUCER is set if both PRODUCER_IS_GXX_LT_4_6 and PRODUCER_IS_ICC
485 are valid. This information is cached because profiling CU expansion
486 showed excessive time spent in producer_is_gxx_lt_4_6. */
ba919b58
TT
487 unsigned int checked_producer : 1;
488 unsigned int producer_is_gxx_lt_4_6 : 1;
685b1105 489 unsigned int producer_is_icc : 1;
e7c27a73
DJ
490};
491
10b3939b
DJ
492/* Persistent data held for a compilation unit, even when not
493 processing it. We put a pointer to this structure in the
28dee7f5 494 read_symtab_private field of the psymtab. */
10b3939b 495
ae038cb0
DJ
496struct dwarf2_per_cu_data
497{
36586728 498 /* The start offset and length of this compilation unit.
45452591 499 NOTE: Unlike comp_unit_head.length, this length includes
3019eac3
DE
500 initial_length_size.
501 If the DIE refers to a DWO file, this is always of the original die,
502 not the DWO file. */
b64f50a1 503 sect_offset offset;
36586728 504 unsigned int length;
ae038cb0
DJ
505
506 /* Flag indicating this compilation unit will be read in before
507 any of the current compilation units are processed. */
c764a876 508 unsigned int queued : 1;
ae038cb0 509
0d99eb77
DE
510 /* This flag will be set when reading partial DIEs if we need to load
511 absolutely all DIEs for this compilation unit, instead of just the ones
512 we think are interesting. It gets set if we look for a DIE in the
5afb4e99
DJ
513 hash table and don't find it. */
514 unsigned int load_all_dies : 1;
515
3019eac3
DE
516 /* Non-zero if this CU is from .debug_types. */
517 unsigned int is_debug_types : 1;
518
36586728
TT
519 /* Non-zero if this CU is from the .dwz file. */
520 unsigned int is_dwz : 1;
521
3019eac3
DE
522 /* The section this CU/TU lives in.
523 If the DIE refers to a DWO file, this is always the original die,
524 not the DWO file. */
525 struct dwarf2_section_info *info_or_types_section;
348e048f 526
17ea53c3
JK
527 /* Set to non-NULL iff this CU is currently loaded. When it gets freed out
528 of the CU cache it gets reset to NULL again. */
ae038cb0 529 struct dwarf2_cu *cu;
1c379e20 530
9cdd5dbd
DE
531 /* The corresponding objfile.
532 Normally we can get the objfile from dwarf2_per_objfile.
533 However we can enter this file with just a "per_cu" handle. */
9291a0cd
TT
534 struct objfile *objfile;
535
536 /* When using partial symbol tables, the 'psymtab' field is active.
537 Otherwise the 'quick' field is active. */
538 union
539 {
540 /* The partial symbol table associated with this compilation unit,
95554aad 541 or NULL for unread partial units. */
9291a0cd
TT
542 struct partial_symtab *psymtab;
543
544 /* Data needed by the "quick" functions. */
545 struct dwarf2_per_cu_quick_data *quick;
546 } v;
95554aad 547
f4dc4d17
DE
548 union
549 {
550 /* The CUs we import using DW_TAG_imported_unit. This is filled in
551 while reading psymtabs, used to compute the psymtab dependencies,
552 and then cleared. Then it is filled in again while reading full
553 symbols, and only deleted when the objfile is destroyed. */
554 VEC (dwarf2_per_cu_ptr) *imported_symtabs;
555
556 /* Type units are grouped by their DW_AT_stmt_list entry so that they
557 can share them. If this is a TU, this points to the containing
558 symtab. */
559 struct type_unit_group *type_unit_group;
560 } s;
ae038cb0
DJ
561};
562
348e048f
DE
563/* Entry in the signatured_types hash table. */
564
565struct signatured_type
566{
42e7ad6c
DE
567 /* The "per_cu" object of this type.
568 N.B.: This is the first member so that it's easy to convert pointers
569 between them. */
570 struct dwarf2_per_cu_data per_cu;
571
3019eac3 572 /* The type's signature. */
348e048f
DE
573 ULONGEST signature;
574
3019eac3
DE
575 /* Offset in the TU of the type's DIE, as read from the TU header.
576 If the definition lives in a DWO file, this value is unusable. */
577 cu_offset type_offset_in_tu;
578
579 /* Offset in the section of the type's DIE.
580 If the definition lives in a DWO file, this is the offset in the
581 .debug_types.dwo section.
582 The value is zero until the actual value is known.
583 Zero is otherwise not a valid section offset. */
584 sect_offset type_offset_in_section;
348e048f
DE
585};
586
094b34ac
DE
587/* A struct that can be used as a hash key for tables based on DW_AT_stmt_list.
588 This includes type_unit_group and quick_file_names. */
589
590struct stmt_list_hash
591{
592 /* The DWO unit this table is from or NULL if there is none. */
593 struct dwo_unit *dwo_unit;
594
595 /* Offset in .debug_line or .debug_line.dwo. */
596 sect_offset line_offset;
597};
598
f4dc4d17
DE
599/* Each element of dwarf2_per_objfile->type_unit_groups is a pointer to
600 an object of this type. */
601
602struct type_unit_group
603{
604 /* dwarf2read.c's main "handle" on the symtab.
605 To simplify things we create an artificial CU that "includes" all the
606 type units using this stmt_list so that the rest of the code still has
607 a "per_cu" handle on the symtab.
608 This PER_CU is recognized by having no section. */
609#define IS_TYPE_UNIT_GROUP(per_cu) ((per_cu)->info_or_types_section == NULL)
094b34ac
DE
610 struct dwarf2_per_cu_data per_cu;
611
612 union
613 {
614 /* The TUs that share this DW_AT_stmt_list entry.
615 This is added to while parsing type units to build partial symtabs,
616 and is deleted afterwards and not used again. */
617 VEC (dwarf2_per_cu_ptr) *tus;
f4dc4d17 618
094b34ac
DE
619 /* When reading the line table in "quick" functions, we need a real TU.
620 Any will do, we know they all share the same DW_AT_stmt_list entry.
621 For simplicity's sake, we pick the first one. */
622 struct dwarf2_per_cu_data *first_tu;
623 } t;
f4dc4d17
DE
624
625 /* The primary symtab.
094b34ac
DE
626 Type units in a group needn't all be defined in the same source file,
627 so we create an essentially anonymous symtab as the primary symtab. */
f4dc4d17
DE
628 struct symtab *primary_symtab;
629
094b34ac
DE
630 /* The data used to construct the hash key. */
631 struct stmt_list_hash hash;
f4dc4d17
DE
632
633 /* The number of symtabs from the line header.
634 The value here must match line_header.num_file_names. */
635 unsigned int num_symtabs;
636
637 /* The symbol tables for this TU (obtained from the files listed in
638 DW_AT_stmt_list).
639 WARNING: The order of entries here must match the order of entries
640 in the line header. After the first TU using this type_unit_group, the
641 line header for the subsequent TUs is recreated from this. This is done
642 because we need to use the same symtabs for each TU using the same
643 DW_AT_stmt_list value. Also note that symtabs may be repeated here,
644 there's no guarantee the line header doesn't have duplicate entries. */
645 struct symtab **symtabs;
646};
647
3019eac3
DE
648/* These sections are what may appear in a "dwo" file. */
649
650struct dwo_sections
651{
652 struct dwarf2_section_info abbrev;
653 struct dwarf2_section_info info;
654 struct dwarf2_section_info line;
655 struct dwarf2_section_info loc;
09262596
DE
656 struct dwarf2_section_info macinfo;
657 struct dwarf2_section_info macro;
3019eac3
DE
658 struct dwarf2_section_info str;
659 struct dwarf2_section_info str_offsets;
660 VEC (dwarf2_section_info_def) *types;
661};
662
663/* Common bits of DWO CUs/TUs. */
664
665struct dwo_unit
666{
667 /* Backlink to the containing struct dwo_file. */
668 struct dwo_file *dwo_file;
669
670 /* The "id" that distinguishes this CU/TU.
671 .debug_info calls this "dwo_id", .debug_types calls this "signature".
672 Since signatures came first, we stick with it for consistency. */
673 ULONGEST signature;
674
675 /* The section this CU/TU lives in, in the DWO file. */
676 struct dwarf2_section_info *info_or_types_section;
677
678 /* Same as dwarf2_per_cu_data:{offset,length} but for the DWO section. */
679 sect_offset offset;
680 unsigned int length;
681
682 /* For types, offset in the type's DIE of the type defined by this TU. */
683 cu_offset type_offset_in_tu;
684};
685
686/* Data for one DWO file. */
687
688struct dwo_file
689{
690 /* The DW_AT_GNU_dwo_name attribute.
691 We don't manage space for this, it's an attribute. */
692 const char *dwo_name;
693
694 /* The bfd, when the file is open. Otherwise this is NULL. */
695 bfd *dwo_bfd;
696
697 /* Section info for this file. */
698 struct dwo_sections sections;
699
700 /* Table of CUs in the file.
701 Each element is a struct dwo_unit. */
702 htab_t cus;
703
704 /* Table of TUs in the file.
705 Each element is a struct dwo_unit. */
706 htab_t tus;
707};
708
36586728
TT
709/* This represents a '.dwz' file. */
710
711struct dwz_file
712{
713 /* A dwz file can only contain a few sections. */
714 struct dwarf2_section_info abbrev;
715 struct dwarf2_section_info info;
716 struct dwarf2_section_info str;
717 struct dwarf2_section_info line;
718 struct dwarf2_section_info macro;
2ec9a5e0 719 struct dwarf2_section_info gdb_index;
36586728
TT
720
721 /* The dwz's BFD. */
722 bfd *dwz_bfd;
723};
724
0963b4bd
MS
725/* Struct used to pass misc. parameters to read_die_and_children, et
726 al. which are used for both .debug_info and .debug_types dies.
727 All parameters here are unchanging for the life of the call. This
dee91e82 728 struct exists to abstract away the constant parameters of die reading. */
93311388
DE
729
730struct die_reader_specs
731{
dee91e82 732 /* die_section->asection->owner. */
93311388
DE
733 bfd* abfd;
734
735 /* The CU of the DIE we are parsing. */
736 struct dwarf2_cu *cu;
737
3019eac3
DE
738 /* Non-NULL if reading a DWO file. */
739 struct dwo_file *dwo_file;
740
dee91e82 741 /* The section the die comes from.
3019eac3 742 This is either .debug_info or .debug_types, or the .dwo variants. */
dee91e82
DE
743 struct dwarf2_section_info *die_section;
744
745 /* die_section->buffer. */
746 gdb_byte *buffer;
f664829e
DE
747
748 /* The end of the buffer. */
749 const gdb_byte *buffer_end;
93311388
DE
750};
751
fd820528 752/* Type of function passed to init_cutu_and_read_dies, et.al. */
dee91e82
DE
753typedef void (die_reader_func_ftype) (const struct die_reader_specs *reader,
754 gdb_byte *info_ptr,
755 struct die_info *comp_unit_die,
756 int has_children,
757 void *data);
758
debd256d
JB
759/* The line number information for a compilation unit (found in the
760 .debug_line section) begins with a "statement program header",
761 which contains the following information. */
762struct line_header
763{
764 unsigned int total_length;
765 unsigned short version;
766 unsigned int header_length;
767 unsigned char minimum_instruction_length;
2dc7f7b3 768 unsigned char maximum_ops_per_instruction;
debd256d
JB
769 unsigned char default_is_stmt;
770 int line_base;
771 unsigned char line_range;
772 unsigned char opcode_base;
773
774 /* standard_opcode_lengths[i] is the number of operands for the
775 standard opcode whose value is i. This means that
776 standard_opcode_lengths[0] is unused, and the last meaningful
777 element is standard_opcode_lengths[opcode_base - 1]. */
778 unsigned char *standard_opcode_lengths;
779
780 /* The include_directories table. NOTE! These strings are not
781 allocated with xmalloc; instead, they are pointers into
782 debug_line_buffer. If you try to free them, `free' will get
783 indigestion. */
784 unsigned int num_include_dirs, include_dirs_size;
785 char **include_dirs;
786
787 /* The file_names table. NOTE! These strings are not allocated
788 with xmalloc; instead, they are pointers into debug_line_buffer.
789 Don't try to free them directly. */
790 unsigned int num_file_names, file_names_size;
791 struct file_entry
c906108c 792 {
debd256d
JB
793 char *name;
794 unsigned int dir_index;
795 unsigned int mod_time;
796 unsigned int length;
aaa75496 797 int included_p; /* Non-zero if referenced by the Line Number Program. */
cb1df416 798 struct symtab *symtab; /* The associated symbol table, if any. */
debd256d
JB
799 } *file_names;
800
801 /* The start and end of the statement program following this
6502dd73 802 header. These point into dwarf2_per_objfile->line_buffer. */
fe1b8b76 803 gdb_byte *statement_program_start, *statement_program_end;
debd256d 804};
c906108c
SS
805
806/* When we construct a partial symbol table entry we only
0963b4bd 807 need this much information. */
c906108c
SS
808struct partial_die_info
809 {
72bf9492 810 /* Offset of this DIE. */
b64f50a1 811 sect_offset offset;
72bf9492
DJ
812
813 /* DWARF-2 tag for this DIE. */
814 ENUM_BITFIELD(dwarf_tag) tag : 16;
815
72bf9492
DJ
816 /* Assorted flags describing the data found in this DIE. */
817 unsigned int has_children : 1;
818 unsigned int is_external : 1;
819 unsigned int is_declaration : 1;
820 unsigned int has_type : 1;
821 unsigned int has_specification : 1;
822 unsigned int has_pc_info : 1;
481860b3 823 unsigned int may_be_inlined : 1;
72bf9492
DJ
824
825 /* Flag set if the SCOPE field of this structure has been
826 computed. */
827 unsigned int scope_set : 1;
828
fa4028e9
JB
829 /* Flag set if the DIE has a byte_size attribute. */
830 unsigned int has_byte_size : 1;
831
98bfdba5
PA
832 /* Flag set if any of the DIE's children are template arguments. */
833 unsigned int has_template_arguments : 1;
834
abc72ce4
DE
835 /* Flag set if fixup_partial_die has been called on this die. */
836 unsigned int fixup_called : 1;
837
36586728
TT
838 /* Flag set if DW_TAG_imported_unit uses DW_FORM_GNU_ref_alt. */
839 unsigned int is_dwz : 1;
840
841 /* Flag set if spec_offset uses DW_FORM_GNU_ref_alt. */
842 unsigned int spec_is_dwz : 1;
843
72bf9492 844 /* The name of this DIE. Normally the value of DW_AT_name, but
94af9270 845 sometimes a default name for unnamed DIEs. */
c906108c 846 char *name;
72bf9492 847
abc72ce4
DE
848 /* The linkage name, if present. */
849 const char *linkage_name;
850
72bf9492
DJ
851 /* The scope to prepend to our children. This is generally
852 allocated on the comp_unit_obstack, so will disappear
853 when this compilation unit leaves the cache. */
854 char *scope;
855
95554aad
TT
856 /* Some data associated with the partial DIE. The tag determines
857 which field is live. */
858 union
859 {
860 /* The location description associated with this DIE, if any. */
861 struct dwarf_block *locdesc;
862 /* The offset of an import, for DW_TAG_imported_unit. */
863 sect_offset offset;
864 } d;
72bf9492
DJ
865
866 /* If HAS_PC_INFO, the PC range associated with this DIE. */
c906108c
SS
867 CORE_ADDR lowpc;
868 CORE_ADDR highpc;
72bf9492 869
93311388 870 /* Pointer into the info_buffer (or types_buffer) pointing at the target of
72bf9492 871 DW_AT_sibling, if any. */
abc72ce4
DE
872 /* NOTE: This member isn't strictly necessary, read_partial_die could
873 return DW_AT_sibling values to its caller load_partial_dies. */
fe1b8b76 874 gdb_byte *sibling;
72bf9492
DJ
875
876 /* If HAS_SPECIFICATION, the offset of the DIE referred to by
877 DW_AT_specification (or DW_AT_abstract_origin or
878 DW_AT_extension). */
b64f50a1 879 sect_offset spec_offset;
72bf9492
DJ
880
881 /* Pointers to this DIE's parent, first child, and next sibling,
882 if any. */
883 struct partial_die_info *die_parent, *die_child, *die_sibling;
c906108c
SS
884 };
885
0963b4bd 886/* This data structure holds the information of an abbrev. */
c906108c
SS
887struct abbrev_info
888 {
889 unsigned int number; /* number identifying abbrev */
890 enum dwarf_tag tag; /* dwarf tag */
f3dd6933
DJ
891 unsigned short has_children; /* boolean */
892 unsigned short num_attrs; /* number of attributes */
c906108c
SS
893 struct attr_abbrev *attrs; /* an array of attribute descriptions */
894 struct abbrev_info *next; /* next in chain */
895 };
896
897struct attr_abbrev
898 {
9d25dd43
DE
899 ENUM_BITFIELD(dwarf_attribute) name : 16;
900 ENUM_BITFIELD(dwarf_form) form : 16;
c906108c
SS
901 };
902
433df2d4
DE
903/* Size of abbrev_table.abbrev_hash_table. */
904#define ABBREV_HASH_SIZE 121
905
906/* Top level data structure to contain an abbreviation table. */
907
908struct abbrev_table
909{
f4dc4d17
DE
910 /* Where the abbrev table came from.
911 This is used as a sanity check when the table is used. */
433df2d4
DE
912 sect_offset offset;
913
914 /* Storage for the abbrev table. */
915 struct obstack abbrev_obstack;
916
917 /* Hash table of abbrevs.
918 This is an array of size ABBREV_HASH_SIZE allocated in abbrev_obstack.
919 It could be statically allocated, but the previous code didn't so we
920 don't either. */
921 struct abbrev_info **abbrevs;
922};
923
0963b4bd 924/* Attributes have a name and a value. */
b60c80d6
DJ
925struct attribute
926 {
9d25dd43 927 ENUM_BITFIELD(dwarf_attribute) name : 16;
8285870a
JK
928 ENUM_BITFIELD(dwarf_form) form : 15;
929
930 /* Has DW_STRING already been updated by dwarf2_canonicalize_name? This
931 field should be in u.str (existing only for DW_STRING) but it is kept
932 here for better struct attribute alignment. */
933 unsigned int string_is_canonical : 1;
934
b60c80d6
DJ
935 union
936 {
937 char *str;
938 struct dwarf_block *blk;
43bbcdc2
PH
939 ULONGEST unsnd;
940 LONGEST snd;
b60c80d6 941 CORE_ADDR addr;
348e048f 942 struct signatured_type *signatured_type;
b60c80d6
DJ
943 }
944 u;
945 };
946
0963b4bd 947/* This data structure holds a complete die structure. */
c906108c
SS
948struct die_info
949 {
76815b17
DE
950 /* DWARF-2 tag for this DIE. */
951 ENUM_BITFIELD(dwarf_tag) tag : 16;
952
953 /* Number of attributes */
98bfdba5
PA
954 unsigned char num_attrs;
955
956 /* True if we're presently building the full type name for the
957 type derived from this DIE. */
958 unsigned char building_fullname : 1;
76815b17
DE
959
960 /* Abbrev number */
961 unsigned int abbrev;
962
93311388 963 /* Offset in .debug_info or .debug_types section. */
b64f50a1 964 sect_offset offset;
78ba4af6
JB
965
966 /* The dies in a compilation unit form an n-ary tree. PARENT
967 points to this die's parent; CHILD points to the first child of
968 this node; and all the children of a given node are chained
4950bc1c 969 together via their SIBLING fields. */
639d11d3
DC
970 struct die_info *child; /* Its first child, if any. */
971 struct die_info *sibling; /* Its next sibling, if any. */
972 struct die_info *parent; /* Its parent, if any. */
c906108c 973
b60c80d6
DJ
974 /* An array of attributes, with NUM_ATTRS elements. There may be
975 zero, but it's not common and zero-sized arrays are not
976 sufficiently portable C. */
977 struct attribute attrs[1];
c906108c
SS
978 };
979
0963b4bd 980/* Get at parts of an attribute structure. */
c906108c
SS
981
982#define DW_STRING(attr) ((attr)->u.str)
8285870a 983#define DW_STRING_IS_CANONICAL(attr) ((attr)->string_is_canonical)
c906108c
SS
984#define DW_UNSND(attr) ((attr)->u.unsnd)
985#define DW_BLOCK(attr) ((attr)->u.blk)
986#define DW_SND(attr) ((attr)->u.snd)
987#define DW_ADDR(attr) ((attr)->u.addr)
348e048f 988#define DW_SIGNATURED_TYPE(attr) ((attr)->u.signatured_type)
c906108c 989
0963b4bd 990/* Blocks are a bunch of untyped bytes. */
c906108c
SS
991struct dwarf_block
992 {
56eb65bd 993 size_t size;
1d6edc3c
JK
994
995 /* Valid only if SIZE is not zero. */
fe1b8b76 996 gdb_byte *data;
c906108c
SS
997 };
998
c906108c
SS
999#ifndef ATTR_ALLOC_CHUNK
1000#define ATTR_ALLOC_CHUNK 4
1001#endif
1002
c906108c
SS
1003/* Allocate fields for structs, unions and enums in this size. */
1004#ifndef DW_FIELD_ALLOC_CHUNK
1005#define DW_FIELD_ALLOC_CHUNK 4
1006#endif
1007
c906108c
SS
1008/* FIXME: We might want to set this from BFD via bfd_arch_bits_per_byte,
1009 but this would require a corresponding change in unpack_field_as_long
1010 and friends. */
1011static int bits_per_byte = 8;
1012
1013/* The routines that read and process dies for a C struct or C++ class
1014 pass lists of data member fields and lists of member function fields
1015 in an instance of a field_info structure, as defined below. */
1016struct field_info
c5aa993b 1017 {
0963b4bd 1018 /* List of data member and baseclasses fields. */
c5aa993b
JM
1019 struct nextfield
1020 {
1021 struct nextfield *next;
1022 int accessibility;
1023 int virtuality;
1024 struct field field;
1025 }
7d0ccb61 1026 *fields, *baseclasses;
c906108c 1027
7d0ccb61 1028 /* Number of fields (including baseclasses). */
c5aa993b 1029 int nfields;
c906108c 1030
c5aa993b
JM
1031 /* Number of baseclasses. */
1032 int nbaseclasses;
c906108c 1033
c5aa993b
JM
1034 /* Set if the accesibility of one of the fields is not public. */
1035 int non_public_fields;
c906108c 1036
c5aa993b
JM
1037 /* Member function fields array, entries are allocated in the order they
1038 are encountered in the object file. */
1039 struct nextfnfield
1040 {
1041 struct nextfnfield *next;
1042 struct fn_field fnfield;
1043 }
1044 *fnfields;
c906108c 1045
c5aa993b
JM
1046 /* Member function fieldlist array, contains name of possibly overloaded
1047 member function, number of overloaded member functions and a pointer
1048 to the head of the member function field chain. */
1049 struct fnfieldlist
1050 {
1051 char *name;
1052 int length;
1053 struct nextfnfield *head;
1054 }
1055 *fnfieldlists;
c906108c 1056
c5aa993b
JM
1057 /* Number of entries in the fnfieldlists array. */
1058 int nfnfields;
98751a41
JK
1059
1060 /* typedefs defined inside this class. TYPEDEF_FIELD_LIST contains head of
1061 a NULL terminated list of TYPEDEF_FIELD_LIST_COUNT elements. */
1062 struct typedef_field_list
1063 {
1064 struct typedef_field field;
1065 struct typedef_field_list *next;
1066 }
1067 *typedef_field_list;
1068 unsigned typedef_field_list_count;
c5aa993b 1069 };
c906108c 1070
10b3939b
DJ
1071/* One item on the queue of compilation units to read in full symbols
1072 for. */
1073struct dwarf2_queue_item
1074{
1075 struct dwarf2_per_cu_data *per_cu;
95554aad 1076 enum language pretend_language;
10b3939b
DJ
1077 struct dwarf2_queue_item *next;
1078};
1079
1080/* The current queue. */
1081static struct dwarf2_queue_item *dwarf2_queue, *dwarf2_queue_tail;
1082
ae038cb0
DJ
1083/* Loaded secondary compilation units are kept in memory until they
1084 have not been referenced for the processing of this many
1085 compilation units. Set this to zero to disable caching. Cache
1086 sizes of up to at least twenty will improve startup time for
1087 typical inter-CU-reference binaries, at an obvious memory cost. */
1088static int dwarf2_max_cache_age = 5;
920d2a44
AC
1089static void
1090show_dwarf2_max_cache_age (struct ui_file *file, int from_tty,
1091 struct cmd_list_element *c, const char *value)
1092{
3e43a32a
MS
1093 fprintf_filtered (file, _("The upper bound on the age of cached "
1094 "dwarf2 compilation units is %s.\n"),
920d2a44
AC
1095 value);
1096}
1097
ae038cb0 1098
0963b4bd 1099/* Various complaints about symbol reading that don't abort the process. */
c906108c 1100
4d3c2250
KB
1101static void
1102dwarf2_statement_list_fits_in_line_number_section_complaint (void)
2e276125 1103{
4d3c2250 1104 complaint (&symfile_complaints,
e2e0b3e5 1105 _("statement list doesn't fit in .debug_line section"));
4d3c2250
KB
1106}
1107
25e43795
DJ
1108static void
1109dwarf2_debug_line_missing_file_complaint (void)
1110{
1111 complaint (&symfile_complaints,
1112 _(".debug_line section has line data without a file"));
1113}
1114
59205f5a
JB
1115static void
1116dwarf2_debug_line_missing_end_sequence_complaint (void)
1117{
1118 complaint (&symfile_complaints,
3e43a32a
MS
1119 _(".debug_line section has line "
1120 "program sequence without an end"));
59205f5a
JB
1121}
1122
4d3c2250
KB
1123static void
1124dwarf2_complex_location_expr_complaint (void)
2e276125 1125{
e2e0b3e5 1126 complaint (&symfile_complaints, _("location expression too complex"));
4d3c2250
KB
1127}
1128
4d3c2250
KB
1129static void
1130dwarf2_const_value_length_mismatch_complaint (const char *arg1, int arg2,
1131 int arg3)
2e276125 1132{
4d3c2250 1133 complaint (&symfile_complaints,
3e43a32a
MS
1134 _("const value length mismatch for '%s', got %d, expected %d"),
1135 arg1, arg2, arg3);
4d3c2250
KB
1136}
1137
1138static void
f664829e 1139dwarf2_section_buffer_overflow_complaint (struct dwarf2_section_info *section)
2e276125 1140{
4d3c2250 1141 complaint (&symfile_complaints,
f664829e
DE
1142 _("debug info runs off end of %s section"
1143 " [in module %s]"),
1144 section->asection->name,
1145 bfd_get_filename (section->asection->owner));
4d3c2250
KB
1146}
1147
1148static void
1149dwarf2_macro_malformed_definition_complaint (const char *arg1)
8e19ed76 1150{
4d3c2250 1151 complaint (&symfile_complaints,
3e43a32a
MS
1152 _("macro debug info contains a "
1153 "malformed macro definition:\n`%s'"),
4d3c2250
KB
1154 arg1);
1155}
1156
1157static void
1158dwarf2_invalid_attrib_class_complaint (const char *arg1, const char *arg2)
8b2dbe47 1159{
4d3c2250 1160 complaint (&symfile_complaints,
3e43a32a
MS
1161 _("invalid attribute class or form for '%s' in '%s'"),
1162 arg1, arg2);
4d3c2250 1163}
c906108c 1164
c906108c
SS
1165/* local function prototypes */
1166
4efb68b1 1167static void dwarf2_locate_sections (bfd *, asection *, void *);
c906108c 1168
aaa75496
JB
1169static void dwarf2_create_include_psymtab (char *, struct partial_symtab *,
1170 struct objfile *);
1171
918dd910
JK
1172static void dwarf2_find_base_address (struct die_info *die,
1173 struct dwarf2_cu *cu);
1174
c67a9c90 1175static void dwarf2_build_psymtabs_hard (struct objfile *);
c906108c 1176
72bf9492
DJ
1177static void scan_partial_symbols (struct partial_die_info *,
1178 CORE_ADDR *, CORE_ADDR *,
5734ee8b 1179 int, struct dwarf2_cu *);
c906108c 1180
72bf9492
DJ
1181static void add_partial_symbol (struct partial_die_info *,
1182 struct dwarf2_cu *);
63d06c5c 1183
72bf9492
DJ
1184static void add_partial_namespace (struct partial_die_info *pdi,
1185 CORE_ADDR *lowpc, CORE_ADDR *highpc,
5734ee8b 1186 int need_pc, struct dwarf2_cu *cu);
63d06c5c 1187
5d7cb8df
JK
1188static void add_partial_module (struct partial_die_info *pdi, CORE_ADDR *lowpc,
1189 CORE_ADDR *highpc, int need_pc,
1190 struct dwarf2_cu *cu);
1191
72bf9492
DJ
1192static void add_partial_enumeration (struct partial_die_info *enum_pdi,
1193 struct dwarf2_cu *cu);
91c24f0a 1194
bc30ff58
JB
1195static void add_partial_subprogram (struct partial_die_info *pdi,
1196 CORE_ADDR *lowpc, CORE_ADDR *highpc,
5734ee8b 1197 int need_pc, struct dwarf2_cu *cu);
bc30ff58 1198
a14ed312 1199static void dwarf2_psymtab_to_symtab (struct partial_symtab *);
c906108c 1200
a14ed312 1201static void psymtab_to_symtab_1 (struct partial_symtab *);
c906108c 1202
433df2d4
DE
1203static struct abbrev_info *abbrev_table_lookup_abbrev
1204 (const struct abbrev_table *, unsigned int);
1205
1206static struct abbrev_table *abbrev_table_read_table
1207 (struct dwarf2_section_info *, sect_offset);
1208
1209static void abbrev_table_free (struct abbrev_table *);
1210
f4dc4d17
DE
1211static void abbrev_table_free_cleanup (void *);
1212
dee91e82
DE
1213static void dwarf2_read_abbrevs (struct dwarf2_cu *,
1214 struct dwarf2_section_info *);
c906108c 1215
f3dd6933 1216static void dwarf2_free_abbrev_table (void *);
c906108c 1217
6caca83c
CC
1218static unsigned int peek_abbrev_code (bfd *, gdb_byte *);
1219
dee91e82
DE
1220static struct partial_die_info *load_partial_dies
1221 (const struct die_reader_specs *, gdb_byte *, int);
72bf9492 1222
dee91e82
DE
1223static gdb_byte *read_partial_die (const struct die_reader_specs *,
1224 struct partial_die_info *,
1225 struct abbrev_info *,
1226 unsigned int,
1227 gdb_byte *);
c906108c 1228
36586728 1229static struct partial_die_info *find_partial_die (sect_offset, int,
10b3939b 1230 struct dwarf2_cu *);
72bf9492
DJ
1231
1232static void fixup_partial_die (struct partial_die_info *,
1233 struct dwarf2_cu *);
1234
dee91e82
DE
1235static gdb_byte *read_attribute (const struct die_reader_specs *,
1236 struct attribute *, struct attr_abbrev *,
1237 gdb_byte *);
a8329558 1238
fe1b8b76 1239static unsigned int read_1_byte (bfd *, gdb_byte *);
c906108c 1240
fe1b8b76 1241static int read_1_signed_byte (bfd *, gdb_byte *);
c906108c 1242
fe1b8b76 1243static unsigned int read_2_bytes (bfd *, gdb_byte *);
c906108c 1244
fe1b8b76 1245static unsigned int read_4_bytes (bfd *, gdb_byte *);
c906108c 1246
93311388 1247static ULONGEST read_8_bytes (bfd *, gdb_byte *);
c906108c 1248
fe1b8b76 1249static CORE_ADDR read_address (bfd *, gdb_byte *ptr, struct dwarf2_cu *,
891d2f0b 1250 unsigned int *);
c906108c 1251
c764a876
DE
1252static LONGEST read_initial_length (bfd *, gdb_byte *, unsigned int *);
1253
1254static LONGEST read_checked_initial_length_and_offset
1255 (bfd *, gdb_byte *, const struct comp_unit_head *,
1256 unsigned int *, unsigned int *);
613e1657 1257
fe1b8b76 1258static LONGEST read_offset (bfd *, gdb_byte *, const struct comp_unit_head *,
c764a876
DE
1259 unsigned int *);
1260
1261static LONGEST read_offset_1 (bfd *, gdb_byte *, unsigned int);
613e1657 1262
f4dc4d17
DE
1263static sect_offset read_abbrev_offset (struct dwarf2_section_info *,
1264 sect_offset);
1265
fe1b8b76 1266static gdb_byte *read_n_bytes (bfd *, gdb_byte *, unsigned int);
c906108c 1267
9b1c24c8 1268static char *read_direct_string (bfd *, gdb_byte *, unsigned int *);
c906108c 1269
fe1b8b76
JB
1270static char *read_indirect_string (bfd *, gdb_byte *,
1271 const struct comp_unit_head *,
1272 unsigned int *);
4bdf3d34 1273
36586728
TT
1274static char *read_indirect_string_from_dwz (struct dwz_file *, LONGEST);
1275
12df843f 1276static ULONGEST read_unsigned_leb128 (bfd *, gdb_byte *, unsigned int *);
c906108c 1277
12df843f 1278static LONGEST read_signed_leb128 (bfd *, gdb_byte *, unsigned int *);
c906108c 1279
3019eac3
DE
1280static CORE_ADDR read_addr_index_from_leb128 (struct dwarf2_cu *, gdb_byte *,
1281 unsigned int *);
1282
1283static char *read_str_index (const struct die_reader_specs *reader,
1284 struct dwarf2_cu *cu, ULONGEST str_index);
1285
e142c38c 1286static void set_cu_language (unsigned int, struct dwarf2_cu *);
c906108c 1287
e142c38c
DJ
1288static struct attribute *dwarf2_attr (struct die_info *, unsigned int,
1289 struct dwarf2_cu *);
c906108c 1290
348e048f 1291static struct attribute *dwarf2_attr_no_follow (struct die_info *,
45e58e77 1292 unsigned int);
348e048f 1293
05cf31d1
JB
1294static int dwarf2_flag_true_p (struct die_info *die, unsigned name,
1295 struct dwarf2_cu *cu);
1296
e142c38c 1297static int die_is_declaration (struct die_info *, struct dwarf2_cu *cu);
3ca72b44 1298
e142c38c 1299static struct die_info *die_specification (struct die_info *die,
f2f0e013 1300 struct dwarf2_cu **);
63d06c5c 1301
debd256d
JB
1302static void free_line_header (struct line_header *lh);
1303
aaa75496
JB
1304static void add_file_name (struct line_header *, char *, unsigned int,
1305 unsigned int, unsigned int);
1306
3019eac3
DE
1307static struct line_header *dwarf_decode_line_header (unsigned int offset,
1308 struct dwarf2_cu *cu);
debd256d 1309
f3f5162e
DE
1310static void dwarf_decode_lines (struct line_header *, const char *,
1311 struct dwarf2_cu *, struct partial_symtab *,
1312 int);
c906108c 1313
72b9f47f 1314static void dwarf2_start_subfile (char *, const char *, const char *);
c906108c 1315
f4dc4d17
DE
1316static void dwarf2_start_symtab (struct dwarf2_cu *,
1317 char *, char *, CORE_ADDR);
1318
a14ed312 1319static struct symbol *new_symbol (struct die_info *, struct type *,
e7c27a73 1320 struct dwarf2_cu *);
c906108c 1321
34eaf542
TT
1322static struct symbol *new_symbol_full (struct die_info *, struct type *,
1323 struct dwarf2_cu *, struct symbol *);
1324
a14ed312 1325static void dwarf2_const_value (struct attribute *, struct symbol *,
e7c27a73 1326 struct dwarf2_cu *);
c906108c 1327
98bfdba5
PA
1328static void dwarf2_const_value_attr (struct attribute *attr,
1329 struct type *type,
1330 const char *name,
1331 struct obstack *obstack,
12df843f 1332 struct dwarf2_cu *cu, LONGEST *value,
98bfdba5
PA
1333 gdb_byte **bytes,
1334 struct dwarf2_locexpr_baton **baton);
2df3850c 1335
e7c27a73 1336static struct type *die_type (struct die_info *, struct dwarf2_cu *);
c906108c 1337
b4ba55a1
JB
1338static int need_gnat_info (struct dwarf2_cu *);
1339
3e43a32a
MS
1340static struct type *die_descriptive_type (struct die_info *,
1341 struct dwarf2_cu *);
b4ba55a1
JB
1342
1343static void set_descriptive_type (struct type *, struct die_info *,
1344 struct dwarf2_cu *);
1345
e7c27a73
DJ
1346static struct type *die_containing_type (struct die_info *,
1347 struct dwarf2_cu *);
c906108c 1348
673bfd45
DE
1349static struct type *lookup_die_type (struct die_info *, struct attribute *,
1350 struct dwarf2_cu *);
c906108c 1351
f792889a 1352static struct type *read_type_die (struct die_info *, struct dwarf2_cu *);
c906108c 1353
673bfd45
DE
1354static struct type *read_type_die_1 (struct die_info *, struct dwarf2_cu *);
1355
0d5cff50 1356static const char *determine_prefix (struct die_info *die, struct dwarf2_cu *);
63d06c5c 1357
6e70227d 1358static char *typename_concat (struct obstack *obs, const char *prefix,
f55ee35c
JK
1359 const char *suffix, int physname,
1360 struct dwarf2_cu *cu);
63d06c5c 1361
e7c27a73 1362static void read_file_scope (struct die_info *, struct dwarf2_cu *);
c906108c 1363
348e048f
DE
1364static void read_type_unit_scope (struct die_info *, struct dwarf2_cu *);
1365
e7c27a73 1366static void read_func_scope (struct die_info *, struct dwarf2_cu *);
c906108c 1367
e7c27a73 1368static void read_lexical_block_scope (struct die_info *, struct dwarf2_cu *);
c906108c 1369
96408a79
SA
1370static void read_call_site_scope (struct die_info *die, struct dwarf2_cu *cu);
1371
ff013f42
JK
1372static int dwarf2_ranges_read (unsigned, CORE_ADDR *, CORE_ADDR *,
1373 struct dwarf2_cu *, struct partial_symtab *);
1374
a14ed312 1375static int dwarf2_get_pc_bounds (struct die_info *,
d85a05f0
DJ
1376 CORE_ADDR *, CORE_ADDR *, struct dwarf2_cu *,
1377 struct partial_symtab *);
c906108c 1378
fae299cd
DC
1379static void get_scope_pc_bounds (struct die_info *,
1380 CORE_ADDR *, CORE_ADDR *,
1381 struct dwarf2_cu *);
1382
801e3a5b
JB
1383static void dwarf2_record_block_ranges (struct die_info *, struct block *,
1384 CORE_ADDR, struct dwarf2_cu *);
1385
a14ed312 1386static void dwarf2_add_field (struct field_info *, struct die_info *,
e7c27a73 1387 struct dwarf2_cu *);
c906108c 1388
a14ed312 1389static void dwarf2_attach_fields_to_type (struct field_info *,
e7c27a73 1390 struct type *, struct dwarf2_cu *);
c906108c 1391
a14ed312 1392static void dwarf2_add_member_fn (struct field_info *,
e26fb1d7 1393 struct die_info *, struct type *,
e7c27a73 1394 struct dwarf2_cu *);
c906108c 1395
a14ed312 1396static void dwarf2_attach_fn_fields_to_type (struct field_info *,
3e43a32a
MS
1397 struct type *,
1398 struct dwarf2_cu *);
c906108c 1399
134d01f1 1400static void process_structure_scope (struct die_info *, struct dwarf2_cu *);
c906108c 1401
e7c27a73 1402static void read_common_block (struct die_info *, struct dwarf2_cu *);
c906108c 1403
e7c27a73 1404static void read_namespace (struct die_info *die, struct dwarf2_cu *);
d9fa45fe 1405
5d7cb8df
JK
1406static void read_module (struct die_info *die, struct dwarf2_cu *cu);
1407
27aa8d6a
SW
1408static void read_import_statement (struct die_info *die, struct dwarf2_cu *);
1409
f55ee35c
JK
1410static struct type *read_module_type (struct die_info *die,
1411 struct dwarf2_cu *cu);
1412
38d518c9 1413static const char *namespace_name (struct die_info *die,
e142c38c 1414 int *is_anonymous, struct dwarf2_cu *);
38d518c9 1415
134d01f1 1416static void process_enumeration_scope (struct die_info *, struct dwarf2_cu *);
c906108c 1417
e7c27a73 1418static CORE_ADDR decode_locdesc (struct dwarf_block *, struct dwarf2_cu *);
c906108c 1419
6e70227d 1420static enum dwarf_array_dim_ordering read_array_order (struct die_info *,
7ca2d3a3
DL
1421 struct dwarf2_cu *);
1422
dee91e82 1423static struct die_info *read_die_and_children (const struct die_reader_specs *,
93311388 1424 gdb_byte *info_ptr,
fe1b8b76 1425 gdb_byte **new_info_ptr,
639d11d3
DC
1426 struct die_info *parent);
1427
dee91e82 1428static struct die_info *read_die_and_siblings (const struct die_reader_specs *,
93311388 1429 gdb_byte *info_ptr,
fe1b8b76 1430 gdb_byte **new_info_ptr,
639d11d3
DC
1431 struct die_info *parent);
1432
3019eac3
DE
1433static gdb_byte *read_full_die_1 (const struct die_reader_specs *,
1434 struct die_info **, gdb_byte *, int *, int);
1435
dee91e82
DE
1436static gdb_byte *read_full_die (const struct die_reader_specs *,
1437 struct die_info **, gdb_byte *, int *);
93311388 1438
e7c27a73 1439static void process_die (struct die_info *, struct dwarf2_cu *);
c906108c 1440
71c25dea
TT
1441static char *dwarf2_canonicalize_name (char *, struct dwarf2_cu *,
1442 struct obstack *);
1443
e142c38c 1444static char *dwarf2_name (struct die_info *die, struct dwarf2_cu *);
9219021c 1445
98bfdba5
PA
1446static const char *dwarf2_full_name (char *name,
1447 struct die_info *die,
1448 struct dwarf2_cu *cu);
1449
e142c38c 1450static struct die_info *dwarf2_extension (struct die_info *die,
f2f0e013 1451 struct dwarf2_cu **);
9219021c 1452
f39c6ffd 1453static const char *dwarf_tag_name (unsigned int);
c906108c 1454
f39c6ffd 1455static const char *dwarf_attr_name (unsigned int);
c906108c 1456
f39c6ffd 1457static const char *dwarf_form_name (unsigned int);
c906108c 1458
a14ed312 1459static char *dwarf_bool_name (unsigned int);
c906108c 1460
f39c6ffd 1461static const char *dwarf_type_encoding_name (unsigned int);
c906108c 1462
f9aca02d 1463static struct die_info *sibling_die (struct die_info *);
c906108c 1464
d97bc12b
DE
1465static void dump_die_shallow (struct ui_file *, int indent, struct die_info *);
1466
1467static void dump_die_for_error (struct die_info *);
1468
1469static void dump_die_1 (struct ui_file *, int level, int max_level,
1470 struct die_info *);
c906108c 1471
d97bc12b 1472/*static*/ void dump_die (struct die_info *, int max_level);
c906108c 1473
51545339 1474static void store_in_ref_table (struct die_info *,
10b3939b 1475 struct dwarf2_cu *);
c906108c 1476
93311388
DE
1477static int is_ref_attr (struct attribute *);
1478
b64f50a1 1479static sect_offset dwarf2_get_ref_die_offset (struct attribute *);
c906108c 1480
43bbcdc2 1481static LONGEST dwarf2_get_attr_constant_value (struct attribute *, int);
a02abb62 1482
348e048f
DE
1483static struct die_info *follow_die_ref_or_sig (struct die_info *,
1484 struct attribute *,
1485 struct dwarf2_cu **);
1486
10b3939b
DJ
1487static struct die_info *follow_die_ref (struct die_info *,
1488 struct attribute *,
f2f0e013 1489 struct dwarf2_cu **);
c906108c 1490
348e048f
DE
1491static struct die_info *follow_die_sig (struct die_info *,
1492 struct attribute *,
1493 struct dwarf2_cu **);
1494
6c83ed52
TT
1495static struct signatured_type *lookup_signatured_type_at_offset
1496 (struct objfile *objfile,
b64f50a1 1497 struct dwarf2_section_info *section, sect_offset offset);
6c83ed52 1498
e5fe5e75 1499static void load_full_type_unit (struct dwarf2_per_cu_data *per_cu);
348e048f 1500
52dc124a 1501static void read_signatured_type (struct signatured_type *);
348e048f 1502
f4dc4d17 1503static struct type_unit_group *get_type_unit_group
094b34ac 1504 (struct dwarf2_cu *, struct attribute *);
f4dc4d17
DE
1505
1506static void build_type_unit_groups (die_reader_func_ftype *, void *);
1507
c906108c
SS
1508/* memory allocation interface */
1509
7b5a2f43 1510static struct dwarf_block *dwarf_alloc_block (struct dwarf2_cu *);
c906108c 1511
b60c80d6 1512static struct die_info *dwarf_alloc_die (struct dwarf2_cu *, int);
c906108c 1513
09262596
DE
1514static void dwarf_decode_macros (struct dwarf2_cu *, unsigned int,
1515 char *, int);
2e276125 1516
8e19ed76
PS
1517static int attr_form_is_block (struct attribute *);
1518
3690dd37
JB
1519static int attr_form_is_section_offset (struct attribute *);
1520
1521static int attr_form_is_constant (struct attribute *);
1522
8cf6f0b1
TT
1523static void fill_in_loclist_baton (struct dwarf2_cu *cu,
1524 struct dwarf2_loclist_baton *baton,
1525 struct attribute *attr);
1526
93e7bd98
DJ
1527static void dwarf2_symbol_mark_computed (struct attribute *attr,
1528 struct symbol *sym,
1529 struct dwarf2_cu *cu);
4c2df51b 1530
dee91e82
DE
1531static gdb_byte *skip_one_die (const struct die_reader_specs *reader,
1532 gdb_byte *info_ptr,
1533 struct abbrev_info *abbrev);
4bb7a0a7 1534
72bf9492
DJ
1535static void free_stack_comp_unit (void *);
1536
72bf9492
DJ
1537static hashval_t partial_die_hash (const void *item);
1538
1539static int partial_die_eq (const void *item_lhs, const void *item_rhs);
1540
ae038cb0 1541static struct dwarf2_per_cu_data *dwarf2_find_containing_comp_unit
36586728 1542 (sect_offset offset, unsigned int offset_in_dwz, struct objfile *objfile);
ae038cb0 1543
9816fde3 1544static void init_one_comp_unit (struct dwarf2_cu *cu,
23745b47 1545 struct dwarf2_per_cu_data *per_cu);
9816fde3
JK
1546
1547static void prepare_one_comp_unit (struct dwarf2_cu *cu,
95554aad
TT
1548 struct die_info *comp_unit_die,
1549 enum language pretend_language);
93311388 1550
68dc6402 1551static void free_heap_comp_unit (void *);
ae038cb0
DJ
1552
1553static void free_cached_comp_units (void *);
1554
1555static void age_cached_comp_units (void);
1556
dee91e82 1557static void free_one_cached_comp_unit (struct dwarf2_per_cu_data *);
ae038cb0 1558
f792889a
DJ
1559static struct type *set_die_type (struct die_info *, struct type *,
1560 struct dwarf2_cu *);
1c379e20 1561
ae038cb0
DJ
1562static void create_all_comp_units (struct objfile *);
1563
0e50663e 1564static int create_all_type_units (struct objfile *);
1fd400ff 1565
95554aad
TT
1566static void load_full_comp_unit (struct dwarf2_per_cu_data *,
1567 enum language);
10b3939b 1568
95554aad
TT
1569static void process_full_comp_unit (struct dwarf2_per_cu_data *,
1570 enum language);
10b3939b 1571
f4dc4d17
DE
1572static void process_full_type_unit (struct dwarf2_per_cu_data *,
1573 enum language);
1574
10b3939b
DJ
1575static void dwarf2_add_dependence (struct dwarf2_cu *,
1576 struct dwarf2_per_cu_data *);
1577
ae038cb0
DJ
1578static void dwarf2_mark (struct dwarf2_cu *);
1579
1580static void dwarf2_clear_marks (struct dwarf2_per_cu_data *);
1581
b64f50a1 1582static struct type *get_die_type_at_offset (sect_offset,
673bfd45
DE
1583 struct dwarf2_per_cu_data *per_cu);
1584
f792889a 1585static struct type *get_die_type (struct die_info *die, struct dwarf2_cu *cu);
72019c9c 1586
9291a0cd
TT
1587static void dwarf2_release_queue (void *dummy);
1588
95554aad
TT
1589static void queue_comp_unit (struct dwarf2_per_cu_data *per_cu,
1590 enum language pretend_language);
1591
1592static int maybe_queue_comp_unit (struct dwarf2_cu *this_cu,
1593 struct dwarf2_per_cu_data *per_cu,
1594 enum language pretend_language);
9291a0cd 1595
a0f42c21 1596static void process_queue (void);
9291a0cd
TT
1597
1598static void find_file_and_directory (struct die_info *die,
1599 struct dwarf2_cu *cu,
1600 char **name, char **comp_dir);
1601
1602static char *file_full_name (int file, struct line_header *lh,
1603 const char *comp_dir);
1604
36586728
TT
1605static gdb_byte *read_and_check_comp_unit_head
1606 (struct comp_unit_head *header,
1607 struct dwarf2_section_info *section,
1608 struct dwarf2_section_info *abbrev_section, gdb_byte *info_ptr,
1609 int is_debug_types_section);
1610
fd820528 1611static void init_cutu_and_read_dies
f4dc4d17
DE
1612 (struct dwarf2_per_cu_data *this_cu, struct abbrev_table *abbrev_table,
1613 int use_existing_cu, int keep,
3019eac3
DE
1614 die_reader_func_ftype *die_reader_func, void *data);
1615
dee91e82
DE
1616static void init_cutu_and_read_dies_simple
1617 (struct dwarf2_per_cu_data *this_cu,
1618 die_reader_func_ftype *die_reader_func, void *data);
9291a0cd 1619
673bfd45 1620static htab_t allocate_signatured_type_table (struct objfile *objfile);
1fd400ff 1621
3019eac3
DE
1622static htab_t allocate_dwo_unit_table (struct objfile *objfile);
1623
1624static struct dwo_unit *lookup_dwo_comp_unit
1625 (struct dwarf2_per_cu_data *, char *, const char *, ULONGEST);
1626
1627static struct dwo_unit *lookup_dwo_type_unit
1628 (struct signatured_type *, char *, const char *);
1629
1630static void free_dwo_file_cleanup (void *);
1631
95554aad
TT
1632static void process_cu_includes (void);
1633
9291a0cd
TT
1634#if WORDS_BIGENDIAN
1635
1636/* Convert VALUE between big- and little-endian. */
1637static offset_type
1638byte_swap (offset_type value)
1639{
1640 offset_type result;
1641
1642 result = (value & 0xff) << 24;
1643 result |= (value & 0xff00) << 8;
1644 result |= (value & 0xff0000) >> 8;
1645 result |= (value & 0xff000000) >> 24;
1646 return result;
1647}
1648
1649#define MAYBE_SWAP(V) byte_swap (V)
1650
1651#else
1652#define MAYBE_SWAP(V) (V)
1653#endif /* WORDS_BIGENDIAN */
1654
1655/* The suffix for an index file. */
1656#define INDEX_SUFFIX ".gdb-index"
1657
3da10d80
KS
1658static const char *dwarf2_physname (char *name, struct die_info *die,
1659 struct dwarf2_cu *cu);
1660
c906108c 1661/* Try to locate the sections we need for DWARF 2 debugging
251d32d9
TG
1662 information and return true if we have enough to do something.
1663 NAMES points to the dwarf2 section names, or is NULL if the standard
1664 ELF names are used. */
c906108c
SS
1665
1666int
251d32d9
TG
1667dwarf2_has_info (struct objfile *objfile,
1668 const struct dwarf2_debug_sections *names)
c906108c 1669{
be391dca
TT
1670 dwarf2_per_objfile = objfile_data (objfile, dwarf2_objfile_data_key);
1671 if (!dwarf2_per_objfile)
1672 {
1673 /* Initialize per-objfile state. */
1674 struct dwarf2_per_objfile *data
1675 = obstack_alloc (&objfile->objfile_obstack, sizeof (*data));
9a619af0 1676
be391dca
TT
1677 memset (data, 0, sizeof (*data));
1678 set_objfile_data (objfile, dwarf2_objfile_data_key, data);
1679 dwarf2_per_objfile = data;
6502dd73 1680
251d32d9
TG
1681 bfd_map_over_sections (objfile->obfd, dwarf2_locate_sections,
1682 (void *) names);
be391dca
TT
1683 dwarf2_per_objfile->objfile = objfile;
1684 }
1685 return (dwarf2_per_objfile->info.asection != NULL
1686 && dwarf2_per_objfile->abbrev.asection != NULL);
c906108c
SS
1687}
1688
251d32d9
TG
1689/* When loading sections, we look either for uncompressed section or for
1690 compressed section names. */
233a11ab
CS
1691
1692static int
251d32d9
TG
1693section_is_p (const char *section_name,
1694 const struct dwarf2_section_names *names)
233a11ab 1695{
251d32d9
TG
1696 if (names->normal != NULL
1697 && strcmp (section_name, names->normal) == 0)
1698 return 1;
1699 if (names->compressed != NULL
1700 && strcmp (section_name, names->compressed) == 0)
1701 return 1;
1702 return 0;
233a11ab
CS
1703}
1704
c906108c
SS
1705/* This function is mapped across the sections and remembers the
1706 offset and size of each of the debugging sections we are interested
1707 in. */
1708
1709static void
251d32d9 1710dwarf2_locate_sections (bfd *abfd, asection *sectp, void *vnames)
c906108c 1711{
251d32d9
TG
1712 const struct dwarf2_debug_sections *names;
1713
1714 if (vnames == NULL)
1715 names = &dwarf2_elf_names;
1716 else
1717 names = (const struct dwarf2_debug_sections *) vnames;
1718
1719 if (section_is_p (sectp->name, &names->info))
c906108c 1720 {
dce234bc
PP
1721 dwarf2_per_objfile->info.asection = sectp;
1722 dwarf2_per_objfile->info.size = bfd_get_section_size (sectp);
c906108c 1723 }
251d32d9 1724 else if (section_is_p (sectp->name, &names->abbrev))
c906108c 1725 {
dce234bc
PP
1726 dwarf2_per_objfile->abbrev.asection = sectp;
1727 dwarf2_per_objfile->abbrev.size = bfd_get_section_size (sectp);
c906108c 1728 }
251d32d9 1729 else if (section_is_p (sectp->name, &names->line))
c906108c 1730 {
dce234bc
PP
1731 dwarf2_per_objfile->line.asection = sectp;
1732 dwarf2_per_objfile->line.size = bfd_get_section_size (sectp);
c906108c 1733 }
251d32d9 1734 else if (section_is_p (sectp->name, &names->loc))
c906108c 1735 {
dce234bc
PP
1736 dwarf2_per_objfile->loc.asection = sectp;
1737 dwarf2_per_objfile->loc.size = bfd_get_section_size (sectp);
c906108c 1738 }
251d32d9 1739 else if (section_is_p (sectp->name, &names->macinfo))
c906108c 1740 {
dce234bc
PP
1741 dwarf2_per_objfile->macinfo.asection = sectp;
1742 dwarf2_per_objfile->macinfo.size = bfd_get_section_size (sectp);
c906108c 1743 }
cf2c3c16
TT
1744 else if (section_is_p (sectp->name, &names->macro))
1745 {
1746 dwarf2_per_objfile->macro.asection = sectp;
1747 dwarf2_per_objfile->macro.size = bfd_get_section_size (sectp);
1748 }
251d32d9 1749 else if (section_is_p (sectp->name, &names->str))
c906108c 1750 {
dce234bc
PP
1751 dwarf2_per_objfile->str.asection = sectp;
1752 dwarf2_per_objfile->str.size = bfd_get_section_size (sectp);
c906108c 1753 }
3019eac3
DE
1754 else if (section_is_p (sectp->name, &names->addr))
1755 {
1756 dwarf2_per_objfile->addr.asection = sectp;
1757 dwarf2_per_objfile->addr.size = bfd_get_section_size (sectp);
1758 }
251d32d9 1759 else if (section_is_p (sectp->name, &names->frame))
b6af0555 1760 {
dce234bc
PP
1761 dwarf2_per_objfile->frame.asection = sectp;
1762 dwarf2_per_objfile->frame.size = bfd_get_section_size (sectp);
b6af0555 1763 }
251d32d9 1764 else if (section_is_p (sectp->name, &names->eh_frame))
b6af0555 1765 {
81b9b86e 1766 flagword aflag = bfd_get_section_flags (abfd, sectp);
9a619af0 1767
3799ccc6
EZ
1768 if (aflag & SEC_HAS_CONTENTS)
1769 {
dce234bc
PP
1770 dwarf2_per_objfile->eh_frame.asection = sectp;
1771 dwarf2_per_objfile->eh_frame.size = bfd_get_section_size (sectp);
3799ccc6 1772 }
b6af0555 1773 }
251d32d9 1774 else if (section_is_p (sectp->name, &names->ranges))
af34e669 1775 {
dce234bc
PP
1776 dwarf2_per_objfile->ranges.asection = sectp;
1777 dwarf2_per_objfile->ranges.size = bfd_get_section_size (sectp);
af34e669 1778 }
251d32d9 1779 else if (section_is_p (sectp->name, &names->types))
348e048f 1780 {
8b70b953
TT
1781 struct dwarf2_section_info type_section;
1782
1783 memset (&type_section, 0, sizeof (type_section));
1784 type_section.asection = sectp;
1785 type_section.size = bfd_get_section_size (sectp);
1786
1787 VEC_safe_push (dwarf2_section_info_def, dwarf2_per_objfile->types,
1788 &type_section);
348e048f 1789 }
251d32d9 1790 else if (section_is_p (sectp->name, &names->gdb_index))
9291a0cd
TT
1791 {
1792 dwarf2_per_objfile->gdb_index.asection = sectp;
1793 dwarf2_per_objfile->gdb_index.size = bfd_get_section_size (sectp);
1794 }
dce234bc 1795
72dca2f5
FR
1796 if ((bfd_get_section_flags (abfd, sectp) & SEC_LOAD)
1797 && bfd_section_vma (abfd, sectp) == 0)
1798 dwarf2_per_objfile->has_section_at_zero = 1;
c906108c
SS
1799}
1800
fceca515
DE
1801/* A helper function that decides whether a section is empty,
1802 or not present. */
9e0ac564
TT
1803
1804static int
1805dwarf2_section_empty_p (struct dwarf2_section_info *info)
1806{
1807 return info->asection == NULL || info->size == 0;
1808}
1809
3019eac3
DE
1810/* Read the contents of the section INFO.
1811 OBJFILE is the main object file, but not necessarily the file where
1812 the section comes from. E.g., for DWO files INFO->asection->owner
1813 is the bfd of the DWO file.
dce234bc 1814 If the section is compressed, uncompress it before returning. */
c906108c 1815
dce234bc
PP
1816static void
1817dwarf2_read_section (struct objfile *objfile, struct dwarf2_section_info *info)
c906108c 1818{
dce234bc 1819 asection *sectp = info->asection;
3019eac3 1820 bfd *abfd;
dce234bc
PP
1821 gdb_byte *buf, *retbuf;
1822 unsigned char header[4];
c906108c 1823
be391dca
TT
1824 if (info->readin)
1825 return;
dce234bc 1826 info->buffer = NULL;
be391dca 1827 info->readin = 1;
188dd5d6 1828
9e0ac564 1829 if (dwarf2_section_empty_p (info))
dce234bc 1830 return;
c906108c 1831
3019eac3
DE
1832 abfd = sectp->owner;
1833
4bf44c1c
TT
1834 /* If the section has relocations, we must read it ourselves.
1835 Otherwise we attach it to the BFD. */
1836 if ((sectp->flags & SEC_RELOC) == 0)
dce234bc 1837 {
4bf44c1c 1838 const gdb_byte *bytes = gdb_bfd_map_section (sectp, &info->size);
dce234bc 1839
4bf44c1c
TT
1840 /* We have to cast away const here for historical reasons.
1841 Fixing dwarf2read to be const-correct would be quite nice. */
1842 info->buffer = (gdb_byte *) bytes;
1843 return;
dce234bc 1844 }
dce234bc 1845
4bf44c1c
TT
1846 buf = obstack_alloc (&objfile->objfile_obstack, info->size);
1847 info->buffer = buf;
dce234bc
PP
1848
1849 /* When debugging .o files, we may need to apply relocations; see
1850 http://sourceware.org/ml/gdb-patches/2002-04/msg00136.html .
1851 We never compress sections in .o files, so we only need to
1852 try this when the section is not compressed. */
ac8035ab 1853 retbuf = symfile_relocate_debug_section (objfile, sectp, buf);
dce234bc
PP
1854 if (retbuf != NULL)
1855 {
1856 info->buffer = retbuf;
1857 return;
1858 }
1859
1860 if (bfd_seek (abfd, sectp->filepos, SEEK_SET) != 0
1861 || bfd_bread (buf, info->size, abfd) != info->size)
1862 error (_("Dwarf Error: Can't read DWARF data from '%s'"),
1863 bfd_get_filename (abfd));
1864}
1865
9e0ac564
TT
1866/* A helper function that returns the size of a section in a safe way.
1867 If you are positive that the section has been read before using the
1868 size, then it is safe to refer to the dwarf2_section_info object's
1869 "size" field directly. In other cases, you must call this
1870 function, because for compressed sections the size field is not set
1871 correctly until the section has been read. */
1872
1873static bfd_size_type
1874dwarf2_section_size (struct objfile *objfile,
1875 struct dwarf2_section_info *info)
1876{
1877 if (!info->readin)
1878 dwarf2_read_section (objfile, info);
1879 return info->size;
1880}
1881
dce234bc 1882/* Fill in SECTP, BUFP and SIZEP with section info, given OBJFILE and
0963b4bd 1883 SECTION_NAME. */
af34e669 1884
dce234bc 1885void
3017a003
TG
1886dwarf2_get_section_info (struct objfile *objfile,
1887 enum dwarf2_section_enum sect,
dce234bc
PP
1888 asection **sectp, gdb_byte **bufp,
1889 bfd_size_type *sizep)
1890{
1891 struct dwarf2_per_objfile *data
1892 = objfile_data (objfile, dwarf2_objfile_data_key);
1893 struct dwarf2_section_info *info;
a3b2a86b
TT
1894
1895 /* We may see an objfile without any DWARF, in which case we just
1896 return nothing. */
1897 if (data == NULL)
1898 {
1899 *sectp = NULL;
1900 *bufp = NULL;
1901 *sizep = 0;
1902 return;
1903 }
3017a003
TG
1904 switch (sect)
1905 {
1906 case DWARF2_DEBUG_FRAME:
1907 info = &data->frame;
1908 break;
1909 case DWARF2_EH_FRAME:
1910 info = &data->eh_frame;
1911 break;
1912 default:
1913 gdb_assert_not_reached ("unexpected section");
1914 }
dce234bc 1915
9e0ac564 1916 dwarf2_read_section (objfile, info);
dce234bc
PP
1917
1918 *sectp = info->asection;
1919 *bufp = info->buffer;
1920 *sizep = info->size;
1921}
1922
36586728
TT
1923/* A helper function to find the sections for a .dwz file. */
1924
1925static void
1926locate_dwz_sections (bfd *abfd, asection *sectp, void *arg)
1927{
1928 struct dwz_file *dwz_file = arg;
1929
1930 /* Note that we only support the standard ELF names, because .dwz
1931 is ELF-only (at the time of writing). */
1932 if (section_is_p (sectp->name, &dwarf2_elf_names.abbrev))
1933 {
1934 dwz_file->abbrev.asection = sectp;
1935 dwz_file->abbrev.size = bfd_get_section_size (sectp);
1936 }
1937 else if (section_is_p (sectp->name, &dwarf2_elf_names.info))
1938 {
1939 dwz_file->info.asection = sectp;
1940 dwz_file->info.size = bfd_get_section_size (sectp);
1941 }
1942 else if (section_is_p (sectp->name, &dwarf2_elf_names.str))
1943 {
1944 dwz_file->str.asection = sectp;
1945 dwz_file->str.size = bfd_get_section_size (sectp);
1946 }
1947 else if (section_is_p (sectp->name, &dwarf2_elf_names.line))
1948 {
1949 dwz_file->line.asection = sectp;
1950 dwz_file->line.size = bfd_get_section_size (sectp);
1951 }
1952 else if (section_is_p (sectp->name, &dwarf2_elf_names.macro))
1953 {
1954 dwz_file->macro.asection = sectp;
1955 dwz_file->macro.size = bfd_get_section_size (sectp);
1956 }
2ec9a5e0
TT
1957 else if (section_is_p (sectp->name, &dwarf2_elf_names.gdb_index))
1958 {
1959 dwz_file->gdb_index.asection = sectp;
1960 dwz_file->gdb_index.size = bfd_get_section_size (sectp);
1961 }
36586728
TT
1962}
1963
1964/* Open the separate '.dwz' debug file, if needed. Error if the file
1965 cannot be found. */
1966
1967static struct dwz_file *
1968dwarf2_get_dwz_file (void)
1969{
1970 bfd *abfd, *dwz_bfd;
1971 asection *section;
1972 gdb_byte *data;
1973 struct cleanup *cleanup;
1974 const char *filename;
1975 struct dwz_file *result;
1976
1977 if (dwarf2_per_objfile->dwz_file != NULL)
1978 return dwarf2_per_objfile->dwz_file;
1979
1980 abfd = dwarf2_per_objfile->objfile->obfd;
1981 section = bfd_get_section_by_name (abfd, ".gnu_debugaltlink");
1982 if (section == NULL)
1983 error (_("could not find '.gnu_debugaltlink' section"));
1984 if (!bfd_malloc_and_get_section (abfd, section, &data))
1985 error (_("could not read '.gnu_debugaltlink' section: %s"),
1986 bfd_errmsg (bfd_get_error ()));
1987 cleanup = make_cleanup (xfree, data);
1988
1989 filename = data;
1990 if (!IS_ABSOLUTE_PATH (filename))
1991 {
1992 char *abs = gdb_realpath (dwarf2_per_objfile->objfile->name);
1993 char *rel;
1994
1995 make_cleanup (xfree, abs);
1996 abs = ldirname (abs);
1997 make_cleanup (xfree, abs);
1998
1999 rel = concat (abs, SLASH_STRING, filename, (char *) NULL);
2000 make_cleanup (xfree, rel);
2001 filename = rel;
2002 }
2003
2004 /* The format is just a NUL-terminated file name, followed by the
2005 build-id. For now, though, we ignore the build-id. */
2006 dwz_bfd = gdb_bfd_open (filename, gnutarget, -1);
2007 if (dwz_bfd == NULL)
2008 error (_("could not read '%s': %s"), filename,
2009 bfd_errmsg (bfd_get_error ()));
2010
2011 if (!bfd_check_format (dwz_bfd, bfd_object))
2012 {
2013 gdb_bfd_unref (dwz_bfd);
2014 error (_("file '%s' was not usable: %s"), filename,
2015 bfd_errmsg (bfd_get_error ()));
2016 }
2017
2018 result = OBSTACK_ZALLOC (&dwarf2_per_objfile->objfile->objfile_obstack,
2019 struct dwz_file);
2020 result->dwz_bfd = dwz_bfd;
2021
2022 bfd_map_over_sections (dwz_bfd, locate_dwz_sections, result);
2023
2024 do_cleanups (cleanup);
2025
2026 return result;
2027}
9291a0cd 2028\f
7b9f3c50
DE
2029/* DWARF quick_symbols_functions support. */
2030
2031/* TUs can share .debug_line entries, and there can be a lot more TUs than
2032 unique line tables, so we maintain a separate table of all .debug_line
2033 derived entries to support the sharing.
2034 All the quick functions need is the list of file names. We discard the
2035 line_header when we're done and don't need to record it here. */
2036struct quick_file_names
2037{
094b34ac
DE
2038 /* The data used to construct the hash key. */
2039 struct stmt_list_hash hash;
7b9f3c50
DE
2040
2041 /* The number of entries in file_names, real_names. */
2042 unsigned int num_file_names;
2043
2044 /* The file names from the line table, after being run through
2045 file_full_name. */
2046 const char **file_names;
2047
2048 /* The file names from the line table after being run through
2049 gdb_realpath. These are computed lazily. */
2050 const char **real_names;
2051};
2052
2053/* When using the index (and thus not using psymtabs), each CU has an
2054 object of this type. This is used to hold information needed by
2055 the various "quick" methods. */
2056struct dwarf2_per_cu_quick_data
2057{
2058 /* The file table. This can be NULL if there was no file table
2059 or it's currently not read in.
2060 NOTE: This points into dwarf2_per_objfile->quick_file_names_table. */
2061 struct quick_file_names *file_names;
2062
2063 /* The corresponding symbol table. This is NULL if symbols for this
2064 CU have not yet been read. */
2065 struct symtab *symtab;
2066
2067 /* A temporary mark bit used when iterating over all CUs in
2068 expand_symtabs_matching. */
2069 unsigned int mark : 1;
2070
2071 /* True if we've tried to read the file table and found there isn't one.
2072 There will be no point in trying to read it again next time. */
2073 unsigned int no_file_data : 1;
2074};
2075
094b34ac
DE
2076/* Utility hash function for a stmt_list_hash. */
2077
2078static hashval_t
2079hash_stmt_list_entry (const struct stmt_list_hash *stmt_list_hash)
2080{
2081 hashval_t v = 0;
2082
2083 if (stmt_list_hash->dwo_unit != NULL)
2084 v += (uintptr_t) stmt_list_hash->dwo_unit->dwo_file;
2085 v += stmt_list_hash->line_offset.sect_off;
2086 return v;
2087}
2088
2089/* Utility equality function for a stmt_list_hash. */
2090
2091static int
2092eq_stmt_list_entry (const struct stmt_list_hash *lhs,
2093 const struct stmt_list_hash *rhs)
2094{
2095 if ((lhs->dwo_unit != NULL) != (rhs->dwo_unit != NULL))
2096 return 0;
2097 if (lhs->dwo_unit != NULL
2098 && lhs->dwo_unit->dwo_file != rhs->dwo_unit->dwo_file)
2099 return 0;
2100
2101 return lhs->line_offset.sect_off == rhs->line_offset.sect_off;
2102}
2103
7b9f3c50
DE
2104/* Hash function for a quick_file_names. */
2105
2106static hashval_t
2107hash_file_name_entry (const void *e)
2108{
2109 const struct quick_file_names *file_data = e;
2110
094b34ac 2111 return hash_stmt_list_entry (&file_data->hash);
7b9f3c50
DE
2112}
2113
2114/* Equality function for a quick_file_names. */
2115
2116static int
2117eq_file_name_entry (const void *a, const void *b)
2118{
2119 const struct quick_file_names *ea = a;
2120 const struct quick_file_names *eb = b;
2121
094b34ac 2122 return eq_stmt_list_entry (&ea->hash, &eb->hash);
7b9f3c50
DE
2123}
2124
2125/* Delete function for a quick_file_names. */
2126
2127static void
2128delete_file_name_entry (void *e)
2129{
2130 struct quick_file_names *file_data = e;
2131 int i;
2132
2133 for (i = 0; i < file_data->num_file_names; ++i)
2134 {
2135 xfree ((void*) file_data->file_names[i]);
2136 if (file_data->real_names)
2137 xfree ((void*) file_data->real_names[i]);
2138 }
2139
2140 /* The space for the struct itself lives on objfile_obstack,
2141 so we don't free it here. */
2142}
2143
2144/* Create a quick_file_names hash table. */
2145
2146static htab_t
2147create_quick_file_names_table (unsigned int nr_initial_entries)
2148{
2149 return htab_create_alloc (nr_initial_entries,
2150 hash_file_name_entry, eq_file_name_entry,
2151 delete_file_name_entry, xcalloc, xfree);
2152}
9291a0cd 2153
918dd910
JK
2154/* Read in PER_CU->CU. This function is unrelated to symtabs, symtab would
2155 have to be created afterwards. You should call age_cached_comp_units after
2156 processing PER_CU->CU. dw2_setup must have been already called. */
2157
2158static void
2159load_cu (struct dwarf2_per_cu_data *per_cu)
2160{
3019eac3 2161 if (per_cu->is_debug_types)
e5fe5e75 2162 load_full_type_unit (per_cu);
918dd910 2163 else
95554aad 2164 load_full_comp_unit (per_cu, language_minimal);
918dd910 2165
918dd910 2166 gdb_assert (per_cu->cu != NULL);
2dc860c0
DE
2167
2168 dwarf2_find_base_address (per_cu->cu->dies, per_cu->cu);
918dd910
JK
2169}
2170
a0f42c21 2171/* Read in the symbols for PER_CU. */
2fdf6df6 2172
9291a0cd 2173static void
a0f42c21 2174dw2_do_instantiate_symtab (struct dwarf2_per_cu_data *per_cu)
9291a0cd
TT
2175{
2176 struct cleanup *back_to;
2177
f4dc4d17
DE
2178 /* Skip type_unit_groups, reading the type units they contain
2179 is handled elsewhere. */
2180 if (IS_TYPE_UNIT_GROUP (per_cu))
2181 return;
2182
9291a0cd
TT
2183 back_to = make_cleanup (dwarf2_release_queue, NULL);
2184
95554aad
TT
2185 if (dwarf2_per_objfile->using_index
2186 ? per_cu->v.quick->symtab == NULL
2187 : (per_cu->v.psymtab == NULL || !per_cu->v.psymtab->readin))
2188 {
2189 queue_comp_unit (per_cu, language_minimal);
2190 load_cu (per_cu);
2191 }
9291a0cd 2192
a0f42c21 2193 process_queue ();
9291a0cd
TT
2194
2195 /* Age the cache, releasing compilation units that have not
2196 been used recently. */
2197 age_cached_comp_units ();
2198
2199 do_cleanups (back_to);
2200}
2201
2202/* Ensure that the symbols for PER_CU have been read in. OBJFILE is
2203 the objfile from which this CU came. Returns the resulting symbol
2204 table. */
2fdf6df6 2205
9291a0cd 2206static struct symtab *
a0f42c21 2207dw2_instantiate_symtab (struct dwarf2_per_cu_data *per_cu)
9291a0cd 2208{
95554aad 2209 gdb_assert (dwarf2_per_objfile->using_index);
9291a0cd
TT
2210 if (!per_cu->v.quick->symtab)
2211 {
2212 struct cleanup *back_to = make_cleanup (free_cached_comp_units, NULL);
2213 increment_reading_symtab ();
a0f42c21 2214 dw2_do_instantiate_symtab (per_cu);
95554aad 2215 process_cu_includes ();
9291a0cd
TT
2216 do_cleanups (back_to);
2217 }
2218 return per_cu->v.quick->symtab;
2219}
2220
f4dc4d17
DE
2221/* Return the CU given its index.
2222
2223 This is intended for loops like:
2224
2225 for (i = 0; i < (dwarf2_per_objfile->n_comp_units
2226 + dwarf2_per_objfile->n_type_units); ++i)
2227 {
2228 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
2229
2230 ...;
2231 }
2232*/
2fdf6df6 2233
1fd400ff
TT
2234static struct dwarf2_per_cu_data *
2235dw2_get_cu (int index)
2236{
2237 if (index >= dwarf2_per_objfile->n_comp_units)
2238 {
f4dc4d17 2239 index -= dwarf2_per_objfile->n_comp_units;
094b34ac
DE
2240 gdb_assert (index < dwarf2_per_objfile->n_type_units);
2241 return &dwarf2_per_objfile->all_type_units[index]->per_cu;
f4dc4d17
DE
2242 }
2243
2244 return dwarf2_per_objfile->all_comp_units[index];
2245}
2246
2247/* Return the primary CU given its index.
2248 The difference between this function and dw2_get_cu is in the handling
2249 of type units (TUs). Here we return the type_unit_group object.
2250
2251 This is intended for loops like:
2252
2253 for (i = 0; i < (dwarf2_per_objfile->n_comp_units
2254 + dwarf2_per_objfile->n_type_unit_groups); ++i)
2255 {
2256 struct dwarf2_per_cu_data *per_cu = dw2_get_primary_cu (i);
2257
2258 ...;
2259 }
2260*/
2261
2262static struct dwarf2_per_cu_data *
2263dw2_get_primary_cu (int index)
2264{
2265 if (index >= dwarf2_per_objfile->n_comp_units)
2266 {
1fd400ff 2267 index -= dwarf2_per_objfile->n_comp_units;
094b34ac
DE
2268 gdb_assert (index < dwarf2_per_objfile->n_type_unit_groups);
2269 return &dwarf2_per_objfile->all_type_unit_groups[index]->per_cu;
1fd400ff 2270 }
f4dc4d17 2271
1fd400ff
TT
2272 return dwarf2_per_objfile->all_comp_units[index];
2273}
2274
9291a0cd
TT
2275/* A helper function that knows how to read a 64-bit value in a way
2276 that doesn't make gdb die. Returns 1 if the conversion went ok, 0
2277 otherwise. */
2fdf6df6 2278
9291a0cd
TT
2279static int
2280extract_cu_value (const char *bytes, ULONGEST *result)
2281{
2282 if (sizeof (ULONGEST) < 8)
2283 {
2284 int i;
2285
2286 /* Ignore the upper 4 bytes if they are all zero. */
2287 for (i = 0; i < 4; ++i)
2288 if (bytes[i + 4] != 0)
2289 return 0;
2290
2291 *result = extract_unsigned_integer (bytes, 4, BFD_ENDIAN_LITTLE);
2292 }
2293 else
2294 *result = extract_unsigned_integer (bytes, 8, BFD_ENDIAN_LITTLE);
2295 return 1;
2296}
2297
2ec9a5e0
TT
2298/* A helper for create_cus_from_index that handles a given list of
2299 CUs. */
2fdf6df6 2300
9291a0cd 2301static int
2ec9a5e0
TT
2302create_cus_from_index_list (struct objfile *objfile,
2303 const gdb_byte *cu_list, offset_type n_elements,
2304 struct dwarf2_section_info *section,
2305 int is_dwz,
2306 int base_offset)
9291a0cd
TT
2307{
2308 offset_type i;
9291a0cd 2309
2ec9a5e0 2310 for (i = 0; i < n_elements; i += 2)
9291a0cd
TT
2311 {
2312 struct dwarf2_per_cu_data *the_cu;
2313 ULONGEST offset, length;
2314
2315 if (!extract_cu_value (cu_list, &offset)
2316 || !extract_cu_value (cu_list + 8, &length))
2317 return 0;
2318 cu_list += 2 * 8;
2319
2320 the_cu = OBSTACK_ZALLOC (&objfile->objfile_obstack,
2321 struct dwarf2_per_cu_data);
b64f50a1 2322 the_cu->offset.sect_off = offset;
9291a0cd
TT
2323 the_cu->length = length;
2324 the_cu->objfile = objfile;
2ec9a5e0 2325 the_cu->info_or_types_section = section;
9291a0cd
TT
2326 the_cu->v.quick = OBSTACK_ZALLOC (&objfile->objfile_obstack,
2327 struct dwarf2_per_cu_quick_data);
2ec9a5e0
TT
2328 the_cu->is_dwz = is_dwz;
2329 dwarf2_per_objfile->all_comp_units[base_offset + i / 2] = the_cu;
9291a0cd
TT
2330 }
2331
2332 return 1;
2333}
2334
2ec9a5e0
TT
2335/* Read the CU list from the mapped index, and use it to create all
2336 the CU objects for this objfile. Return 0 if something went wrong,
2337 1 if everything went ok. */
2338
2339static int
2340create_cus_from_index (struct objfile *objfile,
2341 const gdb_byte *cu_list, offset_type cu_list_elements,
2342 const gdb_byte *dwz_list, offset_type dwz_elements)
2343{
2344 struct dwz_file *dwz;
2345
2346 dwarf2_per_objfile->n_comp_units = (cu_list_elements + dwz_elements) / 2;
2347 dwarf2_per_objfile->all_comp_units
2348 = obstack_alloc (&objfile->objfile_obstack,
2349 dwarf2_per_objfile->n_comp_units
2350 * sizeof (struct dwarf2_per_cu_data *));
2351
2352 if (!create_cus_from_index_list (objfile, cu_list, cu_list_elements,
2353 &dwarf2_per_objfile->info, 0, 0))
2354 return 0;
2355
2356 if (dwz_elements == 0)
2357 return 1;
2358
2359 dwz = dwarf2_get_dwz_file ();
2360 return create_cus_from_index_list (objfile, dwz_list, dwz_elements,
2361 &dwz->info, 1, cu_list_elements / 2);
2362}
2363
1fd400ff 2364/* Create the signatured type hash table from the index. */
673bfd45 2365
1fd400ff 2366static int
673bfd45 2367create_signatured_type_table_from_index (struct objfile *objfile,
8b70b953 2368 struct dwarf2_section_info *section,
673bfd45
DE
2369 const gdb_byte *bytes,
2370 offset_type elements)
1fd400ff
TT
2371{
2372 offset_type i;
673bfd45 2373 htab_t sig_types_hash;
1fd400ff 2374
d467dd73
DE
2375 dwarf2_per_objfile->n_type_units = elements / 3;
2376 dwarf2_per_objfile->all_type_units
1fd400ff 2377 = obstack_alloc (&objfile->objfile_obstack,
d467dd73 2378 dwarf2_per_objfile->n_type_units
b4dd5633 2379 * sizeof (struct signatured_type *));
1fd400ff 2380
673bfd45 2381 sig_types_hash = allocate_signatured_type_table (objfile);
1fd400ff
TT
2382
2383 for (i = 0; i < elements; i += 3)
2384 {
52dc124a
DE
2385 struct signatured_type *sig_type;
2386 ULONGEST offset, type_offset_in_tu, signature;
1fd400ff
TT
2387 void **slot;
2388
2389 if (!extract_cu_value (bytes, &offset)
52dc124a 2390 || !extract_cu_value (bytes + 8, &type_offset_in_tu))
1fd400ff
TT
2391 return 0;
2392 signature = extract_unsigned_integer (bytes + 16, 8, BFD_ENDIAN_LITTLE);
2393 bytes += 3 * 8;
2394
52dc124a 2395 sig_type = OBSTACK_ZALLOC (&objfile->objfile_obstack,
1fd400ff 2396 struct signatured_type);
52dc124a 2397 sig_type->signature = signature;
3019eac3
DE
2398 sig_type->type_offset_in_tu.cu_off = type_offset_in_tu;
2399 sig_type->per_cu.is_debug_types = 1;
2400 sig_type->per_cu.info_or_types_section = section;
52dc124a
DE
2401 sig_type->per_cu.offset.sect_off = offset;
2402 sig_type->per_cu.objfile = objfile;
2403 sig_type->per_cu.v.quick
1fd400ff
TT
2404 = OBSTACK_ZALLOC (&objfile->objfile_obstack,
2405 struct dwarf2_per_cu_quick_data);
2406
52dc124a
DE
2407 slot = htab_find_slot (sig_types_hash, sig_type, INSERT);
2408 *slot = sig_type;
1fd400ff 2409
b4dd5633 2410 dwarf2_per_objfile->all_type_units[i / 3] = sig_type;
1fd400ff
TT
2411 }
2412
673bfd45 2413 dwarf2_per_objfile->signatured_types = sig_types_hash;
1fd400ff
TT
2414
2415 return 1;
2416}
2417
9291a0cd
TT
2418/* Read the address map data from the mapped index, and use it to
2419 populate the objfile's psymtabs_addrmap. */
2fdf6df6 2420
9291a0cd
TT
2421static void
2422create_addrmap_from_index (struct objfile *objfile, struct mapped_index *index)
2423{
2424 const gdb_byte *iter, *end;
2425 struct obstack temp_obstack;
2426 struct addrmap *mutable_map;
2427 struct cleanup *cleanup;
2428 CORE_ADDR baseaddr;
2429
2430 obstack_init (&temp_obstack);
2431 cleanup = make_cleanup_obstack_free (&temp_obstack);
2432 mutable_map = addrmap_create_mutable (&temp_obstack);
2433
2434 iter = index->address_table;
2435 end = iter + index->address_table_size;
2436
2437 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
2438
2439 while (iter < end)
2440 {
2441 ULONGEST hi, lo, cu_index;
2442 lo = extract_unsigned_integer (iter, 8, BFD_ENDIAN_LITTLE);
2443 iter += 8;
2444 hi = extract_unsigned_integer (iter, 8, BFD_ENDIAN_LITTLE);
2445 iter += 8;
2446 cu_index = extract_unsigned_integer (iter, 4, BFD_ENDIAN_LITTLE);
2447 iter += 4;
2448
2449 addrmap_set_empty (mutable_map, lo + baseaddr, hi + baseaddr - 1,
1fd400ff 2450 dw2_get_cu (cu_index));
9291a0cd
TT
2451 }
2452
2453 objfile->psymtabs_addrmap = addrmap_create_fixed (mutable_map,
2454 &objfile->objfile_obstack);
2455 do_cleanups (cleanup);
2456}
2457
59d7bcaf
JK
2458/* The hash function for strings in the mapped index. This is the same as
2459 SYMBOL_HASH_NEXT, but we keep a separate copy to maintain control over the
2460 implementation. This is necessary because the hash function is tied to the
2461 format of the mapped index file. The hash values do not have to match with
559a7a62
JK
2462 SYMBOL_HASH_NEXT.
2463
2464 Use INT_MAX for INDEX_VERSION if you generate the current index format. */
2fdf6df6 2465
9291a0cd 2466static hashval_t
559a7a62 2467mapped_index_string_hash (int index_version, const void *p)
9291a0cd
TT
2468{
2469 const unsigned char *str = (const unsigned char *) p;
2470 hashval_t r = 0;
2471 unsigned char c;
2472
2473 while ((c = *str++) != 0)
559a7a62
JK
2474 {
2475 if (index_version >= 5)
2476 c = tolower (c);
2477 r = r * 67 + c - 113;
2478 }
9291a0cd
TT
2479
2480 return r;
2481}
2482
2483/* Find a slot in the mapped index INDEX for the object named NAME.
2484 If NAME is found, set *VEC_OUT to point to the CU vector in the
2485 constant pool and return 1. If NAME cannot be found, return 0. */
2fdf6df6 2486
9291a0cd
TT
2487static int
2488find_slot_in_mapped_hash (struct mapped_index *index, const char *name,
2489 offset_type **vec_out)
2490{
0cf03b49
JK
2491 struct cleanup *back_to = make_cleanup (null_cleanup, 0);
2492 offset_type hash;
9291a0cd 2493 offset_type slot, step;
559a7a62 2494 int (*cmp) (const char *, const char *);
9291a0cd 2495
0cf03b49
JK
2496 if (current_language->la_language == language_cplus
2497 || current_language->la_language == language_java
2498 || current_language->la_language == language_fortran)
2499 {
2500 /* NAME is already canonical. Drop any qualifiers as .gdb_index does
2501 not contain any. */
2502 const char *paren = strchr (name, '(');
2503
2504 if (paren)
2505 {
2506 char *dup;
2507
2508 dup = xmalloc (paren - name + 1);
2509 memcpy (dup, name, paren - name);
2510 dup[paren - name] = 0;
2511
2512 make_cleanup (xfree, dup);
2513 name = dup;
2514 }
2515 }
2516
559a7a62 2517 /* Index version 4 did not support case insensitive searches. But the
feea76c2 2518 indices for case insensitive languages are built in lowercase, therefore
559a7a62
JK
2519 simulate our NAME being searched is also lowercased. */
2520 hash = mapped_index_string_hash ((index->version == 4
2521 && case_sensitivity == case_sensitive_off
2522 ? 5 : index->version),
2523 name);
2524
3876f04e
DE
2525 slot = hash & (index->symbol_table_slots - 1);
2526 step = ((hash * 17) & (index->symbol_table_slots - 1)) | 1;
559a7a62 2527 cmp = (case_sensitivity == case_sensitive_on ? strcmp : strcasecmp);
9291a0cd
TT
2528
2529 for (;;)
2530 {
2531 /* Convert a slot number to an offset into the table. */
2532 offset_type i = 2 * slot;
2533 const char *str;
3876f04e 2534 if (index->symbol_table[i] == 0 && index->symbol_table[i + 1] == 0)
0cf03b49
JK
2535 {
2536 do_cleanups (back_to);
2537 return 0;
2538 }
9291a0cd 2539
3876f04e 2540 str = index->constant_pool + MAYBE_SWAP (index->symbol_table[i]);
559a7a62 2541 if (!cmp (name, str))
9291a0cd
TT
2542 {
2543 *vec_out = (offset_type *) (index->constant_pool
3876f04e 2544 + MAYBE_SWAP (index->symbol_table[i + 1]));
0cf03b49 2545 do_cleanups (back_to);
9291a0cd
TT
2546 return 1;
2547 }
2548
3876f04e 2549 slot = (slot + step) & (index->symbol_table_slots - 1);
9291a0cd
TT
2550 }
2551}
2552
2ec9a5e0
TT
2553/* A helper function that reads the .gdb_index from SECTION and fills
2554 in MAP. FILENAME is the name of the file containing the section;
2555 it is used for error reporting. DEPRECATED_OK is nonzero if it is
2556 ok to use deprecated sections.
2557
2558 CU_LIST, CU_LIST_ELEMENTS, TYPES_LIST, and TYPES_LIST_ELEMENTS are
2559 out parameters that are filled in with information about the CU and
2560 TU lists in the section.
2561
2562 Returns 1 if all went well, 0 otherwise. */
2fdf6df6 2563
9291a0cd 2564static int
2ec9a5e0
TT
2565read_index_from_section (struct objfile *objfile,
2566 const char *filename,
2567 int deprecated_ok,
2568 struct dwarf2_section_info *section,
2569 struct mapped_index *map,
2570 const gdb_byte **cu_list,
2571 offset_type *cu_list_elements,
2572 const gdb_byte **types_list,
2573 offset_type *types_list_elements)
9291a0cd 2574{
9291a0cd 2575 char *addr;
2ec9a5e0 2576 offset_type version;
b3b272e1 2577 offset_type *metadata;
1fd400ff 2578 int i;
9291a0cd 2579
2ec9a5e0 2580 if (dwarf2_section_empty_p (section))
9291a0cd 2581 return 0;
82430852
JK
2582
2583 /* Older elfutils strip versions could keep the section in the main
2584 executable while splitting it for the separate debug info file. */
2ec9a5e0 2585 if ((bfd_get_file_flags (section->asection) & SEC_HAS_CONTENTS) == 0)
82430852
JK
2586 return 0;
2587
2ec9a5e0 2588 dwarf2_read_section (objfile, section);
9291a0cd 2589
2ec9a5e0 2590 addr = section->buffer;
9291a0cd 2591 /* Version check. */
1fd400ff 2592 version = MAYBE_SWAP (*(offset_type *) addr);
987d643c 2593 /* Versions earlier than 3 emitted every copy of a psymbol. This
a6e293d1 2594 causes the index to behave very poorly for certain requests. Version 3
831adc1f 2595 contained incomplete addrmap. So, it seems better to just ignore such
481860b3 2596 indices. */
831adc1f 2597 if (version < 4)
481860b3
GB
2598 {
2599 static int warning_printed = 0;
2600 if (!warning_printed)
2601 {
2602 warning (_("Skipping obsolete .gdb_index section in %s."),
2ec9a5e0 2603 filename);
481860b3
GB
2604 warning_printed = 1;
2605 }
2606 return 0;
2607 }
2608 /* Index version 4 uses a different hash function than index version
2609 5 and later.
2610
2611 Versions earlier than 6 did not emit psymbols for inlined
2612 functions. Using these files will cause GDB not to be able to
2613 set breakpoints on inlined functions by name, so we ignore these
e615022a
DE
2614 indices unless the user has done
2615 "set use-deprecated-index-sections on". */
2ec9a5e0 2616 if (version < 6 && !deprecated_ok)
481860b3
GB
2617 {
2618 static int warning_printed = 0;
2619 if (!warning_printed)
2620 {
e615022a
DE
2621 warning (_("\
2622Skipping deprecated .gdb_index section in %s.\n\
2623Do \"set use-deprecated-index-sections on\" before the file is read\n\
2624to use the section anyway."),
2ec9a5e0 2625 filename);
481860b3
GB
2626 warning_printed = 1;
2627 }
2628 return 0;
2629 }
2630 /* Indexes with higher version than the one supported by GDB may be no
594e8718 2631 longer backward compatible. */
156942c7 2632 if (version > 7)
594e8718 2633 return 0;
9291a0cd 2634
559a7a62 2635 map->version = version;
2ec9a5e0 2636 map->total_size = section->size;
9291a0cd
TT
2637
2638 metadata = (offset_type *) (addr + sizeof (offset_type));
1fd400ff
TT
2639
2640 i = 0;
2ec9a5e0
TT
2641 *cu_list = addr + MAYBE_SWAP (metadata[i]);
2642 *cu_list_elements = ((MAYBE_SWAP (metadata[i + 1]) - MAYBE_SWAP (metadata[i]))
2643 / 8);
1fd400ff
TT
2644 ++i;
2645
2ec9a5e0
TT
2646 *types_list = addr + MAYBE_SWAP (metadata[i]);
2647 *types_list_elements = ((MAYBE_SWAP (metadata[i + 1])
2648 - MAYBE_SWAP (metadata[i]))
2649 / 8);
987d643c 2650 ++i;
1fd400ff
TT
2651
2652 map->address_table = addr + MAYBE_SWAP (metadata[i]);
2653 map->address_table_size = (MAYBE_SWAP (metadata[i + 1])
2654 - MAYBE_SWAP (metadata[i]));
2655 ++i;
2656
3876f04e
DE
2657 map->symbol_table = (offset_type *) (addr + MAYBE_SWAP (metadata[i]));
2658 map->symbol_table_slots = ((MAYBE_SWAP (metadata[i + 1])
2659 - MAYBE_SWAP (metadata[i]))
2660 / (2 * sizeof (offset_type)));
1fd400ff 2661 ++i;
9291a0cd 2662
1fd400ff
TT
2663 map->constant_pool = addr + MAYBE_SWAP (metadata[i]);
2664
2ec9a5e0
TT
2665 return 1;
2666}
2667
2668
2669/* Read the index file. If everything went ok, initialize the "quick"
2670 elements of all the CUs and return 1. Otherwise, return 0. */
2671
2672static int
2673dwarf2_read_index (struct objfile *objfile)
2674{
2675 struct mapped_index local_map, *map;
2676 const gdb_byte *cu_list, *types_list, *dwz_list = NULL;
2677 offset_type cu_list_elements, types_list_elements, dwz_list_elements = 0;
2678
2679 if (!read_index_from_section (objfile, objfile->name,
2680 use_deprecated_index_sections,
2681 &dwarf2_per_objfile->gdb_index, &local_map,
2682 &cu_list, &cu_list_elements,
2683 &types_list, &types_list_elements))
2684 return 0;
2685
0fefef59 2686 /* Don't use the index if it's empty. */
2ec9a5e0 2687 if (local_map.symbol_table_slots == 0)
0fefef59
DE
2688 return 0;
2689
2ec9a5e0
TT
2690 /* If there is a .dwz file, read it so we can get its CU list as
2691 well. */
2692 if (bfd_get_section_by_name (objfile->obfd, ".gnu_debugaltlink") != NULL)
2693 {
2694 struct dwz_file *dwz = dwarf2_get_dwz_file ();
2695 struct mapped_index dwz_map;
2696 const gdb_byte *dwz_types_ignore;
2697 offset_type dwz_types_elements_ignore;
2698
2699 if (!read_index_from_section (objfile, bfd_get_filename (dwz->dwz_bfd),
2700 1,
2701 &dwz->gdb_index, &dwz_map,
2702 &dwz_list, &dwz_list_elements,
2703 &dwz_types_ignore,
2704 &dwz_types_elements_ignore))
2705 {
2706 warning (_("could not read '.gdb_index' section from %s; skipping"),
2707 bfd_get_filename (dwz->dwz_bfd));
2708 return 0;
2709 }
2710 }
2711
2712 if (!create_cus_from_index (objfile, cu_list, cu_list_elements,
2713 dwz_list, dwz_list_elements))
1fd400ff
TT
2714 return 0;
2715
8b70b953
TT
2716 if (types_list_elements)
2717 {
2718 struct dwarf2_section_info *section;
2719
2720 /* We can only handle a single .debug_types when we have an
2721 index. */
2722 if (VEC_length (dwarf2_section_info_def, dwarf2_per_objfile->types) != 1)
2723 return 0;
2724
2725 section = VEC_index (dwarf2_section_info_def,
2726 dwarf2_per_objfile->types, 0);
2727
2728 if (!create_signatured_type_table_from_index (objfile, section,
2729 types_list,
2730 types_list_elements))
2731 return 0;
2732 }
9291a0cd 2733
2ec9a5e0
TT
2734 create_addrmap_from_index (objfile, &local_map);
2735
2736 map = obstack_alloc (&objfile->objfile_obstack, sizeof (struct mapped_index));
2737 *map = local_map;
9291a0cd
TT
2738
2739 dwarf2_per_objfile->index_table = map;
2740 dwarf2_per_objfile->using_index = 1;
7b9f3c50
DE
2741 dwarf2_per_objfile->quick_file_names_table =
2742 create_quick_file_names_table (dwarf2_per_objfile->n_comp_units);
9291a0cd
TT
2743
2744 return 1;
2745}
2746
2747/* A helper for the "quick" functions which sets the global
2748 dwarf2_per_objfile according to OBJFILE. */
2fdf6df6 2749
9291a0cd
TT
2750static void
2751dw2_setup (struct objfile *objfile)
2752{
2753 dwarf2_per_objfile = objfile_data (objfile, dwarf2_objfile_data_key);
2754 gdb_assert (dwarf2_per_objfile);
2755}
2756
f4dc4d17
DE
2757/* Reader function for dw2_build_type_unit_groups. */
2758
2759static void
2760dw2_build_type_unit_groups_reader (const struct die_reader_specs *reader,
2761 gdb_byte *info_ptr,
2762 struct die_info *type_unit_die,
2763 int has_children,
2764 void *data)
2765{
2766 struct dwarf2_cu *cu = reader->cu;
f4dc4d17
DE
2767 struct attribute *attr;
2768 struct type_unit_group *tu_group;
2769
2770 gdb_assert (data == NULL);
2771
2772 if (! has_children)
2773 return;
2774
2775 attr = dwarf2_attr_no_follow (type_unit_die, DW_AT_stmt_list);
2776 /* Call this for its side-effect of creating the associated
2777 struct type_unit_group if it doesn't already exist. */
094b34ac 2778 tu_group = get_type_unit_group (cu, attr);
f4dc4d17
DE
2779}
2780
2781/* Build dwarf2_per_objfile->type_unit_groups.
2782 This function may be called multiple times. */
2783
2784static void
2785dw2_build_type_unit_groups (void)
2786{
2787 if (dwarf2_per_objfile->type_unit_groups == NULL)
2788 build_type_unit_groups (dw2_build_type_unit_groups_reader, NULL);
2789}
2790
dee91e82 2791/* die_reader_func for dw2_get_file_names. */
2fdf6df6 2792
dee91e82
DE
2793static void
2794dw2_get_file_names_reader (const struct die_reader_specs *reader,
2795 gdb_byte *info_ptr,
2796 struct die_info *comp_unit_die,
2797 int has_children,
2798 void *data)
9291a0cd 2799{
dee91e82
DE
2800 struct dwarf2_cu *cu = reader->cu;
2801 struct dwarf2_per_cu_data *this_cu = cu->per_cu;
2802 struct objfile *objfile = dwarf2_per_objfile->objfile;
094b34ac 2803 struct dwarf2_per_cu_data *lh_cu;
7b9f3c50 2804 struct line_header *lh;
9291a0cd 2805 struct attribute *attr;
dee91e82 2806 int i;
9291a0cd 2807 char *name, *comp_dir;
7b9f3c50
DE
2808 void **slot;
2809 struct quick_file_names *qfn;
2810 unsigned int line_offset;
9291a0cd 2811
07261596
TT
2812 /* Our callers never want to match partial units -- instead they
2813 will match the enclosing full CU. */
2814 if (comp_unit_die->tag == DW_TAG_partial_unit)
2815 {
2816 this_cu->v.quick->no_file_data = 1;
2817 return;
2818 }
2819
094b34ac
DE
2820 /* If we're reading the line header for TUs, store it in the "per_cu"
2821 for tu_group. */
2822 if (this_cu->is_debug_types)
2823 {
2824 struct type_unit_group *tu_group = data;
2825
2826 gdb_assert (tu_group != NULL);
2827 lh_cu = &tu_group->per_cu;
2828 }
2829 else
2830 lh_cu = this_cu;
2831
7b9f3c50
DE
2832 lh = NULL;
2833 slot = NULL;
2834 line_offset = 0;
dee91e82
DE
2835
2836 attr = dwarf2_attr (comp_unit_die, DW_AT_stmt_list, cu);
9291a0cd
TT
2837 if (attr)
2838 {
7b9f3c50
DE
2839 struct quick_file_names find_entry;
2840
2841 line_offset = DW_UNSND (attr);
2842
2843 /* We may have already read in this line header (TU line header sharing).
2844 If we have we're done. */
094b34ac
DE
2845 find_entry.hash.dwo_unit = cu->dwo_unit;
2846 find_entry.hash.line_offset.sect_off = line_offset;
7b9f3c50
DE
2847 slot = htab_find_slot (dwarf2_per_objfile->quick_file_names_table,
2848 &find_entry, INSERT);
2849 if (*slot != NULL)
2850 {
094b34ac 2851 lh_cu->v.quick->file_names = *slot;
dee91e82 2852 return;
7b9f3c50
DE
2853 }
2854
3019eac3 2855 lh = dwarf_decode_line_header (line_offset, cu);
9291a0cd
TT
2856 }
2857 if (lh == NULL)
2858 {
094b34ac 2859 lh_cu->v.quick->no_file_data = 1;
dee91e82 2860 return;
9291a0cd
TT
2861 }
2862
7b9f3c50 2863 qfn = obstack_alloc (&objfile->objfile_obstack, sizeof (*qfn));
094b34ac
DE
2864 qfn->hash.dwo_unit = cu->dwo_unit;
2865 qfn->hash.line_offset.sect_off = line_offset;
7b9f3c50
DE
2866 gdb_assert (slot != NULL);
2867 *slot = qfn;
9291a0cd 2868
dee91e82 2869 find_file_and_directory (comp_unit_die, cu, &name, &comp_dir);
9291a0cd 2870
7b9f3c50
DE
2871 qfn->num_file_names = lh->num_file_names;
2872 qfn->file_names = obstack_alloc (&objfile->objfile_obstack,
2873 lh->num_file_names * sizeof (char *));
9291a0cd 2874 for (i = 0; i < lh->num_file_names; ++i)
7b9f3c50
DE
2875 qfn->file_names[i] = file_full_name (i + 1, lh, comp_dir);
2876 qfn->real_names = NULL;
9291a0cd 2877
7b9f3c50 2878 free_line_header (lh);
7b9f3c50 2879
094b34ac 2880 lh_cu->v.quick->file_names = qfn;
dee91e82
DE
2881}
2882
2883/* A helper for the "quick" functions which attempts to read the line
2884 table for THIS_CU. */
2885
2886static struct quick_file_names *
2887dw2_get_file_names (struct objfile *objfile,
2888 struct dwarf2_per_cu_data *this_cu)
2889{
f4dc4d17
DE
2890 /* For TUs this should only be called on the parent group. */
2891 if (this_cu->is_debug_types)
2892 gdb_assert (IS_TYPE_UNIT_GROUP (this_cu));
2893
dee91e82
DE
2894 if (this_cu->v.quick->file_names != NULL)
2895 return this_cu->v.quick->file_names;
2896 /* If we know there is no line data, no point in looking again. */
2897 if (this_cu->v.quick->no_file_data)
2898 return NULL;
2899
3019eac3
DE
2900 /* If DWO files are in use, we can still find the DW_AT_stmt_list attribute
2901 in the stub for CUs, there's is no need to lookup the DWO file.
2902 However, that's not the case for TUs where DW_AT_stmt_list lives in the
2903 DWO file. */
2904 if (this_cu->is_debug_types)
094b34ac
DE
2905 {
2906 struct type_unit_group *tu_group = this_cu->s.type_unit_group;
2907
2908 init_cutu_and_read_dies (tu_group->t.first_tu, NULL, 0, 0,
2909 dw2_get_file_names_reader, tu_group);
2910 }
3019eac3
DE
2911 else
2912 init_cutu_and_read_dies_simple (this_cu, dw2_get_file_names_reader, NULL);
dee91e82
DE
2913
2914 if (this_cu->v.quick->no_file_data)
2915 return NULL;
2916 return this_cu->v.quick->file_names;
9291a0cd
TT
2917}
2918
2919/* A helper for the "quick" functions which computes and caches the
7b9f3c50 2920 real path for a given file name from the line table. */
2fdf6df6 2921
9291a0cd 2922static const char *
7b9f3c50
DE
2923dw2_get_real_path (struct objfile *objfile,
2924 struct quick_file_names *qfn, int index)
9291a0cd 2925{
7b9f3c50
DE
2926 if (qfn->real_names == NULL)
2927 qfn->real_names = OBSTACK_CALLOC (&objfile->objfile_obstack,
2928 qfn->num_file_names, sizeof (char *));
9291a0cd 2929
7b9f3c50
DE
2930 if (qfn->real_names[index] == NULL)
2931 qfn->real_names[index] = gdb_realpath (qfn->file_names[index]);
9291a0cd 2932
7b9f3c50 2933 return qfn->real_names[index];
9291a0cd
TT
2934}
2935
2936static struct symtab *
2937dw2_find_last_source_symtab (struct objfile *objfile)
2938{
2939 int index;
ae2de4f8 2940
9291a0cd
TT
2941 dw2_setup (objfile);
2942 index = dwarf2_per_objfile->n_comp_units - 1;
a0f42c21 2943 return dw2_instantiate_symtab (dw2_get_cu (index));
9291a0cd
TT
2944}
2945
7b9f3c50
DE
2946/* Traversal function for dw2_forget_cached_source_info. */
2947
2948static int
2949dw2_free_cached_file_names (void **slot, void *info)
9291a0cd 2950{
7b9f3c50 2951 struct quick_file_names *file_data = (struct quick_file_names *) *slot;
9291a0cd 2952
7b9f3c50 2953 if (file_data->real_names)
9291a0cd 2954 {
7b9f3c50 2955 int i;
9291a0cd 2956
7b9f3c50 2957 for (i = 0; i < file_data->num_file_names; ++i)
9291a0cd 2958 {
7b9f3c50
DE
2959 xfree ((void*) file_data->real_names[i]);
2960 file_data->real_names[i] = NULL;
9291a0cd
TT
2961 }
2962 }
7b9f3c50
DE
2963
2964 return 1;
2965}
2966
2967static void
2968dw2_forget_cached_source_info (struct objfile *objfile)
2969{
2970 dw2_setup (objfile);
2971
2972 htab_traverse_noresize (dwarf2_per_objfile->quick_file_names_table,
2973 dw2_free_cached_file_names, NULL);
9291a0cd
TT
2974}
2975
f8eba3c6
TT
2976/* Helper function for dw2_map_symtabs_matching_filename that expands
2977 the symtabs and calls the iterator. */
2978
2979static int
2980dw2_map_expand_apply (struct objfile *objfile,
2981 struct dwarf2_per_cu_data *per_cu,
2982 const char *name,
2983 const char *full_path, const char *real_path,
2984 int (*callback) (struct symtab *, void *),
2985 void *data)
2986{
2987 struct symtab *last_made = objfile->symtabs;
2988
2989 /* Don't visit already-expanded CUs. */
2990 if (per_cu->v.quick->symtab)
2991 return 0;
2992
2993 /* This may expand more than one symtab, and we want to iterate over
2994 all of them. */
a0f42c21 2995 dw2_instantiate_symtab (per_cu);
f8eba3c6
TT
2996
2997 return iterate_over_some_symtabs (name, full_path, real_path, callback, data,
2998 objfile->symtabs, last_made);
2999}
3000
3001/* Implementation of the map_symtabs_matching_filename method. */
3002
9291a0cd 3003static int
f8eba3c6
TT
3004dw2_map_symtabs_matching_filename (struct objfile *objfile, const char *name,
3005 const char *full_path, const char *real_path,
3006 int (*callback) (struct symtab *, void *),
3007 void *data)
9291a0cd
TT
3008{
3009 int i;
c011a4f4 3010 const char *name_basename = lbasename (name);
4aac40c8
TT
3011 int name_len = strlen (name);
3012 int is_abs = IS_ABSOLUTE_PATH (name);
9291a0cd
TT
3013
3014 dw2_setup (objfile);
ae2de4f8 3015
f4dc4d17
DE
3016 dw2_build_type_unit_groups ();
3017
1fd400ff 3018 for (i = 0; i < (dwarf2_per_objfile->n_comp_units
f4dc4d17 3019 + dwarf2_per_objfile->n_type_unit_groups); ++i)
9291a0cd
TT
3020 {
3021 int j;
f4dc4d17 3022 struct dwarf2_per_cu_data *per_cu = dw2_get_primary_cu (i);
7b9f3c50 3023 struct quick_file_names *file_data;
9291a0cd 3024
3d7bb9d9 3025 /* We only need to look at symtabs not already expanded. */
e254ef6a 3026 if (per_cu->v.quick->symtab)
9291a0cd
TT
3027 continue;
3028
7b9f3c50
DE
3029 file_data = dw2_get_file_names (objfile, per_cu);
3030 if (file_data == NULL)
9291a0cd
TT
3031 continue;
3032
7b9f3c50 3033 for (j = 0; j < file_data->num_file_names; ++j)
9291a0cd 3034 {
7b9f3c50 3035 const char *this_name = file_data->file_names[j];
9291a0cd 3036
4aac40c8
TT
3037 if (FILENAME_CMP (name, this_name) == 0
3038 || (!is_abs && compare_filenames_for_search (this_name,
3039 name, name_len)))
9291a0cd 3040 {
f8eba3c6
TT
3041 if (dw2_map_expand_apply (objfile, per_cu,
3042 name, full_path, real_path,
3043 callback, data))
3044 return 1;
4aac40c8 3045 }
9291a0cd 3046
c011a4f4
DE
3047 /* Before we invoke realpath, which can get expensive when many
3048 files are involved, do a quick comparison of the basenames. */
3049 if (! basenames_may_differ
3050 && FILENAME_CMP (lbasename (this_name), name_basename) != 0)
3051 continue;
3052
9291a0cd
TT
3053 if (full_path != NULL)
3054 {
7b9f3c50
DE
3055 const char *this_real_name = dw2_get_real_path (objfile,
3056 file_data, j);
9291a0cd 3057
7b9f3c50 3058 if (this_real_name != NULL
4aac40c8
TT
3059 && (FILENAME_CMP (full_path, this_real_name) == 0
3060 || (!is_abs
3061 && compare_filenames_for_search (this_real_name,
3062 name, name_len))))
9291a0cd 3063 {
f8eba3c6
TT
3064 if (dw2_map_expand_apply (objfile, per_cu,
3065 name, full_path, real_path,
3066 callback, data))
3067 return 1;
9291a0cd
TT
3068 }
3069 }
3070
3071 if (real_path != NULL)
3072 {
7b9f3c50
DE
3073 const char *this_real_name = dw2_get_real_path (objfile,
3074 file_data, j);
9291a0cd 3075
7b9f3c50 3076 if (this_real_name != NULL
4aac40c8
TT
3077 && (FILENAME_CMP (real_path, this_real_name) == 0
3078 || (!is_abs
3079 && compare_filenames_for_search (this_real_name,
3080 name, name_len))))
9291a0cd 3081 {
f8eba3c6
TT
3082 if (dw2_map_expand_apply (objfile, per_cu,
3083 name, full_path, real_path,
3084 callback, data))
3085 return 1;
9291a0cd
TT
3086 }
3087 }
3088 }
3089 }
3090
9291a0cd
TT
3091 return 0;
3092}
3093
3094static struct symtab *
3095dw2_lookup_symbol (struct objfile *objfile, int block_index,
3096 const char *name, domain_enum domain)
3097{
774b6a14 3098 /* We do all the work in the pre_expand_symtabs_matching hook
9291a0cd
TT
3099 instead. */
3100 return NULL;
3101}
3102
3103/* A helper function that expands all symtabs that hold an object
156942c7
DE
3104 named NAME. If WANT_SPECIFIC_BLOCK is non-zero, only look for
3105 symbols in block BLOCK_KIND. */
2fdf6df6 3106
9291a0cd 3107static void
156942c7
DE
3108dw2_do_expand_symtabs_matching (struct objfile *objfile,
3109 int want_specific_block,
3110 enum block_enum block_kind,
3111 const char *name, domain_enum domain)
9291a0cd 3112{
156942c7
DE
3113 struct mapped_index *index;
3114
9291a0cd
TT
3115 dw2_setup (objfile);
3116
156942c7
DE
3117 index = dwarf2_per_objfile->index_table;
3118
ae2de4f8 3119 /* index_table is NULL if OBJF_READNOW. */
156942c7 3120 if (index)
9291a0cd
TT
3121 {
3122 offset_type *vec;
3123
156942c7 3124 if (find_slot_in_mapped_hash (index, name, &vec))
9291a0cd
TT
3125 {
3126 offset_type i, len = MAYBE_SWAP (*vec);
3127 for (i = 0; i < len; ++i)
3128 {
156942c7
DE
3129 offset_type cu_index_and_attrs = MAYBE_SWAP (vec[i + 1]);
3130 offset_type cu_index = GDB_INDEX_CU_VALUE (cu_index_and_attrs);
e254ef6a 3131 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (cu_index);
156942c7
DE
3132 int want_static = block_kind != GLOBAL_BLOCK;
3133 /* This value is only valid for index versions >= 7. */
3134 int is_static = GDB_INDEX_SYMBOL_STATIC_VALUE (cu_index_and_attrs);
3135 gdb_index_symbol_kind symbol_kind =
3136 GDB_INDEX_SYMBOL_KIND_VALUE (cu_index_and_attrs);
3137
3138 if (want_specific_block
3139 && index->version >= 7
3140 && want_static != is_static)
3141 continue;
3142
3143 /* Only check the symbol's kind if it has one.
3144 Indices prior to version 7 don't record it. */
3145 if (index->version >= 7)
3146 {
3147 switch (domain)
3148 {
3149 case VAR_DOMAIN:
3150 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_VARIABLE
3151 && symbol_kind != GDB_INDEX_SYMBOL_KIND_FUNCTION
3152 /* Some types are also in VAR_DOMAIN. */
3153 && symbol_kind != GDB_INDEX_SYMBOL_KIND_TYPE)
3154 continue;
3155 break;
3156 case STRUCT_DOMAIN:
3157 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_TYPE)
3158 continue;
3159 break;
3160 case LABEL_DOMAIN:
3161 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_OTHER)
3162 continue;
3163 break;
3164 default:
3165 break;
3166 }
3167 }
1fd400ff 3168
a0f42c21 3169 dw2_instantiate_symtab (per_cu);
9291a0cd
TT
3170 }
3171 }
3172 }
3173}
3174
774b6a14
TT
3175static void
3176dw2_pre_expand_symtabs_matching (struct objfile *objfile,
8903c50d 3177 enum block_enum block_kind, const char *name,
774b6a14 3178 domain_enum domain)
9291a0cd 3179{
156942c7 3180 dw2_do_expand_symtabs_matching (objfile, 1, block_kind, name, domain);
9291a0cd
TT
3181}
3182
3183static void
3184dw2_print_stats (struct objfile *objfile)
3185{
3186 int i, count;
3187
3188 dw2_setup (objfile);
3189 count = 0;
1fd400ff 3190 for (i = 0; i < (dwarf2_per_objfile->n_comp_units
d467dd73 3191 + dwarf2_per_objfile->n_type_units); ++i)
9291a0cd 3192 {
e254ef6a 3193 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
9291a0cd 3194
e254ef6a 3195 if (!per_cu->v.quick->symtab)
9291a0cd
TT
3196 ++count;
3197 }
3198 printf_filtered (_(" Number of unread CUs: %d\n"), count);
3199}
3200
3201static void
3202dw2_dump (struct objfile *objfile)
3203{
3204 /* Nothing worth printing. */
3205}
3206
3207static void
3208dw2_relocate (struct objfile *objfile, struct section_offsets *new_offsets,
3209 struct section_offsets *delta)
3210{
3211 /* There's nothing to relocate here. */
3212}
3213
3214static void
3215dw2_expand_symtabs_for_function (struct objfile *objfile,
3216 const char *func_name)
3217{
156942c7
DE
3218 /* Note: It doesn't matter what we pass for block_kind here. */
3219 dw2_do_expand_symtabs_matching (objfile, 0, GLOBAL_BLOCK, func_name,
3220 VAR_DOMAIN);
9291a0cd
TT
3221}
3222
3223static void
3224dw2_expand_all_symtabs (struct objfile *objfile)
3225{
3226 int i;
3227
3228 dw2_setup (objfile);
1fd400ff
TT
3229
3230 for (i = 0; i < (dwarf2_per_objfile->n_comp_units
d467dd73 3231 + dwarf2_per_objfile->n_type_units); ++i)
9291a0cd 3232 {
e254ef6a 3233 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
9291a0cd 3234
a0f42c21 3235 dw2_instantiate_symtab (per_cu);
9291a0cd
TT
3236 }
3237}
3238
3239static void
3240dw2_expand_symtabs_with_filename (struct objfile *objfile,
3241 const char *filename)
3242{
3243 int i;
3244
3245 dw2_setup (objfile);
d4637a04
DE
3246
3247 /* We don't need to consider type units here.
3248 This is only called for examining code, e.g. expand_line_sal.
3249 There can be an order of magnitude (or more) more type units
3250 than comp units, and we avoid them if we can. */
3251
3252 for (i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
9291a0cd
TT
3253 {
3254 int j;
e254ef6a 3255 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
7b9f3c50 3256 struct quick_file_names *file_data;
9291a0cd 3257
3d7bb9d9 3258 /* We only need to look at symtabs not already expanded. */
e254ef6a 3259 if (per_cu->v.quick->symtab)
9291a0cd
TT
3260 continue;
3261
7b9f3c50
DE
3262 file_data = dw2_get_file_names (objfile, per_cu);
3263 if (file_data == NULL)
9291a0cd
TT
3264 continue;
3265
7b9f3c50 3266 for (j = 0; j < file_data->num_file_names; ++j)
9291a0cd 3267 {
7b9f3c50 3268 const char *this_name = file_data->file_names[j];
1ef75ecc 3269 if (FILENAME_CMP (this_name, filename) == 0)
9291a0cd 3270 {
a0f42c21 3271 dw2_instantiate_symtab (per_cu);
9291a0cd
TT
3272 break;
3273 }
3274 }
3275 }
3276}
3277
356d9f9d
TT
3278/* A helper function for dw2_find_symbol_file that finds the primary
3279 file name for a given CU. This is a die_reader_func. */
3280
3281static void
3282dw2_get_primary_filename_reader (const struct die_reader_specs *reader,
3283 gdb_byte *info_ptr,
3284 struct die_info *comp_unit_die,
3285 int has_children,
3286 void *data)
3287{
3288 const char **result_ptr = data;
3289 struct dwarf2_cu *cu = reader->cu;
3290 struct attribute *attr;
3291
3292 attr = dwarf2_attr (comp_unit_die, DW_AT_name, cu);
3293 if (attr == NULL)
3294 *result_ptr = NULL;
3295 else
3296 *result_ptr = DW_STRING (attr);
3297}
3298
dd786858 3299static const char *
9291a0cd
TT
3300dw2_find_symbol_file (struct objfile *objfile, const char *name)
3301{
e254ef6a 3302 struct dwarf2_per_cu_data *per_cu;
9291a0cd 3303 offset_type *vec;
7b9f3c50 3304 struct quick_file_names *file_data;
356d9f9d 3305 const char *filename;
9291a0cd
TT
3306
3307 dw2_setup (objfile);
3308
ae2de4f8 3309 /* index_table is NULL if OBJF_READNOW. */
9291a0cd 3310 if (!dwarf2_per_objfile->index_table)
96408a79
SA
3311 {
3312 struct symtab *s;
3313
d790cf0a
DE
3314 ALL_OBJFILE_PRIMARY_SYMTABS (objfile, s)
3315 {
3316 struct blockvector *bv = BLOCKVECTOR (s);
3317 const struct block *block = BLOCKVECTOR_BLOCK (bv, GLOBAL_BLOCK);
3318 struct symbol *sym = lookup_block_symbol (block, name, VAR_DOMAIN);
3319
3320 if (sym)
3321 return sym->symtab->filename;
3322 }
96408a79
SA
3323 return NULL;
3324 }
9291a0cd
TT
3325
3326 if (!find_slot_in_mapped_hash (dwarf2_per_objfile->index_table,
3327 name, &vec))
3328 return NULL;
3329
3330 /* Note that this just looks at the very first one named NAME -- but
3331 actually we are looking for a function. find_main_filename
3332 should be rewritten so that it doesn't require a custom hook. It
3333 could just use the ordinary symbol tables. */
3334 /* vec[0] is the length, which must always be >0. */
156942c7 3335 per_cu = dw2_get_cu (GDB_INDEX_CU_VALUE (MAYBE_SWAP (vec[1])));
9291a0cd 3336
356d9f9d
TT
3337 if (per_cu->v.quick->symtab != NULL)
3338 return per_cu->v.quick->symtab->filename;
3339
f4dc4d17
DE
3340 init_cutu_and_read_dies (per_cu, NULL, 0, 0,
3341 dw2_get_primary_filename_reader, &filename);
9291a0cd 3342
356d9f9d 3343 return filename;
9291a0cd
TT
3344}
3345
3346static void
40658b94
PH
3347dw2_map_matching_symbols (const char * name, domain_enum namespace,
3348 struct objfile *objfile, int global,
3349 int (*callback) (struct block *,
3350 struct symbol *, void *),
2edb89d3
JK
3351 void *data, symbol_compare_ftype *match,
3352 symbol_compare_ftype *ordered_compare)
9291a0cd 3353{
40658b94 3354 /* Currently unimplemented; used for Ada. The function can be called if the
a9e6a4bb
JK
3355 current language is Ada for a non-Ada objfile using GNU index. As Ada
3356 does not look for non-Ada symbols this function should just return. */
9291a0cd
TT
3357}
3358
3359static void
f8eba3c6
TT
3360dw2_expand_symtabs_matching
3361 (struct objfile *objfile,
3362 int (*file_matcher) (const char *, void *),
e078317b 3363 int (*name_matcher) (const char *, void *),
f8eba3c6
TT
3364 enum search_domain kind,
3365 void *data)
9291a0cd
TT
3366{
3367 int i;
3368 offset_type iter;
4b5246aa 3369 struct mapped_index *index;
9291a0cd
TT
3370
3371 dw2_setup (objfile);
ae2de4f8
DE
3372
3373 /* index_table is NULL if OBJF_READNOW. */
9291a0cd
TT
3374 if (!dwarf2_per_objfile->index_table)
3375 return;
4b5246aa 3376 index = dwarf2_per_objfile->index_table;
9291a0cd 3377
7b08b9eb 3378 if (file_matcher != NULL)
24c79950
TT
3379 {
3380 struct cleanup *cleanup;
3381 htab_t visited_found, visited_not_found;
3382
f4dc4d17
DE
3383 dw2_build_type_unit_groups ();
3384
24c79950
TT
3385 visited_found = htab_create_alloc (10,
3386 htab_hash_pointer, htab_eq_pointer,
3387 NULL, xcalloc, xfree);
3388 cleanup = make_cleanup_htab_delete (visited_found);
3389 visited_not_found = htab_create_alloc (10,
3390 htab_hash_pointer, htab_eq_pointer,
3391 NULL, xcalloc, xfree);
3392 make_cleanup_htab_delete (visited_not_found);
3393
3394 for (i = 0; i < (dwarf2_per_objfile->n_comp_units
f4dc4d17 3395 + dwarf2_per_objfile->n_type_unit_groups); ++i)
24c79950
TT
3396 {
3397 int j;
f4dc4d17 3398 struct dwarf2_per_cu_data *per_cu = dw2_get_primary_cu (i);
24c79950
TT
3399 struct quick_file_names *file_data;
3400 void **slot;
7b08b9eb 3401
24c79950 3402 per_cu->v.quick->mark = 0;
3d7bb9d9 3403
24c79950
TT
3404 /* We only need to look at symtabs not already expanded. */
3405 if (per_cu->v.quick->symtab)
3406 continue;
7b08b9eb 3407
24c79950
TT
3408 file_data = dw2_get_file_names (objfile, per_cu);
3409 if (file_data == NULL)
3410 continue;
7b08b9eb 3411
24c79950
TT
3412 if (htab_find (visited_not_found, file_data) != NULL)
3413 continue;
3414 else if (htab_find (visited_found, file_data) != NULL)
3415 {
3416 per_cu->v.quick->mark = 1;
3417 continue;
3418 }
3419
3420 for (j = 0; j < file_data->num_file_names; ++j)
3421 {
3422 if (file_matcher (file_data->file_names[j], data))
3423 {
3424 per_cu->v.quick->mark = 1;
3425 break;
3426 }
3427 }
3428
3429 slot = htab_find_slot (per_cu->v.quick->mark
3430 ? visited_found
3431 : visited_not_found,
3432 file_data, INSERT);
3433 *slot = file_data;
3434 }
3435
3436 do_cleanups (cleanup);
3437 }
9291a0cd 3438
3876f04e 3439 for (iter = 0; iter < index->symbol_table_slots; ++iter)
9291a0cd
TT
3440 {
3441 offset_type idx = 2 * iter;
3442 const char *name;
3443 offset_type *vec, vec_len, vec_idx;
3444
3876f04e 3445 if (index->symbol_table[idx] == 0 && index->symbol_table[idx + 1] == 0)
9291a0cd
TT
3446 continue;
3447
3876f04e 3448 name = index->constant_pool + MAYBE_SWAP (index->symbol_table[idx]);
9291a0cd 3449
e078317b 3450 if (! (*name_matcher) (name, data))
9291a0cd
TT
3451 continue;
3452
3453 /* The name was matched, now expand corresponding CUs that were
3454 marked. */
4b5246aa 3455 vec = (offset_type *) (index->constant_pool
3876f04e 3456 + MAYBE_SWAP (index->symbol_table[idx + 1]));
9291a0cd
TT
3457 vec_len = MAYBE_SWAP (vec[0]);
3458 for (vec_idx = 0; vec_idx < vec_len; ++vec_idx)
3459 {
e254ef6a 3460 struct dwarf2_per_cu_data *per_cu;
156942c7
DE
3461 offset_type cu_index_and_attrs = MAYBE_SWAP (vec[vec_idx + 1]);
3462 gdb_index_symbol_kind symbol_kind =
3463 GDB_INDEX_SYMBOL_KIND_VALUE (cu_index_and_attrs);
3464 int cu_index = GDB_INDEX_CU_VALUE (cu_index_and_attrs);
3465
3466 /* Don't crash on bad data. */
3467 if (cu_index >= (dwarf2_per_objfile->n_comp_units
667e0a4b 3468 + dwarf2_per_objfile->n_type_units))
156942c7 3469 continue;
1fd400ff 3470
156942c7
DE
3471 /* Only check the symbol's kind if it has one.
3472 Indices prior to version 7 don't record it. */
3473 if (index->version >= 7)
3474 {
3475 switch (kind)
3476 {
3477 case VARIABLES_DOMAIN:
3478 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_VARIABLE)
3479 continue;
3480 break;
3481 case FUNCTIONS_DOMAIN:
3482 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_FUNCTION)
3483 continue;
3484 break;
3485 case TYPES_DOMAIN:
3486 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_TYPE)
3487 continue;
3488 break;
3489 default:
3490 break;
3491 }
3492 }
3493
3494 per_cu = dw2_get_cu (cu_index);
7b08b9eb 3495 if (file_matcher == NULL || per_cu->v.quick->mark)
a0f42c21 3496 dw2_instantiate_symtab (per_cu);
9291a0cd
TT
3497 }
3498 }
3499}
3500
9703b513
TT
3501/* A helper for dw2_find_pc_sect_symtab which finds the most specific
3502 symtab. */
3503
3504static struct symtab *
3505recursively_find_pc_sect_symtab (struct symtab *symtab, CORE_ADDR pc)
3506{
3507 int i;
3508
3509 if (BLOCKVECTOR (symtab) != NULL
3510 && blockvector_contains_pc (BLOCKVECTOR (symtab), pc))
3511 return symtab;
3512
a3ec0bb1
DE
3513 if (symtab->includes == NULL)
3514 return NULL;
3515
9703b513
TT
3516 for (i = 0; symtab->includes[i]; ++i)
3517 {
a3ec0bb1 3518 struct symtab *s = symtab->includes[i];
9703b513
TT
3519
3520 s = recursively_find_pc_sect_symtab (s, pc);
3521 if (s != NULL)
3522 return s;
3523 }
3524
3525 return NULL;
3526}
3527
9291a0cd
TT
3528static struct symtab *
3529dw2_find_pc_sect_symtab (struct objfile *objfile,
3530 struct minimal_symbol *msymbol,
3531 CORE_ADDR pc,
3532 struct obj_section *section,
3533 int warn_if_readin)
3534{
3535 struct dwarf2_per_cu_data *data;
9703b513 3536 struct symtab *result;
9291a0cd
TT
3537
3538 dw2_setup (objfile);
3539
3540 if (!objfile->psymtabs_addrmap)
3541 return NULL;
3542
3543 data = addrmap_find (objfile->psymtabs_addrmap, pc);
3544 if (!data)
3545 return NULL;
3546
3547 if (warn_if_readin && data->v.quick->symtab)
abebb8b0 3548 warning (_("(Internal error: pc %s in read in CU, but not in symtab.)"),
9291a0cd
TT
3549 paddress (get_objfile_arch (objfile), pc));
3550
9703b513
TT
3551 result = recursively_find_pc_sect_symtab (dw2_instantiate_symtab (data), pc);
3552 gdb_assert (result != NULL);
3553 return result;
9291a0cd
TT
3554}
3555
9291a0cd 3556static void
44b13c5a 3557dw2_map_symbol_filenames (struct objfile *objfile, symbol_filename_ftype *fun,
74e2f255 3558 void *data, int need_fullname)
9291a0cd
TT
3559{
3560 int i;
24c79950
TT
3561 struct cleanup *cleanup;
3562 htab_t visited = htab_create_alloc (10, htab_hash_pointer, htab_eq_pointer,
3563 NULL, xcalloc, xfree);
9291a0cd 3564
24c79950 3565 cleanup = make_cleanup_htab_delete (visited);
9291a0cd 3566 dw2_setup (objfile);
ae2de4f8 3567
f4dc4d17
DE
3568 dw2_build_type_unit_groups ();
3569
24c79950
TT
3570 /* We can ignore file names coming from already-expanded CUs. */
3571 for (i = 0; i < (dwarf2_per_objfile->n_comp_units
3572 + dwarf2_per_objfile->n_type_units); ++i)
3573 {
3574 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
3575
3576 if (per_cu->v.quick->symtab)
3577 {
3578 void **slot = htab_find_slot (visited, per_cu->v.quick->file_names,
3579 INSERT);
3580
3581 *slot = per_cu->v.quick->file_names;
3582 }
3583 }
3584
1fd400ff 3585 for (i = 0; i < (dwarf2_per_objfile->n_comp_units
f4dc4d17 3586 + dwarf2_per_objfile->n_type_unit_groups); ++i)
9291a0cd
TT
3587 {
3588 int j;
f4dc4d17 3589 struct dwarf2_per_cu_data *per_cu = dw2_get_primary_cu (i);
7b9f3c50 3590 struct quick_file_names *file_data;
24c79950 3591 void **slot;
9291a0cd 3592
3d7bb9d9 3593 /* We only need to look at symtabs not already expanded. */
e254ef6a 3594 if (per_cu->v.quick->symtab)
9291a0cd
TT
3595 continue;
3596
7b9f3c50
DE
3597 file_data = dw2_get_file_names (objfile, per_cu);
3598 if (file_data == NULL)
9291a0cd
TT
3599 continue;
3600
24c79950
TT
3601 slot = htab_find_slot (visited, file_data, INSERT);
3602 if (*slot)
3603 {
3604 /* Already visited. */
3605 continue;
3606 }
3607 *slot = file_data;
3608
7b9f3c50 3609 for (j = 0; j < file_data->num_file_names; ++j)
9291a0cd 3610 {
74e2f255
DE
3611 const char *this_real_name;
3612
3613 if (need_fullname)
3614 this_real_name = dw2_get_real_path (objfile, file_data, j);
3615 else
3616 this_real_name = NULL;
7b9f3c50 3617 (*fun) (file_data->file_names[j], this_real_name, data);
9291a0cd
TT
3618 }
3619 }
24c79950
TT
3620
3621 do_cleanups (cleanup);
9291a0cd
TT
3622}
3623
3624static int
3625dw2_has_symbols (struct objfile *objfile)
3626{
3627 return 1;
3628}
3629
3630const struct quick_symbol_functions dwarf2_gdb_index_functions =
3631{
3632 dw2_has_symbols,
3633 dw2_find_last_source_symtab,
3634 dw2_forget_cached_source_info,
f8eba3c6 3635 dw2_map_symtabs_matching_filename,
9291a0cd 3636 dw2_lookup_symbol,
774b6a14 3637 dw2_pre_expand_symtabs_matching,
9291a0cd
TT
3638 dw2_print_stats,
3639 dw2_dump,
3640 dw2_relocate,
3641 dw2_expand_symtabs_for_function,
3642 dw2_expand_all_symtabs,
3643 dw2_expand_symtabs_with_filename,
3644 dw2_find_symbol_file,
40658b94 3645 dw2_map_matching_symbols,
9291a0cd
TT
3646 dw2_expand_symtabs_matching,
3647 dw2_find_pc_sect_symtab,
9291a0cd
TT
3648 dw2_map_symbol_filenames
3649};
3650
3651/* Initialize for reading DWARF for this objfile. Return 0 if this
3652 file will use psymtabs, or 1 if using the GNU index. */
3653
3654int
3655dwarf2_initialize_objfile (struct objfile *objfile)
3656{
3657 /* If we're about to read full symbols, don't bother with the
3658 indices. In this case we also don't care if some other debug
3659 format is making psymtabs, because they are all about to be
3660 expanded anyway. */
3661 if ((objfile->flags & OBJF_READNOW))
3662 {
3663 int i;
3664
3665 dwarf2_per_objfile->using_index = 1;
3666 create_all_comp_units (objfile);
0e50663e 3667 create_all_type_units (objfile);
7b9f3c50
DE
3668 dwarf2_per_objfile->quick_file_names_table =
3669 create_quick_file_names_table (dwarf2_per_objfile->n_comp_units);
9291a0cd 3670
1fd400ff 3671 for (i = 0; i < (dwarf2_per_objfile->n_comp_units
d467dd73 3672 + dwarf2_per_objfile->n_type_units); ++i)
9291a0cd 3673 {
e254ef6a 3674 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
9291a0cd 3675
e254ef6a
DE
3676 per_cu->v.quick = OBSTACK_ZALLOC (&objfile->objfile_obstack,
3677 struct dwarf2_per_cu_quick_data);
9291a0cd
TT
3678 }
3679
3680 /* Return 1 so that gdb sees the "quick" functions. However,
3681 these functions will be no-ops because we will have expanded
3682 all symtabs. */
3683 return 1;
3684 }
3685
3686 if (dwarf2_read_index (objfile))
3687 return 1;
3688
9291a0cd
TT
3689 return 0;
3690}
3691
3692\f
3693
dce234bc
PP
3694/* Build a partial symbol table. */
3695
3696void
f29dff0a 3697dwarf2_build_psymtabs (struct objfile *objfile)
dce234bc 3698{
f29dff0a 3699 if (objfile->global_psymbols.size == 0 && objfile->static_psymbols.size == 0)
c906108c
SS
3700 {
3701 init_psymbol_list (objfile, 1024);
3702 }
3703
d146bf1e 3704 dwarf2_build_psymtabs_hard (objfile);
c906108c 3705}
c906108c 3706
1ce1cefd
DE
3707/* Return the total length of the CU described by HEADER. */
3708
3709static unsigned int
3710get_cu_length (const struct comp_unit_head *header)
3711{
3712 return header->initial_length_size + header->length;
3713}
3714
45452591
DE
3715/* Return TRUE if OFFSET is within CU_HEADER. */
3716
3717static inline int
b64f50a1 3718offset_in_cu_p (const struct comp_unit_head *cu_header, sect_offset offset)
45452591 3719{
b64f50a1 3720 sect_offset bottom = { cu_header->offset.sect_off };
1ce1cefd 3721 sect_offset top = { cu_header->offset.sect_off + get_cu_length (cu_header) };
9a619af0 3722
b64f50a1 3723 return (offset.sect_off >= bottom.sect_off && offset.sect_off < top.sect_off);
45452591
DE
3724}
3725
3b80fe9b
DE
3726/* Find the base address of the compilation unit for range lists and
3727 location lists. It will normally be specified by DW_AT_low_pc.
3728 In DWARF-3 draft 4, the base address could be overridden by
3729 DW_AT_entry_pc. It's been removed, but GCC still uses this for
3730 compilation units with discontinuous ranges. */
3731
3732static void
3733dwarf2_find_base_address (struct die_info *die, struct dwarf2_cu *cu)
3734{
3735 struct attribute *attr;
3736
3737 cu->base_known = 0;
3738 cu->base_address = 0;
3739
3740 attr = dwarf2_attr (die, DW_AT_entry_pc, cu);
3741 if (attr)
3742 {
3743 cu->base_address = DW_ADDR (attr);
3744 cu->base_known = 1;
3745 }
3746 else
3747 {
3748 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
3749 if (attr)
3750 {
3751 cu->base_address = DW_ADDR (attr);
3752 cu->base_known = 1;
3753 }
3754 }
3755}
3756
93311388
DE
3757/* Read in the comp unit header information from the debug_info at info_ptr.
3758 NOTE: This leaves members offset, first_die_offset to be filled in
3759 by the caller. */
107d2387 3760
fe1b8b76 3761static gdb_byte *
107d2387 3762read_comp_unit_head (struct comp_unit_head *cu_header,
fe1b8b76 3763 gdb_byte *info_ptr, bfd *abfd)
107d2387
AC
3764{
3765 int signed_addr;
891d2f0b 3766 unsigned int bytes_read;
c764a876
DE
3767
3768 cu_header->length = read_initial_length (abfd, info_ptr, &bytes_read);
3769 cu_header->initial_length_size = bytes_read;
3770 cu_header->offset_size = (bytes_read == 4) ? 4 : 8;
613e1657 3771 info_ptr += bytes_read;
107d2387
AC
3772 cu_header->version = read_2_bytes (abfd, info_ptr);
3773 info_ptr += 2;
b64f50a1
JK
3774 cu_header->abbrev_offset.sect_off = read_offset (abfd, info_ptr, cu_header,
3775 &bytes_read);
613e1657 3776 info_ptr += bytes_read;
107d2387
AC
3777 cu_header->addr_size = read_1_byte (abfd, info_ptr);
3778 info_ptr += 1;
3779 signed_addr = bfd_get_sign_extend_vma (abfd);
3780 if (signed_addr < 0)
8e65ff28 3781 internal_error (__FILE__, __LINE__,
e2e0b3e5 3782 _("read_comp_unit_head: dwarf from non elf file"));
107d2387 3783 cu_header->signed_addr_p = signed_addr;
c764a876 3784
107d2387
AC
3785 return info_ptr;
3786}
3787
36586728
TT
3788/* Helper function that returns the proper abbrev section for
3789 THIS_CU. */
3790
3791static struct dwarf2_section_info *
3792get_abbrev_section_for_cu (struct dwarf2_per_cu_data *this_cu)
3793{
3794 struct dwarf2_section_info *abbrev;
3795
3796 if (this_cu->is_dwz)
3797 abbrev = &dwarf2_get_dwz_file ()->abbrev;
3798 else
3799 abbrev = &dwarf2_per_objfile->abbrev;
3800
3801 return abbrev;
3802}
3803
9ff913ba
DE
3804/* Subroutine of read_and_check_comp_unit_head and
3805 read_and_check_type_unit_head to simplify them.
3806 Perform various error checking on the header. */
3807
3808static void
3809error_check_comp_unit_head (struct comp_unit_head *header,
4bdcc0c1
DE
3810 struct dwarf2_section_info *section,
3811 struct dwarf2_section_info *abbrev_section)
9ff913ba
DE
3812{
3813 bfd *abfd = section->asection->owner;
3814 const char *filename = bfd_get_filename (abfd);
3815
3816 if (header->version != 2 && header->version != 3 && header->version != 4)
3817 error (_("Dwarf Error: wrong version in compilation unit header "
3818 "(is %d, should be 2, 3, or 4) [in module %s]"), header->version,
3819 filename);
3820
b64f50a1 3821 if (header->abbrev_offset.sect_off
36586728 3822 >= dwarf2_section_size (dwarf2_per_objfile->objfile, abbrev_section))
9ff913ba
DE
3823 error (_("Dwarf Error: bad offset (0x%lx) in compilation unit header "
3824 "(offset 0x%lx + 6) [in module %s]"),
b64f50a1 3825 (long) header->abbrev_offset.sect_off, (long) header->offset.sect_off,
9ff913ba
DE
3826 filename);
3827
3828 /* Cast to unsigned long to use 64-bit arithmetic when possible to
3829 avoid potential 32-bit overflow. */
1ce1cefd 3830 if (((unsigned long) header->offset.sect_off + get_cu_length (header))
9ff913ba
DE
3831 > section->size)
3832 error (_("Dwarf Error: bad length (0x%lx) in compilation unit header "
3833 "(offset 0x%lx + 0) [in module %s]"),
b64f50a1 3834 (long) header->length, (long) header->offset.sect_off,
9ff913ba
DE
3835 filename);
3836}
3837
3838/* Read in a CU/TU header and perform some basic error checking.
3839 The contents of the header are stored in HEADER.
3840 The result is a pointer to the start of the first DIE. */
adabb602 3841
fe1b8b76 3842static gdb_byte *
9ff913ba
DE
3843read_and_check_comp_unit_head (struct comp_unit_head *header,
3844 struct dwarf2_section_info *section,
4bdcc0c1 3845 struct dwarf2_section_info *abbrev_section,
9ff913ba
DE
3846 gdb_byte *info_ptr,
3847 int is_debug_types_section)
72bf9492 3848{
fe1b8b76 3849 gdb_byte *beg_of_comp_unit = info_ptr;
9ff913ba 3850 bfd *abfd = section->asection->owner;
72bf9492 3851
b64f50a1 3852 header->offset.sect_off = beg_of_comp_unit - section->buffer;
adabb602 3853
72bf9492
DJ
3854 info_ptr = read_comp_unit_head (header, info_ptr, abfd);
3855
460c1c54
CC
3856 /* If we're reading a type unit, skip over the signature and
3857 type_offset fields. */
b0df02fd 3858 if (is_debug_types_section)
460c1c54
CC
3859 info_ptr += 8 /*signature*/ + header->offset_size;
3860
b64f50a1 3861 header->first_die_offset.cu_off = info_ptr - beg_of_comp_unit;
adabb602 3862
4bdcc0c1 3863 error_check_comp_unit_head (header, section, abbrev_section);
72bf9492
DJ
3864
3865 return info_ptr;
3866}
3867
348e048f
DE
3868/* Read in the types comp unit header information from .debug_types entry at
3869 types_ptr. The result is a pointer to one past the end of the header. */
3870
3871static gdb_byte *
9ff913ba
DE
3872read_and_check_type_unit_head (struct comp_unit_head *header,
3873 struct dwarf2_section_info *section,
4bdcc0c1 3874 struct dwarf2_section_info *abbrev_section,
9ff913ba 3875 gdb_byte *info_ptr,
dee91e82
DE
3876 ULONGEST *signature,
3877 cu_offset *type_offset_in_tu)
348e048f 3878{
9ff913ba
DE
3879 gdb_byte *beg_of_comp_unit = info_ptr;
3880 bfd *abfd = section->asection->owner;
348e048f 3881
b64f50a1 3882 header->offset.sect_off = beg_of_comp_unit - section->buffer;
348e048f 3883
9ff913ba 3884 info_ptr = read_comp_unit_head (header, info_ptr, abfd);
348e048f 3885
9ff913ba
DE
3886 /* If we're reading a type unit, skip over the signature and
3887 type_offset fields. */
3888 if (signature != NULL)
3889 *signature = read_8_bytes (abfd, info_ptr);
3890 info_ptr += 8;
dee91e82
DE
3891 if (type_offset_in_tu != NULL)
3892 type_offset_in_tu->cu_off = read_offset_1 (abfd, info_ptr,
3893 header->offset_size);
9ff913ba
DE
3894 info_ptr += header->offset_size;
3895
b64f50a1 3896 header->first_die_offset.cu_off = info_ptr - beg_of_comp_unit;
348e048f 3897
4bdcc0c1 3898 error_check_comp_unit_head (header, section, abbrev_section);
9ff913ba
DE
3899
3900 return info_ptr;
348e048f
DE
3901}
3902
f4dc4d17
DE
3903/* Fetch the abbreviation table offset from a comp or type unit header. */
3904
3905static sect_offset
3906read_abbrev_offset (struct dwarf2_section_info *section,
3907 sect_offset offset)
3908{
3909 bfd *abfd = section->asection->owner;
3910 gdb_byte *info_ptr;
3911 unsigned int length, initial_length_size, offset_size;
3912 sect_offset abbrev_offset;
3913
3914 dwarf2_read_section (dwarf2_per_objfile->objfile, section);
3915 info_ptr = section->buffer + offset.sect_off;
3916 length = read_initial_length (abfd, info_ptr, &initial_length_size);
3917 offset_size = initial_length_size == 4 ? 4 : 8;
3918 info_ptr += initial_length_size + 2 /*version*/;
3919 abbrev_offset.sect_off = read_offset_1 (abfd, info_ptr, offset_size);
3920 return abbrev_offset;
3921}
3922
aaa75496
JB
3923/* Allocate a new partial symtab for file named NAME and mark this new
3924 partial symtab as being an include of PST. */
3925
3926static void
3927dwarf2_create_include_psymtab (char *name, struct partial_symtab *pst,
3928 struct objfile *objfile)
3929{
3930 struct partial_symtab *subpst = allocate_psymtab (name, objfile);
3931
3932 subpst->section_offsets = pst->section_offsets;
3933 subpst->textlow = 0;
3934 subpst->texthigh = 0;
3935
3936 subpst->dependencies = (struct partial_symtab **)
3937 obstack_alloc (&objfile->objfile_obstack,
3938 sizeof (struct partial_symtab *));
3939 subpst->dependencies[0] = pst;
3940 subpst->number_of_dependencies = 1;
3941
3942 subpst->globals_offset = 0;
3943 subpst->n_global_syms = 0;
3944 subpst->statics_offset = 0;
3945 subpst->n_static_syms = 0;
3946 subpst->symtab = NULL;
3947 subpst->read_symtab = pst->read_symtab;
3948 subpst->readin = 0;
3949
3950 /* No private part is necessary for include psymtabs. This property
3951 can be used to differentiate between such include psymtabs and
10b3939b 3952 the regular ones. */
58a9656e 3953 subpst->read_symtab_private = NULL;
aaa75496
JB
3954}
3955
3956/* Read the Line Number Program data and extract the list of files
3957 included by the source file represented by PST. Build an include
d85a05f0 3958 partial symtab for each of these included files. */
aaa75496
JB
3959
3960static void
3961dwarf2_build_include_psymtabs (struct dwarf2_cu *cu,
dee91e82
DE
3962 struct die_info *die,
3963 struct partial_symtab *pst)
aaa75496 3964{
d85a05f0
DJ
3965 struct line_header *lh = NULL;
3966 struct attribute *attr;
aaa75496 3967
d85a05f0
DJ
3968 attr = dwarf2_attr (die, DW_AT_stmt_list, cu);
3969 if (attr)
3019eac3 3970 lh = dwarf_decode_line_header (DW_UNSND (attr), cu);
aaa75496
JB
3971 if (lh == NULL)
3972 return; /* No linetable, so no includes. */
3973
c6da4cef 3974 /* NOTE: pst->dirname is DW_AT_comp_dir (if present). */
f3f5162e 3975 dwarf_decode_lines (lh, pst->dirname, cu, pst, 1);
aaa75496
JB
3976
3977 free_line_header (lh);
3978}
3979
348e048f 3980static hashval_t
52dc124a 3981hash_signatured_type (const void *item)
348e048f 3982{
52dc124a 3983 const struct signatured_type *sig_type = item;
9a619af0 3984
348e048f 3985 /* This drops the top 32 bits of the signature, but is ok for a hash. */
52dc124a 3986 return sig_type->signature;
348e048f
DE
3987}
3988
3989static int
52dc124a 3990eq_signatured_type (const void *item_lhs, const void *item_rhs)
348e048f
DE
3991{
3992 const struct signatured_type *lhs = item_lhs;
3993 const struct signatured_type *rhs = item_rhs;
9a619af0 3994
348e048f
DE
3995 return lhs->signature == rhs->signature;
3996}
3997
1fd400ff
TT
3998/* Allocate a hash table for signatured types. */
3999
4000static htab_t
673bfd45 4001allocate_signatured_type_table (struct objfile *objfile)
1fd400ff
TT
4002{
4003 return htab_create_alloc_ex (41,
52dc124a
DE
4004 hash_signatured_type,
4005 eq_signatured_type,
1fd400ff
TT
4006 NULL,
4007 &objfile->objfile_obstack,
4008 hashtab_obstack_allocate,
4009 dummy_obstack_deallocate);
4010}
4011
d467dd73 4012/* A helper function to add a signatured type CU to a table. */
1fd400ff
TT
4013
4014static int
d467dd73 4015add_signatured_type_cu_to_table (void **slot, void *datum)
1fd400ff
TT
4016{
4017 struct signatured_type *sigt = *slot;
b4dd5633 4018 struct signatured_type ***datap = datum;
1fd400ff 4019
b4dd5633 4020 **datap = sigt;
1fd400ff
TT
4021 ++*datap;
4022
4023 return 1;
4024}
4025
3019eac3
DE
4026/* Create the hash table of all entries in the .debug_types section.
4027 DWO_FILE is a pointer to the DWO file for .debug_types.dwo, NULL otherwise.
4028 The result is a pointer to the hash table or NULL if there are
4029 no types. */
348e048f 4030
3019eac3
DE
4031static htab_t
4032create_debug_types_hash_table (struct dwo_file *dwo_file,
4033 VEC (dwarf2_section_info_def) *types)
348e048f 4034{
3019eac3 4035 struct objfile *objfile = dwarf2_per_objfile->objfile;
8b70b953 4036 htab_t types_htab = NULL;
8b70b953
TT
4037 int ix;
4038 struct dwarf2_section_info *section;
4bdcc0c1 4039 struct dwarf2_section_info *abbrev_section;
348e048f 4040
3019eac3
DE
4041 if (VEC_empty (dwarf2_section_info_def, types))
4042 return NULL;
348e048f 4043
4bdcc0c1
DE
4044 abbrev_section = (dwo_file != NULL
4045 ? &dwo_file->sections.abbrev
4046 : &dwarf2_per_objfile->abbrev);
4047
09406207
DE
4048 if (dwarf2_read_debug)
4049 fprintf_unfiltered (gdb_stdlog, "Reading .debug_types%s for %s:\n",
4050 dwo_file ? ".dwo" : "",
4051 bfd_get_filename (abbrev_section->asection->owner));
4052
8b70b953 4053 for (ix = 0;
3019eac3 4054 VEC_iterate (dwarf2_section_info_def, types, ix, section);
8b70b953
TT
4055 ++ix)
4056 {
3019eac3 4057 bfd *abfd;
8b70b953 4058 gdb_byte *info_ptr, *end_ptr;
36586728 4059 struct dwarf2_section_info *abbrev_section;
348e048f 4060
8b70b953
TT
4061 dwarf2_read_section (objfile, section);
4062 info_ptr = section->buffer;
348e048f 4063
8b70b953
TT
4064 if (info_ptr == NULL)
4065 continue;
348e048f 4066
3019eac3
DE
4067 /* We can't set abfd until now because the section may be empty or
4068 not present, in which case section->asection will be NULL. */
4069 abfd = section->asection->owner;
4070
36586728
TT
4071 if (dwo_file)
4072 abbrev_section = &dwo_file->sections.abbrev;
4073 else
4074 abbrev_section = &dwarf2_per_objfile->abbrev;
4075
8b70b953 4076 if (types_htab == NULL)
3019eac3
DE
4077 {
4078 if (dwo_file)
4079 types_htab = allocate_dwo_unit_table (objfile);
4080 else
4081 types_htab = allocate_signatured_type_table (objfile);
4082 }
348e048f 4083
dee91e82
DE
4084 /* We don't use init_cutu_and_read_dies_simple, or some such, here
4085 because we don't need to read any dies: the signature is in the
4086 header. */
8b70b953
TT
4087
4088 end_ptr = info_ptr + section->size;
4089 while (info_ptr < end_ptr)
4090 {
b64f50a1 4091 sect_offset offset;
3019eac3 4092 cu_offset type_offset_in_tu;
8b70b953 4093 ULONGEST signature;
52dc124a 4094 struct signatured_type *sig_type;
3019eac3 4095 struct dwo_unit *dwo_tu;
8b70b953
TT
4096 void **slot;
4097 gdb_byte *ptr = info_ptr;
9ff913ba 4098 struct comp_unit_head header;
dee91e82 4099 unsigned int length;
348e048f 4100
b64f50a1 4101 offset.sect_off = ptr - section->buffer;
348e048f 4102
8b70b953 4103 /* We need to read the type's signature in order to build the hash
9ff913ba 4104 table, but we don't need anything else just yet. */
348e048f 4105
4bdcc0c1
DE
4106 ptr = read_and_check_type_unit_head (&header, section,
4107 abbrev_section, ptr,
3019eac3 4108 &signature, &type_offset_in_tu);
6caca83c 4109
1ce1cefd 4110 length = get_cu_length (&header);
dee91e82 4111
6caca83c 4112 /* Skip dummy type units. */
dee91e82
DE
4113 if (ptr >= info_ptr + length
4114 || peek_abbrev_code (abfd, ptr) == 0)
6caca83c 4115 {
1ce1cefd 4116 info_ptr += length;
6caca83c
CC
4117 continue;
4118 }
8b70b953 4119
3019eac3
DE
4120 if (dwo_file)
4121 {
4122 sig_type = NULL;
4123 dwo_tu = OBSTACK_ZALLOC (&objfile->objfile_obstack,
4124 struct dwo_unit);
4125 dwo_tu->dwo_file = dwo_file;
4126 dwo_tu->signature = signature;
4127 dwo_tu->type_offset_in_tu = type_offset_in_tu;
4128 dwo_tu->info_or_types_section = section;
4129 dwo_tu->offset = offset;
4130 dwo_tu->length = length;
4131 }
4132 else
4133 {
4134 /* N.B.: type_offset is not usable if this type uses a DWO file.
4135 The real type_offset is in the DWO file. */
4136 dwo_tu = NULL;
4137 sig_type = OBSTACK_ZALLOC (&objfile->objfile_obstack,
4138 struct signatured_type);
4139 sig_type->signature = signature;
4140 sig_type->type_offset_in_tu = type_offset_in_tu;
4141 sig_type->per_cu.objfile = objfile;
4142 sig_type->per_cu.is_debug_types = 1;
4143 sig_type->per_cu.info_or_types_section = section;
4144 sig_type->per_cu.offset = offset;
4145 sig_type->per_cu.length = length;
4146 }
8b70b953 4147
3019eac3
DE
4148 slot = htab_find_slot (types_htab,
4149 dwo_file ? (void*) dwo_tu : (void *) sig_type,
4150 INSERT);
8b70b953
TT
4151 gdb_assert (slot != NULL);
4152 if (*slot != NULL)
4153 {
3019eac3
DE
4154 sect_offset dup_offset;
4155
4156 if (dwo_file)
4157 {
4158 const struct dwo_unit *dup_tu = *slot;
4159
4160 dup_offset = dup_tu->offset;
4161 }
4162 else
4163 {
4164 const struct signatured_type *dup_tu = *slot;
4165
4166 dup_offset = dup_tu->per_cu.offset;
4167 }
b3c8eb43 4168
8b70b953
TT
4169 complaint (&symfile_complaints,
4170 _("debug type entry at offset 0x%x is duplicate to the "
4171 "entry at offset 0x%x, signature 0x%s"),
3019eac3 4172 offset.sect_off, dup_offset.sect_off,
8b70b953 4173 phex (signature, sizeof (signature)));
8b70b953 4174 }
3019eac3 4175 *slot = dwo_file ? (void *) dwo_tu : (void *) sig_type;
348e048f 4176
09406207 4177 if (dwarf2_read_debug)
8b70b953 4178 fprintf_unfiltered (gdb_stdlog, " offset 0x%x, signature 0x%s\n",
b64f50a1
JK
4179 offset.sect_off,
4180 phex (signature, sizeof (signature)));
348e048f 4181
dee91e82 4182 info_ptr += length;
8b70b953 4183 }
348e048f
DE
4184 }
4185
3019eac3
DE
4186 return types_htab;
4187}
4188
4189/* Create the hash table of all entries in the .debug_types section,
4190 and initialize all_type_units.
4191 The result is zero if there is an error (e.g. missing .debug_types section),
4192 otherwise non-zero. */
4193
4194static int
4195create_all_type_units (struct objfile *objfile)
4196{
4197 htab_t types_htab;
b4dd5633 4198 struct signatured_type **iter;
3019eac3
DE
4199
4200 types_htab = create_debug_types_hash_table (NULL, dwarf2_per_objfile->types);
4201 if (types_htab == NULL)
4202 {
4203 dwarf2_per_objfile->signatured_types = NULL;
4204 return 0;
4205 }
4206
348e048f
DE
4207 dwarf2_per_objfile->signatured_types = types_htab;
4208
d467dd73
DE
4209 dwarf2_per_objfile->n_type_units = htab_elements (types_htab);
4210 dwarf2_per_objfile->all_type_units
1fd400ff 4211 = obstack_alloc (&objfile->objfile_obstack,
d467dd73 4212 dwarf2_per_objfile->n_type_units
b4dd5633 4213 * sizeof (struct signatured_type *));
d467dd73
DE
4214 iter = &dwarf2_per_objfile->all_type_units[0];
4215 htab_traverse_noresize (types_htab, add_signatured_type_cu_to_table, &iter);
4216 gdb_assert (iter - &dwarf2_per_objfile->all_type_units[0]
4217 == dwarf2_per_objfile->n_type_units);
1fd400ff 4218
348e048f
DE
4219 return 1;
4220}
4221
380bca97 4222/* Lookup a signature based type for DW_FORM_ref_sig8.
e319fa28 4223 Returns NULL if signature SIG is not present in the table. */
348e048f
DE
4224
4225static struct signatured_type *
e319fa28 4226lookup_signatured_type (ULONGEST sig)
348e048f
DE
4227{
4228 struct signatured_type find_entry, *entry;
4229
4230 if (dwarf2_per_objfile->signatured_types == NULL)
4231 {
4232 complaint (&symfile_complaints,
55f1336d 4233 _("missing `.debug_types' section for DW_FORM_ref_sig8 die"));
dcc07052 4234 return NULL;
348e048f
DE
4235 }
4236
4237 find_entry.signature = sig;
4238 entry = htab_find (dwarf2_per_objfile->signatured_types, &find_entry);
4239 return entry;
4240}
42e7ad6c
DE
4241\f
4242/* Low level DIE reading support. */
348e048f 4243
d85a05f0
DJ
4244/* Initialize a die_reader_specs struct from a dwarf2_cu struct. */
4245
4246static void
4247init_cu_die_reader (struct die_reader_specs *reader,
dee91e82 4248 struct dwarf2_cu *cu,
3019eac3
DE
4249 struct dwarf2_section_info *section,
4250 struct dwo_file *dwo_file)
d85a05f0 4251{
fceca515 4252 gdb_assert (section->readin && section->buffer != NULL);
dee91e82 4253 reader->abfd = section->asection->owner;
d85a05f0 4254 reader->cu = cu;
3019eac3 4255 reader->dwo_file = dwo_file;
dee91e82
DE
4256 reader->die_section = section;
4257 reader->buffer = section->buffer;
f664829e 4258 reader->buffer_end = section->buffer + section->size;
d85a05f0
DJ
4259}
4260
fd820528 4261/* Initialize a CU (or TU) and read its DIEs.
3019eac3 4262 If the CU defers to a DWO file, read the DWO file as well.
dee91e82 4263
f4dc4d17
DE
4264 ABBREV_TABLE, if non-NULL, is the abbreviation table to use.
4265 Otherwise the table specified in the comp unit header is read in and used.
4266 This is an optimization for when we already have the abbrev table.
4267
dee91e82
DE
4268 If USE_EXISTING_CU is non-zero, and THIS_CU->cu is non-NULL, then use it.
4269 Otherwise, a new CU is allocated with xmalloc.
4270
4271 If KEEP is non-zero, then if we allocated a dwarf2_cu we add it to
4272 read_in_chain. Otherwise the dwarf2_cu data is freed at the end.
4273
4274 WARNING: If THIS_CU is a "dummy CU" (used as filler by the incremental
fd820528 4275 linker) then DIE_READER_FUNC will not get called. */
aaa75496 4276
70221824 4277static void
fd820528 4278init_cutu_and_read_dies (struct dwarf2_per_cu_data *this_cu,
f4dc4d17 4279 struct abbrev_table *abbrev_table,
fd820528
DE
4280 int use_existing_cu, int keep,
4281 die_reader_func_ftype *die_reader_func,
4282 void *data)
c906108c 4283{
dee91e82 4284 struct objfile *objfile = dwarf2_per_objfile->objfile;
3019eac3
DE
4285 struct dwarf2_section_info *section = this_cu->info_or_types_section;
4286 bfd *abfd = section->asection->owner;
dee91e82
DE
4287 struct dwarf2_cu *cu;
4288 gdb_byte *begin_info_ptr, *info_ptr;
4289 struct die_reader_specs reader;
d85a05f0 4290 struct die_info *comp_unit_die;
dee91e82 4291 int has_children;
d85a05f0 4292 struct attribute *attr;
dee91e82
DE
4293 struct cleanup *cleanups, *free_cu_cleanup = NULL;
4294 struct signatured_type *sig_type = NULL;
4bdcc0c1 4295 struct dwarf2_section_info *abbrev_section;
42e7ad6c
DE
4296 /* Non-zero if CU currently points to a DWO file and we need to
4297 reread it. When this happens we need to reread the skeleton die
4298 before we can reread the DWO file. */
4299 int rereading_dwo_cu = 0;
c906108c 4300
09406207
DE
4301 if (dwarf2_die_debug)
4302 fprintf_unfiltered (gdb_stdlog, "Reading %s unit at offset 0x%x\n",
4303 this_cu->is_debug_types ? "type" : "comp",
4304 this_cu->offset.sect_off);
4305
dee91e82
DE
4306 if (use_existing_cu)
4307 gdb_assert (keep);
23745b47 4308
dee91e82
DE
4309 cleanups = make_cleanup (null_cleanup, NULL);
4310
4311 /* This is cheap if the section is already read in. */
4312 dwarf2_read_section (objfile, section);
4313
4314 begin_info_ptr = info_ptr = section->buffer + this_cu->offset.sect_off;
36586728
TT
4315
4316 abbrev_section = get_abbrev_section_for_cu (this_cu);
dee91e82
DE
4317
4318 if (use_existing_cu && this_cu->cu != NULL)
4319 {
4320 cu = this_cu->cu;
42e7ad6c
DE
4321
4322 /* If this CU is from a DWO file we need to start over, we need to
4323 refetch the attributes from the skeleton CU.
4324 This could be optimized by retrieving those attributes from when we
4325 were here the first time: the previous comp_unit_die was stored in
4326 comp_unit_obstack. But there's no data yet that we need this
4327 optimization. */
4328 if (cu->dwo_unit != NULL)
4329 rereading_dwo_cu = 1;
dee91e82
DE
4330 }
4331 else
4332 {
4333 /* If !use_existing_cu, this_cu->cu must be NULL. */
4334 gdb_assert (this_cu->cu == NULL);
4335
4336 cu = xmalloc (sizeof (*cu));
4337 init_one_comp_unit (cu, this_cu);
4338
4339 /* If an error occurs while loading, release our storage. */
4340 free_cu_cleanup = make_cleanup (free_heap_comp_unit, cu);
42e7ad6c 4341 }
dee91e82 4342
42e7ad6c
DE
4343 if (cu->header.first_die_offset.cu_off != 0 && ! rereading_dwo_cu)
4344 {
4345 /* We already have the header, there's no need to read it in again. */
4346 info_ptr += cu->header.first_die_offset.cu_off;
4347 }
4348 else
4349 {
3019eac3 4350 if (this_cu->is_debug_types)
dee91e82
DE
4351 {
4352 ULONGEST signature;
42e7ad6c 4353 cu_offset type_offset_in_tu;
dee91e82 4354
4bdcc0c1
DE
4355 info_ptr = read_and_check_type_unit_head (&cu->header, section,
4356 abbrev_section, info_ptr,
42e7ad6c
DE
4357 &signature,
4358 &type_offset_in_tu);
dee91e82 4359
42e7ad6c
DE
4360 /* Since per_cu is the first member of struct signatured_type,
4361 we can go from a pointer to one to a pointer to the other. */
4362 sig_type = (struct signatured_type *) this_cu;
4363 gdb_assert (sig_type->signature == signature);
4364 gdb_assert (sig_type->type_offset_in_tu.cu_off
4365 == type_offset_in_tu.cu_off);
dee91e82
DE
4366 gdb_assert (this_cu->offset.sect_off == cu->header.offset.sect_off);
4367
42e7ad6c
DE
4368 /* LENGTH has not been set yet for type units if we're
4369 using .gdb_index. */
1ce1cefd 4370 this_cu->length = get_cu_length (&cu->header);
3019eac3
DE
4371
4372 /* Establish the type offset that can be used to lookup the type. */
4373 sig_type->type_offset_in_section.sect_off =
4374 this_cu->offset.sect_off + sig_type->type_offset_in_tu.cu_off;
dee91e82
DE
4375 }
4376 else
4377 {
4bdcc0c1
DE
4378 info_ptr = read_and_check_comp_unit_head (&cu->header, section,
4379 abbrev_section,
4380 info_ptr, 0);
dee91e82
DE
4381
4382 gdb_assert (this_cu->offset.sect_off == cu->header.offset.sect_off);
1ce1cefd 4383 gdb_assert (this_cu->length == get_cu_length (&cu->header));
dee91e82
DE
4384 }
4385 }
10b3939b 4386
6caca83c 4387 /* Skip dummy compilation units. */
dee91e82 4388 if (info_ptr >= begin_info_ptr + this_cu->length
6caca83c
CC
4389 || peek_abbrev_code (abfd, info_ptr) == 0)
4390 {
dee91e82 4391 do_cleanups (cleanups);
21b2bd31 4392 return;
6caca83c
CC
4393 }
4394
433df2d4
DE
4395 /* If we don't have them yet, read the abbrevs for this compilation unit.
4396 And if we need to read them now, make sure they're freed when we're
42e7ad6c
DE
4397 done. Note that it's important that if the CU had an abbrev table
4398 on entry we don't free it when we're done: Somewhere up the call stack
4399 it may be in use. */
f4dc4d17
DE
4400 if (abbrev_table != NULL)
4401 {
4402 gdb_assert (cu->abbrev_table == NULL);
4403 gdb_assert (cu->header.abbrev_offset.sect_off
4404 == abbrev_table->offset.sect_off);
4405 cu->abbrev_table = abbrev_table;
4406 }
4407 else if (cu->abbrev_table == NULL)
dee91e82 4408 {
4bdcc0c1 4409 dwarf2_read_abbrevs (cu, abbrev_section);
dee91e82
DE
4410 make_cleanup (dwarf2_free_abbrev_table, cu);
4411 }
42e7ad6c
DE
4412 else if (rereading_dwo_cu)
4413 {
4414 dwarf2_free_abbrev_table (cu);
4415 dwarf2_read_abbrevs (cu, abbrev_section);
4416 }
af703f96 4417
dee91e82 4418 /* Read the top level CU/TU die. */
3019eac3 4419 init_cu_die_reader (&reader, cu, section, NULL);
dee91e82 4420 info_ptr = read_full_die (&reader, &comp_unit_die, info_ptr, &has_children);
93311388 4421
3019eac3
DE
4422 /* If we have a DWO stub, process it and then read in the DWO file.
4423 Note that if USE_EXISTING_OK != 0, and THIS_CU->cu already contains
4424 a DWO CU, that this test will fail. */
4425 attr = dwarf2_attr (comp_unit_die, DW_AT_GNU_dwo_name, cu);
4426 if (attr)
4427 {
4428 char *dwo_name = DW_STRING (attr);
42e7ad6c 4429 const char *comp_dir_string;
3019eac3
DE
4430 struct dwo_unit *dwo_unit;
4431 ULONGEST signature; /* Or dwo_id. */
42e7ad6c 4432 struct attribute *comp_dir, *stmt_list, *low_pc, *high_pc, *ranges;
3019eac3 4433 int i,num_extra_attrs;
4bdcc0c1 4434 struct dwarf2_section_info *dwo_abbrev_section;
3019eac3
DE
4435
4436 if (has_children)
4437 error (_("Dwarf Error: compilation unit with DW_AT_GNU_dwo_name"
4438 " has children (offset 0x%x) [in module %s]"),
4439 this_cu->offset.sect_off, bfd_get_filename (abfd));
4440
4441 /* These attributes aren't processed until later:
4442 DW_AT_stmt_list, DW_AT_low_pc, DW_AT_high_pc, DW_AT_ranges.
4443 However, the attribute is found in the stub which we won't have later.
4444 In order to not impose this complication on the rest of the code,
4445 we read them here and copy them to the DWO CU/TU die. */
3019eac3
DE
4446
4447 /* For TUs in DWO files, the DW_AT_stmt_list attribute lives in the
4448 DWO file. */
42e7ad6c 4449 stmt_list = NULL;
3019eac3
DE
4450 if (! this_cu->is_debug_types)
4451 stmt_list = dwarf2_attr (comp_unit_die, DW_AT_stmt_list, cu);
4452 low_pc = dwarf2_attr (comp_unit_die, DW_AT_low_pc, cu);
4453 high_pc = dwarf2_attr (comp_unit_die, DW_AT_high_pc, cu);
4454 ranges = dwarf2_attr (comp_unit_die, DW_AT_ranges, cu);
42e7ad6c 4455 comp_dir = dwarf2_attr (comp_unit_die, DW_AT_comp_dir, cu);
3019eac3
DE
4456
4457 /* There should be a DW_AT_addr_base attribute here (if needed).
4458 We need the value before we can process DW_FORM_GNU_addr_index. */
4459 cu->addr_base = 0;
3019eac3
DE
4460 attr = dwarf2_attr (comp_unit_die, DW_AT_GNU_addr_base, cu);
4461 if (attr)
2e3cf129
DE
4462 cu->addr_base = DW_UNSND (attr);
4463
4464 /* There should be a DW_AT_ranges_base attribute here (if needed).
4465 We need the value before we can process DW_AT_ranges. */
4466 cu->ranges_base = 0;
4467 attr = dwarf2_attr (comp_unit_die, DW_AT_GNU_ranges_base, cu);
4468 if (attr)
4469 cu->ranges_base = DW_UNSND (attr);
3019eac3
DE
4470
4471 if (this_cu->is_debug_types)
4472 {
4473 gdb_assert (sig_type != NULL);
4474 signature = sig_type->signature;
4475 }
4476 else
4477 {
4478 attr = dwarf2_attr (comp_unit_die, DW_AT_GNU_dwo_id, cu);
4479 if (! attr)
4480 error (_("Dwarf Error: missing dwo_id [in module %s]"),
4481 dwo_name);
4482 signature = DW_UNSND (attr);
4483 }
4484
4485 /* We may need the comp_dir in order to find the DWO file. */
42e7ad6c
DE
4486 comp_dir_string = NULL;
4487 if (comp_dir)
4488 comp_dir_string = DW_STRING (comp_dir);
3019eac3
DE
4489
4490 if (this_cu->is_debug_types)
42e7ad6c 4491 dwo_unit = lookup_dwo_type_unit (sig_type, dwo_name, comp_dir_string);
3019eac3 4492 else
42e7ad6c 4493 dwo_unit = lookup_dwo_comp_unit (this_cu, dwo_name, comp_dir_string,
3019eac3
DE
4494 signature);
4495
4496 if (dwo_unit == NULL)
4497 {
4498 error (_("Dwarf Error: CU at offset 0x%x references unknown DWO"
4499 " with ID %s [in module %s]"),
4500 this_cu->offset.sect_off,
4501 phex (signature, sizeof (signature)),
4502 objfile->name);
4503 }
4504
4505 /* Set up for reading the DWO CU/TU. */
4506 cu->dwo_unit = dwo_unit;
4507 section = dwo_unit->info_or_types_section;
4508 begin_info_ptr = info_ptr = section->buffer + dwo_unit->offset.sect_off;
4bdcc0c1 4509 dwo_abbrev_section = &dwo_unit->dwo_file->sections.abbrev;
3019eac3
DE
4510 init_cu_die_reader (&reader, cu, section, dwo_unit->dwo_file);
4511
4512 if (this_cu->is_debug_types)
4513 {
4514 ULONGEST signature;
4515
4bdcc0c1
DE
4516 info_ptr = read_and_check_type_unit_head (&cu->header, section,
4517 dwo_abbrev_section,
4518 info_ptr,
3019eac3
DE
4519 &signature, NULL);
4520 gdb_assert (sig_type->signature == signature);
4521 gdb_assert (dwo_unit->offset.sect_off == cu->header.offset.sect_off);
1ce1cefd 4522 gdb_assert (dwo_unit->length == get_cu_length (&cu->header));
3019eac3
DE
4523
4524 /* Establish the type offset that can be used to lookup the type.
4525 For DWO files, we don't know it until now. */
4526 sig_type->type_offset_in_section.sect_off =
4527 dwo_unit->offset.sect_off + dwo_unit->type_offset_in_tu.cu_off;
4528 }
4529 else
4530 {
4bdcc0c1
DE
4531 info_ptr = read_and_check_comp_unit_head (&cu->header, section,
4532 dwo_abbrev_section,
4533 info_ptr, 0);
3019eac3 4534 gdb_assert (dwo_unit->offset.sect_off == cu->header.offset.sect_off);
1ce1cefd 4535 gdb_assert (dwo_unit->length == get_cu_length (&cu->header));
3019eac3
DE
4536 }
4537
4538 /* Discard the original CU's abbrev table, and read the DWO's. */
f4dc4d17
DE
4539 if (abbrev_table == NULL)
4540 {
4541 dwarf2_free_abbrev_table (cu);
4542 dwarf2_read_abbrevs (cu, dwo_abbrev_section);
4543 }
4544 else
4545 {
4546 dwarf2_read_abbrevs (cu, dwo_abbrev_section);
4547 make_cleanup (dwarf2_free_abbrev_table, cu);
4548 }
3019eac3
DE
4549
4550 /* Read in the die, but leave space to copy over the attributes
4551 from the stub. This has the benefit of simplifying the rest of
4552 the code - all the real work is done here. */
4553 num_extra_attrs = ((stmt_list != NULL)
4554 + (low_pc != NULL)
4555 + (high_pc != NULL)
42e7ad6c
DE
4556 + (ranges != NULL)
4557 + (comp_dir != NULL));
3019eac3
DE
4558 info_ptr = read_full_die_1 (&reader, &comp_unit_die, info_ptr,
4559 &has_children, num_extra_attrs);
4560
4561 /* Copy over the attributes from the stub to the DWO die. */
4562 i = comp_unit_die->num_attrs;
4563 if (stmt_list != NULL)
4564 comp_unit_die->attrs[i++] = *stmt_list;
4565 if (low_pc != NULL)
4566 comp_unit_die->attrs[i++] = *low_pc;
4567 if (high_pc != NULL)
4568 comp_unit_die->attrs[i++] = *high_pc;
4569 if (ranges != NULL)
4570 comp_unit_die->attrs[i++] = *ranges;
42e7ad6c
DE
4571 if (comp_dir != NULL)
4572 comp_unit_die->attrs[i++] = *comp_dir;
3019eac3
DE
4573 comp_unit_die->num_attrs += num_extra_attrs;
4574
4575 /* Skip dummy compilation units. */
4576 if (info_ptr >= begin_info_ptr + dwo_unit->length
4577 || peek_abbrev_code (abfd, info_ptr) == 0)
4578 {
4579 do_cleanups (cleanups);
4580 return;
4581 }
4582 }
4583
dee91e82
DE
4584 die_reader_func (&reader, info_ptr, comp_unit_die, has_children, data);
4585
4586 if (free_cu_cleanup != NULL)
348e048f 4587 {
dee91e82
DE
4588 if (keep)
4589 {
4590 /* We've successfully allocated this compilation unit. Let our
4591 caller clean it up when finished with it. */
4592 discard_cleanups (free_cu_cleanup);
4593
4594 /* We can only discard free_cu_cleanup and all subsequent cleanups.
4595 So we have to manually free the abbrev table. */
4596 dwarf2_free_abbrev_table (cu);
4597
4598 /* Link this CU into read_in_chain. */
4599 this_cu->cu->read_in_chain = dwarf2_per_objfile->read_in_chain;
4600 dwarf2_per_objfile->read_in_chain = this_cu;
4601 }
4602 else
4603 do_cleanups (free_cu_cleanup);
348e048f 4604 }
dee91e82
DE
4605
4606 do_cleanups (cleanups);
4607}
4608
3019eac3
DE
4609/* Read CU/TU THIS_CU in section SECTION,
4610 but do not follow DW_AT_GNU_dwo_name if present.
4611 DWO_FILE, if non-NULL, is the DWO file to read (the caller is assumed to
4612 have already done the lookup to find the DWO file).
dee91e82
DE
4613
4614 The caller is required to fill in THIS_CU->section, THIS_CU->offset, and
3019eac3 4615 THIS_CU->is_debug_types, but nothing else.
dee91e82
DE
4616
4617 We fill in THIS_CU->length.
4618
4619 WARNING: If THIS_CU is a "dummy CU" (used as filler by the incremental
4620 linker) then DIE_READER_FUNC will not get called.
4621
4622 THIS_CU->cu is always freed when done.
3019eac3
DE
4623 This is done in order to not leave THIS_CU->cu in a state where we have
4624 to care whether it refers to the "main" CU or the DWO CU. */
dee91e82
DE
4625
4626static void
4627init_cutu_and_read_dies_no_follow (struct dwarf2_per_cu_data *this_cu,
4628 struct dwarf2_section_info *abbrev_section,
3019eac3 4629 struct dwo_file *dwo_file,
dee91e82
DE
4630 die_reader_func_ftype *die_reader_func,
4631 void *data)
4632{
4633 struct objfile *objfile = dwarf2_per_objfile->objfile;
3019eac3
DE
4634 struct dwarf2_section_info *section = this_cu->info_or_types_section;
4635 bfd *abfd = section->asection->owner;
dee91e82
DE
4636 struct dwarf2_cu cu;
4637 gdb_byte *begin_info_ptr, *info_ptr;
4638 struct die_reader_specs reader;
4639 struct cleanup *cleanups;
4640 struct die_info *comp_unit_die;
4641 int has_children;
4642
09406207
DE
4643 if (dwarf2_die_debug)
4644 fprintf_unfiltered (gdb_stdlog, "Reading %s unit at offset 0x%x\n",
4645 this_cu->is_debug_types ? "type" : "comp",
4646 this_cu->offset.sect_off);
4647
dee91e82
DE
4648 gdb_assert (this_cu->cu == NULL);
4649
dee91e82
DE
4650 /* This is cheap if the section is already read in. */
4651 dwarf2_read_section (objfile, section);
4652
4653 init_one_comp_unit (&cu, this_cu);
4654
4655 cleanups = make_cleanup (free_stack_comp_unit, &cu);
4656
4657 begin_info_ptr = info_ptr = section->buffer + this_cu->offset.sect_off;
4bdcc0c1
DE
4658 info_ptr = read_and_check_comp_unit_head (&cu.header, section,
4659 abbrev_section, info_ptr,
3019eac3 4660 this_cu->is_debug_types);
dee91e82 4661
1ce1cefd 4662 this_cu->length = get_cu_length (&cu.header);
dee91e82
DE
4663
4664 /* Skip dummy compilation units. */
4665 if (info_ptr >= begin_info_ptr + this_cu->length
4666 || peek_abbrev_code (abfd, info_ptr) == 0)
c906108c 4667 {
dee91e82 4668 do_cleanups (cleanups);
21b2bd31 4669 return;
93311388 4670 }
72bf9492 4671
dee91e82
DE
4672 dwarf2_read_abbrevs (&cu, abbrev_section);
4673 make_cleanup (dwarf2_free_abbrev_table, &cu);
4674
3019eac3 4675 init_cu_die_reader (&reader, &cu, section, dwo_file);
dee91e82
DE
4676 info_ptr = read_full_die (&reader, &comp_unit_die, info_ptr, &has_children);
4677
4678 die_reader_func (&reader, info_ptr, comp_unit_die, has_children, data);
4679
4680 do_cleanups (cleanups);
4681}
4682
3019eac3
DE
4683/* Read a CU/TU, except that this does not look for DW_AT_GNU_dwo_name and
4684 does not lookup the specified DWO file.
4685 This cannot be used to read DWO files.
dee91e82
DE
4686
4687 THIS_CU->cu is always freed when done.
3019eac3
DE
4688 This is done in order to not leave THIS_CU->cu in a state where we have
4689 to care whether it refers to the "main" CU or the DWO CU.
4690 We can revisit this if the data shows there's a performance issue. */
dee91e82
DE
4691
4692static void
4693init_cutu_and_read_dies_simple (struct dwarf2_per_cu_data *this_cu,
4694 die_reader_func_ftype *die_reader_func,
4695 void *data)
4696{
4697 init_cutu_and_read_dies_no_follow (this_cu,
36586728 4698 get_abbrev_section_for_cu (this_cu),
3019eac3 4699 NULL,
dee91e82
DE
4700 die_reader_func, data);
4701}
4702
f4dc4d17
DE
4703/* Create a psymtab named NAME and assign it to PER_CU.
4704
4705 The caller must fill in the following details:
4706 dirname, textlow, texthigh. */
4707
4708static struct partial_symtab *
4709create_partial_symtab (struct dwarf2_per_cu_data *per_cu, const char *name)
4710{
4711 struct objfile *objfile = per_cu->objfile;
4712 struct partial_symtab *pst;
4713
4714 pst = start_psymtab_common (objfile, objfile->section_offsets,
4715 name, 0,
4716 objfile->global_psymbols.next,
4717 objfile->static_psymbols.next);
4718
4719 pst->psymtabs_addrmap_supported = 1;
4720
4721 /* This is the glue that links PST into GDB's symbol API. */
4722 pst->read_symtab_private = per_cu;
4723 pst->read_symtab = dwarf2_psymtab_to_symtab;
4724 per_cu->v.psymtab = pst;
4725
4726 return pst;
4727}
4728
dee91e82
DE
4729/* die_reader_func for process_psymtab_comp_unit. */
4730
4731static void
4732process_psymtab_comp_unit_reader (const struct die_reader_specs *reader,
4733 gdb_byte *info_ptr,
4734 struct die_info *comp_unit_die,
4735 int has_children,
4736 void *data)
4737{
4738 struct dwarf2_cu *cu = reader->cu;
4739 struct objfile *objfile = cu->objfile;
4740 struct dwarf2_per_cu_data *per_cu = cu->per_cu;
dee91e82
DE
4741 struct attribute *attr;
4742 CORE_ADDR baseaddr;
4743 CORE_ADDR best_lowpc = 0, best_highpc = 0;
4744 struct partial_symtab *pst;
4745 int has_pc_info;
4746 const char *filename;
95554aad 4747 int *want_partial_unit_ptr = data;
dee91e82 4748
95554aad
TT
4749 if (comp_unit_die->tag == DW_TAG_partial_unit
4750 && (want_partial_unit_ptr == NULL
4751 || !*want_partial_unit_ptr))
dee91e82
DE
4752 return;
4753
f4dc4d17
DE
4754 gdb_assert (! per_cu->is_debug_types);
4755
95554aad 4756 prepare_one_comp_unit (cu, comp_unit_die, language_minimal);
dee91e82
DE
4757
4758 cu->list_in_scope = &file_symbols;
c906108c 4759
93311388 4760 /* Allocate a new partial symbol table structure. */
dee91e82 4761 attr = dwarf2_attr (comp_unit_die, DW_AT_name, cu);
3e2a0cee
TT
4762 if (attr == NULL || !DW_STRING (attr))
4763 filename = "";
4764 else
4765 filename = DW_STRING (attr);
72bf9492 4766
f4dc4d17
DE
4767 pst = create_partial_symtab (per_cu, filename);
4768
4769 /* This must be done before calling dwarf2_build_include_psymtabs. */
dee91e82 4770 attr = dwarf2_attr (comp_unit_die, DW_AT_comp_dir, cu);
d85a05f0
DJ
4771 if (attr != NULL)
4772 pst->dirname = DW_STRING (attr);
72bf9492 4773
93311388 4774 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
e7c27a73 4775
dee91e82 4776 dwarf2_find_base_address (comp_unit_die, cu);
d85a05f0 4777
93311388
DE
4778 /* Possibly set the default values of LOWPC and HIGHPC from
4779 `DW_AT_ranges'. */
d85a05f0 4780 has_pc_info = dwarf2_get_pc_bounds (comp_unit_die, &best_lowpc,
dee91e82 4781 &best_highpc, cu, pst);
d85a05f0 4782 if (has_pc_info == 1 && best_lowpc < best_highpc)
93311388
DE
4783 /* Store the contiguous range if it is not empty; it can be empty for
4784 CUs with no code. */
4785 addrmap_set_empty (objfile->psymtabs_addrmap,
d85a05f0
DJ
4786 best_lowpc + baseaddr,
4787 best_highpc + baseaddr - 1, pst);
93311388
DE
4788
4789 /* Check if comp unit has_children.
4790 If so, read the rest of the partial symbols from this comp unit.
0963b4bd 4791 If not, there's no more debug_info for this comp unit. */
d85a05f0 4792 if (has_children)
93311388
DE
4793 {
4794 struct partial_die_info *first_die;
4795 CORE_ADDR lowpc, highpc;
31ffec48 4796
93311388
DE
4797 lowpc = ((CORE_ADDR) -1);
4798 highpc = ((CORE_ADDR) 0);
c906108c 4799
dee91e82 4800 first_die = load_partial_dies (reader, info_ptr, 1);
c906108c 4801
93311388 4802 scan_partial_symbols (first_die, &lowpc, &highpc,
dee91e82 4803 ! has_pc_info, cu);
57c22c6c 4804
93311388
DE
4805 /* If we didn't find a lowpc, set it to highpc to avoid
4806 complaints from `maint check'. */
4807 if (lowpc == ((CORE_ADDR) -1))
4808 lowpc = highpc;
10b3939b 4809
93311388
DE
4810 /* If the compilation unit didn't have an explicit address range,
4811 then use the information extracted from its child dies. */
d85a05f0 4812 if (! has_pc_info)
93311388 4813 {
d85a05f0
DJ
4814 best_lowpc = lowpc;
4815 best_highpc = highpc;
93311388
DE
4816 }
4817 }
d85a05f0
DJ
4818 pst->textlow = best_lowpc + baseaddr;
4819 pst->texthigh = best_highpc + baseaddr;
c906108c 4820
93311388
DE
4821 pst->n_global_syms = objfile->global_psymbols.next -
4822 (objfile->global_psymbols.list + pst->globals_offset);
4823 pst->n_static_syms = objfile->static_psymbols.next -
4824 (objfile->static_psymbols.list + pst->statics_offset);
4825 sort_pst_symbols (pst);
c906108c 4826
f4dc4d17 4827 if (!VEC_empty (dwarf2_per_cu_ptr, cu->per_cu->s.imported_symtabs))
95554aad
TT
4828 {
4829 int i;
f4dc4d17 4830 int len = VEC_length (dwarf2_per_cu_ptr, cu->per_cu->s.imported_symtabs);
95554aad
TT
4831 struct dwarf2_per_cu_data *iter;
4832
4833 /* Fill in 'dependencies' here; we fill in 'users' in a
4834 post-pass. */
4835 pst->number_of_dependencies = len;
4836 pst->dependencies = obstack_alloc (&objfile->objfile_obstack,
4837 len * sizeof (struct symtab *));
4838 for (i = 0;
f4dc4d17 4839 VEC_iterate (dwarf2_per_cu_ptr, cu->per_cu->s.imported_symtabs,
95554aad
TT
4840 i, iter);
4841 ++i)
4842 pst->dependencies[i] = iter->v.psymtab;
4843
f4dc4d17 4844 VEC_free (dwarf2_per_cu_ptr, cu->per_cu->s.imported_symtabs);
95554aad
TT
4845 }
4846
f4dc4d17
DE
4847 /* Get the list of files included in the current compilation unit,
4848 and build a psymtab for each of them. */
4849 dwarf2_build_include_psymtabs (cu, comp_unit_die, pst);
09406207
DE
4850
4851 if (dwarf2_read_debug)
4852 {
4853 struct gdbarch *gdbarch = get_objfile_arch (objfile);
4854
4855 fprintf_unfiltered (gdb_stdlog,
4856 "Psymtab for %s unit @0x%x: 0x%s - 0x%s"
4857 ", %d global, %d static syms\n",
4858 per_cu->is_debug_types ? "type" : "comp",
4859 per_cu->offset.sect_off,
4860 paddress (gdbarch, pst->textlow),
4861 paddress (gdbarch, pst->texthigh),
4862 pst->n_global_syms, pst->n_static_syms);
4863 }
dee91e82 4864}
ae038cb0 4865
dee91e82
DE
4866/* Subroutine of dwarf2_build_psymtabs_hard to simplify it.
4867 Process compilation unit THIS_CU for a psymtab. */
4868
4869static void
95554aad
TT
4870process_psymtab_comp_unit (struct dwarf2_per_cu_data *this_cu,
4871 int want_partial_unit)
dee91e82
DE
4872{
4873 /* If this compilation unit was already read in, free the
4874 cached copy in order to read it in again. This is
4875 necessary because we skipped some symbols when we first
4876 read in the compilation unit (see load_partial_dies).
4877 This problem could be avoided, but the benefit is unclear. */
4878 if (this_cu->cu != NULL)
4879 free_one_cached_comp_unit (this_cu);
4880
3019eac3 4881 gdb_assert (! this_cu->is_debug_types);
f4dc4d17
DE
4882 init_cutu_and_read_dies (this_cu, NULL, 0, 0,
4883 process_psymtab_comp_unit_reader,
95554aad 4884 &want_partial_unit);
dee91e82
DE
4885
4886 /* Age out any secondary CUs. */
4887 age_cached_comp_units ();
93311388 4888}
ff013f42 4889
f4dc4d17
DE
4890static hashval_t
4891hash_type_unit_group (const void *item)
4892{
094b34ac 4893 const struct type_unit_group *tu_group = item;
f4dc4d17 4894
094b34ac 4895 return hash_stmt_list_entry (&tu_group->hash);
f4dc4d17 4896}
348e048f
DE
4897
4898static int
f4dc4d17 4899eq_type_unit_group (const void *item_lhs, const void *item_rhs)
348e048f 4900{
f4dc4d17
DE
4901 const struct type_unit_group *lhs = item_lhs;
4902 const struct type_unit_group *rhs = item_rhs;
348e048f 4903
094b34ac 4904 return eq_stmt_list_entry (&lhs->hash, &rhs->hash);
f4dc4d17 4905}
348e048f 4906
f4dc4d17
DE
4907/* Allocate a hash table for type unit groups. */
4908
4909static htab_t
4910allocate_type_unit_groups_table (void)
4911{
4912 return htab_create_alloc_ex (3,
4913 hash_type_unit_group,
4914 eq_type_unit_group,
4915 NULL,
4916 &dwarf2_per_objfile->objfile->objfile_obstack,
4917 hashtab_obstack_allocate,
4918 dummy_obstack_deallocate);
4919}
dee91e82 4920
f4dc4d17
DE
4921/* Type units that don't have DW_AT_stmt_list are grouped into their own
4922 partial symtabs. We combine several TUs per psymtab to not let the size
4923 of any one psymtab grow too big. */
4924#define NO_STMT_LIST_TYPE_UNIT_PSYMTAB (1 << 31)
4925#define NO_STMT_LIST_TYPE_UNIT_PSYMTAB_SIZE 10
dee91e82 4926
094b34ac 4927/* Helper routine for get_type_unit_group.
f4dc4d17
DE
4928 Create the type_unit_group object used to hold one or more TUs. */
4929
4930static struct type_unit_group *
094b34ac 4931create_type_unit_group (struct dwarf2_cu *cu, sect_offset line_offset_struct)
f4dc4d17
DE
4932{
4933 struct objfile *objfile = dwarf2_per_objfile->objfile;
094b34ac 4934 struct dwarf2_per_cu_data *per_cu;
f4dc4d17 4935 struct type_unit_group *tu_group;
f4dc4d17
DE
4936
4937 tu_group = OBSTACK_ZALLOC (&objfile->objfile_obstack,
4938 struct type_unit_group);
094b34ac 4939 per_cu = &tu_group->per_cu;
f4dc4d17
DE
4940 per_cu->objfile = objfile;
4941 per_cu->is_debug_types = 1;
4942 per_cu->s.type_unit_group = tu_group;
4943
094b34ac
DE
4944 if (dwarf2_per_objfile->using_index)
4945 {
4946 per_cu->v.quick = OBSTACK_ZALLOC (&objfile->objfile_obstack,
4947 struct dwarf2_per_cu_quick_data);
4948 tu_group->t.first_tu = cu->per_cu;
4949 }
4950 else
4951 {
4952 unsigned int line_offset = line_offset_struct.sect_off;
4953 struct partial_symtab *pst;
4954 char *name;
4955
4956 /* Give the symtab a useful name for debug purposes. */
4957 if ((line_offset & NO_STMT_LIST_TYPE_UNIT_PSYMTAB) != 0)
4958 name = xstrprintf ("<type_units_%d>",
4959 (line_offset & ~NO_STMT_LIST_TYPE_UNIT_PSYMTAB));
4960 else
4961 name = xstrprintf ("<type_units_at_0x%x>", line_offset);
4962
4963 pst = create_partial_symtab (per_cu, name);
4964 pst->anonymous = 1;
f4dc4d17 4965
094b34ac
DE
4966 xfree (name);
4967 }
f4dc4d17 4968
094b34ac
DE
4969 tu_group->hash.dwo_unit = cu->dwo_unit;
4970 tu_group->hash.line_offset = line_offset_struct;
f4dc4d17
DE
4971
4972 return tu_group;
4973}
4974
094b34ac
DE
4975/* Look up the type_unit_group for type unit CU, and create it if necessary.
4976 STMT_LIST is a DW_AT_stmt_list attribute. */
f4dc4d17
DE
4977
4978static struct type_unit_group *
094b34ac 4979get_type_unit_group (struct dwarf2_cu *cu, struct attribute *stmt_list)
f4dc4d17
DE
4980{
4981 struct tu_stats *tu_stats = &dwarf2_per_objfile->tu_stats;
4982 struct type_unit_group *tu_group;
4983 void **slot;
4984 unsigned int line_offset;
4985 struct type_unit_group type_unit_group_for_lookup;
4986
4987 if (dwarf2_per_objfile->type_unit_groups == NULL)
4988 {
4989 dwarf2_per_objfile->type_unit_groups =
4990 allocate_type_unit_groups_table ();
4991 }
4992
4993 /* Do we need to create a new group, or can we use an existing one? */
4994
4995 if (stmt_list)
4996 {
4997 line_offset = DW_UNSND (stmt_list);
4998 ++tu_stats->nr_symtab_sharers;
4999 }
5000 else
5001 {
5002 /* Ugh, no stmt_list. Rare, but we have to handle it.
5003 We can do various things here like create one group per TU or
5004 spread them over multiple groups to split up the expansion work.
5005 To avoid worst case scenarios (too many groups or too large groups)
5006 we, umm, group them in bunches. */
5007 line_offset = (NO_STMT_LIST_TYPE_UNIT_PSYMTAB
5008 | (tu_stats->nr_stmt_less_type_units
5009 / NO_STMT_LIST_TYPE_UNIT_PSYMTAB_SIZE));
5010 ++tu_stats->nr_stmt_less_type_units;
5011 }
5012
094b34ac
DE
5013 type_unit_group_for_lookup.hash.dwo_unit = cu->dwo_unit;
5014 type_unit_group_for_lookup.hash.line_offset.sect_off = line_offset;
f4dc4d17
DE
5015 slot = htab_find_slot (dwarf2_per_objfile->type_unit_groups,
5016 &type_unit_group_for_lookup, INSERT);
5017 if (*slot != NULL)
5018 {
5019 tu_group = *slot;
5020 gdb_assert (tu_group != NULL);
5021 }
5022 else
5023 {
5024 sect_offset line_offset_struct;
5025
5026 line_offset_struct.sect_off = line_offset;
094b34ac 5027 tu_group = create_type_unit_group (cu, line_offset_struct);
f4dc4d17
DE
5028 *slot = tu_group;
5029 ++tu_stats->nr_symtabs;
5030 }
5031
5032 return tu_group;
5033}
5034
5035/* Struct used to sort TUs by their abbreviation table offset. */
5036
5037struct tu_abbrev_offset
5038{
5039 struct signatured_type *sig_type;
5040 sect_offset abbrev_offset;
5041};
5042
5043/* Helper routine for build_type_unit_groups, passed to qsort. */
5044
5045static int
5046sort_tu_by_abbrev_offset (const void *ap, const void *bp)
5047{
5048 const struct tu_abbrev_offset * const *a = ap;
5049 const struct tu_abbrev_offset * const *b = bp;
5050 unsigned int aoff = (*a)->abbrev_offset.sect_off;
5051 unsigned int boff = (*b)->abbrev_offset.sect_off;
5052
5053 return (aoff > boff) - (aoff < boff);
5054}
5055
5056/* A helper function to add a type_unit_group to a table. */
5057
5058static int
5059add_type_unit_group_to_table (void **slot, void *datum)
5060{
5061 struct type_unit_group *tu_group = *slot;
5062 struct type_unit_group ***datap = datum;
5063
5064 **datap = tu_group;
5065 ++*datap;
5066
5067 return 1;
5068}
5069
5070/* Efficiently read all the type units, calling init_cutu_and_read_dies on
5071 each one passing FUNC,DATA.
5072
5073 The efficiency is because we sort TUs by the abbrev table they use and
5074 only read each abbrev table once. In one program there are 200K TUs
5075 sharing 8K abbrev tables.
5076
5077 The main purpose of this function is to support building the
5078 dwarf2_per_objfile->type_unit_groups table.
5079 TUs typically share the DW_AT_stmt_list of the CU they came from, so we
5080 can collapse the search space by grouping them by stmt_list.
5081 The savings can be significant, in the same program from above the 200K TUs
5082 share 8K stmt_list tables.
5083
5084 FUNC is expected to call get_type_unit_group, which will create the
5085 struct type_unit_group if necessary and add it to
5086 dwarf2_per_objfile->type_unit_groups. */
5087
5088static void
5089build_type_unit_groups (die_reader_func_ftype *func, void *data)
5090{
5091 struct objfile *objfile = dwarf2_per_objfile->objfile;
5092 struct tu_stats *tu_stats = &dwarf2_per_objfile->tu_stats;
5093 struct cleanup *cleanups;
5094 struct abbrev_table *abbrev_table;
5095 sect_offset abbrev_offset;
5096 struct tu_abbrev_offset *sorted_by_abbrev;
5097 struct type_unit_group **iter;
5098 int i;
5099
5100 /* It's up to the caller to not call us multiple times. */
5101 gdb_assert (dwarf2_per_objfile->type_unit_groups == NULL);
5102
5103 if (dwarf2_per_objfile->n_type_units == 0)
5104 return;
5105
5106 /* TUs typically share abbrev tables, and there can be way more TUs than
5107 abbrev tables. Sort by abbrev table to reduce the number of times we
5108 read each abbrev table in.
5109 Alternatives are to punt or to maintain a cache of abbrev tables.
5110 This is simpler and efficient enough for now.
5111
5112 Later we group TUs by their DW_AT_stmt_list value (as this defines the
5113 symtab to use). Typically TUs with the same abbrev offset have the same
5114 stmt_list value too so in practice this should work well.
5115
5116 The basic algorithm here is:
5117
5118 sort TUs by abbrev table
5119 for each TU with same abbrev table:
5120 read abbrev table if first user
5121 read TU top level DIE
5122 [IWBN if DWO skeletons had DW_AT_stmt_list]
5123 call FUNC */
5124
5125 if (dwarf2_read_debug)
5126 fprintf_unfiltered (gdb_stdlog, "Building type unit groups ...\n");
5127
5128 /* Sort in a separate table to maintain the order of all_type_units
5129 for .gdb_index: TU indices directly index all_type_units. */
5130 sorted_by_abbrev = XNEWVEC (struct tu_abbrev_offset,
5131 dwarf2_per_objfile->n_type_units);
5132 for (i = 0; i < dwarf2_per_objfile->n_type_units; ++i)
5133 {
5134 struct signatured_type *sig_type = dwarf2_per_objfile->all_type_units[i];
5135
5136 sorted_by_abbrev[i].sig_type = sig_type;
5137 sorted_by_abbrev[i].abbrev_offset =
5138 read_abbrev_offset (sig_type->per_cu.info_or_types_section,
5139 sig_type->per_cu.offset);
5140 }
5141 cleanups = make_cleanup (xfree, sorted_by_abbrev);
5142 qsort (sorted_by_abbrev, dwarf2_per_objfile->n_type_units,
5143 sizeof (struct tu_abbrev_offset), sort_tu_by_abbrev_offset);
5144
094b34ac
DE
5145 /* Note: In the .gdb_index case, get_type_unit_group may have already been
5146 called any number of times, so we don't reset tu_stats here. */
5147
f4dc4d17
DE
5148 abbrev_offset.sect_off = ~(unsigned) 0;
5149 abbrev_table = NULL;
5150 make_cleanup (abbrev_table_free_cleanup, &abbrev_table);
5151
5152 for (i = 0; i < dwarf2_per_objfile->n_type_units; ++i)
5153 {
5154 const struct tu_abbrev_offset *tu = &sorted_by_abbrev[i];
5155
5156 /* Switch to the next abbrev table if necessary. */
5157 if (abbrev_table == NULL
5158 || tu->abbrev_offset.sect_off != abbrev_offset.sect_off)
5159 {
5160 if (abbrev_table != NULL)
5161 {
5162 abbrev_table_free (abbrev_table);
5163 /* Reset to NULL in case abbrev_table_read_table throws
5164 an error: abbrev_table_free_cleanup will get called. */
5165 abbrev_table = NULL;
5166 }
5167 abbrev_offset = tu->abbrev_offset;
5168 abbrev_table =
5169 abbrev_table_read_table (&dwarf2_per_objfile->abbrev,
5170 abbrev_offset);
5171 ++tu_stats->nr_uniq_abbrev_tables;
5172 }
5173
5174 init_cutu_and_read_dies (&tu->sig_type->per_cu, abbrev_table, 0, 0,
5175 func, data);
5176 }
5177
5178 /* Create a vector of pointers to primary type units to make it easy to
5179 iterate over them and CUs. See dw2_get_primary_cu. */
5180 dwarf2_per_objfile->n_type_unit_groups =
5181 htab_elements (dwarf2_per_objfile->type_unit_groups);
5182 dwarf2_per_objfile->all_type_unit_groups =
5183 obstack_alloc (&objfile->objfile_obstack,
5184 dwarf2_per_objfile->n_type_unit_groups
5185 * sizeof (struct type_unit_group *));
5186 iter = &dwarf2_per_objfile->all_type_unit_groups[0];
5187 htab_traverse_noresize (dwarf2_per_objfile->type_unit_groups,
5188 add_type_unit_group_to_table, &iter);
5189 gdb_assert (iter - &dwarf2_per_objfile->all_type_unit_groups[0]
5190 == dwarf2_per_objfile->n_type_unit_groups);
5191
5192 do_cleanups (cleanups);
5193
5194 if (dwarf2_read_debug)
5195 {
5196 fprintf_unfiltered (gdb_stdlog, "Done building type unit groups:\n");
5197 fprintf_unfiltered (gdb_stdlog, " %d TUs\n",
5198 dwarf2_per_objfile->n_type_units);
5199 fprintf_unfiltered (gdb_stdlog, " %d uniq abbrev tables\n",
5200 tu_stats->nr_uniq_abbrev_tables);
5201 fprintf_unfiltered (gdb_stdlog, " %d symtabs from stmt_list entries\n",
5202 tu_stats->nr_symtabs);
5203 fprintf_unfiltered (gdb_stdlog, " %d symtab sharers\n",
5204 tu_stats->nr_symtab_sharers);
5205 fprintf_unfiltered (gdb_stdlog, " %d type units without a stmt_list\n",
5206 tu_stats->nr_stmt_less_type_units);
5207 }
5208}
5209
5210/* Reader function for build_type_psymtabs. */
5211
5212static void
5213build_type_psymtabs_reader (const struct die_reader_specs *reader,
5214 gdb_byte *info_ptr,
5215 struct die_info *type_unit_die,
5216 int has_children,
5217 void *data)
5218{
5219 struct objfile *objfile = dwarf2_per_objfile->objfile;
5220 struct dwarf2_cu *cu = reader->cu;
5221 struct dwarf2_per_cu_data *per_cu = cu->per_cu;
5222 struct type_unit_group *tu_group;
5223 struct attribute *attr;
5224 struct partial_die_info *first_die;
5225 CORE_ADDR lowpc, highpc;
5226 struct partial_symtab *pst;
5227
5228 gdb_assert (data == NULL);
5229
5230 if (! has_children)
5231 return;
5232
5233 attr = dwarf2_attr_no_follow (type_unit_die, DW_AT_stmt_list);
094b34ac 5234 tu_group = get_type_unit_group (cu, attr);
f4dc4d17 5235
094b34ac 5236 VEC_safe_push (dwarf2_per_cu_ptr, tu_group->t.tus, per_cu);
f4dc4d17
DE
5237
5238 prepare_one_comp_unit (cu, type_unit_die, language_minimal);
5239 cu->list_in_scope = &file_symbols;
5240 pst = create_partial_symtab (per_cu, "");
5241 pst->anonymous = 1;
5242
5243 first_die = load_partial_dies (reader, info_ptr, 1);
5244
5245 lowpc = (CORE_ADDR) -1;
5246 highpc = (CORE_ADDR) 0;
5247 scan_partial_symbols (first_die, &lowpc, &highpc, 0, cu);
5248
5249 pst->n_global_syms = objfile->global_psymbols.next -
5250 (objfile->global_psymbols.list + pst->globals_offset);
5251 pst->n_static_syms = objfile->static_psymbols.next -
5252 (objfile->static_psymbols.list + pst->statics_offset);
5253 sort_pst_symbols (pst);
5254}
5255
5256/* Traversal function for build_type_psymtabs. */
5257
5258static int
5259build_type_psymtab_dependencies (void **slot, void *info)
5260{
5261 struct objfile *objfile = dwarf2_per_objfile->objfile;
5262 struct type_unit_group *tu_group = (struct type_unit_group *) *slot;
094b34ac 5263 struct dwarf2_per_cu_data *per_cu = &tu_group->per_cu;
f4dc4d17 5264 struct partial_symtab *pst = per_cu->v.psymtab;
094b34ac 5265 int len = VEC_length (dwarf2_per_cu_ptr, tu_group->t.tus);
f4dc4d17
DE
5266 struct dwarf2_per_cu_data *iter;
5267 int i;
5268
5269 gdb_assert (len > 0);
5270
5271 pst->number_of_dependencies = len;
5272 pst->dependencies = obstack_alloc (&objfile->objfile_obstack,
5273 len * sizeof (struct psymtab *));
5274 for (i = 0;
094b34ac 5275 VEC_iterate (dwarf2_per_cu_ptr, tu_group->t.tus, i, iter);
f4dc4d17
DE
5276 ++i)
5277 {
5278 pst->dependencies[i] = iter->v.psymtab;
5279 iter->s.type_unit_group = tu_group;
5280 }
5281
094b34ac 5282 VEC_free (dwarf2_per_cu_ptr, tu_group->t.tus);
348e048f
DE
5283
5284 return 1;
5285}
5286
5287/* Subroutine of dwarf2_build_psymtabs_hard to simplify it.
5288 Build partial symbol tables for the .debug_types comp-units. */
5289
5290static void
5291build_type_psymtabs (struct objfile *objfile)
5292{
0e50663e 5293 if (! create_all_type_units (objfile))
348e048f
DE
5294 return;
5295
f4dc4d17
DE
5296 build_type_unit_groups (build_type_psymtabs_reader, NULL);
5297
5298 /* Now that all TUs have been processed we can fill in the dependencies. */
5299 htab_traverse_noresize (dwarf2_per_objfile->type_unit_groups,
5300 build_type_psymtab_dependencies, NULL);
348e048f
DE
5301}
5302
60606b2c
TT
5303/* A cleanup function that clears objfile's psymtabs_addrmap field. */
5304
5305static void
5306psymtabs_addrmap_cleanup (void *o)
5307{
5308 struct objfile *objfile = o;
ec61707d 5309
60606b2c
TT
5310 objfile->psymtabs_addrmap = NULL;
5311}
5312
95554aad
TT
5313/* Compute the 'user' field for each psymtab in OBJFILE. */
5314
5315static void
5316set_partial_user (struct objfile *objfile)
5317{
5318 int i;
5319
5320 for (i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
5321 {
5322 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
5323 struct partial_symtab *pst = per_cu->v.psymtab;
5324 int j;
5325
36586728
TT
5326 if (pst == NULL)
5327 continue;
5328
95554aad
TT
5329 for (j = 0; j < pst->number_of_dependencies; ++j)
5330 {
5331 /* Set the 'user' field only if it is not already set. */
5332 if (pst->dependencies[j]->user == NULL)
5333 pst->dependencies[j]->user = pst;
5334 }
5335 }
5336}
5337
93311388
DE
5338/* Build the partial symbol table by doing a quick pass through the
5339 .debug_info and .debug_abbrev sections. */
72bf9492 5340
93311388 5341static void
c67a9c90 5342dwarf2_build_psymtabs_hard (struct objfile *objfile)
93311388 5343{
60606b2c
TT
5344 struct cleanup *back_to, *addrmap_cleanup;
5345 struct obstack temp_obstack;
21b2bd31 5346 int i;
93311388 5347
45cfd468
DE
5348 if (dwarf2_read_debug)
5349 {
5350 fprintf_unfiltered (gdb_stdlog, "Building psymtabs of objfile %s ...\n",
5351 objfile->name);
5352 }
5353
98bfdba5
PA
5354 dwarf2_per_objfile->reading_partial_symbols = 1;
5355
be391dca 5356 dwarf2_read_section (objfile, &dwarf2_per_objfile->info);
91c24f0a 5357
93311388
DE
5358 /* Any cached compilation units will be linked by the per-objfile
5359 read_in_chain. Make sure to free them when we're done. */
5360 back_to = make_cleanup (free_cached_comp_units, NULL);
72bf9492 5361
348e048f
DE
5362 build_type_psymtabs (objfile);
5363
93311388 5364 create_all_comp_units (objfile);
c906108c 5365
60606b2c
TT
5366 /* Create a temporary address map on a temporary obstack. We later
5367 copy this to the final obstack. */
5368 obstack_init (&temp_obstack);
5369 make_cleanup_obstack_free (&temp_obstack);
5370 objfile->psymtabs_addrmap = addrmap_create_mutable (&temp_obstack);
5371 addrmap_cleanup = make_cleanup (psymtabs_addrmap_cleanup, objfile);
72bf9492 5372
21b2bd31 5373 for (i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
93311388 5374 {
21b2bd31 5375 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
aaa75496 5376
95554aad 5377 process_psymtab_comp_unit (per_cu, 0);
c906108c 5378 }
ff013f42 5379
95554aad
TT
5380 set_partial_user (objfile);
5381
ff013f42
JK
5382 objfile->psymtabs_addrmap = addrmap_create_fixed (objfile->psymtabs_addrmap,
5383 &objfile->objfile_obstack);
60606b2c 5384 discard_cleanups (addrmap_cleanup);
ff013f42 5385
ae038cb0 5386 do_cleanups (back_to);
45cfd468
DE
5387
5388 if (dwarf2_read_debug)
5389 fprintf_unfiltered (gdb_stdlog, "Done building psymtabs of %s\n",
5390 objfile->name);
ae038cb0
DJ
5391}
5392
3019eac3 5393/* die_reader_func for load_partial_comp_unit. */
ae038cb0
DJ
5394
5395static void
dee91e82
DE
5396load_partial_comp_unit_reader (const struct die_reader_specs *reader,
5397 gdb_byte *info_ptr,
5398 struct die_info *comp_unit_die,
5399 int has_children,
5400 void *data)
ae038cb0 5401{
dee91e82 5402 struct dwarf2_cu *cu = reader->cu;
ae038cb0 5403
95554aad 5404 prepare_one_comp_unit (cu, comp_unit_die, language_minimal);
ae038cb0 5405
ae038cb0
DJ
5406 /* Check if comp unit has_children.
5407 If so, read the rest of the partial symbols from this comp unit.
0963b4bd 5408 If not, there's no more debug_info for this comp unit. */
d85a05f0 5409 if (has_children)
dee91e82
DE
5410 load_partial_dies (reader, info_ptr, 0);
5411}
98bfdba5 5412
dee91e82
DE
5413/* Load the partial DIEs for a secondary CU into memory.
5414 This is also used when rereading a primary CU with load_all_dies. */
c5b7e1cb 5415
dee91e82
DE
5416static void
5417load_partial_comp_unit (struct dwarf2_per_cu_data *this_cu)
5418{
f4dc4d17
DE
5419 init_cutu_and_read_dies (this_cu, NULL, 1, 1,
5420 load_partial_comp_unit_reader, NULL);
ae038cb0
DJ
5421}
5422
ae038cb0 5423static void
36586728
TT
5424read_comp_units_from_section (struct objfile *objfile,
5425 struct dwarf2_section_info *section,
5426 unsigned int is_dwz,
5427 int *n_allocated,
5428 int *n_comp_units,
5429 struct dwarf2_per_cu_data ***all_comp_units)
ae038cb0 5430{
be391dca 5431 gdb_byte *info_ptr;
36586728 5432 bfd *abfd = section->asection->owner;
be391dca 5433
36586728 5434 dwarf2_read_section (objfile, section);
ae038cb0 5435
36586728 5436 info_ptr = section->buffer;
6e70227d 5437
36586728 5438 while (info_ptr < section->buffer + section->size)
ae038cb0 5439 {
c764a876 5440 unsigned int length, initial_length_size;
ae038cb0 5441 struct dwarf2_per_cu_data *this_cu;
b64f50a1 5442 sect_offset offset;
ae038cb0 5443
36586728 5444 offset.sect_off = info_ptr - section->buffer;
ae038cb0
DJ
5445
5446 /* Read just enough information to find out where the next
5447 compilation unit is. */
36586728 5448 length = read_initial_length (abfd, info_ptr, &initial_length_size);
ae038cb0
DJ
5449
5450 /* Save the compilation unit for later lookup. */
5451 this_cu = obstack_alloc (&objfile->objfile_obstack,
5452 sizeof (struct dwarf2_per_cu_data));
5453 memset (this_cu, 0, sizeof (*this_cu));
5454 this_cu->offset = offset;
c764a876 5455 this_cu->length = length + initial_length_size;
36586728 5456 this_cu->is_dwz = is_dwz;
9291a0cd 5457 this_cu->objfile = objfile;
36586728 5458 this_cu->info_or_types_section = section;
ae038cb0 5459
36586728 5460 if (*n_comp_units == *n_allocated)
ae038cb0 5461 {
36586728
TT
5462 *n_allocated *= 2;
5463 *all_comp_units = xrealloc (*all_comp_units,
5464 *n_allocated
5465 * sizeof (struct dwarf2_per_cu_data *));
ae038cb0 5466 }
36586728
TT
5467 (*all_comp_units)[*n_comp_units] = this_cu;
5468 ++*n_comp_units;
ae038cb0
DJ
5469
5470 info_ptr = info_ptr + this_cu->length;
5471 }
36586728
TT
5472}
5473
5474/* Create a list of all compilation units in OBJFILE.
5475 This is only done for -readnow and building partial symtabs. */
5476
5477static void
5478create_all_comp_units (struct objfile *objfile)
5479{
5480 int n_allocated;
5481 int n_comp_units;
5482 struct dwarf2_per_cu_data **all_comp_units;
5483
5484 n_comp_units = 0;
5485 n_allocated = 10;
5486 all_comp_units = xmalloc (n_allocated
5487 * sizeof (struct dwarf2_per_cu_data *));
5488
5489 read_comp_units_from_section (objfile, &dwarf2_per_objfile->info, 0,
5490 &n_allocated, &n_comp_units, &all_comp_units);
5491
5492 if (bfd_get_section_by_name (objfile->obfd, ".gnu_debugaltlink") != NULL)
5493 {
5494 struct dwz_file *dwz = dwarf2_get_dwz_file ();
5495
5496 read_comp_units_from_section (objfile, &dwz->info, 1,
5497 &n_allocated, &n_comp_units,
5498 &all_comp_units);
5499 }
ae038cb0
DJ
5500
5501 dwarf2_per_objfile->all_comp_units
5502 = obstack_alloc (&objfile->objfile_obstack,
5503 n_comp_units * sizeof (struct dwarf2_per_cu_data *));
5504 memcpy (dwarf2_per_objfile->all_comp_units, all_comp_units,
5505 n_comp_units * sizeof (struct dwarf2_per_cu_data *));
5506 xfree (all_comp_units);
5507 dwarf2_per_objfile->n_comp_units = n_comp_units;
c906108c
SS
5508}
5509
5734ee8b
DJ
5510/* Process all loaded DIEs for compilation unit CU, starting at
5511 FIRST_DIE. The caller should pass NEED_PC == 1 if the compilation
5512 unit DIE did not have PC info (DW_AT_low_pc and DW_AT_high_pc, or
5513 DW_AT_ranges). If NEED_PC is set, then this function will set
5514 *LOWPC and *HIGHPC to the lowest and highest PC values found in CU
5515 and record the covered ranges in the addrmap. */
c906108c 5516
72bf9492
DJ
5517static void
5518scan_partial_symbols (struct partial_die_info *first_die, CORE_ADDR *lowpc,
5734ee8b 5519 CORE_ADDR *highpc, int need_pc, struct dwarf2_cu *cu)
c906108c 5520{
72bf9492 5521 struct partial_die_info *pdi;
c906108c 5522
91c24f0a
DC
5523 /* Now, march along the PDI's, descending into ones which have
5524 interesting children but skipping the children of the other ones,
5525 until we reach the end of the compilation unit. */
c906108c 5526
72bf9492 5527 pdi = first_die;
91c24f0a 5528
72bf9492
DJ
5529 while (pdi != NULL)
5530 {
5531 fixup_partial_die (pdi, cu);
c906108c 5532
f55ee35c 5533 /* Anonymous namespaces or modules have no name but have interesting
91c24f0a
DC
5534 children, so we need to look at them. Ditto for anonymous
5535 enums. */
933c6fe4 5536
72bf9492 5537 if (pdi->name != NULL || pdi->tag == DW_TAG_namespace
95554aad
TT
5538 || pdi->tag == DW_TAG_module || pdi->tag == DW_TAG_enumeration_type
5539 || pdi->tag == DW_TAG_imported_unit)
c906108c 5540 {
72bf9492 5541 switch (pdi->tag)
c906108c
SS
5542 {
5543 case DW_TAG_subprogram:
5734ee8b 5544 add_partial_subprogram (pdi, lowpc, highpc, need_pc, cu);
c906108c 5545 break;
72929c62 5546 case DW_TAG_constant:
c906108c
SS
5547 case DW_TAG_variable:
5548 case DW_TAG_typedef:
91c24f0a 5549 case DW_TAG_union_type:
72bf9492 5550 if (!pdi->is_declaration)
63d06c5c 5551 {
72bf9492 5552 add_partial_symbol (pdi, cu);
63d06c5c
DC
5553 }
5554 break;
c906108c 5555 case DW_TAG_class_type:
680b30c7 5556 case DW_TAG_interface_type:
c906108c 5557 case DW_TAG_structure_type:
72bf9492 5558 if (!pdi->is_declaration)
c906108c 5559 {
72bf9492 5560 add_partial_symbol (pdi, cu);
c906108c
SS
5561 }
5562 break;
91c24f0a 5563 case DW_TAG_enumeration_type:
72bf9492
DJ
5564 if (!pdi->is_declaration)
5565 add_partial_enumeration (pdi, cu);
c906108c
SS
5566 break;
5567 case DW_TAG_base_type:
a02abb62 5568 case DW_TAG_subrange_type:
c906108c 5569 /* File scope base type definitions are added to the partial
c5aa993b 5570 symbol table. */
72bf9492 5571 add_partial_symbol (pdi, cu);
c906108c 5572 break;
d9fa45fe 5573 case DW_TAG_namespace:
5734ee8b 5574 add_partial_namespace (pdi, lowpc, highpc, need_pc, cu);
91c24f0a 5575 break;
5d7cb8df
JK
5576 case DW_TAG_module:
5577 add_partial_module (pdi, lowpc, highpc, need_pc, cu);
5578 break;
95554aad
TT
5579 case DW_TAG_imported_unit:
5580 {
5581 struct dwarf2_per_cu_data *per_cu;
5582
f4dc4d17
DE
5583 /* For now we don't handle imported units in type units. */
5584 if (cu->per_cu->is_debug_types)
5585 {
5586 error (_("Dwarf Error: DW_TAG_imported_unit is not"
5587 " supported in type units [in module %s]"),
5588 cu->objfile->name);
5589 }
5590
95554aad 5591 per_cu = dwarf2_find_containing_comp_unit (pdi->d.offset,
36586728 5592 pdi->is_dwz,
95554aad
TT
5593 cu->objfile);
5594
5595 /* Go read the partial unit, if needed. */
5596 if (per_cu->v.psymtab == NULL)
5597 process_psymtab_comp_unit (per_cu, 1);
5598
f4dc4d17
DE
5599 VEC_safe_push (dwarf2_per_cu_ptr,
5600 cu->per_cu->s.imported_symtabs, per_cu);
95554aad
TT
5601 }
5602 break;
c906108c
SS
5603 default:
5604 break;
5605 }
5606 }
5607
72bf9492
DJ
5608 /* If the die has a sibling, skip to the sibling. */
5609
5610 pdi = pdi->die_sibling;
5611 }
5612}
5613
5614/* Functions used to compute the fully scoped name of a partial DIE.
91c24f0a 5615
72bf9492 5616 Normally, this is simple. For C++, the parent DIE's fully scoped
987504bb
JJ
5617 name is concatenated with "::" and the partial DIE's name. For
5618 Java, the same thing occurs except that "." is used instead of "::".
72bf9492
DJ
5619 Enumerators are an exception; they use the scope of their parent
5620 enumeration type, i.e. the name of the enumeration type is not
5621 prepended to the enumerator.
91c24f0a 5622
72bf9492
DJ
5623 There are two complexities. One is DW_AT_specification; in this
5624 case "parent" means the parent of the target of the specification,
5625 instead of the direct parent of the DIE. The other is compilers
5626 which do not emit DW_TAG_namespace; in this case we try to guess
5627 the fully qualified name of structure types from their members'
5628 linkage names. This must be done using the DIE's children rather
5629 than the children of any DW_AT_specification target. We only need
5630 to do this for structures at the top level, i.e. if the target of
5631 any DW_AT_specification (if any; otherwise the DIE itself) does not
5632 have a parent. */
5633
5634/* Compute the scope prefix associated with PDI's parent, in
5635 compilation unit CU. The result will be allocated on CU's
5636 comp_unit_obstack, or a copy of the already allocated PDI->NAME
5637 field. NULL is returned if no prefix is necessary. */
5638static char *
5639partial_die_parent_scope (struct partial_die_info *pdi,
5640 struct dwarf2_cu *cu)
5641{
5642 char *grandparent_scope;
5643 struct partial_die_info *parent, *real_pdi;
91c24f0a 5644
72bf9492
DJ
5645 /* We need to look at our parent DIE; if we have a DW_AT_specification,
5646 then this means the parent of the specification DIE. */
5647
5648 real_pdi = pdi;
72bf9492 5649 while (real_pdi->has_specification)
36586728
TT
5650 real_pdi = find_partial_die (real_pdi->spec_offset,
5651 real_pdi->spec_is_dwz, cu);
72bf9492
DJ
5652
5653 parent = real_pdi->die_parent;
5654 if (parent == NULL)
5655 return NULL;
5656
5657 if (parent->scope_set)
5658 return parent->scope;
5659
5660 fixup_partial_die (parent, cu);
5661
10b3939b 5662 grandparent_scope = partial_die_parent_scope (parent, cu);
72bf9492 5663
acebe513
UW
5664 /* GCC 4.0 and 4.1 had a bug (PR c++/28460) where they generated bogus
5665 DW_TAG_namespace DIEs with a name of "::" for the global namespace.
5666 Work around this problem here. */
5667 if (cu->language == language_cplus
6e70227d 5668 && parent->tag == DW_TAG_namespace
acebe513
UW
5669 && strcmp (parent->name, "::") == 0
5670 && grandparent_scope == NULL)
5671 {
5672 parent->scope = NULL;
5673 parent->scope_set = 1;
5674 return NULL;
5675 }
5676
9c6c53f7
SA
5677 if (pdi->tag == DW_TAG_enumerator)
5678 /* Enumerators should not get the name of the enumeration as a prefix. */
5679 parent->scope = grandparent_scope;
5680 else if (parent->tag == DW_TAG_namespace
f55ee35c 5681 || parent->tag == DW_TAG_module
72bf9492
DJ
5682 || parent->tag == DW_TAG_structure_type
5683 || parent->tag == DW_TAG_class_type
680b30c7 5684 || parent->tag == DW_TAG_interface_type
ceeb3d5a
TT
5685 || parent->tag == DW_TAG_union_type
5686 || parent->tag == DW_TAG_enumeration_type)
72bf9492
DJ
5687 {
5688 if (grandparent_scope == NULL)
5689 parent->scope = parent->name;
5690 else
3e43a32a
MS
5691 parent->scope = typename_concat (&cu->comp_unit_obstack,
5692 grandparent_scope,
f55ee35c 5693 parent->name, 0, cu);
72bf9492 5694 }
72bf9492
DJ
5695 else
5696 {
5697 /* FIXME drow/2004-04-01: What should we be doing with
5698 function-local names? For partial symbols, we should probably be
5699 ignoring them. */
5700 complaint (&symfile_complaints,
e2e0b3e5 5701 _("unhandled containing DIE tag %d for DIE at %d"),
b64f50a1 5702 parent->tag, pdi->offset.sect_off);
72bf9492 5703 parent->scope = grandparent_scope;
c906108c
SS
5704 }
5705
72bf9492
DJ
5706 parent->scope_set = 1;
5707 return parent->scope;
5708}
5709
5710/* Return the fully scoped name associated with PDI, from compilation unit
5711 CU. The result will be allocated with malloc. */
4568ecf9 5712
72bf9492
DJ
5713static char *
5714partial_die_full_name (struct partial_die_info *pdi,
5715 struct dwarf2_cu *cu)
5716{
5717 char *parent_scope;
5718
98bfdba5
PA
5719 /* If this is a template instantiation, we can not work out the
5720 template arguments from partial DIEs. So, unfortunately, we have
5721 to go through the full DIEs. At least any work we do building
5722 types here will be reused if full symbols are loaded later. */
5723 if (pdi->has_template_arguments)
5724 {
5725 fixup_partial_die (pdi, cu);
5726
5727 if (pdi->name != NULL && strchr (pdi->name, '<') == NULL)
5728 {
5729 struct die_info *die;
5730 struct attribute attr;
5731 struct dwarf2_cu *ref_cu = cu;
5732
b64f50a1 5733 /* DW_FORM_ref_addr is using section offset. */
98bfdba5
PA
5734 attr.name = 0;
5735 attr.form = DW_FORM_ref_addr;
4568ecf9 5736 attr.u.unsnd = pdi->offset.sect_off;
98bfdba5
PA
5737 die = follow_die_ref (NULL, &attr, &ref_cu);
5738
5739 return xstrdup (dwarf2_full_name (NULL, die, ref_cu));
5740 }
5741 }
5742
72bf9492
DJ
5743 parent_scope = partial_die_parent_scope (pdi, cu);
5744 if (parent_scope == NULL)
5745 return NULL;
5746 else
f55ee35c 5747 return typename_concat (NULL, parent_scope, pdi->name, 0, cu);
c906108c
SS
5748}
5749
5750static void
72bf9492 5751add_partial_symbol (struct partial_die_info *pdi, struct dwarf2_cu *cu)
c906108c 5752{
e7c27a73 5753 struct objfile *objfile = cu->objfile;
c906108c 5754 CORE_ADDR addr = 0;
decbce07 5755 char *actual_name = NULL;
e142c38c 5756 CORE_ADDR baseaddr;
72bf9492 5757 int built_actual_name = 0;
e142c38c
DJ
5758
5759 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
c906108c 5760
94af9270
KS
5761 actual_name = partial_die_full_name (pdi, cu);
5762 if (actual_name)
5763 built_actual_name = 1;
63d06c5c 5764
72bf9492
DJ
5765 if (actual_name == NULL)
5766 actual_name = pdi->name;
5767
c906108c
SS
5768 switch (pdi->tag)
5769 {
5770 case DW_TAG_subprogram:
2cfa0c8d 5771 if (pdi->is_external || cu->language == language_ada)
c906108c 5772 {
2cfa0c8d
JB
5773 /* brobecker/2007-12-26: Normally, only "external" DIEs are part
5774 of the global scope. But in Ada, we want to be able to access
5775 nested procedures globally. So all Ada subprograms are stored
5776 in the global scope. */
f47fb265 5777 /* prim_record_minimal_symbol (actual_name, pdi->lowpc + baseaddr,
c5aa993b 5778 mst_text, objfile); */
f47fb265
MS
5779 add_psymbol_to_list (actual_name, strlen (actual_name),
5780 built_actual_name,
5781 VAR_DOMAIN, LOC_BLOCK,
5782 &objfile->global_psymbols,
5783 0, pdi->lowpc + baseaddr,
5784 cu->language, objfile);
c906108c
SS
5785 }
5786 else
5787 {
f47fb265 5788 /* prim_record_minimal_symbol (actual_name, pdi->lowpc + baseaddr,
c5aa993b 5789 mst_file_text, objfile); */
f47fb265
MS
5790 add_psymbol_to_list (actual_name, strlen (actual_name),
5791 built_actual_name,
5792 VAR_DOMAIN, LOC_BLOCK,
5793 &objfile->static_psymbols,
5794 0, pdi->lowpc + baseaddr,
5795 cu->language, objfile);
c906108c
SS
5796 }
5797 break;
72929c62
JB
5798 case DW_TAG_constant:
5799 {
5800 struct psymbol_allocation_list *list;
5801
5802 if (pdi->is_external)
5803 list = &objfile->global_psymbols;
5804 else
5805 list = &objfile->static_psymbols;
f47fb265
MS
5806 add_psymbol_to_list (actual_name, strlen (actual_name),
5807 built_actual_name, VAR_DOMAIN, LOC_STATIC,
5808 list, 0, 0, cu->language, objfile);
72929c62
JB
5809 }
5810 break;
c906108c 5811 case DW_TAG_variable:
95554aad
TT
5812 if (pdi->d.locdesc)
5813 addr = decode_locdesc (pdi->d.locdesc, cu);
caac4577 5814
95554aad 5815 if (pdi->d.locdesc
caac4577
JG
5816 && addr == 0
5817 && !dwarf2_per_objfile->has_section_at_zero)
5818 {
5819 /* A global or static variable may also have been stripped
5820 out by the linker if unused, in which case its address
5821 will be nullified; do not add such variables into partial
5822 symbol table then. */
5823 }
5824 else if (pdi->is_external)
c906108c
SS
5825 {
5826 /* Global Variable.
5827 Don't enter into the minimal symbol tables as there is
5828 a minimal symbol table entry from the ELF symbols already.
5829 Enter into partial symbol table if it has a location
5830 descriptor or a type.
5831 If the location descriptor is missing, new_symbol will create
5832 a LOC_UNRESOLVED symbol, the address of the variable will then
5833 be determined from the minimal symbol table whenever the variable
5834 is referenced.
5835 The address for the partial symbol table entry is not
5836 used by GDB, but it comes in handy for debugging partial symbol
5837 table building. */
5838
95554aad 5839 if (pdi->d.locdesc || pdi->has_type)
f47fb265
MS
5840 add_psymbol_to_list (actual_name, strlen (actual_name),
5841 built_actual_name,
5842 VAR_DOMAIN, LOC_STATIC,
5843 &objfile->global_psymbols,
5844 0, addr + baseaddr,
5845 cu->language, objfile);
c906108c
SS
5846 }
5847 else
5848 {
0963b4bd 5849 /* Static Variable. Skip symbols without location descriptors. */
95554aad 5850 if (pdi->d.locdesc == NULL)
decbce07
MS
5851 {
5852 if (built_actual_name)
5853 xfree (actual_name);
5854 return;
5855 }
f47fb265 5856 /* prim_record_minimal_symbol (actual_name, addr + baseaddr,
c5aa993b 5857 mst_file_data, objfile); */
f47fb265
MS
5858 add_psymbol_to_list (actual_name, strlen (actual_name),
5859 built_actual_name,
5860 VAR_DOMAIN, LOC_STATIC,
5861 &objfile->static_psymbols,
5862 0, addr + baseaddr,
5863 cu->language, objfile);
c906108c
SS
5864 }
5865 break;
5866 case DW_TAG_typedef:
5867 case DW_TAG_base_type:
a02abb62 5868 case DW_TAG_subrange_type:
38d518c9 5869 add_psymbol_to_list (actual_name, strlen (actual_name),
04a679b8 5870 built_actual_name,
176620f1 5871 VAR_DOMAIN, LOC_TYPEDEF,
c906108c 5872 &objfile->static_psymbols,
e142c38c 5873 0, (CORE_ADDR) 0, cu->language, objfile);
c906108c 5874 break;
72bf9492
DJ
5875 case DW_TAG_namespace:
5876 add_psymbol_to_list (actual_name, strlen (actual_name),
04a679b8 5877 built_actual_name,
72bf9492
DJ
5878 VAR_DOMAIN, LOC_TYPEDEF,
5879 &objfile->global_psymbols,
5880 0, (CORE_ADDR) 0, cu->language, objfile);
5881 break;
c906108c 5882 case DW_TAG_class_type:
680b30c7 5883 case DW_TAG_interface_type:
c906108c
SS
5884 case DW_TAG_structure_type:
5885 case DW_TAG_union_type:
5886 case DW_TAG_enumeration_type:
fa4028e9
JB
5887 /* Skip external references. The DWARF standard says in the section
5888 about "Structure, Union, and Class Type Entries": "An incomplete
5889 structure, union or class type is represented by a structure,
5890 union or class entry that does not have a byte size attribute
5891 and that has a DW_AT_declaration attribute." */
5892 if (!pdi->has_byte_size && pdi->is_declaration)
decbce07
MS
5893 {
5894 if (built_actual_name)
5895 xfree (actual_name);
5896 return;
5897 }
fa4028e9 5898
63d06c5c
DC
5899 /* NOTE: carlton/2003-10-07: See comment in new_symbol about
5900 static vs. global. */
38d518c9 5901 add_psymbol_to_list (actual_name, strlen (actual_name),
04a679b8 5902 built_actual_name,
176620f1 5903 STRUCT_DOMAIN, LOC_TYPEDEF,
987504bb
JJ
5904 (cu->language == language_cplus
5905 || cu->language == language_java)
63d06c5c
DC
5906 ? &objfile->global_psymbols
5907 : &objfile->static_psymbols,
e142c38c 5908 0, (CORE_ADDR) 0, cu->language, objfile);
c906108c 5909
c906108c
SS
5910 break;
5911 case DW_TAG_enumerator:
38d518c9 5912 add_psymbol_to_list (actual_name, strlen (actual_name),
04a679b8 5913 built_actual_name,
176620f1 5914 VAR_DOMAIN, LOC_CONST,
987504bb
JJ
5915 (cu->language == language_cplus
5916 || cu->language == language_java)
f6fe98ef
DJ
5917 ? &objfile->global_psymbols
5918 : &objfile->static_psymbols,
e142c38c 5919 0, (CORE_ADDR) 0, cu->language, objfile);
c906108c
SS
5920 break;
5921 default:
5922 break;
5923 }
5c4e30ca 5924
72bf9492
DJ
5925 if (built_actual_name)
5926 xfree (actual_name);
c906108c
SS
5927}
5928
5c4e30ca
DC
5929/* Read a partial die corresponding to a namespace; also, add a symbol
5930 corresponding to that namespace to the symbol table. NAMESPACE is
5931 the name of the enclosing namespace. */
91c24f0a 5932
72bf9492
DJ
5933static void
5934add_partial_namespace (struct partial_die_info *pdi,
91c24f0a 5935 CORE_ADDR *lowpc, CORE_ADDR *highpc,
5734ee8b 5936 int need_pc, struct dwarf2_cu *cu)
91c24f0a 5937{
72bf9492 5938 /* Add a symbol for the namespace. */
e7c27a73 5939
72bf9492 5940 add_partial_symbol (pdi, cu);
5c4e30ca
DC
5941
5942 /* Now scan partial symbols in that namespace. */
5943
91c24f0a 5944 if (pdi->has_children)
5734ee8b 5945 scan_partial_symbols (pdi->die_child, lowpc, highpc, need_pc, cu);
91c24f0a
DC
5946}
5947
5d7cb8df
JK
5948/* Read a partial die corresponding to a Fortran module. */
5949
5950static void
5951add_partial_module (struct partial_die_info *pdi, CORE_ADDR *lowpc,
5952 CORE_ADDR *highpc, int need_pc, struct dwarf2_cu *cu)
5953{
f55ee35c 5954 /* Now scan partial symbols in that module. */
5d7cb8df
JK
5955
5956 if (pdi->has_children)
5957 scan_partial_symbols (pdi->die_child, lowpc, highpc, need_pc, cu);
5958}
5959
bc30ff58
JB
5960/* Read a partial die corresponding to a subprogram and create a partial
5961 symbol for that subprogram. When the CU language allows it, this
5962 routine also defines a partial symbol for each nested subprogram
5963 that this subprogram contains.
6e70227d 5964
bc30ff58
JB
5965 DIE my also be a lexical block, in which case we simply search
5966 recursively for suprograms defined inside that lexical block.
5967 Again, this is only performed when the CU language allows this
5968 type of definitions. */
5969
5970static void
5971add_partial_subprogram (struct partial_die_info *pdi,
5972 CORE_ADDR *lowpc, CORE_ADDR *highpc,
5734ee8b 5973 int need_pc, struct dwarf2_cu *cu)
bc30ff58
JB
5974{
5975 if (pdi->tag == DW_TAG_subprogram)
5976 {
5977 if (pdi->has_pc_info)
5978 {
5979 if (pdi->lowpc < *lowpc)
5980 *lowpc = pdi->lowpc;
5981 if (pdi->highpc > *highpc)
5982 *highpc = pdi->highpc;
5734ee8b
DJ
5983 if (need_pc)
5984 {
5985 CORE_ADDR baseaddr;
5986 struct objfile *objfile = cu->objfile;
5987
5988 baseaddr = ANOFFSET (objfile->section_offsets,
5989 SECT_OFF_TEXT (objfile));
5990 addrmap_set_empty (objfile->psymtabs_addrmap,
01637564
DE
5991 pdi->lowpc + baseaddr,
5992 pdi->highpc - 1 + baseaddr,
9291a0cd 5993 cu->per_cu->v.psymtab);
5734ee8b 5994 }
481860b3
GB
5995 }
5996
5997 if (pdi->has_pc_info || (!pdi->is_external && pdi->may_be_inlined))
5998 {
bc30ff58 5999 if (!pdi->is_declaration)
e8d05480
JB
6000 /* Ignore subprogram DIEs that do not have a name, they are
6001 illegal. Do not emit a complaint at this point, we will
6002 do so when we convert this psymtab into a symtab. */
6003 if (pdi->name)
6004 add_partial_symbol (pdi, cu);
bc30ff58
JB
6005 }
6006 }
6e70227d 6007
bc30ff58
JB
6008 if (! pdi->has_children)
6009 return;
6010
6011 if (cu->language == language_ada)
6012 {
6013 pdi = pdi->die_child;
6014 while (pdi != NULL)
6015 {
6016 fixup_partial_die (pdi, cu);
6017 if (pdi->tag == DW_TAG_subprogram
6018 || pdi->tag == DW_TAG_lexical_block)
5734ee8b 6019 add_partial_subprogram (pdi, lowpc, highpc, need_pc, cu);
bc30ff58
JB
6020 pdi = pdi->die_sibling;
6021 }
6022 }
6023}
6024
91c24f0a
DC
6025/* Read a partial die corresponding to an enumeration type. */
6026
72bf9492
DJ
6027static void
6028add_partial_enumeration (struct partial_die_info *enum_pdi,
6029 struct dwarf2_cu *cu)
91c24f0a 6030{
72bf9492 6031 struct partial_die_info *pdi;
91c24f0a
DC
6032
6033 if (enum_pdi->name != NULL)
72bf9492
DJ
6034 add_partial_symbol (enum_pdi, cu);
6035
6036 pdi = enum_pdi->die_child;
6037 while (pdi)
91c24f0a 6038 {
72bf9492 6039 if (pdi->tag != DW_TAG_enumerator || pdi->name == NULL)
e2e0b3e5 6040 complaint (&symfile_complaints, _("malformed enumerator DIE ignored"));
91c24f0a 6041 else
72bf9492
DJ
6042 add_partial_symbol (pdi, cu);
6043 pdi = pdi->die_sibling;
91c24f0a 6044 }
91c24f0a
DC
6045}
6046
6caca83c
CC
6047/* Return the initial uleb128 in the die at INFO_PTR. */
6048
6049static unsigned int
6050peek_abbrev_code (bfd *abfd, gdb_byte *info_ptr)
6051{
6052 unsigned int bytes_read;
6053
6054 return read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
6055}
6056
4bb7a0a7
DJ
6057/* Read the initial uleb128 in the die at INFO_PTR in compilation unit CU.
6058 Return the corresponding abbrev, or NULL if the number is zero (indicating
6059 an empty DIE). In either case *BYTES_READ will be set to the length of
6060 the initial number. */
6061
6062static struct abbrev_info *
fe1b8b76 6063peek_die_abbrev (gdb_byte *info_ptr, unsigned int *bytes_read,
891d2f0b 6064 struct dwarf2_cu *cu)
4bb7a0a7
DJ
6065{
6066 bfd *abfd = cu->objfile->obfd;
6067 unsigned int abbrev_number;
6068 struct abbrev_info *abbrev;
6069
6070 abbrev_number = read_unsigned_leb128 (abfd, info_ptr, bytes_read);
6071
6072 if (abbrev_number == 0)
6073 return NULL;
6074
433df2d4 6075 abbrev = abbrev_table_lookup_abbrev (cu->abbrev_table, abbrev_number);
4bb7a0a7
DJ
6076 if (!abbrev)
6077 {
3e43a32a
MS
6078 error (_("Dwarf Error: Could not find abbrev number %d [in module %s]"),
6079 abbrev_number, bfd_get_filename (abfd));
4bb7a0a7
DJ
6080 }
6081
6082 return abbrev;
6083}
6084
93311388
DE
6085/* Scan the debug information for CU starting at INFO_PTR in buffer BUFFER.
6086 Returns a pointer to the end of a series of DIEs, terminated by an empty
4bb7a0a7
DJ
6087 DIE. Any children of the skipped DIEs will also be skipped. */
6088
fe1b8b76 6089static gdb_byte *
dee91e82 6090skip_children (const struct die_reader_specs *reader, gdb_byte *info_ptr)
4bb7a0a7 6091{
dee91e82 6092 struct dwarf2_cu *cu = reader->cu;
4bb7a0a7
DJ
6093 struct abbrev_info *abbrev;
6094 unsigned int bytes_read;
6095
6096 while (1)
6097 {
6098 abbrev = peek_die_abbrev (info_ptr, &bytes_read, cu);
6099 if (abbrev == NULL)
6100 return info_ptr + bytes_read;
6101 else
dee91e82 6102 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
4bb7a0a7
DJ
6103 }
6104}
6105
93311388
DE
6106/* Scan the debug information for CU starting at INFO_PTR in buffer BUFFER.
6107 INFO_PTR should point just after the initial uleb128 of a DIE, and the
4bb7a0a7
DJ
6108 abbrev corresponding to that skipped uleb128 should be passed in
6109 ABBREV. Returns a pointer to this DIE's sibling, skipping any
6110 children. */
6111
fe1b8b76 6112static gdb_byte *
dee91e82
DE
6113skip_one_die (const struct die_reader_specs *reader, gdb_byte *info_ptr,
6114 struct abbrev_info *abbrev)
4bb7a0a7
DJ
6115{
6116 unsigned int bytes_read;
6117 struct attribute attr;
dee91e82
DE
6118 bfd *abfd = reader->abfd;
6119 struct dwarf2_cu *cu = reader->cu;
6120 gdb_byte *buffer = reader->buffer;
f664829e
DE
6121 const gdb_byte *buffer_end = reader->buffer_end;
6122 gdb_byte *start_info_ptr = info_ptr;
4bb7a0a7
DJ
6123 unsigned int form, i;
6124
6125 for (i = 0; i < abbrev->num_attrs; i++)
6126 {
6127 /* The only abbrev we care about is DW_AT_sibling. */
6128 if (abbrev->attrs[i].name == DW_AT_sibling)
6129 {
dee91e82 6130 read_attribute (reader, &attr, &abbrev->attrs[i], info_ptr);
4bb7a0a7 6131 if (attr.form == DW_FORM_ref_addr)
3e43a32a
MS
6132 complaint (&symfile_complaints,
6133 _("ignoring absolute DW_AT_sibling"));
4bb7a0a7 6134 else
b64f50a1 6135 return buffer + dwarf2_get_ref_die_offset (&attr).sect_off;
4bb7a0a7
DJ
6136 }
6137
6138 /* If it isn't DW_AT_sibling, skip this attribute. */
6139 form = abbrev->attrs[i].form;
6140 skip_attribute:
6141 switch (form)
6142 {
4bb7a0a7 6143 case DW_FORM_ref_addr:
ae411497
TT
6144 /* In DWARF 2, DW_FORM_ref_addr is address sized; in DWARF 3
6145 and later it is offset sized. */
6146 if (cu->header.version == 2)
6147 info_ptr += cu->header.addr_size;
6148 else
6149 info_ptr += cu->header.offset_size;
6150 break;
36586728
TT
6151 case DW_FORM_GNU_ref_alt:
6152 info_ptr += cu->header.offset_size;
6153 break;
ae411497 6154 case DW_FORM_addr:
4bb7a0a7
DJ
6155 info_ptr += cu->header.addr_size;
6156 break;
6157 case DW_FORM_data1:
6158 case DW_FORM_ref1:
6159 case DW_FORM_flag:
6160 info_ptr += 1;
6161 break;
2dc7f7b3
TT
6162 case DW_FORM_flag_present:
6163 break;
4bb7a0a7
DJ
6164 case DW_FORM_data2:
6165 case DW_FORM_ref2:
6166 info_ptr += 2;
6167 break;
6168 case DW_FORM_data4:
6169 case DW_FORM_ref4:
6170 info_ptr += 4;
6171 break;
6172 case DW_FORM_data8:
6173 case DW_FORM_ref8:
55f1336d 6174 case DW_FORM_ref_sig8:
4bb7a0a7
DJ
6175 info_ptr += 8;
6176 break;
6177 case DW_FORM_string:
9b1c24c8 6178 read_direct_string (abfd, info_ptr, &bytes_read);
4bb7a0a7
DJ
6179 info_ptr += bytes_read;
6180 break;
2dc7f7b3 6181 case DW_FORM_sec_offset:
4bb7a0a7 6182 case DW_FORM_strp:
36586728 6183 case DW_FORM_GNU_strp_alt:
4bb7a0a7
DJ
6184 info_ptr += cu->header.offset_size;
6185 break;
2dc7f7b3 6186 case DW_FORM_exprloc:
4bb7a0a7
DJ
6187 case DW_FORM_block:
6188 info_ptr += read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
6189 info_ptr += bytes_read;
6190 break;
6191 case DW_FORM_block1:
6192 info_ptr += 1 + read_1_byte (abfd, info_ptr);
6193 break;
6194 case DW_FORM_block2:
6195 info_ptr += 2 + read_2_bytes (abfd, info_ptr);
6196 break;
6197 case DW_FORM_block4:
6198 info_ptr += 4 + read_4_bytes (abfd, info_ptr);
6199 break;
6200 case DW_FORM_sdata:
6201 case DW_FORM_udata:
6202 case DW_FORM_ref_udata:
3019eac3
DE
6203 case DW_FORM_GNU_addr_index:
6204 case DW_FORM_GNU_str_index:
f664829e 6205 info_ptr = (gdb_byte *) safe_skip_leb128 (info_ptr, buffer_end);
4bb7a0a7
DJ
6206 break;
6207 case DW_FORM_indirect:
6208 form = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
6209 info_ptr += bytes_read;
6210 /* We need to continue parsing from here, so just go back to
6211 the top. */
6212 goto skip_attribute;
6213
6214 default:
3e43a32a
MS
6215 error (_("Dwarf Error: Cannot handle %s "
6216 "in DWARF reader [in module %s]"),
4bb7a0a7
DJ
6217 dwarf_form_name (form),
6218 bfd_get_filename (abfd));
6219 }
6220 }
6221
6222 if (abbrev->has_children)
dee91e82 6223 return skip_children (reader, info_ptr);
4bb7a0a7
DJ
6224 else
6225 return info_ptr;
6226}
6227
93311388 6228/* Locate ORIG_PDI's sibling.
dee91e82 6229 INFO_PTR should point to the start of the next DIE after ORIG_PDI. */
91c24f0a 6230
fe1b8b76 6231static gdb_byte *
dee91e82
DE
6232locate_pdi_sibling (const struct die_reader_specs *reader,
6233 struct partial_die_info *orig_pdi,
6234 gdb_byte *info_ptr)
91c24f0a
DC
6235{
6236 /* Do we know the sibling already? */
72bf9492 6237
91c24f0a
DC
6238 if (orig_pdi->sibling)
6239 return orig_pdi->sibling;
6240
6241 /* Are there any children to deal with? */
6242
6243 if (!orig_pdi->has_children)
6244 return info_ptr;
6245
4bb7a0a7 6246 /* Skip the children the long way. */
91c24f0a 6247
dee91e82 6248 return skip_children (reader, info_ptr);
91c24f0a
DC
6249}
6250
c906108c
SS
6251/* Expand this partial symbol table into a full symbol table. */
6252
6253static void
fba45db2 6254dwarf2_psymtab_to_symtab (struct partial_symtab *pst)
c906108c 6255{
c906108c
SS
6256 if (pst != NULL)
6257 {
6258 if (pst->readin)
6259 {
3e43a32a
MS
6260 warning (_("bug: psymtab for %s is already read in."),
6261 pst->filename);
c906108c
SS
6262 }
6263 else
6264 {
6265 if (info_verbose)
6266 {
3e43a32a
MS
6267 printf_filtered (_("Reading in symbols for %s..."),
6268 pst->filename);
c906108c
SS
6269 gdb_flush (gdb_stdout);
6270 }
6271
10b3939b
DJ
6272 /* Restore our global data. */
6273 dwarf2_per_objfile = objfile_data (pst->objfile,
6274 dwarf2_objfile_data_key);
6275
b2ab525c
KB
6276 /* If this psymtab is constructed from a debug-only objfile, the
6277 has_section_at_zero flag will not necessarily be correct. We
6278 can get the correct value for this flag by looking at the data
6279 associated with the (presumably stripped) associated objfile. */
6280 if (pst->objfile->separate_debug_objfile_backlink)
6281 {
6282 struct dwarf2_per_objfile *dpo_backlink
6283 = objfile_data (pst->objfile->separate_debug_objfile_backlink,
6284 dwarf2_objfile_data_key);
9a619af0 6285
b2ab525c
KB
6286 dwarf2_per_objfile->has_section_at_zero
6287 = dpo_backlink->has_section_at_zero;
6288 }
6289
98bfdba5
PA
6290 dwarf2_per_objfile->reading_partial_symbols = 0;
6291
c906108c
SS
6292 psymtab_to_symtab_1 (pst);
6293
6294 /* Finish up the debug error message. */
6295 if (info_verbose)
a3f17187 6296 printf_filtered (_("done.\n"));
c906108c
SS
6297 }
6298 }
95554aad
TT
6299
6300 process_cu_includes ();
c906108c 6301}
9cdd5dbd
DE
6302\f
6303/* Reading in full CUs. */
c906108c 6304
10b3939b
DJ
6305/* Add PER_CU to the queue. */
6306
6307static void
95554aad
TT
6308queue_comp_unit (struct dwarf2_per_cu_data *per_cu,
6309 enum language pretend_language)
10b3939b
DJ
6310{
6311 struct dwarf2_queue_item *item;
6312
6313 per_cu->queued = 1;
6314 item = xmalloc (sizeof (*item));
6315 item->per_cu = per_cu;
95554aad 6316 item->pretend_language = pretend_language;
10b3939b
DJ
6317 item->next = NULL;
6318
6319 if (dwarf2_queue == NULL)
6320 dwarf2_queue = item;
6321 else
6322 dwarf2_queue_tail->next = item;
6323
6324 dwarf2_queue_tail = item;
6325}
6326
0907af0c
DE
6327/* THIS_CU has a reference to PER_CU. If necessary, load the new compilation
6328 unit and add it to our queue.
6329 The result is non-zero if PER_CU was queued, otherwise the result is zero
6330 meaning either PER_CU is already queued or it is already loaded. */
6331
6332static int
6333maybe_queue_comp_unit (struct dwarf2_cu *this_cu,
6334 struct dwarf2_per_cu_data *per_cu,
6335 enum language pretend_language)
6336{
6337 /* We may arrive here during partial symbol reading, if we need full
6338 DIEs to process an unusual case (e.g. template arguments). Do
6339 not queue PER_CU, just tell our caller to load its DIEs. */
6340 if (dwarf2_per_objfile->reading_partial_symbols)
6341 {
6342 if (per_cu->cu == NULL || per_cu->cu->dies == NULL)
6343 return 1;
6344 return 0;
6345 }
6346
6347 /* Mark the dependence relation so that we don't flush PER_CU
6348 too early. */
6349 dwarf2_add_dependence (this_cu, per_cu);
6350
6351 /* If it's already on the queue, we have nothing to do. */
6352 if (per_cu->queued)
6353 return 0;
6354
6355 /* If the compilation unit is already loaded, just mark it as
6356 used. */
6357 if (per_cu->cu != NULL)
6358 {
6359 per_cu->cu->last_used = 0;
6360 return 0;
6361 }
6362
6363 /* Add it to the queue. */
6364 queue_comp_unit (per_cu, pretend_language);
6365
6366 return 1;
6367}
6368
10b3939b
DJ
6369/* Process the queue. */
6370
6371static void
a0f42c21 6372process_queue (void)
10b3939b
DJ
6373{
6374 struct dwarf2_queue_item *item, *next_item;
6375
45cfd468
DE
6376 if (dwarf2_read_debug)
6377 {
6378 fprintf_unfiltered (gdb_stdlog,
6379 "Expanding one or more symtabs of objfile %s ...\n",
6380 dwarf2_per_objfile->objfile->name);
6381 }
6382
03dd20cc
DJ
6383 /* The queue starts out with one item, but following a DIE reference
6384 may load a new CU, adding it to the end of the queue. */
10b3939b
DJ
6385 for (item = dwarf2_queue; item != NULL; dwarf2_queue = item = next_item)
6386 {
9291a0cd
TT
6387 if (dwarf2_per_objfile->using_index
6388 ? !item->per_cu->v.quick->symtab
6389 : (item->per_cu->v.psymtab && !item->per_cu->v.psymtab->readin))
f4dc4d17
DE
6390 {
6391 struct dwarf2_per_cu_data *per_cu = item->per_cu;
6392
6393 if (dwarf2_read_debug)
6394 {
6395 fprintf_unfiltered (gdb_stdlog,
6396 "Expanding symtab of %s at offset 0x%x\n",
6397 per_cu->is_debug_types ? "TU" : "CU",
6398 per_cu->offset.sect_off);
6399 }
6400
6401 if (per_cu->is_debug_types)
6402 process_full_type_unit (per_cu, item->pretend_language);
6403 else
6404 process_full_comp_unit (per_cu, item->pretend_language);
6405
6406 if (dwarf2_read_debug)
6407 {
6408 fprintf_unfiltered (gdb_stdlog,
6409 "Done expanding %s at offset 0x%x\n",
6410 per_cu->is_debug_types ? "TU" : "CU",
6411 per_cu->offset.sect_off);
6412 }
6413 }
10b3939b
DJ
6414
6415 item->per_cu->queued = 0;
6416 next_item = item->next;
6417 xfree (item);
6418 }
6419
6420 dwarf2_queue_tail = NULL;
45cfd468
DE
6421
6422 if (dwarf2_read_debug)
6423 {
6424 fprintf_unfiltered (gdb_stdlog, "Done expanding symtabs of %s.\n",
6425 dwarf2_per_objfile->objfile->name);
6426 }
10b3939b
DJ
6427}
6428
6429/* Free all allocated queue entries. This function only releases anything if
6430 an error was thrown; if the queue was processed then it would have been
6431 freed as we went along. */
6432
6433static void
6434dwarf2_release_queue (void *dummy)
6435{
6436 struct dwarf2_queue_item *item, *last;
6437
6438 item = dwarf2_queue;
6439 while (item)
6440 {
6441 /* Anything still marked queued is likely to be in an
6442 inconsistent state, so discard it. */
6443 if (item->per_cu->queued)
6444 {
6445 if (item->per_cu->cu != NULL)
dee91e82 6446 free_one_cached_comp_unit (item->per_cu);
10b3939b
DJ
6447 item->per_cu->queued = 0;
6448 }
6449
6450 last = item;
6451 item = item->next;
6452 xfree (last);
6453 }
6454
6455 dwarf2_queue = dwarf2_queue_tail = NULL;
6456}
6457
6458/* Read in full symbols for PST, and anything it depends on. */
6459
c906108c 6460static void
fba45db2 6461psymtab_to_symtab_1 (struct partial_symtab *pst)
c906108c 6462{
10b3939b 6463 struct dwarf2_per_cu_data *per_cu;
aaa75496
JB
6464 int i;
6465
95554aad
TT
6466 if (pst->readin)
6467 return;
6468
aaa75496 6469 for (i = 0; i < pst->number_of_dependencies; i++)
95554aad
TT
6470 if (!pst->dependencies[i]->readin
6471 && pst->dependencies[i]->user == NULL)
aaa75496
JB
6472 {
6473 /* Inform about additional files that need to be read in. */
6474 if (info_verbose)
6475 {
a3f17187 6476 /* FIXME: i18n: Need to make this a single string. */
aaa75496
JB
6477 fputs_filtered (" ", gdb_stdout);
6478 wrap_here ("");
6479 fputs_filtered ("and ", gdb_stdout);
6480 wrap_here ("");
6481 printf_filtered ("%s...", pst->dependencies[i]->filename);
0963b4bd 6482 wrap_here (""); /* Flush output. */
aaa75496
JB
6483 gdb_flush (gdb_stdout);
6484 }
6485 psymtab_to_symtab_1 (pst->dependencies[i]);
6486 }
6487
e38df1d0 6488 per_cu = pst->read_symtab_private;
10b3939b
DJ
6489
6490 if (per_cu == NULL)
aaa75496
JB
6491 {
6492 /* It's an include file, no symbols to read for it.
6493 Everything is in the parent symtab. */
6494 pst->readin = 1;
6495 return;
6496 }
c906108c 6497
a0f42c21 6498 dw2_do_instantiate_symtab (per_cu);
10b3939b
DJ
6499}
6500
dee91e82
DE
6501/* Trivial hash function for die_info: the hash value of a DIE
6502 is its offset in .debug_info for this objfile. */
10b3939b 6503
dee91e82
DE
6504static hashval_t
6505die_hash (const void *item)
10b3939b 6506{
dee91e82 6507 const struct die_info *die = item;
6502dd73 6508
dee91e82
DE
6509 return die->offset.sect_off;
6510}
63d06c5c 6511
dee91e82
DE
6512/* Trivial comparison function for die_info structures: two DIEs
6513 are equal if they have the same offset. */
98bfdba5 6514
dee91e82
DE
6515static int
6516die_eq (const void *item_lhs, const void *item_rhs)
6517{
6518 const struct die_info *die_lhs = item_lhs;
6519 const struct die_info *die_rhs = item_rhs;
c906108c 6520
dee91e82
DE
6521 return die_lhs->offset.sect_off == die_rhs->offset.sect_off;
6522}
c906108c 6523
dee91e82
DE
6524/* die_reader_func for load_full_comp_unit.
6525 This is identical to read_signatured_type_reader,
6526 but is kept separate for now. */
c906108c 6527
dee91e82
DE
6528static void
6529load_full_comp_unit_reader (const struct die_reader_specs *reader,
6530 gdb_byte *info_ptr,
6531 struct die_info *comp_unit_die,
6532 int has_children,
6533 void *data)
6534{
6535 struct dwarf2_cu *cu = reader->cu;
95554aad 6536 enum language *language_ptr = data;
6caca83c 6537
dee91e82
DE
6538 gdb_assert (cu->die_hash == NULL);
6539 cu->die_hash =
6540 htab_create_alloc_ex (cu->header.length / 12,
6541 die_hash,
6542 die_eq,
6543 NULL,
6544 &cu->comp_unit_obstack,
6545 hashtab_obstack_allocate,
6546 dummy_obstack_deallocate);
e142c38c 6547
dee91e82
DE
6548 if (has_children)
6549 comp_unit_die->child = read_die_and_siblings (reader, info_ptr,
6550 &info_ptr, comp_unit_die);
6551 cu->dies = comp_unit_die;
6552 /* comp_unit_die is not stored in die_hash, no need. */
10b3939b
DJ
6553
6554 /* We try not to read any attributes in this function, because not
9cdd5dbd 6555 all CUs needed for references have been loaded yet, and symbol
10b3939b 6556 table processing isn't initialized. But we have to set the CU language,
dee91e82
DE
6557 or we won't be able to build types correctly.
6558 Similarly, if we do not read the producer, we can not apply
6559 producer-specific interpretation. */
95554aad 6560 prepare_one_comp_unit (cu, cu->dies, *language_ptr);
dee91e82 6561}
10b3939b 6562
dee91e82 6563/* Load the DIEs associated with PER_CU into memory. */
a6c727b2 6564
dee91e82 6565static void
95554aad
TT
6566load_full_comp_unit (struct dwarf2_per_cu_data *this_cu,
6567 enum language pretend_language)
dee91e82 6568{
3019eac3 6569 gdb_assert (! this_cu->is_debug_types);
c5b7e1cb 6570
f4dc4d17
DE
6571 init_cutu_and_read_dies (this_cu, NULL, 1, 1,
6572 load_full_comp_unit_reader, &pretend_language);
10b3939b
DJ
6573}
6574
3da10d80
KS
6575/* Add a DIE to the delayed physname list. */
6576
6577static void
6578add_to_method_list (struct type *type, int fnfield_index, int index,
6579 const char *name, struct die_info *die,
6580 struct dwarf2_cu *cu)
6581{
6582 struct delayed_method_info mi;
6583 mi.type = type;
6584 mi.fnfield_index = fnfield_index;
6585 mi.index = index;
6586 mi.name = name;
6587 mi.die = die;
6588 VEC_safe_push (delayed_method_info, cu->method_list, &mi);
6589}
6590
6591/* A cleanup for freeing the delayed method list. */
6592
6593static void
6594free_delayed_list (void *ptr)
6595{
6596 struct dwarf2_cu *cu = (struct dwarf2_cu *) ptr;
6597 if (cu->method_list != NULL)
6598 {
6599 VEC_free (delayed_method_info, cu->method_list);
6600 cu->method_list = NULL;
6601 }
6602}
6603
6604/* Compute the physnames of any methods on the CU's method list.
6605
6606 The computation of method physnames is delayed in order to avoid the
6607 (bad) condition that one of the method's formal parameters is of an as yet
6608 incomplete type. */
6609
6610static void
6611compute_delayed_physnames (struct dwarf2_cu *cu)
6612{
6613 int i;
6614 struct delayed_method_info *mi;
6615 for (i = 0; VEC_iterate (delayed_method_info, cu->method_list, i, mi) ; ++i)
6616 {
1d06ead6 6617 const char *physname;
3da10d80
KS
6618 struct fn_fieldlist *fn_flp
6619 = &TYPE_FN_FIELDLIST (mi->type, mi->fnfield_index);
1d06ead6 6620 physname = dwarf2_physname ((char *) mi->name, mi->die, cu);
3da10d80
KS
6621 fn_flp->fn_fields[mi->index].physname = physname ? physname : "";
6622 }
6623}
6624
a766d390
DE
6625/* Go objects should be embedded in a DW_TAG_module DIE,
6626 and it's not clear if/how imported objects will appear.
6627 To keep Go support simple until that's worked out,
6628 go back through what we've read and create something usable.
6629 We could do this while processing each DIE, and feels kinda cleaner,
6630 but that way is more invasive.
6631 This is to, for example, allow the user to type "p var" or "b main"
6632 without having to specify the package name, and allow lookups
6633 of module.object to work in contexts that use the expression
6634 parser. */
6635
6636static void
6637fixup_go_packaging (struct dwarf2_cu *cu)
6638{
6639 char *package_name = NULL;
6640 struct pending *list;
6641 int i;
6642
6643 for (list = global_symbols; list != NULL; list = list->next)
6644 {
6645 for (i = 0; i < list->nsyms; ++i)
6646 {
6647 struct symbol *sym = list->symbol[i];
6648
6649 if (SYMBOL_LANGUAGE (sym) == language_go
6650 && SYMBOL_CLASS (sym) == LOC_BLOCK)
6651 {
6652 char *this_package_name = go_symbol_package_name (sym);
6653
6654 if (this_package_name == NULL)
6655 continue;
6656 if (package_name == NULL)
6657 package_name = this_package_name;
6658 else
6659 {
6660 if (strcmp (package_name, this_package_name) != 0)
6661 complaint (&symfile_complaints,
6662 _("Symtab %s has objects from two different Go packages: %s and %s"),
6663 (sym->symtab && sym->symtab->filename
6664 ? sym->symtab->filename
6665 : cu->objfile->name),
6666 this_package_name, package_name);
6667 xfree (this_package_name);
6668 }
6669 }
6670 }
6671 }
6672
6673 if (package_name != NULL)
6674 {
6675 struct objfile *objfile = cu->objfile;
6676 struct type *type = init_type (TYPE_CODE_MODULE, 0, 0,
6677 package_name, objfile);
6678 struct symbol *sym;
6679
6680 TYPE_TAG_NAME (type) = TYPE_NAME (type);
6681
6682 sym = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct symbol);
6683 SYMBOL_SET_LANGUAGE (sym, language_go);
6684 SYMBOL_SET_NAMES (sym, package_name, strlen (package_name), 1, objfile);
6685 /* This is not VAR_DOMAIN because we want a way to ensure a lookup of,
6686 e.g., "main" finds the "main" module and not C's main(). */
6687 SYMBOL_DOMAIN (sym) = STRUCT_DOMAIN;
6688 SYMBOL_CLASS (sym) = LOC_TYPEDEF;
6689 SYMBOL_TYPE (sym) = type;
6690
6691 add_symbol_to_list (sym, &global_symbols);
6692
6693 xfree (package_name);
6694 }
6695}
6696
95554aad
TT
6697static void compute_symtab_includes (struct dwarf2_per_cu_data *per_cu);
6698
6699/* Return the symtab for PER_CU. This works properly regardless of
6700 whether we're using the index or psymtabs. */
6701
6702static struct symtab *
6703get_symtab (struct dwarf2_per_cu_data *per_cu)
6704{
6705 return (dwarf2_per_objfile->using_index
6706 ? per_cu->v.quick->symtab
6707 : per_cu->v.psymtab->symtab);
6708}
6709
6710/* A helper function for computing the list of all symbol tables
6711 included by PER_CU. */
6712
6713static void
6714recursively_compute_inclusions (VEC (dwarf2_per_cu_ptr) **result,
6715 htab_t all_children,
6716 struct dwarf2_per_cu_data *per_cu)
6717{
6718 void **slot;
6719 int ix;
6720 struct dwarf2_per_cu_data *iter;
6721
6722 slot = htab_find_slot (all_children, per_cu, INSERT);
6723 if (*slot != NULL)
6724 {
6725 /* This inclusion and its children have been processed. */
6726 return;
6727 }
6728
6729 *slot = per_cu;
6730 /* Only add a CU if it has a symbol table. */
6731 if (get_symtab (per_cu) != NULL)
6732 VEC_safe_push (dwarf2_per_cu_ptr, *result, per_cu);
6733
6734 for (ix = 0;
f4dc4d17 6735 VEC_iterate (dwarf2_per_cu_ptr, per_cu->s.imported_symtabs, ix, iter);
95554aad
TT
6736 ++ix)
6737 recursively_compute_inclusions (result, all_children, iter);
6738}
6739
6740/* Compute the symtab 'includes' fields for the symtab related to
6741 PER_CU. */
6742
6743static void
6744compute_symtab_includes (struct dwarf2_per_cu_data *per_cu)
6745{
f4dc4d17
DE
6746 gdb_assert (! per_cu->is_debug_types);
6747
6748 if (!VEC_empty (dwarf2_per_cu_ptr, per_cu->s.imported_symtabs))
95554aad
TT
6749 {
6750 int ix, len;
6751 struct dwarf2_per_cu_data *iter;
6752 VEC (dwarf2_per_cu_ptr) *result_children = NULL;
6753 htab_t all_children;
6754 struct symtab *symtab = get_symtab (per_cu);
6755
6756 /* If we don't have a symtab, we can just skip this case. */
6757 if (symtab == NULL)
6758 return;
6759
6760 all_children = htab_create_alloc (1, htab_hash_pointer, htab_eq_pointer,
6761 NULL, xcalloc, xfree);
6762
6763 for (ix = 0;
f4dc4d17 6764 VEC_iterate (dwarf2_per_cu_ptr, per_cu->s.imported_symtabs,
95554aad
TT
6765 ix, iter);
6766 ++ix)
6767 recursively_compute_inclusions (&result_children, all_children, iter);
6768
6769 /* Now we have a transitive closure of all the included CUs, so
6770 we can convert it to a list of symtabs. */
6771 len = VEC_length (dwarf2_per_cu_ptr, result_children);
6772 symtab->includes
6773 = obstack_alloc (&dwarf2_per_objfile->objfile->objfile_obstack,
6774 (len + 1) * sizeof (struct symtab *));
6775 for (ix = 0;
6776 VEC_iterate (dwarf2_per_cu_ptr, result_children, ix, iter);
6777 ++ix)
6778 symtab->includes[ix] = get_symtab (iter);
6779 symtab->includes[len] = NULL;
6780
6781 VEC_free (dwarf2_per_cu_ptr, result_children);
6782 htab_delete (all_children);
6783 }
6784}
6785
6786/* Compute the 'includes' field for the symtabs of all the CUs we just
6787 read. */
6788
6789static void
6790process_cu_includes (void)
6791{
6792 int ix;
6793 struct dwarf2_per_cu_data *iter;
6794
6795 for (ix = 0;
6796 VEC_iterate (dwarf2_per_cu_ptr, dwarf2_per_objfile->just_read_cus,
6797 ix, iter);
6798 ++ix)
f4dc4d17
DE
6799 {
6800 if (! iter->is_debug_types)
6801 compute_symtab_includes (iter);
6802 }
95554aad
TT
6803
6804 VEC_free (dwarf2_per_cu_ptr, dwarf2_per_objfile->just_read_cus);
6805}
6806
9cdd5dbd 6807/* Generate full symbol information for PER_CU, whose DIEs have
10b3939b
DJ
6808 already been loaded into memory. */
6809
6810static void
95554aad
TT
6811process_full_comp_unit (struct dwarf2_per_cu_data *per_cu,
6812 enum language pretend_language)
10b3939b 6813{
10b3939b 6814 struct dwarf2_cu *cu = per_cu->cu;
9291a0cd 6815 struct objfile *objfile = per_cu->objfile;
10b3939b
DJ
6816 CORE_ADDR lowpc, highpc;
6817 struct symtab *symtab;
3da10d80 6818 struct cleanup *back_to, *delayed_list_cleanup;
10b3939b 6819 CORE_ADDR baseaddr;
4359dff1 6820 struct block *static_block;
10b3939b
DJ
6821
6822 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
6823
10b3939b
DJ
6824 buildsym_init ();
6825 back_to = make_cleanup (really_free_pendings, NULL);
3da10d80 6826 delayed_list_cleanup = make_cleanup (free_delayed_list, cu);
10b3939b
DJ
6827
6828 cu->list_in_scope = &file_symbols;
c906108c 6829
95554aad
TT
6830 cu->language = pretend_language;
6831 cu->language_defn = language_def (cu->language);
6832
c906108c 6833 /* Do line number decoding in read_file_scope () */
10b3939b 6834 process_die (cu->dies, cu);
c906108c 6835
a766d390
DE
6836 /* For now fudge the Go package. */
6837 if (cu->language == language_go)
6838 fixup_go_packaging (cu);
6839
3da10d80
KS
6840 /* Now that we have processed all the DIEs in the CU, all the types
6841 should be complete, and it should now be safe to compute all of the
6842 physnames. */
6843 compute_delayed_physnames (cu);
6844 do_cleanups (delayed_list_cleanup);
6845
fae299cd
DC
6846 /* Some compilers don't define a DW_AT_high_pc attribute for the
6847 compilation unit. If the DW_AT_high_pc is missing, synthesize
6848 it, by scanning the DIE's below the compilation unit. */
10b3939b 6849 get_scope_pc_bounds (cu->dies, &lowpc, &highpc, cu);
c906108c 6850
36586728
TT
6851 static_block
6852 = end_symtab_get_static_block (highpc + baseaddr, objfile, 0,
6853 per_cu->s.imported_symtabs != NULL);
4359dff1
JK
6854
6855 /* If the comp unit has DW_AT_ranges, it may have discontiguous ranges.
6856 Also, DW_AT_ranges may record ranges not belonging to any child DIEs
6857 (such as virtual method tables). Record the ranges in STATIC_BLOCK's
6858 addrmap to help ensure it has an accurate map of pc values belonging to
6859 this comp unit. */
6860 dwarf2_record_block_ranges (cu->dies, static_block, baseaddr, cu);
6861
6862 symtab = end_symtab_from_static_block (static_block, objfile,
6863 SECT_OFF_TEXT (objfile), 0);
c906108c 6864
8be455d7 6865 if (symtab != NULL)
c906108c 6866 {
df15bd07 6867 int gcc_4_minor = producer_is_gcc_ge_4 (cu->producer);
4632c0d0 6868
8be455d7
JK
6869 /* Set symtab language to language from DW_AT_language. If the
6870 compilation is from a C file generated by language preprocessors, do
6871 not set the language if it was already deduced by start_subfile. */
6872 if (!(cu->language == language_c && symtab->language != language_c))
6873 symtab->language = cu->language;
6874
6875 /* GCC-4.0 has started to support -fvar-tracking. GCC-3.x still can
6876 produce DW_AT_location with location lists but it can be possibly
ab260dad
JK
6877 invalid without -fvar-tracking. Still up to GCC-4.4.x incl. 4.4.0
6878 there were bugs in prologue debug info, fixed later in GCC-4.5
6879 by "unwind info for epilogues" patch (which is not directly related).
8be455d7
JK
6880
6881 For -gdwarf-4 type units LOCATIONS_VALID indication is fortunately not
6882 needed, it would be wrong due to missing DW_AT_producer there.
6883
6884 Still one can confuse GDB by using non-standard GCC compilation
6885 options - this waits on GCC PR other/32998 (-frecord-gcc-switches).
6886 */
ab260dad 6887 if (cu->has_loclist && gcc_4_minor >= 5)
8be455d7 6888 symtab->locations_valid = 1;
e0d00bc7
JK
6889
6890 if (gcc_4_minor >= 5)
6891 symtab->epilogue_unwind_valid = 1;
96408a79
SA
6892
6893 symtab->call_site_htab = cu->call_site_htab;
c906108c 6894 }
9291a0cd
TT
6895
6896 if (dwarf2_per_objfile->using_index)
6897 per_cu->v.quick->symtab = symtab;
6898 else
6899 {
6900 struct partial_symtab *pst = per_cu->v.psymtab;
6901 pst->symtab = symtab;
6902 pst->readin = 1;
6903 }
c906108c 6904
95554aad
TT
6905 /* Push it for inclusion processing later. */
6906 VEC_safe_push (dwarf2_per_cu_ptr, dwarf2_per_objfile->just_read_cus, per_cu);
6907
c906108c 6908 do_cleanups (back_to);
f4dc4d17 6909}
45cfd468 6910
f4dc4d17
DE
6911/* Generate full symbol information for type unit PER_CU, whose DIEs have
6912 already been loaded into memory. */
6913
6914static void
6915process_full_type_unit (struct dwarf2_per_cu_data *per_cu,
6916 enum language pretend_language)
6917{
6918 struct dwarf2_cu *cu = per_cu->cu;
6919 struct objfile *objfile = per_cu->objfile;
6920 struct symtab *symtab;
6921 struct cleanup *back_to, *delayed_list_cleanup;
6922
6923 buildsym_init ();
6924 back_to = make_cleanup (really_free_pendings, NULL);
6925 delayed_list_cleanup = make_cleanup (free_delayed_list, cu);
6926
6927 cu->list_in_scope = &file_symbols;
6928
6929 cu->language = pretend_language;
6930 cu->language_defn = language_def (cu->language);
6931
6932 /* The symbol tables are set up in read_type_unit_scope. */
6933 process_die (cu->dies, cu);
6934
6935 /* For now fudge the Go package. */
6936 if (cu->language == language_go)
6937 fixup_go_packaging (cu);
6938
6939 /* Now that we have processed all the DIEs in the CU, all the types
6940 should be complete, and it should now be safe to compute all of the
6941 physnames. */
6942 compute_delayed_physnames (cu);
6943 do_cleanups (delayed_list_cleanup);
6944
6945 /* TUs share symbol tables.
6946 If this is the first TU to use this symtab, complete the construction
094b34ac
DE
6947 of it with end_expandable_symtab. Otherwise, complete the addition of
6948 this TU's symbols to the existing symtab. */
f4dc4d17 6949 if (per_cu->s.type_unit_group->primary_symtab == NULL)
45cfd468 6950 {
f4dc4d17
DE
6951 symtab = end_expandable_symtab (0, objfile, SECT_OFF_TEXT (objfile));
6952 per_cu->s.type_unit_group->primary_symtab = symtab;
6953
6954 if (symtab != NULL)
6955 {
6956 /* Set symtab language to language from DW_AT_language. If the
6957 compilation is from a C file generated by language preprocessors,
6958 do not set the language if it was already deduced by
6959 start_subfile. */
6960 if (!(cu->language == language_c && symtab->language != language_c))
6961 symtab->language = cu->language;
6962 }
6963 }
6964 else
6965 {
6966 augment_type_symtab (objfile,
6967 per_cu->s.type_unit_group->primary_symtab);
6968 symtab = per_cu->s.type_unit_group->primary_symtab;
6969 }
6970
6971 if (dwarf2_per_objfile->using_index)
6972 per_cu->v.quick->symtab = symtab;
6973 else
6974 {
6975 struct partial_symtab *pst = per_cu->v.psymtab;
6976 pst->symtab = symtab;
6977 pst->readin = 1;
45cfd468 6978 }
f4dc4d17
DE
6979
6980 do_cleanups (back_to);
c906108c
SS
6981}
6982
95554aad
TT
6983/* Process an imported unit DIE. */
6984
6985static void
6986process_imported_unit_die (struct die_info *die, struct dwarf2_cu *cu)
6987{
6988 struct attribute *attr;
6989
f4dc4d17
DE
6990 /* For now we don't handle imported units in type units. */
6991 if (cu->per_cu->is_debug_types)
6992 {
6993 error (_("Dwarf Error: DW_TAG_imported_unit is not"
6994 " supported in type units [in module %s]"),
6995 cu->objfile->name);
6996 }
6997
95554aad
TT
6998 attr = dwarf2_attr (die, DW_AT_import, cu);
6999 if (attr != NULL)
7000 {
7001 struct dwarf2_per_cu_data *per_cu;
7002 struct symtab *imported_symtab;
7003 sect_offset offset;
36586728 7004 int is_dwz;
95554aad
TT
7005
7006 offset = dwarf2_get_ref_die_offset (attr);
36586728
TT
7007 is_dwz = (attr->form == DW_FORM_GNU_ref_alt || cu->per_cu->is_dwz);
7008 per_cu = dwarf2_find_containing_comp_unit (offset, is_dwz, cu->objfile);
95554aad
TT
7009
7010 /* Queue the unit, if needed. */
7011 if (maybe_queue_comp_unit (cu, per_cu, cu->language))
7012 load_full_comp_unit (per_cu, cu->language);
7013
f4dc4d17 7014 VEC_safe_push (dwarf2_per_cu_ptr, cu->per_cu->s.imported_symtabs,
95554aad
TT
7015 per_cu);
7016 }
7017}
7018
c906108c
SS
7019/* Process a die and its children. */
7020
7021static void
e7c27a73 7022process_die (struct die_info *die, struct dwarf2_cu *cu)
c906108c
SS
7023{
7024 switch (die->tag)
7025 {
7026 case DW_TAG_padding:
7027 break;
7028 case DW_TAG_compile_unit:
95554aad 7029 case DW_TAG_partial_unit:
e7c27a73 7030 read_file_scope (die, cu);
c906108c 7031 break;
348e048f
DE
7032 case DW_TAG_type_unit:
7033 read_type_unit_scope (die, cu);
7034 break;
c906108c 7035 case DW_TAG_subprogram:
c906108c 7036 case DW_TAG_inlined_subroutine:
edb3359d 7037 read_func_scope (die, cu);
c906108c
SS
7038 break;
7039 case DW_TAG_lexical_block:
14898363
L
7040 case DW_TAG_try_block:
7041 case DW_TAG_catch_block:
e7c27a73 7042 read_lexical_block_scope (die, cu);
c906108c 7043 break;
96408a79
SA
7044 case DW_TAG_GNU_call_site:
7045 read_call_site_scope (die, cu);
7046 break;
c906108c 7047 case DW_TAG_class_type:
680b30c7 7048 case DW_TAG_interface_type:
c906108c
SS
7049 case DW_TAG_structure_type:
7050 case DW_TAG_union_type:
134d01f1 7051 process_structure_scope (die, cu);
c906108c
SS
7052 break;
7053 case DW_TAG_enumeration_type:
134d01f1 7054 process_enumeration_scope (die, cu);
c906108c 7055 break;
134d01f1 7056
f792889a
DJ
7057 /* These dies have a type, but processing them does not create
7058 a symbol or recurse to process the children. Therefore we can
7059 read them on-demand through read_type_die. */
c906108c 7060 case DW_TAG_subroutine_type:
72019c9c 7061 case DW_TAG_set_type:
c906108c 7062 case DW_TAG_array_type:
c906108c 7063 case DW_TAG_pointer_type:
c906108c 7064 case DW_TAG_ptr_to_member_type:
c906108c 7065 case DW_TAG_reference_type:
c906108c 7066 case DW_TAG_string_type:
c906108c 7067 break;
134d01f1 7068
c906108c 7069 case DW_TAG_base_type:
a02abb62 7070 case DW_TAG_subrange_type:
cb249c71 7071 case DW_TAG_typedef:
134d01f1
DJ
7072 /* Add a typedef symbol for the type definition, if it has a
7073 DW_AT_name. */
f792889a 7074 new_symbol (die, read_type_die (die, cu), cu);
a02abb62 7075 break;
c906108c 7076 case DW_TAG_common_block:
e7c27a73 7077 read_common_block (die, cu);
c906108c
SS
7078 break;
7079 case DW_TAG_common_inclusion:
7080 break;
d9fa45fe 7081 case DW_TAG_namespace:
63d06c5c 7082 processing_has_namespace_info = 1;
e7c27a73 7083 read_namespace (die, cu);
d9fa45fe 7084 break;
5d7cb8df 7085 case DW_TAG_module:
f55ee35c 7086 processing_has_namespace_info = 1;
5d7cb8df
JK
7087 read_module (die, cu);
7088 break;
d9fa45fe
DC
7089 case DW_TAG_imported_declaration:
7090 case DW_TAG_imported_module:
63d06c5c 7091 processing_has_namespace_info = 1;
27aa8d6a
SW
7092 if (die->child != NULL && (die->tag == DW_TAG_imported_declaration
7093 || cu->language != language_fortran))
7094 complaint (&symfile_complaints, _("Tag '%s' has unexpected children"),
7095 dwarf_tag_name (die->tag));
7096 read_import_statement (die, cu);
d9fa45fe 7097 break;
95554aad
TT
7098
7099 case DW_TAG_imported_unit:
7100 process_imported_unit_die (die, cu);
7101 break;
7102
c906108c 7103 default:
e7c27a73 7104 new_symbol (die, NULL, cu);
c906108c
SS
7105 break;
7106 }
7107}
7108
94af9270
KS
7109/* A helper function for dwarf2_compute_name which determines whether DIE
7110 needs to have the name of the scope prepended to the name listed in the
7111 die. */
7112
7113static int
7114die_needs_namespace (struct die_info *die, struct dwarf2_cu *cu)
7115{
1c809c68
TT
7116 struct attribute *attr;
7117
94af9270
KS
7118 switch (die->tag)
7119 {
7120 case DW_TAG_namespace:
7121 case DW_TAG_typedef:
7122 case DW_TAG_class_type:
7123 case DW_TAG_interface_type:
7124 case DW_TAG_structure_type:
7125 case DW_TAG_union_type:
7126 case DW_TAG_enumeration_type:
7127 case DW_TAG_enumerator:
7128 case DW_TAG_subprogram:
7129 case DW_TAG_member:
7130 return 1;
7131
7132 case DW_TAG_variable:
c2b0a229 7133 case DW_TAG_constant:
94af9270
KS
7134 /* We only need to prefix "globally" visible variables. These include
7135 any variable marked with DW_AT_external or any variable that
7136 lives in a namespace. [Variables in anonymous namespaces
7137 require prefixing, but they are not DW_AT_external.] */
7138
7139 if (dwarf2_attr (die, DW_AT_specification, cu))
7140 {
7141 struct dwarf2_cu *spec_cu = cu;
9a619af0 7142
94af9270
KS
7143 return die_needs_namespace (die_specification (die, &spec_cu),
7144 spec_cu);
7145 }
7146
1c809c68 7147 attr = dwarf2_attr (die, DW_AT_external, cu);
f55ee35c
JK
7148 if (attr == NULL && die->parent->tag != DW_TAG_namespace
7149 && die->parent->tag != DW_TAG_module)
1c809c68
TT
7150 return 0;
7151 /* A variable in a lexical block of some kind does not need a
7152 namespace, even though in C++ such variables may be external
7153 and have a mangled name. */
7154 if (die->parent->tag == DW_TAG_lexical_block
7155 || die->parent->tag == DW_TAG_try_block
1054b214
TT
7156 || die->parent->tag == DW_TAG_catch_block
7157 || die->parent->tag == DW_TAG_subprogram)
1c809c68
TT
7158 return 0;
7159 return 1;
94af9270
KS
7160
7161 default:
7162 return 0;
7163 }
7164}
7165
98bfdba5
PA
7166/* Retrieve the last character from a mem_file. */
7167
7168static void
7169do_ui_file_peek_last (void *object, const char *buffer, long length)
7170{
7171 char *last_char_p = (char *) object;
7172
7173 if (length > 0)
7174 *last_char_p = buffer[length - 1];
7175}
7176
94af9270 7177/* Compute the fully qualified name of DIE in CU. If PHYSNAME is nonzero,
a766d390
DE
7178 compute the physname for the object, which include a method's:
7179 - formal parameters (C++/Java),
7180 - receiver type (Go),
7181 - return type (Java).
7182
7183 The term "physname" is a bit confusing.
7184 For C++, for example, it is the demangled name.
7185 For Go, for example, it's the mangled name.
94af9270 7186
af6b7be1
JB
7187 For Ada, return the DIE's linkage name rather than the fully qualified
7188 name. PHYSNAME is ignored..
7189
94af9270
KS
7190 The result is allocated on the objfile_obstack and canonicalized. */
7191
7192static const char *
7193dwarf2_compute_name (char *name, struct die_info *die, struct dwarf2_cu *cu,
7194 int physname)
7195{
bb5ed363
DE
7196 struct objfile *objfile = cu->objfile;
7197
94af9270
KS
7198 if (name == NULL)
7199 name = dwarf2_name (die, cu);
7200
f55ee35c
JK
7201 /* For Fortran GDB prefers DW_AT_*linkage_name if present but otherwise
7202 compute it by typename_concat inside GDB. */
7203 if (cu->language == language_ada
7204 || (cu->language == language_fortran && physname))
7205 {
7206 /* For Ada unit, we prefer the linkage name over the name, as
7207 the former contains the exported name, which the user expects
7208 to be able to reference. Ideally, we want the user to be able
7209 to reference this entity using either natural or linkage name,
7210 but we haven't started looking at this enhancement yet. */
7211 struct attribute *attr;
7212
7213 attr = dwarf2_attr (die, DW_AT_linkage_name, cu);
7214 if (attr == NULL)
7215 attr = dwarf2_attr (die, DW_AT_MIPS_linkage_name, cu);
7216 if (attr && DW_STRING (attr))
7217 return DW_STRING (attr);
7218 }
7219
94af9270
KS
7220 /* These are the only languages we know how to qualify names in. */
7221 if (name != NULL
f55ee35c
JK
7222 && (cu->language == language_cplus || cu->language == language_java
7223 || cu->language == language_fortran))
94af9270
KS
7224 {
7225 if (die_needs_namespace (die, cu))
7226 {
7227 long length;
0d5cff50 7228 const char *prefix;
94af9270
KS
7229 struct ui_file *buf;
7230
7231 prefix = determine_prefix (die, cu);
7232 buf = mem_fileopen ();
7233 if (*prefix != '\0')
7234 {
f55ee35c
JK
7235 char *prefixed_name = typename_concat (NULL, prefix, name,
7236 physname, cu);
9a619af0 7237
94af9270
KS
7238 fputs_unfiltered (prefixed_name, buf);
7239 xfree (prefixed_name);
7240 }
7241 else
62d5b8da 7242 fputs_unfiltered (name, buf);
94af9270 7243
98bfdba5
PA
7244 /* Template parameters may be specified in the DIE's DW_AT_name, or
7245 as children with DW_TAG_template_type_param or
7246 DW_TAG_value_type_param. If the latter, add them to the name
7247 here. If the name already has template parameters, then
7248 skip this step; some versions of GCC emit both, and
7249 it is more efficient to use the pre-computed name.
7250
7251 Something to keep in mind about this process: it is very
7252 unlikely, or in some cases downright impossible, to produce
7253 something that will match the mangled name of a function.
7254 If the definition of the function has the same debug info,
7255 we should be able to match up with it anyway. But fallbacks
7256 using the minimal symbol, for instance to find a method
7257 implemented in a stripped copy of libstdc++, will not work.
7258 If we do not have debug info for the definition, we will have to
7259 match them up some other way.
7260
7261 When we do name matching there is a related problem with function
7262 templates; two instantiated function templates are allowed to
7263 differ only by their return types, which we do not add here. */
7264
7265 if (cu->language == language_cplus && strchr (name, '<') == NULL)
7266 {
7267 struct attribute *attr;
7268 struct die_info *child;
7269 int first = 1;
7270
7271 die->building_fullname = 1;
7272
7273 for (child = die->child; child != NULL; child = child->sibling)
7274 {
7275 struct type *type;
12df843f 7276 LONGEST value;
98bfdba5
PA
7277 gdb_byte *bytes;
7278 struct dwarf2_locexpr_baton *baton;
7279 struct value *v;
7280
7281 if (child->tag != DW_TAG_template_type_param
7282 && child->tag != DW_TAG_template_value_param)
7283 continue;
7284
7285 if (first)
7286 {
7287 fputs_unfiltered ("<", buf);
7288 first = 0;
7289 }
7290 else
7291 fputs_unfiltered (", ", buf);
7292
7293 attr = dwarf2_attr (child, DW_AT_type, cu);
7294 if (attr == NULL)
7295 {
7296 complaint (&symfile_complaints,
7297 _("template parameter missing DW_AT_type"));
7298 fputs_unfiltered ("UNKNOWN_TYPE", buf);
7299 continue;
7300 }
7301 type = die_type (child, cu);
7302
7303 if (child->tag == DW_TAG_template_type_param)
7304 {
7305 c_print_type (type, "", buf, -1, 0);
7306 continue;
7307 }
7308
7309 attr = dwarf2_attr (child, DW_AT_const_value, cu);
7310 if (attr == NULL)
7311 {
7312 complaint (&symfile_complaints,
3e43a32a
MS
7313 _("template parameter missing "
7314 "DW_AT_const_value"));
98bfdba5
PA
7315 fputs_unfiltered ("UNKNOWN_VALUE", buf);
7316 continue;
7317 }
7318
7319 dwarf2_const_value_attr (attr, type, name,
7320 &cu->comp_unit_obstack, cu,
7321 &value, &bytes, &baton);
7322
7323 if (TYPE_NOSIGN (type))
7324 /* GDB prints characters as NUMBER 'CHAR'. If that's
7325 changed, this can use value_print instead. */
7326 c_printchar (value, type, buf);
7327 else
7328 {
7329 struct value_print_options opts;
7330
7331 if (baton != NULL)
7332 v = dwarf2_evaluate_loc_desc (type, NULL,
7333 baton->data,
7334 baton->size,
7335 baton->per_cu);
7336 else if (bytes != NULL)
7337 {
7338 v = allocate_value (type);
7339 memcpy (value_contents_writeable (v), bytes,
7340 TYPE_LENGTH (type));
7341 }
7342 else
7343 v = value_from_longest (type, value);
7344
3e43a32a
MS
7345 /* Specify decimal so that we do not depend on
7346 the radix. */
98bfdba5
PA
7347 get_formatted_print_options (&opts, 'd');
7348 opts.raw = 1;
7349 value_print (v, buf, &opts);
7350 release_value (v);
7351 value_free (v);
7352 }
7353 }
7354
7355 die->building_fullname = 0;
7356
7357 if (!first)
7358 {
7359 /* Close the argument list, with a space if necessary
7360 (nested templates). */
7361 char last_char = '\0';
7362 ui_file_put (buf, do_ui_file_peek_last, &last_char);
7363 if (last_char == '>')
7364 fputs_unfiltered (" >", buf);
7365 else
7366 fputs_unfiltered (">", buf);
7367 }
7368 }
7369
94af9270
KS
7370 /* For Java and C++ methods, append formal parameter type
7371 information, if PHYSNAME. */
6e70227d 7372
94af9270
KS
7373 if (physname && die->tag == DW_TAG_subprogram
7374 && (cu->language == language_cplus
7375 || cu->language == language_java))
7376 {
7377 struct type *type = read_type_die (die, cu);
7378
3167638f 7379 c_type_print_args (type, buf, 1, cu->language);
94af9270
KS
7380
7381 if (cu->language == language_java)
7382 {
7383 /* For java, we must append the return type to method
0963b4bd 7384 names. */
94af9270
KS
7385 if (die->tag == DW_TAG_subprogram)
7386 java_print_type (TYPE_TARGET_TYPE (type), "", buf,
7387 0, 0);
7388 }
7389 else if (cu->language == language_cplus)
7390 {
60430eff
DJ
7391 /* Assume that an artificial first parameter is
7392 "this", but do not crash if it is not. RealView
7393 marks unnamed (and thus unused) parameters as
7394 artificial; there is no way to differentiate
7395 the two cases. */
94af9270
KS
7396 if (TYPE_NFIELDS (type) > 0
7397 && TYPE_FIELD_ARTIFICIAL (type, 0)
60430eff 7398 && TYPE_CODE (TYPE_FIELD_TYPE (type, 0)) == TYPE_CODE_PTR
3e43a32a
MS
7399 && TYPE_CONST (TYPE_TARGET_TYPE (TYPE_FIELD_TYPE (type,
7400 0))))
94af9270
KS
7401 fputs_unfiltered (" const", buf);
7402 }
7403 }
7404
bb5ed363 7405 name = ui_file_obsavestring (buf, &objfile->objfile_obstack,
94af9270
KS
7406 &length);
7407 ui_file_delete (buf);
7408
7409 if (cu->language == language_cplus)
7410 {
7411 char *cname
7412 = dwarf2_canonicalize_name (name, cu,
bb5ed363 7413 &objfile->objfile_obstack);
9a619af0 7414
94af9270
KS
7415 if (cname != NULL)
7416 name = cname;
7417 }
7418 }
7419 }
7420
7421 return name;
7422}
7423
0114d602
DJ
7424/* Return the fully qualified name of DIE, based on its DW_AT_name.
7425 If scope qualifiers are appropriate they will be added. The result
7426 will be allocated on the objfile_obstack, or NULL if the DIE does
94af9270
KS
7427 not have a name. NAME may either be from a previous call to
7428 dwarf2_name or NULL.
7429
0963b4bd 7430 The output string will be canonicalized (if C++/Java). */
0114d602
DJ
7431
7432static const char *
94af9270 7433dwarf2_full_name (char *name, struct die_info *die, struct dwarf2_cu *cu)
0114d602 7434{
94af9270
KS
7435 return dwarf2_compute_name (name, die, cu, 0);
7436}
0114d602 7437
94af9270
KS
7438/* Construct a physname for the given DIE in CU. NAME may either be
7439 from a previous call to dwarf2_name or NULL. The result will be
7440 allocated on the objfile_objstack or NULL if the DIE does not have a
7441 name.
0114d602 7442
94af9270 7443 The output string will be canonicalized (if C++/Java). */
0114d602 7444
94af9270
KS
7445static const char *
7446dwarf2_physname (char *name, struct die_info *die, struct dwarf2_cu *cu)
7447{
bb5ed363 7448 struct objfile *objfile = cu->objfile;
900e11f9
JK
7449 struct attribute *attr;
7450 const char *retval, *mangled = NULL, *canon = NULL;
7451 struct cleanup *back_to;
7452 int need_copy = 1;
7453
7454 /* In this case dwarf2_compute_name is just a shortcut not building anything
7455 on its own. */
7456 if (!die_needs_namespace (die, cu))
7457 return dwarf2_compute_name (name, die, cu, 1);
7458
7459 back_to = make_cleanup (null_cleanup, NULL);
7460
7461 attr = dwarf2_attr (die, DW_AT_linkage_name, cu);
7462 if (!attr)
7463 attr = dwarf2_attr (die, DW_AT_MIPS_linkage_name, cu);
7464
7465 /* DW_AT_linkage_name is missing in some cases - depend on what GDB
7466 has computed. */
7467 if (attr && DW_STRING (attr))
7468 {
7469 char *demangled;
7470
7471 mangled = DW_STRING (attr);
7472
7473 /* Use DMGL_RET_DROP for C++ template functions to suppress their return
7474 type. It is easier for GDB users to search for such functions as
7475 `name(params)' than `long name(params)'. In such case the minimal
7476 symbol names do not match the full symbol names but for template
7477 functions there is never a need to look up their definition from their
7478 declaration so the only disadvantage remains the minimal symbol
7479 variant `long name(params)' does not have the proper inferior type.
7480 */
7481
a766d390
DE
7482 if (cu->language == language_go)
7483 {
7484 /* This is a lie, but we already lie to the caller new_symbol_full.
7485 new_symbol_full assumes we return the mangled name.
7486 This just undoes that lie until things are cleaned up. */
7487 demangled = NULL;
7488 }
7489 else
7490 {
7491 demangled = cplus_demangle (mangled,
7492 (DMGL_PARAMS | DMGL_ANSI
7493 | (cu->language == language_java
7494 ? DMGL_JAVA | DMGL_RET_POSTFIX
7495 : DMGL_RET_DROP)));
7496 }
900e11f9
JK
7497 if (demangled)
7498 {
7499 make_cleanup (xfree, demangled);
7500 canon = demangled;
7501 }
7502 else
7503 {
7504 canon = mangled;
7505 need_copy = 0;
7506 }
7507 }
7508
7509 if (canon == NULL || check_physname)
7510 {
7511 const char *physname = dwarf2_compute_name (name, die, cu, 1);
7512
7513 if (canon != NULL && strcmp (physname, canon) != 0)
7514 {
7515 /* It may not mean a bug in GDB. The compiler could also
7516 compute DW_AT_linkage_name incorrectly. But in such case
7517 GDB would need to be bug-to-bug compatible. */
7518
7519 complaint (&symfile_complaints,
7520 _("Computed physname <%s> does not match demangled <%s> "
7521 "(from linkage <%s>) - DIE at 0x%x [in module %s]"),
b64f50a1 7522 physname, canon, mangled, die->offset.sect_off, objfile->name);
900e11f9
JK
7523
7524 /* Prefer DW_AT_linkage_name (in the CANON form) - when it
7525 is available here - over computed PHYSNAME. It is safer
7526 against both buggy GDB and buggy compilers. */
7527
7528 retval = canon;
7529 }
7530 else
7531 {
7532 retval = physname;
7533 need_copy = 0;
7534 }
7535 }
7536 else
7537 retval = canon;
7538
7539 if (need_copy)
7540 retval = obsavestring (retval, strlen (retval),
bb5ed363 7541 &objfile->objfile_obstack);
900e11f9
JK
7542
7543 do_cleanups (back_to);
7544 return retval;
0114d602
DJ
7545}
7546
27aa8d6a
SW
7547/* Read the import statement specified by the given die and record it. */
7548
7549static void
7550read_import_statement (struct die_info *die, struct dwarf2_cu *cu)
7551{
bb5ed363 7552 struct objfile *objfile = cu->objfile;
27aa8d6a 7553 struct attribute *import_attr;
32019081 7554 struct die_info *imported_die, *child_die;
de4affc9 7555 struct dwarf2_cu *imported_cu;
27aa8d6a 7556 const char *imported_name;
794684b6 7557 const char *imported_name_prefix;
13387711
SW
7558 const char *canonical_name;
7559 const char *import_alias;
7560 const char *imported_declaration = NULL;
794684b6 7561 const char *import_prefix;
32019081
JK
7562 VEC (const_char_ptr) *excludes = NULL;
7563 struct cleanup *cleanups;
13387711
SW
7564
7565 char *temp;
27aa8d6a
SW
7566
7567 import_attr = dwarf2_attr (die, DW_AT_import, cu);
7568 if (import_attr == NULL)
7569 {
7570 complaint (&symfile_complaints, _("Tag '%s' has no DW_AT_import"),
7571 dwarf_tag_name (die->tag));
7572 return;
7573 }
7574
de4affc9
CC
7575 imported_cu = cu;
7576 imported_die = follow_die_ref_or_sig (die, import_attr, &imported_cu);
7577 imported_name = dwarf2_name (imported_die, imported_cu);
27aa8d6a
SW
7578 if (imported_name == NULL)
7579 {
7580 /* GCC bug: https://bugzilla.redhat.com/show_bug.cgi?id=506524
7581
7582 The import in the following code:
7583 namespace A
7584 {
7585 typedef int B;
7586 }
7587
7588 int main ()
7589 {
7590 using A::B;
7591 B b;
7592 return b;
7593 }
7594
7595 ...
7596 <2><51>: Abbrev Number: 3 (DW_TAG_imported_declaration)
7597 <52> DW_AT_decl_file : 1
7598 <53> DW_AT_decl_line : 6
7599 <54> DW_AT_import : <0x75>
7600 <2><58>: Abbrev Number: 4 (DW_TAG_typedef)
7601 <59> DW_AT_name : B
7602 <5b> DW_AT_decl_file : 1
7603 <5c> DW_AT_decl_line : 2
7604 <5d> DW_AT_type : <0x6e>
7605 ...
7606 <1><75>: Abbrev Number: 7 (DW_TAG_base_type)
7607 <76> DW_AT_byte_size : 4
7608 <77> DW_AT_encoding : 5 (signed)
7609
7610 imports the wrong die ( 0x75 instead of 0x58 ).
7611 This case will be ignored until the gcc bug is fixed. */
7612 return;
7613 }
7614
82856980
SW
7615 /* Figure out the local name after import. */
7616 import_alias = dwarf2_name (die, cu);
27aa8d6a 7617
794684b6
SW
7618 /* Figure out where the statement is being imported to. */
7619 import_prefix = determine_prefix (die, cu);
7620
7621 /* Figure out what the scope of the imported die is and prepend it
7622 to the name of the imported die. */
de4affc9 7623 imported_name_prefix = determine_prefix (imported_die, imported_cu);
794684b6 7624
f55ee35c
JK
7625 if (imported_die->tag != DW_TAG_namespace
7626 && imported_die->tag != DW_TAG_module)
794684b6 7627 {
13387711
SW
7628 imported_declaration = imported_name;
7629 canonical_name = imported_name_prefix;
794684b6 7630 }
13387711 7631 else if (strlen (imported_name_prefix) > 0)
794684b6 7632 {
13387711
SW
7633 temp = alloca (strlen (imported_name_prefix)
7634 + 2 + strlen (imported_name) + 1);
7635 strcpy (temp, imported_name_prefix);
7636 strcat (temp, "::");
7637 strcat (temp, imported_name);
7638 canonical_name = temp;
794684b6 7639 }
13387711
SW
7640 else
7641 canonical_name = imported_name;
794684b6 7642
32019081
JK
7643 cleanups = make_cleanup (VEC_cleanup (const_char_ptr), &excludes);
7644
7645 if (die->tag == DW_TAG_imported_module && cu->language == language_fortran)
7646 for (child_die = die->child; child_die && child_die->tag;
7647 child_die = sibling_die (child_die))
7648 {
7649 /* DWARF-4: A Fortran use statement with a “rename list” may be
7650 represented by an imported module entry with an import attribute
7651 referring to the module and owned entries corresponding to those
7652 entities that are renamed as part of being imported. */
7653
7654 if (child_die->tag != DW_TAG_imported_declaration)
7655 {
7656 complaint (&symfile_complaints,
7657 _("child DW_TAG_imported_declaration expected "
7658 "- DIE at 0x%x [in module %s]"),
b64f50a1 7659 child_die->offset.sect_off, objfile->name);
32019081
JK
7660 continue;
7661 }
7662
7663 import_attr = dwarf2_attr (child_die, DW_AT_import, cu);
7664 if (import_attr == NULL)
7665 {
7666 complaint (&symfile_complaints, _("Tag '%s' has no DW_AT_import"),
7667 dwarf_tag_name (child_die->tag));
7668 continue;
7669 }
7670
7671 imported_cu = cu;
7672 imported_die = follow_die_ref_or_sig (child_die, import_attr,
7673 &imported_cu);
7674 imported_name = dwarf2_name (imported_die, imported_cu);
7675 if (imported_name == NULL)
7676 {
7677 complaint (&symfile_complaints,
7678 _("child DW_TAG_imported_declaration has unknown "
7679 "imported name - DIE at 0x%x [in module %s]"),
b64f50a1 7680 child_die->offset.sect_off, objfile->name);
32019081
JK
7681 continue;
7682 }
7683
7684 VEC_safe_push (const_char_ptr, excludes, imported_name);
7685
7686 process_die (child_die, cu);
7687 }
7688
c0cc3a76
SW
7689 cp_add_using_directive (import_prefix,
7690 canonical_name,
7691 import_alias,
13387711 7692 imported_declaration,
32019081 7693 excludes,
bb5ed363 7694 &objfile->objfile_obstack);
32019081
JK
7695
7696 do_cleanups (cleanups);
27aa8d6a
SW
7697}
7698
f4dc4d17 7699/* Cleanup function for handle_DW_AT_stmt_list. */
ae2de4f8 7700
cb1df416
DJ
7701static void
7702free_cu_line_header (void *arg)
7703{
7704 struct dwarf2_cu *cu = arg;
7705
7706 free_line_header (cu->line_header);
7707 cu->line_header = NULL;
7708}
7709
9291a0cd
TT
7710static void
7711find_file_and_directory (struct die_info *die, struct dwarf2_cu *cu,
7712 char **name, char **comp_dir)
7713{
7714 struct attribute *attr;
7715
7716 *name = NULL;
7717 *comp_dir = NULL;
7718
7719 /* Find the filename. Do not use dwarf2_name here, since the filename
7720 is not a source language identifier. */
7721 attr = dwarf2_attr (die, DW_AT_name, cu);
7722 if (attr)
7723 {
7724 *name = DW_STRING (attr);
7725 }
7726
7727 attr = dwarf2_attr (die, DW_AT_comp_dir, cu);
7728 if (attr)
7729 *comp_dir = DW_STRING (attr);
7730 else if (*name != NULL && IS_ABSOLUTE_PATH (*name))
7731 {
7732 *comp_dir = ldirname (*name);
7733 if (*comp_dir != NULL)
7734 make_cleanup (xfree, *comp_dir);
7735 }
7736 if (*comp_dir != NULL)
7737 {
7738 /* Irix 6.2 native cc prepends <machine>.: to the compilation
7739 directory, get rid of it. */
7740 char *cp = strchr (*comp_dir, ':');
7741
7742 if (cp && cp != *comp_dir && cp[-1] == '.' && cp[1] == '/')
7743 *comp_dir = cp + 1;
7744 }
7745
7746 if (*name == NULL)
7747 *name = "<unknown>";
7748}
7749
f4dc4d17
DE
7750/* Handle DW_AT_stmt_list for a compilation unit.
7751 DIE is the DW_TAG_compile_unit die for CU.
f3f5162e
DE
7752 COMP_DIR is the compilation directory.
7753 WANT_LINE_INFO is non-zero if the pc/line-number mapping is needed. */
2ab95328
TT
7754
7755static void
7756handle_DW_AT_stmt_list (struct die_info *die, struct dwarf2_cu *cu,
f4dc4d17 7757 const char *comp_dir)
2ab95328
TT
7758{
7759 struct attribute *attr;
2ab95328 7760
f4dc4d17
DE
7761 gdb_assert (! cu->per_cu->is_debug_types);
7762
2ab95328
TT
7763 attr = dwarf2_attr (die, DW_AT_stmt_list, cu);
7764 if (attr)
7765 {
7766 unsigned int line_offset = DW_UNSND (attr);
7767 struct line_header *line_header
3019eac3 7768 = dwarf_decode_line_header (line_offset, cu);
2ab95328
TT
7769
7770 if (line_header)
dee91e82
DE
7771 {
7772 cu->line_header = line_header;
7773 make_cleanup (free_cu_line_header, cu);
f4dc4d17 7774 dwarf_decode_lines (line_header, comp_dir, cu, NULL, 1);
dee91e82 7775 }
2ab95328
TT
7776 }
7777}
7778
95554aad 7779/* Process DW_TAG_compile_unit or DW_TAG_partial_unit. */
ae2de4f8 7780
c906108c 7781static void
e7c27a73 7782read_file_scope (struct die_info *die, struct dwarf2_cu *cu)
c906108c 7783{
dee91e82 7784 struct objfile *objfile = dwarf2_per_objfile->objfile;
debd256d 7785 struct cleanup *back_to = make_cleanup (null_cleanup, 0);
2acceee2 7786 CORE_ADDR lowpc = ((CORE_ADDR) -1);
c906108c
SS
7787 CORE_ADDR highpc = ((CORE_ADDR) 0);
7788 struct attribute *attr;
e1024ff1 7789 char *name = NULL;
c906108c
SS
7790 char *comp_dir = NULL;
7791 struct die_info *child_die;
7792 bfd *abfd = objfile->obfd;
e142c38c 7793 CORE_ADDR baseaddr;
6e70227d 7794
e142c38c 7795 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
c906108c 7796
fae299cd 7797 get_scope_pc_bounds (die, &lowpc, &highpc, cu);
c906108c
SS
7798
7799 /* If we didn't find a lowpc, set it to highpc to avoid complaints
7800 from finish_block. */
2acceee2 7801 if (lowpc == ((CORE_ADDR) -1))
c906108c
SS
7802 lowpc = highpc;
7803 lowpc += baseaddr;
7804 highpc += baseaddr;
7805
9291a0cd 7806 find_file_and_directory (die, cu, &name, &comp_dir);
e1024ff1 7807
95554aad 7808 prepare_one_comp_unit (cu, die, cu->language);
303b6f5d 7809
f4b8a18d
KW
7810 /* The XLCL doesn't generate DW_LANG_OpenCL because this attribute is not
7811 standardised yet. As a workaround for the language detection we fall
7812 back to the DW_AT_producer string. */
7813 if (cu->producer && strstr (cu->producer, "IBM XL C for OpenCL") != NULL)
7814 cu->language = language_opencl;
7815
3019eac3
DE
7816 /* Similar hack for Go. */
7817 if (cu->producer && strstr (cu->producer, "GNU Go ") != NULL)
7818 set_cu_language (DW_LANG_Go, cu);
7819
f4dc4d17 7820 dwarf2_start_symtab (cu, name, comp_dir, lowpc);
3019eac3
DE
7821
7822 /* Decode line number information if present. We do this before
7823 processing child DIEs, so that the line header table is available
7824 for DW_AT_decl_file. */
f4dc4d17 7825 handle_DW_AT_stmt_list (die, cu, comp_dir);
3019eac3
DE
7826
7827 /* Process all dies in compilation unit. */
7828 if (die->child != NULL)
7829 {
7830 child_die = die->child;
7831 while (child_die && child_die->tag)
7832 {
7833 process_die (child_die, cu);
7834 child_die = sibling_die (child_die);
7835 }
7836 }
7837
7838 /* Decode macro information, if present. Dwarf 2 macro information
7839 refers to information in the line number info statement program
7840 header, so we can only read it if we've read the header
7841 successfully. */
7842 attr = dwarf2_attr (die, DW_AT_GNU_macros, cu);
7843 if (attr && cu->line_header)
7844 {
7845 if (dwarf2_attr (die, DW_AT_macro_info, cu))
7846 complaint (&symfile_complaints,
7847 _("CU refers to both DW_AT_GNU_macros and DW_AT_macro_info"));
7848
09262596 7849 dwarf_decode_macros (cu, DW_UNSND (attr), comp_dir, 1);
3019eac3
DE
7850 }
7851 else
7852 {
7853 attr = dwarf2_attr (die, DW_AT_macro_info, cu);
7854 if (attr && cu->line_header)
7855 {
7856 unsigned int macro_offset = DW_UNSND (attr);
7857
09262596 7858 dwarf_decode_macros (cu, macro_offset, comp_dir, 0);
3019eac3
DE
7859 }
7860 }
7861
7862 do_cleanups (back_to);
7863}
7864
f4dc4d17
DE
7865/* TU version of handle_DW_AT_stmt_list for read_type_unit_scope.
7866 Create the set of symtabs used by this TU, or if this TU is sharing
7867 symtabs with another TU and the symtabs have already been created
7868 then restore those symtabs in the line header.
7869 We don't need the pc/line-number mapping for type units. */
3019eac3
DE
7870
7871static void
f4dc4d17 7872setup_type_unit_groups (struct die_info *die, struct dwarf2_cu *cu)
3019eac3 7873{
f4dc4d17
DE
7874 struct objfile *objfile = dwarf2_per_objfile->objfile;
7875 struct dwarf2_per_cu_data *per_cu = cu->per_cu;
7876 struct type_unit_group *tu_group;
7877 int first_time;
7878 struct line_header *lh;
3019eac3 7879 struct attribute *attr;
f4dc4d17 7880 unsigned int i, line_offset;
3019eac3 7881
f4dc4d17 7882 gdb_assert (per_cu->is_debug_types);
3019eac3 7883
f4dc4d17 7884 attr = dwarf2_attr (die, DW_AT_stmt_list, cu);
3019eac3 7885
f4dc4d17
DE
7886 /* If we're using .gdb_index (includes -readnow) then
7887 per_cu->s.type_unit_group may not have been set up yet. */
7888 if (per_cu->s.type_unit_group == NULL)
094b34ac 7889 per_cu->s.type_unit_group = get_type_unit_group (cu, attr);
f4dc4d17
DE
7890 tu_group = per_cu->s.type_unit_group;
7891
7892 /* If we've already processed this stmt_list there's no real need to
7893 do it again, we could fake it and just recreate the part we need
7894 (file name,index -> symtab mapping). If data shows this optimization
7895 is useful we can do it then. */
7896 first_time = tu_group->primary_symtab == NULL;
7897
7898 /* We have to handle the case of both a missing DW_AT_stmt_list or bad
7899 debug info. */
7900 lh = NULL;
7901 if (attr != NULL)
3019eac3 7902 {
f4dc4d17
DE
7903 line_offset = DW_UNSND (attr);
7904 lh = dwarf_decode_line_header (line_offset, cu);
7905 }
7906 if (lh == NULL)
7907 {
7908 if (first_time)
7909 dwarf2_start_symtab (cu, "", NULL, 0);
7910 else
7911 {
7912 gdb_assert (tu_group->symtabs == NULL);
7913 restart_symtab (0);
7914 }
7915 /* Note: The primary symtab will get allocated at the end. */
7916 return;
3019eac3
DE
7917 }
7918
f4dc4d17
DE
7919 cu->line_header = lh;
7920 make_cleanup (free_cu_line_header, cu);
3019eac3 7921
f4dc4d17
DE
7922 if (first_time)
7923 {
7924 dwarf2_start_symtab (cu, "", NULL, 0);
3019eac3 7925
f4dc4d17
DE
7926 tu_group->num_symtabs = lh->num_file_names;
7927 tu_group->symtabs = XNEWVEC (struct symtab *, lh->num_file_names);
3019eac3 7928
f4dc4d17
DE
7929 for (i = 0; i < lh->num_file_names; ++i)
7930 {
7931 char *dir = NULL;
7932 struct file_entry *fe = &lh->file_names[i];
3019eac3 7933
f4dc4d17
DE
7934 if (fe->dir_index)
7935 dir = lh->include_dirs[fe->dir_index - 1];
7936 dwarf2_start_subfile (fe->name, dir, NULL);
3019eac3 7937
f4dc4d17
DE
7938 /* Note: We don't have to watch for the main subfile here, type units
7939 don't have DW_AT_name. */
3019eac3 7940
f4dc4d17
DE
7941 if (current_subfile->symtab == NULL)
7942 {
7943 /* NOTE: start_subfile will recognize when it's been passed
7944 a file it has already seen. So we can't assume there's a
7945 simple mapping from lh->file_names to subfiles,
7946 lh->file_names may contain dups. */
7947 current_subfile->symtab = allocate_symtab (current_subfile->name,
7948 objfile);
7949 }
7950
7951 fe->symtab = current_subfile->symtab;
7952 tu_group->symtabs[i] = fe->symtab;
7953 }
7954 }
7955 else
3019eac3 7956 {
f4dc4d17
DE
7957 restart_symtab (0);
7958
7959 for (i = 0; i < lh->num_file_names; ++i)
7960 {
7961 struct file_entry *fe = &lh->file_names[i];
7962
7963 fe->symtab = tu_group->symtabs[i];
7964 }
3019eac3
DE
7965 }
7966
f4dc4d17
DE
7967 /* The main symtab is allocated last. Type units don't have DW_AT_name
7968 so they don't have a "real" (so to speak) symtab anyway.
7969 There is later code that will assign the main symtab to all symbols
7970 that don't have one. We need to handle the case of a symbol with a
7971 missing symtab (DW_AT_decl_file) anyway. */
7972}
3019eac3 7973
f4dc4d17
DE
7974/* Process DW_TAG_type_unit.
7975 For TUs we want to skip the first top level sibling if it's not the
7976 actual type being defined by this TU. In this case the first top
7977 level sibling is there to provide context only. */
3019eac3 7978
f4dc4d17
DE
7979static void
7980read_type_unit_scope (struct die_info *die, struct dwarf2_cu *cu)
7981{
7982 struct die_info *child_die;
3019eac3 7983
f4dc4d17
DE
7984 prepare_one_comp_unit (cu, die, language_minimal);
7985
7986 /* Initialize (or reinitialize) the machinery for building symtabs.
7987 We do this before processing child DIEs, so that the line header table
7988 is available for DW_AT_decl_file. */
7989 setup_type_unit_groups (die, cu);
7990
7991 if (die->child != NULL)
7992 {
7993 child_die = die->child;
7994 while (child_die && child_die->tag)
7995 {
7996 process_die (child_die, cu);
7997 child_die = sibling_die (child_die);
7998 }
7999 }
3019eac3
DE
8000}
8001\f
8002/* DWO files. */
8003
8004static hashval_t
8005hash_dwo_file (const void *item)
8006{
8007 const struct dwo_file *dwo_file = item;
8008
8009 return htab_hash_string (dwo_file->dwo_name);
8010}
8011
8012static int
8013eq_dwo_file (const void *item_lhs, const void *item_rhs)
8014{
8015 const struct dwo_file *lhs = item_lhs;
8016 const struct dwo_file *rhs = item_rhs;
8017
8018 return strcmp (lhs->dwo_name, rhs->dwo_name) == 0;
8019}
8020
8021/* Allocate a hash table for DWO files. */
8022
8023static htab_t
8024allocate_dwo_file_hash_table (void)
8025{
8026 struct objfile *objfile = dwarf2_per_objfile->objfile;
8027
8028 return htab_create_alloc_ex (41,
8029 hash_dwo_file,
8030 eq_dwo_file,
8031 NULL,
8032 &objfile->objfile_obstack,
8033 hashtab_obstack_allocate,
8034 dummy_obstack_deallocate);
8035}
8036
8037static hashval_t
8038hash_dwo_unit (const void *item)
8039{
8040 const struct dwo_unit *dwo_unit = item;
8041
8042 /* This drops the top 32 bits of the id, but is ok for a hash. */
8043 return dwo_unit->signature;
8044}
8045
8046static int
8047eq_dwo_unit (const void *item_lhs, const void *item_rhs)
8048{
8049 const struct dwo_unit *lhs = item_lhs;
8050 const struct dwo_unit *rhs = item_rhs;
8051
8052 /* The signature is assumed to be unique within the DWO file.
8053 So while object file CU dwo_id's always have the value zero,
8054 that's OK, assuming each object file DWO file has only one CU,
8055 and that's the rule for now. */
8056 return lhs->signature == rhs->signature;
8057}
8058
8059/* Allocate a hash table for DWO CUs,TUs.
8060 There is one of these tables for each of CUs,TUs for each DWO file. */
8061
8062static htab_t
8063allocate_dwo_unit_table (struct objfile *objfile)
8064{
8065 /* Start out with a pretty small number.
8066 Generally DWO files contain only one CU and maybe some TUs. */
8067 return htab_create_alloc_ex (3,
8068 hash_dwo_unit,
8069 eq_dwo_unit,
8070 NULL,
8071 &objfile->objfile_obstack,
8072 hashtab_obstack_allocate,
8073 dummy_obstack_deallocate);
8074}
8075
8076/* This function is mapped across the sections and remembers the offset and
8077 size of each of the DWO debugging sections we are interested in. */
8078
8079static void
8080dwarf2_locate_dwo_sections (bfd *abfd, asection *sectp, void *dwo_file_ptr)
8081{
8082 struct dwo_file *dwo_file = dwo_file_ptr;
8083 const struct dwo_section_names *names = &dwo_section_names;
8084
8085 if (section_is_p (sectp->name, &names->abbrev_dwo))
8086 {
8087 dwo_file->sections.abbrev.asection = sectp;
8088 dwo_file->sections.abbrev.size = bfd_get_section_size (sectp);
8089 }
8090 else if (section_is_p (sectp->name, &names->info_dwo))
8091 {
8092 dwo_file->sections.info.asection = sectp;
8093 dwo_file->sections.info.size = bfd_get_section_size (sectp);
8094 }
8095 else if (section_is_p (sectp->name, &names->line_dwo))
8096 {
8097 dwo_file->sections.line.asection = sectp;
8098 dwo_file->sections.line.size = bfd_get_section_size (sectp);
8099 }
8100 else if (section_is_p (sectp->name, &names->loc_dwo))
8101 {
8102 dwo_file->sections.loc.asection = sectp;
8103 dwo_file->sections.loc.size = bfd_get_section_size (sectp);
8104 }
09262596
DE
8105 else if (section_is_p (sectp->name, &names->macinfo_dwo))
8106 {
8107 dwo_file->sections.macinfo.asection = sectp;
8108 dwo_file->sections.macinfo.size = bfd_get_section_size (sectp);
8109 }
8110 else if (section_is_p (sectp->name, &names->macro_dwo))
8111 {
8112 dwo_file->sections.macro.asection = sectp;
8113 dwo_file->sections.macro.size = bfd_get_section_size (sectp);
8114 }
3019eac3
DE
8115 else if (section_is_p (sectp->name, &names->str_dwo))
8116 {
8117 dwo_file->sections.str.asection = sectp;
8118 dwo_file->sections.str.size = bfd_get_section_size (sectp);
8119 }
8120 else if (section_is_p (sectp->name, &names->str_offsets_dwo))
8121 {
8122 dwo_file->sections.str_offsets.asection = sectp;
8123 dwo_file->sections.str_offsets.size = bfd_get_section_size (sectp);
8124 }
8125 else if (section_is_p (sectp->name, &names->types_dwo))
8126 {
8127 struct dwarf2_section_info type_section;
8128
8129 memset (&type_section, 0, sizeof (type_section));
8130 type_section.asection = sectp;
8131 type_section.size = bfd_get_section_size (sectp);
8132 VEC_safe_push (dwarf2_section_info_def, dwo_file->sections.types,
8133 &type_section);
8134 }
8135}
8136
8137/* Structure used to pass data to create_debug_info_hash_table_reader. */
8138
8139struct create_dwo_info_table_data
8140{
8141 struct dwo_file *dwo_file;
8142 htab_t cu_htab;
8143};
8144
8145/* die_reader_func for create_debug_info_hash_table. */
8146
8147static void
8148create_debug_info_hash_table_reader (const struct die_reader_specs *reader,
8149 gdb_byte *info_ptr,
8150 struct die_info *comp_unit_die,
8151 int has_children,
8152 void *datap)
8153{
8154 struct dwarf2_cu *cu = reader->cu;
8155 struct objfile *objfile = dwarf2_per_objfile->objfile;
8156 sect_offset offset = cu->per_cu->offset;
8157 struct dwarf2_section_info *section = cu->per_cu->info_or_types_section;
8158 struct create_dwo_info_table_data *data = datap;
8159 struct dwo_file *dwo_file = data->dwo_file;
8160 htab_t cu_htab = data->cu_htab;
8161 void **slot;
8162 struct attribute *attr;
8163 struct dwo_unit *dwo_unit;
8164
8165 attr = dwarf2_attr (comp_unit_die, DW_AT_GNU_dwo_id, cu);
8166 if (attr == NULL)
8167 {
8168 error (_("Dwarf Error: debug entry at offset 0x%x is missing"
8169 " its dwo_id [in module %s]"),
8170 offset.sect_off, dwo_file->dwo_name);
8171 return;
8172 }
8173
8174 dwo_unit = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct dwo_unit);
8175 dwo_unit->dwo_file = dwo_file;
8176 dwo_unit->signature = DW_UNSND (attr);
8177 dwo_unit->info_or_types_section = section;
8178 dwo_unit->offset = offset;
8179 dwo_unit->length = cu->per_cu->length;
8180
8181 slot = htab_find_slot (cu_htab, dwo_unit, INSERT);
8182 gdb_assert (slot != NULL);
8183 if (*slot != NULL)
8184 {
8185 const struct dwo_unit *dup_dwo_unit = *slot;
8186
8187 complaint (&symfile_complaints,
8188 _("debug entry at offset 0x%x is duplicate to the entry at"
8189 " offset 0x%x, dwo_id 0x%s [in module %s]"),
8190 offset.sect_off, dup_dwo_unit->offset.sect_off,
8191 phex (dwo_unit->signature, sizeof (dwo_unit->signature)),
8192 dwo_file->dwo_name);
8193 }
8194 else
8195 *slot = dwo_unit;
8196
09406207 8197 if (dwarf2_read_debug)
3019eac3
DE
8198 fprintf_unfiltered (gdb_stdlog, " offset 0x%x, dwo_id 0x%s\n",
8199 offset.sect_off,
8200 phex (dwo_unit->signature,
8201 sizeof (dwo_unit->signature)));
8202}
8203
8204/* Create a hash table to map DWO IDs to their CU entry in .debug_info.dwo. */
8205
8206static htab_t
8207create_debug_info_hash_table (struct dwo_file *dwo_file)
8208{
8209 struct objfile *objfile = dwarf2_per_objfile->objfile;
8210 struct dwarf2_section_info *section = &dwo_file->sections.info;
8211 bfd *abfd;
8212 htab_t cu_htab;
8213 gdb_byte *info_ptr, *end_ptr;
8214 struct create_dwo_info_table_data create_dwo_info_table_data;
8215
8216 dwarf2_read_section (objfile, section);
8217 info_ptr = section->buffer;
8218
8219 if (info_ptr == NULL)
8220 return NULL;
8221
8222 /* We can't set abfd until now because the section may be empty or
8223 not present, in which case section->asection will be NULL. */
8224 abfd = section->asection->owner;
8225
09406207 8226 if (dwarf2_read_debug)
3019eac3
DE
8227 fprintf_unfiltered (gdb_stdlog, "Reading .debug_info.dwo for %s:\n",
8228 bfd_get_filename (abfd));
8229
8230 cu_htab = allocate_dwo_unit_table (objfile);
8231
8232 create_dwo_info_table_data.dwo_file = dwo_file;
8233 create_dwo_info_table_data.cu_htab = cu_htab;
8234
8235 end_ptr = info_ptr + section->size;
8236 while (info_ptr < end_ptr)
8237 {
8238 struct dwarf2_per_cu_data per_cu;
8239
8240 memset (&per_cu, 0, sizeof (per_cu));
8241 per_cu.objfile = objfile;
8242 per_cu.is_debug_types = 0;
8243 per_cu.offset.sect_off = info_ptr - section->buffer;
8244 per_cu.info_or_types_section = section;
8245
8246 init_cutu_and_read_dies_no_follow (&per_cu,
8247 &dwo_file->sections.abbrev,
8248 dwo_file,
8249 create_debug_info_hash_table_reader,
8250 &create_dwo_info_table_data);
8251
8252 info_ptr += per_cu.length;
8253 }
8254
8255 return cu_htab;
8256}
8257
8258/* Subroutine of open_dwo_file to simplify it.
8259 Open the file specified by FILE_NAME and hand it off to BFD for
8260 preliminary analysis. Return a newly initialized bfd *, which
8261 includes a canonicalized copy of FILE_NAME.
8262 In case of trouble, return NULL.
8263 NOTE: This function is derived from symfile_bfd_open. */
8264
8265static bfd *
8266try_open_dwo_file (const char *file_name)
8267{
8268 bfd *sym_bfd;
8269 int desc;
8270 char *absolute_name;
3019eac3
DE
8271
8272 desc = openp (debug_file_directory, OPF_TRY_CWD_FIRST, file_name,
8273 O_RDONLY | O_BINARY, &absolute_name);
8274 if (desc < 0)
8275 return NULL;
8276
bb397797 8277 sym_bfd = gdb_bfd_open (absolute_name, gnutarget, desc);
3019eac3
DE
8278 if (!sym_bfd)
8279 {
3019eac3
DE
8280 xfree (absolute_name);
8281 return NULL;
8282 }
a4453b7e 8283 xfree (absolute_name);
3019eac3
DE
8284 bfd_set_cacheable (sym_bfd, 1);
8285
8286 if (!bfd_check_format (sym_bfd, bfd_object))
8287 {
cbb099e8 8288 gdb_bfd_unref (sym_bfd); /* This also closes desc. */
3019eac3
DE
8289 return NULL;
8290 }
8291
3019eac3
DE
8292 return sym_bfd;
8293}
8294
8295/* Try to open DWO file DWO_NAME.
8296 COMP_DIR is the DW_AT_comp_dir attribute.
8297 The result is the bfd handle of the file.
8298 If there is a problem finding or opening the file, return NULL.
8299 Upon success, the canonicalized path of the file is stored in the bfd,
8300 same as symfile_bfd_open. */
8301
8302static bfd *
8303open_dwo_file (const char *dwo_name, const char *comp_dir)
8304{
8305 bfd *abfd;
3019eac3
DE
8306
8307 if (IS_ABSOLUTE_PATH (dwo_name))
8308 return try_open_dwo_file (dwo_name);
8309
8310 /* Before trying the search path, try DWO_NAME in COMP_DIR. */
8311
8312 if (comp_dir != NULL)
8313 {
8314 char *path_to_try = concat (comp_dir, SLASH_STRING, dwo_name, NULL);
8315
8316 /* NOTE: If comp_dir is a relative path, this will also try the
8317 search path, which seems useful. */
8318 abfd = try_open_dwo_file (path_to_try);
8319 xfree (path_to_try);
8320 if (abfd != NULL)
8321 return abfd;
8322 }
8323
8324 /* That didn't work, try debug-file-directory, which, despite its name,
8325 is a list of paths. */
8326
8327 if (*debug_file_directory == '\0')
8328 return NULL;
8329
8330 return try_open_dwo_file (dwo_name);
8331}
8332
8333/* Initialize the use of the DWO file specified by DWO_NAME. */
8334
8335static struct dwo_file *
8336init_dwo_file (const char *dwo_name, const char *comp_dir)
8337{
8338 struct objfile *objfile = dwarf2_per_objfile->objfile;
8339 struct dwo_file *dwo_file = OBSTACK_ZALLOC (&objfile->objfile_obstack,
8340 struct dwo_file);
8341 bfd *abfd;
8342 struct cleanup *cleanups;
8343
09406207 8344 if (dwarf2_read_debug)
3019eac3
DE
8345 fprintf_unfiltered (gdb_stdlog, "Reading DWO file %s:\n", dwo_name);
8346
8347 abfd = open_dwo_file (dwo_name, comp_dir);
8348 if (abfd == NULL)
8349 return NULL;
8350 dwo_file->dwo_name = dwo_name;
8351 dwo_file->dwo_bfd = abfd;
8352
8353 cleanups = make_cleanup (free_dwo_file_cleanup, dwo_file);
8354
8355 bfd_map_over_sections (abfd, dwarf2_locate_dwo_sections, dwo_file);
8356
8357 dwo_file->cus = create_debug_info_hash_table (dwo_file);
8358
8359 dwo_file->tus = create_debug_types_hash_table (dwo_file,
8360 dwo_file->sections.types);
8361
8362 discard_cleanups (cleanups);
8363
8364 return dwo_file;
8365}
8366
8367/* Lookup DWO file DWO_NAME. */
8368
8369static struct dwo_file *
8370lookup_dwo_file (char *dwo_name, const char *comp_dir)
8371{
8372 struct dwo_file *dwo_file;
8373 struct dwo_file find_entry;
8374 void **slot;
8375
8376 if (dwarf2_per_objfile->dwo_files == NULL)
8377 dwarf2_per_objfile->dwo_files = allocate_dwo_file_hash_table ();
8378
8379 /* Have we already seen this DWO file? */
8380 find_entry.dwo_name = dwo_name;
8381 slot = htab_find_slot (dwarf2_per_objfile->dwo_files, &find_entry, INSERT);
8382
8383 /* If not, read it in and build a table of the DWOs it contains. */
8384 if (*slot == NULL)
8385 *slot = init_dwo_file (dwo_name, comp_dir);
8386
8387 /* NOTE: This will be NULL if unable to open the file. */
8388 dwo_file = *slot;
8389
8390 return dwo_file;
8391}
8392
8393/* Lookup the DWO CU referenced from THIS_CU in DWO file DWO_NAME.
8394 If non-NULL, comp_dir is the DW_AT_comp_dir attribute.
8395 SIGNATURE is the "dwo_id" of the CU (for consistency we use the same
8396 nomenclature as TUs).
1c658ad5 8397 The result is a pointer to the dwo_unit object or NULL if we didn't find it
3019eac3
DE
8398 (dwo_id mismatch or couldn't find the DWO file). */
8399
8400static struct dwo_unit *
8401lookup_dwo_comp_unit (struct dwarf2_per_cu_data *this_cu,
8402 char *dwo_name, const char *comp_dir,
8403 ULONGEST signature)
8404{
8405 struct objfile *objfile = dwarf2_per_objfile->objfile;
8406 struct dwo_file *dwo_file;
8407
8408 dwo_file = lookup_dwo_file (dwo_name, comp_dir);
8409 if (dwo_file == NULL)
8410 return NULL;
8411
8412 /* Look up the DWO using its signature(dwo_id). */
8413
8414 if (dwo_file->cus != NULL)
8415 {
8416 struct dwo_unit find_dwo_cu, *dwo_cu;
8417
8418 find_dwo_cu.signature = signature;
8419 dwo_cu = htab_find (dwo_file->cus, &find_dwo_cu);
a766d390 8420
3019eac3
DE
8421 if (dwo_cu != NULL)
8422 return dwo_cu;
8423 }
c906108c 8424
3019eac3 8425 /* We didn't find it. This must mean a dwo_id mismatch. */
df8a16a1 8426
3019eac3
DE
8427 complaint (&symfile_complaints,
8428 _("Could not find DWO CU referenced by CU at offset 0x%x"
8429 " [in module %s]"),
8430 this_cu->offset.sect_off, objfile->name);
8431 return NULL;
8432}
c906108c 8433
3019eac3
DE
8434/* Lookup the DWO TU referenced from THIS_TU in DWO file DWO_NAME.
8435 If non-NULL, comp_dir is the DW_AT_comp_dir attribute.
1c658ad5 8436 The result is a pointer to the dwo_unit object or NULL if we didn't find it
3019eac3 8437 (dwo_id mismatch or couldn't find the DWO file). */
debd256d 8438
3019eac3
DE
8439static struct dwo_unit *
8440lookup_dwo_type_unit (struct signatured_type *this_tu,
8441 char *dwo_name, const char *comp_dir)
8442{
8443 struct objfile *objfile = dwarf2_per_objfile->objfile;
8444 struct dwo_file *dwo_file;
cb1df416 8445
3019eac3
DE
8446 dwo_file = lookup_dwo_file (dwo_name, comp_dir);
8447 if (dwo_file == NULL)
8448 return NULL;
cf2c3c16 8449
3019eac3
DE
8450 /* Look up the DWO using its signature(dwo_id). */
8451
8452 if (dwo_file->tus != NULL)
cf2c3c16 8453 {
3019eac3 8454 struct dwo_unit find_dwo_tu, *dwo_tu;
9a619af0 8455
3019eac3
DE
8456 find_dwo_tu.signature = this_tu->signature;
8457 dwo_tu = htab_find (dwo_file->tus, &find_dwo_tu);
8458
8459 if (dwo_tu != NULL)
8460 return dwo_tu;
2e276125 8461 }
9cdd5dbd 8462
3019eac3
DE
8463 /* We didn't find it. This must mean a dwo_id mismatch. */
8464
8465 complaint (&symfile_complaints,
8466 _("Could not find DWO TU referenced by TU at offset 0x%x"
8467 " [in module %s]"),
8468 this_tu->per_cu.offset.sect_off, objfile->name);
8469 return NULL;
5fb290d7
DJ
8470}
8471
3019eac3
DE
8472/* Free all resources associated with DWO_FILE.
8473 Close the DWO file and munmap the sections.
8474 All memory should be on the objfile obstack. */
348e048f
DE
8475
8476static void
3019eac3 8477free_dwo_file (struct dwo_file *dwo_file, struct objfile *objfile)
348e048f 8478{
3019eac3
DE
8479 int ix;
8480 struct dwarf2_section_info *section;
348e048f 8481
3019eac3 8482 gdb_assert (dwo_file->dwo_bfd != objfile->obfd);
cbb099e8 8483 gdb_bfd_unref (dwo_file->dwo_bfd);
348e048f 8484
3019eac3
DE
8485 VEC_free (dwarf2_section_info_def, dwo_file->sections.types);
8486}
348e048f 8487
3019eac3 8488/* Wrapper for free_dwo_file for use in cleanups. */
348e048f 8489
3019eac3
DE
8490static void
8491free_dwo_file_cleanup (void *arg)
8492{
8493 struct dwo_file *dwo_file = (struct dwo_file *) arg;
8494 struct objfile *objfile = dwarf2_per_objfile->objfile;
348e048f 8495
3019eac3
DE
8496 free_dwo_file (dwo_file, objfile);
8497}
348e048f 8498
3019eac3 8499/* Traversal function for free_dwo_files. */
2ab95328 8500
3019eac3
DE
8501static int
8502free_dwo_file_from_slot (void **slot, void *info)
8503{
8504 struct dwo_file *dwo_file = (struct dwo_file *) *slot;
8505 struct objfile *objfile = (struct objfile *) info;
348e048f 8506
3019eac3 8507 free_dwo_file (dwo_file, objfile);
348e048f 8508
3019eac3
DE
8509 return 1;
8510}
348e048f 8511
3019eac3 8512/* Free all resources associated with DWO_FILES. */
348e048f 8513
3019eac3
DE
8514static void
8515free_dwo_files (htab_t dwo_files, struct objfile *objfile)
8516{
8517 htab_traverse_noresize (dwo_files, free_dwo_file_from_slot, objfile);
348e048f 8518}
3019eac3
DE
8519\f
8520/* Read in various DIEs. */
348e048f 8521
d389af10
JK
8522/* qsort helper for inherit_abstract_dies. */
8523
8524static int
8525unsigned_int_compar (const void *ap, const void *bp)
8526{
8527 unsigned int a = *(unsigned int *) ap;
8528 unsigned int b = *(unsigned int *) bp;
8529
8530 return (a > b) - (b > a);
8531}
8532
8533/* DW_AT_abstract_origin inherits whole DIEs (not just their attributes).
3e43a32a
MS
8534 Inherit only the children of the DW_AT_abstract_origin DIE not being
8535 already referenced by DW_AT_abstract_origin from the children of the
8536 current DIE. */
d389af10
JK
8537
8538static void
8539inherit_abstract_dies (struct die_info *die, struct dwarf2_cu *cu)
8540{
8541 struct die_info *child_die;
8542 unsigned die_children_count;
8543 /* CU offsets which were referenced by children of the current DIE. */
b64f50a1
JK
8544 sect_offset *offsets;
8545 sect_offset *offsets_end, *offsetp;
d389af10
JK
8546 /* Parent of DIE - referenced by DW_AT_abstract_origin. */
8547 struct die_info *origin_die;
8548 /* Iterator of the ORIGIN_DIE children. */
8549 struct die_info *origin_child_die;
8550 struct cleanup *cleanups;
8551 struct attribute *attr;
cd02d79d
PA
8552 struct dwarf2_cu *origin_cu;
8553 struct pending **origin_previous_list_in_scope;
d389af10
JK
8554
8555 attr = dwarf2_attr (die, DW_AT_abstract_origin, cu);
8556 if (!attr)
8557 return;
8558
cd02d79d
PA
8559 /* Note that following die references may follow to a die in a
8560 different cu. */
8561
8562 origin_cu = cu;
8563 origin_die = follow_die_ref (die, attr, &origin_cu);
8564
8565 /* We're inheriting ORIGIN's children into the scope we'd put DIE's
8566 symbols in. */
8567 origin_previous_list_in_scope = origin_cu->list_in_scope;
8568 origin_cu->list_in_scope = cu->list_in_scope;
8569
edb3359d
DJ
8570 if (die->tag != origin_die->tag
8571 && !(die->tag == DW_TAG_inlined_subroutine
8572 && origin_die->tag == DW_TAG_subprogram))
d389af10
JK
8573 complaint (&symfile_complaints,
8574 _("DIE 0x%x and its abstract origin 0x%x have different tags"),
b64f50a1 8575 die->offset.sect_off, origin_die->offset.sect_off);
d389af10
JK
8576
8577 child_die = die->child;
8578 die_children_count = 0;
8579 while (child_die && child_die->tag)
8580 {
8581 child_die = sibling_die (child_die);
8582 die_children_count++;
8583 }
8584 offsets = xmalloc (sizeof (*offsets) * die_children_count);
8585 cleanups = make_cleanup (xfree, offsets);
8586
8587 offsets_end = offsets;
8588 child_die = die->child;
8589 while (child_die && child_die->tag)
8590 {
c38f313d
DJ
8591 /* For each CHILD_DIE, find the corresponding child of
8592 ORIGIN_DIE. If there is more than one layer of
8593 DW_AT_abstract_origin, follow them all; there shouldn't be,
8594 but GCC versions at least through 4.4 generate this (GCC PR
8595 40573). */
8596 struct die_info *child_origin_die = child_die;
cd02d79d 8597 struct dwarf2_cu *child_origin_cu = cu;
9a619af0 8598
c38f313d
DJ
8599 while (1)
8600 {
cd02d79d
PA
8601 attr = dwarf2_attr (child_origin_die, DW_AT_abstract_origin,
8602 child_origin_cu);
c38f313d
DJ
8603 if (attr == NULL)
8604 break;
cd02d79d
PA
8605 child_origin_die = follow_die_ref (child_origin_die, attr,
8606 &child_origin_cu);
c38f313d
DJ
8607 }
8608
d389af10
JK
8609 /* According to DWARF3 3.3.8.2 #3 new entries without their abstract
8610 counterpart may exist. */
c38f313d 8611 if (child_origin_die != child_die)
d389af10 8612 {
edb3359d
DJ
8613 if (child_die->tag != child_origin_die->tag
8614 && !(child_die->tag == DW_TAG_inlined_subroutine
8615 && child_origin_die->tag == DW_TAG_subprogram))
d389af10
JK
8616 complaint (&symfile_complaints,
8617 _("Child DIE 0x%x and its abstract origin 0x%x have "
b64f50a1
JK
8618 "different tags"), child_die->offset.sect_off,
8619 child_origin_die->offset.sect_off);
c38f313d
DJ
8620 if (child_origin_die->parent != origin_die)
8621 complaint (&symfile_complaints,
8622 _("Child DIE 0x%x and its abstract origin 0x%x have "
b64f50a1
JK
8623 "different parents"), child_die->offset.sect_off,
8624 child_origin_die->offset.sect_off);
c38f313d
DJ
8625 else
8626 *offsets_end++ = child_origin_die->offset;
d389af10
JK
8627 }
8628 child_die = sibling_die (child_die);
8629 }
8630 qsort (offsets, offsets_end - offsets, sizeof (*offsets),
8631 unsigned_int_compar);
8632 for (offsetp = offsets + 1; offsetp < offsets_end; offsetp++)
b64f50a1 8633 if (offsetp[-1].sect_off == offsetp->sect_off)
3e43a32a
MS
8634 complaint (&symfile_complaints,
8635 _("Multiple children of DIE 0x%x refer "
8636 "to DIE 0x%x as their abstract origin"),
b64f50a1 8637 die->offset.sect_off, offsetp->sect_off);
d389af10
JK
8638
8639 offsetp = offsets;
8640 origin_child_die = origin_die->child;
8641 while (origin_child_die && origin_child_die->tag)
8642 {
8643 /* Is ORIGIN_CHILD_DIE referenced by any of the DIE children? */
b64f50a1
JK
8644 while (offsetp < offsets_end
8645 && offsetp->sect_off < origin_child_die->offset.sect_off)
d389af10 8646 offsetp++;
b64f50a1
JK
8647 if (offsetp >= offsets_end
8648 || offsetp->sect_off > origin_child_die->offset.sect_off)
d389af10
JK
8649 {
8650 /* Found that ORIGIN_CHILD_DIE is really not referenced. */
cd02d79d 8651 process_die (origin_child_die, origin_cu);
d389af10
JK
8652 }
8653 origin_child_die = sibling_die (origin_child_die);
8654 }
cd02d79d 8655 origin_cu->list_in_scope = origin_previous_list_in_scope;
d389af10
JK
8656
8657 do_cleanups (cleanups);
8658}
8659
c906108c 8660static void
e7c27a73 8661read_func_scope (struct die_info *die, struct dwarf2_cu *cu)
c906108c 8662{
e7c27a73 8663 struct objfile *objfile = cu->objfile;
52f0bd74 8664 struct context_stack *new;
c906108c
SS
8665 CORE_ADDR lowpc;
8666 CORE_ADDR highpc;
8667 struct die_info *child_die;
edb3359d 8668 struct attribute *attr, *call_line, *call_file;
c906108c 8669 char *name;
e142c38c 8670 CORE_ADDR baseaddr;
801e3a5b 8671 struct block *block;
edb3359d 8672 int inlined_func = (die->tag == DW_TAG_inlined_subroutine);
34eaf542
TT
8673 VEC (symbolp) *template_args = NULL;
8674 struct template_symbol *templ_func = NULL;
edb3359d
DJ
8675
8676 if (inlined_func)
8677 {
8678 /* If we do not have call site information, we can't show the
8679 caller of this inlined function. That's too confusing, so
8680 only use the scope for local variables. */
8681 call_line = dwarf2_attr (die, DW_AT_call_line, cu);
8682 call_file = dwarf2_attr (die, DW_AT_call_file, cu);
8683 if (call_line == NULL || call_file == NULL)
8684 {
8685 read_lexical_block_scope (die, cu);
8686 return;
8687 }
8688 }
c906108c 8689
e142c38c
DJ
8690 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
8691
94af9270 8692 name = dwarf2_name (die, cu);
c906108c 8693
e8d05480
JB
8694 /* Ignore functions with missing or empty names. These are actually
8695 illegal according to the DWARF standard. */
8696 if (name == NULL)
8697 {
8698 complaint (&symfile_complaints,
b64f50a1
JK
8699 _("missing name for subprogram DIE at %d"),
8700 die->offset.sect_off);
e8d05480
JB
8701 return;
8702 }
8703
8704 /* Ignore functions with missing or invalid low and high pc attributes. */
8705 if (!dwarf2_get_pc_bounds (die, &lowpc, &highpc, cu, NULL))
8706 {
ae4d0c03
PM
8707 attr = dwarf2_attr (die, DW_AT_external, cu);
8708 if (!attr || !DW_UNSND (attr))
8709 complaint (&symfile_complaints,
3e43a32a
MS
8710 _("cannot get low and high bounds "
8711 "for subprogram DIE at %d"),
b64f50a1 8712 die->offset.sect_off);
e8d05480
JB
8713 return;
8714 }
c906108c
SS
8715
8716 lowpc += baseaddr;
8717 highpc += baseaddr;
8718
34eaf542
TT
8719 /* If we have any template arguments, then we must allocate a
8720 different sort of symbol. */
8721 for (child_die = die->child; child_die; child_die = sibling_die (child_die))
8722 {
8723 if (child_die->tag == DW_TAG_template_type_param
8724 || child_die->tag == DW_TAG_template_value_param)
8725 {
8726 templ_func = OBSTACK_ZALLOC (&objfile->objfile_obstack,
8727 struct template_symbol);
8728 templ_func->base.is_cplus_template_function = 1;
8729 break;
8730 }
8731 }
8732
c906108c 8733 new = push_context (0, lowpc);
34eaf542
TT
8734 new->name = new_symbol_full (die, read_type_die (die, cu), cu,
8735 (struct symbol *) templ_func);
4c2df51b 8736
4cecd739
DJ
8737 /* If there is a location expression for DW_AT_frame_base, record
8738 it. */
e142c38c 8739 attr = dwarf2_attr (die, DW_AT_frame_base, cu);
4c2df51b 8740 if (attr)
c034e007
AC
8741 /* FIXME: cagney/2004-01-26: The DW_AT_frame_base's location
8742 expression is being recorded directly in the function's symbol
8743 and not in a separate frame-base object. I guess this hack is
8744 to avoid adding some sort of frame-base adjunct/annex to the
8745 function's symbol :-(. The problem with doing this is that it
8746 results in a function symbol with a location expression that
8747 has nothing to do with the location of the function, ouch! The
8748 relationship should be: a function's symbol has-a frame base; a
8749 frame-base has-a location expression. */
e7c27a73 8750 dwarf2_symbol_mark_computed (attr, new->name, cu);
4c2df51b 8751
e142c38c 8752 cu->list_in_scope = &local_symbols;
c906108c 8753
639d11d3 8754 if (die->child != NULL)
c906108c 8755 {
639d11d3 8756 child_die = die->child;
c906108c
SS
8757 while (child_die && child_die->tag)
8758 {
34eaf542
TT
8759 if (child_die->tag == DW_TAG_template_type_param
8760 || child_die->tag == DW_TAG_template_value_param)
8761 {
8762 struct symbol *arg = new_symbol (child_die, NULL, cu);
8763
f1078f66
DJ
8764 if (arg != NULL)
8765 VEC_safe_push (symbolp, template_args, arg);
34eaf542
TT
8766 }
8767 else
8768 process_die (child_die, cu);
c906108c
SS
8769 child_die = sibling_die (child_die);
8770 }
8771 }
8772
d389af10
JK
8773 inherit_abstract_dies (die, cu);
8774
4a811a97
UW
8775 /* If we have a DW_AT_specification, we might need to import using
8776 directives from the context of the specification DIE. See the
8777 comment in determine_prefix. */
8778 if (cu->language == language_cplus
8779 && dwarf2_attr (die, DW_AT_specification, cu))
8780 {
8781 struct dwarf2_cu *spec_cu = cu;
8782 struct die_info *spec_die = die_specification (die, &spec_cu);
8783
8784 while (spec_die)
8785 {
8786 child_die = spec_die->child;
8787 while (child_die && child_die->tag)
8788 {
8789 if (child_die->tag == DW_TAG_imported_module)
8790 process_die (child_die, spec_cu);
8791 child_die = sibling_die (child_die);
8792 }
8793
8794 /* In some cases, GCC generates specification DIEs that
8795 themselves contain DW_AT_specification attributes. */
8796 spec_die = die_specification (spec_die, &spec_cu);
8797 }
8798 }
8799
c906108c
SS
8800 new = pop_context ();
8801 /* Make a block for the local symbols within. */
801e3a5b
JB
8802 block = finish_block (new->name, &local_symbols, new->old_blocks,
8803 lowpc, highpc, objfile);
8804
df8a16a1 8805 /* For C++, set the block's scope. */
f55ee35c 8806 if (cu->language == language_cplus || cu->language == language_fortran)
df8a16a1 8807 cp_set_block_scope (new->name, block, &objfile->objfile_obstack,
0114d602 8808 determine_prefix (die, cu),
df8a16a1
DJ
8809 processing_has_namespace_info);
8810
801e3a5b
JB
8811 /* If we have address ranges, record them. */
8812 dwarf2_record_block_ranges (die, block, baseaddr, cu);
6e70227d 8813
34eaf542
TT
8814 /* Attach template arguments to function. */
8815 if (! VEC_empty (symbolp, template_args))
8816 {
8817 gdb_assert (templ_func != NULL);
8818
8819 templ_func->n_template_arguments = VEC_length (symbolp, template_args);
8820 templ_func->template_arguments
8821 = obstack_alloc (&objfile->objfile_obstack,
8822 (templ_func->n_template_arguments
8823 * sizeof (struct symbol *)));
8824 memcpy (templ_func->template_arguments,
8825 VEC_address (symbolp, template_args),
8826 (templ_func->n_template_arguments * sizeof (struct symbol *)));
8827 VEC_free (symbolp, template_args);
8828 }
8829
208d8187
JB
8830 /* In C++, we can have functions nested inside functions (e.g., when
8831 a function declares a class that has methods). This means that
8832 when we finish processing a function scope, we may need to go
8833 back to building a containing block's symbol lists. */
8834 local_symbols = new->locals;
8835 param_symbols = new->params;
27aa8d6a 8836 using_directives = new->using_directives;
208d8187 8837
921e78cf
JB
8838 /* If we've finished processing a top-level function, subsequent
8839 symbols go in the file symbol list. */
8840 if (outermost_context_p ())
e142c38c 8841 cu->list_in_scope = &file_symbols;
c906108c
SS
8842}
8843
8844/* Process all the DIES contained within a lexical block scope. Start
8845 a new scope, process the dies, and then close the scope. */
8846
8847static void
e7c27a73 8848read_lexical_block_scope (struct die_info *die, struct dwarf2_cu *cu)
c906108c 8849{
e7c27a73 8850 struct objfile *objfile = cu->objfile;
52f0bd74 8851 struct context_stack *new;
c906108c
SS
8852 CORE_ADDR lowpc, highpc;
8853 struct die_info *child_die;
e142c38c
DJ
8854 CORE_ADDR baseaddr;
8855
8856 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
c906108c
SS
8857
8858 /* Ignore blocks with missing or invalid low and high pc attributes. */
af34e669
DJ
8859 /* ??? Perhaps consider discontiguous blocks defined by DW_AT_ranges
8860 as multiple lexical blocks? Handling children in a sane way would
6e70227d 8861 be nasty. Might be easier to properly extend generic blocks to
af34e669 8862 describe ranges. */
d85a05f0 8863 if (!dwarf2_get_pc_bounds (die, &lowpc, &highpc, cu, NULL))
c906108c
SS
8864 return;
8865 lowpc += baseaddr;
8866 highpc += baseaddr;
8867
8868 push_context (0, lowpc);
639d11d3 8869 if (die->child != NULL)
c906108c 8870 {
639d11d3 8871 child_die = die->child;
c906108c
SS
8872 while (child_die && child_die->tag)
8873 {
e7c27a73 8874 process_die (child_die, cu);
c906108c
SS
8875 child_die = sibling_die (child_die);
8876 }
8877 }
8878 new = pop_context ();
8879
8540c487 8880 if (local_symbols != NULL || using_directives != NULL)
c906108c 8881 {
801e3a5b
JB
8882 struct block *block
8883 = finish_block (0, &local_symbols, new->old_blocks, new->start_addr,
8884 highpc, objfile);
8885
8886 /* Note that recording ranges after traversing children, as we
8887 do here, means that recording a parent's ranges entails
8888 walking across all its children's ranges as they appear in
8889 the address map, which is quadratic behavior.
8890
8891 It would be nicer to record the parent's ranges before
8892 traversing its children, simply overriding whatever you find
8893 there. But since we don't even decide whether to create a
8894 block until after we've traversed its children, that's hard
8895 to do. */
8896 dwarf2_record_block_ranges (die, block, baseaddr, cu);
c906108c
SS
8897 }
8898 local_symbols = new->locals;
27aa8d6a 8899 using_directives = new->using_directives;
c906108c
SS
8900}
8901
96408a79
SA
8902/* Read in DW_TAG_GNU_call_site and insert it to CU->call_site_htab. */
8903
8904static void
8905read_call_site_scope (struct die_info *die, struct dwarf2_cu *cu)
8906{
8907 struct objfile *objfile = cu->objfile;
8908 struct gdbarch *gdbarch = get_objfile_arch (objfile);
8909 CORE_ADDR pc, baseaddr;
8910 struct attribute *attr;
8911 struct call_site *call_site, call_site_local;
8912 void **slot;
8913 int nparams;
8914 struct die_info *child_die;
8915
8916 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
8917
8918 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
8919 if (!attr)
8920 {
8921 complaint (&symfile_complaints,
8922 _("missing DW_AT_low_pc for DW_TAG_GNU_call_site "
8923 "DIE 0x%x [in module %s]"),
b64f50a1 8924 die->offset.sect_off, objfile->name);
96408a79
SA
8925 return;
8926 }
8927 pc = DW_ADDR (attr) + baseaddr;
8928
8929 if (cu->call_site_htab == NULL)
8930 cu->call_site_htab = htab_create_alloc_ex (16, core_addr_hash, core_addr_eq,
8931 NULL, &objfile->objfile_obstack,
8932 hashtab_obstack_allocate, NULL);
8933 call_site_local.pc = pc;
8934 slot = htab_find_slot (cu->call_site_htab, &call_site_local, INSERT);
8935 if (*slot != NULL)
8936 {
8937 complaint (&symfile_complaints,
8938 _("Duplicate PC %s for DW_TAG_GNU_call_site "
8939 "DIE 0x%x [in module %s]"),
b64f50a1 8940 paddress (gdbarch, pc), die->offset.sect_off, objfile->name);
96408a79
SA
8941 return;
8942 }
8943
8944 /* Count parameters at the caller. */
8945
8946 nparams = 0;
8947 for (child_die = die->child; child_die && child_die->tag;
8948 child_die = sibling_die (child_die))
8949 {
8950 if (child_die->tag != DW_TAG_GNU_call_site_parameter)
8951 {
8952 complaint (&symfile_complaints,
8953 _("Tag %d is not DW_TAG_GNU_call_site_parameter in "
8954 "DW_TAG_GNU_call_site child DIE 0x%x [in module %s]"),
b64f50a1 8955 child_die->tag, child_die->offset.sect_off, objfile->name);
96408a79
SA
8956 continue;
8957 }
8958
8959 nparams++;
8960 }
8961
8962 call_site = obstack_alloc (&objfile->objfile_obstack,
8963 (sizeof (*call_site)
8964 + (sizeof (*call_site->parameter)
8965 * (nparams - 1))));
8966 *slot = call_site;
8967 memset (call_site, 0, sizeof (*call_site) - sizeof (*call_site->parameter));
8968 call_site->pc = pc;
8969
8970 if (dwarf2_flag_true_p (die, DW_AT_GNU_tail_call, cu))
8971 {
8972 struct die_info *func_die;
8973
8974 /* Skip also over DW_TAG_inlined_subroutine. */
8975 for (func_die = die->parent;
8976 func_die && func_die->tag != DW_TAG_subprogram
8977 && func_die->tag != DW_TAG_subroutine_type;
8978 func_die = func_die->parent);
8979
8980 /* DW_AT_GNU_all_call_sites is a superset
8981 of DW_AT_GNU_all_tail_call_sites. */
8982 if (func_die
8983 && !dwarf2_flag_true_p (func_die, DW_AT_GNU_all_call_sites, cu)
8984 && !dwarf2_flag_true_p (func_die, DW_AT_GNU_all_tail_call_sites, cu))
8985 {
8986 /* TYPE_TAIL_CALL_LIST is not interesting in functions where it is
8987 not complete. But keep CALL_SITE for look ups via call_site_htab,
8988 both the initial caller containing the real return address PC and
8989 the final callee containing the current PC of a chain of tail
8990 calls do not need to have the tail call list complete. But any
8991 function candidate for a virtual tail call frame searched via
8992 TYPE_TAIL_CALL_LIST must have the tail call list complete to be
8993 determined unambiguously. */
8994 }
8995 else
8996 {
8997 struct type *func_type = NULL;
8998
8999 if (func_die)
9000 func_type = get_die_type (func_die, cu);
9001 if (func_type != NULL)
9002 {
9003 gdb_assert (TYPE_CODE (func_type) == TYPE_CODE_FUNC);
9004
9005 /* Enlist this call site to the function. */
9006 call_site->tail_call_next = TYPE_TAIL_CALL_LIST (func_type);
9007 TYPE_TAIL_CALL_LIST (func_type) = call_site;
9008 }
9009 else
9010 complaint (&symfile_complaints,
9011 _("Cannot find function owning DW_TAG_GNU_call_site "
9012 "DIE 0x%x [in module %s]"),
b64f50a1 9013 die->offset.sect_off, objfile->name);
96408a79
SA
9014 }
9015 }
9016
9017 attr = dwarf2_attr (die, DW_AT_GNU_call_site_target, cu);
9018 if (attr == NULL)
9019 attr = dwarf2_attr (die, DW_AT_abstract_origin, cu);
9020 SET_FIELD_DWARF_BLOCK (call_site->target, NULL);
9021 if (!attr || (attr_form_is_block (attr) && DW_BLOCK (attr)->size == 0))
9022 /* Keep NULL DWARF_BLOCK. */;
9023 else if (attr_form_is_block (attr))
9024 {
9025 struct dwarf2_locexpr_baton *dlbaton;
9026
9027 dlbaton = obstack_alloc (&objfile->objfile_obstack, sizeof (*dlbaton));
9028 dlbaton->data = DW_BLOCK (attr)->data;
9029 dlbaton->size = DW_BLOCK (attr)->size;
9030 dlbaton->per_cu = cu->per_cu;
9031
9032 SET_FIELD_DWARF_BLOCK (call_site->target, dlbaton);
9033 }
9034 else if (is_ref_attr (attr))
9035 {
96408a79
SA
9036 struct dwarf2_cu *target_cu = cu;
9037 struct die_info *target_die;
9038
9039 target_die = follow_die_ref_or_sig (die, attr, &target_cu);
9040 gdb_assert (target_cu->objfile == objfile);
9041 if (die_is_declaration (target_die, target_cu))
9042 {
9043 const char *target_physname;
9044
9045 target_physname = dwarf2_physname (NULL, target_die, target_cu);
9046 if (target_physname == NULL)
9047 complaint (&symfile_complaints,
9048 _("DW_AT_GNU_call_site_target target DIE has invalid "
9049 "physname, for referencing DIE 0x%x [in module %s]"),
b64f50a1 9050 die->offset.sect_off, objfile->name);
96408a79
SA
9051 else
9052 SET_FIELD_PHYSNAME (call_site->target, (char *) target_physname);
9053 }
9054 else
9055 {
9056 CORE_ADDR lowpc;
9057
9058 /* DW_AT_entry_pc should be preferred. */
9059 if (!dwarf2_get_pc_bounds (target_die, &lowpc, NULL, target_cu, NULL))
9060 complaint (&symfile_complaints,
9061 _("DW_AT_GNU_call_site_target target DIE has invalid "
9062 "low pc, for referencing DIE 0x%x [in module %s]"),
b64f50a1 9063 die->offset.sect_off, objfile->name);
96408a79
SA
9064 else
9065 SET_FIELD_PHYSADDR (call_site->target, lowpc + baseaddr);
9066 }
9067 }
9068 else
9069 complaint (&symfile_complaints,
9070 _("DW_TAG_GNU_call_site DW_AT_GNU_call_site_target is neither "
9071 "block nor reference, for DIE 0x%x [in module %s]"),
b64f50a1 9072 die->offset.sect_off, objfile->name);
96408a79
SA
9073
9074 call_site->per_cu = cu->per_cu;
9075
9076 for (child_die = die->child;
9077 child_die && child_die->tag;
9078 child_die = sibling_die (child_die))
9079 {
96408a79 9080 struct call_site_parameter *parameter;
1788b2d3 9081 struct attribute *loc, *origin;
96408a79
SA
9082
9083 if (child_die->tag != DW_TAG_GNU_call_site_parameter)
9084 {
9085 /* Already printed the complaint above. */
9086 continue;
9087 }
9088
9089 gdb_assert (call_site->parameter_count < nparams);
9090 parameter = &call_site->parameter[call_site->parameter_count];
9091
1788b2d3
JK
9092 /* DW_AT_location specifies the register number or DW_AT_abstract_origin
9093 specifies DW_TAG_formal_parameter. Value of the data assumed for the
9094 register is contained in DW_AT_GNU_call_site_value. */
96408a79 9095
24c5c679 9096 loc = dwarf2_attr (child_die, DW_AT_location, cu);
1788b2d3
JK
9097 origin = dwarf2_attr (child_die, DW_AT_abstract_origin, cu);
9098 if (loc == NULL && origin != NULL && is_ref_attr (origin))
9099 {
9100 sect_offset offset;
9101
9102 parameter->kind = CALL_SITE_PARAMETER_PARAM_OFFSET;
9103 offset = dwarf2_get_ref_die_offset (origin);
d76b7dbc
JK
9104 if (!offset_in_cu_p (&cu->header, offset))
9105 {
9106 /* As DW_OP_GNU_parameter_ref uses CU-relative offset this
9107 binding can be done only inside one CU. Such referenced DIE
9108 therefore cannot be even moved to DW_TAG_partial_unit. */
9109 complaint (&symfile_complaints,
9110 _("DW_AT_abstract_origin offset is not in CU for "
9111 "DW_TAG_GNU_call_site child DIE 0x%x "
9112 "[in module %s]"),
9113 child_die->offset.sect_off, objfile->name);
9114 continue;
9115 }
1788b2d3
JK
9116 parameter->u.param_offset.cu_off = (offset.sect_off
9117 - cu->header.offset.sect_off);
9118 }
9119 else if (loc == NULL || origin != NULL || !attr_form_is_block (loc))
96408a79
SA
9120 {
9121 complaint (&symfile_complaints,
9122 _("No DW_FORM_block* DW_AT_location for "
9123 "DW_TAG_GNU_call_site child DIE 0x%x [in module %s]"),
b64f50a1 9124 child_die->offset.sect_off, objfile->name);
96408a79
SA
9125 continue;
9126 }
24c5c679 9127 else
96408a79 9128 {
24c5c679
JK
9129 parameter->u.dwarf_reg = dwarf_block_to_dwarf_reg
9130 (DW_BLOCK (loc)->data, &DW_BLOCK (loc)->data[DW_BLOCK (loc)->size]);
9131 if (parameter->u.dwarf_reg != -1)
9132 parameter->kind = CALL_SITE_PARAMETER_DWARF_REG;
9133 else if (dwarf_block_to_sp_offset (gdbarch, DW_BLOCK (loc)->data,
9134 &DW_BLOCK (loc)->data[DW_BLOCK (loc)->size],
9135 &parameter->u.fb_offset))
9136 parameter->kind = CALL_SITE_PARAMETER_FB_OFFSET;
9137 else
9138 {
9139 complaint (&symfile_complaints,
9140 _("Only single DW_OP_reg or DW_OP_fbreg is supported "
9141 "for DW_FORM_block* DW_AT_location is supported for "
9142 "DW_TAG_GNU_call_site child DIE 0x%x "
9143 "[in module %s]"),
9144 child_die->offset.sect_off, objfile->name);
9145 continue;
9146 }
96408a79
SA
9147 }
9148
9149 attr = dwarf2_attr (child_die, DW_AT_GNU_call_site_value, cu);
9150 if (!attr_form_is_block (attr))
9151 {
9152 complaint (&symfile_complaints,
9153 _("No DW_FORM_block* DW_AT_GNU_call_site_value for "
9154 "DW_TAG_GNU_call_site child DIE 0x%x [in module %s]"),
b64f50a1 9155 child_die->offset.sect_off, objfile->name);
96408a79
SA
9156 continue;
9157 }
9158 parameter->value = DW_BLOCK (attr)->data;
9159 parameter->value_size = DW_BLOCK (attr)->size;
9160
9161 /* Parameters are not pre-cleared by memset above. */
9162 parameter->data_value = NULL;
9163 parameter->data_value_size = 0;
9164 call_site->parameter_count++;
9165
9166 attr = dwarf2_attr (child_die, DW_AT_GNU_call_site_data_value, cu);
9167 if (attr)
9168 {
9169 if (!attr_form_is_block (attr))
9170 complaint (&symfile_complaints,
9171 _("No DW_FORM_block* DW_AT_GNU_call_site_data_value for "
9172 "DW_TAG_GNU_call_site child DIE 0x%x [in module %s]"),
b64f50a1 9173 child_die->offset.sect_off, objfile->name);
96408a79
SA
9174 else
9175 {
9176 parameter->data_value = DW_BLOCK (attr)->data;
9177 parameter->data_value_size = DW_BLOCK (attr)->size;
9178 }
9179 }
9180 }
9181}
9182
43039443 9183/* Get low and high pc attributes from DW_AT_ranges attribute value OFFSET.
ff013f42
JK
9184 Return 1 if the attributes are present and valid, otherwise, return 0.
9185 If RANGES_PST is not NULL we should setup `objfile->psymtabs_addrmap'. */
43039443
JK
9186
9187static int
9188dwarf2_ranges_read (unsigned offset, CORE_ADDR *low_return,
ff013f42
JK
9189 CORE_ADDR *high_return, struct dwarf2_cu *cu,
9190 struct partial_symtab *ranges_pst)
43039443
JK
9191{
9192 struct objfile *objfile = cu->objfile;
9193 struct comp_unit_head *cu_header = &cu->header;
9194 bfd *obfd = objfile->obfd;
9195 unsigned int addr_size = cu_header->addr_size;
9196 CORE_ADDR mask = ~(~(CORE_ADDR)1 << (addr_size * 8 - 1));
9197 /* Base address selection entry. */
9198 CORE_ADDR base;
9199 int found_base;
9200 unsigned int dummy;
9201 gdb_byte *buffer;
9202 CORE_ADDR marker;
9203 int low_set;
9204 CORE_ADDR low = 0;
9205 CORE_ADDR high = 0;
ff013f42 9206 CORE_ADDR baseaddr;
43039443 9207
d00adf39
DE
9208 found_base = cu->base_known;
9209 base = cu->base_address;
43039443 9210
be391dca 9211 dwarf2_read_section (objfile, &dwarf2_per_objfile->ranges);
dce234bc 9212 if (offset >= dwarf2_per_objfile->ranges.size)
43039443
JK
9213 {
9214 complaint (&symfile_complaints,
9215 _("Offset %d out of bounds for DW_AT_ranges attribute"),
9216 offset);
9217 return 0;
9218 }
dce234bc 9219 buffer = dwarf2_per_objfile->ranges.buffer + offset;
43039443
JK
9220
9221 /* Read in the largest possible address. */
9222 marker = read_address (obfd, buffer, cu, &dummy);
9223 if ((marker & mask) == mask)
9224 {
9225 /* If we found the largest possible address, then
9226 read the base address. */
9227 base = read_address (obfd, buffer + addr_size, cu, &dummy);
9228 buffer += 2 * addr_size;
9229 offset += 2 * addr_size;
9230 found_base = 1;
9231 }
9232
9233 low_set = 0;
9234
e7030f15 9235 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
ff013f42 9236
43039443
JK
9237 while (1)
9238 {
9239 CORE_ADDR range_beginning, range_end;
9240
9241 range_beginning = read_address (obfd, buffer, cu, &dummy);
9242 buffer += addr_size;
9243 range_end = read_address (obfd, buffer, cu, &dummy);
9244 buffer += addr_size;
9245 offset += 2 * addr_size;
9246
9247 /* An end of list marker is a pair of zero addresses. */
9248 if (range_beginning == 0 && range_end == 0)
9249 /* Found the end of list entry. */
9250 break;
9251
9252 /* Each base address selection entry is a pair of 2 values.
9253 The first is the largest possible address, the second is
9254 the base address. Check for a base address here. */
9255 if ((range_beginning & mask) == mask)
9256 {
9257 /* If we found the largest possible address, then
9258 read the base address. */
9259 base = read_address (obfd, buffer + addr_size, cu, &dummy);
9260 found_base = 1;
9261 continue;
9262 }
9263
9264 if (!found_base)
9265 {
9266 /* We have no valid base address for the ranges
9267 data. */
9268 complaint (&symfile_complaints,
9269 _("Invalid .debug_ranges data (no base address)"));
9270 return 0;
9271 }
9272
9277c30c
UW
9273 if (range_beginning > range_end)
9274 {
9275 /* Inverted range entries are invalid. */
9276 complaint (&symfile_complaints,
9277 _("Invalid .debug_ranges data (inverted range)"));
9278 return 0;
9279 }
9280
9281 /* Empty range entries have no effect. */
9282 if (range_beginning == range_end)
9283 continue;
9284
43039443
JK
9285 range_beginning += base;
9286 range_end += base;
9287
01093045
DE
9288 /* A not-uncommon case of bad debug info.
9289 Don't pollute the addrmap with bad data. */
9290 if (range_beginning + baseaddr == 0
9291 && !dwarf2_per_objfile->has_section_at_zero)
9292 {
9293 complaint (&symfile_complaints,
9294 _(".debug_ranges entry has start address of zero"
9295 " [in module %s]"), objfile->name);
9296 continue;
9297 }
9298
9277c30c 9299 if (ranges_pst != NULL)
ff013f42 9300 addrmap_set_empty (objfile->psymtabs_addrmap,
3e43a32a
MS
9301 range_beginning + baseaddr,
9302 range_end - 1 + baseaddr,
ff013f42
JK
9303 ranges_pst);
9304
43039443
JK
9305 /* FIXME: This is recording everything as a low-high
9306 segment of consecutive addresses. We should have a
9307 data structure for discontiguous block ranges
9308 instead. */
9309 if (! low_set)
9310 {
9311 low = range_beginning;
9312 high = range_end;
9313 low_set = 1;
9314 }
9315 else
9316 {
9317 if (range_beginning < low)
9318 low = range_beginning;
9319 if (range_end > high)
9320 high = range_end;
9321 }
9322 }
9323
9324 if (! low_set)
9325 /* If the first entry is an end-of-list marker, the range
9326 describes an empty scope, i.e. no instructions. */
9327 return 0;
9328
9329 if (low_return)
9330 *low_return = low;
9331 if (high_return)
9332 *high_return = high;
9333 return 1;
9334}
9335
af34e669
DJ
9336/* Get low and high pc attributes from a die. Return 1 if the attributes
9337 are present and valid, otherwise, return 0. Return -1 if the range is
9338 discontinuous, i.e. derived from DW_AT_ranges information. */
380bca97 9339
c906108c 9340static int
af34e669 9341dwarf2_get_pc_bounds (struct die_info *die, CORE_ADDR *lowpc,
d85a05f0
DJ
9342 CORE_ADDR *highpc, struct dwarf2_cu *cu,
9343 struct partial_symtab *pst)
c906108c
SS
9344{
9345 struct attribute *attr;
91da1414 9346 struct attribute *attr_high;
af34e669
DJ
9347 CORE_ADDR low = 0;
9348 CORE_ADDR high = 0;
9349 int ret = 0;
c906108c 9350
91da1414
MW
9351 attr_high = dwarf2_attr (die, DW_AT_high_pc, cu);
9352 if (attr_high)
af34e669 9353 {
e142c38c 9354 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
af34e669 9355 if (attr)
91da1414
MW
9356 {
9357 low = DW_ADDR (attr);
3019eac3
DE
9358 if (attr_high->form == DW_FORM_addr
9359 || attr_high->form == DW_FORM_GNU_addr_index)
91da1414
MW
9360 high = DW_ADDR (attr_high);
9361 else
9362 high = low + DW_UNSND (attr_high);
9363 }
af34e669
DJ
9364 else
9365 /* Found high w/o low attribute. */
9366 return 0;
9367
9368 /* Found consecutive range of addresses. */
9369 ret = 1;
9370 }
c906108c 9371 else
af34e669 9372 {
e142c38c 9373 attr = dwarf2_attr (die, DW_AT_ranges, cu);
af34e669
DJ
9374 if (attr != NULL)
9375 {
2e3cf129
DE
9376 unsigned int ranges_offset = DW_UNSND (attr) + cu->ranges_base;
9377
af34e669 9378 /* Value of the DW_AT_ranges attribute is the offset in the
a604369a 9379 .debug_ranges section. */
2e3cf129 9380 if (!dwarf2_ranges_read (ranges_offset, &low, &high, cu, pst))
af34e669 9381 return 0;
43039443 9382 /* Found discontinuous range of addresses. */
af34e669
DJ
9383 ret = -1;
9384 }
9385 }
c906108c 9386
9373cf26
JK
9387 /* read_partial_die has also the strict LOW < HIGH requirement. */
9388 if (high <= low)
c906108c
SS
9389 return 0;
9390
9391 /* When using the GNU linker, .gnu.linkonce. sections are used to
9392 eliminate duplicate copies of functions and vtables and such.
9393 The linker will arbitrarily choose one and discard the others.
9394 The AT_*_pc values for such functions refer to local labels in
9395 these sections. If the section from that file was discarded, the
9396 labels are not in the output, so the relocs get a value of 0.
9397 If this is a discarded function, mark the pc bounds as invalid,
9398 so that GDB will ignore it. */
72dca2f5 9399 if (low == 0 && !dwarf2_per_objfile->has_section_at_zero)
c906108c
SS
9400 return 0;
9401
9402 *lowpc = low;
96408a79
SA
9403 if (highpc)
9404 *highpc = high;
af34e669 9405 return ret;
c906108c
SS
9406}
9407
b084d499
JB
9408/* Assuming that DIE represents a subprogram DIE or a lexical block, get
9409 its low and high PC addresses. Do nothing if these addresses could not
9410 be determined. Otherwise, set LOWPC to the low address if it is smaller,
9411 and HIGHPC to the high address if greater than HIGHPC. */
9412
9413static void
9414dwarf2_get_subprogram_pc_bounds (struct die_info *die,
9415 CORE_ADDR *lowpc, CORE_ADDR *highpc,
9416 struct dwarf2_cu *cu)
9417{
9418 CORE_ADDR low, high;
9419 struct die_info *child = die->child;
9420
d85a05f0 9421 if (dwarf2_get_pc_bounds (die, &low, &high, cu, NULL))
b084d499
JB
9422 {
9423 *lowpc = min (*lowpc, low);
9424 *highpc = max (*highpc, high);
9425 }
9426
9427 /* If the language does not allow nested subprograms (either inside
9428 subprograms or lexical blocks), we're done. */
9429 if (cu->language != language_ada)
9430 return;
6e70227d 9431
b084d499
JB
9432 /* Check all the children of the given DIE. If it contains nested
9433 subprograms, then check their pc bounds. Likewise, we need to
9434 check lexical blocks as well, as they may also contain subprogram
9435 definitions. */
9436 while (child && child->tag)
9437 {
9438 if (child->tag == DW_TAG_subprogram
9439 || child->tag == DW_TAG_lexical_block)
9440 dwarf2_get_subprogram_pc_bounds (child, lowpc, highpc, cu);
9441 child = sibling_die (child);
9442 }
9443}
9444
fae299cd
DC
9445/* Get the low and high pc's represented by the scope DIE, and store
9446 them in *LOWPC and *HIGHPC. If the correct values can't be
9447 determined, set *LOWPC to -1 and *HIGHPC to 0. */
9448
9449static void
9450get_scope_pc_bounds (struct die_info *die,
9451 CORE_ADDR *lowpc, CORE_ADDR *highpc,
9452 struct dwarf2_cu *cu)
9453{
9454 CORE_ADDR best_low = (CORE_ADDR) -1;
9455 CORE_ADDR best_high = (CORE_ADDR) 0;
9456 CORE_ADDR current_low, current_high;
9457
d85a05f0 9458 if (dwarf2_get_pc_bounds (die, &current_low, &current_high, cu, NULL))
fae299cd
DC
9459 {
9460 best_low = current_low;
9461 best_high = current_high;
9462 }
9463 else
9464 {
9465 struct die_info *child = die->child;
9466
9467 while (child && child->tag)
9468 {
9469 switch (child->tag) {
9470 case DW_TAG_subprogram:
b084d499 9471 dwarf2_get_subprogram_pc_bounds (child, &best_low, &best_high, cu);
fae299cd
DC
9472 break;
9473 case DW_TAG_namespace:
f55ee35c 9474 case DW_TAG_module:
fae299cd
DC
9475 /* FIXME: carlton/2004-01-16: Should we do this for
9476 DW_TAG_class_type/DW_TAG_structure_type, too? I think
9477 that current GCC's always emit the DIEs corresponding
9478 to definitions of methods of classes as children of a
9479 DW_TAG_compile_unit or DW_TAG_namespace (as opposed to
9480 the DIEs giving the declarations, which could be
9481 anywhere). But I don't see any reason why the
9482 standards says that they have to be there. */
9483 get_scope_pc_bounds (child, &current_low, &current_high, cu);
9484
9485 if (current_low != ((CORE_ADDR) -1))
9486 {
9487 best_low = min (best_low, current_low);
9488 best_high = max (best_high, current_high);
9489 }
9490 break;
9491 default:
0963b4bd 9492 /* Ignore. */
fae299cd
DC
9493 break;
9494 }
9495
9496 child = sibling_die (child);
9497 }
9498 }
9499
9500 *lowpc = best_low;
9501 *highpc = best_high;
9502}
9503
801e3a5b
JB
9504/* Record the address ranges for BLOCK, offset by BASEADDR, as given
9505 in DIE. */
380bca97 9506
801e3a5b
JB
9507static void
9508dwarf2_record_block_ranges (struct die_info *die, struct block *block,
9509 CORE_ADDR baseaddr, struct dwarf2_cu *cu)
9510{
bb5ed363 9511 struct objfile *objfile = cu->objfile;
801e3a5b 9512 struct attribute *attr;
91da1414 9513 struct attribute *attr_high;
801e3a5b 9514
91da1414
MW
9515 attr_high = dwarf2_attr (die, DW_AT_high_pc, cu);
9516 if (attr_high)
801e3a5b 9517 {
801e3a5b
JB
9518 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
9519 if (attr)
9520 {
9521 CORE_ADDR low = DW_ADDR (attr);
91da1414 9522 CORE_ADDR high;
3019eac3
DE
9523 if (attr_high->form == DW_FORM_addr
9524 || attr_high->form == DW_FORM_GNU_addr_index)
91da1414
MW
9525 high = DW_ADDR (attr_high);
9526 else
9527 high = low + DW_UNSND (attr_high);
9a619af0 9528
801e3a5b
JB
9529 record_block_range (block, baseaddr + low, baseaddr + high - 1);
9530 }
9531 }
9532
9533 attr = dwarf2_attr (die, DW_AT_ranges, cu);
9534 if (attr)
9535 {
bb5ed363 9536 bfd *obfd = objfile->obfd;
801e3a5b
JB
9537
9538 /* The value of the DW_AT_ranges attribute is the offset of the
9539 address range list in the .debug_ranges section. */
2e3cf129 9540 unsigned long offset = DW_UNSND (attr) + cu->ranges_base;
dce234bc 9541 gdb_byte *buffer = dwarf2_per_objfile->ranges.buffer + offset;
801e3a5b
JB
9542
9543 /* For some target architectures, but not others, the
9544 read_address function sign-extends the addresses it returns.
9545 To recognize base address selection entries, we need a
9546 mask. */
9547 unsigned int addr_size = cu->header.addr_size;
9548 CORE_ADDR base_select_mask = ~(~(CORE_ADDR)1 << (addr_size * 8 - 1));
9549
9550 /* The base address, to which the next pair is relative. Note
9551 that this 'base' is a DWARF concept: most entries in a range
9552 list are relative, to reduce the number of relocs against the
9553 debugging information. This is separate from this function's
9554 'baseaddr' argument, which GDB uses to relocate debugging
9555 information from a shared library based on the address at
9556 which the library was loaded. */
d00adf39
DE
9557 CORE_ADDR base = cu->base_address;
9558 int base_known = cu->base_known;
801e3a5b 9559
be391dca 9560 gdb_assert (dwarf2_per_objfile->ranges.readin);
dce234bc 9561 if (offset >= dwarf2_per_objfile->ranges.size)
801e3a5b
JB
9562 {
9563 complaint (&symfile_complaints,
9564 _("Offset %lu out of bounds for DW_AT_ranges attribute"),
9565 offset);
9566 return;
9567 }
9568
9569 for (;;)
9570 {
9571 unsigned int bytes_read;
9572 CORE_ADDR start, end;
9573
9574 start = read_address (obfd, buffer, cu, &bytes_read);
9575 buffer += bytes_read;
9576 end = read_address (obfd, buffer, cu, &bytes_read);
9577 buffer += bytes_read;
9578
9579 /* Did we find the end of the range list? */
9580 if (start == 0 && end == 0)
9581 break;
9582
9583 /* Did we find a base address selection entry? */
9584 else if ((start & base_select_mask) == base_select_mask)
9585 {
9586 base = end;
9587 base_known = 1;
9588 }
9589
9590 /* We found an ordinary address range. */
9591 else
9592 {
9593 if (!base_known)
9594 {
9595 complaint (&symfile_complaints,
3e43a32a
MS
9596 _("Invalid .debug_ranges data "
9597 "(no base address)"));
801e3a5b
JB
9598 return;
9599 }
9600
9277c30c
UW
9601 if (start > end)
9602 {
9603 /* Inverted range entries are invalid. */
9604 complaint (&symfile_complaints,
9605 _("Invalid .debug_ranges data "
9606 "(inverted range)"));
9607 return;
9608 }
9609
9610 /* Empty range entries have no effect. */
9611 if (start == end)
9612 continue;
9613
01093045
DE
9614 start += base + baseaddr;
9615 end += base + baseaddr;
9616
9617 /* A not-uncommon case of bad debug info.
9618 Don't pollute the addrmap with bad data. */
9619 if (start == 0 && !dwarf2_per_objfile->has_section_at_zero)
9620 {
9621 complaint (&symfile_complaints,
9622 _(".debug_ranges entry has start address of zero"
9623 " [in module %s]"), objfile->name);
9624 continue;
9625 }
9626
9627 record_block_range (block, start, end - 1);
801e3a5b
JB
9628 }
9629 }
9630 }
9631}
9632
685b1105
JK
9633/* Check whether the producer field indicates either of GCC < 4.6, or the
9634 Intel C/C++ compiler, and cache the result in CU. */
60d5a603 9635
685b1105
JK
9636static void
9637check_producer (struct dwarf2_cu *cu)
60d5a603
JK
9638{
9639 const char *cs;
9640 int major, minor, release;
9641
9642 if (cu->producer == NULL)
9643 {
9644 /* For unknown compilers expect their behavior is DWARF version
9645 compliant.
9646
9647 GCC started to support .debug_types sections by -gdwarf-4 since
9648 gcc-4.5.x. As the .debug_types sections are missing DW_AT_producer
9649 for their space efficiency GDB cannot workaround gcc-4.5.x -gdwarf-4
9650 combination. gcc-4.5.x -gdwarf-4 binaries have DW_AT_accessibility
9651 interpreted incorrectly by GDB now - GCC PR debug/48229. */
60d5a603 9652 }
685b1105 9653 else if (strncmp (cu->producer, "GNU ", strlen ("GNU ")) == 0)
60d5a603 9654 {
685b1105
JK
9655 /* Skip any identifier after "GNU " - such as "C++" or "Java". */
9656
ba919b58
TT
9657 cs = &cu->producer[strlen ("GNU ")];
9658 while (*cs && !isdigit (*cs))
9659 cs++;
9660 if (sscanf (cs, "%d.%d.%d", &major, &minor, &release) != 3)
9661 {
9662 /* Not recognized as GCC. */
9663 }
9664 else
685b1105
JK
9665 cu->producer_is_gxx_lt_4_6 = major < 4 || (major == 4 && minor < 6);
9666 }
9667 else if (strncmp (cu->producer, "Intel(R) C", strlen ("Intel(R) C")) == 0)
9668 cu->producer_is_icc = 1;
9669 else
9670 {
9671 /* For other non-GCC compilers, expect their behavior is DWARF version
9672 compliant. */
60d5a603
JK
9673 }
9674
ba919b58 9675 cu->checked_producer = 1;
685b1105 9676}
ba919b58 9677
685b1105
JK
9678/* Check for GCC PR debug/45124 fix which is not present in any G++ version up
9679 to 4.5.any while it is present already in G++ 4.6.0 - the PR has been fixed
9680 during 4.6.0 experimental. */
9681
9682static int
9683producer_is_gxx_lt_4_6 (struct dwarf2_cu *cu)
9684{
9685 if (!cu->checked_producer)
9686 check_producer (cu);
9687
9688 return cu->producer_is_gxx_lt_4_6;
60d5a603
JK
9689}
9690
9691/* Return the default accessibility type if it is not overriden by
9692 DW_AT_accessibility. */
9693
9694static enum dwarf_access_attribute
9695dwarf2_default_access_attribute (struct die_info *die, struct dwarf2_cu *cu)
9696{
9697 if (cu->header.version < 3 || producer_is_gxx_lt_4_6 (cu))
9698 {
9699 /* The default DWARF 2 accessibility for members is public, the default
9700 accessibility for inheritance is private. */
9701
9702 if (die->tag != DW_TAG_inheritance)
9703 return DW_ACCESS_public;
9704 else
9705 return DW_ACCESS_private;
9706 }
9707 else
9708 {
9709 /* DWARF 3+ defines the default accessibility a different way. The same
9710 rules apply now for DW_TAG_inheritance as for the members and it only
9711 depends on the container kind. */
9712
9713 if (die->parent->tag == DW_TAG_class_type)
9714 return DW_ACCESS_private;
9715 else
9716 return DW_ACCESS_public;
9717 }
9718}
9719
74ac6d43
TT
9720/* Look for DW_AT_data_member_location. Set *OFFSET to the byte
9721 offset. If the attribute was not found return 0, otherwise return
9722 1. If it was found but could not properly be handled, set *OFFSET
9723 to 0. */
9724
9725static int
9726handle_data_member_location (struct die_info *die, struct dwarf2_cu *cu,
9727 LONGEST *offset)
9728{
9729 struct attribute *attr;
9730
9731 attr = dwarf2_attr (die, DW_AT_data_member_location, cu);
9732 if (attr != NULL)
9733 {
9734 *offset = 0;
9735
9736 /* Note that we do not check for a section offset first here.
9737 This is because DW_AT_data_member_location is new in DWARF 4,
9738 so if we see it, we can assume that a constant form is really
9739 a constant and not a section offset. */
9740 if (attr_form_is_constant (attr))
9741 *offset = dwarf2_get_attr_constant_value (attr, 0);
9742 else if (attr_form_is_section_offset (attr))
9743 dwarf2_complex_location_expr_complaint ();
9744 else if (attr_form_is_block (attr))
9745 *offset = decode_locdesc (DW_BLOCK (attr), cu);
9746 else
9747 dwarf2_complex_location_expr_complaint ();
9748
9749 return 1;
9750 }
9751
9752 return 0;
9753}
9754
c906108c
SS
9755/* Add an aggregate field to the field list. */
9756
9757static void
107d2387 9758dwarf2_add_field (struct field_info *fip, struct die_info *die,
e7c27a73 9759 struct dwarf2_cu *cu)
6e70227d 9760{
e7c27a73 9761 struct objfile *objfile = cu->objfile;
5e2b427d 9762 struct gdbarch *gdbarch = get_objfile_arch (objfile);
c906108c
SS
9763 struct nextfield *new_field;
9764 struct attribute *attr;
9765 struct field *fp;
9766 char *fieldname = "";
9767
9768 /* Allocate a new field list entry and link it in. */
9769 new_field = (struct nextfield *) xmalloc (sizeof (struct nextfield));
b8c9b27d 9770 make_cleanup (xfree, new_field);
c906108c 9771 memset (new_field, 0, sizeof (struct nextfield));
7d0ccb61
DJ
9772
9773 if (die->tag == DW_TAG_inheritance)
9774 {
9775 new_field->next = fip->baseclasses;
9776 fip->baseclasses = new_field;
9777 }
9778 else
9779 {
9780 new_field->next = fip->fields;
9781 fip->fields = new_field;
9782 }
c906108c
SS
9783 fip->nfields++;
9784
e142c38c 9785 attr = dwarf2_attr (die, DW_AT_accessibility, cu);
c906108c
SS
9786 if (attr)
9787 new_field->accessibility = DW_UNSND (attr);
60d5a603
JK
9788 else
9789 new_field->accessibility = dwarf2_default_access_attribute (die, cu);
c906108c
SS
9790 if (new_field->accessibility != DW_ACCESS_public)
9791 fip->non_public_fields = 1;
60d5a603 9792
e142c38c 9793 attr = dwarf2_attr (die, DW_AT_virtuality, cu);
c906108c
SS
9794 if (attr)
9795 new_field->virtuality = DW_UNSND (attr);
60d5a603
JK
9796 else
9797 new_field->virtuality = DW_VIRTUALITY_none;
c906108c
SS
9798
9799 fp = &new_field->field;
a9a9bd0f 9800
e142c38c 9801 if (die->tag == DW_TAG_member && ! die_is_declaration (die, cu))
c906108c 9802 {
74ac6d43
TT
9803 LONGEST offset;
9804
a9a9bd0f 9805 /* Data member other than a C++ static data member. */
6e70227d 9806
c906108c 9807 /* Get type of field. */
e7c27a73 9808 fp->type = die_type (die, cu);
c906108c 9809
d6a843b5 9810 SET_FIELD_BITPOS (*fp, 0);
01ad7f36 9811
c906108c 9812 /* Get bit size of field (zero if none). */
e142c38c 9813 attr = dwarf2_attr (die, DW_AT_bit_size, cu);
c906108c
SS
9814 if (attr)
9815 {
9816 FIELD_BITSIZE (*fp) = DW_UNSND (attr);
9817 }
9818 else
9819 {
9820 FIELD_BITSIZE (*fp) = 0;
9821 }
9822
9823 /* Get bit offset of field. */
74ac6d43
TT
9824 if (handle_data_member_location (die, cu, &offset))
9825 SET_FIELD_BITPOS (*fp, offset * bits_per_byte);
e142c38c 9826 attr = dwarf2_attr (die, DW_AT_bit_offset, cu);
c906108c
SS
9827 if (attr)
9828 {
5e2b427d 9829 if (gdbarch_bits_big_endian (gdbarch))
c906108c
SS
9830 {
9831 /* For big endian bits, the DW_AT_bit_offset gives the
c5aa993b
JM
9832 additional bit offset from the MSB of the containing
9833 anonymous object to the MSB of the field. We don't
9834 have to do anything special since we don't need to
9835 know the size of the anonymous object. */
f41f5e61 9836 SET_FIELD_BITPOS (*fp, FIELD_BITPOS (*fp) + DW_UNSND (attr));
c906108c
SS
9837 }
9838 else
9839 {
9840 /* For little endian bits, compute the bit offset to the
c5aa993b
JM
9841 MSB of the anonymous object, subtract off the number of
9842 bits from the MSB of the field to the MSB of the
9843 object, and then subtract off the number of bits of
9844 the field itself. The result is the bit offset of
9845 the LSB of the field. */
c906108c
SS
9846 int anonymous_size;
9847 int bit_offset = DW_UNSND (attr);
9848
e142c38c 9849 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
c906108c
SS
9850 if (attr)
9851 {
9852 /* The size of the anonymous object containing
9853 the bit field is explicit, so use the
9854 indicated size (in bytes). */
9855 anonymous_size = DW_UNSND (attr);
9856 }
9857 else
9858 {
9859 /* The size of the anonymous object containing
9860 the bit field must be inferred from the type
9861 attribute of the data member containing the
9862 bit field. */
9863 anonymous_size = TYPE_LENGTH (fp->type);
9864 }
f41f5e61
PA
9865 SET_FIELD_BITPOS (*fp,
9866 (FIELD_BITPOS (*fp)
9867 + anonymous_size * bits_per_byte
9868 - bit_offset - FIELD_BITSIZE (*fp)));
c906108c
SS
9869 }
9870 }
9871
9872 /* Get name of field. */
39cbfefa
DJ
9873 fieldname = dwarf2_name (die, cu);
9874 if (fieldname == NULL)
9875 fieldname = "";
d8151005
DJ
9876
9877 /* The name is already allocated along with this objfile, so we don't
9878 need to duplicate it for the type. */
9879 fp->name = fieldname;
c906108c
SS
9880
9881 /* Change accessibility for artificial fields (e.g. virtual table
c5aa993b 9882 pointer or virtual base class pointer) to private. */
e142c38c 9883 if (dwarf2_attr (die, DW_AT_artificial, cu))
c906108c 9884 {
d48cc9dd 9885 FIELD_ARTIFICIAL (*fp) = 1;
c906108c
SS
9886 new_field->accessibility = DW_ACCESS_private;
9887 fip->non_public_fields = 1;
9888 }
9889 }
a9a9bd0f 9890 else if (die->tag == DW_TAG_member || die->tag == DW_TAG_variable)
c906108c 9891 {
a9a9bd0f
DC
9892 /* C++ static member. */
9893
9894 /* NOTE: carlton/2002-11-05: It should be a DW_TAG_member that
9895 is a declaration, but all versions of G++ as of this writing
9896 (so through at least 3.2.1) incorrectly generate
9897 DW_TAG_variable tags. */
6e70227d 9898
ff355380 9899 const char *physname;
c906108c 9900
a9a9bd0f 9901 /* Get name of field. */
39cbfefa
DJ
9902 fieldname = dwarf2_name (die, cu);
9903 if (fieldname == NULL)
c906108c
SS
9904 return;
9905
254e6b9e 9906 attr = dwarf2_attr (die, DW_AT_const_value, cu);
3863f96c
DE
9907 if (attr
9908 /* Only create a symbol if this is an external value.
9909 new_symbol checks this and puts the value in the global symbol
9910 table, which we want. If it is not external, new_symbol
9911 will try to put the value in cu->list_in_scope which is wrong. */
9912 && dwarf2_flag_true_p (die, DW_AT_external, cu))
254e6b9e
DE
9913 {
9914 /* A static const member, not much different than an enum as far as
9915 we're concerned, except that we can support more types. */
9916 new_symbol (die, NULL, cu);
9917 }
9918
2df3850c 9919 /* Get physical name. */
ff355380 9920 physname = dwarf2_physname (fieldname, die, cu);
c906108c 9921
d8151005
DJ
9922 /* The name is already allocated along with this objfile, so we don't
9923 need to duplicate it for the type. */
9924 SET_FIELD_PHYSNAME (*fp, physname ? physname : "");
e7c27a73 9925 FIELD_TYPE (*fp) = die_type (die, cu);
d8151005 9926 FIELD_NAME (*fp) = fieldname;
c906108c
SS
9927 }
9928 else if (die->tag == DW_TAG_inheritance)
9929 {
74ac6d43 9930 LONGEST offset;
d4b96c9a 9931
74ac6d43
TT
9932 /* C++ base class field. */
9933 if (handle_data_member_location (die, cu, &offset))
9934 SET_FIELD_BITPOS (*fp, offset * bits_per_byte);
c906108c 9935 FIELD_BITSIZE (*fp) = 0;
e7c27a73 9936 FIELD_TYPE (*fp) = die_type (die, cu);
c906108c
SS
9937 FIELD_NAME (*fp) = type_name_no_tag (fp->type);
9938 fip->nbaseclasses++;
9939 }
9940}
9941
98751a41
JK
9942/* Add a typedef defined in the scope of the FIP's class. */
9943
9944static void
9945dwarf2_add_typedef (struct field_info *fip, struct die_info *die,
9946 struct dwarf2_cu *cu)
6e70227d 9947{
98751a41 9948 struct objfile *objfile = cu->objfile;
98751a41
JK
9949 struct typedef_field_list *new_field;
9950 struct attribute *attr;
9951 struct typedef_field *fp;
9952 char *fieldname = "";
9953
9954 /* Allocate a new field list entry and link it in. */
9955 new_field = xzalloc (sizeof (*new_field));
9956 make_cleanup (xfree, new_field);
9957
9958 gdb_assert (die->tag == DW_TAG_typedef);
9959
9960 fp = &new_field->field;
9961
9962 /* Get name of field. */
9963 fp->name = dwarf2_name (die, cu);
9964 if (fp->name == NULL)
9965 return;
9966
9967 fp->type = read_type_die (die, cu);
9968
9969 new_field->next = fip->typedef_field_list;
9970 fip->typedef_field_list = new_field;
9971 fip->typedef_field_list_count++;
9972}
9973
c906108c
SS
9974/* Create the vector of fields, and attach it to the type. */
9975
9976static void
fba45db2 9977dwarf2_attach_fields_to_type (struct field_info *fip, struct type *type,
e7c27a73 9978 struct dwarf2_cu *cu)
c906108c
SS
9979{
9980 int nfields = fip->nfields;
9981
9982 /* Record the field count, allocate space for the array of fields,
9983 and create blank accessibility bitfields if necessary. */
9984 TYPE_NFIELDS (type) = nfields;
9985 TYPE_FIELDS (type) = (struct field *)
9986 TYPE_ALLOC (type, sizeof (struct field) * nfields);
9987 memset (TYPE_FIELDS (type), 0, sizeof (struct field) * nfields);
9988
b4ba55a1 9989 if (fip->non_public_fields && cu->language != language_ada)
c906108c
SS
9990 {
9991 ALLOCATE_CPLUS_STRUCT_TYPE (type);
9992
9993 TYPE_FIELD_PRIVATE_BITS (type) =
9994 (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
9995 B_CLRALL (TYPE_FIELD_PRIVATE_BITS (type), nfields);
9996
9997 TYPE_FIELD_PROTECTED_BITS (type) =
9998 (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
9999 B_CLRALL (TYPE_FIELD_PROTECTED_BITS (type), nfields);
10000
774b6a14
TT
10001 TYPE_FIELD_IGNORE_BITS (type) =
10002 (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
10003 B_CLRALL (TYPE_FIELD_IGNORE_BITS (type), nfields);
c906108c
SS
10004 }
10005
10006 /* If the type has baseclasses, allocate and clear a bit vector for
10007 TYPE_FIELD_VIRTUAL_BITS. */
b4ba55a1 10008 if (fip->nbaseclasses && cu->language != language_ada)
c906108c
SS
10009 {
10010 int num_bytes = B_BYTES (fip->nbaseclasses);
fe1b8b76 10011 unsigned char *pointer;
c906108c
SS
10012
10013 ALLOCATE_CPLUS_STRUCT_TYPE (type);
fe1b8b76
JB
10014 pointer = TYPE_ALLOC (type, num_bytes);
10015 TYPE_FIELD_VIRTUAL_BITS (type) = pointer;
c906108c
SS
10016 B_CLRALL (TYPE_FIELD_VIRTUAL_BITS (type), fip->nbaseclasses);
10017 TYPE_N_BASECLASSES (type) = fip->nbaseclasses;
10018 }
10019
3e43a32a
MS
10020 /* Copy the saved-up fields into the field vector. Start from the head of
10021 the list, adding to the tail of the field array, so that they end up in
10022 the same order in the array in which they were added to the list. */
c906108c
SS
10023 while (nfields-- > 0)
10024 {
7d0ccb61
DJ
10025 struct nextfield *fieldp;
10026
10027 if (fip->fields)
10028 {
10029 fieldp = fip->fields;
10030 fip->fields = fieldp->next;
10031 }
10032 else
10033 {
10034 fieldp = fip->baseclasses;
10035 fip->baseclasses = fieldp->next;
10036 }
10037
10038 TYPE_FIELD (type, nfields) = fieldp->field;
10039 switch (fieldp->accessibility)
c906108c 10040 {
c5aa993b 10041 case DW_ACCESS_private:
b4ba55a1
JB
10042 if (cu->language != language_ada)
10043 SET_TYPE_FIELD_PRIVATE (type, nfields);
c5aa993b 10044 break;
c906108c 10045
c5aa993b 10046 case DW_ACCESS_protected:
b4ba55a1
JB
10047 if (cu->language != language_ada)
10048 SET_TYPE_FIELD_PROTECTED (type, nfields);
c5aa993b 10049 break;
c906108c 10050
c5aa993b
JM
10051 case DW_ACCESS_public:
10052 break;
c906108c 10053
c5aa993b
JM
10054 default:
10055 /* Unknown accessibility. Complain and treat it as public. */
10056 {
e2e0b3e5 10057 complaint (&symfile_complaints, _("unsupported accessibility %d"),
7d0ccb61 10058 fieldp->accessibility);
c5aa993b
JM
10059 }
10060 break;
c906108c
SS
10061 }
10062 if (nfields < fip->nbaseclasses)
10063 {
7d0ccb61 10064 switch (fieldp->virtuality)
c906108c 10065 {
c5aa993b
JM
10066 case DW_VIRTUALITY_virtual:
10067 case DW_VIRTUALITY_pure_virtual:
b4ba55a1 10068 if (cu->language == language_ada)
a73c6dcd 10069 error (_("unexpected virtuality in component of Ada type"));
c5aa993b
JM
10070 SET_TYPE_FIELD_VIRTUAL (type, nfields);
10071 break;
c906108c
SS
10072 }
10073 }
c906108c
SS
10074 }
10075}
10076
c906108c
SS
10077/* Add a member function to the proper fieldlist. */
10078
10079static void
107d2387 10080dwarf2_add_member_fn (struct field_info *fip, struct die_info *die,
e7c27a73 10081 struct type *type, struct dwarf2_cu *cu)
c906108c 10082{
e7c27a73 10083 struct objfile *objfile = cu->objfile;
c906108c
SS
10084 struct attribute *attr;
10085 struct fnfieldlist *flp;
10086 int i;
10087 struct fn_field *fnp;
10088 char *fieldname;
c906108c 10089 struct nextfnfield *new_fnfield;
f792889a 10090 struct type *this_type;
60d5a603 10091 enum dwarf_access_attribute accessibility;
c906108c 10092
b4ba55a1 10093 if (cu->language == language_ada)
a73c6dcd 10094 error (_("unexpected member function in Ada type"));
b4ba55a1 10095
2df3850c 10096 /* Get name of member function. */
39cbfefa
DJ
10097 fieldname = dwarf2_name (die, cu);
10098 if (fieldname == NULL)
2df3850c 10099 return;
c906108c 10100
c906108c
SS
10101 /* Look up member function name in fieldlist. */
10102 for (i = 0; i < fip->nfnfields; i++)
10103 {
27bfe10e 10104 if (strcmp (fip->fnfieldlists[i].name, fieldname) == 0)
c906108c
SS
10105 break;
10106 }
10107
10108 /* Create new list element if necessary. */
10109 if (i < fip->nfnfields)
10110 flp = &fip->fnfieldlists[i];
10111 else
10112 {
10113 if ((fip->nfnfields % DW_FIELD_ALLOC_CHUNK) == 0)
10114 {
10115 fip->fnfieldlists = (struct fnfieldlist *)
10116 xrealloc (fip->fnfieldlists,
10117 (fip->nfnfields + DW_FIELD_ALLOC_CHUNK)
c5aa993b 10118 * sizeof (struct fnfieldlist));
c906108c 10119 if (fip->nfnfields == 0)
c13c43fd 10120 make_cleanup (free_current_contents, &fip->fnfieldlists);
c906108c
SS
10121 }
10122 flp = &fip->fnfieldlists[fip->nfnfields];
10123 flp->name = fieldname;
10124 flp->length = 0;
10125 flp->head = NULL;
3da10d80 10126 i = fip->nfnfields++;
c906108c
SS
10127 }
10128
10129 /* Create a new member function field and chain it to the field list
0963b4bd 10130 entry. */
c906108c 10131 new_fnfield = (struct nextfnfield *) xmalloc (sizeof (struct nextfnfield));
b8c9b27d 10132 make_cleanup (xfree, new_fnfield);
c906108c
SS
10133 memset (new_fnfield, 0, sizeof (struct nextfnfield));
10134 new_fnfield->next = flp->head;
10135 flp->head = new_fnfield;
10136 flp->length++;
10137
10138 /* Fill in the member function field info. */
10139 fnp = &new_fnfield->fnfield;
3da10d80
KS
10140
10141 /* Delay processing of the physname until later. */
10142 if (cu->language == language_cplus || cu->language == language_java)
10143 {
10144 add_to_method_list (type, i, flp->length - 1, fieldname,
10145 die, cu);
10146 }
10147 else
10148 {
1d06ead6 10149 const char *physname = dwarf2_physname (fieldname, die, cu);
3da10d80
KS
10150 fnp->physname = physname ? physname : "";
10151 }
10152
c906108c 10153 fnp->type = alloc_type (objfile);
f792889a
DJ
10154 this_type = read_type_die (die, cu);
10155 if (this_type && TYPE_CODE (this_type) == TYPE_CODE_FUNC)
c906108c 10156 {
f792889a 10157 int nparams = TYPE_NFIELDS (this_type);
c906108c 10158
f792889a 10159 /* TYPE is the domain of this method, and THIS_TYPE is the type
e26fb1d7
DC
10160 of the method itself (TYPE_CODE_METHOD). */
10161 smash_to_method_type (fnp->type, type,
f792889a
DJ
10162 TYPE_TARGET_TYPE (this_type),
10163 TYPE_FIELDS (this_type),
10164 TYPE_NFIELDS (this_type),
10165 TYPE_VARARGS (this_type));
c906108c
SS
10166
10167 /* Handle static member functions.
c5aa993b 10168 Dwarf2 has no clean way to discern C++ static and non-static
0963b4bd
MS
10169 member functions. G++ helps GDB by marking the first
10170 parameter for non-static member functions (which is the this
10171 pointer) as artificial. We obtain this information from
10172 read_subroutine_type via TYPE_FIELD_ARTIFICIAL. */
f792889a 10173 if (nparams == 0 || TYPE_FIELD_ARTIFICIAL (this_type, 0) == 0)
c906108c
SS
10174 fnp->voffset = VOFFSET_STATIC;
10175 }
10176 else
e2e0b3e5 10177 complaint (&symfile_complaints, _("member function type missing for '%s'"),
3da10d80 10178 dwarf2_full_name (fieldname, die, cu));
c906108c
SS
10179
10180 /* Get fcontext from DW_AT_containing_type if present. */
e142c38c 10181 if (dwarf2_attr (die, DW_AT_containing_type, cu) != NULL)
e7c27a73 10182 fnp->fcontext = die_containing_type (die, cu);
c906108c 10183
3e43a32a
MS
10184 /* dwarf2 doesn't have stubbed physical names, so the setting of is_const and
10185 is_volatile is irrelevant, as it is needed by gdb_mangle_name only. */
c906108c
SS
10186
10187 /* Get accessibility. */
e142c38c 10188 attr = dwarf2_attr (die, DW_AT_accessibility, cu);
c906108c 10189 if (attr)
60d5a603
JK
10190 accessibility = DW_UNSND (attr);
10191 else
10192 accessibility = dwarf2_default_access_attribute (die, cu);
10193 switch (accessibility)
c906108c 10194 {
60d5a603
JK
10195 case DW_ACCESS_private:
10196 fnp->is_private = 1;
10197 break;
10198 case DW_ACCESS_protected:
10199 fnp->is_protected = 1;
10200 break;
c906108c
SS
10201 }
10202
b02dede2 10203 /* Check for artificial methods. */
e142c38c 10204 attr = dwarf2_attr (die, DW_AT_artificial, cu);
b02dede2
DJ
10205 if (attr && DW_UNSND (attr) != 0)
10206 fnp->is_artificial = 1;
10207
0d564a31 10208 /* Get index in virtual function table if it is a virtual member
aec5aa8b
TT
10209 function. For older versions of GCC, this is an offset in the
10210 appropriate virtual table, as specified by DW_AT_containing_type.
10211 For everyone else, it is an expression to be evaluated relative
0d564a31
DJ
10212 to the object address. */
10213
e142c38c 10214 attr = dwarf2_attr (die, DW_AT_vtable_elem_location, cu);
aec5aa8b 10215 if (attr)
8e19ed76 10216 {
aec5aa8b 10217 if (attr_form_is_block (attr) && DW_BLOCK (attr)->size > 0)
8e19ed76 10218 {
aec5aa8b
TT
10219 if (DW_BLOCK (attr)->data[0] == DW_OP_constu)
10220 {
10221 /* Old-style GCC. */
10222 fnp->voffset = decode_locdesc (DW_BLOCK (attr), cu) + 2;
10223 }
10224 else if (DW_BLOCK (attr)->data[0] == DW_OP_deref
10225 || (DW_BLOCK (attr)->size > 1
10226 && DW_BLOCK (attr)->data[0] == DW_OP_deref_size
10227 && DW_BLOCK (attr)->data[1] == cu->header.addr_size))
10228 {
10229 struct dwarf_block blk;
10230 int offset;
10231
10232 offset = (DW_BLOCK (attr)->data[0] == DW_OP_deref
10233 ? 1 : 2);
10234 blk.size = DW_BLOCK (attr)->size - offset;
10235 blk.data = DW_BLOCK (attr)->data + offset;
10236 fnp->voffset = decode_locdesc (DW_BLOCK (attr), cu);
10237 if ((fnp->voffset % cu->header.addr_size) != 0)
10238 dwarf2_complex_location_expr_complaint ();
10239 else
10240 fnp->voffset /= cu->header.addr_size;
10241 fnp->voffset += 2;
10242 }
10243 else
10244 dwarf2_complex_location_expr_complaint ();
10245
10246 if (!fnp->fcontext)
10247 fnp->fcontext = TYPE_TARGET_TYPE (TYPE_FIELD_TYPE (this_type, 0));
10248 }
3690dd37 10249 else if (attr_form_is_section_offset (attr))
8e19ed76 10250 {
4d3c2250 10251 dwarf2_complex_location_expr_complaint ();
8e19ed76
PS
10252 }
10253 else
10254 {
4d3c2250
KB
10255 dwarf2_invalid_attrib_class_complaint ("DW_AT_vtable_elem_location",
10256 fieldname);
8e19ed76 10257 }
0d564a31 10258 }
d48cc9dd
DJ
10259 else
10260 {
10261 attr = dwarf2_attr (die, DW_AT_virtuality, cu);
10262 if (attr && DW_UNSND (attr))
10263 {
10264 /* GCC does this, as of 2008-08-25; PR debug/37237. */
10265 complaint (&symfile_complaints,
3e43a32a
MS
10266 _("Member function \"%s\" (offset %d) is virtual "
10267 "but the vtable offset is not specified"),
b64f50a1 10268 fieldname, die->offset.sect_off);
9655fd1a 10269 ALLOCATE_CPLUS_STRUCT_TYPE (type);
d48cc9dd
DJ
10270 TYPE_CPLUS_DYNAMIC (type) = 1;
10271 }
10272 }
c906108c
SS
10273}
10274
10275/* Create the vector of member function fields, and attach it to the type. */
10276
10277static void
fba45db2 10278dwarf2_attach_fn_fields_to_type (struct field_info *fip, struct type *type,
e7c27a73 10279 struct dwarf2_cu *cu)
c906108c
SS
10280{
10281 struct fnfieldlist *flp;
c906108c
SS
10282 int i;
10283
b4ba55a1 10284 if (cu->language == language_ada)
a73c6dcd 10285 error (_("unexpected member functions in Ada type"));
b4ba55a1 10286
c906108c
SS
10287 ALLOCATE_CPLUS_STRUCT_TYPE (type);
10288 TYPE_FN_FIELDLISTS (type) = (struct fn_fieldlist *)
10289 TYPE_ALLOC (type, sizeof (struct fn_fieldlist) * fip->nfnfields);
10290
10291 for (i = 0, flp = fip->fnfieldlists; i < fip->nfnfields; i++, flp++)
10292 {
10293 struct nextfnfield *nfp = flp->head;
10294 struct fn_fieldlist *fn_flp = &TYPE_FN_FIELDLIST (type, i);
10295 int k;
10296
10297 TYPE_FN_FIELDLIST_NAME (type, i) = flp->name;
10298 TYPE_FN_FIELDLIST_LENGTH (type, i) = flp->length;
10299 fn_flp->fn_fields = (struct fn_field *)
10300 TYPE_ALLOC (type, sizeof (struct fn_field) * flp->length);
10301 for (k = flp->length; (k--, nfp); nfp = nfp->next)
c5aa993b 10302 fn_flp->fn_fields[k] = nfp->fnfield;
c906108c
SS
10303 }
10304
10305 TYPE_NFN_FIELDS (type) = fip->nfnfields;
c906108c
SS
10306}
10307
1168df01
JB
10308/* Returns non-zero if NAME is the name of a vtable member in CU's
10309 language, zero otherwise. */
10310static int
10311is_vtable_name (const char *name, struct dwarf2_cu *cu)
10312{
10313 static const char vptr[] = "_vptr";
987504bb 10314 static const char vtable[] = "vtable";
1168df01 10315
987504bb
JJ
10316 /* Look for the C++ and Java forms of the vtable. */
10317 if ((cu->language == language_java
10318 && strncmp (name, vtable, sizeof (vtable) - 1) == 0)
10319 || (strncmp (name, vptr, sizeof (vptr) - 1) == 0
10320 && is_cplus_marker (name[sizeof (vptr) - 1])))
1168df01
JB
10321 return 1;
10322
10323 return 0;
10324}
10325
c0dd20ea 10326/* GCC outputs unnamed structures that are really pointers to member
0b92b5bb
TT
10327 functions, with the ABI-specified layout. If TYPE describes
10328 such a structure, smash it into a member function type.
61049d3b
DJ
10329
10330 GCC shouldn't do this; it should just output pointer to member DIEs.
10331 This is GCC PR debug/28767. */
c0dd20ea 10332
0b92b5bb
TT
10333static void
10334quirk_gcc_member_function_pointer (struct type *type, struct objfile *objfile)
c0dd20ea 10335{
0b92b5bb 10336 struct type *pfn_type, *domain_type, *new_type;
c0dd20ea
DJ
10337
10338 /* Check for a structure with no name and two children. */
0b92b5bb
TT
10339 if (TYPE_CODE (type) != TYPE_CODE_STRUCT || TYPE_NFIELDS (type) != 2)
10340 return;
c0dd20ea
DJ
10341
10342 /* Check for __pfn and __delta members. */
0b92b5bb
TT
10343 if (TYPE_FIELD_NAME (type, 0) == NULL
10344 || strcmp (TYPE_FIELD_NAME (type, 0), "__pfn") != 0
10345 || TYPE_FIELD_NAME (type, 1) == NULL
10346 || strcmp (TYPE_FIELD_NAME (type, 1), "__delta") != 0)
10347 return;
c0dd20ea
DJ
10348
10349 /* Find the type of the method. */
0b92b5bb 10350 pfn_type = TYPE_FIELD_TYPE (type, 0);
c0dd20ea
DJ
10351 if (pfn_type == NULL
10352 || TYPE_CODE (pfn_type) != TYPE_CODE_PTR
10353 || TYPE_CODE (TYPE_TARGET_TYPE (pfn_type)) != TYPE_CODE_FUNC)
0b92b5bb 10354 return;
c0dd20ea
DJ
10355
10356 /* Look for the "this" argument. */
10357 pfn_type = TYPE_TARGET_TYPE (pfn_type);
10358 if (TYPE_NFIELDS (pfn_type) == 0
0b92b5bb 10359 /* || TYPE_FIELD_TYPE (pfn_type, 0) == NULL */
c0dd20ea 10360 || TYPE_CODE (TYPE_FIELD_TYPE (pfn_type, 0)) != TYPE_CODE_PTR)
0b92b5bb 10361 return;
c0dd20ea
DJ
10362
10363 domain_type = TYPE_TARGET_TYPE (TYPE_FIELD_TYPE (pfn_type, 0));
0b92b5bb
TT
10364 new_type = alloc_type (objfile);
10365 smash_to_method_type (new_type, domain_type, TYPE_TARGET_TYPE (pfn_type),
c0dd20ea
DJ
10366 TYPE_FIELDS (pfn_type), TYPE_NFIELDS (pfn_type),
10367 TYPE_VARARGS (pfn_type));
0b92b5bb 10368 smash_to_methodptr_type (type, new_type);
c0dd20ea 10369}
1168df01 10370
685b1105
JK
10371/* Return non-zero if the CU's PRODUCER string matches the Intel C/C++ compiler
10372 (icc). */
10373
10374static int
10375producer_is_icc (struct dwarf2_cu *cu)
10376{
10377 if (!cu->checked_producer)
10378 check_producer (cu);
10379
10380 return cu->producer_is_icc;
10381}
10382
c906108c 10383/* Called when we find the DIE that starts a structure or union scope
c767944b
DJ
10384 (definition) to create a type for the structure or union. Fill in
10385 the type's name and general properties; the members will not be
10386 processed until process_structure_type.
c906108c 10387
c767944b
DJ
10388 NOTE: we need to call these functions regardless of whether or not the
10389 DIE has a DW_AT_name attribute, since it might be an anonymous
c906108c
SS
10390 structure or union. This gets the type entered into our set of
10391 user defined types.
10392
10393 However, if the structure is incomplete (an opaque struct/union)
10394 then suppress creating a symbol table entry for it since gdb only
10395 wants to find the one with the complete definition. Note that if
10396 it is complete, we just call new_symbol, which does it's own
10397 checking about whether the struct/union is anonymous or not (and
10398 suppresses creating a symbol table entry itself). */
10399
f792889a 10400static struct type *
134d01f1 10401read_structure_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 10402{
e7c27a73 10403 struct objfile *objfile = cu->objfile;
c906108c
SS
10404 struct type *type;
10405 struct attribute *attr;
39cbfefa 10406 char *name;
c906108c 10407
348e048f
DE
10408 /* If the definition of this type lives in .debug_types, read that type.
10409 Don't follow DW_AT_specification though, that will take us back up
10410 the chain and we want to go down. */
45e58e77 10411 attr = dwarf2_attr_no_follow (die, DW_AT_signature);
348e048f
DE
10412 if (attr)
10413 {
10414 struct dwarf2_cu *type_cu = cu;
10415 struct die_info *type_die = follow_die_ref_or_sig (die, attr, &type_cu);
9a619af0 10416
348e048f
DE
10417 /* We could just recurse on read_structure_type, but we need to call
10418 get_die_type to ensure only one type for this DIE is created.
10419 This is important, for example, because for c++ classes we need
10420 TYPE_NAME set which is only done by new_symbol. Blech. */
10421 type = read_type_die (type_die, type_cu);
9dc481d3
DE
10422
10423 /* TYPE_CU may not be the same as CU.
10424 Ensure TYPE is recorded in CU's type_hash table. */
348e048f
DE
10425 return set_die_type (die, type, cu);
10426 }
10427
c0dd20ea 10428 type = alloc_type (objfile);
c906108c 10429 INIT_CPLUS_SPECIFIC (type);
93311388 10430
39cbfefa
DJ
10431 name = dwarf2_name (die, cu);
10432 if (name != NULL)
c906108c 10433 {
987504bb
JJ
10434 if (cu->language == language_cplus
10435 || cu->language == language_java)
63d06c5c 10436 {
3da10d80
KS
10437 char *full_name = (char *) dwarf2_full_name (name, die, cu);
10438
10439 /* dwarf2_full_name might have already finished building the DIE's
10440 type. If so, there is no need to continue. */
10441 if (get_die_type (die, cu) != NULL)
10442 return get_die_type (die, cu);
10443
10444 TYPE_TAG_NAME (type) = full_name;
94af9270
KS
10445 if (die->tag == DW_TAG_structure_type
10446 || die->tag == DW_TAG_class_type)
10447 TYPE_NAME (type) = TYPE_TAG_NAME (type);
63d06c5c
DC
10448 }
10449 else
10450 {
d8151005
DJ
10451 /* The name is already allocated along with this objfile, so
10452 we don't need to duplicate it for the type. */
94af9270
KS
10453 TYPE_TAG_NAME (type) = (char *) name;
10454 if (die->tag == DW_TAG_class_type)
10455 TYPE_NAME (type) = TYPE_TAG_NAME (type);
63d06c5c 10456 }
c906108c
SS
10457 }
10458
10459 if (die->tag == DW_TAG_structure_type)
10460 {
10461 TYPE_CODE (type) = TYPE_CODE_STRUCT;
10462 }
10463 else if (die->tag == DW_TAG_union_type)
10464 {
10465 TYPE_CODE (type) = TYPE_CODE_UNION;
10466 }
10467 else
10468 {
c906108c
SS
10469 TYPE_CODE (type) = TYPE_CODE_CLASS;
10470 }
10471
0cc2414c
TT
10472 if (cu->language == language_cplus && die->tag == DW_TAG_class_type)
10473 TYPE_DECLARED_CLASS (type) = 1;
10474
e142c38c 10475 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
c906108c
SS
10476 if (attr)
10477 {
10478 TYPE_LENGTH (type) = DW_UNSND (attr);
10479 }
10480 else
10481 {
10482 TYPE_LENGTH (type) = 0;
10483 }
10484
685b1105
JK
10485 if (producer_is_icc (cu))
10486 {
10487 /* ICC does not output the required DW_AT_declaration
10488 on incomplete types, but gives them a size of zero. */
10489 }
10490 else
10491 TYPE_STUB_SUPPORTED (type) = 1;
10492
dc718098 10493 if (die_is_declaration (die, cu))
876cecd0 10494 TYPE_STUB (type) = 1;
a6c727b2
DJ
10495 else if (attr == NULL && die->child == NULL
10496 && producer_is_realview (cu->producer))
10497 /* RealView does not output the required DW_AT_declaration
10498 on incomplete types. */
10499 TYPE_STUB (type) = 1;
dc718098 10500
c906108c
SS
10501 /* We need to add the type field to the die immediately so we don't
10502 infinitely recurse when dealing with pointers to the structure
0963b4bd 10503 type within the structure itself. */
1c379e20 10504 set_die_type (die, type, cu);
c906108c 10505
7e314c57
JK
10506 /* set_die_type should be already done. */
10507 set_descriptive_type (type, die, cu);
10508
c767944b
DJ
10509 return type;
10510}
10511
10512/* Finish creating a structure or union type, including filling in
10513 its members and creating a symbol for it. */
10514
10515static void
10516process_structure_scope (struct die_info *die, struct dwarf2_cu *cu)
10517{
10518 struct objfile *objfile = cu->objfile;
10519 struct die_info *child_die = die->child;
10520 struct type *type;
10521
10522 type = get_die_type (die, cu);
10523 if (type == NULL)
10524 type = read_structure_type (die, cu);
10525
e142c38c 10526 if (die->child != NULL && ! die_is_declaration (die, cu))
c906108c
SS
10527 {
10528 struct field_info fi;
10529 struct die_info *child_die;
34eaf542 10530 VEC (symbolp) *template_args = NULL;
c767944b 10531 struct cleanup *back_to = make_cleanup (null_cleanup, 0);
c906108c
SS
10532
10533 memset (&fi, 0, sizeof (struct field_info));
10534
639d11d3 10535 child_die = die->child;
c906108c
SS
10536
10537 while (child_die && child_die->tag)
10538 {
a9a9bd0f
DC
10539 if (child_die->tag == DW_TAG_member
10540 || child_die->tag == DW_TAG_variable)
c906108c 10541 {
a9a9bd0f
DC
10542 /* NOTE: carlton/2002-11-05: A C++ static data member
10543 should be a DW_TAG_member that is a declaration, but
10544 all versions of G++ as of this writing (so through at
10545 least 3.2.1) incorrectly generate DW_TAG_variable
10546 tags for them instead. */
e7c27a73 10547 dwarf2_add_field (&fi, child_die, cu);
c906108c 10548 }
8713b1b1 10549 else if (child_die->tag == DW_TAG_subprogram)
c906108c 10550 {
0963b4bd 10551 /* C++ member function. */
e7c27a73 10552 dwarf2_add_member_fn (&fi, child_die, type, cu);
c906108c
SS
10553 }
10554 else if (child_die->tag == DW_TAG_inheritance)
10555 {
10556 /* C++ base class field. */
e7c27a73 10557 dwarf2_add_field (&fi, child_die, cu);
c906108c 10558 }
98751a41
JK
10559 else if (child_die->tag == DW_TAG_typedef)
10560 dwarf2_add_typedef (&fi, child_die, cu);
34eaf542
TT
10561 else if (child_die->tag == DW_TAG_template_type_param
10562 || child_die->tag == DW_TAG_template_value_param)
10563 {
10564 struct symbol *arg = new_symbol (child_die, NULL, cu);
10565
f1078f66
DJ
10566 if (arg != NULL)
10567 VEC_safe_push (symbolp, template_args, arg);
34eaf542
TT
10568 }
10569
c906108c
SS
10570 child_die = sibling_die (child_die);
10571 }
10572
34eaf542
TT
10573 /* Attach template arguments to type. */
10574 if (! VEC_empty (symbolp, template_args))
10575 {
10576 ALLOCATE_CPLUS_STRUCT_TYPE (type);
10577 TYPE_N_TEMPLATE_ARGUMENTS (type)
10578 = VEC_length (symbolp, template_args);
10579 TYPE_TEMPLATE_ARGUMENTS (type)
10580 = obstack_alloc (&objfile->objfile_obstack,
10581 (TYPE_N_TEMPLATE_ARGUMENTS (type)
10582 * sizeof (struct symbol *)));
10583 memcpy (TYPE_TEMPLATE_ARGUMENTS (type),
10584 VEC_address (symbolp, template_args),
10585 (TYPE_N_TEMPLATE_ARGUMENTS (type)
10586 * sizeof (struct symbol *)));
10587 VEC_free (symbolp, template_args);
10588 }
10589
c906108c
SS
10590 /* Attach fields and member functions to the type. */
10591 if (fi.nfields)
e7c27a73 10592 dwarf2_attach_fields_to_type (&fi, type, cu);
c906108c
SS
10593 if (fi.nfnfields)
10594 {
e7c27a73 10595 dwarf2_attach_fn_fields_to_type (&fi, type, cu);
c906108c 10596
c5aa993b 10597 /* Get the type which refers to the base class (possibly this
c906108c 10598 class itself) which contains the vtable pointer for the current
0d564a31
DJ
10599 class from the DW_AT_containing_type attribute. This use of
10600 DW_AT_containing_type is a GNU extension. */
c906108c 10601
e142c38c 10602 if (dwarf2_attr (die, DW_AT_containing_type, cu) != NULL)
c906108c 10603 {
e7c27a73 10604 struct type *t = die_containing_type (die, cu);
c906108c
SS
10605
10606 TYPE_VPTR_BASETYPE (type) = t;
10607 if (type == t)
10608 {
c906108c
SS
10609 int i;
10610
10611 /* Our own class provides vtbl ptr. */
10612 for (i = TYPE_NFIELDS (t) - 1;
10613 i >= TYPE_N_BASECLASSES (t);
10614 --i)
10615 {
0d5cff50 10616 const char *fieldname = TYPE_FIELD_NAME (t, i);
c906108c 10617
1168df01 10618 if (is_vtable_name (fieldname, cu))
c906108c
SS
10619 {
10620 TYPE_VPTR_FIELDNO (type) = i;
10621 break;
10622 }
10623 }
10624
10625 /* Complain if virtual function table field not found. */
10626 if (i < TYPE_N_BASECLASSES (t))
4d3c2250 10627 complaint (&symfile_complaints,
3e43a32a
MS
10628 _("virtual function table pointer "
10629 "not found when defining class '%s'"),
4d3c2250
KB
10630 TYPE_TAG_NAME (type) ? TYPE_TAG_NAME (type) :
10631 "");
c906108c
SS
10632 }
10633 else
10634 {
10635 TYPE_VPTR_FIELDNO (type) = TYPE_VPTR_FIELDNO (t);
10636 }
10637 }
f6235d4c
EZ
10638 else if (cu->producer
10639 && strncmp (cu->producer,
10640 "IBM(R) XL C/C++ Advanced Edition", 32) == 0)
10641 {
10642 /* The IBM XLC compiler does not provide direct indication
10643 of the containing type, but the vtable pointer is
10644 always named __vfp. */
10645
10646 int i;
10647
10648 for (i = TYPE_NFIELDS (type) - 1;
10649 i >= TYPE_N_BASECLASSES (type);
10650 --i)
10651 {
10652 if (strcmp (TYPE_FIELD_NAME (type, i), "__vfp") == 0)
10653 {
10654 TYPE_VPTR_FIELDNO (type) = i;
10655 TYPE_VPTR_BASETYPE (type) = type;
10656 break;
10657 }
10658 }
10659 }
c906108c 10660 }
98751a41
JK
10661
10662 /* Copy fi.typedef_field_list linked list elements content into the
10663 allocated array TYPE_TYPEDEF_FIELD_ARRAY (type). */
10664 if (fi.typedef_field_list)
10665 {
10666 int i = fi.typedef_field_list_count;
10667
a0d7a4ff 10668 ALLOCATE_CPLUS_STRUCT_TYPE (type);
98751a41
JK
10669 TYPE_TYPEDEF_FIELD_ARRAY (type)
10670 = TYPE_ALLOC (type, sizeof (TYPE_TYPEDEF_FIELD (type, 0)) * i);
10671 TYPE_TYPEDEF_FIELD_COUNT (type) = i;
10672
10673 /* Reverse the list order to keep the debug info elements order. */
10674 while (--i >= 0)
10675 {
10676 struct typedef_field *dest, *src;
6e70227d 10677
98751a41
JK
10678 dest = &TYPE_TYPEDEF_FIELD (type, i);
10679 src = &fi.typedef_field_list->field;
10680 fi.typedef_field_list = fi.typedef_field_list->next;
10681 *dest = *src;
10682 }
10683 }
c767944b
DJ
10684
10685 do_cleanups (back_to);
eb2a6f42
TT
10686
10687 if (HAVE_CPLUS_STRUCT (type))
10688 TYPE_CPLUS_REALLY_JAVA (type) = cu->language == language_java;
c906108c 10689 }
63d06c5c 10690
bb5ed363 10691 quirk_gcc_member_function_pointer (type, objfile);
0b92b5bb 10692
90aeadfc
DC
10693 /* NOTE: carlton/2004-03-16: GCC 3.4 (or at least one of its
10694 snapshots) has been known to create a die giving a declaration
10695 for a class that has, as a child, a die giving a definition for a
10696 nested class. So we have to process our children even if the
10697 current die is a declaration. Normally, of course, a declaration
10698 won't have any children at all. */
134d01f1 10699
90aeadfc
DC
10700 while (child_die != NULL && child_die->tag)
10701 {
10702 if (child_die->tag == DW_TAG_member
10703 || child_die->tag == DW_TAG_variable
34eaf542
TT
10704 || child_die->tag == DW_TAG_inheritance
10705 || child_die->tag == DW_TAG_template_value_param
10706 || child_die->tag == DW_TAG_template_type_param)
134d01f1 10707 {
90aeadfc 10708 /* Do nothing. */
134d01f1 10709 }
90aeadfc
DC
10710 else
10711 process_die (child_die, cu);
134d01f1 10712
90aeadfc 10713 child_die = sibling_die (child_die);
134d01f1
DJ
10714 }
10715
fa4028e9
JB
10716 /* Do not consider external references. According to the DWARF standard,
10717 these DIEs are identified by the fact that they have no byte_size
10718 attribute, and a declaration attribute. */
10719 if (dwarf2_attr (die, DW_AT_byte_size, cu) != NULL
10720 || !die_is_declaration (die, cu))
c767944b 10721 new_symbol (die, type, cu);
134d01f1
DJ
10722}
10723
10724/* Given a DW_AT_enumeration_type die, set its type. We do not
10725 complete the type's fields yet, or create any symbols. */
c906108c 10726
f792889a 10727static struct type *
134d01f1 10728read_enumeration_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 10729{
e7c27a73 10730 struct objfile *objfile = cu->objfile;
c906108c 10731 struct type *type;
c906108c 10732 struct attribute *attr;
0114d602 10733 const char *name;
134d01f1 10734
348e048f
DE
10735 /* If the definition of this type lives in .debug_types, read that type.
10736 Don't follow DW_AT_specification though, that will take us back up
10737 the chain and we want to go down. */
45e58e77 10738 attr = dwarf2_attr_no_follow (die, DW_AT_signature);
348e048f
DE
10739 if (attr)
10740 {
10741 struct dwarf2_cu *type_cu = cu;
10742 struct die_info *type_die = follow_die_ref_or_sig (die, attr, &type_cu);
9a619af0 10743
348e048f 10744 type = read_type_die (type_die, type_cu);
9dc481d3
DE
10745
10746 /* TYPE_CU may not be the same as CU.
10747 Ensure TYPE is recorded in CU's type_hash table. */
348e048f
DE
10748 return set_die_type (die, type, cu);
10749 }
10750
c906108c
SS
10751 type = alloc_type (objfile);
10752
10753 TYPE_CODE (type) = TYPE_CODE_ENUM;
94af9270 10754 name = dwarf2_full_name (NULL, die, cu);
39cbfefa 10755 if (name != NULL)
0114d602 10756 TYPE_TAG_NAME (type) = (char *) name;
c906108c 10757
e142c38c 10758 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
c906108c
SS
10759 if (attr)
10760 {
10761 TYPE_LENGTH (type) = DW_UNSND (attr);
10762 }
10763 else
10764 {
10765 TYPE_LENGTH (type) = 0;
10766 }
10767
137033e9
JB
10768 /* The enumeration DIE can be incomplete. In Ada, any type can be
10769 declared as private in the package spec, and then defined only
10770 inside the package body. Such types are known as Taft Amendment
10771 Types. When another package uses such a type, an incomplete DIE
10772 may be generated by the compiler. */
02eb380e 10773 if (die_is_declaration (die, cu))
876cecd0 10774 TYPE_STUB (type) = 1;
02eb380e 10775
f792889a 10776 return set_die_type (die, type, cu);
134d01f1
DJ
10777}
10778
10779/* Given a pointer to a die which begins an enumeration, process all
10780 the dies that define the members of the enumeration, and create the
10781 symbol for the enumeration type.
10782
10783 NOTE: We reverse the order of the element list. */
10784
10785static void
10786process_enumeration_scope (struct die_info *die, struct dwarf2_cu *cu)
10787{
f792889a 10788 struct type *this_type;
134d01f1 10789
f792889a
DJ
10790 this_type = get_die_type (die, cu);
10791 if (this_type == NULL)
10792 this_type = read_enumeration_type (die, cu);
9dc481d3 10793
639d11d3 10794 if (die->child != NULL)
c906108c 10795 {
9dc481d3
DE
10796 struct die_info *child_die;
10797 struct symbol *sym;
10798 struct field *fields = NULL;
10799 int num_fields = 0;
10800 int unsigned_enum = 1;
10801 char *name;
cafec441
TT
10802 int flag_enum = 1;
10803 ULONGEST mask = 0;
9dc481d3 10804
639d11d3 10805 child_die = die->child;
c906108c
SS
10806 while (child_die && child_die->tag)
10807 {
10808 if (child_die->tag != DW_TAG_enumerator)
10809 {
e7c27a73 10810 process_die (child_die, cu);
c906108c
SS
10811 }
10812 else
10813 {
39cbfefa
DJ
10814 name = dwarf2_name (child_die, cu);
10815 if (name)
c906108c 10816 {
f792889a 10817 sym = new_symbol (child_die, this_type, cu);
c906108c 10818 if (SYMBOL_VALUE (sym) < 0)
cafec441
TT
10819 {
10820 unsigned_enum = 0;
10821 flag_enum = 0;
10822 }
10823 else if ((mask & SYMBOL_VALUE (sym)) != 0)
10824 flag_enum = 0;
10825 else
10826 mask |= SYMBOL_VALUE (sym);
c906108c
SS
10827
10828 if ((num_fields % DW_FIELD_ALLOC_CHUNK) == 0)
10829 {
10830 fields = (struct field *)
10831 xrealloc (fields,
10832 (num_fields + DW_FIELD_ALLOC_CHUNK)
c5aa993b 10833 * sizeof (struct field));
c906108c
SS
10834 }
10835
3567439c 10836 FIELD_NAME (fields[num_fields]) = SYMBOL_LINKAGE_NAME (sym);
c906108c 10837 FIELD_TYPE (fields[num_fields]) = NULL;
14e75d8e 10838 SET_FIELD_ENUMVAL (fields[num_fields], SYMBOL_VALUE (sym));
c906108c
SS
10839 FIELD_BITSIZE (fields[num_fields]) = 0;
10840
10841 num_fields++;
10842 }
10843 }
10844
10845 child_die = sibling_die (child_die);
10846 }
10847
10848 if (num_fields)
10849 {
f792889a
DJ
10850 TYPE_NFIELDS (this_type) = num_fields;
10851 TYPE_FIELDS (this_type) = (struct field *)
10852 TYPE_ALLOC (this_type, sizeof (struct field) * num_fields);
10853 memcpy (TYPE_FIELDS (this_type), fields,
c906108c 10854 sizeof (struct field) * num_fields);
b8c9b27d 10855 xfree (fields);
c906108c
SS
10856 }
10857 if (unsigned_enum)
876cecd0 10858 TYPE_UNSIGNED (this_type) = 1;
cafec441
TT
10859 if (flag_enum)
10860 TYPE_FLAG_ENUM (this_type) = 1;
c906108c 10861 }
134d01f1 10862
6c83ed52
TT
10863 /* If we are reading an enum from a .debug_types unit, and the enum
10864 is a declaration, and the enum is not the signatured type in the
10865 unit, then we do not want to add a symbol for it. Adding a
10866 symbol would in some cases obscure the true definition of the
10867 enum, giving users an incomplete type when the definition is
10868 actually available. Note that we do not want to do this for all
10869 enums which are just declarations, because C++0x allows forward
10870 enum declarations. */
3019eac3 10871 if (cu->per_cu->is_debug_types
6c83ed52
TT
10872 && die_is_declaration (die, cu))
10873 {
52dc124a 10874 struct signatured_type *sig_type;
6c83ed52 10875
52dc124a 10876 sig_type
6c83ed52 10877 = lookup_signatured_type_at_offset (dwarf2_per_objfile->objfile,
3019eac3 10878 cu->per_cu->info_or_types_section,
6c83ed52 10879 cu->per_cu->offset);
3019eac3
DE
10880 gdb_assert (sig_type->type_offset_in_section.sect_off != 0);
10881 if (sig_type->type_offset_in_section.sect_off != die->offset.sect_off)
6c83ed52
TT
10882 return;
10883 }
10884
f792889a 10885 new_symbol (die, this_type, cu);
c906108c
SS
10886}
10887
10888/* Extract all information from a DW_TAG_array_type DIE and put it in
10889 the DIE's type field. For now, this only handles one dimensional
10890 arrays. */
10891
f792889a 10892static struct type *
e7c27a73 10893read_array_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 10894{
e7c27a73 10895 struct objfile *objfile = cu->objfile;
c906108c 10896 struct die_info *child_die;
7e314c57 10897 struct type *type;
c906108c
SS
10898 struct type *element_type, *range_type, *index_type;
10899 struct type **range_types = NULL;
10900 struct attribute *attr;
10901 int ndim = 0;
10902 struct cleanup *back_to;
39cbfefa 10903 char *name;
c906108c 10904
e7c27a73 10905 element_type = die_type (die, cu);
c906108c 10906
7e314c57
JK
10907 /* The die_type call above may have already set the type for this DIE. */
10908 type = get_die_type (die, cu);
10909 if (type)
10910 return type;
10911
c906108c
SS
10912 /* Irix 6.2 native cc creates array types without children for
10913 arrays with unspecified length. */
639d11d3 10914 if (die->child == NULL)
c906108c 10915 {
46bf5051 10916 index_type = objfile_type (objfile)->builtin_int;
c906108c 10917 range_type = create_range_type (NULL, index_type, 0, -1);
f792889a
DJ
10918 type = create_array_type (NULL, element_type, range_type);
10919 return set_die_type (die, type, cu);
c906108c
SS
10920 }
10921
10922 back_to = make_cleanup (null_cleanup, NULL);
639d11d3 10923 child_die = die->child;
c906108c
SS
10924 while (child_die && child_die->tag)
10925 {
10926 if (child_die->tag == DW_TAG_subrange_type)
10927 {
f792889a 10928 struct type *child_type = read_type_die (child_die, cu);
9a619af0 10929
f792889a 10930 if (child_type != NULL)
a02abb62 10931 {
0963b4bd
MS
10932 /* The range type was succesfully read. Save it for the
10933 array type creation. */
a02abb62
JB
10934 if ((ndim % DW_FIELD_ALLOC_CHUNK) == 0)
10935 {
10936 range_types = (struct type **)
10937 xrealloc (range_types, (ndim + DW_FIELD_ALLOC_CHUNK)
10938 * sizeof (struct type *));
10939 if (ndim == 0)
10940 make_cleanup (free_current_contents, &range_types);
10941 }
f792889a 10942 range_types[ndim++] = child_type;
a02abb62 10943 }
c906108c
SS
10944 }
10945 child_die = sibling_die (child_die);
10946 }
10947
10948 /* Dwarf2 dimensions are output from left to right, create the
10949 necessary array types in backwards order. */
7ca2d3a3 10950
c906108c 10951 type = element_type;
7ca2d3a3
DL
10952
10953 if (read_array_order (die, cu) == DW_ORD_col_major)
10954 {
10955 int i = 0;
9a619af0 10956
7ca2d3a3
DL
10957 while (i < ndim)
10958 type = create_array_type (NULL, type, range_types[i++]);
10959 }
10960 else
10961 {
10962 while (ndim-- > 0)
10963 type = create_array_type (NULL, type, range_types[ndim]);
10964 }
c906108c 10965
f5f8a009
EZ
10966 /* Understand Dwarf2 support for vector types (like they occur on
10967 the PowerPC w/ AltiVec). Gcc just adds another attribute to the
10968 array type. This is not part of the Dwarf2/3 standard yet, but a
10969 custom vendor extension. The main difference between a regular
10970 array and the vector variant is that vectors are passed by value
10971 to functions. */
e142c38c 10972 attr = dwarf2_attr (die, DW_AT_GNU_vector, cu);
f5f8a009 10973 if (attr)
ea37ba09 10974 make_vector_type (type);
f5f8a009 10975
dbc98a8b
KW
10976 /* The DIE may have DW_AT_byte_size set. For example an OpenCL
10977 implementation may choose to implement triple vectors using this
10978 attribute. */
10979 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
10980 if (attr)
10981 {
10982 if (DW_UNSND (attr) >= TYPE_LENGTH (type))
10983 TYPE_LENGTH (type) = DW_UNSND (attr);
10984 else
3e43a32a
MS
10985 complaint (&symfile_complaints,
10986 _("DW_AT_byte_size for array type smaller "
10987 "than the total size of elements"));
dbc98a8b
KW
10988 }
10989
39cbfefa
DJ
10990 name = dwarf2_name (die, cu);
10991 if (name)
10992 TYPE_NAME (type) = name;
6e70227d 10993
0963b4bd 10994 /* Install the type in the die. */
7e314c57
JK
10995 set_die_type (die, type, cu);
10996
10997 /* set_die_type should be already done. */
b4ba55a1
JB
10998 set_descriptive_type (type, die, cu);
10999
c906108c
SS
11000 do_cleanups (back_to);
11001
7e314c57 11002 return type;
c906108c
SS
11003}
11004
7ca2d3a3 11005static enum dwarf_array_dim_ordering
6e70227d 11006read_array_order (struct die_info *die, struct dwarf2_cu *cu)
7ca2d3a3
DL
11007{
11008 struct attribute *attr;
11009
11010 attr = dwarf2_attr (die, DW_AT_ordering, cu);
11011
11012 if (attr) return DW_SND (attr);
11013
0963b4bd
MS
11014 /* GNU F77 is a special case, as at 08/2004 array type info is the
11015 opposite order to the dwarf2 specification, but data is still
11016 laid out as per normal fortran.
7ca2d3a3 11017
0963b4bd
MS
11018 FIXME: dsl/2004-8-20: If G77 is ever fixed, this will also need
11019 version checking. */
7ca2d3a3 11020
905e0470
PM
11021 if (cu->language == language_fortran
11022 && cu->producer && strstr (cu->producer, "GNU F77"))
7ca2d3a3
DL
11023 {
11024 return DW_ORD_row_major;
11025 }
11026
6e70227d 11027 switch (cu->language_defn->la_array_ordering)
7ca2d3a3
DL
11028 {
11029 case array_column_major:
11030 return DW_ORD_col_major;
11031 case array_row_major:
11032 default:
11033 return DW_ORD_row_major;
11034 };
11035}
11036
72019c9c 11037/* Extract all information from a DW_TAG_set_type DIE and put it in
0963b4bd 11038 the DIE's type field. */
72019c9c 11039
f792889a 11040static struct type *
72019c9c
GM
11041read_set_type (struct die_info *die, struct dwarf2_cu *cu)
11042{
7e314c57
JK
11043 struct type *domain_type, *set_type;
11044 struct attribute *attr;
f792889a 11045
7e314c57
JK
11046 domain_type = die_type (die, cu);
11047
11048 /* The die_type call above may have already set the type for this DIE. */
11049 set_type = get_die_type (die, cu);
11050 if (set_type)
11051 return set_type;
11052
11053 set_type = create_set_type (NULL, domain_type);
11054
11055 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
d09039dd
PM
11056 if (attr)
11057 TYPE_LENGTH (set_type) = DW_UNSND (attr);
7e314c57 11058
f792889a 11059 return set_die_type (die, set_type, cu);
72019c9c 11060}
7ca2d3a3 11061
c906108c
SS
11062/* First cut: install each common block member as a global variable. */
11063
11064static void
e7c27a73 11065read_common_block (struct die_info *die, struct dwarf2_cu *cu)
c906108c
SS
11066{
11067 struct die_info *child_die;
11068 struct attribute *attr;
11069 struct symbol *sym;
11070 CORE_ADDR base = (CORE_ADDR) 0;
11071
e142c38c 11072 attr = dwarf2_attr (die, DW_AT_location, cu);
c906108c
SS
11073 if (attr)
11074 {
0963b4bd 11075 /* Support the .debug_loc offsets. */
8e19ed76
PS
11076 if (attr_form_is_block (attr))
11077 {
e7c27a73 11078 base = decode_locdesc (DW_BLOCK (attr), cu);
8e19ed76 11079 }
3690dd37 11080 else if (attr_form_is_section_offset (attr))
8e19ed76 11081 {
4d3c2250 11082 dwarf2_complex_location_expr_complaint ();
8e19ed76
PS
11083 }
11084 else
11085 {
4d3c2250
KB
11086 dwarf2_invalid_attrib_class_complaint ("DW_AT_location",
11087 "common block member");
8e19ed76 11088 }
c906108c 11089 }
639d11d3 11090 if (die->child != NULL)
c906108c 11091 {
639d11d3 11092 child_die = die->child;
c906108c
SS
11093 while (child_die && child_die->tag)
11094 {
74ac6d43
TT
11095 LONGEST offset;
11096
e7c27a73 11097 sym = new_symbol (child_die, NULL, cu);
e8d28ef4
TT
11098 if (sym != NULL
11099 && handle_data_member_location (child_die, cu, &offset))
c906108c 11100 {
74ac6d43 11101 SYMBOL_VALUE_ADDRESS (sym) = base + offset;
c906108c
SS
11102 add_symbol_to_list (sym, &global_symbols);
11103 }
11104 child_die = sibling_die (child_die);
11105 }
11106 }
11107}
11108
0114d602 11109/* Create a type for a C++ namespace. */
d9fa45fe 11110
0114d602
DJ
11111static struct type *
11112read_namespace_type (struct die_info *die, struct dwarf2_cu *cu)
d9fa45fe 11113{
e7c27a73 11114 struct objfile *objfile = cu->objfile;
0114d602 11115 const char *previous_prefix, *name;
9219021c 11116 int is_anonymous;
0114d602
DJ
11117 struct type *type;
11118
11119 /* For extensions, reuse the type of the original namespace. */
11120 if (dwarf2_attr (die, DW_AT_extension, cu) != NULL)
11121 {
11122 struct die_info *ext_die;
11123 struct dwarf2_cu *ext_cu = cu;
9a619af0 11124
0114d602
DJ
11125 ext_die = dwarf2_extension (die, &ext_cu);
11126 type = read_type_die (ext_die, ext_cu);
9dc481d3
DE
11127
11128 /* EXT_CU may not be the same as CU.
11129 Ensure TYPE is recorded in CU's type_hash table. */
0114d602
DJ
11130 return set_die_type (die, type, cu);
11131 }
9219021c 11132
e142c38c 11133 name = namespace_name (die, &is_anonymous, cu);
9219021c
DC
11134
11135 /* Now build the name of the current namespace. */
11136
0114d602
DJ
11137 previous_prefix = determine_prefix (die, cu);
11138 if (previous_prefix[0] != '\0')
11139 name = typename_concat (&objfile->objfile_obstack,
f55ee35c 11140 previous_prefix, name, 0, cu);
0114d602
DJ
11141
11142 /* Create the type. */
11143 type = init_type (TYPE_CODE_NAMESPACE, 0, 0, NULL,
11144 objfile);
11145 TYPE_NAME (type) = (char *) name;
11146 TYPE_TAG_NAME (type) = TYPE_NAME (type);
11147
60531b24 11148 return set_die_type (die, type, cu);
0114d602
DJ
11149}
11150
11151/* Read a C++ namespace. */
11152
11153static void
11154read_namespace (struct die_info *die, struct dwarf2_cu *cu)
11155{
11156 struct objfile *objfile = cu->objfile;
0114d602 11157 int is_anonymous;
9219021c 11158
5c4e30ca
DC
11159 /* Add a symbol associated to this if we haven't seen the namespace
11160 before. Also, add a using directive if it's an anonymous
11161 namespace. */
9219021c 11162
f2f0e013 11163 if (dwarf2_attr (die, DW_AT_extension, cu) == NULL)
5c4e30ca
DC
11164 {
11165 struct type *type;
11166
0114d602 11167 type = read_type_die (die, cu);
e7c27a73 11168 new_symbol (die, type, cu);
5c4e30ca 11169
e8e80198 11170 namespace_name (die, &is_anonymous, cu);
5c4e30ca 11171 if (is_anonymous)
0114d602
DJ
11172 {
11173 const char *previous_prefix = determine_prefix (die, cu);
9a619af0 11174
c0cc3a76 11175 cp_add_using_directive (previous_prefix, TYPE_NAME (type), NULL,
32019081 11176 NULL, NULL, &objfile->objfile_obstack);
0114d602 11177 }
5c4e30ca 11178 }
9219021c 11179
639d11d3 11180 if (die->child != NULL)
d9fa45fe 11181 {
639d11d3 11182 struct die_info *child_die = die->child;
6e70227d 11183
d9fa45fe
DC
11184 while (child_die && child_die->tag)
11185 {
e7c27a73 11186 process_die (child_die, cu);
d9fa45fe
DC
11187 child_die = sibling_die (child_die);
11188 }
11189 }
38d518c9
EZ
11190}
11191
f55ee35c
JK
11192/* Read a Fortran module as type. This DIE can be only a declaration used for
11193 imported module. Still we need that type as local Fortran "use ... only"
11194 declaration imports depend on the created type in determine_prefix. */
11195
11196static struct type *
11197read_module_type (struct die_info *die, struct dwarf2_cu *cu)
11198{
11199 struct objfile *objfile = cu->objfile;
11200 char *module_name;
11201 struct type *type;
11202
11203 module_name = dwarf2_name (die, cu);
11204 if (!module_name)
3e43a32a
MS
11205 complaint (&symfile_complaints,
11206 _("DW_TAG_module has no name, offset 0x%x"),
b64f50a1 11207 die->offset.sect_off);
f55ee35c
JK
11208 type = init_type (TYPE_CODE_MODULE, 0, 0, module_name, objfile);
11209
11210 /* determine_prefix uses TYPE_TAG_NAME. */
11211 TYPE_TAG_NAME (type) = TYPE_NAME (type);
11212
11213 return set_die_type (die, type, cu);
11214}
11215
5d7cb8df
JK
11216/* Read a Fortran module. */
11217
11218static void
11219read_module (struct die_info *die, struct dwarf2_cu *cu)
11220{
11221 struct die_info *child_die = die->child;
11222
5d7cb8df
JK
11223 while (child_die && child_die->tag)
11224 {
11225 process_die (child_die, cu);
11226 child_die = sibling_die (child_die);
11227 }
11228}
11229
38d518c9
EZ
11230/* Return the name of the namespace represented by DIE. Set
11231 *IS_ANONYMOUS to tell whether or not the namespace is an anonymous
11232 namespace. */
11233
11234static const char *
e142c38c 11235namespace_name (struct die_info *die, int *is_anonymous, struct dwarf2_cu *cu)
38d518c9
EZ
11236{
11237 struct die_info *current_die;
11238 const char *name = NULL;
11239
11240 /* Loop through the extensions until we find a name. */
11241
11242 for (current_die = die;
11243 current_die != NULL;
f2f0e013 11244 current_die = dwarf2_extension (die, &cu))
38d518c9 11245 {
e142c38c 11246 name = dwarf2_name (current_die, cu);
38d518c9
EZ
11247 if (name != NULL)
11248 break;
11249 }
11250
11251 /* Is it an anonymous namespace? */
11252
11253 *is_anonymous = (name == NULL);
11254 if (*is_anonymous)
2b1dbab0 11255 name = CP_ANONYMOUS_NAMESPACE_STR;
38d518c9
EZ
11256
11257 return name;
d9fa45fe
DC
11258}
11259
c906108c
SS
11260/* Extract all information from a DW_TAG_pointer_type DIE and add to
11261 the user defined type vector. */
11262
f792889a 11263static struct type *
e7c27a73 11264read_tag_pointer_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 11265{
5e2b427d 11266 struct gdbarch *gdbarch = get_objfile_arch (cu->objfile);
e7c27a73 11267 struct comp_unit_head *cu_header = &cu->header;
c906108c 11268 struct type *type;
8b2dbe47
KB
11269 struct attribute *attr_byte_size;
11270 struct attribute *attr_address_class;
11271 int byte_size, addr_class;
7e314c57
JK
11272 struct type *target_type;
11273
11274 target_type = die_type (die, cu);
c906108c 11275
7e314c57
JK
11276 /* The die_type call above may have already set the type for this DIE. */
11277 type = get_die_type (die, cu);
11278 if (type)
11279 return type;
11280
11281 type = lookup_pointer_type (target_type);
8b2dbe47 11282
e142c38c 11283 attr_byte_size = dwarf2_attr (die, DW_AT_byte_size, cu);
8b2dbe47
KB
11284 if (attr_byte_size)
11285 byte_size = DW_UNSND (attr_byte_size);
c906108c 11286 else
8b2dbe47
KB
11287 byte_size = cu_header->addr_size;
11288
e142c38c 11289 attr_address_class = dwarf2_attr (die, DW_AT_address_class, cu);
8b2dbe47
KB
11290 if (attr_address_class)
11291 addr_class = DW_UNSND (attr_address_class);
11292 else
11293 addr_class = DW_ADDR_none;
11294
11295 /* If the pointer size or address class is different than the
11296 default, create a type variant marked as such and set the
11297 length accordingly. */
11298 if (TYPE_LENGTH (type) != byte_size || addr_class != DW_ADDR_none)
c906108c 11299 {
5e2b427d 11300 if (gdbarch_address_class_type_flags_p (gdbarch))
8b2dbe47
KB
11301 {
11302 int type_flags;
11303
849957d9 11304 type_flags = gdbarch_address_class_type_flags
5e2b427d 11305 (gdbarch, byte_size, addr_class);
876cecd0
TT
11306 gdb_assert ((type_flags & ~TYPE_INSTANCE_FLAG_ADDRESS_CLASS_ALL)
11307 == 0);
8b2dbe47
KB
11308 type = make_type_with_address_space (type, type_flags);
11309 }
11310 else if (TYPE_LENGTH (type) != byte_size)
11311 {
3e43a32a
MS
11312 complaint (&symfile_complaints,
11313 _("invalid pointer size %d"), byte_size);
8b2dbe47 11314 }
6e70227d 11315 else
9a619af0
MS
11316 {
11317 /* Should we also complain about unhandled address classes? */
11318 }
c906108c 11319 }
8b2dbe47
KB
11320
11321 TYPE_LENGTH (type) = byte_size;
f792889a 11322 return set_die_type (die, type, cu);
c906108c
SS
11323}
11324
11325/* Extract all information from a DW_TAG_ptr_to_member_type DIE and add to
11326 the user defined type vector. */
11327
f792889a 11328static struct type *
e7c27a73 11329read_tag_ptr_to_member_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c
SS
11330{
11331 struct type *type;
11332 struct type *to_type;
11333 struct type *domain;
11334
e7c27a73
DJ
11335 to_type = die_type (die, cu);
11336 domain = die_containing_type (die, cu);
0d5de010 11337
7e314c57
JK
11338 /* The calls above may have already set the type for this DIE. */
11339 type = get_die_type (die, cu);
11340 if (type)
11341 return type;
11342
0d5de010
DJ
11343 if (TYPE_CODE (check_typedef (to_type)) == TYPE_CODE_METHOD)
11344 type = lookup_methodptr_type (to_type);
11345 else
11346 type = lookup_memberptr_type (to_type, domain);
c906108c 11347
f792889a 11348 return set_die_type (die, type, cu);
c906108c
SS
11349}
11350
11351/* Extract all information from a DW_TAG_reference_type DIE and add to
11352 the user defined type vector. */
11353
f792889a 11354static struct type *
e7c27a73 11355read_tag_reference_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 11356{
e7c27a73 11357 struct comp_unit_head *cu_header = &cu->header;
7e314c57 11358 struct type *type, *target_type;
c906108c
SS
11359 struct attribute *attr;
11360
7e314c57
JK
11361 target_type = die_type (die, cu);
11362
11363 /* The die_type call above may have already set the type for this DIE. */
11364 type = get_die_type (die, cu);
11365 if (type)
11366 return type;
11367
11368 type = lookup_reference_type (target_type);
e142c38c 11369 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
c906108c
SS
11370 if (attr)
11371 {
11372 TYPE_LENGTH (type) = DW_UNSND (attr);
11373 }
11374 else
11375 {
107d2387 11376 TYPE_LENGTH (type) = cu_header->addr_size;
c906108c 11377 }
f792889a 11378 return set_die_type (die, type, cu);
c906108c
SS
11379}
11380
f792889a 11381static struct type *
e7c27a73 11382read_tag_const_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 11383{
f792889a 11384 struct type *base_type, *cv_type;
c906108c 11385
e7c27a73 11386 base_type = die_type (die, cu);
7e314c57
JK
11387
11388 /* The die_type call above may have already set the type for this DIE. */
11389 cv_type = get_die_type (die, cu);
11390 if (cv_type)
11391 return cv_type;
11392
2f608a3a
KW
11393 /* In case the const qualifier is applied to an array type, the element type
11394 is so qualified, not the array type (section 6.7.3 of C99). */
11395 if (TYPE_CODE (base_type) == TYPE_CODE_ARRAY)
11396 {
11397 struct type *el_type, *inner_array;
11398
11399 base_type = copy_type (base_type);
11400 inner_array = base_type;
11401
11402 while (TYPE_CODE (TYPE_TARGET_TYPE (inner_array)) == TYPE_CODE_ARRAY)
11403 {
11404 TYPE_TARGET_TYPE (inner_array) =
11405 copy_type (TYPE_TARGET_TYPE (inner_array));
11406 inner_array = TYPE_TARGET_TYPE (inner_array);
11407 }
11408
11409 el_type = TYPE_TARGET_TYPE (inner_array);
11410 TYPE_TARGET_TYPE (inner_array) =
11411 make_cv_type (1, TYPE_VOLATILE (el_type), el_type, NULL);
11412
11413 return set_die_type (die, base_type, cu);
11414 }
11415
f792889a
DJ
11416 cv_type = make_cv_type (1, TYPE_VOLATILE (base_type), base_type, 0);
11417 return set_die_type (die, cv_type, cu);
c906108c
SS
11418}
11419
f792889a 11420static struct type *
e7c27a73 11421read_tag_volatile_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 11422{
f792889a 11423 struct type *base_type, *cv_type;
c906108c 11424
e7c27a73 11425 base_type = die_type (die, cu);
7e314c57
JK
11426
11427 /* The die_type call above may have already set the type for this DIE. */
11428 cv_type = get_die_type (die, cu);
11429 if (cv_type)
11430 return cv_type;
11431
f792889a
DJ
11432 cv_type = make_cv_type (TYPE_CONST (base_type), 1, base_type, 0);
11433 return set_die_type (die, cv_type, cu);
c906108c
SS
11434}
11435
11436/* Extract all information from a DW_TAG_string_type DIE and add to
11437 the user defined type vector. It isn't really a user defined type,
11438 but it behaves like one, with other DIE's using an AT_user_def_type
11439 attribute to reference it. */
11440
f792889a 11441static struct type *
e7c27a73 11442read_tag_string_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 11443{
e7c27a73 11444 struct objfile *objfile = cu->objfile;
3b7538c0 11445 struct gdbarch *gdbarch = get_objfile_arch (objfile);
c906108c
SS
11446 struct type *type, *range_type, *index_type, *char_type;
11447 struct attribute *attr;
11448 unsigned int length;
11449
e142c38c 11450 attr = dwarf2_attr (die, DW_AT_string_length, cu);
c906108c
SS
11451 if (attr)
11452 {
11453 length = DW_UNSND (attr);
11454 }
11455 else
11456 {
0963b4bd 11457 /* Check for the DW_AT_byte_size attribute. */
e142c38c 11458 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
b21b22e0
PS
11459 if (attr)
11460 {
11461 length = DW_UNSND (attr);
11462 }
11463 else
11464 {
11465 length = 1;
11466 }
c906108c 11467 }
6ccb9162 11468
46bf5051 11469 index_type = objfile_type (objfile)->builtin_int;
c906108c 11470 range_type = create_range_type (NULL, index_type, 1, length);
3b7538c0
UW
11471 char_type = language_string_char_type (cu->language_defn, gdbarch);
11472 type = create_string_type (NULL, char_type, range_type);
6ccb9162 11473
f792889a 11474 return set_die_type (die, type, cu);
c906108c
SS
11475}
11476
11477/* Handle DIES due to C code like:
11478
11479 struct foo
c5aa993b
JM
11480 {
11481 int (*funcp)(int a, long l);
11482 int b;
11483 };
c906108c 11484
0963b4bd 11485 ('funcp' generates a DW_TAG_subroutine_type DIE). */
c906108c 11486
f792889a 11487static struct type *
e7c27a73 11488read_subroutine_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 11489{
bb5ed363 11490 struct objfile *objfile = cu->objfile;
0963b4bd
MS
11491 struct type *type; /* Type that this function returns. */
11492 struct type *ftype; /* Function that returns above type. */
c906108c
SS
11493 struct attribute *attr;
11494
e7c27a73 11495 type = die_type (die, cu);
7e314c57
JK
11496
11497 /* The die_type call above may have already set the type for this DIE. */
11498 ftype = get_die_type (die, cu);
11499 if (ftype)
11500 return ftype;
11501
0c8b41f1 11502 ftype = lookup_function_type (type);
c906108c 11503
5b8101ae 11504 /* All functions in C++, Pascal and Java have prototypes. */
e142c38c 11505 attr = dwarf2_attr (die, DW_AT_prototyped, cu);
c906108c 11506 if ((attr && (DW_UNSND (attr) != 0))
987504bb 11507 || cu->language == language_cplus
5b8101ae
PM
11508 || cu->language == language_java
11509 || cu->language == language_pascal)
876cecd0 11510 TYPE_PROTOTYPED (ftype) = 1;
a6c727b2
DJ
11511 else if (producer_is_realview (cu->producer))
11512 /* RealView does not emit DW_AT_prototyped. We can not
11513 distinguish prototyped and unprototyped functions; default to
11514 prototyped, since that is more common in modern code (and
11515 RealView warns about unprototyped functions). */
11516 TYPE_PROTOTYPED (ftype) = 1;
c906108c 11517
c055b101
CV
11518 /* Store the calling convention in the type if it's available in
11519 the subroutine die. Otherwise set the calling convention to
11520 the default value DW_CC_normal. */
11521 attr = dwarf2_attr (die, DW_AT_calling_convention, cu);
54fcddd0
UW
11522 if (attr)
11523 TYPE_CALLING_CONVENTION (ftype) = DW_UNSND (attr);
11524 else if (cu->producer && strstr (cu->producer, "IBM XL C for OpenCL"))
11525 TYPE_CALLING_CONVENTION (ftype) = DW_CC_GDB_IBM_OpenCL;
11526 else
11527 TYPE_CALLING_CONVENTION (ftype) = DW_CC_normal;
76c10ea2
GM
11528
11529 /* We need to add the subroutine type to the die immediately so
11530 we don't infinitely recurse when dealing with parameters
0963b4bd 11531 declared as the same subroutine type. */
76c10ea2 11532 set_die_type (die, ftype, cu);
6e70227d 11533
639d11d3 11534 if (die->child != NULL)
c906108c 11535 {
bb5ed363 11536 struct type *void_type = objfile_type (objfile)->builtin_void;
c906108c 11537 struct die_info *child_die;
8072405b 11538 int nparams, iparams;
c906108c
SS
11539
11540 /* Count the number of parameters.
11541 FIXME: GDB currently ignores vararg functions, but knows about
11542 vararg member functions. */
8072405b 11543 nparams = 0;
639d11d3 11544 child_die = die->child;
c906108c
SS
11545 while (child_die && child_die->tag)
11546 {
11547 if (child_die->tag == DW_TAG_formal_parameter)
11548 nparams++;
11549 else if (child_die->tag == DW_TAG_unspecified_parameters)
876cecd0 11550 TYPE_VARARGS (ftype) = 1;
c906108c
SS
11551 child_die = sibling_die (child_die);
11552 }
11553
11554 /* Allocate storage for parameters and fill them in. */
11555 TYPE_NFIELDS (ftype) = nparams;
11556 TYPE_FIELDS (ftype) = (struct field *)
ae5a43e0 11557 TYPE_ZALLOC (ftype, nparams * sizeof (struct field));
c906108c 11558
8072405b
JK
11559 /* TYPE_FIELD_TYPE must never be NULL. Pre-fill the array to ensure it
11560 even if we error out during the parameters reading below. */
11561 for (iparams = 0; iparams < nparams; iparams++)
11562 TYPE_FIELD_TYPE (ftype, iparams) = void_type;
11563
11564 iparams = 0;
639d11d3 11565 child_die = die->child;
c906108c
SS
11566 while (child_die && child_die->tag)
11567 {
11568 if (child_die->tag == DW_TAG_formal_parameter)
11569 {
3ce3b1ba
PA
11570 struct type *arg_type;
11571
11572 /* DWARF version 2 has no clean way to discern C++
11573 static and non-static member functions. G++ helps
11574 GDB by marking the first parameter for non-static
11575 member functions (which is the this pointer) as
11576 artificial. We pass this information to
11577 dwarf2_add_member_fn via TYPE_FIELD_ARTIFICIAL.
11578
11579 DWARF version 3 added DW_AT_object_pointer, which GCC
11580 4.5 does not yet generate. */
e142c38c 11581 attr = dwarf2_attr (child_die, DW_AT_artificial, cu);
c906108c
SS
11582 if (attr)
11583 TYPE_FIELD_ARTIFICIAL (ftype, iparams) = DW_UNSND (attr);
11584 else
418835cc
KS
11585 {
11586 TYPE_FIELD_ARTIFICIAL (ftype, iparams) = 0;
11587
11588 /* GCC/43521: In java, the formal parameter
11589 "this" is sometimes not marked with DW_AT_artificial. */
11590 if (cu->language == language_java)
11591 {
11592 const char *name = dwarf2_name (child_die, cu);
9a619af0 11593
418835cc
KS
11594 if (name && !strcmp (name, "this"))
11595 TYPE_FIELD_ARTIFICIAL (ftype, iparams) = 1;
11596 }
11597 }
3ce3b1ba
PA
11598 arg_type = die_type (child_die, cu);
11599
11600 /* RealView does not mark THIS as const, which the testsuite
11601 expects. GCC marks THIS as const in method definitions,
11602 but not in the class specifications (GCC PR 43053). */
11603 if (cu->language == language_cplus && !TYPE_CONST (arg_type)
11604 && TYPE_FIELD_ARTIFICIAL (ftype, iparams))
11605 {
11606 int is_this = 0;
11607 struct dwarf2_cu *arg_cu = cu;
11608 const char *name = dwarf2_name (child_die, cu);
11609
11610 attr = dwarf2_attr (die, DW_AT_object_pointer, cu);
11611 if (attr)
11612 {
11613 /* If the compiler emits this, use it. */
11614 if (follow_die_ref (die, attr, &arg_cu) == child_die)
11615 is_this = 1;
11616 }
11617 else if (name && strcmp (name, "this") == 0)
11618 /* Function definitions will have the argument names. */
11619 is_this = 1;
11620 else if (name == NULL && iparams == 0)
11621 /* Declarations may not have the names, so like
11622 elsewhere in GDB, assume an artificial first
11623 argument is "this". */
11624 is_this = 1;
11625
11626 if (is_this)
11627 arg_type = make_cv_type (1, TYPE_VOLATILE (arg_type),
11628 arg_type, 0);
11629 }
11630
11631 TYPE_FIELD_TYPE (ftype, iparams) = arg_type;
c906108c
SS
11632 iparams++;
11633 }
11634 child_die = sibling_die (child_die);
11635 }
11636 }
11637
76c10ea2 11638 return ftype;
c906108c
SS
11639}
11640
f792889a 11641static struct type *
e7c27a73 11642read_typedef (struct die_info *die, struct dwarf2_cu *cu)
c906108c 11643{
e7c27a73 11644 struct objfile *objfile = cu->objfile;
0114d602 11645 const char *name = NULL;
3c8e0968 11646 struct type *this_type, *target_type;
c906108c 11647
94af9270 11648 name = dwarf2_full_name (NULL, die, cu);
f792889a 11649 this_type = init_type (TYPE_CODE_TYPEDEF, 0,
0114d602
DJ
11650 TYPE_FLAG_TARGET_STUB, NULL, objfile);
11651 TYPE_NAME (this_type) = (char *) name;
f792889a 11652 set_die_type (die, this_type, cu);
3c8e0968
DE
11653 target_type = die_type (die, cu);
11654 if (target_type != this_type)
11655 TYPE_TARGET_TYPE (this_type) = target_type;
11656 else
11657 {
11658 /* Self-referential typedefs are, it seems, not allowed by the DWARF
11659 spec and cause infinite loops in GDB. */
11660 complaint (&symfile_complaints,
11661 _("Self-referential DW_TAG_typedef "
11662 "- DIE at 0x%x [in module %s]"),
b64f50a1 11663 die->offset.sect_off, objfile->name);
3c8e0968
DE
11664 TYPE_TARGET_TYPE (this_type) = NULL;
11665 }
f792889a 11666 return this_type;
c906108c
SS
11667}
11668
11669/* Find a representation of a given base type and install
11670 it in the TYPE field of the die. */
11671
f792889a 11672static struct type *
e7c27a73 11673read_base_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 11674{
e7c27a73 11675 struct objfile *objfile = cu->objfile;
c906108c
SS
11676 struct type *type;
11677 struct attribute *attr;
11678 int encoding = 0, size = 0;
39cbfefa 11679 char *name;
6ccb9162
UW
11680 enum type_code code = TYPE_CODE_INT;
11681 int type_flags = 0;
11682 struct type *target_type = NULL;
c906108c 11683
e142c38c 11684 attr = dwarf2_attr (die, DW_AT_encoding, cu);
c906108c
SS
11685 if (attr)
11686 {
11687 encoding = DW_UNSND (attr);
11688 }
e142c38c 11689 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
c906108c
SS
11690 if (attr)
11691 {
11692 size = DW_UNSND (attr);
11693 }
39cbfefa 11694 name = dwarf2_name (die, cu);
6ccb9162 11695 if (!name)
c906108c 11696 {
6ccb9162
UW
11697 complaint (&symfile_complaints,
11698 _("DW_AT_name missing from DW_TAG_base_type"));
c906108c 11699 }
6ccb9162
UW
11700
11701 switch (encoding)
c906108c 11702 {
6ccb9162
UW
11703 case DW_ATE_address:
11704 /* Turn DW_ATE_address into a void * pointer. */
11705 code = TYPE_CODE_PTR;
11706 type_flags |= TYPE_FLAG_UNSIGNED;
11707 target_type = init_type (TYPE_CODE_VOID, 1, 0, NULL, objfile);
11708 break;
11709 case DW_ATE_boolean:
11710 code = TYPE_CODE_BOOL;
11711 type_flags |= TYPE_FLAG_UNSIGNED;
11712 break;
11713 case DW_ATE_complex_float:
11714 code = TYPE_CODE_COMPLEX;
11715 target_type = init_type (TYPE_CODE_FLT, size / 2, 0, NULL, objfile);
11716 break;
11717 case DW_ATE_decimal_float:
11718 code = TYPE_CODE_DECFLOAT;
11719 break;
11720 case DW_ATE_float:
11721 code = TYPE_CODE_FLT;
11722 break;
11723 case DW_ATE_signed:
11724 break;
11725 case DW_ATE_unsigned:
11726 type_flags |= TYPE_FLAG_UNSIGNED;
3b2b8fea
TT
11727 if (cu->language == language_fortran
11728 && name
11729 && strncmp (name, "character(", sizeof ("character(") - 1) == 0)
11730 code = TYPE_CODE_CHAR;
6ccb9162
UW
11731 break;
11732 case DW_ATE_signed_char:
6e70227d 11733 if (cu->language == language_ada || cu->language == language_m2
3b2b8fea
TT
11734 || cu->language == language_pascal
11735 || cu->language == language_fortran)
6ccb9162
UW
11736 code = TYPE_CODE_CHAR;
11737 break;
11738 case DW_ATE_unsigned_char:
868a0084 11739 if (cu->language == language_ada || cu->language == language_m2
3b2b8fea
TT
11740 || cu->language == language_pascal
11741 || cu->language == language_fortran)
6ccb9162
UW
11742 code = TYPE_CODE_CHAR;
11743 type_flags |= TYPE_FLAG_UNSIGNED;
11744 break;
75079b2b
TT
11745 case DW_ATE_UTF:
11746 /* We just treat this as an integer and then recognize the
11747 type by name elsewhere. */
11748 break;
11749
6ccb9162
UW
11750 default:
11751 complaint (&symfile_complaints, _("unsupported DW_AT_encoding: '%s'"),
11752 dwarf_type_encoding_name (encoding));
11753 break;
c906108c 11754 }
6ccb9162 11755
0114d602
DJ
11756 type = init_type (code, size, type_flags, NULL, objfile);
11757 TYPE_NAME (type) = name;
6ccb9162
UW
11758 TYPE_TARGET_TYPE (type) = target_type;
11759
0114d602 11760 if (name && strcmp (name, "char") == 0)
876cecd0 11761 TYPE_NOSIGN (type) = 1;
0114d602 11762
f792889a 11763 return set_die_type (die, type, cu);
c906108c
SS
11764}
11765
a02abb62
JB
11766/* Read the given DW_AT_subrange DIE. */
11767
f792889a 11768static struct type *
a02abb62
JB
11769read_subrange_type (struct die_info *die, struct dwarf2_cu *cu)
11770{
11771 struct type *base_type;
11772 struct type *range_type;
11773 struct attribute *attr;
4fae6e18
JK
11774 LONGEST low, high;
11775 int low_default_is_valid;
39cbfefa 11776 char *name;
43bbcdc2 11777 LONGEST negative_mask;
e77813c8 11778
a02abb62 11779 base_type = die_type (die, cu);
953ac07e
JK
11780 /* Preserve BASE_TYPE's original type, just set its LENGTH. */
11781 check_typedef (base_type);
a02abb62 11782
7e314c57
JK
11783 /* The die_type call above may have already set the type for this DIE. */
11784 range_type = get_die_type (die, cu);
11785 if (range_type)
11786 return range_type;
11787
4fae6e18
JK
11788 /* Set LOW_DEFAULT_IS_VALID if current language and DWARF version allow
11789 omitting DW_AT_lower_bound. */
11790 switch (cu->language)
6e70227d 11791 {
4fae6e18
JK
11792 case language_c:
11793 case language_cplus:
11794 low = 0;
11795 low_default_is_valid = 1;
11796 break;
11797 case language_fortran:
11798 low = 1;
11799 low_default_is_valid = 1;
11800 break;
11801 case language_d:
11802 case language_java:
11803 case language_objc:
11804 low = 0;
11805 low_default_is_valid = (cu->header.version >= 4);
11806 break;
11807 case language_ada:
11808 case language_m2:
11809 case language_pascal:
a02abb62 11810 low = 1;
4fae6e18
JK
11811 low_default_is_valid = (cu->header.version >= 4);
11812 break;
11813 default:
11814 low = 0;
11815 low_default_is_valid = 0;
11816 break;
a02abb62
JB
11817 }
11818
dd5e6932
DJ
11819 /* FIXME: For variable sized arrays either of these could be
11820 a variable rather than a constant value. We'll allow it,
11821 but we don't know how to handle it. */
e142c38c 11822 attr = dwarf2_attr (die, DW_AT_lower_bound, cu);
a02abb62 11823 if (attr)
4fae6e18
JK
11824 low = dwarf2_get_attr_constant_value (attr, low);
11825 else if (!low_default_is_valid)
11826 complaint (&symfile_complaints, _("Missing DW_AT_lower_bound "
11827 "- DIE at 0x%x [in module %s]"),
11828 die->offset.sect_off, cu->objfile->name);
a02abb62 11829
e142c38c 11830 attr = dwarf2_attr (die, DW_AT_upper_bound, cu);
a02abb62 11831 if (attr)
6e70227d 11832 {
d48323d8 11833 if (attr_form_is_block (attr) || is_ref_attr (attr))
a02abb62
JB
11834 {
11835 /* GCC encodes arrays with unspecified or dynamic length
e77813c8 11836 with a DW_FORM_block1 attribute or a reference attribute.
a02abb62
JB
11837 FIXME: GDB does not yet know how to handle dynamic
11838 arrays properly, treat them as arrays with unspecified
11839 length for now.
11840
11841 FIXME: jimb/2003-09-22: GDB does not really know
11842 how to handle arrays of unspecified length
11843 either; we just represent them as zero-length
11844 arrays. Choose an appropriate upper bound given
11845 the lower bound we've computed above. */
11846 high = low - 1;
11847 }
11848 else
11849 high = dwarf2_get_attr_constant_value (attr, 1);
11850 }
e77813c8
PM
11851 else
11852 {
11853 attr = dwarf2_attr (die, DW_AT_count, cu);
11854 if (attr)
11855 {
11856 int count = dwarf2_get_attr_constant_value (attr, 1);
11857 high = low + count - 1;
11858 }
c2ff108b
JK
11859 else
11860 {
11861 /* Unspecified array length. */
11862 high = low - 1;
11863 }
e77813c8
PM
11864 }
11865
11866 /* Dwarf-2 specifications explicitly allows to create subrange types
11867 without specifying a base type.
11868 In that case, the base type must be set to the type of
11869 the lower bound, upper bound or count, in that order, if any of these
11870 three attributes references an object that has a type.
11871 If no base type is found, the Dwarf-2 specifications say that
11872 a signed integer type of size equal to the size of an address should
11873 be used.
11874 For the following C code: `extern char gdb_int [];'
11875 GCC produces an empty range DIE.
11876 FIXME: muller/2010-05-28: Possible references to object for low bound,
0963b4bd 11877 high bound or count are not yet handled by this code. */
e77813c8
PM
11878 if (TYPE_CODE (base_type) == TYPE_CODE_VOID)
11879 {
11880 struct objfile *objfile = cu->objfile;
11881 struct gdbarch *gdbarch = get_objfile_arch (objfile);
11882 int addr_size = gdbarch_addr_bit (gdbarch) /8;
11883 struct type *int_type = objfile_type (objfile)->builtin_int;
11884
11885 /* Test "int", "long int", and "long long int" objfile types,
11886 and select the first one having a size above or equal to the
11887 architecture address size. */
11888 if (int_type && TYPE_LENGTH (int_type) >= addr_size)
11889 base_type = int_type;
11890 else
11891 {
11892 int_type = objfile_type (objfile)->builtin_long;
11893 if (int_type && TYPE_LENGTH (int_type) >= addr_size)
11894 base_type = int_type;
11895 else
11896 {
11897 int_type = objfile_type (objfile)->builtin_long_long;
11898 if (int_type && TYPE_LENGTH (int_type) >= addr_size)
11899 base_type = int_type;
11900 }
11901 }
11902 }
a02abb62 11903
6e70227d 11904 negative_mask =
43bbcdc2
PH
11905 (LONGEST) -1 << (TYPE_LENGTH (base_type) * TARGET_CHAR_BIT - 1);
11906 if (!TYPE_UNSIGNED (base_type) && (low & negative_mask))
11907 low |= negative_mask;
11908 if (!TYPE_UNSIGNED (base_type) && (high & negative_mask))
11909 high |= negative_mask;
11910
a02abb62
JB
11911 range_type = create_range_type (NULL, base_type, low, high);
11912
bbb0eef6
JK
11913 /* Mark arrays with dynamic length at least as an array of unspecified
11914 length. GDB could check the boundary but before it gets implemented at
11915 least allow accessing the array elements. */
d48323d8 11916 if (attr && attr_form_is_block (attr))
bbb0eef6
JK
11917 TYPE_HIGH_BOUND_UNDEFINED (range_type) = 1;
11918
c2ff108b
JK
11919 /* Ada expects an empty array on no boundary attributes. */
11920 if (attr == NULL && cu->language != language_ada)
11921 TYPE_HIGH_BOUND_UNDEFINED (range_type) = 1;
11922
39cbfefa
DJ
11923 name = dwarf2_name (die, cu);
11924 if (name)
11925 TYPE_NAME (range_type) = name;
6e70227d 11926
e142c38c 11927 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
a02abb62
JB
11928 if (attr)
11929 TYPE_LENGTH (range_type) = DW_UNSND (attr);
11930
7e314c57
JK
11931 set_die_type (die, range_type, cu);
11932
11933 /* set_die_type should be already done. */
b4ba55a1
JB
11934 set_descriptive_type (range_type, die, cu);
11935
7e314c57 11936 return range_type;
a02abb62 11937}
6e70227d 11938
f792889a 11939static struct type *
81a17f79
JB
11940read_unspecified_type (struct die_info *die, struct dwarf2_cu *cu)
11941{
11942 struct type *type;
81a17f79 11943
81a17f79
JB
11944 /* For now, we only support the C meaning of an unspecified type: void. */
11945
0114d602
DJ
11946 type = init_type (TYPE_CODE_VOID, 0, 0, NULL, cu->objfile);
11947 TYPE_NAME (type) = dwarf2_name (die, cu);
81a17f79 11948
f792889a 11949 return set_die_type (die, type, cu);
81a17f79 11950}
a02abb62 11951
639d11d3
DC
11952/* Read a single die and all its descendents. Set the die's sibling
11953 field to NULL; set other fields in the die correctly, and set all
11954 of the descendents' fields correctly. Set *NEW_INFO_PTR to the
11955 location of the info_ptr after reading all of those dies. PARENT
11956 is the parent of the die in question. */
11957
11958static struct die_info *
dee91e82
DE
11959read_die_and_children (const struct die_reader_specs *reader,
11960 gdb_byte *info_ptr,
11961 gdb_byte **new_info_ptr,
11962 struct die_info *parent)
639d11d3
DC
11963{
11964 struct die_info *die;
fe1b8b76 11965 gdb_byte *cur_ptr;
639d11d3
DC
11966 int has_children;
11967
93311388 11968 cur_ptr = read_full_die (reader, &die, info_ptr, &has_children);
1d325ec1
DJ
11969 if (die == NULL)
11970 {
11971 *new_info_ptr = cur_ptr;
11972 return NULL;
11973 }
93311388 11974 store_in_ref_table (die, reader->cu);
639d11d3
DC
11975
11976 if (has_children)
348e048f 11977 die->child = read_die_and_siblings (reader, cur_ptr, new_info_ptr, die);
639d11d3
DC
11978 else
11979 {
11980 die->child = NULL;
11981 *new_info_ptr = cur_ptr;
11982 }
11983
11984 die->sibling = NULL;
11985 die->parent = parent;
11986 return die;
11987}
11988
11989/* Read a die, all of its descendents, and all of its siblings; set
11990 all of the fields of all of the dies correctly. Arguments are as
11991 in read_die_and_children. */
11992
11993static struct die_info *
93311388
DE
11994read_die_and_siblings (const struct die_reader_specs *reader,
11995 gdb_byte *info_ptr,
fe1b8b76 11996 gdb_byte **new_info_ptr,
639d11d3
DC
11997 struct die_info *parent)
11998{
11999 struct die_info *first_die, *last_sibling;
fe1b8b76 12000 gdb_byte *cur_ptr;
639d11d3 12001
c906108c 12002 cur_ptr = info_ptr;
639d11d3
DC
12003 first_die = last_sibling = NULL;
12004
12005 while (1)
c906108c 12006 {
639d11d3 12007 struct die_info *die
dee91e82 12008 = read_die_and_children (reader, cur_ptr, &cur_ptr, parent);
639d11d3 12009
1d325ec1 12010 if (die == NULL)
c906108c 12011 {
639d11d3
DC
12012 *new_info_ptr = cur_ptr;
12013 return first_die;
c906108c 12014 }
1d325ec1
DJ
12015
12016 if (!first_die)
12017 first_die = die;
c906108c 12018 else
1d325ec1
DJ
12019 last_sibling->sibling = die;
12020
12021 last_sibling = die;
c906108c 12022 }
c906108c
SS
12023}
12024
3019eac3
DE
12025/* Read a die and all its attributes, leave space for NUM_EXTRA_ATTRS
12026 attributes.
12027 The caller is responsible for filling in the extra attributes
12028 and updating (*DIEP)->num_attrs.
12029 Set DIEP to point to a newly allocated die with its information,
12030 except for its child, sibling, and parent fields.
12031 Set HAS_CHILDREN to tell whether the die has children or not. */
93311388
DE
12032
12033static gdb_byte *
3019eac3
DE
12034read_full_die_1 (const struct die_reader_specs *reader,
12035 struct die_info **diep, gdb_byte *info_ptr,
12036 int *has_children, int num_extra_attrs)
93311388 12037{
b64f50a1
JK
12038 unsigned int abbrev_number, bytes_read, i;
12039 sect_offset offset;
93311388
DE
12040 struct abbrev_info *abbrev;
12041 struct die_info *die;
12042 struct dwarf2_cu *cu = reader->cu;
12043 bfd *abfd = reader->abfd;
12044
b64f50a1 12045 offset.sect_off = info_ptr - reader->buffer;
93311388
DE
12046 abbrev_number = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
12047 info_ptr += bytes_read;
12048 if (!abbrev_number)
12049 {
12050 *diep = NULL;
12051 *has_children = 0;
12052 return info_ptr;
12053 }
12054
433df2d4 12055 abbrev = abbrev_table_lookup_abbrev (cu->abbrev_table, abbrev_number);
93311388 12056 if (!abbrev)
348e048f
DE
12057 error (_("Dwarf Error: could not find abbrev number %d [in module %s]"),
12058 abbrev_number,
12059 bfd_get_filename (abfd));
12060
3019eac3 12061 die = dwarf_alloc_die (cu, abbrev->num_attrs + num_extra_attrs);
93311388
DE
12062 die->offset = offset;
12063 die->tag = abbrev->tag;
12064 die->abbrev = abbrev_number;
12065
3019eac3
DE
12066 /* Make the result usable.
12067 The caller needs to update num_attrs after adding the extra
12068 attributes. */
93311388
DE
12069 die->num_attrs = abbrev->num_attrs;
12070
12071 for (i = 0; i < abbrev->num_attrs; ++i)
dee91e82
DE
12072 info_ptr = read_attribute (reader, &die->attrs[i], &abbrev->attrs[i],
12073 info_ptr);
93311388
DE
12074
12075 *diep = die;
12076 *has_children = abbrev->has_children;
12077 return info_ptr;
12078}
12079
3019eac3
DE
12080/* Read a die and all its attributes.
12081 Set DIEP to point to a newly allocated die with its information,
12082 except for its child, sibling, and parent fields.
12083 Set HAS_CHILDREN to tell whether the die has children or not. */
12084
12085static gdb_byte *
12086read_full_die (const struct die_reader_specs *reader,
12087 struct die_info **diep, gdb_byte *info_ptr,
12088 int *has_children)
12089{
12090 return read_full_die_1 (reader, diep, info_ptr, has_children, 0);
12091}
433df2d4
DE
12092\f
12093/* Abbreviation tables.
3019eac3 12094
433df2d4 12095 In DWARF version 2, the description of the debugging information is
c906108c
SS
12096 stored in a separate .debug_abbrev section. Before we read any
12097 dies from a section we read in all abbreviations and install them
433df2d4
DE
12098 in a hash table. */
12099
12100/* Allocate space for a struct abbrev_info object in ABBREV_TABLE. */
12101
12102static struct abbrev_info *
12103abbrev_table_alloc_abbrev (struct abbrev_table *abbrev_table)
12104{
12105 struct abbrev_info *abbrev;
12106
12107 abbrev = (struct abbrev_info *)
12108 obstack_alloc (&abbrev_table->abbrev_obstack, sizeof (struct abbrev_info));
12109 memset (abbrev, 0, sizeof (struct abbrev_info));
12110 return abbrev;
12111}
12112
12113/* Add an abbreviation to the table. */
c906108c
SS
12114
12115static void
433df2d4
DE
12116abbrev_table_add_abbrev (struct abbrev_table *abbrev_table,
12117 unsigned int abbrev_number,
12118 struct abbrev_info *abbrev)
12119{
12120 unsigned int hash_number;
12121
12122 hash_number = abbrev_number % ABBREV_HASH_SIZE;
12123 abbrev->next = abbrev_table->abbrevs[hash_number];
12124 abbrev_table->abbrevs[hash_number] = abbrev;
12125}
dee91e82 12126
433df2d4
DE
12127/* Look up an abbrev in the table.
12128 Returns NULL if the abbrev is not found. */
12129
12130static struct abbrev_info *
12131abbrev_table_lookup_abbrev (const struct abbrev_table *abbrev_table,
12132 unsigned int abbrev_number)
c906108c 12133{
433df2d4
DE
12134 unsigned int hash_number;
12135 struct abbrev_info *abbrev;
12136
12137 hash_number = abbrev_number % ABBREV_HASH_SIZE;
12138 abbrev = abbrev_table->abbrevs[hash_number];
12139
12140 while (abbrev)
12141 {
12142 if (abbrev->number == abbrev_number)
12143 return abbrev;
12144 abbrev = abbrev->next;
12145 }
12146 return NULL;
12147}
12148
12149/* Read in an abbrev table. */
12150
12151static struct abbrev_table *
12152abbrev_table_read_table (struct dwarf2_section_info *section,
12153 sect_offset offset)
12154{
12155 struct objfile *objfile = dwarf2_per_objfile->objfile;
12156 bfd *abfd = section->asection->owner;
12157 struct abbrev_table *abbrev_table;
fe1b8b76 12158 gdb_byte *abbrev_ptr;
c906108c
SS
12159 struct abbrev_info *cur_abbrev;
12160 unsigned int abbrev_number, bytes_read, abbrev_name;
433df2d4 12161 unsigned int abbrev_form;
f3dd6933
DJ
12162 struct attr_abbrev *cur_attrs;
12163 unsigned int allocated_attrs;
c906108c 12164
433df2d4 12165 abbrev_table = XMALLOC (struct abbrev_table);
f4dc4d17 12166 abbrev_table->offset = offset;
433df2d4
DE
12167 obstack_init (&abbrev_table->abbrev_obstack);
12168 abbrev_table->abbrevs = obstack_alloc (&abbrev_table->abbrev_obstack,
12169 (ABBREV_HASH_SIZE
12170 * sizeof (struct abbrev_info *)));
12171 memset (abbrev_table->abbrevs, 0,
12172 ABBREV_HASH_SIZE * sizeof (struct abbrev_info *));
c906108c 12173
433df2d4
DE
12174 dwarf2_read_section (objfile, section);
12175 abbrev_ptr = section->buffer + offset.sect_off;
c906108c
SS
12176 abbrev_number = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
12177 abbrev_ptr += bytes_read;
12178
f3dd6933
DJ
12179 allocated_attrs = ATTR_ALLOC_CHUNK;
12180 cur_attrs = xmalloc (allocated_attrs * sizeof (struct attr_abbrev));
6e70227d 12181
0963b4bd 12182 /* Loop until we reach an abbrev number of 0. */
c906108c
SS
12183 while (abbrev_number)
12184 {
433df2d4 12185 cur_abbrev = abbrev_table_alloc_abbrev (abbrev_table);
c906108c
SS
12186
12187 /* read in abbrev header */
12188 cur_abbrev->number = abbrev_number;
12189 cur_abbrev->tag = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
12190 abbrev_ptr += bytes_read;
12191 cur_abbrev->has_children = read_1_byte (abfd, abbrev_ptr);
12192 abbrev_ptr += 1;
12193
12194 /* now read in declarations */
12195 abbrev_name = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
12196 abbrev_ptr += bytes_read;
12197 abbrev_form = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
12198 abbrev_ptr += bytes_read;
12199 while (abbrev_name)
12200 {
f3dd6933 12201 if (cur_abbrev->num_attrs == allocated_attrs)
c906108c 12202 {
f3dd6933
DJ
12203 allocated_attrs += ATTR_ALLOC_CHUNK;
12204 cur_attrs
12205 = xrealloc (cur_attrs, (allocated_attrs
12206 * sizeof (struct attr_abbrev)));
c906108c 12207 }
ae038cb0 12208
f3dd6933
DJ
12209 cur_attrs[cur_abbrev->num_attrs].name = abbrev_name;
12210 cur_attrs[cur_abbrev->num_attrs++].form = abbrev_form;
c906108c
SS
12211 abbrev_name = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
12212 abbrev_ptr += bytes_read;
12213 abbrev_form = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
12214 abbrev_ptr += bytes_read;
12215 }
12216
433df2d4 12217 cur_abbrev->attrs = obstack_alloc (&abbrev_table->abbrev_obstack,
f3dd6933
DJ
12218 (cur_abbrev->num_attrs
12219 * sizeof (struct attr_abbrev)));
12220 memcpy (cur_abbrev->attrs, cur_attrs,
12221 cur_abbrev->num_attrs * sizeof (struct attr_abbrev));
12222
433df2d4 12223 abbrev_table_add_abbrev (abbrev_table, abbrev_number, cur_abbrev);
c906108c
SS
12224
12225 /* Get next abbreviation.
12226 Under Irix6 the abbreviations for a compilation unit are not
c5aa993b
JM
12227 always properly terminated with an abbrev number of 0.
12228 Exit loop if we encounter an abbreviation which we have
12229 already read (which means we are about to read the abbreviations
12230 for the next compile unit) or if the end of the abbreviation
12231 table is reached. */
433df2d4 12232 if ((unsigned int) (abbrev_ptr - section->buffer) >= section->size)
c906108c
SS
12233 break;
12234 abbrev_number = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
12235 abbrev_ptr += bytes_read;
433df2d4 12236 if (abbrev_table_lookup_abbrev (abbrev_table, abbrev_number) != NULL)
c906108c
SS
12237 break;
12238 }
f3dd6933
DJ
12239
12240 xfree (cur_attrs);
433df2d4 12241 return abbrev_table;
c906108c
SS
12242}
12243
433df2d4 12244/* Free the resources held by ABBREV_TABLE. */
c906108c 12245
c906108c 12246static void
433df2d4 12247abbrev_table_free (struct abbrev_table *abbrev_table)
c906108c 12248{
433df2d4
DE
12249 obstack_free (&abbrev_table->abbrev_obstack, NULL);
12250 xfree (abbrev_table);
c906108c
SS
12251}
12252
f4dc4d17
DE
12253/* Same as abbrev_table_free but as a cleanup.
12254 We pass in a pointer to the pointer to the table so that we can
12255 set the pointer to NULL when we're done. It also simplifies
12256 build_type_unit_groups. */
12257
12258static void
12259abbrev_table_free_cleanup (void *table_ptr)
12260{
12261 struct abbrev_table **abbrev_table_ptr = table_ptr;
12262
12263 if (*abbrev_table_ptr != NULL)
12264 abbrev_table_free (*abbrev_table_ptr);
12265 *abbrev_table_ptr = NULL;
12266}
12267
433df2d4
DE
12268/* Read the abbrev table for CU from ABBREV_SECTION. */
12269
12270static void
12271dwarf2_read_abbrevs (struct dwarf2_cu *cu,
12272 struct dwarf2_section_info *abbrev_section)
c906108c 12273{
433df2d4
DE
12274 cu->abbrev_table =
12275 abbrev_table_read_table (abbrev_section, cu->header.abbrev_offset);
12276}
c906108c 12277
433df2d4 12278/* Release the memory used by the abbrev table for a compilation unit. */
c906108c 12279
433df2d4
DE
12280static void
12281dwarf2_free_abbrev_table (void *ptr_to_cu)
12282{
12283 struct dwarf2_cu *cu = ptr_to_cu;
c906108c 12284
433df2d4
DE
12285 abbrev_table_free (cu->abbrev_table);
12286 /* Set this to NULL so that we SEGV if we try to read it later,
12287 and also because free_comp_unit verifies this is NULL. */
12288 cu->abbrev_table = NULL;
12289}
12290\f
72bf9492
DJ
12291/* Returns nonzero if TAG represents a type that we might generate a partial
12292 symbol for. */
12293
12294static int
12295is_type_tag_for_partial (int tag)
12296{
12297 switch (tag)
12298 {
12299#if 0
12300 /* Some types that would be reasonable to generate partial symbols for,
12301 that we don't at present. */
12302 case DW_TAG_array_type:
12303 case DW_TAG_file_type:
12304 case DW_TAG_ptr_to_member_type:
12305 case DW_TAG_set_type:
12306 case DW_TAG_string_type:
12307 case DW_TAG_subroutine_type:
12308#endif
12309 case DW_TAG_base_type:
12310 case DW_TAG_class_type:
680b30c7 12311 case DW_TAG_interface_type:
72bf9492
DJ
12312 case DW_TAG_enumeration_type:
12313 case DW_TAG_structure_type:
12314 case DW_TAG_subrange_type:
12315 case DW_TAG_typedef:
12316 case DW_TAG_union_type:
12317 return 1;
12318 default:
12319 return 0;
12320 }
12321}
12322
12323/* Load all DIEs that are interesting for partial symbols into memory. */
12324
12325static struct partial_die_info *
dee91e82
DE
12326load_partial_dies (const struct die_reader_specs *reader,
12327 gdb_byte *info_ptr, int building_psymtab)
72bf9492 12328{
dee91e82 12329 struct dwarf2_cu *cu = reader->cu;
bb5ed363 12330 struct objfile *objfile = cu->objfile;
72bf9492
DJ
12331 struct partial_die_info *part_die;
12332 struct partial_die_info *parent_die, *last_die, *first_die = NULL;
12333 struct abbrev_info *abbrev;
12334 unsigned int bytes_read;
5afb4e99 12335 unsigned int load_all = 0;
72bf9492
DJ
12336 int nesting_level = 1;
12337
12338 parent_die = NULL;
12339 last_die = NULL;
12340
7adf1e79
DE
12341 gdb_assert (cu->per_cu != NULL);
12342 if (cu->per_cu->load_all_dies)
5afb4e99
DJ
12343 load_all = 1;
12344
72bf9492
DJ
12345 cu->partial_dies
12346 = htab_create_alloc_ex (cu->header.length / 12,
12347 partial_die_hash,
12348 partial_die_eq,
12349 NULL,
12350 &cu->comp_unit_obstack,
12351 hashtab_obstack_allocate,
12352 dummy_obstack_deallocate);
12353
12354 part_die = obstack_alloc (&cu->comp_unit_obstack,
12355 sizeof (struct partial_die_info));
12356
12357 while (1)
12358 {
12359 abbrev = peek_die_abbrev (info_ptr, &bytes_read, cu);
12360
12361 /* A NULL abbrev means the end of a series of children. */
12362 if (abbrev == NULL)
12363 {
12364 if (--nesting_level == 0)
12365 {
12366 /* PART_DIE was probably the last thing allocated on the
12367 comp_unit_obstack, so we could call obstack_free
12368 here. We don't do that because the waste is small,
12369 and will be cleaned up when we're done with this
12370 compilation unit. This way, we're also more robust
12371 against other users of the comp_unit_obstack. */
12372 return first_die;
12373 }
12374 info_ptr += bytes_read;
12375 last_die = parent_die;
12376 parent_die = parent_die->die_parent;
12377 continue;
12378 }
12379
98bfdba5
PA
12380 /* Check for template arguments. We never save these; if
12381 they're seen, we just mark the parent, and go on our way. */
12382 if (parent_die != NULL
12383 && cu->language == language_cplus
12384 && (abbrev->tag == DW_TAG_template_type_param
12385 || abbrev->tag == DW_TAG_template_value_param))
12386 {
12387 parent_die->has_template_arguments = 1;
12388
12389 if (!load_all)
12390 {
12391 /* We don't need a partial DIE for the template argument. */
dee91e82 12392 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
98bfdba5
PA
12393 continue;
12394 }
12395 }
12396
0d99eb77 12397 /* We only recurse into c++ subprograms looking for template arguments.
98bfdba5
PA
12398 Skip their other children. */
12399 if (!load_all
12400 && cu->language == language_cplus
12401 && parent_die != NULL
12402 && parent_die->tag == DW_TAG_subprogram)
12403 {
dee91e82 12404 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
98bfdba5
PA
12405 continue;
12406 }
12407
5afb4e99
DJ
12408 /* Check whether this DIE is interesting enough to save. Normally
12409 we would not be interested in members here, but there may be
12410 later variables referencing them via DW_AT_specification (for
12411 static members). */
12412 if (!load_all
12413 && !is_type_tag_for_partial (abbrev->tag)
72929c62 12414 && abbrev->tag != DW_TAG_constant
72bf9492
DJ
12415 && abbrev->tag != DW_TAG_enumerator
12416 && abbrev->tag != DW_TAG_subprogram
bc30ff58 12417 && abbrev->tag != DW_TAG_lexical_block
72bf9492 12418 && abbrev->tag != DW_TAG_variable
5afb4e99 12419 && abbrev->tag != DW_TAG_namespace
f55ee35c 12420 && abbrev->tag != DW_TAG_module
95554aad
TT
12421 && abbrev->tag != DW_TAG_member
12422 && abbrev->tag != DW_TAG_imported_unit)
72bf9492
DJ
12423 {
12424 /* Otherwise we skip to the next sibling, if any. */
dee91e82 12425 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
72bf9492
DJ
12426 continue;
12427 }
12428
dee91e82
DE
12429 info_ptr = read_partial_die (reader, part_die, abbrev, bytes_read,
12430 info_ptr);
72bf9492
DJ
12431
12432 /* This two-pass algorithm for processing partial symbols has a
12433 high cost in cache pressure. Thus, handle some simple cases
12434 here which cover the majority of C partial symbols. DIEs
12435 which neither have specification tags in them, nor could have
12436 specification tags elsewhere pointing at them, can simply be
12437 processed and discarded.
12438
12439 This segment is also optional; scan_partial_symbols and
12440 add_partial_symbol will handle these DIEs if we chain
12441 them in normally. When compilers which do not emit large
12442 quantities of duplicate debug information are more common,
12443 this code can probably be removed. */
12444
12445 /* Any complete simple types at the top level (pretty much all
12446 of them, for a language without namespaces), can be processed
12447 directly. */
12448 if (parent_die == NULL
12449 && part_die->has_specification == 0
12450 && part_die->is_declaration == 0
d8228535 12451 && ((part_die->tag == DW_TAG_typedef && !part_die->has_children)
72bf9492
DJ
12452 || part_die->tag == DW_TAG_base_type
12453 || part_die->tag == DW_TAG_subrange_type))
12454 {
12455 if (building_psymtab && part_die->name != NULL)
04a679b8 12456 add_psymbol_to_list (part_die->name, strlen (part_die->name), 0,
72bf9492 12457 VAR_DOMAIN, LOC_TYPEDEF,
bb5ed363
DE
12458 &objfile->static_psymbols,
12459 0, (CORE_ADDR) 0, cu->language, objfile);
dee91e82 12460 info_ptr = locate_pdi_sibling (reader, part_die, info_ptr);
72bf9492
DJ
12461 continue;
12462 }
12463
d8228535
JK
12464 /* The exception for DW_TAG_typedef with has_children above is
12465 a workaround of GCC PR debug/47510. In the case of this complaint
12466 type_name_no_tag_or_error will error on such types later.
12467
12468 GDB skipped children of DW_TAG_typedef by the shortcut above and then
12469 it could not find the child DIEs referenced later, this is checked
12470 above. In correct DWARF DW_TAG_typedef should have no children. */
12471
12472 if (part_die->tag == DW_TAG_typedef && part_die->has_children)
12473 complaint (&symfile_complaints,
12474 _("DW_TAG_typedef has childen - GCC PR debug/47510 bug "
12475 "- DIE at 0x%x [in module %s]"),
b64f50a1 12476 part_die->offset.sect_off, objfile->name);
d8228535 12477
72bf9492
DJ
12478 /* If we're at the second level, and we're an enumerator, and
12479 our parent has no specification (meaning possibly lives in a
12480 namespace elsewhere), then we can add the partial symbol now
12481 instead of queueing it. */
12482 if (part_die->tag == DW_TAG_enumerator
12483 && parent_die != NULL
12484 && parent_die->die_parent == NULL
12485 && parent_die->tag == DW_TAG_enumeration_type
12486 && parent_die->has_specification == 0)
12487 {
12488 if (part_die->name == NULL)
3e43a32a
MS
12489 complaint (&symfile_complaints,
12490 _("malformed enumerator DIE ignored"));
72bf9492 12491 else if (building_psymtab)
04a679b8 12492 add_psymbol_to_list (part_die->name, strlen (part_die->name), 0,
72bf9492 12493 VAR_DOMAIN, LOC_CONST,
987504bb
JJ
12494 (cu->language == language_cplus
12495 || cu->language == language_java)
bb5ed363
DE
12496 ? &objfile->global_psymbols
12497 : &objfile->static_psymbols,
12498 0, (CORE_ADDR) 0, cu->language, objfile);
72bf9492 12499
dee91e82 12500 info_ptr = locate_pdi_sibling (reader, part_die, info_ptr);
72bf9492
DJ
12501 continue;
12502 }
12503
12504 /* We'll save this DIE so link it in. */
12505 part_die->die_parent = parent_die;
12506 part_die->die_sibling = NULL;
12507 part_die->die_child = NULL;
12508
12509 if (last_die && last_die == parent_die)
12510 last_die->die_child = part_die;
12511 else if (last_die)
12512 last_die->die_sibling = part_die;
12513
12514 last_die = part_die;
12515
12516 if (first_die == NULL)
12517 first_die = part_die;
12518
12519 /* Maybe add the DIE to the hash table. Not all DIEs that we
12520 find interesting need to be in the hash table, because we
12521 also have the parent/sibling/child chains; only those that we
12522 might refer to by offset later during partial symbol reading.
12523
12524 For now this means things that might have be the target of a
12525 DW_AT_specification, DW_AT_abstract_origin, or
12526 DW_AT_extension. DW_AT_extension will refer only to
12527 namespaces; DW_AT_abstract_origin refers to functions (and
12528 many things under the function DIE, but we do not recurse
12529 into function DIEs during partial symbol reading) and
12530 possibly variables as well; DW_AT_specification refers to
12531 declarations. Declarations ought to have the DW_AT_declaration
12532 flag. It happens that GCC forgets to put it in sometimes, but
12533 only for functions, not for types.
12534
12535 Adding more things than necessary to the hash table is harmless
12536 except for the performance cost. Adding too few will result in
5afb4e99
DJ
12537 wasted time in find_partial_die, when we reread the compilation
12538 unit with load_all_dies set. */
72bf9492 12539
5afb4e99 12540 if (load_all
72929c62 12541 || abbrev->tag == DW_TAG_constant
5afb4e99 12542 || abbrev->tag == DW_TAG_subprogram
72bf9492
DJ
12543 || abbrev->tag == DW_TAG_variable
12544 || abbrev->tag == DW_TAG_namespace
12545 || part_die->is_declaration)
12546 {
12547 void **slot;
12548
12549 slot = htab_find_slot_with_hash (cu->partial_dies, part_die,
b64f50a1 12550 part_die->offset.sect_off, INSERT);
72bf9492
DJ
12551 *slot = part_die;
12552 }
12553
12554 part_die = obstack_alloc (&cu->comp_unit_obstack,
12555 sizeof (struct partial_die_info));
12556
12557 /* For some DIEs we want to follow their children (if any). For C
bc30ff58 12558 we have no reason to follow the children of structures; for other
98bfdba5
PA
12559 languages we have to, so that we can get at method physnames
12560 to infer fully qualified class names, for DW_AT_specification,
12561 and for C++ template arguments. For C++, we also look one level
12562 inside functions to find template arguments (if the name of the
12563 function does not already contain the template arguments).
bc30ff58
JB
12564
12565 For Ada, we need to scan the children of subprograms and lexical
12566 blocks as well because Ada allows the definition of nested
12567 entities that could be interesting for the debugger, such as
12568 nested subprograms for instance. */
72bf9492 12569 if (last_die->has_children
5afb4e99
DJ
12570 && (load_all
12571 || last_die->tag == DW_TAG_namespace
f55ee35c 12572 || last_die->tag == DW_TAG_module
72bf9492 12573 || last_die->tag == DW_TAG_enumeration_type
98bfdba5
PA
12574 || (cu->language == language_cplus
12575 && last_die->tag == DW_TAG_subprogram
12576 && (last_die->name == NULL
12577 || strchr (last_die->name, '<') == NULL))
72bf9492
DJ
12578 || (cu->language != language_c
12579 && (last_die->tag == DW_TAG_class_type
680b30c7 12580 || last_die->tag == DW_TAG_interface_type
72bf9492 12581 || last_die->tag == DW_TAG_structure_type
bc30ff58
JB
12582 || last_die->tag == DW_TAG_union_type))
12583 || (cu->language == language_ada
12584 && (last_die->tag == DW_TAG_subprogram
12585 || last_die->tag == DW_TAG_lexical_block))))
72bf9492
DJ
12586 {
12587 nesting_level++;
12588 parent_die = last_die;
12589 continue;
12590 }
12591
12592 /* Otherwise we skip to the next sibling, if any. */
dee91e82 12593 info_ptr = locate_pdi_sibling (reader, last_die, info_ptr);
72bf9492
DJ
12594
12595 /* Back to the top, do it again. */
12596 }
12597}
12598
c906108c
SS
12599/* Read a minimal amount of information into the minimal die structure. */
12600
fe1b8b76 12601static gdb_byte *
dee91e82
DE
12602read_partial_die (const struct die_reader_specs *reader,
12603 struct partial_die_info *part_die,
12604 struct abbrev_info *abbrev, unsigned int abbrev_len,
12605 gdb_byte *info_ptr)
c906108c 12606{
dee91e82 12607 struct dwarf2_cu *cu = reader->cu;
bb5ed363 12608 struct objfile *objfile = cu->objfile;
dee91e82 12609 gdb_byte *buffer = reader->buffer;
fa238c03 12610 unsigned int i;
c906108c 12611 struct attribute attr;
c5aa993b 12612 int has_low_pc_attr = 0;
c906108c 12613 int has_high_pc_attr = 0;
91da1414 12614 int high_pc_relative = 0;
c906108c 12615
72bf9492 12616 memset (part_die, 0, sizeof (struct partial_die_info));
c906108c 12617
b64f50a1 12618 part_die->offset.sect_off = info_ptr - buffer;
72bf9492
DJ
12619
12620 info_ptr += abbrev_len;
12621
12622 if (abbrev == NULL)
12623 return info_ptr;
12624
c906108c
SS
12625 part_die->tag = abbrev->tag;
12626 part_die->has_children = abbrev->has_children;
c906108c
SS
12627
12628 for (i = 0; i < abbrev->num_attrs; ++i)
12629 {
dee91e82 12630 info_ptr = read_attribute (reader, &attr, &abbrev->attrs[i], info_ptr);
c906108c
SS
12631
12632 /* Store the data if it is of an attribute we want to keep in a
c5aa993b 12633 partial symbol table. */
c906108c
SS
12634 switch (attr.name)
12635 {
12636 case DW_AT_name:
71c25dea
TT
12637 switch (part_die->tag)
12638 {
12639 case DW_TAG_compile_unit:
95554aad 12640 case DW_TAG_partial_unit:
348e048f 12641 case DW_TAG_type_unit:
71c25dea
TT
12642 /* Compilation units have a DW_AT_name that is a filename, not
12643 a source language identifier. */
12644 case DW_TAG_enumeration_type:
12645 case DW_TAG_enumerator:
12646 /* These tags always have simple identifiers already; no need
12647 to canonicalize them. */
12648 part_die->name = DW_STRING (&attr);
12649 break;
12650 default:
12651 part_die->name
12652 = dwarf2_canonicalize_name (DW_STRING (&attr), cu,
bb5ed363 12653 &objfile->objfile_obstack);
71c25dea
TT
12654 break;
12655 }
c906108c 12656 break;
31ef98ae 12657 case DW_AT_linkage_name:
c906108c 12658 case DW_AT_MIPS_linkage_name:
31ef98ae
TT
12659 /* Note that both forms of linkage name might appear. We
12660 assume they will be the same, and we only store the last
12661 one we see. */
94af9270
KS
12662 if (cu->language == language_ada)
12663 part_die->name = DW_STRING (&attr);
abc72ce4 12664 part_die->linkage_name = DW_STRING (&attr);
c906108c
SS
12665 break;
12666 case DW_AT_low_pc:
12667 has_low_pc_attr = 1;
12668 part_die->lowpc = DW_ADDR (&attr);
12669 break;
12670 case DW_AT_high_pc:
12671 has_high_pc_attr = 1;
3019eac3
DE
12672 if (attr.form == DW_FORM_addr
12673 || attr.form == DW_FORM_GNU_addr_index)
91da1414
MW
12674 part_die->highpc = DW_ADDR (&attr);
12675 else
12676 {
12677 high_pc_relative = 1;
12678 part_die->highpc = DW_UNSND (&attr);
12679 }
c906108c
SS
12680 break;
12681 case DW_AT_location:
0963b4bd 12682 /* Support the .debug_loc offsets. */
8e19ed76
PS
12683 if (attr_form_is_block (&attr))
12684 {
95554aad 12685 part_die->d.locdesc = DW_BLOCK (&attr);
8e19ed76 12686 }
3690dd37 12687 else if (attr_form_is_section_offset (&attr))
8e19ed76 12688 {
4d3c2250 12689 dwarf2_complex_location_expr_complaint ();
8e19ed76
PS
12690 }
12691 else
12692 {
4d3c2250
KB
12693 dwarf2_invalid_attrib_class_complaint ("DW_AT_location",
12694 "partial symbol information");
8e19ed76 12695 }
c906108c 12696 break;
c906108c
SS
12697 case DW_AT_external:
12698 part_die->is_external = DW_UNSND (&attr);
12699 break;
12700 case DW_AT_declaration:
12701 part_die->is_declaration = DW_UNSND (&attr);
12702 break;
12703 case DW_AT_type:
12704 part_die->has_type = 1;
12705 break;
12706 case DW_AT_abstract_origin:
12707 case DW_AT_specification:
72bf9492
DJ
12708 case DW_AT_extension:
12709 part_die->has_specification = 1;
c764a876 12710 part_die->spec_offset = dwarf2_get_ref_die_offset (&attr);
36586728
TT
12711 part_die->spec_is_dwz = (attr.form == DW_FORM_GNU_ref_alt
12712 || cu->per_cu->is_dwz);
c906108c
SS
12713 break;
12714 case DW_AT_sibling:
12715 /* Ignore absolute siblings, they might point outside of
12716 the current compile unit. */
12717 if (attr.form == DW_FORM_ref_addr)
3e43a32a
MS
12718 complaint (&symfile_complaints,
12719 _("ignoring absolute DW_AT_sibling"));
c906108c 12720 else
b64f50a1 12721 part_die->sibling = buffer + dwarf2_get_ref_die_offset (&attr).sect_off;
c906108c 12722 break;
fa4028e9
JB
12723 case DW_AT_byte_size:
12724 part_die->has_byte_size = 1;
12725 break;
68511cec
CES
12726 case DW_AT_calling_convention:
12727 /* DWARF doesn't provide a way to identify a program's source-level
12728 entry point. DW_AT_calling_convention attributes are only meant
12729 to describe functions' calling conventions.
12730
12731 However, because it's a necessary piece of information in
12732 Fortran, and because DW_CC_program is the only piece of debugging
12733 information whose definition refers to a 'main program' at all,
12734 several compilers have begun marking Fortran main programs with
12735 DW_CC_program --- even when those functions use the standard
12736 calling conventions.
12737
12738 So until DWARF specifies a way to provide this information and
12739 compilers pick up the new representation, we'll support this
12740 practice. */
12741 if (DW_UNSND (&attr) == DW_CC_program
12742 && cu->language == language_fortran)
01f8c46d
JK
12743 {
12744 set_main_name (part_die->name);
12745
12746 /* As this DIE has a static linkage the name would be difficult
12747 to look up later. */
12748 language_of_main = language_fortran;
12749 }
68511cec 12750 break;
481860b3
GB
12751 case DW_AT_inline:
12752 if (DW_UNSND (&attr) == DW_INL_inlined
12753 || DW_UNSND (&attr) == DW_INL_declared_inlined)
12754 part_die->may_be_inlined = 1;
12755 break;
95554aad
TT
12756
12757 case DW_AT_import:
12758 if (part_die->tag == DW_TAG_imported_unit)
36586728
TT
12759 {
12760 part_die->d.offset = dwarf2_get_ref_die_offset (&attr);
12761 part_die->is_dwz = (attr.form == DW_FORM_GNU_ref_alt
12762 || cu->per_cu->is_dwz);
12763 }
95554aad
TT
12764 break;
12765
c906108c
SS
12766 default:
12767 break;
12768 }
12769 }
12770
91da1414
MW
12771 if (high_pc_relative)
12772 part_die->highpc += part_die->lowpc;
12773
9373cf26
JK
12774 if (has_low_pc_attr && has_high_pc_attr)
12775 {
12776 /* When using the GNU linker, .gnu.linkonce. sections are used to
12777 eliminate duplicate copies of functions and vtables and such.
12778 The linker will arbitrarily choose one and discard the others.
12779 The AT_*_pc values for such functions refer to local labels in
12780 these sections. If the section from that file was discarded, the
12781 labels are not in the output, so the relocs get a value of 0.
12782 If this is a discarded function, mark the pc bounds as invalid,
12783 so that GDB will ignore it. */
12784 if (part_die->lowpc == 0 && !dwarf2_per_objfile->has_section_at_zero)
12785 {
bb5ed363 12786 struct gdbarch *gdbarch = get_objfile_arch (objfile);
9373cf26
JK
12787
12788 complaint (&symfile_complaints,
12789 _("DW_AT_low_pc %s is zero "
12790 "for DIE at 0x%x [in module %s]"),
12791 paddress (gdbarch, part_die->lowpc),
b64f50a1 12792 part_die->offset.sect_off, objfile->name);
9373cf26
JK
12793 }
12794 /* dwarf2_get_pc_bounds has also the strict low < high requirement. */
12795 else if (part_die->lowpc >= part_die->highpc)
12796 {
bb5ed363 12797 struct gdbarch *gdbarch = get_objfile_arch (objfile);
9373cf26
JK
12798
12799 complaint (&symfile_complaints,
12800 _("DW_AT_low_pc %s is not < DW_AT_high_pc %s "
12801 "for DIE at 0x%x [in module %s]"),
12802 paddress (gdbarch, part_die->lowpc),
12803 paddress (gdbarch, part_die->highpc),
b64f50a1 12804 part_die->offset.sect_off, objfile->name);
9373cf26
JK
12805 }
12806 else
12807 part_die->has_pc_info = 1;
12808 }
85cbf3d3 12809
c906108c
SS
12810 return info_ptr;
12811}
12812
72bf9492
DJ
12813/* Find a cached partial DIE at OFFSET in CU. */
12814
12815static struct partial_die_info *
b64f50a1 12816find_partial_die_in_comp_unit (sect_offset offset, struct dwarf2_cu *cu)
72bf9492
DJ
12817{
12818 struct partial_die_info *lookup_die = NULL;
12819 struct partial_die_info part_die;
12820
12821 part_die.offset = offset;
b64f50a1
JK
12822 lookup_die = htab_find_with_hash (cu->partial_dies, &part_die,
12823 offset.sect_off);
72bf9492 12824
72bf9492
DJ
12825 return lookup_die;
12826}
12827
348e048f
DE
12828/* Find a partial DIE at OFFSET, which may or may not be in CU,
12829 except in the case of .debug_types DIEs which do not reference
12830 outside their CU (they do however referencing other types via
55f1336d 12831 DW_FORM_ref_sig8). */
72bf9492
DJ
12832
12833static struct partial_die_info *
36586728 12834find_partial_die (sect_offset offset, int offset_in_dwz, struct dwarf2_cu *cu)
72bf9492 12835{
bb5ed363 12836 struct objfile *objfile = cu->objfile;
5afb4e99
DJ
12837 struct dwarf2_per_cu_data *per_cu = NULL;
12838 struct partial_die_info *pd = NULL;
72bf9492 12839
36586728
TT
12840 if (offset_in_dwz == cu->per_cu->is_dwz
12841 && offset_in_cu_p (&cu->header, offset))
5afb4e99
DJ
12842 {
12843 pd = find_partial_die_in_comp_unit (offset, cu);
12844 if (pd != NULL)
12845 return pd;
0d99eb77
DE
12846 /* We missed recording what we needed.
12847 Load all dies and try again. */
12848 per_cu = cu->per_cu;
5afb4e99 12849 }
0d99eb77
DE
12850 else
12851 {
12852 /* TUs don't reference other CUs/TUs (except via type signatures). */
3019eac3 12853 if (cu->per_cu->is_debug_types)
0d99eb77
DE
12854 {
12855 error (_("Dwarf Error: Type Unit at offset 0x%lx contains"
12856 " external reference to offset 0x%lx [in module %s].\n"),
12857 (long) cu->header.offset.sect_off, (long) offset.sect_off,
12858 bfd_get_filename (objfile->obfd));
12859 }
36586728
TT
12860 per_cu = dwarf2_find_containing_comp_unit (offset, offset_in_dwz,
12861 objfile);
72bf9492 12862
0d99eb77
DE
12863 if (per_cu->cu == NULL || per_cu->cu->partial_dies == NULL)
12864 load_partial_comp_unit (per_cu);
ae038cb0 12865
0d99eb77
DE
12866 per_cu->cu->last_used = 0;
12867 pd = find_partial_die_in_comp_unit (offset, per_cu->cu);
12868 }
5afb4e99 12869
dee91e82
DE
12870 /* If we didn't find it, and not all dies have been loaded,
12871 load them all and try again. */
12872
5afb4e99
DJ
12873 if (pd == NULL && per_cu->load_all_dies == 0)
12874 {
5afb4e99 12875 per_cu->load_all_dies = 1;
fd820528
DE
12876
12877 /* This is nasty. When we reread the DIEs, somewhere up the call chain
12878 THIS_CU->cu may already be in use. So we can't just free it and
12879 replace its DIEs with the ones we read in. Instead, we leave those
12880 DIEs alone (which can still be in use, e.g. in scan_partial_symbols),
12881 and clobber THIS_CU->cu->partial_dies with the hash table for the new
12882 set. */
dee91e82 12883 load_partial_comp_unit (per_cu);
5afb4e99
DJ
12884
12885 pd = find_partial_die_in_comp_unit (offset, per_cu->cu);
12886 }
12887
12888 if (pd == NULL)
12889 internal_error (__FILE__, __LINE__,
3e43a32a
MS
12890 _("could not find partial DIE 0x%x "
12891 "in cache [from module %s]\n"),
b64f50a1 12892 offset.sect_off, bfd_get_filename (objfile->obfd));
5afb4e99 12893 return pd;
72bf9492
DJ
12894}
12895
abc72ce4
DE
12896/* See if we can figure out if the class lives in a namespace. We do
12897 this by looking for a member function; its demangled name will
12898 contain namespace info, if there is any. */
12899
12900static void
12901guess_partial_die_structure_name (struct partial_die_info *struct_pdi,
12902 struct dwarf2_cu *cu)
12903{
12904 /* NOTE: carlton/2003-10-07: Getting the info this way changes
12905 what template types look like, because the demangler
12906 frequently doesn't give the same name as the debug info. We
12907 could fix this by only using the demangled name to get the
12908 prefix (but see comment in read_structure_type). */
12909
12910 struct partial_die_info *real_pdi;
12911 struct partial_die_info *child_pdi;
12912
12913 /* If this DIE (this DIE's specification, if any) has a parent, then
12914 we should not do this. We'll prepend the parent's fully qualified
12915 name when we create the partial symbol. */
12916
12917 real_pdi = struct_pdi;
12918 while (real_pdi->has_specification)
36586728
TT
12919 real_pdi = find_partial_die (real_pdi->spec_offset,
12920 real_pdi->spec_is_dwz, cu);
abc72ce4
DE
12921
12922 if (real_pdi->die_parent != NULL)
12923 return;
12924
12925 for (child_pdi = struct_pdi->die_child;
12926 child_pdi != NULL;
12927 child_pdi = child_pdi->die_sibling)
12928 {
12929 if (child_pdi->tag == DW_TAG_subprogram
12930 && child_pdi->linkage_name != NULL)
12931 {
12932 char *actual_class_name
12933 = language_class_name_from_physname (cu->language_defn,
12934 child_pdi->linkage_name);
12935 if (actual_class_name != NULL)
12936 {
12937 struct_pdi->name
12938 = obsavestring (actual_class_name,
12939 strlen (actual_class_name),
12940 &cu->objfile->objfile_obstack);
12941 xfree (actual_class_name);
12942 }
12943 break;
12944 }
12945 }
12946}
12947
72bf9492
DJ
12948/* Adjust PART_DIE before generating a symbol for it. This function
12949 may set the is_external flag or change the DIE's name. */
12950
12951static void
12952fixup_partial_die (struct partial_die_info *part_die,
12953 struct dwarf2_cu *cu)
12954{
abc72ce4
DE
12955 /* Once we've fixed up a die, there's no point in doing so again.
12956 This also avoids a memory leak if we were to call
12957 guess_partial_die_structure_name multiple times. */
12958 if (part_die->fixup_called)
12959 return;
12960
72bf9492
DJ
12961 /* If we found a reference attribute and the DIE has no name, try
12962 to find a name in the referred to DIE. */
12963
12964 if (part_die->name == NULL && part_die->has_specification)
12965 {
12966 struct partial_die_info *spec_die;
72bf9492 12967
36586728
TT
12968 spec_die = find_partial_die (part_die->spec_offset,
12969 part_die->spec_is_dwz, cu);
72bf9492 12970
10b3939b 12971 fixup_partial_die (spec_die, cu);
72bf9492
DJ
12972
12973 if (spec_die->name)
12974 {
12975 part_die->name = spec_die->name;
12976
12977 /* Copy DW_AT_external attribute if it is set. */
12978 if (spec_die->is_external)
12979 part_die->is_external = spec_die->is_external;
12980 }
12981 }
12982
12983 /* Set default names for some unnamed DIEs. */
72bf9492
DJ
12984
12985 if (part_die->name == NULL && part_die->tag == DW_TAG_namespace)
2b1dbab0 12986 part_die->name = CP_ANONYMOUS_NAMESPACE_STR;
72bf9492 12987
abc72ce4
DE
12988 /* If there is no parent die to provide a namespace, and there are
12989 children, see if we can determine the namespace from their linkage
122d1940 12990 name. */
abc72ce4 12991 if (cu->language == language_cplus
8b70b953 12992 && !VEC_empty (dwarf2_section_info_def, dwarf2_per_objfile->types)
abc72ce4
DE
12993 && part_die->die_parent == NULL
12994 && part_die->has_children
12995 && (part_die->tag == DW_TAG_class_type
12996 || part_die->tag == DW_TAG_structure_type
12997 || part_die->tag == DW_TAG_union_type))
12998 guess_partial_die_structure_name (part_die, cu);
12999
53832f31
TT
13000 /* GCC might emit a nameless struct or union that has a linkage
13001 name. See http://gcc.gnu.org/bugzilla/show_bug.cgi?id=47510. */
13002 if (part_die->name == NULL
96408a79
SA
13003 && (part_die->tag == DW_TAG_class_type
13004 || part_die->tag == DW_TAG_interface_type
13005 || part_die->tag == DW_TAG_structure_type
13006 || part_die->tag == DW_TAG_union_type)
53832f31
TT
13007 && part_die->linkage_name != NULL)
13008 {
13009 char *demangled;
13010
13011 demangled = cplus_demangle (part_die->linkage_name, DMGL_TYPES);
13012 if (demangled)
13013 {
96408a79
SA
13014 const char *base;
13015
13016 /* Strip any leading namespaces/classes, keep only the base name.
13017 DW_AT_name for named DIEs does not contain the prefixes. */
13018 base = strrchr (demangled, ':');
13019 if (base && base > demangled && base[-1] == ':')
13020 base++;
13021 else
13022 base = demangled;
13023
13024 part_die->name = obsavestring (base, strlen (base),
53832f31
TT
13025 &cu->objfile->objfile_obstack);
13026 xfree (demangled);
13027 }
13028 }
13029
abc72ce4 13030 part_die->fixup_called = 1;
72bf9492
DJ
13031}
13032
a8329558 13033/* Read an attribute value described by an attribute form. */
c906108c 13034
fe1b8b76 13035static gdb_byte *
dee91e82
DE
13036read_attribute_value (const struct die_reader_specs *reader,
13037 struct attribute *attr, unsigned form,
13038 gdb_byte *info_ptr)
c906108c 13039{
dee91e82
DE
13040 struct dwarf2_cu *cu = reader->cu;
13041 bfd *abfd = reader->abfd;
e7c27a73 13042 struct comp_unit_head *cu_header = &cu->header;
c906108c
SS
13043 unsigned int bytes_read;
13044 struct dwarf_block *blk;
13045
a8329558
KW
13046 attr->form = form;
13047 switch (form)
c906108c 13048 {
c906108c 13049 case DW_FORM_ref_addr:
ae411497 13050 if (cu->header.version == 2)
4568ecf9 13051 DW_UNSND (attr) = read_address (abfd, info_ptr, cu, &bytes_read);
ae411497 13052 else
4568ecf9
DE
13053 DW_UNSND (attr) = read_offset (abfd, info_ptr,
13054 &cu->header, &bytes_read);
ae411497
TT
13055 info_ptr += bytes_read;
13056 break;
36586728
TT
13057 case DW_FORM_GNU_ref_alt:
13058 DW_UNSND (attr) = read_offset (abfd, info_ptr, &cu->header, &bytes_read);
13059 info_ptr += bytes_read;
13060 break;
ae411497 13061 case DW_FORM_addr:
e7c27a73 13062 DW_ADDR (attr) = read_address (abfd, info_ptr, cu, &bytes_read);
107d2387 13063 info_ptr += bytes_read;
c906108c
SS
13064 break;
13065 case DW_FORM_block2:
7b5a2f43 13066 blk = dwarf_alloc_block (cu);
c906108c
SS
13067 blk->size = read_2_bytes (abfd, info_ptr);
13068 info_ptr += 2;
13069 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
13070 info_ptr += blk->size;
13071 DW_BLOCK (attr) = blk;
13072 break;
13073 case DW_FORM_block4:
7b5a2f43 13074 blk = dwarf_alloc_block (cu);
c906108c
SS
13075 blk->size = read_4_bytes (abfd, info_ptr);
13076 info_ptr += 4;
13077 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
13078 info_ptr += blk->size;
13079 DW_BLOCK (attr) = blk;
13080 break;
13081 case DW_FORM_data2:
13082 DW_UNSND (attr) = read_2_bytes (abfd, info_ptr);
13083 info_ptr += 2;
13084 break;
13085 case DW_FORM_data4:
13086 DW_UNSND (attr) = read_4_bytes (abfd, info_ptr);
13087 info_ptr += 4;
13088 break;
13089 case DW_FORM_data8:
13090 DW_UNSND (attr) = read_8_bytes (abfd, info_ptr);
13091 info_ptr += 8;
13092 break;
2dc7f7b3
TT
13093 case DW_FORM_sec_offset:
13094 DW_UNSND (attr) = read_offset (abfd, info_ptr, &cu->header, &bytes_read);
13095 info_ptr += bytes_read;
13096 break;
c906108c 13097 case DW_FORM_string:
9b1c24c8 13098 DW_STRING (attr) = read_direct_string (abfd, info_ptr, &bytes_read);
8285870a 13099 DW_STRING_IS_CANONICAL (attr) = 0;
c906108c
SS
13100 info_ptr += bytes_read;
13101 break;
4bdf3d34 13102 case DW_FORM_strp:
36586728
TT
13103 if (!cu->per_cu->is_dwz)
13104 {
13105 DW_STRING (attr) = read_indirect_string (abfd, info_ptr, cu_header,
13106 &bytes_read);
13107 DW_STRING_IS_CANONICAL (attr) = 0;
13108 info_ptr += bytes_read;
13109 break;
13110 }
13111 /* FALLTHROUGH */
13112 case DW_FORM_GNU_strp_alt:
13113 {
13114 struct dwz_file *dwz = dwarf2_get_dwz_file ();
13115 LONGEST str_offset = read_offset (abfd, info_ptr, cu_header,
13116 &bytes_read);
13117
13118 DW_STRING (attr) = read_indirect_string_from_dwz (dwz, str_offset);
13119 DW_STRING_IS_CANONICAL (attr) = 0;
13120 info_ptr += bytes_read;
13121 }
4bdf3d34 13122 break;
2dc7f7b3 13123 case DW_FORM_exprloc:
c906108c 13124 case DW_FORM_block:
7b5a2f43 13125 blk = dwarf_alloc_block (cu);
c906108c
SS
13126 blk->size = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
13127 info_ptr += bytes_read;
13128 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
13129 info_ptr += blk->size;
13130 DW_BLOCK (attr) = blk;
13131 break;
13132 case DW_FORM_block1:
7b5a2f43 13133 blk = dwarf_alloc_block (cu);
c906108c
SS
13134 blk->size = read_1_byte (abfd, info_ptr);
13135 info_ptr += 1;
13136 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
13137 info_ptr += blk->size;
13138 DW_BLOCK (attr) = blk;
13139 break;
13140 case DW_FORM_data1:
13141 DW_UNSND (attr) = read_1_byte (abfd, info_ptr);
13142 info_ptr += 1;
13143 break;
13144 case DW_FORM_flag:
13145 DW_UNSND (attr) = read_1_byte (abfd, info_ptr);
13146 info_ptr += 1;
13147 break;
2dc7f7b3
TT
13148 case DW_FORM_flag_present:
13149 DW_UNSND (attr) = 1;
13150 break;
c906108c
SS
13151 case DW_FORM_sdata:
13152 DW_SND (attr) = read_signed_leb128 (abfd, info_ptr, &bytes_read);
13153 info_ptr += bytes_read;
13154 break;
13155 case DW_FORM_udata:
13156 DW_UNSND (attr) = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
13157 info_ptr += bytes_read;
13158 break;
13159 case DW_FORM_ref1:
4568ecf9
DE
13160 DW_UNSND (attr) = (cu->header.offset.sect_off
13161 + read_1_byte (abfd, info_ptr));
c906108c
SS
13162 info_ptr += 1;
13163 break;
13164 case DW_FORM_ref2:
4568ecf9
DE
13165 DW_UNSND (attr) = (cu->header.offset.sect_off
13166 + read_2_bytes (abfd, info_ptr));
c906108c
SS
13167 info_ptr += 2;
13168 break;
13169 case DW_FORM_ref4:
4568ecf9
DE
13170 DW_UNSND (attr) = (cu->header.offset.sect_off
13171 + read_4_bytes (abfd, info_ptr));
c906108c
SS
13172 info_ptr += 4;
13173 break;
613e1657 13174 case DW_FORM_ref8:
4568ecf9
DE
13175 DW_UNSND (attr) = (cu->header.offset.sect_off
13176 + read_8_bytes (abfd, info_ptr));
613e1657
KB
13177 info_ptr += 8;
13178 break;
55f1336d 13179 case DW_FORM_ref_sig8:
348e048f
DE
13180 /* Convert the signature to something we can record in DW_UNSND
13181 for later lookup.
13182 NOTE: This is NULL if the type wasn't found. */
13183 DW_SIGNATURED_TYPE (attr) =
e319fa28 13184 lookup_signatured_type (read_8_bytes (abfd, info_ptr));
348e048f
DE
13185 info_ptr += 8;
13186 break;
c906108c 13187 case DW_FORM_ref_udata:
4568ecf9
DE
13188 DW_UNSND (attr) = (cu->header.offset.sect_off
13189 + read_unsigned_leb128 (abfd, info_ptr, &bytes_read));
c906108c
SS
13190 info_ptr += bytes_read;
13191 break;
c906108c 13192 case DW_FORM_indirect:
a8329558
KW
13193 form = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
13194 info_ptr += bytes_read;
dee91e82 13195 info_ptr = read_attribute_value (reader, attr, form, info_ptr);
a8329558 13196 break;
3019eac3
DE
13197 case DW_FORM_GNU_addr_index:
13198 if (reader->dwo_file == NULL)
13199 {
13200 /* For now flag a hard error.
13201 Later we can turn this into a complaint. */
13202 error (_("Dwarf Error: %s found in non-DWO CU [in module %s]"),
13203 dwarf_form_name (form),
13204 bfd_get_filename (abfd));
13205 }
13206 DW_ADDR (attr) = read_addr_index_from_leb128 (cu, info_ptr, &bytes_read);
13207 info_ptr += bytes_read;
13208 break;
13209 case DW_FORM_GNU_str_index:
13210 if (reader->dwo_file == NULL)
13211 {
13212 /* For now flag a hard error.
13213 Later we can turn this into a complaint if warranted. */
13214 error (_("Dwarf Error: %s found in non-DWO CU [in module %s]"),
13215 dwarf_form_name (form),
13216 bfd_get_filename (abfd));
13217 }
13218 {
13219 ULONGEST str_index =
13220 read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
13221
13222 DW_STRING (attr) = read_str_index (reader, cu, str_index);
13223 DW_STRING_IS_CANONICAL (attr) = 0;
13224 info_ptr += bytes_read;
13225 }
13226 break;
c906108c 13227 default:
8a3fe4f8 13228 error (_("Dwarf Error: Cannot handle %s in DWARF reader [in module %s]"),
659b0389
ML
13229 dwarf_form_name (form),
13230 bfd_get_filename (abfd));
c906108c 13231 }
28e94949 13232
36586728
TT
13233 /* Super hack. */
13234 if (cu->per_cu->is_dwz && is_ref_attr (attr))
13235 attr->form = DW_FORM_GNU_ref_alt;
13236
28e94949
JB
13237 /* We have seen instances where the compiler tried to emit a byte
13238 size attribute of -1 which ended up being encoded as an unsigned
13239 0xffffffff. Although 0xffffffff is technically a valid size value,
13240 an object of this size seems pretty unlikely so we can relatively
13241 safely treat these cases as if the size attribute was invalid and
13242 treat them as zero by default. */
13243 if (attr->name == DW_AT_byte_size
13244 && form == DW_FORM_data4
13245 && DW_UNSND (attr) >= 0xffffffff)
01c66ae6
JB
13246 {
13247 complaint
13248 (&symfile_complaints,
43bbcdc2
PH
13249 _("Suspicious DW_AT_byte_size value treated as zero instead of %s"),
13250 hex_string (DW_UNSND (attr)));
01c66ae6
JB
13251 DW_UNSND (attr) = 0;
13252 }
28e94949 13253
c906108c
SS
13254 return info_ptr;
13255}
13256
a8329558
KW
13257/* Read an attribute described by an abbreviated attribute. */
13258
fe1b8b76 13259static gdb_byte *
dee91e82
DE
13260read_attribute (const struct die_reader_specs *reader,
13261 struct attribute *attr, struct attr_abbrev *abbrev,
13262 gdb_byte *info_ptr)
a8329558
KW
13263{
13264 attr->name = abbrev->name;
dee91e82 13265 return read_attribute_value (reader, attr, abbrev->form, info_ptr);
a8329558
KW
13266}
13267
0963b4bd 13268/* Read dwarf information from a buffer. */
c906108c
SS
13269
13270static unsigned int
fe1b8b76 13271read_1_byte (bfd *abfd, gdb_byte *buf)
c906108c 13272{
fe1b8b76 13273 return bfd_get_8 (abfd, buf);
c906108c
SS
13274}
13275
13276static int
fe1b8b76 13277read_1_signed_byte (bfd *abfd, gdb_byte *buf)
c906108c 13278{
fe1b8b76 13279 return bfd_get_signed_8 (abfd, buf);
c906108c
SS
13280}
13281
13282static unsigned int
fe1b8b76 13283read_2_bytes (bfd *abfd, gdb_byte *buf)
c906108c 13284{
fe1b8b76 13285 return bfd_get_16 (abfd, buf);
c906108c
SS
13286}
13287
21ae7a4d
JK
13288static int
13289read_2_signed_bytes (bfd *abfd, gdb_byte *buf)
13290{
13291 return bfd_get_signed_16 (abfd, buf);
13292}
13293
c906108c 13294static unsigned int
fe1b8b76 13295read_4_bytes (bfd *abfd, gdb_byte *buf)
c906108c 13296{
fe1b8b76 13297 return bfd_get_32 (abfd, buf);
c906108c
SS
13298}
13299
21ae7a4d
JK
13300static int
13301read_4_signed_bytes (bfd *abfd, gdb_byte *buf)
13302{
13303 return bfd_get_signed_32 (abfd, buf);
13304}
13305
93311388 13306static ULONGEST
fe1b8b76 13307read_8_bytes (bfd *abfd, gdb_byte *buf)
c906108c 13308{
fe1b8b76 13309 return bfd_get_64 (abfd, buf);
c906108c
SS
13310}
13311
13312static CORE_ADDR
fe1b8b76 13313read_address (bfd *abfd, gdb_byte *buf, struct dwarf2_cu *cu,
891d2f0b 13314 unsigned int *bytes_read)
c906108c 13315{
e7c27a73 13316 struct comp_unit_head *cu_header = &cu->header;
c906108c
SS
13317 CORE_ADDR retval = 0;
13318
107d2387 13319 if (cu_header->signed_addr_p)
c906108c 13320 {
107d2387
AC
13321 switch (cu_header->addr_size)
13322 {
13323 case 2:
fe1b8b76 13324 retval = bfd_get_signed_16 (abfd, buf);
107d2387
AC
13325 break;
13326 case 4:
fe1b8b76 13327 retval = bfd_get_signed_32 (abfd, buf);
107d2387
AC
13328 break;
13329 case 8:
fe1b8b76 13330 retval = bfd_get_signed_64 (abfd, buf);
107d2387
AC
13331 break;
13332 default:
8e65ff28 13333 internal_error (__FILE__, __LINE__,
e2e0b3e5 13334 _("read_address: bad switch, signed [in module %s]"),
659b0389 13335 bfd_get_filename (abfd));
107d2387
AC
13336 }
13337 }
13338 else
13339 {
13340 switch (cu_header->addr_size)
13341 {
13342 case 2:
fe1b8b76 13343 retval = bfd_get_16 (abfd, buf);
107d2387
AC
13344 break;
13345 case 4:
fe1b8b76 13346 retval = bfd_get_32 (abfd, buf);
107d2387
AC
13347 break;
13348 case 8:
fe1b8b76 13349 retval = bfd_get_64 (abfd, buf);
107d2387
AC
13350 break;
13351 default:
8e65ff28 13352 internal_error (__FILE__, __LINE__,
a73c6dcd
MS
13353 _("read_address: bad switch, "
13354 "unsigned [in module %s]"),
659b0389 13355 bfd_get_filename (abfd));
107d2387 13356 }
c906108c 13357 }
64367e0a 13358
107d2387
AC
13359 *bytes_read = cu_header->addr_size;
13360 return retval;
c906108c
SS
13361}
13362
f7ef9339 13363/* Read the initial length from a section. The (draft) DWARF 3
613e1657
KB
13364 specification allows the initial length to take up either 4 bytes
13365 or 12 bytes. If the first 4 bytes are 0xffffffff, then the next 8
13366 bytes describe the length and all offsets will be 8 bytes in length
13367 instead of 4.
13368
f7ef9339
KB
13369 An older, non-standard 64-bit format is also handled by this
13370 function. The older format in question stores the initial length
13371 as an 8-byte quantity without an escape value. Lengths greater
13372 than 2^32 aren't very common which means that the initial 4 bytes
13373 is almost always zero. Since a length value of zero doesn't make
13374 sense for the 32-bit format, this initial zero can be considered to
13375 be an escape value which indicates the presence of the older 64-bit
13376 format. As written, the code can't detect (old format) lengths
917c78fc
MK
13377 greater than 4GB. If it becomes necessary to handle lengths
13378 somewhat larger than 4GB, we could allow other small values (such
13379 as the non-sensical values of 1, 2, and 3) to also be used as
13380 escape values indicating the presence of the old format.
f7ef9339 13381
917c78fc
MK
13382 The value returned via bytes_read should be used to increment the
13383 relevant pointer after calling read_initial_length().
c764a876 13384
613e1657
KB
13385 [ Note: read_initial_length() and read_offset() are based on the
13386 document entitled "DWARF Debugging Information Format", revision
f7ef9339 13387 3, draft 8, dated November 19, 2001. This document was obtained
613e1657
KB
13388 from:
13389
f7ef9339 13390 http://reality.sgiweb.org/davea/dwarf3-draft8-011125.pdf
6e70227d 13391
613e1657
KB
13392 This document is only a draft and is subject to change. (So beware.)
13393
f7ef9339 13394 Details regarding the older, non-standard 64-bit format were
917c78fc
MK
13395 determined empirically by examining 64-bit ELF files produced by
13396 the SGI toolchain on an IRIX 6.5 machine.
f7ef9339
KB
13397
13398 - Kevin, July 16, 2002
613e1657
KB
13399 ] */
13400
13401static LONGEST
c764a876 13402read_initial_length (bfd *abfd, gdb_byte *buf, unsigned int *bytes_read)
613e1657 13403{
fe1b8b76 13404 LONGEST length = bfd_get_32 (abfd, buf);
613e1657 13405
dd373385 13406 if (length == 0xffffffff)
613e1657 13407 {
fe1b8b76 13408 length = bfd_get_64 (abfd, buf + 4);
613e1657 13409 *bytes_read = 12;
613e1657 13410 }
dd373385 13411 else if (length == 0)
f7ef9339 13412 {
dd373385 13413 /* Handle the (non-standard) 64-bit DWARF2 format used by IRIX. */
fe1b8b76 13414 length = bfd_get_64 (abfd, buf);
f7ef9339 13415 *bytes_read = 8;
f7ef9339 13416 }
613e1657
KB
13417 else
13418 {
13419 *bytes_read = 4;
613e1657
KB
13420 }
13421
c764a876
DE
13422 return length;
13423}
dd373385 13424
c764a876
DE
13425/* Cover function for read_initial_length.
13426 Returns the length of the object at BUF, and stores the size of the
13427 initial length in *BYTES_READ and stores the size that offsets will be in
13428 *OFFSET_SIZE.
13429 If the initial length size is not equivalent to that specified in
13430 CU_HEADER then issue a complaint.
13431 This is useful when reading non-comp-unit headers. */
dd373385 13432
c764a876
DE
13433static LONGEST
13434read_checked_initial_length_and_offset (bfd *abfd, gdb_byte *buf,
13435 const struct comp_unit_head *cu_header,
13436 unsigned int *bytes_read,
13437 unsigned int *offset_size)
13438{
13439 LONGEST length = read_initial_length (abfd, buf, bytes_read);
13440
13441 gdb_assert (cu_header->initial_length_size == 4
13442 || cu_header->initial_length_size == 8
13443 || cu_header->initial_length_size == 12);
13444
13445 if (cu_header->initial_length_size != *bytes_read)
13446 complaint (&symfile_complaints,
13447 _("intermixed 32-bit and 64-bit DWARF sections"));
dd373385 13448
c764a876 13449 *offset_size = (*bytes_read == 4) ? 4 : 8;
dd373385 13450 return length;
613e1657
KB
13451}
13452
13453/* Read an offset from the data stream. The size of the offset is
917c78fc 13454 given by cu_header->offset_size. */
613e1657
KB
13455
13456static LONGEST
fe1b8b76 13457read_offset (bfd *abfd, gdb_byte *buf, const struct comp_unit_head *cu_header,
891d2f0b 13458 unsigned int *bytes_read)
c764a876
DE
13459{
13460 LONGEST offset = read_offset_1 (abfd, buf, cu_header->offset_size);
9a619af0 13461
c764a876
DE
13462 *bytes_read = cu_header->offset_size;
13463 return offset;
13464}
13465
13466/* Read an offset from the data stream. */
13467
13468static LONGEST
13469read_offset_1 (bfd *abfd, gdb_byte *buf, unsigned int offset_size)
613e1657
KB
13470{
13471 LONGEST retval = 0;
13472
c764a876 13473 switch (offset_size)
613e1657
KB
13474 {
13475 case 4:
fe1b8b76 13476 retval = bfd_get_32 (abfd, buf);
613e1657
KB
13477 break;
13478 case 8:
fe1b8b76 13479 retval = bfd_get_64 (abfd, buf);
613e1657
KB
13480 break;
13481 default:
8e65ff28 13482 internal_error (__FILE__, __LINE__,
c764a876 13483 _("read_offset_1: bad switch [in module %s]"),
659b0389 13484 bfd_get_filename (abfd));
613e1657
KB
13485 }
13486
917c78fc 13487 return retval;
613e1657
KB
13488}
13489
fe1b8b76
JB
13490static gdb_byte *
13491read_n_bytes (bfd *abfd, gdb_byte *buf, unsigned int size)
c906108c
SS
13492{
13493 /* If the size of a host char is 8 bits, we can return a pointer
13494 to the buffer, otherwise we have to copy the data to a buffer
13495 allocated on the temporary obstack. */
4bdf3d34 13496 gdb_assert (HOST_CHAR_BIT == 8);
c906108c 13497 return buf;
c906108c
SS
13498}
13499
13500static char *
9b1c24c8 13501read_direct_string (bfd *abfd, gdb_byte *buf, unsigned int *bytes_read_ptr)
c906108c
SS
13502{
13503 /* If the size of a host char is 8 bits, we can return a pointer
13504 to the string, otherwise we have to copy the string to a buffer
13505 allocated on the temporary obstack. */
4bdf3d34 13506 gdb_assert (HOST_CHAR_BIT == 8);
c906108c
SS
13507 if (*buf == '\0')
13508 {
13509 *bytes_read_ptr = 1;
13510 return NULL;
13511 }
fe1b8b76
JB
13512 *bytes_read_ptr = strlen ((char *) buf) + 1;
13513 return (char *) buf;
4bdf3d34
JJ
13514}
13515
13516static char *
cf2c3c16 13517read_indirect_string_at_offset (bfd *abfd, LONGEST str_offset)
4bdf3d34 13518{
be391dca 13519 dwarf2_read_section (dwarf2_per_objfile->objfile, &dwarf2_per_objfile->str);
dce234bc 13520 if (dwarf2_per_objfile->str.buffer == NULL)
cf2c3c16
TT
13521 error (_("DW_FORM_strp used without .debug_str section [in module %s]"),
13522 bfd_get_filename (abfd));
dce234bc 13523 if (str_offset >= dwarf2_per_objfile->str.size)
cf2c3c16
TT
13524 error (_("DW_FORM_strp pointing outside of "
13525 ".debug_str section [in module %s]"),
13526 bfd_get_filename (abfd));
4bdf3d34 13527 gdb_assert (HOST_CHAR_BIT == 8);
dce234bc 13528 if (dwarf2_per_objfile->str.buffer[str_offset] == '\0')
4bdf3d34 13529 return NULL;
dce234bc 13530 return (char *) (dwarf2_per_objfile->str.buffer + str_offset);
c906108c
SS
13531}
13532
36586728
TT
13533/* Read a string at offset STR_OFFSET in the .debug_str section from
13534 the .dwz file DWZ. Throw an error if the offset is too large. If
13535 the string consists of a single NUL byte, return NULL; otherwise
13536 return a pointer to the string. */
13537
13538static char *
13539read_indirect_string_from_dwz (struct dwz_file *dwz, LONGEST str_offset)
13540{
13541 dwarf2_read_section (dwarf2_per_objfile->objfile, &dwz->str);
13542
13543 if (dwz->str.buffer == NULL)
13544 error (_("DW_FORM_GNU_strp_alt used without .debug_str "
13545 "section [in module %s]"),
13546 bfd_get_filename (dwz->dwz_bfd));
13547 if (str_offset >= dwz->str.size)
13548 error (_("DW_FORM_GNU_strp_alt pointing outside of "
13549 ".debug_str section [in module %s]"),
13550 bfd_get_filename (dwz->dwz_bfd));
13551 gdb_assert (HOST_CHAR_BIT == 8);
13552 if (dwz->str.buffer[str_offset] == '\0')
13553 return NULL;
13554 return (char *) (dwz->str.buffer + str_offset);
13555}
13556
cf2c3c16
TT
13557static char *
13558read_indirect_string (bfd *abfd, gdb_byte *buf,
13559 const struct comp_unit_head *cu_header,
13560 unsigned int *bytes_read_ptr)
13561{
13562 LONGEST str_offset = read_offset (abfd, buf, cu_header, bytes_read_ptr);
13563
13564 return read_indirect_string_at_offset (abfd, str_offset);
13565}
13566
12df843f 13567static ULONGEST
fe1b8b76 13568read_unsigned_leb128 (bfd *abfd, gdb_byte *buf, unsigned int *bytes_read_ptr)
c906108c 13569{
12df843f 13570 ULONGEST result;
ce5d95e1 13571 unsigned int num_read;
c906108c
SS
13572 int i, shift;
13573 unsigned char byte;
13574
13575 result = 0;
13576 shift = 0;
13577 num_read = 0;
13578 i = 0;
13579 while (1)
13580 {
fe1b8b76 13581 byte = bfd_get_8 (abfd, buf);
c906108c
SS
13582 buf++;
13583 num_read++;
12df843f 13584 result |= ((ULONGEST) (byte & 127) << shift);
c906108c
SS
13585 if ((byte & 128) == 0)
13586 {
13587 break;
13588 }
13589 shift += 7;
13590 }
13591 *bytes_read_ptr = num_read;
13592 return result;
13593}
13594
12df843f 13595static LONGEST
fe1b8b76 13596read_signed_leb128 (bfd *abfd, gdb_byte *buf, unsigned int *bytes_read_ptr)
c906108c 13597{
12df843f 13598 LONGEST result;
77e0b926 13599 int i, shift, num_read;
c906108c
SS
13600 unsigned char byte;
13601
13602 result = 0;
13603 shift = 0;
c906108c
SS
13604 num_read = 0;
13605 i = 0;
13606 while (1)
13607 {
fe1b8b76 13608 byte = bfd_get_8 (abfd, buf);
c906108c
SS
13609 buf++;
13610 num_read++;
12df843f 13611 result |= ((LONGEST) (byte & 127) << shift);
c906108c
SS
13612 shift += 7;
13613 if ((byte & 128) == 0)
13614 {
13615 break;
13616 }
13617 }
77e0b926 13618 if ((shift < 8 * sizeof (result)) && (byte & 0x40))
12df843f 13619 result |= -(((LONGEST) 1) << shift);
c906108c
SS
13620 *bytes_read_ptr = num_read;
13621 return result;
13622}
13623
3019eac3
DE
13624/* Given index ADDR_INDEX in .debug_addr, fetch the value.
13625 ADDR_BASE is the DW_AT_GNU_addr_base attribute or zero.
13626 ADDR_SIZE is the size of addresses from the CU header. */
13627
13628static CORE_ADDR
13629read_addr_index_1 (unsigned int addr_index, ULONGEST addr_base, int addr_size)
13630{
13631 struct objfile *objfile = dwarf2_per_objfile->objfile;
13632 bfd *abfd = objfile->obfd;
13633 const gdb_byte *info_ptr;
13634
13635 dwarf2_read_section (objfile, &dwarf2_per_objfile->addr);
13636 if (dwarf2_per_objfile->addr.buffer == NULL)
13637 error (_("DW_FORM_addr_index used without .debug_addr section [in module %s]"),
13638 objfile->name);
13639 if (addr_base + addr_index * addr_size >= dwarf2_per_objfile->addr.size)
13640 error (_("DW_FORM_addr_index pointing outside of "
13641 ".debug_addr section [in module %s]"),
13642 objfile->name);
13643 info_ptr = (dwarf2_per_objfile->addr.buffer
13644 + addr_base + addr_index * addr_size);
13645 if (addr_size == 4)
13646 return bfd_get_32 (abfd, info_ptr);
13647 else
13648 return bfd_get_64 (abfd, info_ptr);
13649}
13650
13651/* Given index ADDR_INDEX in .debug_addr, fetch the value. */
13652
13653static CORE_ADDR
13654read_addr_index (struct dwarf2_cu *cu, unsigned int addr_index)
13655{
13656 return read_addr_index_1 (addr_index, cu->addr_base, cu->header.addr_size);
13657}
13658
13659/* Given a pointer to an leb128 value, fetch the value from .debug_addr. */
13660
13661static CORE_ADDR
13662read_addr_index_from_leb128 (struct dwarf2_cu *cu, gdb_byte *info_ptr,
13663 unsigned int *bytes_read)
13664{
13665 bfd *abfd = cu->objfile->obfd;
13666 unsigned int addr_index = read_unsigned_leb128 (abfd, info_ptr, bytes_read);
13667
13668 return read_addr_index (cu, addr_index);
13669}
13670
13671/* Data structure to pass results from dwarf2_read_addr_index_reader
13672 back to dwarf2_read_addr_index. */
13673
13674struct dwarf2_read_addr_index_data
13675{
13676 ULONGEST addr_base;
13677 int addr_size;
13678};
13679
13680/* die_reader_func for dwarf2_read_addr_index. */
13681
13682static void
13683dwarf2_read_addr_index_reader (const struct die_reader_specs *reader,
13684 gdb_byte *info_ptr,
13685 struct die_info *comp_unit_die,
13686 int has_children,
13687 void *data)
13688{
13689 struct dwarf2_cu *cu = reader->cu;
13690 struct dwarf2_read_addr_index_data *aidata =
13691 (struct dwarf2_read_addr_index_data *) data;
13692
13693 aidata->addr_base = cu->addr_base;
13694 aidata->addr_size = cu->header.addr_size;
13695}
13696
13697/* Given an index in .debug_addr, fetch the value.
13698 NOTE: This can be called during dwarf expression evaluation,
13699 long after the debug information has been read, and thus per_cu->cu
13700 may no longer exist. */
13701
13702CORE_ADDR
13703dwarf2_read_addr_index (struct dwarf2_per_cu_data *per_cu,
13704 unsigned int addr_index)
13705{
13706 struct objfile *objfile = per_cu->objfile;
13707 struct dwarf2_cu *cu = per_cu->cu;
13708 ULONGEST addr_base;
13709 int addr_size;
13710
13711 /* This is intended to be called from outside this file. */
13712 dw2_setup (objfile);
13713
13714 /* We need addr_base and addr_size.
13715 If we don't have PER_CU->cu, we have to get it.
13716 Nasty, but the alternative is storing the needed info in PER_CU,
13717 which at this point doesn't seem justified: it's not clear how frequently
13718 it would get used and it would increase the size of every PER_CU.
13719 Entry points like dwarf2_per_cu_addr_size do a similar thing
13720 so we're not in uncharted territory here.
13721 Alas we need to be a bit more complicated as addr_base is contained
13722 in the DIE.
13723
13724 We don't need to read the entire CU(/TU).
13725 We just need the header and top level die.
13726 IWBN to use the aging mechanism to let us lazily later discard the CU.
13727 See however init_cutu_and_read_dies_simple. */
13728
13729 if (cu != NULL)
13730 {
13731 addr_base = cu->addr_base;
13732 addr_size = cu->header.addr_size;
13733 }
13734 else
13735 {
13736 struct dwarf2_read_addr_index_data aidata;
13737
13738 init_cutu_and_read_dies_simple (per_cu, dwarf2_read_addr_index_reader,
13739 &aidata);
13740 addr_base = aidata.addr_base;
13741 addr_size = aidata.addr_size;
13742 }
13743
13744 return read_addr_index_1 (addr_index, addr_base, addr_size);
13745}
13746
13747/* Given a DW_AT_str_index, fetch the string. */
13748
13749static char *
13750read_str_index (const struct die_reader_specs *reader,
13751 struct dwarf2_cu *cu, ULONGEST str_index)
13752{
13753 struct objfile *objfile = dwarf2_per_objfile->objfile;
13754 const char *dwo_name = objfile->name;
13755 bfd *abfd = objfile->obfd;
13756 struct dwo_sections *sections = &reader->dwo_file->sections;
13757 gdb_byte *info_ptr;
13758 ULONGEST str_offset;
13759
13760 dwarf2_read_section (objfile, &sections->str);
13761 dwarf2_read_section (objfile, &sections->str_offsets);
13762 if (sections->str.buffer == NULL)
13763 error (_("DW_FORM_str_index used without .debug_str.dwo section"
13764 " in CU at offset 0x%lx [in module %s]"),
13765 (long) cu->header.offset.sect_off, dwo_name);
13766 if (sections->str_offsets.buffer == NULL)
13767 error (_("DW_FORM_str_index used without .debug_str_offsets.dwo section"
13768 " in CU at offset 0x%lx [in module %s]"),
13769 (long) cu->header.offset.sect_off, dwo_name);
13770 if (str_index * cu->header.offset_size >= sections->str_offsets.size)
13771 error (_("DW_FORM_str_index pointing outside of .debug_str_offsets.dwo"
13772 " section in CU at offset 0x%lx [in module %s]"),
13773 (long) cu->header.offset.sect_off, dwo_name);
13774 info_ptr = (sections->str_offsets.buffer
13775 + str_index * cu->header.offset_size);
13776 if (cu->header.offset_size == 4)
13777 str_offset = bfd_get_32 (abfd, info_ptr);
13778 else
13779 str_offset = bfd_get_64 (abfd, info_ptr);
13780 if (str_offset >= sections->str.size)
13781 error (_("Offset from DW_FORM_str_index pointing outside of"
13782 " .debug_str.dwo section in CU at offset 0x%lx [in module %s]"),
13783 (long) cu->header.offset.sect_off, dwo_name);
13784 return (char *) (sections->str.buffer + str_offset);
13785}
13786
3019eac3
DE
13787/* Return the length of an LEB128 number in BUF. */
13788
13789static int
13790leb128_size (const gdb_byte *buf)
13791{
13792 const gdb_byte *begin = buf;
13793 gdb_byte byte;
13794
13795 while (1)
13796 {
13797 byte = *buf++;
13798 if ((byte & 128) == 0)
13799 return buf - begin;
13800 }
13801}
13802
c906108c 13803static void
e142c38c 13804set_cu_language (unsigned int lang, struct dwarf2_cu *cu)
c906108c
SS
13805{
13806 switch (lang)
13807 {
13808 case DW_LANG_C89:
76bee0cc 13809 case DW_LANG_C99:
c906108c 13810 case DW_LANG_C:
e142c38c 13811 cu->language = language_c;
c906108c
SS
13812 break;
13813 case DW_LANG_C_plus_plus:
e142c38c 13814 cu->language = language_cplus;
c906108c 13815 break;
6aecb9c2
JB
13816 case DW_LANG_D:
13817 cu->language = language_d;
13818 break;
c906108c
SS
13819 case DW_LANG_Fortran77:
13820 case DW_LANG_Fortran90:
b21b22e0 13821 case DW_LANG_Fortran95:
e142c38c 13822 cu->language = language_fortran;
c906108c 13823 break;
a766d390
DE
13824 case DW_LANG_Go:
13825 cu->language = language_go;
13826 break;
c906108c 13827 case DW_LANG_Mips_Assembler:
e142c38c 13828 cu->language = language_asm;
c906108c 13829 break;
bebd888e 13830 case DW_LANG_Java:
e142c38c 13831 cu->language = language_java;
bebd888e 13832 break;
c906108c 13833 case DW_LANG_Ada83:
8aaf0b47 13834 case DW_LANG_Ada95:
bc5f45f8
JB
13835 cu->language = language_ada;
13836 break;
72019c9c
GM
13837 case DW_LANG_Modula2:
13838 cu->language = language_m2;
13839 break;
fe8e67fd
PM
13840 case DW_LANG_Pascal83:
13841 cu->language = language_pascal;
13842 break;
22566fbd
DJ
13843 case DW_LANG_ObjC:
13844 cu->language = language_objc;
13845 break;
c906108c
SS
13846 case DW_LANG_Cobol74:
13847 case DW_LANG_Cobol85:
c906108c 13848 default:
e142c38c 13849 cu->language = language_minimal;
c906108c
SS
13850 break;
13851 }
e142c38c 13852 cu->language_defn = language_def (cu->language);
c906108c
SS
13853}
13854
13855/* Return the named attribute or NULL if not there. */
13856
13857static struct attribute *
e142c38c 13858dwarf2_attr (struct die_info *die, unsigned int name, struct dwarf2_cu *cu)
c906108c 13859{
a48e046c 13860 for (;;)
c906108c 13861 {
a48e046c
TT
13862 unsigned int i;
13863 struct attribute *spec = NULL;
13864
13865 for (i = 0; i < die->num_attrs; ++i)
13866 {
13867 if (die->attrs[i].name == name)
13868 return &die->attrs[i];
13869 if (die->attrs[i].name == DW_AT_specification
13870 || die->attrs[i].name == DW_AT_abstract_origin)
13871 spec = &die->attrs[i];
13872 }
13873
13874 if (!spec)
13875 break;
c906108c 13876
f2f0e013 13877 die = follow_die_ref (die, spec, &cu);
f2f0e013 13878 }
c5aa993b 13879
c906108c
SS
13880 return NULL;
13881}
13882
348e048f
DE
13883/* Return the named attribute or NULL if not there,
13884 but do not follow DW_AT_specification, etc.
13885 This is for use in contexts where we're reading .debug_types dies.
13886 Following DW_AT_specification, DW_AT_abstract_origin will take us
13887 back up the chain, and we want to go down. */
13888
13889static struct attribute *
45e58e77 13890dwarf2_attr_no_follow (struct die_info *die, unsigned int name)
348e048f
DE
13891{
13892 unsigned int i;
13893
13894 for (i = 0; i < die->num_attrs; ++i)
13895 if (die->attrs[i].name == name)
13896 return &die->attrs[i];
13897
13898 return NULL;
13899}
13900
05cf31d1
JB
13901/* Return non-zero iff the attribute NAME is defined for the given DIE,
13902 and holds a non-zero value. This function should only be used for
2dc7f7b3 13903 DW_FORM_flag or DW_FORM_flag_present attributes. */
05cf31d1
JB
13904
13905static int
13906dwarf2_flag_true_p (struct die_info *die, unsigned name, struct dwarf2_cu *cu)
13907{
13908 struct attribute *attr = dwarf2_attr (die, name, cu);
13909
13910 return (attr && DW_UNSND (attr));
13911}
13912
3ca72b44 13913static int
e142c38c 13914die_is_declaration (struct die_info *die, struct dwarf2_cu *cu)
3ca72b44 13915{
05cf31d1
JB
13916 /* A DIE is a declaration if it has a DW_AT_declaration attribute
13917 which value is non-zero. However, we have to be careful with
13918 DIEs having a DW_AT_specification attribute, because dwarf2_attr()
13919 (via dwarf2_flag_true_p) follows this attribute. So we may
13920 end up accidently finding a declaration attribute that belongs
13921 to a different DIE referenced by the specification attribute,
13922 even though the given DIE does not have a declaration attribute. */
13923 return (dwarf2_flag_true_p (die, DW_AT_declaration, cu)
13924 && dwarf2_attr (die, DW_AT_specification, cu) == NULL);
3ca72b44
AC
13925}
13926
63d06c5c 13927/* Return the die giving the specification for DIE, if there is
f2f0e013 13928 one. *SPEC_CU is the CU containing DIE on input, and the CU
edb3359d
DJ
13929 containing the return value on output. If there is no
13930 specification, but there is an abstract origin, that is
13931 returned. */
63d06c5c
DC
13932
13933static struct die_info *
f2f0e013 13934die_specification (struct die_info *die, struct dwarf2_cu **spec_cu)
63d06c5c 13935{
f2f0e013
DJ
13936 struct attribute *spec_attr = dwarf2_attr (die, DW_AT_specification,
13937 *spec_cu);
63d06c5c 13938
edb3359d
DJ
13939 if (spec_attr == NULL)
13940 spec_attr = dwarf2_attr (die, DW_AT_abstract_origin, *spec_cu);
13941
63d06c5c
DC
13942 if (spec_attr == NULL)
13943 return NULL;
13944 else
f2f0e013 13945 return follow_die_ref (die, spec_attr, spec_cu);
63d06c5c 13946}
c906108c 13947
debd256d 13948/* Free the line_header structure *LH, and any arrays and strings it
ae2de4f8
DE
13949 refers to.
13950 NOTE: This is also used as a "cleanup" function. */
13951
debd256d
JB
13952static void
13953free_line_header (struct line_header *lh)
13954{
13955 if (lh->standard_opcode_lengths)
a8bc7b56 13956 xfree (lh->standard_opcode_lengths);
debd256d
JB
13957
13958 /* Remember that all the lh->file_names[i].name pointers are
13959 pointers into debug_line_buffer, and don't need to be freed. */
13960 if (lh->file_names)
a8bc7b56 13961 xfree (lh->file_names);
debd256d
JB
13962
13963 /* Similarly for the include directory names. */
13964 if (lh->include_dirs)
a8bc7b56 13965 xfree (lh->include_dirs);
debd256d 13966
a8bc7b56 13967 xfree (lh);
debd256d
JB
13968}
13969
debd256d 13970/* Add an entry to LH's include directory table. */
ae2de4f8 13971
debd256d
JB
13972static void
13973add_include_dir (struct line_header *lh, char *include_dir)
c906108c 13974{
debd256d
JB
13975 /* Grow the array if necessary. */
13976 if (lh->include_dirs_size == 0)
c5aa993b 13977 {
debd256d
JB
13978 lh->include_dirs_size = 1; /* for testing */
13979 lh->include_dirs = xmalloc (lh->include_dirs_size
13980 * sizeof (*lh->include_dirs));
13981 }
13982 else if (lh->num_include_dirs >= lh->include_dirs_size)
13983 {
13984 lh->include_dirs_size *= 2;
13985 lh->include_dirs = xrealloc (lh->include_dirs,
13986 (lh->include_dirs_size
13987 * sizeof (*lh->include_dirs)));
c5aa993b 13988 }
c906108c 13989
debd256d
JB
13990 lh->include_dirs[lh->num_include_dirs++] = include_dir;
13991}
6e70227d 13992
debd256d 13993/* Add an entry to LH's file name table. */
ae2de4f8 13994
debd256d
JB
13995static void
13996add_file_name (struct line_header *lh,
13997 char *name,
13998 unsigned int dir_index,
13999 unsigned int mod_time,
14000 unsigned int length)
14001{
14002 struct file_entry *fe;
14003
14004 /* Grow the array if necessary. */
14005 if (lh->file_names_size == 0)
14006 {
14007 lh->file_names_size = 1; /* for testing */
14008 lh->file_names = xmalloc (lh->file_names_size
14009 * sizeof (*lh->file_names));
14010 }
14011 else if (lh->num_file_names >= lh->file_names_size)
14012 {
14013 lh->file_names_size *= 2;
14014 lh->file_names = xrealloc (lh->file_names,
14015 (lh->file_names_size
14016 * sizeof (*lh->file_names)));
14017 }
14018
14019 fe = &lh->file_names[lh->num_file_names++];
14020 fe->name = name;
14021 fe->dir_index = dir_index;
14022 fe->mod_time = mod_time;
14023 fe->length = length;
aaa75496 14024 fe->included_p = 0;
cb1df416 14025 fe->symtab = NULL;
debd256d 14026}
6e70227d 14027
36586728
TT
14028/* A convenience function to find the proper .debug_line section for a
14029 CU. */
14030
14031static struct dwarf2_section_info *
14032get_debug_line_section (struct dwarf2_cu *cu)
14033{
14034 struct dwarf2_section_info *section;
14035
14036 /* For TUs in DWO files, the DW_AT_stmt_list attribute lives in the
14037 DWO file. */
14038 if (cu->dwo_unit && cu->per_cu->is_debug_types)
14039 section = &cu->dwo_unit->dwo_file->sections.line;
14040 else if (cu->per_cu->is_dwz)
14041 {
14042 struct dwz_file *dwz = dwarf2_get_dwz_file ();
14043
14044 section = &dwz->line;
14045 }
14046 else
14047 section = &dwarf2_per_objfile->line;
14048
14049 return section;
14050}
14051
debd256d 14052/* Read the statement program header starting at OFFSET in
3019eac3 14053 .debug_line, or .debug_line.dwo. Return a pointer
6502dd73 14054 to a struct line_header, allocated using xmalloc.
debd256d
JB
14055
14056 NOTE: the strings in the include directory and file name tables of
3019eac3
DE
14057 the returned object point into the dwarf line section buffer,
14058 and must not be freed. */
ae2de4f8 14059
debd256d 14060static struct line_header *
3019eac3 14061dwarf_decode_line_header (unsigned int offset, struct dwarf2_cu *cu)
debd256d
JB
14062{
14063 struct cleanup *back_to;
14064 struct line_header *lh;
fe1b8b76 14065 gdb_byte *line_ptr;
c764a876 14066 unsigned int bytes_read, offset_size;
debd256d
JB
14067 int i;
14068 char *cur_dir, *cur_file;
3019eac3
DE
14069 struct dwarf2_section_info *section;
14070 bfd *abfd;
14071
36586728 14072 section = get_debug_line_section (cu);
3019eac3
DE
14073 dwarf2_read_section (dwarf2_per_objfile->objfile, section);
14074 if (section->buffer == NULL)
debd256d 14075 {
3019eac3
DE
14076 if (cu->dwo_unit && cu->per_cu->is_debug_types)
14077 complaint (&symfile_complaints, _("missing .debug_line.dwo section"));
14078 else
14079 complaint (&symfile_complaints, _("missing .debug_line section"));
debd256d
JB
14080 return 0;
14081 }
14082
fceca515
DE
14083 /* We can't do this until we know the section is non-empty.
14084 Only then do we know we have such a section. */
14085 abfd = section->asection->owner;
14086
a738430d
MK
14087 /* Make sure that at least there's room for the total_length field.
14088 That could be 12 bytes long, but we're just going to fudge that. */
3019eac3 14089 if (offset + 4 >= section->size)
debd256d 14090 {
4d3c2250 14091 dwarf2_statement_list_fits_in_line_number_section_complaint ();
debd256d
JB
14092 return 0;
14093 }
14094
14095 lh = xmalloc (sizeof (*lh));
14096 memset (lh, 0, sizeof (*lh));
14097 back_to = make_cleanup ((make_cleanup_ftype *) free_line_header,
14098 (void *) lh);
14099
3019eac3 14100 line_ptr = section->buffer + offset;
debd256d 14101
a738430d 14102 /* Read in the header. */
6e70227d 14103 lh->total_length =
c764a876
DE
14104 read_checked_initial_length_and_offset (abfd, line_ptr, &cu->header,
14105 &bytes_read, &offset_size);
debd256d 14106 line_ptr += bytes_read;
3019eac3 14107 if (line_ptr + lh->total_length > (section->buffer + section->size))
debd256d 14108 {
4d3c2250 14109 dwarf2_statement_list_fits_in_line_number_section_complaint ();
debd256d
JB
14110 return 0;
14111 }
14112 lh->statement_program_end = line_ptr + lh->total_length;
14113 lh->version = read_2_bytes (abfd, line_ptr);
14114 line_ptr += 2;
c764a876
DE
14115 lh->header_length = read_offset_1 (abfd, line_ptr, offset_size);
14116 line_ptr += offset_size;
debd256d
JB
14117 lh->minimum_instruction_length = read_1_byte (abfd, line_ptr);
14118 line_ptr += 1;
2dc7f7b3
TT
14119 if (lh->version >= 4)
14120 {
14121 lh->maximum_ops_per_instruction = read_1_byte (abfd, line_ptr);
14122 line_ptr += 1;
14123 }
14124 else
14125 lh->maximum_ops_per_instruction = 1;
14126
14127 if (lh->maximum_ops_per_instruction == 0)
14128 {
14129 lh->maximum_ops_per_instruction = 1;
14130 complaint (&symfile_complaints,
3e43a32a
MS
14131 _("invalid maximum_ops_per_instruction "
14132 "in `.debug_line' section"));
2dc7f7b3
TT
14133 }
14134
debd256d
JB
14135 lh->default_is_stmt = read_1_byte (abfd, line_ptr);
14136 line_ptr += 1;
14137 lh->line_base = read_1_signed_byte (abfd, line_ptr);
14138 line_ptr += 1;
14139 lh->line_range = read_1_byte (abfd, line_ptr);
14140 line_ptr += 1;
14141 lh->opcode_base = read_1_byte (abfd, line_ptr);
14142 line_ptr += 1;
14143 lh->standard_opcode_lengths
fe1b8b76 14144 = xmalloc (lh->opcode_base * sizeof (lh->standard_opcode_lengths[0]));
debd256d
JB
14145
14146 lh->standard_opcode_lengths[0] = 1; /* This should never be used anyway. */
14147 for (i = 1; i < lh->opcode_base; ++i)
14148 {
14149 lh->standard_opcode_lengths[i] = read_1_byte (abfd, line_ptr);
14150 line_ptr += 1;
14151 }
14152
a738430d 14153 /* Read directory table. */
9b1c24c8 14154 while ((cur_dir = read_direct_string (abfd, line_ptr, &bytes_read)) != NULL)
debd256d
JB
14155 {
14156 line_ptr += bytes_read;
14157 add_include_dir (lh, cur_dir);
14158 }
14159 line_ptr += bytes_read;
14160
a738430d 14161 /* Read file name table. */
9b1c24c8 14162 while ((cur_file = read_direct_string (abfd, line_ptr, &bytes_read)) != NULL)
debd256d
JB
14163 {
14164 unsigned int dir_index, mod_time, length;
14165
14166 line_ptr += bytes_read;
14167 dir_index = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
14168 line_ptr += bytes_read;
14169 mod_time = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
14170 line_ptr += bytes_read;
14171 length = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
14172 line_ptr += bytes_read;
14173
14174 add_file_name (lh, cur_file, dir_index, mod_time, length);
14175 }
14176 line_ptr += bytes_read;
6e70227d 14177 lh->statement_program_start = line_ptr;
debd256d 14178
3019eac3 14179 if (line_ptr > (section->buffer + section->size))
4d3c2250 14180 complaint (&symfile_complaints,
3e43a32a
MS
14181 _("line number info header doesn't "
14182 "fit in `.debug_line' section"));
debd256d
JB
14183
14184 discard_cleanups (back_to);
14185 return lh;
14186}
c906108c 14187
c6da4cef
DE
14188/* Subroutine of dwarf_decode_lines to simplify it.
14189 Return the file name of the psymtab for included file FILE_INDEX
14190 in line header LH of PST.
14191 COMP_DIR is the compilation directory (DW_AT_comp_dir) or NULL if unknown.
14192 If space for the result is malloc'd, it will be freed by a cleanup.
14193 Returns NULL if FILE_INDEX should be ignored, i.e., it is pst->filename. */
14194
14195static char *
14196psymtab_include_file_name (const struct line_header *lh, int file_index,
14197 const struct partial_symtab *pst,
14198 const char *comp_dir)
14199{
14200 const struct file_entry fe = lh->file_names [file_index];
14201 char *include_name = fe.name;
14202 char *include_name_to_compare = include_name;
14203 char *dir_name = NULL;
72b9f47f
TT
14204 const char *pst_filename;
14205 char *copied_name = NULL;
c6da4cef
DE
14206 int file_is_pst;
14207
14208 if (fe.dir_index)
14209 dir_name = lh->include_dirs[fe.dir_index - 1];
14210
14211 if (!IS_ABSOLUTE_PATH (include_name)
14212 && (dir_name != NULL || comp_dir != NULL))
14213 {
14214 /* Avoid creating a duplicate psymtab for PST.
14215 We do this by comparing INCLUDE_NAME and PST_FILENAME.
14216 Before we do the comparison, however, we need to account
14217 for DIR_NAME and COMP_DIR.
14218 First prepend dir_name (if non-NULL). If we still don't
14219 have an absolute path prepend comp_dir (if non-NULL).
14220 However, the directory we record in the include-file's
14221 psymtab does not contain COMP_DIR (to match the
14222 corresponding symtab(s)).
14223
14224 Example:
14225
14226 bash$ cd /tmp
14227 bash$ gcc -g ./hello.c
14228 include_name = "hello.c"
14229 dir_name = "."
14230 DW_AT_comp_dir = comp_dir = "/tmp"
14231 DW_AT_name = "./hello.c" */
14232
14233 if (dir_name != NULL)
14234 {
14235 include_name = concat (dir_name, SLASH_STRING,
14236 include_name, (char *)NULL);
14237 include_name_to_compare = include_name;
14238 make_cleanup (xfree, include_name);
14239 }
14240 if (!IS_ABSOLUTE_PATH (include_name) && comp_dir != NULL)
14241 {
14242 include_name_to_compare = concat (comp_dir, SLASH_STRING,
14243 include_name, (char *)NULL);
14244 }
14245 }
14246
14247 pst_filename = pst->filename;
14248 if (!IS_ABSOLUTE_PATH (pst_filename) && pst->dirname != NULL)
14249 {
72b9f47f
TT
14250 copied_name = concat (pst->dirname, SLASH_STRING,
14251 pst_filename, (char *)NULL);
14252 pst_filename = copied_name;
c6da4cef
DE
14253 }
14254
1e3fad37 14255 file_is_pst = FILENAME_CMP (include_name_to_compare, pst_filename) == 0;
c6da4cef
DE
14256
14257 if (include_name_to_compare != include_name)
14258 xfree (include_name_to_compare);
72b9f47f
TT
14259 if (copied_name != NULL)
14260 xfree (copied_name);
c6da4cef
DE
14261
14262 if (file_is_pst)
14263 return NULL;
14264 return include_name;
14265}
14266
c91513d8
PP
14267/* Ignore this record_line request. */
14268
14269static void
14270noop_record_line (struct subfile *subfile, int line, CORE_ADDR pc)
14271{
14272 return;
14273}
14274
f3f5162e
DE
14275/* Subroutine of dwarf_decode_lines to simplify it.
14276 Process the line number information in LH. */
debd256d 14277
c906108c 14278static void
f3f5162e
DE
14279dwarf_decode_lines_1 (struct line_header *lh, const char *comp_dir,
14280 struct dwarf2_cu *cu, struct partial_symtab *pst)
c906108c 14281{
a8c50c1f 14282 gdb_byte *line_ptr, *extended_end;
fe1b8b76 14283 gdb_byte *line_end;
a8c50c1f 14284 unsigned int bytes_read, extended_len;
c906108c 14285 unsigned char op_code, extended_op, adj_opcode;
e142c38c
DJ
14286 CORE_ADDR baseaddr;
14287 struct objfile *objfile = cu->objfile;
f3f5162e 14288 bfd *abfd = objfile->obfd;
fbf65064 14289 struct gdbarch *gdbarch = get_objfile_arch (objfile);
aaa75496 14290 const int decode_for_pst_p = (pst != NULL);
f3f5162e 14291 struct subfile *last_subfile = NULL;
c91513d8
PP
14292 void (*p_record_line) (struct subfile *subfile, int line, CORE_ADDR pc)
14293 = record_line;
e142c38c
DJ
14294
14295 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
c906108c 14296
debd256d
JB
14297 line_ptr = lh->statement_program_start;
14298 line_end = lh->statement_program_end;
c906108c
SS
14299
14300 /* Read the statement sequences until there's nothing left. */
14301 while (line_ptr < line_end)
14302 {
14303 /* state machine registers */
14304 CORE_ADDR address = 0;
14305 unsigned int file = 1;
14306 unsigned int line = 1;
14307 unsigned int column = 0;
debd256d 14308 int is_stmt = lh->default_is_stmt;
c906108c
SS
14309 int basic_block = 0;
14310 int end_sequence = 0;
fbf65064 14311 CORE_ADDR addr;
2dc7f7b3 14312 unsigned char op_index = 0;
c906108c 14313
aaa75496 14314 if (!decode_for_pst_p && lh->num_file_names >= file)
c906108c 14315 {
aaa75496 14316 /* Start a subfile for the current file of the state machine. */
debd256d
JB
14317 /* lh->include_dirs and lh->file_names are 0-based, but the
14318 directory and file name numbers in the statement program
14319 are 1-based. */
14320 struct file_entry *fe = &lh->file_names[file - 1];
4f1520fb 14321 char *dir = NULL;
a738430d 14322
debd256d
JB
14323 if (fe->dir_index)
14324 dir = lh->include_dirs[fe->dir_index - 1];
4f1520fb
FR
14325
14326 dwarf2_start_subfile (fe->name, dir, comp_dir);
c906108c
SS
14327 }
14328
a738430d 14329 /* Decode the table. */
c5aa993b 14330 while (!end_sequence)
c906108c
SS
14331 {
14332 op_code = read_1_byte (abfd, line_ptr);
14333 line_ptr += 1;
59205f5a
JB
14334 if (line_ptr > line_end)
14335 {
14336 dwarf2_debug_line_missing_end_sequence_complaint ();
14337 break;
14338 }
9aa1fe7e 14339
debd256d 14340 if (op_code >= lh->opcode_base)
6e70227d 14341 {
a738430d 14342 /* Special operand. */
debd256d 14343 adj_opcode = op_code - lh->opcode_base;
2dc7f7b3
TT
14344 address += (((op_index + (adj_opcode / lh->line_range))
14345 / lh->maximum_ops_per_instruction)
14346 * lh->minimum_instruction_length);
14347 op_index = ((op_index + (adj_opcode / lh->line_range))
14348 % lh->maximum_ops_per_instruction);
debd256d 14349 line += lh->line_base + (adj_opcode % lh->line_range);
59205f5a 14350 if (lh->num_file_names < file || file == 0)
25e43795 14351 dwarf2_debug_line_missing_file_complaint ();
2dc7f7b3
TT
14352 /* For now we ignore lines not starting on an
14353 instruction boundary. */
14354 else if (op_index == 0)
25e43795
DJ
14355 {
14356 lh->file_names[file - 1].included_p = 1;
ca5f395d 14357 if (!decode_for_pst_p && is_stmt)
fbf65064
UW
14358 {
14359 if (last_subfile != current_subfile)
14360 {
14361 addr = gdbarch_addr_bits_remove (gdbarch, address);
14362 if (last_subfile)
c91513d8 14363 (*p_record_line) (last_subfile, 0, addr);
fbf65064
UW
14364 last_subfile = current_subfile;
14365 }
25e43795 14366 /* Append row to matrix using current values. */
7019d805 14367 addr = gdbarch_addr_bits_remove (gdbarch, address);
c91513d8 14368 (*p_record_line) (current_subfile, line, addr);
366da635 14369 }
25e43795 14370 }
ca5f395d 14371 basic_block = 0;
9aa1fe7e
GK
14372 }
14373 else switch (op_code)
c906108c
SS
14374 {
14375 case DW_LNS_extended_op:
3e43a32a
MS
14376 extended_len = read_unsigned_leb128 (abfd, line_ptr,
14377 &bytes_read);
473b7be6 14378 line_ptr += bytes_read;
a8c50c1f 14379 extended_end = line_ptr + extended_len;
c906108c
SS
14380 extended_op = read_1_byte (abfd, line_ptr);
14381 line_ptr += 1;
14382 switch (extended_op)
14383 {
14384 case DW_LNE_end_sequence:
c91513d8 14385 p_record_line = record_line;
c906108c 14386 end_sequence = 1;
c906108c
SS
14387 break;
14388 case DW_LNE_set_address:
e7c27a73 14389 address = read_address (abfd, line_ptr, cu, &bytes_read);
c91513d8
PP
14390
14391 if (address == 0 && !dwarf2_per_objfile->has_section_at_zero)
14392 {
14393 /* This line table is for a function which has been
14394 GCd by the linker. Ignore it. PR gdb/12528 */
14395
14396 long line_offset
36586728 14397 = line_ptr - get_debug_line_section (cu)->buffer;
c91513d8
PP
14398
14399 complaint (&symfile_complaints,
14400 _(".debug_line address at offset 0x%lx is 0 "
14401 "[in module %s]"),
bb5ed363 14402 line_offset, objfile->name);
c91513d8
PP
14403 p_record_line = noop_record_line;
14404 }
14405
2dc7f7b3 14406 op_index = 0;
107d2387
AC
14407 line_ptr += bytes_read;
14408 address += baseaddr;
c906108c
SS
14409 break;
14410 case DW_LNE_define_file:
debd256d
JB
14411 {
14412 char *cur_file;
14413 unsigned int dir_index, mod_time, length;
6e70227d 14414
3e43a32a
MS
14415 cur_file = read_direct_string (abfd, line_ptr,
14416 &bytes_read);
debd256d
JB
14417 line_ptr += bytes_read;
14418 dir_index =
14419 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
14420 line_ptr += bytes_read;
14421 mod_time =
14422 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
14423 line_ptr += bytes_read;
14424 length =
14425 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
14426 line_ptr += bytes_read;
14427 add_file_name (lh, cur_file, dir_index, mod_time, length);
14428 }
c906108c 14429 break;
d0c6ba3d
CC
14430 case DW_LNE_set_discriminator:
14431 /* The discriminator is not interesting to the debugger;
14432 just ignore it. */
14433 line_ptr = extended_end;
14434 break;
c906108c 14435 default:
4d3c2250 14436 complaint (&symfile_complaints,
e2e0b3e5 14437 _("mangled .debug_line section"));
debd256d 14438 return;
c906108c 14439 }
a8c50c1f
DJ
14440 /* Make sure that we parsed the extended op correctly. If e.g.
14441 we expected a different address size than the producer used,
14442 we may have read the wrong number of bytes. */
14443 if (line_ptr != extended_end)
14444 {
14445 complaint (&symfile_complaints,
14446 _("mangled .debug_line section"));
14447 return;
14448 }
c906108c
SS
14449 break;
14450 case DW_LNS_copy:
59205f5a 14451 if (lh->num_file_names < file || file == 0)
25e43795
DJ
14452 dwarf2_debug_line_missing_file_complaint ();
14453 else
366da635 14454 {
25e43795 14455 lh->file_names[file - 1].included_p = 1;
ca5f395d 14456 if (!decode_for_pst_p && is_stmt)
fbf65064
UW
14457 {
14458 if (last_subfile != current_subfile)
14459 {
14460 addr = gdbarch_addr_bits_remove (gdbarch, address);
14461 if (last_subfile)
c91513d8 14462 (*p_record_line) (last_subfile, 0, addr);
fbf65064
UW
14463 last_subfile = current_subfile;
14464 }
7019d805 14465 addr = gdbarch_addr_bits_remove (gdbarch, address);
c91513d8 14466 (*p_record_line) (current_subfile, line, addr);
fbf65064 14467 }
366da635 14468 }
c906108c
SS
14469 basic_block = 0;
14470 break;
14471 case DW_LNS_advance_pc:
2dc7f7b3
TT
14472 {
14473 CORE_ADDR adjust
14474 = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
14475
14476 address += (((op_index + adjust)
14477 / lh->maximum_ops_per_instruction)
14478 * lh->minimum_instruction_length);
14479 op_index = ((op_index + adjust)
14480 % lh->maximum_ops_per_instruction);
14481 line_ptr += bytes_read;
14482 }
c906108c
SS
14483 break;
14484 case DW_LNS_advance_line:
14485 line += read_signed_leb128 (abfd, line_ptr, &bytes_read);
14486 line_ptr += bytes_read;
14487 break;
14488 case DW_LNS_set_file:
debd256d 14489 {
a738430d
MK
14490 /* The arrays lh->include_dirs and lh->file_names are
14491 0-based, but the directory and file name numbers in
14492 the statement program are 1-based. */
debd256d 14493 struct file_entry *fe;
4f1520fb 14494 char *dir = NULL;
a738430d 14495
debd256d
JB
14496 file = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
14497 line_ptr += bytes_read;
59205f5a 14498 if (lh->num_file_names < file || file == 0)
25e43795
DJ
14499 dwarf2_debug_line_missing_file_complaint ();
14500 else
14501 {
14502 fe = &lh->file_names[file - 1];
14503 if (fe->dir_index)
14504 dir = lh->include_dirs[fe->dir_index - 1];
14505 if (!decode_for_pst_p)
14506 {
14507 last_subfile = current_subfile;
14508 dwarf2_start_subfile (fe->name, dir, comp_dir);
14509 }
14510 }
debd256d 14511 }
c906108c
SS
14512 break;
14513 case DW_LNS_set_column:
14514 column = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
14515 line_ptr += bytes_read;
14516 break;
14517 case DW_LNS_negate_stmt:
14518 is_stmt = (!is_stmt);
14519 break;
14520 case DW_LNS_set_basic_block:
14521 basic_block = 1;
14522 break;
c2c6d25f
JM
14523 /* Add to the address register of the state machine the
14524 address increment value corresponding to special opcode
a738430d
MK
14525 255. I.e., this value is scaled by the minimum
14526 instruction length since special opcode 255 would have
b021a221 14527 scaled the increment. */
c906108c 14528 case DW_LNS_const_add_pc:
2dc7f7b3
TT
14529 {
14530 CORE_ADDR adjust = (255 - lh->opcode_base) / lh->line_range;
14531
14532 address += (((op_index + adjust)
14533 / lh->maximum_ops_per_instruction)
14534 * lh->minimum_instruction_length);
14535 op_index = ((op_index + adjust)
14536 % lh->maximum_ops_per_instruction);
14537 }
c906108c
SS
14538 break;
14539 case DW_LNS_fixed_advance_pc:
14540 address += read_2_bytes (abfd, line_ptr);
2dc7f7b3 14541 op_index = 0;
c906108c
SS
14542 line_ptr += 2;
14543 break;
9aa1fe7e 14544 default:
a738430d
MK
14545 {
14546 /* Unknown standard opcode, ignore it. */
9aa1fe7e 14547 int i;
a738430d 14548
debd256d 14549 for (i = 0; i < lh->standard_opcode_lengths[op_code]; i++)
9aa1fe7e
GK
14550 {
14551 (void) read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
14552 line_ptr += bytes_read;
14553 }
14554 }
c906108c
SS
14555 }
14556 }
59205f5a
JB
14557 if (lh->num_file_names < file || file == 0)
14558 dwarf2_debug_line_missing_file_complaint ();
14559 else
14560 {
14561 lh->file_names[file - 1].included_p = 1;
14562 if (!decode_for_pst_p)
fbf65064
UW
14563 {
14564 addr = gdbarch_addr_bits_remove (gdbarch, address);
c91513d8 14565 (*p_record_line) (current_subfile, 0, addr);
fbf65064 14566 }
59205f5a 14567 }
c906108c 14568 }
f3f5162e
DE
14569}
14570
14571/* Decode the Line Number Program (LNP) for the given line_header
14572 structure and CU. The actual information extracted and the type
14573 of structures created from the LNP depends on the value of PST.
14574
14575 1. If PST is NULL, then this procedure uses the data from the program
14576 to create all necessary symbol tables, and their linetables.
14577
14578 2. If PST is not NULL, this procedure reads the program to determine
14579 the list of files included by the unit represented by PST, and
14580 builds all the associated partial symbol tables.
14581
14582 COMP_DIR is the compilation directory (DW_AT_comp_dir) or NULL if unknown.
14583 It is used for relative paths in the line table.
14584 NOTE: When processing partial symtabs (pst != NULL),
14585 comp_dir == pst->dirname.
14586
14587 NOTE: It is important that psymtabs have the same file name (via strcmp)
14588 as the corresponding symtab. Since COMP_DIR is not used in the name of the
14589 symtab we don't use it in the name of the psymtabs we create.
14590 E.g. expand_line_sal requires this when finding psymtabs to expand.
14591 A good testcase for this is mb-inline.exp. */
14592
14593static void
14594dwarf_decode_lines (struct line_header *lh, const char *comp_dir,
14595 struct dwarf2_cu *cu, struct partial_symtab *pst,
14596 int want_line_info)
14597{
14598 struct objfile *objfile = cu->objfile;
14599 const int decode_for_pst_p = (pst != NULL);
14600 struct subfile *first_subfile = current_subfile;
14601
14602 if (want_line_info)
14603 dwarf_decode_lines_1 (lh, comp_dir, cu, pst);
aaa75496
JB
14604
14605 if (decode_for_pst_p)
14606 {
14607 int file_index;
14608
14609 /* Now that we're done scanning the Line Header Program, we can
14610 create the psymtab of each included file. */
14611 for (file_index = 0; file_index < lh->num_file_names; file_index++)
14612 if (lh->file_names[file_index].included_p == 1)
14613 {
c6da4cef
DE
14614 char *include_name =
14615 psymtab_include_file_name (lh, file_index, pst, comp_dir);
14616 if (include_name != NULL)
aaa75496
JB
14617 dwarf2_create_include_psymtab (include_name, pst, objfile);
14618 }
14619 }
cb1df416
DJ
14620 else
14621 {
14622 /* Make sure a symtab is created for every file, even files
14623 which contain only variables (i.e. no code with associated
14624 line numbers). */
cb1df416 14625 int i;
cb1df416
DJ
14626
14627 for (i = 0; i < lh->num_file_names; i++)
14628 {
14629 char *dir = NULL;
f3f5162e 14630 struct file_entry *fe;
9a619af0 14631
cb1df416
DJ
14632 fe = &lh->file_names[i];
14633 if (fe->dir_index)
14634 dir = lh->include_dirs[fe->dir_index - 1];
14635 dwarf2_start_subfile (fe->name, dir, comp_dir);
14636
14637 /* Skip the main file; we don't need it, and it must be
14638 allocated last, so that it will show up before the
14639 non-primary symtabs in the objfile's symtab list. */
14640 if (current_subfile == first_subfile)
14641 continue;
14642
14643 if (current_subfile->symtab == NULL)
14644 current_subfile->symtab = allocate_symtab (current_subfile->name,
bb5ed363 14645 objfile);
cb1df416
DJ
14646 fe->symtab = current_subfile->symtab;
14647 }
14648 }
c906108c
SS
14649}
14650
14651/* Start a subfile for DWARF. FILENAME is the name of the file and
14652 DIRNAME the name of the source directory which contains FILENAME
4f1520fb
FR
14653 or NULL if not known. COMP_DIR is the compilation directory for the
14654 linetable's compilation unit or NULL if not known.
c906108c
SS
14655 This routine tries to keep line numbers from identical absolute and
14656 relative file names in a common subfile.
14657
14658 Using the `list' example from the GDB testsuite, which resides in
14659 /srcdir and compiling it with Irix6.2 cc in /compdir using a filename
14660 of /srcdir/list0.c yields the following debugging information for list0.c:
14661
c5aa993b
JM
14662 DW_AT_name: /srcdir/list0.c
14663 DW_AT_comp_dir: /compdir
357e46e7 14664 files.files[0].name: list0.h
c5aa993b 14665 files.files[0].dir: /srcdir
357e46e7 14666 files.files[1].name: list0.c
c5aa993b 14667 files.files[1].dir: /srcdir
c906108c
SS
14668
14669 The line number information for list0.c has to end up in a single
4f1520fb
FR
14670 subfile, so that `break /srcdir/list0.c:1' works as expected.
14671 start_subfile will ensure that this happens provided that we pass the
14672 concatenation of files.files[1].dir and files.files[1].name as the
14673 subfile's name. */
c906108c
SS
14674
14675static void
3e43a32a
MS
14676dwarf2_start_subfile (char *filename, const char *dirname,
14677 const char *comp_dir)
c906108c 14678{
4f1520fb
FR
14679 char *fullname;
14680
14681 /* While reading the DIEs, we call start_symtab(DW_AT_name, DW_AT_comp_dir).
14682 `start_symtab' will always pass the contents of DW_AT_comp_dir as
14683 second argument to start_subfile. To be consistent, we do the
14684 same here. In order not to lose the line information directory,
14685 we concatenate it to the filename when it makes sense.
14686 Note that the Dwarf3 standard says (speaking of filenames in line
14687 information): ``The directory index is ignored for file names
14688 that represent full path names''. Thus ignoring dirname in the
14689 `else' branch below isn't an issue. */
c906108c 14690
d5166ae1 14691 if (!IS_ABSOLUTE_PATH (filename) && dirname != NULL)
4f1520fb
FR
14692 fullname = concat (dirname, SLASH_STRING, filename, (char *)NULL);
14693 else
14694 fullname = filename;
c906108c 14695
4f1520fb
FR
14696 start_subfile (fullname, comp_dir);
14697
14698 if (fullname != filename)
14699 xfree (fullname);
c906108c
SS
14700}
14701
f4dc4d17
DE
14702/* Start a symtab for DWARF.
14703 NAME, COMP_DIR, LOW_PC are passed to start_symtab. */
14704
14705static void
14706dwarf2_start_symtab (struct dwarf2_cu *cu,
14707 char *name, char *comp_dir, CORE_ADDR low_pc)
14708{
14709 start_symtab (name, comp_dir, low_pc);
14710 record_debugformat ("DWARF 2");
14711 record_producer (cu->producer);
14712
14713 /* We assume that we're processing GCC output. */
14714 processing_gcc_compilation = 2;
14715
14716 processing_has_namespace_info = 0;
14717}
14718
4c2df51b
DJ
14719static void
14720var_decode_location (struct attribute *attr, struct symbol *sym,
e7c27a73 14721 struct dwarf2_cu *cu)
4c2df51b 14722{
e7c27a73
DJ
14723 struct objfile *objfile = cu->objfile;
14724 struct comp_unit_head *cu_header = &cu->header;
14725
4c2df51b
DJ
14726 /* NOTE drow/2003-01-30: There used to be a comment and some special
14727 code here to turn a symbol with DW_AT_external and a
14728 SYMBOL_VALUE_ADDRESS of 0 into a LOC_UNRESOLVED symbol. This was
14729 necessary for platforms (maybe Alpha, certainly PowerPC GNU/Linux
14730 with some versions of binutils) where shared libraries could have
14731 relocations against symbols in their debug information - the
14732 minimal symbol would have the right address, but the debug info
14733 would not. It's no longer necessary, because we will explicitly
14734 apply relocations when we read in the debug information now. */
14735
14736 /* A DW_AT_location attribute with no contents indicates that a
14737 variable has been optimized away. */
14738 if (attr_form_is_block (attr) && DW_BLOCK (attr)->size == 0)
14739 {
14740 SYMBOL_CLASS (sym) = LOC_OPTIMIZED_OUT;
14741 return;
14742 }
14743
14744 /* Handle one degenerate form of location expression specially, to
14745 preserve GDB's previous behavior when section offsets are
3019eac3
DE
14746 specified. If this is just a DW_OP_addr or DW_OP_GNU_addr_index
14747 then mark this symbol as LOC_STATIC. */
4c2df51b
DJ
14748
14749 if (attr_form_is_block (attr)
3019eac3
DE
14750 && ((DW_BLOCK (attr)->data[0] == DW_OP_addr
14751 && DW_BLOCK (attr)->size == 1 + cu_header->addr_size)
14752 || (DW_BLOCK (attr)->data[0] == DW_OP_GNU_addr_index
14753 && (DW_BLOCK (attr)->size
14754 == 1 + leb128_size (&DW_BLOCK (attr)->data[1])))))
4c2df51b 14755 {
891d2f0b 14756 unsigned int dummy;
4c2df51b 14757
3019eac3
DE
14758 if (DW_BLOCK (attr)->data[0] == DW_OP_addr)
14759 SYMBOL_VALUE_ADDRESS (sym) =
14760 read_address (objfile->obfd, DW_BLOCK (attr)->data + 1, cu, &dummy);
14761 else
14762 SYMBOL_VALUE_ADDRESS (sym) =
14763 read_addr_index_from_leb128 (cu, DW_BLOCK (attr)->data + 1, &dummy);
907fc202 14764 SYMBOL_CLASS (sym) = LOC_STATIC;
4c2df51b
DJ
14765 fixup_symbol_section (sym, objfile);
14766 SYMBOL_VALUE_ADDRESS (sym) += ANOFFSET (objfile->section_offsets,
14767 SYMBOL_SECTION (sym));
4c2df51b
DJ
14768 return;
14769 }
14770
14771 /* NOTE drow/2002-01-30: It might be worthwhile to have a static
14772 expression evaluator, and use LOC_COMPUTED only when necessary
14773 (i.e. when the value of a register or memory location is
14774 referenced, or a thread-local block, etc.). Then again, it might
14775 not be worthwhile. I'm assuming that it isn't unless performance
14776 or memory numbers show me otherwise. */
14777
e7c27a73 14778 dwarf2_symbol_mark_computed (attr, sym, cu);
4c2df51b 14779 SYMBOL_CLASS (sym) = LOC_COMPUTED;
8be455d7
JK
14780
14781 if (SYMBOL_COMPUTED_OPS (sym) == &dwarf2_loclist_funcs)
14782 cu->has_loclist = 1;
4c2df51b
DJ
14783}
14784
c906108c
SS
14785/* Given a pointer to a DWARF information entry, figure out if we need
14786 to make a symbol table entry for it, and if so, create a new entry
14787 and return a pointer to it.
14788 If TYPE is NULL, determine symbol type from the die, otherwise
34eaf542
TT
14789 used the passed type.
14790 If SPACE is not NULL, use it to hold the new symbol. If it is
14791 NULL, allocate a new symbol on the objfile's obstack. */
c906108c
SS
14792
14793static struct symbol *
34eaf542
TT
14794new_symbol_full (struct die_info *die, struct type *type, struct dwarf2_cu *cu,
14795 struct symbol *space)
c906108c 14796{
e7c27a73 14797 struct objfile *objfile = cu->objfile;
c906108c
SS
14798 struct symbol *sym = NULL;
14799 char *name;
14800 struct attribute *attr = NULL;
14801 struct attribute *attr2 = NULL;
e142c38c 14802 CORE_ADDR baseaddr;
e37fd15a
SW
14803 struct pending **list_to_add = NULL;
14804
edb3359d 14805 int inlined_func = (die->tag == DW_TAG_inlined_subroutine);
e142c38c
DJ
14806
14807 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
c906108c 14808
94af9270 14809 name = dwarf2_name (die, cu);
c906108c
SS
14810 if (name)
14811 {
94af9270 14812 const char *linkagename;
34eaf542 14813 int suppress_add = 0;
94af9270 14814
34eaf542
TT
14815 if (space)
14816 sym = space;
14817 else
14818 sym = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct symbol);
c906108c 14819 OBJSTAT (objfile, n_syms++);
2de7ced7
DJ
14820
14821 /* Cache this symbol's name and the name's demangled form (if any). */
33e5013e 14822 SYMBOL_SET_LANGUAGE (sym, cu->language);
94af9270
KS
14823 linkagename = dwarf2_physname (name, die, cu);
14824 SYMBOL_SET_NAMES (sym, linkagename, strlen (linkagename), 0, objfile);
c906108c 14825
f55ee35c
JK
14826 /* Fortran does not have mangling standard and the mangling does differ
14827 between gfortran, iFort etc. */
14828 if (cu->language == language_fortran
b250c185 14829 && symbol_get_demangled_name (&(sym->ginfo)) == NULL)
29df156d
SW
14830 symbol_set_demangled_name (&(sym->ginfo),
14831 (char *) dwarf2_full_name (name, die, cu),
14832 NULL);
f55ee35c 14833
c906108c 14834 /* Default assumptions.
c5aa993b 14835 Use the passed type or decode it from the die. */
176620f1 14836 SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
875dc2fc 14837 SYMBOL_CLASS (sym) = LOC_OPTIMIZED_OUT;
c906108c
SS
14838 if (type != NULL)
14839 SYMBOL_TYPE (sym) = type;
14840 else
e7c27a73 14841 SYMBOL_TYPE (sym) = die_type (die, cu);
edb3359d
DJ
14842 attr = dwarf2_attr (die,
14843 inlined_func ? DW_AT_call_line : DW_AT_decl_line,
14844 cu);
c906108c
SS
14845 if (attr)
14846 {
14847 SYMBOL_LINE (sym) = DW_UNSND (attr);
14848 }
cb1df416 14849
edb3359d
DJ
14850 attr = dwarf2_attr (die,
14851 inlined_func ? DW_AT_call_file : DW_AT_decl_file,
14852 cu);
cb1df416
DJ
14853 if (attr)
14854 {
14855 int file_index = DW_UNSND (attr);
9a619af0 14856
cb1df416
DJ
14857 if (cu->line_header == NULL
14858 || file_index > cu->line_header->num_file_names)
14859 complaint (&symfile_complaints,
14860 _("file index out of range"));
1c3d648d 14861 else if (file_index > 0)
cb1df416
DJ
14862 {
14863 struct file_entry *fe;
9a619af0 14864
cb1df416
DJ
14865 fe = &cu->line_header->file_names[file_index - 1];
14866 SYMBOL_SYMTAB (sym) = fe->symtab;
14867 }
14868 }
14869
c906108c
SS
14870 switch (die->tag)
14871 {
14872 case DW_TAG_label:
e142c38c 14873 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
c906108c
SS
14874 if (attr)
14875 {
14876 SYMBOL_VALUE_ADDRESS (sym) = DW_ADDR (attr) + baseaddr;
14877 }
0f5238ed
TT
14878 SYMBOL_TYPE (sym) = objfile_type (objfile)->builtin_core_addr;
14879 SYMBOL_DOMAIN (sym) = LABEL_DOMAIN;
c906108c 14880 SYMBOL_CLASS (sym) = LOC_LABEL;
0f5238ed 14881 add_symbol_to_list (sym, cu->list_in_scope);
c906108c
SS
14882 break;
14883 case DW_TAG_subprogram:
14884 /* SYMBOL_BLOCK_VALUE (sym) will be filled in later by
14885 finish_block. */
14886 SYMBOL_CLASS (sym) = LOC_BLOCK;
e142c38c 14887 attr2 = dwarf2_attr (die, DW_AT_external, cu);
2cfa0c8d
JB
14888 if ((attr2 && (DW_UNSND (attr2) != 0))
14889 || cu->language == language_ada)
c906108c 14890 {
2cfa0c8d
JB
14891 /* Subprograms marked external are stored as a global symbol.
14892 Ada subprograms, whether marked external or not, are always
14893 stored as a global symbol, because we want to be able to
14894 access them globally. For instance, we want to be able
14895 to break on a nested subprogram without having to
14896 specify the context. */
e37fd15a 14897 list_to_add = &global_symbols;
c906108c
SS
14898 }
14899 else
14900 {
e37fd15a 14901 list_to_add = cu->list_in_scope;
c906108c
SS
14902 }
14903 break;
edb3359d
DJ
14904 case DW_TAG_inlined_subroutine:
14905 /* SYMBOL_BLOCK_VALUE (sym) will be filled in later by
14906 finish_block. */
14907 SYMBOL_CLASS (sym) = LOC_BLOCK;
14908 SYMBOL_INLINED (sym) = 1;
481860b3 14909 list_to_add = cu->list_in_scope;
edb3359d 14910 break;
34eaf542
TT
14911 case DW_TAG_template_value_param:
14912 suppress_add = 1;
14913 /* Fall through. */
72929c62 14914 case DW_TAG_constant:
c906108c 14915 case DW_TAG_variable:
254e6b9e 14916 case DW_TAG_member:
0963b4bd
MS
14917 /* Compilation with minimal debug info may result in
14918 variables with missing type entries. Change the
14919 misleading `void' type to something sensible. */
c906108c 14920 if (TYPE_CODE (SYMBOL_TYPE (sym)) == TYPE_CODE_VOID)
64c50499 14921 SYMBOL_TYPE (sym)
46bf5051 14922 = objfile_type (objfile)->nodebug_data_symbol;
64c50499 14923
e142c38c 14924 attr = dwarf2_attr (die, DW_AT_const_value, cu);
254e6b9e
DE
14925 /* In the case of DW_TAG_member, we should only be called for
14926 static const members. */
14927 if (die->tag == DW_TAG_member)
14928 {
3863f96c
DE
14929 /* dwarf2_add_field uses die_is_declaration,
14930 so we do the same. */
254e6b9e
DE
14931 gdb_assert (die_is_declaration (die, cu));
14932 gdb_assert (attr);
14933 }
c906108c
SS
14934 if (attr)
14935 {
e7c27a73 14936 dwarf2_const_value (attr, sym, cu);
e142c38c 14937 attr2 = dwarf2_attr (die, DW_AT_external, cu);
e37fd15a 14938 if (!suppress_add)
34eaf542
TT
14939 {
14940 if (attr2 && (DW_UNSND (attr2) != 0))
e37fd15a 14941 list_to_add = &global_symbols;
34eaf542 14942 else
e37fd15a 14943 list_to_add = cu->list_in_scope;
34eaf542 14944 }
c906108c
SS
14945 break;
14946 }
e142c38c 14947 attr = dwarf2_attr (die, DW_AT_location, cu);
c906108c
SS
14948 if (attr)
14949 {
e7c27a73 14950 var_decode_location (attr, sym, cu);
e142c38c 14951 attr2 = dwarf2_attr (die, DW_AT_external, cu);
caac4577
JG
14952 if (SYMBOL_CLASS (sym) == LOC_STATIC
14953 && SYMBOL_VALUE_ADDRESS (sym) == 0
14954 && !dwarf2_per_objfile->has_section_at_zero)
14955 {
14956 /* When a static variable is eliminated by the linker,
14957 the corresponding debug information is not stripped
14958 out, but the variable address is set to null;
14959 do not add such variables into symbol table. */
14960 }
14961 else if (attr2 && (DW_UNSND (attr2) != 0))
1c809c68 14962 {
f55ee35c
JK
14963 /* Workaround gfortran PR debug/40040 - it uses
14964 DW_AT_location for variables in -fPIC libraries which may
14965 get overriden by other libraries/executable and get
14966 a different address. Resolve it by the minimal symbol
14967 which may come from inferior's executable using copy
14968 relocation. Make this workaround only for gfortran as for
14969 other compilers GDB cannot guess the minimal symbol
14970 Fortran mangling kind. */
14971 if (cu->language == language_fortran && die->parent
14972 && die->parent->tag == DW_TAG_module
14973 && cu->producer
14974 && strncmp (cu->producer, "GNU Fortran ", 12) == 0)
14975 SYMBOL_CLASS (sym) = LOC_UNRESOLVED;
14976
1c809c68
TT
14977 /* A variable with DW_AT_external is never static,
14978 but it may be block-scoped. */
14979 list_to_add = (cu->list_in_scope == &file_symbols
14980 ? &global_symbols : cu->list_in_scope);
1c809c68 14981 }
c906108c 14982 else
e37fd15a 14983 list_to_add = cu->list_in_scope;
c906108c
SS
14984 }
14985 else
14986 {
14987 /* We do not know the address of this symbol.
c5aa993b
JM
14988 If it is an external symbol and we have type information
14989 for it, enter the symbol as a LOC_UNRESOLVED symbol.
14990 The address of the variable will then be determined from
14991 the minimal symbol table whenever the variable is
14992 referenced. */
e142c38c 14993 attr2 = dwarf2_attr (die, DW_AT_external, cu);
c906108c 14994 if (attr2 && (DW_UNSND (attr2) != 0)
e142c38c 14995 && dwarf2_attr (die, DW_AT_type, cu) != NULL)
c906108c 14996 {
0fe7935b
DJ
14997 /* A variable with DW_AT_external is never static, but it
14998 may be block-scoped. */
14999 list_to_add = (cu->list_in_scope == &file_symbols
15000 ? &global_symbols : cu->list_in_scope);
15001
c906108c 15002 SYMBOL_CLASS (sym) = LOC_UNRESOLVED;
c906108c 15003 }
442ddf59
JK
15004 else if (!die_is_declaration (die, cu))
15005 {
15006 /* Use the default LOC_OPTIMIZED_OUT class. */
15007 gdb_assert (SYMBOL_CLASS (sym) == LOC_OPTIMIZED_OUT);
e37fd15a
SW
15008 if (!suppress_add)
15009 list_to_add = cu->list_in_scope;
442ddf59 15010 }
c906108c
SS
15011 }
15012 break;
15013 case DW_TAG_formal_parameter:
edb3359d
DJ
15014 /* If we are inside a function, mark this as an argument. If
15015 not, we might be looking at an argument to an inlined function
15016 when we do not have enough information to show inlined frames;
15017 pretend it's a local variable in that case so that the user can
15018 still see it. */
15019 if (context_stack_depth > 0
15020 && context_stack[context_stack_depth - 1].name != NULL)
15021 SYMBOL_IS_ARGUMENT (sym) = 1;
e142c38c 15022 attr = dwarf2_attr (die, DW_AT_location, cu);
c906108c
SS
15023 if (attr)
15024 {
e7c27a73 15025 var_decode_location (attr, sym, cu);
c906108c 15026 }
e142c38c 15027 attr = dwarf2_attr (die, DW_AT_const_value, cu);
c906108c
SS
15028 if (attr)
15029 {
e7c27a73 15030 dwarf2_const_value (attr, sym, cu);
c906108c 15031 }
f346a30d 15032
e37fd15a 15033 list_to_add = cu->list_in_scope;
c906108c
SS
15034 break;
15035 case DW_TAG_unspecified_parameters:
15036 /* From varargs functions; gdb doesn't seem to have any
15037 interest in this information, so just ignore it for now.
15038 (FIXME?) */
15039 break;
34eaf542
TT
15040 case DW_TAG_template_type_param:
15041 suppress_add = 1;
15042 /* Fall through. */
c906108c 15043 case DW_TAG_class_type:
680b30c7 15044 case DW_TAG_interface_type:
c906108c
SS
15045 case DW_TAG_structure_type:
15046 case DW_TAG_union_type:
72019c9c 15047 case DW_TAG_set_type:
c906108c
SS
15048 case DW_TAG_enumeration_type:
15049 SYMBOL_CLASS (sym) = LOC_TYPEDEF;
176620f1 15050 SYMBOL_DOMAIN (sym) = STRUCT_DOMAIN;
c906108c 15051
63d06c5c 15052 {
987504bb 15053 /* NOTE: carlton/2003-11-10: C++ and Java class symbols shouldn't
63d06c5c
DC
15054 really ever be static objects: otherwise, if you try
15055 to, say, break of a class's method and you're in a file
15056 which doesn't mention that class, it won't work unless
15057 the check for all static symbols in lookup_symbol_aux
15058 saves you. See the OtherFileClass tests in
15059 gdb.c++/namespace.exp. */
15060
e37fd15a 15061 if (!suppress_add)
34eaf542 15062 {
34eaf542
TT
15063 list_to_add = (cu->list_in_scope == &file_symbols
15064 && (cu->language == language_cplus
15065 || cu->language == language_java)
15066 ? &global_symbols : cu->list_in_scope);
63d06c5c 15067
64382290
TT
15068 /* The semantics of C++ state that "struct foo {
15069 ... }" also defines a typedef for "foo". A Java
15070 class declaration also defines a typedef for the
15071 class. */
15072 if (cu->language == language_cplus
15073 || cu->language == language_java
15074 || cu->language == language_ada)
15075 {
15076 /* The symbol's name is already allocated along
15077 with this objfile, so we don't need to
15078 duplicate it for the type. */
15079 if (TYPE_NAME (SYMBOL_TYPE (sym)) == 0)
15080 TYPE_NAME (SYMBOL_TYPE (sym)) = SYMBOL_SEARCH_NAME (sym);
15081 }
63d06c5c
DC
15082 }
15083 }
c906108c
SS
15084 break;
15085 case DW_TAG_typedef:
63d06c5c
DC
15086 SYMBOL_CLASS (sym) = LOC_TYPEDEF;
15087 SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
e37fd15a 15088 list_to_add = cu->list_in_scope;
63d06c5c 15089 break;
c906108c 15090 case DW_TAG_base_type:
a02abb62 15091 case DW_TAG_subrange_type:
c906108c 15092 SYMBOL_CLASS (sym) = LOC_TYPEDEF;
176620f1 15093 SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
e37fd15a 15094 list_to_add = cu->list_in_scope;
c906108c
SS
15095 break;
15096 case DW_TAG_enumerator:
e142c38c 15097 attr = dwarf2_attr (die, DW_AT_const_value, cu);
c906108c
SS
15098 if (attr)
15099 {
e7c27a73 15100 dwarf2_const_value (attr, sym, cu);
c906108c 15101 }
63d06c5c
DC
15102 {
15103 /* NOTE: carlton/2003-11-10: See comment above in the
15104 DW_TAG_class_type, etc. block. */
15105
e142c38c 15106 list_to_add = (cu->list_in_scope == &file_symbols
987504bb
JJ
15107 && (cu->language == language_cplus
15108 || cu->language == language_java)
e142c38c 15109 ? &global_symbols : cu->list_in_scope);
63d06c5c 15110 }
c906108c 15111 break;
5c4e30ca
DC
15112 case DW_TAG_namespace:
15113 SYMBOL_CLASS (sym) = LOC_TYPEDEF;
e37fd15a 15114 list_to_add = &global_symbols;
5c4e30ca 15115 break;
c906108c
SS
15116 default:
15117 /* Not a tag we recognize. Hopefully we aren't processing
15118 trash data, but since we must specifically ignore things
15119 we don't recognize, there is nothing else we should do at
0963b4bd 15120 this point. */
e2e0b3e5 15121 complaint (&symfile_complaints, _("unsupported tag: '%s'"),
4d3c2250 15122 dwarf_tag_name (die->tag));
c906108c
SS
15123 break;
15124 }
df8a16a1 15125
e37fd15a
SW
15126 if (suppress_add)
15127 {
15128 sym->hash_next = objfile->template_symbols;
15129 objfile->template_symbols = sym;
15130 list_to_add = NULL;
15131 }
15132
15133 if (list_to_add != NULL)
15134 add_symbol_to_list (sym, list_to_add);
15135
df8a16a1
DJ
15136 /* For the benefit of old versions of GCC, check for anonymous
15137 namespaces based on the demangled name. */
15138 if (!processing_has_namespace_info
94af9270 15139 && cu->language == language_cplus)
a10964d1 15140 cp_scan_for_anonymous_namespaces (sym, objfile);
c906108c
SS
15141 }
15142 return (sym);
15143}
15144
34eaf542
TT
15145/* A wrapper for new_symbol_full that always allocates a new symbol. */
15146
15147static struct symbol *
15148new_symbol (struct die_info *die, struct type *type, struct dwarf2_cu *cu)
15149{
15150 return new_symbol_full (die, type, cu, NULL);
15151}
15152
98bfdba5
PA
15153/* Given an attr with a DW_FORM_dataN value in host byte order,
15154 zero-extend it as appropriate for the symbol's type. The DWARF
15155 standard (v4) is not entirely clear about the meaning of using
15156 DW_FORM_dataN for a constant with a signed type, where the type is
15157 wider than the data. The conclusion of a discussion on the DWARF
15158 list was that this is unspecified. We choose to always zero-extend
15159 because that is the interpretation long in use by GCC. */
c906108c 15160
98bfdba5
PA
15161static gdb_byte *
15162dwarf2_const_value_data (struct attribute *attr, struct type *type,
15163 const char *name, struct obstack *obstack,
12df843f 15164 struct dwarf2_cu *cu, LONGEST *value, int bits)
c906108c 15165{
e7c27a73 15166 struct objfile *objfile = cu->objfile;
e17a4113
UW
15167 enum bfd_endian byte_order = bfd_big_endian (objfile->obfd) ?
15168 BFD_ENDIAN_BIG : BFD_ENDIAN_LITTLE;
98bfdba5
PA
15169 LONGEST l = DW_UNSND (attr);
15170
15171 if (bits < sizeof (*value) * 8)
15172 {
15173 l &= ((LONGEST) 1 << bits) - 1;
15174 *value = l;
15175 }
15176 else if (bits == sizeof (*value) * 8)
15177 *value = l;
15178 else
15179 {
15180 gdb_byte *bytes = obstack_alloc (obstack, bits / 8);
15181 store_unsigned_integer (bytes, bits / 8, byte_order, l);
15182 return bytes;
15183 }
15184
15185 return NULL;
15186}
15187
15188/* Read a constant value from an attribute. Either set *VALUE, or if
15189 the value does not fit in *VALUE, set *BYTES - either already
15190 allocated on the objfile obstack, or newly allocated on OBSTACK,
15191 or, set *BATON, if we translated the constant to a location
15192 expression. */
15193
15194static void
15195dwarf2_const_value_attr (struct attribute *attr, struct type *type,
15196 const char *name, struct obstack *obstack,
15197 struct dwarf2_cu *cu,
12df843f 15198 LONGEST *value, gdb_byte **bytes,
98bfdba5
PA
15199 struct dwarf2_locexpr_baton **baton)
15200{
15201 struct objfile *objfile = cu->objfile;
15202 struct comp_unit_head *cu_header = &cu->header;
c906108c 15203 struct dwarf_block *blk;
98bfdba5
PA
15204 enum bfd_endian byte_order = (bfd_big_endian (objfile->obfd) ?
15205 BFD_ENDIAN_BIG : BFD_ENDIAN_LITTLE);
15206
15207 *value = 0;
15208 *bytes = NULL;
15209 *baton = NULL;
c906108c
SS
15210
15211 switch (attr->form)
15212 {
15213 case DW_FORM_addr:
3019eac3 15214 case DW_FORM_GNU_addr_index:
ac56253d 15215 {
ac56253d
TT
15216 gdb_byte *data;
15217
98bfdba5
PA
15218 if (TYPE_LENGTH (type) != cu_header->addr_size)
15219 dwarf2_const_value_length_mismatch_complaint (name,
ac56253d 15220 cu_header->addr_size,
98bfdba5 15221 TYPE_LENGTH (type));
ac56253d
TT
15222 /* Symbols of this form are reasonably rare, so we just
15223 piggyback on the existing location code rather than writing
15224 a new implementation of symbol_computed_ops. */
98bfdba5
PA
15225 *baton = obstack_alloc (&objfile->objfile_obstack,
15226 sizeof (struct dwarf2_locexpr_baton));
15227 (*baton)->per_cu = cu->per_cu;
15228 gdb_assert ((*baton)->per_cu);
ac56253d 15229
98bfdba5
PA
15230 (*baton)->size = 2 + cu_header->addr_size;
15231 data = obstack_alloc (&objfile->objfile_obstack, (*baton)->size);
15232 (*baton)->data = data;
ac56253d
TT
15233
15234 data[0] = DW_OP_addr;
15235 store_unsigned_integer (&data[1], cu_header->addr_size,
15236 byte_order, DW_ADDR (attr));
15237 data[cu_header->addr_size + 1] = DW_OP_stack_value;
ac56253d 15238 }
c906108c 15239 break;
4ac36638 15240 case DW_FORM_string:
93b5768b 15241 case DW_FORM_strp:
3019eac3 15242 case DW_FORM_GNU_str_index:
36586728 15243 case DW_FORM_GNU_strp_alt:
98bfdba5
PA
15244 /* DW_STRING is already allocated on the objfile obstack, point
15245 directly to it. */
15246 *bytes = (gdb_byte *) DW_STRING (attr);
93b5768b 15247 break;
c906108c
SS
15248 case DW_FORM_block1:
15249 case DW_FORM_block2:
15250 case DW_FORM_block4:
15251 case DW_FORM_block:
2dc7f7b3 15252 case DW_FORM_exprloc:
c906108c 15253 blk = DW_BLOCK (attr);
98bfdba5
PA
15254 if (TYPE_LENGTH (type) != blk->size)
15255 dwarf2_const_value_length_mismatch_complaint (name, blk->size,
15256 TYPE_LENGTH (type));
15257 *bytes = blk->data;
c906108c 15258 break;
2df3850c
JM
15259
15260 /* The DW_AT_const_value attributes are supposed to carry the
15261 symbol's value "represented as it would be on the target
15262 architecture." By the time we get here, it's already been
15263 converted to host endianness, so we just need to sign- or
15264 zero-extend it as appropriate. */
15265 case DW_FORM_data1:
3e43a32a
MS
15266 *bytes = dwarf2_const_value_data (attr, type, name,
15267 obstack, cu, value, 8);
2df3850c 15268 break;
c906108c 15269 case DW_FORM_data2:
3e43a32a
MS
15270 *bytes = dwarf2_const_value_data (attr, type, name,
15271 obstack, cu, value, 16);
2df3850c 15272 break;
c906108c 15273 case DW_FORM_data4:
3e43a32a
MS
15274 *bytes = dwarf2_const_value_data (attr, type, name,
15275 obstack, cu, value, 32);
2df3850c 15276 break;
c906108c 15277 case DW_FORM_data8:
3e43a32a
MS
15278 *bytes = dwarf2_const_value_data (attr, type, name,
15279 obstack, cu, value, 64);
2df3850c
JM
15280 break;
15281
c906108c 15282 case DW_FORM_sdata:
98bfdba5 15283 *value = DW_SND (attr);
2df3850c
JM
15284 break;
15285
c906108c 15286 case DW_FORM_udata:
98bfdba5 15287 *value = DW_UNSND (attr);
c906108c 15288 break;
2df3850c 15289
c906108c 15290 default:
4d3c2250 15291 complaint (&symfile_complaints,
e2e0b3e5 15292 _("unsupported const value attribute form: '%s'"),
4d3c2250 15293 dwarf_form_name (attr->form));
98bfdba5 15294 *value = 0;
c906108c
SS
15295 break;
15296 }
15297}
15298
2df3850c 15299
98bfdba5
PA
15300/* Copy constant value from an attribute to a symbol. */
15301
2df3850c 15302static void
98bfdba5
PA
15303dwarf2_const_value (struct attribute *attr, struct symbol *sym,
15304 struct dwarf2_cu *cu)
2df3850c 15305{
98bfdba5
PA
15306 struct objfile *objfile = cu->objfile;
15307 struct comp_unit_head *cu_header = &cu->header;
12df843f 15308 LONGEST value;
98bfdba5
PA
15309 gdb_byte *bytes;
15310 struct dwarf2_locexpr_baton *baton;
2df3850c 15311
98bfdba5
PA
15312 dwarf2_const_value_attr (attr, SYMBOL_TYPE (sym),
15313 SYMBOL_PRINT_NAME (sym),
15314 &objfile->objfile_obstack, cu,
15315 &value, &bytes, &baton);
2df3850c 15316
98bfdba5
PA
15317 if (baton != NULL)
15318 {
15319 SYMBOL_COMPUTED_OPS (sym) = &dwarf2_locexpr_funcs;
15320 SYMBOL_LOCATION_BATON (sym) = baton;
15321 SYMBOL_CLASS (sym) = LOC_COMPUTED;
15322 }
15323 else if (bytes != NULL)
15324 {
15325 SYMBOL_VALUE_BYTES (sym) = bytes;
15326 SYMBOL_CLASS (sym) = LOC_CONST_BYTES;
15327 }
15328 else
15329 {
15330 SYMBOL_VALUE (sym) = value;
15331 SYMBOL_CLASS (sym) = LOC_CONST;
15332 }
2df3850c
JM
15333}
15334
c906108c
SS
15335/* Return the type of the die in question using its DW_AT_type attribute. */
15336
15337static struct type *
e7c27a73 15338die_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 15339{
c906108c 15340 struct attribute *type_attr;
c906108c 15341
e142c38c 15342 type_attr = dwarf2_attr (die, DW_AT_type, cu);
c906108c
SS
15343 if (!type_attr)
15344 {
15345 /* A missing DW_AT_type represents a void type. */
46bf5051 15346 return objfile_type (cu->objfile)->builtin_void;
c906108c 15347 }
348e048f 15348
673bfd45 15349 return lookup_die_type (die, type_attr, cu);
c906108c
SS
15350}
15351
b4ba55a1
JB
15352/* True iff CU's producer generates GNAT Ada auxiliary information
15353 that allows to find parallel types through that information instead
15354 of having to do expensive parallel lookups by type name. */
15355
15356static int
15357need_gnat_info (struct dwarf2_cu *cu)
15358{
15359 /* FIXME: brobecker/2010-10-12: As of now, only the AdaCore version
15360 of GNAT produces this auxiliary information, without any indication
15361 that it is produced. Part of enhancing the FSF version of GNAT
15362 to produce that information will be to put in place an indicator
15363 that we can use in order to determine whether the descriptive type
15364 info is available or not. One suggestion that has been made is
15365 to use a new attribute, attached to the CU die. For now, assume
15366 that the descriptive type info is not available. */
15367 return 0;
15368}
15369
b4ba55a1
JB
15370/* Return the auxiliary type of the die in question using its
15371 DW_AT_GNAT_descriptive_type attribute. Returns NULL if the
15372 attribute is not present. */
15373
15374static struct type *
15375die_descriptive_type (struct die_info *die, struct dwarf2_cu *cu)
15376{
b4ba55a1 15377 struct attribute *type_attr;
b4ba55a1
JB
15378
15379 type_attr = dwarf2_attr (die, DW_AT_GNAT_descriptive_type, cu);
15380 if (!type_attr)
15381 return NULL;
15382
673bfd45 15383 return lookup_die_type (die, type_attr, cu);
b4ba55a1
JB
15384}
15385
15386/* If DIE has a descriptive_type attribute, then set the TYPE's
15387 descriptive type accordingly. */
15388
15389static void
15390set_descriptive_type (struct type *type, struct die_info *die,
15391 struct dwarf2_cu *cu)
15392{
15393 struct type *descriptive_type = die_descriptive_type (die, cu);
15394
15395 if (descriptive_type)
15396 {
15397 ALLOCATE_GNAT_AUX_TYPE (type);
15398 TYPE_DESCRIPTIVE_TYPE (type) = descriptive_type;
15399 }
15400}
15401
c906108c
SS
15402/* Return the containing type of the die in question using its
15403 DW_AT_containing_type attribute. */
15404
15405static struct type *
e7c27a73 15406die_containing_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 15407{
c906108c 15408 struct attribute *type_attr;
c906108c 15409
e142c38c 15410 type_attr = dwarf2_attr (die, DW_AT_containing_type, cu);
33ac96f0
JK
15411 if (!type_attr)
15412 error (_("Dwarf Error: Problem turning containing type into gdb type "
15413 "[in module %s]"), cu->objfile->name);
15414
673bfd45 15415 return lookup_die_type (die, type_attr, cu);
c906108c
SS
15416}
15417
673bfd45
DE
15418/* Look up the type of DIE in CU using its type attribute ATTR.
15419 If there is no type substitute an error marker. */
15420
c906108c 15421static struct type *
673bfd45
DE
15422lookup_die_type (struct die_info *die, struct attribute *attr,
15423 struct dwarf2_cu *cu)
c906108c 15424{
bb5ed363 15425 struct objfile *objfile = cu->objfile;
f792889a
DJ
15426 struct type *this_type;
15427
673bfd45
DE
15428 /* First see if we have it cached. */
15429
36586728
TT
15430 if (attr->form == DW_FORM_GNU_ref_alt)
15431 {
15432 struct dwarf2_per_cu_data *per_cu;
15433 sect_offset offset = dwarf2_get_ref_die_offset (attr);
15434
15435 per_cu = dwarf2_find_containing_comp_unit (offset, 1, cu->objfile);
15436 this_type = get_die_type_at_offset (offset, per_cu);
15437 }
15438 else if (is_ref_attr (attr))
673bfd45 15439 {
b64f50a1 15440 sect_offset offset = dwarf2_get_ref_die_offset (attr);
673bfd45
DE
15441
15442 this_type = get_die_type_at_offset (offset, cu->per_cu);
15443 }
55f1336d 15444 else if (attr->form == DW_FORM_ref_sig8)
673bfd45
DE
15445 {
15446 struct signatured_type *sig_type = DW_SIGNATURED_TYPE (attr);
673bfd45
DE
15447
15448 /* sig_type will be NULL if the signatured type is missing from
15449 the debug info. */
15450 if (sig_type == NULL)
15451 error (_("Dwarf Error: Cannot find signatured DIE referenced from DIE "
15452 "at 0x%x [in module %s]"),
b64f50a1 15453 die->offset.sect_off, objfile->name);
673bfd45 15454
3019eac3
DE
15455 gdb_assert (sig_type->per_cu.is_debug_types);
15456 /* If we haven't filled in type_offset_in_section yet, then we
15457 haven't read the type in yet. */
15458 this_type = NULL;
15459 if (sig_type->type_offset_in_section.sect_off != 0)
15460 {
15461 this_type =
15462 get_die_type_at_offset (sig_type->type_offset_in_section,
15463 &sig_type->per_cu);
15464 }
673bfd45
DE
15465 }
15466 else
15467 {
15468 dump_die_for_error (die);
15469 error (_("Dwarf Error: Bad type attribute %s [in module %s]"),
bb5ed363 15470 dwarf_attr_name (attr->name), objfile->name);
673bfd45
DE
15471 }
15472
15473 /* If not cached we need to read it in. */
15474
15475 if (this_type == NULL)
15476 {
15477 struct die_info *type_die;
15478 struct dwarf2_cu *type_cu = cu;
15479
15480 type_die = follow_die_ref_or_sig (die, attr, &type_cu);
3019eac3
DE
15481 /* If we found the type now, it's probably because the type came
15482 from an inter-CU reference and the type's CU got expanded before
15483 ours. */
15484 this_type = get_die_type (type_die, type_cu);
15485 if (this_type == NULL)
15486 this_type = read_type_die_1 (type_die, type_cu);
673bfd45
DE
15487 }
15488
15489 /* If we still don't have a type use an error marker. */
15490
15491 if (this_type == NULL)
c906108c 15492 {
b00fdb78
TT
15493 char *message, *saved;
15494
15495 /* read_type_die already issued a complaint. */
15496 message = xstrprintf (_("<unknown type in %s, CU 0x%x, DIE 0x%x>"),
bb5ed363 15497 objfile->name,
b64f50a1
JK
15498 cu->header.offset.sect_off,
15499 die->offset.sect_off);
bb5ed363 15500 saved = obstack_copy0 (&objfile->objfile_obstack,
b00fdb78
TT
15501 message, strlen (message));
15502 xfree (message);
15503
bb5ed363 15504 this_type = init_type (TYPE_CODE_ERROR, 0, 0, saved, objfile);
c906108c 15505 }
673bfd45 15506
f792889a 15507 return this_type;
c906108c
SS
15508}
15509
673bfd45
DE
15510/* Return the type in DIE, CU.
15511 Returns NULL for invalid types.
15512
15513 This first does a lookup in the appropriate type_hash table,
15514 and only reads the die in if necessary.
15515
15516 NOTE: This can be called when reading in partial or full symbols. */
15517
f792889a 15518static struct type *
e7c27a73 15519read_type_die (struct die_info *die, struct dwarf2_cu *cu)
c906108c 15520{
f792889a
DJ
15521 struct type *this_type;
15522
15523 this_type = get_die_type (die, cu);
15524 if (this_type)
15525 return this_type;
15526
673bfd45
DE
15527 return read_type_die_1 (die, cu);
15528}
15529
15530/* Read the type in DIE, CU.
15531 Returns NULL for invalid types. */
15532
15533static struct type *
15534read_type_die_1 (struct die_info *die, struct dwarf2_cu *cu)
15535{
15536 struct type *this_type = NULL;
15537
c906108c
SS
15538 switch (die->tag)
15539 {
15540 case DW_TAG_class_type:
680b30c7 15541 case DW_TAG_interface_type:
c906108c
SS
15542 case DW_TAG_structure_type:
15543 case DW_TAG_union_type:
f792889a 15544 this_type = read_structure_type (die, cu);
c906108c
SS
15545 break;
15546 case DW_TAG_enumeration_type:
f792889a 15547 this_type = read_enumeration_type (die, cu);
c906108c
SS
15548 break;
15549 case DW_TAG_subprogram:
15550 case DW_TAG_subroutine_type:
edb3359d 15551 case DW_TAG_inlined_subroutine:
f792889a 15552 this_type = read_subroutine_type (die, cu);
c906108c
SS
15553 break;
15554 case DW_TAG_array_type:
f792889a 15555 this_type = read_array_type (die, cu);
c906108c 15556 break;
72019c9c 15557 case DW_TAG_set_type:
f792889a 15558 this_type = read_set_type (die, cu);
72019c9c 15559 break;
c906108c 15560 case DW_TAG_pointer_type:
f792889a 15561 this_type = read_tag_pointer_type (die, cu);
c906108c
SS
15562 break;
15563 case DW_TAG_ptr_to_member_type:
f792889a 15564 this_type = read_tag_ptr_to_member_type (die, cu);
c906108c
SS
15565 break;
15566 case DW_TAG_reference_type:
f792889a 15567 this_type = read_tag_reference_type (die, cu);
c906108c
SS
15568 break;
15569 case DW_TAG_const_type:
f792889a 15570 this_type = read_tag_const_type (die, cu);
c906108c
SS
15571 break;
15572 case DW_TAG_volatile_type:
f792889a 15573 this_type = read_tag_volatile_type (die, cu);
c906108c
SS
15574 break;
15575 case DW_TAG_string_type:
f792889a 15576 this_type = read_tag_string_type (die, cu);
c906108c
SS
15577 break;
15578 case DW_TAG_typedef:
f792889a 15579 this_type = read_typedef (die, cu);
c906108c 15580 break;
a02abb62 15581 case DW_TAG_subrange_type:
f792889a 15582 this_type = read_subrange_type (die, cu);
a02abb62 15583 break;
c906108c 15584 case DW_TAG_base_type:
f792889a 15585 this_type = read_base_type (die, cu);
c906108c 15586 break;
81a17f79 15587 case DW_TAG_unspecified_type:
f792889a 15588 this_type = read_unspecified_type (die, cu);
81a17f79 15589 break;
0114d602
DJ
15590 case DW_TAG_namespace:
15591 this_type = read_namespace_type (die, cu);
15592 break;
f55ee35c
JK
15593 case DW_TAG_module:
15594 this_type = read_module_type (die, cu);
15595 break;
c906108c 15596 default:
3e43a32a
MS
15597 complaint (&symfile_complaints,
15598 _("unexpected tag in read_type_die: '%s'"),
4d3c2250 15599 dwarf_tag_name (die->tag));
c906108c
SS
15600 break;
15601 }
63d06c5c 15602
f792889a 15603 return this_type;
63d06c5c
DC
15604}
15605
abc72ce4
DE
15606/* See if we can figure out if the class lives in a namespace. We do
15607 this by looking for a member function; its demangled name will
15608 contain namespace info, if there is any.
15609 Return the computed name or NULL.
15610 Space for the result is allocated on the objfile's obstack.
15611 This is the full-die version of guess_partial_die_structure_name.
15612 In this case we know DIE has no useful parent. */
15613
15614static char *
15615guess_full_die_structure_name (struct die_info *die, struct dwarf2_cu *cu)
15616{
15617 struct die_info *spec_die;
15618 struct dwarf2_cu *spec_cu;
15619 struct die_info *child;
15620
15621 spec_cu = cu;
15622 spec_die = die_specification (die, &spec_cu);
15623 if (spec_die != NULL)
15624 {
15625 die = spec_die;
15626 cu = spec_cu;
15627 }
15628
15629 for (child = die->child;
15630 child != NULL;
15631 child = child->sibling)
15632 {
15633 if (child->tag == DW_TAG_subprogram)
15634 {
15635 struct attribute *attr;
15636
15637 attr = dwarf2_attr (child, DW_AT_linkage_name, cu);
15638 if (attr == NULL)
15639 attr = dwarf2_attr (child, DW_AT_MIPS_linkage_name, cu);
15640 if (attr != NULL)
15641 {
15642 char *actual_name
15643 = language_class_name_from_physname (cu->language_defn,
15644 DW_STRING (attr));
15645 char *name = NULL;
15646
15647 if (actual_name != NULL)
15648 {
15649 char *die_name = dwarf2_name (die, cu);
15650
15651 if (die_name != NULL
15652 && strcmp (die_name, actual_name) != 0)
15653 {
15654 /* Strip off the class name from the full name.
15655 We want the prefix. */
15656 int die_name_len = strlen (die_name);
15657 int actual_name_len = strlen (actual_name);
15658
15659 /* Test for '::' as a sanity check. */
15660 if (actual_name_len > die_name_len + 2
3e43a32a
MS
15661 && actual_name[actual_name_len
15662 - die_name_len - 1] == ':')
abc72ce4
DE
15663 name =
15664 obsavestring (actual_name,
15665 actual_name_len - die_name_len - 2,
15666 &cu->objfile->objfile_obstack);
15667 }
15668 }
15669 xfree (actual_name);
15670 return name;
15671 }
15672 }
15673 }
15674
15675 return NULL;
15676}
15677
96408a79
SA
15678/* GCC might emit a nameless typedef that has a linkage name. Determine the
15679 prefix part in such case. See
15680 http://gcc.gnu.org/bugzilla/show_bug.cgi?id=47510. */
15681
15682static char *
15683anonymous_struct_prefix (struct die_info *die, struct dwarf2_cu *cu)
15684{
15685 struct attribute *attr;
15686 char *base;
15687
15688 if (die->tag != DW_TAG_class_type && die->tag != DW_TAG_interface_type
15689 && die->tag != DW_TAG_structure_type && die->tag != DW_TAG_union_type)
15690 return NULL;
15691
15692 attr = dwarf2_attr (die, DW_AT_name, cu);
15693 if (attr != NULL && DW_STRING (attr) != NULL)
15694 return NULL;
15695
15696 attr = dwarf2_attr (die, DW_AT_linkage_name, cu);
15697 if (attr == NULL)
15698 attr = dwarf2_attr (die, DW_AT_MIPS_linkage_name, cu);
15699 if (attr == NULL || DW_STRING (attr) == NULL)
15700 return NULL;
15701
15702 /* dwarf2_name had to be already called. */
15703 gdb_assert (DW_STRING_IS_CANONICAL (attr));
15704
15705 /* Strip the base name, keep any leading namespaces/classes. */
15706 base = strrchr (DW_STRING (attr), ':');
15707 if (base == NULL || base == DW_STRING (attr) || base[-1] != ':')
15708 return "";
15709
15710 return obsavestring (DW_STRING (attr), &base[-1] - DW_STRING (attr),
15711 &cu->objfile->objfile_obstack);
15712}
15713
fdde2d81 15714/* Return the name of the namespace/class that DIE is defined within,
0114d602 15715 or "" if we can't tell. The caller should not xfree the result.
fdde2d81 15716
0114d602
DJ
15717 For example, if we're within the method foo() in the following
15718 code:
15719
15720 namespace N {
15721 class C {
15722 void foo () {
15723 }
15724 };
15725 }
15726
15727 then determine_prefix on foo's die will return "N::C". */
fdde2d81 15728
0d5cff50 15729static const char *
e142c38c 15730determine_prefix (struct die_info *die, struct dwarf2_cu *cu)
63d06c5c 15731{
0114d602
DJ
15732 struct die_info *parent, *spec_die;
15733 struct dwarf2_cu *spec_cu;
15734 struct type *parent_type;
96408a79 15735 char *retval;
63d06c5c 15736
f55ee35c
JK
15737 if (cu->language != language_cplus && cu->language != language_java
15738 && cu->language != language_fortran)
0114d602
DJ
15739 return "";
15740
96408a79
SA
15741 retval = anonymous_struct_prefix (die, cu);
15742 if (retval)
15743 return retval;
15744
0114d602
DJ
15745 /* We have to be careful in the presence of DW_AT_specification.
15746 For example, with GCC 3.4, given the code
15747
15748 namespace N {
15749 void foo() {
15750 // Definition of N::foo.
15751 }
15752 }
15753
15754 then we'll have a tree of DIEs like this:
15755
15756 1: DW_TAG_compile_unit
15757 2: DW_TAG_namespace // N
15758 3: DW_TAG_subprogram // declaration of N::foo
15759 4: DW_TAG_subprogram // definition of N::foo
15760 DW_AT_specification // refers to die #3
15761
15762 Thus, when processing die #4, we have to pretend that we're in
15763 the context of its DW_AT_specification, namely the contex of die
15764 #3. */
15765 spec_cu = cu;
15766 spec_die = die_specification (die, &spec_cu);
15767 if (spec_die == NULL)
15768 parent = die->parent;
15769 else
63d06c5c 15770 {
0114d602
DJ
15771 parent = spec_die->parent;
15772 cu = spec_cu;
63d06c5c 15773 }
0114d602
DJ
15774
15775 if (parent == NULL)
15776 return "";
98bfdba5
PA
15777 else if (parent->building_fullname)
15778 {
15779 const char *name;
15780 const char *parent_name;
15781
15782 /* It has been seen on RealView 2.2 built binaries,
15783 DW_TAG_template_type_param types actually _defined_ as
15784 children of the parent class:
15785
15786 enum E {};
15787 template class <class Enum> Class{};
15788 Class<enum E> class_e;
15789
15790 1: DW_TAG_class_type (Class)
15791 2: DW_TAG_enumeration_type (E)
15792 3: DW_TAG_enumerator (enum1:0)
15793 3: DW_TAG_enumerator (enum2:1)
15794 ...
15795 2: DW_TAG_template_type_param
15796 DW_AT_type DW_FORM_ref_udata (E)
15797
15798 Besides being broken debug info, it can put GDB into an
15799 infinite loop. Consider:
15800
15801 When we're building the full name for Class<E>, we'll start
15802 at Class, and go look over its template type parameters,
15803 finding E. We'll then try to build the full name of E, and
15804 reach here. We're now trying to build the full name of E,
15805 and look over the parent DIE for containing scope. In the
15806 broken case, if we followed the parent DIE of E, we'd again
15807 find Class, and once again go look at its template type
15808 arguments, etc., etc. Simply don't consider such parent die
15809 as source-level parent of this die (it can't be, the language
15810 doesn't allow it), and break the loop here. */
15811 name = dwarf2_name (die, cu);
15812 parent_name = dwarf2_name (parent, cu);
15813 complaint (&symfile_complaints,
15814 _("template param type '%s' defined within parent '%s'"),
15815 name ? name : "<unknown>",
15816 parent_name ? parent_name : "<unknown>");
15817 return "";
15818 }
63d06c5c 15819 else
0114d602
DJ
15820 switch (parent->tag)
15821 {
63d06c5c 15822 case DW_TAG_namespace:
0114d602 15823 parent_type = read_type_die (parent, cu);
acebe513
UW
15824 /* GCC 4.0 and 4.1 had a bug (PR c++/28460) where they generated bogus
15825 DW_TAG_namespace DIEs with a name of "::" for the global namespace.
15826 Work around this problem here. */
15827 if (cu->language == language_cplus
15828 && strcmp (TYPE_TAG_NAME (parent_type), "::") == 0)
15829 return "";
0114d602
DJ
15830 /* We give a name to even anonymous namespaces. */
15831 return TYPE_TAG_NAME (parent_type);
63d06c5c 15832 case DW_TAG_class_type:
680b30c7 15833 case DW_TAG_interface_type:
63d06c5c 15834 case DW_TAG_structure_type:
0114d602 15835 case DW_TAG_union_type:
f55ee35c 15836 case DW_TAG_module:
0114d602
DJ
15837 parent_type = read_type_die (parent, cu);
15838 if (TYPE_TAG_NAME (parent_type) != NULL)
15839 return TYPE_TAG_NAME (parent_type);
15840 else
15841 /* An anonymous structure is only allowed non-static data
15842 members; no typedefs, no member functions, et cetera.
15843 So it does not need a prefix. */
15844 return "";
abc72ce4 15845 case DW_TAG_compile_unit:
95554aad 15846 case DW_TAG_partial_unit:
abc72ce4
DE
15847 /* gcc-4.5 -gdwarf-4 can drop the enclosing namespace. Cope. */
15848 if (cu->language == language_cplus
8b70b953 15849 && !VEC_empty (dwarf2_section_info_def, dwarf2_per_objfile->types)
abc72ce4
DE
15850 && die->child != NULL
15851 && (die->tag == DW_TAG_class_type
15852 || die->tag == DW_TAG_structure_type
15853 || die->tag == DW_TAG_union_type))
15854 {
15855 char *name = guess_full_die_structure_name (die, cu);
15856 if (name != NULL)
15857 return name;
15858 }
15859 return "";
63d06c5c 15860 default:
8176b9b8 15861 return determine_prefix (parent, cu);
63d06c5c 15862 }
63d06c5c
DC
15863}
15864
3e43a32a
MS
15865/* Return a newly-allocated string formed by concatenating PREFIX and SUFFIX
15866 with appropriate separator. If PREFIX or SUFFIX is NULL or empty, then
15867 simply copy the SUFFIX or PREFIX, respectively. If OBS is non-null, perform
15868 an obconcat, otherwise allocate storage for the result. The CU argument is
15869 used to determine the language and hence, the appropriate separator. */
987504bb 15870
f55ee35c 15871#define MAX_SEP_LEN 7 /* strlen ("__") + strlen ("_MOD_") */
63d06c5c
DC
15872
15873static char *
f55ee35c
JK
15874typename_concat (struct obstack *obs, const char *prefix, const char *suffix,
15875 int physname, struct dwarf2_cu *cu)
63d06c5c 15876{
f55ee35c 15877 const char *lead = "";
5c315b68 15878 const char *sep;
63d06c5c 15879
3e43a32a
MS
15880 if (suffix == NULL || suffix[0] == '\0'
15881 || prefix == NULL || prefix[0] == '\0')
987504bb
JJ
15882 sep = "";
15883 else if (cu->language == language_java)
15884 sep = ".";
f55ee35c
JK
15885 else if (cu->language == language_fortran && physname)
15886 {
15887 /* This is gfortran specific mangling. Normally DW_AT_linkage_name or
15888 DW_AT_MIPS_linkage_name is preferred and used instead. */
15889
15890 lead = "__";
15891 sep = "_MOD_";
15892 }
987504bb
JJ
15893 else
15894 sep = "::";
63d06c5c 15895
6dd47d34
DE
15896 if (prefix == NULL)
15897 prefix = "";
15898 if (suffix == NULL)
15899 suffix = "";
15900
987504bb
JJ
15901 if (obs == NULL)
15902 {
3e43a32a
MS
15903 char *retval
15904 = xmalloc (strlen (prefix) + MAX_SEP_LEN + strlen (suffix) + 1);
9a619af0 15905
f55ee35c
JK
15906 strcpy (retval, lead);
15907 strcat (retval, prefix);
6dd47d34
DE
15908 strcat (retval, sep);
15909 strcat (retval, suffix);
63d06c5c
DC
15910 return retval;
15911 }
987504bb
JJ
15912 else
15913 {
15914 /* We have an obstack. */
f55ee35c 15915 return obconcat (obs, lead, prefix, sep, suffix, (char *) NULL);
987504bb 15916 }
63d06c5c
DC
15917}
15918
c906108c
SS
15919/* Return sibling of die, NULL if no sibling. */
15920
f9aca02d 15921static struct die_info *
fba45db2 15922sibling_die (struct die_info *die)
c906108c 15923{
639d11d3 15924 return die->sibling;
c906108c
SS
15925}
15926
71c25dea
TT
15927/* Get name of a die, return NULL if not found. */
15928
15929static char *
15930dwarf2_canonicalize_name (char *name, struct dwarf2_cu *cu,
15931 struct obstack *obstack)
15932{
15933 if (name && cu->language == language_cplus)
15934 {
15935 char *canon_name = cp_canonicalize_string (name);
15936
15937 if (canon_name != NULL)
15938 {
15939 if (strcmp (canon_name, name) != 0)
15940 name = obsavestring (canon_name, strlen (canon_name),
15941 obstack);
15942 xfree (canon_name);
15943 }
15944 }
15945
15946 return name;
c906108c
SS
15947}
15948
9219021c
DC
15949/* Get name of a die, return NULL if not found. */
15950
15951static char *
e142c38c 15952dwarf2_name (struct die_info *die, struct dwarf2_cu *cu)
9219021c
DC
15953{
15954 struct attribute *attr;
15955
e142c38c 15956 attr = dwarf2_attr (die, DW_AT_name, cu);
53832f31
TT
15957 if ((!attr || !DW_STRING (attr))
15958 && die->tag != DW_TAG_class_type
15959 && die->tag != DW_TAG_interface_type
15960 && die->tag != DW_TAG_structure_type
15961 && die->tag != DW_TAG_union_type)
71c25dea
TT
15962 return NULL;
15963
15964 switch (die->tag)
15965 {
15966 case DW_TAG_compile_unit:
95554aad 15967 case DW_TAG_partial_unit:
71c25dea
TT
15968 /* Compilation units have a DW_AT_name that is a filename, not
15969 a source language identifier. */
15970 case DW_TAG_enumeration_type:
15971 case DW_TAG_enumerator:
15972 /* These tags always have simple identifiers already; no need
15973 to canonicalize them. */
15974 return DW_STRING (attr);
907af001 15975
418835cc
KS
15976 case DW_TAG_subprogram:
15977 /* Java constructors will all be named "<init>", so return
15978 the class name when we see this special case. */
15979 if (cu->language == language_java
15980 && DW_STRING (attr) != NULL
15981 && strcmp (DW_STRING (attr), "<init>") == 0)
15982 {
15983 struct dwarf2_cu *spec_cu = cu;
15984 struct die_info *spec_die;
15985
15986 /* GCJ will output '<init>' for Java constructor names.
15987 For this special case, return the name of the parent class. */
15988
15989 /* GCJ may output suprogram DIEs with AT_specification set.
15990 If so, use the name of the specified DIE. */
15991 spec_die = die_specification (die, &spec_cu);
15992 if (spec_die != NULL)
15993 return dwarf2_name (spec_die, spec_cu);
15994
15995 do
15996 {
15997 die = die->parent;
15998 if (die->tag == DW_TAG_class_type)
15999 return dwarf2_name (die, cu);
16000 }
95554aad
TT
16001 while (die->tag != DW_TAG_compile_unit
16002 && die->tag != DW_TAG_partial_unit);
418835cc 16003 }
907af001
UW
16004 break;
16005
16006 case DW_TAG_class_type:
16007 case DW_TAG_interface_type:
16008 case DW_TAG_structure_type:
16009 case DW_TAG_union_type:
16010 /* Some GCC versions emit spurious DW_AT_name attributes for unnamed
16011 structures or unions. These were of the form "._%d" in GCC 4.1,
16012 or simply "<anonymous struct>" or "<anonymous union>" in GCC 4.3
16013 and GCC 4.4. We work around this problem by ignoring these. */
53832f31
TT
16014 if (attr && DW_STRING (attr)
16015 && (strncmp (DW_STRING (attr), "._", 2) == 0
16016 || strncmp (DW_STRING (attr), "<anonymous", 10) == 0))
907af001 16017 return NULL;
53832f31
TT
16018
16019 /* GCC might emit a nameless typedef that has a linkage name. See
16020 http://gcc.gnu.org/bugzilla/show_bug.cgi?id=47510. */
16021 if (!attr || DW_STRING (attr) == NULL)
16022 {
df5c6c50 16023 char *demangled = NULL;
53832f31
TT
16024
16025 attr = dwarf2_attr (die, DW_AT_linkage_name, cu);
16026 if (attr == NULL)
16027 attr = dwarf2_attr (die, DW_AT_MIPS_linkage_name, cu);
16028
16029 if (attr == NULL || DW_STRING (attr) == NULL)
16030 return NULL;
16031
df5c6c50
JK
16032 /* Avoid demangling DW_STRING (attr) the second time on a second
16033 call for the same DIE. */
16034 if (!DW_STRING_IS_CANONICAL (attr))
16035 demangled = cplus_demangle (DW_STRING (attr), DMGL_TYPES);
53832f31
TT
16036
16037 if (demangled)
16038 {
96408a79
SA
16039 char *base;
16040
53832f31 16041 /* FIXME: we already did this for the partial symbol... */
96408a79
SA
16042 DW_STRING (attr) = obsavestring (demangled, strlen (demangled),
16043 &cu->objfile->objfile_obstack);
53832f31
TT
16044 DW_STRING_IS_CANONICAL (attr) = 1;
16045 xfree (demangled);
96408a79
SA
16046
16047 /* Strip any leading namespaces/classes, keep only the base name.
16048 DW_AT_name for named DIEs does not contain the prefixes. */
16049 base = strrchr (DW_STRING (attr), ':');
16050 if (base && base > DW_STRING (attr) && base[-1] == ':')
16051 return &base[1];
16052 else
16053 return DW_STRING (attr);
53832f31
TT
16054 }
16055 }
907af001
UW
16056 break;
16057
71c25dea 16058 default:
907af001
UW
16059 break;
16060 }
16061
16062 if (!DW_STRING_IS_CANONICAL (attr))
16063 {
16064 DW_STRING (attr)
16065 = dwarf2_canonicalize_name (DW_STRING (attr), cu,
16066 &cu->objfile->objfile_obstack);
16067 DW_STRING_IS_CANONICAL (attr) = 1;
71c25dea 16068 }
907af001 16069 return DW_STRING (attr);
9219021c
DC
16070}
16071
16072/* Return the die that this die in an extension of, or NULL if there
f2f0e013
DJ
16073 is none. *EXT_CU is the CU containing DIE on input, and the CU
16074 containing the return value on output. */
9219021c
DC
16075
16076static struct die_info *
f2f0e013 16077dwarf2_extension (struct die_info *die, struct dwarf2_cu **ext_cu)
9219021c
DC
16078{
16079 struct attribute *attr;
9219021c 16080
f2f0e013 16081 attr = dwarf2_attr (die, DW_AT_extension, *ext_cu);
9219021c
DC
16082 if (attr == NULL)
16083 return NULL;
16084
f2f0e013 16085 return follow_die_ref (die, attr, ext_cu);
9219021c
DC
16086}
16087
c906108c
SS
16088/* Convert a DIE tag into its string name. */
16089
f39c6ffd 16090static const char *
aa1ee363 16091dwarf_tag_name (unsigned tag)
c906108c 16092{
f39c6ffd
TT
16093 const char *name = get_DW_TAG_name (tag);
16094
16095 if (name == NULL)
16096 return "DW_TAG_<unknown>";
16097
16098 return name;
c906108c
SS
16099}
16100
16101/* Convert a DWARF attribute code into its string name. */
16102
f39c6ffd 16103static const char *
aa1ee363 16104dwarf_attr_name (unsigned attr)
c906108c 16105{
f39c6ffd
TT
16106 const char *name;
16107
c764a876 16108#ifdef MIPS /* collides with DW_AT_HP_block_index */
f39c6ffd
TT
16109 if (attr == DW_AT_MIPS_fde)
16110 return "DW_AT_MIPS_fde";
16111#else
16112 if (attr == DW_AT_HP_block_index)
16113 return "DW_AT_HP_block_index";
c764a876 16114#endif
f39c6ffd
TT
16115
16116 name = get_DW_AT_name (attr);
16117
16118 if (name == NULL)
16119 return "DW_AT_<unknown>";
16120
16121 return name;
c906108c
SS
16122}
16123
16124/* Convert a DWARF value form code into its string name. */
16125
f39c6ffd 16126static const char *
aa1ee363 16127dwarf_form_name (unsigned form)
c906108c 16128{
f39c6ffd
TT
16129 const char *name = get_DW_FORM_name (form);
16130
16131 if (name == NULL)
16132 return "DW_FORM_<unknown>";
16133
16134 return name;
c906108c
SS
16135}
16136
16137static char *
fba45db2 16138dwarf_bool_name (unsigned mybool)
c906108c
SS
16139{
16140 if (mybool)
16141 return "TRUE";
16142 else
16143 return "FALSE";
16144}
16145
16146/* Convert a DWARF type code into its string name. */
16147
f39c6ffd 16148static const char *
aa1ee363 16149dwarf_type_encoding_name (unsigned enc)
c906108c 16150{
f39c6ffd 16151 const char *name = get_DW_ATE_name (enc);
c906108c 16152
f39c6ffd
TT
16153 if (name == NULL)
16154 return "DW_ATE_<unknown>";
c906108c 16155
f39c6ffd 16156 return name;
c906108c 16157}
c906108c 16158
f9aca02d 16159static void
d97bc12b 16160dump_die_shallow (struct ui_file *f, int indent, struct die_info *die)
c906108c
SS
16161{
16162 unsigned int i;
16163
d97bc12b
DE
16164 print_spaces (indent, f);
16165 fprintf_unfiltered (f, "Die: %s (abbrev %d, offset 0x%x)\n",
b64f50a1 16166 dwarf_tag_name (die->tag), die->abbrev, die->offset.sect_off);
d97bc12b
DE
16167
16168 if (die->parent != NULL)
16169 {
16170 print_spaces (indent, f);
16171 fprintf_unfiltered (f, " parent at offset: 0x%x\n",
b64f50a1 16172 die->parent->offset.sect_off);
d97bc12b
DE
16173 }
16174
16175 print_spaces (indent, f);
16176 fprintf_unfiltered (f, " has children: %s\n",
639d11d3 16177 dwarf_bool_name (die->child != NULL));
c906108c 16178
d97bc12b
DE
16179 print_spaces (indent, f);
16180 fprintf_unfiltered (f, " attributes:\n");
16181
c906108c
SS
16182 for (i = 0; i < die->num_attrs; ++i)
16183 {
d97bc12b
DE
16184 print_spaces (indent, f);
16185 fprintf_unfiltered (f, " %s (%s) ",
c906108c
SS
16186 dwarf_attr_name (die->attrs[i].name),
16187 dwarf_form_name (die->attrs[i].form));
d97bc12b 16188
c906108c
SS
16189 switch (die->attrs[i].form)
16190 {
c906108c 16191 case DW_FORM_addr:
3019eac3 16192 case DW_FORM_GNU_addr_index:
d97bc12b 16193 fprintf_unfiltered (f, "address: ");
5af949e3 16194 fputs_filtered (hex_string (DW_ADDR (&die->attrs[i])), f);
c906108c
SS
16195 break;
16196 case DW_FORM_block2:
16197 case DW_FORM_block4:
16198 case DW_FORM_block:
16199 case DW_FORM_block1:
56eb65bd
SP
16200 fprintf_unfiltered (f, "block: size %s",
16201 pulongest (DW_BLOCK (&die->attrs[i])->size));
c906108c 16202 break;
2dc7f7b3 16203 case DW_FORM_exprloc:
56eb65bd
SP
16204 fprintf_unfiltered (f, "expression: size %s",
16205 pulongest (DW_BLOCK (&die->attrs[i])->size));
2dc7f7b3 16206 break;
4568ecf9
DE
16207 case DW_FORM_ref_addr:
16208 fprintf_unfiltered (f, "ref address: ");
16209 fputs_filtered (hex_string (DW_UNSND (&die->attrs[i])), f);
16210 break;
36586728
TT
16211 case DW_FORM_GNU_ref_alt:
16212 fprintf_unfiltered (f, "alt ref address: ");
16213 fputs_filtered (hex_string (DW_UNSND (&die->attrs[i])), f);
16214 break;
10b3939b
DJ
16215 case DW_FORM_ref1:
16216 case DW_FORM_ref2:
16217 case DW_FORM_ref4:
4568ecf9
DE
16218 case DW_FORM_ref8:
16219 case DW_FORM_ref_udata:
d97bc12b 16220 fprintf_unfiltered (f, "constant ref: 0x%lx (adjusted)",
4568ecf9 16221 (long) (DW_UNSND (&die->attrs[i])));
10b3939b 16222 break;
c906108c
SS
16223 case DW_FORM_data1:
16224 case DW_FORM_data2:
16225 case DW_FORM_data4:
ce5d95e1 16226 case DW_FORM_data8:
c906108c
SS
16227 case DW_FORM_udata:
16228 case DW_FORM_sdata:
43bbcdc2
PH
16229 fprintf_unfiltered (f, "constant: %s",
16230 pulongest (DW_UNSND (&die->attrs[i])));
c906108c 16231 break;
2dc7f7b3
TT
16232 case DW_FORM_sec_offset:
16233 fprintf_unfiltered (f, "section offset: %s",
16234 pulongest (DW_UNSND (&die->attrs[i])));
16235 break;
55f1336d 16236 case DW_FORM_ref_sig8:
348e048f
DE
16237 if (DW_SIGNATURED_TYPE (&die->attrs[i]) != NULL)
16238 fprintf_unfiltered (f, "signatured type, offset: 0x%x",
b64f50a1 16239 DW_SIGNATURED_TYPE (&die->attrs[i])->per_cu.offset.sect_off);
348e048f
DE
16240 else
16241 fprintf_unfiltered (f, "signatured type, offset: unknown");
16242 break;
c906108c 16243 case DW_FORM_string:
4bdf3d34 16244 case DW_FORM_strp:
3019eac3 16245 case DW_FORM_GNU_str_index:
36586728 16246 case DW_FORM_GNU_strp_alt:
8285870a 16247 fprintf_unfiltered (f, "string: \"%s\" (%s canonicalized)",
c906108c 16248 DW_STRING (&die->attrs[i])
8285870a
JK
16249 ? DW_STRING (&die->attrs[i]) : "",
16250 DW_STRING_IS_CANONICAL (&die->attrs[i]) ? "is" : "not");
c906108c
SS
16251 break;
16252 case DW_FORM_flag:
16253 if (DW_UNSND (&die->attrs[i]))
d97bc12b 16254 fprintf_unfiltered (f, "flag: TRUE");
c906108c 16255 else
d97bc12b 16256 fprintf_unfiltered (f, "flag: FALSE");
c906108c 16257 break;
2dc7f7b3
TT
16258 case DW_FORM_flag_present:
16259 fprintf_unfiltered (f, "flag: TRUE");
16260 break;
a8329558 16261 case DW_FORM_indirect:
0963b4bd
MS
16262 /* The reader will have reduced the indirect form to
16263 the "base form" so this form should not occur. */
3e43a32a
MS
16264 fprintf_unfiltered (f,
16265 "unexpected attribute form: DW_FORM_indirect");
a8329558 16266 break;
c906108c 16267 default:
d97bc12b 16268 fprintf_unfiltered (f, "unsupported attribute form: %d.",
c5aa993b 16269 die->attrs[i].form);
d97bc12b 16270 break;
c906108c 16271 }
d97bc12b 16272 fprintf_unfiltered (f, "\n");
c906108c
SS
16273 }
16274}
16275
f9aca02d 16276static void
d97bc12b 16277dump_die_for_error (struct die_info *die)
c906108c 16278{
d97bc12b
DE
16279 dump_die_shallow (gdb_stderr, 0, die);
16280}
16281
16282static void
16283dump_die_1 (struct ui_file *f, int level, int max_level, struct die_info *die)
16284{
16285 int indent = level * 4;
16286
16287 gdb_assert (die != NULL);
16288
16289 if (level >= max_level)
16290 return;
16291
16292 dump_die_shallow (f, indent, die);
16293
16294 if (die->child != NULL)
c906108c 16295 {
d97bc12b
DE
16296 print_spaces (indent, f);
16297 fprintf_unfiltered (f, " Children:");
16298 if (level + 1 < max_level)
16299 {
16300 fprintf_unfiltered (f, "\n");
16301 dump_die_1 (f, level + 1, max_level, die->child);
16302 }
16303 else
16304 {
3e43a32a
MS
16305 fprintf_unfiltered (f,
16306 " [not printed, max nesting level reached]\n");
d97bc12b
DE
16307 }
16308 }
16309
16310 if (die->sibling != NULL && level > 0)
16311 {
16312 dump_die_1 (f, level, max_level, die->sibling);
c906108c
SS
16313 }
16314}
16315
d97bc12b
DE
16316/* This is called from the pdie macro in gdbinit.in.
16317 It's not static so gcc will keep a copy callable from gdb. */
16318
16319void
16320dump_die (struct die_info *die, int max_level)
16321{
16322 dump_die_1 (gdb_stdlog, 0, max_level, die);
16323}
16324
f9aca02d 16325static void
51545339 16326store_in_ref_table (struct die_info *die, struct dwarf2_cu *cu)
c906108c 16327{
51545339 16328 void **slot;
c906108c 16329
b64f50a1
JK
16330 slot = htab_find_slot_with_hash (cu->die_hash, die, die->offset.sect_off,
16331 INSERT);
51545339
DJ
16332
16333 *slot = die;
c906108c
SS
16334}
16335
b64f50a1
JK
16336/* DW_ADDR is always stored already as sect_offset; despite for the forms
16337 besides DW_FORM_ref_addr it is stored as cu_offset in the DWARF file. */
16338
93311388
DE
16339static int
16340is_ref_attr (struct attribute *attr)
c906108c 16341{
c906108c
SS
16342 switch (attr->form)
16343 {
16344 case DW_FORM_ref_addr:
c906108c
SS
16345 case DW_FORM_ref1:
16346 case DW_FORM_ref2:
16347 case DW_FORM_ref4:
613e1657 16348 case DW_FORM_ref8:
c906108c 16349 case DW_FORM_ref_udata:
36586728 16350 case DW_FORM_GNU_ref_alt:
93311388 16351 return 1;
c906108c 16352 default:
93311388 16353 return 0;
c906108c 16354 }
93311388
DE
16355}
16356
b64f50a1
JK
16357/* Return DIE offset of ATTR. Return 0 with complaint if ATTR is not of the
16358 required kind. */
16359
16360static sect_offset
93311388
DE
16361dwarf2_get_ref_die_offset (struct attribute *attr)
16362{
4568ecf9 16363 sect_offset retval = { DW_UNSND (attr) };
b64f50a1 16364
93311388 16365 if (is_ref_attr (attr))
b64f50a1 16366 return retval;
93311388 16367
b64f50a1 16368 retval.sect_off = 0;
93311388
DE
16369 complaint (&symfile_complaints,
16370 _("unsupported die ref attribute form: '%s'"),
16371 dwarf_form_name (attr->form));
b64f50a1 16372 return retval;
c906108c
SS
16373}
16374
43bbcdc2
PH
16375/* Return the constant value held by ATTR. Return DEFAULT_VALUE if
16376 * the value held by the attribute is not constant. */
a02abb62 16377
43bbcdc2 16378static LONGEST
a02abb62
JB
16379dwarf2_get_attr_constant_value (struct attribute *attr, int default_value)
16380{
16381 if (attr->form == DW_FORM_sdata)
16382 return DW_SND (attr);
16383 else if (attr->form == DW_FORM_udata
16384 || attr->form == DW_FORM_data1
16385 || attr->form == DW_FORM_data2
16386 || attr->form == DW_FORM_data4
16387 || attr->form == DW_FORM_data8)
16388 return DW_UNSND (attr);
16389 else
16390 {
3e43a32a
MS
16391 complaint (&symfile_complaints,
16392 _("Attribute value is not a constant (%s)"),
a02abb62
JB
16393 dwarf_form_name (attr->form));
16394 return default_value;
16395 }
16396}
16397
348e048f
DE
16398/* Follow reference or signature attribute ATTR of SRC_DIE.
16399 On entry *REF_CU is the CU of SRC_DIE.
16400 On exit *REF_CU is the CU of the result. */
16401
16402static struct die_info *
16403follow_die_ref_or_sig (struct die_info *src_die, struct attribute *attr,
16404 struct dwarf2_cu **ref_cu)
16405{
16406 struct die_info *die;
16407
16408 if (is_ref_attr (attr))
16409 die = follow_die_ref (src_die, attr, ref_cu);
55f1336d 16410 else if (attr->form == DW_FORM_ref_sig8)
348e048f
DE
16411 die = follow_die_sig (src_die, attr, ref_cu);
16412 else
16413 {
16414 dump_die_for_error (src_die);
16415 error (_("Dwarf Error: Expected reference attribute [in module %s]"),
16416 (*ref_cu)->objfile->name);
16417 }
16418
16419 return die;
03dd20cc
DJ
16420}
16421
5c631832 16422/* Follow reference OFFSET.
673bfd45
DE
16423 On entry *REF_CU is the CU of the source die referencing OFFSET.
16424 On exit *REF_CU is the CU of the result.
16425 Returns NULL if OFFSET is invalid. */
f504f079 16426
f9aca02d 16427static struct die_info *
36586728
TT
16428follow_die_offset (sect_offset offset, int offset_in_dwz,
16429 struct dwarf2_cu **ref_cu)
c906108c 16430{
10b3939b 16431 struct die_info temp_die;
f2f0e013 16432 struct dwarf2_cu *target_cu, *cu = *ref_cu;
10b3939b 16433
348e048f
DE
16434 gdb_assert (cu->per_cu != NULL);
16435
98bfdba5
PA
16436 target_cu = cu;
16437
3019eac3 16438 if (cu->per_cu->is_debug_types)
348e048f
DE
16439 {
16440 /* .debug_types CUs cannot reference anything outside their CU.
16441 If they need to, they have to reference a signatured type via
55f1336d 16442 DW_FORM_ref_sig8. */
348e048f 16443 if (! offset_in_cu_p (&cu->header, offset))
5c631832 16444 return NULL;
348e048f 16445 }
36586728
TT
16446 else if (offset_in_dwz != cu->per_cu->is_dwz
16447 || ! offset_in_cu_p (&cu->header, offset))
10b3939b
DJ
16448 {
16449 struct dwarf2_per_cu_data *per_cu;
9a619af0 16450
36586728
TT
16451 per_cu = dwarf2_find_containing_comp_unit (offset, offset_in_dwz,
16452 cu->objfile);
03dd20cc
DJ
16453
16454 /* If necessary, add it to the queue and load its DIEs. */
95554aad
TT
16455 if (maybe_queue_comp_unit (cu, per_cu, cu->language))
16456 load_full_comp_unit (per_cu, cu->language);
03dd20cc 16457
10b3939b
DJ
16458 target_cu = per_cu->cu;
16459 }
98bfdba5
PA
16460 else if (cu->dies == NULL)
16461 {
16462 /* We're loading full DIEs during partial symbol reading. */
16463 gdb_assert (dwarf2_per_objfile->reading_partial_symbols);
95554aad 16464 load_full_comp_unit (cu->per_cu, language_minimal);
98bfdba5 16465 }
c906108c 16466
f2f0e013 16467 *ref_cu = target_cu;
51545339 16468 temp_die.offset = offset;
b64f50a1 16469 return htab_find_with_hash (target_cu->die_hash, &temp_die, offset.sect_off);
5c631832 16470}
10b3939b 16471
5c631832
JK
16472/* Follow reference attribute ATTR of SRC_DIE.
16473 On entry *REF_CU is the CU of SRC_DIE.
16474 On exit *REF_CU is the CU of the result. */
16475
16476static struct die_info *
16477follow_die_ref (struct die_info *src_die, struct attribute *attr,
16478 struct dwarf2_cu **ref_cu)
16479{
b64f50a1 16480 sect_offset offset = dwarf2_get_ref_die_offset (attr);
5c631832
JK
16481 struct dwarf2_cu *cu = *ref_cu;
16482 struct die_info *die;
16483
36586728
TT
16484 die = follow_die_offset (offset,
16485 (attr->form == DW_FORM_GNU_ref_alt
16486 || cu->per_cu->is_dwz),
16487 ref_cu);
5c631832
JK
16488 if (!die)
16489 error (_("Dwarf Error: Cannot find DIE at 0x%x referenced from DIE "
16490 "at 0x%x [in module %s]"),
b64f50a1 16491 offset.sect_off, src_die->offset.sect_off, cu->objfile->name);
348e048f 16492
5c631832
JK
16493 return die;
16494}
16495
d83e736b
JK
16496/* Return DWARF block referenced by DW_AT_location of DIE at OFFSET at PER_CU.
16497 Returned value is intended for DW_OP_call*. Returned
16498 dwarf2_locexpr_baton->data has lifetime of PER_CU->OBJFILE. */
5c631832
JK
16499
16500struct dwarf2_locexpr_baton
b64f50a1 16501dwarf2_fetch_die_location_block (cu_offset offset_in_cu,
8cf6f0b1
TT
16502 struct dwarf2_per_cu_data *per_cu,
16503 CORE_ADDR (*get_frame_pc) (void *baton),
16504 void *baton)
5c631832 16505{
b64f50a1 16506 sect_offset offset = { per_cu->offset.sect_off + offset_in_cu.cu_off };
918dd910 16507 struct dwarf2_cu *cu;
5c631832
JK
16508 struct die_info *die;
16509 struct attribute *attr;
16510 struct dwarf2_locexpr_baton retval;
16511
8cf6f0b1
TT
16512 dw2_setup (per_cu->objfile);
16513
918dd910
JK
16514 if (per_cu->cu == NULL)
16515 load_cu (per_cu);
16516 cu = per_cu->cu;
16517
36586728 16518 die = follow_die_offset (offset, per_cu->is_dwz, &cu);
5c631832
JK
16519 if (!die)
16520 error (_("Dwarf Error: Cannot find DIE at 0x%x referenced in module %s"),
b64f50a1 16521 offset.sect_off, per_cu->objfile->name);
5c631832
JK
16522
16523 attr = dwarf2_attr (die, DW_AT_location, cu);
16524 if (!attr)
16525 {
e103e986
JK
16526 /* DWARF: "If there is no such attribute, then there is no effect.".
16527 DATA is ignored if SIZE is 0. */
5c631832 16528
e103e986 16529 retval.data = NULL;
5c631832
JK
16530 retval.size = 0;
16531 }
8cf6f0b1
TT
16532 else if (attr_form_is_section_offset (attr))
16533 {
16534 struct dwarf2_loclist_baton loclist_baton;
16535 CORE_ADDR pc = (*get_frame_pc) (baton);
16536 size_t size;
16537
16538 fill_in_loclist_baton (cu, &loclist_baton, attr);
16539
16540 retval.data = dwarf2_find_location_expression (&loclist_baton,
16541 &size, pc);
16542 retval.size = size;
16543 }
5c631832
JK
16544 else
16545 {
16546 if (!attr_form_is_block (attr))
16547 error (_("Dwarf Error: DIE at 0x%x referenced in module %s "
16548 "is neither DW_FORM_block* nor DW_FORM_exprloc"),
b64f50a1 16549 offset.sect_off, per_cu->objfile->name);
5c631832
JK
16550
16551 retval.data = DW_BLOCK (attr)->data;
16552 retval.size = DW_BLOCK (attr)->size;
16553 }
16554 retval.per_cu = cu->per_cu;
918dd910 16555
918dd910
JK
16556 age_cached_comp_units ();
16557
5c631832 16558 return retval;
348e048f
DE
16559}
16560
8a9b8146
TT
16561/* Return the type of the DIE at DIE_OFFSET in the CU named by
16562 PER_CU. */
16563
16564struct type *
b64f50a1 16565dwarf2_get_die_type (cu_offset die_offset,
8a9b8146
TT
16566 struct dwarf2_per_cu_data *per_cu)
16567{
b64f50a1
JK
16568 sect_offset die_offset_sect;
16569
8a9b8146 16570 dw2_setup (per_cu->objfile);
b64f50a1
JK
16571
16572 die_offset_sect.sect_off = per_cu->offset.sect_off + die_offset.cu_off;
16573 return get_die_type_at_offset (die_offset_sect, per_cu);
8a9b8146
TT
16574}
16575
348e048f
DE
16576/* Follow the signature attribute ATTR in SRC_DIE.
16577 On entry *REF_CU is the CU of SRC_DIE.
16578 On exit *REF_CU is the CU of the result. */
16579
16580static struct die_info *
16581follow_die_sig (struct die_info *src_die, struct attribute *attr,
16582 struct dwarf2_cu **ref_cu)
16583{
16584 struct objfile *objfile = (*ref_cu)->objfile;
16585 struct die_info temp_die;
16586 struct signatured_type *sig_type = DW_SIGNATURED_TYPE (attr);
16587 struct dwarf2_cu *sig_cu;
16588 struct die_info *die;
16589
16590 /* sig_type will be NULL if the signatured type is missing from
16591 the debug info. */
16592 if (sig_type == NULL)
16593 error (_("Dwarf Error: Cannot find signatured DIE referenced from DIE "
16594 "at 0x%x [in module %s]"),
b64f50a1 16595 src_die->offset.sect_off, objfile->name);
348e048f
DE
16596
16597 /* If necessary, add it to the queue and load its DIEs. */
16598
95554aad 16599 if (maybe_queue_comp_unit (*ref_cu, &sig_type->per_cu, language_minimal))
a0f42c21 16600 read_signatured_type (sig_type);
348e048f
DE
16601
16602 gdb_assert (sig_type->per_cu.cu != NULL);
16603
16604 sig_cu = sig_type->per_cu.cu;
3019eac3
DE
16605 gdb_assert (sig_type->type_offset_in_section.sect_off != 0);
16606 temp_die.offset = sig_type->type_offset_in_section;
b64f50a1
JK
16607 die = htab_find_with_hash (sig_cu->die_hash, &temp_die,
16608 temp_die.offset.sect_off);
348e048f
DE
16609 if (die)
16610 {
16611 *ref_cu = sig_cu;
16612 return die;
16613 }
16614
3e43a32a
MS
16615 error (_("Dwarf Error: Cannot find signatured DIE at 0x%x referenced "
16616 "from DIE at 0x%x [in module %s]"),
b64f50a1 16617 temp_die.offset.sect_off, src_die->offset.sect_off, objfile->name);
348e048f
DE
16618}
16619
16620/* Given an offset of a signatured type, return its signatured_type. */
16621
16622static struct signatured_type *
8b70b953
TT
16623lookup_signatured_type_at_offset (struct objfile *objfile,
16624 struct dwarf2_section_info *section,
b64f50a1 16625 sect_offset offset)
348e048f 16626{
b64f50a1 16627 gdb_byte *info_ptr = section->buffer + offset.sect_off;
348e048f
DE
16628 unsigned int length, initial_length_size;
16629 unsigned int sig_offset;
52dc124a 16630 struct signatured_type find_entry, *sig_type;
348e048f
DE
16631
16632 length = read_initial_length (objfile->obfd, info_ptr, &initial_length_size);
16633 sig_offset = (initial_length_size
16634 + 2 /*version*/
16635 + (initial_length_size == 4 ? 4 : 8) /*debug_abbrev_offset*/
16636 + 1 /*address_size*/);
16637 find_entry.signature = bfd_get_64 (objfile->obfd, info_ptr + sig_offset);
52dc124a 16638 sig_type = htab_find (dwarf2_per_objfile->signatured_types, &find_entry);
348e048f
DE
16639
16640 /* This is only used to lookup previously recorded types.
16641 If we didn't find it, it's our bug. */
52dc124a
DE
16642 gdb_assert (sig_type != NULL);
16643 gdb_assert (offset.sect_off == sig_type->per_cu.offset.sect_off);
348e048f 16644
52dc124a 16645 return sig_type;
348e048f
DE
16646}
16647
e5fe5e75 16648/* Load the DIEs associated with type unit PER_CU into memory. */
348e048f
DE
16649
16650static void
e5fe5e75 16651load_full_type_unit (struct dwarf2_per_cu_data *per_cu)
348e048f 16652{
52dc124a 16653 struct signatured_type *sig_type;
348e048f 16654
f4dc4d17
DE
16655 /* Caller is responsible for ensuring type_unit_groups don't get here. */
16656 gdb_assert (! IS_TYPE_UNIT_GROUP (per_cu));
16657
6721b2ec
DE
16658 /* We have the per_cu, but we need the signatured_type.
16659 Fortunately this is an easy translation. */
16660 gdb_assert (per_cu->is_debug_types);
16661 sig_type = (struct signatured_type *) per_cu;
348e048f 16662
6721b2ec 16663 gdb_assert (per_cu->cu == NULL);
348e048f 16664
52dc124a 16665 read_signatured_type (sig_type);
348e048f 16666
6721b2ec 16667 gdb_assert (per_cu->cu != NULL);
348e048f
DE
16668}
16669
dee91e82
DE
16670/* die_reader_func for read_signatured_type.
16671 This is identical to load_full_comp_unit_reader,
16672 but is kept separate for now. */
348e048f
DE
16673
16674static void
dee91e82
DE
16675read_signatured_type_reader (const struct die_reader_specs *reader,
16676 gdb_byte *info_ptr,
16677 struct die_info *comp_unit_die,
16678 int has_children,
16679 void *data)
348e048f 16680{
dee91e82 16681 struct dwarf2_cu *cu = reader->cu;
348e048f 16682
dee91e82
DE
16683 gdb_assert (cu->die_hash == NULL);
16684 cu->die_hash =
16685 htab_create_alloc_ex (cu->header.length / 12,
16686 die_hash,
16687 die_eq,
16688 NULL,
16689 &cu->comp_unit_obstack,
16690 hashtab_obstack_allocate,
16691 dummy_obstack_deallocate);
348e048f 16692
dee91e82
DE
16693 if (has_children)
16694 comp_unit_die->child = read_die_and_siblings (reader, info_ptr,
16695 &info_ptr, comp_unit_die);
16696 cu->dies = comp_unit_die;
16697 /* comp_unit_die is not stored in die_hash, no need. */
348e048f
DE
16698
16699 /* We try not to read any attributes in this function, because not
9cdd5dbd 16700 all CUs needed for references have been loaded yet, and symbol
348e048f 16701 table processing isn't initialized. But we have to set the CU language,
dee91e82
DE
16702 or we won't be able to build types correctly.
16703 Similarly, if we do not read the producer, we can not apply
16704 producer-specific interpretation. */
95554aad 16705 prepare_one_comp_unit (cu, cu->dies, language_minimal);
dee91e82 16706}
348e048f 16707
3019eac3
DE
16708/* Read in a signatured type and build its CU and DIEs.
16709 If the type is a stub for the real type in a DWO file,
16710 read in the real type from the DWO file as well. */
dee91e82
DE
16711
16712static void
16713read_signatured_type (struct signatured_type *sig_type)
16714{
16715 struct dwarf2_per_cu_data *per_cu = &sig_type->per_cu;
348e048f 16716
3019eac3 16717 gdb_assert (per_cu->is_debug_types);
dee91e82 16718 gdb_assert (per_cu->cu == NULL);
348e048f 16719
f4dc4d17
DE
16720 init_cutu_and_read_dies (per_cu, NULL, 0, 1,
16721 read_signatured_type_reader, NULL);
c906108c
SS
16722}
16723
c906108c
SS
16724/* Decode simple location descriptions.
16725 Given a pointer to a dwarf block that defines a location, compute
16726 the location and return the value.
16727
4cecd739
DJ
16728 NOTE drow/2003-11-18: This function is called in two situations
16729 now: for the address of static or global variables (partial symbols
16730 only) and for offsets into structures which are expected to be
16731 (more or less) constant. The partial symbol case should go away,
16732 and only the constant case should remain. That will let this
16733 function complain more accurately. A few special modes are allowed
16734 without complaint for global variables (for instance, global
16735 register values and thread-local values).
c906108c
SS
16736
16737 A location description containing no operations indicates that the
4cecd739 16738 object is optimized out. The return value is 0 for that case.
6b992462
DJ
16739 FIXME drow/2003-11-16: No callers check for this case any more; soon all
16740 callers will only want a very basic result and this can become a
21ae7a4d
JK
16741 complaint.
16742
16743 Note that stack[0] is unused except as a default error return. */
c906108c
SS
16744
16745static CORE_ADDR
e7c27a73 16746decode_locdesc (struct dwarf_block *blk, struct dwarf2_cu *cu)
c906108c 16747{
e7c27a73 16748 struct objfile *objfile = cu->objfile;
56eb65bd
SP
16749 size_t i;
16750 size_t size = blk->size;
21ae7a4d
JK
16751 gdb_byte *data = blk->data;
16752 CORE_ADDR stack[64];
16753 int stacki;
16754 unsigned int bytes_read, unsnd;
16755 gdb_byte op;
c906108c 16756
21ae7a4d
JK
16757 i = 0;
16758 stacki = 0;
16759 stack[stacki] = 0;
16760 stack[++stacki] = 0;
16761
16762 while (i < size)
16763 {
16764 op = data[i++];
16765 switch (op)
16766 {
16767 case DW_OP_lit0:
16768 case DW_OP_lit1:
16769 case DW_OP_lit2:
16770 case DW_OP_lit3:
16771 case DW_OP_lit4:
16772 case DW_OP_lit5:
16773 case DW_OP_lit6:
16774 case DW_OP_lit7:
16775 case DW_OP_lit8:
16776 case DW_OP_lit9:
16777 case DW_OP_lit10:
16778 case DW_OP_lit11:
16779 case DW_OP_lit12:
16780 case DW_OP_lit13:
16781 case DW_OP_lit14:
16782 case DW_OP_lit15:
16783 case DW_OP_lit16:
16784 case DW_OP_lit17:
16785 case DW_OP_lit18:
16786 case DW_OP_lit19:
16787 case DW_OP_lit20:
16788 case DW_OP_lit21:
16789 case DW_OP_lit22:
16790 case DW_OP_lit23:
16791 case DW_OP_lit24:
16792 case DW_OP_lit25:
16793 case DW_OP_lit26:
16794 case DW_OP_lit27:
16795 case DW_OP_lit28:
16796 case DW_OP_lit29:
16797 case DW_OP_lit30:
16798 case DW_OP_lit31:
16799 stack[++stacki] = op - DW_OP_lit0;
16800 break;
f1bea926 16801
21ae7a4d
JK
16802 case DW_OP_reg0:
16803 case DW_OP_reg1:
16804 case DW_OP_reg2:
16805 case DW_OP_reg3:
16806 case DW_OP_reg4:
16807 case DW_OP_reg5:
16808 case DW_OP_reg6:
16809 case DW_OP_reg7:
16810 case DW_OP_reg8:
16811 case DW_OP_reg9:
16812 case DW_OP_reg10:
16813 case DW_OP_reg11:
16814 case DW_OP_reg12:
16815 case DW_OP_reg13:
16816 case DW_OP_reg14:
16817 case DW_OP_reg15:
16818 case DW_OP_reg16:
16819 case DW_OP_reg17:
16820 case DW_OP_reg18:
16821 case DW_OP_reg19:
16822 case DW_OP_reg20:
16823 case DW_OP_reg21:
16824 case DW_OP_reg22:
16825 case DW_OP_reg23:
16826 case DW_OP_reg24:
16827 case DW_OP_reg25:
16828 case DW_OP_reg26:
16829 case DW_OP_reg27:
16830 case DW_OP_reg28:
16831 case DW_OP_reg29:
16832 case DW_OP_reg30:
16833 case DW_OP_reg31:
16834 stack[++stacki] = op - DW_OP_reg0;
16835 if (i < size)
16836 dwarf2_complex_location_expr_complaint ();
16837 break;
c906108c 16838
21ae7a4d
JK
16839 case DW_OP_regx:
16840 unsnd = read_unsigned_leb128 (NULL, (data + i), &bytes_read);
16841 i += bytes_read;
16842 stack[++stacki] = unsnd;
16843 if (i < size)
16844 dwarf2_complex_location_expr_complaint ();
16845 break;
c906108c 16846
21ae7a4d
JK
16847 case DW_OP_addr:
16848 stack[++stacki] = read_address (objfile->obfd, &data[i],
16849 cu, &bytes_read);
16850 i += bytes_read;
16851 break;
d53d4ac5 16852
21ae7a4d
JK
16853 case DW_OP_const1u:
16854 stack[++stacki] = read_1_byte (objfile->obfd, &data[i]);
16855 i += 1;
16856 break;
16857
16858 case DW_OP_const1s:
16859 stack[++stacki] = read_1_signed_byte (objfile->obfd, &data[i]);
16860 i += 1;
16861 break;
16862
16863 case DW_OP_const2u:
16864 stack[++stacki] = read_2_bytes (objfile->obfd, &data[i]);
16865 i += 2;
16866 break;
16867
16868 case DW_OP_const2s:
16869 stack[++stacki] = read_2_signed_bytes (objfile->obfd, &data[i]);
16870 i += 2;
16871 break;
d53d4ac5 16872
21ae7a4d
JK
16873 case DW_OP_const4u:
16874 stack[++stacki] = read_4_bytes (objfile->obfd, &data[i]);
16875 i += 4;
16876 break;
16877
16878 case DW_OP_const4s:
16879 stack[++stacki] = read_4_signed_bytes (objfile->obfd, &data[i]);
16880 i += 4;
16881 break;
16882
585861ea
JK
16883 case DW_OP_const8u:
16884 stack[++stacki] = read_8_bytes (objfile->obfd, &data[i]);
16885 i += 8;
16886 break;
16887
21ae7a4d
JK
16888 case DW_OP_constu:
16889 stack[++stacki] = read_unsigned_leb128 (NULL, (data + i),
16890 &bytes_read);
16891 i += bytes_read;
16892 break;
16893
16894 case DW_OP_consts:
16895 stack[++stacki] = read_signed_leb128 (NULL, (data + i), &bytes_read);
16896 i += bytes_read;
16897 break;
16898
16899 case DW_OP_dup:
16900 stack[stacki + 1] = stack[stacki];
16901 stacki++;
16902 break;
16903
16904 case DW_OP_plus:
16905 stack[stacki - 1] += stack[stacki];
16906 stacki--;
16907 break;
16908
16909 case DW_OP_plus_uconst:
16910 stack[stacki] += read_unsigned_leb128 (NULL, (data + i),
16911 &bytes_read);
16912 i += bytes_read;
16913 break;
16914
16915 case DW_OP_minus:
16916 stack[stacki - 1] -= stack[stacki];
16917 stacki--;
16918 break;
16919
16920 case DW_OP_deref:
16921 /* If we're not the last op, then we definitely can't encode
16922 this using GDB's address_class enum. This is valid for partial
16923 global symbols, although the variable's address will be bogus
16924 in the psymtab. */
16925 if (i < size)
16926 dwarf2_complex_location_expr_complaint ();
16927 break;
16928
16929 case DW_OP_GNU_push_tls_address:
16930 /* The top of the stack has the offset from the beginning
16931 of the thread control block at which the variable is located. */
16932 /* Nothing should follow this operator, so the top of stack would
16933 be returned. */
16934 /* This is valid for partial global symbols, but the variable's
585861ea
JK
16935 address will be bogus in the psymtab. Make it always at least
16936 non-zero to not look as a variable garbage collected by linker
16937 which have DW_OP_addr 0. */
21ae7a4d
JK
16938 if (i < size)
16939 dwarf2_complex_location_expr_complaint ();
585861ea 16940 stack[stacki]++;
21ae7a4d
JK
16941 break;
16942
16943 case DW_OP_GNU_uninit:
16944 break;
16945
3019eac3 16946 case DW_OP_GNU_addr_index:
49f6c839 16947 case DW_OP_GNU_const_index:
3019eac3
DE
16948 stack[++stacki] = read_addr_index_from_leb128 (cu, &data[i],
16949 &bytes_read);
16950 i += bytes_read;
16951 break;
16952
21ae7a4d
JK
16953 default:
16954 {
f39c6ffd 16955 const char *name = get_DW_OP_name (op);
21ae7a4d
JK
16956
16957 if (name)
16958 complaint (&symfile_complaints, _("unsupported stack op: '%s'"),
16959 name);
16960 else
16961 complaint (&symfile_complaints, _("unsupported stack op: '%02x'"),
16962 op);
16963 }
16964
16965 return (stack[stacki]);
d53d4ac5 16966 }
3c6e0cb3 16967
21ae7a4d
JK
16968 /* Enforce maximum stack depth of SIZE-1 to avoid writing
16969 outside of the allocated space. Also enforce minimum>0. */
16970 if (stacki >= ARRAY_SIZE (stack) - 1)
16971 {
16972 complaint (&symfile_complaints,
16973 _("location description stack overflow"));
16974 return 0;
16975 }
16976
16977 if (stacki <= 0)
16978 {
16979 complaint (&symfile_complaints,
16980 _("location description stack underflow"));
16981 return 0;
16982 }
16983 }
16984 return (stack[stacki]);
c906108c
SS
16985}
16986
16987/* memory allocation interface */
16988
c906108c 16989static struct dwarf_block *
7b5a2f43 16990dwarf_alloc_block (struct dwarf2_cu *cu)
c906108c
SS
16991{
16992 struct dwarf_block *blk;
16993
16994 blk = (struct dwarf_block *)
7b5a2f43 16995 obstack_alloc (&cu->comp_unit_obstack, sizeof (struct dwarf_block));
c906108c
SS
16996 return (blk);
16997}
16998
c906108c 16999static struct die_info *
b60c80d6 17000dwarf_alloc_die (struct dwarf2_cu *cu, int num_attrs)
c906108c
SS
17001{
17002 struct die_info *die;
b60c80d6
DJ
17003 size_t size = sizeof (struct die_info);
17004
17005 if (num_attrs > 1)
17006 size += (num_attrs - 1) * sizeof (struct attribute);
c906108c 17007
b60c80d6 17008 die = (struct die_info *) obstack_alloc (&cu->comp_unit_obstack, size);
c906108c
SS
17009 memset (die, 0, sizeof (struct die_info));
17010 return (die);
17011}
2e276125
JB
17012
17013\f
17014/* Macro support. */
17015
2e276125
JB
17016/* Return the full name of file number I in *LH's file name table.
17017 Use COMP_DIR as the name of the current directory of the
17018 compilation. The result is allocated using xmalloc; the caller is
17019 responsible for freeing it. */
17020static char *
17021file_full_name (int file, struct line_header *lh, const char *comp_dir)
17022{
6a83a1e6
EZ
17023 /* Is the file number a valid index into the line header's file name
17024 table? Remember that file numbers start with one, not zero. */
17025 if (1 <= file && file <= lh->num_file_names)
17026 {
17027 struct file_entry *fe = &lh->file_names[file - 1];
6e70227d 17028
6a83a1e6
EZ
17029 if (IS_ABSOLUTE_PATH (fe->name))
17030 return xstrdup (fe->name);
17031 else
17032 {
17033 const char *dir;
17034 int dir_len;
17035 char *full_name;
17036
17037 if (fe->dir_index)
17038 dir = lh->include_dirs[fe->dir_index - 1];
17039 else
17040 dir = comp_dir;
17041
17042 if (dir)
17043 {
17044 dir_len = strlen (dir);
17045 full_name = xmalloc (dir_len + 1 + strlen (fe->name) + 1);
17046 strcpy (full_name, dir);
17047 full_name[dir_len] = '/';
17048 strcpy (full_name + dir_len + 1, fe->name);
17049 return full_name;
17050 }
17051 else
17052 return xstrdup (fe->name);
17053 }
17054 }
2e276125
JB
17055 else
17056 {
6a83a1e6
EZ
17057 /* The compiler produced a bogus file number. We can at least
17058 record the macro definitions made in the file, even if we
17059 won't be able to find the file by name. */
17060 char fake_name[80];
9a619af0 17061
6a83a1e6 17062 sprintf (fake_name, "<bad macro file number %d>", file);
2e276125 17063
6e70227d 17064 complaint (&symfile_complaints,
6a83a1e6
EZ
17065 _("bad file number in macro information (%d)"),
17066 file);
2e276125 17067
6a83a1e6 17068 return xstrdup (fake_name);
2e276125
JB
17069 }
17070}
17071
17072
17073static struct macro_source_file *
17074macro_start_file (int file, int line,
17075 struct macro_source_file *current_file,
17076 const char *comp_dir,
17077 struct line_header *lh, struct objfile *objfile)
17078{
17079 /* The full name of this source file. */
17080 char *full_name = file_full_name (file, lh, comp_dir);
17081
17082 /* We don't create a macro table for this compilation unit
17083 at all until we actually get a filename. */
17084 if (! pending_macros)
4a146b47 17085 pending_macros = new_macro_table (&objfile->objfile_obstack,
af5f3db6 17086 objfile->macro_cache);
2e276125
JB
17087
17088 if (! current_file)
abc9d0dc
TT
17089 {
17090 /* If we have no current file, then this must be the start_file
17091 directive for the compilation unit's main source file. */
17092 current_file = macro_set_main (pending_macros, full_name);
17093 macro_define_special (pending_macros);
17094 }
2e276125
JB
17095 else
17096 current_file = macro_include (current_file, line, full_name);
17097
17098 xfree (full_name);
6e70227d 17099
2e276125
JB
17100 return current_file;
17101}
17102
17103
17104/* Copy the LEN characters at BUF to a xmalloc'ed block of memory,
17105 followed by a null byte. */
17106static char *
17107copy_string (const char *buf, int len)
17108{
17109 char *s = xmalloc (len + 1);
9a619af0 17110
2e276125
JB
17111 memcpy (s, buf, len);
17112 s[len] = '\0';
2e276125
JB
17113 return s;
17114}
17115
17116
17117static const char *
17118consume_improper_spaces (const char *p, const char *body)
17119{
17120 if (*p == ' ')
17121 {
4d3c2250 17122 complaint (&symfile_complaints,
3e43a32a
MS
17123 _("macro definition contains spaces "
17124 "in formal argument list:\n`%s'"),
4d3c2250 17125 body);
2e276125
JB
17126
17127 while (*p == ' ')
17128 p++;
17129 }
17130
17131 return p;
17132}
17133
17134
17135static void
17136parse_macro_definition (struct macro_source_file *file, int line,
17137 const char *body)
17138{
17139 const char *p;
17140
17141 /* The body string takes one of two forms. For object-like macro
17142 definitions, it should be:
17143
17144 <macro name> " " <definition>
17145
17146 For function-like macro definitions, it should be:
17147
17148 <macro name> "() " <definition>
17149 or
17150 <macro name> "(" <arg name> ( "," <arg name> ) * ") " <definition>
17151
17152 Spaces may appear only where explicitly indicated, and in the
17153 <definition>.
17154
17155 The Dwarf 2 spec says that an object-like macro's name is always
17156 followed by a space, but versions of GCC around March 2002 omit
6e70227d 17157 the space when the macro's definition is the empty string.
2e276125
JB
17158
17159 The Dwarf 2 spec says that there should be no spaces between the
17160 formal arguments in a function-like macro's formal argument list,
17161 but versions of GCC around March 2002 include spaces after the
17162 commas. */
17163
17164
17165 /* Find the extent of the macro name. The macro name is terminated
17166 by either a space or null character (for an object-like macro) or
17167 an opening paren (for a function-like macro). */
17168 for (p = body; *p; p++)
17169 if (*p == ' ' || *p == '(')
17170 break;
17171
17172 if (*p == ' ' || *p == '\0')
17173 {
17174 /* It's an object-like macro. */
17175 int name_len = p - body;
17176 char *name = copy_string (body, name_len);
17177 const char *replacement;
17178
17179 if (*p == ' ')
17180 replacement = body + name_len + 1;
17181 else
17182 {
4d3c2250 17183 dwarf2_macro_malformed_definition_complaint (body);
2e276125
JB
17184 replacement = body + name_len;
17185 }
6e70227d 17186
2e276125
JB
17187 macro_define_object (file, line, name, replacement);
17188
17189 xfree (name);
17190 }
17191 else if (*p == '(')
17192 {
17193 /* It's a function-like macro. */
17194 char *name = copy_string (body, p - body);
17195 int argc = 0;
17196 int argv_size = 1;
17197 char **argv = xmalloc (argv_size * sizeof (*argv));
17198
17199 p++;
17200
17201 p = consume_improper_spaces (p, body);
17202
17203 /* Parse the formal argument list. */
17204 while (*p && *p != ')')
17205 {
17206 /* Find the extent of the current argument name. */
17207 const char *arg_start = p;
17208
17209 while (*p && *p != ',' && *p != ')' && *p != ' ')
17210 p++;
17211
17212 if (! *p || p == arg_start)
4d3c2250 17213 dwarf2_macro_malformed_definition_complaint (body);
2e276125
JB
17214 else
17215 {
17216 /* Make sure argv has room for the new argument. */
17217 if (argc >= argv_size)
17218 {
17219 argv_size *= 2;
17220 argv = xrealloc (argv, argv_size * sizeof (*argv));
17221 }
17222
17223 argv[argc++] = copy_string (arg_start, p - arg_start);
17224 }
17225
17226 p = consume_improper_spaces (p, body);
17227
17228 /* Consume the comma, if present. */
17229 if (*p == ',')
17230 {
17231 p++;
17232
17233 p = consume_improper_spaces (p, body);
17234 }
17235 }
17236
17237 if (*p == ')')
17238 {
17239 p++;
17240
17241 if (*p == ' ')
17242 /* Perfectly formed definition, no complaints. */
17243 macro_define_function (file, line, name,
6e70227d 17244 argc, (const char **) argv,
2e276125
JB
17245 p + 1);
17246 else if (*p == '\0')
17247 {
17248 /* Complain, but do define it. */
4d3c2250 17249 dwarf2_macro_malformed_definition_complaint (body);
2e276125 17250 macro_define_function (file, line, name,
6e70227d 17251 argc, (const char **) argv,
2e276125
JB
17252 p);
17253 }
17254 else
17255 /* Just complain. */
4d3c2250 17256 dwarf2_macro_malformed_definition_complaint (body);
2e276125
JB
17257 }
17258 else
17259 /* Just complain. */
4d3c2250 17260 dwarf2_macro_malformed_definition_complaint (body);
2e276125
JB
17261
17262 xfree (name);
17263 {
17264 int i;
17265
17266 for (i = 0; i < argc; i++)
17267 xfree (argv[i]);
17268 }
17269 xfree (argv);
17270 }
17271 else
4d3c2250 17272 dwarf2_macro_malformed_definition_complaint (body);
2e276125
JB
17273}
17274
cf2c3c16
TT
17275/* Skip some bytes from BYTES according to the form given in FORM.
17276 Returns the new pointer. */
2e276125 17277
cf2c3c16 17278static gdb_byte *
f664829e 17279skip_form_bytes (bfd *abfd, gdb_byte *bytes, gdb_byte *buffer_end,
cf2c3c16
TT
17280 enum dwarf_form form,
17281 unsigned int offset_size,
17282 struct dwarf2_section_info *section)
2e276125 17283{
cf2c3c16 17284 unsigned int bytes_read;
2e276125 17285
cf2c3c16 17286 switch (form)
2e276125 17287 {
cf2c3c16
TT
17288 case DW_FORM_data1:
17289 case DW_FORM_flag:
17290 ++bytes;
17291 break;
17292
17293 case DW_FORM_data2:
17294 bytes += 2;
17295 break;
17296
17297 case DW_FORM_data4:
17298 bytes += 4;
17299 break;
17300
17301 case DW_FORM_data8:
17302 bytes += 8;
17303 break;
17304
17305 case DW_FORM_string:
17306 read_direct_string (abfd, bytes, &bytes_read);
17307 bytes += bytes_read;
17308 break;
17309
17310 case DW_FORM_sec_offset:
17311 case DW_FORM_strp:
36586728 17312 case DW_FORM_GNU_strp_alt:
cf2c3c16
TT
17313 bytes += offset_size;
17314 break;
17315
17316 case DW_FORM_block:
17317 bytes += read_unsigned_leb128 (abfd, bytes, &bytes_read);
17318 bytes += bytes_read;
17319 break;
17320
17321 case DW_FORM_block1:
17322 bytes += 1 + read_1_byte (abfd, bytes);
17323 break;
17324 case DW_FORM_block2:
17325 bytes += 2 + read_2_bytes (abfd, bytes);
17326 break;
17327 case DW_FORM_block4:
17328 bytes += 4 + read_4_bytes (abfd, bytes);
17329 break;
17330
17331 case DW_FORM_sdata:
17332 case DW_FORM_udata:
3019eac3
DE
17333 case DW_FORM_GNU_addr_index:
17334 case DW_FORM_GNU_str_index:
f664829e
DE
17335 bytes = (gdb_byte *) gdb_skip_leb128 (bytes, buffer_end);
17336 if (bytes == NULL)
17337 {
17338 dwarf2_section_buffer_overflow_complaint (section);
17339 return NULL;
17340 }
cf2c3c16
TT
17341 break;
17342
17343 default:
17344 {
17345 complain:
17346 complaint (&symfile_complaints,
17347 _("invalid form 0x%x in `%s'"),
17348 form,
17349 section->asection->name);
17350 return NULL;
17351 }
2e276125
JB
17352 }
17353
cf2c3c16
TT
17354 return bytes;
17355}
757a13d0 17356
cf2c3c16
TT
17357/* A helper for dwarf_decode_macros that handles skipping an unknown
17358 opcode. Returns an updated pointer to the macro data buffer; or,
17359 on error, issues a complaint and returns NULL. */
757a13d0 17360
cf2c3c16
TT
17361static gdb_byte *
17362skip_unknown_opcode (unsigned int opcode,
17363 gdb_byte **opcode_definitions,
f664829e 17364 gdb_byte *mac_ptr, gdb_byte *mac_end,
cf2c3c16
TT
17365 bfd *abfd,
17366 unsigned int offset_size,
17367 struct dwarf2_section_info *section)
17368{
17369 unsigned int bytes_read, i;
17370 unsigned long arg;
17371 gdb_byte *defn;
2e276125 17372
cf2c3c16 17373 if (opcode_definitions[opcode] == NULL)
2e276125 17374 {
cf2c3c16
TT
17375 complaint (&symfile_complaints,
17376 _("unrecognized DW_MACFINO opcode 0x%x"),
17377 opcode);
17378 return NULL;
17379 }
2e276125 17380
cf2c3c16
TT
17381 defn = opcode_definitions[opcode];
17382 arg = read_unsigned_leb128 (abfd, defn, &bytes_read);
17383 defn += bytes_read;
2e276125 17384
cf2c3c16
TT
17385 for (i = 0; i < arg; ++i)
17386 {
f664829e
DE
17387 mac_ptr = skip_form_bytes (abfd, mac_ptr, mac_end, defn[i], offset_size,
17388 section);
cf2c3c16
TT
17389 if (mac_ptr == NULL)
17390 {
17391 /* skip_form_bytes already issued the complaint. */
17392 return NULL;
17393 }
17394 }
757a13d0 17395
cf2c3c16
TT
17396 return mac_ptr;
17397}
757a13d0 17398
cf2c3c16
TT
17399/* A helper function which parses the header of a macro section.
17400 If the macro section is the extended (for now called "GNU") type,
17401 then this updates *OFFSET_SIZE. Returns a pointer to just after
17402 the header, or issues a complaint and returns NULL on error. */
757a13d0 17403
cf2c3c16
TT
17404static gdb_byte *
17405dwarf_parse_macro_header (gdb_byte **opcode_definitions,
17406 bfd *abfd,
17407 gdb_byte *mac_ptr,
17408 unsigned int *offset_size,
17409 int section_is_gnu)
17410{
17411 memset (opcode_definitions, 0, 256 * sizeof (gdb_byte *));
757a13d0 17412
cf2c3c16
TT
17413 if (section_is_gnu)
17414 {
17415 unsigned int version, flags;
757a13d0 17416
cf2c3c16
TT
17417 version = read_2_bytes (abfd, mac_ptr);
17418 if (version != 4)
17419 {
17420 complaint (&symfile_complaints,
17421 _("unrecognized version `%d' in .debug_macro section"),
17422 version);
17423 return NULL;
17424 }
17425 mac_ptr += 2;
757a13d0 17426
cf2c3c16
TT
17427 flags = read_1_byte (abfd, mac_ptr);
17428 ++mac_ptr;
17429 *offset_size = (flags & 1) ? 8 : 4;
757a13d0 17430
cf2c3c16
TT
17431 if ((flags & 2) != 0)
17432 /* We don't need the line table offset. */
17433 mac_ptr += *offset_size;
757a13d0 17434
cf2c3c16
TT
17435 /* Vendor opcode descriptions. */
17436 if ((flags & 4) != 0)
17437 {
17438 unsigned int i, count;
757a13d0 17439
cf2c3c16
TT
17440 count = read_1_byte (abfd, mac_ptr);
17441 ++mac_ptr;
17442 for (i = 0; i < count; ++i)
17443 {
17444 unsigned int opcode, bytes_read;
17445 unsigned long arg;
17446
17447 opcode = read_1_byte (abfd, mac_ptr);
17448 ++mac_ptr;
17449 opcode_definitions[opcode] = mac_ptr;
17450 arg = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
17451 mac_ptr += bytes_read;
17452 mac_ptr += arg;
17453 }
757a13d0 17454 }
cf2c3c16 17455 }
757a13d0 17456
cf2c3c16
TT
17457 return mac_ptr;
17458}
757a13d0 17459
cf2c3c16 17460/* A helper for dwarf_decode_macros that handles the GNU extensions,
8fc3fc34 17461 including DW_MACRO_GNU_transparent_include. */
cf2c3c16
TT
17462
17463static void
17464dwarf_decode_macro_bytes (bfd *abfd, gdb_byte *mac_ptr, gdb_byte *mac_end,
17465 struct macro_source_file *current_file,
17466 struct line_header *lh, char *comp_dir,
17467 struct dwarf2_section_info *section,
36586728 17468 int section_is_gnu, int section_is_dwz,
cf2c3c16 17469 unsigned int offset_size,
8fc3fc34
TT
17470 struct objfile *objfile,
17471 htab_t include_hash)
cf2c3c16
TT
17472{
17473 enum dwarf_macro_record_type macinfo_type;
17474 int at_commandline;
17475 gdb_byte *opcode_definitions[256];
757a13d0 17476
cf2c3c16
TT
17477 mac_ptr = dwarf_parse_macro_header (opcode_definitions, abfd, mac_ptr,
17478 &offset_size, section_is_gnu);
17479 if (mac_ptr == NULL)
17480 {
17481 /* We already issued a complaint. */
17482 return;
17483 }
757a13d0
JK
17484
17485 /* Determines if GDB is still before first DW_MACINFO_start_file. If true
17486 GDB is still reading the definitions from command line. First
17487 DW_MACINFO_start_file will need to be ignored as it was already executed
17488 to create CURRENT_FILE for the main source holding also the command line
17489 definitions. On first met DW_MACINFO_start_file this flag is reset to
17490 normally execute all the remaining DW_MACINFO_start_file macinfos. */
17491
17492 at_commandline = 1;
17493
17494 do
17495 {
17496 /* Do we at least have room for a macinfo type byte? */
17497 if (mac_ptr >= mac_end)
17498 {
f664829e 17499 dwarf2_section_buffer_overflow_complaint (section);
757a13d0
JK
17500 break;
17501 }
17502
17503 macinfo_type = read_1_byte (abfd, mac_ptr);
17504 mac_ptr++;
17505
cf2c3c16
TT
17506 /* Note that we rely on the fact that the corresponding GNU and
17507 DWARF constants are the same. */
757a13d0
JK
17508 switch (macinfo_type)
17509 {
17510 /* A zero macinfo type indicates the end of the macro
17511 information. */
17512 case 0:
17513 break;
2e276125 17514
cf2c3c16
TT
17515 case DW_MACRO_GNU_define:
17516 case DW_MACRO_GNU_undef:
17517 case DW_MACRO_GNU_define_indirect:
17518 case DW_MACRO_GNU_undef_indirect:
36586728
TT
17519 case DW_MACRO_GNU_define_indirect_alt:
17520 case DW_MACRO_GNU_undef_indirect_alt:
2e276125 17521 {
891d2f0b 17522 unsigned int bytes_read;
2e276125
JB
17523 int line;
17524 char *body;
cf2c3c16 17525 int is_define;
2e276125 17526
cf2c3c16
TT
17527 line = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
17528 mac_ptr += bytes_read;
17529
17530 if (macinfo_type == DW_MACRO_GNU_define
17531 || macinfo_type == DW_MACRO_GNU_undef)
17532 {
17533 body = read_direct_string (abfd, mac_ptr, &bytes_read);
17534 mac_ptr += bytes_read;
17535 }
17536 else
17537 {
17538 LONGEST str_offset;
17539
17540 str_offset = read_offset_1 (abfd, mac_ptr, offset_size);
17541 mac_ptr += offset_size;
2e276125 17542
36586728 17543 if (macinfo_type == DW_MACRO_GNU_define_indirect_alt
f7a35f02
TT
17544 || macinfo_type == DW_MACRO_GNU_undef_indirect_alt
17545 || section_is_dwz)
36586728
TT
17546 {
17547 struct dwz_file *dwz = dwarf2_get_dwz_file ();
17548
17549 body = read_indirect_string_from_dwz (dwz, str_offset);
17550 }
17551 else
17552 body = read_indirect_string_at_offset (abfd, str_offset);
cf2c3c16
TT
17553 }
17554
17555 is_define = (macinfo_type == DW_MACRO_GNU_define
36586728
TT
17556 || macinfo_type == DW_MACRO_GNU_define_indirect
17557 || macinfo_type == DW_MACRO_GNU_define_indirect_alt);
2e276125 17558 if (! current_file)
757a13d0
JK
17559 {
17560 /* DWARF violation as no main source is present. */
17561 complaint (&symfile_complaints,
17562 _("debug info with no main source gives macro %s "
17563 "on line %d: %s"),
cf2c3c16
TT
17564 is_define ? _("definition") : _("undefinition"),
17565 line, body);
757a13d0
JK
17566 break;
17567 }
3e43a32a
MS
17568 if ((line == 0 && !at_commandline)
17569 || (line != 0 && at_commandline))
4d3c2250 17570 complaint (&symfile_complaints,
757a13d0
JK
17571 _("debug info gives %s macro %s with %s line %d: %s"),
17572 at_commandline ? _("command-line") : _("in-file"),
cf2c3c16 17573 is_define ? _("definition") : _("undefinition"),
757a13d0
JK
17574 line == 0 ? _("zero") : _("non-zero"), line, body);
17575
cf2c3c16 17576 if (is_define)
757a13d0 17577 parse_macro_definition (current_file, line, body);
cf2c3c16
TT
17578 else
17579 {
17580 gdb_assert (macinfo_type == DW_MACRO_GNU_undef
36586728
TT
17581 || macinfo_type == DW_MACRO_GNU_undef_indirect
17582 || macinfo_type == DW_MACRO_GNU_undef_indirect_alt);
cf2c3c16
TT
17583 macro_undef (current_file, line, body);
17584 }
2e276125
JB
17585 }
17586 break;
17587
cf2c3c16 17588 case DW_MACRO_GNU_start_file:
2e276125 17589 {
891d2f0b 17590 unsigned int bytes_read;
2e276125
JB
17591 int line, file;
17592
17593 line = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
17594 mac_ptr += bytes_read;
17595 file = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
17596 mac_ptr += bytes_read;
17597
3e43a32a
MS
17598 if ((line == 0 && !at_commandline)
17599 || (line != 0 && at_commandline))
757a13d0
JK
17600 complaint (&symfile_complaints,
17601 _("debug info gives source %d included "
17602 "from %s at %s line %d"),
17603 file, at_commandline ? _("command-line") : _("file"),
17604 line == 0 ? _("zero") : _("non-zero"), line);
17605
17606 if (at_commandline)
17607 {
cf2c3c16
TT
17608 /* This DW_MACRO_GNU_start_file was executed in the
17609 pass one. */
757a13d0
JK
17610 at_commandline = 0;
17611 }
17612 else
17613 current_file = macro_start_file (file, line,
17614 current_file, comp_dir,
cf2c3c16 17615 lh, objfile);
2e276125
JB
17616 }
17617 break;
17618
cf2c3c16 17619 case DW_MACRO_GNU_end_file:
2e276125 17620 if (! current_file)
4d3c2250 17621 complaint (&symfile_complaints,
3e43a32a
MS
17622 _("macro debug info has an unmatched "
17623 "`close_file' directive"));
2e276125
JB
17624 else
17625 {
17626 current_file = current_file->included_by;
17627 if (! current_file)
17628 {
cf2c3c16 17629 enum dwarf_macro_record_type next_type;
2e276125
JB
17630
17631 /* GCC circa March 2002 doesn't produce the zero
17632 type byte marking the end of the compilation
17633 unit. Complain if it's not there, but exit no
17634 matter what. */
17635
17636 /* Do we at least have room for a macinfo type byte? */
17637 if (mac_ptr >= mac_end)
17638 {
f664829e 17639 dwarf2_section_buffer_overflow_complaint (section);
2e276125
JB
17640 return;
17641 }
17642
17643 /* We don't increment mac_ptr here, so this is just
17644 a look-ahead. */
17645 next_type = read_1_byte (abfd, mac_ptr);
17646 if (next_type != 0)
4d3c2250 17647 complaint (&symfile_complaints,
3e43a32a
MS
17648 _("no terminating 0-type entry for "
17649 "macros in `.debug_macinfo' section"));
2e276125
JB
17650
17651 return;
17652 }
17653 }
17654 break;
17655
cf2c3c16 17656 case DW_MACRO_GNU_transparent_include:
36586728 17657 case DW_MACRO_GNU_transparent_include_alt:
cf2c3c16
TT
17658 {
17659 LONGEST offset;
8fc3fc34 17660 void **slot;
cf2c3c16
TT
17661
17662 offset = read_offset_1 (abfd, mac_ptr, offset_size);
17663 mac_ptr += offset_size;
17664
8fc3fc34
TT
17665 slot = htab_find_slot (include_hash, mac_ptr, INSERT);
17666 if (*slot != NULL)
17667 {
17668 /* This has actually happened; see
17669 http://sourceware.org/bugzilla/show_bug.cgi?id=13568. */
17670 complaint (&symfile_complaints,
17671 _("recursive DW_MACRO_GNU_transparent_include in "
17672 ".debug_macro section"));
17673 }
17674 else
17675 {
36586728
TT
17676 bfd *include_bfd = abfd;
17677 struct dwarf2_section_info *include_section = section;
17678 struct dwarf2_section_info alt_section;
17679 gdb_byte *include_mac_end = mac_end;
17680 int is_dwz = section_is_dwz;
17681
8fc3fc34
TT
17682 *slot = mac_ptr;
17683
36586728
TT
17684 if (macinfo_type == DW_MACRO_GNU_transparent_include_alt)
17685 {
17686 struct dwz_file *dwz = dwarf2_get_dwz_file ();
17687
17688 dwarf2_read_section (dwarf2_per_objfile->objfile,
17689 &dwz->macro);
17690
17691 include_bfd = dwz->macro.asection->owner;
17692 include_section = &dwz->macro;
17693 include_mac_end = dwz->macro.buffer + dwz->macro.size;
17694 is_dwz = 1;
17695 }
17696
17697 dwarf_decode_macro_bytes (include_bfd,
17698 include_section->buffer + offset,
17699 include_mac_end, current_file,
8fc3fc34 17700 lh, comp_dir,
36586728 17701 section, section_is_gnu, is_dwz,
8fc3fc34
TT
17702 offset_size, objfile, include_hash);
17703
17704 htab_remove_elt (include_hash, mac_ptr);
17705 }
cf2c3c16
TT
17706 }
17707 break;
17708
2e276125 17709 case DW_MACINFO_vendor_ext:
cf2c3c16
TT
17710 if (!section_is_gnu)
17711 {
17712 unsigned int bytes_read;
17713 int constant;
2e276125 17714
cf2c3c16
TT
17715 constant = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
17716 mac_ptr += bytes_read;
17717 read_direct_string (abfd, mac_ptr, &bytes_read);
17718 mac_ptr += bytes_read;
2e276125 17719
cf2c3c16
TT
17720 /* We don't recognize any vendor extensions. */
17721 break;
17722 }
17723 /* FALLTHROUGH */
17724
17725 default:
17726 mac_ptr = skip_unknown_opcode (macinfo_type, opcode_definitions,
f664829e 17727 mac_ptr, mac_end, abfd, offset_size,
cf2c3c16
TT
17728 section);
17729 if (mac_ptr == NULL)
17730 return;
17731 break;
2e276125 17732 }
757a13d0 17733 } while (macinfo_type != 0);
2e276125 17734}
8e19ed76 17735
cf2c3c16 17736static void
09262596
DE
17737dwarf_decode_macros (struct dwarf2_cu *cu, unsigned int offset,
17738 char *comp_dir, int section_is_gnu)
cf2c3c16 17739{
bb5ed363 17740 struct objfile *objfile = dwarf2_per_objfile->objfile;
09262596
DE
17741 struct line_header *lh = cu->line_header;
17742 bfd *abfd;
cf2c3c16
TT
17743 gdb_byte *mac_ptr, *mac_end;
17744 struct macro_source_file *current_file = 0;
17745 enum dwarf_macro_record_type macinfo_type;
17746 unsigned int offset_size = cu->header.offset_size;
17747 gdb_byte *opcode_definitions[256];
8fc3fc34
TT
17748 struct cleanup *cleanup;
17749 htab_t include_hash;
17750 void **slot;
09262596
DE
17751 struct dwarf2_section_info *section;
17752 const char *section_name;
17753
17754 if (cu->dwo_unit != NULL)
17755 {
17756 if (section_is_gnu)
17757 {
17758 section = &cu->dwo_unit->dwo_file->sections.macro;
17759 section_name = ".debug_macro.dwo";
17760 }
17761 else
17762 {
17763 section = &cu->dwo_unit->dwo_file->sections.macinfo;
17764 section_name = ".debug_macinfo.dwo";
17765 }
17766 }
17767 else
17768 {
17769 if (section_is_gnu)
17770 {
17771 section = &dwarf2_per_objfile->macro;
17772 section_name = ".debug_macro";
17773 }
17774 else
17775 {
17776 section = &dwarf2_per_objfile->macinfo;
17777 section_name = ".debug_macinfo";
17778 }
17779 }
cf2c3c16 17780
bb5ed363 17781 dwarf2_read_section (objfile, section);
cf2c3c16
TT
17782 if (section->buffer == NULL)
17783 {
fceca515 17784 complaint (&symfile_complaints, _("missing %s section"), section_name);
cf2c3c16
TT
17785 return;
17786 }
09262596 17787 abfd = section->asection->owner;
cf2c3c16
TT
17788
17789 /* First pass: Find the name of the base filename.
17790 This filename is needed in order to process all macros whose definition
17791 (or undefinition) comes from the command line. These macros are defined
17792 before the first DW_MACINFO_start_file entry, and yet still need to be
17793 associated to the base file.
17794
17795 To determine the base file name, we scan the macro definitions until we
17796 reach the first DW_MACINFO_start_file entry. We then initialize
17797 CURRENT_FILE accordingly so that any macro definition found before the
17798 first DW_MACINFO_start_file can still be associated to the base file. */
17799
17800 mac_ptr = section->buffer + offset;
17801 mac_end = section->buffer + section->size;
17802
17803 mac_ptr = dwarf_parse_macro_header (opcode_definitions, abfd, mac_ptr,
17804 &offset_size, section_is_gnu);
17805 if (mac_ptr == NULL)
17806 {
17807 /* We already issued a complaint. */
17808 return;
17809 }
17810
17811 do
17812 {
17813 /* Do we at least have room for a macinfo type byte? */
17814 if (mac_ptr >= mac_end)
17815 {
17816 /* Complaint is printed during the second pass as GDB will probably
17817 stop the first pass earlier upon finding
17818 DW_MACINFO_start_file. */
17819 break;
17820 }
17821
17822 macinfo_type = read_1_byte (abfd, mac_ptr);
17823 mac_ptr++;
17824
17825 /* Note that we rely on the fact that the corresponding GNU and
17826 DWARF constants are the same. */
17827 switch (macinfo_type)
17828 {
17829 /* A zero macinfo type indicates the end of the macro
17830 information. */
17831 case 0:
17832 break;
17833
17834 case DW_MACRO_GNU_define:
17835 case DW_MACRO_GNU_undef:
17836 /* Only skip the data by MAC_PTR. */
17837 {
17838 unsigned int bytes_read;
17839
17840 read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
17841 mac_ptr += bytes_read;
17842 read_direct_string (abfd, mac_ptr, &bytes_read);
17843 mac_ptr += bytes_read;
17844 }
17845 break;
17846
17847 case DW_MACRO_GNU_start_file:
17848 {
17849 unsigned int bytes_read;
17850 int line, file;
17851
17852 line = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
17853 mac_ptr += bytes_read;
17854 file = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
17855 mac_ptr += bytes_read;
17856
17857 current_file = macro_start_file (file, line, current_file,
bb5ed363 17858 comp_dir, lh, objfile);
cf2c3c16
TT
17859 }
17860 break;
17861
17862 case DW_MACRO_GNU_end_file:
17863 /* No data to skip by MAC_PTR. */
17864 break;
17865
17866 case DW_MACRO_GNU_define_indirect:
17867 case DW_MACRO_GNU_undef_indirect:
f7a35f02
TT
17868 case DW_MACRO_GNU_define_indirect_alt:
17869 case DW_MACRO_GNU_undef_indirect_alt:
cf2c3c16
TT
17870 {
17871 unsigned int bytes_read;
17872
17873 read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
17874 mac_ptr += bytes_read;
17875 mac_ptr += offset_size;
17876 }
17877 break;
17878
17879 case DW_MACRO_GNU_transparent_include:
f7a35f02 17880 case DW_MACRO_GNU_transparent_include_alt:
cf2c3c16
TT
17881 /* Note that, according to the spec, a transparent include
17882 chain cannot call DW_MACRO_GNU_start_file. So, we can just
17883 skip this opcode. */
17884 mac_ptr += offset_size;
17885 break;
17886
17887 case DW_MACINFO_vendor_ext:
17888 /* Only skip the data by MAC_PTR. */
17889 if (!section_is_gnu)
17890 {
17891 unsigned int bytes_read;
17892
17893 read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
17894 mac_ptr += bytes_read;
17895 read_direct_string (abfd, mac_ptr, &bytes_read);
17896 mac_ptr += bytes_read;
17897 }
17898 /* FALLTHROUGH */
17899
17900 default:
17901 mac_ptr = skip_unknown_opcode (macinfo_type, opcode_definitions,
f664829e 17902 mac_ptr, mac_end, abfd, offset_size,
cf2c3c16
TT
17903 section);
17904 if (mac_ptr == NULL)
17905 return;
17906 break;
17907 }
17908 } while (macinfo_type != 0 && current_file == NULL);
17909
17910 /* Second pass: Process all entries.
17911
17912 Use the AT_COMMAND_LINE flag to determine whether we are still processing
17913 command-line macro definitions/undefinitions. This flag is unset when we
17914 reach the first DW_MACINFO_start_file entry. */
17915
8fc3fc34
TT
17916 include_hash = htab_create_alloc (1, htab_hash_pointer, htab_eq_pointer,
17917 NULL, xcalloc, xfree);
17918 cleanup = make_cleanup_htab_delete (include_hash);
17919 mac_ptr = section->buffer + offset;
17920 slot = htab_find_slot (include_hash, mac_ptr, INSERT);
17921 *slot = mac_ptr;
17922 dwarf_decode_macro_bytes (abfd, mac_ptr, mac_end,
36586728
TT
17923 current_file, lh, comp_dir, section,
17924 section_is_gnu, 0,
8fc3fc34
TT
17925 offset_size, objfile, include_hash);
17926 do_cleanups (cleanup);
cf2c3c16
TT
17927}
17928
8e19ed76 17929/* Check if the attribute's form is a DW_FORM_block*
0963b4bd 17930 if so return true else false. */
380bca97 17931
8e19ed76
PS
17932static int
17933attr_form_is_block (struct attribute *attr)
17934{
17935 return (attr == NULL ? 0 :
17936 attr->form == DW_FORM_block1
17937 || attr->form == DW_FORM_block2
17938 || attr->form == DW_FORM_block4
2dc7f7b3
TT
17939 || attr->form == DW_FORM_block
17940 || attr->form == DW_FORM_exprloc);
8e19ed76 17941}
4c2df51b 17942
c6a0999f
JB
17943/* Return non-zero if ATTR's value is a section offset --- classes
17944 lineptr, loclistptr, macptr or rangelistptr --- or zero, otherwise.
17945 You may use DW_UNSND (attr) to retrieve such offsets.
17946
17947 Section 7.5.4, "Attribute Encodings", explains that no attribute
17948 may have a value that belongs to more than one of these classes; it
17949 would be ambiguous if we did, because we use the same forms for all
17950 of them. */
380bca97 17951
3690dd37
JB
17952static int
17953attr_form_is_section_offset (struct attribute *attr)
17954{
17955 return (attr->form == DW_FORM_data4
2dc7f7b3
TT
17956 || attr->form == DW_FORM_data8
17957 || attr->form == DW_FORM_sec_offset);
3690dd37
JB
17958}
17959
3690dd37
JB
17960/* Return non-zero if ATTR's value falls in the 'constant' class, or
17961 zero otherwise. When this function returns true, you can apply
17962 dwarf2_get_attr_constant_value to it.
17963
17964 However, note that for some attributes you must check
17965 attr_form_is_section_offset before using this test. DW_FORM_data4
17966 and DW_FORM_data8 are members of both the constant class, and of
17967 the classes that contain offsets into other debug sections
17968 (lineptr, loclistptr, macptr or rangelistptr). The DWARF spec says
17969 that, if an attribute's can be either a constant or one of the
17970 section offset classes, DW_FORM_data4 and DW_FORM_data8 should be
17971 taken as section offsets, not constants. */
380bca97 17972
3690dd37
JB
17973static int
17974attr_form_is_constant (struct attribute *attr)
17975{
17976 switch (attr->form)
17977 {
17978 case DW_FORM_sdata:
17979 case DW_FORM_udata:
17980 case DW_FORM_data1:
17981 case DW_FORM_data2:
17982 case DW_FORM_data4:
17983 case DW_FORM_data8:
17984 return 1;
17985 default:
17986 return 0;
17987 }
17988}
17989
3019eac3
DE
17990/* Return the .debug_loc section to use for CU.
17991 For DWO files use .debug_loc.dwo. */
17992
17993static struct dwarf2_section_info *
17994cu_debug_loc_section (struct dwarf2_cu *cu)
17995{
17996 if (cu->dwo_unit)
17997 return &cu->dwo_unit->dwo_file->sections.loc;
17998 return &dwarf2_per_objfile->loc;
17999}
18000
8cf6f0b1
TT
18001/* A helper function that fills in a dwarf2_loclist_baton. */
18002
18003static void
18004fill_in_loclist_baton (struct dwarf2_cu *cu,
18005 struct dwarf2_loclist_baton *baton,
18006 struct attribute *attr)
18007{
3019eac3
DE
18008 struct dwarf2_section_info *section = cu_debug_loc_section (cu);
18009
18010 dwarf2_read_section (dwarf2_per_objfile->objfile, section);
8cf6f0b1
TT
18011
18012 baton->per_cu = cu->per_cu;
18013 gdb_assert (baton->per_cu);
18014 /* We don't know how long the location list is, but make sure we
18015 don't run off the edge of the section. */
3019eac3
DE
18016 baton->size = section->size - DW_UNSND (attr);
18017 baton->data = section->buffer + DW_UNSND (attr);
8cf6f0b1 18018 baton->base_address = cu->base_address;
f664829e 18019 baton->from_dwo = cu->dwo_unit != NULL;
8cf6f0b1
TT
18020}
18021
4c2df51b
DJ
18022static void
18023dwarf2_symbol_mark_computed (struct attribute *attr, struct symbol *sym,
e7c27a73 18024 struct dwarf2_cu *cu)
4c2df51b 18025{
bb5ed363 18026 struct objfile *objfile = dwarf2_per_objfile->objfile;
3019eac3 18027 struct dwarf2_section_info *section = cu_debug_loc_section (cu);
bb5ed363 18028
3690dd37 18029 if (attr_form_is_section_offset (attr)
3019eac3 18030 /* .debug_loc{,.dwo} may not exist at all, or the offset may be outside
99bcc461
DJ
18031 the section. If so, fall through to the complaint in the
18032 other branch. */
3019eac3 18033 && DW_UNSND (attr) < dwarf2_section_size (objfile, section))
4c2df51b 18034 {
0d53c4c4 18035 struct dwarf2_loclist_baton *baton;
4c2df51b 18036
bb5ed363 18037 baton = obstack_alloc (&objfile->objfile_obstack,
0d53c4c4 18038 sizeof (struct dwarf2_loclist_baton));
4c2df51b 18039
8cf6f0b1 18040 fill_in_loclist_baton (cu, baton, attr);
be391dca 18041
d00adf39 18042 if (cu->base_known == 0)
0d53c4c4 18043 complaint (&symfile_complaints,
3e43a32a
MS
18044 _("Location list used without "
18045 "specifying the CU base address."));
4c2df51b 18046
768a979c 18047 SYMBOL_COMPUTED_OPS (sym) = &dwarf2_loclist_funcs;
0d53c4c4
DJ
18048 SYMBOL_LOCATION_BATON (sym) = baton;
18049 }
18050 else
18051 {
18052 struct dwarf2_locexpr_baton *baton;
18053
bb5ed363 18054 baton = obstack_alloc (&objfile->objfile_obstack,
0d53c4c4 18055 sizeof (struct dwarf2_locexpr_baton));
ae0d2f24
UW
18056 baton->per_cu = cu->per_cu;
18057 gdb_assert (baton->per_cu);
0d53c4c4
DJ
18058
18059 if (attr_form_is_block (attr))
18060 {
18061 /* Note that we're just copying the block's data pointer
18062 here, not the actual data. We're still pointing into the
6502dd73
DJ
18063 info_buffer for SYM's objfile; right now we never release
18064 that buffer, but when we do clean up properly this may
18065 need to change. */
0d53c4c4
DJ
18066 baton->size = DW_BLOCK (attr)->size;
18067 baton->data = DW_BLOCK (attr)->data;
18068 }
18069 else
18070 {
18071 dwarf2_invalid_attrib_class_complaint ("location description",
18072 SYMBOL_NATURAL_NAME (sym));
18073 baton->size = 0;
0d53c4c4 18074 }
6e70227d 18075
768a979c 18076 SYMBOL_COMPUTED_OPS (sym) = &dwarf2_locexpr_funcs;
0d53c4c4
DJ
18077 SYMBOL_LOCATION_BATON (sym) = baton;
18078 }
4c2df51b 18079}
6502dd73 18080
9aa1f1e3
TT
18081/* Return the OBJFILE associated with the compilation unit CU. If CU
18082 came from a separate debuginfo file, then the master objfile is
18083 returned. */
ae0d2f24
UW
18084
18085struct objfile *
18086dwarf2_per_cu_objfile (struct dwarf2_per_cu_data *per_cu)
18087{
9291a0cd 18088 struct objfile *objfile = per_cu->objfile;
ae0d2f24
UW
18089
18090 /* Return the master objfile, so that we can report and look up the
18091 correct file containing this variable. */
18092 if (objfile->separate_debug_objfile_backlink)
18093 objfile = objfile->separate_debug_objfile_backlink;
18094
18095 return objfile;
18096}
18097
96408a79
SA
18098/* Return comp_unit_head for PER_CU, either already available in PER_CU->CU
18099 (CU_HEADERP is unused in such case) or prepare a temporary copy at
18100 CU_HEADERP first. */
18101
18102static const struct comp_unit_head *
18103per_cu_header_read_in (struct comp_unit_head *cu_headerp,
18104 struct dwarf2_per_cu_data *per_cu)
18105{
96408a79
SA
18106 gdb_byte *info_ptr;
18107
18108 if (per_cu->cu)
18109 return &per_cu->cu->header;
18110
0bc3a05c 18111 info_ptr = per_cu->info_or_types_section->buffer + per_cu->offset.sect_off;
96408a79
SA
18112
18113 memset (cu_headerp, 0, sizeof (*cu_headerp));
0bc3a05c 18114 read_comp_unit_head (cu_headerp, info_ptr, per_cu->objfile->obfd);
96408a79
SA
18115
18116 return cu_headerp;
18117}
18118
ae0d2f24
UW
18119/* Return the address size given in the compilation unit header for CU. */
18120
98714339 18121int
ae0d2f24
UW
18122dwarf2_per_cu_addr_size (struct dwarf2_per_cu_data *per_cu)
18123{
96408a79
SA
18124 struct comp_unit_head cu_header_local;
18125 const struct comp_unit_head *cu_headerp;
c471e790 18126
96408a79
SA
18127 cu_headerp = per_cu_header_read_in (&cu_header_local, per_cu);
18128
18129 return cu_headerp->addr_size;
ae0d2f24
UW
18130}
18131
9eae7c52
TT
18132/* Return the offset size given in the compilation unit header for CU. */
18133
18134int
18135dwarf2_per_cu_offset_size (struct dwarf2_per_cu_data *per_cu)
18136{
96408a79
SA
18137 struct comp_unit_head cu_header_local;
18138 const struct comp_unit_head *cu_headerp;
9c6c53f7 18139
96408a79
SA
18140 cu_headerp = per_cu_header_read_in (&cu_header_local, per_cu);
18141
18142 return cu_headerp->offset_size;
18143}
18144
18145/* See its dwarf2loc.h declaration. */
18146
18147int
18148dwarf2_per_cu_ref_addr_size (struct dwarf2_per_cu_data *per_cu)
18149{
18150 struct comp_unit_head cu_header_local;
18151 const struct comp_unit_head *cu_headerp;
18152
18153 cu_headerp = per_cu_header_read_in (&cu_header_local, per_cu);
18154
18155 if (cu_headerp->version == 2)
18156 return cu_headerp->addr_size;
18157 else
18158 return cu_headerp->offset_size;
181cebd4
JK
18159}
18160
9aa1f1e3
TT
18161/* Return the text offset of the CU. The returned offset comes from
18162 this CU's objfile. If this objfile came from a separate debuginfo
18163 file, then the offset may be different from the corresponding
18164 offset in the parent objfile. */
18165
18166CORE_ADDR
18167dwarf2_per_cu_text_offset (struct dwarf2_per_cu_data *per_cu)
18168{
bb3fa9d0 18169 struct objfile *objfile = per_cu->objfile;
9aa1f1e3
TT
18170
18171 return ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
18172}
18173
348e048f
DE
18174/* Locate the .debug_info compilation unit from CU's objfile which contains
18175 the DIE at OFFSET. Raises an error on failure. */
ae038cb0
DJ
18176
18177static struct dwarf2_per_cu_data *
b64f50a1 18178dwarf2_find_containing_comp_unit (sect_offset offset,
36586728 18179 unsigned int offset_in_dwz,
ae038cb0
DJ
18180 struct objfile *objfile)
18181{
18182 struct dwarf2_per_cu_data *this_cu;
18183 int low, high;
36586728 18184 const sect_offset *cu_off;
ae038cb0 18185
ae038cb0
DJ
18186 low = 0;
18187 high = dwarf2_per_objfile->n_comp_units - 1;
18188 while (high > low)
18189 {
36586728 18190 struct dwarf2_per_cu_data *mid_cu;
ae038cb0 18191 int mid = low + (high - low) / 2;
9a619af0 18192
36586728
TT
18193 mid_cu = dwarf2_per_objfile->all_comp_units[mid];
18194 cu_off = &mid_cu->offset;
18195 if (mid_cu->is_dwz > offset_in_dwz
18196 || (mid_cu->is_dwz == offset_in_dwz
18197 && cu_off->sect_off >= offset.sect_off))
ae038cb0
DJ
18198 high = mid;
18199 else
18200 low = mid + 1;
18201 }
18202 gdb_assert (low == high);
36586728
TT
18203 this_cu = dwarf2_per_objfile->all_comp_units[low];
18204 cu_off = &this_cu->offset;
18205 if (this_cu->is_dwz != offset_in_dwz || cu_off->sect_off > offset.sect_off)
ae038cb0 18206 {
36586728 18207 if (low == 0 || this_cu->is_dwz != offset_in_dwz)
8a3fe4f8
AC
18208 error (_("Dwarf Error: could not find partial DIE containing "
18209 "offset 0x%lx [in module %s]"),
b64f50a1 18210 (long) offset.sect_off, bfd_get_filename (objfile->obfd));
10b3939b 18211
b64f50a1
JK
18212 gdb_assert (dwarf2_per_objfile->all_comp_units[low-1]->offset.sect_off
18213 <= offset.sect_off);
ae038cb0
DJ
18214 return dwarf2_per_objfile->all_comp_units[low-1];
18215 }
18216 else
18217 {
18218 this_cu = dwarf2_per_objfile->all_comp_units[low];
18219 if (low == dwarf2_per_objfile->n_comp_units - 1
b64f50a1
JK
18220 && offset.sect_off >= this_cu->offset.sect_off + this_cu->length)
18221 error (_("invalid dwarf2 offset %u"), offset.sect_off);
18222 gdb_assert (offset.sect_off < this_cu->offset.sect_off + this_cu->length);
ae038cb0
DJ
18223 return this_cu;
18224 }
18225}
18226
23745b47 18227/* Initialize dwarf2_cu CU, owned by PER_CU. */
93311388 18228
9816fde3 18229static void
23745b47 18230init_one_comp_unit (struct dwarf2_cu *cu, struct dwarf2_per_cu_data *per_cu)
93311388 18231{
9816fde3 18232 memset (cu, 0, sizeof (*cu));
23745b47
DE
18233 per_cu->cu = cu;
18234 cu->per_cu = per_cu;
18235 cu->objfile = per_cu->objfile;
93311388 18236 obstack_init (&cu->comp_unit_obstack);
9816fde3
JK
18237}
18238
18239/* Initialize basic fields of dwarf_cu CU according to DIE COMP_UNIT_DIE. */
18240
18241static void
95554aad
TT
18242prepare_one_comp_unit (struct dwarf2_cu *cu, struct die_info *comp_unit_die,
18243 enum language pretend_language)
9816fde3
JK
18244{
18245 struct attribute *attr;
18246
18247 /* Set the language we're debugging. */
18248 attr = dwarf2_attr (comp_unit_die, DW_AT_language, cu);
18249 if (attr)
18250 set_cu_language (DW_UNSND (attr), cu);
18251 else
9cded63f 18252 {
95554aad 18253 cu->language = pretend_language;
9cded63f
TT
18254 cu->language_defn = language_def (cu->language);
18255 }
dee91e82
DE
18256
18257 attr = dwarf2_attr (comp_unit_die, DW_AT_producer, cu);
18258 if (attr)
18259 cu->producer = DW_STRING (attr);
93311388
DE
18260}
18261
ae038cb0
DJ
18262/* Release one cached compilation unit, CU. We unlink it from the tree
18263 of compilation units, but we don't remove it from the read_in_chain;
93311388
DE
18264 the caller is responsible for that.
18265 NOTE: DATA is a void * because this function is also used as a
18266 cleanup routine. */
ae038cb0
DJ
18267
18268static void
68dc6402 18269free_heap_comp_unit (void *data)
ae038cb0
DJ
18270{
18271 struct dwarf2_cu *cu = data;
18272
23745b47
DE
18273 gdb_assert (cu->per_cu != NULL);
18274 cu->per_cu->cu = NULL;
ae038cb0
DJ
18275 cu->per_cu = NULL;
18276
18277 obstack_free (&cu->comp_unit_obstack, NULL);
18278
18279 xfree (cu);
18280}
18281
72bf9492 18282/* This cleanup function is passed the address of a dwarf2_cu on the stack
ae038cb0 18283 when we're finished with it. We can't free the pointer itself, but be
dee91e82 18284 sure to unlink it from the cache. Also release any associated storage. */
72bf9492
DJ
18285
18286static void
18287free_stack_comp_unit (void *data)
18288{
18289 struct dwarf2_cu *cu = data;
18290
23745b47
DE
18291 gdb_assert (cu->per_cu != NULL);
18292 cu->per_cu->cu = NULL;
18293 cu->per_cu = NULL;
18294
72bf9492
DJ
18295 obstack_free (&cu->comp_unit_obstack, NULL);
18296 cu->partial_dies = NULL;
ae038cb0
DJ
18297}
18298
18299/* Free all cached compilation units. */
18300
18301static void
18302free_cached_comp_units (void *data)
18303{
18304 struct dwarf2_per_cu_data *per_cu, **last_chain;
18305
18306 per_cu = dwarf2_per_objfile->read_in_chain;
18307 last_chain = &dwarf2_per_objfile->read_in_chain;
18308 while (per_cu != NULL)
18309 {
18310 struct dwarf2_per_cu_data *next_cu;
18311
18312 next_cu = per_cu->cu->read_in_chain;
18313
68dc6402 18314 free_heap_comp_unit (per_cu->cu);
ae038cb0
DJ
18315 *last_chain = next_cu;
18316
18317 per_cu = next_cu;
18318 }
18319}
18320
18321/* Increase the age counter on each cached compilation unit, and free
18322 any that are too old. */
18323
18324static void
18325age_cached_comp_units (void)
18326{
18327 struct dwarf2_per_cu_data *per_cu, **last_chain;
18328
18329 dwarf2_clear_marks (dwarf2_per_objfile->read_in_chain);
18330 per_cu = dwarf2_per_objfile->read_in_chain;
18331 while (per_cu != NULL)
18332 {
18333 per_cu->cu->last_used ++;
18334 if (per_cu->cu->last_used <= dwarf2_max_cache_age)
18335 dwarf2_mark (per_cu->cu);
18336 per_cu = per_cu->cu->read_in_chain;
18337 }
18338
18339 per_cu = dwarf2_per_objfile->read_in_chain;
18340 last_chain = &dwarf2_per_objfile->read_in_chain;
18341 while (per_cu != NULL)
18342 {
18343 struct dwarf2_per_cu_data *next_cu;
18344
18345 next_cu = per_cu->cu->read_in_chain;
18346
18347 if (!per_cu->cu->mark)
18348 {
68dc6402 18349 free_heap_comp_unit (per_cu->cu);
ae038cb0
DJ
18350 *last_chain = next_cu;
18351 }
18352 else
18353 last_chain = &per_cu->cu->read_in_chain;
18354
18355 per_cu = next_cu;
18356 }
18357}
18358
18359/* Remove a single compilation unit from the cache. */
18360
18361static void
dee91e82 18362free_one_cached_comp_unit (struct dwarf2_per_cu_data *target_per_cu)
ae038cb0
DJ
18363{
18364 struct dwarf2_per_cu_data *per_cu, **last_chain;
18365
18366 per_cu = dwarf2_per_objfile->read_in_chain;
18367 last_chain = &dwarf2_per_objfile->read_in_chain;
18368 while (per_cu != NULL)
18369 {
18370 struct dwarf2_per_cu_data *next_cu;
18371
18372 next_cu = per_cu->cu->read_in_chain;
18373
dee91e82 18374 if (per_cu == target_per_cu)
ae038cb0 18375 {
68dc6402 18376 free_heap_comp_unit (per_cu->cu);
dee91e82 18377 per_cu->cu = NULL;
ae038cb0
DJ
18378 *last_chain = next_cu;
18379 break;
18380 }
18381 else
18382 last_chain = &per_cu->cu->read_in_chain;
18383
18384 per_cu = next_cu;
18385 }
18386}
18387
fe3e1990
DJ
18388/* Release all extra memory associated with OBJFILE. */
18389
18390void
18391dwarf2_free_objfile (struct objfile *objfile)
18392{
18393 dwarf2_per_objfile = objfile_data (objfile, dwarf2_objfile_data_key);
18394
18395 if (dwarf2_per_objfile == NULL)
18396 return;
18397
18398 /* Cached DIE trees use xmalloc and the comp_unit_obstack. */
18399 free_cached_comp_units (NULL);
18400
7b9f3c50
DE
18401 if (dwarf2_per_objfile->quick_file_names_table)
18402 htab_delete (dwarf2_per_objfile->quick_file_names_table);
9291a0cd 18403
fe3e1990
DJ
18404 /* Everything else should be on the objfile obstack. */
18405}
18406
dee91e82
DE
18407/* A set of CU "per_cu" pointer, DIE offset, and GDB type pointer.
18408 We store these in a hash table separate from the DIEs, and preserve them
18409 when the DIEs are flushed out of cache.
18410
18411 The CU "per_cu" pointer is needed because offset alone is not enough to
3019eac3
DE
18412 uniquely identify the type. A file may have multiple .debug_types sections,
18413 or the type may come from a DWO file. We have to use something in
18414 dwarf2_per_cu_data (or the pointer to it) because we can enter the lookup
18415 routine, get_die_type_at_offset, from outside this file, and thus won't
18416 necessarily have PER_CU->cu. Fortunately, PER_CU is stable for the life
18417 of the objfile. */
1c379e20 18418
dee91e82 18419struct dwarf2_per_cu_offset_and_type
1c379e20 18420{
dee91e82 18421 const struct dwarf2_per_cu_data *per_cu;
b64f50a1 18422 sect_offset offset;
1c379e20
DJ
18423 struct type *type;
18424};
18425
dee91e82 18426/* Hash function for a dwarf2_per_cu_offset_and_type. */
1c379e20
DJ
18427
18428static hashval_t
dee91e82 18429per_cu_offset_and_type_hash (const void *item)
1c379e20 18430{
dee91e82 18431 const struct dwarf2_per_cu_offset_and_type *ofs = item;
9a619af0 18432
dee91e82 18433 return (uintptr_t) ofs->per_cu + ofs->offset.sect_off;
1c379e20
DJ
18434}
18435
dee91e82 18436/* Equality function for a dwarf2_per_cu_offset_and_type. */
1c379e20
DJ
18437
18438static int
dee91e82 18439per_cu_offset_and_type_eq (const void *item_lhs, const void *item_rhs)
1c379e20 18440{
dee91e82
DE
18441 const struct dwarf2_per_cu_offset_and_type *ofs_lhs = item_lhs;
18442 const struct dwarf2_per_cu_offset_and_type *ofs_rhs = item_rhs;
9a619af0 18443
dee91e82
DE
18444 return (ofs_lhs->per_cu == ofs_rhs->per_cu
18445 && ofs_lhs->offset.sect_off == ofs_rhs->offset.sect_off);
1c379e20
DJ
18446}
18447
18448/* Set the type associated with DIE to TYPE. Save it in CU's hash
7e314c57
JK
18449 table if necessary. For convenience, return TYPE.
18450
18451 The DIEs reading must have careful ordering to:
18452 * Not cause infite loops trying to read in DIEs as a prerequisite for
18453 reading current DIE.
18454 * Not trying to dereference contents of still incompletely read in types
18455 while reading in other DIEs.
18456 * Enable referencing still incompletely read in types just by a pointer to
18457 the type without accessing its fields.
18458
18459 Therefore caller should follow these rules:
18460 * Try to fetch any prerequisite types we may need to build this DIE type
18461 before building the type and calling set_die_type.
e71ec853 18462 * After building type call set_die_type for current DIE as soon as
7e314c57
JK
18463 possible before fetching more types to complete the current type.
18464 * Make the type as complete as possible before fetching more types. */
1c379e20 18465
f792889a 18466static struct type *
1c379e20
DJ
18467set_die_type (struct die_info *die, struct type *type, struct dwarf2_cu *cu)
18468{
dee91e82 18469 struct dwarf2_per_cu_offset_and_type **slot, ofs;
673bfd45 18470 struct objfile *objfile = cu->objfile;
1c379e20 18471
b4ba55a1
JB
18472 /* For Ada types, make sure that the gnat-specific data is always
18473 initialized (if not already set). There are a few types where
18474 we should not be doing so, because the type-specific area is
18475 already used to hold some other piece of info (eg: TYPE_CODE_FLT
18476 where the type-specific area is used to store the floatformat).
18477 But this is not a problem, because the gnat-specific information
18478 is actually not needed for these types. */
18479 if (need_gnat_info (cu)
18480 && TYPE_CODE (type) != TYPE_CODE_FUNC
18481 && TYPE_CODE (type) != TYPE_CODE_FLT
18482 && !HAVE_GNAT_AUX_INFO (type))
18483 INIT_GNAT_SPECIFIC (type);
18484
dee91e82 18485 if (dwarf2_per_objfile->die_type_hash == NULL)
f792889a 18486 {
dee91e82
DE
18487 dwarf2_per_objfile->die_type_hash =
18488 htab_create_alloc_ex (127,
18489 per_cu_offset_and_type_hash,
18490 per_cu_offset_and_type_eq,
18491 NULL,
18492 &objfile->objfile_obstack,
18493 hashtab_obstack_allocate,
18494 dummy_obstack_deallocate);
f792889a 18495 }
1c379e20 18496
dee91e82 18497 ofs.per_cu = cu->per_cu;
1c379e20
DJ
18498 ofs.offset = die->offset;
18499 ofs.type = type;
dee91e82
DE
18500 slot = (struct dwarf2_per_cu_offset_and_type **)
18501 htab_find_slot (dwarf2_per_objfile->die_type_hash, &ofs, INSERT);
7e314c57
JK
18502 if (*slot)
18503 complaint (&symfile_complaints,
18504 _("A problem internal to GDB: DIE 0x%x has type already set"),
b64f50a1 18505 die->offset.sect_off);
673bfd45 18506 *slot = obstack_alloc (&objfile->objfile_obstack, sizeof (**slot));
1c379e20 18507 **slot = ofs;
f792889a 18508 return type;
1c379e20
DJ
18509}
18510
380bca97 18511/* Look up the type for the die at OFFSET in the appropriate type_hash
673bfd45 18512 table, or return NULL if the die does not have a saved type. */
1c379e20
DJ
18513
18514static struct type *
b64f50a1 18515get_die_type_at_offset (sect_offset offset,
673bfd45 18516 struct dwarf2_per_cu_data *per_cu)
1c379e20 18517{
dee91e82 18518 struct dwarf2_per_cu_offset_and_type *slot, ofs;
f792889a 18519
dee91e82 18520 if (dwarf2_per_objfile->die_type_hash == NULL)
f792889a 18521 return NULL;
1c379e20 18522
dee91e82 18523 ofs.per_cu = per_cu;
673bfd45 18524 ofs.offset = offset;
dee91e82 18525 slot = htab_find (dwarf2_per_objfile->die_type_hash, &ofs);
1c379e20
DJ
18526 if (slot)
18527 return slot->type;
18528 else
18529 return NULL;
18530}
18531
673bfd45
DE
18532/* Look up the type for DIE in the appropriate type_hash table,
18533 or return NULL if DIE does not have a saved type. */
18534
18535static struct type *
18536get_die_type (struct die_info *die, struct dwarf2_cu *cu)
18537{
18538 return get_die_type_at_offset (die->offset, cu->per_cu);
18539}
18540
10b3939b
DJ
18541/* Add a dependence relationship from CU to REF_PER_CU. */
18542
18543static void
18544dwarf2_add_dependence (struct dwarf2_cu *cu,
18545 struct dwarf2_per_cu_data *ref_per_cu)
18546{
18547 void **slot;
18548
18549 if (cu->dependencies == NULL)
18550 cu->dependencies
18551 = htab_create_alloc_ex (5, htab_hash_pointer, htab_eq_pointer,
18552 NULL, &cu->comp_unit_obstack,
18553 hashtab_obstack_allocate,
18554 dummy_obstack_deallocate);
18555
18556 slot = htab_find_slot (cu->dependencies, ref_per_cu, INSERT);
18557 if (*slot == NULL)
18558 *slot = ref_per_cu;
18559}
1c379e20 18560
f504f079
DE
18561/* Subroutine of dwarf2_mark to pass to htab_traverse.
18562 Set the mark field in every compilation unit in the
ae038cb0
DJ
18563 cache that we must keep because we are keeping CU. */
18564
10b3939b
DJ
18565static int
18566dwarf2_mark_helper (void **slot, void *data)
18567{
18568 struct dwarf2_per_cu_data *per_cu;
18569
18570 per_cu = (struct dwarf2_per_cu_data *) *slot;
d07ed419
JK
18571
18572 /* cu->dependencies references may not yet have been ever read if QUIT aborts
18573 reading of the chain. As such dependencies remain valid it is not much
18574 useful to track and undo them during QUIT cleanups. */
18575 if (per_cu->cu == NULL)
18576 return 1;
18577
10b3939b
DJ
18578 if (per_cu->cu->mark)
18579 return 1;
18580 per_cu->cu->mark = 1;
18581
18582 if (per_cu->cu->dependencies != NULL)
18583 htab_traverse (per_cu->cu->dependencies, dwarf2_mark_helper, NULL);
18584
18585 return 1;
18586}
18587
f504f079
DE
18588/* Set the mark field in CU and in every other compilation unit in the
18589 cache that we must keep because we are keeping CU. */
18590
ae038cb0
DJ
18591static void
18592dwarf2_mark (struct dwarf2_cu *cu)
18593{
18594 if (cu->mark)
18595 return;
18596 cu->mark = 1;
10b3939b
DJ
18597 if (cu->dependencies != NULL)
18598 htab_traverse (cu->dependencies, dwarf2_mark_helper, NULL);
ae038cb0
DJ
18599}
18600
18601static void
18602dwarf2_clear_marks (struct dwarf2_per_cu_data *per_cu)
18603{
18604 while (per_cu)
18605 {
18606 per_cu->cu->mark = 0;
18607 per_cu = per_cu->cu->read_in_chain;
18608 }
72bf9492
DJ
18609}
18610
72bf9492
DJ
18611/* Trivial hash function for partial_die_info: the hash value of a DIE
18612 is its offset in .debug_info for this objfile. */
18613
18614static hashval_t
18615partial_die_hash (const void *item)
18616{
18617 const struct partial_die_info *part_die = item;
9a619af0 18618
b64f50a1 18619 return part_die->offset.sect_off;
72bf9492
DJ
18620}
18621
18622/* Trivial comparison function for partial_die_info structures: two DIEs
18623 are equal if they have the same offset. */
18624
18625static int
18626partial_die_eq (const void *item_lhs, const void *item_rhs)
18627{
18628 const struct partial_die_info *part_die_lhs = item_lhs;
18629 const struct partial_die_info *part_die_rhs = item_rhs;
9a619af0 18630
b64f50a1 18631 return part_die_lhs->offset.sect_off == part_die_rhs->offset.sect_off;
72bf9492
DJ
18632}
18633
ae038cb0
DJ
18634static struct cmd_list_element *set_dwarf2_cmdlist;
18635static struct cmd_list_element *show_dwarf2_cmdlist;
18636
18637static void
18638set_dwarf2_cmd (char *args, int from_tty)
18639{
18640 help_list (set_dwarf2_cmdlist, "maintenance set dwarf2 ", -1, gdb_stdout);
18641}
18642
18643static void
18644show_dwarf2_cmd (char *args, int from_tty)
6e70227d 18645{
ae038cb0
DJ
18646 cmd_show_list (show_dwarf2_cmdlist, from_tty, "");
18647}
18648
4bf44c1c 18649/* Free data associated with OBJFILE, if necessary. */
dce234bc
PP
18650
18651static void
c1bd65d0 18652dwarf2_per_objfile_free (struct objfile *objfile, void *d)
dce234bc
PP
18653{
18654 struct dwarf2_per_objfile *data = d;
8b70b953 18655 int ix;
8b70b953 18656
95554aad
TT
18657 for (ix = 0; ix < dwarf2_per_objfile->n_comp_units; ++ix)
18658 VEC_free (dwarf2_per_cu_ptr,
f4dc4d17 18659 dwarf2_per_objfile->all_comp_units[ix]->s.imported_symtabs);
95554aad 18660
8b70b953 18661 VEC_free (dwarf2_section_info_def, data->types);
3019eac3
DE
18662
18663 if (data->dwo_files)
18664 free_dwo_files (data->dwo_files, objfile);
36586728
TT
18665
18666 if (data->dwz_file && data->dwz_file->dwz_bfd)
18667 gdb_bfd_unref (data->dwz_file->dwz_bfd);
9291a0cd
TT
18668}
18669
18670\f
ae2de4f8 18671/* The "save gdb-index" command. */
9291a0cd
TT
18672
18673/* The contents of the hash table we create when building the string
18674 table. */
18675struct strtab_entry
18676{
18677 offset_type offset;
18678 const char *str;
18679};
18680
559a7a62
JK
18681/* Hash function for a strtab_entry.
18682
18683 Function is used only during write_hash_table so no index format backward
18684 compatibility is needed. */
b89be57b 18685
9291a0cd
TT
18686static hashval_t
18687hash_strtab_entry (const void *e)
18688{
18689 const struct strtab_entry *entry = e;
559a7a62 18690 return mapped_index_string_hash (INT_MAX, entry->str);
9291a0cd
TT
18691}
18692
18693/* Equality function for a strtab_entry. */
b89be57b 18694
9291a0cd
TT
18695static int
18696eq_strtab_entry (const void *a, const void *b)
18697{
18698 const struct strtab_entry *ea = a;
18699 const struct strtab_entry *eb = b;
18700 return !strcmp (ea->str, eb->str);
18701}
18702
18703/* Create a strtab_entry hash table. */
b89be57b 18704
9291a0cd
TT
18705static htab_t
18706create_strtab (void)
18707{
18708 return htab_create_alloc (100, hash_strtab_entry, eq_strtab_entry,
18709 xfree, xcalloc, xfree);
18710}
18711
18712/* Add a string to the constant pool. Return the string's offset in
18713 host order. */
b89be57b 18714
9291a0cd
TT
18715static offset_type
18716add_string (htab_t table, struct obstack *cpool, const char *str)
18717{
18718 void **slot;
18719 struct strtab_entry entry;
18720 struct strtab_entry *result;
18721
18722 entry.str = str;
18723 slot = htab_find_slot (table, &entry, INSERT);
18724 if (*slot)
18725 result = *slot;
18726 else
18727 {
18728 result = XNEW (struct strtab_entry);
18729 result->offset = obstack_object_size (cpool);
18730 result->str = str;
18731 obstack_grow_str0 (cpool, str);
18732 *slot = result;
18733 }
18734 return result->offset;
18735}
18736
18737/* An entry in the symbol table. */
18738struct symtab_index_entry
18739{
18740 /* The name of the symbol. */
18741 const char *name;
18742 /* The offset of the name in the constant pool. */
18743 offset_type index_offset;
18744 /* A sorted vector of the indices of all the CUs that hold an object
18745 of this name. */
18746 VEC (offset_type) *cu_indices;
18747};
18748
18749/* The symbol table. This is a power-of-2-sized hash table. */
18750struct mapped_symtab
18751{
18752 offset_type n_elements;
18753 offset_type size;
18754 struct symtab_index_entry **data;
18755};
18756
18757/* Hash function for a symtab_index_entry. */
b89be57b 18758
9291a0cd
TT
18759static hashval_t
18760hash_symtab_entry (const void *e)
18761{
18762 const struct symtab_index_entry *entry = e;
18763 return iterative_hash (VEC_address (offset_type, entry->cu_indices),
18764 sizeof (offset_type) * VEC_length (offset_type,
18765 entry->cu_indices),
18766 0);
18767}
18768
18769/* Equality function for a symtab_index_entry. */
b89be57b 18770
9291a0cd
TT
18771static int
18772eq_symtab_entry (const void *a, const void *b)
18773{
18774 const struct symtab_index_entry *ea = a;
18775 const struct symtab_index_entry *eb = b;
18776 int len = VEC_length (offset_type, ea->cu_indices);
18777 if (len != VEC_length (offset_type, eb->cu_indices))
18778 return 0;
18779 return !memcmp (VEC_address (offset_type, ea->cu_indices),
18780 VEC_address (offset_type, eb->cu_indices),
18781 sizeof (offset_type) * len);
18782}
18783
18784/* Destroy a symtab_index_entry. */
b89be57b 18785
9291a0cd
TT
18786static void
18787delete_symtab_entry (void *p)
18788{
18789 struct symtab_index_entry *entry = p;
18790 VEC_free (offset_type, entry->cu_indices);
18791 xfree (entry);
18792}
18793
18794/* Create a hash table holding symtab_index_entry objects. */
b89be57b 18795
9291a0cd 18796static htab_t
3876f04e 18797create_symbol_hash_table (void)
9291a0cd
TT
18798{
18799 return htab_create_alloc (100, hash_symtab_entry, eq_symtab_entry,
18800 delete_symtab_entry, xcalloc, xfree);
18801}
18802
18803/* Create a new mapped symtab object. */
b89be57b 18804
9291a0cd
TT
18805static struct mapped_symtab *
18806create_mapped_symtab (void)
18807{
18808 struct mapped_symtab *symtab = XNEW (struct mapped_symtab);
18809 symtab->n_elements = 0;
18810 symtab->size = 1024;
18811 symtab->data = XCNEWVEC (struct symtab_index_entry *, symtab->size);
18812 return symtab;
18813}
18814
18815/* Destroy a mapped_symtab. */
b89be57b 18816
9291a0cd
TT
18817static void
18818cleanup_mapped_symtab (void *p)
18819{
18820 struct mapped_symtab *symtab = p;
18821 /* The contents of the array are freed when the other hash table is
18822 destroyed. */
18823 xfree (symtab->data);
18824 xfree (symtab);
18825}
18826
18827/* Find a slot in SYMTAB for the symbol NAME. Returns a pointer to
559a7a62
JK
18828 the slot.
18829
18830 Function is used only during write_hash_table so no index format backward
18831 compatibility is needed. */
b89be57b 18832
9291a0cd
TT
18833static struct symtab_index_entry **
18834find_slot (struct mapped_symtab *symtab, const char *name)
18835{
559a7a62 18836 offset_type index, step, hash = mapped_index_string_hash (INT_MAX, name);
9291a0cd
TT
18837
18838 index = hash & (symtab->size - 1);
18839 step = ((hash * 17) & (symtab->size - 1)) | 1;
18840
18841 for (;;)
18842 {
18843 if (!symtab->data[index] || !strcmp (name, symtab->data[index]->name))
18844 return &symtab->data[index];
18845 index = (index + step) & (symtab->size - 1);
18846 }
18847}
18848
18849/* Expand SYMTAB's hash table. */
b89be57b 18850
9291a0cd
TT
18851static void
18852hash_expand (struct mapped_symtab *symtab)
18853{
18854 offset_type old_size = symtab->size;
18855 offset_type i;
18856 struct symtab_index_entry **old_entries = symtab->data;
18857
18858 symtab->size *= 2;
18859 symtab->data = XCNEWVEC (struct symtab_index_entry *, symtab->size);
18860
18861 for (i = 0; i < old_size; ++i)
18862 {
18863 if (old_entries[i])
18864 {
18865 struct symtab_index_entry **slot = find_slot (symtab,
18866 old_entries[i]->name);
18867 *slot = old_entries[i];
18868 }
18869 }
18870
18871 xfree (old_entries);
18872}
18873
156942c7
DE
18874/* Add an entry to SYMTAB. NAME is the name of the symbol.
18875 CU_INDEX is the index of the CU in which the symbol appears.
18876 IS_STATIC is one if the symbol is static, otherwise zero (global). */
b89be57b 18877
9291a0cd
TT
18878static void
18879add_index_entry (struct mapped_symtab *symtab, const char *name,
156942c7 18880 int is_static, gdb_index_symbol_kind kind,
9291a0cd
TT
18881 offset_type cu_index)
18882{
18883 struct symtab_index_entry **slot;
156942c7 18884 offset_type cu_index_and_attrs;
9291a0cd
TT
18885
18886 ++symtab->n_elements;
18887 if (4 * symtab->n_elements / 3 >= symtab->size)
18888 hash_expand (symtab);
18889
18890 slot = find_slot (symtab, name);
18891 if (!*slot)
18892 {
18893 *slot = XNEW (struct symtab_index_entry);
18894 (*slot)->name = name;
156942c7 18895 /* index_offset is set later. */
9291a0cd
TT
18896 (*slot)->cu_indices = NULL;
18897 }
156942c7
DE
18898
18899 cu_index_and_attrs = 0;
18900 DW2_GDB_INDEX_CU_SET_VALUE (cu_index_and_attrs, cu_index);
18901 DW2_GDB_INDEX_SYMBOL_STATIC_SET_VALUE (cu_index_and_attrs, is_static);
18902 DW2_GDB_INDEX_SYMBOL_KIND_SET_VALUE (cu_index_and_attrs, kind);
18903
18904 /* We don't want to record an index value twice as we want to avoid the
18905 duplication.
18906 We process all global symbols and then all static symbols
18907 (which would allow us to avoid the duplication by only having to check
18908 the last entry pushed), but a symbol could have multiple kinds in one CU.
18909 To keep things simple we don't worry about the duplication here and
18910 sort and uniqufy the list after we've processed all symbols. */
18911 VEC_safe_push (offset_type, (*slot)->cu_indices, cu_index_and_attrs);
18912}
18913
18914/* qsort helper routine for uniquify_cu_indices. */
18915
18916static int
18917offset_type_compare (const void *ap, const void *bp)
18918{
18919 offset_type a = *(offset_type *) ap;
18920 offset_type b = *(offset_type *) bp;
18921
18922 return (a > b) - (b > a);
18923}
18924
18925/* Sort and remove duplicates of all symbols' cu_indices lists. */
18926
18927static void
18928uniquify_cu_indices (struct mapped_symtab *symtab)
18929{
18930 int i;
18931
18932 for (i = 0; i < symtab->size; ++i)
18933 {
18934 struct symtab_index_entry *entry = symtab->data[i];
18935
18936 if (entry
18937 && entry->cu_indices != NULL)
18938 {
18939 unsigned int next_to_insert, next_to_check;
18940 offset_type last_value;
18941
18942 qsort (VEC_address (offset_type, entry->cu_indices),
18943 VEC_length (offset_type, entry->cu_indices),
18944 sizeof (offset_type), offset_type_compare);
18945
18946 last_value = VEC_index (offset_type, entry->cu_indices, 0);
18947 next_to_insert = 1;
18948 for (next_to_check = 1;
18949 next_to_check < VEC_length (offset_type, entry->cu_indices);
18950 ++next_to_check)
18951 {
18952 if (VEC_index (offset_type, entry->cu_indices, next_to_check)
18953 != last_value)
18954 {
18955 last_value = VEC_index (offset_type, entry->cu_indices,
18956 next_to_check);
18957 VEC_replace (offset_type, entry->cu_indices, next_to_insert,
18958 last_value);
18959 ++next_to_insert;
18960 }
18961 }
18962 VEC_truncate (offset_type, entry->cu_indices, next_to_insert);
18963 }
18964 }
9291a0cd
TT
18965}
18966
18967/* Add a vector of indices to the constant pool. */
b89be57b 18968
9291a0cd 18969static offset_type
3876f04e 18970add_indices_to_cpool (htab_t symbol_hash_table, struct obstack *cpool,
9291a0cd
TT
18971 struct symtab_index_entry *entry)
18972{
18973 void **slot;
18974
3876f04e 18975 slot = htab_find_slot (symbol_hash_table, entry, INSERT);
9291a0cd
TT
18976 if (!*slot)
18977 {
18978 offset_type len = VEC_length (offset_type, entry->cu_indices);
18979 offset_type val = MAYBE_SWAP (len);
18980 offset_type iter;
18981 int i;
18982
18983 *slot = entry;
18984 entry->index_offset = obstack_object_size (cpool);
18985
18986 obstack_grow (cpool, &val, sizeof (val));
18987 for (i = 0;
18988 VEC_iterate (offset_type, entry->cu_indices, i, iter);
18989 ++i)
18990 {
18991 val = MAYBE_SWAP (iter);
18992 obstack_grow (cpool, &val, sizeof (val));
18993 }
18994 }
18995 else
18996 {
18997 struct symtab_index_entry *old_entry = *slot;
18998 entry->index_offset = old_entry->index_offset;
18999 entry = old_entry;
19000 }
19001 return entry->index_offset;
19002}
19003
19004/* Write the mapped hash table SYMTAB to the obstack OUTPUT, with
19005 constant pool entries going into the obstack CPOOL. */
b89be57b 19006
9291a0cd
TT
19007static void
19008write_hash_table (struct mapped_symtab *symtab,
19009 struct obstack *output, struct obstack *cpool)
19010{
19011 offset_type i;
3876f04e 19012 htab_t symbol_hash_table;
9291a0cd
TT
19013 htab_t str_table;
19014
3876f04e 19015 symbol_hash_table = create_symbol_hash_table ();
9291a0cd 19016 str_table = create_strtab ();
3876f04e 19017
9291a0cd
TT
19018 /* We add all the index vectors to the constant pool first, to
19019 ensure alignment is ok. */
19020 for (i = 0; i < symtab->size; ++i)
19021 {
19022 if (symtab->data[i])
3876f04e 19023 add_indices_to_cpool (symbol_hash_table, cpool, symtab->data[i]);
9291a0cd
TT
19024 }
19025
19026 /* Now write out the hash table. */
19027 for (i = 0; i < symtab->size; ++i)
19028 {
19029 offset_type str_off, vec_off;
19030
19031 if (symtab->data[i])
19032 {
19033 str_off = add_string (str_table, cpool, symtab->data[i]->name);
19034 vec_off = symtab->data[i]->index_offset;
19035 }
19036 else
19037 {
19038 /* While 0 is a valid constant pool index, it is not valid
19039 to have 0 for both offsets. */
19040 str_off = 0;
19041 vec_off = 0;
19042 }
19043
19044 str_off = MAYBE_SWAP (str_off);
19045 vec_off = MAYBE_SWAP (vec_off);
19046
19047 obstack_grow (output, &str_off, sizeof (str_off));
19048 obstack_grow (output, &vec_off, sizeof (vec_off));
19049 }
19050
19051 htab_delete (str_table);
3876f04e 19052 htab_delete (symbol_hash_table);
9291a0cd
TT
19053}
19054
0a5429f6
DE
19055/* Struct to map psymtab to CU index in the index file. */
19056struct psymtab_cu_index_map
19057{
19058 struct partial_symtab *psymtab;
19059 unsigned int cu_index;
19060};
19061
19062static hashval_t
19063hash_psymtab_cu_index (const void *item)
19064{
19065 const struct psymtab_cu_index_map *map = item;
19066
19067 return htab_hash_pointer (map->psymtab);
19068}
19069
19070static int
19071eq_psymtab_cu_index (const void *item_lhs, const void *item_rhs)
19072{
19073 const struct psymtab_cu_index_map *lhs = item_lhs;
19074 const struct psymtab_cu_index_map *rhs = item_rhs;
19075
19076 return lhs->psymtab == rhs->psymtab;
19077}
19078
19079/* Helper struct for building the address table. */
19080struct addrmap_index_data
19081{
19082 struct objfile *objfile;
19083 struct obstack *addr_obstack;
19084 htab_t cu_index_htab;
19085
19086 /* Non-zero if the previous_* fields are valid.
19087 We can't write an entry until we see the next entry (since it is only then
19088 that we know the end of the entry). */
19089 int previous_valid;
19090 /* Index of the CU in the table of all CUs in the index file. */
19091 unsigned int previous_cu_index;
0963b4bd 19092 /* Start address of the CU. */
0a5429f6
DE
19093 CORE_ADDR previous_cu_start;
19094};
19095
19096/* Write an address entry to OBSTACK. */
b89be57b 19097
9291a0cd 19098static void
0a5429f6
DE
19099add_address_entry (struct objfile *objfile, struct obstack *obstack,
19100 CORE_ADDR start, CORE_ADDR end, unsigned int cu_index)
9291a0cd 19101{
0a5429f6 19102 offset_type cu_index_to_write;
9291a0cd
TT
19103 char addr[8];
19104 CORE_ADDR baseaddr;
19105
19106 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
19107
0a5429f6
DE
19108 store_unsigned_integer (addr, 8, BFD_ENDIAN_LITTLE, start - baseaddr);
19109 obstack_grow (obstack, addr, 8);
19110 store_unsigned_integer (addr, 8, BFD_ENDIAN_LITTLE, end - baseaddr);
19111 obstack_grow (obstack, addr, 8);
19112 cu_index_to_write = MAYBE_SWAP (cu_index);
19113 obstack_grow (obstack, &cu_index_to_write, sizeof (offset_type));
19114}
19115
19116/* Worker function for traversing an addrmap to build the address table. */
19117
19118static int
19119add_address_entry_worker (void *datap, CORE_ADDR start_addr, void *obj)
19120{
19121 struct addrmap_index_data *data = datap;
19122 struct partial_symtab *pst = obj;
0a5429f6
DE
19123
19124 if (data->previous_valid)
19125 add_address_entry (data->objfile, data->addr_obstack,
19126 data->previous_cu_start, start_addr,
19127 data->previous_cu_index);
19128
19129 data->previous_cu_start = start_addr;
19130 if (pst != NULL)
19131 {
19132 struct psymtab_cu_index_map find_map, *map;
19133 find_map.psymtab = pst;
19134 map = htab_find (data->cu_index_htab, &find_map);
19135 gdb_assert (map != NULL);
19136 data->previous_cu_index = map->cu_index;
19137 data->previous_valid = 1;
19138 }
19139 else
19140 data->previous_valid = 0;
19141
19142 return 0;
19143}
19144
19145/* Write OBJFILE's address map to OBSTACK.
19146 CU_INDEX_HTAB is used to map addrmap entries to their CU indices
19147 in the index file. */
19148
19149static void
19150write_address_map (struct objfile *objfile, struct obstack *obstack,
19151 htab_t cu_index_htab)
19152{
19153 struct addrmap_index_data addrmap_index_data;
19154
19155 /* When writing the address table, we have to cope with the fact that
19156 the addrmap iterator only provides the start of a region; we have to
19157 wait until the next invocation to get the start of the next region. */
19158
19159 addrmap_index_data.objfile = objfile;
19160 addrmap_index_data.addr_obstack = obstack;
19161 addrmap_index_data.cu_index_htab = cu_index_htab;
19162 addrmap_index_data.previous_valid = 0;
19163
19164 addrmap_foreach (objfile->psymtabs_addrmap, add_address_entry_worker,
19165 &addrmap_index_data);
19166
19167 /* It's highly unlikely the last entry (end address = 0xff...ff)
19168 is valid, but we should still handle it.
19169 The end address is recorded as the start of the next region, but that
19170 doesn't work here. To cope we pass 0xff...ff, this is a rare situation
19171 anyway. */
19172 if (addrmap_index_data.previous_valid)
19173 add_address_entry (objfile, obstack,
19174 addrmap_index_data.previous_cu_start, (CORE_ADDR) -1,
19175 addrmap_index_data.previous_cu_index);
9291a0cd
TT
19176}
19177
156942c7
DE
19178/* Return the symbol kind of PSYM. */
19179
19180static gdb_index_symbol_kind
19181symbol_kind (struct partial_symbol *psym)
19182{
19183 domain_enum domain = PSYMBOL_DOMAIN (psym);
19184 enum address_class aclass = PSYMBOL_CLASS (psym);
19185
19186 switch (domain)
19187 {
19188 case VAR_DOMAIN:
19189 switch (aclass)
19190 {
19191 case LOC_BLOCK:
19192 return GDB_INDEX_SYMBOL_KIND_FUNCTION;
19193 case LOC_TYPEDEF:
19194 return GDB_INDEX_SYMBOL_KIND_TYPE;
19195 case LOC_COMPUTED:
19196 case LOC_CONST_BYTES:
19197 case LOC_OPTIMIZED_OUT:
19198 case LOC_STATIC:
19199 return GDB_INDEX_SYMBOL_KIND_VARIABLE;
19200 case LOC_CONST:
19201 /* Note: It's currently impossible to recognize psyms as enum values
19202 short of reading the type info. For now punt. */
19203 return GDB_INDEX_SYMBOL_KIND_VARIABLE;
19204 default:
19205 /* There are other LOC_FOO values that one might want to classify
19206 as variables, but dwarf2read.c doesn't currently use them. */
19207 return GDB_INDEX_SYMBOL_KIND_OTHER;
19208 }
19209 case STRUCT_DOMAIN:
19210 return GDB_INDEX_SYMBOL_KIND_TYPE;
19211 default:
19212 return GDB_INDEX_SYMBOL_KIND_OTHER;
19213 }
19214}
19215
9291a0cd 19216/* Add a list of partial symbols to SYMTAB. */
b89be57b 19217
9291a0cd
TT
19218static void
19219write_psymbols (struct mapped_symtab *symtab,
987d643c 19220 htab_t psyms_seen,
9291a0cd
TT
19221 struct partial_symbol **psymp,
19222 int count,
987d643c
TT
19223 offset_type cu_index,
19224 int is_static)
9291a0cd
TT
19225{
19226 for (; count-- > 0; ++psymp)
19227 {
156942c7
DE
19228 struct partial_symbol *psym = *psymp;
19229 void **slot;
987d643c 19230
156942c7 19231 if (SYMBOL_LANGUAGE (psym) == language_ada)
9291a0cd 19232 error (_("Ada is not currently supported by the index"));
987d643c 19233
987d643c 19234 /* Only add a given psymbol once. */
156942c7 19235 slot = htab_find_slot (psyms_seen, psym, INSERT);
987d643c
TT
19236 if (!*slot)
19237 {
156942c7
DE
19238 gdb_index_symbol_kind kind = symbol_kind (psym);
19239
19240 *slot = psym;
19241 add_index_entry (symtab, SYMBOL_SEARCH_NAME (psym),
19242 is_static, kind, cu_index);
987d643c 19243 }
9291a0cd
TT
19244 }
19245}
19246
19247/* Write the contents of an ("unfinished") obstack to FILE. Throw an
19248 exception if there is an error. */
b89be57b 19249
9291a0cd
TT
19250static void
19251write_obstack (FILE *file, struct obstack *obstack)
19252{
19253 if (fwrite (obstack_base (obstack), 1, obstack_object_size (obstack),
19254 file)
19255 != obstack_object_size (obstack))
19256 error (_("couldn't data write to file"));
19257}
19258
19259/* Unlink a file if the argument is not NULL. */
b89be57b 19260
9291a0cd
TT
19261static void
19262unlink_if_set (void *p)
19263{
19264 char **filename = p;
19265 if (*filename)
19266 unlink (*filename);
19267}
19268
1fd400ff
TT
19269/* A helper struct used when iterating over debug_types. */
19270struct signatured_type_index_data
19271{
19272 struct objfile *objfile;
19273 struct mapped_symtab *symtab;
19274 struct obstack *types_list;
987d643c 19275 htab_t psyms_seen;
1fd400ff
TT
19276 int cu_index;
19277};
19278
19279/* A helper function that writes a single signatured_type to an
19280 obstack. */
b89be57b 19281
1fd400ff
TT
19282static int
19283write_one_signatured_type (void **slot, void *d)
19284{
19285 struct signatured_type_index_data *info = d;
19286 struct signatured_type *entry = (struct signatured_type *) *slot;
e254ef6a
DE
19287 struct dwarf2_per_cu_data *per_cu = &entry->per_cu;
19288 struct partial_symtab *psymtab = per_cu->v.psymtab;
1fd400ff
TT
19289 gdb_byte val[8];
19290
19291 write_psymbols (info->symtab,
987d643c 19292 info->psyms_seen,
3e43a32a
MS
19293 info->objfile->global_psymbols.list
19294 + psymtab->globals_offset,
987d643c
TT
19295 psymtab->n_global_syms, info->cu_index,
19296 0);
1fd400ff 19297 write_psymbols (info->symtab,
987d643c 19298 info->psyms_seen,
3e43a32a
MS
19299 info->objfile->static_psymbols.list
19300 + psymtab->statics_offset,
987d643c
TT
19301 psymtab->n_static_syms, info->cu_index,
19302 1);
1fd400ff 19303
b64f50a1
JK
19304 store_unsigned_integer (val, 8, BFD_ENDIAN_LITTLE,
19305 entry->per_cu.offset.sect_off);
1fd400ff 19306 obstack_grow (info->types_list, val, 8);
3019eac3
DE
19307 store_unsigned_integer (val, 8, BFD_ENDIAN_LITTLE,
19308 entry->type_offset_in_tu.cu_off);
1fd400ff
TT
19309 obstack_grow (info->types_list, val, 8);
19310 store_unsigned_integer (val, 8, BFD_ENDIAN_LITTLE, entry->signature);
19311 obstack_grow (info->types_list, val, 8);
19312
19313 ++info->cu_index;
19314
19315 return 1;
19316}
19317
95554aad
TT
19318/* Recurse into all "included" dependencies and write their symbols as
19319 if they appeared in this psymtab. */
19320
19321static void
19322recursively_write_psymbols (struct objfile *objfile,
19323 struct partial_symtab *psymtab,
19324 struct mapped_symtab *symtab,
19325 htab_t psyms_seen,
19326 offset_type cu_index)
19327{
19328 int i;
19329
19330 for (i = 0; i < psymtab->number_of_dependencies; ++i)
19331 if (psymtab->dependencies[i]->user != NULL)
19332 recursively_write_psymbols (objfile, psymtab->dependencies[i],
19333 symtab, psyms_seen, cu_index);
19334
19335 write_psymbols (symtab,
19336 psyms_seen,
19337 objfile->global_psymbols.list + psymtab->globals_offset,
19338 psymtab->n_global_syms, cu_index,
19339 0);
19340 write_psymbols (symtab,
19341 psyms_seen,
19342 objfile->static_psymbols.list + psymtab->statics_offset,
19343 psymtab->n_static_syms, cu_index,
19344 1);
19345}
19346
9291a0cd 19347/* Create an index file for OBJFILE in the directory DIR. */
b89be57b 19348
9291a0cd
TT
19349static void
19350write_psymtabs_to_index (struct objfile *objfile, const char *dir)
19351{
19352 struct cleanup *cleanup;
19353 char *filename, *cleanup_filename;
1fd400ff
TT
19354 struct obstack contents, addr_obstack, constant_pool, symtab_obstack;
19355 struct obstack cu_list, types_cu_list;
9291a0cd
TT
19356 int i;
19357 FILE *out_file;
19358 struct mapped_symtab *symtab;
19359 offset_type val, size_of_contents, total_len;
19360 struct stat st;
987d643c 19361 htab_t psyms_seen;
0a5429f6
DE
19362 htab_t cu_index_htab;
19363 struct psymtab_cu_index_map *psymtab_cu_index_map;
9291a0cd 19364
b4f2f049 19365 if (!objfile->psymtabs || !objfile->psymtabs_addrmap)
9291a0cd 19366 return;
b4f2f049 19367
9291a0cd
TT
19368 if (dwarf2_per_objfile->using_index)
19369 error (_("Cannot use an index to create the index"));
19370
8b70b953
TT
19371 if (VEC_length (dwarf2_section_info_def, dwarf2_per_objfile->types) > 1)
19372 error (_("Cannot make an index when the file has multiple .debug_types sections"));
19373
9291a0cd 19374 if (stat (objfile->name, &st) < 0)
7e17e088 19375 perror_with_name (objfile->name);
9291a0cd
TT
19376
19377 filename = concat (dir, SLASH_STRING, lbasename (objfile->name),
19378 INDEX_SUFFIX, (char *) NULL);
19379 cleanup = make_cleanup (xfree, filename);
19380
19381 out_file = fopen (filename, "wb");
19382 if (!out_file)
19383 error (_("Can't open `%s' for writing"), filename);
19384
19385 cleanup_filename = filename;
19386 make_cleanup (unlink_if_set, &cleanup_filename);
19387
19388 symtab = create_mapped_symtab ();
19389 make_cleanup (cleanup_mapped_symtab, symtab);
19390
19391 obstack_init (&addr_obstack);
19392 make_cleanup_obstack_free (&addr_obstack);
19393
19394 obstack_init (&cu_list);
19395 make_cleanup_obstack_free (&cu_list);
19396
1fd400ff
TT
19397 obstack_init (&types_cu_list);
19398 make_cleanup_obstack_free (&types_cu_list);
19399
987d643c
TT
19400 psyms_seen = htab_create_alloc (100, htab_hash_pointer, htab_eq_pointer,
19401 NULL, xcalloc, xfree);
96408a79 19402 make_cleanup_htab_delete (psyms_seen);
987d643c 19403
0a5429f6
DE
19404 /* While we're scanning CU's create a table that maps a psymtab pointer
19405 (which is what addrmap records) to its index (which is what is recorded
19406 in the index file). This will later be needed to write the address
19407 table. */
19408 cu_index_htab = htab_create_alloc (100,
19409 hash_psymtab_cu_index,
19410 eq_psymtab_cu_index,
19411 NULL, xcalloc, xfree);
96408a79 19412 make_cleanup_htab_delete (cu_index_htab);
0a5429f6
DE
19413 psymtab_cu_index_map = (struct psymtab_cu_index_map *)
19414 xmalloc (sizeof (struct psymtab_cu_index_map)
19415 * dwarf2_per_objfile->n_comp_units);
19416 make_cleanup (xfree, psymtab_cu_index_map);
19417
19418 /* The CU list is already sorted, so we don't need to do additional
1fd400ff
TT
19419 work here. Also, the debug_types entries do not appear in
19420 all_comp_units, but only in their own hash table. */
9291a0cd
TT
19421 for (i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
19422 {
3e43a32a
MS
19423 struct dwarf2_per_cu_data *per_cu
19424 = dwarf2_per_objfile->all_comp_units[i];
e254ef6a 19425 struct partial_symtab *psymtab = per_cu->v.psymtab;
9291a0cd 19426 gdb_byte val[8];
0a5429f6
DE
19427 struct psymtab_cu_index_map *map;
19428 void **slot;
9291a0cd 19429
95554aad
TT
19430 if (psymtab->user == NULL)
19431 recursively_write_psymbols (objfile, psymtab, symtab, psyms_seen, i);
9291a0cd 19432
0a5429f6
DE
19433 map = &psymtab_cu_index_map[i];
19434 map->psymtab = psymtab;
19435 map->cu_index = i;
19436 slot = htab_find_slot (cu_index_htab, map, INSERT);
19437 gdb_assert (slot != NULL);
19438 gdb_assert (*slot == NULL);
19439 *slot = map;
9291a0cd 19440
b64f50a1
JK
19441 store_unsigned_integer (val, 8, BFD_ENDIAN_LITTLE,
19442 per_cu->offset.sect_off);
9291a0cd 19443 obstack_grow (&cu_list, val, 8);
e254ef6a 19444 store_unsigned_integer (val, 8, BFD_ENDIAN_LITTLE, per_cu->length);
9291a0cd
TT
19445 obstack_grow (&cu_list, val, 8);
19446 }
19447
0a5429f6
DE
19448 /* Dump the address map. */
19449 write_address_map (objfile, &addr_obstack, cu_index_htab);
19450
1fd400ff
TT
19451 /* Write out the .debug_type entries, if any. */
19452 if (dwarf2_per_objfile->signatured_types)
19453 {
19454 struct signatured_type_index_data sig_data;
19455
19456 sig_data.objfile = objfile;
19457 sig_data.symtab = symtab;
19458 sig_data.types_list = &types_cu_list;
987d643c 19459 sig_data.psyms_seen = psyms_seen;
1fd400ff
TT
19460 sig_data.cu_index = dwarf2_per_objfile->n_comp_units;
19461 htab_traverse_noresize (dwarf2_per_objfile->signatured_types,
19462 write_one_signatured_type, &sig_data);
19463 }
19464
156942c7
DE
19465 /* Now that we've processed all symbols we can shrink their cu_indices
19466 lists. */
19467 uniquify_cu_indices (symtab);
19468
9291a0cd
TT
19469 obstack_init (&constant_pool);
19470 make_cleanup_obstack_free (&constant_pool);
19471 obstack_init (&symtab_obstack);
19472 make_cleanup_obstack_free (&symtab_obstack);
19473 write_hash_table (symtab, &symtab_obstack, &constant_pool);
19474
19475 obstack_init (&contents);
19476 make_cleanup_obstack_free (&contents);
1fd400ff 19477 size_of_contents = 6 * sizeof (offset_type);
9291a0cd
TT
19478 total_len = size_of_contents;
19479
19480 /* The version number. */
156942c7 19481 val = MAYBE_SWAP (7);
9291a0cd
TT
19482 obstack_grow (&contents, &val, sizeof (val));
19483
19484 /* The offset of the CU list from the start of the file. */
19485 val = MAYBE_SWAP (total_len);
19486 obstack_grow (&contents, &val, sizeof (val));
19487 total_len += obstack_object_size (&cu_list);
19488
1fd400ff
TT
19489 /* The offset of the types CU list from the start of the file. */
19490 val = MAYBE_SWAP (total_len);
19491 obstack_grow (&contents, &val, sizeof (val));
19492 total_len += obstack_object_size (&types_cu_list);
19493
9291a0cd
TT
19494 /* The offset of the address table from the start of the file. */
19495 val = MAYBE_SWAP (total_len);
19496 obstack_grow (&contents, &val, sizeof (val));
19497 total_len += obstack_object_size (&addr_obstack);
19498
19499 /* The offset of the symbol table from the start of the file. */
19500 val = MAYBE_SWAP (total_len);
19501 obstack_grow (&contents, &val, sizeof (val));
19502 total_len += obstack_object_size (&symtab_obstack);
19503
19504 /* The offset of the constant pool from the start of the file. */
19505 val = MAYBE_SWAP (total_len);
19506 obstack_grow (&contents, &val, sizeof (val));
19507 total_len += obstack_object_size (&constant_pool);
19508
19509 gdb_assert (obstack_object_size (&contents) == size_of_contents);
19510
19511 write_obstack (out_file, &contents);
19512 write_obstack (out_file, &cu_list);
1fd400ff 19513 write_obstack (out_file, &types_cu_list);
9291a0cd
TT
19514 write_obstack (out_file, &addr_obstack);
19515 write_obstack (out_file, &symtab_obstack);
19516 write_obstack (out_file, &constant_pool);
19517
19518 fclose (out_file);
19519
19520 /* We want to keep the file, so we set cleanup_filename to NULL
19521 here. See unlink_if_set. */
19522 cleanup_filename = NULL;
19523
19524 do_cleanups (cleanup);
19525}
19526
90476074
TT
19527/* Implementation of the `save gdb-index' command.
19528
19529 Note that the file format used by this command is documented in the
19530 GDB manual. Any changes here must be documented there. */
11570e71 19531
9291a0cd
TT
19532static void
19533save_gdb_index_command (char *arg, int from_tty)
19534{
19535 struct objfile *objfile;
19536
19537 if (!arg || !*arg)
96d19272 19538 error (_("usage: save gdb-index DIRECTORY"));
9291a0cd
TT
19539
19540 ALL_OBJFILES (objfile)
19541 {
19542 struct stat st;
19543
19544 /* If the objfile does not correspond to an actual file, skip it. */
19545 if (stat (objfile->name, &st) < 0)
19546 continue;
19547
19548 dwarf2_per_objfile = objfile_data (objfile, dwarf2_objfile_data_key);
19549 if (dwarf2_per_objfile)
19550 {
19551 volatile struct gdb_exception except;
19552
19553 TRY_CATCH (except, RETURN_MASK_ERROR)
19554 {
19555 write_psymtabs_to_index (objfile, arg);
19556 }
19557 if (except.reason < 0)
19558 exception_fprintf (gdb_stderr, except,
19559 _("Error while writing index for `%s': "),
19560 objfile->name);
19561 }
19562 }
dce234bc
PP
19563}
19564
9291a0cd
TT
19565\f
19566
9eae7c52
TT
19567int dwarf2_always_disassemble;
19568
19569static void
19570show_dwarf2_always_disassemble (struct ui_file *file, int from_tty,
19571 struct cmd_list_element *c, const char *value)
19572{
3e43a32a
MS
19573 fprintf_filtered (file,
19574 _("Whether to always disassemble "
19575 "DWARF expressions is %s.\n"),
9eae7c52
TT
19576 value);
19577}
19578
900e11f9
JK
19579static void
19580show_check_physname (struct ui_file *file, int from_tty,
19581 struct cmd_list_element *c, const char *value)
19582{
19583 fprintf_filtered (file,
19584 _("Whether to check \"physname\" is %s.\n"),
19585 value);
19586}
19587
6502dd73
DJ
19588void _initialize_dwarf2_read (void);
19589
19590void
19591_initialize_dwarf2_read (void)
19592{
96d19272
JK
19593 struct cmd_list_element *c;
19594
dce234bc 19595 dwarf2_objfile_data_key
c1bd65d0 19596 = register_objfile_data_with_cleanup (NULL, dwarf2_per_objfile_free);
ae038cb0 19597
1bedd215
AC
19598 add_prefix_cmd ("dwarf2", class_maintenance, set_dwarf2_cmd, _("\
19599Set DWARF 2 specific variables.\n\
19600Configure DWARF 2 variables such as the cache size"),
ae038cb0
DJ
19601 &set_dwarf2_cmdlist, "maintenance set dwarf2 ",
19602 0/*allow-unknown*/, &maintenance_set_cmdlist);
19603
1bedd215
AC
19604 add_prefix_cmd ("dwarf2", class_maintenance, show_dwarf2_cmd, _("\
19605Show DWARF 2 specific variables\n\
19606Show DWARF 2 variables such as the cache size"),
ae038cb0
DJ
19607 &show_dwarf2_cmdlist, "maintenance show dwarf2 ",
19608 0/*allow-unknown*/, &maintenance_show_cmdlist);
19609
19610 add_setshow_zinteger_cmd ("max-cache-age", class_obscure,
7915a72c
AC
19611 &dwarf2_max_cache_age, _("\
19612Set the upper bound on the age of cached dwarf2 compilation units."), _("\
19613Show the upper bound on the age of cached dwarf2 compilation units."), _("\
19614A higher limit means that cached compilation units will be stored\n\
19615in memory longer, and more total memory will be used. Zero disables\n\
19616caching, which can slow down startup."),
2c5b56ce 19617 NULL,
920d2a44 19618 show_dwarf2_max_cache_age,
2c5b56ce 19619 &set_dwarf2_cmdlist,
ae038cb0 19620 &show_dwarf2_cmdlist);
d97bc12b 19621
9eae7c52
TT
19622 add_setshow_boolean_cmd ("always-disassemble", class_obscure,
19623 &dwarf2_always_disassemble, _("\
19624Set whether `info address' always disassembles DWARF expressions."), _("\
19625Show whether `info address' always disassembles DWARF expressions."), _("\
19626When enabled, DWARF expressions are always printed in an assembly-like\n\
19627syntax. When disabled, expressions will be printed in a more\n\
19628conversational style, when possible."),
19629 NULL,
19630 show_dwarf2_always_disassemble,
19631 &set_dwarf2_cmdlist,
19632 &show_dwarf2_cmdlist);
19633
45cfd468
DE
19634 add_setshow_boolean_cmd ("dwarf2-read", no_class, &dwarf2_read_debug, _("\
19635Set debugging of the dwarf2 reader."), _("\
19636Show debugging of the dwarf2 reader."), _("\
19637When enabled, debugging messages are printed during dwarf2 reading\n\
19638and symtab expansion."),
19639 NULL,
19640 NULL,
19641 &setdebuglist, &showdebuglist);
19642
d97bc12b
DE
19643 add_setshow_zinteger_cmd ("dwarf2-die", no_class, &dwarf2_die_debug, _("\
19644Set debugging of the dwarf2 DIE reader."), _("\
19645Show debugging of the dwarf2 DIE reader."), _("\
19646When enabled (non-zero), DIEs are dumped after they are read in.\n\
19647The value is the maximum depth to print."),
19648 NULL,
19649 NULL,
19650 &setdebuglist, &showdebuglist);
9291a0cd 19651
900e11f9
JK
19652 add_setshow_boolean_cmd ("check-physname", no_class, &check_physname, _("\
19653Set cross-checking of \"physname\" code against demangler."), _("\
19654Show cross-checking of \"physname\" code against demangler."), _("\
19655When enabled, GDB's internal \"physname\" code is checked against\n\
19656the demangler."),
19657 NULL, show_check_physname,
19658 &setdebuglist, &showdebuglist);
19659
e615022a
DE
19660 add_setshow_boolean_cmd ("use-deprecated-index-sections",
19661 no_class, &use_deprecated_index_sections, _("\
19662Set whether to use deprecated gdb_index sections."), _("\
19663Show whether to use deprecated gdb_index sections."), _("\
19664When enabled, deprecated .gdb_index sections are used anyway.\n\
19665Normally they are ignored either because of a missing feature or\n\
19666performance issue.\n\
19667Warning: This option must be enabled before gdb reads the file."),
19668 NULL,
19669 NULL,
19670 &setlist, &showlist);
19671
96d19272 19672 c = add_cmd ("gdb-index", class_files, save_gdb_index_command,
11570e71 19673 _("\
fc1a9d6e 19674Save a gdb-index file.\n\
11570e71 19675Usage: save gdb-index DIRECTORY"),
96d19272
JK
19676 &save_cmdlist);
19677 set_cmd_completer (c, filename_completer);
6502dd73 19678}
This page took 3.563949 seconds and 4 git commands to generate.