* ada-exp.y (write_object_renaming, write_var_or_type)
[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
DE
259 /* When using index_table, this keeps track of all quick_file_names entries.
260 TUs can share line table entries with CUs or other TUs, and there can be
261 a lot more TUs than unique line tables, so we maintain a separate table
262 of all line table entries to support the sharing. */
263 htab_t quick_file_names_table;
264
98bfdba5
PA
265 /* Set during partial symbol reading, to prevent queueing of full
266 symbols. */
267 int reading_partial_symbols;
673bfd45 268
dee91e82 269 /* Table mapping type DIEs to their struct type *.
673bfd45 270 This is NULL if not allocated yet.
dee91e82
DE
271 The mapping is done via (CU/TU signature + DIE offset) -> type. */
272 htab_t die_type_hash;
95554aad
TT
273
274 /* The CUs we recently read. */
275 VEC (dwarf2_per_cu_ptr) *just_read_cus;
6502dd73
DJ
276};
277
278static struct dwarf2_per_objfile *dwarf2_per_objfile;
c906108c 279
251d32d9 280/* Default names of the debugging sections. */
c906108c 281
233a11ab
CS
282/* Note that if the debugging section has been compressed, it might
283 have a name like .zdebug_info. */
284
9cdd5dbd
DE
285static const struct dwarf2_debug_sections dwarf2_elf_names =
286{
251d32d9
TG
287 { ".debug_info", ".zdebug_info" },
288 { ".debug_abbrev", ".zdebug_abbrev" },
289 { ".debug_line", ".zdebug_line" },
290 { ".debug_loc", ".zdebug_loc" },
291 { ".debug_macinfo", ".zdebug_macinfo" },
cf2c3c16 292 { ".debug_macro", ".zdebug_macro" },
251d32d9
TG
293 { ".debug_str", ".zdebug_str" },
294 { ".debug_ranges", ".zdebug_ranges" },
295 { ".debug_types", ".zdebug_types" },
3019eac3 296 { ".debug_addr", ".zdebug_addr" },
251d32d9
TG
297 { ".debug_frame", ".zdebug_frame" },
298 { ".eh_frame", NULL },
24d3216f
TT
299 { ".gdb_index", ".zgdb_index" },
300 23
251d32d9 301};
c906108c 302
80626a55 303/* List of DWO/DWP sections. */
3019eac3 304
80626a55 305static const struct dwop_section_names
3019eac3
DE
306{
307 struct dwarf2_section_names abbrev_dwo;
308 struct dwarf2_section_names info_dwo;
309 struct dwarf2_section_names line_dwo;
310 struct dwarf2_section_names loc_dwo;
09262596
DE
311 struct dwarf2_section_names macinfo_dwo;
312 struct dwarf2_section_names macro_dwo;
3019eac3
DE
313 struct dwarf2_section_names str_dwo;
314 struct dwarf2_section_names str_offsets_dwo;
315 struct dwarf2_section_names types_dwo;
80626a55
DE
316 struct dwarf2_section_names cu_index;
317 struct dwarf2_section_names tu_index;
3019eac3 318}
80626a55 319dwop_section_names =
3019eac3
DE
320{
321 { ".debug_abbrev.dwo", ".zdebug_abbrev.dwo" },
322 { ".debug_info.dwo", ".zdebug_info.dwo" },
323 { ".debug_line.dwo", ".zdebug_line.dwo" },
324 { ".debug_loc.dwo", ".zdebug_loc.dwo" },
09262596
DE
325 { ".debug_macinfo.dwo", ".zdebug_macinfo.dwo" },
326 { ".debug_macro.dwo", ".zdebug_macro.dwo" },
3019eac3
DE
327 { ".debug_str.dwo", ".zdebug_str.dwo" },
328 { ".debug_str_offsets.dwo", ".zdebug_str_offsets.dwo" },
329 { ".debug_types.dwo", ".zdebug_types.dwo" },
80626a55
DE
330 { ".debug_cu_index", ".zdebug_cu_index" },
331 { ".debug_tu_index", ".zdebug_tu_index" },
3019eac3
DE
332};
333
c906108c
SS
334/* local data types */
335
107d2387
AC
336/* The data in a compilation unit header, after target2host
337 translation, looks like this. */
c906108c 338struct comp_unit_head
a738430d 339{
c764a876 340 unsigned int length;
a738430d 341 short version;
a738430d
MK
342 unsigned char addr_size;
343 unsigned char signed_addr_p;
b64f50a1 344 sect_offset abbrev_offset;
57349743 345
a738430d
MK
346 /* Size of file offsets; either 4 or 8. */
347 unsigned int offset_size;
57349743 348
a738430d
MK
349 /* Size of the length field; either 4 or 12. */
350 unsigned int initial_length_size;
57349743 351
a738430d
MK
352 /* Offset to the first byte of this compilation unit header in the
353 .debug_info section, for resolving relative reference dies. */
b64f50a1 354 sect_offset offset;
57349743 355
d00adf39
DE
356 /* Offset to first die in this cu from the start of the cu.
357 This will be the first byte following the compilation unit header. */
b64f50a1 358 cu_offset first_die_offset;
a738430d 359};
c906108c 360
3da10d80
KS
361/* Type used for delaying computation of method physnames.
362 See comments for compute_delayed_physnames. */
363struct delayed_method_info
364{
365 /* The type to which the method is attached, i.e., its parent class. */
366 struct type *type;
367
368 /* The index of the method in the type's function fieldlists. */
369 int fnfield_index;
370
371 /* The index of the method in the fieldlist. */
372 int index;
373
374 /* The name of the DIE. */
375 const char *name;
376
377 /* The DIE associated with this method. */
378 struct die_info *die;
379};
380
381typedef struct delayed_method_info delayed_method_info;
382DEF_VEC_O (delayed_method_info);
383
e7c27a73
DJ
384/* Internal state when decoding a particular compilation unit. */
385struct dwarf2_cu
386{
387 /* The objfile containing this compilation unit. */
388 struct objfile *objfile;
389
d00adf39 390 /* The header of the compilation unit. */
e7c27a73 391 struct comp_unit_head header;
e142c38c 392
d00adf39
DE
393 /* Base address of this compilation unit. */
394 CORE_ADDR base_address;
395
396 /* Non-zero if base_address has been set. */
397 int base_known;
398
e142c38c
DJ
399 /* The language we are debugging. */
400 enum language language;
401 const struct language_defn *language_defn;
402
b0f35d58
DL
403 const char *producer;
404
e142c38c
DJ
405 /* The generic symbol table building routines have separate lists for
406 file scope symbols and all all other scopes (local scopes). So
407 we need to select the right one to pass to add_symbol_to_list().
408 We do it by keeping a pointer to the correct list in list_in_scope.
409
410 FIXME: The original dwarf code just treated the file scope as the
411 first local scope, and all other local scopes as nested local
412 scopes, and worked fine. Check to see if we really need to
413 distinguish these in buildsym.c. */
414 struct pending **list_in_scope;
415
433df2d4
DE
416 /* The abbrev table for this CU.
417 Normally this points to the abbrev table in the objfile.
418 But if DWO_UNIT is non-NULL this is the abbrev table in the DWO file. */
419 struct abbrev_table *abbrev_table;
72bf9492 420
b64f50a1
JK
421 /* Hash table holding all the loaded partial DIEs
422 with partial_die->offset.SECT_OFF as hash. */
72bf9492
DJ
423 htab_t partial_dies;
424
425 /* Storage for things with the same lifetime as this read-in compilation
426 unit, including partial DIEs. */
427 struct obstack comp_unit_obstack;
428
ae038cb0
DJ
429 /* When multiple dwarf2_cu structures are living in memory, this field
430 chains them all together, so that they can be released efficiently.
431 We will probably also want a generation counter so that most-recently-used
432 compilation units are cached... */
433 struct dwarf2_per_cu_data *read_in_chain;
434
435 /* Backchain to our per_cu entry if the tree has been built. */
436 struct dwarf2_per_cu_data *per_cu;
437
438 /* How many compilation units ago was this CU last referenced? */
439 int last_used;
440
b64f50a1
JK
441 /* A hash table of DIE cu_offset for following references with
442 die_info->offset.sect_off as hash. */
51545339 443 htab_t die_hash;
10b3939b
DJ
444
445 /* Full DIEs if read in. */
446 struct die_info *dies;
447
448 /* A set of pointers to dwarf2_per_cu_data objects for compilation
449 units referenced by this one. Only set during full symbol processing;
450 partial symbol tables do not have dependencies. */
451 htab_t dependencies;
452
cb1df416
DJ
453 /* Header data from the line table, during full symbol processing. */
454 struct line_header *line_header;
455
3da10d80
KS
456 /* A list of methods which need to have physnames computed
457 after all type information has been read. */
458 VEC (delayed_method_info) *method_list;
459
96408a79
SA
460 /* To be copied to symtab->call_site_htab. */
461 htab_t call_site_htab;
462
034e5797
DE
463 /* Non-NULL if this CU came from a DWO file.
464 There is an invariant here that is important to remember:
465 Except for attributes copied from the top level DIE in the "main"
466 (or "stub") file in preparation for reading the DWO file
467 (e.g., DW_AT_GNU_addr_base), we KISS: there is only *one* CU.
468 Either there isn't a DWO file (in which case this is NULL and the point
469 is moot), or there is and either we're not going to read it (in which
470 case this is NULL) or there is and we are reading it (in which case this
471 is non-NULL). */
3019eac3
DE
472 struct dwo_unit *dwo_unit;
473
474 /* The DW_AT_addr_base attribute if present, zero otherwise
475 (zero is a valid value though).
476 Note this value comes from the stub CU/TU's DIE. */
477 ULONGEST addr_base;
478
2e3cf129
DE
479 /* The DW_AT_ranges_base attribute if present, zero otherwise
480 (zero is a valid value though).
481 Note this value comes from the stub CU/TU's DIE.
482 Also note that the value is zero in the non-DWO case so this value can
483 be used without needing to know whether DWO files are in use or not. */
484 ULONGEST ranges_base;
485
ae038cb0
DJ
486 /* Mark used when releasing cached dies. */
487 unsigned int mark : 1;
488
8be455d7
JK
489 /* This CU references .debug_loc. See the symtab->locations_valid field.
490 This test is imperfect as there may exist optimized debug code not using
491 any location list and still facing inlining issues if handled as
492 unoptimized code. For a future better test see GCC PR other/32998. */
8be455d7 493 unsigned int has_loclist : 1;
ba919b58 494
685b1105
JK
495 /* These cache the results for producer_is_gxx_lt_4_6 and producer_is_icc.
496 CHECKED_PRODUCER is set if both PRODUCER_IS_GXX_LT_4_6 and PRODUCER_IS_ICC
497 are valid. This information is cached because profiling CU expansion
498 showed excessive time spent in producer_is_gxx_lt_4_6. */
ba919b58
TT
499 unsigned int checked_producer : 1;
500 unsigned int producer_is_gxx_lt_4_6 : 1;
685b1105 501 unsigned int producer_is_icc : 1;
e7c27a73
DJ
502};
503
10b3939b
DJ
504/* Persistent data held for a compilation unit, even when not
505 processing it. We put a pointer to this structure in the
28dee7f5 506 read_symtab_private field of the psymtab. */
10b3939b 507
ae038cb0
DJ
508struct dwarf2_per_cu_data
509{
36586728 510 /* The start offset and length of this compilation unit.
45452591 511 NOTE: Unlike comp_unit_head.length, this length includes
3019eac3
DE
512 initial_length_size.
513 If the DIE refers to a DWO file, this is always of the original die,
514 not the DWO file. */
b64f50a1 515 sect_offset offset;
36586728 516 unsigned int length;
ae038cb0
DJ
517
518 /* Flag indicating this compilation unit will be read in before
519 any of the current compilation units are processed. */
c764a876 520 unsigned int queued : 1;
ae038cb0 521
0d99eb77
DE
522 /* This flag will be set when reading partial DIEs if we need to load
523 absolutely all DIEs for this compilation unit, instead of just the ones
524 we think are interesting. It gets set if we look for a DIE in the
5afb4e99
DJ
525 hash table and don't find it. */
526 unsigned int load_all_dies : 1;
527
3019eac3
DE
528 /* Non-zero if this CU is from .debug_types. */
529 unsigned int is_debug_types : 1;
530
36586728
TT
531 /* Non-zero if this CU is from the .dwz file. */
532 unsigned int is_dwz : 1;
533
3019eac3
DE
534 /* The section this CU/TU lives in.
535 If the DIE refers to a DWO file, this is always the original die,
536 not the DWO file. */
537 struct dwarf2_section_info *info_or_types_section;
348e048f 538
17ea53c3
JK
539 /* Set to non-NULL iff this CU is currently loaded. When it gets freed out
540 of the CU cache it gets reset to NULL again. */
ae038cb0 541 struct dwarf2_cu *cu;
1c379e20 542
9cdd5dbd
DE
543 /* The corresponding objfile.
544 Normally we can get the objfile from dwarf2_per_objfile.
545 However we can enter this file with just a "per_cu" handle. */
9291a0cd
TT
546 struct objfile *objfile;
547
548 /* When using partial symbol tables, the 'psymtab' field is active.
549 Otherwise the 'quick' field is active. */
550 union
551 {
552 /* The partial symbol table associated with this compilation unit,
95554aad 553 or NULL for unread partial units. */
9291a0cd
TT
554 struct partial_symtab *psymtab;
555
556 /* Data needed by the "quick" functions. */
557 struct dwarf2_per_cu_quick_data *quick;
558 } v;
95554aad 559
f4dc4d17
DE
560 union
561 {
562 /* The CUs we import using DW_TAG_imported_unit. This is filled in
563 while reading psymtabs, used to compute the psymtab dependencies,
564 and then cleared. Then it is filled in again while reading full
565 symbols, and only deleted when the objfile is destroyed. */
566 VEC (dwarf2_per_cu_ptr) *imported_symtabs;
567
568 /* Type units are grouped by their DW_AT_stmt_list entry so that they
569 can share them. If this is a TU, this points to the containing
570 symtab. */
571 struct type_unit_group *type_unit_group;
572 } s;
ae038cb0
DJ
573};
574
348e048f
DE
575/* Entry in the signatured_types hash table. */
576
577struct signatured_type
578{
42e7ad6c
DE
579 /* The "per_cu" object of this type.
580 N.B.: This is the first member so that it's easy to convert pointers
581 between them. */
582 struct dwarf2_per_cu_data per_cu;
583
3019eac3 584 /* The type's signature. */
348e048f
DE
585 ULONGEST signature;
586
3019eac3
DE
587 /* Offset in the TU of the type's DIE, as read from the TU header.
588 If the definition lives in a DWO file, this value is unusable. */
589 cu_offset type_offset_in_tu;
590
591 /* Offset in the section of the type's DIE.
592 If the definition lives in a DWO file, this is the offset in the
593 .debug_types.dwo section.
594 The value is zero until the actual value is known.
595 Zero is otherwise not a valid section offset. */
596 sect_offset type_offset_in_section;
348e048f
DE
597};
598
094b34ac
DE
599/* A struct that can be used as a hash key for tables based on DW_AT_stmt_list.
600 This includes type_unit_group and quick_file_names. */
601
602struct stmt_list_hash
603{
604 /* The DWO unit this table is from or NULL if there is none. */
605 struct dwo_unit *dwo_unit;
606
607 /* Offset in .debug_line or .debug_line.dwo. */
608 sect_offset line_offset;
609};
610
f4dc4d17
DE
611/* Each element of dwarf2_per_objfile->type_unit_groups is a pointer to
612 an object of this type. */
613
614struct type_unit_group
615{
616 /* dwarf2read.c's main "handle" on the symtab.
617 To simplify things we create an artificial CU that "includes" all the
618 type units using this stmt_list so that the rest of the code still has
619 a "per_cu" handle on the symtab.
620 This PER_CU is recognized by having no section. */
621#define IS_TYPE_UNIT_GROUP(per_cu) ((per_cu)->info_or_types_section == NULL)
094b34ac
DE
622 struct dwarf2_per_cu_data per_cu;
623
624 union
625 {
626 /* The TUs that share this DW_AT_stmt_list entry.
627 This is added to while parsing type units to build partial symtabs,
628 and is deleted afterwards and not used again. */
629 VEC (dwarf2_per_cu_ptr) *tus;
f4dc4d17 630
094b34ac
DE
631 /* When reading the line table in "quick" functions, we need a real TU.
632 Any will do, we know they all share the same DW_AT_stmt_list entry.
633 For simplicity's sake, we pick the first one. */
634 struct dwarf2_per_cu_data *first_tu;
635 } t;
f4dc4d17
DE
636
637 /* The primary symtab.
094b34ac
DE
638 Type units in a group needn't all be defined in the same source file,
639 so we create an essentially anonymous symtab as the primary symtab. */
f4dc4d17
DE
640 struct symtab *primary_symtab;
641
094b34ac
DE
642 /* The data used to construct the hash key. */
643 struct stmt_list_hash hash;
f4dc4d17
DE
644
645 /* The number of symtabs from the line header.
646 The value here must match line_header.num_file_names. */
647 unsigned int num_symtabs;
648
649 /* The symbol tables for this TU (obtained from the files listed in
650 DW_AT_stmt_list).
651 WARNING: The order of entries here must match the order of entries
652 in the line header. After the first TU using this type_unit_group, the
653 line header for the subsequent TUs is recreated from this. This is done
654 because we need to use the same symtabs for each TU using the same
655 DW_AT_stmt_list value. Also note that symtabs may be repeated here,
656 there's no guarantee the line header doesn't have duplicate entries. */
657 struct symtab **symtabs;
658};
659
80626a55 660/* These sections are what may appear in a DWO file. */
3019eac3
DE
661
662struct dwo_sections
663{
664 struct dwarf2_section_info abbrev;
3019eac3
DE
665 struct dwarf2_section_info line;
666 struct dwarf2_section_info loc;
09262596
DE
667 struct dwarf2_section_info macinfo;
668 struct dwarf2_section_info macro;
3019eac3
DE
669 struct dwarf2_section_info str;
670 struct dwarf2_section_info str_offsets;
80626a55
DE
671 /* In the case of a virtual DWO file, these two are unused. */
672 struct dwarf2_section_info info;
3019eac3
DE
673 VEC (dwarf2_section_info_def) *types;
674};
675
676/* Common bits of DWO CUs/TUs. */
677
678struct dwo_unit
679{
680 /* Backlink to the containing struct dwo_file. */
681 struct dwo_file *dwo_file;
682
683 /* The "id" that distinguishes this CU/TU.
684 .debug_info calls this "dwo_id", .debug_types calls this "signature".
685 Since signatures came first, we stick with it for consistency. */
686 ULONGEST signature;
687
688 /* The section this CU/TU lives in, in the DWO file. */
689 struct dwarf2_section_info *info_or_types_section;
690
691 /* Same as dwarf2_per_cu_data:{offset,length} but for the DWO section. */
692 sect_offset offset;
693 unsigned int length;
694
695 /* For types, offset in the type's DIE of the type defined by this TU. */
696 cu_offset type_offset_in_tu;
697};
698
80626a55
DE
699/* Data for one DWO file.
700 This includes virtual DWO files that have been packaged into a
701 DWP file. */
3019eac3
DE
702
703struct dwo_file
704{
80626a55
DE
705 /* The DW_AT_GNU_dwo_name attribute. This is the hash key.
706 For virtual DWO files the name is constructed from the section offsets
707 of abbrev,line,loc,str_offsets so that we combine virtual DWO files
708 from related CU+TUs. */
709 const char *name;
3019eac3 710
80626a55
DE
711 /* The bfd, when the file is open. Otherwise this is NULL.
712 This is unused(NULL) for virtual DWO files where we use dwp_file.dbfd. */
713 bfd *dbfd;
3019eac3
DE
714
715 /* Section info for this file. */
716 struct dwo_sections sections;
717
718 /* Table of CUs in the file.
719 Each element is a struct dwo_unit. */
720 htab_t cus;
721
722 /* Table of TUs in the file.
723 Each element is a struct dwo_unit. */
724 htab_t tus;
725};
726
80626a55
DE
727/* These sections are what may appear in a DWP file. */
728
729struct dwp_sections
730{
731 struct dwarf2_section_info str;
732 struct dwarf2_section_info cu_index;
733 struct dwarf2_section_info tu_index;
734 /* The .debug_info.dwo, .debug_types.dwo, and other sections are referenced
735 by section number. We don't need to record them here. */
736};
737
738/* These sections are what may appear in a virtual DWO file. */
739
740struct virtual_dwo_sections
741{
742 struct dwarf2_section_info abbrev;
743 struct dwarf2_section_info line;
744 struct dwarf2_section_info loc;
745 struct dwarf2_section_info macinfo;
746 struct dwarf2_section_info macro;
747 struct dwarf2_section_info str_offsets;
748 /* Each DWP hash table entry records one CU or one TU.
749 That is recorded here, and copied to dwo_unit.info_or_types_section. */
750 struct dwarf2_section_info info_or_types;
751};
752
753/* Contents of DWP hash tables. */
754
755struct dwp_hash_table
756{
757 uint32_t nr_units, nr_slots;
758 const gdb_byte *hash_table, *unit_table, *section_pool;
759};
760
761/* Data for one DWP file. */
762
763struct dwp_file
764{
765 /* Name of the file. */
766 const char *name;
767
768 /* The bfd, when the file is open. Otherwise this is NULL. */
769 bfd *dbfd;
770
771 /* Section info for this file. */
772 struct dwp_sections sections;
773
774 /* Table of CUs in the file. */
775 const struct dwp_hash_table *cus;
776
777 /* Table of TUs in the file. */
778 const struct dwp_hash_table *tus;
779
780 /* Table of loaded CUs/TUs. Each entry is a struct dwo_unit *. */
781 htab_t loaded_cutus;
782
783 /* Table to map ELF section numbers to their sections. */
784 unsigned int num_sections;
785 asection **elf_sections;
786};
787
36586728
TT
788/* This represents a '.dwz' file. */
789
790struct dwz_file
791{
792 /* A dwz file can only contain a few sections. */
793 struct dwarf2_section_info abbrev;
794 struct dwarf2_section_info info;
795 struct dwarf2_section_info str;
796 struct dwarf2_section_info line;
797 struct dwarf2_section_info macro;
2ec9a5e0 798 struct dwarf2_section_info gdb_index;
36586728
TT
799
800 /* The dwz's BFD. */
801 bfd *dwz_bfd;
802};
803
0963b4bd
MS
804/* Struct used to pass misc. parameters to read_die_and_children, et
805 al. which are used for both .debug_info and .debug_types dies.
806 All parameters here are unchanging for the life of the call. This
dee91e82 807 struct exists to abstract away the constant parameters of die reading. */
93311388
DE
808
809struct die_reader_specs
810{
dee91e82 811 /* die_section->asection->owner. */
93311388
DE
812 bfd* abfd;
813
814 /* The CU of the DIE we are parsing. */
815 struct dwarf2_cu *cu;
816
80626a55 817 /* Non-NULL if reading a DWO file (including one packaged into a DWP). */
3019eac3
DE
818 struct dwo_file *dwo_file;
819
dee91e82 820 /* The section the die comes from.
3019eac3 821 This is either .debug_info or .debug_types, or the .dwo variants. */
dee91e82
DE
822 struct dwarf2_section_info *die_section;
823
824 /* die_section->buffer. */
825 gdb_byte *buffer;
f664829e
DE
826
827 /* The end of the buffer. */
828 const gdb_byte *buffer_end;
93311388
DE
829};
830
fd820528 831/* Type of function passed to init_cutu_and_read_dies, et.al. */
dee91e82
DE
832typedef void (die_reader_func_ftype) (const struct die_reader_specs *reader,
833 gdb_byte *info_ptr,
834 struct die_info *comp_unit_die,
835 int has_children,
836 void *data);
837
debd256d
JB
838/* The line number information for a compilation unit (found in the
839 .debug_line section) begins with a "statement program header",
840 which contains the following information. */
841struct line_header
842{
843 unsigned int total_length;
844 unsigned short version;
845 unsigned int header_length;
846 unsigned char minimum_instruction_length;
2dc7f7b3 847 unsigned char maximum_ops_per_instruction;
debd256d
JB
848 unsigned char default_is_stmt;
849 int line_base;
850 unsigned char line_range;
851 unsigned char opcode_base;
852
853 /* standard_opcode_lengths[i] is the number of operands for the
854 standard opcode whose value is i. This means that
855 standard_opcode_lengths[0] is unused, and the last meaningful
856 element is standard_opcode_lengths[opcode_base - 1]. */
857 unsigned char *standard_opcode_lengths;
858
859 /* The include_directories table. NOTE! These strings are not
860 allocated with xmalloc; instead, they are pointers into
861 debug_line_buffer. If you try to free them, `free' will get
862 indigestion. */
863 unsigned int num_include_dirs, include_dirs_size;
864 char **include_dirs;
865
866 /* The file_names table. NOTE! These strings are not allocated
867 with xmalloc; instead, they are pointers into debug_line_buffer.
868 Don't try to free them directly. */
869 unsigned int num_file_names, file_names_size;
870 struct file_entry
c906108c 871 {
debd256d
JB
872 char *name;
873 unsigned int dir_index;
874 unsigned int mod_time;
875 unsigned int length;
aaa75496 876 int included_p; /* Non-zero if referenced by the Line Number Program. */
cb1df416 877 struct symtab *symtab; /* The associated symbol table, if any. */
debd256d
JB
878 } *file_names;
879
880 /* The start and end of the statement program following this
6502dd73 881 header. These point into dwarf2_per_objfile->line_buffer. */
fe1b8b76 882 gdb_byte *statement_program_start, *statement_program_end;
debd256d 883};
c906108c
SS
884
885/* When we construct a partial symbol table entry we only
0963b4bd 886 need this much information. */
c906108c
SS
887struct partial_die_info
888 {
72bf9492 889 /* Offset of this DIE. */
b64f50a1 890 sect_offset offset;
72bf9492
DJ
891
892 /* DWARF-2 tag for this DIE. */
893 ENUM_BITFIELD(dwarf_tag) tag : 16;
894
72bf9492
DJ
895 /* Assorted flags describing the data found in this DIE. */
896 unsigned int has_children : 1;
897 unsigned int is_external : 1;
898 unsigned int is_declaration : 1;
899 unsigned int has_type : 1;
900 unsigned int has_specification : 1;
901 unsigned int has_pc_info : 1;
481860b3 902 unsigned int may_be_inlined : 1;
72bf9492
DJ
903
904 /* Flag set if the SCOPE field of this structure has been
905 computed. */
906 unsigned int scope_set : 1;
907
fa4028e9
JB
908 /* Flag set if the DIE has a byte_size attribute. */
909 unsigned int has_byte_size : 1;
910
98bfdba5
PA
911 /* Flag set if any of the DIE's children are template arguments. */
912 unsigned int has_template_arguments : 1;
913
abc72ce4
DE
914 /* Flag set if fixup_partial_die has been called on this die. */
915 unsigned int fixup_called : 1;
916
36586728
TT
917 /* Flag set if DW_TAG_imported_unit uses DW_FORM_GNU_ref_alt. */
918 unsigned int is_dwz : 1;
919
920 /* Flag set if spec_offset uses DW_FORM_GNU_ref_alt. */
921 unsigned int spec_is_dwz : 1;
922
72bf9492 923 /* The name of this DIE. Normally the value of DW_AT_name, but
94af9270 924 sometimes a default name for unnamed DIEs. */
c906108c 925 char *name;
72bf9492 926
abc72ce4
DE
927 /* The linkage name, if present. */
928 const char *linkage_name;
929
72bf9492
DJ
930 /* The scope to prepend to our children. This is generally
931 allocated on the comp_unit_obstack, so will disappear
932 when this compilation unit leaves the cache. */
933 char *scope;
934
95554aad
TT
935 /* Some data associated with the partial DIE. The tag determines
936 which field is live. */
937 union
938 {
939 /* The location description associated with this DIE, if any. */
940 struct dwarf_block *locdesc;
941 /* The offset of an import, for DW_TAG_imported_unit. */
942 sect_offset offset;
943 } d;
72bf9492
DJ
944
945 /* If HAS_PC_INFO, the PC range associated with this DIE. */
c906108c
SS
946 CORE_ADDR lowpc;
947 CORE_ADDR highpc;
72bf9492 948
93311388 949 /* Pointer into the info_buffer (or types_buffer) pointing at the target of
72bf9492 950 DW_AT_sibling, if any. */
abc72ce4
DE
951 /* NOTE: This member isn't strictly necessary, read_partial_die could
952 return DW_AT_sibling values to its caller load_partial_dies. */
fe1b8b76 953 gdb_byte *sibling;
72bf9492
DJ
954
955 /* If HAS_SPECIFICATION, the offset of the DIE referred to by
956 DW_AT_specification (or DW_AT_abstract_origin or
957 DW_AT_extension). */
b64f50a1 958 sect_offset spec_offset;
72bf9492
DJ
959
960 /* Pointers to this DIE's parent, first child, and next sibling,
961 if any. */
962 struct partial_die_info *die_parent, *die_child, *die_sibling;
c906108c
SS
963 };
964
0963b4bd 965/* This data structure holds the information of an abbrev. */
c906108c
SS
966struct abbrev_info
967 {
968 unsigned int number; /* number identifying abbrev */
969 enum dwarf_tag tag; /* dwarf tag */
f3dd6933
DJ
970 unsigned short has_children; /* boolean */
971 unsigned short num_attrs; /* number of attributes */
c906108c
SS
972 struct attr_abbrev *attrs; /* an array of attribute descriptions */
973 struct abbrev_info *next; /* next in chain */
974 };
975
976struct attr_abbrev
977 {
9d25dd43
DE
978 ENUM_BITFIELD(dwarf_attribute) name : 16;
979 ENUM_BITFIELD(dwarf_form) form : 16;
c906108c
SS
980 };
981
433df2d4
DE
982/* Size of abbrev_table.abbrev_hash_table. */
983#define ABBREV_HASH_SIZE 121
984
985/* Top level data structure to contain an abbreviation table. */
986
987struct abbrev_table
988{
f4dc4d17
DE
989 /* Where the abbrev table came from.
990 This is used as a sanity check when the table is used. */
433df2d4
DE
991 sect_offset offset;
992
993 /* Storage for the abbrev table. */
994 struct obstack abbrev_obstack;
995
996 /* Hash table of abbrevs.
997 This is an array of size ABBREV_HASH_SIZE allocated in abbrev_obstack.
998 It could be statically allocated, but the previous code didn't so we
999 don't either. */
1000 struct abbrev_info **abbrevs;
1001};
1002
0963b4bd 1003/* Attributes have a name and a value. */
b60c80d6
DJ
1004struct attribute
1005 {
9d25dd43 1006 ENUM_BITFIELD(dwarf_attribute) name : 16;
8285870a
JK
1007 ENUM_BITFIELD(dwarf_form) form : 15;
1008
1009 /* Has DW_STRING already been updated by dwarf2_canonicalize_name? This
1010 field should be in u.str (existing only for DW_STRING) but it is kept
1011 here for better struct attribute alignment. */
1012 unsigned int string_is_canonical : 1;
1013
b60c80d6
DJ
1014 union
1015 {
1016 char *str;
1017 struct dwarf_block *blk;
43bbcdc2
PH
1018 ULONGEST unsnd;
1019 LONGEST snd;
b60c80d6 1020 CORE_ADDR addr;
348e048f 1021 struct signatured_type *signatured_type;
b60c80d6
DJ
1022 }
1023 u;
1024 };
1025
0963b4bd 1026/* This data structure holds a complete die structure. */
c906108c
SS
1027struct die_info
1028 {
76815b17
DE
1029 /* DWARF-2 tag for this DIE. */
1030 ENUM_BITFIELD(dwarf_tag) tag : 16;
1031
1032 /* Number of attributes */
98bfdba5
PA
1033 unsigned char num_attrs;
1034
1035 /* True if we're presently building the full type name for the
1036 type derived from this DIE. */
1037 unsigned char building_fullname : 1;
76815b17
DE
1038
1039 /* Abbrev number */
1040 unsigned int abbrev;
1041
93311388 1042 /* Offset in .debug_info or .debug_types section. */
b64f50a1 1043 sect_offset offset;
78ba4af6
JB
1044
1045 /* The dies in a compilation unit form an n-ary tree. PARENT
1046 points to this die's parent; CHILD points to the first child of
1047 this node; and all the children of a given node are chained
4950bc1c 1048 together via their SIBLING fields. */
639d11d3
DC
1049 struct die_info *child; /* Its first child, if any. */
1050 struct die_info *sibling; /* Its next sibling, if any. */
1051 struct die_info *parent; /* Its parent, if any. */
c906108c 1052
b60c80d6
DJ
1053 /* An array of attributes, with NUM_ATTRS elements. There may be
1054 zero, but it's not common and zero-sized arrays are not
1055 sufficiently portable C. */
1056 struct attribute attrs[1];
c906108c
SS
1057 };
1058
0963b4bd 1059/* Get at parts of an attribute structure. */
c906108c
SS
1060
1061#define DW_STRING(attr) ((attr)->u.str)
8285870a 1062#define DW_STRING_IS_CANONICAL(attr) ((attr)->string_is_canonical)
c906108c
SS
1063#define DW_UNSND(attr) ((attr)->u.unsnd)
1064#define DW_BLOCK(attr) ((attr)->u.blk)
1065#define DW_SND(attr) ((attr)->u.snd)
1066#define DW_ADDR(attr) ((attr)->u.addr)
348e048f 1067#define DW_SIGNATURED_TYPE(attr) ((attr)->u.signatured_type)
c906108c 1068
0963b4bd 1069/* Blocks are a bunch of untyped bytes. */
c906108c
SS
1070struct dwarf_block
1071 {
56eb65bd 1072 size_t size;
1d6edc3c
JK
1073
1074 /* Valid only if SIZE is not zero. */
fe1b8b76 1075 gdb_byte *data;
c906108c
SS
1076 };
1077
c906108c
SS
1078#ifndef ATTR_ALLOC_CHUNK
1079#define ATTR_ALLOC_CHUNK 4
1080#endif
1081
c906108c
SS
1082/* Allocate fields for structs, unions and enums in this size. */
1083#ifndef DW_FIELD_ALLOC_CHUNK
1084#define DW_FIELD_ALLOC_CHUNK 4
1085#endif
1086
c906108c
SS
1087/* FIXME: We might want to set this from BFD via bfd_arch_bits_per_byte,
1088 but this would require a corresponding change in unpack_field_as_long
1089 and friends. */
1090static int bits_per_byte = 8;
1091
1092/* The routines that read and process dies for a C struct or C++ class
1093 pass lists of data member fields and lists of member function fields
1094 in an instance of a field_info structure, as defined below. */
1095struct field_info
c5aa993b 1096 {
0963b4bd 1097 /* List of data member and baseclasses fields. */
c5aa993b
JM
1098 struct nextfield
1099 {
1100 struct nextfield *next;
1101 int accessibility;
1102 int virtuality;
1103 struct field field;
1104 }
7d0ccb61 1105 *fields, *baseclasses;
c906108c 1106
7d0ccb61 1107 /* Number of fields (including baseclasses). */
c5aa993b 1108 int nfields;
c906108c 1109
c5aa993b
JM
1110 /* Number of baseclasses. */
1111 int nbaseclasses;
c906108c 1112
c5aa993b
JM
1113 /* Set if the accesibility of one of the fields is not public. */
1114 int non_public_fields;
c906108c 1115
c5aa993b
JM
1116 /* Member function fields array, entries are allocated in the order they
1117 are encountered in the object file. */
1118 struct nextfnfield
1119 {
1120 struct nextfnfield *next;
1121 struct fn_field fnfield;
1122 }
1123 *fnfields;
c906108c 1124
c5aa993b
JM
1125 /* Member function fieldlist array, contains name of possibly overloaded
1126 member function, number of overloaded member functions and a pointer
1127 to the head of the member function field chain. */
1128 struct fnfieldlist
1129 {
1130 char *name;
1131 int length;
1132 struct nextfnfield *head;
1133 }
1134 *fnfieldlists;
c906108c 1135
c5aa993b
JM
1136 /* Number of entries in the fnfieldlists array. */
1137 int nfnfields;
98751a41
JK
1138
1139 /* typedefs defined inside this class. TYPEDEF_FIELD_LIST contains head of
1140 a NULL terminated list of TYPEDEF_FIELD_LIST_COUNT elements. */
1141 struct typedef_field_list
1142 {
1143 struct typedef_field field;
1144 struct typedef_field_list *next;
1145 }
1146 *typedef_field_list;
1147 unsigned typedef_field_list_count;
c5aa993b 1148 };
c906108c 1149
10b3939b
DJ
1150/* One item on the queue of compilation units to read in full symbols
1151 for. */
1152struct dwarf2_queue_item
1153{
1154 struct dwarf2_per_cu_data *per_cu;
95554aad 1155 enum language pretend_language;
10b3939b
DJ
1156 struct dwarf2_queue_item *next;
1157};
1158
1159/* The current queue. */
1160static struct dwarf2_queue_item *dwarf2_queue, *dwarf2_queue_tail;
1161
ae038cb0
DJ
1162/* Loaded secondary compilation units are kept in memory until they
1163 have not been referenced for the processing of this many
1164 compilation units. Set this to zero to disable caching. Cache
1165 sizes of up to at least twenty will improve startup time for
1166 typical inter-CU-reference binaries, at an obvious memory cost. */
1167static int dwarf2_max_cache_age = 5;
920d2a44
AC
1168static void
1169show_dwarf2_max_cache_age (struct ui_file *file, int from_tty,
1170 struct cmd_list_element *c, const char *value)
1171{
3e43a32a
MS
1172 fprintf_filtered (file, _("The upper bound on the age of cached "
1173 "dwarf2 compilation units is %s.\n"),
920d2a44
AC
1174 value);
1175}
1176
ae038cb0 1177
0963b4bd 1178/* Various complaints about symbol reading that don't abort the process. */
c906108c 1179
4d3c2250
KB
1180static void
1181dwarf2_statement_list_fits_in_line_number_section_complaint (void)
2e276125 1182{
4d3c2250 1183 complaint (&symfile_complaints,
e2e0b3e5 1184 _("statement list doesn't fit in .debug_line section"));
4d3c2250
KB
1185}
1186
25e43795
DJ
1187static void
1188dwarf2_debug_line_missing_file_complaint (void)
1189{
1190 complaint (&symfile_complaints,
1191 _(".debug_line section has line data without a file"));
1192}
1193
59205f5a
JB
1194static void
1195dwarf2_debug_line_missing_end_sequence_complaint (void)
1196{
1197 complaint (&symfile_complaints,
3e43a32a
MS
1198 _(".debug_line section has line "
1199 "program sequence without an end"));
59205f5a
JB
1200}
1201
4d3c2250
KB
1202static void
1203dwarf2_complex_location_expr_complaint (void)
2e276125 1204{
e2e0b3e5 1205 complaint (&symfile_complaints, _("location expression too complex"));
4d3c2250
KB
1206}
1207
4d3c2250
KB
1208static void
1209dwarf2_const_value_length_mismatch_complaint (const char *arg1, int arg2,
1210 int arg3)
2e276125 1211{
4d3c2250 1212 complaint (&symfile_complaints,
3e43a32a
MS
1213 _("const value length mismatch for '%s', got %d, expected %d"),
1214 arg1, arg2, arg3);
4d3c2250
KB
1215}
1216
1217static void
f664829e 1218dwarf2_section_buffer_overflow_complaint (struct dwarf2_section_info *section)
2e276125 1219{
4d3c2250 1220 complaint (&symfile_complaints,
f664829e
DE
1221 _("debug info runs off end of %s section"
1222 " [in module %s]"),
1223 section->asection->name,
1224 bfd_get_filename (section->asection->owner));
4d3c2250
KB
1225}
1226
1227static void
1228dwarf2_macro_malformed_definition_complaint (const char *arg1)
8e19ed76 1229{
4d3c2250 1230 complaint (&symfile_complaints,
3e43a32a
MS
1231 _("macro debug info contains a "
1232 "malformed macro definition:\n`%s'"),
4d3c2250
KB
1233 arg1);
1234}
1235
1236static void
1237dwarf2_invalid_attrib_class_complaint (const char *arg1, const char *arg2)
8b2dbe47 1238{
4d3c2250 1239 complaint (&symfile_complaints,
3e43a32a
MS
1240 _("invalid attribute class or form for '%s' in '%s'"),
1241 arg1, arg2);
4d3c2250 1242}
c906108c 1243
c906108c
SS
1244/* local function prototypes */
1245
4efb68b1 1246static void dwarf2_locate_sections (bfd *, asection *, void *);
c906108c 1247
aaa75496
JB
1248static void dwarf2_create_include_psymtab (char *, struct partial_symtab *,
1249 struct objfile *);
1250
918dd910
JK
1251static void dwarf2_find_base_address (struct die_info *die,
1252 struct dwarf2_cu *cu);
1253
c67a9c90 1254static void dwarf2_build_psymtabs_hard (struct objfile *);
c906108c 1255
72bf9492
DJ
1256static void scan_partial_symbols (struct partial_die_info *,
1257 CORE_ADDR *, CORE_ADDR *,
5734ee8b 1258 int, struct dwarf2_cu *);
c906108c 1259
72bf9492
DJ
1260static void add_partial_symbol (struct partial_die_info *,
1261 struct dwarf2_cu *);
63d06c5c 1262
72bf9492
DJ
1263static void add_partial_namespace (struct partial_die_info *pdi,
1264 CORE_ADDR *lowpc, CORE_ADDR *highpc,
5734ee8b 1265 int need_pc, struct dwarf2_cu *cu);
63d06c5c 1266
5d7cb8df
JK
1267static void add_partial_module (struct partial_die_info *pdi, CORE_ADDR *lowpc,
1268 CORE_ADDR *highpc, int need_pc,
1269 struct dwarf2_cu *cu);
1270
72bf9492
DJ
1271static void add_partial_enumeration (struct partial_die_info *enum_pdi,
1272 struct dwarf2_cu *cu);
91c24f0a 1273
bc30ff58
JB
1274static void add_partial_subprogram (struct partial_die_info *pdi,
1275 CORE_ADDR *lowpc, CORE_ADDR *highpc,
5734ee8b 1276 int need_pc, struct dwarf2_cu *cu);
bc30ff58 1277
a14ed312 1278static void dwarf2_psymtab_to_symtab (struct partial_symtab *);
c906108c 1279
a14ed312 1280static void psymtab_to_symtab_1 (struct partial_symtab *);
c906108c 1281
433df2d4
DE
1282static struct abbrev_info *abbrev_table_lookup_abbrev
1283 (const struct abbrev_table *, unsigned int);
1284
1285static struct abbrev_table *abbrev_table_read_table
1286 (struct dwarf2_section_info *, sect_offset);
1287
1288static void abbrev_table_free (struct abbrev_table *);
1289
f4dc4d17
DE
1290static void abbrev_table_free_cleanup (void *);
1291
dee91e82
DE
1292static void dwarf2_read_abbrevs (struct dwarf2_cu *,
1293 struct dwarf2_section_info *);
c906108c 1294
f3dd6933 1295static void dwarf2_free_abbrev_table (void *);
c906108c 1296
6caca83c
CC
1297static unsigned int peek_abbrev_code (bfd *, gdb_byte *);
1298
dee91e82
DE
1299static struct partial_die_info *load_partial_dies
1300 (const struct die_reader_specs *, gdb_byte *, int);
72bf9492 1301
dee91e82
DE
1302static gdb_byte *read_partial_die (const struct die_reader_specs *,
1303 struct partial_die_info *,
1304 struct abbrev_info *,
1305 unsigned int,
1306 gdb_byte *);
c906108c 1307
36586728 1308static struct partial_die_info *find_partial_die (sect_offset, int,
10b3939b 1309 struct dwarf2_cu *);
72bf9492
DJ
1310
1311static void fixup_partial_die (struct partial_die_info *,
1312 struct dwarf2_cu *);
1313
dee91e82
DE
1314static gdb_byte *read_attribute (const struct die_reader_specs *,
1315 struct attribute *, struct attr_abbrev *,
1316 gdb_byte *);
a8329558 1317
a1855c1d 1318static unsigned int read_1_byte (bfd *, const gdb_byte *);
c906108c 1319
a1855c1d 1320static int read_1_signed_byte (bfd *, const gdb_byte *);
c906108c 1321
a1855c1d 1322static unsigned int read_2_bytes (bfd *, const gdb_byte *);
c906108c 1323
a1855c1d 1324static unsigned int read_4_bytes (bfd *, const gdb_byte *);
c906108c 1325
a1855c1d 1326static ULONGEST read_8_bytes (bfd *, const gdb_byte *);
c906108c 1327
fe1b8b76 1328static CORE_ADDR read_address (bfd *, gdb_byte *ptr, struct dwarf2_cu *,
891d2f0b 1329 unsigned int *);
c906108c 1330
c764a876
DE
1331static LONGEST read_initial_length (bfd *, gdb_byte *, unsigned int *);
1332
1333static LONGEST read_checked_initial_length_and_offset
1334 (bfd *, gdb_byte *, const struct comp_unit_head *,
1335 unsigned int *, unsigned int *);
613e1657 1336
fe1b8b76 1337static LONGEST read_offset (bfd *, gdb_byte *, const struct comp_unit_head *,
c764a876
DE
1338 unsigned int *);
1339
1340static LONGEST read_offset_1 (bfd *, gdb_byte *, unsigned int);
613e1657 1341
f4dc4d17
DE
1342static sect_offset read_abbrev_offset (struct dwarf2_section_info *,
1343 sect_offset);
1344
fe1b8b76 1345static gdb_byte *read_n_bytes (bfd *, gdb_byte *, unsigned int);
c906108c 1346
9b1c24c8 1347static char *read_direct_string (bfd *, gdb_byte *, unsigned int *);
c906108c 1348
fe1b8b76
JB
1349static char *read_indirect_string (bfd *, gdb_byte *,
1350 const struct comp_unit_head *,
1351 unsigned int *);
4bdf3d34 1352
36586728
TT
1353static char *read_indirect_string_from_dwz (struct dwz_file *, LONGEST);
1354
12df843f 1355static ULONGEST read_unsigned_leb128 (bfd *, gdb_byte *, unsigned int *);
c906108c 1356
12df843f 1357static LONGEST read_signed_leb128 (bfd *, gdb_byte *, unsigned int *);
c906108c 1358
3019eac3
DE
1359static CORE_ADDR read_addr_index_from_leb128 (struct dwarf2_cu *, gdb_byte *,
1360 unsigned int *);
1361
1362static char *read_str_index (const struct die_reader_specs *reader,
1363 struct dwarf2_cu *cu, ULONGEST str_index);
1364
e142c38c 1365static void set_cu_language (unsigned int, struct dwarf2_cu *);
c906108c 1366
e142c38c
DJ
1367static struct attribute *dwarf2_attr (struct die_info *, unsigned int,
1368 struct dwarf2_cu *);
c906108c 1369
348e048f 1370static struct attribute *dwarf2_attr_no_follow (struct die_info *,
45e58e77 1371 unsigned int);
348e048f 1372
05cf31d1
JB
1373static int dwarf2_flag_true_p (struct die_info *die, unsigned name,
1374 struct dwarf2_cu *cu);
1375
e142c38c 1376static int die_is_declaration (struct die_info *, struct dwarf2_cu *cu);
3ca72b44 1377
e142c38c 1378static struct die_info *die_specification (struct die_info *die,
f2f0e013 1379 struct dwarf2_cu **);
63d06c5c 1380
debd256d
JB
1381static void free_line_header (struct line_header *lh);
1382
aaa75496
JB
1383static void add_file_name (struct line_header *, char *, unsigned int,
1384 unsigned int, unsigned int);
1385
3019eac3
DE
1386static struct line_header *dwarf_decode_line_header (unsigned int offset,
1387 struct dwarf2_cu *cu);
debd256d 1388
f3f5162e
DE
1389static void dwarf_decode_lines (struct line_header *, const char *,
1390 struct dwarf2_cu *, struct partial_symtab *,
1391 int);
c906108c 1392
72b9f47f 1393static void dwarf2_start_subfile (char *, const char *, const char *);
c906108c 1394
f4dc4d17
DE
1395static void dwarf2_start_symtab (struct dwarf2_cu *,
1396 char *, char *, CORE_ADDR);
1397
a14ed312 1398static struct symbol *new_symbol (struct die_info *, struct type *,
e7c27a73 1399 struct dwarf2_cu *);
c906108c 1400
34eaf542
TT
1401static struct symbol *new_symbol_full (struct die_info *, struct type *,
1402 struct dwarf2_cu *, struct symbol *);
1403
a14ed312 1404static void dwarf2_const_value (struct attribute *, struct symbol *,
e7c27a73 1405 struct dwarf2_cu *);
c906108c 1406
98bfdba5
PA
1407static void dwarf2_const_value_attr (struct attribute *attr,
1408 struct type *type,
1409 const char *name,
1410 struct obstack *obstack,
12df843f 1411 struct dwarf2_cu *cu, LONGEST *value,
98bfdba5
PA
1412 gdb_byte **bytes,
1413 struct dwarf2_locexpr_baton **baton);
2df3850c 1414
e7c27a73 1415static struct type *die_type (struct die_info *, struct dwarf2_cu *);
c906108c 1416
b4ba55a1
JB
1417static int need_gnat_info (struct dwarf2_cu *);
1418
3e43a32a
MS
1419static struct type *die_descriptive_type (struct die_info *,
1420 struct dwarf2_cu *);
b4ba55a1
JB
1421
1422static void set_descriptive_type (struct type *, struct die_info *,
1423 struct dwarf2_cu *);
1424
e7c27a73
DJ
1425static struct type *die_containing_type (struct die_info *,
1426 struct dwarf2_cu *);
c906108c 1427
673bfd45
DE
1428static struct type *lookup_die_type (struct die_info *, struct attribute *,
1429 struct dwarf2_cu *);
c906108c 1430
f792889a 1431static struct type *read_type_die (struct die_info *, struct dwarf2_cu *);
c906108c 1432
673bfd45
DE
1433static struct type *read_type_die_1 (struct die_info *, struct dwarf2_cu *);
1434
0d5cff50 1435static const char *determine_prefix (struct die_info *die, struct dwarf2_cu *);
63d06c5c 1436
6e70227d 1437static char *typename_concat (struct obstack *obs, const char *prefix,
f55ee35c
JK
1438 const char *suffix, int physname,
1439 struct dwarf2_cu *cu);
63d06c5c 1440
e7c27a73 1441static void read_file_scope (struct die_info *, struct dwarf2_cu *);
c906108c 1442
348e048f
DE
1443static void read_type_unit_scope (struct die_info *, struct dwarf2_cu *);
1444
e7c27a73 1445static void read_func_scope (struct die_info *, struct dwarf2_cu *);
c906108c 1446
e7c27a73 1447static void read_lexical_block_scope (struct die_info *, struct dwarf2_cu *);
c906108c 1448
96408a79
SA
1449static void read_call_site_scope (struct die_info *die, struct dwarf2_cu *cu);
1450
ff013f42
JK
1451static int dwarf2_ranges_read (unsigned, CORE_ADDR *, CORE_ADDR *,
1452 struct dwarf2_cu *, struct partial_symtab *);
1453
a14ed312 1454static int dwarf2_get_pc_bounds (struct die_info *,
d85a05f0
DJ
1455 CORE_ADDR *, CORE_ADDR *, struct dwarf2_cu *,
1456 struct partial_symtab *);
c906108c 1457
fae299cd
DC
1458static void get_scope_pc_bounds (struct die_info *,
1459 CORE_ADDR *, CORE_ADDR *,
1460 struct dwarf2_cu *);
1461
801e3a5b
JB
1462static void dwarf2_record_block_ranges (struct die_info *, struct block *,
1463 CORE_ADDR, struct dwarf2_cu *);
1464
a14ed312 1465static void dwarf2_add_field (struct field_info *, struct die_info *,
e7c27a73 1466 struct dwarf2_cu *);
c906108c 1467
a14ed312 1468static void dwarf2_attach_fields_to_type (struct field_info *,
e7c27a73 1469 struct type *, struct dwarf2_cu *);
c906108c 1470
a14ed312 1471static void dwarf2_add_member_fn (struct field_info *,
e26fb1d7 1472 struct die_info *, struct type *,
e7c27a73 1473 struct dwarf2_cu *);
c906108c 1474
a14ed312 1475static void dwarf2_attach_fn_fields_to_type (struct field_info *,
3e43a32a
MS
1476 struct type *,
1477 struct dwarf2_cu *);
c906108c 1478
134d01f1 1479static void process_structure_scope (struct die_info *, struct dwarf2_cu *);
c906108c 1480
e7c27a73 1481static void read_common_block (struct die_info *, struct dwarf2_cu *);
c906108c 1482
e7c27a73 1483static void read_namespace (struct die_info *die, struct dwarf2_cu *);
d9fa45fe 1484
5d7cb8df
JK
1485static void read_module (struct die_info *die, struct dwarf2_cu *cu);
1486
27aa8d6a
SW
1487static void read_import_statement (struct die_info *die, struct dwarf2_cu *);
1488
f55ee35c
JK
1489static struct type *read_module_type (struct die_info *die,
1490 struct dwarf2_cu *cu);
1491
38d518c9 1492static const char *namespace_name (struct die_info *die,
e142c38c 1493 int *is_anonymous, struct dwarf2_cu *);
38d518c9 1494
134d01f1 1495static void process_enumeration_scope (struct die_info *, struct dwarf2_cu *);
c906108c 1496
e7c27a73 1497static CORE_ADDR decode_locdesc (struct dwarf_block *, struct dwarf2_cu *);
c906108c 1498
6e70227d 1499static enum dwarf_array_dim_ordering read_array_order (struct die_info *,
7ca2d3a3
DL
1500 struct dwarf2_cu *);
1501
dee91e82 1502static struct die_info *read_die_and_children (const struct die_reader_specs *,
93311388 1503 gdb_byte *info_ptr,
fe1b8b76 1504 gdb_byte **new_info_ptr,
639d11d3
DC
1505 struct die_info *parent);
1506
dee91e82 1507static struct die_info *read_die_and_siblings (const struct die_reader_specs *,
93311388 1508 gdb_byte *info_ptr,
fe1b8b76 1509 gdb_byte **new_info_ptr,
639d11d3
DC
1510 struct die_info *parent);
1511
3019eac3
DE
1512static gdb_byte *read_full_die_1 (const struct die_reader_specs *,
1513 struct die_info **, gdb_byte *, int *, int);
1514
dee91e82
DE
1515static gdb_byte *read_full_die (const struct die_reader_specs *,
1516 struct die_info **, gdb_byte *, int *);
93311388 1517
e7c27a73 1518static void process_die (struct die_info *, struct dwarf2_cu *);
c906108c 1519
71c25dea
TT
1520static char *dwarf2_canonicalize_name (char *, struct dwarf2_cu *,
1521 struct obstack *);
1522
e142c38c 1523static char *dwarf2_name (struct die_info *die, struct dwarf2_cu *);
9219021c 1524
98bfdba5
PA
1525static const char *dwarf2_full_name (char *name,
1526 struct die_info *die,
1527 struct dwarf2_cu *cu);
1528
e142c38c 1529static struct die_info *dwarf2_extension (struct die_info *die,
f2f0e013 1530 struct dwarf2_cu **);
9219021c 1531
f39c6ffd 1532static const char *dwarf_tag_name (unsigned int);
c906108c 1533
f39c6ffd 1534static const char *dwarf_attr_name (unsigned int);
c906108c 1535
f39c6ffd 1536static const char *dwarf_form_name (unsigned int);
c906108c 1537
a14ed312 1538static char *dwarf_bool_name (unsigned int);
c906108c 1539
f39c6ffd 1540static const char *dwarf_type_encoding_name (unsigned int);
c906108c 1541
f9aca02d 1542static struct die_info *sibling_die (struct die_info *);
c906108c 1543
d97bc12b
DE
1544static void dump_die_shallow (struct ui_file *, int indent, struct die_info *);
1545
1546static void dump_die_for_error (struct die_info *);
1547
1548static void dump_die_1 (struct ui_file *, int level, int max_level,
1549 struct die_info *);
c906108c 1550
d97bc12b 1551/*static*/ void dump_die (struct die_info *, int max_level);
c906108c 1552
51545339 1553static void store_in_ref_table (struct die_info *,
10b3939b 1554 struct dwarf2_cu *);
c906108c 1555
93311388
DE
1556static int is_ref_attr (struct attribute *);
1557
b64f50a1 1558static sect_offset dwarf2_get_ref_die_offset (struct attribute *);
c906108c 1559
43bbcdc2 1560static LONGEST dwarf2_get_attr_constant_value (struct attribute *, int);
a02abb62 1561
348e048f
DE
1562static struct die_info *follow_die_ref_or_sig (struct die_info *,
1563 struct attribute *,
1564 struct dwarf2_cu **);
1565
10b3939b
DJ
1566static struct die_info *follow_die_ref (struct die_info *,
1567 struct attribute *,
f2f0e013 1568 struct dwarf2_cu **);
c906108c 1569
348e048f
DE
1570static struct die_info *follow_die_sig (struct die_info *,
1571 struct attribute *,
1572 struct dwarf2_cu **);
1573
6c83ed52
TT
1574static struct signatured_type *lookup_signatured_type_at_offset
1575 (struct objfile *objfile,
b64f50a1 1576 struct dwarf2_section_info *section, sect_offset offset);
6c83ed52 1577
e5fe5e75 1578static void load_full_type_unit (struct dwarf2_per_cu_data *per_cu);
348e048f 1579
52dc124a 1580static void read_signatured_type (struct signatured_type *);
348e048f 1581
f4dc4d17 1582static struct type_unit_group *get_type_unit_group
094b34ac 1583 (struct dwarf2_cu *, struct attribute *);
f4dc4d17
DE
1584
1585static void build_type_unit_groups (die_reader_func_ftype *, void *);
1586
c906108c
SS
1587/* memory allocation interface */
1588
7b5a2f43 1589static struct dwarf_block *dwarf_alloc_block (struct dwarf2_cu *);
c906108c 1590
b60c80d6 1591static struct die_info *dwarf_alloc_die (struct dwarf2_cu *, int);
c906108c 1592
09262596
DE
1593static void dwarf_decode_macros (struct dwarf2_cu *, unsigned int,
1594 char *, int);
2e276125 1595
8e19ed76
PS
1596static int attr_form_is_block (struct attribute *);
1597
3690dd37
JB
1598static int attr_form_is_section_offset (struct attribute *);
1599
1600static int attr_form_is_constant (struct attribute *);
1601
8cf6f0b1
TT
1602static void fill_in_loclist_baton (struct dwarf2_cu *cu,
1603 struct dwarf2_loclist_baton *baton,
1604 struct attribute *attr);
1605
93e7bd98
DJ
1606static void dwarf2_symbol_mark_computed (struct attribute *attr,
1607 struct symbol *sym,
1608 struct dwarf2_cu *cu);
4c2df51b 1609
dee91e82
DE
1610static gdb_byte *skip_one_die (const struct die_reader_specs *reader,
1611 gdb_byte *info_ptr,
1612 struct abbrev_info *abbrev);
4bb7a0a7 1613
72bf9492
DJ
1614static void free_stack_comp_unit (void *);
1615
72bf9492
DJ
1616static hashval_t partial_die_hash (const void *item);
1617
1618static int partial_die_eq (const void *item_lhs, const void *item_rhs);
1619
ae038cb0 1620static struct dwarf2_per_cu_data *dwarf2_find_containing_comp_unit
36586728 1621 (sect_offset offset, unsigned int offset_in_dwz, struct objfile *objfile);
ae038cb0 1622
9816fde3 1623static void init_one_comp_unit (struct dwarf2_cu *cu,
23745b47 1624 struct dwarf2_per_cu_data *per_cu);
9816fde3
JK
1625
1626static void prepare_one_comp_unit (struct dwarf2_cu *cu,
95554aad
TT
1627 struct die_info *comp_unit_die,
1628 enum language pretend_language);
93311388 1629
68dc6402 1630static void free_heap_comp_unit (void *);
ae038cb0
DJ
1631
1632static void free_cached_comp_units (void *);
1633
1634static void age_cached_comp_units (void);
1635
dee91e82 1636static void free_one_cached_comp_unit (struct dwarf2_per_cu_data *);
ae038cb0 1637
f792889a
DJ
1638static struct type *set_die_type (struct die_info *, struct type *,
1639 struct dwarf2_cu *);
1c379e20 1640
ae038cb0
DJ
1641static void create_all_comp_units (struct objfile *);
1642
0e50663e 1643static int create_all_type_units (struct objfile *);
1fd400ff 1644
95554aad
TT
1645static void load_full_comp_unit (struct dwarf2_per_cu_data *,
1646 enum language);
10b3939b 1647
95554aad
TT
1648static void process_full_comp_unit (struct dwarf2_per_cu_data *,
1649 enum language);
10b3939b 1650
f4dc4d17
DE
1651static void process_full_type_unit (struct dwarf2_per_cu_data *,
1652 enum language);
1653
10b3939b
DJ
1654static void dwarf2_add_dependence (struct dwarf2_cu *,
1655 struct dwarf2_per_cu_data *);
1656
ae038cb0
DJ
1657static void dwarf2_mark (struct dwarf2_cu *);
1658
1659static void dwarf2_clear_marks (struct dwarf2_per_cu_data *);
1660
b64f50a1 1661static struct type *get_die_type_at_offset (sect_offset,
673bfd45
DE
1662 struct dwarf2_per_cu_data *per_cu);
1663
f792889a 1664static struct type *get_die_type (struct die_info *die, struct dwarf2_cu *cu);
72019c9c 1665
9291a0cd
TT
1666static void dwarf2_release_queue (void *dummy);
1667
95554aad
TT
1668static void queue_comp_unit (struct dwarf2_per_cu_data *per_cu,
1669 enum language pretend_language);
1670
1671static int maybe_queue_comp_unit (struct dwarf2_cu *this_cu,
1672 struct dwarf2_per_cu_data *per_cu,
1673 enum language pretend_language);
9291a0cd 1674
a0f42c21 1675static void process_queue (void);
9291a0cd
TT
1676
1677static void find_file_and_directory (struct die_info *die,
1678 struct dwarf2_cu *cu,
1679 char **name, char **comp_dir);
1680
1681static char *file_full_name (int file, struct line_header *lh,
1682 const char *comp_dir);
1683
36586728
TT
1684static gdb_byte *read_and_check_comp_unit_head
1685 (struct comp_unit_head *header,
1686 struct dwarf2_section_info *section,
1687 struct dwarf2_section_info *abbrev_section, gdb_byte *info_ptr,
1688 int is_debug_types_section);
1689
fd820528 1690static void init_cutu_and_read_dies
f4dc4d17
DE
1691 (struct dwarf2_per_cu_data *this_cu, struct abbrev_table *abbrev_table,
1692 int use_existing_cu, int keep,
3019eac3
DE
1693 die_reader_func_ftype *die_reader_func, void *data);
1694
dee91e82
DE
1695static void init_cutu_and_read_dies_simple
1696 (struct dwarf2_per_cu_data *this_cu,
1697 die_reader_func_ftype *die_reader_func, void *data);
9291a0cd 1698
673bfd45 1699static htab_t allocate_signatured_type_table (struct objfile *objfile);
1fd400ff 1700
3019eac3
DE
1701static htab_t allocate_dwo_unit_table (struct objfile *objfile);
1702
1703static struct dwo_unit *lookup_dwo_comp_unit
a1855c1d 1704 (struct dwarf2_per_cu_data *, const char *, const char *, ULONGEST);
3019eac3
DE
1705
1706static struct dwo_unit *lookup_dwo_type_unit
a1855c1d 1707 (struct signatured_type *, const char *, const char *);
3019eac3
DE
1708
1709static void free_dwo_file_cleanup (void *);
1710
95554aad
TT
1711static void process_cu_includes (void);
1712
9291a0cd
TT
1713#if WORDS_BIGENDIAN
1714
1715/* Convert VALUE between big- and little-endian. */
1716static offset_type
1717byte_swap (offset_type value)
1718{
1719 offset_type result;
1720
1721 result = (value & 0xff) << 24;
1722 result |= (value & 0xff00) << 8;
1723 result |= (value & 0xff0000) >> 8;
1724 result |= (value & 0xff000000) >> 24;
1725 return result;
1726}
1727
1728#define MAYBE_SWAP(V) byte_swap (V)
1729
1730#else
1731#define MAYBE_SWAP(V) (V)
1732#endif /* WORDS_BIGENDIAN */
1733
1734/* The suffix for an index file. */
1735#define INDEX_SUFFIX ".gdb-index"
1736
3da10d80
KS
1737static const char *dwarf2_physname (char *name, struct die_info *die,
1738 struct dwarf2_cu *cu);
1739
c906108c 1740/* Try to locate the sections we need for DWARF 2 debugging
251d32d9
TG
1741 information and return true if we have enough to do something.
1742 NAMES points to the dwarf2 section names, or is NULL if the standard
1743 ELF names are used. */
c906108c
SS
1744
1745int
251d32d9
TG
1746dwarf2_has_info (struct objfile *objfile,
1747 const struct dwarf2_debug_sections *names)
c906108c 1748{
be391dca
TT
1749 dwarf2_per_objfile = objfile_data (objfile, dwarf2_objfile_data_key);
1750 if (!dwarf2_per_objfile)
1751 {
1752 /* Initialize per-objfile state. */
1753 struct dwarf2_per_objfile *data
1754 = obstack_alloc (&objfile->objfile_obstack, sizeof (*data));
9a619af0 1755
be391dca
TT
1756 memset (data, 0, sizeof (*data));
1757 set_objfile_data (objfile, dwarf2_objfile_data_key, data);
1758 dwarf2_per_objfile = data;
6502dd73 1759
251d32d9
TG
1760 bfd_map_over_sections (objfile->obfd, dwarf2_locate_sections,
1761 (void *) names);
be391dca
TT
1762 dwarf2_per_objfile->objfile = objfile;
1763 }
1764 return (dwarf2_per_objfile->info.asection != NULL
1765 && dwarf2_per_objfile->abbrev.asection != NULL);
c906108c
SS
1766}
1767
251d32d9
TG
1768/* When loading sections, we look either for uncompressed section or for
1769 compressed section names. */
233a11ab
CS
1770
1771static int
251d32d9
TG
1772section_is_p (const char *section_name,
1773 const struct dwarf2_section_names *names)
233a11ab 1774{
251d32d9
TG
1775 if (names->normal != NULL
1776 && strcmp (section_name, names->normal) == 0)
1777 return 1;
1778 if (names->compressed != NULL
1779 && strcmp (section_name, names->compressed) == 0)
1780 return 1;
1781 return 0;
233a11ab
CS
1782}
1783
c906108c
SS
1784/* This function is mapped across the sections and remembers the
1785 offset and size of each of the debugging sections we are interested
1786 in. */
1787
1788static void
251d32d9 1789dwarf2_locate_sections (bfd *abfd, asection *sectp, void *vnames)
c906108c 1790{
251d32d9 1791 const struct dwarf2_debug_sections *names;
dc7650b8 1792 flagword aflag = bfd_get_section_flags (abfd, sectp);
251d32d9
TG
1793
1794 if (vnames == NULL)
1795 names = &dwarf2_elf_names;
1796 else
1797 names = (const struct dwarf2_debug_sections *) vnames;
1798
dc7650b8
JK
1799 if ((aflag & SEC_HAS_CONTENTS) == 0)
1800 {
1801 }
1802 else if (section_is_p (sectp->name, &names->info))
c906108c 1803 {
dce234bc
PP
1804 dwarf2_per_objfile->info.asection = sectp;
1805 dwarf2_per_objfile->info.size = bfd_get_section_size (sectp);
c906108c 1806 }
251d32d9 1807 else if (section_is_p (sectp->name, &names->abbrev))
c906108c 1808 {
dce234bc
PP
1809 dwarf2_per_objfile->abbrev.asection = sectp;
1810 dwarf2_per_objfile->abbrev.size = bfd_get_section_size (sectp);
c906108c 1811 }
251d32d9 1812 else if (section_is_p (sectp->name, &names->line))
c906108c 1813 {
dce234bc
PP
1814 dwarf2_per_objfile->line.asection = sectp;
1815 dwarf2_per_objfile->line.size = bfd_get_section_size (sectp);
c906108c 1816 }
251d32d9 1817 else if (section_is_p (sectp->name, &names->loc))
c906108c 1818 {
dce234bc
PP
1819 dwarf2_per_objfile->loc.asection = sectp;
1820 dwarf2_per_objfile->loc.size = bfd_get_section_size (sectp);
c906108c 1821 }
251d32d9 1822 else if (section_is_p (sectp->name, &names->macinfo))
c906108c 1823 {
dce234bc
PP
1824 dwarf2_per_objfile->macinfo.asection = sectp;
1825 dwarf2_per_objfile->macinfo.size = bfd_get_section_size (sectp);
c906108c 1826 }
cf2c3c16
TT
1827 else if (section_is_p (sectp->name, &names->macro))
1828 {
1829 dwarf2_per_objfile->macro.asection = sectp;
1830 dwarf2_per_objfile->macro.size = bfd_get_section_size (sectp);
1831 }
251d32d9 1832 else if (section_is_p (sectp->name, &names->str))
c906108c 1833 {
dce234bc
PP
1834 dwarf2_per_objfile->str.asection = sectp;
1835 dwarf2_per_objfile->str.size = bfd_get_section_size (sectp);
c906108c 1836 }
3019eac3
DE
1837 else if (section_is_p (sectp->name, &names->addr))
1838 {
1839 dwarf2_per_objfile->addr.asection = sectp;
1840 dwarf2_per_objfile->addr.size = bfd_get_section_size (sectp);
1841 }
251d32d9 1842 else if (section_is_p (sectp->name, &names->frame))
b6af0555 1843 {
dce234bc
PP
1844 dwarf2_per_objfile->frame.asection = sectp;
1845 dwarf2_per_objfile->frame.size = bfd_get_section_size (sectp);
b6af0555 1846 }
251d32d9 1847 else if (section_is_p (sectp->name, &names->eh_frame))
b6af0555 1848 {
dc7650b8
JK
1849 dwarf2_per_objfile->eh_frame.asection = sectp;
1850 dwarf2_per_objfile->eh_frame.size = bfd_get_section_size (sectp);
b6af0555 1851 }
251d32d9 1852 else if (section_is_p (sectp->name, &names->ranges))
af34e669 1853 {
dce234bc
PP
1854 dwarf2_per_objfile->ranges.asection = sectp;
1855 dwarf2_per_objfile->ranges.size = bfd_get_section_size (sectp);
af34e669 1856 }
251d32d9 1857 else if (section_is_p (sectp->name, &names->types))
348e048f 1858 {
8b70b953
TT
1859 struct dwarf2_section_info type_section;
1860
1861 memset (&type_section, 0, sizeof (type_section));
1862 type_section.asection = sectp;
1863 type_section.size = bfd_get_section_size (sectp);
1864
1865 VEC_safe_push (dwarf2_section_info_def, dwarf2_per_objfile->types,
1866 &type_section);
348e048f 1867 }
251d32d9 1868 else if (section_is_p (sectp->name, &names->gdb_index))
9291a0cd
TT
1869 {
1870 dwarf2_per_objfile->gdb_index.asection = sectp;
1871 dwarf2_per_objfile->gdb_index.size = bfd_get_section_size (sectp);
1872 }
dce234bc 1873
72dca2f5
FR
1874 if ((bfd_get_section_flags (abfd, sectp) & SEC_LOAD)
1875 && bfd_section_vma (abfd, sectp) == 0)
1876 dwarf2_per_objfile->has_section_at_zero = 1;
c906108c
SS
1877}
1878
fceca515
DE
1879/* A helper function that decides whether a section is empty,
1880 or not present. */
9e0ac564
TT
1881
1882static int
1883dwarf2_section_empty_p (struct dwarf2_section_info *info)
1884{
1885 return info->asection == NULL || info->size == 0;
1886}
1887
3019eac3
DE
1888/* Read the contents of the section INFO.
1889 OBJFILE is the main object file, but not necessarily the file where
1890 the section comes from. E.g., for DWO files INFO->asection->owner
1891 is the bfd of the DWO file.
dce234bc 1892 If the section is compressed, uncompress it before returning. */
c906108c 1893
dce234bc
PP
1894static void
1895dwarf2_read_section (struct objfile *objfile, struct dwarf2_section_info *info)
c906108c 1896{
dce234bc 1897 asection *sectp = info->asection;
3019eac3 1898 bfd *abfd;
dce234bc
PP
1899 gdb_byte *buf, *retbuf;
1900 unsigned char header[4];
c906108c 1901
be391dca
TT
1902 if (info->readin)
1903 return;
dce234bc 1904 info->buffer = NULL;
be391dca 1905 info->readin = 1;
188dd5d6 1906
9e0ac564 1907 if (dwarf2_section_empty_p (info))
dce234bc 1908 return;
c906108c 1909
3019eac3
DE
1910 abfd = sectp->owner;
1911
4bf44c1c
TT
1912 /* If the section has relocations, we must read it ourselves.
1913 Otherwise we attach it to the BFD. */
1914 if ((sectp->flags & SEC_RELOC) == 0)
dce234bc 1915 {
4bf44c1c 1916 const gdb_byte *bytes = gdb_bfd_map_section (sectp, &info->size);
dce234bc 1917
4bf44c1c
TT
1918 /* We have to cast away const here for historical reasons.
1919 Fixing dwarf2read to be const-correct would be quite nice. */
1920 info->buffer = (gdb_byte *) bytes;
1921 return;
dce234bc 1922 }
dce234bc 1923
4bf44c1c
TT
1924 buf = obstack_alloc (&objfile->objfile_obstack, info->size);
1925 info->buffer = buf;
dce234bc
PP
1926
1927 /* When debugging .o files, we may need to apply relocations; see
1928 http://sourceware.org/ml/gdb-patches/2002-04/msg00136.html .
1929 We never compress sections in .o files, so we only need to
1930 try this when the section is not compressed. */
ac8035ab 1931 retbuf = symfile_relocate_debug_section (objfile, sectp, buf);
dce234bc
PP
1932 if (retbuf != NULL)
1933 {
1934 info->buffer = retbuf;
1935 return;
1936 }
1937
1938 if (bfd_seek (abfd, sectp->filepos, SEEK_SET) != 0
1939 || bfd_bread (buf, info->size, abfd) != info->size)
1940 error (_("Dwarf Error: Can't read DWARF data from '%s'"),
1941 bfd_get_filename (abfd));
1942}
1943
9e0ac564
TT
1944/* A helper function that returns the size of a section in a safe way.
1945 If you are positive that the section has been read before using the
1946 size, then it is safe to refer to the dwarf2_section_info object's
1947 "size" field directly. In other cases, you must call this
1948 function, because for compressed sections the size field is not set
1949 correctly until the section has been read. */
1950
1951static bfd_size_type
1952dwarf2_section_size (struct objfile *objfile,
1953 struct dwarf2_section_info *info)
1954{
1955 if (!info->readin)
1956 dwarf2_read_section (objfile, info);
1957 return info->size;
1958}
1959
dce234bc 1960/* Fill in SECTP, BUFP and SIZEP with section info, given OBJFILE and
0963b4bd 1961 SECTION_NAME. */
af34e669 1962
dce234bc 1963void
3017a003
TG
1964dwarf2_get_section_info (struct objfile *objfile,
1965 enum dwarf2_section_enum sect,
dce234bc
PP
1966 asection **sectp, gdb_byte **bufp,
1967 bfd_size_type *sizep)
1968{
1969 struct dwarf2_per_objfile *data
1970 = objfile_data (objfile, dwarf2_objfile_data_key);
1971 struct dwarf2_section_info *info;
a3b2a86b
TT
1972
1973 /* We may see an objfile without any DWARF, in which case we just
1974 return nothing. */
1975 if (data == NULL)
1976 {
1977 *sectp = NULL;
1978 *bufp = NULL;
1979 *sizep = 0;
1980 return;
1981 }
3017a003
TG
1982 switch (sect)
1983 {
1984 case DWARF2_DEBUG_FRAME:
1985 info = &data->frame;
1986 break;
1987 case DWARF2_EH_FRAME:
1988 info = &data->eh_frame;
1989 break;
1990 default:
1991 gdb_assert_not_reached ("unexpected section");
1992 }
dce234bc 1993
9e0ac564 1994 dwarf2_read_section (objfile, info);
dce234bc
PP
1995
1996 *sectp = info->asection;
1997 *bufp = info->buffer;
1998 *sizep = info->size;
1999}
2000
36586728
TT
2001/* A helper function to find the sections for a .dwz file. */
2002
2003static void
2004locate_dwz_sections (bfd *abfd, asection *sectp, void *arg)
2005{
2006 struct dwz_file *dwz_file = arg;
2007
2008 /* Note that we only support the standard ELF names, because .dwz
2009 is ELF-only (at the time of writing). */
2010 if (section_is_p (sectp->name, &dwarf2_elf_names.abbrev))
2011 {
2012 dwz_file->abbrev.asection = sectp;
2013 dwz_file->abbrev.size = bfd_get_section_size (sectp);
2014 }
2015 else if (section_is_p (sectp->name, &dwarf2_elf_names.info))
2016 {
2017 dwz_file->info.asection = sectp;
2018 dwz_file->info.size = bfd_get_section_size (sectp);
2019 }
2020 else if (section_is_p (sectp->name, &dwarf2_elf_names.str))
2021 {
2022 dwz_file->str.asection = sectp;
2023 dwz_file->str.size = bfd_get_section_size (sectp);
2024 }
2025 else if (section_is_p (sectp->name, &dwarf2_elf_names.line))
2026 {
2027 dwz_file->line.asection = sectp;
2028 dwz_file->line.size = bfd_get_section_size (sectp);
2029 }
2030 else if (section_is_p (sectp->name, &dwarf2_elf_names.macro))
2031 {
2032 dwz_file->macro.asection = sectp;
2033 dwz_file->macro.size = bfd_get_section_size (sectp);
2034 }
2ec9a5e0
TT
2035 else if (section_is_p (sectp->name, &dwarf2_elf_names.gdb_index))
2036 {
2037 dwz_file->gdb_index.asection = sectp;
2038 dwz_file->gdb_index.size = bfd_get_section_size (sectp);
2039 }
36586728
TT
2040}
2041
2042/* Open the separate '.dwz' debug file, if needed. Error if the file
2043 cannot be found. */
2044
2045static struct dwz_file *
2046dwarf2_get_dwz_file (void)
2047{
2048 bfd *abfd, *dwz_bfd;
2049 asection *section;
2050 gdb_byte *data;
2051 struct cleanup *cleanup;
2052 const char *filename;
2053 struct dwz_file *result;
2054
2055 if (dwarf2_per_objfile->dwz_file != NULL)
2056 return dwarf2_per_objfile->dwz_file;
2057
2058 abfd = dwarf2_per_objfile->objfile->obfd;
2059 section = bfd_get_section_by_name (abfd, ".gnu_debugaltlink");
2060 if (section == NULL)
2061 error (_("could not find '.gnu_debugaltlink' section"));
2062 if (!bfd_malloc_and_get_section (abfd, section, &data))
2063 error (_("could not read '.gnu_debugaltlink' section: %s"),
2064 bfd_errmsg (bfd_get_error ()));
2065 cleanup = make_cleanup (xfree, data);
2066
2067 filename = data;
2068 if (!IS_ABSOLUTE_PATH (filename))
2069 {
2070 char *abs = gdb_realpath (dwarf2_per_objfile->objfile->name);
2071 char *rel;
2072
2073 make_cleanup (xfree, abs);
2074 abs = ldirname (abs);
2075 make_cleanup (xfree, abs);
2076
2077 rel = concat (abs, SLASH_STRING, filename, (char *) NULL);
2078 make_cleanup (xfree, rel);
2079 filename = rel;
2080 }
2081
2082 /* The format is just a NUL-terminated file name, followed by the
2083 build-id. For now, though, we ignore the build-id. */
2084 dwz_bfd = gdb_bfd_open (filename, gnutarget, -1);
2085 if (dwz_bfd == NULL)
2086 error (_("could not read '%s': %s"), filename,
2087 bfd_errmsg (bfd_get_error ()));
2088
2089 if (!bfd_check_format (dwz_bfd, bfd_object))
2090 {
2091 gdb_bfd_unref (dwz_bfd);
2092 error (_("file '%s' was not usable: %s"), filename,
2093 bfd_errmsg (bfd_get_error ()));
2094 }
2095
2096 result = OBSTACK_ZALLOC (&dwarf2_per_objfile->objfile->objfile_obstack,
2097 struct dwz_file);
2098 result->dwz_bfd = dwz_bfd;
2099
2100 bfd_map_over_sections (dwz_bfd, locate_dwz_sections, result);
2101
2102 do_cleanups (cleanup);
2103
2104 return result;
2105}
9291a0cd 2106\f
7b9f3c50
DE
2107/* DWARF quick_symbols_functions support. */
2108
2109/* TUs can share .debug_line entries, and there can be a lot more TUs than
2110 unique line tables, so we maintain a separate table of all .debug_line
2111 derived entries to support the sharing.
2112 All the quick functions need is the list of file names. We discard the
2113 line_header when we're done and don't need to record it here. */
2114struct quick_file_names
2115{
094b34ac
DE
2116 /* The data used to construct the hash key. */
2117 struct stmt_list_hash hash;
7b9f3c50
DE
2118
2119 /* The number of entries in file_names, real_names. */
2120 unsigned int num_file_names;
2121
2122 /* The file names from the line table, after being run through
2123 file_full_name. */
2124 const char **file_names;
2125
2126 /* The file names from the line table after being run through
2127 gdb_realpath. These are computed lazily. */
2128 const char **real_names;
2129};
2130
2131/* When using the index (and thus not using psymtabs), each CU has an
2132 object of this type. This is used to hold information needed by
2133 the various "quick" methods. */
2134struct dwarf2_per_cu_quick_data
2135{
2136 /* The file table. This can be NULL if there was no file table
2137 or it's currently not read in.
2138 NOTE: This points into dwarf2_per_objfile->quick_file_names_table. */
2139 struct quick_file_names *file_names;
2140
2141 /* The corresponding symbol table. This is NULL if symbols for this
2142 CU have not yet been read. */
2143 struct symtab *symtab;
2144
2145 /* A temporary mark bit used when iterating over all CUs in
2146 expand_symtabs_matching. */
2147 unsigned int mark : 1;
2148
2149 /* True if we've tried to read the file table and found there isn't one.
2150 There will be no point in trying to read it again next time. */
2151 unsigned int no_file_data : 1;
2152};
2153
094b34ac
DE
2154/* Utility hash function for a stmt_list_hash. */
2155
2156static hashval_t
2157hash_stmt_list_entry (const struct stmt_list_hash *stmt_list_hash)
2158{
2159 hashval_t v = 0;
2160
2161 if (stmt_list_hash->dwo_unit != NULL)
2162 v += (uintptr_t) stmt_list_hash->dwo_unit->dwo_file;
2163 v += stmt_list_hash->line_offset.sect_off;
2164 return v;
2165}
2166
2167/* Utility equality function for a stmt_list_hash. */
2168
2169static int
2170eq_stmt_list_entry (const struct stmt_list_hash *lhs,
2171 const struct stmt_list_hash *rhs)
2172{
2173 if ((lhs->dwo_unit != NULL) != (rhs->dwo_unit != NULL))
2174 return 0;
2175 if (lhs->dwo_unit != NULL
2176 && lhs->dwo_unit->dwo_file != rhs->dwo_unit->dwo_file)
2177 return 0;
2178
2179 return lhs->line_offset.sect_off == rhs->line_offset.sect_off;
2180}
2181
7b9f3c50
DE
2182/* Hash function for a quick_file_names. */
2183
2184static hashval_t
2185hash_file_name_entry (const void *e)
2186{
2187 const struct quick_file_names *file_data = e;
2188
094b34ac 2189 return hash_stmt_list_entry (&file_data->hash);
7b9f3c50
DE
2190}
2191
2192/* Equality function for a quick_file_names. */
2193
2194static int
2195eq_file_name_entry (const void *a, const void *b)
2196{
2197 const struct quick_file_names *ea = a;
2198 const struct quick_file_names *eb = b;
2199
094b34ac 2200 return eq_stmt_list_entry (&ea->hash, &eb->hash);
7b9f3c50
DE
2201}
2202
2203/* Delete function for a quick_file_names. */
2204
2205static void
2206delete_file_name_entry (void *e)
2207{
2208 struct quick_file_names *file_data = e;
2209 int i;
2210
2211 for (i = 0; i < file_data->num_file_names; ++i)
2212 {
2213 xfree ((void*) file_data->file_names[i]);
2214 if (file_data->real_names)
2215 xfree ((void*) file_data->real_names[i]);
2216 }
2217
2218 /* The space for the struct itself lives on objfile_obstack,
2219 so we don't free it here. */
2220}
2221
2222/* Create a quick_file_names hash table. */
2223
2224static htab_t
2225create_quick_file_names_table (unsigned int nr_initial_entries)
2226{
2227 return htab_create_alloc (nr_initial_entries,
2228 hash_file_name_entry, eq_file_name_entry,
2229 delete_file_name_entry, xcalloc, xfree);
2230}
9291a0cd 2231
918dd910
JK
2232/* Read in PER_CU->CU. This function is unrelated to symtabs, symtab would
2233 have to be created afterwards. You should call age_cached_comp_units after
2234 processing PER_CU->CU. dw2_setup must have been already called. */
2235
2236static void
2237load_cu (struct dwarf2_per_cu_data *per_cu)
2238{
3019eac3 2239 if (per_cu->is_debug_types)
e5fe5e75 2240 load_full_type_unit (per_cu);
918dd910 2241 else
95554aad 2242 load_full_comp_unit (per_cu, language_minimal);
918dd910 2243
918dd910 2244 gdb_assert (per_cu->cu != NULL);
2dc860c0
DE
2245
2246 dwarf2_find_base_address (per_cu->cu->dies, per_cu->cu);
918dd910
JK
2247}
2248
a0f42c21 2249/* Read in the symbols for PER_CU. */
2fdf6df6 2250
9291a0cd 2251static void
a0f42c21 2252dw2_do_instantiate_symtab (struct dwarf2_per_cu_data *per_cu)
9291a0cd
TT
2253{
2254 struct cleanup *back_to;
2255
f4dc4d17
DE
2256 /* Skip type_unit_groups, reading the type units they contain
2257 is handled elsewhere. */
2258 if (IS_TYPE_UNIT_GROUP (per_cu))
2259 return;
2260
9291a0cd
TT
2261 back_to = make_cleanup (dwarf2_release_queue, NULL);
2262
95554aad
TT
2263 if (dwarf2_per_objfile->using_index
2264 ? per_cu->v.quick->symtab == NULL
2265 : (per_cu->v.psymtab == NULL || !per_cu->v.psymtab->readin))
2266 {
2267 queue_comp_unit (per_cu, language_minimal);
2268 load_cu (per_cu);
2269 }
9291a0cd 2270
a0f42c21 2271 process_queue ();
9291a0cd
TT
2272
2273 /* Age the cache, releasing compilation units that have not
2274 been used recently. */
2275 age_cached_comp_units ();
2276
2277 do_cleanups (back_to);
2278}
2279
2280/* Ensure that the symbols for PER_CU have been read in. OBJFILE is
2281 the objfile from which this CU came. Returns the resulting symbol
2282 table. */
2fdf6df6 2283
9291a0cd 2284static struct symtab *
a0f42c21 2285dw2_instantiate_symtab (struct dwarf2_per_cu_data *per_cu)
9291a0cd 2286{
95554aad 2287 gdb_assert (dwarf2_per_objfile->using_index);
9291a0cd
TT
2288 if (!per_cu->v.quick->symtab)
2289 {
2290 struct cleanup *back_to = make_cleanup (free_cached_comp_units, NULL);
2291 increment_reading_symtab ();
a0f42c21 2292 dw2_do_instantiate_symtab (per_cu);
95554aad 2293 process_cu_includes ();
9291a0cd
TT
2294 do_cleanups (back_to);
2295 }
2296 return per_cu->v.quick->symtab;
2297}
2298
f4dc4d17
DE
2299/* Return the CU given its index.
2300
2301 This is intended for loops like:
2302
2303 for (i = 0; i < (dwarf2_per_objfile->n_comp_units
2304 + dwarf2_per_objfile->n_type_units); ++i)
2305 {
2306 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
2307
2308 ...;
2309 }
2310*/
2fdf6df6 2311
1fd400ff
TT
2312static struct dwarf2_per_cu_data *
2313dw2_get_cu (int index)
2314{
2315 if (index >= dwarf2_per_objfile->n_comp_units)
2316 {
f4dc4d17 2317 index -= dwarf2_per_objfile->n_comp_units;
094b34ac
DE
2318 gdb_assert (index < dwarf2_per_objfile->n_type_units);
2319 return &dwarf2_per_objfile->all_type_units[index]->per_cu;
f4dc4d17
DE
2320 }
2321
2322 return dwarf2_per_objfile->all_comp_units[index];
2323}
2324
2325/* Return the primary CU given its index.
2326 The difference between this function and dw2_get_cu is in the handling
2327 of type units (TUs). Here we return the type_unit_group object.
2328
2329 This is intended for loops like:
2330
2331 for (i = 0; i < (dwarf2_per_objfile->n_comp_units
2332 + dwarf2_per_objfile->n_type_unit_groups); ++i)
2333 {
2334 struct dwarf2_per_cu_data *per_cu = dw2_get_primary_cu (i);
2335
2336 ...;
2337 }
2338*/
2339
2340static struct dwarf2_per_cu_data *
2341dw2_get_primary_cu (int index)
2342{
2343 if (index >= dwarf2_per_objfile->n_comp_units)
2344 {
1fd400ff 2345 index -= dwarf2_per_objfile->n_comp_units;
094b34ac
DE
2346 gdb_assert (index < dwarf2_per_objfile->n_type_unit_groups);
2347 return &dwarf2_per_objfile->all_type_unit_groups[index]->per_cu;
1fd400ff 2348 }
f4dc4d17 2349
1fd400ff
TT
2350 return dwarf2_per_objfile->all_comp_units[index];
2351}
2352
9291a0cd
TT
2353/* A helper function that knows how to read a 64-bit value in a way
2354 that doesn't make gdb die. Returns 1 if the conversion went ok, 0
2355 otherwise. */
2fdf6df6 2356
9291a0cd
TT
2357static int
2358extract_cu_value (const char *bytes, ULONGEST *result)
2359{
2360 if (sizeof (ULONGEST) < 8)
2361 {
2362 int i;
2363
2364 /* Ignore the upper 4 bytes if they are all zero. */
2365 for (i = 0; i < 4; ++i)
2366 if (bytes[i + 4] != 0)
2367 return 0;
2368
2369 *result = extract_unsigned_integer (bytes, 4, BFD_ENDIAN_LITTLE);
2370 }
2371 else
2372 *result = extract_unsigned_integer (bytes, 8, BFD_ENDIAN_LITTLE);
2373 return 1;
2374}
2375
2ec9a5e0
TT
2376/* A helper for create_cus_from_index that handles a given list of
2377 CUs. */
2fdf6df6 2378
9291a0cd 2379static int
2ec9a5e0
TT
2380create_cus_from_index_list (struct objfile *objfile,
2381 const gdb_byte *cu_list, offset_type n_elements,
2382 struct dwarf2_section_info *section,
2383 int is_dwz,
2384 int base_offset)
9291a0cd
TT
2385{
2386 offset_type i;
9291a0cd 2387
2ec9a5e0 2388 for (i = 0; i < n_elements; i += 2)
9291a0cd
TT
2389 {
2390 struct dwarf2_per_cu_data *the_cu;
2391 ULONGEST offset, length;
2392
2393 if (!extract_cu_value (cu_list, &offset)
2394 || !extract_cu_value (cu_list + 8, &length))
2395 return 0;
2396 cu_list += 2 * 8;
2397
2398 the_cu = OBSTACK_ZALLOC (&objfile->objfile_obstack,
2399 struct dwarf2_per_cu_data);
b64f50a1 2400 the_cu->offset.sect_off = offset;
9291a0cd
TT
2401 the_cu->length = length;
2402 the_cu->objfile = objfile;
2ec9a5e0 2403 the_cu->info_or_types_section = section;
9291a0cd
TT
2404 the_cu->v.quick = OBSTACK_ZALLOC (&objfile->objfile_obstack,
2405 struct dwarf2_per_cu_quick_data);
2ec9a5e0
TT
2406 the_cu->is_dwz = is_dwz;
2407 dwarf2_per_objfile->all_comp_units[base_offset + i / 2] = the_cu;
9291a0cd
TT
2408 }
2409
2410 return 1;
2411}
2412
2ec9a5e0
TT
2413/* Read the CU list from the mapped index, and use it to create all
2414 the CU objects for this objfile. Return 0 if something went wrong,
2415 1 if everything went ok. */
2416
2417static int
2418create_cus_from_index (struct objfile *objfile,
2419 const gdb_byte *cu_list, offset_type cu_list_elements,
2420 const gdb_byte *dwz_list, offset_type dwz_elements)
2421{
2422 struct dwz_file *dwz;
2423
2424 dwarf2_per_objfile->n_comp_units = (cu_list_elements + dwz_elements) / 2;
2425 dwarf2_per_objfile->all_comp_units
2426 = obstack_alloc (&objfile->objfile_obstack,
2427 dwarf2_per_objfile->n_comp_units
2428 * sizeof (struct dwarf2_per_cu_data *));
2429
2430 if (!create_cus_from_index_list (objfile, cu_list, cu_list_elements,
2431 &dwarf2_per_objfile->info, 0, 0))
2432 return 0;
2433
2434 if (dwz_elements == 0)
2435 return 1;
2436
2437 dwz = dwarf2_get_dwz_file ();
2438 return create_cus_from_index_list (objfile, dwz_list, dwz_elements,
2439 &dwz->info, 1, cu_list_elements / 2);
2440}
2441
1fd400ff 2442/* Create the signatured type hash table from the index. */
673bfd45 2443
1fd400ff 2444static int
673bfd45 2445create_signatured_type_table_from_index (struct objfile *objfile,
8b70b953 2446 struct dwarf2_section_info *section,
673bfd45
DE
2447 const gdb_byte *bytes,
2448 offset_type elements)
1fd400ff
TT
2449{
2450 offset_type i;
673bfd45 2451 htab_t sig_types_hash;
1fd400ff 2452
d467dd73
DE
2453 dwarf2_per_objfile->n_type_units = elements / 3;
2454 dwarf2_per_objfile->all_type_units
1fd400ff 2455 = obstack_alloc (&objfile->objfile_obstack,
d467dd73 2456 dwarf2_per_objfile->n_type_units
b4dd5633 2457 * sizeof (struct signatured_type *));
1fd400ff 2458
673bfd45 2459 sig_types_hash = allocate_signatured_type_table (objfile);
1fd400ff
TT
2460
2461 for (i = 0; i < elements; i += 3)
2462 {
52dc124a
DE
2463 struct signatured_type *sig_type;
2464 ULONGEST offset, type_offset_in_tu, signature;
1fd400ff
TT
2465 void **slot;
2466
2467 if (!extract_cu_value (bytes, &offset)
52dc124a 2468 || !extract_cu_value (bytes + 8, &type_offset_in_tu))
1fd400ff
TT
2469 return 0;
2470 signature = extract_unsigned_integer (bytes + 16, 8, BFD_ENDIAN_LITTLE);
2471 bytes += 3 * 8;
2472
52dc124a 2473 sig_type = OBSTACK_ZALLOC (&objfile->objfile_obstack,
1fd400ff 2474 struct signatured_type);
52dc124a 2475 sig_type->signature = signature;
3019eac3
DE
2476 sig_type->type_offset_in_tu.cu_off = type_offset_in_tu;
2477 sig_type->per_cu.is_debug_types = 1;
2478 sig_type->per_cu.info_or_types_section = section;
52dc124a
DE
2479 sig_type->per_cu.offset.sect_off = offset;
2480 sig_type->per_cu.objfile = objfile;
2481 sig_type->per_cu.v.quick
1fd400ff
TT
2482 = OBSTACK_ZALLOC (&objfile->objfile_obstack,
2483 struct dwarf2_per_cu_quick_data);
2484
52dc124a
DE
2485 slot = htab_find_slot (sig_types_hash, sig_type, INSERT);
2486 *slot = sig_type;
1fd400ff 2487
b4dd5633 2488 dwarf2_per_objfile->all_type_units[i / 3] = sig_type;
1fd400ff
TT
2489 }
2490
673bfd45 2491 dwarf2_per_objfile->signatured_types = sig_types_hash;
1fd400ff
TT
2492
2493 return 1;
2494}
2495
9291a0cd
TT
2496/* Read the address map data from the mapped index, and use it to
2497 populate the objfile's psymtabs_addrmap. */
2fdf6df6 2498
9291a0cd
TT
2499static void
2500create_addrmap_from_index (struct objfile *objfile, struct mapped_index *index)
2501{
2502 const gdb_byte *iter, *end;
2503 struct obstack temp_obstack;
2504 struct addrmap *mutable_map;
2505 struct cleanup *cleanup;
2506 CORE_ADDR baseaddr;
2507
2508 obstack_init (&temp_obstack);
2509 cleanup = make_cleanup_obstack_free (&temp_obstack);
2510 mutable_map = addrmap_create_mutable (&temp_obstack);
2511
2512 iter = index->address_table;
2513 end = iter + index->address_table_size;
2514
2515 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
2516
2517 while (iter < end)
2518 {
2519 ULONGEST hi, lo, cu_index;
2520 lo = extract_unsigned_integer (iter, 8, BFD_ENDIAN_LITTLE);
2521 iter += 8;
2522 hi = extract_unsigned_integer (iter, 8, BFD_ENDIAN_LITTLE);
2523 iter += 8;
2524 cu_index = extract_unsigned_integer (iter, 4, BFD_ENDIAN_LITTLE);
2525 iter += 4;
2526
2527 addrmap_set_empty (mutable_map, lo + baseaddr, hi + baseaddr - 1,
1fd400ff 2528 dw2_get_cu (cu_index));
9291a0cd
TT
2529 }
2530
2531 objfile->psymtabs_addrmap = addrmap_create_fixed (mutable_map,
2532 &objfile->objfile_obstack);
2533 do_cleanups (cleanup);
2534}
2535
59d7bcaf
JK
2536/* The hash function for strings in the mapped index. This is the same as
2537 SYMBOL_HASH_NEXT, but we keep a separate copy to maintain control over the
2538 implementation. This is necessary because the hash function is tied to the
2539 format of the mapped index file. The hash values do not have to match with
559a7a62
JK
2540 SYMBOL_HASH_NEXT.
2541
2542 Use INT_MAX for INDEX_VERSION if you generate the current index format. */
2fdf6df6 2543
9291a0cd 2544static hashval_t
559a7a62 2545mapped_index_string_hash (int index_version, const void *p)
9291a0cd
TT
2546{
2547 const unsigned char *str = (const unsigned char *) p;
2548 hashval_t r = 0;
2549 unsigned char c;
2550
2551 while ((c = *str++) != 0)
559a7a62
JK
2552 {
2553 if (index_version >= 5)
2554 c = tolower (c);
2555 r = r * 67 + c - 113;
2556 }
9291a0cd
TT
2557
2558 return r;
2559}
2560
2561/* Find a slot in the mapped index INDEX for the object named NAME.
2562 If NAME is found, set *VEC_OUT to point to the CU vector in the
2563 constant pool and return 1. If NAME cannot be found, return 0. */
2fdf6df6 2564
9291a0cd
TT
2565static int
2566find_slot_in_mapped_hash (struct mapped_index *index, const char *name,
2567 offset_type **vec_out)
2568{
0cf03b49
JK
2569 struct cleanup *back_to = make_cleanup (null_cleanup, 0);
2570 offset_type hash;
9291a0cd 2571 offset_type slot, step;
559a7a62 2572 int (*cmp) (const char *, const char *);
9291a0cd 2573
0cf03b49
JK
2574 if (current_language->la_language == language_cplus
2575 || current_language->la_language == language_java
2576 || current_language->la_language == language_fortran)
2577 {
2578 /* NAME is already canonical. Drop any qualifiers as .gdb_index does
2579 not contain any. */
2580 const char *paren = strchr (name, '(');
2581
2582 if (paren)
2583 {
2584 char *dup;
2585
2586 dup = xmalloc (paren - name + 1);
2587 memcpy (dup, name, paren - name);
2588 dup[paren - name] = 0;
2589
2590 make_cleanup (xfree, dup);
2591 name = dup;
2592 }
2593 }
2594
559a7a62 2595 /* Index version 4 did not support case insensitive searches. But the
feea76c2 2596 indices for case insensitive languages are built in lowercase, therefore
559a7a62
JK
2597 simulate our NAME being searched is also lowercased. */
2598 hash = mapped_index_string_hash ((index->version == 4
2599 && case_sensitivity == case_sensitive_off
2600 ? 5 : index->version),
2601 name);
2602
3876f04e
DE
2603 slot = hash & (index->symbol_table_slots - 1);
2604 step = ((hash * 17) & (index->symbol_table_slots - 1)) | 1;
559a7a62 2605 cmp = (case_sensitivity == case_sensitive_on ? strcmp : strcasecmp);
9291a0cd
TT
2606
2607 for (;;)
2608 {
2609 /* Convert a slot number to an offset into the table. */
2610 offset_type i = 2 * slot;
2611 const char *str;
3876f04e 2612 if (index->symbol_table[i] == 0 && index->symbol_table[i + 1] == 0)
0cf03b49
JK
2613 {
2614 do_cleanups (back_to);
2615 return 0;
2616 }
9291a0cd 2617
3876f04e 2618 str = index->constant_pool + MAYBE_SWAP (index->symbol_table[i]);
559a7a62 2619 if (!cmp (name, str))
9291a0cd
TT
2620 {
2621 *vec_out = (offset_type *) (index->constant_pool
3876f04e 2622 + MAYBE_SWAP (index->symbol_table[i + 1]));
0cf03b49 2623 do_cleanups (back_to);
9291a0cd
TT
2624 return 1;
2625 }
2626
3876f04e 2627 slot = (slot + step) & (index->symbol_table_slots - 1);
9291a0cd
TT
2628 }
2629}
2630
2ec9a5e0
TT
2631/* A helper function that reads the .gdb_index from SECTION and fills
2632 in MAP. FILENAME is the name of the file containing the section;
2633 it is used for error reporting. DEPRECATED_OK is nonzero if it is
2634 ok to use deprecated sections.
2635
2636 CU_LIST, CU_LIST_ELEMENTS, TYPES_LIST, and TYPES_LIST_ELEMENTS are
2637 out parameters that are filled in with information about the CU and
2638 TU lists in the section.
2639
2640 Returns 1 if all went well, 0 otherwise. */
2fdf6df6 2641
9291a0cd 2642static int
2ec9a5e0
TT
2643read_index_from_section (struct objfile *objfile,
2644 const char *filename,
2645 int deprecated_ok,
2646 struct dwarf2_section_info *section,
2647 struct mapped_index *map,
2648 const gdb_byte **cu_list,
2649 offset_type *cu_list_elements,
2650 const gdb_byte **types_list,
2651 offset_type *types_list_elements)
9291a0cd 2652{
9291a0cd 2653 char *addr;
2ec9a5e0 2654 offset_type version;
b3b272e1 2655 offset_type *metadata;
1fd400ff 2656 int i;
9291a0cd 2657
2ec9a5e0 2658 if (dwarf2_section_empty_p (section))
9291a0cd 2659 return 0;
82430852
JK
2660
2661 /* Older elfutils strip versions could keep the section in the main
2662 executable while splitting it for the separate debug info file. */
2ec9a5e0 2663 if ((bfd_get_file_flags (section->asection) & SEC_HAS_CONTENTS) == 0)
82430852
JK
2664 return 0;
2665
2ec9a5e0 2666 dwarf2_read_section (objfile, section);
9291a0cd 2667
2ec9a5e0 2668 addr = section->buffer;
9291a0cd 2669 /* Version check. */
1fd400ff 2670 version = MAYBE_SWAP (*(offset_type *) addr);
987d643c 2671 /* Versions earlier than 3 emitted every copy of a psymbol. This
a6e293d1 2672 causes the index to behave very poorly for certain requests. Version 3
831adc1f 2673 contained incomplete addrmap. So, it seems better to just ignore such
481860b3 2674 indices. */
831adc1f 2675 if (version < 4)
481860b3
GB
2676 {
2677 static int warning_printed = 0;
2678 if (!warning_printed)
2679 {
2680 warning (_("Skipping obsolete .gdb_index section in %s."),
2ec9a5e0 2681 filename);
481860b3
GB
2682 warning_printed = 1;
2683 }
2684 return 0;
2685 }
2686 /* Index version 4 uses a different hash function than index version
2687 5 and later.
2688
2689 Versions earlier than 6 did not emit psymbols for inlined
2690 functions. Using these files will cause GDB not to be able to
2691 set breakpoints on inlined functions by name, so we ignore these
e615022a
DE
2692 indices unless the user has done
2693 "set use-deprecated-index-sections on". */
2ec9a5e0 2694 if (version < 6 && !deprecated_ok)
481860b3
GB
2695 {
2696 static int warning_printed = 0;
2697 if (!warning_printed)
2698 {
e615022a
DE
2699 warning (_("\
2700Skipping deprecated .gdb_index section in %s.\n\
2701Do \"set use-deprecated-index-sections on\" before the file is read\n\
2702to use the section anyway."),
2ec9a5e0 2703 filename);
481860b3
GB
2704 warning_printed = 1;
2705 }
2706 return 0;
2707 }
2708 /* Indexes with higher version than the one supported by GDB may be no
594e8718 2709 longer backward compatible. */
156942c7 2710 if (version > 7)
594e8718 2711 return 0;
9291a0cd 2712
559a7a62 2713 map->version = version;
2ec9a5e0 2714 map->total_size = section->size;
9291a0cd
TT
2715
2716 metadata = (offset_type *) (addr + sizeof (offset_type));
1fd400ff
TT
2717
2718 i = 0;
2ec9a5e0
TT
2719 *cu_list = addr + MAYBE_SWAP (metadata[i]);
2720 *cu_list_elements = ((MAYBE_SWAP (metadata[i + 1]) - MAYBE_SWAP (metadata[i]))
2721 / 8);
1fd400ff
TT
2722 ++i;
2723
2ec9a5e0
TT
2724 *types_list = addr + MAYBE_SWAP (metadata[i]);
2725 *types_list_elements = ((MAYBE_SWAP (metadata[i + 1])
2726 - MAYBE_SWAP (metadata[i]))
2727 / 8);
987d643c 2728 ++i;
1fd400ff
TT
2729
2730 map->address_table = addr + MAYBE_SWAP (metadata[i]);
2731 map->address_table_size = (MAYBE_SWAP (metadata[i + 1])
2732 - MAYBE_SWAP (metadata[i]));
2733 ++i;
2734
3876f04e
DE
2735 map->symbol_table = (offset_type *) (addr + MAYBE_SWAP (metadata[i]));
2736 map->symbol_table_slots = ((MAYBE_SWAP (metadata[i + 1])
2737 - MAYBE_SWAP (metadata[i]))
2738 / (2 * sizeof (offset_type)));
1fd400ff 2739 ++i;
9291a0cd 2740
1fd400ff
TT
2741 map->constant_pool = addr + MAYBE_SWAP (metadata[i]);
2742
2ec9a5e0
TT
2743 return 1;
2744}
2745
2746
2747/* Read the index file. If everything went ok, initialize the "quick"
2748 elements of all the CUs and return 1. Otherwise, return 0. */
2749
2750static int
2751dwarf2_read_index (struct objfile *objfile)
2752{
2753 struct mapped_index local_map, *map;
2754 const gdb_byte *cu_list, *types_list, *dwz_list = NULL;
2755 offset_type cu_list_elements, types_list_elements, dwz_list_elements = 0;
2756
2757 if (!read_index_from_section (objfile, objfile->name,
2758 use_deprecated_index_sections,
2759 &dwarf2_per_objfile->gdb_index, &local_map,
2760 &cu_list, &cu_list_elements,
2761 &types_list, &types_list_elements))
2762 return 0;
2763
0fefef59 2764 /* Don't use the index if it's empty. */
2ec9a5e0 2765 if (local_map.symbol_table_slots == 0)
0fefef59
DE
2766 return 0;
2767
2ec9a5e0
TT
2768 /* If there is a .dwz file, read it so we can get its CU list as
2769 well. */
2770 if (bfd_get_section_by_name (objfile->obfd, ".gnu_debugaltlink") != NULL)
2771 {
2772 struct dwz_file *dwz = dwarf2_get_dwz_file ();
2773 struct mapped_index dwz_map;
2774 const gdb_byte *dwz_types_ignore;
2775 offset_type dwz_types_elements_ignore;
2776
2777 if (!read_index_from_section (objfile, bfd_get_filename (dwz->dwz_bfd),
2778 1,
2779 &dwz->gdb_index, &dwz_map,
2780 &dwz_list, &dwz_list_elements,
2781 &dwz_types_ignore,
2782 &dwz_types_elements_ignore))
2783 {
2784 warning (_("could not read '.gdb_index' section from %s; skipping"),
2785 bfd_get_filename (dwz->dwz_bfd));
2786 return 0;
2787 }
2788 }
2789
2790 if (!create_cus_from_index (objfile, cu_list, cu_list_elements,
2791 dwz_list, dwz_list_elements))
1fd400ff
TT
2792 return 0;
2793
8b70b953
TT
2794 if (types_list_elements)
2795 {
2796 struct dwarf2_section_info *section;
2797
2798 /* We can only handle a single .debug_types when we have an
2799 index. */
2800 if (VEC_length (dwarf2_section_info_def, dwarf2_per_objfile->types) != 1)
2801 return 0;
2802
2803 section = VEC_index (dwarf2_section_info_def,
2804 dwarf2_per_objfile->types, 0);
2805
2806 if (!create_signatured_type_table_from_index (objfile, section,
2807 types_list,
2808 types_list_elements))
2809 return 0;
2810 }
9291a0cd 2811
2ec9a5e0
TT
2812 create_addrmap_from_index (objfile, &local_map);
2813
2814 map = obstack_alloc (&objfile->objfile_obstack, sizeof (struct mapped_index));
2815 *map = local_map;
9291a0cd
TT
2816
2817 dwarf2_per_objfile->index_table = map;
2818 dwarf2_per_objfile->using_index = 1;
7b9f3c50
DE
2819 dwarf2_per_objfile->quick_file_names_table =
2820 create_quick_file_names_table (dwarf2_per_objfile->n_comp_units);
9291a0cd
TT
2821
2822 return 1;
2823}
2824
2825/* A helper for the "quick" functions which sets the global
2826 dwarf2_per_objfile according to OBJFILE. */
2fdf6df6 2827
9291a0cd
TT
2828static void
2829dw2_setup (struct objfile *objfile)
2830{
2831 dwarf2_per_objfile = objfile_data (objfile, dwarf2_objfile_data_key);
2832 gdb_assert (dwarf2_per_objfile);
2833}
2834
f4dc4d17
DE
2835/* Reader function for dw2_build_type_unit_groups. */
2836
2837static void
2838dw2_build_type_unit_groups_reader (const struct die_reader_specs *reader,
2839 gdb_byte *info_ptr,
2840 struct die_info *type_unit_die,
2841 int has_children,
2842 void *data)
2843{
2844 struct dwarf2_cu *cu = reader->cu;
f4dc4d17
DE
2845 struct attribute *attr;
2846 struct type_unit_group *tu_group;
2847
2848 gdb_assert (data == NULL);
2849
2850 if (! has_children)
2851 return;
2852
2853 attr = dwarf2_attr_no_follow (type_unit_die, DW_AT_stmt_list);
2854 /* Call this for its side-effect of creating the associated
2855 struct type_unit_group if it doesn't already exist. */
094b34ac 2856 tu_group = get_type_unit_group (cu, attr);
f4dc4d17
DE
2857}
2858
2859/* Build dwarf2_per_objfile->type_unit_groups.
2860 This function may be called multiple times. */
2861
2862static void
2863dw2_build_type_unit_groups (void)
2864{
2865 if (dwarf2_per_objfile->type_unit_groups == NULL)
2866 build_type_unit_groups (dw2_build_type_unit_groups_reader, NULL);
2867}
2868
dee91e82 2869/* die_reader_func for dw2_get_file_names. */
2fdf6df6 2870
dee91e82
DE
2871static void
2872dw2_get_file_names_reader (const struct die_reader_specs *reader,
2873 gdb_byte *info_ptr,
2874 struct die_info *comp_unit_die,
2875 int has_children,
2876 void *data)
9291a0cd 2877{
dee91e82
DE
2878 struct dwarf2_cu *cu = reader->cu;
2879 struct dwarf2_per_cu_data *this_cu = cu->per_cu;
2880 struct objfile *objfile = dwarf2_per_objfile->objfile;
094b34ac 2881 struct dwarf2_per_cu_data *lh_cu;
7b9f3c50 2882 struct line_header *lh;
9291a0cd 2883 struct attribute *attr;
dee91e82 2884 int i;
9291a0cd 2885 char *name, *comp_dir;
7b9f3c50
DE
2886 void **slot;
2887 struct quick_file_names *qfn;
2888 unsigned int line_offset;
9291a0cd 2889
07261596
TT
2890 /* Our callers never want to match partial units -- instead they
2891 will match the enclosing full CU. */
2892 if (comp_unit_die->tag == DW_TAG_partial_unit)
2893 {
2894 this_cu->v.quick->no_file_data = 1;
2895 return;
2896 }
2897
094b34ac
DE
2898 /* If we're reading the line header for TUs, store it in the "per_cu"
2899 for tu_group. */
2900 if (this_cu->is_debug_types)
2901 {
2902 struct type_unit_group *tu_group = data;
2903
2904 gdb_assert (tu_group != NULL);
2905 lh_cu = &tu_group->per_cu;
2906 }
2907 else
2908 lh_cu = this_cu;
2909
7b9f3c50
DE
2910 lh = NULL;
2911 slot = NULL;
2912 line_offset = 0;
dee91e82
DE
2913
2914 attr = dwarf2_attr (comp_unit_die, DW_AT_stmt_list, cu);
9291a0cd
TT
2915 if (attr)
2916 {
7b9f3c50
DE
2917 struct quick_file_names find_entry;
2918
2919 line_offset = DW_UNSND (attr);
2920
2921 /* We may have already read in this line header (TU line header sharing).
2922 If we have we're done. */
094b34ac
DE
2923 find_entry.hash.dwo_unit = cu->dwo_unit;
2924 find_entry.hash.line_offset.sect_off = line_offset;
7b9f3c50
DE
2925 slot = htab_find_slot (dwarf2_per_objfile->quick_file_names_table,
2926 &find_entry, INSERT);
2927 if (*slot != NULL)
2928 {
094b34ac 2929 lh_cu->v.quick->file_names = *slot;
dee91e82 2930 return;
7b9f3c50
DE
2931 }
2932
3019eac3 2933 lh = dwarf_decode_line_header (line_offset, cu);
9291a0cd
TT
2934 }
2935 if (lh == NULL)
2936 {
094b34ac 2937 lh_cu->v.quick->no_file_data = 1;
dee91e82 2938 return;
9291a0cd
TT
2939 }
2940
7b9f3c50 2941 qfn = obstack_alloc (&objfile->objfile_obstack, sizeof (*qfn));
094b34ac
DE
2942 qfn->hash.dwo_unit = cu->dwo_unit;
2943 qfn->hash.line_offset.sect_off = line_offset;
7b9f3c50
DE
2944 gdb_assert (slot != NULL);
2945 *slot = qfn;
9291a0cd 2946
dee91e82 2947 find_file_and_directory (comp_unit_die, cu, &name, &comp_dir);
9291a0cd 2948
7b9f3c50
DE
2949 qfn->num_file_names = lh->num_file_names;
2950 qfn->file_names = obstack_alloc (&objfile->objfile_obstack,
2951 lh->num_file_names * sizeof (char *));
9291a0cd 2952 for (i = 0; i < lh->num_file_names; ++i)
7b9f3c50
DE
2953 qfn->file_names[i] = file_full_name (i + 1, lh, comp_dir);
2954 qfn->real_names = NULL;
9291a0cd 2955
7b9f3c50 2956 free_line_header (lh);
7b9f3c50 2957
094b34ac 2958 lh_cu->v.quick->file_names = qfn;
dee91e82
DE
2959}
2960
2961/* A helper for the "quick" functions which attempts to read the line
2962 table for THIS_CU. */
2963
2964static struct quick_file_names *
2965dw2_get_file_names (struct objfile *objfile,
2966 struct dwarf2_per_cu_data *this_cu)
2967{
f4dc4d17
DE
2968 /* For TUs this should only be called on the parent group. */
2969 if (this_cu->is_debug_types)
2970 gdb_assert (IS_TYPE_UNIT_GROUP (this_cu));
2971
dee91e82
DE
2972 if (this_cu->v.quick->file_names != NULL)
2973 return this_cu->v.quick->file_names;
2974 /* If we know there is no line data, no point in looking again. */
2975 if (this_cu->v.quick->no_file_data)
2976 return NULL;
2977
3019eac3
DE
2978 /* If DWO files are in use, we can still find the DW_AT_stmt_list attribute
2979 in the stub for CUs, there's is no need to lookup the DWO file.
2980 However, that's not the case for TUs where DW_AT_stmt_list lives in the
2981 DWO file. */
2982 if (this_cu->is_debug_types)
094b34ac
DE
2983 {
2984 struct type_unit_group *tu_group = this_cu->s.type_unit_group;
2985
2986 init_cutu_and_read_dies (tu_group->t.first_tu, NULL, 0, 0,
2987 dw2_get_file_names_reader, tu_group);
2988 }
3019eac3
DE
2989 else
2990 init_cutu_and_read_dies_simple (this_cu, dw2_get_file_names_reader, NULL);
dee91e82
DE
2991
2992 if (this_cu->v.quick->no_file_data)
2993 return NULL;
2994 return this_cu->v.quick->file_names;
9291a0cd
TT
2995}
2996
2997/* A helper for the "quick" functions which computes and caches the
7b9f3c50 2998 real path for a given file name from the line table. */
2fdf6df6 2999
9291a0cd 3000static const char *
7b9f3c50
DE
3001dw2_get_real_path (struct objfile *objfile,
3002 struct quick_file_names *qfn, int index)
9291a0cd 3003{
7b9f3c50
DE
3004 if (qfn->real_names == NULL)
3005 qfn->real_names = OBSTACK_CALLOC (&objfile->objfile_obstack,
3006 qfn->num_file_names, sizeof (char *));
9291a0cd 3007
7b9f3c50
DE
3008 if (qfn->real_names[index] == NULL)
3009 qfn->real_names[index] = gdb_realpath (qfn->file_names[index]);
9291a0cd 3010
7b9f3c50 3011 return qfn->real_names[index];
9291a0cd
TT
3012}
3013
3014static struct symtab *
3015dw2_find_last_source_symtab (struct objfile *objfile)
3016{
3017 int index;
ae2de4f8 3018
9291a0cd
TT
3019 dw2_setup (objfile);
3020 index = dwarf2_per_objfile->n_comp_units - 1;
a0f42c21 3021 return dw2_instantiate_symtab (dw2_get_cu (index));
9291a0cd
TT
3022}
3023
7b9f3c50
DE
3024/* Traversal function for dw2_forget_cached_source_info. */
3025
3026static int
3027dw2_free_cached_file_names (void **slot, void *info)
9291a0cd 3028{
7b9f3c50 3029 struct quick_file_names *file_data = (struct quick_file_names *) *slot;
9291a0cd 3030
7b9f3c50 3031 if (file_data->real_names)
9291a0cd 3032 {
7b9f3c50 3033 int i;
9291a0cd 3034
7b9f3c50 3035 for (i = 0; i < file_data->num_file_names; ++i)
9291a0cd 3036 {
7b9f3c50
DE
3037 xfree ((void*) file_data->real_names[i]);
3038 file_data->real_names[i] = NULL;
9291a0cd
TT
3039 }
3040 }
7b9f3c50
DE
3041
3042 return 1;
3043}
3044
3045static void
3046dw2_forget_cached_source_info (struct objfile *objfile)
3047{
3048 dw2_setup (objfile);
3049
3050 htab_traverse_noresize (dwarf2_per_objfile->quick_file_names_table,
3051 dw2_free_cached_file_names, NULL);
9291a0cd
TT
3052}
3053
f8eba3c6
TT
3054/* Helper function for dw2_map_symtabs_matching_filename that expands
3055 the symtabs and calls the iterator. */
3056
3057static int
3058dw2_map_expand_apply (struct objfile *objfile,
3059 struct dwarf2_per_cu_data *per_cu,
3060 const char *name,
3061 const char *full_path, const char *real_path,
3062 int (*callback) (struct symtab *, void *),
3063 void *data)
3064{
3065 struct symtab *last_made = objfile->symtabs;
3066
3067 /* Don't visit already-expanded CUs. */
3068 if (per_cu->v.quick->symtab)
3069 return 0;
3070
3071 /* This may expand more than one symtab, and we want to iterate over
3072 all of them. */
a0f42c21 3073 dw2_instantiate_symtab (per_cu);
f8eba3c6
TT
3074
3075 return iterate_over_some_symtabs (name, full_path, real_path, callback, data,
3076 objfile->symtabs, last_made);
3077}
3078
3079/* Implementation of the map_symtabs_matching_filename method. */
3080
9291a0cd 3081static int
f8eba3c6
TT
3082dw2_map_symtabs_matching_filename (struct objfile *objfile, const char *name,
3083 const char *full_path, const char *real_path,
3084 int (*callback) (struct symtab *, void *),
3085 void *data)
9291a0cd
TT
3086{
3087 int i;
c011a4f4 3088 const char *name_basename = lbasename (name);
4aac40c8
TT
3089 int name_len = strlen (name);
3090 int is_abs = IS_ABSOLUTE_PATH (name);
9291a0cd
TT
3091
3092 dw2_setup (objfile);
ae2de4f8 3093
f4dc4d17
DE
3094 dw2_build_type_unit_groups ();
3095
1fd400ff 3096 for (i = 0; i < (dwarf2_per_objfile->n_comp_units
f4dc4d17 3097 + dwarf2_per_objfile->n_type_unit_groups); ++i)
9291a0cd
TT
3098 {
3099 int j;
f4dc4d17 3100 struct dwarf2_per_cu_data *per_cu = dw2_get_primary_cu (i);
7b9f3c50 3101 struct quick_file_names *file_data;
9291a0cd 3102
3d7bb9d9 3103 /* We only need to look at symtabs not already expanded. */
e254ef6a 3104 if (per_cu->v.quick->symtab)
9291a0cd
TT
3105 continue;
3106
7b9f3c50
DE
3107 file_data = dw2_get_file_names (objfile, per_cu);
3108 if (file_data == NULL)
9291a0cd
TT
3109 continue;
3110
7b9f3c50 3111 for (j = 0; j < file_data->num_file_names; ++j)
9291a0cd 3112 {
7b9f3c50 3113 const char *this_name = file_data->file_names[j];
9291a0cd 3114
4aac40c8
TT
3115 if (FILENAME_CMP (name, this_name) == 0
3116 || (!is_abs && compare_filenames_for_search (this_name,
3117 name, name_len)))
9291a0cd 3118 {
f8eba3c6
TT
3119 if (dw2_map_expand_apply (objfile, per_cu,
3120 name, full_path, real_path,
3121 callback, data))
3122 return 1;
4aac40c8 3123 }
9291a0cd 3124
c011a4f4
DE
3125 /* Before we invoke realpath, which can get expensive when many
3126 files are involved, do a quick comparison of the basenames. */
3127 if (! basenames_may_differ
3128 && FILENAME_CMP (lbasename (this_name), name_basename) != 0)
3129 continue;
3130
9291a0cd
TT
3131 if (full_path != NULL)
3132 {
7b9f3c50
DE
3133 const char *this_real_name = dw2_get_real_path (objfile,
3134 file_data, j);
9291a0cd 3135
7b9f3c50 3136 if (this_real_name != NULL
4aac40c8
TT
3137 && (FILENAME_CMP (full_path, this_real_name) == 0
3138 || (!is_abs
3139 && compare_filenames_for_search (this_real_name,
3140 name, name_len))))
9291a0cd 3141 {
f8eba3c6
TT
3142 if (dw2_map_expand_apply (objfile, per_cu,
3143 name, full_path, real_path,
3144 callback, data))
3145 return 1;
9291a0cd
TT
3146 }
3147 }
3148
3149 if (real_path != NULL)
3150 {
7b9f3c50
DE
3151 const char *this_real_name = dw2_get_real_path (objfile,
3152 file_data, j);
9291a0cd 3153
7b9f3c50 3154 if (this_real_name != NULL
4aac40c8
TT
3155 && (FILENAME_CMP (real_path, this_real_name) == 0
3156 || (!is_abs
3157 && compare_filenames_for_search (this_real_name,
3158 name, name_len))))
9291a0cd 3159 {
f8eba3c6
TT
3160 if (dw2_map_expand_apply (objfile, per_cu,
3161 name, full_path, real_path,
3162 callback, data))
3163 return 1;
9291a0cd
TT
3164 }
3165 }
3166 }
3167 }
3168
9291a0cd
TT
3169 return 0;
3170}
3171
3172static struct symtab *
3173dw2_lookup_symbol (struct objfile *objfile, int block_index,
3174 const char *name, domain_enum domain)
3175{
774b6a14 3176 /* We do all the work in the pre_expand_symtabs_matching hook
9291a0cd
TT
3177 instead. */
3178 return NULL;
3179}
3180
3181/* A helper function that expands all symtabs that hold an object
156942c7
DE
3182 named NAME. If WANT_SPECIFIC_BLOCK is non-zero, only look for
3183 symbols in block BLOCK_KIND. */
2fdf6df6 3184
9291a0cd 3185static void
156942c7
DE
3186dw2_do_expand_symtabs_matching (struct objfile *objfile,
3187 int want_specific_block,
3188 enum block_enum block_kind,
3189 const char *name, domain_enum domain)
9291a0cd 3190{
156942c7
DE
3191 struct mapped_index *index;
3192
9291a0cd
TT
3193 dw2_setup (objfile);
3194
156942c7
DE
3195 index = dwarf2_per_objfile->index_table;
3196
ae2de4f8 3197 /* index_table is NULL if OBJF_READNOW. */
156942c7 3198 if (index)
9291a0cd
TT
3199 {
3200 offset_type *vec;
3201
156942c7 3202 if (find_slot_in_mapped_hash (index, name, &vec))
9291a0cd
TT
3203 {
3204 offset_type i, len = MAYBE_SWAP (*vec);
3205 for (i = 0; i < len; ++i)
3206 {
156942c7
DE
3207 offset_type cu_index_and_attrs = MAYBE_SWAP (vec[i + 1]);
3208 offset_type cu_index = GDB_INDEX_CU_VALUE (cu_index_and_attrs);
e254ef6a 3209 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (cu_index);
156942c7
DE
3210 int want_static = block_kind != GLOBAL_BLOCK;
3211 /* This value is only valid for index versions >= 7. */
3212 int is_static = GDB_INDEX_SYMBOL_STATIC_VALUE (cu_index_and_attrs);
3213 gdb_index_symbol_kind symbol_kind =
3214 GDB_INDEX_SYMBOL_KIND_VALUE (cu_index_and_attrs);
83a788b8
DE
3215 /* Only check the symbol attributes if they're present.
3216 Indices prior to version 7 don't record them,
3217 and indices >= 7 may elide them for certain symbols
3218 (gold does this). */
3219 int attrs_valid =
3220 (index->version >= 7
3221 && symbol_kind != GDB_INDEX_SYMBOL_KIND_NONE);
3222
3223 if (attrs_valid
3224 && want_specific_block
156942c7
DE
3225 && want_static != is_static)
3226 continue;
3227
83a788b8
DE
3228 /* Only check the symbol's kind if it has one. */
3229 if (attrs_valid)
156942c7
DE
3230 {
3231 switch (domain)
3232 {
3233 case VAR_DOMAIN:
3234 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_VARIABLE
3235 && symbol_kind != GDB_INDEX_SYMBOL_KIND_FUNCTION
3236 /* Some types are also in VAR_DOMAIN. */
3237 && symbol_kind != GDB_INDEX_SYMBOL_KIND_TYPE)
3238 continue;
3239 break;
3240 case STRUCT_DOMAIN:
3241 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_TYPE)
3242 continue;
3243 break;
3244 case LABEL_DOMAIN:
3245 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_OTHER)
3246 continue;
3247 break;
3248 default:
3249 break;
3250 }
3251 }
1fd400ff 3252
a0f42c21 3253 dw2_instantiate_symtab (per_cu);
9291a0cd
TT
3254 }
3255 }
3256 }
3257}
3258
774b6a14
TT
3259static void
3260dw2_pre_expand_symtabs_matching (struct objfile *objfile,
8903c50d 3261 enum block_enum block_kind, const char *name,
774b6a14 3262 domain_enum domain)
9291a0cd 3263{
156942c7 3264 dw2_do_expand_symtabs_matching (objfile, 1, block_kind, name, domain);
9291a0cd
TT
3265}
3266
3267static void
3268dw2_print_stats (struct objfile *objfile)
3269{
3270 int i, count;
3271
3272 dw2_setup (objfile);
3273 count = 0;
1fd400ff 3274 for (i = 0; i < (dwarf2_per_objfile->n_comp_units
d467dd73 3275 + dwarf2_per_objfile->n_type_units); ++i)
9291a0cd 3276 {
e254ef6a 3277 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
9291a0cd 3278
e254ef6a 3279 if (!per_cu->v.quick->symtab)
9291a0cd
TT
3280 ++count;
3281 }
3282 printf_filtered (_(" Number of unread CUs: %d\n"), count);
3283}
3284
3285static void
3286dw2_dump (struct objfile *objfile)
3287{
3288 /* Nothing worth printing. */
3289}
3290
3291static void
3292dw2_relocate (struct objfile *objfile, struct section_offsets *new_offsets,
3293 struct section_offsets *delta)
3294{
3295 /* There's nothing to relocate here. */
3296}
3297
3298static void
3299dw2_expand_symtabs_for_function (struct objfile *objfile,
3300 const char *func_name)
3301{
156942c7
DE
3302 /* Note: It doesn't matter what we pass for block_kind here. */
3303 dw2_do_expand_symtabs_matching (objfile, 0, GLOBAL_BLOCK, func_name,
3304 VAR_DOMAIN);
9291a0cd
TT
3305}
3306
3307static void
3308dw2_expand_all_symtabs (struct objfile *objfile)
3309{
3310 int i;
3311
3312 dw2_setup (objfile);
1fd400ff
TT
3313
3314 for (i = 0; i < (dwarf2_per_objfile->n_comp_units
d467dd73 3315 + dwarf2_per_objfile->n_type_units); ++i)
9291a0cd 3316 {
e254ef6a 3317 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
9291a0cd 3318
a0f42c21 3319 dw2_instantiate_symtab (per_cu);
9291a0cd
TT
3320 }
3321}
3322
3323static void
3324dw2_expand_symtabs_with_filename (struct objfile *objfile,
3325 const char *filename)
3326{
3327 int i;
3328
3329 dw2_setup (objfile);
d4637a04
DE
3330
3331 /* We don't need to consider type units here.
3332 This is only called for examining code, e.g. expand_line_sal.
3333 There can be an order of magnitude (or more) more type units
3334 than comp units, and we avoid them if we can. */
3335
3336 for (i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
9291a0cd
TT
3337 {
3338 int j;
e254ef6a 3339 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
7b9f3c50 3340 struct quick_file_names *file_data;
9291a0cd 3341
3d7bb9d9 3342 /* We only need to look at symtabs not already expanded. */
e254ef6a 3343 if (per_cu->v.quick->symtab)
9291a0cd
TT
3344 continue;
3345
7b9f3c50
DE
3346 file_data = dw2_get_file_names (objfile, per_cu);
3347 if (file_data == NULL)
9291a0cd
TT
3348 continue;
3349
7b9f3c50 3350 for (j = 0; j < file_data->num_file_names; ++j)
9291a0cd 3351 {
7b9f3c50 3352 const char *this_name = file_data->file_names[j];
1ef75ecc 3353 if (FILENAME_CMP (this_name, filename) == 0)
9291a0cd 3354 {
a0f42c21 3355 dw2_instantiate_symtab (per_cu);
9291a0cd
TT
3356 break;
3357 }
3358 }
3359 }
3360}
3361
356d9f9d
TT
3362/* A helper function for dw2_find_symbol_file that finds the primary
3363 file name for a given CU. This is a die_reader_func. */
3364
3365static void
3366dw2_get_primary_filename_reader (const struct die_reader_specs *reader,
3367 gdb_byte *info_ptr,
3368 struct die_info *comp_unit_die,
3369 int has_children,
3370 void *data)
3371{
3372 const char **result_ptr = data;
3373 struct dwarf2_cu *cu = reader->cu;
3374 struct attribute *attr;
3375
3376 attr = dwarf2_attr (comp_unit_die, DW_AT_name, cu);
3377 if (attr == NULL)
3378 *result_ptr = NULL;
3379 else
3380 *result_ptr = DW_STRING (attr);
3381}
3382
dd786858 3383static const char *
9291a0cd
TT
3384dw2_find_symbol_file (struct objfile *objfile, const char *name)
3385{
e254ef6a 3386 struct dwarf2_per_cu_data *per_cu;
9291a0cd 3387 offset_type *vec;
7b9f3c50 3388 struct quick_file_names *file_data;
356d9f9d 3389 const char *filename;
9291a0cd
TT
3390
3391 dw2_setup (objfile);
3392
ae2de4f8 3393 /* index_table is NULL if OBJF_READNOW. */
9291a0cd 3394 if (!dwarf2_per_objfile->index_table)
96408a79
SA
3395 {
3396 struct symtab *s;
3397
d790cf0a
DE
3398 ALL_OBJFILE_PRIMARY_SYMTABS (objfile, s)
3399 {
3400 struct blockvector *bv = BLOCKVECTOR (s);
3401 const struct block *block = BLOCKVECTOR_BLOCK (bv, GLOBAL_BLOCK);
3402 struct symbol *sym = lookup_block_symbol (block, name, VAR_DOMAIN);
3403
3404 if (sym)
210bbc17 3405 return SYMBOL_SYMTAB (sym)->filename;
d790cf0a 3406 }
96408a79
SA
3407 return NULL;
3408 }
9291a0cd
TT
3409
3410 if (!find_slot_in_mapped_hash (dwarf2_per_objfile->index_table,
3411 name, &vec))
3412 return NULL;
3413
3414 /* Note that this just looks at the very first one named NAME -- but
3415 actually we are looking for a function. find_main_filename
3416 should be rewritten so that it doesn't require a custom hook. It
3417 could just use the ordinary symbol tables. */
3418 /* vec[0] is the length, which must always be >0. */
156942c7 3419 per_cu = dw2_get_cu (GDB_INDEX_CU_VALUE (MAYBE_SWAP (vec[1])));
9291a0cd 3420
356d9f9d
TT
3421 if (per_cu->v.quick->symtab != NULL)
3422 return per_cu->v.quick->symtab->filename;
3423
f4dc4d17
DE
3424 init_cutu_and_read_dies (per_cu, NULL, 0, 0,
3425 dw2_get_primary_filename_reader, &filename);
9291a0cd 3426
356d9f9d 3427 return filename;
9291a0cd
TT
3428}
3429
3430static void
40658b94
PH
3431dw2_map_matching_symbols (const char * name, domain_enum namespace,
3432 struct objfile *objfile, int global,
3433 int (*callback) (struct block *,
3434 struct symbol *, void *),
2edb89d3
JK
3435 void *data, symbol_compare_ftype *match,
3436 symbol_compare_ftype *ordered_compare)
9291a0cd 3437{
40658b94 3438 /* Currently unimplemented; used for Ada. The function can be called if the
a9e6a4bb
JK
3439 current language is Ada for a non-Ada objfile using GNU index. As Ada
3440 does not look for non-Ada symbols this function should just return. */
9291a0cd
TT
3441}
3442
3443static void
f8eba3c6
TT
3444dw2_expand_symtabs_matching
3445 (struct objfile *objfile,
3446 int (*file_matcher) (const char *, void *),
e078317b 3447 int (*name_matcher) (const char *, void *),
f8eba3c6
TT
3448 enum search_domain kind,
3449 void *data)
9291a0cd
TT
3450{
3451 int i;
3452 offset_type iter;
4b5246aa 3453 struct mapped_index *index;
9291a0cd
TT
3454
3455 dw2_setup (objfile);
ae2de4f8
DE
3456
3457 /* index_table is NULL if OBJF_READNOW. */
9291a0cd
TT
3458 if (!dwarf2_per_objfile->index_table)
3459 return;
4b5246aa 3460 index = dwarf2_per_objfile->index_table;
9291a0cd 3461
7b08b9eb 3462 if (file_matcher != NULL)
24c79950
TT
3463 {
3464 struct cleanup *cleanup;
3465 htab_t visited_found, visited_not_found;
3466
f4dc4d17
DE
3467 dw2_build_type_unit_groups ();
3468
24c79950
TT
3469 visited_found = htab_create_alloc (10,
3470 htab_hash_pointer, htab_eq_pointer,
3471 NULL, xcalloc, xfree);
3472 cleanup = make_cleanup_htab_delete (visited_found);
3473 visited_not_found = htab_create_alloc (10,
3474 htab_hash_pointer, htab_eq_pointer,
3475 NULL, xcalloc, xfree);
3476 make_cleanup_htab_delete (visited_not_found);
3477
3478 for (i = 0; i < (dwarf2_per_objfile->n_comp_units
f4dc4d17 3479 + dwarf2_per_objfile->n_type_unit_groups); ++i)
24c79950
TT
3480 {
3481 int j;
f4dc4d17 3482 struct dwarf2_per_cu_data *per_cu = dw2_get_primary_cu (i);
24c79950
TT
3483 struct quick_file_names *file_data;
3484 void **slot;
7b08b9eb 3485
24c79950 3486 per_cu->v.quick->mark = 0;
3d7bb9d9 3487
24c79950
TT
3488 /* We only need to look at symtabs not already expanded. */
3489 if (per_cu->v.quick->symtab)
3490 continue;
7b08b9eb 3491
24c79950
TT
3492 file_data = dw2_get_file_names (objfile, per_cu);
3493 if (file_data == NULL)
3494 continue;
7b08b9eb 3495
24c79950
TT
3496 if (htab_find (visited_not_found, file_data) != NULL)
3497 continue;
3498 else if (htab_find (visited_found, file_data) != NULL)
3499 {
3500 per_cu->v.quick->mark = 1;
3501 continue;
3502 }
3503
3504 for (j = 0; j < file_data->num_file_names; ++j)
3505 {
3506 if (file_matcher (file_data->file_names[j], data))
3507 {
3508 per_cu->v.quick->mark = 1;
3509 break;
3510 }
3511 }
3512
3513 slot = htab_find_slot (per_cu->v.quick->mark
3514 ? visited_found
3515 : visited_not_found,
3516 file_data, INSERT);
3517 *slot = file_data;
3518 }
3519
3520 do_cleanups (cleanup);
3521 }
9291a0cd 3522
3876f04e 3523 for (iter = 0; iter < index->symbol_table_slots; ++iter)
9291a0cd
TT
3524 {
3525 offset_type idx = 2 * iter;
3526 const char *name;
3527 offset_type *vec, vec_len, vec_idx;
3528
3876f04e 3529 if (index->symbol_table[idx] == 0 && index->symbol_table[idx + 1] == 0)
9291a0cd
TT
3530 continue;
3531
3876f04e 3532 name = index->constant_pool + MAYBE_SWAP (index->symbol_table[idx]);
9291a0cd 3533
e078317b 3534 if (! (*name_matcher) (name, data))
9291a0cd
TT
3535 continue;
3536
3537 /* The name was matched, now expand corresponding CUs that were
3538 marked. */
4b5246aa 3539 vec = (offset_type *) (index->constant_pool
3876f04e 3540 + MAYBE_SWAP (index->symbol_table[idx + 1]));
9291a0cd
TT
3541 vec_len = MAYBE_SWAP (vec[0]);
3542 for (vec_idx = 0; vec_idx < vec_len; ++vec_idx)
3543 {
e254ef6a 3544 struct dwarf2_per_cu_data *per_cu;
156942c7
DE
3545 offset_type cu_index_and_attrs = MAYBE_SWAP (vec[vec_idx + 1]);
3546 gdb_index_symbol_kind symbol_kind =
3547 GDB_INDEX_SYMBOL_KIND_VALUE (cu_index_and_attrs);
3548 int cu_index = GDB_INDEX_CU_VALUE (cu_index_and_attrs);
3549
3550 /* Don't crash on bad data. */
3551 if (cu_index >= (dwarf2_per_objfile->n_comp_units
667e0a4b 3552 + dwarf2_per_objfile->n_type_units))
156942c7 3553 continue;
1fd400ff 3554
156942c7
DE
3555 /* Only check the symbol's kind if it has one.
3556 Indices prior to version 7 don't record it. */
3557 if (index->version >= 7)
3558 {
3559 switch (kind)
3560 {
3561 case VARIABLES_DOMAIN:
3562 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_VARIABLE)
3563 continue;
3564 break;
3565 case FUNCTIONS_DOMAIN:
3566 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_FUNCTION)
3567 continue;
3568 break;
3569 case TYPES_DOMAIN:
3570 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_TYPE)
3571 continue;
3572 break;
3573 default:
3574 break;
3575 }
3576 }
3577
3578 per_cu = dw2_get_cu (cu_index);
7b08b9eb 3579 if (file_matcher == NULL || per_cu->v.quick->mark)
a0f42c21 3580 dw2_instantiate_symtab (per_cu);
9291a0cd
TT
3581 }
3582 }
3583}
3584
9703b513
TT
3585/* A helper for dw2_find_pc_sect_symtab which finds the most specific
3586 symtab. */
3587
3588static struct symtab *
3589recursively_find_pc_sect_symtab (struct symtab *symtab, CORE_ADDR pc)
3590{
3591 int i;
3592
3593 if (BLOCKVECTOR (symtab) != NULL
3594 && blockvector_contains_pc (BLOCKVECTOR (symtab), pc))
3595 return symtab;
3596
a3ec0bb1
DE
3597 if (symtab->includes == NULL)
3598 return NULL;
3599
9703b513
TT
3600 for (i = 0; symtab->includes[i]; ++i)
3601 {
a3ec0bb1 3602 struct symtab *s = symtab->includes[i];
9703b513
TT
3603
3604 s = recursively_find_pc_sect_symtab (s, pc);
3605 if (s != NULL)
3606 return s;
3607 }
3608
3609 return NULL;
3610}
3611
9291a0cd
TT
3612static struct symtab *
3613dw2_find_pc_sect_symtab (struct objfile *objfile,
3614 struct minimal_symbol *msymbol,
3615 CORE_ADDR pc,
3616 struct obj_section *section,
3617 int warn_if_readin)
3618{
3619 struct dwarf2_per_cu_data *data;
9703b513 3620 struct symtab *result;
9291a0cd
TT
3621
3622 dw2_setup (objfile);
3623
3624 if (!objfile->psymtabs_addrmap)
3625 return NULL;
3626
3627 data = addrmap_find (objfile->psymtabs_addrmap, pc);
3628 if (!data)
3629 return NULL;
3630
3631 if (warn_if_readin && data->v.quick->symtab)
abebb8b0 3632 warning (_("(Internal error: pc %s in read in CU, but not in symtab.)"),
9291a0cd
TT
3633 paddress (get_objfile_arch (objfile), pc));
3634
9703b513
TT
3635 result = recursively_find_pc_sect_symtab (dw2_instantiate_symtab (data), pc);
3636 gdb_assert (result != NULL);
3637 return result;
9291a0cd
TT
3638}
3639
9291a0cd 3640static void
44b13c5a 3641dw2_map_symbol_filenames (struct objfile *objfile, symbol_filename_ftype *fun,
74e2f255 3642 void *data, int need_fullname)
9291a0cd
TT
3643{
3644 int i;
24c79950
TT
3645 struct cleanup *cleanup;
3646 htab_t visited = htab_create_alloc (10, htab_hash_pointer, htab_eq_pointer,
3647 NULL, xcalloc, xfree);
9291a0cd 3648
24c79950 3649 cleanup = make_cleanup_htab_delete (visited);
9291a0cd 3650 dw2_setup (objfile);
ae2de4f8 3651
f4dc4d17
DE
3652 dw2_build_type_unit_groups ();
3653
24c79950
TT
3654 /* We can ignore file names coming from already-expanded CUs. */
3655 for (i = 0; i < (dwarf2_per_objfile->n_comp_units
3656 + dwarf2_per_objfile->n_type_units); ++i)
3657 {
3658 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
3659
3660 if (per_cu->v.quick->symtab)
3661 {
3662 void **slot = htab_find_slot (visited, per_cu->v.quick->file_names,
3663 INSERT);
3664
3665 *slot = per_cu->v.quick->file_names;
3666 }
3667 }
3668
1fd400ff 3669 for (i = 0; i < (dwarf2_per_objfile->n_comp_units
f4dc4d17 3670 + dwarf2_per_objfile->n_type_unit_groups); ++i)
9291a0cd
TT
3671 {
3672 int j;
f4dc4d17 3673 struct dwarf2_per_cu_data *per_cu = dw2_get_primary_cu (i);
7b9f3c50 3674 struct quick_file_names *file_data;
24c79950 3675 void **slot;
9291a0cd 3676
3d7bb9d9 3677 /* We only need to look at symtabs not already expanded. */
e254ef6a 3678 if (per_cu->v.quick->symtab)
9291a0cd
TT
3679 continue;
3680
7b9f3c50
DE
3681 file_data = dw2_get_file_names (objfile, per_cu);
3682 if (file_data == NULL)
9291a0cd
TT
3683 continue;
3684
24c79950
TT
3685 slot = htab_find_slot (visited, file_data, INSERT);
3686 if (*slot)
3687 {
3688 /* Already visited. */
3689 continue;
3690 }
3691 *slot = file_data;
3692
7b9f3c50 3693 for (j = 0; j < file_data->num_file_names; ++j)
9291a0cd 3694 {
74e2f255
DE
3695 const char *this_real_name;
3696
3697 if (need_fullname)
3698 this_real_name = dw2_get_real_path (objfile, file_data, j);
3699 else
3700 this_real_name = NULL;
7b9f3c50 3701 (*fun) (file_data->file_names[j], this_real_name, data);
9291a0cd
TT
3702 }
3703 }
24c79950
TT
3704
3705 do_cleanups (cleanup);
9291a0cd
TT
3706}
3707
3708static int
3709dw2_has_symbols (struct objfile *objfile)
3710{
3711 return 1;
3712}
3713
3714const struct quick_symbol_functions dwarf2_gdb_index_functions =
3715{
3716 dw2_has_symbols,
3717 dw2_find_last_source_symtab,
3718 dw2_forget_cached_source_info,
f8eba3c6 3719 dw2_map_symtabs_matching_filename,
9291a0cd 3720 dw2_lookup_symbol,
774b6a14 3721 dw2_pre_expand_symtabs_matching,
9291a0cd
TT
3722 dw2_print_stats,
3723 dw2_dump,
3724 dw2_relocate,
3725 dw2_expand_symtabs_for_function,
3726 dw2_expand_all_symtabs,
3727 dw2_expand_symtabs_with_filename,
3728 dw2_find_symbol_file,
40658b94 3729 dw2_map_matching_symbols,
9291a0cd
TT
3730 dw2_expand_symtabs_matching,
3731 dw2_find_pc_sect_symtab,
9291a0cd
TT
3732 dw2_map_symbol_filenames
3733};
3734
3735/* Initialize for reading DWARF for this objfile. Return 0 if this
3736 file will use psymtabs, or 1 if using the GNU index. */
3737
3738int
3739dwarf2_initialize_objfile (struct objfile *objfile)
3740{
3741 /* If we're about to read full symbols, don't bother with the
3742 indices. In this case we also don't care if some other debug
3743 format is making psymtabs, because they are all about to be
3744 expanded anyway. */
3745 if ((objfile->flags & OBJF_READNOW))
3746 {
3747 int i;
3748
3749 dwarf2_per_objfile->using_index = 1;
3750 create_all_comp_units (objfile);
0e50663e 3751 create_all_type_units (objfile);
7b9f3c50
DE
3752 dwarf2_per_objfile->quick_file_names_table =
3753 create_quick_file_names_table (dwarf2_per_objfile->n_comp_units);
9291a0cd 3754
1fd400ff 3755 for (i = 0; i < (dwarf2_per_objfile->n_comp_units
d467dd73 3756 + dwarf2_per_objfile->n_type_units); ++i)
9291a0cd 3757 {
e254ef6a 3758 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
9291a0cd 3759
e254ef6a
DE
3760 per_cu->v.quick = OBSTACK_ZALLOC (&objfile->objfile_obstack,
3761 struct dwarf2_per_cu_quick_data);
9291a0cd
TT
3762 }
3763
3764 /* Return 1 so that gdb sees the "quick" functions. However,
3765 these functions will be no-ops because we will have expanded
3766 all symtabs. */
3767 return 1;
3768 }
3769
3770 if (dwarf2_read_index (objfile))
3771 return 1;
3772
9291a0cd
TT
3773 return 0;
3774}
3775
3776\f
3777
dce234bc
PP
3778/* Build a partial symbol table. */
3779
3780void
f29dff0a 3781dwarf2_build_psymtabs (struct objfile *objfile)
dce234bc 3782{
f29dff0a 3783 if (objfile->global_psymbols.size == 0 && objfile->static_psymbols.size == 0)
c906108c
SS
3784 {
3785 init_psymbol_list (objfile, 1024);
3786 }
3787
d146bf1e 3788 dwarf2_build_psymtabs_hard (objfile);
c906108c 3789}
c906108c 3790
1ce1cefd
DE
3791/* Return the total length of the CU described by HEADER. */
3792
3793static unsigned int
3794get_cu_length (const struct comp_unit_head *header)
3795{
3796 return header->initial_length_size + header->length;
3797}
3798
45452591
DE
3799/* Return TRUE if OFFSET is within CU_HEADER. */
3800
3801static inline int
b64f50a1 3802offset_in_cu_p (const struct comp_unit_head *cu_header, sect_offset offset)
45452591 3803{
b64f50a1 3804 sect_offset bottom = { cu_header->offset.sect_off };
1ce1cefd 3805 sect_offset top = { cu_header->offset.sect_off + get_cu_length (cu_header) };
9a619af0 3806
b64f50a1 3807 return (offset.sect_off >= bottom.sect_off && offset.sect_off < top.sect_off);
45452591
DE
3808}
3809
3b80fe9b
DE
3810/* Find the base address of the compilation unit for range lists and
3811 location lists. It will normally be specified by DW_AT_low_pc.
3812 In DWARF-3 draft 4, the base address could be overridden by
3813 DW_AT_entry_pc. It's been removed, but GCC still uses this for
3814 compilation units with discontinuous ranges. */
3815
3816static void
3817dwarf2_find_base_address (struct die_info *die, struct dwarf2_cu *cu)
3818{
3819 struct attribute *attr;
3820
3821 cu->base_known = 0;
3822 cu->base_address = 0;
3823
3824 attr = dwarf2_attr (die, DW_AT_entry_pc, cu);
3825 if (attr)
3826 {
3827 cu->base_address = DW_ADDR (attr);
3828 cu->base_known = 1;
3829 }
3830 else
3831 {
3832 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
3833 if (attr)
3834 {
3835 cu->base_address = DW_ADDR (attr);
3836 cu->base_known = 1;
3837 }
3838 }
3839}
3840
93311388
DE
3841/* Read in the comp unit header information from the debug_info at info_ptr.
3842 NOTE: This leaves members offset, first_die_offset to be filled in
3843 by the caller. */
107d2387 3844
fe1b8b76 3845static gdb_byte *
107d2387 3846read_comp_unit_head (struct comp_unit_head *cu_header,
fe1b8b76 3847 gdb_byte *info_ptr, bfd *abfd)
107d2387
AC
3848{
3849 int signed_addr;
891d2f0b 3850 unsigned int bytes_read;
c764a876
DE
3851
3852 cu_header->length = read_initial_length (abfd, info_ptr, &bytes_read);
3853 cu_header->initial_length_size = bytes_read;
3854 cu_header->offset_size = (bytes_read == 4) ? 4 : 8;
613e1657 3855 info_ptr += bytes_read;
107d2387
AC
3856 cu_header->version = read_2_bytes (abfd, info_ptr);
3857 info_ptr += 2;
b64f50a1
JK
3858 cu_header->abbrev_offset.sect_off = read_offset (abfd, info_ptr, cu_header,
3859 &bytes_read);
613e1657 3860 info_ptr += bytes_read;
107d2387
AC
3861 cu_header->addr_size = read_1_byte (abfd, info_ptr);
3862 info_ptr += 1;
3863 signed_addr = bfd_get_sign_extend_vma (abfd);
3864 if (signed_addr < 0)
8e65ff28 3865 internal_error (__FILE__, __LINE__,
e2e0b3e5 3866 _("read_comp_unit_head: dwarf from non elf file"));
107d2387 3867 cu_header->signed_addr_p = signed_addr;
c764a876 3868
107d2387
AC
3869 return info_ptr;
3870}
3871
36586728
TT
3872/* Helper function that returns the proper abbrev section for
3873 THIS_CU. */
3874
3875static struct dwarf2_section_info *
3876get_abbrev_section_for_cu (struct dwarf2_per_cu_data *this_cu)
3877{
3878 struct dwarf2_section_info *abbrev;
3879
3880 if (this_cu->is_dwz)
3881 abbrev = &dwarf2_get_dwz_file ()->abbrev;
3882 else
3883 abbrev = &dwarf2_per_objfile->abbrev;
3884
3885 return abbrev;
3886}
3887
9ff913ba
DE
3888/* Subroutine of read_and_check_comp_unit_head and
3889 read_and_check_type_unit_head to simplify them.
3890 Perform various error checking on the header. */
3891
3892static void
3893error_check_comp_unit_head (struct comp_unit_head *header,
4bdcc0c1
DE
3894 struct dwarf2_section_info *section,
3895 struct dwarf2_section_info *abbrev_section)
9ff913ba
DE
3896{
3897 bfd *abfd = section->asection->owner;
3898 const char *filename = bfd_get_filename (abfd);
3899
3900 if (header->version != 2 && header->version != 3 && header->version != 4)
3901 error (_("Dwarf Error: wrong version in compilation unit header "
3902 "(is %d, should be 2, 3, or 4) [in module %s]"), header->version,
3903 filename);
3904
b64f50a1 3905 if (header->abbrev_offset.sect_off
36586728 3906 >= dwarf2_section_size (dwarf2_per_objfile->objfile, abbrev_section))
9ff913ba
DE
3907 error (_("Dwarf Error: bad offset (0x%lx) in compilation unit header "
3908 "(offset 0x%lx + 6) [in module %s]"),
b64f50a1 3909 (long) header->abbrev_offset.sect_off, (long) header->offset.sect_off,
9ff913ba
DE
3910 filename);
3911
3912 /* Cast to unsigned long to use 64-bit arithmetic when possible to
3913 avoid potential 32-bit overflow. */
1ce1cefd 3914 if (((unsigned long) header->offset.sect_off + get_cu_length (header))
9ff913ba
DE
3915 > section->size)
3916 error (_("Dwarf Error: bad length (0x%lx) in compilation unit header "
3917 "(offset 0x%lx + 0) [in module %s]"),
b64f50a1 3918 (long) header->length, (long) header->offset.sect_off,
9ff913ba
DE
3919 filename);
3920}
3921
3922/* Read in a CU/TU header and perform some basic error checking.
3923 The contents of the header are stored in HEADER.
3924 The result is a pointer to the start of the first DIE. */
adabb602 3925
fe1b8b76 3926static gdb_byte *
9ff913ba
DE
3927read_and_check_comp_unit_head (struct comp_unit_head *header,
3928 struct dwarf2_section_info *section,
4bdcc0c1 3929 struct dwarf2_section_info *abbrev_section,
9ff913ba
DE
3930 gdb_byte *info_ptr,
3931 int is_debug_types_section)
72bf9492 3932{
fe1b8b76 3933 gdb_byte *beg_of_comp_unit = info_ptr;
9ff913ba 3934 bfd *abfd = section->asection->owner;
72bf9492 3935
b64f50a1 3936 header->offset.sect_off = beg_of_comp_unit - section->buffer;
adabb602 3937
72bf9492
DJ
3938 info_ptr = read_comp_unit_head (header, info_ptr, abfd);
3939
460c1c54
CC
3940 /* If we're reading a type unit, skip over the signature and
3941 type_offset fields. */
b0df02fd 3942 if (is_debug_types_section)
460c1c54
CC
3943 info_ptr += 8 /*signature*/ + header->offset_size;
3944
b64f50a1 3945 header->first_die_offset.cu_off = info_ptr - beg_of_comp_unit;
adabb602 3946
4bdcc0c1 3947 error_check_comp_unit_head (header, section, abbrev_section);
72bf9492
DJ
3948
3949 return info_ptr;
3950}
3951
348e048f
DE
3952/* Read in the types comp unit header information from .debug_types entry at
3953 types_ptr. The result is a pointer to one past the end of the header. */
3954
3955static gdb_byte *
9ff913ba
DE
3956read_and_check_type_unit_head (struct comp_unit_head *header,
3957 struct dwarf2_section_info *section,
4bdcc0c1 3958 struct dwarf2_section_info *abbrev_section,
9ff913ba 3959 gdb_byte *info_ptr,
dee91e82
DE
3960 ULONGEST *signature,
3961 cu_offset *type_offset_in_tu)
348e048f 3962{
9ff913ba
DE
3963 gdb_byte *beg_of_comp_unit = info_ptr;
3964 bfd *abfd = section->asection->owner;
348e048f 3965
b64f50a1 3966 header->offset.sect_off = beg_of_comp_unit - section->buffer;
348e048f 3967
9ff913ba 3968 info_ptr = read_comp_unit_head (header, info_ptr, abfd);
348e048f 3969
9ff913ba
DE
3970 /* If we're reading a type unit, skip over the signature and
3971 type_offset fields. */
3972 if (signature != NULL)
3973 *signature = read_8_bytes (abfd, info_ptr);
3974 info_ptr += 8;
dee91e82
DE
3975 if (type_offset_in_tu != NULL)
3976 type_offset_in_tu->cu_off = read_offset_1 (abfd, info_ptr,
3977 header->offset_size);
9ff913ba
DE
3978 info_ptr += header->offset_size;
3979
b64f50a1 3980 header->first_die_offset.cu_off = info_ptr - beg_of_comp_unit;
348e048f 3981
4bdcc0c1 3982 error_check_comp_unit_head (header, section, abbrev_section);
9ff913ba
DE
3983
3984 return info_ptr;
348e048f
DE
3985}
3986
f4dc4d17
DE
3987/* Fetch the abbreviation table offset from a comp or type unit header. */
3988
3989static sect_offset
3990read_abbrev_offset (struct dwarf2_section_info *section,
3991 sect_offset offset)
3992{
3993 bfd *abfd = section->asection->owner;
3994 gdb_byte *info_ptr;
3995 unsigned int length, initial_length_size, offset_size;
3996 sect_offset abbrev_offset;
3997
3998 dwarf2_read_section (dwarf2_per_objfile->objfile, section);
3999 info_ptr = section->buffer + offset.sect_off;
4000 length = read_initial_length (abfd, info_ptr, &initial_length_size);
4001 offset_size = initial_length_size == 4 ? 4 : 8;
4002 info_ptr += initial_length_size + 2 /*version*/;
4003 abbrev_offset.sect_off = read_offset_1 (abfd, info_ptr, offset_size);
4004 return abbrev_offset;
4005}
4006
aaa75496
JB
4007/* Allocate a new partial symtab for file named NAME and mark this new
4008 partial symtab as being an include of PST. */
4009
4010static void
4011dwarf2_create_include_psymtab (char *name, struct partial_symtab *pst,
4012 struct objfile *objfile)
4013{
4014 struct partial_symtab *subpst = allocate_psymtab (name, objfile);
4015
4016 subpst->section_offsets = pst->section_offsets;
4017 subpst->textlow = 0;
4018 subpst->texthigh = 0;
4019
4020 subpst->dependencies = (struct partial_symtab **)
4021 obstack_alloc (&objfile->objfile_obstack,
4022 sizeof (struct partial_symtab *));
4023 subpst->dependencies[0] = pst;
4024 subpst->number_of_dependencies = 1;
4025
4026 subpst->globals_offset = 0;
4027 subpst->n_global_syms = 0;
4028 subpst->statics_offset = 0;
4029 subpst->n_static_syms = 0;
4030 subpst->symtab = NULL;
4031 subpst->read_symtab = pst->read_symtab;
4032 subpst->readin = 0;
4033
4034 /* No private part is necessary for include psymtabs. This property
4035 can be used to differentiate between such include psymtabs and
10b3939b 4036 the regular ones. */
58a9656e 4037 subpst->read_symtab_private = NULL;
aaa75496
JB
4038}
4039
4040/* Read the Line Number Program data and extract the list of files
4041 included by the source file represented by PST. Build an include
d85a05f0 4042 partial symtab for each of these included files. */
aaa75496
JB
4043
4044static void
4045dwarf2_build_include_psymtabs (struct dwarf2_cu *cu,
dee91e82
DE
4046 struct die_info *die,
4047 struct partial_symtab *pst)
aaa75496 4048{
d85a05f0
DJ
4049 struct line_header *lh = NULL;
4050 struct attribute *attr;
aaa75496 4051
d85a05f0
DJ
4052 attr = dwarf2_attr (die, DW_AT_stmt_list, cu);
4053 if (attr)
3019eac3 4054 lh = dwarf_decode_line_header (DW_UNSND (attr), cu);
aaa75496
JB
4055 if (lh == NULL)
4056 return; /* No linetable, so no includes. */
4057
c6da4cef 4058 /* NOTE: pst->dirname is DW_AT_comp_dir (if present). */
f3f5162e 4059 dwarf_decode_lines (lh, pst->dirname, cu, pst, 1);
aaa75496
JB
4060
4061 free_line_header (lh);
4062}
4063
348e048f 4064static hashval_t
52dc124a 4065hash_signatured_type (const void *item)
348e048f 4066{
52dc124a 4067 const struct signatured_type *sig_type = item;
9a619af0 4068
348e048f 4069 /* This drops the top 32 bits of the signature, but is ok for a hash. */
52dc124a 4070 return sig_type->signature;
348e048f
DE
4071}
4072
4073static int
52dc124a 4074eq_signatured_type (const void *item_lhs, const void *item_rhs)
348e048f
DE
4075{
4076 const struct signatured_type *lhs = item_lhs;
4077 const struct signatured_type *rhs = item_rhs;
9a619af0 4078
348e048f
DE
4079 return lhs->signature == rhs->signature;
4080}
4081
1fd400ff
TT
4082/* Allocate a hash table for signatured types. */
4083
4084static htab_t
673bfd45 4085allocate_signatured_type_table (struct objfile *objfile)
1fd400ff
TT
4086{
4087 return htab_create_alloc_ex (41,
52dc124a
DE
4088 hash_signatured_type,
4089 eq_signatured_type,
1fd400ff
TT
4090 NULL,
4091 &objfile->objfile_obstack,
4092 hashtab_obstack_allocate,
4093 dummy_obstack_deallocate);
4094}
4095
d467dd73 4096/* A helper function to add a signatured type CU to a table. */
1fd400ff
TT
4097
4098static int
d467dd73 4099add_signatured_type_cu_to_table (void **slot, void *datum)
1fd400ff
TT
4100{
4101 struct signatured_type *sigt = *slot;
b4dd5633 4102 struct signatured_type ***datap = datum;
1fd400ff 4103
b4dd5633 4104 **datap = sigt;
1fd400ff
TT
4105 ++*datap;
4106
4107 return 1;
4108}
4109
3019eac3 4110/* Create the hash table of all entries in the .debug_types section.
80626a55
DE
4111 DWO_FILE is a pointer to the DWO file for .debug_types.dwo,
4112 NULL otherwise.
4113 Note: This function processes DWO files only, not DWP files.
3019eac3
DE
4114 The result is a pointer to the hash table or NULL if there are
4115 no types. */
348e048f 4116
3019eac3
DE
4117static htab_t
4118create_debug_types_hash_table (struct dwo_file *dwo_file,
4119 VEC (dwarf2_section_info_def) *types)
348e048f 4120{
3019eac3 4121 struct objfile *objfile = dwarf2_per_objfile->objfile;
8b70b953 4122 htab_t types_htab = NULL;
8b70b953
TT
4123 int ix;
4124 struct dwarf2_section_info *section;
4bdcc0c1 4125 struct dwarf2_section_info *abbrev_section;
348e048f 4126
3019eac3
DE
4127 if (VEC_empty (dwarf2_section_info_def, types))
4128 return NULL;
348e048f 4129
4bdcc0c1
DE
4130 abbrev_section = (dwo_file != NULL
4131 ? &dwo_file->sections.abbrev
4132 : &dwarf2_per_objfile->abbrev);
4133
09406207
DE
4134 if (dwarf2_read_debug)
4135 fprintf_unfiltered (gdb_stdlog, "Reading .debug_types%s for %s:\n",
4136 dwo_file ? ".dwo" : "",
4137 bfd_get_filename (abbrev_section->asection->owner));
4138
8b70b953 4139 for (ix = 0;
3019eac3 4140 VEC_iterate (dwarf2_section_info_def, types, ix, section);
8b70b953
TT
4141 ++ix)
4142 {
3019eac3 4143 bfd *abfd;
8b70b953 4144 gdb_byte *info_ptr, *end_ptr;
36586728 4145 struct dwarf2_section_info *abbrev_section;
348e048f 4146
8b70b953
TT
4147 dwarf2_read_section (objfile, section);
4148 info_ptr = section->buffer;
348e048f 4149
8b70b953
TT
4150 if (info_ptr == NULL)
4151 continue;
348e048f 4152
3019eac3
DE
4153 /* We can't set abfd until now because the section may be empty or
4154 not present, in which case section->asection will be NULL. */
4155 abfd = section->asection->owner;
4156
36586728
TT
4157 if (dwo_file)
4158 abbrev_section = &dwo_file->sections.abbrev;
4159 else
4160 abbrev_section = &dwarf2_per_objfile->abbrev;
4161
8b70b953 4162 if (types_htab == NULL)
3019eac3
DE
4163 {
4164 if (dwo_file)
4165 types_htab = allocate_dwo_unit_table (objfile);
4166 else
4167 types_htab = allocate_signatured_type_table (objfile);
4168 }
348e048f 4169
dee91e82
DE
4170 /* We don't use init_cutu_and_read_dies_simple, or some such, here
4171 because we don't need to read any dies: the signature is in the
4172 header. */
8b70b953
TT
4173
4174 end_ptr = info_ptr + section->size;
4175 while (info_ptr < end_ptr)
4176 {
b64f50a1 4177 sect_offset offset;
3019eac3 4178 cu_offset type_offset_in_tu;
8b70b953 4179 ULONGEST signature;
52dc124a 4180 struct signatured_type *sig_type;
3019eac3 4181 struct dwo_unit *dwo_tu;
8b70b953
TT
4182 void **slot;
4183 gdb_byte *ptr = info_ptr;
9ff913ba 4184 struct comp_unit_head header;
dee91e82 4185 unsigned int length;
348e048f 4186
b64f50a1 4187 offset.sect_off = ptr - section->buffer;
348e048f 4188
8b70b953 4189 /* We need to read the type's signature in order to build the hash
9ff913ba 4190 table, but we don't need anything else just yet. */
348e048f 4191
4bdcc0c1
DE
4192 ptr = read_and_check_type_unit_head (&header, section,
4193 abbrev_section, ptr,
3019eac3 4194 &signature, &type_offset_in_tu);
6caca83c 4195
1ce1cefd 4196 length = get_cu_length (&header);
dee91e82 4197
6caca83c 4198 /* Skip dummy type units. */
dee91e82
DE
4199 if (ptr >= info_ptr + length
4200 || peek_abbrev_code (abfd, ptr) == 0)
6caca83c 4201 {
1ce1cefd 4202 info_ptr += length;
6caca83c
CC
4203 continue;
4204 }
8b70b953 4205
3019eac3
DE
4206 if (dwo_file)
4207 {
4208 sig_type = NULL;
4209 dwo_tu = OBSTACK_ZALLOC (&objfile->objfile_obstack,
4210 struct dwo_unit);
4211 dwo_tu->dwo_file = dwo_file;
4212 dwo_tu->signature = signature;
4213 dwo_tu->type_offset_in_tu = type_offset_in_tu;
4214 dwo_tu->info_or_types_section = section;
4215 dwo_tu->offset = offset;
4216 dwo_tu->length = length;
4217 }
4218 else
4219 {
4220 /* N.B.: type_offset is not usable if this type uses a DWO file.
4221 The real type_offset is in the DWO file. */
4222 dwo_tu = NULL;
4223 sig_type = OBSTACK_ZALLOC (&objfile->objfile_obstack,
4224 struct signatured_type);
4225 sig_type->signature = signature;
4226 sig_type->type_offset_in_tu = type_offset_in_tu;
4227 sig_type->per_cu.objfile = objfile;
4228 sig_type->per_cu.is_debug_types = 1;
4229 sig_type->per_cu.info_or_types_section = section;
4230 sig_type->per_cu.offset = offset;
4231 sig_type->per_cu.length = length;
4232 }
8b70b953 4233
3019eac3
DE
4234 slot = htab_find_slot (types_htab,
4235 dwo_file ? (void*) dwo_tu : (void *) sig_type,
4236 INSERT);
8b70b953
TT
4237 gdb_assert (slot != NULL);
4238 if (*slot != NULL)
4239 {
3019eac3
DE
4240 sect_offset dup_offset;
4241
4242 if (dwo_file)
4243 {
4244 const struct dwo_unit *dup_tu = *slot;
4245
4246 dup_offset = dup_tu->offset;
4247 }
4248 else
4249 {
4250 const struct signatured_type *dup_tu = *slot;
4251
4252 dup_offset = dup_tu->per_cu.offset;
4253 }
b3c8eb43 4254
8b70b953
TT
4255 complaint (&symfile_complaints,
4256 _("debug type entry at offset 0x%x is duplicate to the "
4257 "entry at offset 0x%x, signature 0x%s"),
3019eac3 4258 offset.sect_off, dup_offset.sect_off,
8b70b953 4259 phex (signature, sizeof (signature)));
8b70b953 4260 }
3019eac3 4261 *slot = dwo_file ? (void *) dwo_tu : (void *) sig_type;
348e048f 4262
09406207 4263 if (dwarf2_read_debug)
8b70b953 4264 fprintf_unfiltered (gdb_stdlog, " offset 0x%x, signature 0x%s\n",
b64f50a1
JK
4265 offset.sect_off,
4266 phex (signature, sizeof (signature)));
348e048f 4267
dee91e82 4268 info_ptr += length;
8b70b953 4269 }
348e048f
DE
4270 }
4271
3019eac3
DE
4272 return types_htab;
4273}
4274
4275/* Create the hash table of all entries in the .debug_types section,
4276 and initialize all_type_units.
4277 The result is zero if there is an error (e.g. missing .debug_types section),
4278 otherwise non-zero. */
4279
4280static int
4281create_all_type_units (struct objfile *objfile)
4282{
4283 htab_t types_htab;
b4dd5633 4284 struct signatured_type **iter;
3019eac3
DE
4285
4286 types_htab = create_debug_types_hash_table (NULL, dwarf2_per_objfile->types);
4287 if (types_htab == NULL)
4288 {
4289 dwarf2_per_objfile->signatured_types = NULL;
4290 return 0;
4291 }
4292
348e048f
DE
4293 dwarf2_per_objfile->signatured_types = types_htab;
4294
d467dd73
DE
4295 dwarf2_per_objfile->n_type_units = htab_elements (types_htab);
4296 dwarf2_per_objfile->all_type_units
1fd400ff 4297 = obstack_alloc (&objfile->objfile_obstack,
d467dd73 4298 dwarf2_per_objfile->n_type_units
b4dd5633 4299 * sizeof (struct signatured_type *));
d467dd73
DE
4300 iter = &dwarf2_per_objfile->all_type_units[0];
4301 htab_traverse_noresize (types_htab, add_signatured_type_cu_to_table, &iter);
4302 gdb_assert (iter - &dwarf2_per_objfile->all_type_units[0]
4303 == dwarf2_per_objfile->n_type_units);
1fd400ff 4304
348e048f
DE
4305 return 1;
4306}
4307
380bca97 4308/* Lookup a signature based type for DW_FORM_ref_sig8.
e319fa28 4309 Returns NULL if signature SIG is not present in the table. */
348e048f
DE
4310
4311static struct signatured_type *
e319fa28 4312lookup_signatured_type (ULONGEST sig)
348e048f
DE
4313{
4314 struct signatured_type find_entry, *entry;
4315
4316 if (dwarf2_per_objfile->signatured_types == NULL)
4317 {
4318 complaint (&symfile_complaints,
55f1336d 4319 _("missing `.debug_types' section for DW_FORM_ref_sig8 die"));
dcc07052 4320 return NULL;
348e048f
DE
4321 }
4322
4323 find_entry.signature = sig;
4324 entry = htab_find (dwarf2_per_objfile->signatured_types, &find_entry);
4325 return entry;
4326}
42e7ad6c
DE
4327\f
4328/* Low level DIE reading support. */
348e048f 4329
d85a05f0
DJ
4330/* Initialize a die_reader_specs struct from a dwarf2_cu struct. */
4331
4332static void
4333init_cu_die_reader (struct die_reader_specs *reader,
dee91e82 4334 struct dwarf2_cu *cu,
3019eac3
DE
4335 struct dwarf2_section_info *section,
4336 struct dwo_file *dwo_file)
d85a05f0 4337{
fceca515 4338 gdb_assert (section->readin && section->buffer != NULL);
dee91e82 4339 reader->abfd = section->asection->owner;
d85a05f0 4340 reader->cu = cu;
3019eac3 4341 reader->dwo_file = dwo_file;
dee91e82
DE
4342 reader->die_section = section;
4343 reader->buffer = section->buffer;
f664829e 4344 reader->buffer_end = section->buffer + section->size;
d85a05f0
DJ
4345}
4346
fd820528 4347/* Initialize a CU (or TU) and read its DIEs.
3019eac3 4348 If the CU defers to a DWO file, read the DWO file as well.
dee91e82 4349
f4dc4d17
DE
4350 ABBREV_TABLE, if non-NULL, is the abbreviation table to use.
4351 Otherwise the table specified in the comp unit header is read in and used.
4352 This is an optimization for when we already have the abbrev table.
4353
dee91e82
DE
4354 If USE_EXISTING_CU is non-zero, and THIS_CU->cu is non-NULL, then use it.
4355 Otherwise, a new CU is allocated with xmalloc.
4356
4357 If KEEP is non-zero, then if we allocated a dwarf2_cu we add it to
4358 read_in_chain. Otherwise the dwarf2_cu data is freed at the end.
4359
4360 WARNING: If THIS_CU is a "dummy CU" (used as filler by the incremental
fd820528 4361 linker) then DIE_READER_FUNC will not get called. */
aaa75496 4362
70221824 4363static void
fd820528 4364init_cutu_and_read_dies (struct dwarf2_per_cu_data *this_cu,
f4dc4d17 4365 struct abbrev_table *abbrev_table,
fd820528
DE
4366 int use_existing_cu, int keep,
4367 die_reader_func_ftype *die_reader_func,
4368 void *data)
c906108c 4369{
dee91e82 4370 struct objfile *objfile = dwarf2_per_objfile->objfile;
3019eac3
DE
4371 struct dwarf2_section_info *section = this_cu->info_or_types_section;
4372 bfd *abfd = section->asection->owner;
dee91e82
DE
4373 struct dwarf2_cu *cu;
4374 gdb_byte *begin_info_ptr, *info_ptr;
4375 struct die_reader_specs reader;
d85a05f0 4376 struct die_info *comp_unit_die;
dee91e82 4377 int has_children;
d85a05f0 4378 struct attribute *attr;
dee91e82
DE
4379 struct cleanup *cleanups, *free_cu_cleanup = NULL;
4380 struct signatured_type *sig_type = NULL;
4bdcc0c1 4381 struct dwarf2_section_info *abbrev_section;
42e7ad6c
DE
4382 /* Non-zero if CU currently points to a DWO file and we need to
4383 reread it. When this happens we need to reread the skeleton die
4384 before we can reread the DWO file. */
4385 int rereading_dwo_cu = 0;
c906108c 4386
09406207
DE
4387 if (dwarf2_die_debug)
4388 fprintf_unfiltered (gdb_stdlog, "Reading %s unit at offset 0x%x\n",
4389 this_cu->is_debug_types ? "type" : "comp",
4390 this_cu->offset.sect_off);
4391
dee91e82
DE
4392 if (use_existing_cu)
4393 gdb_assert (keep);
23745b47 4394
dee91e82
DE
4395 cleanups = make_cleanup (null_cleanup, NULL);
4396
4397 /* This is cheap if the section is already read in. */
4398 dwarf2_read_section (objfile, section);
4399
4400 begin_info_ptr = info_ptr = section->buffer + this_cu->offset.sect_off;
36586728
TT
4401
4402 abbrev_section = get_abbrev_section_for_cu (this_cu);
dee91e82
DE
4403
4404 if (use_existing_cu && this_cu->cu != NULL)
4405 {
4406 cu = this_cu->cu;
42e7ad6c
DE
4407
4408 /* If this CU is from a DWO file we need to start over, we need to
4409 refetch the attributes from the skeleton CU.
4410 This could be optimized by retrieving those attributes from when we
4411 were here the first time: the previous comp_unit_die was stored in
4412 comp_unit_obstack. But there's no data yet that we need this
4413 optimization. */
4414 if (cu->dwo_unit != NULL)
4415 rereading_dwo_cu = 1;
dee91e82
DE
4416 }
4417 else
4418 {
4419 /* If !use_existing_cu, this_cu->cu must be NULL. */
4420 gdb_assert (this_cu->cu == NULL);
4421
4422 cu = xmalloc (sizeof (*cu));
4423 init_one_comp_unit (cu, this_cu);
4424
4425 /* If an error occurs while loading, release our storage. */
4426 free_cu_cleanup = make_cleanup (free_heap_comp_unit, cu);
42e7ad6c 4427 }
dee91e82 4428
42e7ad6c
DE
4429 if (cu->header.first_die_offset.cu_off != 0 && ! rereading_dwo_cu)
4430 {
4431 /* We already have the header, there's no need to read it in again. */
4432 info_ptr += cu->header.first_die_offset.cu_off;
4433 }
4434 else
4435 {
3019eac3 4436 if (this_cu->is_debug_types)
dee91e82
DE
4437 {
4438 ULONGEST signature;
42e7ad6c 4439 cu_offset type_offset_in_tu;
dee91e82 4440
4bdcc0c1
DE
4441 info_ptr = read_and_check_type_unit_head (&cu->header, section,
4442 abbrev_section, info_ptr,
42e7ad6c
DE
4443 &signature,
4444 &type_offset_in_tu);
dee91e82 4445
42e7ad6c
DE
4446 /* Since per_cu is the first member of struct signatured_type,
4447 we can go from a pointer to one to a pointer to the other. */
4448 sig_type = (struct signatured_type *) this_cu;
4449 gdb_assert (sig_type->signature == signature);
4450 gdb_assert (sig_type->type_offset_in_tu.cu_off
4451 == type_offset_in_tu.cu_off);
dee91e82
DE
4452 gdb_assert (this_cu->offset.sect_off == cu->header.offset.sect_off);
4453
42e7ad6c
DE
4454 /* LENGTH has not been set yet for type units if we're
4455 using .gdb_index. */
1ce1cefd 4456 this_cu->length = get_cu_length (&cu->header);
3019eac3
DE
4457
4458 /* Establish the type offset that can be used to lookup the type. */
4459 sig_type->type_offset_in_section.sect_off =
4460 this_cu->offset.sect_off + sig_type->type_offset_in_tu.cu_off;
dee91e82
DE
4461 }
4462 else
4463 {
4bdcc0c1
DE
4464 info_ptr = read_and_check_comp_unit_head (&cu->header, section,
4465 abbrev_section,
4466 info_ptr, 0);
dee91e82
DE
4467
4468 gdb_assert (this_cu->offset.sect_off == cu->header.offset.sect_off);
1ce1cefd 4469 gdb_assert (this_cu->length == get_cu_length (&cu->header));
dee91e82
DE
4470 }
4471 }
10b3939b 4472
6caca83c 4473 /* Skip dummy compilation units. */
dee91e82 4474 if (info_ptr >= begin_info_ptr + this_cu->length
6caca83c
CC
4475 || peek_abbrev_code (abfd, info_ptr) == 0)
4476 {
dee91e82 4477 do_cleanups (cleanups);
21b2bd31 4478 return;
6caca83c
CC
4479 }
4480
433df2d4
DE
4481 /* If we don't have them yet, read the abbrevs for this compilation unit.
4482 And if we need to read them now, make sure they're freed when we're
42e7ad6c
DE
4483 done. Note that it's important that if the CU had an abbrev table
4484 on entry we don't free it when we're done: Somewhere up the call stack
4485 it may be in use. */
f4dc4d17
DE
4486 if (abbrev_table != NULL)
4487 {
4488 gdb_assert (cu->abbrev_table == NULL);
4489 gdb_assert (cu->header.abbrev_offset.sect_off
4490 == abbrev_table->offset.sect_off);
4491 cu->abbrev_table = abbrev_table;
4492 }
4493 else if (cu->abbrev_table == NULL)
dee91e82 4494 {
4bdcc0c1 4495 dwarf2_read_abbrevs (cu, abbrev_section);
dee91e82
DE
4496 make_cleanup (dwarf2_free_abbrev_table, cu);
4497 }
42e7ad6c
DE
4498 else if (rereading_dwo_cu)
4499 {
4500 dwarf2_free_abbrev_table (cu);
4501 dwarf2_read_abbrevs (cu, abbrev_section);
4502 }
af703f96 4503
dee91e82 4504 /* Read the top level CU/TU die. */
3019eac3 4505 init_cu_die_reader (&reader, cu, section, NULL);
dee91e82 4506 info_ptr = read_full_die (&reader, &comp_unit_die, info_ptr, &has_children);
93311388 4507
3019eac3
DE
4508 /* If we have a DWO stub, process it and then read in the DWO file.
4509 Note that if USE_EXISTING_OK != 0, and THIS_CU->cu already contains
4510 a DWO CU, that this test will fail. */
4511 attr = dwarf2_attr (comp_unit_die, DW_AT_GNU_dwo_name, cu);
4512 if (attr)
4513 {
4514 char *dwo_name = DW_STRING (attr);
42e7ad6c 4515 const char *comp_dir_string;
3019eac3
DE
4516 struct dwo_unit *dwo_unit;
4517 ULONGEST signature; /* Or dwo_id. */
42e7ad6c 4518 struct attribute *comp_dir, *stmt_list, *low_pc, *high_pc, *ranges;
3019eac3 4519 int i,num_extra_attrs;
4bdcc0c1 4520 struct dwarf2_section_info *dwo_abbrev_section;
3019eac3
DE
4521
4522 if (has_children)
4523 error (_("Dwarf Error: compilation unit with DW_AT_GNU_dwo_name"
4524 " has children (offset 0x%x) [in module %s]"),
4525 this_cu->offset.sect_off, bfd_get_filename (abfd));
4526
4527 /* These attributes aren't processed until later:
4528 DW_AT_stmt_list, DW_AT_low_pc, DW_AT_high_pc, DW_AT_ranges.
4529 However, the attribute is found in the stub which we won't have later.
4530 In order to not impose this complication on the rest of the code,
4531 we read them here and copy them to the DWO CU/TU die. */
3019eac3
DE
4532
4533 /* For TUs in DWO files, the DW_AT_stmt_list attribute lives in the
4534 DWO file. */
42e7ad6c 4535 stmt_list = NULL;
3019eac3
DE
4536 if (! this_cu->is_debug_types)
4537 stmt_list = dwarf2_attr (comp_unit_die, DW_AT_stmt_list, cu);
4538 low_pc = dwarf2_attr (comp_unit_die, DW_AT_low_pc, cu);
4539 high_pc = dwarf2_attr (comp_unit_die, DW_AT_high_pc, cu);
4540 ranges = dwarf2_attr (comp_unit_die, DW_AT_ranges, cu);
42e7ad6c 4541 comp_dir = dwarf2_attr (comp_unit_die, DW_AT_comp_dir, cu);
3019eac3
DE
4542
4543 /* There should be a DW_AT_addr_base attribute here (if needed).
4544 We need the value before we can process DW_FORM_GNU_addr_index. */
4545 cu->addr_base = 0;
3019eac3
DE
4546 attr = dwarf2_attr (comp_unit_die, DW_AT_GNU_addr_base, cu);
4547 if (attr)
2e3cf129
DE
4548 cu->addr_base = DW_UNSND (attr);
4549
4550 /* There should be a DW_AT_ranges_base attribute here (if needed).
4551 We need the value before we can process DW_AT_ranges. */
4552 cu->ranges_base = 0;
4553 attr = dwarf2_attr (comp_unit_die, DW_AT_GNU_ranges_base, cu);
4554 if (attr)
4555 cu->ranges_base = DW_UNSND (attr);
3019eac3
DE
4556
4557 if (this_cu->is_debug_types)
4558 {
4559 gdb_assert (sig_type != NULL);
4560 signature = sig_type->signature;
4561 }
4562 else
4563 {
4564 attr = dwarf2_attr (comp_unit_die, DW_AT_GNU_dwo_id, cu);
4565 if (! attr)
4566 error (_("Dwarf Error: missing dwo_id [in module %s]"),
4567 dwo_name);
4568 signature = DW_UNSND (attr);
4569 }
4570
4571 /* We may need the comp_dir in order to find the DWO file. */
42e7ad6c
DE
4572 comp_dir_string = NULL;
4573 if (comp_dir)
4574 comp_dir_string = DW_STRING (comp_dir);
3019eac3
DE
4575
4576 if (this_cu->is_debug_types)
42e7ad6c 4577 dwo_unit = lookup_dwo_type_unit (sig_type, dwo_name, comp_dir_string);
3019eac3 4578 else
42e7ad6c 4579 dwo_unit = lookup_dwo_comp_unit (this_cu, dwo_name, comp_dir_string,
3019eac3
DE
4580 signature);
4581
4582 if (dwo_unit == NULL)
4583 {
4584 error (_("Dwarf Error: CU at offset 0x%x references unknown DWO"
4585 " with ID %s [in module %s]"),
4586 this_cu->offset.sect_off,
4587 phex (signature, sizeof (signature)),
4588 objfile->name);
4589 }
4590
4591 /* Set up for reading the DWO CU/TU. */
4592 cu->dwo_unit = dwo_unit;
4593 section = dwo_unit->info_or_types_section;
80626a55 4594 dwarf2_read_section (objfile, section);
3019eac3 4595 begin_info_ptr = info_ptr = section->buffer + dwo_unit->offset.sect_off;
4bdcc0c1 4596 dwo_abbrev_section = &dwo_unit->dwo_file->sections.abbrev;
3019eac3
DE
4597 init_cu_die_reader (&reader, cu, section, dwo_unit->dwo_file);
4598
4599 if (this_cu->is_debug_types)
4600 {
4601 ULONGEST signature;
80626a55 4602 cu_offset type_offset_in_tu;
3019eac3 4603
4bdcc0c1
DE
4604 info_ptr = read_and_check_type_unit_head (&cu->header, section,
4605 dwo_abbrev_section,
4606 info_ptr,
80626a55
DE
4607 &signature,
4608 &type_offset_in_tu);
3019eac3
DE
4609 gdb_assert (sig_type->signature == signature);
4610 gdb_assert (dwo_unit->offset.sect_off == cu->header.offset.sect_off);
80626a55
DE
4611 /* For DWOs coming from DWP files, we don't know the CU length
4612 nor the type's offset in the TU until now. */
4613 dwo_unit->length = get_cu_length (&cu->header);
4614 dwo_unit->type_offset_in_tu = type_offset_in_tu;
3019eac3
DE
4615
4616 /* Establish the type offset that can be used to lookup the type.
4617 For DWO files, we don't know it until now. */
4618 sig_type->type_offset_in_section.sect_off =
4619 dwo_unit->offset.sect_off + dwo_unit->type_offset_in_tu.cu_off;
4620 }
4621 else
4622 {
4bdcc0c1
DE
4623 info_ptr = read_and_check_comp_unit_head (&cu->header, section,
4624 dwo_abbrev_section,
4625 info_ptr, 0);
3019eac3 4626 gdb_assert (dwo_unit->offset.sect_off == cu->header.offset.sect_off);
80626a55
DE
4627 /* For DWOs coming from DWP files, we don't know the CU length
4628 until now. */
4629 dwo_unit->length = get_cu_length (&cu->header);
3019eac3
DE
4630 }
4631
4632 /* Discard the original CU's abbrev table, and read the DWO's. */
f4dc4d17
DE
4633 if (abbrev_table == NULL)
4634 {
4635 dwarf2_free_abbrev_table (cu);
4636 dwarf2_read_abbrevs (cu, dwo_abbrev_section);
4637 }
4638 else
4639 {
4640 dwarf2_read_abbrevs (cu, dwo_abbrev_section);
4641 make_cleanup (dwarf2_free_abbrev_table, cu);
4642 }
3019eac3
DE
4643
4644 /* Read in the die, but leave space to copy over the attributes
4645 from the stub. This has the benefit of simplifying the rest of
4646 the code - all the real work is done here. */
4647 num_extra_attrs = ((stmt_list != NULL)
4648 + (low_pc != NULL)
4649 + (high_pc != NULL)
42e7ad6c
DE
4650 + (ranges != NULL)
4651 + (comp_dir != NULL));
3019eac3
DE
4652 info_ptr = read_full_die_1 (&reader, &comp_unit_die, info_ptr,
4653 &has_children, num_extra_attrs);
4654
4655 /* Copy over the attributes from the stub to the DWO die. */
4656 i = comp_unit_die->num_attrs;
4657 if (stmt_list != NULL)
4658 comp_unit_die->attrs[i++] = *stmt_list;
4659 if (low_pc != NULL)
4660 comp_unit_die->attrs[i++] = *low_pc;
4661 if (high_pc != NULL)
4662 comp_unit_die->attrs[i++] = *high_pc;
4663 if (ranges != NULL)
4664 comp_unit_die->attrs[i++] = *ranges;
42e7ad6c
DE
4665 if (comp_dir != NULL)
4666 comp_unit_die->attrs[i++] = *comp_dir;
3019eac3
DE
4667 comp_unit_die->num_attrs += num_extra_attrs;
4668
4669 /* Skip dummy compilation units. */
4670 if (info_ptr >= begin_info_ptr + dwo_unit->length
4671 || peek_abbrev_code (abfd, info_ptr) == 0)
4672 {
4673 do_cleanups (cleanups);
4674 return;
4675 }
4676 }
4677
dee91e82
DE
4678 die_reader_func (&reader, info_ptr, comp_unit_die, has_children, data);
4679
4680 if (free_cu_cleanup != NULL)
348e048f 4681 {
dee91e82
DE
4682 if (keep)
4683 {
4684 /* We've successfully allocated this compilation unit. Let our
4685 caller clean it up when finished with it. */
4686 discard_cleanups (free_cu_cleanup);
4687
4688 /* We can only discard free_cu_cleanup and all subsequent cleanups.
4689 So we have to manually free the abbrev table. */
4690 dwarf2_free_abbrev_table (cu);
4691
4692 /* Link this CU into read_in_chain. */
4693 this_cu->cu->read_in_chain = dwarf2_per_objfile->read_in_chain;
4694 dwarf2_per_objfile->read_in_chain = this_cu;
4695 }
4696 else
4697 do_cleanups (free_cu_cleanup);
348e048f 4698 }
dee91e82
DE
4699
4700 do_cleanups (cleanups);
4701}
4702
3019eac3
DE
4703/* Read CU/TU THIS_CU in section SECTION,
4704 but do not follow DW_AT_GNU_dwo_name if present.
80626a55
DE
4705 DWOP_FILE, if non-NULL, is the DWO/DWP file to read (the caller is assumed
4706 to have already done the lookup to find the DWO/DWP file).
dee91e82
DE
4707
4708 The caller is required to fill in THIS_CU->section, THIS_CU->offset, and
3019eac3 4709 THIS_CU->is_debug_types, but nothing else.
dee91e82
DE
4710
4711 We fill in THIS_CU->length.
4712
4713 WARNING: If THIS_CU is a "dummy CU" (used as filler by the incremental
4714 linker) then DIE_READER_FUNC will not get called.
4715
4716 THIS_CU->cu is always freed when done.
3019eac3
DE
4717 This is done in order to not leave THIS_CU->cu in a state where we have
4718 to care whether it refers to the "main" CU or the DWO CU. */
dee91e82
DE
4719
4720static void
4721init_cutu_and_read_dies_no_follow (struct dwarf2_per_cu_data *this_cu,
4722 struct dwarf2_section_info *abbrev_section,
3019eac3 4723 struct dwo_file *dwo_file,
dee91e82
DE
4724 die_reader_func_ftype *die_reader_func,
4725 void *data)
4726{
4727 struct objfile *objfile = dwarf2_per_objfile->objfile;
3019eac3
DE
4728 struct dwarf2_section_info *section = this_cu->info_or_types_section;
4729 bfd *abfd = section->asection->owner;
dee91e82
DE
4730 struct dwarf2_cu cu;
4731 gdb_byte *begin_info_ptr, *info_ptr;
4732 struct die_reader_specs reader;
4733 struct cleanup *cleanups;
4734 struct die_info *comp_unit_die;
4735 int has_children;
4736
09406207
DE
4737 if (dwarf2_die_debug)
4738 fprintf_unfiltered (gdb_stdlog, "Reading %s unit at offset 0x%x\n",
4739 this_cu->is_debug_types ? "type" : "comp",
4740 this_cu->offset.sect_off);
4741
dee91e82
DE
4742 gdb_assert (this_cu->cu == NULL);
4743
dee91e82
DE
4744 /* This is cheap if the section is already read in. */
4745 dwarf2_read_section (objfile, section);
4746
4747 init_one_comp_unit (&cu, this_cu);
4748
4749 cleanups = make_cleanup (free_stack_comp_unit, &cu);
4750
4751 begin_info_ptr = info_ptr = section->buffer + this_cu->offset.sect_off;
4bdcc0c1
DE
4752 info_ptr = read_and_check_comp_unit_head (&cu.header, section,
4753 abbrev_section, info_ptr,
3019eac3 4754 this_cu->is_debug_types);
dee91e82 4755
1ce1cefd 4756 this_cu->length = get_cu_length (&cu.header);
dee91e82
DE
4757
4758 /* Skip dummy compilation units. */
4759 if (info_ptr >= begin_info_ptr + this_cu->length
4760 || peek_abbrev_code (abfd, info_ptr) == 0)
c906108c 4761 {
dee91e82 4762 do_cleanups (cleanups);
21b2bd31 4763 return;
93311388 4764 }
72bf9492 4765
dee91e82
DE
4766 dwarf2_read_abbrevs (&cu, abbrev_section);
4767 make_cleanup (dwarf2_free_abbrev_table, &cu);
4768
3019eac3 4769 init_cu_die_reader (&reader, &cu, section, dwo_file);
dee91e82
DE
4770 info_ptr = read_full_die (&reader, &comp_unit_die, info_ptr, &has_children);
4771
4772 die_reader_func (&reader, info_ptr, comp_unit_die, has_children, data);
4773
4774 do_cleanups (cleanups);
4775}
4776
3019eac3
DE
4777/* Read a CU/TU, except that this does not look for DW_AT_GNU_dwo_name and
4778 does not lookup the specified DWO file.
4779 This cannot be used to read DWO files.
dee91e82
DE
4780
4781 THIS_CU->cu is always freed when done.
3019eac3
DE
4782 This is done in order to not leave THIS_CU->cu in a state where we have
4783 to care whether it refers to the "main" CU or the DWO CU.
4784 We can revisit this if the data shows there's a performance issue. */
dee91e82
DE
4785
4786static void
4787init_cutu_and_read_dies_simple (struct dwarf2_per_cu_data *this_cu,
4788 die_reader_func_ftype *die_reader_func,
4789 void *data)
4790{
4791 init_cutu_and_read_dies_no_follow (this_cu,
36586728 4792 get_abbrev_section_for_cu (this_cu),
3019eac3 4793 NULL,
dee91e82
DE
4794 die_reader_func, data);
4795}
4796
f4dc4d17
DE
4797/* Create a psymtab named NAME and assign it to PER_CU.
4798
4799 The caller must fill in the following details:
4800 dirname, textlow, texthigh. */
4801
4802static struct partial_symtab *
4803create_partial_symtab (struct dwarf2_per_cu_data *per_cu, const char *name)
4804{
4805 struct objfile *objfile = per_cu->objfile;
4806 struct partial_symtab *pst;
4807
4808 pst = start_psymtab_common (objfile, objfile->section_offsets,
4809 name, 0,
4810 objfile->global_psymbols.next,
4811 objfile->static_psymbols.next);
4812
4813 pst->psymtabs_addrmap_supported = 1;
4814
4815 /* This is the glue that links PST into GDB's symbol API. */
4816 pst->read_symtab_private = per_cu;
4817 pst->read_symtab = dwarf2_psymtab_to_symtab;
4818 per_cu->v.psymtab = pst;
4819
4820 return pst;
4821}
4822
dee91e82
DE
4823/* die_reader_func for process_psymtab_comp_unit. */
4824
4825static void
4826process_psymtab_comp_unit_reader (const struct die_reader_specs *reader,
4827 gdb_byte *info_ptr,
4828 struct die_info *comp_unit_die,
4829 int has_children,
4830 void *data)
4831{
4832 struct dwarf2_cu *cu = reader->cu;
4833 struct objfile *objfile = cu->objfile;
4834 struct dwarf2_per_cu_data *per_cu = cu->per_cu;
dee91e82
DE
4835 struct attribute *attr;
4836 CORE_ADDR baseaddr;
4837 CORE_ADDR best_lowpc = 0, best_highpc = 0;
4838 struct partial_symtab *pst;
4839 int has_pc_info;
4840 const char *filename;
95554aad 4841 int *want_partial_unit_ptr = data;
dee91e82 4842
95554aad
TT
4843 if (comp_unit_die->tag == DW_TAG_partial_unit
4844 && (want_partial_unit_ptr == NULL
4845 || !*want_partial_unit_ptr))
dee91e82
DE
4846 return;
4847
f4dc4d17
DE
4848 gdb_assert (! per_cu->is_debug_types);
4849
95554aad 4850 prepare_one_comp_unit (cu, comp_unit_die, language_minimal);
dee91e82
DE
4851
4852 cu->list_in_scope = &file_symbols;
c906108c 4853
93311388 4854 /* Allocate a new partial symbol table structure. */
dee91e82 4855 attr = dwarf2_attr (comp_unit_die, DW_AT_name, cu);
3e2a0cee
TT
4856 if (attr == NULL || !DW_STRING (attr))
4857 filename = "";
4858 else
4859 filename = DW_STRING (attr);
72bf9492 4860
f4dc4d17
DE
4861 pst = create_partial_symtab (per_cu, filename);
4862
4863 /* This must be done before calling dwarf2_build_include_psymtabs. */
dee91e82 4864 attr = dwarf2_attr (comp_unit_die, DW_AT_comp_dir, cu);
d85a05f0
DJ
4865 if (attr != NULL)
4866 pst->dirname = DW_STRING (attr);
72bf9492 4867
93311388 4868 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
e7c27a73 4869
dee91e82 4870 dwarf2_find_base_address (comp_unit_die, cu);
d85a05f0 4871
93311388
DE
4872 /* Possibly set the default values of LOWPC and HIGHPC from
4873 `DW_AT_ranges'. */
d85a05f0 4874 has_pc_info = dwarf2_get_pc_bounds (comp_unit_die, &best_lowpc,
dee91e82 4875 &best_highpc, cu, pst);
d85a05f0 4876 if (has_pc_info == 1 && best_lowpc < best_highpc)
93311388
DE
4877 /* Store the contiguous range if it is not empty; it can be empty for
4878 CUs with no code. */
4879 addrmap_set_empty (objfile->psymtabs_addrmap,
d85a05f0
DJ
4880 best_lowpc + baseaddr,
4881 best_highpc + baseaddr - 1, pst);
93311388
DE
4882
4883 /* Check if comp unit has_children.
4884 If so, read the rest of the partial symbols from this comp unit.
0963b4bd 4885 If not, there's no more debug_info for this comp unit. */
d85a05f0 4886 if (has_children)
93311388
DE
4887 {
4888 struct partial_die_info *first_die;
4889 CORE_ADDR lowpc, highpc;
31ffec48 4890
93311388
DE
4891 lowpc = ((CORE_ADDR) -1);
4892 highpc = ((CORE_ADDR) 0);
c906108c 4893
dee91e82 4894 first_die = load_partial_dies (reader, info_ptr, 1);
c906108c 4895
93311388 4896 scan_partial_symbols (first_die, &lowpc, &highpc,
dee91e82 4897 ! has_pc_info, cu);
57c22c6c 4898
93311388
DE
4899 /* If we didn't find a lowpc, set it to highpc to avoid
4900 complaints from `maint check'. */
4901 if (lowpc == ((CORE_ADDR) -1))
4902 lowpc = highpc;
10b3939b 4903
93311388
DE
4904 /* If the compilation unit didn't have an explicit address range,
4905 then use the information extracted from its child dies. */
d85a05f0 4906 if (! has_pc_info)
93311388 4907 {
d85a05f0
DJ
4908 best_lowpc = lowpc;
4909 best_highpc = highpc;
93311388
DE
4910 }
4911 }
d85a05f0
DJ
4912 pst->textlow = best_lowpc + baseaddr;
4913 pst->texthigh = best_highpc + baseaddr;
c906108c 4914
93311388
DE
4915 pst->n_global_syms = objfile->global_psymbols.next -
4916 (objfile->global_psymbols.list + pst->globals_offset);
4917 pst->n_static_syms = objfile->static_psymbols.next -
4918 (objfile->static_psymbols.list + pst->statics_offset);
4919 sort_pst_symbols (pst);
c906108c 4920
f4dc4d17 4921 if (!VEC_empty (dwarf2_per_cu_ptr, cu->per_cu->s.imported_symtabs))
95554aad
TT
4922 {
4923 int i;
f4dc4d17 4924 int len = VEC_length (dwarf2_per_cu_ptr, cu->per_cu->s.imported_symtabs);
95554aad
TT
4925 struct dwarf2_per_cu_data *iter;
4926
4927 /* Fill in 'dependencies' here; we fill in 'users' in a
4928 post-pass. */
4929 pst->number_of_dependencies = len;
4930 pst->dependencies = obstack_alloc (&objfile->objfile_obstack,
4931 len * sizeof (struct symtab *));
4932 for (i = 0;
f4dc4d17 4933 VEC_iterate (dwarf2_per_cu_ptr, cu->per_cu->s.imported_symtabs,
95554aad
TT
4934 i, iter);
4935 ++i)
4936 pst->dependencies[i] = iter->v.psymtab;
4937
f4dc4d17 4938 VEC_free (dwarf2_per_cu_ptr, cu->per_cu->s.imported_symtabs);
95554aad
TT
4939 }
4940
f4dc4d17
DE
4941 /* Get the list of files included in the current compilation unit,
4942 and build a psymtab for each of them. */
4943 dwarf2_build_include_psymtabs (cu, comp_unit_die, pst);
09406207
DE
4944
4945 if (dwarf2_read_debug)
4946 {
4947 struct gdbarch *gdbarch = get_objfile_arch (objfile);
4948
4949 fprintf_unfiltered (gdb_stdlog,
844226d6 4950 "Psymtab for %s unit @0x%x: %s - %s"
09406207
DE
4951 ", %d global, %d static syms\n",
4952 per_cu->is_debug_types ? "type" : "comp",
4953 per_cu->offset.sect_off,
4954 paddress (gdbarch, pst->textlow),
4955 paddress (gdbarch, pst->texthigh),
4956 pst->n_global_syms, pst->n_static_syms);
4957 }
dee91e82 4958}
ae038cb0 4959
dee91e82
DE
4960/* Subroutine of dwarf2_build_psymtabs_hard to simplify it.
4961 Process compilation unit THIS_CU for a psymtab. */
4962
4963static void
95554aad
TT
4964process_psymtab_comp_unit (struct dwarf2_per_cu_data *this_cu,
4965 int want_partial_unit)
dee91e82
DE
4966{
4967 /* If this compilation unit was already read in, free the
4968 cached copy in order to read it in again. This is
4969 necessary because we skipped some symbols when we first
4970 read in the compilation unit (see load_partial_dies).
4971 This problem could be avoided, but the benefit is unclear. */
4972 if (this_cu->cu != NULL)
4973 free_one_cached_comp_unit (this_cu);
4974
3019eac3 4975 gdb_assert (! this_cu->is_debug_types);
f4dc4d17
DE
4976 init_cutu_and_read_dies (this_cu, NULL, 0, 0,
4977 process_psymtab_comp_unit_reader,
95554aad 4978 &want_partial_unit);
dee91e82
DE
4979
4980 /* Age out any secondary CUs. */
4981 age_cached_comp_units ();
93311388 4982}
ff013f42 4983
f4dc4d17
DE
4984static hashval_t
4985hash_type_unit_group (const void *item)
4986{
094b34ac 4987 const struct type_unit_group *tu_group = item;
f4dc4d17 4988
094b34ac 4989 return hash_stmt_list_entry (&tu_group->hash);
f4dc4d17 4990}
348e048f
DE
4991
4992static int
f4dc4d17 4993eq_type_unit_group (const void *item_lhs, const void *item_rhs)
348e048f 4994{
f4dc4d17
DE
4995 const struct type_unit_group *lhs = item_lhs;
4996 const struct type_unit_group *rhs = item_rhs;
348e048f 4997
094b34ac 4998 return eq_stmt_list_entry (&lhs->hash, &rhs->hash);
f4dc4d17 4999}
348e048f 5000
f4dc4d17
DE
5001/* Allocate a hash table for type unit groups. */
5002
5003static htab_t
5004allocate_type_unit_groups_table (void)
5005{
5006 return htab_create_alloc_ex (3,
5007 hash_type_unit_group,
5008 eq_type_unit_group,
5009 NULL,
5010 &dwarf2_per_objfile->objfile->objfile_obstack,
5011 hashtab_obstack_allocate,
5012 dummy_obstack_deallocate);
5013}
dee91e82 5014
f4dc4d17
DE
5015/* Type units that don't have DW_AT_stmt_list are grouped into their own
5016 partial symtabs. We combine several TUs per psymtab to not let the size
5017 of any one psymtab grow too big. */
5018#define NO_STMT_LIST_TYPE_UNIT_PSYMTAB (1 << 31)
5019#define NO_STMT_LIST_TYPE_UNIT_PSYMTAB_SIZE 10
dee91e82 5020
094b34ac 5021/* Helper routine for get_type_unit_group.
f4dc4d17
DE
5022 Create the type_unit_group object used to hold one or more TUs. */
5023
5024static struct type_unit_group *
094b34ac 5025create_type_unit_group (struct dwarf2_cu *cu, sect_offset line_offset_struct)
f4dc4d17
DE
5026{
5027 struct objfile *objfile = dwarf2_per_objfile->objfile;
094b34ac 5028 struct dwarf2_per_cu_data *per_cu;
f4dc4d17 5029 struct type_unit_group *tu_group;
f4dc4d17
DE
5030
5031 tu_group = OBSTACK_ZALLOC (&objfile->objfile_obstack,
5032 struct type_unit_group);
094b34ac 5033 per_cu = &tu_group->per_cu;
f4dc4d17
DE
5034 per_cu->objfile = objfile;
5035 per_cu->is_debug_types = 1;
5036 per_cu->s.type_unit_group = tu_group;
5037
094b34ac
DE
5038 if (dwarf2_per_objfile->using_index)
5039 {
5040 per_cu->v.quick = OBSTACK_ZALLOC (&objfile->objfile_obstack,
5041 struct dwarf2_per_cu_quick_data);
5042 tu_group->t.first_tu = cu->per_cu;
5043 }
5044 else
5045 {
5046 unsigned int line_offset = line_offset_struct.sect_off;
5047 struct partial_symtab *pst;
5048 char *name;
5049
5050 /* Give the symtab a useful name for debug purposes. */
5051 if ((line_offset & NO_STMT_LIST_TYPE_UNIT_PSYMTAB) != 0)
5052 name = xstrprintf ("<type_units_%d>",
5053 (line_offset & ~NO_STMT_LIST_TYPE_UNIT_PSYMTAB));
5054 else
5055 name = xstrprintf ("<type_units_at_0x%x>", line_offset);
5056
5057 pst = create_partial_symtab (per_cu, name);
5058 pst->anonymous = 1;
f4dc4d17 5059
094b34ac
DE
5060 xfree (name);
5061 }
f4dc4d17 5062
094b34ac
DE
5063 tu_group->hash.dwo_unit = cu->dwo_unit;
5064 tu_group->hash.line_offset = line_offset_struct;
f4dc4d17
DE
5065
5066 return tu_group;
5067}
5068
094b34ac
DE
5069/* Look up the type_unit_group for type unit CU, and create it if necessary.
5070 STMT_LIST is a DW_AT_stmt_list attribute. */
f4dc4d17
DE
5071
5072static struct type_unit_group *
094b34ac 5073get_type_unit_group (struct dwarf2_cu *cu, struct attribute *stmt_list)
f4dc4d17
DE
5074{
5075 struct tu_stats *tu_stats = &dwarf2_per_objfile->tu_stats;
5076 struct type_unit_group *tu_group;
5077 void **slot;
5078 unsigned int line_offset;
5079 struct type_unit_group type_unit_group_for_lookup;
5080
5081 if (dwarf2_per_objfile->type_unit_groups == NULL)
5082 {
5083 dwarf2_per_objfile->type_unit_groups =
5084 allocate_type_unit_groups_table ();
5085 }
5086
5087 /* Do we need to create a new group, or can we use an existing one? */
5088
5089 if (stmt_list)
5090 {
5091 line_offset = DW_UNSND (stmt_list);
5092 ++tu_stats->nr_symtab_sharers;
5093 }
5094 else
5095 {
5096 /* Ugh, no stmt_list. Rare, but we have to handle it.
5097 We can do various things here like create one group per TU or
5098 spread them over multiple groups to split up the expansion work.
5099 To avoid worst case scenarios (too many groups or too large groups)
5100 we, umm, group them in bunches. */
5101 line_offset = (NO_STMT_LIST_TYPE_UNIT_PSYMTAB
5102 | (tu_stats->nr_stmt_less_type_units
5103 / NO_STMT_LIST_TYPE_UNIT_PSYMTAB_SIZE));
5104 ++tu_stats->nr_stmt_less_type_units;
5105 }
5106
094b34ac
DE
5107 type_unit_group_for_lookup.hash.dwo_unit = cu->dwo_unit;
5108 type_unit_group_for_lookup.hash.line_offset.sect_off = line_offset;
f4dc4d17
DE
5109 slot = htab_find_slot (dwarf2_per_objfile->type_unit_groups,
5110 &type_unit_group_for_lookup, INSERT);
5111 if (*slot != NULL)
5112 {
5113 tu_group = *slot;
5114 gdb_assert (tu_group != NULL);
5115 }
5116 else
5117 {
5118 sect_offset line_offset_struct;
5119
5120 line_offset_struct.sect_off = line_offset;
094b34ac 5121 tu_group = create_type_unit_group (cu, line_offset_struct);
f4dc4d17
DE
5122 *slot = tu_group;
5123 ++tu_stats->nr_symtabs;
5124 }
5125
5126 return tu_group;
5127}
5128
5129/* Struct used to sort TUs by their abbreviation table offset. */
5130
5131struct tu_abbrev_offset
5132{
5133 struct signatured_type *sig_type;
5134 sect_offset abbrev_offset;
5135};
5136
5137/* Helper routine for build_type_unit_groups, passed to qsort. */
5138
5139static int
5140sort_tu_by_abbrev_offset (const void *ap, const void *bp)
5141{
5142 const struct tu_abbrev_offset * const *a = ap;
5143 const struct tu_abbrev_offset * const *b = bp;
5144 unsigned int aoff = (*a)->abbrev_offset.sect_off;
5145 unsigned int boff = (*b)->abbrev_offset.sect_off;
5146
5147 return (aoff > boff) - (aoff < boff);
5148}
5149
5150/* A helper function to add a type_unit_group to a table. */
5151
5152static int
5153add_type_unit_group_to_table (void **slot, void *datum)
5154{
5155 struct type_unit_group *tu_group = *slot;
5156 struct type_unit_group ***datap = datum;
5157
5158 **datap = tu_group;
5159 ++*datap;
5160
5161 return 1;
5162}
5163
5164/* Efficiently read all the type units, calling init_cutu_and_read_dies on
5165 each one passing FUNC,DATA.
5166
5167 The efficiency is because we sort TUs by the abbrev table they use and
5168 only read each abbrev table once. In one program there are 200K TUs
5169 sharing 8K abbrev tables.
5170
5171 The main purpose of this function is to support building the
5172 dwarf2_per_objfile->type_unit_groups table.
5173 TUs typically share the DW_AT_stmt_list of the CU they came from, so we
5174 can collapse the search space by grouping them by stmt_list.
5175 The savings can be significant, in the same program from above the 200K TUs
5176 share 8K stmt_list tables.
5177
5178 FUNC is expected to call get_type_unit_group, which will create the
5179 struct type_unit_group if necessary and add it to
5180 dwarf2_per_objfile->type_unit_groups. */
5181
5182static void
5183build_type_unit_groups (die_reader_func_ftype *func, void *data)
5184{
5185 struct objfile *objfile = dwarf2_per_objfile->objfile;
5186 struct tu_stats *tu_stats = &dwarf2_per_objfile->tu_stats;
5187 struct cleanup *cleanups;
5188 struct abbrev_table *abbrev_table;
5189 sect_offset abbrev_offset;
5190 struct tu_abbrev_offset *sorted_by_abbrev;
5191 struct type_unit_group **iter;
5192 int i;
5193
5194 /* It's up to the caller to not call us multiple times. */
5195 gdb_assert (dwarf2_per_objfile->type_unit_groups == NULL);
5196
5197 if (dwarf2_per_objfile->n_type_units == 0)
5198 return;
5199
5200 /* TUs typically share abbrev tables, and there can be way more TUs than
5201 abbrev tables. Sort by abbrev table to reduce the number of times we
5202 read each abbrev table in.
5203 Alternatives are to punt or to maintain a cache of abbrev tables.
5204 This is simpler and efficient enough for now.
5205
5206 Later we group TUs by their DW_AT_stmt_list value (as this defines the
5207 symtab to use). Typically TUs with the same abbrev offset have the same
5208 stmt_list value too so in practice this should work well.
5209
5210 The basic algorithm here is:
5211
5212 sort TUs by abbrev table
5213 for each TU with same abbrev table:
5214 read abbrev table if first user
5215 read TU top level DIE
5216 [IWBN if DWO skeletons had DW_AT_stmt_list]
5217 call FUNC */
5218
5219 if (dwarf2_read_debug)
5220 fprintf_unfiltered (gdb_stdlog, "Building type unit groups ...\n");
5221
5222 /* Sort in a separate table to maintain the order of all_type_units
5223 for .gdb_index: TU indices directly index all_type_units. */
5224 sorted_by_abbrev = XNEWVEC (struct tu_abbrev_offset,
5225 dwarf2_per_objfile->n_type_units);
5226 for (i = 0; i < dwarf2_per_objfile->n_type_units; ++i)
5227 {
5228 struct signatured_type *sig_type = dwarf2_per_objfile->all_type_units[i];
5229
5230 sorted_by_abbrev[i].sig_type = sig_type;
5231 sorted_by_abbrev[i].abbrev_offset =
5232 read_abbrev_offset (sig_type->per_cu.info_or_types_section,
5233 sig_type->per_cu.offset);
5234 }
5235 cleanups = make_cleanup (xfree, sorted_by_abbrev);
5236 qsort (sorted_by_abbrev, dwarf2_per_objfile->n_type_units,
5237 sizeof (struct tu_abbrev_offset), sort_tu_by_abbrev_offset);
5238
094b34ac
DE
5239 /* Note: In the .gdb_index case, get_type_unit_group may have already been
5240 called any number of times, so we don't reset tu_stats here. */
5241
f4dc4d17
DE
5242 abbrev_offset.sect_off = ~(unsigned) 0;
5243 abbrev_table = NULL;
5244 make_cleanup (abbrev_table_free_cleanup, &abbrev_table);
5245
5246 for (i = 0; i < dwarf2_per_objfile->n_type_units; ++i)
5247 {
5248 const struct tu_abbrev_offset *tu = &sorted_by_abbrev[i];
5249
5250 /* Switch to the next abbrev table if necessary. */
5251 if (abbrev_table == NULL
5252 || tu->abbrev_offset.sect_off != abbrev_offset.sect_off)
5253 {
5254 if (abbrev_table != NULL)
5255 {
5256 abbrev_table_free (abbrev_table);
5257 /* Reset to NULL in case abbrev_table_read_table throws
5258 an error: abbrev_table_free_cleanup will get called. */
5259 abbrev_table = NULL;
5260 }
5261 abbrev_offset = tu->abbrev_offset;
5262 abbrev_table =
5263 abbrev_table_read_table (&dwarf2_per_objfile->abbrev,
5264 abbrev_offset);
5265 ++tu_stats->nr_uniq_abbrev_tables;
5266 }
5267
5268 init_cutu_and_read_dies (&tu->sig_type->per_cu, abbrev_table, 0, 0,
5269 func, data);
5270 }
5271
5272 /* Create a vector of pointers to primary type units to make it easy to
5273 iterate over them and CUs. See dw2_get_primary_cu. */
5274 dwarf2_per_objfile->n_type_unit_groups =
5275 htab_elements (dwarf2_per_objfile->type_unit_groups);
5276 dwarf2_per_objfile->all_type_unit_groups =
5277 obstack_alloc (&objfile->objfile_obstack,
5278 dwarf2_per_objfile->n_type_unit_groups
5279 * sizeof (struct type_unit_group *));
5280 iter = &dwarf2_per_objfile->all_type_unit_groups[0];
5281 htab_traverse_noresize (dwarf2_per_objfile->type_unit_groups,
5282 add_type_unit_group_to_table, &iter);
5283 gdb_assert (iter - &dwarf2_per_objfile->all_type_unit_groups[0]
5284 == dwarf2_per_objfile->n_type_unit_groups);
5285
5286 do_cleanups (cleanups);
5287
5288 if (dwarf2_read_debug)
5289 {
5290 fprintf_unfiltered (gdb_stdlog, "Done building type unit groups:\n");
5291 fprintf_unfiltered (gdb_stdlog, " %d TUs\n",
5292 dwarf2_per_objfile->n_type_units);
5293 fprintf_unfiltered (gdb_stdlog, " %d uniq abbrev tables\n",
5294 tu_stats->nr_uniq_abbrev_tables);
5295 fprintf_unfiltered (gdb_stdlog, " %d symtabs from stmt_list entries\n",
5296 tu_stats->nr_symtabs);
5297 fprintf_unfiltered (gdb_stdlog, " %d symtab sharers\n",
5298 tu_stats->nr_symtab_sharers);
5299 fprintf_unfiltered (gdb_stdlog, " %d type units without a stmt_list\n",
5300 tu_stats->nr_stmt_less_type_units);
5301 }
5302}
5303
5304/* Reader function for build_type_psymtabs. */
5305
5306static void
5307build_type_psymtabs_reader (const struct die_reader_specs *reader,
5308 gdb_byte *info_ptr,
5309 struct die_info *type_unit_die,
5310 int has_children,
5311 void *data)
5312{
5313 struct objfile *objfile = dwarf2_per_objfile->objfile;
5314 struct dwarf2_cu *cu = reader->cu;
5315 struct dwarf2_per_cu_data *per_cu = cu->per_cu;
5316 struct type_unit_group *tu_group;
5317 struct attribute *attr;
5318 struct partial_die_info *first_die;
5319 CORE_ADDR lowpc, highpc;
5320 struct partial_symtab *pst;
5321
5322 gdb_assert (data == NULL);
5323
5324 if (! has_children)
5325 return;
5326
5327 attr = dwarf2_attr_no_follow (type_unit_die, DW_AT_stmt_list);
094b34ac 5328 tu_group = get_type_unit_group (cu, attr);
f4dc4d17 5329
094b34ac 5330 VEC_safe_push (dwarf2_per_cu_ptr, tu_group->t.tus, per_cu);
f4dc4d17
DE
5331
5332 prepare_one_comp_unit (cu, type_unit_die, language_minimal);
5333 cu->list_in_scope = &file_symbols;
5334 pst = create_partial_symtab (per_cu, "");
5335 pst->anonymous = 1;
5336
5337 first_die = load_partial_dies (reader, info_ptr, 1);
5338
5339 lowpc = (CORE_ADDR) -1;
5340 highpc = (CORE_ADDR) 0;
5341 scan_partial_symbols (first_die, &lowpc, &highpc, 0, cu);
5342
5343 pst->n_global_syms = objfile->global_psymbols.next -
5344 (objfile->global_psymbols.list + pst->globals_offset);
5345 pst->n_static_syms = objfile->static_psymbols.next -
5346 (objfile->static_psymbols.list + pst->statics_offset);
5347 sort_pst_symbols (pst);
5348}
5349
5350/* Traversal function for build_type_psymtabs. */
5351
5352static int
5353build_type_psymtab_dependencies (void **slot, void *info)
5354{
5355 struct objfile *objfile = dwarf2_per_objfile->objfile;
5356 struct type_unit_group *tu_group = (struct type_unit_group *) *slot;
094b34ac 5357 struct dwarf2_per_cu_data *per_cu = &tu_group->per_cu;
f4dc4d17 5358 struct partial_symtab *pst = per_cu->v.psymtab;
094b34ac 5359 int len = VEC_length (dwarf2_per_cu_ptr, tu_group->t.tus);
f4dc4d17
DE
5360 struct dwarf2_per_cu_data *iter;
5361 int i;
5362
5363 gdb_assert (len > 0);
5364
5365 pst->number_of_dependencies = len;
5366 pst->dependencies = obstack_alloc (&objfile->objfile_obstack,
5367 len * sizeof (struct psymtab *));
5368 for (i = 0;
094b34ac 5369 VEC_iterate (dwarf2_per_cu_ptr, tu_group->t.tus, i, iter);
f4dc4d17
DE
5370 ++i)
5371 {
5372 pst->dependencies[i] = iter->v.psymtab;
5373 iter->s.type_unit_group = tu_group;
5374 }
5375
094b34ac 5376 VEC_free (dwarf2_per_cu_ptr, tu_group->t.tus);
348e048f
DE
5377
5378 return 1;
5379}
5380
5381/* Subroutine of dwarf2_build_psymtabs_hard to simplify it.
5382 Build partial symbol tables for the .debug_types comp-units. */
5383
5384static void
5385build_type_psymtabs (struct objfile *objfile)
5386{
0e50663e 5387 if (! create_all_type_units (objfile))
348e048f
DE
5388 return;
5389
f4dc4d17
DE
5390 build_type_unit_groups (build_type_psymtabs_reader, NULL);
5391
5392 /* Now that all TUs have been processed we can fill in the dependencies. */
5393 htab_traverse_noresize (dwarf2_per_objfile->type_unit_groups,
5394 build_type_psymtab_dependencies, NULL);
348e048f
DE
5395}
5396
60606b2c
TT
5397/* A cleanup function that clears objfile's psymtabs_addrmap field. */
5398
5399static void
5400psymtabs_addrmap_cleanup (void *o)
5401{
5402 struct objfile *objfile = o;
ec61707d 5403
60606b2c
TT
5404 objfile->psymtabs_addrmap = NULL;
5405}
5406
95554aad
TT
5407/* Compute the 'user' field for each psymtab in OBJFILE. */
5408
5409static void
5410set_partial_user (struct objfile *objfile)
5411{
5412 int i;
5413
5414 for (i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
5415 {
5416 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
5417 struct partial_symtab *pst = per_cu->v.psymtab;
5418 int j;
5419
36586728
TT
5420 if (pst == NULL)
5421 continue;
5422
95554aad
TT
5423 for (j = 0; j < pst->number_of_dependencies; ++j)
5424 {
5425 /* Set the 'user' field only if it is not already set. */
5426 if (pst->dependencies[j]->user == NULL)
5427 pst->dependencies[j]->user = pst;
5428 }
5429 }
5430}
5431
93311388
DE
5432/* Build the partial symbol table by doing a quick pass through the
5433 .debug_info and .debug_abbrev sections. */
72bf9492 5434
93311388 5435static void
c67a9c90 5436dwarf2_build_psymtabs_hard (struct objfile *objfile)
93311388 5437{
60606b2c
TT
5438 struct cleanup *back_to, *addrmap_cleanup;
5439 struct obstack temp_obstack;
21b2bd31 5440 int i;
93311388 5441
45cfd468
DE
5442 if (dwarf2_read_debug)
5443 {
5444 fprintf_unfiltered (gdb_stdlog, "Building psymtabs of objfile %s ...\n",
5445 objfile->name);
5446 }
5447
98bfdba5
PA
5448 dwarf2_per_objfile->reading_partial_symbols = 1;
5449
be391dca 5450 dwarf2_read_section (objfile, &dwarf2_per_objfile->info);
91c24f0a 5451
93311388
DE
5452 /* Any cached compilation units will be linked by the per-objfile
5453 read_in_chain. Make sure to free them when we're done. */
5454 back_to = make_cleanup (free_cached_comp_units, NULL);
72bf9492 5455
348e048f
DE
5456 build_type_psymtabs (objfile);
5457
93311388 5458 create_all_comp_units (objfile);
c906108c 5459
60606b2c
TT
5460 /* Create a temporary address map on a temporary obstack. We later
5461 copy this to the final obstack. */
5462 obstack_init (&temp_obstack);
5463 make_cleanup_obstack_free (&temp_obstack);
5464 objfile->psymtabs_addrmap = addrmap_create_mutable (&temp_obstack);
5465 addrmap_cleanup = make_cleanup (psymtabs_addrmap_cleanup, objfile);
72bf9492 5466
21b2bd31 5467 for (i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
93311388 5468 {
21b2bd31 5469 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
aaa75496 5470
95554aad 5471 process_psymtab_comp_unit (per_cu, 0);
c906108c 5472 }
ff013f42 5473
95554aad
TT
5474 set_partial_user (objfile);
5475
ff013f42
JK
5476 objfile->psymtabs_addrmap = addrmap_create_fixed (objfile->psymtabs_addrmap,
5477 &objfile->objfile_obstack);
60606b2c 5478 discard_cleanups (addrmap_cleanup);
ff013f42 5479
ae038cb0 5480 do_cleanups (back_to);
45cfd468
DE
5481
5482 if (dwarf2_read_debug)
5483 fprintf_unfiltered (gdb_stdlog, "Done building psymtabs of %s\n",
5484 objfile->name);
ae038cb0
DJ
5485}
5486
3019eac3 5487/* die_reader_func for load_partial_comp_unit. */
ae038cb0
DJ
5488
5489static void
dee91e82
DE
5490load_partial_comp_unit_reader (const struct die_reader_specs *reader,
5491 gdb_byte *info_ptr,
5492 struct die_info *comp_unit_die,
5493 int has_children,
5494 void *data)
ae038cb0 5495{
dee91e82 5496 struct dwarf2_cu *cu = reader->cu;
ae038cb0 5497
95554aad 5498 prepare_one_comp_unit (cu, comp_unit_die, language_minimal);
ae038cb0 5499
ae038cb0
DJ
5500 /* Check if comp unit has_children.
5501 If so, read the rest of the partial symbols from this comp unit.
0963b4bd 5502 If not, there's no more debug_info for this comp unit. */
d85a05f0 5503 if (has_children)
dee91e82
DE
5504 load_partial_dies (reader, info_ptr, 0);
5505}
98bfdba5 5506
dee91e82
DE
5507/* Load the partial DIEs for a secondary CU into memory.
5508 This is also used when rereading a primary CU with load_all_dies. */
c5b7e1cb 5509
dee91e82
DE
5510static void
5511load_partial_comp_unit (struct dwarf2_per_cu_data *this_cu)
5512{
f4dc4d17
DE
5513 init_cutu_and_read_dies (this_cu, NULL, 1, 1,
5514 load_partial_comp_unit_reader, NULL);
ae038cb0
DJ
5515}
5516
ae038cb0 5517static void
36586728
TT
5518read_comp_units_from_section (struct objfile *objfile,
5519 struct dwarf2_section_info *section,
5520 unsigned int is_dwz,
5521 int *n_allocated,
5522 int *n_comp_units,
5523 struct dwarf2_per_cu_data ***all_comp_units)
ae038cb0 5524{
be391dca 5525 gdb_byte *info_ptr;
36586728 5526 bfd *abfd = section->asection->owner;
be391dca 5527
36586728 5528 dwarf2_read_section (objfile, section);
ae038cb0 5529
36586728 5530 info_ptr = section->buffer;
6e70227d 5531
36586728 5532 while (info_ptr < section->buffer + section->size)
ae038cb0 5533 {
c764a876 5534 unsigned int length, initial_length_size;
ae038cb0 5535 struct dwarf2_per_cu_data *this_cu;
b64f50a1 5536 sect_offset offset;
ae038cb0 5537
36586728 5538 offset.sect_off = info_ptr - section->buffer;
ae038cb0
DJ
5539
5540 /* Read just enough information to find out where the next
5541 compilation unit is. */
36586728 5542 length = read_initial_length (abfd, info_ptr, &initial_length_size);
ae038cb0
DJ
5543
5544 /* Save the compilation unit for later lookup. */
5545 this_cu = obstack_alloc (&objfile->objfile_obstack,
5546 sizeof (struct dwarf2_per_cu_data));
5547 memset (this_cu, 0, sizeof (*this_cu));
5548 this_cu->offset = offset;
c764a876 5549 this_cu->length = length + initial_length_size;
36586728 5550 this_cu->is_dwz = is_dwz;
9291a0cd 5551 this_cu->objfile = objfile;
36586728 5552 this_cu->info_or_types_section = section;
ae038cb0 5553
36586728 5554 if (*n_comp_units == *n_allocated)
ae038cb0 5555 {
36586728
TT
5556 *n_allocated *= 2;
5557 *all_comp_units = xrealloc (*all_comp_units,
5558 *n_allocated
5559 * sizeof (struct dwarf2_per_cu_data *));
ae038cb0 5560 }
36586728
TT
5561 (*all_comp_units)[*n_comp_units] = this_cu;
5562 ++*n_comp_units;
ae038cb0
DJ
5563
5564 info_ptr = info_ptr + this_cu->length;
5565 }
36586728
TT
5566}
5567
5568/* Create a list of all compilation units in OBJFILE.
5569 This is only done for -readnow and building partial symtabs. */
5570
5571static void
5572create_all_comp_units (struct objfile *objfile)
5573{
5574 int n_allocated;
5575 int n_comp_units;
5576 struct dwarf2_per_cu_data **all_comp_units;
5577
5578 n_comp_units = 0;
5579 n_allocated = 10;
5580 all_comp_units = xmalloc (n_allocated
5581 * sizeof (struct dwarf2_per_cu_data *));
5582
5583 read_comp_units_from_section (objfile, &dwarf2_per_objfile->info, 0,
5584 &n_allocated, &n_comp_units, &all_comp_units);
5585
5586 if (bfd_get_section_by_name (objfile->obfd, ".gnu_debugaltlink") != NULL)
5587 {
5588 struct dwz_file *dwz = dwarf2_get_dwz_file ();
5589
5590 read_comp_units_from_section (objfile, &dwz->info, 1,
5591 &n_allocated, &n_comp_units,
5592 &all_comp_units);
5593 }
ae038cb0
DJ
5594
5595 dwarf2_per_objfile->all_comp_units
5596 = obstack_alloc (&objfile->objfile_obstack,
5597 n_comp_units * sizeof (struct dwarf2_per_cu_data *));
5598 memcpy (dwarf2_per_objfile->all_comp_units, all_comp_units,
5599 n_comp_units * sizeof (struct dwarf2_per_cu_data *));
5600 xfree (all_comp_units);
5601 dwarf2_per_objfile->n_comp_units = n_comp_units;
c906108c
SS
5602}
5603
5734ee8b
DJ
5604/* Process all loaded DIEs for compilation unit CU, starting at
5605 FIRST_DIE. The caller should pass NEED_PC == 1 if the compilation
5606 unit DIE did not have PC info (DW_AT_low_pc and DW_AT_high_pc, or
5607 DW_AT_ranges). If NEED_PC is set, then this function will set
5608 *LOWPC and *HIGHPC to the lowest and highest PC values found in CU
5609 and record the covered ranges in the addrmap. */
c906108c 5610
72bf9492
DJ
5611static void
5612scan_partial_symbols (struct partial_die_info *first_die, CORE_ADDR *lowpc,
5734ee8b 5613 CORE_ADDR *highpc, int need_pc, struct dwarf2_cu *cu)
c906108c 5614{
72bf9492 5615 struct partial_die_info *pdi;
c906108c 5616
91c24f0a
DC
5617 /* Now, march along the PDI's, descending into ones which have
5618 interesting children but skipping the children of the other ones,
5619 until we reach the end of the compilation unit. */
c906108c 5620
72bf9492 5621 pdi = first_die;
91c24f0a 5622
72bf9492
DJ
5623 while (pdi != NULL)
5624 {
5625 fixup_partial_die (pdi, cu);
c906108c 5626
f55ee35c 5627 /* Anonymous namespaces or modules have no name but have interesting
91c24f0a
DC
5628 children, so we need to look at them. Ditto for anonymous
5629 enums. */
933c6fe4 5630
72bf9492 5631 if (pdi->name != NULL || pdi->tag == DW_TAG_namespace
95554aad
TT
5632 || pdi->tag == DW_TAG_module || pdi->tag == DW_TAG_enumeration_type
5633 || pdi->tag == DW_TAG_imported_unit)
c906108c 5634 {
72bf9492 5635 switch (pdi->tag)
c906108c
SS
5636 {
5637 case DW_TAG_subprogram:
5734ee8b 5638 add_partial_subprogram (pdi, lowpc, highpc, need_pc, cu);
c906108c 5639 break;
72929c62 5640 case DW_TAG_constant:
c906108c
SS
5641 case DW_TAG_variable:
5642 case DW_TAG_typedef:
91c24f0a 5643 case DW_TAG_union_type:
72bf9492 5644 if (!pdi->is_declaration)
63d06c5c 5645 {
72bf9492 5646 add_partial_symbol (pdi, cu);
63d06c5c
DC
5647 }
5648 break;
c906108c 5649 case DW_TAG_class_type:
680b30c7 5650 case DW_TAG_interface_type:
c906108c 5651 case DW_TAG_structure_type:
72bf9492 5652 if (!pdi->is_declaration)
c906108c 5653 {
72bf9492 5654 add_partial_symbol (pdi, cu);
c906108c
SS
5655 }
5656 break;
91c24f0a 5657 case DW_TAG_enumeration_type:
72bf9492
DJ
5658 if (!pdi->is_declaration)
5659 add_partial_enumeration (pdi, cu);
c906108c
SS
5660 break;
5661 case DW_TAG_base_type:
a02abb62 5662 case DW_TAG_subrange_type:
c906108c 5663 /* File scope base type definitions are added to the partial
c5aa993b 5664 symbol table. */
72bf9492 5665 add_partial_symbol (pdi, cu);
c906108c 5666 break;
d9fa45fe 5667 case DW_TAG_namespace:
5734ee8b 5668 add_partial_namespace (pdi, lowpc, highpc, need_pc, cu);
91c24f0a 5669 break;
5d7cb8df
JK
5670 case DW_TAG_module:
5671 add_partial_module (pdi, lowpc, highpc, need_pc, cu);
5672 break;
95554aad
TT
5673 case DW_TAG_imported_unit:
5674 {
5675 struct dwarf2_per_cu_data *per_cu;
5676
f4dc4d17
DE
5677 /* For now we don't handle imported units in type units. */
5678 if (cu->per_cu->is_debug_types)
5679 {
5680 error (_("Dwarf Error: DW_TAG_imported_unit is not"
5681 " supported in type units [in module %s]"),
5682 cu->objfile->name);
5683 }
5684
95554aad 5685 per_cu = dwarf2_find_containing_comp_unit (pdi->d.offset,
36586728 5686 pdi->is_dwz,
95554aad
TT
5687 cu->objfile);
5688
5689 /* Go read the partial unit, if needed. */
5690 if (per_cu->v.psymtab == NULL)
5691 process_psymtab_comp_unit (per_cu, 1);
5692
f4dc4d17
DE
5693 VEC_safe_push (dwarf2_per_cu_ptr,
5694 cu->per_cu->s.imported_symtabs, per_cu);
95554aad
TT
5695 }
5696 break;
c906108c
SS
5697 default:
5698 break;
5699 }
5700 }
5701
72bf9492
DJ
5702 /* If the die has a sibling, skip to the sibling. */
5703
5704 pdi = pdi->die_sibling;
5705 }
5706}
5707
5708/* Functions used to compute the fully scoped name of a partial DIE.
91c24f0a 5709
72bf9492 5710 Normally, this is simple. For C++, the parent DIE's fully scoped
987504bb
JJ
5711 name is concatenated with "::" and the partial DIE's name. For
5712 Java, the same thing occurs except that "." is used instead of "::".
72bf9492
DJ
5713 Enumerators are an exception; they use the scope of their parent
5714 enumeration type, i.e. the name of the enumeration type is not
5715 prepended to the enumerator.
91c24f0a 5716
72bf9492
DJ
5717 There are two complexities. One is DW_AT_specification; in this
5718 case "parent" means the parent of the target of the specification,
5719 instead of the direct parent of the DIE. The other is compilers
5720 which do not emit DW_TAG_namespace; in this case we try to guess
5721 the fully qualified name of structure types from their members'
5722 linkage names. This must be done using the DIE's children rather
5723 than the children of any DW_AT_specification target. We only need
5724 to do this for structures at the top level, i.e. if the target of
5725 any DW_AT_specification (if any; otherwise the DIE itself) does not
5726 have a parent. */
5727
5728/* Compute the scope prefix associated with PDI's parent, in
5729 compilation unit CU. The result will be allocated on CU's
5730 comp_unit_obstack, or a copy of the already allocated PDI->NAME
5731 field. NULL is returned if no prefix is necessary. */
5732static char *
5733partial_die_parent_scope (struct partial_die_info *pdi,
5734 struct dwarf2_cu *cu)
5735{
5736 char *grandparent_scope;
5737 struct partial_die_info *parent, *real_pdi;
91c24f0a 5738
72bf9492
DJ
5739 /* We need to look at our parent DIE; if we have a DW_AT_specification,
5740 then this means the parent of the specification DIE. */
5741
5742 real_pdi = pdi;
72bf9492 5743 while (real_pdi->has_specification)
36586728
TT
5744 real_pdi = find_partial_die (real_pdi->spec_offset,
5745 real_pdi->spec_is_dwz, cu);
72bf9492
DJ
5746
5747 parent = real_pdi->die_parent;
5748 if (parent == NULL)
5749 return NULL;
5750
5751 if (parent->scope_set)
5752 return parent->scope;
5753
5754 fixup_partial_die (parent, cu);
5755
10b3939b 5756 grandparent_scope = partial_die_parent_scope (parent, cu);
72bf9492 5757
acebe513
UW
5758 /* GCC 4.0 and 4.1 had a bug (PR c++/28460) where they generated bogus
5759 DW_TAG_namespace DIEs with a name of "::" for the global namespace.
5760 Work around this problem here. */
5761 if (cu->language == language_cplus
6e70227d 5762 && parent->tag == DW_TAG_namespace
acebe513
UW
5763 && strcmp (parent->name, "::") == 0
5764 && grandparent_scope == NULL)
5765 {
5766 parent->scope = NULL;
5767 parent->scope_set = 1;
5768 return NULL;
5769 }
5770
9c6c53f7
SA
5771 if (pdi->tag == DW_TAG_enumerator)
5772 /* Enumerators should not get the name of the enumeration as a prefix. */
5773 parent->scope = grandparent_scope;
5774 else if (parent->tag == DW_TAG_namespace
f55ee35c 5775 || parent->tag == DW_TAG_module
72bf9492
DJ
5776 || parent->tag == DW_TAG_structure_type
5777 || parent->tag == DW_TAG_class_type
680b30c7 5778 || parent->tag == DW_TAG_interface_type
ceeb3d5a
TT
5779 || parent->tag == DW_TAG_union_type
5780 || parent->tag == DW_TAG_enumeration_type)
72bf9492
DJ
5781 {
5782 if (grandparent_scope == NULL)
5783 parent->scope = parent->name;
5784 else
3e43a32a
MS
5785 parent->scope = typename_concat (&cu->comp_unit_obstack,
5786 grandparent_scope,
f55ee35c 5787 parent->name, 0, cu);
72bf9492 5788 }
72bf9492
DJ
5789 else
5790 {
5791 /* FIXME drow/2004-04-01: What should we be doing with
5792 function-local names? For partial symbols, we should probably be
5793 ignoring them. */
5794 complaint (&symfile_complaints,
e2e0b3e5 5795 _("unhandled containing DIE tag %d for DIE at %d"),
b64f50a1 5796 parent->tag, pdi->offset.sect_off);
72bf9492 5797 parent->scope = grandparent_scope;
c906108c
SS
5798 }
5799
72bf9492
DJ
5800 parent->scope_set = 1;
5801 return parent->scope;
5802}
5803
5804/* Return the fully scoped name associated with PDI, from compilation unit
5805 CU. The result will be allocated with malloc. */
4568ecf9 5806
72bf9492
DJ
5807static char *
5808partial_die_full_name (struct partial_die_info *pdi,
5809 struct dwarf2_cu *cu)
5810{
5811 char *parent_scope;
5812
98bfdba5
PA
5813 /* If this is a template instantiation, we can not work out the
5814 template arguments from partial DIEs. So, unfortunately, we have
5815 to go through the full DIEs. At least any work we do building
5816 types here will be reused if full symbols are loaded later. */
5817 if (pdi->has_template_arguments)
5818 {
5819 fixup_partial_die (pdi, cu);
5820
5821 if (pdi->name != NULL && strchr (pdi->name, '<') == NULL)
5822 {
5823 struct die_info *die;
5824 struct attribute attr;
5825 struct dwarf2_cu *ref_cu = cu;
5826
b64f50a1 5827 /* DW_FORM_ref_addr is using section offset. */
98bfdba5
PA
5828 attr.name = 0;
5829 attr.form = DW_FORM_ref_addr;
4568ecf9 5830 attr.u.unsnd = pdi->offset.sect_off;
98bfdba5
PA
5831 die = follow_die_ref (NULL, &attr, &ref_cu);
5832
5833 return xstrdup (dwarf2_full_name (NULL, die, ref_cu));
5834 }
5835 }
5836
72bf9492
DJ
5837 parent_scope = partial_die_parent_scope (pdi, cu);
5838 if (parent_scope == NULL)
5839 return NULL;
5840 else
f55ee35c 5841 return typename_concat (NULL, parent_scope, pdi->name, 0, cu);
c906108c
SS
5842}
5843
5844static void
72bf9492 5845add_partial_symbol (struct partial_die_info *pdi, struct dwarf2_cu *cu)
c906108c 5846{
e7c27a73 5847 struct objfile *objfile = cu->objfile;
c906108c 5848 CORE_ADDR addr = 0;
decbce07 5849 char *actual_name = NULL;
e142c38c 5850 CORE_ADDR baseaddr;
72bf9492 5851 int built_actual_name = 0;
e142c38c
DJ
5852
5853 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
c906108c 5854
94af9270
KS
5855 actual_name = partial_die_full_name (pdi, cu);
5856 if (actual_name)
5857 built_actual_name = 1;
63d06c5c 5858
72bf9492
DJ
5859 if (actual_name == NULL)
5860 actual_name = pdi->name;
5861
c906108c
SS
5862 switch (pdi->tag)
5863 {
5864 case DW_TAG_subprogram:
2cfa0c8d 5865 if (pdi->is_external || cu->language == language_ada)
c906108c 5866 {
2cfa0c8d
JB
5867 /* brobecker/2007-12-26: Normally, only "external" DIEs are part
5868 of the global scope. But in Ada, we want to be able to access
5869 nested procedures globally. So all Ada subprograms are stored
5870 in the global scope. */
f47fb265 5871 /* prim_record_minimal_symbol (actual_name, pdi->lowpc + baseaddr,
c5aa993b 5872 mst_text, objfile); */
f47fb265
MS
5873 add_psymbol_to_list (actual_name, strlen (actual_name),
5874 built_actual_name,
5875 VAR_DOMAIN, LOC_BLOCK,
5876 &objfile->global_psymbols,
5877 0, pdi->lowpc + baseaddr,
5878 cu->language, objfile);
c906108c
SS
5879 }
5880 else
5881 {
f47fb265 5882 /* prim_record_minimal_symbol (actual_name, pdi->lowpc + baseaddr,
c5aa993b 5883 mst_file_text, objfile); */
f47fb265
MS
5884 add_psymbol_to_list (actual_name, strlen (actual_name),
5885 built_actual_name,
5886 VAR_DOMAIN, LOC_BLOCK,
5887 &objfile->static_psymbols,
5888 0, pdi->lowpc + baseaddr,
5889 cu->language, objfile);
c906108c
SS
5890 }
5891 break;
72929c62
JB
5892 case DW_TAG_constant:
5893 {
5894 struct psymbol_allocation_list *list;
5895
5896 if (pdi->is_external)
5897 list = &objfile->global_psymbols;
5898 else
5899 list = &objfile->static_psymbols;
f47fb265
MS
5900 add_psymbol_to_list (actual_name, strlen (actual_name),
5901 built_actual_name, VAR_DOMAIN, LOC_STATIC,
5902 list, 0, 0, cu->language, objfile);
72929c62
JB
5903 }
5904 break;
c906108c 5905 case DW_TAG_variable:
95554aad
TT
5906 if (pdi->d.locdesc)
5907 addr = decode_locdesc (pdi->d.locdesc, cu);
caac4577 5908
95554aad 5909 if (pdi->d.locdesc
caac4577
JG
5910 && addr == 0
5911 && !dwarf2_per_objfile->has_section_at_zero)
5912 {
5913 /* A global or static variable may also have been stripped
5914 out by the linker if unused, in which case its address
5915 will be nullified; do not add such variables into partial
5916 symbol table then. */
5917 }
5918 else if (pdi->is_external)
c906108c
SS
5919 {
5920 /* Global Variable.
5921 Don't enter into the minimal symbol tables as there is
5922 a minimal symbol table entry from the ELF symbols already.
5923 Enter into partial symbol table if it has a location
5924 descriptor or a type.
5925 If the location descriptor is missing, new_symbol will create
5926 a LOC_UNRESOLVED symbol, the address of the variable will then
5927 be determined from the minimal symbol table whenever the variable
5928 is referenced.
5929 The address for the partial symbol table entry is not
5930 used by GDB, but it comes in handy for debugging partial symbol
5931 table building. */
5932
95554aad 5933 if (pdi->d.locdesc || pdi->has_type)
f47fb265
MS
5934 add_psymbol_to_list (actual_name, strlen (actual_name),
5935 built_actual_name,
5936 VAR_DOMAIN, LOC_STATIC,
5937 &objfile->global_psymbols,
5938 0, addr + baseaddr,
5939 cu->language, objfile);
c906108c
SS
5940 }
5941 else
5942 {
0963b4bd 5943 /* Static Variable. Skip symbols without location descriptors. */
95554aad 5944 if (pdi->d.locdesc == NULL)
decbce07
MS
5945 {
5946 if (built_actual_name)
5947 xfree (actual_name);
5948 return;
5949 }
f47fb265 5950 /* prim_record_minimal_symbol (actual_name, addr + baseaddr,
c5aa993b 5951 mst_file_data, objfile); */
f47fb265
MS
5952 add_psymbol_to_list (actual_name, strlen (actual_name),
5953 built_actual_name,
5954 VAR_DOMAIN, LOC_STATIC,
5955 &objfile->static_psymbols,
5956 0, addr + baseaddr,
5957 cu->language, objfile);
c906108c
SS
5958 }
5959 break;
5960 case DW_TAG_typedef:
5961 case DW_TAG_base_type:
a02abb62 5962 case DW_TAG_subrange_type:
38d518c9 5963 add_psymbol_to_list (actual_name, strlen (actual_name),
04a679b8 5964 built_actual_name,
176620f1 5965 VAR_DOMAIN, LOC_TYPEDEF,
c906108c 5966 &objfile->static_psymbols,
e142c38c 5967 0, (CORE_ADDR) 0, cu->language, objfile);
c906108c 5968 break;
72bf9492
DJ
5969 case DW_TAG_namespace:
5970 add_psymbol_to_list (actual_name, strlen (actual_name),
04a679b8 5971 built_actual_name,
72bf9492
DJ
5972 VAR_DOMAIN, LOC_TYPEDEF,
5973 &objfile->global_psymbols,
5974 0, (CORE_ADDR) 0, cu->language, objfile);
5975 break;
c906108c 5976 case DW_TAG_class_type:
680b30c7 5977 case DW_TAG_interface_type:
c906108c
SS
5978 case DW_TAG_structure_type:
5979 case DW_TAG_union_type:
5980 case DW_TAG_enumeration_type:
fa4028e9
JB
5981 /* Skip external references. The DWARF standard says in the section
5982 about "Structure, Union, and Class Type Entries": "An incomplete
5983 structure, union or class type is represented by a structure,
5984 union or class entry that does not have a byte size attribute
5985 and that has a DW_AT_declaration attribute." */
5986 if (!pdi->has_byte_size && pdi->is_declaration)
decbce07
MS
5987 {
5988 if (built_actual_name)
5989 xfree (actual_name);
5990 return;
5991 }
fa4028e9 5992
63d06c5c
DC
5993 /* NOTE: carlton/2003-10-07: See comment in new_symbol about
5994 static vs. global. */
38d518c9 5995 add_psymbol_to_list (actual_name, strlen (actual_name),
04a679b8 5996 built_actual_name,
176620f1 5997 STRUCT_DOMAIN, LOC_TYPEDEF,
987504bb
JJ
5998 (cu->language == language_cplus
5999 || cu->language == language_java)
63d06c5c
DC
6000 ? &objfile->global_psymbols
6001 : &objfile->static_psymbols,
e142c38c 6002 0, (CORE_ADDR) 0, cu->language, objfile);
c906108c 6003
c906108c
SS
6004 break;
6005 case DW_TAG_enumerator:
38d518c9 6006 add_psymbol_to_list (actual_name, strlen (actual_name),
04a679b8 6007 built_actual_name,
176620f1 6008 VAR_DOMAIN, LOC_CONST,
987504bb
JJ
6009 (cu->language == language_cplus
6010 || cu->language == language_java)
f6fe98ef
DJ
6011 ? &objfile->global_psymbols
6012 : &objfile->static_psymbols,
e142c38c 6013 0, (CORE_ADDR) 0, cu->language, objfile);
c906108c
SS
6014 break;
6015 default:
6016 break;
6017 }
5c4e30ca 6018
72bf9492
DJ
6019 if (built_actual_name)
6020 xfree (actual_name);
c906108c
SS
6021}
6022
5c4e30ca
DC
6023/* Read a partial die corresponding to a namespace; also, add a symbol
6024 corresponding to that namespace to the symbol table. NAMESPACE is
6025 the name of the enclosing namespace. */
91c24f0a 6026
72bf9492
DJ
6027static void
6028add_partial_namespace (struct partial_die_info *pdi,
91c24f0a 6029 CORE_ADDR *lowpc, CORE_ADDR *highpc,
5734ee8b 6030 int need_pc, struct dwarf2_cu *cu)
91c24f0a 6031{
72bf9492 6032 /* Add a symbol for the namespace. */
e7c27a73 6033
72bf9492 6034 add_partial_symbol (pdi, cu);
5c4e30ca
DC
6035
6036 /* Now scan partial symbols in that namespace. */
6037
91c24f0a 6038 if (pdi->has_children)
5734ee8b 6039 scan_partial_symbols (pdi->die_child, lowpc, highpc, need_pc, cu);
91c24f0a
DC
6040}
6041
5d7cb8df
JK
6042/* Read a partial die corresponding to a Fortran module. */
6043
6044static void
6045add_partial_module (struct partial_die_info *pdi, CORE_ADDR *lowpc,
6046 CORE_ADDR *highpc, int need_pc, struct dwarf2_cu *cu)
6047{
f55ee35c 6048 /* Now scan partial symbols in that module. */
5d7cb8df
JK
6049
6050 if (pdi->has_children)
6051 scan_partial_symbols (pdi->die_child, lowpc, highpc, need_pc, cu);
6052}
6053
bc30ff58
JB
6054/* Read a partial die corresponding to a subprogram and create a partial
6055 symbol for that subprogram. When the CU language allows it, this
6056 routine also defines a partial symbol for each nested subprogram
6057 that this subprogram contains.
6e70227d 6058
bc30ff58
JB
6059 DIE my also be a lexical block, in which case we simply search
6060 recursively for suprograms defined inside that lexical block.
6061 Again, this is only performed when the CU language allows this
6062 type of definitions. */
6063
6064static void
6065add_partial_subprogram (struct partial_die_info *pdi,
6066 CORE_ADDR *lowpc, CORE_ADDR *highpc,
5734ee8b 6067 int need_pc, struct dwarf2_cu *cu)
bc30ff58
JB
6068{
6069 if (pdi->tag == DW_TAG_subprogram)
6070 {
6071 if (pdi->has_pc_info)
6072 {
6073 if (pdi->lowpc < *lowpc)
6074 *lowpc = pdi->lowpc;
6075 if (pdi->highpc > *highpc)
6076 *highpc = pdi->highpc;
5734ee8b
DJ
6077 if (need_pc)
6078 {
6079 CORE_ADDR baseaddr;
6080 struct objfile *objfile = cu->objfile;
6081
6082 baseaddr = ANOFFSET (objfile->section_offsets,
6083 SECT_OFF_TEXT (objfile));
6084 addrmap_set_empty (objfile->psymtabs_addrmap,
01637564
DE
6085 pdi->lowpc + baseaddr,
6086 pdi->highpc - 1 + baseaddr,
9291a0cd 6087 cu->per_cu->v.psymtab);
5734ee8b 6088 }
481860b3
GB
6089 }
6090
6091 if (pdi->has_pc_info || (!pdi->is_external && pdi->may_be_inlined))
6092 {
bc30ff58 6093 if (!pdi->is_declaration)
e8d05480
JB
6094 /* Ignore subprogram DIEs that do not have a name, they are
6095 illegal. Do not emit a complaint at this point, we will
6096 do so when we convert this psymtab into a symtab. */
6097 if (pdi->name)
6098 add_partial_symbol (pdi, cu);
bc30ff58
JB
6099 }
6100 }
6e70227d 6101
bc30ff58
JB
6102 if (! pdi->has_children)
6103 return;
6104
6105 if (cu->language == language_ada)
6106 {
6107 pdi = pdi->die_child;
6108 while (pdi != NULL)
6109 {
6110 fixup_partial_die (pdi, cu);
6111 if (pdi->tag == DW_TAG_subprogram
6112 || pdi->tag == DW_TAG_lexical_block)
5734ee8b 6113 add_partial_subprogram (pdi, lowpc, highpc, need_pc, cu);
bc30ff58
JB
6114 pdi = pdi->die_sibling;
6115 }
6116 }
6117}
6118
91c24f0a
DC
6119/* Read a partial die corresponding to an enumeration type. */
6120
72bf9492
DJ
6121static void
6122add_partial_enumeration (struct partial_die_info *enum_pdi,
6123 struct dwarf2_cu *cu)
91c24f0a 6124{
72bf9492 6125 struct partial_die_info *pdi;
91c24f0a
DC
6126
6127 if (enum_pdi->name != NULL)
72bf9492
DJ
6128 add_partial_symbol (enum_pdi, cu);
6129
6130 pdi = enum_pdi->die_child;
6131 while (pdi)
91c24f0a 6132 {
72bf9492 6133 if (pdi->tag != DW_TAG_enumerator || pdi->name == NULL)
e2e0b3e5 6134 complaint (&symfile_complaints, _("malformed enumerator DIE ignored"));
91c24f0a 6135 else
72bf9492
DJ
6136 add_partial_symbol (pdi, cu);
6137 pdi = pdi->die_sibling;
91c24f0a 6138 }
91c24f0a
DC
6139}
6140
6caca83c
CC
6141/* Return the initial uleb128 in the die at INFO_PTR. */
6142
6143static unsigned int
6144peek_abbrev_code (bfd *abfd, gdb_byte *info_ptr)
6145{
6146 unsigned int bytes_read;
6147
6148 return read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
6149}
6150
4bb7a0a7
DJ
6151/* Read the initial uleb128 in the die at INFO_PTR in compilation unit CU.
6152 Return the corresponding abbrev, or NULL if the number is zero (indicating
6153 an empty DIE). In either case *BYTES_READ will be set to the length of
6154 the initial number. */
6155
6156static struct abbrev_info *
fe1b8b76 6157peek_die_abbrev (gdb_byte *info_ptr, unsigned int *bytes_read,
891d2f0b 6158 struct dwarf2_cu *cu)
4bb7a0a7
DJ
6159{
6160 bfd *abfd = cu->objfile->obfd;
6161 unsigned int abbrev_number;
6162 struct abbrev_info *abbrev;
6163
6164 abbrev_number = read_unsigned_leb128 (abfd, info_ptr, bytes_read);
6165
6166 if (abbrev_number == 0)
6167 return NULL;
6168
433df2d4 6169 abbrev = abbrev_table_lookup_abbrev (cu->abbrev_table, abbrev_number);
4bb7a0a7
DJ
6170 if (!abbrev)
6171 {
3e43a32a
MS
6172 error (_("Dwarf Error: Could not find abbrev number %d [in module %s]"),
6173 abbrev_number, bfd_get_filename (abfd));
4bb7a0a7
DJ
6174 }
6175
6176 return abbrev;
6177}
6178
93311388
DE
6179/* Scan the debug information for CU starting at INFO_PTR in buffer BUFFER.
6180 Returns a pointer to the end of a series of DIEs, terminated by an empty
4bb7a0a7
DJ
6181 DIE. Any children of the skipped DIEs will also be skipped. */
6182
fe1b8b76 6183static gdb_byte *
dee91e82 6184skip_children (const struct die_reader_specs *reader, gdb_byte *info_ptr)
4bb7a0a7 6185{
dee91e82 6186 struct dwarf2_cu *cu = reader->cu;
4bb7a0a7
DJ
6187 struct abbrev_info *abbrev;
6188 unsigned int bytes_read;
6189
6190 while (1)
6191 {
6192 abbrev = peek_die_abbrev (info_ptr, &bytes_read, cu);
6193 if (abbrev == NULL)
6194 return info_ptr + bytes_read;
6195 else
dee91e82 6196 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
4bb7a0a7
DJ
6197 }
6198}
6199
93311388
DE
6200/* Scan the debug information for CU starting at INFO_PTR in buffer BUFFER.
6201 INFO_PTR should point just after the initial uleb128 of a DIE, and the
4bb7a0a7
DJ
6202 abbrev corresponding to that skipped uleb128 should be passed in
6203 ABBREV. Returns a pointer to this DIE's sibling, skipping any
6204 children. */
6205
fe1b8b76 6206static gdb_byte *
dee91e82
DE
6207skip_one_die (const struct die_reader_specs *reader, gdb_byte *info_ptr,
6208 struct abbrev_info *abbrev)
4bb7a0a7
DJ
6209{
6210 unsigned int bytes_read;
6211 struct attribute attr;
dee91e82
DE
6212 bfd *abfd = reader->abfd;
6213 struct dwarf2_cu *cu = reader->cu;
6214 gdb_byte *buffer = reader->buffer;
f664829e
DE
6215 const gdb_byte *buffer_end = reader->buffer_end;
6216 gdb_byte *start_info_ptr = info_ptr;
4bb7a0a7
DJ
6217 unsigned int form, i;
6218
6219 for (i = 0; i < abbrev->num_attrs; i++)
6220 {
6221 /* The only abbrev we care about is DW_AT_sibling. */
6222 if (abbrev->attrs[i].name == DW_AT_sibling)
6223 {
dee91e82 6224 read_attribute (reader, &attr, &abbrev->attrs[i], info_ptr);
4bb7a0a7 6225 if (attr.form == DW_FORM_ref_addr)
3e43a32a
MS
6226 complaint (&symfile_complaints,
6227 _("ignoring absolute DW_AT_sibling"));
4bb7a0a7 6228 else
b64f50a1 6229 return buffer + dwarf2_get_ref_die_offset (&attr).sect_off;
4bb7a0a7
DJ
6230 }
6231
6232 /* If it isn't DW_AT_sibling, skip this attribute. */
6233 form = abbrev->attrs[i].form;
6234 skip_attribute:
6235 switch (form)
6236 {
4bb7a0a7 6237 case DW_FORM_ref_addr:
ae411497
TT
6238 /* In DWARF 2, DW_FORM_ref_addr is address sized; in DWARF 3
6239 and later it is offset sized. */
6240 if (cu->header.version == 2)
6241 info_ptr += cu->header.addr_size;
6242 else
6243 info_ptr += cu->header.offset_size;
6244 break;
36586728
TT
6245 case DW_FORM_GNU_ref_alt:
6246 info_ptr += cu->header.offset_size;
6247 break;
ae411497 6248 case DW_FORM_addr:
4bb7a0a7
DJ
6249 info_ptr += cu->header.addr_size;
6250 break;
6251 case DW_FORM_data1:
6252 case DW_FORM_ref1:
6253 case DW_FORM_flag:
6254 info_ptr += 1;
6255 break;
2dc7f7b3
TT
6256 case DW_FORM_flag_present:
6257 break;
4bb7a0a7
DJ
6258 case DW_FORM_data2:
6259 case DW_FORM_ref2:
6260 info_ptr += 2;
6261 break;
6262 case DW_FORM_data4:
6263 case DW_FORM_ref4:
6264 info_ptr += 4;
6265 break;
6266 case DW_FORM_data8:
6267 case DW_FORM_ref8:
55f1336d 6268 case DW_FORM_ref_sig8:
4bb7a0a7
DJ
6269 info_ptr += 8;
6270 break;
6271 case DW_FORM_string:
9b1c24c8 6272 read_direct_string (abfd, info_ptr, &bytes_read);
4bb7a0a7
DJ
6273 info_ptr += bytes_read;
6274 break;
2dc7f7b3 6275 case DW_FORM_sec_offset:
4bb7a0a7 6276 case DW_FORM_strp:
36586728 6277 case DW_FORM_GNU_strp_alt:
4bb7a0a7
DJ
6278 info_ptr += cu->header.offset_size;
6279 break;
2dc7f7b3 6280 case DW_FORM_exprloc:
4bb7a0a7
DJ
6281 case DW_FORM_block:
6282 info_ptr += read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
6283 info_ptr += bytes_read;
6284 break;
6285 case DW_FORM_block1:
6286 info_ptr += 1 + read_1_byte (abfd, info_ptr);
6287 break;
6288 case DW_FORM_block2:
6289 info_ptr += 2 + read_2_bytes (abfd, info_ptr);
6290 break;
6291 case DW_FORM_block4:
6292 info_ptr += 4 + read_4_bytes (abfd, info_ptr);
6293 break;
6294 case DW_FORM_sdata:
6295 case DW_FORM_udata:
6296 case DW_FORM_ref_udata:
3019eac3
DE
6297 case DW_FORM_GNU_addr_index:
6298 case DW_FORM_GNU_str_index:
f664829e 6299 info_ptr = (gdb_byte *) safe_skip_leb128 (info_ptr, buffer_end);
4bb7a0a7
DJ
6300 break;
6301 case DW_FORM_indirect:
6302 form = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
6303 info_ptr += bytes_read;
6304 /* We need to continue parsing from here, so just go back to
6305 the top. */
6306 goto skip_attribute;
6307
6308 default:
3e43a32a
MS
6309 error (_("Dwarf Error: Cannot handle %s "
6310 "in DWARF reader [in module %s]"),
4bb7a0a7
DJ
6311 dwarf_form_name (form),
6312 bfd_get_filename (abfd));
6313 }
6314 }
6315
6316 if (abbrev->has_children)
dee91e82 6317 return skip_children (reader, info_ptr);
4bb7a0a7
DJ
6318 else
6319 return info_ptr;
6320}
6321
93311388 6322/* Locate ORIG_PDI's sibling.
dee91e82 6323 INFO_PTR should point to the start of the next DIE after ORIG_PDI. */
91c24f0a 6324
fe1b8b76 6325static gdb_byte *
dee91e82
DE
6326locate_pdi_sibling (const struct die_reader_specs *reader,
6327 struct partial_die_info *orig_pdi,
6328 gdb_byte *info_ptr)
91c24f0a
DC
6329{
6330 /* Do we know the sibling already? */
72bf9492 6331
91c24f0a
DC
6332 if (orig_pdi->sibling)
6333 return orig_pdi->sibling;
6334
6335 /* Are there any children to deal with? */
6336
6337 if (!orig_pdi->has_children)
6338 return info_ptr;
6339
4bb7a0a7 6340 /* Skip the children the long way. */
91c24f0a 6341
dee91e82 6342 return skip_children (reader, info_ptr);
91c24f0a
DC
6343}
6344
c906108c
SS
6345/* Expand this partial symbol table into a full symbol table. */
6346
6347static void
fba45db2 6348dwarf2_psymtab_to_symtab (struct partial_symtab *pst)
c906108c 6349{
c906108c
SS
6350 if (pst != NULL)
6351 {
6352 if (pst->readin)
6353 {
3e43a32a
MS
6354 warning (_("bug: psymtab for %s is already read in."),
6355 pst->filename);
c906108c
SS
6356 }
6357 else
6358 {
6359 if (info_verbose)
6360 {
3e43a32a
MS
6361 printf_filtered (_("Reading in symbols for %s..."),
6362 pst->filename);
c906108c
SS
6363 gdb_flush (gdb_stdout);
6364 }
6365
10b3939b
DJ
6366 /* Restore our global data. */
6367 dwarf2_per_objfile = objfile_data (pst->objfile,
6368 dwarf2_objfile_data_key);
6369
b2ab525c
KB
6370 /* If this psymtab is constructed from a debug-only objfile, the
6371 has_section_at_zero flag will not necessarily be correct. We
6372 can get the correct value for this flag by looking at the data
6373 associated with the (presumably stripped) associated objfile. */
6374 if (pst->objfile->separate_debug_objfile_backlink)
6375 {
6376 struct dwarf2_per_objfile *dpo_backlink
6377 = objfile_data (pst->objfile->separate_debug_objfile_backlink,
6378 dwarf2_objfile_data_key);
9a619af0 6379
b2ab525c
KB
6380 dwarf2_per_objfile->has_section_at_zero
6381 = dpo_backlink->has_section_at_zero;
6382 }
6383
98bfdba5
PA
6384 dwarf2_per_objfile->reading_partial_symbols = 0;
6385
c906108c
SS
6386 psymtab_to_symtab_1 (pst);
6387
6388 /* Finish up the debug error message. */
6389 if (info_verbose)
a3f17187 6390 printf_filtered (_("done.\n"));
c906108c
SS
6391 }
6392 }
95554aad
TT
6393
6394 process_cu_includes ();
c906108c 6395}
9cdd5dbd
DE
6396\f
6397/* Reading in full CUs. */
c906108c 6398
10b3939b
DJ
6399/* Add PER_CU to the queue. */
6400
6401static void
95554aad
TT
6402queue_comp_unit (struct dwarf2_per_cu_data *per_cu,
6403 enum language pretend_language)
10b3939b
DJ
6404{
6405 struct dwarf2_queue_item *item;
6406
6407 per_cu->queued = 1;
6408 item = xmalloc (sizeof (*item));
6409 item->per_cu = per_cu;
95554aad 6410 item->pretend_language = pretend_language;
10b3939b
DJ
6411 item->next = NULL;
6412
6413 if (dwarf2_queue == NULL)
6414 dwarf2_queue = item;
6415 else
6416 dwarf2_queue_tail->next = item;
6417
6418 dwarf2_queue_tail = item;
6419}
6420
0907af0c
DE
6421/* THIS_CU has a reference to PER_CU. If necessary, load the new compilation
6422 unit and add it to our queue.
6423 The result is non-zero if PER_CU was queued, otherwise the result is zero
6424 meaning either PER_CU is already queued or it is already loaded. */
6425
6426static int
6427maybe_queue_comp_unit (struct dwarf2_cu *this_cu,
6428 struct dwarf2_per_cu_data *per_cu,
6429 enum language pretend_language)
6430{
6431 /* We may arrive here during partial symbol reading, if we need full
6432 DIEs to process an unusual case (e.g. template arguments). Do
6433 not queue PER_CU, just tell our caller to load its DIEs. */
6434 if (dwarf2_per_objfile->reading_partial_symbols)
6435 {
6436 if (per_cu->cu == NULL || per_cu->cu->dies == NULL)
6437 return 1;
6438 return 0;
6439 }
6440
6441 /* Mark the dependence relation so that we don't flush PER_CU
6442 too early. */
6443 dwarf2_add_dependence (this_cu, per_cu);
6444
6445 /* If it's already on the queue, we have nothing to do. */
6446 if (per_cu->queued)
6447 return 0;
6448
6449 /* If the compilation unit is already loaded, just mark it as
6450 used. */
6451 if (per_cu->cu != NULL)
6452 {
6453 per_cu->cu->last_used = 0;
6454 return 0;
6455 }
6456
6457 /* Add it to the queue. */
6458 queue_comp_unit (per_cu, pretend_language);
6459
6460 return 1;
6461}
6462
10b3939b
DJ
6463/* Process the queue. */
6464
6465static void
a0f42c21 6466process_queue (void)
10b3939b
DJ
6467{
6468 struct dwarf2_queue_item *item, *next_item;
6469
45cfd468
DE
6470 if (dwarf2_read_debug)
6471 {
6472 fprintf_unfiltered (gdb_stdlog,
6473 "Expanding one or more symtabs of objfile %s ...\n",
6474 dwarf2_per_objfile->objfile->name);
6475 }
6476
03dd20cc
DJ
6477 /* The queue starts out with one item, but following a DIE reference
6478 may load a new CU, adding it to the end of the queue. */
10b3939b
DJ
6479 for (item = dwarf2_queue; item != NULL; dwarf2_queue = item = next_item)
6480 {
9291a0cd
TT
6481 if (dwarf2_per_objfile->using_index
6482 ? !item->per_cu->v.quick->symtab
6483 : (item->per_cu->v.psymtab && !item->per_cu->v.psymtab->readin))
f4dc4d17
DE
6484 {
6485 struct dwarf2_per_cu_data *per_cu = item->per_cu;
6486
6487 if (dwarf2_read_debug)
6488 {
6489 fprintf_unfiltered (gdb_stdlog,
6490 "Expanding symtab of %s at offset 0x%x\n",
6491 per_cu->is_debug_types ? "TU" : "CU",
6492 per_cu->offset.sect_off);
6493 }
6494
6495 if (per_cu->is_debug_types)
6496 process_full_type_unit (per_cu, item->pretend_language);
6497 else
6498 process_full_comp_unit (per_cu, item->pretend_language);
6499
6500 if (dwarf2_read_debug)
6501 {
6502 fprintf_unfiltered (gdb_stdlog,
6503 "Done expanding %s at offset 0x%x\n",
6504 per_cu->is_debug_types ? "TU" : "CU",
6505 per_cu->offset.sect_off);
6506 }
6507 }
10b3939b
DJ
6508
6509 item->per_cu->queued = 0;
6510 next_item = item->next;
6511 xfree (item);
6512 }
6513
6514 dwarf2_queue_tail = NULL;
45cfd468
DE
6515
6516 if (dwarf2_read_debug)
6517 {
6518 fprintf_unfiltered (gdb_stdlog, "Done expanding symtabs of %s.\n",
6519 dwarf2_per_objfile->objfile->name);
6520 }
10b3939b
DJ
6521}
6522
6523/* Free all allocated queue entries. This function only releases anything if
6524 an error was thrown; if the queue was processed then it would have been
6525 freed as we went along. */
6526
6527static void
6528dwarf2_release_queue (void *dummy)
6529{
6530 struct dwarf2_queue_item *item, *last;
6531
6532 item = dwarf2_queue;
6533 while (item)
6534 {
6535 /* Anything still marked queued is likely to be in an
6536 inconsistent state, so discard it. */
6537 if (item->per_cu->queued)
6538 {
6539 if (item->per_cu->cu != NULL)
dee91e82 6540 free_one_cached_comp_unit (item->per_cu);
10b3939b
DJ
6541 item->per_cu->queued = 0;
6542 }
6543
6544 last = item;
6545 item = item->next;
6546 xfree (last);
6547 }
6548
6549 dwarf2_queue = dwarf2_queue_tail = NULL;
6550}
6551
6552/* Read in full symbols for PST, and anything it depends on. */
6553
c906108c 6554static void
fba45db2 6555psymtab_to_symtab_1 (struct partial_symtab *pst)
c906108c 6556{
10b3939b 6557 struct dwarf2_per_cu_data *per_cu;
aaa75496
JB
6558 int i;
6559
95554aad
TT
6560 if (pst->readin)
6561 return;
6562
aaa75496 6563 for (i = 0; i < pst->number_of_dependencies; i++)
95554aad
TT
6564 if (!pst->dependencies[i]->readin
6565 && pst->dependencies[i]->user == NULL)
aaa75496
JB
6566 {
6567 /* Inform about additional files that need to be read in. */
6568 if (info_verbose)
6569 {
a3f17187 6570 /* FIXME: i18n: Need to make this a single string. */
aaa75496
JB
6571 fputs_filtered (" ", gdb_stdout);
6572 wrap_here ("");
6573 fputs_filtered ("and ", gdb_stdout);
6574 wrap_here ("");
6575 printf_filtered ("%s...", pst->dependencies[i]->filename);
0963b4bd 6576 wrap_here (""); /* Flush output. */
aaa75496
JB
6577 gdb_flush (gdb_stdout);
6578 }
6579 psymtab_to_symtab_1 (pst->dependencies[i]);
6580 }
6581
e38df1d0 6582 per_cu = pst->read_symtab_private;
10b3939b
DJ
6583
6584 if (per_cu == NULL)
aaa75496
JB
6585 {
6586 /* It's an include file, no symbols to read for it.
6587 Everything is in the parent symtab. */
6588 pst->readin = 1;
6589 return;
6590 }
c906108c 6591
a0f42c21 6592 dw2_do_instantiate_symtab (per_cu);
10b3939b
DJ
6593}
6594
dee91e82
DE
6595/* Trivial hash function for die_info: the hash value of a DIE
6596 is its offset in .debug_info for this objfile. */
10b3939b 6597
dee91e82
DE
6598static hashval_t
6599die_hash (const void *item)
10b3939b 6600{
dee91e82 6601 const struct die_info *die = item;
6502dd73 6602
dee91e82
DE
6603 return die->offset.sect_off;
6604}
63d06c5c 6605
dee91e82
DE
6606/* Trivial comparison function for die_info structures: two DIEs
6607 are equal if they have the same offset. */
98bfdba5 6608
dee91e82
DE
6609static int
6610die_eq (const void *item_lhs, const void *item_rhs)
6611{
6612 const struct die_info *die_lhs = item_lhs;
6613 const struct die_info *die_rhs = item_rhs;
c906108c 6614
dee91e82
DE
6615 return die_lhs->offset.sect_off == die_rhs->offset.sect_off;
6616}
c906108c 6617
dee91e82
DE
6618/* die_reader_func for load_full_comp_unit.
6619 This is identical to read_signatured_type_reader,
6620 but is kept separate for now. */
c906108c 6621
dee91e82
DE
6622static void
6623load_full_comp_unit_reader (const struct die_reader_specs *reader,
6624 gdb_byte *info_ptr,
6625 struct die_info *comp_unit_die,
6626 int has_children,
6627 void *data)
6628{
6629 struct dwarf2_cu *cu = reader->cu;
95554aad 6630 enum language *language_ptr = data;
6caca83c 6631
dee91e82
DE
6632 gdb_assert (cu->die_hash == NULL);
6633 cu->die_hash =
6634 htab_create_alloc_ex (cu->header.length / 12,
6635 die_hash,
6636 die_eq,
6637 NULL,
6638 &cu->comp_unit_obstack,
6639 hashtab_obstack_allocate,
6640 dummy_obstack_deallocate);
e142c38c 6641
dee91e82
DE
6642 if (has_children)
6643 comp_unit_die->child = read_die_and_siblings (reader, info_ptr,
6644 &info_ptr, comp_unit_die);
6645 cu->dies = comp_unit_die;
6646 /* comp_unit_die is not stored in die_hash, no need. */
10b3939b
DJ
6647
6648 /* We try not to read any attributes in this function, because not
9cdd5dbd 6649 all CUs needed for references have been loaded yet, and symbol
10b3939b 6650 table processing isn't initialized. But we have to set the CU language,
dee91e82
DE
6651 or we won't be able to build types correctly.
6652 Similarly, if we do not read the producer, we can not apply
6653 producer-specific interpretation. */
95554aad 6654 prepare_one_comp_unit (cu, cu->dies, *language_ptr);
dee91e82 6655}
10b3939b 6656
dee91e82 6657/* Load the DIEs associated with PER_CU into memory. */
a6c727b2 6658
dee91e82 6659static void
95554aad
TT
6660load_full_comp_unit (struct dwarf2_per_cu_data *this_cu,
6661 enum language pretend_language)
dee91e82 6662{
3019eac3 6663 gdb_assert (! this_cu->is_debug_types);
c5b7e1cb 6664
f4dc4d17
DE
6665 init_cutu_and_read_dies (this_cu, NULL, 1, 1,
6666 load_full_comp_unit_reader, &pretend_language);
10b3939b
DJ
6667}
6668
3da10d80
KS
6669/* Add a DIE to the delayed physname list. */
6670
6671static void
6672add_to_method_list (struct type *type, int fnfield_index, int index,
6673 const char *name, struct die_info *die,
6674 struct dwarf2_cu *cu)
6675{
6676 struct delayed_method_info mi;
6677 mi.type = type;
6678 mi.fnfield_index = fnfield_index;
6679 mi.index = index;
6680 mi.name = name;
6681 mi.die = die;
6682 VEC_safe_push (delayed_method_info, cu->method_list, &mi);
6683}
6684
6685/* A cleanup for freeing the delayed method list. */
6686
6687static void
6688free_delayed_list (void *ptr)
6689{
6690 struct dwarf2_cu *cu = (struct dwarf2_cu *) ptr;
6691 if (cu->method_list != NULL)
6692 {
6693 VEC_free (delayed_method_info, cu->method_list);
6694 cu->method_list = NULL;
6695 }
6696}
6697
6698/* Compute the physnames of any methods on the CU's method list.
6699
6700 The computation of method physnames is delayed in order to avoid the
6701 (bad) condition that one of the method's formal parameters is of an as yet
6702 incomplete type. */
6703
6704static void
6705compute_delayed_physnames (struct dwarf2_cu *cu)
6706{
6707 int i;
6708 struct delayed_method_info *mi;
6709 for (i = 0; VEC_iterate (delayed_method_info, cu->method_list, i, mi) ; ++i)
6710 {
1d06ead6 6711 const char *physname;
3da10d80
KS
6712 struct fn_fieldlist *fn_flp
6713 = &TYPE_FN_FIELDLIST (mi->type, mi->fnfield_index);
1d06ead6 6714 physname = dwarf2_physname ((char *) mi->name, mi->die, cu);
3da10d80
KS
6715 fn_flp->fn_fields[mi->index].physname = physname ? physname : "";
6716 }
6717}
6718
a766d390
DE
6719/* Go objects should be embedded in a DW_TAG_module DIE,
6720 and it's not clear if/how imported objects will appear.
6721 To keep Go support simple until that's worked out,
6722 go back through what we've read and create something usable.
6723 We could do this while processing each DIE, and feels kinda cleaner,
6724 but that way is more invasive.
6725 This is to, for example, allow the user to type "p var" or "b main"
6726 without having to specify the package name, and allow lookups
6727 of module.object to work in contexts that use the expression
6728 parser. */
6729
6730static void
6731fixup_go_packaging (struct dwarf2_cu *cu)
6732{
6733 char *package_name = NULL;
6734 struct pending *list;
6735 int i;
6736
6737 for (list = global_symbols; list != NULL; list = list->next)
6738 {
6739 for (i = 0; i < list->nsyms; ++i)
6740 {
6741 struct symbol *sym = list->symbol[i];
6742
6743 if (SYMBOL_LANGUAGE (sym) == language_go
6744 && SYMBOL_CLASS (sym) == LOC_BLOCK)
6745 {
6746 char *this_package_name = go_symbol_package_name (sym);
6747
6748 if (this_package_name == NULL)
6749 continue;
6750 if (package_name == NULL)
6751 package_name = this_package_name;
6752 else
6753 {
6754 if (strcmp (package_name, this_package_name) != 0)
6755 complaint (&symfile_complaints,
6756 _("Symtab %s has objects from two different Go packages: %s and %s"),
210bbc17
TT
6757 (SYMBOL_SYMTAB (sym)
6758 && SYMBOL_SYMTAB (sym)->filename
6759 ? SYMBOL_SYMTAB (sym)->filename
a766d390
DE
6760 : cu->objfile->name),
6761 this_package_name, package_name);
6762 xfree (this_package_name);
6763 }
6764 }
6765 }
6766 }
6767
6768 if (package_name != NULL)
6769 {
6770 struct objfile *objfile = cu->objfile;
6771 struct type *type = init_type (TYPE_CODE_MODULE, 0, 0,
6772 package_name, objfile);
6773 struct symbol *sym;
6774
6775 TYPE_TAG_NAME (type) = TYPE_NAME (type);
6776
6777 sym = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct symbol);
6778 SYMBOL_SET_LANGUAGE (sym, language_go);
6779 SYMBOL_SET_NAMES (sym, package_name, strlen (package_name), 1, objfile);
6780 /* This is not VAR_DOMAIN because we want a way to ensure a lookup of,
6781 e.g., "main" finds the "main" module and not C's main(). */
6782 SYMBOL_DOMAIN (sym) = STRUCT_DOMAIN;
6783 SYMBOL_CLASS (sym) = LOC_TYPEDEF;
6784 SYMBOL_TYPE (sym) = type;
6785
6786 add_symbol_to_list (sym, &global_symbols);
6787
6788 xfree (package_name);
6789 }
6790}
6791
95554aad
TT
6792static void compute_symtab_includes (struct dwarf2_per_cu_data *per_cu);
6793
6794/* Return the symtab for PER_CU. This works properly regardless of
6795 whether we're using the index or psymtabs. */
6796
6797static struct symtab *
6798get_symtab (struct dwarf2_per_cu_data *per_cu)
6799{
6800 return (dwarf2_per_objfile->using_index
6801 ? per_cu->v.quick->symtab
6802 : per_cu->v.psymtab->symtab);
6803}
6804
6805/* A helper function for computing the list of all symbol tables
6806 included by PER_CU. */
6807
6808static void
6809recursively_compute_inclusions (VEC (dwarf2_per_cu_ptr) **result,
6810 htab_t all_children,
6811 struct dwarf2_per_cu_data *per_cu)
6812{
6813 void **slot;
6814 int ix;
6815 struct dwarf2_per_cu_data *iter;
6816
6817 slot = htab_find_slot (all_children, per_cu, INSERT);
6818 if (*slot != NULL)
6819 {
6820 /* This inclusion and its children have been processed. */
6821 return;
6822 }
6823
6824 *slot = per_cu;
6825 /* Only add a CU if it has a symbol table. */
6826 if (get_symtab (per_cu) != NULL)
6827 VEC_safe_push (dwarf2_per_cu_ptr, *result, per_cu);
6828
6829 for (ix = 0;
f4dc4d17 6830 VEC_iterate (dwarf2_per_cu_ptr, per_cu->s.imported_symtabs, ix, iter);
95554aad
TT
6831 ++ix)
6832 recursively_compute_inclusions (result, all_children, iter);
6833}
6834
6835/* Compute the symtab 'includes' fields for the symtab related to
6836 PER_CU. */
6837
6838static void
6839compute_symtab_includes (struct dwarf2_per_cu_data *per_cu)
6840{
f4dc4d17
DE
6841 gdb_assert (! per_cu->is_debug_types);
6842
6843 if (!VEC_empty (dwarf2_per_cu_ptr, per_cu->s.imported_symtabs))
95554aad
TT
6844 {
6845 int ix, len;
6846 struct dwarf2_per_cu_data *iter;
6847 VEC (dwarf2_per_cu_ptr) *result_children = NULL;
6848 htab_t all_children;
6849 struct symtab *symtab = get_symtab (per_cu);
6850
6851 /* If we don't have a symtab, we can just skip this case. */
6852 if (symtab == NULL)
6853 return;
6854
6855 all_children = htab_create_alloc (1, htab_hash_pointer, htab_eq_pointer,
6856 NULL, xcalloc, xfree);
6857
6858 for (ix = 0;
f4dc4d17 6859 VEC_iterate (dwarf2_per_cu_ptr, per_cu->s.imported_symtabs,
95554aad
TT
6860 ix, iter);
6861 ++ix)
6862 recursively_compute_inclusions (&result_children, all_children, iter);
6863
6864 /* Now we have a transitive closure of all the included CUs, so
6865 we can convert it to a list of symtabs. */
6866 len = VEC_length (dwarf2_per_cu_ptr, result_children);
6867 symtab->includes
6868 = obstack_alloc (&dwarf2_per_objfile->objfile->objfile_obstack,
6869 (len + 1) * sizeof (struct symtab *));
6870 for (ix = 0;
6871 VEC_iterate (dwarf2_per_cu_ptr, result_children, ix, iter);
6872 ++ix)
6873 symtab->includes[ix] = get_symtab (iter);
6874 symtab->includes[len] = NULL;
6875
6876 VEC_free (dwarf2_per_cu_ptr, result_children);
6877 htab_delete (all_children);
6878 }
6879}
6880
6881/* Compute the 'includes' field for the symtabs of all the CUs we just
6882 read. */
6883
6884static void
6885process_cu_includes (void)
6886{
6887 int ix;
6888 struct dwarf2_per_cu_data *iter;
6889
6890 for (ix = 0;
6891 VEC_iterate (dwarf2_per_cu_ptr, dwarf2_per_objfile->just_read_cus,
6892 ix, iter);
6893 ++ix)
f4dc4d17
DE
6894 {
6895 if (! iter->is_debug_types)
6896 compute_symtab_includes (iter);
6897 }
95554aad
TT
6898
6899 VEC_free (dwarf2_per_cu_ptr, dwarf2_per_objfile->just_read_cus);
6900}
6901
9cdd5dbd 6902/* Generate full symbol information for PER_CU, whose DIEs have
10b3939b
DJ
6903 already been loaded into memory. */
6904
6905static void
95554aad
TT
6906process_full_comp_unit (struct dwarf2_per_cu_data *per_cu,
6907 enum language pretend_language)
10b3939b 6908{
10b3939b 6909 struct dwarf2_cu *cu = per_cu->cu;
9291a0cd 6910 struct objfile *objfile = per_cu->objfile;
10b3939b
DJ
6911 CORE_ADDR lowpc, highpc;
6912 struct symtab *symtab;
3da10d80 6913 struct cleanup *back_to, *delayed_list_cleanup;
10b3939b 6914 CORE_ADDR baseaddr;
4359dff1 6915 struct block *static_block;
10b3939b
DJ
6916
6917 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
6918
10b3939b
DJ
6919 buildsym_init ();
6920 back_to = make_cleanup (really_free_pendings, NULL);
3da10d80 6921 delayed_list_cleanup = make_cleanup (free_delayed_list, cu);
10b3939b
DJ
6922
6923 cu->list_in_scope = &file_symbols;
c906108c 6924
95554aad
TT
6925 cu->language = pretend_language;
6926 cu->language_defn = language_def (cu->language);
6927
c906108c 6928 /* Do line number decoding in read_file_scope () */
10b3939b 6929 process_die (cu->dies, cu);
c906108c 6930
a766d390
DE
6931 /* For now fudge the Go package. */
6932 if (cu->language == language_go)
6933 fixup_go_packaging (cu);
6934
3da10d80
KS
6935 /* Now that we have processed all the DIEs in the CU, all the types
6936 should be complete, and it should now be safe to compute all of the
6937 physnames. */
6938 compute_delayed_physnames (cu);
6939 do_cleanups (delayed_list_cleanup);
6940
fae299cd
DC
6941 /* Some compilers don't define a DW_AT_high_pc attribute for the
6942 compilation unit. If the DW_AT_high_pc is missing, synthesize
6943 it, by scanning the DIE's below the compilation unit. */
10b3939b 6944 get_scope_pc_bounds (cu->dies, &lowpc, &highpc, cu);
c906108c 6945
36586728
TT
6946 static_block
6947 = end_symtab_get_static_block (highpc + baseaddr, objfile, 0,
6948 per_cu->s.imported_symtabs != NULL);
4359dff1
JK
6949
6950 /* If the comp unit has DW_AT_ranges, it may have discontiguous ranges.
6951 Also, DW_AT_ranges may record ranges not belonging to any child DIEs
6952 (such as virtual method tables). Record the ranges in STATIC_BLOCK's
6953 addrmap to help ensure it has an accurate map of pc values belonging to
6954 this comp unit. */
6955 dwarf2_record_block_ranges (cu->dies, static_block, baseaddr, cu);
6956
6957 symtab = end_symtab_from_static_block (static_block, objfile,
6958 SECT_OFF_TEXT (objfile), 0);
c906108c 6959
8be455d7 6960 if (symtab != NULL)
c906108c 6961 {
df15bd07 6962 int gcc_4_minor = producer_is_gcc_ge_4 (cu->producer);
4632c0d0 6963
8be455d7
JK
6964 /* Set symtab language to language from DW_AT_language. If the
6965 compilation is from a C file generated by language preprocessors, do
6966 not set the language if it was already deduced by start_subfile. */
6967 if (!(cu->language == language_c && symtab->language != language_c))
6968 symtab->language = cu->language;
6969
6970 /* GCC-4.0 has started to support -fvar-tracking. GCC-3.x still can
6971 produce DW_AT_location with location lists but it can be possibly
ab260dad
JK
6972 invalid without -fvar-tracking. Still up to GCC-4.4.x incl. 4.4.0
6973 there were bugs in prologue debug info, fixed later in GCC-4.5
6974 by "unwind info for epilogues" patch (which is not directly related).
8be455d7
JK
6975
6976 For -gdwarf-4 type units LOCATIONS_VALID indication is fortunately not
6977 needed, it would be wrong due to missing DW_AT_producer there.
6978
6979 Still one can confuse GDB by using non-standard GCC compilation
6980 options - this waits on GCC PR other/32998 (-frecord-gcc-switches).
6981 */
ab260dad 6982 if (cu->has_loclist && gcc_4_minor >= 5)
8be455d7 6983 symtab->locations_valid = 1;
e0d00bc7
JK
6984
6985 if (gcc_4_minor >= 5)
6986 symtab->epilogue_unwind_valid = 1;
96408a79
SA
6987
6988 symtab->call_site_htab = cu->call_site_htab;
c906108c 6989 }
9291a0cd
TT
6990
6991 if (dwarf2_per_objfile->using_index)
6992 per_cu->v.quick->symtab = symtab;
6993 else
6994 {
6995 struct partial_symtab *pst = per_cu->v.psymtab;
6996 pst->symtab = symtab;
6997 pst->readin = 1;
6998 }
c906108c 6999
95554aad
TT
7000 /* Push it for inclusion processing later. */
7001 VEC_safe_push (dwarf2_per_cu_ptr, dwarf2_per_objfile->just_read_cus, per_cu);
7002
c906108c 7003 do_cleanups (back_to);
f4dc4d17 7004}
45cfd468 7005
f4dc4d17
DE
7006/* Generate full symbol information for type unit PER_CU, whose DIEs have
7007 already been loaded into memory. */
7008
7009static void
7010process_full_type_unit (struct dwarf2_per_cu_data *per_cu,
7011 enum language pretend_language)
7012{
7013 struct dwarf2_cu *cu = per_cu->cu;
7014 struct objfile *objfile = per_cu->objfile;
7015 struct symtab *symtab;
7016 struct cleanup *back_to, *delayed_list_cleanup;
7017
7018 buildsym_init ();
7019 back_to = make_cleanup (really_free_pendings, NULL);
7020 delayed_list_cleanup = make_cleanup (free_delayed_list, cu);
7021
7022 cu->list_in_scope = &file_symbols;
7023
7024 cu->language = pretend_language;
7025 cu->language_defn = language_def (cu->language);
7026
7027 /* The symbol tables are set up in read_type_unit_scope. */
7028 process_die (cu->dies, cu);
7029
7030 /* For now fudge the Go package. */
7031 if (cu->language == language_go)
7032 fixup_go_packaging (cu);
7033
7034 /* Now that we have processed all the DIEs in the CU, all the types
7035 should be complete, and it should now be safe to compute all of the
7036 physnames. */
7037 compute_delayed_physnames (cu);
7038 do_cleanups (delayed_list_cleanup);
7039
7040 /* TUs share symbol tables.
7041 If this is the first TU to use this symtab, complete the construction
094b34ac
DE
7042 of it with end_expandable_symtab. Otherwise, complete the addition of
7043 this TU's symbols to the existing symtab. */
f4dc4d17 7044 if (per_cu->s.type_unit_group->primary_symtab == NULL)
45cfd468 7045 {
f4dc4d17
DE
7046 symtab = end_expandable_symtab (0, objfile, SECT_OFF_TEXT (objfile));
7047 per_cu->s.type_unit_group->primary_symtab = symtab;
7048
7049 if (symtab != NULL)
7050 {
7051 /* Set symtab language to language from DW_AT_language. If the
7052 compilation is from a C file generated by language preprocessors,
7053 do not set the language if it was already deduced by
7054 start_subfile. */
7055 if (!(cu->language == language_c && symtab->language != language_c))
7056 symtab->language = cu->language;
7057 }
7058 }
7059 else
7060 {
7061 augment_type_symtab (objfile,
7062 per_cu->s.type_unit_group->primary_symtab);
7063 symtab = per_cu->s.type_unit_group->primary_symtab;
7064 }
7065
7066 if (dwarf2_per_objfile->using_index)
7067 per_cu->v.quick->symtab = symtab;
7068 else
7069 {
7070 struct partial_symtab *pst = per_cu->v.psymtab;
7071 pst->symtab = symtab;
7072 pst->readin = 1;
45cfd468 7073 }
f4dc4d17
DE
7074
7075 do_cleanups (back_to);
c906108c
SS
7076}
7077
95554aad
TT
7078/* Process an imported unit DIE. */
7079
7080static void
7081process_imported_unit_die (struct die_info *die, struct dwarf2_cu *cu)
7082{
7083 struct attribute *attr;
7084
f4dc4d17
DE
7085 /* For now we don't handle imported units in type units. */
7086 if (cu->per_cu->is_debug_types)
7087 {
7088 error (_("Dwarf Error: DW_TAG_imported_unit is not"
7089 " supported in type units [in module %s]"),
7090 cu->objfile->name);
7091 }
7092
95554aad
TT
7093 attr = dwarf2_attr (die, DW_AT_import, cu);
7094 if (attr != NULL)
7095 {
7096 struct dwarf2_per_cu_data *per_cu;
7097 struct symtab *imported_symtab;
7098 sect_offset offset;
36586728 7099 int is_dwz;
95554aad
TT
7100
7101 offset = dwarf2_get_ref_die_offset (attr);
36586728
TT
7102 is_dwz = (attr->form == DW_FORM_GNU_ref_alt || cu->per_cu->is_dwz);
7103 per_cu = dwarf2_find_containing_comp_unit (offset, is_dwz, cu->objfile);
95554aad
TT
7104
7105 /* Queue the unit, if needed. */
7106 if (maybe_queue_comp_unit (cu, per_cu, cu->language))
7107 load_full_comp_unit (per_cu, cu->language);
7108
f4dc4d17 7109 VEC_safe_push (dwarf2_per_cu_ptr, cu->per_cu->s.imported_symtabs,
95554aad
TT
7110 per_cu);
7111 }
7112}
7113
c906108c
SS
7114/* Process a die and its children. */
7115
7116static void
e7c27a73 7117process_die (struct die_info *die, struct dwarf2_cu *cu)
c906108c
SS
7118{
7119 switch (die->tag)
7120 {
7121 case DW_TAG_padding:
7122 break;
7123 case DW_TAG_compile_unit:
95554aad 7124 case DW_TAG_partial_unit:
e7c27a73 7125 read_file_scope (die, cu);
c906108c 7126 break;
348e048f
DE
7127 case DW_TAG_type_unit:
7128 read_type_unit_scope (die, cu);
7129 break;
c906108c 7130 case DW_TAG_subprogram:
c906108c 7131 case DW_TAG_inlined_subroutine:
edb3359d 7132 read_func_scope (die, cu);
c906108c
SS
7133 break;
7134 case DW_TAG_lexical_block:
14898363
L
7135 case DW_TAG_try_block:
7136 case DW_TAG_catch_block:
e7c27a73 7137 read_lexical_block_scope (die, cu);
c906108c 7138 break;
96408a79
SA
7139 case DW_TAG_GNU_call_site:
7140 read_call_site_scope (die, cu);
7141 break;
c906108c 7142 case DW_TAG_class_type:
680b30c7 7143 case DW_TAG_interface_type:
c906108c
SS
7144 case DW_TAG_structure_type:
7145 case DW_TAG_union_type:
134d01f1 7146 process_structure_scope (die, cu);
c906108c
SS
7147 break;
7148 case DW_TAG_enumeration_type:
134d01f1 7149 process_enumeration_scope (die, cu);
c906108c 7150 break;
134d01f1 7151
f792889a
DJ
7152 /* These dies have a type, but processing them does not create
7153 a symbol or recurse to process the children. Therefore we can
7154 read them on-demand through read_type_die. */
c906108c 7155 case DW_TAG_subroutine_type:
72019c9c 7156 case DW_TAG_set_type:
c906108c 7157 case DW_TAG_array_type:
c906108c 7158 case DW_TAG_pointer_type:
c906108c 7159 case DW_TAG_ptr_to_member_type:
c906108c 7160 case DW_TAG_reference_type:
c906108c 7161 case DW_TAG_string_type:
c906108c 7162 break;
134d01f1 7163
c906108c 7164 case DW_TAG_base_type:
a02abb62 7165 case DW_TAG_subrange_type:
cb249c71 7166 case DW_TAG_typedef:
134d01f1
DJ
7167 /* Add a typedef symbol for the type definition, if it has a
7168 DW_AT_name. */
f792889a 7169 new_symbol (die, read_type_die (die, cu), cu);
a02abb62 7170 break;
c906108c 7171 case DW_TAG_common_block:
e7c27a73 7172 read_common_block (die, cu);
c906108c
SS
7173 break;
7174 case DW_TAG_common_inclusion:
7175 break;
d9fa45fe 7176 case DW_TAG_namespace:
63d06c5c 7177 processing_has_namespace_info = 1;
e7c27a73 7178 read_namespace (die, cu);
d9fa45fe 7179 break;
5d7cb8df 7180 case DW_TAG_module:
f55ee35c 7181 processing_has_namespace_info = 1;
5d7cb8df
JK
7182 read_module (die, cu);
7183 break;
d9fa45fe
DC
7184 case DW_TAG_imported_declaration:
7185 case DW_TAG_imported_module:
63d06c5c 7186 processing_has_namespace_info = 1;
27aa8d6a
SW
7187 if (die->child != NULL && (die->tag == DW_TAG_imported_declaration
7188 || cu->language != language_fortran))
7189 complaint (&symfile_complaints, _("Tag '%s' has unexpected children"),
7190 dwarf_tag_name (die->tag));
7191 read_import_statement (die, cu);
d9fa45fe 7192 break;
95554aad
TT
7193
7194 case DW_TAG_imported_unit:
7195 process_imported_unit_die (die, cu);
7196 break;
7197
c906108c 7198 default:
e7c27a73 7199 new_symbol (die, NULL, cu);
c906108c
SS
7200 break;
7201 }
7202}
7203
94af9270
KS
7204/* A helper function for dwarf2_compute_name which determines whether DIE
7205 needs to have the name of the scope prepended to the name listed in the
7206 die. */
7207
7208static int
7209die_needs_namespace (struct die_info *die, struct dwarf2_cu *cu)
7210{
1c809c68
TT
7211 struct attribute *attr;
7212
94af9270
KS
7213 switch (die->tag)
7214 {
7215 case DW_TAG_namespace:
7216 case DW_TAG_typedef:
7217 case DW_TAG_class_type:
7218 case DW_TAG_interface_type:
7219 case DW_TAG_structure_type:
7220 case DW_TAG_union_type:
7221 case DW_TAG_enumeration_type:
7222 case DW_TAG_enumerator:
7223 case DW_TAG_subprogram:
7224 case DW_TAG_member:
7225 return 1;
7226
7227 case DW_TAG_variable:
c2b0a229 7228 case DW_TAG_constant:
94af9270
KS
7229 /* We only need to prefix "globally" visible variables. These include
7230 any variable marked with DW_AT_external or any variable that
7231 lives in a namespace. [Variables in anonymous namespaces
7232 require prefixing, but they are not DW_AT_external.] */
7233
7234 if (dwarf2_attr (die, DW_AT_specification, cu))
7235 {
7236 struct dwarf2_cu *spec_cu = cu;
9a619af0 7237
94af9270
KS
7238 return die_needs_namespace (die_specification (die, &spec_cu),
7239 spec_cu);
7240 }
7241
1c809c68 7242 attr = dwarf2_attr (die, DW_AT_external, cu);
f55ee35c
JK
7243 if (attr == NULL && die->parent->tag != DW_TAG_namespace
7244 && die->parent->tag != DW_TAG_module)
1c809c68
TT
7245 return 0;
7246 /* A variable in a lexical block of some kind does not need a
7247 namespace, even though in C++ such variables may be external
7248 and have a mangled name. */
7249 if (die->parent->tag == DW_TAG_lexical_block
7250 || die->parent->tag == DW_TAG_try_block
1054b214
TT
7251 || die->parent->tag == DW_TAG_catch_block
7252 || die->parent->tag == DW_TAG_subprogram)
1c809c68
TT
7253 return 0;
7254 return 1;
94af9270
KS
7255
7256 default:
7257 return 0;
7258 }
7259}
7260
98bfdba5
PA
7261/* Retrieve the last character from a mem_file. */
7262
7263static void
7264do_ui_file_peek_last (void *object, const char *buffer, long length)
7265{
7266 char *last_char_p = (char *) object;
7267
7268 if (length > 0)
7269 *last_char_p = buffer[length - 1];
7270}
7271
94af9270 7272/* Compute the fully qualified name of DIE in CU. If PHYSNAME is nonzero,
a766d390
DE
7273 compute the physname for the object, which include a method's:
7274 - formal parameters (C++/Java),
7275 - receiver type (Go),
7276 - return type (Java).
7277
7278 The term "physname" is a bit confusing.
7279 For C++, for example, it is the demangled name.
7280 For Go, for example, it's the mangled name.
94af9270 7281
af6b7be1
JB
7282 For Ada, return the DIE's linkage name rather than the fully qualified
7283 name. PHYSNAME is ignored..
7284
94af9270
KS
7285 The result is allocated on the objfile_obstack and canonicalized. */
7286
7287static const char *
7288dwarf2_compute_name (char *name, struct die_info *die, struct dwarf2_cu *cu,
7289 int physname)
7290{
bb5ed363
DE
7291 struct objfile *objfile = cu->objfile;
7292
94af9270
KS
7293 if (name == NULL)
7294 name = dwarf2_name (die, cu);
7295
f55ee35c
JK
7296 /* For Fortran GDB prefers DW_AT_*linkage_name if present but otherwise
7297 compute it by typename_concat inside GDB. */
7298 if (cu->language == language_ada
7299 || (cu->language == language_fortran && physname))
7300 {
7301 /* For Ada unit, we prefer the linkage name over the name, as
7302 the former contains the exported name, which the user expects
7303 to be able to reference. Ideally, we want the user to be able
7304 to reference this entity using either natural or linkage name,
7305 but we haven't started looking at this enhancement yet. */
7306 struct attribute *attr;
7307
7308 attr = dwarf2_attr (die, DW_AT_linkage_name, cu);
7309 if (attr == NULL)
7310 attr = dwarf2_attr (die, DW_AT_MIPS_linkage_name, cu);
7311 if (attr && DW_STRING (attr))
7312 return DW_STRING (attr);
7313 }
7314
94af9270
KS
7315 /* These are the only languages we know how to qualify names in. */
7316 if (name != NULL
f55ee35c
JK
7317 && (cu->language == language_cplus || cu->language == language_java
7318 || cu->language == language_fortran))
94af9270
KS
7319 {
7320 if (die_needs_namespace (die, cu))
7321 {
7322 long length;
0d5cff50 7323 const char *prefix;
94af9270
KS
7324 struct ui_file *buf;
7325
7326 prefix = determine_prefix (die, cu);
7327 buf = mem_fileopen ();
7328 if (*prefix != '\0')
7329 {
f55ee35c
JK
7330 char *prefixed_name = typename_concat (NULL, prefix, name,
7331 physname, cu);
9a619af0 7332
94af9270
KS
7333 fputs_unfiltered (prefixed_name, buf);
7334 xfree (prefixed_name);
7335 }
7336 else
62d5b8da 7337 fputs_unfiltered (name, buf);
94af9270 7338
98bfdba5
PA
7339 /* Template parameters may be specified in the DIE's DW_AT_name, or
7340 as children with DW_TAG_template_type_param or
7341 DW_TAG_value_type_param. If the latter, add them to the name
7342 here. If the name already has template parameters, then
7343 skip this step; some versions of GCC emit both, and
7344 it is more efficient to use the pre-computed name.
7345
7346 Something to keep in mind about this process: it is very
7347 unlikely, or in some cases downright impossible, to produce
7348 something that will match the mangled name of a function.
7349 If the definition of the function has the same debug info,
7350 we should be able to match up with it anyway. But fallbacks
7351 using the minimal symbol, for instance to find a method
7352 implemented in a stripped copy of libstdc++, will not work.
7353 If we do not have debug info for the definition, we will have to
7354 match them up some other way.
7355
7356 When we do name matching there is a related problem with function
7357 templates; two instantiated function templates are allowed to
7358 differ only by their return types, which we do not add here. */
7359
7360 if (cu->language == language_cplus && strchr (name, '<') == NULL)
7361 {
7362 struct attribute *attr;
7363 struct die_info *child;
7364 int first = 1;
7365
7366 die->building_fullname = 1;
7367
7368 for (child = die->child; child != NULL; child = child->sibling)
7369 {
7370 struct type *type;
12df843f 7371 LONGEST value;
98bfdba5
PA
7372 gdb_byte *bytes;
7373 struct dwarf2_locexpr_baton *baton;
7374 struct value *v;
7375
7376 if (child->tag != DW_TAG_template_type_param
7377 && child->tag != DW_TAG_template_value_param)
7378 continue;
7379
7380 if (first)
7381 {
7382 fputs_unfiltered ("<", buf);
7383 first = 0;
7384 }
7385 else
7386 fputs_unfiltered (", ", buf);
7387
7388 attr = dwarf2_attr (child, DW_AT_type, cu);
7389 if (attr == NULL)
7390 {
7391 complaint (&symfile_complaints,
7392 _("template parameter missing DW_AT_type"));
7393 fputs_unfiltered ("UNKNOWN_TYPE", buf);
7394 continue;
7395 }
7396 type = die_type (child, cu);
7397
7398 if (child->tag == DW_TAG_template_type_param)
7399 {
79d43c61 7400 c_print_type (type, "", buf, -1, 0, &type_print_raw_options);
98bfdba5
PA
7401 continue;
7402 }
7403
7404 attr = dwarf2_attr (child, DW_AT_const_value, cu);
7405 if (attr == NULL)
7406 {
7407 complaint (&symfile_complaints,
3e43a32a
MS
7408 _("template parameter missing "
7409 "DW_AT_const_value"));
98bfdba5
PA
7410 fputs_unfiltered ("UNKNOWN_VALUE", buf);
7411 continue;
7412 }
7413
7414 dwarf2_const_value_attr (attr, type, name,
7415 &cu->comp_unit_obstack, cu,
7416 &value, &bytes, &baton);
7417
7418 if (TYPE_NOSIGN (type))
7419 /* GDB prints characters as NUMBER 'CHAR'. If that's
7420 changed, this can use value_print instead. */
7421 c_printchar (value, type, buf);
7422 else
7423 {
7424 struct value_print_options opts;
7425
7426 if (baton != NULL)
7427 v = dwarf2_evaluate_loc_desc (type, NULL,
7428 baton->data,
7429 baton->size,
7430 baton->per_cu);
7431 else if (bytes != NULL)
7432 {
7433 v = allocate_value (type);
7434 memcpy (value_contents_writeable (v), bytes,
7435 TYPE_LENGTH (type));
7436 }
7437 else
7438 v = value_from_longest (type, value);
7439
3e43a32a
MS
7440 /* Specify decimal so that we do not depend on
7441 the radix. */
98bfdba5
PA
7442 get_formatted_print_options (&opts, 'd');
7443 opts.raw = 1;
7444 value_print (v, buf, &opts);
7445 release_value (v);
7446 value_free (v);
7447 }
7448 }
7449
7450 die->building_fullname = 0;
7451
7452 if (!first)
7453 {
7454 /* Close the argument list, with a space if necessary
7455 (nested templates). */
7456 char last_char = '\0';
7457 ui_file_put (buf, do_ui_file_peek_last, &last_char);
7458 if (last_char == '>')
7459 fputs_unfiltered (" >", buf);
7460 else
7461 fputs_unfiltered (">", buf);
7462 }
7463 }
7464
94af9270
KS
7465 /* For Java and C++ methods, append formal parameter type
7466 information, if PHYSNAME. */
6e70227d 7467
94af9270
KS
7468 if (physname && die->tag == DW_TAG_subprogram
7469 && (cu->language == language_cplus
7470 || cu->language == language_java))
7471 {
7472 struct type *type = read_type_die (die, cu);
7473
79d43c61
TT
7474 c_type_print_args (type, buf, 1, cu->language,
7475 &type_print_raw_options);
94af9270
KS
7476
7477 if (cu->language == language_java)
7478 {
7479 /* For java, we must append the return type to method
0963b4bd 7480 names. */
94af9270
KS
7481 if (die->tag == DW_TAG_subprogram)
7482 java_print_type (TYPE_TARGET_TYPE (type), "", buf,
79d43c61 7483 0, 0, &type_print_raw_options);
94af9270
KS
7484 }
7485 else if (cu->language == language_cplus)
7486 {
60430eff
DJ
7487 /* Assume that an artificial first parameter is
7488 "this", but do not crash if it is not. RealView
7489 marks unnamed (and thus unused) parameters as
7490 artificial; there is no way to differentiate
7491 the two cases. */
94af9270
KS
7492 if (TYPE_NFIELDS (type) > 0
7493 && TYPE_FIELD_ARTIFICIAL (type, 0)
60430eff 7494 && TYPE_CODE (TYPE_FIELD_TYPE (type, 0)) == TYPE_CODE_PTR
3e43a32a
MS
7495 && TYPE_CONST (TYPE_TARGET_TYPE (TYPE_FIELD_TYPE (type,
7496 0))))
94af9270
KS
7497 fputs_unfiltered (" const", buf);
7498 }
7499 }
7500
bb5ed363 7501 name = ui_file_obsavestring (buf, &objfile->objfile_obstack,
94af9270
KS
7502 &length);
7503 ui_file_delete (buf);
7504
7505 if (cu->language == language_cplus)
7506 {
7507 char *cname
7508 = dwarf2_canonicalize_name (name, cu,
bb5ed363 7509 &objfile->objfile_obstack);
9a619af0 7510
94af9270
KS
7511 if (cname != NULL)
7512 name = cname;
7513 }
7514 }
7515 }
7516
7517 return name;
7518}
7519
0114d602
DJ
7520/* Return the fully qualified name of DIE, based on its DW_AT_name.
7521 If scope qualifiers are appropriate they will be added. The result
7522 will be allocated on the objfile_obstack, or NULL if the DIE does
94af9270
KS
7523 not have a name. NAME may either be from a previous call to
7524 dwarf2_name or NULL.
7525
0963b4bd 7526 The output string will be canonicalized (if C++/Java). */
0114d602
DJ
7527
7528static const char *
94af9270 7529dwarf2_full_name (char *name, struct die_info *die, struct dwarf2_cu *cu)
0114d602 7530{
94af9270
KS
7531 return dwarf2_compute_name (name, die, cu, 0);
7532}
0114d602 7533
94af9270
KS
7534/* Construct a physname for the given DIE in CU. NAME may either be
7535 from a previous call to dwarf2_name or NULL. The result will be
7536 allocated on the objfile_objstack or NULL if the DIE does not have a
7537 name.
0114d602 7538
94af9270 7539 The output string will be canonicalized (if C++/Java). */
0114d602 7540
94af9270
KS
7541static const char *
7542dwarf2_physname (char *name, struct die_info *die, struct dwarf2_cu *cu)
7543{
bb5ed363 7544 struct objfile *objfile = cu->objfile;
900e11f9
JK
7545 struct attribute *attr;
7546 const char *retval, *mangled = NULL, *canon = NULL;
7547 struct cleanup *back_to;
7548 int need_copy = 1;
7549
7550 /* In this case dwarf2_compute_name is just a shortcut not building anything
7551 on its own. */
7552 if (!die_needs_namespace (die, cu))
7553 return dwarf2_compute_name (name, die, cu, 1);
7554
7555 back_to = make_cleanup (null_cleanup, NULL);
7556
7557 attr = dwarf2_attr (die, DW_AT_linkage_name, cu);
7558 if (!attr)
7559 attr = dwarf2_attr (die, DW_AT_MIPS_linkage_name, cu);
7560
7561 /* DW_AT_linkage_name is missing in some cases - depend on what GDB
7562 has computed. */
7563 if (attr && DW_STRING (attr))
7564 {
7565 char *demangled;
7566
7567 mangled = DW_STRING (attr);
7568
7569 /* Use DMGL_RET_DROP for C++ template functions to suppress their return
7570 type. It is easier for GDB users to search for such functions as
7571 `name(params)' than `long name(params)'. In such case the minimal
7572 symbol names do not match the full symbol names but for template
7573 functions there is never a need to look up their definition from their
7574 declaration so the only disadvantage remains the minimal symbol
7575 variant `long name(params)' does not have the proper inferior type.
7576 */
7577
a766d390
DE
7578 if (cu->language == language_go)
7579 {
7580 /* This is a lie, but we already lie to the caller new_symbol_full.
7581 new_symbol_full assumes we return the mangled name.
7582 This just undoes that lie until things are cleaned up. */
7583 demangled = NULL;
7584 }
7585 else
7586 {
7587 demangled = cplus_demangle (mangled,
7588 (DMGL_PARAMS | DMGL_ANSI
7589 | (cu->language == language_java
7590 ? DMGL_JAVA | DMGL_RET_POSTFIX
7591 : DMGL_RET_DROP)));
7592 }
900e11f9
JK
7593 if (demangled)
7594 {
7595 make_cleanup (xfree, demangled);
7596 canon = demangled;
7597 }
7598 else
7599 {
7600 canon = mangled;
7601 need_copy = 0;
7602 }
7603 }
7604
7605 if (canon == NULL || check_physname)
7606 {
7607 const char *physname = dwarf2_compute_name (name, die, cu, 1);
7608
7609 if (canon != NULL && strcmp (physname, canon) != 0)
7610 {
7611 /* It may not mean a bug in GDB. The compiler could also
7612 compute DW_AT_linkage_name incorrectly. But in such case
7613 GDB would need to be bug-to-bug compatible. */
7614
7615 complaint (&symfile_complaints,
7616 _("Computed physname <%s> does not match demangled <%s> "
7617 "(from linkage <%s>) - DIE at 0x%x [in module %s]"),
b64f50a1 7618 physname, canon, mangled, die->offset.sect_off, objfile->name);
900e11f9
JK
7619
7620 /* Prefer DW_AT_linkage_name (in the CANON form) - when it
7621 is available here - over computed PHYSNAME. It is safer
7622 against both buggy GDB and buggy compilers. */
7623
7624 retval = canon;
7625 }
7626 else
7627 {
7628 retval = physname;
7629 need_copy = 0;
7630 }
7631 }
7632 else
7633 retval = canon;
7634
7635 if (need_copy)
7636 retval = obsavestring (retval, strlen (retval),
bb5ed363 7637 &objfile->objfile_obstack);
900e11f9
JK
7638
7639 do_cleanups (back_to);
7640 return retval;
0114d602
DJ
7641}
7642
27aa8d6a
SW
7643/* Read the import statement specified by the given die and record it. */
7644
7645static void
7646read_import_statement (struct die_info *die, struct dwarf2_cu *cu)
7647{
bb5ed363 7648 struct objfile *objfile = cu->objfile;
27aa8d6a 7649 struct attribute *import_attr;
32019081 7650 struct die_info *imported_die, *child_die;
de4affc9 7651 struct dwarf2_cu *imported_cu;
27aa8d6a 7652 const char *imported_name;
794684b6 7653 const char *imported_name_prefix;
13387711
SW
7654 const char *canonical_name;
7655 const char *import_alias;
7656 const char *imported_declaration = NULL;
794684b6 7657 const char *import_prefix;
32019081
JK
7658 VEC (const_char_ptr) *excludes = NULL;
7659 struct cleanup *cleanups;
13387711
SW
7660
7661 char *temp;
27aa8d6a
SW
7662
7663 import_attr = dwarf2_attr (die, DW_AT_import, cu);
7664 if (import_attr == NULL)
7665 {
7666 complaint (&symfile_complaints, _("Tag '%s' has no DW_AT_import"),
7667 dwarf_tag_name (die->tag));
7668 return;
7669 }
7670
de4affc9
CC
7671 imported_cu = cu;
7672 imported_die = follow_die_ref_or_sig (die, import_attr, &imported_cu);
7673 imported_name = dwarf2_name (imported_die, imported_cu);
27aa8d6a
SW
7674 if (imported_name == NULL)
7675 {
7676 /* GCC bug: https://bugzilla.redhat.com/show_bug.cgi?id=506524
7677
7678 The import in the following code:
7679 namespace A
7680 {
7681 typedef int B;
7682 }
7683
7684 int main ()
7685 {
7686 using A::B;
7687 B b;
7688 return b;
7689 }
7690
7691 ...
7692 <2><51>: Abbrev Number: 3 (DW_TAG_imported_declaration)
7693 <52> DW_AT_decl_file : 1
7694 <53> DW_AT_decl_line : 6
7695 <54> DW_AT_import : <0x75>
7696 <2><58>: Abbrev Number: 4 (DW_TAG_typedef)
7697 <59> DW_AT_name : B
7698 <5b> DW_AT_decl_file : 1
7699 <5c> DW_AT_decl_line : 2
7700 <5d> DW_AT_type : <0x6e>
7701 ...
7702 <1><75>: Abbrev Number: 7 (DW_TAG_base_type)
7703 <76> DW_AT_byte_size : 4
7704 <77> DW_AT_encoding : 5 (signed)
7705
7706 imports the wrong die ( 0x75 instead of 0x58 ).
7707 This case will be ignored until the gcc bug is fixed. */
7708 return;
7709 }
7710
82856980
SW
7711 /* Figure out the local name after import. */
7712 import_alias = dwarf2_name (die, cu);
27aa8d6a 7713
794684b6
SW
7714 /* Figure out where the statement is being imported to. */
7715 import_prefix = determine_prefix (die, cu);
7716
7717 /* Figure out what the scope of the imported die is and prepend it
7718 to the name of the imported die. */
de4affc9 7719 imported_name_prefix = determine_prefix (imported_die, imported_cu);
794684b6 7720
f55ee35c
JK
7721 if (imported_die->tag != DW_TAG_namespace
7722 && imported_die->tag != DW_TAG_module)
794684b6 7723 {
13387711
SW
7724 imported_declaration = imported_name;
7725 canonical_name = imported_name_prefix;
794684b6 7726 }
13387711 7727 else if (strlen (imported_name_prefix) > 0)
794684b6 7728 {
13387711
SW
7729 temp = alloca (strlen (imported_name_prefix)
7730 + 2 + strlen (imported_name) + 1);
7731 strcpy (temp, imported_name_prefix);
7732 strcat (temp, "::");
7733 strcat (temp, imported_name);
7734 canonical_name = temp;
794684b6 7735 }
13387711
SW
7736 else
7737 canonical_name = imported_name;
794684b6 7738
32019081
JK
7739 cleanups = make_cleanup (VEC_cleanup (const_char_ptr), &excludes);
7740
7741 if (die->tag == DW_TAG_imported_module && cu->language == language_fortran)
7742 for (child_die = die->child; child_die && child_die->tag;
7743 child_die = sibling_die (child_die))
7744 {
7745 /* DWARF-4: A Fortran use statement with a “rename list” may be
7746 represented by an imported module entry with an import attribute
7747 referring to the module and owned entries corresponding to those
7748 entities that are renamed as part of being imported. */
7749
7750 if (child_die->tag != DW_TAG_imported_declaration)
7751 {
7752 complaint (&symfile_complaints,
7753 _("child DW_TAG_imported_declaration expected "
7754 "- DIE at 0x%x [in module %s]"),
b64f50a1 7755 child_die->offset.sect_off, objfile->name);
32019081
JK
7756 continue;
7757 }
7758
7759 import_attr = dwarf2_attr (child_die, DW_AT_import, cu);
7760 if (import_attr == NULL)
7761 {
7762 complaint (&symfile_complaints, _("Tag '%s' has no DW_AT_import"),
7763 dwarf_tag_name (child_die->tag));
7764 continue;
7765 }
7766
7767 imported_cu = cu;
7768 imported_die = follow_die_ref_or_sig (child_die, import_attr,
7769 &imported_cu);
7770 imported_name = dwarf2_name (imported_die, imported_cu);
7771 if (imported_name == NULL)
7772 {
7773 complaint (&symfile_complaints,
7774 _("child DW_TAG_imported_declaration has unknown "
7775 "imported name - DIE at 0x%x [in module %s]"),
b64f50a1 7776 child_die->offset.sect_off, objfile->name);
32019081
JK
7777 continue;
7778 }
7779
7780 VEC_safe_push (const_char_ptr, excludes, imported_name);
7781
7782 process_die (child_die, cu);
7783 }
7784
c0cc3a76
SW
7785 cp_add_using_directive (import_prefix,
7786 canonical_name,
7787 import_alias,
13387711 7788 imported_declaration,
32019081 7789 excludes,
bb5ed363 7790 &objfile->objfile_obstack);
32019081
JK
7791
7792 do_cleanups (cleanups);
27aa8d6a
SW
7793}
7794
f4dc4d17 7795/* Cleanup function for handle_DW_AT_stmt_list. */
ae2de4f8 7796
cb1df416
DJ
7797static void
7798free_cu_line_header (void *arg)
7799{
7800 struct dwarf2_cu *cu = arg;
7801
7802 free_line_header (cu->line_header);
7803 cu->line_header = NULL;
7804}
7805
9291a0cd
TT
7806static void
7807find_file_and_directory (struct die_info *die, struct dwarf2_cu *cu,
7808 char **name, char **comp_dir)
7809{
7810 struct attribute *attr;
7811
7812 *name = NULL;
7813 *comp_dir = NULL;
7814
7815 /* Find the filename. Do not use dwarf2_name here, since the filename
7816 is not a source language identifier. */
7817 attr = dwarf2_attr (die, DW_AT_name, cu);
7818 if (attr)
7819 {
7820 *name = DW_STRING (attr);
7821 }
7822
7823 attr = dwarf2_attr (die, DW_AT_comp_dir, cu);
7824 if (attr)
7825 *comp_dir = DW_STRING (attr);
7826 else if (*name != NULL && IS_ABSOLUTE_PATH (*name))
7827 {
7828 *comp_dir = ldirname (*name);
7829 if (*comp_dir != NULL)
7830 make_cleanup (xfree, *comp_dir);
7831 }
7832 if (*comp_dir != NULL)
7833 {
7834 /* Irix 6.2 native cc prepends <machine>.: to the compilation
7835 directory, get rid of it. */
7836 char *cp = strchr (*comp_dir, ':');
7837
7838 if (cp && cp != *comp_dir && cp[-1] == '.' && cp[1] == '/')
7839 *comp_dir = cp + 1;
7840 }
7841
7842 if (*name == NULL)
7843 *name = "<unknown>";
7844}
7845
f4dc4d17
DE
7846/* Handle DW_AT_stmt_list for a compilation unit.
7847 DIE is the DW_TAG_compile_unit die for CU.
f3f5162e
DE
7848 COMP_DIR is the compilation directory.
7849 WANT_LINE_INFO is non-zero if the pc/line-number mapping is needed. */
2ab95328
TT
7850
7851static void
7852handle_DW_AT_stmt_list (struct die_info *die, struct dwarf2_cu *cu,
f4dc4d17 7853 const char *comp_dir)
2ab95328
TT
7854{
7855 struct attribute *attr;
2ab95328 7856
f4dc4d17
DE
7857 gdb_assert (! cu->per_cu->is_debug_types);
7858
2ab95328
TT
7859 attr = dwarf2_attr (die, DW_AT_stmt_list, cu);
7860 if (attr)
7861 {
7862 unsigned int line_offset = DW_UNSND (attr);
7863 struct line_header *line_header
3019eac3 7864 = dwarf_decode_line_header (line_offset, cu);
2ab95328
TT
7865
7866 if (line_header)
dee91e82
DE
7867 {
7868 cu->line_header = line_header;
7869 make_cleanup (free_cu_line_header, cu);
f4dc4d17 7870 dwarf_decode_lines (line_header, comp_dir, cu, NULL, 1);
dee91e82 7871 }
2ab95328
TT
7872 }
7873}
7874
95554aad 7875/* Process DW_TAG_compile_unit or DW_TAG_partial_unit. */
ae2de4f8 7876
c906108c 7877static void
e7c27a73 7878read_file_scope (struct die_info *die, struct dwarf2_cu *cu)
c906108c 7879{
dee91e82 7880 struct objfile *objfile = dwarf2_per_objfile->objfile;
debd256d 7881 struct cleanup *back_to = make_cleanup (null_cleanup, 0);
2acceee2 7882 CORE_ADDR lowpc = ((CORE_ADDR) -1);
c906108c
SS
7883 CORE_ADDR highpc = ((CORE_ADDR) 0);
7884 struct attribute *attr;
e1024ff1 7885 char *name = NULL;
c906108c
SS
7886 char *comp_dir = NULL;
7887 struct die_info *child_die;
7888 bfd *abfd = objfile->obfd;
e142c38c 7889 CORE_ADDR baseaddr;
6e70227d 7890
e142c38c 7891 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
c906108c 7892
fae299cd 7893 get_scope_pc_bounds (die, &lowpc, &highpc, cu);
c906108c
SS
7894
7895 /* If we didn't find a lowpc, set it to highpc to avoid complaints
7896 from finish_block. */
2acceee2 7897 if (lowpc == ((CORE_ADDR) -1))
c906108c
SS
7898 lowpc = highpc;
7899 lowpc += baseaddr;
7900 highpc += baseaddr;
7901
9291a0cd 7902 find_file_and_directory (die, cu, &name, &comp_dir);
e1024ff1 7903
95554aad 7904 prepare_one_comp_unit (cu, die, cu->language);
303b6f5d 7905
f4b8a18d
KW
7906 /* The XLCL doesn't generate DW_LANG_OpenCL because this attribute is not
7907 standardised yet. As a workaround for the language detection we fall
7908 back to the DW_AT_producer string. */
7909 if (cu->producer && strstr (cu->producer, "IBM XL C for OpenCL") != NULL)
7910 cu->language = language_opencl;
7911
3019eac3
DE
7912 /* Similar hack for Go. */
7913 if (cu->producer && strstr (cu->producer, "GNU Go ") != NULL)
7914 set_cu_language (DW_LANG_Go, cu);
7915
f4dc4d17 7916 dwarf2_start_symtab (cu, name, comp_dir, lowpc);
3019eac3
DE
7917
7918 /* Decode line number information if present. We do this before
7919 processing child DIEs, so that the line header table is available
7920 for DW_AT_decl_file. */
f4dc4d17 7921 handle_DW_AT_stmt_list (die, cu, comp_dir);
3019eac3
DE
7922
7923 /* Process all dies in compilation unit. */
7924 if (die->child != NULL)
7925 {
7926 child_die = die->child;
7927 while (child_die && child_die->tag)
7928 {
7929 process_die (child_die, cu);
7930 child_die = sibling_die (child_die);
7931 }
7932 }
7933
7934 /* Decode macro information, if present. Dwarf 2 macro information
7935 refers to information in the line number info statement program
7936 header, so we can only read it if we've read the header
7937 successfully. */
7938 attr = dwarf2_attr (die, DW_AT_GNU_macros, cu);
7939 if (attr && cu->line_header)
7940 {
7941 if (dwarf2_attr (die, DW_AT_macro_info, cu))
7942 complaint (&symfile_complaints,
7943 _("CU refers to both DW_AT_GNU_macros and DW_AT_macro_info"));
7944
09262596 7945 dwarf_decode_macros (cu, DW_UNSND (attr), comp_dir, 1);
3019eac3
DE
7946 }
7947 else
7948 {
7949 attr = dwarf2_attr (die, DW_AT_macro_info, cu);
7950 if (attr && cu->line_header)
7951 {
7952 unsigned int macro_offset = DW_UNSND (attr);
7953
09262596 7954 dwarf_decode_macros (cu, macro_offset, comp_dir, 0);
3019eac3
DE
7955 }
7956 }
7957
7958 do_cleanups (back_to);
7959}
7960
f4dc4d17
DE
7961/* TU version of handle_DW_AT_stmt_list for read_type_unit_scope.
7962 Create the set of symtabs used by this TU, or if this TU is sharing
7963 symtabs with another TU and the symtabs have already been created
7964 then restore those symtabs in the line header.
7965 We don't need the pc/line-number mapping for type units. */
3019eac3
DE
7966
7967static void
f4dc4d17 7968setup_type_unit_groups (struct die_info *die, struct dwarf2_cu *cu)
3019eac3 7969{
f4dc4d17
DE
7970 struct objfile *objfile = dwarf2_per_objfile->objfile;
7971 struct dwarf2_per_cu_data *per_cu = cu->per_cu;
7972 struct type_unit_group *tu_group;
7973 int first_time;
7974 struct line_header *lh;
3019eac3 7975 struct attribute *attr;
f4dc4d17 7976 unsigned int i, line_offset;
3019eac3 7977
f4dc4d17 7978 gdb_assert (per_cu->is_debug_types);
3019eac3 7979
f4dc4d17 7980 attr = dwarf2_attr (die, DW_AT_stmt_list, cu);
3019eac3 7981
f4dc4d17
DE
7982 /* If we're using .gdb_index (includes -readnow) then
7983 per_cu->s.type_unit_group may not have been set up yet. */
7984 if (per_cu->s.type_unit_group == NULL)
094b34ac 7985 per_cu->s.type_unit_group = get_type_unit_group (cu, attr);
f4dc4d17
DE
7986 tu_group = per_cu->s.type_unit_group;
7987
7988 /* If we've already processed this stmt_list there's no real need to
7989 do it again, we could fake it and just recreate the part we need
7990 (file name,index -> symtab mapping). If data shows this optimization
7991 is useful we can do it then. */
7992 first_time = tu_group->primary_symtab == NULL;
7993
7994 /* We have to handle the case of both a missing DW_AT_stmt_list or bad
7995 debug info. */
7996 lh = NULL;
7997 if (attr != NULL)
3019eac3 7998 {
f4dc4d17
DE
7999 line_offset = DW_UNSND (attr);
8000 lh = dwarf_decode_line_header (line_offset, cu);
8001 }
8002 if (lh == NULL)
8003 {
8004 if (first_time)
8005 dwarf2_start_symtab (cu, "", NULL, 0);
8006 else
8007 {
8008 gdb_assert (tu_group->symtabs == NULL);
8009 restart_symtab (0);
8010 }
8011 /* Note: The primary symtab will get allocated at the end. */
8012 return;
3019eac3
DE
8013 }
8014
f4dc4d17
DE
8015 cu->line_header = lh;
8016 make_cleanup (free_cu_line_header, cu);
3019eac3 8017
f4dc4d17
DE
8018 if (first_time)
8019 {
8020 dwarf2_start_symtab (cu, "", NULL, 0);
3019eac3 8021
f4dc4d17
DE
8022 tu_group->num_symtabs = lh->num_file_names;
8023 tu_group->symtabs = XNEWVEC (struct symtab *, lh->num_file_names);
3019eac3 8024
f4dc4d17
DE
8025 for (i = 0; i < lh->num_file_names; ++i)
8026 {
8027 char *dir = NULL;
8028 struct file_entry *fe = &lh->file_names[i];
3019eac3 8029
f4dc4d17
DE
8030 if (fe->dir_index)
8031 dir = lh->include_dirs[fe->dir_index - 1];
8032 dwarf2_start_subfile (fe->name, dir, NULL);
3019eac3 8033
f4dc4d17
DE
8034 /* Note: We don't have to watch for the main subfile here, type units
8035 don't have DW_AT_name. */
3019eac3 8036
f4dc4d17
DE
8037 if (current_subfile->symtab == NULL)
8038 {
8039 /* NOTE: start_subfile will recognize when it's been passed
8040 a file it has already seen. So we can't assume there's a
8041 simple mapping from lh->file_names to subfiles,
8042 lh->file_names may contain dups. */
8043 current_subfile->symtab = allocate_symtab (current_subfile->name,
8044 objfile);
8045 }
8046
8047 fe->symtab = current_subfile->symtab;
8048 tu_group->symtabs[i] = fe->symtab;
8049 }
8050 }
8051 else
3019eac3 8052 {
f4dc4d17
DE
8053 restart_symtab (0);
8054
8055 for (i = 0; i < lh->num_file_names; ++i)
8056 {
8057 struct file_entry *fe = &lh->file_names[i];
8058
8059 fe->symtab = tu_group->symtabs[i];
8060 }
3019eac3
DE
8061 }
8062
f4dc4d17
DE
8063 /* The main symtab is allocated last. Type units don't have DW_AT_name
8064 so they don't have a "real" (so to speak) symtab anyway.
8065 There is later code that will assign the main symtab to all symbols
8066 that don't have one. We need to handle the case of a symbol with a
8067 missing symtab (DW_AT_decl_file) anyway. */
8068}
3019eac3 8069
f4dc4d17
DE
8070/* Process DW_TAG_type_unit.
8071 For TUs we want to skip the first top level sibling if it's not the
8072 actual type being defined by this TU. In this case the first top
8073 level sibling is there to provide context only. */
3019eac3 8074
f4dc4d17
DE
8075static void
8076read_type_unit_scope (struct die_info *die, struct dwarf2_cu *cu)
8077{
8078 struct die_info *child_die;
3019eac3 8079
f4dc4d17
DE
8080 prepare_one_comp_unit (cu, die, language_minimal);
8081
8082 /* Initialize (or reinitialize) the machinery for building symtabs.
8083 We do this before processing child DIEs, so that the line header table
8084 is available for DW_AT_decl_file. */
8085 setup_type_unit_groups (die, cu);
8086
8087 if (die->child != NULL)
8088 {
8089 child_die = die->child;
8090 while (child_die && child_die->tag)
8091 {
8092 process_die (child_die, cu);
8093 child_die = sibling_die (child_die);
8094 }
8095 }
3019eac3
DE
8096}
8097\f
80626a55
DE
8098/* DWO/DWP files.
8099
8100 http://gcc.gnu.org/wiki/DebugFission
8101 http://gcc.gnu.org/wiki/DebugFissionDWP
8102
8103 To simplify handling of both DWO files ("object" files with the DWARF info)
8104 and DWP files (a file with the DWOs packaged up into one file), we treat
8105 DWP files as having a collection of virtual DWO files. */
3019eac3
DE
8106
8107static hashval_t
8108hash_dwo_file (const void *item)
8109{
8110 const struct dwo_file *dwo_file = item;
8111
80626a55 8112 return htab_hash_string (dwo_file->name);
3019eac3
DE
8113}
8114
8115static int
8116eq_dwo_file (const void *item_lhs, const void *item_rhs)
8117{
8118 const struct dwo_file *lhs = item_lhs;
8119 const struct dwo_file *rhs = item_rhs;
8120
80626a55 8121 return strcmp (lhs->name, rhs->name) == 0;
3019eac3
DE
8122}
8123
8124/* Allocate a hash table for DWO files. */
8125
8126static htab_t
8127allocate_dwo_file_hash_table (void)
8128{
8129 struct objfile *objfile = dwarf2_per_objfile->objfile;
8130
8131 return htab_create_alloc_ex (41,
8132 hash_dwo_file,
8133 eq_dwo_file,
8134 NULL,
8135 &objfile->objfile_obstack,
8136 hashtab_obstack_allocate,
8137 dummy_obstack_deallocate);
8138}
8139
80626a55
DE
8140/* Lookup DWO file DWO_NAME. */
8141
8142static void **
8143lookup_dwo_file_slot (const char *dwo_name)
8144{
8145 struct dwo_file find_entry;
8146 void **slot;
8147
8148 if (dwarf2_per_objfile->dwo_files == NULL)
8149 dwarf2_per_objfile->dwo_files = allocate_dwo_file_hash_table ();
8150
8151 memset (&find_entry, 0, sizeof (find_entry));
8152 find_entry.name = dwo_name;
8153 slot = htab_find_slot (dwarf2_per_objfile->dwo_files, &find_entry, INSERT);
8154
8155 return slot;
8156}
8157
3019eac3
DE
8158static hashval_t
8159hash_dwo_unit (const void *item)
8160{
8161 const struct dwo_unit *dwo_unit = item;
8162
8163 /* This drops the top 32 bits of the id, but is ok for a hash. */
8164 return dwo_unit->signature;
8165}
8166
8167static int
8168eq_dwo_unit (const void *item_lhs, const void *item_rhs)
8169{
8170 const struct dwo_unit *lhs = item_lhs;
8171 const struct dwo_unit *rhs = item_rhs;
8172
8173 /* The signature is assumed to be unique within the DWO file.
8174 So while object file CU dwo_id's always have the value zero,
8175 that's OK, assuming each object file DWO file has only one CU,
8176 and that's the rule for now. */
8177 return lhs->signature == rhs->signature;
8178}
8179
8180/* Allocate a hash table for DWO CUs,TUs.
8181 There is one of these tables for each of CUs,TUs for each DWO file. */
8182
8183static htab_t
8184allocate_dwo_unit_table (struct objfile *objfile)
8185{
8186 /* Start out with a pretty small number.
8187 Generally DWO files contain only one CU and maybe some TUs. */
8188 return htab_create_alloc_ex (3,
8189 hash_dwo_unit,
8190 eq_dwo_unit,
8191 NULL,
8192 &objfile->objfile_obstack,
8193 hashtab_obstack_allocate,
8194 dummy_obstack_deallocate);
8195}
8196
80626a55 8197/* Structure used to pass data to create_dwo_debug_info_hash_table_reader. */
3019eac3
DE
8198
8199struct create_dwo_info_table_data
8200{
8201 struct dwo_file *dwo_file;
8202 htab_t cu_htab;
8203};
8204
80626a55 8205/* die_reader_func for create_dwo_debug_info_hash_table. */
3019eac3
DE
8206
8207static void
80626a55
DE
8208create_dwo_debug_info_hash_table_reader (const struct die_reader_specs *reader,
8209 gdb_byte *info_ptr,
8210 struct die_info *comp_unit_die,
8211 int has_children,
8212 void *datap)
3019eac3
DE
8213{
8214 struct dwarf2_cu *cu = reader->cu;
8215 struct objfile *objfile = dwarf2_per_objfile->objfile;
8216 sect_offset offset = cu->per_cu->offset;
8217 struct dwarf2_section_info *section = cu->per_cu->info_or_types_section;
8218 struct create_dwo_info_table_data *data = datap;
8219 struct dwo_file *dwo_file = data->dwo_file;
8220 htab_t cu_htab = data->cu_htab;
8221 void **slot;
8222 struct attribute *attr;
8223 struct dwo_unit *dwo_unit;
8224
8225 attr = dwarf2_attr (comp_unit_die, DW_AT_GNU_dwo_id, cu);
8226 if (attr == NULL)
8227 {
8228 error (_("Dwarf Error: debug entry at offset 0x%x is missing"
8229 " its dwo_id [in module %s]"),
80626a55 8230 offset.sect_off, dwo_file->name);
3019eac3
DE
8231 return;
8232 }
8233
8234 dwo_unit = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct dwo_unit);
8235 dwo_unit->dwo_file = dwo_file;
8236 dwo_unit->signature = DW_UNSND (attr);
8237 dwo_unit->info_or_types_section = section;
8238 dwo_unit->offset = offset;
8239 dwo_unit->length = cu->per_cu->length;
8240
8241 slot = htab_find_slot (cu_htab, dwo_unit, INSERT);
8242 gdb_assert (slot != NULL);
8243 if (*slot != NULL)
8244 {
8245 const struct dwo_unit *dup_dwo_unit = *slot;
8246
8247 complaint (&symfile_complaints,
8248 _("debug entry at offset 0x%x is duplicate to the entry at"
8249 " offset 0x%x, dwo_id 0x%s [in module %s]"),
8250 offset.sect_off, dup_dwo_unit->offset.sect_off,
8251 phex (dwo_unit->signature, sizeof (dwo_unit->signature)),
80626a55 8252 dwo_file->name);
3019eac3
DE
8253 }
8254 else
8255 *slot = dwo_unit;
8256
09406207 8257 if (dwarf2_read_debug)
3019eac3
DE
8258 fprintf_unfiltered (gdb_stdlog, " offset 0x%x, dwo_id 0x%s\n",
8259 offset.sect_off,
8260 phex (dwo_unit->signature,
8261 sizeof (dwo_unit->signature)));
8262}
8263
80626a55
DE
8264/* Create a hash table to map DWO IDs to their CU entry in
8265 .debug_info.dwo in DWO_FILE.
8266 Note: This function processes DWO files only, not DWP files. */
3019eac3
DE
8267
8268static htab_t
80626a55 8269create_dwo_debug_info_hash_table (struct dwo_file *dwo_file)
3019eac3
DE
8270{
8271 struct objfile *objfile = dwarf2_per_objfile->objfile;
8272 struct dwarf2_section_info *section = &dwo_file->sections.info;
8273 bfd *abfd;
8274 htab_t cu_htab;
8275 gdb_byte *info_ptr, *end_ptr;
8276 struct create_dwo_info_table_data create_dwo_info_table_data;
8277
8278 dwarf2_read_section (objfile, section);
8279 info_ptr = section->buffer;
8280
8281 if (info_ptr == NULL)
8282 return NULL;
8283
8284 /* We can't set abfd until now because the section may be empty or
8285 not present, in which case section->asection will be NULL. */
8286 abfd = section->asection->owner;
8287
09406207 8288 if (dwarf2_read_debug)
3019eac3
DE
8289 fprintf_unfiltered (gdb_stdlog, "Reading .debug_info.dwo for %s:\n",
8290 bfd_get_filename (abfd));
8291
8292 cu_htab = allocate_dwo_unit_table (objfile);
8293
8294 create_dwo_info_table_data.dwo_file = dwo_file;
8295 create_dwo_info_table_data.cu_htab = cu_htab;
8296
8297 end_ptr = info_ptr + section->size;
8298 while (info_ptr < end_ptr)
8299 {
8300 struct dwarf2_per_cu_data per_cu;
8301
8302 memset (&per_cu, 0, sizeof (per_cu));
8303 per_cu.objfile = objfile;
8304 per_cu.is_debug_types = 0;
8305 per_cu.offset.sect_off = info_ptr - section->buffer;
8306 per_cu.info_or_types_section = section;
8307
8308 init_cutu_and_read_dies_no_follow (&per_cu,
8309 &dwo_file->sections.abbrev,
8310 dwo_file,
80626a55 8311 create_dwo_debug_info_hash_table_reader,
3019eac3
DE
8312 &create_dwo_info_table_data);
8313
8314 info_ptr += per_cu.length;
8315 }
8316
8317 return cu_htab;
8318}
8319
80626a55
DE
8320/* DWP file .debug_{cu,tu}_index section format:
8321 [ref: http://gcc.gnu.org/wiki/DebugFissionDWP]
8322
8323 Both index sections have the same format, and serve to map a 64-bit
8324 signature to a set of section numbers. Each section begins with a header,
8325 followed by a hash table of 64-bit signatures, a parallel table of 32-bit
8326 indexes, and a pool of 32-bit section numbers. The index sections will be
8327 aligned at 8-byte boundaries in the file.
8328
8329 The index section header contains two unsigned 32-bit values (using the
8330 byte order of the application binary):
8331
8332 N, the number of compilation units or type units in the index
8333 M, the number of slots in the hash table
8334
8335 (We assume that N and M will not exceed 2^32 - 1.)
8336
8337 The size of the hash table, M, must be 2^k such that 2^k > 3*N/2.
8338
8339 The hash table begins at offset 8 in the section, and consists of an array
8340 of M 64-bit slots. Each slot contains a 64-bit signature (using the byte
8341 order of the application binary). Unused slots in the hash table are 0.
8342 (We rely on the extreme unlikeliness of a signature being exactly 0.)
8343
8344 The parallel table begins immediately after the hash table
8345 (at offset 8 + 8 * M from the beginning of the section), and consists of an
8346 array of 32-bit indexes (using the byte order of the application binary),
8347 corresponding 1-1 with slots in the hash table. Each entry in the parallel
8348 table contains a 32-bit index into the pool of section numbers. For unused
8349 hash table slots, the corresponding entry in the parallel table will be 0.
8350
8351 Given a 64-bit compilation unit signature or a type signature S, an entry
8352 in the hash table is located as follows:
8353
8354 1) Calculate a primary hash H = S & MASK(k), where MASK(k) is a mask with
8355 the low-order k bits all set to 1.
8356
8357 2) Calculate a secondary hash H' = (((S >> 32) & MASK(k)) | 1).
8358
8359 3) If the hash table entry at index H matches the signature, use that
8360 entry. If the hash table entry at index H is unused (all zeroes),
8361 terminate the search: the signature is not present in the table.
8362
8363 4) Let H = (H + H') modulo M. Repeat at Step 3.
8364
8365 Because M > N and H' and M are relatively prime, the search is guaranteed
8366 to stop at an unused slot or find the match.
8367
8368 The pool of section numbers begins immediately following the hash table
8369 (at offset 8 + 12 * M from the beginning of the section). The pool of
8370 section numbers consists of an array of 32-bit words (using the byte order
8371 of the application binary). Each item in the array is indexed starting
8372 from 0. The hash table entry provides the index of the first section
8373 number in the set. Additional section numbers in the set follow, and the
8374 set is terminated by a 0 entry (section number 0 is not used in ELF).
8375
8376 In each set of section numbers, the .debug_info.dwo or .debug_types.dwo
8377 section must be the first entry in the set, and the .debug_abbrev.dwo must
8378 be the second entry. Other members of the set may follow in any order. */
8379
8380/* Create a hash table to map DWO IDs to their CU/TU entry in
8381 .debug_{info,types}.dwo in DWP_FILE.
8382 Returns NULL if there isn't one.
8383 Note: This function processes DWP files only, not DWO files. */
8384
8385static struct dwp_hash_table *
8386create_dwp_hash_table (struct dwp_file *dwp_file, int is_debug_types)
8387{
8388 struct objfile *objfile = dwarf2_per_objfile->objfile;
8389 bfd *dbfd = dwp_file->dbfd;
8390 char *index_ptr, *index_end;
8391 struct dwarf2_section_info *index;
8392 uint32_t version, nr_units, nr_slots;
8393 struct dwp_hash_table *htab;
8394
8395 if (is_debug_types)
8396 index = &dwp_file->sections.tu_index;
8397 else
8398 index = &dwp_file->sections.cu_index;
8399
8400 if (dwarf2_section_empty_p (index))
8401 return NULL;
8402 dwarf2_read_section (objfile, index);
8403
8404 index_ptr = index->buffer;
8405 index_end = index_ptr + index->size;
8406
8407 version = read_4_bytes (dbfd, index_ptr);
8408 index_ptr += 8; /* Skip the unused word. */
8409 nr_units = read_4_bytes (dbfd, index_ptr);
8410 index_ptr += 4;
8411 nr_slots = read_4_bytes (dbfd, index_ptr);
8412 index_ptr += 4;
8413
8414 if (version != 1)
8415 {
8416 error (_("Dwarf Error: unsupported DWP file version (%u)"
8417 " [in module %s]"),
8418 version, dwp_file->name);
8419 }
8420 if (nr_slots != (nr_slots & -nr_slots))
8421 {
8422 error (_("Dwarf Error: number of slots in DWP hash table (%u)"
8423 " is not power of 2 [in module %s]"),
8424 nr_slots, dwp_file->name);
8425 }
8426
8427 htab = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct dwp_hash_table);
8428 htab->nr_units = nr_units;
8429 htab->nr_slots = nr_slots;
8430 htab->hash_table = index_ptr;
8431 htab->unit_table = htab->hash_table + sizeof (uint64_t) * nr_slots;
8432 htab->section_pool = htab->unit_table + sizeof (uint32_t) * nr_slots;
8433
8434 return htab;
8435}
8436
8437/* Update SECTIONS with the data from SECTP.
8438
8439 This function is like the other "locate" section routines that are
8440 passed to bfd_map_over_sections, but in this context the sections to
8441 read comes from the DWP hash table, not the full ELF section table.
8442
8443 The result is non-zero for success, or zero if an error was found. */
8444
8445static int
8446locate_virtual_dwo_sections (asection *sectp,
8447 struct virtual_dwo_sections *sections)
8448{
8449 const struct dwop_section_names *names = &dwop_section_names;
8450
8451 if (section_is_p (sectp->name, &names->abbrev_dwo))
8452 {
8453 /* There can be only one. */
8454 if (sections->abbrev.asection != NULL)
8455 return 0;
8456 sections->abbrev.asection = sectp;
8457 sections->abbrev.size = bfd_get_section_size (sectp);
8458 }
8459 else if (section_is_p (sectp->name, &names->info_dwo)
8460 || section_is_p (sectp->name, &names->types_dwo))
8461 {
8462 /* There can be only one. */
8463 if (sections->info_or_types.asection != NULL)
8464 return 0;
8465 sections->info_or_types.asection = sectp;
8466 sections->info_or_types.size = bfd_get_section_size (sectp);
8467 }
8468 else if (section_is_p (sectp->name, &names->line_dwo))
8469 {
8470 /* There can be only one. */
8471 if (sections->line.asection != NULL)
8472 return 0;
8473 sections->line.asection = sectp;
8474 sections->line.size = bfd_get_section_size (sectp);
8475 }
8476 else if (section_is_p (sectp->name, &names->loc_dwo))
8477 {
8478 /* There can be only one. */
8479 if (sections->loc.asection != NULL)
8480 return 0;
8481 sections->loc.asection = sectp;
8482 sections->loc.size = bfd_get_section_size (sectp);
8483 }
8484 else if (section_is_p (sectp->name, &names->macinfo_dwo))
8485 {
8486 /* There can be only one. */
8487 if (sections->macinfo.asection != NULL)
8488 return 0;
8489 sections->macinfo.asection = sectp;
8490 sections->macinfo.size = bfd_get_section_size (sectp);
8491 }
8492 else if (section_is_p (sectp->name, &names->macro_dwo))
8493 {
8494 /* There can be only one. */
8495 if (sections->macro.asection != NULL)
8496 return 0;
8497 sections->macro.asection = sectp;
8498 sections->macro.size = bfd_get_section_size (sectp);
8499 }
8500 else if (section_is_p (sectp->name, &names->str_offsets_dwo))
8501 {
8502 /* There can be only one. */
8503 if (sections->str_offsets.asection != NULL)
8504 return 0;
8505 sections->str_offsets.asection = sectp;
8506 sections->str_offsets.size = bfd_get_section_size (sectp);
8507 }
8508 else
8509 {
8510 /* No other kind of section is valid. */
8511 return 0;
8512 }
8513
8514 return 1;
8515}
8516
8517/* Create a dwo_unit object for the DWO with signature SIGNATURE.
8518 HTAB is the hash table from the DWP file.
8519 SECTION_INDEX is the index of the DWO in HTAB. */
8520
8521static struct dwo_unit *
8522create_dwo_in_dwp (struct dwp_file *dwp_file,
8523 const struct dwp_hash_table *htab,
8524 uint32_t section_index,
8525 ULONGEST signature, int is_debug_types)
8526{
8527 struct objfile *objfile = dwarf2_per_objfile->objfile;
8528 bfd *dbfd = dwp_file->dbfd;
8529 const char *kind = is_debug_types ? "TU" : "CU";
8530 struct dwo_file *dwo_file;
8531 struct dwo_unit *dwo_unit;
8532 struct virtual_dwo_sections sections;
8533 void **dwo_file_slot;
8534 char *virtual_dwo_name;
8535 struct dwarf2_section_info *cutu;
8536 struct cleanup *cleanups;
8537 int i;
8538
8539 if (dwarf2_read_debug)
8540 {
8541 fprintf_unfiltered (gdb_stdlog, "Reading %s %u/0x%s in DWP file: %s\n",
8542 kind,
8543 section_index, phex (signature, sizeof (signature)),
8544 dwp_file->name);
8545 }
8546
8547 /* Fetch the sections of this DWO.
8548 Put a limit on the number of sections we look for so that bad data
8549 doesn't cause us to loop forever. */
8550
8551#define MAX_NR_DWO_SECTIONS \
8552 (1 /* .debug_info or .debug_types */ \
8553 + 1 /* .debug_abbrev */ \
8554 + 1 /* .debug_line */ \
8555 + 1 /* .debug_loc */ \
8556 + 1 /* .debug_str_offsets */ \
8557 + 1 /* .debug_macro */ \
8558 + 1 /* .debug_macinfo */ \
8559 + 1 /* trailing zero */)
8560
8561 memset (&sections, 0, sizeof (sections));
8562 cleanups = make_cleanup (null_cleanup, 0);
8563
8564 for (i = 0; i < MAX_NR_DWO_SECTIONS; ++i)
8565 {
8566 asection *sectp;
8567 uint32_t section_nr =
8568 read_4_bytes (dbfd,
8569 htab->section_pool
8570 + (section_index + i) * sizeof (uint32_t));
8571
8572 if (section_nr == 0)
8573 break;
8574 if (section_nr >= dwp_file->num_sections)
8575 {
8576 error (_("Dwarf Error: bad DWP hash table, section number too large"
8577 " [in module %s]"),
8578 dwp_file->name);
8579 }
8580
8581 sectp = dwp_file->elf_sections[section_nr];
8582 if (! locate_virtual_dwo_sections (sectp, &sections))
8583 {
8584 error (_("Dwarf Error: bad DWP hash table, invalid section found"
8585 " [in module %s]"),
8586 dwp_file->name);
8587 }
8588 }
8589
8590 if (i < 2
8591 || sections.info_or_types.asection == NULL
8592 || sections.abbrev.asection == NULL)
8593 {
8594 error (_("Dwarf Error: bad DWP hash table, missing DWO sections"
8595 " [in module %s]"),
8596 dwp_file->name);
8597 }
8598 if (i == MAX_NR_DWO_SECTIONS)
8599 {
8600 error (_("Dwarf Error: bad DWP hash table, too many DWO sections"
8601 " [in module %s]"),
8602 dwp_file->name);
8603 }
8604
8605 /* It's easier for the rest of the code if we fake a struct dwo_file and
8606 have dwo_unit "live" in that. At least for now.
8607
8608 The DWP file can be made up of a random collection of CUs and TUs.
8609 However, for each CU + set of TUs that came from the same original
8610 DWO file, we want combine them back into a virtual DWO file to save space
8611 (fewer struct dwo_file objects to allocated). Remember that for really
8612 large apps there can be on the order of 8K CUs and 200K TUs, or more. */
8613
2792b94d
PM
8614 virtual_dwo_name =
8615 xstrprintf ("virtual-dwo/%d-%d-%d-%d",
8616 sections.abbrev.asection ? sections.abbrev.asection->id : 0,
8617 sections.line.asection ? sections.line.asection->id : 0,
8618 sections.loc.asection ? sections.loc.asection->id : 0,
8619 (sections.str_offsets.asection
8620 ? sections.str_offsets.asection->id
8621 : 0));
80626a55
DE
8622 make_cleanup (xfree, virtual_dwo_name);
8623 /* Can we use an existing virtual DWO file? */
8624 dwo_file_slot = lookup_dwo_file_slot (virtual_dwo_name);
8625 /* Create one if necessary. */
8626 if (*dwo_file_slot == NULL)
8627 {
8628 if (dwarf2_read_debug)
8629 {
8630 fprintf_unfiltered (gdb_stdlog, "Creating virtual DWO: %s\n",
8631 virtual_dwo_name);
8632 }
8633 dwo_file = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct dwo_file);
8634 dwo_file->name = obstack_copy0 (&objfile->objfile_obstack,
8635 virtual_dwo_name,
8636 strlen (virtual_dwo_name));
8637 dwo_file->sections.abbrev = sections.abbrev;
8638 dwo_file->sections.line = sections.line;
8639 dwo_file->sections.loc = sections.loc;
8640 dwo_file->sections.macinfo = sections.macinfo;
8641 dwo_file->sections.macro = sections.macro;
8642 dwo_file->sections.str_offsets = sections.str_offsets;
8643 /* The "str" section is global to the entire DWP file. */
8644 dwo_file->sections.str = dwp_file->sections.str;
8645 /* The info or types section is assigned later to dwo_unit,
8646 there's no need to record it in dwo_file.
8647 Also, we can't simply record type sections in dwo_file because
8648 we record a pointer into the vector in dwo_unit. As we collect more
8649 types we'll grow the vector and eventually have to reallocate space
8650 for it, invalidating all the pointers into the current copy. */
8651 *dwo_file_slot = dwo_file;
8652 }
8653 else
8654 {
8655 if (dwarf2_read_debug)
8656 {
8657 fprintf_unfiltered (gdb_stdlog, "Using existing virtual DWO: %s\n",
8658 virtual_dwo_name);
8659 }
8660 dwo_file = *dwo_file_slot;
8661 }
8662 do_cleanups (cleanups);
8663
8664 dwo_unit = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct dwo_unit);
8665 dwo_unit->dwo_file = dwo_file;
8666 dwo_unit->signature = signature;
8667 dwo_unit->info_or_types_section =
8668 obstack_alloc (&objfile->objfile_obstack,
8669 sizeof (struct dwarf2_section_info));
8670 *dwo_unit->info_or_types_section = sections.info_or_types;
8671 /* offset, length, type_offset_in_tu are set later. */
8672
8673 return dwo_unit;
8674}
8675
8676/* Lookup the DWO with SIGNATURE in DWP_FILE. */
8677
8678static struct dwo_unit *
8679lookup_dwo_in_dwp (struct dwp_file *dwp_file,
8680 const struct dwp_hash_table *htab,
8681 ULONGEST signature, int is_debug_types)
8682{
8683 bfd *dbfd = dwp_file->dbfd;
8684 uint32_t mask = htab->nr_slots - 1;
8685 uint32_t hash = signature & mask;
8686 uint32_t hash2 = ((signature >> 32) & mask) | 1;
8687 unsigned int i;
8688 void **slot;
8689 struct dwo_unit find_dwo_cu, *dwo_cu;
8690
8691 memset (&find_dwo_cu, 0, sizeof (find_dwo_cu));
8692 find_dwo_cu.signature = signature;
8693 slot = htab_find_slot (dwp_file->loaded_cutus, &find_dwo_cu, INSERT);
8694
8695 if (*slot != NULL)
8696 return *slot;
8697
8698 /* Use a for loop so that we don't loop forever on bad debug info. */
8699 for (i = 0; i < htab->nr_slots; ++i)
8700 {
8701 ULONGEST signature_in_table;
8702
8703 signature_in_table =
8704 read_8_bytes (dbfd, htab->hash_table + hash * sizeof (uint64_t));
8705 if (signature_in_table == signature)
8706 {
8707 uint32_t section_index =
8708 read_4_bytes (dbfd, htab->unit_table + hash * sizeof (uint32_t));
8709
8710 *slot = create_dwo_in_dwp (dwp_file, htab, section_index,
8711 signature, is_debug_types);
8712 return *slot;
8713 }
8714 if (signature_in_table == 0)
8715 return NULL;
8716 hash = (hash + hash2) & mask;
8717 }
8718
8719 error (_("Dwarf Error: bad DWP hash table, lookup didn't terminate"
8720 " [in module %s]"),
8721 dwp_file->name);
8722}
8723
8724/* Subroutine of open_dwop_file to simplify it.
3019eac3
DE
8725 Open the file specified by FILE_NAME and hand it off to BFD for
8726 preliminary analysis. Return a newly initialized bfd *, which
8727 includes a canonicalized copy of FILE_NAME.
80626a55 8728 If IS_DWP is TRUE, we're opening a DWP file, otherwise a DWO file.
3019eac3
DE
8729 In case of trouble, return NULL.
8730 NOTE: This function is derived from symfile_bfd_open. */
8731
8732static bfd *
80626a55 8733try_open_dwop_file (const char *file_name, int is_dwp)
3019eac3
DE
8734{
8735 bfd *sym_bfd;
80626a55 8736 int desc, flags;
3019eac3 8737 char *absolute_name;
3019eac3 8738
80626a55
DE
8739 flags = OPF_TRY_CWD_FIRST;
8740 if (is_dwp)
8741 flags |= OPF_SEARCH_IN_PATH;
8742 desc = openp (debug_file_directory, flags, file_name,
3019eac3
DE
8743 O_RDONLY | O_BINARY, &absolute_name);
8744 if (desc < 0)
8745 return NULL;
8746
bb397797 8747 sym_bfd = gdb_bfd_open (absolute_name, gnutarget, desc);
3019eac3
DE
8748 if (!sym_bfd)
8749 {
3019eac3
DE
8750 xfree (absolute_name);
8751 return NULL;
8752 }
a4453b7e 8753 xfree (absolute_name);
3019eac3
DE
8754 bfd_set_cacheable (sym_bfd, 1);
8755
8756 if (!bfd_check_format (sym_bfd, bfd_object))
8757 {
cbb099e8 8758 gdb_bfd_unref (sym_bfd); /* This also closes desc. */
3019eac3
DE
8759 return NULL;
8760 }
8761
3019eac3
DE
8762 return sym_bfd;
8763}
8764
80626a55 8765/* Try to open DWO/DWP file FILE_NAME.
3019eac3 8766 COMP_DIR is the DW_AT_comp_dir attribute.
80626a55 8767 If IS_DWP is TRUE, we're opening a DWP file, otherwise a DWO file.
3019eac3
DE
8768 The result is the bfd handle of the file.
8769 If there is a problem finding or opening the file, return NULL.
8770 Upon success, the canonicalized path of the file is stored in the bfd,
8771 same as symfile_bfd_open. */
8772
8773static bfd *
80626a55 8774open_dwop_file (const char *file_name, const char *comp_dir, int is_dwp)
3019eac3
DE
8775{
8776 bfd *abfd;
3019eac3 8777
80626a55
DE
8778 if (IS_ABSOLUTE_PATH (file_name))
8779 return try_open_dwop_file (file_name, is_dwp);
3019eac3
DE
8780
8781 /* Before trying the search path, try DWO_NAME in COMP_DIR. */
8782
8783 if (comp_dir != NULL)
8784 {
80626a55 8785 char *path_to_try = concat (comp_dir, SLASH_STRING, file_name, NULL);
3019eac3
DE
8786
8787 /* NOTE: If comp_dir is a relative path, this will also try the
8788 search path, which seems useful. */
80626a55 8789 abfd = try_open_dwop_file (path_to_try, is_dwp);
3019eac3
DE
8790 xfree (path_to_try);
8791 if (abfd != NULL)
8792 return abfd;
8793 }
8794
8795 /* That didn't work, try debug-file-directory, which, despite its name,
8796 is a list of paths. */
8797
8798 if (*debug_file_directory == '\0')
8799 return NULL;
8800
80626a55 8801 return try_open_dwop_file (file_name, is_dwp);
3019eac3
DE
8802}
8803
80626a55
DE
8804/* This function is mapped across the sections and remembers the offset and
8805 size of each of the DWO debugging sections we are interested in. */
8806
8807static void
8808dwarf2_locate_dwo_sections (bfd *abfd, asection *sectp, void *dwo_sections_ptr)
8809{
8810 struct dwo_sections *dwo_sections = dwo_sections_ptr;
8811 const struct dwop_section_names *names = &dwop_section_names;
8812
8813 if (section_is_p (sectp->name, &names->abbrev_dwo))
8814 {
8815 dwo_sections->abbrev.asection = sectp;
8816 dwo_sections->abbrev.size = bfd_get_section_size (sectp);
8817 }
8818 else if (section_is_p (sectp->name, &names->info_dwo))
8819 {
8820 dwo_sections->info.asection = sectp;
8821 dwo_sections->info.size = bfd_get_section_size (sectp);
8822 }
8823 else if (section_is_p (sectp->name, &names->line_dwo))
8824 {
8825 dwo_sections->line.asection = sectp;
8826 dwo_sections->line.size = bfd_get_section_size (sectp);
8827 }
8828 else if (section_is_p (sectp->name, &names->loc_dwo))
8829 {
8830 dwo_sections->loc.asection = sectp;
8831 dwo_sections->loc.size = bfd_get_section_size (sectp);
8832 }
8833 else if (section_is_p (sectp->name, &names->macinfo_dwo))
8834 {
8835 dwo_sections->macinfo.asection = sectp;
8836 dwo_sections->macinfo.size = bfd_get_section_size (sectp);
8837 }
8838 else if (section_is_p (sectp->name, &names->macro_dwo))
8839 {
8840 dwo_sections->macro.asection = sectp;
8841 dwo_sections->macro.size = bfd_get_section_size (sectp);
8842 }
8843 else if (section_is_p (sectp->name, &names->str_dwo))
8844 {
8845 dwo_sections->str.asection = sectp;
8846 dwo_sections->str.size = bfd_get_section_size (sectp);
8847 }
8848 else if (section_is_p (sectp->name, &names->str_offsets_dwo))
8849 {
8850 dwo_sections->str_offsets.asection = sectp;
8851 dwo_sections->str_offsets.size = bfd_get_section_size (sectp);
8852 }
8853 else if (section_is_p (sectp->name, &names->types_dwo))
8854 {
8855 struct dwarf2_section_info type_section;
8856
8857 memset (&type_section, 0, sizeof (type_section));
8858 type_section.asection = sectp;
8859 type_section.size = bfd_get_section_size (sectp);
8860 VEC_safe_push (dwarf2_section_info_def, dwo_sections->types,
8861 &type_section);
8862 }
8863}
8864
8865/* Initialize the use of the DWO file specified by DWO_NAME.
8866 The result is NULL if DWO_NAME can't be found. */
3019eac3
DE
8867
8868static struct dwo_file *
80626a55 8869open_and_init_dwo_file (const char *dwo_name, const char *comp_dir)
3019eac3
DE
8870{
8871 struct objfile *objfile = dwarf2_per_objfile->objfile;
80626a55
DE
8872 struct dwo_file *dwo_file;
8873 bfd *dbfd;
3019eac3
DE
8874 struct cleanup *cleanups;
8875
80626a55
DE
8876 dbfd = open_dwop_file (dwo_name, comp_dir, 0);
8877 if (dbfd == NULL)
8878 {
8879 if (dwarf2_read_debug)
8880 fprintf_unfiltered (gdb_stdlog, "DWO file not found: %s\n", dwo_name);
8881 return NULL;
8882 }
8883 dwo_file = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct dwo_file);
8884 dwo_file->name = obstack_copy0 (&objfile->objfile_obstack,
8885 dwo_name, strlen (dwo_name));
8886 dwo_file->dbfd = dbfd;
3019eac3
DE
8887
8888 cleanups = make_cleanup (free_dwo_file_cleanup, dwo_file);
8889
80626a55 8890 bfd_map_over_sections (dbfd, dwarf2_locate_dwo_sections, &dwo_file->sections);
3019eac3 8891
80626a55 8892 dwo_file->cus = create_dwo_debug_info_hash_table (dwo_file);
3019eac3
DE
8893
8894 dwo_file->tus = create_debug_types_hash_table (dwo_file,
8895 dwo_file->sections.types);
8896
8897 discard_cleanups (cleanups);
8898
80626a55
DE
8899 if (dwarf2_read_debug)
8900 fprintf_unfiltered (gdb_stdlog, "DWO file found: %s\n", dwo_name);
8901
3019eac3
DE
8902 return dwo_file;
8903}
8904
80626a55
DE
8905/* This function is mapped across the sections and remembers the offset and
8906 size of each of the DWP debugging sections we are interested in. */
3019eac3 8907
80626a55
DE
8908static void
8909dwarf2_locate_dwp_sections (bfd *abfd, asection *sectp, void *dwp_file_ptr)
3019eac3 8910{
80626a55
DE
8911 struct dwp_file *dwp_file = dwp_file_ptr;
8912 const struct dwop_section_names *names = &dwop_section_names;
8913 unsigned int elf_section_nr = elf_section_data (sectp)->this_idx;
3019eac3 8914
80626a55
DE
8915 /* Record the ELF section number for later lookup: this is what the
8916 .debug_cu_index,.debug_tu_index tables use. */
8917 gdb_assert (elf_section_nr < dwp_file->num_sections);
8918 dwp_file->elf_sections[elf_section_nr] = sectp;
3019eac3 8919
80626a55
DE
8920 /* Look for specific sections that we need. */
8921 if (section_is_p (sectp->name, &names->str_dwo))
8922 {
8923 dwp_file->sections.str.asection = sectp;
8924 dwp_file->sections.str.size = bfd_get_section_size (sectp);
8925 }
8926 else if (section_is_p (sectp->name, &names->cu_index))
8927 {
8928 dwp_file->sections.cu_index.asection = sectp;
8929 dwp_file->sections.cu_index.size = bfd_get_section_size (sectp);
8930 }
8931 else if (section_is_p (sectp->name, &names->tu_index))
8932 {
8933 dwp_file->sections.tu_index.asection = sectp;
8934 dwp_file->sections.tu_index.size = bfd_get_section_size (sectp);
8935 }
8936}
3019eac3 8937
80626a55 8938/* Hash function for dwp_file loaded CUs/TUs. */
3019eac3 8939
80626a55
DE
8940static hashval_t
8941hash_dwp_loaded_cutus (const void *item)
8942{
8943 const struct dwo_unit *dwo_unit = item;
3019eac3 8944
80626a55
DE
8945 /* This drops the top 32 bits of the signature, but is ok for a hash. */
8946 return dwo_unit->signature;
3019eac3
DE
8947}
8948
80626a55 8949/* Equality function for dwp_file loaded CUs/TUs. */
3019eac3 8950
80626a55
DE
8951static int
8952eq_dwp_loaded_cutus (const void *a, const void *b)
3019eac3 8953{
80626a55
DE
8954 const struct dwo_unit *dua = a;
8955 const struct dwo_unit *dub = b;
3019eac3 8956
80626a55
DE
8957 return dua->signature == dub->signature;
8958}
3019eac3 8959
80626a55 8960/* Allocate a hash table for dwp_file loaded CUs/TUs. */
3019eac3 8961
80626a55
DE
8962static htab_t
8963allocate_dwp_loaded_cutus_table (struct objfile *objfile)
8964{
8965 return htab_create_alloc_ex (3,
8966 hash_dwp_loaded_cutus,
8967 eq_dwp_loaded_cutus,
8968 NULL,
8969 &objfile->objfile_obstack,
8970 hashtab_obstack_allocate,
8971 dummy_obstack_deallocate);
8972}
3019eac3 8973
80626a55
DE
8974/* Initialize the use of the DWP file for the current objfile.
8975 By convention the name of the DWP file is ${objfile}.dwp.
8976 The result is NULL if it can't be found. */
a766d390 8977
80626a55
DE
8978static struct dwp_file *
8979open_and_init_dwp_file (const char *comp_dir)
8980{
8981 struct objfile *objfile = dwarf2_per_objfile->objfile;
8982 struct dwp_file *dwp_file;
8983 char *dwp_name;
8984 bfd *dbfd;
8985 struct cleanup *cleanups;
8986
2792b94d 8987 dwp_name = xstrprintf ("%s.dwp", dwarf2_per_objfile->objfile->name);
80626a55
DE
8988 cleanups = make_cleanup (xfree, dwp_name);
8989
8990 dbfd = open_dwop_file (dwp_name, comp_dir, 1);
8991 if (dbfd == NULL)
8992 {
8993 if (dwarf2_read_debug)
8994 fprintf_unfiltered (gdb_stdlog, "DWP file not found: %s\n", dwp_name);
8995 do_cleanups (cleanups);
8996 return NULL;
3019eac3 8997 }
80626a55
DE
8998 dwp_file = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct dwp_file);
8999 dwp_file->name = obstack_copy0 (&objfile->objfile_obstack,
9000 dwp_name, strlen (dwp_name));
9001 dwp_file->dbfd = dbfd;
9002 do_cleanups (cleanups);
c906108c 9003
80626a55 9004 cleanups = make_cleanup (free_dwo_file_cleanup, dwp_file);
df8a16a1 9005
80626a55
DE
9006 /* +1: section 0 is unused */
9007 dwp_file->num_sections = bfd_count_sections (dbfd) + 1;
9008 dwp_file->elf_sections =
9009 OBSTACK_CALLOC (&objfile->objfile_obstack,
9010 dwp_file->num_sections, asection *);
9011
9012 bfd_map_over_sections (dbfd, dwarf2_locate_dwp_sections, dwp_file);
9013
9014 dwp_file->cus = create_dwp_hash_table (dwp_file, 0);
9015
9016 dwp_file->tus = create_dwp_hash_table (dwp_file, 1);
9017
9018 dwp_file->loaded_cutus = allocate_dwp_loaded_cutus_table (objfile);
9019
9020 discard_cleanups (cleanups);
9021
9022 if (dwarf2_read_debug)
9023 {
9024 fprintf_unfiltered (gdb_stdlog, "DWP file found: %s\n", dwp_file->name);
9025 fprintf_unfiltered (gdb_stdlog,
9026 " %u CUs, %u TUs\n",
9027 dwp_file->cus ? dwp_file->cus->nr_units : 0,
9028 dwp_file->tus ? dwp_file->tus->nr_units : 0);
9029 }
9030
9031 return dwp_file;
3019eac3 9032}
c906108c 9033
80626a55
DE
9034/* Subroutine of lookup_dwo_comp_unit, lookup_dwo_type_unit.
9035 Look up the CU/TU with signature SIGNATURE, either in DWO file DWO_NAME
9036 or in the DWP file for the objfile, referenced by THIS_UNIT.
3019eac3 9037 If non-NULL, comp_dir is the DW_AT_comp_dir attribute.
80626a55
DE
9038 IS_DEBUG_TYPES is non-zero if reading a TU, otherwise read a CU.
9039
9040 This is called, for example, when wanting to read a variable with a
9041 complex location. Therefore we don't want to do file i/o for every call.
9042 Therefore we don't want to look for a DWO file on every call.
9043 Therefore we first see if we've already seen SIGNATURE in a DWP file,
9044 then we check if we've already seen DWO_NAME, and only THEN do we check
9045 for a DWO file.
9046
1c658ad5 9047 The result is a pointer to the dwo_unit object or NULL if we didn't find it
80626a55 9048 (dwo_id mismatch or couldn't find the DWO/DWP file). */
debd256d 9049
3019eac3 9050static struct dwo_unit *
80626a55
DE
9051lookup_dwo_cutu (struct dwarf2_per_cu_data *this_unit,
9052 const char *dwo_name, const char *comp_dir,
9053 ULONGEST signature, int is_debug_types)
3019eac3
DE
9054{
9055 struct objfile *objfile = dwarf2_per_objfile->objfile;
80626a55
DE
9056 const char *kind = is_debug_types ? "TU" : "CU";
9057 void **dwo_file_slot;
3019eac3 9058 struct dwo_file *dwo_file;
80626a55 9059 struct dwp_file *dwp_file;
cb1df416 9060
80626a55 9061 /* Have we already read SIGNATURE from a DWP file? */
cf2c3c16 9062
80626a55
DE
9063 if (! dwarf2_per_objfile->dwp_checked)
9064 {
9065 dwarf2_per_objfile->dwp_file = open_and_init_dwp_file (comp_dir);
9066 dwarf2_per_objfile->dwp_checked = 1;
9067 }
9068 dwp_file = dwarf2_per_objfile->dwp_file;
3019eac3 9069
80626a55 9070 if (dwp_file != NULL)
cf2c3c16 9071 {
80626a55
DE
9072 const struct dwp_hash_table *dwp_htab =
9073 is_debug_types ? dwp_file->tus : dwp_file->cus;
9074
9075 if (dwp_htab != NULL)
9076 {
9077 struct dwo_unit *dwo_cutu =
9078 lookup_dwo_in_dwp (dwp_file, dwp_htab, signature, is_debug_types);
9079
9080 if (dwo_cutu != NULL)
9081 {
9082 if (dwarf2_read_debug)
9083 {
9084 fprintf_unfiltered (gdb_stdlog,
9085 "Virtual DWO %s %s found: @%s\n",
9086 kind, hex_string (signature),
9087 host_address_to_string (dwo_cutu));
9088 }
9089 return dwo_cutu;
9090 }
9091 }
9092 }
9093
9094 /* Have we already seen DWO_NAME? */
9095
9096 dwo_file_slot = lookup_dwo_file_slot (dwo_name);
9097 if (*dwo_file_slot == NULL)
9098 {
9099 /* Read in the file and build a table of the DWOs it contains. */
9100 *dwo_file_slot = open_and_init_dwo_file (dwo_name, comp_dir);
9101 }
9102 /* NOTE: This will be NULL if unable to open the file. */
9103 dwo_file = *dwo_file_slot;
9104
9105 if (dwo_file != NULL)
9106 {
9107 htab_t htab = is_debug_types ? dwo_file->tus : dwo_file->cus;
9108
9109 if (htab != NULL)
9110 {
9111 struct dwo_unit find_dwo_cutu, *dwo_cutu;
9a619af0 9112
80626a55
DE
9113 memset (&find_dwo_cutu, 0, sizeof (find_dwo_cutu));
9114 find_dwo_cutu.signature = signature;
9115 dwo_cutu = htab_find (htab, &find_dwo_cutu);
3019eac3 9116
80626a55
DE
9117 if (dwo_cutu != NULL)
9118 {
9119 if (dwarf2_read_debug)
9120 {
9121 fprintf_unfiltered (gdb_stdlog, "DWO %s %s(%s) found: @%s\n",
9122 kind, dwo_name, hex_string (signature),
9123 host_address_to_string (dwo_cutu));
9124 }
9125 return dwo_cutu;
9126 }
9127 }
2e276125 9128 }
9cdd5dbd 9129
80626a55
DE
9130 /* We didn't find it. This could mean a dwo_id mismatch, or
9131 someone deleted the DWO/DWP file, or the search path isn't set up
9132 correctly to find the file. */
9133
9134 if (dwarf2_read_debug)
9135 {
9136 fprintf_unfiltered (gdb_stdlog, "DWO %s %s(%s) not found\n",
9137 kind, dwo_name, hex_string (signature));
9138 }
3019eac3
DE
9139
9140 complaint (&symfile_complaints,
80626a55 9141 _("Could not find DWO CU referenced by CU at offset 0x%x"
3019eac3 9142 " [in module %s]"),
80626a55 9143 this_unit->offset.sect_off, objfile->name);
3019eac3 9144 return NULL;
5fb290d7
DJ
9145}
9146
80626a55
DE
9147/* Lookup the DWO CU DWO_NAME/SIGNATURE referenced from THIS_CU.
9148 See lookup_dwo_cutu_unit for details. */
9149
9150static struct dwo_unit *
9151lookup_dwo_comp_unit (struct dwarf2_per_cu_data *this_cu,
9152 const char *dwo_name, const char *comp_dir,
9153 ULONGEST signature)
9154{
9155 return lookup_dwo_cutu (this_cu, dwo_name, comp_dir, signature, 0);
9156}
9157
9158/* Lookup the DWO TU DWO_NAME/SIGNATURE referenced from THIS_TU.
9159 See lookup_dwo_cutu_unit for details. */
9160
9161static struct dwo_unit *
9162lookup_dwo_type_unit (struct signatured_type *this_tu,
9163 const char *dwo_name, const char *comp_dir)
9164{
9165 return lookup_dwo_cutu (&this_tu->per_cu, dwo_name, comp_dir, this_tu->signature, 1);
9166}
9167
3019eac3
DE
9168/* Free all resources associated with DWO_FILE.
9169 Close the DWO file and munmap the sections.
9170 All memory should be on the objfile obstack. */
348e048f
DE
9171
9172static void
3019eac3 9173free_dwo_file (struct dwo_file *dwo_file, struct objfile *objfile)
348e048f 9174{
3019eac3
DE
9175 int ix;
9176 struct dwarf2_section_info *section;
348e048f 9177
80626a55
DE
9178 gdb_assert (dwo_file->dbfd != objfile->obfd);
9179 gdb_bfd_unref (dwo_file->dbfd);
348e048f 9180
3019eac3
DE
9181 VEC_free (dwarf2_section_info_def, dwo_file->sections.types);
9182}
348e048f 9183
3019eac3 9184/* Wrapper for free_dwo_file for use in cleanups. */
348e048f 9185
3019eac3
DE
9186static void
9187free_dwo_file_cleanup (void *arg)
9188{
9189 struct dwo_file *dwo_file = (struct dwo_file *) arg;
9190 struct objfile *objfile = dwarf2_per_objfile->objfile;
348e048f 9191
3019eac3
DE
9192 free_dwo_file (dwo_file, objfile);
9193}
348e048f 9194
3019eac3 9195/* Traversal function for free_dwo_files. */
2ab95328 9196
3019eac3
DE
9197static int
9198free_dwo_file_from_slot (void **slot, void *info)
9199{
9200 struct dwo_file *dwo_file = (struct dwo_file *) *slot;
9201 struct objfile *objfile = (struct objfile *) info;
348e048f 9202
3019eac3 9203 free_dwo_file (dwo_file, objfile);
348e048f 9204
3019eac3
DE
9205 return 1;
9206}
348e048f 9207
3019eac3 9208/* Free all resources associated with DWO_FILES. */
348e048f 9209
3019eac3
DE
9210static void
9211free_dwo_files (htab_t dwo_files, struct objfile *objfile)
9212{
9213 htab_traverse_noresize (dwo_files, free_dwo_file_from_slot, objfile);
348e048f 9214}
3019eac3
DE
9215\f
9216/* Read in various DIEs. */
348e048f 9217
d389af10
JK
9218/* qsort helper for inherit_abstract_dies. */
9219
9220static int
9221unsigned_int_compar (const void *ap, const void *bp)
9222{
9223 unsigned int a = *(unsigned int *) ap;
9224 unsigned int b = *(unsigned int *) bp;
9225
9226 return (a > b) - (b > a);
9227}
9228
9229/* DW_AT_abstract_origin inherits whole DIEs (not just their attributes).
3e43a32a
MS
9230 Inherit only the children of the DW_AT_abstract_origin DIE not being
9231 already referenced by DW_AT_abstract_origin from the children of the
9232 current DIE. */
d389af10
JK
9233
9234static void
9235inherit_abstract_dies (struct die_info *die, struct dwarf2_cu *cu)
9236{
9237 struct die_info *child_die;
9238 unsigned die_children_count;
9239 /* CU offsets which were referenced by children of the current DIE. */
b64f50a1
JK
9240 sect_offset *offsets;
9241 sect_offset *offsets_end, *offsetp;
d389af10
JK
9242 /* Parent of DIE - referenced by DW_AT_abstract_origin. */
9243 struct die_info *origin_die;
9244 /* Iterator of the ORIGIN_DIE children. */
9245 struct die_info *origin_child_die;
9246 struct cleanup *cleanups;
9247 struct attribute *attr;
cd02d79d
PA
9248 struct dwarf2_cu *origin_cu;
9249 struct pending **origin_previous_list_in_scope;
d389af10
JK
9250
9251 attr = dwarf2_attr (die, DW_AT_abstract_origin, cu);
9252 if (!attr)
9253 return;
9254
cd02d79d
PA
9255 /* Note that following die references may follow to a die in a
9256 different cu. */
9257
9258 origin_cu = cu;
9259 origin_die = follow_die_ref (die, attr, &origin_cu);
9260
9261 /* We're inheriting ORIGIN's children into the scope we'd put DIE's
9262 symbols in. */
9263 origin_previous_list_in_scope = origin_cu->list_in_scope;
9264 origin_cu->list_in_scope = cu->list_in_scope;
9265
edb3359d
DJ
9266 if (die->tag != origin_die->tag
9267 && !(die->tag == DW_TAG_inlined_subroutine
9268 && origin_die->tag == DW_TAG_subprogram))
d389af10
JK
9269 complaint (&symfile_complaints,
9270 _("DIE 0x%x and its abstract origin 0x%x have different tags"),
b64f50a1 9271 die->offset.sect_off, origin_die->offset.sect_off);
d389af10
JK
9272
9273 child_die = die->child;
9274 die_children_count = 0;
9275 while (child_die && child_die->tag)
9276 {
9277 child_die = sibling_die (child_die);
9278 die_children_count++;
9279 }
9280 offsets = xmalloc (sizeof (*offsets) * die_children_count);
9281 cleanups = make_cleanup (xfree, offsets);
9282
9283 offsets_end = offsets;
9284 child_die = die->child;
9285 while (child_die && child_die->tag)
9286 {
c38f313d
DJ
9287 /* For each CHILD_DIE, find the corresponding child of
9288 ORIGIN_DIE. If there is more than one layer of
9289 DW_AT_abstract_origin, follow them all; there shouldn't be,
9290 but GCC versions at least through 4.4 generate this (GCC PR
9291 40573). */
9292 struct die_info *child_origin_die = child_die;
cd02d79d 9293 struct dwarf2_cu *child_origin_cu = cu;
9a619af0 9294
c38f313d
DJ
9295 while (1)
9296 {
cd02d79d
PA
9297 attr = dwarf2_attr (child_origin_die, DW_AT_abstract_origin,
9298 child_origin_cu);
c38f313d
DJ
9299 if (attr == NULL)
9300 break;
cd02d79d
PA
9301 child_origin_die = follow_die_ref (child_origin_die, attr,
9302 &child_origin_cu);
c38f313d
DJ
9303 }
9304
d389af10
JK
9305 /* According to DWARF3 3.3.8.2 #3 new entries without their abstract
9306 counterpart may exist. */
c38f313d 9307 if (child_origin_die != child_die)
d389af10 9308 {
edb3359d
DJ
9309 if (child_die->tag != child_origin_die->tag
9310 && !(child_die->tag == DW_TAG_inlined_subroutine
9311 && child_origin_die->tag == DW_TAG_subprogram))
d389af10
JK
9312 complaint (&symfile_complaints,
9313 _("Child DIE 0x%x and its abstract origin 0x%x have "
b64f50a1
JK
9314 "different tags"), child_die->offset.sect_off,
9315 child_origin_die->offset.sect_off);
c38f313d
DJ
9316 if (child_origin_die->parent != origin_die)
9317 complaint (&symfile_complaints,
9318 _("Child DIE 0x%x and its abstract origin 0x%x have "
b64f50a1
JK
9319 "different parents"), child_die->offset.sect_off,
9320 child_origin_die->offset.sect_off);
c38f313d
DJ
9321 else
9322 *offsets_end++ = child_origin_die->offset;
d389af10
JK
9323 }
9324 child_die = sibling_die (child_die);
9325 }
9326 qsort (offsets, offsets_end - offsets, sizeof (*offsets),
9327 unsigned_int_compar);
9328 for (offsetp = offsets + 1; offsetp < offsets_end; offsetp++)
b64f50a1 9329 if (offsetp[-1].sect_off == offsetp->sect_off)
3e43a32a
MS
9330 complaint (&symfile_complaints,
9331 _("Multiple children of DIE 0x%x refer "
9332 "to DIE 0x%x as their abstract origin"),
b64f50a1 9333 die->offset.sect_off, offsetp->sect_off);
d389af10
JK
9334
9335 offsetp = offsets;
9336 origin_child_die = origin_die->child;
9337 while (origin_child_die && origin_child_die->tag)
9338 {
9339 /* Is ORIGIN_CHILD_DIE referenced by any of the DIE children? */
b64f50a1
JK
9340 while (offsetp < offsets_end
9341 && offsetp->sect_off < origin_child_die->offset.sect_off)
d389af10 9342 offsetp++;
b64f50a1
JK
9343 if (offsetp >= offsets_end
9344 || offsetp->sect_off > origin_child_die->offset.sect_off)
d389af10
JK
9345 {
9346 /* Found that ORIGIN_CHILD_DIE is really not referenced. */
cd02d79d 9347 process_die (origin_child_die, origin_cu);
d389af10
JK
9348 }
9349 origin_child_die = sibling_die (origin_child_die);
9350 }
cd02d79d 9351 origin_cu->list_in_scope = origin_previous_list_in_scope;
d389af10
JK
9352
9353 do_cleanups (cleanups);
9354}
9355
c906108c 9356static void
e7c27a73 9357read_func_scope (struct die_info *die, struct dwarf2_cu *cu)
c906108c 9358{
e7c27a73 9359 struct objfile *objfile = cu->objfile;
52f0bd74 9360 struct context_stack *new;
c906108c
SS
9361 CORE_ADDR lowpc;
9362 CORE_ADDR highpc;
9363 struct die_info *child_die;
edb3359d 9364 struct attribute *attr, *call_line, *call_file;
c906108c 9365 char *name;
e142c38c 9366 CORE_ADDR baseaddr;
801e3a5b 9367 struct block *block;
edb3359d 9368 int inlined_func = (die->tag == DW_TAG_inlined_subroutine);
34eaf542
TT
9369 VEC (symbolp) *template_args = NULL;
9370 struct template_symbol *templ_func = NULL;
edb3359d
DJ
9371
9372 if (inlined_func)
9373 {
9374 /* If we do not have call site information, we can't show the
9375 caller of this inlined function. That's too confusing, so
9376 only use the scope for local variables. */
9377 call_line = dwarf2_attr (die, DW_AT_call_line, cu);
9378 call_file = dwarf2_attr (die, DW_AT_call_file, cu);
9379 if (call_line == NULL || call_file == NULL)
9380 {
9381 read_lexical_block_scope (die, cu);
9382 return;
9383 }
9384 }
c906108c 9385
e142c38c
DJ
9386 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
9387
94af9270 9388 name = dwarf2_name (die, cu);
c906108c 9389
e8d05480
JB
9390 /* Ignore functions with missing or empty names. These are actually
9391 illegal according to the DWARF standard. */
9392 if (name == NULL)
9393 {
9394 complaint (&symfile_complaints,
b64f50a1
JK
9395 _("missing name for subprogram DIE at %d"),
9396 die->offset.sect_off);
e8d05480
JB
9397 return;
9398 }
9399
9400 /* Ignore functions with missing or invalid low and high pc attributes. */
9401 if (!dwarf2_get_pc_bounds (die, &lowpc, &highpc, cu, NULL))
9402 {
ae4d0c03
PM
9403 attr = dwarf2_attr (die, DW_AT_external, cu);
9404 if (!attr || !DW_UNSND (attr))
9405 complaint (&symfile_complaints,
3e43a32a
MS
9406 _("cannot get low and high bounds "
9407 "for subprogram DIE at %d"),
b64f50a1 9408 die->offset.sect_off);
e8d05480
JB
9409 return;
9410 }
c906108c
SS
9411
9412 lowpc += baseaddr;
9413 highpc += baseaddr;
9414
34eaf542
TT
9415 /* If we have any template arguments, then we must allocate a
9416 different sort of symbol. */
9417 for (child_die = die->child; child_die; child_die = sibling_die (child_die))
9418 {
9419 if (child_die->tag == DW_TAG_template_type_param
9420 || child_die->tag == DW_TAG_template_value_param)
9421 {
9422 templ_func = OBSTACK_ZALLOC (&objfile->objfile_obstack,
9423 struct template_symbol);
9424 templ_func->base.is_cplus_template_function = 1;
9425 break;
9426 }
9427 }
9428
c906108c 9429 new = push_context (0, lowpc);
34eaf542
TT
9430 new->name = new_symbol_full (die, read_type_die (die, cu), cu,
9431 (struct symbol *) templ_func);
4c2df51b 9432
4cecd739
DJ
9433 /* If there is a location expression for DW_AT_frame_base, record
9434 it. */
e142c38c 9435 attr = dwarf2_attr (die, DW_AT_frame_base, cu);
4c2df51b 9436 if (attr)
c034e007
AC
9437 /* FIXME: cagney/2004-01-26: The DW_AT_frame_base's location
9438 expression is being recorded directly in the function's symbol
9439 and not in a separate frame-base object. I guess this hack is
9440 to avoid adding some sort of frame-base adjunct/annex to the
9441 function's symbol :-(. The problem with doing this is that it
9442 results in a function symbol with a location expression that
9443 has nothing to do with the location of the function, ouch! The
9444 relationship should be: a function's symbol has-a frame base; a
9445 frame-base has-a location expression. */
e7c27a73 9446 dwarf2_symbol_mark_computed (attr, new->name, cu);
4c2df51b 9447
e142c38c 9448 cu->list_in_scope = &local_symbols;
c906108c 9449
639d11d3 9450 if (die->child != NULL)
c906108c 9451 {
639d11d3 9452 child_die = die->child;
c906108c
SS
9453 while (child_die && child_die->tag)
9454 {
34eaf542
TT
9455 if (child_die->tag == DW_TAG_template_type_param
9456 || child_die->tag == DW_TAG_template_value_param)
9457 {
9458 struct symbol *arg = new_symbol (child_die, NULL, cu);
9459
f1078f66
DJ
9460 if (arg != NULL)
9461 VEC_safe_push (symbolp, template_args, arg);
34eaf542
TT
9462 }
9463 else
9464 process_die (child_die, cu);
c906108c
SS
9465 child_die = sibling_die (child_die);
9466 }
9467 }
9468
d389af10
JK
9469 inherit_abstract_dies (die, cu);
9470
4a811a97
UW
9471 /* If we have a DW_AT_specification, we might need to import using
9472 directives from the context of the specification DIE. See the
9473 comment in determine_prefix. */
9474 if (cu->language == language_cplus
9475 && dwarf2_attr (die, DW_AT_specification, cu))
9476 {
9477 struct dwarf2_cu *spec_cu = cu;
9478 struct die_info *spec_die = die_specification (die, &spec_cu);
9479
9480 while (spec_die)
9481 {
9482 child_die = spec_die->child;
9483 while (child_die && child_die->tag)
9484 {
9485 if (child_die->tag == DW_TAG_imported_module)
9486 process_die (child_die, spec_cu);
9487 child_die = sibling_die (child_die);
9488 }
9489
9490 /* In some cases, GCC generates specification DIEs that
9491 themselves contain DW_AT_specification attributes. */
9492 spec_die = die_specification (spec_die, &spec_cu);
9493 }
9494 }
9495
c906108c
SS
9496 new = pop_context ();
9497 /* Make a block for the local symbols within. */
801e3a5b
JB
9498 block = finish_block (new->name, &local_symbols, new->old_blocks,
9499 lowpc, highpc, objfile);
9500
df8a16a1 9501 /* For C++, set the block's scope. */
f55ee35c 9502 if (cu->language == language_cplus || cu->language == language_fortran)
df8a16a1 9503 cp_set_block_scope (new->name, block, &objfile->objfile_obstack,
0114d602 9504 determine_prefix (die, cu),
df8a16a1
DJ
9505 processing_has_namespace_info);
9506
801e3a5b
JB
9507 /* If we have address ranges, record them. */
9508 dwarf2_record_block_ranges (die, block, baseaddr, cu);
6e70227d 9509
34eaf542
TT
9510 /* Attach template arguments to function. */
9511 if (! VEC_empty (symbolp, template_args))
9512 {
9513 gdb_assert (templ_func != NULL);
9514
9515 templ_func->n_template_arguments = VEC_length (symbolp, template_args);
9516 templ_func->template_arguments
9517 = obstack_alloc (&objfile->objfile_obstack,
9518 (templ_func->n_template_arguments
9519 * sizeof (struct symbol *)));
9520 memcpy (templ_func->template_arguments,
9521 VEC_address (symbolp, template_args),
9522 (templ_func->n_template_arguments * sizeof (struct symbol *)));
9523 VEC_free (symbolp, template_args);
9524 }
9525
208d8187
JB
9526 /* In C++, we can have functions nested inside functions (e.g., when
9527 a function declares a class that has methods). This means that
9528 when we finish processing a function scope, we may need to go
9529 back to building a containing block's symbol lists. */
9530 local_symbols = new->locals;
27aa8d6a 9531 using_directives = new->using_directives;
208d8187 9532
921e78cf
JB
9533 /* If we've finished processing a top-level function, subsequent
9534 symbols go in the file symbol list. */
9535 if (outermost_context_p ())
e142c38c 9536 cu->list_in_scope = &file_symbols;
c906108c
SS
9537}
9538
9539/* Process all the DIES contained within a lexical block scope. Start
9540 a new scope, process the dies, and then close the scope. */
9541
9542static void
e7c27a73 9543read_lexical_block_scope (struct die_info *die, struct dwarf2_cu *cu)
c906108c 9544{
e7c27a73 9545 struct objfile *objfile = cu->objfile;
52f0bd74 9546 struct context_stack *new;
c906108c
SS
9547 CORE_ADDR lowpc, highpc;
9548 struct die_info *child_die;
e142c38c
DJ
9549 CORE_ADDR baseaddr;
9550
9551 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
c906108c
SS
9552
9553 /* Ignore blocks with missing or invalid low and high pc attributes. */
af34e669
DJ
9554 /* ??? Perhaps consider discontiguous blocks defined by DW_AT_ranges
9555 as multiple lexical blocks? Handling children in a sane way would
6e70227d 9556 be nasty. Might be easier to properly extend generic blocks to
af34e669 9557 describe ranges. */
d85a05f0 9558 if (!dwarf2_get_pc_bounds (die, &lowpc, &highpc, cu, NULL))
c906108c
SS
9559 return;
9560 lowpc += baseaddr;
9561 highpc += baseaddr;
9562
9563 push_context (0, lowpc);
639d11d3 9564 if (die->child != NULL)
c906108c 9565 {
639d11d3 9566 child_die = die->child;
c906108c
SS
9567 while (child_die && child_die->tag)
9568 {
e7c27a73 9569 process_die (child_die, cu);
c906108c
SS
9570 child_die = sibling_die (child_die);
9571 }
9572 }
9573 new = pop_context ();
9574
8540c487 9575 if (local_symbols != NULL || using_directives != NULL)
c906108c 9576 {
801e3a5b
JB
9577 struct block *block
9578 = finish_block (0, &local_symbols, new->old_blocks, new->start_addr,
9579 highpc, objfile);
9580
9581 /* Note that recording ranges after traversing children, as we
9582 do here, means that recording a parent's ranges entails
9583 walking across all its children's ranges as they appear in
9584 the address map, which is quadratic behavior.
9585
9586 It would be nicer to record the parent's ranges before
9587 traversing its children, simply overriding whatever you find
9588 there. But since we don't even decide whether to create a
9589 block until after we've traversed its children, that's hard
9590 to do. */
9591 dwarf2_record_block_ranges (die, block, baseaddr, cu);
c906108c
SS
9592 }
9593 local_symbols = new->locals;
27aa8d6a 9594 using_directives = new->using_directives;
c906108c
SS
9595}
9596
96408a79
SA
9597/* Read in DW_TAG_GNU_call_site and insert it to CU->call_site_htab. */
9598
9599static void
9600read_call_site_scope (struct die_info *die, struct dwarf2_cu *cu)
9601{
9602 struct objfile *objfile = cu->objfile;
9603 struct gdbarch *gdbarch = get_objfile_arch (objfile);
9604 CORE_ADDR pc, baseaddr;
9605 struct attribute *attr;
9606 struct call_site *call_site, call_site_local;
9607 void **slot;
9608 int nparams;
9609 struct die_info *child_die;
9610
9611 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
9612
9613 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
9614 if (!attr)
9615 {
9616 complaint (&symfile_complaints,
9617 _("missing DW_AT_low_pc for DW_TAG_GNU_call_site "
9618 "DIE 0x%x [in module %s]"),
b64f50a1 9619 die->offset.sect_off, objfile->name);
96408a79
SA
9620 return;
9621 }
9622 pc = DW_ADDR (attr) + baseaddr;
9623
9624 if (cu->call_site_htab == NULL)
9625 cu->call_site_htab = htab_create_alloc_ex (16, core_addr_hash, core_addr_eq,
9626 NULL, &objfile->objfile_obstack,
9627 hashtab_obstack_allocate, NULL);
9628 call_site_local.pc = pc;
9629 slot = htab_find_slot (cu->call_site_htab, &call_site_local, INSERT);
9630 if (*slot != NULL)
9631 {
9632 complaint (&symfile_complaints,
9633 _("Duplicate PC %s for DW_TAG_GNU_call_site "
9634 "DIE 0x%x [in module %s]"),
b64f50a1 9635 paddress (gdbarch, pc), die->offset.sect_off, objfile->name);
96408a79
SA
9636 return;
9637 }
9638
9639 /* Count parameters at the caller. */
9640
9641 nparams = 0;
9642 for (child_die = die->child; child_die && child_die->tag;
9643 child_die = sibling_die (child_die))
9644 {
9645 if (child_die->tag != DW_TAG_GNU_call_site_parameter)
9646 {
9647 complaint (&symfile_complaints,
9648 _("Tag %d is not DW_TAG_GNU_call_site_parameter in "
9649 "DW_TAG_GNU_call_site child DIE 0x%x [in module %s]"),
b64f50a1 9650 child_die->tag, child_die->offset.sect_off, objfile->name);
96408a79
SA
9651 continue;
9652 }
9653
9654 nparams++;
9655 }
9656
9657 call_site = obstack_alloc (&objfile->objfile_obstack,
9658 (sizeof (*call_site)
9659 + (sizeof (*call_site->parameter)
9660 * (nparams - 1))));
9661 *slot = call_site;
9662 memset (call_site, 0, sizeof (*call_site) - sizeof (*call_site->parameter));
9663 call_site->pc = pc;
9664
9665 if (dwarf2_flag_true_p (die, DW_AT_GNU_tail_call, cu))
9666 {
9667 struct die_info *func_die;
9668
9669 /* Skip also over DW_TAG_inlined_subroutine. */
9670 for (func_die = die->parent;
9671 func_die && func_die->tag != DW_TAG_subprogram
9672 && func_die->tag != DW_TAG_subroutine_type;
9673 func_die = func_die->parent);
9674
9675 /* DW_AT_GNU_all_call_sites is a superset
9676 of DW_AT_GNU_all_tail_call_sites. */
9677 if (func_die
9678 && !dwarf2_flag_true_p (func_die, DW_AT_GNU_all_call_sites, cu)
9679 && !dwarf2_flag_true_p (func_die, DW_AT_GNU_all_tail_call_sites, cu))
9680 {
9681 /* TYPE_TAIL_CALL_LIST is not interesting in functions where it is
9682 not complete. But keep CALL_SITE for look ups via call_site_htab,
9683 both the initial caller containing the real return address PC and
9684 the final callee containing the current PC of a chain of tail
9685 calls do not need to have the tail call list complete. But any
9686 function candidate for a virtual tail call frame searched via
9687 TYPE_TAIL_CALL_LIST must have the tail call list complete to be
9688 determined unambiguously. */
9689 }
9690 else
9691 {
9692 struct type *func_type = NULL;
9693
9694 if (func_die)
9695 func_type = get_die_type (func_die, cu);
9696 if (func_type != NULL)
9697 {
9698 gdb_assert (TYPE_CODE (func_type) == TYPE_CODE_FUNC);
9699
9700 /* Enlist this call site to the function. */
9701 call_site->tail_call_next = TYPE_TAIL_CALL_LIST (func_type);
9702 TYPE_TAIL_CALL_LIST (func_type) = call_site;
9703 }
9704 else
9705 complaint (&symfile_complaints,
9706 _("Cannot find function owning DW_TAG_GNU_call_site "
9707 "DIE 0x%x [in module %s]"),
b64f50a1 9708 die->offset.sect_off, objfile->name);
96408a79
SA
9709 }
9710 }
9711
9712 attr = dwarf2_attr (die, DW_AT_GNU_call_site_target, cu);
9713 if (attr == NULL)
9714 attr = dwarf2_attr (die, DW_AT_abstract_origin, cu);
9715 SET_FIELD_DWARF_BLOCK (call_site->target, NULL);
9716 if (!attr || (attr_form_is_block (attr) && DW_BLOCK (attr)->size == 0))
9717 /* Keep NULL DWARF_BLOCK. */;
9718 else if (attr_form_is_block (attr))
9719 {
9720 struct dwarf2_locexpr_baton *dlbaton;
9721
9722 dlbaton = obstack_alloc (&objfile->objfile_obstack, sizeof (*dlbaton));
9723 dlbaton->data = DW_BLOCK (attr)->data;
9724 dlbaton->size = DW_BLOCK (attr)->size;
9725 dlbaton->per_cu = cu->per_cu;
9726
9727 SET_FIELD_DWARF_BLOCK (call_site->target, dlbaton);
9728 }
9729 else if (is_ref_attr (attr))
9730 {
96408a79
SA
9731 struct dwarf2_cu *target_cu = cu;
9732 struct die_info *target_die;
9733
9734 target_die = follow_die_ref_or_sig (die, attr, &target_cu);
9735 gdb_assert (target_cu->objfile == objfile);
9736 if (die_is_declaration (target_die, target_cu))
9737 {
9738 const char *target_physname;
9739
9740 target_physname = dwarf2_physname (NULL, target_die, target_cu);
9741 if (target_physname == NULL)
9742 complaint (&symfile_complaints,
9743 _("DW_AT_GNU_call_site_target target DIE has invalid "
9744 "physname, for referencing DIE 0x%x [in module %s]"),
b64f50a1 9745 die->offset.sect_off, objfile->name);
96408a79
SA
9746 else
9747 SET_FIELD_PHYSNAME (call_site->target, (char *) target_physname);
9748 }
9749 else
9750 {
9751 CORE_ADDR lowpc;
9752
9753 /* DW_AT_entry_pc should be preferred. */
9754 if (!dwarf2_get_pc_bounds (target_die, &lowpc, NULL, target_cu, NULL))
9755 complaint (&symfile_complaints,
9756 _("DW_AT_GNU_call_site_target target DIE has invalid "
9757 "low pc, for referencing DIE 0x%x [in module %s]"),
b64f50a1 9758 die->offset.sect_off, objfile->name);
96408a79
SA
9759 else
9760 SET_FIELD_PHYSADDR (call_site->target, lowpc + baseaddr);
9761 }
9762 }
9763 else
9764 complaint (&symfile_complaints,
9765 _("DW_TAG_GNU_call_site DW_AT_GNU_call_site_target is neither "
9766 "block nor reference, for DIE 0x%x [in module %s]"),
b64f50a1 9767 die->offset.sect_off, objfile->name);
96408a79
SA
9768
9769 call_site->per_cu = cu->per_cu;
9770
9771 for (child_die = die->child;
9772 child_die && child_die->tag;
9773 child_die = sibling_die (child_die))
9774 {
96408a79 9775 struct call_site_parameter *parameter;
1788b2d3 9776 struct attribute *loc, *origin;
96408a79
SA
9777
9778 if (child_die->tag != DW_TAG_GNU_call_site_parameter)
9779 {
9780 /* Already printed the complaint above. */
9781 continue;
9782 }
9783
9784 gdb_assert (call_site->parameter_count < nparams);
9785 parameter = &call_site->parameter[call_site->parameter_count];
9786
1788b2d3
JK
9787 /* DW_AT_location specifies the register number or DW_AT_abstract_origin
9788 specifies DW_TAG_formal_parameter. Value of the data assumed for the
9789 register is contained in DW_AT_GNU_call_site_value. */
96408a79 9790
24c5c679 9791 loc = dwarf2_attr (child_die, DW_AT_location, cu);
1788b2d3
JK
9792 origin = dwarf2_attr (child_die, DW_AT_abstract_origin, cu);
9793 if (loc == NULL && origin != NULL && is_ref_attr (origin))
9794 {
9795 sect_offset offset;
9796
9797 parameter->kind = CALL_SITE_PARAMETER_PARAM_OFFSET;
9798 offset = dwarf2_get_ref_die_offset (origin);
d76b7dbc
JK
9799 if (!offset_in_cu_p (&cu->header, offset))
9800 {
9801 /* As DW_OP_GNU_parameter_ref uses CU-relative offset this
9802 binding can be done only inside one CU. Such referenced DIE
9803 therefore cannot be even moved to DW_TAG_partial_unit. */
9804 complaint (&symfile_complaints,
9805 _("DW_AT_abstract_origin offset is not in CU for "
9806 "DW_TAG_GNU_call_site child DIE 0x%x "
9807 "[in module %s]"),
9808 child_die->offset.sect_off, objfile->name);
9809 continue;
9810 }
1788b2d3
JK
9811 parameter->u.param_offset.cu_off = (offset.sect_off
9812 - cu->header.offset.sect_off);
9813 }
9814 else if (loc == NULL || origin != NULL || !attr_form_is_block (loc))
96408a79
SA
9815 {
9816 complaint (&symfile_complaints,
9817 _("No DW_FORM_block* DW_AT_location for "
9818 "DW_TAG_GNU_call_site child DIE 0x%x [in module %s]"),
b64f50a1 9819 child_die->offset.sect_off, objfile->name);
96408a79
SA
9820 continue;
9821 }
24c5c679 9822 else
96408a79 9823 {
24c5c679
JK
9824 parameter->u.dwarf_reg = dwarf_block_to_dwarf_reg
9825 (DW_BLOCK (loc)->data, &DW_BLOCK (loc)->data[DW_BLOCK (loc)->size]);
9826 if (parameter->u.dwarf_reg != -1)
9827 parameter->kind = CALL_SITE_PARAMETER_DWARF_REG;
9828 else if (dwarf_block_to_sp_offset (gdbarch, DW_BLOCK (loc)->data,
9829 &DW_BLOCK (loc)->data[DW_BLOCK (loc)->size],
9830 &parameter->u.fb_offset))
9831 parameter->kind = CALL_SITE_PARAMETER_FB_OFFSET;
9832 else
9833 {
9834 complaint (&symfile_complaints,
9835 _("Only single DW_OP_reg or DW_OP_fbreg is supported "
9836 "for DW_FORM_block* DW_AT_location is supported for "
9837 "DW_TAG_GNU_call_site child DIE 0x%x "
9838 "[in module %s]"),
9839 child_die->offset.sect_off, objfile->name);
9840 continue;
9841 }
96408a79
SA
9842 }
9843
9844 attr = dwarf2_attr (child_die, DW_AT_GNU_call_site_value, cu);
9845 if (!attr_form_is_block (attr))
9846 {
9847 complaint (&symfile_complaints,
9848 _("No DW_FORM_block* DW_AT_GNU_call_site_value for "
9849 "DW_TAG_GNU_call_site child DIE 0x%x [in module %s]"),
b64f50a1 9850 child_die->offset.sect_off, objfile->name);
96408a79
SA
9851 continue;
9852 }
9853 parameter->value = DW_BLOCK (attr)->data;
9854 parameter->value_size = DW_BLOCK (attr)->size;
9855
9856 /* Parameters are not pre-cleared by memset above. */
9857 parameter->data_value = NULL;
9858 parameter->data_value_size = 0;
9859 call_site->parameter_count++;
9860
9861 attr = dwarf2_attr (child_die, DW_AT_GNU_call_site_data_value, cu);
9862 if (attr)
9863 {
9864 if (!attr_form_is_block (attr))
9865 complaint (&symfile_complaints,
9866 _("No DW_FORM_block* DW_AT_GNU_call_site_data_value for "
9867 "DW_TAG_GNU_call_site child DIE 0x%x [in module %s]"),
b64f50a1 9868 child_die->offset.sect_off, objfile->name);
96408a79
SA
9869 else
9870 {
9871 parameter->data_value = DW_BLOCK (attr)->data;
9872 parameter->data_value_size = DW_BLOCK (attr)->size;
9873 }
9874 }
9875 }
9876}
9877
43039443 9878/* Get low and high pc attributes from DW_AT_ranges attribute value OFFSET.
ff013f42
JK
9879 Return 1 if the attributes are present and valid, otherwise, return 0.
9880 If RANGES_PST is not NULL we should setup `objfile->psymtabs_addrmap'. */
43039443
JK
9881
9882static int
9883dwarf2_ranges_read (unsigned offset, CORE_ADDR *low_return,
ff013f42
JK
9884 CORE_ADDR *high_return, struct dwarf2_cu *cu,
9885 struct partial_symtab *ranges_pst)
43039443
JK
9886{
9887 struct objfile *objfile = cu->objfile;
9888 struct comp_unit_head *cu_header = &cu->header;
9889 bfd *obfd = objfile->obfd;
9890 unsigned int addr_size = cu_header->addr_size;
9891 CORE_ADDR mask = ~(~(CORE_ADDR)1 << (addr_size * 8 - 1));
9892 /* Base address selection entry. */
9893 CORE_ADDR base;
9894 int found_base;
9895 unsigned int dummy;
9896 gdb_byte *buffer;
9897 CORE_ADDR marker;
9898 int low_set;
9899 CORE_ADDR low = 0;
9900 CORE_ADDR high = 0;
ff013f42 9901 CORE_ADDR baseaddr;
43039443 9902
d00adf39
DE
9903 found_base = cu->base_known;
9904 base = cu->base_address;
43039443 9905
be391dca 9906 dwarf2_read_section (objfile, &dwarf2_per_objfile->ranges);
dce234bc 9907 if (offset >= dwarf2_per_objfile->ranges.size)
43039443
JK
9908 {
9909 complaint (&symfile_complaints,
9910 _("Offset %d out of bounds for DW_AT_ranges attribute"),
9911 offset);
9912 return 0;
9913 }
dce234bc 9914 buffer = dwarf2_per_objfile->ranges.buffer + offset;
43039443
JK
9915
9916 /* Read in the largest possible address. */
9917 marker = read_address (obfd, buffer, cu, &dummy);
9918 if ((marker & mask) == mask)
9919 {
9920 /* If we found the largest possible address, then
9921 read the base address. */
9922 base = read_address (obfd, buffer + addr_size, cu, &dummy);
9923 buffer += 2 * addr_size;
9924 offset += 2 * addr_size;
9925 found_base = 1;
9926 }
9927
9928 low_set = 0;
9929
e7030f15 9930 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
ff013f42 9931
43039443
JK
9932 while (1)
9933 {
9934 CORE_ADDR range_beginning, range_end;
9935
9936 range_beginning = read_address (obfd, buffer, cu, &dummy);
9937 buffer += addr_size;
9938 range_end = read_address (obfd, buffer, cu, &dummy);
9939 buffer += addr_size;
9940 offset += 2 * addr_size;
9941
9942 /* An end of list marker is a pair of zero addresses. */
9943 if (range_beginning == 0 && range_end == 0)
9944 /* Found the end of list entry. */
9945 break;
9946
9947 /* Each base address selection entry is a pair of 2 values.
9948 The first is the largest possible address, the second is
9949 the base address. Check for a base address here. */
9950 if ((range_beginning & mask) == mask)
9951 {
9952 /* If we found the largest possible address, then
9953 read the base address. */
9954 base = read_address (obfd, buffer + addr_size, cu, &dummy);
9955 found_base = 1;
9956 continue;
9957 }
9958
9959 if (!found_base)
9960 {
9961 /* We have no valid base address for the ranges
9962 data. */
9963 complaint (&symfile_complaints,
9964 _("Invalid .debug_ranges data (no base address)"));
9965 return 0;
9966 }
9967
9277c30c
UW
9968 if (range_beginning > range_end)
9969 {
9970 /* Inverted range entries are invalid. */
9971 complaint (&symfile_complaints,
9972 _("Invalid .debug_ranges data (inverted range)"));
9973 return 0;
9974 }
9975
9976 /* Empty range entries have no effect. */
9977 if (range_beginning == range_end)
9978 continue;
9979
43039443
JK
9980 range_beginning += base;
9981 range_end += base;
9982
01093045
DE
9983 /* A not-uncommon case of bad debug info.
9984 Don't pollute the addrmap with bad data. */
9985 if (range_beginning + baseaddr == 0
9986 && !dwarf2_per_objfile->has_section_at_zero)
9987 {
9988 complaint (&symfile_complaints,
9989 _(".debug_ranges entry has start address of zero"
9990 " [in module %s]"), objfile->name);
9991 continue;
9992 }
9993
9277c30c 9994 if (ranges_pst != NULL)
ff013f42 9995 addrmap_set_empty (objfile->psymtabs_addrmap,
3e43a32a
MS
9996 range_beginning + baseaddr,
9997 range_end - 1 + baseaddr,
ff013f42
JK
9998 ranges_pst);
9999
43039443
JK
10000 /* FIXME: This is recording everything as a low-high
10001 segment of consecutive addresses. We should have a
10002 data structure for discontiguous block ranges
10003 instead. */
10004 if (! low_set)
10005 {
10006 low = range_beginning;
10007 high = range_end;
10008 low_set = 1;
10009 }
10010 else
10011 {
10012 if (range_beginning < low)
10013 low = range_beginning;
10014 if (range_end > high)
10015 high = range_end;
10016 }
10017 }
10018
10019 if (! low_set)
10020 /* If the first entry is an end-of-list marker, the range
10021 describes an empty scope, i.e. no instructions. */
10022 return 0;
10023
10024 if (low_return)
10025 *low_return = low;
10026 if (high_return)
10027 *high_return = high;
10028 return 1;
10029}
10030
af34e669
DJ
10031/* Get low and high pc attributes from a die. Return 1 if the attributes
10032 are present and valid, otherwise, return 0. Return -1 if the range is
10033 discontinuous, i.e. derived from DW_AT_ranges information. */
380bca97 10034
c906108c 10035static int
af34e669 10036dwarf2_get_pc_bounds (struct die_info *die, CORE_ADDR *lowpc,
d85a05f0
DJ
10037 CORE_ADDR *highpc, struct dwarf2_cu *cu,
10038 struct partial_symtab *pst)
c906108c
SS
10039{
10040 struct attribute *attr;
91da1414 10041 struct attribute *attr_high;
af34e669
DJ
10042 CORE_ADDR low = 0;
10043 CORE_ADDR high = 0;
10044 int ret = 0;
c906108c 10045
91da1414
MW
10046 attr_high = dwarf2_attr (die, DW_AT_high_pc, cu);
10047 if (attr_high)
af34e669 10048 {
e142c38c 10049 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
af34e669 10050 if (attr)
91da1414
MW
10051 {
10052 low = DW_ADDR (attr);
3019eac3
DE
10053 if (attr_high->form == DW_FORM_addr
10054 || attr_high->form == DW_FORM_GNU_addr_index)
91da1414
MW
10055 high = DW_ADDR (attr_high);
10056 else
10057 high = low + DW_UNSND (attr_high);
10058 }
af34e669
DJ
10059 else
10060 /* Found high w/o low attribute. */
10061 return 0;
10062
10063 /* Found consecutive range of addresses. */
10064 ret = 1;
10065 }
c906108c 10066 else
af34e669 10067 {
e142c38c 10068 attr = dwarf2_attr (die, DW_AT_ranges, cu);
af34e669
DJ
10069 if (attr != NULL)
10070 {
2e3cf129
DE
10071 unsigned int ranges_offset = DW_UNSND (attr) + cu->ranges_base;
10072
af34e669 10073 /* Value of the DW_AT_ranges attribute is the offset in the
a604369a 10074 .debug_ranges section. */
2e3cf129 10075 if (!dwarf2_ranges_read (ranges_offset, &low, &high, cu, pst))
af34e669 10076 return 0;
43039443 10077 /* Found discontinuous range of addresses. */
af34e669
DJ
10078 ret = -1;
10079 }
10080 }
c906108c 10081
9373cf26
JK
10082 /* read_partial_die has also the strict LOW < HIGH requirement. */
10083 if (high <= low)
c906108c
SS
10084 return 0;
10085
10086 /* When using the GNU linker, .gnu.linkonce. sections are used to
10087 eliminate duplicate copies of functions and vtables and such.
10088 The linker will arbitrarily choose one and discard the others.
10089 The AT_*_pc values for such functions refer to local labels in
10090 these sections. If the section from that file was discarded, the
10091 labels are not in the output, so the relocs get a value of 0.
10092 If this is a discarded function, mark the pc bounds as invalid,
10093 so that GDB will ignore it. */
72dca2f5 10094 if (low == 0 && !dwarf2_per_objfile->has_section_at_zero)
c906108c
SS
10095 return 0;
10096
10097 *lowpc = low;
96408a79
SA
10098 if (highpc)
10099 *highpc = high;
af34e669 10100 return ret;
c906108c
SS
10101}
10102
b084d499
JB
10103/* Assuming that DIE represents a subprogram DIE or a lexical block, get
10104 its low and high PC addresses. Do nothing if these addresses could not
10105 be determined. Otherwise, set LOWPC to the low address if it is smaller,
10106 and HIGHPC to the high address if greater than HIGHPC. */
10107
10108static void
10109dwarf2_get_subprogram_pc_bounds (struct die_info *die,
10110 CORE_ADDR *lowpc, CORE_ADDR *highpc,
10111 struct dwarf2_cu *cu)
10112{
10113 CORE_ADDR low, high;
10114 struct die_info *child = die->child;
10115
d85a05f0 10116 if (dwarf2_get_pc_bounds (die, &low, &high, cu, NULL))
b084d499
JB
10117 {
10118 *lowpc = min (*lowpc, low);
10119 *highpc = max (*highpc, high);
10120 }
10121
10122 /* If the language does not allow nested subprograms (either inside
10123 subprograms or lexical blocks), we're done. */
10124 if (cu->language != language_ada)
10125 return;
6e70227d 10126
b084d499
JB
10127 /* Check all the children of the given DIE. If it contains nested
10128 subprograms, then check their pc bounds. Likewise, we need to
10129 check lexical blocks as well, as they may also contain subprogram
10130 definitions. */
10131 while (child && child->tag)
10132 {
10133 if (child->tag == DW_TAG_subprogram
10134 || child->tag == DW_TAG_lexical_block)
10135 dwarf2_get_subprogram_pc_bounds (child, lowpc, highpc, cu);
10136 child = sibling_die (child);
10137 }
10138}
10139
fae299cd
DC
10140/* Get the low and high pc's represented by the scope DIE, and store
10141 them in *LOWPC and *HIGHPC. If the correct values can't be
10142 determined, set *LOWPC to -1 and *HIGHPC to 0. */
10143
10144static void
10145get_scope_pc_bounds (struct die_info *die,
10146 CORE_ADDR *lowpc, CORE_ADDR *highpc,
10147 struct dwarf2_cu *cu)
10148{
10149 CORE_ADDR best_low = (CORE_ADDR) -1;
10150 CORE_ADDR best_high = (CORE_ADDR) 0;
10151 CORE_ADDR current_low, current_high;
10152
d85a05f0 10153 if (dwarf2_get_pc_bounds (die, &current_low, &current_high, cu, NULL))
fae299cd
DC
10154 {
10155 best_low = current_low;
10156 best_high = current_high;
10157 }
10158 else
10159 {
10160 struct die_info *child = die->child;
10161
10162 while (child && child->tag)
10163 {
10164 switch (child->tag) {
10165 case DW_TAG_subprogram:
b084d499 10166 dwarf2_get_subprogram_pc_bounds (child, &best_low, &best_high, cu);
fae299cd
DC
10167 break;
10168 case DW_TAG_namespace:
f55ee35c 10169 case DW_TAG_module:
fae299cd
DC
10170 /* FIXME: carlton/2004-01-16: Should we do this for
10171 DW_TAG_class_type/DW_TAG_structure_type, too? I think
10172 that current GCC's always emit the DIEs corresponding
10173 to definitions of methods of classes as children of a
10174 DW_TAG_compile_unit or DW_TAG_namespace (as opposed to
10175 the DIEs giving the declarations, which could be
10176 anywhere). But I don't see any reason why the
10177 standards says that they have to be there. */
10178 get_scope_pc_bounds (child, &current_low, &current_high, cu);
10179
10180 if (current_low != ((CORE_ADDR) -1))
10181 {
10182 best_low = min (best_low, current_low);
10183 best_high = max (best_high, current_high);
10184 }
10185 break;
10186 default:
0963b4bd 10187 /* Ignore. */
fae299cd
DC
10188 break;
10189 }
10190
10191 child = sibling_die (child);
10192 }
10193 }
10194
10195 *lowpc = best_low;
10196 *highpc = best_high;
10197}
10198
801e3a5b
JB
10199/* Record the address ranges for BLOCK, offset by BASEADDR, as given
10200 in DIE. */
380bca97 10201
801e3a5b
JB
10202static void
10203dwarf2_record_block_ranges (struct die_info *die, struct block *block,
10204 CORE_ADDR baseaddr, struct dwarf2_cu *cu)
10205{
bb5ed363 10206 struct objfile *objfile = cu->objfile;
801e3a5b 10207 struct attribute *attr;
91da1414 10208 struct attribute *attr_high;
801e3a5b 10209
91da1414
MW
10210 attr_high = dwarf2_attr (die, DW_AT_high_pc, cu);
10211 if (attr_high)
801e3a5b 10212 {
801e3a5b
JB
10213 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
10214 if (attr)
10215 {
10216 CORE_ADDR low = DW_ADDR (attr);
91da1414 10217 CORE_ADDR high;
3019eac3
DE
10218 if (attr_high->form == DW_FORM_addr
10219 || attr_high->form == DW_FORM_GNU_addr_index)
91da1414
MW
10220 high = DW_ADDR (attr_high);
10221 else
10222 high = low + DW_UNSND (attr_high);
9a619af0 10223
801e3a5b
JB
10224 record_block_range (block, baseaddr + low, baseaddr + high - 1);
10225 }
10226 }
10227
10228 attr = dwarf2_attr (die, DW_AT_ranges, cu);
10229 if (attr)
10230 {
bb5ed363 10231 bfd *obfd = objfile->obfd;
801e3a5b
JB
10232
10233 /* The value of the DW_AT_ranges attribute is the offset of the
10234 address range list in the .debug_ranges section. */
2e3cf129 10235 unsigned long offset = DW_UNSND (attr) + cu->ranges_base;
dce234bc 10236 gdb_byte *buffer = dwarf2_per_objfile->ranges.buffer + offset;
801e3a5b
JB
10237
10238 /* For some target architectures, but not others, the
10239 read_address function sign-extends the addresses it returns.
10240 To recognize base address selection entries, we need a
10241 mask. */
10242 unsigned int addr_size = cu->header.addr_size;
10243 CORE_ADDR base_select_mask = ~(~(CORE_ADDR)1 << (addr_size * 8 - 1));
10244
10245 /* The base address, to which the next pair is relative. Note
10246 that this 'base' is a DWARF concept: most entries in a range
10247 list are relative, to reduce the number of relocs against the
10248 debugging information. This is separate from this function's
10249 'baseaddr' argument, which GDB uses to relocate debugging
10250 information from a shared library based on the address at
10251 which the library was loaded. */
d00adf39
DE
10252 CORE_ADDR base = cu->base_address;
10253 int base_known = cu->base_known;
801e3a5b 10254
be391dca 10255 gdb_assert (dwarf2_per_objfile->ranges.readin);
dce234bc 10256 if (offset >= dwarf2_per_objfile->ranges.size)
801e3a5b
JB
10257 {
10258 complaint (&symfile_complaints,
10259 _("Offset %lu out of bounds for DW_AT_ranges attribute"),
10260 offset);
10261 return;
10262 }
10263
10264 for (;;)
10265 {
10266 unsigned int bytes_read;
10267 CORE_ADDR start, end;
10268
10269 start = read_address (obfd, buffer, cu, &bytes_read);
10270 buffer += bytes_read;
10271 end = read_address (obfd, buffer, cu, &bytes_read);
10272 buffer += bytes_read;
10273
10274 /* Did we find the end of the range list? */
10275 if (start == 0 && end == 0)
10276 break;
10277
10278 /* Did we find a base address selection entry? */
10279 else if ((start & base_select_mask) == base_select_mask)
10280 {
10281 base = end;
10282 base_known = 1;
10283 }
10284
10285 /* We found an ordinary address range. */
10286 else
10287 {
10288 if (!base_known)
10289 {
10290 complaint (&symfile_complaints,
3e43a32a
MS
10291 _("Invalid .debug_ranges data "
10292 "(no base address)"));
801e3a5b
JB
10293 return;
10294 }
10295
9277c30c
UW
10296 if (start > end)
10297 {
10298 /* Inverted range entries are invalid. */
10299 complaint (&symfile_complaints,
10300 _("Invalid .debug_ranges data "
10301 "(inverted range)"));
10302 return;
10303 }
10304
10305 /* Empty range entries have no effect. */
10306 if (start == end)
10307 continue;
10308
01093045
DE
10309 start += base + baseaddr;
10310 end += base + baseaddr;
10311
10312 /* A not-uncommon case of bad debug info.
10313 Don't pollute the addrmap with bad data. */
10314 if (start == 0 && !dwarf2_per_objfile->has_section_at_zero)
10315 {
10316 complaint (&symfile_complaints,
10317 _(".debug_ranges entry has start address of zero"
10318 " [in module %s]"), objfile->name);
10319 continue;
10320 }
10321
10322 record_block_range (block, start, end - 1);
801e3a5b
JB
10323 }
10324 }
10325 }
10326}
10327
685b1105
JK
10328/* Check whether the producer field indicates either of GCC < 4.6, or the
10329 Intel C/C++ compiler, and cache the result in CU. */
60d5a603 10330
685b1105
JK
10331static void
10332check_producer (struct dwarf2_cu *cu)
60d5a603
JK
10333{
10334 const char *cs;
10335 int major, minor, release;
10336
10337 if (cu->producer == NULL)
10338 {
10339 /* For unknown compilers expect their behavior is DWARF version
10340 compliant.
10341
10342 GCC started to support .debug_types sections by -gdwarf-4 since
10343 gcc-4.5.x. As the .debug_types sections are missing DW_AT_producer
10344 for their space efficiency GDB cannot workaround gcc-4.5.x -gdwarf-4
10345 combination. gcc-4.5.x -gdwarf-4 binaries have DW_AT_accessibility
10346 interpreted incorrectly by GDB now - GCC PR debug/48229. */
60d5a603 10347 }
685b1105 10348 else if (strncmp (cu->producer, "GNU ", strlen ("GNU ")) == 0)
60d5a603 10349 {
685b1105
JK
10350 /* Skip any identifier after "GNU " - such as "C++" or "Java". */
10351
ba919b58
TT
10352 cs = &cu->producer[strlen ("GNU ")];
10353 while (*cs && !isdigit (*cs))
10354 cs++;
10355 if (sscanf (cs, "%d.%d.%d", &major, &minor, &release) != 3)
10356 {
10357 /* Not recognized as GCC. */
10358 }
10359 else
685b1105
JK
10360 cu->producer_is_gxx_lt_4_6 = major < 4 || (major == 4 && minor < 6);
10361 }
10362 else if (strncmp (cu->producer, "Intel(R) C", strlen ("Intel(R) C")) == 0)
10363 cu->producer_is_icc = 1;
10364 else
10365 {
10366 /* For other non-GCC compilers, expect their behavior is DWARF version
10367 compliant. */
60d5a603
JK
10368 }
10369
ba919b58 10370 cu->checked_producer = 1;
685b1105 10371}
ba919b58 10372
685b1105
JK
10373/* Check for GCC PR debug/45124 fix which is not present in any G++ version up
10374 to 4.5.any while it is present already in G++ 4.6.0 - the PR has been fixed
10375 during 4.6.0 experimental. */
10376
10377static int
10378producer_is_gxx_lt_4_6 (struct dwarf2_cu *cu)
10379{
10380 if (!cu->checked_producer)
10381 check_producer (cu);
10382
10383 return cu->producer_is_gxx_lt_4_6;
60d5a603
JK
10384}
10385
10386/* Return the default accessibility type if it is not overriden by
10387 DW_AT_accessibility. */
10388
10389static enum dwarf_access_attribute
10390dwarf2_default_access_attribute (struct die_info *die, struct dwarf2_cu *cu)
10391{
10392 if (cu->header.version < 3 || producer_is_gxx_lt_4_6 (cu))
10393 {
10394 /* The default DWARF 2 accessibility for members is public, the default
10395 accessibility for inheritance is private. */
10396
10397 if (die->tag != DW_TAG_inheritance)
10398 return DW_ACCESS_public;
10399 else
10400 return DW_ACCESS_private;
10401 }
10402 else
10403 {
10404 /* DWARF 3+ defines the default accessibility a different way. The same
10405 rules apply now for DW_TAG_inheritance as for the members and it only
10406 depends on the container kind. */
10407
10408 if (die->parent->tag == DW_TAG_class_type)
10409 return DW_ACCESS_private;
10410 else
10411 return DW_ACCESS_public;
10412 }
10413}
10414
74ac6d43
TT
10415/* Look for DW_AT_data_member_location. Set *OFFSET to the byte
10416 offset. If the attribute was not found return 0, otherwise return
10417 1. If it was found but could not properly be handled, set *OFFSET
10418 to 0. */
10419
10420static int
10421handle_data_member_location (struct die_info *die, struct dwarf2_cu *cu,
10422 LONGEST *offset)
10423{
10424 struct attribute *attr;
10425
10426 attr = dwarf2_attr (die, DW_AT_data_member_location, cu);
10427 if (attr != NULL)
10428 {
10429 *offset = 0;
10430
10431 /* Note that we do not check for a section offset first here.
10432 This is because DW_AT_data_member_location is new in DWARF 4,
10433 so if we see it, we can assume that a constant form is really
10434 a constant and not a section offset. */
10435 if (attr_form_is_constant (attr))
10436 *offset = dwarf2_get_attr_constant_value (attr, 0);
10437 else if (attr_form_is_section_offset (attr))
10438 dwarf2_complex_location_expr_complaint ();
10439 else if (attr_form_is_block (attr))
10440 *offset = decode_locdesc (DW_BLOCK (attr), cu);
10441 else
10442 dwarf2_complex_location_expr_complaint ();
10443
10444 return 1;
10445 }
10446
10447 return 0;
10448}
10449
c906108c
SS
10450/* Add an aggregate field to the field list. */
10451
10452static void
107d2387 10453dwarf2_add_field (struct field_info *fip, struct die_info *die,
e7c27a73 10454 struct dwarf2_cu *cu)
6e70227d 10455{
e7c27a73 10456 struct objfile *objfile = cu->objfile;
5e2b427d 10457 struct gdbarch *gdbarch = get_objfile_arch (objfile);
c906108c
SS
10458 struct nextfield *new_field;
10459 struct attribute *attr;
10460 struct field *fp;
10461 char *fieldname = "";
10462
10463 /* Allocate a new field list entry and link it in. */
10464 new_field = (struct nextfield *) xmalloc (sizeof (struct nextfield));
b8c9b27d 10465 make_cleanup (xfree, new_field);
c906108c 10466 memset (new_field, 0, sizeof (struct nextfield));
7d0ccb61
DJ
10467
10468 if (die->tag == DW_TAG_inheritance)
10469 {
10470 new_field->next = fip->baseclasses;
10471 fip->baseclasses = new_field;
10472 }
10473 else
10474 {
10475 new_field->next = fip->fields;
10476 fip->fields = new_field;
10477 }
c906108c
SS
10478 fip->nfields++;
10479
e142c38c 10480 attr = dwarf2_attr (die, DW_AT_accessibility, cu);
c906108c
SS
10481 if (attr)
10482 new_field->accessibility = DW_UNSND (attr);
60d5a603
JK
10483 else
10484 new_field->accessibility = dwarf2_default_access_attribute (die, cu);
c906108c
SS
10485 if (new_field->accessibility != DW_ACCESS_public)
10486 fip->non_public_fields = 1;
60d5a603 10487
e142c38c 10488 attr = dwarf2_attr (die, DW_AT_virtuality, cu);
c906108c
SS
10489 if (attr)
10490 new_field->virtuality = DW_UNSND (attr);
60d5a603
JK
10491 else
10492 new_field->virtuality = DW_VIRTUALITY_none;
c906108c
SS
10493
10494 fp = &new_field->field;
a9a9bd0f 10495
e142c38c 10496 if (die->tag == DW_TAG_member && ! die_is_declaration (die, cu))
c906108c 10497 {
74ac6d43
TT
10498 LONGEST offset;
10499
a9a9bd0f 10500 /* Data member other than a C++ static data member. */
6e70227d 10501
c906108c 10502 /* Get type of field. */
e7c27a73 10503 fp->type = die_type (die, cu);
c906108c 10504
d6a843b5 10505 SET_FIELD_BITPOS (*fp, 0);
01ad7f36 10506
c906108c 10507 /* Get bit size of field (zero if none). */
e142c38c 10508 attr = dwarf2_attr (die, DW_AT_bit_size, cu);
c906108c
SS
10509 if (attr)
10510 {
10511 FIELD_BITSIZE (*fp) = DW_UNSND (attr);
10512 }
10513 else
10514 {
10515 FIELD_BITSIZE (*fp) = 0;
10516 }
10517
10518 /* Get bit offset of field. */
74ac6d43
TT
10519 if (handle_data_member_location (die, cu, &offset))
10520 SET_FIELD_BITPOS (*fp, offset * bits_per_byte);
e142c38c 10521 attr = dwarf2_attr (die, DW_AT_bit_offset, cu);
c906108c
SS
10522 if (attr)
10523 {
5e2b427d 10524 if (gdbarch_bits_big_endian (gdbarch))
c906108c
SS
10525 {
10526 /* For big endian bits, the DW_AT_bit_offset gives the
c5aa993b
JM
10527 additional bit offset from the MSB of the containing
10528 anonymous object to the MSB of the field. We don't
10529 have to do anything special since we don't need to
10530 know the size of the anonymous object. */
f41f5e61 10531 SET_FIELD_BITPOS (*fp, FIELD_BITPOS (*fp) + DW_UNSND (attr));
c906108c
SS
10532 }
10533 else
10534 {
10535 /* For little endian bits, compute the bit offset to the
c5aa993b
JM
10536 MSB of the anonymous object, subtract off the number of
10537 bits from the MSB of the field to the MSB of the
10538 object, and then subtract off the number of bits of
10539 the field itself. The result is the bit offset of
10540 the LSB of the field. */
c906108c
SS
10541 int anonymous_size;
10542 int bit_offset = DW_UNSND (attr);
10543
e142c38c 10544 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
c906108c
SS
10545 if (attr)
10546 {
10547 /* The size of the anonymous object containing
10548 the bit field is explicit, so use the
10549 indicated size (in bytes). */
10550 anonymous_size = DW_UNSND (attr);
10551 }
10552 else
10553 {
10554 /* The size of the anonymous object containing
10555 the bit field must be inferred from the type
10556 attribute of the data member containing the
10557 bit field. */
10558 anonymous_size = TYPE_LENGTH (fp->type);
10559 }
f41f5e61
PA
10560 SET_FIELD_BITPOS (*fp,
10561 (FIELD_BITPOS (*fp)
10562 + anonymous_size * bits_per_byte
10563 - bit_offset - FIELD_BITSIZE (*fp)));
c906108c
SS
10564 }
10565 }
10566
10567 /* Get name of field. */
39cbfefa
DJ
10568 fieldname = dwarf2_name (die, cu);
10569 if (fieldname == NULL)
10570 fieldname = "";
d8151005
DJ
10571
10572 /* The name is already allocated along with this objfile, so we don't
10573 need to duplicate it for the type. */
10574 fp->name = fieldname;
c906108c
SS
10575
10576 /* Change accessibility for artificial fields (e.g. virtual table
c5aa993b 10577 pointer or virtual base class pointer) to private. */
e142c38c 10578 if (dwarf2_attr (die, DW_AT_artificial, cu))
c906108c 10579 {
d48cc9dd 10580 FIELD_ARTIFICIAL (*fp) = 1;
c906108c
SS
10581 new_field->accessibility = DW_ACCESS_private;
10582 fip->non_public_fields = 1;
10583 }
10584 }
a9a9bd0f 10585 else if (die->tag == DW_TAG_member || die->tag == DW_TAG_variable)
c906108c 10586 {
a9a9bd0f
DC
10587 /* C++ static member. */
10588
10589 /* NOTE: carlton/2002-11-05: It should be a DW_TAG_member that
10590 is a declaration, but all versions of G++ as of this writing
10591 (so through at least 3.2.1) incorrectly generate
10592 DW_TAG_variable tags. */
6e70227d 10593
ff355380 10594 const char *physname;
c906108c 10595
a9a9bd0f 10596 /* Get name of field. */
39cbfefa
DJ
10597 fieldname = dwarf2_name (die, cu);
10598 if (fieldname == NULL)
c906108c
SS
10599 return;
10600
254e6b9e 10601 attr = dwarf2_attr (die, DW_AT_const_value, cu);
3863f96c
DE
10602 if (attr
10603 /* Only create a symbol if this is an external value.
10604 new_symbol checks this and puts the value in the global symbol
10605 table, which we want. If it is not external, new_symbol
10606 will try to put the value in cu->list_in_scope which is wrong. */
10607 && dwarf2_flag_true_p (die, DW_AT_external, cu))
254e6b9e
DE
10608 {
10609 /* A static const member, not much different than an enum as far as
10610 we're concerned, except that we can support more types. */
10611 new_symbol (die, NULL, cu);
10612 }
10613
2df3850c 10614 /* Get physical name. */
ff355380 10615 physname = dwarf2_physname (fieldname, die, cu);
c906108c 10616
d8151005
DJ
10617 /* The name is already allocated along with this objfile, so we don't
10618 need to duplicate it for the type. */
10619 SET_FIELD_PHYSNAME (*fp, physname ? physname : "");
e7c27a73 10620 FIELD_TYPE (*fp) = die_type (die, cu);
d8151005 10621 FIELD_NAME (*fp) = fieldname;
c906108c
SS
10622 }
10623 else if (die->tag == DW_TAG_inheritance)
10624 {
74ac6d43 10625 LONGEST offset;
d4b96c9a 10626
74ac6d43
TT
10627 /* C++ base class field. */
10628 if (handle_data_member_location (die, cu, &offset))
10629 SET_FIELD_BITPOS (*fp, offset * bits_per_byte);
c906108c 10630 FIELD_BITSIZE (*fp) = 0;
e7c27a73 10631 FIELD_TYPE (*fp) = die_type (die, cu);
c906108c
SS
10632 FIELD_NAME (*fp) = type_name_no_tag (fp->type);
10633 fip->nbaseclasses++;
10634 }
10635}
10636
98751a41
JK
10637/* Add a typedef defined in the scope of the FIP's class. */
10638
10639static void
10640dwarf2_add_typedef (struct field_info *fip, struct die_info *die,
10641 struct dwarf2_cu *cu)
6e70227d 10642{
98751a41 10643 struct objfile *objfile = cu->objfile;
98751a41
JK
10644 struct typedef_field_list *new_field;
10645 struct attribute *attr;
10646 struct typedef_field *fp;
10647 char *fieldname = "";
10648
10649 /* Allocate a new field list entry and link it in. */
10650 new_field = xzalloc (sizeof (*new_field));
10651 make_cleanup (xfree, new_field);
10652
10653 gdb_assert (die->tag == DW_TAG_typedef);
10654
10655 fp = &new_field->field;
10656
10657 /* Get name of field. */
10658 fp->name = dwarf2_name (die, cu);
10659 if (fp->name == NULL)
10660 return;
10661
10662 fp->type = read_type_die (die, cu);
10663
10664 new_field->next = fip->typedef_field_list;
10665 fip->typedef_field_list = new_field;
10666 fip->typedef_field_list_count++;
10667}
10668
c906108c
SS
10669/* Create the vector of fields, and attach it to the type. */
10670
10671static void
fba45db2 10672dwarf2_attach_fields_to_type (struct field_info *fip, struct type *type,
e7c27a73 10673 struct dwarf2_cu *cu)
c906108c
SS
10674{
10675 int nfields = fip->nfields;
10676
10677 /* Record the field count, allocate space for the array of fields,
10678 and create blank accessibility bitfields if necessary. */
10679 TYPE_NFIELDS (type) = nfields;
10680 TYPE_FIELDS (type) = (struct field *)
10681 TYPE_ALLOC (type, sizeof (struct field) * nfields);
10682 memset (TYPE_FIELDS (type), 0, sizeof (struct field) * nfields);
10683
b4ba55a1 10684 if (fip->non_public_fields && cu->language != language_ada)
c906108c
SS
10685 {
10686 ALLOCATE_CPLUS_STRUCT_TYPE (type);
10687
10688 TYPE_FIELD_PRIVATE_BITS (type) =
10689 (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
10690 B_CLRALL (TYPE_FIELD_PRIVATE_BITS (type), nfields);
10691
10692 TYPE_FIELD_PROTECTED_BITS (type) =
10693 (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
10694 B_CLRALL (TYPE_FIELD_PROTECTED_BITS (type), nfields);
10695
774b6a14
TT
10696 TYPE_FIELD_IGNORE_BITS (type) =
10697 (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
10698 B_CLRALL (TYPE_FIELD_IGNORE_BITS (type), nfields);
c906108c
SS
10699 }
10700
10701 /* If the type has baseclasses, allocate and clear a bit vector for
10702 TYPE_FIELD_VIRTUAL_BITS. */
b4ba55a1 10703 if (fip->nbaseclasses && cu->language != language_ada)
c906108c
SS
10704 {
10705 int num_bytes = B_BYTES (fip->nbaseclasses);
fe1b8b76 10706 unsigned char *pointer;
c906108c
SS
10707
10708 ALLOCATE_CPLUS_STRUCT_TYPE (type);
fe1b8b76
JB
10709 pointer = TYPE_ALLOC (type, num_bytes);
10710 TYPE_FIELD_VIRTUAL_BITS (type) = pointer;
c906108c
SS
10711 B_CLRALL (TYPE_FIELD_VIRTUAL_BITS (type), fip->nbaseclasses);
10712 TYPE_N_BASECLASSES (type) = fip->nbaseclasses;
10713 }
10714
3e43a32a
MS
10715 /* Copy the saved-up fields into the field vector. Start from the head of
10716 the list, adding to the tail of the field array, so that they end up in
10717 the same order in the array in which they were added to the list. */
c906108c
SS
10718 while (nfields-- > 0)
10719 {
7d0ccb61
DJ
10720 struct nextfield *fieldp;
10721
10722 if (fip->fields)
10723 {
10724 fieldp = fip->fields;
10725 fip->fields = fieldp->next;
10726 }
10727 else
10728 {
10729 fieldp = fip->baseclasses;
10730 fip->baseclasses = fieldp->next;
10731 }
10732
10733 TYPE_FIELD (type, nfields) = fieldp->field;
10734 switch (fieldp->accessibility)
c906108c 10735 {
c5aa993b 10736 case DW_ACCESS_private:
b4ba55a1
JB
10737 if (cu->language != language_ada)
10738 SET_TYPE_FIELD_PRIVATE (type, nfields);
c5aa993b 10739 break;
c906108c 10740
c5aa993b 10741 case DW_ACCESS_protected:
b4ba55a1
JB
10742 if (cu->language != language_ada)
10743 SET_TYPE_FIELD_PROTECTED (type, nfields);
c5aa993b 10744 break;
c906108c 10745
c5aa993b
JM
10746 case DW_ACCESS_public:
10747 break;
c906108c 10748
c5aa993b
JM
10749 default:
10750 /* Unknown accessibility. Complain and treat it as public. */
10751 {
e2e0b3e5 10752 complaint (&symfile_complaints, _("unsupported accessibility %d"),
7d0ccb61 10753 fieldp->accessibility);
c5aa993b
JM
10754 }
10755 break;
c906108c
SS
10756 }
10757 if (nfields < fip->nbaseclasses)
10758 {
7d0ccb61 10759 switch (fieldp->virtuality)
c906108c 10760 {
c5aa993b
JM
10761 case DW_VIRTUALITY_virtual:
10762 case DW_VIRTUALITY_pure_virtual:
b4ba55a1 10763 if (cu->language == language_ada)
a73c6dcd 10764 error (_("unexpected virtuality in component of Ada type"));
c5aa993b
JM
10765 SET_TYPE_FIELD_VIRTUAL (type, nfields);
10766 break;
c906108c
SS
10767 }
10768 }
c906108c
SS
10769 }
10770}
10771
c906108c
SS
10772/* Add a member function to the proper fieldlist. */
10773
10774static void
107d2387 10775dwarf2_add_member_fn (struct field_info *fip, struct die_info *die,
e7c27a73 10776 struct type *type, struct dwarf2_cu *cu)
c906108c 10777{
e7c27a73 10778 struct objfile *objfile = cu->objfile;
c906108c
SS
10779 struct attribute *attr;
10780 struct fnfieldlist *flp;
10781 int i;
10782 struct fn_field *fnp;
10783 char *fieldname;
c906108c 10784 struct nextfnfield *new_fnfield;
f792889a 10785 struct type *this_type;
60d5a603 10786 enum dwarf_access_attribute accessibility;
c906108c 10787
b4ba55a1 10788 if (cu->language == language_ada)
a73c6dcd 10789 error (_("unexpected member function in Ada type"));
b4ba55a1 10790
2df3850c 10791 /* Get name of member function. */
39cbfefa
DJ
10792 fieldname = dwarf2_name (die, cu);
10793 if (fieldname == NULL)
2df3850c 10794 return;
c906108c 10795
c906108c
SS
10796 /* Look up member function name in fieldlist. */
10797 for (i = 0; i < fip->nfnfields; i++)
10798 {
27bfe10e 10799 if (strcmp (fip->fnfieldlists[i].name, fieldname) == 0)
c906108c
SS
10800 break;
10801 }
10802
10803 /* Create new list element if necessary. */
10804 if (i < fip->nfnfields)
10805 flp = &fip->fnfieldlists[i];
10806 else
10807 {
10808 if ((fip->nfnfields % DW_FIELD_ALLOC_CHUNK) == 0)
10809 {
10810 fip->fnfieldlists = (struct fnfieldlist *)
10811 xrealloc (fip->fnfieldlists,
10812 (fip->nfnfields + DW_FIELD_ALLOC_CHUNK)
c5aa993b 10813 * sizeof (struct fnfieldlist));
c906108c 10814 if (fip->nfnfields == 0)
c13c43fd 10815 make_cleanup (free_current_contents, &fip->fnfieldlists);
c906108c
SS
10816 }
10817 flp = &fip->fnfieldlists[fip->nfnfields];
10818 flp->name = fieldname;
10819 flp->length = 0;
10820 flp->head = NULL;
3da10d80 10821 i = fip->nfnfields++;
c906108c
SS
10822 }
10823
10824 /* Create a new member function field and chain it to the field list
0963b4bd 10825 entry. */
c906108c 10826 new_fnfield = (struct nextfnfield *) xmalloc (sizeof (struct nextfnfield));
b8c9b27d 10827 make_cleanup (xfree, new_fnfield);
c906108c
SS
10828 memset (new_fnfield, 0, sizeof (struct nextfnfield));
10829 new_fnfield->next = flp->head;
10830 flp->head = new_fnfield;
10831 flp->length++;
10832
10833 /* Fill in the member function field info. */
10834 fnp = &new_fnfield->fnfield;
3da10d80
KS
10835
10836 /* Delay processing of the physname until later. */
10837 if (cu->language == language_cplus || cu->language == language_java)
10838 {
10839 add_to_method_list (type, i, flp->length - 1, fieldname,
10840 die, cu);
10841 }
10842 else
10843 {
1d06ead6 10844 const char *physname = dwarf2_physname (fieldname, die, cu);
3da10d80
KS
10845 fnp->physname = physname ? physname : "";
10846 }
10847
c906108c 10848 fnp->type = alloc_type (objfile);
f792889a
DJ
10849 this_type = read_type_die (die, cu);
10850 if (this_type && TYPE_CODE (this_type) == TYPE_CODE_FUNC)
c906108c 10851 {
f792889a 10852 int nparams = TYPE_NFIELDS (this_type);
c906108c 10853
f792889a 10854 /* TYPE is the domain of this method, and THIS_TYPE is the type
e26fb1d7
DC
10855 of the method itself (TYPE_CODE_METHOD). */
10856 smash_to_method_type (fnp->type, type,
f792889a
DJ
10857 TYPE_TARGET_TYPE (this_type),
10858 TYPE_FIELDS (this_type),
10859 TYPE_NFIELDS (this_type),
10860 TYPE_VARARGS (this_type));
c906108c
SS
10861
10862 /* Handle static member functions.
c5aa993b 10863 Dwarf2 has no clean way to discern C++ static and non-static
0963b4bd
MS
10864 member functions. G++ helps GDB by marking the first
10865 parameter for non-static member functions (which is the this
10866 pointer) as artificial. We obtain this information from
10867 read_subroutine_type via TYPE_FIELD_ARTIFICIAL. */
f792889a 10868 if (nparams == 0 || TYPE_FIELD_ARTIFICIAL (this_type, 0) == 0)
c906108c
SS
10869 fnp->voffset = VOFFSET_STATIC;
10870 }
10871 else
e2e0b3e5 10872 complaint (&symfile_complaints, _("member function type missing for '%s'"),
3da10d80 10873 dwarf2_full_name (fieldname, die, cu));
c906108c
SS
10874
10875 /* Get fcontext from DW_AT_containing_type if present. */
e142c38c 10876 if (dwarf2_attr (die, DW_AT_containing_type, cu) != NULL)
e7c27a73 10877 fnp->fcontext = die_containing_type (die, cu);
c906108c 10878
3e43a32a
MS
10879 /* dwarf2 doesn't have stubbed physical names, so the setting of is_const and
10880 is_volatile is irrelevant, as it is needed by gdb_mangle_name only. */
c906108c
SS
10881
10882 /* Get accessibility. */
e142c38c 10883 attr = dwarf2_attr (die, DW_AT_accessibility, cu);
c906108c 10884 if (attr)
60d5a603
JK
10885 accessibility = DW_UNSND (attr);
10886 else
10887 accessibility = dwarf2_default_access_attribute (die, cu);
10888 switch (accessibility)
c906108c 10889 {
60d5a603
JK
10890 case DW_ACCESS_private:
10891 fnp->is_private = 1;
10892 break;
10893 case DW_ACCESS_protected:
10894 fnp->is_protected = 1;
10895 break;
c906108c
SS
10896 }
10897
b02dede2 10898 /* Check for artificial methods. */
e142c38c 10899 attr = dwarf2_attr (die, DW_AT_artificial, cu);
b02dede2
DJ
10900 if (attr && DW_UNSND (attr) != 0)
10901 fnp->is_artificial = 1;
10902
0d564a31 10903 /* Get index in virtual function table if it is a virtual member
aec5aa8b
TT
10904 function. For older versions of GCC, this is an offset in the
10905 appropriate virtual table, as specified by DW_AT_containing_type.
10906 For everyone else, it is an expression to be evaluated relative
0d564a31
DJ
10907 to the object address. */
10908
e142c38c 10909 attr = dwarf2_attr (die, DW_AT_vtable_elem_location, cu);
aec5aa8b 10910 if (attr)
8e19ed76 10911 {
aec5aa8b 10912 if (attr_form_is_block (attr) && DW_BLOCK (attr)->size > 0)
8e19ed76 10913 {
aec5aa8b
TT
10914 if (DW_BLOCK (attr)->data[0] == DW_OP_constu)
10915 {
10916 /* Old-style GCC. */
10917 fnp->voffset = decode_locdesc (DW_BLOCK (attr), cu) + 2;
10918 }
10919 else if (DW_BLOCK (attr)->data[0] == DW_OP_deref
10920 || (DW_BLOCK (attr)->size > 1
10921 && DW_BLOCK (attr)->data[0] == DW_OP_deref_size
10922 && DW_BLOCK (attr)->data[1] == cu->header.addr_size))
10923 {
10924 struct dwarf_block blk;
10925 int offset;
10926
10927 offset = (DW_BLOCK (attr)->data[0] == DW_OP_deref
10928 ? 1 : 2);
10929 blk.size = DW_BLOCK (attr)->size - offset;
10930 blk.data = DW_BLOCK (attr)->data + offset;
10931 fnp->voffset = decode_locdesc (DW_BLOCK (attr), cu);
10932 if ((fnp->voffset % cu->header.addr_size) != 0)
10933 dwarf2_complex_location_expr_complaint ();
10934 else
10935 fnp->voffset /= cu->header.addr_size;
10936 fnp->voffset += 2;
10937 }
10938 else
10939 dwarf2_complex_location_expr_complaint ();
10940
10941 if (!fnp->fcontext)
10942 fnp->fcontext = TYPE_TARGET_TYPE (TYPE_FIELD_TYPE (this_type, 0));
10943 }
3690dd37 10944 else if (attr_form_is_section_offset (attr))
8e19ed76 10945 {
4d3c2250 10946 dwarf2_complex_location_expr_complaint ();
8e19ed76
PS
10947 }
10948 else
10949 {
4d3c2250
KB
10950 dwarf2_invalid_attrib_class_complaint ("DW_AT_vtable_elem_location",
10951 fieldname);
8e19ed76 10952 }
0d564a31 10953 }
d48cc9dd
DJ
10954 else
10955 {
10956 attr = dwarf2_attr (die, DW_AT_virtuality, cu);
10957 if (attr && DW_UNSND (attr))
10958 {
10959 /* GCC does this, as of 2008-08-25; PR debug/37237. */
10960 complaint (&symfile_complaints,
3e43a32a
MS
10961 _("Member function \"%s\" (offset %d) is virtual "
10962 "but the vtable offset is not specified"),
b64f50a1 10963 fieldname, die->offset.sect_off);
9655fd1a 10964 ALLOCATE_CPLUS_STRUCT_TYPE (type);
d48cc9dd
DJ
10965 TYPE_CPLUS_DYNAMIC (type) = 1;
10966 }
10967 }
c906108c
SS
10968}
10969
10970/* Create the vector of member function fields, and attach it to the type. */
10971
10972static void
fba45db2 10973dwarf2_attach_fn_fields_to_type (struct field_info *fip, struct type *type,
e7c27a73 10974 struct dwarf2_cu *cu)
c906108c
SS
10975{
10976 struct fnfieldlist *flp;
c906108c
SS
10977 int i;
10978
b4ba55a1 10979 if (cu->language == language_ada)
a73c6dcd 10980 error (_("unexpected member functions in Ada type"));
b4ba55a1 10981
c906108c
SS
10982 ALLOCATE_CPLUS_STRUCT_TYPE (type);
10983 TYPE_FN_FIELDLISTS (type) = (struct fn_fieldlist *)
10984 TYPE_ALLOC (type, sizeof (struct fn_fieldlist) * fip->nfnfields);
10985
10986 for (i = 0, flp = fip->fnfieldlists; i < fip->nfnfields; i++, flp++)
10987 {
10988 struct nextfnfield *nfp = flp->head;
10989 struct fn_fieldlist *fn_flp = &TYPE_FN_FIELDLIST (type, i);
10990 int k;
10991
10992 TYPE_FN_FIELDLIST_NAME (type, i) = flp->name;
10993 TYPE_FN_FIELDLIST_LENGTH (type, i) = flp->length;
10994 fn_flp->fn_fields = (struct fn_field *)
10995 TYPE_ALLOC (type, sizeof (struct fn_field) * flp->length);
10996 for (k = flp->length; (k--, nfp); nfp = nfp->next)
c5aa993b 10997 fn_flp->fn_fields[k] = nfp->fnfield;
c906108c
SS
10998 }
10999
11000 TYPE_NFN_FIELDS (type) = fip->nfnfields;
c906108c
SS
11001}
11002
1168df01
JB
11003/* Returns non-zero if NAME is the name of a vtable member in CU's
11004 language, zero otherwise. */
11005static int
11006is_vtable_name (const char *name, struct dwarf2_cu *cu)
11007{
11008 static const char vptr[] = "_vptr";
987504bb 11009 static const char vtable[] = "vtable";
1168df01 11010
987504bb
JJ
11011 /* Look for the C++ and Java forms of the vtable. */
11012 if ((cu->language == language_java
11013 && strncmp (name, vtable, sizeof (vtable) - 1) == 0)
11014 || (strncmp (name, vptr, sizeof (vptr) - 1) == 0
11015 && is_cplus_marker (name[sizeof (vptr) - 1])))
1168df01
JB
11016 return 1;
11017
11018 return 0;
11019}
11020
c0dd20ea 11021/* GCC outputs unnamed structures that are really pointers to member
0b92b5bb
TT
11022 functions, with the ABI-specified layout. If TYPE describes
11023 such a structure, smash it into a member function type.
61049d3b
DJ
11024
11025 GCC shouldn't do this; it should just output pointer to member DIEs.
11026 This is GCC PR debug/28767. */
c0dd20ea 11027
0b92b5bb
TT
11028static void
11029quirk_gcc_member_function_pointer (struct type *type, struct objfile *objfile)
c0dd20ea 11030{
0b92b5bb 11031 struct type *pfn_type, *domain_type, *new_type;
c0dd20ea
DJ
11032
11033 /* Check for a structure with no name and two children. */
0b92b5bb
TT
11034 if (TYPE_CODE (type) != TYPE_CODE_STRUCT || TYPE_NFIELDS (type) != 2)
11035 return;
c0dd20ea
DJ
11036
11037 /* Check for __pfn and __delta members. */
0b92b5bb
TT
11038 if (TYPE_FIELD_NAME (type, 0) == NULL
11039 || strcmp (TYPE_FIELD_NAME (type, 0), "__pfn") != 0
11040 || TYPE_FIELD_NAME (type, 1) == NULL
11041 || strcmp (TYPE_FIELD_NAME (type, 1), "__delta") != 0)
11042 return;
c0dd20ea
DJ
11043
11044 /* Find the type of the method. */
0b92b5bb 11045 pfn_type = TYPE_FIELD_TYPE (type, 0);
c0dd20ea
DJ
11046 if (pfn_type == NULL
11047 || TYPE_CODE (pfn_type) != TYPE_CODE_PTR
11048 || TYPE_CODE (TYPE_TARGET_TYPE (pfn_type)) != TYPE_CODE_FUNC)
0b92b5bb 11049 return;
c0dd20ea
DJ
11050
11051 /* Look for the "this" argument. */
11052 pfn_type = TYPE_TARGET_TYPE (pfn_type);
11053 if (TYPE_NFIELDS (pfn_type) == 0
0b92b5bb 11054 /* || TYPE_FIELD_TYPE (pfn_type, 0) == NULL */
c0dd20ea 11055 || TYPE_CODE (TYPE_FIELD_TYPE (pfn_type, 0)) != TYPE_CODE_PTR)
0b92b5bb 11056 return;
c0dd20ea
DJ
11057
11058 domain_type = TYPE_TARGET_TYPE (TYPE_FIELD_TYPE (pfn_type, 0));
0b92b5bb
TT
11059 new_type = alloc_type (objfile);
11060 smash_to_method_type (new_type, domain_type, TYPE_TARGET_TYPE (pfn_type),
c0dd20ea
DJ
11061 TYPE_FIELDS (pfn_type), TYPE_NFIELDS (pfn_type),
11062 TYPE_VARARGS (pfn_type));
0b92b5bb 11063 smash_to_methodptr_type (type, new_type);
c0dd20ea 11064}
1168df01 11065
685b1105
JK
11066/* Return non-zero if the CU's PRODUCER string matches the Intel C/C++ compiler
11067 (icc). */
11068
11069static int
11070producer_is_icc (struct dwarf2_cu *cu)
11071{
11072 if (!cu->checked_producer)
11073 check_producer (cu);
11074
11075 return cu->producer_is_icc;
11076}
11077
c906108c 11078/* Called when we find the DIE that starts a structure or union scope
c767944b
DJ
11079 (definition) to create a type for the structure or union. Fill in
11080 the type's name and general properties; the members will not be
11081 processed until process_structure_type.
c906108c 11082
c767944b
DJ
11083 NOTE: we need to call these functions regardless of whether or not the
11084 DIE has a DW_AT_name attribute, since it might be an anonymous
c906108c
SS
11085 structure or union. This gets the type entered into our set of
11086 user defined types.
11087
11088 However, if the structure is incomplete (an opaque struct/union)
11089 then suppress creating a symbol table entry for it since gdb only
11090 wants to find the one with the complete definition. Note that if
11091 it is complete, we just call new_symbol, which does it's own
11092 checking about whether the struct/union is anonymous or not (and
11093 suppresses creating a symbol table entry itself). */
11094
f792889a 11095static struct type *
134d01f1 11096read_structure_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 11097{
e7c27a73 11098 struct objfile *objfile = cu->objfile;
c906108c
SS
11099 struct type *type;
11100 struct attribute *attr;
39cbfefa 11101 char *name;
c906108c 11102
348e048f
DE
11103 /* If the definition of this type lives in .debug_types, read that type.
11104 Don't follow DW_AT_specification though, that will take us back up
11105 the chain and we want to go down. */
45e58e77 11106 attr = dwarf2_attr_no_follow (die, DW_AT_signature);
348e048f
DE
11107 if (attr)
11108 {
11109 struct dwarf2_cu *type_cu = cu;
11110 struct die_info *type_die = follow_die_ref_or_sig (die, attr, &type_cu);
9a619af0 11111
348e048f
DE
11112 /* We could just recurse on read_structure_type, but we need to call
11113 get_die_type to ensure only one type for this DIE is created.
11114 This is important, for example, because for c++ classes we need
11115 TYPE_NAME set which is only done by new_symbol. Blech. */
11116 type = read_type_die (type_die, type_cu);
9dc481d3
DE
11117
11118 /* TYPE_CU may not be the same as CU.
11119 Ensure TYPE is recorded in CU's type_hash table. */
348e048f
DE
11120 return set_die_type (die, type, cu);
11121 }
11122
c0dd20ea 11123 type = alloc_type (objfile);
c906108c 11124 INIT_CPLUS_SPECIFIC (type);
93311388 11125
39cbfefa
DJ
11126 name = dwarf2_name (die, cu);
11127 if (name != NULL)
c906108c 11128 {
987504bb
JJ
11129 if (cu->language == language_cplus
11130 || cu->language == language_java)
63d06c5c 11131 {
3da10d80
KS
11132 char *full_name = (char *) dwarf2_full_name (name, die, cu);
11133
11134 /* dwarf2_full_name might have already finished building the DIE's
11135 type. If so, there is no need to continue. */
11136 if (get_die_type (die, cu) != NULL)
11137 return get_die_type (die, cu);
11138
11139 TYPE_TAG_NAME (type) = full_name;
94af9270
KS
11140 if (die->tag == DW_TAG_structure_type
11141 || die->tag == DW_TAG_class_type)
11142 TYPE_NAME (type) = TYPE_TAG_NAME (type);
63d06c5c
DC
11143 }
11144 else
11145 {
d8151005
DJ
11146 /* The name is already allocated along with this objfile, so
11147 we don't need to duplicate it for the type. */
94af9270
KS
11148 TYPE_TAG_NAME (type) = (char *) name;
11149 if (die->tag == DW_TAG_class_type)
11150 TYPE_NAME (type) = TYPE_TAG_NAME (type);
63d06c5c 11151 }
c906108c
SS
11152 }
11153
11154 if (die->tag == DW_TAG_structure_type)
11155 {
11156 TYPE_CODE (type) = TYPE_CODE_STRUCT;
11157 }
11158 else if (die->tag == DW_TAG_union_type)
11159 {
11160 TYPE_CODE (type) = TYPE_CODE_UNION;
11161 }
11162 else
11163 {
c906108c
SS
11164 TYPE_CODE (type) = TYPE_CODE_CLASS;
11165 }
11166
0cc2414c
TT
11167 if (cu->language == language_cplus && die->tag == DW_TAG_class_type)
11168 TYPE_DECLARED_CLASS (type) = 1;
11169
e142c38c 11170 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
c906108c
SS
11171 if (attr)
11172 {
11173 TYPE_LENGTH (type) = DW_UNSND (attr);
11174 }
11175 else
11176 {
11177 TYPE_LENGTH (type) = 0;
11178 }
11179
685b1105
JK
11180 if (producer_is_icc (cu))
11181 {
11182 /* ICC does not output the required DW_AT_declaration
11183 on incomplete types, but gives them a size of zero. */
11184 }
11185 else
11186 TYPE_STUB_SUPPORTED (type) = 1;
11187
dc718098 11188 if (die_is_declaration (die, cu))
876cecd0 11189 TYPE_STUB (type) = 1;
a6c727b2
DJ
11190 else if (attr == NULL && die->child == NULL
11191 && producer_is_realview (cu->producer))
11192 /* RealView does not output the required DW_AT_declaration
11193 on incomplete types. */
11194 TYPE_STUB (type) = 1;
dc718098 11195
c906108c
SS
11196 /* We need to add the type field to the die immediately so we don't
11197 infinitely recurse when dealing with pointers to the structure
0963b4bd 11198 type within the structure itself. */
1c379e20 11199 set_die_type (die, type, cu);
c906108c 11200
7e314c57
JK
11201 /* set_die_type should be already done. */
11202 set_descriptive_type (type, die, cu);
11203
c767944b
DJ
11204 return type;
11205}
11206
11207/* Finish creating a structure or union type, including filling in
11208 its members and creating a symbol for it. */
11209
11210static void
11211process_structure_scope (struct die_info *die, struct dwarf2_cu *cu)
11212{
11213 struct objfile *objfile = cu->objfile;
11214 struct die_info *child_die = die->child;
11215 struct type *type;
11216
11217 type = get_die_type (die, cu);
11218 if (type == NULL)
11219 type = read_structure_type (die, cu);
11220
e142c38c 11221 if (die->child != NULL && ! die_is_declaration (die, cu))
c906108c
SS
11222 {
11223 struct field_info fi;
11224 struct die_info *child_die;
34eaf542 11225 VEC (symbolp) *template_args = NULL;
c767944b 11226 struct cleanup *back_to = make_cleanup (null_cleanup, 0);
c906108c
SS
11227
11228 memset (&fi, 0, sizeof (struct field_info));
11229
639d11d3 11230 child_die = die->child;
c906108c
SS
11231
11232 while (child_die && child_die->tag)
11233 {
a9a9bd0f
DC
11234 if (child_die->tag == DW_TAG_member
11235 || child_die->tag == DW_TAG_variable)
c906108c 11236 {
a9a9bd0f
DC
11237 /* NOTE: carlton/2002-11-05: A C++ static data member
11238 should be a DW_TAG_member that is a declaration, but
11239 all versions of G++ as of this writing (so through at
11240 least 3.2.1) incorrectly generate DW_TAG_variable
11241 tags for them instead. */
e7c27a73 11242 dwarf2_add_field (&fi, child_die, cu);
c906108c 11243 }
8713b1b1 11244 else if (child_die->tag == DW_TAG_subprogram)
c906108c 11245 {
0963b4bd 11246 /* C++ member function. */
e7c27a73 11247 dwarf2_add_member_fn (&fi, child_die, type, cu);
c906108c
SS
11248 }
11249 else if (child_die->tag == DW_TAG_inheritance)
11250 {
11251 /* C++ base class field. */
e7c27a73 11252 dwarf2_add_field (&fi, child_die, cu);
c906108c 11253 }
98751a41
JK
11254 else if (child_die->tag == DW_TAG_typedef)
11255 dwarf2_add_typedef (&fi, child_die, cu);
34eaf542
TT
11256 else if (child_die->tag == DW_TAG_template_type_param
11257 || child_die->tag == DW_TAG_template_value_param)
11258 {
11259 struct symbol *arg = new_symbol (child_die, NULL, cu);
11260
f1078f66
DJ
11261 if (arg != NULL)
11262 VEC_safe_push (symbolp, template_args, arg);
34eaf542
TT
11263 }
11264
c906108c
SS
11265 child_die = sibling_die (child_die);
11266 }
11267
34eaf542
TT
11268 /* Attach template arguments to type. */
11269 if (! VEC_empty (symbolp, template_args))
11270 {
11271 ALLOCATE_CPLUS_STRUCT_TYPE (type);
11272 TYPE_N_TEMPLATE_ARGUMENTS (type)
11273 = VEC_length (symbolp, template_args);
11274 TYPE_TEMPLATE_ARGUMENTS (type)
11275 = obstack_alloc (&objfile->objfile_obstack,
11276 (TYPE_N_TEMPLATE_ARGUMENTS (type)
11277 * sizeof (struct symbol *)));
11278 memcpy (TYPE_TEMPLATE_ARGUMENTS (type),
11279 VEC_address (symbolp, template_args),
11280 (TYPE_N_TEMPLATE_ARGUMENTS (type)
11281 * sizeof (struct symbol *)));
11282 VEC_free (symbolp, template_args);
11283 }
11284
c906108c
SS
11285 /* Attach fields and member functions to the type. */
11286 if (fi.nfields)
e7c27a73 11287 dwarf2_attach_fields_to_type (&fi, type, cu);
c906108c
SS
11288 if (fi.nfnfields)
11289 {
e7c27a73 11290 dwarf2_attach_fn_fields_to_type (&fi, type, cu);
c906108c 11291
c5aa993b 11292 /* Get the type which refers to the base class (possibly this
c906108c 11293 class itself) which contains the vtable pointer for the current
0d564a31
DJ
11294 class from the DW_AT_containing_type attribute. This use of
11295 DW_AT_containing_type is a GNU extension. */
c906108c 11296
e142c38c 11297 if (dwarf2_attr (die, DW_AT_containing_type, cu) != NULL)
c906108c 11298 {
e7c27a73 11299 struct type *t = die_containing_type (die, cu);
c906108c
SS
11300
11301 TYPE_VPTR_BASETYPE (type) = t;
11302 if (type == t)
11303 {
c906108c
SS
11304 int i;
11305
11306 /* Our own class provides vtbl ptr. */
11307 for (i = TYPE_NFIELDS (t) - 1;
11308 i >= TYPE_N_BASECLASSES (t);
11309 --i)
11310 {
0d5cff50 11311 const char *fieldname = TYPE_FIELD_NAME (t, i);
c906108c 11312
1168df01 11313 if (is_vtable_name (fieldname, cu))
c906108c
SS
11314 {
11315 TYPE_VPTR_FIELDNO (type) = i;
11316 break;
11317 }
11318 }
11319
11320 /* Complain if virtual function table field not found. */
11321 if (i < TYPE_N_BASECLASSES (t))
4d3c2250 11322 complaint (&symfile_complaints,
3e43a32a
MS
11323 _("virtual function table pointer "
11324 "not found when defining class '%s'"),
4d3c2250
KB
11325 TYPE_TAG_NAME (type) ? TYPE_TAG_NAME (type) :
11326 "");
c906108c
SS
11327 }
11328 else
11329 {
11330 TYPE_VPTR_FIELDNO (type) = TYPE_VPTR_FIELDNO (t);
11331 }
11332 }
f6235d4c
EZ
11333 else if (cu->producer
11334 && strncmp (cu->producer,
11335 "IBM(R) XL C/C++ Advanced Edition", 32) == 0)
11336 {
11337 /* The IBM XLC compiler does not provide direct indication
11338 of the containing type, but the vtable pointer is
11339 always named __vfp. */
11340
11341 int i;
11342
11343 for (i = TYPE_NFIELDS (type) - 1;
11344 i >= TYPE_N_BASECLASSES (type);
11345 --i)
11346 {
11347 if (strcmp (TYPE_FIELD_NAME (type, i), "__vfp") == 0)
11348 {
11349 TYPE_VPTR_FIELDNO (type) = i;
11350 TYPE_VPTR_BASETYPE (type) = type;
11351 break;
11352 }
11353 }
11354 }
c906108c 11355 }
98751a41
JK
11356
11357 /* Copy fi.typedef_field_list linked list elements content into the
11358 allocated array TYPE_TYPEDEF_FIELD_ARRAY (type). */
11359 if (fi.typedef_field_list)
11360 {
11361 int i = fi.typedef_field_list_count;
11362
a0d7a4ff 11363 ALLOCATE_CPLUS_STRUCT_TYPE (type);
98751a41
JK
11364 TYPE_TYPEDEF_FIELD_ARRAY (type)
11365 = TYPE_ALLOC (type, sizeof (TYPE_TYPEDEF_FIELD (type, 0)) * i);
11366 TYPE_TYPEDEF_FIELD_COUNT (type) = i;
11367
11368 /* Reverse the list order to keep the debug info elements order. */
11369 while (--i >= 0)
11370 {
11371 struct typedef_field *dest, *src;
6e70227d 11372
98751a41
JK
11373 dest = &TYPE_TYPEDEF_FIELD (type, i);
11374 src = &fi.typedef_field_list->field;
11375 fi.typedef_field_list = fi.typedef_field_list->next;
11376 *dest = *src;
11377 }
11378 }
c767944b
DJ
11379
11380 do_cleanups (back_to);
eb2a6f42
TT
11381
11382 if (HAVE_CPLUS_STRUCT (type))
11383 TYPE_CPLUS_REALLY_JAVA (type) = cu->language == language_java;
c906108c 11384 }
63d06c5c 11385
bb5ed363 11386 quirk_gcc_member_function_pointer (type, objfile);
0b92b5bb 11387
90aeadfc
DC
11388 /* NOTE: carlton/2004-03-16: GCC 3.4 (or at least one of its
11389 snapshots) has been known to create a die giving a declaration
11390 for a class that has, as a child, a die giving a definition for a
11391 nested class. So we have to process our children even if the
11392 current die is a declaration. Normally, of course, a declaration
11393 won't have any children at all. */
134d01f1 11394
90aeadfc
DC
11395 while (child_die != NULL && child_die->tag)
11396 {
11397 if (child_die->tag == DW_TAG_member
11398 || child_die->tag == DW_TAG_variable
34eaf542
TT
11399 || child_die->tag == DW_TAG_inheritance
11400 || child_die->tag == DW_TAG_template_value_param
11401 || child_die->tag == DW_TAG_template_type_param)
134d01f1 11402 {
90aeadfc 11403 /* Do nothing. */
134d01f1 11404 }
90aeadfc
DC
11405 else
11406 process_die (child_die, cu);
134d01f1 11407
90aeadfc 11408 child_die = sibling_die (child_die);
134d01f1
DJ
11409 }
11410
fa4028e9
JB
11411 /* Do not consider external references. According to the DWARF standard,
11412 these DIEs are identified by the fact that they have no byte_size
11413 attribute, and a declaration attribute. */
11414 if (dwarf2_attr (die, DW_AT_byte_size, cu) != NULL
11415 || !die_is_declaration (die, cu))
c767944b 11416 new_symbol (die, type, cu);
134d01f1
DJ
11417}
11418
11419/* Given a DW_AT_enumeration_type die, set its type. We do not
11420 complete the type's fields yet, or create any symbols. */
c906108c 11421
f792889a 11422static struct type *
134d01f1 11423read_enumeration_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 11424{
e7c27a73 11425 struct objfile *objfile = cu->objfile;
c906108c 11426 struct type *type;
c906108c 11427 struct attribute *attr;
0114d602 11428 const char *name;
134d01f1 11429
348e048f
DE
11430 /* If the definition of this type lives in .debug_types, read that type.
11431 Don't follow DW_AT_specification though, that will take us back up
11432 the chain and we want to go down. */
45e58e77 11433 attr = dwarf2_attr_no_follow (die, DW_AT_signature);
348e048f
DE
11434 if (attr)
11435 {
11436 struct dwarf2_cu *type_cu = cu;
11437 struct die_info *type_die = follow_die_ref_or_sig (die, attr, &type_cu);
9a619af0 11438
348e048f 11439 type = read_type_die (type_die, type_cu);
9dc481d3
DE
11440
11441 /* TYPE_CU may not be the same as CU.
11442 Ensure TYPE is recorded in CU's type_hash table. */
348e048f
DE
11443 return set_die_type (die, type, cu);
11444 }
11445
c906108c
SS
11446 type = alloc_type (objfile);
11447
11448 TYPE_CODE (type) = TYPE_CODE_ENUM;
94af9270 11449 name = dwarf2_full_name (NULL, die, cu);
39cbfefa 11450 if (name != NULL)
0114d602 11451 TYPE_TAG_NAME (type) = (char *) name;
c906108c 11452
e142c38c 11453 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
c906108c
SS
11454 if (attr)
11455 {
11456 TYPE_LENGTH (type) = DW_UNSND (attr);
11457 }
11458 else
11459 {
11460 TYPE_LENGTH (type) = 0;
11461 }
11462
137033e9
JB
11463 /* The enumeration DIE can be incomplete. In Ada, any type can be
11464 declared as private in the package spec, and then defined only
11465 inside the package body. Such types are known as Taft Amendment
11466 Types. When another package uses such a type, an incomplete DIE
11467 may be generated by the compiler. */
02eb380e 11468 if (die_is_declaration (die, cu))
876cecd0 11469 TYPE_STUB (type) = 1;
02eb380e 11470
f792889a 11471 return set_die_type (die, type, cu);
134d01f1
DJ
11472}
11473
11474/* Given a pointer to a die which begins an enumeration, process all
11475 the dies that define the members of the enumeration, and create the
11476 symbol for the enumeration type.
11477
11478 NOTE: We reverse the order of the element list. */
11479
11480static void
11481process_enumeration_scope (struct die_info *die, struct dwarf2_cu *cu)
11482{
f792889a 11483 struct type *this_type;
134d01f1 11484
f792889a
DJ
11485 this_type = get_die_type (die, cu);
11486 if (this_type == NULL)
11487 this_type = read_enumeration_type (die, cu);
9dc481d3 11488
639d11d3 11489 if (die->child != NULL)
c906108c 11490 {
9dc481d3
DE
11491 struct die_info *child_die;
11492 struct symbol *sym;
11493 struct field *fields = NULL;
11494 int num_fields = 0;
11495 int unsigned_enum = 1;
11496 char *name;
cafec441
TT
11497 int flag_enum = 1;
11498 ULONGEST mask = 0;
9dc481d3 11499
639d11d3 11500 child_die = die->child;
c906108c
SS
11501 while (child_die && child_die->tag)
11502 {
11503 if (child_die->tag != DW_TAG_enumerator)
11504 {
e7c27a73 11505 process_die (child_die, cu);
c906108c
SS
11506 }
11507 else
11508 {
39cbfefa
DJ
11509 name = dwarf2_name (child_die, cu);
11510 if (name)
c906108c 11511 {
f792889a 11512 sym = new_symbol (child_die, this_type, cu);
c906108c 11513 if (SYMBOL_VALUE (sym) < 0)
cafec441
TT
11514 {
11515 unsigned_enum = 0;
11516 flag_enum = 0;
11517 }
11518 else if ((mask & SYMBOL_VALUE (sym)) != 0)
11519 flag_enum = 0;
11520 else
11521 mask |= SYMBOL_VALUE (sym);
c906108c
SS
11522
11523 if ((num_fields % DW_FIELD_ALLOC_CHUNK) == 0)
11524 {
11525 fields = (struct field *)
11526 xrealloc (fields,
11527 (num_fields + DW_FIELD_ALLOC_CHUNK)
c5aa993b 11528 * sizeof (struct field));
c906108c
SS
11529 }
11530
3567439c 11531 FIELD_NAME (fields[num_fields]) = SYMBOL_LINKAGE_NAME (sym);
c906108c 11532 FIELD_TYPE (fields[num_fields]) = NULL;
14e75d8e 11533 SET_FIELD_ENUMVAL (fields[num_fields], SYMBOL_VALUE (sym));
c906108c
SS
11534 FIELD_BITSIZE (fields[num_fields]) = 0;
11535
11536 num_fields++;
11537 }
11538 }
11539
11540 child_die = sibling_die (child_die);
11541 }
11542
11543 if (num_fields)
11544 {
f792889a
DJ
11545 TYPE_NFIELDS (this_type) = num_fields;
11546 TYPE_FIELDS (this_type) = (struct field *)
11547 TYPE_ALLOC (this_type, sizeof (struct field) * num_fields);
11548 memcpy (TYPE_FIELDS (this_type), fields,
c906108c 11549 sizeof (struct field) * num_fields);
b8c9b27d 11550 xfree (fields);
c906108c
SS
11551 }
11552 if (unsigned_enum)
876cecd0 11553 TYPE_UNSIGNED (this_type) = 1;
cafec441
TT
11554 if (flag_enum)
11555 TYPE_FLAG_ENUM (this_type) = 1;
c906108c 11556 }
134d01f1 11557
6c83ed52
TT
11558 /* If we are reading an enum from a .debug_types unit, and the enum
11559 is a declaration, and the enum is not the signatured type in the
11560 unit, then we do not want to add a symbol for it. Adding a
11561 symbol would in some cases obscure the true definition of the
11562 enum, giving users an incomplete type when the definition is
11563 actually available. Note that we do not want to do this for all
11564 enums which are just declarations, because C++0x allows forward
11565 enum declarations. */
3019eac3 11566 if (cu->per_cu->is_debug_types
6c83ed52
TT
11567 && die_is_declaration (die, cu))
11568 {
52dc124a 11569 struct signatured_type *sig_type;
6c83ed52 11570
52dc124a 11571 sig_type
6c83ed52 11572 = lookup_signatured_type_at_offset (dwarf2_per_objfile->objfile,
3019eac3 11573 cu->per_cu->info_or_types_section,
6c83ed52 11574 cu->per_cu->offset);
3019eac3
DE
11575 gdb_assert (sig_type->type_offset_in_section.sect_off != 0);
11576 if (sig_type->type_offset_in_section.sect_off != die->offset.sect_off)
6c83ed52
TT
11577 return;
11578 }
11579
f792889a 11580 new_symbol (die, this_type, cu);
c906108c
SS
11581}
11582
11583/* Extract all information from a DW_TAG_array_type DIE and put it in
11584 the DIE's type field. For now, this only handles one dimensional
11585 arrays. */
11586
f792889a 11587static struct type *
e7c27a73 11588read_array_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 11589{
e7c27a73 11590 struct objfile *objfile = cu->objfile;
c906108c 11591 struct die_info *child_die;
7e314c57 11592 struct type *type;
c906108c
SS
11593 struct type *element_type, *range_type, *index_type;
11594 struct type **range_types = NULL;
11595 struct attribute *attr;
11596 int ndim = 0;
11597 struct cleanup *back_to;
39cbfefa 11598 char *name;
c906108c 11599
e7c27a73 11600 element_type = die_type (die, cu);
c906108c 11601
7e314c57
JK
11602 /* The die_type call above may have already set the type for this DIE. */
11603 type = get_die_type (die, cu);
11604 if (type)
11605 return type;
11606
c906108c
SS
11607 /* Irix 6.2 native cc creates array types without children for
11608 arrays with unspecified length. */
639d11d3 11609 if (die->child == NULL)
c906108c 11610 {
46bf5051 11611 index_type = objfile_type (objfile)->builtin_int;
c906108c 11612 range_type = create_range_type (NULL, index_type, 0, -1);
f792889a
DJ
11613 type = create_array_type (NULL, element_type, range_type);
11614 return set_die_type (die, type, cu);
c906108c
SS
11615 }
11616
11617 back_to = make_cleanup (null_cleanup, NULL);
639d11d3 11618 child_die = die->child;
c906108c
SS
11619 while (child_die && child_die->tag)
11620 {
11621 if (child_die->tag == DW_TAG_subrange_type)
11622 {
f792889a 11623 struct type *child_type = read_type_die (child_die, cu);
9a619af0 11624
f792889a 11625 if (child_type != NULL)
a02abb62 11626 {
0963b4bd
MS
11627 /* The range type was succesfully read. Save it for the
11628 array type creation. */
a02abb62
JB
11629 if ((ndim % DW_FIELD_ALLOC_CHUNK) == 0)
11630 {
11631 range_types = (struct type **)
11632 xrealloc (range_types, (ndim + DW_FIELD_ALLOC_CHUNK)
11633 * sizeof (struct type *));
11634 if (ndim == 0)
11635 make_cleanup (free_current_contents, &range_types);
11636 }
f792889a 11637 range_types[ndim++] = child_type;
a02abb62 11638 }
c906108c
SS
11639 }
11640 child_die = sibling_die (child_die);
11641 }
11642
11643 /* Dwarf2 dimensions are output from left to right, create the
11644 necessary array types in backwards order. */
7ca2d3a3 11645
c906108c 11646 type = element_type;
7ca2d3a3
DL
11647
11648 if (read_array_order (die, cu) == DW_ORD_col_major)
11649 {
11650 int i = 0;
9a619af0 11651
7ca2d3a3
DL
11652 while (i < ndim)
11653 type = create_array_type (NULL, type, range_types[i++]);
11654 }
11655 else
11656 {
11657 while (ndim-- > 0)
11658 type = create_array_type (NULL, type, range_types[ndim]);
11659 }
c906108c 11660
f5f8a009
EZ
11661 /* Understand Dwarf2 support for vector types (like they occur on
11662 the PowerPC w/ AltiVec). Gcc just adds another attribute to the
11663 array type. This is not part of the Dwarf2/3 standard yet, but a
11664 custom vendor extension. The main difference between a regular
11665 array and the vector variant is that vectors are passed by value
11666 to functions. */
e142c38c 11667 attr = dwarf2_attr (die, DW_AT_GNU_vector, cu);
f5f8a009 11668 if (attr)
ea37ba09 11669 make_vector_type (type);
f5f8a009 11670
dbc98a8b
KW
11671 /* The DIE may have DW_AT_byte_size set. For example an OpenCL
11672 implementation may choose to implement triple vectors using this
11673 attribute. */
11674 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
11675 if (attr)
11676 {
11677 if (DW_UNSND (attr) >= TYPE_LENGTH (type))
11678 TYPE_LENGTH (type) = DW_UNSND (attr);
11679 else
3e43a32a
MS
11680 complaint (&symfile_complaints,
11681 _("DW_AT_byte_size for array type smaller "
11682 "than the total size of elements"));
dbc98a8b
KW
11683 }
11684
39cbfefa
DJ
11685 name = dwarf2_name (die, cu);
11686 if (name)
11687 TYPE_NAME (type) = name;
6e70227d 11688
0963b4bd 11689 /* Install the type in the die. */
7e314c57
JK
11690 set_die_type (die, type, cu);
11691
11692 /* set_die_type should be already done. */
b4ba55a1
JB
11693 set_descriptive_type (type, die, cu);
11694
c906108c
SS
11695 do_cleanups (back_to);
11696
7e314c57 11697 return type;
c906108c
SS
11698}
11699
7ca2d3a3 11700static enum dwarf_array_dim_ordering
6e70227d 11701read_array_order (struct die_info *die, struct dwarf2_cu *cu)
7ca2d3a3
DL
11702{
11703 struct attribute *attr;
11704
11705 attr = dwarf2_attr (die, DW_AT_ordering, cu);
11706
11707 if (attr) return DW_SND (attr);
11708
0963b4bd
MS
11709 /* GNU F77 is a special case, as at 08/2004 array type info is the
11710 opposite order to the dwarf2 specification, but data is still
11711 laid out as per normal fortran.
7ca2d3a3 11712
0963b4bd
MS
11713 FIXME: dsl/2004-8-20: If G77 is ever fixed, this will also need
11714 version checking. */
7ca2d3a3 11715
905e0470
PM
11716 if (cu->language == language_fortran
11717 && cu->producer && strstr (cu->producer, "GNU F77"))
7ca2d3a3
DL
11718 {
11719 return DW_ORD_row_major;
11720 }
11721
6e70227d 11722 switch (cu->language_defn->la_array_ordering)
7ca2d3a3
DL
11723 {
11724 case array_column_major:
11725 return DW_ORD_col_major;
11726 case array_row_major:
11727 default:
11728 return DW_ORD_row_major;
11729 };
11730}
11731
72019c9c 11732/* Extract all information from a DW_TAG_set_type DIE and put it in
0963b4bd 11733 the DIE's type field. */
72019c9c 11734
f792889a 11735static struct type *
72019c9c
GM
11736read_set_type (struct die_info *die, struct dwarf2_cu *cu)
11737{
7e314c57
JK
11738 struct type *domain_type, *set_type;
11739 struct attribute *attr;
f792889a 11740
7e314c57
JK
11741 domain_type = die_type (die, cu);
11742
11743 /* The die_type call above may have already set the type for this DIE. */
11744 set_type = get_die_type (die, cu);
11745 if (set_type)
11746 return set_type;
11747
11748 set_type = create_set_type (NULL, domain_type);
11749
11750 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
d09039dd
PM
11751 if (attr)
11752 TYPE_LENGTH (set_type) = DW_UNSND (attr);
7e314c57 11753
f792889a 11754 return set_die_type (die, set_type, cu);
72019c9c 11755}
7ca2d3a3 11756
0971de02
TT
11757/* A helper for read_common_block that creates a locexpr baton.
11758 SYM is the symbol which we are marking as computed.
11759 COMMON_DIE is the DIE for the common block.
11760 COMMON_LOC is the location expression attribute for the common
11761 block itself.
11762 MEMBER_LOC is the location expression attribute for the particular
11763 member of the common block that we are processing.
11764 CU is the CU from which the above come. */
11765
11766static void
11767mark_common_block_symbol_computed (struct symbol *sym,
11768 struct die_info *common_die,
11769 struct attribute *common_loc,
11770 struct attribute *member_loc,
11771 struct dwarf2_cu *cu)
11772{
11773 struct objfile *objfile = dwarf2_per_objfile->objfile;
11774 struct dwarf2_locexpr_baton *baton;
11775 gdb_byte *ptr;
11776 unsigned int cu_off;
11777 enum bfd_endian byte_order = gdbarch_byte_order (get_objfile_arch (objfile));
11778 LONGEST offset = 0;
11779
11780 gdb_assert (common_loc && member_loc);
11781 gdb_assert (attr_form_is_block (common_loc));
11782 gdb_assert (attr_form_is_block (member_loc)
11783 || attr_form_is_constant (member_loc));
11784
11785 baton = obstack_alloc (&objfile->objfile_obstack,
11786 sizeof (struct dwarf2_locexpr_baton));
11787 baton->per_cu = cu->per_cu;
11788 gdb_assert (baton->per_cu);
11789
11790 baton->size = 5 /* DW_OP_call4 */ + 1 /* DW_OP_plus */;
11791
11792 if (attr_form_is_constant (member_loc))
11793 {
11794 offset = dwarf2_get_attr_constant_value (member_loc, 0);
11795 baton->size += 1 /* DW_OP_addr */ + cu->header.addr_size;
11796 }
11797 else
11798 baton->size += DW_BLOCK (member_loc)->size;
11799
11800 ptr = obstack_alloc (&objfile->objfile_obstack, baton->size);
11801 baton->data = ptr;
11802
11803 *ptr++ = DW_OP_call4;
11804 cu_off = common_die->offset.sect_off - cu->per_cu->offset.sect_off;
11805 store_unsigned_integer (ptr, 4, byte_order, cu_off);
11806 ptr += 4;
11807
11808 if (attr_form_is_constant (member_loc))
11809 {
11810 *ptr++ = DW_OP_addr;
11811 store_unsigned_integer (ptr, cu->header.addr_size, byte_order, offset);
11812 ptr += cu->header.addr_size;
11813 }
11814 else
11815 {
11816 /* We have to copy the data here, because DW_OP_call4 will only
11817 use a DW_AT_location attribute. */
11818 memcpy (ptr, DW_BLOCK (member_loc)->data, DW_BLOCK (member_loc)->size);
11819 ptr += DW_BLOCK (member_loc)->size;
11820 }
11821
11822 *ptr++ = DW_OP_plus;
11823 gdb_assert (ptr - baton->data == baton->size);
11824
11825 SYMBOL_COMPUTED_OPS (sym) = &dwarf2_locexpr_funcs;
11826 SYMBOL_LOCATION_BATON (sym) = baton;
11827 SYMBOL_CLASS (sym) = LOC_COMPUTED;
11828}
11829
4357ac6c
TT
11830/* Create appropriate locally-scoped variables for all the
11831 DW_TAG_common_block entries. Also create a struct common_block
11832 listing all such variables for `info common'. COMMON_BLOCK_DOMAIN
11833 is used to sepate the common blocks name namespace from regular
11834 variable names. */
c906108c
SS
11835
11836static void
e7c27a73 11837read_common_block (struct die_info *die, struct dwarf2_cu *cu)
c906108c 11838{
0971de02
TT
11839 struct attribute *attr;
11840
11841 attr = dwarf2_attr (die, DW_AT_location, cu);
11842 if (attr)
11843 {
11844 /* Support the .debug_loc offsets. */
11845 if (attr_form_is_block (attr))
11846 {
11847 /* Ok. */
11848 }
11849 else if (attr_form_is_section_offset (attr))
11850 {
11851 dwarf2_complex_location_expr_complaint ();
11852 attr = NULL;
11853 }
11854 else
11855 {
11856 dwarf2_invalid_attrib_class_complaint ("DW_AT_location",
11857 "common block member");
11858 attr = NULL;
11859 }
11860 }
11861
639d11d3 11862 if (die->child != NULL)
c906108c 11863 {
4357ac6c
TT
11864 struct objfile *objfile = cu->objfile;
11865 struct die_info *child_die;
11866 size_t n_entries = 0, size;
11867 struct common_block *common_block;
11868 struct symbol *sym;
74ac6d43 11869
4357ac6c
TT
11870 for (child_die = die->child;
11871 child_die && child_die->tag;
11872 child_die = sibling_die (child_die))
11873 ++n_entries;
11874
11875 size = (sizeof (struct common_block)
11876 + (n_entries - 1) * sizeof (struct symbol *));
11877 common_block = obstack_alloc (&objfile->objfile_obstack, size);
11878 memset (common_block->contents, 0, n_entries * sizeof (struct symbol *));
11879 common_block->n_entries = 0;
11880
11881 for (child_die = die->child;
11882 child_die && child_die->tag;
11883 child_die = sibling_die (child_die))
11884 {
11885 /* Create the symbol in the DW_TAG_common_block block in the current
11886 symbol scope. */
e7c27a73 11887 sym = new_symbol (child_die, NULL, cu);
0971de02
TT
11888 if (sym != NULL)
11889 {
11890 struct attribute *member_loc;
11891
11892 common_block->contents[common_block->n_entries++] = sym;
11893
11894 member_loc = dwarf2_attr (child_die, DW_AT_data_member_location,
11895 cu);
11896 if (member_loc)
11897 {
11898 /* GDB has handled this for a long time, but it is
11899 not specified by DWARF. It seems to have been
11900 emitted by gfortran at least as recently as:
11901 http://gcc.gnu.org/bugzilla/show_bug.cgi?id=23057. */
11902 complaint (&symfile_complaints,
11903 _("Variable in common block has "
11904 "DW_AT_data_member_location "
11905 "- DIE at 0x%x [in module %s]"),
11906 child_die->offset.sect_off, cu->objfile->name);
11907
11908 if (attr_form_is_section_offset (member_loc))
11909 dwarf2_complex_location_expr_complaint ();
11910 else if (attr_form_is_constant (member_loc)
11911 || attr_form_is_block (member_loc))
11912 {
11913 if (attr)
11914 mark_common_block_symbol_computed (sym, die, attr,
11915 member_loc, cu);
11916 }
11917 else
11918 dwarf2_complex_location_expr_complaint ();
11919 }
11920 }
c906108c 11921 }
4357ac6c
TT
11922
11923 sym = new_symbol (die, objfile_type (objfile)->builtin_void, cu);
11924 SYMBOL_VALUE_COMMON_BLOCK (sym) = common_block;
c906108c
SS
11925 }
11926}
11927
0114d602 11928/* Create a type for a C++ namespace. */
d9fa45fe 11929
0114d602
DJ
11930static struct type *
11931read_namespace_type (struct die_info *die, struct dwarf2_cu *cu)
d9fa45fe 11932{
e7c27a73 11933 struct objfile *objfile = cu->objfile;
0114d602 11934 const char *previous_prefix, *name;
9219021c 11935 int is_anonymous;
0114d602
DJ
11936 struct type *type;
11937
11938 /* For extensions, reuse the type of the original namespace. */
11939 if (dwarf2_attr (die, DW_AT_extension, cu) != NULL)
11940 {
11941 struct die_info *ext_die;
11942 struct dwarf2_cu *ext_cu = cu;
9a619af0 11943
0114d602
DJ
11944 ext_die = dwarf2_extension (die, &ext_cu);
11945 type = read_type_die (ext_die, ext_cu);
9dc481d3
DE
11946
11947 /* EXT_CU may not be the same as CU.
11948 Ensure TYPE is recorded in CU's type_hash table. */
0114d602
DJ
11949 return set_die_type (die, type, cu);
11950 }
9219021c 11951
e142c38c 11952 name = namespace_name (die, &is_anonymous, cu);
9219021c
DC
11953
11954 /* Now build the name of the current namespace. */
11955
0114d602
DJ
11956 previous_prefix = determine_prefix (die, cu);
11957 if (previous_prefix[0] != '\0')
11958 name = typename_concat (&objfile->objfile_obstack,
f55ee35c 11959 previous_prefix, name, 0, cu);
0114d602
DJ
11960
11961 /* Create the type. */
11962 type = init_type (TYPE_CODE_NAMESPACE, 0, 0, NULL,
11963 objfile);
11964 TYPE_NAME (type) = (char *) name;
11965 TYPE_TAG_NAME (type) = TYPE_NAME (type);
11966
60531b24 11967 return set_die_type (die, type, cu);
0114d602
DJ
11968}
11969
11970/* Read a C++ namespace. */
11971
11972static void
11973read_namespace (struct die_info *die, struct dwarf2_cu *cu)
11974{
11975 struct objfile *objfile = cu->objfile;
0114d602 11976 int is_anonymous;
9219021c 11977
5c4e30ca
DC
11978 /* Add a symbol associated to this if we haven't seen the namespace
11979 before. Also, add a using directive if it's an anonymous
11980 namespace. */
9219021c 11981
f2f0e013 11982 if (dwarf2_attr (die, DW_AT_extension, cu) == NULL)
5c4e30ca
DC
11983 {
11984 struct type *type;
11985
0114d602 11986 type = read_type_die (die, cu);
e7c27a73 11987 new_symbol (die, type, cu);
5c4e30ca 11988
e8e80198 11989 namespace_name (die, &is_anonymous, cu);
5c4e30ca 11990 if (is_anonymous)
0114d602
DJ
11991 {
11992 const char *previous_prefix = determine_prefix (die, cu);
9a619af0 11993
c0cc3a76 11994 cp_add_using_directive (previous_prefix, TYPE_NAME (type), NULL,
32019081 11995 NULL, NULL, &objfile->objfile_obstack);
0114d602 11996 }
5c4e30ca 11997 }
9219021c 11998
639d11d3 11999 if (die->child != NULL)
d9fa45fe 12000 {
639d11d3 12001 struct die_info *child_die = die->child;
6e70227d 12002
d9fa45fe
DC
12003 while (child_die && child_die->tag)
12004 {
e7c27a73 12005 process_die (child_die, cu);
d9fa45fe
DC
12006 child_die = sibling_die (child_die);
12007 }
12008 }
38d518c9
EZ
12009}
12010
f55ee35c
JK
12011/* Read a Fortran module as type. This DIE can be only a declaration used for
12012 imported module. Still we need that type as local Fortran "use ... only"
12013 declaration imports depend on the created type in determine_prefix. */
12014
12015static struct type *
12016read_module_type (struct die_info *die, struct dwarf2_cu *cu)
12017{
12018 struct objfile *objfile = cu->objfile;
12019 char *module_name;
12020 struct type *type;
12021
12022 module_name = dwarf2_name (die, cu);
12023 if (!module_name)
3e43a32a
MS
12024 complaint (&symfile_complaints,
12025 _("DW_TAG_module has no name, offset 0x%x"),
b64f50a1 12026 die->offset.sect_off);
f55ee35c
JK
12027 type = init_type (TYPE_CODE_MODULE, 0, 0, module_name, objfile);
12028
12029 /* determine_prefix uses TYPE_TAG_NAME. */
12030 TYPE_TAG_NAME (type) = TYPE_NAME (type);
12031
12032 return set_die_type (die, type, cu);
12033}
12034
5d7cb8df
JK
12035/* Read a Fortran module. */
12036
12037static void
12038read_module (struct die_info *die, struct dwarf2_cu *cu)
12039{
12040 struct die_info *child_die = die->child;
12041
5d7cb8df
JK
12042 while (child_die && child_die->tag)
12043 {
12044 process_die (child_die, cu);
12045 child_die = sibling_die (child_die);
12046 }
12047}
12048
38d518c9
EZ
12049/* Return the name of the namespace represented by DIE. Set
12050 *IS_ANONYMOUS to tell whether or not the namespace is an anonymous
12051 namespace. */
12052
12053static const char *
e142c38c 12054namespace_name (struct die_info *die, int *is_anonymous, struct dwarf2_cu *cu)
38d518c9
EZ
12055{
12056 struct die_info *current_die;
12057 const char *name = NULL;
12058
12059 /* Loop through the extensions until we find a name. */
12060
12061 for (current_die = die;
12062 current_die != NULL;
f2f0e013 12063 current_die = dwarf2_extension (die, &cu))
38d518c9 12064 {
e142c38c 12065 name = dwarf2_name (current_die, cu);
38d518c9
EZ
12066 if (name != NULL)
12067 break;
12068 }
12069
12070 /* Is it an anonymous namespace? */
12071
12072 *is_anonymous = (name == NULL);
12073 if (*is_anonymous)
2b1dbab0 12074 name = CP_ANONYMOUS_NAMESPACE_STR;
38d518c9
EZ
12075
12076 return name;
d9fa45fe
DC
12077}
12078
c906108c
SS
12079/* Extract all information from a DW_TAG_pointer_type DIE and add to
12080 the user defined type vector. */
12081
f792889a 12082static struct type *
e7c27a73 12083read_tag_pointer_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 12084{
5e2b427d 12085 struct gdbarch *gdbarch = get_objfile_arch (cu->objfile);
e7c27a73 12086 struct comp_unit_head *cu_header = &cu->header;
c906108c 12087 struct type *type;
8b2dbe47
KB
12088 struct attribute *attr_byte_size;
12089 struct attribute *attr_address_class;
12090 int byte_size, addr_class;
7e314c57
JK
12091 struct type *target_type;
12092
12093 target_type = die_type (die, cu);
c906108c 12094
7e314c57
JK
12095 /* The die_type call above may have already set the type for this DIE. */
12096 type = get_die_type (die, cu);
12097 if (type)
12098 return type;
12099
12100 type = lookup_pointer_type (target_type);
8b2dbe47 12101
e142c38c 12102 attr_byte_size = dwarf2_attr (die, DW_AT_byte_size, cu);
8b2dbe47
KB
12103 if (attr_byte_size)
12104 byte_size = DW_UNSND (attr_byte_size);
c906108c 12105 else
8b2dbe47
KB
12106 byte_size = cu_header->addr_size;
12107
e142c38c 12108 attr_address_class = dwarf2_attr (die, DW_AT_address_class, cu);
8b2dbe47
KB
12109 if (attr_address_class)
12110 addr_class = DW_UNSND (attr_address_class);
12111 else
12112 addr_class = DW_ADDR_none;
12113
12114 /* If the pointer size or address class is different than the
12115 default, create a type variant marked as such and set the
12116 length accordingly. */
12117 if (TYPE_LENGTH (type) != byte_size || addr_class != DW_ADDR_none)
c906108c 12118 {
5e2b427d 12119 if (gdbarch_address_class_type_flags_p (gdbarch))
8b2dbe47
KB
12120 {
12121 int type_flags;
12122
849957d9 12123 type_flags = gdbarch_address_class_type_flags
5e2b427d 12124 (gdbarch, byte_size, addr_class);
876cecd0
TT
12125 gdb_assert ((type_flags & ~TYPE_INSTANCE_FLAG_ADDRESS_CLASS_ALL)
12126 == 0);
8b2dbe47
KB
12127 type = make_type_with_address_space (type, type_flags);
12128 }
12129 else if (TYPE_LENGTH (type) != byte_size)
12130 {
3e43a32a
MS
12131 complaint (&symfile_complaints,
12132 _("invalid pointer size %d"), byte_size);
8b2dbe47 12133 }
6e70227d 12134 else
9a619af0
MS
12135 {
12136 /* Should we also complain about unhandled address classes? */
12137 }
c906108c 12138 }
8b2dbe47
KB
12139
12140 TYPE_LENGTH (type) = byte_size;
f792889a 12141 return set_die_type (die, type, cu);
c906108c
SS
12142}
12143
12144/* Extract all information from a DW_TAG_ptr_to_member_type DIE and add to
12145 the user defined type vector. */
12146
f792889a 12147static struct type *
e7c27a73 12148read_tag_ptr_to_member_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c
SS
12149{
12150 struct type *type;
12151 struct type *to_type;
12152 struct type *domain;
12153
e7c27a73
DJ
12154 to_type = die_type (die, cu);
12155 domain = die_containing_type (die, cu);
0d5de010 12156
7e314c57
JK
12157 /* The calls above may have already set the type for this DIE. */
12158 type = get_die_type (die, cu);
12159 if (type)
12160 return type;
12161
0d5de010
DJ
12162 if (TYPE_CODE (check_typedef (to_type)) == TYPE_CODE_METHOD)
12163 type = lookup_methodptr_type (to_type);
12164 else
12165 type = lookup_memberptr_type (to_type, domain);
c906108c 12166
f792889a 12167 return set_die_type (die, type, cu);
c906108c
SS
12168}
12169
12170/* Extract all information from a DW_TAG_reference_type DIE and add to
12171 the user defined type vector. */
12172
f792889a 12173static struct type *
e7c27a73 12174read_tag_reference_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 12175{
e7c27a73 12176 struct comp_unit_head *cu_header = &cu->header;
7e314c57 12177 struct type *type, *target_type;
c906108c
SS
12178 struct attribute *attr;
12179
7e314c57
JK
12180 target_type = die_type (die, cu);
12181
12182 /* The die_type call above may have already set the type for this DIE. */
12183 type = get_die_type (die, cu);
12184 if (type)
12185 return type;
12186
12187 type = lookup_reference_type (target_type);
e142c38c 12188 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
c906108c
SS
12189 if (attr)
12190 {
12191 TYPE_LENGTH (type) = DW_UNSND (attr);
12192 }
12193 else
12194 {
107d2387 12195 TYPE_LENGTH (type) = cu_header->addr_size;
c906108c 12196 }
f792889a 12197 return set_die_type (die, type, cu);
c906108c
SS
12198}
12199
f792889a 12200static struct type *
e7c27a73 12201read_tag_const_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 12202{
f792889a 12203 struct type *base_type, *cv_type;
c906108c 12204
e7c27a73 12205 base_type = die_type (die, cu);
7e314c57
JK
12206
12207 /* The die_type call above may have already set the type for this DIE. */
12208 cv_type = get_die_type (die, cu);
12209 if (cv_type)
12210 return cv_type;
12211
2f608a3a
KW
12212 /* In case the const qualifier is applied to an array type, the element type
12213 is so qualified, not the array type (section 6.7.3 of C99). */
12214 if (TYPE_CODE (base_type) == TYPE_CODE_ARRAY)
12215 {
12216 struct type *el_type, *inner_array;
12217
12218 base_type = copy_type (base_type);
12219 inner_array = base_type;
12220
12221 while (TYPE_CODE (TYPE_TARGET_TYPE (inner_array)) == TYPE_CODE_ARRAY)
12222 {
12223 TYPE_TARGET_TYPE (inner_array) =
12224 copy_type (TYPE_TARGET_TYPE (inner_array));
12225 inner_array = TYPE_TARGET_TYPE (inner_array);
12226 }
12227
12228 el_type = TYPE_TARGET_TYPE (inner_array);
12229 TYPE_TARGET_TYPE (inner_array) =
12230 make_cv_type (1, TYPE_VOLATILE (el_type), el_type, NULL);
12231
12232 return set_die_type (die, base_type, cu);
12233 }
12234
f792889a
DJ
12235 cv_type = make_cv_type (1, TYPE_VOLATILE (base_type), base_type, 0);
12236 return set_die_type (die, cv_type, cu);
c906108c
SS
12237}
12238
f792889a 12239static struct type *
e7c27a73 12240read_tag_volatile_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 12241{
f792889a 12242 struct type *base_type, *cv_type;
c906108c 12243
e7c27a73 12244 base_type = die_type (die, cu);
7e314c57
JK
12245
12246 /* The die_type call above may have already set the type for this DIE. */
12247 cv_type = get_die_type (die, cu);
12248 if (cv_type)
12249 return cv_type;
12250
f792889a
DJ
12251 cv_type = make_cv_type (TYPE_CONST (base_type), 1, base_type, 0);
12252 return set_die_type (die, cv_type, cu);
c906108c
SS
12253}
12254
12255/* Extract all information from a DW_TAG_string_type DIE and add to
12256 the user defined type vector. It isn't really a user defined type,
12257 but it behaves like one, with other DIE's using an AT_user_def_type
12258 attribute to reference it. */
12259
f792889a 12260static struct type *
e7c27a73 12261read_tag_string_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 12262{
e7c27a73 12263 struct objfile *objfile = cu->objfile;
3b7538c0 12264 struct gdbarch *gdbarch = get_objfile_arch (objfile);
c906108c
SS
12265 struct type *type, *range_type, *index_type, *char_type;
12266 struct attribute *attr;
12267 unsigned int length;
12268
e142c38c 12269 attr = dwarf2_attr (die, DW_AT_string_length, cu);
c906108c
SS
12270 if (attr)
12271 {
12272 length = DW_UNSND (attr);
12273 }
12274 else
12275 {
0963b4bd 12276 /* Check for the DW_AT_byte_size attribute. */
e142c38c 12277 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
b21b22e0
PS
12278 if (attr)
12279 {
12280 length = DW_UNSND (attr);
12281 }
12282 else
12283 {
12284 length = 1;
12285 }
c906108c 12286 }
6ccb9162 12287
46bf5051 12288 index_type = objfile_type (objfile)->builtin_int;
c906108c 12289 range_type = create_range_type (NULL, index_type, 1, length);
3b7538c0
UW
12290 char_type = language_string_char_type (cu->language_defn, gdbarch);
12291 type = create_string_type (NULL, char_type, range_type);
6ccb9162 12292
f792889a 12293 return set_die_type (die, type, cu);
c906108c
SS
12294}
12295
12296/* Handle DIES due to C code like:
12297
12298 struct foo
c5aa993b
JM
12299 {
12300 int (*funcp)(int a, long l);
12301 int b;
12302 };
c906108c 12303
0963b4bd 12304 ('funcp' generates a DW_TAG_subroutine_type DIE). */
c906108c 12305
f792889a 12306static struct type *
e7c27a73 12307read_subroutine_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 12308{
bb5ed363 12309 struct objfile *objfile = cu->objfile;
0963b4bd
MS
12310 struct type *type; /* Type that this function returns. */
12311 struct type *ftype; /* Function that returns above type. */
c906108c
SS
12312 struct attribute *attr;
12313
e7c27a73 12314 type = die_type (die, cu);
7e314c57
JK
12315
12316 /* The die_type call above may have already set the type for this DIE. */
12317 ftype = get_die_type (die, cu);
12318 if (ftype)
12319 return ftype;
12320
0c8b41f1 12321 ftype = lookup_function_type (type);
c906108c 12322
5b8101ae 12323 /* All functions in C++, Pascal and Java have prototypes. */
e142c38c 12324 attr = dwarf2_attr (die, DW_AT_prototyped, cu);
c906108c 12325 if ((attr && (DW_UNSND (attr) != 0))
987504bb 12326 || cu->language == language_cplus
5b8101ae
PM
12327 || cu->language == language_java
12328 || cu->language == language_pascal)
876cecd0 12329 TYPE_PROTOTYPED (ftype) = 1;
a6c727b2
DJ
12330 else if (producer_is_realview (cu->producer))
12331 /* RealView does not emit DW_AT_prototyped. We can not
12332 distinguish prototyped and unprototyped functions; default to
12333 prototyped, since that is more common in modern code (and
12334 RealView warns about unprototyped functions). */
12335 TYPE_PROTOTYPED (ftype) = 1;
c906108c 12336
c055b101
CV
12337 /* Store the calling convention in the type if it's available in
12338 the subroutine die. Otherwise set the calling convention to
12339 the default value DW_CC_normal. */
12340 attr = dwarf2_attr (die, DW_AT_calling_convention, cu);
54fcddd0
UW
12341 if (attr)
12342 TYPE_CALLING_CONVENTION (ftype) = DW_UNSND (attr);
12343 else if (cu->producer && strstr (cu->producer, "IBM XL C for OpenCL"))
12344 TYPE_CALLING_CONVENTION (ftype) = DW_CC_GDB_IBM_OpenCL;
12345 else
12346 TYPE_CALLING_CONVENTION (ftype) = DW_CC_normal;
76c10ea2
GM
12347
12348 /* We need to add the subroutine type to the die immediately so
12349 we don't infinitely recurse when dealing with parameters
0963b4bd 12350 declared as the same subroutine type. */
76c10ea2 12351 set_die_type (die, ftype, cu);
6e70227d 12352
639d11d3 12353 if (die->child != NULL)
c906108c 12354 {
bb5ed363 12355 struct type *void_type = objfile_type (objfile)->builtin_void;
c906108c 12356 struct die_info *child_die;
8072405b 12357 int nparams, iparams;
c906108c
SS
12358
12359 /* Count the number of parameters.
12360 FIXME: GDB currently ignores vararg functions, but knows about
12361 vararg member functions. */
8072405b 12362 nparams = 0;
639d11d3 12363 child_die = die->child;
c906108c
SS
12364 while (child_die && child_die->tag)
12365 {
12366 if (child_die->tag == DW_TAG_formal_parameter)
12367 nparams++;
12368 else if (child_die->tag == DW_TAG_unspecified_parameters)
876cecd0 12369 TYPE_VARARGS (ftype) = 1;
c906108c
SS
12370 child_die = sibling_die (child_die);
12371 }
12372
12373 /* Allocate storage for parameters and fill them in. */
12374 TYPE_NFIELDS (ftype) = nparams;
12375 TYPE_FIELDS (ftype) = (struct field *)
ae5a43e0 12376 TYPE_ZALLOC (ftype, nparams * sizeof (struct field));
c906108c 12377
8072405b
JK
12378 /* TYPE_FIELD_TYPE must never be NULL. Pre-fill the array to ensure it
12379 even if we error out during the parameters reading below. */
12380 for (iparams = 0; iparams < nparams; iparams++)
12381 TYPE_FIELD_TYPE (ftype, iparams) = void_type;
12382
12383 iparams = 0;
639d11d3 12384 child_die = die->child;
c906108c
SS
12385 while (child_die && child_die->tag)
12386 {
12387 if (child_die->tag == DW_TAG_formal_parameter)
12388 {
3ce3b1ba
PA
12389 struct type *arg_type;
12390
12391 /* DWARF version 2 has no clean way to discern C++
12392 static and non-static member functions. G++ helps
12393 GDB by marking the first parameter for non-static
12394 member functions (which is the this pointer) as
12395 artificial. We pass this information to
12396 dwarf2_add_member_fn via TYPE_FIELD_ARTIFICIAL.
12397
12398 DWARF version 3 added DW_AT_object_pointer, which GCC
12399 4.5 does not yet generate. */
e142c38c 12400 attr = dwarf2_attr (child_die, DW_AT_artificial, cu);
c906108c
SS
12401 if (attr)
12402 TYPE_FIELD_ARTIFICIAL (ftype, iparams) = DW_UNSND (attr);
12403 else
418835cc
KS
12404 {
12405 TYPE_FIELD_ARTIFICIAL (ftype, iparams) = 0;
12406
12407 /* GCC/43521: In java, the formal parameter
12408 "this" is sometimes not marked with DW_AT_artificial. */
12409 if (cu->language == language_java)
12410 {
12411 const char *name = dwarf2_name (child_die, cu);
9a619af0 12412
418835cc
KS
12413 if (name && !strcmp (name, "this"))
12414 TYPE_FIELD_ARTIFICIAL (ftype, iparams) = 1;
12415 }
12416 }
3ce3b1ba
PA
12417 arg_type = die_type (child_die, cu);
12418
12419 /* RealView does not mark THIS as const, which the testsuite
12420 expects. GCC marks THIS as const in method definitions,
12421 but not in the class specifications (GCC PR 43053). */
12422 if (cu->language == language_cplus && !TYPE_CONST (arg_type)
12423 && TYPE_FIELD_ARTIFICIAL (ftype, iparams))
12424 {
12425 int is_this = 0;
12426 struct dwarf2_cu *arg_cu = cu;
12427 const char *name = dwarf2_name (child_die, cu);
12428
12429 attr = dwarf2_attr (die, DW_AT_object_pointer, cu);
12430 if (attr)
12431 {
12432 /* If the compiler emits this, use it. */
12433 if (follow_die_ref (die, attr, &arg_cu) == child_die)
12434 is_this = 1;
12435 }
12436 else if (name && strcmp (name, "this") == 0)
12437 /* Function definitions will have the argument names. */
12438 is_this = 1;
12439 else if (name == NULL && iparams == 0)
12440 /* Declarations may not have the names, so like
12441 elsewhere in GDB, assume an artificial first
12442 argument is "this". */
12443 is_this = 1;
12444
12445 if (is_this)
12446 arg_type = make_cv_type (1, TYPE_VOLATILE (arg_type),
12447 arg_type, 0);
12448 }
12449
12450 TYPE_FIELD_TYPE (ftype, iparams) = arg_type;
c906108c
SS
12451 iparams++;
12452 }
12453 child_die = sibling_die (child_die);
12454 }
12455 }
12456
76c10ea2 12457 return ftype;
c906108c
SS
12458}
12459
f792889a 12460static struct type *
e7c27a73 12461read_typedef (struct die_info *die, struct dwarf2_cu *cu)
c906108c 12462{
e7c27a73 12463 struct objfile *objfile = cu->objfile;
0114d602 12464 const char *name = NULL;
3c8e0968 12465 struct type *this_type, *target_type;
c906108c 12466
94af9270 12467 name = dwarf2_full_name (NULL, die, cu);
f792889a 12468 this_type = init_type (TYPE_CODE_TYPEDEF, 0,
0114d602
DJ
12469 TYPE_FLAG_TARGET_STUB, NULL, objfile);
12470 TYPE_NAME (this_type) = (char *) name;
f792889a 12471 set_die_type (die, this_type, cu);
3c8e0968
DE
12472 target_type = die_type (die, cu);
12473 if (target_type != this_type)
12474 TYPE_TARGET_TYPE (this_type) = target_type;
12475 else
12476 {
12477 /* Self-referential typedefs are, it seems, not allowed by the DWARF
12478 spec and cause infinite loops in GDB. */
12479 complaint (&symfile_complaints,
12480 _("Self-referential DW_TAG_typedef "
12481 "- DIE at 0x%x [in module %s]"),
b64f50a1 12482 die->offset.sect_off, objfile->name);
3c8e0968
DE
12483 TYPE_TARGET_TYPE (this_type) = NULL;
12484 }
f792889a 12485 return this_type;
c906108c
SS
12486}
12487
12488/* Find a representation of a given base type and install
12489 it in the TYPE field of the die. */
12490
f792889a 12491static struct type *
e7c27a73 12492read_base_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 12493{
e7c27a73 12494 struct objfile *objfile = cu->objfile;
c906108c
SS
12495 struct type *type;
12496 struct attribute *attr;
12497 int encoding = 0, size = 0;
39cbfefa 12498 char *name;
6ccb9162
UW
12499 enum type_code code = TYPE_CODE_INT;
12500 int type_flags = 0;
12501 struct type *target_type = NULL;
c906108c 12502
e142c38c 12503 attr = dwarf2_attr (die, DW_AT_encoding, cu);
c906108c
SS
12504 if (attr)
12505 {
12506 encoding = DW_UNSND (attr);
12507 }
e142c38c 12508 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
c906108c
SS
12509 if (attr)
12510 {
12511 size = DW_UNSND (attr);
12512 }
39cbfefa 12513 name = dwarf2_name (die, cu);
6ccb9162 12514 if (!name)
c906108c 12515 {
6ccb9162
UW
12516 complaint (&symfile_complaints,
12517 _("DW_AT_name missing from DW_TAG_base_type"));
c906108c 12518 }
6ccb9162
UW
12519
12520 switch (encoding)
c906108c 12521 {
6ccb9162
UW
12522 case DW_ATE_address:
12523 /* Turn DW_ATE_address into a void * pointer. */
12524 code = TYPE_CODE_PTR;
12525 type_flags |= TYPE_FLAG_UNSIGNED;
12526 target_type = init_type (TYPE_CODE_VOID, 1, 0, NULL, objfile);
12527 break;
12528 case DW_ATE_boolean:
12529 code = TYPE_CODE_BOOL;
12530 type_flags |= TYPE_FLAG_UNSIGNED;
12531 break;
12532 case DW_ATE_complex_float:
12533 code = TYPE_CODE_COMPLEX;
12534 target_type = init_type (TYPE_CODE_FLT, size / 2, 0, NULL, objfile);
12535 break;
12536 case DW_ATE_decimal_float:
12537 code = TYPE_CODE_DECFLOAT;
12538 break;
12539 case DW_ATE_float:
12540 code = TYPE_CODE_FLT;
12541 break;
12542 case DW_ATE_signed:
12543 break;
12544 case DW_ATE_unsigned:
12545 type_flags |= TYPE_FLAG_UNSIGNED;
3b2b8fea
TT
12546 if (cu->language == language_fortran
12547 && name
12548 && strncmp (name, "character(", sizeof ("character(") - 1) == 0)
12549 code = TYPE_CODE_CHAR;
6ccb9162
UW
12550 break;
12551 case DW_ATE_signed_char:
6e70227d 12552 if (cu->language == language_ada || cu->language == language_m2
3b2b8fea
TT
12553 || cu->language == language_pascal
12554 || cu->language == language_fortran)
6ccb9162
UW
12555 code = TYPE_CODE_CHAR;
12556 break;
12557 case DW_ATE_unsigned_char:
868a0084 12558 if (cu->language == language_ada || cu->language == language_m2
3b2b8fea
TT
12559 || cu->language == language_pascal
12560 || cu->language == language_fortran)
6ccb9162
UW
12561 code = TYPE_CODE_CHAR;
12562 type_flags |= TYPE_FLAG_UNSIGNED;
12563 break;
75079b2b
TT
12564 case DW_ATE_UTF:
12565 /* We just treat this as an integer and then recognize the
12566 type by name elsewhere. */
12567 break;
12568
6ccb9162
UW
12569 default:
12570 complaint (&symfile_complaints, _("unsupported DW_AT_encoding: '%s'"),
12571 dwarf_type_encoding_name (encoding));
12572 break;
c906108c 12573 }
6ccb9162 12574
0114d602
DJ
12575 type = init_type (code, size, type_flags, NULL, objfile);
12576 TYPE_NAME (type) = name;
6ccb9162
UW
12577 TYPE_TARGET_TYPE (type) = target_type;
12578
0114d602 12579 if (name && strcmp (name, "char") == 0)
876cecd0 12580 TYPE_NOSIGN (type) = 1;
0114d602 12581
f792889a 12582 return set_die_type (die, type, cu);
c906108c
SS
12583}
12584
a02abb62
JB
12585/* Read the given DW_AT_subrange DIE. */
12586
f792889a 12587static struct type *
a02abb62
JB
12588read_subrange_type (struct die_info *die, struct dwarf2_cu *cu)
12589{
12590 struct type *base_type;
12591 struct type *range_type;
12592 struct attribute *attr;
4fae6e18
JK
12593 LONGEST low, high;
12594 int low_default_is_valid;
39cbfefa 12595 char *name;
43bbcdc2 12596 LONGEST negative_mask;
e77813c8 12597
a02abb62 12598 base_type = die_type (die, cu);
953ac07e
JK
12599 /* Preserve BASE_TYPE's original type, just set its LENGTH. */
12600 check_typedef (base_type);
a02abb62 12601
7e314c57
JK
12602 /* The die_type call above may have already set the type for this DIE. */
12603 range_type = get_die_type (die, cu);
12604 if (range_type)
12605 return range_type;
12606
4fae6e18
JK
12607 /* Set LOW_DEFAULT_IS_VALID if current language and DWARF version allow
12608 omitting DW_AT_lower_bound. */
12609 switch (cu->language)
6e70227d 12610 {
4fae6e18
JK
12611 case language_c:
12612 case language_cplus:
12613 low = 0;
12614 low_default_is_valid = 1;
12615 break;
12616 case language_fortran:
12617 low = 1;
12618 low_default_is_valid = 1;
12619 break;
12620 case language_d:
12621 case language_java:
12622 case language_objc:
12623 low = 0;
12624 low_default_is_valid = (cu->header.version >= 4);
12625 break;
12626 case language_ada:
12627 case language_m2:
12628 case language_pascal:
a02abb62 12629 low = 1;
4fae6e18
JK
12630 low_default_is_valid = (cu->header.version >= 4);
12631 break;
12632 default:
12633 low = 0;
12634 low_default_is_valid = 0;
12635 break;
a02abb62
JB
12636 }
12637
dd5e6932
DJ
12638 /* FIXME: For variable sized arrays either of these could be
12639 a variable rather than a constant value. We'll allow it,
12640 but we don't know how to handle it. */
e142c38c 12641 attr = dwarf2_attr (die, DW_AT_lower_bound, cu);
a02abb62 12642 if (attr)
4fae6e18
JK
12643 low = dwarf2_get_attr_constant_value (attr, low);
12644 else if (!low_default_is_valid)
12645 complaint (&symfile_complaints, _("Missing DW_AT_lower_bound "
12646 "- DIE at 0x%x [in module %s]"),
12647 die->offset.sect_off, cu->objfile->name);
a02abb62 12648
e142c38c 12649 attr = dwarf2_attr (die, DW_AT_upper_bound, cu);
a02abb62 12650 if (attr)
6e70227d 12651 {
d48323d8 12652 if (attr_form_is_block (attr) || is_ref_attr (attr))
a02abb62
JB
12653 {
12654 /* GCC encodes arrays with unspecified or dynamic length
e77813c8 12655 with a DW_FORM_block1 attribute or a reference attribute.
a02abb62
JB
12656 FIXME: GDB does not yet know how to handle dynamic
12657 arrays properly, treat them as arrays with unspecified
12658 length for now.
12659
12660 FIXME: jimb/2003-09-22: GDB does not really know
12661 how to handle arrays of unspecified length
12662 either; we just represent them as zero-length
12663 arrays. Choose an appropriate upper bound given
12664 the lower bound we've computed above. */
12665 high = low - 1;
12666 }
12667 else
12668 high = dwarf2_get_attr_constant_value (attr, 1);
12669 }
e77813c8
PM
12670 else
12671 {
12672 attr = dwarf2_attr (die, DW_AT_count, cu);
12673 if (attr)
12674 {
12675 int count = dwarf2_get_attr_constant_value (attr, 1);
12676 high = low + count - 1;
12677 }
c2ff108b
JK
12678 else
12679 {
12680 /* Unspecified array length. */
12681 high = low - 1;
12682 }
e77813c8
PM
12683 }
12684
12685 /* Dwarf-2 specifications explicitly allows to create subrange types
12686 without specifying a base type.
12687 In that case, the base type must be set to the type of
12688 the lower bound, upper bound or count, in that order, if any of these
12689 three attributes references an object that has a type.
12690 If no base type is found, the Dwarf-2 specifications say that
12691 a signed integer type of size equal to the size of an address should
12692 be used.
12693 For the following C code: `extern char gdb_int [];'
12694 GCC produces an empty range DIE.
12695 FIXME: muller/2010-05-28: Possible references to object for low bound,
0963b4bd 12696 high bound or count are not yet handled by this code. */
e77813c8
PM
12697 if (TYPE_CODE (base_type) == TYPE_CODE_VOID)
12698 {
12699 struct objfile *objfile = cu->objfile;
12700 struct gdbarch *gdbarch = get_objfile_arch (objfile);
12701 int addr_size = gdbarch_addr_bit (gdbarch) /8;
12702 struct type *int_type = objfile_type (objfile)->builtin_int;
12703
12704 /* Test "int", "long int", and "long long int" objfile types,
12705 and select the first one having a size above or equal to the
12706 architecture address size. */
12707 if (int_type && TYPE_LENGTH (int_type) >= addr_size)
12708 base_type = int_type;
12709 else
12710 {
12711 int_type = objfile_type (objfile)->builtin_long;
12712 if (int_type && TYPE_LENGTH (int_type) >= addr_size)
12713 base_type = int_type;
12714 else
12715 {
12716 int_type = objfile_type (objfile)->builtin_long_long;
12717 if (int_type && TYPE_LENGTH (int_type) >= addr_size)
12718 base_type = int_type;
12719 }
12720 }
12721 }
a02abb62 12722
6e70227d 12723 negative_mask =
43bbcdc2
PH
12724 (LONGEST) -1 << (TYPE_LENGTH (base_type) * TARGET_CHAR_BIT - 1);
12725 if (!TYPE_UNSIGNED (base_type) && (low & negative_mask))
12726 low |= negative_mask;
12727 if (!TYPE_UNSIGNED (base_type) && (high & negative_mask))
12728 high |= negative_mask;
12729
a02abb62
JB
12730 range_type = create_range_type (NULL, base_type, low, high);
12731
bbb0eef6
JK
12732 /* Mark arrays with dynamic length at least as an array of unspecified
12733 length. GDB could check the boundary but before it gets implemented at
12734 least allow accessing the array elements. */
d48323d8 12735 if (attr && attr_form_is_block (attr))
bbb0eef6
JK
12736 TYPE_HIGH_BOUND_UNDEFINED (range_type) = 1;
12737
c2ff108b
JK
12738 /* Ada expects an empty array on no boundary attributes. */
12739 if (attr == NULL && cu->language != language_ada)
12740 TYPE_HIGH_BOUND_UNDEFINED (range_type) = 1;
12741
39cbfefa
DJ
12742 name = dwarf2_name (die, cu);
12743 if (name)
12744 TYPE_NAME (range_type) = name;
6e70227d 12745
e142c38c 12746 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
a02abb62
JB
12747 if (attr)
12748 TYPE_LENGTH (range_type) = DW_UNSND (attr);
12749
7e314c57
JK
12750 set_die_type (die, range_type, cu);
12751
12752 /* set_die_type should be already done. */
b4ba55a1
JB
12753 set_descriptive_type (range_type, die, cu);
12754
7e314c57 12755 return range_type;
a02abb62 12756}
6e70227d 12757
f792889a 12758static struct type *
81a17f79
JB
12759read_unspecified_type (struct die_info *die, struct dwarf2_cu *cu)
12760{
12761 struct type *type;
81a17f79 12762
81a17f79
JB
12763 /* For now, we only support the C meaning of an unspecified type: void. */
12764
0114d602
DJ
12765 type = init_type (TYPE_CODE_VOID, 0, 0, NULL, cu->objfile);
12766 TYPE_NAME (type) = dwarf2_name (die, cu);
81a17f79 12767
f792889a 12768 return set_die_type (die, type, cu);
81a17f79 12769}
a02abb62 12770
639d11d3
DC
12771/* Read a single die and all its descendents. Set the die's sibling
12772 field to NULL; set other fields in the die correctly, and set all
12773 of the descendents' fields correctly. Set *NEW_INFO_PTR to the
12774 location of the info_ptr after reading all of those dies. PARENT
12775 is the parent of the die in question. */
12776
12777static struct die_info *
dee91e82
DE
12778read_die_and_children (const struct die_reader_specs *reader,
12779 gdb_byte *info_ptr,
12780 gdb_byte **new_info_ptr,
12781 struct die_info *parent)
639d11d3
DC
12782{
12783 struct die_info *die;
fe1b8b76 12784 gdb_byte *cur_ptr;
639d11d3
DC
12785 int has_children;
12786
93311388 12787 cur_ptr = read_full_die (reader, &die, info_ptr, &has_children);
1d325ec1
DJ
12788 if (die == NULL)
12789 {
12790 *new_info_ptr = cur_ptr;
12791 return NULL;
12792 }
93311388 12793 store_in_ref_table (die, reader->cu);
639d11d3
DC
12794
12795 if (has_children)
348e048f 12796 die->child = read_die_and_siblings (reader, cur_ptr, new_info_ptr, die);
639d11d3
DC
12797 else
12798 {
12799 die->child = NULL;
12800 *new_info_ptr = cur_ptr;
12801 }
12802
12803 die->sibling = NULL;
12804 die->parent = parent;
12805 return die;
12806}
12807
12808/* Read a die, all of its descendents, and all of its siblings; set
12809 all of the fields of all of the dies correctly. Arguments are as
12810 in read_die_and_children. */
12811
12812static struct die_info *
93311388
DE
12813read_die_and_siblings (const struct die_reader_specs *reader,
12814 gdb_byte *info_ptr,
fe1b8b76 12815 gdb_byte **new_info_ptr,
639d11d3
DC
12816 struct die_info *parent)
12817{
12818 struct die_info *first_die, *last_sibling;
fe1b8b76 12819 gdb_byte *cur_ptr;
639d11d3 12820
c906108c 12821 cur_ptr = info_ptr;
639d11d3
DC
12822 first_die = last_sibling = NULL;
12823
12824 while (1)
c906108c 12825 {
639d11d3 12826 struct die_info *die
dee91e82 12827 = read_die_and_children (reader, cur_ptr, &cur_ptr, parent);
639d11d3 12828
1d325ec1 12829 if (die == NULL)
c906108c 12830 {
639d11d3
DC
12831 *new_info_ptr = cur_ptr;
12832 return first_die;
c906108c 12833 }
1d325ec1
DJ
12834
12835 if (!first_die)
12836 first_die = die;
c906108c 12837 else
1d325ec1
DJ
12838 last_sibling->sibling = die;
12839
12840 last_sibling = die;
c906108c 12841 }
c906108c
SS
12842}
12843
3019eac3
DE
12844/* Read a die and all its attributes, leave space for NUM_EXTRA_ATTRS
12845 attributes.
12846 The caller is responsible for filling in the extra attributes
12847 and updating (*DIEP)->num_attrs.
12848 Set DIEP to point to a newly allocated die with its information,
12849 except for its child, sibling, and parent fields.
12850 Set HAS_CHILDREN to tell whether the die has children or not. */
93311388
DE
12851
12852static gdb_byte *
3019eac3
DE
12853read_full_die_1 (const struct die_reader_specs *reader,
12854 struct die_info **diep, gdb_byte *info_ptr,
12855 int *has_children, int num_extra_attrs)
93311388 12856{
b64f50a1
JK
12857 unsigned int abbrev_number, bytes_read, i;
12858 sect_offset offset;
93311388
DE
12859 struct abbrev_info *abbrev;
12860 struct die_info *die;
12861 struct dwarf2_cu *cu = reader->cu;
12862 bfd *abfd = reader->abfd;
12863
b64f50a1 12864 offset.sect_off = info_ptr - reader->buffer;
93311388
DE
12865 abbrev_number = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
12866 info_ptr += bytes_read;
12867 if (!abbrev_number)
12868 {
12869 *diep = NULL;
12870 *has_children = 0;
12871 return info_ptr;
12872 }
12873
433df2d4 12874 abbrev = abbrev_table_lookup_abbrev (cu->abbrev_table, abbrev_number);
93311388 12875 if (!abbrev)
348e048f
DE
12876 error (_("Dwarf Error: could not find abbrev number %d [in module %s]"),
12877 abbrev_number,
12878 bfd_get_filename (abfd));
12879
3019eac3 12880 die = dwarf_alloc_die (cu, abbrev->num_attrs + num_extra_attrs);
93311388
DE
12881 die->offset = offset;
12882 die->tag = abbrev->tag;
12883 die->abbrev = abbrev_number;
12884
3019eac3
DE
12885 /* Make the result usable.
12886 The caller needs to update num_attrs after adding the extra
12887 attributes. */
93311388
DE
12888 die->num_attrs = abbrev->num_attrs;
12889
12890 for (i = 0; i < abbrev->num_attrs; ++i)
dee91e82
DE
12891 info_ptr = read_attribute (reader, &die->attrs[i], &abbrev->attrs[i],
12892 info_ptr);
93311388
DE
12893
12894 *diep = die;
12895 *has_children = abbrev->has_children;
12896 return info_ptr;
12897}
12898
3019eac3
DE
12899/* Read a die and all its attributes.
12900 Set DIEP to point to a newly allocated die with its information,
12901 except for its child, sibling, and parent fields.
12902 Set HAS_CHILDREN to tell whether the die has children or not. */
12903
12904static gdb_byte *
12905read_full_die (const struct die_reader_specs *reader,
12906 struct die_info **diep, gdb_byte *info_ptr,
12907 int *has_children)
12908{
12909 return read_full_die_1 (reader, diep, info_ptr, has_children, 0);
12910}
433df2d4
DE
12911\f
12912/* Abbreviation tables.
3019eac3 12913
433df2d4 12914 In DWARF version 2, the description of the debugging information is
c906108c
SS
12915 stored in a separate .debug_abbrev section. Before we read any
12916 dies from a section we read in all abbreviations and install them
433df2d4
DE
12917 in a hash table. */
12918
12919/* Allocate space for a struct abbrev_info object in ABBREV_TABLE. */
12920
12921static struct abbrev_info *
12922abbrev_table_alloc_abbrev (struct abbrev_table *abbrev_table)
12923{
12924 struct abbrev_info *abbrev;
12925
12926 abbrev = (struct abbrev_info *)
12927 obstack_alloc (&abbrev_table->abbrev_obstack, sizeof (struct abbrev_info));
12928 memset (abbrev, 0, sizeof (struct abbrev_info));
12929 return abbrev;
12930}
12931
12932/* Add an abbreviation to the table. */
c906108c
SS
12933
12934static void
433df2d4
DE
12935abbrev_table_add_abbrev (struct abbrev_table *abbrev_table,
12936 unsigned int abbrev_number,
12937 struct abbrev_info *abbrev)
12938{
12939 unsigned int hash_number;
12940
12941 hash_number = abbrev_number % ABBREV_HASH_SIZE;
12942 abbrev->next = abbrev_table->abbrevs[hash_number];
12943 abbrev_table->abbrevs[hash_number] = abbrev;
12944}
dee91e82 12945
433df2d4
DE
12946/* Look up an abbrev in the table.
12947 Returns NULL if the abbrev is not found. */
12948
12949static struct abbrev_info *
12950abbrev_table_lookup_abbrev (const struct abbrev_table *abbrev_table,
12951 unsigned int abbrev_number)
c906108c 12952{
433df2d4
DE
12953 unsigned int hash_number;
12954 struct abbrev_info *abbrev;
12955
12956 hash_number = abbrev_number % ABBREV_HASH_SIZE;
12957 abbrev = abbrev_table->abbrevs[hash_number];
12958
12959 while (abbrev)
12960 {
12961 if (abbrev->number == abbrev_number)
12962 return abbrev;
12963 abbrev = abbrev->next;
12964 }
12965 return NULL;
12966}
12967
12968/* Read in an abbrev table. */
12969
12970static struct abbrev_table *
12971abbrev_table_read_table (struct dwarf2_section_info *section,
12972 sect_offset offset)
12973{
12974 struct objfile *objfile = dwarf2_per_objfile->objfile;
12975 bfd *abfd = section->asection->owner;
12976 struct abbrev_table *abbrev_table;
fe1b8b76 12977 gdb_byte *abbrev_ptr;
c906108c
SS
12978 struct abbrev_info *cur_abbrev;
12979 unsigned int abbrev_number, bytes_read, abbrev_name;
433df2d4 12980 unsigned int abbrev_form;
f3dd6933
DJ
12981 struct attr_abbrev *cur_attrs;
12982 unsigned int allocated_attrs;
c906108c 12983
433df2d4 12984 abbrev_table = XMALLOC (struct abbrev_table);
f4dc4d17 12985 abbrev_table->offset = offset;
433df2d4
DE
12986 obstack_init (&abbrev_table->abbrev_obstack);
12987 abbrev_table->abbrevs = obstack_alloc (&abbrev_table->abbrev_obstack,
12988 (ABBREV_HASH_SIZE
12989 * sizeof (struct abbrev_info *)));
12990 memset (abbrev_table->abbrevs, 0,
12991 ABBREV_HASH_SIZE * sizeof (struct abbrev_info *));
c906108c 12992
433df2d4
DE
12993 dwarf2_read_section (objfile, section);
12994 abbrev_ptr = section->buffer + offset.sect_off;
c906108c
SS
12995 abbrev_number = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
12996 abbrev_ptr += bytes_read;
12997
f3dd6933
DJ
12998 allocated_attrs = ATTR_ALLOC_CHUNK;
12999 cur_attrs = xmalloc (allocated_attrs * sizeof (struct attr_abbrev));
6e70227d 13000
0963b4bd 13001 /* Loop until we reach an abbrev number of 0. */
c906108c
SS
13002 while (abbrev_number)
13003 {
433df2d4 13004 cur_abbrev = abbrev_table_alloc_abbrev (abbrev_table);
c906108c
SS
13005
13006 /* read in abbrev header */
13007 cur_abbrev->number = abbrev_number;
13008 cur_abbrev->tag = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
13009 abbrev_ptr += bytes_read;
13010 cur_abbrev->has_children = read_1_byte (abfd, abbrev_ptr);
13011 abbrev_ptr += 1;
13012
13013 /* now read in declarations */
13014 abbrev_name = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
13015 abbrev_ptr += bytes_read;
13016 abbrev_form = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
13017 abbrev_ptr += bytes_read;
13018 while (abbrev_name)
13019 {
f3dd6933 13020 if (cur_abbrev->num_attrs == allocated_attrs)
c906108c 13021 {
f3dd6933
DJ
13022 allocated_attrs += ATTR_ALLOC_CHUNK;
13023 cur_attrs
13024 = xrealloc (cur_attrs, (allocated_attrs
13025 * sizeof (struct attr_abbrev)));
c906108c 13026 }
ae038cb0 13027
f3dd6933
DJ
13028 cur_attrs[cur_abbrev->num_attrs].name = abbrev_name;
13029 cur_attrs[cur_abbrev->num_attrs++].form = abbrev_form;
c906108c
SS
13030 abbrev_name = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
13031 abbrev_ptr += bytes_read;
13032 abbrev_form = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
13033 abbrev_ptr += bytes_read;
13034 }
13035
433df2d4 13036 cur_abbrev->attrs = obstack_alloc (&abbrev_table->abbrev_obstack,
f3dd6933
DJ
13037 (cur_abbrev->num_attrs
13038 * sizeof (struct attr_abbrev)));
13039 memcpy (cur_abbrev->attrs, cur_attrs,
13040 cur_abbrev->num_attrs * sizeof (struct attr_abbrev));
13041
433df2d4 13042 abbrev_table_add_abbrev (abbrev_table, abbrev_number, cur_abbrev);
c906108c
SS
13043
13044 /* Get next abbreviation.
13045 Under Irix6 the abbreviations for a compilation unit are not
c5aa993b
JM
13046 always properly terminated with an abbrev number of 0.
13047 Exit loop if we encounter an abbreviation which we have
13048 already read (which means we are about to read the abbreviations
13049 for the next compile unit) or if the end of the abbreviation
13050 table is reached. */
433df2d4 13051 if ((unsigned int) (abbrev_ptr - section->buffer) >= section->size)
c906108c
SS
13052 break;
13053 abbrev_number = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
13054 abbrev_ptr += bytes_read;
433df2d4 13055 if (abbrev_table_lookup_abbrev (abbrev_table, abbrev_number) != NULL)
c906108c
SS
13056 break;
13057 }
f3dd6933
DJ
13058
13059 xfree (cur_attrs);
433df2d4 13060 return abbrev_table;
c906108c
SS
13061}
13062
433df2d4 13063/* Free the resources held by ABBREV_TABLE. */
c906108c 13064
c906108c 13065static void
433df2d4 13066abbrev_table_free (struct abbrev_table *abbrev_table)
c906108c 13067{
433df2d4
DE
13068 obstack_free (&abbrev_table->abbrev_obstack, NULL);
13069 xfree (abbrev_table);
c906108c
SS
13070}
13071
f4dc4d17
DE
13072/* Same as abbrev_table_free but as a cleanup.
13073 We pass in a pointer to the pointer to the table so that we can
13074 set the pointer to NULL when we're done. It also simplifies
13075 build_type_unit_groups. */
13076
13077static void
13078abbrev_table_free_cleanup (void *table_ptr)
13079{
13080 struct abbrev_table **abbrev_table_ptr = table_ptr;
13081
13082 if (*abbrev_table_ptr != NULL)
13083 abbrev_table_free (*abbrev_table_ptr);
13084 *abbrev_table_ptr = NULL;
13085}
13086
433df2d4
DE
13087/* Read the abbrev table for CU from ABBREV_SECTION. */
13088
13089static void
13090dwarf2_read_abbrevs (struct dwarf2_cu *cu,
13091 struct dwarf2_section_info *abbrev_section)
c906108c 13092{
433df2d4
DE
13093 cu->abbrev_table =
13094 abbrev_table_read_table (abbrev_section, cu->header.abbrev_offset);
13095}
c906108c 13096
433df2d4 13097/* Release the memory used by the abbrev table for a compilation unit. */
c906108c 13098
433df2d4
DE
13099static void
13100dwarf2_free_abbrev_table (void *ptr_to_cu)
13101{
13102 struct dwarf2_cu *cu = ptr_to_cu;
c906108c 13103
433df2d4
DE
13104 abbrev_table_free (cu->abbrev_table);
13105 /* Set this to NULL so that we SEGV if we try to read it later,
13106 and also because free_comp_unit verifies this is NULL. */
13107 cu->abbrev_table = NULL;
13108}
13109\f
72bf9492
DJ
13110/* Returns nonzero if TAG represents a type that we might generate a partial
13111 symbol for. */
13112
13113static int
13114is_type_tag_for_partial (int tag)
13115{
13116 switch (tag)
13117 {
13118#if 0
13119 /* Some types that would be reasonable to generate partial symbols for,
13120 that we don't at present. */
13121 case DW_TAG_array_type:
13122 case DW_TAG_file_type:
13123 case DW_TAG_ptr_to_member_type:
13124 case DW_TAG_set_type:
13125 case DW_TAG_string_type:
13126 case DW_TAG_subroutine_type:
13127#endif
13128 case DW_TAG_base_type:
13129 case DW_TAG_class_type:
680b30c7 13130 case DW_TAG_interface_type:
72bf9492
DJ
13131 case DW_TAG_enumeration_type:
13132 case DW_TAG_structure_type:
13133 case DW_TAG_subrange_type:
13134 case DW_TAG_typedef:
13135 case DW_TAG_union_type:
13136 return 1;
13137 default:
13138 return 0;
13139 }
13140}
13141
13142/* Load all DIEs that are interesting for partial symbols into memory. */
13143
13144static struct partial_die_info *
dee91e82
DE
13145load_partial_dies (const struct die_reader_specs *reader,
13146 gdb_byte *info_ptr, int building_psymtab)
72bf9492 13147{
dee91e82 13148 struct dwarf2_cu *cu = reader->cu;
bb5ed363 13149 struct objfile *objfile = cu->objfile;
72bf9492
DJ
13150 struct partial_die_info *part_die;
13151 struct partial_die_info *parent_die, *last_die, *first_die = NULL;
13152 struct abbrev_info *abbrev;
13153 unsigned int bytes_read;
5afb4e99 13154 unsigned int load_all = 0;
72bf9492
DJ
13155 int nesting_level = 1;
13156
13157 parent_die = NULL;
13158 last_die = NULL;
13159
7adf1e79
DE
13160 gdb_assert (cu->per_cu != NULL);
13161 if (cu->per_cu->load_all_dies)
5afb4e99
DJ
13162 load_all = 1;
13163
72bf9492
DJ
13164 cu->partial_dies
13165 = htab_create_alloc_ex (cu->header.length / 12,
13166 partial_die_hash,
13167 partial_die_eq,
13168 NULL,
13169 &cu->comp_unit_obstack,
13170 hashtab_obstack_allocate,
13171 dummy_obstack_deallocate);
13172
13173 part_die = obstack_alloc (&cu->comp_unit_obstack,
13174 sizeof (struct partial_die_info));
13175
13176 while (1)
13177 {
13178 abbrev = peek_die_abbrev (info_ptr, &bytes_read, cu);
13179
13180 /* A NULL abbrev means the end of a series of children. */
13181 if (abbrev == NULL)
13182 {
13183 if (--nesting_level == 0)
13184 {
13185 /* PART_DIE was probably the last thing allocated on the
13186 comp_unit_obstack, so we could call obstack_free
13187 here. We don't do that because the waste is small,
13188 and will be cleaned up when we're done with this
13189 compilation unit. This way, we're also more robust
13190 against other users of the comp_unit_obstack. */
13191 return first_die;
13192 }
13193 info_ptr += bytes_read;
13194 last_die = parent_die;
13195 parent_die = parent_die->die_parent;
13196 continue;
13197 }
13198
98bfdba5
PA
13199 /* Check for template arguments. We never save these; if
13200 they're seen, we just mark the parent, and go on our way. */
13201 if (parent_die != NULL
13202 && cu->language == language_cplus
13203 && (abbrev->tag == DW_TAG_template_type_param
13204 || abbrev->tag == DW_TAG_template_value_param))
13205 {
13206 parent_die->has_template_arguments = 1;
13207
13208 if (!load_all)
13209 {
13210 /* We don't need a partial DIE for the template argument. */
dee91e82 13211 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
98bfdba5
PA
13212 continue;
13213 }
13214 }
13215
0d99eb77 13216 /* We only recurse into c++ subprograms looking for template arguments.
98bfdba5
PA
13217 Skip their other children. */
13218 if (!load_all
13219 && cu->language == language_cplus
13220 && parent_die != NULL
13221 && parent_die->tag == DW_TAG_subprogram)
13222 {
dee91e82 13223 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
98bfdba5
PA
13224 continue;
13225 }
13226
5afb4e99
DJ
13227 /* Check whether this DIE is interesting enough to save. Normally
13228 we would not be interested in members here, but there may be
13229 later variables referencing them via DW_AT_specification (for
13230 static members). */
13231 if (!load_all
13232 && !is_type_tag_for_partial (abbrev->tag)
72929c62 13233 && abbrev->tag != DW_TAG_constant
72bf9492
DJ
13234 && abbrev->tag != DW_TAG_enumerator
13235 && abbrev->tag != DW_TAG_subprogram
bc30ff58 13236 && abbrev->tag != DW_TAG_lexical_block
72bf9492 13237 && abbrev->tag != DW_TAG_variable
5afb4e99 13238 && abbrev->tag != DW_TAG_namespace
f55ee35c 13239 && abbrev->tag != DW_TAG_module
95554aad
TT
13240 && abbrev->tag != DW_TAG_member
13241 && abbrev->tag != DW_TAG_imported_unit)
72bf9492
DJ
13242 {
13243 /* Otherwise we skip to the next sibling, if any. */
dee91e82 13244 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
72bf9492
DJ
13245 continue;
13246 }
13247
dee91e82
DE
13248 info_ptr = read_partial_die (reader, part_die, abbrev, bytes_read,
13249 info_ptr);
72bf9492
DJ
13250
13251 /* This two-pass algorithm for processing partial symbols has a
13252 high cost in cache pressure. Thus, handle some simple cases
13253 here which cover the majority of C partial symbols. DIEs
13254 which neither have specification tags in them, nor could have
13255 specification tags elsewhere pointing at them, can simply be
13256 processed and discarded.
13257
13258 This segment is also optional; scan_partial_symbols and
13259 add_partial_symbol will handle these DIEs if we chain
13260 them in normally. When compilers which do not emit large
13261 quantities of duplicate debug information are more common,
13262 this code can probably be removed. */
13263
13264 /* Any complete simple types at the top level (pretty much all
13265 of them, for a language without namespaces), can be processed
13266 directly. */
13267 if (parent_die == NULL
13268 && part_die->has_specification == 0
13269 && part_die->is_declaration == 0
d8228535 13270 && ((part_die->tag == DW_TAG_typedef && !part_die->has_children)
72bf9492
DJ
13271 || part_die->tag == DW_TAG_base_type
13272 || part_die->tag == DW_TAG_subrange_type))
13273 {
13274 if (building_psymtab && part_die->name != NULL)
04a679b8 13275 add_psymbol_to_list (part_die->name, strlen (part_die->name), 0,
72bf9492 13276 VAR_DOMAIN, LOC_TYPEDEF,
bb5ed363
DE
13277 &objfile->static_psymbols,
13278 0, (CORE_ADDR) 0, cu->language, objfile);
dee91e82 13279 info_ptr = locate_pdi_sibling (reader, part_die, info_ptr);
72bf9492
DJ
13280 continue;
13281 }
13282
d8228535
JK
13283 /* The exception for DW_TAG_typedef with has_children above is
13284 a workaround of GCC PR debug/47510. In the case of this complaint
13285 type_name_no_tag_or_error will error on such types later.
13286
13287 GDB skipped children of DW_TAG_typedef by the shortcut above and then
13288 it could not find the child DIEs referenced later, this is checked
13289 above. In correct DWARF DW_TAG_typedef should have no children. */
13290
13291 if (part_die->tag == DW_TAG_typedef && part_die->has_children)
13292 complaint (&symfile_complaints,
13293 _("DW_TAG_typedef has childen - GCC PR debug/47510 bug "
13294 "- DIE at 0x%x [in module %s]"),
b64f50a1 13295 part_die->offset.sect_off, objfile->name);
d8228535 13296
72bf9492
DJ
13297 /* If we're at the second level, and we're an enumerator, and
13298 our parent has no specification (meaning possibly lives in a
13299 namespace elsewhere), then we can add the partial symbol now
13300 instead of queueing it. */
13301 if (part_die->tag == DW_TAG_enumerator
13302 && parent_die != NULL
13303 && parent_die->die_parent == NULL
13304 && parent_die->tag == DW_TAG_enumeration_type
13305 && parent_die->has_specification == 0)
13306 {
13307 if (part_die->name == NULL)
3e43a32a
MS
13308 complaint (&symfile_complaints,
13309 _("malformed enumerator DIE ignored"));
72bf9492 13310 else if (building_psymtab)
04a679b8 13311 add_psymbol_to_list (part_die->name, strlen (part_die->name), 0,
72bf9492 13312 VAR_DOMAIN, LOC_CONST,
987504bb
JJ
13313 (cu->language == language_cplus
13314 || cu->language == language_java)
bb5ed363
DE
13315 ? &objfile->global_psymbols
13316 : &objfile->static_psymbols,
13317 0, (CORE_ADDR) 0, cu->language, objfile);
72bf9492 13318
dee91e82 13319 info_ptr = locate_pdi_sibling (reader, part_die, info_ptr);
72bf9492
DJ
13320 continue;
13321 }
13322
13323 /* We'll save this DIE so link it in. */
13324 part_die->die_parent = parent_die;
13325 part_die->die_sibling = NULL;
13326 part_die->die_child = NULL;
13327
13328 if (last_die && last_die == parent_die)
13329 last_die->die_child = part_die;
13330 else if (last_die)
13331 last_die->die_sibling = part_die;
13332
13333 last_die = part_die;
13334
13335 if (first_die == NULL)
13336 first_die = part_die;
13337
13338 /* Maybe add the DIE to the hash table. Not all DIEs that we
13339 find interesting need to be in the hash table, because we
13340 also have the parent/sibling/child chains; only those that we
13341 might refer to by offset later during partial symbol reading.
13342
13343 For now this means things that might have be the target of a
13344 DW_AT_specification, DW_AT_abstract_origin, or
13345 DW_AT_extension. DW_AT_extension will refer only to
13346 namespaces; DW_AT_abstract_origin refers to functions (and
13347 many things under the function DIE, but we do not recurse
13348 into function DIEs during partial symbol reading) and
13349 possibly variables as well; DW_AT_specification refers to
13350 declarations. Declarations ought to have the DW_AT_declaration
13351 flag. It happens that GCC forgets to put it in sometimes, but
13352 only for functions, not for types.
13353
13354 Adding more things than necessary to the hash table is harmless
13355 except for the performance cost. Adding too few will result in
5afb4e99
DJ
13356 wasted time in find_partial_die, when we reread the compilation
13357 unit with load_all_dies set. */
72bf9492 13358
5afb4e99 13359 if (load_all
72929c62 13360 || abbrev->tag == DW_TAG_constant
5afb4e99 13361 || abbrev->tag == DW_TAG_subprogram
72bf9492
DJ
13362 || abbrev->tag == DW_TAG_variable
13363 || abbrev->tag == DW_TAG_namespace
13364 || part_die->is_declaration)
13365 {
13366 void **slot;
13367
13368 slot = htab_find_slot_with_hash (cu->partial_dies, part_die,
b64f50a1 13369 part_die->offset.sect_off, INSERT);
72bf9492
DJ
13370 *slot = part_die;
13371 }
13372
13373 part_die = obstack_alloc (&cu->comp_unit_obstack,
13374 sizeof (struct partial_die_info));
13375
13376 /* For some DIEs we want to follow their children (if any). For C
bc30ff58 13377 we have no reason to follow the children of structures; for other
98bfdba5
PA
13378 languages we have to, so that we can get at method physnames
13379 to infer fully qualified class names, for DW_AT_specification,
13380 and for C++ template arguments. For C++, we also look one level
13381 inside functions to find template arguments (if the name of the
13382 function does not already contain the template arguments).
bc30ff58
JB
13383
13384 For Ada, we need to scan the children of subprograms and lexical
13385 blocks as well because Ada allows the definition of nested
13386 entities that could be interesting for the debugger, such as
13387 nested subprograms for instance. */
72bf9492 13388 if (last_die->has_children
5afb4e99
DJ
13389 && (load_all
13390 || last_die->tag == DW_TAG_namespace
f55ee35c 13391 || last_die->tag == DW_TAG_module
72bf9492 13392 || last_die->tag == DW_TAG_enumeration_type
98bfdba5
PA
13393 || (cu->language == language_cplus
13394 && last_die->tag == DW_TAG_subprogram
13395 && (last_die->name == NULL
13396 || strchr (last_die->name, '<') == NULL))
72bf9492
DJ
13397 || (cu->language != language_c
13398 && (last_die->tag == DW_TAG_class_type
680b30c7 13399 || last_die->tag == DW_TAG_interface_type
72bf9492 13400 || last_die->tag == DW_TAG_structure_type
bc30ff58
JB
13401 || last_die->tag == DW_TAG_union_type))
13402 || (cu->language == language_ada
13403 && (last_die->tag == DW_TAG_subprogram
13404 || last_die->tag == DW_TAG_lexical_block))))
72bf9492
DJ
13405 {
13406 nesting_level++;
13407 parent_die = last_die;
13408 continue;
13409 }
13410
13411 /* Otherwise we skip to the next sibling, if any. */
dee91e82 13412 info_ptr = locate_pdi_sibling (reader, last_die, info_ptr);
72bf9492
DJ
13413
13414 /* Back to the top, do it again. */
13415 }
13416}
13417
c906108c
SS
13418/* Read a minimal amount of information into the minimal die structure. */
13419
fe1b8b76 13420static gdb_byte *
dee91e82
DE
13421read_partial_die (const struct die_reader_specs *reader,
13422 struct partial_die_info *part_die,
13423 struct abbrev_info *abbrev, unsigned int abbrev_len,
13424 gdb_byte *info_ptr)
c906108c 13425{
dee91e82 13426 struct dwarf2_cu *cu = reader->cu;
bb5ed363 13427 struct objfile *objfile = cu->objfile;
dee91e82 13428 gdb_byte *buffer = reader->buffer;
fa238c03 13429 unsigned int i;
c906108c 13430 struct attribute attr;
c5aa993b 13431 int has_low_pc_attr = 0;
c906108c 13432 int has_high_pc_attr = 0;
91da1414 13433 int high_pc_relative = 0;
c906108c 13434
72bf9492 13435 memset (part_die, 0, sizeof (struct partial_die_info));
c906108c 13436
b64f50a1 13437 part_die->offset.sect_off = info_ptr - buffer;
72bf9492
DJ
13438
13439 info_ptr += abbrev_len;
13440
13441 if (abbrev == NULL)
13442 return info_ptr;
13443
c906108c
SS
13444 part_die->tag = abbrev->tag;
13445 part_die->has_children = abbrev->has_children;
c906108c
SS
13446
13447 for (i = 0; i < abbrev->num_attrs; ++i)
13448 {
dee91e82 13449 info_ptr = read_attribute (reader, &attr, &abbrev->attrs[i], info_ptr);
c906108c
SS
13450
13451 /* Store the data if it is of an attribute we want to keep in a
c5aa993b 13452 partial symbol table. */
c906108c
SS
13453 switch (attr.name)
13454 {
13455 case DW_AT_name:
71c25dea
TT
13456 switch (part_die->tag)
13457 {
13458 case DW_TAG_compile_unit:
95554aad 13459 case DW_TAG_partial_unit:
348e048f 13460 case DW_TAG_type_unit:
71c25dea
TT
13461 /* Compilation units have a DW_AT_name that is a filename, not
13462 a source language identifier. */
13463 case DW_TAG_enumeration_type:
13464 case DW_TAG_enumerator:
13465 /* These tags always have simple identifiers already; no need
13466 to canonicalize them. */
13467 part_die->name = DW_STRING (&attr);
13468 break;
13469 default:
13470 part_die->name
13471 = dwarf2_canonicalize_name (DW_STRING (&attr), cu,
bb5ed363 13472 &objfile->objfile_obstack);
71c25dea
TT
13473 break;
13474 }
c906108c 13475 break;
31ef98ae 13476 case DW_AT_linkage_name:
c906108c 13477 case DW_AT_MIPS_linkage_name:
31ef98ae
TT
13478 /* Note that both forms of linkage name might appear. We
13479 assume they will be the same, and we only store the last
13480 one we see. */
94af9270
KS
13481 if (cu->language == language_ada)
13482 part_die->name = DW_STRING (&attr);
abc72ce4 13483 part_die->linkage_name = DW_STRING (&attr);
c906108c
SS
13484 break;
13485 case DW_AT_low_pc:
13486 has_low_pc_attr = 1;
13487 part_die->lowpc = DW_ADDR (&attr);
13488 break;
13489 case DW_AT_high_pc:
13490 has_high_pc_attr = 1;
3019eac3
DE
13491 if (attr.form == DW_FORM_addr
13492 || attr.form == DW_FORM_GNU_addr_index)
91da1414
MW
13493 part_die->highpc = DW_ADDR (&attr);
13494 else
13495 {
13496 high_pc_relative = 1;
13497 part_die->highpc = DW_UNSND (&attr);
13498 }
c906108c
SS
13499 break;
13500 case DW_AT_location:
0963b4bd 13501 /* Support the .debug_loc offsets. */
8e19ed76
PS
13502 if (attr_form_is_block (&attr))
13503 {
95554aad 13504 part_die->d.locdesc = DW_BLOCK (&attr);
8e19ed76 13505 }
3690dd37 13506 else if (attr_form_is_section_offset (&attr))
8e19ed76 13507 {
4d3c2250 13508 dwarf2_complex_location_expr_complaint ();
8e19ed76
PS
13509 }
13510 else
13511 {
4d3c2250
KB
13512 dwarf2_invalid_attrib_class_complaint ("DW_AT_location",
13513 "partial symbol information");
8e19ed76 13514 }
c906108c 13515 break;
c906108c
SS
13516 case DW_AT_external:
13517 part_die->is_external = DW_UNSND (&attr);
13518 break;
13519 case DW_AT_declaration:
13520 part_die->is_declaration = DW_UNSND (&attr);
13521 break;
13522 case DW_AT_type:
13523 part_die->has_type = 1;
13524 break;
13525 case DW_AT_abstract_origin:
13526 case DW_AT_specification:
72bf9492
DJ
13527 case DW_AT_extension:
13528 part_die->has_specification = 1;
c764a876 13529 part_die->spec_offset = dwarf2_get_ref_die_offset (&attr);
36586728
TT
13530 part_die->spec_is_dwz = (attr.form == DW_FORM_GNU_ref_alt
13531 || cu->per_cu->is_dwz);
c906108c
SS
13532 break;
13533 case DW_AT_sibling:
13534 /* Ignore absolute siblings, they might point outside of
13535 the current compile unit. */
13536 if (attr.form == DW_FORM_ref_addr)
3e43a32a
MS
13537 complaint (&symfile_complaints,
13538 _("ignoring absolute DW_AT_sibling"));
c906108c 13539 else
b64f50a1 13540 part_die->sibling = buffer + dwarf2_get_ref_die_offset (&attr).sect_off;
c906108c 13541 break;
fa4028e9
JB
13542 case DW_AT_byte_size:
13543 part_die->has_byte_size = 1;
13544 break;
68511cec
CES
13545 case DW_AT_calling_convention:
13546 /* DWARF doesn't provide a way to identify a program's source-level
13547 entry point. DW_AT_calling_convention attributes are only meant
13548 to describe functions' calling conventions.
13549
13550 However, because it's a necessary piece of information in
13551 Fortran, and because DW_CC_program is the only piece of debugging
13552 information whose definition refers to a 'main program' at all,
13553 several compilers have begun marking Fortran main programs with
13554 DW_CC_program --- even when those functions use the standard
13555 calling conventions.
13556
13557 So until DWARF specifies a way to provide this information and
13558 compilers pick up the new representation, we'll support this
13559 practice. */
13560 if (DW_UNSND (&attr) == DW_CC_program
13561 && cu->language == language_fortran)
01f8c46d
JK
13562 {
13563 set_main_name (part_die->name);
13564
13565 /* As this DIE has a static linkage the name would be difficult
13566 to look up later. */
13567 language_of_main = language_fortran;
13568 }
68511cec 13569 break;
481860b3
GB
13570 case DW_AT_inline:
13571 if (DW_UNSND (&attr) == DW_INL_inlined
13572 || DW_UNSND (&attr) == DW_INL_declared_inlined)
13573 part_die->may_be_inlined = 1;
13574 break;
95554aad
TT
13575
13576 case DW_AT_import:
13577 if (part_die->tag == DW_TAG_imported_unit)
36586728
TT
13578 {
13579 part_die->d.offset = dwarf2_get_ref_die_offset (&attr);
13580 part_die->is_dwz = (attr.form == DW_FORM_GNU_ref_alt
13581 || cu->per_cu->is_dwz);
13582 }
95554aad
TT
13583 break;
13584
c906108c
SS
13585 default:
13586 break;
13587 }
13588 }
13589
91da1414
MW
13590 if (high_pc_relative)
13591 part_die->highpc += part_die->lowpc;
13592
9373cf26
JK
13593 if (has_low_pc_attr && has_high_pc_attr)
13594 {
13595 /* When using the GNU linker, .gnu.linkonce. sections are used to
13596 eliminate duplicate copies of functions and vtables and such.
13597 The linker will arbitrarily choose one and discard the others.
13598 The AT_*_pc values for such functions refer to local labels in
13599 these sections. If the section from that file was discarded, the
13600 labels are not in the output, so the relocs get a value of 0.
13601 If this is a discarded function, mark the pc bounds as invalid,
13602 so that GDB will ignore it. */
13603 if (part_die->lowpc == 0 && !dwarf2_per_objfile->has_section_at_zero)
13604 {
bb5ed363 13605 struct gdbarch *gdbarch = get_objfile_arch (objfile);
9373cf26
JK
13606
13607 complaint (&symfile_complaints,
13608 _("DW_AT_low_pc %s is zero "
13609 "for DIE at 0x%x [in module %s]"),
13610 paddress (gdbarch, part_die->lowpc),
b64f50a1 13611 part_die->offset.sect_off, objfile->name);
9373cf26
JK
13612 }
13613 /* dwarf2_get_pc_bounds has also the strict low < high requirement. */
13614 else if (part_die->lowpc >= part_die->highpc)
13615 {
bb5ed363 13616 struct gdbarch *gdbarch = get_objfile_arch (objfile);
9373cf26
JK
13617
13618 complaint (&symfile_complaints,
13619 _("DW_AT_low_pc %s is not < DW_AT_high_pc %s "
13620 "for DIE at 0x%x [in module %s]"),
13621 paddress (gdbarch, part_die->lowpc),
13622 paddress (gdbarch, part_die->highpc),
b64f50a1 13623 part_die->offset.sect_off, objfile->name);
9373cf26
JK
13624 }
13625 else
13626 part_die->has_pc_info = 1;
13627 }
85cbf3d3 13628
c906108c
SS
13629 return info_ptr;
13630}
13631
72bf9492
DJ
13632/* Find a cached partial DIE at OFFSET in CU. */
13633
13634static struct partial_die_info *
b64f50a1 13635find_partial_die_in_comp_unit (sect_offset offset, struct dwarf2_cu *cu)
72bf9492
DJ
13636{
13637 struct partial_die_info *lookup_die = NULL;
13638 struct partial_die_info part_die;
13639
13640 part_die.offset = offset;
b64f50a1
JK
13641 lookup_die = htab_find_with_hash (cu->partial_dies, &part_die,
13642 offset.sect_off);
72bf9492 13643
72bf9492
DJ
13644 return lookup_die;
13645}
13646
348e048f
DE
13647/* Find a partial DIE at OFFSET, which may or may not be in CU,
13648 except in the case of .debug_types DIEs which do not reference
13649 outside their CU (they do however referencing other types via
55f1336d 13650 DW_FORM_ref_sig8). */
72bf9492
DJ
13651
13652static struct partial_die_info *
36586728 13653find_partial_die (sect_offset offset, int offset_in_dwz, struct dwarf2_cu *cu)
72bf9492 13654{
bb5ed363 13655 struct objfile *objfile = cu->objfile;
5afb4e99
DJ
13656 struct dwarf2_per_cu_data *per_cu = NULL;
13657 struct partial_die_info *pd = NULL;
72bf9492 13658
36586728
TT
13659 if (offset_in_dwz == cu->per_cu->is_dwz
13660 && offset_in_cu_p (&cu->header, offset))
5afb4e99
DJ
13661 {
13662 pd = find_partial_die_in_comp_unit (offset, cu);
13663 if (pd != NULL)
13664 return pd;
0d99eb77
DE
13665 /* We missed recording what we needed.
13666 Load all dies and try again. */
13667 per_cu = cu->per_cu;
5afb4e99 13668 }
0d99eb77
DE
13669 else
13670 {
13671 /* TUs don't reference other CUs/TUs (except via type signatures). */
3019eac3 13672 if (cu->per_cu->is_debug_types)
0d99eb77
DE
13673 {
13674 error (_("Dwarf Error: Type Unit at offset 0x%lx contains"
13675 " external reference to offset 0x%lx [in module %s].\n"),
13676 (long) cu->header.offset.sect_off, (long) offset.sect_off,
13677 bfd_get_filename (objfile->obfd));
13678 }
36586728
TT
13679 per_cu = dwarf2_find_containing_comp_unit (offset, offset_in_dwz,
13680 objfile);
72bf9492 13681
0d99eb77
DE
13682 if (per_cu->cu == NULL || per_cu->cu->partial_dies == NULL)
13683 load_partial_comp_unit (per_cu);
ae038cb0 13684
0d99eb77
DE
13685 per_cu->cu->last_used = 0;
13686 pd = find_partial_die_in_comp_unit (offset, per_cu->cu);
13687 }
5afb4e99 13688
dee91e82
DE
13689 /* If we didn't find it, and not all dies have been loaded,
13690 load them all and try again. */
13691
5afb4e99
DJ
13692 if (pd == NULL && per_cu->load_all_dies == 0)
13693 {
5afb4e99 13694 per_cu->load_all_dies = 1;
fd820528
DE
13695
13696 /* This is nasty. When we reread the DIEs, somewhere up the call chain
13697 THIS_CU->cu may already be in use. So we can't just free it and
13698 replace its DIEs with the ones we read in. Instead, we leave those
13699 DIEs alone (which can still be in use, e.g. in scan_partial_symbols),
13700 and clobber THIS_CU->cu->partial_dies with the hash table for the new
13701 set. */
dee91e82 13702 load_partial_comp_unit (per_cu);
5afb4e99
DJ
13703
13704 pd = find_partial_die_in_comp_unit (offset, per_cu->cu);
13705 }
13706
13707 if (pd == NULL)
13708 internal_error (__FILE__, __LINE__,
3e43a32a
MS
13709 _("could not find partial DIE 0x%x "
13710 "in cache [from module %s]\n"),
b64f50a1 13711 offset.sect_off, bfd_get_filename (objfile->obfd));
5afb4e99 13712 return pd;
72bf9492
DJ
13713}
13714
abc72ce4
DE
13715/* See if we can figure out if the class lives in a namespace. We do
13716 this by looking for a member function; its demangled name will
13717 contain namespace info, if there is any. */
13718
13719static void
13720guess_partial_die_structure_name (struct partial_die_info *struct_pdi,
13721 struct dwarf2_cu *cu)
13722{
13723 /* NOTE: carlton/2003-10-07: Getting the info this way changes
13724 what template types look like, because the demangler
13725 frequently doesn't give the same name as the debug info. We
13726 could fix this by only using the demangled name to get the
13727 prefix (but see comment in read_structure_type). */
13728
13729 struct partial_die_info *real_pdi;
13730 struct partial_die_info *child_pdi;
13731
13732 /* If this DIE (this DIE's specification, if any) has a parent, then
13733 we should not do this. We'll prepend the parent's fully qualified
13734 name when we create the partial symbol. */
13735
13736 real_pdi = struct_pdi;
13737 while (real_pdi->has_specification)
36586728
TT
13738 real_pdi = find_partial_die (real_pdi->spec_offset,
13739 real_pdi->spec_is_dwz, cu);
abc72ce4
DE
13740
13741 if (real_pdi->die_parent != NULL)
13742 return;
13743
13744 for (child_pdi = struct_pdi->die_child;
13745 child_pdi != NULL;
13746 child_pdi = child_pdi->die_sibling)
13747 {
13748 if (child_pdi->tag == DW_TAG_subprogram
13749 && child_pdi->linkage_name != NULL)
13750 {
13751 char *actual_class_name
13752 = language_class_name_from_physname (cu->language_defn,
13753 child_pdi->linkage_name);
13754 if (actual_class_name != NULL)
13755 {
13756 struct_pdi->name
13757 = obsavestring (actual_class_name,
13758 strlen (actual_class_name),
13759 &cu->objfile->objfile_obstack);
13760 xfree (actual_class_name);
13761 }
13762 break;
13763 }
13764 }
13765}
13766
72bf9492
DJ
13767/* Adjust PART_DIE before generating a symbol for it. This function
13768 may set the is_external flag or change the DIE's name. */
13769
13770static void
13771fixup_partial_die (struct partial_die_info *part_die,
13772 struct dwarf2_cu *cu)
13773{
abc72ce4
DE
13774 /* Once we've fixed up a die, there's no point in doing so again.
13775 This also avoids a memory leak if we were to call
13776 guess_partial_die_structure_name multiple times. */
13777 if (part_die->fixup_called)
13778 return;
13779
72bf9492
DJ
13780 /* If we found a reference attribute and the DIE has no name, try
13781 to find a name in the referred to DIE. */
13782
13783 if (part_die->name == NULL && part_die->has_specification)
13784 {
13785 struct partial_die_info *spec_die;
72bf9492 13786
36586728
TT
13787 spec_die = find_partial_die (part_die->spec_offset,
13788 part_die->spec_is_dwz, cu);
72bf9492 13789
10b3939b 13790 fixup_partial_die (spec_die, cu);
72bf9492
DJ
13791
13792 if (spec_die->name)
13793 {
13794 part_die->name = spec_die->name;
13795
13796 /* Copy DW_AT_external attribute if it is set. */
13797 if (spec_die->is_external)
13798 part_die->is_external = spec_die->is_external;
13799 }
13800 }
13801
13802 /* Set default names for some unnamed DIEs. */
72bf9492
DJ
13803
13804 if (part_die->name == NULL && part_die->tag == DW_TAG_namespace)
2b1dbab0 13805 part_die->name = CP_ANONYMOUS_NAMESPACE_STR;
72bf9492 13806
abc72ce4
DE
13807 /* If there is no parent die to provide a namespace, and there are
13808 children, see if we can determine the namespace from their linkage
122d1940 13809 name. */
abc72ce4 13810 if (cu->language == language_cplus
8b70b953 13811 && !VEC_empty (dwarf2_section_info_def, dwarf2_per_objfile->types)
abc72ce4
DE
13812 && part_die->die_parent == NULL
13813 && part_die->has_children
13814 && (part_die->tag == DW_TAG_class_type
13815 || part_die->tag == DW_TAG_structure_type
13816 || part_die->tag == DW_TAG_union_type))
13817 guess_partial_die_structure_name (part_die, cu);
13818
53832f31
TT
13819 /* GCC might emit a nameless struct or union that has a linkage
13820 name. See http://gcc.gnu.org/bugzilla/show_bug.cgi?id=47510. */
13821 if (part_die->name == NULL
96408a79
SA
13822 && (part_die->tag == DW_TAG_class_type
13823 || part_die->tag == DW_TAG_interface_type
13824 || part_die->tag == DW_TAG_structure_type
13825 || part_die->tag == DW_TAG_union_type)
53832f31
TT
13826 && part_die->linkage_name != NULL)
13827 {
13828 char *demangled;
13829
13830 demangled = cplus_demangle (part_die->linkage_name, DMGL_TYPES);
13831 if (demangled)
13832 {
96408a79
SA
13833 const char *base;
13834
13835 /* Strip any leading namespaces/classes, keep only the base name.
13836 DW_AT_name for named DIEs does not contain the prefixes. */
13837 base = strrchr (demangled, ':');
13838 if (base && base > demangled && base[-1] == ':')
13839 base++;
13840 else
13841 base = demangled;
13842
13843 part_die->name = obsavestring (base, strlen (base),
53832f31
TT
13844 &cu->objfile->objfile_obstack);
13845 xfree (demangled);
13846 }
13847 }
13848
abc72ce4 13849 part_die->fixup_called = 1;
72bf9492
DJ
13850}
13851
a8329558 13852/* Read an attribute value described by an attribute form. */
c906108c 13853
fe1b8b76 13854static gdb_byte *
dee91e82
DE
13855read_attribute_value (const struct die_reader_specs *reader,
13856 struct attribute *attr, unsigned form,
13857 gdb_byte *info_ptr)
c906108c 13858{
dee91e82
DE
13859 struct dwarf2_cu *cu = reader->cu;
13860 bfd *abfd = reader->abfd;
e7c27a73 13861 struct comp_unit_head *cu_header = &cu->header;
c906108c
SS
13862 unsigned int bytes_read;
13863 struct dwarf_block *blk;
13864
a8329558
KW
13865 attr->form = form;
13866 switch (form)
c906108c 13867 {
c906108c 13868 case DW_FORM_ref_addr:
ae411497 13869 if (cu->header.version == 2)
4568ecf9 13870 DW_UNSND (attr) = read_address (abfd, info_ptr, cu, &bytes_read);
ae411497 13871 else
4568ecf9
DE
13872 DW_UNSND (attr) = read_offset (abfd, info_ptr,
13873 &cu->header, &bytes_read);
ae411497
TT
13874 info_ptr += bytes_read;
13875 break;
36586728
TT
13876 case DW_FORM_GNU_ref_alt:
13877 DW_UNSND (attr) = read_offset (abfd, info_ptr, &cu->header, &bytes_read);
13878 info_ptr += bytes_read;
13879 break;
ae411497 13880 case DW_FORM_addr:
e7c27a73 13881 DW_ADDR (attr) = read_address (abfd, info_ptr, cu, &bytes_read);
107d2387 13882 info_ptr += bytes_read;
c906108c
SS
13883 break;
13884 case DW_FORM_block2:
7b5a2f43 13885 blk = dwarf_alloc_block (cu);
c906108c
SS
13886 blk->size = read_2_bytes (abfd, info_ptr);
13887 info_ptr += 2;
13888 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
13889 info_ptr += blk->size;
13890 DW_BLOCK (attr) = blk;
13891 break;
13892 case DW_FORM_block4:
7b5a2f43 13893 blk = dwarf_alloc_block (cu);
c906108c
SS
13894 blk->size = read_4_bytes (abfd, info_ptr);
13895 info_ptr += 4;
13896 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
13897 info_ptr += blk->size;
13898 DW_BLOCK (attr) = blk;
13899 break;
13900 case DW_FORM_data2:
13901 DW_UNSND (attr) = read_2_bytes (abfd, info_ptr);
13902 info_ptr += 2;
13903 break;
13904 case DW_FORM_data4:
13905 DW_UNSND (attr) = read_4_bytes (abfd, info_ptr);
13906 info_ptr += 4;
13907 break;
13908 case DW_FORM_data8:
13909 DW_UNSND (attr) = read_8_bytes (abfd, info_ptr);
13910 info_ptr += 8;
13911 break;
2dc7f7b3
TT
13912 case DW_FORM_sec_offset:
13913 DW_UNSND (attr) = read_offset (abfd, info_ptr, &cu->header, &bytes_read);
13914 info_ptr += bytes_read;
13915 break;
c906108c 13916 case DW_FORM_string:
9b1c24c8 13917 DW_STRING (attr) = read_direct_string (abfd, info_ptr, &bytes_read);
8285870a 13918 DW_STRING_IS_CANONICAL (attr) = 0;
c906108c
SS
13919 info_ptr += bytes_read;
13920 break;
4bdf3d34 13921 case DW_FORM_strp:
36586728
TT
13922 if (!cu->per_cu->is_dwz)
13923 {
13924 DW_STRING (attr) = read_indirect_string (abfd, info_ptr, cu_header,
13925 &bytes_read);
13926 DW_STRING_IS_CANONICAL (attr) = 0;
13927 info_ptr += bytes_read;
13928 break;
13929 }
13930 /* FALLTHROUGH */
13931 case DW_FORM_GNU_strp_alt:
13932 {
13933 struct dwz_file *dwz = dwarf2_get_dwz_file ();
13934 LONGEST str_offset = read_offset (abfd, info_ptr, cu_header,
13935 &bytes_read);
13936
13937 DW_STRING (attr) = read_indirect_string_from_dwz (dwz, str_offset);
13938 DW_STRING_IS_CANONICAL (attr) = 0;
13939 info_ptr += bytes_read;
13940 }
4bdf3d34 13941 break;
2dc7f7b3 13942 case DW_FORM_exprloc:
c906108c 13943 case DW_FORM_block:
7b5a2f43 13944 blk = dwarf_alloc_block (cu);
c906108c
SS
13945 blk->size = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
13946 info_ptr += bytes_read;
13947 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
13948 info_ptr += blk->size;
13949 DW_BLOCK (attr) = blk;
13950 break;
13951 case DW_FORM_block1:
7b5a2f43 13952 blk = dwarf_alloc_block (cu);
c906108c
SS
13953 blk->size = read_1_byte (abfd, info_ptr);
13954 info_ptr += 1;
13955 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
13956 info_ptr += blk->size;
13957 DW_BLOCK (attr) = blk;
13958 break;
13959 case DW_FORM_data1:
13960 DW_UNSND (attr) = read_1_byte (abfd, info_ptr);
13961 info_ptr += 1;
13962 break;
13963 case DW_FORM_flag:
13964 DW_UNSND (attr) = read_1_byte (abfd, info_ptr);
13965 info_ptr += 1;
13966 break;
2dc7f7b3
TT
13967 case DW_FORM_flag_present:
13968 DW_UNSND (attr) = 1;
13969 break;
c906108c
SS
13970 case DW_FORM_sdata:
13971 DW_SND (attr) = read_signed_leb128 (abfd, info_ptr, &bytes_read);
13972 info_ptr += bytes_read;
13973 break;
13974 case DW_FORM_udata:
13975 DW_UNSND (attr) = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
13976 info_ptr += bytes_read;
13977 break;
13978 case DW_FORM_ref1:
4568ecf9
DE
13979 DW_UNSND (attr) = (cu->header.offset.sect_off
13980 + read_1_byte (abfd, info_ptr));
c906108c
SS
13981 info_ptr += 1;
13982 break;
13983 case DW_FORM_ref2:
4568ecf9
DE
13984 DW_UNSND (attr) = (cu->header.offset.sect_off
13985 + read_2_bytes (abfd, info_ptr));
c906108c
SS
13986 info_ptr += 2;
13987 break;
13988 case DW_FORM_ref4:
4568ecf9
DE
13989 DW_UNSND (attr) = (cu->header.offset.sect_off
13990 + read_4_bytes (abfd, info_ptr));
c906108c
SS
13991 info_ptr += 4;
13992 break;
613e1657 13993 case DW_FORM_ref8:
4568ecf9
DE
13994 DW_UNSND (attr) = (cu->header.offset.sect_off
13995 + read_8_bytes (abfd, info_ptr));
613e1657
KB
13996 info_ptr += 8;
13997 break;
55f1336d 13998 case DW_FORM_ref_sig8:
348e048f
DE
13999 /* Convert the signature to something we can record in DW_UNSND
14000 for later lookup.
14001 NOTE: This is NULL if the type wasn't found. */
14002 DW_SIGNATURED_TYPE (attr) =
e319fa28 14003 lookup_signatured_type (read_8_bytes (abfd, info_ptr));
348e048f
DE
14004 info_ptr += 8;
14005 break;
c906108c 14006 case DW_FORM_ref_udata:
4568ecf9
DE
14007 DW_UNSND (attr) = (cu->header.offset.sect_off
14008 + read_unsigned_leb128 (abfd, info_ptr, &bytes_read));
c906108c
SS
14009 info_ptr += bytes_read;
14010 break;
c906108c 14011 case DW_FORM_indirect:
a8329558
KW
14012 form = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
14013 info_ptr += bytes_read;
dee91e82 14014 info_ptr = read_attribute_value (reader, attr, form, info_ptr);
a8329558 14015 break;
3019eac3
DE
14016 case DW_FORM_GNU_addr_index:
14017 if (reader->dwo_file == NULL)
14018 {
14019 /* For now flag a hard error.
14020 Later we can turn this into a complaint. */
14021 error (_("Dwarf Error: %s found in non-DWO CU [in module %s]"),
14022 dwarf_form_name (form),
14023 bfd_get_filename (abfd));
14024 }
14025 DW_ADDR (attr) = read_addr_index_from_leb128 (cu, info_ptr, &bytes_read);
14026 info_ptr += bytes_read;
14027 break;
14028 case DW_FORM_GNU_str_index:
14029 if (reader->dwo_file == NULL)
14030 {
14031 /* For now flag a hard error.
14032 Later we can turn this into a complaint if warranted. */
14033 error (_("Dwarf Error: %s found in non-DWO CU [in module %s]"),
14034 dwarf_form_name (form),
14035 bfd_get_filename (abfd));
14036 }
14037 {
14038 ULONGEST str_index =
14039 read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
14040
14041 DW_STRING (attr) = read_str_index (reader, cu, str_index);
14042 DW_STRING_IS_CANONICAL (attr) = 0;
14043 info_ptr += bytes_read;
14044 }
14045 break;
c906108c 14046 default:
8a3fe4f8 14047 error (_("Dwarf Error: Cannot handle %s in DWARF reader [in module %s]"),
659b0389
ML
14048 dwarf_form_name (form),
14049 bfd_get_filename (abfd));
c906108c 14050 }
28e94949 14051
36586728
TT
14052 /* Super hack. */
14053 if (cu->per_cu->is_dwz && is_ref_attr (attr))
14054 attr->form = DW_FORM_GNU_ref_alt;
14055
28e94949
JB
14056 /* We have seen instances where the compiler tried to emit a byte
14057 size attribute of -1 which ended up being encoded as an unsigned
14058 0xffffffff. Although 0xffffffff is technically a valid size value,
14059 an object of this size seems pretty unlikely so we can relatively
14060 safely treat these cases as if the size attribute was invalid and
14061 treat them as zero by default. */
14062 if (attr->name == DW_AT_byte_size
14063 && form == DW_FORM_data4
14064 && DW_UNSND (attr) >= 0xffffffff)
01c66ae6
JB
14065 {
14066 complaint
14067 (&symfile_complaints,
43bbcdc2
PH
14068 _("Suspicious DW_AT_byte_size value treated as zero instead of %s"),
14069 hex_string (DW_UNSND (attr)));
01c66ae6
JB
14070 DW_UNSND (attr) = 0;
14071 }
28e94949 14072
c906108c
SS
14073 return info_ptr;
14074}
14075
a8329558
KW
14076/* Read an attribute described by an abbreviated attribute. */
14077
fe1b8b76 14078static gdb_byte *
dee91e82
DE
14079read_attribute (const struct die_reader_specs *reader,
14080 struct attribute *attr, struct attr_abbrev *abbrev,
14081 gdb_byte *info_ptr)
a8329558
KW
14082{
14083 attr->name = abbrev->name;
dee91e82 14084 return read_attribute_value (reader, attr, abbrev->form, info_ptr);
a8329558
KW
14085}
14086
0963b4bd 14087/* Read dwarf information from a buffer. */
c906108c
SS
14088
14089static unsigned int
a1855c1d 14090read_1_byte (bfd *abfd, const gdb_byte *buf)
c906108c 14091{
fe1b8b76 14092 return bfd_get_8 (abfd, buf);
c906108c
SS
14093}
14094
14095static int
a1855c1d 14096read_1_signed_byte (bfd *abfd, const gdb_byte *buf)
c906108c 14097{
fe1b8b76 14098 return bfd_get_signed_8 (abfd, buf);
c906108c
SS
14099}
14100
14101static unsigned int
a1855c1d 14102read_2_bytes (bfd *abfd, const gdb_byte *buf)
c906108c 14103{
fe1b8b76 14104 return bfd_get_16 (abfd, buf);
c906108c
SS
14105}
14106
21ae7a4d 14107static int
a1855c1d 14108read_2_signed_bytes (bfd *abfd, const gdb_byte *buf)
21ae7a4d
JK
14109{
14110 return bfd_get_signed_16 (abfd, buf);
14111}
14112
c906108c 14113static unsigned int
a1855c1d 14114read_4_bytes (bfd *abfd, const gdb_byte *buf)
c906108c 14115{
fe1b8b76 14116 return bfd_get_32 (abfd, buf);
c906108c
SS
14117}
14118
21ae7a4d 14119static int
a1855c1d 14120read_4_signed_bytes (bfd *abfd, const gdb_byte *buf)
21ae7a4d
JK
14121{
14122 return bfd_get_signed_32 (abfd, buf);
14123}
14124
93311388 14125static ULONGEST
a1855c1d 14126read_8_bytes (bfd *abfd, const gdb_byte *buf)
c906108c 14127{
fe1b8b76 14128 return bfd_get_64 (abfd, buf);
c906108c
SS
14129}
14130
14131static CORE_ADDR
fe1b8b76 14132read_address (bfd *abfd, gdb_byte *buf, struct dwarf2_cu *cu,
891d2f0b 14133 unsigned int *bytes_read)
c906108c 14134{
e7c27a73 14135 struct comp_unit_head *cu_header = &cu->header;
c906108c
SS
14136 CORE_ADDR retval = 0;
14137
107d2387 14138 if (cu_header->signed_addr_p)
c906108c 14139 {
107d2387
AC
14140 switch (cu_header->addr_size)
14141 {
14142 case 2:
fe1b8b76 14143 retval = bfd_get_signed_16 (abfd, buf);
107d2387
AC
14144 break;
14145 case 4:
fe1b8b76 14146 retval = bfd_get_signed_32 (abfd, buf);
107d2387
AC
14147 break;
14148 case 8:
fe1b8b76 14149 retval = bfd_get_signed_64 (abfd, buf);
107d2387
AC
14150 break;
14151 default:
8e65ff28 14152 internal_error (__FILE__, __LINE__,
e2e0b3e5 14153 _("read_address: bad switch, signed [in module %s]"),
659b0389 14154 bfd_get_filename (abfd));
107d2387
AC
14155 }
14156 }
14157 else
14158 {
14159 switch (cu_header->addr_size)
14160 {
14161 case 2:
fe1b8b76 14162 retval = bfd_get_16 (abfd, buf);
107d2387
AC
14163 break;
14164 case 4:
fe1b8b76 14165 retval = bfd_get_32 (abfd, buf);
107d2387
AC
14166 break;
14167 case 8:
fe1b8b76 14168 retval = bfd_get_64 (abfd, buf);
107d2387
AC
14169 break;
14170 default:
8e65ff28 14171 internal_error (__FILE__, __LINE__,
a73c6dcd
MS
14172 _("read_address: bad switch, "
14173 "unsigned [in module %s]"),
659b0389 14174 bfd_get_filename (abfd));
107d2387 14175 }
c906108c 14176 }
64367e0a 14177
107d2387
AC
14178 *bytes_read = cu_header->addr_size;
14179 return retval;
c906108c
SS
14180}
14181
f7ef9339 14182/* Read the initial length from a section. The (draft) DWARF 3
613e1657
KB
14183 specification allows the initial length to take up either 4 bytes
14184 or 12 bytes. If the first 4 bytes are 0xffffffff, then the next 8
14185 bytes describe the length and all offsets will be 8 bytes in length
14186 instead of 4.
14187
f7ef9339
KB
14188 An older, non-standard 64-bit format is also handled by this
14189 function. The older format in question stores the initial length
14190 as an 8-byte quantity without an escape value. Lengths greater
14191 than 2^32 aren't very common which means that the initial 4 bytes
14192 is almost always zero. Since a length value of zero doesn't make
14193 sense for the 32-bit format, this initial zero can be considered to
14194 be an escape value which indicates the presence of the older 64-bit
14195 format. As written, the code can't detect (old format) lengths
917c78fc
MK
14196 greater than 4GB. If it becomes necessary to handle lengths
14197 somewhat larger than 4GB, we could allow other small values (such
14198 as the non-sensical values of 1, 2, and 3) to also be used as
14199 escape values indicating the presence of the old format.
f7ef9339 14200
917c78fc
MK
14201 The value returned via bytes_read should be used to increment the
14202 relevant pointer after calling read_initial_length().
c764a876 14203
613e1657
KB
14204 [ Note: read_initial_length() and read_offset() are based on the
14205 document entitled "DWARF Debugging Information Format", revision
f7ef9339 14206 3, draft 8, dated November 19, 2001. This document was obtained
613e1657
KB
14207 from:
14208
f7ef9339 14209 http://reality.sgiweb.org/davea/dwarf3-draft8-011125.pdf
6e70227d 14210
613e1657
KB
14211 This document is only a draft and is subject to change. (So beware.)
14212
f7ef9339 14213 Details regarding the older, non-standard 64-bit format were
917c78fc
MK
14214 determined empirically by examining 64-bit ELF files produced by
14215 the SGI toolchain on an IRIX 6.5 machine.
f7ef9339
KB
14216
14217 - Kevin, July 16, 2002
613e1657
KB
14218 ] */
14219
14220static LONGEST
c764a876 14221read_initial_length (bfd *abfd, gdb_byte *buf, unsigned int *bytes_read)
613e1657 14222{
fe1b8b76 14223 LONGEST length = bfd_get_32 (abfd, buf);
613e1657 14224
dd373385 14225 if (length == 0xffffffff)
613e1657 14226 {
fe1b8b76 14227 length = bfd_get_64 (abfd, buf + 4);
613e1657 14228 *bytes_read = 12;
613e1657 14229 }
dd373385 14230 else if (length == 0)
f7ef9339 14231 {
dd373385 14232 /* Handle the (non-standard) 64-bit DWARF2 format used by IRIX. */
fe1b8b76 14233 length = bfd_get_64 (abfd, buf);
f7ef9339 14234 *bytes_read = 8;
f7ef9339 14235 }
613e1657
KB
14236 else
14237 {
14238 *bytes_read = 4;
613e1657
KB
14239 }
14240
c764a876
DE
14241 return length;
14242}
dd373385 14243
c764a876
DE
14244/* Cover function for read_initial_length.
14245 Returns the length of the object at BUF, and stores the size of the
14246 initial length in *BYTES_READ and stores the size that offsets will be in
14247 *OFFSET_SIZE.
14248 If the initial length size is not equivalent to that specified in
14249 CU_HEADER then issue a complaint.
14250 This is useful when reading non-comp-unit headers. */
dd373385 14251
c764a876
DE
14252static LONGEST
14253read_checked_initial_length_and_offset (bfd *abfd, gdb_byte *buf,
14254 const struct comp_unit_head *cu_header,
14255 unsigned int *bytes_read,
14256 unsigned int *offset_size)
14257{
14258 LONGEST length = read_initial_length (abfd, buf, bytes_read);
14259
14260 gdb_assert (cu_header->initial_length_size == 4
14261 || cu_header->initial_length_size == 8
14262 || cu_header->initial_length_size == 12);
14263
14264 if (cu_header->initial_length_size != *bytes_read)
14265 complaint (&symfile_complaints,
14266 _("intermixed 32-bit and 64-bit DWARF sections"));
dd373385 14267
c764a876 14268 *offset_size = (*bytes_read == 4) ? 4 : 8;
dd373385 14269 return length;
613e1657
KB
14270}
14271
14272/* Read an offset from the data stream. The size of the offset is
917c78fc 14273 given by cu_header->offset_size. */
613e1657
KB
14274
14275static LONGEST
fe1b8b76 14276read_offset (bfd *abfd, gdb_byte *buf, const struct comp_unit_head *cu_header,
891d2f0b 14277 unsigned int *bytes_read)
c764a876
DE
14278{
14279 LONGEST offset = read_offset_1 (abfd, buf, cu_header->offset_size);
9a619af0 14280
c764a876
DE
14281 *bytes_read = cu_header->offset_size;
14282 return offset;
14283}
14284
14285/* Read an offset from the data stream. */
14286
14287static LONGEST
14288read_offset_1 (bfd *abfd, gdb_byte *buf, unsigned int offset_size)
613e1657
KB
14289{
14290 LONGEST retval = 0;
14291
c764a876 14292 switch (offset_size)
613e1657
KB
14293 {
14294 case 4:
fe1b8b76 14295 retval = bfd_get_32 (abfd, buf);
613e1657
KB
14296 break;
14297 case 8:
fe1b8b76 14298 retval = bfd_get_64 (abfd, buf);
613e1657
KB
14299 break;
14300 default:
8e65ff28 14301 internal_error (__FILE__, __LINE__,
c764a876 14302 _("read_offset_1: bad switch [in module %s]"),
659b0389 14303 bfd_get_filename (abfd));
613e1657
KB
14304 }
14305
917c78fc 14306 return retval;
613e1657
KB
14307}
14308
fe1b8b76
JB
14309static gdb_byte *
14310read_n_bytes (bfd *abfd, gdb_byte *buf, unsigned int size)
c906108c
SS
14311{
14312 /* If the size of a host char is 8 bits, we can return a pointer
14313 to the buffer, otherwise we have to copy the data to a buffer
14314 allocated on the temporary obstack. */
4bdf3d34 14315 gdb_assert (HOST_CHAR_BIT == 8);
c906108c 14316 return buf;
c906108c
SS
14317}
14318
14319static char *
9b1c24c8 14320read_direct_string (bfd *abfd, gdb_byte *buf, unsigned int *bytes_read_ptr)
c906108c
SS
14321{
14322 /* If the size of a host char is 8 bits, we can return a pointer
14323 to the string, otherwise we have to copy the string to a buffer
14324 allocated on the temporary obstack. */
4bdf3d34 14325 gdb_assert (HOST_CHAR_BIT == 8);
c906108c
SS
14326 if (*buf == '\0')
14327 {
14328 *bytes_read_ptr = 1;
14329 return NULL;
14330 }
fe1b8b76
JB
14331 *bytes_read_ptr = strlen ((char *) buf) + 1;
14332 return (char *) buf;
4bdf3d34
JJ
14333}
14334
14335static char *
cf2c3c16 14336read_indirect_string_at_offset (bfd *abfd, LONGEST str_offset)
4bdf3d34 14337{
be391dca 14338 dwarf2_read_section (dwarf2_per_objfile->objfile, &dwarf2_per_objfile->str);
dce234bc 14339 if (dwarf2_per_objfile->str.buffer == NULL)
cf2c3c16
TT
14340 error (_("DW_FORM_strp used without .debug_str section [in module %s]"),
14341 bfd_get_filename (abfd));
dce234bc 14342 if (str_offset >= dwarf2_per_objfile->str.size)
cf2c3c16
TT
14343 error (_("DW_FORM_strp pointing outside of "
14344 ".debug_str section [in module %s]"),
14345 bfd_get_filename (abfd));
4bdf3d34 14346 gdb_assert (HOST_CHAR_BIT == 8);
dce234bc 14347 if (dwarf2_per_objfile->str.buffer[str_offset] == '\0')
4bdf3d34 14348 return NULL;
dce234bc 14349 return (char *) (dwarf2_per_objfile->str.buffer + str_offset);
c906108c
SS
14350}
14351
36586728
TT
14352/* Read a string at offset STR_OFFSET in the .debug_str section from
14353 the .dwz file DWZ. Throw an error if the offset is too large. If
14354 the string consists of a single NUL byte, return NULL; otherwise
14355 return a pointer to the string. */
14356
14357static char *
14358read_indirect_string_from_dwz (struct dwz_file *dwz, LONGEST str_offset)
14359{
14360 dwarf2_read_section (dwarf2_per_objfile->objfile, &dwz->str);
14361
14362 if (dwz->str.buffer == NULL)
14363 error (_("DW_FORM_GNU_strp_alt used without .debug_str "
14364 "section [in module %s]"),
14365 bfd_get_filename (dwz->dwz_bfd));
14366 if (str_offset >= dwz->str.size)
14367 error (_("DW_FORM_GNU_strp_alt pointing outside of "
14368 ".debug_str section [in module %s]"),
14369 bfd_get_filename (dwz->dwz_bfd));
14370 gdb_assert (HOST_CHAR_BIT == 8);
14371 if (dwz->str.buffer[str_offset] == '\0')
14372 return NULL;
14373 return (char *) (dwz->str.buffer + str_offset);
14374}
14375
cf2c3c16
TT
14376static char *
14377read_indirect_string (bfd *abfd, gdb_byte *buf,
14378 const struct comp_unit_head *cu_header,
14379 unsigned int *bytes_read_ptr)
14380{
14381 LONGEST str_offset = read_offset (abfd, buf, cu_header, bytes_read_ptr);
14382
14383 return read_indirect_string_at_offset (abfd, str_offset);
14384}
14385
12df843f 14386static ULONGEST
fe1b8b76 14387read_unsigned_leb128 (bfd *abfd, gdb_byte *buf, unsigned int *bytes_read_ptr)
c906108c 14388{
12df843f 14389 ULONGEST result;
ce5d95e1 14390 unsigned int num_read;
c906108c
SS
14391 int i, shift;
14392 unsigned char byte;
14393
14394 result = 0;
14395 shift = 0;
14396 num_read = 0;
14397 i = 0;
14398 while (1)
14399 {
fe1b8b76 14400 byte = bfd_get_8 (abfd, buf);
c906108c
SS
14401 buf++;
14402 num_read++;
12df843f 14403 result |= ((ULONGEST) (byte & 127) << shift);
c906108c
SS
14404 if ((byte & 128) == 0)
14405 {
14406 break;
14407 }
14408 shift += 7;
14409 }
14410 *bytes_read_ptr = num_read;
14411 return result;
14412}
14413
12df843f 14414static LONGEST
fe1b8b76 14415read_signed_leb128 (bfd *abfd, gdb_byte *buf, unsigned int *bytes_read_ptr)
c906108c 14416{
12df843f 14417 LONGEST result;
77e0b926 14418 int i, shift, num_read;
c906108c
SS
14419 unsigned char byte;
14420
14421 result = 0;
14422 shift = 0;
c906108c
SS
14423 num_read = 0;
14424 i = 0;
14425 while (1)
14426 {
fe1b8b76 14427 byte = bfd_get_8 (abfd, buf);
c906108c
SS
14428 buf++;
14429 num_read++;
12df843f 14430 result |= ((LONGEST) (byte & 127) << shift);
c906108c
SS
14431 shift += 7;
14432 if ((byte & 128) == 0)
14433 {
14434 break;
14435 }
14436 }
77e0b926 14437 if ((shift < 8 * sizeof (result)) && (byte & 0x40))
12df843f 14438 result |= -(((LONGEST) 1) << shift);
c906108c
SS
14439 *bytes_read_ptr = num_read;
14440 return result;
14441}
14442
3019eac3
DE
14443/* Given index ADDR_INDEX in .debug_addr, fetch the value.
14444 ADDR_BASE is the DW_AT_GNU_addr_base attribute or zero.
14445 ADDR_SIZE is the size of addresses from the CU header. */
14446
14447static CORE_ADDR
14448read_addr_index_1 (unsigned int addr_index, ULONGEST addr_base, int addr_size)
14449{
14450 struct objfile *objfile = dwarf2_per_objfile->objfile;
14451 bfd *abfd = objfile->obfd;
14452 const gdb_byte *info_ptr;
14453
14454 dwarf2_read_section (objfile, &dwarf2_per_objfile->addr);
14455 if (dwarf2_per_objfile->addr.buffer == NULL)
14456 error (_("DW_FORM_addr_index used without .debug_addr section [in module %s]"),
14457 objfile->name);
14458 if (addr_base + addr_index * addr_size >= dwarf2_per_objfile->addr.size)
14459 error (_("DW_FORM_addr_index pointing outside of "
14460 ".debug_addr section [in module %s]"),
14461 objfile->name);
14462 info_ptr = (dwarf2_per_objfile->addr.buffer
14463 + addr_base + addr_index * addr_size);
14464 if (addr_size == 4)
14465 return bfd_get_32 (abfd, info_ptr);
14466 else
14467 return bfd_get_64 (abfd, info_ptr);
14468}
14469
14470/* Given index ADDR_INDEX in .debug_addr, fetch the value. */
14471
14472static CORE_ADDR
14473read_addr_index (struct dwarf2_cu *cu, unsigned int addr_index)
14474{
14475 return read_addr_index_1 (addr_index, cu->addr_base, cu->header.addr_size);
14476}
14477
14478/* Given a pointer to an leb128 value, fetch the value from .debug_addr. */
14479
14480static CORE_ADDR
14481read_addr_index_from_leb128 (struct dwarf2_cu *cu, gdb_byte *info_ptr,
14482 unsigned int *bytes_read)
14483{
14484 bfd *abfd = cu->objfile->obfd;
14485 unsigned int addr_index = read_unsigned_leb128 (abfd, info_ptr, bytes_read);
14486
14487 return read_addr_index (cu, addr_index);
14488}
14489
14490/* Data structure to pass results from dwarf2_read_addr_index_reader
14491 back to dwarf2_read_addr_index. */
14492
14493struct dwarf2_read_addr_index_data
14494{
14495 ULONGEST addr_base;
14496 int addr_size;
14497};
14498
14499/* die_reader_func for dwarf2_read_addr_index. */
14500
14501static void
14502dwarf2_read_addr_index_reader (const struct die_reader_specs *reader,
14503 gdb_byte *info_ptr,
14504 struct die_info *comp_unit_die,
14505 int has_children,
14506 void *data)
14507{
14508 struct dwarf2_cu *cu = reader->cu;
14509 struct dwarf2_read_addr_index_data *aidata =
14510 (struct dwarf2_read_addr_index_data *) data;
14511
14512 aidata->addr_base = cu->addr_base;
14513 aidata->addr_size = cu->header.addr_size;
14514}
14515
14516/* Given an index in .debug_addr, fetch the value.
14517 NOTE: This can be called during dwarf expression evaluation,
14518 long after the debug information has been read, and thus per_cu->cu
14519 may no longer exist. */
14520
14521CORE_ADDR
14522dwarf2_read_addr_index (struct dwarf2_per_cu_data *per_cu,
14523 unsigned int addr_index)
14524{
14525 struct objfile *objfile = per_cu->objfile;
14526 struct dwarf2_cu *cu = per_cu->cu;
14527 ULONGEST addr_base;
14528 int addr_size;
14529
14530 /* This is intended to be called from outside this file. */
14531 dw2_setup (objfile);
14532
14533 /* We need addr_base and addr_size.
14534 If we don't have PER_CU->cu, we have to get it.
14535 Nasty, but the alternative is storing the needed info in PER_CU,
14536 which at this point doesn't seem justified: it's not clear how frequently
14537 it would get used and it would increase the size of every PER_CU.
14538 Entry points like dwarf2_per_cu_addr_size do a similar thing
14539 so we're not in uncharted territory here.
14540 Alas we need to be a bit more complicated as addr_base is contained
14541 in the DIE.
14542
14543 We don't need to read the entire CU(/TU).
14544 We just need the header and top level die.
a1b64ce1 14545
3019eac3 14546 IWBN to use the aging mechanism to let us lazily later discard the CU.
a1b64ce1 14547 For now we skip this optimization. */
3019eac3
DE
14548
14549 if (cu != NULL)
14550 {
14551 addr_base = cu->addr_base;
14552 addr_size = cu->header.addr_size;
14553 }
14554 else
14555 {
14556 struct dwarf2_read_addr_index_data aidata;
14557
a1b64ce1
DE
14558 /* Note: We can't use init_cutu_and_read_dies_simple here,
14559 we need addr_base. */
14560 init_cutu_and_read_dies (per_cu, NULL, 0, 0,
14561 dwarf2_read_addr_index_reader, &aidata);
3019eac3
DE
14562 addr_base = aidata.addr_base;
14563 addr_size = aidata.addr_size;
14564 }
14565
14566 return read_addr_index_1 (addr_index, addr_base, addr_size);
14567}
14568
14569/* Given a DW_AT_str_index, fetch the string. */
14570
14571static char *
14572read_str_index (const struct die_reader_specs *reader,
14573 struct dwarf2_cu *cu, ULONGEST str_index)
14574{
14575 struct objfile *objfile = dwarf2_per_objfile->objfile;
14576 const char *dwo_name = objfile->name;
14577 bfd *abfd = objfile->obfd;
14578 struct dwo_sections *sections = &reader->dwo_file->sections;
14579 gdb_byte *info_ptr;
14580 ULONGEST str_offset;
14581
14582 dwarf2_read_section (objfile, &sections->str);
14583 dwarf2_read_section (objfile, &sections->str_offsets);
14584 if (sections->str.buffer == NULL)
14585 error (_("DW_FORM_str_index used without .debug_str.dwo section"
14586 " in CU at offset 0x%lx [in module %s]"),
14587 (long) cu->header.offset.sect_off, dwo_name);
14588 if (sections->str_offsets.buffer == NULL)
14589 error (_("DW_FORM_str_index used without .debug_str_offsets.dwo section"
14590 " in CU at offset 0x%lx [in module %s]"),
14591 (long) cu->header.offset.sect_off, dwo_name);
14592 if (str_index * cu->header.offset_size >= sections->str_offsets.size)
14593 error (_("DW_FORM_str_index pointing outside of .debug_str_offsets.dwo"
14594 " section in CU at offset 0x%lx [in module %s]"),
14595 (long) cu->header.offset.sect_off, dwo_name);
14596 info_ptr = (sections->str_offsets.buffer
14597 + str_index * cu->header.offset_size);
14598 if (cu->header.offset_size == 4)
14599 str_offset = bfd_get_32 (abfd, info_ptr);
14600 else
14601 str_offset = bfd_get_64 (abfd, info_ptr);
14602 if (str_offset >= sections->str.size)
14603 error (_("Offset from DW_FORM_str_index pointing outside of"
14604 " .debug_str.dwo section in CU at offset 0x%lx [in module %s]"),
14605 (long) cu->header.offset.sect_off, dwo_name);
14606 return (char *) (sections->str.buffer + str_offset);
14607}
14608
3019eac3
DE
14609/* Return the length of an LEB128 number in BUF. */
14610
14611static int
14612leb128_size (const gdb_byte *buf)
14613{
14614 const gdb_byte *begin = buf;
14615 gdb_byte byte;
14616
14617 while (1)
14618 {
14619 byte = *buf++;
14620 if ((byte & 128) == 0)
14621 return buf - begin;
14622 }
14623}
14624
c906108c 14625static void
e142c38c 14626set_cu_language (unsigned int lang, struct dwarf2_cu *cu)
c906108c
SS
14627{
14628 switch (lang)
14629 {
14630 case DW_LANG_C89:
76bee0cc 14631 case DW_LANG_C99:
c906108c 14632 case DW_LANG_C:
e142c38c 14633 cu->language = language_c;
c906108c
SS
14634 break;
14635 case DW_LANG_C_plus_plus:
e142c38c 14636 cu->language = language_cplus;
c906108c 14637 break;
6aecb9c2
JB
14638 case DW_LANG_D:
14639 cu->language = language_d;
14640 break;
c906108c
SS
14641 case DW_LANG_Fortran77:
14642 case DW_LANG_Fortran90:
b21b22e0 14643 case DW_LANG_Fortran95:
e142c38c 14644 cu->language = language_fortran;
c906108c 14645 break;
a766d390
DE
14646 case DW_LANG_Go:
14647 cu->language = language_go;
14648 break;
c906108c 14649 case DW_LANG_Mips_Assembler:
e142c38c 14650 cu->language = language_asm;
c906108c 14651 break;
bebd888e 14652 case DW_LANG_Java:
e142c38c 14653 cu->language = language_java;
bebd888e 14654 break;
c906108c 14655 case DW_LANG_Ada83:
8aaf0b47 14656 case DW_LANG_Ada95:
bc5f45f8
JB
14657 cu->language = language_ada;
14658 break;
72019c9c
GM
14659 case DW_LANG_Modula2:
14660 cu->language = language_m2;
14661 break;
fe8e67fd
PM
14662 case DW_LANG_Pascal83:
14663 cu->language = language_pascal;
14664 break;
22566fbd
DJ
14665 case DW_LANG_ObjC:
14666 cu->language = language_objc;
14667 break;
c906108c
SS
14668 case DW_LANG_Cobol74:
14669 case DW_LANG_Cobol85:
c906108c 14670 default:
e142c38c 14671 cu->language = language_minimal;
c906108c
SS
14672 break;
14673 }
e142c38c 14674 cu->language_defn = language_def (cu->language);
c906108c
SS
14675}
14676
14677/* Return the named attribute or NULL if not there. */
14678
14679static struct attribute *
e142c38c 14680dwarf2_attr (struct die_info *die, unsigned int name, struct dwarf2_cu *cu)
c906108c 14681{
a48e046c 14682 for (;;)
c906108c 14683 {
a48e046c
TT
14684 unsigned int i;
14685 struct attribute *spec = NULL;
14686
14687 for (i = 0; i < die->num_attrs; ++i)
14688 {
14689 if (die->attrs[i].name == name)
14690 return &die->attrs[i];
14691 if (die->attrs[i].name == DW_AT_specification
14692 || die->attrs[i].name == DW_AT_abstract_origin)
14693 spec = &die->attrs[i];
14694 }
14695
14696 if (!spec)
14697 break;
c906108c 14698
f2f0e013 14699 die = follow_die_ref (die, spec, &cu);
f2f0e013 14700 }
c5aa993b 14701
c906108c
SS
14702 return NULL;
14703}
14704
348e048f
DE
14705/* Return the named attribute or NULL if not there,
14706 but do not follow DW_AT_specification, etc.
14707 This is for use in contexts where we're reading .debug_types dies.
14708 Following DW_AT_specification, DW_AT_abstract_origin will take us
14709 back up the chain, and we want to go down. */
14710
14711static struct attribute *
45e58e77 14712dwarf2_attr_no_follow (struct die_info *die, unsigned int name)
348e048f
DE
14713{
14714 unsigned int i;
14715
14716 for (i = 0; i < die->num_attrs; ++i)
14717 if (die->attrs[i].name == name)
14718 return &die->attrs[i];
14719
14720 return NULL;
14721}
14722
05cf31d1
JB
14723/* Return non-zero iff the attribute NAME is defined for the given DIE,
14724 and holds a non-zero value. This function should only be used for
2dc7f7b3 14725 DW_FORM_flag or DW_FORM_flag_present attributes. */
05cf31d1
JB
14726
14727static int
14728dwarf2_flag_true_p (struct die_info *die, unsigned name, struct dwarf2_cu *cu)
14729{
14730 struct attribute *attr = dwarf2_attr (die, name, cu);
14731
14732 return (attr && DW_UNSND (attr));
14733}
14734
3ca72b44 14735static int
e142c38c 14736die_is_declaration (struct die_info *die, struct dwarf2_cu *cu)
3ca72b44 14737{
05cf31d1
JB
14738 /* A DIE is a declaration if it has a DW_AT_declaration attribute
14739 which value is non-zero. However, we have to be careful with
14740 DIEs having a DW_AT_specification attribute, because dwarf2_attr()
14741 (via dwarf2_flag_true_p) follows this attribute. So we may
14742 end up accidently finding a declaration attribute that belongs
14743 to a different DIE referenced by the specification attribute,
14744 even though the given DIE does not have a declaration attribute. */
14745 return (dwarf2_flag_true_p (die, DW_AT_declaration, cu)
14746 && dwarf2_attr (die, DW_AT_specification, cu) == NULL);
3ca72b44
AC
14747}
14748
63d06c5c 14749/* Return the die giving the specification for DIE, if there is
f2f0e013 14750 one. *SPEC_CU is the CU containing DIE on input, and the CU
edb3359d
DJ
14751 containing the return value on output. If there is no
14752 specification, but there is an abstract origin, that is
14753 returned. */
63d06c5c
DC
14754
14755static struct die_info *
f2f0e013 14756die_specification (struct die_info *die, struct dwarf2_cu **spec_cu)
63d06c5c 14757{
f2f0e013
DJ
14758 struct attribute *spec_attr = dwarf2_attr (die, DW_AT_specification,
14759 *spec_cu);
63d06c5c 14760
edb3359d
DJ
14761 if (spec_attr == NULL)
14762 spec_attr = dwarf2_attr (die, DW_AT_abstract_origin, *spec_cu);
14763
63d06c5c
DC
14764 if (spec_attr == NULL)
14765 return NULL;
14766 else
f2f0e013 14767 return follow_die_ref (die, spec_attr, spec_cu);
63d06c5c 14768}
c906108c 14769
debd256d 14770/* Free the line_header structure *LH, and any arrays and strings it
ae2de4f8
DE
14771 refers to.
14772 NOTE: This is also used as a "cleanup" function. */
14773
debd256d
JB
14774static void
14775free_line_header (struct line_header *lh)
14776{
14777 if (lh->standard_opcode_lengths)
a8bc7b56 14778 xfree (lh->standard_opcode_lengths);
debd256d
JB
14779
14780 /* Remember that all the lh->file_names[i].name pointers are
14781 pointers into debug_line_buffer, and don't need to be freed. */
14782 if (lh->file_names)
a8bc7b56 14783 xfree (lh->file_names);
debd256d
JB
14784
14785 /* Similarly for the include directory names. */
14786 if (lh->include_dirs)
a8bc7b56 14787 xfree (lh->include_dirs);
debd256d 14788
a8bc7b56 14789 xfree (lh);
debd256d
JB
14790}
14791
debd256d 14792/* Add an entry to LH's include directory table. */
ae2de4f8 14793
debd256d
JB
14794static void
14795add_include_dir (struct line_header *lh, char *include_dir)
c906108c 14796{
debd256d
JB
14797 /* Grow the array if necessary. */
14798 if (lh->include_dirs_size == 0)
c5aa993b 14799 {
debd256d
JB
14800 lh->include_dirs_size = 1; /* for testing */
14801 lh->include_dirs = xmalloc (lh->include_dirs_size
14802 * sizeof (*lh->include_dirs));
14803 }
14804 else if (lh->num_include_dirs >= lh->include_dirs_size)
14805 {
14806 lh->include_dirs_size *= 2;
14807 lh->include_dirs = xrealloc (lh->include_dirs,
14808 (lh->include_dirs_size
14809 * sizeof (*lh->include_dirs)));
c5aa993b 14810 }
c906108c 14811
debd256d
JB
14812 lh->include_dirs[lh->num_include_dirs++] = include_dir;
14813}
6e70227d 14814
debd256d 14815/* Add an entry to LH's file name table. */
ae2de4f8 14816
debd256d
JB
14817static void
14818add_file_name (struct line_header *lh,
14819 char *name,
14820 unsigned int dir_index,
14821 unsigned int mod_time,
14822 unsigned int length)
14823{
14824 struct file_entry *fe;
14825
14826 /* Grow the array if necessary. */
14827 if (lh->file_names_size == 0)
14828 {
14829 lh->file_names_size = 1; /* for testing */
14830 lh->file_names = xmalloc (lh->file_names_size
14831 * sizeof (*lh->file_names));
14832 }
14833 else if (lh->num_file_names >= lh->file_names_size)
14834 {
14835 lh->file_names_size *= 2;
14836 lh->file_names = xrealloc (lh->file_names,
14837 (lh->file_names_size
14838 * sizeof (*lh->file_names)));
14839 }
14840
14841 fe = &lh->file_names[lh->num_file_names++];
14842 fe->name = name;
14843 fe->dir_index = dir_index;
14844 fe->mod_time = mod_time;
14845 fe->length = length;
aaa75496 14846 fe->included_p = 0;
cb1df416 14847 fe->symtab = NULL;
debd256d 14848}
6e70227d 14849
36586728
TT
14850/* A convenience function to find the proper .debug_line section for a
14851 CU. */
14852
14853static struct dwarf2_section_info *
14854get_debug_line_section (struct dwarf2_cu *cu)
14855{
14856 struct dwarf2_section_info *section;
14857
14858 /* For TUs in DWO files, the DW_AT_stmt_list attribute lives in the
14859 DWO file. */
14860 if (cu->dwo_unit && cu->per_cu->is_debug_types)
14861 section = &cu->dwo_unit->dwo_file->sections.line;
14862 else if (cu->per_cu->is_dwz)
14863 {
14864 struct dwz_file *dwz = dwarf2_get_dwz_file ();
14865
14866 section = &dwz->line;
14867 }
14868 else
14869 section = &dwarf2_per_objfile->line;
14870
14871 return section;
14872}
14873
debd256d 14874/* Read the statement program header starting at OFFSET in
3019eac3 14875 .debug_line, or .debug_line.dwo. Return a pointer
6502dd73 14876 to a struct line_header, allocated using xmalloc.
debd256d
JB
14877
14878 NOTE: the strings in the include directory and file name tables of
3019eac3
DE
14879 the returned object point into the dwarf line section buffer,
14880 and must not be freed. */
ae2de4f8 14881
debd256d 14882static struct line_header *
3019eac3 14883dwarf_decode_line_header (unsigned int offset, struct dwarf2_cu *cu)
debd256d
JB
14884{
14885 struct cleanup *back_to;
14886 struct line_header *lh;
fe1b8b76 14887 gdb_byte *line_ptr;
c764a876 14888 unsigned int bytes_read, offset_size;
debd256d
JB
14889 int i;
14890 char *cur_dir, *cur_file;
3019eac3
DE
14891 struct dwarf2_section_info *section;
14892 bfd *abfd;
14893
36586728 14894 section = get_debug_line_section (cu);
3019eac3
DE
14895 dwarf2_read_section (dwarf2_per_objfile->objfile, section);
14896 if (section->buffer == NULL)
debd256d 14897 {
3019eac3
DE
14898 if (cu->dwo_unit && cu->per_cu->is_debug_types)
14899 complaint (&symfile_complaints, _("missing .debug_line.dwo section"));
14900 else
14901 complaint (&symfile_complaints, _("missing .debug_line section"));
debd256d
JB
14902 return 0;
14903 }
14904
fceca515
DE
14905 /* We can't do this until we know the section is non-empty.
14906 Only then do we know we have such a section. */
14907 abfd = section->asection->owner;
14908
a738430d
MK
14909 /* Make sure that at least there's room for the total_length field.
14910 That could be 12 bytes long, but we're just going to fudge that. */
3019eac3 14911 if (offset + 4 >= section->size)
debd256d 14912 {
4d3c2250 14913 dwarf2_statement_list_fits_in_line_number_section_complaint ();
debd256d
JB
14914 return 0;
14915 }
14916
14917 lh = xmalloc (sizeof (*lh));
14918 memset (lh, 0, sizeof (*lh));
14919 back_to = make_cleanup ((make_cleanup_ftype *) free_line_header,
14920 (void *) lh);
14921
3019eac3 14922 line_ptr = section->buffer + offset;
debd256d 14923
a738430d 14924 /* Read in the header. */
6e70227d 14925 lh->total_length =
c764a876
DE
14926 read_checked_initial_length_and_offset (abfd, line_ptr, &cu->header,
14927 &bytes_read, &offset_size);
debd256d 14928 line_ptr += bytes_read;
3019eac3 14929 if (line_ptr + lh->total_length > (section->buffer + section->size))
debd256d 14930 {
4d3c2250 14931 dwarf2_statement_list_fits_in_line_number_section_complaint ();
debd256d
JB
14932 return 0;
14933 }
14934 lh->statement_program_end = line_ptr + lh->total_length;
14935 lh->version = read_2_bytes (abfd, line_ptr);
14936 line_ptr += 2;
c764a876
DE
14937 lh->header_length = read_offset_1 (abfd, line_ptr, offset_size);
14938 line_ptr += offset_size;
debd256d
JB
14939 lh->minimum_instruction_length = read_1_byte (abfd, line_ptr);
14940 line_ptr += 1;
2dc7f7b3
TT
14941 if (lh->version >= 4)
14942 {
14943 lh->maximum_ops_per_instruction = read_1_byte (abfd, line_ptr);
14944 line_ptr += 1;
14945 }
14946 else
14947 lh->maximum_ops_per_instruction = 1;
14948
14949 if (lh->maximum_ops_per_instruction == 0)
14950 {
14951 lh->maximum_ops_per_instruction = 1;
14952 complaint (&symfile_complaints,
3e43a32a
MS
14953 _("invalid maximum_ops_per_instruction "
14954 "in `.debug_line' section"));
2dc7f7b3
TT
14955 }
14956
debd256d
JB
14957 lh->default_is_stmt = read_1_byte (abfd, line_ptr);
14958 line_ptr += 1;
14959 lh->line_base = read_1_signed_byte (abfd, line_ptr);
14960 line_ptr += 1;
14961 lh->line_range = read_1_byte (abfd, line_ptr);
14962 line_ptr += 1;
14963 lh->opcode_base = read_1_byte (abfd, line_ptr);
14964 line_ptr += 1;
14965 lh->standard_opcode_lengths
fe1b8b76 14966 = xmalloc (lh->opcode_base * sizeof (lh->standard_opcode_lengths[0]));
debd256d
JB
14967
14968 lh->standard_opcode_lengths[0] = 1; /* This should never be used anyway. */
14969 for (i = 1; i < lh->opcode_base; ++i)
14970 {
14971 lh->standard_opcode_lengths[i] = read_1_byte (abfd, line_ptr);
14972 line_ptr += 1;
14973 }
14974
a738430d 14975 /* Read directory table. */
9b1c24c8 14976 while ((cur_dir = read_direct_string (abfd, line_ptr, &bytes_read)) != NULL)
debd256d
JB
14977 {
14978 line_ptr += bytes_read;
14979 add_include_dir (lh, cur_dir);
14980 }
14981 line_ptr += bytes_read;
14982
a738430d 14983 /* Read file name table. */
9b1c24c8 14984 while ((cur_file = read_direct_string (abfd, line_ptr, &bytes_read)) != NULL)
debd256d
JB
14985 {
14986 unsigned int dir_index, mod_time, length;
14987
14988 line_ptr += bytes_read;
14989 dir_index = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
14990 line_ptr += bytes_read;
14991 mod_time = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
14992 line_ptr += bytes_read;
14993 length = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
14994 line_ptr += bytes_read;
14995
14996 add_file_name (lh, cur_file, dir_index, mod_time, length);
14997 }
14998 line_ptr += bytes_read;
6e70227d 14999 lh->statement_program_start = line_ptr;
debd256d 15000
3019eac3 15001 if (line_ptr > (section->buffer + section->size))
4d3c2250 15002 complaint (&symfile_complaints,
3e43a32a
MS
15003 _("line number info header doesn't "
15004 "fit in `.debug_line' section"));
debd256d
JB
15005
15006 discard_cleanups (back_to);
15007 return lh;
15008}
c906108c 15009
c6da4cef
DE
15010/* Subroutine of dwarf_decode_lines to simplify it.
15011 Return the file name of the psymtab for included file FILE_INDEX
15012 in line header LH of PST.
15013 COMP_DIR is the compilation directory (DW_AT_comp_dir) or NULL if unknown.
15014 If space for the result is malloc'd, it will be freed by a cleanup.
15015 Returns NULL if FILE_INDEX should be ignored, i.e., it is pst->filename. */
15016
15017static char *
15018psymtab_include_file_name (const struct line_header *lh, int file_index,
15019 const struct partial_symtab *pst,
15020 const char *comp_dir)
15021{
15022 const struct file_entry fe = lh->file_names [file_index];
15023 char *include_name = fe.name;
15024 char *include_name_to_compare = include_name;
15025 char *dir_name = NULL;
72b9f47f
TT
15026 const char *pst_filename;
15027 char *copied_name = NULL;
c6da4cef
DE
15028 int file_is_pst;
15029
15030 if (fe.dir_index)
15031 dir_name = lh->include_dirs[fe.dir_index - 1];
15032
15033 if (!IS_ABSOLUTE_PATH (include_name)
15034 && (dir_name != NULL || comp_dir != NULL))
15035 {
15036 /* Avoid creating a duplicate psymtab for PST.
15037 We do this by comparing INCLUDE_NAME and PST_FILENAME.
15038 Before we do the comparison, however, we need to account
15039 for DIR_NAME and COMP_DIR.
15040 First prepend dir_name (if non-NULL). If we still don't
15041 have an absolute path prepend comp_dir (if non-NULL).
15042 However, the directory we record in the include-file's
15043 psymtab does not contain COMP_DIR (to match the
15044 corresponding symtab(s)).
15045
15046 Example:
15047
15048 bash$ cd /tmp
15049 bash$ gcc -g ./hello.c
15050 include_name = "hello.c"
15051 dir_name = "."
15052 DW_AT_comp_dir = comp_dir = "/tmp"
15053 DW_AT_name = "./hello.c" */
15054
15055 if (dir_name != NULL)
15056 {
15057 include_name = concat (dir_name, SLASH_STRING,
15058 include_name, (char *)NULL);
15059 include_name_to_compare = include_name;
15060 make_cleanup (xfree, include_name);
15061 }
15062 if (!IS_ABSOLUTE_PATH (include_name) && comp_dir != NULL)
15063 {
15064 include_name_to_compare = concat (comp_dir, SLASH_STRING,
15065 include_name, (char *)NULL);
15066 }
15067 }
15068
15069 pst_filename = pst->filename;
15070 if (!IS_ABSOLUTE_PATH (pst_filename) && pst->dirname != NULL)
15071 {
72b9f47f
TT
15072 copied_name = concat (pst->dirname, SLASH_STRING,
15073 pst_filename, (char *)NULL);
15074 pst_filename = copied_name;
c6da4cef
DE
15075 }
15076
1e3fad37 15077 file_is_pst = FILENAME_CMP (include_name_to_compare, pst_filename) == 0;
c6da4cef
DE
15078
15079 if (include_name_to_compare != include_name)
15080 xfree (include_name_to_compare);
72b9f47f
TT
15081 if (copied_name != NULL)
15082 xfree (copied_name);
c6da4cef
DE
15083
15084 if (file_is_pst)
15085 return NULL;
15086 return include_name;
15087}
15088
c91513d8
PP
15089/* Ignore this record_line request. */
15090
15091static void
15092noop_record_line (struct subfile *subfile, int line, CORE_ADDR pc)
15093{
15094 return;
15095}
15096
f3f5162e
DE
15097/* Subroutine of dwarf_decode_lines to simplify it.
15098 Process the line number information in LH. */
debd256d 15099
c906108c 15100static void
f3f5162e
DE
15101dwarf_decode_lines_1 (struct line_header *lh, const char *comp_dir,
15102 struct dwarf2_cu *cu, struct partial_symtab *pst)
c906108c 15103{
a8c50c1f 15104 gdb_byte *line_ptr, *extended_end;
fe1b8b76 15105 gdb_byte *line_end;
a8c50c1f 15106 unsigned int bytes_read, extended_len;
c906108c 15107 unsigned char op_code, extended_op, adj_opcode;
e142c38c
DJ
15108 CORE_ADDR baseaddr;
15109 struct objfile *objfile = cu->objfile;
f3f5162e 15110 bfd *abfd = objfile->obfd;
fbf65064 15111 struct gdbarch *gdbarch = get_objfile_arch (objfile);
aaa75496 15112 const int decode_for_pst_p = (pst != NULL);
f3f5162e 15113 struct subfile *last_subfile = NULL;
c91513d8
PP
15114 void (*p_record_line) (struct subfile *subfile, int line, CORE_ADDR pc)
15115 = record_line;
e142c38c
DJ
15116
15117 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
c906108c 15118
debd256d
JB
15119 line_ptr = lh->statement_program_start;
15120 line_end = lh->statement_program_end;
c906108c
SS
15121
15122 /* Read the statement sequences until there's nothing left. */
15123 while (line_ptr < line_end)
15124 {
15125 /* state machine registers */
15126 CORE_ADDR address = 0;
15127 unsigned int file = 1;
15128 unsigned int line = 1;
15129 unsigned int column = 0;
debd256d 15130 int is_stmt = lh->default_is_stmt;
c906108c
SS
15131 int basic_block = 0;
15132 int end_sequence = 0;
fbf65064 15133 CORE_ADDR addr;
2dc7f7b3 15134 unsigned char op_index = 0;
c906108c 15135
aaa75496 15136 if (!decode_for_pst_p && lh->num_file_names >= file)
c906108c 15137 {
aaa75496 15138 /* Start a subfile for the current file of the state machine. */
debd256d
JB
15139 /* lh->include_dirs and lh->file_names are 0-based, but the
15140 directory and file name numbers in the statement program
15141 are 1-based. */
15142 struct file_entry *fe = &lh->file_names[file - 1];
4f1520fb 15143 char *dir = NULL;
a738430d 15144
debd256d
JB
15145 if (fe->dir_index)
15146 dir = lh->include_dirs[fe->dir_index - 1];
4f1520fb
FR
15147
15148 dwarf2_start_subfile (fe->name, dir, comp_dir);
c906108c
SS
15149 }
15150
a738430d 15151 /* Decode the table. */
c5aa993b 15152 while (!end_sequence)
c906108c
SS
15153 {
15154 op_code = read_1_byte (abfd, line_ptr);
15155 line_ptr += 1;
59205f5a
JB
15156 if (line_ptr > line_end)
15157 {
15158 dwarf2_debug_line_missing_end_sequence_complaint ();
15159 break;
15160 }
9aa1fe7e 15161
debd256d 15162 if (op_code >= lh->opcode_base)
6e70227d 15163 {
a738430d 15164 /* Special operand. */
debd256d 15165 adj_opcode = op_code - lh->opcode_base;
2dc7f7b3
TT
15166 address += (((op_index + (adj_opcode / lh->line_range))
15167 / lh->maximum_ops_per_instruction)
15168 * lh->minimum_instruction_length);
15169 op_index = ((op_index + (adj_opcode / lh->line_range))
15170 % lh->maximum_ops_per_instruction);
debd256d 15171 line += lh->line_base + (adj_opcode % lh->line_range);
59205f5a 15172 if (lh->num_file_names < file || file == 0)
25e43795 15173 dwarf2_debug_line_missing_file_complaint ();
2dc7f7b3
TT
15174 /* For now we ignore lines not starting on an
15175 instruction boundary. */
15176 else if (op_index == 0)
25e43795
DJ
15177 {
15178 lh->file_names[file - 1].included_p = 1;
ca5f395d 15179 if (!decode_for_pst_p && is_stmt)
fbf65064
UW
15180 {
15181 if (last_subfile != current_subfile)
15182 {
15183 addr = gdbarch_addr_bits_remove (gdbarch, address);
15184 if (last_subfile)
c91513d8 15185 (*p_record_line) (last_subfile, 0, addr);
fbf65064
UW
15186 last_subfile = current_subfile;
15187 }
25e43795 15188 /* Append row to matrix using current values. */
7019d805 15189 addr = gdbarch_addr_bits_remove (gdbarch, address);
c91513d8 15190 (*p_record_line) (current_subfile, line, addr);
366da635 15191 }
25e43795 15192 }
ca5f395d 15193 basic_block = 0;
9aa1fe7e
GK
15194 }
15195 else switch (op_code)
c906108c
SS
15196 {
15197 case DW_LNS_extended_op:
3e43a32a
MS
15198 extended_len = read_unsigned_leb128 (abfd, line_ptr,
15199 &bytes_read);
473b7be6 15200 line_ptr += bytes_read;
a8c50c1f 15201 extended_end = line_ptr + extended_len;
c906108c
SS
15202 extended_op = read_1_byte (abfd, line_ptr);
15203 line_ptr += 1;
15204 switch (extended_op)
15205 {
15206 case DW_LNE_end_sequence:
c91513d8 15207 p_record_line = record_line;
c906108c 15208 end_sequence = 1;
c906108c
SS
15209 break;
15210 case DW_LNE_set_address:
e7c27a73 15211 address = read_address (abfd, line_ptr, cu, &bytes_read);
c91513d8
PP
15212
15213 if (address == 0 && !dwarf2_per_objfile->has_section_at_zero)
15214 {
15215 /* This line table is for a function which has been
15216 GCd by the linker. Ignore it. PR gdb/12528 */
15217
15218 long line_offset
36586728 15219 = line_ptr - get_debug_line_section (cu)->buffer;
c91513d8
PP
15220
15221 complaint (&symfile_complaints,
15222 _(".debug_line address at offset 0x%lx is 0 "
15223 "[in module %s]"),
bb5ed363 15224 line_offset, objfile->name);
c91513d8
PP
15225 p_record_line = noop_record_line;
15226 }
15227
2dc7f7b3 15228 op_index = 0;
107d2387
AC
15229 line_ptr += bytes_read;
15230 address += baseaddr;
c906108c
SS
15231 break;
15232 case DW_LNE_define_file:
debd256d
JB
15233 {
15234 char *cur_file;
15235 unsigned int dir_index, mod_time, length;
6e70227d 15236
3e43a32a
MS
15237 cur_file = read_direct_string (abfd, line_ptr,
15238 &bytes_read);
debd256d
JB
15239 line_ptr += bytes_read;
15240 dir_index =
15241 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
15242 line_ptr += bytes_read;
15243 mod_time =
15244 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
15245 line_ptr += bytes_read;
15246 length =
15247 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
15248 line_ptr += bytes_read;
15249 add_file_name (lh, cur_file, dir_index, mod_time, length);
15250 }
c906108c 15251 break;
d0c6ba3d
CC
15252 case DW_LNE_set_discriminator:
15253 /* The discriminator is not interesting to the debugger;
15254 just ignore it. */
15255 line_ptr = extended_end;
15256 break;
c906108c 15257 default:
4d3c2250 15258 complaint (&symfile_complaints,
e2e0b3e5 15259 _("mangled .debug_line section"));
debd256d 15260 return;
c906108c 15261 }
a8c50c1f
DJ
15262 /* Make sure that we parsed the extended op correctly. If e.g.
15263 we expected a different address size than the producer used,
15264 we may have read the wrong number of bytes. */
15265 if (line_ptr != extended_end)
15266 {
15267 complaint (&symfile_complaints,
15268 _("mangled .debug_line section"));
15269 return;
15270 }
c906108c
SS
15271 break;
15272 case DW_LNS_copy:
59205f5a 15273 if (lh->num_file_names < file || file == 0)
25e43795
DJ
15274 dwarf2_debug_line_missing_file_complaint ();
15275 else
366da635 15276 {
25e43795 15277 lh->file_names[file - 1].included_p = 1;
ca5f395d 15278 if (!decode_for_pst_p && is_stmt)
fbf65064
UW
15279 {
15280 if (last_subfile != current_subfile)
15281 {
15282 addr = gdbarch_addr_bits_remove (gdbarch, address);
15283 if (last_subfile)
c91513d8 15284 (*p_record_line) (last_subfile, 0, addr);
fbf65064
UW
15285 last_subfile = current_subfile;
15286 }
7019d805 15287 addr = gdbarch_addr_bits_remove (gdbarch, address);
c91513d8 15288 (*p_record_line) (current_subfile, line, addr);
fbf65064 15289 }
366da635 15290 }
c906108c
SS
15291 basic_block = 0;
15292 break;
15293 case DW_LNS_advance_pc:
2dc7f7b3
TT
15294 {
15295 CORE_ADDR adjust
15296 = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
15297
15298 address += (((op_index + adjust)
15299 / lh->maximum_ops_per_instruction)
15300 * lh->minimum_instruction_length);
15301 op_index = ((op_index + adjust)
15302 % lh->maximum_ops_per_instruction);
15303 line_ptr += bytes_read;
15304 }
c906108c
SS
15305 break;
15306 case DW_LNS_advance_line:
15307 line += read_signed_leb128 (abfd, line_ptr, &bytes_read);
15308 line_ptr += bytes_read;
15309 break;
15310 case DW_LNS_set_file:
debd256d 15311 {
a738430d
MK
15312 /* The arrays lh->include_dirs and lh->file_names are
15313 0-based, but the directory and file name numbers in
15314 the statement program are 1-based. */
debd256d 15315 struct file_entry *fe;
4f1520fb 15316 char *dir = NULL;
a738430d 15317
debd256d
JB
15318 file = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
15319 line_ptr += bytes_read;
59205f5a 15320 if (lh->num_file_names < file || file == 0)
25e43795
DJ
15321 dwarf2_debug_line_missing_file_complaint ();
15322 else
15323 {
15324 fe = &lh->file_names[file - 1];
15325 if (fe->dir_index)
15326 dir = lh->include_dirs[fe->dir_index - 1];
15327 if (!decode_for_pst_p)
15328 {
15329 last_subfile = current_subfile;
15330 dwarf2_start_subfile (fe->name, dir, comp_dir);
15331 }
15332 }
debd256d 15333 }
c906108c
SS
15334 break;
15335 case DW_LNS_set_column:
15336 column = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
15337 line_ptr += bytes_read;
15338 break;
15339 case DW_LNS_negate_stmt:
15340 is_stmt = (!is_stmt);
15341 break;
15342 case DW_LNS_set_basic_block:
15343 basic_block = 1;
15344 break;
c2c6d25f
JM
15345 /* Add to the address register of the state machine the
15346 address increment value corresponding to special opcode
a738430d
MK
15347 255. I.e., this value is scaled by the minimum
15348 instruction length since special opcode 255 would have
b021a221 15349 scaled the increment. */
c906108c 15350 case DW_LNS_const_add_pc:
2dc7f7b3
TT
15351 {
15352 CORE_ADDR adjust = (255 - lh->opcode_base) / lh->line_range;
15353
15354 address += (((op_index + adjust)
15355 / lh->maximum_ops_per_instruction)
15356 * lh->minimum_instruction_length);
15357 op_index = ((op_index + adjust)
15358 % lh->maximum_ops_per_instruction);
15359 }
c906108c
SS
15360 break;
15361 case DW_LNS_fixed_advance_pc:
15362 address += read_2_bytes (abfd, line_ptr);
2dc7f7b3 15363 op_index = 0;
c906108c
SS
15364 line_ptr += 2;
15365 break;
9aa1fe7e 15366 default:
a738430d
MK
15367 {
15368 /* Unknown standard opcode, ignore it. */
9aa1fe7e 15369 int i;
a738430d 15370
debd256d 15371 for (i = 0; i < lh->standard_opcode_lengths[op_code]; i++)
9aa1fe7e
GK
15372 {
15373 (void) read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
15374 line_ptr += bytes_read;
15375 }
15376 }
c906108c
SS
15377 }
15378 }
59205f5a
JB
15379 if (lh->num_file_names < file || file == 0)
15380 dwarf2_debug_line_missing_file_complaint ();
15381 else
15382 {
15383 lh->file_names[file - 1].included_p = 1;
15384 if (!decode_for_pst_p)
fbf65064
UW
15385 {
15386 addr = gdbarch_addr_bits_remove (gdbarch, address);
c91513d8 15387 (*p_record_line) (current_subfile, 0, addr);
fbf65064 15388 }
59205f5a 15389 }
c906108c 15390 }
f3f5162e
DE
15391}
15392
15393/* Decode the Line Number Program (LNP) for the given line_header
15394 structure and CU. The actual information extracted and the type
15395 of structures created from the LNP depends on the value of PST.
15396
15397 1. If PST is NULL, then this procedure uses the data from the program
15398 to create all necessary symbol tables, and their linetables.
15399
15400 2. If PST is not NULL, this procedure reads the program to determine
15401 the list of files included by the unit represented by PST, and
15402 builds all the associated partial symbol tables.
15403
15404 COMP_DIR is the compilation directory (DW_AT_comp_dir) or NULL if unknown.
15405 It is used for relative paths in the line table.
15406 NOTE: When processing partial symtabs (pst != NULL),
15407 comp_dir == pst->dirname.
15408
15409 NOTE: It is important that psymtabs have the same file name (via strcmp)
15410 as the corresponding symtab. Since COMP_DIR is not used in the name of the
15411 symtab we don't use it in the name of the psymtabs we create.
15412 E.g. expand_line_sal requires this when finding psymtabs to expand.
15413 A good testcase for this is mb-inline.exp. */
15414
15415static void
15416dwarf_decode_lines (struct line_header *lh, const char *comp_dir,
15417 struct dwarf2_cu *cu, struct partial_symtab *pst,
15418 int want_line_info)
15419{
15420 struct objfile *objfile = cu->objfile;
15421 const int decode_for_pst_p = (pst != NULL);
15422 struct subfile *first_subfile = current_subfile;
15423
15424 if (want_line_info)
15425 dwarf_decode_lines_1 (lh, comp_dir, cu, pst);
aaa75496
JB
15426
15427 if (decode_for_pst_p)
15428 {
15429 int file_index;
15430
15431 /* Now that we're done scanning the Line Header Program, we can
15432 create the psymtab of each included file. */
15433 for (file_index = 0; file_index < lh->num_file_names; file_index++)
15434 if (lh->file_names[file_index].included_p == 1)
15435 {
c6da4cef
DE
15436 char *include_name =
15437 psymtab_include_file_name (lh, file_index, pst, comp_dir);
15438 if (include_name != NULL)
aaa75496
JB
15439 dwarf2_create_include_psymtab (include_name, pst, objfile);
15440 }
15441 }
cb1df416
DJ
15442 else
15443 {
15444 /* Make sure a symtab is created for every file, even files
15445 which contain only variables (i.e. no code with associated
15446 line numbers). */
cb1df416 15447 int i;
cb1df416
DJ
15448
15449 for (i = 0; i < lh->num_file_names; i++)
15450 {
15451 char *dir = NULL;
f3f5162e 15452 struct file_entry *fe;
9a619af0 15453
cb1df416
DJ
15454 fe = &lh->file_names[i];
15455 if (fe->dir_index)
15456 dir = lh->include_dirs[fe->dir_index - 1];
15457 dwarf2_start_subfile (fe->name, dir, comp_dir);
15458
15459 /* Skip the main file; we don't need it, and it must be
15460 allocated last, so that it will show up before the
15461 non-primary symtabs in the objfile's symtab list. */
15462 if (current_subfile == first_subfile)
15463 continue;
15464
15465 if (current_subfile->symtab == NULL)
15466 current_subfile->symtab = allocate_symtab (current_subfile->name,
bb5ed363 15467 objfile);
cb1df416
DJ
15468 fe->symtab = current_subfile->symtab;
15469 }
15470 }
c906108c
SS
15471}
15472
15473/* Start a subfile for DWARF. FILENAME is the name of the file and
15474 DIRNAME the name of the source directory which contains FILENAME
4f1520fb
FR
15475 or NULL if not known. COMP_DIR is the compilation directory for the
15476 linetable's compilation unit or NULL if not known.
c906108c
SS
15477 This routine tries to keep line numbers from identical absolute and
15478 relative file names in a common subfile.
15479
15480 Using the `list' example from the GDB testsuite, which resides in
15481 /srcdir and compiling it with Irix6.2 cc in /compdir using a filename
15482 of /srcdir/list0.c yields the following debugging information for list0.c:
15483
c5aa993b
JM
15484 DW_AT_name: /srcdir/list0.c
15485 DW_AT_comp_dir: /compdir
357e46e7 15486 files.files[0].name: list0.h
c5aa993b 15487 files.files[0].dir: /srcdir
357e46e7 15488 files.files[1].name: list0.c
c5aa993b 15489 files.files[1].dir: /srcdir
c906108c
SS
15490
15491 The line number information for list0.c has to end up in a single
4f1520fb
FR
15492 subfile, so that `break /srcdir/list0.c:1' works as expected.
15493 start_subfile will ensure that this happens provided that we pass the
15494 concatenation of files.files[1].dir and files.files[1].name as the
15495 subfile's name. */
c906108c
SS
15496
15497static void
3e43a32a
MS
15498dwarf2_start_subfile (char *filename, const char *dirname,
15499 const char *comp_dir)
c906108c 15500{
4f1520fb
FR
15501 char *fullname;
15502
15503 /* While reading the DIEs, we call start_symtab(DW_AT_name, DW_AT_comp_dir).
15504 `start_symtab' will always pass the contents of DW_AT_comp_dir as
15505 second argument to start_subfile. To be consistent, we do the
15506 same here. In order not to lose the line information directory,
15507 we concatenate it to the filename when it makes sense.
15508 Note that the Dwarf3 standard says (speaking of filenames in line
15509 information): ``The directory index is ignored for file names
15510 that represent full path names''. Thus ignoring dirname in the
15511 `else' branch below isn't an issue. */
c906108c 15512
d5166ae1 15513 if (!IS_ABSOLUTE_PATH (filename) && dirname != NULL)
4f1520fb
FR
15514 fullname = concat (dirname, SLASH_STRING, filename, (char *)NULL);
15515 else
15516 fullname = filename;
c906108c 15517
4f1520fb
FR
15518 start_subfile (fullname, comp_dir);
15519
15520 if (fullname != filename)
15521 xfree (fullname);
c906108c
SS
15522}
15523
f4dc4d17
DE
15524/* Start a symtab for DWARF.
15525 NAME, COMP_DIR, LOW_PC are passed to start_symtab. */
15526
15527static void
15528dwarf2_start_symtab (struct dwarf2_cu *cu,
15529 char *name, char *comp_dir, CORE_ADDR low_pc)
15530{
15531 start_symtab (name, comp_dir, low_pc);
15532 record_debugformat ("DWARF 2");
15533 record_producer (cu->producer);
15534
15535 /* We assume that we're processing GCC output. */
15536 processing_gcc_compilation = 2;
15537
15538 processing_has_namespace_info = 0;
15539}
15540
4c2df51b
DJ
15541static void
15542var_decode_location (struct attribute *attr, struct symbol *sym,
e7c27a73 15543 struct dwarf2_cu *cu)
4c2df51b 15544{
e7c27a73
DJ
15545 struct objfile *objfile = cu->objfile;
15546 struct comp_unit_head *cu_header = &cu->header;
15547
4c2df51b
DJ
15548 /* NOTE drow/2003-01-30: There used to be a comment and some special
15549 code here to turn a symbol with DW_AT_external and a
15550 SYMBOL_VALUE_ADDRESS of 0 into a LOC_UNRESOLVED symbol. This was
15551 necessary for platforms (maybe Alpha, certainly PowerPC GNU/Linux
15552 with some versions of binutils) where shared libraries could have
15553 relocations against symbols in their debug information - the
15554 minimal symbol would have the right address, but the debug info
15555 would not. It's no longer necessary, because we will explicitly
15556 apply relocations when we read in the debug information now. */
15557
15558 /* A DW_AT_location attribute with no contents indicates that a
15559 variable has been optimized away. */
15560 if (attr_form_is_block (attr) && DW_BLOCK (attr)->size == 0)
15561 {
15562 SYMBOL_CLASS (sym) = LOC_OPTIMIZED_OUT;
15563 return;
15564 }
15565
15566 /* Handle one degenerate form of location expression specially, to
15567 preserve GDB's previous behavior when section offsets are
3019eac3
DE
15568 specified. If this is just a DW_OP_addr or DW_OP_GNU_addr_index
15569 then mark this symbol as LOC_STATIC. */
4c2df51b
DJ
15570
15571 if (attr_form_is_block (attr)
3019eac3
DE
15572 && ((DW_BLOCK (attr)->data[0] == DW_OP_addr
15573 && DW_BLOCK (attr)->size == 1 + cu_header->addr_size)
15574 || (DW_BLOCK (attr)->data[0] == DW_OP_GNU_addr_index
15575 && (DW_BLOCK (attr)->size
15576 == 1 + leb128_size (&DW_BLOCK (attr)->data[1])))))
4c2df51b 15577 {
891d2f0b 15578 unsigned int dummy;
4c2df51b 15579
3019eac3
DE
15580 if (DW_BLOCK (attr)->data[0] == DW_OP_addr)
15581 SYMBOL_VALUE_ADDRESS (sym) =
15582 read_address (objfile->obfd, DW_BLOCK (attr)->data + 1, cu, &dummy);
15583 else
15584 SYMBOL_VALUE_ADDRESS (sym) =
15585 read_addr_index_from_leb128 (cu, DW_BLOCK (attr)->data + 1, &dummy);
907fc202 15586 SYMBOL_CLASS (sym) = LOC_STATIC;
4c2df51b
DJ
15587 fixup_symbol_section (sym, objfile);
15588 SYMBOL_VALUE_ADDRESS (sym) += ANOFFSET (objfile->section_offsets,
15589 SYMBOL_SECTION (sym));
4c2df51b
DJ
15590 return;
15591 }
15592
15593 /* NOTE drow/2002-01-30: It might be worthwhile to have a static
15594 expression evaluator, and use LOC_COMPUTED only when necessary
15595 (i.e. when the value of a register or memory location is
15596 referenced, or a thread-local block, etc.). Then again, it might
15597 not be worthwhile. I'm assuming that it isn't unless performance
15598 or memory numbers show me otherwise. */
15599
e7c27a73 15600 dwarf2_symbol_mark_computed (attr, sym, cu);
4c2df51b 15601 SYMBOL_CLASS (sym) = LOC_COMPUTED;
8be455d7
JK
15602
15603 if (SYMBOL_COMPUTED_OPS (sym) == &dwarf2_loclist_funcs)
15604 cu->has_loclist = 1;
4c2df51b
DJ
15605}
15606
c906108c
SS
15607/* Given a pointer to a DWARF information entry, figure out if we need
15608 to make a symbol table entry for it, and if so, create a new entry
15609 and return a pointer to it.
15610 If TYPE is NULL, determine symbol type from the die, otherwise
34eaf542
TT
15611 used the passed type.
15612 If SPACE is not NULL, use it to hold the new symbol. If it is
15613 NULL, allocate a new symbol on the objfile's obstack. */
c906108c
SS
15614
15615static struct symbol *
34eaf542
TT
15616new_symbol_full (struct die_info *die, struct type *type, struct dwarf2_cu *cu,
15617 struct symbol *space)
c906108c 15618{
e7c27a73 15619 struct objfile *objfile = cu->objfile;
c906108c
SS
15620 struct symbol *sym = NULL;
15621 char *name;
15622 struct attribute *attr = NULL;
15623 struct attribute *attr2 = NULL;
e142c38c 15624 CORE_ADDR baseaddr;
e37fd15a
SW
15625 struct pending **list_to_add = NULL;
15626
edb3359d 15627 int inlined_func = (die->tag == DW_TAG_inlined_subroutine);
e142c38c
DJ
15628
15629 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
c906108c 15630
94af9270 15631 name = dwarf2_name (die, cu);
c906108c
SS
15632 if (name)
15633 {
94af9270 15634 const char *linkagename;
34eaf542 15635 int suppress_add = 0;
94af9270 15636
34eaf542
TT
15637 if (space)
15638 sym = space;
15639 else
15640 sym = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct symbol);
c906108c 15641 OBJSTAT (objfile, n_syms++);
2de7ced7
DJ
15642
15643 /* Cache this symbol's name and the name's demangled form (if any). */
33e5013e 15644 SYMBOL_SET_LANGUAGE (sym, cu->language);
94af9270
KS
15645 linkagename = dwarf2_physname (name, die, cu);
15646 SYMBOL_SET_NAMES (sym, linkagename, strlen (linkagename), 0, objfile);
c906108c 15647
f55ee35c
JK
15648 /* Fortran does not have mangling standard and the mangling does differ
15649 between gfortran, iFort etc. */
15650 if (cu->language == language_fortran
b250c185 15651 && symbol_get_demangled_name (&(sym->ginfo)) == NULL)
29df156d
SW
15652 symbol_set_demangled_name (&(sym->ginfo),
15653 (char *) dwarf2_full_name (name, die, cu),
15654 NULL);
f55ee35c 15655
c906108c 15656 /* Default assumptions.
c5aa993b 15657 Use the passed type or decode it from the die. */
176620f1 15658 SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
875dc2fc 15659 SYMBOL_CLASS (sym) = LOC_OPTIMIZED_OUT;
c906108c
SS
15660 if (type != NULL)
15661 SYMBOL_TYPE (sym) = type;
15662 else
e7c27a73 15663 SYMBOL_TYPE (sym) = die_type (die, cu);
edb3359d
DJ
15664 attr = dwarf2_attr (die,
15665 inlined_func ? DW_AT_call_line : DW_AT_decl_line,
15666 cu);
c906108c
SS
15667 if (attr)
15668 {
15669 SYMBOL_LINE (sym) = DW_UNSND (attr);
15670 }
cb1df416 15671
edb3359d
DJ
15672 attr = dwarf2_attr (die,
15673 inlined_func ? DW_AT_call_file : DW_AT_decl_file,
15674 cu);
cb1df416
DJ
15675 if (attr)
15676 {
15677 int file_index = DW_UNSND (attr);
9a619af0 15678
cb1df416
DJ
15679 if (cu->line_header == NULL
15680 || file_index > cu->line_header->num_file_names)
15681 complaint (&symfile_complaints,
15682 _("file index out of range"));
1c3d648d 15683 else if (file_index > 0)
cb1df416
DJ
15684 {
15685 struct file_entry *fe;
9a619af0 15686
cb1df416
DJ
15687 fe = &cu->line_header->file_names[file_index - 1];
15688 SYMBOL_SYMTAB (sym) = fe->symtab;
15689 }
15690 }
15691
c906108c
SS
15692 switch (die->tag)
15693 {
15694 case DW_TAG_label:
e142c38c 15695 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
c906108c
SS
15696 if (attr)
15697 {
15698 SYMBOL_VALUE_ADDRESS (sym) = DW_ADDR (attr) + baseaddr;
15699 }
0f5238ed
TT
15700 SYMBOL_TYPE (sym) = objfile_type (objfile)->builtin_core_addr;
15701 SYMBOL_DOMAIN (sym) = LABEL_DOMAIN;
c906108c 15702 SYMBOL_CLASS (sym) = LOC_LABEL;
0f5238ed 15703 add_symbol_to_list (sym, cu->list_in_scope);
c906108c
SS
15704 break;
15705 case DW_TAG_subprogram:
15706 /* SYMBOL_BLOCK_VALUE (sym) will be filled in later by
15707 finish_block. */
15708 SYMBOL_CLASS (sym) = LOC_BLOCK;
e142c38c 15709 attr2 = dwarf2_attr (die, DW_AT_external, cu);
2cfa0c8d
JB
15710 if ((attr2 && (DW_UNSND (attr2) != 0))
15711 || cu->language == language_ada)
c906108c 15712 {
2cfa0c8d
JB
15713 /* Subprograms marked external are stored as a global symbol.
15714 Ada subprograms, whether marked external or not, are always
15715 stored as a global symbol, because we want to be able to
15716 access them globally. For instance, we want to be able
15717 to break on a nested subprogram without having to
15718 specify the context. */
e37fd15a 15719 list_to_add = &global_symbols;
c906108c
SS
15720 }
15721 else
15722 {
e37fd15a 15723 list_to_add = cu->list_in_scope;
c906108c
SS
15724 }
15725 break;
edb3359d
DJ
15726 case DW_TAG_inlined_subroutine:
15727 /* SYMBOL_BLOCK_VALUE (sym) will be filled in later by
15728 finish_block. */
15729 SYMBOL_CLASS (sym) = LOC_BLOCK;
15730 SYMBOL_INLINED (sym) = 1;
481860b3 15731 list_to_add = cu->list_in_scope;
edb3359d 15732 break;
34eaf542
TT
15733 case DW_TAG_template_value_param:
15734 suppress_add = 1;
15735 /* Fall through. */
72929c62 15736 case DW_TAG_constant:
c906108c 15737 case DW_TAG_variable:
254e6b9e 15738 case DW_TAG_member:
0963b4bd
MS
15739 /* Compilation with minimal debug info may result in
15740 variables with missing type entries. Change the
15741 misleading `void' type to something sensible. */
c906108c 15742 if (TYPE_CODE (SYMBOL_TYPE (sym)) == TYPE_CODE_VOID)
64c50499 15743 SYMBOL_TYPE (sym)
46bf5051 15744 = objfile_type (objfile)->nodebug_data_symbol;
64c50499 15745
e142c38c 15746 attr = dwarf2_attr (die, DW_AT_const_value, cu);
254e6b9e
DE
15747 /* In the case of DW_TAG_member, we should only be called for
15748 static const members. */
15749 if (die->tag == DW_TAG_member)
15750 {
3863f96c
DE
15751 /* dwarf2_add_field uses die_is_declaration,
15752 so we do the same. */
254e6b9e
DE
15753 gdb_assert (die_is_declaration (die, cu));
15754 gdb_assert (attr);
15755 }
c906108c
SS
15756 if (attr)
15757 {
e7c27a73 15758 dwarf2_const_value (attr, sym, cu);
e142c38c 15759 attr2 = dwarf2_attr (die, DW_AT_external, cu);
e37fd15a 15760 if (!suppress_add)
34eaf542
TT
15761 {
15762 if (attr2 && (DW_UNSND (attr2) != 0))
e37fd15a 15763 list_to_add = &global_symbols;
34eaf542 15764 else
e37fd15a 15765 list_to_add = cu->list_in_scope;
34eaf542 15766 }
c906108c
SS
15767 break;
15768 }
e142c38c 15769 attr = dwarf2_attr (die, DW_AT_location, cu);
c906108c
SS
15770 if (attr)
15771 {
e7c27a73 15772 var_decode_location (attr, sym, cu);
e142c38c 15773 attr2 = dwarf2_attr (die, DW_AT_external, cu);
4357ac6c
TT
15774
15775 /* Fortran explicitly imports any global symbols to the local
15776 scope by DW_TAG_common_block. */
15777 if (cu->language == language_fortran && die->parent
15778 && die->parent->tag == DW_TAG_common_block)
15779 attr2 = NULL;
15780
caac4577
JG
15781 if (SYMBOL_CLASS (sym) == LOC_STATIC
15782 && SYMBOL_VALUE_ADDRESS (sym) == 0
15783 && !dwarf2_per_objfile->has_section_at_zero)
15784 {
15785 /* When a static variable is eliminated by the linker,
15786 the corresponding debug information is not stripped
15787 out, but the variable address is set to null;
15788 do not add such variables into symbol table. */
15789 }
15790 else if (attr2 && (DW_UNSND (attr2) != 0))
1c809c68 15791 {
f55ee35c
JK
15792 /* Workaround gfortran PR debug/40040 - it uses
15793 DW_AT_location for variables in -fPIC libraries which may
15794 get overriden by other libraries/executable and get
15795 a different address. Resolve it by the minimal symbol
15796 which may come from inferior's executable using copy
15797 relocation. Make this workaround only for gfortran as for
15798 other compilers GDB cannot guess the minimal symbol
15799 Fortran mangling kind. */
15800 if (cu->language == language_fortran && die->parent
15801 && die->parent->tag == DW_TAG_module
15802 && cu->producer
15803 && strncmp (cu->producer, "GNU Fortran ", 12) == 0)
15804 SYMBOL_CLASS (sym) = LOC_UNRESOLVED;
15805
1c809c68
TT
15806 /* A variable with DW_AT_external is never static,
15807 but it may be block-scoped. */
15808 list_to_add = (cu->list_in_scope == &file_symbols
15809 ? &global_symbols : cu->list_in_scope);
1c809c68 15810 }
c906108c 15811 else
e37fd15a 15812 list_to_add = cu->list_in_scope;
c906108c
SS
15813 }
15814 else
15815 {
15816 /* We do not know the address of this symbol.
c5aa993b
JM
15817 If it is an external symbol and we have type information
15818 for it, enter the symbol as a LOC_UNRESOLVED symbol.
15819 The address of the variable will then be determined from
15820 the minimal symbol table whenever the variable is
15821 referenced. */
e142c38c 15822 attr2 = dwarf2_attr (die, DW_AT_external, cu);
0971de02
TT
15823
15824 /* Fortran explicitly imports any global symbols to the local
15825 scope by DW_TAG_common_block. */
15826 if (cu->language == language_fortran && die->parent
15827 && die->parent->tag == DW_TAG_common_block)
15828 {
15829 /* SYMBOL_CLASS doesn't matter here because
15830 read_common_block is going to reset it. */
15831 if (!suppress_add)
15832 list_to_add = cu->list_in_scope;
15833 }
15834 else if (attr2 && (DW_UNSND (attr2) != 0)
15835 && dwarf2_attr (die, DW_AT_type, cu) != NULL)
c906108c 15836 {
0fe7935b
DJ
15837 /* A variable with DW_AT_external is never static, but it
15838 may be block-scoped. */
15839 list_to_add = (cu->list_in_scope == &file_symbols
15840 ? &global_symbols : cu->list_in_scope);
15841
c906108c 15842 SYMBOL_CLASS (sym) = LOC_UNRESOLVED;
c906108c 15843 }
442ddf59
JK
15844 else if (!die_is_declaration (die, cu))
15845 {
15846 /* Use the default LOC_OPTIMIZED_OUT class. */
15847 gdb_assert (SYMBOL_CLASS (sym) == LOC_OPTIMIZED_OUT);
e37fd15a
SW
15848 if (!suppress_add)
15849 list_to_add = cu->list_in_scope;
442ddf59 15850 }
c906108c
SS
15851 }
15852 break;
15853 case DW_TAG_formal_parameter:
edb3359d
DJ
15854 /* If we are inside a function, mark this as an argument. If
15855 not, we might be looking at an argument to an inlined function
15856 when we do not have enough information to show inlined frames;
15857 pretend it's a local variable in that case so that the user can
15858 still see it. */
15859 if (context_stack_depth > 0
15860 && context_stack[context_stack_depth - 1].name != NULL)
15861 SYMBOL_IS_ARGUMENT (sym) = 1;
e142c38c 15862 attr = dwarf2_attr (die, DW_AT_location, cu);
c906108c
SS
15863 if (attr)
15864 {
e7c27a73 15865 var_decode_location (attr, sym, cu);
c906108c 15866 }
e142c38c 15867 attr = dwarf2_attr (die, DW_AT_const_value, cu);
c906108c
SS
15868 if (attr)
15869 {
e7c27a73 15870 dwarf2_const_value (attr, sym, cu);
c906108c 15871 }
f346a30d 15872
e37fd15a 15873 list_to_add = cu->list_in_scope;
c906108c
SS
15874 break;
15875 case DW_TAG_unspecified_parameters:
15876 /* From varargs functions; gdb doesn't seem to have any
15877 interest in this information, so just ignore it for now.
15878 (FIXME?) */
15879 break;
34eaf542
TT
15880 case DW_TAG_template_type_param:
15881 suppress_add = 1;
15882 /* Fall through. */
c906108c 15883 case DW_TAG_class_type:
680b30c7 15884 case DW_TAG_interface_type:
c906108c
SS
15885 case DW_TAG_structure_type:
15886 case DW_TAG_union_type:
72019c9c 15887 case DW_TAG_set_type:
c906108c
SS
15888 case DW_TAG_enumeration_type:
15889 SYMBOL_CLASS (sym) = LOC_TYPEDEF;
176620f1 15890 SYMBOL_DOMAIN (sym) = STRUCT_DOMAIN;
c906108c 15891
63d06c5c 15892 {
987504bb 15893 /* NOTE: carlton/2003-11-10: C++ and Java class symbols shouldn't
63d06c5c
DC
15894 really ever be static objects: otherwise, if you try
15895 to, say, break of a class's method and you're in a file
15896 which doesn't mention that class, it won't work unless
15897 the check for all static symbols in lookup_symbol_aux
15898 saves you. See the OtherFileClass tests in
15899 gdb.c++/namespace.exp. */
15900
e37fd15a 15901 if (!suppress_add)
34eaf542 15902 {
34eaf542
TT
15903 list_to_add = (cu->list_in_scope == &file_symbols
15904 && (cu->language == language_cplus
15905 || cu->language == language_java)
15906 ? &global_symbols : cu->list_in_scope);
63d06c5c 15907
64382290
TT
15908 /* The semantics of C++ state that "struct foo {
15909 ... }" also defines a typedef for "foo". A Java
15910 class declaration also defines a typedef for the
15911 class. */
15912 if (cu->language == language_cplus
15913 || cu->language == language_java
15914 || cu->language == language_ada)
15915 {
15916 /* The symbol's name is already allocated along
15917 with this objfile, so we don't need to
15918 duplicate it for the type. */
15919 if (TYPE_NAME (SYMBOL_TYPE (sym)) == 0)
15920 TYPE_NAME (SYMBOL_TYPE (sym)) = SYMBOL_SEARCH_NAME (sym);
15921 }
63d06c5c
DC
15922 }
15923 }
c906108c
SS
15924 break;
15925 case DW_TAG_typedef:
63d06c5c
DC
15926 SYMBOL_CLASS (sym) = LOC_TYPEDEF;
15927 SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
e37fd15a 15928 list_to_add = cu->list_in_scope;
63d06c5c 15929 break;
c906108c 15930 case DW_TAG_base_type:
a02abb62 15931 case DW_TAG_subrange_type:
c906108c 15932 SYMBOL_CLASS (sym) = LOC_TYPEDEF;
176620f1 15933 SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
e37fd15a 15934 list_to_add = cu->list_in_scope;
c906108c
SS
15935 break;
15936 case DW_TAG_enumerator:
e142c38c 15937 attr = dwarf2_attr (die, DW_AT_const_value, cu);
c906108c
SS
15938 if (attr)
15939 {
e7c27a73 15940 dwarf2_const_value (attr, sym, cu);
c906108c 15941 }
63d06c5c
DC
15942 {
15943 /* NOTE: carlton/2003-11-10: See comment above in the
15944 DW_TAG_class_type, etc. block. */
15945
e142c38c 15946 list_to_add = (cu->list_in_scope == &file_symbols
987504bb
JJ
15947 && (cu->language == language_cplus
15948 || cu->language == language_java)
e142c38c 15949 ? &global_symbols : cu->list_in_scope);
63d06c5c 15950 }
c906108c 15951 break;
5c4e30ca
DC
15952 case DW_TAG_namespace:
15953 SYMBOL_CLASS (sym) = LOC_TYPEDEF;
e37fd15a 15954 list_to_add = &global_symbols;
5c4e30ca 15955 break;
4357ac6c
TT
15956 case DW_TAG_common_block:
15957 SYMBOL_CLASS (sym) = LOC_STATIC;
15958 SYMBOL_DOMAIN (sym) = COMMON_BLOCK_DOMAIN;
15959 add_symbol_to_list (sym, cu->list_in_scope);
15960 break;
c906108c
SS
15961 default:
15962 /* Not a tag we recognize. Hopefully we aren't processing
15963 trash data, but since we must specifically ignore things
15964 we don't recognize, there is nothing else we should do at
0963b4bd 15965 this point. */
e2e0b3e5 15966 complaint (&symfile_complaints, _("unsupported tag: '%s'"),
4d3c2250 15967 dwarf_tag_name (die->tag));
c906108c
SS
15968 break;
15969 }
df8a16a1 15970
e37fd15a
SW
15971 if (suppress_add)
15972 {
15973 sym->hash_next = objfile->template_symbols;
15974 objfile->template_symbols = sym;
15975 list_to_add = NULL;
15976 }
15977
15978 if (list_to_add != NULL)
15979 add_symbol_to_list (sym, list_to_add);
15980
df8a16a1
DJ
15981 /* For the benefit of old versions of GCC, check for anonymous
15982 namespaces based on the demangled name. */
15983 if (!processing_has_namespace_info
94af9270 15984 && cu->language == language_cplus)
a10964d1 15985 cp_scan_for_anonymous_namespaces (sym, objfile);
c906108c
SS
15986 }
15987 return (sym);
15988}
15989
34eaf542
TT
15990/* A wrapper for new_symbol_full that always allocates a new symbol. */
15991
15992static struct symbol *
15993new_symbol (struct die_info *die, struct type *type, struct dwarf2_cu *cu)
15994{
15995 return new_symbol_full (die, type, cu, NULL);
15996}
15997
98bfdba5
PA
15998/* Given an attr with a DW_FORM_dataN value in host byte order,
15999 zero-extend it as appropriate for the symbol's type. The DWARF
16000 standard (v4) is not entirely clear about the meaning of using
16001 DW_FORM_dataN for a constant with a signed type, where the type is
16002 wider than the data. The conclusion of a discussion on the DWARF
16003 list was that this is unspecified. We choose to always zero-extend
16004 because that is the interpretation long in use by GCC. */
c906108c 16005
98bfdba5
PA
16006static gdb_byte *
16007dwarf2_const_value_data (struct attribute *attr, struct type *type,
16008 const char *name, struct obstack *obstack,
12df843f 16009 struct dwarf2_cu *cu, LONGEST *value, int bits)
c906108c 16010{
e7c27a73 16011 struct objfile *objfile = cu->objfile;
e17a4113
UW
16012 enum bfd_endian byte_order = bfd_big_endian (objfile->obfd) ?
16013 BFD_ENDIAN_BIG : BFD_ENDIAN_LITTLE;
98bfdba5
PA
16014 LONGEST l = DW_UNSND (attr);
16015
16016 if (bits < sizeof (*value) * 8)
16017 {
16018 l &= ((LONGEST) 1 << bits) - 1;
16019 *value = l;
16020 }
16021 else if (bits == sizeof (*value) * 8)
16022 *value = l;
16023 else
16024 {
16025 gdb_byte *bytes = obstack_alloc (obstack, bits / 8);
16026 store_unsigned_integer (bytes, bits / 8, byte_order, l);
16027 return bytes;
16028 }
16029
16030 return NULL;
16031}
16032
16033/* Read a constant value from an attribute. Either set *VALUE, or if
16034 the value does not fit in *VALUE, set *BYTES - either already
16035 allocated on the objfile obstack, or newly allocated on OBSTACK,
16036 or, set *BATON, if we translated the constant to a location
16037 expression. */
16038
16039static void
16040dwarf2_const_value_attr (struct attribute *attr, struct type *type,
16041 const char *name, struct obstack *obstack,
16042 struct dwarf2_cu *cu,
12df843f 16043 LONGEST *value, gdb_byte **bytes,
98bfdba5
PA
16044 struct dwarf2_locexpr_baton **baton)
16045{
16046 struct objfile *objfile = cu->objfile;
16047 struct comp_unit_head *cu_header = &cu->header;
c906108c 16048 struct dwarf_block *blk;
98bfdba5
PA
16049 enum bfd_endian byte_order = (bfd_big_endian (objfile->obfd) ?
16050 BFD_ENDIAN_BIG : BFD_ENDIAN_LITTLE);
16051
16052 *value = 0;
16053 *bytes = NULL;
16054 *baton = NULL;
c906108c
SS
16055
16056 switch (attr->form)
16057 {
16058 case DW_FORM_addr:
3019eac3 16059 case DW_FORM_GNU_addr_index:
ac56253d 16060 {
ac56253d
TT
16061 gdb_byte *data;
16062
98bfdba5
PA
16063 if (TYPE_LENGTH (type) != cu_header->addr_size)
16064 dwarf2_const_value_length_mismatch_complaint (name,
ac56253d 16065 cu_header->addr_size,
98bfdba5 16066 TYPE_LENGTH (type));
ac56253d
TT
16067 /* Symbols of this form are reasonably rare, so we just
16068 piggyback on the existing location code rather than writing
16069 a new implementation of symbol_computed_ops. */
98bfdba5
PA
16070 *baton = obstack_alloc (&objfile->objfile_obstack,
16071 sizeof (struct dwarf2_locexpr_baton));
16072 (*baton)->per_cu = cu->per_cu;
16073 gdb_assert ((*baton)->per_cu);
ac56253d 16074
98bfdba5
PA
16075 (*baton)->size = 2 + cu_header->addr_size;
16076 data = obstack_alloc (&objfile->objfile_obstack, (*baton)->size);
16077 (*baton)->data = data;
ac56253d
TT
16078
16079 data[0] = DW_OP_addr;
16080 store_unsigned_integer (&data[1], cu_header->addr_size,
16081 byte_order, DW_ADDR (attr));
16082 data[cu_header->addr_size + 1] = DW_OP_stack_value;
ac56253d 16083 }
c906108c 16084 break;
4ac36638 16085 case DW_FORM_string:
93b5768b 16086 case DW_FORM_strp:
3019eac3 16087 case DW_FORM_GNU_str_index:
36586728 16088 case DW_FORM_GNU_strp_alt:
98bfdba5
PA
16089 /* DW_STRING is already allocated on the objfile obstack, point
16090 directly to it. */
16091 *bytes = (gdb_byte *) DW_STRING (attr);
93b5768b 16092 break;
c906108c
SS
16093 case DW_FORM_block1:
16094 case DW_FORM_block2:
16095 case DW_FORM_block4:
16096 case DW_FORM_block:
2dc7f7b3 16097 case DW_FORM_exprloc:
c906108c 16098 blk = DW_BLOCK (attr);
98bfdba5
PA
16099 if (TYPE_LENGTH (type) != blk->size)
16100 dwarf2_const_value_length_mismatch_complaint (name, blk->size,
16101 TYPE_LENGTH (type));
16102 *bytes = blk->data;
c906108c 16103 break;
2df3850c
JM
16104
16105 /* The DW_AT_const_value attributes are supposed to carry the
16106 symbol's value "represented as it would be on the target
16107 architecture." By the time we get here, it's already been
16108 converted to host endianness, so we just need to sign- or
16109 zero-extend it as appropriate. */
16110 case DW_FORM_data1:
3e43a32a
MS
16111 *bytes = dwarf2_const_value_data (attr, type, name,
16112 obstack, cu, value, 8);
2df3850c 16113 break;
c906108c 16114 case DW_FORM_data2:
3e43a32a
MS
16115 *bytes = dwarf2_const_value_data (attr, type, name,
16116 obstack, cu, value, 16);
2df3850c 16117 break;
c906108c 16118 case DW_FORM_data4:
3e43a32a
MS
16119 *bytes = dwarf2_const_value_data (attr, type, name,
16120 obstack, cu, value, 32);
2df3850c 16121 break;
c906108c 16122 case DW_FORM_data8:
3e43a32a
MS
16123 *bytes = dwarf2_const_value_data (attr, type, name,
16124 obstack, cu, value, 64);
2df3850c
JM
16125 break;
16126
c906108c 16127 case DW_FORM_sdata:
98bfdba5 16128 *value = DW_SND (attr);
2df3850c
JM
16129 break;
16130
c906108c 16131 case DW_FORM_udata:
98bfdba5 16132 *value = DW_UNSND (attr);
c906108c 16133 break;
2df3850c 16134
c906108c 16135 default:
4d3c2250 16136 complaint (&symfile_complaints,
e2e0b3e5 16137 _("unsupported const value attribute form: '%s'"),
4d3c2250 16138 dwarf_form_name (attr->form));
98bfdba5 16139 *value = 0;
c906108c
SS
16140 break;
16141 }
16142}
16143
2df3850c 16144
98bfdba5
PA
16145/* Copy constant value from an attribute to a symbol. */
16146
2df3850c 16147static void
98bfdba5
PA
16148dwarf2_const_value (struct attribute *attr, struct symbol *sym,
16149 struct dwarf2_cu *cu)
2df3850c 16150{
98bfdba5
PA
16151 struct objfile *objfile = cu->objfile;
16152 struct comp_unit_head *cu_header = &cu->header;
12df843f 16153 LONGEST value;
98bfdba5
PA
16154 gdb_byte *bytes;
16155 struct dwarf2_locexpr_baton *baton;
2df3850c 16156
98bfdba5
PA
16157 dwarf2_const_value_attr (attr, SYMBOL_TYPE (sym),
16158 SYMBOL_PRINT_NAME (sym),
16159 &objfile->objfile_obstack, cu,
16160 &value, &bytes, &baton);
2df3850c 16161
98bfdba5
PA
16162 if (baton != NULL)
16163 {
16164 SYMBOL_COMPUTED_OPS (sym) = &dwarf2_locexpr_funcs;
16165 SYMBOL_LOCATION_BATON (sym) = baton;
16166 SYMBOL_CLASS (sym) = LOC_COMPUTED;
16167 }
16168 else if (bytes != NULL)
16169 {
16170 SYMBOL_VALUE_BYTES (sym) = bytes;
16171 SYMBOL_CLASS (sym) = LOC_CONST_BYTES;
16172 }
16173 else
16174 {
16175 SYMBOL_VALUE (sym) = value;
16176 SYMBOL_CLASS (sym) = LOC_CONST;
16177 }
2df3850c
JM
16178}
16179
c906108c
SS
16180/* Return the type of the die in question using its DW_AT_type attribute. */
16181
16182static struct type *
e7c27a73 16183die_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 16184{
c906108c 16185 struct attribute *type_attr;
c906108c 16186
e142c38c 16187 type_attr = dwarf2_attr (die, DW_AT_type, cu);
c906108c
SS
16188 if (!type_attr)
16189 {
16190 /* A missing DW_AT_type represents a void type. */
46bf5051 16191 return objfile_type (cu->objfile)->builtin_void;
c906108c 16192 }
348e048f 16193
673bfd45 16194 return lookup_die_type (die, type_attr, cu);
c906108c
SS
16195}
16196
b4ba55a1
JB
16197/* True iff CU's producer generates GNAT Ada auxiliary information
16198 that allows to find parallel types through that information instead
16199 of having to do expensive parallel lookups by type name. */
16200
16201static int
16202need_gnat_info (struct dwarf2_cu *cu)
16203{
16204 /* FIXME: brobecker/2010-10-12: As of now, only the AdaCore version
16205 of GNAT produces this auxiliary information, without any indication
16206 that it is produced. Part of enhancing the FSF version of GNAT
16207 to produce that information will be to put in place an indicator
16208 that we can use in order to determine whether the descriptive type
16209 info is available or not. One suggestion that has been made is
16210 to use a new attribute, attached to the CU die. For now, assume
16211 that the descriptive type info is not available. */
16212 return 0;
16213}
16214
b4ba55a1
JB
16215/* Return the auxiliary type of the die in question using its
16216 DW_AT_GNAT_descriptive_type attribute. Returns NULL if the
16217 attribute is not present. */
16218
16219static struct type *
16220die_descriptive_type (struct die_info *die, struct dwarf2_cu *cu)
16221{
b4ba55a1 16222 struct attribute *type_attr;
b4ba55a1
JB
16223
16224 type_attr = dwarf2_attr (die, DW_AT_GNAT_descriptive_type, cu);
16225 if (!type_attr)
16226 return NULL;
16227
673bfd45 16228 return lookup_die_type (die, type_attr, cu);
b4ba55a1
JB
16229}
16230
16231/* If DIE has a descriptive_type attribute, then set the TYPE's
16232 descriptive type accordingly. */
16233
16234static void
16235set_descriptive_type (struct type *type, struct die_info *die,
16236 struct dwarf2_cu *cu)
16237{
16238 struct type *descriptive_type = die_descriptive_type (die, cu);
16239
16240 if (descriptive_type)
16241 {
16242 ALLOCATE_GNAT_AUX_TYPE (type);
16243 TYPE_DESCRIPTIVE_TYPE (type) = descriptive_type;
16244 }
16245}
16246
c906108c
SS
16247/* Return the containing type of the die in question using its
16248 DW_AT_containing_type attribute. */
16249
16250static struct type *
e7c27a73 16251die_containing_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 16252{
c906108c 16253 struct attribute *type_attr;
c906108c 16254
e142c38c 16255 type_attr = dwarf2_attr (die, DW_AT_containing_type, cu);
33ac96f0
JK
16256 if (!type_attr)
16257 error (_("Dwarf Error: Problem turning containing type into gdb type "
16258 "[in module %s]"), cu->objfile->name);
16259
673bfd45 16260 return lookup_die_type (die, type_attr, cu);
c906108c
SS
16261}
16262
673bfd45
DE
16263/* Look up the type of DIE in CU using its type attribute ATTR.
16264 If there is no type substitute an error marker. */
16265
c906108c 16266static struct type *
673bfd45
DE
16267lookup_die_type (struct die_info *die, struct attribute *attr,
16268 struct dwarf2_cu *cu)
c906108c 16269{
bb5ed363 16270 struct objfile *objfile = cu->objfile;
f792889a
DJ
16271 struct type *this_type;
16272
673bfd45
DE
16273 /* First see if we have it cached. */
16274
36586728
TT
16275 if (attr->form == DW_FORM_GNU_ref_alt)
16276 {
16277 struct dwarf2_per_cu_data *per_cu;
16278 sect_offset offset = dwarf2_get_ref_die_offset (attr);
16279
16280 per_cu = dwarf2_find_containing_comp_unit (offset, 1, cu->objfile);
16281 this_type = get_die_type_at_offset (offset, per_cu);
16282 }
16283 else if (is_ref_attr (attr))
673bfd45 16284 {
b64f50a1 16285 sect_offset offset = dwarf2_get_ref_die_offset (attr);
673bfd45
DE
16286
16287 this_type = get_die_type_at_offset (offset, cu->per_cu);
16288 }
55f1336d 16289 else if (attr->form == DW_FORM_ref_sig8)
673bfd45
DE
16290 {
16291 struct signatured_type *sig_type = DW_SIGNATURED_TYPE (attr);
673bfd45
DE
16292
16293 /* sig_type will be NULL if the signatured type is missing from
16294 the debug info. */
16295 if (sig_type == NULL)
16296 error (_("Dwarf Error: Cannot find signatured DIE referenced from DIE "
16297 "at 0x%x [in module %s]"),
b64f50a1 16298 die->offset.sect_off, objfile->name);
673bfd45 16299
3019eac3
DE
16300 gdb_assert (sig_type->per_cu.is_debug_types);
16301 /* If we haven't filled in type_offset_in_section yet, then we
16302 haven't read the type in yet. */
16303 this_type = NULL;
16304 if (sig_type->type_offset_in_section.sect_off != 0)
16305 {
16306 this_type =
16307 get_die_type_at_offset (sig_type->type_offset_in_section,
16308 &sig_type->per_cu);
16309 }
673bfd45
DE
16310 }
16311 else
16312 {
16313 dump_die_for_error (die);
16314 error (_("Dwarf Error: Bad type attribute %s [in module %s]"),
bb5ed363 16315 dwarf_attr_name (attr->name), objfile->name);
673bfd45
DE
16316 }
16317
16318 /* If not cached we need to read it in. */
16319
16320 if (this_type == NULL)
16321 {
16322 struct die_info *type_die;
16323 struct dwarf2_cu *type_cu = cu;
16324
16325 type_die = follow_die_ref_or_sig (die, attr, &type_cu);
3019eac3
DE
16326 /* If we found the type now, it's probably because the type came
16327 from an inter-CU reference and the type's CU got expanded before
16328 ours. */
16329 this_type = get_die_type (type_die, type_cu);
16330 if (this_type == NULL)
16331 this_type = read_type_die_1 (type_die, type_cu);
673bfd45
DE
16332 }
16333
16334 /* If we still don't have a type use an error marker. */
16335
16336 if (this_type == NULL)
c906108c 16337 {
b00fdb78
TT
16338 char *message, *saved;
16339
16340 /* read_type_die already issued a complaint. */
16341 message = xstrprintf (_("<unknown type in %s, CU 0x%x, DIE 0x%x>"),
bb5ed363 16342 objfile->name,
b64f50a1
JK
16343 cu->header.offset.sect_off,
16344 die->offset.sect_off);
bb5ed363 16345 saved = obstack_copy0 (&objfile->objfile_obstack,
b00fdb78
TT
16346 message, strlen (message));
16347 xfree (message);
16348
bb5ed363 16349 this_type = init_type (TYPE_CODE_ERROR, 0, 0, saved, objfile);
c906108c 16350 }
673bfd45 16351
f792889a 16352 return this_type;
c906108c
SS
16353}
16354
673bfd45
DE
16355/* Return the type in DIE, CU.
16356 Returns NULL for invalid types.
16357
16358 This first does a lookup in the appropriate type_hash table,
16359 and only reads the die in if necessary.
16360
16361 NOTE: This can be called when reading in partial or full symbols. */
16362
f792889a 16363static struct type *
e7c27a73 16364read_type_die (struct die_info *die, struct dwarf2_cu *cu)
c906108c 16365{
f792889a
DJ
16366 struct type *this_type;
16367
16368 this_type = get_die_type (die, cu);
16369 if (this_type)
16370 return this_type;
16371
673bfd45
DE
16372 return read_type_die_1 (die, cu);
16373}
16374
16375/* Read the type in DIE, CU.
16376 Returns NULL for invalid types. */
16377
16378static struct type *
16379read_type_die_1 (struct die_info *die, struct dwarf2_cu *cu)
16380{
16381 struct type *this_type = NULL;
16382
c906108c
SS
16383 switch (die->tag)
16384 {
16385 case DW_TAG_class_type:
680b30c7 16386 case DW_TAG_interface_type:
c906108c
SS
16387 case DW_TAG_structure_type:
16388 case DW_TAG_union_type:
f792889a 16389 this_type = read_structure_type (die, cu);
c906108c
SS
16390 break;
16391 case DW_TAG_enumeration_type:
f792889a 16392 this_type = read_enumeration_type (die, cu);
c906108c
SS
16393 break;
16394 case DW_TAG_subprogram:
16395 case DW_TAG_subroutine_type:
edb3359d 16396 case DW_TAG_inlined_subroutine:
f792889a 16397 this_type = read_subroutine_type (die, cu);
c906108c
SS
16398 break;
16399 case DW_TAG_array_type:
f792889a 16400 this_type = read_array_type (die, cu);
c906108c 16401 break;
72019c9c 16402 case DW_TAG_set_type:
f792889a 16403 this_type = read_set_type (die, cu);
72019c9c 16404 break;
c906108c 16405 case DW_TAG_pointer_type:
f792889a 16406 this_type = read_tag_pointer_type (die, cu);
c906108c
SS
16407 break;
16408 case DW_TAG_ptr_to_member_type:
f792889a 16409 this_type = read_tag_ptr_to_member_type (die, cu);
c906108c
SS
16410 break;
16411 case DW_TAG_reference_type:
f792889a 16412 this_type = read_tag_reference_type (die, cu);
c906108c
SS
16413 break;
16414 case DW_TAG_const_type:
f792889a 16415 this_type = read_tag_const_type (die, cu);
c906108c
SS
16416 break;
16417 case DW_TAG_volatile_type:
f792889a 16418 this_type = read_tag_volatile_type (die, cu);
c906108c
SS
16419 break;
16420 case DW_TAG_string_type:
f792889a 16421 this_type = read_tag_string_type (die, cu);
c906108c
SS
16422 break;
16423 case DW_TAG_typedef:
f792889a 16424 this_type = read_typedef (die, cu);
c906108c 16425 break;
a02abb62 16426 case DW_TAG_subrange_type:
f792889a 16427 this_type = read_subrange_type (die, cu);
a02abb62 16428 break;
c906108c 16429 case DW_TAG_base_type:
f792889a 16430 this_type = read_base_type (die, cu);
c906108c 16431 break;
81a17f79 16432 case DW_TAG_unspecified_type:
f792889a 16433 this_type = read_unspecified_type (die, cu);
81a17f79 16434 break;
0114d602
DJ
16435 case DW_TAG_namespace:
16436 this_type = read_namespace_type (die, cu);
16437 break;
f55ee35c
JK
16438 case DW_TAG_module:
16439 this_type = read_module_type (die, cu);
16440 break;
c906108c 16441 default:
3e43a32a
MS
16442 complaint (&symfile_complaints,
16443 _("unexpected tag in read_type_die: '%s'"),
4d3c2250 16444 dwarf_tag_name (die->tag));
c906108c
SS
16445 break;
16446 }
63d06c5c 16447
f792889a 16448 return this_type;
63d06c5c
DC
16449}
16450
abc72ce4
DE
16451/* See if we can figure out if the class lives in a namespace. We do
16452 this by looking for a member function; its demangled name will
16453 contain namespace info, if there is any.
16454 Return the computed name or NULL.
16455 Space for the result is allocated on the objfile's obstack.
16456 This is the full-die version of guess_partial_die_structure_name.
16457 In this case we know DIE has no useful parent. */
16458
16459static char *
16460guess_full_die_structure_name (struct die_info *die, struct dwarf2_cu *cu)
16461{
16462 struct die_info *spec_die;
16463 struct dwarf2_cu *spec_cu;
16464 struct die_info *child;
16465
16466 spec_cu = cu;
16467 spec_die = die_specification (die, &spec_cu);
16468 if (spec_die != NULL)
16469 {
16470 die = spec_die;
16471 cu = spec_cu;
16472 }
16473
16474 for (child = die->child;
16475 child != NULL;
16476 child = child->sibling)
16477 {
16478 if (child->tag == DW_TAG_subprogram)
16479 {
16480 struct attribute *attr;
16481
16482 attr = dwarf2_attr (child, DW_AT_linkage_name, cu);
16483 if (attr == NULL)
16484 attr = dwarf2_attr (child, DW_AT_MIPS_linkage_name, cu);
16485 if (attr != NULL)
16486 {
16487 char *actual_name
16488 = language_class_name_from_physname (cu->language_defn,
16489 DW_STRING (attr));
16490 char *name = NULL;
16491
16492 if (actual_name != NULL)
16493 {
16494 char *die_name = dwarf2_name (die, cu);
16495
16496 if (die_name != NULL
16497 && strcmp (die_name, actual_name) != 0)
16498 {
16499 /* Strip off the class name from the full name.
16500 We want the prefix. */
16501 int die_name_len = strlen (die_name);
16502 int actual_name_len = strlen (actual_name);
16503
16504 /* Test for '::' as a sanity check. */
16505 if (actual_name_len > die_name_len + 2
3e43a32a
MS
16506 && actual_name[actual_name_len
16507 - die_name_len - 1] == ':')
abc72ce4
DE
16508 name =
16509 obsavestring (actual_name,
16510 actual_name_len - die_name_len - 2,
16511 &cu->objfile->objfile_obstack);
16512 }
16513 }
16514 xfree (actual_name);
16515 return name;
16516 }
16517 }
16518 }
16519
16520 return NULL;
16521}
16522
96408a79
SA
16523/* GCC might emit a nameless typedef that has a linkage name. Determine the
16524 prefix part in such case. See
16525 http://gcc.gnu.org/bugzilla/show_bug.cgi?id=47510. */
16526
16527static char *
16528anonymous_struct_prefix (struct die_info *die, struct dwarf2_cu *cu)
16529{
16530 struct attribute *attr;
16531 char *base;
16532
16533 if (die->tag != DW_TAG_class_type && die->tag != DW_TAG_interface_type
16534 && die->tag != DW_TAG_structure_type && die->tag != DW_TAG_union_type)
16535 return NULL;
16536
16537 attr = dwarf2_attr (die, DW_AT_name, cu);
16538 if (attr != NULL && DW_STRING (attr) != NULL)
16539 return NULL;
16540
16541 attr = dwarf2_attr (die, DW_AT_linkage_name, cu);
16542 if (attr == NULL)
16543 attr = dwarf2_attr (die, DW_AT_MIPS_linkage_name, cu);
16544 if (attr == NULL || DW_STRING (attr) == NULL)
16545 return NULL;
16546
16547 /* dwarf2_name had to be already called. */
16548 gdb_assert (DW_STRING_IS_CANONICAL (attr));
16549
16550 /* Strip the base name, keep any leading namespaces/classes. */
16551 base = strrchr (DW_STRING (attr), ':');
16552 if (base == NULL || base == DW_STRING (attr) || base[-1] != ':')
16553 return "";
16554
16555 return obsavestring (DW_STRING (attr), &base[-1] - DW_STRING (attr),
16556 &cu->objfile->objfile_obstack);
16557}
16558
fdde2d81 16559/* Return the name of the namespace/class that DIE is defined within,
0114d602 16560 or "" if we can't tell. The caller should not xfree the result.
fdde2d81 16561
0114d602
DJ
16562 For example, if we're within the method foo() in the following
16563 code:
16564
16565 namespace N {
16566 class C {
16567 void foo () {
16568 }
16569 };
16570 }
16571
16572 then determine_prefix on foo's die will return "N::C". */
fdde2d81 16573
0d5cff50 16574static const char *
e142c38c 16575determine_prefix (struct die_info *die, struct dwarf2_cu *cu)
63d06c5c 16576{
0114d602
DJ
16577 struct die_info *parent, *spec_die;
16578 struct dwarf2_cu *spec_cu;
16579 struct type *parent_type;
96408a79 16580 char *retval;
63d06c5c 16581
f55ee35c
JK
16582 if (cu->language != language_cplus && cu->language != language_java
16583 && cu->language != language_fortran)
0114d602
DJ
16584 return "";
16585
96408a79
SA
16586 retval = anonymous_struct_prefix (die, cu);
16587 if (retval)
16588 return retval;
16589
0114d602
DJ
16590 /* We have to be careful in the presence of DW_AT_specification.
16591 For example, with GCC 3.4, given the code
16592
16593 namespace N {
16594 void foo() {
16595 // Definition of N::foo.
16596 }
16597 }
16598
16599 then we'll have a tree of DIEs like this:
16600
16601 1: DW_TAG_compile_unit
16602 2: DW_TAG_namespace // N
16603 3: DW_TAG_subprogram // declaration of N::foo
16604 4: DW_TAG_subprogram // definition of N::foo
16605 DW_AT_specification // refers to die #3
16606
16607 Thus, when processing die #4, we have to pretend that we're in
16608 the context of its DW_AT_specification, namely the contex of die
16609 #3. */
16610 spec_cu = cu;
16611 spec_die = die_specification (die, &spec_cu);
16612 if (spec_die == NULL)
16613 parent = die->parent;
16614 else
63d06c5c 16615 {
0114d602
DJ
16616 parent = spec_die->parent;
16617 cu = spec_cu;
63d06c5c 16618 }
0114d602
DJ
16619
16620 if (parent == NULL)
16621 return "";
98bfdba5
PA
16622 else if (parent->building_fullname)
16623 {
16624 const char *name;
16625 const char *parent_name;
16626
16627 /* It has been seen on RealView 2.2 built binaries,
16628 DW_TAG_template_type_param types actually _defined_ as
16629 children of the parent class:
16630
16631 enum E {};
16632 template class <class Enum> Class{};
16633 Class<enum E> class_e;
16634
16635 1: DW_TAG_class_type (Class)
16636 2: DW_TAG_enumeration_type (E)
16637 3: DW_TAG_enumerator (enum1:0)
16638 3: DW_TAG_enumerator (enum2:1)
16639 ...
16640 2: DW_TAG_template_type_param
16641 DW_AT_type DW_FORM_ref_udata (E)
16642
16643 Besides being broken debug info, it can put GDB into an
16644 infinite loop. Consider:
16645
16646 When we're building the full name for Class<E>, we'll start
16647 at Class, and go look over its template type parameters,
16648 finding E. We'll then try to build the full name of E, and
16649 reach here. We're now trying to build the full name of E,
16650 and look over the parent DIE for containing scope. In the
16651 broken case, if we followed the parent DIE of E, we'd again
16652 find Class, and once again go look at its template type
16653 arguments, etc., etc. Simply don't consider such parent die
16654 as source-level parent of this die (it can't be, the language
16655 doesn't allow it), and break the loop here. */
16656 name = dwarf2_name (die, cu);
16657 parent_name = dwarf2_name (parent, cu);
16658 complaint (&symfile_complaints,
16659 _("template param type '%s' defined within parent '%s'"),
16660 name ? name : "<unknown>",
16661 parent_name ? parent_name : "<unknown>");
16662 return "";
16663 }
63d06c5c 16664 else
0114d602
DJ
16665 switch (parent->tag)
16666 {
63d06c5c 16667 case DW_TAG_namespace:
0114d602 16668 parent_type = read_type_die (parent, cu);
acebe513
UW
16669 /* GCC 4.0 and 4.1 had a bug (PR c++/28460) where they generated bogus
16670 DW_TAG_namespace DIEs with a name of "::" for the global namespace.
16671 Work around this problem here. */
16672 if (cu->language == language_cplus
16673 && strcmp (TYPE_TAG_NAME (parent_type), "::") == 0)
16674 return "";
0114d602
DJ
16675 /* We give a name to even anonymous namespaces. */
16676 return TYPE_TAG_NAME (parent_type);
63d06c5c 16677 case DW_TAG_class_type:
680b30c7 16678 case DW_TAG_interface_type:
63d06c5c 16679 case DW_TAG_structure_type:
0114d602 16680 case DW_TAG_union_type:
f55ee35c 16681 case DW_TAG_module:
0114d602
DJ
16682 parent_type = read_type_die (parent, cu);
16683 if (TYPE_TAG_NAME (parent_type) != NULL)
16684 return TYPE_TAG_NAME (parent_type);
16685 else
16686 /* An anonymous structure is only allowed non-static data
16687 members; no typedefs, no member functions, et cetera.
16688 So it does not need a prefix. */
16689 return "";
abc72ce4 16690 case DW_TAG_compile_unit:
95554aad 16691 case DW_TAG_partial_unit:
abc72ce4
DE
16692 /* gcc-4.5 -gdwarf-4 can drop the enclosing namespace. Cope. */
16693 if (cu->language == language_cplus
8b70b953 16694 && !VEC_empty (dwarf2_section_info_def, dwarf2_per_objfile->types)
abc72ce4
DE
16695 && die->child != NULL
16696 && (die->tag == DW_TAG_class_type
16697 || die->tag == DW_TAG_structure_type
16698 || die->tag == DW_TAG_union_type))
16699 {
16700 char *name = guess_full_die_structure_name (die, cu);
16701 if (name != NULL)
16702 return name;
16703 }
16704 return "";
63d06c5c 16705 default:
8176b9b8 16706 return determine_prefix (parent, cu);
63d06c5c 16707 }
63d06c5c
DC
16708}
16709
3e43a32a
MS
16710/* Return a newly-allocated string formed by concatenating PREFIX and SUFFIX
16711 with appropriate separator. If PREFIX or SUFFIX is NULL or empty, then
16712 simply copy the SUFFIX or PREFIX, respectively. If OBS is non-null, perform
16713 an obconcat, otherwise allocate storage for the result. The CU argument is
16714 used to determine the language and hence, the appropriate separator. */
987504bb 16715
f55ee35c 16716#define MAX_SEP_LEN 7 /* strlen ("__") + strlen ("_MOD_") */
63d06c5c
DC
16717
16718static char *
f55ee35c
JK
16719typename_concat (struct obstack *obs, const char *prefix, const char *suffix,
16720 int physname, struct dwarf2_cu *cu)
63d06c5c 16721{
f55ee35c 16722 const char *lead = "";
5c315b68 16723 const char *sep;
63d06c5c 16724
3e43a32a
MS
16725 if (suffix == NULL || suffix[0] == '\0'
16726 || prefix == NULL || prefix[0] == '\0')
987504bb
JJ
16727 sep = "";
16728 else if (cu->language == language_java)
16729 sep = ".";
f55ee35c
JK
16730 else if (cu->language == language_fortran && physname)
16731 {
16732 /* This is gfortran specific mangling. Normally DW_AT_linkage_name or
16733 DW_AT_MIPS_linkage_name is preferred and used instead. */
16734
16735 lead = "__";
16736 sep = "_MOD_";
16737 }
987504bb
JJ
16738 else
16739 sep = "::";
63d06c5c 16740
6dd47d34
DE
16741 if (prefix == NULL)
16742 prefix = "";
16743 if (suffix == NULL)
16744 suffix = "";
16745
987504bb
JJ
16746 if (obs == NULL)
16747 {
3e43a32a
MS
16748 char *retval
16749 = xmalloc (strlen (prefix) + MAX_SEP_LEN + strlen (suffix) + 1);
9a619af0 16750
f55ee35c
JK
16751 strcpy (retval, lead);
16752 strcat (retval, prefix);
6dd47d34
DE
16753 strcat (retval, sep);
16754 strcat (retval, suffix);
63d06c5c
DC
16755 return retval;
16756 }
987504bb
JJ
16757 else
16758 {
16759 /* We have an obstack. */
f55ee35c 16760 return obconcat (obs, lead, prefix, sep, suffix, (char *) NULL);
987504bb 16761 }
63d06c5c
DC
16762}
16763
c906108c
SS
16764/* Return sibling of die, NULL if no sibling. */
16765
f9aca02d 16766static struct die_info *
fba45db2 16767sibling_die (struct die_info *die)
c906108c 16768{
639d11d3 16769 return die->sibling;
c906108c
SS
16770}
16771
71c25dea
TT
16772/* Get name of a die, return NULL if not found. */
16773
16774static char *
16775dwarf2_canonicalize_name (char *name, struct dwarf2_cu *cu,
16776 struct obstack *obstack)
16777{
16778 if (name && cu->language == language_cplus)
16779 {
16780 char *canon_name = cp_canonicalize_string (name);
16781
16782 if (canon_name != NULL)
16783 {
16784 if (strcmp (canon_name, name) != 0)
16785 name = obsavestring (canon_name, strlen (canon_name),
16786 obstack);
16787 xfree (canon_name);
16788 }
16789 }
16790
16791 return name;
c906108c
SS
16792}
16793
9219021c
DC
16794/* Get name of a die, return NULL if not found. */
16795
16796static char *
e142c38c 16797dwarf2_name (struct die_info *die, struct dwarf2_cu *cu)
9219021c
DC
16798{
16799 struct attribute *attr;
16800
e142c38c 16801 attr = dwarf2_attr (die, DW_AT_name, cu);
53832f31
TT
16802 if ((!attr || !DW_STRING (attr))
16803 && die->tag != DW_TAG_class_type
16804 && die->tag != DW_TAG_interface_type
16805 && die->tag != DW_TAG_structure_type
16806 && die->tag != DW_TAG_union_type)
71c25dea
TT
16807 return NULL;
16808
16809 switch (die->tag)
16810 {
16811 case DW_TAG_compile_unit:
95554aad 16812 case DW_TAG_partial_unit:
71c25dea
TT
16813 /* Compilation units have a DW_AT_name that is a filename, not
16814 a source language identifier. */
16815 case DW_TAG_enumeration_type:
16816 case DW_TAG_enumerator:
16817 /* These tags always have simple identifiers already; no need
16818 to canonicalize them. */
16819 return DW_STRING (attr);
907af001 16820
418835cc
KS
16821 case DW_TAG_subprogram:
16822 /* Java constructors will all be named "<init>", so return
16823 the class name when we see this special case. */
16824 if (cu->language == language_java
16825 && DW_STRING (attr) != NULL
16826 && strcmp (DW_STRING (attr), "<init>") == 0)
16827 {
16828 struct dwarf2_cu *spec_cu = cu;
16829 struct die_info *spec_die;
16830
16831 /* GCJ will output '<init>' for Java constructor names.
16832 For this special case, return the name of the parent class. */
16833
16834 /* GCJ may output suprogram DIEs with AT_specification set.
16835 If so, use the name of the specified DIE. */
16836 spec_die = die_specification (die, &spec_cu);
16837 if (spec_die != NULL)
16838 return dwarf2_name (spec_die, spec_cu);
16839
16840 do
16841 {
16842 die = die->parent;
16843 if (die->tag == DW_TAG_class_type)
16844 return dwarf2_name (die, cu);
16845 }
95554aad
TT
16846 while (die->tag != DW_TAG_compile_unit
16847 && die->tag != DW_TAG_partial_unit);
418835cc 16848 }
907af001
UW
16849 break;
16850
16851 case DW_TAG_class_type:
16852 case DW_TAG_interface_type:
16853 case DW_TAG_structure_type:
16854 case DW_TAG_union_type:
16855 /* Some GCC versions emit spurious DW_AT_name attributes for unnamed
16856 structures or unions. These were of the form "._%d" in GCC 4.1,
16857 or simply "<anonymous struct>" or "<anonymous union>" in GCC 4.3
16858 and GCC 4.4. We work around this problem by ignoring these. */
53832f31
TT
16859 if (attr && DW_STRING (attr)
16860 && (strncmp (DW_STRING (attr), "._", 2) == 0
16861 || strncmp (DW_STRING (attr), "<anonymous", 10) == 0))
907af001 16862 return NULL;
53832f31
TT
16863
16864 /* GCC might emit a nameless typedef that has a linkage name. See
16865 http://gcc.gnu.org/bugzilla/show_bug.cgi?id=47510. */
16866 if (!attr || DW_STRING (attr) == NULL)
16867 {
df5c6c50 16868 char *demangled = NULL;
53832f31
TT
16869
16870 attr = dwarf2_attr (die, DW_AT_linkage_name, cu);
16871 if (attr == NULL)
16872 attr = dwarf2_attr (die, DW_AT_MIPS_linkage_name, cu);
16873
16874 if (attr == NULL || DW_STRING (attr) == NULL)
16875 return NULL;
16876
df5c6c50
JK
16877 /* Avoid demangling DW_STRING (attr) the second time on a second
16878 call for the same DIE. */
16879 if (!DW_STRING_IS_CANONICAL (attr))
16880 demangled = cplus_demangle (DW_STRING (attr), DMGL_TYPES);
53832f31
TT
16881
16882 if (demangled)
16883 {
96408a79
SA
16884 char *base;
16885
53832f31 16886 /* FIXME: we already did this for the partial symbol... */
96408a79
SA
16887 DW_STRING (attr) = obsavestring (demangled, strlen (demangled),
16888 &cu->objfile->objfile_obstack);
53832f31
TT
16889 DW_STRING_IS_CANONICAL (attr) = 1;
16890 xfree (demangled);
96408a79
SA
16891
16892 /* Strip any leading namespaces/classes, keep only the base name.
16893 DW_AT_name for named DIEs does not contain the prefixes. */
16894 base = strrchr (DW_STRING (attr), ':');
16895 if (base && base > DW_STRING (attr) && base[-1] == ':')
16896 return &base[1];
16897 else
16898 return DW_STRING (attr);
53832f31
TT
16899 }
16900 }
907af001
UW
16901 break;
16902
71c25dea 16903 default:
907af001
UW
16904 break;
16905 }
16906
16907 if (!DW_STRING_IS_CANONICAL (attr))
16908 {
16909 DW_STRING (attr)
16910 = dwarf2_canonicalize_name (DW_STRING (attr), cu,
16911 &cu->objfile->objfile_obstack);
16912 DW_STRING_IS_CANONICAL (attr) = 1;
71c25dea 16913 }
907af001 16914 return DW_STRING (attr);
9219021c
DC
16915}
16916
16917/* Return the die that this die in an extension of, or NULL if there
f2f0e013
DJ
16918 is none. *EXT_CU is the CU containing DIE on input, and the CU
16919 containing the return value on output. */
9219021c
DC
16920
16921static struct die_info *
f2f0e013 16922dwarf2_extension (struct die_info *die, struct dwarf2_cu **ext_cu)
9219021c
DC
16923{
16924 struct attribute *attr;
9219021c 16925
f2f0e013 16926 attr = dwarf2_attr (die, DW_AT_extension, *ext_cu);
9219021c
DC
16927 if (attr == NULL)
16928 return NULL;
16929
f2f0e013 16930 return follow_die_ref (die, attr, ext_cu);
9219021c
DC
16931}
16932
c906108c
SS
16933/* Convert a DIE tag into its string name. */
16934
f39c6ffd 16935static const char *
aa1ee363 16936dwarf_tag_name (unsigned tag)
c906108c 16937{
f39c6ffd
TT
16938 const char *name = get_DW_TAG_name (tag);
16939
16940 if (name == NULL)
16941 return "DW_TAG_<unknown>";
16942
16943 return name;
c906108c
SS
16944}
16945
16946/* Convert a DWARF attribute code into its string name. */
16947
f39c6ffd 16948static const char *
aa1ee363 16949dwarf_attr_name (unsigned attr)
c906108c 16950{
f39c6ffd
TT
16951 const char *name;
16952
c764a876 16953#ifdef MIPS /* collides with DW_AT_HP_block_index */
f39c6ffd
TT
16954 if (attr == DW_AT_MIPS_fde)
16955 return "DW_AT_MIPS_fde";
16956#else
16957 if (attr == DW_AT_HP_block_index)
16958 return "DW_AT_HP_block_index";
c764a876 16959#endif
f39c6ffd
TT
16960
16961 name = get_DW_AT_name (attr);
16962
16963 if (name == NULL)
16964 return "DW_AT_<unknown>";
16965
16966 return name;
c906108c
SS
16967}
16968
16969/* Convert a DWARF value form code into its string name. */
16970
f39c6ffd 16971static const char *
aa1ee363 16972dwarf_form_name (unsigned form)
c906108c 16973{
f39c6ffd
TT
16974 const char *name = get_DW_FORM_name (form);
16975
16976 if (name == NULL)
16977 return "DW_FORM_<unknown>";
16978
16979 return name;
c906108c
SS
16980}
16981
16982static char *
fba45db2 16983dwarf_bool_name (unsigned mybool)
c906108c
SS
16984{
16985 if (mybool)
16986 return "TRUE";
16987 else
16988 return "FALSE";
16989}
16990
16991/* Convert a DWARF type code into its string name. */
16992
f39c6ffd 16993static const char *
aa1ee363 16994dwarf_type_encoding_name (unsigned enc)
c906108c 16995{
f39c6ffd 16996 const char *name = get_DW_ATE_name (enc);
c906108c 16997
f39c6ffd
TT
16998 if (name == NULL)
16999 return "DW_ATE_<unknown>";
c906108c 17000
f39c6ffd 17001 return name;
c906108c 17002}
c906108c 17003
f9aca02d 17004static void
d97bc12b 17005dump_die_shallow (struct ui_file *f, int indent, struct die_info *die)
c906108c
SS
17006{
17007 unsigned int i;
17008
d97bc12b
DE
17009 print_spaces (indent, f);
17010 fprintf_unfiltered (f, "Die: %s (abbrev %d, offset 0x%x)\n",
b64f50a1 17011 dwarf_tag_name (die->tag), die->abbrev, die->offset.sect_off);
d97bc12b
DE
17012
17013 if (die->parent != NULL)
17014 {
17015 print_spaces (indent, f);
17016 fprintf_unfiltered (f, " parent at offset: 0x%x\n",
b64f50a1 17017 die->parent->offset.sect_off);
d97bc12b
DE
17018 }
17019
17020 print_spaces (indent, f);
17021 fprintf_unfiltered (f, " has children: %s\n",
639d11d3 17022 dwarf_bool_name (die->child != NULL));
c906108c 17023
d97bc12b
DE
17024 print_spaces (indent, f);
17025 fprintf_unfiltered (f, " attributes:\n");
17026
c906108c
SS
17027 for (i = 0; i < die->num_attrs; ++i)
17028 {
d97bc12b
DE
17029 print_spaces (indent, f);
17030 fprintf_unfiltered (f, " %s (%s) ",
c906108c
SS
17031 dwarf_attr_name (die->attrs[i].name),
17032 dwarf_form_name (die->attrs[i].form));
d97bc12b 17033
c906108c
SS
17034 switch (die->attrs[i].form)
17035 {
c906108c 17036 case DW_FORM_addr:
3019eac3 17037 case DW_FORM_GNU_addr_index:
d97bc12b 17038 fprintf_unfiltered (f, "address: ");
5af949e3 17039 fputs_filtered (hex_string (DW_ADDR (&die->attrs[i])), f);
c906108c
SS
17040 break;
17041 case DW_FORM_block2:
17042 case DW_FORM_block4:
17043 case DW_FORM_block:
17044 case DW_FORM_block1:
56eb65bd
SP
17045 fprintf_unfiltered (f, "block: size %s",
17046 pulongest (DW_BLOCK (&die->attrs[i])->size));
c906108c 17047 break;
2dc7f7b3 17048 case DW_FORM_exprloc:
56eb65bd
SP
17049 fprintf_unfiltered (f, "expression: size %s",
17050 pulongest (DW_BLOCK (&die->attrs[i])->size));
2dc7f7b3 17051 break;
4568ecf9
DE
17052 case DW_FORM_ref_addr:
17053 fprintf_unfiltered (f, "ref address: ");
17054 fputs_filtered (hex_string (DW_UNSND (&die->attrs[i])), f);
17055 break;
36586728
TT
17056 case DW_FORM_GNU_ref_alt:
17057 fprintf_unfiltered (f, "alt ref address: ");
17058 fputs_filtered (hex_string (DW_UNSND (&die->attrs[i])), f);
17059 break;
10b3939b
DJ
17060 case DW_FORM_ref1:
17061 case DW_FORM_ref2:
17062 case DW_FORM_ref4:
4568ecf9
DE
17063 case DW_FORM_ref8:
17064 case DW_FORM_ref_udata:
d97bc12b 17065 fprintf_unfiltered (f, "constant ref: 0x%lx (adjusted)",
4568ecf9 17066 (long) (DW_UNSND (&die->attrs[i])));
10b3939b 17067 break;
c906108c
SS
17068 case DW_FORM_data1:
17069 case DW_FORM_data2:
17070 case DW_FORM_data4:
ce5d95e1 17071 case DW_FORM_data8:
c906108c
SS
17072 case DW_FORM_udata:
17073 case DW_FORM_sdata:
43bbcdc2
PH
17074 fprintf_unfiltered (f, "constant: %s",
17075 pulongest (DW_UNSND (&die->attrs[i])));
c906108c 17076 break;
2dc7f7b3
TT
17077 case DW_FORM_sec_offset:
17078 fprintf_unfiltered (f, "section offset: %s",
17079 pulongest (DW_UNSND (&die->attrs[i])));
17080 break;
55f1336d 17081 case DW_FORM_ref_sig8:
348e048f
DE
17082 if (DW_SIGNATURED_TYPE (&die->attrs[i]) != NULL)
17083 fprintf_unfiltered (f, "signatured type, offset: 0x%x",
b64f50a1 17084 DW_SIGNATURED_TYPE (&die->attrs[i])->per_cu.offset.sect_off);
348e048f
DE
17085 else
17086 fprintf_unfiltered (f, "signatured type, offset: unknown");
17087 break;
c906108c 17088 case DW_FORM_string:
4bdf3d34 17089 case DW_FORM_strp:
3019eac3 17090 case DW_FORM_GNU_str_index:
36586728 17091 case DW_FORM_GNU_strp_alt:
8285870a 17092 fprintf_unfiltered (f, "string: \"%s\" (%s canonicalized)",
c906108c 17093 DW_STRING (&die->attrs[i])
8285870a
JK
17094 ? DW_STRING (&die->attrs[i]) : "",
17095 DW_STRING_IS_CANONICAL (&die->attrs[i]) ? "is" : "not");
c906108c
SS
17096 break;
17097 case DW_FORM_flag:
17098 if (DW_UNSND (&die->attrs[i]))
d97bc12b 17099 fprintf_unfiltered (f, "flag: TRUE");
c906108c 17100 else
d97bc12b 17101 fprintf_unfiltered (f, "flag: FALSE");
c906108c 17102 break;
2dc7f7b3
TT
17103 case DW_FORM_flag_present:
17104 fprintf_unfiltered (f, "flag: TRUE");
17105 break;
a8329558 17106 case DW_FORM_indirect:
0963b4bd
MS
17107 /* The reader will have reduced the indirect form to
17108 the "base form" so this form should not occur. */
3e43a32a
MS
17109 fprintf_unfiltered (f,
17110 "unexpected attribute form: DW_FORM_indirect");
a8329558 17111 break;
c906108c 17112 default:
d97bc12b 17113 fprintf_unfiltered (f, "unsupported attribute form: %d.",
c5aa993b 17114 die->attrs[i].form);
d97bc12b 17115 break;
c906108c 17116 }
d97bc12b 17117 fprintf_unfiltered (f, "\n");
c906108c
SS
17118 }
17119}
17120
f9aca02d 17121static void
d97bc12b 17122dump_die_for_error (struct die_info *die)
c906108c 17123{
d97bc12b
DE
17124 dump_die_shallow (gdb_stderr, 0, die);
17125}
17126
17127static void
17128dump_die_1 (struct ui_file *f, int level, int max_level, struct die_info *die)
17129{
17130 int indent = level * 4;
17131
17132 gdb_assert (die != NULL);
17133
17134 if (level >= max_level)
17135 return;
17136
17137 dump_die_shallow (f, indent, die);
17138
17139 if (die->child != NULL)
c906108c 17140 {
d97bc12b
DE
17141 print_spaces (indent, f);
17142 fprintf_unfiltered (f, " Children:");
17143 if (level + 1 < max_level)
17144 {
17145 fprintf_unfiltered (f, "\n");
17146 dump_die_1 (f, level + 1, max_level, die->child);
17147 }
17148 else
17149 {
3e43a32a
MS
17150 fprintf_unfiltered (f,
17151 " [not printed, max nesting level reached]\n");
d97bc12b
DE
17152 }
17153 }
17154
17155 if (die->sibling != NULL && level > 0)
17156 {
17157 dump_die_1 (f, level, max_level, die->sibling);
c906108c
SS
17158 }
17159}
17160
d97bc12b
DE
17161/* This is called from the pdie macro in gdbinit.in.
17162 It's not static so gcc will keep a copy callable from gdb. */
17163
17164void
17165dump_die (struct die_info *die, int max_level)
17166{
17167 dump_die_1 (gdb_stdlog, 0, max_level, die);
17168}
17169
f9aca02d 17170static void
51545339 17171store_in_ref_table (struct die_info *die, struct dwarf2_cu *cu)
c906108c 17172{
51545339 17173 void **slot;
c906108c 17174
b64f50a1
JK
17175 slot = htab_find_slot_with_hash (cu->die_hash, die, die->offset.sect_off,
17176 INSERT);
51545339
DJ
17177
17178 *slot = die;
c906108c
SS
17179}
17180
b64f50a1
JK
17181/* DW_ADDR is always stored already as sect_offset; despite for the forms
17182 besides DW_FORM_ref_addr it is stored as cu_offset in the DWARF file. */
17183
93311388
DE
17184static int
17185is_ref_attr (struct attribute *attr)
c906108c 17186{
c906108c
SS
17187 switch (attr->form)
17188 {
17189 case DW_FORM_ref_addr:
c906108c
SS
17190 case DW_FORM_ref1:
17191 case DW_FORM_ref2:
17192 case DW_FORM_ref4:
613e1657 17193 case DW_FORM_ref8:
c906108c 17194 case DW_FORM_ref_udata:
36586728 17195 case DW_FORM_GNU_ref_alt:
93311388 17196 return 1;
c906108c 17197 default:
93311388 17198 return 0;
c906108c 17199 }
93311388
DE
17200}
17201
b64f50a1
JK
17202/* Return DIE offset of ATTR. Return 0 with complaint if ATTR is not of the
17203 required kind. */
17204
17205static sect_offset
93311388
DE
17206dwarf2_get_ref_die_offset (struct attribute *attr)
17207{
4568ecf9 17208 sect_offset retval = { DW_UNSND (attr) };
b64f50a1 17209
93311388 17210 if (is_ref_attr (attr))
b64f50a1 17211 return retval;
93311388 17212
b64f50a1 17213 retval.sect_off = 0;
93311388
DE
17214 complaint (&symfile_complaints,
17215 _("unsupported die ref attribute form: '%s'"),
17216 dwarf_form_name (attr->form));
b64f50a1 17217 return retval;
c906108c
SS
17218}
17219
43bbcdc2
PH
17220/* Return the constant value held by ATTR. Return DEFAULT_VALUE if
17221 * the value held by the attribute is not constant. */
a02abb62 17222
43bbcdc2 17223static LONGEST
a02abb62
JB
17224dwarf2_get_attr_constant_value (struct attribute *attr, int default_value)
17225{
17226 if (attr->form == DW_FORM_sdata)
17227 return DW_SND (attr);
17228 else if (attr->form == DW_FORM_udata
17229 || attr->form == DW_FORM_data1
17230 || attr->form == DW_FORM_data2
17231 || attr->form == DW_FORM_data4
17232 || attr->form == DW_FORM_data8)
17233 return DW_UNSND (attr);
17234 else
17235 {
3e43a32a
MS
17236 complaint (&symfile_complaints,
17237 _("Attribute value is not a constant (%s)"),
a02abb62
JB
17238 dwarf_form_name (attr->form));
17239 return default_value;
17240 }
17241}
17242
348e048f
DE
17243/* Follow reference or signature attribute ATTR of SRC_DIE.
17244 On entry *REF_CU is the CU of SRC_DIE.
17245 On exit *REF_CU is the CU of the result. */
17246
17247static struct die_info *
17248follow_die_ref_or_sig (struct die_info *src_die, struct attribute *attr,
17249 struct dwarf2_cu **ref_cu)
17250{
17251 struct die_info *die;
17252
17253 if (is_ref_attr (attr))
17254 die = follow_die_ref (src_die, attr, ref_cu);
55f1336d 17255 else if (attr->form == DW_FORM_ref_sig8)
348e048f
DE
17256 die = follow_die_sig (src_die, attr, ref_cu);
17257 else
17258 {
17259 dump_die_for_error (src_die);
17260 error (_("Dwarf Error: Expected reference attribute [in module %s]"),
17261 (*ref_cu)->objfile->name);
17262 }
17263
17264 return die;
03dd20cc
DJ
17265}
17266
5c631832 17267/* Follow reference OFFSET.
673bfd45
DE
17268 On entry *REF_CU is the CU of the source die referencing OFFSET.
17269 On exit *REF_CU is the CU of the result.
17270 Returns NULL if OFFSET is invalid. */
f504f079 17271
f9aca02d 17272static struct die_info *
36586728
TT
17273follow_die_offset (sect_offset offset, int offset_in_dwz,
17274 struct dwarf2_cu **ref_cu)
c906108c 17275{
10b3939b 17276 struct die_info temp_die;
f2f0e013 17277 struct dwarf2_cu *target_cu, *cu = *ref_cu;
10b3939b 17278
348e048f
DE
17279 gdb_assert (cu->per_cu != NULL);
17280
98bfdba5
PA
17281 target_cu = cu;
17282
3019eac3 17283 if (cu->per_cu->is_debug_types)
348e048f
DE
17284 {
17285 /* .debug_types CUs cannot reference anything outside their CU.
17286 If they need to, they have to reference a signatured type via
55f1336d 17287 DW_FORM_ref_sig8. */
348e048f 17288 if (! offset_in_cu_p (&cu->header, offset))
5c631832 17289 return NULL;
348e048f 17290 }
36586728
TT
17291 else if (offset_in_dwz != cu->per_cu->is_dwz
17292 || ! offset_in_cu_p (&cu->header, offset))
10b3939b
DJ
17293 {
17294 struct dwarf2_per_cu_data *per_cu;
9a619af0 17295
36586728
TT
17296 per_cu = dwarf2_find_containing_comp_unit (offset, offset_in_dwz,
17297 cu->objfile);
03dd20cc
DJ
17298
17299 /* If necessary, add it to the queue and load its DIEs. */
95554aad
TT
17300 if (maybe_queue_comp_unit (cu, per_cu, cu->language))
17301 load_full_comp_unit (per_cu, cu->language);
03dd20cc 17302
10b3939b
DJ
17303 target_cu = per_cu->cu;
17304 }
98bfdba5
PA
17305 else if (cu->dies == NULL)
17306 {
17307 /* We're loading full DIEs during partial symbol reading. */
17308 gdb_assert (dwarf2_per_objfile->reading_partial_symbols);
95554aad 17309 load_full_comp_unit (cu->per_cu, language_minimal);
98bfdba5 17310 }
c906108c 17311
f2f0e013 17312 *ref_cu = target_cu;
51545339 17313 temp_die.offset = offset;
b64f50a1 17314 return htab_find_with_hash (target_cu->die_hash, &temp_die, offset.sect_off);
5c631832 17315}
10b3939b 17316
5c631832
JK
17317/* Follow reference attribute ATTR of SRC_DIE.
17318 On entry *REF_CU is the CU of SRC_DIE.
17319 On exit *REF_CU is the CU of the result. */
17320
17321static struct die_info *
17322follow_die_ref (struct die_info *src_die, struct attribute *attr,
17323 struct dwarf2_cu **ref_cu)
17324{
b64f50a1 17325 sect_offset offset = dwarf2_get_ref_die_offset (attr);
5c631832
JK
17326 struct dwarf2_cu *cu = *ref_cu;
17327 struct die_info *die;
17328
36586728
TT
17329 die = follow_die_offset (offset,
17330 (attr->form == DW_FORM_GNU_ref_alt
17331 || cu->per_cu->is_dwz),
17332 ref_cu);
5c631832
JK
17333 if (!die)
17334 error (_("Dwarf Error: Cannot find DIE at 0x%x referenced from DIE "
17335 "at 0x%x [in module %s]"),
b64f50a1 17336 offset.sect_off, src_die->offset.sect_off, cu->objfile->name);
348e048f 17337
5c631832
JK
17338 return die;
17339}
17340
d83e736b
JK
17341/* Return DWARF block referenced by DW_AT_location of DIE at OFFSET at PER_CU.
17342 Returned value is intended for DW_OP_call*. Returned
17343 dwarf2_locexpr_baton->data has lifetime of PER_CU->OBJFILE. */
5c631832
JK
17344
17345struct dwarf2_locexpr_baton
b64f50a1 17346dwarf2_fetch_die_location_block (cu_offset offset_in_cu,
8cf6f0b1
TT
17347 struct dwarf2_per_cu_data *per_cu,
17348 CORE_ADDR (*get_frame_pc) (void *baton),
17349 void *baton)
5c631832 17350{
b64f50a1 17351 sect_offset offset = { per_cu->offset.sect_off + offset_in_cu.cu_off };
918dd910 17352 struct dwarf2_cu *cu;
5c631832
JK
17353 struct die_info *die;
17354 struct attribute *attr;
17355 struct dwarf2_locexpr_baton retval;
17356
8cf6f0b1
TT
17357 dw2_setup (per_cu->objfile);
17358
918dd910
JK
17359 if (per_cu->cu == NULL)
17360 load_cu (per_cu);
17361 cu = per_cu->cu;
17362
36586728 17363 die = follow_die_offset (offset, per_cu->is_dwz, &cu);
5c631832
JK
17364 if (!die)
17365 error (_("Dwarf Error: Cannot find DIE at 0x%x referenced in module %s"),
b64f50a1 17366 offset.sect_off, per_cu->objfile->name);
5c631832
JK
17367
17368 attr = dwarf2_attr (die, DW_AT_location, cu);
17369 if (!attr)
17370 {
e103e986
JK
17371 /* DWARF: "If there is no such attribute, then there is no effect.".
17372 DATA is ignored if SIZE is 0. */
5c631832 17373
e103e986 17374 retval.data = NULL;
5c631832
JK
17375 retval.size = 0;
17376 }
8cf6f0b1
TT
17377 else if (attr_form_is_section_offset (attr))
17378 {
17379 struct dwarf2_loclist_baton loclist_baton;
17380 CORE_ADDR pc = (*get_frame_pc) (baton);
17381 size_t size;
17382
17383 fill_in_loclist_baton (cu, &loclist_baton, attr);
17384
17385 retval.data = dwarf2_find_location_expression (&loclist_baton,
17386 &size, pc);
17387 retval.size = size;
17388 }
5c631832
JK
17389 else
17390 {
17391 if (!attr_form_is_block (attr))
17392 error (_("Dwarf Error: DIE at 0x%x referenced in module %s "
17393 "is neither DW_FORM_block* nor DW_FORM_exprloc"),
b64f50a1 17394 offset.sect_off, per_cu->objfile->name);
5c631832
JK
17395
17396 retval.data = DW_BLOCK (attr)->data;
17397 retval.size = DW_BLOCK (attr)->size;
17398 }
17399 retval.per_cu = cu->per_cu;
918dd910 17400
918dd910
JK
17401 age_cached_comp_units ();
17402
5c631832 17403 return retval;
348e048f
DE
17404}
17405
8a9b8146
TT
17406/* Return the type of the DIE at DIE_OFFSET in the CU named by
17407 PER_CU. */
17408
17409struct type *
b64f50a1 17410dwarf2_get_die_type (cu_offset die_offset,
8a9b8146
TT
17411 struct dwarf2_per_cu_data *per_cu)
17412{
b64f50a1
JK
17413 sect_offset die_offset_sect;
17414
8a9b8146 17415 dw2_setup (per_cu->objfile);
b64f50a1
JK
17416
17417 die_offset_sect.sect_off = per_cu->offset.sect_off + die_offset.cu_off;
17418 return get_die_type_at_offset (die_offset_sect, per_cu);
8a9b8146
TT
17419}
17420
348e048f
DE
17421/* Follow the signature attribute ATTR in SRC_DIE.
17422 On entry *REF_CU is the CU of SRC_DIE.
17423 On exit *REF_CU is the CU of the result. */
17424
17425static struct die_info *
17426follow_die_sig (struct die_info *src_die, struct attribute *attr,
17427 struct dwarf2_cu **ref_cu)
17428{
17429 struct objfile *objfile = (*ref_cu)->objfile;
17430 struct die_info temp_die;
17431 struct signatured_type *sig_type = DW_SIGNATURED_TYPE (attr);
17432 struct dwarf2_cu *sig_cu;
17433 struct die_info *die;
17434
17435 /* sig_type will be NULL if the signatured type is missing from
17436 the debug info. */
17437 if (sig_type == NULL)
17438 error (_("Dwarf Error: Cannot find signatured DIE referenced from DIE "
17439 "at 0x%x [in module %s]"),
b64f50a1 17440 src_die->offset.sect_off, objfile->name);
348e048f
DE
17441
17442 /* If necessary, add it to the queue and load its DIEs. */
17443
95554aad 17444 if (maybe_queue_comp_unit (*ref_cu, &sig_type->per_cu, language_minimal))
a0f42c21 17445 read_signatured_type (sig_type);
348e048f
DE
17446
17447 gdb_assert (sig_type->per_cu.cu != NULL);
17448
17449 sig_cu = sig_type->per_cu.cu;
3019eac3
DE
17450 gdb_assert (sig_type->type_offset_in_section.sect_off != 0);
17451 temp_die.offset = sig_type->type_offset_in_section;
b64f50a1
JK
17452 die = htab_find_with_hash (sig_cu->die_hash, &temp_die,
17453 temp_die.offset.sect_off);
348e048f
DE
17454 if (die)
17455 {
17456 *ref_cu = sig_cu;
17457 return die;
17458 }
17459
3e43a32a
MS
17460 error (_("Dwarf Error: Cannot find signatured DIE at 0x%x referenced "
17461 "from DIE at 0x%x [in module %s]"),
b64f50a1 17462 temp_die.offset.sect_off, src_die->offset.sect_off, objfile->name);
348e048f
DE
17463}
17464
17465/* Given an offset of a signatured type, return its signatured_type. */
17466
17467static struct signatured_type *
8b70b953
TT
17468lookup_signatured_type_at_offset (struct objfile *objfile,
17469 struct dwarf2_section_info *section,
b64f50a1 17470 sect_offset offset)
348e048f 17471{
b64f50a1 17472 gdb_byte *info_ptr = section->buffer + offset.sect_off;
348e048f
DE
17473 unsigned int length, initial_length_size;
17474 unsigned int sig_offset;
52dc124a 17475 struct signatured_type find_entry, *sig_type;
348e048f
DE
17476
17477 length = read_initial_length (objfile->obfd, info_ptr, &initial_length_size);
17478 sig_offset = (initial_length_size
17479 + 2 /*version*/
17480 + (initial_length_size == 4 ? 4 : 8) /*debug_abbrev_offset*/
17481 + 1 /*address_size*/);
17482 find_entry.signature = bfd_get_64 (objfile->obfd, info_ptr + sig_offset);
52dc124a 17483 sig_type = htab_find (dwarf2_per_objfile->signatured_types, &find_entry);
348e048f
DE
17484
17485 /* This is only used to lookup previously recorded types.
17486 If we didn't find it, it's our bug. */
52dc124a
DE
17487 gdb_assert (sig_type != NULL);
17488 gdb_assert (offset.sect_off == sig_type->per_cu.offset.sect_off);
348e048f 17489
52dc124a 17490 return sig_type;
348e048f
DE
17491}
17492
e5fe5e75 17493/* Load the DIEs associated with type unit PER_CU into memory. */
348e048f
DE
17494
17495static void
e5fe5e75 17496load_full_type_unit (struct dwarf2_per_cu_data *per_cu)
348e048f 17497{
52dc124a 17498 struct signatured_type *sig_type;
348e048f 17499
f4dc4d17
DE
17500 /* Caller is responsible for ensuring type_unit_groups don't get here. */
17501 gdb_assert (! IS_TYPE_UNIT_GROUP (per_cu));
17502
6721b2ec
DE
17503 /* We have the per_cu, but we need the signatured_type.
17504 Fortunately this is an easy translation. */
17505 gdb_assert (per_cu->is_debug_types);
17506 sig_type = (struct signatured_type *) per_cu;
348e048f 17507
6721b2ec 17508 gdb_assert (per_cu->cu == NULL);
348e048f 17509
52dc124a 17510 read_signatured_type (sig_type);
348e048f 17511
6721b2ec 17512 gdb_assert (per_cu->cu != NULL);
348e048f
DE
17513}
17514
dee91e82
DE
17515/* die_reader_func for read_signatured_type.
17516 This is identical to load_full_comp_unit_reader,
17517 but is kept separate for now. */
348e048f
DE
17518
17519static void
dee91e82
DE
17520read_signatured_type_reader (const struct die_reader_specs *reader,
17521 gdb_byte *info_ptr,
17522 struct die_info *comp_unit_die,
17523 int has_children,
17524 void *data)
348e048f 17525{
dee91e82 17526 struct dwarf2_cu *cu = reader->cu;
348e048f 17527
dee91e82
DE
17528 gdb_assert (cu->die_hash == NULL);
17529 cu->die_hash =
17530 htab_create_alloc_ex (cu->header.length / 12,
17531 die_hash,
17532 die_eq,
17533 NULL,
17534 &cu->comp_unit_obstack,
17535 hashtab_obstack_allocate,
17536 dummy_obstack_deallocate);
348e048f 17537
dee91e82
DE
17538 if (has_children)
17539 comp_unit_die->child = read_die_and_siblings (reader, info_ptr,
17540 &info_ptr, comp_unit_die);
17541 cu->dies = comp_unit_die;
17542 /* comp_unit_die is not stored in die_hash, no need. */
348e048f
DE
17543
17544 /* We try not to read any attributes in this function, because not
9cdd5dbd 17545 all CUs needed for references have been loaded yet, and symbol
348e048f 17546 table processing isn't initialized. But we have to set the CU language,
dee91e82
DE
17547 or we won't be able to build types correctly.
17548 Similarly, if we do not read the producer, we can not apply
17549 producer-specific interpretation. */
95554aad 17550 prepare_one_comp_unit (cu, cu->dies, language_minimal);
dee91e82 17551}
348e048f 17552
3019eac3
DE
17553/* Read in a signatured type and build its CU and DIEs.
17554 If the type is a stub for the real type in a DWO file,
17555 read in the real type from the DWO file as well. */
dee91e82
DE
17556
17557static void
17558read_signatured_type (struct signatured_type *sig_type)
17559{
17560 struct dwarf2_per_cu_data *per_cu = &sig_type->per_cu;
348e048f 17561
3019eac3 17562 gdb_assert (per_cu->is_debug_types);
dee91e82 17563 gdb_assert (per_cu->cu == NULL);
348e048f 17564
f4dc4d17
DE
17565 init_cutu_and_read_dies (per_cu, NULL, 0, 1,
17566 read_signatured_type_reader, NULL);
c906108c
SS
17567}
17568
c906108c
SS
17569/* Decode simple location descriptions.
17570 Given a pointer to a dwarf block that defines a location, compute
17571 the location and return the value.
17572
4cecd739
DJ
17573 NOTE drow/2003-11-18: This function is called in two situations
17574 now: for the address of static or global variables (partial symbols
17575 only) and for offsets into structures which are expected to be
17576 (more or less) constant. The partial symbol case should go away,
17577 and only the constant case should remain. That will let this
17578 function complain more accurately. A few special modes are allowed
17579 without complaint for global variables (for instance, global
17580 register values and thread-local values).
c906108c
SS
17581
17582 A location description containing no operations indicates that the
4cecd739 17583 object is optimized out. The return value is 0 for that case.
6b992462
DJ
17584 FIXME drow/2003-11-16: No callers check for this case any more; soon all
17585 callers will only want a very basic result and this can become a
21ae7a4d
JK
17586 complaint.
17587
17588 Note that stack[0] is unused except as a default error return. */
c906108c
SS
17589
17590static CORE_ADDR
e7c27a73 17591decode_locdesc (struct dwarf_block *blk, struct dwarf2_cu *cu)
c906108c 17592{
e7c27a73 17593 struct objfile *objfile = cu->objfile;
56eb65bd
SP
17594 size_t i;
17595 size_t size = blk->size;
21ae7a4d
JK
17596 gdb_byte *data = blk->data;
17597 CORE_ADDR stack[64];
17598 int stacki;
17599 unsigned int bytes_read, unsnd;
17600 gdb_byte op;
c906108c 17601
21ae7a4d
JK
17602 i = 0;
17603 stacki = 0;
17604 stack[stacki] = 0;
17605 stack[++stacki] = 0;
17606
17607 while (i < size)
17608 {
17609 op = data[i++];
17610 switch (op)
17611 {
17612 case DW_OP_lit0:
17613 case DW_OP_lit1:
17614 case DW_OP_lit2:
17615 case DW_OP_lit3:
17616 case DW_OP_lit4:
17617 case DW_OP_lit5:
17618 case DW_OP_lit6:
17619 case DW_OP_lit7:
17620 case DW_OP_lit8:
17621 case DW_OP_lit9:
17622 case DW_OP_lit10:
17623 case DW_OP_lit11:
17624 case DW_OP_lit12:
17625 case DW_OP_lit13:
17626 case DW_OP_lit14:
17627 case DW_OP_lit15:
17628 case DW_OP_lit16:
17629 case DW_OP_lit17:
17630 case DW_OP_lit18:
17631 case DW_OP_lit19:
17632 case DW_OP_lit20:
17633 case DW_OP_lit21:
17634 case DW_OP_lit22:
17635 case DW_OP_lit23:
17636 case DW_OP_lit24:
17637 case DW_OP_lit25:
17638 case DW_OP_lit26:
17639 case DW_OP_lit27:
17640 case DW_OP_lit28:
17641 case DW_OP_lit29:
17642 case DW_OP_lit30:
17643 case DW_OP_lit31:
17644 stack[++stacki] = op - DW_OP_lit0;
17645 break;
f1bea926 17646
21ae7a4d
JK
17647 case DW_OP_reg0:
17648 case DW_OP_reg1:
17649 case DW_OP_reg2:
17650 case DW_OP_reg3:
17651 case DW_OP_reg4:
17652 case DW_OP_reg5:
17653 case DW_OP_reg6:
17654 case DW_OP_reg7:
17655 case DW_OP_reg8:
17656 case DW_OP_reg9:
17657 case DW_OP_reg10:
17658 case DW_OP_reg11:
17659 case DW_OP_reg12:
17660 case DW_OP_reg13:
17661 case DW_OP_reg14:
17662 case DW_OP_reg15:
17663 case DW_OP_reg16:
17664 case DW_OP_reg17:
17665 case DW_OP_reg18:
17666 case DW_OP_reg19:
17667 case DW_OP_reg20:
17668 case DW_OP_reg21:
17669 case DW_OP_reg22:
17670 case DW_OP_reg23:
17671 case DW_OP_reg24:
17672 case DW_OP_reg25:
17673 case DW_OP_reg26:
17674 case DW_OP_reg27:
17675 case DW_OP_reg28:
17676 case DW_OP_reg29:
17677 case DW_OP_reg30:
17678 case DW_OP_reg31:
17679 stack[++stacki] = op - DW_OP_reg0;
17680 if (i < size)
17681 dwarf2_complex_location_expr_complaint ();
17682 break;
c906108c 17683
21ae7a4d
JK
17684 case DW_OP_regx:
17685 unsnd = read_unsigned_leb128 (NULL, (data + i), &bytes_read);
17686 i += bytes_read;
17687 stack[++stacki] = unsnd;
17688 if (i < size)
17689 dwarf2_complex_location_expr_complaint ();
17690 break;
c906108c 17691
21ae7a4d
JK
17692 case DW_OP_addr:
17693 stack[++stacki] = read_address (objfile->obfd, &data[i],
17694 cu, &bytes_read);
17695 i += bytes_read;
17696 break;
d53d4ac5 17697
21ae7a4d
JK
17698 case DW_OP_const1u:
17699 stack[++stacki] = read_1_byte (objfile->obfd, &data[i]);
17700 i += 1;
17701 break;
17702
17703 case DW_OP_const1s:
17704 stack[++stacki] = read_1_signed_byte (objfile->obfd, &data[i]);
17705 i += 1;
17706 break;
17707
17708 case DW_OP_const2u:
17709 stack[++stacki] = read_2_bytes (objfile->obfd, &data[i]);
17710 i += 2;
17711 break;
17712
17713 case DW_OP_const2s:
17714 stack[++stacki] = read_2_signed_bytes (objfile->obfd, &data[i]);
17715 i += 2;
17716 break;
d53d4ac5 17717
21ae7a4d
JK
17718 case DW_OP_const4u:
17719 stack[++stacki] = read_4_bytes (objfile->obfd, &data[i]);
17720 i += 4;
17721 break;
17722
17723 case DW_OP_const4s:
17724 stack[++stacki] = read_4_signed_bytes (objfile->obfd, &data[i]);
17725 i += 4;
17726 break;
17727
585861ea
JK
17728 case DW_OP_const8u:
17729 stack[++stacki] = read_8_bytes (objfile->obfd, &data[i]);
17730 i += 8;
17731 break;
17732
21ae7a4d
JK
17733 case DW_OP_constu:
17734 stack[++stacki] = read_unsigned_leb128 (NULL, (data + i),
17735 &bytes_read);
17736 i += bytes_read;
17737 break;
17738
17739 case DW_OP_consts:
17740 stack[++stacki] = read_signed_leb128 (NULL, (data + i), &bytes_read);
17741 i += bytes_read;
17742 break;
17743
17744 case DW_OP_dup:
17745 stack[stacki + 1] = stack[stacki];
17746 stacki++;
17747 break;
17748
17749 case DW_OP_plus:
17750 stack[stacki - 1] += stack[stacki];
17751 stacki--;
17752 break;
17753
17754 case DW_OP_plus_uconst:
17755 stack[stacki] += read_unsigned_leb128 (NULL, (data + i),
17756 &bytes_read);
17757 i += bytes_read;
17758 break;
17759
17760 case DW_OP_minus:
17761 stack[stacki - 1] -= stack[stacki];
17762 stacki--;
17763 break;
17764
17765 case DW_OP_deref:
17766 /* If we're not the last op, then we definitely can't encode
17767 this using GDB's address_class enum. This is valid for partial
17768 global symbols, although the variable's address will be bogus
17769 in the psymtab. */
17770 if (i < size)
17771 dwarf2_complex_location_expr_complaint ();
17772 break;
17773
17774 case DW_OP_GNU_push_tls_address:
17775 /* The top of the stack has the offset from the beginning
17776 of the thread control block at which the variable is located. */
17777 /* Nothing should follow this operator, so the top of stack would
17778 be returned. */
17779 /* This is valid for partial global symbols, but the variable's
585861ea
JK
17780 address will be bogus in the psymtab. Make it always at least
17781 non-zero to not look as a variable garbage collected by linker
17782 which have DW_OP_addr 0. */
21ae7a4d
JK
17783 if (i < size)
17784 dwarf2_complex_location_expr_complaint ();
585861ea 17785 stack[stacki]++;
21ae7a4d
JK
17786 break;
17787
17788 case DW_OP_GNU_uninit:
17789 break;
17790
3019eac3 17791 case DW_OP_GNU_addr_index:
49f6c839 17792 case DW_OP_GNU_const_index:
3019eac3
DE
17793 stack[++stacki] = read_addr_index_from_leb128 (cu, &data[i],
17794 &bytes_read);
17795 i += bytes_read;
17796 break;
17797
21ae7a4d
JK
17798 default:
17799 {
f39c6ffd 17800 const char *name = get_DW_OP_name (op);
21ae7a4d
JK
17801
17802 if (name)
17803 complaint (&symfile_complaints, _("unsupported stack op: '%s'"),
17804 name);
17805 else
17806 complaint (&symfile_complaints, _("unsupported stack op: '%02x'"),
17807 op);
17808 }
17809
17810 return (stack[stacki]);
d53d4ac5 17811 }
3c6e0cb3 17812
21ae7a4d
JK
17813 /* Enforce maximum stack depth of SIZE-1 to avoid writing
17814 outside of the allocated space. Also enforce minimum>0. */
17815 if (stacki >= ARRAY_SIZE (stack) - 1)
17816 {
17817 complaint (&symfile_complaints,
17818 _("location description stack overflow"));
17819 return 0;
17820 }
17821
17822 if (stacki <= 0)
17823 {
17824 complaint (&symfile_complaints,
17825 _("location description stack underflow"));
17826 return 0;
17827 }
17828 }
17829 return (stack[stacki]);
c906108c
SS
17830}
17831
17832/* memory allocation interface */
17833
c906108c 17834static struct dwarf_block *
7b5a2f43 17835dwarf_alloc_block (struct dwarf2_cu *cu)
c906108c
SS
17836{
17837 struct dwarf_block *blk;
17838
17839 blk = (struct dwarf_block *)
7b5a2f43 17840 obstack_alloc (&cu->comp_unit_obstack, sizeof (struct dwarf_block));
c906108c
SS
17841 return (blk);
17842}
17843
c906108c 17844static struct die_info *
b60c80d6 17845dwarf_alloc_die (struct dwarf2_cu *cu, int num_attrs)
c906108c
SS
17846{
17847 struct die_info *die;
b60c80d6
DJ
17848 size_t size = sizeof (struct die_info);
17849
17850 if (num_attrs > 1)
17851 size += (num_attrs - 1) * sizeof (struct attribute);
c906108c 17852
b60c80d6 17853 die = (struct die_info *) obstack_alloc (&cu->comp_unit_obstack, size);
c906108c
SS
17854 memset (die, 0, sizeof (struct die_info));
17855 return (die);
17856}
2e276125
JB
17857
17858\f
17859/* Macro support. */
17860
2e276125
JB
17861/* Return the full name of file number I in *LH's file name table.
17862 Use COMP_DIR as the name of the current directory of the
17863 compilation. The result is allocated using xmalloc; the caller is
17864 responsible for freeing it. */
17865static char *
17866file_full_name (int file, struct line_header *lh, const char *comp_dir)
17867{
6a83a1e6
EZ
17868 /* Is the file number a valid index into the line header's file name
17869 table? Remember that file numbers start with one, not zero. */
17870 if (1 <= file && file <= lh->num_file_names)
17871 {
17872 struct file_entry *fe = &lh->file_names[file - 1];
6e70227d 17873
6a83a1e6
EZ
17874 if (IS_ABSOLUTE_PATH (fe->name))
17875 return xstrdup (fe->name);
17876 else
17877 {
17878 const char *dir;
17879 int dir_len;
17880 char *full_name;
17881
17882 if (fe->dir_index)
17883 dir = lh->include_dirs[fe->dir_index - 1];
17884 else
17885 dir = comp_dir;
17886
17887 if (dir)
17888 {
17889 dir_len = strlen (dir);
17890 full_name = xmalloc (dir_len + 1 + strlen (fe->name) + 1);
17891 strcpy (full_name, dir);
17892 full_name[dir_len] = '/';
17893 strcpy (full_name + dir_len + 1, fe->name);
17894 return full_name;
17895 }
17896 else
17897 return xstrdup (fe->name);
17898 }
17899 }
2e276125
JB
17900 else
17901 {
6a83a1e6
EZ
17902 /* The compiler produced a bogus file number. We can at least
17903 record the macro definitions made in the file, even if we
17904 won't be able to find the file by name. */
17905 char fake_name[80];
9a619af0 17906
8c042590
PM
17907 xsnprintf (fake_name, sizeof (fake_name),
17908 "<bad macro file number %d>", file);
2e276125 17909
6e70227d 17910 complaint (&symfile_complaints,
6a83a1e6
EZ
17911 _("bad file number in macro information (%d)"),
17912 file);
2e276125 17913
6a83a1e6 17914 return xstrdup (fake_name);
2e276125
JB
17915 }
17916}
17917
17918
17919static struct macro_source_file *
17920macro_start_file (int file, int line,
17921 struct macro_source_file *current_file,
17922 const char *comp_dir,
17923 struct line_header *lh, struct objfile *objfile)
17924{
17925 /* The full name of this source file. */
17926 char *full_name = file_full_name (file, lh, comp_dir);
17927
17928 /* We don't create a macro table for this compilation unit
17929 at all until we actually get a filename. */
17930 if (! pending_macros)
6532ff36
TT
17931 pending_macros = new_macro_table (&objfile->per_bfd->storage_obstack,
17932 objfile->per_bfd->macro_cache);
2e276125
JB
17933
17934 if (! current_file)
abc9d0dc
TT
17935 {
17936 /* If we have no current file, then this must be the start_file
17937 directive for the compilation unit's main source file. */
17938 current_file = macro_set_main (pending_macros, full_name);
17939 macro_define_special (pending_macros);
17940 }
2e276125
JB
17941 else
17942 current_file = macro_include (current_file, line, full_name);
17943
17944 xfree (full_name);
6e70227d 17945
2e276125
JB
17946 return current_file;
17947}
17948
17949
17950/* Copy the LEN characters at BUF to a xmalloc'ed block of memory,
17951 followed by a null byte. */
17952static char *
17953copy_string (const char *buf, int len)
17954{
17955 char *s = xmalloc (len + 1);
9a619af0 17956
2e276125
JB
17957 memcpy (s, buf, len);
17958 s[len] = '\0';
2e276125
JB
17959 return s;
17960}
17961
17962
17963static const char *
17964consume_improper_spaces (const char *p, const char *body)
17965{
17966 if (*p == ' ')
17967 {
4d3c2250 17968 complaint (&symfile_complaints,
3e43a32a
MS
17969 _("macro definition contains spaces "
17970 "in formal argument list:\n`%s'"),
4d3c2250 17971 body);
2e276125
JB
17972
17973 while (*p == ' ')
17974 p++;
17975 }
17976
17977 return p;
17978}
17979
17980
17981static void
17982parse_macro_definition (struct macro_source_file *file, int line,
17983 const char *body)
17984{
17985 const char *p;
17986
17987 /* The body string takes one of two forms. For object-like macro
17988 definitions, it should be:
17989
17990 <macro name> " " <definition>
17991
17992 For function-like macro definitions, it should be:
17993
17994 <macro name> "() " <definition>
17995 or
17996 <macro name> "(" <arg name> ( "," <arg name> ) * ") " <definition>
17997
17998 Spaces may appear only where explicitly indicated, and in the
17999 <definition>.
18000
18001 The Dwarf 2 spec says that an object-like macro's name is always
18002 followed by a space, but versions of GCC around March 2002 omit
6e70227d 18003 the space when the macro's definition is the empty string.
2e276125
JB
18004
18005 The Dwarf 2 spec says that there should be no spaces between the
18006 formal arguments in a function-like macro's formal argument list,
18007 but versions of GCC around March 2002 include spaces after the
18008 commas. */
18009
18010
18011 /* Find the extent of the macro name. The macro name is terminated
18012 by either a space or null character (for an object-like macro) or
18013 an opening paren (for a function-like macro). */
18014 for (p = body; *p; p++)
18015 if (*p == ' ' || *p == '(')
18016 break;
18017
18018 if (*p == ' ' || *p == '\0')
18019 {
18020 /* It's an object-like macro. */
18021 int name_len = p - body;
18022 char *name = copy_string (body, name_len);
18023 const char *replacement;
18024
18025 if (*p == ' ')
18026 replacement = body + name_len + 1;
18027 else
18028 {
4d3c2250 18029 dwarf2_macro_malformed_definition_complaint (body);
2e276125
JB
18030 replacement = body + name_len;
18031 }
6e70227d 18032
2e276125
JB
18033 macro_define_object (file, line, name, replacement);
18034
18035 xfree (name);
18036 }
18037 else if (*p == '(')
18038 {
18039 /* It's a function-like macro. */
18040 char *name = copy_string (body, p - body);
18041 int argc = 0;
18042 int argv_size = 1;
18043 char **argv = xmalloc (argv_size * sizeof (*argv));
18044
18045 p++;
18046
18047 p = consume_improper_spaces (p, body);
18048
18049 /* Parse the formal argument list. */
18050 while (*p && *p != ')')
18051 {
18052 /* Find the extent of the current argument name. */
18053 const char *arg_start = p;
18054
18055 while (*p && *p != ',' && *p != ')' && *p != ' ')
18056 p++;
18057
18058 if (! *p || p == arg_start)
4d3c2250 18059 dwarf2_macro_malformed_definition_complaint (body);
2e276125
JB
18060 else
18061 {
18062 /* Make sure argv has room for the new argument. */
18063 if (argc >= argv_size)
18064 {
18065 argv_size *= 2;
18066 argv = xrealloc (argv, argv_size * sizeof (*argv));
18067 }
18068
18069 argv[argc++] = copy_string (arg_start, p - arg_start);
18070 }
18071
18072 p = consume_improper_spaces (p, body);
18073
18074 /* Consume the comma, if present. */
18075 if (*p == ',')
18076 {
18077 p++;
18078
18079 p = consume_improper_spaces (p, body);
18080 }
18081 }
18082
18083 if (*p == ')')
18084 {
18085 p++;
18086
18087 if (*p == ' ')
18088 /* Perfectly formed definition, no complaints. */
18089 macro_define_function (file, line, name,
6e70227d 18090 argc, (const char **) argv,
2e276125
JB
18091 p + 1);
18092 else if (*p == '\0')
18093 {
18094 /* Complain, but do define it. */
4d3c2250 18095 dwarf2_macro_malformed_definition_complaint (body);
2e276125 18096 macro_define_function (file, line, name,
6e70227d 18097 argc, (const char **) argv,
2e276125
JB
18098 p);
18099 }
18100 else
18101 /* Just complain. */
4d3c2250 18102 dwarf2_macro_malformed_definition_complaint (body);
2e276125
JB
18103 }
18104 else
18105 /* Just complain. */
4d3c2250 18106 dwarf2_macro_malformed_definition_complaint (body);
2e276125
JB
18107
18108 xfree (name);
18109 {
18110 int i;
18111
18112 for (i = 0; i < argc; i++)
18113 xfree (argv[i]);
18114 }
18115 xfree (argv);
18116 }
18117 else
4d3c2250 18118 dwarf2_macro_malformed_definition_complaint (body);
2e276125
JB
18119}
18120
cf2c3c16
TT
18121/* Skip some bytes from BYTES according to the form given in FORM.
18122 Returns the new pointer. */
2e276125 18123
cf2c3c16 18124static gdb_byte *
f664829e 18125skip_form_bytes (bfd *abfd, gdb_byte *bytes, gdb_byte *buffer_end,
cf2c3c16
TT
18126 enum dwarf_form form,
18127 unsigned int offset_size,
18128 struct dwarf2_section_info *section)
2e276125 18129{
cf2c3c16 18130 unsigned int bytes_read;
2e276125 18131
cf2c3c16 18132 switch (form)
2e276125 18133 {
cf2c3c16
TT
18134 case DW_FORM_data1:
18135 case DW_FORM_flag:
18136 ++bytes;
18137 break;
18138
18139 case DW_FORM_data2:
18140 bytes += 2;
18141 break;
18142
18143 case DW_FORM_data4:
18144 bytes += 4;
18145 break;
18146
18147 case DW_FORM_data8:
18148 bytes += 8;
18149 break;
18150
18151 case DW_FORM_string:
18152 read_direct_string (abfd, bytes, &bytes_read);
18153 bytes += bytes_read;
18154 break;
18155
18156 case DW_FORM_sec_offset:
18157 case DW_FORM_strp:
36586728 18158 case DW_FORM_GNU_strp_alt:
cf2c3c16
TT
18159 bytes += offset_size;
18160 break;
18161
18162 case DW_FORM_block:
18163 bytes += read_unsigned_leb128 (abfd, bytes, &bytes_read);
18164 bytes += bytes_read;
18165 break;
18166
18167 case DW_FORM_block1:
18168 bytes += 1 + read_1_byte (abfd, bytes);
18169 break;
18170 case DW_FORM_block2:
18171 bytes += 2 + read_2_bytes (abfd, bytes);
18172 break;
18173 case DW_FORM_block4:
18174 bytes += 4 + read_4_bytes (abfd, bytes);
18175 break;
18176
18177 case DW_FORM_sdata:
18178 case DW_FORM_udata:
3019eac3
DE
18179 case DW_FORM_GNU_addr_index:
18180 case DW_FORM_GNU_str_index:
f664829e
DE
18181 bytes = (gdb_byte *) gdb_skip_leb128 (bytes, buffer_end);
18182 if (bytes == NULL)
18183 {
18184 dwarf2_section_buffer_overflow_complaint (section);
18185 return NULL;
18186 }
cf2c3c16
TT
18187 break;
18188
18189 default:
18190 {
18191 complain:
18192 complaint (&symfile_complaints,
18193 _("invalid form 0x%x in `%s'"),
18194 form,
18195 section->asection->name);
18196 return NULL;
18197 }
2e276125
JB
18198 }
18199
cf2c3c16
TT
18200 return bytes;
18201}
757a13d0 18202
cf2c3c16
TT
18203/* A helper for dwarf_decode_macros that handles skipping an unknown
18204 opcode. Returns an updated pointer to the macro data buffer; or,
18205 on error, issues a complaint and returns NULL. */
757a13d0 18206
cf2c3c16
TT
18207static gdb_byte *
18208skip_unknown_opcode (unsigned int opcode,
18209 gdb_byte **opcode_definitions,
f664829e 18210 gdb_byte *mac_ptr, gdb_byte *mac_end,
cf2c3c16
TT
18211 bfd *abfd,
18212 unsigned int offset_size,
18213 struct dwarf2_section_info *section)
18214{
18215 unsigned int bytes_read, i;
18216 unsigned long arg;
18217 gdb_byte *defn;
2e276125 18218
cf2c3c16 18219 if (opcode_definitions[opcode] == NULL)
2e276125 18220 {
cf2c3c16
TT
18221 complaint (&symfile_complaints,
18222 _("unrecognized DW_MACFINO opcode 0x%x"),
18223 opcode);
18224 return NULL;
18225 }
2e276125 18226
cf2c3c16
TT
18227 defn = opcode_definitions[opcode];
18228 arg = read_unsigned_leb128 (abfd, defn, &bytes_read);
18229 defn += bytes_read;
2e276125 18230
cf2c3c16
TT
18231 for (i = 0; i < arg; ++i)
18232 {
f664829e
DE
18233 mac_ptr = skip_form_bytes (abfd, mac_ptr, mac_end, defn[i], offset_size,
18234 section);
cf2c3c16
TT
18235 if (mac_ptr == NULL)
18236 {
18237 /* skip_form_bytes already issued the complaint. */
18238 return NULL;
18239 }
18240 }
757a13d0 18241
cf2c3c16
TT
18242 return mac_ptr;
18243}
757a13d0 18244
cf2c3c16
TT
18245/* A helper function which parses the header of a macro section.
18246 If the macro section is the extended (for now called "GNU") type,
18247 then this updates *OFFSET_SIZE. Returns a pointer to just after
18248 the header, or issues a complaint and returns NULL on error. */
757a13d0 18249
cf2c3c16
TT
18250static gdb_byte *
18251dwarf_parse_macro_header (gdb_byte **opcode_definitions,
18252 bfd *abfd,
18253 gdb_byte *mac_ptr,
18254 unsigned int *offset_size,
18255 int section_is_gnu)
18256{
18257 memset (opcode_definitions, 0, 256 * sizeof (gdb_byte *));
757a13d0 18258
cf2c3c16
TT
18259 if (section_is_gnu)
18260 {
18261 unsigned int version, flags;
757a13d0 18262
cf2c3c16
TT
18263 version = read_2_bytes (abfd, mac_ptr);
18264 if (version != 4)
18265 {
18266 complaint (&symfile_complaints,
18267 _("unrecognized version `%d' in .debug_macro section"),
18268 version);
18269 return NULL;
18270 }
18271 mac_ptr += 2;
757a13d0 18272
cf2c3c16
TT
18273 flags = read_1_byte (abfd, mac_ptr);
18274 ++mac_ptr;
18275 *offset_size = (flags & 1) ? 8 : 4;
757a13d0 18276
cf2c3c16
TT
18277 if ((flags & 2) != 0)
18278 /* We don't need the line table offset. */
18279 mac_ptr += *offset_size;
757a13d0 18280
cf2c3c16
TT
18281 /* Vendor opcode descriptions. */
18282 if ((flags & 4) != 0)
18283 {
18284 unsigned int i, count;
757a13d0 18285
cf2c3c16
TT
18286 count = read_1_byte (abfd, mac_ptr);
18287 ++mac_ptr;
18288 for (i = 0; i < count; ++i)
18289 {
18290 unsigned int opcode, bytes_read;
18291 unsigned long arg;
18292
18293 opcode = read_1_byte (abfd, mac_ptr);
18294 ++mac_ptr;
18295 opcode_definitions[opcode] = mac_ptr;
18296 arg = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
18297 mac_ptr += bytes_read;
18298 mac_ptr += arg;
18299 }
757a13d0 18300 }
cf2c3c16 18301 }
757a13d0 18302
cf2c3c16
TT
18303 return mac_ptr;
18304}
757a13d0 18305
cf2c3c16 18306/* A helper for dwarf_decode_macros that handles the GNU extensions,
8fc3fc34 18307 including DW_MACRO_GNU_transparent_include. */
cf2c3c16
TT
18308
18309static void
18310dwarf_decode_macro_bytes (bfd *abfd, gdb_byte *mac_ptr, gdb_byte *mac_end,
18311 struct macro_source_file *current_file,
18312 struct line_header *lh, char *comp_dir,
18313 struct dwarf2_section_info *section,
36586728 18314 int section_is_gnu, int section_is_dwz,
cf2c3c16 18315 unsigned int offset_size,
8fc3fc34
TT
18316 struct objfile *objfile,
18317 htab_t include_hash)
cf2c3c16
TT
18318{
18319 enum dwarf_macro_record_type macinfo_type;
18320 int at_commandline;
18321 gdb_byte *opcode_definitions[256];
757a13d0 18322
cf2c3c16
TT
18323 mac_ptr = dwarf_parse_macro_header (opcode_definitions, abfd, mac_ptr,
18324 &offset_size, section_is_gnu);
18325 if (mac_ptr == NULL)
18326 {
18327 /* We already issued a complaint. */
18328 return;
18329 }
757a13d0
JK
18330
18331 /* Determines if GDB is still before first DW_MACINFO_start_file. If true
18332 GDB is still reading the definitions from command line. First
18333 DW_MACINFO_start_file will need to be ignored as it was already executed
18334 to create CURRENT_FILE for the main source holding also the command line
18335 definitions. On first met DW_MACINFO_start_file this flag is reset to
18336 normally execute all the remaining DW_MACINFO_start_file macinfos. */
18337
18338 at_commandline = 1;
18339
18340 do
18341 {
18342 /* Do we at least have room for a macinfo type byte? */
18343 if (mac_ptr >= mac_end)
18344 {
f664829e 18345 dwarf2_section_buffer_overflow_complaint (section);
757a13d0
JK
18346 break;
18347 }
18348
18349 macinfo_type = read_1_byte (abfd, mac_ptr);
18350 mac_ptr++;
18351
cf2c3c16
TT
18352 /* Note that we rely on the fact that the corresponding GNU and
18353 DWARF constants are the same. */
757a13d0
JK
18354 switch (macinfo_type)
18355 {
18356 /* A zero macinfo type indicates the end of the macro
18357 information. */
18358 case 0:
18359 break;
2e276125 18360
cf2c3c16
TT
18361 case DW_MACRO_GNU_define:
18362 case DW_MACRO_GNU_undef:
18363 case DW_MACRO_GNU_define_indirect:
18364 case DW_MACRO_GNU_undef_indirect:
36586728
TT
18365 case DW_MACRO_GNU_define_indirect_alt:
18366 case DW_MACRO_GNU_undef_indirect_alt:
2e276125 18367 {
891d2f0b 18368 unsigned int bytes_read;
2e276125
JB
18369 int line;
18370 char *body;
cf2c3c16 18371 int is_define;
2e276125 18372
cf2c3c16
TT
18373 line = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
18374 mac_ptr += bytes_read;
18375
18376 if (macinfo_type == DW_MACRO_GNU_define
18377 || macinfo_type == DW_MACRO_GNU_undef)
18378 {
18379 body = read_direct_string (abfd, mac_ptr, &bytes_read);
18380 mac_ptr += bytes_read;
18381 }
18382 else
18383 {
18384 LONGEST str_offset;
18385
18386 str_offset = read_offset_1 (abfd, mac_ptr, offset_size);
18387 mac_ptr += offset_size;
2e276125 18388
36586728 18389 if (macinfo_type == DW_MACRO_GNU_define_indirect_alt
f7a35f02
TT
18390 || macinfo_type == DW_MACRO_GNU_undef_indirect_alt
18391 || section_is_dwz)
36586728
TT
18392 {
18393 struct dwz_file *dwz = dwarf2_get_dwz_file ();
18394
18395 body = read_indirect_string_from_dwz (dwz, str_offset);
18396 }
18397 else
18398 body = read_indirect_string_at_offset (abfd, str_offset);
cf2c3c16
TT
18399 }
18400
18401 is_define = (macinfo_type == DW_MACRO_GNU_define
36586728
TT
18402 || macinfo_type == DW_MACRO_GNU_define_indirect
18403 || macinfo_type == DW_MACRO_GNU_define_indirect_alt);
2e276125 18404 if (! current_file)
757a13d0
JK
18405 {
18406 /* DWARF violation as no main source is present. */
18407 complaint (&symfile_complaints,
18408 _("debug info with no main source gives macro %s "
18409 "on line %d: %s"),
cf2c3c16
TT
18410 is_define ? _("definition") : _("undefinition"),
18411 line, body);
757a13d0
JK
18412 break;
18413 }
3e43a32a
MS
18414 if ((line == 0 && !at_commandline)
18415 || (line != 0 && at_commandline))
4d3c2250 18416 complaint (&symfile_complaints,
757a13d0
JK
18417 _("debug info gives %s macro %s with %s line %d: %s"),
18418 at_commandline ? _("command-line") : _("in-file"),
cf2c3c16 18419 is_define ? _("definition") : _("undefinition"),
757a13d0
JK
18420 line == 0 ? _("zero") : _("non-zero"), line, body);
18421
cf2c3c16 18422 if (is_define)
757a13d0 18423 parse_macro_definition (current_file, line, body);
cf2c3c16
TT
18424 else
18425 {
18426 gdb_assert (macinfo_type == DW_MACRO_GNU_undef
36586728
TT
18427 || macinfo_type == DW_MACRO_GNU_undef_indirect
18428 || macinfo_type == DW_MACRO_GNU_undef_indirect_alt);
cf2c3c16
TT
18429 macro_undef (current_file, line, body);
18430 }
2e276125
JB
18431 }
18432 break;
18433
cf2c3c16 18434 case DW_MACRO_GNU_start_file:
2e276125 18435 {
891d2f0b 18436 unsigned int bytes_read;
2e276125
JB
18437 int line, file;
18438
18439 line = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
18440 mac_ptr += bytes_read;
18441 file = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
18442 mac_ptr += bytes_read;
18443
3e43a32a
MS
18444 if ((line == 0 && !at_commandline)
18445 || (line != 0 && at_commandline))
757a13d0
JK
18446 complaint (&symfile_complaints,
18447 _("debug info gives source %d included "
18448 "from %s at %s line %d"),
18449 file, at_commandline ? _("command-line") : _("file"),
18450 line == 0 ? _("zero") : _("non-zero"), line);
18451
18452 if (at_commandline)
18453 {
cf2c3c16
TT
18454 /* This DW_MACRO_GNU_start_file was executed in the
18455 pass one. */
757a13d0
JK
18456 at_commandline = 0;
18457 }
18458 else
18459 current_file = macro_start_file (file, line,
18460 current_file, comp_dir,
cf2c3c16 18461 lh, objfile);
2e276125
JB
18462 }
18463 break;
18464
cf2c3c16 18465 case DW_MACRO_GNU_end_file:
2e276125 18466 if (! current_file)
4d3c2250 18467 complaint (&symfile_complaints,
3e43a32a
MS
18468 _("macro debug info has an unmatched "
18469 "`close_file' directive"));
2e276125
JB
18470 else
18471 {
18472 current_file = current_file->included_by;
18473 if (! current_file)
18474 {
cf2c3c16 18475 enum dwarf_macro_record_type next_type;
2e276125
JB
18476
18477 /* GCC circa March 2002 doesn't produce the zero
18478 type byte marking the end of the compilation
18479 unit. Complain if it's not there, but exit no
18480 matter what. */
18481
18482 /* Do we at least have room for a macinfo type byte? */
18483 if (mac_ptr >= mac_end)
18484 {
f664829e 18485 dwarf2_section_buffer_overflow_complaint (section);
2e276125
JB
18486 return;
18487 }
18488
18489 /* We don't increment mac_ptr here, so this is just
18490 a look-ahead. */
18491 next_type = read_1_byte (abfd, mac_ptr);
18492 if (next_type != 0)
4d3c2250 18493 complaint (&symfile_complaints,
3e43a32a
MS
18494 _("no terminating 0-type entry for "
18495 "macros in `.debug_macinfo' section"));
2e276125
JB
18496
18497 return;
18498 }
18499 }
18500 break;
18501
cf2c3c16 18502 case DW_MACRO_GNU_transparent_include:
36586728 18503 case DW_MACRO_GNU_transparent_include_alt:
cf2c3c16
TT
18504 {
18505 LONGEST offset;
8fc3fc34 18506 void **slot;
a036ba48
TT
18507 bfd *include_bfd = abfd;
18508 struct dwarf2_section_info *include_section = section;
18509 struct dwarf2_section_info alt_section;
18510 gdb_byte *include_mac_end = mac_end;
18511 int is_dwz = section_is_dwz;
18512 gdb_byte *new_mac_ptr;
cf2c3c16
TT
18513
18514 offset = read_offset_1 (abfd, mac_ptr, offset_size);
18515 mac_ptr += offset_size;
18516
a036ba48
TT
18517 if (macinfo_type == DW_MACRO_GNU_transparent_include_alt)
18518 {
18519 struct dwz_file *dwz = dwarf2_get_dwz_file ();
18520
18521 dwarf2_read_section (dwarf2_per_objfile->objfile,
18522 &dwz->macro);
18523
18524 include_bfd = dwz->macro.asection->owner;
18525 include_section = &dwz->macro;
18526 include_mac_end = dwz->macro.buffer + dwz->macro.size;
18527 is_dwz = 1;
18528 }
18529
18530 new_mac_ptr = include_section->buffer + offset;
18531 slot = htab_find_slot (include_hash, new_mac_ptr, INSERT);
18532
8fc3fc34
TT
18533 if (*slot != NULL)
18534 {
18535 /* This has actually happened; see
18536 http://sourceware.org/bugzilla/show_bug.cgi?id=13568. */
18537 complaint (&symfile_complaints,
18538 _("recursive DW_MACRO_GNU_transparent_include in "
18539 ".debug_macro section"));
18540 }
18541 else
18542 {
a036ba48 18543 *slot = new_mac_ptr;
36586728 18544
a036ba48 18545 dwarf_decode_macro_bytes (include_bfd, new_mac_ptr,
36586728 18546 include_mac_end, current_file,
8fc3fc34 18547 lh, comp_dir,
36586728 18548 section, section_is_gnu, is_dwz,
8fc3fc34
TT
18549 offset_size, objfile, include_hash);
18550
a036ba48 18551 htab_remove_elt (include_hash, new_mac_ptr);
8fc3fc34 18552 }
cf2c3c16
TT
18553 }
18554 break;
18555
2e276125 18556 case DW_MACINFO_vendor_ext:
cf2c3c16
TT
18557 if (!section_is_gnu)
18558 {
18559 unsigned int bytes_read;
18560 int constant;
2e276125 18561
cf2c3c16
TT
18562 constant = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
18563 mac_ptr += bytes_read;
18564 read_direct_string (abfd, mac_ptr, &bytes_read);
18565 mac_ptr += bytes_read;
2e276125 18566
cf2c3c16
TT
18567 /* We don't recognize any vendor extensions. */
18568 break;
18569 }
18570 /* FALLTHROUGH */
18571
18572 default:
18573 mac_ptr = skip_unknown_opcode (macinfo_type, opcode_definitions,
f664829e 18574 mac_ptr, mac_end, abfd, offset_size,
cf2c3c16
TT
18575 section);
18576 if (mac_ptr == NULL)
18577 return;
18578 break;
2e276125 18579 }
757a13d0 18580 } while (macinfo_type != 0);
2e276125 18581}
8e19ed76 18582
cf2c3c16 18583static void
09262596
DE
18584dwarf_decode_macros (struct dwarf2_cu *cu, unsigned int offset,
18585 char *comp_dir, int section_is_gnu)
cf2c3c16 18586{
bb5ed363 18587 struct objfile *objfile = dwarf2_per_objfile->objfile;
09262596
DE
18588 struct line_header *lh = cu->line_header;
18589 bfd *abfd;
cf2c3c16
TT
18590 gdb_byte *mac_ptr, *mac_end;
18591 struct macro_source_file *current_file = 0;
18592 enum dwarf_macro_record_type macinfo_type;
18593 unsigned int offset_size = cu->header.offset_size;
18594 gdb_byte *opcode_definitions[256];
8fc3fc34
TT
18595 struct cleanup *cleanup;
18596 htab_t include_hash;
18597 void **slot;
09262596
DE
18598 struct dwarf2_section_info *section;
18599 const char *section_name;
18600
18601 if (cu->dwo_unit != NULL)
18602 {
18603 if (section_is_gnu)
18604 {
18605 section = &cu->dwo_unit->dwo_file->sections.macro;
18606 section_name = ".debug_macro.dwo";
18607 }
18608 else
18609 {
18610 section = &cu->dwo_unit->dwo_file->sections.macinfo;
18611 section_name = ".debug_macinfo.dwo";
18612 }
18613 }
18614 else
18615 {
18616 if (section_is_gnu)
18617 {
18618 section = &dwarf2_per_objfile->macro;
18619 section_name = ".debug_macro";
18620 }
18621 else
18622 {
18623 section = &dwarf2_per_objfile->macinfo;
18624 section_name = ".debug_macinfo";
18625 }
18626 }
cf2c3c16 18627
bb5ed363 18628 dwarf2_read_section (objfile, section);
cf2c3c16
TT
18629 if (section->buffer == NULL)
18630 {
fceca515 18631 complaint (&symfile_complaints, _("missing %s section"), section_name);
cf2c3c16
TT
18632 return;
18633 }
09262596 18634 abfd = section->asection->owner;
cf2c3c16
TT
18635
18636 /* First pass: Find the name of the base filename.
18637 This filename is needed in order to process all macros whose definition
18638 (or undefinition) comes from the command line. These macros are defined
18639 before the first DW_MACINFO_start_file entry, and yet still need to be
18640 associated to the base file.
18641
18642 To determine the base file name, we scan the macro definitions until we
18643 reach the first DW_MACINFO_start_file entry. We then initialize
18644 CURRENT_FILE accordingly so that any macro definition found before the
18645 first DW_MACINFO_start_file can still be associated to the base file. */
18646
18647 mac_ptr = section->buffer + offset;
18648 mac_end = section->buffer + section->size;
18649
18650 mac_ptr = dwarf_parse_macro_header (opcode_definitions, abfd, mac_ptr,
18651 &offset_size, section_is_gnu);
18652 if (mac_ptr == NULL)
18653 {
18654 /* We already issued a complaint. */
18655 return;
18656 }
18657
18658 do
18659 {
18660 /* Do we at least have room for a macinfo type byte? */
18661 if (mac_ptr >= mac_end)
18662 {
18663 /* Complaint is printed during the second pass as GDB will probably
18664 stop the first pass earlier upon finding
18665 DW_MACINFO_start_file. */
18666 break;
18667 }
18668
18669 macinfo_type = read_1_byte (abfd, mac_ptr);
18670 mac_ptr++;
18671
18672 /* Note that we rely on the fact that the corresponding GNU and
18673 DWARF constants are the same. */
18674 switch (macinfo_type)
18675 {
18676 /* A zero macinfo type indicates the end of the macro
18677 information. */
18678 case 0:
18679 break;
18680
18681 case DW_MACRO_GNU_define:
18682 case DW_MACRO_GNU_undef:
18683 /* Only skip the data by MAC_PTR. */
18684 {
18685 unsigned int bytes_read;
18686
18687 read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
18688 mac_ptr += bytes_read;
18689 read_direct_string (abfd, mac_ptr, &bytes_read);
18690 mac_ptr += bytes_read;
18691 }
18692 break;
18693
18694 case DW_MACRO_GNU_start_file:
18695 {
18696 unsigned int bytes_read;
18697 int line, file;
18698
18699 line = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
18700 mac_ptr += bytes_read;
18701 file = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
18702 mac_ptr += bytes_read;
18703
18704 current_file = macro_start_file (file, line, current_file,
bb5ed363 18705 comp_dir, lh, objfile);
cf2c3c16
TT
18706 }
18707 break;
18708
18709 case DW_MACRO_GNU_end_file:
18710 /* No data to skip by MAC_PTR. */
18711 break;
18712
18713 case DW_MACRO_GNU_define_indirect:
18714 case DW_MACRO_GNU_undef_indirect:
f7a35f02
TT
18715 case DW_MACRO_GNU_define_indirect_alt:
18716 case DW_MACRO_GNU_undef_indirect_alt:
cf2c3c16
TT
18717 {
18718 unsigned int bytes_read;
18719
18720 read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
18721 mac_ptr += bytes_read;
18722 mac_ptr += offset_size;
18723 }
18724 break;
18725
18726 case DW_MACRO_GNU_transparent_include:
f7a35f02 18727 case DW_MACRO_GNU_transparent_include_alt:
cf2c3c16
TT
18728 /* Note that, according to the spec, a transparent include
18729 chain cannot call DW_MACRO_GNU_start_file. So, we can just
18730 skip this opcode. */
18731 mac_ptr += offset_size;
18732 break;
18733
18734 case DW_MACINFO_vendor_ext:
18735 /* Only skip the data by MAC_PTR. */
18736 if (!section_is_gnu)
18737 {
18738 unsigned int bytes_read;
18739
18740 read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
18741 mac_ptr += bytes_read;
18742 read_direct_string (abfd, mac_ptr, &bytes_read);
18743 mac_ptr += bytes_read;
18744 }
18745 /* FALLTHROUGH */
18746
18747 default:
18748 mac_ptr = skip_unknown_opcode (macinfo_type, opcode_definitions,
f664829e 18749 mac_ptr, mac_end, abfd, offset_size,
cf2c3c16
TT
18750 section);
18751 if (mac_ptr == NULL)
18752 return;
18753 break;
18754 }
18755 } while (macinfo_type != 0 && current_file == NULL);
18756
18757 /* Second pass: Process all entries.
18758
18759 Use the AT_COMMAND_LINE flag to determine whether we are still processing
18760 command-line macro definitions/undefinitions. This flag is unset when we
18761 reach the first DW_MACINFO_start_file entry. */
18762
8fc3fc34
TT
18763 include_hash = htab_create_alloc (1, htab_hash_pointer, htab_eq_pointer,
18764 NULL, xcalloc, xfree);
18765 cleanup = make_cleanup_htab_delete (include_hash);
18766 mac_ptr = section->buffer + offset;
18767 slot = htab_find_slot (include_hash, mac_ptr, INSERT);
18768 *slot = mac_ptr;
18769 dwarf_decode_macro_bytes (abfd, mac_ptr, mac_end,
36586728
TT
18770 current_file, lh, comp_dir, section,
18771 section_is_gnu, 0,
8fc3fc34
TT
18772 offset_size, objfile, include_hash);
18773 do_cleanups (cleanup);
cf2c3c16
TT
18774}
18775
8e19ed76 18776/* Check if the attribute's form is a DW_FORM_block*
0963b4bd 18777 if so return true else false. */
380bca97 18778
8e19ed76
PS
18779static int
18780attr_form_is_block (struct attribute *attr)
18781{
18782 return (attr == NULL ? 0 :
18783 attr->form == DW_FORM_block1
18784 || attr->form == DW_FORM_block2
18785 || attr->form == DW_FORM_block4
2dc7f7b3
TT
18786 || attr->form == DW_FORM_block
18787 || attr->form == DW_FORM_exprloc);
8e19ed76 18788}
4c2df51b 18789
c6a0999f
JB
18790/* Return non-zero if ATTR's value is a section offset --- classes
18791 lineptr, loclistptr, macptr or rangelistptr --- or zero, otherwise.
18792 You may use DW_UNSND (attr) to retrieve such offsets.
18793
18794 Section 7.5.4, "Attribute Encodings", explains that no attribute
18795 may have a value that belongs to more than one of these classes; it
18796 would be ambiguous if we did, because we use the same forms for all
18797 of them. */
380bca97 18798
3690dd37
JB
18799static int
18800attr_form_is_section_offset (struct attribute *attr)
18801{
18802 return (attr->form == DW_FORM_data4
2dc7f7b3
TT
18803 || attr->form == DW_FORM_data8
18804 || attr->form == DW_FORM_sec_offset);
3690dd37
JB
18805}
18806
3690dd37
JB
18807/* Return non-zero if ATTR's value falls in the 'constant' class, or
18808 zero otherwise. When this function returns true, you can apply
18809 dwarf2_get_attr_constant_value to it.
18810
18811 However, note that for some attributes you must check
18812 attr_form_is_section_offset before using this test. DW_FORM_data4
18813 and DW_FORM_data8 are members of both the constant class, and of
18814 the classes that contain offsets into other debug sections
18815 (lineptr, loclistptr, macptr or rangelistptr). The DWARF spec says
18816 that, if an attribute's can be either a constant or one of the
18817 section offset classes, DW_FORM_data4 and DW_FORM_data8 should be
18818 taken as section offsets, not constants. */
380bca97 18819
3690dd37
JB
18820static int
18821attr_form_is_constant (struct attribute *attr)
18822{
18823 switch (attr->form)
18824 {
18825 case DW_FORM_sdata:
18826 case DW_FORM_udata:
18827 case DW_FORM_data1:
18828 case DW_FORM_data2:
18829 case DW_FORM_data4:
18830 case DW_FORM_data8:
18831 return 1;
18832 default:
18833 return 0;
18834 }
18835}
18836
3019eac3
DE
18837/* Return the .debug_loc section to use for CU.
18838 For DWO files use .debug_loc.dwo. */
18839
18840static struct dwarf2_section_info *
18841cu_debug_loc_section (struct dwarf2_cu *cu)
18842{
18843 if (cu->dwo_unit)
18844 return &cu->dwo_unit->dwo_file->sections.loc;
18845 return &dwarf2_per_objfile->loc;
18846}
18847
8cf6f0b1
TT
18848/* A helper function that fills in a dwarf2_loclist_baton. */
18849
18850static void
18851fill_in_loclist_baton (struct dwarf2_cu *cu,
18852 struct dwarf2_loclist_baton *baton,
18853 struct attribute *attr)
18854{
3019eac3
DE
18855 struct dwarf2_section_info *section = cu_debug_loc_section (cu);
18856
18857 dwarf2_read_section (dwarf2_per_objfile->objfile, section);
8cf6f0b1
TT
18858
18859 baton->per_cu = cu->per_cu;
18860 gdb_assert (baton->per_cu);
18861 /* We don't know how long the location list is, but make sure we
18862 don't run off the edge of the section. */
3019eac3
DE
18863 baton->size = section->size - DW_UNSND (attr);
18864 baton->data = section->buffer + DW_UNSND (attr);
8cf6f0b1 18865 baton->base_address = cu->base_address;
f664829e 18866 baton->from_dwo = cu->dwo_unit != NULL;
8cf6f0b1
TT
18867}
18868
4c2df51b
DJ
18869static void
18870dwarf2_symbol_mark_computed (struct attribute *attr, struct symbol *sym,
e7c27a73 18871 struct dwarf2_cu *cu)
4c2df51b 18872{
bb5ed363 18873 struct objfile *objfile = dwarf2_per_objfile->objfile;
3019eac3 18874 struct dwarf2_section_info *section = cu_debug_loc_section (cu);
bb5ed363 18875
3690dd37 18876 if (attr_form_is_section_offset (attr)
3019eac3 18877 /* .debug_loc{,.dwo} may not exist at all, or the offset may be outside
99bcc461
DJ
18878 the section. If so, fall through to the complaint in the
18879 other branch. */
3019eac3 18880 && DW_UNSND (attr) < dwarf2_section_size (objfile, section))
4c2df51b 18881 {
0d53c4c4 18882 struct dwarf2_loclist_baton *baton;
4c2df51b 18883
bb5ed363 18884 baton = obstack_alloc (&objfile->objfile_obstack,
0d53c4c4 18885 sizeof (struct dwarf2_loclist_baton));
4c2df51b 18886
8cf6f0b1 18887 fill_in_loclist_baton (cu, baton, attr);
be391dca 18888
d00adf39 18889 if (cu->base_known == 0)
0d53c4c4 18890 complaint (&symfile_complaints,
3e43a32a
MS
18891 _("Location list used without "
18892 "specifying the CU base address."));
4c2df51b 18893
768a979c 18894 SYMBOL_COMPUTED_OPS (sym) = &dwarf2_loclist_funcs;
0d53c4c4
DJ
18895 SYMBOL_LOCATION_BATON (sym) = baton;
18896 }
18897 else
18898 {
18899 struct dwarf2_locexpr_baton *baton;
18900
bb5ed363 18901 baton = obstack_alloc (&objfile->objfile_obstack,
0d53c4c4 18902 sizeof (struct dwarf2_locexpr_baton));
ae0d2f24
UW
18903 baton->per_cu = cu->per_cu;
18904 gdb_assert (baton->per_cu);
0d53c4c4
DJ
18905
18906 if (attr_form_is_block (attr))
18907 {
18908 /* Note that we're just copying the block's data pointer
18909 here, not the actual data. We're still pointing into the
6502dd73
DJ
18910 info_buffer for SYM's objfile; right now we never release
18911 that buffer, but when we do clean up properly this may
18912 need to change. */
0d53c4c4
DJ
18913 baton->size = DW_BLOCK (attr)->size;
18914 baton->data = DW_BLOCK (attr)->data;
18915 }
18916 else
18917 {
18918 dwarf2_invalid_attrib_class_complaint ("location description",
18919 SYMBOL_NATURAL_NAME (sym));
18920 baton->size = 0;
0d53c4c4 18921 }
6e70227d 18922
768a979c 18923 SYMBOL_COMPUTED_OPS (sym) = &dwarf2_locexpr_funcs;
0d53c4c4
DJ
18924 SYMBOL_LOCATION_BATON (sym) = baton;
18925 }
4c2df51b 18926}
6502dd73 18927
9aa1f1e3
TT
18928/* Return the OBJFILE associated with the compilation unit CU. If CU
18929 came from a separate debuginfo file, then the master objfile is
18930 returned. */
ae0d2f24
UW
18931
18932struct objfile *
18933dwarf2_per_cu_objfile (struct dwarf2_per_cu_data *per_cu)
18934{
9291a0cd 18935 struct objfile *objfile = per_cu->objfile;
ae0d2f24
UW
18936
18937 /* Return the master objfile, so that we can report and look up the
18938 correct file containing this variable. */
18939 if (objfile->separate_debug_objfile_backlink)
18940 objfile = objfile->separate_debug_objfile_backlink;
18941
18942 return objfile;
18943}
18944
96408a79
SA
18945/* Return comp_unit_head for PER_CU, either already available in PER_CU->CU
18946 (CU_HEADERP is unused in such case) or prepare a temporary copy at
18947 CU_HEADERP first. */
18948
18949static const struct comp_unit_head *
18950per_cu_header_read_in (struct comp_unit_head *cu_headerp,
18951 struct dwarf2_per_cu_data *per_cu)
18952{
96408a79
SA
18953 gdb_byte *info_ptr;
18954
18955 if (per_cu->cu)
18956 return &per_cu->cu->header;
18957
0bc3a05c 18958 info_ptr = per_cu->info_or_types_section->buffer + per_cu->offset.sect_off;
96408a79
SA
18959
18960 memset (cu_headerp, 0, sizeof (*cu_headerp));
0bc3a05c 18961 read_comp_unit_head (cu_headerp, info_ptr, per_cu->objfile->obfd);
96408a79
SA
18962
18963 return cu_headerp;
18964}
18965
ae0d2f24
UW
18966/* Return the address size given in the compilation unit header for CU. */
18967
98714339 18968int
ae0d2f24
UW
18969dwarf2_per_cu_addr_size (struct dwarf2_per_cu_data *per_cu)
18970{
96408a79
SA
18971 struct comp_unit_head cu_header_local;
18972 const struct comp_unit_head *cu_headerp;
c471e790 18973
96408a79
SA
18974 cu_headerp = per_cu_header_read_in (&cu_header_local, per_cu);
18975
18976 return cu_headerp->addr_size;
ae0d2f24
UW
18977}
18978
9eae7c52
TT
18979/* Return the offset size given in the compilation unit header for CU. */
18980
18981int
18982dwarf2_per_cu_offset_size (struct dwarf2_per_cu_data *per_cu)
18983{
96408a79
SA
18984 struct comp_unit_head cu_header_local;
18985 const struct comp_unit_head *cu_headerp;
9c6c53f7 18986
96408a79
SA
18987 cu_headerp = per_cu_header_read_in (&cu_header_local, per_cu);
18988
18989 return cu_headerp->offset_size;
18990}
18991
18992/* See its dwarf2loc.h declaration. */
18993
18994int
18995dwarf2_per_cu_ref_addr_size (struct dwarf2_per_cu_data *per_cu)
18996{
18997 struct comp_unit_head cu_header_local;
18998 const struct comp_unit_head *cu_headerp;
18999
19000 cu_headerp = per_cu_header_read_in (&cu_header_local, per_cu);
19001
19002 if (cu_headerp->version == 2)
19003 return cu_headerp->addr_size;
19004 else
19005 return cu_headerp->offset_size;
181cebd4
JK
19006}
19007
9aa1f1e3
TT
19008/* Return the text offset of the CU. The returned offset comes from
19009 this CU's objfile. If this objfile came from a separate debuginfo
19010 file, then the offset may be different from the corresponding
19011 offset in the parent objfile. */
19012
19013CORE_ADDR
19014dwarf2_per_cu_text_offset (struct dwarf2_per_cu_data *per_cu)
19015{
bb3fa9d0 19016 struct objfile *objfile = per_cu->objfile;
9aa1f1e3
TT
19017
19018 return ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
19019}
19020
348e048f
DE
19021/* Locate the .debug_info compilation unit from CU's objfile which contains
19022 the DIE at OFFSET. Raises an error on failure. */
ae038cb0
DJ
19023
19024static struct dwarf2_per_cu_data *
b64f50a1 19025dwarf2_find_containing_comp_unit (sect_offset offset,
36586728 19026 unsigned int offset_in_dwz,
ae038cb0
DJ
19027 struct objfile *objfile)
19028{
19029 struct dwarf2_per_cu_data *this_cu;
19030 int low, high;
36586728 19031 const sect_offset *cu_off;
ae038cb0 19032
ae038cb0
DJ
19033 low = 0;
19034 high = dwarf2_per_objfile->n_comp_units - 1;
19035 while (high > low)
19036 {
36586728 19037 struct dwarf2_per_cu_data *mid_cu;
ae038cb0 19038 int mid = low + (high - low) / 2;
9a619af0 19039
36586728
TT
19040 mid_cu = dwarf2_per_objfile->all_comp_units[mid];
19041 cu_off = &mid_cu->offset;
19042 if (mid_cu->is_dwz > offset_in_dwz
19043 || (mid_cu->is_dwz == offset_in_dwz
19044 && cu_off->sect_off >= offset.sect_off))
ae038cb0
DJ
19045 high = mid;
19046 else
19047 low = mid + 1;
19048 }
19049 gdb_assert (low == high);
36586728
TT
19050 this_cu = dwarf2_per_objfile->all_comp_units[low];
19051 cu_off = &this_cu->offset;
19052 if (this_cu->is_dwz != offset_in_dwz || cu_off->sect_off > offset.sect_off)
ae038cb0 19053 {
36586728 19054 if (low == 0 || this_cu->is_dwz != offset_in_dwz)
8a3fe4f8
AC
19055 error (_("Dwarf Error: could not find partial DIE containing "
19056 "offset 0x%lx [in module %s]"),
b64f50a1 19057 (long) offset.sect_off, bfd_get_filename (objfile->obfd));
10b3939b 19058
b64f50a1
JK
19059 gdb_assert (dwarf2_per_objfile->all_comp_units[low-1]->offset.sect_off
19060 <= offset.sect_off);
ae038cb0
DJ
19061 return dwarf2_per_objfile->all_comp_units[low-1];
19062 }
19063 else
19064 {
19065 this_cu = dwarf2_per_objfile->all_comp_units[low];
19066 if (low == dwarf2_per_objfile->n_comp_units - 1
b64f50a1
JK
19067 && offset.sect_off >= this_cu->offset.sect_off + this_cu->length)
19068 error (_("invalid dwarf2 offset %u"), offset.sect_off);
19069 gdb_assert (offset.sect_off < this_cu->offset.sect_off + this_cu->length);
ae038cb0
DJ
19070 return this_cu;
19071 }
19072}
19073
23745b47 19074/* Initialize dwarf2_cu CU, owned by PER_CU. */
93311388 19075
9816fde3 19076static void
23745b47 19077init_one_comp_unit (struct dwarf2_cu *cu, struct dwarf2_per_cu_data *per_cu)
93311388 19078{
9816fde3 19079 memset (cu, 0, sizeof (*cu));
23745b47
DE
19080 per_cu->cu = cu;
19081 cu->per_cu = per_cu;
19082 cu->objfile = per_cu->objfile;
93311388 19083 obstack_init (&cu->comp_unit_obstack);
9816fde3
JK
19084}
19085
19086/* Initialize basic fields of dwarf_cu CU according to DIE COMP_UNIT_DIE. */
19087
19088static void
95554aad
TT
19089prepare_one_comp_unit (struct dwarf2_cu *cu, struct die_info *comp_unit_die,
19090 enum language pretend_language)
9816fde3
JK
19091{
19092 struct attribute *attr;
19093
19094 /* Set the language we're debugging. */
19095 attr = dwarf2_attr (comp_unit_die, DW_AT_language, cu);
19096 if (attr)
19097 set_cu_language (DW_UNSND (attr), cu);
19098 else
9cded63f 19099 {
95554aad 19100 cu->language = pretend_language;
9cded63f
TT
19101 cu->language_defn = language_def (cu->language);
19102 }
dee91e82
DE
19103
19104 attr = dwarf2_attr (comp_unit_die, DW_AT_producer, cu);
19105 if (attr)
19106 cu->producer = DW_STRING (attr);
93311388
DE
19107}
19108
ae038cb0
DJ
19109/* Release one cached compilation unit, CU. We unlink it from the tree
19110 of compilation units, but we don't remove it from the read_in_chain;
93311388
DE
19111 the caller is responsible for that.
19112 NOTE: DATA is a void * because this function is also used as a
19113 cleanup routine. */
ae038cb0
DJ
19114
19115static void
68dc6402 19116free_heap_comp_unit (void *data)
ae038cb0
DJ
19117{
19118 struct dwarf2_cu *cu = data;
19119
23745b47
DE
19120 gdb_assert (cu->per_cu != NULL);
19121 cu->per_cu->cu = NULL;
ae038cb0
DJ
19122 cu->per_cu = NULL;
19123
19124 obstack_free (&cu->comp_unit_obstack, NULL);
19125
19126 xfree (cu);
19127}
19128
72bf9492 19129/* This cleanup function is passed the address of a dwarf2_cu on the stack
ae038cb0 19130 when we're finished with it. We can't free the pointer itself, but be
dee91e82 19131 sure to unlink it from the cache. Also release any associated storage. */
72bf9492
DJ
19132
19133static void
19134free_stack_comp_unit (void *data)
19135{
19136 struct dwarf2_cu *cu = data;
19137
23745b47
DE
19138 gdb_assert (cu->per_cu != NULL);
19139 cu->per_cu->cu = NULL;
19140 cu->per_cu = NULL;
19141
72bf9492
DJ
19142 obstack_free (&cu->comp_unit_obstack, NULL);
19143 cu->partial_dies = NULL;
ae038cb0
DJ
19144}
19145
19146/* Free all cached compilation units. */
19147
19148static void
19149free_cached_comp_units (void *data)
19150{
19151 struct dwarf2_per_cu_data *per_cu, **last_chain;
19152
19153 per_cu = dwarf2_per_objfile->read_in_chain;
19154 last_chain = &dwarf2_per_objfile->read_in_chain;
19155 while (per_cu != NULL)
19156 {
19157 struct dwarf2_per_cu_data *next_cu;
19158
19159 next_cu = per_cu->cu->read_in_chain;
19160
68dc6402 19161 free_heap_comp_unit (per_cu->cu);
ae038cb0
DJ
19162 *last_chain = next_cu;
19163
19164 per_cu = next_cu;
19165 }
19166}
19167
19168/* Increase the age counter on each cached compilation unit, and free
19169 any that are too old. */
19170
19171static void
19172age_cached_comp_units (void)
19173{
19174 struct dwarf2_per_cu_data *per_cu, **last_chain;
19175
19176 dwarf2_clear_marks (dwarf2_per_objfile->read_in_chain);
19177 per_cu = dwarf2_per_objfile->read_in_chain;
19178 while (per_cu != NULL)
19179 {
19180 per_cu->cu->last_used ++;
19181 if (per_cu->cu->last_used <= dwarf2_max_cache_age)
19182 dwarf2_mark (per_cu->cu);
19183 per_cu = per_cu->cu->read_in_chain;
19184 }
19185
19186 per_cu = dwarf2_per_objfile->read_in_chain;
19187 last_chain = &dwarf2_per_objfile->read_in_chain;
19188 while (per_cu != NULL)
19189 {
19190 struct dwarf2_per_cu_data *next_cu;
19191
19192 next_cu = per_cu->cu->read_in_chain;
19193
19194 if (!per_cu->cu->mark)
19195 {
68dc6402 19196 free_heap_comp_unit (per_cu->cu);
ae038cb0
DJ
19197 *last_chain = next_cu;
19198 }
19199 else
19200 last_chain = &per_cu->cu->read_in_chain;
19201
19202 per_cu = next_cu;
19203 }
19204}
19205
19206/* Remove a single compilation unit from the cache. */
19207
19208static void
dee91e82 19209free_one_cached_comp_unit (struct dwarf2_per_cu_data *target_per_cu)
ae038cb0
DJ
19210{
19211 struct dwarf2_per_cu_data *per_cu, **last_chain;
19212
19213 per_cu = dwarf2_per_objfile->read_in_chain;
19214 last_chain = &dwarf2_per_objfile->read_in_chain;
19215 while (per_cu != NULL)
19216 {
19217 struct dwarf2_per_cu_data *next_cu;
19218
19219 next_cu = per_cu->cu->read_in_chain;
19220
dee91e82 19221 if (per_cu == target_per_cu)
ae038cb0 19222 {
68dc6402 19223 free_heap_comp_unit (per_cu->cu);
dee91e82 19224 per_cu->cu = NULL;
ae038cb0
DJ
19225 *last_chain = next_cu;
19226 break;
19227 }
19228 else
19229 last_chain = &per_cu->cu->read_in_chain;
19230
19231 per_cu = next_cu;
19232 }
19233}
19234
fe3e1990
DJ
19235/* Release all extra memory associated with OBJFILE. */
19236
19237void
19238dwarf2_free_objfile (struct objfile *objfile)
19239{
19240 dwarf2_per_objfile = objfile_data (objfile, dwarf2_objfile_data_key);
19241
19242 if (dwarf2_per_objfile == NULL)
19243 return;
19244
19245 /* Cached DIE trees use xmalloc and the comp_unit_obstack. */
19246 free_cached_comp_units (NULL);
19247
7b9f3c50
DE
19248 if (dwarf2_per_objfile->quick_file_names_table)
19249 htab_delete (dwarf2_per_objfile->quick_file_names_table);
9291a0cd 19250
fe3e1990
DJ
19251 /* Everything else should be on the objfile obstack. */
19252}
19253
dee91e82
DE
19254/* A set of CU "per_cu" pointer, DIE offset, and GDB type pointer.
19255 We store these in a hash table separate from the DIEs, and preserve them
19256 when the DIEs are flushed out of cache.
19257
19258 The CU "per_cu" pointer is needed because offset alone is not enough to
3019eac3
DE
19259 uniquely identify the type. A file may have multiple .debug_types sections,
19260 or the type may come from a DWO file. We have to use something in
19261 dwarf2_per_cu_data (or the pointer to it) because we can enter the lookup
19262 routine, get_die_type_at_offset, from outside this file, and thus won't
19263 necessarily have PER_CU->cu. Fortunately, PER_CU is stable for the life
19264 of the objfile. */
1c379e20 19265
dee91e82 19266struct dwarf2_per_cu_offset_and_type
1c379e20 19267{
dee91e82 19268 const struct dwarf2_per_cu_data *per_cu;
b64f50a1 19269 sect_offset offset;
1c379e20
DJ
19270 struct type *type;
19271};
19272
dee91e82 19273/* Hash function for a dwarf2_per_cu_offset_and_type. */
1c379e20
DJ
19274
19275static hashval_t
dee91e82 19276per_cu_offset_and_type_hash (const void *item)
1c379e20 19277{
dee91e82 19278 const struct dwarf2_per_cu_offset_and_type *ofs = item;
9a619af0 19279
dee91e82 19280 return (uintptr_t) ofs->per_cu + ofs->offset.sect_off;
1c379e20
DJ
19281}
19282
dee91e82 19283/* Equality function for a dwarf2_per_cu_offset_and_type. */
1c379e20
DJ
19284
19285static int
dee91e82 19286per_cu_offset_and_type_eq (const void *item_lhs, const void *item_rhs)
1c379e20 19287{
dee91e82
DE
19288 const struct dwarf2_per_cu_offset_and_type *ofs_lhs = item_lhs;
19289 const struct dwarf2_per_cu_offset_and_type *ofs_rhs = item_rhs;
9a619af0 19290
dee91e82
DE
19291 return (ofs_lhs->per_cu == ofs_rhs->per_cu
19292 && ofs_lhs->offset.sect_off == ofs_rhs->offset.sect_off);
1c379e20
DJ
19293}
19294
19295/* Set the type associated with DIE to TYPE. Save it in CU's hash
7e314c57
JK
19296 table if necessary. For convenience, return TYPE.
19297
19298 The DIEs reading must have careful ordering to:
19299 * Not cause infite loops trying to read in DIEs as a prerequisite for
19300 reading current DIE.
19301 * Not trying to dereference contents of still incompletely read in types
19302 while reading in other DIEs.
19303 * Enable referencing still incompletely read in types just by a pointer to
19304 the type without accessing its fields.
19305
19306 Therefore caller should follow these rules:
19307 * Try to fetch any prerequisite types we may need to build this DIE type
19308 before building the type and calling set_die_type.
e71ec853 19309 * After building type call set_die_type for current DIE as soon as
7e314c57
JK
19310 possible before fetching more types to complete the current type.
19311 * Make the type as complete as possible before fetching more types. */
1c379e20 19312
f792889a 19313static struct type *
1c379e20
DJ
19314set_die_type (struct die_info *die, struct type *type, struct dwarf2_cu *cu)
19315{
dee91e82 19316 struct dwarf2_per_cu_offset_and_type **slot, ofs;
673bfd45 19317 struct objfile *objfile = cu->objfile;
1c379e20 19318
b4ba55a1
JB
19319 /* For Ada types, make sure that the gnat-specific data is always
19320 initialized (if not already set). There are a few types where
19321 we should not be doing so, because the type-specific area is
19322 already used to hold some other piece of info (eg: TYPE_CODE_FLT
19323 where the type-specific area is used to store the floatformat).
19324 But this is not a problem, because the gnat-specific information
19325 is actually not needed for these types. */
19326 if (need_gnat_info (cu)
19327 && TYPE_CODE (type) != TYPE_CODE_FUNC
19328 && TYPE_CODE (type) != TYPE_CODE_FLT
19329 && !HAVE_GNAT_AUX_INFO (type))
19330 INIT_GNAT_SPECIFIC (type);
19331
dee91e82 19332 if (dwarf2_per_objfile->die_type_hash == NULL)
f792889a 19333 {
dee91e82
DE
19334 dwarf2_per_objfile->die_type_hash =
19335 htab_create_alloc_ex (127,
19336 per_cu_offset_and_type_hash,
19337 per_cu_offset_and_type_eq,
19338 NULL,
19339 &objfile->objfile_obstack,
19340 hashtab_obstack_allocate,
19341 dummy_obstack_deallocate);
f792889a 19342 }
1c379e20 19343
dee91e82 19344 ofs.per_cu = cu->per_cu;
1c379e20
DJ
19345 ofs.offset = die->offset;
19346 ofs.type = type;
dee91e82
DE
19347 slot = (struct dwarf2_per_cu_offset_and_type **)
19348 htab_find_slot (dwarf2_per_objfile->die_type_hash, &ofs, INSERT);
7e314c57
JK
19349 if (*slot)
19350 complaint (&symfile_complaints,
19351 _("A problem internal to GDB: DIE 0x%x has type already set"),
b64f50a1 19352 die->offset.sect_off);
673bfd45 19353 *slot = obstack_alloc (&objfile->objfile_obstack, sizeof (**slot));
1c379e20 19354 **slot = ofs;
f792889a 19355 return type;
1c379e20
DJ
19356}
19357
380bca97 19358/* Look up the type for the die at OFFSET in the appropriate type_hash
673bfd45 19359 table, or return NULL if the die does not have a saved type. */
1c379e20
DJ
19360
19361static struct type *
b64f50a1 19362get_die_type_at_offset (sect_offset offset,
673bfd45 19363 struct dwarf2_per_cu_data *per_cu)
1c379e20 19364{
dee91e82 19365 struct dwarf2_per_cu_offset_and_type *slot, ofs;
f792889a 19366
dee91e82 19367 if (dwarf2_per_objfile->die_type_hash == NULL)
f792889a 19368 return NULL;
1c379e20 19369
dee91e82 19370 ofs.per_cu = per_cu;
673bfd45 19371 ofs.offset = offset;
dee91e82 19372 slot = htab_find (dwarf2_per_objfile->die_type_hash, &ofs);
1c379e20
DJ
19373 if (slot)
19374 return slot->type;
19375 else
19376 return NULL;
19377}
19378
673bfd45
DE
19379/* Look up the type for DIE in the appropriate type_hash table,
19380 or return NULL if DIE does not have a saved type. */
19381
19382static struct type *
19383get_die_type (struct die_info *die, struct dwarf2_cu *cu)
19384{
19385 return get_die_type_at_offset (die->offset, cu->per_cu);
19386}
19387
10b3939b
DJ
19388/* Add a dependence relationship from CU to REF_PER_CU. */
19389
19390static void
19391dwarf2_add_dependence (struct dwarf2_cu *cu,
19392 struct dwarf2_per_cu_data *ref_per_cu)
19393{
19394 void **slot;
19395
19396 if (cu->dependencies == NULL)
19397 cu->dependencies
19398 = htab_create_alloc_ex (5, htab_hash_pointer, htab_eq_pointer,
19399 NULL, &cu->comp_unit_obstack,
19400 hashtab_obstack_allocate,
19401 dummy_obstack_deallocate);
19402
19403 slot = htab_find_slot (cu->dependencies, ref_per_cu, INSERT);
19404 if (*slot == NULL)
19405 *slot = ref_per_cu;
19406}
1c379e20 19407
f504f079
DE
19408/* Subroutine of dwarf2_mark to pass to htab_traverse.
19409 Set the mark field in every compilation unit in the
ae038cb0
DJ
19410 cache that we must keep because we are keeping CU. */
19411
10b3939b
DJ
19412static int
19413dwarf2_mark_helper (void **slot, void *data)
19414{
19415 struct dwarf2_per_cu_data *per_cu;
19416
19417 per_cu = (struct dwarf2_per_cu_data *) *slot;
d07ed419
JK
19418
19419 /* cu->dependencies references may not yet have been ever read if QUIT aborts
19420 reading of the chain. As such dependencies remain valid it is not much
19421 useful to track and undo them during QUIT cleanups. */
19422 if (per_cu->cu == NULL)
19423 return 1;
19424
10b3939b
DJ
19425 if (per_cu->cu->mark)
19426 return 1;
19427 per_cu->cu->mark = 1;
19428
19429 if (per_cu->cu->dependencies != NULL)
19430 htab_traverse (per_cu->cu->dependencies, dwarf2_mark_helper, NULL);
19431
19432 return 1;
19433}
19434
f504f079
DE
19435/* Set the mark field in CU and in every other compilation unit in the
19436 cache that we must keep because we are keeping CU. */
19437
ae038cb0
DJ
19438static void
19439dwarf2_mark (struct dwarf2_cu *cu)
19440{
19441 if (cu->mark)
19442 return;
19443 cu->mark = 1;
10b3939b
DJ
19444 if (cu->dependencies != NULL)
19445 htab_traverse (cu->dependencies, dwarf2_mark_helper, NULL);
ae038cb0
DJ
19446}
19447
19448static void
19449dwarf2_clear_marks (struct dwarf2_per_cu_data *per_cu)
19450{
19451 while (per_cu)
19452 {
19453 per_cu->cu->mark = 0;
19454 per_cu = per_cu->cu->read_in_chain;
19455 }
72bf9492
DJ
19456}
19457
72bf9492
DJ
19458/* Trivial hash function for partial_die_info: the hash value of a DIE
19459 is its offset in .debug_info for this objfile. */
19460
19461static hashval_t
19462partial_die_hash (const void *item)
19463{
19464 const struct partial_die_info *part_die = item;
9a619af0 19465
b64f50a1 19466 return part_die->offset.sect_off;
72bf9492
DJ
19467}
19468
19469/* Trivial comparison function for partial_die_info structures: two DIEs
19470 are equal if they have the same offset. */
19471
19472static int
19473partial_die_eq (const void *item_lhs, const void *item_rhs)
19474{
19475 const struct partial_die_info *part_die_lhs = item_lhs;
19476 const struct partial_die_info *part_die_rhs = item_rhs;
9a619af0 19477
b64f50a1 19478 return part_die_lhs->offset.sect_off == part_die_rhs->offset.sect_off;
72bf9492
DJ
19479}
19480
ae038cb0
DJ
19481static struct cmd_list_element *set_dwarf2_cmdlist;
19482static struct cmd_list_element *show_dwarf2_cmdlist;
19483
19484static void
19485set_dwarf2_cmd (char *args, int from_tty)
19486{
19487 help_list (set_dwarf2_cmdlist, "maintenance set dwarf2 ", -1, gdb_stdout);
19488}
19489
19490static void
19491show_dwarf2_cmd (char *args, int from_tty)
6e70227d 19492{
ae038cb0
DJ
19493 cmd_show_list (show_dwarf2_cmdlist, from_tty, "");
19494}
19495
4bf44c1c 19496/* Free data associated with OBJFILE, if necessary. */
dce234bc
PP
19497
19498static void
c1bd65d0 19499dwarf2_per_objfile_free (struct objfile *objfile, void *d)
dce234bc
PP
19500{
19501 struct dwarf2_per_objfile *data = d;
8b70b953 19502 int ix;
8b70b953 19503
95554aad
TT
19504 for (ix = 0; ix < dwarf2_per_objfile->n_comp_units; ++ix)
19505 VEC_free (dwarf2_per_cu_ptr,
f4dc4d17 19506 dwarf2_per_objfile->all_comp_units[ix]->s.imported_symtabs);
95554aad 19507
8b70b953 19508 VEC_free (dwarf2_section_info_def, data->types);
3019eac3
DE
19509
19510 if (data->dwo_files)
19511 free_dwo_files (data->dwo_files, objfile);
36586728
TT
19512
19513 if (data->dwz_file && data->dwz_file->dwz_bfd)
19514 gdb_bfd_unref (data->dwz_file->dwz_bfd);
9291a0cd
TT
19515}
19516
19517\f
ae2de4f8 19518/* The "save gdb-index" command. */
9291a0cd
TT
19519
19520/* The contents of the hash table we create when building the string
19521 table. */
19522struct strtab_entry
19523{
19524 offset_type offset;
19525 const char *str;
19526};
19527
559a7a62
JK
19528/* Hash function for a strtab_entry.
19529
19530 Function is used only during write_hash_table so no index format backward
19531 compatibility is needed. */
b89be57b 19532
9291a0cd
TT
19533static hashval_t
19534hash_strtab_entry (const void *e)
19535{
19536 const struct strtab_entry *entry = e;
559a7a62 19537 return mapped_index_string_hash (INT_MAX, entry->str);
9291a0cd
TT
19538}
19539
19540/* Equality function for a strtab_entry. */
b89be57b 19541
9291a0cd
TT
19542static int
19543eq_strtab_entry (const void *a, const void *b)
19544{
19545 const struct strtab_entry *ea = a;
19546 const struct strtab_entry *eb = b;
19547 return !strcmp (ea->str, eb->str);
19548}
19549
19550/* Create a strtab_entry hash table. */
b89be57b 19551
9291a0cd
TT
19552static htab_t
19553create_strtab (void)
19554{
19555 return htab_create_alloc (100, hash_strtab_entry, eq_strtab_entry,
19556 xfree, xcalloc, xfree);
19557}
19558
19559/* Add a string to the constant pool. Return the string's offset in
19560 host order. */
b89be57b 19561
9291a0cd
TT
19562static offset_type
19563add_string (htab_t table, struct obstack *cpool, const char *str)
19564{
19565 void **slot;
19566 struct strtab_entry entry;
19567 struct strtab_entry *result;
19568
19569 entry.str = str;
19570 slot = htab_find_slot (table, &entry, INSERT);
19571 if (*slot)
19572 result = *slot;
19573 else
19574 {
19575 result = XNEW (struct strtab_entry);
19576 result->offset = obstack_object_size (cpool);
19577 result->str = str;
19578 obstack_grow_str0 (cpool, str);
19579 *slot = result;
19580 }
19581 return result->offset;
19582}
19583
19584/* An entry in the symbol table. */
19585struct symtab_index_entry
19586{
19587 /* The name of the symbol. */
19588 const char *name;
19589 /* The offset of the name in the constant pool. */
19590 offset_type index_offset;
19591 /* A sorted vector of the indices of all the CUs that hold an object
19592 of this name. */
19593 VEC (offset_type) *cu_indices;
19594};
19595
19596/* The symbol table. This is a power-of-2-sized hash table. */
19597struct mapped_symtab
19598{
19599 offset_type n_elements;
19600 offset_type size;
19601 struct symtab_index_entry **data;
19602};
19603
19604/* Hash function for a symtab_index_entry. */
b89be57b 19605
9291a0cd
TT
19606static hashval_t
19607hash_symtab_entry (const void *e)
19608{
19609 const struct symtab_index_entry *entry = e;
19610 return iterative_hash (VEC_address (offset_type, entry->cu_indices),
19611 sizeof (offset_type) * VEC_length (offset_type,
19612 entry->cu_indices),
19613 0);
19614}
19615
19616/* Equality function for a symtab_index_entry. */
b89be57b 19617
9291a0cd
TT
19618static int
19619eq_symtab_entry (const void *a, const void *b)
19620{
19621 const struct symtab_index_entry *ea = a;
19622 const struct symtab_index_entry *eb = b;
19623 int len = VEC_length (offset_type, ea->cu_indices);
19624 if (len != VEC_length (offset_type, eb->cu_indices))
19625 return 0;
19626 return !memcmp (VEC_address (offset_type, ea->cu_indices),
19627 VEC_address (offset_type, eb->cu_indices),
19628 sizeof (offset_type) * len);
19629}
19630
19631/* Destroy a symtab_index_entry. */
b89be57b 19632
9291a0cd
TT
19633static void
19634delete_symtab_entry (void *p)
19635{
19636 struct symtab_index_entry *entry = p;
19637 VEC_free (offset_type, entry->cu_indices);
19638 xfree (entry);
19639}
19640
19641/* Create a hash table holding symtab_index_entry objects. */
b89be57b 19642
9291a0cd 19643static htab_t
3876f04e 19644create_symbol_hash_table (void)
9291a0cd
TT
19645{
19646 return htab_create_alloc (100, hash_symtab_entry, eq_symtab_entry,
19647 delete_symtab_entry, xcalloc, xfree);
19648}
19649
19650/* Create a new mapped symtab object. */
b89be57b 19651
9291a0cd
TT
19652static struct mapped_symtab *
19653create_mapped_symtab (void)
19654{
19655 struct mapped_symtab *symtab = XNEW (struct mapped_symtab);
19656 symtab->n_elements = 0;
19657 symtab->size = 1024;
19658 symtab->data = XCNEWVEC (struct symtab_index_entry *, symtab->size);
19659 return symtab;
19660}
19661
19662/* Destroy a mapped_symtab. */
b89be57b 19663
9291a0cd
TT
19664static void
19665cleanup_mapped_symtab (void *p)
19666{
19667 struct mapped_symtab *symtab = p;
19668 /* The contents of the array are freed when the other hash table is
19669 destroyed. */
19670 xfree (symtab->data);
19671 xfree (symtab);
19672}
19673
19674/* Find a slot in SYMTAB for the symbol NAME. Returns a pointer to
559a7a62
JK
19675 the slot.
19676
19677 Function is used only during write_hash_table so no index format backward
19678 compatibility is needed. */
b89be57b 19679
9291a0cd
TT
19680static struct symtab_index_entry **
19681find_slot (struct mapped_symtab *symtab, const char *name)
19682{
559a7a62 19683 offset_type index, step, hash = mapped_index_string_hash (INT_MAX, name);
9291a0cd
TT
19684
19685 index = hash & (symtab->size - 1);
19686 step = ((hash * 17) & (symtab->size - 1)) | 1;
19687
19688 for (;;)
19689 {
19690 if (!symtab->data[index] || !strcmp (name, symtab->data[index]->name))
19691 return &symtab->data[index];
19692 index = (index + step) & (symtab->size - 1);
19693 }
19694}
19695
19696/* Expand SYMTAB's hash table. */
b89be57b 19697
9291a0cd
TT
19698static void
19699hash_expand (struct mapped_symtab *symtab)
19700{
19701 offset_type old_size = symtab->size;
19702 offset_type i;
19703 struct symtab_index_entry **old_entries = symtab->data;
19704
19705 symtab->size *= 2;
19706 symtab->data = XCNEWVEC (struct symtab_index_entry *, symtab->size);
19707
19708 for (i = 0; i < old_size; ++i)
19709 {
19710 if (old_entries[i])
19711 {
19712 struct symtab_index_entry **slot = find_slot (symtab,
19713 old_entries[i]->name);
19714 *slot = old_entries[i];
19715 }
19716 }
19717
19718 xfree (old_entries);
19719}
19720
156942c7
DE
19721/* Add an entry to SYMTAB. NAME is the name of the symbol.
19722 CU_INDEX is the index of the CU in which the symbol appears.
19723 IS_STATIC is one if the symbol is static, otherwise zero (global). */
b89be57b 19724
9291a0cd
TT
19725static void
19726add_index_entry (struct mapped_symtab *symtab, const char *name,
156942c7 19727 int is_static, gdb_index_symbol_kind kind,
9291a0cd
TT
19728 offset_type cu_index)
19729{
19730 struct symtab_index_entry **slot;
156942c7 19731 offset_type cu_index_and_attrs;
9291a0cd
TT
19732
19733 ++symtab->n_elements;
19734 if (4 * symtab->n_elements / 3 >= symtab->size)
19735 hash_expand (symtab);
19736
19737 slot = find_slot (symtab, name);
19738 if (!*slot)
19739 {
19740 *slot = XNEW (struct symtab_index_entry);
19741 (*slot)->name = name;
156942c7 19742 /* index_offset is set later. */
9291a0cd
TT
19743 (*slot)->cu_indices = NULL;
19744 }
156942c7
DE
19745
19746 cu_index_and_attrs = 0;
19747 DW2_GDB_INDEX_CU_SET_VALUE (cu_index_and_attrs, cu_index);
19748 DW2_GDB_INDEX_SYMBOL_STATIC_SET_VALUE (cu_index_and_attrs, is_static);
19749 DW2_GDB_INDEX_SYMBOL_KIND_SET_VALUE (cu_index_and_attrs, kind);
19750
19751 /* We don't want to record an index value twice as we want to avoid the
19752 duplication.
19753 We process all global symbols and then all static symbols
19754 (which would allow us to avoid the duplication by only having to check
19755 the last entry pushed), but a symbol could have multiple kinds in one CU.
19756 To keep things simple we don't worry about the duplication here and
19757 sort and uniqufy the list after we've processed all symbols. */
19758 VEC_safe_push (offset_type, (*slot)->cu_indices, cu_index_and_attrs);
19759}
19760
19761/* qsort helper routine for uniquify_cu_indices. */
19762
19763static int
19764offset_type_compare (const void *ap, const void *bp)
19765{
19766 offset_type a = *(offset_type *) ap;
19767 offset_type b = *(offset_type *) bp;
19768
19769 return (a > b) - (b > a);
19770}
19771
19772/* Sort and remove duplicates of all symbols' cu_indices lists. */
19773
19774static void
19775uniquify_cu_indices (struct mapped_symtab *symtab)
19776{
19777 int i;
19778
19779 for (i = 0; i < symtab->size; ++i)
19780 {
19781 struct symtab_index_entry *entry = symtab->data[i];
19782
19783 if (entry
19784 && entry->cu_indices != NULL)
19785 {
19786 unsigned int next_to_insert, next_to_check;
19787 offset_type last_value;
19788
19789 qsort (VEC_address (offset_type, entry->cu_indices),
19790 VEC_length (offset_type, entry->cu_indices),
19791 sizeof (offset_type), offset_type_compare);
19792
19793 last_value = VEC_index (offset_type, entry->cu_indices, 0);
19794 next_to_insert = 1;
19795 for (next_to_check = 1;
19796 next_to_check < VEC_length (offset_type, entry->cu_indices);
19797 ++next_to_check)
19798 {
19799 if (VEC_index (offset_type, entry->cu_indices, next_to_check)
19800 != last_value)
19801 {
19802 last_value = VEC_index (offset_type, entry->cu_indices,
19803 next_to_check);
19804 VEC_replace (offset_type, entry->cu_indices, next_to_insert,
19805 last_value);
19806 ++next_to_insert;
19807 }
19808 }
19809 VEC_truncate (offset_type, entry->cu_indices, next_to_insert);
19810 }
19811 }
9291a0cd
TT
19812}
19813
19814/* Add a vector of indices to the constant pool. */
b89be57b 19815
9291a0cd 19816static offset_type
3876f04e 19817add_indices_to_cpool (htab_t symbol_hash_table, struct obstack *cpool,
9291a0cd
TT
19818 struct symtab_index_entry *entry)
19819{
19820 void **slot;
19821
3876f04e 19822 slot = htab_find_slot (symbol_hash_table, entry, INSERT);
9291a0cd
TT
19823 if (!*slot)
19824 {
19825 offset_type len = VEC_length (offset_type, entry->cu_indices);
19826 offset_type val = MAYBE_SWAP (len);
19827 offset_type iter;
19828 int i;
19829
19830 *slot = entry;
19831 entry->index_offset = obstack_object_size (cpool);
19832
19833 obstack_grow (cpool, &val, sizeof (val));
19834 for (i = 0;
19835 VEC_iterate (offset_type, entry->cu_indices, i, iter);
19836 ++i)
19837 {
19838 val = MAYBE_SWAP (iter);
19839 obstack_grow (cpool, &val, sizeof (val));
19840 }
19841 }
19842 else
19843 {
19844 struct symtab_index_entry *old_entry = *slot;
19845 entry->index_offset = old_entry->index_offset;
19846 entry = old_entry;
19847 }
19848 return entry->index_offset;
19849}
19850
19851/* Write the mapped hash table SYMTAB to the obstack OUTPUT, with
19852 constant pool entries going into the obstack CPOOL. */
b89be57b 19853
9291a0cd
TT
19854static void
19855write_hash_table (struct mapped_symtab *symtab,
19856 struct obstack *output, struct obstack *cpool)
19857{
19858 offset_type i;
3876f04e 19859 htab_t symbol_hash_table;
9291a0cd
TT
19860 htab_t str_table;
19861
3876f04e 19862 symbol_hash_table = create_symbol_hash_table ();
9291a0cd 19863 str_table = create_strtab ();
3876f04e 19864
9291a0cd
TT
19865 /* We add all the index vectors to the constant pool first, to
19866 ensure alignment is ok. */
19867 for (i = 0; i < symtab->size; ++i)
19868 {
19869 if (symtab->data[i])
3876f04e 19870 add_indices_to_cpool (symbol_hash_table, cpool, symtab->data[i]);
9291a0cd
TT
19871 }
19872
19873 /* Now write out the hash table. */
19874 for (i = 0; i < symtab->size; ++i)
19875 {
19876 offset_type str_off, vec_off;
19877
19878 if (symtab->data[i])
19879 {
19880 str_off = add_string (str_table, cpool, symtab->data[i]->name);
19881 vec_off = symtab->data[i]->index_offset;
19882 }
19883 else
19884 {
19885 /* While 0 is a valid constant pool index, it is not valid
19886 to have 0 for both offsets. */
19887 str_off = 0;
19888 vec_off = 0;
19889 }
19890
19891 str_off = MAYBE_SWAP (str_off);
19892 vec_off = MAYBE_SWAP (vec_off);
19893
19894 obstack_grow (output, &str_off, sizeof (str_off));
19895 obstack_grow (output, &vec_off, sizeof (vec_off));
19896 }
19897
19898 htab_delete (str_table);
3876f04e 19899 htab_delete (symbol_hash_table);
9291a0cd
TT
19900}
19901
0a5429f6
DE
19902/* Struct to map psymtab to CU index in the index file. */
19903struct psymtab_cu_index_map
19904{
19905 struct partial_symtab *psymtab;
19906 unsigned int cu_index;
19907};
19908
19909static hashval_t
19910hash_psymtab_cu_index (const void *item)
19911{
19912 const struct psymtab_cu_index_map *map = item;
19913
19914 return htab_hash_pointer (map->psymtab);
19915}
19916
19917static int
19918eq_psymtab_cu_index (const void *item_lhs, const void *item_rhs)
19919{
19920 const struct psymtab_cu_index_map *lhs = item_lhs;
19921 const struct psymtab_cu_index_map *rhs = item_rhs;
19922
19923 return lhs->psymtab == rhs->psymtab;
19924}
19925
19926/* Helper struct for building the address table. */
19927struct addrmap_index_data
19928{
19929 struct objfile *objfile;
19930 struct obstack *addr_obstack;
19931 htab_t cu_index_htab;
19932
19933 /* Non-zero if the previous_* fields are valid.
19934 We can't write an entry until we see the next entry (since it is only then
19935 that we know the end of the entry). */
19936 int previous_valid;
19937 /* Index of the CU in the table of all CUs in the index file. */
19938 unsigned int previous_cu_index;
0963b4bd 19939 /* Start address of the CU. */
0a5429f6
DE
19940 CORE_ADDR previous_cu_start;
19941};
19942
19943/* Write an address entry to OBSTACK. */
b89be57b 19944
9291a0cd 19945static void
0a5429f6
DE
19946add_address_entry (struct objfile *objfile, struct obstack *obstack,
19947 CORE_ADDR start, CORE_ADDR end, unsigned int cu_index)
9291a0cd 19948{
0a5429f6 19949 offset_type cu_index_to_write;
9291a0cd
TT
19950 char addr[8];
19951 CORE_ADDR baseaddr;
19952
19953 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
19954
0a5429f6
DE
19955 store_unsigned_integer (addr, 8, BFD_ENDIAN_LITTLE, start - baseaddr);
19956 obstack_grow (obstack, addr, 8);
19957 store_unsigned_integer (addr, 8, BFD_ENDIAN_LITTLE, end - baseaddr);
19958 obstack_grow (obstack, addr, 8);
19959 cu_index_to_write = MAYBE_SWAP (cu_index);
19960 obstack_grow (obstack, &cu_index_to_write, sizeof (offset_type));
19961}
19962
19963/* Worker function for traversing an addrmap to build the address table. */
19964
19965static int
19966add_address_entry_worker (void *datap, CORE_ADDR start_addr, void *obj)
19967{
19968 struct addrmap_index_data *data = datap;
19969 struct partial_symtab *pst = obj;
0a5429f6
DE
19970
19971 if (data->previous_valid)
19972 add_address_entry (data->objfile, data->addr_obstack,
19973 data->previous_cu_start, start_addr,
19974 data->previous_cu_index);
19975
19976 data->previous_cu_start = start_addr;
19977 if (pst != NULL)
19978 {
19979 struct psymtab_cu_index_map find_map, *map;
19980 find_map.psymtab = pst;
19981 map = htab_find (data->cu_index_htab, &find_map);
19982 gdb_assert (map != NULL);
19983 data->previous_cu_index = map->cu_index;
19984 data->previous_valid = 1;
19985 }
19986 else
19987 data->previous_valid = 0;
19988
19989 return 0;
19990}
19991
19992/* Write OBJFILE's address map to OBSTACK.
19993 CU_INDEX_HTAB is used to map addrmap entries to their CU indices
19994 in the index file. */
19995
19996static void
19997write_address_map (struct objfile *objfile, struct obstack *obstack,
19998 htab_t cu_index_htab)
19999{
20000 struct addrmap_index_data addrmap_index_data;
20001
20002 /* When writing the address table, we have to cope with the fact that
20003 the addrmap iterator only provides the start of a region; we have to
20004 wait until the next invocation to get the start of the next region. */
20005
20006 addrmap_index_data.objfile = objfile;
20007 addrmap_index_data.addr_obstack = obstack;
20008 addrmap_index_data.cu_index_htab = cu_index_htab;
20009 addrmap_index_data.previous_valid = 0;
20010
20011 addrmap_foreach (objfile->psymtabs_addrmap, add_address_entry_worker,
20012 &addrmap_index_data);
20013
20014 /* It's highly unlikely the last entry (end address = 0xff...ff)
20015 is valid, but we should still handle it.
20016 The end address is recorded as the start of the next region, but that
20017 doesn't work here. To cope we pass 0xff...ff, this is a rare situation
20018 anyway. */
20019 if (addrmap_index_data.previous_valid)
20020 add_address_entry (objfile, obstack,
20021 addrmap_index_data.previous_cu_start, (CORE_ADDR) -1,
20022 addrmap_index_data.previous_cu_index);
9291a0cd
TT
20023}
20024
156942c7
DE
20025/* Return the symbol kind of PSYM. */
20026
20027static gdb_index_symbol_kind
20028symbol_kind (struct partial_symbol *psym)
20029{
20030 domain_enum domain = PSYMBOL_DOMAIN (psym);
20031 enum address_class aclass = PSYMBOL_CLASS (psym);
20032
20033 switch (domain)
20034 {
20035 case VAR_DOMAIN:
20036 switch (aclass)
20037 {
20038 case LOC_BLOCK:
20039 return GDB_INDEX_SYMBOL_KIND_FUNCTION;
20040 case LOC_TYPEDEF:
20041 return GDB_INDEX_SYMBOL_KIND_TYPE;
20042 case LOC_COMPUTED:
20043 case LOC_CONST_BYTES:
20044 case LOC_OPTIMIZED_OUT:
20045 case LOC_STATIC:
20046 return GDB_INDEX_SYMBOL_KIND_VARIABLE;
20047 case LOC_CONST:
20048 /* Note: It's currently impossible to recognize psyms as enum values
20049 short of reading the type info. For now punt. */
20050 return GDB_INDEX_SYMBOL_KIND_VARIABLE;
20051 default:
20052 /* There are other LOC_FOO values that one might want to classify
20053 as variables, but dwarf2read.c doesn't currently use them. */
20054 return GDB_INDEX_SYMBOL_KIND_OTHER;
20055 }
20056 case STRUCT_DOMAIN:
20057 return GDB_INDEX_SYMBOL_KIND_TYPE;
20058 default:
20059 return GDB_INDEX_SYMBOL_KIND_OTHER;
20060 }
20061}
20062
9291a0cd 20063/* Add a list of partial symbols to SYMTAB. */
b89be57b 20064
9291a0cd
TT
20065static void
20066write_psymbols (struct mapped_symtab *symtab,
987d643c 20067 htab_t psyms_seen,
9291a0cd
TT
20068 struct partial_symbol **psymp,
20069 int count,
987d643c
TT
20070 offset_type cu_index,
20071 int is_static)
9291a0cd
TT
20072{
20073 for (; count-- > 0; ++psymp)
20074 {
156942c7
DE
20075 struct partial_symbol *psym = *psymp;
20076 void **slot;
987d643c 20077
156942c7 20078 if (SYMBOL_LANGUAGE (psym) == language_ada)
9291a0cd 20079 error (_("Ada is not currently supported by the index"));
987d643c 20080
987d643c 20081 /* Only add a given psymbol once. */
156942c7 20082 slot = htab_find_slot (psyms_seen, psym, INSERT);
987d643c
TT
20083 if (!*slot)
20084 {
156942c7
DE
20085 gdb_index_symbol_kind kind = symbol_kind (psym);
20086
20087 *slot = psym;
20088 add_index_entry (symtab, SYMBOL_SEARCH_NAME (psym),
20089 is_static, kind, cu_index);
987d643c 20090 }
9291a0cd
TT
20091 }
20092}
20093
20094/* Write the contents of an ("unfinished") obstack to FILE. Throw an
20095 exception if there is an error. */
b89be57b 20096
9291a0cd
TT
20097static void
20098write_obstack (FILE *file, struct obstack *obstack)
20099{
20100 if (fwrite (obstack_base (obstack), 1, obstack_object_size (obstack),
20101 file)
20102 != obstack_object_size (obstack))
20103 error (_("couldn't data write to file"));
20104}
20105
20106/* Unlink a file if the argument is not NULL. */
b89be57b 20107
9291a0cd
TT
20108static void
20109unlink_if_set (void *p)
20110{
20111 char **filename = p;
20112 if (*filename)
20113 unlink (*filename);
20114}
20115
1fd400ff
TT
20116/* A helper struct used when iterating over debug_types. */
20117struct signatured_type_index_data
20118{
20119 struct objfile *objfile;
20120 struct mapped_symtab *symtab;
20121 struct obstack *types_list;
987d643c 20122 htab_t psyms_seen;
1fd400ff
TT
20123 int cu_index;
20124};
20125
20126/* A helper function that writes a single signatured_type to an
20127 obstack. */
b89be57b 20128
1fd400ff
TT
20129static int
20130write_one_signatured_type (void **slot, void *d)
20131{
20132 struct signatured_type_index_data *info = d;
20133 struct signatured_type *entry = (struct signatured_type *) *slot;
e254ef6a
DE
20134 struct dwarf2_per_cu_data *per_cu = &entry->per_cu;
20135 struct partial_symtab *psymtab = per_cu->v.psymtab;
1fd400ff
TT
20136 gdb_byte val[8];
20137
20138 write_psymbols (info->symtab,
987d643c 20139 info->psyms_seen,
3e43a32a
MS
20140 info->objfile->global_psymbols.list
20141 + psymtab->globals_offset,
987d643c
TT
20142 psymtab->n_global_syms, info->cu_index,
20143 0);
1fd400ff 20144 write_psymbols (info->symtab,
987d643c 20145 info->psyms_seen,
3e43a32a
MS
20146 info->objfile->static_psymbols.list
20147 + psymtab->statics_offset,
987d643c
TT
20148 psymtab->n_static_syms, info->cu_index,
20149 1);
1fd400ff 20150
b64f50a1
JK
20151 store_unsigned_integer (val, 8, BFD_ENDIAN_LITTLE,
20152 entry->per_cu.offset.sect_off);
1fd400ff 20153 obstack_grow (info->types_list, val, 8);
3019eac3
DE
20154 store_unsigned_integer (val, 8, BFD_ENDIAN_LITTLE,
20155 entry->type_offset_in_tu.cu_off);
1fd400ff
TT
20156 obstack_grow (info->types_list, val, 8);
20157 store_unsigned_integer (val, 8, BFD_ENDIAN_LITTLE, entry->signature);
20158 obstack_grow (info->types_list, val, 8);
20159
20160 ++info->cu_index;
20161
20162 return 1;
20163}
20164
95554aad
TT
20165/* Recurse into all "included" dependencies and write their symbols as
20166 if they appeared in this psymtab. */
20167
20168static void
20169recursively_write_psymbols (struct objfile *objfile,
20170 struct partial_symtab *psymtab,
20171 struct mapped_symtab *symtab,
20172 htab_t psyms_seen,
20173 offset_type cu_index)
20174{
20175 int i;
20176
20177 for (i = 0; i < psymtab->number_of_dependencies; ++i)
20178 if (psymtab->dependencies[i]->user != NULL)
20179 recursively_write_psymbols (objfile, psymtab->dependencies[i],
20180 symtab, psyms_seen, cu_index);
20181
20182 write_psymbols (symtab,
20183 psyms_seen,
20184 objfile->global_psymbols.list + psymtab->globals_offset,
20185 psymtab->n_global_syms, cu_index,
20186 0);
20187 write_psymbols (symtab,
20188 psyms_seen,
20189 objfile->static_psymbols.list + psymtab->statics_offset,
20190 psymtab->n_static_syms, cu_index,
20191 1);
20192}
20193
9291a0cd 20194/* Create an index file for OBJFILE in the directory DIR. */
b89be57b 20195
9291a0cd
TT
20196static void
20197write_psymtabs_to_index (struct objfile *objfile, const char *dir)
20198{
20199 struct cleanup *cleanup;
20200 char *filename, *cleanup_filename;
1fd400ff
TT
20201 struct obstack contents, addr_obstack, constant_pool, symtab_obstack;
20202 struct obstack cu_list, types_cu_list;
9291a0cd
TT
20203 int i;
20204 FILE *out_file;
20205 struct mapped_symtab *symtab;
20206 offset_type val, size_of_contents, total_len;
20207 struct stat st;
987d643c 20208 htab_t psyms_seen;
0a5429f6
DE
20209 htab_t cu_index_htab;
20210 struct psymtab_cu_index_map *psymtab_cu_index_map;
9291a0cd 20211
b4f2f049 20212 if (!objfile->psymtabs || !objfile->psymtabs_addrmap)
9291a0cd 20213 return;
b4f2f049 20214
9291a0cd
TT
20215 if (dwarf2_per_objfile->using_index)
20216 error (_("Cannot use an index to create the index"));
20217
8b70b953
TT
20218 if (VEC_length (dwarf2_section_info_def, dwarf2_per_objfile->types) > 1)
20219 error (_("Cannot make an index when the file has multiple .debug_types sections"));
20220
9291a0cd 20221 if (stat (objfile->name, &st) < 0)
7e17e088 20222 perror_with_name (objfile->name);
9291a0cd
TT
20223
20224 filename = concat (dir, SLASH_STRING, lbasename (objfile->name),
20225 INDEX_SUFFIX, (char *) NULL);
20226 cleanup = make_cleanup (xfree, filename);
20227
20228 out_file = fopen (filename, "wb");
20229 if (!out_file)
20230 error (_("Can't open `%s' for writing"), filename);
20231
20232 cleanup_filename = filename;
20233 make_cleanup (unlink_if_set, &cleanup_filename);
20234
20235 symtab = create_mapped_symtab ();
20236 make_cleanup (cleanup_mapped_symtab, symtab);
20237
20238 obstack_init (&addr_obstack);
20239 make_cleanup_obstack_free (&addr_obstack);
20240
20241 obstack_init (&cu_list);
20242 make_cleanup_obstack_free (&cu_list);
20243
1fd400ff
TT
20244 obstack_init (&types_cu_list);
20245 make_cleanup_obstack_free (&types_cu_list);
20246
987d643c
TT
20247 psyms_seen = htab_create_alloc (100, htab_hash_pointer, htab_eq_pointer,
20248 NULL, xcalloc, xfree);
96408a79 20249 make_cleanup_htab_delete (psyms_seen);
987d643c 20250
0a5429f6
DE
20251 /* While we're scanning CU's create a table that maps a psymtab pointer
20252 (which is what addrmap records) to its index (which is what is recorded
20253 in the index file). This will later be needed to write the address
20254 table. */
20255 cu_index_htab = htab_create_alloc (100,
20256 hash_psymtab_cu_index,
20257 eq_psymtab_cu_index,
20258 NULL, xcalloc, xfree);
96408a79 20259 make_cleanup_htab_delete (cu_index_htab);
0a5429f6
DE
20260 psymtab_cu_index_map = (struct psymtab_cu_index_map *)
20261 xmalloc (sizeof (struct psymtab_cu_index_map)
20262 * dwarf2_per_objfile->n_comp_units);
20263 make_cleanup (xfree, psymtab_cu_index_map);
20264
20265 /* The CU list is already sorted, so we don't need to do additional
1fd400ff
TT
20266 work here. Also, the debug_types entries do not appear in
20267 all_comp_units, but only in their own hash table. */
9291a0cd
TT
20268 for (i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
20269 {
3e43a32a
MS
20270 struct dwarf2_per_cu_data *per_cu
20271 = dwarf2_per_objfile->all_comp_units[i];
e254ef6a 20272 struct partial_symtab *psymtab = per_cu->v.psymtab;
9291a0cd 20273 gdb_byte val[8];
0a5429f6
DE
20274 struct psymtab_cu_index_map *map;
20275 void **slot;
9291a0cd 20276
95554aad
TT
20277 if (psymtab->user == NULL)
20278 recursively_write_psymbols (objfile, psymtab, symtab, psyms_seen, i);
9291a0cd 20279
0a5429f6
DE
20280 map = &psymtab_cu_index_map[i];
20281 map->psymtab = psymtab;
20282 map->cu_index = i;
20283 slot = htab_find_slot (cu_index_htab, map, INSERT);
20284 gdb_assert (slot != NULL);
20285 gdb_assert (*slot == NULL);
20286 *slot = map;
9291a0cd 20287
b64f50a1
JK
20288 store_unsigned_integer (val, 8, BFD_ENDIAN_LITTLE,
20289 per_cu->offset.sect_off);
9291a0cd 20290 obstack_grow (&cu_list, val, 8);
e254ef6a 20291 store_unsigned_integer (val, 8, BFD_ENDIAN_LITTLE, per_cu->length);
9291a0cd
TT
20292 obstack_grow (&cu_list, val, 8);
20293 }
20294
0a5429f6
DE
20295 /* Dump the address map. */
20296 write_address_map (objfile, &addr_obstack, cu_index_htab);
20297
1fd400ff
TT
20298 /* Write out the .debug_type entries, if any. */
20299 if (dwarf2_per_objfile->signatured_types)
20300 {
20301 struct signatured_type_index_data sig_data;
20302
20303 sig_data.objfile = objfile;
20304 sig_data.symtab = symtab;
20305 sig_data.types_list = &types_cu_list;
987d643c 20306 sig_data.psyms_seen = psyms_seen;
1fd400ff
TT
20307 sig_data.cu_index = dwarf2_per_objfile->n_comp_units;
20308 htab_traverse_noresize (dwarf2_per_objfile->signatured_types,
20309 write_one_signatured_type, &sig_data);
20310 }
20311
156942c7
DE
20312 /* Now that we've processed all symbols we can shrink their cu_indices
20313 lists. */
20314 uniquify_cu_indices (symtab);
20315
9291a0cd
TT
20316 obstack_init (&constant_pool);
20317 make_cleanup_obstack_free (&constant_pool);
20318 obstack_init (&symtab_obstack);
20319 make_cleanup_obstack_free (&symtab_obstack);
20320 write_hash_table (symtab, &symtab_obstack, &constant_pool);
20321
20322 obstack_init (&contents);
20323 make_cleanup_obstack_free (&contents);
1fd400ff 20324 size_of_contents = 6 * sizeof (offset_type);
9291a0cd
TT
20325 total_len = size_of_contents;
20326
20327 /* The version number. */
156942c7 20328 val = MAYBE_SWAP (7);
9291a0cd
TT
20329 obstack_grow (&contents, &val, sizeof (val));
20330
20331 /* The offset of the CU list from the start of the file. */
20332 val = MAYBE_SWAP (total_len);
20333 obstack_grow (&contents, &val, sizeof (val));
20334 total_len += obstack_object_size (&cu_list);
20335
1fd400ff
TT
20336 /* The offset of the types CU list from the start of the file. */
20337 val = MAYBE_SWAP (total_len);
20338 obstack_grow (&contents, &val, sizeof (val));
20339 total_len += obstack_object_size (&types_cu_list);
20340
9291a0cd
TT
20341 /* The offset of the address table from the start of the file. */
20342 val = MAYBE_SWAP (total_len);
20343 obstack_grow (&contents, &val, sizeof (val));
20344 total_len += obstack_object_size (&addr_obstack);
20345
20346 /* The offset of the symbol table from the start of the file. */
20347 val = MAYBE_SWAP (total_len);
20348 obstack_grow (&contents, &val, sizeof (val));
20349 total_len += obstack_object_size (&symtab_obstack);
20350
20351 /* The offset of the constant pool from the start of the file. */
20352 val = MAYBE_SWAP (total_len);
20353 obstack_grow (&contents, &val, sizeof (val));
20354 total_len += obstack_object_size (&constant_pool);
20355
20356 gdb_assert (obstack_object_size (&contents) == size_of_contents);
20357
20358 write_obstack (out_file, &contents);
20359 write_obstack (out_file, &cu_list);
1fd400ff 20360 write_obstack (out_file, &types_cu_list);
9291a0cd
TT
20361 write_obstack (out_file, &addr_obstack);
20362 write_obstack (out_file, &symtab_obstack);
20363 write_obstack (out_file, &constant_pool);
20364
20365 fclose (out_file);
20366
20367 /* We want to keep the file, so we set cleanup_filename to NULL
20368 here. See unlink_if_set. */
20369 cleanup_filename = NULL;
20370
20371 do_cleanups (cleanup);
20372}
20373
90476074
TT
20374/* Implementation of the `save gdb-index' command.
20375
20376 Note that the file format used by this command is documented in the
20377 GDB manual. Any changes here must be documented there. */
11570e71 20378
9291a0cd
TT
20379static void
20380save_gdb_index_command (char *arg, int from_tty)
20381{
20382 struct objfile *objfile;
20383
20384 if (!arg || !*arg)
96d19272 20385 error (_("usage: save gdb-index DIRECTORY"));
9291a0cd
TT
20386
20387 ALL_OBJFILES (objfile)
20388 {
20389 struct stat st;
20390
20391 /* If the objfile does not correspond to an actual file, skip it. */
20392 if (stat (objfile->name, &st) < 0)
20393 continue;
20394
20395 dwarf2_per_objfile = objfile_data (objfile, dwarf2_objfile_data_key);
20396 if (dwarf2_per_objfile)
20397 {
20398 volatile struct gdb_exception except;
20399
20400 TRY_CATCH (except, RETURN_MASK_ERROR)
20401 {
20402 write_psymtabs_to_index (objfile, arg);
20403 }
20404 if (except.reason < 0)
20405 exception_fprintf (gdb_stderr, except,
20406 _("Error while writing index for `%s': "),
20407 objfile->name);
20408 }
20409 }
dce234bc
PP
20410}
20411
9291a0cd
TT
20412\f
20413
9eae7c52
TT
20414int dwarf2_always_disassemble;
20415
20416static void
20417show_dwarf2_always_disassemble (struct ui_file *file, int from_tty,
20418 struct cmd_list_element *c, const char *value)
20419{
3e43a32a
MS
20420 fprintf_filtered (file,
20421 _("Whether to always disassemble "
20422 "DWARF expressions is %s.\n"),
9eae7c52
TT
20423 value);
20424}
20425
900e11f9
JK
20426static void
20427show_check_physname (struct ui_file *file, int from_tty,
20428 struct cmd_list_element *c, const char *value)
20429{
20430 fprintf_filtered (file,
20431 _("Whether to check \"physname\" is %s.\n"),
20432 value);
20433}
20434
6502dd73
DJ
20435void _initialize_dwarf2_read (void);
20436
20437void
20438_initialize_dwarf2_read (void)
20439{
96d19272
JK
20440 struct cmd_list_element *c;
20441
dce234bc 20442 dwarf2_objfile_data_key
c1bd65d0 20443 = register_objfile_data_with_cleanup (NULL, dwarf2_per_objfile_free);
ae038cb0 20444
1bedd215
AC
20445 add_prefix_cmd ("dwarf2", class_maintenance, set_dwarf2_cmd, _("\
20446Set DWARF 2 specific variables.\n\
20447Configure DWARF 2 variables such as the cache size"),
ae038cb0
DJ
20448 &set_dwarf2_cmdlist, "maintenance set dwarf2 ",
20449 0/*allow-unknown*/, &maintenance_set_cmdlist);
20450
1bedd215
AC
20451 add_prefix_cmd ("dwarf2", class_maintenance, show_dwarf2_cmd, _("\
20452Show DWARF 2 specific variables\n\
20453Show DWARF 2 variables such as the cache size"),
ae038cb0
DJ
20454 &show_dwarf2_cmdlist, "maintenance show dwarf2 ",
20455 0/*allow-unknown*/, &maintenance_show_cmdlist);
20456
20457 add_setshow_zinteger_cmd ("max-cache-age", class_obscure,
7915a72c
AC
20458 &dwarf2_max_cache_age, _("\
20459Set the upper bound on the age of cached dwarf2 compilation units."), _("\
20460Show the upper bound on the age of cached dwarf2 compilation units."), _("\
20461A higher limit means that cached compilation units will be stored\n\
20462in memory longer, and more total memory will be used. Zero disables\n\
20463caching, which can slow down startup."),
2c5b56ce 20464 NULL,
920d2a44 20465 show_dwarf2_max_cache_age,
2c5b56ce 20466 &set_dwarf2_cmdlist,
ae038cb0 20467 &show_dwarf2_cmdlist);
d97bc12b 20468
9eae7c52
TT
20469 add_setshow_boolean_cmd ("always-disassemble", class_obscure,
20470 &dwarf2_always_disassemble, _("\
20471Set whether `info address' always disassembles DWARF expressions."), _("\
20472Show whether `info address' always disassembles DWARF expressions."), _("\
20473When enabled, DWARF expressions are always printed in an assembly-like\n\
20474syntax. When disabled, expressions will be printed in a more\n\
20475conversational style, when possible."),
20476 NULL,
20477 show_dwarf2_always_disassemble,
20478 &set_dwarf2_cmdlist,
20479 &show_dwarf2_cmdlist);
20480
45cfd468
DE
20481 add_setshow_boolean_cmd ("dwarf2-read", no_class, &dwarf2_read_debug, _("\
20482Set debugging of the dwarf2 reader."), _("\
20483Show debugging of the dwarf2 reader."), _("\
20484When enabled, debugging messages are printed during dwarf2 reading\n\
20485and symtab expansion."),
20486 NULL,
20487 NULL,
20488 &setdebuglist, &showdebuglist);
20489
ccce17b0 20490 add_setshow_zuinteger_cmd ("dwarf2-die", no_class, &dwarf2_die_debug, _("\
d97bc12b
DE
20491Set debugging of the dwarf2 DIE reader."), _("\
20492Show debugging of the dwarf2 DIE reader."), _("\
20493When enabled (non-zero), DIEs are dumped after they are read in.\n\
20494The value is the maximum depth to print."),
ccce17b0
YQ
20495 NULL,
20496 NULL,
20497 &setdebuglist, &showdebuglist);
9291a0cd 20498
900e11f9
JK
20499 add_setshow_boolean_cmd ("check-physname", no_class, &check_physname, _("\
20500Set cross-checking of \"physname\" code against demangler."), _("\
20501Show cross-checking of \"physname\" code against demangler."), _("\
20502When enabled, GDB's internal \"physname\" code is checked against\n\
20503the demangler."),
20504 NULL, show_check_physname,
20505 &setdebuglist, &showdebuglist);
20506
e615022a
DE
20507 add_setshow_boolean_cmd ("use-deprecated-index-sections",
20508 no_class, &use_deprecated_index_sections, _("\
20509Set whether to use deprecated gdb_index sections."), _("\
20510Show whether to use deprecated gdb_index sections."), _("\
20511When enabled, deprecated .gdb_index sections are used anyway.\n\
20512Normally they are ignored either because of a missing feature or\n\
20513performance issue.\n\
20514Warning: This option must be enabled before gdb reads the file."),
20515 NULL,
20516 NULL,
20517 &setlist, &showlist);
20518
96d19272 20519 c = add_cmd ("gdb-index", class_files, save_gdb_index_command,
11570e71 20520 _("\
fc1a9d6e 20521Save a gdb-index file.\n\
11570e71 20522Usage: save gdb-index DIRECTORY"),
96d19272
JK
20523 &save_cmdlist);
20524 set_cmd_completer (c, filename_completer);
6502dd73 20525}
This page took 2.626839 seconds and 4 git commands to generate.