gdb/testsuite
[deliverable/binutils-gdb.git] / gdb / dwarf2read.c
CommitLineData
c906108c 1/* DWARF 2 debugging format support for GDB.
917c78fc 2
0b302171 3 Copyright (C) 1994-2012 Free Software Foundation, Inc.
c906108c
SS
4
5 Adapted by Gary Funck (gary@intrepid.com), Intrepid Technology,
6 Inc. with support from Florida State University (under contract
7 with the Ada Joint Program Office), and Silicon Graphics, Inc.
8 Initial contribution by Brent Benson, Harris Computer Systems, Inc.,
9 based on Fred Fish's (Cygnus Support) implementation of DWARF 1
7ce59000 10 support.
c906108c 11
c5aa993b 12 This file is part of GDB.
c906108c 13
c5aa993b
JM
14 This program is free software; you can redistribute it and/or modify
15 it under the terms of the GNU General Public License as published by
a9762ec7
JB
16 the Free Software Foundation; either version 3 of the License, or
17 (at your option) any later version.
c906108c 18
a9762ec7
JB
19 This program is distributed in the hope that it will be useful,
20 but WITHOUT ANY WARRANTY; without even the implied warranty of
21 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22 GNU General Public License for more details.
c906108c 23
c5aa993b 24 You should have received a copy of the GNU General Public License
a9762ec7 25 along with this program. If not, see <http://www.gnu.org/licenses/>. */
c906108c 26
21b2bd31
DE
27/* FIXME: Various die-reading functions need to be more careful with
28 reading off the end of the section.
29 E.g., load_partial_dies, read_partial_die. */
30
c906108c
SS
31#include "defs.h"
32#include "bfd.h"
80626a55 33#include "elf-bfd.h"
c906108c
SS
34#include "symtab.h"
35#include "gdbtypes.h"
c906108c 36#include "objfiles.h"
fa8f86ff 37#include "dwarf2.h"
c906108c
SS
38#include "buildsym.h"
39#include "demangle.h"
50f182aa 40#include "gdb-demangle.h"
c906108c 41#include "expression.h"
d5166ae1 42#include "filenames.h" /* for DOSish file names */
2e276125 43#include "macrotab.h"
c906108c
SS
44#include "language.h"
45#include "complaints.h"
357e46e7 46#include "bcache.h"
4c2df51b
DJ
47#include "dwarf2expr.h"
48#include "dwarf2loc.h"
9219021c 49#include "cp-support.h"
72bf9492 50#include "hashtab.h"
ae038cb0
DJ
51#include "command.h"
52#include "gdbcmd.h"
edb3359d 53#include "block.h"
ff013f42 54#include "addrmap.h"
94af9270
KS
55#include "typeprint.h"
56#include "jv-lang.h"
ccefe4c4 57#include "psympriv.h"
9291a0cd
TT
58#include "exceptions.h"
59#include "gdb_stat.h"
96d19272 60#include "completer.h"
34eaf542 61#include "vec.h"
98bfdba5 62#include "c-lang.h"
a766d390 63#include "go-lang.h"
98bfdba5 64#include "valprint.h"
3019eac3 65#include "gdbcore.h" /* for gnutarget */
156942c7 66#include "gdb/gdb-index.h"
60d5a603 67#include <ctype.h>
cbb099e8 68#include "gdb_bfd.h"
4357ac6c 69#include "f-lang.h"
4c2df51b 70
c906108c
SS
71#include <fcntl.h>
72#include "gdb_string.h"
4bdf3d34 73#include "gdb_assert.h"
c906108c 74#include <sys/types.h>
d8151005 75
34eaf542
TT
76typedef struct symbol *symbolp;
77DEF_VEC_P (symbolp);
78
45cfd468
DE
79/* When non-zero, print basic high level tracing messages.
80 This is in contrast to the low level DIE reading of dwarf2_die_debug. */
81static int dwarf2_read_debug = 0;
82
d97bc12b 83/* When non-zero, dump DIEs after they are read in. */
ccce17b0 84static unsigned int dwarf2_die_debug = 0;
d97bc12b 85
900e11f9
JK
86/* When non-zero, cross-check physname against demangler. */
87static int check_physname = 0;
88
481860b3 89/* When non-zero, do not reject deprecated .gdb_index sections. */
e615022a 90static int use_deprecated_index_sections = 0;
481860b3 91
df8a16a1
DJ
92/* When set, the file that we're processing is known to have debugging
93 info for C++ namespaces. GCC 3.3.x did not produce this information,
94 but later versions do. */
95
96static int processing_has_namespace_info;
97
6502dd73
DJ
98static const struct objfile_data *dwarf2_objfile_data_key;
99
dce234bc
PP
100struct dwarf2_section_info
101{
102 asection *asection;
103 gdb_byte *buffer;
104 bfd_size_type size;
be391dca
TT
105 /* True if we have tried to read this section. */
106 int readin;
dce234bc
PP
107};
108
8b70b953
TT
109typedef struct dwarf2_section_info dwarf2_section_info_def;
110DEF_VEC_O (dwarf2_section_info_def);
111
9291a0cd
TT
112/* All offsets in the index are of this type. It must be
113 architecture-independent. */
114typedef uint32_t offset_type;
115
116DEF_VEC_I (offset_type);
117
156942c7
DE
118/* Ensure only legit values are used. */
119#define DW2_GDB_INDEX_SYMBOL_STATIC_SET_VALUE(cu_index, value) \
120 do { \
121 gdb_assert ((unsigned int) (value) <= 1); \
122 GDB_INDEX_SYMBOL_STATIC_SET_VALUE((cu_index), (value)); \
123 } while (0)
124
125/* Ensure only legit values are used. */
126#define DW2_GDB_INDEX_SYMBOL_KIND_SET_VALUE(cu_index, value) \
127 do { \
128 gdb_assert ((value) >= GDB_INDEX_SYMBOL_KIND_TYPE \
129 && (value) <= GDB_INDEX_SYMBOL_KIND_OTHER); \
130 GDB_INDEX_SYMBOL_KIND_SET_VALUE((cu_index), (value)); \
131 } while (0)
132
133/* Ensure we don't use more than the alloted nuber of bits for the CU. */
134#define DW2_GDB_INDEX_CU_SET_VALUE(cu_index, value) \
135 do { \
136 gdb_assert (((value) & ~GDB_INDEX_CU_MASK) == 0); \
137 GDB_INDEX_CU_SET_VALUE((cu_index), (value)); \
138 } while (0)
139
9291a0cd
TT
140/* A description of the mapped index. The file format is described in
141 a comment by the code that writes the index. */
142struct mapped_index
143{
559a7a62
JK
144 /* Index data format version. */
145 int version;
146
9291a0cd
TT
147 /* The total length of the buffer. */
148 off_t total_size;
b11b1f88 149
9291a0cd
TT
150 /* A pointer to the address table data. */
151 const gdb_byte *address_table;
b11b1f88 152
9291a0cd
TT
153 /* Size of the address table data in bytes. */
154 offset_type address_table_size;
b11b1f88 155
3876f04e
DE
156 /* The symbol table, implemented as a hash table. */
157 const offset_type *symbol_table;
b11b1f88 158
9291a0cd 159 /* Size in slots, each slot is 2 offset_types. */
3876f04e 160 offset_type symbol_table_slots;
b11b1f88 161
9291a0cd
TT
162 /* A pointer to the constant pool. */
163 const char *constant_pool;
164};
165
95554aad
TT
166typedef struct dwarf2_per_cu_data *dwarf2_per_cu_ptr;
167DEF_VEC_P (dwarf2_per_cu_ptr);
168
9cdd5dbd
DE
169/* Collection of data recorded per objfile.
170 This hangs off of dwarf2_objfile_data_key. */
171
6502dd73
DJ
172struct dwarf2_per_objfile
173{
dce234bc
PP
174 struct dwarf2_section_info info;
175 struct dwarf2_section_info abbrev;
176 struct dwarf2_section_info line;
dce234bc
PP
177 struct dwarf2_section_info loc;
178 struct dwarf2_section_info macinfo;
cf2c3c16 179 struct dwarf2_section_info macro;
dce234bc
PP
180 struct dwarf2_section_info str;
181 struct dwarf2_section_info ranges;
3019eac3 182 struct dwarf2_section_info addr;
dce234bc
PP
183 struct dwarf2_section_info frame;
184 struct dwarf2_section_info eh_frame;
9291a0cd 185 struct dwarf2_section_info gdb_index;
ae038cb0 186
8b70b953
TT
187 VEC (dwarf2_section_info_def) *types;
188
be391dca
TT
189 /* Back link. */
190 struct objfile *objfile;
191
d467dd73 192 /* Table of all the compilation units. This is used to locate
10b3939b 193 the target compilation unit of a particular reference. */
ae038cb0
DJ
194 struct dwarf2_per_cu_data **all_comp_units;
195
196 /* The number of compilation units in ALL_COMP_UNITS. */
197 int n_comp_units;
198
1fd400ff 199 /* The number of .debug_types-related CUs. */
d467dd73 200 int n_type_units;
1fd400ff 201
d467dd73 202 /* The .debug_types-related CUs (TUs). */
b4dd5633 203 struct signatured_type **all_type_units;
1fd400ff 204
f4dc4d17
DE
205 /* The number of entries in all_type_unit_groups. */
206 int n_type_unit_groups;
207
208 /* Table of type unit groups.
209 This exists to make it easy to iterate over all CUs and TU groups. */
210 struct type_unit_group **all_type_unit_groups;
211
212 /* Table of struct type_unit_group objects.
213 The hash key is the DW_AT_stmt_list value. */
214 htab_t type_unit_groups;
72dca2f5 215
348e048f
DE
216 /* A table mapping .debug_types signatures to its signatured_type entry.
217 This is NULL if the .debug_types section hasn't been read in yet. */
218 htab_t signatured_types;
219
f4dc4d17
DE
220 /* Type unit statistics, to see how well the scaling improvements
221 are doing. */
222 struct tu_stats
223 {
224 int nr_uniq_abbrev_tables;
225 int nr_symtabs;
226 int nr_symtab_sharers;
227 int nr_stmt_less_type_units;
228 } tu_stats;
229
230 /* A chain of compilation units that are currently read in, so that
231 they can be freed later. */
232 struct dwarf2_per_cu_data *read_in_chain;
233
3019eac3
DE
234 /* A table mapping DW_AT_dwo_name values to struct dwo_file objects.
235 This is NULL if the table hasn't been allocated yet. */
236 htab_t dwo_files;
237
80626a55
DE
238 /* Non-zero if we've check for whether there is a DWP file. */
239 int dwp_checked;
240
241 /* The DWP file if there is one, or NULL. */
242 struct dwp_file *dwp_file;
243
36586728
TT
244 /* The shared '.dwz' file, if one exists. This is used when the
245 original data was compressed using 'dwz -m'. */
246 struct dwz_file *dwz_file;
247
72dca2f5
FR
248 /* A flag indicating wether this objfile has a section loaded at a
249 VMA of 0. */
250 int has_section_at_zero;
9291a0cd 251
ae2de4f8
DE
252 /* True if we are using the mapped index,
253 or we are faking it for OBJF_READNOW's sake. */
9291a0cd
TT
254 unsigned char using_index;
255
ae2de4f8 256 /* The mapped index, or NULL if .gdb_index is missing or not being used. */
9291a0cd 257 struct mapped_index *index_table;
98bfdba5 258
7b9f3c50 259 /* When using index_table, this keeps track of all quick_file_names entries.
56e64610
DE
260 TUs typically share line table entries with a CU, so we maintain a
261 separate table of all line table entries to support the sharing.
262 Note that while there can be way more TUs than CUs, we've already
263 sorted all the TUs into "type unit groups", grouped by their
264 DW_AT_stmt_list value. Therefore the only sharing done here is with a
265 CU and its associated TU group if there is one. */
7b9f3c50
DE
266 htab_t quick_file_names_table;
267
98bfdba5
PA
268 /* Set during partial symbol reading, to prevent queueing of full
269 symbols. */
270 int reading_partial_symbols;
673bfd45 271
dee91e82 272 /* Table mapping type DIEs to their struct type *.
673bfd45 273 This is NULL if not allocated yet.
dee91e82
DE
274 The mapping is done via (CU/TU signature + DIE offset) -> type. */
275 htab_t die_type_hash;
95554aad
TT
276
277 /* The CUs we recently read. */
278 VEC (dwarf2_per_cu_ptr) *just_read_cus;
6502dd73
DJ
279};
280
281static struct dwarf2_per_objfile *dwarf2_per_objfile;
c906108c 282
251d32d9 283/* Default names of the debugging sections. */
c906108c 284
233a11ab
CS
285/* Note that if the debugging section has been compressed, it might
286 have a name like .zdebug_info. */
287
9cdd5dbd
DE
288static const struct dwarf2_debug_sections dwarf2_elf_names =
289{
251d32d9
TG
290 { ".debug_info", ".zdebug_info" },
291 { ".debug_abbrev", ".zdebug_abbrev" },
292 { ".debug_line", ".zdebug_line" },
293 { ".debug_loc", ".zdebug_loc" },
294 { ".debug_macinfo", ".zdebug_macinfo" },
cf2c3c16 295 { ".debug_macro", ".zdebug_macro" },
251d32d9
TG
296 { ".debug_str", ".zdebug_str" },
297 { ".debug_ranges", ".zdebug_ranges" },
298 { ".debug_types", ".zdebug_types" },
3019eac3 299 { ".debug_addr", ".zdebug_addr" },
251d32d9
TG
300 { ".debug_frame", ".zdebug_frame" },
301 { ".eh_frame", NULL },
24d3216f
TT
302 { ".gdb_index", ".zgdb_index" },
303 23
251d32d9 304};
c906108c 305
80626a55 306/* List of DWO/DWP sections. */
3019eac3 307
80626a55 308static const struct dwop_section_names
3019eac3
DE
309{
310 struct dwarf2_section_names abbrev_dwo;
311 struct dwarf2_section_names info_dwo;
312 struct dwarf2_section_names line_dwo;
313 struct dwarf2_section_names loc_dwo;
09262596
DE
314 struct dwarf2_section_names macinfo_dwo;
315 struct dwarf2_section_names macro_dwo;
3019eac3
DE
316 struct dwarf2_section_names str_dwo;
317 struct dwarf2_section_names str_offsets_dwo;
318 struct dwarf2_section_names types_dwo;
80626a55
DE
319 struct dwarf2_section_names cu_index;
320 struct dwarf2_section_names tu_index;
3019eac3 321}
80626a55 322dwop_section_names =
3019eac3
DE
323{
324 { ".debug_abbrev.dwo", ".zdebug_abbrev.dwo" },
325 { ".debug_info.dwo", ".zdebug_info.dwo" },
326 { ".debug_line.dwo", ".zdebug_line.dwo" },
327 { ".debug_loc.dwo", ".zdebug_loc.dwo" },
09262596
DE
328 { ".debug_macinfo.dwo", ".zdebug_macinfo.dwo" },
329 { ".debug_macro.dwo", ".zdebug_macro.dwo" },
3019eac3
DE
330 { ".debug_str.dwo", ".zdebug_str.dwo" },
331 { ".debug_str_offsets.dwo", ".zdebug_str_offsets.dwo" },
332 { ".debug_types.dwo", ".zdebug_types.dwo" },
80626a55
DE
333 { ".debug_cu_index", ".zdebug_cu_index" },
334 { ".debug_tu_index", ".zdebug_tu_index" },
3019eac3
DE
335};
336
c906108c
SS
337/* local data types */
338
107d2387
AC
339/* The data in a compilation unit header, after target2host
340 translation, looks like this. */
c906108c 341struct comp_unit_head
a738430d 342{
c764a876 343 unsigned int length;
a738430d 344 short version;
a738430d
MK
345 unsigned char addr_size;
346 unsigned char signed_addr_p;
b64f50a1 347 sect_offset abbrev_offset;
57349743 348
a738430d
MK
349 /* Size of file offsets; either 4 or 8. */
350 unsigned int offset_size;
57349743 351
a738430d
MK
352 /* Size of the length field; either 4 or 12. */
353 unsigned int initial_length_size;
57349743 354
a738430d
MK
355 /* Offset to the first byte of this compilation unit header in the
356 .debug_info section, for resolving relative reference dies. */
b64f50a1 357 sect_offset offset;
57349743 358
d00adf39
DE
359 /* Offset to first die in this cu from the start of the cu.
360 This will be the first byte following the compilation unit header. */
b64f50a1 361 cu_offset first_die_offset;
a738430d 362};
c906108c 363
3da10d80
KS
364/* Type used for delaying computation of method physnames.
365 See comments for compute_delayed_physnames. */
366struct delayed_method_info
367{
368 /* The type to which the method is attached, i.e., its parent class. */
369 struct type *type;
370
371 /* The index of the method in the type's function fieldlists. */
372 int fnfield_index;
373
374 /* The index of the method in the fieldlist. */
375 int index;
376
377 /* The name of the DIE. */
378 const char *name;
379
380 /* The DIE associated with this method. */
381 struct die_info *die;
382};
383
384typedef struct delayed_method_info delayed_method_info;
385DEF_VEC_O (delayed_method_info);
386
e7c27a73
DJ
387/* Internal state when decoding a particular compilation unit. */
388struct dwarf2_cu
389{
390 /* The objfile containing this compilation unit. */
391 struct objfile *objfile;
392
d00adf39 393 /* The header of the compilation unit. */
e7c27a73 394 struct comp_unit_head header;
e142c38c 395
d00adf39
DE
396 /* Base address of this compilation unit. */
397 CORE_ADDR base_address;
398
399 /* Non-zero if base_address has been set. */
400 int base_known;
401
e142c38c
DJ
402 /* The language we are debugging. */
403 enum language language;
404 const struct language_defn *language_defn;
405
b0f35d58
DL
406 const char *producer;
407
e142c38c
DJ
408 /* The generic symbol table building routines have separate lists for
409 file scope symbols and all all other scopes (local scopes). So
410 we need to select the right one to pass to add_symbol_to_list().
411 We do it by keeping a pointer to the correct list in list_in_scope.
412
413 FIXME: The original dwarf code just treated the file scope as the
414 first local scope, and all other local scopes as nested local
415 scopes, and worked fine. Check to see if we really need to
416 distinguish these in buildsym.c. */
417 struct pending **list_in_scope;
418
433df2d4
DE
419 /* The abbrev table for this CU.
420 Normally this points to the abbrev table in the objfile.
421 But if DWO_UNIT is non-NULL this is the abbrev table in the DWO file. */
422 struct abbrev_table *abbrev_table;
72bf9492 423
b64f50a1
JK
424 /* Hash table holding all the loaded partial DIEs
425 with partial_die->offset.SECT_OFF as hash. */
72bf9492
DJ
426 htab_t partial_dies;
427
428 /* Storage for things with the same lifetime as this read-in compilation
429 unit, including partial DIEs. */
430 struct obstack comp_unit_obstack;
431
ae038cb0
DJ
432 /* When multiple dwarf2_cu structures are living in memory, this field
433 chains them all together, so that they can be released efficiently.
434 We will probably also want a generation counter so that most-recently-used
435 compilation units are cached... */
436 struct dwarf2_per_cu_data *read_in_chain;
437
438 /* Backchain to our per_cu entry if the tree has been built. */
439 struct dwarf2_per_cu_data *per_cu;
440
441 /* How many compilation units ago was this CU last referenced? */
442 int last_used;
443
b64f50a1
JK
444 /* A hash table of DIE cu_offset for following references with
445 die_info->offset.sect_off as hash. */
51545339 446 htab_t die_hash;
10b3939b
DJ
447
448 /* Full DIEs if read in. */
449 struct die_info *dies;
450
451 /* A set of pointers to dwarf2_per_cu_data objects for compilation
452 units referenced by this one. Only set during full symbol processing;
453 partial symbol tables do not have dependencies. */
454 htab_t dependencies;
455
cb1df416
DJ
456 /* Header data from the line table, during full symbol processing. */
457 struct line_header *line_header;
458
3da10d80
KS
459 /* A list of methods which need to have physnames computed
460 after all type information has been read. */
461 VEC (delayed_method_info) *method_list;
462
96408a79
SA
463 /* To be copied to symtab->call_site_htab. */
464 htab_t call_site_htab;
465
034e5797
DE
466 /* Non-NULL if this CU came from a DWO file.
467 There is an invariant here that is important to remember:
468 Except for attributes copied from the top level DIE in the "main"
469 (or "stub") file in preparation for reading the DWO file
470 (e.g., DW_AT_GNU_addr_base), we KISS: there is only *one* CU.
471 Either there isn't a DWO file (in which case this is NULL and the point
472 is moot), or there is and either we're not going to read it (in which
473 case this is NULL) or there is and we are reading it (in which case this
474 is non-NULL). */
3019eac3
DE
475 struct dwo_unit *dwo_unit;
476
477 /* The DW_AT_addr_base attribute if present, zero otherwise
478 (zero is a valid value though).
479 Note this value comes from the stub CU/TU's DIE. */
480 ULONGEST addr_base;
481
2e3cf129
DE
482 /* The DW_AT_ranges_base attribute if present, zero otherwise
483 (zero is a valid value though).
484 Note this value comes from the stub CU/TU's DIE.
485 Also note that the value is zero in the non-DWO case so this value can
486 be used without needing to know whether DWO files are in use or not. */
487 ULONGEST ranges_base;
488
ae038cb0
DJ
489 /* Mark used when releasing cached dies. */
490 unsigned int mark : 1;
491
8be455d7
JK
492 /* This CU references .debug_loc. See the symtab->locations_valid field.
493 This test is imperfect as there may exist optimized debug code not using
494 any location list and still facing inlining issues if handled as
495 unoptimized code. For a future better test see GCC PR other/32998. */
8be455d7 496 unsigned int has_loclist : 1;
ba919b58 497
685b1105
JK
498 /* These cache the results for producer_is_gxx_lt_4_6 and producer_is_icc.
499 CHECKED_PRODUCER is set if both PRODUCER_IS_GXX_LT_4_6 and PRODUCER_IS_ICC
500 are valid. This information is cached because profiling CU expansion
501 showed excessive time spent in producer_is_gxx_lt_4_6. */
ba919b58
TT
502 unsigned int checked_producer : 1;
503 unsigned int producer_is_gxx_lt_4_6 : 1;
685b1105 504 unsigned int producer_is_icc : 1;
e7c27a73
DJ
505};
506
10b3939b
DJ
507/* Persistent data held for a compilation unit, even when not
508 processing it. We put a pointer to this structure in the
28dee7f5 509 read_symtab_private field of the psymtab. */
10b3939b 510
ae038cb0
DJ
511struct dwarf2_per_cu_data
512{
36586728 513 /* The start offset and length of this compilation unit.
45452591 514 NOTE: Unlike comp_unit_head.length, this length includes
3019eac3
DE
515 initial_length_size.
516 If the DIE refers to a DWO file, this is always of the original die,
517 not the DWO file. */
b64f50a1 518 sect_offset offset;
36586728 519 unsigned int length;
ae038cb0
DJ
520
521 /* Flag indicating this compilation unit will be read in before
522 any of the current compilation units are processed. */
c764a876 523 unsigned int queued : 1;
ae038cb0 524
0d99eb77
DE
525 /* This flag will be set when reading partial DIEs if we need to load
526 absolutely all DIEs for this compilation unit, instead of just the ones
527 we think are interesting. It gets set if we look for a DIE in the
5afb4e99
DJ
528 hash table and don't find it. */
529 unsigned int load_all_dies : 1;
530
3019eac3
DE
531 /* Non-zero if this CU is from .debug_types. */
532 unsigned int is_debug_types : 1;
533
36586728
TT
534 /* Non-zero if this CU is from the .dwz file. */
535 unsigned int is_dwz : 1;
536
3019eac3
DE
537 /* The section this CU/TU lives in.
538 If the DIE refers to a DWO file, this is always the original die,
539 not the DWO file. */
540 struct dwarf2_section_info *info_or_types_section;
348e048f 541
17ea53c3
JK
542 /* Set to non-NULL iff this CU is currently loaded. When it gets freed out
543 of the CU cache it gets reset to NULL again. */
ae038cb0 544 struct dwarf2_cu *cu;
1c379e20 545
9cdd5dbd
DE
546 /* The corresponding objfile.
547 Normally we can get the objfile from dwarf2_per_objfile.
548 However we can enter this file with just a "per_cu" handle. */
9291a0cd
TT
549 struct objfile *objfile;
550
551 /* When using partial symbol tables, the 'psymtab' field is active.
552 Otherwise the 'quick' field is active. */
553 union
554 {
555 /* The partial symbol table associated with this compilation unit,
95554aad 556 or NULL for unread partial units. */
9291a0cd
TT
557 struct partial_symtab *psymtab;
558
559 /* Data needed by the "quick" functions. */
560 struct dwarf2_per_cu_quick_data *quick;
561 } v;
95554aad 562
f4dc4d17
DE
563 union
564 {
565 /* The CUs we import using DW_TAG_imported_unit. This is filled in
566 while reading psymtabs, used to compute the psymtab dependencies,
567 and then cleared. Then it is filled in again while reading full
568 symbols, and only deleted when the objfile is destroyed. */
569 VEC (dwarf2_per_cu_ptr) *imported_symtabs;
570
571 /* Type units are grouped by their DW_AT_stmt_list entry so that they
572 can share them. If this is a TU, this points to the containing
573 symtab. */
574 struct type_unit_group *type_unit_group;
575 } s;
ae038cb0
DJ
576};
577
348e048f
DE
578/* Entry in the signatured_types hash table. */
579
580struct signatured_type
581{
42e7ad6c
DE
582 /* The "per_cu" object of this type.
583 N.B.: This is the first member so that it's easy to convert pointers
584 between them. */
585 struct dwarf2_per_cu_data per_cu;
586
3019eac3 587 /* The type's signature. */
348e048f
DE
588 ULONGEST signature;
589
3019eac3
DE
590 /* Offset in the TU of the type's DIE, as read from the TU header.
591 If the definition lives in a DWO file, this value is unusable. */
592 cu_offset type_offset_in_tu;
593
594 /* Offset in the section of the type's DIE.
595 If the definition lives in a DWO file, this is the offset in the
596 .debug_types.dwo section.
597 The value is zero until the actual value is known.
598 Zero is otherwise not a valid section offset. */
599 sect_offset type_offset_in_section;
348e048f
DE
600};
601
094b34ac
DE
602/* A struct that can be used as a hash key for tables based on DW_AT_stmt_list.
603 This includes type_unit_group and quick_file_names. */
604
605struct stmt_list_hash
606{
607 /* The DWO unit this table is from or NULL if there is none. */
608 struct dwo_unit *dwo_unit;
609
610 /* Offset in .debug_line or .debug_line.dwo. */
611 sect_offset line_offset;
612};
613
f4dc4d17
DE
614/* Each element of dwarf2_per_objfile->type_unit_groups is a pointer to
615 an object of this type. */
616
617struct type_unit_group
618{
619 /* dwarf2read.c's main "handle" on the symtab.
620 To simplify things we create an artificial CU that "includes" all the
621 type units using this stmt_list so that the rest of the code still has
622 a "per_cu" handle on the symtab.
623 This PER_CU is recognized by having no section. */
624#define IS_TYPE_UNIT_GROUP(per_cu) ((per_cu)->info_or_types_section == NULL)
094b34ac
DE
625 struct dwarf2_per_cu_data per_cu;
626
627 union
628 {
629 /* The TUs that share this DW_AT_stmt_list entry.
630 This is added to while parsing type units to build partial symtabs,
631 and is deleted afterwards and not used again. */
632 VEC (dwarf2_per_cu_ptr) *tus;
f4dc4d17 633
094b34ac
DE
634 /* When reading the line table in "quick" functions, we need a real TU.
635 Any will do, we know they all share the same DW_AT_stmt_list entry.
636 For simplicity's sake, we pick the first one. */
637 struct dwarf2_per_cu_data *first_tu;
638 } t;
f4dc4d17
DE
639
640 /* The primary symtab.
094b34ac
DE
641 Type units in a group needn't all be defined in the same source file,
642 so we create an essentially anonymous symtab as the primary symtab. */
f4dc4d17
DE
643 struct symtab *primary_symtab;
644
094b34ac
DE
645 /* The data used to construct the hash key. */
646 struct stmt_list_hash hash;
f4dc4d17
DE
647
648 /* The number of symtabs from the line header.
649 The value here must match line_header.num_file_names. */
650 unsigned int num_symtabs;
651
652 /* The symbol tables for this TU (obtained from the files listed in
653 DW_AT_stmt_list).
654 WARNING: The order of entries here must match the order of entries
655 in the line header. After the first TU using this type_unit_group, the
656 line header for the subsequent TUs is recreated from this. This is done
657 because we need to use the same symtabs for each TU using the same
658 DW_AT_stmt_list value. Also note that symtabs may be repeated here,
659 there's no guarantee the line header doesn't have duplicate entries. */
660 struct symtab **symtabs;
661};
662
80626a55 663/* These sections are what may appear in a DWO file. */
3019eac3
DE
664
665struct dwo_sections
666{
667 struct dwarf2_section_info abbrev;
3019eac3
DE
668 struct dwarf2_section_info line;
669 struct dwarf2_section_info loc;
09262596
DE
670 struct dwarf2_section_info macinfo;
671 struct dwarf2_section_info macro;
3019eac3
DE
672 struct dwarf2_section_info str;
673 struct dwarf2_section_info str_offsets;
80626a55
DE
674 /* In the case of a virtual DWO file, these two are unused. */
675 struct dwarf2_section_info info;
3019eac3
DE
676 VEC (dwarf2_section_info_def) *types;
677};
678
679/* Common bits of DWO CUs/TUs. */
680
681struct dwo_unit
682{
683 /* Backlink to the containing struct dwo_file. */
684 struct dwo_file *dwo_file;
685
686 /* The "id" that distinguishes this CU/TU.
687 .debug_info calls this "dwo_id", .debug_types calls this "signature".
688 Since signatures came first, we stick with it for consistency. */
689 ULONGEST signature;
690
691 /* The section this CU/TU lives in, in the DWO file. */
692 struct dwarf2_section_info *info_or_types_section;
693
694 /* Same as dwarf2_per_cu_data:{offset,length} but for the DWO section. */
695 sect_offset offset;
696 unsigned int length;
697
698 /* For types, offset in the type's DIE of the type defined by this TU. */
699 cu_offset type_offset_in_tu;
700};
701
80626a55
DE
702/* Data for one DWO file.
703 This includes virtual DWO files that have been packaged into a
704 DWP file. */
3019eac3
DE
705
706struct dwo_file
707{
80626a55
DE
708 /* The DW_AT_GNU_dwo_name attribute. This is the hash key.
709 For virtual DWO files the name is constructed from the section offsets
710 of abbrev,line,loc,str_offsets so that we combine virtual DWO files
711 from related CU+TUs. */
712 const char *name;
3019eac3 713
80626a55
DE
714 /* The bfd, when the file is open. Otherwise this is NULL.
715 This is unused(NULL) for virtual DWO files where we use dwp_file.dbfd. */
716 bfd *dbfd;
3019eac3
DE
717
718 /* Section info for this file. */
719 struct dwo_sections sections;
720
721 /* Table of CUs in the file.
722 Each element is a struct dwo_unit. */
723 htab_t cus;
724
725 /* Table of TUs in the file.
726 Each element is a struct dwo_unit. */
727 htab_t tus;
728};
729
80626a55
DE
730/* These sections are what may appear in a DWP file. */
731
732struct dwp_sections
733{
734 struct dwarf2_section_info str;
735 struct dwarf2_section_info cu_index;
736 struct dwarf2_section_info tu_index;
737 /* The .debug_info.dwo, .debug_types.dwo, and other sections are referenced
738 by section number. We don't need to record them here. */
739};
740
741/* These sections are what may appear in a virtual DWO file. */
742
743struct virtual_dwo_sections
744{
745 struct dwarf2_section_info abbrev;
746 struct dwarf2_section_info line;
747 struct dwarf2_section_info loc;
748 struct dwarf2_section_info macinfo;
749 struct dwarf2_section_info macro;
750 struct dwarf2_section_info str_offsets;
751 /* Each DWP hash table entry records one CU or one TU.
752 That is recorded here, and copied to dwo_unit.info_or_types_section. */
753 struct dwarf2_section_info info_or_types;
754};
755
756/* Contents of DWP hash tables. */
757
758struct dwp_hash_table
759{
760 uint32_t nr_units, nr_slots;
761 const gdb_byte *hash_table, *unit_table, *section_pool;
762};
763
764/* Data for one DWP file. */
765
766struct dwp_file
767{
768 /* Name of the file. */
769 const char *name;
770
771 /* The bfd, when the file is open. Otherwise this is NULL. */
772 bfd *dbfd;
773
774 /* Section info for this file. */
775 struct dwp_sections sections;
776
777 /* Table of CUs in the file. */
778 const struct dwp_hash_table *cus;
779
780 /* Table of TUs in the file. */
781 const struct dwp_hash_table *tus;
782
783 /* Table of loaded CUs/TUs. Each entry is a struct dwo_unit *. */
784 htab_t loaded_cutus;
785
786 /* Table to map ELF section numbers to their sections. */
787 unsigned int num_sections;
788 asection **elf_sections;
789};
790
36586728
TT
791/* This represents a '.dwz' file. */
792
793struct dwz_file
794{
795 /* A dwz file can only contain a few sections. */
796 struct dwarf2_section_info abbrev;
797 struct dwarf2_section_info info;
798 struct dwarf2_section_info str;
799 struct dwarf2_section_info line;
800 struct dwarf2_section_info macro;
2ec9a5e0 801 struct dwarf2_section_info gdb_index;
36586728
TT
802
803 /* The dwz's BFD. */
804 bfd *dwz_bfd;
805};
806
0963b4bd
MS
807/* Struct used to pass misc. parameters to read_die_and_children, et
808 al. which are used for both .debug_info and .debug_types dies.
809 All parameters here are unchanging for the life of the call. This
dee91e82 810 struct exists to abstract away the constant parameters of die reading. */
93311388
DE
811
812struct die_reader_specs
813{
dee91e82 814 /* die_section->asection->owner. */
93311388
DE
815 bfd* abfd;
816
817 /* The CU of the DIE we are parsing. */
818 struct dwarf2_cu *cu;
819
80626a55 820 /* Non-NULL if reading a DWO file (including one packaged into a DWP). */
3019eac3
DE
821 struct dwo_file *dwo_file;
822
dee91e82 823 /* The section the die comes from.
3019eac3 824 This is either .debug_info or .debug_types, or the .dwo variants. */
dee91e82
DE
825 struct dwarf2_section_info *die_section;
826
827 /* die_section->buffer. */
828 gdb_byte *buffer;
f664829e
DE
829
830 /* The end of the buffer. */
831 const gdb_byte *buffer_end;
93311388
DE
832};
833
fd820528 834/* Type of function passed to init_cutu_and_read_dies, et.al. */
dee91e82
DE
835typedef void (die_reader_func_ftype) (const struct die_reader_specs *reader,
836 gdb_byte *info_ptr,
837 struct die_info *comp_unit_die,
838 int has_children,
839 void *data);
840
debd256d
JB
841/* The line number information for a compilation unit (found in the
842 .debug_line section) begins with a "statement program header",
843 which contains the following information. */
844struct line_header
845{
846 unsigned int total_length;
847 unsigned short version;
848 unsigned int header_length;
849 unsigned char minimum_instruction_length;
2dc7f7b3 850 unsigned char maximum_ops_per_instruction;
debd256d
JB
851 unsigned char default_is_stmt;
852 int line_base;
853 unsigned char line_range;
854 unsigned char opcode_base;
855
856 /* standard_opcode_lengths[i] is the number of operands for the
857 standard opcode whose value is i. This means that
858 standard_opcode_lengths[0] is unused, and the last meaningful
859 element is standard_opcode_lengths[opcode_base - 1]. */
860 unsigned char *standard_opcode_lengths;
861
862 /* The include_directories table. NOTE! These strings are not
863 allocated with xmalloc; instead, they are pointers into
864 debug_line_buffer. If you try to free them, `free' will get
865 indigestion. */
866 unsigned int num_include_dirs, include_dirs_size;
867 char **include_dirs;
868
869 /* The file_names table. NOTE! These strings are not allocated
870 with xmalloc; instead, they are pointers into debug_line_buffer.
871 Don't try to free them directly. */
872 unsigned int num_file_names, file_names_size;
873 struct file_entry
c906108c 874 {
debd256d
JB
875 char *name;
876 unsigned int dir_index;
877 unsigned int mod_time;
878 unsigned int length;
aaa75496 879 int included_p; /* Non-zero if referenced by the Line Number Program. */
cb1df416 880 struct symtab *symtab; /* The associated symbol table, if any. */
debd256d
JB
881 } *file_names;
882
883 /* The start and end of the statement program following this
6502dd73 884 header. These point into dwarf2_per_objfile->line_buffer. */
fe1b8b76 885 gdb_byte *statement_program_start, *statement_program_end;
debd256d 886};
c906108c
SS
887
888/* When we construct a partial symbol table entry we only
0963b4bd 889 need this much information. */
c906108c
SS
890struct partial_die_info
891 {
72bf9492 892 /* Offset of this DIE. */
b64f50a1 893 sect_offset offset;
72bf9492
DJ
894
895 /* DWARF-2 tag for this DIE. */
896 ENUM_BITFIELD(dwarf_tag) tag : 16;
897
72bf9492
DJ
898 /* Assorted flags describing the data found in this DIE. */
899 unsigned int has_children : 1;
900 unsigned int is_external : 1;
901 unsigned int is_declaration : 1;
902 unsigned int has_type : 1;
903 unsigned int has_specification : 1;
904 unsigned int has_pc_info : 1;
481860b3 905 unsigned int may_be_inlined : 1;
72bf9492
DJ
906
907 /* Flag set if the SCOPE field of this structure has been
908 computed. */
909 unsigned int scope_set : 1;
910
fa4028e9
JB
911 /* Flag set if the DIE has a byte_size attribute. */
912 unsigned int has_byte_size : 1;
913
98bfdba5
PA
914 /* Flag set if any of the DIE's children are template arguments. */
915 unsigned int has_template_arguments : 1;
916
abc72ce4
DE
917 /* Flag set if fixup_partial_die has been called on this die. */
918 unsigned int fixup_called : 1;
919
36586728
TT
920 /* Flag set if DW_TAG_imported_unit uses DW_FORM_GNU_ref_alt. */
921 unsigned int is_dwz : 1;
922
923 /* Flag set if spec_offset uses DW_FORM_GNU_ref_alt. */
924 unsigned int spec_is_dwz : 1;
925
72bf9492 926 /* The name of this DIE. Normally the value of DW_AT_name, but
94af9270 927 sometimes a default name for unnamed DIEs. */
c906108c 928 char *name;
72bf9492 929
abc72ce4
DE
930 /* The linkage name, if present. */
931 const char *linkage_name;
932
72bf9492
DJ
933 /* The scope to prepend to our children. This is generally
934 allocated on the comp_unit_obstack, so will disappear
935 when this compilation unit leaves the cache. */
936 char *scope;
937
95554aad
TT
938 /* Some data associated with the partial DIE. The tag determines
939 which field is live. */
940 union
941 {
942 /* The location description associated with this DIE, if any. */
943 struct dwarf_block *locdesc;
944 /* The offset of an import, for DW_TAG_imported_unit. */
945 sect_offset offset;
946 } d;
72bf9492
DJ
947
948 /* If HAS_PC_INFO, the PC range associated with this DIE. */
c906108c
SS
949 CORE_ADDR lowpc;
950 CORE_ADDR highpc;
72bf9492 951
93311388 952 /* Pointer into the info_buffer (or types_buffer) pointing at the target of
72bf9492 953 DW_AT_sibling, if any. */
abc72ce4
DE
954 /* NOTE: This member isn't strictly necessary, read_partial_die could
955 return DW_AT_sibling values to its caller load_partial_dies. */
fe1b8b76 956 gdb_byte *sibling;
72bf9492
DJ
957
958 /* If HAS_SPECIFICATION, the offset of the DIE referred to by
959 DW_AT_specification (or DW_AT_abstract_origin or
960 DW_AT_extension). */
b64f50a1 961 sect_offset spec_offset;
72bf9492
DJ
962
963 /* Pointers to this DIE's parent, first child, and next sibling,
964 if any. */
965 struct partial_die_info *die_parent, *die_child, *die_sibling;
c906108c
SS
966 };
967
0963b4bd 968/* This data structure holds the information of an abbrev. */
c906108c
SS
969struct abbrev_info
970 {
971 unsigned int number; /* number identifying abbrev */
972 enum dwarf_tag tag; /* dwarf tag */
f3dd6933
DJ
973 unsigned short has_children; /* boolean */
974 unsigned short num_attrs; /* number of attributes */
c906108c
SS
975 struct attr_abbrev *attrs; /* an array of attribute descriptions */
976 struct abbrev_info *next; /* next in chain */
977 };
978
979struct attr_abbrev
980 {
9d25dd43
DE
981 ENUM_BITFIELD(dwarf_attribute) name : 16;
982 ENUM_BITFIELD(dwarf_form) form : 16;
c906108c
SS
983 };
984
433df2d4
DE
985/* Size of abbrev_table.abbrev_hash_table. */
986#define ABBREV_HASH_SIZE 121
987
988/* Top level data structure to contain an abbreviation table. */
989
990struct abbrev_table
991{
f4dc4d17
DE
992 /* Where the abbrev table came from.
993 This is used as a sanity check when the table is used. */
433df2d4
DE
994 sect_offset offset;
995
996 /* Storage for the abbrev table. */
997 struct obstack abbrev_obstack;
998
999 /* Hash table of abbrevs.
1000 This is an array of size ABBREV_HASH_SIZE allocated in abbrev_obstack.
1001 It could be statically allocated, but the previous code didn't so we
1002 don't either. */
1003 struct abbrev_info **abbrevs;
1004};
1005
0963b4bd 1006/* Attributes have a name and a value. */
b60c80d6
DJ
1007struct attribute
1008 {
9d25dd43 1009 ENUM_BITFIELD(dwarf_attribute) name : 16;
8285870a
JK
1010 ENUM_BITFIELD(dwarf_form) form : 15;
1011
1012 /* Has DW_STRING already been updated by dwarf2_canonicalize_name? This
1013 field should be in u.str (existing only for DW_STRING) but it is kept
1014 here for better struct attribute alignment. */
1015 unsigned int string_is_canonical : 1;
1016
b60c80d6
DJ
1017 union
1018 {
1019 char *str;
1020 struct dwarf_block *blk;
43bbcdc2
PH
1021 ULONGEST unsnd;
1022 LONGEST snd;
b60c80d6 1023 CORE_ADDR addr;
348e048f 1024 struct signatured_type *signatured_type;
b60c80d6
DJ
1025 }
1026 u;
1027 };
1028
0963b4bd 1029/* This data structure holds a complete die structure. */
c906108c
SS
1030struct die_info
1031 {
76815b17
DE
1032 /* DWARF-2 tag for this DIE. */
1033 ENUM_BITFIELD(dwarf_tag) tag : 16;
1034
1035 /* Number of attributes */
98bfdba5
PA
1036 unsigned char num_attrs;
1037
1038 /* True if we're presently building the full type name for the
1039 type derived from this DIE. */
1040 unsigned char building_fullname : 1;
76815b17
DE
1041
1042 /* Abbrev number */
1043 unsigned int abbrev;
1044
93311388 1045 /* Offset in .debug_info or .debug_types section. */
b64f50a1 1046 sect_offset offset;
78ba4af6
JB
1047
1048 /* The dies in a compilation unit form an n-ary tree. PARENT
1049 points to this die's parent; CHILD points to the first child of
1050 this node; and all the children of a given node are chained
4950bc1c 1051 together via their SIBLING fields. */
639d11d3
DC
1052 struct die_info *child; /* Its first child, if any. */
1053 struct die_info *sibling; /* Its next sibling, if any. */
1054 struct die_info *parent; /* Its parent, if any. */
c906108c 1055
b60c80d6
DJ
1056 /* An array of attributes, with NUM_ATTRS elements. There may be
1057 zero, but it's not common and zero-sized arrays are not
1058 sufficiently portable C. */
1059 struct attribute attrs[1];
c906108c
SS
1060 };
1061
0963b4bd 1062/* Get at parts of an attribute structure. */
c906108c
SS
1063
1064#define DW_STRING(attr) ((attr)->u.str)
8285870a 1065#define DW_STRING_IS_CANONICAL(attr) ((attr)->string_is_canonical)
c906108c
SS
1066#define DW_UNSND(attr) ((attr)->u.unsnd)
1067#define DW_BLOCK(attr) ((attr)->u.blk)
1068#define DW_SND(attr) ((attr)->u.snd)
1069#define DW_ADDR(attr) ((attr)->u.addr)
348e048f 1070#define DW_SIGNATURED_TYPE(attr) ((attr)->u.signatured_type)
c906108c 1071
0963b4bd 1072/* Blocks are a bunch of untyped bytes. */
c906108c
SS
1073struct dwarf_block
1074 {
56eb65bd 1075 size_t size;
1d6edc3c
JK
1076
1077 /* Valid only if SIZE is not zero. */
fe1b8b76 1078 gdb_byte *data;
c906108c
SS
1079 };
1080
c906108c
SS
1081#ifndef ATTR_ALLOC_CHUNK
1082#define ATTR_ALLOC_CHUNK 4
1083#endif
1084
c906108c
SS
1085/* Allocate fields for structs, unions and enums in this size. */
1086#ifndef DW_FIELD_ALLOC_CHUNK
1087#define DW_FIELD_ALLOC_CHUNK 4
1088#endif
1089
c906108c
SS
1090/* FIXME: We might want to set this from BFD via bfd_arch_bits_per_byte,
1091 but this would require a corresponding change in unpack_field_as_long
1092 and friends. */
1093static int bits_per_byte = 8;
1094
1095/* The routines that read and process dies for a C struct or C++ class
1096 pass lists of data member fields and lists of member function fields
1097 in an instance of a field_info structure, as defined below. */
1098struct field_info
c5aa993b 1099 {
0963b4bd 1100 /* List of data member and baseclasses fields. */
c5aa993b
JM
1101 struct nextfield
1102 {
1103 struct nextfield *next;
1104 int accessibility;
1105 int virtuality;
1106 struct field field;
1107 }
7d0ccb61 1108 *fields, *baseclasses;
c906108c 1109
7d0ccb61 1110 /* Number of fields (including baseclasses). */
c5aa993b 1111 int nfields;
c906108c 1112
c5aa993b
JM
1113 /* Number of baseclasses. */
1114 int nbaseclasses;
c906108c 1115
c5aa993b
JM
1116 /* Set if the accesibility of one of the fields is not public. */
1117 int non_public_fields;
c906108c 1118
c5aa993b
JM
1119 /* Member function fields array, entries are allocated in the order they
1120 are encountered in the object file. */
1121 struct nextfnfield
1122 {
1123 struct nextfnfield *next;
1124 struct fn_field fnfield;
1125 }
1126 *fnfields;
c906108c 1127
c5aa993b
JM
1128 /* Member function fieldlist array, contains name of possibly overloaded
1129 member function, number of overloaded member functions and a pointer
1130 to the head of the member function field chain. */
1131 struct fnfieldlist
1132 {
1133 char *name;
1134 int length;
1135 struct nextfnfield *head;
1136 }
1137 *fnfieldlists;
c906108c 1138
c5aa993b
JM
1139 /* Number of entries in the fnfieldlists array. */
1140 int nfnfields;
98751a41
JK
1141
1142 /* typedefs defined inside this class. TYPEDEF_FIELD_LIST contains head of
1143 a NULL terminated list of TYPEDEF_FIELD_LIST_COUNT elements. */
1144 struct typedef_field_list
1145 {
1146 struct typedef_field field;
1147 struct typedef_field_list *next;
1148 }
1149 *typedef_field_list;
1150 unsigned typedef_field_list_count;
c5aa993b 1151 };
c906108c 1152
10b3939b
DJ
1153/* One item on the queue of compilation units to read in full symbols
1154 for. */
1155struct dwarf2_queue_item
1156{
1157 struct dwarf2_per_cu_data *per_cu;
95554aad 1158 enum language pretend_language;
10b3939b
DJ
1159 struct dwarf2_queue_item *next;
1160};
1161
1162/* The current queue. */
1163static struct dwarf2_queue_item *dwarf2_queue, *dwarf2_queue_tail;
1164
ae038cb0
DJ
1165/* Loaded secondary compilation units are kept in memory until they
1166 have not been referenced for the processing of this many
1167 compilation units. Set this to zero to disable caching. Cache
1168 sizes of up to at least twenty will improve startup time for
1169 typical inter-CU-reference binaries, at an obvious memory cost. */
1170static int dwarf2_max_cache_age = 5;
920d2a44
AC
1171static void
1172show_dwarf2_max_cache_age (struct ui_file *file, int from_tty,
1173 struct cmd_list_element *c, const char *value)
1174{
3e43a32a
MS
1175 fprintf_filtered (file, _("The upper bound on the age of cached "
1176 "dwarf2 compilation units is %s.\n"),
920d2a44
AC
1177 value);
1178}
1179
ae038cb0 1180
0963b4bd 1181/* Various complaints about symbol reading that don't abort the process. */
c906108c 1182
4d3c2250
KB
1183static void
1184dwarf2_statement_list_fits_in_line_number_section_complaint (void)
2e276125 1185{
4d3c2250 1186 complaint (&symfile_complaints,
e2e0b3e5 1187 _("statement list doesn't fit in .debug_line section"));
4d3c2250
KB
1188}
1189
25e43795
DJ
1190static void
1191dwarf2_debug_line_missing_file_complaint (void)
1192{
1193 complaint (&symfile_complaints,
1194 _(".debug_line section has line data without a file"));
1195}
1196
59205f5a
JB
1197static void
1198dwarf2_debug_line_missing_end_sequence_complaint (void)
1199{
1200 complaint (&symfile_complaints,
3e43a32a
MS
1201 _(".debug_line section has line "
1202 "program sequence without an end"));
59205f5a
JB
1203}
1204
4d3c2250
KB
1205static void
1206dwarf2_complex_location_expr_complaint (void)
2e276125 1207{
e2e0b3e5 1208 complaint (&symfile_complaints, _("location expression too complex"));
4d3c2250
KB
1209}
1210
4d3c2250
KB
1211static void
1212dwarf2_const_value_length_mismatch_complaint (const char *arg1, int arg2,
1213 int arg3)
2e276125 1214{
4d3c2250 1215 complaint (&symfile_complaints,
3e43a32a
MS
1216 _("const value length mismatch for '%s', got %d, expected %d"),
1217 arg1, arg2, arg3);
4d3c2250
KB
1218}
1219
1220static void
f664829e 1221dwarf2_section_buffer_overflow_complaint (struct dwarf2_section_info *section)
2e276125 1222{
4d3c2250 1223 complaint (&symfile_complaints,
f664829e
DE
1224 _("debug info runs off end of %s section"
1225 " [in module %s]"),
1226 section->asection->name,
1227 bfd_get_filename (section->asection->owner));
4d3c2250
KB
1228}
1229
1230static void
1231dwarf2_macro_malformed_definition_complaint (const char *arg1)
8e19ed76 1232{
4d3c2250 1233 complaint (&symfile_complaints,
3e43a32a
MS
1234 _("macro debug info contains a "
1235 "malformed macro definition:\n`%s'"),
4d3c2250
KB
1236 arg1);
1237}
1238
1239static void
1240dwarf2_invalid_attrib_class_complaint (const char *arg1, const char *arg2)
8b2dbe47 1241{
4d3c2250 1242 complaint (&symfile_complaints,
3e43a32a
MS
1243 _("invalid attribute class or form for '%s' in '%s'"),
1244 arg1, arg2);
4d3c2250 1245}
c906108c 1246
c906108c
SS
1247/* local function prototypes */
1248
4efb68b1 1249static void dwarf2_locate_sections (bfd *, asection *, void *);
c906108c 1250
aaa75496
JB
1251static void dwarf2_create_include_psymtab (char *, struct partial_symtab *,
1252 struct objfile *);
1253
918dd910
JK
1254static void dwarf2_find_base_address (struct die_info *die,
1255 struct dwarf2_cu *cu);
1256
c67a9c90 1257static void dwarf2_build_psymtabs_hard (struct objfile *);
c906108c 1258
72bf9492
DJ
1259static void scan_partial_symbols (struct partial_die_info *,
1260 CORE_ADDR *, CORE_ADDR *,
5734ee8b 1261 int, struct dwarf2_cu *);
c906108c 1262
72bf9492
DJ
1263static void add_partial_symbol (struct partial_die_info *,
1264 struct dwarf2_cu *);
63d06c5c 1265
72bf9492
DJ
1266static void add_partial_namespace (struct partial_die_info *pdi,
1267 CORE_ADDR *lowpc, CORE_ADDR *highpc,
5734ee8b 1268 int need_pc, struct dwarf2_cu *cu);
63d06c5c 1269
5d7cb8df
JK
1270static void add_partial_module (struct partial_die_info *pdi, CORE_ADDR *lowpc,
1271 CORE_ADDR *highpc, int need_pc,
1272 struct dwarf2_cu *cu);
1273
72bf9492
DJ
1274static void add_partial_enumeration (struct partial_die_info *enum_pdi,
1275 struct dwarf2_cu *cu);
91c24f0a 1276
bc30ff58
JB
1277static void add_partial_subprogram (struct partial_die_info *pdi,
1278 CORE_ADDR *lowpc, CORE_ADDR *highpc,
5734ee8b 1279 int need_pc, struct dwarf2_cu *cu);
bc30ff58 1280
a14ed312 1281static void dwarf2_psymtab_to_symtab (struct partial_symtab *);
c906108c 1282
a14ed312 1283static void psymtab_to_symtab_1 (struct partial_symtab *);
c906108c 1284
433df2d4
DE
1285static struct abbrev_info *abbrev_table_lookup_abbrev
1286 (const struct abbrev_table *, unsigned int);
1287
1288static struct abbrev_table *abbrev_table_read_table
1289 (struct dwarf2_section_info *, sect_offset);
1290
1291static void abbrev_table_free (struct abbrev_table *);
1292
f4dc4d17
DE
1293static void abbrev_table_free_cleanup (void *);
1294
dee91e82
DE
1295static void dwarf2_read_abbrevs (struct dwarf2_cu *,
1296 struct dwarf2_section_info *);
c906108c 1297
f3dd6933 1298static void dwarf2_free_abbrev_table (void *);
c906108c 1299
6caca83c
CC
1300static unsigned int peek_abbrev_code (bfd *, gdb_byte *);
1301
dee91e82
DE
1302static struct partial_die_info *load_partial_dies
1303 (const struct die_reader_specs *, gdb_byte *, int);
72bf9492 1304
dee91e82
DE
1305static gdb_byte *read_partial_die (const struct die_reader_specs *,
1306 struct partial_die_info *,
1307 struct abbrev_info *,
1308 unsigned int,
1309 gdb_byte *);
c906108c 1310
36586728 1311static struct partial_die_info *find_partial_die (sect_offset, int,
10b3939b 1312 struct dwarf2_cu *);
72bf9492
DJ
1313
1314static void fixup_partial_die (struct partial_die_info *,
1315 struct dwarf2_cu *);
1316
dee91e82
DE
1317static gdb_byte *read_attribute (const struct die_reader_specs *,
1318 struct attribute *, struct attr_abbrev *,
1319 gdb_byte *);
a8329558 1320
a1855c1d 1321static unsigned int read_1_byte (bfd *, const gdb_byte *);
c906108c 1322
a1855c1d 1323static int read_1_signed_byte (bfd *, const gdb_byte *);
c906108c 1324
a1855c1d 1325static unsigned int read_2_bytes (bfd *, const gdb_byte *);
c906108c 1326
a1855c1d 1327static unsigned int read_4_bytes (bfd *, const gdb_byte *);
c906108c 1328
a1855c1d 1329static ULONGEST read_8_bytes (bfd *, const gdb_byte *);
c906108c 1330
fe1b8b76 1331static CORE_ADDR read_address (bfd *, gdb_byte *ptr, struct dwarf2_cu *,
891d2f0b 1332 unsigned int *);
c906108c 1333
c764a876
DE
1334static LONGEST read_initial_length (bfd *, gdb_byte *, unsigned int *);
1335
1336static LONGEST read_checked_initial_length_and_offset
1337 (bfd *, gdb_byte *, const struct comp_unit_head *,
1338 unsigned int *, unsigned int *);
613e1657 1339
fe1b8b76 1340static LONGEST read_offset (bfd *, gdb_byte *, const struct comp_unit_head *,
c764a876
DE
1341 unsigned int *);
1342
1343static LONGEST read_offset_1 (bfd *, gdb_byte *, unsigned int);
613e1657 1344
f4dc4d17
DE
1345static sect_offset read_abbrev_offset (struct dwarf2_section_info *,
1346 sect_offset);
1347
fe1b8b76 1348static gdb_byte *read_n_bytes (bfd *, gdb_byte *, unsigned int);
c906108c 1349
9b1c24c8 1350static char *read_direct_string (bfd *, gdb_byte *, unsigned int *);
c906108c 1351
fe1b8b76
JB
1352static char *read_indirect_string (bfd *, gdb_byte *,
1353 const struct comp_unit_head *,
1354 unsigned int *);
4bdf3d34 1355
36586728
TT
1356static char *read_indirect_string_from_dwz (struct dwz_file *, LONGEST);
1357
12df843f 1358static ULONGEST read_unsigned_leb128 (bfd *, gdb_byte *, unsigned int *);
c906108c 1359
12df843f 1360static LONGEST read_signed_leb128 (bfd *, gdb_byte *, unsigned int *);
c906108c 1361
3019eac3
DE
1362static CORE_ADDR read_addr_index_from_leb128 (struct dwarf2_cu *, gdb_byte *,
1363 unsigned int *);
1364
1365static char *read_str_index (const struct die_reader_specs *reader,
1366 struct dwarf2_cu *cu, ULONGEST str_index);
1367
e142c38c 1368static void set_cu_language (unsigned int, struct dwarf2_cu *);
c906108c 1369
e142c38c
DJ
1370static struct attribute *dwarf2_attr (struct die_info *, unsigned int,
1371 struct dwarf2_cu *);
c906108c 1372
348e048f 1373static struct attribute *dwarf2_attr_no_follow (struct die_info *,
45e58e77 1374 unsigned int);
348e048f 1375
05cf31d1
JB
1376static int dwarf2_flag_true_p (struct die_info *die, unsigned name,
1377 struct dwarf2_cu *cu);
1378
e142c38c 1379static int die_is_declaration (struct die_info *, struct dwarf2_cu *cu);
3ca72b44 1380
e142c38c 1381static struct die_info *die_specification (struct die_info *die,
f2f0e013 1382 struct dwarf2_cu **);
63d06c5c 1383
debd256d
JB
1384static void free_line_header (struct line_header *lh);
1385
aaa75496
JB
1386static void add_file_name (struct line_header *, char *, unsigned int,
1387 unsigned int, unsigned int);
1388
3019eac3
DE
1389static struct line_header *dwarf_decode_line_header (unsigned int offset,
1390 struct dwarf2_cu *cu);
debd256d 1391
f3f5162e
DE
1392static void dwarf_decode_lines (struct line_header *, const char *,
1393 struct dwarf2_cu *, struct partial_symtab *,
1394 int);
c906108c 1395
72b9f47f 1396static void dwarf2_start_subfile (char *, const char *, const char *);
c906108c 1397
f4dc4d17
DE
1398static void dwarf2_start_symtab (struct dwarf2_cu *,
1399 char *, char *, CORE_ADDR);
1400
a14ed312 1401static struct symbol *new_symbol (struct die_info *, struct type *,
e7c27a73 1402 struct dwarf2_cu *);
c906108c 1403
34eaf542
TT
1404static struct symbol *new_symbol_full (struct die_info *, struct type *,
1405 struct dwarf2_cu *, struct symbol *);
1406
a14ed312 1407static void dwarf2_const_value (struct attribute *, struct symbol *,
e7c27a73 1408 struct dwarf2_cu *);
c906108c 1409
98bfdba5
PA
1410static void dwarf2_const_value_attr (struct attribute *attr,
1411 struct type *type,
1412 const char *name,
1413 struct obstack *obstack,
12df843f 1414 struct dwarf2_cu *cu, LONGEST *value,
98bfdba5
PA
1415 gdb_byte **bytes,
1416 struct dwarf2_locexpr_baton **baton);
2df3850c 1417
e7c27a73 1418static struct type *die_type (struct die_info *, struct dwarf2_cu *);
c906108c 1419
b4ba55a1
JB
1420static int need_gnat_info (struct dwarf2_cu *);
1421
3e43a32a
MS
1422static struct type *die_descriptive_type (struct die_info *,
1423 struct dwarf2_cu *);
b4ba55a1
JB
1424
1425static void set_descriptive_type (struct type *, struct die_info *,
1426 struct dwarf2_cu *);
1427
e7c27a73
DJ
1428static struct type *die_containing_type (struct die_info *,
1429 struct dwarf2_cu *);
c906108c 1430
673bfd45
DE
1431static struct type *lookup_die_type (struct die_info *, struct attribute *,
1432 struct dwarf2_cu *);
c906108c 1433
f792889a 1434static struct type *read_type_die (struct die_info *, struct dwarf2_cu *);
c906108c 1435
673bfd45
DE
1436static struct type *read_type_die_1 (struct die_info *, struct dwarf2_cu *);
1437
0d5cff50 1438static const char *determine_prefix (struct die_info *die, struct dwarf2_cu *);
63d06c5c 1439
6e70227d 1440static char *typename_concat (struct obstack *obs, const char *prefix,
f55ee35c
JK
1441 const char *suffix, int physname,
1442 struct dwarf2_cu *cu);
63d06c5c 1443
e7c27a73 1444static void read_file_scope (struct die_info *, struct dwarf2_cu *);
c906108c 1445
348e048f
DE
1446static void read_type_unit_scope (struct die_info *, struct dwarf2_cu *);
1447
e7c27a73 1448static void read_func_scope (struct die_info *, struct dwarf2_cu *);
c906108c 1449
e7c27a73 1450static void read_lexical_block_scope (struct die_info *, struct dwarf2_cu *);
c906108c 1451
96408a79
SA
1452static void read_call_site_scope (struct die_info *die, struct dwarf2_cu *cu);
1453
ff013f42
JK
1454static int dwarf2_ranges_read (unsigned, CORE_ADDR *, CORE_ADDR *,
1455 struct dwarf2_cu *, struct partial_symtab *);
1456
a14ed312 1457static int dwarf2_get_pc_bounds (struct die_info *,
d85a05f0
DJ
1458 CORE_ADDR *, CORE_ADDR *, struct dwarf2_cu *,
1459 struct partial_symtab *);
c906108c 1460
fae299cd
DC
1461static void get_scope_pc_bounds (struct die_info *,
1462 CORE_ADDR *, CORE_ADDR *,
1463 struct dwarf2_cu *);
1464
801e3a5b
JB
1465static void dwarf2_record_block_ranges (struct die_info *, struct block *,
1466 CORE_ADDR, struct dwarf2_cu *);
1467
a14ed312 1468static void dwarf2_add_field (struct field_info *, struct die_info *,
e7c27a73 1469 struct dwarf2_cu *);
c906108c 1470
a14ed312 1471static void dwarf2_attach_fields_to_type (struct field_info *,
e7c27a73 1472 struct type *, struct dwarf2_cu *);
c906108c 1473
a14ed312 1474static void dwarf2_add_member_fn (struct field_info *,
e26fb1d7 1475 struct die_info *, struct type *,
e7c27a73 1476 struct dwarf2_cu *);
c906108c 1477
a14ed312 1478static void dwarf2_attach_fn_fields_to_type (struct field_info *,
3e43a32a
MS
1479 struct type *,
1480 struct dwarf2_cu *);
c906108c 1481
134d01f1 1482static void process_structure_scope (struct die_info *, struct dwarf2_cu *);
c906108c 1483
e7c27a73 1484static void read_common_block (struct die_info *, struct dwarf2_cu *);
c906108c 1485
e7c27a73 1486static void read_namespace (struct die_info *die, struct dwarf2_cu *);
d9fa45fe 1487
5d7cb8df
JK
1488static void read_module (struct die_info *die, struct dwarf2_cu *cu);
1489
27aa8d6a
SW
1490static void read_import_statement (struct die_info *die, struct dwarf2_cu *);
1491
f55ee35c
JK
1492static struct type *read_module_type (struct die_info *die,
1493 struct dwarf2_cu *cu);
1494
38d518c9 1495static const char *namespace_name (struct die_info *die,
e142c38c 1496 int *is_anonymous, struct dwarf2_cu *);
38d518c9 1497
134d01f1 1498static void process_enumeration_scope (struct die_info *, struct dwarf2_cu *);
c906108c 1499
e7c27a73 1500static CORE_ADDR decode_locdesc (struct dwarf_block *, struct dwarf2_cu *);
c906108c 1501
6e70227d 1502static enum dwarf_array_dim_ordering read_array_order (struct die_info *,
7ca2d3a3
DL
1503 struct dwarf2_cu *);
1504
dee91e82 1505static struct die_info *read_die_and_children (const struct die_reader_specs *,
93311388 1506 gdb_byte *info_ptr,
fe1b8b76 1507 gdb_byte **new_info_ptr,
639d11d3
DC
1508 struct die_info *parent);
1509
dee91e82 1510static struct die_info *read_die_and_siblings (const struct die_reader_specs *,
93311388 1511 gdb_byte *info_ptr,
fe1b8b76 1512 gdb_byte **new_info_ptr,
639d11d3
DC
1513 struct die_info *parent);
1514
3019eac3
DE
1515static gdb_byte *read_full_die_1 (const struct die_reader_specs *,
1516 struct die_info **, gdb_byte *, int *, int);
1517
dee91e82
DE
1518static gdb_byte *read_full_die (const struct die_reader_specs *,
1519 struct die_info **, gdb_byte *, int *);
93311388 1520
e7c27a73 1521static void process_die (struct die_info *, struct dwarf2_cu *);
c906108c 1522
71c25dea
TT
1523static char *dwarf2_canonicalize_name (char *, struct dwarf2_cu *,
1524 struct obstack *);
1525
e142c38c 1526static char *dwarf2_name (struct die_info *die, struct dwarf2_cu *);
9219021c 1527
98bfdba5
PA
1528static const char *dwarf2_full_name (char *name,
1529 struct die_info *die,
1530 struct dwarf2_cu *cu);
1531
e142c38c 1532static struct die_info *dwarf2_extension (struct die_info *die,
f2f0e013 1533 struct dwarf2_cu **);
9219021c 1534
f39c6ffd 1535static const char *dwarf_tag_name (unsigned int);
c906108c 1536
f39c6ffd 1537static const char *dwarf_attr_name (unsigned int);
c906108c 1538
f39c6ffd 1539static const char *dwarf_form_name (unsigned int);
c906108c 1540
a14ed312 1541static char *dwarf_bool_name (unsigned int);
c906108c 1542
f39c6ffd 1543static const char *dwarf_type_encoding_name (unsigned int);
c906108c 1544
f9aca02d 1545static struct die_info *sibling_die (struct die_info *);
c906108c 1546
d97bc12b
DE
1547static void dump_die_shallow (struct ui_file *, int indent, struct die_info *);
1548
1549static void dump_die_for_error (struct die_info *);
1550
1551static void dump_die_1 (struct ui_file *, int level, int max_level,
1552 struct die_info *);
c906108c 1553
d97bc12b 1554/*static*/ void dump_die (struct die_info *, int max_level);
c906108c 1555
51545339 1556static void store_in_ref_table (struct die_info *,
10b3939b 1557 struct dwarf2_cu *);
c906108c 1558
93311388
DE
1559static int is_ref_attr (struct attribute *);
1560
b64f50a1 1561static sect_offset dwarf2_get_ref_die_offset (struct attribute *);
c906108c 1562
43bbcdc2 1563static LONGEST dwarf2_get_attr_constant_value (struct attribute *, int);
a02abb62 1564
348e048f
DE
1565static struct die_info *follow_die_ref_or_sig (struct die_info *,
1566 struct attribute *,
1567 struct dwarf2_cu **);
1568
10b3939b
DJ
1569static struct die_info *follow_die_ref (struct die_info *,
1570 struct attribute *,
f2f0e013 1571 struct dwarf2_cu **);
c906108c 1572
348e048f
DE
1573static struct die_info *follow_die_sig (struct die_info *,
1574 struct attribute *,
1575 struct dwarf2_cu **);
1576
6c83ed52
TT
1577static struct signatured_type *lookup_signatured_type_at_offset
1578 (struct objfile *objfile,
b64f50a1 1579 struct dwarf2_section_info *section, sect_offset offset);
6c83ed52 1580
e5fe5e75 1581static void load_full_type_unit (struct dwarf2_per_cu_data *per_cu);
348e048f 1582
52dc124a 1583static void read_signatured_type (struct signatured_type *);
348e048f 1584
f4dc4d17 1585static struct type_unit_group *get_type_unit_group
094b34ac 1586 (struct dwarf2_cu *, struct attribute *);
f4dc4d17
DE
1587
1588static void build_type_unit_groups (die_reader_func_ftype *, void *);
1589
c906108c
SS
1590/* memory allocation interface */
1591
7b5a2f43 1592static struct dwarf_block *dwarf_alloc_block (struct dwarf2_cu *);
c906108c 1593
b60c80d6 1594static struct die_info *dwarf_alloc_die (struct dwarf2_cu *, int);
c906108c 1595
09262596
DE
1596static void dwarf_decode_macros (struct dwarf2_cu *, unsigned int,
1597 char *, int);
2e276125 1598
8e19ed76
PS
1599static int attr_form_is_block (struct attribute *);
1600
3690dd37
JB
1601static int attr_form_is_section_offset (struct attribute *);
1602
1603static int attr_form_is_constant (struct attribute *);
1604
8cf6f0b1
TT
1605static void fill_in_loclist_baton (struct dwarf2_cu *cu,
1606 struct dwarf2_loclist_baton *baton,
1607 struct attribute *attr);
1608
93e7bd98
DJ
1609static void dwarf2_symbol_mark_computed (struct attribute *attr,
1610 struct symbol *sym,
1611 struct dwarf2_cu *cu);
4c2df51b 1612
dee91e82
DE
1613static gdb_byte *skip_one_die (const struct die_reader_specs *reader,
1614 gdb_byte *info_ptr,
1615 struct abbrev_info *abbrev);
4bb7a0a7 1616
72bf9492
DJ
1617static void free_stack_comp_unit (void *);
1618
72bf9492
DJ
1619static hashval_t partial_die_hash (const void *item);
1620
1621static int partial_die_eq (const void *item_lhs, const void *item_rhs);
1622
ae038cb0 1623static struct dwarf2_per_cu_data *dwarf2_find_containing_comp_unit
36586728 1624 (sect_offset offset, unsigned int offset_in_dwz, struct objfile *objfile);
ae038cb0 1625
9816fde3 1626static void init_one_comp_unit (struct dwarf2_cu *cu,
23745b47 1627 struct dwarf2_per_cu_data *per_cu);
9816fde3
JK
1628
1629static void prepare_one_comp_unit (struct dwarf2_cu *cu,
95554aad
TT
1630 struct die_info *comp_unit_die,
1631 enum language pretend_language);
93311388 1632
68dc6402 1633static void free_heap_comp_unit (void *);
ae038cb0
DJ
1634
1635static void free_cached_comp_units (void *);
1636
1637static void age_cached_comp_units (void);
1638
dee91e82 1639static void free_one_cached_comp_unit (struct dwarf2_per_cu_data *);
ae038cb0 1640
f792889a
DJ
1641static struct type *set_die_type (struct die_info *, struct type *,
1642 struct dwarf2_cu *);
1c379e20 1643
ae038cb0
DJ
1644static void create_all_comp_units (struct objfile *);
1645
0e50663e 1646static int create_all_type_units (struct objfile *);
1fd400ff 1647
95554aad
TT
1648static void load_full_comp_unit (struct dwarf2_per_cu_data *,
1649 enum language);
10b3939b 1650
95554aad
TT
1651static void process_full_comp_unit (struct dwarf2_per_cu_data *,
1652 enum language);
10b3939b 1653
f4dc4d17
DE
1654static void process_full_type_unit (struct dwarf2_per_cu_data *,
1655 enum language);
1656
10b3939b
DJ
1657static void dwarf2_add_dependence (struct dwarf2_cu *,
1658 struct dwarf2_per_cu_data *);
1659
ae038cb0
DJ
1660static void dwarf2_mark (struct dwarf2_cu *);
1661
1662static void dwarf2_clear_marks (struct dwarf2_per_cu_data *);
1663
b64f50a1 1664static struct type *get_die_type_at_offset (sect_offset,
673bfd45
DE
1665 struct dwarf2_per_cu_data *per_cu);
1666
f792889a 1667static struct type *get_die_type (struct die_info *die, struct dwarf2_cu *cu);
72019c9c 1668
9291a0cd
TT
1669static void dwarf2_release_queue (void *dummy);
1670
95554aad
TT
1671static void queue_comp_unit (struct dwarf2_per_cu_data *per_cu,
1672 enum language pretend_language);
1673
1674static int maybe_queue_comp_unit (struct dwarf2_cu *this_cu,
1675 struct dwarf2_per_cu_data *per_cu,
1676 enum language pretend_language);
9291a0cd 1677
a0f42c21 1678static void process_queue (void);
9291a0cd
TT
1679
1680static void find_file_and_directory (struct die_info *die,
1681 struct dwarf2_cu *cu,
1682 char **name, char **comp_dir);
1683
1684static char *file_full_name (int file, struct line_header *lh,
1685 const char *comp_dir);
1686
36586728
TT
1687static gdb_byte *read_and_check_comp_unit_head
1688 (struct comp_unit_head *header,
1689 struct dwarf2_section_info *section,
1690 struct dwarf2_section_info *abbrev_section, gdb_byte *info_ptr,
1691 int is_debug_types_section);
1692
fd820528 1693static void init_cutu_and_read_dies
f4dc4d17
DE
1694 (struct dwarf2_per_cu_data *this_cu, struct abbrev_table *abbrev_table,
1695 int use_existing_cu, int keep,
3019eac3
DE
1696 die_reader_func_ftype *die_reader_func, void *data);
1697
dee91e82
DE
1698static void init_cutu_and_read_dies_simple
1699 (struct dwarf2_per_cu_data *this_cu,
1700 die_reader_func_ftype *die_reader_func, void *data);
9291a0cd 1701
673bfd45 1702static htab_t allocate_signatured_type_table (struct objfile *objfile);
1fd400ff 1703
3019eac3
DE
1704static htab_t allocate_dwo_unit_table (struct objfile *objfile);
1705
1706static struct dwo_unit *lookup_dwo_comp_unit
a1855c1d 1707 (struct dwarf2_per_cu_data *, const char *, const char *, ULONGEST);
3019eac3
DE
1708
1709static struct dwo_unit *lookup_dwo_type_unit
a1855c1d 1710 (struct signatured_type *, const char *, const char *);
3019eac3
DE
1711
1712static void free_dwo_file_cleanup (void *);
1713
95554aad
TT
1714static void process_cu_includes (void);
1715
9291a0cd
TT
1716#if WORDS_BIGENDIAN
1717
1718/* Convert VALUE between big- and little-endian. */
1719static offset_type
1720byte_swap (offset_type value)
1721{
1722 offset_type result;
1723
1724 result = (value & 0xff) << 24;
1725 result |= (value & 0xff00) << 8;
1726 result |= (value & 0xff0000) >> 8;
1727 result |= (value & 0xff000000) >> 24;
1728 return result;
1729}
1730
1731#define MAYBE_SWAP(V) byte_swap (V)
1732
1733#else
1734#define MAYBE_SWAP(V) (V)
1735#endif /* WORDS_BIGENDIAN */
1736
1737/* The suffix for an index file. */
1738#define INDEX_SUFFIX ".gdb-index"
1739
3da10d80
KS
1740static const char *dwarf2_physname (char *name, struct die_info *die,
1741 struct dwarf2_cu *cu);
1742
c906108c 1743/* Try to locate the sections we need for DWARF 2 debugging
251d32d9
TG
1744 information and return true if we have enough to do something.
1745 NAMES points to the dwarf2 section names, or is NULL if the standard
1746 ELF names are used. */
c906108c
SS
1747
1748int
251d32d9
TG
1749dwarf2_has_info (struct objfile *objfile,
1750 const struct dwarf2_debug_sections *names)
c906108c 1751{
be391dca
TT
1752 dwarf2_per_objfile = objfile_data (objfile, dwarf2_objfile_data_key);
1753 if (!dwarf2_per_objfile)
1754 {
1755 /* Initialize per-objfile state. */
1756 struct dwarf2_per_objfile *data
1757 = obstack_alloc (&objfile->objfile_obstack, sizeof (*data));
9a619af0 1758
be391dca
TT
1759 memset (data, 0, sizeof (*data));
1760 set_objfile_data (objfile, dwarf2_objfile_data_key, data);
1761 dwarf2_per_objfile = data;
6502dd73 1762
251d32d9
TG
1763 bfd_map_over_sections (objfile->obfd, dwarf2_locate_sections,
1764 (void *) names);
be391dca
TT
1765 dwarf2_per_objfile->objfile = objfile;
1766 }
1767 return (dwarf2_per_objfile->info.asection != NULL
1768 && dwarf2_per_objfile->abbrev.asection != NULL);
c906108c
SS
1769}
1770
251d32d9
TG
1771/* When loading sections, we look either for uncompressed section or for
1772 compressed section names. */
233a11ab
CS
1773
1774static int
251d32d9
TG
1775section_is_p (const char *section_name,
1776 const struct dwarf2_section_names *names)
233a11ab 1777{
251d32d9
TG
1778 if (names->normal != NULL
1779 && strcmp (section_name, names->normal) == 0)
1780 return 1;
1781 if (names->compressed != NULL
1782 && strcmp (section_name, names->compressed) == 0)
1783 return 1;
1784 return 0;
233a11ab
CS
1785}
1786
c906108c
SS
1787/* This function is mapped across the sections and remembers the
1788 offset and size of each of the debugging sections we are interested
1789 in. */
1790
1791static void
251d32d9 1792dwarf2_locate_sections (bfd *abfd, asection *sectp, void *vnames)
c906108c 1793{
251d32d9 1794 const struct dwarf2_debug_sections *names;
dc7650b8 1795 flagword aflag = bfd_get_section_flags (abfd, sectp);
251d32d9
TG
1796
1797 if (vnames == NULL)
1798 names = &dwarf2_elf_names;
1799 else
1800 names = (const struct dwarf2_debug_sections *) vnames;
1801
dc7650b8
JK
1802 if ((aflag & SEC_HAS_CONTENTS) == 0)
1803 {
1804 }
1805 else if (section_is_p (sectp->name, &names->info))
c906108c 1806 {
dce234bc
PP
1807 dwarf2_per_objfile->info.asection = sectp;
1808 dwarf2_per_objfile->info.size = bfd_get_section_size (sectp);
c906108c 1809 }
251d32d9 1810 else if (section_is_p (sectp->name, &names->abbrev))
c906108c 1811 {
dce234bc
PP
1812 dwarf2_per_objfile->abbrev.asection = sectp;
1813 dwarf2_per_objfile->abbrev.size = bfd_get_section_size (sectp);
c906108c 1814 }
251d32d9 1815 else if (section_is_p (sectp->name, &names->line))
c906108c 1816 {
dce234bc
PP
1817 dwarf2_per_objfile->line.asection = sectp;
1818 dwarf2_per_objfile->line.size = bfd_get_section_size (sectp);
c906108c 1819 }
251d32d9 1820 else if (section_is_p (sectp->name, &names->loc))
c906108c 1821 {
dce234bc
PP
1822 dwarf2_per_objfile->loc.asection = sectp;
1823 dwarf2_per_objfile->loc.size = bfd_get_section_size (sectp);
c906108c 1824 }
251d32d9 1825 else if (section_is_p (sectp->name, &names->macinfo))
c906108c 1826 {
dce234bc
PP
1827 dwarf2_per_objfile->macinfo.asection = sectp;
1828 dwarf2_per_objfile->macinfo.size = bfd_get_section_size (sectp);
c906108c 1829 }
cf2c3c16
TT
1830 else if (section_is_p (sectp->name, &names->macro))
1831 {
1832 dwarf2_per_objfile->macro.asection = sectp;
1833 dwarf2_per_objfile->macro.size = bfd_get_section_size (sectp);
1834 }
251d32d9 1835 else if (section_is_p (sectp->name, &names->str))
c906108c 1836 {
dce234bc
PP
1837 dwarf2_per_objfile->str.asection = sectp;
1838 dwarf2_per_objfile->str.size = bfd_get_section_size (sectp);
c906108c 1839 }
3019eac3
DE
1840 else if (section_is_p (sectp->name, &names->addr))
1841 {
1842 dwarf2_per_objfile->addr.asection = sectp;
1843 dwarf2_per_objfile->addr.size = bfd_get_section_size (sectp);
1844 }
251d32d9 1845 else if (section_is_p (sectp->name, &names->frame))
b6af0555 1846 {
dce234bc
PP
1847 dwarf2_per_objfile->frame.asection = sectp;
1848 dwarf2_per_objfile->frame.size = bfd_get_section_size (sectp);
b6af0555 1849 }
251d32d9 1850 else if (section_is_p (sectp->name, &names->eh_frame))
b6af0555 1851 {
dc7650b8
JK
1852 dwarf2_per_objfile->eh_frame.asection = sectp;
1853 dwarf2_per_objfile->eh_frame.size = bfd_get_section_size (sectp);
b6af0555 1854 }
251d32d9 1855 else if (section_is_p (sectp->name, &names->ranges))
af34e669 1856 {
dce234bc
PP
1857 dwarf2_per_objfile->ranges.asection = sectp;
1858 dwarf2_per_objfile->ranges.size = bfd_get_section_size (sectp);
af34e669 1859 }
251d32d9 1860 else if (section_is_p (sectp->name, &names->types))
348e048f 1861 {
8b70b953
TT
1862 struct dwarf2_section_info type_section;
1863
1864 memset (&type_section, 0, sizeof (type_section));
1865 type_section.asection = sectp;
1866 type_section.size = bfd_get_section_size (sectp);
1867
1868 VEC_safe_push (dwarf2_section_info_def, dwarf2_per_objfile->types,
1869 &type_section);
348e048f 1870 }
251d32d9 1871 else if (section_is_p (sectp->name, &names->gdb_index))
9291a0cd
TT
1872 {
1873 dwarf2_per_objfile->gdb_index.asection = sectp;
1874 dwarf2_per_objfile->gdb_index.size = bfd_get_section_size (sectp);
1875 }
dce234bc 1876
72dca2f5
FR
1877 if ((bfd_get_section_flags (abfd, sectp) & SEC_LOAD)
1878 && bfd_section_vma (abfd, sectp) == 0)
1879 dwarf2_per_objfile->has_section_at_zero = 1;
c906108c
SS
1880}
1881
fceca515
DE
1882/* A helper function that decides whether a section is empty,
1883 or not present. */
9e0ac564
TT
1884
1885static int
1886dwarf2_section_empty_p (struct dwarf2_section_info *info)
1887{
1888 return info->asection == NULL || info->size == 0;
1889}
1890
3019eac3
DE
1891/* Read the contents of the section INFO.
1892 OBJFILE is the main object file, but not necessarily the file where
1893 the section comes from. E.g., for DWO files INFO->asection->owner
1894 is the bfd of the DWO file.
dce234bc 1895 If the section is compressed, uncompress it before returning. */
c906108c 1896
dce234bc
PP
1897static void
1898dwarf2_read_section (struct objfile *objfile, struct dwarf2_section_info *info)
c906108c 1899{
dce234bc 1900 asection *sectp = info->asection;
3019eac3 1901 bfd *abfd;
dce234bc
PP
1902 gdb_byte *buf, *retbuf;
1903 unsigned char header[4];
c906108c 1904
be391dca
TT
1905 if (info->readin)
1906 return;
dce234bc 1907 info->buffer = NULL;
be391dca 1908 info->readin = 1;
188dd5d6 1909
9e0ac564 1910 if (dwarf2_section_empty_p (info))
dce234bc 1911 return;
c906108c 1912
3019eac3
DE
1913 abfd = sectp->owner;
1914
4bf44c1c
TT
1915 /* If the section has relocations, we must read it ourselves.
1916 Otherwise we attach it to the BFD. */
1917 if ((sectp->flags & SEC_RELOC) == 0)
dce234bc 1918 {
4bf44c1c 1919 const gdb_byte *bytes = gdb_bfd_map_section (sectp, &info->size);
dce234bc 1920
4bf44c1c
TT
1921 /* We have to cast away const here for historical reasons.
1922 Fixing dwarf2read to be const-correct would be quite nice. */
1923 info->buffer = (gdb_byte *) bytes;
1924 return;
dce234bc 1925 }
dce234bc 1926
4bf44c1c
TT
1927 buf = obstack_alloc (&objfile->objfile_obstack, info->size);
1928 info->buffer = buf;
dce234bc
PP
1929
1930 /* When debugging .o files, we may need to apply relocations; see
1931 http://sourceware.org/ml/gdb-patches/2002-04/msg00136.html .
1932 We never compress sections in .o files, so we only need to
1933 try this when the section is not compressed. */
ac8035ab 1934 retbuf = symfile_relocate_debug_section (objfile, sectp, buf);
dce234bc
PP
1935 if (retbuf != NULL)
1936 {
1937 info->buffer = retbuf;
1938 return;
1939 }
1940
1941 if (bfd_seek (abfd, sectp->filepos, SEEK_SET) != 0
1942 || bfd_bread (buf, info->size, abfd) != info->size)
1943 error (_("Dwarf Error: Can't read DWARF data from '%s'"),
1944 bfd_get_filename (abfd));
1945}
1946
9e0ac564
TT
1947/* A helper function that returns the size of a section in a safe way.
1948 If you are positive that the section has been read before using the
1949 size, then it is safe to refer to the dwarf2_section_info object's
1950 "size" field directly. In other cases, you must call this
1951 function, because for compressed sections the size field is not set
1952 correctly until the section has been read. */
1953
1954static bfd_size_type
1955dwarf2_section_size (struct objfile *objfile,
1956 struct dwarf2_section_info *info)
1957{
1958 if (!info->readin)
1959 dwarf2_read_section (objfile, info);
1960 return info->size;
1961}
1962
dce234bc 1963/* Fill in SECTP, BUFP and SIZEP with section info, given OBJFILE and
0963b4bd 1964 SECTION_NAME. */
af34e669 1965
dce234bc 1966void
3017a003
TG
1967dwarf2_get_section_info (struct objfile *objfile,
1968 enum dwarf2_section_enum sect,
dce234bc
PP
1969 asection **sectp, gdb_byte **bufp,
1970 bfd_size_type *sizep)
1971{
1972 struct dwarf2_per_objfile *data
1973 = objfile_data (objfile, dwarf2_objfile_data_key);
1974 struct dwarf2_section_info *info;
a3b2a86b
TT
1975
1976 /* We may see an objfile without any DWARF, in which case we just
1977 return nothing. */
1978 if (data == NULL)
1979 {
1980 *sectp = NULL;
1981 *bufp = NULL;
1982 *sizep = 0;
1983 return;
1984 }
3017a003
TG
1985 switch (sect)
1986 {
1987 case DWARF2_DEBUG_FRAME:
1988 info = &data->frame;
1989 break;
1990 case DWARF2_EH_FRAME:
1991 info = &data->eh_frame;
1992 break;
1993 default:
1994 gdb_assert_not_reached ("unexpected section");
1995 }
dce234bc 1996
9e0ac564 1997 dwarf2_read_section (objfile, info);
dce234bc
PP
1998
1999 *sectp = info->asection;
2000 *bufp = info->buffer;
2001 *sizep = info->size;
2002}
2003
36586728
TT
2004/* A helper function to find the sections for a .dwz file. */
2005
2006static void
2007locate_dwz_sections (bfd *abfd, asection *sectp, void *arg)
2008{
2009 struct dwz_file *dwz_file = arg;
2010
2011 /* Note that we only support the standard ELF names, because .dwz
2012 is ELF-only (at the time of writing). */
2013 if (section_is_p (sectp->name, &dwarf2_elf_names.abbrev))
2014 {
2015 dwz_file->abbrev.asection = sectp;
2016 dwz_file->abbrev.size = bfd_get_section_size (sectp);
2017 }
2018 else if (section_is_p (sectp->name, &dwarf2_elf_names.info))
2019 {
2020 dwz_file->info.asection = sectp;
2021 dwz_file->info.size = bfd_get_section_size (sectp);
2022 }
2023 else if (section_is_p (sectp->name, &dwarf2_elf_names.str))
2024 {
2025 dwz_file->str.asection = sectp;
2026 dwz_file->str.size = bfd_get_section_size (sectp);
2027 }
2028 else if (section_is_p (sectp->name, &dwarf2_elf_names.line))
2029 {
2030 dwz_file->line.asection = sectp;
2031 dwz_file->line.size = bfd_get_section_size (sectp);
2032 }
2033 else if (section_is_p (sectp->name, &dwarf2_elf_names.macro))
2034 {
2035 dwz_file->macro.asection = sectp;
2036 dwz_file->macro.size = bfd_get_section_size (sectp);
2037 }
2ec9a5e0
TT
2038 else if (section_is_p (sectp->name, &dwarf2_elf_names.gdb_index))
2039 {
2040 dwz_file->gdb_index.asection = sectp;
2041 dwz_file->gdb_index.size = bfd_get_section_size (sectp);
2042 }
36586728
TT
2043}
2044
2045/* Open the separate '.dwz' debug file, if needed. Error if the file
2046 cannot be found. */
2047
2048static struct dwz_file *
2049dwarf2_get_dwz_file (void)
2050{
2051 bfd *abfd, *dwz_bfd;
2052 asection *section;
2053 gdb_byte *data;
2054 struct cleanup *cleanup;
2055 const char *filename;
2056 struct dwz_file *result;
2057
2058 if (dwarf2_per_objfile->dwz_file != NULL)
2059 return dwarf2_per_objfile->dwz_file;
2060
2061 abfd = dwarf2_per_objfile->objfile->obfd;
2062 section = bfd_get_section_by_name (abfd, ".gnu_debugaltlink");
2063 if (section == NULL)
2064 error (_("could not find '.gnu_debugaltlink' section"));
2065 if (!bfd_malloc_and_get_section (abfd, section, &data))
2066 error (_("could not read '.gnu_debugaltlink' section: %s"),
2067 bfd_errmsg (bfd_get_error ()));
2068 cleanup = make_cleanup (xfree, data);
2069
2070 filename = data;
2071 if (!IS_ABSOLUTE_PATH (filename))
2072 {
2073 char *abs = gdb_realpath (dwarf2_per_objfile->objfile->name);
2074 char *rel;
2075
2076 make_cleanup (xfree, abs);
2077 abs = ldirname (abs);
2078 make_cleanup (xfree, abs);
2079
2080 rel = concat (abs, SLASH_STRING, filename, (char *) NULL);
2081 make_cleanup (xfree, rel);
2082 filename = rel;
2083 }
2084
2085 /* The format is just a NUL-terminated file name, followed by the
2086 build-id. For now, though, we ignore the build-id. */
2087 dwz_bfd = gdb_bfd_open (filename, gnutarget, -1);
2088 if (dwz_bfd == NULL)
2089 error (_("could not read '%s': %s"), filename,
2090 bfd_errmsg (bfd_get_error ()));
2091
2092 if (!bfd_check_format (dwz_bfd, bfd_object))
2093 {
2094 gdb_bfd_unref (dwz_bfd);
2095 error (_("file '%s' was not usable: %s"), filename,
2096 bfd_errmsg (bfd_get_error ()));
2097 }
2098
2099 result = OBSTACK_ZALLOC (&dwarf2_per_objfile->objfile->objfile_obstack,
2100 struct dwz_file);
2101 result->dwz_bfd = dwz_bfd;
2102
2103 bfd_map_over_sections (dwz_bfd, locate_dwz_sections, result);
2104
2105 do_cleanups (cleanup);
2106
2107 return result;
2108}
9291a0cd 2109\f
7b9f3c50
DE
2110/* DWARF quick_symbols_functions support. */
2111
2112/* TUs can share .debug_line entries, and there can be a lot more TUs than
2113 unique line tables, so we maintain a separate table of all .debug_line
2114 derived entries to support the sharing.
2115 All the quick functions need is the list of file names. We discard the
2116 line_header when we're done and don't need to record it here. */
2117struct quick_file_names
2118{
094b34ac
DE
2119 /* The data used to construct the hash key. */
2120 struct stmt_list_hash hash;
7b9f3c50
DE
2121
2122 /* The number of entries in file_names, real_names. */
2123 unsigned int num_file_names;
2124
2125 /* The file names from the line table, after being run through
2126 file_full_name. */
2127 const char **file_names;
2128
2129 /* The file names from the line table after being run through
2130 gdb_realpath. These are computed lazily. */
2131 const char **real_names;
2132};
2133
2134/* When using the index (and thus not using psymtabs), each CU has an
2135 object of this type. This is used to hold information needed by
2136 the various "quick" methods. */
2137struct dwarf2_per_cu_quick_data
2138{
2139 /* The file table. This can be NULL if there was no file table
2140 or it's currently not read in.
2141 NOTE: This points into dwarf2_per_objfile->quick_file_names_table. */
2142 struct quick_file_names *file_names;
2143
2144 /* The corresponding symbol table. This is NULL if symbols for this
2145 CU have not yet been read. */
2146 struct symtab *symtab;
2147
2148 /* A temporary mark bit used when iterating over all CUs in
2149 expand_symtabs_matching. */
2150 unsigned int mark : 1;
2151
2152 /* True if we've tried to read the file table and found there isn't one.
2153 There will be no point in trying to read it again next time. */
2154 unsigned int no_file_data : 1;
2155};
2156
094b34ac
DE
2157/* Utility hash function for a stmt_list_hash. */
2158
2159static hashval_t
2160hash_stmt_list_entry (const struct stmt_list_hash *stmt_list_hash)
2161{
2162 hashval_t v = 0;
2163
2164 if (stmt_list_hash->dwo_unit != NULL)
2165 v += (uintptr_t) stmt_list_hash->dwo_unit->dwo_file;
2166 v += stmt_list_hash->line_offset.sect_off;
2167 return v;
2168}
2169
2170/* Utility equality function for a stmt_list_hash. */
2171
2172static int
2173eq_stmt_list_entry (const struct stmt_list_hash *lhs,
2174 const struct stmt_list_hash *rhs)
2175{
2176 if ((lhs->dwo_unit != NULL) != (rhs->dwo_unit != NULL))
2177 return 0;
2178 if (lhs->dwo_unit != NULL
2179 && lhs->dwo_unit->dwo_file != rhs->dwo_unit->dwo_file)
2180 return 0;
2181
2182 return lhs->line_offset.sect_off == rhs->line_offset.sect_off;
2183}
2184
7b9f3c50
DE
2185/* Hash function for a quick_file_names. */
2186
2187static hashval_t
2188hash_file_name_entry (const void *e)
2189{
2190 const struct quick_file_names *file_data = e;
2191
094b34ac 2192 return hash_stmt_list_entry (&file_data->hash);
7b9f3c50
DE
2193}
2194
2195/* Equality function for a quick_file_names. */
2196
2197static int
2198eq_file_name_entry (const void *a, const void *b)
2199{
2200 const struct quick_file_names *ea = a;
2201 const struct quick_file_names *eb = b;
2202
094b34ac 2203 return eq_stmt_list_entry (&ea->hash, &eb->hash);
7b9f3c50
DE
2204}
2205
2206/* Delete function for a quick_file_names. */
2207
2208static void
2209delete_file_name_entry (void *e)
2210{
2211 struct quick_file_names *file_data = e;
2212 int i;
2213
2214 for (i = 0; i < file_data->num_file_names; ++i)
2215 {
2216 xfree ((void*) file_data->file_names[i]);
2217 if (file_data->real_names)
2218 xfree ((void*) file_data->real_names[i]);
2219 }
2220
2221 /* The space for the struct itself lives on objfile_obstack,
2222 so we don't free it here. */
2223}
2224
2225/* Create a quick_file_names hash table. */
2226
2227static htab_t
2228create_quick_file_names_table (unsigned int nr_initial_entries)
2229{
2230 return htab_create_alloc (nr_initial_entries,
2231 hash_file_name_entry, eq_file_name_entry,
2232 delete_file_name_entry, xcalloc, xfree);
2233}
9291a0cd 2234
918dd910
JK
2235/* Read in PER_CU->CU. This function is unrelated to symtabs, symtab would
2236 have to be created afterwards. You should call age_cached_comp_units after
2237 processing PER_CU->CU. dw2_setup must have been already called. */
2238
2239static void
2240load_cu (struct dwarf2_per_cu_data *per_cu)
2241{
3019eac3 2242 if (per_cu->is_debug_types)
e5fe5e75 2243 load_full_type_unit (per_cu);
918dd910 2244 else
95554aad 2245 load_full_comp_unit (per_cu, language_minimal);
918dd910 2246
918dd910 2247 gdb_assert (per_cu->cu != NULL);
2dc860c0
DE
2248
2249 dwarf2_find_base_address (per_cu->cu->dies, per_cu->cu);
918dd910
JK
2250}
2251
a0f42c21 2252/* Read in the symbols for PER_CU. */
2fdf6df6 2253
9291a0cd 2254static void
a0f42c21 2255dw2_do_instantiate_symtab (struct dwarf2_per_cu_data *per_cu)
9291a0cd
TT
2256{
2257 struct cleanup *back_to;
2258
f4dc4d17
DE
2259 /* Skip type_unit_groups, reading the type units they contain
2260 is handled elsewhere. */
2261 if (IS_TYPE_UNIT_GROUP (per_cu))
2262 return;
2263
9291a0cd
TT
2264 back_to = make_cleanup (dwarf2_release_queue, NULL);
2265
95554aad
TT
2266 if (dwarf2_per_objfile->using_index
2267 ? per_cu->v.quick->symtab == NULL
2268 : (per_cu->v.psymtab == NULL || !per_cu->v.psymtab->readin))
2269 {
2270 queue_comp_unit (per_cu, language_minimal);
2271 load_cu (per_cu);
2272 }
9291a0cd 2273
a0f42c21 2274 process_queue ();
9291a0cd
TT
2275
2276 /* Age the cache, releasing compilation units that have not
2277 been used recently. */
2278 age_cached_comp_units ();
2279
2280 do_cleanups (back_to);
2281}
2282
2283/* Ensure that the symbols for PER_CU have been read in. OBJFILE is
2284 the objfile from which this CU came. Returns the resulting symbol
2285 table. */
2fdf6df6 2286
9291a0cd 2287static struct symtab *
a0f42c21 2288dw2_instantiate_symtab (struct dwarf2_per_cu_data *per_cu)
9291a0cd 2289{
95554aad 2290 gdb_assert (dwarf2_per_objfile->using_index);
9291a0cd
TT
2291 if (!per_cu->v.quick->symtab)
2292 {
2293 struct cleanup *back_to = make_cleanup (free_cached_comp_units, NULL);
2294 increment_reading_symtab ();
a0f42c21 2295 dw2_do_instantiate_symtab (per_cu);
95554aad 2296 process_cu_includes ();
9291a0cd
TT
2297 do_cleanups (back_to);
2298 }
2299 return per_cu->v.quick->symtab;
2300}
2301
f4dc4d17
DE
2302/* Return the CU given its index.
2303
2304 This is intended for loops like:
2305
2306 for (i = 0; i < (dwarf2_per_objfile->n_comp_units
2307 + dwarf2_per_objfile->n_type_units); ++i)
2308 {
2309 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
2310
2311 ...;
2312 }
2313*/
2fdf6df6 2314
1fd400ff
TT
2315static struct dwarf2_per_cu_data *
2316dw2_get_cu (int index)
2317{
2318 if (index >= dwarf2_per_objfile->n_comp_units)
2319 {
f4dc4d17 2320 index -= dwarf2_per_objfile->n_comp_units;
094b34ac
DE
2321 gdb_assert (index < dwarf2_per_objfile->n_type_units);
2322 return &dwarf2_per_objfile->all_type_units[index]->per_cu;
f4dc4d17
DE
2323 }
2324
2325 return dwarf2_per_objfile->all_comp_units[index];
2326}
2327
2328/* Return the primary CU given its index.
2329 The difference between this function and dw2_get_cu is in the handling
2330 of type units (TUs). Here we return the type_unit_group object.
2331
2332 This is intended for loops like:
2333
2334 for (i = 0; i < (dwarf2_per_objfile->n_comp_units
2335 + dwarf2_per_objfile->n_type_unit_groups); ++i)
2336 {
2337 struct dwarf2_per_cu_data *per_cu = dw2_get_primary_cu (i);
2338
2339 ...;
2340 }
2341*/
2342
2343static struct dwarf2_per_cu_data *
2344dw2_get_primary_cu (int index)
2345{
2346 if (index >= dwarf2_per_objfile->n_comp_units)
2347 {
1fd400ff 2348 index -= dwarf2_per_objfile->n_comp_units;
094b34ac
DE
2349 gdb_assert (index < dwarf2_per_objfile->n_type_unit_groups);
2350 return &dwarf2_per_objfile->all_type_unit_groups[index]->per_cu;
1fd400ff 2351 }
f4dc4d17 2352
1fd400ff
TT
2353 return dwarf2_per_objfile->all_comp_units[index];
2354}
2355
9291a0cd
TT
2356/* A helper function that knows how to read a 64-bit value in a way
2357 that doesn't make gdb die. Returns 1 if the conversion went ok, 0
2358 otherwise. */
2fdf6df6 2359
9291a0cd
TT
2360static int
2361extract_cu_value (const char *bytes, ULONGEST *result)
2362{
2363 if (sizeof (ULONGEST) < 8)
2364 {
2365 int i;
2366
2367 /* Ignore the upper 4 bytes if they are all zero. */
2368 for (i = 0; i < 4; ++i)
2369 if (bytes[i + 4] != 0)
2370 return 0;
2371
2372 *result = extract_unsigned_integer (bytes, 4, BFD_ENDIAN_LITTLE);
2373 }
2374 else
2375 *result = extract_unsigned_integer (bytes, 8, BFD_ENDIAN_LITTLE);
2376 return 1;
2377}
2378
2ec9a5e0
TT
2379/* A helper for create_cus_from_index that handles a given list of
2380 CUs. */
2fdf6df6 2381
9291a0cd 2382static int
2ec9a5e0
TT
2383create_cus_from_index_list (struct objfile *objfile,
2384 const gdb_byte *cu_list, offset_type n_elements,
2385 struct dwarf2_section_info *section,
2386 int is_dwz,
2387 int base_offset)
9291a0cd
TT
2388{
2389 offset_type i;
9291a0cd 2390
2ec9a5e0 2391 for (i = 0; i < n_elements; i += 2)
9291a0cd
TT
2392 {
2393 struct dwarf2_per_cu_data *the_cu;
2394 ULONGEST offset, length;
2395
2396 if (!extract_cu_value (cu_list, &offset)
2397 || !extract_cu_value (cu_list + 8, &length))
2398 return 0;
2399 cu_list += 2 * 8;
2400
2401 the_cu = OBSTACK_ZALLOC (&objfile->objfile_obstack,
2402 struct dwarf2_per_cu_data);
b64f50a1 2403 the_cu->offset.sect_off = offset;
9291a0cd
TT
2404 the_cu->length = length;
2405 the_cu->objfile = objfile;
2ec9a5e0 2406 the_cu->info_or_types_section = section;
9291a0cd
TT
2407 the_cu->v.quick = OBSTACK_ZALLOC (&objfile->objfile_obstack,
2408 struct dwarf2_per_cu_quick_data);
2ec9a5e0
TT
2409 the_cu->is_dwz = is_dwz;
2410 dwarf2_per_objfile->all_comp_units[base_offset + i / 2] = the_cu;
9291a0cd
TT
2411 }
2412
2413 return 1;
2414}
2415
2ec9a5e0
TT
2416/* Read the CU list from the mapped index, and use it to create all
2417 the CU objects for this objfile. Return 0 if something went wrong,
2418 1 if everything went ok. */
2419
2420static int
2421create_cus_from_index (struct objfile *objfile,
2422 const gdb_byte *cu_list, offset_type cu_list_elements,
2423 const gdb_byte *dwz_list, offset_type dwz_elements)
2424{
2425 struct dwz_file *dwz;
2426
2427 dwarf2_per_objfile->n_comp_units = (cu_list_elements + dwz_elements) / 2;
2428 dwarf2_per_objfile->all_comp_units
2429 = obstack_alloc (&objfile->objfile_obstack,
2430 dwarf2_per_objfile->n_comp_units
2431 * sizeof (struct dwarf2_per_cu_data *));
2432
2433 if (!create_cus_from_index_list (objfile, cu_list, cu_list_elements,
2434 &dwarf2_per_objfile->info, 0, 0))
2435 return 0;
2436
2437 if (dwz_elements == 0)
2438 return 1;
2439
2440 dwz = dwarf2_get_dwz_file ();
2441 return create_cus_from_index_list (objfile, dwz_list, dwz_elements,
2442 &dwz->info, 1, cu_list_elements / 2);
2443}
2444
1fd400ff 2445/* Create the signatured type hash table from the index. */
673bfd45 2446
1fd400ff 2447static int
673bfd45 2448create_signatured_type_table_from_index (struct objfile *objfile,
8b70b953 2449 struct dwarf2_section_info *section,
673bfd45
DE
2450 const gdb_byte *bytes,
2451 offset_type elements)
1fd400ff
TT
2452{
2453 offset_type i;
673bfd45 2454 htab_t sig_types_hash;
1fd400ff 2455
d467dd73
DE
2456 dwarf2_per_objfile->n_type_units = elements / 3;
2457 dwarf2_per_objfile->all_type_units
1fd400ff 2458 = obstack_alloc (&objfile->objfile_obstack,
d467dd73 2459 dwarf2_per_objfile->n_type_units
b4dd5633 2460 * sizeof (struct signatured_type *));
1fd400ff 2461
673bfd45 2462 sig_types_hash = allocate_signatured_type_table (objfile);
1fd400ff
TT
2463
2464 for (i = 0; i < elements; i += 3)
2465 {
52dc124a
DE
2466 struct signatured_type *sig_type;
2467 ULONGEST offset, type_offset_in_tu, signature;
1fd400ff
TT
2468 void **slot;
2469
2470 if (!extract_cu_value (bytes, &offset)
52dc124a 2471 || !extract_cu_value (bytes + 8, &type_offset_in_tu))
1fd400ff
TT
2472 return 0;
2473 signature = extract_unsigned_integer (bytes + 16, 8, BFD_ENDIAN_LITTLE);
2474 bytes += 3 * 8;
2475
52dc124a 2476 sig_type = OBSTACK_ZALLOC (&objfile->objfile_obstack,
1fd400ff 2477 struct signatured_type);
52dc124a 2478 sig_type->signature = signature;
3019eac3
DE
2479 sig_type->type_offset_in_tu.cu_off = type_offset_in_tu;
2480 sig_type->per_cu.is_debug_types = 1;
2481 sig_type->per_cu.info_or_types_section = section;
52dc124a
DE
2482 sig_type->per_cu.offset.sect_off = offset;
2483 sig_type->per_cu.objfile = objfile;
2484 sig_type->per_cu.v.quick
1fd400ff
TT
2485 = OBSTACK_ZALLOC (&objfile->objfile_obstack,
2486 struct dwarf2_per_cu_quick_data);
2487
52dc124a
DE
2488 slot = htab_find_slot (sig_types_hash, sig_type, INSERT);
2489 *slot = sig_type;
1fd400ff 2490
b4dd5633 2491 dwarf2_per_objfile->all_type_units[i / 3] = sig_type;
1fd400ff
TT
2492 }
2493
673bfd45 2494 dwarf2_per_objfile->signatured_types = sig_types_hash;
1fd400ff
TT
2495
2496 return 1;
2497}
2498
9291a0cd
TT
2499/* Read the address map data from the mapped index, and use it to
2500 populate the objfile's psymtabs_addrmap. */
2fdf6df6 2501
9291a0cd
TT
2502static void
2503create_addrmap_from_index (struct objfile *objfile, struct mapped_index *index)
2504{
2505 const gdb_byte *iter, *end;
2506 struct obstack temp_obstack;
2507 struct addrmap *mutable_map;
2508 struct cleanup *cleanup;
2509 CORE_ADDR baseaddr;
2510
2511 obstack_init (&temp_obstack);
2512 cleanup = make_cleanup_obstack_free (&temp_obstack);
2513 mutable_map = addrmap_create_mutable (&temp_obstack);
2514
2515 iter = index->address_table;
2516 end = iter + index->address_table_size;
2517
2518 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
2519
2520 while (iter < end)
2521 {
2522 ULONGEST hi, lo, cu_index;
2523 lo = extract_unsigned_integer (iter, 8, BFD_ENDIAN_LITTLE);
2524 iter += 8;
2525 hi = extract_unsigned_integer (iter, 8, BFD_ENDIAN_LITTLE);
2526 iter += 8;
2527 cu_index = extract_unsigned_integer (iter, 4, BFD_ENDIAN_LITTLE);
2528 iter += 4;
2529
2530 addrmap_set_empty (mutable_map, lo + baseaddr, hi + baseaddr - 1,
1fd400ff 2531 dw2_get_cu (cu_index));
9291a0cd
TT
2532 }
2533
2534 objfile->psymtabs_addrmap = addrmap_create_fixed (mutable_map,
2535 &objfile->objfile_obstack);
2536 do_cleanups (cleanup);
2537}
2538
59d7bcaf
JK
2539/* The hash function for strings in the mapped index. This is the same as
2540 SYMBOL_HASH_NEXT, but we keep a separate copy to maintain control over the
2541 implementation. This is necessary because the hash function is tied to the
2542 format of the mapped index file. The hash values do not have to match with
559a7a62
JK
2543 SYMBOL_HASH_NEXT.
2544
2545 Use INT_MAX for INDEX_VERSION if you generate the current index format. */
2fdf6df6 2546
9291a0cd 2547static hashval_t
559a7a62 2548mapped_index_string_hash (int index_version, const void *p)
9291a0cd
TT
2549{
2550 const unsigned char *str = (const unsigned char *) p;
2551 hashval_t r = 0;
2552 unsigned char c;
2553
2554 while ((c = *str++) != 0)
559a7a62
JK
2555 {
2556 if (index_version >= 5)
2557 c = tolower (c);
2558 r = r * 67 + c - 113;
2559 }
9291a0cd
TT
2560
2561 return r;
2562}
2563
2564/* Find a slot in the mapped index INDEX for the object named NAME.
2565 If NAME is found, set *VEC_OUT to point to the CU vector in the
2566 constant pool and return 1. If NAME cannot be found, return 0. */
2fdf6df6 2567
9291a0cd
TT
2568static int
2569find_slot_in_mapped_hash (struct mapped_index *index, const char *name,
2570 offset_type **vec_out)
2571{
0cf03b49
JK
2572 struct cleanup *back_to = make_cleanup (null_cleanup, 0);
2573 offset_type hash;
9291a0cd 2574 offset_type slot, step;
559a7a62 2575 int (*cmp) (const char *, const char *);
9291a0cd 2576
0cf03b49
JK
2577 if (current_language->la_language == language_cplus
2578 || current_language->la_language == language_java
2579 || current_language->la_language == language_fortran)
2580 {
2581 /* NAME is already canonical. Drop any qualifiers as .gdb_index does
2582 not contain any. */
2583 const char *paren = strchr (name, '(');
2584
2585 if (paren)
2586 {
2587 char *dup;
2588
2589 dup = xmalloc (paren - name + 1);
2590 memcpy (dup, name, paren - name);
2591 dup[paren - name] = 0;
2592
2593 make_cleanup (xfree, dup);
2594 name = dup;
2595 }
2596 }
2597
559a7a62 2598 /* Index version 4 did not support case insensitive searches. But the
feea76c2 2599 indices for case insensitive languages are built in lowercase, therefore
559a7a62
JK
2600 simulate our NAME being searched is also lowercased. */
2601 hash = mapped_index_string_hash ((index->version == 4
2602 && case_sensitivity == case_sensitive_off
2603 ? 5 : index->version),
2604 name);
2605
3876f04e
DE
2606 slot = hash & (index->symbol_table_slots - 1);
2607 step = ((hash * 17) & (index->symbol_table_slots - 1)) | 1;
559a7a62 2608 cmp = (case_sensitivity == case_sensitive_on ? strcmp : strcasecmp);
9291a0cd
TT
2609
2610 for (;;)
2611 {
2612 /* Convert a slot number to an offset into the table. */
2613 offset_type i = 2 * slot;
2614 const char *str;
3876f04e 2615 if (index->symbol_table[i] == 0 && index->symbol_table[i + 1] == 0)
0cf03b49
JK
2616 {
2617 do_cleanups (back_to);
2618 return 0;
2619 }
9291a0cd 2620
3876f04e 2621 str = index->constant_pool + MAYBE_SWAP (index->symbol_table[i]);
559a7a62 2622 if (!cmp (name, str))
9291a0cd
TT
2623 {
2624 *vec_out = (offset_type *) (index->constant_pool
3876f04e 2625 + MAYBE_SWAP (index->symbol_table[i + 1]));
0cf03b49 2626 do_cleanups (back_to);
9291a0cd
TT
2627 return 1;
2628 }
2629
3876f04e 2630 slot = (slot + step) & (index->symbol_table_slots - 1);
9291a0cd
TT
2631 }
2632}
2633
2ec9a5e0
TT
2634/* A helper function that reads the .gdb_index from SECTION and fills
2635 in MAP. FILENAME is the name of the file containing the section;
2636 it is used for error reporting. DEPRECATED_OK is nonzero if it is
2637 ok to use deprecated sections.
2638
2639 CU_LIST, CU_LIST_ELEMENTS, TYPES_LIST, and TYPES_LIST_ELEMENTS are
2640 out parameters that are filled in with information about the CU and
2641 TU lists in the section.
2642
2643 Returns 1 if all went well, 0 otherwise. */
2fdf6df6 2644
9291a0cd 2645static int
2ec9a5e0
TT
2646read_index_from_section (struct objfile *objfile,
2647 const char *filename,
2648 int deprecated_ok,
2649 struct dwarf2_section_info *section,
2650 struct mapped_index *map,
2651 const gdb_byte **cu_list,
2652 offset_type *cu_list_elements,
2653 const gdb_byte **types_list,
2654 offset_type *types_list_elements)
9291a0cd 2655{
9291a0cd 2656 char *addr;
2ec9a5e0 2657 offset_type version;
b3b272e1 2658 offset_type *metadata;
1fd400ff 2659 int i;
9291a0cd 2660
2ec9a5e0 2661 if (dwarf2_section_empty_p (section))
9291a0cd 2662 return 0;
82430852
JK
2663
2664 /* Older elfutils strip versions could keep the section in the main
2665 executable while splitting it for the separate debug info file. */
2ec9a5e0 2666 if ((bfd_get_file_flags (section->asection) & SEC_HAS_CONTENTS) == 0)
82430852
JK
2667 return 0;
2668
2ec9a5e0 2669 dwarf2_read_section (objfile, section);
9291a0cd 2670
2ec9a5e0 2671 addr = section->buffer;
9291a0cd 2672 /* Version check. */
1fd400ff 2673 version = MAYBE_SWAP (*(offset_type *) addr);
987d643c 2674 /* Versions earlier than 3 emitted every copy of a psymbol. This
a6e293d1 2675 causes the index to behave very poorly for certain requests. Version 3
831adc1f 2676 contained incomplete addrmap. So, it seems better to just ignore such
481860b3 2677 indices. */
831adc1f 2678 if (version < 4)
481860b3
GB
2679 {
2680 static int warning_printed = 0;
2681 if (!warning_printed)
2682 {
2683 warning (_("Skipping obsolete .gdb_index section in %s."),
2ec9a5e0 2684 filename);
481860b3
GB
2685 warning_printed = 1;
2686 }
2687 return 0;
2688 }
2689 /* Index version 4 uses a different hash function than index version
2690 5 and later.
2691
2692 Versions earlier than 6 did not emit psymbols for inlined
2693 functions. Using these files will cause GDB not to be able to
2694 set breakpoints on inlined functions by name, so we ignore these
e615022a
DE
2695 indices unless the user has done
2696 "set use-deprecated-index-sections on". */
2ec9a5e0 2697 if (version < 6 && !deprecated_ok)
481860b3
GB
2698 {
2699 static int warning_printed = 0;
2700 if (!warning_printed)
2701 {
e615022a
DE
2702 warning (_("\
2703Skipping deprecated .gdb_index section in %s.\n\
2704Do \"set use-deprecated-index-sections on\" before the file is read\n\
2705to use the section anyway."),
2ec9a5e0 2706 filename);
481860b3
GB
2707 warning_printed = 1;
2708 }
2709 return 0;
2710 }
2711 /* Indexes with higher version than the one supported by GDB may be no
594e8718 2712 longer backward compatible. */
156942c7 2713 if (version > 7)
594e8718 2714 return 0;
9291a0cd 2715
559a7a62 2716 map->version = version;
2ec9a5e0 2717 map->total_size = section->size;
9291a0cd
TT
2718
2719 metadata = (offset_type *) (addr + sizeof (offset_type));
1fd400ff
TT
2720
2721 i = 0;
2ec9a5e0
TT
2722 *cu_list = addr + MAYBE_SWAP (metadata[i]);
2723 *cu_list_elements = ((MAYBE_SWAP (metadata[i + 1]) - MAYBE_SWAP (metadata[i]))
2724 / 8);
1fd400ff
TT
2725 ++i;
2726
2ec9a5e0
TT
2727 *types_list = addr + MAYBE_SWAP (metadata[i]);
2728 *types_list_elements = ((MAYBE_SWAP (metadata[i + 1])
2729 - MAYBE_SWAP (metadata[i]))
2730 / 8);
987d643c 2731 ++i;
1fd400ff
TT
2732
2733 map->address_table = addr + MAYBE_SWAP (metadata[i]);
2734 map->address_table_size = (MAYBE_SWAP (metadata[i + 1])
2735 - MAYBE_SWAP (metadata[i]));
2736 ++i;
2737
3876f04e
DE
2738 map->symbol_table = (offset_type *) (addr + MAYBE_SWAP (metadata[i]));
2739 map->symbol_table_slots = ((MAYBE_SWAP (metadata[i + 1])
2740 - MAYBE_SWAP (metadata[i]))
2741 / (2 * sizeof (offset_type)));
1fd400ff 2742 ++i;
9291a0cd 2743
1fd400ff
TT
2744 map->constant_pool = addr + MAYBE_SWAP (metadata[i]);
2745
2ec9a5e0
TT
2746 return 1;
2747}
2748
2749
2750/* Read the index file. If everything went ok, initialize the "quick"
2751 elements of all the CUs and return 1. Otherwise, return 0. */
2752
2753static int
2754dwarf2_read_index (struct objfile *objfile)
2755{
2756 struct mapped_index local_map, *map;
2757 const gdb_byte *cu_list, *types_list, *dwz_list = NULL;
2758 offset_type cu_list_elements, types_list_elements, dwz_list_elements = 0;
2759
2760 if (!read_index_from_section (objfile, objfile->name,
2761 use_deprecated_index_sections,
2762 &dwarf2_per_objfile->gdb_index, &local_map,
2763 &cu_list, &cu_list_elements,
2764 &types_list, &types_list_elements))
2765 return 0;
2766
0fefef59 2767 /* Don't use the index if it's empty. */
2ec9a5e0 2768 if (local_map.symbol_table_slots == 0)
0fefef59
DE
2769 return 0;
2770
2ec9a5e0
TT
2771 /* If there is a .dwz file, read it so we can get its CU list as
2772 well. */
2773 if (bfd_get_section_by_name (objfile->obfd, ".gnu_debugaltlink") != NULL)
2774 {
2775 struct dwz_file *dwz = dwarf2_get_dwz_file ();
2776 struct mapped_index dwz_map;
2777 const gdb_byte *dwz_types_ignore;
2778 offset_type dwz_types_elements_ignore;
2779
2780 if (!read_index_from_section (objfile, bfd_get_filename (dwz->dwz_bfd),
2781 1,
2782 &dwz->gdb_index, &dwz_map,
2783 &dwz_list, &dwz_list_elements,
2784 &dwz_types_ignore,
2785 &dwz_types_elements_ignore))
2786 {
2787 warning (_("could not read '.gdb_index' section from %s; skipping"),
2788 bfd_get_filename (dwz->dwz_bfd));
2789 return 0;
2790 }
2791 }
2792
2793 if (!create_cus_from_index (objfile, cu_list, cu_list_elements,
2794 dwz_list, dwz_list_elements))
1fd400ff
TT
2795 return 0;
2796
8b70b953
TT
2797 if (types_list_elements)
2798 {
2799 struct dwarf2_section_info *section;
2800
2801 /* We can only handle a single .debug_types when we have an
2802 index. */
2803 if (VEC_length (dwarf2_section_info_def, dwarf2_per_objfile->types) != 1)
2804 return 0;
2805
2806 section = VEC_index (dwarf2_section_info_def,
2807 dwarf2_per_objfile->types, 0);
2808
2809 if (!create_signatured_type_table_from_index (objfile, section,
2810 types_list,
2811 types_list_elements))
2812 return 0;
2813 }
9291a0cd 2814
2ec9a5e0
TT
2815 create_addrmap_from_index (objfile, &local_map);
2816
2817 map = obstack_alloc (&objfile->objfile_obstack, sizeof (struct mapped_index));
2818 *map = local_map;
9291a0cd
TT
2819
2820 dwarf2_per_objfile->index_table = map;
2821 dwarf2_per_objfile->using_index = 1;
7b9f3c50
DE
2822 dwarf2_per_objfile->quick_file_names_table =
2823 create_quick_file_names_table (dwarf2_per_objfile->n_comp_units);
9291a0cd
TT
2824
2825 return 1;
2826}
2827
2828/* A helper for the "quick" functions which sets the global
2829 dwarf2_per_objfile according to OBJFILE. */
2fdf6df6 2830
9291a0cd
TT
2831static void
2832dw2_setup (struct objfile *objfile)
2833{
2834 dwarf2_per_objfile = objfile_data (objfile, dwarf2_objfile_data_key);
2835 gdb_assert (dwarf2_per_objfile);
2836}
2837
f4dc4d17
DE
2838/* Reader function for dw2_build_type_unit_groups. */
2839
2840static void
2841dw2_build_type_unit_groups_reader (const struct die_reader_specs *reader,
2842 gdb_byte *info_ptr,
2843 struct die_info *type_unit_die,
2844 int has_children,
2845 void *data)
2846{
2847 struct dwarf2_cu *cu = reader->cu;
f4dc4d17
DE
2848 struct attribute *attr;
2849 struct type_unit_group *tu_group;
2850
2851 gdb_assert (data == NULL);
2852
2853 if (! has_children)
2854 return;
2855
2856 attr = dwarf2_attr_no_follow (type_unit_die, DW_AT_stmt_list);
2857 /* Call this for its side-effect of creating the associated
2858 struct type_unit_group if it doesn't already exist. */
094b34ac 2859 tu_group = get_type_unit_group (cu, attr);
f4dc4d17
DE
2860}
2861
2862/* Build dwarf2_per_objfile->type_unit_groups.
2863 This function may be called multiple times. */
2864
2865static void
2866dw2_build_type_unit_groups (void)
2867{
2868 if (dwarf2_per_objfile->type_unit_groups == NULL)
2869 build_type_unit_groups (dw2_build_type_unit_groups_reader, NULL);
2870}
2871
dee91e82 2872/* die_reader_func for dw2_get_file_names. */
2fdf6df6 2873
dee91e82
DE
2874static void
2875dw2_get_file_names_reader (const struct die_reader_specs *reader,
2876 gdb_byte *info_ptr,
2877 struct die_info *comp_unit_die,
2878 int has_children,
2879 void *data)
9291a0cd 2880{
dee91e82
DE
2881 struct dwarf2_cu *cu = reader->cu;
2882 struct dwarf2_per_cu_data *this_cu = cu->per_cu;
2883 struct objfile *objfile = dwarf2_per_objfile->objfile;
094b34ac 2884 struct dwarf2_per_cu_data *lh_cu;
7b9f3c50 2885 struct line_header *lh;
9291a0cd 2886 struct attribute *attr;
dee91e82 2887 int i;
9291a0cd 2888 char *name, *comp_dir;
7b9f3c50
DE
2889 void **slot;
2890 struct quick_file_names *qfn;
2891 unsigned int line_offset;
9291a0cd 2892
07261596
TT
2893 /* Our callers never want to match partial units -- instead they
2894 will match the enclosing full CU. */
2895 if (comp_unit_die->tag == DW_TAG_partial_unit)
2896 {
2897 this_cu->v.quick->no_file_data = 1;
2898 return;
2899 }
2900
094b34ac
DE
2901 /* If we're reading the line header for TUs, store it in the "per_cu"
2902 for tu_group. */
2903 if (this_cu->is_debug_types)
2904 {
2905 struct type_unit_group *tu_group = data;
2906
2907 gdb_assert (tu_group != NULL);
2908 lh_cu = &tu_group->per_cu;
2909 }
2910 else
2911 lh_cu = this_cu;
2912
7b9f3c50
DE
2913 lh = NULL;
2914 slot = NULL;
2915 line_offset = 0;
dee91e82
DE
2916
2917 attr = dwarf2_attr (comp_unit_die, DW_AT_stmt_list, cu);
9291a0cd
TT
2918 if (attr)
2919 {
7b9f3c50
DE
2920 struct quick_file_names find_entry;
2921
2922 line_offset = DW_UNSND (attr);
2923
2924 /* We may have already read in this line header (TU line header sharing).
2925 If we have we're done. */
094b34ac
DE
2926 find_entry.hash.dwo_unit = cu->dwo_unit;
2927 find_entry.hash.line_offset.sect_off = line_offset;
7b9f3c50
DE
2928 slot = htab_find_slot (dwarf2_per_objfile->quick_file_names_table,
2929 &find_entry, INSERT);
2930 if (*slot != NULL)
2931 {
094b34ac 2932 lh_cu->v.quick->file_names = *slot;
dee91e82 2933 return;
7b9f3c50
DE
2934 }
2935
3019eac3 2936 lh = dwarf_decode_line_header (line_offset, cu);
9291a0cd
TT
2937 }
2938 if (lh == NULL)
2939 {
094b34ac 2940 lh_cu->v.quick->no_file_data = 1;
dee91e82 2941 return;
9291a0cd
TT
2942 }
2943
7b9f3c50 2944 qfn = obstack_alloc (&objfile->objfile_obstack, sizeof (*qfn));
094b34ac
DE
2945 qfn->hash.dwo_unit = cu->dwo_unit;
2946 qfn->hash.line_offset.sect_off = line_offset;
7b9f3c50
DE
2947 gdb_assert (slot != NULL);
2948 *slot = qfn;
9291a0cd 2949
dee91e82 2950 find_file_and_directory (comp_unit_die, cu, &name, &comp_dir);
9291a0cd 2951
7b9f3c50
DE
2952 qfn->num_file_names = lh->num_file_names;
2953 qfn->file_names = obstack_alloc (&objfile->objfile_obstack,
2954 lh->num_file_names * sizeof (char *));
9291a0cd 2955 for (i = 0; i < lh->num_file_names; ++i)
7b9f3c50
DE
2956 qfn->file_names[i] = file_full_name (i + 1, lh, comp_dir);
2957 qfn->real_names = NULL;
9291a0cd 2958
7b9f3c50 2959 free_line_header (lh);
7b9f3c50 2960
094b34ac 2961 lh_cu->v.quick->file_names = qfn;
dee91e82
DE
2962}
2963
2964/* A helper for the "quick" functions which attempts to read the line
2965 table for THIS_CU. */
2966
2967static struct quick_file_names *
2968dw2_get_file_names (struct objfile *objfile,
2969 struct dwarf2_per_cu_data *this_cu)
2970{
f4dc4d17
DE
2971 /* For TUs this should only be called on the parent group. */
2972 if (this_cu->is_debug_types)
2973 gdb_assert (IS_TYPE_UNIT_GROUP (this_cu));
2974
dee91e82
DE
2975 if (this_cu->v.quick->file_names != NULL)
2976 return this_cu->v.quick->file_names;
2977 /* If we know there is no line data, no point in looking again. */
2978 if (this_cu->v.quick->no_file_data)
2979 return NULL;
2980
3019eac3
DE
2981 /* If DWO files are in use, we can still find the DW_AT_stmt_list attribute
2982 in the stub for CUs, there's is no need to lookup the DWO file.
2983 However, that's not the case for TUs where DW_AT_stmt_list lives in the
2984 DWO file. */
2985 if (this_cu->is_debug_types)
094b34ac
DE
2986 {
2987 struct type_unit_group *tu_group = this_cu->s.type_unit_group;
2988
2989 init_cutu_and_read_dies (tu_group->t.first_tu, NULL, 0, 0,
2990 dw2_get_file_names_reader, tu_group);
2991 }
3019eac3
DE
2992 else
2993 init_cutu_and_read_dies_simple (this_cu, dw2_get_file_names_reader, NULL);
dee91e82
DE
2994
2995 if (this_cu->v.quick->no_file_data)
2996 return NULL;
2997 return this_cu->v.quick->file_names;
9291a0cd
TT
2998}
2999
3000/* A helper for the "quick" functions which computes and caches the
7b9f3c50 3001 real path for a given file name from the line table. */
2fdf6df6 3002
9291a0cd 3003static const char *
7b9f3c50
DE
3004dw2_get_real_path (struct objfile *objfile,
3005 struct quick_file_names *qfn, int index)
9291a0cd 3006{
7b9f3c50
DE
3007 if (qfn->real_names == NULL)
3008 qfn->real_names = OBSTACK_CALLOC (&objfile->objfile_obstack,
3009 qfn->num_file_names, sizeof (char *));
9291a0cd 3010
7b9f3c50
DE
3011 if (qfn->real_names[index] == NULL)
3012 qfn->real_names[index] = gdb_realpath (qfn->file_names[index]);
9291a0cd 3013
7b9f3c50 3014 return qfn->real_names[index];
9291a0cd
TT
3015}
3016
3017static struct symtab *
3018dw2_find_last_source_symtab (struct objfile *objfile)
3019{
3020 int index;
ae2de4f8 3021
9291a0cd
TT
3022 dw2_setup (objfile);
3023 index = dwarf2_per_objfile->n_comp_units - 1;
a0f42c21 3024 return dw2_instantiate_symtab (dw2_get_cu (index));
9291a0cd
TT
3025}
3026
7b9f3c50
DE
3027/* Traversal function for dw2_forget_cached_source_info. */
3028
3029static int
3030dw2_free_cached_file_names (void **slot, void *info)
9291a0cd 3031{
7b9f3c50 3032 struct quick_file_names *file_data = (struct quick_file_names *) *slot;
9291a0cd 3033
7b9f3c50 3034 if (file_data->real_names)
9291a0cd 3035 {
7b9f3c50 3036 int i;
9291a0cd 3037
7b9f3c50 3038 for (i = 0; i < file_data->num_file_names; ++i)
9291a0cd 3039 {
7b9f3c50
DE
3040 xfree ((void*) file_data->real_names[i]);
3041 file_data->real_names[i] = NULL;
9291a0cd
TT
3042 }
3043 }
7b9f3c50
DE
3044
3045 return 1;
3046}
3047
3048static void
3049dw2_forget_cached_source_info (struct objfile *objfile)
3050{
3051 dw2_setup (objfile);
3052
3053 htab_traverse_noresize (dwarf2_per_objfile->quick_file_names_table,
3054 dw2_free_cached_file_names, NULL);
9291a0cd
TT
3055}
3056
f8eba3c6
TT
3057/* Helper function for dw2_map_symtabs_matching_filename that expands
3058 the symtabs and calls the iterator. */
3059
3060static int
3061dw2_map_expand_apply (struct objfile *objfile,
3062 struct dwarf2_per_cu_data *per_cu,
3063 const char *name,
3064 const char *full_path, const char *real_path,
3065 int (*callback) (struct symtab *, void *),
3066 void *data)
3067{
3068 struct symtab *last_made = objfile->symtabs;
3069
3070 /* Don't visit already-expanded CUs. */
3071 if (per_cu->v.quick->symtab)
3072 return 0;
3073
3074 /* This may expand more than one symtab, and we want to iterate over
3075 all of them. */
a0f42c21 3076 dw2_instantiate_symtab (per_cu);
f8eba3c6
TT
3077
3078 return iterate_over_some_symtabs (name, full_path, real_path, callback, data,
3079 objfile->symtabs, last_made);
3080}
3081
3082/* Implementation of the map_symtabs_matching_filename method. */
3083
9291a0cd 3084static int
f8eba3c6
TT
3085dw2_map_symtabs_matching_filename (struct objfile *objfile, const char *name,
3086 const char *full_path, const char *real_path,
3087 int (*callback) (struct symtab *, void *),
3088 void *data)
9291a0cd
TT
3089{
3090 int i;
c011a4f4 3091 const char *name_basename = lbasename (name);
4aac40c8
TT
3092 int name_len = strlen (name);
3093 int is_abs = IS_ABSOLUTE_PATH (name);
9291a0cd
TT
3094
3095 dw2_setup (objfile);
ae2de4f8 3096
f4dc4d17
DE
3097 dw2_build_type_unit_groups ();
3098
1fd400ff 3099 for (i = 0; i < (dwarf2_per_objfile->n_comp_units
f4dc4d17 3100 + dwarf2_per_objfile->n_type_unit_groups); ++i)
9291a0cd
TT
3101 {
3102 int j;
f4dc4d17 3103 struct dwarf2_per_cu_data *per_cu = dw2_get_primary_cu (i);
7b9f3c50 3104 struct quick_file_names *file_data;
9291a0cd 3105
3d7bb9d9 3106 /* We only need to look at symtabs not already expanded. */
e254ef6a 3107 if (per_cu->v.quick->symtab)
9291a0cd
TT
3108 continue;
3109
7b9f3c50
DE
3110 file_data = dw2_get_file_names (objfile, per_cu);
3111 if (file_data == NULL)
9291a0cd
TT
3112 continue;
3113
7b9f3c50 3114 for (j = 0; j < file_data->num_file_names; ++j)
9291a0cd 3115 {
7b9f3c50 3116 const char *this_name = file_data->file_names[j];
9291a0cd 3117
4aac40c8
TT
3118 if (FILENAME_CMP (name, this_name) == 0
3119 || (!is_abs && compare_filenames_for_search (this_name,
3120 name, name_len)))
9291a0cd 3121 {
f8eba3c6
TT
3122 if (dw2_map_expand_apply (objfile, per_cu,
3123 name, full_path, real_path,
3124 callback, data))
3125 return 1;
4aac40c8 3126 }
9291a0cd 3127
c011a4f4
DE
3128 /* Before we invoke realpath, which can get expensive when many
3129 files are involved, do a quick comparison of the basenames. */
3130 if (! basenames_may_differ
3131 && FILENAME_CMP (lbasename (this_name), name_basename) != 0)
3132 continue;
3133
9291a0cd
TT
3134 if (full_path != NULL)
3135 {
7b9f3c50
DE
3136 const char *this_real_name = dw2_get_real_path (objfile,
3137 file_data, j);
9291a0cd 3138
7b9f3c50 3139 if (this_real_name != NULL
4aac40c8
TT
3140 && (FILENAME_CMP (full_path, this_real_name) == 0
3141 || (!is_abs
3142 && compare_filenames_for_search (this_real_name,
3143 name, name_len))))
9291a0cd 3144 {
f8eba3c6
TT
3145 if (dw2_map_expand_apply (objfile, per_cu,
3146 name, full_path, real_path,
3147 callback, data))
3148 return 1;
9291a0cd
TT
3149 }
3150 }
3151
3152 if (real_path != NULL)
3153 {
7b9f3c50
DE
3154 const char *this_real_name = dw2_get_real_path (objfile,
3155 file_data, j);
9291a0cd 3156
7b9f3c50 3157 if (this_real_name != NULL
4aac40c8
TT
3158 && (FILENAME_CMP (real_path, this_real_name) == 0
3159 || (!is_abs
3160 && compare_filenames_for_search (this_real_name,
3161 name, name_len))))
9291a0cd 3162 {
f8eba3c6
TT
3163 if (dw2_map_expand_apply (objfile, per_cu,
3164 name, full_path, real_path,
3165 callback, data))
3166 return 1;
9291a0cd
TT
3167 }
3168 }
3169 }
3170 }
3171
9291a0cd
TT
3172 return 0;
3173}
3174
3175static struct symtab *
3176dw2_lookup_symbol (struct objfile *objfile, int block_index,
3177 const char *name, domain_enum domain)
3178{
774b6a14 3179 /* We do all the work in the pre_expand_symtabs_matching hook
9291a0cd
TT
3180 instead. */
3181 return NULL;
3182}
3183
3184/* A helper function that expands all symtabs that hold an object
156942c7
DE
3185 named NAME. If WANT_SPECIFIC_BLOCK is non-zero, only look for
3186 symbols in block BLOCK_KIND. */
2fdf6df6 3187
9291a0cd 3188static void
156942c7
DE
3189dw2_do_expand_symtabs_matching (struct objfile *objfile,
3190 int want_specific_block,
3191 enum block_enum block_kind,
3192 const char *name, domain_enum domain)
9291a0cd 3193{
156942c7
DE
3194 struct mapped_index *index;
3195
9291a0cd
TT
3196 dw2_setup (objfile);
3197
156942c7
DE
3198 index = dwarf2_per_objfile->index_table;
3199
ae2de4f8 3200 /* index_table is NULL if OBJF_READNOW. */
156942c7 3201 if (index)
9291a0cd
TT
3202 {
3203 offset_type *vec;
3204
156942c7 3205 if (find_slot_in_mapped_hash (index, name, &vec))
9291a0cd
TT
3206 {
3207 offset_type i, len = MAYBE_SWAP (*vec);
3208 for (i = 0; i < len; ++i)
3209 {
156942c7
DE
3210 offset_type cu_index_and_attrs = MAYBE_SWAP (vec[i + 1]);
3211 offset_type cu_index = GDB_INDEX_CU_VALUE (cu_index_and_attrs);
e254ef6a 3212 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (cu_index);
156942c7
DE
3213 int want_static = block_kind != GLOBAL_BLOCK;
3214 /* This value is only valid for index versions >= 7. */
3215 int is_static = GDB_INDEX_SYMBOL_STATIC_VALUE (cu_index_and_attrs);
3216 gdb_index_symbol_kind symbol_kind =
3217 GDB_INDEX_SYMBOL_KIND_VALUE (cu_index_and_attrs);
83a788b8
DE
3218 /* Only check the symbol attributes if they're present.
3219 Indices prior to version 7 don't record them,
3220 and indices >= 7 may elide them for certain symbols
3221 (gold does this). */
3222 int attrs_valid =
3223 (index->version >= 7
3224 && symbol_kind != GDB_INDEX_SYMBOL_KIND_NONE);
3225
3226 if (attrs_valid
3227 && want_specific_block
156942c7
DE
3228 && want_static != is_static)
3229 continue;
3230
83a788b8
DE
3231 /* Only check the symbol's kind if it has one. */
3232 if (attrs_valid)
156942c7
DE
3233 {
3234 switch (domain)
3235 {
3236 case VAR_DOMAIN:
3237 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_VARIABLE
3238 && symbol_kind != GDB_INDEX_SYMBOL_KIND_FUNCTION
3239 /* Some types are also in VAR_DOMAIN. */
3240 && symbol_kind != GDB_INDEX_SYMBOL_KIND_TYPE)
3241 continue;
3242 break;
3243 case STRUCT_DOMAIN:
3244 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_TYPE)
3245 continue;
3246 break;
3247 case LABEL_DOMAIN:
3248 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_OTHER)
3249 continue;
3250 break;
3251 default:
3252 break;
3253 }
3254 }
1fd400ff 3255
a0f42c21 3256 dw2_instantiate_symtab (per_cu);
9291a0cd
TT
3257 }
3258 }
3259 }
3260}
3261
774b6a14
TT
3262static void
3263dw2_pre_expand_symtabs_matching (struct objfile *objfile,
8903c50d 3264 enum block_enum block_kind, const char *name,
774b6a14 3265 domain_enum domain)
9291a0cd 3266{
156942c7 3267 dw2_do_expand_symtabs_matching (objfile, 1, block_kind, name, domain);
9291a0cd
TT
3268}
3269
3270static void
3271dw2_print_stats (struct objfile *objfile)
3272{
3273 int i, count;
3274
3275 dw2_setup (objfile);
3276 count = 0;
1fd400ff 3277 for (i = 0; i < (dwarf2_per_objfile->n_comp_units
d467dd73 3278 + dwarf2_per_objfile->n_type_units); ++i)
9291a0cd 3279 {
e254ef6a 3280 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
9291a0cd 3281
e254ef6a 3282 if (!per_cu->v.quick->symtab)
9291a0cd
TT
3283 ++count;
3284 }
3285 printf_filtered (_(" Number of unread CUs: %d\n"), count);
3286}
3287
3288static void
3289dw2_dump (struct objfile *objfile)
3290{
3291 /* Nothing worth printing. */
3292}
3293
3294static void
3295dw2_relocate (struct objfile *objfile, struct section_offsets *new_offsets,
3296 struct section_offsets *delta)
3297{
3298 /* There's nothing to relocate here. */
3299}
3300
3301static void
3302dw2_expand_symtabs_for_function (struct objfile *objfile,
3303 const char *func_name)
3304{
156942c7
DE
3305 /* Note: It doesn't matter what we pass for block_kind here. */
3306 dw2_do_expand_symtabs_matching (objfile, 0, GLOBAL_BLOCK, func_name,
3307 VAR_DOMAIN);
9291a0cd
TT
3308}
3309
3310static void
3311dw2_expand_all_symtabs (struct objfile *objfile)
3312{
3313 int i;
3314
3315 dw2_setup (objfile);
1fd400ff
TT
3316
3317 for (i = 0; i < (dwarf2_per_objfile->n_comp_units
d467dd73 3318 + dwarf2_per_objfile->n_type_units); ++i)
9291a0cd 3319 {
e254ef6a 3320 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
9291a0cd 3321
a0f42c21 3322 dw2_instantiate_symtab (per_cu);
9291a0cd
TT
3323 }
3324}
3325
3326static void
3327dw2_expand_symtabs_with_filename (struct objfile *objfile,
3328 const char *filename)
3329{
3330 int i;
3331
3332 dw2_setup (objfile);
d4637a04
DE
3333
3334 /* We don't need to consider type units here.
3335 This is only called for examining code, e.g. expand_line_sal.
3336 There can be an order of magnitude (or more) more type units
3337 than comp units, and we avoid them if we can. */
3338
3339 for (i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
9291a0cd
TT
3340 {
3341 int j;
e254ef6a 3342 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
7b9f3c50 3343 struct quick_file_names *file_data;
9291a0cd 3344
3d7bb9d9 3345 /* We only need to look at symtabs not already expanded. */
e254ef6a 3346 if (per_cu->v.quick->symtab)
9291a0cd
TT
3347 continue;
3348
7b9f3c50
DE
3349 file_data = dw2_get_file_names (objfile, per_cu);
3350 if (file_data == NULL)
9291a0cd
TT
3351 continue;
3352
7b9f3c50 3353 for (j = 0; j < file_data->num_file_names; ++j)
9291a0cd 3354 {
7b9f3c50 3355 const char *this_name = file_data->file_names[j];
1ef75ecc 3356 if (FILENAME_CMP (this_name, filename) == 0)
9291a0cd 3357 {
a0f42c21 3358 dw2_instantiate_symtab (per_cu);
9291a0cd
TT
3359 break;
3360 }
3361 }
3362 }
3363}
3364
356d9f9d
TT
3365/* A helper function for dw2_find_symbol_file that finds the primary
3366 file name for a given CU. This is a die_reader_func. */
3367
3368static void
3369dw2_get_primary_filename_reader (const struct die_reader_specs *reader,
3370 gdb_byte *info_ptr,
3371 struct die_info *comp_unit_die,
3372 int has_children,
3373 void *data)
3374{
3375 const char **result_ptr = data;
3376 struct dwarf2_cu *cu = reader->cu;
3377 struct attribute *attr;
3378
3379 attr = dwarf2_attr (comp_unit_die, DW_AT_name, cu);
3380 if (attr == NULL)
3381 *result_ptr = NULL;
3382 else
3383 *result_ptr = DW_STRING (attr);
3384}
3385
dd786858 3386static const char *
9291a0cd
TT
3387dw2_find_symbol_file (struct objfile *objfile, const char *name)
3388{
e254ef6a 3389 struct dwarf2_per_cu_data *per_cu;
9291a0cd 3390 offset_type *vec;
356d9f9d 3391 const char *filename;
9291a0cd
TT
3392
3393 dw2_setup (objfile);
3394
ae2de4f8 3395 /* index_table is NULL if OBJF_READNOW. */
9291a0cd 3396 if (!dwarf2_per_objfile->index_table)
96408a79
SA
3397 {
3398 struct symtab *s;
3399
d790cf0a
DE
3400 ALL_OBJFILE_PRIMARY_SYMTABS (objfile, s)
3401 {
3402 struct blockvector *bv = BLOCKVECTOR (s);
3403 const struct block *block = BLOCKVECTOR_BLOCK (bv, GLOBAL_BLOCK);
3404 struct symbol *sym = lookup_block_symbol (block, name, VAR_DOMAIN);
3405
3406 if (sym)
210bbc17 3407 return SYMBOL_SYMTAB (sym)->filename;
d790cf0a 3408 }
96408a79
SA
3409 return NULL;
3410 }
9291a0cd
TT
3411
3412 if (!find_slot_in_mapped_hash (dwarf2_per_objfile->index_table,
3413 name, &vec))
3414 return NULL;
3415
3416 /* Note that this just looks at the very first one named NAME -- but
3417 actually we are looking for a function. find_main_filename
3418 should be rewritten so that it doesn't require a custom hook. It
3419 could just use the ordinary symbol tables. */
3420 /* vec[0] is the length, which must always be >0. */
156942c7 3421 per_cu = dw2_get_cu (GDB_INDEX_CU_VALUE (MAYBE_SWAP (vec[1])));
9291a0cd 3422
356d9f9d
TT
3423 if (per_cu->v.quick->symtab != NULL)
3424 return per_cu->v.quick->symtab->filename;
3425
f4dc4d17
DE
3426 init_cutu_and_read_dies (per_cu, NULL, 0, 0,
3427 dw2_get_primary_filename_reader, &filename);
9291a0cd 3428
356d9f9d 3429 return filename;
9291a0cd
TT
3430}
3431
3432static void
40658b94
PH
3433dw2_map_matching_symbols (const char * name, domain_enum namespace,
3434 struct objfile *objfile, int global,
3435 int (*callback) (struct block *,
3436 struct symbol *, void *),
2edb89d3
JK
3437 void *data, symbol_compare_ftype *match,
3438 symbol_compare_ftype *ordered_compare)
9291a0cd 3439{
40658b94 3440 /* Currently unimplemented; used for Ada. The function can be called if the
a9e6a4bb
JK
3441 current language is Ada for a non-Ada objfile using GNU index. As Ada
3442 does not look for non-Ada symbols this function should just return. */
9291a0cd
TT
3443}
3444
3445static void
f8eba3c6
TT
3446dw2_expand_symtabs_matching
3447 (struct objfile *objfile,
3448 int (*file_matcher) (const char *, void *),
e078317b 3449 int (*name_matcher) (const char *, void *),
f8eba3c6
TT
3450 enum search_domain kind,
3451 void *data)
9291a0cd
TT
3452{
3453 int i;
3454 offset_type iter;
4b5246aa 3455 struct mapped_index *index;
9291a0cd
TT
3456
3457 dw2_setup (objfile);
ae2de4f8
DE
3458
3459 /* index_table is NULL if OBJF_READNOW. */
9291a0cd
TT
3460 if (!dwarf2_per_objfile->index_table)
3461 return;
4b5246aa 3462 index = dwarf2_per_objfile->index_table;
9291a0cd 3463
7b08b9eb 3464 if (file_matcher != NULL)
24c79950
TT
3465 {
3466 struct cleanup *cleanup;
3467 htab_t visited_found, visited_not_found;
3468
f4dc4d17
DE
3469 dw2_build_type_unit_groups ();
3470
24c79950
TT
3471 visited_found = htab_create_alloc (10,
3472 htab_hash_pointer, htab_eq_pointer,
3473 NULL, xcalloc, xfree);
3474 cleanup = make_cleanup_htab_delete (visited_found);
3475 visited_not_found = htab_create_alloc (10,
3476 htab_hash_pointer, htab_eq_pointer,
3477 NULL, xcalloc, xfree);
3478 make_cleanup_htab_delete (visited_not_found);
3479
3480 for (i = 0; i < (dwarf2_per_objfile->n_comp_units
f4dc4d17 3481 + dwarf2_per_objfile->n_type_unit_groups); ++i)
24c79950
TT
3482 {
3483 int j;
f4dc4d17 3484 struct dwarf2_per_cu_data *per_cu = dw2_get_primary_cu (i);
24c79950
TT
3485 struct quick_file_names *file_data;
3486 void **slot;
7b08b9eb 3487
24c79950 3488 per_cu->v.quick->mark = 0;
3d7bb9d9 3489
24c79950
TT
3490 /* We only need to look at symtabs not already expanded. */
3491 if (per_cu->v.quick->symtab)
3492 continue;
7b08b9eb 3493
24c79950
TT
3494 file_data = dw2_get_file_names (objfile, per_cu);
3495 if (file_data == NULL)
3496 continue;
7b08b9eb 3497
24c79950
TT
3498 if (htab_find (visited_not_found, file_data) != NULL)
3499 continue;
3500 else if (htab_find (visited_found, file_data) != NULL)
3501 {
3502 per_cu->v.quick->mark = 1;
3503 continue;
3504 }
3505
3506 for (j = 0; j < file_data->num_file_names; ++j)
3507 {
3508 if (file_matcher (file_data->file_names[j], data))
3509 {
3510 per_cu->v.quick->mark = 1;
3511 break;
3512 }
3513 }
3514
3515 slot = htab_find_slot (per_cu->v.quick->mark
3516 ? visited_found
3517 : visited_not_found,
3518 file_data, INSERT);
3519 *slot = file_data;
3520 }
3521
3522 do_cleanups (cleanup);
3523 }
9291a0cd 3524
3876f04e 3525 for (iter = 0; iter < index->symbol_table_slots; ++iter)
9291a0cd
TT
3526 {
3527 offset_type idx = 2 * iter;
3528 const char *name;
3529 offset_type *vec, vec_len, vec_idx;
3530
3876f04e 3531 if (index->symbol_table[idx] == 0 && index->symbol_table[idx + 1] == 0)
9291a0cd
TT
3532 continue;
3533
3876f04e 3534 name = index->constant_pool + MAYBE_SWAP (index->symbol_table[idx]);
9291a0cd 3535
e078317b 3536 if (! (*name_matcher) (name, data))
9291a0cd
TT
3537 continue;
3538
3539 /* The name was matched, now expand corresponding CUs that were
3540 marked. */
4b5246aa 3541 vec = (offset_type *) (index->constant_pool
3876f04e 3542 + MAYBE_SWAP (index->symbol_table[idx + 1]));
9291a0cd
TT
3543 vec_len = MAYBE_SWAP (vec[0]);
3544 for (vec_idx = 0; vec_idx < vec_len; ++vec_idx)
3545 {
e254ef6a 3546 struct dwarf2_per_cu_data *per_cu;
156942c7
DE
3547 offset_type cu_index_and_attrs = MAYBE_SWAP (vec[vec_idx + 1]);
3548 gdb_index_symbol_kind symbol_kind =
3549 GDB_INDEX_SYMBOL_KIND_VALUE (cu_index_and_attrs);
3550 int cu_index = GDB_INDEX_CU_VALUE (cu_index_and_attrs);
3551
3552 /* Don't crash on bad data. */
3553 if (cu_index >= (dwarf2_per_objfile->n_comp_units
667e0a4b 3554 + dwarf2_per_objfile->n_type_units))
156942c7 3555 continue;
1fd400ff 3556
156942c7
DE
3557 /* Only check the symbol's kind if it has one.
3558 Indices prior to version 7 don't record it. */
3559 if (index->version >= 7)
3560 {
3561 switch (kind)
3562 {
3563 case VARIABLES_DOMAIN:
3564 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_VARIABLE)
3565 continue;
3566 break;
3567 case FUNCTIONS_DOMAIN:
3568 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_FUNCTION)
3569 continue;
3570 break;
3571 case TYPES_DOMAIN:
3572 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_TYPE)
3573 continue;
3574 break;
3575 default:
3576 break;
3577 }
3578 }
3579
3580 per_cu = dw2_get_cu (cu_index);
7b08b9eb 3581 if (file_matcher == NULL || per_cu->v.quick->mark)
a0f42c21 3582 dw2_instantiate_symtab (per_cu);
9291a0cd
TT
3583 }
3584 }
3585}
3586
9703b513
TT
3587/* A helper for dw2_find_pc_sect_symtab which finds the most specific
3588 symtab. */
3589
3590static struct symtab *
3591recursively_find_pc_sect_symtab (struct symtab *symtab, CORE_ADDR pc)
3592{
3593 int i;
3594
3595 if (BLOCKVECTOR (symtab) != NULL
3596 && blockvector_contains_pc (BLOCKVECTOR (symtab), pc))
3597 return symtab;
3598
a3ec0bb1
DE
3599 if (symtab->includes == NULL)
3600 return NULL;
3601
9703b513
TT
3602 for (i = 0; symtab->includes[i]; ++i)
3603 {
a3ec0bb1 3604 struct symtab *s = symtab->includes[i];
9703b513
TT
3605
3606 s = recursively_find_pc_sect_symtab (s, pc);
3607 if (s != NULL)
3608 return s;
3609 }
3610
3611 return NULL;
3612}
3613
9291a0cd
TT
3614static struct symtab *
3615dw2_find_pc_sect_symtab (struct objfile *objfile,
3616 struct minimal_symbol *msymbol,
3617 CORE_ADDR pc,
3618 struct obj_section *section,
3619 int warn_if_readin)
3620{
3621 struct dwarf2_per_cu_data *data;
9703b513 3622 struct symtab *result;
9291a0cd
TT
3623
3624 dw2_setup (objfile);
3625
3626 if (!objfile->psymtabs_addrmap)
3627 return NULL;
3628
3629 data = addrmap_find (objfile->psymtabs_addrmap, pc);
3630 if (!data)
3631 return NULL;
3632
3633 if (warn_if_readin && data->v.quick->symtab)
abebb8b0 3634 warning (_("(Internal error: pc %s in read in CU, but not in symtab.)"),
9291a0cd
TT
3635 paddress (get_objfile_arch (objfile), pc));
3636
9703b513
TT
3637 result = recursively_find_pc_sect_symtab (dw2_instantiate_symtab (data), pc);
3638 gdb_assert (result != NULL);
3639 return result;
9291a0cd
TT
3640}
3641
9291a0cd 3642static void
44b13c5a 3643dw2_map_symbol_filenames (struct objfile *objfile, symbol_filename_ftype *fun,
74e2f255 3644 void *data, int need_fullname)
9291a0cd
TT
3645{
3646 int i;
24c79950
TT
3647 struct cleanup *cleanup;
3648 htab_t visited = htab_create_alloc (10, htab_hash_pointer, htab_eq_pointer,
3649 NULL, xcalloc, xfree);
9291a0cd 3650
24c79950 3651 cleanup = make_cleanup_htab_delete (visited);
9291a0cd 3652 dw2_setup (objfile);
ae2de4f8 3653
f4dc4d17
DE
3654 dw2_build_type_unit_groups ();
3655
24c79950
TT
3656 /* We can ignore file names coming from already-expanded CUs. */
3657 for (i = 0; i < (dwarf2_per_objfile->n_comp_units
3658 + dwarf2_per_objfile->n_type_units); ++i)
3659 {
3660 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
3661
3662 if (per_cu->v.quick->symtab)
3663 {
3664 void **slot = htab_find_slot (visited, per_cu->v.quick->file_names,
3665 INSERT);
3666
3667 *slot = per_cu->v.quick->file_names;
3668 }
3669 }
3670
1fd400ff 3671 for (i = 0; i < (dwarf2_per_objfile->n_comp_units
f4dc4d17 3672 + dwarf2_per_objfile->n_type_unit_groups); ++i)
9291a0cd
TT
3673 {
3674 int j;
f4dc4d17 3675 struct dwarf2_per_cu_data *per_cu = dw2_get_primary_cu (i);
7b9f3c50 3676 struct quick_file_names *file_data;
24c79950 3677 void **slot;
9291a0cd 3678
3d7bb9d9 3679 /* We only need to look at symtabs not already expanded. */
e254ef6a 3680 if (per_cu->v.quick->symtab)
9291a0cd
TT
3681 continue;
3682
7b9f3c50
DE
3683 file_data = dw2_get_file_names (objfile, per_cu);
3684 if (file_data == NULL)
9291a0cd
TT
3685 continue;
3686
24c79950
TT
3687 slot = htab_find_slot (visited, file_data, INSERT);
3688 if (*slot)
3689 {
3690 /* Already visited. */
3691 continue;
3692 }
3693 *slot = file_data;
3694
7b9f3c50 3695 for (j = 0; j < file_data->num_file_names; ++j)
9291a0cd 3696 {
74e2f255
DE
3697 const char *this_real_name;
3698
3699 if (need_fullname)
3700 this_real_name = dw2_get_real_path (objfile, file_data, j);
3701 else
3702 this_real_name = NULL;
7b9f3c50 3703 (*fun) (file_data->file_names[j], this_real_name, data);
9291a0cd
TT
3704 }
3705 }
24c79950
TT
3706
3707 do_cleanups (cleanup);
9291a0cd
TT
3708}
3709
3710static int
3711dw2_has_symbols (struct objfile *objfile)
3712{
3713 return 1;
3714}
3715
3716const struct quick_symbol_functions dwarf2_gdb_index_functions =
3717{
3718 dw2_has_symbols,
3719 dw2_find_last_source_symtab,
3720 dw2_forget_cached_source_info,
f8eba3c6 3721 dw2_map_symtabs_matching_filename,
9291a0cd 3722 dw2_lookup_symbol,
774b6a14 3723 dw2_pre_expand_symtabs_matching,
9291a0cd
TT
3724 dw2_print_stats,
3725 dw2_dump,
3726 dw2_relocate,
3727 dw2_expand_symtabs_for_function,
3728 dw2_expand_all_symtabs,
3729 dw2_expand_symtabs_with_filename,
3730 dw2_find_symbol_file,
40658b94 3731 dw2_map_matching_symbols,
9291a0cd
TT
3732 dw2_expand_symtabs_matching,
3733 dw2_find_pc_sect_symtab,
9291a0cd
TT
3734 dw2_map_symbol_filenames
3735};
3736
3737/* Initialize for reading DWARF for this objfile. Return 0 if this
3738 file will use psymtabs, or 1 if using the GNU index. */
3739
3740int
3741dwarf2_initialize_objfile (struct objfile *objfile)
3742{
3743 /* If we're about to read full symbols, don't bother with the
3744 indices. In this case we also don't care if some other debug
3745 format is making psymtabs, because they are all about to be
3746 expanded anyway. */
3747 if ((objfile->flags & OBJF_READNOW))
3748 {
3749 int i;
3750
3751 dwarf2_per_objfile->using_index = 1;
3752 create_all_comp_units (objfile);
0e50663e 3753 create_all_type_units (objfile);
7b9f3c50
DE
3754 dwarf2_per_objfile->quick_file_names_table =
3755 create_quick_file_names_table (dwarf2_per_objfile->n_comp_units);
9291a0cd 3756
1fd400ff 3757 for (i = 0; i < (dwarf2_per_objfile->n_comp_units
d467dd73 3758 + dwarf2_per_objfile->n_type_units); ++i)
9291a0cd 3759 {
e254ef6a 3760 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
9291a0cd 3761
e254ef6a
DE
3762 per_cu->v.quick = OBSTACK_ZALLOC (&objfile->objfile_obstack,
3763 struct dwarf2_per_cu_quick_data);
9291a0cd
TT
3764 }
3765
3766 /* Return 1 so that gdb sees the "quick" functions. However,
3767 these functions will be no-ops because we will have expanded
3768 all symtabs. */
3769 return 1;
3770 }
3771
3772 if (dwarf2_read_index (objfile))
3773 return 1;
3774
9291a0cd
TT
3775 return 0;
3776}
3777
3778\f
3779
dce234bc
PP
3780/* Build a partial symbol table. */
3781
3782void
f29dff0a 3783dwarf2_build_psymtabs (struct objfile *objfile)
dce234bc 3784{
f29dff0a 3785 if (objfile->global_psymbols.size == 0 && objfile->static_psymbols.size == 0)
c906108c
SS
3786 {
3787 init_psymbol_list (objfile, 1024);
3788 }
3789
d146bf1e 3790 dwarf2_build_psymtabs_hard (objfile);
c906108c 3791}
c906108c 3792
1ce1cefd
DE
3793/* Return the total length of the CU described by HEADER. */
3794
3795static unsigned int
3796get_cu_length (const struct comp_unit_head *header)
3797{
3798 return header->initial_length_size + header->length;
3799}
3800
45452591
DE
3801/* Return TRUE if OFFSET is within CU_HEADER. */
3802
3803static inline int
b64f50a1 3804offset_in_cu_p (const struct comp_unit_head *cu_header, sect_offset offset)
45452591 3805{
b64f50a1 3806 sect_offset bottom = { cu_header->offset.sect_off };
1ce1cefd 3807 sect_offset top = { cu_header->offset.sect_off + get_cu_length (cu_header) };
9a619af0 3808
b64f50a1 3809 return (offset.sect_off >= bottom.sect_off && offset.sect_off < top.sect_off);
45452591
DE
3810}
3811
3b80fe9b
DE
3812/* Find the base address of the compilation unit for range lists and
3813 location lists. It will normally be specified by DW_AT_low_pc.
3814 In DWARF-3 draft 4, the base address could be overridden by
3815 DW_AT_entry_pc. It's been removed, but GCC still uses this for
3816 compilation units with discontinuous ranges. */
3817
3818static void
3819dwarf2_find_base_address (struct die_info *die, struct dwarf2_cu *cu)
3820{
3821 struct attribute *attr;
3822
3823 cu->base_known = 0;
3824 cu->base_address = 0;
3825
3826 attr = dwarf2_attr (die, DW_AT_entry_pc, cu);
3827 if (attr)
3828 {
3829 cu->base_address = DW_ADDR (attr);
3830 cu->base_known = 1;
3831 }
3832 else
3833 {
3834 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
3835 if (attr)
3836 {
3837 cu->base_address = DW_ADDR (attr);
3838 cu->base_known = 1;
3839 }
3840 }
3841}
3842
93311388
DE
3843/* Read in the comp unit header information from the debug_info at info_ptr.
3844 NOTE: This leaves members offset, first_die_offset to be filled in
3845 by the caller. */
107d2387 3846
fe1b8b76 3847static gdb_byte *
107d2387 3848read_comp_unit_head (struct comp_unit_head *cu_header,
fe1b8b76 3849 gdb_byte *info_ptr, bfd *abfd)
107d2387
AC
3850{
3851 int signed_addr;
891d2f0b 3852 unsigned int bytes_read;
c764a876
DE
3853
3854 cu_header->length = read_initial_length (abfd, info_ptr, &bytes_read);
3855 cu_header->initial_length_size = bytes_read;
3856 cu_header->offset_size = (bytes_read == 4) ? 4 : 8;
613e1657 3857 info_ptr += bytes_read;
107d2387
AC
3858 cu_header->version = read_2_bytes (abfd, info_ptr);
3859 info_ptr += 2;
b64f50a1
JK
3860 cu_header->abbrev_offset.sect_off = read_offset (abfd, info_ptr, cu_header,
3861 &bytes_read);
613e1657 3862 info_ptr += bytes_read;
107d2387
AC
3863 cu_header->addr_size = read_1_byte (abfd, info_ptr);
3864 info_ptr += 1;
3865 signed_addr = bfd_get_sign_extend_vma (abfd);
3866 if (signed_addr < 0)
8e65ff28 3867 internal_error (__FILE__, __LINE__,
e2e0b3e5 3868 _("read_comp_unit_head: dwarf from non elf file"));
107d2387 3869 cu_header->signed_addr_p = signed_addr;
c764a876 3870
107d2387
AC
3871 return info_ptr;
3872}
3873
36586728
TT
3874/* Helper function that returns the proper abbrev section for
3875 THIS_CU. */
3876
3877static struct dwarf2_section_info *
3878get_abbrev_section_for_cu (struct dwarf2_per_cu_data *this_cu)
3879{
3880 struct dwarf2_section_info *abbrev;
3881
3882 if (this_cu->is_dwz)
3883 abbrev = &dwarf2_get_dwz_file ()->abbrev;
3884 else
3885 abbrev = &dwarf2_per_objfile->abbrev;
3886
3887 return abbrev;
3888}
3889
9ff913ba
DE
3890/* Subroutine of read_and_check_comp_unit_head and
3891 read_and_check_type_unit_head to simplify them.
3892 Perform various error checking on the header. */
3893
3894static void
3895error_check_comp_unit_head (struct comp_unit_head *header,
4bdcc0c1
DE
3896 struct dwarf2_section_info *section,
3897 struct dwarf2_section_info *abbrev_section)
9ff913ba
DE
3898{
3899 bfd *abfd = section->asection->owner;
3900 const char *filename = bfd_get_filename (abfd);
3901
3902 if (header->version != 2 && header->version != 3 && header->version != 4)
3903 error (_("Dwarf Error: wrong version in compilation unit header "
3904 "(is %d, should be 2, 3, or 4) [in module %s]"), header->version,
3905 filename);
3906
b64f50a1 3907 if (header->abbrev_offset.sect_off
36586728 3908 >= dwarf2_section_size (dwarf2_per_objfile->objfile, abbrev_section))
9ff913ba
DE
3909 error (_("Dwarf Error: bad offset (0x%lx) in compilation unit header "
3910 "(offset 0x%lx + 6) [in module %s]"),
b64f50a1 3911 (long) header->abbrev_offset.sect_off, (long) header->offset.sect_off,
9ff913ba
DE
3912 filename);
3913
3914 /* Cast to unsigned long to use 64-bit arithmetic when possible to
3915 avoid potential 32-bit overflow. */
1ce1cefd 3916 if (((unsigned long) header->offset.sect_off + get_cu_length (header))
9ff913ba
DE
3917 > section->size)
3918 error (_("Dwarf Error: bad length (0x%lx) in compilation unit header "
3919 "(offset 0x%lx + 0) [in module %s]"),
b64f50a1 3920 (long) header->length, (long) header->offset.sect_off,
9ff913ba
DE
3921 filename);
3922}
3923
3924/* Read in a CU/TU header and perform some basic error checking.
3925 The contents of the header are stored in HEADER.
3926 The result is a pointer to the start of the first DIE. */
adabb602 3927
fe1b8b76 3928static gdb_byte *
9ff913ba
DE
3929read_and_check_comp_unit_head (struct comp_unit_head *header,
3930 struct dwarf2_section_info *section,
4bdcc0c1 3931 struct dwarf2_section_info *abbrev_section,
9ff913ba
DE
3932 gdb_byte *info_ptr,
3933 int is_debug_types_section)
72bf9492 3934{
fe1b8b76 3935 gdb_byte *beg_of_comp_unit = info_ptr;
9ff913ba 3936 bfd *abfd = section->asection->owner;
72bf9492 3937
b64f50a1 3938 header->offset.sect_off = beg_of_comp_unit - section->buffer;
adabb602 3939
72bf9492
DJ
3940 info_ptr = read_comp_unit_head (header, info_ptr, abfd);
3941
460c1c54
CC
3942 /* If we're reading a type unit, skip over the signature and
3943 type_offset fields. */
b0df02fd 3944 if (is_debug_types_section)
460c1c54
CC
3945 info_ptr += 8 /*signature*/ + header->offset_size;
3946
b64f50a1 3947 header->first_die_offset.cu_off = info_ptr - beg_of_comp_unit;
adabb602 3948
4bdcc0c1 3949 error_check_comp_unit_head (header, section, abbrev_section);
72bf9492
DJ
3950
3951 return info_ptr;
3952}
3953
348e048f
DE
3954/* Read in the types comp unit header information from .debug_types entry at
3955 types_ptr. The result is a pointer to one past the end of the header. */
3956
3957static gdb_byte *
9ff913ba
DE
3958read_and_check_type_unit_head (struct comp_unit_head *header,
3959 struct dwarf2_section_info *section,
4bdcc0c1 3960 struct dwarf2_section_info *abbrev_section,
9ff913ba 3961 gdb_byte *info_ptr,
dee91e82
DE
3962 ULONGEST *signature,
3963 cu_offset *type_offset_in_tu)
348e048f 3964{
9ff913ba
DE
3965 gdb_byte *beg_of_comp_unit = info_ptr;
3966 bfd *abfd = section->asection->owner;
348e048f 3967
b64f50a1 3968 header->offset.sect_off = beg_of_comp_unit - section->buffer;
348e048f 3969
9ff913ba 3970 info_ptr = read_comp_unit_head (header, info_ptr, abfd);
348e048f 3971
9ff913ba
DE
3972 /* If we're reading a type unit, skip over the signature and
3973 type_offset fields. */
3974 if (signature != NULL)
3975 *signature = read_8_bytes (abfd, info_ptr);
3976 info_ptr += 8;
dee91e82
DE
3977 if (type_offset_in_tu != NULL)
3978 type_offset_in_tu->cu_off = read_offset_1 (abfd, info_ptr,
3979 header->offset_size);
9ff913ba
DE
3980 info_ptr += header->offset_size;
3981
b64f50a1 3982 header->first_die_offset.cu_off = info_ptr - beg_of_comp_unit;
348e048f 3983
4bdcc0c1 3984 error_check_comp_unit_head (header, section, abbrev_section);
9ff913ba
DE
3985
3986 return info_ptr;
348e048f
DE
3987}
3988
f4dc4d17
DE
3989/* Fetch the abbreviation table offset from a comp or type unit header. */
3990
3991static sect_offset
3992read_abbrev_offset (struct dwarf2_section_info *section,
3993 sect_offset offset)
3994{
3995 bfd *abfd = section->asection->owner;
3996 gdb_byte *info_ptr;
3997 unsigned int length, initial_length_size, offset_size;
3998 sect_offset abbrev_offset;
3999
4000 dwarf2_read_section (dwarf2_per_objfile->objfile, section);
4001 info_ptr = section->buffer + offset.sect_off;
4002 length = read_initial_length (abfd, info_ptr, &initial_length_size);
4003 offset_size = initial_length_size == 4 ? 4 : 8;
4004 info_ptr += initial_length_size + 2 /*version*/;
4005 abbrev_offset.sect_off = read_offset_1 (abfd, info_ptr, offset_size);
4006 return abbrev_offset;
4007}
4008
aaa75496
JB
4009/* Allocate a new partial symtab for file named NAME and mark this new
4010 partial symtab as being an include of PST. */
4011
4012static void
4013dwarf2_create_include_psymtab (char *name, struct partial_symtab *pst,
4014 struct objfile *objfile)
4015{
4016 struct partial_symtab *subpst = allocate_psymtab (name, objfile);
4017
4018 subpst->section_offsets = pst->section_offsets;
4019 subpst->textlow = 0;
4020 subpst->texthigh = 0;
4021
4022 subpst->dependencies = (struct partial_symtab **)
4023 obstack_alloc (&objfile->objfile_obstack,
4024 sizeof (struct partial_symtab *));
4025 subpst->dependencies[0] = pst;
4026 subpst->number_of_dependencies = 1;
4027
4028 subpst->globals_offset = 0;
4029 subpst->n_global_syms = 0;
4030 subpst->statics_offset = 0;
4031 subpst->n_static_syms = 0;
4032 subpst->symtab = NULL;
4033 subpst->read_symtab = pst->read_symtab;
4034 subpst->readin = 0;
4035
4036 /* No private part is necessary for include psymtabs. This property
4037 can be used to differentiate between such include psymtabs and
10b3939b 4038 the regular ones. */
58a9656e 4039 subpst->read_symtab_private = NULL;
aaa75496
JB
4040}
4041
4042/* Read the Line Number Program data and extract the list of files
4043 included by the source file represented by PST. Build an include
d85a05f0 4044 partial symtab for each of these included files. */
aaa75496
JB
4045
4046static void
4047dwarf2_build_include_psymtabs (struct dwarf2_cu *cu,
dee91e82
DE
4048 struct die_info *die,
4049 struct partial_symtab *pst)
aaa75496 4050{
d85a05f0
DJ
4051 struct line_header *lh = NULL;
4052 struct attribute *attr;
aaa75496 4053
d85a05f0
DJ
4054 attr = dwarf2_attr (die, DW_AT_stmt_list, cu);
4055 if (attr)
3019eac3 4056 lh = dwarf_decode_line_header (DW_UNSND (attr), cu);
aaa75496
JB
4057 if (lh == NULL)
4058 return; /* No linetable, so no includes. */
4059
c6da4cef 4060 /* NOTE: pst->dirname is DW_AT_comp_dir (if present). */
f3f5162e 4061 dwarf_decode_lines (lh, pst->dirname, cu, pst, 1);
aaa75496
JB
4062
4063 free_line_header (lh);
4064}
4065
348e048f 4066static hashval_t
52dc124a 4067hash_signatured_type (const void *item)
348e048f 4068{
52dc124a 4069 const struct signatured_type *sig_type = item;
9a619af0 4070
348e048f 4071 /* This drops the top 32 bits of the signature, but is ok for a hash. */
52dc124a 4072 return sig_type->signature;
348e048f
DE
4073}
4074
4075static int
52dc124a 4076eq_signatured_type (const void *item_lhs, const void *item_rhs)
348e048f
DE
4077{
4078 const struct signatured_type *lhs = item_lhs;
4079 const struct signatured_type *rhs = item_rhs;
9a619af0 4080
348e048f
DE
4081 return lhs->signature == rhs->signature;
4082}
4083
1fd400ff
TT
4084/* Allocate a hash table for signatured types. */
4085
4086static htab_t
673bfd45 4087allocate_signatured_type_table (struct objfile *objfile)
1fd400ff
TT
4088{
4089 return htab_create_alloc_ex (41,
52dc124a
DE
4090 hash_signatured_type,
4091 eq_signatured_type,
1fd400ff
TT
4092 NULL,
4093 &objfile->objfile_obstack,
4094 hashtab_obstack_allocate,
4095 dummy_obstack_deallocate);
4096}
4097
d467dd73 4098/* A helper function to add a signatured type CU to a table. */
1fd400ff
TT
4099
4100static int
d467dd73 4101add_signatured_type_cu_to_table (void **slot, void *datum)
1fd400ff
TT
4102{
4103 struct signatured_type *sigt = *slot;
b4dd5633 4104 struct signatured_type ***datap = datum;
1fd400ff 4105
b4dd5633 4106 **datap = sigt;
1fd400ff
TT
4107 ++*datap;
4108
4109 return 1;
4110}
4111
3019eac3 4112/* Create the hash table of all entries in the .debug_types section.
80626a55
DE
4113 DWO_FILE is a pointer to the DWO file for .debug_types.dwo,
4114 NULL otherwise.
4115 Note: This function processes DWO files only, not DWP files.
3019eac3
DE
4116 The result is a pointer to the hash table or NULL if there are
4117 no types. */
348e048f 4118
3019eac3
DE
4119static htab_t
4120create_debug_types_hash_table (struct dwo_file *dwo_file,
4121 VEC (dwarf2_section_info_def) *types)
348e048f 4122{
3019eac3 4123 struct objfile *objfile = dwarf2_per_objfile->objfile;
8b70b953 4124 htab_t types_htab = NULL;
8b70b953
TT
4125 int ix;
4126 struct dwarf2_section_info *section;
4bdcc0c1 4127 struct dwarf2_section_info *abbrev_section;
348e048f 4128
3019eac3
DE
4129 if (VEC_empty (dwarf2_section_info_def, types))
4130 return NULL;
348e048f 4131
4bdcc0c1
DE
4132 abbrev_section = (dwo_file != NULL
4133 ? &dwo_file->sections.abbrev
4134 : &dwarf2_per_objfile->abbrev);
4135
09406207
DE
4136 if (dwarf2_read_debug)
4137 fprintf_unfiltered (gdb_stdlog, "Reading .debug_types%s for %s:\n",
4138 dwo_file ? ".dwo" : "",
4139 bfd_get_filename (abbrev_section->asection->owner));
4140
8b70b953 4141 for (ix = 0;
3019eac3 4142 VEC_iterate (dwarf2_section_info_def, types, ix, section);
8b70b953
TT
4143 ++ix)
4144 {
3019eac3 4145 bfd *abfd;
8b70b953 4146 gdb_byte *info_ptr, *end_ptr;
36586728 4147 struct dwarf2_section_info *abbrev_section;
348e048f 4148
8b70b953
TT
4149 dwarf2_read_section (objfile, section);
4150 info_ptr = section->buffer;
348e048f 4151
8b70b953
TT
4152 if (info_ptr == NULL)
4153 continue;
348e048f 4154
3019eac3
DE
4155 /* We can't set abfd until now because the section may be empty or
4156 not present, in which case section->asection will be NULL. */
4157 abfd = section->asection->owner;
4158
36586728
TT
4159 if (dwo_file)
4160 abbrev_section = &dwo_file->sections.abbrev;
4161 else
4162 abbrev_section = &dwarf2_per_objfile->abbrev;
4163
8b70b953 4164 if (types_htab == NULL)
3019eac3
DE
4165 {
4166 if (dwo_file)
4167 types_htab = allocate_dwo_unit_table (objfile);
4168 else
4169 types_htab = allocate_signatured_type_table (objfile);
4170 }
348e048f 4171
dee91e82
DE
4172 /* We don't use init_cutu_and_read_dies_simple, or some such, here
4173 because we don't need to read any dies: the signature is in the
4174 header. */
8b70b953
TT
4175
4176 end_ptr = info_ptr + section->size;
4177 while (info_ptr < end_ptr)
4178 {
b64f50a1 4179 sect_offset offset;
3019eac3 4180 cu_offset type_offset_in_tu;
8b70b953 4181 ULONGEST signature;
52dc124a 4182 struct signatured_type *sig_type;
3019eac3 4183 struct dwo_unit *dwo_tu;
8b70b953
TT
4184 void **slot;
4185 gdb_byte *ptr = info_ptr;
9ff913ba 4186 struct comp_unit_head header;
dee91e82 4187 unsigned int length;
348e048f 4188
b64f50a1 4189 offset.sect_off = ptr - section->buffer;
348e048f 4190
8b70b953 4191 /* We need to read the type's signature in order to build the hash
9ff913ba 4192 table, but we don't need anything else just yet. */
348e048f 4193
4bdcc0c1
DE
4194 ptr = read_and_check_type_unit_head (&header, section,
4195 abbrev_section, ptr,
3019eac3 4196 &signature, &type_offset_in_tu);
6caca83c 4197
1ce1cefd 4198 length = get_cu_length (&header);
dee91e82 4199
6caca83c 4200 /* Skip dummy type units. */
dee91e82
DE
4201 if (ptr >= info_ptr + length
4202 || peek_abbrev_code (abfd, ptr) == 0)
6caca83c 4203 {
1ce1cefd 4204 info_ptr += length;
6caca83c
CC
4205 continue;
4206 }
8b70b953 4207
3019eac3
DE
4208 if (dwo_file)
4209 {
4210 sig_type = NULL;
4211 dwo_tu = OBSTACK_ZALLOC (&objfile->objfile_obstack,
4212 struct dwo_unit);
4213 dwo_tu->dwo_file = dwo_file;
4214 dwo_tu->signature = signature;
4215 dwo_tu->type_offset_in_tu = type_offset_in_tu;
4216 dwo_tu->info_or_types_section = section;
4217 dwo_tu->offset = offset;
4218 dwo_tu->length = length;
4219 }
4220 else
4221 {
4222 /* N.B.: type_offset is not usable if this type uses a DWO file.
4223 The real type_offset is in the DWO file. */
4224 dwo_tu = NULL;
4225 sig_type = OBSTACK_ZALLOC (&objfile->objfile_obstack,
4226 struct signatured_type);
4227 sig_type->signature = signature;
4228 sig_type->type_offset_in_tu = type_offset_in_tu;
4229 sig_type->per_cu.objfile = objfile;
4230 sig_type->per_cu.is_debug_types = 1;
4231 sig_type->per_cu.info_or_types_section = section;
4232 sig_type->per_cu.offset = offset;
4233 sig_type->per_cu.length = length;
4234 }
8b70b953 4235
3019eac3
DE
4236 slot = htab_find_slot (types_htab,
4237 dwo_file ? (void*) dwo_tu : (void *) sig_type,
4238 INSERT);
8b70b953
TT
4239 gdb_assert (slot != NULL);
4240 if (*slot != NULL)
4241 {
3019eac3
DE
4242 sect_offset dup_offset;
4243
4244 if (dwo_file)
4245 {
4246 const struct dwo_unit *dup_tu = *slot;
4247
4248 dup_offset = dup_tu->offset;
4249 }
4250 else
4251 {
4252 const struct signatured_type *dup_tu = *slot;
4253
4254 dup_offset = dup_tu->per_cu.offset;
4255 }
b3c8eb43 4256
8b70b953
TT
4257 complaint (&symfile_complaints,
4258 _("debug type entry at offset 0x%x is duplicate to the "
4259 "entry at offset 0x%x, signature 0x%s"),
3019eac3 4260 offset.sect_off, dup_offset.sect_off,
8b70b953 4261 phex (signature, sizeof (signature)));
8b70b953 4262 }
3019eac3 4263 *slot = dwo_file ? (void *) dwo_tu : (void *) sig_type;
348e048f 4264
09406207 4265 if (dwarf2_read_debug)
8b70b953 4266 fprintf_unfiltered (gdb_stdlog, " offset 0x%x, signature 0x%s\n",
b64f50a1
JK
4267 offset.sect_off,
4268 phex (signature, sizeof (signature)));
348e048f 4269
dee91e82 4270 info_ptr += length;
8b70b953 4271 }
348e048f
DE
4272 }
4273
3019eac3
DE
4274 return types_htab;
4275}
4276
4277/* Create the hash table of all entries in the .debug_types section,
4278 and initialize all_type_units.
4279 The result is zero if there is an error (e.g. missing .debug_types section),
4280 otherwise non-zero. */
4281
4282static int
4283create_all_type_units (struct objfile *objfile)
4284{
4285 htab_t types_htab;
b4dd5633 4286 struct signatured_type **iter;
3019eac3
DE
4287
4288 types_htab = create_debug_types_hash_table (NULL, dwarf2_per_objfile->types);
4289 if (types_htab == NULL)
4290 {
4291 dwarf2_per_objfile->signatured_types = NULL;
4292 return 0;
4293 }
4294
348e048f
DE
4295 dwarf2_per_objfile->signatured_types = types_htab;
4296
d467dd73
DE
4297 dwarf2_per_objfile->n_type_units = htab_elements (types_htab);
4298 dwarf2_per_objfile->all_type_units
1fd400ff 4299 = obstack_alloc (&objfile->objfile_obstack,
d467dd73 4300 dwarf2_per_objfile->n_type_units
b4dd5633 4301 * sizeof (struct signatured_type *));
d467dd73
DE
4302 iter = &dwarf2_per_objfile->all_type_units[0];
4303 htab_traverse_noresize (types_htab, add_signatured_type_cu_to_table, &iter);
4304 gdb_assert (iter - &dwarf2_per_objfile->all_type_units[0]
4305 == dwarf2_per_objfile->n_type_units);
1fd400ff 4306
348e048f
DE
4307 return 1;
4308}
4309
380bca97 4310/* Lookup a signature based type for DW_FORM_ref_sig8.
e319fa28 4311 Returns NULL if signature SIG is not present in the table. */
348e048f
DE
4312
4313static struct signatured_type *
e319fa28 4314lookup_signatured_type (ULONGEST sig)
348e048f
DE
4315{
4316 struct signatured_type find_entry, *entry;
4317
4318 if (dwarf2_per_objfile->signatured_types == NULL)
4319 {
4320 complaint (&symfile_complaints,
55f1336d 4321 _("missing `.debug_types' section for DW_FORM_ref_sig8 die"));
dcc07052 4322 return NULL;
348e048f
DE
4323 }
4324
4325 find_entry.signature = sig;
4326 entry = htab_find (dwarf2_per_objfile->signatured_types, &find_entry);
4327 return entry;
4328}
42e7ad6c
DE
4329\f
4330/* Low level DIE reading support. */
348e048f 4331
d85a05f0
DJ
4332/* Initialize a die_reader_specs struct from a dwarf2_cu struct. */
4333
4334static void
4335init_cu_die_reader (struct die_reader_specs *reader,
dee91e82 4336 struct dwarf2_cu *cu,
3019eac3
DE
4337 struct dwarf2_section_info *section,
4338 struct dwo_file *dwo_file)
d85a05f0 4339{
fceca515 4340 gdb_assert (section->readin && section->buffer != NULL);
dee91e82 4341 reader->abfd = section->asection->owner;
d85a05f0 4342 reader->cu = cu;
3019eac3 4343 reader->dwo_file = dwo_file;
dee91e82
DE
4344 reader->die_section = section;
4345 reader->buffer = section->buffer;
f664829e 4346 reader->buffer_end = section->buffer + section->size;
d85a05f0
DJ
4347}
4348
fd820528 4349/* Initialize a CU (or TU) and read its DIEs.
3019eac3 4350 If the CU defers to a DWO file, read the DWO file as well.
dee91e82 4351
f4dc4d17
DE
4352 ABBREV_TABLE, if non-NULL, is the abbreviation table to use.
4353 Otherwise the table specified in the comp unit header is read in and used.
4354 This is an optimization for when we already have the abbrev table.
4355
dee91e82
DE
4356 If USE_EXISTING_CU is non-zero, and THIS_CU->cu is non-NULL, then use it.
4357 Otherwise, a new CU is allocated with xmalloc.
4358
4359 If KEEP is non-zero, then if we allocated a dwarf2_cu we add it to
4360 read_in_chain. Otherwise the dwarf2_cu data is freed at the end.
4361
4362 WARNING: If THIS_CU is a "dummy CU" (used as filler by the incremental
fd820528 4363 linker) then DIE_READER_FUNC will not get called. */
aaa75496 4364
70221824 4365static void
fd820528 4366init_cutu_and_read_dies (struct dwarf2_per_cu_data *this_cu,
f4dc4d17 4367 struct abbrev_table *abbrev_table,
fd820528
DE
4368 int use_existing_cu, int keep,
4369 die_reader_func_ftype *die_reader_func,
4370 void *data)
c906108c 4371{
dee91e82 4372 struct objfile *objfile = dwarf2_per_objfile->objfile;
3019eac3
DE
4373 struct dwarf2_section_info *section = this_cu->info_or_types_section;
4374 bfd *abfd = section->asection->owner;
dee91e82
DE
4375 struct dwarf2_cu *cu;
4376 gdb_byte *begin_info_ptr, *info_ptr;
4377 struct die_reader_specs reader;
d85a05f0 4378 struct die_info *comp_unit_die;
dee91e82 4379 int has_children;
d85a05f0 4380 struct attribute *attr;
dee91e82
DE
4381 struct cleanup *cleanups, *free_cu_cleanup = NULL;
4382 struct signatured_type *sig_type = NULL;
4bdcc0c1 4383 struct dwarf2_section_info *abbrev_section;
42e7ad6c
DE
4384 /* Non-zero if CU currently points to a DWO file and we need to
4385 reread it. When this happens we need to reread the skeleton die
4386 before we can reread the DWO file. */
4387 int rereading_dwo_cu = 0;
c906108c 4388
09406207
DE
4389 if (dwarf2_die_debug)
4390 fprintf_unfiltered (gdb_stdlog, "Reading %s unit at offset 0x%x\n",
4391 this_cu->is_debug_types ? "type" : "comp",
4392 this_cu->offset.sect_off);
4393
dee91e82
DE
4394 if (use_existing_cu)
4395 gdb_assert (keep);
23745b47 4396
dee91e82
DE
4397 cleanups = make_cleanup (null_cleanup, NULL);
4398
4399 /* This is cheap if the section is already read in. */
4400 dwarf2_read_section (objfile, section);
4401
4402 begin_info_ptr = info_ptr = section->buffer + this_cu->offset.sect_off;
36586728
TT
4403
4404 abbrev_section = get_abbrev_section_for_cu (this_cu);
dee91e82
DE
4405
4406 if (use_existing_cu && this_cu->cu != NULL)
4407 {
4408 cu = this_cu->cu;
42e7ad6c
DE
4409
4410 /* If this CU is from a DWO file we need to start over, we need to
4411 refetch the attributes from the skeleton CU.
4412 This could be optimized by retrieving those attributes from when we
4413 were here the first time: the previous comp_unit_die was stored in
4414 comp_unit_obstack. But there's no data yet that we need this
4415 optimization. */
4416 if (cu->dwo_unit != NULL)
4417 rereading_dwo_cu = 1;
dee91e82
DE
4418 }
4419 else
4420 {
4421 /* If !use_existing_cu, this_cu->cu must be NULL. */
4422 gdb_assert (this_cu->cu == NULL);
4423
4424 cu = xmalloc (sizeof (*cu));
4425 init_one_comp_unit (cu, this_cu);
4426
4427 /* If an error occurs while loading, release our storage. */
4428 free_cu_cleanup = make_cleanup (free_heap_comp_unit, cu);
42e7ad6c 4429 }
dee91e82 4430
42e7ad6c
DE
4431 if (cu->header.first_die_offset.cu_off != 0 && ! rereading_dwo_cu)
4432 {
4433 /* We already have the header, there's no need to read it in again. */
4434 info_ptr += cu->header.first_die_offset.cu_off;
4435 }
4436 else
4437 {
3019eac3 4438 if (this_cu->is_debug_types)
dee91e82
DE
4439 {
4440 ULONGEST signature;
42e7ad6c 4441 cu_offset type_offset_in_tu;
dee91e82 4442
4bdcc0c1
DE
4443 info_ptr = read_and_check_type_unit_head (&cu->header, section,
4444 abbrev_section, info_ptr,
42e7ad6c
DE
4445 &signature,
4446 &type_offset_in_tu);
dee91e82 4447
42e7ad6c
DE
4448 /* Since per_cu is the first member of struct signatured_type,
4449 we can go from a pointer to one to a pointer to the other. */
4450 sig_type = (struct signatured_type *) this_cu;
4451 gdb_assert (sig_type->signature == signature);
4452 gdb_assert (sig_type->type_offset_in_tu.cu_off
4453 == type_offset_in_tu.cu_off);
dee91e82
DE
4454 gdb_assert (this_cu->offset.sect_off == cu->header.offset.sect_off);
4455
42e7ad6c
DE
4456 /* LENGTH has not been set yet for type units if we're
4457 using .gdb_index. */
1ce1cefd 4458 this_cu->length = get_cu_length (&cu->header);
3019eac3
DE
4459
4460 /* Establish the type offset that can be used to lookup the type. */
4461 sig_type->type_offset_in_section.sect_off =
4462 this_cu->offset.sect_off + sig_type->type_offset_in_tu.cu_off;
dee91e82
DE
4463 }
4464 else
4465 {
4bdcc0c1
DE
4466 info_ptr = read_and_check_comp_unit_head (&cu->header, section,
4467 abbrev_section,
4468 info_ptr, 0);
dee91e82
DE
4469
4470 gdb_assert (this_cu->offset.sect_off == cu->header.offset.sect_off);
1ce1cefd 4471 gdb_assert (this_cu->length == get_cu_length (&cu->header));
dee91e82
DE
4472 }
4473 }
10b3939b 4474
6caca83c 4475 /* Skip dummy compilation units. */
dee91e82 4476 if (info_ptr >= begin_info_ptr + this_cu->length
6caca83c
CC
4477 || peek_abbrev_code (abfd, info_ptr) == 0)
4478 {
dee91e82 4479 do_cleanups (cleanups);
21b2bd31 4480 return;
6caca83c
CC
4481 }
4482
433df2d4
DE
4483 /* If we don't have them yet, read the abbrevs for this compilation unit.
4484 And if we need to read them now, make sure they're freed when we're
42e7ad6c
DE
4485 done. Note that it's important that if the CU had an abbrev table
4486 on entry we don't free it when we're done: Somewhere up the call stack
4487 it may be in use. */
f4dc4d17
DE
4488 if (abbrev_table != NULL)
4489 {
4490 gdb_assert (cu->abbrev_table == NULL);
4491 gdb_assert (cu->header.abbrev_offset.sect_off
4492 == abbrev_table->offset.sect_off);
4493 cu->abbrev_table = abbrev_table;
4494 }
4495 else if (cu->abbrev_table == NULL)
dee91e82 4496 {
4bdcc0c1 4497 dwarf2_read_abbrevs (cu, abbrev_section);
dee91e82
DE
4498 make_cleanup (dwarf2_free_abbrev_table, cu);
4499 }
42e7ad6c
DE
4500 else if (rereading_dwo_cu)
4501 {
4502 dwarf2_free_abbrev_table (cu);
4503 dwarf2_read_abbrevs (cu, abbrev_section);
4504 }
af703f96 4505
dee91e82 4506 /* Read the top level CU/TU die. */
3019eac3 4507 init_cu_die_reader (&reader, cu, section, NULL);
dee91e82 4508 info_ptr = read_full_die (&reader, &comp_unit_die, info_ptr, &has_children);
93311388 4509
3019eac3
DE
4510 /* If we have a DWO stub, process it and then read in the DWO file.
4511 Note that if USE_EXISTING_OK != 0, and THIS_CU->cu already contains
4512 a DWO CU, that this test will fail. */
4513 attr = dwarf2_attr (comp_unit_die, DW_AT_GNU_dwo_name, cu);
4514 if (attr)
4515 {
4516 char *dwo_name = DW_STRING (attr);
42e7ad6c 4517 const char *comp_dir_string;
3019eac3
DE
4518 struct dwo_unit *dwo_unit;
4519 ULONGEST signature; /* Or dwo_id. */
42e7ad6c 4520 struct attribute *comp_dir, *stmt_list, *low_pc, *high_pc, *ranges;
3019eac3 4521 int i,num_extra_attrs;
4bdcc0c1 4522 struct dwarf2_section_info *dwo_abbrev_section;
3019eac3
DE
4523
4524 if (has_children)
4525 error (_("Dwarf Error: compilation unit with DW_AT_GNU_dwo_name"
4526 " has children (offset 0x%x) [in module %s]"),
4527 this_cu->offset.sect_off, bfd_get_filename (abfd));
4528
4529 /* These attributes aren't processed until later:
4530 DW_AT_stmt_list, DW_AT_low_pc, DW_AT_high_pc, DW_AT_ranges.
4531 However, the attribute is found in the stub which we won't have later.
4532 In order to not impose this complication on the rest of the code,
4533 we read them here and copy them to the DWO CU/TU die. */
3019eac3
DE
4534
4535 /* For TUs in DWO files, the DW_AT_stmt_list attribute lives in the
4536 DWO file. */
42e7ad6c 4537 stmt_list = NULL;
3019eac3
DE
4538 if (! this_cu->is_debug_types)
4539 stmt_list = dwarf2_attr (comp_unit_die, DW_AT_stmt_list, cu);
4540 low_pc = dwarf2_attr (comp_unit_die, DW_AT_low_pc, cu);
4541 high_pc = dwarf2_attr (comp_unit_die, DW_AT_high_pc, cu);
4542 ranges = dwarf2_attr (comp_unit_die, DW_AT_ranges, cu);
42e7ad6c 4543 comp_dir = dwarf2_attr (comp_unit_die, DW_AT_comp_dir, cu);
3019eac3
DE
4544
4545 /* There should be a DW_AT_addr_base attribute here (if needed).
4546 We need the value before we can process DW_FORM_GNU_addr_index. */
4547 cu->addr_base = 0;
3019eac3
DE
4548 attr = dwarf2_attr (comp_unit_die, DW_AT_GNU_addr_base, cu);
4549 if (attr)
2e3cf129
DE
4550 cu->addr_base = DW_UNSND (attr);
4551
4552 /* There should be a DW_AT_ranges_base attribute here (if needed).
4553 We need the value before we can process DW_AT_ranges. */
4554 cu->ranges_base = 0;
4555 attr = dwarf2_attr (comp_unit_die, DW_AT_GNU_ranges_base, cu);
4556 if (attr)
4557 cu->ranges_base = DW_UNSND (attr);
3019eac3
DE
4558
4559 if (this_cu->is_debug_types)
4560 {
4561 gdb_assert (sig_type != NULL);
4562 signature = sig_type->signature;
4563 }
4564 else
4565 {
4566 attr = dwarf2_attr (comp_unit_die, DW_AT_GNU_dwo_id, cu);
4567 if (! attr)
4568 error (_("Dwarf Error: missing dwo_id [in module %s]"),
4569 dwo_name);
4570 signature = DW_UNSND (attr);
4571 }
4572
4573 /* We may need the comp_dir in order to find the DWO file. */
42e7ad6c
DE
4574 comp_dir_string = NULL;
4575 if (comp_dir)
4576 comp_dir_string = DW_STRING (comp_dir);
3019eac3
DE
4577
4578 if (this_cu->is_debug_types)
42e7ad6c 4579 dwo_unit = lookup_dwo_type_unit (sig_type, dwo_name, comp_dir_string);
3019eac3 4580 else
42e7ad6c 4581 dwo_unit = lookup_dwo_comp_unit (this_cu, dwo_name, comp_dir_string,
3019eac3
DE
4582 signature);
4583
4584 if (dwo_unit == NULL)
4585 {
4586 error (_("Dwarf Error: CU at offset 0x%x references unknown DWO"
4587 " with ID %s [in module %s]"),
4588 this_cu->offset.sect_off,
4589 phex (signature, sizeof (signature)),
4590 objfile->name);
4591 }
4592
4593 /* Set up for reading the DWO CU/TU. */
4594 cu->dwo_unit = dwo_unit;
4595 section = dwo_unit->info_or_types_section;
80626a55 4596 dwarf2_read_section (objfile, section);
3019eac3 4597 begin_info_ptr = info_ptr = section->buffer + dwo_unit->offset.sect_off;
4bdcc0c1 4598 dwo_abbrev_section = &dwo_unit->dwo_file->sections.abbrev;
3019eac3
DE
4599 init_cu_die_reader (&reader, cu, section, dwo_unit->dwo_file);
4600
4601 if (this_cu->is_debug_types)
4602 {
4603 ULONGEST signature;
80626a55 4604 cu_offset type_offset_in_tu;
3019eac3 4605
4bdcc0c1
DE
4606 info_ptr = read_and_check_type_unit_head (&cu->header, section,
4607 dwo_abbrev_section,
4608 info_ptr,
80626a55
DE
4609 &signature,
4610 &type_offset_in_tu);
3019eac3
DE
4611 gdb_assert (sig_type->signature == signature);
4612 gdb_assert (dwo_unit->offset.sect_off == cu->header.offset.sect_off);
80626a55
DE
4613 /* For DWOs coming from DWP files, we don't know the CU length
4614 nor the type's offset in the TU until now. */
4615 dwo_unit->length = get_cu_length (&cu->header);
4616 dwo_unit->type_offset_in_tu = type_offset_in_tu;
3019eac3
DE
4617
4618 /* Establish the type offset that can be used to lookup the type.
4619 For DWO files, we don't know it until now. */
4620 sig_type->type_offset_in_section.sect_off =
4621 dwo_unit->offset.sect_off + dwo_unit->type_offset_in_tu.cu_off;
4622 }
4623 else
4624 {
4bdcc0c1
DE
4625 info_ptr = read_and_check_comp_unit_head (&cu->header, section,
4626 dwo_abbrev_section,
4627 info_ptr, 0);
3019eac3 4628 gdb_assert (dwo_unit->offset.sect_off == cu->header.offset.sect_off);
80626a55
DE
4629 /* For DWOs coming from DWP files, we don't know the CU length
4630 until now. */
4631 dwo_unit->length = get_cu_length (&cu->header);
3019eac3
DE
4632 }
4633
4634 /* Discard the original CU's abbrev table, and read the DWO's. */
f4dc4d17
DE
4635 if (abbrev_table == NULL)
4636 {
4637 dwarf2_free_abbrev_table (cu);
4638 dwarf2_read_abbrevs (cu, dwo_abbrev_section);
4639 }
4640 else
4641 {
4642 dwarf2_read_abbrevs (cu, dwo_abbrev_section);
4643 make_cleanup (dwarf2_free_abbrev_table, cu);
4644 }
3019eac3
DE
4645
4646 /* Read in the die, but leave space to copy over the attributes
4647 from the stub. This has the benefit of simplifying the rest of
4648 the code - all the real work is done here. */
4649 num_extra_attrs = ((stmt_list != NULL)
4650 + (low_pc != NULL)
4651 + (high_pc != NULL)
42e7ad6c
DE
4652 + (ranges != NULL)
4653 + (comp_dir != NULL));
3019eac3
DE
4654 info_ptr = read_full_die_1 (&reader, &comp_unit_die, info_ptr,
4655 &has_children, num_extra_attrs);
4656
4657 /* Copy over the attributes from the stub to the DWO die. */
4658 i = comp_unit_die->num_attrs;
4659 if (stmt_list != NULL)
4660 comp_unit_die->attrs[i++] = *stmt_list;
4661 if (low_pc != NULL)
4662 comp_unit_die->attrs[i++] = *low_pc;
4663 if (high_pc != NULL)
4664 comp_unit_die->attrs[i++] = *high_pc;
4665 if (ranges != NULL)
4666 comp_unit_die->attrs[i++] = *ranges;
42e7ad6c
DE
4667 if (comp_dir != NULL)
4668 comp_unit_die->attrs[i++] = *comp_dir;
3019eac3
DE
4669 comp_unit_die->num_attrs += num_extra_attrs;
4670
4671 /* Skip dummy compilation units. */
4672 if (info_ptr >= begin_info_ptr + dwo_unit->length
4673 || peek_abbrev_code (abfd, info_ptr) == 0)
4674 {
4675 do_cleanups (cleanups);
4676 return;
4677 }
4678 }
4679
dee91e82
DE
4680 die_reader_func (&reader, info_ptr, comp_unit_die, has_children, data);
4681
4682 if (free_cu_cleanup != NULL)
348e048f 4683 {
dee91e82
DE
4684 if (keep)
4685 {
4686 /* We've successfully allocated this compilation unit. Let our
4687 caller clean it up when finished with it. */
4688 discard_cleanups (free_cu_cleanup);
4689
4690 /* We can only discard free_cu_cleanup and all subsequent cleanups.
4691 So we have to manually free the abbrev table. */
4692 dwarf2_free_abbrev_table (cu);
4693
4694 /* Link this CU into read_in_chain. */
4695 this_cu->cu->read_in_chain = dwarf2_per_objfile->read_in_chain;
4696 dwarf2_per_objfile->read_in_chain = this_cu;
4697 }
4698 else
4699 do_cleanups (free_cu_cleanup);
348e048f 4700 }
dee91e82
DE
4701
4702 do_cleanups (cleanups);
4703}
4704
3019eac3
DE
4705/* Read CU/TU THIS_CU in section SECTION,
4706 but do not follow DW_AT_GNU_dwo_name if present.
80626a55
DE
4707 DWOP_FILE, if non-NULL, is the DWO/DWP file to read (the caller is assumed
4708 to have already done the lookup to find the DWO/DWP file).
dee91e82
DE
4709
4710 The caller is required to fill in THIS_CU->section, THIS_CU->offset, and
3019eac3 4711 THIS_CU->is_debug_types, but nothing else.
dee91e82
DE
4712
4713 We fill in THIS_CU->length.
4714
4715 WARNING: If THIS_CU is a "dummy CU" (used as filler by the incremental
4716 linker) then DIE_READER_FUNC will not get called.
4717
4718 THIS_CU->cu is always freed when done.
3019eac3
DE
4719 This is done in order to not leave THIS_CU->cu in a state where we have
4720 to care whether it refers to the "main" CU or the DWO CU. */
dee91e82
DE
4721
4722static void
4723init_cutu_and_read_dies_no_follow (struct dwarf2_per_cu_data *this_cu,
4724 struct dwarf2_section_info *abbrev_section,
3019eac3 4725 struct dwo_file *dwo_file,
dee91e82
DE
4726 die_reader_func_ftype *die_reader_func,
4727 void *data)
4728{
4729 struct objfile *objfile = dwarf2_per_objfile->objfile;
3019eac3
DE
4730 struct dwarf2_section_info *section = this_cu->info_or_types_section;
4731 bfd *abfd = section->asection->owner;
dee91e82
DE
4732 struct dwarf2_cu cu;
4733 gdb_byte *begin_info_ptr, *info_ptr;
4734 struct die_reader_specs reader;
4735 struct cleanup *cleanups;
4736 struct die_info *comp_unit_die;
4737 int has_children;
4738
09406207
DE
4739 if (dwarf2_die_debug)
4740 fprintf_unfiltered (gdb_stdlog, "Reading %s unit at offset 0x%x\n",
4741 this_cu->is_debug_types ? "type" : "comp",
4742 this_cu->offset.sect_off);
4743
dee91e82
DE
4744 gdb_assert (this_cu->cu == NULL);
4745
dee91e82
DE
4746 /* This is cheap if the section is already read in. */
4747 dwarf2_read_section (objfile, section);
4748
4749 init_one_comp_unit (&cu, this_cu);
4750
4751 cleanups = make_cleanup (free_stack_comp_unit, &cu);
4752
4753 begin_info_ptr = info_ptr = section->buffer + this_cu->offset.sect_off;
4bdcc0c1
DE
4754 info_ptr = read_and_check_comp_unit_head (&cu.header, section,
4755 abbrev_section, info_ptr,
3019eac3 4756 this_cu->is_debug_types);
dee91e82 4757
1ce1cefd 4758 this_cu->length = get_cu_length (&cu.header);
dee91e82
DE
4759
4760 /* Skip dummy compilation units. */
4761 if (info_ptr >= begin_info_ptr + this_cu->length
4762 || peek_abbrev_code (abfd, info_ptr) == 0)
c906108c 4763 {
dee91e82 4764 do_cleanups (cleanups);
21b2bd31 4765 return;
93311388 4766 }
72bf9492 4767
dee91e82
DE
4768 dwarf2_read_abbrevs (&cu, abbrev_section);
4769 make_cleanup (dwarf2_free_abbrev_table, &cu);
4770
3019eac3 4771 init_cu_die_reader (&reader, &cu, section, dwo_file);
dee91e82
DE
4772 info_ptr = read_full_die (&reader, &comp_unit_die, info_ptr, &has_children);
4773
4774 die_reader_func (&reader, info_ptr, comp_unit_die, has_children, data);
4775
4776 do_cleanups (cleanups);
4777}
4778
3019eac3
DE
4779/* Read a CU/TU, except that this does not look for DW_AT_GNU_dwo_name and
4780 does not lookup the specified DWO file.
4781 This cannot be used to read DWO files.
dee91e82
DE
4782
4783 THIS_CU->cu is always freed when done.
3019eac3
DE
4784 This is done in order to not leave THIS_CU->cu in a state where we have
4785 to care whether it refers to the "main" CU or the DWO CU.
4786 We can revisit this if the data shows there's a performance issue. */
dee91e82
DE
4787
4788static void
4789init_cutu_and_read_dies_simple (struct dwarf2_per_cu_data *this_cu,
4790 die_reader_func_ftype *die_reader_func,
4791 void *data)
4792{
4793 init_cutu_and_read_dies_no_follow (this_cu,
36586728 4794 get_abbrev_section_for_cu (this_cu),
3019eac3 4795 NULL,
dee91e82
DE
4796 die_reader_func, data);
4797}
4798
f4dc4d17
DE
4799/* Create a psymtab named NAME and assign it to PER_CU.
4800
4801 The caller must fill in the following details:
4802 dirname, textlow, texthigh. */
4803
4804static struct partial_symtab *
4805create_partial_symtab (struct dwarf2_per_cu_data *per_cu, const char *name)
4806{
4807 struct objfile *objfile = per_cu->objfile;
4808 struct partial_symtab *pst;
4809
4810 pst = start_psymtab_common (objfile, objfile->section_offsets,
4811 name, 0,
4812 objfile->global_psymbols.next,
4813 objfile->static_psymbols.next);
4814
4815 pst->psymtabs_addrmap_supported = 1;
4816
4817 /* This is the glue that links PST into GDB's symbol API. */
4818 pst->read_symtab_private = per_cu;
4819 pst->read_symtab = dwarf2_psymtab_to_symtab;
4820 per_cu->v.psymtab = pst;
4821
4822 return pst;
4823}
4824
dee91e82
DE
4825/* die_reader_func for process_psymtab_comp_unit. */
4826
4827static void
4828process_psymtab_comp_unit_reader (const struct die_reader_specs *reader,
4829 gdb_byte *info_ptr,
4830 struct die_info *comp_unit_die,
4831 int has_children,
4832 void *data)
4833{
4834 struct dwarf2_cu *cu = reader->cu;
4835 struct objfile *objfile = cu->objfile;
4836 struct dwarf2_per_cu_data *per_cu = cu->per_cu;
dee91e82
DE
4837 struct attribute *attr;
4838 CORE_ADDR baseaddr;
4839 CORE_ADDR best_lowpc = 0, best_highpc = 0;
4840 struct partial_symtab *pst;
4841 int has_pc_info;
4842 const char *filename;
95554aad 4843 int *want_partial_unit_ptr = data;
dee91e82 4844
95554aad
TT
4845 if (comp_unit_die->tag == DW_TAG_partial_unit
4846 && (want_partial_unit_ptr == NULL
4847 || !*want_partial_unit_ptr))
dee91e82
DE
4848 return;
4849
f4dc4d17
DE
4850 gdb_assert (! per_cu->is_debug_types);
4851
95554aad 4852 prepare_one_comp_unit (cu, comp_unit_die, language_minimal);
dee91e82
DE
4853
4854 cu->list_in_scope = &file_symbols;
c906108c 4855
93311388 4856 /* Allocate a new partial symbol table structure. */
dee91e82 4857 attr = dwarf2_attr (comp_unit_die, DW_AT_name, cu);
3e2a0cee
TT
4858 if (attr == NULL || !DW_STRING (attr))
4859 filename = "";
4860 else
4861 filename = DW_STRING (attr);
72bf9492 4862
f4dc4d17
DE
4863 pst = create_partial_symtab (per_cu, filename);
4864
4865 /* This must be done before calling dwarf2_build_include_psymtabs. */
dee91e82 4866 attr = dwarf2_attr (comp_unit_die, DW_AT_comp_dir, cu);
d85a05f0
DJ
4867 if (attr != NULL)
4868 pst->dirname = DW_STRING (attr);
72bf9492 4869
93311388 4870 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
e7c27a73 4871
dee91e82 4872 dwarf2_find_base_address (comp_unit_die, cu);
d85a05f0 4873
93311388
DE
4874 /* Possibly set the default values of LOWPC and HIGHPC from
4875 `DW_AT_ranges'. */
d85a05f0 4876 has_pc_info = dwarf2_get_pc_bounds (comp_unit_die, &best_lowpc,
dee91e82 4877 &best_highpc, cu, pst);
d85a05f0 4878 if (has_pc_info == 1 && best_lowpc < best_highpc)
93311388
DE
4879 /* Store the contiguous range if it is not empty; it can be empty for
4880 CUs with no code. */
4881 addrmap_set_empty (objfile->psymtabs_addrmap,
d85a05f0
DJ
4882 best_lowpc + baseaddr,
4883 best_highpc + baseaddr - 1, pst);
93311388
DE
4884
4885 /* Check if comp unit has_children.
4886 If so, read the rest of the partial symbols from this comp unit.
0963b4bd 4887 If not, there's no more debug_info for this comp unit. */
d85a05f0 4888 if (has_children)
93311388
DE
4889 {
4890 struct partial_die_info *first_die;
4891 CORE_ADDR lowpc, highpc;
31ffec48 4892
93311388
DE
4893 lowpc = ((CORE_ADDR) -1);
4894 highpc = ((CORE_ADDR) 0);
c906108c 4895
dee91e82 4896 first_die = load_partial_dies (reader, info_ptr, 1);
c906108c 4897
93311388 4898 scan_partial_symbols (first_die, &lowpc, &highpc,
dee91e82 4899 ! has_pc_info, cu);
57c22c6c 4900
93311388
DE
4901 /* If we didn't find a lowpc, set it to highpc to avoid
4902 complaints from `maint check'. */
4903 if (lowpc == ((CORE_ADDR) -1))
4904 lowpc = highpc;
10b3939b 4905
93311388
DE
4906 /* If the compilation unit didn't have an explicit address range,
4907 then use the information extracted from its child dies. */
d85a05f0 4908 if (! has_pc_info)
93311388 4909 {
d85a05f0
DJ
4910 best_lowpc = lowpc;
4911 best_highpc = highpc;
93311388
DE
4912 }
4913 }
d85a05f0
DJ
4914 pst->textlow = best_lowpc + baseaddr;
4915 pst->texthigh = best_highpc + baseaddr;
c906108c 4916
93311388
DE
4917 pst->n_global_syms = objfile->global_psymbols.next -
4918 (objfile->global_psymbols.list + pst->globals_offset);
4919 pst->n_static_syms = objfile->static_psymbols.next -
4920 (objfile->static_psymbols.list + pst->statics_offset);
4921 sort_pst_symbols (pst);
c906108c 4922
f4dc4d17 4923 if (!VEC_empty (dwarf2_per_cu_ptr, cu->per_cu->s.imported_symtabs))
95554aad
TT
4924 {
4925 int i;
f4dc4d17 4926 int len = VEC_length (dwarf2_per_cu_ptr, cu->per_cu->s.imported_symtabs);
95554aad
TT
4927 struct dwarf2_per_cu_data *iter;
4928
4929 /* Fill in 'dependencies' here; we fill in 'users' in a
4930 post-pass. */
4931 pst->number_of_dependencies = len;
4932 pst->dependencies = obstack_alloc (&objfile->objfile_obstack,
4933 len * sizeof (struct symtab *));
4934 for (i = 0;
f4dc4d17 4935 VEC_iterate (dwarf2_per_cu_ptr, cu->per_cu->s.imported_symtabs,
95554aad
TT
4936 i, iter);
4937 ++i)
4938 pst->dependencies[i] = iter->v.psymtab;
4939
f4dc4d17 4940 VEC_free (dwarf2_per_cu_ptr, cu->per_cu->s.imported_symtabs);
95554aad
TT
4941 }
4942
f4dc4d17
DE
4943 /* Get the list of files included in the current compilation unit,
4944 and build a psymtab for each of them. */
4945 dwarf2_build_include_psymtabs (cu, comp_unit_die, pst);
09406207
DE
4946
4947 if (dwarf2_read_debug)
4948 {
4949 struct gdbarch *gdbarch = get_objfile_arch (objfile);
4950
4951 fprintf_unfiltered (gdb_stdlog,
844226d6 4952 "Psymtab for %s unit @0x%x: %s - %s"
09406207
DE
4953 ", %d global, %d static syms\n",
4954 per_cu->is_debug_types ? "type" : "comp",
4955 per_cu->offset.sect_off,
4956 paddress (gdbarch, pst->textlow),
4957 paddress (gdbarch, pst->texthigh),
4958 pst->n_global_syms, pst->n_static_syms);
4959 }
dee91e82 4960}
ae038cb0 4961
dee91e82
DE
4962/* Subroutine of dwarf2_build_psymtabs_hard to simplify it.
4963 Process compilation unit THIS_CU for a psymtab. */
4964
4965static void
95554aad
TT
4966process_psymtab_comp_unit (struct dwarf2_per_cu_data *this_cu,
4967 int want_partial_unit)
dee91e82
DE
4968{
4969 /* If this compilation unit was already read in, free the
4970 cached copy in order to read it in again. This is
4971 necessary because we skipped some symbols when we first
4972 read in the compilation unit (see load_partial_dies).
4973 This problem could be avoided, but the benefit is unclear. */
4974 if (this_cu->cu != NULL)
4975 free_one_cached_comp_unit (this_cu);
4976
3019eac3 4977 gdb_assert (! this_cu->is_debug_types);
f4dc4d17
DE
4978 init_cutu_and_read_dies (this_cu, NULL, 0, 0,
4979 process_psymtab_comp_unit_reader,
95554aad 4980 &want_partial_unit);
dee91e82
DE
4981
4982 /* Age out any secondary CUs. */
4983 age_cached_comp_units ();
93311388 4984}
ff013f42 4985
f4dc4d17
DE
4986static hashval_t
4987hash_type_unit_group (const void *item)
4988{
094b34ac 4989 const struct type_unit_group *tu_group = item;
f4dc4d17 4990
094b34ac 4991 return hash_stmt_list_entry (&tu_group->hash);
f4dc4d17 4992}
348e048f
DE
4993
4994static int
f4dc4d17 4995eq_type_unit_group (const void *item_lhs, const void *item_rhs)
348e048f 4996{
f4dc4d17
DE
4997 const struct type_unit_group *lhs = item_lhs;
4998 const struct type_unit_group *rhs = item_rhs;
348e048f 4999
094b34ac 5000 return eq_stmt_list_entry (&lhs->hash, &rhs->hash);
f4dc4d17 5001}
348e048f 5002
f4dc4d17
DE
5003/* Allocate a hash table for type unit groups. */
5004
5005static htab_t
5006allocate_type_unit_groups_table (void)
5007{
5008 return htab_create_alloc_ex (3,
5009 hash_type_unit_group,
5010 eq_type_unit_group,
5011 NULL,
5012 &dwarf2_per_objfile->objfile->objfile_obstack,
5013 hashtab_obstack_allocate,
5014 dummy_obstack_deallocate);
5015}
dee91e82 5016
f4dc4d17
DE
5017/* Type units that don't have DW_AT_stmt_list are grouped into their own
5018 partial symtabs. We combine several TUs per psymtab to not let the size
5019 of any one psymtab grow too big. */
5020#define NO_STMT_LIST_TYPE_UNIT_PSYMTAB (1 << 31)
5021#define NO_STMT_LIST_TYPE_UNIT_PSYMTAB_SIZE 10
dee91e82 5022
094b34ac 5023/* Helper routine for get_type_unit_group.
f4dc4d17
DE
5024 Create the type_unit_group object used to hold one or more TUs. */
5025
5026static struct type_unit_group *
094b34ac 5027create_type_unit_group (struct dwarf2_cu *cu, sect_offset line_offset_struct)
f4dc4d17
DE
5028{
5029 struct objfile *objfile = dwarf2_per_objfile->objfile;
094b34ac 5030 struct dwarf2_per_cu_data *per_cu;
f4dc4d17 5031 struct type_unit_group *tu_group;
f4dc4d17
DE
5032
5033 tu_group = OBSTACK_ZALLOC (&objfile->objfile_obstack,
5034 struct type_unit_group);
094b34ac 5035 per_cu = &tu_group->per_cu;
f4dc4d17
DE
5036 per_cu->objfile = objfile;
5037 per_cu->is_debug_types = 1;
5038 per_cu->s.type_unit_group = tu_group;
5039
094b34ac
DE
5040 if (dwarf2_per_objfile->using_index)
5041 {
5042 per_cu->v.quick = OBSTACK_ZALLOC (&objfile->objfile_obstack,
5043 struct dwarf2_per_cu_quick_data);
5044 tu_group->t.first_tu = cu->per_cu;
5045 }
5046 else
5047 {
5048 unsigned int line_offset = line_offset_struct.sect_off;
5049 struct partial_symtab *pst;
5050 char *name;
5051
5052 /* Give the symtab a useful name for debug purposes. */
5053 if ((line_offset & NO_STMT_LIST_TYPE_UNIT_PSYMTAB) != 0)
5054 name = xstrprintf ("<type_units_%d>",
5055 (line_offset & ~NO_STMT_LIST_TYPE_UNIT_PSYMTAB));
5056 else
5057 name = xstrprintf ("<type_units_at_0x%x>", line_offset);
5058
5059 pst = create_partial_symtab (per_cu, name);
5060 pst->anonymous = 1;
f4dc4d17 5061
094b34ac
DE
5062 xfree (name);
5063 }
f4dc4d17 5064
094b34ac
DE
5065 tu_group->hash.dwo_unit = cu->dwo_unit;
5066 tu_group->hash.line_offset = line_offset_struct;
f4dc4d17
DE
5067
5068 return tu_group;
5069}
5070
094b34ac
DE
5071/* Look up the type_unit_group for type unit CU, and create it if necessary.
5072 STMT_LIST is a DW_AT_stmt_list attribute. */
f4dc4d17
DE
5073
5074static struct type_unit_group *
094b34ac 5075get_type_unit_group (struct dwarf2_cu *cu, struct attribute *stmt_list)
f4dc4d17
DE
5076{
5077 struct tu_stats *tu_stats = &dwarf2_per_objfile->tu_stats;
5078 struct type_unit_group *tu_group;
5079 void **slot;
5080 unsigned int line_offset;
5081 struct type_unit_group type_unit_group_for_lookup;
5082
5083 if (dwarf2_per_objfile->type_unit_groups == NULL)
5084 {
5085 dwarf2_per_objfile->type_unit_groups =
5086 allocate_type_unit_groups_table ();
5087 }
5088
5089 /* Do we need to create a new group, or can we use an existing one? */
5090
5091 if (stmt_list)
5092 {
5093 line_offset = DW_UNSND (stmt_list);
5094 ++tu_stats->nr_symtab_sharers;
5095 }
5096 else
5097 {
5098 /* Ugh, no stmt_list. Rare, but we have to handle it.
5099 We can do various things here like create one group per TU or
5100 spread them over multiple groups to split up the expansion work.
5101 To avoid worst case scenarios (too many groups or too large groups)
5102 we, umm, group them in bunches. */
5103 line_offset = (NO_STMT_LIST_TYPE_UNIT_PSYMTAB
5104 | (tu_stats->nr_stmt_less_type_units
5105 / NO_STMT_LIST_TYPE_UNIT_PSYMTAB_SIZE));
5106 ++tu_stats->nr_stmt_less_type_units;
5107 }
5108
094b34ac
DE
5109 type_unit_group_for_lookup.hash.dwo_unit = cu->dwo_unit;
5110 type_unit_group_for_lookup.hash.line_offset.sect_off = line_offset;
f4dc4d17
DE
5111 slot = htab_find_slot (dwarf2_per_objfile->type_unit_groups,
5112 &type_unit_group_for_lookup, INSERT);
5113 if (*slot != NULL)
5114 {
5115 tu_group = *slot;
5116 gdb_assert (tu_group != NULL);
5117 }
5118 else
5119 {
5120 sect_offset line_offset_struct;
5121
5122 line_offset_struct.sect_off = line_offset;
094b34ac 5123 tu_group = create_type_unit_group (cu, line_offset_struct);
f4dc4d17
DE
5124 *slot = tu_group;
5125 ++tu_stats->nr_symtabs;
5126 }
5127
5128 return tu_group;
5129}
5130
5131/* Struct used to sort TUs by their abbreviation table offset. */
5132
5133struct tu_abbrev_offset
5134{
5135 struct signatured_type *sig_type;
5136 sect_offset abbrev_offset;
5137};
5138
5139/* Helper routine for build_type_unit_groups, passed to qsort. */
5140
5141static int
5142sort_tu_by_abbrev_offset (const void *ap, const void *bp)
5143{
5144 const struct tu_abbrev_offset * const *a = ap;
5145 const struct tu_abbrev_offset * const *b = bp;
5146 unsigned int aoff = (*a)->abbrev_offset.sect_off;
5147 unsigned int boff = (*b)->abbrev_offset.sect_off;
5148
5149 return (aoff > boff) - (aoff < boff);
5150}
5151
5152/* A helper function to add a type_unit_group to a table. */
5153
5154static int
5155add_type_unit_group_to_table (void **slot, void *datum)
5156{
5157 struct type_unit_group *tu_group = *slot;
5158 struct type_unit_group ***datap = datum;
5159
5160 **datap = tu_group;
5161 ++*datap;
5162
5163 return 1;
5164}
5165
5166/* Efficiently read all the type units, calling init_cutu_and_read_dies on
5167 each one passing FUNC,DATA.
5168
5169 The efficiency is because we sort TUs by the abbrev table they use and
5170 only read each abbrev table once. In one program there are 200K TUs
5171 sharing 8K abbrev tables.
5172
5173 The main purpose of this function is to support building the
5174 dwarf2_per_objfile->type_unit_groups table.
5175 TUs typically share the DW_AT_stmt_list of the CU they came from, so we
5176 can collapse the search space by grouping them by stmt_list.
5177 The savings can be significant, in the same program from above the 200K TUs
5178 share 8K stmt_list tables.
5179
5180 FUNC is expected to call get_type_unit_group, which will create the
5181 struct type_unit_group if necessary and add it to
5182 dwarf2_per_objfile->type_unit_groups. */
5183
5184static void
5185build_type_unit_groups (die_reader_func_ftype *func, void *data)
5186{
5187 struct objfile *objfile = dwarf2_per_objfile->objfile;
5188 struct tu_stats *tu_stats = &dwarf2_per_objfile->tu_stats;
5189 struct cleanup *cleanups;
5190 struct abbrev_table *abbrev_table;
5191 sect_offset abbrev_offset;
5192 struct tu_abbrev_offset *sorted_by_abbrev;
5193 struct type_unit_group **iter;
5194 int i;
5195
5196 /* It's up to the caller to not call us multiple times. */
5197 gdb_assert (dwarf2_per_objfile->type_unit_groups == NULL);
5198
5199 if (dwarf2_per_objfile->n_type_units == 0)
5200 return;
5201
5202 /* TUs typically share abbrev tables, and there can be way more TUs than
5203 abbrev tables. Sort by abbrev table to reduce the number of times we
5204 read each abbrev table in.
5205 Alternatives are to punt or to maintain a cache of abbrev tables.
5206 This is simpler and efficient enough for now.
5207
5208 Later we group TUs by their DW_AT_stmt_list value (as this defines the
5209 symtab to use). Typically TUs with the same abbrev offset have the same
5210 stmt_list value too so in practice this should work well.
5211
5212 The basic algorithm here is:
5213
5214 sort TUs by abbrev table
5215 for each TU with same abbrev table:
5216 read abbrev table if first user
5217 read TU top level DIE
5218 [IWBN if DWO skeletons had DW_AT_stmt_list]
5219 call FUNC */
5220
5221 if (dwarf2_read_debug)
5222 fprintf_unfiltered (gdb_stdlog, "Building type unit groups ...\n");
5223
5224 /* Sort in a separate table to maintain the order of all_type_units
5225 for .gdb_index: TU indices directly index all_type_units. */
5226 sorted_by_abbrev = XNEWVEC (struct tu_abbrev_offset,
5227 dwarf2_per_objfile->n_type_units);
5228 for (i = 0; i < dwarf2_per_objfile->n_type_units; ++i)
5229 {
5230 struct signatured_type *sig_type = dwarf2_per_objfile->all_type_units[i];
5231
5232 sorted_by_abbrev[i].sig_type = sig_type;
5233 sorted_by_abbrev[i].abbrev_offset =
5234 read_abbrev_offset (sig_type->per_cu.info_or_types_section,
5235 sig_type->per_cu.offset);
5236 }
5237 cleanups = make_cleanup (xfree, sorted_by_abbrev);
5238 qsort (sorted_by_abbrev, dwarf2_per_objfile->n_type_units,
5239 sizeof (struct tu_abbrev_offset), sort_tu_by_abbrev_offset);
5240
094b34ac
DE
5241 /* Note: In the .gdb_index case, get_type_unit_group may have already been
5242 called any number of times, so we don't reset tu_stats here. */
5243
f4dc4d17
DE
5244 abbrev_offset.sect_off = ~(unsigned) 0;
5245 abbrev_table = NULL;
5246 make_cleanup (abbrev_table_free_cleanup, &abbrev_table);
5247
5248 for (i = 0; i < dwarf2_per_objfile->n_type_units; ++i)
5249 {
5250 const struct tu_abbrev_offset *tu = &sorted_by_abbrev[i];
5251
5252 /* Switch to the next abbrev table if necessary. */
5253 if (abbrev_table == NULL
5254 || tu->abbrev_offset.sect_off != abbrev_offset.sect_off)
5255 {
5256 if (abbrev_table != NULL)
5257 {
5258 abbrev_table_free (abbrev_table);
5259 /* Reset to NULL in case abbrev_table_read_table throws
5260 an error: abbrev_table_free_cleanup will get called. */
5261 abbrev_table = NULL;
5262 }
5263 abbrev_offset = tu->abbrev_offset;
5264 abbrev_table =
5265 abbrev_table_read_table (&dwarf2_per_objfile->abbrev,
5266 abbrev_offset);
5267 ++tu_stats->nr_uniq_abbrev_tables;
5268 }
5269
5270 init_cutu_and_read_dies (&tu->sig_type->per_cu, abbrev_table, 0, 0,
5271 func, data);
5272 }
5273
5274 /* Create a vector of pointers to primary type units to make it easy to
5275 iterate over them and CUs. See dw2_get_primary_cu. */
5276 dwarf2_per_objfile->n_type_unit_groups =
5277 htab_elements (dwarf2_per_objfile->type_unit_groups);
5278 dwarf2_per_objfile->all_type_unit_groups =
5279 obstack_alloc (&objfile->objfile_obstack,
5280 dwarf2_per_objfile->n_type_unit_groups
5281 * sizeof (struct type_unit_group *));
5282 iter = &dwarf2_per_objfile->all_type_unit_groups[0];
5283 htab_traverse_noresize (dwarf2_per_objfile->type_unit_groups,
5284 add_type_unit_group_to_table, &iter);
5285 gdb_assert (iter - &dwarf2_per_objfile->all_type_unit_groups[0]
5286 == dwarf2_per_objfile->n_type_unit_groups);
5287
5288 do_cleanups (cleanups);
5289
5290 if (dwarf2_read_debug)
5291 {
5292 fprintf_unfiltered (gdb_stdlog, "Done building type unit groups:\n");
5293 fprintf_unfiltered (gdb_stdlog, " %d TUs\n",
5294 dwarf2_per_objfile->n_type_units);
5295 fprintf_unfiltered (gdb_stdlog, " %d uniq abbrev tables\n",
5296 tu_stats->nr_uniq_abbrev_tables);
5297 fprintf_unfiltered (gdb_stdlog, " %d symtabs from stmt_list entries\n",
5298 tu_stats->nr_symtabs);
5299 fprintf_unfiltered (gdb_stdlog, " %d symtab sharers\n",
5300 tu_stats->nr_symtab_sharers);
5301 fprintf_unfiltered (gdb_stdlog, " %d type units without a stmt_list\n",
5302 tu_stats->nr_stmt_less_type_units);
5303 }
5304}
5305
5306/* Reader function for build_type_psymtabs. */
5307
5308static void
5309build_type_psymtabs_reader (const struct die_reader_specs *reader,
5310 gdb_byte *info_ptr,
5311 struct die_info *type_unit_die,
5312 int has_children,
5313 void *data)
5314{
5315 struct objfile *objfile = dwarf2_per_objfile->objfile;
5316 struct dwarf2_cu *cu = reader->cu;
5317 struct dwarf2_per_cu_data *per_cu = cu->per_cu;
5318 struct type_unit_group *tu_group;
5319 struct attribute *attr;
5320 struct partial_die_info *first_die;
5321 CORE_ADDR lowpc, highpc;
5322 struct partial_symtab *pst;
5323
5324 gdb_assert (data == NULL);
5325
5326 if (! has_children)
5327 return;
5328
5329 attr = dwarf2_attr_no_follow (type_unit_die, DW_AT_stmt_list);
094b34ac 5330 tu_group = get_type_unit_group (cu, attr);
f4dc4d17 5331
094b34ac 5332 VEC_safe_push (dwarf2_per_cu_ptr, tu_group->t.tus, per_cu);
f4dc4d17
DE
5333
5334 prepare_one_comp_unit (cu, type_unit_die, language_minimal);
5335 cu->list_in_scope = &file_symbols;
5336 pst = create_partial_symtab (per_cu, "");
5337 pst->anonymous = 1;
5338
5339 first_die = load_partial_dies (reader, info_ptr, 1);
5340
5341 lowpc = (CORE_ADDR) -1;
5342 highpc = (CORE_ADDR) 0;
5343 scan_partial_symbols (first_die, &lowpc, &highpc, 0, cu);
5344
5345 pst->n_global_syms = objfile->global_psymbols.next -
5346 (objfile->global_psymbols.list + pst->globals_offset);
5347 pst->n_static_syms = objfile->static_psymbols.next -
5348 (objfile->static_psymbols.list + pst->statics_offset);
5349 sort_pst_symbols (pst);
5350}
5351
5352/* Traversal function for build_type_psymtabs. */
5353
5354static int
5355build_type_psymtab_dependencies (void **slot, void *info)
5356{
5357 struct objfile *objfile = dwarf2_per_objfile->objfile;
5358 struct type_unit_group *tu_group = (struct type_unit_group *) *slot;
094b34ac 5359 struct dwarf2_per_cu_data *per_cu = &tu_group->per_cu;
f4dc4d17 5360 struct partial_symtab *pst = per_cu->v.psymtab;
094b34ac 5361 int len = VEC_length (dwarf2_per_cu_ptr, tu_group->t.tus);
f4dc4d17
DE
5362 struct dwarf2_per_cu_data *iter;
5363 int i;
5364
5365 gdb_assert (len > 0);
5366
5367 pst->number_of_dependencies = len;
5368 pst->dependencies = obstack_alloc (&objfile->objfile_obstack,
5369 len * sizeof (struct psymtab *));
5370 for (i = 0;
094b34ac 5371 VEC_iterate (dwarf2_per_cu_ptr, tu_group->t.tus, i, iter);
f4dc4d17
DE
5372 ++i)
5373 {
5374 pst->dependencies[i] = iter->v.psymtab;
5375 iter->s.type_unit_group = tu_group;
5376 }
5377
094b34ac 5378 VEC_free (dwarf2_per_cu_ptr, tu_group->t.tus);
348e048f
DE
5379
5380 return 1;
5381}
5382
5383/* Subroutine of dwarf2_build_psymtabs_hard to simplify it.
5384 Build partial symbol tables for the .debug_types comp-units. */
5385
5386static void
5387build_type_psymtabs (struct objfile *objfile)
5388{
0e50663e 5389 if (! create_all_type_units (objfile))
348e048f
DE
5390 return;
5391
f4dc4d17
DE
5392 build_type_unit_groups (build_type_psymtabs_reader, NULL);
5393
5394 /* Now that all TUs have been processed we can fill in the dependencies. */
5395 htab_traverse_noresize (dwarf2_per_objfile->type_unit_groups,
5396 build_type_psymtab_dependencies, NULL);
348e048f
DE
5397}
5398
60606b2c
TT
5399/* A cleanup function that clears objfile's psymtabs_addrmap field. */
5400
5401static void
5402psymtabs_addrmap_cleanup (void *o)
5403{
5404 struct objfile *objfile = o;
ec61707d 5405
60606b2c
TT
5406 objfile->psymtabs_addrmap = NULL;
5407}
5408
95554aad
TT
5409/* Compute the 'user' field for each psymtab in OBJFILE. */
5410
5411static void
5412set_partial_user (struct objfile *objfile)
5413{
5414 int i;
5415
5416 for (i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
5417 {
5418 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
5419 struct partial_symtab *pst = per_cu->v.psymtab;
5420 int j;
5421
36586728
TT
5422 if (pst == NULL)
5423 continue;
5424
95554aad
TT
5425 for (j = 0; j < pst->number_of_dependencies; ++j)
5426 {
5427 /* Set the 'user' field only if it is not already set. */
5428 if (pst->dependencies[j]->user == NULL)
5429 pst->dependencies[j]->user = pst;
5430 }
5431 }
5432}
5433
93311388
DE
5434/* Build the partial symbol table by doing a quick pass through the
5435 .debug_info and .debug_abbrev sections. */
72bf9492 5436
93311388 5437static void
c67a9c90 5438dwarf2_build_psymtabs_hard (struct objfile *objfile)
93311388 5439{
60606b2c
TT
5440 struct cleanup *back_to, *addrmap_cleanup;
5441 struct obstack temp_obstack;
21b2bd31 5442 int i;
93311388 5443
45cfd468
DE
5444 if (dwarf2_read_debug)
5445 {
5446 fprintf_unfiltered (gdb_stdlog, "Building psymtabs of objfile %s ...\n",
5447 objfile->name);
5448 }
5449
98bfdba5
PA
5450 dwarf2_per_objfile->reading_partial_symbols = 1;
5451
be391dca 5452 dwarf2_read_section (objfile, &dwarf2_per_objfile->info);
91c24f0a 5453
93311388
DE
5454 /* Any cached compilation units will be linked by the per-objfile
5455 read_in_chain. Make sure to free them when we're done. */
5456 back_to = make_cleanup (free_cached_comp_units, NULL);
72bf9492 5457
348e048f
DE
5458 build_type_psymtabs (objfile);
5459
93311388 5460 create_all_comp_units (objfile);
c906108c 5461
60606b2c
TT
5462 /* Create a temporary address map on a temporary obstack. We later
5463 copy this to the final obstack. */
5464 obstack_init (&temp_obstack);
5465 make_cleanup_obstack_free (&temp_obstack);
5466 objfile->psymtabs_addrmap = addrmap_create_mutable (&temp_obstack);
5467 addrmap_cleanup = make_cleanup (psymtabs_addrmap_cleanup, objfile);
72bf9492 5468
21b2bd31 5469 for (i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
93311388 5470 {
21b2bd31 5471 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
aaa75496 5472
95554aad 5473 process_psymtab_comp_unit (per_cu, 0);
c906108c 5474 }
ff013f42 5475
95554aad
TT
5476 set_partial_user (objfile);
5477
ff013f42
JK
5478 objfile->psymtabs_addrmap = addrmap_create_fixed (objfile->psymtabs_addrmap,
5479 &objfile->objfile_obstack);
60606b2c 5480 discard_cleanups (addrmap_cleanup);
ff013f42 5481
ae038cb0 5482 do_cleanups (back_to);
45cfd468
DE
5483
5484 if (dwarf2_read_debug)
5485 fprintf_unfiltered (gdb_stdlog, "Done building psymtabs of %s\n",
5486 objfile->name);
ae038cb0
DJ
5487}
5488
3019eac3 5489/* die_reader_func for load_partial_comp_unit. */
ae038cb0
DJ
5490
5491static void
dee91e82
DE
5492load_partial_comp_unit_reader (const struct die_reader_specs *reader,
5493 gdb_byte *info_ptr,
5494 struct die_info *comp_unit_die,
5495 int has_children,
5496 void *data)
ae038cb0 5497{
dee91e82 5498 struct dwarf2_cu *cu = reader->cu;
ae038cb0 5499
95554aad 5500 prepare_one_comp_unit (cu, comp_unit_die, language_minimal);
ae038cb0 5501
ae038cb0
DJ
5502 /* Check if comp unit has_children.
5503 If so, read the rest of the partial symbols from this comp unit.
0963b4bd 5504 If not, there's no more debug_info for this comp unit. */
d85a05f0 5505 if (has_children)
dee91e82
DE
5506 load_partial_dies (reader, info_ptr, 0);
5507}
98bfdba5 5508
dee91e82
DE
5509/* Load the partial DIEs for a secondary CU into memory.
5510 This is also used when rereading a primary CU with load_all_dies. */
c5b7e1cb 5511
dee91e82
DE
5512static void
5513load_partial_comp_unit (struct dwarf2_per_cu_data *this_cu)
5514{
f4dc4d17
DE
5515 init_cutu_and_read_dies (this_cu, NULL, 1, 1,
5516 load_partial_comp_unit_reader, NULL);
ae038cb0
DJ
5517}
5518
ae038cb0 5519static void
36586728
TT
5520read_comp_units_from_section (struct objfile *objfile,
5521 struct dwarf2_section_info *section,
5522 unsigned int is_dwz,
5523 int *n_allocated,
5524 int *n_comp_units,
5525 struct dwarf2_per_cu_data ***all_comp_units)
ae038cb0 5526{
be391dca 5527 gdb_byte *info_ptr;
36586728 5528 bfd *abfd = section->asection->owner;
be391dca 5529
36586728 5530 dwarf2_read_section (objfile, section);
ae038cb0 5531
36586728 5532 info_ptr = section->buffer;
6e70227d 5533
36586728 5534 while (info_ptr < section->buffer + section->size)
ae038cb0 5535 {
c764a876 5536 unsigned int length, initial_length_size;
ae038cb0 5537 struct dwarf2_per_cu_data *this_cu;
b64f50a1 5538 sect_offset offset;
ae038cb0 5539
36586728 5540 offset.sect_off = info_ptr - section->buffer;
ae038cb0
DJ
5541
5542 /* Read just enough information to find out where the next
5543 compilation unit is. */
36586728 5544 length = read_initial_length (abfd, info_ptr, &initial_length_size);
ae038cb0
DJ
5545
5546 /* Save the compilation unit for later lookup. */
5547 this_cu = obstack_alloc (&objfile->objfile_obstack,
5548 sizeof (struct dwarf2_per_cu_data));
5549 memset (this_cu, 0, sizeof (*this_cu));
5550 this_cu->offset = offset;
c764a876 5551 this_cu->length = length + initial_length_size;
36586728 5552 this_cu->is_dwz = is_dwz;
9291a0cd 5553 this_cu->objfile = objfile;
36586728 5554 this_cu->info_or_types_section = section;
ae038cb0 5555
36586728 5556 if (*n_comp_units == *n_allocated)
ae038cb0 5557 {
36586728
TT
5558 *n_allocated *= 2;
5559 *all_comp_units = xrealloc (*all_comp_units,
5560 *n_allocated
5561 * sizeof (struct dwarf2_per_cu_data *));
ae038cb0 5562 }
36586728
TT
5563 (*all_comp_units)[*n_comp_units] = this_cu;
5564 ++*n_comp_units;
ae038cb0
DJ
5565
5566 info_ptr = info_ptr + this_cu->length;
5567 }
36586728
TT
5568}
5569
5570/* Create a list of all compilation units in OBJFILE.
5571 This is only done for -readnow and building partial symtabs. */
5572
5573static void
5574create_all_comp_units (struct objfile *objfile)
5575{
5576 int n_allocated;
5577 int n_comp_units;
5578 struct dwarf2_per_cu_data **all_comp_units;
5579
5580 n_comp_units = 0;
5581 n_allocated = 10;
5582 all_comp_units = xmalloc (n_allocated
5583 * sizeof (struct dwarf2_per_cu_data *));
5584
5585 read_comp_units_from_section (objfile, &dwarf2_per_objfile->info, 0,
5586 &n_allocated, &n_comp_units, &all_comp_units);
5587
5588 if (bfd_get_section_by_name (objfile->obfd, ".gnu_debugaltlink") != NULL)
5589 {
5590 struct dwz_file *dwz = dwarf2_get_dwz_file ();
5591
5592 read_comp_units_from_section (objfile, &dwz->info, 1,
5593 &n_allocated, &n_comp_units,
5594 &all_comp_units);
5595 }
ae038cb0
DJ
5596
5597 dwarf2_per_objfile->all_comp_units
5598 = obstack_alloc (&objfile->objfile_obstack,
5599 n_comp_units * sizeof (struct dwarf2_per_cu_data *));
5600 memcpy (dwarf2_per_objfile->all_comp_units, all_comp_units,
5601 n_comp_units * sizeof (struct dwarf2_per_cu_data *));
5602 xfree (all_comp_units);
5603 dwarf2_per_objfile->n_comp_units = n_comp_units;
c906108c
SS
5604}
5605
5734ee8b
DJ
5606/* Process all loaded DIEs for compilation unit CU, starting at
5607 FIRST_DIE. The caller should pass NEED_PC == 1 if the compilation
5608 unit DIE did not have PC info (DW_AT_low_pc and DW_AT_high_pc, or
5609 DW_AT_ranges). If NEED_PC is set, then this function will set
5610 *LOWPC and *HIGHPC to the lowest and highest PC values found in CU
5611 and record the covered ranges in the addrmap. */
c906108c 5612
72bf9492
DJ
5613static void
5614scan_partial_symbols (struct partial_die_info *first_die, CORE_ADDR *lowpc,
5734ee8b 5615 CORE_ADDR *highpc, int need_pc, struct dwarf2_cu *cu)
c906108c 5616{
72bf9492 5617 struct partial_die_info *pdi;
c906108c 5618
91c24f0a
DC
5619 /* Now, march along the PDI's, descending into ones which have
5620 interesting children but skipping the children of the other ones,
5621 until we reach the end of the compilation unit. */
c906108c 5622
72bf9492 5623 pdi = first_die;
91c24f0a 5624
72bf9492
DJ
5625 while (pdi != NULL)
5626 {
5627 fixup_partial_die (pdi, cu);
c906108c 5628
f55ee35c 5629 /* Anonymous namespaces or modules have no name but have interesting
91c24f0a
DC
5630 children, so we need to look at them. Ditto for anonymous
5631 enums. */
933c6fe4 5632
72bf9492 5633 if (pdi->name != NULL || pdi->tag == DW_TAG_namespace
95554aad
TT
5634 || pdi->tag == DW_TAG_module || pdi->tag == DW_TAG_enumeration_type
5635 || pdi->tag == DW_TAG_imported_unit)
c906108c 5636 {
72bf9492 5637 switch (pdi->tag)
c906108c
SS
5638 {
5639 case DW_TAG_subprogram:
5734ee8b 5640 add_partial_subprogram (pdi, lowpc, highpc, need_pc, cu);
c906108c 5641 break;
72929c62 5642 case DW_TAG_constant:
c906108c
SS
5643 case DW_TAG_variable:
5644 case DW_TAG_typedef:
91c24f0a 5645 case DW_TAG_union_type:
72bf9492 5646 if (!pdi->is_declaration)
63d06c5c 5647 {
72bf9492 5648 add_partial_symbol (pdi, cu);
63d06c5c
DC
5649 }
5650 break;
c906108c 5651 case DW_TAG_class_type:
680b30c7 5652 case DW_TAG_interface_type:
c906108c 5653 case DW_TAG_structure_type:
72bf9492 5654 if (!pdi->is_declaration)
c906108c 5655 {
72bf9492 5656 add_partial_symbol (pdi, cu);
c906108c
SS
5657 }
5658 break;
91c24f0a 5659 case DW_TAG_enumeration_type:
72bf9492
DJ
5660 if (!pdi->is_declaration)
5661 add_partial_enumeration (pdi, cu);
c906108c
SS
5662 break;
5663 case DW_TAG_base_type:
a02abb62 5664 case DW_TAG_subrange_type:
c906108c 5665 /* File scope base type definitions are added to the partial
c5aa993b 5666 symbol table. */
72bf9492 5667 add_partial_symbol (pdi, cu);
c906108c 5668 break;
d9fa45fe 5669 case DW_TAG_namespace:
5734ee8b 5670 add_partial_namespace (pdi, lowpc, highpc, need_pc, cu);
91c24f0a 5671 break;
5d7cb8df
JK
5672 case DW_TAG_module:
5673 add_partial_module (pdi, lowpc, highpc, need_pc, cu);
5674 break;
95554aad
TT
5675 case DW_TAG_imported_unit:
5676 {
5677 struct dwarf2_per_cu_data *per_cu;
5678
f4dc4d17
DE
5679 /* For now we don't handle imported units in type units. */
5680 if (cu->per_cu->is_debug_types)
5681 {
5682 error (_("Dwarf Error: DW_TAG_imported_unit is not"
5683 " supported in type units [in module %s]"),
5684 cu->objfile->name);
5685 }
5686
95554aad 5687 per_cu = dwarf2_find_containing_comp_unit (pdi->d.offset,
36586728 5688 pdi->is_dwz,
95554aad
TT
5689 cu->objfile);
5690
5691 /* Go read the partial unit, if needed. */
5692 if (per_cu->v.psymtab == NULL)
5693 process_psymtab_comp_unit (per_cu, 1);
5694
f4dc4d17
DE
5695 VEC_safe_push (dwarf2_per_cu_ptr,
5696 cu->per_cu->s.imported_symtabs, per_cu);
95554aad
TT
5697 }
5698 break;
c906108c
SS
5699 default:
5700 break;
5701 }
5702 }
5703
72bf9492
DJ
5704 /* If the die has a sibling, skip to the sibling. */
5705
5706 pdi = pdi->die_sibling;
5707 }
5708}
5709
5710/* Functions used to compute the fully scoped name of a partial DIE.
91c24f0a 5711
72bf9492 5712 Normally, this is simple. For C++, the parent DIE's fully scoped
987504bb
JJ
5713 name is concatenated with "::" and the partial DIE's name. For
5714 Java, the same thing occurs except that "." is used instead of "::".
72bf9492
DJ
5715 Enumerators are an exception; they use the scope of their parent
5716 enumeration type, i.e. the name of the enumeration type is not
5717 prepended to the enumerator.
91c24f0a 5718
72bf9492
DJ
5719 There are two complexities. One is DW_AT_specification; in this
5720 case "parent" means the parent of the target of the specification,
5721 instead of the direct parent of the DIE. The other is compilers
5722 which do not emit DW_TAG_namespace; in this case we try to guess
5723 the fully qualified name of structure types from their members'
5724 linkage names. This must be done using the DIE's children rather
5725 than the children of any DW_AT_specification target. We only need
5726 to do this for structures at the top level, i.e. if the target of
5727 any DW_AT_specification (if any; otherwise the DIE itself) does not
5728 have a parent. */
5729
5730/* Compute the scope prefix associated with PDI's parent, in
5731 compilation unit CU. The result will be allocated on CU's
5732 comp_unit_obstack, or a copy of the already allocated PDI->NAME
5733 field. NULL is returned if no prefix is necessary. */
5734static char *
5735partial_die_parent_scope (struct partial_die_info *pdi,
5736 struct dwarf2_cu *cu)
5737{
5738 char *grandparent_scope;
5739 struct partial_die_info *parent, *real_pdi;
91c24f0a 5740
72bf9492
DJ
5741 /* We need to look at our parent DIE; if we have a DW_AT_specification,
5742 then this means the parent of the specification DIE. */
5743
5744 real_pdi = pdi;
72bf9492 5745 while (real_pdi->has_specification)
36586728
TT
5746 real_pdi = find_partial_die (real_pdi->spec_offset,
5747 real_pdi->spec_is_dwz, cu);
72bf9492
DJ
5748
5749 parent = real_pdi->die_parent;
5750 if (parent == NULL)
5751 return NULL;
5752
5753 if (parent->scope_set)
5754 return parent->scope;
5755
5756 fixup_partial_die (parent, cu);
5757
10b3939b 5758 grandparent_scope = partial_die_parent_scope (parent, cu);
72bf9492 5759
acebe513
UW
5760 /* GCC 4.0 and 4.1 had a bug (PR c++/28460) where they generated bogus
5761 DW_TAG_namespace DIEs with a name of "::" for the global namespace.
5762 Work around this problem here. */
5763 if (cu->language == language_cplus
6e70227d 5764 && parent->tag == DW_TAG_namespace
acebe513
UW
5765 && strcmp (parent->name, "::") == 0
5766 && grandparent_scope == NULL)
5767 {
5768 parent->scope = NULL;
5769 parent->scope_set = 1;
5770 return NULL;
5771 }
5772
9c6c53f7
SA
5773 if (pdi->tag == DW_TAG_enumerator)
5774 /* Enumerators should not get the name of the enumeration as a prefix. */
5775 parent->scope = grandparent_scope;
5776 else if (parent->tag == DW_TAG_namespace
f55ee35c 5777 || parent->tag == DW_TAG_module
72bf9492
DJ
5778 || parent->tag == DW_TAG_structure_type
5779 || parent->tag == DW_TAG_class_type
680b30c7 5780 || parent->tag == DW_TAG_interface_type
ceeb3d5a
TT
5781 || parent->tag == DW_TAG_union_type
5782 || parent->tag == DW_TAG_enumeration_type)
72bf9492
DJ
5783 {
5784 if (grandparent_scope == NULL)
5785 parent->scope = parent->name;
5786 else
3e43a32a
MS
5787 parent->scope = typename_concat (&cu->comp_unit_obstack,
5788 grandparent_scope,
f55ee35c 5789 parent->name, 0, cu);
72bf9492 5790 }
72bf9492
DJ
5791 else
5792 {
5793 /* FIXME drow/2004-04-01: What should we be doing with
5794 function-local names? For partial symbols, we should probably be
5795 ignoring them. */
5796 complaint (&symfile_complaints,
e2e0b3e5 5797 _("unhandled containing DIE tag %d for DIE at %d"),
b64f50a1 5798 parent->tag, pdi->offset.sect_off);
72bf9492 5799 parent->scope = grandparent_scope;
c906108c
SS
5800 }
5801
72bf9492
DJ
5802 parent->scope_set = 1;
5803 return parent->scope;
5804}
5805
5806/* Return the fully scoped name associated with PDI, from compilation unit
5807 CU. The result will be allocated with malloc. */
4568ecf9 5808
72bf9492
DJ
5809static char *
5810partial_die_full_name (struct partial_die_info *pdi,
5811 struct dwarf2_cu *cu)
5812{
5813 char *parent_scope;
5814
98bfdba5
PA
5815 /* If this is a template instantiation, we can not work out the
5816 template arguments from partial DIEs. So, unfortunately, we have
5817 to go through the full DIEs. At least any work we do building
5818 types here will be reused if full symbols are loaded later. */
5819 if (pdi->has_template_arguments)
5820 {
5821 fixup_partial_die (pdi, cu);
5822
5823 if (pdi->name != NULL && strchr (pdi->name, '<') == NULL)
5824 {
5825 struct die_info *die;
5826 struct attribute attr;
5827 struct dwarf2_cu *ref_cu = cu;
5828
b64f50a1 5829 /* DW_FORM_ref_addr is using section offset. */
98bfdba5
PA
5830 attr.name = 0;
5831 attr.form = DW_FORM_ref_addr;
4568ecf9 5832 attr.u.unsnd = pdi->offset.sect_off;
98bfdba5
PA
5833 die = follow_die_ref (NULL, &attr, &ref_cu);
5834
5835 return xstrdup (dwarf2_full_name (NULL, die, ref_cu));
5836 }
5837 }
5838
72bf9492
DJ
5839 parent_scope = partial_die_parent_scope (pdi, cu);
5840 if (parent_scope == NULL)
5841 return NULL;
5842 else
f55ee35c 5843 return typename_concat (NULL, parent_scope, pdi->name, 0, cu);
c906108c
SS
5844}
5845
5846static void
72bf9492 5847add_partial_symbol (struct partial_die_info *pdi, struct dwarf2_cu *cu)
c906108c 5848{
e7c27a73 5849 struct objfile *objfile = cu->objfile;
c906108c 5850 CORE_ADDR addr = 0;
decbce07 5851 char *actual_name = NULL;
e142c38c 5852 CORE_ADDR baseaddr;
72bf9492 5853 int built_actual_name = 0;
e142c38c
DJ
5854
5855 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
c906108c 5856
94af9270
KS
5857 actual_name = partial_die_full_name (pdi, cu);
5858 if (actual_name)
5859 built_actual_name = 1;
63d06c5c 5860
72bf9492
DJ
5861 if (actual_name == NULL)
5862 actual_name = pdi->name;
5863
c906108c
SS
5864 switch (pdi->tag)
5865 {
5866 case DW_TAG_subprogram:
2cfa0c8d 5867 if (pdi->is_external || cu->language == language_ada)
c906108c 5868 {
2cfa0c8d
JB
5869 /* brobecker/2007-12-26: Normally, only "external" DIEs are part
5870 of the global scope. But in Ada, we want to be able to access
5871 nested procedures globally. So all Ada subprograms are stored
5872 in the global scope. */
f47fb265 5873 /* prim_record_minimal_symbol (actual_name, pdi->lowpc + baseaddr,
c5aa993b 5874 mst_text, objfile); */
f47fb265
MS
5875 add_psymbol_to_list (actual_name, strlen (actual_name),
5876 built_actual_name,
5877 VAR_DOMAIN, LOC_BLOCK,
5878 &objfile->global_psymbols,
5879 0, pdi->lowpc + baseaddr,
5880 cu->language, objfile);
c906108c
SS
5881 }
5882 else
5883 {
f47fb265 5884 /* prim_record_minimal_symbol (actual_name, pdi->lowpc + baseaddr,
c5aa993b 5885 mst_file_text, objfile); */
f47fb265
MS
5886 add_psymbol_to_list (actual_name, strlen (actual_name),
5887 built_actual_name,
5888 VAR_DOMAIN, LOC_BLOCK,
5889 &objfile->static_psymbols,
5890 0, pdi->lowpc + baseaddr,
5891 cu->language, objfile);
c906108c
SS
5892 }
5893 break;
72929c62
JB
5894 case DW_TAG_constant:
5895 {
5896 struct psymbol_allocation_list *list;
5897
5898 if (pdi->is_external)
5899 list = &objfile->global_psymbols;
5900 else
5901 list = &objfile->static_psymbols;
f47fb265
MS
5902 add_psymbol_to_list (actual_name, strlen (actual_name),
5903 built_actual_name, VAR_DOMAIN, LOC_STATIC,
5904 list, 0, 0, cu->language, objfile);
72929c62
JB
5905 }
5906 break;
c906108c 5907 case DW_TAG_variable:
95554aad
TT
5908 if (pdi->d.locdesc)
5909 addr = decode_locdesc (pdi->d.locdesc, cu);
caac4577 5910
95554aad 5911 if (pdi->d.locdesc
caac4577
JG
5912 && addr == 0
5913 && !dwarf2_per_objfile->has_section_at_zero)
5914 {
5915 /* A global or static variable may also have been stripped
5916 out by the linker if unused, in which case its address
5917 will be nullified; do not add such variables into partial
5918 symbol table then. */
5919 }
5920 else if (pdi->is_external)
c906108c
SS
5921 {
5922 /* Global Variable.
5923 Don't enter into the minimal symbol tables as there is
5924 a minimal symbol table entry from the ELF symbols already.
5925 Enter into partial symbol table if it has a location
5926 descriptor or a type.
5927 If the location descriptor is missing, new_symbol will create
5928 a LOC_UNRESOLVED symbol, the address of the variable will then
5929 be determined from the minimal symbol table whenever the variable
5930 is referenced.
5931 The address for the partial symbol table entry is not
5932 used by GDB, but it comes in handy for debugging partial symbol
5933 table building. */
5934
95554aad 5935 if (pdi->d.locdesc || pdi->has_type)
f47fb265
MS
5936 add_psymbol_to_list (actual_name, strlen (actual_name),
5937 built_actual_name,
5938 VAR_DOMAIN, LOC_STATIC,
5939 &objfile->global_psymbols,
5940 0, addr + baseaddr,
5941 cu->language, objfile);
c906108c
SS
5942 }
5943 else
5944 {
0963b4bd 5945 /* Static Variable. Skip symbols without location descriptors. */
95554aad 5946 if (pdi->d.locdesc == NULL)
decbce07
MS
5947 {
5948 if (built_actual_name)
5949 xfree (actual_name);
5950 return;
5951 }
f47fb265 5952 /* prim_record_minimal_symbol (actual_name, addr + baseaddr,
c5aa993b 5953 mst_file_data, objfile); */
f47fb265
MS
5954 add_psymbol_to_list (actual_name, strlen (actual_name),
5955 built_actual_name,
5956 VAR_DOMAIN, LOC_STATIC,
5957 &objfile->static_psymbols,
5958 0, addr + baseaddr,
5959 cu->language, objfile);
c906108c
SS
5960 }
5961 break;
5962 case DW_TAG_typedef:
5963 case DW_TAG_base_type:
a02abb62 5964 case DW_TAG_subrange_type:
38d518c9 5965 add_psymbol_to_list (actual_name, strlen (actual_name),
04a679b8 5966 built_actual_name,
176620f1 5967 VAR_DOMAIN, LOC_TYPEDEF,
c906108c 5968 &objfile->static_psymbols,
e142c38c 5969 0, (CORE_ADDR) 0, cu->language, objfile);
c906108c 5970 break;
72bf9492
DJ
5971 case DW_TAG_namespace:
5972 add_psymbol_to_list (actual_name, strlen (actual_name),
04a679b8 5973 built_actual_name,
72bf9492
DJ
5974 VAR_DOMAIN, LOC_TYPEDEF,
5975 &objfile->global_psymbols,
5976 0, (CORE_ADDR) 0, cu->language, objfile);
5977 break;
c906108c 5978 case DW_TAG_class_type:
680b30c7 5979 case DW_TAG_interface_type:
c906108c
SS
5980 case DW_TAG_structure_type:
5981 case DW_TAG_union_type:
5982 case DW_TAG_enumeration_type:
fa4028e9
JB
5983 /* Skip external references. The DWARF standard says in the section
5984 about "Structure, Union, and Class Type Entries": "An incomplete
5985 structure, union or class type is represented by a structure,
5986 union or class entry that does not have a byte size attribute
5987 and that has a DW_AT_declaration attribute." */
5988 if (!pdi->has_byte_size && pdi->is_declaration)
decbce07
MS
5989 {
5990 if (built_actual_name)
5991 xfree (actual_name);
5992 return;
5993 }
fa4028e9 5994
63d06c5c
DC
5995 /* NOTE: carlton/2003-10-07: See comment in new_symbol about
5996 static vs. global. */
38d518c9 5997 add_psymbol_to_list (actual_name, strlen (actual_name),
04a679b8 5998 built_actual_name,
176620f1 5999 STRUCT_DOMAIN, LOC_TYPEDEF,
987504bb
JJ
6000 (cu->language == language_cplus
6001 || cu->language == language_java)
63d06c5c
DC
6002 ? &objfile->global_psymbols
6003 : &objfile->static_psymbols,
e142c38c 6004 0, (CORE_ADDR) 0, cu->language, objfile);
c906108c 6005
c906108c
SS
6006 break;
6007 case DW_TAG_enumerator:
38d518c9 6008 add_psymbol_to_list (actual_name, strlen (actual_name),
04a679b8 6009 built_actual_name,
176620f1 6010 VAR_DOMAIN, LOC_CONST,
987504bb
JJ
6011 (cu->language == language_cplus
6012 || cu->language == language_java)
f6fe98ef
DJ
6013 ? &objfile->global_psymbols
6014 : &objfile->static_psymbols,
e142c38c 6015 0, (CORE_ADDR) 0, cu->language, objfile);
c906108c
SS
6016 break;
6017 default:
6018 break;
6019 }
5c4e30ca 6020
72bf9492
DJ
6021 if (built_actual_name)
6022 xfree (actual_name);
c906108c
SS
6023}
6024
5c4e30ca
DC
6025/* Read a partial die corresponding to a namespace; also, add a symbol
6026 corresponding to that namespace to the symbol table. NAMESPACE is
6027 the name of the enclosing namespace. */
91c24f0a 6028
72bf9492
DJ
6029static void
6030add_partial_namespace (struct partial_die_info *pdi,
91c24f0a 6031 CORE_ADDR *lowpc, CORE_ADDR *highpc,
5734ee8b 6032 int need_pc, struct dwarf2_cu *cu)
91c24f0a 6033{
72bf9492 6034 /* Add a symbol for the namespace. */
e7c27a73 6035
72bf9492 6036 add_partial_symbol (pdi, cu);
5c4e30ca
DC
6037
6038 /* Now scan partial symbols in that namespace. */
6039
91c24f0a 6040 if (pdi->has_children)
5734ee8b 6041 scan_partial_symbols (pdi->die_child, lowpc, highpc, need_pc, cu);
91c24f0a
DC
6042}
6043
5d7cb8df
JK
6044/* Read a partial die corresponding to a Fortran module. */
6045
6046static void
6047add_partial_module (struct partial_die_info *pdi, CORE_ADDR *lowpc,
6048 CORE_ADDR *highpc, int need_pc, struct dwarf2_cu *cu)
6049{
f55ee35c 6050 /* Now scan partial symbols in that module. */
5d7cb8df
JK
6051
6052 if (pdi->has_children)
6053 scan_partial_symbols (pdi->die_child, lowpc, highpc, need_pc, cu);
6054}
6055
bc30ff58
JB
6056/* Read a partial die corresponding to a subprogram and create a partial
6057 symbol for that subprogram. When the CU language allows it, this
6058 routine also defines a partial symbol for each nested subprogram
6059 that this subprogram contains.
6e70227d 6060
bc30ff58
JB
6061 DIE my also be a lexical block, in which case we simply search
6062 recursively for suprograms defined inside that lexical block.
6063 Again, this is only performed when the CU language allows this
6064 type of definitions. */
6065
6066static void
6067add_partial_subprogram (struct partial_die_info *pdi,
6068 CORE_ADDR *lowpc, CORE_ADDR *highpc,
5734ee8b 6069 int need_pc, struct dwarf2_cu *cu)
bc30ff58
JB
6070{
6071 if (pdi->tag == DW_TAG_subprogram)
6072 {
6073 if (pdi->has_pc_info)
6074 {
6075 if (pdi->lowpc < *lowpc)
6076 *lowpc = pdi->lowpc;
6077 if (pdi->highpc > *highpc)
6078 *highpc = pdi->highpc;
5734ee8b
DJ
6079 if (need_pc)
6080 {
6081 CORE_ADDR baseaddr;
6082 struct objfile *objfile = cu->objfile;
6083
6084 baseaddr = ANOFFSET (objfile->section_offsets,
6085 SECT_OFF_TEXT (objfile));
6086 addrmap_set_empty (objfile->psymtabs_addrmap,
01637564
DE
6087 pdi->lowpc + baseaddr,
6088 pdi->highpc - 1 + baseaddr,
9291a0cd 6089 cu->per_cu->v.psymtab);
5734ee8b 6090 }
481860b3
GB
6091 }
6092
6093 if (pdi->has_pc_info || (!pdi->is_external && pdi->may_be_inlined))
6094 {
bc30ff58 6095 if (!pdi->is_declaration)
e8d05480
JB
6096 /* Ignore subprogram DIEs that do not have a name, they are
6097 illegal. Do not emit a complaint at this point, we will
6098 do so when we convert this psymtab into a symtab. */
6099 if (pdi->name)
6100 add_partial_symbol (pdi, cu);
bc30ff58
JB
6101 }
6102 }
6e70227d 6103
bc30ff58
JB
6104 if (! pdi->has_children)
6105 return;
6106
6107 if (cu->language == language_ada)
6108 {
6109 pdi = pdi->die_child;
6110 while (pdi != NULL)
6111 {
6112 fixup_partial_die (pdi, cu);
6113 if (pdi->tag == DW_TAG_subprogram
6114 || pdi->tag == DW_TAG_lexical_block)
5734ee8b 6115 add_partial_subprogram (pdi, lowpc, highpc, need_pc, cu);
bc30ff58
JB
6116 pdi = pdi->die_sibling;
6117 }
6118 }
6119}
6120
91c24f0a
DC
6121/* Read a partial die corresponding to an enumeration type. */
6122
72bf9492
DJ
6123static void
6124add_partial_enumeration (struct partial_die_info *enum_pdi,
6125 struct dwarf2_cu *cu)
91c24f0a 6126{
72bf9492 6127 struct partial_die_info *pdi;
91c24f0a
DC
6128
6129 if (enum_pdi->name != NULL)
72bf9492
DJ
6130 add_partial_symbol (enum_pdi, cu);
6131
6132 pdi = enum_pdi->die_child;
6133 while (pdi)
91c24f0a 6134 {
72bf9492 6135 if (pdi->tag != DW_TAG_enumerator || pdi->name == NULL)
e2e0b3e5 6136 complaint (&symfile_complaints, _("malformed enumerator DIE ignored"));
91c24f0a 6137 else
72bf9492
DJ
6138 add_partial_symbol (pdi, cu);
6139 pdi = pdi->die_sibling;
91c24f0a 6140 }
91c24f0a
DC
6141}
6142
6caca83c
CC
6143/* Return the initial uleb128 in the die at INFO_PTR. */
6144
6145static unsigned int
6146peek_abbrev_code (bfd *abfd, gdb_byte *info_ptr)
6147{
6148 unsigned int bytes_read;
6149
6150 return read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
6151}
6152
4bb7a0a7
DJ
6153/* Read the initial uleb128 in the die at INFO_PTR in compilation unit CU.
6154 Return the corresponding abbrev, or NULL if the number is zero (indicating
6155 an empty DIE). In either case *BYTES_READ will be set to the length of
6156 the initial number. */
6157
6158static struct abbrev_info *
fe1b8b76 6159peek_die_abbrev (gdb_byte *info_ptr, unsigned int *bytes_read,
891d2f0b 6160 struct dwarf2_cu *cu)
4bb7a0a7
DJ
6161{
6162 bfd *abfd = cu->objfile->obfd;
6163 unsigned int abbrev_number;
6164 struct abbrev_info *abbrev;
6165
6166 abbrev_number = read_unsigned_leb128 (abfd, info_ptr, bytes_read);
6167
6168 if (abbrev_number == 0)
6169 return NULL;
6170
433df2d4 6171 abbrev = abbrev_table_lookup_abbrev (cu->abbrev_table, abbrev_number);
4bb7a0a7
DJ
6172 if (!abbrev)
6173 {
3e43a32a
MS
6174 error (_("Dwarf Error: Could not find abbrev number %d [in module %s]"),
6175 abbrev_number, bfd_get_filename (abfd));
4bb7a0a7
DJ
6176 }
6177
6178 return abbrev;
6179}
6180
93311388
DE
6181/* Scan the debug information for CU starting at INFO_PTR in buffer BUFFER.
6182 Returns a pointer to the end of a series of DIEs, terminated by an empty
4bb7a0a7
DJ
6183 DIE. Any children of the skipped DIEs will also be skipped. */
6184
fe1b8b76 6185static gdb_byte *
dee91e82 6186skip_children (const struct die_reader_specs *reader, gdb_byte *info_ptr)
4bb7a0a7 6187{
dee91e82 6188 struct dwarf2_cu *cu = reader->cu;
4bb7a0a7
DJ
6189 struct abbrev_info *abbrev;
6190 unsigned int bytes_read;
6191
6192 while (1)
6193 {
6194 abbrev = peek_die_abbrev (info_ptr, &bytes_read, cu);
6195 if (abbrev == NULL)
6196 return info_ptr + bytes_read;
6197 else
dee91e82 6198 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
4bb7a0a7
DJ
6199 }
6200}
6201
93311388
DE
6202/* Scan the debug information for CU starting at INFO_PTR in buffer BUFFER.
6203 INFO_PTR should point just after the initial uleb128 of a DIE, and the
4bb7a0a7
DJ
6204 abbrev corresponding to that skipped uleb128 should be passed in
6205 ABBREV. Returns a pointer to this DIE's sibling, skipping any
6206 children. */
6207
fe1b8b76 6208static gdb_byte *
dee91e82
DE
6209skip_one_die (const struct die_reader_specs *reader, gdb_byte *info_ptr,
6210 struct abbrev_info *abbrev)
4bb7a0a7
DJ
6211{
6212 unsigned int bytes_read;
6213 struct attribute attr;
dee91e82
DE
6214 bfd *abfd = reader->abfd;
6215 struct dwarf2_cu *cu = reader->cu;
6216 gdb_byte *buffer = reader->buffer;
f664829e
DE
6217 const gdb_byte *buffer_end = reader->buffer_end;
6218 gdb_byte *start_info_ptr = info_ptr;
4bb7a0a7
DJ
6219 unsigned int form, i;
6220
6221 for (i = 0; i < abbrev->num_attrs; i++)
6222 {
6223 /* The only abbrev we care about is DW_AT_sibling. */
6224 if (abbrev->attrs[i].name == DW_AT_sibling)
6225 {
dee91e82 6226 read_attribute (reader, &attr, &abbrev->attrs[i], info_ptr);
4bb7a0a7 6227 if (attr.form == DW_FORM_ref_addr)
3e43a32a
MS
6228 complaint (&symfile_complaints,
6229 _("ignoring absolute DW_AT_sibling"));
4bb7a0a7 6230 else
b64f50a1 6231 return buffer + dwarf2_get_ref_die_offset (&attr).sect_off;
4bb7a0a7
DJ
6232 }
6233
6234 /* If it isn't DW_AT_sibling, skip this attribute. */
6235 form = abbrev->attrs[i].form;
6236 skip_attribute:
6237 switch (form)
6238 {
4bb7a0a7 6239 case DW_FORM_ref_addr:
ae411497
TT
6240 /* In DWARF 2, DW_FORM_ref_addr is address sized; in DWARF 3
6241 and later it is offset sized. */
6242 if (cu->header.version == 2)
6243 info_ptr += cu->header.addr_size;
6244 else
6245 info_ptr += cu->header.offset_size;
6246 break;
36586728
TT
6247 case DW_FORM_GNU_ref_alt:
6248 info_ptr += cu->header.offset_size;
6249 break;
ae411497 6250 case DW_FORM_addr:
4bb7a0a7
DJ
6251 info_ptr += cu->header.addr_size;
6252 break;
6253 case DW_FORM_data1:
6254 case DW_FORM_ref1:
6255 case DW_FORM_flag:
6256 info_ptr += 1;
6257 break;
2dc7f7b3
TT
6258 case DW_FORM_flag_present:
6259 break;
4bb7a0a7
DJ
6260 case DW_FORM_data2:
6261 case DW_FORM_ref2:
6262 info_ptr += 2;
6263 break;
6264 case DW_FORM_data4:
6265 case DW_FORM_ref4:
6266 info_ptr += 4;
6267 break;
6268 case DW_FORM_data8:
6269 case DW_FORM_ref8:
55f1336d 6270 case DW_FORM_ref_sig8:
4bb7a0a7
DJ
6271 info_ptr += 8;
6272 break;
6273 case DW_FORM_string:
9b1c24c8 6274 read_direct_string (abfd, info_ptr, &bytes_read);
4bb7a0a7
DJ
6275 info_ptr += bytes_read;
6276 break;
2dc7f7b3 6277 case DW_FORM_sec_offset:
4bb7a0a7 6278 case DW_FORM_strp:
36586728 6279 case DW_FORM_GNU_strp_alt:
4bb7a0a7
DJ
6280 info_ptr += cu->header.offset_size;
6281 break;
2dc7f7b3 6282 case DW_FORM_exprloc:
4bb7a0a7
DJ
6283 case DW_FORM_block:
6284 info_ptr += read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
6285 info_ptr += bytes_read;
6286 break;
6287 case DW_FORM_block1:
6288 info_ptr += 1 + read_1_byte (abfd, info_ptr);
6289 break;
6290 case DW_FORM_block2:
6291 info_ptr += 2 + read_2_bytes (abfd, info_ptr);
6292 break;
6293 case DW_FORM_block4:
6294 info_ptr += 4 + read_4_bytes (abfd, info_ptr);
6295 break;
6296 case DW_FORM_sdata:
6297 case DW_FORM_udata:
6298 case DW_FORM_ref_udata:
3019eac3
DE
6299 case DW_FORM_GNU_addr_index:
6300 case DW_FORM_GNU_str_index:
f664829e 6301 info_ptr = (gdb_byte *) safe_skip_leb128 (info_ptr, buffer_end);
4bb7a0a7
DJ
6302 break;
6303 case DW_FORM_indirect:
6304 form = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
6305 info_ptr += bytes_read;
6306 /* We need to continue parsing from here, so just go back to
6307 the top. */
6308 goto skip_attribute;
6309
6310 default:
3e43a32a
MS
6311 error (_("Dwarf Error: Cannot handle %s "
6312 "in DWARF reader [in module %s]"),
4bb7a0a7
DJ
6313 dwarf_form_name (form),
6314 bfd_get_filename (abfd));
6315 }
6316 }
6317
6318 if (abbrev->has_children)
dee91e82 6319 return skip_children (reader, info_ptr);
4bb7a0a7
DJ
6320 else
6321 return info_ptr;
6322}
6323
93311388 6324/* Locate ORIG_PDI's sibling.
dee91e82 6325 INFO_PTR should point to the start of the next DIE after ORIG_PDI. */
91c24f0a 6326
fe1b8b76 6327static gdb_byte *
dee91e82
DE
6328locate_pdi_sibling (const struct die_reader_specs *reader,
6329 struct partial_die_info *orig_pdi,
6330 gdb_byte *info_ptr)
91c24f0a
DC
6331{
6332 /* Do we know the sibling already? */
72bf9492 6333
91c24f0a
DC
6334 if (orig_pdi->sibling)
6335 return orig_pdi->sibling;
6336
6337 /* Are there any children to deal with? */
6338
6339 if (!orig_pdi->has_children)
6340 return info_ptr;
6341
4bb7a0a7 6342 /* Skip the children the long way. */
91c24f0a 6343
dee91e82 6344 return skip_children (reader, info_ptr);
91c24f0a
DC
6345}
6346
c906108c
SS
6347/* Expand this partial symbol table into a full symbol table. */
6348
6349static void
fba45db2 6350dwarf2_psymtab_to_symtab (struct partial_symtab *pst)
c906108c 6351{
c906108c
SS
6352 if (pst != NULL)
6353 {
6354 if (pst->readin)
6355 {
3e43a32a
MS
6356 warning (_("bug: psymtab for %s is already read in."),
6357 pst->filename);
c906108c
SS
6358 }
6359 else
6360 {
6361 if (info_verbose)
6362 {
3e43a32a
MS
6363 printf_filtered (_("Reading in symbols for %s..."),
6364 pst->filename);
c906108c
SS
6365 gdb_flush (gdb_stdout);
6366 }
6367
10b3939b
DJ
6368 /* Restore our global data. */
6369 dwarf2_per_objfile = objfile_data (pst->objfile,
6370 dwarf2_objfile_data_key);
6371
b2ab525c
KB
6372 /* If this psymtab is constructed from a debug-only objfile, the
6373 has_section_at_zero flag will not necessarily be correct. We
6374 can get the correct value for this flag by looking at the data
6375 associated with the (presumably stripped) associated objfile. */
6376 if (pst->objfile->separate_debug_objfile_backlink)
6377 {
6378 struct dwarf2_per_objfile *dpo_backlink
6379 = objfile_data (pst->objfile->separate_debug_objfile_backlink,
6380 dwarf2_objfile_data_key);
9a619af0 6381
b2ab525c
KB
6382 dwarf2_per_objfile->has_section_at_zero
6383 = dpo_backlink->has_section_at_zero;
6384 }
6385
98bfdba5
PA
6386 dwarf2_per_objfile->reading_partial_symbols = 0;
6387
c906108c
SS
6388 psymtab_to_symtab_1 (pst);
6389
6390 /* Finish up the debug error message. */
6391 if (info_verbose)
a3f17187 6392 printf_filtered (_("done.\n"));
c906108c
SS
6393 }
6394 }
95554aad
TT
6395
6396 process_cu_includes ();
c906108c 6397}
9cdd5dbd
DE
6398\f
6399/* Reading in full CUs. */
c906108c 6400
10b3939b
DJ
6401/* Add PER_CU to the queue. */
6402
6403static void
95554aad
TT
6404queue_comp_unit (struct dwarf2_per_cu_data *per_cu,
6405 enum language pretend_language)
10b3939b
DJ
6406{
6407 struct dwarf2_queue_item *item;
6408
6409 per_cu->queued = 1;
6410 item = xmalloc (sizeof (*item));
6411 item->per_cu = per_cu;
95554aad 6412 item->pretend_language = pretend_language;
10b3939b
DJ
6413 item->next = NULL;
6414
6415 if (dwarf2_queue == NULL)
6416 dwarf2_queue = item;
6417 else
6418 dwarf2_queue_tail->next = item;
6419
6420 dwarf2_queue_tail = item;
6421}
6422
0907af0c
DE
6423/* THIS_CU has a reference to PER_CU. If necessary, load the new compilation
6424 unit and add it to our queue.
6425 The result is non-zero if PER_CU was queued, otherwise the result is zero
6426 meaning either PER_CU is already queued or it is already loaded. */
6427
6428static int
6429maybe_queue_comp_unit (struct dwarf2_cu *this_cu,
6430 struct dwarf2_per_cu_data *per_cu,
6431 enum language pretend_language)
6432{
6433 /* We may arrive here during partial symbol reading, if we need full
6434 DIEs to process an unusual case (e.g. template arguments). Do
6435 not queue PER_CU, just tell our caller to load its DIEs. */
6436 if (dwarf2_per_objfile->reading_partial_symbols)
6437 {
6438 if (per_cu->cu == NULL || per_cu->cu->dies == NULL)
6439 return 1;
6440 return 0;
6441 }
6442
6443 /* Mark the dependence relation so that we don't flush PER_CU
6444 too early. */
6445 dwarf2_add_dependence (this_cu, per_cu);
6446
6447 /* If it's already on the queue, we have nothing to do. */
6448 if (per_cu->queued)
6449 return 0;
6450
6451 /* If the compilation unit is already loaded, just mark it as
6452 used. */
6453 if (per_cu->cu != NULL)
6454 {
6455 per_cu->cu->last_used = 0;
6456 return 0;
6457 }
6458
6459 /* Add it to the queue. */
6460 queue_comp_unit (per_cu, pretend_language);
6461
6462 return 1;
6463}
6464
10b3939b
DJ
6465/* Process the queue. */
6466
6467static void
a0f42c21 6468process_queue (void)
10b3939b
DJ
6469{
6470 struct dwarf2_queue_item *item, *next_item;
6471
45cfd468
DE
6472 if (dwarf2_read_debug)
6473 {
6474 fprintf_unfiltered (gdb_stdlog,
6475 "Expanding one or more symtabs of objfile %s ...\n",
6476 dwarf2_per_objfile->objfile->name);
6477 }
6478
03dd20cc
DJ
6479 /* The queue starts out with one item, but following a DIE reference
6480 may load a new CU, adding it to the end of the queue. */
10b3939b
DJ
6481 for (item = dwarf2_queue; item != NULL; dwarf2_queue = item = next_item)
6482 {
9291a0cd
TT
6483 if (dwarf2_per_objfile->using_index
6484 ? !item->per_cu->v.quick->symtab
6485 : (item->per_cu->v.psymtab && !item->per_cu->v.psymtab->readin))
f4dc4d17
DE
6486 {
6487 struct dwarf2_per_cu_data *per_cu = item->per_cu;
6488
6489 if (dwarf2_read_debug)
6490 {
6491 fprintf_unfiltered (gdb_stdlog,
6492 "Expanding symtab of %s at offset 0x%x\n",
6493 per_cu->is_debug_types ? "TU" : "CU",
6494 per_cu->offset.sect_off);
6495 }
6496
6497 if (per_cu->is_debug_types)
6498 process_full_type_unit (per_cu, item->pretend_language);
6499 else
6500 process_full_comp_unit (per_cu, item->pretend_language);
6501
6502 if (dwarf2_read_debug)
6503 {
6504 fprintf_unfiltered (gdb_stdlog,
6505 "Done expanding %s at offset 0x%x\n",
6506 per_cu->is_debug_types ? "TU" : "CU",
6507 per_cu->offset.sect_off);
6508 }
6509 }
10b3939b
DJ
6510
6511 item->per_cu->queued = 0;
6512 next_item = item->next;
6513 xfree (item);
6514 }
6515
6516 dwarf2_queue_tail = NULL;
45cfd468
DE
6517
6518 if (dwarf2_read_debug)
6519 {
6520 fprintf_unfiltered (gdb_stdlog, "Done expanding symtabs of %s.\n",
6521 dwarf2_per_objfile->objfile->name);
6522 }
10b3939b
DJ
6523}
6524
6525/* Free all allocated queue entries. This function only releases anything if
6526 an error was thrown; if the queue was processed then it would have been
6527 freed as we went along. */
6528
6529static void
6530dwarf2_release_queue (void *dummy)
6531{
6532 struct dwarf2_queue_item *item, *last;
6533
6534 item = dwarf2_queue;
6535 while (item)
6536 {
6537 /* Anything still marked queued is likely to be in an
6538 inconsistent state, so discard it. */
6539 if (item->per_cu->queued)
6540 {
6541 if (item->per_cu->cu != NULL)
dee91e82 6542 free_one_cached_comp_unit (item->per_cu);
10b3939b
DJ
6543 item->per_cu->queued = 0;
6544 }
6545
6546 last = item;
6547 item = item->next;
6548 xfree (last);
6549 }
6550
6551 dwarf2_queue = dwarf2_queue_tail = NULL;
6552}
6553
6554/* Read in full symbols for PST, and anything it depends on. */
6555
c906108c 6556static void
fba45db2 6557psymtab_to_symtab_1 (struct partial_symtab *pst)
c906108c 6558{
10b3939b 6559 struct dwarf2_per_cu_data *per_cu;
aaa75496
JB
6560 int i;
6561
95554aad
TT
6562 if (pst->readin)
6563 return;
6564
aaa75496 6565 for (i = 0; i < pst->number_of_dependencies; i++)
95554aad
TT
6566 if (!pst->dependencies[i]->readin
6567 && pst->dependencies[i]->user == NULL)
aaa75496
JB
6568 {
6569 /* Inform about additional files that need to be read in. */
6570 if (info_verbose)
6571 {
a3f17187 6572 /* FIXME: i18n: Need to make this a single string. */
aaa75496
JB
6573 fputs_filtered (" ", gdb_stdout);
6574 wrap_here ("");
6575 fputs_filtered ("and ", gdb_stdout);
6576 wrap_here ("");
6577 printf_filtered ("%s...", pst->dependencies[i]->filename);
0963b4bd 6578 wrap_here (""); /* Flush output. */
aaa75496
JB
6579 gdb_flush (gdb_stdout);
6580 }
6581 psymtab_to_symtab_1 (pst->dependencies[i]);
6582 }
6583
e38df1d0 6584 per_cu = pst->read_symtab_private;
10b3939b
DJ
6585
6586 if (per_cu == NULL)
aaa75496
JB
6587 {
6588 /* It's an include file, no symbols to read for it.
6589 Everything is in the parent symtab. */
6590 pst->readin = 1;
6591 return;
6592 }
c906108c 6593
a0f42c21 6594 dw2_do_instantiate_symtab (per_cu);
10b3939b
DJ
6595}
6596
dee91e82
DE
6597/* Trivial hash function for die_info: the hash value of a DIE
6598 is its offset in .debug_info for this objfile. */
10b3939b 6599
dee91e82
DE
6600static hashval_t
6601die_hash (const void *item)
10b3939b 6602{
dee91e82 6603 const struct die_info *die = item;
6502dd73 6604
dee91e82
DE
6605 return die->offset.sect_off;
6606}
63d06c5c 6607
dee91e82
DE
6608/* Trivial comparison function for die_info structures: two DIEs
6609 are equal if they have the same offset. */
98bfdba5 6610
dee91e82
DE
6611static int
6612die_eq (const void *item_lhs, const void *item_rhs)
6613{
6614 const struct die_info *die_lhs = item_lhs;
6615 const struct die_info *die_rhs = item_rhs;
c906108c 6616
dee91e82
DE
6617 return die_lhs->offset.sect_off == die_rhs->offset.sect_off;
6618}
c906108c 6619
dee91e82
DE
6620/* die_reader_func for load_full_comp_unit.
6621 This is identical to read_signatured_type_reader,
6622 but is kept separate for now. */
c906108c 6623
dee91e82
DE
6624static void
6625load_full_comp_unit_reader (const struct die_reader_specs *reader,
6626 gdb_byte *info_ptr,
6627 struct die_info *comp_unit_die,
6628 int has_children,
6629 void *data)
6630{
6631 struct dwarf2_cu *cu = reader->cu;
95554aad 6632 enum language *language_ptr = data;
6caca83c 6633
dee91e82
DE
6634 gdb_assert (cu->die_hash == NULL);
6635 cu->die_hash =
6636 htab_create_alloc_ex (cu->header.length / 12,
6637 die_hash,
6638 die_eq,
6639 NULL,
6640 &cu->comp_unit_obstack,
6641 hashtab_obstack_allocate,
6642 dummy_obstack_deallocate);
e142c38c 6643
dee91e82
DE
6644 if (has_children)
6645 comp_unit_die->child = read_die_and_siblings (reader, info_ptr,
6646 &info_ptr, comp_unit_die);
6647 cu->dies = comp_unit_die;
6648 /* comp_unit_die is not stored in die_hash, no need. */
10b3939b
DJ
6649
6650 /* We try not to read any attributes in this function, because not
9cdd5dbd 6651 all CUs needed for references have been loaded yet, and symbol
10b3939b 6652 table processing isn't initialized. But we have to set the CU language,
dee91e82
DE
6653 or we won't be able to build types correctly.
6654 Similarly, if we do not read the producer, we can not apply
6655 producer-specific interpretation. */
95554aad 6656 prepare_one_comp_unit (cu, cu->dies, *language_ptr);
dee91e82 6657}
10b3939b 6658
dee91e82 6659/* Load the DIEs associated with PER_CU into memory. */
a6c727b2 6660
dee91e82 6661static void
95554aad
TT
6662load_full_comp_unit (struct dwarf2_per_cu_data *this_cu,
6663 enum language pretend_language)
dee91e82 6664{
3019eac3 6665 gdb_assert (! this_cu->is_debug_types);
c5b7e1cb 6666
f4dc4d17
DE
6667 init_cutu_and_read_dies (this_cu, NULL, 1, 1,
6668 load_full_comp_unit_reader, &pretend_language);
10b3939b
DJ
6669}
6670
3da10d80
KS
6671/* Add a DIE to the delayed physname list. */
6672
6673static void
6674add_to_method_list (struct type *type, int fnfield_index, int index,
6675 const char *name, struct die_info *die,
6676 struct dwarf2_cu *cu)
6677{
6678 struct delayed_method_info mi;
6679 mi.type = type;
6680 mi.fnfield_index = fnfield_index;
6681 mi.index = index;
6682 mi.name = name;
6683 mi.die = die;
6684 VEC_safe_push (delayed_method_info, cu->method_list, &mi);
6685}
6686
6687/* A cleanup for freeing the delayed method list. */
6688
6689static void
6690free_delayed_list (void *ptr)
6691{
6692 struct dwarf2_cu *cu = (struct dwarf2_cu *) ptr;
6693 if (cu->method_list != NULL)
6694 {
6695 VEC_free (delayed_method_info, cu->method_list);
6696 cu->method_list = NULL;
6697 }
6698}
6699
6700/* Compute the physnames of any methods on the CU's method list.
6701
6702 The computation of method physnames is delayed in order to avoid the
6703 (bad) condition that one of the method's formal parameters is of an as yet
6704 incomplete type. */
6705
6706static void
6707compute_delayed_physnames (struct dwarf2_cu *cu)
6708{
6709 int i;
6710 struct delayed_method_info *mi;
6711 for (i = 0; VEC_iterate (delayed_method_info, cu->method_list, i, mi) ; ++i)
6712 {
1d06ead6 6713 const char *physname;
3da10d80
KS
6714 struct fn_fieldlist *fn_flp
6715 = &TYPE_FN_FIELDLIST (mi->type, mi->fnfield_index);
1d06ead6 6716 physname = dwarf2_physname ((char *) mi->name, mi->die, cu);
3da10d80
KS
6717 fn_flp->fn_fields[mi->index].physname = physname ? physname : "";
6718 }
6719}
6720
a766d390
DE
6721/* Go objects should be embedded in a DW_TAG_module DIE,
6722 and it's not clear if/how imported objects will appear.
6723 To keep Go support simple until that's worked out,
6724 go back through what we've read and create something usable.
6725 We could do this while processing each DIE, and feels kinda cleaner,
6726 but that way is more invasive.
6727 This is to, for example, allow the user to type "p var" or "b main"
6728 without having to specify the package name, and allow lookups
6729 of module.object to work in contexts that use the expression
6730 parser. */
6731
6732static void
6733fixup_go_packaging (struct dwarf2_cu *cu)
6734{
6735 char *package_name = NULL;
6736 struct pending *list;
6737 int i;
6738
6739 for (list = global_symbols; list != NULL; list = list->next)
6740 {
6741 for (i = 0; i < list->nsyms; ++i)
6742 {
6743 struct symbol *sym = list->symbol[i];
6744
6745 if (SYMBOL_LANGUAGE (sym) == language_go
6746 && SYMBOL_CLASS (sym) == LOC_BLOCK)
6747 {
6748 char *this_package_name = go_symbol_package_name (sym);
6749
6750 if (this_package_name == NULL)
6751 continue;
6752 if (package_name == NULL)
6753 package_name = this_package_name;
6754 else
6755 {
6756 if (strcmp (package_name, this_package_name) != 0)
6757 complaint (&symfile_complaints,
6758 _("Symtab %s has objects from two different Go packages: %s and %s"),
210bbc17
TT
6759 (SYMBOL_SYMTAB (sym)
6760 && SYMBOL_SYMTAB (sym)->filename
6761 ? SYMBOL_SYMTAB (sym)->filename
a766d390
DE
6762 : cu->objfile->name),
6763 this_package_name, package_name);
6764 xfree (this_package_name);
6765 }
6766 }
6767 }
6768 }
6769
6770 if (package_name != NULL)
6771 {
6772 struct objfile *objfile = cu->objfile;
6773 struct type *type = init_type (TYPE_CODE_MODULE, 0, 0,
6774 package_name, objfile);
6775 struct symbol *sym;
6776
6777 TYPE_TAG_NAME (type) = TYPE_NAME (type);
6778
6779 sym = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct symbol);
6780 SYMBOL_SET_LANGUAGE (sym, language_go);
6781 SYMBOL_SET_NAMES (sym, package_name, strlen (package_name), 1, objfile);
6782 /* This is not VAR_DOMAIN because we want a way to ensure a lookup of,
6783 e.g., "main" finds the "main" module and not C's main(). */
6784 SYMBOL_DOMAIN (sym) = STRUCT_DOMAIN;
6785 SYMBOL_CLASS (sym) = LOC_TYPEDEF;
6786 SYMBOL_TYPE (sym) = type;
6787
6788 add_symbol_to_list (sym, &global_symbols);
6789
6790 xfree (package_name);
6791 }
6792}
6793
95554aad
TT
6794static void compute_symtab_includes (struct dwarf2_per_cu_data *per_cu);
6795
6796/* Return the symtab for PER_CU. This works properly regardless of
6797 whether we're using the index or psymtabs. */
6798
6799static struct symtab *
6800get_symtab (struct dwarf2_per_cu_data *per_cu)
6801{
6802 return (dwarf2_per_objfile->using_index
6803 ? per_cu->v.quick->symtab
6804 : per_cu->v.psymtab->symtab);
6805}
6806
6807/* A helper function for computing the list of all symbol tables
6808 included by PER_CU. */
6809
6810static void
6811recursively_compute_inclusions (VEC (dwarf2_per_cu_ptr) **result,
6812 htab_t all_children,
6813 struct dwarf2_per_cu_data *per_cu)
6814{
6815 void **slot;
6816 int ix;
6817 struct dwarf2_per_cu_data *iter;
6818
6819 slot = htab_find_slot (all_children, per_cu, INSERT);
6820 if (*slot != NULL)
6821 {
6822 /* This inclusion and its children have been processed. */
6823 return;
6824 }
6825
6826 *slot = per_cu;
6827 /* Only add a CU if it has a symbol table. */
6828 if (get_symtab (per_cu) != NULL)
6829 VEC_safe_push (dwarf2_per_cu_ptr, *result, per_cu);
6830
6831 for (ix = 0;
f4dc4d17 6832 VEC_iterate (dwarf2_per_cu_ptr, per_cu->s.imported_symtabs, ix, iter);
95554aad
TT
6833 ++ix)
6834 recursively_compute_inclusions (result, all_children, iter);
6835}
6836
6837/* Compute the symtab 'includes' fields for the symtab related to
6838 PER_CU. */
6839
6840static void
6841compute_symtab_includes (struct dwarf2_per_cu_data *per_cu)
6842{
f4dc4d17
DE
6843 gdb_assert (! per_cu->is_debug_types);
6844
6845 if (!VEC_empty (dwarf2_per_cu_ptr, per_cu->s.imported_symtabs))
95554aad
TT
6846 {
6847 int ix, len;
6848 struct dwarf2_per_cu_data *iter;
6849 VEC (dwarf2_per_cu_ptr) *result_children = NULL;
6850 htab_t all_children;
6851 struct symtab *symtab = get_symtab (per_cu);
6852
6853 /* If we don't have a symtab, we can just skip this case. */
6854 if (symtab == NULL)
6855 return;
6856
6857 all_children = htab_create_alloc (1, htab_hash_pointer, htab_eq_pointer,
6858 NULL, xcalloc, xfree);
6859
6860 for (ix = 0;
f4dc4d17 6861 VEC_iterate (dwarf2_per_cu_ptr, per_cu->s.imported_symtabs,
95554aad
TT
6862 ix, iter);
6863 ++ix)
6864 recursively_compute_inclusions (&result_children, all_children, iter);
6865
6866 /* Now we have a transitive closure of all the included CUs, so
6867 we can convert it to a list of symtabs. */
6868 len = VEC_length (dwarf2_per_cu_ptr, result_children);
6869 symtab->includes
6870 = obstack_alloc (&dwarf2_per_objfile->objfile->objfile_obstack,
6871 (len + 1) * sizeof (struct symtab *));
6872 for (ix = 0;
6873 VEC_iterate (dwarf2_per_cu_ptr, result_children, ix, iter);
6874 ++ix)
6875 symtab->includes[ix] = get_symtab (iter);
6876 symtab->includes[len] = NULL;
6877
6878 VEC_free (dwarf2_per_cu_ptr, result_children);
6879 htab_delete (all_children);
6880 }
6881}
6882
6883/* Compute the 'includes' field for the symtabs of all the CUs we just
6884 read. */
6885
6886static void
6887process_cu_includes (void)
6888{
6889 int ix;
6890 struct dwarf2_per_cu_data *iter;
6891
6892 for (ix = 0;
6893 VEC_iterate (dwarf2_per_cu_ptr, dwarf2_per_objfile->just_read_cus,
6894 ix, iter);
6895 ++ix)
f4dc4d17
DE
6896 {
6897 if (! iter->is_debug_types)
6898 compute_symtab_includes (iter);
6899 }
95554aad
TT
6900
6901 VEC_free (dwarf2_per_cu_ptr, dwarf2_per_objfile->just_read_cus);
6902}
6903
9cdd5dbd 6904/* Generate full symbol information for PER_CU, whose DIEs have
10b3939b
DJ
6905 already been loaded into memory. */
6906
6907static void
95554aad
TT
6908process_full_comp_unit (struct dwarf2_per_cu_data *per_cu,
6909 enum language pretend_language)
10b3939b 6910{
10b3939b 6911 struct dwarf2_cu *cu = per_cu->cu;
9291a0cd 6912 struct objfile *objfile = per_cu->objfile;
10b3939b
DJ
6913 CORE_ADDR lowpc, highpc;
6914 struct symtab *symtab;
3da10d80 6915 struct cleanup *back_to, *delayed_list_cleanup;
10b3939b 6916 CORE_ADDR baseaddr;
4359dff1 6917 struct block *static_block;
10b3939b
DJ
6918
6919 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
6920
10b3939b
DJ
6921 buildsym_init ();
6922 back_to = make_cleanup (really_free_pendings, NULL);
3da10d80 6923 delayed_list_cleanup = make_cleanup (free_delayed_list, cu);
10b3939b
DJ
6924
6925 cu->list_in_scope = &file_symbols;
c906108c 6926
95554aad
TT
6927 cu->language = pretend_language;
6928 cu->language_defn = language_def (cu->language);
6929
c906108c 6930 /* Do line number decoding in read_file_scope () */
10b3939b 6931 process_die (cu->dies, cu);
c906108c 6932
a766d390
DE
6933 /* For now fudge the Go package. */
6934 if (cu->language == language_go)
6935 fixup_go_packaging (cu);
6936
3da10d80
KS
6937 /* Now that we have processed all the DIEs in the CU, all the types
6938 should be complete, and it should now be safe to compute all of the
6939 physnames. */
6940 compute_delayed_physnames (cu);
6941 do_cleanups (delayed_list_cleanup);
6942
fae299cd
DC
6943 /* Some compilers don't define a DW_AT_high_pc attribute for the
6944 compilation unit. If the DW_AT_high_pc is missing, synthesize
6945 it, by scanning the DIE's below the compilation unit. */
10b3939b 6946 get_scope_pc_bounds (cu->dies, &lowpc, &highpc, cu);
c906108c 6947
36586728
TT
6948 static_block
6949 = end_symtab_get_static_block (highpc + baseaddr, objfile, 0,
6950 per_cu->s.imported_symtabs != NULL);
4359dff1
JK
6951
6952 /* If the comp unit has DW_AT_ranges, it may have discontiguous ranges.
6953 Also, DW_AT_ranges may record ranges not belonging to any child DIEs
6954 (such as virtual method tables). Record the ranges in STATIC_BLOCK's
6955 addrmap to help ensure it has an accurate map of pc values belonging to
6956 this comp unit. */
6957 dwarf2_record_block_ranges (cu->dies, static_block, baseaddr, cu);
6958
6959 symtab = end_symtab_from_static_block (static_block, objfile,
6960 SECT_OFF_TEXT (objfile), 0);
c906108c 6961
8be455d7 6962 if (symtab != NULL)
c906108c 6963 {
df15bd07 6964 int gcc_4_minor = producer_is_gcc_ge_4 (cu->producer);
4632c0d0 6965
8be455d7
JK
6966 /* Set symtab language to language from DW_AT_language. If the
6967 compilation is from a C file generated by language preprocessors, do
6968 not set the language if it was already deduced by start_subfile. */
6969 if (!(cu->language == language_c && symtab->language != language_c))
6970 symtab->language = cu->language;
6971
6972 /* GCC-4.0 has started to support -fvar-tracking. GCC-3.x still can
6973 produce DW_AT_location with location lists but it can be possibly
ab260dad
JK
6974 invalid without -fvar-tracking. Still up to GCC-4.4.x incl. 4.4.0
6975 there were bugs in prologue debug info, fixed later in GCC-4.5
6976 by "unwind info for epilogues" patch (which is not directly related).
8be455d7
JK
6977
6978 For -gdwarf-4 type units LOCATIONS_VALID indication is fortunately not
6979 needed, it would be wrong due to missing DW_AT_producer there.
6980
6981 Still one can confuse GDB by using non-standard GCC compilation
6982 options - this waits on GCC PR other/32998 (-frecord-gcc-switches).
6983 */
ab260dad 6984 if (cu->has_loclist && gcc_4_minor >= 5)
8be455d7 6985 symtab->locations_valid = 1;
e0d00bc7
JK
6986
6987 if (gcc_4_minor >= 5)
6988 symtab->epilogue_unwind_valid = 1;
96408a79
SA
6989
6990 symtab->call_site_htab = cu->call_site_htab;
c906108c 6991 }
9291a0cd
TT
6992
6993 if (dwarf2_per_objfile->using_index)
6994 per_cu->v.quick->symtab = symtab;
6995 else
6996 {
6997 struct partial_symtab *pst = per_cu->v.psymtab;
6998 pst->symtab = symtab;
6999 pst->readin = 1;
7000 }
c906108c 7001
95554aad
TT
7002 /* Push it for inclusion processing later. */
7003 VEC_safe_push (dwarf2_per_cu_ptr, dwarf2_per_objfile->just_read_cus, per_cu);
7004
c906108c 7005 do_cleanups (back_to);
f4dc4d17 7006}
45cfd468 7007
f4dc4d17
DE
7008/* Generate full symbol information for type unit PER_CU, whose DIEs have
7009 already been loaded into memory. */
7010
7011static void
7012process_full_type_unit (struct dwarf2_per_cu_data *per_cu,
7013 enum language pretend_language)
7014{
7015 struct dwarf2_cu *cu = per_cu->cu;
7016 struct objfile *objfile = per_cu->objfile;
7017 struct symtab *symtab;
7018 struct cleanup *back_to, *delayed_list_cleanup;
7019
7020 buildsym_init ();
7021 back_to = make_cleanup (really_free_pendings, NULL);
7022 delayed_list_cleanup = make_cleanup (free_delayed_list, cu);
7023
7024 cu->list_in_scope = &file_symbols;
7025
7026 cu->language = pretend_language;
7027 cu->language_defn = language_def (cu->language);
7028
7029 /* The symbol tables are set up in read_type_unit_scope. */
7030 process_die (cu->dies, cu);
7031
7032 /* For now fudge the Go package. */
7033 if (cu->language == language_go)
7034 fixup_go_packaging (cu);
7035
7036 /* Now that we have processed all the DIEs in the CU, all the types
7037 should be complete, and it should now be safe to compute all of the
7038 physnames. */
7039 compute_delayed_physnames (cu);
7040 do_cleanups (delayed_list_cleanup);
7041
7042 /* TUs share symbol tables.
7043 If this is the first TU to use this symtab, complete the construction
094b34ac
DE
7044 of it with end_expandable_symtab. Otherwise, complete the addition of
7045 this TU's symbols to the existing symtab. */
f4dc4d17 7046 if (per_cu->s.type_unit_group->primary_symtab == NULL)
45cfd468 7047 {
f4dc4d17
DE
7048 symtab = end_expandable_symtab (0, objfile, SECT_OFF_TEXT (objfile));
7049 per_cu->s.type_unit_group->primary_symtab = symtab;
7050
7051 if (symtab != NULL)
7052 {
7053 /* Set symtab language to language from DW_AT_language. If the
7054 compilation is from a C file generated by language preprocessors,
7055 do not set the language if it was already deduced by
7056 start_subfile. */
7057 if (!(cu->language == language_c && symtab->language != language_c))
7058 symtab->language = cu->language;
7059 }
7060 }
7061 else
7062 {
7063 augment_type_symtab (objfile,
7064 per_cu->s.type_unit_group->primary_symtab);
7065 symtab = per_cu->s.type_unit_group->primary_symtab;
7066 }
7067
7068 if (dwarf2_per_objfile->using_index)
7069 per_cu->v.quick->symtab = symtab;
7070 else
7071 {
7072 struct partial_symtab *pst = per_cu->v.psymtab;
7073 pst->symtab = symtab;
7074 pst->readin = 1;
45cfd468 7075 }
f4dc4d17
DE
7076
7077 do_cleanups (back_to);
c906108c
SS
7078}
7079
95554aad
TT
7080/* Process an imported unit DIE. */
7081
7082static void
7083process_imported_unit_die (struct die_info *die, struct dwarf2_cu *cu)
7084{
7085 struct attribute *attr;
7086
f4dc4d17
DE
7087 /* For now we don't handle imported units in type units. */
7088 if (cu->per_cu->is_debug_types)
7089 {
7090 error (_("Dwarf Error: DW_TAG_imported_unit is not"
7091 " supported in type units [in module %s]"),
7092 cu->objfile->name);
7093 }
7094
95554aad
TT
7095 attr = dwarf2_attr (die, DW_AT_import, cu);
7096 if (attr != NULL)
7097 {
7098 struct dwarf2_per_cu_data *per_cu;
7099 struct symtab *imported_symtab;
7100 sect_offset offset;
36586728 7101 int is_dwz;
95554aad
TT
7102
7103 offset = dwarf2_get_ref_die_offset (attr);
36586728
TT
7104 is_dwz = (attr->form == DW_FORM_GNU_ref_alt || cu->per_cu->is_dwz);
7105 per_cu = dwarf2_find_containing_comp_unit (offset, is_dwz, cu->objfile);
95554aad
TT
7106
7107 /* Queue the unit, if needed. */
7108 if (maybe_queue_comp_unit (cu, per_cu, cu->language))
7109 load_full_comp_unit (per_cu, cu->language);
7110
f4dc4d17 7111 VEC_safe_push (dwarf2_per_cu_ptr, cu->per_cu->s.imported_symtabs,
95554aad
TT
7112 per_cu);
7113 }
7114}
7115
c906108c
SS
7116/* Process a die and its children. */
7117
7118static void
e7c27a73 7119process_die (struct die_info *die, struct dwarf2_cu *cu)
c906108c
SS
7120{
7121 switch (die->tag)
7122 {
7123 case DW_TAG_padding:
7124 break;
7125 case DW_TAG_compile_unit:
95554aad 7126 case DW_TAG_partial_unit:
e7c27a73 7127 read_file_scope (die, cu);
c906108c 7128 break;
348e048f
DE
7129 case DW_TAG_type_unit:
7130 read_type_unit_scope (die, cu);
7131 break;
c906108c 7132 case DW_TAG_subprogram:
c906108c 7133 case DW_TAG_inlined_subroutine:
edb3359d 7134 read_func_scope (die, cu);
c906108c
SS
7135 break;
7136 case DW_TAG_lexical_block:
14898363
L
7137 case DW_TAG_try_block:
7138 case DW_TAG_catch_block:
e7c27a73 7139 read_lexical_block_scope (die, cu);
c906108c 7140 break;
96408a79
SA
7141 case DW_TAG_GNU_call_site:
7142 read_call_site_scope (die, cu);
7143 break;
c906108c 7144 case DW_TAG_class_type:
680b30c7 7145 case DW_TAG_interface_type:
c906108c
SS
7146 case DW_TAG_structure_type:
7147 case DW_TAG_union_type:
134d01f1 7148 process_structure_scope (die, cu);
c906108c
SS
7149 break;
7150 case DW_TAG_enumeration_type:
134d01f1 7151 process_enumeration_scope (die, cu);
c906108c 7152 break;
134d01f1 7153
f792889a
DJ
7154 /* These dies have a type, but processing them does not create
7155 a symbol or recurse to process the children. Therefore we can
7156 read them on-demand through read_type_die. */
c906108c 7157 case DW_TAG_subroutine_type:
72019c9c 7158 case DW_TAG_set_type:
c906108c 7159 case DW_TAG_array_type:
c906108c 7160 case DW_TAG_pointer_type:
c906108c 7161 case DW_TAG_ptr_to_member_type:
c906108c 7162 case DW_TAG_reference_type:
c906108c 7163 case DW_TAG_string_type:
c906108c 7164 break;
134d01f1 7165
c906108c 7166 case DW_TAG_base_type:
a02abb62 7167 case DW_TAG_subrange_type:
cb249c71 7168 case DW_TAG_typedef:
134d01f1
DJ
7169 /* Add a typedef symbol for the type definition, if it has a
7170 DW_AT_name. */
f792889a 7171 new_symbol (die, read_type_die (die, cu), cu);
a02abb62 7172 break;
c906108c 7173 case DW_TAG_common_block:
e7c27a73 7174 read_common_block (die, cu);
c906108c
SS
7175 break;
7176 case DW_TAG_common_inclusion:
7177 break;
d9fa45fe 7178 case DW_TAG_namespace:
63d06c5c 7179 processing_has_namespace_info = 1;
e7c27a73 7180 read_namespace (die, cu);
d9fa45fe 7181 break;
5d7cb8df 7182 case DW_TAG_module:
f55ee35c 7183 processing_has_namespace_info = 1;
5d7cb8df
JK
7184 read_module (die, cu);
7185 break;
d9fa45fe
DC
7186 case DW_TAG_imported_declaration:
7187 case DW_TAG_imported_module:
63d06c5c 7188 processing_has_namespace_info = 1;
27aa8d6a
SW
7189 if (die->child != NULL && (die->tag == DW_TAG_imported_declaration
7190 || cu->language != language_fortran))
7191 complaint (&symfile_complaints, _("Tag '%s' has unexpected children"),
7192 dwarf_tag_name (die->tag));
7193 read_import_statement (die, cu);
d9fa45fe 7194 break;
95554aad
TT
7195
7196 case DW_TAG_imported_unit:
7197 process_imported_unit_die (die, cu);
7198 break;
7199
c906108c 7200 default:
e7c27a73 7201 new_symbol (die, NULL, cu);
c906108c
SS
7202 break;
7203 }
7204}
7205
94af9270
KS
7206/* A helper function for dwarf2_compute_name which determines whether DIE
7207 needs to have the name of the scope prepended to the name listed in the
7208 die. */
7209
7210static int
7211die_needs_namespace (struct die_info *die, struct dwarf2_cu *cu)
7212{
1c809c68
TT
7213 struct attribute *attr;
7214
94af9270
KS
7215 switch (die->tag)
7216 {
7217 case DW_TAG_namespace:
7218 case DW_TAG_typedef:
7219 case DW_TAG_class_type:
7220 case DW_TAG_interface_type:
7221 case DW_TAG_structure_type:
7222 case DW_TAG_union_type:
7223 case DW_TAG_enumeration_type:
7224 case DW_TAG_enumerator:
7225 case DW_TAG_subprogram:
7226 case DW_TAG_member:
7227 return 1;
7228
7229 case DW_TAG_variable:
c2b0a229 7230 case DW_TAG_constant:
94af9270
KS
7231 /* We only need to prefix "globally" visible variables. These include
7232 any variable marked with DW_AT_external or any variable that
7233 lives in a namespace. [Variables in anonymous namespaces
7234 require prefixing, but they are not DW_AT_external.] */
7235
7236 if (dwarf2_attr (die, DW_AT_specification, cu))
7237 {
7238 struct dwarf2_cu *spec_cu = cu;
9a619af0 7239
94af9270
KS
7240 return die_needs_namespace (die_specification (die, &spec_cu),
7241 spec_cu);
7242 }
7243
1c809c68 7244 attr = dwarf2_attr (die, DW_AT_external, cu);
f55ee35c
JK
7245 if (attr == NULL && die->parent->tag != DW_TAG_namespace
7246 && die->parent->tag != DW_TAG_module)
1c809c68
TT
7247 return 0;
7248 /* A variable in a lexical block of some kind does not need a
7249 namespace, even though in C++ such variables may be external
7250 and have a mangled name. */
7251 if (die->parent->tag == DW_TAG_lexical_block
7252 || die->parent->tag == DW_TAG_try_block
1054b214
TT
7253 || die->parent->tag == DW_TAG_catch_block
7254 || die->parent->tag == DW_TAG_subprogram)
1c809c68
TT
7255 return 0;
7256 return 1;
94af9270
KS
7257
7258 default:
7259 return 0;
7260 }
7261}
7262
98bfdba5
PA
7263/* Retrieve the last character from a mem_file. */
7264
7265static void
7266do_ui_file_peek_last (void *object, const char *buffer, long length)
7267{
7268 char *last_char_p = (char *) object;
7269
7270 if (length > 0)
7271 *last_char_p = buffer[length - 1];
7272}
7273
94af9270 7274/* Compute the fully qualified name of DIE in CU. If PHYSNAME is nonzero,
a766d390
DE
7275 compute the physname for the object, which include a method's:
7276 - formal parameters (C++/Java),
7277 - receiver type (Go),
7278 - return type (Java).
7279
7280 The term "physname" is a bit confusing.
7281 For C++, for example, it is the demangled name.
7282 For Go, for example, it's the mangled name.
94af9270 7283
af6b7be1
JB
7284 For Ada, return the DIE's linkage name rather than the fully qualified
7285 name. PHYSNAME is ignored..
7286
94af9270
KS
7287 The result is allocated on the objfile_obstack and canonicalized. */
7288
7289static const char *
7290dwarf2_compute_name (char *name, struct die_info *die, struct dwarf2_cu *cu,
7291 int physname)
7292{
bb5ed363
DE
7293 struct objfile *objfile = cu->objfile;
7294
94af9270
KS
7295 if (name == NULL)
7296 name = dwarf2_name (die, cu);
7297
f55ee35c
JK
7298 /* For Fortran GDB prefers DW_AT_*linkage_name if present but otherwise
7299 compute it by typename_concat inside GDB. */
7300 if (cu->language == language_ada
7301 || (cu->language == language_fortran && physname))
7302 {
7303 /* For Ada unit, we prefer the linkage name over the name, as
7304 the former contains the exported name, which the user expects
7305 to be able to reference. Ideally, we want the user to be able
7306 to reference this entity using either natural or linkage name,
7307 but we haven't started looking at this enhancement yet. */
7308 struct attribute *attr;
7309
7310 attr = dwarf2_attr (die, DW_AT_linkage_name, cu);
7311 if (attr == NULL)
7312 attr = dwarf2_attr (die, DW_AT_MIPS_linkage_name, cu);
7313 if (attr && DW_STRING (attr))
7314 return DW_STRING (attr);
7315 }
7316
94af9270
KS
7317 /* These are the only languages we know how to qualify names in. */
7318 if (name != NULL
f55ee35c
JK
7319 && (cu->language == language_cplus || cu->language == language_java
7320 || cu->language == language_fortran))
94af9270
KS
7321 {
7322 if (die_needs_namespace (die, cu))
7323 {
7324 long length;
0d5cff50 7325 const char *prefix;
94af9270
KS
7326 struct ui_file *buf;
7327
7328 prefix = determine_prefix (die, cu);
7329 buf = mem_fileopen ();
7330 if (*prefix != '\0')
7331 {
f55ee35c
JK
7332 char *prefixed_name = typename_concat (NULL, prefix, name,
7333 physname, cu);
9a619af0 7334
94af9270
KS
7335 fputs_unfiltered (prefixed_name, buf);
7336 xfree (prefixed_name);
7337 }
7338 else
62d5b8da 7339 fputs_unfiltered (name, buf);
94af9270 7340
98bfdba5
PA
7341 /* Template parameters may be specified in the DIE's DW_AT_name, or
7342 as children with DW_TAG_template_type_param or
7343 DW_TAG_value_type_param. If the latter, add them to the name
7344 here. If the name already has template parameters, then
7345 skip this step; some versions of GCC emit both, and
7346 it is more efficient to use the pre-computed name.
7347
7348 Something to keep in mind about this process: it is very
7349 unlikely, or in some cases downright impossible, to produce
7350 something that will match the mangled name of a function.
7351 If the definition of the function has the same debug info,
7352 we should be able to match up with it anyway. But fallbacks
7353 using the minimal symbol, for instance to find a method
7354 implemented in a stripped copy of libstdc++, will not work.
7355 If we do not have debug info for the definition, we will have to
7356 match them up some other way.
7357
7358 When we do name matching there is a related problem with function
7359 templates; two instantiated function templates are allowed to
7360 differ only by their return types, which we do not add here. */
7361
7362 if (cu->language == language_cplus && strchr (name, '<') == NULL)
7363 {
7364 struct attribute *attr;
7365 struct die_info *child;
7366 int first = 1;
7367
7368 die->building_fullname = 1;
7369
7370 for (child = die->child; child != NULL; child = child->sibling)
7371 {
7372 struct type *type;
12df843f 7373 LONGEST value;
98bfdba5
PA
7374 gdb_byte *bytes;
7375 struct dwarf2_locexpr_baton *baton;
7376 struct value *v;
7377
7378 if (child->tag != DW_TAG_template_type_param
7379 && child->tag != DW_TAG_template_value_param)
7380 continue;
7381
7382 if (first)
7383 {
7384 fputs_unfiltered ("<", buf);
7385 first = 0;
7386 }
7387 else
7388 fputs_unfiltered (", ", buf);
7389
7390 attr = dwarf2_attr (child, DW_AT_type, cu);
7391 if (attr == NULL)
7392 {
7393 complaint (&symfile_complaints,
7394 _("template parameter missing DW_AT_type"));
7395 fputs_unfiltered ("UNKNOWN_TYPE", buf);
7396 continue;
7397 }
7398 type = die_type (child, cu);
7399
7400 if (child->tag == DW_TAG_template_type_param)
7401 {
79d43c61 7402 c_print_type (type, "", buf, -1, 0, &type_print_raw_options);
98bfdba5
PA
7403 continue;
7404 }
7405
7406 attr = dwarf2_attr (child, DW_AT_const_value, cu);
7407 if (attr == NULL)
7408 {
7409 complaint (&symfile_complaints,
3e43a32a
MS
7410 _("template parameter missing "
7411 "DW_AT_const_value"));
98bfdba5
PA
7412 fputs_unfiltered ("UNKNOWN_VALUE", buf);
7413 continue;
7414 }
7415
7416 dwarf2_const_value_attr (attr, type, name,
7417 &cu->comp_unit_obstack, cu,
7418 &value, &bytes, &baton);
7419
7420 if (TYPE_NOSIGN (type))
7421 /* GDB prints characters as NUMBER 'CHAR'. If that's
7422 changed, this can use value_print instead. */
7423 c_printchar (value, type, buf);
7424 else
7425 {
7426 struct value_print_options opts;
7427
7428 if (baton != NULL)
7429 v = dwarf2_evaluate_loc_desc (type, NULL,
7430 baton->data,
7431 baton->size,
7432 baton->per_cu);
7433 else if (bytes != NULL)
7434 {
7435 v = allocate_value (type);
7436 memcpy (value_contents_writeable (v), bytes,
7437 TYPE_LENGTH (type));
7438 }
7439 else
7440 v = value_from_longest (type, value);
7441
3e43a32a
MS
7442 /* Specify decimal so that we do not depend on
7443 the radix. */
98bfdba5
PA
7444 get_formatted_print_options (&opts, 'd');
7445 opts.raw = 1;
7446 value_print (v, buf, &opts);
7447 release_value (v);
7448 value_free (v);
7449 }
7450 }
7451
7452 die->building_fullname = 0;
7453
7454 if (!first)
7455 {
7456 /* Close the argument list, with a space if necessary
7457 (nested templates). */
7458 char last_char = '\0';
7459 ui_file_put (buf, do_ui_file_peek_last, &last_char);
7460 if (last_char == '>')
7461 fputs_unfiltered (" >", buf);
7462 else
7463 fputs_unfiltered (">", buf);
7464 }
7465 }
7466
94af9270
KS
7467 /* For Java and C++ methods, append formal parameter type
7468 information, if PHYSNAME. */
6e70227d 7469
94af9270
KS
7470 if (physname && die->tag == DW_TAG_subprogram
7471 && (cu->language == language_cplus
7472 || cu->language == language_java))
7473 {
7474 struct type *type = read_type_die (die, cu);
7475
79d43c61
TT
7476 c_type_print_args (type, buf, 1, cu->language,
7477 &type_print_raw_options);
94af9270
KS
7478
7479 if (cu->language == language_java)
7480 {
7481 /* For java, we must append the return type to method
0963b4bd 7482 names. */
94af9270
KS
7483 if (die->tag == DW_TAG_subprogram)
7484 java_print_type (TYPE_TARGET_TYPE (type), "", buf,
79d43c61 7485 0, 0, &type_print_raw_options);
94af9270
KS
7486 }
7487 else if (cu->language == language_cplus)
7488 {
60430eff
DJ
7489 /* Assume that an artificial first parameter is
7490 "this", but do not crash if it is not. RealView
7491 marks unnamed (and thus unused) parameters as
7492 artificial; there is no way to differentiate
7493 the two cases. */
94af9270
KS
7494 if (TYPE_NFIELDS (type) > 0
7495 && TYPE_FIELD_ARTIFICIAL (type, 0)
60430eff 7496 && TYPE_CODE (TYPE_FIELD_TYPE (type, 0)) == TYPE_CODE_PTR
3e43a32a
MS
7497 && TYPE_CONST (TYPE_TARGET_TYPE (TYPE_FIELD_TYPE (type,
7498 0))))
94af9270
KS
7499 fputs_unfiltered (" const", buf);
7500 }
7501 }
7502
bb5ed363 7503 name = ui_file_obsavestring (buf, &objfile->objfile_obstack,
94af9270
KS
7504 &length);
7505 ui_file_delete (buf);
7506
7507 if (cu->language == language_cplus)
7508 {
7509 char *cname
7510 = dwarf2_canonicalize_name (name, cu,
bb5ed363 7511 &objfile->objfile_obstack);
9a619af0 7512
94af9270
KS
7513 if (cname != NULL)
7514 name = cname;
7515 }
7516 }
7517 }
7518
7519 return name;
7520}
7521
0114d602
DJ
7522/* Return the fully qualified name of DIE, based on its DW_AT_name.
7523 If scope qualifiers are appropriate they will be added. The result
7524 will be allocated on the objfile_obstack, or NULL if the DIE does
94af9270
KS
7525 not have a name. NAME may either be from a previous call to
7526 dwarf2_name or NULL.
7527
0963b4bd 7528 The output string will be canonicalized (if C++/Java). */
0114d602
DJ
7529
7530static const char *
94af9270 7531dwarf2_full_name (char *name, struct die_info *die, struct dwarf2_cu *cu)
0114d602 7532{
94af9270
KS
7533 return dwarf2_compute_name (name, die, cu, 0);
7534}
0114d602 7535
94af9270
KS
7536/* Construct a physname for the given DIE in CU. NAME may either be
7537 from a previous call to dwarf2_name or NULL. The result will be
7538 allocated on the objfile_objstack or NULL if the DIE does not have a
7539 name.
0114d602 7540
94af9270 7541 The output string will be canonicalized (if C++/Java). */
0114d602 7542
94af9270
KS
7543static const char *
7544dwarf2_physname (char *name, struct die_info *die, struct dwarf2_cu *cu)
7545{
bb5ed363 7546 struct objfile *objfile = cu->objfile;
900e11f9
JK
7547 struct attribute *attr;
7548 const char *retval, *mangled = NULL, *canon = NULL;
7549 struct cleanup *back_to;
7550 int need_copy = 1;
7551
7552 /* In this case dwarf2_compute_name is just a shortcut not building anything
7553 on its own. */
7554 if (!die_needs_namespace (die, cu))
7555 return dwarf2_compute_name (name, die, cu, 1);
7556
7557 back_to = make_cleanup (null_cleanup, NULL);
7558
7559 attr = dwarf2_attr (die, DW_AT_linkage_name, cu);
7560 if (!attr)
7561 attr = dwarf2_attr (die, DW_AT_MIPS_linkage_name, cu);
7562
7563 /* DW_AT_linkage_name is missing in some cases - depend on what GDB
7564 has computed. */
7565 if (attr && DW_STRING (attr))
7566 {
7567 char *demangled;
7568
7569 mangled = DW_STRING (attr);
7570
7571 /* Use DMGL_RET_DROP for C++ template functions to suppress their return
7572 type. It is easier for GDB users to search for such functions as
7573 `name(params)' than `long name(params)'. In such case the minimal
7574 symbol names do not match the full symbol names but for template
7575 functions there is never a need to look up their definition from their
7576 declaration so the only disadvantage remains the minimal symbol
7577 variant `long name(params)' does not have the proper inferior type.
7578 */
7579
a766d390
DE
7580 if (cu->language == language_go)
7581 {
7582 /* This is a lie, but we already lie to the caller new_symbol_full.
7583 new_symbol_full assumes we return the mangled name.
7584 This just undoes that lie until things are cleaned up. */
7585 demangled = NULL;
7586 }
7587 else
7588 {
7589 demangled = cplus_demangle (mangled,
7590 (DMGL_PARAMS | DMGL_ANSI
7591 | (cu->language == language_java
7592 ? DMGL_JAVA | DMGL_RET_POSTFIX
7593 : DMGL_RET_DROP)));
7594 }
900e11f9
JK
7595 if (demangled)
7596 {
7597 make_cleanup (xfree, demangled);
7598 canon = demangled;
7599 }
7600 else
7601 {
7602 canon = mangled;
7603 need_copy = 0;
7604 }
7605 }
7606
7607 if (canon == NULL || check_physname)
7608 {
7609 const char *physname = dwarf2_compute_name (name, die, cu, 1);
7610
7611 if (canon != NULL && strcmp (physname, canon) != 0)
7612 {
7613 /* It may not mean a bug in GDB. The compiler could also
7614 compute DW_AT_linkage_name incorrectly. But in such case
7615 GDB would need to be bug-to-bug compatible. */
7616
7617 complaint (&symfile_complaints,
7618 _("Computed physname <%s> does not match demangled <%s> "
7619 "(from linkage <%s>) - DIE at 0x%x [in module %s]"),
b64f50a1 7620 physname, canon, mangled, die->offset.sect_off, objfile->name);
900e11f9
JK
7621
7622 /* Prefer DW_AT_linkage_name (in the CANON form) - when it
7623 is available here - over computed PHYSNAME. It is safer
7624 against both buggy GDB and buggy compilers. */
7625
7626 retval = canon;
7627 }
7628 else
7629 {
7630 retval = physname;
7631 need_copy = 0;
7632 }
7633 }
7634 else
7635 retval = canon;
7636
7637 if (need_copy)
7638 retval = obsavestring (retval, strlen (retval),
bb5ed363 7639 &objfile->objfile_obstack);
900e11f9
JK
7640
7641 do_cleanups (back_to);
7642 return retval;
0114d602
DJ
7643}
7644
27aa8d6a
SW
7645/* Read the import statement specified by the given die and record it. */
7646
7647static void
7648read_import_statement (struct die_info *die, struct dwarf2_cu *cu)
7649{
bb5ed363 7650 struct objfile *objfile = cu->objfile;
27aa8d6a 7651 struct attribute *import_attr;
32019081 7652 struct die_info *imported_die, *child_die;
de4affc9 7653 struct dwarf2_cu *imported_cu;
27aa8d6a 7654 const char *imported_name;
794684b6 7655 const char *imported_name_prefix;
13387711
SW
7656 const char *canonical_name;
7657 const char *import_alias;
7658 const char *imported_declaration = NULL;
794684b6 7659 const char *import_prefix;
32019081
JK
7660 VEC (const_char_ptr) *excludes = NULL;
7661 struct cleanup *cleanups;
13387711
SW
7662
7663 char *temp;
27aa8d6a
SW
7664
7665 import_attr = dwarf2_attr (die, DW_AT_import, cu);
7666 if (import_attr == NULL)
7667 {
7668 complaint (&symfile_complaints, _("Tag '%s' has no DW_AT_import"),
7669 dwarf_tag_name (die->tag));
7670 return;
7671 }
7672
de4affc9
CC
7673 imported_cu = cu;
7674 imported_die = follow_die_ref_or_sig (die, import_attr, &imported_cu);
7675 imported_name = dwarf2_name (imported_die, imported_cu);
27aa8d6a
SW
7676 if (imported_name == NULL)
7677 {
7678 /* GCC bug: https://bugzilla.redhat.com/show_bug.cgi?id=506524
7679
7680 The import in the following code:
7681 namespace A
7682 {
7683 typedef int B;
7684 }
7685
7686 int main ()
7687 {
7688 using A::B;
7689 B b;
7690 return b;
7691 }
7692
7693 ...
7694 <2><51>: Abbrev Number: 3 (DW_TAG_imported_declaration)
7695 <52> DW_AT_decl_file : 1
7696 <53> DW_AT_decl_line : 6
7697 <54> DW_AT_import : <0x75>
7698 <2><58>: Abbrev Number: 4 (DW_TAG_typedef)
7699 <59> DW_AT_name : B
7700 <5b> DW_AT_decl_file : 1
7701 <5c> DW_AT_decl_line : 2
7702 <5d> DW_AT_type : <0x6e>
7703 ...
7704 <1><75>: Abbrev Number: 7 (DW_TAG_base_type)
7705 <76> DW_AT_byte_size : 4
7706 <77> DW_AT_encoding : 5 (signed)
7707
7708 imports the wrong die ( 0x75 instead of 0x58 ).
7709 This case will be ignored until the gcc bug is fixed. */
7710 return;
7711 }
7712
82856980
SW
7713 /* Figure out the local name after import. */
7714 import_alias = dwarf2_name (die, cu);
27aa8d6a 7715
794684b6
SW
7716 /* Figure out where the statement is being imported to. */
7717 import_prefix = determine_prefix (die, cu);
7718
7719 /* Figure out what the scope of the imported die is and prepend it
7720 to the name of the imported die. */
de4affc9 7721 imported_name_prefix = determine_prefix (imported_die, imported_cu);
794684b6 7722
f55ee35c
JK
7723 if (imported_die->tag != DW_TAG_namespace
7724 && imported_die->tag != DW_TAG_module)
794684b6 7725 {
13387711
SW
7726 imported_declaration = imported_name;
7727 canonical_name = imported_name_prefix;
794684b6 7728 }
13387711 7729 else if (strlen (imported_name_prefix) > 0)
794684b6 7730 {
13387711
SW
7731 temp = alloca (strlen (imported_name_prefix)
7732 + 2 + strlen (imported_name) + 1);
7733 strcpy (temp, imported_name_prefix);
7734 strcat (temp, "::");
7735 strcat (temp, imported_name);
7736 canonical_name = temp;
794684b6 7737 }
13387711
SW
7738 else
7739 canonical_name = imported_name;
794684b6 7740
32019081
JK
7741 cleanups = make_cleanup (VEC_cleanup (const_char_ptr), &excludes);
7742
7743 if (die->tag == DW_TAG_imported_module && cu->language == language_fortran)
7744 for (child_die = die->child; child_die && child_die->tag;
7745 child_die = sibling_die (child_die))
7746 {
7747 /* DWARF-4: A Fortran use statement with a “rename list” may be
7748 represented by an imported module entry with an import attribute
7749 referring to the module and owned entries corresponding to those
7750 entities that are renamed as part of being imported. */
7751
7752 if (child_die->tag != DW_TAG_imported_declaration)
7753 {
7754 complaint (&symfile_complaints,
7755 _("child DW_TAG_imported_declaration expected "
7756 "- DIE at 0x%x [in module %s]"),
b64f50a1 7757 child_die->offset.sect_off, objfile->name);
32019081
JK
7758 continue;
7759 }
7760
7761 import_attr = dwarf2_attr (child_die, DW_AT_import, cu);
7762 if (import_attr == NULL)
7763 {
7764 complaint (&symfile_complaints, _("Tag '%s' has no DW_AT_import"),
7765 dwarf_tag_name (child_die->tag));
7766 continue;
7767 }
7768
7769 imported_cu = cu;
7770 imported_die = follow_die_ref_or_sig (child_die, import_attr,
7771 &imported_cu);
7772 imported_name = dwarf2_name (imported_die, imported_cu);
7773 if (imported_name == NULL)
7774 {
7775 complaint (&symfile_complaints,
7776 _("child DW_TAG_imported_declaration has unknown "
7777 "imported name - DIE at 0x%x [in module %s]"),
b64f50a1 7778 child_die->offset.sect_off, objfile->name);
32019081
JK
7779 continue;
7780 }
7781
7782 VEC_safe_push (const_char_ptr, excludes, imported_name);
7783
7784 process_die (child_die, cu);
7785 }
7786
c0cc3a76
SW
7787 cp_add_using_directive (import_prefix,
7788 canonical_name,
7789 import_alias,
13387711 7790 imported_declaration,
32019081 7791 excludes,
bb5ed363 7792 &objfile->objfile_obstack);
32019081
JK
7793
7794 do_cleanups (cleanups);
27aa8d6a
SW
7795}
7796
f4dc4d17 7797/* Cleanup function for handle_DW_AT_stmt_list. */
ae2de4f8 7798
cb1df416
DJ
7799static void
7800free_cu_line_header (void *arg)
7801{
7802 struct dwarf2_cu *cu = arg;
7803
7804 free_line_header (cu->line_header);
7805 cu->line_header = NULL;
7806}
7807
9291a0cd
TT
7808static void
7809find_file_and_directory (struct die_info *die, struct dwarf2_cu *cu,
7810 char **name, char **comp_dir)
7811{
7812 struct attribute *attr;
7813
7814 *name = NULL;
7815 *comp_dir = NULL;
7816
7817 /* Find the filename. Do not use dwarf2_name here, since the filename
7818 is not a source language identifier. */
7819 attr = dwarf2_attr (die, DW_AT_name, cu);
7820 if (attr)
7821 {
7822 *name = DW_STRING (attr);
7823 }
7824
7825 attr = dwarf2_attr (die, DW_AT_comp_dir, cu);
7826 if (attr)
7827 *comp_dir = DW_STRING (attr);
7828 else if (*name != NULL && IS_ABSOLUTE_PATH (*name))
7829 {
7830 *comp_dir = ldirname (*name);
7831 if (*comp_dir != NULL)
7832 make_cleanup (xfree, *comp_dir);
7833 }
7834 if (*comp_dir != NULL)
7835 {
7836 /* Irix 6.2 native cc prepends <machine>.: to the compilation
7837 directory, get rid of it. */
7838 char *cp = strchr (*comp_dir, ':');
7839
7840 if (cp && cp != *comp_dir && cp[-1] == '.' && cp[1] == '/')
7841 *comp_dir = cp + 1;
7842 }
7843
7844 if (*name == NULL)
7845 *name = "<unknown>";
7846}
7847
f4dc4d17
DE
7848/* Handle DW_AT_stmt_list for a compilation unit.
7849 DIE is the DW_TAG_compile_unit die for CU.
f3f5162e
DE
7850 COMP_DIR is the compilation directory.
7851 WANT_LINE_INFO is non-zero if the pc/line-number mapping is needed. */
2ab95328
TT
7852
7853static void
7854handle_DW_AT_stmt_list (struct die_info *die, struct dwarf2_cu *cu,
f4dc4d17 7855 const char *comp_dir)
2ab95328
TT
7856{
7857 struct attribute *attr;
2ab95328 7858
f4dc4d17
DE
7859 gdb_assert (! cu->per_cu->is_debug_types);
7860
2ab95328
TT
7861 attr = dwarf2_attr (die, DW_AT_stmt_list, cu);
7862 if (attr)
7863 {
7864 unsigned int line_offset = DW_UNSND (attr);
7865 struct line_header *line_header
3019eac3 7866 = dwarf_decode_line_header (line_offset, cu);
2ab95328
TT
7867
7868 if (line_header)
dee91e82
DE
7869 {
7870 cu->line_header = line_header;
7871 make_cleanup (free_cu_line_header, cu);
f4dc4d17 7872 dwarf_decode_lines (line_header, comp_dir, cu, NULL, 1);
dee91e82 7873 }
2ab95328
TT
7874 }
7875}
7876
95554aad 7877/* Process DW_TAG_compile_unit or DW_TAG_partial_unit. */
ae2de4f8 7878
c906108c 7879static void
e7c27a73 7880read_file_scope (struct die_info *die, struct dwarf2_cu *cu)
c906108c 7881{
dee91e82 7882 struct objfile *objfile = dwarf2_per_objfile->objfile;
debd256d 7883 struct cleanup *back_to = make_cleanup (null_cleanup, 0);
2acceee2 7884 CORE_ADDR lowpc = ((CORE_ADDR) -1);
c906108c
SS
7885 CORE_ADDR highpc = ((CORE_ADDR) 0);
7886 struct attribute *attr;
e1024ff1 7887 char *name = NULL;
c906108c
SS
7888 char *comp_dir = NULL;
7889 struct die_info *child_die;
7890 bfd *abfd = objfile->obfd;
e142c38c 7891 CORE_ADDR baseaddr;
6e70227d 7892
e142c38c 7893 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
c906108c 7894
fae299cd 7895 get_scope_pc_bounds (die, &lowpc, &highpc, cu);
c906108c
SS
7896
7897 /* If we didn't find a lowpc, set it to highpc to avoid complaints
7898 from finish_block. */
2acceee2 7899 if (lowpc == ((CORE_ADDR) -1))
c906108c
SS
7900 lowpc = highpc;
7901 lowpc += baseaddr;
7902 highpc += baseaddr;
7903
9291a0cd 7904 find_file_and_directory (die, cu, &name, &comp_dir);
e1024ff1 7905
95554aad 7906 prepare_one_comp_unit (cu, die, cu->language);
303b6f5d 7907
f4b8a18d
KW
7908 /* The XLCL doesn't generate DW_LANG_OpenCL because this attribute is not
7909 standardised yet. As a workaround for the language detection we fall
7910 back to the DW_AT_producer string. */
7911 if (cu->producer && strstr (cu->producer, "IBM XL C for OpenCL") != NULL)
7912 cu->language = language_opencl;
7913
3019eac3
DE
7914 /* Similar hack for Go. */
7915 if (cu->producer && strstr (cu->producer, "GNU Go ") != NULL)
7916 set_cu_language (DW_LANG_Go, cu);
7917
f4dc4d17 7918 dwarf2_start_symtab (cu, name, comp_dir, lowpc);
3019eac3
DE
7919
7920 /* Decode line number information if present. We do this before
7921 processing child DIEs, so that the line header table is available
7922 for DW_AT_decl_file. */
f4dc4d17 7923 handle_DW_AT_stmt_list (die, cu, comp_dir);
3019eac3
DE
7924
7925 /* Process all dies in compilation unit. */
7926 if (die->child != NULL)
7927 {
7928 child_die = die->child;
7929 while (child_die && child_die->tag)
7930 {
7931 process_die (child_die, cu);
7932 child_die = sibling_die (child_die);
7933 }
7934 }
7935
7936 /* Decode macro information, if present. Dwarf 2 macro information
7937 refers to information in the line number info statement program
7938 header, so we can only read it if we've read the header
7939 successfully. */
7940 attr = dwarf2_attr (die, DW_AT_GNU_macros, cu);
7941 if (attr && cu->line_header)
7942 {
7943 if (dwarf2_attr (die, DW_AT_macro_info, cu))
7944 complaint (&symfile_complaints,
7945 _("CU refers to both DW_AT_GNU_macros and DW_AT_macro_info"));
7946
09262596 7947 dwarf_decode_macros (cu, DW_UNSND (attr), comp_dir, 1);
3019eac3
DE
7948 }
7949 else
7950 {
7951 attr = dwarf2_attr (die, DW_AT_macro_info, cu);
7952 if (attr && cu->line_header)
7953 {
7954 unsigned int macro_offset = DW_UNSND (attr);
7955
09262596 7956 dwarf_decode_macros (cu, macro_offset, comp_dir, 0);
3019eac3
DE
7957 }
7958 }
7959
7960 do_cleanups (back_to);
7961}
7962
f4dc4d17
DE
7963/* TU version of handle_DW_AT_stmt_list for read_type_unit_scope.
7964 Create the set of symtabs used by this TU, or if this TU is sharing
7965 symtabs with another TU and the symtabs have already been created
7966 then restore those symtabs in the line header.
7967 We don't need the pc/line-number mapping for type units. */
3019eac3
DE
7968
7969static void
f4dc4d17 7970setup_type_unit_groups (struct die_info *die, struct dwarf2_cu *cu)
3019eac3 7971{
f4dc4d17
DE
7972 struct objfile *objfile = dwarf2_per_objfile->objfile;
7973 struct dwarf2_per_cu_data *per_cu = cu->per_cu;
7974 struct type_unit_group *tu_group;
7975 int first_time;
7976 struct line_header *lh;
3019eac3 7977 struct attribute *attr;
f4dc4d17 7978 unsigned int i, line_offset;
3019eac3 7979
f4dc4d17 7980 gdb_assert (per_cu->is_debug_types);
3019eac3 7981
f4dc4d17 7982 attr = dwarf2_attr (die, DW_AT_stmt_list, cu);
3019eac3 7983
f4dc4d17
DE
7984 /* If we're using .gdb_index (includes -readnow) then
7985 per_cu->s.type_unit_group may not have been set up yet. */
7986 if (per_cu->s.type_unit_group == NULL)
094b34ac 7987 per_cu->s.type_unit_group = get_type_unit_group (cu, attr);
f4dc4d17
DE
7988 tu_group = per_cu->s.type_unit_group;
7989
7990 /* If we've already processed this stmt_list there's no real need to
7991 do it again, we could fake it and just recreate the part we need
7992 (file name,index -> symtab mapping). If data shows this optimization
7993 is useful we can do it then. */
7994 first_time = tu_group->primary_symtab == NULL;
7995
7996 /* We have to handle the case of both a missing DW_AT_stmt_list or bad
7997 debug info. */
7998 lh = NULL;
7999 if (attr != NULL)
3019eac3 8000 {
f4dc4d17
DE
8001 line_offset = DW_UNSND (attr);
8002 lh = dwarf_decode_line_header (line_offset, cu);
8003 }
8004 if (lh == NULL)
8005 {
8006 if (first_time)
8007 dwarf2_start_symtab (cu, "", NULL, 0);
8008 else
8009 {
8010 gdb_assert (tu_group->symtabs == NULL);
8011 restart_symtab (0);
8012 }
8013 /* Note: The primary symtab will get allocated at the end. */
8014 return;
3019eac3
DE
8015 }
8016
f4dc4d17
DE
8017 cu->line_header = lh;
8018 make_cleanup (free_cu_line_header, cu);
3019eac3 8019
f4dc4d17
DE
8020 if (first_time)
8021 {
8022 dwarf2_start_symtab (cu, "", NULL, 0);
3019eac3 8023
f4dc4d17
DE
8024 tu_group->num_symtabs = lh->num_file_names;
8025 tu_group->symtabs = XNEWVEC (struct symtab *, lh->num_file_names);
3019eac3 8026
f4dc4d17
DE
8027 for (i = 0; i < lh->num_file_names; ++i)
8028 {
8029 char *dir = NULL;
8030 struct file_entry *fe = &lh->file_names[i];
3019eac3 8031
f4dc4d17
DE
8032 if (fe->dir_index)
8033 dir = lh->include_dirs[fe->dir_index - 1];
8034 dwarf2_start_subfile (fe->name, dir, NULL);
3019eac3 8035
f4dc4d17
DE
8036 /* Note: We don't have to watch for the main subfile here, type units
8037 don't have DW_AT_name. */
3019eac3 8038
f4dc4d17
DE
8039 if (current_subfile->symtab == NULL)
8040 {
8041 /* NOTE: start_subfile will recognize when it's been passed
8042 a file it has already seen. So we can't assume there's a
8043 simple mapping from lh->file_names to subfiles,
8044 lh->file_names may contain dups. */
8045 current_subfile->symtab = allocate_symtab (current_subfile->name,
8046 objfile);
8047 }
8048
8049 fe->symtab = current_subfile->symtab;
8050 tu_group->symtabs[i] = fe->symtab;
8051 }
8052 }
8053 else
3019eac3 8054 {
f4dc4d17
DE
8055 restart_symtab (0);
8056
8057 for (i = 0; i < lh->num_file_names; ++i)
8058 {
8059 struct file_entry *fe = &lh->file_names[i];
8060
8061 fe->symtab = tu_group->symtabs[i];
8062 }
3019eac3
DE
8063 }
8064
f4dc4d17
DE
8065 /* The main symtab is allocated last. Type units don't have DW_AT_name
8066 so they don't have a "real" (so to speak) symtab anyway.
8067 There is later code that will assign the main symtab to all symbols
8068 that don't have one. We need to handle the case of a symbol with a
8069 missing symtab (DW_AT_decl_file) anyway. */
8070}
3019eac3 8071
f4dc4d17
DE
8072/* Process DW_TAG_type_unit.
8073 For TUs we want to skip the first top level sibling if it's not the
8074 actual type being defined by this TU. In this case the first top
8075 level sibling is there to provide context only. */
3019eac3 8076
f4dc4d17
DE
8077static void
8078read_type_unit_scope (struct die_info *die, struct dwarf2_cu *cu)
8079{
8080 struct die_info *child_die;
3019eac3 8081
f4dc4d17
DE
8082 prepare_one_comp_unit (cu, die, language_minimal);
8083
8084 /* Initialize (or reinitialize) the machinery for building symtabs.
8085 We do this before processing child DIEs, so that the line header table
8086 is available for DW_AT_decl_file. */
8087 setup_type_unit_groups (die, cu);
8088
8089 if (die->child != NULL)
8090 {
8091 child_die = die->child;
8092 while (child_die && child_die->tag)
8093 {
8094 process_die (child_die, cu);
8095 child_die = sibling_die (child_die);
8096 }
8097 }
3019eac3
DE
8098}
8099\f
80626a55
DE
8100/* DWO/DWP files.
8101
8102 http://gcc.gnu.org/wiki/DebugFission
8103 http://gcc.gnu.org/wiki/DebugFissionDWP
8104
8105 To simplify handling of both DWO files ("object" files with the DWARF info)
8106 and DWP files (a file with the DWOs packaged up into one file), we treat
8107 DWP files as having a collection of virtual DWO files. */
3019eac3
DE
8108
8109static hashval_t
8110hash_dwo_file (const void *item)
8111{
8112 const struct dwo_file *dwo_file = item;
8113
80626a55 8114 return htab_hash_string (dwo_file->name);
3019eac3
DE
8115}
8116
8117static int
8118eq_dwo_file (const void *item_lhs, const void *item_rhs)
8119{
8120 const struct dwo_file *lhs = item_lhs;
8121 const struct dwo_file *rhs = item_rhs;
8122
80626a55 8123 return strcmp (lhs->name, rhs->name) == 0;
3019eac3
DE
8124}
8125
8126/* Allocate a hash table for DWO files. */
8127
8128static htab_t
8129allocate_dwo_file_hash_table (void)
8130{
8131 struct objfile *objfile = dwarf2_per_objfile->objfile;
8132
8133 return htab_create_alloc_ex (41,
8134 hash_dwo_file,
8135 eq_dwo_file,
8136 NULL,
8137 &objfile->objfile_obstack,
8138 hashtab_obstack_allocate,
8139 dummy_obstack_deallocate);
8140}
8141
80626a55
DE
8142/* Lookup DWO file DWO_NAME. */
8143
8144static void **
8145lookup_dwo_file_slot (const char *dwo_name)
8146{
8147 struct dwo_file find_entry;
8148 void **slot;
8149
8150 if (dwarf2_per_objfile->dwo_files == NULL)
8151 dwarf2_per_objfile->dwo_files = allocate_dwo_file_hash_table ();
8152
8153 memset (&find_entry, 0, sizeof (find_entry));
8154 find_entry.name = dwo_name;
8155 slot = htab_find_slot (dwarf2_per_objfile->dwo_files, &find_entry, INSERT);
8156
8157 return slot;
8158}
8159
3019eac3
DE
8160static hashval_t
8161hash_dwo_unit (const void *item)
8162{
8163 const struct dwo_unit *dwo_unit = item;
8164
8165 /* This drops the top 32 bits of the id, but is ok for a hash. */
8166 return dwo_unit->signature;
8167}
8168
8169static int
8170eq_dwo_unit (const void *item_lhs, const void *item_rhs)
8171{
8172 const struct dwo_unit *lhs = item_lhs;
8173 const struct dwo_unit *rhs = item_rhs;
8174
8175 /* The signature is assumed to be unique within the DWO file.
8176 So while object file CU dwo_id's always have the value zero,
8177 that's OK, assuming each object file DWO file has only one CU,
8178 and that's the rule for now. */
8179 return lhs->signature == rhs->signature;
8180}
8181
8182/* Allocate a hash table for DWO CUs,TUs.
8183 There is one of these tables for each of CUs,TUs for each DWO file. */
8184
8185static htab_t
8186allocate_dwo_unit_table (struct objfile *objfile)
8187{
8188 /* Start out with a pretty small number.
8189 Generally DWO files contain only one CU and maybe some TUs. */
8190 return htab_create_alloc_ex (3,
8191 hash_dwo_unit,
8192 eq_dwo_unit,
8193 NULL,
8194 &objfile->objfile_obstack,
8195 hashtab_obstack_allocate,
8196 dummy_obstack_deallocate);
8197}
8198
80626a55 8199/* Structure used to pass data to create_dwo_debug_info_hash_table_reader. */
3019eac3
DE
8200
8201struct create_dwo_info_table_data
8202{
8203 struct dwo_file *dwo_file;
8204 htab_t cu_htab;
8205};
8206
80626a55 8207/* die_reader_func for create_dwo_debug_info_hash_table. */
3019eac3
DE
8208
8209static void
80626a55
DE
8210create_dwo_debug_info_hash_table_reader (const struct die_reader_specs *reader,
8211 gdb_byte *info_ptr,
8212 struct die_info *comp_unit_die,
8213 int has_children,
8214 void *datap)
3019eac3
DE
8215{
8216 struct dwarf2_cu *cu = reader->cu;
8217 struct objfile *objfile = dwarf2_per_objfile->objfile;
8218 sect_offset offset = cu->per_cu->offset;
8219 struct dwarf2_section_info *section = cu->per_cu->info_or_types_section;
8220 struct create_dwo_info_table_data *data = datap;
8221 struct dwo_file *dwo_file = data->dwo_file;
8222 htab_t cu_htab = data->cu_htab;
8223 void **slot;
8224 struct attribute *attr;
8225 struct dwo_unit *dwo_unit;
8226
8227 attr = dwarf2_attr (comp_unit_die, DW_AT_GNU_dwo_id, cu);
8228 if (attr == NULL)
8229 {
8230 error (_("Dwarf Error: debug entry at offset 0x%x is missing"
8231 " its dwo_id [in module %s]"),
80626a55 8232 offset.sect_off, dwo_file->name);
3019eac3
DE
8233 return;
8234 }
8235
8236 dwo_unit = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct dwo_unit);
8237 dwo_unit->dwo_file = dwo_file;
8238 dwo_unit->signature = DW_UNSND (attr);
8239 dwo_unit->info_or_types_section = section;
8240 dwo_unit->offset = offset;
8241 dwo_unit->length = cu->per_cu->length;
8242
8243 slot = htab_find_slot (cu_htab, dwo_unit, INSERT);
8244 gdb_assert (slot != NULL);
8245 if (*slot != NULL)
8246 {
8247 const struct dwo_unit *dup_dwo_unit = *slot;
8248
8249 complaint (&symfile_complaints,
8250 _("debug entry at offset 0x%x is duplicate to the entry at"
8251 " offset 0x%x, dwo_id 0x%s [in module %s]"),
8252 offset.sect_off, dup_dwo_unit->offset.sect_off,
8253 phex (dwo_unit->signature, sizeof (dwo_unit->signature)),
80626a55 8254 dwo_file->name);
3019eac3
DE
8255 }
8256 else
8257 *slot = dwo_unit;
8258
09406207 8259 if (dwarf2_read_debug)
3019eac3
DE
8260 fprintf_unfiltered (gdb_stdlog, " offset 0x%x, dwo_id 0x%s\n",
8261 offset.sect_off,
8262 phex (dwo_unit->signature,
8263 sizeof (dwo_unit->signature)));
8264}
8265
80626a55
DE
8266/* Create a hash table to map DWO IDs to their CU entry in
8267 .debug_info.dwo in DWO_FILE.
8268 Note: This function processes DWO files only, not DWP files. */
3019eac3
DE
8269
8270static htab_t
80626a55 8271create_dwo_debug_info_hash_table (struct dwo_file *dwo_file)
3019eac3
DE
8272{
8273 struct objfile *objfile = dwarf2_per_objfile->objfile;
8274 struct dwarf2_section_info *section = &dwo_file->sections.info;
8275 bfd *abfd;
8276 htab_t cu_htab;
8277 gdb_byte *info_ptr, *end_ptr;
8278 struct create_dwo_info_table_data create_dwo_info_table_data;
8279
8280 dwarf2_read_section (objfile, section);
8281 info_ptr = section->buffer;
8282
8283 if (info_ptr == NULL)
8284 return NULL;
8285
8286 /* We can't set abfd until now because the section may be empty or
8287 not present, in which case section->asection will be NULL. */
8288 abfd = section->asection->owner;
8289
09406207 8290 if (dwarf2_read_debug)
3019eac3
DE
8291 fprintf_unfiltered (gdb_stdlog, "Reading .debug_info.dwo for %s:\n",
8292 bfd_get_filename (abfd));
8293
8294 cu_htab = allocate_dwo_unit_table (objfile);
8295
8296 create_dwo_info_table_data.dwo_file = dwo_file;
8297 create_dwo_info_table_data.cu_htab = cu_htab;
8298
8299 end_ptr = info_ptr + section->size;
8300 while (info_ptr < end_ptr)
8301 {
8302 struct dwarf2_per_cu_data per_cu;
8303
8304 memset (&per_cu, 0, sizeof (per_cu));
8305 per_cu.objfile = objfile;
8306 per_cu.is_debug_types = 0;
8307 per_cu.offset.sect_off = info_ptr - section->buffer;
8308 per_cu.info_or_types_section = section;
8309
8310 init_cutu_and_read_dies_no_follow (&per_cu,
8311 &dwo_file->sections.abbrev,
8312 dwo_file,
80626a55 8313 create_dwo_debug_info_hash_table_reader,
3019eac3
DE
8314 &create_dwo_info_table_data);
8315
8316 info_ptr += per_cu.length;
8317 }
8318
8319 return cu_htab;
8320}
8321
80626a55
DE
8322/* DWP file .debug_{cu,tu}_index section format:
8323 [ref: http://gcc.gnu.org/wiki/DebugFissionDWP]
8324
8325 Both index sections have the same format, and serve to map a 64-bit
8326 signature to a set of section numbers. Each section begins with a header,
8327 followed by a hash table of 64-bit signatures, a parallel table of 32-bit
8328 indexes, and a pool of 32-bit section numbers. The index sections will be
8329 aligned at 8-byte boundaries in the file.
8330
8331 The index section header contains two unsigned 32-bit values (using the
8332 byte order of the application binary):
8333
8334 N, the number of compilation units or type units in the index
8335 M, the number of slots in the hash table
8336
8337 (We assume that N and M will not exceed 2^32 - 1.)
8338
8339 The size of the hash table, M, must be 2^k such that 2^k > 3*N/2.
8340
8341 The hash table begins at offset 8 in the section, and consists of an array
8342 of M 64-bit slots. Each slot contains a 64-bit signature (using the byte
8343 order of the application binary). Unused slots in the hash table are 0.
8344 (We rely on the extreme unlikeliness of a signature being exactly 0.)
8345
8346 The parallel table begins immediately after the hash table
8347 (at offset 8 + 8 * M from the beginning of the section), and consists of an
8348 array of 32-bit indexes (using the byte order of the application binary),
8349 corresponding 1-1 with slots in the hash table. Each entry in the parallel
8350 table contains a 32-bit index into the pool of section numbers. For unused
8351 hash table slots, the corresponding entry in the parallel table will be 0.
8352
8353 Given a 64-bit compilation unit signature or a type signature S, an entry
8354 in the hash table is located as follows:
8355
8356 1) Calculate a primary hash H = S & MASK(k), where MASK(k) is a mask with
8357 the low-order k bits all set to 1.
8358
8359 2) Calculate a secondary hash H' = (((S >> 32) & MASK(k)) | 1).
8360
8361 3) If the hash table entry at index H matches the signature, use that
8362 entry. If the hash table entry at index H is unused (all zeroes),
8363 terminate the search: the signature is not present in the table.
8364
8365 4) Let H = (H + H') modulo M. Repeat at Step 3.
8366
8367 Because M > N and H' and M are relatively prime, the search is guaranteed
8368 to stop at an unused slot or find the match.
8369
8370 The pool of section numbers begins immediately following the hash table
8371 (at offset 8 + 12 * M from the beginning of the section). The pool of
8372 section numbers consists of an array of 32-bit words (using the byte order
8373 of the application binary). Each item in the array is indexed starting
8374 from 0. The hash table entry provides the index of the first section
8375 number in the set. Additional section numbers in the set follow, and the
8376 set is terminated by a 0 entry (section number 0 is not used in ELF).
8377
8378 In each set of section numbers, the .debug_info.dwo or .debug_types.dwo
8379 section must be the first entry in the set, and the .debug_abbrev.dwo must
8380 be the second entry. Other members of the set may follow in any order. */
8381
8382/* Create a hash table to map DWO IDs to their CU/TU entry in
8383 .debug_{info,types}.dwo in DWP_FILE.
8384 Returns NULL if there isn't one.
8385 Note: This function processes DWP files only, not DWO files. */
8386
8387static struct dwp_hash_table *
8388create_dwp_hash_table (struct dwp_file *dwp_file, int is_debug_types)
8389{
8390 struct objfile *objfile = dwarf2_per_objfile->objfile;
8391 bfd *dbfd = dwp_file->dbfd;
8392 char *index_ptr, *index_end;
8393 struct dwarf2_section_info *index;
8394 uint32_t version, nr_units, nr_slots;
8395 struct dwp_hash_table *htab;
8396
8397 if (is_debug_types)
8398 index = &dwp_file->sections.tu_index;
8399 else
8400 index = &dwp_file->sections.cu_index;
8401
8402 if (dwarf2_section_empty_p (index))
8403 return NULL;
8404 dwarf2_read_section (objfile, index);
8405
8406 index_ptr = index->buffer;
8407 index_end = index_ptr + index->size;
8408
8409 version = read_4_bytes (dbfd, index_ptr);
8410 index_ptr += 8; /* Skip the unused word. */
8411 nr_units = read_4_bytes (dbfd, index_ptr);
8412 index_ptr += 4;
8413 nr_slots = read_4_bytes (dbfd, index_ptr);
8414 index_ptr += 4;
8415
8416 if (version != 1)
8417 {
8418 error (_("Dwarf Error: unsupported DWP file version (%u)"
8419 " [in module %s]"),
8420 version, dwp_file->name);
8421 }
8422 if (nr_slots != (nr_slots & -nr_slots))
8423 {
8424 error (_("Dwarf Error: number of slots in DWP hash table (%u)"
8425 " is not power of 2 [in module %s]"),
8426 nr_slots, dwp_file->name);
8427 }
8428
8429 htab = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct dwp_hash_table);
8430 htab->nr_units = nr_units;
8431 htab->nr_slots = nr_slots;
8432 htab->hash_table = index_ptr;
8433 htab->unit_table = htab->hash_table + sizeof (uint64_t) * nr_slots;
8434 htab->section_pool = htab->unit_table + sizeof (uint32_t) * nr_slots;
8435
8436 return htab;
8437}
8438
8439/* Update SECTIONS with the data from SECTP.
8440
8441 This function is like the other "locate" section routines that are
8442 passed to bfd_map_over_sections, but in this context the sections to
8443 read comes from the DWP hash table, not the full ELF section table.
8444
8445 The result is non-zero for success, or zero if an error was found. */
8446
8447static int
8448locate_virtual_dwo_sections (asection *sectp,
8449 struct virtual_dwo_sections *sections)
8450{
8451 const struct dwop_section_names *names = &dwop_section_names;
8452
8453 if (section_is_p (sectp->name, &names->abbrev_dwo))
8454 {
8455 /* There can be only one. */
8456 if (sections->abbrev.asection != NULL)
8457 return 0;
8458 sections->abbrev.asection = sectp;
8459 sections->abbrev.size = bfd_get_section_size (sectp);
8460 }
8461 else if (section_is_p (sectp->name, &names->info_dwo)
8462 || section_is_p (sectp->name, &names->types_dwo))
8463 {
8464 /* There can be only one. */
8465 if (sections->info_or_types.asection != NULL)
8466 return 0;
8467 sections->info_or_types.asection = sectp;
8468 sections->info_or_types.size = bfd_get_section_size (sectp);
8469 }
8470 else if (section_is_p (sectp->name, &names->line_dwo))
8471 {
8472 /* There can be only one. */
8473 if (sections->line.asection != NULL)
8474 return 0;
8475 sections->line.asection = sectp;
8476 sections->line.size = bfd_get_section_size (sectp);
8477 }
8478 else if (section_is_p (sectp->name, &names->loc_dwo))
8479 {
8480 /* There can be only one. */
8481 if (sections->loc.asection != NULL)
8482 return 0;
8483 sections->loc.asection = sectp;
8484 sections->loc.size = bfd_get_section_size (sectp);
8485 }
8486 else if (section_is_p (sectp->name, &names->macinfo_dwo))
8487 {
8488 /* There can be only one. */
8489 if (sections->macinfo.asection != NULL)
8490 return 0;
8491 sections->macinfo.asection = sectp;
8492 sections->macinfo.size = bfd_get_section_size (sectp);
8493 }
8494 else if (section_is_p (sectp->name, &names->macro_dwo))
8495 {
8496 /* There can be only one. */
8497 if (sections->macro.asection != NULL)
8498 return 0;
8499 sections->macro.asection = sectp;
8500 sections->macro.size = bfd_get_section_size (sectp);
8501 }
8502 else if (section_is_p (sectp->name, &names->str_offsets_dwo))
8503 {
8504 /* There can be only one. */
8505 if (sections->str_offsets.asection != NULL)
8506 return 0;
8507 sections->str_offsets.asection = sectp;
8508 sections->str_offsets.size = bfd_get_section_size (sectp);
8509 }
8510 else
8511 {
8512 /* No other kind of section is valid. */
8513 return 0;
8514 }
8515
8516 return 1;
8517}
8518
8519/* Create a dwo_unit object for the DWO with signature SIGNATURE.
8520 HTAB is the hash table from the DWP file.
8521 SECTION_INDEX is the index of the DWO in HTAB. */
8522
8523static struct dwo_unit *
8524create_dwo_in_dwp (struct dwp_file *dwp_file,
8525 const struct dwp_hash_table *htab,
8526 uint32_t section_index,
8527 ULONGEST signature, int is_debug_types)
8528{
8529 struct objfile *objfile = dwarf2_per_objfile->objfile;
8530 bfd *dbfd = dwp_file->dbfd;
8531 const char *kind = is_debug_types ? "TU" : "CU";
8532 struct dwo_file *dwo_file;
8533 struct dwo_unit *dwo_unit;
8534 struct virtual_dwo_sections sections;
8535 void **dwo_file_slot;
8536 char *virtual_dwo_name;
8537 struct dwarf2_section_info *cutu;
8538 struct cleanup *cleanups;
8539 int i;
8540
8541 if (dwarf2_read_debug)
8542 {
8543 fprintf_unfiltered (gdb_stdlog, "Reading %s %u/0x%s in DWP file: %s\n",
8544 kind,
8545 section_index, phex (signature, sizeof (signature)),
8546 dwp_file->name);
8547 }
8548
8549 /* Fetch the sections of this DWO.
8550 Put a limit on the number of sections we look for so that bad data
8551 doesn't cause us to loop forever. */
8552
8553#define MAX_NR_DWO_SECTIONS \
8554 (1 /* .debug_info or .debug_types */ \
8555 + 1 /* .debug_abbrev */ \
8556 + 1 /* .debug_line */ \
8557 + 1 /* .debug_loc */ \
8558 + 1 /* .debug_str_offsets */ \
8559 + 1 /* .debug_macro */ \
8560 + 1 /* .debug_macinfo */ \
8561 + 1 /* trailing zero */)
8562
8563 memset (&sections, 0, sizeof (sections));
8564 cleanups = make_cleanup (null_cleanup, 0);
8565
8566 for (i = 0; i < MAX_NR_DWO_SECTIONS; ++i)
8567 {
8568 asection *sectp;
8569 uint32_t section_nr =
8570 read_4_bytes (dbfd,
8571 htab->section_pool
8572 + (section_index + i) * sizeof (uint32_t));
8573
8574 if (section_nr == 0)
8575 break;
8576 if (section_nr >= dwp_file->num_sections)
8577 {
8578 error (_("Dwarf Error: bad DWP hash table, section number too large"
8579 " [in module %s]"),
8580 dwp_file->name);
8581 }
8582
8583 sectp = dwp_file->elf_sections[section_nr];
8584 if (! locate_virtual_dwo_sections (sectp, &sections))
8585 {
8586 error (_("Dwarf Error: bad DWP hash table, invalid section found"
8587 " [in module %s]"),
8588 dwp_file->name);
8589 }
8590 }
8591
8592 if (i < 2
8593 || sections.info_or_types.asection == NULL
8594 || sections.abbrev.asection == NULL)
8595 {
8596 error (_("Dwarf Error: bad DWP hash table, missing DWO sections"
8597 " [in module %s]"),
8598 dwp_file->name);
8599 }
8600 if (i == MAX_NR_DWO_SECTIONS)
8601 {
8602 error (_("Dwarf Error: bad DWP hash table, too many DWO sections"
8603 " [in module %s]"),
8604 dwp_file->name);
8605 }
8606
8607 /* It's easier for the rest of the code if we fake a struct dwo_file and
8608 have dwo_unit "live" in that. At least for now.
8609
8610 The DWP file can be made up of a random collection of CUs and TUs.
8611 However, for each CU + set of TUs that came from the same original
8612 DWO file, we want combine them back into a virtual DWO file to save space
8613 (fewer struct dwo_file objects to allocated). Remember that for really
8614 large apps there can be on the order of 8K CUs and 200K TUs, or more. */
8615
2792b94d
PM
8616 virtual_dwo_name =
8617 xstrprintf ("virtual-dwo/%d-%d-%d-%d",
8618 sections.abbrev.asection ? sections.abbrev.asection->id : 0,
8619 sections.line.asection ? sections.line.asection->id : 0,
8620 sections.loc.asection ? sections.loc.asection->id : 0,
8621 (sections.str_offsets.asection
8622 ? sections.str_offsets.asection->id
8623 : 0));
80626a55
DE
8624 make_cleanup (xfree, virtual_dwo_name);
8625 /* Can we use an existing virtual DWO file? */
8626 dwo_file_slot = lookup_dwo_file_slot (virtual_dwo_name);
8627 /* Create one if necessary. */
8628 if (*dwo_file_slot == NULL)
8629 {
8630 if (dwarf2_read_debug)
8631 {
8632 fprintf_unfiltered (gdb_stdlog, "Creating virtual DWO: %s\n",
8633 virtual_dwo_name);
8634 }
8635 dwo_file = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct dwo_file);
8636 dwo_file->name = obstack_copy0 (&objfile->objfile_obstack,
8637 virtual_dwo_name,
8638 strlen (virtual_dwo_name));
8639 dwo_file->sections.abbrev = sections.abbrev;
8640 dwo_file->sections.line = sections.line;
8641 dwo_file->sections.loc = sections.loc;
8642 dwo_file->sections.macinfo = sections.macinfo;
8643 dwo_file->sections.macro = sections.macro;
8644 dwo_file->sections.str_offsets = sections.str_offsets;
8645 /* The "str" section is global to the entire DWP file. */
8646 dwo_file->sections.str = dwp_file->sections.str;
8647 /* The info or types section is assigned later to dwo_unit,
8648 there's no need to record it in dwo_file.
8649 Also, we can't simply record type sections in dwo_file because
8650 we record a pointer into the vector in dwo_unit. As we collect more
8651 types we'll grow the vector and eventually have to reallocate space
8652 for it, invalidating all the pointers into the current copy. */
8653 *dwo_file_slot = dwo_file;
8654 }
8655 else
8656 {
8657 if (dwarf2_read_debug)
8658 {
8659 fprintf_unfiltered (gdb_stdlog, "Using existing virtual DWO: %s\n",
8660 virtual_dwo_name);
8661 }
8662 dwo_file = *dwo_file_slot;
8663 }
8664 do_cleanups (cleanups);
8665
8666 dwo_unit = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct dwo_unit);
8667 dwo_unit->dwo_file = dwo_file;
8668 dwo_unit->signature = signature;
8669 dwo_unit->info_or_types_section =
8670 obstack_alloc (&objfile->objfile_obstack,
8671 sizeof (struct dwarf2_section_info));
8672 *dwo_unit->info_or_types_section = sections.info_or_types;
8673 /* offset, length, type_offset_in_tu are set later. */
8674
8675 return dwo_unit;
8676}
8677
8678/* Lookup the DWO with SIGNATURE in DWP_FILE. */
8679
8680static struct dwo_unit *
8681lookup_dwo_in_dwp (struct dwp_file *dwp_file,
8682 const struct dwp_hash_table *htab,
8683 ULONGEST signature, int is_debug_types)
8684{
8685 bfd *dbfd = dwp_file->dbfd;
8686 uint32_t mask = htab->nr_slots - 1;
8687 uint32_t hash = signature & mask;
8688 uint32_t hash2 = ((signature >> 32) & mask) | 1;
8689 unsigned int i;
8690 void **slot;
8691 struct dwo_unit find_dwo_cu, *dwo_cu;
8692
8693 memset (&find_dwo_cu, 0, sizeof (find_dwo_cu));
8694 find_dwo_cu.signature = signature;
8695 slot = htab_find_slot (dwp_file->loaded_cutus, &find_dwo_cu, INSERT);
8696
8697 if (*slot != NULL)
8698 return *slot;
8699
8700 /* Use a for loop so that we don't loop forever on bad debug info. */
8701 for (i = 0; i < htab->nr_slots; ++i)
8702 {
8703 ULONGEST signature_in_table;
8704
8705 signature_in_table =
8706 read_8_bytes (dbfd, htab->hash_table + hash * sizeof (uint64_t));
8707 if (signature_in_table == signature)
8708 {
8709 uint32_t section_index =
8710 read_4_bytes (dbfd, htab->unit_table + hash * sizeof (uint32_t));
8711
8712 *slot = create_dwo_in_dwp (dwp_file, htab, section_index,
8713 signature, is_debug_types);
8714 return *slot;
8715 }
8716 if (signature_in_table == 0)
8717 return NULL;
8718 hash = (hash + hash2) & mask;
8719 }
8720
8721 error (_("Dwarf Error: bad DWP hash table, lookup didn't terminate"
8722 " [in module %s]"),
8723 dwp_file->name);
8724}
8725
8726/* Subroutine of open_dwop_file to simplify it.
3019eac3
DE
8727 Open the file specified by FILE_NAME and hand it off to BFD for
8728 preliminary analysis. Return a newly initialized bfd *, which
8729 includes a canonicalized copy of FILE_NAME.
80626a55 8730 If IS_DWP is TRUE, we're opening a DWP file, otherwise a DWO file.
3019eac3
DE
8731 In case of trouble, return NULL.
8732 NOTE: This function is derived from symfile_bfd_open. */
8733
8734static bfd *
80626a55 8735try_open_dwop_file (const char *file_name, int is_dwp)
3019eac3
DE
8736{
8737 bfd *sym_bfd;
80626a55 8738 int desc, flags;
3019eac3 8739 char *absolute_name;
3019eac3 8740
80626a55
DE
8741 flags = OPF_TRY_CWD_FIRST;
8742 if (is_dwp)
8743 flags |= OPF_SEARCH_IN_PATH;
8744 desc = openp (debug_file_directory, flags, file_name,
3019eac3
DE
8745 O_RDONLY | O_BINARY, &absolute_name);
8746 if (desc < 0)
8747 return NULL;
8748
bb397797 8749 sym_bfd = gdb_bfd_open (absolute_name, gnutarget, desc);
3019eac3
DE
8750 if (!sym_bfd)
8751 {
3019eac3
DE
8752 xfree (absolute_name);
8753 return NULL;
8754 }
a4453b7e 8755 xfree (absolute_name);
3019eac3
DE
8756 bfd_set_cacheable (sym_bfd, 1);
8757
8758 if (!bfd_check_format (sym_bfd, bfd_object))
8759 {
cbb099e8 8760 gdb_bfd_unref (sym_bfd); /* This also closes desc. */
3019eac3
DE
8761 return NULL;
8762 }
8763
3019eac3
DE
8764 return sym_bfd;
8765}
8766
80626a55 8767/* Try to open DWO/DWP file FILE_NAME.
3019eac3 8768 COMP_DIR is the DW_AT_comp_dir attribute.
80626a55 8769 If IS_DWP is TRUE, we're opening a DWP file, otherwise a DWO file.
3019eac3
DE
8770 The result is the bfd handle of the file.
8771 If there is a problem finding or opening the file, return NULL.
8772 Upon success, the canonicalized path of the file is stored in the bfd,
8773 same as symfile_bfd_open. */
8774
8775static bfd *
80626a55 8776open_dwop_file (const char *file_name, const char *comp_dir, int is_dwp)
3019eac3
DE
8777{
8778 bfd *abfd;
3019eac3 8779
80626a55
DE
8780 if (IS_ABSOLUTE_PATH (file_name))
8781 return try_open_dwop_file (file_name, is_dwp);
3019eac3
DE
8782
8783 /* Before trying the search path, try DWO_NAME in COMP_DIR. */
8784
8785 if (comp_dir != NULL)
8786 {
80626a55 8787 char *path_to_try = concat (comp_dir, SLASH_STRING, file_name, NULL);
3019eac3
DE
8788
8789 /* NOTE: If comp_dir is a relative path, this will also try the
8790 search path, which seems useful. */
80626a55 8791 abfd = try_open_dwop_file (path_to_try, is_dwp);
3019eac3
DE
8792 xfree (path_to_try);
8793 if (abfd != NULL)
8794 return abfd;
8795 }
8796
8797 /* That didn't work, try debug-file-directory, which, despite its name,
8798 is a list of paths. */
8799
8800 if (*debug_file_directory == '\0')
8801 return NULL;
8802
80626a55 8803 return try_open_dwop_file (file_name, is_dwp);
3019eac3
DE
8804}
8805
80626a55
DE
8806/* This function is mapped across the sections and remembers the offset and
8807 size of each of the DWO debugging sections we are interested in. */
8808
8809static void
8810dwarf2_locate_dwo_sections (bfd *abfd, asection *sectp, void *dwo_sections_ptr)
8811{
8812 struct dwo_sections *dwo_sections = dwo_sections_ptr;
8813 const struct dwop_section_names *names = &dwop_section_names;
8814
8815 if (section_is_p (sectp->name, &names->abbrev_dwo))
8816 {
8817 dwo_sections->abbrev.asection = sectp;
8818 dwo_sections->abbrev.size = bfd_get_section_size (sectp);
8819 }
8820 else if (section_is_p (sectp->name, &names->info_dwo))
8821 {
8822 dwo_sections->info.asection = sectp;
8823 dwo_sections->info.size = bfd_get_section_size (sectp);
8824 }
8825 else if (section_is_p (sectp->name, &names->line_dwo))
8826 {
8827 dwo_sections->line.asection = sectp;
8828 dwo_sections->line.size = bfd_get_section_size (sectp);
8829 }
8830 else if (section_is_p (sectp->name, &names->loc_dwo))
8831 {
8832 dwo_sections->loc.asection = sectp;
8833 dwo_sections->loc.size = bfd_get_section_size (sectp);
8834 }
8835 else if (section_is_p (sectp->name, &names->macinfo_dwo))
8836 {
8837 dwo_sections->macinfo.asection = sectp;
8838 dwo_sections->macinfo.size = bfd_get_section_size (sectp);
8839 }
8840 else if (section_is_p (sectp->name, &names->macro_dwo))
8841 {
8842 dwo_sections->macro.asection = sectp;
8843 dwo_sections->macro.size = bfd_get_section_size (sectp);
8844 }
8845 else if (section_is_p (sectp->name, &names->str_dwo))
8846 {
8847 dwo_sections->str.asection = sectp;
8848 dwo_sections->str.size = bfd_get_section_size (sectp);
8849 }
8850 else if (section_is_p (sectp->name, &names->str_offsets_dwo))
8851 {
8852 dwo_sections->str_offsets.asection = sectp;
8853 dwo_sections->str_offsets.size = bfd_get_section_size (sectp);
8854 }
8855 else if (section_is_p (sectp->name, &names->types_dwo))
8856 {
8857 struct dwarf2_section_info type_section;
8858
8859 memset (&type_section, 0, sizeof (type_section));
8860 type_section.asection = sectp;
8861 type_section.size = bfd_get_section_size (sectp);
8862 VEC_safe_push (dwarf2_section_info_def, dwo_sections->types,
8863 &type_section);
8864 }
8865}
8866
8867/* Initialize the use of the DWO file specified by DWO_NAME.
8868 The result is NULL if DWO_NAME can't be found. */
3019eac3
DE
8869
8870static struct dwo_file *
80626a55 8871open_and_init_dwo_file (const char *dwo_name, const char *comp_dir)
3019eac3
DE
8872{
8873 struct objfile *objfile = dwarf2_per_objfile->objfile;
80626a55
DE
8874 struct dwo_file *dwo_file;
8875 bfd *dbfd;
3019eac3
DE
8876 struct cleanup *cleanups;
8877
80626a55
DE
8878 dbfd = open_dwop_file (dwo_name, comp_dir, 0);
8879 if (dbfd == NULL)
8880 {
8881 if (dwarf2_read_debug)
8882 fprintf_unfiltered (gdb_stdlog, "DWO file not found: %s\n", dwo_name);
8883 return NULL;
8884 }
8885 dwo_file = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct dwo_file);
8886 dwo_file->name = obstack_copy0 (&objfile->objfile_obstack,
8887 dwo_name, strlen (dwo_name));
8888 dwo_file->dbfd = dbfd;
3019eac3
DE
8889
8890 cleanups = make_cleanup (free_dwo_file_cleanup, dwo_file);
8891
80626a55 8892 bfd_map_over_sections (dbfd, dwarf2_locate_dwo_sections, &dwo_file->sections);
3019eac3 8893
80626a55 8894 dwo_file->cus = create_dwo_debug_info_hash_table (dwo_file);
3019eac3
DE
8895
8896 dwo_file->tus = create_debug_types_hash_table (dwo_file,
8897 dwo_file->sections.types);
8898
8899 discard_cleanups (cleanups);
8900
80626a55
DE
8901 if (dwarf2_read_debug)
8902 fprintf_unfiltered (gdb_stdlog, "DWO file found: %s\n", dwo_name);
8903
3019eac3
DE
8904 return dwo_file;
8905}
8906
80626a55
DE
8907/* This function is mapped across the sections and remembers the offset and
8908 size of each of the DWP debugging sections we are interested in. */
3019eac3 8909
80626a55
DE
8910static void
8911dwarf2_locate_dwp_sections (bfd *abfd, asection *sectp, void *dwp_file_ptr)
3019eac3 8912{
80626a55
DE
8913 struct dwp_file *dwp_file = dwp_file_ptr;
8914 const struct dwop_section_names *names = &dwop_section_names;
8915 unsigned int elf_section_nr = elf_section_data (sectp)->this_idx;
3019eac3 8916
80626a55
DE
8917 /* Record the ELF section number for later lookup: this is what the
8918 .debug_cu_index,.debug_tu_index tables use. */
8919 gdb_assert (elf_section_nr < dwp_file->num_sections);
8920 dwp_file->elf_sections[elf_section_nr] = sectp;
3019eac3 8921
80626a55
DE
8922 /* Look for specific sections that we need. */
8923 if (section_is_p (sectp->name, &names->str_dwo))
8924 {
8925 dwp_file->sections.str.asection = sectp;
8926 dwp_file->sections.str.size = bfd_get_section_size (sectp);
8927 }
8928 else if (section_is_p (sectp->name, &names->cu_index))
8929 {
8930 dwp_file->sections.cu_index.asection = sectp;
8931 dwp_file->sections.cu_index.size = bfd_get_section_size (sectp);
8932 }
8933 else if (section_is_p (sectp->name, &names->tu_index))
8934 {
8935 dwp_file->sections.tu_index.asection = sectp;
8936 dwp_file->sections.tu_index.size = bfd_get_section_size (sectp);
8937 }
8938}
3019eac3 8939
80626a55 8940/* Hash function for dwp_file loaded CUs/TUs. */
3019eac3 8941
80626a55
DE
8942static hashval_t
8943hash_dwp_loaded_cutus (const void *item)
8944{
8945 const struct dwo_unit *dwo_unit = item;
3019eac3 8946
80626a55
DE
8947 /* This drops the top 32 bits of the signature, but is ok for a hash. */
8948 return dwo_unit->signature;
3019eac3
DE
8949}
8950
80626a55 8951/* Equality function for dwp_file loaded CUs/TUs. */
3019eac3 8952
80626a55
DE
8953static int
8954eq_dwp_loaded_cutus (const void *a, const void *b)
3019eac3 8955{
80626a55
DE
8956 const struct dwo_unit *dua = a;
8957 const struct dwo_unit *dub = b;
3019eac3 8958
80626a55
DE
8959 return dua->signature == dub->signature;
8960}
3019eac3 8961
80626a55 8962/* Allocate a hash table for dwp_file loaded CUs/TUs. */
3019eac3 8963
80626a55
DE
8964static htab_t
8965allocate_dwp_loaded_cutus_table (struct objfile *objfile)
8966{
8967 return htab_create_alloc_ex (3,
8968 hash_dwp_loaded_cutus,
8969 eq_dwp_loaded_cutus,
8970 NULL,
8971 &objfile->objfile_obstack,
8972 hashtab_obstack_allocate,
8973 dummy_obstack_deallocate);
8974}
3019eac3 8975
80626a55
DE
8976/* Initialize the use of the DWP file for the current objfile.
8977 By convention the name of the DWP file is ${objfile}.dwp.
8978 The result is NULL if it can't be found. */
a766d390 8979
80626a55
DE
8980static struct dwp_file *
8981open_and_init_dwp_file (const char *comp_dir)
8982{
8983 struct objfile *objfile = dwarf2_per_objfile->objfile;
8984 struct dwp_file *dwp_file;
8985 char *dwp_name;
8986 bfd *dbfd;
8987 struct cleanup *cleanups;
8988
2792b94d 8989 dwp_name = xstrprintf ("%s.dwp", dwarf2_per_objfile->objfile->name);
80626a55
DE
8990 cleanups = make_cleanup (xfree, dwp_name);
8991
8992 dbfd = open_dwop_file (dwp_name, comp_dir, 1);
8993 if (dbfd == NULL)
8994 {
8995 if (dwarf2_read_debug)
8996 fprintf_unfiltered (gdb_stdlog, "DWP file not found: %s\n", dwp_name);
8997 do_cleanups (cleanups);
8998 return NULL;
3019eac3 8999 }
80626a55
DE
9000 dwp_file = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct dwp_file);
9001 dwp_file->name = obstack_copy0 (&objfile->objfile_obstack,
9002 dwp_name, strlen (dwp_name));
9003 dwp_file->dbfd = dbfd;
9004 do_cleanups (cleanups);
c906108c 9005
80626a55 9006 cleanups = make_cleanup (free_dwo_file_cleanup, dwp_file);
df8a16a1 9007
80626a55
DE
9008 /* +1: section 0 is unused */
9009 dwp_file->num_sections = bfd_count_sections (dbfd) + 1;
9010 dwp_file->elf_sections =
9011 OBSTACK_CALLOC (&objfile->objfile_obstack,
9012 dwp_file->num_sections, asection *);
9013
9014 bfd_map_over_sections (dbfd, dwarf2_locate_dwp_sections, dwp_file);
9015
9016 dwp_file->cus = create_dwp_hash_table (dwp_file, 0);
9017
9018 dwp_file->tus = create_dwp_hash_table (dwp_file, 1);
9019
9020 dwp_file->loaded_cutus = allocate_dwp_loaded_cutus_table (objfile);
9021
9022 discard_cleanups (cleanups);
9023
9024 if (dwarf2_read_debug)
9025 {
9026 fprintf_unfiltered (gdb_stdlog, "DWP file found: %s\n", dwp_file->name);
9027 fprintf_unfiltered (gdb_stdlog,
9028 " %u CUs, %u TUs\n",
9029 dwp_file->cus ? dwp_file->cus->nr_units : 0,
9030 dwp_file->tus ? dwp_file->tus->nr_units : 0);
9031 }
9032
9033 return dwp_file;
3019eac3 9034}
c906108c 9035
80626a55
DE
9036/* Subroutine of lookup_dwo_comp_unit, lookup_dwo_type_unit.
9037 Look up the CU/TU with signature SIGNATURE, either in DWO file DWO_NAME
9038 or in the DWP file for the objfile, referenced by THIS_UNIT.
3019eac3 9039 If non-NULL, comp_dir is the DW_AT_comp_dir attribute.
80626a55
DE
9040 IS_DEBUG_TYPES is non-zero if reading a TU, otherwise read a CU.
9041
9042 This is called, for example, when wanting to read a variable with a
9043 complex location. Therefore we don't want to do file i/o for every call.
9044 Therefore we don't want to look for a DWO file on every call.
9045 Therefore we first see if we've already seen SIGNATURE in a DWP file,
9046 then we check if we've already seen DWO_NAME, and only THEN do we check
9047 for a DWO file.
9048
1c658ad5 9049 The result is a pointer to the dwo_unit object or NULL if we didn't find it
80626a55 9050 (dwo_id mismatch or couldn't find the DWO/DWP file). */
debd256d 9051
3019eac3 9052static struct dwo_unit *
80626a55
DE
9053lookup_dwo_cutu (struct dwarf2_per_cu_data *this_unit,
9054 const char *dwo_name, const char *comp_dir,
9055 ULONGEST signature, int is_debug_types)
3019eac3
DE
9056{
9057 struct objfile *objfile = dwarf2_per_objfile->objfile;
80626a55
DE
9058 const char *kind = is_debug_types ? "TU" : "CU";
9059 void **dwo_file_slot;
3019eac3 9060 struct dwo_file *dwo_file;
80626a55 9061 struct dwp_file *dwp_file;
cb1df416 9062
80626a55 9063 /* Have we already read SIGNATURE from a DWP file? */
cf2c3c16 9064
80626a55
DE
9065 if (! dwarf2_per_objfile->dwp_checked)
9066 {
9067 dwarf2_per_objfile->dwp_file = open_and_init_dwp_file (comp_dir);
9068 dwarf2_per_objfile->dwp_checked = 1;
9069 }
9070 dwp_file = dwarf2_per_objfile->dwp_file;
3019eac3 9071
80626a55 9072 if (dwp_file != NULL)
cf2c3c16 9073 {
80626a55
DE
9074 const struct dwp_hash_table *dwp_htab =
9075 is_debug_types ? dwp_file->tus : dwp_file->cus;
9076
9077 if (dwp_htab != NULL)
9078 {
9079 struct dwo_unit *dwo_cutu =
9080 lookup_dwo_in_dwp (dwp_file, dwp_htab, signature, is_debug_types);
9081
9082 if (dwo_cutu != NULL)
9083 {
9084 if (dwarf2_read_debug)
9085 {
9086 fprintf_unfiltered (gdb_stdlog,
9087 "Virtual DWO %s %s found: @%s\n",
9088 kind, hex_string (signature),
9089 host_address_to_string (dwo_cutu));
9090 }
9091 return dwo_cutu;
9092 }
9093 }
9094 }
9095
9096 /* Have we already seen DWO_NAME? */
9097
9098 dwo_file_slot = lookup_dwo_file_slot (dwo_name);
9099 if (*dwo_file_slot == NULL)
9100 {
9101 /* Read in the file and build a table of the DWOs it contains. */
9102 *dwo_file_slot = open_and_init_dwo_file (dwo_name, comp_dir);
9103 }
9104 /* NOTE: This will be NULL if unable to open the file. */
9105 dwo_file = *dwo_file_slot;
9106
9107 if (dwo_file != NULL)
9108 {
9109 htab_t htab = is_debug_types ? dwo_file->tus : dwo_file->cus;
9110
9111 if (htab != NULL)
9112 {
9113 struct dwo_unit find_dwo_cutu, *dwo_cutu;
9a619af0 9114
80626a55
DE
9115 memset (&find_dwo_cutu, 0, sizeof (find_dwo_cutu));
9116 find_dwo_cutu.signature = signature;
9117 dwo_cutu = htab_find (htab, &find_dwo_cutu);
3019eac3 9118
80626a55
DE
9119 if (dwo_cutu != NULL)
9120 {
9121 if (dwarf2_read_debug)
9122 {
9123 fprintf_unfiltered (gdb_stdlog, "DWO %s %s(%s) found: @%s\n",
9124 kind, dwo_name, hex_string (signature),
9125 host_address_to_string (dwo_cutu));
9126 }
9127 return dwo_cutu;
9128 }
9129 }
2e276125 9130 }
9cdd5dbd 9131
80626a55
DE
9132 /* We didn't find it. This could mean a dwo_id mismatch, or
9133 someone deleted the DWO/DWP file, or the search path isn't set up
9134 correctly to find the file. */
9135
9136 if (dwarf2_read_debug)
9137 {
9138 fprintf_unfiltered (gdb_stdlog, "DWO %s %s(%s) not found\n",
9139 kind, dwo_name, hex_string (signature));
9140 }
3019eac3
DE
9141
9142 complaint (&symfile_complaints,
80626a55 9143 _("Could not find DWO CU referenced by CU at offset 0x%x"
3019eac3 9144 " [in module %s]"),
80626a55 9145 this_unit->offset.sect_off, objfile->name);
3019eac3 9146 return NULL;
5fb290d7
DJ
9147}
9148
80626a55
DE
9149/* Lookup the DWO CU DWO_NAME/SIGNATURE referenced from THIS_CU.
9150 See lookup_dwo_cutu_unit for details. */
9151
9152static struct dwo_unit *
9153lookup_dwo_comp_unit (struct dwarf2_per_cu_data *this_cu,
9154 const char *dwo_name, const char *comp_dir,
9155 ULONGEST signature)
9156{
9157 return lookup_dwo_cutu (this_cu, dwo_name, comp_dir, signature, 0);
9158}
9159
9160/* Lookup the DWO TU DWO_NAME/SIGNATURE referenced from THIS_TU.
9161 See lookup_dwo_cutu_unit for details. */
9162
9163static struct dwo_unit *
9164lookup_dwo_type_unit (struct signatured_type *this_tu,
9165 const char *dwo_name, const char *comp_dir)
9166{
9167 return lookup_dwo_cutu (&this_tu->per_cu, dwo_name, comp_dir, this_tu->signature, 1);
9168}
9169
3019eac3
DE
9170/* Free all resources associated with DWO_FILE.
9171 Close the DWO file and munmap the sections.
9172 All memory should be on the objfile obstack. */
348e048f
DE
9173
9174static void
3019eac3 9175free_dwo_file (struct dwo_file *dwo_file, struct objfile *objfile)
348e048f 9176{
3019eac3
DE
9177 int ix;
9178 struct dwarf2_section_info *section;
348e048f 9179
80626a55
DE
9180 gdb_assert (dwo_file->dbfd != objfile->obfd);
9181 gdb_bfd_unref (dwo_file->dbfd);
348e048f 9182
3019eac3
DE
9183 VEC_free (dwarf2_section_info_def, dwo_file->sections.types);
9184}
348e048f 9185
3019eac3 9186/* Wrapper for free_dwo_file for use in cleanups. */
348e048f 9187
3019eac3
DE
9188static void
9189free_dwo_file_cleanup (void *arg)
9190{
9191 struct dwo_file *dwo_file = (struct dwo_file *) arg;
9192 struct objfile *objfile = dwarf2_per_objfile->objfile;
348e048f 9193
3019eac3
DE
9194 free_dwo_file (dwo_file, objfile);
9195}
348e048f 9196
3019eac3 9197/* Traversal function for free_dwo_files. */
2ab95328 9198
3019eac3
DE
9199static int
9200free_dwo_file_from_slot (void **slot, void *info)
9201{
9202 struct dwo_file *dwo_file = (struct dwo_file *) *slot;
9203 struct objfile *objfile = (struct objfile *) info;
348e048f 9204
3019eac3 9205 free_dwo_file (dwo_file, objfile);
348e048f 9206
3019eac3
DE
9207 return 1;
9208}
348e048f 9209
3019eac3 9210/* Free all resources associated with DWO_FILES. */
348e048f 9211
3019eac3
DE
9212static void
9213free_dwo_files (htab_t dwo_files, struct objfile *objfile)
9214{
9215 htab_traverse_noresize (dwo_files, free_dwo_file_from_slot, objfile);
348e048f 9216}
3019eac3
DE
9217\f
9218/* Read in various DIEs. */
348e048f 9219
d389af10
JK
9220/* qsort helper for inherit_abstract_dies. */
9221
9222static int
9223unsigned_int_compar (const void *ap, const void *bp)
9224{
9225 unsigned int a = *(unsigned int *) ap;
9226 unsigned int b = *(unsigned int *) bp;
9227
9228 return (a > b) - (b > a);
9229}
9230
9231/* DW_AT_abstract_origin inherits whole DIEs (not just their attributes).
3e43a32a
MS
9232 Inherit only the children of the DW_AT_abstract_origin DIE not being
9233 already referenced by DW_AT_abstract_origin from the children of the
9234 current DIE. */
d389af10
JK
9235
9236static void
9237inherit_abstract_dies (struct die_info *die, struct dwarf2_cu *cu)
9238{
9239 struct die_info *child_die;
9240 unsigned die_children_count;
9241 /* CU offsets which were referenced by children of the current DIE. */
b64f50a1
JK
9242 sect_offset *offsets;
9243 sect_offset *offsets_end, *offsetp;
d389af10
JK
9244 /* Parent of DIE - referenced by DW_AT_abstract_origin. */
9245 struct die_info *origin_die;
9246 /* Iterator of the ORIGIN_DIE children. */
9247 struct die_info *origin_child_die;
9248 struct cleanup *cleanups;
9249 struct attribute *attr;
cd02d79d
PA
9250 struct dwarf2_cu *origin_cu;
9251 struct pending **origin_previous_list_in_scope;
d389af10
JK
9252
9253 attr = dwarf2_attr (die, DW_AT_abstract_origin, cu);
9254 if (!attr)
9255 return;
9256
cd02d79d
PA
9257 /* Note that following die references may follow to a die in a
9258 different cu. */
9259
9260 origin_cu = cu;
9261 origin_die = follow_die_ref (die, attr, &origin_cu);
9262
9263 /* We're inheriting ORIGIN's children into the scope we'd put DIE's
9264 symbols in. */
9265 origin_previous_list_in_scope = origin_cu->list_in_scope;
9266 origin_cu->list_in_scope = cu->list_in_scope;
9267
edb3359d
DJ
9268 if (die->tag != origin_die->tag
9269 && !(die->tag == DW_TAG_inlined_subroutine
9270 && origin_die->tag == DW_TAG_subprogram))
d389af10
JK
9271 complaint (&symfile_complaints,
9272 _("DIE 0x%x and its abstract origin 0x%x have different tags"),
b64f50a1 9273 die->offset.sect_off, origin_die->offset.sect_off);
d389af10
JK
9274
9275 child_die = die->child;
9276 die_children_count = 0;
9277 while (child_die && child_die->tag)
9278 {
9279 child_die = sibling_die (child_die);
9280 die_children_count++;
9281 }
9282 offsets = xmalloc (sizeof (*offsets) * die_children_count);
9283 cleanups = make_cleanup (xfree, offsets);
9284
9285 offsets_end = offsets;
9286 child_die = die->child;
9287 while (child_die && child_die->tag)
9288 {
c38f313d
DJ
9289 /* For each CHILD_DIE, find the corresponding child of
9290 ORIGIN_DIE. If there is more than one layer of
9291 DW_AT_abstract_origin, follow them all; there shouldn't be,
9292 but GCC versions at least through 4.4 generate this (GCC PR
9293 40573). */
9294 struct die_info *child_origin_die = child_die;
cd02d79d 9295 struct dwarf2_cu *child_origin_cu = cu;
9a619af0 9296
c38f313d
DJ
9297 while (1)
9298 {
cd02d79d
PA
9299 attr = dwarf2_attr (child_origin_die, DW_AT_abstract_origin,
9300 child_origin_cu);
c38f313d
DJ
9301 if (attr == NULL)
9302 break;
cd02d79d
PA
9303 child_origin_die = follow_die_ref (child_origin_die, attr,
9304 &child_origin_cu);
c38f313d
DJ
9305 }
9306
d389af10
JK
9307 /* According to DWARF3 3.3.8.2 #3 new entries without their abstract
9308 counterpart may exist. */
c38f313d 9309 if (child_origin_die != child_die)
d389af10 9310 {
edb3359d
DJ
9311 if (child_die->tag != child_origin_die->tag
9312 && !(child_die->tag == DW_TAG_inlined_subroutine
9313 && child_origin_die->tag == DW_TAG_subprogram))
d389af10
JK
9314 complaint (&symfile_complaints,
9315 _("Child DIE 0x%x and its abstract origin 0x%x have "
b64f50a1
JK
9316 "different tags"), child_die->offset.sect_off,
9317 child_origin_die->offset.sect_off);
c38f313d
DJ
9318 if (child_origin_die->parent != origin_die)
9319 complaint (&symfile_complaints,
9320 _("Child DIE 0x%x and its abstract origin 0x%x have "
b64f50a1
JK
9321 "different parents"), child_die->offset.sect_off,
9322 child_origin_die->offset.sect_off);
c38f313d
DJ
9323 else
9324 *offsets_end++ = child_origin_die->offset;
d389af10
JK
9325 }
9326 child_die = sibling_die (child_die);
9327 }
9328 qsort (offsets, offsets_end - offsets, sizeof (*offsets),
9329 unsigned_int_compar);
9330 for (offsetp = offsets + 1; offsetp < offsets_end; offsetp++)
b64f50a1 9331 if (offsetp[-1].sect_off == offsetp->sect_off)
3e43a32a
MS
9332 complaint (&symfile_complaints,
9333 _("Multiple children of DIE 0x%x refer "
9334 "to DIE 0x%x as their abstract origin"),
b64f50a1 9335 die->offset.sect_off, offsetp->sect_off);
d389af10
JK
9336
9337 offsetp = offsets;
9338 origin_child_die = origin_die->child;
9339 while (origin_child_die && origin_child_die->tag)
9340 {
9341 /* Is ORIGIN_CHILD_DIE referenced by any of the DIE children? */
b64f50a1
JK
9342 while (offsetp < offsets_end
9343 && offsetp->sect_off < origin_child_die->offset.sect_off)
d389af10 9344 offsetp++;
b64f50a1
JK
9345 if (offsetp >= offsets_end
9346 || offsetp->sect_off > origin_child_die->offset.sect_off)
d389af10
JK
9347 {
9348 /* Found that ORIGIN_CHILD_DIE is really not referenced. */
cd02d79d 9349 process_die (origin_child_die, origin_cu);
d389af10
JK
9350 }
9351 origin_child_die = sibling_die (origin_child_die);
9352 }
cd02d79d 9353 origin_cu->list_in_scope = origin_previous_list_in_scope;
d389af10
JK
9354
9355 do_cleanups (cleanups);
9356}
9357
c906108c 9358static void
e7c27a73 9359read_func_scope (struct die_info *die, struct dwarf2_cu *cu)
c906108c 9360{
e7c27a73 9361 struct objfile *objfile = cu->objfile;
52f0bd74 9362 struct context_stack *new;
c906108c
SS
9363 CORE_ADDR lowpc;
9364 CORE_ADDR highpc;
9365 struct die_info *child_die;
edb3359d 9366 struct attribute *attr, *call_line, *call_file;
c906108c 9367 char *name;
e142c38c 9368 CORE_ADDR baseaddr;
801e3a5b 9369 struct block *block;
edb3359d 9370 int inlined_func = (die->tag == DW_TAG_inlined_subroutine);
34eaf542
TT
9371 VEC (symbolp) *template_args = NULL;
9372 struct template_symbol *templ_func = NULL;
edb3359d
DJ
9373
9374 if (inlined_func)
9375 {
9376 /* If we do not have call site information, we can't show the
9377 caller of this inlined function. That's too confusing, so
9378 only use the scope for local variables. */
9379 call_line = dwarf2_attr (die, DW_AT_call_line, cu);
9380 call_file = dwarf2_attr (die, DW_AT_call_file, cu);
9381 if (call_line == NULL || call_file == NULL)
9382 {
9383 read_lexical_block_scope (die, cu);
9384 return;
9385 }
9386 }
c906108c 9387
e142c38c
DJ
9388 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
9389
94af9270 9390 name = dwarf2_name (die, cu);
c906108c 9391
e8d05480
JB
9392 /* Ignore functions with missing or empty names. These are actually
9393 illegal according to the DWARF standard. */
9394 if (name == NULL)
9395 {
9396 complaint (&symfile_complaints,
b64f50a1
JK
9397 _("missing name for subprogram DIE at %d"),
9398 die->offset.sect_off);
e8d05480
JB
9399 return;
9400 }
9401
9402 /* Ignore functions with missing or invalid low and high pc attributes. */
9403 if (!dwarf2_get_pc_bounds (die, &lowpc, &highpc, cu, NULL))
9404 {
ae4d0c03
PM
9405 attr = dwarf2_attr (die, DW_AT_external, cu);
9406 if (!attr || !DW_UNSND (attr))
9407 complaint (&symfile_complaints,
3e43a32a
MS
9408 _("cannot get low and high bounds "
9409 "for subprogram DIE at %d"),
b64f50a1 9410 die->offset.sect_off);
e8d05480
JB
9411 return;
9412 }
c906108c
SS
9413
9414 lowpc += baseaddr;
9415 highpc += baseaddr;
9416
34eaf542
TT
9417 /* If we have any template arguments, then we must allocate a
9418 different sort of symbol. */
9419 for (child_die = die->child; child_die; child_die = sibling_die (child_die))
9420 {
9421 if (child_die->tag == DW_TAG_template_type_param
9422 || child_die->tag == DW_TAG_template_value_param)
9423 {
9424 templ_func = OBSTACK_ZALLOC (&objfile->objfile_obstack,
9425 struct template_symbol);
9426 templ_func->base.is_cplus_template_function = 1;
9427 break;
9428 }
9429 }
9430
c906108c 9431 new = push_context (0, lowpc);
34eaf542
TT
9432 new->name = new_symbol_full (die, read_type_die (die, cu), cu,
9433 (struct symbol *) templ_func);
4c2df51b 9434
4cecd739
DJ
9435 /* If there is a location expression for DW_AT_frame_base, record
9436 it. */
e142c38c 9437 attr = dwarf2_attr (die, DW_AT_frame_base, cu);
4c2df51b 9438 if (attr)
c034e007
AC
9439 /* FIXME: cagney/2004-01-26: The DW_AT_frame_base's location
9440 expression is being recorded directly in the function's symbol
9441 and not in a separate frame-base object. I guess this hack is
9442 to avoid adding some sort of frame-base adjunct/annex to the
9443 function's symbol :-(. The problem with doing this is that it
9444 results in a function symbol with a location expression that
9445 has nothing to do with the location of the function, ouch! The
9446 relationship should be: a function's symbol has-a frame base; a
9447 frame-base has-a location expression. */
e7c27a73 9448 dwarf2_symbol_mark_computed (attr, new->name, cu);
4c2df51b 9449
e142c38c 9450 cu->list_in_scope = &local_symbols;
c906108c 9451
639d11d3 9452 if (die->child != NULL)
c906108c 9453 {
639d11d3 9454 child_die = die->child;
c906108c
SS
9455 while (child_die && child_die->tag)
9456 {
34eaf542
TT
9457 if (child_die->tag == DW_TAG_template_type_param
9458 || child_die->tag == DW_TAG_template_value_param)
9459 {
9460 struct symbol *arg = new_symbol (child_die, NULL, cu);
9461
f1078f66
DJ
9462 if (arg != NULL)
9463 VEC_safe_push (symbolp, template_args, arg);
34eaf542
TT
9464 }
9465 else
9466 process_die (child_die, cu);
c906108c
SS
9467 child_die = sibling_die (child_die);
9468 }
9469 }
9470
d389af10
JK
9471 inherit_abstract_dies (die, cu);
9472
4a811a97
UW
9473 /* If we have a DW_AT_specification, we might need to import using
9474 directives from the context of the specification DIE. See the
9475 comment in determine_prefix. */
9476 if (cu->language == language_cplus
9477 && dwarf2_attr (die, DW_AT_specification, cu))
9478 {
9479 struct dwarf2_cu *spec_cu = cu;
9480 struct die_info *spec_die = die_specification (die, &spec_cu);
9481
9482 while (spec_die)
9483 {
9484 child_die = spec_die->child;
9485 while (child_die && child_die->tag)
9486 {
9487 if (child_die->tag == DW_TAG_imported_module)
9488 process_die (child_die, spec_cu);
9489 child_die = sibling_die (child_die);
9490 }
9491
9492 /* In some cases, GCC generates specification DIEs that
9493 themselves contain DW_AT_specification attributes. */
9494 spec_die = die_specification (spec_die, &spec_cu);
9495 }
9496 }
9497
c906108c
SS
9498 new = pop_context ();
9499 /* Make a block for the local symbols within. */
801e3a5b
JB
9500 block = finish_block (new->name, &local_symbols, new->old_blocks,
9501 lowpc, highpc, objfile);
9502
df8a16a1 9503 /* For C++, set the block's scope. */
f55ee35c 9504 if (cu->language == language_cplus || cu->language == language_fortran)
df8a16a1 9505 cp_set_block_scope (new->name, block, &objfile->objfile_obstack,
0114d602 9506 determine_prefix (die, cu),
df8a16a1
DJ
9507 processing_has_namespace_info);
9508
801e3a5b
JB
9509 /* If we have address ranges, record them. */
9510 dwarf2_record_block_ranges (die, block, baseaddr, cu);
6e70227d 9511
34eaf542
TT
9512 /* Attach template arguments to function. */
9513 if (! VEC_empty (symbolp, template_args))
9514 {
9515 gdb_assert (templ_func != NULL);
9516
9517 templ_func->n_template_arguments = VEC_length (symbolp, template_args);
9518 templ_func->template_arguments
9519 = obstack_alloc (&objfile->objfile_obstack,
9520 (templ_func->n_template_arguments
9521 * sizeof (struct symbol *)));
9522 memcpy (templ_func->template_arguments,
9523 VEC_address (symbolp, template_args),
9524 (templ_func->n_template_arguments * sizeof (struct symbol *)));
9525 VEC_free (symbolp, template_args);
9526 }
9527
208d8187
JB
9528 /* In C++, we can have functions nested inside functions (e.g., when
9529 a function declares a class that has methods). This means that
9530 when we finish processing a function scope, we may need to go
9531 back to building a containing block's symbol lists. */
9532 local_symbols = new->locals;
27aa8d6a 9533 using_directives = new->using_directives;
208d8187 9534
921e78cf
JB
9535 /* If we've finished processing a top-level function, subsequent
9536 symbols go in the file symbol list. */
9537 if (outermost_context_p ())
e142c38c 9538 cu->list_in_scope = &file_symbols;
c906108c
SS
9539}
9540
9541/* Process all the DIES contained within a lexical block scope. Start
9542 a new scope, process the dies, and then close the scope. */
9543
9544static void
e7c27a73 9545read_lexical_block_scope (struct die_info *die, struct dwarf2_cu *cu)
c906108c 9546{
e7c27a73 9547 struct objfile *objfile = cu->objfile;
52f0bd74 9548 struct context_stack *new;
c906108c
SS
9549 CORE_ADDR lowpc, highpc;
9550 struct die_info *child_die;
e142c38c
DJ
9551 CORE_ADDR baseaddr;
9552
9553 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
c906108c
SS
9554
9555 /* Ignore blocks with missing or invalid low and high pc attributes. */
af34e669
DJ
9556 /* ??? Perhaps consider discontiguous blocks defined by DW_AT_ranges
9557 as multiple lexical blocks? Handling children in a sane way would
6e70227d 9558 be nasty. Might be easier to properly extend generic blocks to
af34e669 9559 describe ranges. */
d85a05f0 9560 if (!dwarf2_get_pc_bounds (die, &lowpc, &highpc, cu, NULL))
c906108c
SS
9561 return;
9562 lowpc += baseaddr;
9563 highpc += baseaddr;
9564
9565 push_context (0, lowpc);
639d11d3 9566 if (die->child != NULL)
c906108c 9567 {
639d11d3 9568 child_die = die->child;
c906108c
SS
9569 while (child_die && child_die->tag)
9570 {
e7c27a73 9571 process_die (child_die, cu);
c906108c
SS
9572 child_die = sibling_die (child_die);
9573 }
9574 }
9575 new = pop_context ();
9576
8540c487 9577 if (local_symbols != NULL || using_directives != NULL)
c906108c 9578 {
801e3a5b
JB
9579 struct block *block
9580 = finish_block (0, &local_symbols, new->old_blocks, new->start_addr,
9581 highpc, objfile);
9582
9583 /* Note that recording ranges after traversing children, as we
9584 do here, means that recording a parent's ranges entails
9585 walking across all its children's ranges as they appear in
9586 the address map, which is quadratic behavior.
9587
9588 It would be nicer to record the parent's ranges before
9589 traversing its children, simply overriding whatever you find
9590 there. But since we don't even decide whether to create a
9591 block until after we've traversed its children, that's hard
9592 to do. */
9593 dwarf2_record_block_ranges (die, block, baseaddr, cu);
c906108c
SS
9594 }
9595 local_symbols = new->locals;
27aa8d6a 9596 using_directives = new->using_directives;
c906108c
SS
9597}
9598
96408a79
SA
9599/* Read in DW_TAG_GNU_call_site and insert it to CU->call_site_htab. */
9600
9601static void
9602read_call_site_scope (struct die_info *die, struct dwarf2_cu *cu)
9603{
9604 struct objfile *objfile = cu->objfile;
9605 struct gdbarch *gdbarch = get_objfile_arch (objfile);
9606 CORE_ADDR pc, baseaddr;
9607 struct attribute *attr;
9608 struct call_site *call_site, call_site_local;
9609 void **slot;
9610 int nparams;
9611 struct die_info *child_die;
9612
9613 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
9614
9615 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
9616 if (!attr)
9617 {
9618 complaint (&symfile_complaints,
9619 _("missing DW_AT_low_pc for DW_TAG_GNU_call_site "
9620 "DIE 0x%x [in module %s]"),
b64f50a1 9621 die->offset.sect_off, objfile->name);
96408a79
SA
9622 return;
9623 }
9624 pc = DW_ADDR (attr) + baseaddr;
9625
9626 if (cu->call_site_htab == NULL)
9627 cu->call_site_htab = htab_create_alloc_ex (16, core_addr_hash, core_addr_eq,
9628 NULL, &objfile->objfile_obstack,
9629 hashtab_obstack_allocate, NULL);
9630 call_site_local.pc = pc;
9631 slot = htab_find_slot (cu->call_site_htab, &call_site_local, INSERT);
9632 if (*slot != NULL)
9633 {
9634 complaint (&symfile_complaints,
9635 _("Duplicate PC %s for DW_TAG_GNU_call_site "
9636 "DIE 0x%x [in module %s]"),
b64f50a1 9637 paddress (gdbarch, pc), die->offset.sect_off, objfile->name);
96408a79
SA
9638 return;
9639 }
9640
9641 /* Count parameters at the caller. */
9642
9643 nparams = 0;
9644 for (child_die = die->child; child_die && child_die->tag;
9645 child_die = sibling_die (child_die))
9646 {
9647 if (child_die->tag != DW_TAG_GNU_call_site_parameter)
9648 {
9649 complaint (&symfile_complaints,
9650 _("Tag %d is not DW_TAG_GNU_call_site_parameter in "
9651 "DW_TAG_GNU_call_site child DIE 0x%x [in module %s]"),
b64f50a1 9652 child_die->tag, child_die->offset.sect_off, objfile->name);
96408a79
SA
9653 continue;
9654 }
9655
9656 nparams++;
9657 }
9658
9659 call_site = obstack_alloc (&objfile->objfile_obstack,
9660 (sizeof (*call_site)
9661 + (sizeof (*call_site->parameter)
9662 * (nparams - 1))));
9663 *slot = call_site;
9664 memset (call_site, 0, sizeof (*call_site) - sizeof (*call_site->parameter));
9665 call_site->pc = pc;
9666
9667 if (dwarf2_flag_true_p (die, DW_AT_GNU_tail_call, cu))
9668 {
9669 struct die_info *func_die;
9670
9671 /* Skip also over DW_TAG_inlined_subroutine. */
9672 for (func_die = die->parent;
9673 func_die && func_die->tag != DW_TAG_subprogram
9674 && func_die->tag != DW_TAG_subroutine_type;
9675 func_die = func_die->parent);
9676
9677 /* DW_AT_GNU_all_call_sites is a superset
9678 of DW_AT_GNU_all_tail_call_sites. */
9679 if (func_die
9680 && !dwarf2_flag_true_p (func_die, DW_AT_GNU_all_call_sites, cu)
9681 && !dwarf2_flag_true_p (func_die, DW_AT_GNU_all_tail_call_sites, cu))
9682 {
9683 /* TYPE_TAIL_CALL_LIST is not interesting in functions where it is
9684 not complete. But keep CALL_SITE for look ups via call_site_htab,
9685 both the initial caller containing the real return address PC and
9686 the final callee containing the current PC of a chain of tail
9687 calls do not need to have the tail call list complete. But any
9688 function candidate for a virtual tail call frame searched via
9689 TYPE_TAIL_CALL_LIST must have the tail call list complete to be
9690 determined unambiguously. */
9691 }
9692 else
9693 {
9694 struct type *func_type = NULL;
9695
9696 if (func_die)
9697 func_type = get_die_type (func_die, cu);
9698 if (func_type != NULL)
9699 {
9700 gdb_assert (TYPE_CODE (func_type) == TYPE_CODE_FUNC);
9701
9702 /* Enlist this call site to the function. */
9703 call_site->tail_call_next = TYPE_TAIL_CALL_LIST (func_type);
9704 TYPE_TAIL_CALL_LIST (func_type) = call_site;
9705 }
9706 else
9707 complaint (&symfile_complaints,
9708 _("Cannot find function owning DW_TAG_GNU_call_site "
9709 "DIE 0x%x [in module %s]"),
b64f50a1 9710 die->offset.sect_off, objfile->name);
96408a79
SA
9711 }
9712 }
9713
9714 attr = dwarf2_attr (die, DW_AT_GNU_call_site_target, cu);
9715 if (attr == NULL)
9716 attr = dwarf2_attr (die, DW_AT_abstract_origin, cu);
9717 SET_FIELD_DWARF_BLOCK (call_site->target, NULL);
9718 if (!attr || (attr_form_is_block (attr) && DW_BLOCK (attr)->size == 0))
9719 /* Keep NULL DWARF_BLOCK. */;
9720 else if (attr_form_is_block (attr))
9721 {
9722 struct dwarf2_locexpr_baton *dlbaton;
9723
9724 dlbaton = obstack_alloc (&objfile->objfile_obstack, sizeof (*dlbaton));
9725 dlbaton->data = DW_BLOCK (attr)->data;
9726 dlbaton->size = DW_BLOCK (attr)->size;
9727 dlbaton->per_cu = cu->per_cu;
9728
9729 SET_FIELD_DWARF_BLOCK (call_site->target, dlbaton);
9730 }
9731 else if (is_ref_attr (attr))
9732 {
96408a79
SA
9733 struct dwarf2_cu *target_cu = cu;
9734 struct die_info *target_die;
9735
9736 target_die = follow_die_ref_or_sig (die, attr, &target_cu);
9737 gdb_assert (target_cu->objfile == objfile);
9738 if (die_is_declaration (target_die, target_cu))
9739 {
9740 const char *target_physname;
9741
9742 target_physname = dwarf2_physname (NULL, target_die, target_cu);
9743 if (target_physname == NULL)
9744 complaint (&symfile_complaints,
9745 _("DW_AT_GNU_call_site_target target DIE has invalid "
9746 "physname, for referencing DIE 0x%x [in module %s]"),
b64f50a1 9747 die->offset.sect_off, objfile->name);
96408a79
SA
9748 else
9749 SET_FIELD_PHYSNAME (call_site->target, (char *) target_physname);
9750 }
9751 else
9752 {
9753 CORE_ADDR lowpc;
9754
9755 /* DW_AT_entry_pc should be preferred. */
9756 if (!dwarf2_get_pc_bounds (target_die, &lowpc, NULL, target_cu, NULL))
9757 complaint (&symfile_complaints,
9758 _("DW_AT_GNU_call_site_target target DIE has invalid "
9759 "low pc, for referencing DIE 0x%x [in module %s]"),
b64f50a1 9760 die->offset.sect_off, objfile->name);
96408a79
SA
9761 else
9762 SET_FIELD_PHYSADDR (call_site->target, lowpc + baseaddr);
9763 }
9764 }
9765 else
9766 complaint (&symfile_complaints,
9767 _("DW_TAG_GNU_call_site DW_AT_GNU_call_site_target is neither "
9768 "block nor reference, for DIE 0x%x [in module %s]"),
b64f50a1 9769 die->offset.sect_off, objfile->name);
96408a79
SA
9770
9771 call_site->per_cu = cu->per_cu;
9772
9773 for (child_die = die->child;
9774 child_die && child_die->tag;
9775 child_die = sibling_die (child_die))
9776 {
96408a79 9777 struct call_site_parameter *parameter;
1788b2d3 9778 struct attribute *loc, *origin;
96408a79
SA
9779
9780 if (child_die->tag != DW_TAG_GNU_call_site_parameter)
9781 {
9782 /* Already printed the complaint above. */
9783 continue;
9784 }
9785
9786 gdb_assert (call_site->parameter_count < nparams);
9787 parameter = &call_site->parameter[call_site->parameter_count];
9788
1788b2d3
JK
9789 /* DW_AT_location specifies the register number or DW_AT_abstract_origin
9790 specifies DW_TAG_formal_parameter. Value of the data assumed for the
9791 register is contained in DW_AT_GNU_call_site_value. */
96408a79 9792
24c5c679 9793 loc = dwarf2_attr (child_die, DW_AT_location, cu);
1788b2d3
JK
9794 origin = dwarf2_attr (child_die, DW_AT_abstract_origin, cu);
9795 if (loc == NULL && origin != NULL && is_ref_attr (origin))
9796 {
9797 sect_offset offset;
9798
9799 parameter->kind = CALL_SITE_PARAMETER_PARAM_OFFSET;
9800 offset = dwarf2_get_ref_die_offset (origin);
d76b7dbc
JK
9801 if (!offset_in_cu_p (&cu->header, offset))
9802 {
9803 /* As DW_OP_GNU_parameter_ref uses CU-relative offset this
9804 binding can be done only inside one CU. Such referenced DIE
9805 therefore cannot be even moved to DW_TAG_partial_unit. */
9806 complaint (&symfile_complaints,
9807 _("DW_AT_abstract_origin offset is not in CU for "
9808 "DW_TAG_GNU_call_site child DIE 0x%x "
9809 "[in module %s]"),
9810 child_die->offset.sect_off, objfile->name);
9811 continue;
9812 }
1788b2d3
JK
9813 parameter->u.param_offset.cu_off = (offset.sect_off
9814 - cu->header.offset.sect_off);
9815 }
9816 else if (loc == NULL || origin != NULL || !attr_form_is_block (loc))
96408a79
SA
9817 {
9818 complaint (&symfile_complaints,
9819 _("No DW_FORM_block* DW_AT_location for "
9820 "DW_TAG_GNU_call_site child DIE 0x%x [in module %s]"),
b64f50a1 9821 child_die->offset.sect_off, objfile->name);
96408a79
SA
9822 continue;
9823 }
24c5c679 9824 else
96408a79 9825 {
24c5c679
JK
9826 parameter->u.dwarf_reg = dwarf_block_to_dwarf_reg
9827 (DW_BLOCK (loc)->data, &DW_BLOCK (loc)->data[DW_BLOCK (loc)->size]);
9828 if (parameter->u.dwarf_reg != -1)
9829 parameter->kind = CALL_SITE_PARAMETER_DWARF_REG;
9830 else if (dwarf_block_to_sp_offset (gdbarch, DW_BLOCK (loc)->data,
9831 &DW_BLOCK (loc)->data[DW_BLOCK (loc)->size],
9832 &parameter->u.fb_offset))
9833 parameter->kind = CALL_SITE_PARAMETER_FB_OFFSET;
9834 else
9835 {
9836 complaint (&symfile_complaints,
9837 _("Only single DW_OP_reg or DW_OP_fbreg is supported "
9838 "for DW_FORM_block* DW_AT_location is supported for "
9839 "DW_TAG_GNU_call_site child DIE 0x%x "
9840 "[in module %s]"),
9841 child_die->offset.sect_off, objfile->name);
9842 continue;
9843 }
96408a79
SA
9844 }
9845
9846 attr = dwarf2_attr (child_die, DW_AT_GNU_call_site_value, cu);
9847 if (!attr_form_is_block (attr))
9848 {
9849 complaint (&symfile_complaints,
9850 _("No DW_FORM_block* DW_AT_GNU_call_site_value for "
9851 "DW_TAG_GNU_call_site child DIE 0x%x [in module %s]"),
b64f50a1 9852 child_die->offset.sect_off, objfile->name);
96408a79
SA
9853 continue;
9854 }
9855 parameter->value = DW_BLOCK (attr)->data;
9856 parameter->value_size = DW_BLOCK (attr)->size;
9857
9858 /* Parameters are not pre-cleared by memset above. */
9859 parameter->data_value = NULL;
9860 parameter->data_value_size = 0;
9861 call_site->parameter_count++;
9862
9863 attr = dwarf2_attr (child_die, DW_AT_GNU_call_site_data_value, cu);
9864 if (attr)
9865 {
9866 if (!attr_form_is_block (attr))
9867 complaint (&symfile_complaints,
9868 _("No DW_FORM_block* DW_AT_GNU_call_site_data_value for "
9869 "DW_TAG_GNU_call_site child DIE 0x%x [in module %s]"),
b64f50a1 9870 child_die->offset.sect_off, objfile->name);
96408a79
SA
9871 else
9872 {
9873 parameter->data_value = DW_BLOCK (attr)->data;
9874 parameter->data_value_size = DW_BLOCK (attr)->size;
9875 }
9876 }
9877 }
9878}
9879
43039443 9880/* Get low and high pc attributes from DW_AT_ranges attribute value OFFSET.
ff013f42
JK
9881 Return 1 if the attributes are present and valid, otherwise, return 0.
9882 If RANGES_PST is not NULL we should setup `objfile->psymtabs_addrmap'. */
43039443
JK
9883
9884static int
9885dwarf2_ranges_read (unsigned offset, CORE_ADDR *low_return,
ff013f42
JK
9886 CORE_ADDR *high_return, struct dwarf2_cu *cu,
9887 struct partial_symtab *ranges_pst)
43039443
JK
9888{
9889 struct objfile *objfile = cu->objfile;
9890 struct comp_unit_head *cu_header = &cu->header;
9891 bfd *obfd = objfile->obfd;
9892 unsigned int addr_size = cu_header->addr_size;
9893 CORE_ADDR mask = ~(~(CORE_ADDR)1 << (addr_size * 8 - 1));
9894 /* Base address selection entry. */
9895 CORE_ADDR base;
9896 int found_base;
9897 unsigned int dummy;
9898 gdb_byte *buffer;
9899 CORE_ADDR marker;
9900 int low_set;
9901 CORE_ADDR low = 0;
9902 CORE_ADDR high = 0;
ff013f42 9903 CORE_ADDR baseaddr;
43039443 9904
d00adf39
DE
9905 found_base = cu->base_known;
9906 base = cu->base_address;
43039443 9907
be391dca 9908 dwarf2_read_section (objfile, &dwarf2_per_objfile->ranges);
dce234bc 9909 if (offset >= dwarf2_per_objfile->ranges.size)
43039443
JK
9910 {
9911 complaint (&symfile_complaints,
9912 _("Offset %d out of bounds for DW_AT_ranges attribute"),
9913 offset);
9914 return 0;
9915 }
dce234bc 9916 buffer = dwarf2_per_objfile->ranges.buffer + offset;
43039443
JK
9917
9918 /* Read in the largest possible address. */
9919 marker = read_address (obfd, buffer, cu, &dummy);
9920 if ((marker & mask) == mask)
9921 {
9922 /* If we found the largest possible address, then
9923 read the base address. */
9924 base = read_address (obfd, buffer + addr_size, cu, &dummy);
9925 buffer += 2 * addr_size;
9926 offset += 2 * addr_size;
9927 found_base = 1;
9928 }
9929
9930 low_set = 0;
9931
e7030f15 9932 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
ff013f42 9933
43039443
JK
9934 while (1)
9935 {
9936 CORE_ADDR range_beginning, range_end;
9937
9938 range_beginning = read_address (obfd, buffer, cu, &dummy);
9939 buffer += addr_size;
9940 range_end = read_address (obfd, buffer, cu, &dummy);
9941 buffer += addr_size;
9942 offset += 2 * addr_size;
9943
9944 /* An end of list marker is a pair of zero addresses. */
9945 if (range_beginning == 0 && range_end == 0)
9946 /* Found the end of list entry. */
9947 break;
9948
9949 /* Each base address selection entry is a pair of 2 values.
9950 The first is the largest possible address, the second is
9951 the base address. Check for a base address here. */
9952 if ((range_beginning & mask) == mask)
9953 {
9954 /* If we found the largest possible address, then
9955 read the base address. */
9956 base = read_address (obfd, buffer + addr_size, cu, &dummy);
9957 found_base = 1;
9958 continue;
9959 }
9960
9961 if (!found_base)
9962 {
9963 /* We have no valid base address for the ranges
9964 data. */
9965 complaint (&symfile_complaints,
9966 _("Invalid .debug_ranges data (no base address)"));
9967 return 0;
9968 }
9969
9277c30c
UW
9970 if (range_beginning > range_end)
9971 {
9972 /* Inverted range entries are invalid. */
9973 complaint (&symfile_complaints,
9974 _("Invalid .debug_ranges data (inverted range)"));
9975 return 0;
9976 }
9977
9978 /* Empty range entries have no effect. */
9979 if (range_beginning == range_end)
9980 continue;
9981
43039443
JK
9982 range_beginning += base;
9983 range_end += base;
9984
01093045
DE
9985 /* A not-uncommon case of bad debug info.
9986 Don't pollute the addrmap with bad data. */
9987 if (range_beginning + baseaddr == 0
9988 && !dwarf2_per_objfile->has_section_at_zero)
9989 {
9990 complaint (&symfile_complaints,
9991 _(".debug_ranges entry has start address of zero"
9992 " [in module %s]"), objfile->name);
9993 continue;
9994 }
9995
9277c30c 9996 if (ranges_pst != NULL)
ff013f42 9997 addrmap_set_empty (objfile->psymtabs_addrmap,
3e43a32a
MS
9998 range_beginning + baseaddr,
9999 range_end - 1 + baseaddr,
ff013f42
JK
10000 ranges_pst);
10001
43039443
JK
10002 /* FIXME: This is recording everything as a low-high
10003 segment of consecutive addresses. We should have a
10004 data structure for discontiguous block ranges
10005 instead. */
10006 if (! low_set)
10007 {
10008 low = range_beginning;
10009 high = range_end;
10010 low_set = 1;
10011 }
10012 else
10013 {
10014 if (range_beginning < low)
10015 low = range_beginning;
10016 if (range_end > high)
10017 high = range_end;
10018 }
10019 }
10020
10021 if (! low_set)
10022 /* If the first entry is an end-of-list marker, the range
10023 describes an empty scope, i.e. no instructions. */
10024 return 0;
10025
10026 if (low_return)
10027 *low_return = low;
10028 if (high_return)
10029 *high_return = high;
10030 return 1;
10031}
10032
af34e669
DJ
10033/* Get low and high pc attributes from a die. Return 1 if the attributes
10034 are present and valid, otherwise, return 0. Return -1 if the range is
10035 discontinuous, i.e. derived from DW_AT_ranges information. */
380bca97 10036
c906108c 10037static int
af34e669 10038dwarf2_get_pc_bounds (struct die_info *die, CORE_ADDR *lowpc,
d85a05f0
DJ
10039 CORE_ADDR *highpc, struct dwarf2_cu *cu,
10040 struct partial_symtab *pst)
c906108c
SS
10041{
10042 struct attribute *attr;
91da1414 10043 struct attribute *attr_high;
af34e669
DJ
10044 CORE_ADDR low = 0;
10045 CORE_ADDR high = 0;
10046 int ret = 0;
c906108c 10047
91da1414
MW
10048 attr_high = dwarf2_attr (die, DW_AT_high_pc, cu);
10049 if (attr_high)
af34e669 10050 {
e142c38c 10051 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
af34e669 10052 if (attr)
91da1414
MW
10053 {
10054 low = DW_ADDR (attr);
3019eac3
DE
10055 if (attr_high->form == DW_FORM_addr
10056 || attr_high->form == DW_FORM_GNU_addr_index)
91da1414
MW
10057 high = DW_ADDR (attr_high);
10058 else
10059 high = low + DW_UNSND (attr_high);
10060 }
af34e669
DJ
10061 else
10062 /* Found high w/o low attribute. */
10063 return 0;
10064
10065 /* Found consecutive range of addresses. */
10066 ret = 1;
10067 }
c906108c 10068 else
af34e669 10069 {
e142c38c 10070 attr = dwarf2_attr (die, DW_AT_ranges, cu);
af34e669
DJ
10071 if (attr != NULL)
10072 {
2e3cf129
DE
10073 unsigned int ranges_offset = DW_UNSND (attr) + cu->ranges_base;
10074
af34e669 10075 /* Value of the DW_AT_ranges attribute is the offset in the
a604369a 10076 .debug_ranges section. */
2e3cf129 10077 if (!dwarf2_ranges_read (ranges_offset, &low, &high, cu, pst))
af34e669 10078 return 0;
43039443 10079 /* Found discontinuous range of addresses. */
af34e669
DJ
10080 ret = -1;
10081 }
10082 }
c906108c 10083
9373cf26
JK
10084 /* read_partial_die has also the strict LOW < HIGH requirement. */
10085 if (high <= low)
c906108c
SS
10086 return 0;
10087
10088 /* When using the GNU linker, .gnu.linkonce. sections are used to
10089 eliminate duplicate copies of functions and vtables and such.
10090 The linker will arbitrarily choose one and discard the others.
10091 The AT_*_pc values for such functions refer to local labels in
10092 these sections. If the section from that file was discarded, the
10093 labels are not in the output, so the relocs get a value of 0.
10094 If this is a discarded function, mark the pc bounds as invalid,
10095 so that GDB will ignore it. */
72dca2f5 10096 if (low == 0 && !dwarf2_per_objfile->has_section_at_zero)
c906108c
SS
10097 return 0;
10098
10099 *lowpc = low;
96408a79
SA
10100 if (highpc)
10101 *highpc = high;
af34e669 10102 return ret;
c906108c
SS
10103}
10104
b084d499
JB
10105/* Assuming that DIE represents a subprogram DIE or a lexical block, get
10106 its low and high PC addresses. Do nothing if these addresses could not
10107 be determined. Otherwise, set LOWPC to the low address if it is smaller,
10108 and HIGHPC to the high address if greater than HIGHPC. */
10109
10110static void
10111dwarf2_get_subprogram_pc_bounds (struct die_info *die,
10112 CORE_ADDR *lowpc, CORE_ADDR *highpc,
10113 struct dwarf2_cu *cu)
10114{
10115 CORE_ADDR low, high;
10116 struct die_info *child = die->child;
10117
d85a05f0 10118 if (dwarf2_get_pc_bounds (die, &low, &high, cu, NULL))
b084d499
JB
10119 {
10120 *lowpc = min (*lowpc, low);
10121 *highpc = max (*highpc, high);
10122 }
10123
10124 /* If the language does not allow nested subprograms (either inside
10125 subprograms or lexical blocks), we're done. */
10126 if (cu->language != language_ada)
10127 return;
6e70227d 10128
b084d499
JB
10129 /* Check all the children of the given DIE. If it contains nested
10130 subprograms, then check their pc bounds. Likewise, we need to
10131 check lexical blocks as well, as they may also contain subprogram
10132 definitions. */
10133 while (child && child->tag)
10134 {
10135 if (child->tag == DW_TAG_subprogram
10136 || child->tag == DW_TAG_lexical_block)
10137 dwarf2_get_subprogram_pc_bounds (child, lowpc, highpc, cu);
10138 child = sibling_die (child);
10139 }
10140}
10141
fae299cd
DC
10142/* Get the low and high pc's represented by the scope DIE, and store
10143 them in *LOWPC and *HIGHPC. If the correct values can't be
10144 determined, set *LOWPC to -1 and *HIGHPC to 0. */
10145
10146static void
10147get_scope_pc_bounds (struct die_info *die,
10148 CORE_ADDR *lowpc, CORE_ADDR *highpc,
10149 struct dwarf2_cu *cu)
10150{
10151 CORE_ADDR best_low = (CORE_ADDR) -1;
10152 CORE_ADDR best_high = (CORE_ADDR) 0;
10153 CORE_ADDR current_low, current_high;
10154
d85a05f0 10155 if (dwarf2_get_pc_bounds (die, &current_low, &current_high, cu, NULL))
fae299cd
DC
10156 {
10157 best_low = current_low;
10158 best_high = current_high;
10159 }
10160 else
10161 {
10162 struct die_info *child = die->child;
10163
10164 while (child && child->tag)
10165 {
10166 switch (child->tag) {
10167 case DW_TAG_subprogram:
b084d499 10168 dwarf2_get_subprogram_pc_bounds (child, &best_low, &best_high, cu);
fae299cd
DC
10169 break;
10170 case DW_TAG_namespace:
f55ee35c 10171 case DW_TAG_module:
fae299cd
DC
10172 /* FIXME: carlton/2004-01-16: Should we do this for
10173 DW_TAG_class_type/DW_TAG_structure_type, too? I think
10174 that current GCC's always emit the DIEs corresponding
10175 to definitions of methods of classes as children of a
10176 DW_TAG_compile_unit or DW_TAG_namespace (as opposed to
10177 the DIEs giving the declarations, which could be
10178 anywhere). But I don't see any reason why the
10179 standards says that they have to be there. */
10180 get_scope_pc_bounds (child, &current_low, &current_high, cu);
10181
10182 if (current_low != ((CORE_ADDR) -1))
10183 {
10184 best_low = min (best_low, current_low);
10185 best_high = max (best_high, current_high);
10186 }
10187 break;
10188 default:
0963b4bd 10189 /* Ignore. */
fae299cd
DC
10190 break;
10191 }
10192
10193 child = sibling_die (child);
10194 }
10195 }
10196
10197 *lowpc = best_low;
10198 *highpc = best_high;
10199}
10200
801e3a5b
JB
10201/* Record the address ranges for BLOCK, offset by BASEADDR, as given
10202 in DIE. */
380bca97 10203
801e3a5b
JB
10204static void
10205dwarf2_record_block_ranges (struct die_info *die, struct block *block,
10206 CORE_ADDR baseaddr, struct dwarf2_cu *cu)
10207{
bb5ed363 10208 struct objfile *objfile = cu->objfile;
801e3a5b 10209 struct attribute *attr;
91da1414 10210 struct attribute *attr_high;
801e3a5b 10211
91da1414
MW
10212 attr_high = dwarf2_attr (die, DW_AT_high_pc, cu);
10213 if (attr_high)
801e3a5b 10214 {
801e3a5b
JB
10215 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
10216 if (attr)
10217 {
10218 CORE_ADDR low = DW_ADDR (attr);
91da1414 10219 CORE_ADDR high;
3019eac3
DE
10220 if (attr_high->form == DW_FORM_addr
10221 || attr_high->form == DW_FORM_GNU_addr_index)
91da1414
MW
10222 high = DW_ADDR (attr_high);
10223 else
10224 high = low + DW_UNSND (attr_high);
9a619af0 10225
801e3a5b
JB
10226 record_block_range (block, baseaddr + low, baseaddr + high - 1);
10227 }
10228 }
10229
10230 attr = dwarf2_attr (die, DW_AT_ranges, cu);
10231 if (attr)
10232 {
bb5ed363 10233 bfd *obfd = objfile->obfd;
801e3a5b
JB
10234
10235 /* The value of the DW_AT_ranges attribute is the offset of the
10236 address range list in the .debug_ranges section. */
2e3cf129 10237 unsigned long offset = DW_UNSND (attr) + cu->ranges_base;
dce234bc 10238 gdb_byte *buffer = dwarf2_per_objfile->ranges.buffer + offset;
801e3a5b
JB
10239
10240 /* For some target architectures, but not others, the
10241 read_address function sign-extends the addresses it returns.
10242 To recognize base address selection entries, we need a
10243 mask. */
10244 unsigned int addr_size = cu->header.addr_size;
10245 CORE_ADDR base_select_mask = ~(~(CORE_ADDR)1 << (addr_size * 8 - 1));
10246
10247 /* The base address, to which the next pair is relative. Note
10248 that this 'base' is a DWARF concept: most entries in a range
10249 list are relative, to reduce the number of relocs against the
10250 debugging information. This is separate from this function's
10251 'baseaddr' argument, which GDB uses to relocate debugging
10252 information from a shared library based on the address at
10253 which the library was loaded. */
d00adf39
DE
10254 CORE_ADDR base = cu->base_address;
10255 int base_known = cu->base_known;
801e3a5b 10256
be391dca 10257 gdb_assert (dwarf2_per_objfile->ranges.readin);
dce234bc 10258 if (offset >= dwarf2_per_objfile->ranges.size)
801e3a5b
JB
10259 {
10260 complaint (&symfile_complaints,
10261 _("Offset %lu out of bounds for DW_AT_ranges attribute"),
10262 offset);
10263 return;
10264 }
10265
10266 for (;;)
10267 {
10268 unsigned int bytes_read;
10269 CORE_ADDR start, end;
10270
10271 start = read_address (obfd, buffer, cu, &bytes_read);
10272 buffer += bytes_read;
10273 end = read_address (obfd, buffer, cu, &bytes_read);
10274 buffer += bytes_read;
10275
10276 /* Did we find the end of the range list? */
10277 if (start == 0 && end == 0)
10278 break;
10279
10280 /* Did we find a base address selection entry? */
10281 else if ((start & base_select_mask) == base_select_mask)
10282 {
10283 base = end;
10284 base_known = 1;
10285 }
10286
10287 /* We found an ordinary address range. */
10288 else
10289 {
10290 if (!base_known)
10291 {
10292 complaint (&symfile_complaints,
3e43a32a
MS
10293 _("Invalid .debug_ranges data "
10294 "(no base address)"));
801e3a5b
JB
10295 return;
10296 }
10297
9277c30c
UW
10298 if (start > end)
10299 {
10300 /* Inverted range entries are invalid. */
10301 complaint (&symfile_complaints,
10302 _("Invalid .debug_ranges data "
10303 "(inverted range)"));
10304 return;
10305 }
10306
10307 /* Empty range entries have no effect. */
10308 if (start == end)
10309 continue;
10310
01093045
DE
10311 start += base + baseaddr;
10312 end += base + baseaddr;
10313
10314 /* A not-uncommon case of bad debug info.
10315 Don't pollute the addrmap with bad data. */
10316 if (start == 0 && !dwarf2_per_objfile->has_section_at_zero)
10317 {
10318 complaint (&symfile_complaints,
10319 _(".debug_ranges entry has start address of zero"
10320 " [in module %s]"), objfile->name);
10321 continue;
10322 }
10323
10324 record_block_range (block, start, end - 1);
801e3a5b
JB
10325 }
10326 }
10327 }
10328}
10329
685b1105
JK
10330/* Check whether the producer field indicates either of GCC < 4.6, or the
10331 Intel C/C++ compiler, and cache the result in CU. */
60d5a603 10332
685b1105
JK
10333static void
10334check_producer (struct dwarf2_cu *cu)
60d5a603
JK
10335{
10336 const char *cs;
10337 int major, minor, release;
10338
10339 if (cu->producer == NULL)
10340 {
10341 /* For unknown compilers expect their behavior is DWARF version
10342 compliant.
10343
10344 GCC started to support .debug_types sections by -gdwarf-4 since
10345 gcc-4.5.x. As the .debug_types sections are missing DW_AT_producer
10346 for their space efficiency GDB cannot workaround gcc-4.5.x -gdwarf-4
10347 combination. gcc-4.5.x -gdwarf-4 binaries have DW_AT_accessibility
10348 interpreted incorrectly by GDB now - GCC PR debug/48229. */
60d5a603 10349 }
685b1105 10350 else if (strncmp (cu->producer, "GNU ", strlen ("GNU ")) == 0)
60d5a603 10351 {
685b1105
JK
10352 /* Skip any identifier after "GNU " - such as "C++" or "Java". */
10353
ba919b58
TT
10354 cs = &cu->producer[strlen ("GNU ")];
10355 while (*cs && !isdigit (*cs))
10356 cs++;
10357 if (sscanf (cs, "%d.%d.%d", &major, &minor, &release) != 3)
10358 {
10359 /* Not recognized as GCC. */
10360 }
10361 else
685b1105
JK
10362 cu->producer_is_gxx_lt_4_6 = major < 4 || (major == 4 && minor < 6);
10363 }
10364 else if (strncmp (cu->producer, "Intel(R) C", strlen ("Intel(R) C")) == 0)
10365 cu->producer_is_icc = 1;
10366 else
10367 {
10368 /* For other non-GCC compilers, expect their behavior is DWARF version
10369 compliant. */
60d5a603
JK
10370 }
10371
ba919b58 10372 cu->checked_producer = 1;
685b1105 10373}
ba919b58 10374
685b1105
JK
10375/* Check for GCC PR debug/45124 fix which is not present in any G++ version up
10376 to 4.5.any while it is present already in G++ 4.6.0 - the PR has been fixed
10377 during 4.6.0 experimental. */
10378
10379static int
10380producer_is_gxx_lt_4_6 (struct dwarf2_cu *cu)
10381{
10382 if (!cu->checked_producer)
10383 check_producer (cu);
10384
10385 return cu->producer_is_gxx_lt_4_6;
60d5a603
JK
10386}
10387
10388/* Return the default accessibility type if it is not overriden by
10389 DW_AT_accessibility. */
10390
10391static enum dwarf_access_attribute
10392dwarf2_default_access_attribute (struct die_info *die, struct dwarf2_cu *cu)
10393{
10394 if (cu->header.version < 3 || producer_is_gxx_lt_4_6 (cu))
10395 {
10396 /* The default DWARF 2 accessibility for members is public, the default
10397 accessibility for inheritance is private. */
10398
10399 if (die->tag != DW_TAG_inheritance)
10400 return DW_ACCESS_public;
10401 else
10402 return DW_ACCESS_private;
10403 }
10404 else
10405 {
10406 /* DWARF 3+ defines the default accessibility a different way. The same
10407 rules apply now for DW_TAG_inheritance as for the members and it only
10408 depends on the container kind. */
10409
10410 if (die->parent->tag == DW_TAG_class_type)
10411 return DW_ACCESS_private;
10412 else
10413 return DW_ACCESS_public;
10414 }
10415}
10416
74ac6d43
TT
10417/* Look for DW_AT_data_member_location. Set *OFFSET to the byte
10418 offset. If the attribute was not found return 0, otherwise return
10419 1. If it was found but could not properly be handled, set *OFFSET
10420 to 0. */
10421
10422static int
10423handle_data_member_location (struct die_info *die, struct dwarf2_cu *cu,
10424 LONGEST *offset)
10425{
10426 struct attribute *attr;
10427
10428 attr = dwarf2_attr (die, DW_AT_data_member_location, cu);
10429 if (attr != NULL)
10430 {
10431 *offset = 0;
10432
10433 /* Note that we do not check for a section offset first here.
10434 This is because DW_AT_data_member_location is new in DWARF 4,
10435 so if we see it, we can assume that a constant form is really
10436 a constant and not a section offset. */
10437 if (attr_form_is_constant (attr))
10438 *offset = dwarf2_get_attr_constant_value (attr, 0);
10439 else if (attr_form_is_section_offset (attr))
10440 dwarf2_complex_location_expr_complaint ();
10441 else if (attr_form_is_block (attr))
10442 *offset = decode_locdesc (DW_BLOCK (attr), cu);
10443 else
10444 dwarf2_complex_location_expr_complaint ();
10445
10446 return 1;
10447 }
10448
10449 return 0;
10450}
10451
c906108c
SS
10452/* Add an aggregate field to the field list. */
10453
10454static void
107d2387 10455dwarf2_add_field (struct field_info *fip, struct die_info *die,
e7c27a73 10456 struct dwarf2_cu *cu)
6e70227d 10457{
e7c27a73 10458 struct objfile *objfile = cu->objfile;
5e2b427d 10459 struct gdbarch *gdbarch = get_objfile_arch (objfile);
c906108c
SS
10460 struct nextfield *new_field;
10461 struct attribute *attr;
10462 struct field *fp;
10463 char *fieldname = "";
10464
10465 /* Allocate a new field list entry and link it in. */
10466 new_field = (struct nextfield *) xmalloc (sizeof (struct nextfield));
b8c9b27d 10467 make_cleanup (xfree, new_field);
c906108c 10468 memset (new_field, 0, sizeof (struct nextfield));
7d0ccb61
DJ
10469
10470 if (die->tag == DW_TAG_inheritance)
10471 {
10472 new_field->next = fip->baseclasses;
10473 fip->baseclasses = new_field;
10474 }
10475 else
10476 {
10477 new_field->next = fip->fields;
10478 fip->fields = new_field;
10479 }
c906108c
SS
10480 fip->nfields++;
10481
e142c38c 10482 attr = dwarf2_attr (die, DW_AT_accessibility, cu);
c906108c
SS
10483 if (attr)
10484 new_field->accessibility = DW_UNSND (attr);
60d5a603
JK
10485 else
10486 new_field->accessibility = dwarf2_default_access_attribute (die, cu);
c906108c
SS
10487 if (new_field->accessibility != DW_ACCESS_public)
10488 fip->non_public_fields = 1;
60d5a603 10489
e142c38c 10490 attr = dwarf2_attr (die, DW_AT_virtuality, cu);
c906108c
SS
10491 if (attr)
10492 new_field->virtuality = DW_UNSND (attr);
60d5a603
JK
10493 else
10494 new_field->virtuality = DW_VIRTUALITY_none;
c906108c
SS
10495
10496 fp = &new_field->field;
a9a9bd0f 10497
e142c38c 10498 if (die->tag == DW_TAG_member && ! die_is_declaration (die, cu))
c906108c 10499 {
74ac6d43
TT
10500 LONGEST offset;
10501
a9a9bd0f 10502 /* Data member other than a C++ static data member. */
6e70227d 10503
c906108c 10504 /* Get type of field. */
e7c27a73 10505 fp->type = die_type (die, cu);
c906108c 10506
d6a843b5 10507 SET_FIELD_BITPOS (*fp, 0);
01ad7f36 10508
c906108c 10509 /* Get bit size of field (zero if none). */
e142c38c 10510 attr = dwarf2_attr (die, DW_AT_bit_size, cu);
c906108c
SS
10511 if (attr)
10512 {
10513 FIELD_BITSIZE (*fp) = DW_UNSND (attr);
10514 }
10515 else
10516 {
10517 FIELD_BITSIZE (*fp) = 0;
10518 }
10519
10520 /* Get bit offset of field. */
74ac6d43
TT
10521 if (handle_data_member_location (die, cu, &offset))
10522 SET_FIELD_BITPOS (*fp, offset * bits_per_byte);
e142c38c 10523 attr = dwarf2_attr (die, DW_AT_bit_offset, cu);
c906108c
SS
10524 if (attr)
10525 {
5e2b427d 10526 if (gdbarch_bits_big_endian (gdbarch))
c906108c
SS
10527 {
10528 /* For big endian bits, the DW_AT_bit_offset gives the
c5aa993b
JM
10529 additional bit offset from the MSB of the containing
10530 anonymous object to the MSB of the field. We don't
10531 have to do anything special since we don't need to
10532 know the size of the anonymous object. */
f41f5e61 10533 SET_FIELD_BITPOS (*fp, FIELD_BITPOS (*fp) + DW_UNSND (attr));
c906108c
SS
10534 }
10535 else
10536 {
10537 /* For little endian bits, compute the bit offset to the
c5aa993b
JM
10538 MSB of the anonymous object, subtract off the number of
10539 bits from the MSB of the field to the MSB of the
10540 object, and then subtract off the number of bits of
10541 the field itself. The result is the bit offset of
10542 the LSB of the field. */
c906108c
SS
10543 int anonymous_size;
10544 int bit_offset = DW_UNSND (attr);
10545
e142c38c 10546 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
c906108c
SS
10547 if (attr)
10548 {
10549 /* The size of the anonymous object containing
10550 the bit field is explicit, so use the
10551 indicated size (in bytes). */
10552 anonymous_size = DW_UNSND (attr);
10553 }
10554 else
10555 {
10556 /* The size of the anonymous object containing
10557 the bit field must be inferred from the type
10558 attribute of the data member containing the
10559 bit field. */
10560 anonymous_size = TYPE_LENGTH (fp->type);
10561 }
f41f5e61
PA
10562 SET_FIELD_BITPOS (*fp,
10563 (FIELD_BITPOS (*fp)
10564 + anonymous_size * bits_per_byte
10565 - bit_offset - FIELD_BITSIZE (*fp)));
c906108c
SS
10566 }
10567 }
10568
10569 /* Get name of field. */
39cbfefa
DJ
10570 fieldname = dwarf2_name (die, cu);
10571 if (fieldname == NULL)
10572 fieldname = "";
d8151005
DJ
10573
10574 /* The name is already allocated along with this objfile, so we don't
10575 need to duplicate it for the type. */
10576 fp->name = fieldname;
c906108c
SS
10577
10578 /* Change accessibility for artificial fields (e.g. virtual table
c5aa993b 10579 pointer or virtual base class pointer) to private. */
e142c38c 10580 if (dwarf2_attr (die, DW_AT_artificial, cu))
c906108c 10581 {
d48cc9dd 10582 FIELD_ARTIFICIAL (*fp) = 1;
c906108c
SS
10583 new_field->accessibility = DW_ACCESS_private;
10584 fip->non_public_fields = 1;
10585 }
10586 }
a9a9bd0f 10587 else if (die->tag == DW_TAG_member || die->tag == DW_TAG_variable)
c906108c 10588 {
a9a9bd0f
DC
10589 /* C++ static member. */
10590
10591 /* NOTE: carlton/2002-11-05: It should be a DW_TAG_member that
10592 is a declaration, but all versions of G++ as of this writing
10593 (so through at least 3.2.1) incorrectly generate
10594 DW_TAG_variable tags. */
6e70227d 10595
ff355380 10596 const char *physname;
c906108c 10597
a9a9bd0f 10598 /* Get name of field. */
39cbfefa
DJ
10599 fieldname = dwarf2_name (die, cu);
10600 if (fieldname == NULL)
c906108c
SS
10601 return;
10602
254e6b9e 10603 attr = dwarf2_attr (die, DW_AT_const_value, cu);
3863f96c
DE
10604 if (attr
10605 /* Only create a symbol if this is an external value.
10606 new_symbol checks this and puts the value in the global symbol
10607 table, which we want. If it is not external, new_symbol
10608 will try to put the value in cu->list_in_scope which is wrong. */
10609 && dwarf2_flag_true_p (die, DW_AT_external, cu))
254e6b9e
DE
10610 {
10611 /* A static const member, not much different than an enum as far as
10612 we're concerned, except that we can support more types. */
10613 new_symbol (die, NULL, cu);
10614 }
10615
2df3850c 10616 /* Get physical name. */
ff355380 10617 physname = dwarf2_physname (fieldname, die, cu);
c906108c 10618
d8151005
DJ
10619 /* The name is already allocated along with this objfile, so we don't
10620 need to duplicate it for the type. */
10621 SET_FIELD_PHYSNAME (*fp, physname ? physname : "");
e7c27a73 10622 FIELD_TYPE (*fp) = die_type (die, cu);
d8151005 10623 FIELD_NAME (*fp) = fieldname;
c906108c
SS
10624 }
10625 else if (die->tag == DW_TAG_inheritance)
10626 {
74ac6d43 10627 LONGEST offset;
d4b96c9a 10628
74ac6d43
TT
10629 /* C++ base class field. */
10630 if (handle_data_member_location (die, cu, &offset))
10631 SET_FIELD_BITPOS (*fp, offset * bits_per_byte);
c906108c 10632 FIELD_BITSIZE (*fp) = 0;
e7c27a73 10633 FIELD_TYPE (*fp) = die_type (die, cu);
c906108c
SS
10634 FIELD_NAME (*fp) = type_name_no_tag (fp->type);
10635 fip->nbaseclasses++;
10636 }
10637}
10638
98751a41
JK
10639/* Add a typedef defined in the scope of the FIP's class. */
10640
10641static void
10642dwarf2_add_typedef (struct field_info *fip, struct die_info *die,
10643 struct dwarf2_cu *cu)
6e70227d 10644{
98751a41 10645 struct objfile *objfile = cu->objfile;
98751a41
JK
10646 struct typedef_field_list *new_field;
10647 struct attribute *attr;
10648 struct typedef_field *fp;
10649 char *fieldname = "";
10650
10651 /* Allocate a new field list entry and link it in. */
10652 new_field = xzalloc (sizeof (*new_field));
10653 make_cleanup (xfree, new_field);
10654
10655 gdb_assert (die->tag == DW_TAG_typedef);
10656
10657 fp = &new_field->field;
10658
10659 /* Get name of field. */
10660 fp->name = dwarf2_name (die, cu);
10661 if (fp->name == NULL)
10662 return;
10663
10664 fp->type = read_type_die (die, cu);
10665
10666 new_field->next = fip->typedef_field_list;
10667 fip->typedef_field_list = new_field;
10668 fip->typedef_field_list_count++;
10669}
10670
c906108c
SS
10671/* Create the vector of fields, and attach it to the type. */
10672
10673static void
fba45db2 10674dwarf2_attach_fields_to_type (struct field_info *fip, struct type *type,
e7c27a73 10675 struct dwarf2_cu *cu)
c906108c
SS
10676{
10677 int nfields = fip->nfields;
10678
10679 /* Record the field count, allocate space for the array of fields,
10680 and create blank accessibility bitfields if necessary. */
10681 TYPE_NFIELDS (type) = nfields;
10682 TYPE_FIELDS (type) = (struct field *)
10683 TYPE_ALLOC (type, sizeof (struct field) * nfields);
10684 memset (TYPE_FIELDS (type), 0, sizeof (struct field) * nfields);
10685
b4ba55a1 10686 if (fip->non_public_fields && cu->language != language_ada)
c906108c
SS
10687 {
10688 ALLOCATE_CPLUS_STRUCT_TYPE (type);
10689
10690 TYPE_FIELD_PRIVATE_BITS (type) =
10691 (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
10692 B_CLRALL (TYPE_FIELD_PRIVATE_BITS (type), nfields);
10693
10694 TYPE_FIELD_PROTECTED_BITS (type) =
10695 (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
10696 B_CLRALL (TYPE_FIELD_PROTECTED_BITS (type), nfields);
10697
774b6a14
TT
10698 TYPE_FIELD_IGNORE_BITS (type) =
10699 (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
10700 B_CLRALL (TYPE_FIELD_IGNORE_BITS (type), nfields);
c906108c
SS
10701 }
10702
10703 /* If the type has baseclasses, allocate and clear a bit vector for
10704 TYPE_FIELD_VIRTUAL_BITS. */
b4ba55a1 10705 if (fip->nbaseclasses && cu->language != language_ada)
c906108c
SS
10706 {
10707 int num_bytes = B_BYTES (fip->nbaseclasses);
fe1b8b76 10708 unsigned char *pointer;
c906108c
SS
10709
10710 ALLOCATE_CPLUS_STRUCT_TYPE (type);
fe1b8b76
JB
10711 pointer = TYPE_ALLOC (type, num_bytes);
10712 TYPE_FIELD_VIRTUAL_BITS (type) = pointer;
c906108c
SS
10713 B_CLRALL (TYPE_FIELD_VIRTUAL_BITS (type), fip->nbaseclasses);
10714 TYPE_N_BASECLASSES (type) = fip->nbaseclasses;
10715 }
10716
3e43a32a
MS
10717 /* Copy the saved-up fields into the field vector. Start from the head of
10718 the list, adding to the tail of the field array, so that they end up in
10719 the same order in the array in which they were added to the list. */
c906108c
SS
10720 while (nfields-- > 0)
10721 {
7d0ccb61
DJ
10722 struct nextfield *fieldp;
10723
10724 if (fip->fields)
10725 {
10726 fieldp = fip->fields;
10727 fip->fields = fieldp->next;
10728 }
10729 else
10730 {
10731 fieldp = fip->baseclasses;
10732 fip->baseclasses = fieldp->next;
10733 }
10734
10735 TYPE_FIELD (type, nfields) = fieldp->field;
10736 switch (fieldp->accessibility)
c906108c 10737 {
c5aa993b 10738 case DW_ACCESS_private:
b4ba55a1
JB
10739 if (cu->language != language_ada)
10740 SET_TYPE_FIELD_PRIVATE (type, nfields);
c5aa993b 10741 break;
c906108c 10742
c5aa993b 10743 case DW_ACCESS_protected:
b4ba55a1
JB
10744 if (cu->language != language_ada)
10745 SET_TYPE_FIELD_PROTECTED (type, nfields);
c5aa993b 10746 break;
c906108c 10747
c5aa993b
JM
10748 case DW_ACCESS_public:
10749 break;
c906108c 10750
c5aa993b
JM
10751 default:
10752 /* Unknown accessibility. Complain and treat it as public. */
10753 {
e2e0b3e5 10754 complaint (&symfile_complaints, _("unsupported accessibility %d"),
7d0ccb61 10755 fieldp->accessibility);
c5aa993b
JM
10756 }
10757 break;
c906108c
SS
10758 }
10759 if (nfields < fip->nbaseclasses)
10760 {
7d0ccb61 10761 switch (fieldp->virtuality)
c906108c 10762 {
c5aa993b
JM
10763 case DW_VIRTUALITY_virtual:
10764 case DW_VIRTUALITY_pure_virtual:
b4ba55a1 10765 if (cu->language == language_ada)
a73c6dcd 10766 error (_("unexpected virtuality in component of Ada type"));
c5aa993b
JM
10767 SET_TYPE_FIELD_VIRTUAL (type, nfields);
10768 break;
c906108c
SS
10769 }
10770 }
c906108c
SS
10771 }
10772}
10773
c906108c
SS
10774/* Add a member function to the proper fieldlist. */
10775
10776static void
107d2387 10777dwarf2_add_member_fn (struct field_info *fip, struct die_info *die,
e7c27a73 10778 struct type *type, struct dwarf2_cu *cu)
c906108c 10779{
e7c27a73 10780 struct objfile *objfile = cu->objfile;
c906108c
SS
10781 struct attribute *attr;
10782 struct fnfieldlist *flp;
10783 int i;
10784 struct fn_field *fnp;
10785 char *fieldname;
c906108c 10786 struct nextfnfield *new_fnfield;
f792889a 10787 struct type *this_type;
60d5a603 10788 enum dwarf_access_attribute accessibility;
c906108c 10789
b4ba55a1 10790 if (cu->language == language_ada)
a73c6dcd 10791 error (_("unexpected member function in Ada type"));
b4ba55a1 10792
2df3850c 10793 /* Get name of member function. */
39cbfefa
DJ
10794 fieldname = dwarf2_name (die, cu);
10795 if (fieldname == NULL)
2df3850c 10796 return;
c906108c 10797
c906108c
SS
10798 /* Look up member function name in fieldlist. */
10799 for (i = 0; i < fip->nfnfields; i++)
10800 {
27bfe10e 10801 if (strcmp (fip->fnfieldlists[i].name, fieldname) == 0)
c906108c
SS
10802 break;
10803 }
10804
10805 /* Create new list element if necessary. */
10806 if (i < fip->nfnfields)
10807 flp = &fip->fnfieldlists[i];
10808 else
10809 {
10810 if ((fip->nfnfields % DW_FIELD_ALLOC_CHUNK) == 0)
10811 {
10812 fip->fnfieldlists = (struct fnfieldlist *)
10813 xrealloc (fip->fnfieldlists,
10814 (fip->nfnfields + DW_FIELD_ALLOC_CHUNK)
c5aa993b 10815 * sizeof (struct fnfieldlist));
c906108c 10816 if (fip->nfnfields == 0)
c13c43fd 10817 make_cleanup (free_current_contents, &fip->fnfieldlists);
c906108c
SS
10818 }
10819 flp = &fip->fnfieldlists[fip->nfnfields];
10820 flp->name = fieldname;
10821 flp->length = 0;
10822 flp->head = NULL;
3da10d80 10823 i = fip->nfnfields++;
c906108c
SS
10824 }
10825
10826 /* Create a new member function field and chain it to the field list
0963b4bd 10827 entry. */
c906108c 10828 new_fnfield = (struct nextfnfield *) xmalloc (sizeof (struct nextfnfield));
b8c9b27d 10829 make_cleanup (xfree, new_fnfield);
c906108c
SS
10830 memset (new_fnfield, 0, sizeof (struct nextfnfield));
10831 new_fnfield->next = flp->head;
10832 flp->head = new_fnfield;
10833 flp->length++;
10834
10835 /* Fill in the member function field info. */
10836 fnp = &new_fnfield->fnfield;
3da10d80
KS
10837
10838 /* Delay processing of the physname until later. */
10839 if (cu->language == language_cplus || cu->language == language_java)
10840 {
10841 add_to_method_list (type, i, flp->length - 1, fieldname,
10842 die, cu);
10843 }
10844 else
10845 {
1d06ead6 10846 const char *physname = dwarf2_physname (fieldname, die, cu);
3da10d80
KS
10847 fnp->physname = physname ? physname : "";
10848 }
10849
c906108c 10850 fnp->type = alloc_type (objfile);
f792889a
DJ
10851 this_type = read_type_die (die, cu);
10852 if (this_type && TYPE_CODE (this_type) == TYPE_CODE_FUNC)
c906108c 10853 {
f792889a 10854 int nparams = TYPE_NFIELDS (this_type);
c906108c 10855
f792889a 10856 /* TYPE is the domain of this method, and THIS_TYPE is the type
e26fb1d7
DC
10857 of the method itself (TYPE_CODE_METHOD). */
10858 smash_to_method_type (fnp->type, type,
f792889a
DJ
10859 TYPE_TARGET_TYPE (this_type),
10860 TYPE_FIELDS (this_type),
10861 TYPE_NFIELDS (this_type),
10862 TYPE_VARARGS (this_type));
c906108c
SS
10863
10864 /* Handle static member functions.
c5aa993b 10865 Dwarf2 has no clean way to discern C++ static and non-static
0963b4bd
MS
10866 member functions. G++ helps GDB by marking the first
10867 parameter for non-static member functions (which is the this
10868 pointer) as artificial. We obtain this information from
10869 read_subroutine_type via TYPE_FIELD_ARTIFICIAL. */
f792889a 10870 if (nparams == 0 || TYPE_FIELD_ARTIFICIAL (this_type, 0) == 0)
c906108c
SS
10871 fnp->voffset = VOFFSET_STATIC;
10872 }
10873 else
e2e0b3e5 10874 complaint (&symfile_complaints, _("member function type missing for '%s'"),
3da10d80 10875 dwarf2_full_name (fieldname, die, cu));
c906108c
SS
10876
10877 /* Get fcontext from DW_AT_containing_type if present. */
e142c38c 10878 if (dwarf2_attr (die, DW_AT_containing_type, cu) != NULL)
e7c27a73 10879 fnp->fcontext = die_containing_type (die, cu);
c906108c 10880
3e43a32a
MS
10881 /* dwarf2 doesn't have stubbed physical names, so the setting of is_const and
10882 is_volatile is irrelevant, as it is needed by gdb_mangle_name only. */
c906108c
SS
10883
10884 /* Get accessibility. */
e142c38c 10885 attr = dwarf2_attr (die, DW_AT_accessibility, cu);
c906108c 10886 if (attr)
60d5a603
JK
10887 accessibility = DW_UNSND (attr);
10888 else
10889 accessibility = dwarf2_default_access_attribute (die, cu);
10890 switch (accessibility)
c906108c 10891 {
60d5a603
JK
10892 case DW_ACCESS_private:
10893 fnp->is_private = 1;
10894 break;
10895 case DW_ACCESS_protected:
10896 fnp->is_protected = 1;
10897 break;
c906108c
SS
10898 }
10899
b02dede2 10900 /* Check for artificial methods. */
e142c38c 10901 attr = dwarf2_attr (die, DW_AT_artificial, cu);
b02dede2
DJ
10902 if (attr && DW_UNSND (attr) != 0)
10903 fnp->is_artificial = 1;
10904
0d564a31 10905 /* Get index in virtual function table if it is a virtual member
aec5aa8b
TT
10906 function. For older versions of GCC, this is an offset in the
10907 appropriate virtual table, as specified by DW_AT_containing_type.
10908 For everyone else, it is an expression to be evaluated relative
0d564a31
DJ
10909 to the object address. */
10910
e142c38c 10911 attr = dwarf2_attr (die, DW_AT_vtable_elem_location, cu);
aec5aa8b 10912 if (attr)
8e19ed76 10913 {
aec5aa8b 10914 if (attr_form_is_block (attr) && DW_BLOCK (attr)->size > 0)
8e19ed76 10915 {
aec5aa8b
TT
10916 if (DW_BLOCK (attr)->data[0] == DW_OP_constu)
10917 {
10918 /* Old-style GCC. */
10919 fnp->voffset = decode_locdesc (DW_BLOCK (attr), cu) + 2;
10920 }
10921 else if (DW_BLOCK (attr)->data[0] == DW_OP_deref
10922 || (DW_BLOCK (attr)->size > 1
10923 && DW_BLOCK (attr)->data[0] == DW_OP_deref_size
10924 && DW_BLOCK (attr)->data[1] == cu->header.addr_size))
10925 {
10926 struct dwarf_block blk;
10927 int offset;
10928
10929 offset = (DW_BLOCK (attr)->data[0] == DW_OP_deref
10930 ? 1 : 2);
10931 blk.size = DW_BLOCK (attr)->size - offset;
10932 blk.data = DW_BLOCK (attr)->data + offset;
10933 fnp->voffset = decode_locdesc (DW_BLOCK (attr), cu);
10934 if ((fnp->voffset % cu->header.addr_size) != 0)
10935 dwarf2_complex_location_expr_complaint ();
10936 else
10937 fnp->voffset /= cu->header.addr_size;
10938 fnp->voffset += 2;
10939 }
10940 else
10941 dwarf2_complex_location_expr_complaint ();
10942
10943 if (!fnp->fcontext)
10944 fnp->fcontext = TYPE_TARGET_TYPE (TYPE_FIELD_TYPE (this_type, 0));
10945 }
3690dd37 10946 else if (attr_form_is_section_offset (attr))
8e19ed76 10947 {
4d3c2250 10948 dwarf2_complex_location_expr_complaint ();
8e19ed76
PS
10949 }
10950 else
10951 {
4d3c2250
KB
10952 dwarf2_invalid_attrib_class_complaint ("DW_AT_vtable_elem_location",
10953 fieldname);
8e19ed76 10954 }
0d564a31 10955 }
d48cc9dd
DJ
10956 else
10957 {
10958 attr = dwarf2_attr (die, DW_AT_virtuality, cu);
10959 if (attr && DW_UNSND (attr))
10960 {
10961 /* GCC does this, as of 2008-08-25; PR debug/37237. */
10962 complaint (&symfile_complaints,
3e43a32a
MS
10963 _("Member function \"%s\" (offset %d) is virtual "
10964 "but the vtable offset is not specified"),
b64f50a1 10965 fieldname, die->offset.sect_off);
9655fd1a 10966 ALLOCATE_CPLUS_STRUCT_TYPE (type);
d48cc9dd
DJ
10967 TYPE_CPLUS_DYNAMIC (type) = 1;
10968 }
10969 }
c906108c
SS
10970}
10971
10972/* Create the vector of member function fields, and attach it to the type. */
10973
10974static void
fba45db2 10975dwarf2_attach_fn_fields_to_type (struct field_info *fip, struct type *type,
e7c27a73 10976 struct dwarf2_cu *cu)
c906108c
SS
10977{
10978 struct fnfieldlist *flp;
c906108c
SS
10979 int i;
10980
b4ba55a1 10981 if (cu->language == language_ada)
a73c6dcd 10982 error (_("unexpected member functions in Ada type"));
b4ba55a1 10983
c906108c
SS
10984 ALLOCATE_CPLUS_STRUCT_TYPE (type);
10985 TYPE_FN_FIELDLISTS (type) = (struct fn_fieldlist *)
10986 TYPE_ALLOC (type, sizeof (struct fn_fieldlist) * fip->nfnfields);
10987
10988 for (i = 0, flp = fip->fnfieldlists; i < fip->nfnfields; i++, flp++)
10989 {
10990 struct nextfnfield *nfp = flp->head;
10991 struct fn_fieldlist *fn_flp = &TYPE_FN_FIELDLIST (type, i);
10992 int k;
10993
10994 TYPE_FN_FIELDLIST_NAME (type, i) = flp->name;
10995 TYPE_FN_FIELDLIST_LENGTH (type, i) = flp->length;
10996 fn_flp->fn_fields = (struct fn_field *)
10997 TYPE_ALLOC (type, sizeof (struct fn_field) * flp->length);
10998 for (k = flp->length; (k--, nfp); nfp = nfp->next)
c5aa993b 10999 fn_flp->fn_fields[k] = nfp->fnfield;
c906108c
SS
11000 }
11001
11002 TYPE_NFN_FIELDS (type) = fip->nfnfields;
c906108c
SS
11003}
11004
1168df01
JB
11005/* Returns non-zero if NAME is the name of a vtable member in CU's
11006 language, zero otherwise. */
11007static int
11008is_vtable_name (const char *name, struct dwarf2_cu *cu)
11009{
11010 static const char vptr[] = "_vptr";
987504bb 11011 static const char vtable[] = "vtable";
1168df01 11012
987504bb
JJ
11013 /* Look for the C++ and Java forms of the vtable. */
11014 if ((cu->language == language_java
11015 && strncmp (name, vtable, sizeof (vtable) - 1) == 0)
11016 || (strncmp (name, vptr, sizeof (vptr) - 1) == 0
11017 && is_cplus_marker (name[sizeof (vptr) - 1])))
1168df01
JB
11018 return 1;
11019
11020 return 0;
11021}
11022
c0dd20ea 11023/* GCC outputs unnamed structures that are really pointers to member
0b92b5bb
TT
11024 functions, with the ABI-specified layout. If TYPE describes
11025 such a structure, smash it into a member function type.
61049d3b
DJ
11026
11027 GCC shouldn't do this; it should just output pointer to member DIEs.
11028 This is GCC PR debug/28767. */
c0dd20ea 11029
0b92b5bb
TT
11030static void
11031quirk_gcc_member_function_pointer (struct type *type, struct objfile *objfile)
c0dd20ea 11032{
0b92b5bb 11033 struct type *pfn_type, *domain_type, *new_type;
c0dd20ea
DJ
11034
11035 /* Check for a structure with no name and two children. */
0b92b5bb
TT
11036 if (TYPE_CODE (type) != TYPE_CODE_STRUCT || TYPE_NFIELDS (type) != 2)
11037 return;
c0dd20ea
DJ
11038
11039 /* Check for __pfn and __delta members. */
0b92b5bb
TT
11040 if (TYPE_FIELD_NAME (type, 0) == NULL
11041 || strcmp (TYPE_FIELD_NAME (type, 0), "__pfn") != 0
11042 || TYPE_FIELD_NAME (type, 1) == NULL
11043 || strcmp (TYPE_FIELD_NAME (type, 1), "__delta") != 0)
11044 return;
c0dd20ea
DJ
11045
11046 /* Find the type of the method. */
0b92b5bb 11047 pfn_type = TYPE_FIELD_TYPE (type, 0);
c0dd20ea
DJ
11048 if (pfn_type == NULL
11049 || TYPE_CODE (pfn_type) != TYPE_CODE_PTR
11050 || TYPE_CODE (TYPE_TARGET_TYPE (pfn_type)) != TYPE_CODE_FUNC)
0b92b5bb 11051 return;
c0dd20ea
DJ
11052
11053 /* Look for the "this" argument. */
11054 pfn_type = TYPE_TARGET_TYPE (pfn_type);
11055 if (TYPE_NFIELDS (pfn_type) == 0
0b92b5bb 11056 /* || TYPE_FIELD_TYPE (pfn_type, 0) == NULL */
c0dd20ea 11057 || TYPE_CODE (TYPE_FIELD_TYPE (pfn_type, 0)) != TYPE_CODE_PTR)
0b92b5bb 11058 return;
c0dd20ea
DJ
11059
11060 domain_type = TYPE_TARGET_TYPE (TYPE_FIELD_TYPE (pfn_type, 0));
0b92b5bb
TT
11061 new_type = alloc_type (objfile);
11062 smash_to_method_type (new_type, domain_type, TYPE_TARGET_TYPE (pfn_type),
c0dd20ea
DJ
11063 TYPE_FIELDS (pfn_type), TYPE_NFIELDS (pfn_type),
11064 TYPE_VARARGS (pfn_type));
0b92b5bb 11065 smash_to_methodptr_type (type, new_type);
c0dd20ea 11066}
1168df01 11067
685b1105
JK
11068/* Return non-zero if the CU's PRODUCER string matches the Intel C/C++ compiler
11069 (icc). */
11070
11071static int
11072producer_is_icc (struct dwarf2_cu *cu)
11073{
11074 if (!cu->checked_producer)
11075 check_producer (cu);
11076
11077 return cu->producer_is_icc;
11078}
11079
c906108c 11080/* Called when we find the DIE that starts a structure or union scope
c767944b
DJ
11081 (definition) to create a type for the structure or union. Fill in
11082 the type's name and general properties; the members will not be
11083 processed until process_structure_type.
c906108c 11084
c767944b
DJ
11085 NOTE: we need to call these functions regardless of whether or not the
11086 DIE has a DW_AT_name attribute, since it might be an anonymous
c906108c
SS
11087 structure or union. This gets the type entered into our set of
11088 user defined types.
11089
11090 However, if the structure is incomplete (an opaque struct/union)
11091 then suppress creating a symbol table entry for it since gdb only
11092 wants to find the one with the complete definition. Note that if
11093 it is complete, we just call new_symbol, which does it's own
11094 checking about whether the struct/union is anonymous or not (and
11095 suppresses creating a symbol table entry itself). */
11096
f792889a 11097static struct type *
134d01f1 11098read_structure_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 11099{
e7c27a73 11100 struct objfile *objfile = cu->objfile;
c906108c
SS
11101 struct type *type;
11102 struct attribute *attr;
39cbfefa 11103 char *name;
c906108c 11104
348e048f
DE
11105 /* If the definition of this type lives in .debug_types, read that type.
11106 Don't follow DW_AT_specification though, that will take us back up
11107 the chain and we want to go down. */
45e58e77 11108 attr = dwarf2_attr_no_follow (die, DW_AT_signature);
348e048f
DE
11109 if (attr)
11110 {
11111 struct dwarf2_cu *type_cu = cu;
11112 struct die_info *type_die = follow_die_ref_or_sig (die, attr, &type_cu);
9a619af0 11113
348e048f
DE
11114 /* We could just recurse on read_structure_type, but we need to call
11115 get_die_type to ensure only one type for this DIE is created.
11116 This is important, for example, because for c++ classes we need
11117 TYPE_NAME set which is only done by new_symbol. Blech. */
11118 type = read_type_die (type_die, type_cu);
9dc481d3
DE
11119
11120 /* TYPE_CU may not be the same as CU.
11121 Ensure TYPE is recorded in CU's type_hash table. */
348e048f
DE
11122 return set_die_type (die, type, cu);
11123 }
11124
c0dd20ea 11125 type = alloc_type (objfile);
c906108c 11126 INIT_CPLUS_SPECIFIC (type);
93311388 11127
39cbfefa
DJ
11128 name = dwarf2_name (die, cu);
11129 if (name != NULL)
c906108c 11130 {
987504bb
JJ
11131 if (cu->language == language_cplus
11132 || cu->language == language_java)
63d06c5c 11133 {
3da10d80
KS
11134 char *full_name = (char *) dwarf2_full_name (name, die, cu);
11135
11136 /* dwarf2_full_name might have already finished building the DIE's
11137 type. If so, there is no need to continue. */
11138 if (get_die_type (die, cu) != NULL)
11139 return get_die_type (die, cu);
11140
11141 TYPE_TAG_NAME (type) = full_name;
94af9270
KS
11142 if (die->tag == DW_TAG_structure_type
11143 || die->tag == DW_TAG_class_type)
11144 TYPE_NAME (type) = TYPE_TAG_NAME (type);
63d06c5c
DC
11145 }
11146 else
11147 {
d8151005
DJ
11148 /* The name is already allocated along with this objfile, so
11149 we don't need to duplicate it for the type. */
94af9270
KS
11150 TYPE_TAG_NAME (type) = (char *) name;
11151 if (die->tag == DW_TAG_class_type)
11152 TYPE_NAME (type) = TYPE_TAG_NAME (type);
63d06c5c 11153 }
c906108c
SS
11154 }
11155
11156 if (die->tag == DW_TAG_structure_type)
11157 {
11158 TYPE_CODE (type) = TYPE_CODE_STRUCT;
11159 }
11160 else if (die->tag == DW_TAG_union_type)
11161 {
11162 TYPE_CODE (type) = TYPE_CODE_UNION;
11163 }
11164 else
11165 {
c906108c
SS
11166 TYPE_CODE (type) = TYPE_CODE_CLASS;
11167 }
11168
0cc2414c
TT
11169 if (cu->language == language_cplus && die->tag == DW_TAG_class_type)
11170 TYPE_DECLARED_CLASS (type) = 1;
11171
e142c38c 11172 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
c906108c
SS
11173 if (attr)
11174 {
11175 TYPE_LENGTH (type) = DW_UNSND (attr);
11176 }
11177 else
11178 {
11179 TYPE_LENGTH (type) = 0;
11180 }
11181
685b1105
JK
11182 if (producer_is_icc (cu))
11183 {
11184 /* ICC does not output the required DW_AT_declaration
11185 on incomplete types, but gives them a size of zero. */
11186 }
11187 else
11188 TYPE_STUB_SUPPORTED (type) = 1;
11189
dc718098 11190 if (die_is_declaration (die, cu))
876cecd0 11191 TYPE_STUB (type) = 1;
a6c727b2
DJ
11192 else if (attr == NULL && die->child == NULL
11193 && producer_is_realview (cu->producer))
11194 /* RealView does not output the required DW_AT_declaration
11195 on incomplete types. */
11196 TYPE_STUB (type) = 1;
dc718098 11197
c906108c
SS
11198 /* We need to add the type field to the die immediately so we don't
11199 infinitely recurse when dealing with pointers to the structure
0963b4bd 11200 type within the structure itself. */
1c379e20 11201 set_die_type (die, type, cu);
c906108c 11202
7e314c57
JK
11203 /* set_die_type should be already done. */
11204 set_descriptive_type (type, die, cu);
11205
c767944b
DJ
11206 return type;
11207}
11208
11209/* Finish creating a structure or union type, including filling in
11210 its members and creating a symbol for it. */
11211
11212static void
11213process_structure_scope (struct die_info *die, struct dwarf2_cu *cu)
11214{
11215 struct objfile *objfile = cu->objfile;
11216 struct die_info *child_die = die->child;
11217 struct type *type;
11218
11219 type = get_die_type (die, cu);
11220 if (type == NULL)
11221 type = read_structure_type (die, cu);
11222
e142c38c 11223 if (die->child != NULL && ! die_is_declaration (die, cu))
c906108c
SS
11224 {
11225 struct field_info fi;
11226 struct die_info *child_die;
34eaf542 11227 VEC (symbolp) *template_args = NULL;
c767944b 11228 struct cleanup *back_to = make_cleanup (null_cleanup, 0);
c906108c
SS
11229
11230 memset (&fi, 0, sizeof (struct field_info));
11231
639d11d3 11232 child_die = die->child;
c906108c
SS
11233
11234 while (child_die && child_die->tag)
11235 {
a9a9bd0f
DC
11236 if (child_die->tag == DW_TAG_member
11237 || child_die->tag == DW_TAG_variable)
c906108c 11238 {
a9a9bd0f
DC
11239 /* NOTE: carlton/2002-11-05: A C++ static data member
11240 should be a DW_TAG_member that is a declaration, but
11241 all versions of G++ as of this writing (so through at
11242 least 3.2.1) incorrectly generate DW_TAG_variable
11243 tags for them instead. */
e7c27a73 11244 dwarf2_add_field (&fi, child_die, cu);
c906108c 11245 }
8713b1b1 11246 else if (child_die->tag == DW_TAG_subprogram)
c906108c 11247 {
0963b4bd 11248 /* C++ member function. */
e7c27a73 11249 dwarf2_add_member_fn (&fi, child_die, type, cu);
c906108c
SS
11250 }
11251 else if (child_die->tag == DW_TAG_inheritance)
11252 {
11253 /* C++ base class field. */
e7c27a73 11254 dwarf2_add_field (&fi, child_die, cu);
c906108c 11255 }
98751a41
JK
11256 else if (child_die->tag == DW_TAG_typedef)
11257 dwarf2_add_typedef (&fi, child_die, cu);
34eaf542
TT
11258 else if (child_die->tag == DW_TAG_template_type_param
11259 || child_die->tag == DW_TAG_template_value_param)
11260 {
11261 struct symbol *arg = new_symbol (child_die, NULL, cu);
11262
f1078f66
DJ
11263 if (arg != NULL)
11264 VEC_safe_push (symbolp, template_args, arg);
34eaf542
TT
11265 }
11266
c906108c
SS
11267 child_die = sibling_die (child_die);
11268 }
11269
34eaf542
TT
11270 /* Attach template arguments to type. */
11271 if (! VEC_empty (symbolp, template_args))
11272 {
11273 ALLOCATE_CPLUS_STRUCT_TYPE (type);
11274 TYPE_N_TEMPLATE_ARGUMENTS (type)
11275 = VEC_length (symbolp, template_args);
11276 TYPE_TEMPLATE_ARGUMENTS (type)
11277 = obstack_alloc (&objfile->objfile_obstack,
11278 (TYPE_N_TEMPLATE_ARGUMENTS (type)
11279 * sizeof (struct symbol *)));
11280 memcpy (TYPE_TEMPLATE_ARGUMENTS (type),
11281 VEC_address (symbolp, template_args),
11282 (TYPE_N_TEMPLATE_ARGUMENTS (type)
11283 * sizeof (struct symbol *)));
11284 VEC_free (symbolp, template_args);
11285 }
11286
c906108c
SS
11287 /* Attach fields and member functions to the type. */
11288 if (fi.nfields)
e7c27a73 11289 dwarf2_attach_fields_to_type (&fi, type, cu);
c906108c
SS
11290 if (fi.nfnfields)
11291 {
e7c27a73 11292 dwarf2_attach_fn_fields_to_type (&fi, type, cu);
c906108c 11293
c5aa993b 11294 /* Get the type which refers to the base class (possibly this
c906108c 11295 class itself) which contains the vtable pointer for the current
0d564a31
DJ
11296 class from the DW_AT_containing_type attribute. This use of
11297 DW_AT_containing_type is a GNU extension. */
c906108c 11298
e142c38c 11299 if (dwarf2_attr (die, DW_AT_containing_type, cu) != NULL)
c906108c 11300 {
e7c27a73 11301 struct type *t = die_containing_type (die, cu);
c906108c
SS
11302
11303 TYPE_VPTR_BASETYPE (type) = t;
11304 if (type == t)
11305 {
c906108c
SS
11306 int i;
11307
11308 /* Our own class provides vtbl ptr. */
11309 for (i = TYPE_NFIELDS (t) - 1;
11310 i >= TYPE_N_BASECLASSES (t);
11311 --i)
11312 {
0d5cff50 11313 const char *fieldname = TYPE_FIELD_NAME (t, i);
c906108c 11314
1168df01 11315 if (is_vtable_name (fieldname, cu))
c906108c
SS
11316 {
11317 TYPE_VPTR_FIELDNO (type) = i;
11318 break;
11319 }
11320 }
11321
11322 /* Complain if virtual function table field not found. */
11323 if (i < TYPE_N_BASECLASSES (t))
4d3c2250 11324 complaint (&symfile_complaints,
3e43a32a
MS
11325 _("virtual function table pointer "
11326 "not found when defining class '%s'"),
4d3c2250
KB
11327 TYPE_TAG_NAME (type) ? TYPE_TAG_NAME (type) :
11328 "");
c906108c
SS
11329 }
11330 else
11331 {
11332 TYPE_VPTR_FIELDNO (type) = TYPE_VPTR_FIELDNO (t);
11333 }
11334 }
f6235d4c
EZ
11335 else if (cu->producer
11336 && strncmp (cu->producer,
11337 "IBM(R) XL C/C++ Advanced Edition", 32) == 0)
11338 {
11339 /* The IBM XLC compiler does not provide direct indication
11340 of the containing type, but the vtable pointer is
11341 always named __vfp. */
11342
11343 int i;
11344
11345 for (i = TYPE_NFIELDS (type) - 1;
11346 i >= TYPE_N_BASECLASSES (type);
11347 --i)
11348 {
11349 if (strcmp (TYPE_FIELD_NAME (type, i), "__vfp") == 0)
11350 {
11351 TYPE_VPTR_FIELDNO (type) = i;
11352 TYPE_VPTR_BASETYPE (type) = type;
11353 break;
11354 }
11355 }
11356 }
c906108c 11357 }
98751a41
JK
11358
11359 /* Copy fi.typedef_field_list linked list elements content into the
11360 allocated array TYPE_TYPEDEF_FIELD_ARRAY (type). */
11361 if (fi.typedef_field_list)
11362 {
11363 int i = fi.typedef_field_list_count;
11364
a0d7a4ff 11365 ALLOCATE_CPLUS_STRUCT_TYPE (type);
98751a41
JK
11366 TYPE_TYPEDEF_FIELD_ARRAY (type)
11367 = TYPE_ALLOC (type, sizeof (TYPE_TYPEDEF_FIELD (type, 0)) * i);
11368 TYPE_TYPEDEF_FIELD_COUNT (type) = i;
11369
11370 /* Reverse the list order to keep the debug info elements order. */
11371 while (--i >= 0)
11372 {
11373 struct typedef_field *dest, *src;
6e70227d 11374
98751a41
JK
11375 dest = &TYPE_TYPEDEF_FIELD (type, i);
11376 src = &fi.typedef_field_list->field;
11377 fi.typedef_field_list = fi.typedef_field_list->next;
11378 *dest = *src;
11379 }
11380 }
c767944b
DJ
11381
11382 do_cleanups (back_to);
eb2a6f42
TT
11383
11384 if (HAVE_CPLUS_STRUCT (type))
11385 TYPE_CPLUS_REALLY_JAVA (type) = cu->language == language_java;
c906108c 11386 }
63d06c5c 11387
bb5ed363 11388 quirk_gcc_member_function_pointer (type, objfile);
0b92b5bb 11389
90aeadfc
DC
11390 /* NOTE: carlton/2004-03-16: GCC 3.4 (or at least one of its
11391 snapshots) has been known to create a die giving a declaration
11392 for a class that has, as a child, a die giving a definition for a
11393 nested class. So we have to process our children even if the
11394 current die is a declaration. Normally, of course, a declaration
11395 won't have any children at all. */
134d01f1 11396
90aeadfc
DC
11397 while (child_die != NULL && child_die->tag)
11398 {
11399 if (child_die->tag == DW_TAG_member
11400 || child_die->tag == DW_TAG_variable
34eaf542
TT
11401 || child_die->tag == DW_TAG_inheritance
11402 || child_die->tag == DW_TAG_template_value_param
11403 || child_die->tag == DW_TAG_template_type_param)
134d01f1 11404 {
90aeadfc 11405 /* Do nothing. */
134d01f1 11406 }
90aeadfc
DC
11407 else
11408 process_die (child_die, cu);
134d01f1 11409
90aeadfc 11410 child_die = sibling_die (child_die);
134d01f1
DJ
11411 }
11412
fa4028e9
JB
11413 /* Do not consider external references. According to the DWARF standard,
11414 these DIEs are identified by the fact that they have no byte_size
11415 attribute, and a declaration attribute. */
11416 if (dwarf2_attr (die, DW_AT_byte_size, cu) != NULL
11417 || !die_is_declaration (die, cu))
c767944b 11418 new_symbol (die, type, cu);
134d01f1
DJ
11419}
11420
11421/* Given a DW_AT_enumeration_type die, set its type. We do not
11422 complete the type's fields yet, or create any symbols. */
c906108c 11423
f792889a 11424static struct type *
134d01f1 11425read_enumeration_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 11426{
e7c27a73 11427 struct objfile *objfile = cu->objfile;
c906108c 11428 struct type *type;
c906108c 11429 struct attribute *attr;
0114d602 11430 const char *name;
134d01f1 11431
348e048f
DE
11432 /* If the definition of this type lives in .debug_types, read that type.
11433 Don't follow DW_AT_specification though, that will take us back up
11434 the chain and we want to go down. */
45e58e77 11435 attr = dwarf2_attr_no_follow (die, DW_AT_signature);
348e048f
DE
11436 if (attr)
11437 {
11438 struct dwarf2_cu *type_cu = cu;
11439 struct die_info *type_die = follow_die_ref_or_sig (die, attr, &type_cu);
9a619af0 11440
348e048f 11441 type = read_type_die (type_die, type_cu);
9dc481d3
DE
11442
11443 /* TYPE_CU may not be the same as CU.
11444 Ensure TYPE is recorded in CU's type_hash table. */
348e048f
DE
11445 return set_die_type (die, type, cu);
11446 }
11447
c906108c
SS
11448 type = alloc_type (objfile);
11449
11450 TYPE_CODE (type) = TYPE_CODE_ENUM;
94af9270 11451 name = dwarf2_full_name (NULL, die, cu);
39cbfefa 11452 if (name != NULL)
0114d602 11453 TYPE_TAG_NAME (type) = (char *) name;
c906108c 11454
e142c38c 11455 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
c906108c
SS
11456 if (attr)
11457 {
11458 TYPE_LENGTH (type) = DW_UNSND (attr);
11459 }
11460 else
11461 {
11462 TYPE_LENGTH (type) = 0;
11463 }
11464
137033e9
JB
11465 /* The enumeration DIE can be incomplete. In Ada, any type can be
11466 declared as private in the package spec, and then defined only
11467 inside the package body. Such types are known as Taft Amendment
11468 Types. When another package uses such a type, an incomplete DIE
11469 may be generated by the compiler. */
02eb380e 11470 if (die_is_declaration (die, cu))
876cecd0 11471 TYPE_STUB (type) = 1;
02eb380e 11472
f792889a 11473 return set_die_type (die, type, cu);
134d01f1
DJ
11474}
11475
11476/* Given a pointer to a die which begins an enumeration, process all
11477 the dies that define the members of the enumeration, and create the
11478 symbol for the enumeration type.
11479
11480 NOTE: We reverse the order of the element list. */
11481
11482static void
11483process_enumeration_scope (struct die_info *die, struct dwarf2_cu *cu)
11484{
f792889a 11485 struct type *this_type;
134d01f1 11486
f792889a
DJ
11487 this_type = get_die_type (die, cu);
11488 if (this_type == NULL)
11489 this_type = read_enumeration_type (die, cu);
9dc481d3 11490
639d11d3 11491 if (die->child != NULL)
c906108c 11492 {
9dc481d3
DE
11493 struct die_info *child_die;
11494 struct symbol *sym;
11495 struct field *fields = NULL;
11496 int num_fields = 0;
11497 int unsigned_enum = 1;
11498 char *name;
cafec441
TT
11499 int flag_enum = 1;
11500 ULONGEST mask = 0;
9dc481d3 11501
639d11d3 11502 child_die = die->child;
c906108c
SS
11503 while (child_die && child_die->tag)
11504 {
11505 if (child_die->tag != DW_TAG_enumerator)
11506 {
e7c27a73 11507 process_die (child_die, cu);
c906108c
SS
11508 }
11509 else
11510 {
39cbfefa
DJ
11511 name = dwarf2_name (child_die, cu);
11512 if (name)
c906108c 11513 {
f792889a 11514 sym = new_symbol (child_die, this_type, cu);
c906108c 11515 if (SYMBOL_VALUE (sym) < 0)
cafec441
TT
11516 {
11517 unsigned_enum = 0;
11518 flag_enum = 0;
11519 }
11520 else if ((mask & SYMBOL_VALUE (sym)) != 0)
11521 flag_enum = 0;
11522 else
11523 mask |= SYMBOL_VALUE (sym);
c906108c
SS
11524
11525 if ((num_fields % DW_FIELD_ALLOC_CHUNK) == 0)
11526 {
11527 fields = (struct field *)
11528 xrealloc (fields,
11529 (num_fields + DW_FIELD_ALLOC_CHUNK)
c5aa993b 11530 * sizeof (struct field));
c906108c
SS
11531 }
11532
3567439c 11533 FIELD_NAME (fields[num_fields]) = SYMBOL_LINKAGE_NAME (sym);
c906108c 11534 FIELD_TYPE (fields[num_fields]) = NULL;
14e75d8e 11535 SET_FIELD_ENUMVAL (fields[num_fields], SYMBOL_VALUE (sym));
c906108c
SS
11536 FIELD_BITSIZE (fields[num_fields]) = 0;
11537
11538 num_fields++;
11539 }
11540 }
11541
11542 child_die = sibling_die (child_die);
11543 }
11544
11545 if (num_fields)
11546 {
f792889a
DJ
11547 TYPE_NFIELDS (this_type) = num_fields;
11548 TYPE_FIELDS (this_type) = (struct field *)
11549 TYPE_ALLOC (this_type, sizeof (struct field) * num_fields);
11550 memcpy (TYPE_FIELDS (this_type), fields,
c906108c 11551 sizeof (struct field) * num_fields);
b8c9b27d 11552 xfree (fields);
c906108c
SS
11553 }
11554 if (unsigned_enum)
876cecd0 11555 TYPE_UNSIGNED (this_type) = 1;
cafec441
TT
11556 if (flag_enum)
11557 TYPE_FLAG_ENUM (this_type) = 1;
c906108c 11558 }
134d01f1 11559
6c83ed52
TT
11560 /* If we are reading an enum from a .debug_types unit, and the enum
11561 is a declaration, and the enum is not the signatured type in the
11562 unit, then we do not want to add a symbol for it. Adding a
11563 symbol would in some cases obscure the true definition of the
11564 enum, giving users an incomplete type when the definition is
11565 actually available. Note that we do not want to do this for all
11566 enums which are just declarations, because C++0x allows forward
11567 enum declarations. */
3019eac3 11568 if (cu->per_cu->is_debug_types
6c83ed52
TT
11569 && die_is_declaration (die, cu))
11570 {
52dc124a 11571 struct signatured_type *sig_type;
6c83ed52 11572
52dc124a 11573 sig_type
6c83ed52 11574 = lookup_signatured_type_at_offset (dwarf2_per_objfile->objfile,
3019eac3 11575 cu->per_cu->info_or_types_section,
6c83ed52 11576 cu->per_cu->offset);
3019eac3
DE
11577 gdb_assert (sig_type->type_offset_in_section.sect_off != 0);
11578 if (sig_type->type_offset_in_section.sect_off != die->offset.sect_off)
6c83ed52
TT
11579 return;
11580 }
11581
f792889a 11582 new_symbol (die, this_type, cu);
c906108c
SS
11583}
11584
11585/* Extract all information from a DW_TAG_array_type DIE and put it in
11586 the DIE's type field. For now, this only handles one dimensional
11587 arrays. */
11588
f792889a 11589static struct type *
e7c27a73 11590read_array_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 11591{
e7c27a73 11592 struct objfile *objfile = cu->objfile;
c906108c 11593 struct die_info *child_die;
7e314c57 11594 struct type *type;
c906108c
SS
11595 struct type *element_type, *range_type, *index_type;
11596 struct type **range_types = NULL;
11597 struct attribute *attr;
11598 int ndim = 0;
11599 struct cleanup *back_to;
39cbfefa 11600 char *name;
c906108c 11601
e7c27a73 11602 element_type = die_type (die, cu);
c906108c 11603
7e314c57
JK
11604 /* The die_type call above may have already set the type for this DIE. */
11605 type = get_die_type (die, cu);
11606 if (type)
11607 return type;
11608
c906108c
SS
11609 /* Irix 6.2 native cc creates array types without children for
11610 arrays with unspecified length. */
639d11d3 11611 if (die->child == NULL)
c906108c 11612 {
46bf5051 11613 index_type = objfile_type (objfile)->builtin_int;
c906108c 11614 range_type = create_range_type (NULL, index_type, 0, -1);
f792889a
DJ
11615 type = create_array_type (NULL, element_type, range_type);
11616 return set_die_type (die, type, cu);
c906108c
SS
11617 }
11618
11619 back_to = make_cleanup (null_cleanup, NULL);
639d11d3 11620 child_die = die->child;
c906108c
SS
11621 while (child_die && child_die->tag)
11622 {
11623 if (child_die->tag == DW_TAG_subrange_type)
11624 {
f792889a 11625 struct type *child_type = read_type_die (child_die, cu);
9a619af0 11626
f792889a 11627 if (child_type != NULL)
a02abb62 11628 {
0963b4bd
MS
11629 /* The range type was succesfully read. Save it for the
11630 array type creation. */
a02abb62
JB
11631 if ((ndim % DW_FIELD_ALLOC_CHUNK) == 0)
11632 {
11633 range_types = (struct type **)
11634 xrealloc (range_types, (ndim + DW_FIELD_ALLOC_CHUNK)
11635 * sizeof (struct type *));
11636 if (ndim == 0)
11637 make_cleanup (free_current_contents, &range_types);
11638 }
f792889a 11639 range_types[ndim++] = child_type;
a02abb62 11640 }
c906108c
SS
11641 }
11642 child_die = sibling_die (child_die);
11643 }
11644
11645 /* Dwarf2 dimensions are output from left to right, create the
11646 necessary array types in backwards order. */
7ca2d3a3 11647
c906108c 11648 type = element_type;
7ca2d3a3
DL
11649
11650 if (read_array_order (die, cu) == DW_ORD_col_major)
11651 {
11652 int i = 0;
9a619af0 11653
7ca2d3a3
DL
11654 while (i < ndim)
11655 type = create_array_type (NULL, type, range_types[i++]);
11656 }
11657 else
11658 {
11659 while (ndim-- > 0)
11660 type = create_array_type (NULL, type, range_types[ndim]);
11661 }
c906108c 11662
f5f8a009
EZ
11663 /* Understand Dwarf2 support for vector types (like they occur on
11664 the PowerPC w/ AltiVec). Gcc just adds another attribute to the
11665 array type. This is not part of the Dwarf2/3 standard yet, but a
11666 custom vendor extension. The main difference between a regular
11667 array and the vector variant is that vectors are passed by value
11668 to functions. */
e142c38c 11669 attr = dwarf2_attr (die, DW_AT_GNU_vector, cu);
f5f8a009 11670 if (attr)
ea37ba09 11671 make_vector_type (type);
f5f8a009 11672
dbc98a8b
KW
11673 /* The DIE may have DW_AT_byte_size set. For example an OpenCL
11674 implementation may choose to implement triple vectors using this
11675 attribute. */
11676 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
11677 if (attr)
11678 {
11679 if (DW_UNSND (attr) >= TYPE_LENGTH (type))
11680 TYPE_LENGTH (type) = DW_UNSND (attr);
11681 else
3e43a32a
MS
11682 complaint (&symfile_complaints,
11683 _("DW_AT_byte_size for array type smaller "
11684 "than the total size of elements"));
dbc98a8b
KW
11685 }
11686
39cbfefa
DJ
11687 name = dwarf2_name (die, cu);
11688 if (name)
11689 TYPE_NAME (type) = name;
6e70227d 11690
0963b4bd 11691 /* Install the type in the die. */
7e314c57
JK
11692 set_die_type (die, type, cu);
11693
11694 /* set_die_type should be already done. */
b4ba55a1
JB
11695 set_descriptive_type (type, die, cu);
11696
c906108c
SS
11697 do_cleanups (back_to);
11698
7e314c57 11699 return type;
c906108c
SS
11700}
11701
7ca2d3a3 11702static enum dwarf_array_dim_ordering
6e70227d 11703read_array_order (struct die_info *die, struct dwarf2_cu *cu)
7ca2d3a3
DL
11704{
11705 struct attribute *attr;
11706
11707 attr = dwarf2_attr (die, DW_AT_ordering, cu);
11708
11709 if (attr) return DW_SND (attr);
11710
0963b4bd
MS
11711 /* GNU F77 is a special case, as at 08/2004 array type info is the
11712 opposite order to the dwarf2 specification, but data is still
11713 laid out as per normal fortran.
7ca2d3a3 11714
0963b4bd
MS
11715 FIXME: dsl/2004-8-20: If G77 is ever fixed, this will also need
11716 version checking. */
7ca2d3a3 11717
905e0470
PM
11718 if (cu->language == language_fortran
11719 && cu->producer && strstr (cu->producer, "GNU F77"))
7ca2d3a3
DL
11720 {
11721 return DW_ORD_row_major;
11722 }
11723
6e70227d 11724 switch (cu->language_defn->la_array_ordering)
7ca2d3a3
DL
11725 {
11726 case array_column_major:
11727 return DW_ORD_col_major;
11728 case array_row_major:
11729 default:
11730 return DW_ORD_row_major;
11731 };
11732}
11733
72019c9c 11734/* Extract all information from a DW_TAG_set_type DIE and put it in
0963b4bd 11735 the DIE's type field. */
72019c9c 11736
f792889a 11737static struct type *
72019c9c
GM
11738read_set_type (struct die_info *die, struct dwarf2_cu *cu)
11739{
7e314c57
JK
11740 struct type *domain_type, *set_type;
11741 struct attribute *attr;
f792889a 11742
7e314c57
JK
11743 domain_type = die_type (die, cu);
11744
11745 /* The die_type call above may have already set the type for this DIE. */
11746 set_type = get_die_type (die, cu);
11747 if (set_type)
11748 return set_type;
11749
11750 set_type = create_set_type (NULL, domain_type);
11751
11752 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
d09039dd
PM
11753 if (attr)
11754 TYPE_LENGTH (set_type) = DW_UNSND (attr);
7e314c57 11755
f792889a 11756 return set_die_type (die, set_type, cu);
72019c9c 11757}
7ca2d3a3 11758
0971de02
TT
11759/* A helper for read_common_block that creates a locexpr baton.
11760 SYM is the symbol which we are marking as computed.
11761 COMMON_DIE is the DIE for the common block.
11762 COMMON_LOC is the location expression attribute for the common
11763 block itself.
11764 MEMBER_LOC is the location expression attribute for the particular
11765 member of the common block that we are processing.
11766 CU is the CU from which the above come. */
11767
11768static void
11769mark_common_block_symbol_computed (struct symbol *sym,
11770 struct die_info *common_die,
11771 struct attribute *common_loc,
11772 struct attribute *member_loc,
11773 struct dwarf2_cu *cu)
11774{
11775 struct objfile *objfile = dwarf2_per_objfile->objfile;
11776 struct dwarf2_locexpr_baton *baton;
11777 gdb_byte *ptr;
11778 unsigned int cu_off;
11779 enum bfd_endian byte_order = gdbarch_byte_order (get_objfile_arch (objfile));
11780 LONGEST offset = 0;
11781
11782 gdb_assert (common_loc && member_loc);
11783 gdb_assert (attr_form_is_block (common_loc));
11784 gdb_assert (attr_form_is_block (member_loc)
11785 || attr_form_is_constant (member_loc));
11786
11787 baton = obstack_alloc (&objfile->objfile_obstack,
11788 sizeof (struct dwarf2_locexpr_baton));
11789 baton->per_cu = cu->per_cu;
11790 gdb_assert (baton->per_cu);
11791
11792 baton->size = 5 /* DW_OP_call4 */ + 1 /* DW_OP_plus */;
11793
11794 if (attr_form_is_constant (member_loc))
11795 {
11796 offset = dwarf2_get_attr_constant_value (member_loc, 0);
11797 baton->size += 1 /* DW_OP_addr */ + cu->header.addr_size;
11798 }
11799 else
11800 baton->size += DW_BLOCK (member_loc)->size;
11801
11802 ptr = obstack_alloc (&objfile->objfile_obstack, baton->size);
11803 baton->data = ptr;
11804
11805 *ptr++ = DW_OP_call4;
11806 cu_off = common_die->offset.sect_off - cu->per_cu->offset.sect_off;
11807 store_unsigned_integer (ptr, 4, byte_order, cu_off);
11808 ptr += 4;
11809
11810 if (attr_form_is_constant (member_loc))
11811 {
11812 *ptr++ = DW_OP_addr;
11813 store_unsigned_integer (ptr, cu->header.addr_size, byte_order, offset);
11814 ptr += cu->header.addr_size;
11815 }
11816 else
11817 {
11818 /* We have to copy the data here, because DW_OP_call4 will only
11819 use a DW_AT_location attribute. */
11820 memcpy (ptr, DW_BLOCK (member_loc)->data, DW_BLOCK (member_loc)->size);
11821 ptr += DW_BLOCK (member_loc)->size;
11822 }
11823
11824 *ptr++ = DW_OP_plus;
11825 gdb_assert (ptr - baton->data == baton->size);
11826
11827 SYMBOL_COMPUTED_OPS (sym) = &dwarf2_locexpr_funcs;
11828 SYMBOL_LOCATION_BATON (sym) = baton;
11829 SYMBOL_CLASS (sym) = LOC_COMPUTED;
11830}
11831
4357ac6c
TT
11832/* Create appropriate locally-scoped variables for all the
11833 DW_TAG_common_block entries. Also create a struct common_block
11834 listing all such variables for `info common'. COMMON_BLOCK_DOMAIN
11835 is used to sepate the common blocks name namespace from regular
11836 variable names. */
c906108c
SS
11837
11838static void
e7c27a73 11839read_common_block (struct die_info *die, struct dwarf2_cu *cu)
c906108c 11840{
0971de02
TT
11841 struct attribute *attr;
11842
11843 attr = dwarf2_attr (die, DW_AT_location, cu);
11844 if (attr)
11845 {
11846 /* Support the .debug_loc offsets. */
11847 if (attr_form_is_block (attr))
11848 {
11849 /* Ok. */
11850 }
11851 else if (attr_form_is_section_offset (attr))
11852 {
11853 dwarf2_complex_location_expr_complaint ();
11854 attr = NULL;
11855 }
11856 else
11857 {
11858 dwarf2_invalid_attrib_class_complaint ("DW_AT_location",
11859 "common block member");
11860 attr = NULL;
11861 }
11862 }
11863
639d11d3 11864 if (die->child != NULL)
c906108c 11865 {
4357ac6c
TT
11866 struct objfile *objfile = cu->objfile;
11867 struct die_info *child_die;
11868 size_t n_entries = 0, size;
11869 struct common_block *common_block;
11870 struct symbol *sym;
74ac6d43 11871
4357ac6c
TT
11872 for (child_die = die->child;
11873 child_die && child_die->tag;
11874 child_die = sibling_die (child_die))
11875 ++n_entries;
11876
11877 size = (sizeof (struct common_block)
11878 + (n_entries - 1) * sizeof (struct symbol *));
11879 common_block = obstack_alloc (&objfile->objfile_obstack, size);
11880 memset (common_block->contents, 0, n_entries * sizeof (struct symbol *));
11881 common_block->n_entries = 0;
11882
11883 for (child_die = die->child;
11884 child_die && child_die->tag;
11885 child_die = sibling_die (child_die))
11886 {
11887 /* Create the symbol in the DW_TAG_common_block block in the current
11888 symbol scope. */
e7c27a73 11889 sym = new_symbol (child_die, NULL, cu);
0971de02
TT
11890 if (sym != NULL)
11891 {
11892 struct attribute *member_loc;
11893
11894 common_block->contents[common_block->n_entries++] = sym;
11895
11896 member_loc = dwarf2_attr (child_die, DW_AT_data_member_location,
11897 cu);
11898 if (member_loc)
11899 {
11900 /* GDB has handled this for a long time, but it is
11901 not specified by DWARF. It seems to have been
11902 emitted by gfortran at least as recently as:
11903 http://gcc.gnu.org/bugzilla/show_bug.cgi?id=23057. */
11904 complaint (&symfile_complaints,
11905 _("Variable in common block has "
11906 "DW_AT_data_member_location "
11907 "- DIE at 0x%x [in module %s]"),
11908 child_die->offset.sect_off, cu->objfile->name);
11909
11910 if (attr_form_is_section_offset (member_loc))
11911 dwarf2_complex_location_expr_complaint ();
11912 else if (attr_form_is_constant (member_loc)
11913 || attr_form_is_block (member_loc))
11914 {
11915 if (attr)
11916 mark_common_block_symbol_computed (sym, die, attr,
11917 member_loc, cu);
11918 }
11919 else
11920 dwarf2_complex_location_expr_complaint ();
11921 }
11922 }
c906108c 11923 }
4357ac6c
TT
11924
11925 sym = new_symbol (die, objfile_type (objfile)->builtin_void, cu);
11926 SYMBOL_VALUE_COMMON_BLOCK (sym) = common_block;
c906108c
SS
11927 }
11928}
11929
0114d602 11930/* Create a type for a C++ namespace. */
d9fa45fe 11931
0114d602
DJ
11932static struct type *
11933read_namespace_type (struct die_info *die, struct dwarf2_cu *cu)
d9fa45fe 11934{
e7c27a73 11935 struct objfile *objfile = cu->objfile;
0114d602 11936 const char *previous_prefix, *name;
9219021c 11937 int is_anonymous;
0114d602
DJ
11938 struct type *type;
11939
11940 /* For extensions, reuse the type of the original namespace. */
11941 if (dwarf2_attr (die, DW_AT_extension, cu) != NULL)
11942 {
11943 struct die_info *ext_die;
11944 struct dwarf2_cu *ext_cu = cu;
9a619af0 11945
0114d602
DJ
11946 ext_die = dwarf2_extension (die, &ext_cu);
11947 type = read_type_die (ext_die, ext_cu);
9dc481d3
DE
11948
11949 /* EXT_CU may not be the same as CU.
11950 Ensure TYPE is recorded in CU's type_hash table. */
0114d602
DJ
11951 return set_die_type (die, type, cu);
11952 }
9219021c 11953
e142c38c 11954 name = namespace_name (die, &is_anonymous, cu);
9219021c
DC
11955
11956 /* Now build the name of the current namespace. */
11957
0114d602
DJ
11958 previous_prefix = determine_prefix (die, cu);
11959 if (previous_prefix[0] != '\0')
11960 name = typename_concat (&objfile->objfile_obstack,
f55ee35c 11961 previous_prefix, name, 0, cu);
0114d602
DJ
11962
11963 /* Create the type. */
11964 type = init_type (TYPE_CODE_NAMESPACE, 0, 0, NULL,
11965 objfile);
11966 TYPE_NAME (type) = (char *) name;
11967 TYPE_TAG_NAME (type) = TYPE_NAME (type);
11968
60531b24 11969 return set_die_type (die, type, cu);
0114d602
DJ
11970}
11971
11972/* Read a C++ namespace. */
11973
11974static void
11975read_namespace (struct die_info *die, struct dwarf2_cu *cu)
11976{
11977 struct objfile *objfile = cu->objfile;
0114d602 11978 int is_anonymous;
9219021c 11979
5c4e30ca
DC
11980 /* Add a symbol associated to this if we haven't seen the namespace
11981 before. Also, add a using directive if it's an anonymous
11982 namespace. */
9219021c 11983
f2f0e013 11984 if (dwarf2_attr (die, DW_AT_extension, cu) == NULL)
5c4e30ca
DC
11985 {
11986 struct type *type;
11987
0114d602 11988 type = read_type_die (die, cu);
e7c27a73 11989 new_symbol (die, type, cu);
5c4e30ca 11990
e8e80198 11991 namespace_name (die, &is_anonymous, cu);
5c4e30ca 11992 if (is_anonymous)
0114d602
DJ
11993 {
11994 const char *previous_prefix = determine_prefix (die, cu);
9a619af0 11995
c0cc3a76 11996 cp_add_using_directive (previous_prefix, TYPE_NAME (type), NULL,
32019081 11997 NULL, NULL, &objfile->objfile_obstack);
0114d602 11998 }
5c4e30ca 11999 }
9219021c 12000
639d11d3 12001 if (die->child != NULL)
d9fa45fe 12002 {
639d11d3 12003 struct die_info *child_die = die->child;
6e70227d 12004
d9fa45fe
DC
12005 while (child_die && child_die->tag)
12006 {
e7c27a73 12007 process_die (child_die, cu);
d9fa45fe
DC
12008 child_die = sibling_die (child_die);
12009 }
12010 }
38d518c9
EZ
12011}
12012
f55ee35c
JK
12013/* Read a Fortran module as type. This DIE can be only a declaration used for
12014 imported module. Still we need that type as local Fortran "use ... only"
12015 declaration imports depend on the created type in determine_prefix. */
12016
12017static struct type *
12018read_module_type (struct die_info *die, struct dwarf2_cu *cu)
12019{
12020 struct objfile *objfile = cu->objfile;
12021 char *module_name;
12022 struct type *type;
12023
12024 module_name = dwarf2_name (die, cu);
12025 if (!module_name)
3e43a32a
MS
12026 complaint (&symfile_complaints,
12027 _("DW_TAG_module has no name, offset 0x%x"),
b64f50a1 12028 die->offset.sect_off);
f55ee35c
JK
12029 type = init_type (TYPE_CODE_MODULE, 0, 0, module_name, objfile);
12030
12031 /* determine_prefix uses TYPE_TAG_NAME. */
12032 TYPE_TAG_NAME (type) = TYPE_NAME (type);
12033
12034 return set_die_type (die, type, cu);
12035}
12036
5d7cb8df
JK
12037/* Read a Fortran module. */
12038
12039static void
12040read_module (struct die_info *die, struct dwarf2_cu *cu)
12041{
12042 struct die_info *child_die = die->child;
12043
5d7cb8df
JK
12044 while (child_die && child_die->tag)
12045 {
12046 process_die (child_die, cu);
12047 child_die = sibling_die (child_die);
12048 }
12049}
12050
38d518c9
EZ
12051/* Return the name of the namespace represented by DIE. Set
12052 *IS_ANONYMOUS to tell whether or not the namespace is an anonymous
12053 namespace. */
12054
12055static const char *
e142c38c 12056namespace_name (struct die_info *die, int *is_anonymous, struct dwarf2_cu *cu)
38d518c9
EZ
12057{
12058 struct die_info *current_die;
12059 const char *name = NULL;
12060
12061 /* Loop through the extensions until we find a name. */
12062
12063 for (current_die = die;
12064 current_die != NULL;
f2f0e013 12065 current_die = dwarf2_extension (die, &cu))
38d518c9 12066 {
e142c38c 12067 name = dwarf2_name (current_die, cu);
38d518c9
EZ
12068 if (name != NULL)
12069 break;
12070 }
12071
12072 /* Is it an anonymous namespace? */
12073
12074 *is_anonymous = (name == NULL);
12075 if (*is_anonymous)
2b1dbab0 12076 name = CP_ANONYMOUS_NAMESPACE_STR;
38d518c9
EZ
12077
12078 return name;
d9fa45fe
DC
12079}
12080
c906108c
SS
12081/* Extract all information from a DW_TAG_pointer_type DIE and add to
12082 the user defined type vector. */
12083
f792889a 12084static struct type *
e7c27a73 12085read_tag_pointer_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 12086{
5e2b427d 12087 struct gdbarch *gdbarch = get_objfile_arch (cu->objfile);
e7c27a73 12088 struct comp_unit_head *cu_header = &cu->header;
c906108c 12089 struct type *type;
8b2dbe47
KB
12090 struct attribute *attr_byte_size;
12091 struct attribute *attr_address_class;
12092 int byte_size, addr_class;
7e314c57
JK
12093 struct type *target_type;
12094
12095 target_type = die_type (die, cu);
c906108c 12096
7e314c57
JK
12097 /* The die_type call above may have already set the type for this DIE. */
12098 type = get_die_type (die, cu);
12099 if (type)
12100 return type;
12101
12102 type = lookup_pointer_type (target_type);
8b2dbe47 12103
e142c38c 12104 attr_byte_size = dwarf2_attr (die, DW_AT_byte_size, cu);
8b2dbe47
KB
12105 if (attr_byte_size)
12106 byte_size = DW_UNSND (attr_byte_size);
c906108c 12107 else
8b2dbe47
KB
12108 byte_size = cu_header->addr_size;
12109
e142c38c 12110 attr_address_class = dwarf2_attr (die, DW_AT_address_class, cu);
8b2dbe47
KB
12111 if (attr_address_class)
12112 addr_class = DW_UNSND (attr_address_class);
12113 else
12114 addr_class = DW_ADDR_none;
12115
12116 /* If the pointer size or address class is different than the
12117 default, create a type variant marked as such and set the
12118 length accordingly. */
12119 if (TYPE_LENGTH (type) != byte_size || addr_class != DW_ADDR_none)
c906108c 12120 {
5e2b427d 12121 if (gdbarch_address_class_type_flags_p (gdbarch))
8b2dbe47
KB
12122 {
12123 int type_flags;
12124
849957d9 12125 type_flags = gdbarch_address_class_type_flags
5e2b427d 12126 (gdbarch, byte_size, addr_class);
876cecd0
TT
12127 gdb_assert ((type_flags & ~TYPE_INSTANCE_FLAG_ADDRESS_CLASS_ALL)
12128 == 0);
8b2dbe47
KB
12129 type = make_type_with_address_space (type, type_flags);
12130 }
12131 else if (TYPE_LENGTH (type) != byte_size)
12132 {
3e43a32a
MS
12133 complaint (&symfile_complaints,
12134 _("invalid pointer size %d"), byte_size);
8b2dbe47 12135 }
6e70227d 12136 else
9a619af0
MS
12137 {
12138 /* Should we also complain about unhandled address classes? */
12139 }
c906108c 12140 }
8b2dbe47
KB
12141
12142 TYPE_LENGTH (type) = byte_size;
f792889a 12143 return set_die_type (die, type, cu);
c906108c
SS
12144}
12145
12146/* Extract all information from a DW_TAG_ptr_to_member_type DIE and add to
12147 the user defined type vector. */
12148
f792889a 12149static struct type *
e7c27a73 12150read_tag_ptr_to_member_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c
SS
12151{
12152 struct type *type;
12153 struct type *to_type;
12154 struct type *domain;
12155
e7c27a73
DJ
12156 to_type = die_type (die, cu);
12157 domain = die_containing_type (die, cu);
0d5de010 12158
7e314c57
JK
12159 /* The calls above may have already set the type for this DIE. */
12160 type = get_die_type (die, cu);
12161 if (type)
12162 return type;
12163
0d5de010
DJ
12164 if (TYPE_CODE (check_typedef (to_type)) == TYPE_CODE_METHOD)
12165 type = lookup_methodptr_type (to_type);
12166 else
12167 type = lookup_memberptr_type (to_type, domain);
c906108c 12168
f792889a 12169 return set_die_type (die, type, cu);
c906108c
SS
12170}
12171
12172/* Extract all information from a DW_TAG_reference_type DIE and add to
12173 the user defined type vector. */
12174
f792889a 12175static struct type *
e7c27a73 12176read_tag_reference_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 12177{
e7c27a73 12178 struct comp_unit_head *cu_header = &cu->header;
7e314c57 12179 struct type *type, *target_type;
c906108c
SS
12180 struct attribute *attr;
12181
7e314c57
JK
12182 target_type = die_type (die, cu);
12183
12184 /* The die_type call above may have already set the type for this DIE. */
12185 type = get_die_type (die, cu);
12186 if (type)
12187 return type;
12188
12189 type = lookup_reference_type (target_type);
e142c38c 12190 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
c906108c
SS
12191 if (attr)
12192 {
12193 TYPE_LENGTH (type) = DW_UNSND (attr);
12194 }
12195 else
12196 {
107d2387 12197 TYPE_LENGTH (type) = cu_header->addr_size;
c906108c 12198 }
f792889a 12199 return set_die_type (die, type, cu);
c906108c
SS
12200}
12201
f792889a 12202static struct type *
e7c27a73 12203read_tag_const_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 12204{
f792889a 12205 struct type *base_type, *cv_type;
c906108c 12206
e7c27a73 12207 base_type = die_type (die, cu);
7e314c57
JK
12208
12209 /* The die_type call above may have already set the type for this DIE. */
12210 cv_type = get_die_type (die, cu);
12211 if (cv_type)
12212 return cv_type;
12213
2f608a3a
KW
12214 /* In case the const qualifier is applied to an array type, the element type
12215 is so qualified, not the array type (section 6.7.3 of C99). */
12216 if (TYPE_CODE (base_type) == TYPE_CODE_ARRAY)
12217 {
12218 struct type *el_type, *inner_array;
12219
12220 base_type = copy_type (base_type);
12221 inner_array = base_type;
12222
12223 while (TYPE_CODE (TYPE_TARGET_TYPE (inner_array)) == TYPE_CODE_ARRAY)
12224 {
12225 TYPE_TARGET_TYPE (inner_array) =
12226 copy_type (TYPE_TARGET_TYPE (inner_array));
12227 inner_array = TYPE_TARGET_TYPE (inner_array);
12228 }
12229
12230 el_type = TYPE_TARGET_TYPE (inner_array);
12231 TYPE_TARGET_TYPE (inner_array) =
12232 make_cv_type (1, TYPE_VOLATILE (el_type), el_type, NULL);
12233
12234 return set_die_type (die, base_type, cu);
12235 }
12236
f792889a
DJ
12237 cv_type = make_cv_type (1, TYPE_VOLATILE (base_type), base_type, 0);
12238 return set_die_type (die, cv_type, cu);
c906108c
SS
12239}
12240
f792889a 12241static struct type *
e7c27a73 12242read_tag_volatile_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 12243{
f792889a 12244 struct type *base_type, *cv_type;
c906108c 12245
e7c27a73 12246 base_type = die_type (die, cu);
7e314c57
JK
12247
12248 /* The die_type call above may have already set the type for this DIE. */
12249 cv_type = get_die_type (die, cu);
12250 if (cv_type)
12251 return cv_type;
12252
f792889a
DJ
12253 cv_type = make_cv_type (TYPE_CONST (base_type), 1, base_type, 0);
12254 return set_die_type (die, cv_type, cu);
c906108c
SS
12255}
12256
12257/* Extract all information from a DW_TAG_string_type DIE and add to
12258 the user defined type vector. It isn't really a user defined type,
12259 but it behaves like one, with other DIE's using an AT_user_def_type
12260 attribute to reference it. */
12261
f792889a 12262static struct type *
e7c27a73 12263read_tag_string_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 12264{
e7c27a73 12265 struct objfile *objfile = cu->objfile;
3b7538c0 12266 struct gdbarch *gdbarch = get_objfile_arch (objfile);
c906108c
SS
12267 struct type *type, *range_type, *index_type, *char_type;
12268 struct attribute *attr;
12269 unsigned int length;
12270
e142c38c 12271 attr = dwarf2_attr (die, DW_AT_string_length, cu);
c906108c
SS
12272 if (attr)
12273 {
12274 length = DW_UNSND (attr);
12275 }
12276 else
12277 {
0963b4bd 12278 /* Check for the DW_AT_byte_size attribute. */
e142c38c 12279 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
b21b22e0
PS
12280 if (attr)
12281 {
12282 length = DW_UNSND (attr);
12283 }
12284 else
12285 {
12286 length = 1;
12287 }
c906108c 12288 }
6ccb9162 12289
46bf5051 12290 index_type = objfile_type (objfile)->builtin_int;
c906108c 12291 range_type = create_range_type (NULL, index_type, 1, length);
3b7538c0
UW
12292 char_type = language_string_char_type (cu->language_defn, gdbarch);
12293 type = create_string_type (NULL, char_type, range_type);
6ccb9162 12294
f792889a 12295 return set_die_type (die, type, cu);
c906108c
SS
12296}
12297
12298/* Handle DIES due to C code like:
12299
12300 struct foo
c5aa993b
JM
12301 {
12302 int (*funcp)(int a, long l);
12303 int b;
12304 };
c906108c 12305
0963b4bd 12306 ('funcp' generates a DW_TAG_subroutine_type DIE). */
c906108c 12307
f792889a 12308static struct type *
e7c27a73 12309read_subroutine_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 12310{
bb5ed363 12311 struct objfile *objfile = cu->objfile;
0963b4bd
MS
12312 struct type *type; /* Type that this function returns. */
12313 struct type *ftype; /* Function that returns above type. */
c906108c
SS
12314 struct attribute *attr;
12315
e7c27a73 12316 type = die_type (die, cu);
7e314c57
JK
12317
12318 /* The die_type call above may have already set the type for this DIE. */
12319 ftype = get_die_type (die, cu);
12320 if (ftype)
12321 return ftype;
12322
0c8b41f1 12323 ftype = lookup_function_type (type);
c906108c 12324
5b8101ae 12325 /* All functions in C++, Pascal and Java have prototypes. */
e142c38c 12326 attr = dwarf2_attr (die, DW_AT_prototyped, cu);
c906108c 12327 if ((attr && (DW_UNSND (attr) != 0))
987504bb 12328 || cu->language == language_cplus
5b8101ae
PM
12329 || cu->language == language_java
12330 || cu->language == language_pascal)
876cecd0 12331 TYPE_PROTOTYPED (ftype) = 1;
a6c727b2
DJ
12332 else if (producer_is_realview (cu->producer))
12333 /* RealView does not emit DW_AT_prototyped. We can not
12334 distinguish prototyped and unprototyped functions; default to
12335 prototyped, since that is more common in modern code (and
12336 RealView warns about unprototyped functions). */
12337 TYPE_PROTOTYPED (ftype) = 1;
c906108c 12338
c055b101
CV
12339 /* Store the calling convention in the type if it's available in
12340 the subroutine die. Otherwise set the calling convention to
12341 the default value DW_CC_normal. */
12342 attr = dwarf2_attr (die, DW_AT_calling_convention, cu);
54fcddd0
UW
12343 if (attr)
12344 TYPE_CALLING_CONVENTION (ftype) = DW_UNSND (attr);
12345 else if (cu->producer && strstr (cu->producer, "IBM XL C for OpenCL"))
12346 TYPE_CALLING_CONVENTION (ftype) = DW_CC_GDB_IBM_OpenCL;
12347 else
12348 TYPE_CALLING_CONVENTION (ftype) = DW_CC_normal;
76c10ea2
GM
12349
12350 /* We need to add the subroutine type to the die immediately so
12351 we don't infinitely recurse when dealing with parameters
0963b4bd 12352 declared as the same subroutine type. */
76c10ea2 12353 set_die_type (die, ftype, cu);
6e70227d 12354
639d11d3 12355 if (die->child != NULL)
c906108c 12356 {
bb5ed363 12357 struct type *void_type = objfile_type (objfile)->builtin_void;
c906108c 12358 struct die_info *child_die;
8072405b 12359 int nparams, iparams;
c906108c
SS
12360
12361 /* Count the number of parameters.
12362 FIXME: GDB currently ignores vararg functions, but knows about
12363 vararg member functions. */
8072405b 12364 nparams = 0;
639d11d3 12365 child_die = die->child;
c906108c
SS
12366 while (child_die && child_die->tag)
12367 {
12368 if (child_die->tag == DW_TAG_formal_parameter)
12369 nparams++;
12370 else if (child_die->tag == DW_TAG_unspecified_parameters)
876cecd0 12371 TYPE_VARARGS (ftype) = 1;
c906108c
SS
12372 child_die = sibling_die (child_die);
12373 }
12374
12375 /* Allocate storage for parameters and fill them in. */
12376 TYPE_NFIELDS (ftype) = nparams;
12377 TYPE_FIELDS (ftype) = (struct field *)
ae5a43e0 12378 TYPE_ZALLOC (ftype, nparams * sizeof (struct field));
c906108c 12379
8072405b
JK
12380 /* TYPE_FIELD_TYPE must never be NULL. Pre-fill the array to ensure it
12381 even if we error out during the parameters reading below. */
12382 for (iparams = 0; iparams < nparams; iparams++)
12383 TYPE_FIELD_TYPE (ftype, iparams) = void_type;
12384
12385 iparams = 0;
639d11d3 12386 child_die = die->child;
c906108c
SS
12387 while (child_die && child_die->tag)
12388 {
12389 if (child_die->tag == DW_TAG_formal_parameter)
12390 {
3ce3b1ba
PA
12391 struct type *arg_type;
12392
12393 /* DWARF version 2 has no clean way to discern C++
12394 static and non-static member functions. G++ helps
12395 GDB by marking the first parameter for non-static
12396 member functions (which is the this pointer) as
12397 artificial. We pass this information to
12398 dwarf2_add_member_fn via TYPE_FIELD_ARTIFICIAL.
12399
12400 DWARF version 3 added DW_AT_object_pointer, which GCC
12401 4.5 does not yet generate. */
e142c38c 12402 attr = dwarf2_attr (child_die, DW_AT_artificial, cu);
c906108c
SS
12403 if (attr)
12404 TYPE_FIELD_ARTIFICIAL (ftype, iparams) = DW_UNSND (attr);
12405 else
418835cc
KS
12406 {
12407 TYPE_FIELD_ARTIFICIAL (ftype, iparams) = 0;
12408
12409 /* GCC/43521: In java, the formal parameter
12410 "this" is sometimes not marked with DW_AT_artificial. */
12411 if (cu->language == language_java)
12412 {
12413 const char *name = dwarf2_name (child_die, cu);
9a619af0 12414
418835cc
KS
12415 if (name && !strcmp (name, "this"))
12416 TYPE_FIELD_ARTIFICIAL (ftype, iparams) = 1;
12417 }
12418 }
3ce3b1ba
PA
12419 arg_type = die_type (child_die, cu);
12420
12421 /* RealView does not mark THIS as const, which the testsuite
12422 expects. GCC marks THIS as const in method definitions,
12423 but not in the class specifications (GCC PR 43053). */
12424 if (cu->language == language_cplus && !TYPE_CONST (arg_type)
12425 && TYPE_FIELD_ARTIFICIAL (ftype, iparams))
12426 {
12427 int is_this = 0;
12428 struct dwarf2_cu *arg_cu = cu;
12429 const char *name = dwarf2_name (child_die, cu);
12430
12431 attr = dwarf2_attr (die, DW_AT_object_pointer, cu);
12432 if (attr)
12433 {
12434 /* If the compiler emits this, use it. */
12435 if (follow_die_ref (die, attr, &arg_cu) == child_die)
12436 is_this = 1;
12437 }
12438 else if (name && strcmp (name, "this") == 0)
12439 /* Function definitions will have the argument names. */
12440 is_this = 1;
12441 else if (name == NULL && iparams == 0)
12442 /* Declarations may not have the names, so like
12443 elsewhere in GDB, assume an artificial first
12444 argument is "this". */
12445 is_this = 1;
12446
12447 if (is_this)
12448 arg_type = make_cv_type (1, TYPE_VOLATILE (arg_type),
12449 arg_type, 0);
12450 }
12451
12452 TYPE_FIELD_TYPE (ftype, iparams) = arg_type;
c906108c
SS
12453 iparams++;
12454 }
12455 child_die = sibling_die (child_die);
12456 }
12457 }
12458
76c10ea2 12459 return ftype;
c906108c
SS
12460}
12461
f792889a 12462static struct type *
e7c27a73 12463read_typedef (struct die_info *die, struct dwarf2_cu *cu)
c906108c 12464{
e7c27a73 12465 struct objfile *objfile = cu->objfile;
0114d602 12466 const char *name = NULL;
3c8e0968 12467 struct type *this_type, *target_type;
c906108c 12468
94af9270 12469 name = dwarf2_full_name (NULL, die, cu);
f792889a 12470 this_type = init_type (TYPE_CODE_TYPEDEF, 0,
0114d602
DJ
12471 TYPE_FLAG_TARGET_STUB, NULL, objfile);
12472 TYPE_NAME (this_type) = (char *) name;
f792889a 12473 set_die_type (die, this_type, cu);
3c8e0968
DE
12474 target_type = die_type (die, cu);
12475 if (target_type != this_type)
12476 TYPE_TARGET_TYPE (this_type) = target_type;
12477 else
12478 {
12479 /* Self-referential typedefs are, it seems, not allowed by the DWARF
12480 spec and cause infinite loops in GDB. */
12481 complaint (&symfile_complaints,
12482 _("Self-referential DW_TAG_typedef "
12483 "- DIE at 0x%x [in module %s]"),
b64f50a1 12484 die->offset.sect_off, objfile->name);
3c8e0968
DE
12485 TYPE_TARGET_TYPE (this_type) = NULL;
12486 }
f792889a 12487 return this_type;
c906108c
SS
12488}
12489
12490/* Find a representation of a given base type and install
12491 it in the TYPE field of the die. */
12492
f792889a 12493static struct type *
e7c27a73 12494read_base_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 12495{
e7c27a73 12496 struct objfile *objfile = cu->objfile;
c906108c
SS
12497 struct type *type;
12498 struct attribute *attr;
12499 int encoding = 0, size = 0;
39cbfefa 12500 char *name;
6ccb9162
UW
12501 enum type_code code = TYPE_CODE_INT;
12502 int type_flags = 0;
12503 struct type *target_type = NULL;
c906108c 12504
e142c38c 12505 attr = dwarf2_attr (die, DW_AT_encoding, cu);
c906108c
SS
12506 if (attr)
12507 {
12508 encoding = DW_UNSND (attr);
12509 }
e142c38c 12510 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
c906108c
SS
12511 if (attr)
12512 {
12513 size = DW_UNSND (attr);
12514 }
39cbfefa 12515 name = dwarf2_name (die, cu);
6ccb9162 12516 if (!name)
c906108c 12517 {
6ccb9162
UW
12518 complaint (&symfile_complaints,
12519 _("DW_AT_name missing from DW_TAG_base_type"));
c906108c 12520 }
6ccb9162
UW
12521
12522 switch (encoding)
c906108c 12523 {
6ccb9162
UW
12524 case DW_ATE_address:
12525 /* Turn DW_ATE_address into a void * pointer. */
12526 code = TYPE_CODE_PTR;
12527 type_flags |= TYPE_FLAG_UNSIGNED;
12528 target_type = init_type (TYPE_CODE_VOID, 1, 0, NULL, objfile);
12529 break;
12530 case DW_ATE_boolean:
12531 code = TYPE_CODE_BOOL;
12532 type_flags |= TYPE_FLAG_UNSIGNED;
12533 break;
12534 case DW_ATE_complex_float:
12535 code = TYPE_CODE_COMPLEX;
12536 target_type = init_type (TYPE_CODE_FLT, size / 2, 0, NULL, objfile);
12537 break;
12538 case DW_ATE_decimal_float:
12539 code = TYPE_CODE_DECFLOAT;
12540 break;
12541 case DW_ATE_float:
12542 code = TYPE_CODE_FLT;
12543 break;
12544 case DW_ATE_signed:
12545 break;
12546 case DW_ATE_unsigned:
12547 type_flags |= TYPE_FLAG_UNSIGNED;
3b2b8fea
TT
12548 if (cu->language == language_fortran
12549 && name
12550 && strncmp (name, "character(", sizeof ("character(") - 1) == 0)
12551 code = TYPE_CODE_CHAR;
6ccb9162
UW
12552 break;
12553 case DW_ATE_signed_char:
6e70227d 12554 if (cu->language == language_ada || cu->language == language_m2
3b2b8fea
TT
12555 || cu->language == language_pascal
12556 || cu->language == language_fortran)
6ccb9162
UW
12557 code = TYPE_CODE_CHAR;
12558 break;
12559 case DW_ATE_unsigned_char:
868a0084 12560 if (cu->language == language_ada || cu->language == language_m2
3b2b8fea
TT
12561 || cu->language == language_pascal
12562 || cu->language == language_fortran)
6ccb9162
UW
12563 code = TYPE_CODE_CHAR;
12564 type_flags |= TYPE_FLAG_UNSIGNED;
12565 break;
75079b2b
TT
12566 case DW_ATE_UTF:
12567 /* We just treat this as an integer and then recognize the
12568 type by name elsewhere. */
12569 break;
12570
6ccb9162
UW
12571 default:
12572 complaint (&symfile_complaints, _("unsupported DW_AT_encoding: '%s'"),
12573 dwarf_type_encoding_name (encoding));
12574 break;
c906108c 12575 }
6ccb9162 12576
0114d602
DJ
12577 type = init_type (code, size, type_flags, NULL, objfile);
12578 TYPE_NAME (type) = name;
6ccb9162
UW
12579 TYPE_TARGET_TYPE (type) = target_type;
12580
0114d602 12581 if (name && strcmp (name, "char") == 0)
876cecd0 12582 TYPE_NOSIGN (type) = 1;
0114d602 12583
f792889a 12584 return set_die_type (die, type, cu);
c906108c
SS
12585}
12586
a02abb62
JB
12587/* Read the given DW_AT_subrange DIE. */
12588
f792889a 12589static struct type *
a02abb62
JB
12590read_subrange_type (struct die_info *die, struct dwarf2_cu *cu)
12591{
12592 struct type *base_type;
12593 struct type *range_type;
12594 struct attribute *attr;
4fae6e18
JK
12595 LONGEST low, high;
12596 int low_default_is_valid;
39cbfefa 12597 char *name;
43bbcdc2 12598 LONGEST negative_mask;
e77813c8 12599
a02abb62 12600 base_type = die_type (die, cu);
953ac07e
JK
12601 /* Preserve BASE_TYPE's original type, just set its LENGTH. */
12602 check_typedef (base_type);
a02abb62 12603
7e314c57
JK
12604 /* The die_type call above may have already set the type for this DIE. */
12605 range_type = get_die_type (die, cu);
12606 if (range_type)
12607 return range_type;
12608
4fae6e18
JK
12609 /* Set LOW_DEFAULT_IS_VALID if current language and DWARF version allow
12610 omitting DW_AT_lower_bound. */
12611 switch (cu->language)
6e70227d 12612 {
4fae6e18
JK
12613 case language_c:
12614 case language_cplus:
12615 low = 0;
12616 low_default_is_valid = 1;
12617 break;
12618 case language_fortran:
12619 low = 1;
12620 low_default_is_valid = 1;
12621 break;
12622 case language_d:
12623 case language_java:
12624 case language_objc:
12625 low = 0;
12626 low_default_is_valid = (cu->header.version >= 4);
12627 break;
12628 case language_ada:
12629 case language_m2:
12630 case language_pascal:
a02abb62 12631 low = 1;
4fae6e18
JK
12632 low_default_is_valid = (cu->header.version >= 4);
12633 break;
12634 default:
12635 low = 0;
12636 low_default_is_valid = 0;
12637 break;
a02abb62
JB
12638 }
12639
dd5e6932
DJ
12640 /* FIXME: For variable sized arrays either of these could be
12641 a variable rather than a constant value. We'll allow it,
12642 but we don't know how to handle it. */
e142c38c 12643 attr = dwarf2_attr (die, DW_AT_lower_bound, cu);
a02abb62 12644 if (attr)
4fae6e18
JK
12645 low = dwarf2_get_attr_constant_value (attr, low);
12646 else if (!low_default_is_valid)
12647 complaint (&symfile_complaints, _("Missing DW_AT_lower_bound "
12648 "- DIE at 0x%x [in module %s]"),
12649 die->offset.sect_off, cu->objfile->name);
a02abb62 12650
e142c38c 12651 attr = dwarf2_attr (die, DW_AT_upper_bound, cu);
a02abb62 12652 if (attr)
6e70227d 12653 {
d48323d8 12654 if (attr_form_is_block (attr) || is_ref_attr (attr))
a02abb62
JB
12655 {
12656 /* GCC encodes arrays with unspecified or dynamic length
e77813c8 12657 with a DW_FORM_block1 attribute or a reference attribute.
a02abb62
JB
12658 FIXME: GDB does not yet know how to handle dynamic
12659 arrays properly, treat them as arrays with unspecified
12660 length for now.
12661
12662 FIXME: jimb/2003-09-22: GDB does not really know
12663 how to handle arrays of unspecified length
12664 either; we just represent them as zero-length
12665 arrays. Choose an appropriate upper bound given
12666 the lower bound we've computed above. */
12667 high = low - 1;
12668 }
12669 else
12670 high = dwarf2_get_attr_constant_value (attr, 1);
12671 }
e77813c8
PM
12672 else
12673 {
12674 attr = dwarf2_attr (die, DW_AT_count, cu);
12675 if (attr)
12676 {
12677 int count = dwarf2_get_attr_constant_value (attr, 1);
12678 high = low + count - 1;
12679 }
c2ff108b
JK
12680 else
12681 {
12682 /* Unspecified array length. */
12683 high = low - 1;
12684 }
e77813c8
PM
12685 }
12686
12687 /* Dwarf-2 specifications explicitly allows to create subrange types
12688 without specifying a base type.
12689 In that case, the base type must be set to the type of
12690 the lower bound, upper bound or count, in that order, if any of these
12691 three attributes references an object that has a type.
12692 If no base type is found, the Dwarf-2 specifications say that
12693 a signed integer type of size equal to the size of an address should
12694 be used.
12695 For the following C code: `extern char gdb_int [];'
12696 GCC produces an empty range DIE.
12697 FIXME: muller/2010-05-28: Possible references to object for low bound,
0963b4bd 12698 high bound or count are not yet handled by this code. */
e77813c8
PM
12699 if (TYPE_CODE (base_type) == TYPE_CODE_VOID)
12700 {
12701 struct objfile *objfile = cu->objfile;
12702 struct gdbarch *gdbarch = get_objfile_arch (objfile);
12703 int addr_size = gdbarch_addr_bit (gdbarch) /8;
12704 struct type *int_type = objfile_type (objfile)->builtin_int;
12705
12706 /* Test "int", "long int", and "long long int" objfile types,
12707 and select the first one having a size above or equal to the
12708 architecture address size. */
12709 if (int_type && TYPE_LENGTH (int_type) >= addr_size)
12710 base_type = int_type;
12711 else
12712 {
12713 int_type = objfile_type (objfile)->builtin_long;
12714 if (int_type && TYPE_LENGTH (int_type) >= addr_size)
12715 base_type = int_type;
12716 else
12717 {
12718 int_type = objfile_type (objfile)->builtin_long_long;
12719 if (int_type && TYPE_LENGTH (int_type) >= addr_size)
12720 base_type = int_type;
12721 }
12722 }
12723 }
a02abb62 12724
6e70227d 12725 negative_mask =
43bbcdc2
PH
12726 (LONGEST) -1 << (TYPE_LENGTH (base_type) * TARGET_CHAR_BIT - 1);
12727 if (!TYPE_UNSIGNED (base_type) && (low & negative_mask))
12728 low |= negative_mask;
12729 if (!TYPE_UNSIGNED (base_type) && (high & negative_mask))
12730 high |= negative_mask;
12731
a02abb62
JB
12732 range_type = create_range_type (NULL, base_type, low, high);
12733
bbb0eef6
JK
12734 /* Mark arrays with dynamic length at least as an array of unspecified
12735 length. GDB could check the boundary but before it gets implemented at
12736 least allow accessing the array elements. */
d48323d8 12737 if (attr && attr_form_is_block (attr))
bbb0eef6
JK
12738 TYPE_HIGH_BOUND_UNDEFINED (range_type) = 1;
12739
c2ff108b
JK
12740 /* Ada expects an empty array on no boundary attributes. */
12741 if (attr == NULL && cu->language != language_ada)
12742 TYPE_HIGH_BOUND_UNDEFINED (range_type) = 1;
12743
39cbfefa
DJ
12744 name = dwarf2_name (die, cu);
12745 if (name)
12746 TYPE_NAME (range_type) = name;
6e70227d 12747
e142c38c 12748 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
a02abb62
JB
12749 if (attr)
12750 TYPE_LENGTH (range_type) = DW_UNSND (attr);
12751
7e314c57
JK
12752 set_die_type (die, range_type, cu);
12753
12754 /* set_die_type should be already done. */
b4ba55a1
JB
12755 set_descriptive_type (range_type, die, cu);
12756
7e314c57 12757 return range_type;
a02abb62 12758}
6e70227d 12759
f792889a 12760static struct type *
81a17f79
JB
12761read_unspecified_type (struct die_info *die, struct dwarf2_cu *cu)
12762{
12763 struct type *type;
81a17f79 12764
81a17f79
JB
12765 /* For now, we only support the C meaning of an unspecified type: void. */
12766
0114d602
DJ
12767 type = init_type (TYPE_CODE_VOID, 0, 0, NULL, cu->objfile);
12768 TYPE_NAME (type) = dwarf2_name (die, cu);
81a17f79 12769
f792889a 12770 return set_die_type (die, type, cu);
81a17f79 12771}
a02abb62 12772
639d11d3
DC
12773/* Read a single die and all its descendents. Set the die's sibling
12774 field to NULL; set other fields in the die correctly, and set all
12775 of the descendents' fields correctly. Set *NEW_INFO_PTR to the
12776 location of the info_ptr after reading all of those dies. PARENT
12777 is the parent of the die in question. */
12778
12779static struct die_info *
dee91e82
DE
12780read_die_and_children (const struct die_reader_specs *reader,
12781 gdb_byte *info_ptr,
12782 gdb_byte **new_info_ptr,
12783 struct die_info *parent)
639d11d3
DC
12784{
12785 struct die_info *die;
fe1b8b76 12786 gdb_byte *cur_ptr;
639d11d3
DC
12787 int has_children;
12788
93311388 12789 cur_ptr = read_full_die (reader, &die, info_ptr, &has_children);
1d325ec1
DJ
12790 if (die == NULL)
12791 {
12792 *new_info_ptr = cur_ptr;
12793 return NULL;
12794 }
93311388 12795 store_in_ref_table (die, reader->cu);
639d11d3
DC
12796
12797 if (has_children)
348e048f 12798 die->child = read_die_and_siblings (reader, cur_ptr, new_info_ptr, die);
639d11d3
DC
12799 else
12800 {
12801 die->child = NULL;
12802 *new_info_ptr = cur_ptr;
12803 }
12804
12805 die->sibling = NULL;
12806 die->parent = parent;
12807 return die;
12808}
12809
12810/* Read a die, all of its descendents, and all of its siblings; set
12811 all of the fields of all of the dies correctly. Arguments are as
12812 in read_die_and_children. */
12813
12814static struct die_info *
93311388
DE
12815read_die_and_siblings (const struct die_reader_specs *reader,
12816 gdb_byte *info_ptr,
fe1b8b76 12817 gdb_byte **new_info_ptr,
639d11d3
DC
12818 struct die_info *parent)
12819{
12820 struct die_info *first_die, *last_sibling;
fe1b8b76 12821 gdb_byte *cur_ptr;
639d11d3 12822
c906108c 12823 cur_ptr = info_ptr;
639d11d3
DC
12824 first_die = last_sibling = NULL;
12825
12826 while (1)
c906108c 12827 {
639d11d3 12828 struct die_info *die
dee91e82 12829 = read_die_and_children (reader, cur_ptr, &cur_ptr, parent);
639d11d3 12830
1d325ec1 12831 if (die == NULL)
c906108c 12832 {
639d11d3
DC
12833 *new_info_ptr = cur_ptr;
12834 return first_die;
c906108c 12835 }
1d325ec1
DJ
12836
12837 if (!first_die)
12838 first_die = die;
c906108c 12839 else
1d325ec1
DJ
12840 last_sibling->sibling = die;
12841
12842 last_sibling = die;
c906108c 12843 }
c906108c
SS
12844}
12845
3019eac3
DE
12846/* Read a die and all its attributes, leave space for NUM_EXTRA_ATTRS
12847 attributes.
12848 The caller is responsible for filling in the extra attributes
12849 and updating (*DIEP)->num_attrs.
12850 Set DIEP to point to a newly allocated die with its information,
12851 except for its child, sibling, and parent fields.
12852 Set HAS_CHILDREN to tell whether the die has children or not. */
93311388
DE
12853
12854static gdb_byte *
3019eac3
DE
12855read_full_die_1 (const struct die_reader_specs *reader,
12856 struct die_info **diep, gdb_byte *info_ptr,
12857 int *has_children, int num_extra_attrs)
93311388 12858{
b64f50a1
JK
12859 unsigned int abbrev_number, bytes_read, i;
12860 sect_offset offset;
93311388
DE
12861 struct abbrev_info *abbrev;
12862 struct die_info *die;
12863 struct dwarf2_cu *cu = reader->cu;
12864 bfd *abfd = reader->abfd;
12865
b64f50a1 12866 offset.sect_off = info_ptr - reader->buffer;
93311388
DE
12867 abbrev_number = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
12868 info_ptr += bytes_read;
12869 if (!abbrev_number)
12870 {
12871 *diep = NULL;
12872 *has_children = 0;
12873 return info_ptr;
12874 }
12875
433df2d4 12876 abbrev = abbrev_table_lookup_abbrev (cu->abbrev_table, abbrev_number);
93311388 12877 if (!abbrev)
348e048f
DE
12878 error (_("Dwarf Error: could not find abbrev number %d [in module %s]"),
12879 abbrev_number,
12880 bfd_get_filename (abfd));
12881
3019eac3 12882 die = dwarf_alloc_die (cu, abbrev->num_attrs + num_extra_attrs);
93311388
DE
12883 die->offset = offset;
12884 die->tag = abbrev->tag;
12885 die->abbrev = abbrev_number;
12886
3019eac3
DE
12887 /* Make the result usable.
12888 The caller needs to update num_attrs after adding the extra
12889 attributes. */
93311388
DE
12890 die->num_attrs = abbrev->num_attrs;
12891
12892 for (i = 0; i < abbrev->num_attrs; ++i)
dee91e82
DE
12893 info_ptr = read_attribute (reader, &die->attrs[i], &abbrev->attrs[i],
12894 info_ptr);
93311388
DE
12895
12896 *diep = die;
12897 *has_children = abbrev->has_children;
12898 return info_ptr;
12899}
12900
3019eac3
DE
12901/* Read a die and all its attributes.
12902 Set DIEP to point to a newly allocated die with its information,
12903 except for its child, sibling, and parent fields.
12904 Set HAS_CHILDREN to tell whether the die has children or not. */
12905
12906static gdb_byte *
12907read_full_die (const struct die_reader_specs *reader,
12908 struct die_info **diep, gdb_byte *info_ptr,
12909 int *has_children)
12910{
12911 return read_full_die_1 (reader, diep, info_ptr, has_children, 0);
12912}
433df2d4
DE
12913\f
12914/* Abbreviation tables.
3019eac3 12915
433df2d4 12916 In DWARF version 2, the description of the debugging information is
c906108c
SS
12917 stored in a separate .debug_abbrev section. Before we read any
12918 dies from a section we read in all abbreviations and install them
433df2d4
DE
12919 in a hash table. */
12920
12921/* Allocate space for a struct abbrev_info object in ABBREV_TABLE. */
12922
12923static struct abbrev_info *
12924abbrev_table_alloc_abbrev (struct abbrev_table *abbrev_table)
12925{
12926 struct abbrev_info *abbrev;
12927
12928 abbrev = (struct abbrev_info *)
12929 obstack_alloc (&abbrev_table->abbrev_obstack, sizeof (struct abbrev_info));
12930 memset (abbrev, 0, sizeof (struct abbrev_info));
12931 return abbrev;
12932}
12933
12934/* Add an abbreviation to the table. */
c906108c
SS
12935
12936static void
433df2d4
DE
12937abbrev_table_add_abbrev (struct abbrev_table *abbrev_table,
12938 unsigned int abbrev_number,
12939 struct abbrev_info *abbrev)
12940{
12941 unsigned int hash_number;
12942
12943 hash_number = abbrev_number % ABBREV_HASH_SIZE;
12944 abbrev->next = abbrev_table->abbrevs[hash_number];
12945 abbrev_table->abbrevs[hash_number] = abbrev;
12946}
dee91e82 12947
433df2d4
DE
12948/* Look up an abbrev in the table.
12949 Returns NULL if the abbrev is not found. */
12950
12951static struct abbrev_info *
12952abbrev_table_lookup_abbrev (const struct abbrev_table *abbrev_table,
12953 unsigned int abbrev_number)
c906108c 12954{
433df2d4
DE
12955 unsigned int hash_number;
12956 struct abbrev_info *abbrev;
12957
12958 hash_number = abbrev_number % ABBREV_HASH_SIZE;
12959 abbrev = abbrev_table->abbrevs[hash_number];
12960
12961 while (abbrev)
12962 {
12963 if (abbrev->number == abbrev_number)
12964 return abbrev;
12965 abbrev = abbrev->next;
12966 }
12967 return NULL;
12968}
12969
12970/* Read in an abbrev table. */
12971
12972static struct abbrev_table *
12973abbrev_table_read_table (struct dwarf2_section_info *section,
12974 sect_offset offset)
12975{
12976 struct objfile *objfile = dwarf2_per_objfile->objfile;
12977 bfd *abfd = section->asection->owner;
12978 struct abbrev_table *abbrev_table;
fe1b8b76 12979 gdb_byte *abbrev_ptr;
c906108c
SS
12980 struct abbrev_info *cur_abbrev;
12981 unsigned int abbrev_number, bytes_read, abbrev_name;
433df2d4 12982 unsigned int abbrev_form;
f3dd6933
DJ
12983 struct attr_abbrev *cur_attrs;
12984 unsigned int allocated_attrs;
c906108c 12985
433df2d4 12986 abbrev_table = XMALLOC (struct abbrev_table);
f4dc4d17 12987 abbrev_table->offset = offset;
433df2d4
DE
12988 obstack_init (&abbrev_table->abbrev_obstack);
12989 abbrev_table->abbrevs = obstack_alloc (&abbrev_table->abbrev_obstack,
12990 (ABBREV_HASH_SIZE
12991 * sizeof (struct abbrev_info *)));
12992 memset (abbrev_table->abbrevs, 0,
12993 ABBREV_HASH_SIZE * sizeof (struct abbrev_info *));
c906108c 12994
433df2d4
DE
12995 dwarf2_read_section (objfile, section);
12996 abbrev_ptr = section->buffer + offset.sect_off;
c906108c
SS
12997 abbrev_number = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
12998 abbrev_ptr += bytes_read;
12999
f3dd6933
DJ
13000 allocated_attrs = ATTR_ALLOC_CHUNK;
13001 cur_attrs = xmalloc (allocated_attrs * sizeof (struct attr_abbrev));
6e70227d 13002
0963b4bd 13003 /* Loop until we reach an abbrev number of 0. */
c906108c
SS
13004 while (abbrev_number)
13005 {
433df2d4 13006 cur_abbrev = abbrev_table_alloc_abbrev (abbrev_table);
c906108c
SS
13007
13008 /* read in abbrev header */
13009 cur_abbrev->number = abbrev_number;
13010 cur_abbrev->tag = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
13011 abbrev_ptr += bytes_read;
13012 cur_abbrev->has_children = read_1_byte (abfd, abbrev_ptr);
13013 abbrev_ptr += 1;
13014
13015 /* now read in declarations */
13016 abbrev_name = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
13017 abbrev_ptr += bytes_read;
13018 abbrev_form = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
13019 abbrev_ptr += bytes_read;
13020 while (abbrev_name)
13021 {
f3dd6933 13022 if (cur_abbrev->num_attrs == allocated_attrs)
c906108c 13023 {
f3dd6933
DJ
13024 allocated_attrs += ATTR_ALLOC_CHUNK;
13025 cur_attrs
13026 = xrealloc (cur_attrs, (allocated_attrs
13027 * sizeof (struct attr_abbrev)));
c906108c 13028 }
ae038cb0 13029
f3dd6933
DJ
13030 cur_attrs[cur_abbrev->num_attrs].name = abbrev_name;
13031 cur_attrs[cur_abbrev->num_attrs++].form = abbrev_form;
c906108c
SS
13032 abbrev_name = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
13033 abbrev_ptr += bytes_read;
13034 abbrev_form = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
13035 abbrev_ptr += bytes_read;
13036 }
13037
433df2d4 13038 cur_abbrev->attrs = obstack_alloc (&abbrev_table->abbrev_obstack,
f3dd6933
DJ
13039 (cur_abbrev->num_attrs
13040 * sizeof (struct attr_abbrev)));
13041 memcpy (cur_abbrev->attrs, cur_attrs,
13042 cur_abbrev->num_attrs * sizeof (struct attr_abbrev));
13043
433df2d4 13044 abbrev_table_add_abbrev (abbrev_table, abbrev_number, cur_abbrev);
c906108c
SS
13045
13046 /* Get next abbreviation.
13047 Under Irix6 the abbreviations for a compilation unit are not
c5aa993b
JM
13048 always properly terminated with an abbrev number of 0.
13049 Exit loop if we encounter an abbreviation which we have
13050 already read (which means we are about to read the abbreviations
13051 for the next compile unit) or if the end of the abbreviation
13052 table is reached. */
433df2d4 13053 if ((unsigned int) (abbrev_ptr - section->buffer) >= section->size)
c906108c
SS
13054 break;
13055 abbrev_number = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
13056 abbrev_ptr += bytes_read;
433df2d4 13057 if (abbrev_table_lookup_abbrev (abbrev_table, abbrev_number) != NULL)
c906108c
SS
13058 break;
13059 }
f3dd6933
DJ
13060
13061 xfree (cur_attrs);
433df2d4 13062 return abbrev_table;
c906108c
SS
13063}
13064
433df2d4 13065/* Free the resources held by ABBREV_TABLE. */
c906108c 13066
c906108c 13067static void
433df2d4 13068abbrev_table_free (struct abbrev_table *abbrev_table)
c906108c 13069{
433df2d4
DE
13070 obstack_free (&abbrev_table->abbrev_obstack, NULL);
13071 xfree (abbrev_table);
c906108c
SS
13072}
13073
f4dc4d17
DE
13074/* Same as abbrev_table_free but as a cleanup.
13075 We pass in a pointer to the pointer to the table so that we can
13076 set the pointer to NULL when we're done. It also simplifies
13077 build_type_unit_groups. */
13078
13079static void
13080abbrev_table_free_cleanup (void *table_ptr)
13081{
13082 struct abbrev_table **abbrev_table_ptr = table_ptr;
13083
13084 if (*abbrev_table_ptr != NULL)
13085 abbrev_table_free (*abbrev_table_ptr);
13086 *abbrev_table_ptr = NULL;
13087}
13088
433df2d4
DE
13089/* Read the abbrev table for CU from ABBREV_SECTION. */
13090
13091static void
13092dwarf2_read_abbrevs (struct dwarf2_cu *cu,
13093 struct dwarf2_section_info *abbrev_section)
c906108c 13094{
433df2d4
DE
13095 cu->abbrev_table =
13096 abbrev_table_read_table (abbrev_section, cu->header.abbrev_offset);
13097}
c906108c 13098
433df2d4 13099/* Release the memory used by the abbrev table for a compilation unit. */
c906108c 13100
433df2d4
DE
13101static void
13102dwarf2_free_abbrev_table (void *ptr_to_cu)
13103{
13104 struct dwarf2_cu *cu = ptr_to_cu;
c906108c 13105
433df2d4
DE
13106 abbrev_table_free (cu->abbrev_table);
13107 /* Set this to NULL so that we SEGV if we try to read it later,
13108 and also because free_comp_unit verifies this is NULL. */
13109 cu->abbrev_table = NULL;
13110}
13111\f
72bf9492
DJ
13112/* Returns nonzero if TAG represents a type that we might generate a partial
13113 symbol for. */
13114
13115static int
13116is_type_tag_for_partial (int tag)
13117{
13118 switch (tag)
13119 {
13120#if 0
13121 /* Some types that would be reasonable to generate partial symbols for,
13122 that we don't at present. */
13123 case DW_TAG_array_type:
13124 case DW_TAG_file_type:
13125 case DW_TAG_ptr_to_member_type:
13126 case DW_TAG_set_type:
13127 case DW_TAG_string_type:
13128 case DW_TAG_subroutine_type:
13129#endif
13130 case DW_TAG_base_type:
13131 case DW_TAG_class_type:
680b30c7 13132 case DW_TAG_interface_type:
72bf9492
DJ
13133 case DW_TAG_enumeration_type:
13134 case DW_TAG_structure_type:
13135 case DW_TAG_subrange_type:
13136 case DW_TAG_typedef:
13137 case DW_TAG_union_type:
13138 return 1;
13139 default:
13140 return 0;
13141 }
13142}
13143
13144/* Load all DIEs that are interesting for partial symbols into memory. */
13145
13146static struct partial_die_info *
dee91e82
DE
13147load_partial_dies (const struct die_reader_specs *reader,
13148 gdb_byte *info_ptr, int building_psymtab)
72bf9492 13149{
dee91e82 13150 struct dwarf2_cu *cu = reader->cu;
bb5ed363 13151 struct objfile *objfile = cu->objfile;
72bf9492
DJ
13152 struct partial_die_info *part_die;
13153 struct partial_die_info *parent_die, *last_die, *first_die = NULL;
13154 struct abbrev_info *abbrev;
13155 unsigned int bytes_read;
5afb4e99 13156 unsigned int load_all = 0;
72bf9492
DJ
13157 int nesting_level = 1;
13158
13159 parent_die = NULL;
13160 last_die = NULL;
13161
7adf1e79
DE
13162 gdb_assert (cu->per_cu != NULL);
13163 if (cu->per_cu->load_all_dies)
5afb4e99
DJ
13164 load_all = 1;
13165
72bf9492
DJ
13166 cu->partial_dies
13167 = htab_create_alloc_ex (cu->header.length / 12,
13168 partial_die_hash,
13169 partial_die_eq,
13170 NULL,
13171 &cu->comp_unit_obstack,
13172 hashtab_obstack_allocate,
13173 dummy_obstack_deallocate);
13174
13175 part_die = obstack_alloc (&cu->comp_unit_obstack,
13176 sizeof (struct partial_die_info));
13177
13178 while (1)
13179 {
13180 abbrev = peek_die_abbrev (info_ptr, &bytes_read, cu);
13181
13182 /* A NULL abbrev means the end of a series of children. */
13183 if (abbrev == NULL)
13184 {
13185 if (--nesting_level == 0)
13186 {
13187 /* PART_DIE was probably the last thing allocated on the
13188 comp_unit_obstack, so we could call obstack_free
13189 here. We don't do that because the waste is small,
13190 and will be cleaned up when we're done with this
13191 compilation unit. This way, we're also more robust
13192 against other users of the comp_unit_obstack. */
13193 return first_die;
13194 }
13195 info_ptr += bytes_read;
13196 last_die = parent_die;
13197 parent_die = parent_die->die_parent;
13198 continue;
13199 }
13200
98bfdba5
PA
13201 /* Check for template arguments. We never save these; if
13202 they're seen, we just mark the parent, and go on our way. */
13203 if (parent_die != NULL
13204 && cu->language == language_cplus
13205 && (abbrev->tag == DW_TAG_template_type_param
13206 || abbrev->tag == DW_TAG_template_value_param))
13207 {
13208 parent_die->has_template_arguments = 1;
13209
13210 if (!load_all)
13211 {
13212 /* We don't need a partial DIE for the template argument. */
dee91e82 13213 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
98bfdba5
PA
13214 continue;
13215 }
13216 }
13217
0d99eb77 13218 /* We only recurse into c++ subprograms looking for template arguments.
98bfdba5
PA
13219 Skip their other children. */
13220 if (!load_all
13221 && cu->language == language_cplus
13222 && parent_die != NULL
13223 && parent_die->tag == DW_TAG_subprogram)
13224 {
dee91e82 13225 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
98bfdba5
PA
13226 continue;
13227 }
13228
5afb4e99
DJ
13229 /* Check whether this DIE is interesting enough to save. Normally
13230 we would not be interested in members here, but there may be
13231 later variables referencing them via DW_AT_specification (for
13232 static members). */
13233 if (!load_all
13234 && !is_type_tag_for_partial (abbrev->tag)
72929c62 13235 && abbrev->tag != DW_TAG_constant
72bf9492
DJ
13236 && abbrev->tag != DW_TAG_enumerator
13237 && abbrev->tag != DW_TAG_subprogram
bc30ff58 13238 && abbrev->tag != DW_TAG_lexical_block
72bf9492 13239 && abbrev->tag != DW_TAG_variable
5afb4e99 13240 && abbrev->tag != DW_TAG_namespace
f55ee35c 13241 && abbrev->tag != DW_TAG_module
95554aad
TT
13242 && abbrev->tag != DW_TAG_member
13243 && abbrev->tag != DW_TAG_imported_unit)
72bf9492
DJ
13244 {
13245 /* Otherwise we skip to the next sibling, if any. */
dee91e82 13246 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
72bf9492
DJ
13247 continue;
13248 }
13249
dee91e82
DE
13250 info_ptr = read_partial_die (reader, part_die, abbrev, bytes_read,
13251 info_ptr);
72bf9492
DJ
13252
13253 /* This two-pass algorithm for processing partial symbols has a
13254 high cost in cache pressure. Thus, handle some simple cases
13255 here which cover the majority of C partial symbols. DIEs
13256 which neither have specification tags in them, nor could have
13257 specification tags elsewhere pointing at them, can simply be
13258 processed and discarded.
13259
13260 This segment is also optional; scan_partial_symbols and
13261 add_partial_symbol will handle these DIEs if we chain
13262 them in normally. When compilers which do not emit large
13263 quantities of duplicate debug information are more common,
13264 this code can probably be removed. */
13265
13266 /* Any complete simple types at the top level (pretty much all
13267 of them, for a language without namespaces), can be processed
13268 directly. */
13269 if (parent_die == NULL
13270 && part_die->has_specification == 0
13271 && part_die->is_declaration == 0
d8228535 13272 && ((part_die->tag == DW_TAG_typedef && !part_die->has_children)
72bf9492
DJ
13273 || part_die->tag == DW_TAG_base_type
13274 || part_die->tag == DW_TAG_subrange_type))
13275 {
13276 if (building_psymtab && part_die->name != NULL)
04a679b8 13277 add_psymbol_to_list (part_die->name, strlen (part_die->name), 0,
72bf9492 13278 VAR_DOMAIN, LOC_TYPEDEF,
bb5ed363
DE
13279 &objfile->static_psymbols,
13280 0, (CORE_ADDR) 0, cu->language, objfile);
dee91e82 13281 info_ptr = locate_pdi_sibling (reader, part_die, info_ptr);
72bf9492
DJ
13282 continue;
13283 }
13284
d8228535
JK
13285 /* The exception for DW_TAG_typedef with has_children above is
13286 a workaround of GCC PR debug/47510. In the case of this complaint
13287 type_name_no_tag_or_error will error on such types later.
13288
13289 GDB skipped children of DW_TAG_typedef by the shortcut above and then
13290 it could not find the child DIEs referenced later, this is checked
13291 above. In correct DWARF DW_TAG_typedef should have no children. */
13292
13293 if (part_die->tag == DW_TAG_typedef && part_die->has_children)
13294 complaint (&symfile_complaints,
13295 _("DW_TAG_typedef has childen - GCC PR debug/47510 bug "
13296 "- DIE at 0x%x [in module %s]"),
b64f50a1 13297 part_die->offset.sect_off, objfile->name);
d8228535 13298
72bf9492
DJ
13299 /* If we're at the second level, and we're an enumerator, and
13300 our parent has no specification (meaning possibly lives in a
13301 namespace elsewhere), then we can add the partial symbol now
13302 instead of queueing it. */
13303 if (part_die->tag == DW_TAG_enumerator
13304 && parent_die != NULL
13305 && parent_die->die_parent == NULL
13306 && parent_die->tag == DW_TAG_enumeration_type
13307 && parent_die->has_specification == 0)
13308 {
13309 if (part_die->name == NULL)
3e43a32a
MS
13310 complaint (&symfile_complaints,
13311 _("malformed enumerator DIE ignored"));
72bf9492 13312 else if (building_psymtab)
04a679b8 13313 add_psymbol_to_list (part_die->name, strlen (part_die->name), 0,
72bf9492 13314 VAR_DOMAIN, LOC_CONST,
987504bb
JJ
13315 (cu->language == language_cplus
13316 || cu->language == language_java)
bb5ed363
DE
13317 ? &objfile->global_psymbols
13318 : &objfile->static_psymbols,
13319 0, (CORE_ADDR) 0, cu->language, objfile);
72bf9492 13320
dee91e82 13321 info_ptr = locate_pdi_sibling (reader, part_die, info_ptr);
72bf9492
DJ
13322 continue;
13323 }
13324
13325 /* We'll save this DIE so link it in. */
13326 part_die->die_parent = parent_die;
13327 part_die->die_sibling = NULL;
13328 part_die->die_child = NULL;
13329
13330 if (last_die && last_die == parent_die)
13331 last_die->die_child = part_die;
13332 else if (last_die)
13333 last_die->die_sibling = part_die;
13334
13335 last_die = part_die;
13336
13337 if (first_die == NULL)
13338 first_die = part_die;
13339
13340 /* Maybe add the DIE to the hash table. Not all DIEs that we
13341 find interesting need to be in the hash table, because we
13342 also have the parent/sibling/child chains; only those that we
13343 might refer to by offset later during partial symbol reading.
13344
13345 For now this means things that might have be the target of a
13346 DW_AT_specification, DW_AT_abstract_origin, or
13347 DW_AT_extension. DW_AT_extension will refer only to
13348 namespaces; DW_AT_abstract_origin refers to functions (and
13349 many things under the function DIE, but we do not recurse
13350 into function DIEs during partial symbol reading) and
13351 possibly variables as well; DW_AT_specification refers to
13352 declarations. Declarations ought to have the DW_AT_declaration
13353 flag. It happens that GCC forgets to put it in sometimes, but
13354 only for functions, not for types.
13355
13356 Adding more things than necessary to the hash table is harmless
13357 except for the performance cost. Adding too few will result in
5afb4e99
DJ
13358 wasted time in find_partial_die, when we reread the compilation
13359 unit with load_all_dies set. */
72bf9492 13360
5afb4e99 13361 if (load_all
72929c62 13362 || abbrev->tag == DW_TAG_constant
5afb4e99 13363 || abbrev->tag == DW_TAG_subprogram
72bf9492
DJ
13364 || abbrev->tag == DW_TAG_variable
13365 || abbrev->tag == DW_TAG_namespace
13366 || part_die->is_declaration)
13367 {
13368 void **slot;
13369
13370 slot = htab_find_slot_with_hash (cu->partial_dies, part_die,
b64f50a1 13371 part_die->offset.sect_off, INSERT);
72bf9492
DJ
13372 *slot = part_die;
13373 }
13374
13375 part_die = obstack_alloc (&cu->comp_unit_obstack,
13376 sizeof (struct partial_die_info));
13377
13378 /* For some DIEs we want to follow their children (if any). For C
bc30ff58 13379 we have no reason to follow the children of structures; for other
98bfdba5
PA
13380 languages we have to, so that we can get at method physnames
13381 to infer fully qualified class names, for DW_AT_specification,
13382 and for C++ template arguments. For C++, we also look one level
13383 inside functions to find template arguments (if the name of the
13384 function does not already contain the template arguments).
bc30ff58
JB
13385
13386 For Ada, we need to scan the children of subprograms and lexical
13387 blocks as well because Ada allows the definition of nested
13388 entities that could be interesting for the debugger, such as
13389 nested subprograms for instance. */
72bf9492 13390 if (last_die->has_children
5afb4e99
DJ
13391 && (load_all
13392 || last_die->tag == DW_TAG_namespace
f55ee35c 13393 || last_die->tag == DW_TAG_module
72bf9492 13394 || last_die->tag == DW_TAG_enumeration_type
98bfdba5
PA
13395 || (cu->language == language_cplus
13396 && last_die->tag == DW_TAG_subprogram
13397 && (last_die->name == NULL
13398 || strchr (last_die->name, '<') == NULL))
72bf9492
DJ
13399 || (cu->language != language_c
13400 && (last_die->tag == DW_TAG_class_type
680b30c7 13401 || last_die->tag == DW_TAG_interface_type
72bf9492 13402 || last_die->tag == DW_TAG_structure_type
bc30ff58
JB
13403 || last_die->tag == DW_TAG_union_type))
13404 || (cu->language == language_ada
13405 && (last_die->tag == DW_TAG_subprogram
13406 || last_die->tag == DW_TAG_lexical_block))))
72bf9492
DJ
13407 {
13408 nesting_level++;
13409 parent_die = last_die;
13410 continue;
13411 }
13412
13413 /* Otherwise we skip to the next sibling, if any. */
dee91e82 13414 info_ptr = locate_pdi_sibling (reader, last_die, info_ptr);
72bf9492
DJ
13415
13416 /* Back to the top, do it again. */
13417 }
13418}
13419
c906108c
SS
13420/* Read a minimal amount of information into the minimal die structure. */
13421
fe1b8b76 13422static gdb_byte *
dee91e82
DE
13423read_partial_die (const struct die_reader_specs *reader,
13424 struct partial_die_info *part_die,
13425 struct abbrev_info *abbrev, unsigned int abbrev_len,
13426 gdb_byte *info_ptr)
c906108c 13427{
dee91e82 13428 struct dwarf2_cu *cu = reader->cu;
bb5ed363 13429 struct objfile *objfile = cu->objfile;
dee91e82 13430 gdb_byte *buffer = reader->buffer;
fa238c03 13431 unsigned int i;
c906108c 13432 struct attribute attr;
c5aa993b 13433 int has_low_pc_attr = 0;
c906108c 13434 int has_high_pc_attr = 0;
91da1414 13435 int high_pc_relative = 0;
c906108c 13436
72bf9492 13437 memset (part_die, 0, sizeof (struct partial_die_info));
c906108c 13438
b64f50a1 13439 part_die->offset.sect_off = info_ptr - buffer;
72bf9492
DJ
13440
13441 info_ptr += abbrev_len;
13442
13443 if (abbrev == NULL)
13444 return info_ptr;
13445
c906108c
SS
13446 part_die->tag = abbrev->tag;
13447 part_die->has_children = abbrev->has_children;
c906108c
SS
13448
13449 for (i = 0; i < abbrev->num_attrs; ++i)
13450 {
dee91e82 13451 info_ptr = read_attribute (reader, &attr, &abbrev->attrs[i], info_ptr);
c906108c
SS
13452
13453 /* Store the data if it is of an attribute we want to keep in a
c5aa993b 13454 partial symbol table. */
c906108c
SS
13455 switch (attr.name)
13456 {
13457 case DW_AT_name:
71c25dea
TT
13458 switch (part_die->tag)
13459 {
13460 case DW_TAG_compile_unit:
95554aad 13461 case DW_TAG_partial_unit:
348e048f 13462 case DW_TAG_type_unit:
71c25dea
TT
13463 /* Compilation units have a DW_AT_name that is a filename, not
13464 a source language identifier. */
13465 case DW_TAG_enumeration_type:
13466 case DW_TAG_enumerator:
13467 /* These tags always have simple identifiers already; no need
13468 to canonicalize them. */
13469 part_die->name = DW_STRING (&attr);
13470 break;
13471 default:
13472 part_die->name
13473 = dwarf2_canonicalize_name (DW_STRING (&attr), cu,
bb5ed363 13474 &objfile->objfile_obstack);
71c25dea
TT
13475 break;
13476 }
c906108c 13477 break;
31ef98ae 13478 case DW_AT_linkage_name:
c906108c 13479 case DW_AT_MIPS_linkage_name:
31ef98ae
TT
13480 /* Note that both forms of linkage name might appear. We
13481 assume they will be the same, and we only store the last
13482 one we see. */
94af9270
KS
13483 if (cu->language == language_ada)
13484 part_die->name = DW_STRING (&attr);
abc72ce4 13485 part_die->linkage_name = DW_STRING (&attr);
c906108c
SS
13486 break;
13487 case DW_AT_low_pc:
13488 has_low_pc_attr = 1;
13489 part_die->lowpc = DW_ADDR (&attr);
13490 break;
13491 case DW_AT_high_pc:
13492 has_high_pc_attr = 1;
3019eac3
DE
13493 if (attr.form == DW_FORM_addr
13494 || attr.form == DW_FORM_GNU_addr_index)
91da1414
MW
13495 part_die->highpc = DW_ADDR (&attr);
13496 else
13497 {
13498 high_pc_relative = 1;
13499 part_die->highpc = DW_UNSND (&attr);
13500 }
c906108c
SS
13501 break;
13502 case DW_AT_location:
0963b4bd 13503 /* Support the .debug_loc offsets. */
8e19ed76
PS
13504 if (attr_form_is_block (&attr))
13505 {
95554aad 13506 part_die->d.locdesc = DW_BLOCK (&attr);
8e19ed76 13507 }
3690dd37 13508 else if (attr_form_is_section_offset (&attr))
8e19ed76 13509 {
4d3c2250 13510 dwarf2_complex_location_expr_complaint ();
8e19ed76
PS
13511 }
13512 else
13513 {
4d3c2250
KB
13514 dwarf2_invalid_attrib_class_complaint ("DW_AT_location",
13515 "partial symbol information");
8e19ed76 13516 }
c906108c 13517 break;
c906108c
SS
13518 case DW_AT_external:
13519 part_die->is_external = DW_UNSND (&attr);
13520 break;
13521 case DW_AT_declaration:
13522 part_die->is_declaration = DW_UNSND (&attr);
13523 break;
13524 case DW_AT_type:
13525 part_die->has_type = 1;
13526 break;
13527 case DW_AT_abstract_origin:
13528 case DW_AT_specification:
72bf9492
DJ
13529 case DW_AT_extension:
13530 part_die->has_specification = 1;
c764a876 13531 part_die->spec_offset = dwarf2_get_ref_die_offset (&attr);
36586728
TT
13532 part_die->spec_is_dwz = (attr.form == DW_FORM_GNU_ref_alt
13533 || cu->per_cu->is_dwz);
c906108c
SS
13534 break;
13535 case DW_AT_sibling:
13536 /* Ignore absolute siblings, they might point outside of
13537 the current compile unit. */
13538 if (attr.form == DW_FORM_ref_addr)
3e43a32a
MS
13539 complaint (&symfile_complaints,
13540 _("ignoring absolute DW_AT_sibling"));
c906108c 13541 else
b64f50a1 13542 part_die->sibling = buffer + dwarf2_get_ref_die_offset (&attr).sect_off;
c906108c 13543 break;
fa4028e9
JB
13544 case DW_AT_byte_size:
13545 part_die->has_byte_size = 1;
13546 break;
68511cec
CES
13547 case DW_AT_calling_convention:
13548 /* DWARF doesn't provide a way to identify a program's source-level
13549 entry point. DW_AT_calling_convention attributes are only meant
13550 to describe functions' calling conventions.
13551
13552 However, because it's a necessary piece of information in
13553 Fortran, and because DW_CC_program is the only piece of debugging
13554 information whose definition refers to a 'main program' at all,
13555 several compilers have begun marking Fortran main programs with
13556 DW_CC_program --- even when those functions use the standard
13557 calling conventions.
13558
13559 So until DWARF specifies a way to provide this information and
13560 compilers pick up the new representation, we'll support this
13561 practice. */
13562 if (DW_UNSND (&attr) == DW_CC_program
13563 && cu->language == language_fortran)
01f8c46d
JK
13564 {
13565 set_main_name (part_die->name);
13566
13567 /* As this DIE has a static linkage the name would be difficult
13568 to look up later. */
13569 language_of_main = language_fortran;
13570 }
68511cec 13571 break;
481860b3
GB
13572 case DW_AT_inline:
13573 if (DW_UNSND (&attr) == DW_INL_inlined
13574 || DW_UNSND (&attr) == DW_INL_declared_inlined)
13575 part_die->may_be_inlined = 1;
13576 break;
95554aad
TT
13577
13578 case DW_AT_import:
13579 if (part_die->tag == DW_TAG_imported_unit)
36586728
TT
13580 {
13581 part_die->d.offset = dwarf2_get_ref_die_offset (&attr);
13582 part_die->is_dwz = (attr.form == DW_FORM_GNU_ref_alt
13583 || cu->per_cu->is_dwz);
13584 }
95554aad
TT
13585 break;
13586
c906108c
SS
13587 default:
13588 break;
13589 }
13590 }
13591
91da1414
MW
13592 if (high_pc_relative)
13593 part_die->highpc += part_die->lowpc;
13594
9373cf26
JK
13595 if (has_low_pc_attr && has_high_pc_attr)
13596 {
13597 /* When using the GNU linker, .gnu.linkonce. sections are used to
13598 eliminate duplicate copies of functions and vtables and such.
13599 The linker will arbitrarily choose one and discard the others.
13600 The AT_*_pc values for such functions refer to local labels in
13601 these sections. If the section from that file was discarded, the
13602 labels are not in the output, so the relocs get a value of 0.
13603 If this is a discarded function, mark the pc bounds as invalid,
13604 so that GDB will ignore it. */
13605 if (part_die->lowpc == 0 && !dwarf2_per_objfile->has_section_at_zero)
13606 {
bb5ed363 13607 struct gdbarch *gdbarch = get_objfile_arch (objfile);
9373cf26
JK
13608
13609 complaint (&symfile_complaints,
13610 _("DW_AT_low_pc %s is zero "
13611 "for DIE at 0x%x [in module %s]"),
13612 paddress (gdbarch, part_die->lowpc),
b64f50a1 13613 part_die->offset.sect_off, objfile->name);
9373cf26
JK
13614 }
13615 /* dwarf2_get_pc_bounds has also the strict low < high requirement. */
13616 else if (part_die->lowpc >= part_die->highpc)
13617 {
bb5ed363 13618 struct gdbarch *gdbarch = get_objfile_arch (objfile);
9373cf26
JK
13619
13620 complaint (&symfile_complaints,
13621 _("DW_AT_low_pc %s is not < DW_AT_high_pc %s "
13622 "for DIE at 0x%x [in module %s]"),
13623 paddress (gdbarch, part_die->lowpc),
13624 paddress (gdbarch, part_die->highpc),
b64f50a1 13625 part_die->offset.sect_off, objfile->name);
9373cf26
JK
13626 }
13627 else
13628 part_die->has_pc_info = 1;
13629 }
85cbf3d3 13630
c906108c
SS
13631 return info_ptr;
13632}
13633
72bf9492
DJ
13634/* Find a cached partial DIE at OFFSET in CU. */
13635
13636static struct partial_die_info *
b64f50a1 13637find_partial_die_in_comp_unit (sect_offset offset, struct dwarf2_cu *cu)
72bf9492
DJ
13638{
13639 struct partial_die_info *lookup_die = NULL;
13640 struct partial_die_info part_die;
13641
13642 part_die.offset = offset;
b64f50a1
JK
13643 lookup_die = htab_find_with_hash (cu->partial_dies, &part_die,
13644 offset.sect_off);
72bf9492 13645
72bf9492
DJ
13646 return lookup_die;
13647}
13648
348e048f
DE
13649/* Find a partial DIE at OFFSET, which may or may not be in CU,
13650 except in the case of .debug_types DIEs which do not reference
13651 outside their CU (they do however referencing other types via
55f1336d 13652 DW_FORM_ref_sig8). */
72bf9492
DJ
13653
13654static struct partial_die_info *
36586728 13655find_partial_die (sect_offset offset, int offset_in_dwz, struct dwarf2_cu *cu)
72bf9492 13656{
bb5ed363 13657 struct objfile *objfile = cu->objfile;
5afb4e99
DJ
13658 struct dwarf2_per_cu_data *per_cu = NULL;
13659 struct partial_die_info *pd = NULL;
72bf9492 13660
36586728
TT
13661 if (offset_in_dwz == cu->per_cu->is_dwz
13662 && offset_in_cu_p (&cu->header, offset))
5afb4e99
DJ
13663 {
13664 pd = find_partial_die_in_comp_unit (offset, cu);
13665 if (pd != NULL)
13666 return pd;
0d99eb77
DE
13667 /* We missed recording what we needed.
13668 Load all dies and try again. */
13669 per_cu = cu->per_cu;
5afb4e99 13670 }
0d99eb77
DE
13671 else
13672 {
13673 /* TUs don't reference other CUs/TUs (except via type signatures). */
3019eac3 13674 if (cu->per_cu->is_debug_types)
0d99eb77
DE
13675 {
13676 error (_("Dwarf Error: Type Unit at offset 0x%lx contains"
13677 " external reference to offset 0x%lx [in module %s].\n"),
13678 (long) cu->header.offset.sect_off, (long) offset.sect_off,
13679 bfd_get_filename (objfile->obfd));
13680 }
36586728
TT
13681 per_cu = dwarf2_find_containing_comp_unit (offset, offset_in_dwz,
13682 objfile);
72bf9492 13683
0d99eb77
DE
13684 if (per_cu->cu == NULL || per_cu->cu->partial_dies == NULL)
13685 load_partial_comp_unit (per_cu);
ae038cb0 13686
0d99eb77
DE
13687 per_cu->cu->last_used = 0;
13688 pd = find_partial_die_in_comp_unit (offset, per_cu->cu);
13689 }
5afb4e99 13690
dee91e82
DE
13691 /* If we didn't find it, and not all dies have been loaded,
13692 load them all and try again. */
13693
5afb4e99
DJ
13694 if (pd == NULL && per_cu->load_all_dies == 0)
13695 {
5afb4e99 13696 per_cu->load_all_dies = 1;
fd820528
DE
13697
13698 /* This is nasty. When we reread the DIEs, somewhere up the call chain
13699 THIS_CU->cu may already be in use. So we can't just free it and
13700 replace its DIEs with the ones we read in. Instead, we leave those
13701 DIEs alone (which can still be in use, e.g. in scan_partial_symbols),
13702 and clobber THIS_CU->cu->partial_dies with the hash table for the new
13703 set. */
dee91e82 13704 load_partial_comp_unit (per_cu);
5afb4e99
DJ
13705
13706 pd = find_partial_die_in_comp_unit (offset, per_cu->cu);
13707 }
13708
13709 if (pd == NULL)
13710 internal_error (__FILE__, __LINE__,
3e43a32a
MS
13711 _("could not find partial DIE 0x%x "
13712 "in cache [from module %s]\n"),
b64f50a1 13713 offset.sect_off, bfd_get_filename (objfile->obfd));
5afb4e99 13714 return pd;
72bf9492
DJ
13715}
13716
abc72ce4
DE
13717/* See if we can figure out if the class lives in a namespace. We do
13718 this by looking for a member function; its demangled name will
13719 contain namespace info, if there is any. */
13720
13721static void
13722guess_partial_die_structure_name (struct partial_die_info *struct_pdi,
13723 struct dwarf2_cu *cu)
13724{
13725 /* NOTE: carlton/2003-10-07: Getting the info this way changes
13726 what template types look like, because the demangler
13727 frequently doesn't give the same name as the debug info. We
13728 could fix this by only using the demangled name to get the
13729 prefix (but see comment in read_structure_type). */
13730
13731 struct partial_die_info *real_pdi;
13732 struct partial_die_info *child_pdi;
13733
13734 /* If this DIE (this DIE's specification, if any) has a parent, then
13735 we should not do this. We'll prepend the parent's fully qualified
13736 name when we create the partial symbol. */
13737
13738 real_pdi = struct_pdi;
13739 while (real_pdi->has_specification)
36586728
TT
13740 real_pdi = find_partial_die (real_pdi->spec_offset,
13741 real_pdi->spec_is_dwz, cu);
abc72ce4
DE
13742
13743 if (real_pdi->die_parent != NULL)
13744 return;
13745
13746 for (child_pdi = struct_pdi->die_child;
13747 child_pdi != NULL;
13748 child_pdi = child_pdi->die_sibling)
13749 {
13750 if (child_pdi->tag == DW_TAG_subprogram
13751 && child_pdi->linkage_name != NULL)
13752 {
13753 char *actual_class_name
13754 = language_class_name_from_physname (cu->language_defn,
13755 child_pdi->linkage_name);
13756 if (actual_class_name != NULL)
13757 {
13758 struct_pdi->name
13759 = obsavestring (actual_class_name,
13760 strlen (actual_class_name),
13761 &cu->objfile->objfile_obstack);
13762 xfree (actual_class_name);
13763 }
13764 break;
13765 }
13766 }
13767}
13768
72bf9492
DJ
13769/* Adjust PART_DIE before generating a symbol for it. This function
13770 may set the is_external flag or change the DIE's name. */
13771
13772static void
13773fixup_partial_die (struct partial_die_info *part_die,
13774 struct dwarf2_cu *cu)
13775{
abc72ce4
DE
13776 /* Once we've fixed up a die, there's no point in doing so again.
13777 This also avoids a memory leak if we were to call
13778 guess_partial_die_structure_name multiple times. */
13779 if (part_die->fixup_called)
13780 return;
13781
72bf9492
DJ
13782 /* If we found a reference attribute and the DIE has no name, try
13783 to find a name in the referred to DIE. */
13784
13785 if (part_die->name == NULL && part_die->has_specification)
13786 {
13787 struct partial_die_info *spec_die;
72bf9492 13788
36586728
TT
13789 spec_die = find_partial_die (part_die->spec_offset,
13790 part_die->spec_is_dwz, cu);
72bf9492 13791
10b3939b 13792 fixup_partial_die (spec_die, cu);
72bf9492
DJ
13793
13794 if (spec_die->name)
13795 {
13796 part_die->name = spec_die->name;
13797
13798 /* Copy DW_AT_external attribute if it is set. */
13799 if (spec_die->is_external)
13800 part_die->is_external = spec_die->is_external;
13801 }
13802 }
13803
13804 /* Set default names for some unnamed DIEs. */
72bf9492
DJ
13805
13806 if (part_die->name == NULL && part_die->tag == DW_TAG_namespace)
2b1dbab0 13807 part_die->name = CP_ANONYMOUS_NAMESPACE_STR;
72bf9492 13808
abc72ce4
DE
13809 /* If there is no parent die to provide a namespace, and there are
13810 children, see if we can determine the namespace from their linkage
122d1940 13811 name. */
abc72ce4 13812 if (cu->language == language_cplus
8b70b953 13813 && !VEC_empty (dwarf2_section_info_def, dwarf2_per_objfile->types)
abc72ce4
DE
13814 && part_die->die_parent == NULL
13815 && part_die->has_children
13816 && (part_die->tag == DW_TAG_class_type
13817 || part_die->tag == DW_TAG_structure_type
13818 || part_die->tag == DW_TAG_union_type))
13819 guess_partial_die_structure_name (part_die, cu);
13820
53832f31
TT
13821 /* GCC might emit a nameless struct or union that has a linkage
13822 name. See http://gcc.gnu.org/bugzilla/show_bug.cgi?id=47510. */
13823 if (part_die->name == NULL
96408a79
SA
13824 && (part_die->tag == DW_TAG_class_type
13825 || part_die->tag == DW_TAG_interface_type
13826 || part_die->tag == DW_TAG_structure_type
13827 || part_die->tag == DW_TAG_union_type)
53832f31
TT
13828 && part_die->linkage_name != NULL)
13829 {
13830 char *demangled;
13831
13832 demangled = cplus_demangle (part_die->linkage_name, DMGL_TYPES);
13833 if (demangled)
13834 {
96408a79
SA
13835 const char *base;
13836
13837 /* Strip any leading namespaces/classes, keep only the base name.
13838 DW_AT_name for named DIEs does not contain the prefixes. */
13839 base = strrchr (demangled, ':');
13840 if (base && base > demangled && base[-1] == ':')
13841 base++;
13842 else
13843 base = demangled;
13844
13845 part_die->name = obsavestring (base, strlen (base),
53832f31
TT
13846 &cu->objfile->objfile_obstack);
13847 xfree (demangled);
13848 }
13849 }
13850
abc72ce4 13851 part_die->fixup_called = 1;
72bf9492
DJ
13852}
13853
a8329558 13854/* Read an attribute value described by an attribute form. */
c906108c 13855
fe1b8b76 13856static gdb_byte *
dee91e82
DE
13857read_attribute_value (const struct die_reader_specs *reader,
13858 struct attribute *attr, unsigned form,
13859 gdb_byte *info_ptr)
c906108c 13860{
dee91e82
DE
13861 struct dwarf2_cu *cu = reader->cu;
13862 bfd *abfd = reader->abfd;
e7c27a73 13863 struct comp_unit_head *cu_header = &cu->header;
c906108c
SS
13864 unsigned int bytes_read;
13865 struct dwarf_block *blk;
13866
a8329558
KW
13867 attr->form = form;
13868 switch (form)
c906108c 13869 {
c906108c 13870 case DW_FORM_ref_addr:
ae411497 13871 if (cu->header.version == 2)
4568ecf9 13872 DW_UNSND (attr) = read_address (abfd, info_ptr, cu, &bytes_read);
ae411497 13873 else
4568ecf9
DE
13874 DW_UNSND (attr) = read_offset (abfd, info_ptr,
13875 &cu->header, &bytes_read);
ae411497
TT
13876 info_ptr += bytes_read;
13877 break;
36586728
TT
13878 case DW_FORM_GNU_ref_alt:
13879 DW_UNSND (attr) = read_offset (abfd, info_ptr, &cu->header, &bytes_read);
13880 info_ptr += bytes_read;
13881 break;
ae411497 13882 case DW_FORM_addr:
e7c27a73 13883 DW_ADDR (attr) = read_address (abfd, info_ptr, cu, &bytes_read);
107d2387 13884 info_ptr += bytes_read;
c906108c
SS
13885 break;
13886 case DW_FORM_block2:
7b5a2f43 13887 blk = dwarf_alloc_block (cu);
c906108c
SS
13888 blk->size = read_2_bytes (abfd, info_ptr);
13889 info_ptr += 2;
13890 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
13891 info_ptr += blk->size;
13892 DW_BLOCK (attr) = blk;
13893 break;
13894 case DW_FORM_block4:
7b5a2f43 13895 blk = dwarf_alloc_block (cu);
c906108c
SS
13896 blk->size = read_4_bytes (abfd, info_ptr);
13897 info_ptr += 4;
13898 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
13899 info_ptr += blk->size;
13900 DW_BLOCK (attr) = blk;
13901 break;
13902 case DW_FORM_data2:
13903 DW_UNSND (attr) = read_2_bytes (abfd, info_ptr);
13904 info_ptr += 2;
13905 break;
13906 case DW_FORM_data4:
13907 DW_UNSND (attr) = read_4_bytes (abfd, info_ptr);
13908 info_ptr += 4;
13909 break;
13910 case DW_FORM_data8:
13911 DW_UNSND (attr) = read_8_bytes (abfd, info_ptr);
13912 info_ptr += 8;
13913 break;
2dc7f7b3
TT
13914 case DW_FORM_sec_offset:
13915 DW_UNSND (attr) = read_offset (abfd, info_ptr, &cu->header, &bytes_read);
13916 info_ptr += bytes_read;
13917 break;
c906108c 13918 case DW_FORM_string:
9b1c24c8 13919 DW_STRING (attr) = read_direct_string (abfd, info_ptr, &bytes_read);
8285870a 13920 DW_STRING_IS_CANONICAL (attr) = 0;
c906108c
SS
13921 info_ptr += bytes_read;
13922 break;
4bdf3d34 13923 case DW_FORM_strp:
36586728
TT
13924 if (!cu->per_cu->is_dwz)
13925 {
13926 DW_STRING (attr) = read_indirect_string (abfd, info_ptr, cu_header,
13927 &bytes_read);
13928 DW_STRING_IS_CANONICAL (attr) = 0;
13929 info_ptr += bytes_read;
13930 break;
13931 }
13932 /* FALLTHROUGH */
13933 case DW_FORM_GNU_strp_alt:
13934 {
13935 struct dwz_file *dwz = dwarf2_get_dwz_file ();
13936 LONGEST str_offset = read_offset (abfd, info_ptr, cu_header,
13937 &bytes_read);
13938
13939 DW_STRING (attr) = read_indirect_string_from_dwz (dwz, str_offset);
13940 DW_STRING_IS_CANONICAL (attr) = 0;
13941 info_ptr += bytes_read;
13942 }
4bdf3d34 13943 break;
2dc7f7b3 13944 case DW_FORM_exprloc:
c906108c 13945 case DW_FORM_block:
7b5a2f43 13946 blk = dwarf_alloc_block (cu);
c906108c
SS
13947 blk->size = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
13948 info_ptr += bytes_read;
13949 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
13950 info_ptr += blk->size;
13951 DW_BLOCK (attr) = blk;
13952 break;
13953 case DW_FORM_block1:
7b5a2f43 13954 blk = dwarf_alloc_block (cu);
c906108c
SS
13955 blk->size = read_1_byte (abfd, info_ptr);
13956 info_ptr += 1;
13957 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
13958 info_ptr += blk->size;
13959 DW_BLOCK (attr) = blk;
13960 break;
13961 case DW_FORM_data1:
13962 DW_UNSND (attr) = read_1_byte (abfd, info_ptr);
13963 info_ptr += 1;
13964 break;
13965 case DW_FORM_flag:
13966 DW_UNSND (attr) = read_1_byte (abfd, info_ptr);
13967 info_ptr += 1;
13968 break;
2dc7f7b3
TT
13969 case DW_FORM_flag_present:
13970 DW_UNSND (attr) = 1;
13971 break;
c906108c
SS
13972 case DW_FORM_sdata:
13973 DW_SND (attr) = read_signed_leb128 (abfd, info_ptr, &bytes_read);
13974 info_ptr += bytes_read;
13975 break;
13976 case DW_FORM_udata:
13977 DW_UNSND (attr) = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
13978 info_ptr += bytes_read;
13979 break;
13980 case DW_FORM_ref1:
4568ecf9
DE
13981 DW_UNSND (attr) = (cu->header.offset.sect_off
13982 + read_1_byte (abfd, info_ptr));
c906108c
SS
13983 info_ptr += 1;
13984 break;
13985 case DW_FORM_ref2:
4568ecf9
DE
13986 DW_UNSND (attr) = (cu->header.offset.sect_off
13987 + read_2_bytes (abfd, info_ptr));
c906108c
SS
13988 info_ptr += 2;
13989 break;
13990 case DW_FORM_ref4:
4568ecf9
DE
13991 DW_UNSND (attr) = (cu->header.offset.sect_off
13992 + read_4_bytes (abfd, info_ptr));
c906108c
SS
13993 info_ptr += 4;
13994 break;
613e1657 13995 case DW_FORM_ref8:
4568ecf9
DE
13996 DW_UNSND (attr) = (cu->header.offset.sect_off
13997 + read_8_bytes (abfd, info_ptr));
613e1657
KB
13998 info_ptr += 8;
13999 break;
55f1336d 14000 case DW_FORM_ref_sig8:
348e048f
DE
14001 /* Convert the signature to something we can record in DW_UNSND
14002 for later lookup.
14003 NOTE: This is NULL if the type wasn't found. */
14004 DW_SIGNATURED_TYPE (attr) =
e319fa28 14005 lookup_signatured_type (read_8_bytes (abfd, info_ptr));
348e048f
DE
14006 info_ptr += 8;
14007 break;
c906108c 14008 case DW_FORM_ref_udata:
4568ecf9
DE
14009 DW_UNSND (attr) = (cu->header.offset.sect_off
14010 + read_unsigned_leb128 (abfd, info_ptr, &bytes_read));
c906108c
SS
14011 info_ptr += bytes_read;
14012 break;
c906108c 14013 case DW_FORM_indirect:
a8329558
KW
14014 form = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
14015 info_ptr += bytes_read;
dee91e82 14016 info_ptr = read_attribute_value (reader, attr, form, info_ptr);
a8329558 14017 break;
3019eac3
DE
14018 case DW_FORM_GNU_addr_index:
14019 if (reader->dwo_file == NULL)
14020 {
14021 /* For now flag a hard error.
14022 Later we can turn this into a complaint. */
14023 error (_("Dwarf Error: %s found in non-DWO CU [in module %s]"),
14024 dwarf_form_name (form),
14025 bfd_get_filename (abfd));
14026 }
14027 DW_ADDR (attr) = read_addr_index_from_leb128 (cu, info_ptr, &bytes_read);
14028 info_ptr += bytes_read;
14029 break;
14030 case DW_FORM_GNU_str_index:
14031 if (reader->dwo_file == NULL)
14032 {
14033 /* For now flag a hard error.
14034 Later we can turn this into a complaint if warranted. */
14035 error (_("Dwarf Error: %s found in non-DWO CU [in module %s]"),
14036 dwarf_form_name (form),
14037 bfd_get_filename (abfd));
14038 }
14039 {
14040 ULONGEST str_index =
14041 read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
14042
14043 DW_STRING (attr) = read_str_index (reader, cu, str_index);
14044 DW_STRING_IS_CANONICAL (attr) = 0;
14045 info_ptr += bytes_read;
14046 }
14047 break;
c906108c 14048 default:
8a3fe4f8 14049 error (_("Dwarf Error: Cannot handle %s in DWARF reader [in module %s]"),
659b0389
ML
14050 dwarf_form_name (form),
14051 bfd_get_filename (abfd));
c906108c 14052 }
28e94949 14053
36586728
TT
14054 /* Super hack. */
14055 if (cu->per_cu->is_dwz && is_ref_attr (attr))
14056 attr->form = DW_FORM_GNU_ref_alt;
14057
28e94949
JB
14058 /* We have seen instances where the compiler tried to emit a byte
14059 size attribute of -1 which ended up being encoded as an unsigned
14060 0xffffffff. Although 0xffffffff is technically a valid size value,
14061 an object of this size seems pretty unlikely so we can relatively
14062 safely treat these cases as if the size attribute was invalid and
14063 treat them as zero by default. */
14064 if (attr->name == DW_AT_byte_size
14065 && form == DW_FORM_data4
14066 && DW_UNSND (attr) >= 0xffffffff)
01c66ae6
JB
14067 {
14068 complaint
14069 (&symfile_complaints,
43bbcdc2
PH
14070 _("Suspicious DW_AT_byte_size value treated as zero instead of %s"),
14071 hex_string (DW_UNSND (attr)));
01c66ae6
JB
14072 DW_UNSND (attr) = 0;
14073 }
28e94949 14074
c906108c
SS
14075 return info_ptr;
14076}
14077
a8329558
KW
14078/* Read an attribute described by an abbreviated attribute. */
14079
fe1b8b76 14080static gdb_byte *
dee91e82
DE
14081read_attribute (const struct die_reader_specs *reader,
14082 struct attribute *attr, struct attr_abbrev *abbrev,
14083 gdb_byte *info_ptr)
a8329558
KW
14084{
14085 attr->name = abbrev->name;
dee91e82 14086 return read_attribute_value (reader, attr, abbrev->form, info_ptr);
a8329558
KW
14087}
14088
0963b4bd 14089/* Read dwarf information from a buffer. */
c906108c
SS
14090
14091static unsigned int
a1855c1d 14092read_1_byte (bfd *abfd, const gdb_byte *buf)
c906108c 14093{
fe1b8b76 14094 return bfd_get_8 (abfd, buf);
c906108c
SS
14095}
14096
14097static int
a1855c1d 14098read_1_signed_byte (bfd *abfd, const gdb_byte *buf)
c906108c 14099{
fe1b8b76 14100 return bfd_get_signed_8 (abfd, buf);
c906108c
SS
14101}
14102
14103static unsigned int
a1855c1d 14104read_2_bytes (bfd *abfd, const gdb_byte *buf)
c906108c 14105{
fe1b8b76 14106 return bfd_get_16 (abfd, buf);
c906108c
SS
14107}
14108
21ae7a4d 14109static int
a1855c1d 14110read_2_signed_bytes (bfd *abfd, const gdb_byte *buf)
21ae7a4d
JK
14111{
14112 return bfd_get_signed_16 (abfd, buf);
14113}
14114
c906108c 14115static unsigned int
a1855c1d 14116read_4_bytes (bfd *abfd, const gdb_byte *buf)
c906108c 14117{
fe1b8b76 14118 return bfd_get_32 (abfd, buf);
c906108c
SS
14119}
14120
21ae7a4d 14121static int
a1855c1d 14122read_4_signed_bytes (bfd *abfd, const gdb_byte *buf)
21ae7a4d
JK
14123{
14124 return bfd_get_signed_32 (abfd, buf);
14125}
14126
93311388 14127static ULONGEST
a1855c1d 14128read_8_bytes (bfd *abfd, const gdb_byte *buf)
c906108c 14129{
fe1b8b76 14130 return bfd_get_64 (abfd, buf);
c906108c
SS
14131}
14132
14133static CORE_ADDR
fe1b8b76 14134read_address (bfd *abfd, gdb_byte *buf, struct dwarf2_cu *cu,
891d2f0b 14135 unsigned int *bytes_read)
c906108c 14136{
e7c27a73 14137 struct comp_unit_head *cu_header = &cu->header;
c906108c
SS
14138 CORE_ADDR retval = 0;
14139
107d2387 14140 if (cu_header->signed_addr_p)
c906108c 14141 {
107d2387
AC
14142 switch (cu_header->addr_size)
14143 {
14144 case 2:
fe1b8b76 14145 retval = bfd_get_signed_16 (abfd, buf);
107d2387
AC
14146 break;
14147 case 4:
fe1b8b76 14148 retval = bfd_get_signed_32 (abfd, buf);
107d2387
AC
14149 break;
14150 case 8:
fe1b8b76 14151 retval = bfd_get_signed_64 (abfd, buf);
107d2387
AC
14152 break;
14153 default:
8e65ff28 14154 internal_error (__FILE__, __LINE__,
e2e0b3e5 14155 _("read_address: bad switch, signed [in module %s]"),
659b0389 14156 bfd_get_filename (abfd));
107d2387
AC
14157 }
14158 }
14159 else
14160 {
14161 switch (cu_header->addr_size)
14162 {
14163 case 2:
fe1b8b76 14164 retval = bfd_get_16 (abfd, buf);
107d2387
AC
14165 break;
14166 case 4:
fe1b8b76 14167 retval = bfd_get_32 (abfd, buf);
107d2387
AC
14168 break;
14169 case 8:
fe1b8b76 14170 retval = bfd_get_64 (abfd, buf);
107d2387
AC
14171 break;
14172 default:
8e65ff28 14173 internal_error (__FILE__, __LINE__,
a73c6dcd
MS
14174 _("read_address: bad switch, "
14175 "unsigned [in module %s]"),
659b0389 14176 bfd_get_filename (abfd));
107d2387 14177 }
c906108c 14178 }
64367e0a 14179
107d2387
AC
14180 *bytes_read = cu_header->addr_size;
14181 return retval;
c906108c
SS
14182}
14183
f7ef9339 14184/* Read the initial length from a section. The (draft) DWARF 3
613e1657
KB
14185 specification allows the initial length to take up either 4 bytes
14186 or 12 bytes. If the first 4 bytes are 0xffffffff, then the next 8
14187 bytes describe the length and all offsets will be 8 bytes in length
14188 instead of 4.
14189
f7ef9339
KB
14190 An older, non-standard 64-bit format is also handled by this
14191 function. The older format in question stores the initial length
14192 as an 8-byte quantity without an escape value. Lengths greater
14193 than 2^32 aren't very common which means that the initial 4 bytes
14194 is almost always zero. Since a length value of zero doesn't make
14195 sense for the 32-bit format, this initial zero can be considered to
14196 be an escape value which indicates the presence of the older 64-bit
14197 format. As written, the code can't detect (old format) lengths
917c78fc
MK
14198 greater than 4GB. If it becomes necessary to handle lengths
14199 somewhat larger than 4GB, we could allow other small values (such
14200 as the non-sensical values of 1, 2, and 3) to also be used as
14201 escape values indicating the presence of the old format.
f7ef9339 14202
917c78fc
MK
14203 The value returned via bytes_read should be used to increment the
14204 relevant pointer after calling read_initial_length().
c764a876 14205
613e1657
KB
14206 [ Note: read_initial_length() and read_offset() are based on the
14207 document entitled "DWARF Debugging Information Format", revision
f7ef9339 14208 3, draft 8, dated November 19, 2001. This document was obtained
613e1657
KB
14209 from:
14210
f7ef9339 14211 http://reality.sgiweb.org/davea/dwarf3-draft8-011125.pdf
6e70227d 14212
613e1657
KB
14213 This document is only a draft and is subject to change. (So beware.)
14214
f7ef9339 14215 Details regarding the older, non-standard 64-bit format were
917c78fc
MK
14216 determined empirically by examining 64-bit ELF files produced by
14217 the SGI toolchain on an IRIX 6.5 machine.
f7ef9339
KB
14218
14219 - Kevin, July 16, 2002
613e1657
KB
14220 ] */
14221
14222static LONGEST
c764a876 14223read_initial_length (bfd *abfd, gdb_byte *buf, unsigned int *bytes_read)
613e1657 14224{
fe1b8b76 14225 LONGEST length = bfd_get_32 (abfd, buf);
613e1657 14226
dd373385 14227 if (length == 0xffffffff)
613e1657 14228 {
fe1b8b76 14229 length = bfd_get_64 (abfd, buf + 4);
613e1657 14230 *bytes_read = 12;
613e1657 14231 }
dd373385 14232 else if (length == 0)
f7ef9339 14233 {
dd373385 14234 /* Handle the (non-standard) 64-bit DWARF2 format used by IRIX. */
fe1b8b76 14235 length = bfd_get_64 (abfd, buf);
f7ef9339 14236 *bytes_read = 8;
f7ef9339 14237 }
613e1657
KB
14238 else
14239 {
14240 *bytes_read = 4;
613e1657
KB
14241 }
14242
c764a876
DE
14243 return length;
14244}
dd373385 14245
c764a876
DE
14246/* Cover function for read_initial_length.
14247 Returns the length of the object at BUF, and stores the size of the
14248 initial length in *BYTES_READ and stores the size that offsets will be in
14249 *OFFSET_SIZE.
14250 If the initial length size is not equivalent to that specified in
14251 CU_HEADER then issue a complaint.
14252 This is useful when reading non-comp-unit headers. */
dd373385 14253
c764a876
DE
14254static LONGEST
14255read_checked_initial_length_and_offset (bfd *abfd, gdb_byte *buf,
14256 const struct comp_unit_head *cu_header,
14257 unsigned int *bytes_read,
14258 unsigned int *offset_size)
14259{
14260 LONGEST length = read_initial_length (abfd, buf, bytes_read);
14261
14262 gdb_assert (cu_header->initial_length_size == 4
14263 || cu_header->initial_length_size == 8
14264 || cu_header->initial_length_size == 12);
14265
14266 if (cu_header->initial_length_size != *bytes_read)
14267 complaint (&symfile_complaints,
14268 _("intermixed 32-bit and 64-bit DWARF sections"));
dd373385 14269
c764a876 14270 *offset_size = (*bytes_read == 4) ? 4 : 8;
dd373385 14271 return length;
613e1657
KB
14272}
14273
14274/* Read an offset from the data stream. The size of the offset is
917c78fc 14275 given by cu_header->offset_size. */
613e1657
KB
14276
14277static LONGEST
fe1b8b76 14278read_offset (bfd *abfd, gdb_byte *buf, const struct comp_unit_head *cu_header,
891d2f0b 14279 unsigned int *bytes_read)
c764a876
DE
14280{
14281 LONGEST offset = read_offset_1 (abfd, buf, cu_header->offset_size);
9a619af0 14282
c764a876
DE
14283 *bytes_read = cu_header->offset_size;
14284 return offset;
14285}
14286
14287/* Read an offset from the data stream. */
14288
14289static LONGEST
14290read_offset_1 (bfd *abfd, gdb_byte *buf, unsigned int offset_size)
613e1657
KB
14291{
14292 LONGEST retval = 0;
14293
c764a876 14294 switch (offset_size)
613e1657
KB
14295 {
14296 case 4:
fe1b8b76 14297 retval = bfd_get_32 (abfd, buf);
613e1657
KB
14298 break;
14299 case 8:
fe1b8b76 14300 retval = bfd_get_64 (abfd, buf);
613e1657
KB
14301 break;
14302 default:
8e65ff28 14303 internal_error (__FILE__, __LINE__,
c764a876 14304 _("read_offset_1: bad switch [in module %s]"),
659b0389 14305 bfd_get_filename (abfd));
613e1657
KB
14306 }
14307
917c78fc 14308 return retval;
613e1657
KB
14309}
14310
fe1b8b76
JB
14311static gdb_byte *
14312read_n_bytes (bfd *abfd, gdb_byte *buf, unsigned int size)
c906108c
SS
14313{
14314 /* If the size of a host char is 8 bits, we can return a pointer
14315 to the buffer, otherwise we have to copy the data to a buffer
14316 allocated on the temporary obstack. */
4bdf3d34 14317 gdb_assert (HOST_CHAR_BIT == 8);
c906108c 14318 return buf;
c906108c
SS
14319}
14320
14321static char *
9b1c24c8 14322read_direct_string (bfd *abfd, gdb_byte *buf, unsigned int *bytes_read_ptr)
c906108c
SS
14323{
14324 /* If the size of a host char is 8 bits, we can return a pointer
14325 to the string, otherwise we have to copy the string to a buffer
14326 allocated on the temporary obstack. */
4bdf3d34 14327 gdb_assert (HOST_CHAR_BIT == 8);
c906108c
SS
14328 if (*buf == '\0')
14329 {
14330 *bytes_read_ptr = 1;
14331 return NULL;
14332 }
fe1b8b76
JB
14333 *bytes_read_ptr = strlen ((char *) buf) + 1;
14334 return (char *) buf;
4bdf3d34
JJ
14335}
14336
14337static char *
cf2c3c16 14338read_indirect_string_at_offset (bfd *abfd, LONGEST str_offset)
4bdf3d34 14339{
be391dca 14340 dwarf2_read_section (dwarf2_per_objfile->objfile, &dwarf2_per_objfile->str);
dce234bc 14341 if (dwarf2_per_objfile->str.buffer == NULL)
cf2c3c16
TT
14342 error (_("DW_FORM_strp used without .debug_str section [in module %s]"),
14343 bfd_get_filename (abfd));
dce234bc 14344 if (str_offset >= dwarf2_per_objfile->str.size)
cf2c3c16
TT
14345 error (_("DW_FORM_strp pointing outside of "
14346 ".debug_str section [in module %s]"),
14347 bfd_get_filename (abfd));
4bdf3d34 14348 gdb_assert (HOST_CHAR_BIT == 8);
dce234bc 14349 if (dwarf2_per_objfile->str.buffer[str_offset] == '\0')
4bdf3d34 14350 return NULL;
dce234bc 14351 return (char *) (dwarf2_per_objfile->str.buffer + str_offset);
c906108c
SS
14352}
14353
36586728
TT
14354/* Read a string at offset STR_OFFSET in the .debug_str section from
14355 the .dwz file DWZ. Throw an error if the offset is too large. If
14356 the string consists of a single NUL byte, return NULL; otherwise
14357 return a pointer to the string. */
14358
14359static char *
14360read_indirect_string_from_dwz (struct dwz_file *dwz, LONGEST str_offset)
14361{
14362 dwarf2_read_section (dwarf2_per_objfile->objfile, &dwz->str);
14363
14364 if (dwz->str.buffer == NULL)
14365 error (_("DW_FORM_GNU_strp_alt used without .debug_str "
14366 "section [in module %s]"),
14367 bfd_get_filename (dwz->dwz_bfd));
14368 if (str_offset >= dwz->str.size)
14369 error (_("DW_FORM_GNU_strp_alt pointing outside of "
14370 ".debug_str section [in module %s]"),
14371 bfd_get_filename (dwz->dwz_bfd));
14372 gdb_assert (HOST_CHAR_BIT == 8);
14373 if (dwz->str.buffer[str_offset] == '\0')
14374 return NULL;
14375 return (char *) (dwz->str.buffer + str_offset);
14376}
14377
cf2c3c16
TT
14378static char *
14379read_indirect_string (bfd *abfd, gdb_byte *buf,
14380 const struct comp_unit_head *cu_header,
14381 unsigned int *bytes_read_ptr)
14382{
14383 LONGEST str_offset = read_offset (abfd, buf, cu_header, bytes_read_ptr);
14384
14385 return read_indirect_string_at_offset (abfd, str_offset);
14386}
14387
12df843f 14388static ULONGEST
fe1b8b76 14389read_unsigned_leb128 (bfd *abfd, gdb_byte *buf, unsigned int *bytes_read_ptr)
c906108c 14390{
12df843f 14391 ULONGEST result;
ce5d95e1 14392 unsigned int num_read;
c906108c
SS
14393 int i, shift;
14394 unsigned char byte;
14395
14396 result = 0;
14397 shift = 0;
14398 num_read = 0;
14399 i = 0;
14400 while (1)
14401 {
fe1b8b76 14402 byte = bfd_get_8 (abfd, buf);
c906108c
SS
14403 buf++;
14404 num_read++;
12df843f 14405 result |= ((ULONGEST) (byte & 127) << shift);
c906108c
SS
14406 if ((byte & 128) == 0)
14407 {
14408 break;
14409 }
14410 shift += 7;
14411 }
14412 *bytes_read_ptr = num_read;
14413 return result;
14414}
14415
12df843f 14416static LONGEST
fe1b8b76 14417read_signed_leb128 (bfd *abfd, gdb_byte *buf, unsigned int *bytes_read_ptr)
c906108c 14418{
12df843f 14419 LONGEST result;
77e0b926 14420 int i, shift, num_read;
c906108c
SS
14421 unsigned char byte;
14422
14423 result = 0;
14424 shift = 0;
c906108c
SS
14425 num_read = 0;
14426 i = 0;
14427 while (1)
14428 {
fe1b8b76 14429 byte = bfd_get_8 (abfd, buf);
c906108c
SS
14430 buf++;
14431 num_read++;
12df843f 14432 result |= ((LONGEST) (byte & 127) << shift);
c906108c
SS
14433 shift += 7;
14434 if ((byte & 128) == 0)
14435 {
14436 break;
14437 }
14438 }
77e0b926 14439 if ((shift < 8 * sizeof (result)) && (byte & 0x40))
12df843f 14440 result |= -(((LONGEST) 1) << shift);
c906108c
SS
14441 *bytes_read_ptr = num_read;
14442 return result;
14443}
14444
3019eac3
DE
14445/* Given index ADDR_INDEX in .debug_addr, fetch the value.
14446 ADDR_BASE is the DW_AT_GNU_addr_base attribute or zero.
14447 ADDR_SIZE is the size of addresses from the CU header. */
14448
14449static CORE_ADDR
14450read_addr_index_1 (unsigned int addr_index, ULONGEST addr_base, int addr_size)
14451{
14452 struct objfile *objfile = dwarf2_per_objfile->objfile;
14453 bfd *abfd = objfile->obfd;
14454 const gdb_byte *info_ptr;
14455
14456 dwarf2_read_section (objfile, &dwarf2_per_objfile->addr);
14457 if (dwarf2_per_objfile->addr.buffer == NULL)
14458 error (_("DW_FORM_addr_index used without .debug_addr section [in module %s]"),
14459 objfile->name);
14460 if (addr_base + addr_index * addr_size >= dwarf2_per_objfile->addr.size)
14461 error (_("DW_FORM_addr_index pointing outside of "
14462 ".debug_addr section [in module %s]"),
14463 objfile->name);
14464 info_ptr = (dwarf2_per_objfile->addr.buffer
14465 + addr_base + addr_index * addr_size);
14466 if (addr_size == 4)
14467 return bfd_get_32 (abfd, info_ptr);
14468 else
14469 return bfd_get_64 (abfd, info_ptr);
14470}
14471
14472/* Given index ADDR_INDEX in .debug_addr, fetch the value. */
14473
14474static CORE_ADDR
14475read_addr_index (struct dwarf2_cu *cu, unsigned int addr_index)
14476{
14477 return read_addr_index_1 (addr_index, cu->addr_base, cu->header.addr_size);
14478}
14479
14480/* Given a pointer to an leb128 value, fetch the value from .debug_addr. */
14481
14482static CORE_ADDR
14483read_addr_index_from_leb128 (struct dwarf2_cu *cu, gdb_byte *info_ptr,
14484 unsigned int *bytes_read)
14485{
14486 bfd *abfd = cu->objfile->obfd;
14487 unsigned int addr_index = read_unsigned_leb128 (abfd, info_ptr, bytes_read);
14488
14489 return read_addr_index (cu, addr_index);
14490}
14491
14492/* Data structure to pass results from dwarf2_read_addr_index_reader
14493 back to dwarf2_read_addr_index. */
14494
14495struct dwarf2_read_addr_index_data
14496{
14497 ULONGEST addr_base;
14498 int addr_size;
14499};
14500
14501/* die_reader_func for dwarf2_read_addr_index. */
14502
14503static void
14504dwarf2_read_addr_index_reader (const struct die_reader_specs *reader,
14505 gdb_byte *info_ptr,
14506 struct die_info *comp_unit_die,
14507 int has_children,
14508 void *data)
14509{
14510 struct dwarf2_cu *cu = reader->cu;
14511 struct dwarf2_read_addr_index_data *aidata =
14512 (struct dwarf2_read_addr_index_data *) data;
14513
14514 aidata->addr_base = cu->addr_base;
14515 aidata->addr_size = cu->header.addr_size;
14516}
14517
14518/* Given an index in .debug_addr, fetch the value.
14519 NOTE: This can be called during dwarf expression evaluation,
14520 long after the debug information has been read, and thus per_cu->cu
14521 may no longer exist. */
14522
14523CORE_ADDR
14524dwarf2_read_addr_index (struct dwarf2_per_cu_data *per_cu,
14525 unsigned int addr_index)
14526{
14527 struct objfile *objfile = per_cu->objfile;
14528 struct dwarf2_cu *cu = per_cu->cu;
14529 ULONGEST addr_base;
14530 int addr_size;
14531
14532 /* This is intended to be called from outside this file. */
14533 dw2_setup (objfile);
14534
14535 /* We need addr_base and addr_size.
14536 If we don't have PER_CU->cu, we have to get it.
14537 Nasty, but the alternative is storing the needed info in PER_CU,
14538 which at this point doesn't seem justified: it's not clear how frequently
14539 it would get used and it would increase the size of every PER_CU.
14540 Entry points like dwarf2_per_cu_addr_size do a similar thing
14541 so we're not in uncharted territory here.
14542 Alas we need to be a bit more complicated as addr_base is contained
14543 in the DIE.
14544
14545 We don't need to read the entire CU(/TU).
14546 We just need the header and top level die.
a1b64ce1 14547
3019eac3 14548 IWBN to use the aging mechanism to let us lazily later discard the CU.
a1b64ce1 14549 For now we skip this optimization. */
3019eac3
DE
14550
14551 if (cu != NULL)
14552 {
14553 addr_base = cu->addr_base;
14554 addr_size = cu->header.addr_size;
14555 }
14556 else
14557 {
14558 struct dwarf2_read_addr_index_data aidata;
14559
a1b64ce1
DE
14560 /* Note: We can't use init_cutu_and_read_dies_simple here,
14561 we need addr_base. */
14562 init_cutu_and_read_dies (per_cu, NULL, 0, 0,
14563 dwarf2_read_addr_index_reader, &aidata);
3019eac3
DE
14564 addr_base = aidata.addr_base;
14565 addr_size = aidata.addr_size;
14566 }
14567
14568 return read_addr_index_1 (addr_index, addr_base, addr_size);
14569}
14570
14571/* Given a DW_AT_str_index, fetch the string. */
14572
14573static char *
14574read_str_index (const struct die_reader_specs *reader,
14575 struct dwarf2_cu *cu, ULONGEST str_index)
14576{
14577 struct objfile *objfile = dwarf2_per_objfile->objfile;
14578 const char *dwo_name = objfile->name;
14579 bfd *abfd = objfile->obfd;
14580 struct dwo_sections *sections = &reader->dwo_file->sections;
14581 gdb_byte *info_ptr;
14582 ULONGEST str_offset;
14583
14584 dwarf2_read_section (objfile, &sections->str);
14585 dwarf2_read_section (objfile, &sections->str_offsets);
14586 if (sections->str.buffer == NULL)
14587 error (_("DW_FORM_str_index used without .debug_str.dwo section"
14588 " in CU at offset 0x%lx [in module %s]"),
14589 (long) cu->header.offset.sect_off, dwo_name);
14590 if (sections->str_offsets.buffer == NULL)
14591 error (_("DW_FORM_str_index used without .debug_str_offsets.dwo section"
14592 " in CU at offset 0x%lx [in module %s]"),
14593 (long) cu->header.offset.sect_off, dwo_name);
14594 if (str_index * cu->header.offset_size >= sections->str_offsets.size)
14595 error (_("DW_FORM_str_index pointing outside of .debug_str_offsets.dwo"
14596 " section in CU at offset 0x%lx [in module %s]"),
14597 (long) cu->header.offset.sect_off, dwo_name);
14598 info_ptr = (sections->str_offsets.buffer
14599 + str_index * cu->header.offset_size);
14600 if (cu->header.offset_size == 4)
14601 str_offset = bfd_get_32 (abfd, info_ptr);
14602 else
14603 str_offset = bfd_get_64 (abfd, info_ptr);
14604 if (str_offset >= sections->str.size)
14605 error (_("Offset from DW_FORM_str_index pointing outside of"
14606 " .debug_str.dwo section in CU at offset 0x%lx [in module %s]"),
14607 (long) cu->header.offset.sect_off, dwo_name);
14608 return (char *) (sections->str.buffer + str_offset);
14609}
14610
3019eac3
DE
14611/* Return the length of an LEB128 number in BUF. */
14612
14613static int
14614leb128_size (const gdb_byte *buf)
14615{
14616 const gdb_byte *begin = buf;
14617 gdb_byte byte;
14618
14619 while (1)
14620 {
14621 byte = *buf++;
14622 if ((byte & 128) == 0)
14623 return buf - begin;
14624 }
14625}
14626
c906108c 14627static void
e142c38c 14628set_cu_language (unsigned int lang, struct dwarf2_cu *cu)
c906108c
SS
14629{
14630 switch (lang)
14631 {
14632 case DW_LANG_C89:
76bee0cc 14633 case DW_LANG_C99:
c906108c 14634 case DW_LANG_C:
e142c38c 14635 cu->language = language_c;
c906108c
SS
14636 break;
14637 case DW_LANG_C_plus_plus:
e142c38c 14638 cu->language = language_cplus;
c906108c 14639 break;
6aecb9c2
JB
14640 case DW_LANG_D:
14641 cu->language = language_d;
14642 break;
c906108c
SS
14643 case DW_LANG_Fortran77:
14644 case DW_LANG_Fortran90:
b21b22e0 14645 case DW_LANG_Fortran95:
e142c38c 14646 cu->language = language_fortran;
c906108c 14647 break;
a766d390
DE
14648 case DW_LANG_Go:
14649 cu->language = language_go;
14650 break;
c906108c 14651 case DW_LANG_Mips_Assembler:
e142c38c 14652 cu->language = language_asm;
c906108c 14653 break;
bebd888e 14654 case DW_LANG_Java:
e142c38c 14655 cu->language = language_java;
bebd888e 14656 break;
c906108c 14657 case DW_LANG_Ada83:
8aaf0b47 14658 case DW_LANG_Ada95:
bc5f45f8
JB
14659 cu->language = language_ada;
14660 break;
72019c9c
GM
14661 case DW_LANG_Modula2:
14662 cu->language = language_m2;
14663 break;
fe8e67fd
PM
14664 case DW_LANG_Pascal83:
14665 cu->language = language_pascal;
14666 break;
22566fbd
DJ
14667 case DW_LANG_ObjC:
14668 cu->language = language_objc;
14669 break;
c906108c
SS
14670 case DW_LANG_Cobol74:
14671 case DW_LANG_Cobol85:
c906108c 14672 default:
e142c38c 14673 cu->language = language_minimal;
c906108c
SS
14674 break;
14675 }
e142c38c 14676 cu->language_defn = language_def (cu->language);
c906108c
SS
14677}
14678
14679/* Return the named attribute or NULL if not there. */
14680
14681static struct attribute *
e142c38c 14682dwarf2_attr (struct die_info *die, unsigned int name, struct dwarf2_cu *cu)
c906108c 14683{
a48e046c 14684 for (;;)
c906108c 14685 {
a48e046c
TT
14686 unsigned int i;
14687 struct attribute *spec = NULL;
14688
14689 for (i = 0; i < die->num_attrs; ++i)
14690 {
14691 if (die->attrs[i].name == name)
14692 return &die->attrs[i];
14693 if (die->attrs[i].name == DW_AT_specification
14694 || die->attrs[i].name == DW_AT_abstract_origin)
14695 spec = &die->attrs[i];
14696 }
14697
14698 if (!spec)
14699 break;
c906108c 14700
f2f0e013 14701 die = follow_die_ref (die, spec, &cu);
f2f0e013 14702 }
c5aa993b 14703
c906108c
SS
14704 return NULL;
14705}
14706
348e048f
DE
14707/* Return the named attribute or NULL if not there,
14708 but do not follow DW_AT_specification, etc.
14709 This is for use in contexts where we're reading .debug_types dies.
14710 Following DW_AT_specification, DW_AT_abstract_origin will take us
14711 back up the chain, and we want to go down. */
14712
14713static struct attribute *
45e58e77 14714dwarf2_attr_no_follow (struct die_info *die, unsigned int name)
348e048f
DE
14715{
14716 unsigned int i;
14717
14718 for (i = 0; i < die->num_attrs; ++i)
14719 if (die->attrs[i].name == name)
14720 return &die->attrs[i];
14721
14722 return NULL;
14723}
14724
05cf31d1
JB
14725/* Return non-zero iff the attribute NAME is defined for the given DIE,
14726 and holds a non-zero value. This function should only be used for
2dc7f7b3 14727 DW_FORM_flag or DW_FORM_flag_present attributes. */
05cf31d1
JB
14728
14729static int
14730dwarf2_flag_true_p (struct die_info *die, unsigned name, struct dwarf2_cu *cu)
14731{
14732 struct attribute *attr = dwarf2_attr (die, name, cu);
14733
14734 return (attr && DW_UNSND (attr));
14735}
14736
3ca72b44 14737static int
e142c38c 14738die_is_declaration (struct die_info *die, struct dwarf2_cu *cu)
3ca72b44 14739{
05cf31d1
JB
14740 /* A DIE is a declaration if it has a DW_AT_declaration attribute
14741 which value is non-zero. However, we have to be careful with
14742 DIEs having a DW_AT_specification attribute, because dwarf2_attr()
14743 (via dwarf2_flag_true_p) follows this attribute. So we may
14744 end up accidently finding a declaration attribute that belongs
14745 to a different DIE referenced by the specification attribute,
14746 even though the given DIE does not have a declaration attribute. */
14747 return (dwarf2_flag_true_p (die, DW_AT_declaration, cu)
14748 && dwarf2_attr (die, DW_AT_specification, cu) == NULL);
3ca72b44
AC
14749}
14750
63d06c5c 14751/* Return the die giving the specification for DIE, if there is
f2f0e013 14752 one. *SPEC_CU is the CU containing DIE on input, and the CU
edb3359d
DJ
14753 containing the return value on output. If there is no
14754 specification, but there is an abstract origin, that is
14755 returned. */
63d06c5c
DC
14756
14757static struct die_info *
f2f0e013 14758die_specification (struct die_info *die, struct dwarf2_cu **spec_cu)
63d06c5c 14759{
f2f0e013
DJ
14760 struct attribute *spec_attr = dwarf2_attr (die, DW_AT_specification,
14761 *spec_cu);
63d06c5c 14762
edb3359d
DJ
14763 if (spec_attr == NULL)
14764 spec_attr = dwarf2_attr (die, DW_AT_abstract_origin, *spec_cu);
14765
63d06c5c
DC
14766 if (spec_attr == NULL)
14767 return NULL;
14768 else
f2f0e013 14769 return follow_die_ref (die, spec_attr, spec_cu);
63d06c5c 14770}
c906108c 14771
debd256d 14772/* Free the line_header structure *LH, and any arrays and strings it
ae2de4f8
DE
14773 refers to.
14774 NOTE: This is also used as a "cleanup" function. */
14775
debd256d
JB
14776static void
14777free_line_header (struct line_header *lh)
14778{
14779 if (lh->standard_opcode_lengths)
a8bc7b56 14780 xfree (lh->standard_opcode_lengths);
debd256d
JB
14781
14782 /* Remember that all the lh->file_names[i].name pointers are
14783 pointers into debug_line_buffer, and don't need to be freed. */
14784 if (lh->file_names)
a8bc7b56 14785 xfree (lh->file_names);
debd256d
JB
14786
14787 /* Similarly for the include directory names. */
14788 if (lh->include_dirs)
a8bc7b56 14789 xfree (lh->include_dirs);
debd256d 14790
a8bc7b56 14791 xfree (lh);
debd256d
JB
14792}
14793
debd256d 14794/* Add an entry to LH's include directory table. */
ae2de4f8 14795
debd256d
JB
14796static void
14797add_include_dir (struct line_header *lh, char *include_dir)
c906108c 14798{
debd256d
JB
14799 /* Grow the array if necessary. */
14800 if (lh->include_dirs_size == 0)
c5aa993b 14801 {
debd256d
JB
14802 lh->include_dirs_size = 1; /* for testing */
14803 lh->include_dirs = xmalloc (lh->include_dirs_size
14804 * sizeof (*lh->include_dirs));
14805 }
14806 else if (lh->num_include_dirs >= lh->include_dirs_size)
14807 {
14808 lh->include_dirs_size *= 2;
14809 lh->include_dirs = xrealloc (lh->include_dirs,
14810 (lh->include_dirs_size
14811 * sizeof (*lh->include_dirs)));
c5aa993b 14812 }
c906108c 14813
debd256d
JB
14814 lh->include_dirs[lh->num_include_dirs++] = include_dir;
14815}
6e70227d 14816
debd256d 14817/* Add an entry to LH's file name table. */
ae2de4f8 14818
debd256d
JB
14819static void
14820add_file_name (struct line_header *lh,
14821 char *name,
14822 unsigned int dir_index,
14823 unsigned int mod_time,
14824 unsigned int length)
14825{
14826 struct file_entry *fe;
14827
14828 /* Grow the array if necessary. */
14829 if (lh->file_names_size == 0)
14830 {
14831 lh->file_names_size = 1; /* for testing */
14832 lh->file_names = xmalloc (lh->file_names_size
14833 * sizeof (*lh->file_names));
14834 }
14835 else if (lh->num_file_names >= lh->file_names_size)
14836 {
14837 lh->file_names_size *= 2;
14838 lh->file_names = xrealloc (lh->file_names,
14839 (lh->file_names_size
14840 * sizeof (*lh->file_names)));
14841 }
14842
14843 fe = &lh->file_names[lh->num_file_names++];
14844 fe->name = name;
14845 fe->dir_index = dir_index;
14846 fe->mod_time = mod_time;
14847 fe->length = length;
aaa75496 14848 fe->included_p = 0;
cb1df416 14849 fe->symtab = NULL;
debd256d 14850}
6e70227d 14851
36586728
TT
14852/* A convenience function to find the proper .debug_line section for a
14853 CU. */
14854
14855static struct dwarf2_section_info *
14856get_debug_line_section (struct dwarf2_cu *cu)
14857{
14858 struct dwarf2_section_info *section;
14859
14860 /* For TUs in DWO files, the DW_AT_stmt_list attribute lives in the
14861 DWO file. */
14862 if (cu->dwo_unit && cu->per_cu->is_debug_types)
14863 section = &cu->dwo_unit->dwo_file->sections.line;
14864 else if (cu->per_cu->is_dwz)
14865 {
14866 struct dwz_file *dwz = dwarf2_get_dwz_file ();
14867
14868 section = &dwz->line;
14869 }
14870 else
14871 section = &dwarf2_per_objfile->line;
14872
14873 return section;
14874}
14875
debd256d 14876/* Read the statement program header starting at OFFSET in
3019eac3 14877 .debug_line, or .debug_line.dwo. Return a pointer
6502dd73 14878 to a struct line_header, allocated using xmalloc.
debd256d
JB
14879
14880 NOTE: the strings in the include directory and file name tables of
3019eac3
DE
14881 the returned object point into the dwarf line section buffer,
14882 and must not be freed. */
ae2de4f8 14883
debd256d 14884static struct line_header *
3019eac3 14885dwarf_decode_line_header (unsigned int offset, struct dwarf2_cu *cu)
debd256d
JB
14886{
14887 struct cleanup *back_to;
14888 struct line_header *lh;
fe1b8b76 14889 gdb_byte *line_ptr;
c764a876 14890 unsigned int bytes_read, offset_size;
debd256d
JB
14891 int i;
14892 char *cur_dir, *cur_file;
3019eac3
DE
14893 struct dwarf2_section_info *section;
14894 bfd *abfd;
14895
36586728 14896 section = get_debug_line_section (cu);
3019eac3
DE
14897 dwarf2_read_section (dwarf2_per_objfile->objfile, section);
14898 if (section->buffer == NULL)
debd256d 14899 {
3019eac3
DE
14900 if (cu->dwo_unit && cu->per_cu->is_debug_types)
14901 complaint (&symfile_complaints, _("missing .debug_line.dwo section"));
14902 else
14903 complaint (&symfile_complaints, _("missing .debug_line section"));
debd256d
JB
14904 return 0;
14905 }
14906
fceca515
DE
14907 /* We can't do this until we know the section is non-empty.
14908 Only then do we know we have such a section. */
14909 abfd = section->asection->owner;
14910
a738430d
MK
14911 /* Make sure that at least there's room for the total_length field.
14912 That could be 12 bytes long, but we're just going to fudge that. */
3019eac3 14913 if (offset + 4 >= section->size)
debd256d 14914 {
4d3c2250 14915 dwarf2_statement_list_fits_in_line_number_section_complaint ();
debd256d
JB
14916 return 0;
14917 }
14918
14919 lh = xmalloc (sizeof (*lh));
14920 memset (lh, 0, sizeof (*lh));
14921 back_to = make_cleanup ((make_cleanup_ftype *) free_line_header,
14922 (void *) lh);
14923
3019eac3 14924 line_ptr = section->buffer + offset;
debd256d 14925
a738430d 14926 /* Read in the header. */
6e70227d 14927 lh->total_length =
c764a876
DE
14928 read_checked_initial_length_and_offset (abfd, line_ptr, &cu->header,
14929 &bytes_read, &offset_size);
debd256d 14930 line_ptr += bytes_read;
3019eac3 14931 if (line_ptr + lh->total_length > (section->buffer + section->size))
debd256d 14932 {
4d3c2250 14933 dwarf2_statement_list_fits_in_line_number_section_complaint ();
debd256d
JB
14934 return 0;
14935 }
14936 lh->statement_program_end = line_ptr + lh->total_length;
14937 lh->version = read_2_bytes (abfd, line_ptr);
14938 line_ptr += 2;
c764a876
DE
14939 lh->header_length = read_offset_1 (abfd, line_ptr, offset_size);
14940 line_ptr += offset_size;
debd256d
JB
14941 lh->minimum_instruction_length = read_1_byte (abfd, line_ptr);
14942 line_ptr += 1;
2dc7f7b3
TT
14943 if (lh->version >= 4)
14944 {
14945 lh->maximum_ops_per_instruction = read_1_byte (abfd, line_ptr);
14946 line_ptr += 1;
14947 }
14948 else
14949 lh->maximum_ops_per_instruction = 1;
14950
14951 if (lh->maximum_ops_per_instruction == 0)
14952 {
14953 lh->maximum_ops_per_instruction = 1;
14954 complaint (&symfile_complaints,
3e43a32a
MS
14955 _("invalid maximum_ops_per_instruction "
14956 "in `.debug_line' section"));
2dc7f7b3
TT
14957 }
14958
debd256d
JB
14959 lh->default_is_stmt = read_1_byte (abfd, line_ptr);
14960 line_ptr += 1;
14961 lh->line_base = read_1_signed_byte (abfd, line_ptr);
14962 line_ptr += 1;
14963 lh->line_range = read_1_byte (abfd, line_ptr);
14964 line_ptr += 1;
14965 lh->opcode_base = read_1_byte (abfd, line_ptr);
14966 line_ptr += 1;
14967 lh->standard_opcode_lengths
fe1b8b76 14968 = xmalloc (lh->opcode_base * sizeof (lh->standard_opcode_lengths[0]));
debd256d
JB
14969
14970 lh->standard_opcode_lengths[0] = 1; /* This should never be used anyway. */
14971 for (i = 1; i < lh->opcode_base; ++i)
14972 {
14973 lh->standard_opcode_lengths[i] = read_1_byte (abfd, line_ptr);
14974 line_ptr += 1;
14975 }
14976
a738430d 14977 /* Read directory table. */
9b1c24c8 14978 while ((cur_dir = read_direct_string (abfd, line_ptr, &bytes_read)) != NULL)
debd256d
JB
14979 {
14980 line_ptr += bytes_read;
14981 add_include_dir (lh, cur_dir);
14982 }
14983 line_ptr += bytes_read;
14984
a738430d 14985 /* Read file name table. */
9b1c24c8 14986 while ((cur_file = read_direct_string (abfd, line_ptr, &bytes_read)) != NULL)
debd256d
JB
14987 {
14988 unsigned int dir_index, mod_time, length;
14989
14990 line_ptr += bytes_read;
14991 dir_index = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
14992 line_ptr += bytes_read;
14993 mod_time = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
14994 line_ptr += bytes_read;
14995 length = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
14996 line_ptr += bytes_read;
14997
14998 add_file_name (lh, cur_file, dir_index, mod_time, length);
14999 }
15000 line_ptr += bytes_read;
6e70227d 15001 lh->statement_program_start = line_ptr;
debd256d 15002
3019eac3 15003 if (line_ptr > (section->buffer + section->size))
4d3c2250 15004 complaint (&symfile_complaints,
3e43a32a
MS
15005 _("line number info header doesn't "
15006 "fit in `.debug_line' section"));
debd256d
JB
15007
15008 discard_cleanups (back_to);
15009 return lh;
15010}
c906108c 15011
c6da4cef
DE
15012/* Subroutine of dwarf_decode_lines to simplify it.
15013 Return the file name of the psymtab for included file FILE_INDEX
15014 in line header LH of PST.
15015 COMP_DIR is the compilation directory (DW_AT_comp_dir) or NULL if unknown.
15016 If space for the result is malloc'd, it will be freed by a cleanup.
15017 Returns NULL if FILE_INDEX should be ignored, i.e., it is pst->filename. */
15018
15019static char *
15020psymtab_include_file_name (const struct line_header *lh, int file_index,
15021 const struct partial_symtab *pst,
15022 const char *comp_dir)
15023{
15024 const struct file_entry fe = lh->file_names [file_index];
15025 char *include_name = fe.name;
15026 char *include_name_to_compare = include_name;
15027 char *dir_name = NULL;
72b9f47f
TT
15028 const char *pst_filename;
15029 char *copied_name = NULL;
c6da4cef
DE
15030 int file_is_pst;
15031
15032 if (fe.dir_index)
15033 dir_name = lh->include_dirs[fe.dir_index - 1];
15034
15035 if (!IS_ABSOLUTE_PATH (include_name)
15036 && (dir_name != NULL || comp_dir != NULL))
15037 {
15038 /* Avoid creating a duplicate psymtab for PST.
15039 We do this by comparing INCLUDE_NAME and PST_FILENAME.
15040 Before we do the comparison, however, we need to account
15041 for DIR_NAME and COMP_DIR.
15042 First prepend dir_name (if non-NULL). If we still don't
15043 have an absolute path prepend comp_dir (if non-NULL).
15044 However, the directory we record in the include-file's
15045 psymtab does not contain COMP_DIR (to match the
15046 corresponding symtab(s)).
15047
15048 Example:
15049
15050 bash$ cd /tmp
15051 bash$ gcc -g ./hello.c
15052 include_name = "hello.c"
15053 dir_name = "."
15054 DW_AT_comp_dir = comp_dir = "/tmp"
15055 DW_AT_name = "./hello.c" */
15056
15057 if (dir_name != NULL)
15058 {
15059 include_name = concat (dir_name, SLASH_STRING,
15060 include_name, (char *)NULL);
15061 include_name_to_compare = include_name;
15062 make_cleanup (xfree, include_name);
15063 }
15064 if (!IS_ABSOLUTE_PATH (include_name) && comp_dir != NULL)
15065 {
15066 include_name_to_compare = concat (comp_dir, SLASH_STRING,
15067 include_name, (char *)NULL);
15068 }
15069 }
15070
15071 pst_filename = pst->filename;
15072 if (!IS_ABSOLUTE_PATH (pst_filename) && pst->dirname != NULL)
15073 {
72b9f47f
TT
15074 copied_name = concat (pst->dirname, SLASH_STRING,
15075 pst_filename, (char *)NULL);
15076 pst_filename = copied_name;
c6da4cef
DE
15077 }
15078
1e3fad37 15079 file_is_pst = FILENAME_CMP (include_name_to_compare, pst_filename) == 0;
c6da4cef
DE
15080
15081 if (include_name_to_compare != include_name)
15082 xfree (include_name_to_compare);
72b9f47f
TT
15083 if (copied_name != NULL)
15084 xfree (copied_name);
c6da4cef
DE
15085
15086 if (file_is_pst)
15087 return NULL;
15088 return include_name;
15089}
15090
c91513d8
PP
15091/* Ignore this record_line request. */
15092
15093static void
15094noop_record_line (struct subfile *subfile, int line, CORE_ADDR pc)
15095{
15096 return;
15097}
15098
f3f5162e
DE
15099/* Subroutine of dwarf_decode_lines to simplify it.
15100 Process the line number information in LH. */
debd256d 15101
c906108c 15102static void
f3f5162e
DE
15103dwarf_decode_lines_1 (struct line_header *lh, const char *comp_dir,
15104 struct dwarf2_cu *cu, struct partial_symtab *pst)
c906108c 15105{
a8c50c1f 15106 gdb_byte *line_ptr, *extended_end;
fe1b8b76 15107 gdb_byte *line_end;
a8c50c1f 15108 unsigned int bytes_read, extended_len;
c906108c 15109 unsigned char op_code, extended_op, adj_opcode;
e142c38c
DJ
15110 CORE_ADDR baseaddr;
15111 struct objfile *objfile = cu->objfile;
f3f5162e 15112 bfd *abfd = objfile->obfd;
fbf65064 15113 struct gdbarch *gdbarch = get_objfile_arch (objfile);
aaa75496 15114 const int decode_for_pst_p = (pst != NULL);
f3f5162e 15115 struct subfile *last_subfile = NULL;
c91513d8
PP
15116 void (*p_record_line) (struct subfile *subfile, int line, CORE_ADDR pc)
15117 = record_line;
e142c38c
DJ
15118
15119 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
c906108c 15120
debd256d
JB
15121 line_ptr = lh->statement_program_start;
15122 line_end = lh->statement_program_end;
c906108c
SS
15123
15124 /* Read the statement sequences until there's nothing left. */
15125 while (line_ptr < line_end)
15126 {
15127 /* state machine registers */
15128 CORE_ADDR address = 0;
15129 unsigned int file = 1;
15130 unsigned int line = 1;
15131 unsigned int column = 0;
debd256d 15132 int is_stmt = lh->default_is_stmt;
c906108c
SS
15133 int basic_block = 0;
15134 int end_sequence = 0;
fbf65064 15135 CORE_ADDR addr;
2dc7f7b3 15136 unsigned char op_index = 0;
c906108c 15137
aaa75496 15138 if (!decode_for_pst_p && lh->num_file_names >= file)
c906108c 15139 {
aaa75496 15140 /* Start a subfile for the current file of the state machine. */
debd256d
JB
15141 /* lh->include_dirs and lh->file_names are 0-based, but the
15142 directory and file name numbers in the statement program
15143 are 1-based. */
15144 struct file_entry *fe = &lh->file_names[file - 1];
4f1520fb 15145 char *dir = NULL;
a738430d 15146
debd256d
JB
15147 if (fe->dir_index)
15148 dir = lh->include_dirs[fe->dir_index - 1];
4f1520fb
FR
15149
15150 dwarf2_start_subfile (fe->name, dir, comp_dir);
c906108c
SS
15151 }
15152
a738430d 15153 /* Decode the table. */
c5aa993b 15154 while (!end_sequence)
c906108c
SS
15155 {
15156 op_code = read_1_byte (abfd, line_ptr);
15157 line_ptr += 1;
59205f5a
JB
15158 if (line_ptr > line_end)
15159 {
15160 dwarf2_debug_line_missing_end_sequence_complaint ();
15161 break;
15162 }
9aa1fe7e 15163
debd256d 15164 if (op_code >= lh->opcode_base)
6e70227d 15165 {
a738430d 15166 /* Special operand. */
debd256d 15167 adj_opcode = op_code - lh->opcode_base;
2dc7f7b3
TT
15168 address += (((op_index + (adj_opcode / lh->line_range))
15169 / lh->maximum_ops_per_instruction)
15170 * lh->minimum_instruction_length);
15171 op_index = ((op_index + (adj_opcode / lh->line_range))
15172 % lh->maximum_ops_per_instruction);
debd256d 15173 line += lh->line_base + (adj_opcode % lh->line_range);
59205f5a 15174 if (lh->num_file_names < file || file == 0)
25e43795 15175 dwarf2_debug_line_missing_file_complaint ();
2dc7f7b3
TT
15176 /* For now we ignore lines not starting on an
15177 instruction boundary. */
15178 else if (op_index == 0)
25e43795
DJ
15179 {
15180 lh->file_names[file - 1].included_p = 1;
ca5f395d 15181 if (!decode_for_pst_p && is_stmt)
fbf65064
UW
15182 {
15183 if (last_subfile != current_subfile)
15184 {
15185 addr = gdbarch_addr_bits_remove (gdbarch, address);
15186 if (last_subfile)
c91513d8 15187 (*p_record_line) (last_subfile, 0, addr);
fbf65064
UW
15188 last_subfile = current_subfile;
15189 }
25e43795 15190 /* Append row to matrix using current values. */
7019d805 15191 addr = gdbarch_addr_bits_remove (gdbarch, address);
c91513d8 15192 (*p_record_line) (current_subfile, line, addr);
366da635 15193 }
25e43795 15194 }
ca5f395d 15195 basic_block = 0;
9aa1fe7e
GK
15196 }
15197 else switch (op_code)
c906108c
SS
15198 {
15199 case DW_LNS_extended_op:
3e43a32a
MS
15200 extended_len = read_unsigned_leb128 (abfd, line_ptr,
15201 &bytes_read);
473b7be6 15202 line_ptr += bytes_read;
a8c50c1f 15203 extended_end = line_ptr + extended_len;
c906108c
SS
15204 extended_op = read_1_byte (abfd, line_ptr);
15205 line_ptr += 1;
15206 switch (extended_op)
15207 {
15208 case DW_LNE_end_sequence:
c91513d8 15209 p_record_line = record_line;
c906108c 15210 end_sequence = 1;
c906108c
SS
15211 break;
15212 case DW_LNE_set_address:
e7c27a73 15213 address = read_address (abfd, line_ptr, cu, &bytes_read);
c91513d8
PP
15214
15215 if (address == 0 && !dwarf2_per_objfile->has_section_at_zero)
15216 {
15217 /* This line table is for a function which has been
15218 GCd by the linker. Ignore it. PR gdb/12528 */
15219
15220 long line_offset
36586728 15221 = line_ptr - get_debug_line_section (cu)->buffer;
c91513d8
PP
15222
15223 complaint (&symfile_complaints,
15224 _(".debug_line address at offset 0x%lx is 0 "
15225 "[in module %s]"),
bb5ed363 15226 line_offset, objfile->name);
c91513d8
PP
15227 p_record_line = noop_record_line;
15228 }
15229
2dc7f7b3 15230 op_index = 0;
107d2387
AC
15231 line_ptr += bytes_read;
15232 address += baseaddr;
c906108c
SS
15233 break;
15234 case DW_LNE_define_file:
debd256d
JB
15235 {
15236 char *cur_file;
15237 unsigned int dir_index, mod_time, length;
6e70227d 15238
3e43a32a
MS
15239 cur_file = read_direct_string (abfd, line_ptr,
15240 &bytes_read);
debd256d
JB
15241 line_ptr += bytes_read;
15242 dir_index =
15243 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
15244 line_ptr += bytes_read;
15245 mod_time =
15246 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
15247 line_ptr += bytes_read;
15248 length =
15249 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
15250 line_ptr += bytes_read;
15251 add_file_name (lh, cur_file, dir_index, mod_time, length);
15252 }
c906108c 15253 break;
d0c6ba3d
CC
15254 case DW_LNE_set_discriminator:
15255 /* The discriminator is not interesting to the debugger;
15256 just ignore it. */
15257 line_ptr = extended_end;
15258 break;
c906108c 15259 default:
4d3c2250 15260 complaint (&symfile_complaints,
e2e0b3e5 15261 _("mangled .debug_line section"));
debd256d 15262 return;
c906108c 15263 }
a8c50c1f
DJ
15264 /* Make sure that we parsed the extended op correctly. If e.g.
15265 we expected a different address size than the producer used,
15266 we may have read the wrong number of bytes. */
15267 if (line_ptr != extended_end)
15268 {
15269 complaint (&symfile_complaints,
15270 _("mangled .debug_line section"));
15271 return;
15272 }
c906108c
SS
15273 break;
15274 case DW_LNS_copy:
59205f5a 15275 if (lh->num_file_names < file || file == 0)
25e43795
DJ
15276 dwarf2_debug_line_missing_file_complaint ();
15277 else
366da635 15278 {
25e43795 15279 lh->file_names[file - 1].included_p = 1;
ca5f395d 15280 if (!decode_for_pst_p && is_stmt)
fbf65064
UW
15281 {
15282 if (last_subfile != current_subfile)
15283 {
15284 addr = gdbarch_addr_bits_remove (gdbarch, address);
15285 if (last_subfile)
c91513d8 15286 (*p_record_line) (last_subfile, 0, addr);
fbf65064
UW
15287 last_subfile = current_subfile;
15288 }
7019d805 15289 addr = gdbarch_addr_bits_remove (gdbarch, address);
c91513d8 15290 (*p_record_line) (current_subfile, line, addr);
fbf65064 15291 }
366da635 15292 }
c906108c
SS
15293 basic_block = 0;
15294 break;
15295 case DW_LNS_advance_pc:
2dc7f7b3
TT
15296 {
15297 CORE_ADDR adjust
15298 = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
15299
15300 address += (((op_index + adjust)
15301 / lh->maximum_ops_per_instruction)
15302 * lh->minimum_instruction_length);
15303 op_index = ((op_index + adjust)
15304 % lh->maximum_ops_per_instruction);
15305 line_ptr += bytes_read;
15306 }
c906108c
SS
15307 break;
15308 case DW_LNS_advance_line:
15309 line += read_signed_leb128 (abfd, line_ptr, &bytes_read);
15310 line_ptr += bytes_read;
15311 break;
15312 case DW_LNS_set_file:
debd256d 15313 {
a738430d
MK
15314 /* The arrays lh->include_dirs and lh->file_names are
15315 0-based, but the directory and file name numbers in
15316 the statement program are 1-based. */
debd256d 15317 struct file_entry *fe;
4f1520fb 15318 char *dir = NULL;
a738430d 15319
debd256d
JB
15320 file = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
15321 line_ptr += bytes_read;
59205f5a 15322 if (lh->num_file_names < file || file == 0)
25e43795
DJ
15323 dwarf2_debug_line_missing_file_complaint ();
15324 else
15325 {
15326 fe = &lh->file_names[file - 1];
15327 if (fe->dir_index)
15328 dir = lh->include_dirs[fe->dir_index - 1];
15329 if (!decode_for_pst_p)
15330 {
15331 last_subfile = current_subfile;
15332 dwarf2_start_subfile (fe->name, dir, comp_dir);
15333 }
15334 }
debd256d 15335 }
c906108c
SS
15336 break;
15337 case DW_LNS_set_column:
15338 column = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
15339 line_ptr += bytes_read;
15340 break;
15341 case DW_LNS_negate_stmt:
15342 is_stmt = (!is_stmt);
15343 break;
15344 case DW_LNS_set_basic_block:
15345 basic_block = 1;
15346 break;
c2c6d25f
JM
15347 /* Add to the address register of the state machine the
15348 address increment value corresponding to special opcode
a738430d
MK
15349 255. I.e., this value is scaled by the minimum
15350 instruction length since special opcode 255 would have
b021a221 15351 scaled the increment. */
c906108c 15352 case DW_LNS_const_add_pc:
2dc7f7b3
TT
15353 {
15354 CORE_ADDR adjust = (255 - lh->opcode_base) / lh->line_range;
15355
15356 address += (((op_index + adjust)
15357 / lh->maximum_ops_per_instruction)
15358 * lh->minimum_instruction_length);
15359 op_index = ((op_index + adjust)
15360 % lh->maximum_ops_per_instruction);
15361 }
c906108c
SS
15362 break;
15363 case DW_LNS_fixed_advance_pc:
15364 address += read_2_bytes (abfd, line_ptr);
2dc7f7b3 15365 op_index = 0;
c906108c
SS
15366 line_ptr += 2;
15367 break;
9aa1fe7e 15368 default:
a738430d
MK
15369 {
15370 /* Unknown standard opcode, ignore it. */
9aa1fe7e 15371 int i;
a738430d 15372
debd256d 15373 for (i = 0; i < lh->standard_opcode_lengths[op_code]; i++)
9aa1fe7e
GK
15374 {
15375 (void) read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
15376 line_ptr += bytes_read;
15377 }
15378 }
c906108c
SS
15379 }
15380 }
59205f5a
JB
15381 if (lh->num_file_names < file || file == 0)
15382 dwarf2_debug_line_missing_file_complaint ();
15383 else
15384 {
15385 lh->file_names[file - 1].included_p = 1;
15386 if (!decode_for_pst_p)
fbf65064
UW
15387 {
15388 addr = gdbarch_addr_bits_remove (gdbarch, address);
c91513d8 15389 (*p_record_line) (current_subfile, 0, addr);
fbf65064 15390 }
59205f5a 15391 }
c906108c 15392 }
f3f5162e
DE
15393}
15394
15395/* Decode the Line Number Program (LNP) for the given line_header
15396 structure and CU. The actual information extracted and the type
15397 of structures created from the LNP depends on the value of PST.
15398
15399 1. If PST is NULL, then this procedure uses the data from the program
15400 to create all necessary symbol tables, and their linetables.
15401
15402 2. If PST is not NULL, this procedure reads the program to determine
15403 the list of files included by the unit represented by PST, and
15404 builds all the associated partial symbol tables.
15405
15406 COMP_DIR is the compilation directory (DW_AT_comp_dir) or NULL if unknown.
15407 It is used for relative paths in the line table.
15408 NOTE: When processing partial symtabs (pst != NULL),
15409 comp_dir == pst->dirname.
15410
15411 NOTE: It is important that psymtabs have the same file name (via strcmp)
15412 as the corresponding symtab. Since COMP_DIR is not used in the name of the
15413 symtab we don't use it in the name of the psymtabs we create.
15414 E.g. expand_line_sal requires this when finding psymtabs to expand.
15415 A good testcase for this is mb-inline.exp. */
15416
15417static void
15418dwarf_decode_lines (struct line_header *lh, const char *comp_dir,
15419 struct dwarf2_cu *cu, struct partial_symtab *pst,
15420 int want_line_info)
15421{
15422 struct objfile *objfile = cu->objfile;
15423 const int decode_for_pst_p = (pst != NULL);
15424 struct subfile *first_subfile = current_subfile;
15425
15426 if (want_line_info)
15427 dwarf_decode_lines_1 (lh, comp_dir, cu, pst);
aaa75496
JB
15428
15429 if (decode_for_pst_p)
15430 {
15431 int file_index;
15432
15433 /* Now that we're done scanning the Line Header Program, we can
15434 create the psymtab of each included file. */
15435 for (file_index = 0; file_index < lh->num_file_names; file_index++)
15436 if (lh->file_names[file_index].included_p == 1)
15437 {
c6da4cef
DE
15438 char *include_name =
15439 psymtab_include_file_name (lh, file_index, pst, comp_dir);
15440 if (include_name != NULL)
aaa75496
JB
15441 dwarf2_create_include_psymtab (include_name, pst, objfile);
15442 }
15443 }
cb1df416
DJ
15444 else
15445 {
15446 /* Make sure a symtab is created for every file, even files
15447 which contain only variables (i.e. no code with associated
15448 line numbers). */
cb1df416 15449 int i;
cb1df416
DJ
15450
15451 for (i = 0; i < lh->num_file_names; i++)
15452 {
15453 char *dir = NULL;
f3f5162e 15454 struct file_entry *fe;
9a619af0 15455
cb1df416
DJ
15456 fe = &lh->file_names[i];
15457 if (fe->dir_index)
15458 dir = lh->include_dirs[fe->dir_index - 1];
15459 dwarf2_start_subfile (fe->name, dir, comp_dir);
15460
15461 /* Skip the main file; we don't need it, and it must be
15462 allocated last, so that it will show up before the
15463 non-primary symtabs in the objfile's symtab list. */
15464 if (current_subfile == first_subfile)
15465 continue;
15466
15467 if (current_subfile->symtab == NULL)
15468 current_subfile->symtab = allocate_symtab (current_subfile->name,
bb5ed363 15469 objfile);
cb1df416
DJ
15470 fe->symtab = current_subfile->symtab;
15471 }
15472 }
c906108c
SS
15473}
15474
15475/* Start a subfile for DWARF. FILENAME is the name of the file and
15476 DIRNAME the name of the source directory which contains FILENAME
4f1520fb
FR
15477 or NULL if not known. COMP_DIR is the compilation directory for the
15478 linetable's compilation unit or NULL if not known.
c906108c
SS
15479 This routine tries to keep line numbers from identical absolute and
15480 relative file names in a common subfile.
15481
15482 Using the `list' example from the GDB testsuite, which resides in
15483 /srcdir and compiling it with Irix6.2 cc in /compdir using a filename
15484 of /srcdir/list0.c yields the following debugging information for list0.c:
15485
c5aa993b
JM
15486 DW_AT_name: /srcdir/list0.c
15487 DW_AT_comp_dir: /compdir
357e46e7 15488 files.files[0].name: list0.h
c5aa993b 15489 files.files[0].dir: /srcdir
357e46e7 15490 files.files[1].name: list0.c
c5aa993b 15491 files.files[1].dir: /srcdir
c906108c
SS
15492
15493 The line number information for list0.c has to end up in a single
4f1520fb
FR
15494 subfile, so that `break /srcdir/list0.c:1' works as expected.
15495 start_subfile will ensure that this happens provided that we pass the
15496 concatenation of files.files[1].dir and files.files[1].name as the
15497 subfile's name. */
c906108c
SS
15498
15499static void
3e43a32a
MS
15500dwarf2_start_subfile (char *filename, const char *dirname,
15501 const char *comp_dir)
c906108c 15502{
4f1520fb
FR
15503 char *fullname;
15504
15505 /* While reading the DIEs, we call start_symtab(DW_AT_name, DW_AT_comp_dir).
15506 `start_symtab' will always pass the contents of DW_AT_comp_dir as
15507 second argument to start_subfile. To be consistent, we do the
15508 same here. In order not to lose the line information directory,
15509 we concatenate it to the filename when it makes sense.
15510 Note that the Dwarf3 standard says (speaking of filenames in line
15511 information): ``The directory index is ignored for file names
15512 that represent full path names''. Thus ignoring dirname in the
15513 `else' branch below isn't an issue. */
c906108c 15514
d5166ae1 15515 if (!IS_ABSOLUTE_PATH (filename) && dirname != NULL)
4f1520fb
FR
15516 fullname = concat (dirname, SLASH_STRING, filename, (char *)NULL);
15517 else
15518 fullname = filename;
c906108c 15519
4f1520fb
FR
15520 start_subfile (fullname, comp_dir);
15521
15522 if (fullname != filename)
15523 xfree (fullname);
c906108c
SS
15524}
15525
f4dc4d17
DE
15526/* Start a symtab for DWARF.
15527 NAME, COMP_DIR, LOW_PC are passed to start_symtab. */
15528
15529static void
15530dwarf2_start_symtab (struct dwarf2_cu *cu,
15531 char *name, char *comp_dir, CORE_ADDR low_pc)
15532{
15533 start_symtab (name, comp_dir, low_pc);
15534 record_debugformat ("DWARF 2");
15535 record_producer (cu->producer);
15536
15537 /* We assume that we're processing GCC output. */
15538 processing_gcc_compilation = 2;
15539
15540 processing_has_namespace_info = 0;
15541}
15542
4c2df51b
DJ
15543static void
15544var_decode_location (struct attribute *attr, struct symbol *sym,
e7c27a73 15545 struct dwarf2_cu *cu)
4c2df51b 15546{
e7c27a73
DJ
15547 struct objfile *objfile = cu->objfile;
15548 struct comp_unit_head *cu_header = &cu->header;
15549
4c2df51b
DJ
15550 /* NOTE drow/2003-01-30: There used to be a comment and some special
15551 code here to turn a symbol with DW_AT_external and a
15552 SYMBOL_VALUE_ADDRESS of 0 into a LOC_UNRESOLVED symbol. This was
15553 necessary for platforms (maybe Alpha, certainly PowerPC GNU/Linux
15554 with some versions of binutils) where shared libraries could have
15555 relocations against symbols in their debug information - the
15556 minimal symbol would have the right address, but the debug info
15557 would not. It's no longer necessary, because we will explicitly
15558 apply relocations when we read in the debug information now. */
15559
15560 /* A DW_AT_location attribute with no contents indicates that a
15561 variable has been optimized away. */
15562 if (attr_form_is_block (attr) && DW_BLOCK (attr)->size == 0)
15563 {
15564 SYMBOL_CLASS (sym) = LOC_OPTIMIZED_OUT;
15565 return;
15566 }
15567
15568 /* Handle one degenerate form of location expression specially, to
15569 preserve GDB's previous behavior when section offsets are
3019eac3
DE
15570 specified. If this is just a DW_OP_addr or DW_OP_GNU_addr_index
15571 then mark this symbol as LOC_STATIC. */
4c2df51b
DJ
15572
15573 if (attr_form_is_block (attr)
3019eac3
DE
15574 && ((DW_BLOCK (attr)->data[0] == DW_OP_addr
15575 && DW_BLOCK (attr)->size == 1 + cu_header->addr_size)
15576 || (DW_BLOCK (attr)->data[0] == DW_OP_GNU_addr_index
15577 && (DW_BLOCK (attr)->size
15578 == 1 + leb128_size (&DW_BLOCK (attr)->data[1])))))
4c2df51b 15579 {
891d2f0b 15580 unsigned int dummy;
4c2df51b 15581
3019eac3
DE
15582 if (DW_BLOCK (attr)->data[0] == DW_OP_addr)
15583 SYMBOL_VALUE_ADDRESS (sym) =
15584 read_address (objfile->obfd, DW_BLOCK (attr)->data + 1, cu, &dummy);
15585 else
15586 SYMBOL_VALUE_ADDRESS (sym) =
15587 read_addr_index_from_leb128 (cu, DW_BLOCK (attr)->data + 1, &dummy);
907fc202 15588 SYMBOL_CLASS (sym) = LOC_STATIC;
4c2df51b
DJ
15589 fixup_symbol_section (sym, objfile);
15590 SYMBOL_VALUE_ADDRESS (sym) += ANOFFSET (objfile->section_offsets,
15591 SYMBOL_SECTION (sym));
4c2df51b
DJ
15592 return;
15593 }
15594
15595 /* NOTE drow/2002-01-30: It might be worthwhile to have a static
15596 expression evaluator, and use LOC_COMPUTED only when necessary
15597 (i.e. when the value of a register or memory location is
15598 referenced, or a thread-local block, etc.). Then again, it might
15599 not be worthwhile. I'm assuming that it isn't unless performance
15600 or memory numbers show me otherwise. */
15601
e7c27a73 15602 dwarf2_symbol_mark_computed (attr, sym, cu);
4c2df51b 15603 SYMBOL_CLASS (sym) = LOC_COMPUTED;
8be455d7
JK
15604
15605 if (SYMBOL_COMPUTED_OPS (sym) == &dwarf2_loclist_funcs)
15606 cu->has_loclist = 1;
4c2df51b
DJ
15607}
15608
c906108c
SS
15609/* Given a pointer to a DWARF information entry, figure out if we need
15610 to make a symbol table entry for it, and if so, create a new entry
15611 and return a pointer to it.
15612 If TYPE is NULL, determine symbol type from the die, otherwise
34eaf542
TT
15613 used the passed type.
15614 If SPACE is not NULL, use it to hold the new symbol. If it is
15615 NULL, allocate a new symbol on the objfile's obstack. */
c906108c
SS
15616
15617static struct symbol *
34eaf542
TT
15618new_symbol_full (struct die_info *die, struct type *type, struct dwarf2_cu *cu,
15619 struct symbol *space)
c906108c 15620{
e7c27a73 15621 struct objfile *objfile = cu->objfile;
c906108c
SS
15622 struct symbol *sym = NULL;
15623 char *name;
15624 struct attribute *attr = NULL;
15625 struct attribute *attr2 = NULL;
e142c38c 15626 CORE_ADDR baseaddr;
e37fd15a
SW
15627 struct pending **list_to_add = NULL;
15628
edb3359d 15629 int inlined_func = (die->tag == DW_TAG_inlined_subroutine);
e142c38c
DJ
15630
15631 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
c906108c 15632
94af9270 15633 name = dwarf2_name (die, cu);
c906108c
SS
15634 if (name)
15635 {
94af9270 15636 const char *linkagename;
34eaf542 15637 int suppress_add = 0;
94af9270 15638
34eaf542
TT
15639 if (space)
15640 sym = space;
15641 else
15642 sym = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct symbol);
c906108c 15643 OBJSTAT (objfile, n_syms++);
2de7ced7
DJ
15644
15645 /* Cache this symbol's name and the name's demangled form (if any). */
33e5013e 15646 SYMBOL_SET_LANGUAGE (sym, cu->language);
94af9270
KS
15647 linkagename = dwarf2_physname (name, die, cu);
15648 SYMBOL_SET_NAMES (sym, linkagename, strlen (linkagename), 0, objfile);
c906108c 15649
f55ee35c
JK
15650 /* Fortran does not have mangling standard and the mangling does differ
15651 between gfortran, iFort etc. */
15652 if (cu->language == language_fortran
b250c185 15653 && symbol_get_demangled_name (&(sym->ginfo)) == NULL)
29df156d
SW
15654 symbol_set_demangled_name (&(sym->ginfo),
15655 (char *) dwarf2_full_name (name, die, cu),
15656 NULL);
f55ee35c 15657
c906108c 15658 /* Default assumptions.
c5aa993b 15659 Use the passed type or decode it from the die. */
176620f1 15660 SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
875dc2fc 15661 SYMBOL_CLASS (sym) = LOC_OPTIMIZED_OUT;
c906108c
SS
15662 if (type != NULL)
15663 SYMBOL_TYPE (sym) = type;
15664 else
e7c27a73 15665 SYMBOL_TYPE (sym) = die_type (die, cu);
edb3359d
DJ
15666 attr = dwarf2_attr (die,
15667 inlined_func ? DW_AT_call_line : DW_AT_decl_line,
15668 cu);
c906108c
SS
15669 if (attr)
15670 {
15671 SYMBOL_LINE (sym) = DW_UNSND (attr);
15672 }
cb1df416 15673
edb3359d
DJ
15674 attr = dwarf2_attr (die,
15675 inlined_func ? DW_AT_call_file : DW_AT_decl_file,
15676 cu);
cb1df416
DJ
15677 if (attr)
15678 {
15679 int file_index = DW_UNSND (attr);
9a619af0 15680
cb1df416
DJ
15681 if (cu->line_header == NULL
15682 || file_index > cu->line_header->num_file_names)
15683 complaint (&symfile_complaints,
15684 _("file index out of range"));
1c3d648d 15685 else if (file_index > 0)
cb1df416
DJ
15686 {
15687 struct file_entry *fe;
9a619af0 15688
cb1df416
DJ
15689 fe = &cu->line_header->file_names[file_index - 1];
15690 SYMBOL_SYMTAB (sym) = fe->symtab;
15691 }
15692 }
15693
c906108c
SS
15694 switch (die->tag)
15695 {
15696 case DW_TAG_label:
e142c38c 15697 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
c906108c
SS
15698 if (attr)
15699 {
15700 SYMBOL_VALUE_ADDRESS (sym) = DW_ADDR (attr) + baseaddr;
15701 }
0f5238ed
TT
15702 SYMBOL_TYPE (sym) = objfile_type (objfile)->builtin_core_addr;
15703 SYMBOL_DOMAIN (sym) = LABEL_DOMAIN;
c906108c 15704 SYMBOL_CLASS (sym) = LOC_LABEL;
0f5238ed 15705 add_symbol_to_list (sym, cu->list_in_scope);
c906108c
SS
15706 break;
15707 case DW_TAG_subprogram:
15708 /* SYMBOL_BLOCK_VALUE (sym) will be filled in later by
15709 finish_block. */
15710 SYMBOL_CLASS (sym) = LOC_BLOCK;
e142c38c 15711 attr2 = dwarf2_attr (die, DW_AT_external, cu);
2cfa0c8d
JB
15712 if ((attr2 && (DW_UNSND (attr2) != 0))
15713 || cu->language == language_ada)
c906108c 15714 {
2cfa0c8d
JB
15715 /* Subprograms marked external are stored as a global symbol.
15716 Ada subprograms, whether marked external or not, are always
15717 stored as a global symbol, because we want to be able to
15718 access them globally. For instance, we want to be able
15719 to break on a nested subprogram without having to
15720 specify the context. */
e37fd15a 15721 list_to_add = &global_symbols;
c906108c
SS
15722 }
15723 else
15724 {
e37fd15a 15725 list_to_add = cu->list_in_scope;
c906108c
SS
15726 }
15727 break;
edb3359d
DJ
15728 case DW_TAG_inlined_subroutine:
15729 /* SYMBOL_BLOCK_VALUE (sym) will be filled in later by
15730 finish_block. */
15731 SYMBOL_CLASS (sym) = LOC_BLOCK;
15732 SYMBOL_INLINED (sym) = 1;
481860b3 15733 list_to_add = cu->list_in_scope;
edb3359d 15734 break;
34eaf542
TT
15735 case DW_TAG_template_value_param:
15736 suppress_add = 1;
15737 /* Fall through. */
72929c62 15738 case DW_TAG_constant:
c906108c 15739 case DW_TAG_variable:
254e6b9e 15740 case DW_TAG_member:
0963b4bd
MS
15741 /* Compilation with minimal debug info may result in
15742 variables with missing type entries. Change the
15743 misleading `void' type to something sensible. */
c906108c 15744 if (TYPE_CODE (SYMBOL_TYPE (sym)) == TYPE_CODE_VOID)
64c50499 15745 SYMBOL_TYPE (sym)
46bf5051 15746 = objfile_type (objfile)->nodebug_data_symbol;
64c50499 15747
e142c38c 15748 attr = dwarf2_attr (die, DW_AT_const_value, cu);
254e6b9e
DE
15749 /* In the case of DW_TAG_member, we should only be called for
15750 static const members. */
15751 if (die->tag == DW_TAG_member)
15752 {
3863f96c
DE
15753 /* dwarf2_add_field uses die_is_declaration,
15754 so we do the same. */
254e6b9e
DE
15755 gdb_assert (die_is_declaration (die, cu));
15756 gdb_assert (attr);
15757 }
c906108c
SS
15758 if (attr)
15759 {
e7c27a73 15760 dwarf2_const_value (attr, sym, cu);
e142c38c 15761 attr2 = dwarf2_attr (die, DW_AT_external, cu);
e37fd15a 15762 if (!suppress_add)
34eaf542
TT
15763 {
15764 if (attr2 && (DW_UNSND (attr2) != 0))
e37fd15a 15765 list_to_add = &global_symbols;
34eaf542 15766 else
e37fd15a 15767 list_to_add = cu->list_in_scope;
34eaf542 15768 }
c906108c
SS
15769 break;
15770 }
e142c38c 15771 attr = dwarf2_attr (die, DW_AT_location, cu);
c906108c
SS
15772 if (attr)
15773 {
e7c27a73 15774 var_decode_location (attr, sym, cu);
e142c38c 15775 attr2 = dwarf2_attr (die, DW_AT_external, cu);
4357ac6c
TT
15776
15777 /* Fortran explicitly imports any global symbols to the local
15778 scope by DW_TAG_common_block. */
15779 if (cu->language == language_fortran && die->parent
15780 && die->parent->tag == DW_TAG_common_block)
15781 attr2 = NULL;
15782
caac4577
JG
15783 if (SYMBOL_CLASS (sym) == LOC_STATIC
15784 && SYMBOL_VALUE_ADDRESS (sym) == 0
15785 && !dwarf2_per_objfile->has_section_at_zero)
15786 {
15787 /* When a static variable is eliminated by the linker,
15788 the corresponding debug information is not stripped
15789 out, but the variable address is set to null;
15790 do not add such variables into symbol table. */
15791 }
15792 else if (attr2 && (DW_UNSND (attr2) != 0))
1c809c68 15793 {
f55ee35c
JK
15794 /* Workaround gfortran PR debug/40040 - it uses
15795 DW_AT_location for variables in -fPIC libraries which may
15796 get overriden by other libraries/executable and get
15797 a different address. Resolve it by the minimal symbol
15798 which may come from inferior's executable using copy
15799 relocation. Make this workaround only for gfortran as for
15800 other compilers GDB cannot guess the minimal symbol
15801 Fortran mangling kind. */
15802 if (cu->language == language_fortran && die->parent
15803 && die->parent->tag == DW_TAG_module
15804 && cu->producer
15805 && strncmp (cu->producer, "GNU Fortran ", 12) == 0)
15806 SYMBOL_CLASS (sym) = LOC_UNRESOLVED;
15807
1c809c68
TT
15808 /* A variable with DW_AT_external is never static,
15809 but it may be block-scoped. */
15810 list_to_add = (cu->list_in_scope == &file_symbols
15811 ? &global_symbols : cu->list_in_scope);
1c809c68 15812 }
c906108c 15813 else
e37fd15a 15814 list_to_add = cu->list_in_scope;
c906108c
SS
15815 }
15816 else
15817 {
15818 /* We do not know the address of this symbol.
c5aa993b
JM
15819 If it is an external symbol and we have type information
15820 for it, enter the symbol as a LOC_UNRESOLVED symbol.
15821 The address of the variable will then be determined from
15822 the minimal symbol table whenever the variable is
15823 referenced. */
e142c38c 15824 attr2 = dwarf2_attr (die, DW_AT_external, cu);
0971de02
TT
15825
15826 /* Fortran explicitly imports any global symbols to the local
15827 scope by DW_TAG_common_block. */
15828 if (cu->language == language_fortran && die->parent
15829 && die->parent->tag == DW_TAG_common_block)
15830 {
15831 /* SYMBOL_CLASS doesn't matter here because
15832 read_common_block is going to reset it. */
15833 if (!suppress_add)
15834 list_to_add = cu->list_in_scope;
15835 }
15836 else if (attr2 && (DW_UNSND (attr2) != 0)
15837 && dwarf2_attr (die, DW_AT_type, cu) != NULL)
c906108c 15838 {
0fe7935b
DJ
15839 /* A variable with DW_AT_external is never static, but it
15840 may be block-scoped. */
15841 list_to_add = (cu->list_in_scope == &file_symbols
15842 ? &global_symbols : cu->list_in_scope);
15843
c906108c 15844 SYMBOL_CLASS (sym) = LOC_UNRESOLVED;
c906108c 15845 }
442ddf59
JK
15846 else if (!die_is_declaration (die, cu))
15847 {
15848 /* Use the default LOC_OPTIMIZED_OUT class. */
15849 gdb_assert (SYMBOL_CLASS (sym) == LOC_OPTIMIZED_OUT);
e37fd15a
SW
15850 if (!suppress_add)
15851 list_to_add = cu->list_in_scope;
442ddf59 15852 }
c906108c
SS
15853 }
15854 break;
15855 case DW_TAG_formal_parameter:
edb3359d
DJ
15856 /* If we are inside a function, mark this as an argument. If
15857 not, we might be looking at an argument to an inlined function
15858 when we do not have enough information to show inlined frames;
15859 pretend it's a local variable in that case so that the user can
15860 still see it. */
15861 if (context_stack_depth > 0
15862 && context_stack[context_stack_depth - 1].name != NULL)
15863 SYMBOL_IS_ARGUMENT (sym) = 1;
e142c38c 15864 attr = dwarf2_attr (die, DW_AT_location, cu);
c906108c
SS
15865 if (attr)
15866 {
e7c27a73 15867 var_decode_location (attr, sym, cu);
c906108c 15868 }
e142c38c 15869 attr = dwarf2_attr (die, DW_AT_const_value, cu);
c906108c
SS
15870 if (attr)
15871 {
e7c27a73 15872 dwarf2_const_value (attr, sym, cu);
c906108c 15873 }
f346a30d 15874
e37fd15a 15875 list_to_add = cu->list_in_scope;
c906108c
SS
15876 break;
15877 case DW_TAG_unspecified_parameters:
15878 /* From varargs functions; gdb doesn't seem to have any
15879 interest in this information, so just ignore it for now.
15880 (FIXME?) */
15881 break;
34eaf542
TT
15882 case DW_TAG_template_type_param:
15883 suppress_add = 1;
15884 /* Fall through. */
c906108c 15885 case DW_TAG_class_type:
680b30c7 15886 case DW_TAG_interface_type:
c906108c
SS
15887 case DW_TAG_structure_type:
15888 case DW_TAG_union_type:
72019c9c 15889 case DW_TAG_set_type:
c906108c
SS
15890 case DW_TAG_enumeration_type:
15891 SYMBOL_CLASS (sym) = LOC_TYPEDEF;
176620f1 15892 SYMBOL_DOMAIN (sym) = STRUCT_DOMAIN;
c906108c 15893
63d06c5c 15894 {
987504bb 15895 /* NOTE: carlton/2003-11-10: C++ and Java class symbols shouldn't
63d06c5c
DC
15896 really ever be static objects: otherwise, if you try
15897 to, say, break of a class's method and you're in a file
15898 which doesn't mention that class, it won't work unless
15899 the check for all static symbols in lookup_symbol_aux
15900 saves you. See the OtherFileClass tests in
15901 gdb.c++/namespace.exp. */
15902
e37fd15a 15903 if (!suppress_add)
34eaf542 15904 {
34eaf542
TT
15905 list_to_add = (cu->list_in_scope == &file_symbols
15906 && (cu->language == language_cplus
15907 || cu->language == language_java)
15908 ? &global_symbols : cu->list_in_scope);
63d06c5c 15909
64382290
TT
15910 /* The semantics of C++ state that "struct foo {
15911 ... }" also defines a typedef for "foo". A Java
15912 class declaration also defines a typedef for the
15913 class. */
15914 if (cu->language == language_cplus
15915 || cu->language == language_java
15916 || cu->language == language_ada)
15917 {
15918 /* The symbol's name is already allocated along
15919 with this objfile, so we don't need to
15920 duplicate it for the type. */
15921 if (TYPE_NAME (SYMBOL_TYPE (sym)) == 0)
15922 TYPE_NAME (SYMBOL_TYPE (sym)) = SYMBOL_SEARCH_NAME (sym);
15923 }
63d06c5c
DC
15924 }
15925 }
c906108c
SS
15926 break;
15927 case DW_TAG_typedef:
63d06c5c
DC
15928 SYMBOL_CLASS (sym) = LOC_TYPEDEF;
15929 SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
e37fd15a 15930 list_to_add = cu->list_in_scope;
63d06c5c 15931 break;
c906108c 15932 case DW_TAG_base_type:
a02abb62 15933 case DW_TAG_subrange_type:
c906108c 15934 SYMBOL_CLASS (sym) = LOC_TYPEDEF;
176620f1 15935 SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
e37fd15a 15936 list_to_add = cu->list_in_scope;
c906108c
SS
15937 break;
15938 case DW_TAG_enumerator:
e142c38c 15939 attr = dwarf2_attr (die, DW_AT_const_value, cu);
c906108c
SS
15940 if (attr)
15941 {
e7c27a73 15942 dwarf2_const_value (attr, sym, cu);
c906108c 15943 }
63d06c5c
DC
15944 {
15945 /* NOTE: carlton/2003-11-10: See comment above in the
15946 DW_TAG_class_type, etc. block. */
15947
e142c38c 15948 list_to_add = (cu->list_in_scope == &file_symbols
987504bb
JJ
15949 && (cu->language == language_cplus
15950 || cu->language == language_java)
e142c38c 15951 ? &global_symbols : cu->list_in_scope);
63d06c5c 15952 }
c906108c 15953 break;
5c4e30ca
DC
15954 case DW_TAG_namespace:
15955 SYMBOL_CLASS (sym) = LOC_TYPEDEF;
e37fd15a 15956 list_to_add = &global_symbols;
5c4e30ca 15957 break;
4357ac6c
TT
15958 case DW_TAG_common_block:
15959 SYMBOL_CLASS (sym) = LOC_STATIC;
15960 SYMBOL_DOMAIN (sym) = COMMON_BLOCK_DOMAIN;
15961 add_symbol_to_list (sym, cu->list_in_scope);
15962 break;
c906108c
SS
15963 default:
15964 /* Not a tag we recognize. Hopefully we aren't processing
15965 trash data, but since we must specifically ignore things
15966 we don't recognize, there is nothing else we should do at
0963b4bd 15967 this point. */
e2e0b3e5 15968 complaint (&symfile_complaints, _("unsupported tag: '%s'"),
4d3c2250 15969 dwarf_tag_name (die->tag));
c906108c
SS
15970 break;
15971 }
df8a16a1 15972
e37fd15a
SW
15973 if (suppress_add)
15974 {
15975 sym->hash_next = objfile->template_symbols;
15976 objfile->template_symbols = sym;
15977 list_to_add = NULL;
15978 }
15979
15980 if (list_to_add != NULL)
15981 add_symbol_to_list (sym, list_to_add);
15982
df8a16a1
DJ
15983 /* For the benefit of old versions of GCC, check for anonymous
15984 namespaces based on the demangled name. */
15985 if (!processing_has_namespace_info
94af9270 15986 && cu->language == language_cplus)
a10964d1 15987 cp_scan_for_anonymous_namespaces (sym, objfile);
c906108c
SS
15988 }
15989 return (sym);
15990}
15991
34eaf542
TT
15992/* A wrapper for new_symbol_full that always allocates a new symbol. */
15993
15994static struct symbol *
15995new_symbol (struct die_info *die, struct type *type, struct dwarf2_cu *cu)
15996{
15997 return new_symbol_full (die, type, cu, NULL);
15998}
15999
98bfdba5
PA
16000/* Given an attr with a DW_FORM_dataN value in host byte order,
16001 zero-extend it as appropriate for the symbol's type. The DWARF
16002 standard (v4) is not entirely clear about the meaning of using
16003 DW_FORM_dataN for a constant with a signed type, where the type is
16004 wider than the data. The conclusion of a discussion on the DWARF
16005 list was that this is unspecified. We choose to always zero-extend
16006 because that is the interpretation long in use by GCC. */
c906108c 16007
98bfdba5
PA
16008static gdb_byte *
16009dwarf2_const_value_data (struct attribute *attr, struct type *type,
16010 const char *name, struct obstack *obstack,
12df843f 16011 struct dwarf2_cu *cu, LONGEST *value, int bits)
c906108c 16012{
e7c27a73 16013 struct objfile *objfile = cu->objfile;
e17a4113
UW
16014 enum bfd_endian byte_order = bfd_big_endian (objfile->obfd) ?
16015 BFD_ENDIAN_BIG : BFD_ENDIAN_LITTLE;
98bfdba5
PA
16016 LONGEST l = DW_UNSND (attr);
16017
16018 if (bits < sizeof (*value) * 8)
16019 {
16020 l &= ((LONGEST) 1 << bits) - 1;
16021 *value = l;
16022 }
16023 else if (bits == sizeof (*value) * 8)
16024 *value = l;
16025 else
16026 {
16027 gdb_byte *bytes = obstack_alloc (obstack, bits / 8);
16028 store_unsigned_integer (bytes, bits / 8, byte_order, l);
16029 return bytes;
16030 }
16031
16032 return NULL;
16033}
16034
16035/* Read a constant value from an attribute. Either set *VALUE, or if
16036 the value does not fit in *VALUE, set *BYTES - either already
16037 allocated on the objfile obstack, or newly allocated on OBSTACK,
16038 or, set *BATON, if we translated the constant to a location
16039 expression. */
16040
16041static void
16042dwarf2_const_value_attr (struct attribute *attr, struct type *type,
16043 const char *name, struct obstack *obstack,
16044 struct dwarf2_cu *cu,
12df843f 16045 LONGEST *value, gdb_byte **bytes,
98bfdba5
PA
16046 struct dwarf2_locexpr_baton **baton)
16047{
16048 struct objfile *objfile = cu->objfile;
16049 struct comp_unit_head *cu_header = &cu->header;
c906108c 16050 struct dwarf_block *blk;
98bfdba5
PA
16051 enum bfd_endian byte_order = (bfd_big_endian (objfile->obfd) ?
16052 BFD_ENDIAN_BIG : BFD_ENDIAN_LITTLE);
16053
16054 *value = 0;
16055 *bytes = NULL;
16056 *baton = NULL;
c906108c
SS
16057
16058 switch (attr->form)
16059 {
16060 case DW_FORM_addr:
3019eac3 16061 case DW_FORM_GNU_addr_index:
ac56253d 16062 {
ac56253d
TT
16063 gdb_byte *data;
16064
98bfdba5
PA
16065 if (TYPE_LENGTH (type) != cu_header->addr_size)
16066 dwarf2_const_value_length_mismatch_complaint (name,
ac56253d 16067 cu_header->addr_size,
98bfdba5 16068 TYPE_LENGTH (type));
ac56253d
TT
16069 /* Symbols of this form are reasonably rare, so we just
16070 piggyback on the existing location code rather than writing
16071 a new implementation of symbol_computed_ops. */
98bfdba5
PA
16072 *baton = obstack_alloc (&objfile->objfile_obstack,
16073 sizeof (struct dwarf2_locexpr_baton));
16074 (*baton)->per_cu = cu->per_cu;
16075 gdb_assert ((*baton)->per_cu);
ac56253d 16076
98bfdba5
PA
16077 (*baton)->size = 2 + cu_header->addr_size;
16078 data = obstack_alloc (&objfile->objfile_obstack, (*baton)->size);
16079 (*baton)->data = data;
ac56253d
TT
16080
16081 data[0] = DW_OP_addr;
16082 store_unsigned_integer (&data[1], cu_header->addr_size,
16083 byte_order, DW_ADDR (attr));
16084 data[cu_header->addr_size + 1] = DW_OP_stack_value;
ac56253d 16085 }
c906108c 16086 break;
4ac36638 16087 case DW_FORM_string:
93b5768b 16088 case DW_FORM_strp:
3019eac3 16089 case DW_FORM_GNU_str_index:
36586728 16090 case DW_FORM_GNU_strp_alt:
98bfdba5
PA
16091 /* DW_STRING is already allocated on the objfile obstack, point
16092 directly to it. */
16093 *bytes = (gdb_byte *) DW_STRING (attr);
93b5768b 16094 break;
c906108c
SS
16095 case DW_FORM_block1:
16096 case DW_FORM_block2:
16097 case DW_FORM_block4:
16098 case DW_FORM_block:
2dc7f7b3 16099 case DW_FORM_exprloc:
c906108c 16100 blk = DW_BLOCK (attr);
98bfdba5
PA
16101 if (TYPE_LENGTH (type) != blk->size)
16102 dwarf2_const_value_length_mismatch_complaint (name, blk->size,
16103 TYPE_LENGTH (type));
16104 *bytes = blk->data;
c906108c 16105 break;
2df3850c
JM
16106
16107 /* The DW_AT_const_value attributes are supposed to carry the
16108 symbol's value "represented as it would be on the target
16109 architecture." By the time we get here, it's already been
16110 converted to host endianness, so we just need to sign- or
16111 zero-extend it as appropriate. */
16112 case DW_FORM_data1:
3e43a32a
MS
16113 *bytes = dwarf2_const_value_data (attr, type, name,
16114 obstack, cu, value, 8);
2df3850c 16115 break;
c906108c 16116 case DW_FORM_data2:
3e43a32a
MS
16117 *bytes = dwarf2_const_value_data (attr, type, name,
16118 obstack, cu, value, 16);
2df3850c 16119 break;
c906108c 16120 case DW_FORM_data4:
3e43a32a
MS
16121 *bytes = dwarf2_const_value_data (attr, type, name,
16122 obstack, cu, value, 32);
2df3850c 16123 break;
c906108c 16124 case DW_FORM_data8:
3e43a32a
MS
16125 *bytes = dwarf2_const_value_data (attr, type, name,
16126 obstack, cu, value, 64);
2df3850c
JM
16127 break;
16128
c906108c 16129 case DW_FORM_sdata:
98bfdba5 16130 *value = DW_SND (attr);
2df3850c
JM
16131 break;
16132
c906108c 16133 case DW_FORM_udata:
98bfdba5 16134 *value = DW_UNSND (attr);
c906108c 16135 break;
2df3850c 16136
c906108c 16137 default:
4d3c2250 16138 complaint (&symfile_complaints,
e2e0b3e5 16139 _("unsupported const value attribute form: '%s'"),
4d3c2250 16140 dwarf_form_name (attr->form));
98bfdba5 16141 *value = 0;
c906108c
SS
16142 break;
16143 }
16144}
16145
2df3850c 16146
98bfdba5
PA
16147/* Copy constant value from an attribute to a symbol. */
16148
2df3850c 16149static void
98bfdba5
PA
16150dwarf2_const_value (struct attribute *attr, struct symbol *sym,
16151 struct dwarf2_cu *cu)
2df3850c 16152{
98bfdba5
PA
16153 struct objfile *objfile = cu->objfile;
16154 struct comp_unit_head *cu_header = &cu->header;
12df843f 16155 LONGEST value;
98bfdba5
PA
16156 gdb_byte *bytes;
16157 struct dwarf2_locexpr_baton *baton;
2df3850c 16158
98bfdba5
PA
16159 dwarf2_const_value_attr (attr, SYMBOL_TYPE (sym),
16160 SYMBOL_PRINT_NAME (sym),
16161 &objfile->objfile_obstack, cu,
16162 &value, &bytes, &baton);
2df3850c 16163
98bfdba5
PA
16164 if (baton != NULL)
16165 {
16166 SYMBOL_COMPUTED_OPS (sym) = &dwarf2_locexpr_funcs;
16167 SYMBOL_LOCATION_BATON (sym) = baton;
16168 SYMBOL_CLASS (sym) = LOC_COMPUTED;
16169 }
16170 else if (bytes != NULL)
16171 {
16172 SYMBOL_VALUE_BYTES (sym) = bytes;
16173 SYMBOL_CLASS (sym) = LOC_CONST_BYTES;
16174 }
16175 else
16176 {
16177 SYMBOL_VALUE (sym) = value;
16178 SYMBOL_CLASS (sym) = LOC_CONST;
16179 }
2df3850c
JM
16180}
16181
c906108c
SS
16182/* Return the type of the die in question using its DW_AT_type attribute. */
16183
16184static struct type *
e7c27a73 16185die_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 16186{
c906108c 16187 struct attribute *type_attr;
c906108c 16188
e142c38c 16189 type_attr = dwarf2_attr (die, DW_AT_type, cu);
c906108c
SS
16190 if (!type_attr)
16191 {
16192 /* A missing DW_AT_type represents a void type. */
46bf5051 16193 return objfile_type (cu->objfile)->builtin_void;
c906108c 16194 }
348e048f 16195
673bfd45 16196 return lookup_die_type (die, type_attr, cu);
c906108c
SS
16197}
16198
b4ba55a1
JB
16199/* True iff CU's producer generates GNAT Ada auxiliary information
16200 that allows to find parallel types through that information instead
16201 of having to do expensive parallel lookups by type name. */
16202
16203static int
16204need_gnat_info (struct dwarf2_cu *cu)
16205{
16206 /* FIXME: brobecker/2010-10-12: As of now, only the AdaCore version
16207 of GNAT produces this auxiliary information, without any indication
16208 that it is produced. Part of enhancing the FSF version of GNAT
16209 to produce that information will be to put in place an indicator
16210 that we can use in order to determine whether the descriptive type
16211 info is available or not. One suggestion that has been made is
16212 to use a new attribute, attached to the CU die. For now, assume
16213 that the descriptive type info is not available. */
16214 return 0;
16215}
16216
b4ba55a1
JB
16217/* Return the auxiliary type of the die in question using its
16218 DW_AT_GNAT_descriptive_type attribute. Returns NULL if the
16219 attribute is not present. */
16220
16221static struct type *
16222die_descriptive_type (struct die_info *die, struct dwarf2_cu *cu)
16223{
b4ba55a1 16224 struct attribute *type_attr;
b4ba55a1
JB
16225
16226 type_attr = dwarf2_attr (die, DW_AT_GNAT_descriptive_type, cu);
16227 if (!type_attr)
16228 return NULL;
16229
673bfd45 16230 return lookup_die_type (die, type_attr, cu);
b4ba55a1
JB
16231}
16232
16233/* If DIE has a descriptive_type attribute, then set the TYPE's
16234 descriptive type accordingly. */
16235
16236static void
16237set_descriptive_type (struct type *type, struct die_info *die,
16238 struct dwarf2_cu *cu)
16239{
16240 struct type *descriptive_type = die_descriptive_type (die, cu);
16241
16242 if (descriptive_type)
16243 {
16244 ALLOCATE_GNAT_AUX_TYPE (type);
16245 TYPE_DESCRIPTIVE_TYPE (type) = descriptive_type;
16246 }
16247}
16248
c906108c
SS
16249/* Return the containing type of the die in question using its
16250 DW_AT_containing_type attribute. */
16251
16252static struct type *
e7c27a73 16253die_containing_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 16254{
c906108c 16255 struct attribute *type_attr;
c906108c 16256
e142c38c 16257 type_attr = dwarf2_attr (die, DW_AT_containing_type, cu);
33ac96f0
JK
16258 if (!type_attr)
16259 error (_("Dwarf Error: Problem turning containing type into gdb type "
16260 "[in module %s]"), cu->objfile->name);
16261
673bfd45 16262 return lookup_die_type (die, type_attr, cu);
c906108c
SS
16263}
16264
673bfd45
DE
16265/* Look up the type of DIE in CU using its type attribute ATTR.
16266 If there is no type substitute an error marker. */
16267
c906108c 16268static struct type *
673bfd45
DE
16269lookup_die_type (struct die_info *die, struct attribute *attr,
16270 struct dwarf2_cu *cu)
c906108c 16271{
bb5ed363 16272 struct objfile *objfile = cu->objfile;
f792889a
DJ
16273 struct type *this_type;
16274
673bfd45
DE
16275 /* First see if we have it cached. */
16276
36586728
TT
16277 if (attr->form == DW_FORM_GNU_ref_alt)
16278 {
16279 struct dwarf2_per_cu_data *per_cu;
16280 sect_offset offset = dwarf2_get_ref_die_offset (attr);
16281
16282 per_cu = dwarf2_find_containing_comp_unit (offset, 1, cu->objfile);
16283 this_type = get_die_type_at_offset (offset, per_cu);
16284 }
16285 else if (is_ref_attr (attr))
673bfd45 16286 {
b64f50a1 16287 sect_offset offset = dwarf2_get_ref_die_offset (attr);
673bfd45
DE
16288
16289 this_type = get_die_type_at_offset (offset, cu->per_cu);
16290 }
55f1336d 16291 else if (attr->form == DW_FORM_ref_sig8)
673bfd45
DE
16292 {
16293 struct signatured_type *sig_type = DW_SIGNATURED_TYPE (attr);
673bfd45
DE
16294
16295 /* sig_type will be NULL if the signatured type is missing from
16296 the debug info. */
16297 if (sig_type == NULL)
16298 error (_("Dwarf Error: Cannot find signatured DIE referenced from DIE "
16299 "at 0x%x [in module %s]"),
b64f50a1 16300 die->offset.sect_off, objfile->name);
673bfd45 16301
3019eac3
DE
16302 gdb_assert (sig_type->per_cu.is_debug_types);
16303 /* If we haven't filled in type_offset_in_section yet, then we
16304 haven't read the type in yet. */
16305 this_type = NULL;
16306 if (sig_type->type_offset_in_section.sect_off != 0)
16307 {
16308 this_type =
16309 get_die_type_at_offset (sig_type->type_offset_in_section,
16310 &sig_type->per_cu);
16311 }
673bfd45
DE
16312 }
16313 else
16314 {
16315 dump_die_for_error (die);
16316 error (_("Dwarf Error: Bad type attribute %s [in module %s]"),
bb5ed363 16317 dwarf_attr_name (attr->name), objfile->name);
673bfd45
DE
16318 }
16319
16320 /* If not cached we need to read it in. */
16321
16322 if (this_type == NULL)
16323 {
16324 struct die_info *type_die;
16325 struct dwarf2_cu *type_cu = cu;
16326
16327 type_die = follow_die_ref_or_sig (die, attr, &type_cu);
3019eac3
DE
16328 /* If we found the type now, it's probably because the type came
16329 from an inter-CU reference and the type's CU got expanded before
16330 ours. */
16331 this_type = get_die_type (type_die, type_cu);
16332 if (this_type == NULL)
16333 this_type = read_type_die_1 (type_die, type_cu);
673bfd45
DE
16334 }
16335
16336 /* If we still don't have a type use an error marker. */
16337
16338 if (this_type == NULL)
c906108c 16339 {
b00fdb78
TT
16340 char *message, *saved;
16341
16342 /* read_type_die already issued a complaint. */
16343 message = xstrprintf (_("<unknown type in %s, CU 0x%x, DIE 0x%x>"),
bb5ed363 16344 objfile->name,
b64f50a1
JK
16345 cu->header.offset.sect_off,
16346 die->offset.sect_off);
bb5ed363 16347 saved = obstack_copy0 (&objfile->objfile_obstack,
b00fdb78
TT
16348 message, strlen (message));
16349 xfree (message);
16350
bb5ed363 16351 this_type = init_type (TYPE_CODE_ERROR, 0, 0, saved, objfile);
c906108c 16352 }
673bfd45 16353
f792889a 16354 return this_type;
c906108c
SS
16355}
16356
673bfd45
DE
16357/* Return the type in DIE, CU.
16358 Returns NULL for invalid types.
16359
16360 This first does a lookup in the appropriate type_hash table,
16361 and only reads the die in if necessary.
16362
16363 NOTE: This can be called when reading in partial or full symbols. */
16364
f792889a 16365static struct type *
e7c27a73 16366read_type_die (struct die_info *die, struct dwarf2_cu *cu)
c906108c 16367{
f792889a
DJ
16368 struct type *this_type;
16369
16370 this_type = get_die_type (die, cu);
16371 if (this_type)
16372 return this_type;
16373
673bfd45
DE
16374 return read_type_die_1 (die, cu);
16375}
16376
16377/* Read the type in DIE, CU.
16378 Returns NULL for invalid types. */
16379
16380static struct type *
16381read_type_die_1 (struct die_info *die, struct dwarf2_cu *cu)
16382{
16383 struct type *this_type = NULL;
16384
c906108c
SS
16385 switch (die->tag)
16386 {
16387 case DW_TAG_class_type:
680b30c7 16388 case DW_TAG_interface_type:
c906108c
SS
16389 case DW_TAG_structure_type:
16390 case DW_TAG_union_type:
f792889a 16391 this_type = read_structure_type (die, cu);
c906108c
SS
16392 break;
16393 case DW_TAG_enumeration_type:
f792889a 16394 this_type = read_enumeration_type (die, cu);
c906108c
SS
16395 break;
16396 case DW_TAG_subprogram:
16397 case DW_TAG_subroutine_type:
edb3359d 16398 case DW_TAG_inlined_subroutine:
f792889a 16399 this_type = read_subroutine_type (die, cu);
c906108c
SS
16400 break;
16401 case DW_TAG_array_type:
f792889a 16402 this_type = read_array_type (die, cu);
c906108c 16403 break;
72019c9c 16404 case DW_TAG_set_type:
f792889a 16405 this_type = read_set_type (die, cu);
72019c9c 16406 break;
c906108c 16407 case DW_TAG_pointer_type:
f792889a 16408 this_type = read_tag_pointer_type (die, cu);
c906108c
SS
16409 break;
16410 case DW_TAG_ptr_to_member_type:
f792889a 16411 this_type = read_tag_ptr_to_member_type (die, cu);
c906108c
SS
16412 break;
16413 case DW_TAG_reference_type:
f792889a 16414 this_type = read_tag_reference_type (die, cu);
c906108c
SS
16415 break;
16416 case DW_TAG_const_type:
f792889a 16417 this_type = read_tag_const_type (die, cu);
c906108c
SS
16418 break;
16419 case DW_TAG_volatile_type:
f792889a 16420 this_type = read_tag_volatile_type (die, cu);
c906108c
SS
16421 break;
16422 case DW_TAG_string_type:
f792889a 16423 this_type = read_tag_string_type (die, cu);
c906108c
SS
16424 break;
16425 case DW_TAG_typedef:
f792889a 16426 this_type = read_typedef (die, cu);
c906108c 16427 break;
a02abb62 16428 case DW_TAG_subrange_type:
f792889a 16429 this_type = read_subrange_type (die, cu);
a02abb62 16430 break;
c906108c 16431 case DW_TAG_base_type:
f792889a 16432 this_type = read_base_type (die, cu);
c906108c 16433 break;
81a17f79 16434 case DW_TAG_unspecified_type:
f792889a 16435 this_type = read_unspecified_type (die, cu);
81a17f79 16436 break;
0114d602
DJ
16437 case DW_TAG_namespace:
16438 this_type = read_namespace_type (die, cu);
16439 break;
f55ee35c
JK
16440 case DW_TAG_module:
16441 this_type = read_module_type (die, cu);
16442 break;
c906108c 16443 default:
3e43a32a
MS
16444 complaint (&symfile_complaints,
16445 _("unexpected tag in read_type_die: '%s'"),
4d3c2250 16446 dwarf_tag_name (die->tag));
c906108c
SS
16447 break;
16448 }
63d06c5c 16449
f792889a 16450 return this_type;
63d06c5c
DC
16451}
16452
abc72ce4
DE
16453/* See if we can figure out if the class lives in a namespace. We do
16454 this by looking for a member function; its demangled name will
16455 contain namespace info, if there is any.
16456 Return the computed name or NULL.
16457 Space for the result is allocated on the objfile's obstack.
16458 This is the full-die version of guess_partial_die_structure_name.
16459 In this case we know DIE has no useful parent. */
16460
16461static char *
16462guess_full_die_structure_name (struct die_info *die, struct dwarf2_cu *cu)
16463{
16464 struct die_info *spec_die;
16465 struct dwarf2_cu *spec_cu;
16466 struct die_info *child;
16467
16468 spec_cu = cu;
16469 spec_die = die_specification (die, &spec_cu);
16470 if (spec_die != NULL)
16471 {
16472 die = spec_die;
16473 cu = spec_cu;
16474 }
16475
16476 for (child = die->child;
16477 child != NULL;
16478 child = child->sibling)
16479 {
16480 if (child->tag == DW_TAG_subprogram)
16481 {
16482 struct attribute *attr;
16483
16484 attr = dwarf2_attr (child, DW_AT_linkage_name, cu);
16485 if (attr == NULL)
16486 attr = dwarf2_attr (child, DW_AT_MIPS_linkage_name, cu);
16487 if (attr != NULL)
16488 {
16489 char *actual_name
16490 = language_class_name_from_physname (cu->language_defn,
16491 DW_STRING (attr));
16492 char *name = NULL;
16493
16494 if (actual_name != NULL)
16495 {
16496 char *die_name = dwarf2_name (die, cu);
16497
16498 if (die_name != NULL
16499 && strcmp (die_name, actual_name) != 0)
16500 {
16501 /* Strip off the class name from the full name.
16502 We want the prefix. */
16503 int die_name_len = strlen (die_name);
16504 int actual_name_len = strlen (actual_name);
16505
16506 /* Test for '::' as a sanity check. */
16507 if (actual_name_len > die_name_len + 2
3e43a32a
MS
16508 && actual_name[actual_name_len
16509 - die_name_len - 1] == ':')
abc72ce4
DE
16510 name =
16511 obsavestring (actual_name,
16512 actual_name_len - die_name_len - 2,
16513 &cu->objfile->objfile_obstack);
16514 }
16515 }
16516 xfree (actual_name);
16517 return name;
16518 }
16519 }
16520 }
16521
16522 return NULL;
16523}
16524
96408a79
SA
16525/* GCC might emit a nameless typedef that has a linkage name. Determine the
16526 prefix part in such case. See
16527 http://gcc.gnu.org/bugzilla/show_bug.cgi?id=47510. */
16528
16529static char *
16530anonymous_struct_prefix (struct die_info *die, struct dwarf2_cu *cu)
16531{
16532 struct attribute *attr;
16533 char *base;
16534
16535 if (die->tag != DW_TAG_class_type && die->tag != DW_TAG_interface_type
16536 && die->tag != DW_TAG_structure_type && die->tag != DW_TAG_union_type)
16537 return NULL;
16538
16539 attr = dwarf2_attr (die, DW_AT_name, cu);
16540 if (attr != NULL && DW_STRING (attr) != NULL)
16541 return NULL;
16542
16543 attr = dwarf2_attr (die, DW_AT_linkage_name, cu);
16544 if (attr == NULL)
16545 attr = dwarf2_attr (die, DW_AT_MIPS_linkage_name, cu);
16546 if (attr == NULL || DW_STRING (attr) == NULL)
16547 return NULL;
16548
16549 /* dwarf2_name had to be already called. */
16550 gdb_assert (DW_STRING_IS_CANONICAL (attr));
16551
16552 /* Strip the base name, keep any leading namespaces/classes. */
16553 base = strrchr (DW_STRING (attr), ':');
16554 if (base == NULL || base == DW_STRING (attr) || base[-1] != ':')
16555 return "";
16556
16557 return obsavestring (DW_STRING (attr), &base[-1] - DW_STRING (attr),
16558 &cu->objfile->objfile_obstack);
16559}
16560
fdde2d81 16561/* Return the name of the namespace/class that DIE is defined within,
0114d602 16562 or "" if we can't tell. The caller should not xfree the result.
fdde2d81 16563
0114d602
DJ
16564 For example, if we're within the method foo() in the following
16565 code:
16566
16567 namespace N {
16568 class C {
16569 void foo () {
16570 }
16571 };
16572 }
16573
16574 then determine_prefix on foo's die will return "N::C". */
fdde2d81 16575
0d5cff50 16576static const char *
e142c38c 16577determine_prefix (struct die_info *die, struct dwarf2_cu *cu)
63d06c5c 16578{
0114d602
DJ
16579 struct die_info *parent, *spec_die;
16580 struct dwarf2_cu *spec_cu;
16581 struct type *parent_type;
96408a79 16582 char *retval;
63d06c5c 16583
f55ee35c
JK
16584 if (cu->language != language_cplus && cu->language != language_java
16585 && cu->language != language_fortran)
0114d602
DJ
16586 return "";
16587
96408a79
SA
16588 retval = anonymous_struct_prefix (die, cu);
16589 if (retval)
16590 return retval;
16591
0114d602
DJ
16592 /* We have to be careful in the presence of DW_AT_specification.
16593 For example, with GCC 3.4, given the code
16594
16595 namespace N {
16596 void foo() {
16597 // Definition of N::foo.
16598 }
16599 }
16600
16601 then we'll have a tree of DIEs like this:
16602
16603 1: DW_TAG_compile_unit
16604 2: DW_TAG_namespace // N
16605 3: DW_TAG_subprogram // declaration of N::foo
16606 4: DW_TAG_subprogram // definition of N::foo
16607 DW_AT_specification // refers to die #3
16608
16609 Thus, when processing die #4, we have to pretend that we're in
16610 the context of its DW_AT_specification, namely the contex of die
16611 #3. */
16612 spec_cu = cu;
16613 spec_die = die_specification (die, &spec_cu);
16614 if (spec_die == NULL)
16615 parent = die->parent;
16616 else
63d06c5c 16617 {
0114d602
DJ
16618 parent = spec_die->parent;
16619 cu = spec_cu;
63d06c5c 16620 }
0114d602
DJ
16621
16622 if (parent == NULL)
16623 return "";
98bfdba5
PA
16624 else if (parent->building_fullname)
16625 {
16626 const char *name;
16627 const char *parent_name;
16628
16629 /* It has been seen on RealView 2.2 built binaries,
16630 DW_TAG_template_type_param types actually _defined_ as
16631 children of the parent class:
16632
16633 enum E {};
16634 template class <class Enum> Class{};
16635 Class<enum E> class_e;
16636
16637 1: DW_TAG_class_type (Class)
16638 2: DW_TAG_enumeration_type (E)
16639 3: DW_TAG_enumerator (enum1:0)
16640 3: DW_TAG_enumerator (enum2:1)
16641 ...
16642 2: DW_TAG_template_type_param
16643 DW_AT_type DW_FORM_ref_udata (E)
16644
16645 Besides being broken debug info, it can put GDB into an
16646 infinite loop. Consider:
16647
16648 When we're building the full name for Class<E>, we'll start
16649 at Class, and go look over its template type parameters,
16650 finding E. We'll then try to build the full name of E, and
16651 reach here. We're now trying to build the full name of E,
16652 and look over the parent DIE for containing scope. In the
16653 broken case, if we followed the parent DIE of E, we'd again
16654 find Class, and once again go look at its template type
16655 arguments, etc., etc. Simply don't consider such parent die
16656 as source-level parent of this die (it can't be, the language
16657 doesn't allow it), and break the loop here. */
16658 name = dwarf2_name (die, cu);
16659 parent_name = dwarf2_name (parent, cu);
16660 complaint (&symfile_complaints,
16661 _("template param type '%s' defined within parent '%s'"),
16662 name ? name : "<unknown>",
16663 parent_name ? parent_name : "<unknown>");
16664 return "";
16665 }
63d06c5c 16666 else
0114d602
DJ
16667 switch (parent->tag)
16668 {
63d06c5c 16669 case DW_TAG_namespace:
0114d602 16670 parent_type = read_type_die (parent, cu);
acebe513
UW
16671 /* GCC 4.0 and 4.1 had a bug (PR c++/28460) where they generated bogus
16672 DW_TAG_namespace DIEs with a name of "::" for the global namespace.
16673 Work around this problem here. */
16674 if (cu->language == language_cplus
16675 && strcmp (TYPE_TAG_NAME (parent_type), "::") == 0)
16676 return "";
0114d602
DJ
16677 /* We give a name to even anonymous namespaces. */
16678 return TYPE_TAG_NAME (parent_type);
63d06c5c 16679 case DW_TAG_class_type:
680b30c7 16680 case DW_TAG_interface_type:
63d06c5c 16681 case DW_TAG_structure_type:
0114d602 16682 case DW_TAG_union_type:
f55ee35c 16683 case DW_TAG_module:
0114d602
DJ
16684 parent_type = read_type_die (parent, cu);
16685 if (TYPE_TAG_NAME (parent_type) != NULL)
16686 return TYPE_TAG_NAME (parent_type);
16687 else
16688 /* An anonymous structure is only allowed non-static data
16689 members; no typedefs, no member functions, et cetera.
16690 So it does not need a prefix. */
16691 return "";
abc72ce4 16692 case DW_TAG_compile_unit:
95554aad 16693 case DW_TAG_partial_unit:
abc72ce4
DE
16694 /* gcc-4.5 -gdwarf-4 can drop the enclosing namespace. Cope. */
16695 if (cu->language == language_cplus
8b70b953 16696 && !VEC_empty (dwarf2_section_info_def, dwarf2_per_objfile->types)
abc72ce4
DE
16697 && die->child != NULL
16698 && (die->tag == DW_TAG_class_type
16699 || die->tag == DW_TAG_structure_type
16700 || die->tag == DW_TAG_union_type))
16701 {
16702 char *name = guess_full_die_structure_name (die, cu);
16703 if (name != NULL)
16704 return name;
16705 }
16706 return "";
63d06c5c 16707 default:
8176b9b8 16708 return determine_prefix (parent, cu);
63d06c5c 16709 }
63d06c5c
DC
16710}
16711
3e43a32a
MS
16712/* Return a newly-allocated string formed by concatenating PREFIX and SUFFIX
16713 with appropriate separator. If PREFIX or SUFFIX is NULL or empty, then
16714 simply copy the SUFFIX or PREFIX, respectively. If OBS is non-null, perform
16715 an obconcat, otherwise allocate storage for the result. The CU argument is
16716 used to determine the language and hence, the appropriate separator. */
987504bb 16717
f55ee35c 16718#define MAX_SEP_LEN 7 /* strlen ("__") + strlen ("_MOD_") */
63d06c5c
DC
16719
16720static char *
f55ee35c
JK
16721typename_concat (struct obstack *obs, const char *prefix, const char *suffix,
16722 int physname, struct dwarf2_cu *cu)
63d06c5c 16723{
f55ee35c 16724 const char *lead = "";
5c315b68 16725 const char *sep;
63d06c5c 16726
3e43a32a
MS
16727 if (suffix == NULL || suffix[0] == '\0'
16728 || prefix == NULL || prefix[0] == '\0')
987504bb
JJ
16729 sep = "";
16730 else if (cu->language == language_java)
16731 sep = ".";
f55ee35c
JK
16732 else if (cu->language == language_fortran && physname)
16733 {
16734 /* This is gfortran specific mangling. Normally DW_AT_linkage_name or
16735 DW_AT_MIPS_linkage_name is preferred and used instead. */
16736
16737 lead = "__";
16738 sep = "_MOD_";
16739 }
987504bb
JJ
16740 else
16741 sep = "::";
63d06c5c 16742
6dd47d34
DE
16743 if (prefix == NULL)
16744 prefix = "";
16745 if (suffix == NULL)
16746 suffix = "";
16747
987504bb
JJ
16748 if (obs == NULL)
16749 {
3e43a32a
MS
16750 char *retval
16751 = xmalloc (strlen (prefix) + MAX_SEP_LEN + strlen (suffix) + 1);
9a619af0 16752
f55ee35c
JK
16753 strcpy (retval, lead);
16754 strcat (retval, prefix);
6dd47d34
DE
16755 strcat (retval, sep);
16756 strcat (retval, suffix);
63d06c5c
DC
16757 return retval;
16758 }
987504bb
JJ
16759 else
16760 {
16761 /* We have an obstack. */
f55ee35c 16762 return obconcat (obs, lead, prefix, sep, suffix, (char *) NULL);
987504bb 16763 }
63d06c5c
DC
16764}
16765
c906108c
SS
16766/* Return sibling of die, NULL if no sibling. */
16767
f9aca02d 16768static struct die_info *
fba45db2 16769sibling_die (struct die_info *die)
c906108c 16770{
639d11d3 16771 return die->sibling;
c906108c
SS
16772}
16773
71c25dea
TT
16774/* Get name of a die, return NULL if not found. */
16775
16776static char *
16777dwarf2_canonicalize_name (char *name, struct dwarf2_cu *cu,
16778 struct obstack *obstack)
16779{
16780 if (name && cu->language == language_cplus)
16781 {
16782 char *canon_name = cp_canonicalize_string (name);
16783
16784 if (canon_name != NULL)
16785 {
16786 if (strcmp (canon_name, name) != 0)
16787 name = obsavestring (canon_name, strlen (canon_name),
16788 obstack);
16789 xfree (canon_name);
16790 }
16791 }
16792
16793 return name;
c906108c
SS
16794}
16795
9219021c
DC
16796/* Get name of a die, return NULL if not found. */
16797
16798static char *
e142c38c 16799dwarf2_name (struct die_info *die, struct dwarf2_cu *cu)
9219021c
DC
16800{
16801 struct attribute *attr;
16802
e142c38c 16803 attr = dwarf2_attr (die, DW_AT_name, cu);
53832f31
TT
16804 if ((!attr || !DW_STRING (attr))
16805 && die->tag != DW_TAG_class_type
16806 && die->tag != DW_TAG_interface_type
16807 && die->tag != DW_TAG_structure_type
16808 && die->tag != DW_TAG_union_type)
71c25dea
TT
16809 return NULL;
16810
16811 switch (die->tag)
16812 {
16813 case DW_TAG_compile_unit:
95554aad 16814 case DW_TAG_partial_unit:
71c25dea
TT
16815 /* Compilation units have a DW_AT_name that is a filename, not
16816 a source language identifier. */
16817 case DW_TAG_enumeration_type:
16818 case DW_TAG_enumerator:
16819 /* These tags always have simple identifiers already; no need
16820 to canonicalize them. */
16821 return DW_STRING (attr);
907af001 16822
418835cc
KS
16823 case DW_TAG_subprogram:
16824 /* Java constructors will all be named "<init>", so return
16825 the class name when we see this special case. */
16826 if (cu->language == language_java
16827 && DW_STRING (attr) != NULL
16828 && strcmp (DW_STRING (attr), "<init>") == 0)
16829 {
16830 struct dwarf2_cu *spec_cu = cu;
16831 struct die_info *spec_die;
16832
16833 /* GCJ will output '<init>' for Java constructor names.
16834 For this special case, return the name of the parent class. */
16835
16836 /* GCJ may output suprogram DIEs with AT_specification set.
16837 If so, use the name of the specified DIE. */
16838 spec_die = die_specification (die, &spec_cu);
16839 if (spec_die != NULL)
16840 return dwarf2_name (spec_die, spec_cu);
16841
16842 do
16843 {
16844 die = die->parent;
16845 if (die->tag == DW_TAG_class_type)
16846 return dwarf2_name (die, cu);
16847 }
95554aad
TT
16848 while (die->tag != DW_TAG_compile_unit
16849 && die->tag != DW_TAG_partial_unit);
418835cc 16850 }
907af001
UW
16851 break;
16852
16853 case DW_TAG_class_type:
16854 case DW_TAG_interface_type:
16855 case DW_TAG_structure_type:
16856 case DW_TAG_union_type:
16857 /* Some GCC versions emit spurious DW_AT_name attributes for unnamed
16858 structures or unions. These were of the form "._%d" in GCC 4.1,
16859 or simply "<anonymous struct>" or "<anonymous union>" in GCC 4.3
16860 and GCC 4.4. We work around this problem by ignoring these. */
53832f31
TT
16861 if (attr && DW_STRING (attr)
16862 && (strncmp (DW_STRING (attr), "._", 2) == 0
16863 || strncmp (DW_STRING (attr), "<anonymous", 10) == 0))
907af001 16864 return NULL;
53832f31
TT
16865
16866 /* GCC might emit a nameless typedef that has a linkage name. See
16867 http://gcc.gnu.org/bugzilla/show_bug.cgi?id=47510. */
16868 if (!attr || DW_STRING (attr) == NULL)
16869 {
df5c6c50 16870 char *demangled = NULL;
53832f31
TT
16871
16872 attr = dwarf2_attr (die, DW_AT_linkage_name, cu);
16873 if (attr == NULL)
16874 attr = dwarf2_attr (die, DW_AT_MIPS_linkage_name, cu);
16875
16876 if (attr == NULL || DW_STRING (attr) == NULL)
16877 return NULL;
16878
df5c6c50
JK
16879 /* Avoid demangling DW_STRING (attr) the second time on a second
16880 call for the same DIE. */
16881 if (!DW_STRING_IS_CANONICAL (attr))
16882 demangled = cplus_demangle (DW_STRING (attr), DMGL_TYPES);
53832f31
TT
16883
16884 if (demangled)
16885 {
96408a79
SA
16886 char *base;
16887
53832f31 16888 /* FIXME: we already did this for the partial symbol... */
96408a79
SA
16889 DW_STRING (attr) = obsavestring (demangled, strlen (demangled),
16890 &cu->objfile->objfile_obstack);
53832f31
TT
16891 DW_STRING_IS_CANONICAL (attr) = 1;
16892 xfree (demangled);
96408a79
SA
16893
16894 /* Strip any leading namespaces/classes, keep only the base name.
16895 DW_AT_name for named DIEs does not contain the prefixes. */
16896 base = strrchr (DW_STRING (attr), ':');
16897 if (base && base > DW_STRING (attr) && base[-1] == ':')
16898 return &base[1];
16899 else
16900 return DW_STRING (attr);
53832f31
TT
16901 }
16902 }
907af001
UW
16903 break;
16904
71c25dea 16905 default:
907af001
UW
16906 break;
16907 }
16908
16909 if (!DW_STRING_IS_CANONICAL (attr))
16910 {
16911 DW_STRING (attr)
16912 = dwarf2_canonicalize_name (DW_STRING (attr), cu,
16913 &cu->objfile->objfile_obstack);
16914 DW_STRING_IS_CANONICAL (attr) = 1;
71c25dea 16915 }
907af001 16916 return DW_STRING (attr);
9219021c
DC
16917}
16918
16919/* Return the die that this die in an extension of, or NULL if there
f2f0e013
DJ
16920 is none. *EXT_CU is the CU containing DIE on input, and the CU
16921 containing the return value on output. */
9219021c
DC
16922
16923static struct die_info *
f2f0e013 16924dwarf2_extension (struct die_info *die, struct dwarf2_cu **ext_cu)
9219021c
DC
16925{
16926 struct attribute *attr;
9219021c 16927
f2f0e013 16928 attr = dwarf2_attr (die, DW_AT_extension, *ext_cu);
9219021c
DC
16929 if (attr == NULL)
16930 return NULL;
16931
f2f0e013 16932 return follow_die_ref (die, attr, ext_cu);
9219021c
DC
16933}
16934
c906108c
SS
16935/* Convert a DIE tag into its string name. */
16936
f39c6ffd 16937static const char *
aa1ee363 16938dwarf_tag_name (unsigned tag)
c906108c 16939{
f39c6ffd
TT
16940 const char *name = get_DW_TAG_name (tag);
16941
16942 if (name == NULL)
16943 return "DW_TAG_<unknown>";
16944
16945 return name;
c906108c
SS
16946}
16947
16948/* Convert a DWARF attribute code into its string name. */
16949
f39c6ffd 16950static const char *
aa1ee363 16951dwarf_attr_name (unsigned attr)
c906108c 16952{
f39c6ffd
TT
16953 const char *name;
16954
c764a876 16955#ifdef MIPS /* collides with DW_AT_HP_block_index */
f39c6ffd
TT
16956 if (attr == DW_AT_MIPS_fde)
16957 return "DW_AT_MIPS_fde";
16958#else
16959 if (attr == DW_AT_HP_block_index)
16960 return "DW_AT_HP_block_index";
c764a876 16961#endif
f39c6ffd
TT
16962
16963 name = get_DW_AT_name (attr);
16964
16965 if (name == NULL)
16966 return "DW_AT_<unknown>";
16967
16968 return name;
c906108c
SS
16969}
16970
16971/* Convert a DWARF value form code into its string name. */
16972
f39c6ffd 16973static const char *
aa1ee363 16974dwarf_form_name (unsigned form)
c906108c 16975{
f39c6ffd
TT
16976 const char *name = get_DW_FORM_name (form);
16977
16978 if (name == NULL)
16979 return "DW_FORM_<unknown>";
16980
16981 return name;
c906108c
SS
16982}
16983
16984static char *
fba45db2 16985dwarf_bool_name (unsigned mybool)
c906108c
SS
16986{
16987 if (mybool)
16988 return "TRUE";
16989 else
16990 return "FALSE";
16991}
16992
16993/* Convert a DWARF type code into its string name. */
16994
f39c6ffd 16995static const char *
aa1ee363 16996dwarf_type_encoding_name (unsigned enc)
c906108c 16997{
f39c6ffd 16998 const char *name = get_DW_ATE_name (enc);
c906108c 16999
f39c6ffd
TT
17000 if (name == NULL)
17001 return "DW_ATE_<unknown>";
c906108c 17002
f39c6ffd 17003 return name;
c906108c 17004}
c906108c 17005
f9aca02d 17006static void
d97bc12b 17007dump_die_shallow (struct ui_file *f, int indent, struct die_info *die)
c906108c
SS
17008{
17009 unsigned int i;
17010
d97bc12b
DE
17011 print_spaces (indent, f);
17012 fprintf_unfiltered (f, "Die: %s (abbrev %d, offset 0x%x)\n",
b64f50a1 17013 dwarf_tag_name (die->tag), die->abbrev, die->offset.sect_off);
d97bc12b
DE
17014
17015 if (die->parent != NULL)
17016 {
17017 print_spaces (indent, f);
17018 fprintf_unfiltered (f, " parent at offset: 0x%x\n",
b64f50a1 17019 die->parent->offset.sect_off);
d97bc12b
DE
17020 }
17021
17022 print_spaces (indent, f);
17023 fprintf_unfiltered (f, " has children: %s\n",
639d11d3 17024 dwarf_bool_name (die->child != NULL));
c906108c 17025
d97bc12b
DE
17026 print_spaces (indent, f);
17027 fprintf_unfiltered (f, " attributes:\n");
17028
c906108c
SS
17029 for (i = 0; i < die->num_attrs; ++i)
17030 {
d97bc12b
DE
17031 print_spaces (indent, f);
17032 fprintf_unfiltered (f, " %s (%s) ",
c906108c
SS
17033 dwarf_attr_name (die->attrs[i].name),
17034 dwarf_form_name (die->attrs[i].form));
d97bc12b 17035
c906108c
SS
17036 switch (die->attrs[i].form)
17037 {
c906108c 17038 case DW_FORM_addr:
3019eac3 17039 case DW_FORM_GNU_addr_index:
d97bc12b 17040 fprintf_unfiltered (f, "address: ");
5af949e3 17041 fputs_filtered (hex_string (DW_ADDR (&die->attrs[i])), f);
c906108c
SS
17042 break;
17043 case DW_FORM_block2:
17044 case DW_FORM_block4:
17045 case DW_FORM_block:
17046 case DW_FORM_block1:
56eb65bd
SP
17047 fprintf_unfiltered (f, "block: size %s",
17048 pulongest (DW_BLOCK (&die->attrs[i])->size));
c906108c 17049 break;
2dc7f7b3 17050 case DW_FORM_exprloc:
56eb65bd
SP
17051 fprintf_unfiltered (f, "expression: size %s",
17052 pulongest (DW_BLOCK (&die->attrs[i])->size));
2dc7f7b3 17053 break;
4568ecf9
DE
17054 case DW_FORM_ref_addr:
17055 fprintf_unfiltered (f, "ref address: ");
17056 fputs_filtered (hex_string (DW_UNSND (&die->attrs[i])), f);
17057 break;
36586728
TT
17058 case DW_FORM_GNU_ref_alt:
17059 fprintf_unfiltered (f, "alt ref address: ");
17060 fputs_filtered (hex_string (DW_UNSND (&die->attrs[i])), f);
17061 break;
10b3939b
DJ
17062 case DW_FORM_ref1:
17063 case DW_FORM_ref2:
17064 case DW_FORM_ref4:
4568ecf9
DE
17065 case DW_FORM_ref8:
17066 case DW_FORM_ref_udata:
d97bc12b 17067 fprintf_unfiltered (f, "constant ref: 0x%lx (adjusted)",
4568ecf9 17068 (long) (DW_UNSND (&die->attrs[i])));
10b3939b 17069 break;
c906108c
SS
17070 case DW_FORM_data1:
17071 case DW_FORM_data2:
17072 case DW_FORM_data4:
ce5d95e1 17073 case DW_FORM_data8:
c906108c
SS
17074 case DW_FORM_udata:
17075 case DW_FORM_sdata:
43bbcdc2
PH
17076 fprintf_unfiltered (f, "constant: %s",
17077 pulongest (DW_UNSND (&die->attrs[i])));
c906108c 17078 break;
2dc7f7b3
TT
17079 case DW_FORM_sec_offset:
17080 fprintf_unfiltered (f, "section offset: %s",
17081 pulongest (DW_UNSND (&die->attrs[i])));
17082 break;
55f1336d 17083 case DW_FORM_ref_sig8:
348e048f
DE
17084 if (DW_SIGNATURED_TYPE (&die->attrs[i]) != NULL)
17085 fprintf_unfiltered (f, "signatured type, offset: 0x%x",
b64f50a1 17086 DW_SIGNATURED_TYPE (&die->attrs[i])->per_cu.offset.sect_off);
348e048f
DE
17087 else
17088 fprintf_unfiltered (f, "signatured type, offset: unknown");
17089 break;
c906108c 17090 case DW_FORM_string:
4bdf3d34 17091 case DW_FORM_strp:
3019eac3 17092 case DW_FORM_GNU_str_index:
36586728 17093 case DW_FORM_GNU_strp_alt:
8285870a 17094 fprintf_unfiltered (f, "string: \"%s\" (%s canonicalized)",
c906108c 17095 DW_STRING (&die->attrs[i])
8285870a
JK
17096 ? DW_STRING (&die->attrs[i]) : "",
17097 DW_STRING_IS_CANONICAL (&die->attrs[i]) ? "is" : "not");
c906108c
SS
17098 break;
17099 case DW_FORM_flag:
17100 if (DW_UNSND (&die->attrs[i]))
d97bc12b 17101 fprintf_unfiltered (f, "flag: TRUE");
c906108c 17102 else
d97bc12b 17103 fprintf_unfiltered (f, "flag: FALSE");
c906108c 17104 break;
2dc7f7b3
TT
17105 case DW_FORM_flag_present:
17106 fprintf_unfiltered (f, "flag: TRUE");
17107 break;
a8329558 17108 case DW_FORM_indirect:
0963b4bd
MS
17109 /* The reader will have reduced the indirect form to
17110 the "base form" so this form should not occur. */
3e43a32a
MS
17111 fprintf_unfiltered (f,
17112 "unexpected attribute form: DW_FORM_indirect");
a8329558 17113 break;
c906108c 17114 default:
d97bc12b 17115 fprintf_unfiltered (f, "unsupported attribute form: %d.",
c5aa993b 17116 die->attrs[i].form);
d97bc12b 17117 break;
c906108c 17118 }
d97bc12b 17119 fprintf_unfiltered (f, "\n");
c906108c
SS
17120 }
17121}
17122
f9aca02d 17123static void
d97bc12b 17124dump_die_for_error (struct die_info *die)
c906108c 17125{
d97bc12b
DE
17126 dump_die_shallow (gdb_stderr, 0, die);
17127}
17128
17129static void
17130dump_die_1 (struct ui_file *f, int level, int max_level, struct die_info *die)
17131{
17132 int indent = level * 4;
17133
17134 gdb_assert (die != NULL);
17135
17136 if (level >= max_level)
17137 return;
17138
17139 dump_die_shallow (f, indent, die);
17140
17141 if (die->child != NULL)
c906108c 17142 {
d97bc12b
DE
17143 print_spaces (indent, f);
17144 fprintf_unfiltered (f, " Children:");
17145 if (level + 1 < max_level)
17146 {
17147 fprintf_unfiltered (f, "\n");
17148 dump_die_1 (f, level + 1, max_level, die->child);
17149 }
17150 else
17151 {
3e43a32a
MS
17152 fprintf_unfiltered (f,
17153 " [not printed, max nesting level reached]\n");
d97bc12b
DE
17154 }
17155 }
17156
17157 if (die->sibling != NULL && level > 0)
17158 {
17159 dump_die_1 (f, level, max_level, die->sibling);
c906108c
SS
17160 }
17161}
17162
d97bc12b
DE
17163/* This is called from the pdie macro in gdbinit.in.
17164 It's not static so gcc will keep a copy callable from gdb. */
17165
17166void
17167dump_die (struct die_info *die, int max_level)
17168{
17169 dump_die_1 (gdb_stdlog, 0, max_level, die);
17170}
17171
f9aca02d 17172static void
51545339 17173store_in_ref_table (struct die_info *die, struct dwarf2_cu *cu)
c906108c 17174{
51545339 17175 void **slot;
c906108c 17176
b64f50a1
JK
17177 slot = htab_find_slot_with_hash (cu->die_hash, die, die->offset.sect_off,
17178 INSERT);
51545339
DJ
17179
17180 *slot = die;
c906108c
SS
17181}
17182
b64f50a1
JK
17183/* DW_ADDR is always stored already as sect_offset; despite for the forms
17184 besides DW_FORM_ref_addr it is stored as cu_offset in the DWARF file. */
17185
93311388
DE
17186static int
17187is_ref_attr (struct attribute *attr)
c906108c 17188{
c906108c
SS
17189 switch (attr->form)
17190 {
17191 case DW_FORM_ref_addr:
c906108c
SS
17192 case DW_FORM_ref1:
17193 case DW_FORM_ref2:
17194 case DW_FORM_ref4:
613e1657 17195 case DW_FORM_ref8:
c906108c 17196 case DW_FORM_ref_udata:
36586728 17197 case DW_FORM_GNU_ref_alt:
93311388 17198 return 1;
c906108c 17199 default:
93311388 17200 return 0;
c906108c 17201 }
93311388
DE
17202}
17203
b64f50a1
JK
17204/* Return DIE offset of ATTR. Return 0 with complaint if ATTR is not of the
17205 required kind. */
17206
17207static sect_offset
93311388
DE
17208dwarf2_get_ref_die_offset (struct attribute *attr)
17209{
4568ecf9 17210 sect_offset retval = { DW_UNSND (attr) };
b64f50a1 17211
93311388 17212 if (is_ref_attr (attr))
b64f50a1 17213 return retval;
93311388 17214
b64f50a1 17215 retval.sect_off = 0;
93311388
DE
17216 complaint (&symfile_complaints,
17217 _("unsupported die ref attribute form: '%s'"),
17218 dwarf_form_name (attr->form));
b64f50a1 17219 return retval;
c906108c
SS
17220}
17221
43bbcdc2
PH
17222/* Return the constant value held by ATTR. Return DEFAULT_VALUE if
17223 * the value held by the attribute is not constant. */
a02abb62 17224
43bbcdc2 17225static LONGEST
a02abb62
JB
17226dwarf2_get_attr_constant_value (struct attribute *attr, int default_value)
17227{
17228 if (attr->form == DW_FORM_sdata)
17229 return DW_SND (attr);
17230 else if (attr->form == DW_FORM_udata
17231 || attr->form == DW_FORM_data1
17232 || attr->form == DW_FORM_data2
17233 || attr->form == DW_FORM_data4
17234 || attr->form == DW_FORM_data8)
17235 return DW_UNSND (attr);
17236 else
17237 {
3e43a32a
MS
17238 complaint (&symfile_complaints,
17239 _("Attribute value is not a constant (%s)"),
a02abb62
JB
17240 dwarf_form_name (attr->form));
17241 return default_value;
17242 }
17243}
17244
348e048f
DE
17245/* Follow reference or signature attribute ATTR of SRC_DIE.
17246 On entry *REF_CU is the CU of SRC_DIE.
17247 On exit *REF_CU is the CU of the result. */
17248
17249static struct die_info *
17250follow_die_ref_or_sig (struct die_info *src_die, struct attribute *attr,
17251 struct dwarf2_cu **ref_cu)
17252{
17253 struct die_info *die;
17254
17255 if (is_ref_attr (attr))
17256 die = follow_die_ref (src_die, attr, ref_cu);
55f1336d 17257 else if (attr->form == DW_FORM_ref_sig8)
348e048f
DE
17258 die = follow_die_sig (src_die, attr, ref_cu);
17259 else
17260 {
17261 dump_die_for_error (src_die);
17262 error (_("Dwarf Error: Expected reference attribute [in module %s]"),
17263 (*ref_cu)->objfile->name);
17264 }
17265
17266 return die;
03dd20cc
DJ
17267}
17268
5c631832 17269/* Follow reference OFFSET.
673bfd45
DE
17270 On entry *REF_CU is the CU of the source die referencing OFFSET.
17271 On exit *REF_CU is the CU of the result.
17272 Returns NULL if OFFSET is invalid. */
f504f079 17273
f9aca02d 17274static struct die_info *
36586728
TT
17275follow_die_offset (sect_offset offset, int offset_in_dwz,
17276 struct dwarf2_cu **ref_cu)
c906108c 17277{
10b3939b 17278 struct die_info temp_die;
f2f0e013 17279 struct dwarf2_cu *target_cu, *cu = *ref_cu;
10b3939b 17280
348e048f
DE
17281 gdb_assert (cu->per_cu != NULL);
17282
98bfdba5
PA
17283 target_cu = cu;
17284
3019eac3 17285 if (cu->per_cu->is_debug_types)
348e048f
DE
17286 {
17287 /* .debug_types CUs cannot reference anything outside their CU.
17288 If they need to, they have to reference a signatured type via
55f1336d 17289 DW_FORM_ref_sig8. */
348e048f 17290 if (! offset_in_cu_p (&cu->header, offset))
5c631832 17291 return NULL;
348e048f 17292 }
36586728
TT
17293 else if (offset_in_dwz != cu->per_cu->is_dwz
17294 || ! offset_in_cu_p (&cu->header, offset))
10b3939b
DJ
17295 {
17296 struct dwarf2_per_cu_data *per_cu;
9a619af0 17297
36586728
TT
17298 per_cu = dwarf2_find_containing_comp_unit (offset, offset_in_dwz,
17299 cu->objfile);
03dd20cc
DJ
17300
17301 /* If necessary, add it to the queue and load its DIEs. */
95554aad
TT
17302 if (maybe_queue_comp_unit (cu, per_cu, cu->language))
17303 load_full_comp_unit (per_cu, cu->language);
03dd20cc 17304
10b3939b
DJ
17305 target_cu = per_cu->cu;
17306 }
98bfdba5
PA
17307 else if (cu->dies == NULL)
17308 {
17309 /* We're loading full DIEs during partial symbol reading. */
17310 gdb_assert (dwarf2_per_objfile->reading_partial_symbols);
95554aad 17311 load_full_comp_unit (cu->per_cu, language_minimal);
98bfdba5 17312 }
c906108c 17313
f2f0e013 17314 *ref_cu = target_cu;
51545339 17315 temp_die.offset = offset;
b64f50a1 17316 return htab_find_with_hash (target_cu->die_hash, &temp_die, offset.sect_off);
5c631832 17317}
10b3939b 17318
5c631832
JK
17319/* Follow reference attribute ATTR of SRC_DIE.
17320 On entry *REF_CU is the CU of SRC_DIE.
17321 On exit *REF_CU is the CU of the result. */
17322
17323static struct die_info *
17324follow_die_ref (struct die_info *src_die, struct attribute *attr,
17325 struct dwarf2_cu **ref_cu)
17326{
b64f50a1 17327 sect_offset offset = dwarf2_get_ref_die_offset (attr);
5c631832
JK
17328 struct dwarf2_cu *cu = *ref_cu;
17329 struct die_info *die;
17330
36586728
TT
17331 die = follow_die_offset (offset,
17332 (attr->form == DW_FORM_GNU_ref_alt
17333 || cu->per_cu->is_dwz),
17334 ref_cu);
5c631832
JK
17335 if (!die)
17336 error (_("Dwarf Error: Cannot find DIE at 0x%x referenced from DIE "
17337 "at 0x%x [in module %s]"),
b64f50a1 17338 offset.sect_off, src_die->offset.sect_off, cu->objfile->name);
348e048f 17339
5c631832
JK
17340 return die;
17341}
17342
d83e736b
JK
17343/* Return DWARF block referenced by DW_AT_location of DIE at OFFSET at PER_CU.
17344 Returned value is intended for DW_OP_call*. Returned
17345 dwarf2_locexpr_baton->data has lifetime of PER_CU->OBJFILE. */
5c631832
JK
17346
17347struct dwarf2_locexpr_baton
b64f50a1 17348dwarf2_fetch_die_location_block (cu_offset offset_in_cu,
8cf6f0b1
TT
17349 struct dwarf2_per_cu_data *per_cu,
17350 CORE_ADDR (*get_frame_pc) (void *baton),
17351 void *baton)
5c631832 17352{
b64f50a1 17353 sect_offset offset = { per_cu->offset.sect_off + offset_in_cu.cu_off };
918dd910 17354 struct dwarf2_cu *cu;
5c631832
JK
17355 struct die_info *die;
17356 struct attribute *attr;
17357 struct dwarf2_locexpr_baton retval;
17358
8cf6f0b1
TT
17359 dw2_setup (per_cu->objfile);
17360
918dd910
JK
17361 if (per_cu->cu == NULL)
17362 load_cu (per_cu);
17363 cu = per_cu->cu;
17364
36586728 17365 die = follow_die_offset (offset, per_cu->is_dwz, &cu);
5c631832
JK
17366 if (!die)
17367 error (_("Dwarf Error: Cannot find DIE at 0x%x referenced in module %s"),
b64f50a1 17368 offset.sect_off, per_cu->objfile->name);
5c631832
JK
17369
17370 attr = dwarf2_attr (die, DW_AT_location, cu);
17371 if (!attr)
17372 {
e103e986
JK
17373 /* DWARF: "If there is no such attribute, then there is no effect.".
17374 DATA is ignored if SIZE is 0. */
5c631832 17375
e103e986 17376 retval.data = NULL;
5c631832
JK
17377 retval.size = 0;
17378 }
8cf6f0b1
TT
17379 else if (attr_form_is_section_offset (attr))
17380 {
17381 struct dwarf2_loclist_baton loclist_baton;
17382 CORE_ADDR pc = (*get_frame_pc) (baton);
17383 size_t size;
17384
17385 fill_in_loclist_baton (cu, &loclist_baton, attr);
17386
17387 retval.data = dwarf2_find_location_expression (&loclist_baton,
17388 &size, pc);
17389 retval.size = size;
17390 }
5c631832
JK
17391 else
17392 {
17393 if (!attr_form_is_block (attr))
17394 error (_("Dwarf Error: DIE at 0x%x referenced in module %s "
17395 "is neither DW_FORM_block* nor DW_FORM_exprloc"),
b64f50a1 17396 offset.sect_off, per_cu->objfile->name);
5c631832
JK
17397
17398 retval.data = DW_BLOCK (attr)->data;
17399 retval.size = DW_BLOCK (attr)->size;
17400 }
17401 retval.per_cu = cu->per_cu;
918dd910 17402
918dd910
JK
17403 age_cached_comp_units ();
17404
5c631832 17405 return retval;
348e048f
DE
17406}
17407
8a9b8146
TT
17408/* Return the type of the DIE at DIE_OFFSET in the CU named by
17409 PER_CU. */
17410
17411struct type *
b64f50a1 17412dwarf2_get_die_type (cu_offset die_offset,
8a9b8146
TT
17413 struct dwarf2_per_cu_data *per_cu)
17414{
b64f50a1
JK
17415 sect_offset die_offset_sect;
17416
8a9b8146 17417 dw2_setup (per_cu->objfile);
b64f50a1
JK
17418
17419 die_offset_sect.sect_off = per_cu->offset.sect_off + die_offset.cu_off;
17420 return get_die_type_at_offset (die_offset_sect, per_cu);
8a9b8146
TT
17421}
17422
348e048f
DE
17423/* Follow the signature attribute ATTR in SRC_DIE.
17424 On entry *REF_CU is the CU of SRC_DIE.
17425 On exit *REF_CU is the CU of the result. */
17426
17427static struct die_info *
17428follow_die_sig (struct die_info *src_die, struct attribute *attr,
17429 struct dwarf2_cu **ref_cu)
17430{
17431 struct objfile *objfile = (*ref_cu)->objfile;
17432 struct die_info temp_die;
17433 struct signatured_type *sig_type = DW_SIGNATURED_TYPE (attr);
17434 struct dwarf2_cu *sig_cu;
17435 struct die_info *die;
17436
17437 /* sig_type will be NULL if the signatured type is missing from
17438 the debug info. */
17439 if (sig_type == NULL)
17440 error (_("Dwarf Error: Cannot find signatured DIE referenced from DIE "
17441 "at 0x%x [in module %s]"),
b64f50a1 17442 src_die->offset.sect_off, objfile->name);
348e048f
DE
17443
17444 /* If necessary, add it to the queue and load its DIEs. */
17445
95554aad 17446 if (maybe_queue_comp_unit (*ref_cu, &sig_type->per_cu, language_minimal))
a0f42c21 17447 read_signatured_type (sig_type);
348e048f
DE
17448
17449 gdb_assert (sig_type->per_cu.cu != NULL);
17450
17451 sig_cu = sig_type->per_cu.cu;
3019eac3
DE
17452 gdb_assert (sig_type->type_offset_in_section.sect_off != 0);
17453 temp_die.offset = sig_type->type_offset_in_section;
b64f50a1
JK
17454 die = htab_find_with_hash (sig_cu->die_hash, &temp_die,
17455 temp_die.offset.sect_off);
348e048f
DE
17456 if (die)
17457 {
17458 *ref_cu = sig_cu;
17459 return die;
17460 }
17461
3e43a32a
MS
17462 error (_("Dwarf Error: Cannot find signatured DIE at 0x%x referenced "
17463 "from DIE at 0x%x [in module %s]"),
b64f50a1 17464 temp_die.offset.sect_off, src_die->offset.sect_off, objfile->name);
348e048f
DE
17465}
17466
17467/* Given an offset of a signatured type, return its signatured_type. */
17468
17469static struct signatured_type *
8b70b953
TT
17470lookup_signatured_type_at_offset (struct objfile *objfile,
17471 struct dwarf2_section_info *section,
b64f50a1 17472 sect_offset offset)
348e048f 17473{
b64f50a1 17474 gdb_byte *info_ptr = section->buffer + offset.sect_off;
348e048f
DE
17475 unsigned int length, initial_length_size;
17476 unsigned int sig_offset;
52dc124a 17477 struct signatured_type find_entry, *sig_type;
348e048f
DE
17478
17479 length = read_initial_length (objfile->obfd, info_ptr, &initial_length_size);
17480 sig_offset = (initial_length_size
17481 + 2 /*version*/
17482 + (initial_length_size == 4 ? 4 : 8) /*debug_abbrev_offset*/
17483 + 1 /*address_size*/);
17484 find_entry.signature = bfd_get_64 (objfile->obfd, info_ptr + sig_offset);
52dc124a 17485 sig_type = htab_find (dwarf2_per_objfile->signatured_types, &find_entry);
348e048f
DE
17486
17487 /* This is only used to lookup previously recorded types.
17488 If we didn't find it, it's our bug. */
52dc124a
DE
17489 gdb_assert (sig_type != NULL);
17490 gdb_assert (offset.sect_off == sig_type->per_cu.offset.sect_off);
348e048f 17491
52dc124a 17492 return sig_type;
348e048f
DE
17493}
17494
e5fe5e75 17495/* Load the DIEs associated with type unit PER_CU into memory. */
348e048f
DE
17496
17497static void
e5fe5e75 17498load_full_type_unit (struct dwarf2_per_cu_data *per_cu)
348e048f 17499{
52dc124a 17500 struct signatured_type *sig_type;
348e048f 17501
f4dc4d17
DE
17502 /* Caller is responsible for ensuring type_unit_groups don't get here. */
17503 gdb_assert (! IS_TYPE_UNIT_GROUP (per_cu));
17504
6721b2ec
DE
17505 /* We have the per_cu, but we need the signatured_type.
17506 Fortunately this is an easy translation. */
17507 gdb_assert (per_cu->is_debug_types);
17508 sig_type = (struct signatured_type *) per_cu;
348e048f 17509
6721b2ec 17510 gdb_assert (per_cu->cu == NULL);
348e048f 17511
52dc124a 17512 read_signatured_type (sig_type);
348e048f 17513
6721b2ec 17514 gdb_assert (per_cu->cu != NULL);
348e048f
DE
17515}
17516
dee91e82
DE
17517/* die_reader_func for read_signatured_type.
17518 This is identical to load_full_comp_unit_reader,
17519 but is kept separate for now. */
348e048f
DE
17520
17521static void
dee91e82
DE
17522read_signatured_type_reader (const struct die_reader_specs *reader,
17523 gdb_byte *info_ptr,
17524 struct die_info *comp_unit_die,
17525 int has_children,
17526 void *data)
348e048f 17527{
dee91e82 17528 struct dwarf2_cu *cu = reader->cu;
348e048f 17529
dee91e82
DE
17530 gdb_assert (cu->die_hash == NULL);
17531 cu->die_hash =
17532 htab_create_alloc_ex (cu->header.length / 12,
17533 die_hash,
17534 die_eq,
17535 NULL,
17536 &cu->comp_unit_obstack,
17537 hashtab_obstack_allocate,
17538 dummy_obstack_deallocate);
348e048f 17539
dee91e82
DE
17540 if (has_children)
17541 comp_unit_die->child = read_die_and_siblings (reader, info_ptr,
17542 &info_ptr, comp_unit_die);
17543 cu->dies = comp_unit_die;
17544 /* comp_unit_die is not stored in die_hash, no need. */
348e048f
DE
17545
17546 /* We try not to read any attributes in this function, because not
9cdd5dbd 17547 all CUs needed for references have been loaded yet, and symbol
348e048f 17548 table processing isn't initialized. But we have to set the CU language,
dee91e82
DE
17549 or we won't be able to build types correctly.
17550 Similarly, if we do not read the producer, we can not apply
17551 producer-specific interpretation. */
95554aad 17552 prepare_one_comp_unit (cu, cu->dies, language_minimal);
dee91e82 17553}
348e048f 17554
3019eac3
DE
17555/* Read in a signatured type and build its CU and DIEs.
17556 If the type is a stub for the real type in a DWO file,
17557 read in the real type from the DWO file as well. */
dee91e82
DE
17558
17559static void
17560read_signatured_type (struct signatured_type *sig_type)
17561{
17562 struct dwarf2_per_cu_data *per_cu = &sig_type->per_cu;
348e048f 17563
3019eac3 17564 gdb_assert (per_cu->is_debug_types);
dee91e82 17565 gdb_assert (per_cu->cu == NULL);
348e048f 17566
f4dc4d17
DE
17567 init_cutu_and_read_dies (per_cu, NULL, 0, 1,
17568 read_signatured_type_reader, NULL);
c906108c
SS
17569}
17570
c906108c
SS
17571/* Decode simple location descriptions.
17572 Given a pointer to a dwarf block that defines a location, compute
17573 the location and return the value.
17574
4cecd739
DJ
17575 NOTE drow/2003-11-18: This function is called in two situations
17576 now: for the address of static or global variables (partial symbols
17577 only) and for offsets into structures which are expected to be
17578 (more or less) constant. The partial symbol case should go away,
17579 and only the constant case should remain. That will let this
17580 function complain more accurately. A few special modes are allowed
17581 without complaint for global variables (for instance, global
17582 register values and thread-local values).
c906108c
SS
17583
17584 A location description containing no operations indicates that the
4cecd739 17585 object is optimized out. The return value is 0 for that case.
6b992462
DJ
17586 FIXME drow/2003-11-16: No callers check for this case any more; soon all
17587 callers will only want a very basic result and this can become a
21ae7a4d
JK
17588 complaint.
17589
17590 Note that stack[0] is unused except as a default error return. */
c906108c
SS
17591
17592static CORE_ADDR
e7c27a73 17593decode_locdesc (struct dwarf_block *blk, struct dwarf2_cu *cu)
c906108c 17594{
e7c27a73 17595 struct objfile *objfile = cu->objfile;
56eb65bd
SP
17596 size_t i;
17597 size_t size = blk->size;
21ae7a4d
JK
17598 gdb_byte *data = blk->data;
17599 CORE_ADDR stack[64];
17600 int stacki;
17601 unsigned int bytes_read, unsnd;
17602 gdb_byte op;
c906108c 17603
21ae7a4d
JK
17604 i = 0;
17605 stacki = 0;
17606 stack[stacki] = 0;
17607 stack[++stacki] = 0;
17608
17609 while (i < size)
17610 {
17611 op = data[i++];
17612 switch (op)
17613 {
17614 case DW_OP_lit0:
17615 case DW_OP_lit1:
17616 case DW_OP_lit2:
17617 case DW_OP_lit3:
17618 case DW_OP_lit4:
17619 case DW_OP_lit5:
17620 case DW_OP_lit6:
17621 case DW_OP_lit7:
17622 case DW_OP_lit8:
17623 case DW_OP_lit9:
17624 case DW_OP_lit10:
17625 case DW_OP_lit11:
17626 case DW_OP_lit12:
17627 case DW_OP_lit13:
17628 case DW_OP_lit14:
17629 case DW_OP_lit15:
17630 case DW_OP_lit16:
17631 case DW_OP_lit17:
17632 case DW_OP_lit18:
17633 case DW_OP_lit19:
17634 case DW_OP_lit20:
17635 case DW_OP_lit21:
17636 case DW_OP_lit22:
17637 case DW_OP_lit23:
17638 case DW_OP_lit24:
17639 case DW_OP_lit25:
17640 case DW_OP_lit26:
17641 case DW_OP_lit27:
17642 case DW_OP_lit28:
17643 case DW_OP_lit29:
17644 case DW_OP_lit30:
17645 case DW_OP_lit31:
17646 stack[++stacki] = op - DW_OP_lit0;
17647 break;
f1bea926 17648
21ae7a4d
JK
17649 case DW_OP_reg0:
17650 case DW_OP_reg1:
17651 case DW_OP_reg2:
17652 case DW_OP_reg3:
17653 case DW_OP_reg4:
17654 case DW_OP_reg5:
17655 case DW_OP_reg6:
17656 case DW_OP_reg7:
17657 case DW_OP_reg8:
17658 case DW_OP_reg9:
17659 case DW_OP_reg10:
17660 case DW_OP_reg11:
17661 case DW_OP_reg12:
17662 case DW_OP_reg13:
17663 case DW_OP_reg14:
17664 case DW_OP_reg15:
17665 case DW_OP_reg16:
17666 case DW_OP_reg17:
17667 case DW_OP_reg18:
17668 case DW_OP_reg19:
17669 case DW_OP_reg20:
17670 case DW_OP_reg21:
17671 case DW_OP_reg22:
17672 case DW_OP_reg23:
17673 case DW_OP_reg24:
17674 case DW_OP_reg25:
17675 case DW_OP_reg26:
17676 case DW_OP_reg27:
17677 case DW_OP_reg28:
17678 case DW_OP_reg29:
17679 case DW_OP_reg30:
17680 case DW_OP_reg31:
17681 stack[++stacki] = op - DW_OP_reg0;
17682 if (i < size)
17683 dwarf2_complex_location_expr_complaint ();
17684 break;
c906108c 17685
21ae7a4d
JK
17686 case DW_OP_regx:
17687 unsnd = read_unsigned_leb128 (NULL, (data + i), &bytes_read);
17688 i += bytes_read;
17689 stack[++stacki] = unsnd;
17690 if (i < size)
17691 dwarf2_complex_location_expr_complaint ();
17692 break;
c906108c 17693
21ae7a4d
JK
17694 case DW_OP_addr:
17695 stack[++stacki] = read_address (objfile->obfd, &data[i],
17696 cu, &bytes_read);
17697 i += bytes_read;
17698 break;
d53d4ac5 17699
21ae7a4d
JK
17700 case DW_OP_const1u:
17701 stack[++stacki] = read_1_byte (objfile->obfd, &data[i]);
17702 i += 1;
17703 break;
17704
17705 case DW_OP_const1s:
17706 stack[++stacki] = read_1_signed_byte (objfile->obfd, &data[i]);
17707 i += 1;
17708 break;
17709
17710 case DW_OP_const2u:
17711 stack[++stacki] = read_2_bytes (objfile->obfd, &data[i]);
17712 i += 2;
17713 break;
17714
17715 case DW_OP_const2s:
17716 stack[++stacki] = read_2_signed_bytes (objfile->obfd, &data[i]);
17717 i += 2;
17718 break;
d53d4ac5 17719
21ae7a4d
JK
17720 case DW_OP_const4u:
17721 stack[++stacki] = read_4_bytes (objfile->obfd, &data[i]);
17722 i += 4;
17723 break;
17724
17725 case DW_OP_const4s:
17726 stack[++stacki] = read_4_signed_bytes (objfile->obfd, &data[i]);
17727 i += 4;
17728 break;
17729
585861ea
JK
17730 case DW_OP_const8u:
17731 stack[++stacki] = read_8_bytes (objfile->obfd, &data[i]);
17732 i += 8;
17733 break;
17734
21ae7a4d
JK
17735 case DW_OP_constu:
17736 stack[++stacki] = read_unsigned_leb128 (NULL, (data + i),
17737 &bytes_read);
17738 i += bytes_read;
17739 break;
17740
17741 case DW_OP_consts:
17742 stack[++stacki] = read_signed_leb128 (NULL, (data + i), &bytes_read);
17743 i += bytes_read;
17744 break;
17745
17746 case DW_OP_dup:
17747 stack[stacki + 1] = stack[stacki];
17748 stacki++;
17749 break;
17750
17751 case DW_OP_plus:
17752 stack[stacki - 1] += stack[stacki];
17753 stacki--;
17754 break;
17755
17756 case DW_OP_plus_uconst:
17757 stack[stacki] += read_unsigned_leb128 (NULL, (data + i),
17758 &bytes_read);
17759 i += bytes_read;
17760 break;
17761
17762 case DW_OP_minus:
17763 stack[stacki - 1] -= stack[stacki];
17764 stacki--;
17765 break;
17766
17767 case DW_OP_deref:
17768 /* If we're not the last op, then we definitely can't encode
17769 this using GDB's address_class enum. This is valid for partial
17770 global symbols, although the variable's address will be bogus
17771 in the psymtab. */
17772 if (i < size)
17773 dwarf2_complex_location_expr_complaint ();
17774 break;
17775
17776 case DW_OP_GNU_push_tls_address:
17777 /* The top of the stack has the offset from the beginning
17778 of the thread control block at which the variable is located. */
17779 /* Nothing should follow this operator, so the top of stack would
17780 be returned. */
17781 /* This is valid for partial global symbols, but the variable's
585861ea
JK
17782 address will be bogus in the psymtab. Make it always at least
17783 non-zero to not look as a variable garbage collected by linker
17784 which have DW_OP_addr 0. */
21ae7a4d
JK
17785 if (i < size)
17786 dwarf2_complex_location_expr_complaint ();
585861ea 17787 stack[stacki]++;
21ae7a4d
JK
17788 break;
17789
17790 case DW_OP_GNU_uninit:
17791 break;
17792
3019eac3 17793 case DW_OP_GNU_addr_index:
49f6c839 17794 case DW_OP_GNU_const_index:
3019eac3
DE
17795 stack[++stacki] = read_addr_index_from_leb128 (cu, &data[i],
17796 &bytes_read);
17797 i += bytes_read;
17798 break;
17799
21ae7a4d
JK
17800 default:
17801 {
f39c6ffd 17802 const char *name = get_DW_OP_name (op);
21ae7a4d
JK
17803
17804 if (name)
17805 complaint (&symfile_complaints, _("unsupported stack op: '%s'"),
17806 name);
17807 else
17808 complaint (&symfile_complaints, _("unsupported stack op: '%02x'"),
17809 op);
17810 }
17811
17812 return (stack[stacki]);
d53d4ac5 17813 }
3c6e0cb3 17814
21ae7a4d
JK
17815 /* Enforce maximum stack depth of SIZE-1 to avoid writing
17816 outside of the allocated space. Also enforce minimum>0. */
17817 if (stacki >= ARRAY_SIZE (stack) - 1)
17818 {
17819 complaint (&symfile_complaints,
17820 _("location description stack overflow"));
17821 return 0;
17822 }
17823
17824 if (stacki <= 0)
17825 {
17826 complaint (&symfile_complaints,
17827 _("location description stack underflow"));
17828 return 0;
17829 }
17830 }
17831 return (stack[stacki]);
c906108c
SS
17832}
17833
17834/* memory allocation interface */
17835
c906108c 17836static struct dwarf_block *
7b5a2f43 17837dwarf_alloc_block (struct dwarf2_cu *cu)
c906108c
SS
17838{
17839 struct dwarf_block *blk;
17840
17841 blk = (struct dwarf_block *)
7b5a2f43 17842 obstack_alloc (&cu->comp_unit_obstack, sizeof (struct dwarf_block));
c906108c
SS
17843 return (blk);
17844}
17845
c906108c 17846static struct die_info *
b60c80d6 17847dwarf_alloc_die (struct dwarf2_cu *cu, int num_attrs)
c906108c
SS
17848{
17849 struct die_info *die;
b60c80d6
DJ
17850 size_t size = sizeof (struct die_info);
17851
17852 if (num_attrs > 1)
17853 size += (num_attrs - 1) * sizeof (struct attribute);
c906108c 17854
b60c80d6 17855 die = (struct die_info *) obstack_alloc (&cu->comp_unit_obstack, size);
c906108c
SS
17856 memset (die, 0, sizeof (struct die_info));
17857 return (die);
17858}
2e276125
JB
17859
17860\f
17861/* Macro support. */
17862
2e276125
JB
17863/* Return the full name of file number I in *LH's file name table.
17864 Use COMP_DIR as the name of the current directory of the
17865 compilation. The result is allocated using xmalloc; the caller is
17866 responsible for freeing it. */
17867static char *
17868file_full_name (int file, struct line_header *lh, const char *comp_dir)
17869{
6a83a1e6
EZ
17870 /* Is the file number a valid index into the line header's file name
17871 table? Remember that file numbers start with one, not zero. */
17872 if (1 <= file && file <= lh->num_file_names)
17873 {
17874 struct file_entry *fe = &lh->file_names[file - 1];
6e70227d 17875
6a83a1e6
EZ
17876 if (IS_ABSOLUTE_PATH (fe->name))
17877 return xstrdup (fe->name);
17878 else
17879 {
17880 const char *dir;
17881 int dir_len;
17882 char *full_name;
17883
17884 if (fe->dir_index)
17885 dir = lh->include_dirs[fe->dir_index - 1];
17886 else
17887 dir = comp_dir;
17888
17889 if (dir)
17890 {
17891 dir_len = strlen (dir);
17892 full_name = xmalloc (dir_len + 1 + strlen (fe->name) + 1);
17893 strcpy (full_name, dir);
17894 full_name[dir_len] = '/';
17895 strcpy (full_name + dir_len + 1, fe->name);
17896 return full_name;
17897 }
17898 else
17899 return xstrdup (fe->name);
17900 }
17901 }
2e276125
JB
17902 else
17903 {
6a83a1e6
EZ
17904 /* The compiler produced a bogus file number. We can at least
17905 record the macro definitions made in the file, even if we
17906 won't be able to find the file by name. */
17907 char fake_name[80];
9a619af0 17908
8c042590
PM
17909 xsnprintf (fake_name, sizeof (fake_name),
17910 "<bad macro file number %d>", file);
2e276125 17911
6e70227d 17912 complaint (&symfile_complaints,
6a83a1e6
EZ
17913 _("bad file number in macro information (%d)"),
17914 file);
2e276125 17915
6a83a1e6 17916 return xstrdup (fake_name);
2e276125
JB
17917 }
17918}
17919
17920
17921static struct macro_source_file *
17922macro_start_file (int file, int line,
17923 struct macro_source_file *current_file,
17924 const char *comp_dir,
17925 struct line_header *lh, struct objfile *objfile)
17926{
17927 /* The full name of this source file. */
17928 char *full_name = file_full_name (file, lh, comp_dir);
17929
17930 /* We don't create a macro table for this compilation unit
17931 at all until we actually get a filename. */
17932 if (! pending_macros)
6532ff36
TT
17933 pending_macros = new_macro_table (&objfile->per_bfd->storage_obstack,
17934 objfile->per_bfd->macro_cache);
2e276125
JB
17935
17936 if (! current_file)
abc9d0dc
TT
17937 {
17938 /* If we have no current file, then this must be the start_file
17939 directive for the compilation unit's main source file. */
17940 current_file = macro_set_main (pending_macros, full_name);
17941 macro_define_special (pending_macros);
17942 }
2e276125
JB
17943 else
17944 current_file = macro_include (current_file, line, full_name);
17945
17946 xfree (full_name);
6e70227d 17947
2e276125
JB
17948 return current_file;
17949}
17950
17951
17952/* Copy the LEN characters at BUF to a xmalloc'ed block of memory,
17953 followed by a null byte. */
17954static char *
17955copy_string (const char *buf, int len)
17956{
17957 char *s = xmalloc (len + 1);
9a619af0 17958
2e276125
JB
17959 memcpy (s, buf, len);
17960 s[len] = '\0';
2e276125
JB
17961 return s;
17962}
17963
17964
17965static const char *
17966consume_improper_spaces (const char *p, const char *body)
17967{
17968 if (*p == ' ')
17969 {
4d3c2250 17970 complaint (&symfile_complaints,
3e43a32a
MS
17971 _("macro definition contains spaces "
17972 "in formal argument list:\n`%s'"),
4d3c2250 17973 body);
2e276125
JB
17974
17975 while (*p == ' ')
17976 p++;
17977 }
17978
17979 return p;
17980}
17981
17982
17983static void
17984parse_macro_definition (struct macro_source_file *file, int line,
17985 const char *body)
17986{
17987 const char *p;
17988
17989 /* The body string takes one of two forms. For object-like macro
17990 definitions, it should be:
17991
17992 <macro name> " " <definition>
17993
17994 For function-like macro definitions, it should be:
17995
17996 <macro name> "() " <definition>
17997 or
17998 <macro name> "(" <arg name> ( "," <arg name> ) * ") " <definition>
17999
18000 Spaces may appear only where explicitly indicated, and in the
18001 <definition>.
18002
18003 The Dwarf 2 spec says that an object-like macro's name is always
18004 followed by a space, but versions of GCC around March 2002 omit
6e70227d 18005 the space when the macro's definition is the empty string.
2e276125
JB
18006
18007 The Dwarf 2 spec says that there should be no spaces between the
18008 formal arguments in a function-like macro's formal argument list,
18009 but versions of GCC around March 2002 include spaces after the
18010 commas. */
18011
18012
18013 /* Find the extent of the macro name. The macro name is terminated
18014 by either a space or null character (for an object-like macro) or
18015 an opening paren (for a function-like macro). */
18016 for (p = body; *p; p++)
18017 if (*p == ' ' || *p == '(')
18018 break;
18019
18020 if (*p == ' ' || *p == '\0')
18021 {
18022 /* It's an object-like macro. */
18023 int name_len = p - body;
18024 char *name = copy_string (body, name_len);
18025 const char *replacement;
18026
18027 if (*p == ' ')
18028 replacement = body + name_len + 1;
18029 else
18030 {
4d3c2250 18031 dwarf2_macro_malformed_definition_complaint (body);
2e276125
JB
18032 replacement = body + name_len;
18033 }
6e70227d 18034
2e276125
JB
18035 macro_define_object (file, line, name, replacement);
18036
18037 xfree (name);
18038 }
18039 else if (*p == '(')
18040 {
18041 /* It's a function-like macro. */
18042 char *name = copy_string (body, p - body);
18043 int argc = 0;
18044 int argv_size = 1;
18045 char **argv = xmalloc (argv_size * sizeof (*argv));
18046
18047 p++;
18048
18049 p = consume_improper_spaces (p, body);
18050
18051 /* Parse the formal argument list. */
18052 while (*p && *p != ')')
18053 {
18054 /* Find the extent of the current argument name. */
18055 const char *arg_start = p;
18056
18057 while (*p && *p != ',' && *p != ')' && *p != ' ')
18058 p++;
18059
18060 if (! *p || p == arg_start)
4d3c2250 18061 dwarf2_macro_malformed_definition_complaint (body);
2e276125
JB
18062 else
18063 {
18064 /* Make sure argv has room for the new argument. */
18065 if (argc >= argv_size)
18066 {
18067 argv_size *= 2;
18068 argv = xrealloc (argv, argv_size * sizeof (*argv));
18069 }
18070
18071 argv[argc++] = copy_string (arg_start, p - arg_start);
18072 }
18073
18074 p = consume_improper_spaces (p, body);
18075
18076 /* Consume the comma, if present. */
18077 if (*p == ',')
18078 {
18079 p++;
18080
18081 p = consume_improper_spaces (p, body);
18082 }
18083 }
18084
18085 if (*p == ')')
18086 {
18087 p++;
18088
18089 if (*p == ' ')
18090 /* Perfectly formed definition, no complaints. */
18091 macro_define_function (file, line, name,
6e70227d 18092 argc, (const char **) argv,
2e276125
JB
18093 p + 1);
18094 else if (*p == '\0')
18095 {
18096 /* Complain, but do define it. */
4d3c2250 18097 dwarf2_macro_malformed_definition_complaint (body);
2e276125 18098 macro_define_function (file, line, name,
6e70227d 18099 argc, (const char **) argv,
2e276125
JB
18100 p);
18101 }
18102 else
18103 /* Just complain. */
4d3c2250 18104 dwarf2_macro_malformed_definition_complaint (body);
2e276125
JB
18105 }
18106 else
18107 /* Just complain. */
4d3c2250 18108 dwarf2_macro_malformed_definition_complaint (body);
2e276125
JB
18109
18110 xfree (name);
18111 {
18112 int i;
18113
18114 for (i = 0; i < argc; i++)
18115 xfree (argv[i]);
18116 }
18117 xfree (argv);
18118 }
18119 else
4d3c2250 18120 dwarf2_macro_malformed_definition_complaint (body);
2e276125
JB
18121}
18122
cf2c3c16
TT
18123/* Skip some bytes from BYTES according to the form given in FORM.
18124 Returns the new pointer. */
2e276125 18125
cf2c3c16 18126static gdb_byte *
f664829e 18127skip_form_bytes (bfd *abfd, gdb_byte *bytes, gdb_byte *buffer_end,
cf2c3c16
TT
18128 enum dwarf_form form,
18129 unsigned int offset_size,
18130 struct dwarf2_section_info *section)
2e276125 18131{
cf2c3c16 18132 unsigned int bytes_read;
2e276125 18133
cf2c3c16 18134 switch (form)
2e276125 18135 {
cf2c3c16
TT
18136 case DW_FORM_data1:
18137 case DW_FORM_flag:
18138 ++bytes;
18139 break;
18140
18141 case DW_FORM_data2:
18142 bytes += 2;
18143 break;
18144
18145 case DW_FORM_data4:
18146 bytes += 4;
18147 break;
18148
18149 case DW_FORM_data8:
18150 bytes += 8;
18151 break;
18152
18153 case DW_FORM_string:
18154 read_direct_string (abfd, bytes, &bytes_read);
18155 bytes += bytes_read;
18156 break;
18157
18158 case DW_FORM_sec_offset:
18159 case DW_FORM_strp:
36586728 18160 case DW_FORM_GNU_strp_alt:
cf2c3c16
TT
18161 bytes += offset_size;
18162 break;
18163
18164 case DW_FORM_block:
18165 bytes += read_unsigned_leb128 (abfd, bytes, &bytes_read);
18166 bytes += bytes_read;
18167 break;
18168
18169 case DW_FORM_block1:
18170 bytes += 1 + read_1_byte (abfd, bytes);
18171 break;
18172 case DW_FORM_block2:
18173 bytes += 2 + read_2_bytes (abfd, bytes);
18174 break;
18175 case DW_FORM_block4:
18176 bytes += 4 + read_4_bytes (abfd, bytes);
18177 break;
18178
18179 case DW_FORM_sdata:
18180 case DW_FORM_udata:
3019eac3
DE
18181 case DW_FORM_GNU_addr_index:
18182 case DW_FORM_GNU_str_index:
f664829e
DE
18183 bytes = (gdb_byte *) gdb_skip_leb128 (bytes, buffer_end);
18184 if (bytes == NULL)
18185 {
18186 dwarf2_section_buffer_overflow_complaint (section);
18187 return NULL;
18188 }
cf2c3c16
TT
18189 break;
18190
18191 default:
18192 {
18193 complain:
18194 complaint (&symfile_complaints,
18195 _("invalid form 0x%x in `%s'"),
18196 form,
18197 section->asection->name);
18198 return NULL;
18199 }
2e276125
JB
18200 }
18201
cf2c3c16
TT
18202 return bytes;
18203}
757a13d0 18204
cf2c3c16
TT
18205/* A helper for dwarf_decode_macros that handles skipping an unknown
18206 opcode. Returns an updated pointer to the macro data buffer; or,
18207 on error, issues a complaint and returns NULL. */
757a13d0 18208
cf2c3c16
TT
18209static gdb_byte *
18210skip_unknown_opcode (unsigned int opcode,
18211 gdb_byte **opcode_definitions,
f664829e 18212 gdb_byte *mac_ptr, gdb_byte *mac_end,
cf2c3c16
TT
18213 bfd *abfd,
18214 unsigned int offset_size,
18215 struct dwarf2_section_info *section)
18216{
18217 unsigned int bytes_read, i;
18218 unsigned long arg;
18219 gdb_byte *defn;
2e276125 18220
cf2c3c16 18221 if (opcode_definitions[opcode] == NULL)
2e276125 18222 {
cf2c3c16
TT
18223 complaint (&symfile_complaints,
18224 _("unrecognized DW_MACFINO opcode 0x%x"),
18225 opcode);
18226 return NULL;
18227 }
2e276125 18228
cf2c3c16
TT
18229 defn = opcode_definitions[opcode];
18230 arg = read_unsigned_leb128 (abfd, defn, &bytes_read);
18231 defn += bytes_read;
2e276125 18232
cf2c3c16
TT
18233 for (i = 0; i < arg; ++i)
18234 {
f664829e
DE
18235 mac_ptr = skip_form_bytes (abfd, mac_ptr, mac_end, defn[i], offset_size,
18236 section);
cf2c3c16
TT
18237 if (mac_ptr == NULL)
18238 {
18239 /* skip_form_bytes already issued the complaint. */
18240 return NULL;
18241 }
18242 }
757a13d0 18243
cf2c3c16
TT
18244 return mac_ptr;
18245}
757a13d0 18246
cf2c3c16
TT
18247/* A helper function which parses the header of a macro section.
18248 If the macro section is the extended (for now called "GNU") type,
18249 then this updates *OFFSET_SIZE. Returns a pointer to just after
18250 the header, or issues a complaint and returns NULL on error. */
757a13d0 18251
cf2c3c16
TT
18252static gdb_byte *
18253dwarf_parse_macro_header (gdb_byte **opcode_definitions,
18254 bfd *abfd,
18255 gdb_byte *mac_ptr,
18256 unsigned int *offset_size,
18257 int section_is_gnu)
18258{
18259 memset (opcode_definitions, 0, 256 * sizeof (gdb_byte *));
757a13d0 18260
cf2c3c16
TT
18261 if (section_is_gnu)
18262 {
18263 unsigned int version, flags;
757a13d0 18264
cf2c3c16
TT
18265 version = read_2_bytes (abfd, mac_ptr);
18266 if (version != 4)
18267 {
18268 complaint (&symfile_complaints,
18269 _("unrecognized version `%d' in .debug_macro section"),
18270 version);
18271 return NULL;
18272 }
18273 mac_ptr += 2;
757a13d0 18274
cf2c3c16
TT
18275 flags = read_1_byte (abfd, mac_ptr);
18276 ++mac_ptr;
18277 *offset_size = (flags & 1) ? 8 : 4;
757a13d0 18278
cf2c3c16
TT
18279 if ((flags & 2) != 0)
18280 /* We don't need the line table offset. */
18281 mac_ptr += *offset_size;
757a13d0 18282
cf2c3c16
TT
18283 /* Vendor opcode descriptions. */
18284 if ((flags & 4) != 0)
18285 {
18286 unsigned int i, count;
757a13d0 18287
cf2c3c16
TT
18288 count = read_1_byte (abfd, mac_ptr);
18289 ++mac_ptr;
18290 for (i = 0; i < count; ++i)
18291 {
18292 unsigned int opcode, bytes_read;
18293 unsigned long arg;
18294
18295 opcode = read_1_byte (abfd, mac_ptr);
18296 ++mac_ptr;
18297 opcode_definitions[opcode] = mac_ptr;
18298 arg = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
18299 mac_ptr += bytes_read;
18300 mac_ptr += arg;
18301 }
757a13d0 18302 }
cf2c3c16 18303 }
757a13d0 18304
cf2c3c16
TT
18305 return mac_ptr;
18306}
757a13d0 18307
cf2c3c16 18308/* A helper for dwarf_decode_macros that handles the GNU extensions,
8fc3fc34 18309 including DW_MACRO_GNU_transparent_include. */
cf2c3c16
TT
18310
18311static void
18312dwarf_decode_macro_bytes (bfd *abfd, gdb_byte *mac_ptr, gdb_byte *mac_end,
18313 struct macro_source_file *current_file,
18314 struct line_header *lh, char *comp_dir,
18315 struct dwarf2_section_info *section,
36586728 18316 int section_is_gnu, int section_is_dwz,
cf2c3c16 18317 unsigned int offset_size,
8fc3fc34
TT
18318 struct objfile *objfile,
18319 htab_t include_hash)
cf2c3c16
TT
18320{
18321 enum dwarf_macro_record_type macinfo_type;
18322 int at_commandline;
18323 gdb_byte *opcode_definitions[256];
757a13d0 18324
cf2c3c16
TT
18325 mac_ptr = dwarf_parse_macro_header (opcode_definitions, abfd, mac_ptr,
18326 &offset_size, section_is_gnu);
18327 if (mac_ptr == NULL)
18328 {
18329 /* We already issued a complaint. */
18330 return;
18331 }
757a13d0
JK
18332
18333 /* Determines if GDB is still before first DW_MACINFO_start_file. If true
18334 GDB is still reading the definitions from command line. First
18335 DW_MACINFO_start_file will need to be ignored as it was already executed
18336 to create CURRENT_FILE for the main source holding also the command line
18337 definitions. On first met DW_MACINFO_start_file this flag is reset to
18338 normally execute all the remaining DW_MACINFO_start_file macinfos. */
18339
18340 at_commandline = 1;
18341
18342 do
18343 {
18344 /* Do we at least have room for a macinfo type byte? */
18345 if (mac_ptr >= mac_end)
18346 {
f664829e 18347 dwarf2_section_buffer_overflow_complaint (section);
757a13d0
JK
18348 break;
18349 }
18350
18351 macinfo_type = read_1_byte (abfd, mac_ptr);
18352 mac_ptr++;
18353
cf2c3c16
TT
18354 /* Note that we rely on the fact that the corresponding GNU and
18355 DWARF constants are the same. */
757a13d0
JK
18356 switch (macinfo_type)
18357 {
18358 /* A zero macinfo type indicates the end of the macro
18359 information. */
18360 case 0:
18361 break;
2e276125 18362
cf2c3c16
TT
18363 case DW_MACRO_GNU_define:
18364 case DW_MACRO_GNU_undef:
18365 case DW_MACRO_GNU_define_indirect:
18366 case DW_MACRO_GNU_undef_indirect:
36586728
TT
18367 case DW_MACRO_GNU_define_indirect_alt:
18368 case DW_MACRO_GNU_undef_indirect_alt:
2e276125 18369 {
891d2f0b 18370 unsigned int bytes_read;
2e276125
JB
18371 int line;
18372 char *body;
cf2c3c16 18373 int is_define;
2e276125 18374
cf2c3c16
TT
18375 line = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
18376 mac_ptr += bytes_read;
18377
18378 if (macinfo_type == DW_MACRO_GNU_define
18379 || macinfo_type == DW_MACRO_GNU_undef)
18380 {
18381 body = read_direct_string (abfd, mac_ptr, &bytes_read);
18382 mac_ptr += bytes_read;
18383 }
18384 else
18385 {
18386 LONGEST str_offset;
18387
18388 str_offset = read_offset_1 (abfd, mac_ptr, offset_size);
18389 mac_ptr += offset_size;
2e276125 18390
36586728 18391 if (macinfo_type == DW_MACRO_GNU_define_indirect_alt
f7a35f02
TT
18392 || macinfo_type == DW_MACRO_GNU_undef_indirect_alt
18393 || section_is_dwz)
36586728
TT
18394 {
18395 struct dwz_file *dwz = dwarf2_get_dwz_file ();
18396
18397 body = read_indirect_string_from_dwz (dwz, str_offset);
18398 }
18399 else
18400 body = read_indirect_string_at_offset (abfd, str_offset);
cf2c3c16
TT
18401 }
18402
18403 is_define = (macinfo_type == DW_MACRO_GNU_define
36586728
TT
18404 || macinfo_type == DW_MACRO_GNU_define_indirect
18405 || macinfo_type == DW_MACRO_GNU_define_indirect_alt);
2e276125 18406 if (! current_file)
757a13d0
JK
18407 {
18408 /* DWARF violation as no main source is present. */
18409 complaint (&symfile_complaints,
18410 _("debug info with no main source gives macro %s "
18411 "on line %d: %s"),
cf2c3c16
TT
18412 is_define ? _("definition") : _("undefinition"),
18413 line, body);
757a13d0
JK
18414 break;
18415 }
3e43a32a
MS
18416 if ((line == 0 && !at_commandline)
18417 || (line != 0 && at_commandline))
4d3c2250 18418 complaint (&symfile_complaints,
757a13d0
JK
18419 _("debug info gives %s macro %s with %s line %d: %s"),
18420 at_commandline ? _("command-line") : _("in-file"),
cf2c3c16 18421 is_define ? _("definition") : _("undefinition"),
757a13d0
JK
18422 line == 0 ? _("zero") : _("non-zero"), line, body);
18423
cf2c3c16 18424 if (is_define)
757a13d0 18425 parse_macro_definition (current_file, line, body);
cf2c3c16
TT
18426 else
18427 {
18428 gdb_assert (macinfo_type == DW_MACRO_GNU_undef
36586728
TT
18429 || macinfo_type == DW_MACRO_GNU_undef_indirect
18430 || macinfo_type == DW_MACRO_GNU_undef_indirect_alt);
cf2c3c16
TT
18431 macro_undef (current_file, line, body);
18432 }
2e276125
JB
18433 }
18434 break;
18435
cf2c3c16 18436 case DW_MACRO_GNU_start_file:
2e276125 18437 {
891d2f0b 18438 unsigned int bytes_read;
2e276125
JB
18439 int line, file;
18440
18441 line = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
18442 mac_ptr += bytes_read;
18443 file = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
18444 mac_ptr += bytes_read;
18445
3e43a32a
MS
18446 if ((line == 0 && !at_commandline)
18447 || (line != 0 && at_commandline))
757a13d0
JK
18448 complaint (&symfile_complaints,
18449 _("debug info gives source %d included "
18450 "from %s at %s line %d"),
18451 file, at_commandline ? _("command-line") : _("file"),
18452 line == 0 ? _("zero") : _("non-zero"), line);
18453
18454 if (at_commandline)
18455 {
cf2c3c16
TT
18456 /* This DW_MACRO_GNU_start_file was executed in the
18457 pass one. */
757a13d0
JK
18458 at_commandline = 0;
18459 }
18460 else
18461 current_file = macro_start_file (file, line,
18462 current_file, comp_dir,
cf2c3c16 18463 lh, objfile);
2e276125
JB
18464 }
18465 break;
18466
cf2c3c16 18467 case DW_MACRO_GNU_end_file:
2e276125 18468 if (! current_file)
4d3c2250 18469 complaint (&symfile_complaints,
3e43a32a
MS
18470 _("macro debug info has an unmatched "
18471 "`close_file' directive"));
2e276125
JB
18472 else
18473 {
18474 current_file = current_file->included_by;
18475 if (! current_file)
18476 {
cf2c3c16 18477 enum dwarf_macro_record_type next_type;
2e276125
JB
18478
18479 /* GCC circa March 2002 doesn't produce the zero
18480 type byte marking the end of the compilation
18481 unit. Complain if it's not there, but exit no
18482 matter what. */
18483
18484 /* Do we at least have room for a macinfo type byte? */
18485 if (mac_ptr >= mac_end)
18486 {
f664829e 18487 dwarf2_section_buffer_overflow_complaint (section);
2e276125
JB
18488 return;
18489 }
18490
18491 /* We don't increment mac_ptr here, so this is just
18492 a look-ahead. */
18493 next_type = read_1_byte (abfd, mac_ptr);
18494 if (next_type != 0)
4d3c2250 18495 complaint (&symfile_complaints,
3e43a32a
MS
18496 _("no terminating 0-type entry for "
18497 "macros in `.debug_macinfo' section"));
2e276125
JB
18498
18499 return;
18500 }
18501 }
18502 break;
18503
cf2c3c16 18504 case DW_MACRO_GNU_transparent_include:
36586728 18505 case DW_MACRO_GNU_transparent_include_alt:
cf2c3c16
TT
18506 {
18507 LONGEST offset;
8fc3fc34 18508 void **slot;
a036ba48
TT
18509 bfd *include_bfd = abfd;
18510 struct dwarf2_section_info *include_section = section;
18511 struct dwarf2_section_info alt_section;
18512 gdb_byte *include_mac_end = mac_end;
18513 int is_dwz = section_is_dwz;
18514 gdb_byte *new_mac_ptr;
cf2c3c16
TT
18515
18516 offset = read_offset_1 (abfd, mac_ptr, offset_size);
18517 mac_ptr += offset_size;
18518
a036ba48
TT
18519 if (macinfo_type == DW_MACRO_GNU_transparent_include_alt)
18520 {
18521 struct dwz_file *dwz = dwarf2_get_dwz_file ();
18522
18523 dwarf2_read_section (dwarf2_per_objfile->objfile,
18524 &dwz->macro);
18525
18526 include_bfd = dwz->macro.asection->owner;
18527 include_section = &dwz->macro;
18528 include_mac_end = dwz->macro.buffer + dwz->macro.size;
18529 is_dwz = 1;
18530 }
18531
18532 new_mac_ptr = include_section->buffer + offset;
18533 slot = htab_find_slot (include_hash, new_mac_ptr, INSERT);
18534
8fc3fc34
TT
18535 if (*slot != NULL)
18536 {
18537 /* This has actually happened; see
18538 http://sourceware.org/bugzilla/show_bug.cgi?id=13568. */
18539 complaint (&symfile_complaints,
18540 _("recursive DW_MACRO_GNU_transparent_include in "
18541 ".debug_macro section"));
18542 }
18543 else
18544 {
a036ba48 18545 *slot = new_mac_ptr;
36586728 18546
a036ba48 18547 dwarf_decode_macro_bytes (include_bfd, new_mac_ptr,
36586728 18548 include_mac_end, current_file,
8fc3fc34 18549 lh, comp_dir,
36586728 18550 section, section_is_gnu, is_dwz,
8fc3fc34
TT
18551 offset_size, objfile, include_hash);
18552
a036ba48 18553 htab_remove_elt (include_hash, new_mac_ptr);
8fc3fc34 18554 }
cf2c3c16
TT
18555 }
18556 break;
18557
2e276125 18558 case DW_MACINFO_vendor_ext:
cf2c3c16
TT
18559 if (!section_is_gnu)
18560 {
18561 unsigned int bytes_read;
18562 int constant;
2e276125 18563
cf2c3c16
TT
18564 constant = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
18565 mac_ptr += bytes_read;
18566 read_direct_string (abfd, mac_ptr, &bytes_read);
18567 mac_ptr += bytes_read;
2e276125 18568
cf2c3c16
TT
18569 /* We don't recognize any vendor extensions. */
18570 break;
18571 }
18572 /* FALLTHROUGH */
18573
18574 default:
18575 mac_ptr = skip_unknown_opcode (macinfo_type, opcode_definitions,
f664829e 18576 mac_ptr, mac_end, abfd, offset_size,
cf2c3c16
TT
18577 section);
18578 if (mac_ptr == NULL)
18579 return;
18580 break;
2e276125 18581 }
757a13d0 18582 } while (macinfo_type != 0);
2e276125 18583}
8e19ed76 18584
cf2c3c16 18585static void
09262596
DE
18586dwarf_decode_macros (struct dwarf2_cu *cu, unsigned int offset,
18587 char *comp_dir, int section_is_gnu)
cf2c3c16 18588{
bb5ed363 18589 struct objfile *objfile = dwarf2_per_objfile->objfile;
09262596
DE
18590 struct line_header *lh = cu->line_header;
18591 bfd *abfd;
cf2c3c16
TT
18592 gdb_byte *mac_ptr, *mac_end;
18593 struct macro_source_file *current_file = 0;
18594 enum dwarf_macro_record_type macinfo_type;
18595 unsigned int offset_size = cu->header.offset_size;
18596 gdb_byte *opcode_definitions[256];
8fc3fc34
TT
18597 struct cleanup *cleanup;
18598 htab_t include_hash;
18599 void **slot;
09262596
DE
18600 struct dwarf2_section_info *section;
18601 const char *section_name;
18602
18603 if (cu->dwo_unit != NULL)
18604 {
18605 if (section_is_gnu)
18606 {
18607 section = &cu->dwo_unit->dwo_file->sections.macro;
18608 section_name = ".debug_macro.dwo";
18609 }
18610 else
18611 {
18612 section = &cu->dwo_unit->dwo_file->sections.macinfo;
18613 section_name = ".debug_macinfo.dwo";
18614 }
18615 }
18616 else
18617 {
18618 if (section_is_gnu)
18619 {
18620 section = &dwarf2_per_objfile->macro;
18621 section_name = ".debug_macro";
18622 }
18623 else
18624 {
18625 section = &dwarf2_per_objfile->macinfo;
18626 section_name = ".debug_macinfo";
18627 }
18628 }
cf2c3c16 18629
bb5ed363 18630 dwarf2_read_section (objfile, section);
cf2c3c16
TT
18631 if (section->buffer == NULL)
18632 {
fceca515 18633 complaint (&symfile_complaints, _("missing %s section"), section_name);
cf2c3c16
TT
18634 return;
18635 }
09262596 18636 abfd = section->asection->owner;
cf2c3c16
TT
18637
18638 /* First pass: Find the name of the base filename.
18639 This filename is needed in order to process all macros whose definition
18640 (or undefinition) comes from the command line. These macros are defined
18641 before the first DW_MACINFO_start_file entry, and yet still need to be
18642 associated to the base file.
18643
18644 To determine the base file name, we scan the macro definitions until we
18645 reach the first DW_MACINFO_start_file entry. We then initialize
18646 CURRENT_FILE accordingly so that any macro definition found before the
18647 first DW_MACINFO_start_file can still be associated to the base file. */
18648
18649 mac_ptr = section->buffer + offset;
18650 mac_end = section->buffer + section->size;
18651
18652 mac_ptr = dwarf_parse_macro_header (opcode_definitions, abfd, mac_ptr,
18653 &offset_size, section_is_gnu);
18654 if (mac_ptr == NULL)
18655 {
18656 /* We already issued a complaint. */
18657 return;
18658 }
18659
18660 do
18661 {
18662 /* Do we at least have room for a macinfo type byte? */
18663 if (mac_ptr >= mac_end)
18664 {
18665 /* Complaint is printed during the second pass as GDB will probably
18666 stop the first pass earlier upon finding
18667 DW_MACINFO_start_file. */
18668 break;
18669 }
18670
18671 macinfo_type = read_1_byte (abfd, mac_ptr);
18672 mac_ptr++;
18673
18674 /* Note that we rely on the fact that the corresponding GNU and
18675 DWARF constants are the same. */
18676 switch (macinfo_type)
18677 {
18678 /* A zero macinfo type indicates the end of the macro
18679 information. */
18680 case 0:
18681 break;
18682
18683 case DW_MACRO_GNU_define:
18684 case DW_MACRO_GNU_undef:
18685 /* Only skip the data by MAC_PTR. */
18686 {
18687 unsigned int bytes_read;
18688
18689 read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
18690 mac_ptr += bytes_read;
18691 read_direct_string (abfd, mac_ptr, &bytes_read);
18692 mac_ptr += bytes_read;
18693 }
18694 break;
18695
18696 case DW_MACRO_GNU_start_file:
18697 {
18698 unsigned int bytes_read;
18699 int line, file;
18700
18701 line = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
18702 mac_ptr += bytes_read;
18703 file = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
18704 mac_ptr += bytes_read;
18705
18706 current_file = macro_start_file (file, line, current_file,
bb5ed363 18707 comp_dir, lh, objfile);
cf2c3c16
TT
18708 }
18709 break;
18710
18711 case DW_MACRO_GNU_end_file:
18712 /* No data to skip by MAC_PTR. */
18713 break;
18714
18715 case DW_MACRO_GNU_define_indirect:
18716 case DW_MACRO_GNU_undef_indirect:
f7a35f02
TT
18717 case DW_MACRO_GNU_define_indirect_alt:
18718 case DW_MACRO_GNU_undef_indirect_alt:
cf2c3c16
TT
18719 {
18720 unsigned int bytes_read;
18721
18722 read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
18723 mac_ptr += bytes_read;
18724 mac_ptr += offset_size;
18725 }
18726 break;
18727
18728 case DW_MACRO_GNU_transparent_include:
f7a35f02 18729 case DW_MACRO_GNU_transparent_include_alt:
cf2c3c16
TT
18730 /* Note that, according to the spec, a transparent include
18731 chain cannot call DW_MACRO_GNU_start_file. So, we can just
18732 skip this opcode. */
18733 mac_ptr += offset_size;
18734 break;
18735
18736 case DW_MACINFO_vendor_ext:
18737 /* Only skip the data by MAC_PTR. */
18738 if (!section_is_gnu)
18739 {
18740 unsigned int bytes_read;
18741
18742 read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
18743 mac_ptr += bytes_read;
18744 read_direct_string (abfd, mac_ptr, &bytes_read);
18745 mac_ptr += bytes_read;
18746 }
18747 /* FALLTHROUGH */
18748
18749 default:
18750 mac_ptr = skip_unknown_opcode (macinfo_type, opcode_definitions,
f664829e 18751 mac_ptr, mac_end, abfd, offset_size,
cf2c3c16
TT
18752 section);
18753 if (mac_ptr == NULL)
18754 return;
18755 break;
18756 }
18757 } while (macinfo_type != 0 && current_file == NULL);
18758
18759 /* Second pass: Process all entries.
18760
18761 Use the AT_COMMAND_LINE flag to determine whether we are still processing
18762 command-line macro definitions/undefinitions. This flag is unset when we
18763 reach the first DW_MACINFO_start_file entry. */
18764
8fc3fc34
TT
18765 include_hash = htab_create_alloc (1, htab_hash_pointer, htab_eq_pointer,
18766 NULL, xcalloc, xfree);
18767 cleanup = make_cleanup_htab_delete (include_hash);
18768 mac_ptr = section->buffer + offset;
18769 slot = htab_find_slot (include_hash, mac_ptr, INSERT);
18770 *slot = mac_ptr;
18771 dwarf_decode_macro_bytes (abfd, mac_ptr, mac_end,
36586728
TT
18772 current_file, lh, comp_dir, section,
18773 section_is_gnu, 0,
8fc3fc34
TT
18774 offset_size, objfile, include_hash);
18775 do_cleanups (cleanup);
cf2c3c16
TT
18776}
18777
8e19ed76 18778/* Check if the attribute's form is a DW_FORM_block*
0963b4bd 18779 if so return true else false. */
380bca97 18780
8e19ed76
PS
18781static int
18782attr_form_is_block (struct attribute *attr)
18783{
18784 return (attr == NULL ? 0 :
18785 attr->form == DW_FORM_block1
18786 || attr->form == DW_FORM_block2
18787 || attr->form == DW_FORM_block4
2dc7f7b3
TT
18788 || attr->form == DW_FORM_block
18789 || attr->form == DW_FORM_exprloc);
8e19ed76 18790}
4c2df51b 18791
c6a0999f
JB
18792/* Return non-zero if ATTR's value is a section offset --- classes
18793 lineptr, loclistptr, macptr or rangelistptr --- or zero, otherwise.
18794 You may use DW_UNSND (attr) to retrieve such offsets.
18795
18796 Section 7.5.4, "Attribute Encodings", explains that no attribute
18797 may have a value that belongs to more than one of these classes; it
18798 would be ambiguous if we did, because we use the same forms for all
18799 of them. */
380bca97 18800
3690dd37
JB
18801static int
18802attr_form_is_section_offset (struct attribute *attr)
18803{
18804 return (attr->form == DW_FORM_data4
2dc7f7b3
TT
18805 || attr->form == DW_FORM_data8
18806 || attr->form == DW_FORM_sec_offset);
3690dd37
JB
18807}
18808
3690dd37
JB
18809/* Return non-zero if ATTR's value falls in the 'constant' class, or
18810 zero otherwise. When this function returns true, you can apply
18811 dwarf2_get_attr_constant_value to it.
18812
18813 However, note that for some attributes you must check
18814 attr_form_is_section_offset before using this test. DW_FORM_data4
18815 and DW_FORM_data8 are members of both the constant class, and of
18816 the classes that contain offsets into other debug sections
18817 (lineptr, loclistptr, macptr or rangelistptr). The DWARF spec says
18818 that, if an attribute's can be either a constant or one of the
18819 section offset classes, DW_FORM_data4 and DW_FORM_data8 should be
18820 taken as section offsets, not constants. */
380bca97 18821
3690dd37
JB
18822static int
18823attr_form_is_constant (struct attribute *attr)
18824{
18825 switch (attr->form)
18826 {
18827 case DW_FORM_sdata:
18828 case DW_FORM_udata:
18829 case DW_FORM_data1:
18830 case DW_FORM_data2:
18831 case DW_FORM_data4:
18832 case DW_FORM_data8:
18833 return 1;
18834 default:
18835 return 0;
18836 }
18837}
18838
3019eac3
DE
18839/* Return the .debug_loc section to use for CU.
18840 For DWO files use .debug_loc.dwo. */
18841
18842static struct dwarf2_section_info *
18843cu_debug_loc_section (struct dwarf2_cu *cu)
18844{
18845 if (cu->dwo_unit)
18846 return &cu->dwo_unit->dwo_file->sections.loc;
18847 return &dwarf2_per_objfile->loc;
18848}
18849
8cf6f0b1
TT
18850/* A helper function that fills in a dwarf2_loclist_baton. */
18851
18852static void
18853fill_in_loclist_baton (struct dwarf2_cu *cu,
18854 struct dwarf2_loclist_baton *baton,
18855 struct attribute *attr)
18856{
3019eac3
DE
18857 struct dwarf2_section_info *section = cu_debug_loc_section (cu);
18858
18859 dwarf2_read_section (dwarf2_per_objfile->objfile, section);
8cf6f0b1
TT
18860
18861 baton->per_cu = cu->per_cu;
18862 gdb_assert (baton->per_cu);
18863 /* We don't know how long the location list is, but make sure we
18864 don't run off the edge of the section. */
3019eac3
DE
18865 baton->size = section->size - DW_UNSND (attr);
18866 baton->data = section->buffer + DW_UNSND (attr);
8cf6f0b1 18867 baton->base_address = cu->base_address;
f664829e 18868 baton->from_dwo = cu->dwo_unit != NULL;
8cf6f0b1
TT
18869}
18870
4c2df51b
DJ
18871static void
18872dwarf2_symbol_mark_computed (struct attribute *attr, struct symbol *sym,
e7c27a73 18873 struct dwarf2_cu *cu)
4c2df51b 18874{
bb5ed363 18875 struct objfile *objfile = dwarf2_per_objfile->objfile;
3019eac3 18876 struct dwarf2_section_info *section = cu_debug_loc_section (cu);
bb5ed363 18877
3690dd37 18878 if (attr_form_is_section_offset (attr)
3019eac3 18879 /* .debug_loc{,.dwo} may not exist at all, or the offset may be outside
99bcc461
DJ
18880 the section. If so, fall through to the complaint in the
18881 other branch. */
3019eac3 18882 && DW_UNSND (attr) < dwarf2_section_size (objfile, section))
4c2df51b 18883 {
0d53c4c4 18884 struct dwarf2_loclist_baton *baton;
4c2df51b 18885
bb5ed363 18886 baton = obstack_alloc (&objfile->objfile_obstack,
0d53c4c4 18887 sizeof (struct dwarf2_loclist_baton));
4c2df51b 18888
8cf6f0b1 18889 fill_in_loclist_baton (cu, baton, attr);
be391dca 18890
d00adf39 18891 if (cu->base_known == 0)
0d53c4c4 18892 complaint (&symfile_complaints,
3e43a32a
MS
18893 _("Location list used without "
18894 "specifying the CU base address."));
4c2df51b 18895
768a979c 18896 SYMBOL_COMPUTED_OPS (sym) = &dwarf2_loclist_funcs;
0d53c4c4
DJ
18897 SYMBOL_LOCATION_BATON (sym) = baton;
18898 }
18899 else
18900 {
18901 struct dwarf2_locexpr_baton *baton;
18902
bb5ed363 18903 baton = obstack_alloc (&objfile->objfile_obstack,
0d53c4c4 18904 sizeof (struct dwarf2_locexpr_baton));
ae0d2f24
UW
18905 baton->per_cu = cu->per_cu;
18906 gdb_assert (baton->per_cu);
0d53c4c4
DJ
18907
18908 if (attr_form_is_block (attr))
18909 {
18910 /* Note that we're just copying the block's data pointer
18911 here, not the actual data. We're still pointing into the
6502dd73
DJ
18912 info_buffer for SYM's objfile; right now we never release
18913 that buffer, but when we do clean up properly this may
18914 need to change. */
0d53c4c4
DJ
18915 baton->size = DW_BLOCK (attr)->size;
18916 baton->data = DW_BLOCK (attr)->data;
18917 }
18918 else
18919 {
18920 dwarf2_invalid_attrib_class_complaint ("location description",
18921 SYMBOL_NATURAL_NAME (sym));
18922 baton->size = 0;
0d53c4c4 18923 }
6e70227d 18924
768a979c 18925 SYMBOL_COMPUTED_OPS (sym) = &dwarf2_locexpr_funcs;
0d53c4c4
DJ
18926 SYMBOL_LOCATION_BATON (sym) = baton;
18927 }
4c2df51b 18928}
6502dd73 18929
9aa1f1e3
TT
18930/* Return the OBJFILE associated with the compilation unit CU. If CU
18931 came from a separate debuginfo file, then the master objfile is
18932 returned. */
ae0d2f24
UW
18933
18934struct objfile *
18935dwarf2_per_cu_objfile (struct dwarf2_per_cu_data *per_cu)
18936{
9291a0cd 18937 struct objfile *objfile = per_cu->objfile;
ae0d2f24
UW
18938
18939 /* Return the master objfile, so that we can report and look up the
18940 correct file containing this variable. */
18941 if (objfile->separate_debug_objfile_backlink)
18942 objfile = objfile->separate_debug_objfile_backlink;
18943
18944 return objfile;
18945}
18946
96408a79
SA
18947/* Return comp_unit_head for PER_CU, either already available in PER_CU->CU
18948 (CU_HEADERP is unused in such case) or prepare a temporary copy at
18949 CU_HEADERP first. */
18950
18951static const struct comp_unit_head *
18952per_cu_header_read_in (struct comp_unit_head *cu_headerp,
18953 struct dwarf2_per_cu_data *per_cu)
18954{
96408a79
SA
18955 gdb_byte *info_ptr;
18956
18957 if (per_cu->cu)
18958 return &per_cu->cu->header;
18959
0bc3a05c 18960 info_ptr = per_cu->info_or_types_section->buffer + per_cu->offset.sect_off;
96408a79
SA
18961
18962 memset (cu_headerp, 0, sizeof (*cu_headerp));
0bc3a05c 18963 read_comp_unit_head (cu_headerp, info_ptr, per_cu->objfile->obfd);
96408a79
SA
18964
18965 return cu_headerp;
18966}
18967
ae0d2f24
UW
18968/* Return the address size given in the compilation unit header for CU. */
18969
98714339 18970int
ae0d2f24
UW
18971dwarf2_per_cu_addr_size (struct dwarf2_per_cu_data *per_cu)
18972{
96408a79
SA
18973 struct comp_unit_head cu_header_local;
18974 const struct comp_unit_head *cu_headerp;
c471e790 18975
96408a79
SA
18976 cu_headerp = per_cu_header_read_in (&cu_header_local, per_cu);
18977
18978 return cu_headerp->addr_size;
ae0d2f24
UW
18979}
18980
9eae7c52
TT
18981/* Return the offset size given in the compilation unit header for CU. */
18982
18983int
18984dwarf2_per_cu_offset_size (struct dwarf2_per_cu_data *per_cu)
18985{
96408a79
SA
18986 struct comp_unit_head cu_header_local;
18987 const struct comp_unit_head *cu_headerp;
9c6c53f7 18988
96408a79
SA
18989 cu_headerp = per_cu_header_read_in (&cu_header_local, per_cu);
18990
18991 return cu_headerp->offset_size;
18992}
18993
18994/* See its dwarf2loc.h declaration. */
18995
18996int
18997dwarf2_per_cu_ref_addr_size (struct dwarf2_per_cu_data *per_cu)
18998{
18999 struct comp_unit_head cu_header_local;
19000 const struct comp_unit_head *cu_headerp;
19001
19002 cu_headerp = per_cu_header_read_in (&cu_header_local, per_cu);
19003
19004 if (cu_headerp->version == 2)
19005 return cu_headerp->addr_size;
19006 else
19007 return cu_headerp->offset_size;
181cebd4
JK
19008}
19009
9aa1f1e3
TT
19010/* Return the text offset of the CU. The returned offset comes from
19011 this CU's objfile. If this objfile came from a separate debuginfo
19012 file, then the offset may be different from the corresponding
19013 offset in the parent objfile. */
19014
19015CORE_ADDR
19016dwarf2_per_cu_text_offset (struct dwarf2_per_cu_data *per_cu)
19017{
bb3fa9d0 19018 struct objfile *objfile = per_cu->objfile;
9aa1f1e3
TT
19019
19020 return ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
19021}
19022
348e048f
DE
19023/* Locate the .debug_info compilation unit from CU's objfile which contains
19024 the DIE at OFFSET. Raises an error on failure. */
ae038cb0
DJ
19025
19026static struct dwarf2_per_cu_data *
b64f50a1 19027dwarf2_find_containing_comp_unit (sect_offset offset,
36586728 19028 unsigned int offset_in_dwz,
ae038cb0
DJ
19029 struct objfile *objfile)
19030{
19031 struct dwarf2_per_cu_data *this_cu;
19032 int low, high;
36586728 19033 const sect_offset *cu_off;
ae038cb0 19034
ae038cb0
DJ
19035 low = 0;
19036 high = dwarf2_per_objfile->n_comp_units - 1;
19037 while (high > low)
19038 {
36586728 19039 struct dwarf2_per_cu_data *mid_cu;
ae038cb0 19040 int mid = low + (high - low) / 2;
9a619af0 19041
36586728
TT
19042 mid_cu = dwarf2_per_objfile->all_comp_units[mid];
19043 cu_off = &mid_cu->offset;
19044 if (mid_cu->is_dwz > offset_in_dwz
19045 || (mid_cu->is_dwz == offset_in_dwz
19046 && cu_off->sect_off >= offset.sect_off))
ae038cb0
DJ
19047 high = mid;
19048 else
19049 low = mid + 1;
19050 }
19051 gdb_assert (low == high);
36586728
TT
19052 this_cu = dwarf2_per_objfile->all_comp_units[low];
19053 cu_off = &this_cu->offset;
19054 if (this_cu->is_dwz != offset_in_dwz || cu_off->sect_off > offset.sect_off)
ae038cb0 19055 {
36586728 19056 if (low == 0 || this_cu->is_dwz != offset_in_dwz)
8a3fe4f8
AC
19057 error (_("Dwarf Error: could not find partial DIE containing "
19058 "offset 0x%lx [in module %s]"),
b64f50a1 19059 (long) offset.sect_off, bfd_get_filename (objfile->obfd));
10b3939b 19060
b64f50a1
JK
19061 gdb_assert (dwarf2_per_objfile->all_comp_units[low-1]->offset.sect_off
19062 <= offset.sect_off);
ae038cb0
DJ
19063 return dwarf2_per_objfile->all_comp_units[low-1];
19064 }
19065 else
19066 {
19067 this_cu = dwarf2_per_objfile->all_comp_units[low];
19068 if (low == dwarf2_per_objfile->n_comp_units - 1
b64f50a1
JK
19069 && offset.sect_off >= this_cu->offset.sect_off + this_cu->length)
19070 error (_("invalid dwarf2 offset %u"), offset.sect_off);
19071 gdb_assert (offset.sect_off < this_cu->offset.sect_off + this_cu->length);
ae038cb0
DJ
19072 return this_cu;
19073 }
19074}
19075
23745b47 19076/* Initialize dwarf2_cu CU, owned by PER_CU. */
93311388 19077
9816fde3 19078static void
23745b47 19079init_one_comp_unit (struct dwarf2_cu *cu, struct dwarf2_per_cu_data *per_cu)
93311388 19080{
9816fde3 19081 memset (cu, 0, sizeof (*cu));
23745b47
DE
19082 per_cu->cu = cu;
19083 cu->per_cu = per_cu;
19084 cu->objfile = per_cu->objfile;
93311388 19085 obstack_init (&cu->comp_unit_obstack);
9816fde3
JK
19086}
19087
19088/* Initialize basic fields of dwarf_cu CU according to DIE COMP_UNIT_DIE. */
19089
19090static void
95554aad
TT
19091prepare_one_comp_unit (struct dwarf2_cu *cu, struct die_info *comp_unit_die,
19092 enum language pretend_language)
9816fde3
JK
19093{
19094 struct attribute *attr;
19095
19096 /* Set the language we're debugging. */
19097 attr = dwarf2_attr (comp_unit_die, DW_AT_language, cu);
19098 if (attr)
19099 set_cu_language (DW_UNSND (attr), cu);
19100 else
9cded63f 19101 {
95554aad 19102 cu->language = pretend_language;
9cded63f
TT
19103 cu->language_defn = language_def (cu->language);
19104 }
dee91e82
DE
19105
19106 attr = dwarf2_attr (comp_unit_die, DW_AT_producer, cu);
19107 if (attr)
19108 cu->producer = DW_STRING (attr);
93311388
DE
19109}
19110
ae038cb0
DJ
19111/* Release one cached compilation unit, CU. We unlink it from the tree
19112 of compilation units, but we don't remove it from the read_in_chain;
93311388
DE
19113 the caller is responsible for that.
19114 NOTE: DATA is a void * because this function is also used as a
19115 cleanup routine. */
ae038cb0
DJ
19116
19117static void
68dc6402 19118free_heap_comp_unit (void *data)
ae038cb0
DJ
19119{
19120 struct dwarf2_cu *cu = data;
19121
23745b47
DE
19122 gdb_assert (cu->per_cu != NULL);
19123 cu->per_cu->cu = NULL;
ae038cb0
DJ
19124 cu->per_cu = NULL;
19125
19126 obstack_free (&cu->comp_unit_obstack, NULL);
19127
19128 xfree (cu);
19129}
19130
72bf9492 19131/* This cleanup function is passed the address of a dwarf2_cu on the stack
ae038cb0 19132 when we're finished with it. We can't free the pointer itself, but be
dee91e82 19133 sure to unlink it from the cache. Also release any associated storage. */
72bf9492
DJ
19134
19135static void
19136free_stack_comp_unit (void *data)
19137{
19138 struct dwarf2_cu *cu = data;
19139
23745b47
DE
19140 gdb_assert (cu->per_cu != NULL);
19141 cu->per_cu->cu = NULL;
19142 cu->per_cu = NULL;
19143
72bf9492
DJ
19144 obstack_free (&cu->comp_unit_obstack, NULL);
19145 cu->partial_dies = NULL;
ae038cb0
DJ
19146}
19147
19148/* Free all cached compilation units. */
19149
19150static void
19151free_cached_comp_units (void *data)
19152{
19153 struct dwarf2_per_cu_data *per_cu, **last_chain;
19154
19155 per_cu = dwarf2_per_objfile->read_in_chain;
19156 last_chain = &dwarf2_per_objfile->read_in_chain;
19157 while (per_cu != NULL)
19158 {
19159 struct dwarf2_per_cu_data *next_cu;
19160
19161 next_cu = per_cu->cu->read_in_chain;
19162
68dc6402 19163 free_heap_comp_unit (per_cu->cu);
ae038cb0
DJ
19164 *last_chain = next_cu;
19165
19166 per_cu = next_cu;
19167 }
19168}
19169
19170/* Increase the age counter on each cached compilation unit, and free
19171 any that are too old. */
19172
19173static void
19174age_cached_comp_units (void)
19175{
19176 struct dwarf2_per_cu_data *per_cu, **last_chain;
19177
19178 dwarf2_clear_marks (dwarf2_per_objfile->read_in_chain);
19179 per_cu = dwarf2_per_objfile->read_in_chain;
19180 while (per_cu != NULL)
19181 {
19182 per_cu->cu->last_used ++;
19183 if (per_cu->cu->last_used <= dwarf2_max_cache_age)
19184 dwarf2_mark (per_cu->cu);
19185 per_cu = per_cu->cu->read_in_chain;
19186 }
19187
19188 per_cu = dwarf2_per_objfile->read_in_chain;
19189 last_chain = &dwarf2_per_objfile->read_in_chain;
19190 while (per_cu != NULL)
19191 {
19192 struct dwarf2_per_cu_data *next_cu;
19193
19194 next_cu = per_cu->cu->read_in_chain;
19195
19196 if (!per_cu->cu->mark)
19197 {
68dc6402 19198 free_heap_comp_unit (per_cu->cu);
ae038cb0
DJ
19199 *last_chain = next_cu;
19200 }
19201 else
19202 last_chain = &per_cu->cu->read_in_chain;
19203
19204 per_cu = next_cu;
19205 }
19206}
19207
19208/* Remove a single compilation unit from the cache. */
19209
19210static void
dee91e82 19211free_one_cached_comp_unit (struct dwarf2_per_cu_data *target_per_cu)
ae038cb0
DJ
19212{
19213 struct dwarf2_per_cu_data *per_cu, **last_chain;
19214
19215 per_cu = dwarf2_per_objfile->read_in_chain;
19216 last_chain = &dwarf2_per_objfile->read_in_chain;
19217 while (per_cu != NULL)
19218 {
19219 struct dwarf2_per_cu_data *next_cu;
19220
19221 next_cu = per_cu->cu->read_in_chain;
19222
dee91e82 19223 if (per_cu == target_per_cu)
ae038cb0 19224 {
68dc6402 19225 free_heap_comp_unit (per_cu->cu);
dee91e82 19226 per_cu->cu = NULL;
ae038cb0
DJ
19227 *last_chain = next_cu;
19228 break;
19229 }
19230 else
19231 last_chain = &per_cu->cu->read_in_chain;
19232
19233 per_cu = next_cu;
19234 }
19235}
19236
fe3e1990
DJ
19237/* Release all extra memory associated with OBJFILE. */
19238
19239void
19240dwarf2_free_objfile (struct objfile *objfile)
19241{
19242 dwarf2_per_objfile = objfile_data (objfile, dwarf2_objfile_data_key);
19243
19244 if (dwarf2_per_objfile == NULL)
19245 return;
19246
19247 /* Cached DIE trees use xmalloc and the comp_unit_obstack. */
19248 free_cached_comp_units (NULL);
19249
7b9f3c50
DE
19250 if (dwarf2_per_objfile->quick_file_names_table)
19251 htab_delete (dwarf2_per_objfile->quick_file_names_table);
9291a0cd 19252
fe3e1990
DJ
19253 /* Everything else should be on the objfile obstack. */
19254}
19255
dee91e82
DE
19256/* A set of CU "per_cu" pointer, DIE offset, and GDB type pointer.
19257 We store these in a hash table separate from the DIEs, and preserve them
19258 when the DIEs are flushed out of cache.
19259
19260 The CU "per_cu" pointer is needed because offset alone is not enough to
3019eac3
DE
19261 uniquely identify the type. A file may have multiple .debug_types sections,
19262 or the type may come from a DWO file. We have to use something in
19263 dwarf2_per_cu_data (or the pointer to it) because we can enter the lookup
19264 routine, get_die_type_at_offset, from outside this file, and thus won't
19265 necessarily have PER_CU->cu. Fortunately, PER_CU is stable for the life
19266 of the objfile. */
1c379e20 19267
dee91e82 19268struct dwarf2_per_cu_offset_and_type
1c379e20 19269{
dee91e82 19270 const struct dwarf2_per_cu_data *per_cu;
b64f50a1 19271 sect_offset offset;
1c379e20
DJ
19272 struct type *type;
19273};
19274
dee91e82 19275/* Hash function for a dwarf2_per_cu_offset_and_type. */
1c379e20
DJ
19276
19277static hashval_t
dee91e82 19278per_cu_offset_and_type_hash (const void *item)
1c379e20 19279{
dee91e82 19280 const struct dwarf2_per_cu_offset_and_type *ofs = item;
9a619af0 19281
dee91e82 19282 return (uintptr_t) ofs->per_cu + ofs->offset.sect_off;
1c379e20
DJ
19283}
19284
dee91e82 19285/* Equality function for a dwarf2_per_cu_offset_and_type. */
1c379e20
DJ
19286
19287static int
dee91e82 19288per_cu_offset_and_type_eq (const void *item_lhs, const void *item_rhs)
1c379e20 19289{
dee91e82
DE
19290 const struct dwarf2_per_cu_offset_and_type *ofs_lhs = item_lhs;
19291 const struct dwarf2_per_cu_offset_and_type *ofs_rhs = item_rhs;
9a619af0 19292
dee91e82
DE
19293 return (ofs_lhs->per_cu == ofs_rhs->per_cu
19294 && ofs_lhs->offset.sect_off == ofs_rhs->offset.sect_off);
1c379e20
DJ
19295}
19296
19297/* Set the type associated with DIE to TYPE. Save it in CU's hash
7e314c57
JK
19298 table if necessary. For convenience, return TYPE.
19299
19300 The DIEs reading must have careful ordering to:
19301 * Not cause infite loops trying to read in DIEs as a prerequisite for
19302 reading current DIE.
19303 * Not trying to dereference contents of still incompletely read in types
19304 while reading in other DIEs.
19305 * Enable referencing still incompletely read in types just by a pointer to
19306 the type without accessing its fields.
19307
19308 Therefore caller should follow these rules:
19309 * Try to fetch any prerequisite types we may need to build this DIE type
19310 before building the type and calling set_die_type.
e71ec853 19311 * After building type call set_die_type for current DIE as soon as
7e314c57
JK
19312 possible before fetching more types to complete the current type.
19313 * Make the type as complete as possible before fetching more types. */
1c379e20 19314
f792889a 19315static struct type *
1c379e20
DJ
19316set_die_type (struct die_info *die, struct type *type, struct dwarf2_cu *cu)
19317{
dee91e82 19318 struct dwarf2_per_cu_offset_and_type **slot, ofs;
673bfd45 19319 struct objfile *objfile = cu->objfile;
1c379e20 19320
b4ba55a1
JB
19321 /* For Ada types, make sure that the gnat-specific data is always
19322 initialized (if not already set). There are a few types where
19323 we should not be doing so, because the type-specific area is
19324 already used to hold some other piece of info (eg: TYPE_CODE_FLT
19325 where the type-specific area is used to store the floatformat).
19326 But this is not a problem, because the gnat-specific information
19327 is actually not needed for these types. */
19328 if (need_gnat_info (cu)
19329 && TYPE_CODE (type) != TYPE_CODE_FUNC
19330 && TYPE_CODE (type) != TYPE_CODE_FLT
19331 && !HAVE_GNAT_AUX_INFO (type))
19332 INIT_GNAT_SPECIFIC (type);
19333
dee91e82 19334 if (dwarf2_per_objfile->die_type_hash == NULL)
f792889a 19335 {
dee91e82
DE
19336 dwarf2_per_objfile->die_type_hash =
19337 htab_create_alloc_ex (127,
19338 per_cu_offset_and_type_hash,
19339 per_cu_offset_and_type_eq,
19340 NULL,
19341 &objfile->objfile_obstack,
19342 hashtab_obstack_allocate,
19343 dummy_obstack_deallocate);
f792889a 19344 }
1c379e20 19345
dee91e82 19346 ofs.per_cu = cu->per_cu;
1c379e20
DJ
19347 ofs.offset = die->offset;
19348 ofs.type = type;
dee91e82
DE
19349 slot = (struct dwarf2_per_cu_offset_and_type **)
19350 htab_find_slot (dwarf2_per_objfile->die_type_hash, &ofs, INSERT);
7e314c57
JK
19351 if (*slot)
19352 complaint (&symfile_complaints,
19353 _("A problem internal to GDB: DIE 0x%x has type already set"),
b64f50a1 19354 die->offset.sect_off);
673bfd45 19355 *slot = obstack_alloc (&objfile->objfile_obstack, sizeof (**slot));
1c379e20 19356 **slot = ofs;
f792889a 19357 return type;
1c379e20
DJ
19358}
19359
380bca97 19360/* Look up the type for the die at OFFSET in the appropriate type_hash
673bfd45 19361 table, or return NULL if the die does not have a saved type. */
1c379e20
DJ
19362
19363static struct type *
b64f50a1 19364get_die_type_at_offset (sect_offset offset,
673bfd45 19365 struct dwarf2_per_cu_data *per_cu)
1c379e20 19366{
dee91e82 19367 struct dwarf2_per_cu_offset_and_type *slot, ofs;
f792889a 19368
dee91e82 19369 if (dwarf2_per_objfile->die_type_hash == NULL)
f792889a 19370 return NULL;
1c379e20 19371
dee91e82 19372 ofs.per_cu = per_cu;
673bfd45 19373 ofs.offset = offset;
dee91e82 19374 slot = htab_find (dwarf2_per_objfile->die_type_hash, &ofs);
1c379e20
DJ
19375 if (slot)
19376 return slot->type;
19377 else
19378 return NULL;
19379}
19380
673bfd45
DE
19381/* Look up the type for DIE in the appropriate type_hash table,
19382 or return NULL if DIE does not have a saved type. */
19383
19384static struct type *
19385get_die_type (struct die_info *die, struct dwarf2_cu *cu)
19386{
19387 return get_die_type_at_offset (die->offset, cu->per_cu);
19388}
19389
10b3939b
DJ
19390/* Add a dependence relationship from CU to REF_PER_CU. */
19391
19392static void
19393dwarf2_add_dependence (struct dwarf2_cu *cu,
19394 struct dwarf2_per_cu_data *ref_per_cu)
19395{
19396 void **slot;
19397
19398 if (cu->dependencies == NULL)
19399 cu->dependencies
19400 = htab_create_alloc_ex (5, htab_hash_pointer, htab_eq_pointer,
19401 NULL, &cu->comp_unit_obstack,
19402 hashtab_obstack_allocate,
19403 dummy_obstack_deallocate);
19404
19405 slot = htab_find_slot (cu->dependencies, ref_per_cu, INSERT);
19406 if (*slot == NULL)
19407 *slot = ref_per_cu;
19408}
1c379e20 19409
f504f079
DE
19410/* Subroutine of dwarf2_mark to pass to htab_traverse.
19411 Set the mark field in every compilation unit in the
ae038cb0
DJ
19412 cache that we must keep because we are keeping CU. */
19413
10b3939b
DJ
19414static int
19415dwarf2_mark_helper (void **slot, void *data)
19416{
19417 struct dwarf2_per_cu_data *per_cu;
19418
19419 per_cu = (struct dwarf2_per_cu_data *) *slot;
d07ed419
JK
19420
19421 /* cu->dependencies references may not yet have been ever read if QUIT aborts
19422 reading of the chain. As such dependencies remain valid it is not much
19423 useful to track and undo them during QUIT cleanups. */
19424 if (per_cu->cu == NULL)
19425 return 1;
19426
10b3939b
DJ
19427 if (per_cu->cu->mark)
19428 return 1;
19429 per_cu->cu->mark = 1;
19430
19431 if (per_cu->cu->dependencies != NULL)
19432 htab_traverse (per_cu->cu->dependencies, dwarf2_mark_helper, NULL);
19433
19434 return 1;
19435}
19436
f504f079
DE
19437/* Set the mark field in CU and in every other compilation unit in the
19438 cache that we must keep because we are keeping CU. */
19439
ae038cb0
DJ
19440static void
19441dwarf2_mark (struct dwarf2_cu *cu)
19442{
19443 if (cu->mark)
19444 return;
19445 cu->mark = 1;
10b3939b
DJ
19446 if (cu->dependencies != NULL)
19447 htab_traverse (cu->dependencies, dwarf2_mark_helper, NULL);
ae038cb0
DJ
19448}
19449
19450static void
19451dwarf2_clear_marks (struct dwarf2_per_cu_data *per_cu)
19452{
19453 while (per_cu)
19454 {
19455 per_cu->cu->mark = 0;
19456 per_cu = per_cu->cu->read_in_chain;
19457 }
72bf9492
DJ
19458}
19459
72bf9492
DJ
19460/* Trivial hash function for partial_die_info: the hash value of a DIE
19461 is its offset in .debug_info for this objfile. */
19462
19463static hashval_t
19464partial_die_hash (const void *item)
19465{
19466 const struct partial_die_info *part_die = item;
9a619af0 19467
b64f50a1 19468 return part_die->offset.sect_off;
72bf9492
DJ
19469}
19470
19471/* Trivial comparison function for partial_die_info structures: two DIEs
19472 are equal if they have the same offset. */
19473
19474static int
19475partial_die_eq (const void *item_lhs, const void *item_rhs)
19476{
19477 const struct partial_die_info *part_die_lhs = item_lhs;
19478 const struct partial_die_info *part_die_rhs = item_rhs;
9a619af0 19479
b64f50a1 19480 return part_die_lhs->offset.sect_off == part_die_rhs->offset.sect_off;
72bf9492
DJ
19481}
19482
ae038cb0
DJ
19483static struct cmd_list_element *set_dwarf2_cmdlist;
19484static struct cmd_list_element *show_dwarf2_cmdlist;
19485
19486static void
19487set_dwarf2_cmd (char *args, int from_tty)
19488{
19489 help_list (set_dwarf2_cmdlist, "maintenance set dwarf2 ", -1, gdb_stdout);
19490}
19491
19492static void
19493show_dwarf2_cmd (char *args, int from_tty)
6e70227d 19494{
ae038cb0
DJ
19495 cmd_show_list (show_dwarf2_cmdlist, from_tty, "");
19496}
19497
4bf44c1c 19498/* Free data associated with OBJFILE, if necessary. */
dce234bc
PP
19499
19500static void
c1bd65d0 19501dwarf2_per_objfile_free (struct objfile *objfile, void *d)
dce234bc
PP
19502{
19503 struct dwarf2_per_objfile *data = d;
8b70b953 19504 int ix;
8b70b953 19505
95554aad
TT
19506 for (ix = 0; ix < dwarf2_per_objfile->n_comp_units; ++ix)
19507 VEC_free (dwarf2_per_cu_ptr,
f4dc4d17 19508 dwarf2_per_objfile->all_comp_units[ix]->s.imported_symtabs);
95554aad 19509
8b70b953 19510 VEC_free (dwarf2_section_info_def, data->types);
3019eac3
DE
19511
19512 if (data->dwo_files)
19513 free_dwo_files (data->dwo_files, objfile);
36586728
TT
19514
19515 if (data->dwz_file && data->dwz_file->dwz_bfd)
19516 gdb_bfd_unref (data->dwz_file->dwz_bfd);
9291a0cd
TT
19517}
19518
19519\f
ae2de4f8 19520/* The "save gdb-index" command. */
9291a0cd
TT
19521
19522/* The contents of the hash table we create when building the string
19523 table. */
19524struct strtab_entry
19525{
19526 offset_type offset;
19527 const char *str;
19528};
19529
559a7a62
JK
19530/* Hash function for a strtab_entry.
19531
19532 Function is used only during write_hash_table so no index format backward
19533 compatibility is needed. */
b89be57b 19534
9291a0cd
TT
19535static hashval_t
19536hash_strtab_entry (const void *e)
19537{
19538 const struct strtab_entry *entry = e;
559a7a62 19539 return mapped_index_string_hash (INT_MAX, entry->str);
9291a0cd
TT
19540}
19541
19542/* Equality function for a strtab_entry. */
b89be57b 19543
9291a0cd
TT
19544static int
19545eq_strtab_entry (const void *a, const void *b)
19546{
19547 const struct strtab_entry *ea = a;
19548 const struct strtab_entry *eb = b;
19549 return !strcmp (ea->str, eb->str);
19550}
19551
19552/* Create a strtab_entry hash table. */
b89be57b 19553
9291a0cd
TT
19554static htab_t
19555create_strtab (void)
19556{
19557 return htab_create_alloc (100, hash_strtab_entry, eq_strtab_entry,
19558 xfree, xcalloc, xfree);
19559}
19560
19561/* Add a string to the constant pool. Return the string's offset in
19562 host order. */
b89be57b 19563
9291a0cd
TT
19564static offset_type
19565add_string (htab_t table, struct obstack *cpool, const char *str)
19566{
19567 void **slot;
19568 struct strtab_entry entry;
19569 struct strtab_entry *result;
19570
19571 entry.str = str;
19572 slot = htab_find_slot (table, &entry, INSERT);
19573 if (*slot)
19574 result = *slot;
19575 else
19576 {
19577 result = XNEW (struct strtab_entry);
19578 result->offset = obstack_object_size (cpool);
19579 result->str = str;
19580 obstack_grow_str0 (cpool, str);
19581 *slot = result;
19582 }
19583 return result->offset;
19584}
19585
19586/* An entry in the symbol table. */
19587struct symtab_index_entry
19588{
19589 /* The name of the symbol. */
19590 const char *name;
19591 /* The offset of the name in the constant pool. */
19592 offset_type index_offset;
19593 /* A sorted vector of the indices of all the CUs that hold an object
19594 of this name. */
19595 VEC (offset_type) *cu_indices;
19596};
19597
19598/* The symbol table. This is a power-of-2-sized hash table. */
19599struct mapped_symtab
19600{
19601 offset_type n_elements;
19602 offset_type size;
19603 struct symtab_index_entry **data;
19604};
19605
19606/* Hash function for a symtab_index_entry. */
b89be57b 19607
9291a0cd
TT
19608static hashval_t
19609hash_symtab_entry (const void *e)
19610{
19611 const struct symtab_index_entry *entry = e;
19612 return iterative_hash (VEC_address (offset_type, entry->cu_indices),
19613 sizeof (offset_type) * VEC_length (offset_type,
19614 entry->cu_indices),
19615 0);
19616}
19617
19618/* Equality function for a symtab_index_entry. */
b89be57b 19619
9291a0cd
TT
19620static int
19621eq_symtab_entry (const void *a, const void *b)
19622{
19623 const struct symtab_index_entry *ea = a;
19624 const struct symtab_index_entry *eb = b;
19625 int len = VEC_length (offset_type, ea->cu_indices);
19626 if (len != VEC_length (offset_type, eb->cu_indices))
19627 return 0;
19628 return !memcmp (VEC_address (offset_type, ea->cu_indices),
19629 VEC_address (offset_type, eb->cu_indices),
19630 sizeof (offset_type) * len);
19631}
19632
19633/* Destroy a symtab_index_entry. */
b89be57b 19634
9291a0cd
TT
19635static void
19636delete_symtab_entry (void *p)
19637{
19638 struct symtab_index_entry *entry = p;
19639 VEC_free (offset_type, entry->cu_indices);
19640 xfree (entry);
19641}
19642
19643/* Create a hash table holding symtab_index_entry objects. */
b89be57b 19644
9291a0cd 19645static htab_t
3876f04e 19646create_symbol_hash_table (void)
9291a0cd
TT
19647{
19648 return htab_create_alloc (100, hash_symtab_entry, eq_symtab_entry,
19649 delete_symtab_entry, xcalloc, xfree);
19650}
19651
19652/* Create a new mapped symtab object. */
b89be57b 19653
9291a0cd
TT
19654static struct mapped_symtab *
19655create_mapped_symtab (void)
19656{
19657 struct mapped_symtab *symtab = XNEW (struct mapped_symtab);
19658 symtab->n_elements = 0;
19659 symtab->size = 1024;
19660 symtab->data = XCNEWVEC (struct symtab_index_entry *, symtab->size);
19661 return symtab;
19662}
19663
19664/* Destroy a mapped_symtab. */
b89be57b 19665
9291a0cd
TT
19666static void
19667cleanup_mapped_symtab (void *p)
19668{
19669 struct mapped_symtab *symtab = p;
19670 /* The contents of the array are freed when the other hash table is
19671 destroyed. */
19672 xfree (symtab->data);
19673 xfree (symtab);
19674}
19675
19676/* Find a slot in SYMTAB for the symbol NAME. Returns a pointer to
559a7a62
JK
19677 the slot.
19678
19679 Function is used only during write_hash_table so no index format backward
19680 compatibility is needed. */
b89be57b 19681
9291a0cd
TT
19682static struct symtab_index_entry **
19683find_slot (struct mapped_symtab *symtab, const char *name)
19684{
559a7a62 19685 offset_type index, step, hash = mapped_index_string_hash (INT_MAX, name);
9291a0cd
TT
19686
19687 index = hash & (symtab->size - 1);
19688 step = ((hash * 17) & (symtab->size - 1)) | 1;
19689
19690 for (;;)
19691 {
19692 if (!symtab->data[index] || !strcmp (name, symtab->data[index]->name))
19693 return &symtab->data[index];
19694 index = (index + step) & (symtab->size - 1);
19695 }
19696}
19697
19698/* Expand SYMTAB's hash table. */
b89be57b 19699
9291a0cd
TT
19700static void
19701hash_expand (struct mapped_symtab *symtab)
19702{
19703 offset_type old_size = symtab->size;
19704 offset_type i;
19705 struct symtab_index_entry **old_entries = symtab->data;
19706
19707 symtab->size *= 2;
19708 symtab->data = XCNEWVEC (struct symtab_index_entry *, symtab->size);
19709
19710 for (i = 0; i < old_size; ++i)
19711 {
19712 if (old_entries[i])
19713 {
19714 struct symtab_index_entry **slot = find_slot (symtab,
19715 old_entries[i]->name);
19716 *slot = old_entries[i];
19717 }
19718 }
19719
19720 xfree (old_entries);
19721}
19722
156942c7
DE
19723/* Add an entry to SYMTAB. NAME is the name of the symbol.
19724 CU_INDEX is the index of the CU in which the symbol appears.
19725 IS_STATIC is one if the symbol is static, otherwise zero (global). */
b89be57b 19726
9291a0cd
TT
19727static void
19728add_index_entry (struct mapped_symtab *symtab, const char *name,
156942c7 19729 int is_static, gdb_index_symbol_kind kind,
9291a0cd
TT
19730 offset_type cu_index)
19731{
19732 struct symtab_index_entry **slot;
156942c7 19733 offset_type cu_index_and_attrs;
9291a0cd
TT
19734
19735 ++symtab->n_elements;
19736 if (4 * symtab->n_elements / 3 >= symtab->size)
19737 hash_expand (symtab);
19738
19739 slot = find_slot (symtab, name);
19740 if (!*slot)
19741 {
19742 *slot = XNEW (struct symtab_index_entry);
19743 (*slot)->name = name;
156942c7 19744 /* index_offset is set later. */
9291a0cd
TT
19745 (*slot)->cu_indices = NULL;
19746 }
156942c7
DE
19747
19748 cu_index_and_attrs = 0;
19749 DW2_GDB_INDEX_CU_SET_VALUE (cu_index_and_attrs, cu_index);
19750 DW2_GDB_INDEX_SYMBOL_STATIC_SET_VALUE (cu_index_and_attrs, is_static);
19751 DW2_GDB_INDEX_SYMBOL_KIND_SET_VALUE (cu_index_and_attrs, kind);
19752
19753 /* We don't want to record an index value twice as we want to avoid the
19754 duplication.
19755 We process all global symbols and then all static symbols
19756 (which would allow us to avoid the duplication by only having to check
19757 the last entry pushed), but a symbol could have multiple kinds in one CU.
19758 To keep things simple we don't worry about the duplication here and
19759 sort and uniqufy the list after we've processed all symbols. */
19760 VEC_safe_push (offset_type, (*slot)->cu_indices, cu_index_and_attrs);
19761}
19762
19763/* qsort helper routine for uniquify_cu_indices. */
19764
19765static int
19766offset_type_compare (const void *ap, const void *bp)
19767{
19768 offset_type a = *(offset_type *) ap;
19769 offset_type b = *(offset_type *) bp;
19770
19771 return (a > b) - (b > a);
19772}
19773
19774/* Sort and remove duplicates of all symbols' cu_indices lists. */
19775
19776static void
19777uniquify_cu_indices (struct mapped_symtab *symtab)
19778{
19779 int i;
19780
19781 for (i = 0; i < symtab->size; ++i)
19782 {
19783 struct symtab_index_entry *entry = symtab->data[i];
19784
19785 if (entry
19786 && entry->cu_indices != NULL)
19787 {
19788 unsigned int next_to_insert, next_to_check;
19789 offset_type last_value;
19790
19791 qsort (VEC_address (offset_type, entry->cu_indices),
19792 VEC_length (offset_type, entry->cu_indices),
19793 sizeof (offset_type), offset_type_compare);
19794
19795 last_value = VEC_index (offset_type, entry->cu_indices, 0);
19796 next_to_insert = 1;
19797 for (next_to_check = 1;
19798 next_to_check < VEC_length (offset_type, entry->cu_indices);
19799 ++next_to_check)
19800 {
19801 if (VEC_index (offset_type, entry->cu_indices, next_to_check)
19802 != last_value)
19803 {
19804 last_value = VEC_index (offset_type, entry->cu_indices,
19805 next_to_check);
19806 VEC_replace (offset_type, entry->cu_indices, next_to_insert,
19807 last_value);
19808 ++next_to_insert;
19809 }
19810 }
19811 VEC_truncate (offset_type, entry->cu_indices, next_to_insert);
19812 }
19813 }
9291a0cd
TT
19814}
19815
19816/* Add a vector of indices to the constant pool. */
b89be57b 19817
9291a0cd 19818static offset_type
3876f04e 19819add_indices_to_cpool (htab_t symbol_hash_table, struct obstack *cpool,
9291a0cd
TT
19820 struct symtab_index_entry *entry)
19821{
19822 void **slot;
19823
3876f04e 19824 slot = htab_find_slot (symbol_hash_table, entry, INSERT);
9291a0cd
TT
19825 if (!*slot)
19826 {
19827 offset_type len = VEC_length (offset_type, entry->cu_indices);
19828 offset_type val = MAYBE_SWAP (len);
19829 offset_type iter;
19830 int i;
19831
19832 *slot = entry;
19833 entry->index_offset = obstack_object_size (cpool);
19834
19835 obstack_grow (cpool, &val, sizeof (val));
19836 for (i = 0;
19837 VEC_iterate (offset_type, entry->cu_indices, i, iter);
19838 ++i)
19839 {
19840 val = MAYBE_SWAP (iter);
19841 obstack_grow (cpool, &val, sizeof (val));
19842 }
19843 }
19844 else
19845 {
19846 struct symtab_index_entry *old_entry = *slot;
19847 entry->index_offset = old_entry->index_offset;
19848 entry = old_entry;
19849 }
19850 return entry->index_offset;
19851}
19852
19853/* Write the mapped hash table SYMTAB to the obstack OUTPUT, with
19854 constant pool entries going into the obstack CPOOL. */
b89be57b 19855
9291a0cd
TT
19856static void
19857write_hash_table (struct mapped_symtab *symtab,
19858 struct obstack *output, struct obstack *cpool)
19859{
19860 offset_type i;
3876f04e 19861 htab_t symbol_hash_table;
9291a0cd
TT
19862 htab_t str_table;
19863
3876f04e 19864 symbol_hash_table = create_symbol_hash_table ();
9291a0cd 19865 str_table = create_strtab ();
3876f04e 19866
9291a0cd
TT
19867 /* We add all the index vectors to the constant pool first, to
19868 ensure alignment is ok. */
19869 for (i = 0; i < symtab->size; ++i)
19870 {
19871 if (symtab->data[i])
3876f04e 19872 add_indices_to_cpool (symbol_hash_table, cpool, symtab->data[i]);
9291a0cd
TT
19873 }
19874
19875 /* Now write out the hash table. */
19876 for (i = 0; i < symtab->size; ++i)
19877 {
19878 offset_type str_off, vec_off;
19879
19880 if (symtab->data[i])
19881 {
19882 str_off = add_string (str_table, cpool, symtab->data[i]->name);
19883 vec_off = symtab->data[i]->index_offset;
19884 }
19885 else
19886 {
19887 /* While 0 is a valid constant pool index, it is not valid
19888 to have 0 for both offsets. */
19889 str_off = 0;
19890 vec_off = 0;
19891 }
19892
19893 str_off = MAYBE_SWAP (str_off);
19894 vec_off = MAYBE_SWAP (vec_off);
19895
19896 obstack_grow (output, &str_off, sizeof (str_off));
19897 obstack_grow (output, &vec_off, sizeof (vec_off));
19898 }
19899
19900 htab_delete (str_table);
3876f04e 19901 htab_delete (symbol_hash_table);
9291a0cd
TT
19902}
19903
0a5429f6
DE
19904/* Struct to map psymtab to CU index in the index file. */
19905struct psymtab_cu_index_map
19906{
19907 struct partial_symtab *psymtab;
19908 unsigned int cu_index;
19909};
19910
19911static hashval_t
19912hash_psymtab_cu_index (const void *item)
19913{
19914 const struct psymtab_cu_index_map *map = item;
19915
19916 return htab_hash_pointer (map->psymtab);
19917}
19918
19919static int
19920eq_psymtab_cu_index (const void *item_lhs, const void *item_rhs)
19921{
19922 const struct psymtab_cu_index_map *lhs = item_lhs;
19923 const struct psymtab_cu_index_map *rhs = item_rhs;
19924
19925 return lhs->psymtab == rhs->psymtab;
19926}
19927
19928/* Helper struct for building the address table. */
19929struct addrmap_index_data
19930{
19931 struct objfile *objfile;
19932 struct obstack *addr_obstack;
19933 htab_t cu_index_htab;
19934
19935 /* Non-zero if the previous_* fields are valid.
19936 We can't write an entry until we see the next entry (since it is only then
19937 that we know the end of the entry). */
19938 int previous_valid;
19939 /* Index of the CU in the table of all CUs in the index file. */
19940 unsigned int previous_cu_index;
0963b4bd 19941 /* Start address of the CU. */
0a5429f6
DE
19942 CORE_ADDR previous_cu_start;
19943};
19944
19945/* Write an address entry to OBSTACK. */
b89be57b 19946
9291a0cd 19947static void
0a5429f6
DE
19948add_address_entry (struct objfile *objfile, struct obstack *obstack,
19949 CORE_ADDR start, CORE_ADDR end, unsigned int cu_index)
9291a0cd 19950{
0a5429f6 19951 offset_type cu_index_to_write;
9291a0cd
TT
19952 char addr[8];
19953 CORE_ADDR baseaddr;
19954
19955 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
19956
0a5429f6
DE
19957 store_unsigned_integer (addr, 8, BFD_ENDIAN_LITTLE, start - baseaddr);
19958 obstack_grow (obstack, addr, 8);
19959 store_unsigned_integer (addr, 8, BFD_ENDIAN_LITTLE, end - baseaddr);
19960 obstack_grow (obstack, addr, 8);
19961 cu_index_to_write = MAYBE_SWAP (cu_index);
19962 obstack_grow (obstack, &cu_index_to_write, sizeof (offset_type));
19963}
19964
19965/* Worker function for traversing an addrmap to build the address table. */
19966
19967static int
19968add_address_entry_worker (void *datap, CORE_ADDR start_addr, void *obj)
19969{
19970 struct addrmap_index_data *data = datap;
19971 struct partial_symtab *pst = obj;
0a5429f6
DE
19972
19973 if (data->previous_valid)
19974 add_address_entry (data->objfile, data->addr_obstack,
19975 data->previous_cu_start, start_addr,
19976 data->previous_cu_index);
19977
19978 data->previous_cu_start = start_addr;
19979 if (pst != NULL)
19980 {
19981 struct psymtab_cu_index_map find_map, *map;
19982 find_map.psymtab = pst;
19983 map = htab_find (data->cu_index_htab, &find_map);
19984 gdb_assert (map != NULL);
19985 data->previous_cu_index = map->cu_index;
19986 data->previous_valid = 1;
19987 }
19988 else
19989 data->previous_valid = 0;
19990
19991 return 0;
19992}
19993
19994/* Write OBJFILE's address map to OBSTACK.
19995 CU_INDEX_HTAB is used to map addrmap entries to their CU indices
19996 in the index file. */
19997
19998static void
19999write_address_map (struct objfile *objfile, struct obstack *obstack,
20000 htab_t cu_index_htab)
20001{
20002 struct addrmap_index_data addrmap_index_data;
20003
20004 /* When writing the address table, we have to cope with the fact that
20005 the addrmap iterator only provides the start of a region; we have to
20006 wait until the next invocation to get the start of the next region. */
20007
20008 addrmap_index_data.objfile = objfile;
20009 addrmap_index_data.addr_obstack = obstack;
20010 addrmap_index_data.cu_index_htab = cu_index_htab;
20011 addrmap_index_data.previous_valid = 0;
20012
20013 addrmap_foreach (objfile->psymtabs_addrmap, add_address_entry_worker,
20014 &addrmap_index_data);
20015
20016 /* It's highly unlikely the last entry (end address = 0xff...ff)
20017 is valid, but we should still handle it.
20018 The end address is recorded as the start of the next region, but that
20019 doesn't work here. To cope we pass 0xff...ff, this is a rare situation
20020 anyway. */
20021 if (addrmap_index_data.previous_valid)
20022 add_address_entry (objfile, obstack,
20023 addrmap_index_data.previous_cu_start, (CORE_ADDR) -1,
20024 addrmap_index_data.previous_cu_index);
9291a0cd
TT
20025}
20026
156942c7
DE
20027/* Return the symbol kind of PSYM. */
20028
20029static gdb_index_symbol_kind
20030symbol_kind (struct partial_symbol *psym)
20031{
20032 domain_enum domain = PSYMBOL_DOMAIN (psym);
20033 enum address_class aclass = PSYMBOL_CLASS (psym);
20034
20035 switch (domain)
20036 {
20037 case VAR_DOMAIN:
20038 switch (aclass)
20039 {
20040 case LOC_BLOCK:
20041 return GDB_INDEX_SYMBOL_KIND_FUNCTION;
20042 case LOC_TYPEDEF:
20043 return GDB_INDEX_SYMBOL_KIND_TYPE;
20044 case LOC_COMPUTED:
20045 case LOC_CONST_BYTES:
20046 case LOC_OPTIMIZED_OUT:
20047 case LOC_STATIC:
20048 return GDB_INDEX_SYMBOL_KIND_VARIABLE;
20049 case LOC_CONST:
20050 /* Note: It's currently impossible to recognize psyms as enum values
20051 short of reading the type info. For now punt. */
20052 return GDB_INDEX_SYMBOL_KIND_VARIABLE;
20053 default:
20054 /* There are other LOC_FOO values that one might want to classify
20055 as variables, but dwarf2read.c doesn't currently use them. */
20056 return GDB_INDEX_SYMBOL_KIND_OTHER;
20057 }
20058 case STRUCT_DOMAIN:
20059 return GDB_INDEX_SYMBOL_KIND_TYPE;
20060 default:
20061 return GDB_INDEX_SYMBOL_KIND_OTHER;
20062 }
20063}
20064
9291a0cd 20065/* Add a list of partial symbols to SYMTAB. */
b89be57b 20066
9291a0cd
TT
20067static void
20068write_psymbols (struct mapped_symtab *symtab,
987d643c 20069 htab_t psyms_seen,
9291a0cd
TT
20070 struct partial_symbol **psymp,
20071 int count,
987d643c
TT
20072 offset_type cu_index,
20073 int is_static)
9291a0cd
TT
20074{
20075 for (; count-- > 0; ++psymp)
20076 {
156942c7
DE
20077 struct partial_symbol *psym = *psymp;
20078 void **slot;
987d643c 20079
156942c7 20080 if (SYMBOL_LANGUAGE (psym) == language_ada)
9291a0cd 20081 error (_("Ada is not currently supported by the index"));
987d643c 20082
987d643c 20083 /* Only add a given psymbol once. */
156942c7 20084 slot = htab_find_slot (psyms_seen, psym, INSERT);
987d643c
TT
20085 if (!*slot)
20086 {
156942c7
DE
20087 gdb_index_symbol_kind kind = symbol_kind (psym);
20088
20089 *slot = psym;
20090 add_index_entry (symtab, SYMBOL_SEARCH_NAME (psym),
20091 is_static, kind, cu_index);
987d643c 20092 }
9291a0cd
TT
20093 }
20094}
20095
20096/* Write the contents of an ("unfinished") obstack to FILE. Throw an
20097 exception if there is an error. */
b89be57b 20098
9291a0cd
TT
20099static void
20100write_obstack (FILE *file, struct obstack *obstack)
20101{
20102 if (fwrite (obstack_base (obstack), 1, obstack_object_size (obstack),
20103 file)
20104 != obstack_object_size (obstack))
20105 error (_("couldn't data write to file"));
20106}
20107
20108/* Unlink a file if the argument is not NULL. */
b89be57b 20109
9291a0cd
TT
20110static void
20111unlink_if_set (void *p)
20112{
20113 char **filename = p;
20114 if (*filename)
20115 unlink (*filename);
20116}
20117
1fd400ff
TT
20118/* A helper struct used when iterating over debug_types. */
20119struct signatured_type_index_data
20120{
20121 struct objfile *objfile;
20122 struct mapped_symtab *symtab;
20123 struct obstack *types_list;
987d643c 20124 htab_t psyms_seen;
1fd400ff
TT
20125 int cu_index;
20126};
20127
20128/* A helper function that writes a single signatured_type to an
20129 obstack. */
b89be57b 20130
1fd400ff
TT
20131static int
20132write_one_signatured_type (void **slot, void *d)
20133{
20134 struct signatured_type_index_data *info = d;
20135 struct signatured_type *entry = (struct signatured_type *) *slot;
e254ef6a
DE
20136 struct dwarf2_per_cu_data *per_cu = &entry->per_cu;
20137 struct partial_symtab *psymtab = per_cu->v.psymtab;
1fd400ff
TT
20138 gdb_byte val[8];
20139
20140 write_psymbols (info->symtab,
987d643c 20141 info->psyms_seen,
3e43a32a
MS
20142 info->objfile->global_psymbols.list
20143 + psymtab->globals_offset,
987d643c
TT
20144 psymtab->n_global_syms, info->cu_index,
20145 0);
1fd400ff 20146 write_psymbols (info->symtab,
987d643c 20147 info->psyms_seen,
3e43a32a
MS
20148 info->objfile->static_psymbols.list
20149 + psymtab->statics_offset,
987d643c
TT
20150 psymtab->n_static_syms, info->cu_index,
20151 1);
1fd400ff 20152
b64f50a1
JK
20153 store_unsigned_integer (val, 8, BFD_ENDIAN_LITTLE,
20154 entry->per_cu.offset.sect_off);
1fd400ff 20155 obstack_grow (info->types_list, val, 8);
3019eac3
DE
20156 store_unsigned_integer (val, 8, BFD_ENDIAN_LITTLE,
20157 entry->type_offset_in_tu.cu_off);
1fd400ff
TT
20158 obstack_grow (info->types_list, val, 8);
20159 store_unsigned_integer (val, 8, BFD_ENDIAN_LITTLE, entry->signature);
20160 obstack_grow (info->types_list, val, 8);
20161
20162 ++info->cu_index;
20163
20164 return 1;
20165}
20166
95554aad
TT
20167/* Recurse into all "included" dependencies and write their symbols as
20168 if they appeared in this psymtab. */
20169
20170static void
20171recursively_write_psymbols (struct objfile *objfile,
20172 struct partial_symtab *psymtab,
20173 struct mapped_symtab *symtab,
20174 htab_t psyms_seen,
20175 offset_type cu_index)
20176{
20177 int i;
20178
20179 for (i = 0; i < psymtab->number_of_dependencies; ++i)
20180 if (psymtab->dependencies[i]->user != NULL)
20181 recursively_write_psymbols (objfile, psymtab->dependencies[i],
20182 symtab, psyms_seen, cu_index);
20183
20184 write_psymbols (symtab,
20185 psyms_seen,
20186 objfile->global_psymbols.list + psymtab->globals_offset,
20187 psymtab->n_global_syms, cu_index,
20188 0);
20189 write_psymbols (symtab,
20190 psyms_seen,
20191 objfile->static_psymbols.list + psymtab->statics_offset,
20192 psymtab->n_static_syms, cu_index,
20193 1);
20194}
20195
9291a0cd 20196/* Create an index file for OBJFILE in the directory DIR. */
b89be57b 20197
9291a0cd
TT
20198static void
20199write_psymtabs_to_index (struct objfile *objfile, const char *dir)
20200{
20201 struct cleanup *cleanup;
20202 char *filename, *cleanup_filename;
1fd400ff
TT
20203 struct obstack contents, addr_obstack, constant_pool, symtab_obstack;
20204 struct obstack cu_list, types_cu_list;
9291a0cd
TT
20205 int i;
20206 FILE *out_file;
20207 struct mapped_symtab *symtab;
20208 offset_type val, size_of_contents, total_len;
20209 struct stat st;
987d643c 20210 htab_t psyms_seen;
0a5429f6
DE
20211 htab_t cu_index_htab;
20212 struct psymtab_cu_index_map *psymtab_cu_index_map;
9291a0cd 20213
b4f2f049 20214 if (!objfile->psymtabs || !objfile->psymtabs_addrmap)
9291a0cd 20215 return;
b4f2f049 20216
9291a0cd
TT
20217 if (dwarf2_per_objfile->using_index)
20218 error (_("Cannot use an index to create the index"));
20219
8b70b953
TT
20220 if (VEC_length (dwarf2_section_info_def, dwarf2_per_objfile->types) > 1)
20221 error (_("Cannot make an index when the file has multiple .debug_types sections"));
20222
9291a0cd 20223 if (stat (objfile->name, &st) < 0)
7e17e088 20224 perror_with_name (objfile->name);
9291a0cd
TT
20225
20226 filename = concat (dir, SLASH_STRING, lbasename (objfile->name),
20227 INDEX_SUFFIX, (char *) NULL);
20228 cleanup = make_cleanup (xfree, filename);
20229
20230 out_file = fopen (filename, "wb");
20231 if (!out_file)
20232 error (_("Can't open `%s' for writing"), filename);
20233
20234 cleanup_filename = filename;
20235 make_cleanup (unlink_if_set, &cleanup_filename);
20236
20237 symtab = create_mapped_symtab ();
20238 make_cleanup (cleanup_mapped_symtab, symtab);
20239
20240 obstack_init (&addr_obstack);
20241 make_cleanup_obstack_free (&addr_obstack);
20242
20243 obstack_init (&cu_list);
20244 make_cleanup_obstack_free (&cu_list);
20245
1fd400ff
TT
20246 obstack_init (&types_cu_list);
20247 make_cleanup_obstack_free (&types_cu_list);
20248
987d643c
TT
20249 psyms_seen = htab_create_alloc (100, htab_hash_pointer, htab_eq_pointer,
20250 NULL, xcalloc, xfree);
96408a79 20251 make_cleanup_htab_delete (psyms_seen);
987d643c 20252
0a5429f6
DE
20253 /* While we're scanning CU's create a table that maps a psymtab pointer
20254 (which is what addrmap records) to its index (which is what is recorded
20255 in the index file). This will later be needed to write the address
20256 table. */
20257 cu_index_htab = htab_create_alloc (100,
20258 hash_psymtab_cu_index,
20259 eq_psymtab_cu_index,
20260 NULL, xcalloc, xfree);
96408a79 20261 make_cleanup_htab_delete (cu_index_htab);
0a5429f6
DE
20262 psymtab_cu_index_map = (struct psymtab_cu_index_map *)
20263 xmalloc (sizeof (struct psymtab_cu_index_map)
20264 * dwarf2_per_objfile->n_comp_units);
20265 make_cleanup (xfree, psymtab_cu_index_map);
20266
20267 /* The CU list is already sorted, so we don't need to do additional
1fd400ff
TT
20268 work here. Also, the debug_types entries do not appear in
20269 all_comp_units, but only in their own hash table. */
9291a0cd
TT
20270 for (i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
20271 {
3e43a32a
MS
20272 struct dwarf2_per_cu_data *per_cu
20273 = dwarf2_per_objfile->all_comp_units[i];
e254ef6a 20274 struct partial_symtab *psymtab = per_cu->v.psymtab;
9291a0cd 20275 gdb_byte val[8];
0a5429f6
DE
20276 struct psymtab_cu_index_map *map;
20277 void **slot;
9291a0cd 20278
95554aad
TT
20279 if (psymtab->user == NULL)
20280 recursively_write_psymbols (objfile, psymtab, symtab, psyms_seen, i);
9291a0cd 20281
0a5429f6
DE
20282 map = &psymtab_cu_index_map[i];
20283 map->psymtab = psymtab;
20284 map->cu_index = i;
20285 slot = htab_find_slot (cu_index_htab, map, INSERT);
20286 gdb_assert (slot != NULL);
20287 gdb_assert (*slot == NULL);
20288 *slot = map;
9291a0cd 20289
b64f50a1
JK
20290 store_unsigned_integer (val, 8, BFD_ENDIAN_LITTLE,
20291 per_cu->offset.sect_off);
9291a0cd 20292 obstack_grow (&cu_list, val, 8);
e254ef6a 20293 store_unsigned_integer (val, 8, BFD_ENDIAN_LITTLE, per_cu->length);
9291a0cd
TT
20294 obstack_grow (&cu_list, val, 8);
20295 }
20296
0a5429f6
DE
20297 /* Dump the address map. */
20298 write_address_map (objfile, &addr_obstack, cu_index_htab);
20299
1fd400ff
TT
20300 /* Write out the .debug_type entries, if any. */
20301 if (dwarf2_per_objfile->signatured_types)
20302 {
20303 struct signatured_type_index_data sig_data;
20304
20305 sig_data.objfile = objfile;
20306 sig_data.symtab = symtab;
20307 sig_data.types_list = &types_cu_list;
987d643c 20308 sig_data.psyms_seen = psyms_seen;
1fd400ff
TT
20309 sig_data.cu_index = dwarf2_per_objfile->n_comp_units;
20310 htab_traverse_noresize (dwarf2_per_objfile->signatured_types,
20311 write_one_signatured_type, &sig_data);
20312 }
20313
156942c7
DE
20314 /* Now that we've processed all symbols we can shrink their cu_indices
20315 lists. */
20316 uniquify_cu_indices (symtab);
20317
9291a0cd
TT
20318 obstack_init (&constant_pool);
20319 make_cleanup_obstack_free (&constant_pool);
20320 obstack_init (&symtab_obstack);
20321 make_cleanup_obstack_free (&symtab_obstack);
20322 write_hash_table (symtab, &symtab_obstack, &constant_pool);
20323
20324 obstack_init (&contents);
20325 make_cleanup_obstack_free (&contents);
1fd400ff 20326 size_of_contents = 6 * sizeof (offset_type);
9291a0cd
TT
20327 total_len = size_of_contents;
20328
20329 /* The version number. */
156942c7 20330 val = MAYBE_SWAP (7);
9291a0cd
TT
20331 obstack_grow (&contents, &val, sizeof (val));
20332
20333 /* The offset of the CU list from the start of the file. */
20334 val = MAYBE_SWAP (total_len);
20335 obstack_grow (&contents, &val, sizeof (val));
20336 total_len += obstack_object_size (&cu_list);
20337
1fd400ff
TT
20338 /* The offset of the types CU list from the start of the file. */
20339 val = MAYBE_SWAP (total_len);
20340 obstack_grow (&contents, &val, sizeof (val));
20341 total_len += obstack_object_size (&types_cu_list);
20342
9291a0cd
TT
20343 /* The offset of the address table from the start of the file. */
20344 val = MAYBE_SWAP (total_len);
20345 obstack_grow (&contents, &val, sizeof (val));
20346 total_len += obstack_object_size (&addr_obstack);
20347
20348 /* The offset of the symbol table from the start of the file. */
20349 val = MAYBE_SWAP (total_len);
20350 obstack_grow (&contents, &val, sizeof (val));
20351 total_len += obstack_object_size (&symtab_obstack);
20352
20353 /* The offset of the constant pool from the start of the file. */
20354 val = MAYBE_SWAP (total_len);
20355 obstack_grow (&contents, &val, sizeof (val));
20356 total_len += obstack_object_size (&constant_pool);
20357
20358 gdb_assert (obstack_object_size (&contents) == size_of_contents);
20359
20360 write_obstack (out_file, &contents);
20361 write_obstack (out_file, &cu_list);
1fd400ff 20362 write_obstack (out_file, &types_cu_list);
9291a0cd
TT
20363 write_obstack (out_file, &addr_obstack);
20364 write_obstack (out_file, &symtab_obstack);
20365 write_obstack (out_file, &constant_pool);
20366
20367 fclose (out_file);
20368
20369 /* We want to keep the file, so we set cleanup_filename to NULL
20370 here. See unlink_if_set. */
20371 cleanup_filename = NULL;
20372
20373 do_cleanups (cleanup);
20374}
20375
90476074
TT
20376/* Implementation of the `save gdb-index' command.
20377
20378 Note that the file format used by this command is documented in the
20379 GDB manual. Any changes here must be documented there. */
11570e71 20380
9291a0cd
TT
20381static void
20382save_gdb_index_command (char *arg, int from_tty)
20383{
20384 struct objfile *objfile;
20385
20386 if (!arg || !*arg)
96d19272 20387 error (_("usage: save gdb-index DIRECTORY"));
9291a0cd
TT
20388
20389 ALL_OBJFILES (objfile)
20390 {
20391 struct stat st;
20392
20393 /* If the objfile does not correspond to an actual file, skip it. */
20394 if (stat (objfile->name, &st) < 0)
20395 continue;
20396
20397 dwarf2_per_objfile = objfile_data (objfile, dwarf2_objfile_data_key);
20398 if (dwarf2_per_objfile)
20399 {
20400 volatile struct gdb_exception except;
20401
20402 TRY_CATCH (except, RETURN_MASK_ERROR)
20403 {
20404 write_psymtabs_to_index (objfile, arg);
20405 }
20406 if (except.reason < 0)
20407 exception_fprintf (gdb_stderr, except,
20408 _("Error while writing index for `%s': "),
20409 objfile->name);
20410 }
20411 }
dce234bc
PP
20412}
20413
9291a0cd
TT
20414\f
20415
9eae7c52
TT
20416int dwarf2_always_disassemble;
20417
20418static void
20419show_dwarf2_always_disassemble (struct ui_file *file, int from_tty,
20420 struct cmd_list_element *c, const char *value)
20421{
3e43a32a
MS
20422 fprintf_filtered (file,
20423 _("Whether to always disassemble "
20424 "DWARF expressions is %s.\n"),
9eae7c52
TT
20425 value);
20426}
20427
900e11f9
JK
20428static void
20429show_check_physname (struct ui_file *file, int from_tty,
20430 struct cmd_list_element *c, const char *value)
20431{
20432 fprintf_filtered (file,
20433 _("Whether to check \"physname\" is %s.\n"),
20434 value);
20435}
20436
6502dd73
DJ
20437void _initialize_dwarf2_read (void);
20438
20439void
20440_initialize_dwarf2_read (void)
20441{
96d19272
JK
20442 struct cmd_list_element *c;
20443
dce234bc 20444 dwarf2_objfile_data_key
c1bd65d0 20445 = register_objfile_data_with_cleanup (NULL, dwarf2_per_objfile_free);
ae038cb0 20446
1bedd215
AC
20447 add_prefix_cmd ("dwarf2", class_maintenance, set_dwarf2_cmd, _("\
20448Set DWARF 2 specific variables.\n\
20449Configure DWARF 2 variables such as the cache size"),
ae038cb0
DJ
20450 &set_dwarf2_cmdlist, "maintenance set dwarf2 ",
20451 0/*allow-unknown*/, &maintenance_set_cmdlist);
20452
1bedd215
AC
20453 add_prefix_cmd ("dwarf2", class_maintenance, show_dwarf2_cmd, _("\
20454Show DWARF 2 specific variables\n\
20455Show DWARF 2 variables such as the cache size"),
ae038cb0
DJ
20456 &show_dwarf2_cmdlist, "maintenance show dwarf2 ",
20457 0/*allow-unknown*/, &maintenance_show_cmdlist);
20458
20459 add_setshow_zinteger_cmd ("max-cache-age", class_obscure,
7915a72c
AC
20460 &dwarf2_max_cache_age, _("\
20461Set the upper bound on the age of cached dwarf2 compilation units."), _("\
20462Show the upper bound on the age of cached dwarf2 compilation units."), _("\
20463A higher limit means that cached compilation units will be stored\n\
20464in memory longer, and more total memory will be used. Zero disables\n\
20465caching, which can slow down startup."),
2c5b56ce 20466 NULL,
920d2a44 20467 show_dwarf2_max_cache_age,
2c5b56ce 20468 &set_dwarf2_cmdlist,
ae038cb0 20469 &show_dwarf2_cmdlist);
d97bc12b 20470
9eae7c52
TT
20471 add_setshow_boolean_cmd ("always-disassemble", class_obscure,
20472 &dwarf2_always_disassemble, _("\
20473Set whether `info address' always disassembles DWARF expressions."), _("\
20474Show whether `info address' always disassembles DWARF expressions."), _("\
20475When enabled, DWARF expressions are always printed in an assembly-like\n\
20476syntax. When disabled, expressions will be printed in a more\n\
20477conversational style, when possible."),
20478 NULL,
20479 show_dwarf2_always_disassemble,
20480 &set_dwarf2_cmdlist,
20481 &show_dwarf2_cmdlist);
20482
45cfd468
DE
20483 add_setshow_boolean_cmd ("dwarf2-read", no_class, &dwarf2_read_debug, _("\
20484Set debugging of the dwarf2 reader."), _("\
20485Show debugging of the dwarf2 reader."), _("\
20486When enabled, debugging messages are printed during dwarf2 reading\n\
20487and symtab expansion."),
20488 NULL,
20489 NULL,
20490 &setdebuglist, &showdebuglist);
20491
ccce17b0 20492 add_setshow_zuinteger_cmd ("dwarf2-die", no_class, &dwarf2_die_debug, _("\
d97bc12b
DE
20493Set debugging of the dwarf2 DIE reader."), _("\
20494Show debugging of the dwarf2 DIE reader."), _("\
20495When enabled (non-zero), DIEs are dumped after they are read in.\n\
20496The value is the maximum depth to print."),
ccce17b0
YQ
20497 NULL,
20498 NULL,
20499 &setdebuglist, &showdebuglist);
9291a0cd 20500
900e11f9
JK
20501 add_setshow_boolean_cmd ("check-physname", no_class, &check_physname, _("\
20502Set cross-checking of \"physname\" code against demangler."), _("\
20503Show cross-checking of \"physname\" code against demangler."), _("\
20504When enabled, GDB's internal \"physname\" code is checked against\n\
20505the demangler."),
20506 NULL, show_check_physname,
20507 &setdebuglist, &showdebuglist);
20508
e615022a
DE
20509 add_setshow_boolean_cmd ("use-deprecated-index-sections",
20510 no_class, &use_deprecated_index_sections, _("\
20511Set whether to use deprecated gdb_index sections."), _("\
20512Show whether to use deprecated gdb_index sections."), _("\
20513When enabled, deprecated .gdb_index sections are used anyway.\n\
20514Normally they are ignored either because of a missing feature or\n\
20515performance issue.\n\
20516Warning: This option must be enabled before gdb reads the file."),
20517 NULL,
20518 NULL,
20519 &setlist, &showlist);
20520
96d19272 20521 c = add_cmd ("gdb-index", class_files, save_gdb_index_command,
11570e71 20522 _("\
fc1a9d6e 20523Save a gdb-index file.\n\
11570e71 20524Usage: save gdb-index DIRECTORY"),
96d19272
JK
20525 &save_cmdlist);
20526 set_cmd_completer (c, filename_completer);
6502dd73 20527}
This page took 2.818493 seconds and 4 git commands to generate.