PR other/54620
[deliverable/binutils-gdb.git] / gdb / dwarf2read.c
CommitLineData
c906108c 1/* DWARF 2 debugging format support for GDB.
917c78fc 2
28e7fd62 3 Copyright (C) 1994-2013 Free Software Foundation, Inc.
c906108c
SS
4
5 Adapted by Gary Funck (gary@intrepid.com), Intrepid Technology,
6 Inc. with support from Florida State University (under contract
7 with the Ada Joint Program Office), and Silicon Graphics, Inc.
8 Initial contribution by Brent Benson, Harris Computer Systems, Inc.,
9 based on Fred Fish's (Cygnus Support) implementation of DWARF 1
7ce59000 10 support.
c906108c 11
c5aa993b 12 This file is part of GDB.
c906108c 13
c5aa993b
JM
14 This program is free software; you can redistribute it and/or modify
15 it under the terms of the GNU General Public License as published by
a9762ec7
JB
16 the Free Software Foundation; either version 3 of the License, or
17 (at your option) any later version.
c906108c 18
a9762ec7
JB
19 This program is distributed in the hope that it will be useful,
20 but WITHOUT ANY WARRANTY; without even the implied warranty of
21 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22 GNU General Public License for more details.
c906108c 23
c5aa993b 24 You should have received a copy of the GNU General Public License
a9762ec7 25 along with this program. If not, see <http://www.gnu.org/licenses/>. */
c906108c 26
21b2bd31
DE
27/* FIXME: Various die-reading functions need to be more careful with
28 reading off the end of the section.
29 E.g., load_partial_dies, read_partial_die. */
30
c906108c
SS
31#include "defs.h"
32#include "bfd.h"
80626a55 33#include "elf-bfd.h"
c906108c
SS
34#include "symtab.h"
35#include "gdbtypes.h"
c906108c 36#include "objfiles.h"
fa8f86ff 37#include "dwarf2.h"
c906108c
SS
38#include "buildsym.h"
39#include "demangle.h"
50f182aa 40#include "gdb-demangle.h"
c906108c 41#include "expression.h"
d5166ae1 42#include "filenames.h" /* for DOSish file names */
2e276125 43#include "macrotab.h"
c906108c
SS
44#include "language.h"
45#include "complaints.h"
357e46e7 46#include "bcache.h"
4c2df51b
DJ
47#include "dwarf2expr.h"
48#include "dwarf2loc.h"
9219021c 49#include "cp-support.h"
72bf9492 50#include "hashtab.h"
ae038cb0
DJ
51#include "command.h"
52#include "gdbcmd.h"
edb3359d 53#include "block.h"
ff013f42 54#include "addrmap.h"
94af9270
KS
55#include "typeprint.h"
56#include "jv-lang.h"
ccefe4c4 57#include "psympriv.h"
9291a0cd
TT
58#include "exceptions.h"
59#include "gdb_stat.h"
96d19272 60#include "completer.h"
34eaf542 61#include "vec.h"
98bfdba5 62#include "c-lang.h"
a766d390 63#include "go-lang.h"
98bfdba5 64#include "valprint.h"
3019eac3 65#include "gdbcore.h" /* for gnutarget */
156942c7 66#include "gdb/gdb-index.h"
60d5a603 67#include <ctype.h>
cbb099e8 68#include "gdb_bfd.h"
4357ac6c 69#include "f-lang.h"
4c2df51b 70
c906108c
SS
71#include <fcntl.h>
72#include "gdb_string.h"
4bdf3d34 73#include "gdb_assert.h"
c906108c 74#include <sys/types.h>
d8151005 75
34eaf542
TT
76typedef struct symbol *symbolp;
77DEF_VEC_P (symbolp);
78
45cfd468
DE
79/* When non-zero, print basic high level tracing messages.
80 This is in contrast to the low level DIE reading of dwarf2_die_debug. */
81static int dwarf2_read_debug = 0;
82
d97bc12b 83/* When non-zero, dump DIEs after they are read in. */
ccce17b0 84static unsigned int dwarf2_die_debug = 0;
d97bc12b 85
900e11f9
JK
86/* When non-zero, cross-check physname against demangler. */
87static int check_physname = 0;
88
481860b3 89/* When non-zero, do not reject deprecated .gdb_index sections. */
e615022a 90static int use_deprecated_index_sections = 0;
481860b3 91
6502dd73
DJ
92static const struct objfile_data *dwarf2_objfile_data_key;
93
dce234bc
PP
94struct dwarf2_section_info
95{
96 asection *asection;
97 gdb_byte *buffer;
98 bfd_size_type size;
be391dca
TT
99 /* True if we have tried to read this section. */
100 int readin;
dce234bc
PP
101};
102
8b70b953
TT
103typedef struct dwarf2_section_info dwarf2_section_info_def;
104DEF_VEC_O (dwarf2_section_info_def);
105
9291a0cd
TT
106/* All offsets in the index are of this type. It must be
107 architecture-independent. */
108typedef uint32_t offset_type;
109
110DEF_VEC_I (offset_type);
111
156942c7
DE
112/* Ensure only legit values are used. */
113#define DW2_GDB_INDEX_SYMBOL_STATIC_SET_VALUE(cu_index, value) \
114 do { \
115 gdb_assert ((unsigned int) (value) <= 1); \
116 GDB_INDEX_SYMBOL_STATIC_SET_VALUE((cu_index), (value)); \
117 } while (0)
118
119/* Ensure only legit values are used. */
120#define DW2_GDB_INDEX_SYMBOL_KIND_SET_VALUE(cu_index, value) \
121 do { \
122 gdb_assert ((value) >= GDB_INDEX_SYMBOL_KIND_TYPE \
123 && (value) <= GDB_INDEX_SYMBOL_KIND_OTHER); \
124 GDB_INDEX_SYMBOL_KIND_SET_VALUE((cu_index), (value)); \
125 } while (0)
126
127/* Ensure we don't use more than the alloted nuber of bits for the CU. */
128#define DW2_GDB_INDEX_CU_SET_VALUE(cu_index, value) \
129 do { \
130 gdb_assert (((value) & ~GDB_INDEX_CU_MASK) == 0); \
131 GDB_INDEX_CU_SET_VALUE((cu_index), (value)); \
132 } while (0)
133
9291a0cd
TT
134/* A description of the mapped index. The file format is described in
135 a comment by the code that writes the index. */
136struct mapped_index
137{
559a7a62
JK
138 /* Index data format version. */
139 int version;
140
9291a0cd
TT
141 /* The total length of the buffer. */
142 off_t total_size;
b11b1f88 143
9291a0cd
TT
144 /* A pointer to the address table data. */
145 const gdb_byte *address_table;
b11b1f88 146
9291a0cd
TT
147 /* Size of the address table data in bytes. */
148 offset_type address_table_size;
b11b1f88 149
3876f04e
DE
150 /* The symbol table, implemented as a hash table. */
151 const offset_type *symbol_table;
b11b1f88 152
9291a0cd 153 /* Size in slots, each slot is 2 offset_types. */
3876f04e 154 offset_type symbol_table_slots;
b11b1f88 155
9291a0cd
TT
156 /* A pointer to the constant pool. */
157 const char *constant_pool;
158};
159
95554aad
TT
160typedef struct dwarf2_per_cu_data *dwarf2_per_cu_ptr;
161DEF_VEC_P (dwarf2_per_cu_ptr);
162
9cdd5dbd
DE
163/* Collection of data recorded per objfile.
164 This hangs off of dwarf2_objfile_data_key. */
165
6502dd73
DJ
166struct dwarf2_per_objfile
167{
dce234bc
PP
168 struct dwarf2_section_info info;
169 struct dwarf2_section_info abbrev;
170 struct dwarf2_section_info line;
dce234bc
PP
171 struct dwarf2_section_info loc;
172 struct dwarf2_section_info macinfo;
cf2c3c16 173 struct dwarf2_section_info macro;
dce234bc
PP
174 struct dwarf2_section_info str;
175 struct dwarf2_section_info ranges;
3019eac3 176 struct dwarf2_section_info addr;
dce234bc
PP
177 struct dwarf2_section_info frame;
178 struct dwarf2_section_info eh_frame;
9291a0cd 179 struct dwarf2_section_info gdb_index;
ae038cb0 180
8b70b953
TT
181 VEC (dwarf2_section_info_def) *types;
182
be391dca
TT
183 /* Back link. */
184 struct objfile *objfile;
185
d467dd73 186 /* Table of all the compilation units. This is used to locate
10b3939b 187 the target compilation unit of a particular reference. */
ae038cb0
DJ
188 struct dwarf2_per_cu_data **all_comp_units;
189
190 /* The number of compilation units in ALL_COMP_UNITS. */
191 int n_comp_units;
192
1fd400ff 193 /* The number of .debug_types-related CUs. */
d467dd73 194 int n_type_units;
1fd400ff 195
d467dd73 196 /* The .debug_types-related CUs (TUs). */
b4dd5633 197 struct signatured_type **all_type_units;
1fd400ff 198
f4dc4d17
DE
199 /* The number of entries in all_type_unit_groups. */
200 int n_type_unit_groups;
201
202 /* Table of type unit groups.
203 This exists to make it easy to iterate over all CUs and TU groups. */
204 struct type_unit_group **all_type_unit_groups;
205
206 /* Table of struct type_unit_group objects.
207 The hash key is the DW_AT_stmt_list value. */
208 htab_t type_unit_groups;
72dca2f5 209
348e048f
DE
210 /* A table mapping .debug_types signatures to its signatured_type entry.
211 This is NULL if the .debug_types section hasn't been read in yet. */
212 htab_t signatured_types;
213
f4dc4d17
DE
214 /* Type unit statistics, to see how well the scaling improvements
215 are doing. */
216 struct tu_stats
217 {
218 int nr_uniq_abbrev_tables;
219 int nr_symtabs;
220 int nr_symtab_sharers;
221 int nr_stmt_less_type_units;
222 } tu_stats;
223
224 /* A chain of compilation units that are currently read in, so that
225 they can be freed later. */
226 struct dwarf2_per_cu_data *read_in_chain;
227
3019eac3
DE
228 /* A table mapping DW_AT_dwo_name values to struct dwo_file objects.
229 This is NULL if the table hasn't been allocated yet. */
230 htab_t dwo_files;
231
80626a55
DE
232 /* Non-zero if we've check for whether there is a DWP file. */
233 int dwp_checked;
234
235 /* The DWP file if there is one, or NULL. */
236 struct dwp_file *dwp_file;
237
36586728
TT
238 /* The shared '.dwz' file, if one exists. This is used when the
239 original data was compressed using 'dwz -m'. */
240 struct dwz_file *dwz_file;
241
72dca2f5
FR
242 /* A flag indicating wether this objfile has a section loaded at a
243 VMA of 0. */
244 int has_section_at_zero;
9291a0cd 245
ae2de4f8
DE
246 /* True if we are using the mapped index,
247 or we are faking it for OBJF_READNOW's sake. */
9291a0cd
TT
248 unsigned char using_index;
249
ae2de4f8 250 /* The mapped index, or NULL if .gdb_index is missing or not being used. */
9291a0cd 251 struct mapped_index *index_table;
98bfdba5 252
7b9f3c50 253 /* When using index_table, this keeps track of all quick_file_names entries.
56e64610
DE
254 TUs typically share line table entries with a CU, so we maintain a
255 separate table of all line table entries to support the sharing.
256 Note that while there can be way more TUs than CUs, we've already
257 sorted all the TUs into "type unit groups", grouped by their
258 DW_AT_stmt_list value. Therefore the only sharing done here is with a
259 CU and its associated TU group if there is one. */
7b9f3c50
DE
260 htab_t quick_file_names_table;
261
98bfdba5
PA
262 /* Set during partial symbol reading, to prevent queueing of full
263 symbols. */
264 int reading_partial_symbols;
673bfd45 265
dee91e82 266 /* Table mapping type DIEs to their struct type *.
673bfd45 267 This is NULL if not allocated yet.
dee91e82
DE
268 The mapping is done via (CU/TU signature + DIE offset) -> type. */
269 htab_t die_type_hash;
95554aad
TT
270
271 /* The CUs we recently read. */
272 VEC (dwarf2_per_cu_ptr) *just_read_cus;
6502dd73
DJ
273};
274
275static struct dwarf2_per_objfile *dwarf2_per_objfile;
c906108c 276
251d32d9 277/* Default names of the debugging sections. */
c906108c 278
233a11ab
CS
279/* Note that if the debugging section has been compressed, it might
280 have a name like .zdebug_info. */
281
9cdd5dbd
DE
282static const struct dwarf2_debug_sections dwarf2_elf_names =
283{
251d32d9
TG
284 { ".debug_info", ".zdebug_info" },
285 { ".debug_abbrev", ".zdebug_abbrev" },
286 { ".debug_line", ".zdebug_line" },
287 { ".debug_loc", ".zdebug_loc" },
288 { ".debug_macinfo", ".zdebug_macinfo" },
cf2c3c16 289 { ".debug_macro", ".zdebug_macro" },
251d32d9
TG
290 { ".debug_str", ".zdebug_str" },
291 { ".debug_ranges", ".zdebug_ranges" },
292 { ".debug_types", ".zdebug_types" },
3019eac3 293 { ".debug_addr", ".zdebug_addr" },
251d32d9
TG
294 { ".debug_frame", ".zdebug_frame" },
295 { ".eh_frame", NULL },
24d3216f
TT
296 { ".gdb_index", ".zgdb_index" },
297 23
251d32d9 298};
c906108c 299
80626a55 300/* List of DWO/DWP sections. */
3019eac3 301
80626a55 302static const struct dwop_section_names
3019eac3
DE
303{
304 struct dwarf2_section_names abbrev_dwo;
305 struct dwarf2_section_names info_dwo;
306 struct dwarf2_section_names line_dwo;
307 struct dwarf2_section_names loc_dwo;
09262596
DE
308 struct dwarf2_section_names macinfo_dwo;
309 struct dwarf2_section_names macro_dwo;
3019eac3
DE
310 struct dwarf2_section_names str_dwo;
311 struct dwarf2_section_names str_offsets_dwo;
312 struct dwarf2_section_names types_dwo;
80626a55
DE
313 struct dwarf2_section_names cu_index;
314 struct dwarf2_section_names tu_index;
3019eac3 315}
80626a55 316dwop_section_names =
3019eac3
DE
317{
318 { ".debug_abbrev.dwo", ".zdebug_abbrev.dwo" },
319 { ".debug_info.dwo", ".zdebug_info.dwo" },
320 { ".debug_line.dwo", ".zdebug_line.dwo" },
321 { ".debug_loc.dwo", ".zdebug_loc.dwo" },
09262596
DE
322 { ".debug_macinfo.dwo", ".zdebug_macinfo.dwo" },
323 { ".debug_macro.dwo", ".zdebug_macro.dwo" },
3019eac3
DE
324 { ".debug_str.dwo", ".zdebug_str.dwo" },
325 { ".debug_str_offsets.dwo", ".zdebug_str_offsets.dwo" },
326 { ".debug_types.dwo", ".zdebug_types.dwo" },
80626a55
DE
327 { ".debug_cu_index", ".zdebug_cu_index" },
328 { ".debug_tu_index", ".zdebug_tu_index" },
3019eac3
DE
329};
330
c906108c
SS
331/* local data types */
332
107d2387
AC
333/* The data in a compilation unit header, after target2host
334 translation, looks like this. */
c906108c 335struct comp_unit_head
a738430d 336{
c764a876 337 unsigned int length;
a738430d 338 short version;
a738430d
MK
339 unsigned char addr_size;
340 unsigned char signed_addr_p;
b64f50a1 341 sect_offset abbrev_offset;
57349743 342
a738430d
MK
343 /* Size of file offsets; either 4 or 8. */
344 unsigned int offset_size;
57349743 345
a738430d
MK
346 /* Size of the length field; either 4 or 12. */
347 unsigned int initial_length_size;
57349743 348
a738430d
MK
349 /* Offset to the first byte of this compilation unit header in the
350 .debug_info section, for resolving relative reference dies. */
b64f50a1 351 sect_offset offset;
57349743 352
d00adf39
DE
353 /* Offset to first die in this cu from the start of the cu.
354 This will be the first byte following the compilation unit header. */
b64f50a1 355 cu_offset first_die_offset;
a738430d 356};
c906108c 357
3da10d80
KS
358/* Type used for delaying computation of method physnames.
359 See comments for compute_delayed_physnames. */
360struct delayed_method_info
361{
362 /* The type to which the method is attached, i.e., its parent class. */
363 struct type *type;
364
365 /* The index of the method in the type's function fieldlists. */
366 int fnfield_index;
367
368 /* The index of the method in the fieldlist. */
369 int index;
370
371 /* The name of the DIE. */
372 const char *name;
373
374 /* The DIE associated with this method. */
375 struct die_info *die;
376};
377
378typedef struct delayed_method_info delayed_method_info;
379DEF_VEC_O (delayed_method_info);
380
e7c27a73
DJ
381/* Internal state when decoding a particular compilation unit. */
382struct dwarf2_cu
383{
384 /* The objfile containing this compilation unit. */
385 struct objfile *objfile;
386
d00adf39 387 /* The header of the compilation unit. */
e7c27a73 388 struct comp_unit_head header;
e142c38c 389
d00adf39
DE
390 /* Base address of this compilation unit. */
391 CORE_ADDR base_address;
392
393 /* Non-zero if base_address has been set. */
394 int base_known;
395
e142c38c
DJ
396 /* The language we are debugging. */
397 enum language language;
398 const struct language_defn *language_defn;
399
b0f35d58
DL
400 const char *producer;
401
e142c38c
DJ
402 /* The generic symbol table building routines have separate lists for
403 file scope symbols and all all other scopes (local scopes). So
404 we need to select the right one to pass to add_symbol_to_list().
405 We do it by keeping a pointer to the correct list in list_in_scope.
406
407 FIXME: The original dwarf code just treated the file scope as the
408 first local scope, and all other local scopes as nested local
409 scopes, and worked fine. Check to see if we really need to
410 distinguish these in buildsym.c. */
411 struct pending **list_in_scope;
412
433df2d4
DE
413 /* The abbrev table for this CU.
414 Normally this points to the abbrev table in the objfile.
415 But if DWO_UNIT is non-NULL this is the abbrev table in the DWO file. */
416 struct abbrev_table *abbrev_table;
72bf9492 417
b64f50a1
JK
418 /* Hash table holding all the loaded partial DIEs
419 with partial_die->offset.SECT_OFF as hash. */
72bf9492
DJ
420 htab_t partial_dies;
421
422 /* Storage for things with the same lifetime as this read-in compilation
423 unit, including partial DIEs. */
424 struct obstack comp_unit_obstack;
425
ae038cb0
DJ
426 /* When multiple dwarf2_cu structures are living in memory, this field
427 chains them all together, so that they can be released efficiently.
428 We will probably also want a generation counter so that most-recently-used
429 compilation units are cached... */
430 struct dwarf2_per_cu_data *read_in_chain;
431
432 /* Backchain to our per_cu entry if the tree has been built. */
433 struct dwarf2_per_cu_data *per_cu;
434
435 /* How many compilation units ago was this CU last referenced? */
436 int last_used;
437
b64f50a1
JK
438 /* A hash table of DIE cu_offset for following references with
439 die_info->offset.sect_off as hash. */
51545339 440 htab_t die_hash;
10b3939b
DJ
441
442 /* Full DIEs if read in. */
443 struct die_info *dies;
444
445 /* A set of pointers to dwarf2_per_cu_data objects for compilation
446 units referenced by this one. Only set during full symbol processing;
447 partial symbol tables do not have dependencies. */
448 htab_t dependencies;
449
cb1df416
DJ
450 /* Header data from the line table, during full symbol processing. */
451 struct line_header *line_header;
452
3da10d80
KS
453 /* A list of methods which need to have physnames computed
454 after all type information has been read. */
455 VEC (delayed_method_info) *method_list;
456
96408a79
SA
457 /* To be copied to symtab->call_site_htab. */
458 htab_t call_site_htab;
459
034e5797
DE
460 /* Non-NULL if this CU came from a DWO file.
461 There is an invariant here that is important to remember:
462 Except for attributes copied from the top level DIE in the "main"
463 (or "stub") file in preparation for reading the DWO file
464 (e.g., DW_AT_GNU_addr_base), we KISS: there is only *one* CU.
465 Either there isn't a DWO file (in which case this is NULL and the point
466 is moot), or there is and either we're not going to read it (in which
467 case this is NULL) or there is and we are reading it (in which case this
468 is non-NULL). */
3019eac3
DE
469 struct dwo_unit *dwo_unit;
470
471 /* The DW_AT_addr_base attribute if present, zero otherwise
472 (zero is a valid value though).
473 Note this value comes from the stub CU/TU's DIE. */
474 ULONGEST addr_base;
475
2e3cf129
DE
476 /* The DW_AT_ranges_base attribute if present, zero otherwise
477 (zero is a valid value though).
478 Note this value comes from the stub CU/TU's DIE.
479 Also note that the value is zero in the non-DWO case so this value can
ab435259
DE
480 be used without needing to know whether DWO files are in use or not.
481 N.B. This does not apply to DW_AT_ranges appearing in
482 DW_TAG_compile_unit dies. This is a bit of a wart, consider if ever
483 DW_AT_ranges appeared in the DW_TAG_compile_unit of DWO DIEs: then
484 DW_AT_ranges_base *would* have to be applied, and we'd have to care
485 whether the DW_AT_ranges attribute came from the skeleton or DWO. */
2e3cf129
DE
486 ULONGEST ranges_base;
487
ae038cb0
DJ
488 /* Mark used when releasing cached dies. */
489 unsigned int mark : 1;
490
8be455d7
JK
491 /* This CU references .debug_loc. See the symtab->locations_valid field.
492 This test is imperfect as there may exist optimized debug code not using
493 any location list and still facing inlining issues if handled as
494 unoptimized code. For a future better test see GCC PR other/32998. */
8be455d7 495 unsigned int has_loclist : 1;
ba919b58 496
1b80a9fa
JK
497 /* These cache the results for producer_is_* fields. CHECKED_PRODUCER is set
498 if all the producer_is_* fields are valid. This information is cached
499 because profiling CU expansion showed excessive time spent in
500 producer_is_gxx_lt_4_6. */
ba919b58
TT
501 unsigned int checked_producer : 1;
502 unsigned int producer_is_gxx_lt_4_6 : 1;
1b80a9fa 503 unsigned int producer_is_gcc_lt_4_3 : 1;
685b1105 504 unsigned int producer_is_icc : 1;
4d4ec4e5
TT
505
506 /* When set, the file that we're processing is known to have
507 debugging info for C++ namespaces. GCC 3.3.x did not produce
508 this information, but later versions do. */
509
510 unsigned int processing_has_namespace_info : 1;
e7c27a73
DJ
511};
512
10b3939b
DJ
513/* Persistent data held for a compilation unit, even when not
514 processing it. We put a pointer to this structure in the
28dee7f5 515 read_symtab_private field of the psymtab. */
10b3939b 516
ae038cb0
DJ
517struct dwarf2_per_cu_data
518{
36586728 519 /* The start offset and length of this compilation unit.
45452591 520 NOTE: Unlike comp_unit_head.length, this length includes
3019eac3
DE
521 initial_length_size.
522 If the DIE refers to a DWO file, this is always of the original die,
523 not the DWO file. */
b64f50a1 524 sect_offset offset;
36586728 525 unsigned int length;
ae038cb0
DJ
526
527 /* Flag indicating this compilation unit will be read in before
528 any of the current compilation units are processed. */
c764a876 529 unsigned int queued : 1;
ae038cb0 530
0d99eb77
DE
531 /* This flag will be set when reading partial DIEs if we need to load
532 absolutely all DIEs for this compilation unit, instead of just the ones
533 we think are interesting. It gets set if we look for a DIE in the
5afb4e99
DJ
534 hash table and don't find it. */
535 unsigned int load_all_dies : 1;
536
3019eac3
DE
537 /* Non-zero if this CU is from .debug_types. */
538 unsigned int is_debug_types : 1;
539
36586728
TT
540 /* Non-zero if this CU is from the .dwz file. */
541 unsigned int is_dwz : 1;
542
3019eac3
DE
543 /* The section this CU/TU lives in.
544 If the DIE refers to a DWO file, this is always the original die,
545 not the DWO file. */
546 struct dwarf2_section_info *info_or_types_section;
348e048f 547
17ea53c3
JK
548 /* Set to non-NULL iff this CU is currently loaded. When it gets freed out
549 of the CU cache it gets reset to NULL again. */
ae038cb0 550 struct dwarf2_cu *cu;
1c379e20 551
9cdd5dbd
DE
552 /* The corresponding objfile.
553 Normally we can get the objfile from dwarf2_per_objfile.
554 However we can enter this file with just a "per_cu" handle. */
9291a0cd
TT
555 struct objfile *objfile;
556
557 /* When using partial symbol tables, the 'psymtab' field is active.
558 Otherwise the 'quick' field is active. */
559 union
560 {
561 /* The partial symbol table associated with this compilation unit,
95554aad 562 or NULL for unread partial units. */
9291a0cd
TT
563 struct partial_symtab *psymtab;
564
565 /* Data needed by the "quick" functions. */
566 struct dwarf2_per_cu_quick_data *quick;
567 } v;
95554aad 568
796a7ff8
DE
569 /* The CUs we import using DW_TAG_imported_unit. This is filled in
570 while reading psymtabs, used to compute the psymtab dependencies,
571 and then cleared. Then it is filled in again while reading full
572 symbols, and only deleted when the objfile is destroyed.
573
574 This is also used to work around a difference between the way gold
575 generates .gdb_index version <=7 and the way gdb does. Arguably this
576 is a gold bug. For symbols coming from TUs, gold records in the index
577 the CU that includes the TU instead of the TU itself. This breaks
578 dw2_lookup_symbol: It assumes that if the index says symbol X lives
579 in CU/TU Y, then one need only expand Y and a subsequent lookup in Y
580 will find X. Alas TUs live in their own symtab, so after expanding CU Y
581 we need to look in TU Z to find X. Fortunately, this is akin to
582 DW_TAG_imported_unit, so we just use the same mechanism: For
583 .gdb_index version <=7 this also records the TUs that the CU referred
584 to. Concurrently with this change gdb was modified to emit version 8
585 indices so we only pay a price for gold generated indices. */
586 VEC (dwarf2_per_cu_ptr) *imported_symtabs;
587
588 /* Type units are grouped by their DW_AT_stmt_list entry so that they
589 can share them. If this is a TU, this points to the containing
590 symtab. */
591 struct type_unit_group *type_unit_group;
ae038cb0
DJ
592};
593
348e048f
DE
594/* Entry in the signatured_types hash table. */
595
596struct signatured_type
597{
42e7ad6c
DE
598 /* The "per_cu" object of this type.
599 N.B.: This is the first member so that it's easy to convert pointers
600 between them. */
601 struct dwarf2_per_cu_data per_cu;
602
3019eac3 603 /* The type's signature. */
348e048f
DE
604 ULONGEST signature;
605
3019eac3
DE
606 /* Offset in the TU of the type's DIE, as read from the TU header.
607 If the definition lives in a DWO file, this value is unusable. */
608 cu_offset type_offset_in_tu;
609
610 /* Offset in the section of the type's DIE.
611 If the definition lives in a DWO file, this is the offset in the
612 .debug_types.dwo section.
613 The value is zero until the actual value is known.
614 Zero is otherwise not a valid section offset. */
615 sect_offset type_offset_in_section;
348e048f
DE
616};
617
094b34ac
DE
618/* A struct that can be used as a hash key for tables based on DW_AT_stmt_list.
619 This includes type_unit_group and quick_file_names. */
620
621struct stmt_list_hash
622{
623 /* The DWO unit this table is from or NULL if there is none. */
624 struct dwo_unit *dwo_unit;
625
626 /* Offset in .debug_line or .debug_line.dwo. */
627 sect_offset line_offset;
628};
629
f4dc4d17
DE
630/* Each element of dwarf2_per_objfile->type_unit_groups is a pointer to
631 an object of this type. */
632
633struct type_unit_group
634{
635 /* dwarf2read.c's main "handle" on the symtab.
636 To simplify things we create an artificial CU that "includes" all the
637 type units using this stmt_list so that the rest of the code still has
638 a "per_cu" handle on the symtab.
639 This PER_CU is recognized by having no section. */
640#define IS_TYPE_UNIT_GROUP(per_cu) ((per_cu)->info_or_types_section == NULL)
094b34ac
DE
641 struct dwarf2_per_cu_data per_cu;
642
643 union
644 {
645 /* The TUs that share this DW_AT_stmt_list entry.
646 This is added to while parsing type units to build partial symtabs,
647 and is deleted afterwards and not used again. */
648 VEC (dwarf2_per_cu_ptr) *tus;
f4dc4d17 649
094b34ac
DE
650 /* When reading the line table in "quick" functions, we need a real TU.
651 Any will do, we know they all share the same DW_AT_stmt_list entry.
652 For simplicity's sake, we pick the first one. */
653 struct dwarf2_per_cu_data *first_tu;
654 } t;
f4dc4d17
DE
655
656 /* The primary symtab.
094b34ac
DE
657 Type units in a group needn't all be defined in the same source file,
658 so we create an essentially anonymous symtab as the primary symtab. */
f4dc4d17
DE
659 struct symtab *primary_symtab;
660
094b34ac
DE
661 /* The data used to construct the hash key. */
662 struct stmt_list_hash hash;
f4dc4d17
DE
663
664 /* The number of symtabs from the line header.
665 The value here must match line_header.num_file_names. */
666 unsigned int num_symtabs;
667
668 /* The symbol tables for this TU (obtained from the files listed in
669 DW_AT_stmt_list).
670 WARNING: The order of entries here must match the order of entries
671 in the line header. After the first TU using this type_unit_group, the
672 line header for the subsequent TUs is recreated from this. This is done
673 because we need to use the same symtabs for each TU using the same
674 DW_AT_stmt_list value. Also note that symtabs may be repeated here,
675 there's no guarantee the line header doesn't have duplicate entries. */
676 struct symtab **symtabs;
677};
678
80626a55 679/* These sections are what may appear in a DWO file. */
3019eac3
DE
680
681struct dwo_sections
682{
683 struct dwarf2_section_info abbrev;
3019eac3
DE
684 struct dwarf2_section_info line;
685 struct dwarf2_section_info loc;
09262596
DE
686 struct dwarf2_section_info macinfo;
687 struct dwarf2_section_info macro;
3019eac3
DE
688 struct dwarf2_section_info str;
689 struct dwarf2_section_info str_offsets;
80626a55
DE
690 /* In the case of a virtual DWO file, these two are unused. */
691 struct dwarf2_section_info info;
3019eac3
DE
692 VEC (dwarf2_section_info_def) *types;
693};
694
695/* Common bits of DWO CUs/TUs. */
696
697struct dwo_unit
698{
699 /* Backlink to the containing struct dwo_file. */
700 struct dwo_file *dwo_file;
701
702 /* The "id" that distinguishes this CU/TU.
703 .debug_info calls this "dwo_id", .debug_types calls this "signature".
704 Since signatures came first, we stick with it for consistency. */
705 ULONGEST signature;
706
707 /* The section this CU/TU lives in, in the DWO file. */
708 struct dwarf2_section_info *info_or_types_section;
709
710 /* Same as dwarf2_per_cu_data:{offset,length} but for the DWO section. */
711 sect_offset offset;
712 unsigned int length;
713
714 /* For types, offset in the type's DIE of the type defined by this TU. */
715 cu_offset type_offset_in_tu;
716};
717
80626a55
DE
718/* Data for one DWO file.
719 This includes virtual DWO files that have been packaged into a
720 DWP file. */
3019eac3
DE
721
722struct dwo_file
723{
80626a55
DE
724 /* The DW_AT_GNU_dwo_name attribute. This is the hash key.
725 For virtual DWO files the name is constructed from the section offsets
726 of abbrev,line,loc,str_offsets so that we combine virtual DWO files
727 from related CU+TUs. */
728 const char *name;
3019eac3 729
80626a55
DE
730 /* The bfd, when the file is open. Otherwise this is NULL.
731 This is unused(NULL) for virtual DWO files where we use dwp_file.dbfd. */
732 bfd *dbfd;
3019eac3
DE
733
734 /* Section info for this file. */
735 struct dwo_sections sections;
736
737 /* Table of CUs in the file.
738 Each element is a struct dwo_unit. */
739 htab_t cus;
740
741 /* Table of TUs in the file.
742 Each element is a struct dwo_unit. */
743 htab_t tus;
744};
745
80626a55
DE
746/* These sections are what may appear in a DWP file. */
747
748struct dwp_sections
749{
750 struct dwarf2_section_info str;
751 struct dwarf2_section_info cu_index;
752 struct dwarf2_section_info tu_index;
753 /* The .debug_info.dwo, .debug_types.dwo, and other sections are referenced
754 by section number. We don't need to record them here. */
755};
756
757/* These sections are what may appear in a virtual DWO file. */
758
759struct virtual_dwo_sections
760{
761 struct dwarf2_section_info abbrev;
762 struct dwarf2_section_info line;
763 struct dwarf2_section_info loc;
764 struct dwarf2_section_info macinfo;
765 struct dwarf2_section_info macro;
766 struct dwarf2_section_info str_offsets;
767 /* Each DWP hash table entry records one CU or one TU.
768 That is recorded here, and copied to dwo_unit.info_or_types_section. */
769 struct dwarf2_section_info info_or_types;
770};
771
772/* Contents of DWP hash tables. */
773
774struct dwp_hash_table
775{
776 uint32_t nr_units, nr_slots;
777 const gdb_byte *hash_table, *unit_table, *section_pool;
778};
779
780/* Data for one DWP file. */
781
782struct dwp_file
783{
784 /* Name of the file. */
785 const char *name;
786
787 /* The bfd, when the file is open. Otherwise this is NULL. */
788 bfd *dbfd;
789
790 /* Section info for this file. */
791 struct dwp_sections sections;
792
793 /* Table of CUs in the file. */
794 const struct dwp_hash_table *cus;
795
796 /* Table of TUs in the file. */
797 const struct dwp_hash_table *tus;
798
799 /* Table of loaded CUs/TUs. Each entry is a struct dwo_unit *. */
800 htab_t loaded_cutus;
801
802 /* Table to map ELF section numbers to their sections. */
803 unsigned int num_sections;
804 asection **elf_sections;
805};
806
36586728
TT
807/* This represents a '.dwz' file. */
808
809struct dwz_file
810{
811 /* A dwz file can only contain a few sections. */
812 struct dwarf2_section_info abbrev;
813 struct dwarf2_section_info info;
814 struct dwarf2_section_info str;
815 struct dwarf2_section_info line;
816 struct dwarf2_section_info macro;
2ec9a5e0 817 struct dwarf2_section_info gdb_index;
36586728
TT
818
819 /* The dwz's BFD. */
820 bfd *dwz_bfd;
821};
822
0963b4bd
MS
823/* Struct used to pass misc. parameters to read_die_and_children, et
824 al. which are used for both .debug_info and .debug_types dies.
825 All parameters here are unchanging for the life of the call. This
dee91e82 826 struct exists to abstract away the constant parameters of die reading. */
93311388
DE
827
828struct die_reader_specs
829{
dee91e82 830 /* die_section->asection->owner. */
93311388
DE
831 bfd* abfd;
832
833 /* The CU of the DIE we are parsing. */
834 struct dwarf2_cu *cu;
835
80626a55 836 /* Non-NULL if reading a DWO file (including one packaged into a DWP). */
3019eac3
DE
837 struct dwo_file *dwo_file;
838
dee91e82 839 /* The section the die comes from.
3019eac3 840 This is either .debug_info or .debug_types, or the .dwo variants. */
dee91e82
DE
841 struct dwarf2_section_info *die_section;
842
843 /* die_section->buffer. */
844 gdb_byte *buffer;
f664829e
DE
845
846 /* The end of the buffer. */
847 const gdb_byte *buffer_end;
93311388
DE
848};
849
fd820528 850/* Type of function passed to init_cutu_and_read_dies, et.al. */
dee91e82
DE
851typedef void (die_reader_func_ftype) (const struct die_reader_specs *reader,
852 gdb_byte *info_ptr,
853 struct die_info *comp_unit_die,
854 int has_children,
855 void *data);
856
debd256d
JB
857/* The line number information for a compilation unit (found in the
858 .debug_line section) begins with a "statement program header",
859 which contains the following information. */
860struct line_header
861{
862 unsigned int total_length;
863 unsigned short version;
864 unsigned int header_length;
865 unsigned char minimum_instruction_length;
2dc7f7b3 866 unsigned char maximum_ops_per_instruction;
debd256d
JB
867 unsigned char default_is_stmt;
868 int line_base;
869 unsigned char line_range;
870 unsigned char opcode_base;
871
872 /* standard_opcode_lengths[i] is the number of operands for the
873 standard opcode whose value is i. This means that
874 standard_opcode_lengths[0] is unused, and the last meaningful
875 element is standard_opcode_lengths[opcode_base - 1]. */
876 unsigned char *standard_opcode_lengths;
877
878 /* The include_directories table. NOTE! These strings are not
879 allocated with xmalloc; instead, they are pointers into
880 debug_line_buffer. If you try to free them, `free' will get
881 indigestion. */
882 unsigned int num_include_dirs, include_dirs_size;
883 char **include_dirs;
884
885 /* The file_names table. NOTE! These strings are not allocated
886 with xmalloc; instead, they are pointers into debug_line_buffer.
887 Don't try to free them directly. */
888 unsigned int num_file_names, file_names_size;
889 struct file_entry
c906108c 890 {
debd256d
JB
891 char *name;
892 unsigned int dir_index;
893 unsigned int mod_time;
894 unsigned int length;
aaa75496 895 int included_p; /* Non-zero if referenced by the Line Number Program. */
cb1df416 896 struct symtab *symtab; /* The associated symbol table, if any. */
debd256d
JB
897 } *file_names;
898
899 /* The start and end of the statement program following this
6502dd73 900 header. These point into dwarf2_per_objfile->line_buffer. */
fe1b8b76 901 gdb_byte *statement_program_start, *statement_program_end;
debd256d 902};
c906108c
SS
903
904/* When we construct a partial symbol table entry we only
0963b4bd 905 need this much information. */
c906108c
SS
906struct partial_die_info
907 {
72bf9492 908 /* Offset of this DIE. */
b64f50a1 909 sect_offset offset;
72bf9492
DJ
910
911 /* DWARF-2 tag for this DIE. */
912 ENUM_BITFIELD(dwarf_tag) tag : 16;
913
72bf9492
DJ
914 /* Assorted flags describing the data found in this DIE. */
915 unsigned int has_children : 1;
916 unsigned int is_external : 1;
917 unsigned int is_declaration : 1;
918 unsigned int has_type : 1;
919 unsigned int has_specification : 1;
920 unsigned int has_pc_info : 1;
481860b3 921 unsigned int may_be_inlined : 1;
72bf9492
DJ
922
923 /* Flag set if the SCOPE field of this structure has been
924 computed. */
925 unsigned int scope_set : 1;
926
fa4028e9
JB
927 /* Flag set if the DIE has a byte_size attribute. */
928 unsigned int has_byte_size : 1;
929
98bfdba5
PA
930 /* Flag set if any of the DIE's children are template arguments. */
931 unsigned int has_template_arguments : 1;
932
abc72ce4
DE
933 /* Flag set if fixup_partial_die has been called on this die. */
934 unsigned int fixup_called : 1;
935
36586728
TT
936 /* Flag set if DW_TAG_imported_unit uses DW_FORM_GNU_ref_alt. */
937 unsigned int is_dwz : 1;
938
939 /* Flag set if spec_offset uses DW_FORM_GNU_ref_alt. */
940 unsigned int spec_is_dwz : 1;
941
72bf9492 942 /* The name of this DIE. Normally the value of DW_AT_name, but
94af9270 943 sometimes a default name for unnamed DIEs. */
15d034d0 944 const char *name;
72bf9492 945
abc72ce4
DE
946 /* The linkage name, if present. */
947 const char *linkage_name;
948
72bf9492
DJ
949 /* The scope to prepend to our children. This is generally
950 allocated on the comp_unit_obstack, so will disappear
951 when this compilation unit leaves the cache. */
15d034d0 952 const char *scope;
72bf9492 953
95554aad
TT
954 /* Some data associated with the partial DIE. The tag determines
955 which field is live. */
956 union
957 {
958 /* The location description associated with this DIE, if any. */
959 struct dwarf_block *locdesc;
960 /* The offset of an import, for DW_TAG_imported_unit. */
961 sect_offset offset;
962 } d;
72bf9492
DJ
963
964 /* If HAS_PC_INFO, the PC range associated with this DIE. */
c906108c
SS
965 CORE_ADDR lowpc;
966 CORE_ADDR highpc;
72bf9492 967
93311388 968 /* Pointer into the info_buffer (or types_buffer) pointing at the target of
72bf9492 969 DW_AT_sibling, if any. */
abc72ce4
DE
970 /* NOTE: This member isn't strictly necessary, read_partial_die could
971 return DW_AT_sibling values to its caller load_partial_dies. */
fe1b8b76 972 gdb_byte *sibling;
72bf9492
DJ
973
974 /* If HAS_SPECIFICATION, the offset of the DIE referred to by
975 DW_AT_specification (or DW_AT_abstract_origin or
976 DW_AT_extension). */
b64f50a1 977 sect_offset spec_offset;
72bf9492
DJ
978
979 /* Pointers to this DIE's parent, first child, and next sibling,
980 if any. */
981 struct partial_die_info *die_parent, *die_child, *die_sibling;
c906108c
SS
982 };
983
0963b4bd 984/* This data structure holds the information of an abbrev. */
c906108c
SS
985struct abbrev_info
986 {
987 unsigned int number; /* number identifying abbrev */
988 enum dwarf_tag tag; /* dwarf tag */
f3dd6933
DJ
989 unsigned short has_children; /* boolean */
990 unsigned short num_attrs; /* number of attributes */
c906108c
SS
991 struct attr_abbrev *attrs; /* an array of attribute descriptions */
992 struct abbrev_info *next; /* next in chain */
993 };
994
995struct attr_abbrev
996 {
9d25dd43
DE
997 ENUM_BITFIELD(dwarf_attribute) name : 16;
998 ENUM_BITFIELD(dwarf_form) form : 16;
c906108c
SS
999 };
1000
433df2d4
DE
1001/* Size of abbrev_table.abbrev_hash_table. */
1002#define ABBREV_HASH_SIZE 121
1003
1004/* Top level data structure to contain an abbreviation table. */
1005
1006struct abbrev_table
1007{
f4dc4d17
DE
1008 /* Where the abbrev table came from.
1009 This is used as a sanity check when the table is used. */
433df2d4
DE
1010 sect_offset offset;
1011
1012 /* Storage for the abbrev table. */
1013 struct obstack abbrev_obstack;
1014
1015 /* Hash table of abbrevs.
1016 This is an array of size ABBREV_HASH_SIZE allocated in abbrev_obstack.
1017 It could be statically allocated, but the previous code didn't so we
1018 don't either. */
1019 struct abbrev_info **abbrevs;
1020};
1021
0963b4bd 1022/* Attributes have a name and a value. */
b60c80d6
DJ
1023struct attribute
1024 {
9d25dd43 1025 ENUM_BITFIELD(dwarf_attribute) name : 16;
8285870a
JK
1026 ENUM_BITFIELD(dwarf_form) form : 15;
1027
1028 /* Has DW_STRING already been updated by dwarf2_canonicalize_name? This
1029 field should be in u.str (existing only for DW_STRING) but it is kept
1030 here for better struct attribute alignment. */
1031 unsigned int string_is_canonical : 1;
1032
b60c80d6
DJ
1033 union
1034 {
15d034d0 1035 const char *str;
b60c80d6 1036 struct dwarf_block *blk;
43bbcdc2
PH
1037 ULONGEST unsnd;
1038 LONGEST snd;
b60c80d6 1039 CORE_ADDR addr;
348e048f 1040 struct signatured_type *signatured_type;
b60c80d6
DJ
1041 }
1042 u;
1043 };
1044
0963b4bd 1045/* This data structure holds a complete die structure. */
c906108c
SS
1046struct die_info
1047 {
76815b17
DE
1048 /* DWARF-2 tag for this DIE. */
1049 ENUM_BITFIELD(dwarf_tag) tag : 16;
1050
1051 /* Number of attributes */
98bfdba5
PA
1052 unsigned char num_attrs;
1053
1054 /* True if we're presently building the full type name for the
1055 type derived from this DIE. */
1056 unsigned char building_fullname : 1;
76815b17
DE
1057
1058 /* Abbrev number */
1059 unsigned int abbrev;
1060
93311388 1061 /* Offset in .debug_info or .debug_types section. */
b64f50a1 1062 sect_offset offset;
78ba4af6
JB
1063
1064 /* The dies in a compilation unit form an n-ary tree. PARENT
1065 points to this die's parent; CHILD points to the first child of
1066 this node; and all the children of a given node are chained
4950bc1c 1067 together via their SIBLING fields. */
639d11d3
DC
1068 struct die_info *child; /* Its first child, if any. */
1069 struct die_info *sibling; /* Its next sibling, if any. */
1070 struct die_info *parent; /* Its parent, if any. */
c906108c 1071
b60c80d6
DJ
1072 /* An array of attributes, with NUM_ATTRS elements. There may be
1073 zero, but it's not common and zero-sized arrays are not
1074 sufficiently portable C. */
1075 struct attribute attrs[1];
c906108c
SS
1076 };
1077
0963b4bd 1078/* Get at parts of an attribute structure. */
c906108c
SS
1079
1080#define DW_STRING(attr) ((attr)->u.str)
8285870a 1081#define DW_STRING_IS_CANONICAL(attr) ((attr)->string_is_canonical)
c906108c
SS
1082#define DW_UNSND(attr) ((attr)->u.unsnd)
1083#define DW_BLOCK(attr) ((attr)->u.blk)
1084#define DW_SND(attr) ((attr)->u.snd)
1085#define DW_ADDR(attr) ((attr)->u.addr)
348e048f 1086#define DW_SIGNATURED_TYPE(attr) ((attr)->u.signatured_type)
c906108c 1087
0963b4bd 1088/* Blocks are a bunch of untyped bytes. */
c906108c
SS
1089struct dwarf_block
1090 {
56eb65bd 1091 size_t size;
1d6edc3c
JK
1092
1093 /* Valid only if SIZE is not zero. */
fe1b8b76 1094 gdb_byte *data;
c906108c
SS
1095 };
1096
c906108c
SS
1097#ifndef ATTR_ALLOC_CHUNK
1098#define ATTR_ALLOC_CHUNK 4
1099#endif
1100
c906108c
SS
1101/* Allocate fields for structs, unions and enums in this size. */
1102#ifndef DW_FIELD_ALLOC_CHUNK
1103#define DW_FIELD_ALLOC_CHUNK 4
1104#endif
1105
c906108c
SS
1106/* FIXME: We might want to set this from BFD via bfd_arch_bits_per_byte,
1107 but this would require a corresponding change in unpack_field_as_long
1108 and friends. */
1109static int bits_per_byte = 8;
1110
1111/* The routines that read and process dies for a C struct or C++ class
1112 pass lists of data member fields and lists of member function fields
1113 in an instance of a field_info structure, as defined below. */
1114struct field_info
c5aa993b 1115 {
0963b4bd 1116 /* List of data member and baseclasses fields. */
c5aa993b
JM
1117 struct nextfield
1118 {
1119 struct nextfield *next;
1120 int accessibility;
1121 int virtuality;
1122 struct field field;
1123 }
7d0ccb61 1124 *fields, *baseclasses;
c906108c 1125
7d0ccb61 1126 /* Number of fields (including baseclasses). */
c5aa993b 1127 int nfields;
c906108c 1128
c5aa993b
JM
1129 /* Number of baseclasses. */
1130 int nbaseclasses;
c906108c 1131
c5aa993b
JM
1132 /* Set if the accesibility of one of the fields is not public. */
1133 int non_public_fields;
c906108c 1134
c5aa993b
JM
1135 /* Member function fields array, entries are allocated in the order they
1136 are encountered in the object file. */
1137 struct nextfnfield
1138 {
1139 struct nextfnfield *next;
1140 struct fn_field fnfield;
1141 }
1142 *fnfields;
c906108c 1143
c5aa993b
JM
1144 /* Member function fieldlist array, contains name of possibly overloaded
1145 member function, number of overloaded member functions and a pointer
1146 to the head of the member function field chain. */
1147 struct fnfieldlist
1148 {
15d034d0 1149 const char *name;
c5aa993b
JM
1150 int length;
1151 struct nextfnfield *head;
1152 }
1153 *fnfieldlists;
c906108c 1154
c5aa993b
JM
1155 /* Number of entries in the fnfieldlists array. */
1156 int nfnfields;
98751a41
JK
1157
1158 /* typedefs defined inside this class. TYPEDEF_FIELD_LIST contains head of
1159 a NULL terminated list of TYPEDEF_FIELD_LIST_COUNT elements. */
1160 struct typedef_field_list
1161 {
1162 struct typedef_field field;
1163 struct typedef_field_list *next;
1164 }
1165 *typedef_field_list;
1166 unsigned typedef_field_list_count;
c5aa993b 1167 };
c906108c 1168
10b3939b
DJ
1169/* One item on the queue of compilation units to read in full symbols
1170 for. */
1171struct dwarf2_queue_item
1172{
1173 struct dwarf2_per_cu_data *per_cu;
95554aad 1174 enum language pretend_language;
10b3939b
DJ
1175 struct dwarf2_queue_item *next;
1176};
1177
1178/* The current queue. */
1179static struct dwarf2_queue_item *dwarf2_queue, *dwarf2_queue_tail;
1180
ae038cb0
DJ
1181/* Loaded secondary compilation units are kept in memory until they
1182 have not been referenced for the processing of this many
1183 compilation units. Set this to zero to disable caching. Cache
1184 sizes of up to at least twenty will improve startup time for
1185 typical inter-CU-reference binaries, at an obvious memory cost. */
1186static int dwarf2_max_cache_age = 5;
920d2a44
AC
1187static void
1188show_dwarf2_max_cache_age (struct ui_file *file, int from_tty,
1189 struct cmd_list_element *c, const char *value)
1190{
3e43a32a
MS
1191 fprintf_filtered (file, _("The upper bound on the age of cached "
1192 "dwarf2 compilation units is %s.\n"),
920d2a44
AC
1193 value);
1194}
1195
ae038cb0 1196
0963b4bd 1197/* Various complaints about symbol reading that don't abort the process. */
c906108c 1198
4d3c2250
KB
1199static void
1200dwarf2_statement_list_fits_in_line_number_section_complaint (void)
2e276125 1201{
4d3c2250 1202 complaint (&symfile_complaints,
e2e0b3e5 1203 _("statement list doesn't fit in .debug_line section"));
4d3c2250
KB
1204}
1205
25e43795
DJ
1206static void
1207dwarf2_debug_line_missing_file_complaint (void)
1208{
1209 complaint (&symfile_complaints,
1210 _(".debug_line section has line data without a file"));
1211}
1212
59205f5a
JB
1213static void
1214dwarf2_debug_line_missing_end_sequence_complaint (void)
1215{
1216 complaint (&symfile_complaints,
3e43a32a
MS
1217 _(".debug_line section has line "
1218 "program sequence without an end"));
59205f5a
JB
1219}
1220
4d3c2250
KB
1221static void
1222dwarf2_complex_location_expr_complaint (void)
2e276125 1223{
e2e0b3e5 1224 complaint (&symfile_complaints, _("location expression too complex"));
4d3c2250
KB
1225}
1226
4d3c2250
KB
1227static void
1228dwarf2_const_value_length_mismatch_complaint (const char *arg1, int arg2,
1229 int arg3)
2e276125 1230{
4d3c2250 1231 complaint (&symfile_complaints,
3e43a32a
MS
1232 _("const value length mismatch for '%s', got %d, expected %d"),
1233 arg1, arg2, arg3);
4d3c2250
KB
1234}
1235
1236static void
f664829e 1237dwarf2_section_buffer_overflow_complaint (struct dwarf2_section_info *section)
2e276125 1238{
4d3c2250 1239 complaint (&symfile_complaints,
f664829e
DE
1240 _("debug info runs off end of %s section"
1241 " [in module %s]"),
1242 section->asection->name,
1243 bfd_get_filename (section->asection->owner));
4d3c2250
KB
1244}
1245
1246static void
1247dwarf2_macro_malformed_definition_complaint (const char *arg1)
8e19ed76 1248{
4d3c2250 1249 complaint (&symfile_complaints,
3e43a32a
MS
1250 _("macro debug info contains a "
1251 "malformed macro definition:\n`%s'"),
4d3c2250
KB
1252 arg1);
1253}
1254
1255static void
1256dwarf2_invalid_attrib_class_complaint (const char *arg1, const char *arg2)
8b2dbe47 1257{
4d3c2250 1258 complaint (&symfile_complaints,
3e43a32a
MS
1259 _("invalid attribute class or form for '%s' in '%s'"),
1260 arg1, arg2);
4d3c2250 1261}
c906108c 1262
c906108c
SS
1263/* local function prototypes */
1264
4efb68b1 1265static void dwarf2_locate_sections (bfd *, asection *, void *);
c906108c 1266
aaa75496
JB
1267static void dwarf2_create_include_psymtab (char *, struct partial_symtab *,
1268 struct objfile *);
1269
918dd910
JK
1270static void dwarf2_find_base_address (struct die_info *die,
1271 struct dwarf2_cu *cu);
1272
c67a9c90 1273static void dwarf2_build_psymtabs_hard (struct objfile *);
c906108c 1274
72bf9492
DJ
1275static void scan_partial_symbols (struct partial_die_info *,
1276 CORE_ADDR *, CORE_ADDR *,
5734ee8b 1277 int, struct dwarf2_cu *);
c906108c 1278
72bf9492
DJ
1279static void add_partial_symbol (struct partial_die_info *,
1280 struct dwarf2_cu *);
63d06c5c 1281
72bf9492
DJ
1282static void add_partial_namespace (struct partial_die_info *pdi,
1283 CORE_ADDR *lowpc, CORE_ADDR *highpc,
5734ee8b 1284 int need_pc, struct dwarf2_cu *cu);
63d06c5c 1285
5d7cb8df
JK
1286static void add_partial_module (struct partial_die_info *pdi, CORE_ADDR *lowpc,
1287 CORE_ADDR *highpc, int need_pc,
1288 struct dwarf2_cu *cu);
1289
72bf9492
DJ
1290static void add_partial_enumeration (struct partial_die_info *enum_pdi,
1291 struct dwarf2_cu *cu);
91c24f0a 1292
bc30ff58
JB
1293static void add_partial_subprogram (struct partial_die_info *pdi,
1294 CORE_ADDR *lowpc, CORE_ADDR *highpc,
5734ee8b 1295 int need_pc, struct dwarf2_cu *cu);
bc30ff58 1296
257e7a09
YQ
1297static void dwarf2_read_symtab (struct partial_symtab *,
1298 struct objfile *);
c906108c 1299
a14ed312 1300static void psymtab_to_symtab_1 (struct partial_symtab *);
c906108c 1301
433df2d4
DE
1302static struct abbrev_info *abbrev_table_lookup_abbrev
1303 (const struct abbrev_table *, unsigned int);
1304
1305static struct abbrev_table *abbrev_table_read_table
1306 (struct dwarf2_section_info *, sect_offset);
1307
1308static void abbrev_table_free (struct abbrev_table *);
1309
f4dc4d17
DE
1310static void abbrev_table_free_cleanup (void *);
1311
dee91e82
DE
1312static void dwarf2_read_abbrevs (struct dwarf2_cu *,
1313 struct dwarf2_section_info *);
c906108c 1314
f3dd6933 1315static void dwarf2_free_abbrev_table (void *);
c906108c 1316
6caca83c
CC
1317static unsigned int peek_abbrev_code (bfd *, gdb_byte *);
1318
dee91e82
DE
1319static struct partial_die_info *load_partial_dies
1320 (const struct die_reader_specs *, gdb_byte *, int);
72bf9492 1321
dee91e82
DE
1322static gdb_byte *read_partial_die (const struct die_reader_specs *,
1323 struct partial_die_info *,
1324 struct abbrev_info *,
1325 unsigned int,
1326 gdb_byte *);
c906108c 1327
36586728 1328static struct partial_die_info *find_partial_die (sect_offset, int,
10b3939b 1329 struct dwarf2_cu *);
72bf9492
DJ
1330
1331static void fixup_partial_die (struct partial_die_info *,
1332 struct dwarf2_cu *);
1333
dee91e82
DE
1334static gdb_byte *read_attribute (const struct die_reader_specs *,
1335 struct attribute *, struct attr_abbrev *,
1336 gdb_byte *);
a8329558 1337
a1855c1d 1338static unsigned int read_1_byte (bfd *, const gdb_byte *);
c906108c 1339
a1855c1d 1340static int read_1_signed_byte (bfd *, const gdb_byte *);
c906108c 1341
a1855c1d 1342static unsigned int read_2_bytes (bfd *, const gdb_byte *);
c906108c 1343
a1855c1d 1344static unsigned int read_4_bytes (bfd *, const gdb_byte *);
c906108c 1345
a1855c1d 1346static ULONGEST read_8_bytes (bfd *, const gdb_byte *);
c906108c 1347
fe1b8b76 1348static CORE_ADDR read_address (bfd *, gdb_byte *ptr, struct dwarf2_cu *,
891d2f0b 1349 unsigned int *);
c906108c 1350
c764a876
DE
1351static LONGEST read_initial_length (bfd *, gdb_byte *, unsigned int *);
1352
1353static LONGEST read_checked_initial_length_and_offset
1354 (bfd *, gdb_byte *, const struct comp_unit_head *,
1355 unsigned int *, unsigned int *);
613e1657 1356
fe1b8b76 1357static LONGEST read_offset (bfd *, gdb_byte *, const struct comp_unit_head *,
c764a876
DE
1358 unsigned int *);
1359
1360static LONGEST read_offset_1 (bfd *, gdb_byte *, unsigned int);
613e1657 1361
f4dc4d17
DE
1362static sect_offset read_abbrev_offset (struct dwarf2_section_info *,
1363 sect_offset);
1364
fe1b8b76 1365static gdb_byte *read_n_bytes (bfd *, gdb_byte *, unsigned int);
c906108c 1366
9b1c24c8 1367static char *read_direct_string (bfd *, gdb_byte *, unsigned int *);
c906108c 1368
fe1b8b76
JB
1369static char *read_indirect_string (bfd *, gdb_byte *,
1370 const struct comp_unit_head *,
1371 unsigned int *);
4bdf3d34 1372
36586728
TT
1373static char *read_indirect_string_from_dwz (struct dwz_file *, LONGEST);
1374
12df843f 1375static ULONGEST read_unsigned_leb128 (bfd *, gdb_byte *, unsigned int *);
c906108c 1376
12df843f 1377static LONGEST read_signed_leb128 (bfd *, gdb_byte *, unsigned int *);
c906108c 1378
3019eac3
DE
1379static CORE_ADDR read_addr_index_from_leb128 (struct dwarf2_cu *, gdb_byte *,
1380 unsigned int *);
1381
1382static char *read_str_index (const struct die_reader_specs *reader,
1383 struct dwarf2_cu *cu, ULONGEST str_index);
1384
e142c38c 1385static void set_cu_language (unsigned int, struct dwarf2_cu *);
c906108c 1386
e142c38c
DJ
1387static struct attribute *dwarf2_attr (struct die_info *, unsigned int,
1388 struct dwarf2_cu *);
c906108c 1389
348e048f 1390static struct attribute *dwarf2_attr_no_follow (struct die_info *,
45e58e77 1391 unsigned int);
348e048f 1392
05cf31d1
JB
1393static int dwarf2_flag_true_p (struct die_info *die, unsigned name,
1394 struct dwarf2_cu *cu);
1395
e142c38c 1396static int die_is_declaration (struct die_info *, struct dwarf2_cu *cu);
3ca72b44 1397
e142c38c 1398static struct die_info *die_specification (struct die_info *die,
f2f0e013 1399 struct dwarf2_cu **);
63d06c5c 1400
debd256d
JB
1401static void free_line_header (struct line_header *lh);
1402
aaa75496
JB
1403static void add_file_name (struct line_header *, char *, unsigned int,
1404 unsigned int, unsigned int);
1405
3019eac3
DE
1406static struct line_header *dwarf_decode_line_header (unsigned int offset,
1407 struct dwarf2_cu *cu);
debd256d 1408
f3f5162e
DE
1409static void dwarf_decode_lines (struct line_header *, const char *,
1410 struct dwarf2_cu *, struct partial_symtab *,
1411 int);
c906108c 1412
72b9f47f 1413static void dwarf2_start_subfile (char *, const char *, const char *);
c906108c 1414
f4dc4d17 1415static void dwarf2_start_symtab (struct dwarf2_cu *,
15d034d0 1416 const char *, const char *, CORE_ADDR);
f4dc4d17 1417
a14ed312 1418static struct symbol *new_symbol (struct die_info *, struct type *,
e7c27a73 1419 struct dwarf2_cu *);
c906108c 1420
34eaf542
TT
1421static struct symbol *new_symbol_full (struct die_info *, struct type *,
1422 struct dwarf2_cu *, struct symbol *);
1423
a14ed312 1424static void dwarf2_const_value (struct attribute *, struct symbol *,
e7c27a73 1425 struct dwarf2_cu *);
c906108c 1426
98bfdba5
PA
1427static void dwarf2_const_value_attr (struct attribute *attr,
1428 struct type *type,
1429 const char *name,
1430 struct obstack *obstack,
12df843f 1431 struct dwarf2_cu *cu, LONGEST *value,
98bfdba5
PA
1432 gdb_byte **bytes,
1433 struct dwarf2_locexpr_baton **baton);
2df3850c 1434
e7c27a73 1435static struct type *die_type (struct die_info *, struct dwarf2_cu *);
c906108c 1436
b4ba55a1
JB
1437static int need_gnat_info (struct dwarf2_cu *);
1438
3e43a32a
MS
1439static struct type *die_descriptive_type (struct die_info *,
1440 struct dwarf2_cu *);
b4ba55a1
JB
1441
1442static void set_descriptive_type (struct type *, struct die_info *,
1443 struct dwarf2_cu *);
1444
e7c27a73
DJ
1445static struct type *die_containing_type (struct die_info *,
1446 struct dwarf2_cu *);
c906108c 1447
673bfd45
DE
1448static struct type *lookup_die_type (struct die_info *, struct attribute *,
1449 struct dwarf2_cu *);
c906108c 1450
f792889a 1451static struct type *read_type_die (struct die_info *, struct dwarf2_cu *);
c906108c 1452
673bfd45
DE
1453static struct type *read_type_die_1 (struct die_info *, struct dwarf2_cu *);
1454
0d5cff50 1455static const char *determine_prefix (struct die_info *die, struct dwarf2_cu *);
63d06c5c 1456
6e70227d 1457static char *typename_concat (struct obstack *obs, const char *prefix,
f55ee35c
JK
1458 const char *suffix, int physname,
1459 struct dwarf2_cu *cu);
63d06c5c 1460
e7c27a73 1461static void read_file_scope (struct die_info *, struct dwarf2_cu *);
c906108c 1462
348e048f
DE
1463static void read_type_unit_scope (struct die_info *, struct dwarf2_cu *);
1464
e7c27a73 1465static void read_func_scope (struct die_info *, struct dwarf2_cu *);
c906108c 1466
e7c27a73 1467static void read_lexical_block_scope (struct die_info *, struct dwarf2_cu *);
c906108c 1468
96408a79
SA
1469static void read_call_site_scope (struct die_info *die, struct dwarf2_cu *cu);
1470
ff013f42
JK
1471static int dwarf2_ranges_read (unsigned, CORE_ADDR *, CORE_ADDR *,
1472 struct dwarf2_cu *, struct partial_symtab *);
1473
a14ed312 1474static int dwarf2_get_pc_bounds (struct die_info *,
d85a05f0
DJ
1475 CORE_ADDR *, CORE_ADDR *, struct dwarf2_cu *,
1476 struct partial_symtab *);
c906108c 1477
fae299cd
DC
1478static void get_scope_pc_bounds (struct die_info *,
1479 CORE_ADDR *, CORE_ADDR *,
1480 struct dwarf2_cu *);
1481
801e3a5b
JB
1482static void dwarf2_record_block_ranges (struct die_info *, struct block *,
1483 CORE_ADDR, struct dwarf2_cu *);
1484
a14ed312 1485static void dwarf2_add_field (struct field_info *, struct die_info *,
e7c27a73 1486 struct dwarf2_cu *);
c906108c 1487
a14ed312 1488static void dwarf2_attach_fields_to_type (struct field_info *,
e7c27a73 1489 struct type *, struct dwarf2_cu *);
c906108c 1490
a14ed312 1491static void dwarf2_add_member_fn (struct field_info *,
e26fb1d7 1492 struct die_info *, struct type *,
e7c27a73 1493 struct dwarf2_cu *);
c906108c 1494
a14ed312 1495static void dwarf2_attach_fn_fields_to_type (struct field_info *,
3e43a32a
MS
1496 struct type *,
1497 struct dwarf2_cu *);
c906108c 1498
134d01f1 1499static void process_structure_scope (struct die_info *, struct dwarf2_cu *);
c906108c 1500
e7c27a73 1501static void read_common_block (struct die_info *, struct dwarf2_cu *);
c906108c 1502
e7c27a73 1503static void read_namespace (struct die_info *die, struct dwarf2_cu *);
d9fa45fe 1504
5d7cb8df
JK
1505static void read_module (struct die_info *die, struct dwarf2_cu *cu);
1506
27aa8d6a
SW
1507static void read_import_statement (struct die_info *die, struct dwarf2_cu *);
1508
f55ee35c
JK
1509static struct type *read_module_type (struct die_info *die,
1510 struct dwarf2_cu *cu);
1511
38d518c9 1512static const char *namespace_name (struct die_info *die,
e142c38c 1513 int *is_anonymous, struct dwarf2_cu *);
38d518c9 1514
134d01f1 1515static void process_enumeration_scope (struct die_info *, struct dwarf2_cu *);
c906108c 1516
e7c27a73 1517static CORE_ADDR decode_locdesc (struct dwarf_block *, struct dwarf2_cu *);
c906108c 1518
6e70227d 1519static enum dwarf_array_dim_ordering read_array_order (struct die_info *,
7ca2d3a3
DL
1520 struct dwarf2_cu *);
1521
dee91e82 1522static struct die_info *read_die_and_children (const struct die_reader_specs *,
93311388 1523 gdb_byte *info_ptr,
fe1b8b76 1524 gdb_byte **new_info_ptr,
639d11d3
DC
1525 struct die_info *parent);
1526
dee91e82 1527static struct die_info *read_die_and_siblings (const struct die_reader_specs *,
93311388 1528 gdb_byte *info_ptr,
fe1b8b76 1529 gdb_byte **new_info_ptr,
639d11d3
DC
1530 struct die_info *parent);
1531
3019eac3
DE
1532static gdb_byte *read_full_die_1 (const struct die_reader_specs *,
1533 struct die_info **, gdb_byte *, int *, int);
1534
dee91e82
DE
1535static gdb_byte *read_full_die (const struct die_reader_specs *,
1536 struct die_info **, gdb_byte *, int *);
93311388 1537
e7c27a73 1538static void process_die (struct die_info *, struct dwarf2_cu *);
c906108c 1539
15d034d0
TT
1540static const char *dwarf2_canonicalize_name (const char *, struct dwarf2_cu *,
1541 struct obstack *);
71c25dea 1542
15d034d0 1543static const char *dwarf2_name (struct die_info *die, struct dwarf2_cu *);
9219021c 1544
15d034d0 1545static const char *dwarf2_full_name (const char *name,
98bfdba5
PA
1546 struct die_info *die,
1547 struct dwarf2_cu *cu);
1548
e142c38c 1549static struct die_info *dwarf2_extension (struct die_info *die,
f2f0e013 1550 struct dwarf2_cu **);
9219021c 1551
f39c6ffd 1552static const char *dwarf_tag_name (unsigned int);
c906108c 1553
f39c6ffd 1554static const char *dwarf_attr_name (unsigned int);
c906108c 1555
f39c6ffd 1556static const char *dwarf_form_name (unsigned int);
c906108c 1557
a14ed312 1558static char *dwarf_bool_name (unsigned int);
c906108c 1559
f39c6ffd 1560static const char *dwarf_type_encoding_name (unsigned int);
c906108c 1561
f9aca02d 1562static struct die_info *sibling_die (struct die_info *);
c906108c 1563
d97bc12b
DE
1564static void dump_die_shallow (struct ui_file *, int indent, struct die_info *);
1565
1566static void dump_die_for_error (struct die_info *);
1567
1568static void dump_die_1 (struct ui_file *, int level, int max_level,
1569 struct die_info *);
c906108c 1570
d97bc12b 1571/*static*/ void dump_die (struct die_info *, int max_level);
c906108c 1572
51545339 1573static void store_in_ref_table (struct die_info *,
10b3939b 1574 struct dwarf2_cu *);
c906108c 1575
93311388
DE
1576static int is_ref_attr (struct attribute *);
1577
b64f50a1 1578static sect_offset dwarf2_get_ref_die_offset (struct attribute *);
c906108c 1579
43bbcdc2 1580static LONGEST dwarf2_get_attr_constant_value (struct attribute *, int);
a02abb62 1581
348e048f
DE
1582static struct die_info *follow_die_ref_or_sig (struct die_info *,
1583 struct attribute *,
1584 struct dwarf2_cu **);
1585
10b3939b
DJ
1586static struct die_info *follow_die_ref (struct die_info *,
1587 struct attribute *,
f2f0e013 1588 struct dwarf2_cu **);
c906108c 1589
348e048f
DE
1590static struct die_info *follow_die_sig (struct die_info *,
1591 struct attribute *,
1592 struct dwarf2_cu **);
1593
6c83ed52
TT
1594static struct signatured_type *lookup_signatured_type_at_offset
1595 (struct objfile *objfile,
b64f50a1 1596 struct dwarf2_section_info *section, sect_offset offset);
6c83ed52 1597
e5fe5e75 1598static void load_full_type_unit (struct dwarf2_per_cu_data *per_cu);
348e048f 1599
52dc124a 1600static void read_signatured_type (struct signatured_type *);
348e048f 1601
f4dc4d17 1602static struct type_unit_group *get_type_unit_group
094b34ac 1603 (struct dwarf2_cu *, struct attribute *);
f4dc4d17
DE
1604
1605static void build_type_unit_groups (die_reader_func_ftype *, void *);
1606
c906108c
SS
1607/* memory allocation interface */
1608
7b5a2f43 1609static struct dwarf_block *dwarf_alloc_block (struct dwarf2_cu *);
c906108c 1610
b60c80d6 1611static struct die_info *dwarf_alloc_die (struct dwarf2_cu *, int);
c906108c 1612
09262596 1613static void dwarf_decode_macros (struct dwarf2_cu *, unsigned int,
15d034d0 1614 const char *, int);
2e276125 1615
8e19ed76
PS
1616static int attr_form_is_block (struct attribute *);
1617
3690dd37
JB
1618static int attr_form_is_section_offset (struct attribute *);
1619
1620static int attr_form_is_constant (struct attribute *);
1621
8cf6f0b1
TT
1622static void fill_in_loclist_baton (struct dwarf2_cu *cu,
1623 struct dwarf2_loclist_baton *baton,
1624 struct attribute *attr);
1625
93e7bd98
DJ
1626static void dwarf2_symbol_mark_computed (struct attribute *attr,
1627 struct symbol *sym,
1628 struct dwarf2_cu *cu);
4c2df51b 1629
dee91e82
DE
1630static gdb_byte *skip_one_die (const struct die_reader_specs *reader,
1631 gdb_byte *info_ptr,
1632 struct abbrev_info *abbrev);
4bb7a0a7 1633
72bf9492
DJ
1634static void free_stack_comp_unit (void *);
1635
72bf9492
DJ
1636static hashval_t partial_die_hash (const void *item);
1637
1638static int partial_die_eq (const void *item_lhs, const void *item_rhs);
1639
ae038cb0 1640static struct dwarf2_per_cu_data *dwarf2_find_containing_comp_unit
36586728 1641 (sect_offset offset, unsigned int offset_in_dwz, struct objfile *objfile);
ae038cb0 1642
9816fde3 1643static void init_one_comp_unit (struct dwarf2_cu *cu,
23745b47 1644 struct dwarf2_per_cu_data *per_cu);
9816fde3
JK
1645
1646static void prepare_one_comp_unit (struct dwarf2_cu *cu,
95554aad
TT
1647 struct die_info *comp_unit_die,
1648 enum language pretend_language);
93311388 1649
68dc6402 1650static void free_heap_comp_unit (void *);
ae038cb0
DJ
1651
1652static void free_cached_comp_units (void *);
1653
1654static void age_cached_comp_units (void);
1655
dee91e82 1656static void free_one_cached_comp_unit (struct dwarf2_per_cu_data *);
ae038cb0 1657
f792889a
DJ
1658static struct type *set_die_type (struct die_info *, struct type *,
1659 struct dwarf2_cu *);
1c379e20 1660
ae038cb0
DJ
1661static void create_all_comp_units (struct objfile *);
1662
0e50663e 1663static int create_all_type_units (struct objfile *);
1fd400ff 1664
95554aad
TT
1665static void load_full_comp_unit (struct dwarf2_per_cu_data *,
1666 enum language);
10b3939b 1667
95554aad
TT
1668static void process_full_comp_unit (struct dwarf2_per_cu_data *,
1669 enum language);
10b3939b 1670
f4dc4d17
DE
1671static void process_full_type_unit (struct dwarf2_per_cu_data *,
1672 enum language);
1673
10b3939b
DJ
1674static void dwarf2_add_dependence (struct dwarf2_cu *,
1675 struct dwarf2_per_cu_data *);
1676
ae038cb0
DJ
1677static void dwarf2_mark (struct dwarf2_cu *);
1678
1679static void dwarf2_clear_marks (struct dwarf2_per_cu_data *);
1680
b64f50a1 1681static struct type *get_die_type_at_offset (sect_offset,
673bfd45
DE
1682 struct dwarf2_per_cu_data *per_cu);
1683
f792889a 1684static struct type *get_die_type (struct die_info *die, struct dwarf2_cu *cu);
72019c9c 1685
9291a0cd
TT
1686static void dwarf2_release_queue (void *dummy);
1687
95554aad
TT
1688static void queue_comp_unit (struct dwarf2_per_cu_data *per_cu,
1689 enum language pretend_language);
1690
1691static int maybe_queue_comp_unit (struct dwarf2_cu *this_cu,
1692 struct dwarf2_per_cu_data *per_cu,
1693 enum language pretend_language);
9291a0cd 1694
a0f42c21 1695static void process_queue (void);
9291a0cd
TT
1696
1697static void find_file_and_directory (struct die_info *die,
1698 struct dwarf2_cu *cu,
15d034d0 1699 const char **name, const char **comp_dir);
9291a0cd
TT
1700
1701static char *file_full_name (int file, struct line_header *lh,
1702 const char *comp_dir);
1703
36586728
TT
1704static gdb_byte *read_and_check_comp_unit_head
1705 (struct comp_unit_head *header,
1706 struct dwarf2_section_info *section,
1707 struct dwarf2_section_info *abbrev_section, gdb_byte *info_ptr,
1708 int is_debug_types_section);
1709
fd820528 1710static void init_cutu_and_read_dies
f4dc4d17
DE
1711 (struct dwarf2_per_cu_data *this_cu, struct abbrev_table *abbrev_table,
1712 int use_existing_cu, int keep,
3019eac3
DE
1713 die_reader_func_ftype *die_reader_func, void *data);
1714
dee91e82
DE
1715static void init_cutu_and_read_dies_simple
1716 (struct dwarf2_per_cu_data *this_cu,
1717 die_reader_func_ftype *die_reader_func, void *data);
9291a0cd 1718
673bfd45 1719static htab_t allocate_signatured_type_table (struct objfile *objfile);
1fd400ff 1720
3019eac3
DE
1721static htab_t allocate_dwo_unit_table (struct objfile *objfile);
1722
1723static struct dwo_unit *lookup_dwo_comp_unit
a1855c1d 1724 (struct dwarf2_per_cu_data *, const char *, const char *, ULONGEST);
3019eac3
DE
1725
1726static struct dwo_unit *lookup_dwo_type_unit
a1855c1d 1727 (struct signatured_type *, const char *, const char *);
3019eac3
DE
1728
1729static void free_dwo_file_cleanup (void *);
1730
95554aad
TT
1731static void process_cu_includes (void);
1732
1b80a9fa
JK
1733static void check_producer (struct dwarf2_cu *cu);
1734
9291a0cd
TT
1735#if WORDS_BIGENDIAN
1736
1737/* Convert VALUE between big- and little-endian. */
1738static offset_type
1739byte_swap (offset_type value)
1740{
1741 offset_type result;
1742
1743 result = (value & 0xff) << 24;
1744 result |= (value & 0xff00) << 8;
1745 result |= (value & 0xff0000) >> 8;
1746 result |= (value & 0xff000000) >> 24;
1747 return result;
1748}
1749
1750#define MAYBE_SWAP(V) byte_swap (V)
1751
1752#else
1753#define MAYBE_SWAP(V) (V)
1754#endif /* WORDS_BIGENDIAN */
1755
1756/* The suffix for an index file. */
1757#define INDEX_SUFFIX ".gdb-index"
1758
15d034d0 1759static const char *dwarf2_physname (const char *name, struct die_info *die,
3da10d80
KS
1760 struct dwarf2_cu *cu);
1761
c906108c 1762/* Try to locate the sections we need for DWARF 2 debugging
251d32d9
TG
1763 information and return true if we have enough to do something.
1764 NAMES points to the dwarf2 section names, or is NULL if the standard
1765 ELF names are used. */
c906108c
SS
1766
1767int
251d32d9
TG
1768dwarf2_has_info (struct objfile *objfile,
1769 const struct dwarf2_debug_sections *names)
c906108c 1770{
be391dca
TT
1771 dwarf2_per_objfile = objfile_data (objfile, dwarf2_objfile_data_key);
1772 if (!dwarf2_per_objfile)
1773 {
1774 /* Initialize per-objfile state. */
1775 struct dwarf2_per_objfile *data
1776 = obstack_alloc (&objfile->objfile_obstack, sizeof (*data));
9a619af0 1777
be391dca
TT
1778 memset (data, 0, sizeof (*data));
1779 set_objfile_data (objfile, dwarf2_objfile_data_key, data);
1780 dwarf2_per_objfile = data;
6502dd73 1781
251d32d9
TG
1782 bfd_map_over_sections (objfile->obfd, dwarf2_locate_sections,
1783 (void *) names);
be391dca
TT
1784 dwarf2_per_objfile->objfile = objfile;
1785 }
1786 return (dwarf2_per_objfile->info.asection != NULL
1787 && dwarf2_per_objfile->abbrev.asection != NULL);
c906108c
SS
1788}
1789
251d32d9
TG
1790/* When loading sections, we look either for uncompressed section or for
1791 compressed section names. */
233a11ab
CS
1792
1793static int
251d32d9
TG
1794section_is_p (const char *section_name,
1795 const struct dwarf2_section_names *names)
233a11ab 1796{
251d32d9
TG
1797 if (names->normal != NULL
1798 && strcmp (section_name, names->normal) == 0)
1799 return 1;
1800 if (names->compressed != NULL
1801 && strcmp (section_name, names->compressed) == 0)
1802 return 1;
1803 return 0;
233a11ab
CS
1804}
1805
c906108c
SS
1806/* This function is mapped across the sections and remembers the
1807 offset and size of each of the debugging sections we are interested
1808 in. */
1809
1810static void
251d32d9 1811dwarf2_locate_sections (bfd *abfd, asection *sectp, void *vnames)
c906108c 1812{
251d32d9 1813 const struct dwarf2_debug_sections *names;
dc7650b8 1814 flagword aflag = bfd_get_section_flags (abfd, sectp);
251d32d9
TG
1815
1816 if (vnames == NULL)
1817 names = &dwarf2_elf_names;
1818 else
1819 names = (const struct dwarf2_debug_sections *) vnames;
1820
dc7650b8
JK
1821 if ((aflag & SEC_HAS_CONTENTS) == 0)
1822 {
1823 }
1824 else if (section_is_p (sectp->name, &names->info))
c906108c 1825 {
dce234bc
PP
1826 dwarf2_per_objfile->info.asection = sectp;
1827 dwarf2_per_objfile->info.size = bfd_get_section_size (sectp);
c906108c 1828 }
251d32d9 1829 else if (section_is_p (sectp->name, &names->abbrev))
c906108c 1830 {
dce234bc
PP
1831 dwarf2_per_objfile->abbrev.asection = sectp;
1832 dwarf2_per_objfile->abbrev.size = bfd_get_section_size (sectp);
c906108c 1833 }
251d32d9 1834 else if (section_is_p (sectp->name, &names->line))
c906108c 1835 {
dce234bc
PP
1836 dwarf2_per_objfile->line.asection = sectp;
1837 dwarf2_per_objfile->line.size = bfd_get_section_size (sectp);
c906108c 1838 }
251d32d9 1839 else if (section_is_p (sectp->name, &names->loc))
c906108c 1840 {
dce234bc
PP
1841 dwarf2_per_objfile->loc.asection = sectp;
1842 dwarf2_per_objfile->loc.size = bfd_get_section_size (sectp);
c906108c 1843 }
251d32d9 1844 else if (section_is_p (sectp->name, &names->macinfo))
c906108c 1845 {
dce234bc
PP
1846 dwarf2_per_objfile->macinfo.asection = sectp;
1847 dwarf2_per_objfile->macinfo.size = bfd_get_section_size (sectp);
c906108c 1848 }
cf2c3c16
TT
1849 else if (section_is_p (sectp->name, &names->macro))
1850 {
1851 dwarf2_per_objfile->macro.asection = sectp;
1852 dwarf2_per_objfile->macro.size = bfd_get_section_size (sectp);
1853 }
251d32d9 1854 else if (section_is_p (sectp->name, &names->str))
c906108c 1855 {
dce234bc
PP
1856 dwarf2_per_objfile->str.asection = sectp;
1857 dwarf2_per_objfile->str.size = bfd_get_section_size (sectp);
c906108c 1858 }
3019eac3
DE
1859 else if (section_is_p (sectp->name, &names->addr))
1860 {
1861 dwarf2_per_objfile->addr.asection = sectp;
1862 dwarf2_per_objfile->addr.size = bfd_get_section_size (sectp);
1863 }
251d32d9 1864 else if (section_is_p (sectp->name, &names->frame))
b6af0555 1865 {
dce234bc
PP
1866 dwarf2_per_objfile->frame.asection = sectp;
1867 dwarf2_per_objfile->frame.size = bfd_get_section_size (sectp);
b6af0555 1868 }
251d32d9 1869 else if (section_is_p (sectp->name, &names->eh_frame))
b6af0555 1870 {
dc7650b8
JK
1871 dwarf2_per_objfile->eh_frame.asection = sectp;
1872 dwarf2_per_objfile->eh_frame.size = bfd_get_section_size (sectp);
b6af0555 1873 }
251d32d9 1874 else if (section_is_p (sectp->name, &names->ranges))
af34e669 1875 {
dce234bc
PP
1876 dwarf2_per_objfile->ranges.asection = sectp;
1877 dwarf2_per_objfile->ranges.size = bfd_get_section_size (sectp);
af34e669 1878 }
251d32d9 1879 else if (section_is_p (sectp->name, &names->types))
348e048f 1880 {
8b70b953
TT
1881 struct dwarf2_section_info type_section;
1882
1883 memset (&type_section, 0, sizeof (type_section));
1884 type_section.asection = sectp;
1885 type_section.size = bfd_get_section_size (sectp);
1886
1887 VEC_safe_push (dwarf2_section_info_def, dwarf2_per_objfile->types,
1888 &type_section);
348e048f 1889 }
251d32d9 1890 else if (section_is_p (sectp->name, &names->gdb_index))
9291a0cd
TT
1891 {
1892 dwarf2_per_objfile->gdb_index.asection = sectp;
1893 dwarf2_per_objfile->gdb_index.size = bfd_get_section_size (sectp);
1894 }
dce234bc 1895
72dca2f5
FR
1896 if ((bfd_get_section_flags (abfd, sectp) & SEC_LOAD)
1897 && bfd_section_vma (abfd, sectp) == 0)
1898 dwarf2_per_objfile->has_section_at_zero = 1;
c906108c
SS
1899}
1900
fceca515
DE
1901/* A helper function that decides whether a section is empty,
1902 or not present. */
9e0ac564
TT
1903
1904static int
1905dwarf2_section_empty_p (struct dwarf2_section_info *info)
1906{
1907 return info->asection == NULL || info->size == 0;
1908}
1909
3019eac3
DE
1910/* Read the contents of the section INFO.
1911 OBJFILE is the main object file, but not necessarily the file where
1912 the section comes from. E.g., for DWO files INFO->asection->owner
1913 is the bfd of the DWO file.
dce234bc 1914 If the section is compressed, uncompress it before returning. */
c906108c 1915
dce234bc
PP
1916static void
1917dwarf2_read_section (struct objfile *objfile, struct dwarf2_section_info *info)
c906108c 1918{
dce234bc 1919 asection *sectp = info->asection;
3019eac3 1920 bfd *abfd;
dce234bc
PP
1921 gdb_byte *buf, *retbuf;
1922 unsigned char header[4];
c906108c 1923
be391dca
TT
1924 if (info->readin)
1925 return;
dce234bc 1926 info->buffer = NULL;
be391dca 1927 info->readin = 1;
188dd5d6 1928
9e0ac564 1929 if (dwarf2_section_empty_p (info))
dce234bc 1930 return;
c906108c 1931
3019eac3
DE
1932 abfd = sectp->owner;
1933
4bf44c1c
TT
1934 /* If the section has relocations, we must read it ourselves.
1935 Otherwise we attach it to the BFD. */
1936 if ((sectp->flags & SEC_RELOC) == 0)
dce234bc 1937 {
4bf44c1c 1938 const gdb_byte *bytes = gdb_bfd_map_section (sectp, &info->size);
dce234bc 1939
4bf44c1c
TT
1940 /* We have to cast away const here for historical reasons.
1941 Fixing dwarf2read to be const-correct would be quite nice. */
1942 info->buffer = (gdb_byte *) bytes;
1943 return;
dce234bc 1944 }
dce234bc 1945
4bf44c1c
TT
1946 buf = obstack_alloc (&objfile->objfile_obstack, info->size);
1947 info->buffer = buf;
dce234bc
PP
1948
1949 /* When debugging .o files, we may need to apply relocations; see
1950 http://sourceware.org/ml/gdb-patches/2002-04/msg00136.html .
1951 We never compress sections in .o files, so we only need to
1952 try this when the section is not compressed. */
ac8035ab 1953 retbuf = symfile_relocate_debug_section (objfile, sectp, buf);
dce234bc
PP
1954 if (retbuf != NULL)
1955 {
1956 info->buffer = retbuf;
1957 return;
1958 }
1959
1960 if (bfd_seek (abfd, sectp->filepos, SEEK_SET) != 0
1961 || bfd_bread (buf, info->size, abfd) != info->size)
1962 error (_("Dwarf Error: Can't read DWARF data from '%s'"),
1963 bfd_get_filename (abfd));
1964}
1965
9e0ac564
TT
1966/* A helper function that returns the size of a section in a safe way.
1967 If you are positive that the section has been read before using the
1968 size, then it is safe to refer to the dwarf2_section_info object's
1969 "size" field directly. In other cases, you must call this
1970 function, because for compressed sections the size field is not set
1971 correctly until the section has been read. */
1972
1973static bfd_size_type
1974dwarf2_section_size (struct objfile *objfile,
1975 struct dwarf2_section_info *info)
1976{
1977 if (!info->readin)
1978 dwarf2_read_section (objfile, info);
1979 return info->size;
1980}
1981
dce234bc 1982/* Fill in SECTP, BUFP and SIZEP with section info, given OBJFILE and
0963b4bd 1983 SECTION_NAME. */
af34e669 1984
dce234bc 1985void
3017a003
TG
1986dwarf2_get_section_info (struct objfile *objfile,
1987 enum dwarf2_section_enum sect,
dce234bc
PP
1988 asection **sectp, gdb_byte **bufp,
1989 bfd_size_type *sizep)
1990{
1991 struct dwarf2_per_objfile *data
1992 = objfile_data (objfile, dwarf2_objfile_data_key);
1993 struct dwarf2_section_info *info;
a3b2a86b
TT
1994
1995 /* We may see an objfile without any DWARF, in which case we just
1996 return nothing. */
1997 if (data == NULL)
1998 {
1999 *sectp = NULL;
2000 *bufp = NULL;
2001 *sizep = 0;
2002 return;
2003 }
3017a003
TG
2004 switch (sect)
2005 {
2006 case DWARF2_DEBUG_FRAME:
2007 info = &data->frame;
2008 break;
2009 case DWARF2_EH_FRAME:
2010 info = &data->eh_frame;
2011 break;
2012 default:
2013 gdb_assert_not_reached ("unexpected section");
2014 }
dce234bc 2015
9e0ac564 2016 dwarf2_read_section (objfile, info);
dce234bc
PP
2017
2018 *sectp = info->asection;
2019 *bufp = info->buffer;
2020 *sizep = info->size;
2021}
2022
36586728
TT
2023/* A helper function to find the sections for a .dwz file. */
2024
2025static void
2026locate_dwz_sections (bfd *abfd, asection *sectp, void *arg)
2027{
2028 struct dwz_file *dwz_file = arg;
2029
2030 /* Note that we only support the standard ELF names, because .dwz
2031 is ELF-only (at the time of writing). */
2032 if (section_is_p (sectp->name, &dwarf2_elf_names.abbrev))
2033 {
2034 dwz_file->abbrev.asection = sectp;
2035 dwz_file->abbrev.size = bfd_get_section_size (sectp);
2036 }
2037 else if (section_is_p (sectp->name, &dwarf2_elf_names.info))
2038 {
2039 dwz_file->info.asection = sectp;
2040 dwz_file->info.size = bfd_get_section_size (sectp);
2041 }
2042 else if (section_is_p (sectp->name, &dwarf2_elf_names.str))
2043 {
2044 dwz_file->str.asection = sectp;
2045 dwz_file->str.size = bfd_get_section_size (sectp);
2046 }
2047 else if (section_is_p (sectp->name, &dwarf2_elf_names.line))
2048 {
2049 dwz_file->line.asection = sectp;
2050 dwz_file->line.size = bfd_get_section_size (sectp);
2051 }
2052 else if (section_is_p (sectp->name, &dwarf2_elf_names.macro))
2053 {
2054 dwz_file->macro.asection = sectp;
2055 dwz_file->macro.size = bfd_get_section_size (sectp);
2056 }
2ec9a5e0
TT
2057 else if (section_is_p (sectp->name, &dwarf2_elf_names.gdb_index))
2058 {
2059 dwz_file->gdb_index.asection = sectp;
2060 dwz_file->gdb_index.size = bfd_get_section_size (sectp);
2061 }
36586728
TT
2062}
2063
2064/* Open the separate '.dwz' debug file, if needed. Error if the file
2065 cannot be found. */
2066
2067static struct dwz_file *
2068dwarf2_get_dwz_file (void)
2069{
2070 bfd *abfd, *dwz_bfd;
2071 asection *section;
2072 gdb_byte *data;
2073 struct cleanup *cleanup;
2074 const char *filename;
2075 struct dwz_file *result;
2076
2077 if (dwarf2_per_objfile->dwz_file != NULL)
2078 return dwarf2_per_objfile->dwz_file;
2079
2080 abfd = dwarf2_per_objfile->objfile->obfd;
2081 section = bfd_get_section_by_name (abfd, ".gnu_debugaltlink");
2082 if (section == NULL)
2083 error (_("could not find '.gnu_debugaltlink' section"));
2084 if (!bfd_malloc_and_get_section (abfd, section, &data))
2085 error (_("could not read '.gnu_debugaltlink' section: %s"),
2086 bfd_errmsg (bfd_get_error ()));
2087 cleanup = make_cleanup (xfree, data);
2088
2089 filename = data;
2090 if (!IS_ABSOLUTE_PATH (filename))
2091 {
2092 char *abs = gdb_realpath (dwarf2_per_objfile->objfile->name);
2093 char *rel;
2094
2095 make_cleanup (xfree, abs);
2096 abs = ldirname (abs);
2097 make_cleanup (xfree, abs);
2098
2099 rel = concat (abs, SLASH_STRING, filename, (char *) NULL);
2100 make_cleanup (xfree, rel);
2101 filename = rel;
2102 }
2103
2104 /* The format is just a NUL-terminated file name, followed by the
2105 build-id. For now, though, we ignore the build-id. */
2106 dwz_bfd = gdb_bfd_open (filename, gnutarget, -1);
2107 if (dwz_bfd == NULL)
2108 error (_("could not read '%s': %s"), filename,
2109 bfd_errmsg (bfd_get_error ()));
2110
2111 if (!bfd_check_format (dwz_bfd, bfd_object))
2112 {
2113 gdb_bfd_unref (dwz_bfd);
2114 error (_("file '%s' was not usable: %s"), filename,
2115 bfd_errmsg (bfd_get_error ()));
2116 }
2117
2118 result = OBSTACK_ZALLOC (&dwarf2_per_objfile->objfile->objfile_obstack,
2119 struct dwz_file);
2120 result->dwz_bfd = dwz_bfd;
2121
2122 bfd_map_over_sections (dwz_bfd, locate_dwz_sections, result);
2123
2124 do_cleanups (cleanup);
2125
8d2cc612 2126 dwarf2_per_objfile->dwz_file = result;
36586728
TT
2127 return result;
2128}
9291a0cd 2129\f
7b9f3c50
DE
2130/* DWARF quick_symbols_functions support. */
2131
2132/* TUs can share .debug_line entries, and there can be a lot more TUs than
2133 unique line tables, so we maintain a separate table of all .debug_line
2134 derived entries to support the sharing.
2135 All the quick functions need is the list of file names. We discard the
2136 line_header when we're done and don't need to record it here. */
2137struct quick_file_names
2138{
094b34ac
DE
2139 /* The data used to construct the hash key. */
2140 struct stmt_list_hash hash;
7b9f3c50
DE
2141
2142 /* The number of entries in file_names, real_names. */
2143 unsigned int num_file_names;
2144
2145 /* The file names from the line table, after being run through
2146 file_full_name. */
2147 const char **file_names;
2148
2149 /* The file names from the line table after being run through
2150 gdb_realpath. These are computed lazily. */
2151 const char **real_names;
2152};
2153
2154/* When using the index (and thus not using psymtabs), each CU has an
2155 object of this type. This is used to hold information needed by
2156 the various "quick" methods. */
2157struct dwarf2_per_cu_quick_data
2158{
2159 /* The file table. This can be NULL if there was no file table
2160 or it's currently not read in.
2161 NOTE: This points into dwarf2_per_objfile->quick_file_names_table. */
2162 struct quick_file_names *file_names;
2163
2164 /* The corresponding symbol table. This is NULL if symbols for this
2165 CU have not yet been read. */
2166 struct symtab *symtab;
2167
2168 /* A temporary mark bit used when iterating over all CUs in
2169 expand_symtabs_matching. */
2170 unsigned int mark : 1;
2171
2172 /* True if we've tried to read the file table and found there isn't one.
2173 There will be no point in trying to read it again next time. */
2174 unsigned int no_file_data : 1;
2175};
2176
094b34ac
DE
2177/* Utility hash function for a stmt_list_hash. */
2178
2179static hashval_t
2180hash_stmt_list_entry (const struct stmt_list_hash *stmt_list_hash)
2181{
2182 hashval_t v = 0;
2183
2184 if (stmt_list_hash->dwo_unit != NULL)
2185 v += (uintptr_t) stmt_list_hash->dwo_unit->dwo_file;
2186 v += stmt_list_hash->line_offset.sect_off;
2187 return v;
2188}
2189
2190/* Utility equality function for a stmt_list_hash. */
2191
2192static int
2193eq_stmt_list_entry (const struct stmt_list_hash *lhs,
2194 const struct stmt_list_hash *rhs)
2195{
2196 if ((lhs->dwo_unit != NULL) != (rhs->dwo_unit != NULL))
2197 return 0;
2198 if (lhs->dwo_unit != NULL
2199 && lhs->dwo_unit->dwo_file != rhs->dwo_unit->dwo_file)
2200 return 0;
2201
2202 return lhs->line_offset.sect_off == rhs->line_offset.sect_off;
2203}
2204
7b9f3c50
DE
2205/* Hash function for a quick_file_names. */
2206
2207static hashval_t
2208hash_file_name_entry (const void *e)
2209{
2210 const struct quick_file_names *file_data = e;
2211
094b34ac 2212 return hash_stmt_list_entry (&file_data->hash);
7b9f3c50
DE
2213}
2214
2215/* Equality function for a quick_file_names. */
2216
2217static int
2218eq_file_name_entry (const void *a, const void *b)
2219{
2220 const struct quick_file_names *ea = a;
2221 const struct quick_file_names *eb = b;
2222
094b34ac 2223 return eq_stmt_list_entry (&ea->hash, &eb->hash);
7b9f3c50
DE
2224}
2225
2226/* Delete function for a quick_file_names. */
2227
2228static void
2229delete_file_name_entry (void *e)
2230{
2231 struct quick_file_names *file_data = e;
2232 int i;
2233
2234 for (i = 0; i < file_data->num_file_names; ++i)
2235 {
2236 xfree ((void*) file_data->file_names[i]);
2237 if (file_data->real_names)
2238 xfree ((void*) file_data->real_names[i]);
2239 }
2240
2241 /* The space for the struct itself lives on objfile_obstack,
2242 so we don't free it here. */
2243}
2244
2245/* Create a quick_file_names hash table. */
2246
2247static htab_t
2248create_quick_file_names_table (unsigned int nr_initial_entries)
2249{
2250 return htab_create_alloc (nr_initial_entries,
2251 hash_file_name_entry, eq_file_name_entry,
2252 delete_file_name_entry, xcalloc, xfree);
2253}
9291a0cd 2254
918dd910
JK
2255/* Read in PER_CU->CU. This function is unrelated to symtabs, symtab would
2256 have to be created afterwards. You should call age_cached_comp_units after
2257 processing PER_CU->CU. dw2_setup must have been already called. */
2258
2259static void
2260load_cu (struct dwarf2_per_cu_data *per_cu)
2261{
3019eac3 2262 if (per_cu->is_debug_types)
e5fe5e75 2263 load_full_type_unit (per_cu);
918dd910 2264 else
95554aad 2265 load_full_comp_unit (per_cu, language_minimal);
918dd910 2266
918dd910 2267 gdb_assert (per_cu->cu != NULL);
2dc860c0
DE
2268
2269 dwarf2_find_base_address (per_cu->cu->dies, per_cu->cu);
918dd910
JK
2270}
2271
a0f42c21 2272/* Read in the symbols for PER_CU. */
2fdf6df6 2273
9291a0cd 2274static void
a0f42c21 2275dw2_do_instantiate_symtab (struct dwarf2_per_cu_data *per_cu)
9291a0cd
TT
2276{
2277 struct cleanup *back_to;
2278
f4dc4d17
DE
2279 /* Skip type_unit_groups, reading the type units they contain
2280 is handled elsewhere. */
2281 if (IS_TYPE_UNIT_GROUP (per_cu))
2282 return;
2283
9291a0cd
TT
2284 back_to = make_cleanup (dwarf2_release_queue, NULL);
2285
95554aad
TT
2286 if (dwarf2_per_objfile->using_index
2287 ? per_cu->v.quick->symtab == NULL
2288 : (per_cu->v.psymtab == NULL || !per_cu->v.psymtab->readin))
2289 {
2290 queue_comp_unit (per_cu, language_minimal);
2291 load_cu (per_cu);
2292 }
9291a0cd 2293
a0f42c21 2294 process_queue ();
9291a0cd
TT
2295
2296 /* Age the cache, releasing compilation units that have not
2297 been used recently. */
2298 age_cached_comp_units ();
2299
2300 do_cleanups (back_to);
2301}
2302
2303/* Ensure that the symbols for PER_CU have been read in. OBJFILE is
2304 the objfile from which this CU came. Returns the resulting symbol
2305 table. */
2fdf6df6 2306
9291a0cd 2307static struct symtab *
a0f42c21 2308dw2_instantiate_symtab (struct dwarf2_per_cu_data *per_cu)
9291a0cd 2309{
95554aad 2310 gdb_assert (dwarf2_per_objfile->using_index);
9291a0cd
TT
2311 if (!per_cu->v.quick->symtab)
2312 {
2313 struct cleanup *back_to = make_cleanup (free_cached_comp_units, NULL);
2314 increment_reading_symtab ();
a0f42c21 2315 dw2_do_instantiate_symtab (per_cu);
95554aad 2316 process_cu_includes ();
9291a0cd
TT
2317 do_cleanups (back_to);
2318 }
2319 return per_cu->v.quick->symtab;
2320}
2321
f4dc4d17
DE
2322/* Return the CU given its index.
2323
2324 This is intended for loops like:
2325
2326 for (i = 0; i < (dwarf2_per_objfile->n_comp_units
2327 + dwarf2_per_objfile->n_type_units); ++i)
2328 {
2329 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
2330
2331 ...;
2332 }
2333*/
2fdf6df6 2334
1fd400ff
TT
2335static struct dwarf2_per_cu_data *
2336dw2_get_cu (int index)
2337{
2338 if (index >= dwarf2_per_objfile->n_comp_units)
2339 {
f4dc4d17 2340 index -= dwarf2_per_objfile->n_comp_units;
094b34ac
DE
2341 gdb_assert (index < dwarf2_per_objfile->n_type_units);
2342 return &dwarf2_per_objfile->all_type_units[index]->per_cu;
f4dc4d17
DE
2343 }
2344
2345 return dwarf2_per_objfile->all_comp_units[index];
2346}
2347
2348/* Return the primary CU given its index.
2349 The difference between this function and dw2_get_cu is in the handling
2350 of type units (TUs). Here we return the type_unit_group object.
2351
2352 This is intended for loops like:
2353
2354 for (i = 0; i < (dwarf2_per_objfile->n_comp_units
2355 + dwarf2_per_objfile->n_type_unit_groups); ++i)
2356 {
2357 struct dwarf2_per_cu_data *per_cu = dw2_get_primary_cu (i);
2358
2359 ...;
2360 }
2361*/
2362
2363static struct dwarf2_per_cu_data *
2364dw2_get_primary_cu (int index)
2365{
2366 if (index >= dwarf2_per_objfile->n_comp_units)
2367 {
1fd400ff 2368 index -= dwarf2_per_objfile->n_comp_units;
094b34ac
DE
2369 gdb_assert (index < dwarf2_per_objfile->n_type_unit_groups);
2370 return &dwarf2_per_objfile->all_type_unit_groups[index]->per_cu;
1fd400ff 2371 }
f4dc4d17 2372
1fd400ff
TT
2373 return dwarf2_per_objfile->all_comp_units[index];
2374}
2375
2ec9a5e0
TT
2376/* A helper for create_cus_from_index that handles a given list of
2377 CUs. */
2fdf6df6 2378
74a0d9f6 2379static void
2ec9a5e0
TT
2380create_cus_from_index_list (struct objfile *objfile,
2381 const gdb_byte *cu_list, offset_type n_elements,
2382 struct dwarf2_section_info *section,
2383 int is_dwz,
2384 int base_offset)
9291a0cd
TT
2385{
2386 offset_type i;
9291a0cd 2387
2ec9a5e0 2388 for (i = 0; i < n_elements; i += 2)
9291a0cd
TT
2389 {
2390 struct dwarf2_per_cu_data *the_cu;
2391 ULONGEST offset, length;
2392
74a0d9f6
JK
2393 gdb_static_assert (sizeof (ULONGEST) >= 8);
2394 offset = extract_unsigned_integer (cu_list, 8, BFD_ENDIAN_LITTLE);
2395 length = extract_unsigned_integer (cu_list + 8, 8, BFD_ENDIAN_LITTLE);
9291a0cd
TT
2396 cu_list += 2 * 8;
2397
2398 the_cu = OBSTACK_ZALLOC (&objfile->objfile_obstack,
2399 struct dwarf2_per_cu_data);
b64f50a1 2400 the_cu->offset.sect_off = offset;
9291a0cd
TT
2401 the_cu->length = length;
2402 the_cu->objfile = objfile;
2ec9a5e0 2403 the_cu->info_or_types_section = section;
9291a0cd
TT
2404 the_cu->v.quick = OBSTACK_ZALLOC (&objfile->objfile_obstack,
2405 struct dwarf2_per_cu_quick_data);
2ec9a5e0
TT
2406 the_cu->is_dwz = is_dwz;
2407 dwarf2_per_objfile->all_comp_units[base_offset + i / 2] = the_cu;
9291a0cd 2408 }
9291a0cd
TT
2409}
2410
2ec9a5e0 2411/* Read the CU list from the mapped index, and use it to create all
74a0d9f6 2412 the CU objects for this objfile. */
2ec9a5e0 2413
74a0d9f6 2414static void
2ec9a5e0
TT
2415create_cus_from_index (struct objfile *objfile,
2416 const gdb_byte *cu_list, offset_type cu_list_elements,
2417 const gdb_byte *dwz_list, offset_type dwz_elements)
2418{
2419 struct dwz_file *dwz;
2420
2421 dwarf2_per_objfile->n_comp_units = (cu_list_elements + dwz_elements) / 2;
2422 dwarf2_per_objfile->all_comp_units
2423 = obstack_alloc (&objfile->objfile_obstack,
2424 dwarf2_per_objfile->n_comp_units
2425 * sizeof (struct dwarf2_per_cu_data *));
2426
74a0d9f6
JK
2427 create_cus_from_index_list (objfile, cu_list, cu_list_elements,
2428 &dwarf2_per_objfile->info, 0, 0);
2ec9a5e0
TT
2429
2430 if (dwz_elements == 0)
74a0d9f6 2431 return;
2ec9a5e0
TT
2432
2433 dwz = dwarf2_get_dwz_file ();
74a0d9f6
JK
2434 create_cus_from_index_list (objfile, dwz_list, dwz_elements, &dwz->info, 1,
2435 cu_list_elements / 2);
2ec9a5e0
TT
2436}
2437
1fd400ff 2438/* Create the signatured type hash table from the index. */
673bfd45 2439
74a0d9f6 2440static void
673bfd45 2441create_signatured_type_table_from_index (struct objfile *objfile,
8b70b953 2442 struct dwarf2_section_info *section,
673bfd45
DE
2443 const gdb_byte *bytes,
2444 offset_type elements)
1fd400ff
TT
2445{
2446 offset_type i;
673bfd45 2447 htab_t sig_types_hash;
1fd400ff 2448
d467dd73
DE
2449 dwarf2_per_objfile->n_type_units = elements / 3;
2450 dwarf2_per_objfile->all_type_units
1fd400ff 2451 = obstack_alloc (&objfile->objfile_obstack,
d467dd73 2452 dwarf2_per_objfile->n_type_units
b4dd5633 2453 * sizeof (struct signatured_type *));
1fd400ff 2454
673bfd45 2455 sig_types_hash = allocate_signatured_type_table (objfile);
1fd400ff
TT
2456
2457 for (i = 0; i < elements; i += 3)
2458 {
52dc124a
DE
2459 struct signatured_type *sig_type;
2460 ULONGEST offset, type_offset_in_tu, signature;
1fd400ff
TT
2461 void **slot;
2462
74a0d9f6
JK
2463 gdb_static_assert (sizeof (ULONGEST) >= 8);
2464 offset = extract_unsigned_integer (bytes, 8, BFD_ENDIAN_LITTLE);
2465 type_offset_in_tu = extract_unsigned_integer (bytes + 8, 8,
2466 BFD_ENDIAN_LITTLE);
1fd400ff
TT
2467 signature = extract_unsigned_integer (bytes + 16, 8, BFD_ENDIAN_LITTLE);
2468 bytes += 3 * 8;
2469
52dc124a 2470 sig_type = OBSTACK_ZALLOC (&objfile->objfile_obstack,
1fd400ff 2471 struct signatured_type);
52dc124a 2472 sig_type->signature = signature;
3019eac3
DE
2473 sig_type->type_offset_in_tu.cu_off = type_offset_in_tu;
2474 sig_type->per_cu.is_debug_types = 1;
2475 sig_type->per_cu.info_or_types_section = section;
52dc124a
DE
2476 sig_type->per_cu.offset.sect_off = offset;
2477 sig_type->per_cu.objfile = objfile;
2478 sig_type->per_cu.v.quick
1fd400ff
TT
2479 = OBSTACK_ZALLOC (&objfile->objfile_obstack,
2480 struct dwarf2_per_cu_quick_data);
2481
52dc124a
DE
2482 slot = htab_find_slot (sig_types_hash, sig_type, INSERT);
2483 *slot = sig_type;
1fd400ff 2484
b4dd5633 2485 dwarf2_per_objfile->all_type_units[i / 3] = sig_type;
1fd400ff
TT
2486 }
2487
673bfd45 2488 dwarf2_per_objfile->signatured_types = sig_types_hash;
1fd400ff
TT
2489}
2490
9291a0cd
TT
2491/* Read the address map data from the mapped index, and use it to
2492 populate the objfile's psymtabs_addrmap. */
2fdf6df6 2493
9291a0cd
TT
2494static void
2495create_addrmap_from_index (struct objfile *objfile, struct mapped_index *index)
2496{
2497 const gdb_byte *iter, *end;
2498 struct obstack temp_obstack;
2499 struct addrmap *mutable_map;
2500 struct cleanup *cleanup;
2501 CORE_ADDR baseaddr;
2502
2503 obstack_init (&temp_obstack);
2504 cleanup = make_cleanup_obstack_free (&temp_obstack);
2505 mutable_map = addrmap_create_mutable (&temp_obstack);
2506
2507 iter = index->address_table;
2508 end = iter + index->address_table_size;
2509
2510 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
2511
2512 while (iter < end)
2513 {
2514 ULONGEST hi, lo, cu_index;
2515 lo = extract_unsigned_integer (iter, 8, BFD_ENDIAN_LITTLE);
2516 iter += 8;
2517 hi = extract_unsigned_integer (iter, 8, BFD_ENDIAN_LITTLE);
2518 iter += 8;
2519 cu_index = extract_unsigned_integer (iter, 4, BFD_ENDIAN_LITTLE);
2520 iter += 4;
2521
2522 addrmap_set_empty (mutable_map, lo + baseaddr, hi + baseaddr - 1,
1fd400ff 2523 dw2_get_cu (cu_index));
9291a0cd
TT
2524 }
2525
2526 objfile->psymtabs_addrmap = addrmap_create_fixed (mutable_map,
2527 &objfile->objfile_obstack);
2528 do_cleanups (cleanup);
2529}
2530
59d7bcaf
JK
2531/* The hash function for strings in the mapped index. This is the same as
2532 SYMBOL_HASH_NEXT, but we keep a separate copy to maintain control over the
2533 implementation. This is necessary because the hash function is tied to the
2534 format of the mapped index file. The hash values do not have to match with
559a7a62
JK
2535 SYMBOL_HASH_NEXT.
2536
2537 Use INT_MAX for INDEX_VERSION if you generate the current index format. */
2fdf6df6 2538
9291a0cd 2539static hashval_t
559a7a62 2540mapped_index_string_hash (int index_version, const void *p)
9291a0cd
TT
2541{
2542 const unsigned char *str = (const unsigned char *) p;
2543 hashval_t r = 0;
2544 unsigned char c;
2545
2546 while ((c = *str++) != 0)
559a7a62
JK
2547 {
2548 if (index_version >= 5)
2549 c = tolower (c);
2550 r = r * 67 + c - 113;
2551 }
9291a0cd
TT
2552
2553 return r;
2554}
2555
2556/* Find a slot in the mapped index INDEX for the object named NAME.
2557 If NAME is found, set *VEC_OUT to point to the CU vector in the
2558 constant pool and return 1. If NAME cannot be found, return 0. */
2fdf6df6 2559
9291a0cd
TT
2560static int
2561find_slot_in_mapped_hash (struct mapped_index *index, const char *name,
2562 offset_type **vec_out)
2563{
0cf03b49
JK
2564 struct cleanup *back_to = make_cleanup (null_cleanup, 0);
2565 offset_type hash;
9291a0cd 2566 offset_type slot, step;
559a7a62 2567 int (*cmp) (const char *, const char *);
9291a0cd 2568
0cf03b49
JK
2569 if (current_language->la_language == language_cplus
2570 || current_language->la_language == language_java
2571 || current_language->la_language == language_fortran)
2572 {
2573 /* NAME is already canonical. Drop any qualifiers as .gdb_index does
2574 not contain any. */
2575 const char *paren = strchr (name, '(');
2576
2577 if (paren)
2578 {
2579 char *dup;
2580
2581 dup = xmalloc (paren - name + 1);
2582 memcpy (dup, name, paren - name);
2583 dup[paren - name] = 0;
2584
2585 make_cleanup (xfree, dup);
2586 name = dup;
2587 }
2588 }
2589
559a7a62 2590 /* Index version 4 did not support case insensitive searches. But the
feea76c2 2591 indices for case insensitive languages are built in lowercase, therefore
559a7a62
JK
2592 simulate our NAME being searched is also lowercased. */
2593 hash = mapped_index_string_hash ((index->version == 4
2594 && case_sensitivity == case_sensitive_off
2595 ? 5 : index->version),
2596 name);
2597
3876f04e
DE
2598 slot = hash & (index->symbol_table_slots - 1);
2599 step = ((hash * 17) & (index->symbol_table_slots - 1)) | 1;
559a7a62 2600 cmp = (case_sensitivity == case_sensitive_on ? strcmp : strcasecmp);
9291a0cd
TT
2601
2602 for (;;)
2603 {
2604 /* Convert a slot number to an offset into the table. */
2605 offset_type i = 2 * slot;
2606 const char *str;
3876f04e 2607 if (index->symbol_table[i] == 0 && index->symbol_table[i + 1] == 0)
0cf03b49
JK
2608 {
2609 do_cleanups (back_to);
2610 return 0;
2611 }
9291a0cd 2612
3876f04e 2613 str = index->constant_pool + MAYBE_SWAP (index->symbol_table[i]);
559a7a62 2614 if (!cmp (name, str))
9291a0cd
TT
2615 {
2616 *vec_out = (offset_type *) (index->constant_pool
3876f04e 2617 + MAYBE_SWAP (index->symbol_table[i + 1]));
0cf03b49 2618 do_cleanups (back_to);
9291a0cd
TT
2619 return 1;
2620 }
2621
3876f04e 2622 slot = (slot + step) & (index->symbol_table_slots - 1);
9291a0cd
TT
2623 }
2624}
2625
2ec9a5e0
TT
2626/* A helper function that reads the .gdb_index from SECTION and fills
2627 in MAP. FILENAME is the name of the file containing the section;
2628 it is used for error reporting. DEPRECATED_OK is nonzero if it is
2629 ok to use deprecated sections.
2630
2631 CU_LIST, CU_LIST_ELEMENTS, TYPES_LIST, and TYPES_LIST_ELEMENTS are
2632 out parameters that are filled in with information about the CU and
2633 TU lists in the section.
2634
2635 Returns 1 if all went well, 0 otherwise. */
2fdf6df6 2636
9291a0cd 2637static int
2ec9a5e0
TT
2638read_index_from_section (struct objfile *objfile,
2639 const char *filename,
2640 int deprecated_ok,
2641 struct dwarf2_section_info *section,
2642 struct mapped_index *map,
2643 const gdb_byte **cu_list,
2644 offset_type *cu_list_elements,
2645 const gdb_byte **types_list,
2646 offset_type *types_list_elements)
9291a0cd 2647{
9291a0cd 2648 char *addr;
2ec9a5e0 2649 offset_type version;
b3b272e1 2650 offset_type *metadata;
1fd400ff 2651 int i;
9291a0cd 2652
2ec9a5e0 2653 if (dwarf2_section_empty_p (section))
9291a0cd 2654 return 0;
82430852
JK
2655
2656 /* Older elfutils strip versions could keep the section in the main
2657 executable while splitting it for the separate debug info file. */
2ec9a5e0 2658 if ((bfd_get_file_flags (section->asection) & SEC_HAS_CONTENTS) == 0)
82430852
JK
2659 return 0;
2660
2ec9a5e0 2661 dwarf2_read_section (objfile, section);
9291a0cd 2662
2ec9a5e0 2663 addr = section->buffer;
9291a0cd 2664 /* Version check. */
1fd400ff 2665 version = MAYBE_SWAP (*(offset_type *) addr);
987d643c 2666 /* Versions earlier than 3 emitted every copy of a psymbol. This
a6e293d1 2667 causes the index to behave very poorly for certain requests. Version 3
831adc1f 2668 contained incomplete addrmap. So, it seems better to just ignore such
481860b3 2669 indices. */
831adc1f 2670 if (version < 4)
481860b3
GB
2671 {
2672 static int warning_printed = 0;
2673 if (!warning_printed)
2674 {
2675 warning (_("Skipping obsolete .gdb_index section in %s."),
2ec9a5e0 2676 filename);
481860b3
GB
2677 warning_printed = 1;
2678 }
2679 return 0;
2680 }
2681 /* Index version 4 uses a different hash function than index version
2682 5 and later.
2683
2684 Versions earlier than 6 did not emit psymbols for inlined
2685 functions. Using these files will cause GDB not to be able to
2686 set breakpoints on inlined functions by name, so we ignore these
e615022a
DE
2687 indices unless the user has done
2688 "set use-deprecated-index-sections on". */
2ec9a5e0 2689 if (version < 6 && !deprecated_ok)
481860b3
GB
2690 {
2691 static int warning_printed = 0;
2692 if (!warning_printed)
2693 {
e615022a
DE
2694 warning (_("\
2695Skipping deprecated .gdb_index section in %s.\n\
2696Do \"set use-deprecated-index-sections on\" before the file is read\n\
2697to use the section anyway."),
2ec9a5e0 2698 filename);
481860b3
GB
2699 warning_printed = 1;
2700 }
2701 return 0;
2702 }
796a7ff8
DE
2703 /* Version 7 indices generated by gold refer to the CU for a symbol instead
2704 of the TU (for symbols coming from TUs). It's just a performance bug, and
2705 we can't distinguish gdb-generated indices from gold-generated ones, so
2706 nothing to do here. */
2707
481860b3 2708 /* Indexes with higher version than the one supported by GDB may be no
594e8718 2709 longer backward compatible. */
796a7ff8 2710 if (version > 8)
594e8718 2711 return 0;
9291a0cd 2712
559a7a62 2713 map->version = version;
2ec9a5e0 2714 map->total_size = section->size;
9291a0cd
TT
2715
2716 metadata = (offset_type *) (addr + sizeof (offset_type));
1fd400ff
TT
2717
2718 i = 0;
2ec9a5e0
TT
2719 *cu_list = addr + MAYBE_SWAP (metadata[i]);
2720 *cu_list_elements = ((MAYBE_SWAP (metadata[i + 1]) - MAYBE_SWAP (metadata[i]))
2721 / 8);
1fd400ff
TT
2722 ++i;
2723
2ec9a5e0
TT
2724 *types_list = addr + MAYBE_SWAP (metadata[i]);
2725 *types_list_elements = ((MAYBE_SWAP (metadata[i + 1])
2726 - MAYBE_SWAP (metadata[i]))
2727 / 8);
987d643c 2728 ++i;
1fd400ff
TT
2729
2730 map->address_table = addr + MAYBE_SWAP (metadata[i]);
2731 map->address_table_size = (MAYBE_SWAP (metadata[i + 1])
2732 - MAYBE_SWAP (metadata[i]));
2733 ++i;
2734
3876f04e
DE
2735 map->symbol_table = (offset_type *) (addr + MAYBE_SWAP (metadata[i]));
2736 map->symbol_table_slots = ((MAYBE_SWAP (metadata[i + 1])
2737 - MAYBE_SWAP (metadata[i]))
2738 / (2 * sizeof (offset_type)));
1fd400ff 2739 ++i;
9291a0cd 2740
1fd400ff
TT
2741 map->constant_pool = addr + MAYBE_SWAP (metadata[i]);
2742
2ec9a5e0
TT
2743 return 1;
2744}
2745
2746
2747/* Read the index file. If everything went ok, initialize the "quick"
2748 elements of all the CUs and return 1. Otherwise, return 0. */
2749
2750static int
2751dwarf2_read_index (struct objfile *objfile)
2752{
2753 struct mapped_index local_map, *map;
2754 const gdb_byte *cu_list, *types_list, *dwz_list = NULL;
2755 offset_type cu_list_elements, types_list_elements, dwz_list_elements = 0;
2756
2757 if (!read_index_from_section (objfile, objfile->name,
2758 use_deprecated_index_sections,
2759 &dwarf2_per_objfile->gdb_index, &local_map,
2760 &cu_list, &cu_list_elements,
2761 &types_list, &types_list_elements))
2762 return 0;
2763
0fefef59 2764 /* Don't use the index if it's empty. */
2ec9a5e0 2765 if (local_map.symbol_table_slots == 0)
0fefef59
DE
2766 return 0;
2767
2ec9a5e0
TT
2768 /* If there is a .dwz file, read it so we can get its CU list as
2769 well. */
2770 if (bfd_get_section_by_name (objfile->obfd, ".gnu_debugaltlink") != NULL)
2771 {
2772 struct dwz_file *dwz = dwarf2_get_dwz_file ();
2773 struct mapped_index dwz_map;
2774 const gdb_byte *dwz_types_ignore;
2775 offset_type dwz_types_elements_ignore;
2776
2777 if (!read_index_from_section (objfile, bfd_get_filename (dwz->dwz_bfd),
2778 1,
2779 &dwz->gdb_index, &dwz_map,
2780 &dwz_list, &dwz_list_elements,
2781 &dwz_types_ignore,
2782 &dwz_types_elements_ignore))
2783 {
2784 warning (_("could not read '.gdb_index' section from %s; skipping"),
2785 bfd_get_filename (dwz->dwz_bfd));
2786 return 0;
2787 }
2788 }
2789
74a0d9f6
JK
2790 create_cus_from_index (objfile, cu_list, cu_list_elements, dwz_list,
2791 dwz_list_elements);
1fd400ff 2792
8b70b953
TT
2793 if (types_list_elements)
2794 {
2795 struct dwarf2_section_info *section;
2796
2797 /* We can only handle a single .debug_types when we have an
2798 index. */
2799 if (VEC_length (dwarf2_section_info_def, dwarf2_per_objfile->types) != 1)
2800 return 0;
2801
2802 section = VEC_index (dwarf2_section_info_def,
2803 dwarf2_per_objfile->types, 0);
2804
74a0d9f6
JK
2805 create_signatured_type_table_from_index (objfile, section, types_list,
2806 types_list_elements);
8b70b953 2807 }
9291a0cd 2808
2ec9a5e0
TT
2809 create_addrmap_from_index (objfile, &local_map);
2810
2811 map = obstack_alloc (&objfile->objfile_obstack, sizeof (struct mapped_index));
2812 *map = local_map;
9291a0cd
TT
2813
2814 dwarf2_per_objfile->index_table = map;
2815 dwarf2_per_objfile->using_index = 1;
7b9f3c50
DE
2816 dwarf2_per_objfile->quick_file_names_table =
2817 create_quick_file_names_table (dwarf2_per_objfile->n_comp_units);
9291a0cd
TT
2818
2819 return 1;
2820}
2821
2822/* A helper for the "quick" functions which sets the global
2823 dwarf2_per_objfile according to OBJFILE. */
2fdf6df6 2824
9291a0cd
TT
2825static void
2826dw2_setup (struct objfile *objfile)
2827{
2828 dwarf2_per_objfile = objfile_data (objfile, dwarf2_objfile_data_key);
2829 gdb_assert (dwarf2_per_objfile);
2830}
2831
dee91e82 2832/* die_reader_func for dw2_get_file_names. */
2fdf6df6 2833
dee91e82
DE
2834static void
2835dw2_get_file_names_reader (const struct die_reader_specs *reader,
2836 gdb_byte *info_ptr,
2837 struct die_info *comp_unit_die,
2838 int has_children,
2839 void *data)
9291a0cd 2840{
dee91e82
DE
2841 struct dwarf2_cu *cu = reader->cu;
2842 struct dwarf2_per_cu_data *this_cu = cu->per_cu;
2843 struct objfile *objfile = dwarf2_per_objfile->objfile;
094b34ac 2844 struct dwarf2_per_cu_data *lh_cu;
7b9f3c50 2845 struct line_header *lh;
9291a0cd 2846 struct attribute *attr;
dee91e82 2847 int i;
15d034d0 2848 const char *name, *comp_dir;
7b9f3c50
DE
2849 void **slot;
2850 struct quick_file_names *qfn;
2851 unsigned int line_offset;
9291a0cd 2852
07261596
TT
2853 /* Our callers never want to match partial units -- instead they
2854 will match the enclosing full CU. */
2855 if (comp_unit_die->tag == DW_TAG_partial_unit)
2856 {
2857 this_cu->v.quick->no_file_data = 1;
2858 return;
2859 }
2860
094b34ac
DE
2861 /* If we're reading the line header for TUs, store it in the "per_cu"
2862 for tu_group. */
2863 if (this_cu->is_debug_types)
2864 {
2865 struct type_unit_group *tu_group = data;
2866
2867 gdb_assert (tu_group != NULL);
2868 lh_cu = &tu_group->per_cu;
2869 }
2870 else
2871 lh_cu = this_cu;
2872
7b9f3c50
DE
2873 lh = NULL;
2874 slot = NULL;
2875 line_offset = 0;
dee91e82
DE
2876
2877 attr = dwarf2_attr (comp_unit_die, DW_AT_stmt_list, cu);
9291a0cd
TT
2878 if (attr)
2879 {
7b9f3c50
DE
2880 struct quick_file_names find_entry;
2881
2882 line_offset = DW_UNSND (attr);
2883
2884 /* We may have already read in this line header (TU line header sharing).
2885 If we have we're done. */
094b34ac
DE
2886 find_entry.hash.dwo_unit = cu->dwo_unit;
2887 find_entry.hash.line_offset.sect_off = line_offset;
7b9f3c50
DE
2888 slot = htab_find_slot (dwarf2_per_objfile->quick_file_names_table,
2889 &find_entry, INSERT);
2890 if (*slot != NULL)
2891 {
094b34ac 2892 lh_cu->v.quick->file_names = *slot;
dee91e82 2893 return;
7b9f3c50
DE
2894 }
2895
3019eac3 2896 lh = dwarf_decode_line_header (line_offset, cu);
9291a0cd
TT
2897 }
2898 if (lh == NULL)
2899 {
094b34ac 2900 lh_cu->v.quick->no_file_data = 1;
dee91e82 2901 return;
9291a0cd
TT
2902 }
2903
7b9f3c50 2904 qfn = obstack_alloc (&objfile->objfile_obstack, sizeof (*qfn));
094b34ac
DE
2905 qfn->hash.dwo_unit = cu->dwo_unit;
2906 qfn->hash.line_offset.sect_off = line_offset;
7b9f3c50
DE
2907 gdb_assert (slot != NULL);
2908 *slot = qfn;
9291a0cd 2909
dee91e82 2910 find_file_and_directory (comp_unit_die, cu, &name, &comp_dir);
9291a0cd 2911
7b9f3c50
DE
2912 qfn->num_file_names = lh->num_file_names;
2913 qfn->file_names = obstack_alloc (&objfile->objfile_obstack,
2914 lh->num_file_names * sizeof (char *));
9291a0cd 2915 for (i = 0; i < lh->num_file_names; ++i)
7b9f3c50
DE
2916 qfn->file_names[i] = file_full_name (i + 1, lh, comp_dir);
2917 qfn->real_names = NULL;
9291a0cd 2918
7b9f3c50 2919 free_line_header (lh);
7b9f3c50 2920
094b34ac 2921 lh_cu->v.quick->file_names = qfn;
dee91e82
DE
2922}
2923
2924/* A helper for the "quick" functions which attempts to read the line
2925 table for THIS_CU. */
2926
2927static struct quick_file_names *
2928dw2_get_file_names (struct objfile *objfile,
2929 struct dwarf2_per_cu_data *this_cu)
2930{
f4dc4d17
DE
2931 /* For TUs this should only be called on the parent group. */
2932 if (this_cu->is_debug_types)
2933 gdb_assert (IS_TYPE_UNIT_GROUP (this_cu));
2934
dee91e82
DE
2935 if (this_cu->v.quick->file_names != NULL)
2936 return this_cu->v.quick->file_names;
2937 /* If we know there is no line data, no point in looking again. */
2938 if (this_cu->v.quick->no_file_data)
2939 return NULL;
2940
3019eac3
DE
2941 /* If DWO files are in use, we can still find the DW_AT_stmt_list attribute
2942 in the stub for CUs, there's is no need to lookup the DWO file.
2943 However, that's not the case for TUs where DW_AT_stmt_list lives in the
2944 DWO file. */
2945 if (this_cu->is_debug_types)
094b34ac 2946 {
796a7ff8 2947 struct type_unit_group *tu_group = this_cu->type_unit_group;
094b34ac
DE
2948
2949 init_cutu_and_read_dies (tu_group->t.first_tu, NULL, 0, 0,
2950 dw2_get_file_names_reader, tu_group);
2951 }
3019eac3
DE
2952 else
2953 init_cutu_and_read_dies_simple (this_cu, dw2_get_file_names_reader, NULL);
dee91e82
DE
2954
2955 if (this_cu->v.quick->no_file_data)
2956 return NULL;
2957 return this_cu->v.quick->file_names;
9291a0cd
TT
2958}
2959
2960/* A helper for the "quick" functions which computes and caches the
7b9f3c50 2961 real path for a given file name from the line table. */
2fdf6df6 2962
9291a0cd 2963static const char *
7b9f3c50
DE
2964dw2_get_real_path (struct objfile *objfile,
2965 struct quick_file_names *qfn, int index)
9291a0cd 2966{
7b9f3c50
DE
2967 if (qfn->real_names == NULL)
2968 qfn->real_names = OBSTACK_CALLOC (&objfile->objfile_obstack,
2969 qfn->num_file_names, sizeof (char *));
9291a0cd 2970
7b9f3c50
DE
2971 if (qfn->real_names[index] == NULL)
2972 qfn->real_names[index] = gdb_realpath (qfn->file_names[index]);
9291a0cd 2973
7b9f3c50 2974 return qfn->real_names[index];
9291a0cd
TT
2975}
2976
2977static struct symtab *
2978dw2_find_last_source_symtab (struct objfile *objfile)
2979{
2980 int index;
ae2de4f8 2981
9291a0cd
TT
2982 dw2_setup (objfile);
2983 index = dwarf2_per_objfile->n_comp_units - 1;
a0f42c21 2984 return dw2_instantiate_symtab (dw2_get_cu (index));
9291a0cd
TT
2985}
2986
7b9f3c50
DE
2987/* Traversal function for dw2_forget_cached_source_info. */
2988
2989static int
2990dw2_free_cached_file_names (void **slot, void *info)
9291a0cd 2991{
7b9f3c50 2992 struct quick_file_names *file_data = (struct quick_file_names *) *slot;
9291a0cd 2993
7b9f3c50 2994 if (file_data->real_names)
9291a0cd 2995 {
7b9f3c50 2996 int i;
9291a0cd 2997
7b9f3c50 2998 for (i = 0; i < file_data->num_file_names; ++i)
9291a0cd 2999 {
7b9f3c50
DE
3000 xfree ((void*) file_data->real_names[i]);
3001 file_data->real_names[i] = NULL;
9291a0cd
TT
3002 }
3003 }
7b9f3c50
DE
3004
3005 return 1;
3006}
3007
3008static void
3009dw2_forget_cached_source_info (struct objfile *objfile)
3010{
3011 dw2_setup (objfile);
3012
3013 htab_traverse_noresize (dwarf2_per_objfile->quick_file_names_table,
3014 dw2_free_cached_file_names, NULL);
9291a0cd
TT
3015}
3016
f8eba3c6
TT
3017/* Helper function for dw2_map_symtabs_matching_filename that expands
3018 the symtabs and calls the iterator. */
3019
3020static int
3021dw2_map_expand_apply (struct objfile *objfile,
3022 struct dwarf2_per_cu_data *per_cu,
3023 const char *name,
3024 const char *full_path, const char *real_path,
3025 int (*callback) (struct symtab *, void *),
3026 void *data)
3027{
3028 struct symtab *last_made = objfile->symtabs;
3029
3030 /* Don't visit already-expanded CUs. */
3031 if (per_cu->v.quick->symtab)
3032 return 0;
3033
3034 /* This may expand more than one symtab, and we want to iterate over
3035 all of them. */
a0f42c21 3036 dw2_instantiate_symtab (per_cu);
f8eba3c6
TT
3037
3038 return iterate_over_some_symtabs (name, full_path, real_path, callback, data,
3039 objfile->symtabs, last_made);
3040}
3041
3042/* Implementation of the map_symtabs_matching_filename method. */
3043
9291a0cd 3044static int
f8eba3c6
TT
3045dw2_map_symtabs_matching_filename (struct objfile *objfile, const char *name,
3046 const char *full_path, const char *real_path,
3047 int (*callback) (struct symtab *, void *),
3048 void *data)
9291a0cd
TT
3049{
3050 int i;
c011a4f4 3051 const char *name_basename = lbasename (name);
4aac40c8 3052 int is_abs = IS_ABSOLUTE_PATH (name);
9291a0cd
TT
3053
3054 dw2_setup (objfile);
ae2de4f8 3055
848e3e78
DE
3056 /* The rule is CUs specify all the files, including those used by
3057 any TU, so there's no need to scan TUs here. */
f4dc4d17 3058
848e3e78 3059 for (i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
9291a0cd
TT
3060 {
3061 int j;
f4dc4d17 3062 struct dwarf2_per_cu_data *per_cu = dw2_get_primary_cu (i);
7b9f3c50 3063 struct quick_file_names *file_data;
9291a0cd 3064
3d7bb9d9 3065 /* We only need to look at symtabs not already expanded. */
e254ef6a 3066 if (per_cu->v.quick->symtab)
9291a0cd
TT
3067 continue;
3068
7b9f3c50
DE
3069 file_data = dw2_get_file_names (objfile, per_cu);
3070 if (file_data == NULL)
9291a0cd
TT
3071 continue;
3072
7b9f3c50 3073 for (j = 0; j < file_data->num_file_names; ++j)
9291a0cd 3074 {
7b9f3c50 3075 const char *this_name = file_data->file_names[j];
9291a0cd 3076
4aac40c8 3077 if (FILENAME_CMP (name, this_name) == 0
b57a636e 3078 || (!is_abs && compare_filenames_for_search (this_name, name)))
9291a0cd 3079 {
f8eba3c6
TT
3080 if (dw2_map_expand_apply (objfile, per_cu,
3081 name, full_path, real_path,
3082 callback, data))
3083 return 1;
4aac40c8 3084 }
9291a0cd 3085
c011a4f4
DE
3086 /* Before we invoke realpath, which can get expensive when many
3087 files are involved, do a quick comparison of the basenames. */
3088 if (! basenames_may_differ
3089 && FILENAME_CMP (lbasename (this_name), name_basename) != 0)
3090 continue;
3091
9291a0cd
TT
3092 if (full_path != NULL)
3093 {
7b9f3c50
DE
3094 const char *this_real_name = dw2_get_real_path (objfile,
3095 file_data, j);
9291a0cd 3096
7b9f3c50 3097 if (this_real_name != NULL
4aac40c8
TT
3098 && (FILENAME_CMP (full_path, this_real_name) == 0
3099 || (!is_abs
3100 && compare_filenames_for_search (this_real_name,
b57a636e 3101 name))))
9291a0cd 3102 {
f8eba3c6
TT
3103 if (dw2_map_expand_apply (objfile, per_cu,
3104 name, full_path, real_path,
3105 callback, data))
3106 return 1;
9291a0cd
TT
3107 }
3108 }
3109
3110 if (real_path != NULL)
3111 {
7b9f3c50
DE
3112 const char *this_real_name = dw2_get_real_path (objfile,
3113 file_data, j);
9291a0cd 3114
7b9f3c50 3115 if (this_real_name != NULL
4aac40c8
TT
3116 && (FILENAME_CMP (real_path, this_real_name) == 0
3117 || (!is_abs
3118 && compare_filenames_for_search (this_real_name,
b57a636e 3119 name))))
9291a0cd 3120 {
f8eba3c6
TT
3121 if (dw2_map_expand_apply (objfile, per_cu,
3122 name, full_path, real_path,
3123 callback, data))
3124 return 1;
9291a0cd
TT
3125 }
3126 }
3127 }
3128 }
3129
9291a0cd
TT
3130 return 0;
3131}
3132
da51c347
DE
3133/* Struct used to manage iterating over all CUs looking for a symbol. */
3134
3135struct dw2_symtab_iterator
9291a0cd 3136{
da51c347
DE
3137 /* The internalized form of .gdb_index. */
3138 struct mapped_index *index;
3139 /* If non-zero, only look for symbols that match BLOCK_INDEX. */
3140 int want_specific_block;
3141 /* One of GLOBAL_BLOCK or STATIC_BLOCK.
3142 Unused if !WANT_SPECIFIC_BLOCK. */
3143 int block_index;
3144 /* The kind of symbol we're looking for. */
3145 domain_enum domain;
3146 /* The list of CUs from the index entry of the symbol,
3147 or NULL if not found. */
3148 offset_type *vec;
3149 /* The next element in VEC to look at. */
3150 int next;
3151 /* The number of elements in VEC, or zero if there is no match. */
3152 int length;
3153};
9291a0cd 3154
da51c347
DE
3155/* Initialize the index symtab iterator ITER.
3156 If WANT_SPECIFIC_BLOCK is non-zero, only look for symbols
3157 in block BLOCK_INDEX. Otherwise BLOCK_INDEX is ignored. */
2fdf6df6 3158
9291a0cd 3159static void
da51c347
DE
3160dw2_symtab_iter_init (struct dw2_symtab_iterator *iter,
3161 struct mapped_index *index,
3162 int want_specific_block,
3163 int block_index,
3164 domain_enum domain,
3165 const char *name)
3166{
3167 iter->index = index;
3168 iter->want_specific_block = want_specific_block;
3169 iter->block_index = block_index;
3170 iter->domain = domain;
3171 iter->next = 0;
3172
3173 if (find_slot_in_mapped_hash (index, name, &iter->vec))
3174 iter->length = MAYBE_SWAP (*iter->vec);
3175 else
3176 {
3177 iter->vec = NULL;
3178 iter->length = 0;
3179 }
3180}
3181
3182/* Return the next matching CU or NULL if there are no more. */
3183
3184static struct dwarf2_per_cu_data *
3185dw2_symtab_iter_next (struct dw2_symtab_iterator *iter)
3186{
3187 for ( ; iter->next < iter->length; ++iter->next)
3188 {
3189 offset_type cu_index_and_attrs =
3190 MAYBE_SWAP (iter->vec[iter->next + 1]);
3191 offset_type cu_index = GDB_INDEX_CU_VALUE (cu_index_and_attrs);
3192 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (cu_index);
3193 int want_static = iter->block_index != GLOBAL_BLOCK;
3194 /* This value is only valid for index versions >= 7. */
3195 int is_static = GDB_INDEX_SYMBOL_STATIC_VALUE (cu_index_and_attrs);
3196 gdb_index_symbol_kind symbol_kind =
3197 GDB_INDEX_SYMBOL_KIND_VALUE (cu_index_and_attrs);
3198 /* Only check the symbol attributes if they're present.
3199 Indices prior to version 7 don't record them,
3200 and indices >= 7 may elide them for certain symbols
3201 (gold does this). */
3202 int attrs_valid =
3203 (iter->index->version >= 7
3204 && symbol_kind != GDB_INDEX_SYMBOL_KIND_NONE);
3205
3206 /* Skip if already read in. */
3207 if (per_cu->v.quick->symtab)
3208 continue;
3209
3210 if (attrs_valid
3211 && iter->want_specific_block
3212 && want_static != is_static)
3213 continue;
3214
3215 /* Only check the symbol's kind if it has one. */
3216 if (attrs_valid)
3217 {
3218 switch (iter->domain)
3219 {
3220 case VAR_DOMAIN:
3221 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_VARIABLE
3222 && symbol_kind != GDB_INDEX_SYMBOL_KIND_FUNCTION
3223 /* Some types are also in VAR_DOMAIN. */
3224 && symbol_kind != GDB_INDEX_SYMBOL_KIND_TYPE)
3225 continue;
3226 break;
3227 case STRUCT_DOMAIN:
3228 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_TYPE)
3229 continue;
3230 break;
3231 case LABEL_DOMAIN:
3232 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_OTHER)
3233 continue;
3234 break;
3235 default:
3236 break;
3237 }
3238 }
3239
3240 ++iter->next;
3241 return per_cu;
3242 }
3243
3244 return NULL;
3245}
3246
3247static struct symtab *
3248dw2_lookup_symbol (struct objfile *objfile, int block_index,
3249 const char *name, domain_enum domain)
9291a0cd 3250{
da51c347 3251 struct symtab *stab_best = NULL;
156942c7
DE
3252 struct mapped_index *index;
3253
9291a0cd
TT
3254 dw2_setup (objfile);
3255
156942c7
DE
3256 index = dwarf2_per_objfile->index_table;
3257
da51c347 3258 /* index is NULL if OBJF_READNOW. */
156942c7 3259 if (index)
9291a0cd 3260 {
da51c347
DE
3261 struct dw2_symtab_iterator iter;
3262 struct dwarf2_per_cu_data *per_cu;
3263
3264 dw2_symtab_iter_init (&iter, index, 1, block_index, domain, name);
9291a0cd 3265
da51c347 3266 while ((per_cu = dw2_symtab_iter_next (&iter)) != NULL)
9291a0cd 3267 {
da51c347
DE
3268 struct symbol *sym = NULL;
3269 struct symtab *stab = dw2_instantiate_symtab (per_cu);
3270
3271 /* Some caution must be observed with overloaded functions
3272 and methods, since the index will not contain any overload
3273 information (but NAME might contain it). */
3274 if (stab->primary)
9291a0cd 3275 {
da51c347
DE
3276 struct blockvector *bv = BLOCKVECTOR (stab);
3277 struct block *block = BLOCKVECTOR_BLOCK (bv, block_index);
156942c7 3278
da51c347
DE
3279 sym = lookup_block_symbol (block, name, domain);
3280 }
1fd400ff 3281
da51c347
DE
3282 if (sym && strcmp_iw (SYMBOL_SEARCH_NAME (sym), name) == 0)
3283 {
3284 if (!TYPE_IS_OPAQUE (SYMBOL_TYPE (sym)))
3285 return stab;
3286
3287 stab_best = stab;
9291a0cd 3288 }
da51c347
DE
3289
3290 /* Keep looking through other CUs. */
9291a0cd
TT
3291 }
3292 }
9291a0cd 3293
da51c347 3294 return stab_best;
9291a0cd
TT
3295}
3296
3297static void
3298dw2_print_stats (struct objfile *objfile)
3299{
3300 int i, count;
3301
3302 dw2_setup (objfile);
3303 count = 0;
1fd400ff 3304 for (i = 0; i < (dwarf2_per_objfile->n_comp_units
d467dd73 3305 + dwarf2_per_objfile->n_type_units); ++i)
9291a0cd 3306 {
e254ef6a 3307 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
9291a0cd 3308
e254ef6a 3309 if (!per_cu->v.quick->symtab)
9291a0cd
TT
3310 ++count;
3311 }
3312 printf_filtered (_(" Number of unread CUs: %d\n"), count);
3313}
3314
3315static void
3316dw2_dump (struct objfile *objfile)
3317{
3318 /* Nothing worth printing. */
3319}
3320
3321static void
3322dw2_relocate (struct objfile *objfile, struct section_offsets *new_offsets,
3323 struct section_offsets *delta)
3324{
3325 /* There's nothing to relocate here. */
3326}
3327
3328static void
3329dw2_expand_symtabs_for_function (struct objfile *objfile,
3330 const char *func_name)
3331{
da51c347
DE
3332 struct mapped_index *index;
3333
3334 dw2_setup (objfile);
3335
3336 index = dwarf2_per_objfile->index_table;
3337
3338 /* index is NULL if OBJF_READNOW. */
3339 if (index)
3340 {
3341 struct dw2_symtab_iterator iter;
3342 struct dwarf2_per_cu_data *per_cu;
3343
3344 /* Note: It doesn't matter what we pass for block_index here. */
3345 dw2_symtab_iter_init (&iter, index, 0, GLOBAL_BLOCK, VAR_DOMAIN,
3346 func_name);
3347
3348 while ((per_cu = dw2_symtab_iter_next (&iter)) != NULL)
3349 dw2_instantiate_symtab (per_cu);
3350 }
9291a0cd
TT
3351}
3352
3353static void
3354dw2_expand_all_symtabs (struct objfile *objfile)
3355{
3356 int i;
3357
3358 dw2_setup (objfile);
1fd400ff
TT
3359
3360 for (i = 0; i < (dwarf2_per_objfile->n_comp_units
d467dd73 3361 + dwarf2_per_objfile->n_type_units); ++i)
9291a0cd 3362 {
e254ef6a 3363 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
9291a0cd 3364
a0f42c21 3365 dw2_instantiate_symtab (per_cu);
9291a0cd
TT
3366 }
3367}
3368
3369static void
3370dw2_expand_symtabs_with_filename (struct objfile *objfile,
3371 const char *filename)
3372{
3373 int i;
3374
3375 dw2_setup (objfile);
d4637a04
DE
3376
3377 /* We don't need to consider type units here.
3378 This is only called for examining code, e.g. expand_line_sal.
3379 There can be an order of magnitude (or more) more type units
3380 than comp units, and we avoid them if we can. */
3381
3382 for (i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
9291a0cd
TT
3383 {
3384 int j;
e254ef6a 3385 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
7b9f3c50 3386 struct quick_file_names *file_data;
9291a0cd 3387
3d7bb9d9 3388 /* We only need to look at symtabs not already expanded. */
e254ef6a 3389 if (per_cu->v.quick->symtab)
9291a0cd
TT
3390 continue;
3391
7b9f3c50
DE
3392 file_data = dw2_get_file_names (objfile, per_cu);
3393 if (file_data == NULL)
9291a0cd
TT
3394 continue;
3395
7b9f3c50 3396 for (j = 0; j < file_data->num_file_names; ++j)
9291a0cd 3397 {
7b9f3c50 3398 const char *this_name = file_data->file_names[j];
1ef75ecc 3399 if (FILENAME_CMP (this_name, filename) == 0)
9291a0cd 3400 {
a0f42c21 3401 dw2_instantiate_symtab (per_cu);
9291a0cd
TT
3402 break;
3403 }
3404 }
3405 }
3406}
3407
356d9f9d
TT
3408/* A helper function for dw2_find_symbol_file that finds the primary
3409 file name for a given CU. This is a die_reader_func. */
3410
3411static void
3412dw2_get_primary_filename_reader (const struct die_reader_specs *reader,
3413 gdb_byte *info_ptr,
3414 struct die_info *comp_unit_die,
3415 int has_children,
3416 void *data)
3417{
3418 const char **result_ptr = data;
3419 struct dwarf2_cu *cu = reader->cu;
3420 struct attribute *attr;
3421
3422 attr = dwarf2_attr (comp_unit_die, DW_AT_name, cu);
3423 if (attr == NULL)
3424 *result_ptr = NULL;
3425 else
3426 *result_ptr = DW_STRING (attr);
3427}
3428
dd786858 3429static const char *
9291a0cd
TT
3430dw2_find_symbol_file (struct objfile *objfile, const char *name)
3431{
e254ef6a 3432 struct dwarf2_per_cu_data *per_cu;
9291a0cd 3433 offset_type *vec;
356d9f9d 3434 const char *filename;
9291a0cd
TT
3435
3436 dw2_setup (objfile);
3437
ae2de4f8 3438 /* index_table is NULL if OBJF_READNOW. */
9291a0cd 3439 if (!dwarf2_per_objfile->index_table)
96408a79
SA
3440 {
3441 struct symtab *s;
3442
d790cf0a
DE
3443 ALL_OBJFILE_PRIMARY_SYMTABS (objfile, s)
3444 {
3445 struct blockvector *bv = BLOCKVECTOR (s);
3446 const struct block *block = BLOCKVECTOR_BLOCK (bv, GLOBAL_BLOCK);
3447 struct symbol *sym = lookup_block_symbol (block, name, VAR_DOMAIN);
3448
3449 if (sym)
210bbc17 3450 return SYMBOL_SYMTAB (sym)->filename;
d790cf0a 3451 }
96408a79
SA
3452 return NULL;
3453 }
9291a0cd
TT
3454
3455 if (!find_slot_in_mapped_hash (dwarf2_per_objfile->index_table,
3456 name, &vec))
3457 return NULL;
3458
3459 /* Note that this just looks at the very first one named NAME -- but
3460 actually we are looking for a function. find_main_filename
3461 should be rewritten so that it doesn't require a custom hook. It
3462 could just use the ordinary symbol tables. */
3463 /* vec[0] is the length, which must always be >0. */
156942c7 3464 per_cu = dw2_get_cu (GDB_INDEX_CU_VALUE (MAYBE_SWAP (vec[1])));
9291a0cd 3465
356d9f9d
TT
3466 if (per_cu->v.quick->symtab != NULL)
3467 return per_cu->v.quick->symtab->filename;
3468
f4dc4d17
DE
3469 init_cutu_and_read_dies (per_cu, NULL, 0, 0,
3470 dw2_get_primary_filename_reader, &filename);
9291a0cd 3471
356d9f9d 3472 return filename;
9291a0cd
TT
3473}
3474
3475static void
40658b94
PH
3476dw2_map_matching_symbols (const char * name, domain_enum namespace,
3477 struct objfile *objfile, int global,
3478 int (*callback) (struct block *,
3479 struct symbol *, void *),
2edb89d3
JK
3480 void *data, symbol_compare_ftype *match,
3481 symbol_compare_ftype *ordered_compare)
9291a0cd 3482{
40658b94 3483 /* Currently unimplemented; used for Ada. The function can be called if the
a9e6a4bb
JK
3484 current language is Ada for a non-Ada objfile using GNU index. As Ada
3485 does not look for non-Ada symbols this function should just return. */
9291a0cd
TT
3486}
3487
3488static void
f8eba3c6
TT
3489dw2_expand_symtabs_matching
3490 (struct objfile *objfile,
3491 int (*file_matcher) (const char *, void *),
e078317b 3492 int (*name_matcher) (const char *, void *),
f8eba3c6
TT
3493 enum search_domain kind,
3494 void *data)
9291a0cd
TT
3495{
3496 int i;
3497 offset_type iter;
4b5246aa 3498 struct mapped_index *index;
9291a0cd
TT
3499
3500 dw2_setup (objfile);
ae2de4f8
DE
3501
3502 /* index_table is NULL if OBJF_READNOW. */
9291a0cd
TT
3503 if (!dwarf2_per_objfile->index_table)
3504 return;
4b5246aa 3505 index = dwarf2_per_objfile->index_table;
9291a0cd 3506
7b08b9eb 3507 if (file_matcher != NULL)
24c79950
TT
3508 {
3509 struct cleanup *cleanup;
3510 htab_t visited_found, visited_not_found;
3511
3512 visited_found = htab_create_alloc (10,
3513 htab_hash_pointer, htab_eq_pointer,
3514 NULL, xcalloc, xfree);
3515 cleanup = make_cleanup_htab_delete (visited_found);
3516 visited_not_found = htab_create_alloc (10,
3517 htab_hash_pointer, htab_eq_pointer,
3518 NULL, xcalloc, xfree);
3519 make_cleanup_htab_delete (visited_not_found);
3520
848e3e78
DE
3521 /* The rule is CUs specify all the files, including those used by
3522 any TU, so there's no need to scan TUs here. */
3523
3524 for (i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
24c79950
TT
3525 {
3526 int j;
f4dc4d17 3527 struct dwarf2_per_cu_data *per_cu = dw2_get_primary_cu (i);
24c79950
TT
3528 struct quick_file_names *file_data;
3529 void **slot;
7b08b9eb 3530
24c79950 3531 per_cu->v.quick->mark = 0;
3d7bb9d9 3532
24c79950
TT
3533 /* We only need to look at symtabs not already expanded. */
3534 if (per_cu->v.quick->symtab)
3535 continue;
7b08b9eb 3536
24c79950
TT
3537 file_data = dw2_get_file_names (objfile, per_cu);
3538 if (file_data == NULL)
3539 continue;
7b08b9eb 3540
24c79950
TT
3541 if (htab_find (visited_not_found, file_data) != NULL)
3542 continue;
3543 else if (htab_find (visited_found, file_data) != NULL)
3544 {
3545 per_cu->v.quick->mark = 1;
3546 continue;
3547 }
3548
3549 for (j = 0; j < file_data->num_file_names; ++j)
3550 {
3551 if (file_matcher (file_data->file_names[j], data))
3552 {
3553 per_cu->v.quick->mark = 1;
3554 break;
3555 }
3556 }
3557
3558 slot = htab_find_slot (per_cu->v.quick->mark
3559 ? visited_found
3560 : visited_not_found,
3561 file_data, INSERT);
3562 *slot = file_data;
3563 }
3564
3565 do_cleanups (cleanup);
3566 }
9291a0cd 3567
3876f04e 3568 for (iter = 0; iter < index->symbol_table_slots; ++iter)
9291a0cd
TT
3569 {
3570 offset_type idx = 2 * iter;
3571 const char *name;
3572 offset_type *vec, vec_len, vec_idx;
3573
3876f04e 3574 if (index->symbol_table[idx] == 0 && index->symbol_table[idx + 1] == 0)
9291a0cd
TT
3575 continue;
3576
3876f04e 3577 name = index->constant_pool + MAYBE_SWAP (index->symbol_table[idx]);
9291a0cd 3578
e078317b 3579 if (! (*name_matcher) (name, data))
9291a0cd
TT
3580 continue;
3581
3582 /* The name was matched, now expand corresponding CUs that were
3583 marked. */
4b5246aa 3584 vec = (offset_type *) (index->constant_pool
3876f04e 3585 + MAYBE_SWAP (index->symbol_table[idx + 1]));
9291a0cd
TT
3586 vec_len = MAYBE_SWAP (vec[0]);
3587 for (vec_idx = 0; vec_idx < vec_len; ++vec_idx)
3588 {
e254ef6a 3589 struct dwarf2_per_cu_data *per_cu;
156942c7
DE
3590 offset_type cu_index_and_attrs = MAYBE_SWAP (vec[vec_idx + 1]);
3591 gdb_index_symbol_kind symbol_kind =
3592 GDB_INDEX_SYMBOL_KIND_VALUE (cu_index_and_attrs);
3593 int cu_index = GDB_INDEX_CU_VALUE (cu_index_and_attrs);
3594
3595 /* Don't crash on bad data. */
3596 if (cu_index >= (dwarf2_per_objfile->n_comp_units
667e0a4b 3597 + dwarf2_per_objfile->n_type_units))
156942c7 3598 continue;
1fd400ff 3599
156942c7
DE
3600 /* Only check the symbol's kind if it has one.
3601 Indices prior to version 7 don't record it. */
3602 if (index->version >= 7)
3603 {
3604 switch (kind)
3605 {
3606 case VARIABLES_DOMAIN:
3607 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_VARIABLE)
3608 continue;
3609 break;
3610 case FUNCTIONS_DOMAIN:
3611 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_FUNCTION)
3612 continue;
3613 break;
3614 case TYPES_DOMAIN:
3615 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_TYPE)
3616 continue;
3617 break;
3618 default:
3619 break;
3620 }
3621 }
3622
3623 per_cu = dw2_get_cu (cu_index);
7b08b9eb 3624 if (file_matcher == NULL || per_cu->v.quick->mark)
a0f42c21 3625 dw2_instantiate_symtab (per_cu);
9291a0cd
TT
3626 }
3627 }
3628}
3629
9703b513
TT
3630/* A helper for dw2_find_pc_sect_symtab which finds the most specific
3631 symtab. */
3632
3633static struct symtab *
3634recursively_find_pc_sect_symtab (struct symtab *symtab, CORE_ADDR pc)
3635{
3636 int i;
3637
3638 if (BLOCKVECTOR (symtab) != NULL
3639 && blockvector_contains_pc (BLOCKVECTOR (symtab), pc))
3640 return symtab;
3641
a3ec0bb1
DE
3642 if (symtab->includes == NULL)
3643 return NULL;
3644
9703b513
TT
3645 for (i = 0; symtab->includes[i]; ++i)
3646 {
a3ec0bb1 3647 struct symtab *s = symtab->includes[i];
9703b513
TT
3648
3649 s = recursively_find_pc_sect_symtab (s, pc);
3650 if (s != NULL)
3651 return s;
3652 }
3653
3654 return NULL;
3655}
3656
9291a0cd
TT
3657static struct symtab *
3658dw2_find_pc_sect_symtab (struct objfile *objfile,
3659 struct minimal_symbol *msymbol,
3660 CORE_ADDR pc,
3661 struct obj_section *section,
3662 int warn_if_readin)
3663{
3664 struct dwarf2_per_cu_data *data;
9703b513 3665 struct symtab *result;
9291a0cd
TT
3666
3667 dw2_setup (objfile);
3668
3669 if (!objfile->psymtabs_addrmap)
3670 return NULL;
3671
3672 data = addrmap_find (objfile->psymtabs_addrmap, pc);
3673 if (!data)
3674 return NULL;
3675
3676 if (warn_if_readin && data->v.quick->symtab)
abebb8b0 3677 warning (_("(Internal error: pc %s in read in CU, but not in symtab.)"),
9291a0cd
TT
3678 paddress (get_objfile_arch (objfile), pc));
3679
9703b513
TT
3680 result = recursively_find_pc_sect_symtab (dw2_instantiate_symtab (data), pc);
3681 gdb_assert (result != NULL);
3682 return result;
9291a0cd
TT
3683}
3684
9291a0cd 3685static void
44b13c5a 3686dw2_map_symbol_filenames (struct objfile *objfile, symbol_filename_ftype *fun,
74e2f255 3687 void *data, int need_fullname)
9291a0cd
TT
3688{
3689 int i;
24c79950
TT
3690 struct cleanup *cleanup;
3691 htab_t visited = htab_create_alloc (10, htab_hash_pointer, htab_eq_pointer,
3692 NULL, xcalloc, xfree);
9291a0cd 3693
24c79950 3694 cleanup = make_cleanup_htab_delete (visited);
9291a0cd 3695 dw2_setup (objfile);
ae2de4f8 3696
848e3e78
DE
3697 /* The rule is CUs specify all the files, including those used by
3698 any TU, so there's no need to scan TUs here.
3699 We can ignore file names coming from already-expanded CUs. */
f4dc4d17 3700
848e3e78 3701 for (i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
24c79950
TT
3702 {
3703 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
3704
3705 if (per_cu->v.quick->symtab)
3706 {
3707 void **slot = htab_find_slot (visited, per_cu->v.quick->file_names,
3708 INSERT);
3709
3710 *slot = per_cu->v.quick->file_names;
3711 }
3712 }
3713
848e3e78 3714 for (i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
9291a0cd
TT
3715 {
3716 int j;
f4dc4d17 3717 struct dwarf2_per_cu_data *per_cu = dw2_get_primary_cu (i);
7b9f3c50 3718 struct quick_file_names *file_data;
24c79950 3719 void **slot;
9291a0cd 3720
3d7bb9d9 3721 /* We only need to look at symtabs not already expanded. */
e254ef6a 3722 if (per_cu->v.quick->symtab)
9291a0cd
TT
3723 continue;
3724
7b9f3c50
DE
3725 file_data = dw2_get_file_names (objfile, per_cu);
3726 if (file_data == NULL)
9291a0cd
TT
3727 continue;
3728
24c79950
TT
3729 slot = htab_find_slot (visited, file_data, INSERT);
3730 if (*slot)
3731 {
3732 /* Already visited. */
3733 continue;
3734 }
3735 *slot = file_data;
3736
7b9f3c50 3737 for (j = 0; j < file_data->num_file_names; ++j)
9291a0cd 3738 {
74e2f255
DE
3739 const char *this_real_name;
3740
3741 if (need_fullname)
3742 this_real_name = dw2_get_real_path (objfile, file_data, j);
3743 else
3744 this_real_name = NULL;
7b9f3c50 3745 (*fun) (file_data->file_names[j], this_real_name, data);
9291a0cd
TT
3746 }
3747 }
24c79950
TT
3748
3749 do_cleanups (cleanup);
9291a0cd
TT
3750}
3751
3752static int
3753dw2_has_symbols (struct objfile *objfile)
3754{
3755 return 1;
3756}
3757
3758const struct quick_symbol_functions dwarf2_gdb_index_functions =
3759{
3760 dw2_has_symbols,
3761 dw2_find_last_source_symtab,
3762 dw2_forget_cached_source_info,
f8eba3c6 3763 dw2_map_symtabs_matching_filename,
9291a0cd 3764 dw2_lookup_symbol,
9291a0cd
TT
3765 dw2_print_stats,
3766 dw2_dump,
3767 dw2_relocate,
3768 dw2_expand_symtabs_for_function,
3769 dw2_expand_all_symtabs,
3770 dw2_expand_symtabs_with_filename,
3771 dw2_find_symbol_file,
40658b94 3772 dw2_map_matching_symbols,
9291a0cd
TT
3773 dw2_expand_symtabs_matching,
3774 dw2_find_pc_sect_symtab,
9291a0cd
TT
3775 dw2_map_symbol_filenames
3776};
3777
3778/* Initialize for reading DWARF for this objfile. Return 0 if this
3779 file will use psymtabs, or 1 if using the GNU index. */
3780
3781int
3782dwarf2_initialize_objfile (struct objfile *objfile)
3783{
3784 /* If we're about to read full symbols, don't bother with the
3785 indices. In this case we also don't care if some other debug
3786 format is making psymtabs, because they are all about to be
3787 expanded anyway. */
3788 if ((objfile->flags & OBJF_READNOW))
3789 {
3790 int i;
3791
3792 dwarf2_per_objfile->using_index = 1;
3793 create_all_comp_units (objfile);
0e50663e 3794 create_all_type_units (objfile);
7b9f3c50
DE
3795 dwarf2_per_objfile->quick_file_names_table =
3796 create_quick_file_names_table (dwarf2_per_objfile->n_comp_units);
9291a0cd 3797
1fd400ff 3798 for (i = 0; i < (dwarf2_per_objfile->n_comp_units
d467dd73 3799 + dwarf2_per_objfile->n_type_units); ++i)
9291a0cd 3800 {
e254ef6a 3801 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
9291a0cd 3802
e254ef6a
DE
3803 per_cu->v.quick = OBSTACK_ZALLOC (&objfile->objfile_obstack,
3804 struct dwarf2_per_cu_quick_data);
9291a0cd
TT
3805 }
3806
3807 /* Return 1 so that gdb sees the "quick" functions. However,
3808 these functions will be no-ops because we will have expanded
3809 all symtabs. */
3810 return 1;
3811 }
3812
3813 if (dwarf2_read_index (objfile))
3814 return 1;
3815
9291a0cd
TT
3816 return 0;
3817}
3818
3819\f
3820
dce234bc
PP
3821/* Build a partial symbol table. */
3822
3823void
f29dff0a 3824dwarf2_build_psymtabs (struct objfile *objfile)
dce234bc 3825{
c9bf0622
TT
3826 volatile struct gdb_exception except;
3827
f29dff0a 3828 if (objfile->global_psymbols.size == 0 && objfile->static_psymbols.size == 0)
c906108c
SS
3829 {
3830 init_psymbol_list (objfile, 1024);
3831 }
3832
c9bf0622
TT
3833 TRY_CATCH (except, RETURN_MASK_ERROR)
3834 {
3835 /* This isn't really ideal: all the data we allocate on the
3836 objfile's obstack is still uselessly kept around. However,
3837 freeing it seems unsafe. */
3838 struct cleanup *cleanups = make_cleanup_discard_psymtabs (objfile);
3839
3840 dwarf2_build_psymtabs_hard (objfile);
3841 discard_cleanups (cleanups);
3842 }
3843 if (except.reason < 0)
3844 exception_print (gdb_stderr, except);
c906108c 3845}
c906108c 3846
1ce1cefd
DE
3847/* Return the total length of the CU described by HEADER. */
3848
3849static unsigned int
3850get_cu_length (const struct comp_unit_head *header)
3851{
3852 return header->initial_length_size + header->length;
3853}
3854
45452591
DE
3855/* Return TRUE if OFFSET is within CU_HEADER. */
3856
3857static inline int
b64f50a1 3858offset_in_cu_p (const struct comp_unit_head *cu_header, sect_offset offset)
45452591 3859{
b64f50a1 3860 sect_offset bottom = { cu_header->offset.sect_off };
1ce1cefd 3861 sect_offset top = { cu_header->offset.sect_off + get_cu_length (cu_header) };
9a619af0 3862
b64f50a1 3863 return (offset.sect_off >= bottom.sect_off && offset.sect_off < top.sect_off);
45452591
DE
3864}
3865
3b80fe9b
DE
3866/* Find the base address of the compilation unit for range lists and
3867 location lists. It will normally be specified by DW_AT_low_pc.
3868 In DWARF-3 draft 4, the base address could be overridden by
3869 DW_AT_entry_pc. It's been removed, but GCC still uses this for
3870 compilation units with discontinuous ranges. */
3871
3872static void
3873dwarf2_find_base_address (struct die_info *die, struct dwarf2_cu *cu)
3874{
3875 struct attribute *attr;
3876
3877 cu->base_known = 0;
3878 cu->base_address = 0;
3879
3880 attr = dwarf2_attr (die, DW_AT_entry_pc, cu);
3881 if (attr)
3882 {
3883 cu->base_address = DW_ADDR (attr);
3884 cu->base_known = 1;
3885 }
3886 else
3887 {
3888 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
3889 if (attr)
3890 {
3891 cu->base_address = DW_ADDR (attr);
3892 cu->base_known = 1;
3893 }
3894 }
3895}
3896
93311388
DE
3897/* Read in the comp unit header information from the debug_info at info_ptr.
3898 NOTE: This leaves members offset, first_die_offset to be filled in
3899 by the caller. */
107d2387 3900
fe1b8b76 3901static gdb_byte *
107d2387 3902read_comp_unit_head (struct comp_unit_head *cu_header,
fe1b8b76 3903 gdb_byte *info_ptr, bfd *abfd)
107d2387
AC
3904{
3905 int signed_addr;
891d2f0b 3906 unsigned int bytes_read;
c764a876
DE
3907
3908 cu_header->length = read_initial_length (abfd, info_ptr, &bytes_read);
3909 cu_header->initial_length_size = bytes_read;
3910 cu_header->offset_size = (bytes_read == 4) ? 4 : 8;
613e1657 3911 info_ptr += bytes_read;
107d2387
AC
3912 cu_header->version = read_2_bytes (abfd, info_ptr);
3913 info_ptr += 2;
b64f50a1
JK
3914 cu_header->abbrev_offset.sect_off = read_offset (abfd, info_ptr, cu_header,
3915 &bytes_read);
613e1657 3916 info_ptr += bytes_read;
107d2387
AC
3917 cu_header->addr_size = read_1_byte (abfd, info_ptr);
3918 info_ptr += 1;
3919 signed_addr = bfd_get_sign_extend_vma (abfd);
3920 if (signed_addr < 0)
8e65ff28 3921 internal_error (__FILE__, __LINE__,
e2e0b3e5 3922 _("read_comp_unit_head: dwarf from non elf file"));
107d2387 3923 cu_header->signed_addr_p = signed_addr;
c764a876 3924
107d2387
AC
3925 return info_ptr;
3926}
3927
36586728
TT
3928/* Helper function that returns the proper abbrev section for
3929 THIS_CU. */
3930
3931static struct dwarf2_section_info *
3932get_abbrev_section_for_cu (struct dwarf2_per_cu_data *this_cu)
3933{
3934 struct dwarf2_section_info *abbrev;
3935
3936 if (this_cu->is_dwz)
3937 abbrev = &dwarf2_get_dwz_file ()->abbrev;
3938 else
3939 abbrev = &dwarf2_per_objfile->abbrev;
3940
3941 return abbrev;
3942}
3943
9ff913ba
DE
3944/* Subroutine of read_and_check_comp_unit_head and
3945 read_and_check_type_unit_head to simplify them.
3946 Perform various error checking on the header. */
3947
3948static void
3949error_check_comp_unit_head (struct comp_unit_head *header,
4bdcc0c1
DE
3950 struct dwarf2_section_info *section,
3951 struct dwarf2_section_info *abbrev_section)
9ff913ba
DE
3952{
3953 bfd *abfd = section->asection->owner;
3954 const char *filename = bfd_get_filename (abfd);
3955
3956 if (header->version != 2 && header->version != 3 && header->version != 4)
3957 error (_("Dwarf Error: wrong version in compilation unit header "
3958 "(is %d, should be 2, 3, or 4) [in module %s]"), header->version,
3959 filename);
3960
b64f50a1 3961 if (header->abbrev_offset.sect_off
36586728 3962 >= dwarf2_section_size (dwarf2_per_objfile->objfile, abbrev_section))
9ff913ba
DE
3963 error (_("Dwarf Error: bad offset (0x%lx) in compilation unit header "
3964 "(offset 0x%lx + 6) [in module %s]"),
b64f50a1 3965 (long) header->abbrev_offset.sect_off, (long) header->offset.sect_off,
9ff913ba
DE
3966 filename);
3967
3968 /* Cast to unsigned long to use 64-bit arithmetic when possible to
3969 avoid potential 32-bit overflow. */
1ce1cefd 3970 if (((unsigned long) header->offset.sect_off + get_cu_length (header))
9ff913ba
DE
3971 > section->size)
3972 error (_("Dwarf Error: bad length (0x%lx) in compilation unit header "
3973 "(offset 0x%lx + 0) [in module %s]"),
b64f50a1 3974 (long) header->length, (long) header->offset.sect_off,
9ff913ba
DE
3975 filename);
3976}
3977
3978/* Read in a CU/TU header and perform some basic error checking.
3979 The contents of the header are stored in HEADER.
3980 The result is a pointer to the start of the first DIE. */
adabb602 3981
fe1b8b76 3982static gdb_byte *
9ff913ba
DE
3983read_and_check_comp_unit_head (struct comp_unit_head *header,
3984 struct dwarf2_section_info *section,
4bdcc0c1 3985 struct dwarf2_section_info *abbrev_section,
9ff913ba
DE
3986 gdb_byte *info_ptr,
3987 int is_debug_types_section)
72bf9492 3988{
fe1b8b76 3989 gdb_byte *beg_of_comp_unit = info_ptr;
9ff913ba 3990 bfd *abfd = section->asection->owner;
72bf9492 3991
b64f50a1 3992 header->offset.sect_off = beg_of_comp_unit - section->buffer;
adabb602 3993
72bf9492
DJ
3994 info_ptr = read_comp_unit_head (header, info_ptr, abfd);
3995
460c1c54
CC
3996 /* If we're reading a type unit, skip over the signature and
3997 type_offset fields. */
b0df02fd 3998 if (is_debug_types_section)
460c1c54
CC
3999 info_ptr += 8 /*signature*/ + header->offset_size;
4000
b64f50a1 4001 header->first_die_offset.cu_off = info_ptr - beg_of_comp_unit;
adabb602 4002
4bdcc0c1 4003 error_check_comp_unit_head (header, section, abbrev_section);
72bf9492
DJ
4004
4005 return info_ptr;
4006}
4007
348e048f
DE
4008/* Read in the types comp unit header information from .debug_types entry at
4009 types_ptr. The result is a pointer to one past the end of the header. */
4010
4011static gdb_byte *
9ff913ba
DE
4012read_and_check_type_unit_head (struct comp_unit_head *header,
4013 struct dwarf2_section_info *section,
4bdcc0c1 4014 struct dwarf2_section_info *abbrev_section,
9ff913ba 4015 gdb_byte *info_ptr,
dee91e82
DE
4016 ULONGEST *signature,
4017 cu_offset *type_offset_in_tu)
348e048f 4018{
9ff913ba
DE
4019 gdb_byte *beg_of_comp_unit = info_ptr;
4020 bfd *abfd = section->asection->owner;
348e048f 4021
b64f50a1 4022 header->offset.sect_off = beg_of_comp_unit - section->buffer;
348e048f 4023
9ff913ba 4024 info_ptr = read_comp_unit_head (header, info_ptr, abfd);
348e048f 4025
9ff913ba
DE
4026 /* If we're reading a type unit, skip over the signature and
4027 type_offset fields. */
4028 if (signature != NULL)
4029 *signature = read_8_bytes (abfd, info_ptr);
4030 info_ptr += 8;
dee91e82
DE
4031 if (type_offset_in_tu != NULL)
4032 type_offset_in_tu->cu_off = read_offset_1 (abfd, info_ptr,
4033 header->offset_size);
9ff913ba
DE
4034 info_ptr += header->offset_size;
4035
b64f50a1 4036 header->first_die_offset.cu_off = info_ptr - beg_of_comp_unit;
348e048f 4037
4bdcc0c1 4038 error_check_comp_unit_head (header, section, abbrev_section);
9ff913ba
DE
4039
4040 return info_ptr;
348e048f
DE
4041}
4042
f4dc4d17
DE
4043/* Fetch the abbreviation table offset from a comp or type unit header. */
4044
4045static sect_offset
4046read_abbrev_offset (struct dwarf2_section_info *section,
4047 sect_offset offset)
4048{
4049 bfd *abfd = section->asection->owner;
4050 gdb_byte *info_ptr;
4051 unsigned int length, initial_length_size, offset_size;
4052 sect_offset abbrev_offset;
4053
4054 dwarf2_read_section (dwarf2_per_objfile->objfile, section);
4055 info_ptr = section->buffer + offset.sect_off;
4056 length = read_initial_length (abfd, info_ptr, &initial_length_size);
4057 offset_size = initial_length_size == 4 ? 4 : 8;
4058 info_ptr += initial_length_size + 2 /*version*/;
4059 abbrev_offset.sect_off = read_offset_1 (abfd, info_ptr, offset_size);
4060 return abbrev_offset;
4061}
4062
aaa75496
JB
4063/* Allocate a new partial symtab for file named NAME and mark this new
4064 partial symtab as being an include of PST. */
4065
4066static void
4067dwarf2_create_include_psymtab (char *name, struct partial_symtab *pst,
4068 struct objfile *objfile)
4069{
4070 struct partial_symtab *subpst = allocate_psymtab (name, objfile);
4071
4072 subpst->section_offsets = pst->section_offsets;
4073 subpst->textlow = 0;
4074 subpst->texthigh = 0;
4075
4076 subpst->dependencies = (struct partial_symtab **)
4077 obstack_alloc (&objfile->objfile_obstack,
4078 sizeof (struct partial_symtab *));
4079 subpst->dependencies[0] = pst;
4080 subpst->number_of_dependencies = 1;
4081
4082 subpst->globals_offset = 0;
4083 subpst->n_global_syms = 0;
4084 subpst->statics_offset = 0;
4085 subpst->n_static_syms = 0;
4086 subpst->symtab = NULL;
4087 subpst->read_symtab = pst->read_symtab;
4088 subpst->readin = 0;
4089
4090 /* No private part is necessary for include psymtabs. This property
4091 can be used to differentiate between such include psymtabs and
10b3939b 4092 the regular ones. */
58a9656e 4093 subpst->read_symtab_private = NULL;
aaa75496
JB
4094}
4095
4096/* Read the Line Number Program data and extract the list of files
4097 included by the source file represented by PST. Build an include
d85a05f0 4098 partial symtab for each of these included files. */
aaa75496
JB
4099
4100static void
4101dwarf2_build_include_psymtabs (struct dwarf2_cu *cu,
dee91e82
DE
4102 struct die_info *die,
4103 struct partial_symtab *pst)
aaa75496 4104{
d85a05f0
DJ
4105 struct line_header *lh = NULL;
4106 struct attribute *attr;
aaa75496 4107
d85a05f0
DJ
4108 attr = dwarf2_attr (die, DW_AT_stmt_list, cu);
4109 if (attr)
3019eac3 4110 lh = dwarf_decode_line_header (DW_UNSND (attr), cu);
aaa75496
JB
4111 if (lh == NULL)
4112 return; /* No linetable, so no includes. */
4113
c6da4cef 4114 /* NOTE: pst->dirname is DW_AT_comp_dir (if present). */
f3f5162e 4115 dwarf_decode_lines (lh, pst->dirname, cu, pst, 1);
aaa75496
JB
4116
4117 free_line_header (lh);
4118}
4119
348e048f 4120static hashval_t
52dc124a 4121hash_signatured_type (const void *item)
348e048f 4122{
52dc124a 4123 const struct signatured_type *sig_type = item;
9a619af0 4124
348e048f 4125 /* This drops the top 32 bits of the signature, but is ok for a hash. */
52dc124a 4126 return sig_type->signature;
348e048f
DE
4127}
4128
4129static int
52dc124a 4130eq_signatured_type (const void *item_lhs, const void *item_rhs)
348e048f
DE
4131{
4132 const struct signatured_type *lhs = item_lhs;
4133 const struct signatured_type *rhs = item_rhs;
9a619af0 4134
348e048f
DE
4135 return lhs->signature == rhs->signature;
4136}
4137
1fd400ff
TT
4138/* Allocate a hash table for signatured types. */
4139
4140static htab_t
673bfd45 4141allocate_signatured_type_table (struct objfile *objfile)
1fd400ff
TT
4142{
4143 return htab_create_alloc_ex (41,
52dc124a
DE
4144 hash_signatured_type,
4145 eq_signatured_type,
1fd400ff
TT
4146 NULL,
4147 &objfile->objfile_obstack,
4148 hashtab_obstack_allocate,
4149 dummy_obstack_deallocate);
4150}
4151
d467dd73 4152/* A helper function to add a signatured type CU to a table. */
1fd400ff
TT
4153
4154static int
d467dd73 4155add_signatured_type_cu_to_table (void **slot, void *datum)
1fd400ff
TT
4156{
4157 struct signatured_type *sigt = *slot;
b4dd5633 4158 struct signatured_type ***datap = datum;
1fd400ff 4159
b4dd5633 4160 **datap = sigt;
1fd400ff
TT
4161 ++*datap;
4162
4163 return 1;
4164}
4165
3019eac3 4166/* Create the hash table of all entries in the .debug_types section.
80626a55
DE
4167 DWO_FILE is a pointer to the DWO file for .debug_types.dwo,
4168 NULL otherwise.
4169 Note: This function processes DWO files only, not DWP files.
3019eac3
DE
4170 The result is a pointer to the hash table or NULL if there are
4171 no types. */
348e048f 4172
3019eac3
DE
4173static htab_t
4174create_debug_types_hash_table (struct dwo_file *dwo_file,
4175 VEC (dwarf2_section_info_def) *types)
348e048f 4176{
3019eac3 4177 struct objfile *objfile = dwarf2_per_objfile->objfile;
8b70b953 4178 htab_t types_htab = NULL;
8b70b953
TT
4179 int ix;
4180 struct dwarf2_section_info *section;
4bdcc0c1 4181 struct dwarf2_section_info *abbrev_section;
348e048f 4182
3019eac3
DE
4183 if (VEC_empty (dwarf2_section_info_def, types))
4184 return NULL;
348e048f 4185
4bdcc0c1
DE
4186 abbrev_section = (dwo_file != NULL
4187 ? &dwo_file->sections.abbrev
4188 : &dwarf2_per_objfile->abbrev);
4189
09406207
DE
4190 if (dwarf2_read_debug)
4191 fprintf_unfiltered (gdb_stdlog, "Reading .debug_types%s for %s:\n",
4192 dwo_file ? ".dwo" : "",
4193 bfd_get_filename (abbrev_section->asection->owner));
4194
8b70b953 4195 for (ix = 0;
3019eac3 4196 VEC_iterate (dwarf2_section_info_def, types, ix, section);
8b70b953
TT
4197 ++ix)
4198 {
3019eac3 4199 bfd *abfd;
8b70b953 4200 gdb_byte *info_ptr, *end_ptr;
36586728 4201 struct dwarf2_section_info *abbrev_section;
348e048f 4202
8b70b953
TT
4203 dwarf2_read_section (objfile, section);
4204 info_ptr = section->buffer;
348e048f 4205
8b70b953
TT
4206 if (info_ptr == NULL)
4207 continue;
348e048f 4208
3019eac3
DE
4209 /* We can't set abfd until now because the section may be empty or
4210 not present, in which case section->asection will be NULL. */
4211 abfd = section->asection->owner;
4212
36586728
TT
4213 if (dwo_file)
4214 abbrev_section = &dwo_file->sections.abbrev;
4215 else
4216 abbrev_section = &dwarf2_per_objfile->abbrev;
4217
8b70b953 4218 if (types_htab == NULL)
3019eac3
DE
4219 {
4220 if (dwo_file)
4221 types_htab = allocate_dwo_unit_table (objfile);
4222 else
4223 types_htab = allocate_signatured_type_table (objfile);
4224 }
348e048f 4225
dee91e82
DE
4226 /* We don't use init_cutu_and_read_dies_simple, or some such, here
4227 because we don't need to read any dies: the signature is in the
4228 header. */
8b70b953
TT
4229
4230 end_ptr = info_ptr + section->size;
4231 while (info_ptr < end_ptr)
4232 {
b64f50a1 4233 sect_offset offset;
3019eac3 4234 cu_offset type_offset_in_tu;
8b70b953 4235 ULONGEST signature;
52dc124a 4236 struct signatured_type *sig_type;
3019eac3 4237 struct dwo_unit *dwo_tu;
8b70b953
TT
4238 void **slot;
4239 gdb_byte *ptr = info_ptr;
9ff913ba 4240 struct comp_unit_head header;
dee91e82 4241 unsigned int length;
348e048f 4242
b64f50a1 4243 offset.sect_off = ptr - section->buffer;
348e048f 4244
8b70b953 4245 /* We need to read the type's signature in order to build the hash
9ff913ba 4246 table, but we don't need anything else just yet. */
348e048f 4247
4bdcc0c1
DE
4248 ptr = read_and_check_type_unit_head (&header, section,
4249 abbrev_section, ptr,
3019eac3 4250 &signature, &type_offset_in_tu);
6caca83c 4251
1ce1cefd 4252 length = get_cu_length (&header);
dee91e82 4253
6caca83c 4254 /* Skip dummy type units. */
dee91e82
DE
4255 if (ptr >= info_ptr + length
4256 || peek_abbrev_code (abfd, ptr) == 0)
6caca83c 4257 {
1ce1cefd 4258 info_ptr += length;
6caca83c
CC
4259 continue;
4260 }
8b70b953 4261
3019eac3
DE
4262 if (dwo_file)
4263 {
4264 sig_type = NULL;
4265 dwo_tu = OBSTACK_ZALLOC (&objfile->objfile_obstack,
4266 struct dwo_unit);
4267 dwo_tu->dwo_file = dwo_file;
4268 dwo_tu->signature = signature;
4269 dwo_tu->type_offset_in_tu = type_offset_in_tu;
4270 dwo_tu->info_or_types_section = section;
4271 dwo_tu->offset = offset;
4272 dwo_tu->length = length;
4273 }
4274 else
4275 {
4276 /* N.B.: type_offset is not usable if this type uses a DWO file.
4277 The real type_offset is in the DWO file. */
4278 dwo_tu = NULL;
4279 sig_type = OBSTACK_ZALLOC (&objfile->objfile_obstack,
4280 struct signatured_type);
4281 sig_type->signature = signature;
4282 sig_type->type_offset_in_tu = type_offset_in_tu;
4283 sig_type->per_cu.objfile = objfile;
4284 sig_type->per_cu.is_debug_types = 1;
4285 sig_type->per_cu.info_or_types_section = section;
4286 sig_type->per_cu.offset = offset;
4287 sig_type->per_cu.length = length;
4288 }
8b70b953 4289
3019eac3
DE
4290 slot = htab_find_slot (types_htab,
4291 dwo_file ? (void*) dwo_tu : (void *) sig_type,
4292 INSERT);
8b70b953
TT
4293 gdb_assert (slot != NULL);
4294 if (*slot != NULL)
4295 {
3019eac3
DE
4296 sect_offset dup_offset;
4297
4298 if (dwo_file)
4299 {
4300 const struct dwo_unit *dup_tu = *slot;
4301
4302 dup_offset = dup_tu->offset;
4303 }
4304 else
4305 {
4306 const struct signatured_type *dup_tu = *slot;
4307
4308 dup_offset = dup_tu->per_cu.offset;
4309 }
b3c8eb43 4310
8b70b953
TT
4311 complaint (&symfile_complaints,
4312 _("debug type entry at offset 0x%x is duplicate to the "
4313 "entry at offset 0x%x, signature 0x%s"),
3019eac3 4314 offset.sect_off, dup_offset.sect_off,
8b70b953 4315 phex (signature, sizeof (signature)));
8b70b953 4316 }
3019eac3 4317 *slot = dwo_file ? (void *) dwo_tu : (void *) sig_type;
348e048f 4318
09406207 4319 if (dwarf2_read_debug)
8b70b953 4320 fprintf_unfiltered (gdb_stdlog, " offset 0x%x, signature 0x%s\n",
b64f50a1
JK
4321 offset.sect_off,
4322 phex (signature, sizeof (signature)));
348e048f 4323
dee91e82 4324 info_ptr += length;
8b70b953 4325 }
348e048f
DE
4326 }
4327
3019eac3
DE
4328 return types_htab;
4329}
4330
4331/* Create the hash table of all entries in the .debug_types section,
4332 and initialize all_type_units.
4333 The result is zero if there is an error (e.g. missing .debug_types section),
4334 otherwise non-zero. */
4335
4336static int
4337create_all_type_units (struct objfile *objfile)
4338{
4339 htab_t types_htab;
b4dd5633 4340 struct signatured_type **iter;
3019eac3
DE
4341
4342 types_htab = create_debug_types_hash_table (NULL, dwarf2_per_objfile->types);
4343 if (types_htab == NULL)
4344 {
4345 dwarf2_per_objfile->signatured_types = NULL;
4346 return 0;
4347 }
4348
348e048f
DE
4349 dwarf2_per_objfile->signatured_types = types_htab;
4350
d467dd73
DE
4351 dwarf2_per_objfile->n_type_units = htab_elements (types_htab);
4352 dwarf2_per_objfile->all_type_units
1fd400ff 4353 = obstack_alloc (&objfile->objfile_obstack,
d467dd73 4354 dwarf2_per_objfile->n_type_units
b4dd5633 4355 * sizeof (struct signatured_type *));
d467dd73
DE
4356 iter = &dwarf2_per_objfile->all_type_units[0];
4357 htab_traverse_noresize (types_htab, add_signatured_type_cu_to_table, &iter);
4358 gdb_assert (iter - &dwarf2_per_objfile->all_type_units[0]
4359 == dwarf2_per_objfile->n_type_units);
1fd400ff 4360
348e048f
DE
4361 return 1;
4362}
4363
380bca97 4364/* Lookup a signature based type for DW_FORM_ref_sig8.
e319fa28 4365 Returns NULL if signature SIG is not present in the table. */
348e048f
DE
4366
4367static struct signatured_type *
e319fa28 4368lookup_signatured_type (ULONGEST sig)
348e048f
DE
4369{
4370 struct signatured_type find_entry, *entry;
4371
4372 if (dwarf2_per_objfile->signatured_types == NULL)
4373 {
4374 complaint (&symfile_complaints,
55f1336d 4375 _("missing `.debug_types' section for DW_FORM_ref_sig8 die"));
dcc07052 4376 return NULL;
348e048f
DE
4377 }
4378
4379 find_entry.signature = sig;
4380 entry = htab_find (dwarf2_per_objfile->signatured_types, &find_entry);
4381 return entry;
4382}
42e7ad6c
DE
4383\f
4384/* Low level DIE reading support. */
348e048f 4385
d85a05f0
DJ
4386/* Initialize a die_reader_specs struct from a dwarf2_cu struct. */
4387
4388static void
4389init_cu_die_reader (struct die_reader_specs *reader,
dee91e82 4390 struct dwarf2_cu *cu,
3019eac3
DE
4391 struct dwarf2_section_info *section,
4392 struct dwo_file *dwo_file)
d85a05f0 4393{
fceca515 4394 gdb_assert (section->readin && section->buffer != NULL);
dee91e82 4395 reader->abfd = section->asection->owner;
d85a05f0 4396 reader->cu = cu;
3019eac3 4397 reader->dwo_file = dwo_file;
dee91e82
DE
4398 reader->die_section = section;
4399 reader->buffer = section->buffer;
f664829e 4400 reader->buffer_end = section->buffer + section->size;
d85a05f0
DJ
4401}
4402
fd820528 4403/* Initialize a CU (or TU) and read its DIEs.
3019eac3 4404 If the CU defers to a DWO file, read the DWO file as well.
dee91e82 4405
f4dc4d17
DE
4406 ABBREV_TABLE, if non-NULL, is the abbreviation table to use.
4407 Otherwise the table specified in the comp unit header is read in and used.
4408 This is an optimization for when we already have the abbrev table.
4409
dee91e82
DE
4410 If USE_EXISTING_CU is non-zero, and THIS_CU->cu is non-NULL, then use it.
4411 Otherwise, a new CU is allocated with xmalloc.
4412
4413 If KEEP is non-zero, then if we allocated a dwarf2_cu we add it to
4414 read_in_chain. Otherwise the dwarf2_cu data is freed at the end.
4415
4416 WARNING: If THIS_CU is a "dummy CU" (used as filler by the incremental
fd820528 4417 linker) then DIE_READER_FUNC will not get called. */
aaa75496 4418
70221824 4419static void
fd820528 4420init_cutu_and_read_dies (struct dwarf2_per_cu_data *this_cu,
f4dc4d17 4421 struct abbrev_table *abbrev_table,
fd820528
DE
4422 int use_existing_cu, int keep,
4423 die_reader_func_ftype *die_reader_func,
4424 void *data)
c906108c 4425{
dee91e82 4426 struct objfile *objfile = dwarf2_per_objfile->objfile;
3019eac3
DE
4427 struct dwarf2_section_info *section = this_cu->info_or_types_section;
4428 bfd *abfd = section->asection->owner;
dee91e82
DE
4429 struct dwarf2_cu *cu;
4430 gdb_byte *begin_info_ptr, *info_ptr;
4431 struct die_reader_specs reader;
d85a05f0 4432 struct die_info *comp_unit_die;
dee91e82 4433 int has_children;
d85a05f0 4434 struct attribute *attr;
dee91e82
DE
4435 struct cleanup *cleanups, *free_cu_cleanup = NULL;
4436 struct signatured_type *sig_type = NULL;
4bdcc0c1 4437 struct dwarf2_section_info *abbrev_section;
42e7ad6c
DE
4438 /* Non-zero if CU currently points to a DWO file and we need to
4439 reread it. When this happens we need to reread the skeleton die
4440 before we can reread the DWO file. */
4441 int rereading_dwo_cu = 0;
c906108c 4442
09406207
DE
4443 if (dwarf2_die_debug)
4444 fprintf_unfiltered (gdb_stdlog, "Reading %s unit at offset 0x%x\n",
4445 this_cu->is_debug_types ? "type" : "comp",
4446 this_cu->offset.sect_off);
4447
dee91e82
DE
4448 if (use_existing_cu)
4449 gdb_assert (keep);
23745b47 4450
dee91e82
DE
4451 cleanups = make_cleanup (null_cleanup, NULL);
4452
4453 /* This is cheap if the section is already read in. */
4454 dwarf2_read_section (objfile, section);
4455
4456 begin_info_ptr = info_ptr = section->buffer + this_cu->offset.sect_off;
36586728
TT
4457
4458 abbrev_section = get_abbrev_section_for_cu (this_cu);
dee91e82
DE
4459
4460 if (use_existing_cu && this_cu->cu != NULL)
4461 {
4462 cu = this_cu->cu;
42e7ad6c
DE
4463
4464 /* If this CU is from a DWO file we need to start over, we need to
4465 refetch the attributes from the skeleton CU.
4466 This could be optimized by retrieving those attributes from when we
4467 were here the first time: the previous comp_unit_die was stored in
4468 comp_unit_obstack. But there's no data yet that we need this
4469 optimization. */
4470 if (cu->dwo_unit != NULL)
4471 rereading_dwo_cu = 1;
dee91e82
DE
4472 }
4473 else
4474 {
4475 /* If !use_existing_cu, this_cu->cu must be NULL. */
4476 gdb_assert (this_cu->cu == NULL);
4477
4478 cu = xmalloc (sizeof (*cu));
4479 init_one_comp_unit (cu, this_cu);
4480
4481 /* If an error occurs while loading, release our storage. */
4482 free_cu_cleanup = make_cleanup (free_heap_comp_unit, cu);
42e7ad6c 4483 }
dee91e82 4484
42e7ad6c
DE
4485 if (cu->header.first_die_offset.cu_off != 0 && ! rereading_dwo_cu)
4486 {
4487 /* We already have the header, there's no need to read it in again. */
4488 info_ptr += cu->header.first_die_offset.cu_off;
4489 }
4490 else
4491 {
3019eac3 4492 if (this_cu->is_debug_types)
dee91e82
DE
4493 {
4494 ULONGEST signature;
42e7ad6c 4495 cu_offset type_offset_in_tu;
dee91e82 4496
4bdcc0c1
DE
4497 info_ptr = read_and_check_type_unit_head (&cu->header, section,
4498 abbrev_section, info_ptr,
42e7ad6c
DE
4499 &signature,
4500 &type_offset_in_tu);
dee91e82 4501
42e7ad6c
DE
4502 /* Since per_cu is the first member of struct signatured_type,
4503 we can go from a pointer to one to a pointer to the other. */
4504 sig_type = (struct signatured_type *) this_cu;
4505 gdb_assert (sig_type->signature == signature);
4506 gdb_assert (sig_type->type_offset_in_tu.cu_off
4507 == type_offset_in_tu.cu_off);
dee91e82
DE
4508 gdb_assert (this_cu->offset.sect_off == cu->header.offset.sect_off);
4509
42e7ad6c
DE
4510 /* LENGTH has not been set yet for type units if we're
4511 using .gdb_index. */
1ce1cefd 4512 this_cu->length = get_cu_length (&cu->header);
3019eac3
DE
4513
4514 /* Establish the type offset that can be used to lookup the type. */
4515 sig_type->type_offset_in_section.sect_off =
4516 this_cu->offset.sect_off + sig_type->type_offset_in_tu.cu_off;
dee91e82
DE
4517 }
4518 else
4519 {
4bdcc0c1
DE
4520 info_ptr = read_and_check_comp_unit_head (&cu->header, section,
4521 abbrev_section,
4522 info_ptr, 0);
dee91e82
DE
4523
4524 gdb_assert (this_cu->offset.sect_off == cu->header.offset.sect_off);
1ce1cefd 4525 gdb_assert (this_cu->length == get_cu_length (&cu->header));
dee91e82
DE
4526 }
4527 }
10b3939b 4528
6caca83c 4529 /* Skip dummy compilation units. */
dee91e82 4530 if (info_ptr >= begin_info_ptr + this_cu->length
6caca83c
CC
4531 || peek_abbrev_code (abfd, info_ptr) == 0)
4532 {
dee91e82 4533 do_cleanups (cleanups);
21b2bd31 4534 return;
6caca83c
CC
4535 }
4536
433df2d4
DE
4537 /* If we don't have them yet, read the abbrevs for this compilation unit.
4538 And if we need to read them now, make sure they're freed when we're
42e7ad6c
DE
4539 done. Note that it's important that if the CU had an abbrev table
4540 on entry we don't free it when we're done: Somewhere up the call stack
4541 it may be in use. */
f4dc4d17
DE
4542 if (abbrev_table != NULL)
4543 {
4544 gdb_assert (cu->abbrev_table == NULL);
4545 gdb_assert (cu->header.abbrev_offset.sect_off
4546 == abbrev_table->offset.sect_off);
4547 cu->abbrev_table = abbrev_table;
4548 }
4549 else if (cu->abbrev_table == NULL)
dee91e82 4550 {
4bdcc0c1 4551 dwarf2_read_abbrevs (cu, abbrev_section);
dee91e82
DE
4552 make_cleanup (dwarf2_free_abbrev_table, cu);
4553 }
42e7ad6c
DE
4554 else if (rereading_dwo_cu)
4555 {
4556 dwarf2_free_abbrev_table (cu);
4557 dwarf2_read_abbrevs (cu, abbrev_section);
4558 }
af703f96 4559
dee91e82 4560 /* Read the top level CU/TU die. */
3019eac3 4561 init_cu_die_reader (&reader, cu, section, NULL);
dee91e82 4562 info_ptr = read_full_die (&reader, &comp_unit_die, info_ptr, &has_children);
93311388 4563
3019eac3
DE
4564 /* If we have a DWO stub, process it and then read in the DWO file.
4565 Note that if USE_EXISTING_OK != 0, and THIS_CU->cu already contains
4566 a DWO CU, that this test will fail. */
4567 attr = dwarf2_attr (comp_unit_die, DW_AT_GNU_dwo_name, cu);
4568 if (attr)
4569 {
15d034d0 4570 const char *dwo_name = DW_STRING (attr);
42e7ad6c 4571 const char *comp_dir_string;
3019eac3
DE
4572 struct dwo_unit *dwo_unit;
4573 ULONGEST signature; /* Or dwo_id. */
42e7ad6c 4574 struct attribute *comp_dir, *stmt_list, *low_pc, *high_pc, *ranges;
3019eac3 4575 int i,num_extra_attrs;
4bdcc0c1 4576 struct dwarf2_section_info *dwo_abbrev_section;
3019eac3
DE
4577
4578 if (has_children)
4579 error (_("Dwarf Error: compilation unit with DW_AT_GNU_dwo_name"
4580 " has children (offset 0x%x) [in module %s]"),
4581 this_cu->offset.sect_off, bfd_get_filename (abfd));
4582
4583 /* These attributes aren't processed until later:
4584 DW_AT_stmt_list, DW_AT_low_pc, DW_AT_high_pc, DW_AT_ranges.
4585 However, the attribute is found in the stub which we won't have later.
4586 In order to not impose this complication on the rest of the code,
4587 we read them here and copy them to the DWO CU/TU die. */
3019eac3
DE
4588
4589 /* For TUs in DWO files, the DW_AT_stmt_list attribute lives in the
4590 DWO file. */
42e7ad6c 4591 stmt_list = NULL;
3019eac3
DE
4592 if (! this_cu->is_debug_types)
4593 stmt_list = dwarf2_attr (comp_unit_die, DW_AT_stmt_list, cu);
4594 low_pc = dwarf2_attr (comp_unit_die, DW_AT_low_pc, cu);
4595 high_pc = dwarf2_attr (comp_unit_die, DW_AT_high_pc, cu);
4596 ranges = dwarf2_attr (comp_unit_die, DW_AT_ranges, cu);
42e7ad6c 4597 comp_dir = dwarf2_attr (comp_unit_die, DW_AT_comp_dir, cu);
3019eac3
DE
4598
4599 /* There should be a DW_AT_addr_base attribute here (if needed).
4600 We need the value before we can process DW_FORM_GNU_addr_index. */
4601 cu->addr_base = 0;
3019eac3
DE
4602 attr = dwarf2_attr (comp_unit_die, DW_AT_GNU_addr_base, cu);
4603 if (attr)
2e3cf129
DE
4604 cu->addr_base = DW_UNSND (attr);
4605
4606 /* There should be a DW_AT_ranges_base attribute here (if needed).
4607 We need the value before we can process DW_AT_ranges. */
4608 cu->ranges_base = 0;
4609 attr = dwarf2_attr (comp_unit_die, DW_AT_GNU_ranges_base, cu);
4610 if (attr)
4611 cu->ranges_base = DW_UNSND (attr);
3019eac3
DE
4612
4613 if (this_cu->is_debug_types)
4614 {
4615 gdb_assert (sig_type != NULL);
4616 signature = sig_type->signature;
4617 }
4618 else
4619 {
4620 attr = dwarf2_attr (comp_unit_die, DW_AT_GNU_dwo_id, cu);
4621 if (! attr)
4622 error (_("Dwarf Error: missing dwo_id [in module %s]"),
4623 dwo_name);
4624 signature = DW_UNSND (attr);
4625 }
4626
4627 /* We may need the comp_dir in order to find the DWO file. */
42e7ad6c
DE
4628 comp_dir_string = NULL;
4629 if (comp_dir)
4630 comp_dir_string = DW_STRING (comp_dir);
3019eac3
DE
4631
4632 if (this_cu->is_debug_types)
42e7ad6c 4633 dwo_unit = lookup_dwo_type_unit (sig_type, dwo_name, comp_dir_string);
3019eac3 4634 else
42e7ad6c 4635 dwo_unit = lookup_dwo_comp_unit (this_cu, dwo_name, comp_dir_string,
3019eac3
DE
4636 signature);
4637
4638 if (dwo_unit == NULL)
4639 {
4640 error (_("Dwarf Error: CU at offset 0x%x references unknown DWO"
4641 " with ID %s [in module %s]"),
4642 this_cu->offset.sect_off,
4643 phex (signature, sizeof (signature)),
4644 objfile->name);
4645 }
4646
4647 /* Set up for reading the DWO CU/TU. */
4648 cu->dwo_unit = dwo_unit;
4649 section = dwo_unit->info_or_types_section;
80626a55 4650 dwarf2_read_section (objfile, section);
3019eac3 4651 begin_info_ptr = info_ptr = section->buffer + dwo_unit->offset.sect_off;
4bdcc0c1 4652 dwo_abbrev_section = &dwo_unit->dwo_file->sections.abbrev;
3019eac3
DE
4653 init_cu_die_reader (&reader, cu, section, dwo_unit->dwo_file);
4654
4655 if (this_cu->is_debug_types)
4656 {
4657 ULONGEST signature;
80626a55 4658 cu_offset type_offset_in_tu;
3019eac3 4659
4bdcc0c1
DE
4660 info_ptr = read_and_check_type_unit_head (&cu->header, section,
4661 dwo_abbrev_section,
4662 info_ptr,
80626a55
DE
4663 &signature,
4664 &type_offset_in_tu);
3019eac3
DE
4665 gdb_assert (sig_type->signature == signature);
4666 gdb_assert (dwo_unit->offset.sect_off == cu->header.offset.sect_off);
80626a55
DE
4667 /* For DWOs coming from DWP files, we don't know the CU length
4668 nor the type's offset in the TU until now. */
4669 dwo_unit->length = get_cu_length (&cu->header);
4670 dwo_unit->type_offset_in_tu = type_offset_in_tu;
3019eac3
DE
4671
4672 /* Establish the type offset that can be used to lookup the type.
4673 For DWO files, we don't know it until now. */
4674 sig_type->type_offset_in_section.sect_off =
4675 dwo_unit->offset.sect_off + dwo_unit->type_offset_in_tu.cu_off;
4676 }
4677 else
4678 {
4bdcc0c1
DE
4679 info_ptr = read_and_check_comp_unit_head (&cu->header, section,
4680 dwo_abbrev_section,
4681 info_ptr, 0);
3019eac3 4682 gdb_assert (dwo_unit->offset.sect_off == cu->header.offset.sect_off);
80626a55
DE
4683 /* For DWOs coming from DWP files, we don't know the CU length
4684 until now. */
4685 dwo_unit->length = get_cu_length (&cu->header);
3019eac3
DE
4686 }
4687
4688 /* Discard the original CU's abbrev table, and read the DWO's. */
f4dc4d17
DE
4689 if (abbrev_table == NULL)
4690 {
4691 dwarf2_free_abbrev_table (cu);
4692 dwarf2_read_abbrevs (cu, dwo_abbrev_section);
4693 }
4694 else
4695 {
4696 dwarf2_read_abbrevs (cu, dwo_abbrev_section);
4697 make_cleanup (dwarf2_free_abbrev_table, cu);
4698 }
3019eac3
DE
4699
4700 /* Read in the die, but leave space to copy over the attributes
4701 from the stub. This has the benefit of simplifying the rest of
4702 the code - all the real work is done here. */
4703 num_extra_attrs = ((stmt_list != NULL)
4704 + (low_pc != NULL)
4705 + (high_pc != NULL)
42e7ad6c
DE
4706 + (ranges != NULL)
4707 + (comp_dir != NULL));
3019eac3
DE
4708 info_ptr = read_full_die_1 (&reader, &comp_unit_die, info_ptr,
4709 &has_children, num_extra_attrs);
4710
4711 /* Copy over the attributes from the stub to the DWO die. */
4712 i = comp_unit_die->num_attrs;
4713 if (stmt_list != NULL)
4714 comp_unit_die->attrs[i++] = *stmt_list;
4715 if (low_pc != NULL)
4716 comp_unit_die->attrs[i++] = *low_pc;
4717 if (high_pc != NULL)
4718 comp_unit_die->attrs[i++] = *high_pc;
4719 if (ranges != NULL)
4720 comp_unit_die->attrs[i++] = *ranges;
42e7ad6c
DE
4721 if (comp_dir != NULL)
4722 comp_unit_die->attrs[i++] = *comp_dir;
3019eac3
DE
4723 comp_unit_die->num_attrs += num_extra_attrs;
4724
4725 /* Skip dummy compilation units. */
4726 if (info_ptr >= begin_info_ptr + dwo_unit->length
4727 || peek_abbrev_code (abfd, info_ptr) == 0)
4728 {
4729 do_cleanups (cleanups);
4730 return;
4731 }
4732 }
4733
dee91e82
DE
4734 die_reader_func (&reader, info_ptr, comp_unit_die, has_children, data);
4735
4736 if (free_cu_cleanup != NULL)
348e048f 4737 {
dee91e82
DE
4738 if (keep)
4739 {
4740 /* We've successfully allocated this compilation unit. Let our
4741 caller clean it up when finished with it. */
4742 discard_cleanups (free_cu_cleanup);
4743
4744 /* We can only discard free_cu_cleanup and all subsequent cleanups.
4745 So we have to manually free the abbrev table. */
4746 dwarf2_free_abbrev_table (cu);
4747
4748 /* Link this CU into read_in_chain. */
4749 this_cu->cu->read_in_chain = dwarf2_per_objfile->read_in_chain;
4750 dwarf2_per_objfile->read_in_chain = this_cu;
4751 }
4752 else
4753 do_cleanups (free_cu_cleanup);
348e048f 4754 }
dee91e82
DE
4755
4756 do_cleanups (cleanups);
4757}
4758
3019eac3
DE
4759/* Read CU/TU THIS_CU in section SECTION,
4760 but do not follow DW_AT_GNU_dwo_name if present.
80626a55
DE
4761 DWOP_FILE, if non-NULL, is the DWO/DWP file to read (the caller is assumed
4762 to have already done the lookup to find the DWO/DWP file).
dee91e82
DE
4763
4764 The caller is required to fill in THIS_CU->section, THIS_CU->offset, and
3019eac3 4765 THIS_CU->is_debug_types, but nothing else.
dee91e82
DE
4766
4767 We fill in THIS_CU->length.
4768
4769 WARNING: If THIS_CU is a "dummy CU" (used as filler by the incremental
4770 linker) then DIE_READER_FUNC will not get called.
4771
4772 THIS_CU->cu is always freed when done.
3019eac3
DE
4773 This is done in order to not leave THIS_CU->cu in a state where we have
4774 to care whether it refers to the "main" CU or the DWO CU. */
dee91e82
DE
4775
4776static void
4777init_cutu_and_read_dies_no_follow (struct dwarf2_per_cu_data *this_cu,
4778 struct dwarf2_section_info *abbrev_section,
3019eac3 4779 struct dwo_file *dwo_file,
dee91e82
DE
4780 die_reader_func_ftype *die_reader_func,
4781 void *data)
4782{
4783 struct objfile *objfile = dwarf2_per_objfile->objfile;
3019eac3
DE
4784 struct dwarf2_section_info *section = this_cu->info_or_types_section;
4785 bfd *abfd = section->asection->owner;
dee91e82
DE
4786 struct dwarf2_cu cu;
4787 gdb_byte *begin_info_ptr, *info_ptr;
4788 struct die_reader_specs reader;
4789 struct cleanup *cleanups;
4790 struct die_info *comp_unit_die;
4791 int has_children;
4792
09406207
DE
4793 if (dwarf2_die_debug)
4794 fprintf_unfiltered (gdb_stdlog, "Reading %s unit at offset 0x%x\n",
4795 this_cu->is_debug_types ? "type" : "comp",
4796 this_cu->offset.sect_off);
4797
dee91e82
DE
4798 gdb_assert (this_cu->cu == NULL);
4799
dee91e82
DE
4800 /* This is cheap if the section is already read in. */
4801 dwarf2_read_section (objfile, section);
4802
4803 init_one_comp_unit (&cu, this_cu);
4804
4805 cleanups = make_cleanup (free_stack_comp_unit, &cu);
4806
4807 begin_info_ptr = info_ptr = section->buffer + this_cu->offset.sect_off;
4bdcc0c1
DE
4808 info_ptr = read_and_check_comp_unit_head (&cu.header, section,
4809 abbrev_section, info_ptr,
3019eac3 4810 this_cu->is_debug_types);
dee91e82 4811
1ce1cefd 4812 this_cu->length = get_cu_length (&cu.header);
dee91e82
DE
4813
4814 /* Skip dummy compilation units. */
4815 if (info_ptr >= begin_info_ptr + this_cu->length
4816 || peek_abbrev_code (abfd, info_ptr) == 0)
c906108c 4817 {
dee91e82 4818 do_cleanups (cleanups);
21b2bd31 4819 return;
93311388 4820 }
72bf9492 4821
dee91e82
DE
4822 dwarf2_read_abbrevs (&cu, abbrev_section);
4823 make_cleanup (dwarf2_free_abbrev_table, &cu);
4824
3019eac3 4825 init_cu_die_reader (&reader, &cu, section, dwo_file);
dee91e82
DE
4826 info_ptr = read_full_die (&reader, &comp_unit_die, info_ptr, &has_children);
4827
4828 die_reader_func (&reader, info_ptr, comp_unit_die, has_children, data);
4829
4830 do_cleanups (cleanups);
4831}
4832
3019eac3
DE
4833/* Read a CU/TU, except that this does not look for DW_AT_GNU_dwo_name and
4834 does not lookup the specified DWO file.
4835 This cannot be used to read DWO files.
dee91e82
DE
4836
4837 THIS_CU->cu is always freed when done.
3019eac3
DE
4838 This is done in order to not leave THIS_CU->cu in a state where we have
4839 to care whether it refers to the "main" CU or the DWO CU.
4840 We can revisit this if the data shows there's a performance issue. */
dee91e82
DE
4841
4842static void
4843init_cutu_and_read_dies_simple (struct dwarf2_per_cu_data *this_cu,
4844 die_reader_func_ftype *die_reader_func,
4845 void *data)
4846{
4847 init_cutu_and_read_dies_no_follow (this_cu,
36586728 4848 get_abbrev_section_for_cu (this_cu),
3019eac3 4849 NULL,
dee91e82
DE
4850 die_reader_func, data);
4851}
4852
f4dc4d17
DE
4853/* Create a psymtab named NAME and assign it to PER_CU.
4854
4855 The caller must fill in the following details:
4856 dirname, textlow, texthigh. */
4857
4858static struct partial_symtab *
4859create_partial_symtab (struct dwarf2_per_cu_data *per_cu, const char *name)
4860{
4861 struct objfile *objfile = per_cu->objfile;
4862 struct partial_symtab *pst;
4863
4864 pst = start_psymtab_common (objfile, objfile->section_offsets,
4865 name, 0,
4866 objfile->global_psymbols.next,
4867 objfile->static_psymbols.next);
4868
4869 pst->psymtabs_addrmap_supported = 1;
4870
4871 /* This is the glue that links PST into GDB's symbol API. */
4872 pst->read_symtab_private = per_cu;
257e7a09 4873 pst->read_symtab = dwarf2_read_symtab;
f4dc4d17
DE
4874 per_cu->v.psymtab = pst;
4875
4876 return pst;
4877}
4878
dee91e82
DE
4879/* die_reader_func for process_psymtab_comp_unit. */
4880
4881static void
4882process_psymtab_comp_unit_reader (const struct die_reader_specs *reader,
4883 gdb_byte *info_ptr,
4884 struct die_info *comp_unit_die,
4885 int has_children,
4886 void *data)
4887{
4888 struct dwarf2_cu *cu = reader->cu;
4889 struct objfile *objfile = cu->objfile;
4890 struct dwarf2_per_cu_data *per_cu = cu->per_cu;
dee91e82
DE
4891 struct attribute *attr;
4892 CORE_ADDR baseaddr;
4893 CORE_ADDR best_lowpc = 0, best_highpc = 0;
4894 struct partial_symtab *pst;
4895 int has_pc_info;
4896 const char *filename;
95554aad 4897 int *want_partial_unit_ptr = data;
dee91e82 4898
95554aad
TT
4899 if (comp_unit_die->tag == DW_TAG_partial_unit
4900 && (want_partial_unit_ptr == NULL
4901 || !*want_partial_unit_ptr))
dee91e82
DE
4902 return;
4903
f4dc4d17
DE
4904 gdb_assert (! per_cu->is_debug_types);
4905
95554aad 4906 prepare_one_comp_unit (cu, comp_unit_die, language_minimal);
dee91e82
DE
4907
4908 cu->list_in_scope = &file_symbols;
c906108c 4909
93311388 4910 /* Allocate a new partial symbol table structure. */
dee91e82 4911 attr = dwarf2_attr (comp_unit_die, DW_AT_name, cu);
3e2a0cee
TT
4912 if (attr == NULL || !DW_STRING (attr))
4913 filename = "";
4914 else
4915 filename = DW_STRING (attr);
72bf9492 4916
f4dc4d17
DE
4917 pst = create_partial_symtab (per_cu, filename);
4918
4919 /* This must be done before calling dwarf2_build_include_psymtabs. */
dee91e82 4920 attr = dwarf2_attr (comp_unit_die, DW_AT_comp_dir, cu);
d85a05f0
DJ
4921 if (attr != NULL)
4922 pst->dirname = DW_STRING (attr);
72bf9492 4923
93311388 4924 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
e7c27a73 4925
dee91e82 4926 dwarf2_find_base_address (comp_unit_die, cu);
d85a05f0 4927
93311388
DE
4928 /* Possibly set the default values of LOWPC and HIGHPC from
4929 `DW_AT_ranges'. */
d85a05f0 4930 has_pc_info = dwarf2_get_pc_bounds (comp_unit_die, &best_lowpc,
dee91e82 4931 &best_highpc, cu, pst);
d85a05f0 4932 if (has_pc_info == 1 && best_lowpc < best_highpc)
93311388
DE
4933 /* Store the contiguous range if it is not empty; it can be empty for
4934 CUs with no code. */
4935 addrmap_set_empty (objfile->psymtabs_addrmap,
d85a05f0
DJ
4936 best_lowpc + baseaddr,
4937 best_highpc + baseaddr - 1, pst);
93311388
DE
4938
4939 /* Check if comp unit has_children.
4940 If so, read the rest of the partial symbols from this comp unit.
0963b4bd 4941 If not, there's no more debug_info for this comp unit. */
d85a05f0 4942 if (has_children)
93311388
DE
4943 {
4944 struct partial_die_info *first_die;
4945 CORE_ADDR lowpc, highpc;
31ffec48 4946
93311388
DE
4947 lowpc = ((CORE_ADDR) -1);
4948 highpc = ((CORE_ADDR) 0);
c906108c 4949
dee91e82 4950 first_die = load_partial_dies (reader, info_ptr, 1);
c906108c 4951
93311388 4952 scan_partial_symbols (first_die, &lowpc, &highpc,
dee91e82 4953 ! has_pc_info, cu);
57c22c6c 4954
93311388
DE
4955 /* If we didn't find a lowpc, set it to highpc to avoid
4956 complaints from `maint check'. */
4957 if (lowpc == ((CORE_ADDR) -1))
4958 lowpc = highpc;
10b3939b 4959
93311388
DE
4960 /* If the compilation unit didn't have an explicit address range,
4961 then use the information extracted from its child dies. */
d85a05f0 4962 if (! has_pc_info)
93311388 4963 {
d85a05f0
DJ
4964 best_lowpc = lowpc;
4965 best_highpc = highpc;
93311388
DE
4966 }
4967 }
d85a05f0
DJ
4968 pst->textlow = best_lowpc + baseaddr;
4969 pst->texthigh = best_highpc + baseaddr;
c906108c 4970
93311388
DE
4971 pst->n_global_syms = objfile->global_psymbols.next -
4972 (objfile->global_psymbols.list + pst->globals_offset);
4973 pst->n_static_syms = objfile->static_psymbols.next -
4974 (objfile->static_psymbols.list + pst->statics_offset);
5c80ed9d 4975 sort_pst_symbols (objfile, pst);
c906108c 4976
796a7ff8 4977 if (!VEC_empty (dwarf2_per_cu_ptr, cu->per_cu->imported_symtabs))
95554aad
TT
4978 {
4979 int i;
796a7ff8 4980 int len = VEC_length (dwarf2_per_cu_ptr, cu->per_cu->imported_symtabs);
95554aad
TT
4981 struct dwarf2_per_cu_data *iter;
4982
4983 /* Fill in 'dependencies' here; we fill in 'users' in a
4984 post-pass. */
4985 pst->number_of_dependencies = len;
4986 pst->dependencies = obstack_alloc (&objfile->objfile_obstack,
4987 len * sizeof (struct symtab *));
4988 for (i = 0;
796a7ff8 4989 VEC_iterate (dwarf2_per_cu_ptr, cu->per_cu->imported_symtabs,
95554aad
TT
4990 i, iter);
4991 ++i)
4992 pst->dependencies[i] = iter->v.psymtab;
4993
796a7ff8 4994 VEC_free (dwarf2_per_cu_ptr, cu->per_cu->imported_symtabs);
95554aad
TT
4995 }
4996
f4dc4d17
DE
4997 /* Get the list of files included in the current compilation unit,
4998 and build a psymtab for each of them. */
4999 dwarf2_build_include_psymtabs (cu, comp_unit_die, pst);
09406207
DE
5000
5001 if (dwarf2_read_debug)
5002 {
5003 struct gdbarch *gdbarch = get_objfile_arch (objfile);
5004
5005 fprintf_unfiltered (gdb_stdlog,
844226d6 5006 "Psymtab for %s unit @0x%x: %s - %s"
09406207
DE
5007 ", %d global, %d static syms\n",
5008 per_cu->is_debug_types ? "type" : "comp",
5009 per_cu->offset.sect_off,
5010 paddress (gdbarch, pst->textlow),
5011 paddress (gdbarch, pst->texthigh),
5012 pst->n_global_syms, pst->n_static_syms);
5013 }
dee91e82 5014}
ae038cb0 5015
dee91e82
DE
5016/* Subroutine of dwarf2_build_psymtabs_hard to simplify it.
5017 Process compilation unit THIS_CU for a psymtab. */
5018
5019static void
95554aad
TT
5020process_psymtab_comp_unit (struct dwarf2_per_cu_data *this_cu,
5021 int want_partial_unit)
dee91e82
DE
5022{
5023 /* If this compilation unit was already read in, free the
5024 cached copy in order to read it in again. This is
5025 necessary because we skipped some symbols when we first
5026 read in the compilation unit (see load_partial_dies).
5027 This problem could be avoided, but the benefit is unclear. */
5028 if (this_cu->cu != NULL)
5029 free_one_cached_comp_unit (this_cu);
5030
3019eac3 5031 gdb_assert (! this_cu->is_debug_types);
f4dc4d17
DE
5032 init_cutu_and_read_dies (this_cu, NULL, 0, 0,
5033 process_psymtab_comp_unit_reader,
95554aad 5034 &want_partial_unit);
dee91e82
DE
5035
5036 /* Age out any secondary CUs. */
5037 age_cached_comp_units ();
93311388 5038}
ff013f42 5039
f4dc4d17
DE
5040static hashval_t
5041hash_type_unit_group (const void *item)
5042{
094b34ac 5043 const struct type_unit_group *tu_group = item;
f4dc4d17 5044
094b34ac 5045 return hash_stmt_list_entry (&tu_group->hash);
f4dc4d17 5046}
348e048f
DE
5047
5048static int
f4dc4d17 5049eq_type_unit_group (const void *item_lhs, const void *item_rhs)
348e048f 5050{
f4dc4d17
DE
5051 const struct type_unit_group *lhs = item_lhs;
5052 const struct type_unit_group *rhs = item_rhs;
348e048f 5053
094b34ac 5054 return eq_stmt_list_entry (&lhs->hash, &rhs->hash);
f4dc4d17 5055}
348e048f 5056
f4dc4d17
DE
5057/* Allocate a hash table for type unit groups. */
5058
5059static htab_t
5060allocate_type_unit_groups_table (void)
5061{
5062 return htab_create_alloc_ex (3,
5063 hash_type_unit_group,
5064 eq_type_unit_group,
5065 NULL,
5066 &dwarf2_per_objfile->objfile->objfile_obstack,
5067 hashtab_obstack_allocate,
5068 dummy_obstack_deallocate);
5069}
dee91e82 5070
f4dc4d17
DE
5071/* Type units that don't have DW_AT_stmt_list are grouped into their own
5072 partial symtabs. We combine several TUs per psymtab to not let the size
5073 of any one psymtab grow too big. */
5074#define NO_STMT_LIST_TYPE_UNIT_PSYMTAB (1 << 31)
5075#define NO_STMT_LIST_TYPE_UNIT_PSYMTAB_SIZE 10
dee91e82 5076
094b34ac 5077/* Helper routine for get_type_unit_group.
f4dc4d17
DE
5078 Create the type_unit_group object used to hold one or more TUs. */
5079
5080static struct type_unit_group *
094b34ac 5081create_type_unit_group (struct dwarf2_cu *cu, sect_offset line_offset_struct)
f4dc4d17
DE
5082{
5083 struct objfile *objfile = dwarf2_per_objfile->objfile;
094b34ac 5084 struct dwarf2_per_cu_data *per_cu;
f4dc4d17 5085 struct type_unit_group *tu_group;
f4dc4d17
DE
5086
5087 tu_group = OBSTACK_ZALLOC (&objfile->objfile_obstack,
5088 struct type_unit_group);
094b34ac 5089 per_cu = &tu_group->per_cu;
f4dc4d17
DE
5090 per_cu->objfile = objfile;
5091 per_cu->is_debug_types = 1;
796a7ff8 5092 per_cu->type_unit_group = tu_group;
f4dc4d17 5093
094b34ac
DE
5094 if (dwarf2_per_objfile->using_index)
5095 {
5096 per_cu->v.quick = OBSTACK_ZALLOC (&objfile->objfile_obstack,
5097 struct dwarf2_per_cu_quick_data);
5098 tu_group->t.first_tu = cu->per_cu;
5099 }
5100 else
5101 {
5102 unsigned int line_offset = line_offset_struct.sect_off;
5103 struct partial_symtab *pst;
5104 char *name;
5105
5106 /* Give the symtab a useful name for debug purposes. */
5107 if ((line_offset & NO_STMT_LIST_TYPE_UNIT_PSYMTAB) != 0)
5108 name = xstrprintf ("<type_units_%d>",
5109 (line_offset & ~NO_STMT_LIST_TYPE_UNIT_PSYMTAB));
5110 else
5111 name = xstrprintf ("<type_units_at_0x%x>", line_offset);
5112
5113 pst = create_partial_symtab (per_cu, name);
5114 pst->anonymous = 1;
f4dc4d17 5115
094b34ac
DE
5116 xfree (name);
5117 }
f4dc4d17 5118
094b34ac
DE
5119 tu_group->hash.dwo_unit = cu->dwo_unit;
5120 tu_group->hash.line_offset = line_offset_struct;
f4dc4d17
DE
5121
5122 return tu_group;
5123}
5124
094b34ac
DE
5125/* Look up the type_unit_group for type unit CU, and create it if necessary.
5126 STMT_LIST is a DW_AT_stmt_list attribute. */
f4dc4d17
DE
5127
5128static struct type_unit_group *
094b34ac 5129get_type_unit_group (struct dwarf2_cu *cu, struct attribute *stmt_list)
f4dc4d17
DE
5130{
5131 struct tu_stats *tu_stats = &dwarf2_per_objfile->tu_stats;
5132 struct type_unit_group *tu_group;
5133 void **slot;
5134 unsigned int line_offset;
5135 struct type_unit_group type_unit_group_for_lookup;
5136
5137 if (dwarf2_per_objfile->type_unit_groups == NULL)
5138 {
5139 dwarf2_per_objfile->type_unit_groups =
5140 allocate_type_unit_groups_table ();
5141 }
5142
5143 /* Do we need to create a new group, or can we use an existing one? */
5144
5145 if (stmt_list)
5146 {
5147 line_offset = DW_UNSND (stmt_list);
5148 ++tu_stats->nr_symtab_sharers;
5149 }
5150 else
5151 {
5152 /* Ugh, no stmt_list. Rare, but we have to handle it.
5153 We can do various things here like create one group per TU or
5154 spread them over multiple groups to split up the expansion work.
5155 To avoid worst case scenarios (too many groups or too large groups)
5156 we, umm, group them in bunches. */
5157 line_offset = (NO_STMT_LIST_TYPE_UNIT_PSYMTAB
5158 | (tu_stats->nr_stmt_less_type_units
5159 / NO_STMT_LIST_TYPE_UNIT_PSYMTAB_SIZE));
5160 ++tu_stats->nr_stmt_less_type_units;
5161 }
5162
094b34ac
DE
5163 type_unit_group_for_lookup.hash.dwo_unit = cu->dwo_unit;
5164 type_unit_group_for_lookup.hash.line_offset.sect_off = line_offset;
f4dc4d17
DE
5165 slot = htab_find_slot (dwarf2_per_objfile->type_unit_groups,
5166 &type_unit_group_for_lookup, INSERT);
5167 if (*slot != NULL)
5168 {
5169 tu_group = *slot;
5170 gdb_assert (tu_group != NULL);
5171 }
5172 else
5173 {
5174 sect_offset line_offset_struct;
5175
5176 line_offset_struct.sect_off = line_offset;
094b34ac 5177 tu_group = create_type_unit_group (cu, line_offset_struct);
f4dc4d17
DE
5178 *slot = tu_group;
5179 ++tu_stats->nr_symtabs;
5180 }
5181
5182 return tu_group;
5183}
5184
5185/* Struct used to sort TUs by their abbreviation table offset. */
5186
5187struct tu_abbrev_offset
5188{
5189 struct signatured_type *sig_type;
5190 sect_offset abbrev_offset;
5191};
5192
5193/* Helper routine for build_type_unit_groups, passed to qsort. */
5194
5195static int
5196sort_tu_by_abbrev_offset (const void *ap, const void *bp)
5197{
5198 const struct tu_abbrev_offset * const *a = ap;
5199 const struct tu_abbrev_offset * const *b = bp;
5200 unsigned int aoff = (*a)->abbrev_offset.sect_off;
5201 unsigned int boff = (*b)->abbrev_offset.sect_off;
5202
5203 return (aoff > boff) - (aoff < boff);
5204}
5205
5206/* A helper function to add a type_unit_group to a table. */
5207
5208static int
5209add_type_unit_group_to_table (void **slot, void *datum)
5210{
5211 struct type_unit_group *tu_group = *slot;
5212 struct type_unit_group ***datap = datum;
5213
5214 **datap = tu_group;
5215 ++*datap;
5216
5217 return 1;
5218}
5219
5220/* Efficiently read all the type units, calling init_cutu_and_read_dies on
5221 each one passing FUNC,DATA.
5222
5223 The efficiency is because we sort TUs by the abbrev table they use and
5224 only read each abbrev table once. In one program there are 200K TUs
5225 sharing 8K abbrev tables.
5226
5227 The main purpose of this function is to support building the
5228 dwarf2_per_objfile->type_unit_groups table.
5229 TUs typically share the DW_AT_stmt_list of the CU they came from, so we
5230 can collapse the search space by grouping them by stmt_list.
5231 The savings can be significant, in the same program from above the 200K TUs
5232 share 8K stmt_list tables.
5233
5234 FUNC is expected to call get_type_unit_group, which will create the
5235 struct type_unit_group if necessary and add it to
5236 dwarf2_per_objfile->type_unit_groups. */
5237
5238static void
5239build_type_unit_groups (die_reader_func_ftype *func, void *data)
5240{
5241 struct objfile *objfile = dwarf2_per_objfile->objfile;
5242 struct tu_stats *tu_stats = &dwarf2_per_objfile->tu_stats;
5243 struct cleanup *cleanups;
5244 struct abbrev_table *abbrev_table;
5245 sect_offset abbrev_offset;
5246 struct tu_abbrev_offset *sorted_by_abbrev;
5247 struct type_unit_group **iter;
5248 int i;
5249
5250 /* It's up to the caller to not call us multiple times. */
5251 gdb_assert (dwarf2_per_objfile->type_unit_groups == NULL);
5252
5253 if (dwarf2_per_objfile->n_type_units == 0)
5254 return;
5255
5256 /* TUs typically share abbrev tables, and there can be way more TUs than
5257 abbrev tables. Sort by abbrev table to reduce the number of times we
5258 read each abbrev table in.
5259 Alternatives are to punt or to maintain a cache of abbrev tables.
5260 This is simpler and efficient enough for now.
5261
5262 Later we group TUs by their DW_AT_stmt_list value (as this defines the
5263 symtab to use). Typically TUs with the same abbrev offset have the same
5264 stmt_list value too so in practice this should work well.
5265
5266 The basic algorithm here is:
5267
5268 sort TUs by abbrev table
5269 for each TU with same abbrev table:
5270 read abbrev table if first user
5271 read TU top level DIE
5272 [IWBN if DWO skeletons had DW_AT_stmt_list]
5273 call FUNC */
5274
5275 if (dwarf2_read_debug)
5276 fprintf_unfiltered (gdb_stdlog, "Building type unit groups ...\n");
5277
5278 /* Sort in a separate table to maintain the order of all_type_units
5279 for .gdb_index: TU indices directly index all_type_units. */
5280 sorted_by_abbrev = XNEWVEC (struct tu_abbrev_offset,
5281 dwarf2_per_objfile->n_type_units);
5282 for (i = 0; i < dwarf2_per_objfile->n_type_units; ++i)
5283 {
5284 struct signatured_type *sig_type = dwarf2_per_objfile->all_type_units[i];
5285
5286 sorted_by_abbrev[i].sig_type = sig_type;
5287 sorted_by_abbrev[i].abbrev_offset =
5288 read_abbrev_offset (sig_type->per_cu.info_or_types_section,
5289 sig_type->per_cu.offset);
5290 }
5291 cleanups = make_cleanup (xfree, sorted_by_abbrev);
5292 qsort (sorted_by_abbrev, dwarf2_per_objfile->n_type_units,
5293 sizeof (struct tu_abbrev_offset), sort_tu_by_abbrev_offset);
5294
094b34ac
DE
5295 /* Note: In the .gdb_index case, get_type_unit_group may have already been
5296 called any number of times, so we don't reset tu_stats here. */
5297
f4dc4d17
DE
5298 abbrev_offset.sect_off = ~(unsigned) 0;
5299 abbrev_table = NULL;
5300 make_cleanup (abbrev_table_free_cleanup, &abbrev_table);
5301
5302 for (i = 0; i < dwarf2_per_objfile->n_type_units; ++i)
5303 {
5304 const struct tu_abbrev_offset *tu = &sorted_by_abbrev[i];
5305
5306 /* Switch to the next abbrev table if necessary. */
5307 if (abbrev_table == NULL
5308 || tu->abbrev_offset.sect_off != abbrev_offset.sect_off)
5309 {
5310 if (abbrev_table != NULL)
5311 {
5312 abbrev_table_free (abbrev_table);
5313 /* Reset to NULL in case abbrev_table_read_table throws
5314 an error: abbrev_table_free_cleanup will get called. */
5315 abbrev_table = NULL;
5316 }
5317 abbrev_offset = tu->abbrev_offset;
5318 abbrev_table =
5319 abbrev_table_read_table (&dwarf2_per_objfile->abbrev,
5320 abbrev_offset);
5321 ++tu_stats->nr_uniq_abbrev_tables;
5322 }
5323
5324 init_cutu_and_read_dies (&tu->sig_type->per_cu, abbrev_table, 0, 0,
5325 func, data);
5326 }
5327
5328 /* Create a vector of pointers to primary type units to make it easy to
5329 iterate over them and CUs. See dw2_get_primary_cu. */
5330 dwarf2_per_objfile->n_type_unit_groups =
5331 htab_elements (dwarf2_per_objfile->type_unit_groups);
5332 dwarf2_per_objfile->all_type_unit_groups =
5333 obstack_alloc (&objfile->objfile_obstack,
5334 dwarf2_per_objfile->n_type_unit_groups
5335 * sizeof (struct type_unit_group *));
5336 iter = &dwarf2_per_objfile->all_type_unit_groups[0];
5337 htab_traverse_noresize (dwarf2_per_objfile->type_unit_groups,
5338 add_type_unit_group_to_table, &iter);
5339 gdb_assert (iter - &dwarf2_per_objfile->all_type_unit_groups[0]
5340 == dwarf2_per_objfile->n_type_unit_groups);
5341
5342 do_cleanups (cleanups);
5343
5344 if (dwarf2_read_debug)
5345 {
5346 fprintf_unfiltered (gdb_stdlog, "Done building type unit groups:\n");
5347 fprintf_unfiltered (gdb_stdlog, " %d TUs\n",
5348 dwarf2_per_objfile->n_type_units);
5349 fprintf_unfiltered (gdb_stdlog, " %d uniq abbrev tables\n",
5350 tu_stats->nr_uniq_abbrev_tables);
5351 fprintf_unfiltered (gdb_stdlog, " %d symtabs from stmt_list entries\n",
5352 tu_stats->nr_symtabs);
5353 fprintf_unfiltered (gdb_stdlog, " %d symtab sharers\n",
5354 tu_stats->nr_symtab_sharers);
5355 fprintf_unfiltered (gdb_stdlog, " %d type units without a stmt_list\n",
5356 tu_stats->nr_stmt_less_type_units);
5357 }
5358}
5359
5360/* Reader function for build_type_psymtabs. */
5361
5362static void
5363build_type_psymtabs_reader (const struct die_reader_specs *reader,
5364 gdb_byte *info_ptr,
5365 struct die_info *type_unit_die,
5366 int has_children,
5367 void *data)
5368{
5369 struct objfile *objfile = dwarf2_per_objfile->objfile;
5370 struct dwarf2_cu *cu = reader->cu;
5371 struct dwarf2_per_cu_data *per_cu = cu->per_cu;
5372 struct type_unit_group *tu_group;
5373 struct attribute *attr;
5374 struct partial_die_info *first_die;
5375 CORE_ADDR lowpc, highpc;
5376 struct partial_symtab *pst;
5377
5378 gdb_assert (data == NULL);
5379
5380 if (! has_children)
5381 return;
5382
5383 attr = dwarf2_attr_no_follow (type_unit_die, DW_AT_stmt_list);
094b34ac 5384 tu_group = get_type_unit_group (cu, attr);
f4dc4d17 5385
094b34ac 5386 VEC_safe_push (dwarf2_per_cu_ptr, tu_group->t.tus, per_cu);
f4dc4d17
DE
5387
5388 prepare_one_comp_unit (cu, type_unit_die, language_minimal);
5389 cu->list_in_scope = &file_symbols;
5390 pst = create_partial_symtab (per_cu, "");
5391 pst->anonymous = 1;
5392
5393 first_die = load_partial_dies (reader, info_ptr, 1);
5394
5395 lowpc = (CORE_ADDR) -1;
5396 highpc = (CORE_ADDR) 0;
5397 scan_partial_symbols (first_die, &lowpc, &highpc, 0, cu);
5398
5399 pst->n_global_syms = objfile->global_psymbols.next -
5400 (objfile->global_psymbols.list + pst->globals_offset);
5401 pst->n_static_syms = objfile->static_psymbols.next -
5402 (objfile->static_psymbols.list + pst->statics_offset);
5c80ed9d 5403 sort_pst_symbols (objfile, pst);
f4dc4d17
DE
5404}
5405
5406/* Traversal function for build_type_psymtabs. */
5407
5408static int
5409build_type_psymtab_dependencies (void **slot, void *info)
5410{
5411 struct objfile *objfile = dwarf2_per_objfile->objfile;
5412 struct type_unit_group *tu_group = (struct type_unit_group *) *slot;
094b34ac 5413 struct dwarf2_per_cu_data *per_cu = &tu_group->per_cu;
f4dc4d17 5414 struct partial_symtab *pst = per_cu->v.psymtab;
094b34ac 5415 int len = VEC_length (dwarf2_per_cu_ptr, tu_group->t.tus);
f4dc4d17
DE
5416 struct dwarf2_per_cu_data *iter;
5417 int i;
5418
5419 gdb_assert (len > 0);
5420
5421 pst->number_of_dependencies = len;
5422 pst->dependencies = obstack_alloc (&objfile->objfile_obstack,
5423 len * sizeof (struct psymtab *));
5424 for (i = 0;
094b34ac 5425 VEC_iterate (dwarf2_per_cu_ptr, tu_group->t.tus, i, iter);
f4dc4d17
DE
5426 ++i)
5427 {
5428 pst->dependencies[i] = iter->v.psymtab;
796a7ff8 5429 iter->type_unit_group = tu_group;
f4dc4d17
DE
5430 }
5431
094b34ac 5432 VEC_free (dwarf2_per_cu_ptr, tu_group->t.tus);
348e048f
DE
5433
5434 return 1;
5435}
5436
5437/* Subroutine of dwarf2_build_psymtabs_hard to simplify it.
5438 Build partial symbol tables for the .debug_types comp-units. */
5439
5440static void
5441build_type_psymtabs (struct objfile *objfile)
5442{
0e50663e 5443 if (! create_all_type_units (objfile))
348e048f
DE
5444 return;
5445
f4dc4d17
DE
5446 build_type_unit_groups (build_type_psymtabs_reader, NULL);
5447
5448 /* Now that all TUs have been processed we can fill in the dependencies. */
5449 htab_traverse_noresize (dwarf2_per_objfile->type_unit_groups,
5450 build_type_psymtab_dependencies, NULL);
348e048f
DE
5451}
5452
60606b2c
TT
5453/* A cleanup function that clears objfile's psymtabs_addrmap field. */
5454
5455static void
5456psymtabs_addrmap_cleanup (void *o)
5457{
5458 struct objfile *objfile = o;
ec61707d 5459
60606b2c
TT
5460 objfile->psymtabs_addrmap = NULL;
5461}
5462
95554aad
TT
5463/* Compute the 'user' field for each psymtab in OBJFILE. */
5464
5465static void
5466set_partial_user (struct objfile *objfile)
5467{
5468 int i;
5469
5470 for (i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
5471 {
5472 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
5473 struct partial_symtab *pst = per_cu->v.psymtab;
5474 int j;
5475
36586728
TT
5476 if (pst == NULL)
5477 continue;
5478
95554aad
TT
5479 for (j = 0; j < pst->number_of_dependencies; ++j)
5480 {
5481 /* Set the 'user' field only if it is not already set. */
5482 if (pst->dependencies[j]->user == NULL)
5483 pst->dependencies[j]->user = pst;
5484 }
5485 }
5486}
5487
93311388
DE
5488/* Build the partial symbol table by doing a quick pass through the
5489 .debug_info and .debug_abbrev sections. */
72bf9492 5490
93311388 5491static void
c67a9c90 5492dwarf2_build_psymtabs_hard (struct objfile *objfile)
93311388 5493{
60606b2c
TT
5494 struct cleanup *back_to, *addrmap_cleanup;
5495 struct obstack temp_obstack;
21b2bd31 5496 int i;
93311388 5497
45cfd468
DE
5498 if (dwarf2_read_debug)
5499 {
5500 fprintf_unfiltered (gdb_stdlog, "Building psymtabs of objfile %s ...\n",
5501 objfile->name);
5502 }
5503
98bfdba5
PA
5504 dwarf2_per_objfile->reading_partial_symbols = 1;
5505
be391dca 5506 dwarf2_read_section (objfile, &dwarf2_per_objfile->info);
91c24f0a 5507
93311388
DE
5508 /* Any cached compilation units will be linked by the per-objfile
5509 read_in_chain. Make sure to free them when we're done. */
5510 back_to = make_cleanup (free_cached_comp_units, NULL);
72bf9492 5511
348e048f
DE
5512 build_type_psymtabs (objfile);
5513
93311388 5514 create_all_comp_units (objfile);
c906108c 5515
60606b2c
TT
5516 /* Create a temporary address map on a temporary obstack. We later
5517 copy this to the final obstack. */
5518 obstack_init (&temp_obstack);
5519 make_cleanup_obstack_free (&temp_obstack);
5520 objfile->psymtabs_addrmap = addrmap_create_mutable (&temp_obstack);
5521 addrmap_cleanup = make_cleanup (psymtabs_addrmap_cleanup, objfile);
72bf9492 5522
21b2bd31 5523 for (i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
93311388 5524 {
21b2bd31 5525 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
aaa75496 5526
95554aad 5527 process_psymtab_comp_unit (per_cu, 0);
c906108c 5528 }
ff013f42 5529
95554aad
TT
5530 set_partial_user (objfile);
5531
ff013f42
JK
5532 objfile->psymtabs_addrmap = addrmap_create_fixed (objfile->psymtabs_addrmap,
5533 &objfile->objfile_obstack);
60606b2c 5534 discard_cleanups (addrmap_cleanup);
ff013f42 5535
ae038cb0 5536 do_cleanups (back_to);
45cfd468
DE
5537
5538 if (dwarf2_read_debug)
5539 fprintf_unfiltered (gdb_stdlog, "Done building psymtabs of %s\n",
5540 objfile->name);
ae038cb0
DJ
5541}
5542
3019eac3 5543/* die_reader_func for load_partial_comp_unit. */
ae038cb0
DJ
5544
5545static void
dee91e82
DE
5546load_partial_comp_unit_reader (const struct die_reader_specs *reader,
5547 gdb_byte *info_ptr,
5548 struct die_info *comp_unit_die,
5549 int has_children,
5550 void *data)
ae038cb0 5551{
dee91e82 5552 struct dwarf2_cu *cu = reader->cu;
ae038cb0 5553
95554aad 5554 prepare_one_comp_unit (cu, comp_unit_die, language_minimal);
ae038cb0 5555
ae038cb0
DJ
5556 /* Check if comp unit has_children.
5557 If so, read the rest of the partial symbols from this comp unit.
0963b4bd 5558 If not, there's no more debug_info for this comp unit. */
d85a05f0 5559 if (has_children)
dee91e82
DE
5560 load_partial_dies (reader, info_ptr, 0);
5561}
98bfdba5 5562
dee91e82
DE
5563/* Load the partial DIEs for a secondary CU into memory.
5564 This is also used when rereading a primary CU with load_all_dies. */
c5b7e1cb 5565
dee91e82
DE
5566static void
5567load_partial_comp_unit (struct dwarf2_per_cu_data *this_cu)
5568{
f4dc4d17
DE
5569 init_cutu_and_read_dies (this_cu, NULL, 1, 1,
5570 load_partial_comp_unit_reader, NULL);
ae038cb0
DJ
5571}
5572
ae038cb0 5573static void
36586728
TT
5574read_comp_units_from_section (struct objfile *objfile,
5575 struct dwarf2_section_info *section,
5576 unsigned int is_dwz,
5577 int *n_allocated,
5578 int *n_comp_units,
5579 struct dwarf2_per_cu_data ***all_comp_units)
ae038cb0 5580{
be391dca 5581 gdb_byte *info_ptr;
36586728 5582 bfd *abfd = section->asection->owner;
be391dca 5583
36586728 5584 dwarf2_read_section (objfile, section);
ae038cb0 5585
36586728 5586 info_ptr = section->buffer;
6e70227d 5587
36586728 5588 while (info_ptr < section->buffer + section->size)
ae038cb0 5589 {
c764a876 5590 unsigned int length, initial_length_size;
ae038cb0 5591 struct dwarf2_per_cu_data *this_cu;
b64f50a1 5592 sect_offset offset;
ae038cb0 5593
36586728 5594 offset.sect_off = info_ptr - section->buffer;
ae038cb0
DJ
5595
5596 /* Read just enough information to find out where the next
5597 compilation unit is. */
36586728 5598 length = read_initial_length (abfd, info_ptr, &initial_length_size);
ae038cb0
DJ
5599
5600 /* Save the compilation unit for later lookup. */
5601 this_cu = obstack_alloc (&objfile->objfile_obstack,
5602 sizeof (struct dwarf2_per_cu_data));
5603 memset (this_cu, 0, sizeof (*this_cu));
5604 this_cu->offset = offset;
c764a876 5605 this_cu->length = length + initial_length_size;
36586728 5606 this_cu->is_dwz = is_dwz;
9291a0cd 5607 this_cu->objfile = objfile;
36586728 5608 this_cu->info_or_types_section = section;
ae038cb0 5609
36586728 5610 if (*n_comp_units == *n_allocated)
ae038cb0 5611 {
36586728
TT
5612 *n_allocated *= 2;
5613 *all_comp_units = xrealloc (*all_comp_units,
5614 *n_allocated
5615 * sizeof (struct dwarf2_per_cu_data *));
ae038cb0 5616 }
36586728
TT
5617 (*all_comp_units)[*n_comp_units] = this_cu;
5618 ++*n_comp_units;
ae038cb0
DJ
5619
5620 info_ptr = info_ptr + this_cu->length;
5621 }
36586728
TT
5622}
5623
5624/* Create a list of all compilation units in OBJFILE.
5625 This is only done for -readnow and building partial symtabs. */
5626
5627static void
5628create_all_comp_units (struct objfile *objfile)
5629{
5630 int n_allocated;
5631 int n_comp_units;
5632 struct dwarf2_per_cu_data **all_comp_units;
5633
5634 n_comp_units = 0;
5635 n_allocated = 10;
5636 all_comp_units = xmalloc (n_allocated
5637 * sizeof (struct dwarf2_per_cu_data *));
5638
5639 read_comp_units_from_section (objfile, &dwarf2_per_objfile->info, 0,
5640 &n_allocated, &n_comp_units, &all_comp_units);
5641
5642 if (bfd_get_section_by_name (objfile->obfd, ".gnu_debugaltlink") != NULL)
5643 {
5644 struct dwz_file *dwz = dwarf2_get_dwz_file ();
5645
5646 read_comp_units_from_section (objfile, &dwz->info, 1,
5647 &n_allocated, &n_comp_units,
5648 &all_comp_units);
5649 }
ae038cb0
DJ
5650
5651 dwarf2_per_objfile->all_comp_units
5652 = obstack_alloc (&objfile->objfile_obstack,
5653 n_comp_units * sizeof (struct dwarf2_per_cu_data *));
5654 memcpy (dwarf2_per_objfile->all_comp_units, all_comp_units,
5655 n_comp_units * sizeof (struct dwarf2_per_cu_data *));
5656 xfree (all_comp_units);
5657 dwarf2_per_objfile->n_comp_units = n_comp_units;
c906108c
SS
5658}
5659
5734ee8b
DJ
5660/* Process all loaded DIEs for compilation unit CU, starting at
5661 FIRST_DIE. The caller should pass NEED_PC == 1 if the compilation
5662 unit DIE did not have PC info (DW_AT_low_pc and DW_AT_high_pc, or
5663 DW_AT_ranges). If NEED_PC is set, then this function will set
5664 *LOWPC and *HIGHPC to the lowest and highest PC values found in CU
5665 and record the covered ranges in the addrmap. */
c906108c 5666
72bf9492
DJ
5667static void
5668scan_partial_symbols (struct partial_die_info *first_die, CORE_ADDR *lowpc,
5734ee8b 5669 CORE_ADDR *highpc, int need_pc, struct dwarf2_cu *cu)
c906108c 5670{
72bf9492 5671 struct partial_die_info *pdi;
c906108c 5672
91c24f0a
DC
5673 /* Now, march along the PDI's, descending into ones which have
5674 interesting children but skipping the children of the other ones,
5675 until we reach the end of the compilation unit. */
c906108c 5676
72bf9492 5677 pdi = first_die;
91c24f0a 5678
72bf9492
DJ
5679 while (pdi != NULL)
5680 {
5681 fixup_partial_die (pdi, cu);
c906108c 5682
f55ee35c 5683 /* Anonymous namespaces or modules have no name but have interesting
91c24f0a
DC
5684 children, so we need to look at them. Ditto for anonymous
5685 enums. */
933c6fe4 5686
72bf9492 5687 if (pdi->name != NULL || pdi->tag == DW_TAG_namespace
95554aad
TT
5688 || pdi->tag == DW_TAG_module || pdi->tag == DW_TAG_enumeration_type
5689 || pdi->tag == DW_TAG_imported_unit)
c906108c 5690 {
72bf9492 5691 switch (pdi->tag)
c906108c
SS
5692 {
5693 case DW_TAG_subprogram:
5734ee8b 5694 add_partial_subprogram (pdi, lowpc, highpc, need_pc, cu);
c906108c 5695 break;
72929c62 5696 case DW_TAG_constant:
c906108c
SS
5697 case DW_TAG_variable:
5698 case DW_TAG_typedef:
91c24f0a 5699 case DW_TAG_union_type:
72bf9492 5700 if (!pdi->is_declaration)
63d06c5c 5701 {
72bf9492 5702 add_partial_symbol (pdi, cu);
63d06c5c
DC
5703 }
5704 break;
c906108c 5705 case DW_TAG_class_type:
680b30c7 5706 case DW_TAG_interface_type:
c906108c 5707 case DW_TAG_structure_type:
72bf9492 5708 if (!pdi->is_declaration)
c906108c 5709 {
72bf9492 5710 add_partial_symbol (pdi, cu);
c906108c
SS
5711 }
5712 break;
91c24f0a 5713 case DW_TAG_enumeration_type:
72bf9492
DJ
5714 if (!pdi->is_declaration)
5715 add_partial_enumeration (pdi, cu);
c906108c
SS
5716 break;
5717 case DW_TAG_base_type:
a02abb62 5718 case DW_TAG_subrange_type:
c906108c 5719 /* File scope base type definitions are added to the partial
c5aa993b 5720 symbol table. */
72bf9492 5721 add_partial_symbol (pdi, cu);
c906108c 5722 break;
d9fa45fe 5723 case DW_TAG_namespace:
5734ee8b 5724 add_partial_namespace (pdi, lowpc, highpc, need_pc, cu);
91c24f0a 5725 break;
5d7cb8df
JK
5726 case DW_TAG_module:
5727 add_partial_module (pdi, lowpc, highpc, need_pc, cu);
5728 break;
95554aad
TT
5729 case DW_TAG_imported_unit:
5730 {
5731 struct dwarf2_per_cu_data *per_cu;
5732
f4dc4d17
DE
5733 /* For now we don't handle imported units in type units. */
5734 if (cu->per_cu->is_debug_types)
5735 {
5736 error (_("Dwarf Error: DW_TAG_imported_unit is not"
5737 " supported in type units [in module %s]"),
5738 cu->objfile->name);
5739 }
5740
95554aad 5741 per_cu = dwarf2_find_containing_comp_unit (pdi->d.offset,
36586728 5742 pdi->is_dwz,
95554aad
TT
5743 cu->objfile);
5744
5745 /* Go read the partial unit, if needed. */
5746 if (per_cu->v.psymtab == NULL)
5747 process_psymtab_comp_unit (per_cu, 1);
5748
f4dc4d17 5749 VEC_safe_push (dwarf2_per_cu_ptr,
796a7ff8 5750 cu->per_cu->imported_symtabs, per_cu);
95554aad
TT
5751 }
5752 break;
c906108c
SS
5753 default:
5754 break;
5755 }
5756 }
5757
72bf9492
DJ
5758 /* If the die has a sibling, skip to the sibling. */
5759
5760 pdi = pdi->die_sibling;
5761 }
5762}
5763
5764/* Functions used to compute the fully scoped name of a partial DIE.
91c24f0a 5765
72bf9492 5766 Normally, this is simple. For C++, the parent DIE's fully scoped
987504bb
JJ
5767 name is concatenated with "::" and the partial DIE's name. For
5768 Java, the same thing occurs except that "." is used instead of "::".
72bf9492
DJ
5769 Enumerators are an exception; they use the scope of their parent
5770 enumeration type, i.e. the name of the enumeration type is not
5771 prepended to the enumerator.
91c24f0a 5772
72bf9492
DJ
5773 There are two complexities. One is DW_AT_specification; in this
5774 case "parent" means the parent of the target of the specification,
5775 instead of the direct parent of the DIE. The other is compilers
5776 which do not emit DW_TAG_namespace; in this case we try to guess
5777 the fully qualified name of structure types from their members'
5778 linkage names. This must be done using the DIE's children rather
5779 than the children of any DW_AT_specification target. We only need
5780 to do this for structures at the top level, i.e. if the target of
5781 any DW_AT_specification (if any; otherwise the DIE itself) does not
5782 have a parent. */
5783
5784/* Compute the scope prefix associated with PDI's parent, in
5785 compilation unit CU. The result will be allocated on CU's
5786 comp_unit_obstack, or a copy of the already allocated PDI->NAME
5787 field. NULL is returned if no prefix is necessary. */
15d034d0 5788static const char *
72bf9492
DJ
5789partial_die_parent_scope (struct partial_die_info *pdi,
5790 struct dwarf2_cu *cu)
5791{
15d034d0 5792 const char *grandparent_scope;
72bf9492 5793 struct partial_die_info *parent, *real_pdi;
91c24f0a 5794
72bf9492
DJ
5795 /* We need to look at our parent DIE; if we have a DW_AT_specification,
5796 then this means the parent of the specification DIE. */
5797
5798 real_pdi = pdi;
72bf9492 5799 while (real_pdi->has_specification)
36586728
TT
5800 real_pdi = find_partial_die (real_pdi->spec_offset,
5801 real_pdi->spec_is_dwz, cu);
72bf9492
DJ
5802
5803 parent = real_pdi->die_parent;
5804 if (parent == NULL)
5805 return NULL;
5806
5807 if (parent->scope_set)
5808 return parent->scope;
5809
5810 fixup_partial_die (parent, cu);
5811
10b3939b 5812 grandparent_scope = partial_die_parent_scope (parent, cu);
72bf9492 5813
acebe513
UW
5814 /* GCC 4.0 and 4.1 had a bug (PR c++/28460) where they generated bogus
5815 DW_TAG_namespace DIEs with a name of "::" for the global namespace.
5816 Work around this problem here. */
5817 if (cu->language == language_cplus
6e70227d 5818 && parent->tag == DW_TAG_namespace
acebe513
UW
5819 && strcmp (parent->name, "::") == 0
5820 && grandparent_scope == NULL)
5821 {
5822 parent->scope = NULL;
5823 parent->scope_set = 1;
5824 return NULL;
5825 }
5826
9c6c53f7
SA
5827 if (pdi->tag == DW_TAG_enumerator)
5828 /* Enumerators should not get the name of the enumeration as a prefix. */
5829 parent->scope = grandparent_scope;
5830 else if (parent->tag == DW_TAG_namespace
f55ee35c 5831 || parent->tag == DW_TAG_module
72bf9492
DJ
5832 || parent->tag == DW_TAG_structure_type
5833 || parent->tag == DW_TAG_class_type
680b30c7 5834 || parent->tag == DW_TAG_interface_type
ceeb3d5a
TT
5835 || parent->tag == DW_TAG_union_type
5836 || parent->tag == DW_TAG_enumeration_type)
72bf9492
DJ
5837 {
5838 if (grandparent_scope == NULL)
5839 parent->scope = parent->name;
5840 else
3e43a32a
MS
5841 parent->scope = typename_concat (&cu->comp_unit_obstack,
5842 grandparent_scope,
f55ee35c 5843 parent->name, 0, cu);
72bf9492 5844 }
72bf9492
DJ
5845 else
5846 {
5847 /* FIXME drow/2004-04-01: What should we be doing with
5848 function-local names? For partial symbols, we should probably be
5849 ignoring them. */
5850 complaint (&symfile_complaints,
e2e0b3e5 5851 _("unhandled containing DIE tag %d for DIE at %d"),
b64f50a1 5852 parent->tag, pdi->offset.sect_off);
72bf9492 5853 parent->scope = grandparent_scope;
c906108c
SS
5854 }
5855
72bf9492
DJ
5856 parent->scope_set = 1;
5857 return parent->scope;
5858}
5859
5860/* Return the fully scoped name associated with PDI, from compilation unit
5861 CU. The result will be allocated with malloc. */
4568ecf9 5862
72bf9492
DJ
5863static char *
5864partial_die_full_name (struct partial_die_info *pdi,
5865 struct dwarf2_cu *cu)
5866{
15d034d0 5867 const char *parent_scope;
72bf9492 5868
98bfdba5
PA
5869 /* If this is a template instantiation, we can not work out the
5870 template arguments from partial DIEs. So, unfortunately, we have
5871 to go through the full DIEs. At least any work we do building
5872 types here will be reused if full symbols are loaded later. */
5873 if (pdi->has_template_arguments)
5874 {
5875 fixup_partial_die (pdi, cu);
5876
5877 if (pdi->name != NULL && strchr (pdi->name, '<') == NULL)
5878 {
5879 struct die_info *die;
5880 struct attribute attr;
5881 struct dwarf2_cu *ref_cu = cu;
5882
b64f50a1 5883 /* DW_FORM_ref_addr is using section offset. */
98bfdba5
PA
5884 attr.name = 0;
5885 attr.form = DW_FORM_ref_addr;
4568ecf9 5886 attr.u.unsnd = pdi->offset.sect_off;
98bfdba5
PA
5887 die = follow_die_ref (NULL, &attr, &ref_cu);
5888
5889 return xstrdup (dwarf2_full_name (NULL, die, ref_cu));
5890 }
5891 }
5892
72bf9492
DJ
5893 parent_scope = partial_die_parent_scope (pdi, cu);
5894 if (parent_scope == NULL)
5895 return NULL;
5896 else
f55ee35c 5897 return typename_concat (NULL, parent_scope, pdi->name, 0, cu);
c906108c
SS
5898}
5899
5900static void
72bf9492 5901add_partial_symbol (struct partial_die_info *pdi, struct dwarf2_cu *cu)
c906108c 5902{
e7c27a73 5903 struct objfile *objfile = cu->objfile;
c906108c 5904 CORE_ADDR addr = 0;
15d034d0 5905 const char *actual_name = NULL;
e142c38c 5906 CORE_ADDR baseaddr;
15d034d0 5907 char *built_actual_name;
e142c38c
DJ
5908
5909 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
c906108c 5910
15d034d0
TT
5911 built_actual_name = partial_die_full_name (pdi, cu);
5912 if (built_actual_name != NULL)
5913 actual_name = built_actual_name;
63d06c5c 5914
72bf9492
DJ
5915 if (actual_name == NULL)
5916 actual_name = pdi->name;
5917
c906108c
SS
5918 switch (pdi->tag)
5919 {
5920 case DW_TAG_subprogram:
2cfa0c8d 5921 if (pdi->is_external || cu->language == language_ada)
c906108c 5922 {
2cfa0c8d
JB
5923 /* brobecker/2007-12-26: Normally, only "external" DIEs are part
5924 of the global scope. But in Ada, we want to be able to access
5925 nested procedures globally. So all Ada subprograms are stored
5926 in the global scope. */
f47fb265 5927 /* prim_record_minimal_symbol (actual_name, pdi->lowpc + baseaddr,
c5aa993b 5928 mst_text, objfile); */
f47fb265 5929 add_psymbol_to_list (actual_name, strlen (actual_name),
15d034d0 5930 built_actual_name != NULL,
f47fb265
MS
5931 VAR_DOMAIN, LOC_BLOCK,
5932 &objfile->global_psymbols,
5933 0, pdi->lowpc + baseaddr,
5934 cu->language, objfile);
c906108c
SS
5935 }
5936 else
5937 {
f47fb265 5938 /* prim_record_minimal_symbol (actual_name, pdi->lowpc + baseaddr,
c5aa993b 5939 mst_file_text, objfile); */
f47fb265 5940 add_psymbol_to_list (actual_name, strlen (actual_name),
15d034d0 5941 built_actual_name != NULL,
f47fb265
MS
5942 VAR_DOMAIN, LOC_BLOCK,
5943 &objfile->static_psymbols,
5944 0, pdi->lowpc + baseaddr,
5945 cu->language, objfile);
c906108c
SS
5946 }
5947 break;
72929c62
JB
5948 case DW_TAG_constant:
5949 {
5950 struct psymbol_allocation_list *list;
5951
5952 if (pdi->is_external)
5953 list = &objfile->global_psymbols;
5954 else
5955 list = &objfile->static_psymbols;
f47fb265 5956 add_psymbol_to_list (actual_name, strlen (actual_name),
15d034d0 5957 built_actual_name != NULL, VAR_DOMAIN, LOC_STATIC,
f47fb265 5958 list, 0, 0, cu->language, objfile);
72929c62
JB
5959 }
5960 break;
c906108c 5961 case DW_TAG_variable:
95554aad
TT
5962 if (pdi->d.locdesc)
5963 addr = decode_locdesc (pdi->d.locdesc, cu);
caac4577 5964
95554aad 5965 if (pdi->d.locdesc
caac4577
JG
5966 && addr == 0
5967 && !dwarf2_per_objfile->has_section_at_zero)
5968 {
5969 /* A global or static variable may also have been stripped
5970 out by the linker if unused, in which case its address
5971 will be nullified; do not add such variables into partial
5972 symbol table then. */
5973 }
5974 else if (pdi->is_external)
c906108c
SS
5975 {
5976 /* Global Variable.
5977 Don't enter into the minimal symbol tables as there is
5978 a minimal symbol table entry from the ELF symbols already.
5979 Enter into partial symbol table if it has a location
5980 descriptor or a type.
5981 If the location descriptor is missing, new_symbol will create
5982 a LOC_UNRESOLVED symbol, the address of the variable will then
5983 be determined from the minimal symbol table whenever the variable
5984 is referenced.
5985 The address for the partial symbol table entry is not
5986 used by GDB, but it comes in handy for debugging partial symbol
5987 table building. */
5988
95554aad 5989 if (pdi->d.locdesc || pdi->has_type)
f47fb265 5990 add_psymbol_to_list (actual_name, strlen (actual_name),
15d034d0 5991 built_actual_name != NULL,
f47fb265
MS
5992 VAR_DOMAIN, LOC_STATIC,
5993 &objfile->global_psymbols,
5994 0, addr + baseaddr,
5995 cu->language, objfile);
c906108c
SS
5996 }
5997 else
5998 {
0963b4bd 5999 /* Static Variable. Skip symbols without location descriptors. */
95554aad 6000 if (pdi->d.locdesc == NULL)
decbce07 6001 {
15d034d0 6002 xfree (built_actual_name);
decbce07
MS
6003 return;
6004 }
f47fb265 6005 /* prim_record_minimal_symbol (actual_name, addr + baseaddr,
c5aa993b 6006 mst_file_data, objfile); */
f47fb265 6007 add_psymbol_to_list (actual_name, strlen (actual_name),
15d034d0 6008 built_actual_name != NULL,
f47fb265
MS
6009 VAR_DOMAIN, LOC_STATIC,
6010 &objfile->static_psymbols,
6011 0, addr + baseaddr,
6012 cu->language, objfile);
c906108c
SS
6013 }
6014 break;
6015 case DW_TAG_typedef:
6016 case DW_TAG_base_type:
a02abb62 6017 case DW_TAG_subrange_type:
38d518c9 6018 add_psymbol_to_list (actual_name, strlen (actual_name),
15d034d0 6019 built_actual_name != NULL,
176620f1 6020 VAR_DOMAIN, LOC_TYPEDEF,
c906108c 6021 &objfile->static_psymbols,
e142c38c 6022 0, (CORE_ADDR) 0, cu->language, objfile);
c906108c 6023 break;
72bf9492
DJ
6024 case DW_TAG_namespace:
6025 add_psymbol_to_list (actual_name, strlen (actual_name),
15d034d0 6026 built_actual_name != NULL,
72bf9492
DJ
6027 VAR_DOMAIN, LOC_TYPEDEF,
6028 &objfile->global_psymbols,
6029 0, (CORE_ADDR) 0, cu->language, objfile);
6030 break;
c906108c 6031 case DW_TAG_class_type:
680b30c7 6032 case DW_TAG_interface_type:
c906108c
SS
6033 case DW_TAG_structure_type:
6034 case DW_TAG_union_type:
6035 case DW_TAG_enumeration_type:
fa4028e9
JB
6036 /* Skip external references. The DWARF standard says in the section
6037 about "Structure, Union, and Class Type Entries": "An incomplete
6038 structure, union or class type is represented by a structure,
6039 union or class entry that does not have a byte size attribute
6040 and that has a DW_AT_declaration attribute." */
6041 if (!pdi->has_byte_size && pdi->is_declaration)
decbce07 6042 {
15d034d0 6043 xfree (built_actual_name);
decbce07
MS
6044 return;
6045 }
fa4028e9 6046
63d06c5c
DC
6047 /* NOTE: carlton/2003-10-07: See comment in new_symbol about
6048 static vs. global. */
38d518c9 6049 add_psymbol_to_list (actual_name, strlen (actual_name),
15d034d0 6050 built_actual_name != NULL,
176620f1 6051 STRUCT_DOMAIN, LOC_TYPEDEF,
987504bb
JJ
6052 (cu->language == language_cplus
6053 || cu->language == language_java)
63d06c5c
DC
6054 ? &objfile->global_psymbols
6055 : &objfile->static_psymbols,
e142c38c 6056 0, (CORE_ADDR) 0, cu->language, objfile);
c906108c 6057
c906108c
SS
6058 break;
6059 case DW_TAG_enumerator:
38d518c9 6060 add_psymbol_to_list (actual_name, strlen (actual_name),
15d034d0 6061 built_actual_name != NULL,
176620f1 6062 VAR_DOMAIN, LOC_CONST,
987504bb
JJ
6063 (cu->language == language_cplus
6064 || cu->language == language_java)
f6fe98ef
DJ
6065 ? &objfile->global_psymbols
6066 : &objfile->static_psymbols,
e142c38c 6067 0, (CORE_ADDR) 0, cu->language, objfile);
c906108c
SS
6068 break;
6069 default:
6070 break;
6071 }
5c4e30ca 6072
15d034d0 6073 xfree (built_actual_name);
c906108c
SS
6074}
6075
5c4e30ca
DC
6076/* Read a partial die corresponding to a namespace; also, add a symbol
6077 corresponding to that namespace to the symbol table. NAMESPACE is
6078 the name of the enclosing namespace. */
91c24f0a 6079
72bf9492
DJ
6080static void
6081add_partial_namespace (struct partial_die_info *pdi,
91c24f0a 6082 CORE_ADDR *lowpc, CORE_ADDR *highpc,
5734ee8b 6083 int need_pc, struct dwarf2_cu *cu)
91c24f0a 6084{
72bf9492 6085 /* Add a symbol for the namespace. */
e7c27a73 6086
72bf9492 6087 add_partial_symbol (pdi, cu);
5c4e30ca
DC
6088
6089 /* Now scan partial symbols in that namespace. */
6090
91c24f0a 6091 if (pdi->has_children)
5734ee8b 6092 scan_partial_symbols (pdi->die_child, lowpc, highpc, need_pc, cu);
91c24f0a
DC
6093}
6094
5d7cb8df
JK
6095/* Read a partial die corresponding to a Fortran module. */
6096
6097static void
6098add_partial_module (struct partial_die_info *pdi, CORE_ADDR *lowpc,
6099 CORE_ADDR *highpc, int need_pc, struct dwarf2_cu *cu)
6100{
f55ee35c 6101 /* Now scan partial symbols in that module. */
5d7cb8df
JK
6102
6103 if (pdi->has_children)
6104 scan_partial_symbols (pdi->die_child, lowpc, highpc, need_pc, cu);
6105}
6106
bc30ff58
JB
6107/* Read a partial die corresponding to a subprogram and create a partial
6108 symbol for that subprogram. When the CU language allows it, this
6109 routine also defines a partial symbol for each nested subprogram
6110 that this subprogram contains.
6e70227d 6111
bc30ff58
JB
6112 DIE my also be a lexical block, in which case we simply search
6113 recursively for suprograms defined inside that lexical block.
6114 Again, this is only performed when the CU language allows this
6115 type of definitions. */
6116
6117static void
6118add_partial_subprogram (struct partial_die_info *pdi,
6119 CORE_ADDR *lowpc, CORE_ADDR *highpc,
5734ee8b 6120 int need_pc, struct dwarf2_cu *cu)
bc30ff58
JB
6121{
6122 if (pdi->tag == DW_TAG_subprogram)
6123 {
6124 if (pdi->has_pc_info)
6125 {
6126 if (pdi->lowpc < *lowpc)
6127 *lowpc = pdi->lowpc;
6128 if (pdi->highpc > *highpc)
6129 *highpc = pdi->highpc;
5734ee8b
DJ
6130 if (need_pc)
6131 {
6132 CORE_ADDR baseaddr;
6133 struct objfile *objfile = cu->objfile;
6134
6135 baseaddr = ANOFFSET (objfile->section_offsets,
6136 SECT_OFF_TEXT (objfile));
6137 addrmap_set_empty (objfile->psymtabs_addrmap,
01637564
DE
6138 pdi->lowpc + baseaddr,
6139 pdi->highpc - 1 + baseaddr,
9291a0cd 6140 cu->per_cu->v.psymtab);
5734ee8b 6141 }
481860b3
GB
6142 }
6143
6144 if (pdi->has_pc_info || (!pdi->is_external && pdi->may_be_inlined))
6145 {
bc30ff58 6146 if (!pdi->is_declaration)
e8d05480
JB
6147 /* Ignore subprogram DIEs that do not have a name, they are
6148 illegal. Do not emit a complaint at this point, we will
6149 do so when we convert this psymtab into a symtab. */
6150 if (pdi->name)
6151 add_partial_symbol (pdi, cu);
bc30ff58
JB
6152 }
6153 }
6e70227d 6154
bc30ff58
JB
6155 if (! pdi->has_children)
6156 return;
6157
6158 if (cu->language == language_ada)
6159 {
6160 pdi = pdi->die_child;
6161 while (pdi != NULL)
6162 {
6163 fixup_partial_die (pdi, cu);
6164 if (pdi->tag == DW_TAG_subprogram
6165 || pdi->tag == DW_TAG_lexical_block)
5734ee8b 6166 add_partial_subprogram (pdi, lowpc, highpc, need_pc, cu);
bc30ff58
JB
6167 pdi = pdi->die_sibling;
6168 }
6169 }
6170}
6171
91c24f0a
DC
6172/* Read a partial die corresponding to an enumeration type. */
6173
72bf9492
DJ
6174static void
6175add_partial_enumeration (struct partial_die_info *enum_pdi,
6176 struct dwarf2_cu *cu)
91c24f0a 6177{
72bf9492 6178 struct partial_die_info *pdi;
91c24f0a
DC
6179
6180 if (enum_pdi->name != NULL)
72bf9492
DJ
6181 add_partial_symbol (enum_pdi, cu);
6182
6183 pdi = enum_pdi->die_child;
6184 while (pdi)
91c24f0a 6185 {
72bf9492 6186 if (pdi->tag != DW_TAG_enumerator || pdi->name == NULL)
e2e0b3e5 6187 complaint (&symfile_complaints, _("malformed enumerator DIE ignored"));
91c24f0a 6188 else
72bf9492
DJ
6189 add_partial_symbol (pdi, cu);
6190 pdi = pdi->die_sibling;
91c24f0a 6191 }
91c24f0a
DC
6192}
6193
6caca83c
CC
6194/* Return the initial uleb128 in the die at INFO_PTR. */
6195
6196static unsigned int
6197peek_abbrev_code (bfd *abfd, gdb_byte *info_ptr)
6198{
6199 unsigned int bytes_read;
6200
6201 return read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
6202}
6203
4bb7a0a7
DJ
6204/* Read the initial uleb128 in the die at INFO_PTR in compilation unit CU.
6205 Return the corresponding abbrev, or NULL if the number is zero (indicating
6206 an empty DIE). In either case *BYTES_READ will be set to the length of
6207 the initial number. */
6208
6209static struct abbrev_info *
fe1b8b76 6210peek_die_abbrev (gdb_byte *info_ptr, unsigned int *bytes_read,
891d2f0b 6211 struct dwarf2_cu *cu)
4bb7a0a7
DJ
6212{
6213 bfd *abfd = cu->objfile->obfd;
6214 unsigned int abbrev_number;
6215 struct abbrev_info *abbrev;
6216
6217 abbrev_number = read_unsigned_leb128 (abfd, info_ptr, bytes_read);
6218
6219 if (abbrev_number == 0)
6220 return NULL;
6221
433df2d4 6222 abbrev = abbrev_table_lookup_abbrev (cu->abbrev_table, abbrev_number);
4bb7a0a7
DJ
6223 if (!abbrev)
6224 {
3e43a32a
MS
6225 error (_("Dwarf Error: Could not find abbrev number %d [in module %s]"),
6226 abbrev_number, bfd_get_filename (abfd));
4bb7a0a7
DJ
6227 }
6228
6229 return abbrev;
6230}
6231
93311388
DE
6232/* Scan the debug information for CU starting at INFO_PTR in buffer BUFFER.
6233 Returns a pointer to the end of a series of DIEs, terminated by an empty
4bb7a0a7
DJ
6234 DIE. Any children of the skipped DIEs will also be skipped. */
6235
fe1b8b76 6236static gdb_byte *
dee91e82 6237skip_children (const struct die_reader_specs *reader, gdb_byte *info_ptr)
4bb7a0a7 6238{
dee91e82 6239 struct dwarf2_cu *cu = reader->cu;
4bb7a0a7
DJ
6240 struct abbrev_info *abbrev;
6241 unsigned int bytes_read;
6242
6243 while (1)
6244 {
6245 abbrev = peek_die_abbrev (info_ptr, &bytes_read, cu);
6246 if (abbrev == NULL)
6247 return info_ptr + bytes_read;
6248 else
dee91e82 6249 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
4bb7a0a7
DJ
6250 }
6251}
6252
93311388
DE
6253/* Scan the debug information for CU starting at INFO_PTR in buffer BUFFER.
6254 INFO_PTR should point just after the initial uleb128 of a DIE, and the
4bb7a0a7
DJ
6255 abbrev corresponding to that skipped uleb128 should be passed in
6256 ABBREV. Returns a pointer to this DIE's sibling, skipping any
6257 children. */
6258
fe1b8b76 6259static gdb_byte *
dee91e82
DE
6260skip_one_die (const struct die_reader_specs *reader, gdb_byte *info_ptr,
6261 struct abbrev_info *abbrev)
4bb7a0a7
DJ
6262{
6263 unsigned int bytes_read;
6264 struct attribute attr;
dee91e82
DE
6265 bfd *abfd = reader->abfd;
6266 struct dwarf2_cu *cu = reader->cu;
6267 gdb_byte *buffer = reader->buffer;
f664829e
DE
6268 const gdb_byte *buffer_end = reader->buffer_end;
6269 gdb_byte *start_info_ptr = info_ptr;
4bb7a0a7
DJ
6270 unsigned int form, i;
6271
6272 for (i = 0; i < abbrev->num_attrs; i++)
6273 {
6274 /* The only abbrev we care about is DW_AT_sibling. */
6275 if (abbrev->attrs[i].name == DW_AT_sibling)
6276 {
dee91e82 6277 read_attribute (reader, &attr, &abbrev->attrs[i], info_ptr);
4bb7a0a7 6278 if (attr.form == DW_FORM_ref_addr)
3e43a32a
MS
6279 complaint (&symfile_complaints,
6280 _("ignoring absolute DW_AT_sibling"));
4bb7a0a7 6281 else
b64f50a1 6282 return buffer + dwarf2_get_ref_die_offset (&attr).sect_off;
4bb7a0a7
DJ
6283 }
6284
6285 /* If it isn't DW_AT_sibling, skip this attribute. */
6286 form = abbrev->attrs[i].form;
6287 skip_attribute:
6288 switch (form)
6289 {
4bb7a0a7 6290 case DW_FORM_ref_addr:
ae411497
TT
6291 /* In DWARF 2, DW_FORM_ref_addr is address sized; in DWARF 3
6292 and later it is offset sized. */
6293 if (cu->header.version == 2)
6294 info_ptr += cu->header.addr_size;
6295 else
6296 info_ptr += cu->header.offset_size;
6297 break;
36586728
TT
6298 case DW_FORM_GNU_ref_alt:
6299 info_ptr += cu->header.offset_size;
6300 break;
ae411497 6301 case DW_FORM_addr:
4bb7a0a7
DJ
6302 info_ptr += cu->header.addr_size;
6303 break;
6304 case DW_FORM_data1:
6305 case DW_FORM_ref1:
6306 case DW_FORM_flag:
6307 info_ptr += 1;
6308 break;
2dc7f7b3
TT
6309 case DW_FORM_flag_present:
6310 break;
4bb7a0a7
DJ
6311 case DW_FORM_data2:
6312 case DW_FORM_ref2:
6313 info_ptr += 2;
6314 break;
6315 case DW_FORM_data4:
6316 case DW_FORM_ref4:
6317 info_ptr += 4;
6318 break;
6319 case DW_FORM_data8:
6320 case DW_FORM_ref8:
55f1336d 6321 case DW_FORM_ref_sig8:
4bb7a0a7
DJ
6322 info_ptr += 8;
6323 break;
6324 case DW_FORM_string:
9b1c24c8 6325 read_direct_string (abfd, info_ptr, &bytes_read);
4bb7a0a7
DJ
6326 info_ptr += bytes_read;
6327 break;
2dc7f7b3 6328 case DW_FORM_sec_offset:
4bb7a0a7 6329 case DW_FORM_strp:
36586728 6330 case DW_FORM_GNU_strp_alt:
4bb7a0a7
DJ
6331 info_ptr += cu->header.offset_size;
6332 break;
2dc7f7b3 6333 case DW_FORM_exprloc:
4bb7a0a7
DJ
6334 case DW_FORM_block:
6335 info_ptr += read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
6336 info_ptr += bytes_read;
6337 break;
6338 case DW_FORM_block1:
6339 info_ptr += 1 + read_1_byte (abfd, info_ptr);
6340 break;
6341 case DW_FORM_block2:
6342 info_ptr += 2 + read_2_bytes (abfd, info_ptr);
6343 break;
6344 case DW_FORM_block4:
6345 info_ptr += 4 + read_4_bytes (abfd, info_ptr);
6346 break;
6347 case DW_FORM_sdata:
6348 case DW_FORM_udata:
6349 case DW_FORM_ref_udata:
3019eac3
DE
6350 case DW_FORM_GNU_addr_index:
6351 case DW_FORM_GNU_str_index:
f664829e 6352 info_ptr = (gdb_byte *) safe_skip_leb128 (info_ptr, buffer_end);
4bb7a0a7
DJ
6353 break;
6354 case DW_FORM_indirect:
6355 form = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
6356 info_ptr += bytes_read;
6357 /* We need to continue parsing from here, so just go back to
6358 the top. */
6359 goto skip_attribute;
6360
6361 default:
3e43a32a
MS
6362 error (_("Dwarf Error: Cannot handle %s "
6363 "in DWARF reader [in module %s]"),
4bb7a0a7
DJ
6364 dwarf_form_name (form),
6365 bfd_get_filename (abfd));
6366 }
6367 }
6368
6369 if (abbrev->has_children)
dee91e82 6370 return skip_children (reader, info_ptr);
4bb7a0a7
DJ
6371 else
6372 return info_ptr;
6373}
6374
93311388 6375/* Locate ORIG_PDI's sibling.
dee91e82 6376 INFO_PTR should point to the start of the next DIE after ORIG_PDI. */
91c24f0a 6377
fe1b8b76 6378static gdb_byte *
dee91e82
DE
6379locate_pdi_sibling (const struct die_reader_specs *reader,
6380 struct partial_die_info *orig_pdi,
6381 gdb_byte *info_ptr)
91c24f0a
DC
6382{
6383 /* Do we know the sibling already? */
72bf9492 6384
91c24f0a
DC
6385 if (orig_pdi->sibling)
6386 return orig_pdi->sibling;
6387
6388 /* Are there any children to deal with? */
6389
6390 if (!orig_pdi->has_children)
6391 return info_ptr;
6392
4bb7a0a7 6393 /* Skip the children the long way. */
91c24f0a 6394
dee91e82 6395 return skip_children (reader, info_ptr);
91c24f0a
DC
6396}
6397
257e7a09 6398/* Expand this partial symbol table into a full symbol table. SELF is
442e4d9c 6399 not NULL. */
c906108c
SS
6400
6401static void
257e7a09
YQ
6402dwarf2_read_symtab (struct partial_symtab *self,
6403 struct objfile *objfile)
c906108c 6404{
257e7a09 6405 if (self->readin)
c906108c 6406 {
442e4d9c 6407 warning (_("bug: psymtab for %s is already read in."),
257e7a09 6408 self->filename);
442e4d9c
YQ
6409 }
6410 else
6411 {
6412 if (info_verbose)
c906108c 6413 {
442e4d9c 6414 printf_filtered (_("Reading in symbols for %s..."),
257e7a09 6415 self->filename);
442e4d9c 6416 gdb_flush (gdb_stdout);
c906108c 6417 }
c906108c 6418
442e4d9c
YQ
6419 /* Restore our global data. */
6420 dwarf2_per_objfile = objfile_data (objfile, dwarf2_objfile_data_key);
10b3939b 6421
442e4d9c
YQ
6422 /* If this psymtab is constructed from a debug-only objfile, the
6423 has_section_at_zero flag will not necessarily be correct. We
6424 can get the correct value for this flag by looking at the data
6425 associated with the (presumably stripped) associated objfile. */
6426 if (objfile->separate_debug_objfile_backlink)
6427 {
6428 struct dwarf2_per_objfile *dpo_backlink
6429 = objfile_data (objfile->separate_debug_objfile_backlink,
6430 dwarf2_objfile_data_key);
9a619af0 6431
442e4d9c
YQ
6432 dwarf2_per_objfile->has_section_at_zero
6433 = dpo_backlink->has_section_at_zero;
6434 }
b2ab525c 6435
442e4d9c 6436 dwarf2_per_objfile->reading_partial_symbols = 0;
98bfdba5 6437
257e7a09 6438 psymtab_to_symtab_1 (self);
c906108c 6439
442e4d9c
YQ
6440 /* Finish up the debug error message. */
6441 if (info_verbose)
6442 printf_filtered (_("done.\n"));
c906108c 6443 }
95554aad
TT
6444
6445 process_cu_includes ();
c906108c 6446}
9cdd5dbd
DE
6447\f
6448/* Reading in full CUs. */
c906108c 6449
10b3939b
DJ
6450/* Add PER_CU to the queue. */
6451
6452static void
95554aad
TT
6453queue_comp_unit (struct dwarf2_per_cu_data *per_cu,
6454 enum language pretend_language)
10b3939b
DJ
6455{
6456 struct dwarf2_queue_item *item;
6457
6458 per_cu->queued = 1;
6459 item = xmalloc (sizeof (*item));
6460 item->per_cu = per_cu;
95554aad 6461 item->pretend_language = pretend_language;
10b3939b
DJ
6462 item->next = NULL;
6463
6464 if (dwarf2_queue == NULL)
6465 dwarf2_queue = item;
6466 else
6467 dwarf2_queue_tail->next = item;
6468
6469 dwarf2_queue_tail = item;
6470}
6471
0907af0c
DE
6472/* THIS_CU has a reference to PER_CU. If necessary, load the new compilation
6473 unit and add it to our queue.
6474 The result is non-zero if PER_CU was queued, otherwise the result is zero
6475 meaning either PER_CU is already queued or it is already loaded. */
6476
6477static int
6478maybe_queue_comp_unit (struct dwarf2_cu *this_cu,
6479 struct dwarf2_per_cu_data *per_cu,
6480 enum language pretend_language)
6481{
6482 /* We may arrive here during partial symbol reading, if we need full
6483 DIEs to process an unusual case (e.g. template arguments). Do
6484 not queue PER_CU, just tell our caller to load its DIEs. */
6485 if (dwarf2_per_objfile->reading_partial_symbols)
6486 {
6487 if (per_cu->cu == NULL || per_cu->cu->dies == NULL)
6488 return 1;
6489 return 0;
6490 }
6491
6492 /* Mark the dependence relation so that we don't flush PER_CU
6493 too early. */
6494 dwarf2_add_dependence (this_cu, per_cu);
6495
6496 /* If it's already on the queue, we have nothing to do. */
6497 if (per_cu->queued)
6498 return 0;
6499
6500 /* If the compilation unit is already loaded, just mark it as
6501 used. */
6502 if (per_cu->cu != NULL)
6503 {
6504 per_cu->cu->last_used = 0;
6505 return 0;
6506 }
6507
6508 /* Add it to the queue. */
6509 queue_comp_unit (per_cu, pretend_language);
6510
6511 return 1;
6512}
6513
10b3939b
DJ
6514/* Process the queue. */
6515
6516static void
a0f42c21 6517process_queue (void)
10b3939b
DJ
6518{
6519 struct dwarf2_queue_item *item, *next_item;
6520
45cfd468
DE
6521 if (dwarf2_read_debug)
6522 {
6523 fprintf_unfiltered (gdb_stdlog,
6524 "Expanding one or more symtabs of objfile %s ...\n",
6525 dwarf2_per_objfile->objfile->name);
6526 }
6527
03dd20cc
DJ
6528 /* The queue starts out with one item, but following a DIE reference
6529 may load a new CU, adding it to the end of the queue. */
10b3939b
DJ
6530 for (item = dwarf2_queue; item != NULL; dwarf2_queue = item = next_item)
6531 {
9291a0cd
TT
6532 if (dwarf2_per_objfile->using_index
6533 ? !item->per_cu->v.quick->symtab
6534 : (item->per_cu->v.psymtab && !item->per_cu->v.psymtab->readin))
f4dc4d17
DE
6535 {
6536 struct dwarf2_per_cu_data *per_cu = item->per_cu;
6537
6538 if (dwarf2_read_debug)
6539 {
6540 fprintf_unfiltered (gdb_stdlog,
6541 "Expanding symtab of %s at offset 0x%x\n",
6542 per_cu->is_debug_types ? "TU" : "CU",
6543 per_cu->offset.sect_off);
6544 }
6545
6546 if (per_cu->is_debug_types)
6547 process_full_type_unit (per_cu, item->pretend_language);
6548 else
6549 process_full_comp_unit (per_cu, item->pretend_language);
6550
6551 if (dwarf2_read_debug)
6552 {
6553 fprintf_unfiltered (gdb_stdlog,
6554 "Done expanding %s at offset 0x%x\n",
6555 per_cu->is_debug_types ? "TU" : "CU",
6556 per_cu->offset.sect_off);
6557 }
6558 }
10b3939b
DJ
6559
6560 item->per_cu->queued = 0;
6561 next_item = item->next;
6562 xfree (item);
6563 }
6564
6565 dwarf2_queue_tail = NULL;
45cfd468
DE
6566
6567 if (dwarf2_read_debug)
6568 {
6569 fprintf_unfiltered (gdb_stdlog, "Done expanding symtabs of %s.\n",
6570 dwarf2_per_objfile->objfile->name);
6571 }
10b3939b
DJ
6572}
6573
6574/* Free all allocated queue entries. This function only releases anything if
6575 an error was thrown; if the queue was processed then it would have been
6576 freed as we went along. */
6577
6578static void
6579dwarf2_release_queue (void *dummy)
6580{
6581 struct dwarf2_queue_item *item, *last;
6582
6583 item = dwarf2_queue;
6584 while (item)
6585 {
6586 /* Anything still marked queued is likely to be in an
6587 inconsistent state, so discard it. */
6588 if (item->per_cu->queued)
6589 {
6590 if (item->per_cu->cu != NULL)
dee91e82 6591 free_one_cached_comp_unit (item->per_cu);
10b3939b
DJ
6592 item->per_cu->queued = 0;
6593 }
6594
6595 last = item;
6596 item = item->next;
6597 xfree (last);
6598 }
6599
6600 dwarf2_queue = dwarf2_queue_tail = NULL;
6601}
6602
6603/* Read in full symbols for PST, and anything it depends on. */
6604
c906108c 6605static void
fba45db2 6606psymtab_to_symtab_1 (struct partial_symtab *pst)
c906108c 6607{
10b3939b 6608 struct dwarf2_per_cu_data *per_cu;
aaa75496
JB
6609 int i;
6610
95554aad
TT
6611 if (pst->readin)
6612 return;
6613
aaa75496 6614 for (i = 0; i < pst->number_of_dependencies; i++)
95554aad
TT
6615 if (!pst->dependencies[i]->readin
6616 && pst->dependencies[i]->user == NULL)
aaa75496
JB
6617 {
6618 /* Inform about additional files that need to be read in. */
6619 if (info_verbose)
6620 {
a3f17187 6621 /* FIXME: i18n: Need to make this a single string. */
aaa75496
JB
6622 fputs_filtered (" ", gdb_stdout);
6623 wrap_here ("");
6624 fputs_filtered ("and ", gdb_stdout);
6625 wrap_here ("");
6626 printf_filtered ("%s...", pst->dependencies[i]->filename);
0963b4bd 6627 wrap_here (""); /* Flush output. */
aaa75496
JB
6628 gdb_flush (gdb_stdout);
6629 }
6630 psymtab_to_symtab_1 (pst->dependencies[i]);
6631 }
6632
e38df1d0 6633 per_cu = pst->read_symtab_private;
10b3939b
DJ
6634
6635 if (per_cu == NULL)
aaa75496
JB
6636 {
6637 /* It's an include file, no symbols to read for it.
6638 Everything is in the parent symtab. */
6639 pst->readin = 1;
6640 return;
6641 }
c906108c 6642
a0f42c21 6643 dw2_do_instantiate_symtab (per_cu);
10b3939b
DJ
6644}
6645
dee91e82
DE
6646/* Trivial hash function for die_info: the hash value of a DIE
6647 is its offset in .debug_info for this objfile. */
10b3939b 6648
dee91e82
DE
6649static hashval_t
6650die_hash (const void *item)
10b3939b 6651{
dee91e82 6652 const struct die_info *die = item;
6502dd73 6653
dee91e82
DE
6654 return die->offset.sect_off;
6655}
63d06c5c 6656
dee91e82
DE
6657/* Trivial comparison function for die_info structures: two DIEs
6658 are equal if they have the same offset. */
98bfdba5 6659
dee91e82
DE
6660static int
6661die_eq (const void *item_lhs, const void *item_rhs)
6662{
6663 const struct die_info *die_lhs = item_lhs;
6664 const struct die_info *die_rhs = item_rhs;
c906108c 6665
dee91e82
DE
6666 return die_lhs->offset.sect_off == die_rhs->offset.sect_off;
6667}
c906108c 6668
dee91e82
DE
6669/* die_reader_func for load_full_comp_unit.
6670 This is identical to read_signatured_type_reader,
6671 but is kept separate for now. */
c906108c 6672
dee91e82
DE
6673static void
6674load_full_comp_unit_reader (const struct die_reader_specs *reader,
6675 gdb_byte *info_ptr,
6676 struct die_info *comp_unit_die,
6677 int has_children,
6678 void *data)
6679{
6680 struct dwarf2_cu *cu = reader->cu;
95554aad 6681 enum language *language_ptr = data;
6caca83c 6682
dee91e82
DE
6683 gdb_assert (cu->die_hash == NULL);
6684 cu->die_hash =
6685 htab_create_alloc_ex (cu->header.length / 12,
6686 die_hash,
6687 die_eq,
6688 NULL,
6689 &cu->comp_unit_obstack,
6690 hashtab_obstack_allocate,
6691 dummy_obstack_deallocate);
e142c38c 6692
dee91e82
DE
6693 if (has_children)
6694 comp_unit_die->child = read_die_and_siblings (reader, info_ptr,
6695 &info_ptr, comp_unit_die);
6696 cu->dies = comp_unit_die;
6697 /* comp_unit_die is not stored in die_hash, no need. */
10b3939b
DJ
6698
6699 /* We try not to read any attributes in this function, because not
9cdd5dbd 6700 all CUs needed for references have been loaded yet, and symbol
10b3939b 6701 table processing isn't initialized. But we have to set the CU language,
dee91e82
DE
6702 or we won't be able to build types correctly.
6703 Similarly, if we do not read the producer, we can not apply
6704 producer-specific interpretation. */
95554aad 6705 prepare_one_comp_unit (cu, cu->dies, *language_ptr);
dee91e82 6706}
10b3939b 6707
dee91e82 6708/* Load the DIEs associated with PER_CU into memory. */
a6c727b2 6709
dee91e82 6710static void
95554aad
TT
6711load_full_comp_unit (struct dwarf2_per_cu_data *this_cu,
6712 enum language pretend_language)
dee91e82 6713{
3019eac3 6714 gdb_assert (! this_cu->is_debug_types);
c5b7e1cb 6715
f4dc4d17
DE
6716 init_cutu_and_read_dies (this_cu, NULL, 1, 1,
6717 load_full_comp_unit_reader, &pretend_language);
10b3939b
DJ
6718}
6719
3da10d80
KS
6720/* Add a DIE to the delayed physname list. */
6721
6722static void
6723add_to_method_list (struct type *type, int fnfield_index, int index,
6724 const char *name, struct die_info *die,
6725 struct dwarf2_cu *cu)
6726{
6727 struct delayed_method_info mi;
6728 mi.type = type;
6729 mi.fnfield_index = fnfield_index;
6730 mi.index = index;
6731 mi.name = name;
6732 mi.die = die;
6733 VEC_safe_push (delayed_method_info, cu->method_list, &mi);
6734}
6735
6736/* A cleanup for freeing the delayed method list. */
6737
6738static void
6739free_delayed_list (void *ptr)
6740{
6741 struct dwarf2_cu *cu = (struct dwarf2_cu *) ptr;
6742 if (cu->method_list != NULL)
6743 {
6744 VEC_free (delayed_method_info, cu->method_list);
6745 cu->method_list = NULL;
6746 }
6747}
6748
6749/* Compute the physnames of any methods on the CU's method list.
6750
6751 The computation of method physnames is delayed in order to avoid the
6752 (bad) condition that one of the method's formal parameters is of an as yet
6753 incomplete type. */
6754
6755static void
6756compute_delayed_physnames (struct dwarf2_cu *cu)
6757{
6758 int i;
6759 struct delayed_method_info *mi;
6760 for (i = 0; VEC_iterate (delayed_method_info, cu->method_list, i, mi) ; ++i)
6761 {
1d06ead6 6762 const char *physname;
3da10d80
KS
6763 struct fn_fieldlist *fn_flp
6764 = &TYPE_FN_FIELDLIST (mi->type, mi->fnfield_index);
1d06ead6 6765 physname = dwarf2_physname ((char *) mi->name, mi->die, cu);
3da10d80
KS
6766 fn_flp->fn_fields[mi->index].physname = physname ? physname : "";
6767 }
6768}
6769
a766d390
DE
6770/* Go objects should be embedded in a DW_TAG_module DIE,
6771 and it's not clear if/how imported objects will appear.
6772 To keep Go support simple until that's worked out,
6773 go back through what we've read and create something usable.
6774 We could do this while processing each DIE, and feels kinda cleaner,
6775 but that way is more invasive.
6776 This is to, for example, allow the user to type "p var" or "b main"
6777 without having to specify the package name, and allow lookups
6778 of module.object to work in contexts that use the expression
6779 parser. */
6780
6781static void
6782fixup_go_packaging (struct dwarf2_cu *cu)
6783{
6784 char *package_name = NULL;
6785 struct pending *list;
6786 int i;
6787
6788 for (list = global_symbols; list != NULL; list = list->next)
6789 {
6790 for (i = 0; i < list->nsyms; ++i)
6791 {
6792 struct symbol *sym = list->symbol[i];
6793
6794 if (SYMBOL_LANGUAGE (sym) == language_go
6795 && SYMBOL_CLASS (sym) == LOC_BLOCK)
6796 {
6797 char *this_package_name = go_symbol_package_name (sym);
6798
6799 if (this_package_name == NULL)
6800 continue;
6801 if (package_name == NULL)
6802 package_name = this_package_name;
6803 else
6804 {
6805 if (strcmp (package_name, this_package_name) != 0)
6806 complaint (&symfile_complaints,
6807 _("Symtab %s has objects from two different Go packages: %s and %s"),
210bbc17 6808 (SYMBOL_SYMTAB (sym)
210bbc17 6809 ? SYMBOL_SYMTAB (sym)->filename
a766d390
DE
6810 : cu->objfile->name),
6811 this_package_name, package_name);
6812 xfree (this_package_name);
6813 }
6814 }
6815 }
6816 }
6817
6818 if (package_name != NULL)
6819 {
6820 struct objfile *objfile = cu->objfile;
10f0c4bb
TT
6821 const char *saved_package_name = obstack_copy0 (&objfile->objfile_obstack,
6822 package_name,
6823 strlen (package_name));
a766d390 6824 struct type *type = init_type (TYPE_CODE_MODULE, 0, 0,
86f62fd7 6825 saved_package_name, objfile);
a766d390
DE
6826 struct symbol *sym;
6827
6828 TYPE_TAG_NAME (type) = TYPE_NAME (type);
6829
6830 sym = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct symbol);
6831 SYMBOL_SET_LANGUAGE (sym, language_go);
86f62fd7
TT
6832 SYMBOL_SET_NAMES (sym, saved_package_name,
6833 strlen (saved_package_name), 0, objfile);
a766d390
DE
6834 /* This is not VAR_DOMAIN because we want a way to ensure a lookup of,
6835 e.g., "main" finds the "main" module and not C's main(). */
6836 SYMBOL_DOMAIN (sym) = STRUCT_DOMAIN;
6837 SYMBOL_CLASS (sym) = LOC_TYPEDEF;
6838 SYMBOL_TYPE (sym) = type;
6839
6840 add_symbol_to_list (sym, &global_symbols);
6841
6842 xfree (package_name);
6843 }
6844}
6845
95554aad
TT
6846static void compute_symtab_includes (struct dwarf2_per_cu_data *per_cu);
6847
6848/* Return the symtab for PER_CU. This works properly regardless of
6849 whether we're using the index or psymtabs. */
6850
6851static struct symtab *
6852get_symtab (struct dwarf2_per_cu_data *per_cu)
6853{
6854 return (dwarf2_per_objfile->using_index
6855 ? per_cu->v.quick->symtab
6856 : per_cu->v.psymtab->symtab);
6857}
6858
6859/* A helper function for computing the list of all symbol tables
6860 included by PER_CU. */
6861
6862static void
6863recursively_compute_inclusions (VEC (dwarf2_per_cu_ptr) **result,
6864 htab_t all_children,
6865 struct dwarf2_per_cu_data *per_cu)
6866{
6867 void **slot;
6868 int ix;
6869 struct dwarf2_per_cu_data *iter;
6870
6871 slot = htab_find_slot (all_children, per_cu, INSERT);
6872 if (*slot != NULL)
6873 {
6874 /* This inclusion and its children have been processed. */
6875 return;
6876 }
6877
6878 *slot = per_cu;
6879 /* Only add a CU if it has a symbol table. */
6880 if (get_symtab (per_cu) != NULL)
6881 VEC_safe_push (dwarf2_per_cu_ptr, *result, per_cu);
6882
6883 for (ix = 0;
796a7ff8 6884 VEC_iterate (dwarf2_per_cu_ptr, per_cu->imported_symtabs, ix, iter);
95554aad
TT
6885 ++ix)
6886 recursively_compute_inclusions (result, all_children, iter);
6887}
6888
6889/* Compute the symtab 'includes' fields for the symtab related to
6890 PER_CU. */
6891
6892static void
6893compute_symtab_includes (struct dwarf2_per_cu_data *per_cu)
6894{
f4dc4d17
DE
6895 gdb_assert (! per_cu->is_debug_types);
6896
796a7ff8 6897 if (!VEC_empty (dwarf2_per_cu_ptr, per_cu->imported_symtabs))
95554aad
TT
6898 {
6899 int ix, len;
6900 struct dwarf2_per_cu_data *iter;
6901 VEC (dwarf2_per_cu_ptr) *result_children = NULL;
6902 htab_t all_children;
6903 struct symtab *symtab = get_symtab (per_cu);
6904
6905 /* If we don't have a symtab, we can just skip this case. */
6906 if (symtab == NULL)
6907 return;
6908
6909 all_children = htab_create_alloc (1, htab_hash_pointer, htab_eq_pointer,
6910 NULL, xcalloc, xfree);
6911
6912 for (ix = 0;
796a7ff8 6913 VEC_iterate (dwarf2_per_cu_ptr, per_cu->imported_symtabs,
95554aad
TT
6914 ix, iter);
6915 ++ix)
6916 recursively_compute_inclusions (&result_children, all_children, iter);
6917
796a7ff8
DE
6918 /* Now we have a transitive closure of all the included CUs, and
6919 for .gdb_index version 7 the included TUs, so we can convert it
6920 to a list of symtabs. */
95554aad
TT
6921 len = VEC_length (dwarf2_per_cu_ptr, result_children);
6922 symtab->includes
6923 = obstack_alloc (&dwarf2_per_objfile->objfile->objfile_obstack,
6924 (len + 1) * sizeof (struct symtab *));
6925 for (ix = 0;
6926 VEC_iterate (dwarf2_per_cu_ptr, result_children, ix, iter);
6927 ++ix)
6928 symtab->includes[ix] = get_symtab (iter);
6929 symtab->includes[len] = NULL;
6930
6931 VEC_free (dwarf2_per_cu_ptr, result_children);
6932 htab_delete (all_children);
6933 }
6934}
6935
6936/* Compute the 'includes' field for the symtabs of all the CUs we just
6937 read. */
6938
6939static void
6940process_cu_includes (void)
6941{
6942 int ix;
6943 struct dwarf2_per_cu_data *iter;
6944
6945 for (ix = 0;
6946 VEC_iterate (dwarf2_per_cu_ptr, dwarf2_per_objfile->just_read_cus,
6947 ix, iter);
6948 ++ix)
f4dc4d17
DE
6949 {
6950 if (! iter->is_debug_types)
6951 compute_symtab_includes (iter);
6952 }
95554aad
TT
6953
6954 VEC_free (dwarf2_per_cu_ptr, dwarf2_per_objfile->just_read_cus);
6955}
6956
9cdd5dbd 6957/* Generate full symbol information for PER_CU, whose DIEs have
10b3939b
DJ
6958 already been loaded into memory. */
6959
6960static void
95554aad
TT
6961process_full_comp_unit (struct dwarf2_per_cu_data *per_cu,
6962 enum language pretend_language)
10b3939b 6963{
10b3939b 6964 struct dwarf2_cu *cu = per_cu->cu;
9291a0cd 6965 struct objfile *objfile = per_cu->objfile;
10b3939b
DJ
6966 CORE_ADDR lowpc, highpc;
6967 struct symtab *symtab;
3da10d80 6968 struct cleanup *back_to, *delayed_list_cleanup;
10b3939b 6969 CORE_ADDR baseaddr;
4359dff1 6970 struct block *static_block;
10b3939b
DJ
6971
6972 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
6973
10b3939b
DJ
6974 buildsym_init ();
6975 back_to = make_cleanup (really_free_pendings, NULL);
3da10d80 6976 delayed_list_cleanup = make_cleanup (free_delayed_list, cu);
10b3939b
DJ
6977
6978 cu->list_in_scope = &file_symbols;
c906108c 6979
95554aad
TT
6980 cu->language = pretend_language;
6981 cu->language_defn = language_def (cu->language);
6982
c906108c 6983 /* Do line number decoding in read_file_scope () */
10b3939b 6984 process_die (cu->dies, cu);
c906108c 6985
a766d390
DE
6986 /* For now fudge the Go package. */
6987 if (cu->language == language_go)
6988 fixup_go_packaging (cu);
6989
3da10d80
KS
6990 /* Now that we have processed all the DIEs in the CU, all the types
6991 should be complete, and it should now be safe to compute all of the
6992 physnames. */
6993 compute_delayed_physnames (cu);
6994 do_cleanups (delayed_list_cleanup);
6995
fae299cd
DC
6996 /* Some compilers don't define a DW_AT_high_pc attribute for the
6997 compilation unit. If the DW_AT_high_pc is missing, synthesize
6998 it, by scanning the DIE's below the compilation unit. */
10b3939b 6999 get_scope_pc_bounds (cu->dies, &lowpc, &highpc, cu);
c906108c 7000
36586728
TT
7001 static_block
7002 = end_symtab_get_static_block (highpc + baseaddr, objfile, 0,
796a7ff8 7003 per_cu->imported_symtabs != NULL);
4359dff1
JK
7004
7005 /* If the comp unit has DW_AT_ranges, it may have discontiguous ranges.
7006 Also, DW_AT_ranges may record ranges not belonging to any child DIEs
7007 (such as virtual method tables). Record the ranges in STATIC_BLOCK's
7008 addrmap to help ensure it has an accurate map of pc values belonging to
7009 this comp unit. */
7010 dwarf2_record_block_ranges (cu->dies, static_block, baseaddr, cu);
7011
7012 symtab = end_symtab_from_static_block (static_block, objfile,
7013 SECT_OFF_TEXT (objfile), 0);
c906108c 7014
8be455d7 7015 if (symtab != NULL)
c906108c 7016 {
df15bd07 7017 int gcc_4_minor = producer_is_gcc_ge_4 (cu->producer);
4632c0d0 7018
8be455d7
JK
7019 /* Set symtab language to language from DW_AT_language. If the
7020 compilation is from a C file generated by language preprocessors, do
7021 not set the language if it was already deduced by start_subfile. */
7022 if (!(cu->language == language_c && symtab->language != language_c))
7023 symtab->language = cu->language;
7024
7025 /* GCC-4.0 has started to support -fvar-tracking. GCC-3.x still can
7026 produce DW_AT_location with location lists but it can be possibly
ab260dad
JK
7027 invalid without -fvar-tracking. Still up to GCC-4.4.x incl. 4.4.0
7028 there were bugs in prologue debug info, fixed later in GCC-4.5
7029 by "unwind info for epilogues" patch (which is not directly related).
8be455d7
JK
7030
7031 For -gdwarf-4 type units LOCATIONS_VALID indication is fortunately not
7032 needed, it would be wrong due to missing DW_AT_producer there.
7033
7034 Still one can confuse GDB by using non-standard GCC compilation
7035 options - this waits on GCC PR other/32998 (-frecord-gcc-switches).
7036 */
ab260dad 7037 if (cu->has_loclist && gcc_4_minor >= 5)
8be455d7 7038 symtab->locations_valid = 1;
e0d00bc7
JK
7039
7040 if (gcc_4_minor >= 5)
7041 symtab->epilogue_unwind_valid = 1;
96408a79
SA
7042
7043 symtab->call_site_htab = cu->call_site_htab;
c906108c 7044 }
9291a0cd
TT
7045
7046 if (dwarf2_per_objfile->using_index)
7047 per_cu->v.quick->symtab = symtab;
7048 else
7049 {
7050 struct partial_symtab *pst = per_cu->v.psymtab;
7051 pst->symtab = symtab;
7052 pst->readin = 1;
7053 }
c906108c 7054
95554aad
TT
7055 /* Push it for inclusion processing later. */
7056 VEC_safe_push (dwarf2_per_cu_ptr, dwarf2_per_objfile->just_read_cus, per_cu);
7057
c906108c 7058 do_cleanups (back_to);
f4dc4d17 7059}
45cfd468 7060
f4dc4d17
DE
7061/* Generate full symbol information for type unit PER_CU, whose DIEs have
7062 already been loaded into memory. */
7063
7064static void
7065process_full_type_unit (struct dwarf2_per_cu_data *per_cu,
7066 enum language pretend_language)
7067{
7068 struct dwarf2_cu *cu = per_cu->cu;
7069 struct objfile *objfile = per_cu->objfile;
7070 struct symtab *symtab;
7071 struct cleanup *back_to, *delayed_list_cleanup;
7072
7073 buildsym_init ();
7074 back_to = make_cleanup (really_free_pendings, NULL);
7075 delayed_list_cleanup = make_cleanup (free_delayed_list, cu);
7076
7077 cu->list_in_scope = &file_symbols;
7078
7079 cu->language = pretend_language;
7080 cu->language_defn = language_def (cu->language);
7081
7082 /* The symbol tables are set up in read_type_unit_scope. */
7083 process_die (cu->dies, cu);
7084
7085 /* For now fudge the Go package. */
7086 if (cu->language == language_go)
7087 fixup_go_packaging (cu);
7088
7089 /* Now that we have processed all the DIEs in the CU, all the types
7090 should be complete, and it should now be safe to compute all of the
7091 physnames. */
7092 compute_delayed_physnames (cu);
7093 do_cleanups (delayed_list_cleanup);
7094
7095 /* TUs share symbol tables.
7096 If this is the first TU to use this symtab, complete the construction
094b34ac
DE
7097 of it with end_expandable_symtab. Otherwise, complete the addition of
7098 this TU's symbols to the existing symtab. */
796a7ff8 7099 if (per_cu->type_unit_group->primary_symtab == NULL)
45cfd468 7100 {
f4dc4d17 7101 symtab = end_expandable_symtab (0, objfile, SECT_OFF_TEXT (objfile));
796a7ff8 7102 per_cu->type_unit_group->primary_symtab = symtab;
f4dc4d17
DE
7103
7104 if (symtab != NULL)
7105 {
7106 /* Set symtab language to language from DW_AT_language. If the
7107 compilation is from a C file generated by language preprocessors,
7108 do not set the language if it was already deduced by
7109 start_subfile. */
7110 if (!(cu->language == language_c && symtab->language != language_c))
7111 symtab->language = cu->language;
7112 }
7113 }
7114 else
7115 {
7116 augment_type_symtab (objfile,
796a7ff8
DE
7117 per_cu->type_unit_group->primary_symtab);
7118 symtab = per_cu->type_unit_group->primary_symtab;
f4dc4d17
DE
7119 }
7120
7121 if (dwarf2_per_objfile->using_index)
7122 per_cu->v.quick->symtab = symtab;
7123 else
7124 {
7125 struct partial_symtab *pst = per_cu->v.psymtab;
7126 pst->symtab = symtab;
7127 pst->readin = 1;
45cfd468 7128 }
f4dc4d17
DE
7129
7130 do_cleanups (back_to);
c906108c
SS
7131}
7132
95554aad
TT
7133/* Process an imported unit DIE. */
7134
7135static void
7136process_imported_unit_die (struct die_info *die, struct dwarf2_cu *cu)
7137{
7138 struct attribute *attr;
7139
f4dc4d17
DE
7140 /* For now we don't handle imported units in type units. */
7141 if (cu->per_cu->is_debug_types)
7142 {
7143 error (_("Dwarf Error: DW_TAG_imported_unit is not"
7144 " supported in type units [in module %s]"),
7145 cu->objfile->name);
7146 }
7147
95554aad
TT
7148 attr = dwarf2_attr (die, DW_AT_import, cu);
7149 if (attr != NULL)
7150 {
7151 struct dwarf2_per_cu_data *per_cu;
7152 struct symtab *imported_symtab;
7153 sect_offset offset;
36586728 7154 int is_dwz;
95554aad
TT
7155
7156 offset = dwarf2_get_ref_die_offset (attr);
36586728
TT
7157 is_dwz = (attr->form == DW_FORM_GNU_ref_alt || cu->per_cu->is_dwz);
7158 per_cu = dwarf2_find_containing_comp_unit (offset, is_dwz, cu->objfile);
95554aad
TT
7159
7160 /* Queue the unit, if needed. */
7161 if (maybe_queue_comp_unit (cu, per_cu, cu->language))
7162 load_full_comp_unit (per_cu, cu->language);
7163
796a7ff8 7164 VEC_safe_push (dwarf2_per_cu_ptr, cu->per_cu->imported_symtabs,
95554aad
TT
7165 per_cu);
7166 }
7167}
7168
c906108c
SS
7169/* Process a die and its children. */
7170
7171static void
e7c27a73 7172process_die (struct die_info *die, struct dwarf2_cu *cu)
c906108c
SS
7173{
7174 switch (die->tag)
7175 {
7176 case DW_TAG_padding:
7177 break;
7178 case DW_TAG_compile_unit:
95554aad 7179 case DW_TAG_partial_unit:
e7c27a73 7180 read_file_scope (die, cu);
c906108c 7181 break;
348e048f
DE
7182 case DW_TAG_type_unit:
7183 read_type_unit_scope (die, cu);
7184 break;
c906108c 7185 case DW_TAG_subprogram:
c906108c 7186 case DW_TAG_inlined_subroutine:
edb3359d 7187 read_func_scope (die, cu);
c906108c
SS
7188 break;
7189 case DW_TAG_lexical_block:
14898363
L
7190 case DW_TAG_try_block:
7191 case DW_TAG_catch_block:
e7c27a73 7192 read_lexical_block_scope (die, cu);
c906108c 7193 break;
96408a79
SA
7194 case DW_TAG_GNU_call_site:
7195 read_call_site_scope (die, cu);
7196 break;
c906108c 7197 case DW_TAG_class_type:
680b30c7 7198 case DW_TAG_interface_type:
c906108c
SS
7199 case DW_TAG_structure_type:
7200 case DW_TAG_union_type:
134d01f1 7201 process_structure_scope (die, cu);
c906108c
SS
7202 break;
7203 case DW_TAG_enumeration_type:
134d01f1 7204 process_enumeration_scope (die, cu);
c906108c 7205 break;
134d01f1 7206
f792889a
DJ
7207 /* These dies have a type, but processing them does not create
7208 a symbol or recurse to process the children. Therefore we can
7209 read them on-demand through read_type_die. */
c906108c 7210 case DW_TAG_subroutine_type:
72019c9c 7211 case DW_TAG_set_type:
c906108c 7212 case DW_TAG_array_type:
c906108c 7213 case DW_TAG_pointer_type:
c906108c 7214 case DW_TAG_ptr_to_member_type:
c906108c 7215 case DW_TAG_reference_type:
c906108c 7216 case DW_TAG_string_type:
c906108c 7217 break;
134d01f1 7218
c906108c 7219 case DW_TAG_base_type:
a02abb62 7220 case DW_TAG_subrange_type:
cb249c71 7221 case DW_TAG_typedef:
134d01f1
DJ
7222 /* Add a typedef symbol for the type definition, if it has a
7223 DW_AT_name. */
f792889a 7224 new_symbol (die, read_type_die (die, cu), cu);
a02abb62 7225 break;
c906108c 7226 case DW_TAG_common_block:
e7c27a73 7227 read_common_block (die, cu);
c906108c
SS
7228 break;
7229 case DW_TAG_common_inclusion:
7230 break;
d9fa45fe 7231 case DW_TAG_namespace:
4d4ec4e5 7232 cu->processing_has_namespace_info = 1;
e7c27a73 7233 read_namespace (die, cu);
d9fa45fe 7234 break;
5d7cb8df 7235 case DW_TAG_module:
4d4ec4e5 7236 cu->processing_has_namespace_info = 1;
5d7cb8df
JK
7237 read_module (die, cu);
7238 break;
d9fa45fe
DC
7239 case DW_TAG_imported_declaration:
7240 case DW_TAG_imported_module:
4d4ec4e5 7241 cu->processing_has_namespace_info = 1;
27aa8d6a
SW
7242 if (die->child != NULL && (die->tag == DW_TAG_imported_declaration
7243 || cu->language != language_fortran))
7244 complaint (&symfile_complaints, _("Tag '%s' has unexpected children"),
7245 dwarf_tag_name (die->tag));
7246 read_import_statement (die, cu);
d9fa45fe 7247 break;
95554aad
TT
7248
7249 case DW_TAG_imported_unit:
7250 process_imported_unit_die (die, cu);
7251 break;
7252
c906108c 7253 default:
e7c27a73 7254 new_symbol (die, NULL, cu);
c906108c
SS
7255 break;
7256 }
7257}
7258
94af9270
KS
7259/* A helper function for dwarf2_compute_name which determines whether DIE
7260 needs to have the name of the scope prepended to the name listed in the
7261 die. */
7262
7263static int
7264die_needs_namespace (struct die_info *die, struct dwarf2_cu *cu)
7265{
1c809c68
TT
7266 struct attribute *attr;
7267
94af9270
KS
7268 switch (die->tag)
7269 {
7270 case DW_TAG_namespace:
7271 case DW_TAG_typedef:
7272 case DW_TAG_class_type:
7273 case DW_TAG_interface_type:
7274 case DW_TAG_structure_type:
7275 case DW_TAG_union_type:
7276 case DW_TAG_enumeration_type:
7277 case DW_TAG_enumerator:
7278 case DW_TAG_subprogram:
7279 case DW_TAG_member:
7280 return 1;
7281
7282 case DW_TAG_variable:
c2b0a229 7283 case DW_TAG_constant:
94af9270
KS
7284 /* We only need to prefix "globally" visible variables. These include
7285 any variable marked with DW_AT_external or any variable that
7286 lives in a namespace. [Variables in anonymous namespaces
7287 require prefixing, but they are not DW_AT_external.] */
7288
7289 if (dwarf2_attr (die, DW_AT_specification, cu))
7290 {
7291 struct dwarf2_cu *spec_cu = cu;
9a619af0 7292
94af9270
KS
7293 return die_needs_namespace (die_specification (die, &spec_cu),
7294 spec_cu);
7295 }
7296
1c809c68 7297 attr = dwarf2_attr (die, DW_AT_external, cu);
f55ee35c
JK
7298 if (attr == NULL && die->parent->tag != DW_TAG_namespace
7299 && die->parent->tag != DW_TAG_module)
1c809c68
TT
7300 return 0;
7301 /* A variable in a lexical block of some kind does not need a
7302 namespace, even though in C++ such variables may be external
7303 and have a mangled name. */
7304 if (die->parent->tag == DW_TAG_lexical_block
7305 || die->parent->tag == DW_TAG_try_block
1054b214
TT
7306 || die->parent->tag == DW_TAG_catch_block
7307 || die->parent->tag == DW_TAG_subprogram)
1c809c68
TT
7308 return 0;
7309 return 1;
94af9270
KS
7310
7311 default:
7312 return 0;
7313 }
7314}
7315
98bfdba5
PA
7316/* Retrieve the last character from a mem_file. */
7317
7318static void
7319do_ui_file_peek_last (void *object, const char *buffer, long length)
7320{
7321 char *last_char_p = (char *) object;
7322
7323 if (length > 0)
7324 *last_char_p = buffer[length - 1];
7325}
7326
94af9270 7327/* Compute the fully qualified name of DIE in CU. If PHYSNAME is nonzero,
a766d390
DE
7328 compute the physname for the object, which include a method's:
7329 - formal parameters (C++/Java),
7330 - receiver type (Go),
7331 - return type (Java).
7332
7333 The term "physname" is a bit confusing.
7334 For C++, for example, it is the demangled name.
7335 For Go, for example, it's the mangled name.
94af9270 7336
af6b7be1
JB
7337 For Ada, return the DIE's linkage name rather than the fully qualified
7338 name. PHYSNAME is ignored..
7339
94af9270
KS
7340 The result is allocated on the objfile_obstack and canonicalized. */
7341
7342static const char *
15d034d0
TT
7343dwarf2_compute_name (const char *name,
7344 struct die_info *die, struct dwarf2_cu *cu,
94af9270
KS
7345 int physname)
7346{
bb5ed363
DE
7347 struct objfile *objfile = cu->objfile;
7348
94af9270
KS
7349 if (name == NULL)
7350 name = dwarf2_name (die, cu);
7351
f55ee35c
JK
7352 /* For Fortran GDB prefers DW_AT_*linkage_name if present but otherwise
7353 compute it by typename_concat inside GDB. */
7354 if (cu->language == language_ada
7355 || (cu->language == language_fortran && physname))
7356 {
7357 /* For Ada unit, we prefer the linkage name over the name, as
7358 the former contains the exported name, which the user expects
7359 to be able to reference. Ideally, we want the user to be able
7360 to reference this entity using either natural or linkage name,
7361 but we haven't started looking at this enhancement yet. */
7362 struct attribute *attr;
7363
7364 attr = dwarf2_attr (die, DW_AT_linkage_name, cu);
7365 if (attr == NULL)
7366 attr = dwarf2_attr (die, DW_AT_MIPS_linkage_name, cu);
7367 if (attr && DW_STRING (attr))
7368 return DW_STRING (attr);
7369 }
7370
94af9270
KS
7371 /* These are the only languages we know how to qualify names in. */
7372 if (name != NULL
f55ee35c
JK
7373 && (cu->language == language_cplus || cu->language == language_java
7374 || cu->language == language_fortran))
94af9270
KS
7375 {
7376 if (die_needs_namespace (die, cu))
7377 {
7378 long length;
0d5cff50 7379 const char *prefix;
94af9270
KS
7380 struct ui_file *buf;
7381
7382 prefix = determine_prefix (die, cu);
7383 buf = mem_fileopen ();
7384 if (*prefix != '\0')
7385 {
f55ee35c
JK
7386 char *prefixed_name = typename_concat (NULL, prefix, name,
7387 physname, cu);
9a619af0 7388
94af9270
KS
7389 fputs_unfiltered (prefixed_name, buf);
7390 xfree (prefixed_name);
7391 }
7392 else
62d5b8da 7393 fputs_unfiltered (name, buf);
94af9270 7394
98bfdba5
PA
7395 /* Template parameters may be specified in the DIE's DW_AT_name, or
7396 as children with DW_TAG_template_type_param or
7397 DW_TAG_value_type_param. If the latter, add them to the name
7398 here. If the name already has template parameters, then
7399 skip this step; some versions of GCC emit both, and
7400 it is more efficient to use the pre-computed name.
7401
7402 Something to keep in mind about this process: it is very
7403 unlikely, or in some cases downright impossible, to produce
7404 something that will match the mangled name of a function.
7405 If the definition of the function has the same debug info,
7406 we should be able to match up with it anyway. But fallbacks
7407 using the minimal symbol, for instance to find a method
7408 implemented in a stripped copy of libstdc++, will not work.
7409 If we do not have debug info for the definition, we will have to
7410 match them up some other way.
7411
7412 When we do name matching there is a related problem with function
7413 templates; two instantiated function templates are allowed to
7414 differ only by their return types, which we do not add here. */
7415
7416 if (cu->language == language_cplus && strchr (name, '<') == NULL)
7417 {
7418 struct attribute *attr;
7419 struct die_info *child;
7420 int first = 1;
7421
7422 die->building_fullname = 1;
7423
7424 for (child = die->child; child != NULL; child = child->sibling)
7425 {
7426 struct type *type;
12df843f 7427 LONGEST value;
98bfdba5
PA
7428 gdb_byte *bytes;
7429 struct dwarf2_locexpr_baton *baton;
7430 struct value *v;
7431
7432 if (child->tag != DW_TAG_template_type_param
7433 && child->tag != DW_TAG_template_value_param)
7434 continue;
7435
7436 if (first)
7437 {
7438 fputs_unfiltered ("<", buf);
7439 first = 0;
7440 }
7441 else
7442 fputs_unfiltered (", ", buf);
7443
7444 attr = dwarf2_attr (child, DW_AT_type, cu);
7445 if (attr == NULL)
7446 {
7447 complaint (&symfile_complaints,
7448 _("template parameter missing DW_AT_type"));
7449 fputs_unfiltered ("UNKNOWN_TYPE", buf);
7450 continue;
7451 }
7452 type = die_type (child, cu);
7453
7454 if (child->tag == DW_TAG_template_type_param)
7455 {
79d43c61 7456 c_print_type (type, "", buf, -1, 0, &type_print_raw_options);
98bfdba5
PA
7457 continue;
7458 }
7459
7460 attr = dwarf2_attr (child, DW_AT_const_value, cu);
7461 if (attr == NULL)
7462 {
7463 complaint (&symfile_complaints,
3e43a32a
MS
7464 _("template parameter missing "
7465 "DW_AT_const_value"));
98bfdba5
PA
7466 fputs_unfiltered ("UNKNOWN_VALUE", buf);
7467 continue;
7468 }
7469
7470 dwarf2_const_value_attr (attr, type, name,
7471 &cu->comp_unit_obstack, cu,
7472 &value, &bytes, &baton);
7473
7474 if (TYPE_NOSIGN (type))
7475 /* GDB prints characters as NUMBER 'CHAR'. If that's
7476 changed, this can use value_print instead. */
7477 c_printchar (value, type, buf);
7478 else
7479 {
7480 struct value_print_options opts;
7481
7482 if (baton != NULL)
7483 v = dwarf2_evaluate_loc_desc (type, NULL,
7484 baton->data,
7485 baton->size,
7486 baton->per_cu);
7487 else if (bytes != NULL)
7488 {
7489 v = allocate_value (type);
7490 memcpy (value_contents_writeable (v), bytes,
7491 TYPE_LENGTH (type));
7492 }
7493 else
7494 v = value_from_longest (type, value);
7495
3e43a32a
MS
7496 /* Specify decimal so that we do not depend on
7497 the radix. */
98bfdba5
PA
7498 get_formatted_print_options (&opts, 'd');
7499 opts.raw = 1;
7500 value_print (v, buf, &opts);
7501 release_value (v);
7502 value_free (v);
7503 }
7504 }
7505
7506 die->building_fullname = 0;
7507
7508 if (!first)
7509 {
7510 /* Close the argument list, with a space if necessary
7511 (nested templates). */
7512 char last_char = '\0';
7513 ui_file_put (buf, do_ui_file_peek_last, &last_char);
7514 if (last_char == '>')
7515 fputs_unfiltered (" >", buf);
7516 else
7517 fputs_unfiltered (">", buf);
7518 }
7519 }
7520
94af9270
KS
7521 /* For Java and C++ methods, append formal parameter type
7522 information, if PHYSNAME. */
6e70227d 7523
94af9270
KS
7524 if (physname && die->tag == DW_TAG_subprogram
7525 && (cu->language == language_cplus
7526 || cu->language == language_java))
7527 {
7528 struct type *type = read_type_die (die, cu);
7529
79d43c61
TT
7530 c_type_print_args (type, buf, 1, cu->language,
7531 &type_print_raw_options);
94af9270
KS
7532
7533 if (cu->language == language_java)
7534 {
7535 /* For java, we must append the return type to method
0963b4bd 7536 names. */
94af9270
KS
7537 if (die->tag == DW_TAG_subprogram)
7538 java_print_type (TYPE_TARGET_TYPE (type), "", buf,
79d43c61 7539 0, 0, &type_print_raw_options);
94af9270
KS
7540 }
7541 else if (cu->language == language_cplus)
7542 {
60430eff
DJ
7543 /* Assume that an artificial first parameter is
7544 "this", but do not crash if it is not. RealView
7545 marks unnamed (and thus unused) parameters as
7546 artificial; there is no way to differentiate
7547 the two cases. */
94af9270
KS
7548 if (TYPE_NFIELDS (type) > 0
7549 && TYPE_FIELD_ARTIFICIAL (type, 0)
60430eff 7550 && TYPE_CODE (TYPE_FIELD_TYPE (type, 0)) == TYPE_CODE_PTR
3e43a32a
MS
7551 && TYPE_CONST (TYPE_TARGET_TYPE (TYPE_FIELD_TYPE (type,
7552 0))))
94af9270
KS
7553 fputs_unfiltered (" const", buf);
7554 }
7555 }
7556
bb5ed363 7557 name = ui_file_obsavestring (buf, &objfile->objfile_obstack,
94af9270
KS
7558 &length);
7559 ui_file_delete (buf);
7560
7561 if (cu->language == language_cplus)
7562 {
15d034d0 7563 const char *cname
94af9270 7564 = dwarf2_canonicalize_name (name, cu,
bb5ed363 7565 &objfile->objfile_obstack);
9a619af0 7566
94af9270
KS
7567 if (cname != NULL)
7568 name = cname;
7569 }
7570 }
7571 }
7572
7573 return name;
7574}
7575
0114d602
DJ
7576/* Return the fully qualified name of DIE, based on its DW_AT_name.
7577 If scope qualifiers are appropriate they will be added. The result
7578 will be allocated on the objfile_obstack, or NULL if the DIE does
94af9270
KS
7579 not have a name. NAME may either be from a previous call to
7580 dwarf2_name or NULL.
7581
0963b4bd 7582 The output string will be canonicalized (if C++/Java). */
0114d602
DJ
7583
7584static const char *
15d034d0 7585dwarf2_full_name (const char *name, struct die_info *die, struct dwarf2_cu *cu)
0114d602 7586{
94af9270
KS
7587 return dwarf2_compute_name (name, die, cu, 0);
7588}
0114d602 7589
94af9270
KS
7590/* Construct a physname for the given DIE in CU. NAME may either be
7591 from a previous call to dwarf2_name or NULL. The result will be
7592 allocated on the objfile_objstack or NULL if the DIE does not have a
7593 name.
0114d602 7594
94af9270 7595 The output string will be canonicalized (if C++/Java). */
0114d602 7596
94af9270 7597static const char *
15d034d0 7598dwarf2_physname (const char *name, struct die_info *die, struct dwarf2_cu *cu)
94af9270 7599{
bb5ed363 7600 struct objfile *objfile = cu->objfile;
900e11f9
JK
7601 struct attribute *attr;
7602 const char *retval, *mangled = NULL, *canon = NULL;
7603 struct cleanup *back_to;
7604 int need_copy = 1;
7605
7606 /* In this case dwarf2_compute_name is just a shortcut not building anything
7607 on its own. */
7608 if (!die_needs_namespace (die, cu))
7609 return dwarf2_compute_name (name, die, cu, 1);
7610
7611 back_to = make_cleanup (null_cleanup, NULL);
7612
7613 attr = dwarf2_attr (die, DW_AT_linkage_name, cu);
7614 if (!attr)
7615 attr = dwarf2_attr (die, DW_AT_MIPS_linkage_name, cu);
7616
7617 /* DW_AT_linkage_name is missing in some cases - depend on what GDB
7618 has computed. */
7619 if (attr && DW_STRING (attr))
7620 {
7621 char *demangled;
7622
7623 mangled = DW_STRING (attr);
7624
7625 /* Use DMGL_RET_DROP for C++ template functions to suppress their return
7626 type. It is easier for GDB users to search for such functions as
7627 `name(params)' than `long name(params)'. In such case the minimal
7628 symbol names do not match the full symbol names but for template
7629 functions there is never a need to look up their definition from their
7630 declaration so the only disadvantage remains the minimal symbol
7631 variant `long name(params)' does not have the proper inferior type.
7632 */
7633
a766d390
DE
7634 if (cu->language == language_go)
7635 {
7636 /* This is a lie, but we already lie to the caller new_symbol_full.
7637 new_symbol_full assumes we return the mangled name.
7638 This just undoes that lie until things are cleaned up. */
7639 demangled = NULL;
7640 }
7641 else
7642 {
7643 demangled = cplus_demangle (mangled,
7644 (DMGL_PARAMS | DMGL_ANSI
7645 | (cu->language == language_java
7646 ? DMGL_JAVA | DMGL_RET_POSTFIX
7647 : DMGL_RET_DROP)));
7648 }
900e11f9
JK
7649 if (demangled)
7650 {
7651 make_cleanup (xfree, demangled);
7652 canon = demangled;
7653 }
7654 else
7655 {
7656 canon = mangled;
7657 need_copy = 0;
7658 }
7659 }
7660
7661 if (canon == NULL || check_physname)
7662 {
7663 const char *physname = dwarf2_compute_name (name, die, cu, 1);
7664
7665 if (canon != NULL && strcmp (physname, canon) != 0)
7666 {
7667 /* It may not mean a bug in GDB. The compiler could also
7668 compute DW_AT_linkage_name incorrectly. But in such case
7669 GDB would need to be bug-to-bug compatible. */
7670
7671 complaint (&symfile_complaints,
7672 _("Computed physname <%s> does not match demangled <%s> "
7673 "(from linkage <%s>) - DIE at 0x%x [in module %s]"),
b64f50a1 7674 physname, canon, mangled, die->offset.sect_off, objfile->name);
900e11f9
JK
7675
7676 /* Prefer DW_AT_linkage_name (in the CANON form) - when it
7677 is available here - over computed PHYSNAME. It is safer
7678 against both buggy GDB and buggy compilers. */
7679
7680 retval = canon;
7681 }
7682 else
7683 {
7684 retval = physname;
7685 need_copy = 0;
7686 }
7687 }
7688 else
7689 retval = canon;
7690
7691 if (need_copy)
10f0c4bb 7692 retval = obstack_copy0 (&objfile->objfile_obstack, retval, strlen (retval));
900e11f9
JK
7693
7694 do_cleanups (back_to);
7695 return retval;
0114d602
DJ
7696}
7697
27aa8d6a
SW
7698/* Read the import statement specified by the given die and record it. */
7699
7700static void
7701read_import_statement (struct die_info *die, struct dwarf2_cu *cu)
7702{
bb5ed363 7703 struct objfile *objfile = cu->objfile;
27aa8d6a 7704 struct attribute *import_attr;
32019081 7705 struct die_info *imported_die, *child_die;
de4affc9 7706 struct dwarf2_cu *imported_cu;
27aa8d6a 7707 const char *imported_name;
794684b6 7708 const char *imported_name_prefix;
13387711
SW
7709 const char *canonical_name;
7710 const char *import_alias;
7711 const char *imported_declaration = NULL;
794684b6 7712 const char *import_prefix;
32019081
JK
7713 VEC (const_char_ptr) *excludes = NULL;
7714 struct cleanup *cleanups;
13387711 7715
27aa8d6a
SW
7716 import_attr = dwarf2_attr (die, DW_AT_import, cu);
7717 if (import_attr == NULL)
7718 {
7719 complaint (&symfile_complaints, _("Tag '%s' has no DW_AT_import"),
7720 dwarf_tag_name (die->tag));
7721 return;
7722 }
7723
de4affc9
CC
7724 imported_cu = cu;
7725 imported_die = follow_die_ref_or_sig (die, import_attr, &imported_cu);
7726 imported_name = dwarf2_name (imported_die, imported_cu);
27aa8d6a
SW
7727 if (imported_name == NULL)
7728 {
7729 /* GCC bug: https://bugzilla.redhat.com/show_bug.cgi?id=506524
7730
7731 The import in the following code:
7732 namespace A
7733 {
7734 typedef int B;
7735 }
7736
7737 int main ()
7738 {
7739 using A::B;
7740 B b;
7741 return b;
7742 }
7743
7744 ...
7745 <2><51>: Abbrev Number: 3 (DW_TAG_imported_declaration)
7746 <52> DW_AT_decl_file : 1
7747 <53> DW_AT_decl_line : 6
7748 <54> DW_AT_import : <0x75>
7749 <2><58>: Abbrev Number: 4 (DW_TAG_typedef)
7750 <59> DW_AT_name : B
7751 <5b> DW_AT_decl_file : 1
7752 <5c> DW_AT_decl_line : 2
7753 <5d> DW_AT_type : <0x6e>
7754 ...
7755 <1><75>: Abbrev Number: 7 (DW_TAG_base_type)
7756 <76> DW_AT_byte_size : 4
7757 <77> DW_AT_encoding : 5 (signed)
7758
7759 imports the wrong die ( 0x75 instead of 0x58 ).
7760 This case will be ignored until the gcc bug is fixed. */
7761 return;
7762 }
7763
82856980
SW
7764 /* Figure out the local name after import. */
7765 import_alias = dwarf2_name (die, cu);
27aa8d6a 7766
794684b6
SW
7767 /* Figure out where the statement is being imported to. */
7768 import_prefix = determine_prefix (die, cu);
7769
7770 /* Figure out what the scope of the imported die is and prepend it
7771 to the name of the imported die. */
de4affc9 7772 imported_name_prefix = determine_prefix (imported_die, imported_cu);
794684b6 7773
f55ee35c
JK
7774 if (imported_die->tag != DW_TAG_namespace
7775 && imported_die->tag != DW_TAG_module)
794684b6 7776 {
13387711
SW
7777 imported_declaration = imported_name;
7778 canonical_name = imported_name_prefix;
794684b6 7779 }
13387711 7780 else if (strlen (imported_name_prefix) > 0)
12aaed36
TT
7781 canonical_name = obconcat (&objfile->objfile_obstack,
7782 imported_name_prefix, "::", imported_name,
7783 (char *) NULL);
13387711
SW
7784 else
7785 canonical_name = imported_name;
794684b6 7786
32019081
JK
7787 cleanups = make_cleanup (VEC_cleanup (const_char_ptr), &excludes);
7788
7789 if (die->tag == DW_TAG_imported_module && cu->language == language_fortran)
7790 for (child_die = die->child; child_die && child_die->tag;
7791 child_die = sibling_die (child_die))
7792 {
7793 /* DWARF-4: A Fortran use statement with a “rename list” may be
7794 represented by an imported module entry with an import attribute
7795 referring to the module and owned entries corresponding to those
7796 entities that are renamed as part of being imported. */
7797
7798 if (child_die->tag != DW_TAG_imported_declaration)
7799 {
7800 complaint (&symfile_complaints,
7801 _("child DW_TAG_imported_declaration expected "
7802 "- DIE at 0x%x [in module %s]"),
b64f50a1 7803 child_die->offset.sect_off, objfile->name);
32019081
JK
7804 continue;
7805 }
7806
7807 import_attr = dwarf2_attr (child_die, DW_AT_import, cu);
7808 if (import_attr == NULL)
7809 {
7810 complaint (&symfile_complaints, _("Tag '%s' has no DW_AT_import"),
7811 dwarf_tag_name (child_die->tag));
7812 continue;
7813 }
7814
7815 imported_cu = cu;
7816 imported_die = follow_die_ref_or_sig (child_die, import_attr,
7817 &imported_cu);
7818 imported_name = dwarf2_name (imported_die, imported_cu);
7819 if (imported_name == NULL)
7820 {
7821 complaint (&symfile_complaints,
7822 _("child DW_TAG_imported_declaration has unknown "
7823 "imported name - DIE at 0x%x [in module %s]"),
b64f50a1 7824 child_die->offset.sect_off, objfile->name);
32019081
JK
7825 continue;
7826 }
7827
7828 VEC_safe_push (const_char_ptr, excludes, imported_name);
7829
7830 process_die (child_die, cu);
7831 }
7832
c0cc3a76
SW
7833 cp_add_using_directive (import_prefix,
7834 canonical_name,
7835 import_alias,
13387711 7836 imported_declaration,
32019081 7837 excludes,
12aaed36 7838 0,
bb5ed363 7839 &objfile->objfile_obstack);
32019081
JK
7840
7841 do_cleanups (cleanups);
27aa8d6a
SW
7842}
7843
f4dc4d17 7844/* Cleanup function for handle_DW_AT_stmt_list. */
ae2de4f8 7845
cb1df416
DJ
7846static void
7847free_cu_line_header (void *arg)
7848{
7849 struct dwarf2_cu *cu = arg;
7850
7851 free_line_header (cu->line_header);
7852 cu->line_header = NULL;
7853}
7854
1b80a9fa
JK
7855/* Check for possibly missing DW_AT_comp_dir with relative .debug_line
7856 directory paths. GCC SVN r127613 (new option -fdebug-prefix-map) fixed
7857 this, it was first present in GCC release 4.3.0. */
7858
7859static int
7860producer_is_gcc_lt_4_3 (struct dwarf2_cu *cu)
7861{
7862 if (!cu->checked_producer)
7863 check_producer (cu);
7864
7865 return cu->producer_is_gcc_lt_4_3;
7866}
7867
9291a0cd
TT
7868static void
7869find_file_and_directory (struct die_info *die, struct dwarf2_cu *cu,
15d034d0 7870 const char **name, const char **comp_dir)
9291a0cd
TT
7871{
7872 struct attribute *attr;
7873
7874 *name = NULL;
7875 *comp_dir = NULL;
7876
7877 /* Find the filename. Do not use dwarf2_name here, since the filename
7878 is not a source language identifier. */
7879 attr = dwarf2_attr (die, DW_AT_name, cu);
7880 if (attr)
7881 {
7882 *name = DW_STRING (attr);
7883 }
7884
7885 attr = dwarf2_attr (die, DW_AT_comp_dir, cu);
7886 if (attr)
7887 *comp_dir = DW_STRING (attr);
1b80a9fa
JK
7888 else if (producer_is_gcc_lt_4_3 (cu) && *name != NULL
7889 && IS_ABSOLUTE_PATH (*name))
9291a0cd 7890 {
15d034d0
TT
7891 char *d = ldirname (*name);
7892
7893 *comp_dir = d;
7894 if (d != NULL)
7895 make_cleanup (xfree, d);
9291a0cd
TT
7896 }
7897 if (*comp_dir != NULL)
7898 {
7899 /* Irix 6.2 native cc prepends <machine>.: to the compilation
7900 directory, get rid of it. */
7901 char *cp = strchr (*comp_dir, ':');
7902
7903 if (cp && cp != *comp_dir && cp[-1] == '.' && cp[1] == '/')
7904 *comp_dir = cp + 1;
7905 }
7906
7907 if (*name == NULL)
7908 *name = "<unknown>";
7909}
7910
f4dc4d17
DE
7911/* Handle DW_AT_stmt_list for a compilation unit.
7912 DIE is the DW_TAG_compile_unit die for CU.
f3f5162e
DE
7913 COMP_DIR is the compilation directory.
7914 WANT_LINE_INFO is non-zero if the pc/line-number mapping is needed. */
2ab95328
TT
7915
7916static void
7917handle_DW_AT_stmt_list (struct die_info *die, struct dwarf2_cu *cu,
f4dc4d17 7918 const char *comp_dir)
2ab95328
TT
7919{
7920 struct attribute *attr;
2ab95328 7921
f4dc4d17
DE
7922 gdb_assert (! cu->per_cu->is_debug_types);
7923
2ab95328
TT
7924 attr = dwarf2_attr (die, DW_AT_stmt_list, cu);
7925 if (attr)
7926 {
7927 unsigned int line_offset = DW_UNSND (attr);
7928 struct line_header *line_header
3019eac3 7929 = dwarf_decode_line_header (line_offset, cu);
2ab95328
TT
7930
7931 if (line_header)
dee91e82
DE
7932 {
7933 cu->line_header = line_header;
7934 make_cleanup (free_cu_line_header, cu);
f4dc4d17 7935 dwarf_decode_lines (line_header, comp_dir, cu, NULL, 1);
dee91e82 7936 }
2ab95328
TT
7937 }
7938}
7939
95554aad 7940/* Process DW_TAG_compile_unit or DW_TAG_partial_unit. */
ae2de4f8 7941
c906108c 7942static void
e7c27a73 7943read_file_scope (struct die_info *die, struct dwarf2_cu *cu)
c906108c 7944{
dee91e82 7945 struct objfile *objfile = dwarf2_per_objfile->objfile;
debd256d 7946 struct cleanup *back_to = make_cleanup (null_cleanup, 0);
2acceee2 7947 CORE_ADDR lowpc = ((CORE_ADDR) -1);
c906108c
SS
7948 CORE_ADDR highpc = ((CORE_ADDR) 0);
7949 struct attribute *attr;
15d034d0
TT
7950 const char *name = NULL;
7951 const char *comp_dir = NULL;
c906108c
SS
7952 struct die_info *child_die;
7953 bfd *abfd = objfile->obfd;
e142c38c 7954 CORE_ADDR baseaddr;
6e70227d 7955
e142c38c 7956 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
c906108c 7957
fae299cd 7958 get_scope_pc_bounds (die, &lowpc, &highpc, cu);
c906108c
SS
7959
7960 /* If we didn't find a lowpc, set it to highpc to avoid complaints
7961 from finish_block. */
2acceee2 7962 if (lowpc == ((CORE_ADDR) -1))
c906108c
SS
7963 lowpc = highpc;
7964 lowpc += baseaddr;
7965 highpc += baseaddr;
7966
9291a0cd 7967 find_file_and_directory (die, cu, &name, &comp_dir);
e1024ff1 7968
95554aad 7969 prepare_one_comp_unit (cu, die, cu->language);
303b6f5d 7970
f4b8a18d
KW
7971 /* The XLCL doesn't generate DW_LANG_OpenCL because this attribute is not
7972 standardised yet. As a workaround for the language detection we fall
7973 back to the DW_AT_producer string. */
7974 if (cu->producer && strstr (cu->producer, "IBM XL C for OpenCL") != NULL)
7975 cu->language = language_opencl;
7976
3019eac3
DE
7977 /* Similar hack for Go. */
7978 if (cu->producer && strstr (cu->producer, "GNU Go ") != NULL)
7979 set_cu_language (DW_LANG_Go, cu);
7980
f4dc4d17 7981 dwarf2_start_symtab (cu, name, comp_dir, lowpc);
3019eac3
DE
7982
7983 /* Decode line number information if present. We do this before
7984 processing child DIEs, so that the line header table is available
7985 for DW_AT_decl_file. */
f4dc4d17 7986 handle_DW_AT_stmt_list (die, cu, comp_dir);
3019eac3
DE
7987
7988 /* Process all dies in compilation unit. */
7989 if (die->child != NULL)
7990 {
7991 child_die = die->child;
7992 while (child_die && child_die->tag)
7993 {
7994 process_die (child_die, cu);
7995 child_die = sibling_die (child_die);
7996 }
7997 }
7998
7999 /* Decode macro information, if present. Dwarf 2 macro information
8000 refers to information in the line number info statement program
8001 header, so we can only read it if we've read the header
8002 successfully. */
8003 attr = dwarf2_attr (die, DW_AT_GNU_macros, cu);
8004 if (attr && cu->line_header)
8005 {
8006 if (dwarf2_attr (die, DW_AT_macro_info, cu))
8007 complaint (&symfile_complaints,
8008 _("CU refers to both DW_AT_GNU_macros and DW_AT_macro_info"));
8009
09262596 8010 dwarf_decode_macros (cu, DW_UNSND (attr), comp_dir, 1);
3019eac3
DE
8011 }
8012 else
8013 {
8014 attr = dwarf2_attr (die, DW_AT_macro_info, cu);
8015 if (attr && cu->line_header)
8016 {
8017 unsigned int macro_offset = DW_UNSND (attr);
8018
09262596 8019 dwarf_decode_macros (cu, macro_offset, comp_dir, 0);
3019eac3
DE
8020 }
8021 }
8022
8023 do_cleanups (back_to);
8024}
8025
f4dc4d17
DE
8026/* TU version of handle_DW_AT_stmt_list for read_type_unit_scope.
8027 Create the set of symtabs used by this TU, or if this TU is sharing
8028 symtabs with another TU and the symtabs have already been created
8029 then restore those symtabs in the line header.
8030 We don't need the pc/line-number mapping for type units. */
3019eac3
DE
8031
8032static void
f4dc4d17 8033setup_type_unit_groups (struct die_info *die, struct dwarf2_cu *cu)
3019eac3 8034{
f4dc4d17
DE
8035 struct objfile *objfile = dwarf2_per_objfile->objfile;
8036 struct dwarf2_per_cu_data *per_cu = cu->per_cu;
8037 struct type_unit_group *tu_group;
8038 int first_time;
8039 struct line_header *lh;
3019eac3 8040 struct attribute *attr;
f4dc4d17 8041 unsigned int i, line_offset;
3019eac3 8042
f4dc4d17 8043 gdb_assert (per_cu->is_debug_types);
3019eac3 8044
f4dc4d17 8045 attr = dwarf2_attr (die, DW_AT_stmt_list, cu);
3019eac3 8046
f4dc4d17
DE
8047 /* If we're using .gdb_index (includes -readnow) then
8048 per_cu->s.type_unit_group may not have been set up yet. */
796a7ff8
DE
8049 if (per_cu->type_unit_group == NULL)
8050 per_cu->type_unit_group = get_type_unit_group (cu, attr);
8051 tu_group = per_cu->type_unit_group;
f4dc4d17
DE
8052
8053 /* If we've already processed this stmt_list there's no real need to
8054 do it again, we could fake it and just recreate the part we need
8055 (file name,index -> symtab mapping). If data shows this optimization
8056 is useful we can do it then. */
8057 first_time = tu_group->primary_symtab == NULL;
8058
8059 /* We have to handle the case of both a missing DW_AT_stmt_list or bad
8060 debug info. */
8061 lh = NULL;
8062 if (attr != NULL)
3019eac3 8063 {
f4dc4d17
DE
8064 line_offset = DW_UNSND (attr);
8065 lh = dwarf_decode_line_header (line_offset, cu);
8066 }
8067 if (lh == NULL)
8068 {
8069 if (first_time)
8070 dwarf2_start_symtab (cu, "", NULL, 0);
8071 else
8072 {
8073 gdb_assert (tu_group->symtabs == NULL);
8074 restart_symtab (0);
8075 }
8076 /* Note: The primary symtab will get allocated at the end. */
8077 return;
3019eac3
DE
8078 }
8079
f4dc4d17
DE
8080 cu->line_header = lh;
8081 make_cleanup (free_cu_line_header, cu);
3019eac3 8082
f4dc4d17
DE
8083 if (first_time)
8084 {
8085 dwarf2_start_symtab (cu, "", NULL, 0);
3019eac3 8086
f4dc4d17
DE
8087 tu_group->num_symtabs = lh->num_file_names;
8088 tu_group->symtabs = XNEWVEC (struct symtab *, lh->num_file_names);
3019eac3 8089
f4dc4d17
DE
8090 for (i = 0; i < lh->num_file_names; ++i)
8091 {
8092 char *dir = NULL;
8093 struct file_entry *fe = &lh->file_names[i];
3019eac3 8094
f4dc4d17
DE
8095 if (fe->dir_index)
8096 dir = lh->include_dirs[fe->dir_index - 1];
8097 dwarf2_start_subfile (fe->name, dir, NULL);
3019eac3 8098
f4dc4d17
DE
8099 /* Note: We don't have to watch for the main subfile here, type units
8100 don't have DW_AT_name. */
3019eac3 8101
f4dc4d17
DE
8102 if (current_subfile->symtab == NULL)
8103 {
8104 /* NOTE: start_subfile will recognize when it's been passed
8105 a file it has already seen. So we can't assume there's a
8106 simple mapping from lh->file_names to subfiles,
8107 lh->file_names may contain dups. */
8108 current_subfile->symtab = allocate_symtab (current_subfile->name,
8109 objfile);
8110 }
8111
8112 fe->symtab = current_subfile->symtab;
8113 tu_group->symtabs[i] = fe->symtab;
8114 }
8115 }
8116 else
3019eac3 8117 {
f4dc4d17
DE
8118 restart_symtab (0);
8119
8120 for (i = 0; i < lh->num_file_names; ++i)
8121 {
8122 struct file_entry *fe = &lh->file_names[i];
8123
8124 fe->symtab = tu_group->symtabs[i];
8125 }
3019eac3
DE
8126 }
8127
f4dc4d17
DE
8128 /* The main symtab is allocated last. Type units don't have DW_AT_name
8129 so they don't have a "real" (so to speak) symtab anyway.
8130 There is later code that will assign the main symtab to all symbols
8131 that don't have one. We need to handle the case of a symbol with a
8132 missing symtab (DW_AT_decl_file) anyway. */
8133}
3019eac3 8134
f4dc4d17
DE
8135/* Process DW_TAG_type_unit.
8136 For TUs we want to skip the first top level sibling if it's not the
8137 actual type being defined by this TU. In this case the first top
8138 level sibling is there to provide context only. */
3019eac3 8139
f4dc4d17
DE
8140static void
8141read_type_unit_scope (struct die_info *die, struct dwarf2_cu *cu)
8142{
8143 struct die_info *child_die;
3019eac3 8144
f4dc4d17
DE
8145 prepare_one_comp_unit (cu, die, language_minimal);
8146
8147 /* Initialize (or reinitialize) the machinery for building symtabs.
8148 We do this before processing child DIEs, so that the line header table
8149 is available for DW_AT_decl_file. */
8150 setup_type_unit_groups (die, cu);
8151
8152 if (die->child != NULL)
8153 {
8154 child_die = die->child;
8155 while (child_die && child_die->tag)
8156 {
8157 process_die (child_die, cu);
8158 child_die = sibling_die (child_die);
8159 }
8160 }
3019eac3
DE
8161}
8162\f
80626a55
DE
8163/* DWO/DWP files.
8164
8165 http://gcc.gnu.org/wiki/DebugFission
8166 http://gcc.gnu.org/wiki/DebugFissionDWP
8167
8168 To simplify handling of both DWO files ("object" files with the DWARF info)
8169 and DWP files (a file with the DWOs packaged up into one file), we treat
8170 DWP files as having a collection of virtual DWO files. */
3019eac3
DE
8171
8172static hashval_t
8173hash_dwo_file (const void *item)
8174{
8175 const struct dwo_file *dwo_file = item;
8176
80626a55 8177 return htab_hash_string (dwo_file->name);
3019eac3
DE
8178}
8179
8180static int
8181eq_dwo_file (const void *item_lhs, const void *item_rhs)
8182{
8183 const struct dwo_file *lhs = item_lhs;
8184 const struct dwo_file *rhs = item_rhs;
8185
80626a55 8186 return strcmp (lhs->name, rhs->name) == 0;
3019eac3
DE
8187}
8188
8189/* Allocate a hash table for DWO files. */
8190
8191static htab_t
8192allocate_dwo_file_hash_table (void)
8193{
8194 struct objfile *objfile = dwarf2_per_objfile->objfile;
8195
8196 return htab_create_alloc_ex (41,
8197 hash_dwo_file,
8198 eq_dwo_file,
8199 NULL,
8200 &objfile->objfile_obstack,
8201 hashtab_obstack_allocate,
8202 dummy_obstack_deallocate);
8203}
8204
80626a55
DE
8205/* Lookup DWO file DWO_NAME. */
8206
8207static void **
8208lookup_dwo_file_slot (const char *dwo_name)
8209{
8210 struct dwo_file find_entry;
8211 void **slot;
8212
8213 if (dwarf2_per_objfile->dwo_files == NULL)
8214 dwarf2_per_objfile->dwo_files = allocate_dwo_file_hash_table ();
8215
8216 memset (&find_entry, 0, sizeof (find_entry));
8217 find_entry.name = dwo_name;
8218 slot = htab_find_slot (dwarf2_per_objfile->dwo_files, &find_entry, INSERT);
8219
8220 return slot;
8221}
8222
3019eac3
DE
8223static hashval_t
8224hash_dwo_unit (const void *item)
8225{
8226 const struct dwo_unit *dwo_unit = item;
8227
8228 /* This drops the top 32 bits of the id, but is ok for a hash. */
8229 return dwo_unit->signature;
8230}
8231
8232static int
8233eq_dwo_unit (const void *item_lhs, const void *item_rhs)
8234{
8235 const struct dwo_unit *lhs = item_lhs;
8236 const struct dwo_unit *rhs = item_rhs;
8237
8238 /* The signature is assumed to be unique within the DWO file.
8239 So while object file CU dwo_id's always have the value zero,
8240 that's OK, assuming each object file DWO file has only one CU,
8241 and that's the rule for now. */
8242 return lhs->signature == rhs->signature;
8243}
8244
8245/* Allocate a hash table for DWO CUs,TUs.
8246 There is one of these tables for each of CUs,TUs for each DWO file. */
8247
8248static htab_t
8249allocate_dwo_unit_table (struct objfile *objfile)
8250{
8251 /* Start out with a pretty small number.
8252 Generally DWO files contain only one CU and maybe some TUs. */
8253 return htab_create_alloc_ex (3,
8254 hash_dwo_unit,
8255 eq_dwo_unit,
8256 NULL,
8257 &objfile->objfile_obstack,
8258 hashtab_obstack_allocate,
8259 dummy_obstack_deallocate);
8260}
8261
80626a55 8262/* Structure used to pass data to create_dwo_debug_info_hash_table_reader. */
3019eac3
DE
8263
8264struct create_dwo_info_table_data
8265{
8266 struct dwo_file *dwo_file;
8267 htab_t cu_htab;
8268};
8269
80626a55 8270/* die_reader_func for create_dwo_debug_info_hash_table. */
3019eac3
DE
8271
8272static void
80626a55
DE
8273create_dwo_debug_info_hash_table_reader (const struct die_reader_specs *reader,
8274 gdb_byte *info_ptr,
8275 struct die_info *comp_unit_die,
8276 int has_children,
8277 void *datap)
3019eac3
DE
8278{
8279 struct dwarf2_cu *cu = reader->cu;
8280 struct objfile *objfile = dwarf2_per_objfile->objfile;
8281 sect_offset offset = cu->per_cu->offset;
8282 struct dwarf2_section_info *section = cu->per_cu->info_or_types_section;
8283 struct create_dwo_info_table_data *data = datap;
8284 struct dwo_file *dwo_file = data->dwo_file;
8285 htab_t cu_htab = data->cu_htab;
8286 void **slot;
8287 struct attribute *attr;
8288 struct dwo_unit *dwo_unit;
8289
8290 attr = dwarf2_attr (comp_unit_die, DW_AT_GNU_dwo_id, cu);
8291 if (attr == NULL)
8292 {
8293 error (_("Dwarf Error: debug entry at offset 0x%x is missing"
8294 " its dwo_id [in module %s]"),
80626a55 8295 offset.sect_off, dwo_file->name);
3019eac3
DE
8296 return;
8297 }
8298
8299 dwo_unit = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct dwo_unit);
8300 dwo_unit->dwo_file = dwo_file;
8301 dwo_unit->signature = DW_UNSND (attr);
8302 dwo_unit->info_or_types_section = section;
8303 dwo_unit->offset = offset;
8304 dwo_unit->length = cu->per_cu->length;
8305
8306 slot = htab_find_slot (cu_htab, dwo_unit, INSERT);
8307 gdb_assert (slot != NULL);
8308 if (*slot != NULL)
8309 {
8310 const struct dwo_unit *dup_dwo_unit = *slot;
8311
8312 complaint (&symfile_complaints,
8313 _("debug entry at offset 0x%x is duplicate to the entry at"
8314 " offset 0x%x, dwo_id 0x%s [in module %s]"),
8315 offset.sect_off, dup_dwo_unit->offset.sect_off,
8316 phex (dwo_unit->signature, sizeof (dwo_unit->signature)),
80626a55 8317 dwo_file->name);
3019eac3
DE
8318 }
8319 else
8320 *slot = dwo_unit;
8321
09406207 8322 if (dwarf2_read_debug)
3019eac3
DE
8323 fprintf_unfiltered (gdb_stdlog, " offset 0x%x, dwo_id 0x%s\n",
8324 offset.sect_off,
8325 phex (dwo_unit->signature,
8326 sizeof (dwo_unit->signature)));
8327}
8328
80626a55
DE
8329/* Create a hash table to map DWO IDs to their CU entry in
8330 .debug_info.dwo in DWO_FILE.
8331 Note: This function processes DWO files only, not DWP files. */
3019eac3
DE
8332
8333static htab_t
80626a55 8334create_dwo_debug_info_hash_table (struct dwo_file *dwo_file)
3019eac3
DE
8335{
8336 struct objfile *objfile = dwarf2_per_objfile->objfile;
8337 struct dwarf2_section_info *section = &dwo_file->sections.info;
8338 bfd *abfd;
8339 htab_t cu_htab;
8340 gdb_byte *info_ptr, *end_ptr;
8341 struct create_dwo_info_table_data create_dwo_info_table_data;
8342
8343 dwarf2_read_section (objfile, section);
8344 info_ptr = section->buffer;
8345
8346 if (info_ptr == NULL)
8347 return NULL;
8348
8349 /* We can't set abfd until now because the section may be empty or
8350 not present, in which case section->asection will be NULL. */
8351 abfd = section->asection->owner;
8352
09406207 8353 if (dwarf2_read_debug)
3019eac3
DE
8354 fprintf_unfiltered (gdb_stdlog, "Reading .debug_info.dwo for %s:\n",
8355 bfd_get_filename (abfd));
8356
8357 cu_htab = allocate_dwo_unit_table (objfile);
8358
8359 create_dwo_info_table_data.dwo_file = dwo_file;
8360 create_dwo_info_table_data.cu_htab = cu_htab;
8361
8362 end_ptr = info_ptr + section->size;
8363 while (info_ptr < end_ptr)
8364 {
8365 struct dwarf2_per_cu_data per_cu;
8366
8367 memset (&per_cu, 0, sizeof (per_cu));
8368 per_cu.objfile = objfile;
8369 per_cu.is_debug_types = 0;
8370 per_cu.offset.sect_off = info_ptr - section->buffer;
8371 per_cu.info_or_types_section = section;
8372
8373 init_cutu_and_read_dies_no_follow (&per_cu,
8374 &dwo_file->sections.abbrev,
8375 dwo_file,
80626a55 8376 create_dwo_debug_info_hash_table_reader,
3019eac3
DE
8377 &create_dwo_info_table_data);
8378
8379 info_ptr += per_cu.length;
8380 }
8381
8382 return cu_htab;
8383}
8384
80626a55
DE
8385/* DWP file .debug_{cu,tu}_index section format:
8386 [ref: http://gcc.gnu.org/wiki/DebugFissionDWP]
8387
8388 Both index sections have the same format, and serve to map a 64-bit
8389 signature to a set of section numbers. Each section begins with a header,
8390 followed by a hash table of 64-bit signatures, a parallel table of 32-bit
8391 indexes, and a pool of 32-bit section numbers. The index sections will be
8392 aligned at 8-byte boundaries in the file.
8393
8394 The index section header contains two unsigned 32-bit values (using the
8395 byte order of the application binary):
8396
8397 N, the number of compilation units or type units in the index
8398 M, the number of slots in the hash table
8399
8400 (We assume that N and M will not exceed 2^32 - 1.)
8401
8402 The size of the hash table, M, must be 2^k such that 2^k > 3*N/2.
8403
8404 The hash table begins at offset 8 in the section, and consists of an array
8405 of M 64-bit slots. Each slot contains a 64-bit signature (using the byte
8406 order of the application binary). Unused slots in the hash table are 0.
8407 (We rely on the extreme unlikeliness of a signature being exactly 0.)
8408
8409 The parallel table begins immediately after the hash table
8410 (at offset 8 + 8 * M from the beginning of the section), and consists of an
8411 array of 32-bit indexes (using the byte order of the application binary),
8412 corresponding 1-1 with slots in the hash table. Each entry in the parallel
8413 table contains a 32-bit index into the pool of section numbers. For unused
8414 hash table slots, the corresponding entry in the parallel table will be 0.
8415
8416 Given a 64-bit compilation unit signature or a type signature S, an entry
8417 in the hash table is located as follows:
8418
8419 1) Calculate a primary hash H = S & MASK(k), where MASK(k) is a mask with
8420 the low-order k bits all set to 1.
8421
8422 2) Calculate a secondary hash H' = (((S >> 32) & MASK(k)) | 1).
8423
8424 3) If the hash table entry at index H matches the signature, use that
8425 entry. If the hash table entry at index H is unused (all zeroes),
8426 terminate the search: the signature is not present in the table.
8427
8428 4) Let H = (H + H') modulo M. Repeat at Step 3.
8429
8430 Because M > N and H' and M are relatively prime, the search is guaranteed
8431 to stop at an unused slot or find the match.
8432
8433 The pool of section numbers begins immediately following the hash table
8434 (at offset 8 + 12 * M from the beginning of the section). The pool of
8435 section numbers consists of an array of 32-bit words (using the byte order
8436 of the application binary). Each item in the array is indexed starting
8437 from 0. The hash table entry provides the index of the first section
8438 number in the set. Additional section numbers in the set follow, and the
8439 set is terminated by a 0 entry (section number 0 is not used in ELF).
8440
8441 In each set of section numbers, the .debug_info.dwo or .debug_types.dwo
8442 section must be the first entry in the set, and the .debug_abbrev.dwo must
8443 be the second entry. Other members of the set may follow in any order. */
8444
8445/* Create a hash table to map DWO IDs to their CU/TU entry in
8446 .debug_{info,types}.dwo in DWP_FILE.
8447 Returns NULL if there isn't one.
8448 Note: This function processes DWP files only, not DWO files. */
8449
8450static struct dwp_hash_table *
8451create_dwp_hash_table (struct dwp_file *dwp_file, int is_debug_types)
8452{
8453 struct objfile *objfile = dwarf2_per_objfile->objfile;
8454 bfd *dbfd = dwp_file->dbfd;
8455 char *index_ptr, *index_end;
8456 struct dwarf2_section_info *index;
8457 uint32_t version, nr_units, nr_slots;
8458 struct dwp_hash_table *htab;
8459
8460 if (is_debug_types)
8461 index = &dwp_file->sections.tu_index;
8462 else
8463 index = &dwp_file->sections.cu_index;
8464
8465 if (dwarf2_section_empty_p (index))
8466 return NULL;
8467 dwarf2_read_section (objfile, index);
8468
8469 index_ptr = index->buffer;
8470 index_end = index_ptr + index->size;
8471
8472 version = read_4_bytes (dbfd, index_ptr);
8473 index_ptr += 8; /* Skip the unused word. */
8474 nr_units = read_4_bytes (dbfd, index_ptr);
8475 index_ptr += 4;
8476 nr_slots = read_4_bytes (dbfd, index_ptr);
8477 index_ptr += 4;
8478
8479 if (version != 1)
8480 {
8481 error (_("Dwarf Error: unsupported DWP file version (%u)"
8482 " [in module %s]"),
8483 version, dwp_file->name);
8484 }
8485 if (nr_slots != (nr_slots & -nr_slots))
8486 {
8487 error (_("Dwarf Error: number of slots in DWP hash table (%u)"
8488 " is not power of 2 [in module %s]"),
8489 nr_slots, dwp_file->name);
8490 }
8491
8492 htab = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct dwp_hash_table);
8493 htab->nr_units = nr_units;
8494 htab->nr_slots = nr_slots;
8495 htab->hash_table = index_ptr;
8496 htab->unit_table = htab->hash_table + sizeof (uint64_t) * nr_slots;
8497 htab->section_pool = htab->unit_table + sizeof (uint32_t) * nr_slots;
8498
8499 return htab;
8500}
8501
8502/* Update SECTIONS with the data from SECTP.
8503
8504 This function is like the other "locate" section routines that are
8505 passed to bfd_map_over_sections, but in this context the sections to
8506 read comes from the DWP hash table, not the full ELF section table.
8507
8508 The result is non-zero for success, or zero if an error was found. */
8509
8510static int
8511locate_virtual_dwo_sections (asection *sectp,
8512 struct virtual_dwo_sections *sections)
8513{
8514 const struct dwop_section_names *names = &dwop_section_names;
8515
8516 if (section_is_p (sectp->name, &names->abbrev_dwo))
8517 {
8518 /* There can be only one. */
8519 if (sections->abbrev.asection != NULL)
8520 return 0;
8521 sections->abbrev.asection = sectp;
8522 sections->abbrev.size = bfd_get_section_size (sectp);
8523 }
8524 else if (section_is_p (sectp->name, &names->info_dwo)
8525 || section_is_p (sectp->name, &names->types_dwo))
8526 {
8527 /* There can be only one. */
8528 if (sections->info_or_types.asection != NULL)
8529 return 0;
8530 sections->info_or_types.asection = sectp;
8531 sections->info_or_types.size = bfd_get_section_size (sectp);
8532 }
8533 else if (section_is_p (sectp->name, &names->line_dwo))
8534 {
8535 /* There can be only one. */
8536 if (sections->line.asection != NULL)
8537 return 0;
8538 sections->line.asection = sectp;
8539 sections->line.size = bfd_get_section_size (sectp);
8540 }
8541 else if (section_is_p (sectp->name, &names->loc_dwo))
8542 {
8543 /* There can be only one. */
8544 if (sections->loc.asection != NULL)
8545 return 0;
8546 sections->loc.asection = sectp;
8547 sections->loc.size = bfd_get_section_size (sectp);
8548 }
8549 else if (section_is_p (sectp->name, &names->macinfo_dwo))
8550 {
8551 /* There can be only one. */
8552 if (sections->macinfo.asection != NULL)
8553 return 0;
8554 sections->macinfo.asection = sectp;
8555 sections->macinfo.size = bfd_get_section_size (sectp);
8556 }
8557 else if (section_is_p (sectp->name, &names->macro_dwo))
8558 {
8559 /* There can be only one. */
8560 if (sections->macro.asection != NULL)
8561 return 0;
8562 sections->macro.asection = sectp;
8563 sections->macro.size = bfd_get_section_size (sectp);
8564 }
8565 else if (section_is_p (sectp->name, &names->str_offsets_dwo))
8566 {
8567 /* There can be only one. */
8568 if (sections->str_offsets.asection != NULL)
8569 return 0;
8570 sections->str_offsets.asection = sectp;
8571 sections->str_offsets.size = bfd_get_section_size (sectp);
8572 }
8573 else
8574 {
8575 /* No other kind of section is valid. */
8576 return 0;
8577 }
8578
8579 return 1;
8580}
8581
8582/* Create a dwo_unit object for the DWO with signature SIGNATURE.
8583 HTAB is the hash table from the DWP file.
8584 SECTION_INDEX is the index of the DWO in HTAB. */
8585
8586static struct dwo_unit *
8587create_dwo_in_dwp (struct dwp_file *dwp_file,
8588 const struct dwp_hash_table *htab,
8589 uint32_t section_index,
8590 ULONGEST signature, int is_debug_types)
8591{
8592 struct objfile *objfile = dwarf2_per_objfile->objfile;
8593 bfd *dbfd = dwp_file->dbfd;
8594 const char *kind = is_debug_types ? "TU" : "CU";
8595 struct dwo_file *dwo_file;
8596 struct dwo_unit *dwo_unit;
8597 struct virtual_dwo_sections sections;
8598 void **dwo_file_slot;
8599 char *virtual_dwo_name;
8600 struct dwarf2_section_info *cutu;
8601 struct cleanup *cleanups;
8602 int i;
8603
8604 if (dwarf2_read_debug)
8605 {
8606 fprintf_unfiltered (gdb_stdlog, "Reading %s %u/0x%s in DWP file: %s\n",
8607 kind,
8608 section_index, phex (signature, sizeof (signature)),
8609 dwp_file->name);
8610 }
8611
8612 /* Fetch the sections of this DWO.
8613 Put a limit on the number of sections we look for so that bad data
8614 doesn't cause us to loop forever. */
8615
8616#define MAX_NR_DWO_SECTIONS \
8617 (1 /* .debug_info or .debug_types */ \
8618 + 1 /* .debug_abbrev */ \
8619 + 1 /* .debug_line */ \
8620 + 1 /* .debug_loc */ \
8621 + 1 /* .debug_str_offsets */ \
8622 + 1 /* .debug_macro */ \
8623 + 1 /* .debug_macinfo */ \
8624 + 1 /* trailing zero */)
8625
8626 memset (&sections, 0, sizeof (sections));
8627 cleanups = make_cleanup (null_cleanup, 0);
8628
8629 for (i = 0; i < MAX_NR_DWO_SECTIONS; ++i)
8630 {
8631 asection *sectp;
8632 uint32_t section_nr =
8633 read_4_bytes (dbfd,
8634 htab->section_pool
8635 + (section_index + i) * sizeof (uint32_t));
8636
8637 if (section_nr == 0)
8638 break;
8639 if (section_nr >= dwp_file->num_sections)
8640 {
8641 error (_("Dwarf Error: bad DWP hash table, section number too large"
8642 " [in module %s]"),
8643 dwp_file->name);
8644 }
8645
8646 sectp = dwp_file->elf_sections[section_nr];
8647 if (! locate_virtual_dwo_sections (sectp, &sections))
8648 {
8649 error (_("Dwarf Error: bad DWP hash table, invalid section found"
8650 " [in module %s]"),
8651 dwp_file->name);
8652 }
8653 }
8654
8655 if (i < 2
8656 || sections.info_or_types.asection == NULL
8657 || sections.abbrev.asection == NULL)
8658 {
8659 error (_("Dwarf Error: bad DWP hash table, missing DWO sections"
8660 " [in module %s]"),
8661 dwp_file->name);
8662 }
8663 if (i == MAX_NR_DWO_SECTIONS)
8664 {
8665 error (_("Dwarf Error: bad DWP hash table, too many DWO sections"
8666 " [in module %s]"),
8667 dwp_file->name);
8668 }
8669
8670 /* It's easier for the rest of the code if we fake a struct dwo_file and
8671 have dwo_unit "live" in that. At least for now.
8672
8673 The DWP file can be made up of a random collection of CUs and TUs.
c766f7ec
DE
8674 However, for each CU + set of TUs that came from the same original DWO
8675 file, we want to combine them back into a virtual DWO file to save space
80626a55
DE
8676 (fewer struct dwo_file objects to allocated). Remember that for really
8677 large apps there can be on the order of 8K CUs and 200K TUs, or more. */
8678
2792b94d
PM
8679 virtual_dwo_name =
8680 xstrprintf ("virtual-dwo/%d-%d-%d-%d",
8681 sections.abbrev.asection ? sections.abbrev.asection->id : 0,
8682 sections.line.asection ? sections.line.asection->id : 0,
8683 sections.loc.asection ? sections.loc.asection->id : 0,
8684 (sections.str_offsets.asection
8685 ? sections.str_offsets.asection->id
8686 : 0));
80626a55
DE
8687 make_cleanup (xfree, virtual_dwo_name);
8688 /* Can we use an existing virtual DWO file? */
8689 dwo_file_slot = lookup_dwo_file_slot (virtual_dwo_name);
8690 /* Create one if necessary. */
8691 if (*dwo_file_slot == NULL)
8692 {
8693 if (dwarf2_read_debug)
8694 {
8695 fprintf_unfiltered (gdb_stdlog, "Creating virtual DWO: %s\n",
8696 virtual_dwo_name);
8697 }
8698 dwo_file = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct dwo_file);
8699 dwo_file->name = obstack_copy0 (&objfile->objfile_obstack,
8700 virtual_dwo_name,
8701 strlen (virtual_dwo_name));
8702 dwo_file->sections.abbrev = sections.abbrev;
8703 dwo_file->sections.line = sections.line;
8704 dwo_file->sections.loc = sections.loc;
8705 dwo_file->sections.macinfo = sections.macinfo;
8706 dwo_file->sections.macro = sections.macro;
8707 dwo_file->sections.str_offsets = sections.str_offsets;
8708 /* The "str" section is global to the entire DWP file. */
8709 dwo_file->sections.str = dwp_file->sections.str;
8710 /* The info or types section is assigned later to dwo_unit,
8711 there's no need to record it in dwo_file.
8712 Also, we can't simply record type sections in dwo_file because
8713 we record a pointer into the vector in dwo_unit. As we collect more
8714 types we'll grow the vector and eventually have to reallocate space
8715 for it, invalidating all the pointers into the current copy. */
8716 *dwo_file_slot = dwo_file;
8717 }
8718 else
8719 {
8720 if (dwarf2_read_debug)
8721 {
8722 fprintf_unfiltered (gdb_stdlog, "Using existing virtual DWO: %s\n",
8723 virtual_dwo_name);
8724 }
8725 dwo_file = *dwo_file_slot;
8726 }
8727 do_cleanups (cleanups);
8728
8729 dwo_unit = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct dwo_unit);
8730 dwo_unit->dwo_file = dwo_file;
8731 dwo_unit->signature = signature;
8732 dwo_unit->info_or_types_section =
8733 obstack_alloc (&objfile->objfile_obstack,
8734 sizeof (struct dwarf2_section_info));
8735 *dwo_unit->info_or_types_section = sections.info_or_types;
8736 /* offset, length, type_offset_in_tu are set later. */
8737
8738 return dwo_unit;
8739}
8740
8741/* Lookup the DWO with SIGNATURE in DWP_FILE. */
8742
8743static struct dwo_unit *
8744lookup_dwo_in_dwp (struct dwp_file *dwp_file,
8745 const struct dwp_hash_table *htab,
8746 ULONGEST signature, int is_debug_types)
8747{
8748 bfd *dbfd = dwp_file->dbfd;
8749 uint32_t mask = htab->nr_slots - 1;
8750 uint32_t hash = signature & mask;
8751 uint32_t hash2 = ((signature >> 32) & mask) | 1;
8752 unsigned int i;
8753 void **slot;
8754 struct dwo_unit find_dwo_cu, *dwo_cu;
8755
8756 memset (&find_dwo_cu, 0, sizeof (find_dwo_cu));
8757 find_dwo_cu.signature = signature;
8758 slot = htab_find_slot (dwp_file->loaded_cutus, &find_dwo_cu, INSERT);
8759
8760 if (*slot != NULL)
8761 return *slot;
8762
8763 /* Use a for loop so that we don't loop forever on bad debug info. */
8764 for (i = 0; i < htab->nr_slots; ++i)
8765 {
8766 ULONGEST signature_in_table;
8767
8768 signature_in_table =
8769 read_8_bytes (dbfd, htab->hash_table + hash * sizeof (uint64_t));
8770 if (signature_in_table == signature)
8771 {
8772 uint32_t section_index =
8773 read_4_bytes (dbfd, htab->unit_table + hash * sizeof (uint32_t));
8774
8775 *slot = create_dwo_in_dwp (dwp_file, htab, section_index,
8776 signature, is_debug_types);
8777 return *slot;
8778 }
8779 if (signature_in_table == 0)
8780 return NULL;
8781 hash = (hash + hash2) & mask;
8782 }
8783
8784 error (_("Dwarf Error: bad DWP hash table, lookup didn't terminate"
8785 " [in module %s]"),
8786 dwp_file->name);
8787}
8788
8789/* Subroutine of open_dwop_file to simplify it.
3019eac3
DE
8790 Open the file specified by FILE_NAME and hand it off to BFD for
8791 preliminary analysis. Return a newly initialized bfd *, which
8792 includes a canonicalized copy of FILE_NAME.
80626a55 8793 If IS_DWP is TRUE, we're opening a DWP file, otherwise a DWO file.
3019eac3
DE
8794 In case of trouble, return NULL.
8795 NOTE: This function is derived from symfile_bfd_open. */
8796
8797static bfd *
80626a55 8798try_open_dwop_file (const char *file_name, int is_dwp)
3019eac3
DE
8799{
8800 bfd *sym_bfd;
80626a55 8801 int desc, flags;
3019eac3 8802 char *absolute_name;
3019eac3 8803
80626a55
DE
8804 flags = OPF_TRY_CWD_FIRST;
8805 if (is_dwp)
8806 flags |= OPF_SEARCH_IN_PATH;
8807 desc = openp (debug_file_directory, flags, file_name,
3019eac3
DE
8808 O_RDONLY | O_BINARY, &absolute_name);
8809 if (desc < 0)
8810 return NULL;
8811
bb397797 8812 sym_bfd = gdb_bfd_open (absolute_name, gnutarget, desc);
3019eac3
DE
8813 if (!sym_bfd)
8814 {
3019eac3
DE
8815 xfree (absolute_name);
8816 return NULL;
8817 }
a4453b7e 8818 xfree (absolute_name);
3019eac3
DE
8819 bfd_set_cacheable (sym_bfd, 1);
8820
8821 if (!bfd_check_format (sym_bfd, bfd_object))
8822 {
cbb099e8 8823 gdb_bfd_unref (sym_bfd); /* This also closes desc. */
3019eac3
DE
8824 return NULL;
8825 }
8826
3019eac3
DE
8827 return sym_bfd;
8828}
8829
80626a55 8830/* Try to open DWO/DWP file FILE_NAME.
3019eac3 8831 COMP_DIR is the DW_AT_comp_dir attribute.
80626a55 8832 If IS_DWP is TRUE, we're opening a DWP file, otherwise a DWO file.
3019eac3
DE
8833 The result is the bfd handle of the file.
8834 If there is a problem finding or opening the file, return NULL.
8835 Upon success, the canonicalized path of the file is stored in the bfd,
8836 same as symfile_bfd_open. */
8837
8838static bfd *
80626a55 8839open_dwop_file (const char *file_name, const char *comp_dir, int is_dwp)
3019eac3
DE
8840{
8841 bfd *abfd;
3019eac3 8842
80626a55
DE
8843 if (IS_ABSOLUTE_PATH (file_name))
8844 return try_open_dwop_file (file_name, is_dwp);
3019eac3
DE
8845
8846 /* Before trying the search path, try DWO_NAME in COMP_DIR. */
8847
8848 if (comp_dir != NULL)
8849 {
80626a55 8850 char *path_to_try = concat (comp_dir, SLASH_STRING, file_name, NULL);
3019eac3
DE
8851
8852 /* NOTE: If comp_dir is a relative path, this will also try the
8853 search path, which seems useful. */
80626a55 8854 abfd = try_open_dwop_file (path_to_try, is_dwp);
3019eac3
DE
8855 xfree (path_to_try);
8856 if (abfd != NULL)
8857 return abfd;
8858 }
8859
8860 /* That didn't work, try debug-file-directory, which, despite its name,
8861 is a list of paths. */
8862
8863 if (*debug_file_directory == '\0')
8864 return NULL;
8865
80626a55 8866 return try_open_dwop_file (file_name, is_dwp);
3019eac3
DE
8867}
8868
80626a55
DE
8869/* This function is mapped across the sections and remembers the offset and
8870 size of each of the DWO debugging sections we are interested in. */
8871
8872static void
8873dwarf2_locate_dwo_sections (bfd *abfd, asection *sectp, void *dwo_sections_ptr)
8874{
8875 struct dwo_sections *dwo_sections = dwo_sections_ptr;
8876 const struct dwop_section_names *names = &dwop_section_names;
8877
8878 if (section_is_p (sectp->name, &names->abbrev_dwo))
8879 {
8880 dwo_sections->abbrev.asection = sectp;
8881 dwo_sections->abbrev.size = bfd_get_section_size (sectp);
8882 }
8883 else if (section_is_p (sectp->name, &names->info_dwo))
8884 {
8885 dwo_sections->info.asection = sectp;
8886 dwo_sections->info.size = bfd_get_section_size (sectp);
8887 }
8888 else if (section_is_p (sectp->name, &names->line_dwo))
8889 {
8890 dwo_sections->line.asection = sectp;
8891 dwo_sections->line.size = bfd_get_section_size (sectp);
8892 }
8893 else if (section_is_p (sectp->name, &names->loc_dwo))
8894 {
8895 dwo_sections->loc.asection = sectp;
8896 dwo_sections->loc.size = bfd_get_section_size (sectp);
8897 }
8898 else if (section_is_p (sectp->name, &names->macinfo_dwo))
8899 {
8900 dwo_sections->macinfo.asection = sectp;
8901 dwo_sections->macinfo.size = bfd_get_section_size (sectp);
8902 }
8903 else if (section_is_p (sectp->name, &names->macro_dwo))
8904 {
8905 dwo_sections->macro.asection = sectp;
8906 dwo_sections->macro.size = bfd_get_section_size (sectp);
8907 }
8908 else if (section_is_p (sectp->name, &names->str_dwo))
8909 {
8910 dwo_sections->str.asection = sectp;
8911 dwo_sections->str.size = bfd_get_section_size (sectp);
8912 }
8913 else if (section_is_p (sectp->name, &names->str_offsets_dwo))
8914 {
8915 dwo_sections->str_offsets.asection = sectp;
8916 dwo_sections->str_offsets.size = bfd_get_section_size (sectp);
8917 }
8918 else if (section_is_p (sectp->name, &names->types_dwo))
8919 {
8920 struct dwarf2_section_info type_section;
8921
8922 memset (&type_section, 0, sizeof (type_section));
8923 type_section.asection = sectp;
8924 type_section.size = bfd_get_section_size (sectp);
8925 VEC_safe_push (dwarf2_section_info_def, dwo_sections->types,
8926 &type_section);
8927 }
8928}
8929
8930/* Initialize the use of the DWO file specified by DWO_NAME.
8931 The result is NULL if DWO_NAME can't be found. */
3019eac3
DE
8932
8933static struct dwo_file *
80626a55 8934open_and_init_dwo_file (const char *dwo_name, const char *comp_dir)
3019eac3
DE
8935{
8936 struct objfile *objfile = dwarf2_per_objfile->objfile;
80626a55
DE
8937 struct dwo_file *dwo_file;
8938 bfd *dbfd;
3019eac3
DE
8939 struct cleanup *cleanups;
8940
80626a55
DE
8941 dbfd = open_dwop_file (dwo_name, comp_dir, 0);
8942 if (dbfd == NULL)
8943 {
8944 if (dwarf2_read_debug)
8945 fprintf_unfiltered (gdb_stdlog, "DWO file not found: %s\n", dwo_name);
8946 return NULL;
8947 }
8948 dwo_file = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct dwo_file);
8949 dwo_file->name = obstack_copy0 (&objfile->objfile_obstack,
8950 dwo_name, strlen (dwo_name));
8951 dwo_file->dbfd = dbfd;
3019eac3
DE
8952
8953 cleanups = make_cleanup (free_dwo_file_cleanup, dwo_file);
8954
80626a55 8955 bfd_map_over_sections (dbfd, dwarf2_locate_dwo_sections, &dwo_file->sections);
3019eac3 8956
80626a55 8957 dwo_file->cus = create_dwo_debug_info_hash_table (dwo_file);
3019eac3
DE
8958
8959 dwo_file->tus = create_debug_types_hash_table (dwo_file,
8960 dwo_file->sections.types);
8961
8962 discard_cleanups (cleanups);
8963
80626a55
DE
8964 if (dwarf2_read_debug)
8965 fprintf_unfiltered (gdb_stdlog, "DWO file found: %s\n", dwo_name);
8966
3019eac3
DE
8967 return dwo_file;
8968}
8969
80626a55
DE
8970/* This function is mapped across the sections and remembers the offset and
8971 size of each of the DWP debugging sections we are interested in. */
3019eac3 8972
80626a55
DE
8973static void
8974dwarf2_locate_dwp_sections (bfd *abfd, asection *sectp, void *dwp_file_ptr)
3019eac3 8975{
80626a55
DE
8976 struct dwp_file *dwp_file = dwp_file_ptr;
8977 const struct dwop_section_names *names = &dwop_section_names;
8978 unsigned int elf_section_nr = elf_section_data (sectp)->this_idx;
3019eac3 8979
80626a55
DE
8980 /* Record the ELF section number for later lookup: this is what the
8981 .debug_cu_index,.debug_tu_index tables use. */
8982 gdb_assert (elf_section_nr < dwp_file->num_sections);
8983 dwp_file->elf_sections[elf_section_nr] = sectp;
3019eac3 8984
80626a55
DE
8985 /* Look for specific sections that we need. */
8986 if (section_is_p (sectp->name, &names->str_dwo))
8987 {
8988 dwp_file->sections.str.asection = sectp;
8989 dwp_file->sections.str.size = bfd_get_section_size (sectp);
8990 }
8991 else if (section_is_p (sectp->name, &names->cu_index))
8992 {
8993 dwp_file->sections.cu_index.asection = sectp;
8994 dwp_file->sections.cu_index.size = bfd_get_section_size (sectp);
8995 }
8996 else if (section_is_p (sectp->name, &names->tu_index))
8997 {
8998 dwp_file->sections.tu_index.asection = sectp;
8999 dwp_file->sections.tu_index.size = bfd_get_section_size (sectp);
9000 }
9001}
3019eac3 9002
80626a55 9003/* Hash function for dwp_file loaded CUs/TUs. */
3019eac3 9004
80626a55
DE
9005static hashval_t
9006hash_dwp_loaded_cutus (const void *item)
9007{
9008 const struct dwo_unit *dwo_unit = item;
3019eac3 9009
80626a55
DE
9010 /* This drops the top 32 bits of the signature, but is ok for a hash. */
9011 return dwo_unit->signature;
3019eac3
DE
9012}
9013
80626a55 9014/* Equality function for dwp_file loaded CUs/TUs. */
3019eac3 9015
80626a55
DE
9016static int
9017eq_dwp_loaded_cutus (const void *a, const void *b)
3019eac3 9018{
80626a55
DE
9019 const struct dwo_unit *dua = a;
9020 const struct dwo_unit *dub = b;
3019eac3 9021
80626a55
DE
9022 return dua->signature == dub->signature;
9023}
3019eac3 9024
80626a55 9025/* Allocate a hash table for dwp_file loaded CUs/TUs. */
3019eac3 9026
80626a55
DE
9027static htab_t
9028allocate_dwp_loaded_cutus_table (struct objfile *objfile)
9029{
9030 return htab_create_alloc_ex (3,
9031 hash_dwp_loaded_cutus,
9032 eq_dwp_loaded_cutus,
9033 NULL,
9034 &objfile->objfile_obstack,
9035 hashtab_obstack_allocate,
9036 dummy_obstack_deallocate);
9037}
3019eac3 9038
80626a55
DE
9039/* Initialize the use of the DWP file for the current objfile.
9040 By convention the name of the DWP file is ${objfile}.dwp.
9041 The result is NULL if it can't be found. */
a766d390 9042
80626a55
DE
9043static struct dwp_file *
9044open_and_init_dwp_file (const char *comp_dir)
9045{
9046 struct objfile *objfile = dwarf2_per_objfile->objfile;
9047 struct dwp_file *dwp_file;
9048 char *dwp_name;
9049 bfd *dbfd;
9050 struct cleanup *cleanups;
9051
2792b94d 9052 dwp_name = xstrprintf ("%s.dwp", dwarf2_per_objfile->objfile->name);
80626a55
DE
9053 cleanups = make_cleanup (xfree, dwp_name);
9054
9055 dbfd = open_dwop_file (dwp_name, comp_dir, 1);
9056 if (dbfd == NULL)
9057 {
9058 if (dwarf2_read_debug)
9059 fprintf_unfiltered (gdb_stdlog, "DWP file not found: %s\n", dwp_name);
9060 do_cleanups (cleanups);
9061 return NULL;
3019eac3 9062 }
80626a55
DE
9063 dwp_file = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct dwp_file);
9064 dwp_file->name = obstack_copy0 (&objfile->objfile_obstack,
9065 dwp_name, strlen (dwp_name));
9066 dwp_file->dbfd = dbfd;
9067 do_cleanups (cleanups);
c906108c 9068
80626a55 9069 cleanups = make_cleanup (free_dwo_file_cleanup, dwp_file);
df8a16a1 9070
80626a55
DE
9071 /* +1: section 0 is unused */
9072 dwp_file->num_sections = bfd_count_sections (dbfd) + 1;
9073 dwp_file->elf_sections =
9074 OBSTACK_CALLOC (&objfile->objfile_obstack,
9075 dwp_file->num_sections, asection *);
9076
9077 bfd_map_over_sections (dbfd, dwarf2_locate_dwp_sections, dwp_file);
9078
9079 dwp_file->cus = create_dwp_hash_table (dwp_file, 0);
9080
9081 dwp_file->tus = create_dwp_hash_table (dwp_file, 1);
9082
9083 dwp_file->loaded_cutus = allocate_dwp_loaded_cutus_table (objfile);
9084
9085 discard_cleanups (cleanups);
9086
9087 if (dwarf2_read_debug)
9088 {
9089 fprintf_unfiltered (gdb_stdlog, "DWP file found: %s\n", dwp_file->name);
9090 fprintf_unfiltered (gdb_stdlog,
9091 " %u CUs, %u TUs\n",
9092 dwp_file->cus ? dwp_file->cus->nr_units : 0,
9093 dwp_file->tus ? dwp_file->tus->nr_units : 0);
9094 }
9095
9096 return dwp_file;
3019eac3 9097}
c906108c 9098
80626a55
DE
9099/* Subroutine of lookup_dwo_comp_unit, lookup_dwo_type_unit.
9100 Look up the CU/TU with signature SIGNATURE, either in DWO file DWO_NAME
9101 or in the DWP file for the objfile, referenced by THIS_UNIT.
3019eac3 9102 If non-NULL, comp_dir is the DW_AT_comp_dir attribute.
80626a55
DE
9103 IS_DEBUG_TYPES is non-zero if reading a TU, otherwise read a CU.
9104
9105 This is called, for example, when wanting to read a variable with a
9106 complex location. Therefore we don't want to do file i/o for every call.
9107 Therefore we don't want to look for a DWO file on every call.
9108 Therefore we first see if we've already seen SIGNATURE in a DWP file,
9109 then we check if we've already seen DWO_NAME, and only THEN do we check
9110 for a DWO file.
9111
1c658ad5 9112 The result is a pointer to the dwo_unit object or NULL if we didn't find it
80626a55 9113 (dwo_id mismatch or couldn't find the DWO/DWP file). */
debd256d 9114
3019eac3 9115static struct dwo_unit *
80626a55
DE
9116lookup_dwo_cutu (struct dwarf2_per_cu_data *this_unit,
9117 const char *dwo_name, const char *comp_dir,
9118 ULONGEST signature, int is_debug_types)
3019eac3
DE
9119{
9120 struct objfile *objfile = dwarf2_per_objfile->objfile;
80626a55
DE
9121 const char *kind = is_debug_types ? "TU" : "CU";
9122 void **dwo_file_slot;
3019eac3 9123 struct dwo_file *dwo_file;
80626a55 9124 struct dwp_file *dwp_file;
cb1df416 9125
80626a55 9126 /* Have we already read SIGNATURE from a DWP file? */
cf2c3c16 9127
80626a55
DE
9128 if (! dwarf2_per_objfile->dwp_checked)
9129 {
9130 dwarf2_per_objfile->dwp_file = open_and_init_dwp_file (comp_dir);
9131 dwarf2_per_objfile->dwp_checked = 1;
9132 }
9133 dwp_file = dwarf2_per_objfile->dwp_file;
3019eac3 9134
80626a55 9135 if (dwp_file != NULL)
cf2c3c16 9136 {
80626a55
DE
9137 const struct dwp_hash_table *dwp_htab =
9138 is_debug_types ? dwp_file->tus : dwp_file->cus;
9139
9140 if (dwp_htab != NULL)
9141 {
9142 struct dwo_unit *dwo_cutu =
9143 lookup_dwo_in_dwp (dwp_file, dwp_htab, signature, is_debug_types);
9144
9145 if (dwo_cutu != NULL)
9146 {
9147 if (dwarf2_read_debug)
9148 {
9149 fprintf_unfiltered (gdb_stdlog,
9150 "Virtual DWO %s %s found: @%s\n",
9151 kind, hex_string (signature),
9152 host_address_to_string (dwo_cutu));
9153 }
9154 return dwo_cutu;
9155 }
9156 }
9157 }
9158
9159 /* Have we already seen DWO_NAME? */
9160
9161 dwo_file_slot = lookup_dwo_file_slot (dwo_name);
9162 if (*dwo_file_slot == NULL)
9163 {
9164 /* Read in the file and build a table of the DWOs it contains. */
9165 *dwo_file_slot = open_and_init_dwo_file (dwo_name, comp_dir);
9166 }
9167 /* NOTE: This will be NULL if unable to open the file. */
9168 dwo_file = *dwo_file_slot;
9169
9170 if (dwo_file != NULL)
9171 {
9172 htab_t htab = is_debug_types ? dwo_file->tus : dwo_file->cus;
9173
9174 if (htab != NULL)
9175 {
9176 struct dwo_unit find_dwo_cutu, *dwo_cutu;
9a619af0 9177
80626a55
DE
9178 memset (&find_dwo_cutu, 0, sizeof (find_dwo_cutu));
9179 find_dwo_cutu.signature = signature;
9180 dwo_cutu = htab_find (htab, &find_dwo_cutu);
3019eac3 9181
80626a55
DE
9182 if (dwo_cutu != NULL)
9183 {
9184 if (dwarf2_read_debug)
9185 {
9186 fprintf_unfiltered (gdb_stdlog, "DWO %s %s(%s) found: @%s\n",
9187 kind, dwo_name, hex_string (signature),
9188 host_address_to_string (dwo_cutu));
9189 }
9190 return dwo_cutu;
9191 }
9192 }
2e276125 9193 }
9cdd5dbd 9194
80626a55
DE
9195 /* We didn't find it. This could mean a dwo_id mismatch, or
9196 someone deleted the DWO/DWP file, or the search path isn't set up
9197 correctly to find the file. */
9198
9199 if (dwarf2_read_debug)
9200 {
9201 fprintf_unfiltered (gdb_stdlog, "DWO %s %s(%s) not found\n",
9202 kind, dwo_name, hex_string (signature));
9203 }
3019eac3
DE
9204
9205 complaint (&symfile_complaints,
80626a55 9206 _("Could not find DWO CU referenced by CU at offset 0x%x"
3019eac3 9207 " [in module %s]"),
80626a55 9208 this_unit->offset.sect_off, objfile->name);
3019eac3 9209 return NULL;
5fb290d7
DJ
9210}
9211
80626a55
DE
9212/* Lookup the DWO CU DWO_NAME/SIGNATURE referenced from THIS_CU.
9213 See lookup_dwo_cutu_unit for details. */
9214
9215static struct dwo_unit *
9216lookup_dwo_comp_unit (struct dwarf2_per_cu_data *this_cu,
9217 const char *dwo_name, const char *comp_dir,
9218 ULONGEST signature)
9219{
9220 return lookup_dwo_cutu (this_cu, dwo_name, comp_dir, signature, 0);
9221}
9222
9223/* Lookup the DWO TU DWO_NAME/SIGNATURE referenced from THIS_TU.
9224 See lookup_dwo_cutu_unit for details. */
9225
9226static struct dwo_unit *
9227lookup_dwo_type_unit (struct signatured_type *this_tu,
9228 const char *dwo_name, const char *comp_dir)
9229{
9230 return lookup_dwo_cutu (&this_tu->per_cu, dwo_name, comp_dir, this_tu->signature, 1);
9231}
9232
3019eac3
DE
9233/* Free all resources associated with DWO_FILE.
9234 Close the DWO file and munmap the sections.
9235 All memory should be on the objfile obstack. */
348e048f
DE
9236
9237static void
3019eac3 9238free_dwo_file (struct dwo_file *dwo_file, struct objfile *objfile)
348e048f 9239{
3019eac3
DE
9240 int ix;
9241 struct dwarf2_section_info *section;
348e048f 9242
80626a55 9243 gdb_bfd_unref (dwo_file->dbfd);
348e048f 9244
3019eac3
DE
9245 VEC_free (dwarf2_section_info_def, dwo_file->sections.types);
9246}
348e048f 9247
3019eac3 9248/* Wrapper for free_dwo_file for use in cleanups. */
348e048f 9249
3019eac3
DE
9250static void
9251free_dwo_file_cleanup (void *arg)
9252{
9253 struct dwo_file *dwo_file = (struct dwo_file *) arg;
9254 struct objfile *objfile = dwarf2_per_objfile->objfile;
348e048f 9255
3019eac3
DE
9256 free_dwo_file (dwo_file, objfile);
9257}
348e048f 9258
3019eac3 9259/* Traversal function for free_dwo_files. */
2ab95328 9260
3019eac3
DE
9261static int
9262free_dwo_file_from_slot (void **slot, void *info)
9263{
9264 struct dwo_file *dwo_file = (struct dwo_file *) *slot;
9265 struct objfile *objfile = (struct objfile *) info;
348e048f 9266
3019eac3 9267 free_dwo_file (dwo_file, objfile);
348e048f 9268
3019eac3
DE
9269 return 1;
9270}
348e048f 9271
3019eac3 9272/* Free all resources associated with DWO_FILES. */
348e048f 9273
3019eac3
DE
9274static void
9275free_dwo_files (htab_t dwo_files, struct objfile *objfile)
9276{
9277 htab_traverse_noresize (dwo_files, free_dwo_file_from_slot, objfile);
348e048f 9278}
3019eac3
DE
9279\f
9280/* Read in various DIEs. */
348e048f 9281
d389af10
JK
9282/* qsort helper for inherit_abstract_dies. */
9283
9284static int
9285unsigned_int_compar (const void *ap, const void *bp)
9286{
9287 unsigned int a = *(unsigned int *) ap;
9288 unsigned int b = *(unsigned int *) bp;
9289
9290 return (a > b) - (b > a);
9291}
9292
9293/* DW_AT_abstract_origin inherits whole DIEs (not just their attributes).
3e43a32a
MS
9294 Inherit only the children of the DW_AT_abstract_origin DIE not being
9295 already referenced by DW_AT_abstract_origin from the children of the
9296 current DIE. */
d389af10
JK
9297
9298static void
9299inherit_abstract_dies (struct die_info *die, struct dwarf2_cu *cu)
9300{
9301 struct die_info *child_die;
9302 unsigned die_children_count;
9303 /* CU offsets which were referenced by children of the current DIE. */
b64f50a1
JK
9304 sect_offset *offsets;
9305 sect_offset *offsets_end, *offsetp;
d389af10
JK
9306 /* Parent of DIE - referenced by DW_AT_abstract_origin. */
9307 struct die_info *origin_die;
9308 /* Iterator of the ORIGIN_DIE children. */
9309 struct die_info *origin_child_die;
9310 struct cleanup *cleanups;
9311 struct attribute *attr;
cd02d79d
PA
9312 struct dwarf2_cu *origin_cu;
9313 struct pending **origin_previous_list_in_scope;
d389af10
JK
9314
9315 attr = dwarf2_attr (die, DW_AT_abstract_origin, cu);
9316 if (!attr)
9317 return;
9318
cd02d79d
PA
9319 /* Note that following die references may follow to a die in a
9320 different cu. */
9321
9322 origin_cu = cu;
9323 origin_die = follow_die_ref (die, attr, &origin_cu);
9324
9325 /* We're inheriting ORIGIN's children into the scope we'd put DIE's
9326 symbols in. */
9327 origin_previous_list_in_scope = origin_cu->list_in_scope;
9328 origin_cu->list_in_scope = cu->list_in_scope;
9329
edb3359d
DJ
9330 if (die->tag != origin_die->tag
9331 && !(die->tag == DW_TAG_inlined_subroutine
9332 && origin_die->tag == DW_TAG_subprogram))
d389af10
JK
9333 complaint (&symfile_complaints,
9334 _("DIE 0x%x and its abstract origin 0x%x have different tags"),
b64f50a1 9335 die->offset.sect_off, origin_die->offset.sect_off);
d389af10
JK
9336
9337 child_die = die->child;
9338 die_children_count = 0;
9339 while (child_die && child_die->tag)
9340 {
9341 child_die = sibling_die (child_die);
9342 die_children_count++;
9343 }
9344 offsets = xmalloc (sizeof (*offsets) * die_children_count);
9345 cleanups = make_cleanup (xfree, offsets);
9346
9347 offsets_end = offsets;
9348 child_die = die->child;
9349 while (child_die && child_die->tag)
9350 {
c38f313d
DJ
9351 /* For each CHILD_DIE, find the corresponding child of
9352 ORIGIN_DIE. If there is more than one layer of
9353 DW_AT_abstract_origin, follow them all; there shouldn't be,
9354 but GCC versions at least through 4.4 generate this (GCC PR
9355 40573). */
9356 struct die_info *child_origin_die = child_die;
cd02d79d 9357 struct dwarf2_cu *child_origin_cu = cu;
9a619af0 9358
c38f313d
DJ
9359 while (1)
9360 {
cd02d79d
PA
9361 attr = dwarf2_attr (child_origin_die, DW_AT_abstract_origin,
9362 child_origin_cu);
c38f313d
DJ
9363 if (attr == NULL)
9364 break;
cd02d79d
PA
9365 child_origin_die = follow_die_ref (child_origin_die, attr,
9366 &child_origin_cu);
c38f313d
DJ
9367 }
9368
d389af10
JK
9369 /* According to DWARF3 3.3.8.2 #3 new entries without their abstract
9370 counterpart may exist. */
c38f313d 9371 if (child_origin_die != child_die)
d389af10 9372 {
edb3359d
DJ
9373 if (child_die->tag != child_origin_die->tag
9374 && !(child_die->tag == DW_TAG_inlined_subroutine
9375 && child_origin_die->tag == DW_TAG_subprogram))
d389af10
JK
9376 complaint (&symfile_complaints,
9377 _("Child DIE 0x%x and its abstract origin 0x%x have "
b64f50a1
JK
9378 "different tags"), child_die->offset.sect_off,
9379 child_origin_die->offset.sect_off);
c38f313d
DJ
9380 if (child_origin_die->parent != origin_die)
9381 complaint (&symfile_complaints,
9382 _("Child DIE 0x%x and its abstract origin 0x%x have "
b64f50a1
JK
9383 "different parents"), child_die->offset.sect_off,
9384 child_origin_die->offset.sect_off);
c38f313d
DJ
9385 else
9386 *offsets_end++ = child_origin_die->offset;
d389af10
JK
9387 }
9388 child_die = sibling_die (child_die);
9389 }
9390 qsort (offsets, offsets_end - offsets, sizeof (*offsets),
9391 unsigned_int_compar);
9392 for (offsetp = offsets + 1; offsetp < offsets_end; offsetp++)
b64f50a1 9393 if (offsetp[-1].sect_off == offsetp->sect_off)
3e43a32a
MS
9394 complaint (&symfile_complaints,
9395 _("Multiple children of DIE 0x%x refer "
9396 "to DIE 0x%x as their abstract origin"),
b64f50a1 9397 die->offset.sect_off, offsetp->sect_off);
d389af10
JK
9398
9399 offsetp = offsets;
9400 origin_child_die = origin_die->child;
9401 while (origin_child_die && origin_child_die->tag)
9402 {
9403 /* Is ORIGIN_CHILD_DIE referenced by any of the DIE children? */
b64f50a1
JK
9404 while (offsetp < offsets_end
9405 && offsetp->sect_off < origin_child_die->offset.sect_off)
d389af10 9406 offsetp++;
b64f50a1
JK
9407 if (offsetp >= offsets_end
9408 || offsetp->sect_off > origin_child_die->offset.sect_off)
d389af10
JK
9409 {
9410 /* Found that ORIGIN_CHILD_DIE is really not referenced. */
cd02d79d 9411 process_die (origin_child_die, origin_cu);
d389af10
JK
9412 }
9413 origin_child_die = sibling_die (origin_child_die);
9414 }
cd02d79d 9415 origin_cu->list_in_scope = origin_previous_list_in_scope;
d389af10
JK
9416
9417 do_cleanups (cleanups);
9418}
9419
c906108c 9420static void
e7c27a73 9421read_func_scope (struct die_info *die, struct dwarf2_cu *cu)
c906108c 9422{
e7c27a73 9423 struct objfile *objfile = cu->objfile;
52f0bd74 9424 struct context_stack *new;
c906108c
SS
9425 CORE_ADDR lowpc;
9426 CORE_ADDR highpc;
9427 struct die_info *child_die;
edb3359d 9428 struct attribute *attr, *call_line, *call_file;
15d034d0 9429 const char *name;
e142c38c 9430 CORE_ADDR baseaddr;
801e3a5b 9431 struct block *block;
edb3359d 9432 int inlined_func = (die->tag == DW_TAG_inlined_subroutine);
34eaf542
TT
9433 VEC (symbolp) *template_args = NULL;
9434 struct template_symbol *templ_func = NULL;
edb3359d
DJ
9435
9436 if (inlined_func)
9437 {
9438 /* If we do not have call site information, we can't show the
9439 caller of this inlined function. That's too confusing, so
9440 only use the scope for local variables. */
9441 call_line = dwarf2_attr (die, DW_AT_call_line, cu);
9442 call_file = dwarf2_attr (die, DW_AT_call_file, cu);
9443 if (call_line == NULL || call_file == NULL)
9444 {
9445 read_lexical_block_scope (die, cu);
9446 return;
9447 }
9448 }
c906108c 9449
e142c38c
DJ
9450 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
9451
94af9270 9452 name = dwarf2_name (die, cu);
c906108c 9453
e8d05480
JB
9454 /* Ignore functions with missing or empty names. These are actually
9455 illegal according to the DWARF standard. */
9456 if (name == NULL)
9457 {
9458 complaint (&symfile_complaints,
b64f50a1
JK
9459 _("missing name for subprogram DIE at %d"),
9460 die->offset.sect_off);
e8d05480
JB
9461 return;
9462 }
9463
9464 /* Ignore functions with missing or invalid low and high pc attributes. */
9465 if (!dwarf2_get_pc_bounds (die, &lowpc, &highpc, cu, NULL))
9466 {
ae4d0c03
PM
9467 attr = dwarf2_attr (die, DW_AT_external, cu);
9468 if (!attr || !DW_UNSND (attr))
9469 complaint (&symfile_complaints,
3e43a32a
MS
9470 _("cannot get low and high bounds "
9471 "for subprogram DIE at %d"),
b64f50a1 9472 die->offset.sect_off);
e8d05480
JB
9473 return;
9474 }
c906108c
SS
9475
9476 lowpc += baseaddr;
9477 highpc += baseaddr;
9478
34eaf542
TT
9479 /* If we have any template arguments, then we must allocate a
9480 different sort of symbol. */
9481 for (child_die = die->child; child_die; child_die = sibling_die (child_die))
9482 {
9483 if (child_die->tag == DW_TAG_template_type_param
9484 || child_die->tag == DW_TAG_template_value_param)
9485 {
9486 templ_func = OBSTACK_ZALLOC (&objfile->objfile_obstack,
9487 struct template_symbol);
9488 templ_func->base.is_cplus_template_function = 1;
9489 break;
9490 }
9491 }
9492
c906108c 9493 new = push_context (0, lowpc);
34eaf542
TT
9494 new->name = new_symbol_full (die, read_type_die (die, cu), cu,
9495 (struct symbol *) templ_func);
4c2df51b 9496
4cecd739
DJ
9497 /* If there is a location expression for DW_AT_frame_base, record
9498 it. */
e142c38c 9499 attr = dwarf2_attr (die, DW_AT_frame_base, cu);
4c2df51b 9500 if (attr)
c034e007
AC
9501 /* FIXME: cagney/2004-01-26: The DW_AT_frame_base's location
9502 expression is being recorded directly in the function's symbol
9503 and not in a separate frame-base object. I guess this hack is
9504 to avoid adding some sort of frame-base adjunct/annex to the
9505 function's symbol :-(. The problem with doing this is that it
9506 results in a function symbol with a location expression that
9507 has nothing to do with the location of the function, ouch! The
9508 relationship should be: a function's symbol has-a frame base; a
9509 frame-base has-a location expression. */
e7c27a73 9510 dwarf2_symbol_mark_computed (attr, new->name, cu);
4c2df51b 9511
e142c38c 9512 cu->list_in_scope = &local_symbols;
c906108c 9513
639d11d3 9514 if (die->child != NULL)
c906108c 9515 {
639d11d3 9516 child_die = die->child;
c906108c
SS
9517 while (child_die && child_die->tag)
9518 {
34eaf542
TT
9519 if (child_die->tag == DW_TAG_template_type_param
9520 || child_die->tag == DW_TAG_template_value_param)
9521 {
9522 struct symbol *arg = new_symbol (child_die, NULL, cu);
9523
f1078f66
DJ
9524 if (arg != NULL)
9525 VEC_safe_push (symbolp, template_args, arg);
34eaf542
TT
9526 }
9527 else
9528 process_die (child_die, cu);
c906108c
SS
9529 child_die = sibling_die (child_die);
9530 }
9531 }
9532
d389af10
JK
9533 inherit_abstract_dies (die, cu);
9534
4a811a97
UW
9535 /* If we have a DW_AT_specification, we might need to import using
9536 directives from the context of the specification DIE. See the
9537 comment in determine_prefix. */
9538 if (cu->language == language_cplus
9539 && dwarf2_attr (die, DW_AT_specification, cu))
9540 {
9541 struct dwarf2_cu *spec_cu = cu;
9542 struct die_info *spec_die = die_specification (die, &spec_cu);
9543
9544 while (spec_die)
9545 {
9546 child_die = spec_die->child;
9547 while (child_die && child_die->tag)
9548 {
9549 if (child_die->tag == DW_TAG_imported_module)
9550 process_die (child_die, spec_cu);
9551 child_die = sibling_die (child_die);
9552 }
9553
9554 /* In some cases, GCC generates specification DIEs that
9555 themselves contain DW_AT_specification attributes. */
9556 spec_die = die_specification (spec_die, &spec_cu);
9557 }
9558 }
9559
c906108c
SS
9560 new = pop_context ();
9561 /* Make a block for the local symbols within. */
801e3a5b
JB
9562 block = finish_block (new->name, &local_symbols, new->old_blocks,
9563 lowpc, highpc, objfile);
9564
df8a16a1 9565 /* For C++, set the block's scope. */
195a3f6c 9566 if ((cu->language == language_cplus || cu->language == language_fortran)
4d4ec4e5 9567 && cu->processing_has_namespace_info)
195a3f6c
TT
9568 block_set_scope (block, determine_prefix (die, cu),
9569 &objfile->objfile_obstack);
df8a16a1 9570
801e3a5b
JB
9571 /* If we have address ranges, record them. */
9572 dwarf2_record_block_ranges (die, block, baseaddr, cu);
6e70227d 9573
34eaf542
TT
9574 /* Attach template arguments to function. */
9575 if (! VEC_empty (symbolp, template_args))
9576 {
9577 gdb_assert (templ_func != NULL);
9578
9579 templ_func->n_template_arguments = VEC_length (symbolp, template_args);
9580 templ_func->template_arguments
9581 = obstack_alloc (&objfile->objfile_obstack,
9582 (templ_func->n_template_arguments
9583 * sizeof (struct symbol *)));
9584 memcpy (templ_func->template_arguments,
9585 VEC_address (symbolp, template_args),
9586 (templ_func->n_template_arguments * sizeof (struct symbol *)));
9587 VEC_free (symbolp, template_args);
9588 }
9589
208d8187
JB
9590 /* In C++, we can have functions nested inside functions (e.g., when
9591 a function declares a class that has methods). This means that
9592 when we finish processing a function scope, we may need to go
9593 back to building a containing block's symbol lists. */
9594 local_symbols = new->locals;
27aa8d6a 9595 using_directives = new->using_directives;
208d8187 9596
921e78cf
JB
9597 /* If we've finished processing a top-level function, subsequent
9598 symbols go in the file symbol list. */
9599 if (outermost_context_p ())
e142c38c 9600 cu->list_in_scope = &file_symbols;
c906108c
SS
9601}
9602
9603/* Process all the DIES contained within a lexical block scope. Start
9604 a new scope, process the dies, and then close the scope. */
9605
9606static void
e7c27a73 9607read_lexical_block_scope (struct die_info *die, struct dwarf2_cu *cu)
c906108c 9608{
e7c27a73 9609 struct objfile *objfile = cu->objfile;
52f0bd74 9610 struct context_stack *new;
c906108c
SS
9611 CORE_ADDR lowpc, highpc;
9612 struct die_info *child_die;
e142c38c
DJ
9613 CORE_ADDR baseaddr;
9614
9615 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
c906108c
SS
9616
9617 /* Ignore blocks with missing or invalid low and high pc attributes. */
af34e669
DJ
9618 /* ??? Perhaps consider discontiguous blocks defined by DW_AT_ranges
9619 as multiple lexical blocks? Handling children in a sane way would
6e70227d 9620 be nasty. Might be easier to properly extend generic blocks to
af34e669 9621 describe ranges. */
d85a05f0 9622 if (!dwarf2_get_pc_bounds (die, &lowpc, &highpc, cu, NULL))
c906108c
SS
9623 return;
9624 lowpc += baseaddr;
9625 highpc += baseaddr;
9626
9627 push_context (0, lowpc);
639d11d3 9628 if (die->child != NULL)
c906108c 9629 {
639d11d3 9630 child_die = die->child;
c906108c
SS
9631 while (child_die && child_die->tag)
9632 {
e7c27a73 9633 process_die (child_die, cu);
c906108c
SS
9634 child_die = sibling_die (child_die);
9635 }
9636 }
9637 new = pop_context ();
9638
8540c487 9639 if (local_symbols != NULL || using_directives != NULL)
c906108c 9640 {
801e3a5b
JB
9641 struct block *block
9642 = finish_block (0, &local_symbols, new->old_blocks, new->start_addr,
9643 highpc, objfile);
9644
9645 /* Note that recording ranges after traversing children, as we
9646 do here, means that recording a parent's ranges entails
9647 walking across all its children's ranges as they appear in
9648 the address map, which is quadratic behavior.
9649
9650 It would be nicer to record the parent's ranges before
9651 traversing its children, simply overriding whatever you find
9652 there. But since we don't even decide whether to create a
9653 block until after we've traversed its children, that's hard
9654 to do. */
9655 dwarf2_record_block_ranges (die, block, baseaddr, cu);
c906108c
SS
9656 }
9657 local_symbols = new->locals;
27aa8d6a 9658 using_directives = new->using_directives;
c906108c
SS
9659}
9660
96408a79
SA
9661/* Read in DW_TAG_GNU_call_site and insert it to CU->call_site_htab. */
9662
9663static void
9664read_call_site_scope (struct die_info *die, struct dwarf2_cu *cu)
9665{
9666 struct objfile *objfile = cu->objfile;
9667 struct gdbarch *gdbarch = get_objfile_arch (objfile);
9668 CORE_ADDR pc, baseaddr;
9669 struct attribute *attr;
9670 struct call_site *call_site, call_site_local;
9671 void **slot;
9672 int nparams;
9673 struct die_info *child_die;
9674
9675 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
9676
9677 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
9678 if (!attr)
9679 {
9680 complaint (&symfile_complaints,
9681 _("missing DW_AT_low_pc for DW_TAG_GNU_call_site "
9682 "DIE 0x%x [in module %s]"),
b64f50a1 9683 die->offset.sect_off, objfile->name);
96408a79
SA
9684 return;
9685 }
9686 pc = DW_ADDR (attr) + baseaddr;
9687
9688 if (cu->call_site_htab == NULL)
9689 cu->call_site_htab = htab_create_alloc_ex (16, core_addr_hash, core_addr_eq,
9690 NULL, &objfile->objfile_obstack,
9691 hashtab_obstack_allocate, NULL);
9692 call_site_local.pc = pc;
9693 slot = htab_find_slot (cu->call_site_htab, &call_site_local, INSERT);
9694 if (*slot != NULL)
9695 {
9696 complaint (&symfile_complaints,
9697 _("Duplicate PC %s for DW_TAG_GNU_call_site "
9698 "DIE 0x%x [in module %s]"),
b64f50a1 9699 paddress (gdbarch, pc), die->offset.sect_off, objfile->name);
96408a79
SA
9700 return;
9701 }
9702
9703 /* Count parameters at the caller. */
9704
9705 nparams = 0;
9706 for (child_die = die->child; child_die && child_die->tag;
9707 child_die = sibling_die (child_die))
9708 {
9709 if (child_die->tag != DW_TAG_GNU_call_site_parameter)
9710 {
9711 complaint (&symfile_complaints,
9712 _("Tag %d is not DW_TAG_GNU_call_site_parameter in "
9713 "DW_TAG_GNU_call_site child DIE 0x%x [in module %s]"),
b64f50a1 9714 child_die->tag, child_die->offset.sect_off, objfile->name);
96408a79
SA
9715 continue;
9716 }
9717
9718 nparams++;
9719 }
9720
9721 call_site = obstack_alloc (&objfile->objfile_obstack,
9722 (sizeof (*call_site)
9723 + (sizeof (*call_site->parameter)
9724 * (nparams - 1))));
9725 *slot = call_site;
9726 memset (call_site, 0, sizeof (*call_site) - sizeof (*call_site->parameter));
9727 call_site->pc = pc;
9728
9729 if (dwarf2_flag_true_p (die, DW_AT_GNU_tail_call, cu))
9730 {
9731 struct die_info *func_die;
9732
9733 /* Skip also over DW_TAG_inlined_subroutine. */
9734 for (func_die = die->parent;
9735 func_die && func_die->tag != DW_TAG_subprogram
9736 && func_die->tag != DW_TAG_subroutine_type;
9737 func_die = func_die->parent);
9738
9739 /* DW_AT_GNU_all_call_sites is a superset
9740 of DW_AT_GNU_all_tail_call_sites. */
9741 if (func_die
9742 && !dwarf2_flag_true_p (func_die, DW_AT_GNU_all_call_sites, cu)
9743 && !dwarf2_flag_true_p (func_die, DW_AT_GNU_all_tail_call_sites, cu))
9744 {
9745 /* TYPE_TAIL_CALL_LIST is not interesting in functions where it is
9746 not complete. But keep CALL_SITE for look ups via call_site_htab,
9747 both the initial caller containing the real return address PC and
9748 the final callee containing the current PC of a chain of tail
9749 calls do not need to have the tail call list complete. But any
9750 function candidate for a virtual tail call frame searched via
9751 TYPE_TAIL_CALL_LIST must have the tail call list complete to be
9752 determined unambiguously. */
9753 }
9754 else
9755 {
9756 struct type *func_type = NULL;
9757
9758 if (func_die)
9759 func_type = get_die_type (func_die, cu);
9760 if (func_type != NULL)
9761 {
9762 gdb_assert (TYPE_CODE (func_type) == TYPE_CODE_FUNC);
9763
9764 /* Enlist this call site to the function. */
9765 call_site->tail_call_next = TYPE_TAIL_CALL_LIST (func_type);
9766 TYPE_TAIL_CALL_LIST (func_type) = call_site;
9767 }
9768 else
9769 complaint (&symfile_complaints,
9770 _("Cannot find function owning DW_TAG_GNU_call_site "
9771 "DIE 0x%x [in module %s]"),
b64f50a1 9772 die->offset.sect_off, objfile->name);
96408a79
SA
9773 }
9774 }
9775
9776 attr = dwarf2_attr (die, DW_AT_GNU_call_site_target, cu);
9777 if (attr == NULL)
9778 attr = dwarf2_attr (die, DW_AT_abstract_origin, cu);
9779 SET_FIELD_DWARF_BLOCK (call_site->target, NULL);
9780 if (!attr || (attr_form_is_block (attr) && DW_BLOCK (attr)->size == 0))
9781 /* Keep NULL DWARF_BLOCK. */;
9782 else if (attr_form_is_block (attr))
9783 {
9784 struct dwarf2_locexpr_baton *dlbaton;
9785
9786 dlbaton = obstack_alloc (&objfile->objfile_obstack, sizeof (*dlbaton));
9787 dlbaton->data = DW_BLOCK (attr)->data;
9788 dlbaton->size = DW_BLOCK (attr)->size;
9789 dlbaton->per_cu = cu->per_cu;
9790
9791 SET_FIELD_DWARF_BLOCK (call_site->target, dlbaton);
9792 }
9793 else if (is_ref_attr (attr))
9794 {
96408a79
SA
9795 struct dwarf2_cu *target_cu = cu;
9796 struct die_info *target_die;
9797
9798 target_die = follow_die_ref_or_sig (die, attr, &target_cu);
9799 gdb_assert (target_cu->objfile == objfile);
9800 if (die_is_declaration (target_die, target_cu))
9801 {
9802 const char *target_physname;
9803
9804 target_physname = dwarf2_physname (NULL, target_die, target_cu);
9805 if (target_physname == NULL)
9806 complaint (&symfile_complaints,
9807 _("DW_AT_GNU_call_site_target target DIE has invalid "
9808 "physname, for referencing DIE 0x%x [in module %s]"),
b64f50a1 9809 die->offset.sect_off, objfile->name);
96408a79
SA
9810 else
9811 SET_FIELD_PHYSNAME (call_site->target, (char *) target_physname);
9812 }
9813 else
9814 {
9815 CORE_ADDR lowpc;
9816
9817 /* DW_AT_entry_pc should be preferred. */
9818 if (!dwarf2_get_pc_bounds (target_die, &lowpc, NULL, target_cu, NULL))
9819 complaint (&symfile_complaints,
9820 _("DW_AT_GNU_call_site_target target DIE has invalid "
9821 "low pc, for referencing DIE 0x%x [in module %s]"),
b64f50a1 9822 die->offset.sect_off, objfile->name);
96408a79
SA
9823 else
9824 SET_FIELD_PHYSADDR (call_site->target, lowpc + baseaddr);
9825 }
9826 }
9827 else
9828 complaint (&symfile_complaints,
9829 _("DW_TAG_GNU_call_site DW_AT_GNU_call_site_target is neither "
9830 "block nor reference, for DIE 0x%x [in module %s]"),
b64f50a1 9831 die->offset.sect_off, objfile->name);
96408a79
SA
9832
9833 call_site->per_cu = cu->per_cu;
9834
9835 for (child_die = die->child;
9836 child_die && child_die->tag;
9837 child_die = sibling_die (child_die))
9838 {
96408a79 9839 struct call_site_parameter *parameter;
1788b2d3 9840 struct attribute *loc, *origin;
96408a79
SA
9841
9842 if (child_die->tag != DW_TAG_GNU_call_site_parameter)
9843 {
9844 /* Already printed the complaint above. */
9845 continue;
9846 }
9847
9848 gdb_assert (call_site->parameter_count < nparams);
9849 parameter = &call_site->parameter[call_site->parameter_count];
9850
1788b2d3
JK
9851 /* DW_AT_location specifies the register number or DW_AT_abstract_origin
9852 specifies DW_TAG_formal_parameter. Value of the data assumed for the
9853 register is contained in DW_AT_GNU_call_site_value. */
96408a79 9854
24c5c679 9855 loc = dwarf2_attr (child_die, DW_AT_location, cu);
1788b2d3
JK
9856 origin = dwarf2_attr (child_die, DW_AT_abstract_origin, cu);
9857 if (loc == NULL && origin != NULL && is_ref_attr (origin))
9858 {
9859 sect_offset offset;
9860
9861 parameter->kind = CALL_SITE_PARAMETER_PARAM_OFFSET;
9862 offset = dwarf2_get_ref_die_offset (origin);
d76b7dbc
JK
9863 if (!offset_in_cu_p (&cu->header, offset))
9864 {
9865 /* As DW_OP_GNU_parameter_ref uses CU-relative offset this
9866 binding can be done only inside one CU. Such referenced DIE
9867 therefore cannot be even moved to DW_TAG_partial_unit. */
9868 complaint (&symfile_complaints,
9869 _("DW_AT_abstract_origin offset is not in CU for "
9870 "DW_TAG_GNU_call_site child DIE 0x%x "
9871 "[in module %s]"),
9872 child_die->offset.sect_off, objfile->name);
9873 continue;
9874 }
1788b2d3
JK
9875 parameter->u.param_offset.cu_off = (offset.sect_off
9876 - cu->header.offset.sect_off);
9877 }
9878 else if (loc == NULL || origin != NULL || !attr_form_is_block (loc))
96408a79
SA
9879 {
9880 complaint (&symfile_complaints,
9881 _("No DW_FORM_block* DW_AT_location for "
9882 "DW_TAG_GNU_call_site child DIE 0x%x [in module %s]"),
b64f50a1 9883 child_die->offset.sect_off, objfile->name);
96408a79
SA
9884 continue;
9885 }
24c5c679 9886 else
96408a79 9887 {
24c5c679
JK
9888 parameter->u.dwarf_reg = dwarf_block_to_dwarf_reg
9889 (DW_BLOCK (loc)->data, &DW_BLOCK (loc)->data[DW_BLOCK (loc)->size]);
9890 if (parameter->u.dwarf_reg != -1)
9891 parameter->kind = CALL_SITE_PARAMETER_DWARF_REG;
9892 else if (dwarf_block_to_sp_offset (gdbarch, DW_BLOCK (loc)->data,
9893 &DW_BLOCK (loc)->data[DW_BLOCK (loc)->size],
9894 &parameter->u.fb_offset))
9895 parameter->kind = CALL_SITE_PARAMETER_FB_OFFSET;
9896 else
9897 {
9898 complaint (&symfile_complaints,
9899 _("Only single DW_OP_reg or DW_OP_fbreg is supported "
9900 "for DW_FORM_block* DW_AT_location is supported for "
9901 "DW_TAG_GNU_call_site child DIE 0x%x "
9902 "[in module %s]"),
9903 child_die->offset.sect_off, objfile->name);
9904 continue;
9905 }
96408a79
SA
9906 }
9907
9908 attr = dwarf2_attr (child_die, DW_AT_GNU_call_site_value, cu);
9909 if (!attr_form_is_block (attr))
9910 {
9911 complaint (&symfile_complaints,
9912 _("No DW_FORM_block* DW_AT_GNU_call_site_value for "
9913 "DW_TAG_GNU_call_site child DIE 0x%x [in module %s]"),
b64f50a1 9914 child_die->offset.sect_off, objfile->name);
96408a79
SA
9915 continue;
9916 }
9917 parameter->value = DW_BLOCK (attr)->data;
9918 parameter->value_size = DW_BLOCK (attr)->size;
9919
9920 /* Parameters are not pre-cleared by memset above. */
9921 parameter->data_value = NULL;
9922 parameter->data_value_size = 0;
9923 call_site->parameter_count++;
9924
9925 attr = dwarf2_attr (child_die, DW_AT_GNU_call_site_data_value, cu);
9926 if (attr)
9927 {
9928 if (!attr_form_is_block (attr))
9929 complaint (&symfile_complaints,
9930 _("No DW_FORM_block* DW_AT_GNU_call_site_data_value for "
9931 "DW_TAG_GNU_call_site child DIE 0x%x [in module %s]"),
b64f50a1 9932 child_die->offset.sect_off, objfile->name);
96408a79
SA
9933 else
9934 {
9935 parameter->data_value = DW_BLOCK (attr)->data;
9936 parameter->data_value_size = DW_BLOCK (attr)->size;
9937 }
9938 }
9939 }
9940}
9941
43039443 9942/* Get low and high pc attributes from DW_AT_ranges attribute value OFFSET.
ff013f42
JK
9943 Return 1 if the attributes are present and valid, otherwise, return 0.
9944 If RANGES_PST is not NULL we should setup `objfile->psymtabs_addrmap'. */
43039443
JK
9945
9946static int
9947dwarf2_ranges_read (unsigned offset, CORE_ADDR *low_return,
ff013f42
JK
9948 CORE_ADDR *high_return, struct dwarf2_cu *cu,
9949 struct partial_symtab *ranges_pst)
43039443
JK
9950{
9951 struct objfile *objfile = cu->objfile;
9952 struct comp_unit_head *cu_header = &cu->header;
9953 bfd *obfd = objfile->obfd;
9954 unsigned int addr_size = cu_header->addr_size;
9955 CORE_ADDR mask = ~(~(CORE_ADDR)1 << (addr_size * 8 - 1));
9956 /* Base address selection entry. */
9957 CORE_ADDR base;
9958 int found_base;
9959 unsigned int dummy;
9960 gdb_byte *buffer;
9961 CORE_ADDR marker;
9962 int low_set;
9963 CORE_ADDR low = 0;
9964 CORE_ADDR high = 0;
ff013f42 9965 CORE_ADDR baseaddr;
43039443 9966
d00adf39
DE
9967 found_base = cu->base_known;
9968 base = cu->base_address;
43039443 9969
be391dca 9970 dwarf2_read_section (objfile, &dwarf2_per_objfile->ranges);
dce234bc 9971 if (offset >= dwarf2_per_objfile->ranges.size)
43039443
JK
9972 {
9973 complaint (&symfile_complaints,
9974 _("Offset %d out of bounds for DW_AT_ranges attribute"),
9975 offset);
9976 return 0;
9977 }
dce234bc 9978 buffer = dwarf2_per_objfile->ranges.buffer + offset;
43039443
JK
9979
9980 /* Read in the largest possible address. */
9981 marker = read_address (obfd, buffer, cu, &dummy);
9982 if ((marker & mask) == mask)
9983 {
9984 /* If we found the largest possible address, then
9985 read the base address. */
9986 base = read_address (obfd, buffer + addr_size, cu, &dummy);
9987 buffer += 2 * addr_size;
9988 offset += 2 * addr_size;
9989 found_base = 1;
9990 }
9991
9992 low_set = 0;
9993
e7030f15 9994 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
ff013f42 9995
43039443
JK
9996 while (1)
9997 {
9998 CORE_ADDR range_beginning, range_end;
9999
10000 range_beginning = read_address (obfd, buffer, cu, &dummy);
10001 buffer += addr_size;
10002 range_end = read_address (obfd, buffer, cu, &dummy);
10003 buffer += addr_size;
10004 offset += 2 * addr_size;
10005
10006 /* An end of list marker is a pair of zero addresses. */
10007 if (range_beginning == 0 && range_end == 0)
10008 /* Found the end of list entry. */
10009 break;
10010
10011 /* Each base address selection entry is a pair of 2 values.
10012 The first is the largest possible address, the second is
10013 the base address. Check for a base address here. */
10014 if ((range_beginning & mask) == mask)
10015 {
10016 /* If we found the largest possible address, then
10017 read the base address. */
10018 base = read_address (obfd, buffer + addr_size, cu, &dummy);
10019 found_base = 1;
10020 continue;
10021 }
10022
10023 if (!found_base)
10024 {
10025 /* We have no valid base address for the ranges
10026 data. */
10027 complaint (&symfile_complaints,
10028 _("Invalid .debug_ranges data (no base address)"));
10029 return 0;
10030 }
10031
9277c30c
UW
10032 if (range_beginning > range_end)
10033 {
10034 /* Inverted range entries are invalid. */
10035 complaint (&symfile_complaints,
10036 _("Invalid .debug_ranges data (inverted range)"));
10037 return 0;
10038 }
10039
10040 /* Empty range entries have no effect. */
10041 if (range_beginning == range_end)
10042 continue;
10043
43039443
JK
10044 range_beginning += base;
10045 range_end += base;
10046
01093045
DE
10047 /* A not-uncommon case of bad debug info.
10048 Don't pollute the addrmap with bad data. */
10049 if (range_beginning + baseaddr == 0
10050 && !dwarf2_per_objfile->has_section_at_zero)
10051 {
10052 complaint (&symfile_complaints,
10053 _(".debug_ranges entry has start address of zero"
10054 " [in module %s]"), objfile->name);
10055 continue;
10056 }
10057
9277c30c 10058 if (ranges_pst != NULL)
ff013f42 10059 addrmap_set_empty (objfile->psymtabs_addrmap,
3e43a32a
MS
10060 range_beginning + baseaddr,
10061 range_end - 1 + baseaddr,
ff013f42
JK
10062 ranges_pst);
10063
43039443
JK
10064 /* FIXME: This is recording everything as a low-high
10065 segment of consecutive addresses. We should have a
10066 data structure for discontiguous block ranges
10067 instead. */
10068 if (! low_set)
10069 {
10070 low = range_beginning;
10071 high = range_end;
10072 low_set = 1;
10073 }
10074 else
10075 {
10076 if (range_beginning < low)
10077 low = range_beginning;
10078 if (range_end > high)
10079 high = range_end;
10080 }
10081 }
10082
10083 if (! low_set)
10084 /* If the first entry is an end-of-list marker, the range
10085 describes an empty scope, i.e. no instructions. */
10086 return 0;
10087
10088 if (low_return)
10089 *low_return = low;
10090 if (high_return)
10091 *high_return = high;
10092 return 1;
10093}
10094
af34e669
DJ
10095/* Get low and high pc attributes from a die. Return 1 if the attributes
10096 are present and valid, otherwise, return 0. Return -1 if the range is
10097 discontinuous, i.e. derived from DW_AT_ranges information. */
380bca97 10098
c906108c 10099static int
af34e669 10100dwarf2_get_pc_bounds (struct die_info *die, CORE_ADDR *lowpc,
d85a05f0
DJ
10101 CORE_ADDR *highpc, struct dwarf2_cu *cu,
10102 struct partial_symtab *pst)
c906108c
SS
10103{
10104 struct attribute *attr;
91da1414 10105 struct attribute *attr_high;
af34e669
DJ
10106 CORE_ADDR low = 0;
10107 CORE_ADDR high = 0;
10108 int ret = 0;
c906108c 10109
91da1414
MW
10110 attr_high = dwarf2_attr (die, DW_AT_high_pc, cu);
10111 if (attr_high)
af34e669 10112 {
e142c38c 10113 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
af34e669 10114 if (attr)
91da1414
MW
10115 {
10116 low = DW_ADDR (attr);
3019eac3
DE
10117 if (attr_high->form == DW_FORM_addr
10118 || attr_high->form == DW_FORM_GNU_addr_index)
91da1414
MW
10119 high = DW_ADDR (attr_high);
10120 else
10121 high = low + DW_UNSND (attr_high);
10122 }
af34e669
DJ
10123 else
10124 /* Found high w/o low attribute. */
10125 return 0;
10126
10127 /* Found consecutive range of addresses. */
10128 ret = 1;
10129 }
c906108c 10130 else
af34e669 10131 {
e142c38c 10132 attr = dwarf2_attr (die, DW_AT_ranges, cu);
af34e669
DJ
10133 if (attr != NULL)
10134 {
ab435259
DE
10135 /* DW_AT_ranges_base does not apply to DIEs from the DWO skeleton.
10136 We take advantage of the fact that DW_AT_ranges does not appear
10137 in DW_TAG_compile_unit of DWO files. */
10138 int need_ranges_base = die->tag != DW_TAG_compile_unit;
10139 unsigned int ranges_offset = (DW_UNSND (attr)
10140 + (need_ranges_base
10141 ? cu->ranges_base
10142 : 0));
2e3cf129 10143
af34e669 10144 /* Value of the DW_AT_ranges attribute is the offset in the
a604369a 10145 .debug_ranges section. */
2e3cf129 10146 if (!dwarf2_ranges_read (ranges_offset, &low, &high, cu, pst))
af34e669 10147 return 0;
43039443 10148 /* Found discontinuous range of addresses. */
af34e669
DJ
10149 ret = -1;
10150 }
10151 }
c906108c 10152
9373cf26
JK
10153 /* read_partial_die has also the strict LOW < HIGH requirement. */
10154 if (high <= low)
c906108c
SS
10155 return 0;
10156
10157 /* When using the GNU linker, .gnu.linkonce. sections are used to
10158 eliminate duplicate copies of functions and vtables and such.
10159 The linker will arbitrarily choose one and discard the others.
10160 The AT_*_pc values for such functions refer to local labels in
10161 these sections. If the section from that file was discarded, the
10162 labels are not in the output, so the relocs get a value of 0.
10163 If this is a discarded function, mark the pc bounds as invalid,
10164 so that GDB will ignore it. */
72dca2f5 10165 if (low == 0 && !dwarf2_per_objfile->has_section_at_zero)
c906108c
SS
10166 return 0;
10167
10168 *lowpc = low;
96408a79
SA
10169 if (highpc)
10170 *highpc = high;
af34e669 10171 return ret;
c906108c
SS
10172}
10173
b084d499
JB
10174/* Assuming that DIE represents a subprogram DIE or a lexical block, get
10175 its low and high PC addresses. Do nothing if these addresses could not
10176 be determined. Otherwise, set LOWPC to the low address if it is smaller,
10177 and HIGHPC to the high address if greater than HIGHPC. */
10178
10179static void
10180dwarf2_get_subprogram_pc_bounds (struct die_info *die,
10181 CORE_ADDR *lowpc, CORE_ADDR *highpc,
10182 struct dwarf2_cu *cu)
10183{
10184 CORE_ADDR low, high;
10185 struct die_info *child = die->child;
10186
d85a05f0 10187 if (dwarf2_get_pc_bounds (die, &low, &high, cu, NULL))
b084d499
JB
10188 {
10189 *lowpc = min (*lowpc, low);
10190 *highpc = max (*highpc, high);
10191 }
10192
10193 /* If the language does not allow nested subprograms (either inside
10194 subprograms or lexical blocks), we're done. */
10195 if (cu->language != language_ada)
10196 return;
6e70227d 10197
b084d499
JB
10198 /* Check all the children of the given DIE. If it contains nested
10199 subprograms, then check their pc bounds. Likewise, we need to
10200 check lexical blocks as well, as they may also contain subprogram
10201 definitions. */
10202 while (child && child->tag)
10203 {
10204 if (child->tag == DW_TAG_subprogram
10205 || child->tag == DW_TAG_lexical_block)
10206 dwarf2_get_subprogram_pc_bounds (child, lowpc, highpc, cu);
10207 child = sibling_die (child);
10208 }
10209}
10210
fae299cd
DC
10211/* Get the low and high pc's represented by the scope DIE, and store
10212 them in *LOWPC and *HIGHPC. If the correct values can't be
10213 determined, set *LOWPC to -1 and *HIGHPC to 0. */
10214
10215static void
10216get_scope_pc_bounds (struct die_info *die,
10217 CORE_ADDR *lowpc, CORE_ADDR *highpc,
10218 struct dwarf2_cu *cu)
10219{
10220 CORE_ADDR best_low = (CORE_ADDR) -1;
10221 CORE_ADDR best_high = (CORE_ADDR) 0;
10222 CORE_ADDR current_low, current_high;
10223
d85a05f0 10224 if (dwarf2_get_pc_bounds (die, &current_low, &current_high, cu, NULL))
fae299cd
DC
10225 {
10226 best_low = current_low;
10227 best_high = current_high;
10228 }
10229 else
10230 {
10231 struct die_info *child = die->child;
10232
10233 while (child && child->tag)
10234 {
10235 switch (child->tag) {
10236 case DW_TAG_subprogram:
b084d499 10237 dwarf2_get_subprogram_pc_bounds (child, &best_low, &best_high, cu);
fae299cd
DC
10238 break;
10239 case DW_TAG_namespace:
f55ee35c 10240 case DW_TAG_module:
fae299cd
DC
10241 /* FIXME: carlton/2004-01-16: Should we do this for
10242 DW_TAG_class_type/DW_TAG_structure_type, too? I think
10243 that current GCC's always emit the DIEs corresponding
10244 to definitions of methods of classes as children of a
10245 DW_TAG_compile_unit or DW_TAG_namespace (as opposed to
10246 the DIEs giving the declarations, which could be
10247 anywhere). But I don't see any reason why the
10248 standards says that they have to be there. */
10249 get_scope_pc_bounds (child, &current_low, &current_high, cu);
10250
10251 if (current_low != ((CORE_ADDR) -1))
10252 {
10253 best_low = min (best_low, current_low);
10254 best_high = max (best_high, current_high);
10255 }
10256 break;
10257 default:
0963b4bd 10258 /* Ignore. */
fae299cd
DC
10259 break;
10260 }
10261
10262 child = sibling_die (child);
10263 }
10264 }
10265
10266 *lowpc = best_low;
10267 *highpc = best_high;
10268}
10269
801e3a5b
JB
10270/* Record the address ranges for BLOCK, offset by BASEADDR, as given
10271 in DIE. */
380bca97 10272
801e3a5b
JB
10273static void
10274dwarf2_record_block_ranges (struct die_info *die, struct block *block,
10275 CORE_ADDR baseaddr, struct dwarf2_cu *cu)
10276{
bb5ed363 10277 struct objfile *objfile = cu->objfile;
801e3a5b 10278 struct attribute *attr;
91da1414 10279 struct attribute *attr_high;
801e3a5b 10280
91da1414
MW
10281 attr_high = dwarf2_attr (die, DW_AT_high_pc, cu);
10282 if (attr_high)
801e3a5b 10283 {
801e3a5b
JB
10284 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
10285 if (attr)
10286 {
10287 CORE_ADDR low = DW_ADDR (attr);
91da1414 10288 CORE_ADDR high;
3019eac3
DE
10289 if (attr_high->form == DW_FORM_addr
10290 || attr_high->form == DW_FORM_GNU_addr_index)
91da1414
MW
10291 high = DW_ADDR (attr_high);
10292 else
10293 high = low + DW_UNSND (attr_high);
9a619af0 10294
801e3a5b
JB
10295 record_block_range (block, baseaddr + low, baseaddr + high - 1);
10296 }
10297 }
10298
10299 attr = dwarf2_attr (die, DW_AT_ranges, cu);
10300 if (attr)
10301 {
bb5ed363 10302 bfd *obfd = objfile->obfd;
ab435259
DE
10303 /* DW_AT_ranges_base does not apply to DIEs from the DWO skeleton.
10304 We take advantage of the fact that DW_AT_ranges does not appear
10305 in DW_TAG_compile_unit of DWO files. */
10306 int need_ranges_base = die->tag != DW_TAG_compile_unit;
801e3a5b
JB
10307
10308 /* The value of the DW_AT_ranges attribute is the offset of the
10309 address range list in the .debug_ranges section. */
ab435259
DE
10310 unsigned long offset = (DW_UNSND (attr)
10311 + (need_ranges_base ? cu->ranges_base : 0));
dce234bc 10312 gdb_byte *buffer = dwarf2_per_objfile->ranges.buffer + offset;
801e3a5b
JB
10313
10314 /* For some target architectures, but not others, the
10315 read_address function sign-extends the addresses it returns.
10316 To recognize base address selection entries, we need a
10317 mask. */
10318 unsigned int addr_size = cu->header.addr_size;
10319 CORE_ADDR base_select_mask = ~(~(CORE_ADDR)1 << (addr_size * 8 - 1));
10320
10321 /* The base address, to which the next pair is relative. Note
10322 that this 'base' is a DWARF concept: most entries in a range
10323 list are relative, to reduce the number of relocs against the
10324 debugging information. This is separate from this function's
10325 'baseaddr' argument, which GDB uses to relocate debugging
10326 information from a shared library based on the address at
10327 which the library was loaded. */
d00adf39
DE
10328 CORE_ADDR base = cu->base_address;
10329 int base_known = cu->base_known;
801e3a5b 10330
be391dca 10331 gdb_assert (dwarf2_per_objfile->ranges.readin);
dce234bc 10332 if (offset >= dwarf2_per_objfile->ranges.size)
801e3a5b
JB
10333 {
10334 complaint (&symfile_complaints,
10335 _("Offset %lu out of bounds for DW_AT_ranges attribute"),
10336 offset);
10337 return;
10338 }
10339
10340 for (;;)
10341 {
10342 unsigned int bytes_read;
10343 CORE_ADDR start, end;
10344
10345 start = read_address (obfd, buffer, cu, &bytes_read);
10346 buffer += bytes_read;
10347 end = read_address (obfd, buffer, cu, &bytes_read);
10348 buffer += bytes_read;
10349
10350 /* Did we find the end of the range list? */
10351 if (start == 0 && end == 0)
10352 break;
10353
10354 /* Did we find a base address selection entry? */
10355 else if ((start & base_select_mask) == base_select_mask)
10356 {
10357 base = end;
10358 base_known = 1;
10359 }
10360
10361 /* We found an ordinary address range. */
10362 else
10363 {
10364 if (!base_known)
10365 {
10366 complaint (&symfile_complaints,
3e43a32a
MS
10367 _("Invalid .debug_ranges data "
10368 "(no base address)"));
801e3a5b
JB
10369 return;
10370 }
10371
9277c30c
UW
10372 if (start > end)
10373 {
10374 /* Inverted range entries are invalid. */
10375 complaint (&symfile_complaints,
10376 _("Invalid .debug_ranges data "
10377 "(inverted range)"));
10378 return;
10379 }
10380
10381 /* Empty range entries have no effect. */
10382 if (start == end)
10383 continue;
10384
01093045
DE
10385 start += base + baseaddr;
10386 end += base + baseaddr;
10387
10388 /* A not-uncommon case of bad debug info.
10389 Don't pollute the addrmap with bad data. */
10390 if (start == 0 && !dwarf2_per_objfile->has_section_at_zero)
10391 {
10392 complaint (&symfile_complaints,
10393 _(".debug_ranges entry has start address of zero"
10394 " [in module %s]"), objfile->name);
10395 continue;
10396 }
10397
10398 record_block_range (block, start, end - 1);
801e3a5b
JB
10399 }
10400 }
10401 }
10402}
10403
685b1105
JK
10404/* Check whether the producer field indicates either of GCC < 4.6, or the
10405 Intel C/C++ compiler, and cache the result in CU. */
60d5a603 10406
685b1105
JK
10407static void
10408check_producer (struct dwarf2_cu *cu)
60d5a603
JK
10409{
10410 const char *cs;
10411 int major, minor, release;
10412
10413 if (cu->producer == NULL)
10414 {
10415 /* For unknown compilers expect their behavior is DWARF version
10416 compliant.
10417
10418 GCC started to support .debug_types sections by -gdwarf-4 since
10419 gcc-4.5.x. As the .debug_types sections are missing DW_AT_producer
10420 for their space efficiency GDB cannot workaround gcc-4.5.x -gdwarf-4
10421 combination. gcc-4.5.x -gdwarf-4 binaries have DW_AT_accessibility
10422 interpreted incorrectly by GDB now - GCC PR debug/48229. */
60d5a603 10423 }
685b1105 10424 else if (strncmp (cu->producer, "GNU ", strlen ("GNU ")) == 0)
60d5a603 10425 {
685b1105
JK
10426 /* Skip any identifier after "GNU " - such as "C++" or "Java". */
10427
ba919b58
TT
10428 cs = &cu->producer[strlen ("GNU ")];
10429 while (*cs && !isdigit (*cs))
10430 cs++;
10431 if (sscanf (cs, "%d.%d.%d", &major, &minor, &release) != 3)
10432 {
10433 /* Not recognized as GCC. */
10434 }
10435 else
1b80a9fa
JK
10436 {
10437 cu->producer_is_gxx_lt_4_6 = major < 4 || (major == 4 && minor < 6);
10438 cu->producer_is_gcc_lt_4_3 = major < 4 || (major == 4 && minor < 3);
10439 }
685b1105
JK
10440 }
10441 else if (strncmp (cu->producer, "Intel(R) C", strlen ("Intel(R) C")) == 0)
10442 cu->producer_is_icc = 1;
10443 else
10444 {
10445 /* For other non-GCC compilers, expect their behavior is DWARF version
10446 compliant. */
60d5a603
JK
10447 }
10448
ba919b58 10449 cu->checked_producer = 1;
685b1105 10450}
ba919b58 10451
685b1105
JK
10452/* Check for GCC PR debug/45124 fix which is not present in any G++ version up
10453 to 4.5.any while it is present already in G++ 4.6.0 - the PR has been fixed
10454 during 4.6.0 experimental. */
10455
10456static int
10457producer_is_gxx_lt_4_6 (struct dwarf2_cu *cu)
10458{
10459 if (!cu->checked_producer)
10460 check_producer (cu);
10461
10462 return cu->producer_is_gxx_lt_4_6;
60d5a603
JK
10463}
10464
10465/* Return the default accessibility type if it is not overriden by
10466 DW_AT_accessibility. */
10467
10468static enum dwarf_access_attribute
10469dwarf2_default_access_attribute (struct die_info *die, struct dwarf2_cu *cu)
10470{
10471 if (cu->header.version < 3 || producer_is_gxx_lt_4_6 (cu))
10472 {
10473 /* The default DWARF 2 accessibility for members is public, the default
10474 accessibility for inheritance is private. */
10475
10476 if (die->tag != DW_TAG_inheritance)
10477 return DW_ACCESS_public;
10478 else
10479 return DW_ACCESS_private;
10480 }
10481 else
10482 {
10483 /* DWARF 3+ defines the default accessibility a different way. The same
10484 rules apply now for DW_TAG_inheritance as for the members and it only
10485 depends on the container kind. */
10486
10487 if (die->parent->tag == DW_TAG_class_type)
10488 return DW_ACCESS_private;
10489 else
10490 return DW_ACCESS_public;
10491 }
10492}
10493
74ac6d43
TT
10494/* Look for DW_AT_data_member_location. Set *OFFSET to the byte
10495 offset. If the attribute was not found return 0, otherwise return
10496 1. If it was found but could not properly be handled, set *OFFSET
10497 to 0. */
10498
10499static int
10500handle_data_member_location (struct die_info *die, struct dwarf2_cu *cu,
10501 LONGEST *offset)
10502{
10503 struct attribute *attr;
10504
10505 attr = dwarf2_attr (die, DW_AT_data_member_location, cu);
10506 if (attr != NULL)
10507 {
10508 *offset = 0;
10509
10510 /* Note that we do not check for a section offset first here.
10511 This is because DW_AT_data_member_location is new in DWARF 4,
10512 so if we see it, we can assume that a constant form is really
10513 a constant and not a section offset. */
10514 if (attr_form_is_constant (attr))
10515 *offset = dwarf2_get_attr_constant_value (attr, 0);
10516 else if (attr_form_is_section_offset (attr))
10517 dwarf2_complex_location_expr_complaint ();
10518 else if (attr_form_is_block (attr))
10519 *offset = decode_locdesc (DW_BLOCK (attr), cu);
10520 else
10521 dwarf2_complex_location_expr_complaint ();
10522
10523 return 1;
10524 }
10525
10526 return 0;
10527}
10528
c906108c
SS
10529/* Add an aggregate field to the field list. */
10530
10531static void
107d2387 10532dwarf2_add_field (struct field_info *fip, struct die_info *die,
e7c27a73 10533 struct dwarf2_cu *cu)
6e70227d 10534{
e7c27a73 10535 struct objfile *objfile = cu->objfile;
5e2b427d 10536 struct gdbarch *gdbarch = get_objfile_arch (objfile);
c906108c
SS
10537 struct nextfield *new_field;
10538 struct attribute *attr;
10539 struct field *fp;
15d034d0 10540 const char *fieldname = "";
c906108c
SS
10541
10542 /* Allocate a new field list entry and link it in. */
10543 new_field = (struct nextfield *) xmalloc (sizeof (struct nextfield));
b8c9b27d 10544 make_cleanup (xfree, new_field);
c906108c 10545 memset (new_field, 0, sizeof (struct nextfield));
7d0ccb61
DJ
10546
10547 if (die->tag == DW_TAG_inheritance)
10548 {
10549 new_field->next = fip->baseclasses;
10550 fip->baseclasses = new_field;
10551 }
10552 else
10553 {
10554 new_field->next = fip->fields;
10555 fip->fields = new_field;
10556 }
c906108c
SS
10557 fip->nfields++;
10558
e142c38c 10559 attr = dwarf2_attr (die, DW_AT_accessibility, cu);
c906108c
SS
10560 if (attr)
10561 new_field->accessibility = DW_UNSND (attr);
60d5a603
JK
10562 else
10563 new_field->accessibility = dwarf2_default_access_attribute (die, cu);
c906108c
SS
10564 if (new_field->accessibility != DW_ACCESS_public)
10565 fip->non_public_fields = 1;
60d5a603 10566
e142c38c 10567 attr = dwarf2_attr (die, DW_AT_virtuality, cu);
c906108c
SS
10568 if (attr)
10569 new_field->virtuality = DW_UNSND (attr);
60d5a603
JK
10570 else
10571 new_field->virtuality = DW_VIRTUALITY_none;
c906108c
SS
10572
10573 fp = &new_field->field;
a9a9bd0f 10574
e142c38c 10575 if (die->tag == DW_TAG_member && ! die_is_declaration (die, cu))
c906108c 10576 {
74ac6d43
TT
10577 LONGEST offset;
10578
a9a9bd0f 10579 /* Data member other than a C++ static data member. */
6e70227d 10580
c906108c 10581 /* Get type of field. */
e7c27a73 10582 fp->type = die_type (die, cu);
c906108c 10583
d6a843b5 10584 SET_FIELD_BITPOS (*fp, 0);
01ad7f36 10585
c906108c 10586 /* Get bit size of field (zero if none). */
e142c38c 10587 attr = dwarf2_attr (die, DW_AT_bit_size, cu);
c906108c
SS
10588 if (attr)
10589 {
10590 FIELD_BITSIZE (*fp) = DW_UNSND (attr);
10591 }
10592 else
10593 {
10594 FIELD_BITSIZE (*fp) = 0;
10595 }
10596
10597 /* Get bit offset of field. */
74ac6d43
TT
10598 if (handle_data_member_location (die, cu, &offset))
10599 SET_FIELD_BITPOS (*fp, offset * bits_per_byte);
e142c38c 10600 attr = dwarf2_attr (die, DW_AT_bit_offset, cu);
c906108c
SS
10601 if (attr)
10602 {
5e2b427d 10603 if (gdbarch_bits_big_endian (gdbarch))
c906108c
SS
10604 {
10605 /* For big endian bits, the DW_AT_bit_offset gives the
c5aa993b
JM
10606 additional bit offset from the MSB of the containing
10607 anonymous object to the MSB of the field. We don't
10608 have to do anything special since we don't need to
10609 know the size of the anonymous object. */
f41f5e61 10610 SET_FIELD_BITPOS (*fp, FIELD_BITPOS (*fp) + DW_UNSND (attr));
c906108c
SS
10611 }
10612 else
10613 {
10614 /* For little endian bits, compute the bit offset to the
c5aa993b
JM
10615 MSB of the anonymous object, subtract off the number of
10616 bits from the MSB of the field to the MSB of the
10617 object, and then subtract off the number of bits of
10618 the field itself. The result is the bit offset of
10619 the LSB of the field. */
c906108c
SS
10620 int anonymous_size;
10621 int bit_offset = DW_UNSND (attr);
10622
e142c38c 10623 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
c906108c
SS
10624 if (attr)
10625 {
10626 /* The size of the anonymous object containing
10627 the bit field is explicit, so use the
10628 indicated size (in bytes). */
10629 anonymous_size = DW_UNSND (attr);
10630 }
10631 else
10632 {
10633 /* The size of the anonymous object containing
10634 the bit field must be inferred from the type
10635 attribute of the data member containing the
10636 bit field. */
10637 anonymous_size = TYPE_LENGTH (fp->type);
10638 }
f41f5e61
PA
10639 SET_FIELD_BITPOS (*fp,
10640 (FIELD_BITPOS (*fp)
10641 + anonymous_size * bits_per_byte
10642 - bit_offset - FIELD_BITSIZE (*fp)));
c906108c
SS
10643 }
10644 }
10645
10646 /* Get name of field. */
39cbfefa
DJ
10647 fieldname = dwarf2_name (die, cu);
10648 if (fieldname == NULL)
10649 fieldname = "";
d8151005
DJ
10650
10651 /* The name is already allocated along with this objfile, so we don't
10652 need to duplicate it for the type. */
10653 fp->name = fieldname;
c906108c
SS
10654
10655 /* Change accessibility for artificial fields (e.g. virtual table
c5aa993b 10656 pointer or virtual base class pointer) to private. */
e142c38c 10657 if (dwarf2_attr (die, DW_AT_artificial, cu))
c906108c 10658 {
d48cc9dd 10659 FIELD_ARTIFICIAL (*fp) = 1;
c906108c
SS
10660 new_field->accessibility = DW_ACCESS_private;
10661 fip->non_public_fields = 1;
10662 }
10663 }
a9a9bd0f 10664 else if (die->tag == DW_TAG_member || die->tag == DW_TAG_variable)
c906108c 10665 {
a9a9bd0f
DC
10666 /* C++ static member. */
10667
10668 /* NOTE: carlton/2002-11-05: It should be a DW_TAG_member that
10669 is a declaration, but all versions of G++ as of this writing
10670 (so through at least 3.2.1) incorrectly generate
10671 DW_TAG_variable tags. */
6e70227d 10672
ff355380 10673 const char *physname;
c906108c 10674
a9a9bd0f 10675 /* Get name of field. */
39cbfefa
DJ
10676 fieldname = dwarf2_name (die, cu);
10677 if (fieldname == NULL)
c906108c
SS
10678 return;
10679
254e6b9e 10680 attr = dwarf2_attr (die, DW_AT_const_value, cu);
3863f96c
DE
10681 if (attr
10682 /* Only create a symbol if this is an external value.
10683 new_symbol checks this and puts the value in the global symbol
10684 table, which we want. If it is not external, new_symbol
10685 will try to put the value in cu->list_in_scope which is wrong. */
10686 && dwarf2_flag_true_p (die, DW_AT_external, cu))
254e6b9e
DE
10687 {
10688 /* A static const member, not much different than an enum as far as
10689 we're concerned, except that we can support more types. */
10690 new_symbol (die, NULL, cu);
10691 }
10692
2df3850c 10693 /* Get physical name. */
ff355380 10694 physname = dwarf2_physname (fieldname, die, cu);
c906108c 10695
d8151005
DJ
10696 /* The name is already allocated along with this objfile, so we don't
10697 need to duplicate it for the type. */
10698 SET_FIELD_PHYSNAME (*fp, physname ? physname : "");
e7c27a73 10699 FIELD_TYPE (*fp) = die_type (die, cu);
d8151005 10700 FIELD_NAME (*fp) = fieldname;
c906108c
SS
10701 }
10702 else if (die->tag == DW_TAG_inheritance)
10703 {
74ac6d43 10704 LONGEST offset;
d4b96c9a 10705
74ac6d43
TT
10706 /* C++ base class field. */
10707 if (handle_data_member_location (die, cu, &offset))
10708 SET_FIELD_BITPOS (*fp, offset * bits_per_byte);
c906108c 10709 FIELD_BITSIZE (*fp) = 0;
e7c27a73 10710 FIELD_TYPE (*fp) = die_type (die, cu);
c906108c
SS
10711 FIELD_NAME (*fp) = type_name_no_tag (fp->type);
10712 fip->nbaseclasses++;
10713 }
10714}
10715
98751a41
JK
10716/* Add a typedef defined in the scope of the FIP's class. */
10717
10718static void
10719dwarf2_add_typedef (struct field_info *fip, struct die_info *die,
10720 struct dwarf2_cu *cu)
6e70227d 10721{
98751a41 10722 struct objfile *objfile = cu->objfile;
98751a41
JK
10723 struct typedef_field_list *new_field;
10724 struct attribute *attr;
10725 struct typedef_field *fp;
10726 char *fieldname = "";
10727
10728 /* Allocate a new field list entry and link it in. */
10729 new_field = xzalloc (sizeof (*new_field));
10730 make_cleanup (xfree, new_field);
10731
10732 gdb_assert (die->tag == DW_TAG_typedef);
10733
10734 fp = &new_field->field;
10735
10736 /* Get name of field. */
10737 fp->name = dwarf2_name (die, cu);
10738 if (fp->name == NULL)
10739 return;
10740
10741 fp->type = read_type_die (die, cu);
10742
10743 new_field->next = fip->typedef_field_list;
10744 fip->typedef_field_list = new_field;
10745 fip->typedef_field_list_count++;
10746}
10747
c906108c
SS
10748/* Create the vector of fields, and attach it to the type. */
10749
10750static void
fba45db2 10751dwarf2_attach_fields_to_type (struct field_info *fip, struct type *type,
e7c27a73 10752 struct dwarf2_cu *cu)
c906108c
SS
10753{
10754 int nfields = fip->nfields;
10755
10756 /* Record the field count, allocate space for the array of fields,
10757 and create blank accessibility bitfields if necessary. */
10758 TYPE_NFIELDS (type) = nfields;
10759 TYPE_FIELDS (type) = (struct field *)
10760 TYPE_ALLOC (type, sizeof (struct field) * nfields);
10761 memset (TYPE_FIELDS (type), 0, sizeof (struct field) * nfields);
10762
b4ba55a1 10763 if (fip->non_public_fields && cu->language != language_ada)
c906108c
SS
10764 {
10765 ALLOCATE_CPLUS_STRUCT_TYPE (type);
10766
10767 TYPE_FIELD_PRIVATE_BITS (type) =
10768 (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
10769 B_CLRALL (TYPE_FIELD_PRIVATE_BITS (type), nfields);
10770
10771 TYPE_FIELD_PROTECTED_BITS (type) =
10772 (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
10773 B_CLRALL (TYPE_FIELD_PROTECTED_BITS (type), nfields);
10774
774b6a14
TT
10775 TYPE_FIELD_IGNORE_BITS (type) =
10776 (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
10777 B_CLRALL (TYPE_FIELD_IGNORE_BITS (type), nfields);
c906108c
SS
10778 }
10779
10780 /* If the type has baseclasses, allocate and clear a bit vector for
10781 TYPE_FIELD_VIRTUAL_BITS. */
b4ba55a1 10782 if (fip->nbaseclasses && cu->language != language_ada)
c906108c
SS
10783 {
10784 int num_bytes = B_BYTES (fip->nbaseclasses);
fe1b8b76 10785 unsigned char *pointer;
c906108c
SS
10786
10787 ALLOCATE_CPLUS_STRUCT_TYPE (type);
fe1b8b76
JB
10788 pointer = TYPE_ALLOC (type, num_bytes);
10789 TYPE_FIELD_VIRTUAL_BITS (type) = pointer;
c906108c
SS
10790 B_CLRALL (TYPE_FIELD_VIRTUAL_BITS (type), fip->nbaseclasses);
10791 TYPE_N_BASECLASSES (type) = fip->nbaseclasses;
10792 }
10793
3e43a32a
MS
10794 /* Copy the saved-up fields into the field vector. Start from the head of
10795 the list, adding to the tail of the field array, so that they end up in
10796 the same order in the array in which they were added to the list. */
c906108c
SS
10797 while (nfields-- > 0)
10798 {
7d0ccb61
DJ
10799 struct nextfield *fieldp;
10800
10801 if (fip->fields)
10802 {
10803 fieldp = fip->fields;
10804 fip->fields = fieldp->next;
10805 }
10806 else
10807 {
10808 fieldp = fip->baseclasses;
10809 fip->baseclasses = fieldp->next;
10810 }
10811
10812 TYPE_FIELD (type, nfields) = fieldp->field;
10813 switch (fieldp->accessibility)
c906108c 10814 {
c5aa993b 10815 case DW_ACCESS_private:
b4ba55a1
JB
10816 if (cu->language != language_ada)
10817 SET_TYPE_FIELD_PRIVATE (type, nfields);
c5aa993b 10818 break;
c906108c 10819
c5aa993b 10820 case DW_ACCESS_protected:
b4ba55a1
JB
10821 if (cu->language != language_ada)
10822 SET_TYPE_FIELD_PROTECTED (type, nfields);
c5aa993b 10823 break;
c906108c 10824
c5aa993b
JM
10825 case DW_ACCESS_public:
10826 break;
c906108c 10827
c5aa993b
JM
10828 default:
10829 /* Unknown accessibility. Complain and treat it as public. */
10830 {
e2e0b3e5 10831 complaint (&symfile_complaints, _("unsupported accessibility %d"),
7d0ccb61 10832 fieldp->accessibility);
c5aa993b
JM
10833 }
10834 break;
c906108c
SS
10835 }
10836 if (nfields < fip->nbaseclasses)
10837 {
7d0ccb61 10838 switch (fieldp->virtuality)
c906108c 10839 {
c5aa993b
JM
10840 case DW_VIRTUALITY_virtual:
10841 case DW_VIRTUALITY_pure_virtual:
b4ba55a1 10842 if (cu->language == language_ada)
a73c6dcd 10843 error (_("unexpected virtuality in component of Ada type"));
c5aa993b
JM
10844 SET_TYPE_FIELD_VIRTUAL (type, nfields);
10845 break;
c906108c
SS
10846 }
10847 }
c906108c
SS
10848 }
10849}
10850
7d27a96d
TT
10851/* Return true if this member function is a constructor, false
10852 otherwise. */
10853
10854static int
10855dwarf2_is_constructor (struct die_info *die, struct dwarf2_cu *cu)
10856{
10857 const char *fieldname;
10858 const char *typename;
10859 int len;
10860
10861 if (die->parent == NULL)
10862 return 0;
10863
10864 if (die->parent->tag != DW_TAG_structure_type
10865 && die->parent->tag != DW_TAG_union_type
10866 && die->parent->tag != DW_TAG_class_type)
10867 return 0;
10868
10869 fieldname = dwarf2_name (die, cu);
10870 typename = dwarf2_name (die->parent, cu);
10871 if (fieldname == NULL || typename == NULL)
10872 return 0;
10873
10874 len = strlen (fieldname);
10875 return (strncmp (fieldname, typename, len) == 0
10876 && (typename[len] == '\0' || typename[len] == '<'));
10877}
10878
c906108c
SS
10879/* Add a member function to the proper fieldlist. */
10880
10881static void
107d2387 10882dwarf2_add_member_fn (struct field_info *fip, struct die_info *die,
e7c27a73 10883 struct type *type, struct dwarf2_cu *cu)
c906108c 10884{
e7c27a73 10885 struct objfile *objfile = cu->objfile;
c906108c
SS
10886 struct attribute *attr;
10887 struct fnfieldlist *flp;
10888 int i;
10889 struct fn_field *fnp;
15d034d0 10890 const char *fieldname;
c906108c 10891 struct nextfnfield *new_fnfield;
f792889a 10892 struct type *this_type;
60d5a603 10893 enum dwarf_access_attribute accessibility;
c906108c 10894
b4ba55a1 10895 if (cu->language == language_ada)
a73c6dcd 10896 error (_("unexpected member function in Ada type"));
b4ba55a1 10897
2df3850c 10898 /* Get name of member function. */
39cbfefa
DJ
10899 fieldname = dwarf2_name (die, cu);
10900 if (fieldname == NULL)
2df3850c 10901 return;
c906108c 10902
c906108c
SS
10903 /* Look up member function name in fieldlist. */
10904 for (i = 0; i < fip->nfnfields; i++)
10905 {
27bfe10e 10906 if (strcmp (fip->fnfieldlists[i].name, fieldname) == 0)
c906108c
SS
10907 break;
10908 }
10909
10910 /* Create new list element if necessary. */
10911 if (i < fip->nfnfields)
10912 flp = &fip->fnfieldlists[i];
10913 else
10914 {
10915 if ((fip->nfnfields % DW_FIELD_ALLOC_CHUNK) == 0)
10916 {
10917 fip->fnfieldlists = (struct fnfieldlist *)
10918 xrealloc (fip->fnfieldlists,
10919 (fip->nfnfields + DW_FIELD_ALLOC_CHUNK)
c5aa993b 10920 * sizeof (struct fnfieldlist));
c906108c 10921 if (fip->nfnfields == 0)
c13c43fd 10922 make_cleanup (free_current_contents, &fip->fnfieldlists);
c906108c
SS
10923 }
10924 flp = &fip->fnfieldlists[fip->nfnfields];
10925 flp->name = fieldname;
10926 flp->length = 0;
10927 flp->head = NULL;
3da10d80 10928 i = fip->nfnfields++;
c906108c
SS
10929 }
10930
10931 /* Create a new member function field and chain it to the field list
0963b4bd 10932 entry. */
c906108c 10933 new_fnfield = (struct nextfnfield *) xmalloc (sizeof (struct nextfnfield));
b8c9b27d 10934 make_cleanup (xfree, new_fnfield);
c906108c
SS
10935 memset (new_fnfield, 0, sizeof (struct nextfnfield));
10936 new_fnfield->next = flp->head;
10937 flp->head = new_fnfield;
10938 flp->length++;
10939
10940 /* Fill in the member function field info. */
10941 fnp = &new_fnfield->fnfield;
3da10d80
KS
10942
10943 /* Delay processing of the physname until later. */
10944 if (cu->language == language_cplus || cu->language == language_java)
10945 {
10946 add_to_method_list (type, i, flp->length - 1, fieldname,
10947 die, cu);
10948 }
10949 else
10950 {
1d06ead6 10951 const char *physname = dwarf2_physname (fieldname, die, cu);
3da10d80
KS
10952 fnp->physname = physname ? physname : "";
10953 }
10954
c906108c 10955 fnp->type = alloc_type (objfile);
f792889a
DJ
10956 this_type = read_type_die (die, cu);
10957 if (this_type && TYPE_CODE (this_type) == TYPE_CODE_FUNC)
c906108c 10958 {
f792889a 10959 int nparams = TYPE_NFIELDS (this_type);
c906108c 10960
f792889a 10961 /* TYPE is the domain of this method, and THIS_TYPE is the type
e26fb1d7
DC
10962 of the method itself (TYPE_CODE_METHOD). */
10963 smash_to_method_type (fnp->type, type,
f792889a
DJ
10964 TYPE_TARGET_TYPE (this_type),
10965 TYPE_FIELDS (this_type),
10966 TYPE_NFIELDS (this_type),
10967 TYPE_VARARGS (this_type));
c906108c
SS
10968
10969 /* Handle static member functions.
c5aa993b 10970 Dwarf2 has no clean way to discern C++ static and non-static
0963b4bd
MS
10971 member functions. G++ helps GDB by marking the first
10972 parameter for non-static member functions (which is the this
10973 pointer) as artificial. We obtain this information from
10974 read_subroutine_type via TYPE_FIELD_ARTIFICIAL. */
f792889a 10975 if (nparams == 0 || TYPE_FIELD_ARTIFICIAL (this_type, 0) == 0)
c906108c
SS
10976 fnp->voffset = VOFFSET_STATIC;
10977 }
10978 else
e2e0b3e5 10979 complaint (&symfile_complaints, _("member function type missing for '%s'"),
3da10d80 10980 dwarf2_full_name (fieldname, die, cu));
c906108c
SS
10981
10982 /* Get fcontext from DW_AT_containing_type if present. */
e142c38c 10983 if (dwarf2_attr (die, DW_AT_containing_type, cu) != NULL)
e7c27a73 10984 fnp->fcontext = die_containing_type (die, cu);
c906108c 10985
3e43a32a
MS
10986 /* dwarf2 doesn't have stubbed physical names, so the setting of is_const and
10987 is_volatile is irrelevant, as it is needed by gdb_mangle_name only. */
c906108c
SS
10988
10989 /* Get accessibility. */
e142c38c 10990 attr = dwarf2_attr (die, DW_AT_accessibility, cu);
c906108c 10991 if (attr)
60d5a603
JK
10992 accessibility = DW_UNSND (attr);
10993 else
10994 accessibility = dwarf2_default_access_attribute (die, cu);
10995 switch (accessibility)
c906108c 10996 {
60d5a603
JK
10997 case DW_ACCESS_private:
10998 fnp->is_private = 1;
10999 break;
11000 case DW_ACCESS_protected:
11001 fnp->is_protected = 1;
11002 break;
c906108c
SS
11003 }
11004
b02dede2 11005 /* Check for artificial methods. */
e142c38c 11006 attr = dwarf2_attr (die, DW_AT_artificial, cu);
b02dede2
DJ
11007 if (attr && DW_UNSND (attr) != 0)
11008 fnp->is_artificial = 1;
11009
7d27a96d
TT
11010 fnp->is_constructor = dwarf2_is_constructor (die, cu);
11011
0d564a31 11012 /* Get index in virtual function table if it is a virtual member
aec5aa8b
TT
11013 function. For older versions of GCC, this is an offset in the
11014 appropriate virtual table, as specified by DW_AT_containing_type.
11015 For everyone else, it is an expression to be evaluated relative
0d564a31
DJ
11016 to the object address. */
11017
e142c38c 11018 attr = dwarf2_attr (die, DW_AT_vtable_elem_location, cu);
aec5aa8b 11019 if (attr)
8e19ed76 11020 {
aec5aa8b 11021 if (attr_form_is_block (attr) && DW_BLOCK (attr)->size > 0)
8e19ed76 11022 {
aec5aa8b
TT
11023 if (DW_BLOCK (attr)->data[0] == DW_OP_constu)
11024 {
11025 /* Old-style GCC. */
11026 fnp->voffset = decode_locdesc (DW_BLOCK (attr), cu) + 2;
11027 }
11028 else if (DW_BLOCK (attr)->data[0] == DW_OP_deref
11029 || (DW_BLOCK (attr)->size > 1
11030 && DW_BLOCK (attr)->data[0] == DW_OP_deref_size
11031 && DW_BLOCK (attr)->data[1] == cu->header.addr_size))
11032 {
11033 struct dwarf_block blk;
11034 int offset;
11035
11036 offset = (DW_BLOCK (attr)->data[0] == DW_OP_deref
11037 ? 1 : 2);
11038 blk.size = DW_BLOCK (attr)->size - offset;
11039 blk.data = DW_BLOCK (attr)->data + offset;
11040 fnp->voffset = decode_locdesc (DW_BLOCK (attr), cu);
11041 if ((fnp->voffset % cu->header.addr_size) != 0)
11042 dwarf2_complex_location_expr_complaint ();
11043 else
11044 fnp->voffset /= cu->header.addr_size;
11045 fnp->voffset += 2;
11046 }
11047 else
11048 dwarf2_complex_location_expr_complaint ();
11049
11050 if (!fnp->fcontext)
11051 fnp->fcontext = TYPE_TARGET_TYPE (TYPE_FIELD_TYPE (this_type, 0));
11052 }
3690dd37 11053 else if (attr_form_is_section_offset (attr))
8e19ed76 11054 {
4d3c2250 11055 dwarf2_complex_location_expr_complaint ();
8e19ed76
PS
11056 }
11057 else
11058 {
4d3c2250
KB
11059 dwarf2_invalid_attrib_class_complaint ("DW_AT_vtable_elem_location",
11060 fieldname);
8e19ed76 11061 }
0d564a31 11062 }
d48cc9dd
DJ
11063 else
11064 {
11065 attr = dwarf2_attr (die, DW_AT_virtuality, cu);
11066 if (attr && DW_UNSND (attr))
11067 {
11068 /* GCC does this, as of 2008-08-25; PR debug/37237. */
11069 complaint (&symfile_complaints,
3e43a32a
MS
11070 _("Member function \"%s\" (offset %d) is virtual "
11071 "but the vtable offset is not specified"),
b64f50a1 11072 fieldname, die->offset.sect_off);
9655fd1a 11073 ALLOCATE_CPLUS_STRUCT_TYPE (type);
d48cc9dd
DJ
11074 TYPE_CPLUS_DYNAMIC (type) = 1;
11075 }
11076 }
c906108c
SS
11077}
11078
11079/* Create the vector of member function fields, and attach it to the type. */
11080
11081static void
fba45db2 11082dwarf2_attach_fn_fields_to_type (struct field_info *fip, struct type *type,
e7c27a73 11083 struct dwarf2_cu *cu)
c906108c
SS
11084{
11085 struct fnfieldlist *flp;
c906108c
SS
11086 int i;
11087
b4ba55a1 11088 if (cu->language == language_ada)
a73c6dcd 11089 error (_("unexpected member functions in Ada type"));
b4ba55a1 11090
c906108c
SS
11091 ALLOCATE_CPLUS_STRUCT_TYPE (type);
11092 TYPE_FN_FIELDLISTS (type) = (struct fn_fieldlist *)
11093 TYPE_ALLOC (type, sizeof (struct fn_fieldlist) * fip->nfnfields);
11094
11095 for (i = 0, flp = fip->fnfieldlists; i < fip->nfnfields; i++, flp++)
11096 {
11097 struct nextfnfield *nfp = flp->head;
11098 struct fn_fieldlist *fn_flp = &TYPE_FN_FIELDLIST (type, i);
11099 int k;
11100
11101 TYPE_FN_FIELDLIST_NAME (type, i) = flp->name;
11102 TYPE_FN_FIELDLIST_LENGTH (type, i) = flp->length;
11103 fn_flp->fn_fields = (struct fn_field *)
11104 TYPE_ALLOC (type, sizeof (struct fn_field) * flp->length);
11105 for (k = flp->length; (k--, nfp); nfp = nfp->next)
c5aa993b 11106 fn_flp->fn_fields[k] = nfp->fnfield;
c906108c
SS
11107 }
11108
11109 TYPE_NFN_FIELDS (type) = fip->nfnfields;
c906108c
SS
11110}
11111
1168df01
JB
11112/* Returns non-zero if NAME is the name of a vtable member in CU's
11113 language, zero otherwise. */
11114static int
11115is_vtable_name (const char *name, struct dwarf2_cu *cu)
11116{
11117 static const char vptr[] = "_vptr";
987504bb 11118 static const char vtable[] = "vtable";
1168df01 11119
987504bb
JJ
11120 /* Look for the C++ and Java forms of the vtable. */
11121 if ((cu->language == language_java
11122 && strncmp (name, vtable, sizeof (vtable) - 1) == 0)
11123 || (strncmp (name, vptr, sizeof (vptr) - 1) == 0
11124 && is_cplus_marker (name[sizeof (vptr) - 1])))
1168df01
JB
11125 return 1;
11126
11127 return 0;
11128}
11129
c0dd20ea 11130/* GCC outputs unnamed structures that are really pointers to member
0b92b5bb
TT
11131 functions, with the ABI-specified layout. If TYPE describes
11132 such a structure, smash it into a member function type.
61049d3b
DJ
11133
11134 GCC shouldn't do this; it should just output pointer to member DIEs.
11135 This is GCC PR debug/28767. */
c0dd20ea 11136
0b92b5bb
TT
11137static void
11138quirk_gcc_member_function_pointer (struct type *type, struct objfile *objfile)
c0dd20ea 11139{
0b92b5bb 11140 struct type *pfn_type, *domain_type, *new_type;
c0dd20ea
DJ
11141
11142 /* Check for a structure with no name and two children. */
0b92b5bb
TT
11143 if (TYPE_CODE (type) != TYPE_CODE_STRUCT || TYPE_NFIELDS (type) != 2)
11144 return;
c0dd20ea
DJ
11145
11146 /* Check for __pfn and __delta members. */
0b92b5bb
TT
11147 if (TYPE_FIELD_NAME (type, 0) == NULL
11148 || strcmp (TYPE_FIELD_NAME (type, 0), "__pfn") != 0
11149 || TYPE_FIELD_NAME (type, 1) == NULL
11150 || strcmp (TYPE_FIELD_NAME (type, 1), "__delta") != 0)
11151 return;
c0dd20ea
DJ
11152
11153 /* Find the type of the method. */
0b92b5bb 11154 pfn_type = TYPE_FIELD_TYPE (type, 0);
c0dd20ea
DJ
11155 if (pfn_type == NULL
11156 || TYPE_CODE (pfn_type) != TYPE_CODE_PTR
11157 || TYPE_CODE (TYPE_TARGET_TYPE (pfn_type)) != TYPE_CODE_FUNC)
0b92b5bb 11158 return;
c0dd20ea
DJ
11159
11160 /* Look for the "this" argument. */
11161 pfn_type = TYPE_TARGET_TYPE (pfn_type);
11162 if (TYPE_NFIELDS (pfn_type) == 0
0b92b5bb 11163 /* || TYPE_FIELD_TYPE (pfn_type, 0) == NULL */
c0dd20ea 11164 || TYPE_CODE (TYPE_FIELD_TYPE (pfn_type, 0)) != TYPE_CODE_PTR)
0b92b5bb 11165 return;
c0dd20ea
DJ
11166
11167 domain_type = TYPE_TARGET_TYPE (TYPE_FIELD_TYPE (pfn_type, 0));
0b92b5bb
TT
11168 new_type = alloc_type (objfile);
11169 smash_to_method_type (new_type, domain_type, TYPE_TARGET_TYPE (pfn_type),
c0dd20ea
DJ
11170 TYPE_FIELDS (pfn_type), TYPE_NFIELDS (pfn_type),
11171 TYPE_VARARGS (pfn_type));
0b92b5bb 11172 smash_to_methodptr_type (type, new_type);
c0dd20ea 11173}
1168df01 11174
685b1105
JK
11175/* Return non-zero if the CU's PRODUCER string matches the Intel C/C++ compiler
11176 (icc). */
11177
11178static int
11179producer_is_icc (struct dwarf2_cu *cu)
11180{
11181 if (!cu->checked_producer)
11182 check_producer (cu);
11183
11184 return cu->producer_is_icc;
11185}
11186
c906108c 11187/* Called when we find the DIE that starts a structure or union scope
c767944b
DJ
11188 (definition) to create a type for the structure or union. Fill in
11189 the type's name and general properties; the members will not be
11190 processed until process_structure_type.
c906108c 11191
c767944b
DJ
11192 NOTE: we need to call these functions regardless of whether or not the
11193 DIE has a DW_AT_name attribute, since it might be an anonymous
c906108c
SS
11194 structure or union. This gets the type entered into our set of
11195 user defined types.
11196
11197 However, if the structure is incomplete (an opaque struct/union)
11198 then suppress creating a symbol table entry for it since gdb only
11199 wants to find the one with the complete definition. Note that if
11200 it is complete, we just call new_symbol, which does it's own
11201 checking about whether the struct/union is anonymous or not (and
11202 suppresses creating a symbol table entry itself). */
11203
f792889a 11204static struct type *
134d01f1 11205read_structure_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 11206{
e7c27a73 11207 struct objfile *objfile = cu->objfile;
c906108c
SS
11208 struct type *type;
11209 struct attribute *attr;
15d034d0 11210 const char *name;
c906108c 11211
348e048f
DE
11212 /* If the definition of this type lives in .debug_types, read that type.
11213 Don't follow DW_AT_specification though, that will take us back up
11214 the chain and we want to go down. */
45e58e77 11215 attr = dwarf2_attr_no_follow (die, DW_AT_signature);
348e048f
DE
11216 if (attr)
11217 {
11218 struct dwarf2_cu *type_cu = cu;
11219 struct die_info *type_die = follow_die_ref_or_sig (die, attr, &type_cu);
9a619af0 11220
348e048f
DE
11221 /* We could just recurse on read_structure_type, but we need to call
11222 get_die_type to ensure only one type for this DIE is created.
11223 This is important, for example, because for c++ classes we need
11224 TYPE_NAME set which is only done by new_symbol. Blech. */
11225 type = read_type_die (type_die, type_cu);
9dc481d3
DE
11226
11227 /* TYPE_CU may not be the same as CU.
11228 Ensure TYPE is recorded in CU's type_hash table. */
348e048f
DE
11229 return set_die_type (die, type, cu);
11230 }
11231
c0dd20ea 11232 type = alloc_type (objfile);
c906108c 11233 INIT_CPLUS_SPECIFIC (type);
93311388 11234
39cbfefa
DJ
11235 name = dwarf2_name (die, cu);
11236 if (name != NULL)
c906108c 11237 {
987504bb
JJ
11238 if (cu->language == language_cplus
11239 || cu->language == language_java)
63d06c5c 11240 {
15d034d0 11241 const char *full_name = dwarf2_full_name (name, die, cu);
3da10d80
KS
11242
11243 /* dwarf2_full_name might have already finished building the DIE's
11244 type. If so, there is no need to continue. */
11245 if (get_die_type (die, cu) != NULL)
11246 return get_die_type (die, cu);
11247
11248 TYPE_TAG_NAME (type) = full_name;
94af9270
KS
11249 if (die->tag == DW_TAG_structure_type
11250 || die->tag == DW_TAG_class_type)
11251 TYPE_NAME (type) = TYPE_TAG_NAME (type);
63d06c5c
DC
11252 }
11253 else
11254 {
d8151005
DJ
11255 /* The name is already allocated along with this objfile, so
11256 we don't need to duplicate it for the type. */
94af9270
KS
11257 TYPE_TAG_NAME (type) = (char *) name;
11258 if (die->tag == DW_TAG_class_type)
11259 TYPE_NAME (type) = TYPE_TAG_NAME (type);
63d06c5c 11260 }
c906108c
SS
11261 }
11262
11263 if (die->tag == DW_TAG_structure_type)
11264 {
11265 TYPE_CODE (type) = TYPE_CODE_STRUCT;
11266 }
11267 else if (die->tag == DW_TAG_union_type)
11268 {
11269 TYPE_CODE (type) = TYPE_CODE_UNION;
11270 }
11271 else
11272 {
c906108c
SS
11273 TYPE_CODE (type) = TYPE_CODE_CLASS;
11274 }
11275
0cc2414c
TT
11276 if (cu->language == language_cplus && die->tag == DW_TAG_class_type)
11277 TYPE_DECLARED_CLASS (type) = 1;
11278
e142c38c 11279 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
c906108c
SS
11280 if (attr)
11281 {
11282 TYPE_LENGTH (type) = DW_UNSND (attr);
11283 }
11284 else
11285 {
11286 TYPE_LENGTH (type) = 0;
11287 }
11288
685b1105
JK
11289 if (producer_is_icc (cu))
11290 {
11291 /* ICC does not output the required DW_AT_declaration
11292 on incomplete types, but gives them a size of zero. */
11293 }
11294 else
11295 TYPE_STUB_SUPPORTED (type) = 1;
11296
dc718098 11297 if (die_is_declaration (die, cu))
876cecd0 11298 TYPE_STUB (type) = 1;
a6c727b2
DJ
11299 else if (attr == NULL && die->child == NULL
11300 && producer_is_realview (cu->producer))
11301 /* RealView does not output the required DW_AT_declaration
11302 on incomplete types. */
11303 TYPE_STUB (type) = 1;
dc718098 11304
c906108c
SS
11305 /* We need to add the type field to the die immediately so we don't
11306 infinitely recurse when dealing with pointers to the structure
0963b4bd 11307 type within the structure itself. */
1c379e20 11308 set_die_type (die, type, cu);
c906108c 11309
7e314c57
JK
11310 /* set_die_type should be already done. */
11311 set_descriptive_type (type, die, cu);
11312
c767944b
DJ
11313 return type;
11314}
11315
11316/* Finish creating a structure or union type, including filling in
11317 its members and creating a symbol for it. */
11318
11319static void
11320process_structure_scope (struct die_info *die, struct dwarf2_cu *cu)
11321{
11322 struct objfile *objfile = cu->objfile;
11323 struct die_info *child_die = die->child;
11324 struct type *type;
11325
11326 type = get_die_type (die, cu);
11327 if (type == NULL)
11328 type = read_structure_type (die, cu);
11329
e142c38c 11330 if (die->child != NULL && ! die_is_declaration (die, cu))
c906108c
SS
11331 {
11332 struct field_info fi;
11333 struct die_info *child_die;
34eaf542 11334 VEC (symbolp) *template_args = NULL;
c767944b 11335 struct cleanup *back_to = make_cleanup (null_cleanup, 0);
c906108c
SS
11336
11337 memset (&fi, 0, sizeof (struct field_info));
11338
639d11d3 11339 child_die = die->child;
c906108c
SS
11340
11341 while (child_die && child_die->tag)
11342 {
a9a9bd0f
DC
11343 if (child_die->tag == DW_TAG_member
11344 || child_die->tag == DW_TAG_variable)
c906108c 11345 {
a9a9bd0f
DC
11346 /* NOTE: carlton/2002-11-05: A C++ static data member
11347 should be a DW_TAG_member that is a declaration, but
11348 all versions of G++ as of this writing (so through at
11349 least 3.2.1) incorrectly generate DW_TAG_variable
11350 tags for them instead. */
e7c27a73 11351 dwarf2_add_field (&fi, child_die, cu);
c906108c 11352 }
8713b1b1 11353 else if (child_die->tag == DW_TAG_subprogram)
c906108c 11354 {
0963b4bd 11355 /* C++ member function. */
e7c27a73 11356 dwarf2_add_member_fn (&fi, child_die, type, cu);
c906108c
SS
11357 }
11358 else if (child_die->tag == DW_TAG_inheritance)
11359 {
11360 /* C++ base class field. */
e7c27a73 11361 dwarf2_add_field (&fi, child_die, cu);
c906108c 11362 }
98751a41
JK
11363 else if (child_die->tag == DW_TAG_typedef)
11364 dwarf2_add_typedef (&fi, child_die, cu);
34eaf542
TT
11365 else if (child_die->tag == DW_TAG_template_type_param
11366 || child_die->tag == DW_TAG_template_value_param)
11367 {
11368 struct symbol *arg = new_symbol (child_die, NULL, cu);
11369
f1078f66
DJ
11370 if (arg != NULL)
11371 VEC_safe_push (symbolp, template_args, arg);
34eaf542
TT
11372 }
11373
c906108c
SS
11374 child_die = sibling_die (child_die);
11375 }
11376
34eaf542
TT
11377 /* Attach template arguments to type. */
11378 if (! VEC_empty (symbolp, template_args))
11379 {
11380 ALLOCATE_CPLUS_STRUCT_TYPE (type);
11381 TYPE_N_TEMPLATE_ARGUMENTS (type)
11382 = VEC_length (symbolp, template_args);
11383 TYPE_TEMPLATE_ARGUMENTS (type)
11384 = obstack_alloc (&objfile->objfile_obstack,
11385 (TYPE_N_TEMPLATE_ARGUMENTS (type)
11386 * sizeof (struct symbol *)));
11387 memcpy (TYPE_TEMPLATE_ARGUMENTS (type),
11388 VEC_address (symbolp, template_args),
11389 (TYPE_N_TEMPLATE_ARGUMENTS (type)
11390 * sizeof (struct symbol *)));
11391 VEC_free (symbolp, template_args);
11392 }
11393
c906108c
SS
11394 /* Attach fields and member functions to the type. */
11395 if (fi.nfields)
e7c27a73 11396 dwarf2_attach_fields_to_type (&fi, type, cu);
c906108c
SS
11397 if (fi.nfnfields)
11398 {
e7c27a73 11399 dwarf2_attach_fn_fields_to_type (&fi, type, cu);
c906108c 11400
c5aa993b 11401 /* Get the type which refers to the base class (possibly this
c906108c 11402 class itself) which contains the vtable pointer for the current
0d564a31
DJ
11403 class from the DW_AT_containing_type attribute. This use of
11404 DW_AT_containing_type is a GNU extension. */
c906108c 11405
e142c38c 11406 if (dwarf2_attr (die, DW_AT_containing_type, cu) != NULL)
c906108c 11407 {
e7c27a73 11408 struct type *t = die_containing_type (die, cu);
c906108c
SS
11409
11410 TYPE_VPTR_BASETYPE (type) = t;
11411 if (type == t)
11412 {
c906108c
SS
11413 int i;
11414
11415 /* Our own class provides vtbl ptr. */
11416 for (i = TYPE_NFIELDS (t) - 1;
11417 i >= TYPE_N_BASECLASSES (t);
11418 --i)
11419 {
0d5cff50 11420 const char *fieldname = TYPE_FIELD_NAME (t, i);
c906108c 11421
1168df01 11422 if (is_vtable_name (fieldname, cu))
c906108c
SS
11423 {
11424 TYPE_VPTR_FIELDNO (type) = i;
11425 break;
11426 }
11427 }
11428
11429 /* Complain if virtual function table field not found. */
11430 if (i < TYPE_N_BASECLASSES (t))
4d3c2250 11431 complaint (&symfile_complaints,
3e43a32a
MS
11432 _("virtual function table pointer "
11433 "not found when defining class '%s'"),
4d3c2250
KB
11434 TYPE_TAG_NAME (type) ? TYPE_TAG_NAME (type) :
11435 "");
c906108c
SS
11436 }
11437 else
11438 {
11439 TYPE_VPTR_FIELDNO (type) = TYPE_VPTR_FIELDNO (t);
11440 }
11441 }
f6235d4c
EZ
11442 else if (cu->producer
11443 && strncmp (cu->producer,
11444 "IBM(R) XL C/C++ Advanced Edition", 32) == 0)
11445 {
11446 /* The IBM XLC compiler does not provide direct indication
11447 of the containing type, but the vtable pointer is
11448 always named __vfp. */
11449
11450 int i;
11451
11452 for (i = TYPE_NFIELDS (type) - 1;
11453 i >= TYPE_N_BASECLASSES (type);
11454 --i)
11455 {
11456 if (strcmp (TYPE_FIELD_NAME (type, i), "__vfp") == 0)
11457 {
11458 TYPE_VPTR_FIELDNO (type) = i;
11459 TYPE_VPTR_BASETYPE (type) = type;
11460 break;
11461 }
11462 }
11463 }
c906108c 11464 }
98751a41
JK
11465
11466 /* Copy fi.typedef_field_list linked list elements content into the
11467 allocated array TYPE_TYPEDEF_FIELD_ARRAY (type). */
11468 if (fi.typedef_field_list)
11469 {
11470 int i = fi.typedef_field_list_count;
11471
a0d7a4ff 11472 ALLOCATE_CPLUS_STRUCT_TYPE (type);
98751a41
JK
11473 TYPE_TYPEDEF_FIELD_ARRAY (type)
11474 = TYPE_ALLOC (type, sizeof (TYPE_TYPEDEF_FIELD (type, 0)) * i);
11475 TYPE_TYPEDEF_FIELD_COUNT (type) = i;
11476
11477 /* Reverse the list order to keep the debug info elements order. */
11478 while (--i >= 0)
11479 {
11480 struct typedef_field *dest, *src;
6e70227d 11481
98751a41
JK
11482 dest = &TYPE_TYPEDEF_FIELD (type, i);
11483 src = &fi.typedef_field_list->field;
11484 fi.typedef_field_list = fi.typedef_field_list->next;
11485 *dest = *src;
11486 }
11487 }
c767944b
DJ
11488
11489 do_cleanups (back_to);
eb2a6f42
TT
11490
11491 if (HAVE_CPLUS_STRUCT (type))
11492 TYPE_CPLUS_REALLY_JAVA (type) = cu->language == language_java;
c906108c 11493 }
63d06c5c 11494
bb5ed363 11495 quirk_gcc_member_function_pointer (type, objfile);
0b92b5bb 11496
90aeadfc
DC
11497 /* NOTE: carlton/2004-03-16: GCC 3.4 (or at least one of its
11498 snapshots) has been known to create a die giving a declaration
11499 for a class that has, as a child, a die giving a definition for a
11500 nested class. So we have to process our children even if the
11501 current die is a declaration. Normally, of course, a declaration
11502 won't have any children at all. */
134d01f1 11503
90aeadfc
DC
11504 while (child_die != NULL && child_die->tag)
11505 {
11506 if (child_die->tag == DW_TAG_member
11507 || child_die->tag == DW_TAG_variable
34eaf542
TT
11508 || child_die->tag == DW_TAG_inheritance
11509 || child_die->tag == DW_TAG_template_value_param
11510 || child_die->tag == DW_TAG_template_type_param)
134d01f1 11511 {
90aeadfc 11512 /* Do nothing. */
134d01f1 11513 }
90aeadfc
DC
11514 else
11515 process_die (child_die, cu);
134d01f1 11516
90aeadfc 11517 child_die = sibling_die (child_die);
134d01f1
DJ
11518 }
11519
fa4028e9
JB
11520 /* Do not consider external references. According to the DWARF standard,
11521 these DIEs are identified by the fact that they have no byte_size
11522 attribute, and a declaration attribute. */
11523 if (dwarf2_attr (die, DW_AT_byte_size, cu) != NULL
11524 || !die_is_declaration (die, cu))
c767944b 11525 new_symbol (die, type, cu);
134d01f1
DJ
11526}
11527
11528/* Given a DW_AT_enumeration_type die, set its type. We do not
11529 complete the type's fields yet, or create any symbols. */
c906108c 11530
f792889a 11531static struct type *
134d01f1 11532read_enumeration_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 11533{
e7c27a73 11534 struct objfile *objfile = cu->objfile;
c906108c 11535 struct type *type;
c906108c 11536 struct attribute *attr;
0114d602 11537 const char *name;
134d01f1 11538
348e048f
DE
11539 /* If the definition of this type lives in .debug_types, read that type.
11540 Don't follow DW_AT_specification though, that will take us back up
11541 the chain and we want to go down. */
45e58e77 11542 attr = dwarf2_attr_no_follow (die, DW_AT_signature);
348e048f
DE
11543 if (attr)
11544 {
11545 struct dwarf2_cu *type_cu = cu;
11546 struct die_info *type_die = follow_die_ref_or_sig (die, attr, &type_cu);
9a619af0 11547
348e048f 11548 type = read_type_die (type_die, type_cu);
9dc481d3
DE
11549
11550 /* TYPE_CU may not be the same as CU.
11551 Ensure TYPE is recorded in CU's type_hash table. */
348e048f
DE
11552 return set_die_type (die, type, cu);
11553 }
11554
c906108c
SS
11555 type = alloc_type (objfile);
11556
11557 TYPE_CODE (type) = TYPE_CODE_ENUM;
94af9270 11558 name = dwarf2_full_name (NULL, die, cu);
39cbfefa 11559 if (name != NULL)
0114d602 11560 TYPE_TAG_NAME (type) = (char *) name;
c906108c 11561
e142c38c 11562 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
c906108c
SS
11563 if (attr)
11564 {
11565 TYPE_LENGTH (type) = DW_UNSND (attr);
11566 }
11567 else
11568 {
11569 TYPE_LENGTH (type) = 0;
11570 }
11571
137033e9
JB
11572 /* The enumeration DIE can be incomplete. In Ada, any type can be
11573 declared as private in the package spec, and then defined only
11574 inside the package body. Such types are known as Taft Amendment
11575 Types. When another package uses such a type, an incomplete DIE
11576 may be generated by the compiler. */
02eb380e 11577 if (die_is_declaration (die, cu))
876cecd0 11578 TYPE_STUB (type) = 1;
02eb380e 11579
f792889a 11580 return set_die_type (die, type, cu);
134d01f1
DJ
11581}
11582
11583/* Given a pointer to a die which begins an enumeration, process all
11584 the dies that define the members of the enumeration, and create the
11585 symbol for the enumeration type.
11586
11587 NOTE: We reverse the order of the element list. */
11588
11589static void
11590process_enumeration_scope (struct die_info *die, struct dwarf2_cu *cu)
11591{
f792889a 11592 struct type *this_type;
134d01f1 11593
f792889a
DJ
11594 this_type = get_die_type (die, cu);
11595 if (this_type == NULL)
11596 this_type = read_enumeration_type (die, cu);
9dc481d3 11597
639d11d3 11598 if (die->child != NULL)
c906108c 11599 {
9dc481d3
DE
11600 struct die_info *child_die;
11601 struct symbol *sym;
11602 struct field *fields = NULL;
11603 int num_fields = 0;
11604 int unsigned_enum = 1;
15d034d0 11605 const char *name;
cafec441
TT
11606 int flag_enum = 1;
11607 ULONGEST mask = 0;
9dc481d3 11608
639d11d3 11609 child_die = die->child;
c906108c
SS
11610 while (child_die && child_die->tag)
11611 {
11612 if (child_die->tag != DW_TAG_enumerator)
11613 {
e7c27a73 11614 process_die (child_die, cu);
c906108c
SS
11615 }
11616 else
11617 {
39cbfefa
DJ
11618 name = dwarf2_name (child_die, cu);
11619 if (name)
c906108c 11620 {
f792889a 11621 sym = new_symbol (child_die, this_type, cu);
c906108c 11622 if (SYMBOL_VALUE (sym) < 0)
cafec441
TT
11623 {
11624 unsigned_enum = 0;
11625 flag_enum = 0;
11626 }
11627 else if ((mask & SYMBOL_VALUE (sym)) != 0)
11628 flag_enum = 0;
11629 else
11630 mask |= SYMBOL_VALUE (sym);
c906108c
SS
11631
11632 if ((num_fields % DW_FIELD_ALLOC_CHUNK) == 0)
11633 {
11634 fields = (struct field *)
11635 xrealloc (fields,
11636 (num_fields + DW_FIELD_ALLOC_CHUNK)
c5aa993b 11637 * sizeof (struct field));
c906108c
SS
11638 }
11639
3567439c 11640 FIELD_NAME (fields[num_fields]) = SYMBOL_LINKAGE_NAME (sym);
c906108c 11641 FIELD_TYPE (fields[num_fields]) = NULL;
14e75d8e 11642 SET_FIELD_ENUMVAL (fields[num_fields], SYMBOL_VALUE (sym));
c906108c
SS
11643 FIELD_BITSIZE (fields[num_fields]) = 0;
11644
11645 num_fields++;
11646 }
11647 }
11648
11649 child_die = sibling_die (child_die);
11650 }
11651
11652 if (num_fields)
11653 {
f792889a
DJ
11654 TYPE_NFIELDS (this_type) = num_fields;
11655 TYPE_FIELDS (this_type) = (struct field *)
11656 TYPE_ALLOC (this_type, sizeof (struct field) * num_fields);
11657 memcpy (TYPE_FIELDS (this_type), fields,
c906108c 11658 sizeof (struct field) * num_fields);
b8c9b27d 11659 xfree (fields);
c906108c
SS
11660 }
11661 if (unsigned_enum)
876cecd0 11662 TYPE_UNSIGNED (this_type) = 1;
cafec441
TT
11663 if (flag_enum)
11664 TYPE_FLAG_ENUM (this_type) = 1;
c906108c 11665 }
134d01f1 11666
6c83ed52
TT
11667 /* If we are reading an enum from a .debug_types unit, and the enum
11668 is a declaration, and the enum is not the signatured type in the
11669 unit, then we do not want to add a symbol for it. Adding a
11670 symbol would in some cases obscure the true definition of the
11671 enum, giving users an incomplete type when the definition is
11672 actually available. Note that we do not want to do this for all
11673 enums which are just declarations, because C++0x allows forward
11674 enum declarations. */
3019eac3 11675 if (cu->per_cu->is_debug_types
6c83ed52
TT
11676 && die_is_declaration (die, cu))
11677 {
52dc124a 11678 struct signatured_type *sig_type;
6c83ed52 11679
52dc124a 11680 sig_type
6c83ed52 11681 = lookup_signatured_type_at_offset (dwarf2_per_objfile->objfile,
3019eac3 11682 cu->per_cu->info_or_types_section,
6c83ed52 11683 cu->per_cu->offset);
3019eac3
DE
11684 gdb_assert (sig_type->type_offset_in_section.sect_off != 0);
11685 if (sig_type->type_offset_in_section.sect_off != die->offset.sect_off)
6c83ed52
TT
11686 return;
11687 }
11688
f792889a 11689 new_symbol (die, this_type, cu);
c906108c
SS
11690}
11691
11692/* Extract all information from a DW_TAG_array_type DIE and put it in
11693 the DIE's type field. For now, this only handles one dimensional
11694 arrays. */
11695
f792889a 11696static struct type *
e7c27a73 11697read_array_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 11698{
e7c27a73 11699 struct objfile *objfile = cu->objfile;
c906108c 11700 struct die_info *child_die;
7e314c57 11701 struct type *type;
c906108c
SS
11702 struct type *element_type, *range_type, *index_type;
11703 struct type **range_types = NULL;
11704 struct attribute *attr;
11705 int ndim = 0;
11706 struct cleanup *back_to;
15d034d0 11707 const char *name;
c906108c 11708
e7c27a73 11709 element_type = die_type (die, cu);
c906108c 11710
7e314c57
JK
11711 /* The die_type call above may have already set the type for this DIE. */
11712 type = get_die_type (die, cu);
11713 if (type)
11714 return type;
11715
c906108c
SS
11716 /* Irix 6.2 native cc creates array types without children for
11717 arrays with unspecified length. */
639d11d3 11718 if (die->child == NULL)
c906108c 11719 {
46bf5051 11720 index_type = objfile_type (objfile)->builtin_int;
c906108c 11721 range_type = create_range_type (NULL, index_type, 0, -1);
f792889a
DJ
11722 type = create_array_type (NULL, element_type, range_type);
11723 return set_die_type (die, type, cu);
c906108c
SS
11724 }
11725
11726 back_to = make_cleanup (null_cleanup, NULL);
639d11d3 11727 child_die = die->child;
c906108c
SS
11728 while (child_die && child_die->tag)
11729 {
11730 if (child_die->tag == DW_TAG_subrange_type)
11731 {
f792889a 11732 struct type *child_type = read_type_die (child_die, cu);
9a619af0 11733
f792889a 11734 if (child_type != NULL)
a02abb62 11735 {
0963b4bd
MS
11736 /* The range type was succesfully read. Save it for the
11737 array type creation. */
a02abb62
JB
11738 if ((ndim % DW_FIELD_ALLOC_CHUNK) == 0)
11739 {
11740 range_types = (struct type **)
11741 xrealloc (range_types, (ndim + DW_FIELD_ALLOC_CHUNK)
11742 * sizeof (struct type *));
11743 if (ndim == 0)
11744 make_cleanup (free_current_contents, &range_types);
11745 }
f792889a 11746 range_types[ndim++] = child_type;
a02abb62 11747 }
c906108c
SS
11748 }
11749 child_die = sibling_die (child_die);
11750 }
11751
11752 /* Dwarf2 dimensions are output from left to right, create the
11753 necessary array types in backwards order. */
7ca2d3a3 11754
c906108c 11755 type = element_type;
7ca2d3a3
DL
11756
11757 if (read_array_order (die, cu) == DW_ORD_col_major)
11758 {
11759 int i = 0;
9a619af0 11760
7ca2d3a3
DL
11761 while (i < ndim)
11762 type = create_array_type (NULL, type, range_types[i++]);
11763 }
11764 else
11765 {
11766 while (ndim-- > 0)
11767 type = create_array_type (NULL, type, range_types[ndim]);
11768 }
c906108c 11769
f5f8a009
EZ
11770 /* Understand Dwarf2 support for vector types (like they occur on
11771 the PowerPC w/ AltiVec). Gcc just adds another attribute to the
11772 array type. This is not part of the Dwarf2/3 standard yet, but a
11773 custom vendor extension. The main difference between a regular
11774 array and the vector variant is that vectors are passed by value
11775 to functions. */
e142c38c 11776 attr = dwarf2_attr (die, DW_AT_GNU_vector, cu);
f5f8a009 11777 if (attr)
ea37ba09 11778 make_vector_type (type);
f5f8a009 11779
dbc98a8b
KW
11780 /* The DIE may have DW_AT_byte_size set. For example an OpenCL
11781 implementation may choose to implement triple vectors using this
11782 attribute. */
11783 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
11784 if (attr)
11785 {
11786 if (DW_UNSND (attr) >= TYPE_LENGTH (type))
11787 TYPE_LENGTH (type) = DW_UNSND (attr);
11788 else
3e43a32a
MS
11789 complaint (&symfile_complaints,
11790 _("DW_AT_byte_size for array type smaller "
11791 "than the total size of elements"));
dbc98a8b
KW
11792 }
11793
39cbfefa
DJ
11794 name = dwarf2_name (die, cu);
11795 if (name)
11796 TYPE_NAME (type) = name;
6e70227d 11797
0963b4bd 11798 /* Install the type in the die. */
7e314c57
JK
11799 set_die_type (die, type, cu);
11800
11801 /* set_die_type should be already done. */
b4ba55a1
JB
11802 set_descriptive_type (type, die, cu);
11803
c906108c
SS
11804 do_cleanups (back_to);
11805
7e314c57 11806 return type;
c906108c
SS
11807}
11808
7ca2d3a3 11809static enum dwarf_array_dim_ordering
6e70227d 11810read_array_order (struct die_info *die, struct dwarf2_cu *cu)
7ca2d3a3
DL
11811{
11812 struct attribute *attr;
11813
11814 attr = dwarf2_attr (die, DW_AT_ordering, cu);
11815
11816 if (attr) return DW_SND (attr);
11817
0963b4bd
MS
11818 /* GNU F77 is a special case, as at 08/2004 array type info is the
11819 opposite order to the dwarf2 specification, but data is still
11820 laid out as per normal fortran.
7ca2d3a3 11821
0963b4bd
MS
11822 FIXME: dsl/2004-8-20: If G77 is ever fixed, this will also need
11823 version checking. */
7ca2d3a3 11824
905e0470
PM
11825 if (cu->language == language_fortran
11826 && cu->producer && strstr (cu->producer, "GNU F77"))
7ca2d3a3
DL
11827 {
11828 return DW_ORD_row_major;
11829 }
11830
6e70227d 11831 switch (cu->language_defn->la_array_ordering)
7ca2d3a3
DL
11832 {
11833 case array_column_major:
11834 return DW_ORD_col_major;
11835 case array_row_major:
11836 default:
11837 return DW_ORD_row_major;
11838 };
11839}
11840
72019c9c 11841/* Extract all information from a DW_TAG_set_type DIE and put it in
0963b4bd 11842 the DIE's type field. */
72019c9c 11843
f792889a 11844static struct type *
72019c9c
GM
11845read_set_type (struct die_info *die, struct dwarf2_cu *cu)
11846{
7e314c57
JK
11847 struct type *domain_type, *set_type;
11848 struct attribute *attr;
f792889a 11849
7e314c57
JK
11850 domain_type = die_type (die, cu);
11851
11852 /* The die_type call above may have already set the type for this DIE. */
11853 set_type = get_die_type (die, cu);
11854 if (set_type)
11855 return set_type;
11856
11857 set_type = create_set_type (NULL, domain_type);
11858
11859 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
d09039dd
PM
11860 if (attr)
11861 TYPE_LENGTH (set_type) = DW_UNSND (attr);
7e314c57 11862
f792889a 11863 return set_die_type (die, set_type, cu);
72019c9c 11864}
7ca2d3a3 11865
0971de02
TT
11866/* A helper for read_common_block that creates a locexpr baton.
11867 SYM is the symbol which we are marking as computed.
11868 COMMON_DIE is the DIE for the common block.
11869 COMMON_LOC is the location expression attribute for the common
11870 block itself.
11871 MEMBER_LOC is the location expression attribute for the particular
11872 member of the common block that we are processing.
11873 CU is the CU from which the above come. */
11874
11875static void
11876mark_common_block_symbol_computed (struct symbol *sym,
11877 struct die_info *common_die,
11878 struct attribute *common_loc,
11879 struct attribute *member_loc,
11880 struct dwarf2_cu *cu)
11881{
11882 struct objfile *objfile = dwarf2_per_objfile->objfile;
11883 struct dwarf2_locexpr_baton *baton;
11884 gdb_byte *ptr;
11885 unsigned int cu_off;
11886 enum bfd_endian byte_order = gdbarch_byte_order (get_objfile_arch (objfile));
11887 LONGEST offset = 0;
11888
11889 gdb_assert (common_loc && member_loc);
11890 gdb_assert (attr_form_is_block (common_loc));
11891 gdb_assert (attr_form_is_block (member_loc)
11892 || attr_form_is_constant (member_loc));
11893
11894 baton = obstack_alloc (&objfile->objfile_obstack,
11895 sizeof (struct dwarf2_locexpr_baton));
11896 baton->per_cu = cu->per_cu;
11897 gdb_assert (baton->per_cu);
11898
11899 baton->size = 5 /* DW_OP_call4 */ + 1 /* DW_OP_plus */;
11900
11901 if (attr_form_is_constant (member_loc))
11902 {
11903 offset = dwarf2_get_attr_constant_value (member_loc, 0);
11904 baton->size += 1 /* DW_OP_addr */ + cu->header.addr_size;
11905 }
11906 else
11907 baton->size += DW_BLOCK (member_loc)->size;
11908
11909 ptr = obstack_alloc (&objfile->objfile_obstack, baton->size);
11910 baton->data = ptr;
11911
11912 *ptr++ = DW_OP_call4;
11913 cu_off = common_die->offset.sect_off - cu->per_cu->offset.sect_off;
11914 store_unsigned_integer (ptr, 4, byte_order, cu_off);
11915 ptr += 4;
11916
11917 if (attr_form_is_constant (member_loc))
11918 {
11919 *ptr++ = DW_OP_addr;
11920 store_unsigned_integer (ptr, cu->header.addr_size, byte_order, offset);
11921 ptr += cu->header.addr_size;
11922 }
11923 else
11924 {
11925 /* We have to copy the data here, because DW_OP_call4 will only
11926 use a DW_AT_location attribute. */
11927 memcpy (ptr, DW_BLOCK (member_loc)->data, DW_BLOCK (member_loc)->size);
11928 ptr += DW_BLOCK (member_loc)->size;
11929 }
11930
11931 *ptr++ = DW_OP_plus;
11932 gdb_assert (ptr - baton->data == baton->size);
11933
11934 SYMBOL_COMPUTED_OPS (sym) = &dwarf2_locexpr_funcs;
11935 SYMBOL_LOCATION_BATON (sym) = baton;
11936 SYMBOL_CLASS (sym) = LOC_COMPUTED;
11937}
11938
4357ac6c
TT
11939/* Create appropriate locally-scoped variables for all the
11940 DW_TAG_common_block entries. Also create a struct common_block
11941 listing all such variables for `info common'. COMMON_BLOCK_DOMAIN
11942 is used to sepate the common blocks name namespace from regular
11943 variable names. */
c906108c
SS
11944
11945static void
e7c27a73 11946read_common_block (struct die_info *die, struct dwarf2_cu *cu)
c906108c 11947{
0971de02
TT
11948 struct attribute *attr;
11949
11950 attr = dwarf2_attr (die, DW_AT_location, cu);
11951 if (attr)
11952 {
11953 /* Support the .debug_loc offsets. */
11954 if (attr_form_is_block (attr))
11955 {
11956 /* Ok. */
11957 }
11958 else if (attr_form_is_section_offset (attr))
11959 {
11960 dwarf2_complex_location_expr_complaint ();
11961 attr = NULL;
11962 }
11963 else
11964 {
11965 dwarf2_invalid_attrib_class_complaint ("DW_AT_location",
11966 "common block member");
11967 attr = NULL;
11968 }
11969 }
11970
639d11d3 11971 if (die->child != NULL)
c906108c 11972 {
4357ac6c
TT
11973 struct objfile *objfile = cu->objfile;
11974 struct die_info *child_die;
11975 size_t n_entries = 0, size;
11976 struct common_block *common_block;
11977 struct symbol *sym;
74ac6d43 11978
4357ac6c
TT
11979 for (child_die = die->child;
11980 child_die && child_die->tag;
11981 child_die = sibling_die (child_die))
11982 ++n_entries;
11983
11984 size = (sizeof (struct common_block)
11985 + (n_entries - 1) * sizeof (struct symbol *));
11986 common_block = obstack_alloc (&objfile->objfile_obstack, size);
11987 memset (common_block->contents, 0, n_entries * sizeof (struct symbol *));
11988 common_block->n_entries = 0;
11989
11990 for (child_die = die->child;
11991 child_die && child_die->tag;
11992 child_die = sibling_die (child_die))
11993 {
11994 /* Create the symbol in the DW_TAG_common_block block in the current
11995 symbol scope. */
e7c27a73 11996 sym = new_symbol (child_die, NULL, cu);
0971de02
TT
11997 if (sym != NULL)
11998 {
11999 struct attribute *member_loc;
12000
12001 common_block->contents[common_block->n_entries++] = sym;
12002
12003 member_loc = dwarf2_attr (child_die, DW_AT_data_member_location,
12004 cu);
12005 if (member_loc)
12006 {
12007 /* GDB has handled this for a long time, but it is
12008 not specified by DWARF. It seems to have been
12009 emitted by gfortran at least as recently as:
12010 http://gcc.gnu.org/bugzilla/show_bug.cgi?id=23057. */
12011 complaint (&symfile_complaints,
12012 _("Variable in common block has "
12013 "DW_AT_data_member_location "
12014 "- DIE at 0x%x [in module %s]"),
12015 child_die->offset.sect_off, cu->objfile->name);
12016
12017 if (attr_form_is_section_offset (member_loc))
12018 dwarf2_complex_location_expr_complaint ();
12019 else if (attr_form_is_constant (member_loc)
12020 || attr_form_is_block (member_loc))
12021 {
12022 if (attr)
12023 mark_common_block_symbol_computed (sym, die, attr,
12024 member_loc, cu);
12025 }
12026 else
12027 dwarf2_complex_location_expr_complaint ();
12028 }
12029 }
c906108c 12030 }
4357ac6c
TT
12031
12032 sym = new_symbol (die, objfile_type (objfile)->builtin_void, cu);
12033 SYMBOL_VALUE_COMMON_BLOCK (sym) = common_block;
c906108c
SS
12034 }
12035}
12036
0114d602 12037/* Create a type for a C++ namespace. */
d9fa45fe 12038
0114d602
DJ
12039static struct type *
12040read_namespace_type (struct die_info *die, struct dwarf2_cu *cu)
d9fa45fe 12041{
e7c27a73 12042 struct objfile *objfile = cu->objfile;
0114d602 12043 const char *previous_prefix, *name;
9219021c 12044 int is_anonymous;
0114d602
DJ
12045 struct type *type;
12046
12047 /* For extensions, reuse the type of the original namespace. */
12048 if (dwarf2_attr (die, DW_AT_extension, cu) != NULL)
12049 {
12050 struct die_info *ext_die;
12051 struct dwarf2_cu *ext_cu = cu;
9a619af0 12052
0114d602
DJ
12053 ext_die = dwarf2_extension (die, &ext_cu);
12054 type = read_type_die (ext_die, ext_cu);
9dc481d3
DE
12055
12056 /* EXT_CU may not be the same as CU.
12057 Ensure TYPE is recorded in CU's type_hash table. */
0114d602
DJ
12058 return set_die_type (die, type, cu);
12059 }
9219021c 12060
e142c38c 12061 name = namespace_name (die, &is_anonymous, cu);
9219021c
DC
12062
12063 /* Now build the name of the current namespace. */
12064
0114d602
DJ
12065 previous_prefix = determine_prefix (die, cu);
12066 if (previous_prefix[0] != '\0')
12067 name = typename_concat (&objfile->objfile_obstack,
f55ee35c 12068 previous_prefix, name, 0, cu);
0114d602
DJ
12069
12070 /* Create the type. */
12071 type = init_type (TYPE_CODE_NAMESPACE, 0, 0, NULL,
12072 objfile);
12073 TYPE_NAME (type) = (char *) name;
12074 TYPE_TAG_NAME (type) = TYPE_NAME (type);
12075
60531b24 12076 return set_die_type (die, type, cu);
0114d602
DJ
12077}
12078
12079/* Read a C++ namespace. */
12080
12081static void
12082read_namespace (struct die_info *die, struct dwarf2_cu *cu)
12083{
12084 struct objfile *objfile = cu->objfile;
0114d602 12085 int is_anonymous;
9219021c 12086
5c4e30ca
DC
12087 /* Add a symbol associated to this if we haven't seen the namespace
12088 before. Also, add a using directive if it's an anonymous
12089 namespace. */
9219021c 12090
f2f0e013 12091 if (dwarf2_attr (die, DW_AT_extension, cu) == NULL)
5c4e30ca
DC
12092 {
12093 struct type *type;
12094
0114d602 12095 type = read_type_die (die, cu);
e7c27a73 12096 new_symbol (die, type, cu);
5c4e30ca 12097
e8e80198 12098 namespace_name (die, &is_anonymous, cu);
5c4e30ca 12099 if (is_anonymous)
0114d602
DJ
12100 {
12101 const char *previous_prefix = determine_prefix (die, cu);
9a619af0 12102
c0cc3a76 12103 cp_add_using_directive (previous_prefix, TYPE_NAME (type), NULL,
12aaed36 12104 NULL, NULL, 0, &objfile->objfile_obstack);
0114d602 12105 }
5c4e30ca 12106 }
9219021c 12107
639d11d3 12108 if (die->child != NULL)
d9fa45fe 12109 {
639d11d3 12110 struct die_info *child_die = die->child;
6e70227d 12111
d9fa45fe
DC
12112 while (child_die && child_die->tag)
12113 {
e7c27a73 12114 process_die (child_die, cu);
d9fa45fe
DC
12115 child_die = sibling_die (child_die);
12116 }
12117 }
38d518c9
EZ
12118}
12119
f55ee35c
JK
12120/* Read a Fortran module as type. This DIE can be only a declaration used for
12121 imported module. Still we need that type as local Fortran "use ... only"
12122 declaration imports depend on the created type in determine_prefix. */
12123
12124static struct type *
12125read_module_type (struct die_info *die, struct dwarf2_cu *cu)
12126{
12127 struct objfile *objfile = cu->objfile;
15d034d0 12128 const char *module_name;
f55ee35c
JK
12129 struct type *type;
12130
12131 module_name = dwarf2_name (die, cu);
12132 if (!module_name)
3e43a32a
MS
12133 complaint (&symfile_complaints,
12134 _("DW_TAG_module has no name, offset 0x%x"),
b64f50a1 12135 die->offset.sect_off);
f55ee35c
JK
12136 type = init_type (TYPE_CODE_MODULE, 0, 0, module_name, objfile);
12137
12138 /* determine_prefix uses TYPE_TAG_NAME. */
12139 TYPE_TAG_NAME (type) = TYPE_NAME (type);
12140
12141 return set_die_type (die, type, cu);
12142}
12143
5d7cb8df
JK
12144/* Read a Fortran module. */
12145
12146static void
12147read_module (struct die_info *die, struct dwarf2_cu *cu)
12148{
12149 struct die_info *child_die = die->child;
12150
5d7cb8df
JK
12151 while (child_die && child_die->tag)
12152 {
12153 process_die (child_die, cu);
12154 child_die = sibling_die (child_die);
12155 }
12156}
12157
38d518c9
EZ
12158/* Return the name of the namespace represented by DIE. Set
12159 *IS_ANONYMOUS to tell whether or not the namespace is an anonymous
12160 namespace. */
12161
12162static const char *
e142c38c 12163namespace_name (struct die_info *die, int *is_anonymous, struct dwarf2_cu *cu)
38d518c9
EZ
12164{
12165 struct die_info *current_die;
12166 const char *name = NULL;
12167
12168 /* Loop through the extensions until we find a name. */
12169
12170 for (current_die = die;
12171 current_die != NULL;
f2f0e013 12172 current_die = dwarf2_extension (die, &cu))
38d518c9 12173 {
e142c38c 12174 name = dwarf2_name (current_die, cu);
38d518c9
EZ
12175 if (name != NULL)
12176 break;
12177 }
12178
12179 /* Is it an anonymous namespace? */
12180
12181 *is_anonymous = (name == NULL);
12182 if (*is_anonymous)
2b1dbab0 12183 name = CP_ANONYMOUS_NAMESPACE_STR;
38d518c9
EZ
12184
12185 return name;
d9fa45fe
DC
12186}
12187
c906108c
SS
12188/* Extract all information from a DW_TAG_pointer_type DIE and add to
12189 the user defined type vector. */
12190
f792889a 12191static struct type *
e7c27a73 12192read_tag_pointer_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 12193{
5e2b427d 12194 struct gdbarch *gdbarch = get_objfile_arch (cu->objfile);
e7c27a73 12195 struct comp_unit_head *cu_header = &cu->header;
c906108c 12196 struct type *type;
8b2dbe47
KB
12197 struct attribute *attr_byte_size;
12198 struct attribute *attr_address_class;
12199 int byte_size, addr_class;
7e314c57
JK
12200 struct type *target_type;
12201
12202 target_type = die_type (die, cu);
c906108c 12203
7e314c57
JK
12204 /* The die_type call above may have already set the type for this DIE. */
12205 type = get_die_type (die, cu);
12206 if (type)
12207 return type;
12208
12209 type = lookup_pointer_type (target_type);
8b2dbe47 12210
e142c38c 12211 attr_byte_size = dwarf2_attr (die, DW_AT_byte_size, cu);
8b2dbe47
KB
12212 if (attr_byte_size)
12213 byte_size = DW_UNSND (attr_byte_size);
c906108c 12214 else
8b2dbe47
KB
12215 byte_size = cu_header->addr_size;
12216
e142c38c 12217 attr_address_class = dwarf2_attr (die, DW_AT_address_class, cu);
8b2dbe47
KB
12218 if (attr_address_class)
12219 addr_class = DW_UNSND (attr_address_class);
12220 else
12221 addr_class = DW_ADDR_none;
12222
12223 /* If the pointer size or address class is different than the
12224 default, create a type variant marked as such and set the
12225 length accordingly. */
12226 if (TYPE_LENGTH (type) != byte_size || addr_class != DW_ADDR_none)
c906108c 12227 {
5e2b427d 12228 if (gdbarch_address_class_type_flags_p (gdbarch))
8b2dbe47
KB
12229 {
12230 int type_flags;
12231
849957d9 12232 type_flags = gdbarch_address_class_type_flags
5e2b427d 12233 (gdbarch, byte_size, addr_class);
876cecd0
TT
12234 gdb_assert ((type_flags & ~TYPE_INSTANCE_FLAG_ADDRESS_CLASS_ALL)
12235 == 0);
8b2dbe47
KB
12236 type = make_type_with_address_space (type, type_flags);
12237 }
12238 else if (TYPE_LENGTH (type) != byte_size)
12239 {
3e43a32a
MS
12240 complaint (&symfile_complaints,
12241 _("invalid pointer size %d"), byte_size);
8b2dbe47 12242 }
6e70227d 12243 else
9a619af0
MS
12244 {
12245 /* Should we also complain about unhandled address classes? */
12246 }
c906108c 12247 }
8b2dbe47
KB
12248
12249 TYPE_LENGTH (type) = byte_size;
f792889a 12250 return set_die_type (die, type, cu);
c906108c
SS
12251}
12252
12253/* Extract all information from a DW_TAG_ptr_to_member_type DIE and add to
12254 the user defined type vector. */
12255
f792889a 12256static struct type *
e7c27a73 12257read_tag_ptr_to_member_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c
SS
12258{
12259 struct type *type;
12260 struct type *to_type;
12261 struct type *domain;
12262
e7c27a73
DJ
12263 to_type = die_type (die, cu);
12264 domain = die_containing_type (die, cu);
0d5de010 12265
7e314c57
JK
12266 /* The calls above may have already set the type for this DIE. */
12267 type = get_die_type (die, cu);
12268 if (type)
12269 return type;
12270
0d5de010
DJ
12271 if (TYPE_CODE (check_typedef (to_type)) == TYPE_CODE_METHOD)
12272 type = lookup_methodptr_type (to_type);
12273 else
12274 type = lookup_memberptr_type (to_type, domain);
c906108c 12275
f792889a 12276 return set_die_type (die, type, cu);
c906108c
SS
12277}
12278
12279/* Extract all information from a DW_TAG_reference_type DIE and add to
12280 the user defined type vector. */
12281
f792889a 12282static struct type *
e7c27a73 12283read_tag_reference_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 12284{
e7c27a73 12285 struct comp_unit_head *cu_header = &cu->header;
7e314c57 12286 struct type *type, *target_type;
c906108c
SS
12287 struct attribute *attr;
12288
7e314c57
JK
12289 target_type = die_type (die, cu);
12290
12291 /* The die_type call above may have already set the type for this DIE. */
12292 type = get_die_type (die, cu);
12293 if (type)
12294 return type;
12295
12296 type = lookup_reference_type (target_type);
e142c38c 12297 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
c906108c
SS
12298 if (attr)
12299 {
12300 TYPE_LENGTH (type) = DW_UNSND (attr);
12301 }
12302 else
12303 {
107d2387 12304 TYPE_LENGTH (type) = cu_header->addr_size;
c906108c 12305 }
f792889a 12306 return set_die_type (die, type, cu);
c906108c
SS
12307}
12308
f792889a 12309static struct type *
e7c27a73 12310read_tag_const_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 12311{
f792889a 12312 struct type *base_type, *cv_type;
c906108c 12313
e7c27a73 12314 base_type = die_type (die, cu);
7e314c57
JK
12315
12316 /* The die_type call above may have already set the type for this DIE. */
12317 cv_type = get_die_type (die, cu);
12318 if (cv_type)
12319 return cv_type;
12320
2f608a3a
KW
12321 /* In case the const qualifier is applied to an array type, the element type
12322 is so qualified, not the array type (section 6.7.3 of C99). */
12323 if (TYPE_CODE (base_type) == TYPE_CODE_ARRAY)
12324 {
12325 struct type *el_type, *inner_array;
12326
12327 base_type = copy_type (base_type);
12328 inner_array = base_type;
12329
12330 while (TYPE_CODE (TYPE_TARGET_TYPE (inner_array)) == TYPE_CODE_ARRAY)
12331 {
12332 TYPE_TARGET_TYPE (inner_array) =
12333 copy_type (TYPE_TARGET_TYPE (inner_array));
12334 inner_array = TYPE_TARGET_TYPE (inner_array);
12335 }
12336
12337 el_type = TYPE_TARGET_TYPE (inner_array);
12338 TYPE_TARGET_TYPE (inner_array) =
12339 make_cv_type (1, TYPE_VOLATILE (el_type), el_type, NULL);
12340
12341 return set_die_type (die, base_type, cu);
12342 }
12343
f792889a
DJ
12344 cv_type = make_cv_type (1, TYPE_VOLATILE (base_type), base_type, 0);
12345 return set_die_type (die, cv_type, cu);
c906108c
SS
12346}
12347
f792889a 12348static struct type *
e7c27a73 12349read_tag_volatile_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 12350{
f792889a 12351 struct type *base_type, *cv_type;
c906108c 12352
e7c27a73 12353 base_type = die_type (die, cu);
7e314c57
JK
12354
12355 /* The die_type call above may have already set the type for this DIE. */
12356 cv_type = get_die_type (die, cu);
12357 if (cv_type)
12358 return cv_type;
12359
f792889a
DJ
12360 cv_type = make_cv_type (TYPE_CONST (base_type), 1, base_type, 0);
12361 return set_die_type (die, cv_type, cu);
c906108c
SS
12362}
12363
06d66ee9
TT
12364/* Handle DW_TAG_restrict_type. */
12365
12366static struct type *
12367read_tag_restrict_type (struct die_info *die, struct dwarf2_cu *cu)
12368{
12369 struct type *base_type, *cv_type;
12370
12371 base_type = die_type (die, cu);
12372
12373 /* The die_type call above may have already set the type for this DIE. */
12374 cv_type = get_die_type (die, cu);
12375 if (cv_type)
12376 return cv_type;
12377
12378 cv_type = make_restrict_type (base_type);
12379 return set_die_type (die, cv_type, cu);
12380}
12381
c906108c
SS
12382/* Extract all information from a DW_TAG_string_type DIE and add to
12383 the user defined type vector. It isn't really a user defined type,
12384 but it behaves like one, with other DIE's using an AT_user_def_type
12385 attribute to reference it. */
12386
f792889a 12387static struct type *
e7c27a73 12388read_tag_string_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 12389{
e7c27a73 12390 struct objfile *objfile = cu->objfile;
3b7538c0 12391 struct gdbarch *gdbarch = get_objfile_arch (objfile);
c906108c
SS
12392 struct type *type, *range_type, *index_type, *char_type;
12393 struct attribute *attr;
12394 unsigned int length;
12395
e142c38c 12396 attr = dwarf2_attr (die, DW_AT_string_length, cu);
c906108c
SS
12397 if (attr)
12398 {
12399 length = DW_UNSND (attr);
12400 }
12401 else
12402 {
0963b4bd 12403 /* Check for the DW_AT_byte_size attribute. */
e142c38c 12404 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
b21b22e0
PS
12405 if (attr)
12406 {
12407 length = DW_UNSND (attr);
12408 }
12409 else
12410 {
12411 length = 1;
12412 }
c906108c 12413 }
6ccb9162 12414
46bf5051 12415 index_type = objfile_type (objfile)->builtin_int;
c906108c 12416 range_type = create_range_type (NULL, index_type, 1, length);
3b7538c0
UW
12417 char_type = language_string_char_type (cu->language_defn, gdbarch);
12418 type = create_string_type (NULL, char_type, range_type);
6ccb9162 12419
f792889a 12420 return set_die_type (die, type, cu);
c906108c
SS
12421}
12422
12423/* Handle DIES due to C code like:
12424
12425 struct foo
c5aa993b
JM
12426 {
12427 int (*funcp)(int a, long l);
12428 int b;
12429 };
c906108c 12430
0963b4bd 12431 ('funcp' generates a DW_TAG_subroutine_type DIE). */
c906108c 12432
f792889a 12433static struct type *
e7c27a73 12434read_subroutine_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 12435{
bb5ed363 12436 struct objfile *objfile = cu->objfile;
0963b4bd
MS
12437 struct type *type; /* Type that this function returns. */
12438 struct type *ftype; /* Function that returns above type. */
c906108c
SS
12439 struct attribute *attr;
12440
e7c27a73 12441 type = die_type (die, cu);
7e314c57
JK
12442
12443 /* The die_type call above may have already set the type for this DIE. */
12444 ftype = get_die_type (die, cu);
12445 if (ftype)
12446 return ftype;
12447
0c8b41f1 12448 ftype = lookup_function_type (type);
c906108c 12449
5b8101ae 12450 /* All functions in C++, Pascal and Java have prototypes. */
e142c38c 12451 attr = dwarf2_attr (die, DW_AT_prototyped, cu);
c906108c 12452 if ((attr && (DW_UNSND (attr) != 0))
987504bb 12453 || cu->language == language_cplus
5b8101ae
PM
12454 || cu->language == language_java
12455 || cu->language == language_pascal)
876cecd0 12456 TYPE_PROTOTYPED (ftype) = 1;
a6c727b2
DJ
12457 else if (producer_is_realview (cu->producer))
12458 /* RealView does not emit DW_AT_prototyped. We can not
12459 distinguish prototyped and unprototyped functions; default to
12460 prototyped, since that is more common in modern code (and
12461 RealView warns about unprototyped functions). */
12462 TYPE_PROTOTYPED (ftype) = 1;
c906108c 12463
c055b101
CV
12464 /* Store the calling convention in the type if it's available in
12465 the subroutine die. Otherwise set the calling convention to
12466 the default value DW_CC_normal. */
12467 attr = dwarf2_attr (die, DW_AT_calling_convention, cu);
54fcddd0
UW
12468 if (attr)
12469 TYPE_CALLING_CONVENTION (ftype) = DW_UNSND (attr);
12470 else if (cu->producer && strstr (cu->producer, "IBM XL C for OpenCL"))
12471 TYPE_CALLING_CONVENTION (ftype) = DW_CC_GDB_IBM_OpenCL;
12472 else
12473 TYPE_CALLING_CONVENTION (ftype) = DW_CC_normal;
76c10ea2
GM
12474
12475 /* We need to add the subroutine type to the die immediately so
12476 we don't infinitely recurse when dealing with parameters
0963b4bd 12477 declared as the same subroutine type. */
76c10ea2 12478 set_die_type (die, ftype, cu);
6e70227d 12479
639d11d3 12480 if (die->child != NULL)
c906108c 12481 {
bb5ed363 12482 struct type *void_type = objfile_type (objfile)->builtin_void;
c906108c 12483 struct die_info *child_die;
8072405b 12484 int nparams, iparams;
c906108c
SS
12485
12486 /* Count the number of parameters.
12487 FIXME: GDB currently ignores vararg functions, but knows about
12488 vararg member functions. */
8072405b 12489 nparams = 0;
639d11d3 12490 child_die = die->child;
c906108c
SS
12491 while (child_die && child_die->tag)
12492 {
12493 if (child_die->tag == DW_TAG_formal_parameter)
12494 nparams++;
12495 else if (child_die->tag == DW_TAG_unspecified_parameters)
876cecd0 12496 TYPE_VARARGS (ftype) = 1;
c906108c
SS
12497 child_die = sibling_die (child_die);
12498 }
12499
12500 /* Allocate storage for parameters and fill them in. */
12501 TYPE_NFIELDS (ftype) = nparams;
12502 TYPE_FIELDS (ftype) = (struct field *)
ae5a43e0 12503 TYPE_ZALLOC (ftype, nparams * sizeof (struct field));
c906108c 12504
8072405b
JK
12505 /* TYPE_FIELD_TYPE must never be NULL. Pre-fill the array to ensure it
12506 even if we error out during the parameters reading below. */
12507 for (iparams = 0; iparams < nparams; iparams++)
12508 TYPE_FIELD_TYPE (ftype, iparams) = void_type;
12509
12510 iparams = 0;
639d11d3 12511 child_die = die->child;
c906108c
SS
12512 while (child_die && child_die->tag)
12513 {
12514 if (child_die->tag == DW_TAG_formal_parameter)
12515 {
3ce3b1ba
PA
12516 struct type *arg_type;
12517
12518 /* DWARF version 2 has no clean way to discern C++
12519 static and non-static member functions. G++ helps
12520 GDB by marking the first parameter for non-static
12521 member functions (which is the this pointer) as
12522 artificial. We pass this information to
12523 dwarf2_add_member_fn via TYPE_FIELD_ARTIFICIAL.
12524
12525 DWARF version 3 added DW_AT_object_pointer, which GCC
12526 4.5 does not yet generate. */
e142c38c 12527 attr = dwarf2_attr (child_die, DW_AT_artificial, cu);
c906108c
SS
12528 if (attr)
12529 TYPE_FIELD_ARTIFICIAL (ftype, iparams) = DW_UNSND (attr);
12530 else
418835cc
KS
12531 {
12532 TYPE_FIELD_ARTIFICIAL (ftype, iparams) = 0;
12533
12534 /* GCC/43521: In java, the formal parameter
12535 "this" is sometimes not marked with DW_AT_artificial. */
12536 if (cu->language == language_java)
12537 {
12538 const char *name = dwarf2_name (child_die, cu);
9a619af0 12539
418835cc
KS
12540 if (name && !strcmp (name, "this"))
12541 TYPE_FIELD_ARTIFICIAL (ftype, iparams) = 1;
12542 }
12543 }
3ce3b1ba
PA
12544 arg_type = die_type (child_die, cu);
12545
12546 /* RealView does not mark THIS as const, which the testsuite
12547 expects. GCC marks THIS as const in method definitions,
12548 but not in the class specifications (GCC PR 43053). */
12549 if (cu->language == language_cplus && !TYPE_CONST (arg_type)
12550 && TYPE_FIELD_ARTIFICIAL (ftype, iparams))
12551 {
12552 int is_this = 0;
12553 struct dwarf2_cu *arg_cu = cu;
12554 const char *name = dwarf2_name (child_die, cu);
12555
12556 attr = dwarf2_attr (die, DW_AT_object_pointer, cu);
12557 if (attr)
12558 {
12559 /* If the compiler emits this, use it. */
12560 if (follow_die_ref (die, attr, &arg_cu) == child_die)
12561 is_this = 1;
12562 }
12563 else if (name && strcmp (name, "this") == 0)
12564 /* Function definitions will have the argument names. */
12565 is_this = 1;
12566 else if (name == NULL && iparams == 0)
12567 /* Declarations may not have the names, so like
12568 elsewhere in GDB, assume an artificial first
12569 argument is "this". */
12570 is_this = 1;
12571
12572 if (is_this)
12573 arg_type = make_cv_type (1, TYPE_VOLATILE (arg_type),
12574 arg_type, 0);
12575 }
12576
12577 TYPE_FIELD_TYPE (ftype, iparams) = arg_type;
c906108c
SS
12578 iparams++;
12579 }
12580 child_die = sibling_die (child_die);
12581 }
12582 }
12583
76c10ea2 12584 return ftype;
c906108c
SS
12585}
12586
f792889a 12587static struct type *
e7c27a73 12588read_typedef (struct die_info *die, struct dwarf2_cu *cu)
c906108c 12589{
e7c27a73 12590 struct objfile *objfile = cu->objfile;
0114d602 12591 const char *name = NULL;
3c8e0968 12592 struct type *this_type, *target_type;
c906108c 12593
94af9270 12594 name = dwarf2_full_name (NULL, die, cu);
f792889a 12595 this_type = init_type (TYPE_CODE_TYPEDEF, 0,
0114d602
DJ
12596 TYPE_FLAG_TARGET_STUB, NULL, objfile);
12597 TYPE_NAME (this_type) = (char *) name;
f792889a 12598 set_die_type (die, this_type, cu);
3c8e0968
DE
12599 target_type = die_type (die, cu);
12600 if (target_type != this_type)
12601 TYPE_TARGET_TYPE (this_type) = target_type;
12602 else
12603 {
12604 /* Self-referential typedefs are, it seems, not allowed by the DWARF
12605 spec and cause infinite loops in GDB. */
12606 complaint (&symfile_complaints,
12607 _("Self-referential DW_TAG_typedef "
12608 "- DIE at 0x%x [in module %s]"),
b64f50a1 12609 die->offset.sect_off, objfile->name);
3c8e0968
DE
12610 TYPE_TARGET_TYPE (this_type) = NULL;
12611 }
f792889a 12612 return this_type;
c906108c
SS
12613}
12614
12615/* Find a representation of a given base type and install
12616 it in the TYPE field of the die. */
12617
f792889a 12618static struct type *
e7c27a73 12619read_base_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 12620{
e7c27a73 12621 struct objfile *objfile = cu->objfile;
c906108c
SS
12622 struct type *type;
12623 struct attribute *attr;
12624 int encoding = 0, size = 0;
15d034d0 12625 const char *name;
6ccb9162
UW
12626 enum type_code code = TYPE_CODE_INT;
12627 int type_flags = 0;
12628 struct type *target_type = NULL;
c906108c 12629
e142c38c 12630 attr = dwarf2_attr (die, DW_AT_encoding, cu);
c906108c
SS
12631 if (attr)
12632 {
12633 encoding = DW_UNSND (attr);
12634 }
e142c38c 12635 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
c906108c
SS
12636 if (attr)
12637 {
12638 size = DW_UNSND (attr);
12639 }
39cbfefa 12640 name = dwarf2_name (die, cu);
6ccb9162 12641 if (!name)
c906108c 12642 {
6ccb9162
UW
12643 complaint (&symfile_complaints,
12644 _("DW_AT_name missing from DW_TAG_base_type"));
c906108c 12645 }
6ccb9162
UW
12646
12647 switch (encoding)
c906108c 12648 {
6ccb9162
UW
12649 case DW_ATE_address:
12650 /* Turn DW_ATE_address into a void * pointer. */
12651 code = TYPE_CODE_PTR;
12652 type_flags |= TYPE_FLAG_UNSIGNED;
12653 target_type = init_type (TYPE_CODE_VOID, 1, 0, NULL, objfile);
12654 break;
12655 case DW_ATE_boolean:
12656 code = TYPE_CODE_BOOL;
12657 type_flags |= TYPE_FLAG_UNSIGNED;
12658 break;
12659 case DW_ATE_complex_float:
12660 code = TYPE_CODE_COMPLEX;
12661 target_type = init_type (TYPE_CODE_FLT, size / 2, 0, NULL, objfile);
12662 break;
12663 case DW_ATE_decimal_float:
12664 code = TYPE_CODE_DECFLOAT;
12665 break;
12666 case DW_ATE_float:
12667 code = TYPE_CODE_FLT;
12668 break;
12669 case DW_ATE_signed:
12670 break;
12671 case DW_ATE_unsigned:
12672 type_flags |= TYPE_FLAG_UNSIGNED;
3b2b8fea
TT
12673 if (cu->language == language_fortran
12674 && name
12675 && strncmp (name, "character(", sizeof ("character(") - 1) == 0)
12676 code = TYPE_CODE_CHAR;
6ccb9162
UW
12677 break;
12678 case DW_ATE_signed_char:
6e70227d 12679 if (cu->language == language_ada || cu->language == language_m2
3b2b8fea
TT
12680 || cu->language == language_pascal
12681 || cu->language == language_fortran)
6ccb9162
UW
12682 code = TYPE_CODE_CHAR;
12683 break;
12684 case DW_ATE_unsigned_char:
868a0084 12685 if (cu->language == language_ada || cu->language == language_m2
3b2b8fea
TT
12686 || cu->language == language_pascal
12687 || cu->language == language_fortran)
6ccb9162
UW
12688 code = TYPE_CODE_CHAR;
12689 type_flags |= TYPE_FLAG_UNSIGNED;
12690 break;
75079b2b
TT
12691 case DW_ATE_UTF:
12692 /* We just treat this as an integer and then recognize the
12693 type by name elsewhere. */
12694 break;
12695
6ccb9162
UW
12696 default:
12697 complaint (&symfile_complaints, _("unsupported DW_AT_encoding: '%s'"),
12698 dwarf_type_encoding_name (encoding));
12699 break;
c906108c 12700 }
6ccb9162 12701
0114d602
DJ
12702 type = init_type (code, size, type_flags, NULL, objfile);
12703 TYPE_NAME (type) = name;
6ccb9162
UW
12704 TYPE_TARGET_TYPE (type) = target_type;
12705
0114d602 12706 if (name && strcmp (name, "char") == 0)
876cecd0 12707 TYPE_NOSIGN (type) = 1;
0114d602 12708
f792889a 12709 return set_die_type (die, type, cu);
c906108c
SS
12710}
12711
a02abb62
JB
12712/* Read the given DW_AT_subrange DIE. */
12713
f792889a 12714static struct type *
a02abb62
JB
12715read_subrange_type (struct die_info *die, struct dwarf2_cu *cu)
12716{
12717 struct type *base_type;
12718 struct type *range_type;
12719 struct attribute *attr;
4fae6e18
JK
12720 LONGEST low, high;
12721 int low_default_is_valid;
15d034d0 12722 const char *name;
43bbcdc2 12723 LONGEST negative_mask;
e77813c8 12724
a02abb62 12725 base_type = die_type (die, cu);
953ac07e
JK
12726 /* Preserve BASE_TYPE's original type, just set its LENGTH. */
12727 check_typedef (base_type);
a02abb62 12728
7e314c57
JK
12729 /* The die_type call above may have already set the type for this DIE. */
12730 range_type = get_die_type (die, cu);
12731 if (range_type)
12732 return range_type;
12733
4fae6e18
JK
12734 /* Set LOW_DEFAULT_IS_VALID if current language and DWARF version allow
12735 omitting DW_AT_lower_bound. */
12736 switch (cu->language)
6e70227d 12737 {
4fae6e18
JK
12738 case language_c:
12739 case language_cplus:
12740 low = 0;
12741 low_default_is_valid = 1;
12742 break;
12743 case language_fortran:
12744 low = 1;
12745 low_default_is_valid = 1;
12746 break;
12747 case language_d:
12748 case language_java:
12749 case language_objc:
12750 low = 0;
12751 low_default_is_valid = (cu->header.version >= 4);
12752 break;
12753 case language_ada:
12754 case language_m2:
12755 case language_pascal:
a02abb62 12756 low = 1;
4fae6e18
JK
12757 low_default_is_valid = (cu->header.version >= 4);
12758 break;
12759 default:
12760 low = 0;
12761 low_default_is_valid = 0;
12762 break;
a02abb62
JB
12763 }
12764
dd5e6932
DJ
12765 /* FIXME: For variable sized arrays either of these could be
12766 a variable rather than a constant value. We'll allow it,
12767 but we don't know how to handle it. */
e142c38c 12768 attr = dwarf2_attr (die, DW_AT_lower_bound, cu);
a02abb62 12769 if (attr)
4fae6e18
JK
12770 low = dwarf2_get_attr_constant_value (attr, low);
12771 else if (!low_default_is_valid)
12772 complaint (&symfile_complaints, _("Missing DW_AT_lower_bound "
12773 "- DIE at 0x%x [in module %s]"),
12774 die->offset.sect_off, cu->objfile->name);
a02abb62 12775
e142c38c 12776 attr = dwarf2_attr (die, DW_AT_upper_bound, cu);
a02abb62 12777 if (attr)
6e70227d 12778 {
d48323d8 12779 if (attr_form_is_block (attr) || is_ref_attr (attr))
a02abb62
JB
12780 {
12781 /* GCC encodes arrays with unspecified or dynamic length
e77813c8 12782 with a DW_FORM_block1 attribute or a reference attribute.
a02abb62
JB
12783 FIXME: GDB does not yet know how to handle dynamic
12784 arrays properly, treat them as arrays with unspecified
12785 length for now.
12786
12787 FIXME: jimb/2003-09-22: GDB does not really know
12788 how to handle arrays of unspecified length
12789 either; we just represent them as zero-length
12790 arrays. Choose an appropriate upper bound given
12791 the lower bound we've computed above. */
12792 high = low - 1;
12793 }
12794 else
12795 high = dwarf2_get_attr_constant_value (attr, 1);
12796 }
e77813c8
PM
12797 else
12798 {
12799 attr = dwarf2_attr (die, DW_AT_count, cu);
12800 if (attr)
12801 {
12802 int count = dwarf2_get_attr_constant_value (attr, 1);
12803 high = low + count - 1;
12804 }
c2ff108b
JK
12805 else
12806 {
12807 /* Unspecified array length. */
12808 high = low - 1;
12809 }
e77813c8
PM
12810 }
12811
12812 /* Dwarf-2 specifications explicitly allows to create subrange types
12813 without specifying a base type.
12814 In that case, the base type must be set to the type of
12815 the lower bound, upper bound or count, in that order, if any of these
12816 three attributes references an object that has a type.
12817 If no base type is found, the Dwarf-2 specifications say that
12818 a signed integer type of size equal to the size of an address should
12819 be used.
12820 For the following C code: `extern char gdb_int [];'
12821 GCC produces an empty range DIE.
12822 FIXME: muller/2010-05-28: Possible references to object for low bound,
0963b4bd 12823 high bound or count are not yet handled by this code. */
e77813c8
PM
12824 if (TYPE_CODE (base_type) == TYPE_CODE_VOID)
12825 {
12826 struct objfile *objfile = cu->objfile;
12827 struct gdbarch *gdbarch = get_objfile_arch (objfile);
12828 int addr_size = gdbarch_addr_bit (gdbarch) /8;
12829 struct type *int_type = objfile_type (objfile)->builtin_int;
12830
12831 /* Test "int", "long int", and "long long int" objfile types,
12832 and select the first one having a size above or equal to the
12833 architecture address size. */
12834 if (int_type && TYPE_LENGTH (int_type) >= addr_size)
12835 base_type = int_type;
12836 else
12837 {
12838 int_type = objfile_type (objfile)->builtin_long;
12839 if (int_type && TYPE_LENGTH (int_type) >= addr_size)
12840 base_type = int_type;
12841 else
12842 {
12843 int_type = objfile_type (objfile)->builtin_long_long;
12844 if (int_type && TYPE_LENGTH (int_type) >= addr_size)
12845 base_type = int_type;
12846 }
12847 }
12848 }
a02abb62 12849
6e70227d 12850 negative_mask =
43bbcdc2
PH
12851 (LONGEST) -1 << (TYPE_LENGTH (base_type) * TARGET_CHAR_BIT - 1);
12852 if (!TYPE_UNSIGNED (base_type) && (low & negative_mask))
12853 low |= negative_mask;
12854 if (!TYPE_UNSIGNED (base_type) && (high & negative_mask))
12855 high |= negative_mask;
12856
a02abb62
JB
12857 range_type = create_range_type (NULL, base_type, low, high);
12858
bbb0eef6
JK
12859 /* Mark arrays with dynamic length at least as an array of unspecified
12860 length. GDB could check the boundary but before it gets implemented at
12861 least allow accessing the array elements. */
d48323d8 12862 if (attr && attr_form_is_block (attr))
bbb0eef6
JK
12863 TYPE_HIGH_BOUND_UNDEFINED (range_type) = 1;
12864
c2ff108b
JK
12865 /* Ada expects an empty array on no boundary attributes. */
12866 if (attr == NULL && cu->language != language_ada)
12867 TYPE_HIGH_BOUND_UNDEFINED (range_type) = 1;
12868
39cbfefa
DJ
12869 name = dwarf2_name (die, cu);
12870 if (name)
12871 TYPE_NAME (range_type) = name;
6e70227d 12872
e142c38c 12873 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
a02abb62
JB
12874 if (attr)
12875 TYPE_LENGTH (range_type) = DW_UNSND (attr);
12876
7e314c57
JK
12877 set_die_type (die, range_type, cu);
12878
12879 /* set_die_type should be already done. */
b4ba55a1
JB
12880 set_descriptive_type (range_type, die, cu);
12881
7e314c57 12882 return range_type;
a02abb62 12883}
6e70227d 12884
f792889a 12885static struct type *
81a17f79
JB
12886read_unspecified_type (struct die_info *die, struct dwarf2_cu *cu)
12887{
12888 struct type *type;
81a17f79 12889
81a17f79
JB
12890 /* For now, we only support the C meaning of an unspecified type: void. */
12891
0114d602
DJ
12892 type = init_type (TYPE_CODE_VOID, 0, 0, NULL, cu->objfile);
12893 TYPE_NAME (type) = dwarf2_name (die, cu);
81a17f79 12894
f792889a 12895 return set_die_type (die, type, cu);
81a17f79 12896}
a02abb62 12897
639d11d3
DC
12898/* Read a single die and all its descendents. Set the die's sibling
12899 field to NULL; set other fields in the die correctly, and set all
12900 of the descendents' fields correctly. Set *NEW_INFO_PTR to the
12901 location of the info_ptr after reading all of those dies. PARENT
12902 is the parent of the die in question. */
12903
12904static struct die_info *
dee91e82
DE
12905read_die_and_children (const struct die_reader_specs *reader,
12906 gdb_byte *info_ptr,
12907 gdb_byte **new_info_ptr,
12908 struct die_info *parent)
639d11d3
DC
12909{
12910 struct die_info *die;
fe1b8b76 12911 gdb_byte *cur_ptr;
639d11d3
DC
12912 int has_children;
12913
93311388 12914 cur_ptr = read_full_die (reader, &die, info_ptr, &has_children);
1d325ec1
DJ
12915 if (die == NULL)
12916 {
12917 *new_info_ptr = cur_ptr;
12918 return NULL;
12919 }
93311388 12920 store_in_ref_table (die, reader->cu);
639d11d3
DC
12921
12922 if (has_children)
348e048f 12923 die->child = read_die_and_siblings (reader, cur_ptr, new_info_ptr, die);
639d11d3
DC
12924 else
12925 {
12926 die->child = NULL;
12927 *new_info_ptr = cur_ptr;
12928 }
12929
12930 die->sibling = NULL;
12931 die->parent = parent;
12932 return die;
12933}
12934
12935/* Read a die, all of its descendents, and all of its siblings; set
12936 all of the fields of all of the dies correctly. Arguments are as
12937 in read_die_and_children. */
12938
12939static struct die_info *
93311388
DE
12940read_die_and_siblings (const struct die_reader_specs *reader,
12941 gdb_byte *info_ptr,
fe1b8b76 12942 gdb_byte **new_info_ptr,
639d11d3
DC
12943 struct die_info *parent)
12944{
12945 struct die_info *first_die, *last_sibling;
fe1b8b76 12946 gdb_byte *cur_ptr;
639d11d3 12947
c906108c 12948 cur_ptr = info_ptr;
639d11d3
DC
12949 first_die = last_sibling = NULL;
12950
12951 while (1)
c906108c 12952 {
639d11d3 12953 struct die_info *die
dee91e82 12954 = read_die_and_children (reader, cur_ptr, &cur_ptr, parent);
639d11d3 12955
1d325ec1 12956 if (die == NULL)
c906108c 12957 {
639d11d3
DC
12958 *new_info_ptr = cur_ptr;
12959 return first_die;
c906108c 12960 }
1d325ec1
DJ
12961
12962 if (!first_die)
12963 first_die = die;
c906108c 12964 else
1d325ec1
DJ
12965 last_sibling->sibling = die;
12966
12967 last_sibling = die;
c906108c 12968 }
c906108c
SS
12969}
12970
3019eac3
DE
12971/* Read a die and all its attributes, leave space for NUM_EXTRA_ATTRS
12972 attributes.
12973 The caller is responsible for filling in the extra attributes
12974 and updating (*DIEP)->num_attrs.
12975 Set DIEP to point to a newly allocated die with its information,
12976 except for its child, sibling, and parent fields.
12977 Set HAS_CHILDREN to tell whether the die has children or not. */
93311388
DE
12978
12979static gdb_byte *
3019eac3
DE
12980read_full_die_1 (const struct die_reader_specs *reader,
12981 struct die_info **diep, gdb_byte *info_ptr,
12982 int *has_children, int num_extra_attrs)
93311388 12983{
b64f50a1
JK
12984 unsigned int abbrev_number, bytes_read, i;
12985 sect_offset offset;
93311388
DE
12986 struct abbrev_info *abbrev;
12987 struct die_info *die;
12988 struct dwarf2_cu *cu = reader->cu;
12989 bfd *abfd = reader->abfd;
12990
b64f50a1 12991 offset.sect_off = info_ptr - reader->buffer;
93311388
DE
12992 abbrev_number = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
12993 info_ptr += bytes_read;
12994 if (!abbrev_number)
12995 {
12996 *diep = NULL;
12997 *has_children = 0;
12998 return info_ptr;
12999 }
13000
433df2d4 13001 abbrev = abbrev_table_lookup_abbrev (cu->abbrev_table, abbrev_number);
93311388 13002 if (!abbrev)
348e048f
DE
13003 error (_("Dwarf Error: could not find abbrev number %d [in module %s]"),
13004 abbrev_number,
13005 bfd_get_filename (abfd));
13006
3019eac3 13007 die = dwarf_alloc_die (cu, abbrev->num_attrs + num_extra_attrs);
93311388
DE
13008 die->offset = offset;
13009 die->tag = abbrev->tag;
13010 die->abbrev = abbrev_number;
13011
3019eac3
DE
13012 /* Make the result usable.
13013 The caller needs to update num_attrs after adding the extra
13014 attributes. */
93311388
DE
13015 die->num_attrs = abbrev->num_attrs;
13016
13017 for (i = 0; i < abbrev->num_attrs; ++i)
dee91e82
DE
13018 info_ptr = read_attribute (reader, &die->attrs[i], &abbrev->attrs[i],
13019 info_ptr);
93311388
DE
13020
13021 *diep = die;
13022 *has_children = abbrev->has_children;
13023 return info_ptr;
13024}
13025
3019eac3
DE
13026/* Read a die and all its attributes.
13027 Set DIEP to point to a newly allocated die with its information,
13028 except for its child, sibling, and parent fields.
13029 Set HAS_CHILDREN to tell whether the die has children or not. */
13030
13031static gdb_byte *
13032read_full_die (const struct die_reader_specs *reader,
13033 struct die_info **diep, gdb_byte *info_ptr,
13034 int *has_children)
13035{
13036 return read_full_die_1 (reader, diep, info_ptr, has_children, 0);
13037}
433df2d4
DE
13038\f
13039/* Abbreviation tables.
3019eac3 13040
433df2d4 13041 In DWARF version 2, the description of the debugging information is
c906108c
SS
13042 stored in a separate .debug_abbrev section. Before we read any
13043 dies from a section we read in all abbreviations and install them
433df2d4
DE
13044 in a hash table. */
13045
13046/* Allocate space for a struct abbrev_info object in ABBREV_TABLE. */
13047
13048static struct abbrev_info *
13049abbrev_table_alloc_abbrev (struct abbrev_table *abbrev_table)
13050{
13051 struct abbrev_info *abbrev;
13052
13053 abbrev = (struct abbrev_info *)
13054 obstack_alloc (&abbrev_table->abbrev_obstack, sizeof (struct abbrev_info));
13055 memset (abbrev, 0, sizeof (struct abbrev_info));
13056 return abbrev;
13057}
13058
13059/* Add an abbreviation to the table. */
c906108c
SS
13060
13061static void
433df2d4
DE
13062abbrev_table_add_abbrev (struct abbrev_table *abbrev_table,
13063 unsigned int abbrev_number,
13064 struct abbrev_info *abbrev)
13065{
13066 unsigned int hash_number;
13067
13068 hash_number = abbrev_number % ABBREV_HASH_SIZE;
13069 abbrev->next = abbrev_table->abbrevs[hash_number];
13070 abbrev_table->abbrevs[hash_number] = abbrev;
13071}
dee91e82 13072
433df2d4
DE
13073/* Look up an abbrev in the table.
13074 Returns NULL if the abbrev is not found. */
13075
13076static struct abbrev_info *
13077abbrev_table_lookup_abbrev (const struct abbrev_table *abbrev_table,
13078 unsigned int abbrev_number)
c906108c 13079{
433df2d4
DE
13080 unsigned int hash_number;
13081 struct abbrev_info *abbrev;
13082
13083 hash_number = abbrev_number % ABBREV_HASH_SIZE;
13084 abbrev = abbrev_table->abbrevs[hash_number];
13085
13086 while (abbrev)
13087 {
13088 if (abbrev->number == abbrev_number)
13089 return abbrev;
13090 abbrev = abbrev->next;
13091 }
13092 return NULL;
13093}
13094
13095/* Read in an abbrev table. */
13096
13097static struct abbrev_table *
13098abbrev_table_read_table (struct dwarf2_section_info *section,
13099 sect_offset offset)
13100{
13101 struct objfile *objfile = dwarf2_per_objfile->objfile;
13102 bfd *abfd = section->asection->owner;
13103 struct abbrev_table *abbrev_table;
fe1b8b76 13104 gdb_byte *abbrev_ptr;
c906108c
SS
13105 struct abbrev_info *cur_abbrev;
13106 unsigned int abbrev_number, bytes_read, abbrev_name;
433df2d4 13107 unsigned int abbrev_form;
f3dd6933
DJ
13108 struct attr_abbrev *cur_attrs;
13109 unsigned int allocated_attrs;
c906108c 13110
433df2d4 13111 abbrev_table = XMALLOC (struct abbrev_table);
f4dc4d17 13112 abbrev_table->offset = offset;
433df2d4
DE
13113 obstack_init (&abbrev_table->abbrev_obstack);
13114 abbrev_table->abbrevs = obstack_alloc (&abbrev_table->abbrev_obstack,
13115 (ABBREV_HASH_SIZE
13116 * sizeof (struct abbrev_info *)));
13117 memset (abbrev_table->abbrevs, 0,
13118 ABBREV_HASH_SIZE * sizeof (struct abbrev_info *));
c906108c 13119
433df2d4
DE
13120 dwarf2_read_section (objfile, section);
13121 abbrev_ptr = section->buffer + offset.sect_off;
c906108c
SS
13122 abbrev_number = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
13123 abbrev_ptr += bytes_read;
13124
f3dd6933
DJ
13125 allocated_attrs = ATTR_ALLOC_CHUNK;
13126 cur_attrs = xmalloc (allocated_attrs * sizeof (struct attr_abbrev));
6e70227d 13127
0963b4bd 13128 /* Loop until we reach an abbrev number of 0. */
c906108c
SS
13129 while (abbrev_number)
13130 {
433df2d4 13131 cur_abbrev = abbrev_table_alloc_abbrev (abbrev_table);
c906108c
SS
13132
13133 /* read in abbrev header */
13134 cur_abbrev->number = abbrev_number;
13135 cur_abbrev->tag = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
13136 abbrev_ptr += bytes_read;
13137 cur_abbrev->has_children = read_1_byte (abfd, abbrev_ptr);
13138 abbrev_ptr += 1;
13139
13140 /* now read in declarations */
13141 abbrev_name = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
13142 abbrev_ptr += bytes_read;
13143 abbrev_form = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
13144 abbrev_ptr += bytes_read;
13145 while (abbrev_name)
13146 {
f3dd6933 13147 if (cur_abbrev->num_attrs == allocated_attrs)
c906108c 13148 {
f3dd6933
DJ
13149 allocated_attrs += ATTR_ALLOC_CHUNK;
13150 cur_attrs
13151 = xrealloc (cur_attrs, (allocated_attrs
13152 * sizeof (struct attr_abbrev)));
c906108c 13153 }
ae038cb0 13154
f3dd6933
DJ
13155 cur_attrs[cur_abbrev->num_attrs].name = abbrev_name;
13156 cur_attrs[cur_abbrev->num_attrs++].form = abbrev_form;
c906108c
SS
13157 abbrev_name = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
13158 abbrev_ptr += bytes_read;
13159 abbrev_form = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
13160 abbrev_ptr += bytes_read;
13161 }
13162
433df2d4 13163 cur_abbrev->attrs = obstack_alloc (&abbrev_table->abbrev_obstack,
f3dd6933
DJ
13164 (cur_abbrev->num_attrs
13165 * sizeof (struct attr_abbrev)));
13166 memcpy (cur_abbrev->attrs, cur_attrs,
13167 cur_abbrev->num_attrs * sizeof (struct attr_abbrev));
13168
433df2d4 13169 abbrev_table_add_abbrev (abbrev_table, abbrev_number, cur_abbrev);
c906108c
SS
13170
13171 /* Get next abbreviation.
13172 Under Irix6 the abbreviations for a compilation unit are not
c5aa993b
JM
13173 always properly terminated with an abbrev number of 0.
13174 Exit loop if we encounter an abbreviation which we have
13175 already read (which means we are about to read the abbreviations
13176 for the next compile unit) or if the end of the abbreviation
13177 table is reached. */
433df2d4 13178 if ((unsigned int) (abbrev_ptr - section->buffer) >= section->size)
c906108c
SS
13179 break;
13180 abbrev_number = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
13181 abbrev_ptr += bytes_read;
433df2d4 13182 if (abbrev_table_lookup_abbrev (abbrev_table, abbrev_number) != NULL)
c906108c
SS
13183 break;
13184 }
f3dd6933
DJ
13185
13186 xfree (cur_attrs);
433df2d4 13187 return abbrev_table;
c906108c
SS
13188}
13189
433df2d4 13190/* Free the resources held by ABBREV_TABLE. */
c906108c 13191
c906108c 13192static void
433df2d4 13193abbrev_table_free (struct abbrev_table *abbrev_table)
c906108c 13194{
433df2d4
DE
13195 obstack_free (&abbrev_table->abbrev_obstack, NULL);
13196 xfree (abbrev_table);
c906108c
SS
13197}
13198
f4dc4d17
DE
13199/* Same as abbrev_table_free but as a cleanup.
13200 We pass in a pointer to the pointer to the table so that we can
13201 set the pointer to NULL when we're done. It also simplifies
13202 build_type_unit_groups. */
13203
13204static void
13205abbrev_table_free_cleanup (void *table_ptr)
13206{
13207 struct abbrev_table **abbrev_table_ptr = table_ptr;
13208
13209 if (*abbrev_table_ptr != NULL)
13210 abbrev_table_free (*abbrev_table_ptr);
13211 *abbrev_table_ptr = NULL;
13212}
13213
433df2d4
DE
13214/* Read the abbrev table for CU from ABBREV_SECTION. */
13215
13216static void
13217dwarf2_read_abbrevs (struct dwarf2_cu *cu,
13218 struct dwarf2_section_info *abbrev_section)
c906108c 13219{
433df2d4
DE
13220 cu->abbrev_table =
13221 abbrev_table_read_table (abbrev_section, cu->header.abbrev_offset);
13222}
c906108c 13223
433df2d4 13224/* Release the memory used by the abbrev table for a compilation unit. */
c906108c 13225
433df2d4
DE
13226static void
13227dwarf2_free_abbrev_table (void *ptr_to_cu)
13228{
13229 struct dwarf2_cu *cu = ptr_to_cu;
c906108c 13230
433df2d4
DE
13231 abbrev_table_free (cu->abbrev_table);
13232 /* Set this to NULL so that we SEGV if we try to read it later,
13233 and also because free_comp_unit verifies this is NULL. */
13234 cu->abbrev_table = NULL;
13235}
13236\f
72bf9492
DJ
13237/* Returns nonzero if TAG represents a type that we might generate a partial
13238 symbol for. */
13239
13240static int
13241is_type_tag_for_partial (int tag)
13242{
13243 switch (tag)
13244 {
13245#if 0
13246 /* Some types that would be reasonable to generate partial symbols for,
13247 that we don't at present. */
13248 case DW_TAG_array_type:
13249 case DW_TAG_file_type:
13250 case DW_TAG_ptr_to_member_type:
13251 case DW_TAG_set_type:
13252 case DW_TAG_string_type:
13253 case DW_TAG_subroutine_type:
13254#endif
13255 case DW_TAG_base_type:
13256 case DW_TAG_class_type:
680b30c7 13257 case DW_TAG_interface_type:
72bf9492
DJ
13258 case DW_TAG_enumeration_type:
13259 case DW_TAG_structure_type:
13260 case DW_TAG_subrange_type:
13261 case DW_TAG_typedef:
13262 case DW_TAG_union_type:
13263 return 1;
13264 default:
13265 return 0;
13266 }
13267}
13268
13269/* Load all DIEs that are interesting for partial symbols into memory. */
13270
13271static struct partial_die_info *
dee91e82
DE
13272load_partial_dies (const struct die_reader_specs *reader,
13273 gdb_byte *info_ptr, int building_psymtab)
72bf9492 13274{
dee91e82 13275 struct dwarf2_cu *cu = reader->cu;
bb5ed363 13276 struct objfile *objfile = cu->objfile;
72bf9492
DJ
13277 struct partial_die_info *part_die;
13278 struct partial_die_info *parent_die, *last_die, *first_die = NULL;
13279 struct abbrev_info *abbrev;
13280 unsigned int bytes_read;
5afb4e99 13281 unsigned int load_all = 0;
72bf9492
DJ
13282 int nesting_level = 1;
13283
13284 parent_die = NULL;
13285 last_die = NULL;
13286
7adf1e79
DE
13287 gdb_assert (cu->per_cu != NULL);
13288 if (cu->per_cu->load_all_dies)
5afb4e99
DJ
13289 load_all = 1;
13290
72bf9492
DJ
13291 cu->partial_dies
13292 = htab_create_alloc_ex (cu->header.length / 12,
13293 partial_die_hash,
13294 partial_die_eq,
13295 NULL,
13296 &cu->comp_unit_obstack,
13297 hashtab_obstack_allocate,
13298 dummy_obstack_deallocate);
13299
13300 part_die = obstack_alloc (&cu->comp_unit_obstack,
13301 sizeof (struct partial_die_info));
13302
13303 while (1)
13304 {
13305 abbrev = peek_die_abbrev (info_ptr, &bytes_read, cu);
13306
13307 /* A NULL abbrev means the end of a series of children. */
13308 if (abbrev == NULL)
13309 {
13310 if (--nesting_level == 0)
13311 {
13312 /* PART_DIE was probably the last thing allocated on the
13313 comp_unit_obstack, so we could call obstack_free
13314 here. We don't do that because the waste is small,
13315 and will be cleaned up when we're done with this
13316 compilation unit. This way, we're also more robust
13317 against other users of the comp_unit_obstack. */
13318 return first_die;
13319 }
13320 info_ptr += bytes_read;
13321 last_die = parent_die;
13322 parent_die = parent_die->die_parent;
13323 continue;
13324 }
13325
98bfdba5
PA
13326 /* Check for template arguments. We never save these; if
13327 they're seen, we just mark the parent, and go on our way. */
13328 if (parent_die != NULL
13329 && cu->language == language_cplus
13330 && (abbrev->tag == DW_TAG_template_type_param
13331 || abbrev->tag == DW_TAG_template_value_param))
13332 {
13333 parent_die->has_template_arguments = 1;
13334
13335 if (!load_all)
13336 {
13337 /* We don't need a partial DIE for the template argument. */
dee91e82 13338 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
98bfdba5
PA
13339 continue;
13340 }
13341 }
13342
0d99eb77 13343 /* We only recurse into c++ subprograms looking for template arguments.
98bfdba5
PA
13344 Skip their other children. */
13345 if (!load_all
13346 && cu->language == language_cplus
13347 && parent_die != NULL
13348 && parent_die->tag == DW_TAG_subprogram)
13349 {
dee91e82 13350 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
98bfdba5
PA
13351 continue;
13352 }
13353
5afb4e99
DJ
13354 /* Check whether this DIE is interesting enough to save. Normally
13355 we would not be interested in members here, but there may be
13356 later variables referencing them via DW_AT_specification (for
13357 static members). */
13358 if (!load_all
13359 && !is_type_tag_for_partial (abbrev->tag)
72929c62 13360 && abbrev->tag != DW_TAG_constant
72bf9492
DJ
13361 && abbrev->tag != DW_TAG_enumerator
13362 && abbrev->tag != DW_TAG_subprogram
bc30ff58 13363 && abbrev->tag != DW_TAG_lexical_block
72bf9492 13364 && abbrev->tag != DW_TAG_variable
5afb4e99 13365 && abbrev->tag != DW_TAG_namespace
f55ee35c 13366 && abbrev->tag != DW_TAG_module
95554aad
TT
13367 && abbrev->tag != DW_TAG_member
13368 && abbrev->tag != DW_TAG_imported_unit)
72bf9492
DJ
13369 {
13370 /* Otherwise we skip to the next sibling, if any. */
dee91e82 13371 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
72bf9492
DJ
13372 continue;
13373 }
13374
dee91e82
DE
13375 info_ptr = read_partial_die (reader, part_die, abbrev, bytes_read,
13376 info_ptr);
72bf9492
DJ
13377
13378 /* This two-pass algorithm for processing partial symbols has a
13379 high cost in cache pressure. Thus, handle some simple cases
13380 here which cover the majority of C partial symbols. DIEs
13381 which neither have specification tags in them, nor could have
13382 specification tags elsewhere pointing at them, can simply be
13383 processed and discarded.
13384
13385 This segment is also optional; scan_partial_symbols and
13386 add_partial_symbol will handle these DIEs if we chain
13387 them in normally. When compilers which do not emit large
13388 quantities of duplicate debug information are more common,
13389 this code can probably be removed. */
13390
13391 /* Any complete simple types at the top level (pretty much all
13392 of them, for a language without namespaces), can be processed
13393 directly. */
13394 if (parent_die == NULL
13395 && part_die->has_specification == 0
13396 && part_die->is_declaration == 0
d8228535 13397 && ((part_die->tag == DW_TAG_typedef && !part_die->has_children)
72bf9492
DJ
13398 || part_die->tag == DW_TAG_base_type
13399 || part_die->tag == DW_TAG_subrange_type))
13400 {
13401 if (building_psymtab && part_die->name != NULL)
04a679b8 13402 add_psymbol_to_list (part_die->name, strlen (part_die->name), 0,
72bf9492 13403 VAR_DOMAIN, LOC_TYPEDEF,
bb5ed363
DE
13404 &objfile->static_psymbols,
13405 0, (CORE_ADDR) 0, cu->language, objfile);
dee91e82 13406 info_ptr = locate_pdi_sibling (reader, part_die, info_ptr);
72bf9492
DJ
13407 continue;
13408 }
13409
d8228535
JK
13410 /* The exception for DW_TAG_typedef with has_children above is
13411 a workaround of GCC PR debug/47510. In the case of this complaint
13412 type_name_no_tag_or_error will error on such types later.
13413
13414 GDB skipped children of DW_TAG_typedef by the shortcut above and then
13415 it could not find the child DIEs referenced later, this is checked
13416 above. In correct DWARF DW_TAG_typedef should have no children. */
13417
13418 if (part_die->tag == DW_TAG_typedef && part_die->has_children)
13419 complaint (&symfile_complaints,
13420 _("DW_TAG_typedef has childen - GCC PR debug/47510 bug "
13421 "- DIE at 0x%x [in module %s]"),
b64f50a1 13422 part_die->offset.sect_off, objfile->name);
d8228535 13423
72bf9492
DJ
13424 /* If we're at the second level, and we're an enumerator, and
13425 our parent has no specification (meaning possibly lives in a
13426 namespace elsewhere), then we can add the partial symbol now
13427 instead of queueing it. */
13428 if (part_die->tag == DW_TAG_enumerator
13429 && parent_die != NULL
13430 && parent_die->die_parent == NULL
13431 && parent_die->tag == DW_TAG_enumeration_type
13432 && parent_die->has_specification == 0)
13433 {
13434 if (part_die->name == NULL)
3e43a32a
MS
13435 complaint (&symfile_complaints,
13436 _("malformed enumerator DIE ignored"));
72bf9492 13437 else if (building_psymtab)
04a679b8 13438 add_psymbol_to_list (part_die->name, strlen (part_die->name), 0,
72bf9492 13439 VAR_DOMAIN, LOC_CONST,
987504bb
JJ
13440 (cu->language == language_cplus
13441 || cu->language == language_java)
bb5ed363
DE
13442 ? &objfile->global_psymbols
13443 : &objfile->static_psymbols,
13444 0, (CORE_ADDR) 0, cu->language, objfile);
72bf9492 13445
dee91e82 13446 info_ptr = locate_pdi_sibling (reader, part_die, info_ptr);
72bf9492
DJ
13447 continue;
13448 }
13449
13450 /* We'll save this DIE so link it in. */
13451 part_die->die_parent = parent_die;
13452 part_die->die_sibling = NULL;
13453 part_die->die_child = NULL;
13454
13455 if (last_die && last_die == parent_die)
13456 last_die->die_child = part_die;
13457 else if (last_die)
13458 last_die->die_sibling = part_die;
13459
13460 last_die = part_die;
13461
13462 if (first_die == NULL)
13463 first_die = part_die;
13464
13465 /* Maybe add the DIE to the hash table. Not all DIEs that we
13466 find interesting need to be in the hash table, because we
13467 also have the parent/sibling/child chains; only those that we
13468 might refer to by offset later during partial symbol reading.
13469
13470 For now this means things that might have be the target of a
13471 DW_AT_specification, DW_AT_abstract_origin, or
13472 DW_AT_extension. DW_AT_extension will refer only to
13473 namespaces; DW_AT_abstract_origin refers to functions (and
13474 many things under the function DIE, but we do not recurse
13475 into function DIEs during partial symbol reading) and
13476 possibly variables as well; DW_AT_specification refers to
13477 declarations. Declarations ought to have the DW_AT_declaration
13478 flag. It happens that GCC forgets to put it in sometimes, but
13479 only for functions, not for types.
13480
13481 Adding more things than necessary to the hash table is harmless
13482 except for the performance cost. Adding too few will result in
5afb4e99
DJ
13483 wasted time in find_partial_die, when we reread the compilation
13484 unit with load_all_dies set. */
72bf9492 13485
5afb4e99 13486 if (load_all
72929c62 13487 || abbrev->tag == DW_TAG_constant
5afb4e99 13488 || abbrev->tag == DW_TAG_subprogram
72bf9492
DJ
13489 || abbrev->tag == DW_TAG_variable
13490 || abbrev->tag == DW_TAG_namespace
13491 || part_die->is_declaration)
13492 {
13493 void **slot;
13494
13495 slot = htab_find_slot_with_hash (cu->partial_dies, part_die,
b64f50a1 13496 part_die->offset.sect_off, INSERT);
72bf9492
DJ
13497 *slot = part_die;
13498 }
13499
13500 part_die = obstack_alloc (&cu->comp_unit_obstack,
13501 sizeof (struct partial_die_info));
13502
13503 /* For some DIEs we want to follow their children (if any). For C
bc30ff58 13504 we have no reason to follow the children of structures; for other
98bfdba5
PA
13505 languages we have to, so that we can get at method physnames
13506 to infer fully qualified class names, for DW_AT_specification,
13507 and for C++ template arguments. For C++, we also look one level
13508 inside functions to find template arguments (if the name of the
13509 function does not already contain the template arguments).
bc30ff58
JB
13510
13511 For Ada, we need to scan the children of subprograms and lexical
13512 blocks as well because Ada allows the definition of nested
13513 entities that could be interesting for the debugger, such as
13514 nested subprograms for instance. */
72bf9492 13515 if (last_die->has_children
5afb4e99
DJ
13516 && (load_all
13517 || last_die->tag == DW_TAG_namespace
f55ee35c 13518 || last_die->tag == DW_TAG_module
72bf9492 13519 || last_die->tag == DW_TAG_enumeration_type
98bfdba5
PA
13520 || (cu->language == language_cplus
13521 && last_die->tag == DW_TAG_subprogram
13522 && (last_die->name == NULL
13523 || strchr (last_die->name, '<') == NULL))
72bf9492
DJ
13524 || (cu->language != language_c
13525 && (last_die->tag == DW_TAG_class_type
680b30c7 13526 || last_die->tag == DW_TAG_interface_type
72bf9492 13527 || last_die->tag == DW_TAG_structure_type
bc30ff58
JB
13528 || last_die->tag == DW_TAG_union_type))
13529 || (cu->language == language_ada
13530 && (last_die->tag == DW_TAG_subprogram
13531 || last_die->tag == DW_TAG_lexical_block))))
72bf9492
DJ
13532 {
13533 nesting_level++;
13534 parent_die = last_die;
13535 continue;
13536 }
13537
13538 /* Otherwise we skip to the next sibling, if any. */
dee91e82 13539 info_ptr = locate_pdi_sibling (reader, last_die, info_ptr);
72bf9492
DJ
13540
13541 /* Back to the top, do it again. */
13542 }
13543}
13544
c906108c
SS
13545/* Read a minimal amount of information into the minimal die structure. */
13546
fe1b8b76 13547static gdb_byte *
dee91e82
DE
13548read_partial_die (const struct die_reader_specs *reader,
13549 struct partial_die_info *part_die,
13550 struct abbrev_info *abbrev, unsigned int abbrev_len,
13551 gdb_byte *info_ptr)
c906108c 13552{
dee91e82 13553 struct dwarf2_cu *cu = reader->cu;
bb5ed363 13554 struct objfile *objfile = cu->objfile;
dee91e82 13555 gdb_byte *buffer = reader->buffer;
fa238c03 13556 unsigned int i;
c906108c 13557 struct attribute attr;
c5aa993b 13558 int has_low_pc_attr = 0;
c906108c 13559 int has_high_pc_attr = 0;
91da1414 13560 int high_pc_relative = 0;
c906108c 13561
72bf9492 13562 memset (part_die, 0, sizeof (struct partial_die_info));
c906108c 13563
b64f50a1 13564 part_die->offset.sect_off = info_ptr - buffer;
72bf9492
DJ
13565
13566 info_ptr += abbrev_len;
13567
13568 if (abbrev == NULL)
13569 return info_ptr;
13570
c906108c
SS
13571 part_die->tag = abbrev->tag;
13572 part_die->has_children = abbrev->has_children;
c906108c
SS
13573
13574 for (i = 0; i < abbrev->num_attrs; ++i)
13575 {
dee91e82 13576 info_ptr = read_attribute (reader, &attr, &abbrev->attrs[i], info_ptr);
c906108c
SS
13577
13578 /* Store the data if it is of an attribute we want to keep in a
c5aa993b 13579 partial symbol table. */
c906108c
SS
13580 switch (attr.name)
13581 {
13582 case DW_AT_name:
71c25dea
TT
13583 switch (part_die->tag)
13584 {
13585 case DW_TAG_compile_unit:
95554aad 13586 case DW_TAG_partial_unit:
348e048f 13587 case DW_TAG_type_unit:
71c25dea
TT
13588 /* Compilation units have a DW_AT_name that is a filename, not
13589 a source language identifier. */
13590 case DW_TAG_enumeration_type:
13591 case DW_TAG_enumerator:
13592 /* These tags always have simple identifiers already; no need
13593 to canonicalize them. */
13594 part_die->name = DW_STRING (&attr);
13595 break;
13596 default:
13597 part_die->name
13598 = dwarf2_canonicalize_name (DW_STRING (&attr), cu,
bb5ed363 13599 &objfile->objfile_obstack);
71c25dea
TT
13600 break;
13601 }
c906108c 13602 break;
31ef98ae 13603 case DW_AT_linkage_name:
c906108c 13604 case DW_AT_MIPS_linkage_name:
31ef98ae
TT
13605 /* Note that both forms of linkage name might appear. We
13606 assume they will be the same, and we only store the last
13607 one we see. */
94af9270
KS
13608 if (cu->language == language_ada)
13609 part_die->name = DW_STRING (&attr);
abc72ce4 13610 part_die->linkage_name = DW_STRING (&attr);
c906108c
SS
13611 break;
13612 case DW_AT_low_pc:
13613 has_low_pc_attr = 1;
13614 part_die->lowpc = DW_ADDR (&attr);
13615 break;
13616 case DW_AT_high_pc:
13617 has_high_pc_attr = 1;
3019eac3
DE
13618 if (attr.form == DW_FORM_addr
13619 || attr.form == DW_FORM_GNU_addr_index)
91da1414
MW
13620 part_die->highpc = DW_ADDR (&attr);
13621 else
13622 {
13623 high_pc_relative = 1;
13624 part_die->highpc = DW_UNSND (&attr);
13625 }
c906108c
SS
13626 break;
13627 case DW_AT_location:
0963b4bd 13628 /* Support the .debug_loc offsets. */
8e19ed76
PS
13629 if (attr_form_is_block (&attr))
13630 {
95554aad 13631 part_die->d.locdesc = DW_BLOCK (&attr);
8e19ed76 13632 }
3690dd37 13633 else if (attr_form_is_section_offset (&attr))
8e19ed76 13634 {
4d3c2250 13635 dwarf2_complex_location_expr_complaint ();
8e19ed76
PS
13636 }
13637 else
13638 {
4d3c2250
KB
13639 dwarf2_invalid_attrib_class_complaint ("DW_AT_location",
13640 "partial symbol information");
8e19ed76 13641 }
c906108c 13642 break;
c906108c
SS
13643 case DW_AT_external:
13644 part_die->is_external = DW_UNSND (&attr);
13645 break;
13646 case DW_AT_declaration:
13647 part_die->is_declaration = DW_UNSND (&attr);
13648 break;
13649 case DW_AT_type:
13650 part_die->has_type = 1;
13651 break;
13652 case DW_AT_abstract_origin:
13653 case DW_AT_specification:
72bf9492
DJ
13654 case DW_AT_extension:
13655 part_die->has_specification = 1;
c764a876 13656 part_die->spec_offset = dwarf2_get_ref_die_offset (&attr);
36586728
TT
13657 part_die->spec_is_dwz = (attr.form == DW_FORM_GNU_ref_alt
13658 || cu->per_cu->is_dwz);
c906108c
SS
13659 break;
13660 case DW_AT_sibling:
13661 /* Ignore absolute siblings, they might point outside of
13662 the current compile unit. */
13663 if (attr.form == DW_FORM_ref_addr)
3e43a32a
MS
13664 complaint (&symfile_complaints,
13665 _("ignoring absolute DW_AT_sibling"));
c906108c 13666 else
b64f50a1 13667 part_die->sibling = buffer + dwarf2_get_ref_die_offset (&attr).sect_off;
c906108c 13668 break;
fa4028e9
JB
13669 case DW_AT_byte_size:
13670 part_die->has_byte_size = 1;
13671 break;
68511cec
CES
13672 case DW_AT_calling_convention:
13673 /* DWARF doesn't provide a way to identify a program's source-level
13674 entry point. DW_AT_calling_convention attributes are only meant
13675 to describe functions' calling conventions.
13676
13677 However, because it's a necessary piece of information in
13678 Fortran, and because DW_CC_program is the only piece of debugging
13679 information whose definition refers to a 'main program' at all,
13680 several compilers have begun marking Fortran main programs with
13681 DW_CC_program --- even when those functions use the standard
13682 calling conventions.
13683
13684 So until DWARF specifies a way to provide this information and
13685 compilers pick up the new representation, we'll support this
13686 practice. */
13687 if (DW_UNSND (&attr) == DW_CC_program
13688 && cu->language == language_fortran)
01f8c46d
JK
13689 {
13690 set_main_name (part_die->name);
13691
13692 /* As this DIE has a static linkage the name would be difficult
13693 to look up later. */
13694 language_of_main = language_fortran;
13695 }
68511cec 13696 break;
481860b3
GB
13697 case DW_AT_inline:
13698 if (DW_UNSND (&attr) == DW_INL_inlined
13699 || DW_UNSND (&attr) == DW_INL_declared_inlined)
13700 part_die->may_be_inlined = 1;
13701 break;
95554aad
TT
13702
13703 case DW_AT_import:
13704 if (part_die->tag == DW_TAG_imported_unit)
36586728
TT
13705 {
13706 part_die->d.offset = dwarf2_get_ref_die_offset (&attr);
13707 part_die->is_dwz = (attr.form == DW_FORM_GNU_ref_alt
13708 || cu->per_cu->is_dwz);
13709 }
95554aad
TT
13710 break;
13711
c906108c
SS
13712 default:
13713 break;
13714 }
13715 }
13716
91da1414
MW
13717 if (high_pc_relative)
13718 part_die->highpc += part_die->lowpc;
13719
9373cf26
JK
13720 if (has_low_pc_attr && has_high_pc_attr)
13721 {
13722 /* When using the GNU linker, .gnu.linkonce. sections are used to
13723 eliminate duplicate copies of functions and vtables and such.
13724 The linker will arbitrarily choose one and discard the others.
13725 The AT_*_pc values for such functions refer to local labels in
13726 these sections. If the section from that file was discarded, the
13727 labels are not in the output, so the relocs get a value of 0.
13728 If this is a discarded function, mark the pc bounds as invalid,
13729 so that GDB will ignore it. */
13730 if (part_die->lowpc == 0 && !dwarf2_per_objfile->has_section_at_zero)
13731 {
bb5ed363 13732 struct gdbarch *gdbarch = get_objfile_arch (objfile);
9373cf26
JK
13733
13734 complaint (&symfile_complaints,
13735 _("DW_AT_low_pc %s is zero "
13736 "for DIE at 0x%x [in module %s]"),
13737 paddress (gdbarch, part_die->lowpc),
b64f50a1 13738 part_die->offset.sect_off, objfile->name);
9373cf26
JK
13739 }
13740 /* dwarf2_get_pc_bounds has also the strict low < high requirement. */
13741 else if (part_die->lowpc >= part_die->highpc)
13742 {
bb5ed363 13743 struct gdbarch *gdbarch = get_objfile_arch (objfile);
9373cf26
JK
13744
13745 complaint (&symfile_complaints,
13746 _("DW_AT_low_pc %s is not < DW_AT_high_pc %s "
13747 "for DIE at 0x%x [in module %s]"),
13748 paddress (gdbarch, part_die->lowpc),
13749 paddress (gdbarch, part_die->highpc),
b64f50a1 13750 part_die->offset.sect_off, objfile->name);
9373cf26
JK
13751 }
13752 else
13753 part_die->has_pc_info = 1;
13754 }
85cbf3d3 13755
c906108c
SS
13756 return info_ptr;
13757}
13758
72bf9492
DJ
13759/* Find a cached partial DIE at OFFSET in CU. */
13760
13761static struct partial_die_info *
b64f50a1 13762find_partial_die_in_comp_unit (sect_offset offset, struct dwarf2_cu *cu)
72bf9492
DJ
13763{
13764 struct partial_die_info *lookup_die = NULL;
13765 struct partial_die_info part_die;
13766
13767 part_die.offset = offset;
b64f50a1
JK
13768 lookup_die = htab_find_with_hash (cu->partial_dies, &part_die,
13769 offset.sect_off);
72bf9492 13770
72bf9492
DJ
13771 return lookup_die;
13772}
13773
348e048f
DE
13774/* Find a partial DIE at OFFSET, which may or may not be in CU,
13775 except in the case of .debug_types DIEs which do not reference
13776 outside their CU (they do however referencing other types via
55f1336d 13777 DW_FORM_ref_sig8). */
72bf9492
DJ
13778
13779static struct partial_die_info *
36586728 13780find_partial_die (sect_offset offset, int offset_in_dwz, struct dwarf2_cu *cu)
72bf9492 13781{
bb5ed363 13782 struct objfile *objfile = cu->objfile;
5afb4e99
DJ
13783 struct dwarf2_per_cu_data *per_cu = NULL;
13784 struct partial_die_info *pd = NULL;
72bf9492 13785
36586728
TT
13786 if (offset_in_dwz == cu->per_cu->is_dwz
13787 && offset_in_cu_p (&cu->header, offset))
5afb4e99
DJ
13788 {
13789 pd = find_partial_die_in_comp_unit (offset, cu);
13790 if (pd != NULL)
13791 return pd;
0d99eb77
DE
13792 /* We missed recording what we needed.
13793 Load all dies and try again. */
13794 per_cu = cu->per_cu;
5afb4e99 13795 }
0d99eb77
DE
13796 else
13797 {
13798 /* TUs don't reference other CUs/TUs (except via type signatures). */
3019eac3 13799 if (cu->per_cu->is_debug_types)
0d99eb77
DE
13800 {
13801 error (_("Dwarf Error: Type Unit at offset 0x%lx contains"
13802 " external reference to offset 0x%lx [in module %s].\n"),
13803 (long) cu->header.offset.sect_off, (long) offset.sect_off,
13804 bfd_get_filename (objfile->obfd));
13805 }
36586728
TT
13806 per_cu = dwarf2_find_containing_comp_unit (offset, offset_in_dwz,
13807 objfile);
72bf9492 13808
0d99eb77
DE
13809 if (per_cu->cu == NULL || per_cu->cu->partial_dies == NULL)
13810 load_partial_comp_unit (per_cu);
ae038cb0 13811
0d99eb77
DE
13812 per_cu->cu->last_used = 0;
13813 pd = find_partial_die_in_comp_unit (offset, per_cu->cu);
13814 }
5afb4e99 13815
dee91e82
DE
13816 /* If we didn't find it, and not all dies have been loaded,
13817 load them all and try again. */
13818
5afb4e99
DJ
13819 if (pd == NULL && per_cu->load_all_dies == 0)
13820 {
5afb4e99 13821 per_cu->load_all_dies = 1;
fd820528
DE
13822
13823 /* This is nasty. When we reread the DIEs, somewhere up the call chain
13824 THIS_CU->cu may already be in use. So we can't just free it and
13825 replace its DIEs with the ones we read in. Instead, we leave those
13826 DIEs alone (which can still be in use, e.g. in scan_partial_symbols),
13827 and clobber THIS_CU->cu->partial_dies with the hash table for the new
13828 set. */
dee91e82 13829 load_partial_comp_unit (per_cu);
5afb4e99
DJ
13830
13831 pd = find_partial_die_in_comp_unit (offset, per_cu->cu);
13832 }
13833
13834 if (pd == NULL)
13835 internal_error (__FILE__, __LINE__,
3e43a32a
MS
13836 _("could not find partial DIE 0x%x "
13837 "in cache [from module %s]\n"),
b64f50a1 13838 offset.sect_off, bfd_get_filename (objfile->obfd));
5afb4e99 13839 return pd;
72bf9492
DJ
13840}
13841
abc72ce4
DE
13842/* See if we can figure out if the class lives in a namespace. We do
13843 this by looking for a member function; its demangled name will
13844 contain namespace info, if there is any. */
13845
13846static void
13847guess_partial_die_structure_name (struct partial_die_info *struct_pdi,
13848 struct dwarf2_cu *cu)
13849{
13850 /* NOTE: carlton/2003-10-07: Getting the info this way changes
13851 what template types look like, because the demangler
13852 frequently doesn't give the same name as the debug info. We
13853 could fix this by only using the demangled name to get the
13854 prefix (but see comment in read_structure_type). */
13855
13856 struct partial_die_info *real_pdi;
13857 struct partial_die_info *child_pdi;
13858
13859 /* If this DIE (this DIE's specification, if any) has a parent, then
13860 we should not do this. We'll prepend the parent's fully qualified
13861 name when we create the partial symbol. */
13862
13863 real_pdi = struct_pdi;
13864 while (real_pdi->has_specification)
36586728
TT
13865 real_pdi = find_partial_die (real_pdi->spec_offset,
13866 real_pdi->spec_is_dwz, cu);
abc72ce4
DE
13867
13868 if (real_pdi->die_parent != NULL)
13869 return;
13870
13871 for (child_pdi = struct_pdi->die_child;
13872 child_pdi != NULL;
13873 child_pdi = child_pdi->die_sibling)
13874 {
13875 if (child_pdi->tag == DW_TAG_subprogram
13876 && child_pdi->linkage_name != NULL)
13877 {
13878 char *actual_class_name
13879 = language_class_name_from_physname (cu->language_defn,
13880 child_pdi->linkage_name);
13881 if (actual_class_name != NULL)
13882 {
13883 struct_pdi->name
10f0c4bb
TT
13884 = obstack_copy0 (&cu->objfile->objfile_obstack,
13885 actual_class_name,
13886 strlen (actual_class_name));
abc72ce4
DE
13887 xfree (actual_class_name);
13888 }
13889 break;
13890 }
13891 }
13892}
13893
72bf9492
DJ
13894/* Adjust PART_DIE before generating a symbol for it. This function
13895 may set the is_external flag or change the DIE's name. */
13896
13897static void
13898fixup_partial_die (struct partial_die_info *part_die,
13899 struct dwarf2_cu *cu)
13900{
abc72ce4
DE
13901 /* Once we've fixed up a die, there's no point in doing so again.
13902 This also avoids a memory leak if we were to call
13903 guess_partial_die_structure_name multiple times. */
13904 if (part_die->fixup_called)
13905 return;
13906
72bf9492
DJ
13907 /* If we found a reference attribute and the DIE has no name, try
13908 to find a name in the referred to DIE. */
13909
13910 if (part_die->name == NULL && part_die->has_specification)
13911 {
13912 struct partial_die_info *spec_die;
72bf9492 13913
36586728
TT
13914 spec_die = find_partial_die (part_die->spec_offset,
13915 part_die->spec_is_dwz, cu);
72bf9492 13916
10b3939b 13917 fixup_partial_die (spec_die, cu);
72bf9492
DJ
13918
13919 if (spec_die->name)
13920 {
13921 part_die->name = spec_die->name;
13922
13923 /* Copy DW_AT_external attribute if it is set. */
13924 if (spec_die->is_external)
13925 part_die->is_external = spec_die->is_external;
13926 }
13927 }
13928
13929 /* Set default names for some unnamed DIEs. */
72bf9492
DJ
13930
13931 if (part_die->name == NULL && part_die->tag == DW_TAG_namespace)
2b1dbab0 13932 part_die->name = CP_ANONYMOUS_NAMESPACE_STR;
72bf9492 13933
abc72ce4
DE
13934 /* If there is no parent die to provide a namespace, and there are
13935 children, see if we can determine the namespace from their linkage
122d1940 13936 name. */
abc72ce4 13937 if (cu->language == language_cplus
8b70b953 13938 && !VEC_empty (dwarf2_section_info_def, dwarf2_per_objfile->types)
abc72ce4
DE
13939 && part_die->die_parent == NULL
13940 && part_die->has_children
13941 && (part_die->tag == DW_TAG_class_type
13942 || part_die->tag == DW_TAG_structure_type
13943 || part_die->tag == DW_TAG_union_type))
13944 guess_partial_die_structure_name (part_die, cu);
13945
53832f31
TT
13946 /* GCC might emit a nameless struct or union that has a linkage
13947 name. See http://gcc.gnu.org/bugzilla/show_bug.cgi?id=47510. */
13948 if (part_die->name == NULL
96408a79
SA
13949 && (part_die->tag == DW_TAG_class_type
13950 || part_die->tag == DW_TAG_interface_type
13951 || part_die->tag == DW_TAG_structure_type
13952 || part_die->tag == DW_TAG_union_type)
53832f31
TT
13953 && part_die->linkage_name != NULL)
13954 {
13955 char *demangled;
13956
13957 demangled = cplus_demangle (part_die->linkage_name, DMGL_TYPES);
13958 if (demangled)
13959 {
96408a79
SA
13960 const char *base;
13961
13962 /* Strip any leading namespaces/classes, keep only the base name.
13963 DW_AT_name for named DIEs does not contain the prefixes. */
13964 base = strrchr (demangled, ':');
13965 if (base && base > demangled && base[-1] == ':')
13966 base++;
13967 else
13968 base = demangled;
13969
10f0c4bb
TT
13970 part_die->name = obstack_copy0 (&cu->objfile->objfile_obstack,
13971 base, strlen (base));
53832f31
TT
13972 xfree (demangled);
13973 }
13974 }
13975
abc72ce4 13976 part_die->fixup_called = 1;
72bf9492
DJ
13977}
13978
a8329558 13979/* Read an attribute value described by an attribute form. */
c906108c 13980
fe1b8b76 13981static gdb_byte *
dee91e82
DE
13982read_attribute_value (const struct die_reader_specs *reader,
13983 struct attribute *attr, unsigned form,
13984 gdb_byte *info_ptr)
c906108c 13985{
dee91e82
DE
13986 struct dwarf2_cu *cu = reader->cu;
13987 bfd *abfd = reader->abfd;
e7c27a73 13988 struct comp_unit_head *cu_header = &cu->header;
c906108c
SS
13989 unsigned int bytes_read;
13990 struct dwarf_block *blk;
13991
a8329558
KW
13992 attr->form = form;
13993 switch (form)
c906108c 13994 {
c906108c 13995 case DW_FORM_ref_addr:
ae411497 13996 if (cu->header.version == 2)
4568ecf9 13997 DW_UNSND (attr) = read_address (abfd, info_ptr, cu, &bytes_read);
ae411497 13998 else
4568ecf9
DE
13999 DW_UNSND (attr) = read_offset (abfd, info_ptr,
14000 &cu->header, &bytes_read);
ae411497
TT
14001 info_ptr += bytes_read;
14002 break;
36586728
TT
14003 case DW_FORM_GNU_ref_alt:
14004 DW_UNSND (attr) = read_offset (abfd, info_ptr, &cu->header, &bytes_read);
14005 info_ptr += bytes_read;
14006 break;
ae411497 14007 case DW_FORM_addr:
e7c27a73 14008 DW_ADDR (attr) = read_address (abfd, info_ptr, cu, &bytes_read);
107d2387 14009 info_ptr += bytes_read;
c906108c
SS
14010 break;
14011 case DW_FORM_block2:
7b5a2f43 14012 blk = dwarf_alloc_block (cu);
c906108c
SS
14013 blk->size = read_2_bytes (abfd, info_ptr);
14014 info_ptr += 2;
14015 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
14016 info_ptr += blk->size;
14017 DW_BLOCK (attr) = blk;
14018 break;
14019 case DW_FORM_block4:
7b5a2f43 14020 blk = dwarf_alloc_block (cu);
c906108c
SS
14021 blk->size = read_4_bytes (abfd, info_ptr);
14022 info_ptr += 4;
14023 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
14024 info_ptr += blk->size;
14025 DW_BLOCK (attr) = blk;
14026 break;
14027 case DW_FORM_data2:
14028 DW_UNSND (attr) = read_2_bytes (abfd, info_ptr);
14029 info_ptr += 2;
14030 break;
14031 case DW_FORM_data4:
14032 DW_UNSND (attr) = read_4_bytes (abfd, info_ptr);
14033 info_ptr += 4;
14034 break;
14035 case DW_FORM_data8:
14036 DW_UNSND (attr) = read_8_bytes (abfd, info_ptr);
14037 info_ptr += 8;
14038 break;
2dc7f7b3
TT
14039 case DW_FORM_sec_offset:
14040 DW_UNSND (attr) = read_offset (abfd, info_ptr, &cu->header, &bytes_read);
14041 info_ptr += bytes_read;
14042 break;
c906108c 14043 case DW_FORM_string:
9b1c24c8 14044 DW_STRING (attr) = read_direct_string (abfd, info_ptr, &bytes_read);
8285870a 14045 DW_STRING_IS_CANONICAL (attr) = 0;
c906108c
SS
14046 info_ptr += bytes_read;
14047 break;
4bdf3d34 14048 case DW_FORM_strp:
36586728
TT
14049 if (!cu->per_cu->is_dwz)
14050 {
14051 DW_STRING (attr) = read_indirect_string (abfd, info_ptr, cu_header,
14052 &bytes_read);
14053 DW_STRING_IS_CANONICAL (attr) = 0;
14054 info_ptr += bytes_read;
14055 break;
14056 }
14057 /* FALLTHROUGH */
14058 case DW_FORM_GNU_strp_alt:
14059 {
14060 struct dwz_file *dwz = dwarf2_get_dwz_file ();
14061 LONGEST str_offset = read_offset (abfd, info_ptr, cu_header,
14062 &bytes_read);
14063
14064 DW_STRING (attr) = read_indirect_string_from_dwz (dwz, str_offset);
14065 DW_STRING_IS_CANONICAL (attr) = 0;
14066 info_ptr += bytes_read;
14067 }
4bdf3d34 14068 break;
2dc7f7b3 14069 case DW_FORM_exprloc:
c906108c 14070 case DW_FORM_block:
7b5a2f43 14071 blk = dwarf_alloc_block (cu);
c906108c
SS
14072 blk->size = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
14073 info_ptr += bytes_read;
14074 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
14075 info_ptr += blk->size;
14076 DW_BLOCK (attr) = blk;
14077 break;
14078 case DW_FORM_block1:
7b5a2f43 14079 blk = dwarf_alloc_block (cu);
c906108c
SS
14080 blk->size = read_1_byte (abfd, info_ptr);
14081 info_ptr += 1;
14082 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
14083 info_ptr += blk->size;
14084 DW_BLOCK (attr) = blk;
14085 break;
14086 case DW_FORM_data1:
14087 DW_UNSND (attr) = read_1_byte (abfd, info_ptr);
14088 info_ptr += 1;
14089 break;
14090 case DW_FORM_flag:
14091 DW_UNSND (attr) = read_1_byte (abfd, info_ptr);
14092 info_ptr += 1;
14093 break;
2dc7f7b3
TT
14094 case DW_FORM_flag_present:
14095 DW_UNSND (attr) = 1;
14096 break;
c906108c
SS
14097 case DW_FORM_sdata:
14098 DW_SND (attr) = read_signed_leb128 (abfd, info_ptr, &bytes_read);
14099 info_ptr += bytes_read;
14100 break;
14101 case DW_FORM_udata:
14102 DW_UNSND (attr) = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
14103 info_ptr += bytes_read;
14104 break;
14105 case DW_FORM_ref1:
4568ecf9
DE
14106 DW_UNSND (attr) = (cu->header.offset.sect_off
14107 + read_1_byte (abfd, info_ptr));
c906108c
SS
14108 info_ptr += 1;
14109 break;
14110 case DW_FORM_ref2:
4568ecf9
DE
14111 DW_UNSND (attr) = (cu->header.offset.sect_off
14112 + read_2_bytes (abfd, info_ptr));
c906108c
SS
14113 info_ptr += 2;
14114 break;
14115 case DW_FORM_ref4:
4568ecf9
DE
14116 DW_UNSND (attr) = (cu->header.offset.sect_off
14117 + read_4_bytes (abfd, info_ptr));
c906108c
SS
14118 info_ptr += 4;
14119 break;
613e1657 14120 case DW_FORM_ref8:
4568ecf9
DE
14121 DW_UNSND (attr) = (cu->header.offset.sect_off
14122 + read_8_bytes (abfd, info_ptr));
613e1657
KB
14123 info_ptr += 8;
14124 break;
55f1336d 14125 case DW_FORM_ref_sig8:
348e048f
DE
14126 /* Convert the signature to something we can record in DW_UNSND
14127 for later lookup.
14128 NOTE: This is NULL if the type wasn't found. */
14129 DW_SIGNATURED_TYPE (attr) =
e319fa28 14130 lookup_signatured_type (read_8_bytes (abfd, info_ptr));
348e048f
DE
14131 info_ptr += 8;
14132 break;
c906108c 14133 case DW_FORM_ref_udata:
4568ecf9
DE
14134 DW_UNSND (attr) = (cu->header.offset.sect_off
14135 + read_unsigned_leb128 (abfd, info_ptr, &bytes_read));
c906108c
SS
14136 info_ptr += bytes_read;
14137 break;
c906108c 14138 case DW_FORM_indirect:
a8329558
KW
14139 form = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
14140 info_ptr += bytes_read;
dee91e82 14141 info_ptr = read_attribute_value (reader, attr, form, info_ptr);
a8329558 14142 break;
3019eac3
DE
14143 case DW_FORM_GNU_addr_index:
14144 if (reader->dwo_file == NULL)
14145 {
14146 /* For now flag a hard error.
14147 Later we can turn this into a complaint. */
14148 error (_("Dwarf Error: %s found in non-DWO CU [in module %s]"),
14149 dwarf_form_name (form),
14150 bfd_get_filename (abfd));
14151 }
14152 DW_ADDR (attr) = read_addr_index_from_leb128 (cu, info_ptr, &bytes_read);
14153 info_ptr += bytes_read;
14154 break;
14155 case DW_FORM_GNU_str_index:
14156 if (reader->dwo_file == NULL)
14157 {
14158 /* For now flag a hard error.
14159 Later we can turn this into a complaint if warranted. */
14160 error (_("Dwarf Error: %s found in non-DWO CU [in module %s]"),
14161 dwarf_form_name (form),
14162 bfd_get_filename (abfd));
14163 }
14164 {
14165 ULONGEST str_index =
14166 read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
14167
14168 DW_STRING (attr) = read_str_index (reader, cu, str_index);
14169 DW_STRING_IS_CANONICAL (attr) = 0;
14170 info_ptr += bytes_read;
14171 }
14172 break;
c906108c 14173 default:
8a3fe4f8 14174 error (_("Dwarf Error: Cannot handle %s in DWARF reader [in module %s]"),
659b0389
ML
14175 dwarf_form_name (form),
14176 bfd_get_filename (abfd));
c906108c 14177 }
28e94949 14178
36586728
TT
14179 /* Super hack. */
14180 if (cu->per_cu->is_dwz && is_ref_attr (attr))
14181 attr->form = DW_FORM_GNU_ref_alt;
14182
28e94949
JB
14183 /* We have seen instances where the compiler tried to emit a byte
14184 size attribute of -1 which ended up being encoded as an unsigned
14185 0xffffffff. Although 0xffffffff is technically a valid size value,
14186 an object of this size seems pretty unlikely so we can relatively
14187 safely treat these cases as if the size attribute was invalid and
14188 treat them as zero by default. */
14189 if (attr->name == DW_AT_byte_size
14190 && form == DW_FORM_data4
14191 && DW_UNSND (attr) >= 0xffffffff)
01c66ae6
JB
14192 {
14193 complaint
14194 (&symfile_complaints,
43bbcdc2
PH
14195 _("Suspicious DW_AT_byte_size value treated as zero instead of %s"),
14196 hex_string (DW_UNSND (attr)));
01c66ae6
JB
14197 DW_UNSND (attr) = 0;
14198 }
28e94949 14199
c906108c
SS
14200 return info_ptr;
14201}
14202
a8329558
KW
14203/* Read an attribute described by an abbreviated attribute. */
14204
fe1b8b76 14205static gdb_byte *
dee91e82
DE
14206read_attribute (const struct die_reader_specs *reader,
14207 struct attribute *attr, struct attr_abbrev *abbrev,
14208 gdb_byte *info_ptr)
a8329558
KW
14209{
14210 attr->name = abbrev->name;
dee91e82 14211 return read_attribute_value (reader, attr, abbrev->form, info_ptr);
a8329558
KW
14212}
14213
0963b4bd 14214/* Read dwarf information from a buffer. */
c906108c
SS
14215
14216static unsigned int
a1855c1d 14217read_1_byte (bfd *abfd, const gdb_byte *buf)
c906108c 14218{
fe1b8b76 14219 return bfd_get_8 (abfd, buf);
c906108c
SS
14220}
14221
14222static int
a1855c1d 14223read_1_signed_byte (bfd *abfd, const gdb_byte *buf)
c906108c 14224{
fe1b8b76 14225 return bfd_get_signed_8 (abfd, buf);
c906108c
SS
14226}
14227
14228static unsigned int
a1855c1d 14229read_2_bytes (bfd *abfd, const gdb_byte *buf)
c906108c 14230{
fe1b8b76 14231 return bfd_get_16 (abfd, buf);
c906108c
SS
14232}
14233
21ae7a4d 14234static int
a1855c1d 14235read_2_signed_bytes (bfd *abfd, const gdb_byte *buf)
21ae7a4d
JK
14236{
14237 return bfd_get_signed_16 (abfd, buf);
14238}
14239
c906108c 14240static unsigned int
a1855c1d 14241read_4_bytes (bfd *abfd, const gdb_byte *buf)
c906108c 14242{
fe1b8b76 14243 return bfd_get_32 (abfd, buf);
c906108c
SS
14244}
14245
21ae7a4d 14246static int
a1855c1d 14247read_4_signed_bytes (bfd *abfd, const gdb_byte *buf)
21ae7a4d
JK
14248{
14249 return bfd_get_signed_32 (abfd, buf);
14250}
14251
93311388 14252static ULONGEST
a1855c1d 14253read_8_bytes (bfd *abfd, const gdb_byte *buf)
c906108c 14254{
fe1b8b76 14255 return bfd_get_64 (abfd, buf);
c906108c
SS
14256}
14257
14258static CORE_ADDR
fe1b8b76 14259read_address (bfd *abfd, gdb_byte *buf, struct dwarf2_cu *cu,
891d2f0b 14260 unsigned int *bytes_read)
c906108c 14261{
e7c27a73 14262 struct comp_unit_head *cu_header = &cu->header;
c906108c
SS
14263 CORE_ADDR retval = 0;
14264
107d2387 14265 if (cu_header->signed_addr_p)
c906108c 14266 {
107d2387
AC
14267 switch (cu_header->addr_size)
14268 {
14269 case 2:
fe1b8b76 14270 retval = bfd_get_signed_16 (abfd, buf);
107d2387
AC
14271 break;
14272 case 4:
fe1b8b76 14273 retval = bfd_get_signed_32 (abfd, buf);
107d2387
AC
14274 break;
14275 case 8:
fe1b8b76 14276 retval = bfd_get_signed_64 (abfd, buf);
107d2387
AC
14277 break;
14278 default:
8e65ff28 14279 internal_error (__FILE__, __LINE__,
e2e0b3e5 14280 _("read_address: bad switch, signed [in module %s]"),
659b0389 14281 bfd_get_filename (abfd));
107d2387
AC
14282 }
14283 }
14284 else
14285 {
14286 switch (cu_header->addr_size)
14287 {
14288 case 2:
fe1b8b76 14289 retval = bfd_get_16 (abfd, buf);
107d2387
AC
14290 break;
14291 case 4:
fe1b8b76 14292 retval = bfd_get_32 (abfd, buf);
107d2387
AC
14293 break;
14294 case 8:
fe1b8b76 14295 retval = bfd_get_64 (abfd, buf);
107d2387
AC
14296 break;
14297 default:
8e65ff28 14298 internal_error (__FILE__, __LINE__,
a73c6dcd
MS
14299 _("read_address: bad switch, "
14300 "unsigned [in module %s]"),
659b0389 14301 bfd_get_filename (abfd));
107d2387 14302 }
c906108c 14303 }
64367e0a 14304
107d2387
AC
14305 *bytes_read = cu_header->addr_size;
14306 return retval;
c906108c
SS
14307}
14308
f7ef9339 14309/* Read the initial length from a section. The (draft) DWARF 3
613e1657
KB
14310 specification allows the initial length to take up either 4 bytes
14311 or 12 bytes. If the first 4 bytes are 0xffffffff, then the next 8
14312 bytes describe the length and all offsets will be 8 bytes in length
14313 instead of 4.
14314
f7ef9339
KB
14315 An older, non-standard 64-bit format is also handled by this
14316 function. The older format in question stores the initial length
14317 as an 8-byte quantity without an escape value. Lengths greater
14318 than 2^32 aren't very common which means that the initial 4 bytes
14319 is almost always zero. Since a length value of zero doesn't make
14320 sense for the 32-bit format, this initial zero can be considered to
14321 be an escape value which indicates the presence of the older 64-bit
14322 format. As written, the code can't detect (old format) lengths
917c78fc
MK
14323 greater than 4GB. If it becomes necessary to handle lengths
14324 somewhat larger than 4GB, we could allow other small values (such
14325 as the non-sensical values of 1, 2, and 3) to also be used as
14326 escape values indicating the presence of the old format.
f7ef9339 14327
917c78fc
MK
14328 The value returned via bytes_read should be used to increment the
14329 relevant pointer after calling read_initial_length().
c764a876 14330
613e1657
KB
14331 [ Note: read_initial_length() and read_offset() are based on the
14332 document entitled "DWARF Debugging Information Format", revision
f7ef9339 14333 3, draft 8, dated November 19, 2001. This document was obtained
613e1657
KB
14334 from:
14335
f7ef9339 14336 http://reality.sgiweb.org/davea/dwarf3-draft8-011125.pdf
6e70227d 14337
613e1657
KB
14338 This document is only a draft and is subject to change. (So beware.)
14339
f7ef9339 14340 Details regarding the older, non-standard 64-bit format were
917c78fc
MK
14341 determined empirically by examining 64-bit ELF files produced by
14342 the SGI toolchain on an IRIX 6.5 machine.
f7ef9339
KB
14343
14344 - Kevin, July 16, 2002
613e1657
KB
14345 ] */
14346
14347static LONGEST
c764a876 14348read_initial_length (bfd *abfd, gdb_byte *buf, unsigned int *bytes_read)
613e1657 14349{
fe1b8b76 14350 LONGEST length = bfd_get_32 (abfd, buf);
613e1657 14351
dd373385 14352 if (length == 0xffffffff)
613e1657 14353 {
fe1b8b76 14354 length = bfd_get_64 (abfd, buf + 4);
613e1657 14355 *bytes_read = 12;
613e1657 14356 }
dd373385 14357 else if (length == 0)
f7ef9339 14358 {
dd373385 14359 /* Handle the (non-standard) 64-bit DWARF2 format used by IRIX. */
fe1b8b76 14360 length = bfd_get_64 (abfd, buf);
f7ef9339 14361 *bytes_read = 8;
f7ef9339 14362 }
613e1657
KB
14363 else
14364 {
14365 *bytes_read = 4;
613e1657
KB
14366 }
14367
c764a876
DE
14368 return length;
14369}
dd373385 14370
c764a876
DE
14371/* Cover function for read_initial_length.
14372 Returns the length of the object at BUF, and stores the size of the
14373 initial length in *BYTES_READ and stores the size that offsets will be in
14374 *OFFSET_SIZE.
14375 If the initial length size is not equivalent to that specified in
14376 CU_HEADER then issue a complaint.
14377 This is useful when reading non-comp-unit headers. */
dd373385 14378
c764a876
DE
14379static LONGEST
14380read_checked_initial_length_and_offset (bfd *abfd, gdb_byte *buf,
14381 const struct comp_unit_head *cu_header,
14382 unsigned int *bytes_read,
14383 unsigned int *offset_size)
14384{
14385 LONGEST length = read_initial_length (abfd, buf, bytes_read);
14386
14387 gdb_assert (cu_header->initial_length_size == 4
14388 || cu_header->initial_length_size == 8
14389 || cu_header->initial_length_size == 12);
14390
14391 if (cu_header->initial_length_size != *bytes_read)
14392 complaint (&symfile_complaints,
14393 _("intermixed 32-bit and 64-bit DWARF sections"));
dd373385 14394
c764a876 14395 *offset_size = (*bytes_read == 4) ? 4 : 8;
dd373385 14396 return length;
613e1657
KB
14397}
14398
14399/* Read an offset from the data stream. The size of the offset is
917c78fc 14400 given by cu_header->offset_size. */
613e1657
KB
14401
14402static LONGEST
fe1b8b76 14403read_offset (bfd *abfd, gdb_byte *buf, const struct comp_unit_head *cu_header,
891d2f0b 14404 unsigned int *bytes_read)
c764a876
DE
14405{
14406 LONGEST offset = read_offset_1 (abfd, buf, cu_header->offset_size);
9a619af0 14407
c764a876
DE
14408 *bytes_read = cu_header->offset_size;
14409 return offset;
14410}
14411
14412/* Read an offset from the data stream. */
14413
14414static LONGEST
14415read_offset_1 (bfd *abfd, gdb_byte *buf, unsigned int offset_size)
613e1657
KB
14416{
14417 LONGEST retval = 0;
14418
c764a876 14419 switch (offset_size)
613e1657
KB
14420 {
14421 case 4:
fe1b8b76 14422 retval = bfd_get_32 (abfd, buf);
613e1657
KB
14423 break;
14424 case 8:
fe1b8b76 14425 retval = bfd_get_64 (abfd, buf);
613e1657
KB
14426 break;
14427 default:
8e65ff28 14428 internal_error (__FILE__, __LINE__,
c764a876 14429 _("read_offset_1: bad switch [in module %s]"),
659b0389 14430 bfd_get_filename (abfd));
613e1657
KB
14431 }
14432
917c78fc 14433 return retval;
613e1657
KB
14434}
14435
fe1b8b76
JB
14436static gdb_byte *
14437read_n_bytes (bfd *abfd, gdb_byte *buf, unsigned int size)
c906108c
SS
14438{
14439 /* If the size of a host char is 8 bits, we can return a pointer
14440 to the buffer, otherwise we have to copy the data to a buffer
14441 allocated on the temporary obstack. */
4bdf3d34 14442 gdb_assert (HOST_CHAR_BIT == 8);
c906108c 14443 return buf;
c906108c
SS
14444}
14445
14446static char *
9b1c24c8 14447read_direct_string (bfd *abfd, gdb_byte *buf, unsigned int *bytes_read_ptr)
c906108c
SS
14448{
14449 /* If the size of a host char is 8 bits, we can return a pointer
14450 to the string, otherwise we have to copy the string to a buffer
14451 allocated on the temporary obstack. */
4bdf3d34 14452 gdb_assert (HOST_CHAR_BIT == 8);
c906108c
SS
14453 if (*buf == '\0')
14454 {
14455 *bytes_read_ptr = 1;
14456 return NULL;
14457 }
fe1b8b76
JB
14458 *bytes_read_ptr = strlen ((char *) buf) + 1;
14459 return (char *) buf;
4bdf3d34
JJ
14460}
14461
14462static char *
cf2c3c16 14463read_indirect_string_at_offset (bfd *abfd, LONGEST str_offset)
4bdf3d34 14464{
be391dca 14465 dwarf2_read_section (dwarf2_per_objfile->objfile, &dwarf2_per_objfile->str);
dce234bc 14466 if (dwarf2_per_objfile->str.buffer == NULL)
cf2c3c16
TT
14467 error (_("DW_FORM_strp used without .debug_str section [in module %s]"),
14468 bfd_get_filename (abfd));
dce234bc 14469 if (str_offset >= dwarf2_per_objfile->str.size)
cf2c3c16
TT
14470 error (_("DW_FORM_strp pointing outside of "
14471 ".debug_str section [in module %s]"),
14472 bfd_get_filename (abfd));
4bdf3d34 14473 gdb_assert (HOST_CHAR_BIT == 8);
dce234bc 14474 if (dwarf2_per_objfile->str.buffer[str_offset] == '\0')
4bdf3d34 14475 return NULL;
dce234bc 14476 return (char *) (dwarf2_per_objfile->str.buffer + str_offset);
c906108c
SS
14477}
14478
36586728
TT
14479/* Read a string at offset STR_OFFSET in the .debug_str section from
14480 the .dwz file DWZ. Throw an error if the offset is too large. If
14481 the string consists of a single NUL byte, return NULL; otherwise
14482 return a pointer to the string. */
14483
14484static char *
14485read_indirect_string_from_dwz (struct dwz_file *dwz, LONGEST str_offset)
14486{
14487 dwarf2_read_section (dwarf2_per_objfile->objfile, &dwz->str);
14488
14489 if (dwz->str.buffer == NULL)
14490 error (_("DW_FORM_GNU_strp_alt used without .debug_str "
14491 "section [in module %s]"),
14492 bfd_get_filename (dwz->dwz_bfd));
14493 if (str_offset >= dwz->str.size)
14494 error (_("DW_FORM_GNU_strp_alt pointing outside of "
14495 ".debug_str section [in module %s]"),
14496 bfd_get_filename (dwz->dwz_bfd));
14497 gdb_assert (HOST_CHAR_BIT == 8);
14498 if (dwz->str.buffer[str_offset] == '\0')
14499 return NULL;
14500 return (char *) (dwz->str.buffer + str_offset);
14501}
14502
cf2c3c16
TT
14503static char *
14504read_indirect_string (bfd *abfd, gdb_byte *buf,
14505 const struct comp_unit_head *cu_header,
14506 unsigned int *bytes_read_ptr)
14507{
14508 LONGEST str_offset = read_offset (abfd, buf, cu_header, bytes_read_ptr);
14509
14510 return read_indirect_string_at_offset (abfd, str_offset);
14511}
14512
12df843f 14513static ULONGEST
fe1b8b76 14514read_unsigned_leb128 (bfd *abfd, gdb_byte *buf, unsigned int *bytes_read_ptr)
c906108c 14515{
12df843f 14516 ULONGEST result;
ce5d95e1 14517 unsigned int num_read;
c906108c
SS
14518 int i, shift;
14519 unsigned char byte;
14520
14521 result = 0;
14522 shift = 0;
14523 num_read = 0;
14524 i = 0;
14525 while (1)
14526 {
fe1b8b76 14527 byte = bfd_get_8 (abfd, buf);
c906108c
SS
14528 buf++;
14529 num_read++;
12df843f 14530 result |= ((ULONGEST) (byte & 127) << shift);
c906108c
SS
14531 if ((byte & 128) == 0)
14532 {
14533 break;
14534 }
14535 shift += 7;
14536 }
14537 *bytes_read_ptr = num_read;
14538 return result;
14539}
14540
12df843f 14541static LONGEST
fe1b8b76 14542read_signed_leb128 (bfd *abfd, gdb_byte *buf, unsigned int *bytes_read_ptr)
c906108c 14543{
12df843f 14544 LONGEST result;
77e0b926 14545 int i, shift, num_read;
c906108c
SS
14546 unsigned char byte;
14547
14548 result = 0;
14549 shift = 0;
c906108c
SS
14550 num_read = 0;
14551 i = 0;
14552 while (1)
14553 {
fe1b8b76 14554 byte = bfd_get_8 (abfd, buf);
c906108c
SS
14555 buf++;
14556 num_read++;
12df843f 14557 result |= ((LONGEST) (byte & 127) << shift);
c906108c
SS
14558 shift += 7;
14559 if ((byte & 128) == 0)
14560 {
14561 break;
14562 }
14563 }
77e0b926 14564 if ((shift < 8 * sizeof (result)) && (byte & 0x40))
12df843f 14565 result |= -(((LONGEST) 1) << shift);
c906108c
SS
14566 *bytes_read_ptr = num_read;
14567 return result;
14568}
14569
3019eac3
DE
14570/* Given index ADDR_INDEX in .debug_addr, fetch the value.
14571 ADDR_BASE is the DW_AT_GNU_addr_base attribute or zero.
14572 ADDR_SIZE is the size of addresses from the CU header. */
14573
14574static CORE_ADDR
14575read_addr_index_1 (unsigned int addr_index, ULONGEST addr_base, int addr_size)
14576{
14577 struct objfile *objfile = dwarf2_per_objfile->objfile;
14578 bfd *abfd = objfile->obfd;
14579 const gdb_byte *info_ptr;
14580
14581 dwarf2_read_section (objfile, &dwarf2_per_objfile->addr);
14582 if (dwarf2_per_objfile->addr.buffer == NULL)
14583 error (_("DW_FORM_addr_index used without .debug_addr section [in module %s]"),
14584 objfile->name);
14585 if (addr_base + addr_index * addr_size >= dwarf2_per_objfile->addr.size)
14586 error (_("DW_FORM_addr_index pointing outside of "
14587 ".debug_addr section [in module %s]"),
14588 objfile->name);
14589 info_ptr = (dwarf2_per_objfile->addr.buffer
14590 + addr_base + addr_index * addr_size);
14591 if (addr_size == 4)
14592 return bfd_get_32 (abfd, info_ptr);
14593 else
14594 return bfd_get_64 (abfd, info_ptr);
14595}
14596
14597/* Given index ADDR_INDEX in .debug_addr, fetch the value. */
14598
14599static CORE_ADDR
14600read_addr_index (struct dwarf2_cu *cu, unsigned int addr_index)
14601{
14602 return read_addr_index_1 (addr_index, cu->addr_base, cu->header.addr_size);
14603}
14604
14605/* Given a pointer to an leb128 value, fetch the value from .debug_addr. */
14606
14607static CORE_ADDR
14608read_addr_index_from_leb128 (struct dwarf2_cu *cu, gdb_byte *info_ptr,
14609 unsigned int *bytes_read)
14610{
14611 bfd *abfd = cu->objfile->obfd;
14612 unsigned int addr_index = read_unsigned_leb128 (abfd, info_ptr, bytes_read);
14613
14614 return read_addr_index (cu, addr_index);
14615}
14616
14617/* Data structure to pass results from dwarf2_read_addr_index_reader
14618 back to dwarf2_read_addr_index. */
14619
14620struct dwarf2_read_addr_index_data
14621{
14622 ULONGEST addr_base;
14623 int addr_size;
14624};
14625
14626/* die_reader_func for dwarf2_read_addr_index. */
14627
14628static void
14629dwarf2_read_addr_index_reader (const struct die_reader_specs *reader,
14630 gdb_byte *info_ptr,
14631 struct die_info *comp_unit_die,
14632 int has_children,
14633 void *data)
14634{
14635 struct dwarf2_cu *cu = reader->cu;
14636 struct dwarf2_read_addr_index_data *aidata =
14637 (struct dwarf2_read_addr_index_data *) data;
14638
14639 aidata->addr_base = cu->addr_base;
14640 aidata->addr_size = cu->header.addr_size;
14641}
14642
14643/* Given an index in .debug_addr, fetch the value.
14644 NOTE: This can be called during dwarf expression evaluation,
14645 long after the debug information has been read, and thus per_cu->cu
14646 may no longer exist. */
14647
14648CORE_ADDR
14649dwarf2_read_addr_index (struct dwarf2_per_cu_data *per_cu,
14650 unsigned int addr_index)
14651{
14652 struct objfile *objfile = per_cu->objfile;
14653 struct dwarf2_cu *cu = per_cu->cu;
14654 ULONGEST addr_base;
14655 int addr_size;
14656
14657 /* This is intended to be called from outside this file. */
14658 dw2_setup (objfile);
14659
14660 /* We need addr_base and addr_size.
14661 If we don't have PER_CU->cu, we have to get it.
14662 Nasty, but the alternative is storing the needed info in PER_CU,
14663 which at this point doesn't seem justified: it's not clear how frequently
14664 it would get used and it would increase the size of every PER_CU.
14665 Entry points like dwarf2_per_cu_addr_size do a similar thing
14666 so we're not in uncharted territory here.
14667 Alas we need to be a bit more complicated as addr_base is contained
14668 in the DIE.
14669
14670 We don't need to read the entire CU(/TU).
14671 We just need the header and top level die.
a1b64ce1 14672
3019eac3 14673 IWBN to use the aging mechanism to let us lazily later discard the CU.
a1b64ce1 14674 For now we skip this optimization. */
3019eac3
DE
14675
14676 if (cu != NULL)
14677 {
14678 addr_base = cu->addr_base;
14679 addr_size = cu->header.addr_size;
14680 }
14681 else
14682 {
14683 struct dwarf2_read_addr_index_data aidata;
14684
a1b64ce1
DE
14685 /* Note: We can't use init_cutu_and_read_dies_simple here,
14686 we need addr_base. */
14687 init_cutu_and_read_dies (per_cu, NULL, 0, 0,
14688 dwarf2_read_addr_index_reader, &aidata);
3019eac3
DE
14689 addr_base = aidata.addr_base;
14690 addr_size = aidata.addr_size;
14691 }
14692
14693 return read_addr_index_1 (addr_index, addr_base, addr_size);
14694}
14695
14696/* Given a DW_AT_str_index, fetch the string. */
14697
14698static char *
14699read_str_index (const struct die_reader_specs *reader,
14700 struct dwarf2_cu *cu, ULONGEST str_index)
14701{
14702 struct objfile *objfile = dwarf2_per_objfile->objfile;
14703 const char *dwo_name = objfile->name;
14704 bfd *abfd = objfile->obfd;
14705 struct dwo_sections *sections = &reader->dwo_file->sections;
14706 gdb_byte *info_ptr;
14707 ULONGEST str_offset;
14708
14709 dwarf2_read_section (objfile, &sections->str);
14710 dwarf2_read_section (objfile, &sections->str_offsets);
14711 if (sections->str.buffer == NULL)
14712 error (_("DW_FORM_str_index used without .debug_str.dwo section"
14713 " in CU at offset 0x%lx [in module %s]"),
14714 (long) cu->header.offset.sect_off, dwo_name);
14715 if (sections->str_offsets.buffer == NULL)
14716 error (_("DW_FORM_str_index used without .debug_str_offsets.dwo section"
14717 " in CU at offset 0x%lx [in module %s]"),
14718 (long) cu->header.offset.sect_off, dwo_name);
14719 if (str_index * cu->header.offset_size >= sections->str_offsets.size)
14720 error (_("DW_FORM_str_index pointing outside of .debug_str_offsets.dwo"
14721 " section in CU at offset 0x%lx [in module %s]"),
14722 (long) cu->header.offset.sect_off, dwo_name);
14723 info_ptr = (sections->str_offsets.buffer
14724 + str_index * cu->header.offset_size);
14725 if (cu->header.offset_size == 4)
14726 str_offset = bfd_get_32 (abfd, info_ptr);
14727 else
14728 str_offset = bfd_get_64 (abfd, info_ptr);
14729 if (str_offset >= sections->str.size)
14730 error (_("Offset from DW_FORM_str_index pointing outside of"
14731 " .debug_str.dwo section in CU at offset 0x%lx [in module %s]"),
14732 (long) cu->header.offset.sect_off, dwo_name);
14733 return (char *) (sections->str.buffer + str_offset);
14734}
14735
3019eac3
DE
14736/* Return the length of an LEB128 number in BUF. */
14737
14738static int
14739leb128_size (const gdb_byte *buf)
14740{
14741 const gdb_byte *begin = buf;
14742 gdb_byte byte;
14743
14744 while (1)
14745 {
14746 byte = *buf++;
14747 if ((byte & 128) == 0)
14748 return buf - begin;
14749 }
14750}
14751
c906108c 14752static void
e142c38c 14753set_cu_language (unsigned int lang, struct dwarf2_cu *cu)
c906108c
SS
14754{
14755 switch (lang)
14756 {
14757 case DW_LANG_C89:
76bee0cc 14758 case DW_LANG_C99:
c906108c 14759 case DW_LANG_C:
e142c38c 14760 cu->language = language_c;
c906108c
SS
14761 break;
14762 case DW_LANG_C_plus_plus:
e142c38c 14763 cu->language = language_cplus;
c906108c 14764 break;
6aecb9c2
JB
14765 case DW_LANG_D:
14766 cu->language = language_d;
14767 break;
c906108c
SS
14768 case DW_LANG_Fortran77:
14769 case DW_LANG_Fortran90:
b21b22e0 14770 case DW_LANG_Fortran95:
e142c38c 14771 cu->language = language_fortran;
c906108c 14772 break;
a766d390
DE
14773 case DW_LANG_Go:
14774 cu->language = language_go;
14775 break;
c906108c 14776 case DW_LANG_Mips_Assembler:
e142c38c 14777 cu->language = language_asm;
c906108c 14778 break;
bebd888e 14779 case DW_LANG_Java:
e142c38c 14780 cu->language = language_java;
bebd888e 14781 break;
c906108c 14782 case DW_LANG_Ada83:
8aaf0b47 14783 case DW_LANG_Ada95:
bc5f45f8
JB
14784 cu->language = language_ada;
14785 break;
72019c9c
GM
14786 case DW_LANG_Modula2:
14787 cu->language = language_m2;
14788 break;
fe8e67fd
PM
14789 case DW_LANG_Pascal83:
14790 cu->language = language_pascal;
14791 break;
22566fbd
DJ
14792 case DW_LANG_ObjC:
14793 cu->language = language_objc;
14794 break;
c906108c
SS
14795 case DW_LANG_Cobol74:
14796 case DW_LANG_Cobol85:
c906108c 14797 default:
e142c38c 14798 cu->language = language_minimal;
c906108c
SS
14799 break;
14800 }
e142c38c 14801 cu->language_defn = language_def (cu->language);
c906108c
SS
14802}
14803
14804/* Return the named attribute or NULL if not there. */
14805
14806static struct attribute *
e142c38c 14807dwarf2_attr (struct die_info *die, unsigned int name, struct dwarf2_cu *cu)
c906108c 14808{
a48e046c 14809 for (;;)
c906108c 14810 {
a48e046c
TT
14811 unsigned int i;
14812 struct attribute *spec = NULL;
14813
14814 for (i = 0; i < die->num_attrs; ++i)
14815 {
14816 if (die->attrs[i].name == name)
14817 return &die->attrs[i];
14818 if (die->attrs[i].name == DW_AT_specification
14819 || die->attrs[i].name == DW_AT_abstract_origin)
14820 spec = &die->attrs[i];
14821 }
14822
14823 if (!spec)
14824 break;
c906108c 14825
f2f0e013 14826 die = follow_die_ref (die, spec, &cu);
f2f0e013 14827 }
c5aa993b 14828
c906108c
SS
14829 return NULL;
14830}
14831
348e048f
DE
14832/* Return the named attribute or NULL if not there,
14833 but do not follow DW_AT_specification, etc.
14834 This is for use in contexts where we're reading .debug_types dies.
14835 Following DW_AT_specification, DW_AT_abstract_origin will take us
14836 back up the chain, and we want to go down. */
14837
14838static struct attribute *
45e58e77 14839dwarf2_attr_no_follow (struct die_info *die, unsigned int name)
348e048f
DE
14840{
14841 unsigned int i;
14842
14843 for (i = 0; i < die->num_attrs; ++i)
14844 if (die->attrs[i].name == name)
14845 return &die->attrs[i];
14846
14847 return NULL;
14848}
14849
05cf31d1
JB
14850/* Return non-zero iff the attribute NAME is defined for the given DIE,
14851 and holds a non-zero value. This function should only be used for
2dc7f7b3 14852 DW_FORM_flag or DW_FORM_flag_present attributes. */
05cf31d1
JB
14853
14854static int
14855dwarf2_flag_true_p (struct die_info *die, unsigned name, struct dwarf2_cu *cu)
14856{
14857 struct attribute *attr = dwarf2_attr (die, name, cu);
14858
14859 return (attr && DW_UNSND (attr));
14860}
14861
3ca72b44 14862static int
e142c38c 14863die_is_declaration (struct die_info *die, struct dwarf2_cu *cu)
3ca72b44 14864{
05cf31d1
JB
14865 /* A DIE is a declaration if it has a DW_AT_declaration attribute
14866 which value is non-zero. However, we have to be careful with
14867 DIEs having a DW_AT_specification attribute, because dwarf2_attr()
14868 (via dwarf2_flag_true_p) follows this attribute. So we may
14869 end up accidently finding a declaration attribute that belongs
14870 to a different DIE referenced by the specification attribute,
14871 even though the given DIE does not have a declaration attribute. */
14872 return (dwarf2_flag_true_p (die, DW_AT_declaration, cu)
14873 && dwarf2_attr (die, DW_AT_specification, cu) == NULL);
3ca72b44
AC
14874}
14875
63d06c5c 14876/* Return the die giving the specification for DIE, if there is
f2f0e013 14877 one. *SPEC_CU is the CU containing DIE on input, and the CU
edb3359d
DJ
14878 containing the return value on output. If there is no
14879 specification, but there is an abstract origin, that is
14880 returned. */
63d06c5c
DC
14881
14882static struct die_info *
f2f0e013 14883die_specification (struct die_info *die, struct dwarf2_cu **spec_cu)
63d06c5c 14884{
f2f0e013
DJ
14885 struct attribute *spec_attr = dwarf2_attr (die, DW_AT_specification,
14886 *spec_cu);
63d06c5c 14887
edb3359d
DJ
14888 if (spec_attr == NULL)
14889 spec_attr = dwarf2_attr (die, DW_AT_abstract_origin, *spec_cu);
14890
63d06c5c
DC
14891 if (spec_attr == NULL)
14892 return NULL;
14893 else
f2f0e013 14894 return follow_die_ref (die, spec_attr, spec_cu);
63d06c5c 14895}
c906108c 14896
debd256d 14897/* Free the line_header structure *LH, and any arrays and strings it
ae2de4f8
DE
14898 refers to.
14899 NOTE: This is also used as a "cleanup" function. */
14900
debd256d
JB
14901static void
14902free_line_header (struct line_header *lh)
14903{
14904 if (lh->standard_opcode_lengths)
a8bc7b56 14905 xfree (lh->standard_opcode_lengths);
debd256d
JB
14906
14907 /* Remember that all the lh->file_names[i].name pointers are
14908 pointers into debug_line_buffer, and don't need to be freed. */
14909 if (lh->file_names)
a8bc7b56 14910 xfree (lh->file_names);
debd256d
JB
14911
14912 /* Similarly for the include directory names. */
14913 if (lh->include_dirs)
a8bc7b56 14914 xfree (lh->include_dirs);
debd256d 14915
a8bc7b56 14916 xfree (lh);
debd256d
JB
14917}
14918
debd256d 14919/* Add an entry to LH's include directory table. */
ae2de4f8 14920
debd256d
JB
14921static void
14922add_include_dir (struct line_header *lh, char *include_dir)
c906108c 14923{
debd256d
JB
14924 /* Grow the array if necessary. */
14925 if (lh->include_dirs_size == 0)
c5aa993b 14926 {
debd256d
JB
14927 lh->include_dirs_size = 1; /* for testing */
14928 lh->include_dirs = xmalloc (lh->include_dirs_size
14929 * sizeof (*lh->include_dirs));
14930 }
14931 else if (lh->num_include_dirs >= lh->include_dirs_size)
14932 {
14933 lh->include_dirs_size *= 2;
14934 lh->include_dirs = xrealloc (lh->include_dirs,
14935 (lh->include_dirs_size
14936 * sizeof (*lh->include_dirs)));
c5aa993b 14937 }
c906108c 14938
debd256d
JB
14939 lh->include_dirs[lh->num_include_dirs++] = include_dir;
14940}
6e70227d 14941
debd256d 14942/* Add an entry to LH's file name table. */
ae2de4f8 14943
debd256d
JB
14944static void
14945add_file_name (struct line_header *lh,
14946 char *name,
14947 unsigned int dir_index,
14948 unsigned int mod_time,
14949 unsigned int length)
14950{
14951 struct file_entry *fe;
14952
14953 /* Grow the array if necessary. */
14954 if (lh->file_names_size == 0)
14955 {
14956 lh->file_names_size = 1; /* for testing */
14957 lh->file_names = xmalloc (lh->file_names_size
14958 * sizeof (*lh->file_names));
14959 }
14960 else if (lh->num_file_names >= lh->file_names_size)
14961 {
14962 lh->file_names_size *= 2;
14963 lh->file_names = xrealloc (lh->file_names,
14964 (lh->file_names_size
14965 * sizeof (*lh->file_names)));
14966 }
14967
14968 fe = &lh->file_names[lh->num_file_names++];
14969 fe->name = name;
14970 fe->dir_index = dir_index;
14971 fe->mod_time = mod_time;
14972 fe->length = length;
aaa75496 14973 fe->included_p = 0;
cb1df416 14974 fe->symtab = NULL;
debd256d 14975}
6e70227d 14976
36586728
TT
14977/* A convenience function to find the proper .debug_line section for a
14978 CU. */
14979
14980static struct dwarf2_section_info *
14981get_debug_line_section (struct dwarf2_cu *cu)
14982{
14983 struct dwarf2_section_info *section;
14984
14985 /* For TUs in DWO files, the DW_AT_stmt_list attribute lives in the
14986 DWO file. */
14987 if (cu->dwo_unit && cu->per_cu->is_debug_types)
14988 section = &cu->dwo_unit->dwo_file->sections.line;
14989 else if (cu->per_cu->is_dwz)
14990 {
14991 struct dwz_file *dwz = dwarf2_get_dwz_file ();
14992
14993 section = &dwz->line;
14994 }
14995 else
14996 section = &dwarf2_per_objfile->line;
14997
14998 return section;
14999}
15000
debd256d 15001/* Read the statement program header starting at OFFSET in
3019eac3 15002 .debug_line, or .debug_line.dwo. Return a pointer
6502dd73 15003 to a struct line_header, allocated using xmalloc.
debd256d
JB
15004
15005 NOTE: the strings in the include directory and file name tables of
3019eac3
DE
15006 the returned object point into the dwarf line section buffer,
15007 and must not be freed. */
ae2de4f8 15008
debd256d 15009static struct line_header *
3019eac3 15010dwarf_decode_line_header (unsigned int offset, struct dwarf2_cu *cu)
debd256d
JB
15011{
15012 struct cleanup *back_to;
15013 struct line_header *lh;
fe1b8b76 15014 gdb_byte *line_ptr;
c764a876 15015 unsigned int bytes_read, offset_size;
debd256d
JB
15016 int i;
15017 char *cur_dir, *cur_file;
3019eac3
DE
15018 struct dwarf2_section_info *section;
15019 bfd *abfd;
15020
36586728 15021 section = get_debug_line_section (cu);
3019eac3
DE
15022 dwarf2_read_section (dwarf2_per_objfile->objfile, section);
15023 if (section->buffer == NULL)
debd256d 15024 {
3019eac3
DE
15025 if (cu->dwo_unit && cu->per_cu->is_debug_types)
15026 complaint (&symfile_complaints, _("missing .debug_line.dwo section"));
15027 else
15028 complaint (&symfile_complaints, _("missing .debug_line section"));
debd256d
JB
15029 return 0;
15030 }
15031
fceca515
DE
15032 /* We can't do this until we know the section is non-empty.
15033 Only then do we know we have such a section. */
15034 abfd = section->asection->owner;
15035
a738430d
MK
15036 /* Make sure that at least there's room for the total_length field.
15037 That could be 12 bytes long, but we're just going to fudge that. */
3019eac3 15038 if (offset + 4 >= section->size)
debd256d 15039 {
4d3c2250 15040 dwarf2_statement_list_fits_in_line_number_section_complaint ();
debd256d
JB
15041 return 0;
15042 }
15043
15044 lh = xmalloc (sizeof (*lh));
15045 memset (lh, 0, sizeof (*lh));
15046 back_to = make_cleanup ((make_cleanup_ftype *) free_line_header,
15047 (void *) lh);
15048
3019eac3 15049 line_ptr = section->buffer + offset;
debd256d 15050
a738430d 15051 /* Read in the header. */
6e70227d 15052 lh->total_length =
c764a876
DE
15053 read_checked_initial_length_and_offset (abfd, line_ptr, &cu->header,
15054 &bytes_read, &offset_size);
debd256d 15055 line_ptr += bytes_read;
3019eac3 15056 if (line_ptr + lh->total_length > (section->buffer + section->size))
debd256d 15057 {
4d3c2250 15058 dwarf2_statement_list_fits_in_line_number_section_complaint ();
debd256d
JB
15059 return 0;
15060 }
15061 lh->statement_program_end = line_ptr + lh->total_length;
15062 lh->version = read_2_bytes (abfd, line_ptr);
15063 line_ptr += 2;
c764a876
DE
15064 lh->header_length = read_offset_1 (abfd, line_ptr, offset_size);
15065 line_ptr += offset_size;
debd256d
JB
15066 lh->minimum_instruction_length = read_1_byte (abfd, line_ptr);
15067 line_ptr += 1;
2dc7f7b3
TT
15068 if (lh->version >= 4)
15069 {
15070 lh->maximum_ops_per_instruction = read_1_byte (abfd, line_ptr);
15071 line_ptr += 1;
15072 }
15073 else
15074 lh->maximum_ops_per_instruction = 1;
15075
15076 if (lh->maximum_ops_per_instruction == 0)
15077 {
15078 lh->maximum_ops_per_instruction = 1;
15079 complaint (&symfile_complaints,
3e43a32a
MS
15080 _("invalid maximum_ops_per_instruction "
15081 "in `.debug_line' section"));
2dc7f7b3
TT
15082 }
15083
debd256d
JB
15084 lh->default_is_stmt = read_1_byte (abfd, line_ptr);
15085 line_ptr += 1;
15086 lh->line_base = read_1_signed_byte (abfd, line_ptr);
15087 line_ptr += 1;
15088 lh->line_range = read_1_byte (abfd, line_ptr);
15089 line_ptr += 1;
15090 lh->opcode_base = read_1_byte (abfd, line_ptr);
15091 line_ptr += 1;
15092 lh->standard_opcode_lengths
fe1b8b76 15093 = xmalloc (lh->opcode_base * sizeof (lh->standard_opcode_lengths[0]));
debd256d
JB
15094
15095 lh->standard_opcode_lengths[0] = 1; /* This should never be used anyway. */
15096 for (i = 1; i < lh->opcode_base; ++i)
15097 {
15098 lh->standard_opcode_lengths[i] = read_1_byte (abfd, line_ptr);
15099 line_ptr += 1;
15100 }
15101
a738430d 15102 /* Read directory table. */
9b1c24c8 15103 while ((cur_dir = read_direct_string (abfd, line_ptr, &bytes_read)) != NULL)
debd256d
JB
15104 {
15105 line_ptr += bytes_read;
15106 add_include_dir (lh, cur_dir);
15107 }
15108 line_ptr += bytes_read;
15109
a738430d 15110 /* Read file name table. */
9b1c24c8 15111 while ((cur_file = read_direct_string (abfd, line_ptr, &bytes_read)) != NULL)
debd256d
JB
15112 {
15113 unsigned int dir_index, mod_time, length;
15114
15115 line_ptr += bytes_read;
15116 dir_index = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
15117 line_ptr += bytes_read;
15118 mod_time = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
15119 line_ptr += bytes_read;
15120 length = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
15121 line_ptr += bytes_read;
15122
15123 add_file_name (lh, cur_file, dir_index, mod_time, length);
15124 }
15125 line_ptr += bytes_read;
6e70227d 15126 lh->statement_program_start = line_ptr;
debd256d 15127
3019eac3 15128 if (line_ptr > (section->buffer + section->size))
4d3c2250 15129 complaint (&symfile_complaints,
3e43a32a
MS
15130 _("line number info header doesn't "
15131 "fit in `.debug_line' section"));
debd256d
JB
15132
15133 discard_cleanups (back_to);
15134 return lh;
15135}
c906108c 15136
c6da4cef
DE
15137/* Subroutine of dwarf_decode_lines to simplify it.
15138 Return the file name of the psymtab for included file FILE_INDEX
15139 in line header LH of PST.
15140 COMP_DIR is the compilation directory (DW_AT_comp_dir) or NULL if unknown.
15141 If space for the result is malloc'd, it will be freed by a cleanup.
1ed59174
JK
15142 Returns NULL if FILE_INDEX should be ignored, i.e., it is pst->filename.
15143
15144 The function creates dangling cleanup registration. */
c6da4cef
DE
15145
15146static char *
15147psymtab_include_file_name (const struct line_header *lh, int file_index,
15148 const struct partial_symtab *pst,
15149 const char *comp_dir)
15150{
15151 const struct file_entry fe = lh->file_names [file_index];
15152 char *include_name = fe.name;
15153 char *include_name_to_compare = include_name;
15154 char *dir_name = NULL;
72b9f47f
TT
15155 const char *pst_filename;
15156 char *copied_name = NULL;
c6da4cef
DE
15157 int file_is_pst;
15158
15159 if (fe.dir_index)
15160 dir_name = lh->include_dirs[fe.dir_index - 1];
15161
15162 if (!IS_ABSOLUTE_PATH (include_name)
15163 && (dir_name != NULL || comp_dir != NULL))
15164 {
15165 /* Avoid creating a duplicate psymtab for PST.
15166 We do this by comparing INCLUDE_NAME and PST_FILENAME.
15167 Before we do the comparison, however, we need to account
15168 for DIR_NAME and COMP_DIR.
15169 First prepend dir_name (if non-NULL). If we still don't
15170 have an absolute path prepend comp_dir (if non-NULL).
15171 However, the directory we record in the include-file's
15172 psymtab does not contain COMP_DIR (to match the
15173 corresponding symtab(s)).
15174
15175 Example:
15176
15177 bash$ cd /tmp
15178 bash$ gcc -g ./hello.c
15179 include_name = "hello.c"
15180 dir_name = "."
15181 DW_AT_comp_dir = comp_dir = "/tmp"
15182 DW_AT_name = "./hello.c" */
15183
15184 if (dir_name != NULL)
15185 {
15186 include_name = concat (dir_name, SLASH_STRING,
15187 include_name, (char *)NULL);
15188 include_name_to_compare = include_name;
15189 make_cleanup (xfree, include_name);
15190 }
15191 if (!IS_ABSOLUTE_PATH (include_name) && comp_dir != NULL)
15192 {
15193 include_name_to_compare = concat (comp_dir, SLASH_STRING,
15194 include_name, (char *)NULL);
15195 }
15196 }
15197
15198 pst_filename = pst->filename;
15199 if (!IS_ABSOLUTE_PATH (pst_filename) && pst->dirname != NULL)
15200 {
72b9f47f
TT
15201 copied_name = concat (pst->dirname, SLASH_STRING,
15202 pst_filename, (char *)NULL);
15203 pst_filename = copied_name;
c6da4cef
DE
15204 }
15205
1e3fad37 15206 file_is_pst = FILENAME_CMP (include_name_to_compare, pst_filename) == 0;
c6da4cef
DE
15207
15208 if (include_name_to_compare != include_name)
15209 xfree (include_name_to_compare);
72b9f47f
TT
15210 if (copied_name != NULL)
15211 xfree (copied_name);
c6da4cef
DE
15212
15213 if (file_is_pst)
15214 return NULL;
15215 return include_name;
15216}
15217
c91513d8
PP
15218/* Ignore this record_line request. */
15219
15220static void
15221noop_record_line (struct subfile *subfile, int line, CORE_ADDR pc)
15222{
15223 return;
15224}
15225
f3f5162e
DE
15226/* Subroutine of dwarf_decode_lines to simplify it.
15227 Process the line number information in LH. */
debd256d 15228
c906108c 15229static void
f3f5162e
DE
15230dwarf_decode_lines_1 (struct line_header *lh, const char *comp_dir,
15231 struct dwarf2_cu *cu, struct partial_symtab *pst)
c906108c 15232{
a8c50c1f 15233 gdb_byte *line_ptr, *extended_end;
fe1b8b76 15234 gdb_byte *line_end;
a8c50c1f 15235 unsigned int bytes_read, extended_len;
c906108c 15236 unsigned char op_code, extended_op, adj_opcode;
e142c38c
DJ
15237 CORE_ADDR baseaddr;
15238 struct objfile *objfile = cu->objfile;
f3f5162e 15239 bfd *abfd = objfile->obfd;
fbf65064 15240 struct gdbarch *gdbarch = get_objfile_arch (objfile);
aaa75496 15241 const int decode_for_pst_p = (pst != NULL);
f3f5162e 15242 struct subfile *last_subfile = NULL;
c91513d8
PP
15243 void (*p_record_line) (struct subfile *subfile, int line, CORE_ADDR pc)
15244 = record_line;
e142c38c
DJ
15245
15246 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
c906108c 15247
debd256d
JB
15248 line_ptr = lh->statement_program_start;
15249 line_end = lh->statement_program_end;
c906108c
SS
15250
15251 /* Read the statement sequences until there's nothing left. */
15252 while (line_ptr < line_end)
15253 {
15254 /* state machine registers */
15255 CORE_ADDR address = 0;
15256 unsigned int file = 1;
15257 unsigned int line = 1;
15258 unsigned int column = 0;
debd256d 15259 int is_stmt = lh->default_is_stmt;
c906108c
SS
15260 int basic_block = 0;
15261 int end_sequence = 0;
fbf65064 15262 CORE_ADDR addr;
2dc7f7b3 15263 unsigned char op_index = 0;
c906108c 15264
aaa75496 15265 if (!decode_for_pst_p && lh->num_file_names >= file)
c906108c 15266 {
aaa75496 15267 /* Start a subfile for the current file of the state machine. */
debd256d
JB
15268 /* lh->include_dirs and lh->file_names are 0-based, but the
15269 directory and file name numbers in the statement program
15270 are 1-based. */
15271 struct file_entry *fe = &lh->file_names[file - 1];
4f1520fb 15272 char *dir = NULL;
a738430d 15273
debd256d
JB
15274 if (fe->dir_index)
15275 dir = lh->include_dirs[fe->dir_index - 1];
4f1520fb
FR
15276
15277 dwarf2_start_subfile (fe->name, dir, comp_dir);
c906108c
SS
15278 }
15279
a738430d 15280 /* Decode the table. */
c5aa993b 15281 while (!end_sequence)
c906108c
SS
15282 {
15283 op_code = read_1_byte (abfd, line_ptr);
15284 line_ptr += 1;
59205f5a
JB
15285 if (line_ptr > line_end)
15286 {
15287 dwarf2_debug_line_missing_end_sequence_complaint ();
15288 break;
15289 }
9aa1fe7e 15290
debd256d 15291 if (op_code >= lh->opcode_base)
6e70227d 15292 {
a738430d 15293 /* Special operand. */
debd256d 15294 adj_opcode = op_code - lh->opcode_base;
2dc7f7b3
TT
15295 address += (((op_index + (adj_opcode / lh->line_range))
15296 / lh->maximum_ops_per_instruction)
15297 * lh->minimum_instruction_length);
15298 op_index = ((op_index + (adj_opcode / lh->line_range))
15299 % lh->maximum_ops_per_instruction);
debd256d 15300 line += lh->line_base + (adj_opcode % lh->line_range);
59205f5a 15301 if (lh->num_file_names < file || file == 0)
25e43795 15302 dwarf2_debug_line_missing_file_complaint ();
2dc7f7b3
TT
15303 /* For now we ignore lines not starting on an
15304 instruction boundary. */
15305 else if (op_index == 0)
25e43795
DJ
15306 {
15307 lh->file_names[file - 1].included_p = 1;
ca5f395d 15308 if (!decode_for_pst_p && is_stmt)
fbf65064
UW
15309 {
15310 if (last_subfile != current_subfile)
15311 {
15312 addr = gdbarch_addr_bits_remove (gdbarch, address);
15313 if (last_subfile)
c91513d8 15314 (*p_record_line) (last_subfile, 0, addr);
fbf65064
UW
15315 last_subfile = current_subfile;
15316 }
25e43795 15317 /* Append row to matrix using current values. */
7019d805 15318 addr = gdbarch_addr_bits_remove (gdbarch, address);
c91513d8 15319 (*p_record_line) (current_subfile, line, addr);
366da635 15320 }
25e43795 15321 }
ca5f395d 15322 basic_block = 0;
9aa1fe7e
GK
15323 }
15324 else switch (op_code)
c906108c
SS
15325 {
15326 case DW_LNS_extended_op:
3e43a32a
MS
15327 extended_len = read_unsigned_leb128 (abfd, line_ptr,
15328 &bytes_read);
473b7be6 15329 line_ptr += bytes_read;
a8c50c1f 15330 extended_end = line_ptr + extended_len;
c906108c
SS
15331 extended_op = read_1_byte (abfd, line_ptr);
15332 line_ptr += 1;
15333 switch (extended_op)
15334 {
15335 case DW_LNE_end_sequence:
c91513d8 15336 p_record_line = record_line;
c906108c 15337 end_sequence = 1;
c906108c
SS
15338 break;
15339 case DW_LNE_set_address:
e7c27a73 15340 address = read_address (abfd, line_ptr, cu, &bytes_read);
c91513d8
PP
15341
15342 if (address == 0 && !dwarf2_per_objfile->has_section_at_zero)
15343 {
15344 /* This line table is for a function which has been
15345 GCd by the linker. Ignore it. PR gdb/12528 */
15346
15347 long line_offset
36586728 15348 = line_ptr - get_debug_line_section (cu)->buffer;
c91513d8
PP
15349
15350 complaint (&symfile_complaints,
15351 _(".debug_line address at offset 0x%lx is 0 "
15352 "[in module %s]"),
bb5ed363 15353 line_offset, objfile->name);
c91513d8
PP
15354 p_record_line = noop_record_line;
15355 }
15356
2dc7f7b3 15357 op_index = 0;
107d2387
AC
15358 line_ptr += bytes_read;
15359 address += baseaddr;
c906108c
SS
15360 break;
15361 case DW_LNE_define_file:
debd256d
JB
15362 {
15363 char *cur_file;
15364 unsigned int dir_index, mod_time, length;
6e70227d 15365
3e43a32a
MS
15366 cur_file = read_direct_string (abfd, line_ptr,
15367 &bytes_read);
debd256d
JB
15368 line_ptr += bytes_read;
15369 dir_index =
15370 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
15371 line_ptr += bytes_read;
15372 mod_time =
15373 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
15374 line_ptr += bytes_read;
15375 length =
15376 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
15377 line_ptr += bytes_read;
15378 add_file_name (lh, cur_file, dir_index, mod_time, length);
15379 }
c906108c 15380 break;
d0c6ba3d
CC
15381 case DW_LNE_set_discriminator:
15382 /* The discriminator is not interesting to the debugger;
15383 just ignore it. */
15384 line_ptr = extended_end;
15385 break;
c906108c 15386 default:
4d3c2250 15387 complaint (&symfile_complaints,
e2e0b3e5 15388 _("mangled .debug_line section"));
debd256d 15389 return;
c906108c 15390 }
a8c50c1f
DJ
15391 /* Make sure that we parsed the extended op correctly. If e.g.
15392 we expected a different address size than the producer used,
15393 we may have read the wrong number of bytes. */
15394 if (line_ptr != extended_end)
15395 {
15396 complaint (&symfile_complaints,
15397 _("mangled .debug_line section"));
15398 return;
15399 }
c906108c
SS
15400 break;
15401 case DW_LNS_copy:
59205f5a 15402 if (lh->num_file_names < file || file == 0)
25e43795
DJ
15403 dwarf2_debug_line_missing_file_complaint ();
15404 else
366da635 15405 {
25e43795 15406 lh->file_names[file - 1].included_p = 1;
ca5f395d 15407 if (!decode_for_pst_p && is_stmt)
fbf65064
UW
15408 {
15409 if (last_subfile != current_subfile)
15410 {
15411 addr = gdbarch_addr_bits_remove (gdbarch, address);
15412 if (last_subfile)
c91513d8 15413 (*p_record_line) (last_subfile, 0, addr);
fbf65064
UW
15414 last_subfile = current_subfile;
15415 }
7019d805 15416 addr = gdbarch_addr_bits_remove (gdbarch, address);
c91513d8 15417 (*p_record_line) (current_subfile, line, addr);
fbf65064 15418 }
366da635 15419 }
c906108c
SS
15420 basic_block = 0;
15421 break;
15422 case DW_LNS_advance_pc:
2dc7f7b3
TT
15423 {
15424 CORE_ADDR adjust
15425 = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
15426
15427 address += (((op_index + adjust)
15428 / lh->maximum_ops_per_instruction)
15429 * lh->minimum_instruction_length);
15430 op_index = ((op_index + adjust)
15431 % lh->maximum_ops_per_instruction);
15432 line_ptr += bytes_read;
15433 }
c906108c
SS
15434 break;
15435 case DW_LNS_advance_line:
15436 line += read_signed_leb128 (abfd, line_ptr, &bytes_read);
15437 line_ptr += bytes_read;
15438 break;
15439 case DW_LNS_set_file:
debd256d 15440 {
a738430d
MK
15441 /* The arrays lh->include_dirs and lh->file_names are
15442 0-based, but the directory and file name numbers in
15443 the statement program are 1-based. */
debd256d 15444 struct file_entry *fe;
4f1520fb 15445 char *dir = NULL;
a738430d 15446
debd256d
JB
15447 file = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
15448 line_ptr += bytes_read;
59205f5a 15449 if (lh->num_file_names < file || file == 0)
25e43795
DJ
15450 dwarf2_debug_line_missing_file_complaint ();
15451 else
15452 {
15453 fe = &lh->file_names[file - 1];
15454 if (fe->dir_index)
15455 dir = lh->include_dirs[fe->dir_index - 1];
15456 if (!decode_for_pst_p)
15457 {
15458 last_subfile = current_subfile;
15459 dwarf2_start_subfile (fe->name, dir, comp_dir);
15460 }
15461 }
debd256d 15462 }
c906108c
SS
15463 break;
15464 case DW_LNS_set_column:
15465 column = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
15466 line_ptr += bytes_read;
15467 break;
15468 case DW_LNS_negate_stmt:
15469 is_stmt = (!is_stmt);
15470 break;
15471 case DW_LNS_set_basic_block:
15472 basic_block = 1;
15473 break;
c2c6d25f
JM
15474 /* Add to the address register of the state machine the
15475 address increment value corresponding to special opcode
a738430d
MK
15476 255. I.e., this value is scaled by the minimum
15477 instruction length since special opcode 255 would have
b021a221 15478 scaled the increment. */
c906108c 15479 case DW_LNS_const_add_pc:
2dc7f7b3
TT
15480 {
15481 CORE_ADDR adjust = (255 - lh->opcode_base) / lh->line_range;
15482
15483 address += (((op_index + adjust)
15484 / lh->maximum_ops_per_instruction)
15485 * lh->minimum_instruction_length);
15486 op_index = ((op_index + adjust)
15487 % lh->maximum_ops_per_instruction);
15488 }
c906108c
SS
15489 break;
15490 case DW_LNS_fixed_advance_pc:
15491 address += read_2_bytes (abfd, line_ptr);
2dc7f7b3 15492 op_index = 0;
c906108c
SS
15493 line_ptr += 2;
15494 break;
9aa1fe7e 15495 default:
a738430d
MK
15496 {
15497 /* Unknown standard opcode, ignore it. */
9aa1fe7e 15498 int i;
a738430d 15499
debd256d 15500 for (i = 0; i < lh->standard_opcode_lengths[op_code]; i++)
9aa1fe7e
GK
15501 {
15502 (void) read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
15503 line_ptr += bytes_read;
15504 }
15505 }
c906108c
SS
15506 }
15507 }
59205f5a
JB
15508 if (lh->num_file_names < file || file == 0)
15509 dwarf2_debug_line_missing_file_complaint ();
15510 else
15511 {
15512 lh->file_names[file - 1].included_p = 1;
15513 if (!decode_for_pst_p)
fbf65064
UW
15514 {
15515 addr = gdbarch_addr_bits_remove (gdbarch, address);
c91513d8 15516 (*p_record_line) (current_subfile, 0, addr);
fbf65064 15517 }
59205f5a 15518 }
c906108c 15519 }
f3f5162e
DE
15520}
15521
15522/* Decode the Line Number Program (LNP) for the given line_header
15523 structure and CU. The actual information extracted and the type
15524 of structures created from the LNP depends on the value of PST.
15525
15526 1. If PST is NULL, then this procedure uses the data from the program
15527 to create all necessary symbol tables, and their linetables.
15528
15529 2. If PST is not NULL, this procedure reads the program to determine
15530 the list of files included by the unit represented by PST, and
15531 builds all the associated partial symbol tables.
15532
15533 COMP_DIR is the compilation directory (DW_AT_comp_dir) or NULL if unknown.
15534 It is used for relative paths in the line table.
15535 NOTE: When processing partial symtabs (pst != NULL),
15536 comp_dir == pst->dirname.
15537
15538 NOTE: It is important that psymtabs have the same file name (via strcmp)
15539 as the corresponding symtab. Since COMP_DIR is not used in the name of the
15540 symtab we don't use it in the name of the psymtabs we create.
15541 E.g. expand_line_sal requires this when finding psymtabs to expand.
15542 A good testcase for this is mb-inline.exp. */
15543
15544static void
15545dwarf_decode_lines (struct line_header *lh, const char *comp_dir,
15546 struct dwarf2_cu *cu, struct partial_symtab *pst,
15547 int want_line_info)
15548{
15549 struct objfile *objfile = cu->objfile;
15550 const int decode_for_pst_p = (pst != NULL);
15551 struct subfile *first_subfile = current_subfile;
15552
15553 if (want_line_info)
15554 dwarf_decode_lines_1 (lh, comp_dir, cu, pst);
aaa75496
JB
15555
15556 if (decode_for_pst_p)
15557 {
15558 int file_index;
15559
15560 /* Now that we're done scanning the Line Header Program, we can
15561 create the psymtab of each included file. */
15562 for (file_index = 0; file_index < lh->num_file_names; file_index++)
15563 if (lh->file_names[file_index].included_p == 1)
15564 {
c6da4cef
DE
15565 char *include_name =
15566 psymtab_include_file_name (lh, file_index, pst, comp_dir);
15567 if (include_name != NULL)
aaa75496
JB
15568 dwarf2_create_include_psymtab (include_name, pst, objfile);
15569 }
15570 }
cb1df416
DJ
15571 else
15572 {
15573 /* Make sure a symtab is created for every file, even files
15574 which contain only variables (i.e. no code with associated
15575 line numbers). */
cb1df416 15576 int i;
cb1df416
DJ
15577
15578 for (i = 0; i < lh->num_file_names; i++)
15579 {
15580 char *dir = NULL;
f3f5162e 15581 struct file_entry *fe;
9a619af0 15582
cb1df416
DJ
15583 fe = &lh->file_names[i];
15584 if (fe->dir_index)
15585 dir = lh->include_dirs[fe->dir_index - 1];
15586 dwarf2_start_subfile (fe->name, dir, comp_dir);
15587
15588 /* Skip the main file; we don't need it, and it must be
15589 allocated last, so that it will show up before the
15590 non-primary symtabs in the objfile's symtab list. */
15591 if (current_subfile == first_subfile)
15592 continue;
15593
15594 if (current_subfile->symtab == NULL)
15595 current_subfile->symtab = allocate_symtab (current_subfile->name,
bb5ed363 15596 objfile);
cb1df416
DJ
15597 fe->symtab = current_subfile->symtab;
15598 }
15599 }
c906108c
SS
15600}
15601
15602/* Start a subfile for DWARF. FILENAME is the name of the file and
15603 DIRNAME the name of the source directory which contains FILENAME
4f1520fb
FR
15604 or NULL if not known. COMP_DIR is the compilation directory for the
15605 linetable's compilation unit or NULL if not known.
c906108c
SS
15606 This routine tries to keep line numbers from identical absolute and
15607 relative file names in a common subfile.
15608
15609 Using the `list' example from the GDB testsuite, which resides in
15610 /srcdir and compiling it with Irix6.2 cc in /compdir using a filename
15611 of /srcdir/list0.c yields the following debugging information for list0.c:
15612
c5aa993b
JM
15613 DW_AT_name: /srcdir/list0.c
15614 DW_AT_comp_dir: /compdir
357e46e7 15615 files.files[0].name: list0.h
c5aa993b 15616 files.files[0].dir: /srcdir
357e46e7 15617 files.files[1].name: list0.c
c5aa993b 15618 files.files[1].dir: /srcdir
c906108c
SS
15619
15620 The line number information for list0.c has to end up in a single
4f1520fb
FR
15621 subfile, so that `break /srcdir/list0.c:1' works as expected.
15622 start_subfile will ensure that this happens provided that we pass the
15623 concatenation of files.files[1].dir and files.files[1].name as the
15624 subfile's name. */
c906108c
SS
15625
15626static void
3e43a32a
MS
15627dwarf2_start_subfile (char *filename, const char *dirname,
15628 const char *comp_dir)
c906108c 15629{
4f1520fb
FR
15630 char *fullname;
15631
15632 /* While reading the DIEs, we call start_symtab(DW_AT_name, DW_AT_comp_dir).
15633 `start_symtab' will always pass the contents of DW_AT_comp_dir as
15634 second argument to start_subfile. To be consistent, we do the
15635 same here. In order not to lose the line information directory,
15636 we concatenate it to the filename when it makes sense.
15637 Note that the Dwarf3 standard says (speaking of filenames in line
15638 information): ``The directory index is ignored for file names
15639 that represent full path names''. Thus ignoring dirname in the
15640 `else' branch below isn't an issue. */
c906108c 15641
d5166ae1 15642 if (!IS_ABSOLUTE_PATH (filename) && dirname != NULL)
4f1520fb
FR
15643 fullname = concat (dirname, SLASH_STRING, filename, (char *)NULL);
15644 else
15645 fullname = filename;
c906108c 15646
4f1520fb
FR
15647 start_subfile (fullname, comp_dir);
15648
15649 if (fullname != filename)
15650 xfree (fullname);
c906108c
SS
15651}
15652
f4dc4d17
DE
15653/* Start a symtab for DWARF.
15654 NAME, COMP_DIR, LOW_PC are passed to start_symtab. */
15655
15656static void
15657dwarf2_start_symtab (struct dwarf2_cu *cu,
15d034d0 15658 const char *name, const char *comp_dir, CORE_ADDR low_pc)
f4dc4d17
DE
15659{
15660 start_symtab (name, comp_dir, low_pc);
15661 record_debugformat ("DWARF 2");
15662 record_producer (cu->producer);
15663
15664 /* We assume that we're processing GCC output. */
15665 processing_gcc_compilation = 2;
15666
4d4ec4e5 15667 cu->processing_has_namespace_info = 0;
f4dc4d17
DE
15668}
15669
4c2df51b
DJ
15670static void
15671var_decode_location (struct attribute *attr, struct symbol *sym,
e7c27a73 15672 struct dwarf2_cu *cu)
4c2df51b 15673{
e7c27a73
DJ
15674 struct objfile *objfile = cu->objfile;
15675 struct comp_unit_head *cu_header = &cu->header;
15676
4c2df51b
DJ
15677 /* NOTE drow/2003-01-30: There used to be a comment and some special
15678 code here to turn a symbol with DW_AT_external and a
15679 SYMBOL_VALUE_ADDRESS of 0 into a LOC_UNRESOLVED symbol. This was
15680 necessary for platforms (maybe Alpha, certainly PowerPC GNU/Linux
15681 with some versions of binutils) where shared libraries could have
15682 relocations against symbols in their debug information - the
15683 minimal symbol would have the right address, but the debug info
15684 would not. It's no longer necessary, because we will explicitly
15685 apply relocations when we read in the debug information now. */
15686
15687 /* A DW_AT_location attribute with no contents indicates that a
15688 variable has been optimized away. */
15689 if (attr_form_is_block (attr) && DW_BLOCK (attr)->size == 0)
15690 {
15691 SYMBOL_CLASS (sym) = LOC_OPTIMIZED_OUT;
15692 return;
15693 }
15694
15695 /* Handle one degenerate form of location expression specially, to
15696 preserve GDB's previous behavior when section offsets are
3019eac3
DE
15697 specified. If this is just a DW_OP_addr or DW_OP_GNU_addr_index
15698 then mark this symbol as LOC_STATIC. */
4c2df51b
DJ
15699
15700 if (attr_form_is_block (attr)
3019eac3
DE
15701 && ((DW_BLOCK (attr)->data[0] == DW_OP_addr
15702 && DW_BLOCK (attr)->size == 1 + cu_header->addr_size)
15703 || (DW_BLOCK (attr)->data[0] == DW_OP_GNU_addr_index
15704 && (DW_BLOCK (attr)->size
15705 == 1 + leb128_size (&DW_BLOCK (attr)->data[1])))))
4c2df51b 15706 {
891d2f0b 15707 unsigned int dummy;
4c2df51b 15708
3019eac3
DE
15709 if (DW_BLOCK (attr)->data[0] == DW_OP_addr)
15710 SYMBOL_VALUE_ADDRESS (sym) =
15711 read_address (objfile->obfd, DW_BLOCK (attr)->data + 1, cu, &dummy);
15712 else
15713 SYMBOL_VALUE_ADDRESS (sym) =
15714 read_addr_index_from_leb128 (cu, DW_BLOCK (attr)->data + 1, &dummy);
907fc202 15715 SYMBOL_CLASS (sym) = LOC_STATIC;
4c2df51b
DJ
15716 fixup_symbol_section (sym, objfile);
15717 SYMBOL_VALUE_ADDRESS (sym) += ANOFFSET (objfile->section_offsets,
15718 SYMBOL_SECTION (sym));
4c2df51b
DJ
15719 return;
15720 }
15721
15722 /* NOTE drow/2002-01-30: It might be worthwhile to have a static
15723 expression evaluator, and use LOC_COMPUTED only when necessary
15724 (i.e. when the value of a register or memory location is
15725 referenced, or a thread-local block, etc.). Then again, it might
15726 not be worthwhile. I'm assuming that it isn't unless performance
15727 or memory numbers show me otherwise. */
15728
e7c27a73 15729 dwarf2_symbol_mark_computed (attr, sym, cu);
4c2df51b 15730 SYMBOL_CLASS (sym) = LOC_COMPUTED;
8be455d7
JK
15731
15732 if (SYMBOL_COMPUTED_OPS (sym) == &dwarf2_loclist_funcs)
15733 cu->has_loclist = 1;
4c2df51b
DJ
15734}
15735
c906108c
SS
15736/* Given a pointer to a DWARF information entry, figure out if we need
15737 to make a symbol table entry for it, and if so, create a new entry
15738 and return a pointer to it.
15739 If TYPE is NULL, determine symbol type from the die, otherwise
34eaf542
TT
15740 used the passed type.
15741 If SPACE is not NULL, use it to hold the new symbol. If it is
15742 NULL, allocate a new symbol on the objfile's obstack. */
c906108c
SS
15743
15744static struct symbol *
34eaf542
TT
15745new_symbol_full (struct die_info *die, struct type *type, struct dwarf2_cu *cu,
15746 struct symbol *space)
c906108c 15747{
e7c27a73 15748 struct objfile *objfile = cu->objfile;
c906108c 15749 struct symbol *sym = NULL;
15d034d0 15750 const char *name;
c906108c
SS
15751 struct attribute *attr = NULL;
15752 struct attribute *attr2 = NULL;
e142c38c 15753 CORE_ADDR baseaddr;
e37fd15a
SW
15754 struct pending **list_to_add = NULL;
15755
edb3359d 15756 int inlined_func = (die->tag == DW_TAG_inlined_subroutine);
e142c38c
DJ
15757
15758 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
c906108c 15759
94af9270 15760 name = dwarf2_name (die, cu);
c906108c
SS
15761 if (name)
15762 {
94af9270 15763 const char *linkagename;
34eaf542 15764 int suppress_add = 0;
94af9270 15765
34eaf542
TT
15766 if (space)
15767 sym = space;
15768 else
15769 sym = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct symbol);
c906108c 15770 OBJSTAT (objfile, n_syms++);
2de7ced7
DJ
15771
15772 /* Cache this symbol's name and the name's demangled form (if any). */
33e5013e 15773 SYMBOL_SET_LANGUAGE (sym, cu->language);
94af9270
KS
15774 linkagename = dwarf2_physname (name, die, cu);
15775 SYMBOL_SET_NAMES (sym, linkagename, strlen (linkagename), 0, objfile);
c906108c 15776
f55ee35c
JK
15777 /* Fortran does not have mangling standard and the mangling does differ
15778 between gfortran, iFort etc. */
15779 if (cu->language == language_fortran
b250c185 15780 && symbol_get_demangled_name (&(sym->ginfo)) == NULL)
29df156d 15781 symbol_set_demangled_name (&(sym->ginfo),
cfc594ee 15782 dwarf2_full_name (name, die, cu),
29df156d 15783 NULL);
f55ee35c 15784
c906108c 15785 /* Default assumptions.
c5aa993b 15786 Use the passed type or decode it from the die. */
176620f1 15787 SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
875dc2fc 15788 SYMBOL_CLASS (sym) = LOC_OPTIMIZED_OUT;
c906108c
SS
15789 if (type != NULL)
15790 SYMBOL_TYPE (sym) = type;
15791 else
e7c27a73 15792 SYMBOL_TYPE (sym) = die_type (die, cu);
edb3359d
DJ
15793 attr = dwarf2_attr (die,
15794 inlined_func ? DW_AT_call_line : DW_AT_decl_line,
15795 cu);
c906108c
SS
15796 if (attr)
15797 {
15798 SYMBOL_LINE (sym) = DW_UNSND (attr);
15799 }
cb1df416 15800
edb3359d
DJ
15801 attr = dwarf2_attr (die,
15802 inlined_func ? DW_AT_call_file : DW_AT_decl_file,
15803 cu);
cb1df416
DJ
15804 if (attr)
15805 {
15806 int file_index = DW_UNSND (attr);
9a619af0 15807
cb1df416
DJ
15808 if (cu->line_header == NULL
15809 || file_index > cu->line_header->num_file_names)
15810 complaint (&symfile_complaints,
15811 _("file index out of range"));
1c3d648d 15812 else if (file_index > 0)
cb1df416
DJ
15813 {
15814 struct file_entry *fe;
9a619af0 15815
cb1df416
DJ
15816 fe = &cu->line_header->file_names[file_index - 1];
15817 SYMBOL_SYMTAB (sym) = fe->symtab;
15818 }
15819 }
15820
c906108c
SS
15821 switch (die->tag)
15822 {
15823 case DW_TAG_label:
e142c38c 15824 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
c906108c
SS
15825 if (attr)
15826 {
15827 SYMBOL_VALUE_ADDRESS (sym) = DW_ADDR (attr) + baseaddr;
15828 }
0f5238ed
TT
15829 SYMBOL_TYPE (sym) = objfile_type (objfile)->builtin_core_addr;
15830 SYMBOL_DOMAIN (sym) = LABEL_DOMAIN;
c906108c 15831 SYMBOL_CLASS (sym) = LOC_LABEL;
0f5238ed 15832 add_symbol_to_list (sym, cu->list_in_scope);
c906108c
SS
15833 break;
15834 case DW_TAG_subprogram:
15835 /* SYMBOL_BLOCK_VALUE (sym) will be filled in later by
15836 finish_block. */
15837 SYMBOL_CLASS (sym) = LOC_BLOCK;
e142c38c 15838 attr2 = dwarf2_attr (die, DW_AT_external, cu);
2cfa0c8d
JB
15839 if ((attr2 && (DW_UNSND (attr2) != 0))
15840 || cu->language == language_ada)
c906108c 15841 {
2cfa0c8d
JB
15842 /* Subprograms marked external are stored as a global symbol.
15843 Ada subprograms, whether marked external or not, are always
15844 stored as a global symbol, because we want to be able to
15845 access them globally. For instance, we want to be able
15846 to break on a nested subprogram without having to
15847 specify the context. */
e37fd15a 15848 list_to_add = &global_symbols;
c906108c
SS
15849 }
15850 else
15851 {
e37fd15a 15852 list_to_add = cu->list_in_scope;
c906108c
SS
15853 }
15854 break;
edb3359d
DJ
15855 case DW_TAG_inlined_subroutine:
15856 /* SYMBOL_BLOCK_VALUE (sym) will be filled in later by
15857 finish_block. */
15858 SYMBOL_CLASS (sym) = LOC_BLOCK;
15859 SYMBOL_INLINED (sym) = 1;
481860b3 15860 list_to_add = cu->list_in_scope;
edb3359d 15861 break;
34eaf542
TT
15862 case DW_TAG_template_value_param:
15863 suppress_add = 1;
15864 /* Fall through. */
72929c62 15865 case DW_TAG_constant:
c906108c 15866 case DW_TAG_variable:
254e6b9e 15867 case DW_TAG_member:
0963b4bd
MS
15868 /* Compilation with minimal debug info may result in
15869 variables with missing type entries. Change the
15870 misleading `void' type to something sensible. */
c906108c 15871 if (TYPE_CODE (SYMBOL_TYPE (sym)) == TYPE_CODE_VOID)
64c50499 15872 SYMBOL_TYPE (sym)
46bf5051 15873 = objfile_type (objfile)->nodebug_data_symbol;
64c50499 15874
e142c38c 15875 attr = dwarf2_attr (die, DW_AT_const_value, cu);
254e6b9e
DE
15876 /* In the case of DW_TAG_member, we should only be called for
15877 static const members. */
15878 if (die->tag == DW_TAG_member)
15879 {
3863f96c
DE
15880 /* dwarf2_add_field uses die_is_declaration,
15881 so we do the same. */
254e6b9e
DE
15882 gdb_assert (die_is_declaration (die, cu));
15883 gdb_assert (attr);
15884 }
c906108c
SS
15885 if (attr)
15886 {
e7c27a73 15887 dwarf2_const_value (attr, sym, cu);
e142c38c 15888 attr2 = dwarf2_attr (die, DW_AT_external, cu);
e37fd15a 15889 if (!suppress_add)
34eaf542
TT
15890 {
15891 if (attr2 && (DW_UNSND (attr2) != 0))
e37fd15a 15892 list_to_add = &global_symbols;
34eaf542 15893 else
e37fd15a 15894 list_to_add = cu->list_in_scope;
34eaf542 15895 }
c906108c
SS
15896 break;
15897 }
e142c38c 15898 attr = dwarf2_attr (die, DW_AT_location, cu);
c906108c
SS
15899 if (attr)
15900 {
e7c27a73 15901 var_decode_location (attr, sym, cu);
e142c38c 15902 attr2 = dwarf2_attr (die, DW_AT_external, cu);
4357ac6c
TT
15903
15904 /* Fortran explicitly imports any global symbols to the local
15905 scope by DW_TAG_common_block. */
15906 if (cu->language == language_fortran && die->parent
15907 && die->parent->tag == DW_TAG_common_block)
15908 attr2 = NULL;
15909
caac4577
JG
15910 if (SYMBOL_CLASS (sym) == LOC_STATIC
15911 && SYMBOL_VALUE_ADDRESS (sym) == 0
15912 && !dwarf2_per_objfile->has_section_at_zero)
15913 {
15914 /* When a static variable is eliminated by the linker,
15915 the corresponding debug information is not stripped
15916 out, but the variable address is set to null;
15917 do not add such variables into symbol table. */
15918 }
15919 else if (attr2 && (DW_UNSND (attr2) != 0))
1c809c68 15920 {
f55ee35c
JK
15921 /* Workaround gfortran PR debug/40040 - it uses
15922 DW_AT_location for variables in -fPIC libraries which may
15923 get overriden by other libraries/executable and get
15924 a different address. Resolve it by the minimal symbol
15925 which may come from inferior's executable using copy
15926 relocation. Make this workaround only for gfortran as for
15927 other compilers GDB cannot guess the minimal symbol
15928 Fortran mangling kind. */
15929 if (cu->language == language_fortran && die->parent
15930 && die->parent->tag == DW_TAG_module
15931 && cu->producer
15932 && strncmp (cu->producer, "GNU Fortran ", 12) == 0)
15933 SYMBOL_CLASS (sym) = LOC_UNRESOLVED;
15934
1c809c68
TT
15935 /* A variable with DW_AT_external is never static,
15936 but it may be block-scoped. */
15937 list_to_add = (cu->list_in_scope == &file_symbols
15938 ? &global_symbols : cu->list_in_scope);
1c809c68 15939 }
c906108c 15940 else
e37fd15a 15941 list_to_add = cu->list_in_scope;
c906108c
SS
15942 }
15943 else
15944 {
15945 /* We do not know the address of this symbol.
c5aa993b
JM
15946 If it is an external symbol and we have type information
15947 for it, enter the symbol as a LOC_UNRESOLVED symbol.
15948 The address of the variable will then be determined from
15949 the minimal symbol table whenever the variable is
15950 referenced. */
e142c38c 15951 attr2 = dwarf2_attr (die, DW_AT_external, cu);
0971de02
TT
15952
15953 /* Fortran explicitly imports any global symbols to the local
15954 scope by DW_TAG_common_block. */
15955 if (cu->language == language_fortran && die->parent
15956 && die->parent->tag == DW_TAG_common_block)
15957 {
15958 /* SYMBOL_CLASS doesn't matter here because
15959 read_common_block is going to reset it. */
15960 if (!suppress_add)
15961 list_to_add = cu->list_in_scope;
15962 }
15963 else if (attr2 && (DW_UNSND (attr2) != 0)
15964 && dwarf2_attr (die, DW_AT_type, cu) != NULL)
c906108c 15965 {
0fe7935b
DJ
15966 /* A variable with DW_AT_external is never static, but it
15967 may be block-scoped. */
15968 list_to_add = (cu->list_in_scope == &file_symbols
15969 ? &global_symbols : cu->list_in_scope);
15970
c906108c 15971 SYMBOL_CLASS (sym) = LOC_UNRESOLVED;
c906108c 15972 }
442ddf59
JK
15973 else if (!die_is_declaration (die, cu))
15974 {
15975 /* Use the default LOC_OPTIMIZED_OUT class. */
15976 gdb_assert (SYMBOL_CLASS (sym) == LOC_OPTIMIZED_OUT);
e37fd15a
SW
15977 if (!suppress_add)
15978 list_to_add = cu->list_in_scope;
442ddf59 15979 }
c906108c
SS
15980 }
15981 break;
15982 case DW_TAG_formal_parameter:
edb3359d
DJ
15983 /* If we are inside a function, mark this as an argument. If
15984 not, we might be looking at an argument to an inlined function
15985 when we do not have enough information to show inlined frames;
15986 pretend it's a local variable in that case so that the user can
15987 still see it. */
15988 if (context_stack_depth > 0
15989 && context_stack[context_stack_depth - 1].name != NULL)
15990 SYMBOL_IS_ARGUMENT (sym) = 1;
e142c38c 15991 attr = dwarf2_attr (die, DW_AT_location, cu);
c906108c
SS
15992 if (attr)
15993 {
e7c27a73 15994 var_decode_location (attr, sym, cu);
c906108c 15995 }
e142c38c 15996 attr = dwarf2_attr (die, DW_AT_const_value, cu);
c906108c
SS
15997 if (attr)
15998 {
e7c27a73 15999 dwarf2_const_value (attr, sym, cu);
c906108c 16000 }
f346a30d 16001
e37fd15a 16002 list_to_add = cu->list_in_scope;
c906108c
SS
16003 break;
16004 case DW_TAG_unspecified_parameters:
16005 /* From varargs functions; gdb doesn't seem to have any
16006 interest in this information, so just ignore it for now.
16007 (FIXME?) */
16008 break;
34eaf542
TT
16009 case DW_TAG_template_type_param:
16010 suppress_add = 1;
16011 /* Fall through. */
c906108c 16012 case DW_TAG_class_type:
680b30c7 16013 case DW_TAG_interface_type:
c906108c
SS
16014 case DW_TAG_structure_type:
16015 case DW_TAG_union_type:
72019c9c 16016 case DW_TAG_set_type:
c906108c
SS
16017 case DW_TAG_enumeration_type:
16018 SYMBOL_CLASS (sym) = LOC_TYPEDEF;
176620f1 16019 SYMBOL_DOMAIN (sym) = STRUCT_DOMAIN;
c906108c 16020
63d06c5c 16021 {
987504bb 16022 /* NOTE: carlton/2003-11-10: C++ and Java class symbols shouldn't
63d06c5c
DC
16023 really ever be static objects: otherwise, if you try
16024 to, say, break of a class's method and you're in a file
16025 which doesn't mention that class, it won't work unless
16026 the check for all static symbols in lookup_symbol_aux
16027 saves you. See the OtherFileClass tests in
16028 gdb.c++/namespace.exp. */
16029
e37fd15a 16030 if (!suppress_add)
34eaf542 16031 {
34eaf542
TT
16032 list_to_add = (cu->list_in_scope == &file_symbols
16033 && (cu->language == language_cplus
16034 || cu->language == language_java)
16035 ? &global_symbols : cu->list_in_scope);
63d06c5c 16036
64382290
TT
16037 /* The semantics of C++ state that "struct foo {
16038 ... }" also defines a typedef for "foo". A Java
16039 class declaration also defines a typedef for the
16040 class. */
16041 if (cu->language == language_cplus
16042 || cu->language == language_java
16043 || cu->language == language_ada)
16044 {
16045 /* The symbol's name is already allocated along
16046 with this objfile, so we don't need to
16047 duplicate it for the type. */
16048 if (TYPE_NAME (SYMBOL_TYPE (sym)) == 0)
16049 TYPE_NAME (SYMBOL_TYPE (sym)) = SYMBOL_SEARCH_NAME (sym);
16050 }
63d06c5c
DC
16051 }
16052 }
c906108c
SS
16053 break;
16054 case DW_TAG_typedef:
63d06c5c
DC
16055 SYMBOL_CLASS (sym) = LOC_TYPEDEF;
16056 SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
e37fd15a 16057 list_to_add = cu->list_in_scope;
63d06c5c 16058 break;
c906108c 16059 case DW_TAG_base_type:
a02abb62 16060 case DW_TAG_subrange_type:
c906108c 16061 SYMBOL_CLASS (sym) = LOC_TYPEDEF;
176620f1 16062 SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
e37fd15a 16063 list_to_add = cu->list_in_scope;
c906108c
SS
16064 break;
16065 case DW_TAG_enumerator:
e142c38c 16066 attr = dwarf2_attr (die, DW_AT_const_value, cu);
c906108c
SS
16067 if (attr)
16068 {
e7c27a73 16069 dwarf2_const_value (attr, sym, cu);
c906108c 16070 }
63d06c5c
DC
16071 {
16072 /* NOTE: carlton/2003-11-10: See comment above in the
16073 DW_TAG_class_type, etc. block. */
16074
e142c38c 16075 list_to_add = (cu->list_in_scope == &file_symbols
987504bb
JJ
16076 && (cu->language == language_cplus
16077 || cu->language == language_java)
e142c38c 16078 ? &global_symbols : cu->list_in_scope);
63d06c5c 16079 }
c906108c 16080 break;
5c4e30ca
DC
16081 case DW_TAG_namespace:
16082 SYMBOL_CLASS (sym) = LOC_TYPEDEF;
e37fd15a 16083 list_to_add = &global_symbols;
5c4e30ca 16084 break;
4357ac6c 16085 case DW_TAG_common_block:
5a352474 16086 SYMBOL_CLASS (sym) = LOC_COMMON_BLOCK;
4357ac6c
TT
16087 SYMBOL_DOMAIN (sym) = COMMON_BLOCK_DOMAIN;
16088 add_symbol_to_list (sym, cu->list_in_scope);
16089 break;
c906108c
SS
16090 default:
16091 /* Not a tag we recognize. Hopefully we aren't processing
16092 trash data, but since we must specifically ignore things
16093 we don't recognize, there is nothing else we should do at
0963b4bd 16094 this point. */
e2e0b3e5 16095 complaint (&symfile_complaints, _("unsupported tag: '%s'"),
4d3c2250 16096 dwarf_tag_name (die->tag));
c906108c
SS
16097 break;
16098 }
df8a16a1 16099
e37fd15a
SW
16100 if (suppress_add)
16101 {
16102 sym->hash_next = objfile->template_symbols;
16103 objfile->template_symbols = sym;
16104 list_to_add = NULL;
16105 }
16106
16107 if (list_to_add != NULL)
16108 add_symbol_to_list (sym, list_to_add);
16109
df8a16a1
DJ
16110 /* For the benefit of old versions of GCC, check for anonymous
16111 namespaces based on the demangled name. */
4d4ec4e5 16112 if (!cu->processing_has_namespace_info
94af9270 16113 && cu->language == language_cplus)
a10964d1 16114 cp_scan_for_anonymous_namespaces (sym, objfile);
c906108c
SS
16115 }
16116 return (sym);
16117}
16118
34eaf542
TT
16119/* A wrapper for new_symbol_full that always allocates a new symbol. */
16120
16121static struct symbol *
16122new_symbol (struct die_info *die, struct type *type, struct dwarf2_cu *cu)
16123{
16124 return new_symbol_full (die, type, cu, NULL);
16125}
16126
98bfdba5
PA
16127/* Given an attr with a DW_FORM_dataN value in host byte order,
16128 zero-extend it as appropriate for the symbol's type. The DWARF
16129 standard (v4) is not entirely clear about the meaning of using
16130 DW_FORM_dataN for a constant with a signed type, where the type is
16131 wider than the data. The conclusion of a discussion on the DWARF
16132 list was that this is unspecified. We choose to always zero-extend
16133 because that is the interpretation long in use by GCC. */
c906108c 16134
98bfdba5
PA
16135static gdb_byte *
16136dwarf2_const_value_data (struct attribute *attr, struct type *type,
16137 const char *name, struct obstack *obstack,
12df843f 16138 struct dwarf2_cu *cu, LONGEST *value, int bits)
c906108c 16139{
e7c27a73 16140 struct objfile *objfile = cu->objfile;
e17a4113
UW
16141 enum bfd_endian byte_order = bfd_big_endian (objfile->obfd) ?
16142 BFD_ENDIAN_BIG : BFD_ENDIAN_LITTLE;
98bfdba5
PA
16143 LONGEST l = DW_UNSND (attr);
16144
16145 if (bits < sizeof (*value) * 8)
16146 {
16147 l &= ((LONGEST) 1 << bits) - 1;
16148 *value = l;
16149 }
16150 else if (bits == sizeof (*value) * 8)
16151 *value = l;
16152 else
16153 {
16154 gdb_byte *bytes = obstack_alloc (obstack, bits / 8);
16155 store_unsigned_integer (bytes, bits / 8, byte_order, l);
16156 return bytes;
16157 }
16158
16159 return NULL;
16160}
16161
16162/* Read a constant value from an attribute. Either set *VALUE, or if
16163 the value does not fit in *VALUE, set *BYTES - either already
16164 allocated on the objfile obstack, or newly allocated on OBSTACK,
16165 or, set *BATON, if we translated the constant to a location
16166 expression. */
16167
16168static void
16169dwarf2_const_value_attr (struct attribute *attr, struct type *type,
16170 const char *name, struct obstack *obstack,
16171 struct dwarf2_cu *cu,
12df843f 16172 LONGEST *value, gdb_byte **bytes,
98bfdba5
PA
16173 struct dwarf2_locexpr_baton **baton)
16174{
16175 struct objfile *objfile = cu->objfile;
16176 struct comp_unit_head *cu_header = &cu->header;
c906108c 16177 struct dwarf_block *blk;
98bfdba5
PA
16178 enum bfd_endian byte_order = (bfd_big_endian (objfile->obfd) ?
16179 BFD_ENDIAN_BIG : BFD_ENDIAN_LITTLE);
16180
16181 *value = 0;
16182 *bytes = NULL;
16183 *baton = NULL;
c906108c
SS
16184
16185 switch (attr->form)
16186 {
16187 case DW_FORM_addr:
3019eac3 16188 case DW_FORM_GNU_addr_index:
ac56253d 16189 {
ac56253d
TT
16190 gdb_byte *data;
16191
98bfdba5
PA
16192 if (TYPE_LENGTH (type) != cu_header->addr_size)
16193 dwarf2_const_value_length_mismatch_complaint (name,
ac56253d 16194 cu_header->addr_size,
98bfdba5 16195 TYPE_LENGTH (type));
ac56253d
TT
16196 /* Symbols of this form are reasonably rare, so we just
16197 piggyback on the existing location code rather than writing
16198 a new implementation of symbol_computed_ops. */
98bfdba5
PA
16199 *baton = obstack_alloc (&objfile->objfile_obstack,
16200 sizeof (struct dwarf2_locexpr_baton));
16201 (*baton)->per_cu = cu->per_cu;
16202 gdb_assert ((*baton)->per_cu);
ac56253d 16203
98bfdba5
PA
16204 (*baton)->size = 2 + cu_header->addr_size;
16205 data = obstack_alloc (&objfile->objfile_obstack, (*baton)->size);
16206 (*baton)->data = data;
ac56253d
TT
16207
16208 data[0] = DW_OP_addr;
16209 store_unsigned_integer (&data[1], cu_header->addr_size,
16210 byte_order, DW_ADDR (attr));
16211 data[cu_header->addr_size + 1] = DW_OP_stack_value;
ac56253d 16212 }
c906108c 16213 break;
4ac36638 16214 case DW_FORM_string:
93b5768b 16215 case DW_FORM_strp:
3019eac3 16216 case DW_FORM_GNU_str_index:
36586728 16217 case DW_FORM_GNU_strp_alt:
98bfdba5
PA
16218 /* DW_STRING is already allocated on the objfile obstack, point
16219 directly to it. */
16220 *bytes = (gdb_byte *) DW_STRING (attr);
93b5768b 16221 break;
c906108c
SS
16222 case DW_FORM_block1:
16223 case DW_FORM_block2:
16224 case DW_FORM_block4:
16225 case DW_FORM_block:
2dc7f7b3 16226 case DW_FORM_exprloc:
c906108c 16227 blk = DW_BLOCK (attr);
98bfdba5
PA
16228 if (TYPE_LENGTH (type) != blk->size)
16229 dwarf2_const_value_length_mismatch_complaint (name, blk->size,
16230 TYPE_LENGTH (type));
16231 *bytes = blk->data;
c906108c 16232 break;
2df3850c
JM
16233
16234 /* The DW_AT_const_value attributes are supposed to carry the
16235 symbol's value "represented as it would be on the target
16236 architecture." By the time we get here, it's already been
16237 converted to host endianness, so we just need to sign- or
16238 zero-extend it as appropriate. */
16239 case DW_FORM_data1:
3e43a32a
MS
16240 *bytes = dwarf2_const_value_data (attr, type, name,
16241 obstack, cu, value, 8);
2df3850c 16242 break;
c906108c 16243 case DW_FORM_data2:
3e43a32a
MS
16244 *bytes = dwarf2_const_value_data (attr, type, name,
16245 obstack, cu, value, 16);
2df3850c 16246 break;
c906108c 16247 case DW_FORM_data4:
3e43a32a
MS
16248 *bytes = dwarf2_const_value_data (attr, type, name,
16249 obstack, cu, value, 32);
2df3850c 16250 break;
c906108c 16251 case DW_FORM_data8:
3e43a32a
MS
16252 *bytes = dwarf2_const_value_data (attr, type, name,
16253 obstack, cu, value, 64);
2df3850c
JM
16254 break;
16255
c906108c 16256 case DW_FORM_sdata:
98bfdba5 16257 *value = DW_SND (attr);
2df3850c
JM
16258 break;
16259
c906108c 16260 case DW_FORM_udata:
98bfdba5 16261 *value = DW_UNSND (attr);
c906108c 16262 break;
2df3850c 16263
c906108c 16264 default:
4d3c2250 16265 complaint (&symfile_complaints,
e2e0b3e5 16266 _("unsupported const value attribute form: '%s'"),
4d3c2250 16267 dwarf_form_name (attr->form));
98bfdba5 16268 *value = 0;
c906108c
SS
16269 break;
16270 }
16271}
16272
2df3850c 16273
98bfdba5
PA
16274/* Copy constant value from an attribute to a symbol. */
16275
2df3850c 16276static void
98bfdba5
PA
16277dwarf2_const_value (struct attribute *attr, struct symbol *sym,
16278 struct dwarf2_cu *cu)
2df3850c 16279{
98bfdba5
PA
16280 struct objfile *objfile = cu->objfile;
16281 struct comp_unit_head *cu_header = &cu->header;
12df843f 16282 LONGEST value;
98bfdba5
PA
16283 gdb_byte *bytes;
16284 struct dwarf2_locexpr_baton *baton;
2df3850c 16285
98bfdba5
PA
16286 dwarf2_const_value_attr (attr, SYMBOL_TYPE (sym),
16287 SYMBOL_PRINT_NAME (sym),
16288 &objfile->objfile_obstack, cu,
16289 &value, &bytes, &baton);
2df3850c 16290
98bfdba5
PA
16291 if (baton != NULL)
16292 {
16293 SYMBOL_COMPUTED_OPS (sym) = &dwarf2_locexpr_funcs;
16294 SYMBOL_LOCATION_BATON (sym) = baton;
16295 SYMBOL_CLASS (sym) = LOC_COMPUTED;
16296 }
16297 else if (bytes != NULL)
16298 {
16299 SYMBOL_VALUE_BYTES (sym) = bytes;
16300 SYMBOL_CLASS (sym) = LOC_CONST_BYTES;
16301 }
16302 else
16303 {
16304 SYMBOL_VALUE (sym) = value;
16305 SYMBOL_CLASS (sym) = LOC_CONST;
16306 }
2df3850c
JM
16307}
16308
c906108c
SS
16309/* Return the type of the die in question using its DW_AT_type attribute. */
16310
16311static struct type *
e7c27a73 16312die_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 16313{
c906108c 16314 struct attribute *type_attr;
c906108c 16315
e142c38c 16316 type_attr = dwarf2_attr (die, DW_AT_type, cu);
c906108c
SS
16317 if (!type_attr)
16318 {
16319 /* A missing DW_AT_type represents a void type. */
46bf5051 16320 return objfile_type (cu->objfile)->builtin_void;
c906108c 16321 }
348e048f 16322
673bfd45 16323 return lookup_die_type (die, type_attr, cu);
c906108c
SS
16324}
16325
b4ba55a1
JB
16326/* True iff CU's producer generates GNAT Ada auxiliary information
16327 that allows to find parallel types through that information instead
16328 of having to do expensive parallel lookups by type name. */
16329
16330static int
16331need_gnat_info (struct dwarf2_cu *cu)
16332{
16333 /* FIXME: brobecker/2010-10-12: As of now, only the AdaCore version
16334 of GNAT produces this auxiliary information, without any indication
16335 that it is produced. Part of enhancing the FSF version of GNAT
16336 to produce that information will be to put in place an indicator
16337 that we can use in order to determine whether the descriptive type
16338 info is available or not. One suggestion that has been made is
16339 to use a new attribute, attached to the CU die. For now, assume
16340 that the descriptive type info is not available. */
16341 return 0;
16342}
16343
b4ba55a1
JB
16344/* Return the auxiliary type of the die in question using its
16345 DW_AT_GNAT_descriptive_type attribute. Returns NULL if the
16346 attribute is not present. */
16347
16348static struct type *
16349die_descriptive_type (struct die_info *die, struct dwarf2_cu *cu)
16350{
b4ba55a1 16351 struct attribute *type_attr;
b4ba55a1
JB
16352
16353 type_attr = dwarf2_attr (die, DW_AT_GNAT_descriptive_type, cu);
16354 if (!type_attr)
16355 return NULL;
16356
673bfd45 16357 return lookup_die_type (die, type_attr, cu);
b4ba55a1
JB
16358}
16359
16360/* If DIE has a descriptive_type attribute, then set the TYPE's
16361 descriptive type accordingly. */
16362
16363static void
16364set_descriptive_type (struct type *type, struct die_info *die,
16365 struct dwarf2_cu *cu)
16366{
16367 struct type *descriptive_type = die_descriptive_type (die, cu);
16368
16369 if (descriptive_type)
16370 {
16371 ALLOCATE_GNAT_AUX_TYPE (type);
16372 TYPE_DESCRIPTIVE_TYPE (type) = descriptive_type;
16373 }
16374}
16375
c906108c
SS
16376/* Return the containing type of the die in question using its
16377 DW_AT_containing_type attribute. */
16378
16379static struct type *
e7c27a73 16380die_containing_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 16381{
c906108c 16382 struct attribute *type_attr;
c906108c 16383
e142c38c 16384 type_attr = dwarf2_attr (die, DW_AT_containing_type, cu);
33ac96f0
JK
16385 if (!type_attr)
16386 error (_("Dwarf Error: Problem turning containing type into gdb type "
16387 "[in module %s]"), cu->objfile->name);
16388
673bfd45 16389 return lookup_die_type (die, type_attr, cu);
c906108c
SS
16390}
16391
673bfd45
DE
16392/* Look up the type of DIE in CU using its type attribute ATTR.
16393 If there is no type substitute an error marker. */
16394
c906108c 16395static struct type *
673bfd45
DE
16396lookup_die_type (struct die_info *die, struct attribute *attr,
16397 struct dwarf2_cu *cu)
c906108c 16398{
bb5ed363 16399 struct objfile *objfile = cu->objfile;
f792889a
DJ
16400 struct type *this_type;
16401
673bfd45
DE
16402 /* First see if we have it cached. */
16403
36586728
TT
16404 if (attr->form == DW_FORM_GNU_ref_alt)
16405 {
16406 struct dwarf2_per_cu_data *per_cu;
16407 sect_offset offset = dwarf2_get_ref_die_offset (attr);
16408
16409 per_cu = dwarf2_find_containing_comp_unit (offset, 1, cu->objfile);
16410 this_type = get_die_type_at_offset (offset, per_cu);
16411 }
16412 else if (is_ref_attr (attr))
673bfd45 16413 {
b64f50a1 16414 sect_offset offset = dwarf2_get_ref_die_offset (attr);
673bfd45
DE
16415
16416 this_type = get_die_type_at_offset (offset, cu->per_cu);
16417 }
55f1336d 16418 else if (attr->form == DW_FORM_ref_sig8)
673bfd45
DE
16419 {
16420 struct signatured_type *sig_type = DW_SIGNATURED_TYPE (attr);
673bfd45
DE
16421
16422 /* sig_type will be NULL if the signatured type is missing from
16423 the debug info. */
16424 if (sig_type == NULL)
16425 error (_("Dwarf Error: Cannot find signatured DIE referenced from DIE "
16426 "at 0x%x [in module %s]"),
b64f50a1 16427 die->offset.sect_off, objfile->name);
673bfd45 16428
3019eac3
DE
16429 gdb_assert (sig_type->per_cu.is_debug_types);
16430 /* If we haven't filled in type_offset_in_section yet, then we
16431 haven't read the type in yet. */
16432 this_type = NULL;
16433 if (sig_type->type_offset_in_section.sect_off != 0)
16434 {
16435 this_type =
16436 get_die_type_at_offset (sig_type->type_offset_in_section,
16437 &sig_type->per_cu);
16438 }
673bfd45
DE
16439 }
16440 else
16441 {
16442 dump_die_for_error (die);
16443 error (_("Dwarf Error: Bad type attribute %s [in module %s]"),
bb5ed363 16444 dwarf_attr_name (attr->name), objfile->name);
673bfd45
DE
16445 }
16446
16447 /* If not cached we need to read it in. */
16448
16449 if (this_type == NULL)
16450 {
16451 struct die_info *type_die;
16452 struct dwarf2_cu *type_cu = cu;
16453
16454 type_die = follow_die_ref_or_sig (die, attr, &type_cu);
3019eac3
DE
16455 /* If we found the type now, it's probably because the type came
16456 from an inter-CU reference and the type's CU got expanded before
16457 ours. */
16458 this_type = get_die_type (type_die, type_cu);
16459 if (this_type == NULL)
16460 this_type = read_type_die_1 (type_die, type_cu);
673bfd45
DE
16461 }
16462
16463 /* If we still don't have a type use an error marker. */
16464
16465 if (this_type == NULL)
c906108c 16466 {
b00fdb78
TT
16467 char *message, *saved;
16468
16469 /* read_type_die already issued a complaint. */
16470 message = xstrprintf (_("<unknown type in %s, CU 0x%x, DIE 0x%x>"),
bb5ed363 16471 objfile->name,
b64f50a1
JK
16472 cu->header.offset.sect_off,
16473 die->offset.sect_off);
bb5ed363 16474 saved = obstack_copy0 (&objfile->objfile_obstack,
b00fdb78
TT
16475 message, strlen (message));
16476 xfree (message);
16477
bb5ed363 16478 this_type = init_type (TYPE_CODE_ERROR, 0, 0, saved, objfile);
c906108c 16479 }
673bfd45 16480
f792889a 16481 return this_type;
c906108c
SS
16482}
16483
673bfd45
DE
16484/* Return the type in DIE, CU.
16485 Returns NULL for invalid types.
16486
16487 This first does a lookup in the appropriate type_hash table,
16488 and only reads the die in if necessary.
16489
16490 NOTE: This can be called when reading in partial or full symbols. */
16491
f792889a 16492static struct type *
e7c27a73 16493read_type_die (struct die_info *die, struct dwarf2_cu *cu)
c906108c 16494{
f792889a
DJ
16495 struct type *this_type;
16496
16497 this_type = get_die_type (die, cu);
16498 if (this_type)
16499 return this_type;
16500
673bfd45
DE
16501 return read_type_die_1 (die, cu);
16502}
16503
16504/* Read the type in DIE, CU.
16505 Returns NULL for invalid types. */
16506
16507static struct type *
16508read_type_die_1 (struct die_info *die, struct dwarf2_cu *cu)
16509{
16510 struct type *this_type = NULL;
16511
c906108c
SS
16512 switch (die->tag)
16513 {
16514 case DW_TAG_class_type:
680b30c7 16515 case DW_TAG_interface_type:
c906108c
SS
16516 case DW_TAG_structure_type:
16517 case DW_TAG_union_type:
f792889a 16518 this_type = read_structure_type (die, cu);
c906108c
SS
16519 break;
16520 case DW_TAG_enumeration_type:
f792889a 16521 this_type = read_enumeration_type (die, cu);
c906108c
SS
16522 break;
16523 case DW_TAG_subprogram:
16524 case DW_TAG_subroutine_type:
edb3359d 16525 case DW_TAG_inlined_subroutine:
f792889a 16526 this_type = read_subroutine_type (die, cu);
c906108c
SS
16527 break;
16528 case DW_TAG_array_type:
f792889a 16529 this_type = read_array_type (die, cu);
c906108c 16530 break;
72019c9c 16531 case DW_TAG_set_type:
f792889a 16532 this_type = read_set_type (die, cu);
72019c9c 16533 break;
c906108c 16534 case DW_TAG_pointer_type:
f792889a 16535 this_type = read_tag_pointer_type (die, cu);
c906108c
SS
16536 break;
16537 case DW_TAG_ptr_to_member_type:
f792889a 16538 this_type = read_tag_ptr_to_member_type (die, cu);
c906108c
SS
16539 break;
16540 case DW_TAG_reference_type:
f792889a 16541 this_type = read_tag_reference_type (die, cu);
c906108c
SS
16542 break;
16543 case DW_TAG_const_type:
f792889a 16544 this_type = read_tag_const_type (die, cu);
c906108c
SS
16545 break;
16546 case DW_TAG_volatile_type:
f792889a 16547 this_type = read_tag_volatile_type (die, cu);
c906108c 16548 break;
06d66ee9
TT
16549 case DW_TAG_restrict_type:
16550 this_type = read_tag_restrict_type (die, cu);
16551 break;
c906108c 16552 case DW_TAG_string_type:
f792889a 16553 this_type = read_tag_string_type (die, cu);
c906108c
SS
16554 break;
16555 case DW_TAG_typedef:
f792889a 16556 this_type = read_typedef (die, cu);
c906108c 16557 break;
a02abb62 16558 case DW_TAG_subrange_type:
f792889a 16559 this_type = read_subrange_type (die, cu);
a02abb62 16560 break;
c906108c 16561 case DW_TAG_base_type:
f792889a 16562 this_type = read_base_type (die, cu);
c906108c 16563 break;
81a17f79 16564 case DW_TAG_unspecified_type:
f792889a 16565 this_type = read_unspecified_type (die, cu);
81a17f79 16566 break;
0114d602
DJ
16567 case DW_TAG_namespace:
16568 this_type = read_namespace_type (die, cu);
16569 break;
f55ee35c
JK
16570 case DW_TAG_module:
16571 this_type = read_module_type (die, cu);
16572 break;
c906108c 16573 default:
3e43a32a
MS
16574 complaint (&symfile_complaints,
16575 _("unexpected tag in read_type_die: '%s'"),
4d3c2250 16576 dwarf_tag_name (die->tag));
c906108c
SS
16577 break;
16578 }
63d06c5c 16579
f792889a 16580 return this_type;
63d06c5c
DC
16581}
16582
abc72ce4
DE
16583/* See if we can figure out if the class lives in a namespace. We do
16584 this by looking for a member function; its demangled name will
16585 contain namespace info, if there is any.
16586 Return the computed name or NULL.
16587 Space for the result is allocated on the objfile's obstack.
16588 This is the full-die version of guess_partial_die_structure_name.
16589 In this case we know DIE has no useful parent. */
16590
16591static char *
16592guess_full_die_structure_name (struct die_info *die, struct dwarf2_cu *cu)
16593{
16594 struct die_info *spec_die;
16595 struct dwarf2_cu *spec_cu;
16596 struct die_info *child;
16597
16598 spec_cu = cu;
16599 spec_die = die_specification (die, &spec_cu);
16600 if (spec_die != NULL)
16601 {
16602 die = spec_die;
16603 cu = spec_cu;
16604 }
16605
16606 for (child = die->child;
16607 child != NULL;
16608 child = child->sibling)
16609 {
16610 if (child->tag == DW_TAG_subprogram)
16611 {
16612 struct attribute *attr;
16613
16614 attr = dwarf2_attr (child, DW_AT_linkage_name, cu);
16615 if (attr == NULL)
16616 attr = dwarf2_attr (child, DW_AT_MIPS_linkage_name, cu);
16617 if (attr != NULL)
16618 {
16619 char *actual_name
16620 = language_class_name_from_physname (cu->language_defn,
16621 DW_STRING (attr));
16622 char *name = NULL;
16623
16624 if (actual_name != NULL)
16625 {
15d034d0 16626 const char *die_name = dwarf2_name (die, cu);
abc72ce4
DE
16627
16628 if (die_name != NULL
16629 && strcmp (die_name, actual_name) != 0)
16630 {
16631 /* Strip off the class name from the full name.
16632 We want the prefix. */
16633 int die_name_len = strlen (die_name);
16634 int actual_name_len = strlen (actual_name);
16635
16636 /* Test for '::' as a sanity check. */
16637 if (actual_name_len > die_name_len + 2
3e43a32a
MS
16638 && actual_name[actual_name_len
16639 - die_name_len - 1] == ':')
abc72ce4 16640 name =
10f0c4bb
TT
16641 obstack_copy0 (&cu->objfile->objfile_obstack,
16642 actual_name,
16643 actual_name_len - die_name_len - 2);
abc72ce4
DE
16644 }
16645 }
16646 xfree (actual_name);
16647 return name;
16648 }
16649 }
16650 }
16651
16652 return NULL;
16653}
16654
96408a79
SA
16655/* GCC might emit a nameless typedef that has a linkage name. Determine the
16656 prefix part in such case. See
16657 http://gcc.gnu.org/bugzilla/show_bug.cgi?id=47510. */
16658
16659static char *
16660anonymous_struct_prefix (struct die_info *die, struct dwarf2_cu *cu)
16661{
16662 struct attribute *attr;
16663 char *base;
16664
16665 if (die->tag != DW_TAG_class_type && die->tag != DW_TAG_interface_type
16666 && die->tag != DW_TAG_structure_type && die->tag != DW_TAG_union_type)
16667 return NULL;
16668
16669 attr = dwarf2_attr (die, DW_AT_name, cu);
16670 if (attr != NULL && DW_STRING (attr) != NULL)
16671 return NULL;
16672
16673 attr = dwarf2_attr (die, DW_AT_linkage_name, cu);
16674 if (attr == NULL)
16675 attr = dwarf2_attr (die, DW_AT_MIPS_linkage_name, cu);
16676 if (attr == NULL || DW_STRING (attr) == NULL)
16677 return NULL;
16678
16679 /* dwarf2_name had to be already called. */
16680 gdb_assert (DW_STRING_IS_CANONICAL (attr));
16681
16682 /* Strip the base name, keep any leading namespaces/classes. */
16683 base = strrchr (DW_STRING (attr), ':');
16684 if (base == NULL || base == DW_STRING (attr) || base[-1] != ':')
16685 return "";
16686
10f0c4bb
TT
16687 return obstack_copy0 (&cu->objfile->objfile_obstack,
16688 DW_STRING (attr), &base[-1] - DW_STRING (attr));
96408a79
SA
16689}
16690
fdde2d81 16691/* Return the name of the namespace/class that DIE is defined within,
0114d602 16692 or "" if we can't tell. The caller should not xfree the result.
fdde2d81 16693
0114d602
DJ
16694 For example, if we're within the method foo() in the following
16695 code:
16696
16697 namespace N {
16698 class C {
16699 void foo () {
16700 }
16701 };
16702 }
16703
16704 then determine_prefix on foo's die will return "N::C". */
fdde2d81 16705
0d5cff50 16706static const char *
e142c38c 16707determine_prefix (struct die_info *die, struct dwarf2_cu *cu)
63d06c5c 16708{
0114d602
DJ
16709 struct die_info *parent, *spec_die;
16710 struct dwarf2_cu *spec_cu;
16711 struct type *parent_type;
96408a79 16712 char *retval;
63d06c5c 16713
f55ee35c
JK
16714 if (cu->language != language_cplus && cu->language != language_java
16715 && cu->language != language_fortran)
0114d602
DJ
16716 return "";
16717
96408a79
SA
16718 retval = anonymous_struct_prefix (die, cu);
16719 if (retval)
16720 return retval;
16721
0114d602
DJ
16722 /* We have to be careful in the presence of DW_AT_specification.
16723 For example, with GCC 3.4, given the code
16724
16725 namespace N {
16726 void foo() {
16727 // Definition of N::foo.
16728 }
16729 }
16730
16731 then we'll have a tree of DIEs like this:
16732
16733 1: DW_TAG_compile_unit
16734 2: DW_TAG_namespace // N
16735 3: DW_TAG_subprogram // declaration of N::foo
16736 4: DW_TAG_subprogram // definition of N::foo
16737 DW_AT_specification // refers to die #3
16738
16739 Thus, when processing die #4, we have to pretend that we're in
16740 the context of its DW_AT_specification, namely the contex of die
16741 #3. */
16742 spec_cu = cu;
16743 spec_die = die_specification (die, &spec_cu);
16744 if (spec_die == NULL)
16745 parent = die->parent;
16746 else
63d06c5c 16747 {
0114d602
DJ
16748 parent = spec_die->parent;
16749 cu = spec_cu;
63d06c5c 16750 }
0114d602
DJ
16751
16752 if (parent == NULL)
16753 return "";
98bfdba5
PA
16754 else if (parent->building_fullname)
16755 {
16756 const char *name;
16757 const char *parent_name;
16758
16759 /* It has been seen on RealView 2.2 built binaries,
16760 DW_TAG_template_type_param types actually _defined_ as
16761 children of the parent class:
16762
16763 enum E {};
16764 template class <class Enum> Class{};
16765 Class<enum E> class_e;
16766
16767 1: DW_TAG_class_type (Class)
16768 2: DW_TAG_enumeration_type (E)
16769 3: DW_TAG_enumerator (enum1:0)
16770 3: DW_TAG_enumerator (enum2:1)
16771 ...
16772 2: DW_TAG_template_type_param
16773 DW_AT_type DW_FORM_ref_udata (E)
16774
16775 Besides being broken debug info, it can put GDB into an
16776 infinite loop. Consider:
16777
16778 When we're building the full name for Class<E>, we'll start
16779 at Class, and go look over its template type parameters,
16780 finding E. We'll then try to build the full name of E, and
16781 reach here. We're now trying to build the full name of E,
16782 and look over the parent DIE for containing scope. In the
16783 broken case, if we followed the parent DIE of E, we'd again
16784 find Class, and once again go look at its template type
16785 arguments, etc., etc. Simply don't consider such parent die
16786 as source-level parent of this die (it can't be, the language
16787 doesn't allow it), and break the loop here. */
16788 name = dwarf2_name (die, cu);
16789 parent_name = dwarf2_name (parent, cu);
16790 complaint (&symfile_complaints,
16791 _("template param type '%s' defined within parent '%s'"),
16792 name ? name : "<unknown>",
16793 parent_name ? parent_name : "<unknown>");
16794 return "";
16795 }
63d06c5c 16796 else
0114d602
DJ
16797 switch (parent->tag)
16798 {
63d06c5c 16799 case DW_TAG_namespace:
0114d602 16800 parent_type = read_type_die (parent, cu);
acebe513
UW
16801 /* GCC 4.0 and 4.1 had a bug (PR c++/28460) where they generated bogus
16802 DW_TAG_namespace DIEs with a name of "::" for the global namespace.
16803 Work around this problem here. */
16804 if (cu->language == language_cplus
16805 && strcmp (TYPE_TAG_NAME (parent_type), "::") == 0)
16806 return "";
0114d602
DJ
16807 /* We give a name to even anonymous namespaces. */
16808 return TYPE_TAG_NAME (parent_type);
63d06c5c 16809 case DW_TAG_class_type:
680b30c7 16810 case DW_TAG_interface_type:
63d06c5c 16811 case DW_TAG_structure_type:
0114d602 16812 case DW_TAG_union_type:
f55ee35c 16813 case DW_TAG_module:
0114d602
DJ
16814 parent_type = read_type_die (parent, cu);
16815 if (TYPE_TAG_NAME (parent_type) != NULL)
16816 return TYPE_TAG_NAME (parent_type);
16817 else
16818 /* An anonymous structure is only allowed non-static data
16819 members; no typedefs, no member functions, et cetera.
16820 So it does not need a prefix. */
16821 return "";
abc72ce4 16822 case DW_TAG_compile_unit:
95554aad 16823 case DW_TAG_partial_unit:
abc72ce4
DE
16824 /* gcc-4.5 -gdwarf-4 can drop the enclosing namespace. Cope. */
16825 if (cu->language == language_cplus
8b70b953 16826 && !VEC_empty (dwarf2_section_info_def, dwarf2_per_objfile->types)
abc72ce4
DE
16827 && die->child != NULL
16828 && (die->tag == DW_TAG_class_type
16829 || die->tag == DW_TAG_structure_type
16830 || die->tag == DW_TAG_union_type))
16831 {
16832 char *name = guess_full_die_structure_name (die, cu);
16833 if (name != NULL)
16834 return name;
16835 }
16836 return "";
63d06c5c 16837 default:
8176b9b8 16838 return determine_prefix (parent, cu);
63d06c5c 16839 }
63d06c5c
DC
16840}
16841
3e43a32a
MS
16842/* Return a newly-allocated string formed by concatenating PREFIX and SUFFIX
16843 with appropriate separator. If PREFIX or SUFFIX is NULL or empty, then
16844 simply copy the SUFFIX or PREFIX, respectively. If OBS is non-null, perform
16845 an obconcat, otherwise allocate storage for the result. The CU argument is
16846 used to determine the language and hence, the appropriate separator. */
987504bb 16847
f55ee35c 16848#define MAX_SEP_LEN 7 /* strlen ("__") + strlen ("_MOD_") */
63d06c5c
DC
16849
16850static char *
f55ee35c
JK
16851typename_concat (struct obstack *obs, const char *prefix, const char *suffix,
16852 int physname, struct dwarf2_cu *cu)
63d06c5c 16853{
f55ee35c 16854 const char *lead = "";
5c315b68 16855 const char *sep;
63d06c5c 16856
3e43a32a
MS
16857 if (suffix == NULL || suffix[0] == '\0'
16858 || prefix == NULL || prefix[0] == '\0')
987504bb
JJ
16859 sep = "";
16860 else if (cu->language == language_java)
16861 sep = ".";
f55ee35c
JK
16862 else if (cu->language == language_fortran && physname)
16863 {
16864 /* This is gfortran specific mangling. Normally DW_AT_linkage_name or
16865 DW_AT_MIPS_linkage_name is preferred and used instead. */
16866
16867 lead = "__";
16868 sep = "_MOD_";
16869 }
987504bb
JJ
16870 else
16871 sep = "::";
63d06c5c 16872
6dd47d34
DE
16873 if (prefix == NULL)
16874 prefix = "";
16875 if (suffix == NULL)
16876 suffix = "";
16877
987504bb
JJ
16878 if (obs == NULL)
16879 {
3e43a32a
MS
16880 char *retval
16881 = xmalloc (strlen (prefix) + MAX_SEP_LEN + strlen (suffix) + 1);
9a619af0 16882
f55ee35c
JK
16883 strcpy (retval, lead);
16884 strcat (retval, prefix);
6dd47d34
DE
16885 strcat (retval, sep);
16886 strcat (retval, suffix);
63d06c5c
DC
16887 return retval;
16888 }
987504bb
JJ
16889 else
16890 {
16891 /* We have an obstack. */
f55ee35c 16892 return obconcat (obs, lead, prefix, sep, suffix, (char *) NULL);
987504bb 16893 }
63d06c5c
DC
16894}
16895
c906108c
SS
16896/* Return sibling of die, NULL if no sibling. */
16897
f9aca02d 16898static struct die_info *
fba45db2 16899sibling_die (struct die_info *die)
c906108c 16900{
639d11d3 16901 return die->sibling;
c906108c
SS
16902}
16903
71c25dea
TT
16904/* Get name of a die, return NULL if not found. */
16905
15d034d0
TT
16906static const char *
16907dwarf2_canonicalize_name (const char *name, struct dwarf2_cu *cu,
71c25dea
TT
16908 struct obstack *obstack)
16909{
16910 if (name && cu->language == language_cplus)
16911 {
16912 char *canon_name = cp_canonicalize_string (name);
16913
16914 if (canon_name != NULL)
16915 {
16916 if (strcmp (canon_name, name) != 0)
10f0c4bb 16917 name = obstack_copy0 (obstack, canon_name, strlen (canon_name));
71c25dea
TT
16918 xfree (canon_name);
16919 }
16920 }
16921
16922 return name;
c906108c
SS
16923}
16924
9219021c
DC
16925/* Get name of a die, return NULL if not found. */
16926
15d034d0 16927static const char *
e142c38c 16928dwarf2_name (struct die_info *die, struct dwarf2_cu *cu)
9219021c
DC
16929{
16930 struct attribute *attr;
16931
e142c38c 16932 attr = dwarf2_attr (die, DW_AT_name, cu);
53832f31
TT
16933 if ((!attr || !DW_STRING (attr))
16934 && die->tag != DW_TAG_class_type
16935 && die->tag != DW_TAG_interface_type
16936 && die->tag != DW_TAG_structure_type
16937 && die->tag != DW_TAG_union_type)
71c25dea
TT
16938 return NULL;
16939
16940 switch (die->tag)
16941 {
16942 case DW_TAG_compile_unit:
95554aad 16943 case DW_TAG_partial_unit:
71c25dea
TT
16944 /* Compilation units have a DW_AT_name that is a filename, not
16945 a source language identifier. */
16946 case DW_TAG_enumeration_type:
16947 case DW_TAG_enumerator:
16948 /* These tags always have simple identifiers already; no need
16949 to canonicalize them. */
16950 return DW_STRING (attr);
907af001 16951
418835cc
KS
16952 case DW_TAG_subprogram:
16953 /* Java constructors will all be named "<init>", so return
16954 the class name when we see this special case. */
16955 if (cu->language == language_java
16956 && DW_STRING (attr) != NULL
16957 && strcmp (DW_STRING (attr), "<init>") == 0)
16958 {
16959 struct dwarf2_cu *spec_cu = cu;
16960 struct die_info *spec_die;
16961
16962 /* GCJ will output '<init>' for Java constructor names.
16963 For this special case, return the name of the parent class. */
16964
16965 /* GCJ may output suprogram DIEs with AT_specification set.
16966 If so, use the name of the specified DIE. */
16967 spec_die = die_specification (die, &spec_cu);
16968 if (spec_die != NULL)
16969 return dwarf2_name (spec_die, spec_cu);
16970
16971 do
16972 {
16973 die = die->parent;
16974 if (die->tag == DW_TAG_class_type)
16975 return dwarf2_name (die, cu);
16976 }
95554aad
TT
16977 while (die->tag != DW_TAG_compile_unit
16978 && die->tag != DW_TAG_partial_unit);
418835cc 16979 }
907af001
UW
16980 break;
16981
16982 case DW_TAG_class_type:
16983 case DW_TAG_interface_type:
16984 case DW_TAG_structure_type:
16985 case DW_TAG_union_type:
16986 /* Some GCC versions emit spurious DW_AT_name attributes for unnamed
16987 structures or unions. These were of the form "._%d" in GCC 4.1,
16988 or simply "<anonymous struct>" or "<anonymous union>" in GCC 4.3
16989 and GCC 4.4. We work around this problem by ignoring these. */
53832f31
TT
16990 if (attr && DW_STRING (attr)
16991 && (strncmp (DW_STRING (attr), "._", 2) == 0
16992 || strncmp (DW_STRING (attr), "<anonymous", 10) == 0))
907af001 16993 return NULL;
53832f31
TT
16994
16995 /* GCC might emit a nameless typedef that has a linkage name. See
16996 http://gcc.gnu.org/bugzilla/show_bug.cgi?id=47510. */
16997 if (!attr || DW_STRING (attr) == NULL)
16998 {
df5c6c50 16999 char *demangled = NULL;
53832f31
TT
17000
17001 attr = dwarf2_attr (die, DW_AT_linkage_name, cu);
17002 if (attr == NULL)
17003 attr = dwarf2_attr (die, DW_AT_MIPS_linkage_name, cu);
17004
17005 if (attr == NULL || DW_STRING (attr) == NULL)
17006 return NULL;
17007
df5c6c50
JK
17008 /* Avoid demangling DW_STRING (attr) the second time on a second
17009 call for the same DIE. */
17010 if (!DW_STRING_IS_CANONICAL (attr))
17011 demangled = cplus_demangle (DW_STRING (attr), DMGL_TYPES);
53832f31
TT
17012
17013 if (demangled)
17014 {
96408a79
SA
17015 char *base;
17016
53832f31 17017 /* FIXME: we already did this for the partial symbol... */
10f0c4bb
TT
17018 DW_STRING (attr) = obstack_copy0 (&cu->objfile->objfile_obstack,
17019 demangled, strlen (demangled));
53832f31
TT
17020 DW_STRING_IS_CANONICAL (attr) = 1;
17021 xfree (demangled);
96408a79
SA
17022
17023 /* Strip any leading namespaces/classes, keep only the base name.
17024 DW_AT_name for named DIEs does not contain the prefixes. */
17025 base = strrchr (DW_STRING (attr), ':');
17026 if (base && base > DW_STRING (attr) && base[-1] == ':')
17027 return &base[1];
17028 else
17029 return DW_STRING (attr);
53832f31
TT
17030 }
17031 }
907af001
UW
17032 break;
17033
71c25dea 17034 default:
907af001
UW
17035 break;
17036 }
17037
17038 if (!DW_STRING_IS_CANONICAL (attr))
17039 {
17040 DW_STRING (attr)
17041 = dwarf2_canonicalize_name (DW_STRING (attr), cu,
17042 &cu->objfile->objfile_obstack);
17043 DW_STRING_IS_CANONICAL (attr) = 1;
71c25dea 17044 }
907af001 17045 return DW_STRING (attr);
9219021c
DC
17046}
17047
17048/* Return the die that this die in an extension of, or NULL if there
f2f0e013
DJ
17049 is none. *EXT_CU is the CU containing DIE on input, and the CU
17050 containing the return value on output. */
9219021c
DC
17051
17052static struct die_info *
f2f0e013 17053dwarf2_extension (struct die_info *die, struct dwarf2_cu **ext_cu)
9219021c
DC
17054{
17055 struct attribute *attr;
9219021c 17056
f2f0e013 17057 attr = dwarf2_attr (die, DW_AT_extension, *ext_cu);
9219021c
DC
17058 if (attr == NULL)
17059 return NULL;
17060
f2f0e013 17061 return follow_die_ref (die, attr, ext_cu);
9219021c
DC
17062}
17063
c906108c
SS
17064/* Convert a DIE tag into its string name. */
17065
f39c6ffd 17066static const char *
aa1ee363 17067dwarf_tag_name (unsigned tag)
c906108c 17068{
f39c6ffd
TT
17069 const char *name = get_DW_TAG_name (tag);
17070
17071 if (name == NULL)
17072 return "DW_TAG_<unknown>";
17073
17074 return name;
c906108c
SS
17075}
17076
17077/* Convert a DWARF attribute code into its string name. */
17078
f39c6ffd 17079static const char *
aa1ee363 17080dwarf_attr_name (unsigned attr)
c906108c 17081{
f39c6ffd
TT
17082 const char *name;
17083
c764a876 17084#ifdef MIPS /* collides with DW_AT_HP_block_index */
f39c6ffd
TT
17085 if (attr == DW_AT_MIPS_fde)
17086 return "DW_AT_MIPS_fde";
17087#else
17088 if (attr == DW_AT_HP_block_index)
17089 return "DW_AT_HP_block_index";
c764a876 17090#endif
f39c6ffd
TT
17091
17092 name = get_DW_AT_name (attr);
17093
17094 if (name == NULL)
17095 return "DW_AT_<unknown>";
17096
17097 return name;
c906108c
SS
17098}
17099
17100/* Convert a DWARF value form code into its string name. */
17101
f39c6ffd 17102static const char *
aa1ee363 17103dwarf_form_name (unsigned form)
c906108c 17104{
f39c6ffd
TT
17105 const char *name = get_DW_FORM_name (form);
17106
17107 if (name == NULL)
17108 return "DW_FORM_<unknown>";
17109
17110 return name;
c906108c
SS
17111}
17112
17113static char *
fba45db2 17114dwarf_bool_name (unsigned mybool)
c906108c
SS
17115{
17116 if (mybool)
17117 return "TRUE";
17118 else
17119 return "FALSE";
17120}
17121
17122/* Convert a DWARF type code into its string name. */
17123
f39c6ffd 17124static const char *
aa1ee363 17125dwarf_type_encoding_name (unsigned enc)
c906108c 17126{
f39c6ffd 17127 const char *name = get_DW_ATE_name (enc);
c906108c 17128
f39c6ffd
TT
17129 if (name == NULL)
17130 return "DW_ATE_<unknown>";
c906108c 17131
f39c6ffd 17132 return name;
c906108c 17133}
c906108c 17134
f9aca02d 17135static void
d97bc12b 17136dump_die_shallow (struct ui_file *f, int indent, struct die_info *die)
c906108c
SS
17137{
17138 unsigned int i;
17139
d97bc12b
DE
17140 print_spaces (indent, f);
17141 fprintf_unfiltered (f, "Die: %s (abbrev %d, offset 0x%x)\n",
b64f50a1 17142 dwarf_tag_name (die->tag), die->abbrev, die->offset.sect_off);
d97bc12b
DE
17143
17144 if (die->parent != NULL)
17145 {
17146 print_spaces (indent, f);
17147 fprintf_unfiltered (f, " parent at offset: 0x%x\n",
b64f50a1 17148 die->parent->offset.sect_off);
d97bc12b
DE
17149 }
17150
17151 print_spaces (indent, f);
17152 fprintf_unfiltered (f, " has children: %s\n",
639d11d3 17153 dwarf_bool_name (die->child != NULL));
c906108c 17154
d97bc12b
DE
17155 print_spaces (indent, f);
17156 fprintf_unfiltered (f, " attributes:\n");
17157
c906108c
SS
17158 for (i = 0; i < die->num_attrs; ++i)
17159 {
d97bc12b
DE
17160 print_spaces (indent, f);
17161 fprintf_unfiltered (f, " %s (%s) ",
c906108c
SS
17162 dwarf_attr_name (die->attrs[i].name),
17163 dwarf_form_name (die->attrs[i].form));
d97bc12b 17164
c906108c
SS
17165 switch (die->attrs[i].form)
17166 {
c906108c 17167 case DW_FORM_addr:
3019eac3 17168 case DW_FORM_GNU_addr_index:
d97bc12b 17169 fprintf_unfiltered (f, "address: ");
5af949e3 17170 fputs_filtered (hex_string (DW_ADDR (&die->attrs[i])), f);
c906108c
SS
17171 break;
17172 case DW_FORM_block2:
17173 case DW_FORM_block4:
17174 case DW_FORM_block:
17175 case DW_FORM_block1:
56eb65bd
SP
17176 fprintf_unfiltered (f, "block: size %s",
17177 pulongest (DW_BLOCK (&die->attrs[i])->size));
c906108c 17178 break;
2dc7f7b3 17179 case DW_FORM_exprloc:
56eb65bd
SP
17180 fprintf_unfiltered (f, "expression: size %s",
17181 pulongest (DW_BLOCK (&die->attrs[i])->size));
2dc7f7b3 17182 break;
4568ecf9
DE
17183 case DW_FORM_ref_addr:
17184 fprintf_unfiltered (f, "ref address: ");
17185 fputs_filtered (hex_string (DW_UNSND (&die->attrs[i])), f);
17186 break;
36586728
TT
17187 case DW_FORM_GNU_ref_alt:
17188 fprintf_unfiltered (f, "alt ref address: ");
17189 fputs_filtered (hex_string (DW_UNSND (&die->attrs[i])), f);
17190 break;
10b3939b
DJ
17191 case DW_FORM_ref1:
17192 case DW_FORM_ref2:
17193 case DW_FORM_ref4:
4568ecf9
DE
17194 case DW_FORM_ref8:
17195 case DW_FORM_ref_udata:
d97bc12b 17196 fprintf_unfiltered (f, "constant ref: 0x%lx (adjusted)",
4568ecf9 17197 (long) (DW_UNSND (&die->attrs[i])));
10b3939b 17198 break;
c906108c
SS
17199 case DW_FORM_data1:
17200 case DW_FORM_data2:
17201 case DW_FORM_data4:
ce5d95e1 17202 case DW_FORM_data8:
c906108c
SS
17203 case DW_FORM_udata:
17204 case DW_FORM_sdata:
43bbcdc2
PH
17205 fprintf_unfiltered (f, "constant: %s",
17206 pulongest (DW_UNSND (&die->attrs[i])));
c906108c 17207 break;
2dc7f7b3
TT
17208 case DW_FORM_sec_offset:
17209 fprintf_unfiltered (f, "section offset: %s",
17210 pulongest (DW_UNSND (&die->attrs[i])));
17211 break;
55f1336d 17212 case DW_FORM_ref_sig8:
348e048f
DE
17213 if (DW_SIGNATURED_TYPE (&die->attrs[i]) != NULL)
17214 fprintf_unfiltered (f, "signatured type, offset: 0x%x",
b64f50a1 17215 DW_SIGNATURED_TYPE (&die->attrs[i])->per_cu.offset.sect_off);
348e048f
DE
17216 else
17217 fprintf_unfiltered (f, "signatured type, offset: unknown");
17218 break;
c906108c 17219 case DW_FORM_string:
4bdf3d34 17220 case DW_FORM_strp:
3019eac3 17221 case DW_FORM_GNU_str_index:
36586728 17222 case DW_FORM_GNU_strp_alt:
8285870a 17223 fprintf_unfiltered (f, "string: \"%s\" (%s canonicalized)",
c906108c 17224 DW_STRING (&die->attrs[i])
8285870a
JK
17225 ? DW_STRING (&die->attrs[i]) : "",
17226 DW_STRING_IS_CANONICAL (&die->attrs[i]) ? "is" : "not");
c906108c
SS
17227 break;
17228 case DW_FORM_flag:
17229 if (DW_UNSND (&die->attrs[i]))
d97bc12b 17230 fprintf_unfiltered (f, "flag: TRUE");
c906108c 17231 else
d97bc12b 17232 fprintf_unfiltered (f, "flag: FALSE");
c906108c 17233 break;
2dc7f7b3
TT
17234 case DW_FORM_flag_present:
17235 fprintf_unfiltered (f, "flag: TRUE");
17236 break;
a8329558 17237 case DW_FORM_indirect:
0963b4bd
MS
17238 /* The reader will have reduced the indirect form to
17239 the "base form" so this form should not occur. */
3e43a32a
MS
17240 fprintf_unfiltered (f,
17241 "unexpected attribute form: DW_FORM_indirect");
a8329558 17242 break;
c906108c 17243 default:
d97bc12b 17244 fprintf_unfiltered (f, "unsupported attribute form: %d.",
c5aa993b 17245 die->attrs[i].form);
d97bc12b 17246 break;
c906108c 17247 }
d97bc12b 17248 fprintf_unfiltered (f, "\n");
c906108c
SS
17249 }
17250}
17251
f9aca02d 17252static void
d97bc12b 17253dump_die_for_error (struct die_info *die)
c906108c 17254{
d97bc12b
DE
17255 dump_die_shallow (gdb_stderr, 0, die);
17256}
17257
17258static void
17259dump_die_1 (struct ui_file *f, int level, int max_level, struct die_info *die)
17260{
17261 int indent = level * 4;
17262
17263 gdb_assert (die != NULL);
17264
17265 if (level >= max_level)
17266 return;
17267
17268 dump_die_shallow (f, indent, die);
17269
17270 if (die->child != NULL)
c906108c 17271 {
d97bc12b
DE
17272 print_spaces (indent, f);
17273 fprintf_unfiltered (f, " Children:");
17274 if (level + 1 < max_level)
17275 {
17276 fprintf_unfiltered (f, "\n");
17277 dump_die_1 (f, level + 1, max_level, die->child);
17278 }
17279 else
17280 {
3e43a32a
MS
17281 fprintf_unfiltered (f,
17282 " [not printed, max nesting level reached]\n");
d97bc12b
DE
17283 }
17284 }
17285
17286 if (die->sibling != NULL && level > 0)
17287 {
17288 dump_die_1 (f, level, max_level, die->sibling);
c906108c
SS
17289 }
17290}
17291
d97bc12b
DE
17292/* This is called from the pdie macro in gdbinit.in.
17293 It's not static so gcc will keep a copy callable from gdb. */
17294
17295void
17296dump_die (struct die_info *die, int max_level)
17297{
17298 dump_die_1 (gdb_stdlog, 0, max_level, die);
17299}
17300
f9aca02d 17301static void
51545339 17302store_in_ref_table (struct die_info *die, struct dwarf2_cu *cu)
c906108c 17303{
51545339 17304 void **slot;
c906108c 17305
b64f50a1
JK
17306 slot = htab_find_slot_with_hash (cu->die_hash, die, die->offset.sect_off,
17307 INSERT);
51545339
DJ
17308
17309 *slot = die;
c906108c
SS
17310}
17311
b64f50a1
JK
17312/* DW_ADDR is always stored already as sect_offset; despite for the forms
17313 besides DW_FORM_ref_addr it is stored as cu_offset in the DWARF file. */
17314
93311388
DE
17315static int
17316is_ref_attr (struct attribute *attr)
c906108c 17317{
c906108c
SS
17318 switch (attr->form)
17319 {
17320 case DW_FORM_ref_addr:
c906108c
SS
17321 case DW_FORM_ref1:
17322 case DW_FORM_ref2:
17323 case DW_FORM_ref4:
613e1657 17324 case DW_FORM_ref8:
c906108c 17325 case DW_FORM_ref_udata:
36586728 17326 case DW_FORM_GNU_ref_alt:
93311388 17327 return 1;
c906108c 17328 default:
93311388 17329 return 0;
c906108c 17330 }
93311388
DE
17331}
17332
b64f50a1
JK
17333/* Return DIE offset of ATTR. Return 0 with complaint if ATTR is not of the
17334 required kind. */
17335
17336static sect_offset
93311388
DE
17337dwarf2_get_ref_die_offset (struct attribute *attr)
17338{
4568ecf9 17339 sect_offset retval = { DW_UNSND (attr) };
b64f50a1 17340
93311388 17341 if (is_ref_attr (attr))
b64f50a1 17342 return retval;
93311388 17343
b64f50a1 17344 retval.sect_off = 0;
93311388
DE
17345 complaint (&symfile_complaints,
17346 _("unsupported die ref attribute form: '%s'"),
17347 dwarf_form_name (attr->form));
b64f50a1 17348 return retval;
c906108c
SS
17349}
17350
43bbcdc2
PH
17351/* Return the constant value held by ATTR. Return DEFAULT_VALUE if
17352 * the value held by the attribute is not constant. */
a02abb62 17353
43bbcdc2 17354static LONGEST
a02abb62
JB
17355dwarf2_get_attr_constant_value (struct attribute *attr, int default_value)
17356{
17357 if (attr->form == DW_FORM_sdata)
17358 return DW_SND (attr);
17359 else if (attr->form == DW_FORM_udata
17360 || attr->form == DW_FORM_data1
17361 || attr->form == DW_FORM_data2
17362 || attr->form == DW_FORM_data4
17363 || attr->form == DW_FORM_data8)
17364 return DW_UNSND (attr);
17365 else
17366 {
3e43a32a
MS
17367 complaint (&symfile_complaints,
17368 _("Attribute value is not a constant (%s)"),
a02abb62
JB
17369 dwarf_form_name (attr->form));
17370 return default_value;
17371 }
17372}
17373
348e048f
DE
17374/* Follow reference or signature attribute ATTR of SRC_DIE.
17375 On entry *REF_CU is the CU of SRC_DIE.
17376 On exit *REF_CU is the CU of the result. */
17377
17378static struct die_info *
17379follow_die_ref_or_sig (struct die_info *src_die, struct attribute *attr,
17380 struct dwarf2_cu **ref_cu)
17381{
17382 struct die_info *die;
17383
17384 if (is_ref_attr (attr))
17385 die = follow_die_ref (src_die, attr, ref_cu);
55f1336d 17386 else if (attr->form == DW_FORM_ref_sig8)
348e048f
DE
17387 die = follow_die_sig (src_die, attr, ref_cu);
17388 else
17389 {
17390 dump_die_for_error (src_die);
17391 error (_("Dwarf Error: Expected reference attribute [in module %s]"),
17392 (*ref_cu)->objfile->name);
17393 }
17394
17395 return die;
03dd20cc
DJ
17396}
17397
5c631832 17398/* Follow reference OFFSET.
673bfd45
DE
17399 On entry *REF_CU is the CU of the source die referencing OFFSET.
17400 On exit *REF_CU is the CU of the result.
17401 Returns NULL if OFFSET is invalid. */
f504f079 17402
f9aca02d 17403static struct die_info *
36586728
TT
17404follow_die_offset (sect_offset offset, int offset_in_dwz,
17405 struct dwarf2_cu **ref_cu)
c906108c 17406{
10b3939b 17407 struct die_info temp_die;
f2f0e013 17408 struct dwarf2_cu *target_cu, *cu = *ref_cu;
10b3939b 17409
348e048f
DE
17410 gdb_assert (cu->per_cu != NULL);
17411
98bfdba5
PA
17412 target_cu = cu;
17413
3019eac3 17414 if (cu->per_cu->is_debug_types)
348e048f
DE
17415 {
17416 /* .debug_types CUs cannot reference anything outside their CU.
17417 If they need to, they have to reference a signatured type via
55f1336d 17418 DW_FORM_ref_sig8. */
348e048f 17419 if (! offset_in_cu_p (&cu->header, offset))
5c631832 17420 return NULL;
348e048f 17421 }
36586728
TT
17422 else if (offset_in_dwz != cu->per_cu->is_dwz
17423 || ! offset_in_cu_p (&cu->header, offset))
10b3939b
DJ
17424 {
17425 struct dwarf2_per_cu_data *per_cu;
9a619af0 17426
36586728
TT
17427 per_cu = dwarf2_find_containing_comp_unit (offset, offset_in_dwz,
17428 cu->objfile);
03dd20cc
DJ
17429
17430 /* If necessary, add it to the queue and load its DIEs. */
95554aad
TT
17431 if (maybe_queue_comp_unit (cu, per_cu, cu->language))
17432 load_full_comp_unit (per_cu, cu->language);
03dd20cc 17433
10b3939b
DJ
17434 target_cu = per_cu->cu;
17435 }
98bfdba5
PA
17436 else if (cu->dies == NULL)
17437 {
17438 /* We're loading full DIEs during partial symbol reading. */
17439 gdb_assert (dwarf2_per_objfile->reading_partial_symbols);
95554aad 17440 load_full_comp_unit (cu->per_cu, language_minimal);
98bfdba5 17441 }
c906108c 17442
f2f0e013 17443 *ref_cu = target_cu;
51545339 17444 temp_die.offset = offset;
b64f50a1 17445 return htab_find_with_hash (target_cu->die_hash, &temp_die, offset.sect_off);
5c631832 17446}
10b3939b 17447
5c631832
JK
17448/* Follow reference attribute ATTR of SRC_DIE.
17449 On entry *REF_CU is the CU of SRC_DIE.
17450 On exit *REF_CU is the CU of the result. */
17451
17452static struct die_info *
17453follow_die_ref (struct die_info *src_die, struct attribute *attr,
17454 struct dwarf2_cu **ref_cu)
17455{
b64f50a1 17456 sect_offset offset = dwarf2_get_ref_die_offset (attr);
5c631832
JK
17457 struct dwarf2_cu *cu = *ref_cu;
17458 struct die_info *die;
17459
36586728
TT
17460 die = follow_die_offset (offset,
17461 (attr->form == DW_FORM_GNU_ref_alt
17462 || cu->per_cu->is_dwz),
17463 ref_cu);
5c631832
JK
17464 if (!die)
17465 error (_("Dwarf Error: Cannot find DIE at 0x%x referenced from DIE "
17466 "at 0x%x [in module %s]"),
b64f50a1 17467 offset.sect_off, src_die->offset.sect_off, cu->objfile->name);
348e048f 17468
5c631832
JK
17469 return die;
17470}
17471
d83e736b
JK
17472/* Return DWARF block referenced by DW_AT_location of DIE at OFFSET at PER_CU.
17473 Returned value is intended for DW_OP_call*. Returned
17474 dwarf2_locexpr_baton->data has lifetime of PER_CU->OBJFILE. */
5c631832
JK
17475
17476struct dwarf2_locexpr_baton
8b9737bf
TT
17477dwarf2_fetch_die_loc_sect_off (sect_offset offset,
17478 struct dwarf2_per_cu_data *per_cu,
17479 CORE_ADDR (*get_frame_pc) (void *baton),
17480 void *baton)
5c631832 17481{
918dd910 17482 struct dwarf2_cu *cu;
5c631832
JK
17483 struct die_info *die;
17484 struct attribute *attr;
17485 struct dwarf2_locexpr_baton retval;
17486
8cf6f0b1
TT
17487 dw2_setup (per_cu->objfile);
17488
918dd910
JK
17489 if (per_cu->cu == NULL)
17490 load_cu (per_cu);
17491 cu = per_cu->cu;
17492
36586728 17493 die = follow_die_offset (offset, per_cu->is_dwz, &cu);
5c631832
JK
17494 if (!die)
17495 error (_("Dwarf Error: Cannot find DIE at 0x%x referenced in module %s"),
b64f50a1 17496 offset.sect_off, per_cu->objfile->name);
5c631832
JK
17497
17498 attr = dwarf2_attr (die, DW_AT_location, cu);
17499 if (!attr)
17500 {
e103e986
JK
17501 /* DWARF: "If there is no such attribute, then there is no effect.".
17502 DATA is ignored if SIZE is 0. */
5c631832 17503
e103e986 17504 retval.data = NULL;
5c631832
JK
17505 retval.size = 0;
17506 }
8cf6f0b1
TT
17507 else if (attr_form_is_section_offset (attr))
17508 {
17509 struct dwarf2_loclist_baton loclist_baton;
17510 CORE_ADDR pc = (*get_frame_pc) (baton);
17511 size_t size;
17512
17513 fill_in_loclist_baton (cu, &loclist_baton, attr);
17514
17515 retval.data = dwarf2_find_location_expression (&loclist_baton,
17516 &size, pc);
17517 retval.size = size;
17518 }
5c631832
JK
17519 else
17520 {
17521 if (!attr_form_is_block (attr))
17522 error (_("Dwarf Error: DIE at 0x%x referenced in module %s "
17523 "is neither DW_FORM_block* nor DW_FORM_exprloc"),
b64f50a1 17524 offset.sect_off, per_cu->objfile->name);
5c631832
JK
17525
17526 retval.data = DW_BLOCK (attr)->data;
17527 retval.size = DW_BLOCK (attr)->size;
17528 }
17529 retval.per_cu = cu->per_cu;
918dd910 17530
918dd910
JK
17531 age_cached_comp_units ();
17532
5c631832 17533 return retval;
348e048f
DE
17534}
17535
8b9737bf
TT
17536/* Like dwarf2_fetch_die_loc_sect_off, but take a CU
17537 offset. */
17538
17539struct dwarf2_locexpr_baton
17540dwarf2_fetch_die_loc_cu_off (cu_offset offset_in_cu,
17541 struct dwarf2_per_cu_data *per_cu,
17542 CORE_ADDR (*get_frame_pc) (void *baton),
17543 void *baton)
17544{
17545 sect_offset offset = { per_cu->offset.sect_off + offset_in_cu.cu_off };
17546
17547 return dwarf2_fetch_die_loc_sect_off (offset, per_cu, get_frame_pc, baton);
17548}
17549
8a9b8146
TT
17550/* Return the type of the DIE at DIE_OFFSET in the CU named by
17551 PER_CU. */
17552
17553struct type *
b64f50a1 17554dwarf2_get_die_type (cu_offset die_offset,
8a9b8146
TT
17555 struct dwarf2_per_cu_data *per_cu)
17556{
b64f50a1
JK
17557 sect_offset die_offset_sect;
17558
8a9b8146 17559 dw2_setup (per_cu->objfile);
b64f50a1
JK
17560
17561 die_offset_sect.sect_off = per_cu->offset.sect_off + die_offset.cu_off;
17562 return get_die_type_at_offset (die_offset_sect, per_cu);
8a9b8146
TT
17563}
17564
348e048f
DE
17565/* Follow the signature attribute ATTR in SRC_DIE.
17566 On entry *REF_CU is the CU of SRC_DIE.
17567 On exit *REF_CU is the CU of the result. */
17568
17569static struct die_info *
17570follow_die_sig (struct die_info *src_die, struct attribute *attr,
17571 struct dwarf2_cu **ref_cu)
17572{
17573 struct objfile *objfile = (*ref_cu)->objfile;
17574 struct die_info temp_die;
17575 struct signatured_type *sig_type = DW_SIGNATURED_TYPE (attr);
17576 struct dwarf2_cu *sig_cu;
17577 struct die_info *die;
17578
17579 /* sig_type will be NULL if the signatured type is missing from
17580 the debug info. */
17581 if (sig_type == NULL)
17582 error (_("Dwarf Error: Cannot find signatured DIE referenced from DIE "
17583 "at 0x%x [in module %s]"),
b64f50a1 17584 src_die->offset.sect_off, objfile->name);
348e048f
DE
17585
17586 /* If necessary, add it to the queue and load its DIEs. */
17587
95554aad 17588 if (maybe_queue_comp_unit (*ref_cu, &sig_type->per_cu, language_minimal))
a0f42c21 17589 read_signatured_type (sig_type);
348e048f
DE
17590
17591 gdb_assert (sig_type->per_cu.cu != NULL);
17592
17593 sig_cu = sig_type->per_cu.cu;
3019eac3
DE
17594 gdb_assert (sig_type->type_offset_in_section.sect_off != 0);
17595 temp_die.offset = sig_type->type_offset_in_section;
b64f50a1
JK
17596 die = htab_find_with_hash (sig_cu->die_hash, &temp_die,
17597 temp_die.offset.sect_off);
348e048f
DE
17598 if (die)
17599 {
796a7ff8
DE
17600 /* For .gdb_index version 7 keep track of included TUs.
17601 http://sourceware.org/bugzilla/show_bug.cgi?id=15021. */
17602 if (dwarf2_per_objfile->index_table != NULL
17603 && dwarf2_per_objfile->index_table->version <= 7)
17604 {
17605 VEC_safe_push (dwarf2_per_cu_ptr,
17606 (*ref_cu)->per_cu->imported_symtabs,
17607 sig_cu->per_cu);
17608 }
17609
348e048f
DE
17610 *ref_cu = sig_cu;
17611 return die;
17612 }
17613
3e43a32a
MS
17614 error (_("Dwarf Error: Cannot find signatured DIE at 0x%x referenced "
17615 "from DIE at 0x%x [in module %s]"),
b64f50a1 17616 temp_die.offset.sect_off, src_die->offset.sect_off, objfile->name);
348e048f
DE
17617}
17618
17619/* Given an offset of a signatured type, return its signatured_type. */
17620
17621static struct signatured_type *
8b70b953
TT
17622lookup_signatured_type_at_offset (struct objfile *objfile,
17623 struct dwarf2_section_info *section,
b64f50a1 17624 sect_offset offset)
348e048f 17625{
b64f50a1 17626 gdb_byte *info_ptr = section->buffer + offset.sect_off;
348e048f
DE
17627 unsigned int length, initial_length_size;
17628 unsigned int sig_offset;
52dc124a 17629 struct signatured_type find_entry, *sig_type;
348e048f
DE
17630
17631 length = read_initial_length (objfile->obfd, info_ptr, &initial_length_size);
17632 sig_offset = (initial_length_size
17633 + 2 /*version*/
17634 + (initial_length_size == 4 ? 4 : 8) /*debug_abbrev_offset*/
17635 + 1 /*address_size*/);
17636 find_entry.signature = bfd_get_64 (objfile->obfd, info_ptr + sig_offset);
52dc124a 17637 sig_type = htab_find (dwarf2_per_objfile->signatured_types, &find_entry);
348e048f
DE
17638
17639 /* This is only used to lookup previously recorded types.
17640 If we didn't find it, it's our bug. */
52dc124a
DE
17641 gdb_assert (sig_type != NULL);
17642 gdb_assert (offset.sect_off == sig_type->per_cu.offset.sect_off);
348e048f 17643
52dc124a 17644 return sig_type;
348e048f
DE
17645}
17646
e5fe5e75 17647/* Load the DIEs associated with type unit PER_CU into memory. */
348e048f
DE
17648
17649static void
e5fe5e75 17650load_full_type_unit (struct dwarf2_per_cu_data *per_cu)
348e048f 17651{
52dc124a 17652 struct signatured_type *sig_type;
348e048f 17653
f4dc4d17
DE
17654 /* Caller is responsible for ensuring type_unit_groups don't get here. */
17655 gdb_assert (! IS_TYPE_UNIT_GROUP (per_cu));
17656
6721b2ec
DE
17657 /* We have the per_cu, but we need the signatured_type.
17658 Fortunately this is an easy translation. */
17659 gdb_assert (per_cu->is_debug_types);
17660 sig_type = (struct signatured_type *) per_cu;
348e048f 17661
6721b2ec 17662 gdb_assert (per_cu->cu == NULL);
348e048f 17663
52dc124a 17664 read_signatured_type (sig_type);
348e048f 17665
6721b2ec 17666 gdb_assert (per_cu->cu != NULL);
348e048f
DE
17667}
17668
dee91e82
DE
17669/* die_reader_func for read_signatured_type.
17670 This is identical to load_full_comp_unit_reader,
17671 but is kept separate for now. */
348e048f
DE
17672
17673static void
dee91e82
DE
17674read_signatured_type_reader (const struct die_reader_specs *reader,
17675 gdb_byte *info_ptr,
17676 struct die_info *comp_unit_die,
17677 int has_children,
17678 void *data)
348e048f 17679{
dee91e82 17680 struct dwarf2_cu *cu = reader->cu;
348e048f 17681
dee91e82
DE
17682 gdb_assert (cu->die_hash == NULL);
17683 cu->die_hash =
17684 htab_create_alloc_ex (cu->header.length / 12,
17685 die_hash,
17686 die_eq,
17687 NULL,
17688 &cu->comp_unit_obstack,
17689 hashtab_obstack_allocate,
17690 dummy_obstack_deallocate);
348e048f 17691
dee91e82
DE
17692 if (has_children)
17693 comp_unit_die->child = read_die_and_siblings (reader, info_ptr,
17694 &info_ptr, comp_unit_die);
17695 cu->dies = comp_unit_die;
17696 /* comp_unit_die is not stored in die_hash, no need. */
348e048f
DE
17697
17698 /* We try not to read any attributes in this function, because not
9cdd5dbd 17699 all CUs needed for references have been loaded yet, and symbol
348e048f 17700 table processing isn't initialized. But we have to set the CU language,
dee91e82
DE
17701 or we won't be able to build types correctly.
17702 Similarly, if we do not read the producer, we can not apply
17703 producer-specific interpretation. */
95554aad 17704 prepare_one_comp_unit (cu, cu->dies, language_minimal);
dee91e82 17705}
348e048f 17706
3019eac3
DE
17707/* Read in a signatured type and build its CU and DIEs.
17708 If the type is a stub for the real type in a DWO file,
17709 read in the real type from the DWO file as well. */
dee91e82
DE
17710
17711static void
17712read_signatured_type (struct signatured_type *sig_type)
17713{
17714 struct dwarf2_per_cu_data *per_cu = &sig_type->per_cu;
348e048f 17715
3019eac3 17716 gdb_assert (per_cu->is_debug_types);
dee91e82 17717 gdb_assert (per_cu->cu == NULL);
348e048f 17718
f4dc4d17
DE
17719 init_cutu_and_read_dies (per_cu, NULL, 0, 1,
17720 read_signatured_type_reader, NULL);
c906108c
SS
17721}
17722
c906108c
SS
17723/* Decode simple location descriptions.
17724 Given a pointer to a dwarf block that defines a location, compute
17725 the location and return the value.
17726
4cecd739
DJ
17727 NOTE drow/2003-11-18: This function is called in two situations
17728 now: for the address of static or global variables (partial symbols
17729 only) and for offsets into structures which are expected to be
17730 (more or less) constant. The partial symbol case should go away,
17731 and only the constant case should remain. That will let this
17732 function complain more accurately. A few special modes are allowed
17733 without complaint for global variables (for instance, global
17734 register values and thread-local values).
c906108c
SS
17735
17736 A location description containing no operations indicates that the
4cecd739 17737 object is optimized out. The return value is 0 for that case.
6b992462
DJ
17738 FIXME drow/2003-11-16: No callers check for this case any more; soon all
17739 callers will only want a very basic result and this can become a
21ae7a4d
JK
17740 complaint.
17741
17742 Note that stack[0] is unused except as a default error return. */
c906108c
SS
17743
17744static CORE_ADDR
e7c27a73 17745decode_locdesc (struct dwarf_block *blk, struct dwarf2_cu *cu)
c906108c 17746{
e7c27a73 17747 struct objfile *objfile = cu->objfile;
56eb65bd
SP
17748 size_t i;
17749 size_t size = blk->size;
21ae7a4d
JK
17750 gdb_byte *data = blk->data;
17751 CORE_ADDR stack[64];
17752 int stacki;
17753 unsigned int bytes_read, unsnd;
17754 gdb_byte op;
c906108c 17755
21ae7a4d
JK
17756 i = 0;
17757 stacki = 0;
17758 stack[stacki] = 0;
17759 stack[++stacki] = 0;
17760
17761 while (i < size)
17762 {
17763 op = data[i++];
17764 switch (op)
17765 {
17766 case DW_OP_lit0:
17767 case DW_OP_lit1:
17768 case DW_OP_lit2:
17769 case DW_OP_lit3:
17770 case DW_OP_lit4:
17771 case DW_OP_lit5:
17772 case DW_OP_lit6:
17773 case DW_OP_lit7:
17774 case DW_OP_lit8:
17775 case DW_OP_lit9:
17776 case DW_OP_lit10:
17777 case DW_OP_lit11:
17778 case DW_OP_lit12:
17779 case DW_OP_lit13:
17780 case DW_OP_lit14:
17781 case DW_OP_lit15:
17782 case DW_OP_lit16:
17783 case DW_OP_lit17:
17784 case DW_OP_lit18:
17785 case DW_OP_lit19:
17786 case DW_OP_lit20:
17787 case DW_OP_lit21:
17788 case DW_OP_lit22:
17789 case DW_OP_lit23:
17790 case DW_OP_lit24:
17791 case DW_OP_lit25:
17792 case DW_OP_lit26:
17793 case DW_OP_lit27:
17794 case DW_OP_lit28:
17795 case DW_OP_lit29:
17796 case DW_OP_lit30:
17797 case DW_OP_lit31:
17798 stack[++stacki] = op - DW_OP_lit0;
17799 break;
f1bea926 17800
21ae7a4d
JK
17801 case DW_OP_reg0:
17802 case DW_OP_reg1:
17803 case DW_OP_reg2:
17804 case DW_OP_reg3:
17805 case DW_OP_reg4:
17806 case DW_OP_reg5:
17807 case DW_OP_reg6:
17808 case DW_OP_reg7:
17809 case DW_OP_reg8:
17810 case DW_OP_reg9:
17811 case DW_OP_reg10:
17812 case DW_OP_reg11:
17813 case DW_OP_reg12:
17814 case DW_OP_reg13:
17815 case DW_OP_reg14:
17816 case DW_OP_reg15:
17817 case DW_OP_reg16:
17818 case DW_OP_reg17:
17819 case DW_OP_reg18:
17820 case DW_OP_reg19:
17821 case DW_OP_reg20:
17822 case DW_OP_reg21:
17823 case DW_OP_reg22:
17824 case DW_OP_reg23:
17825 case DW_OP_reg24:
17826 case DW_OP_reg25:
17827 case DW_OP_reg26:
17828 case DW_OP_reg27:
17829 case DW_OP_reg28:
17830 case DW_OP_reg29:
17831 case DW_OP_reg30:
17832 case DW_OP_reg31:
17833 stack[++stacki] = op - DW_OP_reg0;
17834 if (i < size)
17835 dwarf2_complex_location_expr_complaint ();
17836 break;
c906108c 17837
21ae7a4d
JK
17838 case DW_OP_regx:
17839 unsnd = read_unsigned_leb128 (NULL, (data + i), &bytes_read);
17840 i += bytes_read;
17841 stack[++stacki] = unsnd;
17842 if (i < size)
17843 dwarf2_complex_location_expr_complaint ();
17844 break;
c906108c 17845
21ae7a4d
JK
17846 case DW_OP_addr:
17847 stack[++stacki] = read_address (objfile->obfd, &data[i],
17848 cu, &bytes_read);
17849 i += bytes_read;
17850 break;
d53d4ac5 17851
21ae7a4d
JK
17852 case DW_OP_const1u:
17853 stack[++stacki] = read_1_byte (objfile->obfd, &data[i]);
17854 i += 1;
17855 break;
17856
17857 case DW_OP_const1s:
17858 stack[++stacki] = read_1_signed_byte (objfile->obfd, &data[i]);
17859 i += 1;
17860 break;
17861
17862 case DW_OP_const2u:
17863 stack[++stacki] = read_2_bytes (objfile->obfd, &data[i]);
17864 i += 2;
17865 break;
17866
17867 case DW_OP_const2s:
17868 stack[++stacki] = read_2_signed_bytes (objfile->obfd, &data[i]);
17869 i += 2;
17870 break;
d53d4ac5 17871
21ae7a4d
JK
17872 case DW_OP_const4u:
17873 stack[++stacki] = read_4_bytes (objfile->obfd, &data[i]);
17874 i += 4;
17875 break;
17876
17877 case DW_OP_const4s:
17878 stack[++stacki] = read_4_signed_bytes (objfile->obfd, &data[i]);
17879 i += 4;
17880 break;
17881
585861ea
JK
17882 case DW_OP_const8u:
17883 stack[++stacki] = read_8_bytes (objfile->obfd, &data[i]);
17884 i += 8;
17885 break;
17886
21ae7a4d
JK
17887 case DW_OP_constu:
17888 stack[++stacki] = read_unsigned_leb128 (NULL, (data + i),
17889 &bytes_read);
17890 i += bytes_read;
17891 break;
17892
17893 case DW_OP_consts:
17894 stack[++stacki] = read_signed_leb128 (NULL, (data + i), &bytes_read);
17895 i += bytes_read;
17896 break;
17897
17898 case DW_OP_dup:
17899 stack[stacki + 1] = stack[stacki];
17900 stacki++;
17901 break;
17902
17903 case DW_OP_plus:
17904 stack[stacki - 1] += stack[stacki];
17905 stacki--;
17906 break;
17907
17908 case DW_OP_plus_uconst:
17909 stack[stacki] += read_unsigned_leb128 (NULL, (data + i),
17910 &bytes_read);
17911 i += bytes_read;
17912 break;
17913
17914 case DW_OP_minus:
17915 stack[stacki - 1] -= stack[stacki];
17916 stacki--;
17917 break;
17918
17919 case DW_OP_deref:
17920 /* If we're not the last op, then we definitely can't encode
17921 this using GDB's address_class enum. This is valid for partial
17922 global symbols, although the variable's address will be bogus
17923 in the psymtab. */
17924 if (i < size)
17925 dwarf2_complex_location_expr_complaint ();
17926 break;
17927
17928 case DW_OP_GNU_push_tls_address:
17929 /* The top of the stack has the offset from the beginning
17930 of the thread control block at which the variable is located. */
17931 /* Nothing should follow this operator, so the top of stack would
17932 be returned. */
17933 /* This is valid for partial global symbols, but the variable's
585861ea
JK
17934 address will be bogus in the psymtab. Make it always at least
17935 non-zero to not look as a variable garbage collected by linker
17936 which have DW_OP_addr 0. */
21ae7a4d
JK
17937 if (i < size)
17938 dwarf2_complex_location_expr_complaint ();
585861ea 17939 stack[stacki]++;
21ae7a4d
JK
17940 break;
17941
17942 case DW_OP_GNU_uninit:
17943 break;
17944
3019eac3 17945 case DW_OP_GNU_addr_index:
49f6c839 17946 case DW_OP_GNU_const_index:
3019eac3
DE
17947 stack[++stacki] = read_addr_index_from_leb128 (cu, &data[i],
17948 &bytes_read);
17949 i += bytes_read;
17950 break;
17951
21ae7a4d
JK
17952 default:
17953 {
f39c6ffd 17954 const char *name = get_DW_OP_name (op);
21ae7a4d
JK
17955
17956 if (name)
17957 complaint (&symfile_complaints, _("unsupported stack op: '%s'"),
17958 name);
17959 else
17960 complaint (&symfile_complaints, _("unsupported stack op: '%02x'"),
17961 op);
17962 }
17963
17964 return (stack[stacki]);
d53d4ac5 17965 }
3c6e0cb3 17966
21ae7a4d
JK
17967 /* Enforce maximum stack depth of SIZE-1 to avoid writing
17968 outside of the allocated space. Also enforce minimum>0. */
17969 if (stacki >= ARRAY_SIZE (stack) - 1)
17970 {
17971 complaint (&symfile_complaints,
17972 _("location description stack overflow"));
17973 return 0;
17974 }
17975
17976 if (stacki <= 0)
17977 {
17978 complaint (&symfile_complaints,
17979 _("location description stack underflow"));
17980 return 0;
17981 }
17982 }
17983 return (stack[stacki]);
c906108c
SS
17984}
17985
17986/* memory allocation interface */
17987
c906108c 17988static struct dwarf_block *
7b5a2f43 17989dwarf_alloc_block (struct dwarf2_cu *cu)
c906108c
SS
17990{
17991 struct dwarf_block *blk;
17992
17993 blk = (struct dwarf_block *)
7b5a2f43 17994 obstack_alloc (&cu->comp_unit_obstack, sizeof (struct dwarf_block));
c906108c
SS
17995 return (blk);
17996}
17997
c906108c 17998static struct die_info *
b60c80d6 17999dwarf_alloc_die (struct dwarf2_cu *cu, int num_attrs)
c906108c
SS
18000{
18001 struct die_info *die;
b60c80d6
DJ
18002 size_t size = sizeof (struct die_info);
18003
18004 if (num_attrs > 1)
18005 size += (num_attrs - 1) * sizeof (struct attribute);
c906108c 18006
b60c80d6 18007 die = (struct die_info *) obstack_alloc (&cu->comp_unit_obstack, size);
c906108c
SS
18008 memset (die, 0, sizeof (struct die_info));
18009 return (die);
18010}
2e276125
JB
18011
18012\f
18013/* Macro support. */
18014
2e276125
JB
18015/* Return the full name of file number I in *LH's file name table.
18016 Use COMP_DIR as the name of the current directory of the
18017 compilation. The result is allocated using xmalloc; the caller is
18018 responsible for freeing it. */
18019static char *
18020file_full_name (int file, struct line_header *lh, const char *comp_dir)
18021{
6a83a1e6
EZ
18022 /* Is the file number a valid index into the line header's file name
18023 table? Remember that file numbers start with one, not zero. */
18024 if (1 <= file && file <= lh->num_file_names)
18025 {
18026 struct file_entry *fe = &lh->file_names[file - 1];
6e70227d 18027
6a83a1e6
EZ
18028 if (IS_ABSOLUTE_PATH (fe->name))
18029 return xstrdup (fe->name);
18030 else
18031 {
18032 const char *dir;
18033 int dir_len;
18034 char *full_name;
18035
18036 if (fe->dir_index)
18037 dir = lh->include_dirs[fe->dir_index - 1];
18038 else
18039 dir = comp_dir;
18040
18041 if (dir)
18042 {
18043 dir_len = strlen (dir);
18044 full_name = xmalloc (dir_len + 1 + strlen (fe->name) + 1);
18045 strcpy (full_name, dir);
18046 full_name[dir_len] = '/';
18047 strcpy (full_name + dir_len + 1, fe->name);
18048 return full_name;
18049 }
18050 else
18051 return xstrdup (fe->name);
18052 }
18053 }
2e276125
JB
18054 else
18055 {
6a83a1e6
EZ
18056 /* The compiler produced a bogus file number. We can at least
18057 record the macro definitions made in the file, even if we
18058 won't be able to find the file by name. */
18059 char fake_name[80];
9a619af0 18060
8c042590
PM
18061 xsnprintf (fake_name, sizeof (fake_name),
18062 "<bad macro file number %d>", file);
2e276125 18063
6e70227d 18064 complaint (&symfile_complaints,
6a83a1e6
EZ
18065 _("bad file number in macro information (%d)"),
18066 file);
2e276125 18067
6a83a1e6 18068 return xstrdup (fake_name);
2e276125
JB
18069 }
18070}
18071
18072
18073static struct macro_source_file *
18074macro_start_file (int file, int line,
18075 struct macro_source_file *current_file,
18076 const char *comp_dir,
18077 struct line_header *lh, struct objfile *objfile)
18078{
18079 /* The full name of this source file. */
18080 char *full_name = file_full_name (file, lh, comp_dir);
18081
18082 /* We don't create a macro table for this compilation unit
18083 at all until we actually get a filename. */
18084 if (! pending_macros)
6532ff36
TT
18085 pending_macros = new_macro_table (&objfile->per_bfd->storage_obstack,
18086 objfile->per_bfd->macro_cache);
2e276125
JB
18087
18088 if (! current_file)
abc9d0dc
TT
18089 {
18090 /* If we have no current file, then this must be the start_file
18091 directive for the compilation unit's main source file. */
18092 current_file = macro_set_main (pending_macros, full_name);
18093 macro_define_special (pending_macros);
18094 }
2e276125
JB
18095 else
18096 current_file = macro_include (current_file, line, full_name);
18097
18098 xfree (full_name);
6e70227d 18099
2e276125
JB
18100 return current_file;
18101}
18102
18103
18104/* Copy the LEN characters at BUF to a xmalloc'ed block of memory,
18105 followed by a null byte. */
18106static char *
18107copy_string (const char *buf, int len)
18108{
18109 char *s = xmalloc (len + 1);
9a619af0 18110
2e276125
JB
18111 memcpy (s, buf, len);
18112 s[len] = '\0';
2e276125
JB
18113 return s;
18114}
18115
18116
18117static const char *
18118consume_improper_spaces (const char *p, const char *body)
18119{
18120 if (*p == ' ')
18121 {
4d3c2250 18122 complaint (&symfile_complaints,
3e43a32a
MS
18123 _("macro definition contains spaces "
18124 "in formal argument list:\n`%s'"),
4d3c2250 18125 body);
2e276125
JB
18126
18127 while (*p == ' ')
18128 p++;
18129 }
18130
18131 return p;
18132}
18133
18134
18135static void
18136parse_macro_definition (struct macro_source_file *file, int line,
18137 const char *body)
18138{
18139 const char *p;
18140
18141 /* The body string takes one of two forms. For object-like macro
18142 definitions, it should be:
18143
18144 <macro name> " " <definition>
18145
18146 For function-like macro definitions, it should be:
18147
18148 <macro name> "() " <definition>
18149 or
18150 <macro name> "(" <arg name> ( "," <arg name> ) * ") " <definition>
18151
18152 Spaces may appear only where explicitly indicated, and in the
18153 <definition>.
18154
18155 The Dwarf 2 spec says that an object-like macro's name is always
18156 followed by a space, but versions of GCC around March 2002 omit
6e70227d 18157 the space when the macro's definition is the empty string.
2e276125
JB
18158
18159 The Dwarf 2 spec says that there should be no spaces between the
18160 formal arguments in a function-like macro's formal argument list,
18161 but versions of GCC around March 2002 include spaces after the
18162 commas. */
18163
18164
18165 /* Find the extent of the macro name. The macro name is terminated
18166 by either a space or null character (for an object-like macro) or
18167 an opening paren (for a function-like macro). */
18168 for (p = body; *p; p++)
18169 if (*p == ' ' || *p == '(')
18170 break;
18171
18172 if (*p == ' ' || *p == '\0')
18173 {
18174 /* It's an object-like macro. */
18175 int name_len = p - body;
18176 char *name = copy_string (body, name_len);
18177 const char *replacement;
18178
18179 if (*p == ' ')
18180 replacement = body + name_len + 1;
18181 else
18182 {
4d3c2250 18183 dwarf2_macro_malformed_definition_complaint (body);
2e276125
JB
18184 replacement = body + name_len;
18185 }
6e70227d 18186
2e276125
JB
18187 macro_define_object (file, line, name, replacement);
18188
18189 xfree (name);
18190 }
18191 else if (*p == '(')
18192 {
18193 /* It's a function-like macro. */
18194 char *name = copy_string (body, p - body);
18195 int argc = 0;
18196 int argv_size = 1;
18197 char **argv = xmalloc (argv_size * sizeof (*argv));
18198
18199 p++;
18200
18201 p = consume_improper_spaces (p, body);
18202
18203 /* Parse the formal argument list. */
18204 while (*p && *p != ')')
18205 {
18206 /* Find the extent of the current argument name. */
18207 const char *arg_start = p;
18208
18209 while (*p && *p != ',' && *p != ')' && *p != ' ')
18210 p++;
18211
18212 if (! *p || p == arg_start)
4d3c2250 18213 dwarf2_macro_malformed_definition_complaint (body);
2e276125
JB
18214 else
18215 {
18216 /* Make sure argv has room for the new argument. */
18217 if (argc >= argv_size)
18218 {
18219 argv_size *= 2;
18220 argv = xrealloc (argv, argv_size * sizeof (*argv));
18221 }
18222
18223 argv[argc++] = copy_string (arg_start, p - arg_start);
18224 }
18225
18226 p = consume_improper_spaces (p, body);
18227
18228 /* Consume the comma, if present. */
18229 if (*p == ',')
18230 {
18231 p++;
18232
18233 p = consume_improper_spaces (p, body);
18234 }
18235 }
18236
18237 if (*p == ')')
18238 {
18239 p++;
18240
18241 if (*p == ' ')
18242 /* Perfectly formed definition, no complaints. */
18243 macro_define_function (file, line, name,
6e70227d 18244 argc, (const char **) argv,
2e276125
JB
18245 p + 1);
18246 else if (*p == '\0')
18247 {
18248 /* Complain, but do define it. */
4d3c2250 18249 dwarf2_macro_malformed_definition_complaint (body);
2e276125 18250 macro_define_function (file, line, name,
6e70227d 18251 argc, (const char **) argv,
2e276125
JB
18252 p);
18253 }
18254 else
18255 /* Just complain. */
4d3c2250 18256 dwarf2_macro_malformed_definition_complaint (body);
2e276125
JB
18257 }
18258 else
18259 /* Just complain. */
4d3c2250 18260 dwarf2_macro_malformed_definition_complaint (body);
2e276125
JB
18261
18262 xfree (name);
18263 {
18264 int i;
18265
18266 for (i = 0; i < argc; i++)
18267 xfree (argv[i]);
18268 }
18269 xfree (argv);
18270 }
18271 else
4d3c2250 18272 dwarf2_macro_malformed_definition_complaint (body);
2e276125
JB
18273}
18274
cf2c3c16
TT
18275/* Skip some bytes from BYTES according to the form given in FORM.
18276 Returns the new pointer. */
2e276125 18277
cf2c3c16 18278static gdb_byte *
f664829e 18279skip_form_bytes (bfd *abfd, gdb_byte *bytes, gdb_byte *buffer_end,
cf2c3c16
TT
18280 enum dwarf_form form,
18281 unsigned int offset_size,
18282 struct dwarf2_section_info *section)
2e276125 18283{
cf2c3c16 18284 unsigned int bytes_read;
2e276125 18285
cf2c3c16 18286 switch (form)
2e276125 18287 {
cf2c3c16
TT
18288 case DW_FORM_data1:
18289 case DW_FORM_flag:
18290 ++bytes;
18291 break;
18292
18293 case DW_FORM_data2:
18294 bytes += 2;
18295 break;
18296
18297 case DW_FORM_data4:
18298 bytes += 4;
18299 break;
18300
18301 case DW_FORM_data8:
18302 bytes += 8;
18303 break;
18304
18305 case DW_FORM_string:
18306 read_direct_string (abfd, bytes, &bytes_read);
18307 bytes += bytes_read;
18308 break;
18309
18310 case DW_FORM_sec_offset:
18311 case DW_FORM_strp:
36586728 18312 case DW_FORM_GNU_strp_alt:
cf2c3c16
TT
18313 bytes += offset_size;
18314 break;
18315
18316 case DW_FORM_block:
18317 bytes += read_unsigned_leb128 (abfd, bytes, &bytes_read);
18318 bytes += bytes_read;
18319 break;
18320
18321 case DW_FORM_block1:
18322 bytes += 1 + read_1_byte (abfd, bytes);
18323 break;
18324 case DW_FORM_block2:
18325 bytes += 2 + read_2_bytes (abfd, bytes);
18326 break;
18327 case DW_FORM_block4:
18328 bytes += 4 + read_4_bytes (abfd, bytes);
18329 break;
18330
18331 case DW_FORM_sdata:
18332 case DW_FORM_udata:
3019eac3
DE
18333 case DW_FORM_GNU_addr_index:
18334 case DW_FORM_GNU_str_index:
f664829e
DE
18335 bytes = (gdb_byte *) gdb_skip_leb128 (bytes, buffer_end);
18336 if (bytes == NULL)
18337 {
18338 dwarf2_section_buffer_overflow_complaint (section);
18339 return NULL;
18340 }
cf2c3c16
TT
18341 break;
18342
18343 default:
18344 {
18345 complain:
18346 complaint (&symfile_complaints,
18347 _("invalid form 0x%x in `%s'"),
18348 form,
18349 section->asection->name);
18350 return NULL;
18351 }
2e276125
JB
18352 }
18353
cf2c3c16
TT
18354 return bytes;
18355}
757a13d0 18356
cf2c3c16
TT
18357/* A helper for dwarf_decode_macros that handles skipping an unknown
18358 opcode. Returns an updated pointer to the macro data buffer; or,
18359 on error, issues a complaint and returns NULL. */
757a13d0 18360
cf2c3c16
TT
18361static gdb_byte *
18362skip_unknown_opcode (unsigned int opcode,
18363 gdb_byte **opcode_definitions,
f664829e 18364 gdb_byte *mac_ptr, gdb_byte *mac_end,
cf2c3c16
TT
18365 bfd *abfd,
18366 unsigned int offset_size,
18367 struct dwarf2_section_info *section)
18368{
18369 unsigned int bytes_read, i;
18370 unsigned long arg;
18371 gdb_byte *defn;
2e276125 18372
cf2c3c16 18373 if (opcode_definitions[opcode] == NULL)
2e276125 18374 {
cf2c3c16
TT
18375 complaint (&symfile_complaints,
18376 _("unrecognized DW_MACFINO opcode 0x%x"),
18377 opcode);
18378 return NULL;
18379 }
2e276125 18380
cf2c3c16
TT
18381 defn = opcode_definitions[opcode];
18382 arg = read_unsigned_leb128 (abfd, defn, &bytes_read);
18383 defn += bytes_read;
2e276125 18384
cf2c3c16
TT
18385 for (i = 0; i < arg; ++i)
18386 {
f664829e
DE
18387 mac_ptr = skip_form_bytes (abfd, mac_ptr, mac_end, defn[i], offset_size,
18388 section);
cf2c3c16
TT
18389 if (mac_ptr == NULL)
18390 {
18391 /* skip_form_bytes already issued the complaint. */
18392 return NULL;
18393 }
18394 }
757a13d0 18395
cf2c3c16
TT
18396 return mac_ptr;
18397}
757a13d0 18398
cf2c3c16
TT
18399/* A helper function which parses the header of a macro section.
18400 If the macro section is the extended (for now called "GNU") type,
18401 then this updates *OFFSET_SIZE. Returns a pointer to just after
18402 the header, or issues a complaint and returns NULL on error. */
757a13d0 18403
cf2c3c16
TT
18404static gdb_byte *
18405dwarf_parse_macro_header (gdb_byte **opcode_definitions,
18406 bfd *abfd,
18407 gdb_byte *mac_ptr,
18408 unsigned int *offset_size,
18409 int section_is_gnu)
18410{
18411 memset (opcode_definitions, 0, 256 * sizeof (gdb_byte *));
757a13d0 18412
cf2c3c16
TT
18413 if (section_is_gnu)
18414 {
18415 unsigned int version, flags;
757a13d0 18416
cf2c3c16
TT
18417 version = read_2_bytes (abfd, mac_ptr);
18418 if (version != 4)
18419 {
18420 complaint (&symfile_complaints,
18421 _("unrecognized version `%d' in .debug_macro section"),
18422 version);
18423 return NULL;
18424 }
18425 mac_ptr += 2;
757a13d0 18426
cf2c3c16
TT
18427 flags = read_1_byte (abfd, mac_ptr);
18428 ++mac_ptr;
18429 *offset_size = (flags & 1) ? 8 : 4;
757a13d0 18430
cf2c3c16
TT
18431 if ((flags & 2) != 0)
18432 /* We don't need the line table offset. */
18433 mac_ptr += *offset_size;
757a13d0 18434
cf2c3c16
TT
18435 /* Vendor opcode descriptions. */
18436 if ((flags & 4) != 0)
18437 {
18438 unsigned int i, count;
757a13d0 18439
cf2c3c16
TT
18440 count = read_1_byte (abfd, mac_ptr);
18441 ++mac_ptr;
18442 for (i = 0; i < count; ++i)
18443 {
18444 unsigned int opcode, bytes_read;
18445 unsigned long arg;
18446
18447 opcode = read_1_byte (abfd, mac_ptr);
18448 ++mac_ptr;
18449 opcode_definitions[opcode] = mac_ptr;
18450 arg = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
18451 mac_ptr += bytes_read;
18452 mac_ptr += arg;
18453 }
757a13d0 18454 }
cf2c3c16 18455 }
757a13d0 18456
cf2c3c16
TT
18457 return mac_ptr;
18458}
757a13d0 18459
cf2c3c16 18460/* A helper for dwarf_decode_macros that handles the GNU extensions,
8fc3fc34 18461 including DW_MACRO_GNU_transparent_include. */
cf2c3c16
TT
18462
18463static void
18464dwarf_decode_macro_bytes (bfd *abfd, gdb_byte *mac_ptr, gdb_byte *mac_end,
18465 struct macro_source_file *current_file,
15d034d0 18466 struct line_header *lh, const char *comp_dir,
cf2c3c16 18467 struct dwarf2_section_info *section,
36586728 18468 int section_is_gnu, int section_is_dwz,
cf2c3c16 18469 unsigned int offset_size,
8fc3fc34
TT
18470 struct objfile *objfile,
18471 htab_t include_hash)
cf2c3c16
TT
18472{
18473 enum dwarf_macro_record_type macinfo_type;
18474 int at_commandline;
18475 gdb_byte *opcode_definitions[256];
757a13d0 18476
cf2c3c16
TT
18477 mac_ptr = dwarf_parse_macro_header (opcode_definitions, abfd, mac_ptr,
18478 &offset_size, section_is_gnu);
18479 if (mac_ptr == NULL)
18480 {
18481 /* We already issued a complaint. */
18482 return;
18483 }
757a13d0
JK
18484
18485 /* Determines if GDB is still before first DW_MACINFO_start_file. If true
18486 GDB is still reading the definitions from command line. First
18487 DW_MACINFO_start_file will need to be ignored as it was already executed
18488 to create CURRENT_FILE for the main source holding also the command line
18489 definitions. On first met DW_MACINFO_start_file this flag is reset to
18490 normally execute all the remaining DW_MACINFO_start_file macinfos. */
18491
18492 at_commandline = 1;
18493
18494 do
18495 {
18496 /* Do we at least have room for a macinfo type byte? */
18497 if (mac_ptr >= mac_end)
18498 {
f664829e 18499 dwarf2_section_buffer_overflow_complaint (section);
757a13d0
JK
18500 break;
18501 }
18502
18503 macinfo_type = read_1_byte (abfd, mac_ptr);
18504 mac_ptr++;
18505
cf2c3c16
TT
18506 /* Note that we rely on the fact that the corresponding GNU and
18507 DWARF constants are the same. */
757a13d0
JK
18508 switch (macinfo_type)
18509 {
18510 /* A zero macinfo type indicates the end of the macro
18511 information. */
18512 case 0:
18513 break;
2e276125 18514
cf2c3c16
TT
18515 case DW_MACRO_GNU_define:
18516 case DW_MACRO_GNU_undef:
18517 case DW_MACRO_GNU_define_indirect:
18518 case DW_MACRO_GNU_undef_indirect:
36586728
TT
18519 case DW_MACRO_GNU_define_indirect_alt:
18520 case DW_MACRO_GNU_undef_indirect_alt:
2e276125 18521 {
891d2f0b 18522 unsigned int bytes_read;
2e276125
JB
18523 int line;
18524 char *body;
cf2c3c16 18525 int is_define;
2e276125 18526
cf2c3c16
TT
18527 line = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
18528 mac_ptr += bytes_read;
18529
18530 if (macinfo_type == DW_MACRO_GNU_define
18531 || macinfo_type == DW_MACRO_GNU_undef)
18532 {
18533 body = read_direct_string (abfd, mac_ptr, &bytes_read);
18534 mac_ptr += bytes_read;
18535 }
18536 else
18537 {
18538 LONGEST str_offset;
18539
18540 str_offset = read_offset_1 (abfd, mac_ptr, offset_size);
18541 mac_ptr += offset_size;
2e276125 18542
36586728 18543 if (macinfo_type == DW_MACRO_GNU_define_indirect_alt
f7a35f02
TT
18544 || macinfo_type == DW_MACRO_GNU_undef_indirect_alt
18545 || section_is_dwz)
36586728
TT
18546 {
18547 struct dwz_file *dwz = dwarf2_get_dwz_file ();
18548
18549 body = read_indirect_string_from_dwz (dwz, str_offset);
18550 }
18551 else
18552 body = read_indirect_string_at_offset (abfd, str_offset);
cf2c3c16
TT
18553 }
18554
18555 is_define = (macinfo_type == DW_MACRO_GNU_define
36586728
TT
18556 || macinfo_type == DW_MACRO_GNU_define_indirect
18557 || macinfo_type == DW_MACRO_GNU_define_indirect_alt);
2e276125 18558 if (! current_file)
757a13d0
JK
18559 {
18560 /* DWARF violation as no main source is present. */
18561 complaint (&symfile_complaints,
18562 _("debug info with no main source gives macro %s "
18563 "on line %d: %s"),
cf2c3c16
TT
18564 is_define ? _("definition") : _("undefinition"),
18565 line, body);
757a13d0
JK
18566 break;
18567 }
3e43a32a
MS
18568 if ((line == 0 && !at_commandline)
18569 || (line != 0 && at_commandline))
4d3c2250 18570 complaint (&symfile_complaints,
757a13d0
JK
18571 _("debug info gives %s macro %s with %s line %d: %s"),
18572 at_commandline ? _("command-line") : _("in-file"),
cf2c3c16 18573 is_define ? _("definition") : _("undefinition"),
757a13d0
JK
18574 line == 0 ? _("zero") : _("non-zero"), line, body);
18575
cf2c3c16 18576 if (is_define)
757a13d0 18577 parse_macro_definition (current_file, line, body);
cf2c3c16
TT
18578 else
18579 {
18580 gdb_assert (macinfo_type == DW_MACRO_GNU_undef
36586728
TT
18581 || macinfo_type == DW_MACRO_GNU_undef_indirect
18582 || macinfo_type == DW_MACRO_GNU_undef_indirect_alt);
cf2c3c16
TT
18583 macro_undef (current_file, line, body);
18584 }
2e276125
JB
18585 }
18586 break;
18587
cf2c3c16 18588 case DW_MACRO_GNU_start_file:
2e276125 18589 {
891d2f0b 18590 unsigned int bytes_read;
2e276125
JB
18591 int line, file;
18592
18593 line = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
18594 mac_ptr += bytes_read;
18595 file = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
18596 mac_ptr += bytes_read;
18597
3e43a32a
MS
18598 if ((line == 0 && !at_commandline)
18599 || (line != 0 && at_commandline))
757a13d0
JK
18600 complaint (&symfile_complaints,
18601 _("debug info gives source %d included "
18602 "from %s at %s line %d"),
18603 file, at_commandline ? _("command-line") : _("file"),
18604 line == 0 ? _("zero") : _("non-zero"), line);
18605
18606 if (at_commandline)
18607 {
cf2c3c16
TT
18608 /* This DW_MACRO_GNU_start_file was executed in the
18609 pass one. */
757a13d0
JK
18610 at_commandline = 0;
18611 }
18612 else
18613 current_file = macro_start_file (file, line,
18614 current_file, comp_dir,
cf2c3c16 18615 lh, objfile);
2e276125
JB
18616 }
18617 break;
18618
cf2c3c16 18619 case DW_MACRO_GNU_end_file:
2e276125 18620 if (! current_file)
4d3c2250 18621 complaint (&symfile_complaints,
3e43a32a
MS
18622 _("macro debug info has an unmatched "
18623 "`close_file' directive"));
2e276125
JB
18624 else
18625 {
18626 current_file = current_file->included_by;
18627 if (! current_file)
18628 {
cf2c3c16 18629 enum dwarf_macro_record_type next_type;
2e276125
JB
18630
18631 /* GCC circa March 2002 doesn't produce the zero
18632 type byte marking the end of the compilation
18633 unit. Complain if it's not there, but exit no
18634 matter what. */
18635
18636 /* Do we at least have room for a macinfo type byte? */
18637 if (mac_ptr >= mac_end)
18638 {
f664829e 18639 dwarf2_section_buffer_overflow_complaint (section);
2e276125
JB
18640 return;
18641 }
18642
18643 /* We don't increment mac_ptr here, so this is just
18644 a look-ahead. */
18645 next_type = read_1_byte (abfd, mac_ptr);
18646 if (next_type != 0)
4d3c2250 18647 complaint (&symfile_complaints,
3e43a32a
MS
18648 _("no terminating 0-type entry for "
18649 "macros in `.debug_macinfo' section"));
2e276125
JB
18650
18651 return;
18652 }
18653 }
18654 break;
18655
cf2c3c16 18656 case DW_MACRO_GNU_transparent_include:
36586728 18657 case DW_MACRO_GNU_transparent_include_alt:
cf2c3c16
TT
18658 {
18659 LONGEST offset;
8fc3fc34 18660 void **slot;
a036ba48
TT
18661 bfd *include_bfd = abfd;
18662 struct dwarf2_section_info *include_section = section;
18663 struct dwarf2_section_info alt_section;
18664 gdb_byte *include_mac_end = mac_end;
18665 int is_dwz = section_is_dwz;
18666 gdb_byte *new_mac_ptr;
cf2c3c16
TT
18667
18668 offset = read_offset_1 (abfd, mac_ptr, offset_size);
18669 mac_ptr += offset_size;
18670
a036ba48
TT
18671 if (macinfo_type == DW_MACRO_GNU_transparent_include_alt)
18672 {
18673 struct dwz_file *dwz = dwarf2_get_dwz_file ();
18674
18675 dwarf2_read_section (dwarf2_per_objfile->objfile,
18676 &dwz->macro);
18677
18678 include_bfd = dwz->macro.asection->owner;
18679 include_section = &dwz->macro;
18680 include_mac_end = dwz->macro.buffer + dwz->macro.size;
18681 is_dwz = 1;
18682 }
18683
18684 new_mac_ptr = include_section->buffer + offset;
18685 slot = htab_find_slot (include_hash, new_mac_ptr, INSERT);
18686
8fc3fc34
TT
18687 if (*slot != NULL)
18688 {
18689 /* This has actually happened; see
18690 http://sourceware.org/bugzilla/show_bug.cgi?id=13568. */
18691 complaint (&symfile_complaints,
18692 _("recursive DW_MACRO_GNU_transparent_include in "
18693 ".debug_macro section"));
18694 }
18695 else
18696 {
a036ba48 18697 *slot = new_mac_ptr;
36586728 18698
a036ba48 18699 dwarf_decode_macro_bytes (include_bfd, new_mac_ptr,
36586728 18700 include_mac_end, current_file,
8fc3fc34 18701 lh, comp_dir,
36586728 18702 section, section_is_gnu, is_dwz,
8fc3fc34
TT
18703 offset_size, objfile, include_hash);
18704
a036ba48 18705 htab_remove_elt (include_hash, new_mac_ptr);
8fc3fc34 18706 }
cf2c3c16
TT
18707 }
18708 break;
18709
2e276125 18710 case DW_MACINFO_vendor_ext:
cf2c3c16
TT
18711 if (!section_is_gnu)
18712 {
18713 unsigned int bytes_read;
18714 int constant;
2e276125 18715
cf2c3c16
TT
18716 constant = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
18717 mac_ptr += bytes_read;
18718 read_direct_string (abfd, mac_ptr, &bytes_read);
18719 mac_ptr += bytes_read;
2e276125 18720
cf2c3c16
TT
18721 /* We don't recognize any vendor extensions. */
18722 break;
18723 }
18724 /* FALLTHROUGH */
18725
18726 default:
18727 mac_ptr = skip_unknown_opcode (macinfo_type, opcode_definitions,
f664829e 18728 mac_ptr, mac_end, abfd, offset_size,
cf2c3c16
TT
18729 section);
18730 if (mac_ptr == NULL)
18731 return;
18732 break;
2e276125 18733 }
757a13d0 18734 } while (macinfo_type != 0);
2e276125 18735}
8e19ed76 18736
cf2c3c16 18737static void
09262596 18738dwarf_decode_macros (struct dwarf2_cu *cu, unsigned int offset,
15d034d0 18739 const char *comp_dir, int section_is_gnu)
cf2c3c16 18740{
bb5ed363 18741 struct objfile *objfile = dwarf2_per_objfile->objfile;
09262596
DE
18742 struct line_header *lh = cu->line_header;
18743 bfd *abfd;
cf2c3c16
TT
18744 gdb_byte *mac_ptr, *mac_end;
18745 struct macro_source_file *current_file = 0;
18746 enum dwarf_macro_record_type macinfo_type;
18747 unsigned int offset_size = cu->header.offset_size;
18748 gdb_byte *opcode_definitions[256];
8fc3fc34
TT
18749 struct cleanup *cleanup;
18750 htab_t include_hash;
18751 void **slot;
09262596
DE
18752 struct dwarf2_section_info *section;
18753 const char *section_name;
18754
18755 if (cu->dwo_unit != NULL)
18756 {
18757 if (section_is_gnu)
18758 {
18759 section = &cu->dwo_unit->dwo_file->sections.macro;
18760 section_name = ".debug_macro.dwo";
18761 }
18762 else
18763 {
18764 section = &cu->dwo_unit->dwo_file->sections.macinfo;
18765 section_name = ".debug_macinfo.dwo";
18766 }
18767 }
18768 else
18769 {
18770 if (section_is_gnu)
18771 {
18772 section = &dwarf2_per_objfile->macro;
18773 section_name = ".debug_macro";
18774 }
18775 else
18776 {
18777 section = &dwarf2_per_objfile->macinfo;
18778 section_name = ".debug_macinfo";
18779 }
18780 }
cf2c3c16 18781
bb5ed363 18782 dwarf2_read_section (objfile, section);
cf2c3c16
TT
18783 if (section->buffer == NULL)
18784 {
fceca515 18785 complaint (&symfile_complaints, _("missing %s section"), section_name);
cf2c3c16
TT
18786 return;
18787 }
09262596 18788 abfd = section->asection->owner;
cf2c3c16
TT
18789
18790 /* First pass: Find the name of the base filename.
18791 This filename is needed in order to process all macros whose definition
18792 (or undefinition) comes from the command line. These macros are defined
18793 before the first DW_MACINFO_start_file entry, and yet still need to be
18794 associated to the base file.
18795
18796 To determine the base file name, we scan the macro definitions until we
18797 reach the first DW_MACINFO_start_file entry. We then initialize
18798 CURRENT_FILE accordingly so that any macro definition found before the
18799 first DW_MACINFO_start_file can still be associated to the base file. */
18800
18801 mac_ptr = section->buffer + offset;
18802 mac_end = section->buffer + section->size;
18803
18804 mac_ptr = dwarf_parse_macro_header (opcode_definitions, abfd, mac_ptr,
18805 &offset_size, section_is_gnu);
18806 if (mac_ptr == NULL)
18807 {
18808 /* We already issued a complaint. */
18809 return;
18810 }
18811
18812 do
18813 {
18814 /* Do we at least have room for a macinfo type byte? */
18815 if (mac_ptr >= mac_end)
18816 {
18817 /* Complaint is printed during the second pass as GDB will probably
18818 stop the first pass earlier upon finding
18819 DW_MACINFO_start_file. */
18820 break;
18821 }
18822
18823 macinfo_type = read_1_byte (abfd, mac_ptr);
18824 mac_ptr++;
18825
18826 /* Note that we rely on the fact that the corresponding GNU and
18827 DWARF constants are the same. */
18828 switch (macinfo_type)
18829 {
18830 /* A zero macinfo type indicates the end of the macro
18831 information. */
18832 case 0:
18833 break;
18834
18835 case DW_MACRO_GNU_define:
18836 case DW_MACRO_GNU_undef:
18837 /* Only skip the data by MAC_PTR. */
18838 {
18839 unsigned int bytes_read;
18840
18841 read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
18842 mac_ptr += bytes_read;
18843 read_direct_string (abfd, mac_ptr, &bytes_read);
18844 mac_ptr += bytes_read;
18845 }
18846 break;
18847
18848 case DW_MACRO_GNU_start_file:
18849 {
18850 unsigned int bytes_read;
18851 int line, file;
18852
18853 line = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
18854 mac_ptr += bytes_read;
18855 file = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
18856 mac_ptr += bytes_read;
18857
18858 current_file = macro_start_file (file, line, current_file,
bb5ed363 18859 comp_dir, lh, objfile);
cf2c3c16
TT
18860 }
18861 break;
18862
18863 case DW_MACRO_GNU_end_file:
18864 /* No data to skip by MAC_PTR. */
18865 break;
18866
18867 case DW_MACRO_GNU_define_indirect:
18868 case DW_MACRO_GNU_undef_indirect:
f7a35f02
TT
18869 case DW_MACRO_GNU_define_indirect_alt:
18870 case DW_MACRO_GNU_undef_indirect_alt:
cf2c3c16
TT
18871 {
18872 unsigned int bytes_read;
18873
18874 read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
18875 mac_ptr += bytes_read;
18876 mac_ptr += offset_size;
18877 }
18878 break;
18879
18880 case DW_MACRO_GNU_transparent_include:
f7a35f02 18881 case DW_MACRO_GNU_transparent_include_alt:
cf2c3c16
TT
18882 /* Note that, according to the spec, a transparent include
18883 chain cannot call DW_MACRO_GNU_start_file. So, we can just
18884 skip this opcode. */
18885 mac_ptr += offset_size;
18886 break;
18887
18888 case DW_MACINFO_vendor_ext:
18889 /* Only skip the data by MAC_PTR. */
18890 if (!section_is_gnu)
18891 {
18892 unsigned int bytes_read;
18893
18894 read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
18895 mac_ptr += bytes_read;
18896 read_direct_string (abfd, mac_ptr, &bytes_read);
18897 mac_ptr += bytes_read;
18898 }
18899 /* FALLTHROUGH */
18900
18901 default:
18902 mac_ptr = skip_unknown_opcode (macinfo_type, opcode_definitions,
f664829e 18903 mac_ptr, mac_end, abfd, offset_size,
cf2c3c16
TT
18904 section);
18905 if (mac_ptr == NULL)
18906 return;
18907 break;
18908 }
18909 } while (macinfo_type != 0 && current_file == NULL);
18910
18911 /* Second pass: Process all entries.
18912
18913 Use the AT_COMMAND_LINE flag to determine whether we are still processing
18914 command-line macro definitions/undefinitions. This flag is unset when we
18915 reach the first DW_MACINFO_start_file entry. */
18916
8fc3fc34
TT
18917 include_hash = htab_create_alloc (1, htab_hash_pointer, htab_eq_pointer,
18918 NULL, xcalloc, xfree);
18919 cleanup = make_cleanup_htab_delete (include_hash);
18920 mac_ptr = section->buffer + offset;
18921 slot = htab_find_slot (include_hash, mac_ptr, INSERT);
18922 *slot = mac_ptr;
18923 dwarf_decode_macro_bytes (abfd, mac_ptr, mac_end,
36586728
TT
18924 current_file, lh, comp_dir, section,
18925 section_is_gnu, 0,
8fc3fc34
TT
18926 offset_size, objfile, include_hash);
18927 do_cleanups (cleanup);
cf2c3c16
TT
18928}
18929
8e19ed76 18930/* Check if the attribute's form is a DW_FORM_block*
0963b4bd 18931 if so return true else false. */
380bca97 18932
8e19ed76
PS
18933static int
18934attr_form_is_block (struct attribute *attr)
18935{
18936 return (attr == NULL ? 0 :
18937 attr->form == DW_FORM_block1
18938 || attr->form == DW_FORM_block2
18939 || attr->form == DW_FORM_block4
2dc7f7b3
TT
18940 || attr->form == DW_FORM_block
18941 || attr->form == DW_FORM_exprloc);
8e19ed76 18942}
4c2df51b 18943
c6a0999f
JB
18944/* Return non-zero if ATTR's value is a section offset --- classes
18945 lineptr, loclistptr, macptr or rangelistptr --- or zero, otherwise.
18946 You may use DW_UNSND (attr) to retrieve such offsets.
18947
18948 Section 7.5.4, "Attribute Encodings", explains that no attribute
18949 may have a value that belongs to more than one of these classes; it
18950 would be ambiguous if we did, because we use the same forms for all
18951 of them. */
380bca97 18952
3690dd37
JB
18953static int
18954attr_form_is_section_offset (struct attribute *attr)
18955{
18956 return (attr->form == DW_FORM_data4
2dc7f7b3
TT
18957 || attr->form == DW_FORM_data8
18958 || attr->form == DW_FORM_sec_offset);
3690dd37
JB
18959}
18960
3690dd37
JB
18961/* Return non-zero if ATTR's value falls in the 'constant' class, or
18962 zero otherwise. When this function returns true, you can apply
18963 dwarf2_get_attr_constant_value to it.
18964
18965 However, note that for some attributes you must check
18966 attr_form_is_section_offset before using this test. DW_FORM_data4
18967 and DW_FORM_data8 are members of both the constant class, and of
18968 the classes that contain offsets into other debug sections
18969 (lineptr, loclistptr, macptr or rangelistptr). The DWARF spec says
18970 that, if an attribute's can be either a constant or one of the
18971 section offset classes, DW_FORM_data4 and DW_FORM_data8 should be
18972 taken as section offsets, not constants. */
380bca97 18973
3690dd37
JB
18974static int
18975attr_form_is_constant (struct attribute *attr)
18976{
18977 switch (attr->form)
18978 {
18979 case DW_FORM_sdata:
18980 case DW_FORM_udata:
18981 case DW_FORM_data1:
18982 case DW_FORM_data2:
18983 case DW_FORM_data4:
18984 case DW_FORM_data8:
18985 return 1;
18986 default:
18987 return 0;
18988 }
18989}
18990
3019eac3
DE
18991/* Return the .debug_loc section to use for CU.
18992 For DWO files use .debug_loc.dwo. */
18993
18994static struct dwarf2_section_info *
18995cu_debug_loc_section (struct dwarf2_cu *cu)
18996{
18997 if (cu->dwo_unit)
18998 return &cu->dwo_unit->dwo_file->sections.loc;
18999 return &dwarf2_per_objfile->loc;
19000}
19001
8cf6f0b1
TT
19002/* A helper function that fills in a dwarf2_loclist_baton. */
19003
19004static void
19005fill_in_loclist_baton (struct dwarf2_cu *cu,
19006 struct dwarf2_loclist_baton *baton,
19007 struct attribute *attr)
19008{
3019eac3
DE
19009 struct dwarf2_section_info *section = cu_debug_loc_section (cu);
19010
19011 dwarf2_read_section (dwarf2_per_objfile->objfile, section);
8cf6f0b1
TT
19012
19013 baton->per_cu = cu->per_cu;
19014 gdb_assert (baton->per_cu);
19015 /* We don't know how long the location list is, but make sure we
19016 don't run off the edge of the section. */
3019eac3
DE
19017 baton->size = section->size - DW_UNSND (attr);
19018 baton->data = section->buffer + DW_UNSND (attr);
8cf6f0b1 19019 baton->base_address = cu->base_address;
f664829e 19020 baton->from_dwo = cu->dwo_unit != NULL;
8cf6f0b1
TT
19021}
19022
4c2df51b
DJ
19023static void
19024dwarf2_symbol_mark_computed (struct attribute *attr, struct symbol *sym,
e7c27a73 19025 struct dwarf2_cu *cu)
4c2df51b 19026{
bb5ed363 19027 struct objfile *objfile = dwarf2_per_objfile->objfile;
3019eac3 19028 struct dwarf2_section_info *section = cu_debug_loc_section (cu);
bb5ed363 19029
3690dd37 19030 if (attr_form_is_section_offset (attr)
3019eac3 19031 /* .debug_loc{,.dwo} may not exist at all, or the offset may be outside
99bcc461
DJ
19032 the section. If so, fall through to the complaint in the
19033 other branch. */
3019eac3 19034 && DW_UNSND (attr) < dwarf2_section_size (objfile, section))
4c2df51b 19035 {
0d53c4c4 19036 struct dwarf2_loclist_baton *baton;
4c2df51b 19037
bb5ed363 19038 baton = obstack_alloc (&objfile->objfile_obstack,
0d53c4c4 19039 sizeof (struct dwarf2_loclist_baton));
4c2df51b 19040
8cf6f0b1 19041 fill_in_loclist_baton (cu, baton, attr);
be391dca 19042
d00adf39 19043 if (cu->base_known == 0)
0d53c4c4 19044 complaint (&symfile_complaints,
3e43a32a
MS
19045 _("Location list used without "
19046 "specifying the CU base address."));
4c2df51b 19047
768a979c 19048 SYMBOL_COMPUTED_OPS (sym) = &dwarf2_loclist_funcs;
0d53c4c4
DJ
19049 SYMBOL_LOCATION_BATON (sym) = baton;
19050 }
19051 else
19052 {
19053 struct dwarf2_locexpr_baton *baton;
19054
bb5ed363 19055 baton = obstack_alloc (&objfile->objfile_obstack,
0d53c4c4 19056 sizeof (struct dwarf2_locexpr_baton));
ae0d2f24
UW
19057 baton->per_cu = cu->per_cu;
19058 gdb_assert (baton->per_cu);
0d53c4c4
DJ
19059
19060 if (attr_form_is_block (attr))
19061 {
19062 /* Note that we're just copying the block's data pointer
19063 here, not the actual data. We're still pointing into the
6502dd73
DJ
19064 info_buffer for SYM's objfile; right now we never release
19065 that buffer, but when we do clean up properly this may
19066 need to change. */
0d53c4c4
DJ
19067 baton->size = DW_BLOCK (attr)->size;
19068 baton->data = DW_BLOCK (attr)->data;
19069 }
19070 else
19071 {
19072 dwarf2_invalid_attrib_class_complaint ("location description",
19073 SYMBOL_NATURAL_NAME (sym));
19074 baton->size = 0;
0d53c4c4 19075 }
6e70227d 19076
768a979c 19077 SYMBOL_COMPUTED_OPS (sym) = &dwarf2_locexpr_funcs;
0d53c4c4
DJ
19078 SYMBOL_LOCATION_BATON (sym) = baton;
19079 }
4c2df51b 19080}
6502dd73 19081
9aa1f1e3
TT
19082/* Return the OBJFILE associated with the compilation unit CU. If CU
19083 came from a separate debuginfo file, then the master objfile is
19084 returned. */
ae0d2f24
UW
19085
19086struct objfile *
19087dwarf2_per_cu_objfile (struct dwarf2_per_cu_data *per_cu)
19088{
9291a0cd 19089 struct objfile *objfile = per_cu->objfile;
ae0d2f24
UW
19090
19091 /* Return the master objfile, so that we can report and look up the
19092 correct file containing this variable. */
19093 if (objfile->separate_debug_objfile_backlink)
19094 objfile = objfile->separate_debug_objfile_backlink;
19095
19096 return objfile;
19097}
19098
96408a79
SA
19099/* Return comp_unit_head for PER_CU, either already available in PER_CU->CU
19100 (CU_HEADERP is unused in such case) or prepare a temporary copy at
19101 CU_HEADERP first. */
19102
19103static const struct comp_unit_head *
19104per_cu_header_read_in (struct comp_unit_head *cu_headerp,
19105 struct dwarf2_per_cu_data *per_cu)
19106{
96408a79
SA
19107 gdb_byte *info_ptr;
19108
19109 if (per_cu->cu)
19110 return &per_cu->cu->header;
19111
0bc3a05c 19112 info_ptr = per_cu->info_or_types_section->buffer + per_cu->offset.sect_off;
96408a79
SA
19113
19114 memset (cu_headerp, 0, sizeof (*cu_headerp));
0bc3a05c 19115 read_comp_unit_head (cu_headerp, info_ptr, per_cu->objfile->obfd);
96408a79
SA
19116
19117 return cu_headerp;
19118}
19119
ae0d2f24
UW
19120/* Return the address size given in the compilation unit header for CU. */
19121
98714339 19122int
ae0d2f24
UW
19123dwarf2_per_cu_addr_size (struct dwarf2_per_cu_data *per_cu)
19124{
96408a79
SA
19125 struct comp_unit_head cu_header_local;
19126 const struct comp_unit_head *cu_headerp;
c471e790 19127
96408a79
SA
19128 cu_headerp = per_cu_header_read_in (&cu_header_local, per_cu);
19129
19130 return cu_headerp->addr_size;
ae0d2f24
UW
19131}
19132
9eae7c52
TT
19133/* Return the offset size given in the compilation unit header for CU. */
19134
19135int
19136dwarf2_per_cu_offset_size (struct dwarf2_per_cu_data *per_cu)
19137{
96408a79
SA
19138 struct comp_unit_head cu_header_local;
19139 const struct comp_unit_head *cu_headerp;
9c6c53f7 19140
96408a79
SA
19141 cu_headerp = per_cu_header_read_in (&cu_header_local, per_cu);
19142
19143 return cu_headerp->offset_size;
19144}
19145
19146/* See its dwarf2loc.h declaration. */
19147
19148int
19149dwarf2_per_cu_ref_addr_size (struct dwarf2_per_cu_data *per_cu)
19150{
19151 struct comp_unit_head cu_header_local;
19152 const struct comp_unit_head *cu_headerp;
19153
19154 cu_headerp = per_cu_header_read_in (&cu_header_local, per_cu);
19155
19156 if (cu_headerp->version == 2)
19157 return cu_headerp->addr_size;
19158 else
19159 return cu_headerp->offset_size;
181cebd4
JK
19160}
19161
9aa1f1e3
TT
19162/* Return the text offset of the CU. The returned offset comes from
19163 this CU's objfile. If this objfile came from a separate debuginfo
19164 file, then the offset may be different from the corresponding
19165 offset in the parent objfile. */
19166
19167CORE_ADDR
19168dwarf2_per_cu_text_offset (struct dwarf2_per_cu_data *per_cu)
19169{
bb3fa9d0 19170 struct objfile *objfile = per_cu->objfile;
9aa1f1e3
TT
19171
19172 return ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
19173}
19174
348e048f
DE
19175/* Locate the .debug_info compilation unit from CU's objfile which contains
19176 the DIE at OFFSET. Raises an error on failure. */
ae038cb0
DJ
19177
19178static struct dwarf2_per_cu_data *
b64f50a1 19179dwarf2_find_containing_comp_unit (sect_offset offset,
36586728 19180 unsigned int offset_in_dwz,
ae038cb0
DJ
19181 struct objfile *objfile)
19182{
19183 struct dwarf2_per_cu_data *this_cu;
19184 int low, high;
36586728 19185 const sect_offset *cu_off;
ae038cb0 19186
ae038cb0
DJ
19187 low = 0;
19188 high = dwarf2_per_objfile->n_comp_units - 1;
19189 while (high > low)
19190 {
36586728 19191 struct dwarf2_per_cu_data *mid_cu;
ae038cb0 19192 int mid = low + (high - low) / 2;
9a619af0 19193
36586728
TT
19194 mid_cu = dwarf2_per_objfile->all_comp_units[mid];
19195 cu_off = &mid_cu->offset;
19196 if (mid_cu->is_dwz > offset_in_dwz
19197 || (mid_cu->is_dwz == offset_in_dwz
19198 && cu_off->sect_off >= offset.sect_off))
ae038cb0
DJ
19199 high = mid;
19200 else
19201 low = mid + 1;
19202 }
19203 gdb_assert (low == high);
36586728
TT
19204 this_cu = dwarf2_per_objfile->all_comp_units[low];
19205 cu_off = &this_cu->offset;
19206 if (this_cu->is_dwz != offset_in_dwz || cu_off->sect_off > offset.sect_off)
ae038cb0 19207 {
36586728 19208 if (low == 0 || this_cu->is_dwz != offset_in_dwz)
8a3fe4f8
AC
19209 error (_("Dwarf Error: could not find partial DIE containing "
19210 "offset 0x%lx [in module %s]"),
b64f50a1 19211 (long) offset.sect_off, bfd_get_filename (objfile->obfd));
10b3939b 19212
b64f50a1
JK
19213 gdb_assert (dwarf2_per_objfile->all_comp_units[low-1]->offset.sect_off
19214 <= offset.sect_off);
ae038cb0
DJ
19215 return dwarf2_per_objfile->all_comp_units[low-1];
19216 }
19217 else
19218 {
19219 this_cu = dwarf2_per_objfile->all_comp_units[low];
19220 if (low == dwarf2_per_objfile->n_comp_units - 1
b64f50a1
JK
19221 && offset.sect_off >= this_cu->offset.sect_off + this_cu->length)
19222 error (_("invalid dwarf2 offset %u"), offset.sect_off);
19223 gdb_assert (offset.sect_off < this_cu->offset.sect_off + this_cu->length);
ae038cb0
DJ
19224 return this_cu;
19225 }
19226}
19227
23745b47 19228/* Initialize dwarf2_cu CU, owned by PER_CU. */
93311388 19229
9816fde3 19230static void
23745b47 19231init_one_comp_unit (struct dwarf2_cu *cu, struct dwarf2_per_cu_data *per_cu)
93311388 19232{
9816fde3 19233 memset (cu, 0, sizeof (*cu));
23745b47
DE
19234 per_cu->cu = cu;
19235 cu->per_cu = per_cu;
19236 cu->objfile = per_cu->objfile;
93311388 19237 obstack_init (&cu->comp_unit_obstack);
9816fde3
JK
19238}
19239
19240/* Initialize basic fields of dwarf_cu CU according to DIE COMP_UNIT_DIE. */
19241
19242static void
95554aad
TT
19243prepare_one_comp_unit (struct dwarf2_cu *cu, struct die_info *comp_unit_die,
19244 enum language pretend_language)
9816fde3
JK
19245{
19246 struct attribute *attr;
19247
19248 /* Set the language we're debugging. */
19249 attr = dwarf2_attr (comp_unit_die, DW_AT_language, cu);
19250 if (attr)
19251 set_cu_language (DW_UNSND (attr), cu);
19252 else
9cded63f 19253 {
95554aad 19254 cu->language = pretend_language;
9cded63f
TT
19255 cu->language_defn = language_def (cu->language);
19256 }
dee91e82
DE
19257
19258 attr = dwarf2_attr (comp_unit_die, DW_AT_producer, cu);
19259 if (attr)
19260 cu->producer = DW_STRING (attr);
93311388
DE
19261}
19262
ae038cb0
DJ
19263/* Release one cached compilation unit, CU. We unlink it from the tree
19264 of compilation units, but we don't remove it from the read_in_chain;
93311388
DE
19265 the caller is responsible for that.
19266 NOTE: DATA is a void * because this function is also used as a
19267 cleanup routine. */
ae038cb0
DJ
19268
19269static void
68dc6402 19270free_heap_comp_unit (void *data)
ae038cb0
DJ
19271{
19272 struct dwarf2_cu *cu = data;
19273
23745b47
DE
19274 gdb_assert (cu->per_cu != NULL);
19275 cu->per_cu->cu = NULL;
ae038cb0
DJ
19276 cu->per_cu = NULL;
19277
19278 obstack_free (&cu->comp_unit_obstack, NULL);
19279
19280 xfree (cu);
19281}
19282
72bf9492 19283/* This cleanup function is passed the address of a dwarf2_cu on the stack
ae038cb0 19284 when we're finished with it. We can't free the pointer itself, but be
dee91e82 19285 sure to unlink it from the cache. Also release any associated storage. */
72bf9492
DJ
19286
19287static void
19288free_stack_comp_unit (void *data)
19289{
19290 struct dwarf2_cu *cu = data;
19291
23745b47
DE
19292 gdb_assert (cu->per_cu != NULL);
19293 cu->per_cu->cu = NULL;
19294 cu->per_cu = NULL;
19295
72bf9492
DJ
19296 obstack_free (&cu->comp_unit_obstack, NULL);
19297 cu->partial_dies = NULL;
ae038cb0
DJ
19298}
19299
19300/* Free all cached compilation units. */
19301
19302static void
19303free_cached_comp_units (void *data)
19304{
19305 struct dwarf2_per_cu_data *per_cu, **last_chain;
19306
19307 per_cu = dwarf2_per_objfile->read_in_chain;
19308 last_chain = &dwarf2_per_objfile->read_in_chain;
19309 while (per_cu != NULL)
19310 {
19311 struct dwarf2_per_cu_data *next_cu;
19312
19313 next_cu = per_cu->cu->read_in_chain;
19314
68dc6402 19315 free_heap_comp_unit (per_cu->cu);
ae038cb0
DJ
19316 *last_chain = next_cu;
19317
19318 per_cu = next_cu;
19319 }
19320}
19321
19322/* Increase the age counter on each cached compilation unit, and free
19323 any that are too old. */
19324
19325static void
19326age_cached_comp_units (void)
19327{
19328 struct dwarf2_per_cu_data *per_cu, **last_chain;
19329
19330 dwarf2_clear_marks (dwarf2_per_objfile->read_in_chain);
19331 per_cu = dwarf2_per_objfile->read_in_chain;
19332 while (per_cu != NULL)
19333 {
19334 per_cu->cu->last_used ++;
19335 if (per_cu->cu->last_used <= dwarf2_max_cache_age)
19336 dwarf2_mark (per_cu->cu);
19337 per_cu = per_cu->cu->read_in_chain;
19338 }
19339
19340 per_cu = dwarf2_per_objfile->read_in_chain;
19341 last_chain = &dwarf2_per_objfile->read_in_chain;
19342 while (per_cu != NULL)
19343 {
19344 struct dwarf2_per_cu_data *next_cu;
19345
19346 next_cu = per_cu->cu->read_in_chain;
19347
19348 if (!per_cu->cu->mark)
19349 {
68dc6402 19350 free_heap_comp_unit (per_cu->cu);
ae038cb0
DJ
19351 *last_chain = next_cu;
19352 }
19353 else
19354 last_chain = &per_cu->cu->read_in_chain;
19355
19356 per_cu = next_cu;
19357 }
19358}
19359
19360/* Remove a single compilation unit from the cache. */
19361
19362static void
dee91e82 19363free_one_cached_comp_unit (struct dwarf2_per_cu_data *target_per_cu)
ae038cb0
DJ
19364{
19365 struct dwarf2_per_cu_data *per_cu, **last_chain;
19366
19367 per_cu = dwarf2_per_objfile->read_in_chain;
19368 last_chain = &dwarf2_per_objfile->read_in_chain;
19369 while (per_cu != NULL)
19370 {
19371 struct dwarf2_per_cu_data *next_cu;
19372
19373 next_cu = per_cu->cu->read_in_chain;
19374
dee91e82 19375 if (per_cu == target_per_cu)
ae038cb0 19376 {
68dc6402 19377 free_heap_comp_unit (per_cu->cu);
dee91e82 19378 per_cu->cu = NULL;
ae038cb0
DJ
19379 *last_chain = next_cu;
19380 break;
19381 }
19382 else
19383 last_chain = &per_cu->cu->read_in_chain;
19384
19385 per_cu = next_cu;
19386 }
19387}
19388
fe3e1990
DJ
19389/* Release all extra memory associated with OBJFILE. */
19390
19391void
19392dwarf2_free_objfile (struct objfile *objfile)
19393{
19394 dwarf2_per_objfile = objfile_data (objfile, dwarf2_objfile_data_key);
19395
19396 if (dwarf2_per_objfile == NULL)
19397 return;
19398
19399 /* Cached DIE trees use xmalloc and the comp_unit_obstack. */
19400 free_cached_comp_units (NULL);
19401
7b9f3c50
DE
19402 if (dwarf2_per_objfile->quick_file_names_table)
19403 htab_delete (dwarf2_per_objfile->quick_file_names_table);
9291a0cd 19404
fe3e1990
DJ
19405 /* Everything else should be on the objfile obstack. */
19406}
19407
dee91e82
DE
19408/* A set of CU "per_cu" pointer, DIE offset, and GDB type pointer.
19409 We store these in a hash table separate from the DIEs, and preserve them
19410 when the DIEs are flushed out of cache.
19411
19412 The CU "per_cu" pointer is needed because offset alone is not enough to
3019eac3
DE
19413 uniquely identify the type. A file may have multiple .debug_types sections,
19414 or the type may come from a DWO file. We have to use something in
19415 dwarf2_per_cu_data (or the pointer to it) because we can enter the lookup
19416 routine, get_die_type_at_offset, from outside this file, and thus won't
19417 necessarily have PER_CU->cu. Fortunately, PER_CU is stable for the life
19418 of the objfile. */
1c379e20 19419
dee91e82 19420struct dwarf2_per_cu_offset_and_type
1c379e20 19421{
dee91e82 19422 const struct dwarf2_per_cu_data *per_cu;
b64f50a1 19423 sect_offset offset;
1c379e20
DJ
19424 struct type *type;
19425};
19426
dee91e82 19427/* Hash function for a dwarf2_per_cu_offset_and_type. */
1c379e20
DJ
19428
19429static hashval_t
dee91e82 19430per_cu_offset_and_type_hash (const void *item)
1c379e20 19431{
dee91e82 19432 const struct dwarf2_per_cu_offset_and_type *ofs = item;
9a619af0 19433
dee91e82 19434 return (uintptr_t) ofs->per_cu + ofs->offset.sect_off;
1c379e20
DJ
19435}
19436
dee91e82 19437/* Equality function for a dwarf2_per_cu_offset_and_type. */
1c379e20
DJ
19438
19439static int
dee91e82 19440per_cu_offset_and_type_eq (const void *item_lhs, const void *item_rhs)
1c379e20 19441{
dee91e82
DE
19442 const struct dwarf2_per_cu_offset_and_type *ofs_lhs = item_lhs;
19443 const struct dwarf2_per_cu_offset_and_type *ofs_rhs = item_rhs;
9a619af0 19444
dee91e82
DE
19445 return (ofs_lhs->per_cu == ofs_rhs->per_cu
19446 && ofs_lhs->offset.sect_off == ofs_rhs->offset.sect_off);
1c379e20
DJ
19447}
19448
19449/* Set the type associated with DIE to TYPE. Save it in CU's hash
7e314c57
JK
19450 table if necessary. For convenience, return TYPE.
19451
19452 The DIEs reading must have careful ordering to:
19453 * Not cause infite loops trying to read in DIEs as a prerequisite for
19454 reading current DIE.
19455 * Not trying to dereference contents of still incompletely read in types
19456 while reading in other DIEs.
19457 * Enable referencing still incompletely read in types just by a pointer to
19458 the type without accessing its fields.
19459
19460 Therefore caller should follow these rules:
19461 * Try to fetch any prerequisite types we may need to build this DIE type
19462 before building the type and calling set_die_type.
e71ec853 19463 * After building type call set_die_type for current DIE as soon as
7e314c57
JK
19464 possible before fetching more types to complete the current type.
19465 * Make the type as complete as possible before fetching more types. */
1c379e20 19466
f792889a 19467static struct type *
1c379e20
DJ
19468set_die_type (struct die_info *die, struct type *type, struct dwarf2_cu *cu)
19469{
dee91e82 19470 struct dwarf2_per_cu_offset_and_type **slot, ofs;
673bfd45 19471 struct objfile *objfile = cu->objfile;
1c379e20 19472
b4ba55a1
JB
19473 /* For Ada types, make sure that the gnat-specific data is always
19474 initialized (if not already set). There are a few types where
19475 we should not be doing so, because the type-specific area is
19476 already used to hold some other piece of info (eg: TYPE_CODE_FLT
19477 where the type-specific area is used to store the floatformat).
19478 But this is not a problem, because the gnat-specific information
19479 is actually not needed for these types. */
19480 if (need_gnat_info (cu)
19481 && TYPE_CODE (type) != TYPE_CODE_FUNC
19482 && TYPE_CODE (type) != TYPE_CODE_FLT
19483 && !HAVE_GNAT_AUX_INFO (type))
19484 INIT_GNAT_SPECIFIC (type);
19485
dee91e82 19486 if (dwarf2_per_objfile->die_type_hash == NULL)
f792889a 19487 {
dee91e82
DE
19488 dwarf2_per_objfile->die_type_hash =
19489 htab_create_alloc_ex (127,
19490 per_cu_offset_and_type_hash,
19491 per_cu_offset_and_type_eq,
19492 NULL,
19493 &objfile->objfile_obstack,
19494 hashtab_obstack_allocate,
19495 dummy_obstack_deallocate);
f792889a 19496 }
1c379e20 19497
dee91e82 19498 ofs.per_cu = cu->per_cu;
1c379e20
DJ
19499 ofs.offset = die->offset;
19500 ofs.type = type;
dee91e82
DE
19501 slot = (struct dwarf2_per_cu_offset_and_type **)
19502 htab_find_slot (dwarf2_per_objfile->die_type_hash, &ofs, INSERT);
7e314c57
JK
19503 if (*slot)
19504 complaint (&symfile_complaints,
19505 _("A problem internal to GDB: DIE 0x%x has type already set"),
b64f50a1 19506 die->offset.sect_off);
673bfd45 19507 *slot = obstack_alloc (&objfile->objfile_obstack, sizeof (**slot));
1c379e20 19508 **slot = ofs;
f792889a 19509 return type;
1c379e20
DJ
19510}
19511
380bca97 19512/* Look up the type for the die at OFFSET in the appropriate type_hash
673bfd45 19513 table, or return NULL if the die does not have a saved type. */
1c379e20
DJ
19514
19515static struct type *
b64f50a1 19516get_die_type_at_offset (sect_offset offset,
673bfd45 19517 struct dwarf2_per_cu_data *per_cu)
1c379e20 19518{
dee91e82 19519 struct dwarf2_per_cu_offset_and_type *slot, ofs;
f792889a 19520
dee91e82 19521 if (dwarf2_per_objfile->die_type_hash == NULL)
f792889a 19522 return NULL;
1c379e20 19523
dee91e82 19524 ofs.per_cu = per_cu;
673bfd45 19525 ofs.offset = offset;
dee91e82 19526 slot = htab_find (dwarf2_per_objfile->die_type_hash, &ofs);
1c379e20
DJ
19527 if (slot)
19528 return slot->type;
19529 else
19530 return NULL;
19531}
19532
673bfd45
DE
19533/* Look up the type for DIE in the appropriate type_hash table,
19534 or return NULL if DIE does not have a saved type. */
19535
19536static struct type *
19537get_die_type (struct die_info *die, struct dwarf2_cu *cu)
19538{
19539 return get_die_type_at_offset (die->offset, cu->per_cu);
19540}
19541
10b3939b
DJ
19542/* Add a dependence relationship from CU to REF_PER_CU. */
19543
19544static void
19545dwarf2_add_dependence (struct dwarf2_cu *cu,
19546 struct dwarf2_per_cu_data *ref_per_cu)
19547{
19548 void **slot;
19549
19550 if (cu->dependencies == NULL)
19551 cu->dependencies
19552 = htab_create_alloc_ex (5, htab_hash_pointer, htab_eq_pointer,
19553 NULL, &cu->comp_unit_obstack,
19554 hashtab_obstack_allocate,
19555 dummy_obstack_deallocate);
19556
19557 slot = htab_find_slot (cu->dependencies, ref_per_cu, INSERT);
19558 if (*slot == NULL)
19559 *slot = ref_per_cu;
19560}
1c379e20 19561
f504f079
DE
19562/* Subroutine of dwarf2_mark to pass to htab_traverse.
19563 Set the mark field in every compilation unit in the
ae038cb0
DJ
19564 cache that we must keep because we are keeping CU. */
19565
10b3939b
DJ
19566static int
19567dwarf2_mark_helper (void **slot, void *data)
19568{
19569 struct dwarf2_per_cu_data *per_cu;
19570
19571 per_cu = (struct dwarf2_per_cu_data *) *slot;
d07ed419
JK
19572
19573 /* cu->dependencies references may not yet have been ever read if QUIT aborts
19574 reading of the chain. As such dependencies remain valid it is not much
19575 useful to track and undo them during QUIT cleanups. */
19576 if (per_cu->cu == NULL)
19577 return 1;
19578
10b3939b
DJ
19579 if (per_cu->cu->mark)
19580 return 1;
19581 per_cu->cu->mark = 1;
19582
19583 if (per_cu->cu->dependencies != NULL)
19584 htab_traverse (per_cu->cu->dependencies, dwarf2_mark_helper, NULL);
19585
19586 return 1;
19587}
19588
f504f079
DE
19589/* Set the mark field in CU and in every other compilation unit in the
19590 cache that we must keep because we are keeping CU. */
19591
ae038cb0
DJ
19592static void
19593dwarf2_mark (struct dwarf2_cu *cu)
19594{
19595 if (cu->mark)
19596 return;
19597 cu->mark = 1;
10b3939b
DJ
19598 if (cu->dependencies != NULL)
19599 htab_traverse (cu->dependencies, dwarf2_mark_helper, NULL);
ae038cb0
DJ
19600}
19601
19602static void
19603dwarf2_clear_marks (struct dwarf2_per_cu_data *per_cu)
19604{
19605 while (per_cu)
19606 {
19607 per_cu->cu->mark = 0;
19608 per_cu = per_cu->cu->read_in_chain;
19609 }
72bf9492
DJ
19610}
19611
72bf9492
DJ
19612/* Trivial hash function for partial_die_info: the hash value of a DIE
19613 is its offset in .debug_info for this objfile. */
19614
19615static hashval_t
19616partial_die_hash (const void *item)
19617{
19618 const struct partial_die_info *part_die = item;
9a619af0 19619
b64f50a1 19620 return part_die->offset.sect_off;
72bf9492
DJ
19621}
19622
19623/* Trivial comparison function for partial_die_info structures: two DIEs
19624 are equal if they have the same offset. */
19625
19626static int
19627partial_die_eq (const void *item_lhs, const void *item_rhs)
19628{
19629 const struct partial_die_info *part_die_lhs = item_lhs;
19630 const struct partial_die_info *part_die_rhs = item_rhs;
9a619af0 19631
b64f50a1 19632 return part_die_lhs->offset.sect_off == part_die_rhs->offset.sect_off;
72bf9492
DJ
19633}
19634
ae038cb0
DJ
19635static struct cmd_list_element *set_dwarf2_cmdlist;
19636static struct cmd_list_element *show_dwarf2_cmdlist;
19637
19638static void
19639set_dwarf2_cmd (char *args, int from_tty)
19640{
19641 help_list (set_dwarf2_cmdlist, "maintenance set dwarf2 ", -1, gdb_stdout);
19642}
19643
19644static void
19645show_dwarf2_cmd (char *args, int from_tty)
6e70227d 19646{
ae038cb0
DJ
19647 cmd_show_list (show_dwarf2_cmdlist, from_tty, "");
19648}
19649
4bf44c1c 19650/* Free data associated with OBJFILE, if necessary. */
dce234bc
PP
19651
19652static void
c1bd65d0 19653dwarf2_per_objfile_free (struct objfile *objfile, void *d)
dce234bc
PP
19654{
19655 struct dwarf2_per_objfile *data = d;
8b70b953 19656 int ix;
8b70b953 19657
95554aad
TT
19658 for (ix = 0; ix < dwarf2_per_objfile->n_comp_units; ++ix)
19659 VEC_free (dwarf2_per_cu_ptr,
796a7ff8
DE
19660 dwarf2_per_objfile->all_comp_units[ix]->imported_symtabs);
19661
19662 for (ix = 0; ix < dwarf2_per_objfile->n_type_units; ++ix)
19663 VEC_free (dwarf2_per_cu_ptr,
19664 dwarf2_per_objfile->all_type_units[ix]->per_cu.imported_symtabs);
95554aad 19665
8b70b953 19666 VEC_free (dwarf2_section_info_def, data->types);
3019eac3
DE
19667
19668 if (data->dwo_files)
19669 free_dwo_files (data->dwo_files, objfile);
36586728
TT
19670
19671 if (data->dwz_file && data->dwz_file->dwz_bfd)
19672 gdb_bfd_unref (data->dwz_file->dwz_bfd);
9291a0cd
TT
19673}
19674
19675\f
ae2de4f8 19676/* The "save gdb-index" command. */
9291a0cd
TT
19677
19678/* The contents of the hash table we create when building the string
19679 table. */
19680struct strtab_entry
19681{
19682 offset_type offset;
19683 const char *str;
19684};
19685
559a7a62
JK
19686/* Hash function for a strtab_entry.
19687
19688 Function is used only during write_hash_table so no index format backward
19689 compatibility is needed. */
b89be57b 19690
9291a0cd
TT
19691static hashval_t
19692hash_strtab_entry (const void *e)
19693{
19694 const struct strtab_entry *entry = e;
559a7a62 19695 return mapped_index_string_hash (INT_MAX, entry->str);
9291a0cd
TT
19696}
19697
19698/* Equality function for a strtab_entry. */
b89be57b 19699
9291a0cd
TT
19700static int
19701eq_strtab_entry (const void *a, const void *b)
19702{
19703 const struct strtab_entry *ea = a;
19704 const struct strtab_entry *eb = b;
19705 return !strcmp (ea->str, eb->str);
19706}
19707
19708/* Create a strtab_entry hash table. */
b89be57b 19709
9291a0cd
TT
19710static htab_t
19711create_strtab (void)
19712{
19713 return htab_create_alloc (100, hash_strtab_entry, eq_strtab_entry,
19714 xfree, xcalloc, xfree);
19715}
19716
19717/* Add a string to the constant pool. Return the string's offset in
19718 host order. */
b89be57b 19719
9291a0cd
TT
19720static offset_type
19721add_string (htab_t table, struct obstack *cpool, const char *str)
19722{
19723 void **slot;
19724 struct strtab_entry entry;
19725 struct strtab_entry *result;
19726
19727 entry.str = str;
19728 slot = htab_find_slot (table, &entry, INSERT);
19729 if (*slot)
19730 result = *slot;
19731 else
19732 {
19733 result = XNEW (struct strtab_entry);
19734 result->offset = obstack_object_size (cpool);
19735 result->str = str;
19736 obstack_grow_str0 (cpool, str);
19737 *slot = result;
19738 }
19739 return result->offset;
19740}
19741
19742/* An entry in the symbol table. */
19743struct symtab_index_entry
19744{
19745 /* The name of the symbol. */
19746 const char *name;
19747 /* The offset of the name in the constant pool. */
19748 offset_type index_offset;
19749 /* A sorted vector of the indices of all the CUs that hold an object
19750 of this name. */
19751 VEC (offset_type) *cu_indices;
19752};
19753
19754/* The symbol table. This is a power-of-2-sized hash table. */
19755struct mapped_symtab
19756{
19757 offset_type n_elements;
19758 offset_type size;
19759 struct symtab_index_entry **data;
19760};
19761
19762/* Hash function for a symtab_index_entry. */
b89be57b 19763
9291a0cd
TT
19764static hashval_t
19765hash_symtab_entry (const void *e)
19766{
19767 const struct symtab_index_entry *entry = e;
19768 return iterative_hash (VEC_address (offset_type, entry->cu_indices),
19769 sizeof (offset_type) * VEC_length (offset_type,
19770 entry->cu_indices),
19771 0);
19772}
19773
19774/* Equality function for a symtab_index_entry. */
b89be57b 19775
9291a0cd
TT
19776static int
19777eq_symtab_entry (const void *a, const void *b)
19778{
19779 const struct symtab_index_entry *ea = a;
19780 const struct symtab_index_entry *eb = b;
19781 int len = VEC_length (offset_type, ea->cu_indices);
19782 if (len != VEC_length (offset_type, eb->cu_indices))
19783 return 0;
19784 return !memcmp (VEC_address (offset_type, ea->cu_indices),
19785 VEC_address (offset_type, eb->cu_indices),
19786 sizeof (offset_type) * len);
19787}
19788
19789/* Destroy a symtab_index_entry. */
b89be57b 19790
9291a0cd
TT
19791static void
19792delete_symtab_entry (void *p)
19793{
19794 struct symtab_index_entry *entry = p;
19795 VEC_free (offset_type, entry->cu_indices);
19796 xfree (entry);
19797}
19798
19799/* Create a hash table holding symtab_index_entry objects. */
b89be57b 19800
9291a0cd 19801static htab_t
3876f04e 19802create_symbol_hash_table (void)
9291a0cd
TT
19803{
19804 return htab_create_alloc (100, hash_symtab_entry, eq_symtab_entry,
19805 delete_symtab_entry, xcalloc, xfree);
19806}
19807
19808/* Create a new mapped symtab object. */
b89be57b 19809
9291a0cd
TT
19810static struct mapped_symtab *
19811create_mapped_symtab (void)
19812{
19813 struct mapped_symtab *symtab = XNEW (struct mapped_symtab);
19814 symtab->n_elements = 0;
19815 symtab->size = 1024;
19816 symtab->data = XCNEWVEC (struct symtab_index_entry *, symtab->size);
19817 return symtab;
19818}
19819
19820/* Destroy a mapped_symtab. */
b89be57b 19821
9291a0cd
TT
19822static void
19823cleanup_mapped_symtab (void *p)
19824{
19825 struct mapped_symtab *symtab = p;
19826 /* The contents of the array are freed when the other hash table is
19827 destroyed. */
19828 xfree (symtab->data);
19829 xfree (symtab);
19830}
19831
19832/* Find a slot in SYMTAB for the symbol NAME. Returns a pointer to
559a7a62
JK
19833 the slot.
19834
19835 Function is used only during write_hash_table so no index format backward
19836 compatibility is needed. */
b89be57b 19837
9291a0cd
TT
19838static struct symtab_index_entry **
19839find_slot (struct mapped_symtab *symtab, const char *name)
19840{
559a7a62 19841 offset_type index, step, hash = mapped_index_string_hash (INT_MAX, name);
9291a0cd
TT
19842
19843 index = hash & (symtab->size - 1);
19844 step = ((hash * 17) & (symtab->size - 1)) | 1;
19845
19846 for (;;)
19847 {
19848 if (!symtab->data[index] || !strcmp (name, symtab->data[index]->name))
19849 return &symtab->data[index];
19850 index = (index + step) & (symtab->size - 1);
19851 }
19852}
19853
19854/* Expand SYMTAB's hash table. */
b89be57b 19855
9291a0cd
TT
19856static void
19857hash_expand (struct mapped_symtab *symtab)
19858{
19859 offset_type old_size = symtab->size;
19860 offset_type i;
19861 struct symtab_index_entry **old_entries = symtab->data;
19862
19863 symtab->size *= 2;
19864 symtab->data = XCNEWVEC (struct symtab_index_entry *, symtab->size);
19865
19866 for (i = 0; i < old_size; ++i)
19867 {
19868 if (old_entries[i])
19869 {
19870 struct symtab_index_entry **slot = find_slot (symtab,
19871 old_entries[i]->name);
19872 *slot = old_entries[i];
19873 }
19874 }
19875
19876 xfree (old_entries);
19877}
19878
156942c7
DE
19879/* Add an entry to SYMTAB. NAME is the name of the symbol.
19880 CU_INDEX is the index of the CU in which the symbol appears.
19881 IS_STATIC is one if the symbol is static, otherwise zero (global). */
b89be57b 19882
9291a0cd
TT
19883static void
19884add_index_entry (struct mapped_symtab *symtab, const char *name,
156942c7 19885 int is_static, gdb_index_symbol_kind kind,
9291a0cd
TT
19886 offset_type cu_index)
19887{
19888 struct symtab_index_entry **slot;
156942c7 19889 offset_type cu_index_and_attrs;
9291a0cd
TT
19890
19891 ++symtab->n_elements;
19892 if (4 * symtab->n_elements / 3 >= symtab->size)
19893 hash_expand (symtab);
19894
19895 slot = find_slot (symtab, name);
19896 if (!*slot)
19897 {
19898 *slot = XNEW (struct symtab_index_entry);
19899 (*slot)->name = name;
156942c7 19900 /* index_offset is set later. */
9291a0cd
TT
19901 (*slot)->cu_indices = NULL;
19902 }
156942c7
DE
19903
19904 cu_index_and_attrs = 0;
19905 DW2_GDB_INDEX_CU_SET_VALUE (cu_index_and_attrs, cu_index);
19906 DW2_GDB_INDEX_SYMBOL_STATIC_SET_VALUE (cu_index_and_attrs, is_static);
19907 DW2_GDB_INDEX_SYMBOL_KIND_SET_VALUE (cu_index_and_attrs, kind);
19908
19909 /* We don't want to record an index value twice as we want to avoid the
19910 duplication.
19911 We process all global symbols and then all static symbols
19912 (which would allow us to avoid the duplication by only having to check
19913 the last entry pushed), but a symbol could have multiple kinds in one CU.
19914 To keep things simple we don't worry about the duplication here and
19915 sort and uniqufy the list after we've processed all symbols. */
19916 VEC_safe_push (offset_type, (*slot)->cu_indices, cu_index_and_attrs);
19917}
19918
19919/* qsort helper routine for uniquify_cu_indices. */
19920
19921static int
19922offset_type_compare (const void *ap, const void *bp)
19923{
19924 offset_type a = *(offset_type *) ap;
19925 offset_type b = *(offset_type *) bp;
19926
19927 return (a > b) - (b > a);
19928}
19929
19930/* Sort and remove duplicates of all symbols' cu_indices lists. */
19931
19932static void
19933uniquify_cu_indices (struct mapped_symtab *symtab)
19934{
19935 int i;
19936
19937 for (i = 0; i < symtab->size; ++i)
19938 {
19939 struct symtab_index_entry *entry = symtab->data[i];
19940
19941 if (entry
19942 && entry->cu_indices != NULL)
19943 {
19944 unsigned int next_to_insert, next_to_check;
19945 offset_type last_value;
19946
19947 qsort (VEC_address (offset_type, entry->cu_indices),
19948 VEC_length (offset_type, entry->cu_indices),
19949 sizeof (offset_type), offset_type_compare);
19950
19951 last_value = VEC_index (offset_type, entry->cu_indices, 0);
19952 next_to_insert = 1;
19953 for (next_to_check = 1;
19954 next_to_check < VEC_length (offset_type, entry->cu_indices);
19955 ++next_to_check)
19956 {
19957 if (VEC_index (offset_type, entry->cu_indices, next_to_check)
19958 != last_value)
19959 {
19960 last_value = VEC_index (offset_type, entry->cu_indices,
19961 next_to_check);
19962 VEC_replace (offset_type, entry->cu_indices, next_to_insert,
19963 last_value);
19964 ++next_to_insert;
19965 }
19966 }
19967 VEC_truncate (offset_type, entry->cu_indices, next_to_insert);
19968 }
19969 }
9291a0cd
TT
19970}
19971
19972/* Add a vector of indices to the constant pool. */
b89be57b 19973
9291a0cd 19974static offset_type
3876f04e 19975add_indices_to_cpool (htab_t symbol_hash_table, struct obstack *cpool,
9291a0cd
TT
19976 struct symtab_index_entry *entry)
19977{
19978 void **slot;
19979
3876f04e 19980 slot = htab_find_slot (symbol_hash_table, entry, INSERT);
9291a0cd
TT
19981 if (!*slot)
19982 {
19983 offset_type len = VEC_length (offset_type, entry->cu_indices);
19984 offset_type val = MAYBE_SWAP (len);
19985 offset_type iter;
19986 int i;
19987
19988 *slot = entry;
19989 entry->index_offset = obstack_object_size (cpool);
19990
19991 obstack_grow (cpool, &val, sizeof (val));
19992 for (i = 0;
19993 VEC_iterate (offset_type, entry->cu_indices, i, iter);
19994 ++i)
19995 {
19996 val = MAYBE_SWAP (iter);
19997 obstack_grow (cpool, &val, sizeof (val));
19998 }
19999 }
20000 else
20001 {
20002 struct symtab_index_entry *old_entry = *slot;
20003 entry->index_offset = old_entry->index_offset;
20004 entry = old_entry;
20005 }
20006 return entry->index_offset;
20007}
20008
20009/* Write the mapped hash table SYMTAB to the obstack OUTPUT, with
20010 constant pool entries going into the obstack CPOOL. */
b89be57b 20011
9291a0cd
TT
20012static void
20013write_hash_table (struct mapped_symtab *symtab,
20014 struct obstack *output, struct obstack *cpool)
20015{
20016 offset_type i;
3876f04e 20017 htab_t symbol_hash_table;
9291a0cd
TT
20018 htab_t str_table;
20019
3876f04e 20020 symbol_hash_table = create_symbol_hash_table ();
9291a0cd 20021 str_table = create_strtab ();
3876f04e 20022
9291a0cd
TT
20023 /* We add all the index vectors to the constant pool first, to
20024 ensure alignment is ok. */
20025 for (i = 0; i < symtab->size; ++i)
20026 {
20027 if (symtab->data[i])
3876f04e 20028 add_indices_to_cpool (symbol_hash_table, cpool, symtab->data[i]);
9291a0cd
TT
20029 }
20030
20031 /* Now write out the hash table. */
20032 for (i = 0; i < symtab->size; ++i)
20033 {
20034 offset_type str_off, vec_off;
20035
20036 if (symtab->data[i])
20037 {
20038 str_off = add_string (str_table, cpool, symtab->data[i]->name);
20039 vec_off = symtab->data[i]->index_offset;
20040 }
20041 else
20042 {
20043 /* While 0 is a valid constant pool index, it is not valid
20044 to have 0 for both offsets. */
20045 str_off = 0;
20046 vec_off = 0;
20047 }
20048
20049 str_off = MAYBE_SWAP (str_off);
20050 vec_off = MAYBE_SWAP (vec_off);
20051
20052 obstack_grow (output, &str_off, sizeof (str_off));
20053 obstack_grow (output, &vec_off, sizeof (vec_off));
20054 }
20055
20056 htab_delete (str_table);
3876f04e 20057 htab_delete (symbol_hash_table);
9291a0cd
TT
20058}
20059
0a5429f6
DE
20060/* Struct to map psymtab to CU index in the index file. */
20061struct psymtab_cu_index_map
20062{
20063 struct partial_symtab *psymtab;
20064 unsigned int cu_index;
20065};
20066
20067static hashval_t
20068hash_psymtab_cu_index (const void *item)
20069{
20070 const struct psymtab_cu_index_map *map = item;
20071
20072 return htab_hash_pointer (map->psymtab);
20073}
20074
20075static int
20076eq_psymtab_cu_index (const void *item_lhs, const void *item_rhs)
20077{
20078 const struct psymtab_cu_index_map *lhs = item_lhs;
20079 const struct psymtab_cu_index_map *rhs = item_rhs;
20080
20081 return lhs->psymtab == rhs->psymtab;
20082}
20083
20084/* Helper struct for building the address table. */
20085struct addrmap_index_data
20086{
20087 struct objfile *objfile;
20088 struct obstack *addr_obstack;
20089 htab_t cu_index_htab;
20090
20091 /* Non-zero if the previous_* fields are valid.
20092 We can't write an entry until we see the next entry (since it is only then
20093 that we know the end of the entry). */
20094 int previous_valid;
20095 /* Index of the CU in the table of all CUs in the index file. */
20096 unsigned int previous_cu_index;
0963b4bd 20097 /* Start address of the CU. */
0a5429f6
DE
20098 CORE_ADDR previous_cu_start;
20099};
20100
20101/* Write an address entry to OBSTACK. */
b89be57b 20102
9291a0cd 20103static void
0a5429f6
DE
20104add_address_entry (struct objfile *objfile, struct obstack *obstack,
20105 CORE_ADDR start, CORE_ADDR end, unsigned int cu_index)
9291a0cd 20106{
0a5429f6 20107 offset_type cu_index_to_write;
9291a0cd
TT
20108 char addr[8];
20109 CORE_ADDR baseaddr;
20110
20111 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
20112
0a5429f6
DE
20113 store_unsigned_integer (addr, 8, BFD_ENDIAN_LITTLE, start - baseaddr);
20114 obstack_grow (obstack, addr, 8);
20115 store_unsigned_integer (addr, 8, BFD_ENDIAN_LITTLE, end - baseaddr);
20116 obstack_grow (obstack, addr, 8);
20117 cu_index_to_write = MAYBE_SWAP (cu_index);
20118 obstack_grow (obstack, &cu_index_to_write, sizeof (offset_type));
20119}
20120
20121/* Worker function for traversing an addrmap to build the address table. */
20122
20123static int
20124add_address_entry_worker (void *datap, CORE_ADDR start_addr, void *obj)
20125{
20126 struct addrmap_index_data *data = datap;
20127 struct partial_symtab *pst = obj;
0a5429f6
DE
20128
20129 if (data->previous_valid)
20130 add_address_entry (data->objfile, data->addr_obstack,
20131 data->previous_cu_start, start_addr,
20132 data->previous_cu_index);
20133
20134 data->previous_cu_start = start_addr;
20135 if (pst != NULL)
20136 {
20137 struct psymtab_cu_index_map find_map, *map;
20138 find_map.psymtab = pst;
20139 map = htab_find (data->cu_index_htab, &find_map);
20140 gdb_assert (map != NULL);
20141 data->previous_cu_index = map->cu_index;
20142 data->previous_valid = 1;
20143 }
20144 else
20145 data->previous_valid = 0;
20146
20147 return 0;
20148}
20149
20150/* Write OBJFILE's address map to OBSTACK.
20151 CU_INDEX_HTAB is used to map addrmap entries to their CU indices
20152 in the index file. */
20153
20154static void
20155write_address_map (struct objfile *objfile, struct obstack *obstack,
20156 htab_t cu_index_htab)
20157{
20158 struct addrmap_index_data addrmap_index_data;
20159
20160 /* When writing the address table, we have to cope with the fact that
20161 the addrmap iterator only provides the start of a region; we have to
20162 wait until the next invocation to get the start of the next region. */
20163
20164 addrmap_index_data.objfile = objfile;
20165 addrmap_index_data.addr_obstack = obstack;
20166 addrmap_index_data.cu_index_htab = cu_index_htab;
20167 addrmap_index_data.previous_valid = 0;
20168
20169 addrmap_foreach (objfile->psymtabs_addrmap, add_address_entry_worker,
20170 &addrmap_index_data);
20171
20172 /* It's highly unlikely the last entry (end address = 0xff...ff)
20173 is valid, but we should still handle it.
20174 The end address is recorded as the start of the next region, but that
20175 doesn't work here. To cope we pass 0xff...ff, this is a rare situation
20176 anyway. */
20177 if (addrmap_index_data.previous_valid)
20178 add_address_entry (objfile, obstack,
20179 addrmap_index_data.previous_cu_start, (CORE_ADDR) -1,
20180 addrmap_index_data.previous_cu_index);
9291a0cd
TT
20181}
20182
156942c7
DE
20183/* Return the symbol kind of PSYM. */
20184
20185static gdb_index_symbol_kind
20186symbol_kind (struct partial_symbol *psym)
20187{
20188 domain_enum domain = PSYMBOL_DOMAIN (psym);
20189 enum address_class aclass = PSYMBOL_CLASS (psym);
20190
20191 switch (domain)
20192 {
20193 case VAR_DOMAIN:
20194 switch (aclass)
20195 {
20196 case LOC_BLOCK:
20197 return GDB_INDEX_SYMBOL_KIND_FUNCTION;
20198 case LOC_TYPEDEF:
20199 return GDB_INDEX_SYMBOL_KIND_TYPE;
20200 case LOC_COMPUTED:
20201 case LOC_CONST_BYTES:
20202 case LOC_OPTIMIZED_OUT:
20203 case LOC_STATIC:
20204 return GDB_INDEX_SYMBOL_KIND_VARIABLE;
20205 case LOC_CONST:
20206 /* Note: It's currently impossible to recognize psyms as enum values
20207 short of reading the type info. For now punt. */
20208 return GDB_INDEX_SYMBOL_KIND_VARIABLE;
20209 default:
20210 /* There are other LOC_FOO values that one might want to classify
20211 as variables, but dwarf2read.c doesn't currently use them. */
20212 return GDB_INDEX_SYMBOL_KIND_OTHER;
20213 }
20214 case STRUCT_DOMAIN:
20215 return GDB_INDEX_SYMBOL_KIND_TYPE;
20216 default:
20217 return GDB_INDEX_SYMBOL_KIND_OTHER;
20218 }
20219}
20220
9291a0cd 20221/* Add a list of partial symbols to SYMTAB. */
b89be57b 20222
9291a0cd
TT
20223static void
20224write_psymbols (struct mapped_symtab *symtab,
987d643c 20225 htab_t psyms_seen,
9291a0cd
TT
20226 struct partial_symbol **psymp,
20227 int count,
987d643c
TT
20228 offset_type cu_index,
20229 int is_static)
9291a0cd
TT
20230{
20231 for (; count-- > 0; ++psymp)
20232 {
156942c7
DE
20233 struct partial_symbol *psym = *psymp;
20234 void **slot;
987d643c 20235
156942c7 20236 if (SYMBOL_LANGUAGE (psym) == language_ada)
9291a0cd 20237 error (_("Ada is not currently supported by the index"));
987d643c 20238
987d643c 20239 /* Only add a given psymbol once. */
156942c7 20240 slot = htab_find_slot (psyms_seen, psym, INSERT);
987d643c
TT
20241 if (!*slot)
20242 {
156942c7
DE
20243 gdb_index_symbol_kind kind = symbol_kind (psym);
20244
20245 *slot = psym;
20246 add_index_entry (symtab, SYMBOL_SEARCH_NAME (psym),
20247 is_static, kind, cu_index);
987d643c 20248 }
9291a0cd
TT
20249 }
20250}
20251
20252/* Write the contents of an ("unfinished") obstack to FILE. Throw an
20253 exception if there is an error. */
b89be57b 20254
9291a0cd
TT
20255static void
20256write_obstack (FILE *file, struct obstack *obstack)
20257{
20258 if (fwrite (obstack_base (obstack), 1, obstack_object_size (obstack),
20259 file)
20260 != obstack_object_size (obstack))
20261 error (_("couldn't data write to file"));
20262}
20263
20264/* Unlink a file if the argument is not NULL. */
b89be57b 20265
9291a0cd
TT
20266static void
20267unlink_if_set (void *p)
20268{
20269 char **filename = p;
20270 if (*filename)
20271 unlink (*filename);
20272}
20273
1fd400ff
TT
20274/* A helper struct used when iterating over debug_types. */
20275struct signatured_type_index_data
20276{
20277 struct objfile *objfile;
20278 struct mapped_symtab *symtab;
20279 struct obstack *types_list;
987d643c 20280 htab_t psyms_seen;
1fd400ff
TT
20281 int cu_index;
20282};
20283
20284/* A helper function that writes a single signatured_type to an
20285 obstack. */
b89be57b 20286
1fd400ff
TT
20287static int
20288write_one_signatured_type (void **slot, void *d)
20289{
20290 struct signatured_type_index_data *info = d;
20291 struct signatured_type *entry = (struct signatured_type *) *slot;
e254ef6a
DE
20292 struct dwarf2_per_cu_data *per_cu = &entry->per_cu;
20293 struct partial_symtab *psymtab = per_cu->v.psymtab;
1fd400ff
TT
20294 gdb_byte val[8];
20295
20296 write_psymbols (info->symtab,
987d643c 20297 info->psyms_seen,
3e43a32a
MS
20298 info->objfile->global_psymbols.list
20299 + psymtab->globals_offset,
987d643c
TT
20300 psymtab->n_global_syms, info->cu_index,
20301 0);
1fd400ff 20302 write_psymbols (info->symtab,
987d643c 20303 info->psyms_seen,
3e43a32a
MS
20304 info->objfile->static_psymbols.list
20305 + psymtab->statics_offset,
987d643c
TT
20306 psymtab->n_static_syms, info->cu_index,
20307 1);
1fd400ff 20308
b64f50a1
JK
20309 store_unsigned_integer (val, 8, BFD_ENDIAN_LITTLE,
20310 entry->per_cu.offset.sect_off);
1fd400ff 20311 obstack_grow (info->types_list, val, 8);
3019eac3
DE
20312 store_unsigned_integer (val, 8, BFD_ENDIAN_LITTLE,
20313 entry->type_offset_in_tu.cu_off);
1fd400ff
TT
20314 obstack_grow (info->types_list, val, 8);
20315 store_unsigned_integer (val, 8, BFD_ENDIAN_LITTLE, entry->signature);
20316 obstack_grow (info->types_list, val, 8);
20317
20318 ++info->cu_index;
20319
20320 return 1;
20321}
20322
95554aad
TT
20323/* Recurse into all "included" dependencies and write their symbols as
20324 if they appeared in this psymtab. */
20325
20326static void
20327recursively_write_psymbols (struct objfile *objfile,
20328 struct partial_symtab *psymtab,
20329 struct mapped_symtab *symtab,
20330 htab_t psyms_seen,
20331 offset_type cu_index)
20332{
20333 int i;
20334
20335 for (i = 0; i < psymtab->number_of_dependencies; ++i)
20336 if (psymtab->dependencies[i]->user != NULL)
20337 recursively_write_psymbols (objfile, psymtab->dependencies[i],
20338 symtab, psyms_seen, cu_index);
20339
20340 write_psymbols (symtab,
20341 psyms_seen,
20342 objfile->global_psymbols.list + psymtab->globals_offset,
20343 psymtab->n_global_syms, cu_index,
20344 0);
20345 write_psymbols (symtab,
20346 psyms_seen,
20347 objfile->static_psymbols.list + psymtab->statics_offset,
20348 psymtab->n_static_syms, cu_index,
20349 1);
20350}
20351
9291a0cd 20352/* Create an index file for OBJFILE in the directory DIR. */
b89be57b 20353
9291a0cd
TT
20354static void
20355write_psymtabs_to_index (struct objfile *objfile, const char *dir)
20356{
20357 struct cleanup *cleanup;
20358 char *filename, *cleanup_filename;
1fd400ff
TT
20359 struct obstack contents, addr_obstack, constant_pool, symtab_obstack;
20360 struct obstack cu_list, types_cu_list;
9291a0cd
TT
20361 int i;
20362 FILE *out_file;
20363 struct mapped_symtab *symtab;
20364 offset_type val, size_of_contents, total_len;
20365 struct stat st;
987d643c 20366 htab_t psyms_seen;
0a5429f6
DE
20367 htab_t cu_index_htab;
20368 struct psymtab_cu_index_map *psymtab_cu_index_map;
9291a0cd 20369
b4f2f049 20370 if (!objfile->psymtabs || !objfile->psymtabs_addrmap)
9291a0cd 20371 return;
b4f2f049 20372
9291a0cd
TT
20373 if (dwarf2_per_objfile->using_index)
20374 error (_("Cannot use an index to create the index"));
20375
8b70b953
TT
20376 if (VEC_length (dwarf2_section_info_def, dwarf2_per_objfile->types) > 1)
20377 error (_("Cannot make an index when the file has multiple .debug_types sections"));
20378
9291a0cd 20379 if (stat (objfile->name, &st) < 0)
7e17e088 20380 perror_with_name (objfile->name);
9291a0cd
TT
20381
20382 filename = concat (dir, SLASH_STRING, lbasename (objfile->name),
20383 INDEX_SUFFIX, (char *) NULL);
20384 cleanup = make_cleanup (xfree, filename);
20385
20386 out_file = fopen (filename, "wb");
20387 if (!out_file)
20388 error (_("Can't open `%s' for writing"), filename);
20389
20390 cleanup_filename = filename;
20391 make_cleanup (unlink_if_set, &cleanup_filename);
20392
20393 symtab = create_mapped_symtab ();
20394 make_cleanup (cleanup_mapped_symtab, symtab);
20395
20396 obstack_init (&addr_obstack);
20397 make_cleanup_obstack_free (&addr_obstack);
20398
20399 obstack_init (&cu_list);
20400 make_cleanup_obstack_free (&cu_list);
20401
1fd400ff
TT
20402 obstack_init (&types_cu_list);
20403 make_cleanup_obstack_free (&types_cu_list);
20404
987d643c
TT
20405 psyms_seen = htab_create_alloc (100, htab_hash_pointer, htab_eq_pointer,
20406 NULL, xcalloc, xfree);
96408a79 20407 make_cleanup_htab_delete (psyms_seen);
987d643c 20408
0a5429f6
DE
20409 /* While we're scanning CU's create a table that maps a psymtab pointer
20410 (which is what addrmap records) to its index (which is what is recorded
20411 in the index file). This will later be needed to write the address
20412 table. */
20413 cu_index_htab = htab_create_alloc (100,
20414 hash_psymtab_cu_index,
20415 eq_psymtab_cu_index,
20416 NULL, xcalloc, xfree);
96408a79 20417 make_cleanup_htab_delete (cu_index_htab);
0a5429f6
DE
20418 psymtab_cu_index_map = (struct psymtab_cu_index_map *)
20419 xmalloc (sizeof (struct psymtab_cu_index_map)
20420 * dwarf2_per_objfile->n_comp_units);
20421 make_cleanup (xfree, psymtab_cu_index_map);
20422
20423 /* The CU list is already sorted, so we don't need to do additional
1fd400ff
TT
20424 work here. Also, the debug_types entries do not appear in
20425 all_comp_units, but only in their own hash table. */
9291a0cd
TT
20426 for (i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
20427 {
3e43a32a
MS
20428 struct dwarf2_per_cu_data *per_cu
20429 = dwarf2_per_objfile->all_comp_units[i];
e254ef6a 20430 struct partial_symtab *psymtab = per_cu->v.psymtab;
9291a0cd 20431 gdb_byte val[8];
0a5429f6
DE
20432 struct psymtab_cu_index_map *map;
20433 void **slot;
9291a0cd 20434
95554aad
TT
20435 if (psymtab->user == NULL)
20436 recursively_write_psymbols (objfile, psymtab, symtab, psyms_seen, i);
9291a0cd 20437
0a5429f6
DE
20438 map = &psymtab_cu_index_map[i];
20439 map->psymtab = psymtab;
20440 map->cu_index = i;
20441 slot = htab_find_slot (cu_index_htab, map, INSERT);
20442 gdb_assert (slot != NULL);
20443 gdb_assert (*slot == NULL);
20444 *slot = map;
9291a0cd 20445
b64f50a1
JK
20446 store_unsigned_integer (val, 8, BFD_ENDIAN_LITTLE,
20447 per_cu->offset.sect_off);
9291a0cd 20448 obstack_grow (&cu_list, val, 8);
e254ef6a 20449 store_unsigned_integer (val, 8, BFD_ENDIAN_LITTLE, per_cu->length);
9291a0cd
TT
20450 obstack_grow (&cu_list, val, 8);
20451 }
20452
0a5429f6
DE
20453 /* Dump the address map. */
20454 write_address_map (objfile, &addr_obstack, cu_index_htab);
20455
1fd400ff
TT
20456 /* Write out the .debug_type entries, if any. */
20457 if (dwarf2_per_objfile->signatured_types)
20458 {
20459 struct signatured_type_index_data sig_data;
20460
20461 sig_data.objfile = objfile;
20462 sig_data.symtab = symtab;
20463 sig_data.types_list = &types_cu_list;
987d643c 20464 sig_data.psyms_seen = psyms_seen;
1fd400ff
TT
20465 sig_data.cu_index = dwarf2_per_objfile->n_comp_units;
20466 htab_traverse_noresize (dwarf2_per_objfile->signatured_types,
20467 write_one_signatured_type, &sig_data);
20468 }
20469
156942c7
DE
20470 /* Now that we've processed all symbols we can shrink their cu_indices
20471 lists. */
20472 uniquify_cu_indices (symtab);
20473
9291a0cd
TT
20474 obstack_init (&constant_pool);
20475 make_cleanup_obstack_free (&constant_pool);
20476 obstack_init (&symtab_obstack);
20477 make_cleanup_obstack_free (&symtab_obstack);
20478 write_hash_table (symtab, &symtab_obstack, &constant_pool);
20479
20480 obstack_init (&contents);
20481 make_cleanup_obstack_free (&contents);
1fd400ff 20482 size_of_contents = 6 * sizeof (offset_type);
9291a0cd
TT
20483 total_len = size_of_contents;
20484
20485 /* The version number. */
796a7ff8 20486 val = MAYBE_SWAP (8);
9291a0cd
TT
20487 obstack_grow (&contents, &val, sizeof (val));
20488
20489 /* The offset of the CU list from the start of the file. */
20490 val = MAYBE_SWAP (total_len);
20491 obstack_grow (&contents, &val, sizeof (val));
20492 total_len += obstack_object_size (&cu_list);
20493
1fd400ff
TT
20494 /* The offset of the types CU list from the start of the file. */
20495 val = MAYBE_SWAP (total_len);
20496 obstack_grow (&contents, &val, sizeof (val));
20497 total_len += obstack_object_size (&types_cu_list);
20498
9291a0cd
TT
20499 /* The offset of the address table from the start of the file. */
20500 val = MAYBE_SWAP (total_len);
20501 obstack_grow (&contents, &val, sizeof (val));
20502 total_len += obstack_object_size (&addr_obstack);
20503
20504 /* The offset of the symbol table from the start of the file. */
20505 val = MAYBE_SWAP (total_len);
20506 obstack_grow (&contents, &val, sizeof (val));
20507 total_len += obstack_object_size (&symtab_obstack);
20508
20509 /* The offset of the constant pool from the start of the file. */
20510 val = MAYBE_SWAP (total_len);
20511 obstack_grow (&contents, &val, sizeof (val));
20512 total_len += obstack_object_size (&constant_pool);
20513
20514 gdb_assert (obstack_object_size (&contents) == size_of_contents);
20515
20516 write_obstack (out_file, &contents);
20517 write_obstack (out_file, &cu_list);
1fd400ff 20518 write_obstack (out_file, &types_cu_list);
9291a0cd
TT
20519 write_obstack (out_file, &addr_obstack);
20520 write_obstack (out_file, &symtab_obstack);
20521 write_obstack (out_file, &constant_pool);
20522
20523 fclose (out_file);
20524
20525 /* We want to keep the file, so we set cleanup_filename to NULL
20526 here. See unlink_if_set. */
20527 cleanup_filename = NULL;
20528
20529 do_cleanups (cleanup);
20530}
20531
90476074
TT
20532/* Implementation of the `save gdb-index' command.
20533
20534 Note that the file format used by this command is documented in the
20535 GDB manual. Any changes here must be documented there. */
11570e71 20536
9291a0cd
TT
20537static void
20538save_gdb_index_command (char *arg, int from_tty)
20539{
20540 struct objfile *objfile;
20541
20542 if (!arg || !*arg)
96d19272 20543 error (_("usage: save gdb-index DIRECTORY"));
9291a0cd
TT
20544
20545 ALL_OBJFILES (objfile)
20546 {
20547 struct stat st;
20548
20549 /* If the objfile does not correspond to an actual file, skip it. */
20550 if (stat (objfile->name, &st) < 0)
20551 continue;
20552
20553 dwarf2_per_objfile = objfile_data (objfile, dwarf2_objfile_data_key);
20554 if (dwarf2_per_objfile)
20555 {
20556 volatile struct gdb_exception except;
20557
20558 TRY_CATCH (except, RETURN_MASK_ERROR)
20559 {
20560 write_psymtabs_to_index (objfile, arg);
20561 }
20562 if (except.reason < 0)
20563 exception_fprintf (gdb_stderr, except,
20564 _("Error while writing index for `%s': "),
20565 objfile->name);
20566 }
20567 }
dce234bc
PP
20568}
20569
9291a0cd
TT
20570\f
20571
9eae7c52
TT
20572int dwarf2_always_disassemble;
20573
20574static void
20575show_dwarf2_always_disassemble (struct ui_file *file, int from_tty,
20576 struct cmd_list_element *c, const char *value)
20577{
3e43a32a
MS
20578 fprintf_filtered (file,
20579 _("Whether to always disassemble "
20580 "DWARF expressions is %s.\n"),
9eae7c52
TT
20581 value);
20582}
20583
900e11f9
JK
20584static void
20585show_check_physname (struct ui_file *file, int from_tty,
20586 struct cmd_list_element *c, const char *value)
20587{
20588 fprintf_filtered (file,
20589 _("Whether to check \"physname\" is %s.\n"),
20590 value);
20591}
20592
6502dd73
DJ
20593void _initialize_dwarf2_read (void);
20594
20595void
20596_initialize_dwarf2_read (void)
20597{
96d19272
JK
20598 struct cmd_list_element *c;
20599
dce234bc 20600 dwarf2_objfile_data_key
c1bd65d0 20601 = register_objfile_data_with_cleanup (NULL, dwarf2_per_objfile_free);
ae038cb0 20602
1bedd215
AC
20603 add_prefix_cmd ("dwarf2", class_maintenance, set_dwarf2_cmd, _("\
20604Set DWARF 2 specific variables.\n\
20605Configure DWARF 2 variables such as the cache size"),
ae038cb0
DJ
20606 &set_dwarf2_cmdlist, "maintenance set dwarf2 ",
20607 0/*allow-unknown*/, &maintenance_set_cmdlist);
20608
1bedd215
AC
20609 add_prefix_cmd ("dwarf2", class_maintenance, show_dwarf2_cmd, _("\
20610Show DWARF 2 specific variables\n\
20611Show DWARF 2 variables such as the cache size"),
ae038cb0
DJ
20612 &show_dwarf2_cmdlist, "maintenance show dwarf2 ",
20613 0/*allow-unknown*/, &maintenance_show_cmdlist);
20614
20615 add_setshow_zinteger_cmd ("max-cache-age", class_obscure,
7915a72c
AC
20616 &dwarf2_max_cache_age, _("\
20617Set the upper bound on the age of cached dwarf2 compilation units."), _("\
20618Show the upper bound on the age of cached dwarf2 compilation units."), _("\
20619A higher limit means that cached compilation units will be stored\n\
20620in memory longer, and more total memory will be used. Zero disables\n\
20621caching, which can slow down startup."),
2c5b56ce 20622 NULL,
920d2a44 20623 show_dwarf2_max_cache_age,
2c5b56ce 20624 &set_dwarf2_cmdlist,
ae038cb0 20625 &show_dwarf2_cmdlist);
d97bc12b 20626
9eae7c52
TT
20627 add_setshow_boolean_cmd ("always-disassemble", class_obscure,
20628 &dwarf2_always_disassemble, _("\
20629Set whether `info address' always disassembles DWARF expressions."), _("\
20630Show whether `info address' always disassembles DWARF expressions."), _("\
20631When enabled, DWARF expressions are always printed in an assembly-like\n\
20632syntax. When disabled, expressions will be printed in a more\n\
20633conversational style, when possible."),
20634 NULL,
20635 show_dwarf2_always_disassemble,
20636 &set_dwarf2_cmdlist,
20637 &show_dwarf2_cmdlist);
20638
45cfd468
DE
20639 add_setshow_boolean_cmd ("dwarf2-read", no_class, &dwarf2_read_debug, _("\
20640Set debugging of the dwarf2 reader."), _("\
20641Show debugging of the dwarf2 reader."), _("\
20642When enabled, debugging messages are printed during dwarf2 reading\n\
20643and symtab expansion."),
20644 NULL,
20645 NULL,
20646 &setdebuglist, &showdebuglist);
20647
ccce17b0 20648 add_setshow_zuinteger_cmd ("dwarf2-die", no_class, &dwarf2_die_debug, _("\
d97bc12b
DE
20649Set debugging of the dwarf2 DIE reader."), _("\
20650Show debugging of the dwarf2 DIE reader."), _("\
20651When enabled (non-zero), DIEs are dumped after they are read in.\n\
20652The value is the maximum depth to print."),
ccce17b0
YQ
20653 NULL,
20654 NULL,
20655 &setdebuglist, &showdebuglist);
9291a0cd 20656
900e11f9
JK
20657 add_setshow_boolean_cmd ("check-physname", no_class, &check_physname, _("\
20658Set cross-checking of \"physname\" code against demangler."), _("\
20659Show cross-checking of \"physname\" code against demangler."), _("\
20660When enabled, GDB's internal \"physname\" code is checked against\n\
20661the demangler."),
20662 NULL, show_check_physname,
20663 &setdebuglist, &showdebuglist);
20664
e615022a
DE
20665 add_setshow_boolean_cmd ("use-deprecated-index-sections",
20666 no_class, &use_deprecated_index_sections, _("\
20667Set whether to use deprecated gdb_index sections."), _("\
20668Show whether to use deprecated gdb_index sections."), _("\
20669When enabled, deprecated .gdb_index sections are used anyway.\n\
20670Normally they are ignored either because of a missing feature or\n\
20671performance issue.\n\
20672Warning: This option must be enabled before gdb reads the file."),
20673 NULL,
20674 NULL,
20675 &setlist, &showlist);
20676
96d19272 20677 c = add_cmd ("gdb-index", class_files, save_gdb_index_command,
11570e71 20678 _("\
fc1a9d6e 20679Save a gdb-index file.\n\
11570e71 20680Usage: save gdb-index DIRECTORY"),
96d19272
JK
20681 &save_cmdlist);
20682 set_cmd_completer (c, filename_completer);
6502dd73 20683}
This page took 3.134714 seconds and 4 git commands to generate.