gdb/
[deliverable/binutils-gdb.git] / gdb / dwarf2read.c
CommitLineData
c906108c 1/* DWARF 2 debugging format support for GDB.
917c78fc 2
28e7fd62 3 Copyright (C) 1994-2013 Free Software Foundation, Inc.
c906108c
SS
4
5 Adapted by Gary Funck (gary@intrepid.com), Intrepid Technology,
6 Inc. with support from Florida State University (under contract
7 with the Ada Joint Program Office), and Silicon Graphics, Inc.
8 Initial contribution by Brent Benson, Harris Computer Systems, Inc.,
9 based on Fred Fish's (Cygnus Support) implementation of DWARF 1
7ce59000 10 support.
c906108c 11
c5aa993b 12 This file is part of GDB.
c906108c 13
c5aa993b
JM
14 This program is free software; you can redistribute it and/or modify
15 it under the terms of the GNU General Public License as published by
a9762ec7
JB
16 the Free Software Foundation; either version 3 of the License, or
17 (at your option) any later version.
c906108c 18
a9762ec7
JB
19 This program is distributed in the hope that it will be useful,
20 but WITHOUT ANY WARRANTY; without even the implied warranty of
21 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22 GNU General Public License for more details.
c906108c 23
c5aa993b 24 You should have received a copy of the GNU General Public License
a9762ec7 25 along with this program. If not, see <http://www.gnu.org/licenses/>. */
c906108c 26
21b2bd31
DE
27/* FIXME: Various die-reading functions need to be more careful with
28 reading off the end of the section.
29 E.g., load_partial_dies, read_partial_die. */
30
c906108c
SS
31#include "defs.h"
32#include "bfd.h"
80626a55 33#include "elf-bfd.h"
c906108c
SS
34#include "symtab.h"
35#include "gdbtypes.h"
c906108c 36#include "objfiles.h"
fa8f86ff 37#include "dwarf2.h"
c906108c
SS
38#include "buildsym.h"
39#include "demangle.h"
50f182aa 40#include "gdb-demangle.h"
c906108c 41#include "expression.h"
d5166ae1 42#include "filenames.h" /* for DOSish file names */
2e276125 43#include "macrotab.h"
c906108c
SS
44#include "language.h"
45#include "complaints.h"
357e46e7 46#include "bcache.h"
4c2df51b
DJ
47#include "dwarf2expr.h"
48#include "dwarf2loc.h"
9219021c 49#include "cp-support.h"
72bf9492 50#include "hashtab.h"
ae038cb0
DJ
51#include "command.h"
52#include "gdbcmd.h"
edb3359d 53#include "block.h"
ff013f42 54#include "addrmap.h"
94af9270
KS
55#include "typeprint.h"
56#include "jv-lang.h"
ccefe4c4 57#include "psympriv.h"
9291a0cd
TT
58#include "exceptions.h"
59#include "gdb_stat.h"
96d19272 60#include "completer.h"
34eaf542 61#include "vec.h"
98bfdba5 62#include "c-lang.h"
a766d390 63#include "go-lang.h"
98bfdba5 64#include "valprint.h"
3019eac3 65#include "gdbcore.h" /* for gnutarget */
156942c7 66#include "gdb/gdb-index.h"
60d5a603 67#include <ctype.h>
cbb099e8 68#include "gdb_bfd.h"
4357ac6c 69#include "f-lang.h"
4c2df51b 70
c906108c
SS
71#include <fcntl.h>
72#include "gdb_string.h"
4bdf3d34 73#include "gdb_assert.h"
c906108c 74#include <sys/types.h>
d8151005 75
34eaf542
TT
76typedef struct symbol *symbolp;
77DEF_VEC_P (symbolp);
78
45cfd468
DE
79/* When non-zero, print basic high level tracing messages.
80 This is in contrast to the low level DIE reading of dwarf2_die_debug. */
81static int dwarf2_read_debug = 0;
82
d97bc12b 83/* When non-zero, dump DIEs after they are read in. */
ccce17b0 84static unsigned int dwarf2_die_debug = 0;
d97bc12b 85
900e11f9
JK
86/* When non-zero, cross-check physname against demangler. */
87static int check_physname = 0;
88
481860b3 89/* When non-zero, do not reject deprecated .gdb_index sections. */
e615022a 90static int use_deprecated_index_sections = 0;
481860b3 91
6502dd73
DJ
92static const struct objfile_data *dwarf2_objfile_data_key;
93
dce234bc
PP
94struct dwarf2_section_info
95{
96 asection *asection;
97 gdb_byte *buffer;
98 bfd_size_type size;
be391dca
TT
99 /* True if we have tried to read this section. */
100 int readin;
dce234bc
PP
101};
102
8b70b953
TT
103typedef struct dwarf2_section_info dwarf2_section_info_def;
104DEF_VEC_O (dwarf2_section_info_def);
105
9291a0cd
TT
106/* All offsets in the index are of this type. It must be
107 architecture-independent. */
108typedef uint32_t offset_type;
109
110DEF_VEC_I (offset_type);
111
156942c7
DE
112/* Ensure only legit values are used. */
113#define DW2_GDB_INDEX_SYMBOL_STATIC_SET_VALUE(cu_index, value) \
114 do { \
115 gdb_assert ((unsigned int) (value) <= 1); \
116 GDB_INDEX_SYMBOL_STATIC_SET_VALUE((cu_index), (value)); \
117 } while (0)
118
119/* Ensure only legit values are used. */
120#define DW2_GDB_INDEX_SYMBOL_KIND_SET_VALUE(cu_index, value) \
121 do { \
122 gdb_assert ((value) >= GDB_INDEX_SYMBOL_KIND_TYPE \
123 && (value) <= GDB_INDEX_SYMBOL_KIND_OTHER); \
124 GDB_INDEX_SYMBOL_KIND_SET_VALUE((cu_index), (value)); \
125 } while (0)
126
127/* Ensure we don't use more than the alloted nuber of bits for the CU. */
128#define DW2_GDB_INDEX_CU_SET_VALUE(cu_index, value) \
129 do { \
130 gdb_assert (((value) & ~GDB_INDEX_CU_MASK) == 0); \
131 GDB_INDEX_CU_SET_VALUE((cu_index), (value)); \
132 } while (0)
133
9291a0cd
TT
134/* A description of the mapped index. The file format is described in
135 a comment by the code that writes the index. */
136struct mapped_index
137{
559a7a62
JK
138 /* Index data format version. */
139 int version;
140
9291a0cd
TT
141 /* The total length of the buffer. */
142 off_t total_size;
b11b1f88 143
9291a0cd
TT
144 /* A pointer to the address table data. */
145 const gdb_byte *address_table;
b11b1f88 146
9291a0cd
TT
147 /* Size of the address table data in bytes. */
148 offset_type address_table_size;
b11b1f88 149
3876f04e
DE
150 /* The symbol table, implemented as a hash table. */
151 const offset_type *symbol_table;
b11b1f88 152
9291a0cd 153 /* Size in slots, each slot is 2 offset_types. */
3876f04e 154 offset_type symbol_table_slots;
b11b1f88 155
9291a0cd
TT
156 /* A pointer to the constant pool. */
157 const char *constant_pool;
158};
159
95554aad
TT
160typedef struct dwarf2_per_cu_data *dwarf2_per_cu_ptr;
161DEF_VEC_P (dwarf2_per_cu_ptr);
162
9cdd5dbd
DE
163/* Collection of data recorded per objfile.
164 This hangs off of dwarf2_objfile_data_key. */
165
6502dd73
DJ
166struct dwarf2_per_objfile
167{
dce234bc
PP
168 struct dwarf2_section_info info;
169 struct dwarf2_section_info abbrev;
170 struct dwarf2_section_info line;
dce234bc
PP
171 struct dwarf2_section_info loc;
172 struct dwarf2_section_info macinfo;
cf2c3c16 173 struct dwarf2_section_info macro;
dce234bc
PP
174 struct dwarf2_section_info str;
175 struct dwarf2_section_info ranges;
3019eac3 176 struct dwarf2_section_info addr;
dce234bc
PP
177 struct dwarf2_section_info frame;
178 struct dwarf2_section_info eh_frame;
9291a0cd 179 struct dwarf2_section_info gdb_index;
ae038cb0 180
8b70b953
TT
181 VEC (dwarf2_section_info_def) *types;
182
be391dca
TT
183 /* Back link. */
184 struct objfile *objfile;
185
d467dd73 186 /* Table of all the compilation units. This is used to locate
10b3939b 187 the target compilation unit of a particular reference. */
ae038cb0
DJ
188 struct dwarf2_per_cu_data **all_comp_units;
189
190 /* The number of compilation units in ALL_COMP_UNITS. */
191 int n_comp_units;
192
1fd400ff 193 /* The number of .debug_types-related CUs. */
d467dd73 194 int n_type_units;
1fd400ff 195
d467dd73 196 /* The .debug_types-related CUs (TUs). */
b4dd5633 197 struct signatured_type **all_type_units;
1fd400ff 198
f4dc4d17
DE
199 /* The number of entries in all_type_unit_groups. */
200 int n_type_unit_groups;
201
202 /* Table of type unit groups.
203 This exists to make it easy to iterate over all CUs and TU groups. */
204 struct type_unit_group **all_type_unit_groups;
205
206 /* Table of struct type_unit_group objects.
207 The hash key is the DW_AT_stmt_list value. */
208 htab_t type_unit_groups;
72dca2f5 209
348e048f
DE
210 /* A table mapping .debug_types signatures to its signatured_type entry.
211 This is NULL if the .debug_types section hasn't been read in yet. */
212 htab_t signatured_types;
213
f4dc4d17
DE
214 /* Type unit statistics, to see how well the scaling improvements
215 are doing. */
216 struct tu_stats
217 {
218 int nr_uniq_abbrev_tables;
219 int nr_symtabs;
220 int nr_symtab_sharers;
221 int nr_stmt_less_type_units;
222 } tu_stats;
223
224 /* A chain of compilation units that are currently read in, so that
225 they can be freed later. */
226 struct dwarf2_per_cu_data *read_in_chain;
227
3019eac3
DE
228 /* A table mapping DW_AT_dwo_name values to struct dwo_file objects.
229 This is NULL if the table hasn't been allocated yet. */
230 htab_t dwo_files;
231
80626a55
DE
232 /* Non-zero if we've check for whether there is a DWP file. */
233 int dwp_checked;
234
235 /* The DWP file if there is one, or NULL. */
236 struct dwp_file *dwp_file;
237
36586728
TT
238 /* The shared '.dwz' file, if one exists. This is used when the
239 original data was compressed using 'dwz -m'. */
240 struct dwz_file *dwz_file;
241
72dca2f5
FR
242 /* A flag indicating wether this objfile has a section loaded at a
243 VMA of 0. */
244 int has_section_at_zero;
9291a0cd 245
ae2de4f8
DE
246 /* True if we are using the mapped index,
247 or we are faking it for OBJF_READNOW's sake. */
9291a0cd
TT
248 unsigned char using_index;
249
ae2de4f8 250 /* The mapped index, or NULL if .gdb_index is missing or not being used. */
9291a0cd 251 struct mapped_index *index_table;
98bfdba5 252
7b9f3c50 253 /* When using index_table, this keeps track of all quick_file_names entries.
56e64610
DE
254 TUs typically share line table entries with a CU, so we maintain a
255 separate table of all line table entries to support the sharing.
256 Note that while there can be way more TUs than CUs, we've already
257 sorted all the TUs into "type unit groups", grouped by their
258 DW_AT_stmt_list value. Therefore the only sharing done here is with a
259 CU and its associated TU group if there is one. */
7b9f3c50
DE
260 htab_t quick_file_names_table;
261
98bfdba5
PA
262 /* Set during partial symbol reading, to prevent queueing of full
263 symbols. */
264 int reading_partial_symbols;
673bfd45 265
dee91e82 266 /* Table mapping type DIEs to their struct type *.
673bfd45 267 This is NULL if not allocated yet.
dee91e82
DE
268 The mapping is done via (CU/TU signature + DIE offset) -> type. */
269 htab_t die_type_hash;
95554aad
TT
270
271 /* The CUs we recently read. */
272 VEC (dwarf2_per_cu_ptr) *just_read_cus;
6502dd73
DJ
273};
274
275static struct dwarf2_per_objfile *dwarf2_per_objfile;
c906108c 276
251d32d9 277/* Default names of the debugging sections. */
c906108c 278
233a11ab
CS
279/* Note that if the debugging section has been compressed, it might
280 have a name like .zdebug_info. */
281
9cdd5dbd
DE
282static const struct dwarf2_debug_sections dwarf2_elf_names =
283{
251d32d9
TG
284 { ".debug_info", ".zdebug_info" },
285 { ".debug_abbrev", ".zdebug_abbrev" },
286 { ".debug_line", ".zdebug_line" },
287 { ".debug_loc", ".zdebug_loc" },
288 { ".debug_macinfo", ".zdebug_macinfo" },
cf2c3c16 289 { ".debug_macro", ".zdebug_macro" },
251d32d9
TG
290 { ".debug_str", ".zdebug_str" },
291 { ".debug_ranges", ".zdebug_ranges" },
292 { ".debug_types", ".zdebug_types" },
3019eac3 293 { ".debug_addr", ".zdebug_addr" },
251d32d9
TG
294 { ".debug_frame", ".zdebug_frame" },
295 { ".eh_frame", NULL },
24d3216f
TT
296 { ".gdb_index", ".zgdb_index" },
297 23
251d32d9 298};
c906108c 299
80626a55 300/* List of DWO/DWP sections. */
3019eac3 301
80626a55 302static const struct dwop_section_names
3019eac3
DE
303{
304 struct dwarf2_section_names abbrev_dwo;
305 struct dwarf2_section_names info_dwo;
306 struct dwarf2_section_names line_dwo;
307 struct dwarf2_section_names loc_dwo;
09262596
DE
308 struct dwarf2_section_names macinfo_dwo;
309 struct dwarf2_section_names macro_dwo;
3019eac3
DE
310 struct dwarf2_section_names str_dwo;
311 struct dwarf2_section_names str_offsets_dwo;
312 struct dwarf2_section_names types_dwo;
80626a55
DE
313 struct dwarf2_section_names cu_index;
314 struct dwarf2_section_names tu_index;
3019eac3 315}
80626a55 316dwop_section_names =
3019eac3
DE
317{
318 { ".debug_abbrev.dwo", ".zdebug_abbrev.dwo" },
319 { ".debug_info.dwo", ".zdebug_info.dwo" },
320 { ".debug_line.dwo", ".zdebug_line.dwo" },
321 { ".debug_loc.dwo", ".zdebug_loc.dwo" },
09262596
DE
322 { ".debug_macinfo.dwo", ".zdebug_macinfo.dwo" },
323 { ".debug_macro.dwo", ".zdebug_macro.dwo" },
3019eac3
DE
324 { ".debug_str.dwo", ".zdebug_str.dwo" },
325 { ".debug_str_offsets.dwo", ".zdebug_str_offsets.dwo" },
326 { ".debug_types.dwo", ".zdebug_types.dwo" },
80626a55
DE
327 { ".debug_cu_index", ".zdebug_cu_index" },
328 { ".debug_tu_index", ".zdebug_tu_index" },
3019eac3
DE
329};
330
c906108c
SS
331/* local data types */
332
107d2387
AC
333/* The data in a compilation unit header, after target2host
334 translation, looks like this. */
c906108c 335struct comp_unit_head
a738430d 336{
c764a876 337 unsigned int length;
a738430d 338 short version;
a738430d
MK
339 unsigned char addr_size;
340 unsigned char signed_addr_p;
b64f50a1 341 sect_offset abbrev_offset;
57349743 342
a738430d
MK
343 /* Size of file offsets; either 4 or 8. */
344 unsigned int offset_size;
57349743 345
a738430d
MK
346 /* Size of the length field; either 4 or 12. */
347 unsigned int initial_length_size;
57349743 348
a738430d
MK
349 /* Offset to the first byte of this compilation unit header in the
350 .debug_info section, for resolving relative reference dies. */
b64f50a1 351 sect_offset offset;
57349743 352
d00adf39
DE
353 /* Offset to first die in this cu from the start of the cu.
354 This will be the first byte following the compilation unit header. */
b64f50a1 355 cu_offset first_die_offset;
a738430d 356};
c906108c 357
3da10d80
KS
358/* Type used for delaying computation of method physnames.
359 See comments for compute_delayed_physnames. */
360struct delayed_method_info
361{
362 /* The type to which the method is attached, i.e., its parent class. */
363 struct type *type;
364
365 /* The index of the method in the type's function fieldlists. */
366 int fnfield_index;
367
368 /* The index of the method in the fieldlist. */
369 int index;
370
371 /* The name of the DIE. */
372 const char *name;
373
374 /* The DIE associated with this method. */
375 struct die_info *die;
376};
377
378typedef struct delayed_method_info delayed_method_info;
379DEF_VEC_O (delayed_method_info);
380
e7c27a73
DJ
381/* Internal state when decoding a particular compilation unit. */
382struct dwarf2_cu
383{
384 /* The objfile containing this compilation unit. */
385 struct objfile *objfile;
386
d00adf39 387 /* The header of the compilation unit. */
e7c27a73 388 struct comp_unit_head header;
e142c38c 389
d00adf39
DE
390 /* Base address of this compilation unit. */
391 CORE_ADDR base_address;
392
393 /* Non-zero if base_address has been set. */
394 int base_known;
395
e142c38c
DJ
396 /* The language we are debugging. */
397 enum language language;
398 const struct language_defn *language_defn;
399
b0f35d58
DL
400 const char *producer;
401
e142c38c
DJ
402 /* The generic symbol table building routines have separate lists for
403 file scope symbols and all all other scopes (local scopes). So
404 we need to select the right one to pass to add_symbol_to_list().
405 We do it by keeping a pointer to the correct list in list_in_scope.
406
407 FIXME: The original dwarf code just treated the file scope as the
408 first local scope, and all other local scopes as nested local
409 scopes, and worked fine. Check to see if we really need to
410 distinguish these in buildsym.c. */
411 struct pending **list_in_scope;
412
433df2d4
DE
413 /* The abbrev table for this CU.
414 Normally this points to the abbrev table in the objfile.
415 But if DWO_UNIT is non-NULL this is the abbrev table in the DWO file. */
416 struct abbrev_table *abbrev_table;
72bf9492 417
b64f50a1
JK
418 /* Hash table holding all the loaded partial DIEs
419 with partial_die->offset.SECT_OFF as hash. */
72bf9492
DJ
420 htab_t partial_dies;
421
422 /* Storage for things with the same lifetime as this read-in compilation
423 unit, including partial DIEs. */
424 struct obstack comp_unit_obstack;
425
ae038cb0
DJ
426 /* When multiple dwarf2_cu structures are living in memory, this field
427 chains them all together, so that they can be released efficiently.
428 We will probably also want a generation counter so that most-recently-used
429 compilation units are cached... */
430 struct dwarf2_per_cu_data *read_in_chain;
431
432 /* Backchain to our per_cu entry if the tree has been built. */
433 struct dwarf2_per_cu_data *per_cu;
434
435 /* How many compilation units ago was this CU last referenced? */
436 int last_used;
437
b64f50a1
JK
438 /* A hash table of DIE cu_offset for following references with
439 die_info->offset.sect_off as hash. */
51545339 440 htab_t die_hash;
10b3939b
DJ
441
442 /* Full DIEs if read in. */
443 struct die_info *dies;
444
445 /* A set of pointers to dwarf2_per_cu_data objects for compilation
446 units referenced by this one. Only set during full symbol processing;
447 partial symbol tables do not have dependencies. */
448 htab_t dependencies;
449
cb1df416
DJ
450 /* Header data from the line table, during full symbol processing. */
451 struct line_header *line_header;
452
3da10d80
KS
453 /* A list of methods which need to have physnames computed
454 after all type information has been read. */
455 VEC (delayed_method_info) *method_list;
456
96408a79
SA
457 /* To be copied to symtab->call_site_htab. */
458 htab_t call_site_htab;
459
034e5797
DE
460 /* Non-NULL if this CU came from a DWO file.
461 There is an invariant here that is important to remember:
462 Except for attributes copied from the top level DIE in the "main"
463 (or "stub") file in preparation for reading the DWO file
464 (e.g., DW_AT_GNU_addr_base), we KISS: there is only *one* CU.
465 Either there isn't a DWO file (in which case this is NULL and the point
466 is moot), or there is and either we're not going to read it (in which
467 case this is NULL) or there is and we are reading it (in which case this
468 is non-NULL). */
3019eac3
DE
469 struct dwo_unit *dwo_unit;
470
471 /* The DW_AT_addr_base attribute if present, zero otherwise
472 (zero is a valid value though).
473 Note this value comes from the stub CU/TU's DIE. */
474 ULONGEST addr_base;
475
2e3cf129
DE
476 /* The DW_AT_ranges_base attribute if present, zero otherwise
477 (zero is a valid value though).
478 Note this value comes from the stub CU/TU's DIE.
479 Also note that the value is zero in the non-DWO case so this value can
ab435259
DE
480 be used without needing to know whether DWO files are in use or not.
481 N.B. This does not apply to DW_AT_ranges appearing in
482 DW_TAG_compile_unit dies. This is a bit of a wart, consider if ever
483 DW_AT_ranges appeared in the DW_TAG_compile_unit of DWO DIEs: then
484 DW_AT_ranges_base *would* have to be applied, and we'd have to care
485 whether the DW_AT_ranges attribute came from the skeleton or DWO. */
2e3cf129
DE
486 ULONGEST ranges_base;
487
ae038cb0
DJ
488 /* Mark used when releasing cached dies. */
489 unsigned int mark : 1;
490
8be455d7
JK
491 /* This CU references .debug_loc. See the symtab->locations_valid field.
492 This test is imperfect as there may exist optimized debug code not using
493 any location list and still facing inlining issues if handled as
494 unoptimized code. For a future better test see GCC PR other/32998. */
8be455d7 495 unsigned int has_loclist : 1;
ba919b58 496
1b80a9fa
JK
497 /* These cache the results for producer_is_* fields. CHECKED_PRODUCER is set
498 if all the producer_is_* fields are valid. This information is cached
499 because profiling CU expansion showed excessive time spent in
500 producer_is_gxx_lt_4_6. */
ba919b58
TT
501 unsigned int checked_producer : 1;
502 unsigned int producer_is_gxx_lt_4_6 : 1;
1b80a9fa 503 unsigned int producer_is_gcc_lt_4_3 : 1;
685b1105 504 unsigned int producer_is_icc : 1;
4d4ec4e5
TT
505
506 /* When set, the file that we're processing is known to have
507 debugging info for C++ namespaces. GCC 3.3.x did not produce
508 this information, but later versions do. */
509
510 unsigned int processing_has_namespace_info : 1;
e7c27a73
DJ
511};
512
10b3939b
DJ
513/* Persistent data held for a compilation unit, even when not
514 processing it. We put a pointer to this structure in the
28dee7f5 515 read_symtab_private field of the psymtab. */
10b3939b 516
ae038cb0
DJ
517struct dwarf2_per_cu_data
518{
36586728 519 /* The start offset and length of this compilation unit.
45452591 520 NOTE: Unlike comp_unit_head.length, this length includes
3019eac3
DE
521 initial_length_size.
522 If the DIE refers to a DWO file, this is always of the original die,
523 not the DWO file. */
b64f50a1 524 sect_offset offset;
36586728 525 unsigned int length;
ae038cb0
DJ
526
527 /* Flag indicating this compilation unit will be read in before
528 any of the current compilation units are processed. */
c764a876 529 unsigned int queued : 1;
ae038cb0 530
0d99eb77
DE
531 /* This flag will be set when reading partial DIEs if we need to load
532 absolutely all DIEs for this compilation unit, instead of just the ones
533 we think are interesting. It gets set if we look for a DIE in the
5afb4e99
DJ
534 hash table and don't find it. */
535 unsigned int load_all_dies : 1;
536
3019eac3
DE
537 /* Non-zero if this CU is from .debug_types. */
538 unsigned int is_debug_types : 1;
539
36586728
TT
540 /* Non-zero if this CU is from the .dwz file. */
541 unsigned int is_dwz : 1;
542
3019eac3
DE
543 /* The section this CU/TU lives in.
544 If the DIE refers to a DWO file, this is always the original die,
545 not the DWO file. */
546 struct dwarf2_section_info *info_or_types_section;
348e048f 547
17ea53c3
JK
548 /* Set to non-NULL iff this CU is currently loaded. When it gets freed out
549 of the CU cache it gets reset to NULL again. */
ae038cb0 550 struct dwarf2_cu *cu;
1c379e20 551
9cdd5dbd
DE
552 /* The corresponding objfile.
553 Normally we can get the objfile from dwarf2_per_objfile.
554 However we can enter this file with just a "per_cu" handle. */
9291a0cd
TT
555 struct objfile *objfile;
556
557 /* When using partial symbol tables, the 'psymtab' field is active.
558 Otherwise the 'quick' field is active. */
559 union
560 {
561 /* The partial symbol table associated with this compilation unit,
95554aad 562 or NULL for unread partial units. */
9291a0cd
TT
563 struct partial_symtab *psymtab;
564
565 /* Data needed by the "quick" functions. */
566 struct dwarf2_per_cu_quick_data *quick;
567 } v;
95554aad 568
796a7ff8
DE
569 /* The CUs we import using DW_TAG_imported_unit. This is filled in
570 while reading psymtabs, used to compute the psymtab dependencies,
571 and then cleared. Then it is filled in again while reading full
572 symbols, and only deleted when the objfile is destroyed.
573
574 This is also used to work around a difference between the way gold
575 generates .gdb_index version <=7 and the way gdb does. Arguably this
576 is a gold bug. For symbols coming from TUs, gold records in the index
577 the CU that includes the TU instead of the TU itself. This breaks
578 dw2_lookup_symbol: It assumes that if the index says symbol X lives
579 in CU/TU Y, then one need only expand Y and a subsequent lookup in Y
580 will find X. Alas TUs live in their own symtab, so after expanding CU Y
581 we need to look in TU Z to find X. Fortunately, this is akin to
582 DW_TAG_imported_unit, so we just use the same mechanism: For
583 .gdb_index version <=7 this also records the TUs that the CU referred
584 to. Concurrently with this change gdb was modified to emit version 8
585 indices so we only pay a price for gold generated indices. */
586 VEC (dwarf2_per_cu_ptr) *imported_symtabs;
587
588 /* Type units are grouped by their DW_AT_stmt_list entry so that they
589 can share them. If this is a TU, this points to the containing
590 symtab. */
591 struct type_unit_group *type_unit_group;
ae038cb0
DJ
592};
593
348e048f
DE
594/* Entry in the signatured_types hash table. */
595
596struct signatured_type
597{
42e7ad6c
DE
598 /* The "per_cu" object of this type.
599 N.B.: This is the first member so that it's easy to convert pointers
600 between them. */
601 struct dwarf2_per_cu_data per_cu;
602
3019eac3 603 /* The type's signature. */
348e048f
DE
604 ULONGEST signature;
605
3019eac3
DE
606 /* Offset in the TU of the type's DIE, as read from the TU header.
607 If the definition lives in a DWO file, this value is unusable. */
608 cu_offset type_offset_in_tu;
609
610 /* Offset in the section of the type's DIE.
611 If the definition lives in a DWO file, this is the offset in the
612 .debug_types.dwo section.
613 The value is zero until the actual value is known.
614 Zero is otherwise not a valid section offset. */
615 sect_offset type_offset_in_section;
348e048f
DE
616};
617
094b34ac
DE
618/* A struct that can be used as a hash key for tables based on DW_AT_stmt_list.
619 This includes type_unit_group and quick_file_names. */
620
621struct stmt_list_hash
622{
623 /* The DWO unit this table is from or NULL if there is none. */
624 struct dwo_unit *dwo_unit;
625
626 /* Offset in .debug_line or .debug_line.dwo. */
627 sect_offset line_offset;
628};
629
f4dc4d17
DE
630/* Each element of dwarf2_per_objfile->type_unit_groups is a pointer to
631 an object of this type. */
632
633struct type_unit_group
634{
635 /* dwarf2read.c's main "handle" on the symtab.
636 To simplify things we create an artificial CU that "includes" all the
637 type units using this stmt_list so that the rest of the code still has
638 a "per_cu" handle on the symtab.
639 This PER_CU is recognized by having no section. */
640#define IS_TYPE_UNIT_GROUP(per_cu) ((per_cu)->info_or_types_section == NULL)
094b34ac
DE
641 struct dwarf2_per_cu_data per_cu;
642
643 union
644 {
645 /* The TUs that share this DW_AT_stmt_list entry.
646 This is added to while parsing type units to build partial symtabs,
647 and is deleted afterwards and not used again. */
648 VEC (dwarf2_per_cu_ptr) *tus;
f4dc4d17 649
094b34ac
DE
650 /* When reading the line table in "quick" functions, we need a real TU.
651 Any will do, we know they all share the same DW_AT_stmt_list entry.
652 For simplicity's sake, we pick the first one. */
653 struct dwarf2_per_cu_data *first_tu;
654 } t;
f4dc4d17
DE
655
656 /* The primary symtab.
094b34ac
DE
657 Type units in a group needn't all be defined in the same source file,
658 so we create an essentially anonymous symtab as the primary symtab. */
f4dc4d17
DE
659 struct symtab *primary_symtab;
660
094b34ac
DE
661 /* The data used to construct the hash key. */
662 struct stmt_list_hash hash;
f4dc4d17
DE
663
664 /* The number of symtabs from the line header.
665 The value here must match line_header.num_file_names. */
666 unsigned int num_symtabs;
667
668 /* The symbol tables for this TU (obtained from the files listed in
669 DW_AT_stmt_list).
670 WARNING: The order of entries here must match the order of entries
671 in the line header. After the first TU using this type_unit_group, the
672 line header for the subsequent TUs is recreated from this. This is done
673 because we need to use the same symtabs for each TU using the same
674 DW_AT_stmt_list value. Also note that symtabs may be repeated here,
675 there's no guarantee the line header doesn't have duplicate entries. */
676 struct symtab **symtabs;
677};
678
80626a55 679/* These sections are what may appear in a DWO file. */
3019eac3
DE
680
681struct dwo_sections
682{
683 struct dwarf2_section_info abbrev;
3019eac3
DE
684 struct dwarf2_section_info line;
685 struct dwarf2_section_info loc;
09262596
DE
686 struct dwarf2_section_info macinfo;
687 struct dwarf2_section_info macro;
3019eac3
DE
688 struct dwarf2_section_info str;
689 struct dwarf2_section_info str_offsets;
80626a55
DE
690 /* In the case of a virtual DWO file, these two are unused. */
691 struct dwarf2_section_info info;
3019eac3
DE
692 VEC (dwarf2_section_info_def) *types;
693};
694
695/* Common bits of DWO CUs/TUs. */
696
697struct dwo_unit
698{
699 /* Backlink to the containing struct dwo_file. */
700 struct dwo_file *dwo_file;
701
702 /* The "id" that distinguishes this CU/TU.
703 .debug_info calls this "dwo_id", .debug_types calls this "signature".
704 Since signatures came first, we stick with it for consistency. */
705 ULONGEST signature;
706
707 /* The section this CU/TU lives in, in the DWO file. */
708 struct dwarf2_section_info *info_or_types_section;
709
710 /* Same as dwarf2_per_cu_data:{offset,length} but for the DWO section. */
711 sect_offset offset;
712 unsigned int length;
713
714 /* For types, offset in the type's DIE of the type defined by this TU. */
715 cu_offset type_offset_in_tu;
716};
717
80626a55
DE
718/* Data for one DWO file.
719 This includes virtual DWO files that have been packaged into a
720 DWP file. */
3019eac3
DE
721
722struct dwo_file
723{
80626a55
DE
724 /* The DW_AT_GNU_dwo_name attribute. This is the hash key.
725 For virtual DWO files the name is constructed from the section offsets
726 of abbrev,line,loc,str_offsets so that we combine virtual DWO files
727 from related CU+TUs. */
728 const char *name;
3019eac3 729
80626a55
DE
730 /* The bfd, when the file is open. Otherwise this is NULL.
731 This is unused(NULL) for virtual DWO files where we use dwp_file.dbfd. */
732 bfd *dbfd;
3019eac3
DE
733
734 /* Section info for this file. */
735 struct dwo_sections sections;
736
737 /* Table of CUs in the file.
738 Each element is a struct dwo_unit. */
739 htab_t cus;
740
741 /* Table of TUs in the file.
742 Each element is a struct dwo_unit. */
743 htab_t tus;
744};
745
80626a55
DE
746/* These sections are what may appear in a DWP file. */
747
748struct dwp_sections
749{
750 struct dwarf2_section_info str;
751 struct dwarf2_section_info cu_index;
752 struct dwarf2_section_info tu_index;
753 /* The .debug_info.dwo, .debug_types.dwo, and other sections are referenced
754 by section number. We don't need to record them here. */
755};
756
757/* These sections are what may appear in a virtual DWO file. */
758
759struct virtual_dwo_sections
760{
761 struct dwarf2_section_info abbrev;
762 struct dwarf2_section_info line;
763 struct dwarf2_section_info loc;
764 struct dwarf2_section_info macinfo;
765 struct dwarf2_section_info macro;
766 struct dwarf2_section_info str_offsets;
767 /* Each DWP hash table entry records one CU or one TU.
768 That is recorded here, and copied to dwo_unit.info_or_types_section. */
769 struct dwarf2_section_info info_or_types;
770};
771
772/* Contents of DWP hash tables. */
773
774struct dwp_hash_table
775{
776 uint32_t nr_units, nr_slots;
777 const gdb_byte *hash_table, *unit_table, *section_pool;
778};
779
780/* Data for one DWP file. */
781
782struct dwp_file
783{
784 /* Name of the file. */
785 const char *name;
786
787 /* The bfd, when the file is open. Otherwise this is NULL. */
788 bfd *dbfd;
789
790 /* Section info for this file. */
791 struct dwp_sections sections;
792
793 /* Table of CUs in the file. */
794 const struct dwp_hash_table *cus;
795
796 /* Table of TUs in the file. */
797 const struct dwp_hash_table *tus;
798
799 /* Table of loaded CUs/TUs. Each entry is a struct dwo_unit *. */
800 htab_t loaded_cutus;
801
802 /* Table to map ELF section numbers to their sections. */
803 unsigned int num_sections;
804 asection **elf_sections;
805};
806
36586728
TT
807/* This represents a '.dwz' file. */
808
809struct dwz_file
810{
811 /* A dwz file can only contain a few sections. */
812 struct dwarf2_section_info abbrev;
813 struct dwarf2_section_info info;
814 struct dwarf2_section_info str;
815 struct dwarf2_section_info line;
816 struct dwarf2_section_info macro;
2ec9a5e0 817 struct dwarf2_section_info gdb_index;
36586728
TT
818
819 /* The dwz's BFD. */
820 bfd *dwz_bfd;
821};
822
0963b4bd
MS
823/* Struct used to pass misc. parameters to read_die_and_children, et
824 al. which are used for both .debug_info and .debug_types dies.
825 All parameters here are unchanging for the life of the call. This
dee91e82 826 struct exists to abstract away the constant parameters of die reading. */
93311388
DE
827
828struct die_reader_specs
829{
dee91e82 830 /* die_section->asection->owner. */
93311388
DE
831 bfd* abfd;
832
833 /* The CU of the DIE we are parsing. */
834 struct dwarf2_cu *cu;
835
80626a55 836 /* Non-NULL if reading a DWO file (including one packaged into a DWP). */
3019eac3
DE
837 struct dwo_file *dwo_file;
838
dee91e82 839 /* The section the die comes from.
3019eac3 840 This is either .debug_info or .debug_types, or the .dwo variants. */
dee91e82
DE
841 struct dwarf2_section_info *die_section;
842
843 /* die_section->buffer. */
844 gdb_byte *buffer;
f664829e
DE
845
846 /* The end of the buffer. */
847 const gdb_byte *buffer_end;
93311388
DE
848};
849
fd820528 850/* Type of function passed to init_cutu_and_read_dies, et.al. */
dee91e82
DE
851typedef void (die_reader_func_ftype) (const struct die_reader_specs *reader,
852 gdb_byte *info_ptr,
853 struct die_info *comp_unit_die,
854 int has_children,
855 void *data);
856
debd256d
JB
857/* The line number information for a compilation unit (found in the
858 .debug_line section) begins with a "statement program header",
859 which contains the following information. */
860struct line_header
861{
862 unsigned int total_length;
863 unsigned short version;
864 unsigned int header_length;
865 unsigned char minimum_instruction_length;
2dc7f7b3 866 unsigned char maximum_ops_per_instruction;
debd256d
JB
867 unsigned char default_is_stmt;
868 int line_base;
869 unsigned char line_range;
870 unsigned char opcode_base;
871
872 /* standard_opcode_lengths[i] is the number of operands for the
873 standard opcode whose value is i. This means that
874 standard_opcode_lengths[0] is unused, and the last meaningful
875 element is standard_opcode_lengths[opcode_base - 1]. */
876 unsigned char *standard_opcode_lengths;
877
878 /* The include_directories table. NOTE! These strings are not
879 allocated with xmalloc; instead, they are pointers into
880 debug_line_buffer. If you try to free them, `free' will get
881 indigestion. */
882 unsigned int num_include_dirs, include_dirs_size;
883 char **include_dirs;
884
885 /* The file_names table. NOTE! These strings are not allocated
886 with xmalloc; instead, they are pointers into debug_line_buffer.
887 Don't try to free them directly. */
888 unsigned int num_file_names, file_names_size;
889 struct file_entry
c906108c 890 {
debd256d
JB
891 char *name;
892 unsigned int dir_index;
893 unsigned int mod_time;
894 unsigned int length;
aaa75496 895 int included_p; /* Non-zero if referenced by the Line Number Program. */
cb1df416 896 struct symtab *symtab; /* The associated symbol table, if any. */
debd256d
JB
897 } *file_names;
898
899 /* The start and end of the statement program following this
6502dd73 900 header. These point into dwarf2_per_objfile->line_buffer. */
fe1b8b76 901 gdb_byte *statement_program_start, *statement_program_end;
debd256d 902};
c906108c
SS
903
904/* When we construct a partial symbol table entry we only
0963b4bd 905 need this much information. */
c906108c
SS
906struct partial_die_info
907 {
72bf9492 908 /* Offset of this DIE. */
b64f50a1 909 sect_offset offset;
72bf9492
DJ
910
911 /* DWARF-2 tag for this DIE. */
912 ENUM_BITFIELD(dwarf_tag) tag : 16;
913
72bf9492
DJ
914 /* Assorted flags describing the data found in this DIE. */
915 unsigned int has_children : 1;
916 unsigned int is_external : 1;
917 unsigned int is_declaration : 1;
918 unsigned int has_type : 1;
919 unsigned int has_specification : 1;
920 unsigned int has_pc_info : 1;
481860b3 921 unsigned int may_be_inlined : 1;
72bf9492
DJ
922
923 /* Flag set if the SCOPE field of this structure has been
924 computed. */
925 unsigned int scope_set : 1;
926
fa4028e9
JB
927 /* Flag set if the DIE has a byte_size attribute. */
928 unsigned int has_byte_size : 1;
929
98bfdba5
PA
930 /* Flag set if any of the DIE's children are template arguments. */
931 unsigned int has_template_arguments : 1;
932
abc72ce4
DE
933 /* Flag set if fixup_partial_die has been called on this die. */
934 unsigned int fixup_called : 1;
935
36586728
TT
936 /* Flag set if DW_TAG_imported_unit uses DW_FORM_GNU_ref_alt. */
937 unsigned int is_dwz : 1;
938
939 /* Flag set if spec_offset uses DW_FORM_GNU_ref_alt. */
940 unsigned int spec_is_dwz : 1;
941
72bf9492 942 /* The name of this DIE. Normally the value of DW_AT_name, but
94af9270 943 sometimes a default name for unnamed DIEs. */
15d034d0 944 const char *name;
72bf9492 945
abc72ce4
DE
946 /* The linkage name, if present. */
947 const char *linkage_name;
948
72bf9492
DJ
949 /* The scope to prepend to our children. This is generally
950 allocated on the comp_unit_obstack, so will disappear
951 when this compilation unit leaves the cache. */
15d034d0 952 const char *scope;
72bf9492 953
95554aad
TT
954 /* Some data associated with the partial DIE. The tag determines
955 which field is live. */
956 union
957 {
958 /* The location description associated with this DIE, if any. */
959 struct dwarf_block *locdesc;
960 /* The offset of an import, for DW_TAG_imported_unit. */
961 sect_offset offset;
962 } d;
72bf9492
DJ
963
964 /* If HAS_PC_INFO, the PC range associated with this DIE. */
c906108c
SS
965 CORE_ADDR lowpc;
966 CORE_ADDR highpc;
72bf9492 967
93311388 968 /* Pointer into the info_buffer (or types_buffer) pointing at the target of
72bf9492 969 DW_AT_sibling, if any. */
abc72ce4
DE
970 /* NOTE: This member isn't strictly necessary, read_partial_die could
971 return DW_AT_sibling values to its caller load_partial_dies. */
fe1b8b76 972 gdb_byte *sibling;
72bf9492
DJ
973
974 /* If HAS_SPECIFICATION, the offset of the DIE referred to by
975 DW_AT_specification (or DW_AT_abstract_origin or
976 DW_AT_extension). */
b64f50a1 977 sect_offset spec_offset;
72bf9492
DJ
978
979 /* Pointers to this DIE's parent, first child, and next sibling,
980 if any. */
981 struct partial_die_info *die_parent, *die_child, *die_sibling;
c906108c
SS
982 };
983
0963b4bd 984/* This data structure holds the information of an abbrev. */
c906108c
SS
985struct abbrev_info
986 {
987 unsigned int number; /* number identifying abbrev */
988 enum dwarf_tag tag; /* dwarf tag */
f3dd6933
DJ
989 unsigned short has_children; /* boolean */
990 unsigned short num_attrs; /* number of attributes */
c906108c
SS
991 struct attr_abbrev *attrs; /* an array of attribute descriptions */
992 struct abbrev_info *next; /* next in chain */
993 };
994
995struct attr_abbrev
996 {
9d25dd43
DE
997 ENUM_BITFIELD(dwarf_attribute) name : 16;
998 ENUM_BITFIELD(dwarf_form) form : 16;
c906108c
SS
999 };
1000
433df2d4
DE
1001/* Size of abbrev_table.abbrev_hash_table. */
1002#define ABBREV_HASH_SIZE 121
1003
1004/* Top level data structure to contain an abbreviation table. */
1005
1006struct abbrev_table
1007{
f4dc4d17
DE
1008 /* Where the abbrev table came from.
1009 This is used as a sanity check when the table is used. */
433df2d4
DE
1010 sect_offset offset;
1011
1012 /* Storage for the abbrev table. */
1013 struct obstack abbrev_obstack;
1014
1015 /* Hash table of abbrevs.
1016 This is an array of size ABBREV_HASH_SIZE allocated in abbrev_obstack.
1017 It could be statically allocated, but the previous code didn't so we
1018 don't either. */
1019 struct abbrev_info **abbrevs;
1020};
1021
0963b4bd 1022/* Attributes have a name and a value. */
b60c80d6
DJ
1023struct attribute
1024 {
9d25dd43 1025 ENUM_BITFIELD(dwarf_attribute) name : 16;
8285870a
JK
1026 ENUM_BITFIELD(dwarf_form) form : 15;
1027
1028 /* Has DW_STRING already been updated by dwarf2_canonicalize_name? This
1029 field should be in u.str (existing only for DW_STRING) but it is kept
1030 here for better struct attribute alignment. */
1031 unsigned int string_is_canonical : 1;
1032
b60c80d6
DJ
1033 union
1034 {
15d034d0 1035 const char *str;
b60c80d6 1036 struct dwarf_block *blk;
43bbcdc2
PH
1037 ULONGEST unsnd;
1038 LONGEST snd;
b60c80d6 1039 CORE_ADDR addr;
348e048f 1040 struct signatured_type *signatured_type;
b60c80d6
DJ
1041 }
1042 u;
1043 };
1044
0963b4bd 1045/* This data structure holds a complete die structure. */
c906108c
SS
1046struct die_info
1047 {
76815b17
DE
1048 /* DWARF-2 tag for this DIE. */
1049 ENUM_BITFIELD(dwarf_tag) tag : 16;
1050
1051 /* Number of attributes */
98bfdba5
PA
1052 unsigned char num_attrs;
1053
1054 /* True if we're presently building the full type name for the
1055 type derived from this DIE. */
1056 unsigned char building_fullname : 1;
76815b17
DE
1057
1058 /* Abbrev number */
1059 unsigned int abbrev;
1060
93311388 1061 /* Offset in .debug_info or .debug_types section. */
b64f50a1 1062 sect_offset offset;
78ba4af6
JB
1063
1064 /* The dies in a compilation unit form an n-ary tree. PARENT
1065 points to this die's parent; CHILD points to the first child of
1066 this node; and all the children of a given node are chained
4950bc1c 1067 together via their SIBLING fields. */
639d11d3
DC
1068 struct die_info *child; /* Its first child, if any. */
1069 struct die_info *sibling; /* Its next sibling, if any. */
1070 struct die_info *parent; /* Its parent, if any. */
c906108c 1071
b60c80d6
DJ
1072 /* An array of attributes, with NUM_ATTRS elements. There may be
1073 zero, but it's not common and zero-sized arrays are not
1074 sufficiently portable C. */
1075 struct attribute attrs[1];
c906108c
SS
1076 };
1077
0963b4bd 1078/* Get at parts of an attribute structure. */
c906108c
SS
1079
1080#define DW_STRING(attr) ((attr)->u.str)
8285870a 1081#define DW_STRING_IS_CANONICAL(attr) ((attr)->string_is_canonical)
c906108c
SS
1082#define DW_UNSND(attr) ((attr)->u.unsnd)
1083#define DW_BLOCK(attr) ((attr)->u.blk)
1084#define DW_SND(attr) ((attr)->u.snd)
1085#define DW_ADDR(attr) ((attr)->u.addr)
348e048f 1086#define DW_SIGNATURED_TYPE(attr) ((attr)->u.signatured_type)
c906108c 1087
0963b4bd 1088/* Blocks are a bunch of untyped bytes. */
c906108c
SS
1089struct dwarf_block
1090 {
56eb65bd 1091 size_t size;
1d6edc3c
JK
1092
1093 /* Valid only if SIZE is not zero. */
fe1b8b76 1094 gdb_byte *data;
c906108c
SS
1095 };
1096
c906108c
SS
1097#ifndef ATTR_ALLOC_CHUNK
1098#define ATTR_ALLOC_CHUNK 4
1099#endif
1100
c906108c
SS
1101/* Allocate fields for structs, unions and enums in this size. */
1102#ifndef DW_FIELD_ALLOC_CHUNK
1103#define DW_FIELD_ALLOC_CHUNK 4
1104#endif
1105
c906108c
SS
1106/* FIXME: We might want to set this from BFD via bfd_arch_bits_per_byte,
1107 but this would require a corresponding change in unpack_field_as_long
1108 and friends. */
1109static int bits_per_byte = 8;
1110
1111/* The routines that read and process dies for a C struct or C++ class
1112 pass lists of data member fields and lists of member function fields
1113 in an instance of a field_info structure, as defined below. */
1114struct field_info
c5aa993b 1115 {
0963b4bd 1116 /* List of data member and baseclasses fields. */
c5aa993b
JM
1117 struct nextfield
1118 {
1119 struct nextfield *next;
1120 int accessibility;
1121 int virtuality;
1122 struct field field;
1123 }
7d0ccb61 1124 *fields, *baseclasses;
c906108c 1125
7d0ccb61 1126 /* Number of fields (including baseclasses). */
c5aa993b 1127 int nfields;
c906108c 1128
c5aa993b
JM
1129 /* Number of baseclasses. */
1130 int nbaseclasses;
c906108c 1131
c5aa993b
JM
1132 /* Set if the accesibility of one of the fields is not public. */
1133 int non_public_fields;
c906108c 1134
c5aa993b
JM
1135 /* Member function fields array, entries are allocated in the order they
1136 are encountered in the object file. */
1137 struct nextfnfield
1138 {
1139 struct nextfnfield *next;
1140 struct fn_field fnfield;
1141 }
1142 *fnfields;
c906108c 1143
c5aa993b
JM
1144 /* Member function fieldlist array, contains name of possibly overloaded
1145 member function, number of overloaded member functions and a pointer
1146 to the head of the member function field chain. */
1147 struct fnfieldlist
1148 {
15d034d0 1149 const char *name;
c5aa993b
JM
1150 int length;
1151 struct nextfnfield *head;
1152 }
1153 *fnfieldlists;
c906108c 1154
c5aa993b
JM
1155 /* Number of entries in the fnfieldlists array. */
1156 int nfnfields;
98751a41
JK
1157
1158 /* typedefs defined inside this class. TYPEDEF_FIELD_LIST contains head of
1159 a NULL terminated list of TYPEDEF_FIELD_LIST_COUNT elements. */
1160 struct typedef_field_list
1161 {
1162 struct typedef_field field;
1163 struct typedef_field_list *next;
1164 }
1165 *typedef_field_list;
1166 unsigned typedef_field_list_count;
c5aa993b 1167 };
c906108c 1168
10b3939b
DJ
1169/* One item on the queue of compilation units to read in full symbols
1170 for. */
1171struct dwarf2_queue_item
1172{
1173 struct dwarf2_per_cu_data *per_cu;
95554aad 1174 enum language pretend_language;
10b3939b
DJ
1175 struct dwarf2_queue_item *next;
1176};
1177
1178/* The current queue. */
1179static struct dwarf2_queue_item *dwarf2_queue, *dwarf2_queue_tail;
1180
ae038cb0
DJ
1181/* Loaded secondary compilation units are kept in memory until they
1182 have not been referenced for the processing of this many
1183 compilation units. Set this to zero to disable caching. Cache
1184 sizes of up to at least twenty will improve startup time for
1185 typical inter-CU-reference binaries, at an obvious memory cost. */
1186static int dwarf2_max_cache_age = 5;
920d2a44
AC
1187static void
1188show_dwarf2_max_cache_age (struct ui_file *file, int from_tty,
1189 struct cmd_list_element *c, const char *value)
1190{
3e43a32a
MS
1191 fprintf_filtered (file, _("The upper bound on the age of cached "
1192 "dwarf2 compilation units is %s.\n"),
920d2a44
AC
1193 value);
1194}
1195
ae038cb0 1196
0963b4bd 1197/* Various complaints about symbol reading that don't abort the process. */
c906108c 1198
4d3c2250
KB
1199static void
1200dwarf2_statement_list_fits_in_line_number_section_complaint (void)
2e276125 1201{
4d3c2250 1202 complaint (&symfile_complaints,
e2e0b3e5 1203 _("statement list doesn't fit in .debug_line section"));
4d3c2250
KB
1204}
1205
25e43795
DJ
1206static void
1207dwarf2_debug_line_missing_file_complaint (void)
1208{
1209 complaint (&symfile_complaints,
1210 _(".debug_line section has line data without a file"));
1211}
1212
59205f5a
JB
1213static void
1214dwarf2_debug_line_missing_end_sequence_complaint (void)
1215{
1216 complaint (&symfile_complaints,
3e43a32a
MS
1217 _(".debug_line section has line "
1218 "program sequence without an end"));
59205f5a
JB
1219}
1220
4d3c2250
KB
1221static void
1222dwarf2_complex_location_expr_complaint (void)
2e276125 1223{
e2e0b3e5 1224 complaint (&symfile_complaints, _("location expression too complex"));
4d3c2250
KB
1225}
1226
4d3c2250
KB
1227static void
1228dwarf2_const_value_length_mismatch_complaint (const char *arg1, int arg2,
1229 int arg3)
2e276125 1230{
4d3c2250 1231 complaint (&symfile_complaints,
3e43a32a
MS
1232 _("const value length mismatch for '%s', got %d, expected %d"),
1233 arg1, arg2, arg3);
4d3c2250
KB
1234}
1235
1236static void
f664829e 1237dwarf2_section_buffer_overflow_complaint (struct dwarf2_section_info *section)
2e276125 1238{
4d3c2250 1239 complaint (&symfile_complaints,
f664829e
DE
1240 _("debug info runs off end of %s section"
1241 " [in module %s]"),
1242 section->asection->name,
1243 bfd_get_filename (section->asection->owner));
4d3c2250
KB
1244}
1245
1246static void
1247dwarf2_macro_malformed_definition_complaint (const char *arg1)
8e19ed76 1248{
4d3c2250 1249 complaint (&symfile_complaints,
3e43a32a
MS
1250 _("macro debug info contains a "
1251 "malformed macro definition:\n`%s'"),
4d3c2250
KB
1252 arg1);
1253}
1254
1255static void
1256dwarf2_invalid_attrib_class_complaint (const char *arg1, const char *arg2)
8b2dbe47 1257{
4d3c2250 1258 complaint (&symfile_complaints,
3e43a32a
MS
1259 _("invalid attribute class or form for '%s' in '%s'"),
1260 arg1, arg2);
4d3c2250 1261}
c906108c 1262
c906108c
SS
1263/* local function prototypes */
1264
4efb68b1 1265static void dwarf2_locate_sections (bfd *, asection *, void *);
c906108c 1266
aaa75496
JB
1267static void dwarf2_create_include_psymtab (char *, struct partial_symtab *,
1268 struct objfile *);
1269
918dd910
JK
1270static void dwarf2_find_base_address (struct die_info *die,
1271 struct dwarf2_cu *cu);
1272
c67a9c90 1273static void dwarf2_build_psymtabs_hard (struct objfile *);
c906108c 1274
72bf9492
DJ
1275static void scan_partial_symbols (struct partial_die_info *,
1276 CORE_ADDR *, CORE_ADDR *,
5734ee8b 1277 int, struct dwarf2_cu *);
c906108c 1278
72bf9492
DJ
1279static void add_partial_symbol (struct partial_die_info *,
1280 struct dwarf2_cu *);
63d06c5c 1281
72bf9492
DJ
1282static void add_partial_namespace (struct partial_die_info *pdi,
1283 CORE_ADDR *lowpc, CORE_ADDR *highpc,
5734ee8b 1284 int need_pc, struct dwarf2_cu *cu);
63d06c5c 1285
5d7cb8df
JK
1286static void add_partial_module (struct partial_die_info *pdi, CORE_ADDR *lowpc,
1287 CORE_ADDR *highpc, int need_pc,
1288 struct dwarf2_cu *cu);
1289
72bf9492
DJ
1290static void add_partial_enumeration (struct partial_die_info *enum_pdi,
1291 struct dwarf2_cu *cu);
91c24f0a 1292
bc30ff58
JB
1293static void add_partial_subprogram (struct partial_die_info *pdi,
1294 CORE_ADDR *lowpc, CORE_ADDR *highpc,
5734ee8b 1295 int need_pc, struct dwarf2_cu *cu);
bc30ff58 1296
257e7a09
YQ
1297static void dwarf2_read_symtab (struct partial_symtab *,
1298 struct objfile *);
c906108c 1299
a14ed312 1300static void psymtab_to_symtab_1 (struct partial_symtab *);
c906108c 1301
433df2d4
DE
1302static struct abbrev_info *abbrev_table_lookup_abbrev
1303 (const struct abbrev_table *, unsigned int);
1304
1305static struct abbrev_table *abbrev_table_read_table
1306 (struct dwarf2_section_info *, sect_offset);
1307
1308static void abbrev_table_free (struct abbrev_table *);
1309
f4dc4d17
DE
1310static void abbrev_table_free_cleanup (void *);
1311
dee91e82
DE
1312static void dwarf2_read_abbrevs (struct dwarf2_cu *,
1313 struct dwarf2_section_info *);
c906108c 1314
f3dd6933 1315static void dwarf2_free_abbrev_table (void *);
c906108c 1316
6caca83c
CC
1317static unsigned int peek_abbrev_code (bfd *, gdb_byte *);
1318
dee91e82
DE
1319static struct partial_die_info *load_partial_dies
1320 (const struct die_reader_specs *, gdb_byte *, int);
72bf9492 1321
dee91e82
DE
1322static gdb_byte *read_partial_die (const struct die_reader_specs *,
1323 struct partial_die_info *,
1324 struct abbrev_info *,
1325 unsigned int,
1326 gdb_byte *);
c906108c 1327
36586728 1328static struct partial_die_info *find_partial_die (sect_offset, int,
10b3939b 1329 struct dwarf2_cu *);
72bf9492
DJ
1330
1331static void fixup_partial_die (struct partial_die_info *,
1332 struct dwarf2_cu *);
1333
dee91e82
DE
1334static gdb_byte *read_attribute (const struct die_reader_specs *,
1335 struct attribute *, struct attr_abbrev *,
1336 gdb_byte *);
a8329558 1337
a1855c1d 1338static unsigned int read_1_byte (bfd *, const gdb_byte *);
c906108c 1339
a1855c1d 1340static int read_1_signed_byte (bfd *, const gdb_byte *);
c906108c 1341
a1855c1d 1342static unsigned int read_2_bytes (bfd *, const gdb_byte *);
c906108c 1343
a1855c1d 1344static unsigned int read_4_bytes (bfd *, const gdb_byte *);
c906108c 1345
a1855c1d 1346static ULONGEST read_8_bytes (bfd *, const gdb_byte *);
c906108c 1347
fe1b8b76 1348static CORE_ADDR read_address (bfd *, gdb_byte *ptr, struct dwarf2_cu *,
891d2f0b 1349 unsigned int *);
c906108c 1350
c764a876
DE
1351static LONGEST read_initial_length (bfd *, gdb_byte *, unsigned int *);
1352
1353static LONGEST read_checked_initial_length_and_offset
1354 (bfd *, gdb_byte *, const struct comp_unit_head *,
1355 unsigned int *, unsigned int *);
613e1657 1356
fe1b8b76 1357static LONGEST read_offset (bfd *, gdb_byte *, const struct comp_unit_head *,
c764a876
DE
1358 unsigned int *);
1359
1360static LONGEST read_offset_1 (bfd *, gdb_byte *, unsigned int);
613e1657 1361
f4dc4d17
DE
1362static sect_offset read_abbrev_offset (struct dwarf2_section_info *,
1363 sect_offset);
1364
fe1b8b76 1365static gdb_byte *read_n_bytes (bfd *, gdb_byte *, unsigned int);
c906108c 1366
9b1c24c8 1367static char *read_direct_string (bfd *, gdb_byte *, unsigned int *);
c906108c 1368
fe1b8b76
JB
1369static char *read_indirect_string (bfd *, gdb_byte *,
1370 const struct comp_unit_head *,
1371 unsigned int *);
4bdf3d34 1372
36586728
TT
1373static char *read_indirect_string_from_dwz (struct dwz_file *, LONGEST);
1374
12df843f 1375static ULONGEST read_unsigned_leb128 (bfd *, gdb_byte *, unsigned int *);
c906108c 1376
12df843f 1377static LONGEST read_signed_leb128 (bfd *, gdb_byte *, unsigned int *);
c906108c 1378
3019eac3
DE
1379static CORE_ADDR read_addr_index_from_leb128 (struct dwarf2_cu *, gdb_byte *,
1380 unsigned int *);
1381
1382static char *read_str_index (const struct die_reader_specs *reader,
1383 struct dwarf2_cu *cu, ULONGEST str_index);
1384
e142c38c 1385static void set_cu_language (unsigned int, struct dwarf2_cu *);
c906108c 1386
e142c38c
DJ
1387static struct attribute *dwarf2_attr (struct die_info *, unsigned int,
1388 struct dwarf2_cu *);
c906108c 1389
348e048f 1390static struct attribute *dwarf2_attr_no_follow (struct die_info *,
45e58e77 1391 unsigned int);
348e048f 1392
05cf31d1
JB
1393static int dwarf2_flag_true_p (struct die_info *die, unsigned name,
1394 struct dwarf2_cu *cu);
1395
e142c38c 1396static int die_is_declaration (struct die_info *, struct dwarf2_cu *cu);
3ca72b44 1397
e142c38c 1398static struct die_info *die_specification (struct die_info *die,
f2f0e013 1399 struct dwarf2_cu **);
63d06c5c 1400
debd256d
JB
1401static void free_line_header (struct line_header *lh);
1402
aaa75496
JB
1403static void add_file_name (struct line_header *, char *, unsigned int,
1404 unsigned int, unsigned int);
1405
3019eac3
DE
1406static struct line_header *dwarf_decode_line_header (unsigned int offset,
1407 struct dwarf2_cu *cu);
debd256d 1408
f3f5162e
DE
1409static void dwarf_decode_lines (struct line_header *, const char *,
1410 struct dwarf2_cu *, struct partial_symtab *,
1411 int);
c906108c 1412
72b9f47f 1413static void dwarf2_start_subfile (char *, const char *, const char *);
c906108c 1414
f4dc4d17 1415static void dwarf2_start_symtab (struct dwarf2_cu *,
15d034d0 1416 const char *, const char *, CORE_ADDR);
f4dc4d17 1417
a14ed312 1418static struct symbol *new_symbol (struct die_info *, struct type *,
e7c27a73 1419 struct dwarf2_cu *);
c906108c 1420
34eaf542
TT
1421static struct symbol *new_symbol_full (struct die_info *, struct type *,
1422 struct dwarf2_cu *, struct symbol *);
1423
a14ed312 1424static void dwarf2_const_value (struct attribute *, struct symbol *,
e7c27a73 1425 struct dwarf2_cu *);
c906108c 1426
98bfdba5
PA
1427static void dwarf2_const_value_attr (struct attribute *attr,
1428 struct type *type,
1429 const char *name,
1430 struct obstack *obstack,
12df843f 1431 struct dwarf2_cu *cu, LONGEST *value,
98bfdba5
PA
1432 gdb_byte **bytes,
1433 struct dwarf2_locexpr_baton **baton);
2df3850c 1434
e7c27a73 1435static struct type *die_type (struct die_info *, struct dwarf2_cu *);
c906108c 1436
b4ba55a1
JB
1437static int need_gnat_info (struct dwarf2_cu *);
1438
3e43a32a
MS
1439static struct type *die_descriptive_type (struct die_info *,
1440 struct dwarf2_cu *);
b4ba55a1
JB
1441
1442static void set_descriptive_type (struct type *, struct die_info *,
1443 struct dwarf2_cu *);
1444
e7c27a73
DJ
1445static struct type *die_containing_type (struct die_info *,
1446 struct dwarf2_cu *);
c906108c 1447
673bfd45
DE
1448static struct type *lookup_die_type (struct die_info *, struct attribute *,
1449 struct dwarf2_cu *);
c906108c 1450
f792889a 1451static struct type *read_type_die (struct die_info *, struct dwarf2_cu *);
c906108c 1452
673bfd45
DE
1453static struct type *read_type_die_1 (struct die_info *, struct dwarf2_cu *);
1454
0d5cff50 1455static const char *determine_prefix (struct die_info *die, struct dwarf2_cu *);
63d06c5c 1456
6e70227d 1457static char *typename_concat (struct obstack *obs, const char *prefix,
f55ee35c
JK
1458 const char *suffix, int physname,
1459 struct dwarf2_cu *cu);
63d06c5c 1460
e7c27a73 1461static void read_file_scope (struct die_info *, struct dwarf2_cu *);
c906108c 1462
348e048f
DE
1463static void read_type_unit_scope (struct die_info *, struct dwarf2_cu *);
1464
e7c27a73 1465static void read_func_scope (struct die_info *, struct dwarf2_cu *);
c906108c 1466
e7c27a73 1467static void read_lexical_block_scope (struct die_info *, struct dwarf2_cu *);
c906108c 1468
96408a79
SA
1469static void read_call_site_scope (struct die_info *die, struct dwarf2_cu *cu);
1470
ff013f42
JK
1471static int dwarf2_ranges_read (unsigned, CORE_ADDR *, CORE_ADDR *,
1472 struct dwarf2_cu *, struct partial_symtab *);
1473
a14ed312 1474static int dwarf2_get_pc_bounds (struct die_info *,
d85a05f0
DJ
1475 CORE_ADDR *, CORE_ADDR *, struct dwarf2_cu *,
1476 struct partial_symtab *);
c906108c 1477
fae299cd
DC
1478static void get_scope_pc_bounds (struct die_info *,
1479 CORE_ADDR *, CORE_ADDR *,
1480 struct dwarf2_cu *);
1481
801e3a5b
JB
1482static void dwarf2_record_block_ranges (struct die_info *, struct block *,
1483 CORE_ADDR, struct dwarf2_cu *);
1484
a14ed312 1485static void dwarf2_add_field (struct field_info *, struct die_info *,
e7c27a73 1486 struct dwarf2_cu *);
c906108c 1487
a14ed312 1488static void dwarf2_attach_fields_to_type (struct field_info *,
e7c27a73 1489 struct type *, struct dwarf2_cu *);
c906108c 1490
a14ed312 1491static void dwarf2_add_member_fn (struct field_info *,
e26fb1d7 1492 struct die_info *, struct type *,
e7c27a73 1493 struct dwarf2_cu *);
c906108c 1494
a14ed312 1495static void dwarf2_attach_fn_fields_to_type (struct field_info *,
3e43a32a
MS
1496 struct type *,
1497 struct dwarf2_cu *);
c906108c 1498
134d01f1 1499static void process_structure_scope (struct die_info *, struct dwarf2_cu *);
c906108c 1500
e7c27a73 1501static void read_common_block (struct die_info *, struct dwarf2_cu *);
c906108c 1502
e7c27a73 1503static void read_namespace (struct die_info *die, struct dwarf2_cu *);
d9fa45fe 1504
5d7cb8df
JK
1505static void read_module (struct die_info *die, struct dwarf2_cu *cu);
1506
27aa8d6a
SW
1507static void read_import_statement (struct die_info *die, struct dwarf2_cu *);
1508
f55ee35c
JK
1509static struct type *read_module_type (struct die_info *die,
1510 struct dwarf2_cu *cu);
1511
38d518c9 1512static const char *namespace_name (struct die_info *die,
e142c38c 1513 int *is_anonymous, struct dwarf2_cu *);
38d518c9 1514
134d01f1 1515static void process_enumeration_scope (struct die_info *, struct dwarf2_cu *);
c906108c 1516
e7c27a73 1517static CORE_ADDR decode_locdesc (struct dwarf_block *, struct dwarf2_cu *);
c906108c 1518
6e70227d 1519static enum dwarf_array_dim_ordering read_array_order (struct die_info *,
7ca2d3a3
DL
1520 struct dwarf2_cu *);
1521
dee91e82 1522static struct die_info *read_die_and_children (const struct die_reader_specs *,
93311388 1523 gdb_byte *info_ptr,
fe1b8b76 1524 gdb_byte **new_info_ptr,
639d11d3
DC
1525 struct die_info *parent);
1526
dee91e82 1527static struct die_info *read_die_and_siblings (const struct die_reader_specs *,
93311388 1528 gdb_byte *info_ptr,
fe1b8b76 1529 gdb_byte **new_info_ptr,
639d11d3
DC
1530 struct die_info *parent);
1531
3019eac3
DE
1532static gdb_byte *read_full_die_1 (const struct die_reader_specs *,
1533 struct die_info **, gdb_byte *, int *, int);
1534
dee91e82
DE
1535static gdb_byte *read_full_die (const struct die_reader_specs *,
1536 struct die_info **, gdb_byte *, int *);
93311388 1537
e7c27a73 1538static void process_die (struct die_info *, struct dwarf2_cu *);
c906108c 1539
15d034d0
TT
1540static const char *dwarf2_canonicalize_name (const char *, struct dwarf2_cu *,
1541 struct obstack *);
71c25dea 1542
15d034d0 1543static const char *dwarf2_name (struct die_info *die, struct dwarf2_cu *);
9219021c 1544
15d034d0 1545static const char *dwarf2_full_name (const char *name,
98bfdba5
PA
1546 struct die_info *die,
1547 struct dwarf2_cu *cu);
1548
e142c38c 1549static struct die_info *dwarf2_extension (struct die_info *die,
f2f0e013 1550 struct dwarf2_cu **);
9219021c 1551
f39c6ffd 1552static const char *dwarf_tag_name (unsigned int);
c906108c 1553
f39c6ffd 1554static const char *dwarf_attr_name (unsigned int);
c906108c 1555
f39c6ffd 1556static const char *dwarf_form_name (unsigned int);
c906108c 1557
a14ed312 1558static char *dwarf_bool_name (unsigned int);
c906108c 1559
f39c6ffd 1560static const char *dwarf_type_encoding_name (unsigned int);
c906108c 1561
f9aca02d 1562static struct die_info *sibling_die (struct die_info *);
c906108c 1563
d97bc12b
DE
1564static void dump_die_shallow (struct ui_file *, int indent, struct die_info *);
1565
1566static void dump_die_for_error (struct die_info *);
1567
1568static void dump_die_1 (struct ui_file *, int level, int max_level,
1569 struct die_info *);
c906108c 1570
d97bc12b 1571/*static*/ void dump_die (struct die_info *, int max_level);
c906108c 1572
51545339 1573static void store_in_ref_table (struct die_info *,
10b3939b 1574 struct dwarf2_cu *);
c906108c 1575
93311388
DE
1576static int is_ref_attr (struct attribute *);
1577
b64f50a1 1578static sect_offset dwarf2_get_ref_die_offset (struct attribute *);
c906108c 1579
43bbcdc2 1580static LONGEST dwarf2_get_attr_constant_value (struct attribute *, int);
a02abb62 1581
348e048f
DE
1582static struct die_info *follow_die_ref_or_sig (struct die_info *,
1583 struct attribute *,
1584 struct dwarf2_cu **);
1585
10b3939b
DJ
1586static struct die_info *follow_die_ref (struct die_info *,
1587 struct attribute *,
f2f0e013 1588 struct dwarf2_cu **);
c906108c 1589
348e048f
DE
1590static struct die_info *follow_die_sig (struct die_info *,
1591 struct attribute *,
1592 struct dwarf2_cu **);
1593
6c83ed52
TT
1594static struct signatured_type *lookup_signatured_type_at_offset
1595 (struct objfile *objfile,
b64f50a1 1596 struct dwarf2_section_info *section, sect_offset offset);
6c83ed52 1597
e5fe5e75 1598static void load_full_type_unit (struct dwarf2_per_cu_data *per_cu);
348e048f 1599
52dc124a 1600static void read_signatured_type (struct signatured_type *);
348e048f 1601
f4dc4d17 1602static struct type_unit_group *get_type_unit_group
094b34ac 1603 (struct dwarf2_cu *, struct attribute *);
f4dc4d17
DE
1604
1605static void build_type_unit_groups (die_reader_func_ftype *, void *);
1606
c906108c
SS
1607/* memory allocation interface */
1608
7b5a2f43 1609static struct dwarf_block *dwarf_alloc_block (struct dwarf2_cu *);
c906108c 1610
b60c80d6 1611static struct die_info *dwarf_alloc_die (struct dwarf2_cu *, int);
c906108c 1612
09262596 1613static void dwarf_decode_macros (struct dwarf2_cu *, unsigned int,
15d034d0 1614 const char *, int);
2e276125 1615
8e19ed76
PS
1616static int attr_form_is_block (struct attribute *);
1617
3690dd37
JB
1618static int attr_form_is_section_offset (struct attribute *);
1619
1620static int attr_form_is_constant (struct attribute *);
1621
8cf6f0b1
TT
1622static void fill_in_loclist_baton (struct dwarf2_cu *cu,
1623 struct dwarf2_loclist_baton *baton,
1624 struct attribute *attr);
1625
93e7bd98
DJ
1626static void dwarf2_symbol_mark_computed (struct attribute *attr,
1627 struct symbol *sym,
1628 struct dwarf2_cu *cu);
4c2df51b 1629
dee91e82
DE
1630static gdb_byte *skip_one_die (const struct die_reader_specs *reader,
1631 gdb_byte *info_ptr,
1632 struct abbrev_info *abbrev);
4bb7a0a7 1633
72bf9492
DJ
1634static void free_stack_comp_unit (void *);
1635
72bf9492
DJ
1636static hashval_t partial_die_hash (const void *item);
1637
1638static int partial_die_eq (const void *item_lhs, const void *item_rhs);
1639
ae038cb0 1640static struct dwarf2_per_cu_data *dwarf2_find_containing_comp_unit
36586728 1641 (sect_offset offset, unsigned int offset_in_dwz, struct objfile *objfile);
ae038cb0 1642
9816fde3 1643static void init_one_comp_unit (struct dwarf2_cu *cu,
23745b47 1644 struct dwarf2_per_cu_data *per_cu);
9816fde3
JK
1645
1646static void prepare_one_comp_unit (struct dwarf2_cu *cu,
95554aad
TT
1647 struct die_info *comp_unit_die,
1648 enum language pretend_language);
93311388 1649
68dc6402 1650static void free_heap_comp_unit (void *);
ae038cb0
DJ
1651
1652static void free_cached_comp_units (void *);
1653
1654static void age_cached_comp_units (void);
1655
dee91e82 1656static void free_one_cached_comp_unit (struct dwarf2_per_cu_data *);
ae038cb0 1657
f792889a
DJ
1658static struct type *set_die_type (struct die_info *, struct type *,
1659 struct dwarf2_cu *);
1c379e20 1660
ae038cb0
DJ
1661static void create_all_comp_units (struct objfile *);
1662
0e50663e 1663static int create_all_type_units (struct objfile *);
1fd400ff 1664
95554aad
TT
1665static void load_full_comp_unit (struct dwarf2_per_cu_data *,
1666 enum language);
10b3939b 1667
95554aad
TT
1668static void process_full_comp_unit (struct dwarf2_per_cu_data *,
1669 enum language);
10b3939b 1670
f4dc4d17
DE
1671static void process_full_type_unit (struct dwarf2_per_cu_data *,
1672 enum language);
1673
10b3939b
DJ
1674static void dwarf2_add_dependence (struct dwarf2_cu *,
1675 struct dwarf2_per_cu_data *);
1676
ae038cb0
DJ
1677static void dwarf2_mark (struct dwarf2_cu *);
1678
1679static void dwarf2_clear_marks (struct dwarf2_per_cu_data *);
1680
b64f50a1 1681static struct type *get_die_type_at_offset (sect_offset,
673bfd45
DE
1682 struct dwarf2_per_cu_data *per_cu);
1683
f792889a 1684static struct type *get_die_type (struct die_info *die, struct dwarf2_cu *cu);
72019c9c 1685
9291a0cd
TT
1686static void dwarf2_release_queue (void *dummy);
1687
95554aad
TT
1688static void queue_comp_unit (struct dwarf2_per_cu_data *per_cu,
1689 enum language pretend_language);
1690
1691static int maybe_queue_comp_unit (struct dwarf2_cu *this_cu,
1692 struct dwarf2_per_cu_data *per_cu,
1693 enum language pretend_language);
9291a0cd 1694
a0f42c21 1695static void process_queue (void);
9291a0cd
TT
1696
1697static void find_file_and_directory (struct die_info *die,
1698 struct dwarf2_cu *cu,
15d034d0 1699 const char **name, const char **comp_dir);
9291a0cd
TT
1700
1701static char *file_full_name (int file, struct line_header *lh,
1702 const char *comp_dir);
1703
36586728
TT
1704static gdb_byte *read_and_check_comp_unit_head
1705 (struct comp_unit_head *header,
1706 struct dwarf2_section_info *section,
1707 struct dwarf2_section_info *abbrev_section, gdb_byte *info_ptr,
1708 int is_debug_types_section);
1709
fd820528 1710static void init_cutu_and_read_dies
f4dc4d17
DE
1711 (struct dwarf2_per_cu_data *this_cu, struct abbrev_table *abbrev_table,
1712 int use_existing_cu, int keep,
3019eac3
DE
1713 die_reader_func_ftype *die_reader_func, void *data);
1714
dee91e82
DE
1715static void init_cutu_and_read_dies_simple
1716 (struct dwarf2_per_cu_data *this_cu,
1717 die_reader_func_ftype *die_reader_func, void *data);
9291a0cd 1718
673bfd45 1719static htab_t allocate_signatured_type_table (struct objfile *objfile);
1fd400ff 1720
3019eac3
DE
1721static htab_t allocate_dwo_unit_table (struct objfile *objfile);
1722
1723static struct dwo_unit *lookup_dwo_comp_unit
a1855c1d 1724 (struct dwarf2_per_cu_data *, const char *, const char *, ULONGEST);
3019eac3
DE
1725
1726static struct dwo_unit *lookup_dwo_type_unit
a1855c1d 1727 (struct signatured_type *, const char *, const char *);
3019eac3
DE
1728
1729static void free_dwo_file_cleanup (void *);
1730
95554aad
TT
1731static void process_cu_includes (void);
1732
1b80a9fa
JK
1733static void check_producer (struct dwarf2_cu *cu);
1734
9291a0cd
TT
1735#if WORDS_BIGENDIAN
1736
1737/* Convert VALUE between big- and little-endian. */
1738static offset_type
1739byte_swap (offset_type value)
1740{
1741 offset_type result;
1742
1743 result = (value & 0xff) << 24;
1744 result |= (value & 0xff00) << 8;
1745 result |= (value & 0xff0000) >> 8;
1746 result |= (value & 0xff000000) >> 24;
1747 return result;
1748}
1749
1750#define MAYBE_SWAP(V) byte_swap (V)
1751
1752#else
1753#define MAYBE_SWAP(V) (V)
1754#endif /* WORDS_BIGENDIAN */
1755
1756/* The suffix for an index file. */
1757#define INDEX_SUFFIX ".gdb-index"
1758
15d034d0 1759static const char *dwarf2_physname (const char *name, struct die_info *die,
3da10d80
KS
1760 struct dwarf2_cu *cu);
1761
c906108c 1762/* Try to locate the sections we need for DWARF 2 debugging
251d32d9
TG
1763 information and return true if we have enough to do something.
1764 NAMES points to the dwarf2 section names, or is NULL if the standard
1765 ELF names are used. */
c906108c
SS
1766
1767int
251d32d9
TG
1768dwarf2_has_info (struct objfile *objfile,
1769 const struct dwarf2_debug_sections *names)
c906108c 1770{
be391dca
TT
1771 dwarf2_per_objfile = objfile_data (objfile, dwarf2_objfile_data_key);
1772 if (!dwarf2_per_objfile)
1773 {
1774 /* Initialize per-objfile state. */
1775 struct dwarf2_per_objfile *data
1776 = obstack_alloc (&objfile->objfile_obstack, sizeof (*data));
9a619af0 1777
be391dca
TT
1778 memset (data, 0, sizeof (*data));
1779 set_objfile_data (objfile, dwarf2_objfile_data_key, data);
1780 dwarf2_per_objfile = data;
6502dd73 1781
251d32d9
TG
1782 bfd_map_over_sections (objfile->obfd, dwarf2_locate_sections,
1783 (void *) names);
be391dca
TT
1784 dwarf2_per_objfile->objfile = objfile;
1785 }
1786 return (dwarf2_per_objfile->info.asection != NULL
1787 && dwarf2_per_objfile->abbrev.asection != NULL);
c906108c
SS
1788}
1789
251d32d9
TG
1790/* When loading sections, we look either for uncompressed section or for
1791 compressed section names. */
233a11ab
CS
1792
1793static int
251d32d9
TG
1794section_is_p (const char *section_name,
1795 const struct dwarf2_section_names *names)
233a11ab 1796{
251d32d9
TG
1797 if (names->normal != NULL
1798 && strcmp (section_name, names->normal) == 0)
1799 return 1;
1800 if (names->compressed != NULL
1801 && strcmp (section_name, names->compressed) == 0)
1802 return 1;
1803 return 0;
233a11ab
CS
1804}
1805
c906108c
SS
1806/* This function is mapped across the sections and remembers the
1807 offset and size of each of the debugging sections we are interested
1808 in. */
1809
1810static void
251d32d9 1811dwarf2_locate_sections (bfd *abfd, asection *sectp, void *vnames)
c906108c 1812{
251d32d9 1813 const struct dwarf2_debug_sections *names;
dc7650b8 1814 flagword aflag = bfd_get_section_flags (abfd, sectp);
251d32d9
TG
1815
1816 if (vnames == NULL)
1817 names = &dwarf2_elf_names;
1818 else
1819 names = (const struct dwarf2_debug_sections *) vnames;
1820
dc7650b8
JK
1821 if ((aflag & SEC_HAS_CONTENTS) == 0)
1822 {
1823 }
1824 else if (section_is_p (sectp->name, &names->info))
c906108c 1825 {
dce234bc
PP
1826 dwarf2_per_objfile->info.asection = sectp;
1827 dwarf2_per_objfile->info.size = bfd_get_section_size (sectp);
c906108c 1828 }
251d32d9 1829 else if (section_is_p (sectp->name, &names->abbrev))
c906108c 1830 {
dce234bc
PP
1831 dwarf2_per_objfile->abbrev.asection = sectp;
1832 dwarf2_per_objfile->abbrev.size = bfd_get_section_size (sectp);
c906108c 1833 }
251d32d9 1834 else if (section_is_p (sectp->name, &names->line))
c906108c 1835 {
dce234bc
PP
1836 dwarf2_per_objfile->line.asection = sectp;
1837 dwarf2_per_objfile->line.size = bfd_get_section_size (sectp);
c906108c 1838 }
251d32d9 1839 else if (section_is_p (sectp->name, &names->loc))
c906108c 1840 {
dce234bc
PP
1841 dwarf2_per_objfile->loc.asection = sectp;
1842 dwarf2_per_objfile->loc.size = bfd_get_section_size (sectp);
c906108c 1843 }
251d32d9 1844 else if (section_is_p (sectp->name, &names->macinfo))
c906108c 1845 {
dce234bc
PP
1846 dwarf2_per_objfile->macinfo.asection = sectp;
1847 dwarf2_per_objfile->macinfo.size = bfd_get_section_size (sectp);
c906108c 1848 }
cf2c3c16
TT
1849 else if (section_is_p (sectp->name, &names->macro))
1850 {
1851 dwarf2_per_objfile->macro.asection = sectp;
1852 dwarf2_per_objfile->macro.size = bfd_get_section_size (sectp);
1853 }
251d32d9 1854 else if (section_is_p (sectp->name, &names->str))
c906108c 1855 {
dce234bc
PP
1856 dwarf2_per_objfile->str.asection = sectp;
1857 dwarf2_per_objfile->str.size = bfd_get_section_size (sectp);
c906108c 1858 }
3019eac3
DE
1859 else if (section_is_p (sectp->name, &names->addr))
1860 {
1861 dwarf2_per_objfile->addr.asection = sectp;
1862 dwarf2_per_objfile->addr.size = bfd_get_section_size (sectp);
1863 }
251d32d9 1864 else if (section_is_p (sectp->name, &names->frame))
b6af0555 1865 {
dce234bc
PP
1866 dwarf2_per_objfile->frame.asection = sectp;
1867 dwarf2_per_objfile->frame.size = bfd_get_section_size (sectp);
b6af0555 1868 }
251d32d9 1869 else if (section_is_p (sectp->name, &names->eh_frame))
b6af0555 1870 {
dc7650b8
JK
1871 dwarf2_per_objfile->eh_frame.asection = sectp;
1872 dwarf2_per_objfile->eh_frame.size = bfd_get_section_size (sectp);
b6af0555 1873 }
251d32d9 1874 else if (section_is_p (sectp->name, &names->ranges))
af34e669 1875 {
dce234bc
PP
1876 dwarf2_per_objfile->ranges.asection = sectp;
1877 dwarf2_per_objfile->ranges.size = bfd_get_section_size (sectp);
af34e669 1878 }
251d32d9 1879 else if (section_is_p (sectp->name, &names->types))
348e048f 1880 {
8b70b953
TT
1881 struct dwarf2_section_info type_section;
1882
1883 memset (&type_section, 0, sizeof (type_section));
1884 type_section.asection = sectp;
1885 type_section.size = bfd_get_section_size (sectp);
1886
1887 VEC_safe_push (dwarf2_section_info_def, dwarf2_per_objfile->types,
1888 &type_section);
348e048f 1889 }
251d32d9 1890 else if (section_is_p (sectp->name, &names->gdb_index))
9291a0cd
TT
1891 {
1892 dwarf2_per_objfile->gdb_index.asection = sectp;
1893 dwarf2_per_objfile->gdb_index.size = bfd_get_section_size (sectp);
1894 }
dce234bc 1895
72dca2f5
FR
1896 if ((bfd_get_section_flags (abfd, sectp) & SEC_LOAD)
1897 && bfd_section_vma (abfd, sectp) == 0)
1898 dwarf2_per_objfile->has_section_at_zero = 1;
c906108c
SS
1899}
1900
fceca515
DE
1901/* A helper function that decides whether a section is empty,
1902 or not present. */
9e0ac564
TT
1903
1904static int
1905dwarf2_section_empty_p (struct dwarf2_section_info *info)
1906{
1907 return info->asection == NULL || info->size == 0;
1908}
1909
3019eac3
DE
1910/* Read the contents of the section INFO.
1911 OBJFILE is the main object file, but not necessarily the file where
1912 the section comes from. E.g., for DWO files INFO->asection->owner
1913 is the bfd of the DWO file.
dce234bc 1914 If the section is compressed, uncompress it before returning. */
c906108c 1915
dce234bc
PP
1916static void
1917dwarf2_read_section (struct objfile *objfile, struct dwarf2_section_info *info)
c906108c 1918{
dce234bc 1919 asection *sectp = info->asection;
3019eac3 1920 bfd *abfd;
dce234bc
PP
1921 gdb_byte *buf, *retbuf;
1922 unsigned char header[4];
c906108c 1923
be391dca
TT
1924 if (info->readin)
1925 return;
dce234bc 1926 info->buffer = NULL;
be391dca 1927 info->readin = 1;
188dd5d6 1928
9e0ac564 1929 if (dwarf2_section_empty_p (info))
dce234bc 1930 return;
c906108c 1931
3019eac3
DE
1932 abfd = sectp->owner;
1933
4bf44c1c
TT
1934 /* If the section has relocations, we must read it ourselves.
1935 Otherwise we attach it to the BFD. */
1936 if ((sectp->flags & SEC_RELOC) == 0)
dce234bc 1937 {
4bf44c1c 1938 const gdb_byte *bytes = gdb_bfd_map_section (sectp, &info->size);
dce234bc 1939
4bf44c1c
TT
1940 /* We have to cast away const here for historical reasons.
1941 Fixing dwarf2read to be const-correct would be quite nice. */
1942 info->buffer = (gdb_byte *) bytes;
1943 return;
dce234bc 1944 }
dce234bc 1945
4bf44c1c
TT
1946 buf = obstack_alloc (&objfile->objfile_obstack, info->size);
1947 info->buffer = buf;
dce234bc
PP
1948
1949 /* When debugging .o files, we may need to apply relocations; see
1950 http://sourceware.org/ml/gdb-patches/2002-04/msg00136.html .
1951 We never compress sections in .o files, so we only need to
1952 try this when the section is not compressed. */
ac8035ab 1953 retbuf = symfile_relocate_debug_section (objfile, sectp, buf);
dce234bc
PP
1954 if (retbuf != NULL)
1955 {
1956 info->buffer = retbuf;
1957 return;
1958 }
1959
1960 if (bfd_seek (abfd, sectp->filepos, SEEK_SET) != 0
1961 || bfd_bread (buf, info->size, abfd) != info->size)
1962 error (_("Dwarf Error: Can't read DWARF data from '%s'"),
1963 bfd_get_filename (abfd));
1964}
1965
9e0ac564
TT
1966/* A helper function that returns the size of a section in a safe way.
1967 If you are positive that the section has been read before using the
1968 size, then it is safe to refer to the dwarf2_section_info object's
1969 "size" field directly. In other cases, you must call this
1970 function, because for compressed sections the size field is not set
1971 correctly until the section has been read. */
1972
1973static bfd_size_type
1974dwarf2_section_size (struct objfile *objfile,
1975 struct dwarf2_section_info *info)
1976{
1977 if (!info->readin)
1978 dwarf2_read_section (objfile, info);
1979 return info->size;
1980}
1981
dce234bc 1982/* Fill in SECTP, BUFP and SIZEP with section info, given OBJFILE and
0963b4bd 1983 SECTION_NAME. */
af34e669 1984
dce234bc 1985void
3017a003
TG
1986dwarf2_get_section_info (struct objfile *objfile,
1987 enum dwarf2_section_enum sect,
dce234bc
PP
1988 asection **sectp, gdb_byte **bufp,
1989 bfd_size_type *sizep)
1990{
1991 struct dwarf2_per_objfile *data
1992 = objfile_data (objfile, dwarf2_objfile_data_key);
1993 struct dwarf2_section_info *info;
a3b2a86b
TT
1994
1995 /* We may see an objfile without any DWARF, in which case we just
1996 return nothing. */
1997 if (data == NULL)
1998 {
1999 *sectp = NULL;
2000 *bufp = NULL;
2001 *sizep = 0;
2002 return;
2003 }
3017a003
TG
2004 switch (sect)
2005 {
2006 case DWARF2_DEBUG_FRAME:
2007 info = &data->frame;
2008 break;
2009 case DWARF2_EH_FRAME:
2010 info = &data->eh_frame;
2011 break;
2012 default:
2013 gdb_assert_not_reached ("unexpected section");
2014 }
dce234bc 2015
9e0ac564 2016 dwarf2_read_section (objfile, info);
dce234bc
PP
2017
2018 *sectp = info->asection;
2019 *bufp = info->buffer;
2020 *sizep = info->size;
2021}
2022
36586728
TT
2023/* A helper function to find the sections for a .dwz file. */
2024
2025static void
2026locate_dwz_sections (bfd *abfd, asection *sectp, void *arg)
2027{
2028 struct dwz_file *dwz_file = arg;
2029
2030 /* Note that we only support the standard ELF names, because .dwz
2031 is ELF-only (at the time of writing). */
2032 if (section_is_p (sectp->name, &dwarf2_elf_names.abbrev))
2033 {
2034 dwz_file->abbrev.asection = sectp;
2035 dwz_file->abbrev.size = bfd_get_section_size (sectp);
2036 }
2037 else if (section_is_p (sectp->name, &dwarf2_elf_names.info))
2038 {
2039 dwz_file->info.asection = sectp;
2040 dwz_file->info.size = bfd_get_section_size (sectp);
2041 }
2042 else if (section_is_p (sectp->name, &dwarf2_elf_names.str))
2043 {
2044 dwz_file->str.asection = sectp;
2045 dwz_file->str.size = bfd_get_section_size (sectp);
2046 }
2047 else if (section_is_p (sectp->name, &dwarf2_elf_names.line))
2048 {
2049 dwz_file->line.asection = sectp;
2050 dwz_file->line.size = bfd_get_section_size (sectp);
2051 }
2052 else if (section_is_p (sectp->name, &dwarf2_elf_names.macro))
2053 {
2054 dwz_file->macro.asection = sectp;
2055 dwz_file->macro.size = bfd_get_section_size (sectp);
2056 }
2ec9a5e0
TT
2057 else if (section_is_p (sectp->name, &dwarf2_elf_names.gdb_index))
2058 {
2059 dwz_file->gdb_index.asection = sectp;
2060 dwz_file->gdb_index.size = bfd_get_section_size (sectp);
2061 }
36586728
TT
2062}
2063
2064/* Open the separate '.dwz' debug file, if needed. Error if the file
2065 cannot be found. */
2066
2067static struct dwz_file *
2068dwarf2_get_dwz_file (void)
2069{
2070 bfd *abfd, *dwz_bfd;
2071 asection *section;
2072 gdb_byte *data;
2073 struct cleanup *cleanup;
2074 const char *filename;
2075 struct dwz_file *result;
2076
2077 if (dwarf2_per_objfile->dwz_file != NULL)
2078 return dwarf2_per_objfile->dwz_file;
2079
2080 abfd = dwarf2_per_objfile->objfile->obfd;
2081 section = bfd_get_section_by_name (abfd, ".gnu_debugaltlink");
2082 if (section == NULL)
2083 error (_("could not find '.gnu_debugaltlink' section"));
2084 if (!bfd_malloc_and_get_section (abfd, section, &data))
2085 error (_("could not read '.gnu_debugaltlink' section: %s"),
2086 bfd_errmsg (bfd_get_error ()));
2087 cleanup = make_cleanup (xfree, data);
2088
2089 filename = data;
2090 if (!IS_ABSOLUTE_PATH (filename))
2091 {
2092 char *abs = gdb_realpath (dwarf2_per_objfile->objfile->name);
2093 char *rel;
2094
2095 make_cleanup (xfree, abs);
2096 abs = ldirname (abs);
2097 make_cleanup (xfree, abs);
2098
2099 rel = concat (abs, SLASH_STRING, filename, (char *) NULL);
2100 make_cleanup (xfree, rel);
2101 filename = rel;
2102 }
2103
2104 /* The format is just a NUL-terminated file name, followed by the
2105 build-id. For now, though, we ignore the build-id. */
2106 dwz_bfd = gdb_bfd_open (filename, gnutarget, -1);
2107 if (dwz_bfd == NULL)
2108 error (_("could not read '%s': %s"), filename,
2109 bfd_errmsg (bfd_get_error ()));
2110
2111 if (!bfd_check_format (dwz_bfd, bfd_object))
2112 {
2113 gdb_bfd_unref (dwz_bfd);
2114 error (_("file '%s' was not usable: %s"), filename,
2115 bfd_errmsg (bfd_get_error ()));
2116 }
2117
2118 result = OBSTACK_ZALLOC (&dwarf2_per_objfile->objfile->objfile_obstack,
2119 struct dwz_file);
2120 result->dwz_bfd = dwz_bfd;
2121
2122 bfd_map_over_sections (dwz_bfd, locate_dwz_sections, result);
2123
2124 do_cleanups (cleanup);
2125
8d2cc612 2126 dwarf2_per_objfile->dwz_file = result;
36586728
TT
2127 return result;
2128}
9291a0cd 2129\f
7b9f3c50
DE
2130/* DWARF quick_symbols_functions support. */
2131
2132/* TUs can share .debug_line entries, and there can be a lot more TUs than
2133 unique line tables, so we maintain a separate table of all .debug_line
2134 derived entries to support the sharing.
2135 All the quick functions need is the list of file names. We discard the
2136 line_header when we're done and don't need to record it here. */
2137struct quick_file_names
2138{
094b34ac
DE
2139 /* The data used to construct the hash key. */
2140 struct stmt_list_hash hash;
7b9f3c50
DE
2141
2142 /* The number of entries in file_names, real_names. */
2143 unsigned int num_file_names;
2144
2145 /* The file names from the line table, after being run through
2146 file_full_name. */
2147 const char **file_names;
2148
2149 /* The file names from the line table after being run through
2150 gdb_realpath. These are computed lazily. */
2151 const char **real_names;
2152};
2153
2154/* When using the index (and thus not using psymtabs), each CU has an
2155 object of this type. This is used to hold information needed by
2156 the various "quick" methods. */
2157struct dwarf2_per_cu_quick_data
2158{
2159 /* The file table. This can be NULL if there was no file table
2160 or it's currently not read in.
2161 NOTE: This points into dwarf2_per_objfile->quick_file_names_table. */
2162 struct quick_file_names *file_names;
2163
2164 /* The corresponding symbol table. This is NULL if symbols for this
2165 CU have not yet been read. */
2166 struct symtab *symtab;
2167
2168 /* A temporary mark bit used when iterating over all CUs in
2169 expand_symtabs_matching. */
2170 unsigned int mark : 1;
2171
2172 /* True if we've tried to read the file table and found there isn't one.
2173 There will be no point in trying to read it again next time. */
2174 unsigned int no_file_data : 1;
2175};
2176
094b34ac
DE
2177/* Utility hash function for a stmt_list_hash. */
2178
2179static hashval_t
2180hash_stmt_list_entry (const struct stmt_list_hash *stmt_list_hash)
2181{
2182 hashval_t v = 0;
2183
2184 if (stmt_list_hash->dwo_unit != NULL)
2185 v += (uintptr_t) stmt_list_hash->dwo_unit->dwo_file;
2186 v += stmt_list_hash->line_offset.sect_off;
2187 return v;
2188}
2189
2190/* Utility equality function for a stmt_list_hash. */
2191
2192static int
2193eq_stmt_list_entry (const struct stmt_list_hash *lhs,
2194 const struct stmt_list_hash *rhs)
2195{
2196 if ((lhs->dwo_unit != NULL) != (rhs->dwo_unit != NULL))
2197 return 0;
2198 if (lhs->dwo_unit != NULL
2199 && lhs->dwo_unit->dwo_file != rhs->dwo_unit->dwo_file)
2200 return 0;
2201
2202 return lhs->line_offset.sect_off == rhs->line_offset.sect_off;
2203}
2204
7b9f3c50
DE
2205/* Hash function for a quick_file_names. */
2206
2207static hashval_t
2208hash_file_name_entry (const void *e)
2209{
2210 const struct quick_file_names *file_data = e;
2211
094b34ac 2212 return hash_stmt_list_entry (&file_data->hash);
7b9f3c50
DE
2213}
2214
2215/* Equality function for a quick_file_names. */
2216
2217static int
2218eq_file_name_entry (const void *a, const void *b)
2219{
2220 const struct quick_file_names *ea = a;
2221 const struct quick_file_names *eb = b;
2222
094b34ac 2223 return eq_stmt_list_entry (&ea->hash, &eb->hash);
7b9f3c50
DE
2224}
2225
2226/* Delete function for a quick_file_names. */
2227
2228static void
2229delete_file_name_entry (void *e)
2230{
2231 struct quick_file_names *file_data = e;
2232 int i;
2233
2234 for (i = 0; i < file_data->num_file_names; ++i)
2235 {
2236 xfree ((void*) file_data->file_names[i]);
2237 if (file_data->real_names)
2238 xfree ((void*) file_data->real_names[i]);
2239 }
2240
2241 /* The space for the struct itself lives on objfile_obstack,
2242 so we don't free it here. */
2243}
2244
2245/* Create a quick_file_names hash table. */
2246
2247static htab_t
2248create_quick_file_names_table (unsigned int nr_initial_entries)
2249{
2250 return htab_create_alloc (nr_initial_entries,
2251 hash_file_name_entry, eq_file_name_entry,
2252 delete_file_name_entry, xcalloc, xfree);
2253}
9291a0cd 2254
918dd910
JK
2255/* Read in PER_CU->CU. This function is unrelated to symtabs, symtab would
2256 have to be created afterwards. You should call age_cached_comp_units after
2257 processing PER_CU->CU. dw2_setup must have been already called. */
2258
2259static void
2260load_cu (struct dwarf2_per_cu_data *per_cu)
2261{
3019eac3 2262 if (per_cu->is_debug_types)
e5fe5e75 2263 load_full_type_unit (per_cu);
918dd910 2264 else
95554aad 2265 load_full_comp_unit (per_cu, language_minimal);
918dd910 2266
918dd910 2267 gdb_assert (per_cu->cu != NULL);
2dc860c0
DE
2268
2269 dwarf2_find_base_address (per_cu->cu->dies, per_cu->cu);
918dd910
JK
2270}
2271
a0f42c21 2272/* Read in the symbols for PER_CU. */
2fdf6df6 2273
9291a0cd 2274static void
a0f42c21 2275dw2_do_instantiate_symtab (struct dwarf2_per_cu_data *per_cu)
9291a0cd
TT
2276{
2277 struct cleanup *back_to;
2278
f4dc4d17
DE
2279 /* Skip type_unit_groups, reading the type units they contain
2280 is handled elsewhere. */
2281 if (IS_TYPE_UNIT_GROUP (per_cu))
2282 return;
2283
9291a0cd
TT
2284 back_to = make_cleanup (dwarf2_release_queue, NULL);
2285
95554aad
TT
2286 if (dwarf2_per_objfile->using_index
2287 ? per_cu->v.quick->symtab == NULL
2288 : (per_cu->v.psymtab == NULL || !per_cu->v.psymtab->readin))
2289 {
2290 queue_comp_unit (per_cu, language_minimal);
2291 load_cu (per_cu);
2292 }
9291a0cd 2293
a0f42c21 2294 process_queue ();
9291a0cd
TT
2295
2296 /* Age the cache, releasing compilation units that have not
2297 been used recently. */
2298 age_cached_comp_units ();
2299
2300 do_cleanups (back_to);
2301}
2302
2303/* Ensure that the symbols for PER_CU have been read in. OBJFILE is
2304 the objfile from which this CU came. Returns the resulting symbol
2305 table. */
2fdf6df6 2306
9291a0cd 2307static struct symtab *
a0f42c21 2308dw2_instantiate_symtab (struct dwarf2_per_cu_data *per_cu)
9291a0cd 2309{
95554aad 2310 gdb_assert (dwarf2_per_objfile->using_index);
9291a0cd
TT
2311 if (!per_cu->v.quick->symtab)
2312 {
2313 struct cleanup *back_to = make_cleanup (free_cached_comp_units, NULL);
2314 increment_reading_symtab ();
a0f42c21 2315 dw2_do_instantiate_symtab (per_cu);
95554aad 2316 process_cu_includes ();
9291a0cd
TT
2317 do_cleanups (back_to);
2318 }
2319 return per_cu->v.quick->symtab;
2320}
2321
f4dc4d17
DE
2322/* Return the CU given its index.
2323
2324 This is intended for loops like:
2325
2326 for (i = 0; i < (dwarf2_per_objfile->n_comp_units
2327 + dwarf2_per_objfile->n_type_units); ++i)
2328 {
2329 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
2330
2331 ...;
2332 }
2333*/
2fdf6df6 2334
1fd400ff
TT
2335static struct dwarf2_per_cu_data *
2336dw2_get_cu (int index)
2337{
2338 if (index >= dwarf2_per_objfile->n_comp_units)
2339 {
f4dc4d17 2340 index -= dwarf2_per_objfile->n_comp_units;
094b34ac
DE
2341 gdb_assert (index < dwarf2_per_objfile->n_type_units);
2342 return &dwarf2_per_objfile->all_type_units[index]->per_cu;
f4dc4d17
DE
2343 }
2344
2345 return dwarf2_per_objfile->all_comp_units[index];
2346}
2347
2348/* Return the primary CU given its index.
2349 The difference between this function and dw2_get_cu is in the handling
2350 of type units (TUs). Here we return the type_unit_group object.
2351
2352 This is intended for loops like:
2353
2354 for (i = 0; i < (dwarf2_per_objfile->n_comp_units
2355 + dwarf2_per_objfile->n_type_unit_groups); ++i)
2356 {
2357 struct dwarf2_per_cu_data *per_cu = dw2_get_primary_cu (i);
2358
2359 ...;
2360 }
2361*/
2362
2363static struct dwarf2_per_cu_data *
2364dw2_get_primary_cu (int index)
2365{
2366 if (index >= dwarf2_per_objfile->n_comp_units)
2367 {
1fd400ff 2368 index -= dwarf2_per_objfile->n_comp_units;
094b34ac
DE
2369 gdb_assert (index < dwarf2_per_objfile->n_type_unit_groups);
2370 return &dwarf2_per_objfile->all_type_unit_groups[index]->per_cu;
1fd400ff 2371 }
f4dc4d17 2372
1fd400ff
TT
2373 return dwarf2_per_objfile->all_comp_units[index];
2374}
2375
2ec9a5e0
TT
2376/* A helper for create_cus_from_index that handles a given list of
2377 CUs. */
2fdf6df6 2378
74a0d9f6 2379static void
2ec9a5e0
TT
2380create_cus_from_index_list (struct objfile *objfile,
2381 const gdb_byte *cu_list, offset_type n_elements,
2382 struct dwarf2_section_info *section,
2383 int is_dwz,
2384 int base_offset)
9291a0cd
TT
2385{
2386 offset_type i;
9291a0cd 2387
2ec9a5e0 2388 for (i = 0; i < n_elements; i += 2)
9291a0cd
TT
2389 {
2390 struct dwarf2_per_cu_data *the_cu;
2391 ULONGEST offset, length;
2392
74a0d9f6
JK
2393 gdb_static_assert (sizeof (ULONGEST) >= 8);
2394 offset = extract_unsigned_integer (cu_list, 8, BFD_ENDIAN_LITTLE);
2395 length = extract_unsigned_integer (cu_list + 8, 8, BFD_ENDIAN_LITTLE);
9291a0cd
TT
2396 cu_list += 2 * 8;
2397
2398 the_cu = OBSTACK_ZALLOC (&objfile->objfile_obstack,
2399 struct dwarf2_per_cu_data);
b64f50a1 2400 the_cu->offset.sect_off = offset;
9291a0cd
TT
2401 the_cu->length = length;
2402 the_cu->objfile = objfile;
2ec9a5e0 2403 the_cu->info_or_types_section = section;
9291a0cd
TT
2404 the_cu->v.quick = OBSTACK_ZALLOC (&objfile->objfile_obstack,
2405 struct dwarf2_per_cu_quick_data);
2ec9a5e0
TT
2406 the_cu->is_dwz = is_dwz;
2407 dwarf2_per_objfile->all_comp_units[base_offset + i / 2] = the_cu;
9291a0cd 2408 }
9291a0cd
TT
2409}
2410
2ec9a5e0 2411/* Read the CU list from the mapped index, and use it to create all
74a0d9f6 2412 the CU objects for this objfile. */
2ec9a5e0 2413
74a0d9f6 2414static void
2ec9a5e0
TT
2415create_cus_from_index (struct objfile *objfile,
2416 const gdb_byte *cu_list, offset_type cu_list_elements,
2417 const gdb_byte *dwz_list, offset_type dwz_elements)
2418{
2419 struct dwz_file *dwz;
2420
2421 dwarf2_per_objfile->n_comp_units = (cu_list_elements + dwz_elements) / 2;
2422 dwarf2_per_objfile->all_comp_units
2423 = obstack_alloc (&objfile->objfile_obstack,
2424 dwarf2_per_objfile->n_comp_units
2425 * sizeof (struct dwarf2_per_cu_data *));
2426
74a0d9f6
JK
2427 create_cus_from_index_list (objfile, cu_list, cu_list_elements,
2428 &dwarf2_per_objfile->info, 0, 0);
2ec9a5e0
TT
2429
2430 if (dwz_elements == 0)
74a0d9f6 2431 return;
2ec9a5e0
TT
2432
2433 dwz = dwarf2_get_dwz_file ();
74a0d9f6
JK
2434 create_cus_from_index_list (objfile, dwz_list, dwz_elements, &dwz->info, 1,
2435 cu_list_elements / 2);
2ec9a5e0
TT
2436}
2437
1fd400ff 2438/* Create the signatured type hash table from the index. */
673bfd45 2439
74a0d9f6 2440static void
673bfd45 2441create_signatured_type_table_from_index (struct objfile *objfile,
8b70b953 2442 struct dwarf2_section_info *section,
673bfd45
DE
2443 const gdb_byte *bytes,
2444 offset_type elements)
1fd400ff
TT
2445{
2446 offset_type i;
673bfd45 2447 htab_t sig_types_hash;
1fd400ff 2448
d467dd73
DE
2449 dwarf2_per_objfile->n_type_units = elements / 3;
2450 dwarf2_per_objfile->all_type_units
1fd400ff 2451 = obstack_alloc (&objfile->objfile_obstack,
d467dd73 2452 dwarf2_per_objfile->n_type_units
b4dd5633 2453 * sizeof (struct signatured_type *));
1fd400ff 2454
673bfd45 2455 sig_types_hash = allocate_signatured_type_table (objfile);
1fd400ff
TT
2456
2457 for (i = 0; i < elements; i += 3)
2458 {
52dc124a
DE
2459 struct signatured_type *sig_type;
2460 ULONGEST offset, type_offset_in_tu, signature;
1fd400ff
TT
2461 void **slot;
2462
74a0d9f6
JK
2463 gdb_static_assert (sizeof (ULONGEST) >= 8);
2464 offset = extract_unsigned_integer (bytes, 8, BFD_ENDIAN_LITTLE);
2465 type_offset_in_tu = extract_unsigned_integer (bytes + 8, 8,
2466 BFD_ENDIAN_LITTLE);
1fd400ff
TT
2467 signature = extract_unsigned_integer (bytes + 16, 8, BFD_ENDIAN_LITTLE);
2468 bytes += 3 * 8;
2469
52dc124a 2470 sig_type = OBSTACK_ZALLOC (&objfile->objfile_obstack,
1fd400ff 2471 struct signatured_type);
52dc124a 2472 sig_type->signature = signature;
3019eac3
DE
2473 sig_type->type_offset_in_tu.cu_off = type_offset_in_tu;
2474 sig_type->per_cu.is_debug_types = 1;
2475 sig_type->per_cu.info_or_types_section = section;
52dc124a
DE
2476 sig_type->per_cu.offset.sect_off = offset;
2477 sig_type->per_cu.objfile = objfile;
2478 sig_type->per_cu.v.quick
1fd400ff
TT
2479 = OBSTACK_ZALLOC (&objfile->objfile_obstack,
2480 struct dwarf2_per_cu_quick_data);
2481
52dc124a
DE
2482 slot = htab_find_slot (sig_types_hash, sig_type, INSERT);
2483 *slot = sig_type;
1fd400ff 2484
b4dd5633 2485 dwarf2_per_objfile->all_type_units[i / 3] = sig_type;
1fd400ff
TT
2486 }
2487
673bfd45 2488 dwarf2_per_objfile->signatured_types = sig_types_hash;
1fd400ff
TT
2489}
2490
9291a0cd
TT
2491/* Read the address map data from the mapped index, and use it to
2492 populate the objfile's psymtabs_addrmap. */
2fdf6df6 2493
9291a0cd
TT
2494static void
2495create_addrmap_from_index (struct objfile *objfile, struct mapped_index *index)
2496{
2497 const gdb_byte *iter, *end;
2498 struct obstack temp_obstack;
2499 struct addrmap *mutable_map;
2500 struct cleanup *cleanup;
2501 CORE_ADDR baseaddr;
2502
2503 obstack_init (&temp_obstack);
2504 cleanup = make_cleanup_obstack_free (&temp_obstack);
2505 mutable_map = addrmap_create_mutable (&temp_obstack);
2506
2507 iter = index->address_table;
2508 end = iter + index->address_table_size;
2509
2510 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
2511
2512 while (iter < end)
2513 {
2514 ULONGEST hi, lo, cu_index;
2515 lo = extract_unsigned_integer (iter, 8, BFD_ENDIAN_LITTLE);
2516 iter += 8;
2517 hi = extract_unsigned_integer (iter, 8, BFD_ENDIAN_LITTLE);
2518 iter += 8;
2519 cu_index = extract_unsigned_integer (iter, 4, BFD_ENDIAN_LITTLE);
2520 iter += 4;
2521
2522 addrmap_set_empty (mutable_map, lo + baseaddr, hi + baseaddr - 1,
1fd400ff 2523 dw2_get_cu (cu_index));
9291a0cd
TT
2524 }
2525
2526 objfile->psymtabs_addrmap = addrmap_create_fixed (mutable_map,
2527 &objfile->objfile_obstack);
2528 do_cleanups (cleanup);
2529}
2530
59d7bcaf
JK
2531/* The hash function for strings in the mapped index. This is the same as
2532 SYMBOL_HASH_NEXT, but we keep a separate copy to maintain control over the
2533 implementation. This is necessary because the hash function is tied to the
2534 format of the mapped index file. The hash values do not have to match with
559a7a62
JK
2535 SYMBOL_HASH_NEXT.
2536
2537 Use INT_MAX for INDEX_VERSION if you generate the current index format. */
2fdf6df6 2538
9291a0cd 2539static hashval_t
559a7a62 2540mapped_index_string_hash (int index_version, const void *p)
9291a0cd
TT
2541{
2542 const unsigned char *str = (const unsigned char *) p;
2543 hashval_t r = 0;
2544 unsigned char c;
2545
2546 while ((c = *str++) != 0)
559a7a62
JK
2547 {
2548 if (index_version >= 5)
2549 c = tolower (c);
2550 r = r * 67 + c - 113;
2551 }
9291a0cd
TT
2552
2553 return r;
2554}
2555
2556/* Find a slot in the mapped index INDEX for the object named NAME.
2557 If NAME is found, set *VEC_OUT to point to the CU vector in the
2558 constant pool and return 1. If NAME cannot be found, return 0. */
2fdf6df6 2559
9291a0cd
TT
2560static int
2561find_slot_in_mapped_hash (struct mapped_index *index, const char *name,
2562 offset_type **vec_out)
2563{
0cf03b49
JK
2564 struct cleanup *back_to = make_cleanup (null_cleanup, 0);
2565 offset_type hash;
9291a0cd 2566 offset_type slot, step;
559a7a62 2567 int (*cmp) (const char *, const char *);
9291a0cd 2568
0cf03b49
JK
2569 if (current_language->la_language == language_cplus
2570 || current_language->la_language == language_java
2571 || current_language->la_language == language_fortran)
2572 {
2573 /* NAME is already canonical. Drop any qualifiers as .gdb_index does
2574 not contain any. */
2575 const char *paren = strchr (name, '(');
2576
2577 if (paren)
2578 {
2579 char *dup;
2580
2581 dup = xmalloc (paren - name + 1);
2582 memcpy (dup, name, paren - name);
2583 dup[paren - name] = 0;
2584
2585 make_cleanup (xfree, dup);
2586 name = dup;
2587 }
2588 }
2589
559a7a62 2590 /* Index version 4 did not support case insensitive searches. But the
feea76c2 2591 indices for case insensitive languages are built in lowercase, therefore
559a7a62
JK
2592 simulate our NAME being searched is also lowercased. */
2593 hash = mapped_index_string_hash ((index->version == 4
2594 && case_sensitivity == case_sensitive_off
2595 ? 5 : index->version),
2596 name);
2597
3876f04e
DE
2598 slot = hash & (index->symbol_table_slots - 1);
2599 step = ((hash * 17) & (index->symbol_table_slots - 1)) | 1;
559a7a62 2600 cmp = (case_sensitivity == case_sensitive_on ? strcmp : strcasecmp);
9291a0cd
TT
2601
2602 for (;;)
2603 {
2604 /* Convert a slot number to an offset into the table. */
2605 offset_type i = 2 * slot;
2606 const char *str;
3876f04e 2607 if (index->symbol_table[i] == 0 && index->symbol_table[i + 1] == 0)
0cf03b49
JK
2608 {
2609 do_cleanups (back_to);
2610 return 0;
2611 }
9291a0cd 2612
3876f04e 2613 str = index->constant_pool + MAYBE_SWAP (index->symbol_table[i]);
559a7a62 2614 if (!cmp (name, str))
9291a0cd
TT
2615 {
2616 *vec_out = (offset_type *) (index->constant_pool
3876f04e 2617 + MAYBE_SWAP (index->symbol_table[i + 1]));
0cf03b49 2618 do_cleanups (back_to);
9291a0cd
TT
2619 return 1;
2620 }
2621
3876f04e 2622 slot = (slot + step) & (index->symbol_table_slots - 1);
9291a0cd
TT
2623 }
2624}
2625
2ec9a5e0
TT
2626/* A helper function that reads the .gdb_index from SECTION and fills
2627 in MAP. FILENAME is the name of the file containing the section;
2628 it is used for error reporting. DEPRECATED_OK is nonzero if it is
2629 ok to use deprecated sections.
2630
2631 CU_LIST, CU_LIST_ELEMENTS, TYPES_LIST, and TYPES_LIST_ELEMENTS are
2632 out parameters that are filled in with information about the CU and
2633 TU lists in the section.
2634
2635 Returns 1 if all went well, 0 otherwise. */
2fdf6df6 2636
9291a0cd 2637static int
2ec9a5e0
TT
2638read_index_from_section (struct objfile *objfile,
2639 const char *filename,
2640 int deprecated_ok,
2641 struct dwarf2_section_info *section,
2642 struct mapped_index *map,
2643 const gdb_byte **cu_list,
2644 offset_type *cu_list_elements,
2645 const gdb_byte **types_list,
2646 offset_type *types_list_elements)
9291a0cd 2647{
9291a0cd 2648 char *addr;
2ec9a5e0 2649 offset_type version;
b3b272e1 2650 offset_type *metadata;
1fd400ff 2651 int i;
9291a0cd 2652
2ec9a5e0 2653 if (dwarf2_section_empty_p (section))
9291a0cd 2654 return 0;
82430852
JK
2655
2656 /* Older elfutils strip versions could keep the section in the main
2657 executable while splitting it for the separate debug info file. */
2ec9a5e0 2658 if ((bfd_get_file_flags (section->asection) & SEC_HAS_CONTENTS) == 0)
82430852
JK
2659 return 0;
2660
2ec9a5e0 2661 dwarf2_read_section (objfile, section);
9291a0cd 2662
2ec9a5e0 2663 addr = section->buffer;
9291a0cd 2664 /* Version check. */
1fd400ff 2665 version = MAYBE_SWAP (*(offset_type *) addr);
987d643c 2666 /* Versions earlier than 3 emitted every copy of a psymbol. This
a6e293d1 2667 causes the index to behave very poorly for certain requests. Version 3
831adc1f 2668 contained incomplete addrmap. So, it seems better to just ignore such
481860b3 2669 indices. */
831adc1f 2670 if (version < 4)
481860b3
GB
2671 {
2672 static int warning_printed = 0;
2673 if (!warning_printed)
2674 {
2675 warning (_("Skipping obsolete .gdb_index section in %s."),
2ec9a5e0 2676 filename);
481860b3
GB
2677 warning_printed = 1;
2678 }
2679 return 0;
2680 }
2681 /* Index version 4 uses a different hash function than index version
2682 5 and later.
2683
2684 Versions earlier than 6 did not emit psymbols for inlined
2685 functions. Using these files will cause GDB not to be able to
2686 set breakpoints on inlined functions by name, so we ignore these
e615022a
DE
2687 indices unless the user has done
2688 "set use-deprecated-index-sections on". */
2ec9a5e0 2689 if (version < 6 && !deprecated_ok)
481860b3
GB
2690 {
2691 static int warning_printed = 0;
2692 if (!warning_printed)
2693 {
e615022a
DE
2694 warning (_("\
2695Skipping deprecated .gdb_index section in %s.\n\
2696Do \"set use-deprecated-index-sections on\" before the file is read\n\
2697to use the section anyway."),
2ec9a5e0 2698 filename);
481860b3
GB
2699 warning_printed = 1;
2700 }
2701 return 0;
2702 }
796a7ff8
DE
2703 /* Version 7 indices generated by gold refer to the CU for a symbol instead
2704 of the TU (for symbols coming from TUs). It's just a performance bug, and
2705 we can't distinguish gdb-generated indices from gold-generated ones, so
2706 nothing to do here. */
2707
481860b3 2708 /* Indexes with higher version than the one supported by GDB may be no
594e8718 2709 longer backward compatible. */
796a7ff8 2710 if (version > 8)
594e8718 2711 return 0;
9291a0cd 2712
559a7a62 2713 map->version = version;
2ec9a5e0 2714 map->total_size = section->size;
9291a0cd
TT
2715
2716 metadata = (offset_type *) (addr + sizeof (offset_type));
1fd400ff
TT
2717
2718 i = 0;
2ec9a5e0
TT
2719 *cu_list = addr + MAYBE_SWAP (metadata[i]);
2720 *cu_list_elements = ((MAYBE_SWAP (metadata[i + 1]) - MAYBE_SWAP (metadata[i]))
2721 / 8);
1fd400ff
TT
2722 ++i;
2723
2ec9a5e0
TT
2724 *types_list = addr + MAYBE_SWAP (metadata[i]);
2725 *types_list_elements = ((MAYBE_SWAP (metadata[i + 1])
2726 - MAYBE_SWAP (metadata[i]))
2727 / 8);
987d643c 2728 ++i;
1fd400ff
TT
2729
2730 map->address_table = addr + MAYBE_SWAP (metadata[i]);
2731 map->address_table_size = (MAYBE_SWAP (metadata[i + 1])
2732 - MAYBE_SWAP (metadata[i]));
2733 ++i;
2734
3876f04e
DE
2735 map->symbol_table = (offset_type *) (addr + MAYBE_SWAP (metadata[i]));
2736 map->symbol_table_slots = ((MAYBE_SWAP (metadata[i + 1])
2737 - MAYBE_SWAP (metadata[i]))
2738 / (2 * sizeof (offset_type)));
1fd400ff 2739 ++i;
9291a0cd 2740
1fd400ff
TT
2741 map->constant_pool = addr + MAYBE_SWAP (metadata[i]);
2742
2ec9a5e0
TT
2743 return 1;
2744}
2745
2746
2747/* Read the index file. If everything went ok, initialize the "quick"
2748 elements of all the CUs and return 1. Otherwise, return 0. */
2749
2750static int
2751dwarf2_read_index (struct objfile *objfile)
2752{
2753 struct mapped_index local_map, *map;
2754 const gdb_byte *cu_list, *types_list, *dwz_list = NULL;
2755 offset_type cu_list_elements, types_list_elements, dwz_list_elements = 0;
2756
2757 if (!read_index_from_section (objfile, objfile->name,
2758 use_deprecated_index_sections,
2759 &dwarf2_per_objfile->gdb_index, &local_map,
2760 &cu_list, &cu_list_elements,
2761 &types_list, &types_list_elements))
2762 return 0;
2763
0fefef59 2764 /* Don't use the index if it's empty. */
2ec9a5e0 2765 if (local_map.symbol_table_slots == 0)
0fefef59
DE
2766 return 0;
2767
2ec9a5e0
TT
2768 /* If there is a .dwz file, read it so we can get its CU list as
2769 well. */
2770 if (bfd_get_section_by_name (objfile->obfd, ".gnu_debugaltlink") != NULL)
2771 {
2772 struct dwz_file *dwz = dwarf2_get_dwz_file ();
2773 struct mapped_index dwz_map;
2774 const gdb_byte *dwz_types_ignore;
2775 offset_type dwz_types_elements_ignore;
2776
2777 if (!read_index_from_section (objfile, bfd_get_filename (dwz->dwz_bfd),
2778 1,
2779 &dwz->gdb_index, &dwz_map,
2780 &dwz_list, &dwz_list_elements,
2781 &dwz_types_ignore,
2782 &dwz_types_elements_ignore))
2783 {
2784 warning (_("could not read '.gdb_index' section from %s; skipping"),
2785 bfd_get_filename (dwz->dwz_bfd));
2786 return 0;
2787 }
2788 }
2789
74a0d9f6
JK
2790 create_cus_from_index (objfile, cu_list, cu_list_elements, dwz_list,
2791 dwz_list_elements);
1fd400ff 2792
8b70b953
TT
2793 if (types_list_elements)
2794 {
2795 struct dwarf2_section_info *section;
2796
2797 /* We can only handle a single .debug_types when we have an
2798 index. */
2799 if (VEC_length (dwarf2_section_info_def, dwarf2_per_objfile->types) != 1)
2800 return 0;
2801
2802 section = VEC_index (dwarf2_section_info_def,
2803 dwarf2_per_objfile->types, 0);
2804
74a0d9f6
JK
2805 create_signatured_type_table_from_index (objfile, section, types_list,
2806 types_list_elements);
8b70b953 2807 }
9291a0cd 2808
2ec9a5e0
TT
2809 create_addrmap_from_index (objfile, &local_map);
2810
2811 map = obstack_alloc (&objfile->objfile_obstack, sizeof (struct mapped_index));
2812 *map = local_map;
9291a0cd
TT
2813
2814 dwarf2_per_objfile->index_table = map;
2815 dwarf2_per_objfile->using_index = 1;
7b9f3c50
DE
2816 dwarf2_per_objfile->quick_file_names_table =
2817 create_quick_file_names_table (dwarf2_per_objfile->n_comp_units);
9291a0cd
TT
2818
2819 return 1;
2820}
2821
2822/* A helper for the "quick" functions which sets the global
2823 dwarf2_per_objfile according to OBJFILE. */
2fdf6df6 2824
9291a0cd
TT
2825static void
2826dw2_setup (struct objfile *objfile)
2827{
2828 dwarf2_per_objfile = objfile_data (objfile, dwarf2_objfile_data_key);
2829 gdb_assert (dwarf2_per_objfile);
2830}
2831
dee91e82 2832/* die_reader_func for dw2_get_file_names. */
2fdf6df6 2833
dee91e82
DE
2834static void
2835dw2_get_file_names_reader (const struct die_reader_specs *reader,
2836 gdb_byte *info_ptr,
2837 struct die_info *comp_unit_die,
2838 int has_children,
2839 void *data)
9291a0cd 2840{
dee91e82
DE
2841 struct dwarf2_cu *cu = reader->cu;
2842 struct dwarf2_per_cu_data *this_cu = cu->per_cu;
2843 struct objfile *objfile = dwarf2_per_objfile->objfile;
094b34ac 2844 struct dwarf2_per_cu_data *lh_cu;
7b9f3c50 2845 struct line_header *lh;
9291a0cd 2846 struct attribute *attr;
dee91e82 2847 int i;
15d034d0 2848 const char *name, *comp_dir;
7b9f3c50
DE
2849 void **slot;
2850 struct quick_file_names *qfn;
2851 unsigned int line_offset;
9291a0cd 2852
07261596
TT
2853 /* Our callers never want to match partial units -- instead they
2854 will match the enclosing full CU. */
2855 if (comp_unit_die->tag == DW_TAG_partial_unit)
2856 {
2857 this_cu->v.quick->no_file_data = 1;
2858 return;
2859 }
2860
094b34ac
DE
2861 /* If we're reading the line header for TUs, store it in the "per_cu"
2862 for tu_group. */
2863 if (this_cu->is_debug_types)
2864 {
2865 struct type_unit_group *tu_group = data;
2866
2867 gdb_assert (tu_group != NULL);
2868 lh_cu = &tu_group->per_cu;
2869 }
2870 else
2871 lh_cu = this_cu;
2872
7b9f3c50
DE
2873 lh = NULL;
2874 slot = NULL;
2875 line_offset = 0;
dee91e82
DE
2876
2877 attr = dwarf2_attr (comp_unit_die, DW_AT_stmt_list, cu);
9291a0cd
TT
2878 if (attr)
2879 {
7b9f3c50
DE
2880 struct quick_file_names find_entry;
2881
2882 line_offset = DW_UNSND (attr);
2883
2884 /* We may have already read in this line header (TU line header sharing).
2885 If we have we're done. */
094b34ac
DE
2886 find_entry.hash.dwo_unit = cu->dwo_unit;
2887 find_entry.hash.line_offset.sect_off = line_offset;
7b9f3c50
DE
2888 slot = htab_find_slot (dwarf2_per_objfile->quick_file_names_table,
2889 &find_entry, INSERT);
2890 if (*slot != NULL)
2891 {
094b34ac 2892 lh_cu->v.quick->file_names = *slot;
dee91e82 2893 return;
7b9f3c50
DE
2894 }
2895
3019eac3 2896 lh = dwarf_decode_line_header (line_offset, cu);
9291a0cd
TT
2897 }
2898 if (lh == NULL)
2899 {
094b34ac 2900 lh_cu->v.quick->no_file_data = 1;
dee91e82 2901 return;
9291a0cd
TT
2902 }
2903
7b9f3c50 2904 qfn = obstack_alloc (&objfile->objfile_obstack, sizeof (*qfn));
094b34ac
DE
2905 qfn->hash.dwo_unit = cu->dwo_unit;
2906 qfn->hash.line_offset.sect_off = line_offset;
7b9f3c50
DE
2907 gdb_assert (slot != NULL);
2908 *slot = qfn;
9291a0cd 2909
dee91e82 2910 find_file_and_directory (comp_unit_die, cu, &name, &comp_dir);
9291a0cd 2911
7b9f3c50
DE
2912 qfn->num_file_names = lh->num_file_names;
2913 qfn->file_names = obstack_alloc (&objfile->objfile_obstack,
2914 lh->num_file_names * sizeof (char *));
9291a0cd 2915 for (i = 0; i < lh->num_file_names; ++i)
7b9f3c50
DE
2916 qfn->file_names[i] = file_full_name (i + 1, lh, comp_dir);
2917 qfn->real_names = NULL;
9291a0cd 2918
7b9f3c50 2919 free_line_header (lh);
7b9f3c50 2920
094b34ac 2921 lh_cu->v.quick->file_names = qfn;
dee91e82
DE
2922}
2923
2924/* A helper for the "quick" functions which attempts to read the line
2925 table for THIS_CU. */
2926
2927static struct quick_file_names *
2928dw2_get_file_names (struct objfile *objfile,
2929 struct dwarf2_per_cu_data *this_cu)
2930{
f4dc4d17
DE
2931 /* For TUs this should only be called on the parent group. */
2932 if (this_cu->is_debug_types)
2933 gdb_assert (IS_TYPE_UNIT_GROUP (this_cu));
2934
dee91e82
DE
2935 if (this_cu->v.quick->file_names != NULL)
2936 return this_cu->v.quick->file_names;
2937 /* If we know there is no line data, no point in looking again. */
2938 if (this_cu->v.quick->no_file_data)
2939 return NULL;
2940
3019eac3
DE
2941 /* If DWO files are in use, we can still find the DW_AT_stmt_list attribute
2942 in the stub for CUs, there's is no need to lookup the DWO file.
2943 However, that's not the case for TUs where DW_AT_stmt_list lives in the
2944 DWO file. */
2945 if (this_cu->is_debug_types)
094b34ac 2946 {
796a7ff8 2947 struct type_unit_group *tu_group = this_cu->type_unit_group;
094b34ac
DE
2948
2949 init_cutu_and_read_dies (tu_group->t.first_tu, NULL, 0, 0,
2950 dw2_get_file_names_reader, tu_group);
2951 }
3019eac3
DE
2952 else
2953 init_cutu_and_read_dies_simple (this_cu, dw2_get_file_names_reader, NULL);
dee91e82
DE
2954
2955 if (this_cu->v.quick->no_file_data)
2956 return NULL;
2957 return this_cu->v.quick->file_names;
9291a0cd
TT
2958}
2959
2960/* A helper for the "quick" functions which computes and caches the
7b9f3c50 2961 real path for a given file name from the line table. */
2fdf6df6 2962
9291a0cd 2963static const char *
7b9f3c50
DE
2964dw2_get_real_path (struct objfile *objfile,
2965 struct quick_file_names *qfn, int index)
9291a0cd 2966{
7b9f3c50
DE
2967 if (qfn->real_names == NULL)
2968 qfn->real_names = OBSTACK_CALLOC (&objfile->objfile_obstack,
2969 qfn->num_file_names, sizeof (char *));
9291a0cd 2970
7b9f3c50
DE
2971 if (qfn->real_names[index] == NULL)
2972 qfn->real_names[index] = gdb_realpath (qfn->file_names[index]);
9291a0cd 2973
7b9f3c50 2974 return qfn->real_names[index];
9291a0cd
TT
2975}
2976
2977static struct symtab *
2978dw2_find_last_source_symtab (struct objfile *objfile)
2979{
2980 int index;
ae2de4f8 2981
9291a0cd
TT
2982 dw2_setup (objfile);
2983 index = dwarf2_per_objfile->n_comp_units - 1;
a0f42c21 2984 return dw2_instantiate_symtab (dw2_get_cu (index));
9291a0cd
TT
2985}
2986
7b9f3c50
DE
2987/* Traversal function for dw2_forget_cached_source_info. */
2988
2989static int
2990dw2_free_cached_file_names (void **slot, void *info)
9291a0cd 2991{
7b9f3c50 2992 struct quick_file_names *file_data = (struct quick_file_names *) *slot;
9291a0cd 2993
7b9f3c50 2994 if (file_data->real_names)
9291a0cd 2995 {
7b9f3c50 2996 int i;
9291a0cd 2997
7b9f3c50 2998 for (i = 0; i < file_data->num_file_names; ++i)
9291a0cd 2999 {
7b9f3c50
DE
3000 xfree ((void*) file_data->real_names[i]);
3001 file_data->real_names[i] = NULL;
9291a0cd
TT
3002 }
3003 }
7b9f3c50
DE
3004
3005 return 1;
3006}
3007
3008static void
3009dw2_forget_cached_source_info (struct objfile *objfile)
3010{
3011 dw2_setup (objfile);
3012
3013 htab_traverse_noresize (dwarf2_per_objfile->quick_file_names_table,
3014 dw2_free_cached_file_names, NULL);
9291a0cd
TT
3015}
3016
f8eba3c6
TT
3017/* Helper function for dw2_map_symtabs_matching_filename that expands
3018 the symtabs and calls the iterator. */
3019
3020static int
3021dw2_map_expand_apply (struct objfile *objfile,
3022 struct dwarf2_per_cu_data *per_cu,
f5b95b50 3023 const char *name, const char *real_path,
f8eba3c6
TT
3024 int (*callback) (struct symtab *, void *),
3025 void *data)
3026{
3027 struct symtab *last_made = objfile->symtabs;
3028
3029 /* Don't visit already-expanded CUs. */
3030 if (per_cu->v.quick->symtab)
3031 return 0;
3032
3033 /* This may expand more than one symtab, and we want to iterate over
3034 all of them. */
a0f42c21 3035 dw2_instantiate_symtab (per_cu);
f8eba3c6 3036
f5b95b50 3037 return iterate_over_some_symtabs (name, real_path, callback, data,
f8eba3c6
TT
3038 objfile->symtabs, last_made);
3039}
3040
3041/* Implementation of the map_symtabs_matching_filename method. */
3042
9291a0cd 3043static int
f8eba3c6 3044dw2_map_symtabs_matching_filename (struct objfile *objfile, const char *name,
f5b95b50 3045 const char *real_path,
f8eba3c6
TT
3046 int (*callback) (struct symtab *, void *),
3047 void *data)
9291a0cd
TT
3048{
3049 int i;
c011a4f4 3050 const char *name_basename = lbasename (name);
9291a0cd
TT
3051
3052 dw2_setup (objfile);
ae2de4f8 3053
848e3e78
DE
3054 /* The rule is CUs specify all the files, including those used by
3055 any TU, so there's no need to scan TUs here. */
f4dc4d17 3056
848e3e78 3057 for (i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
9291a0cd
TT
3058 {
3059 int j;
f4dc4d17 3060 struct dwarf2_per_cu_data *per_cu = dw2_get_primary_cu (i);
7b9f3c50 3061 struct quick_file_names *file_data;
9291a0cd 3062
3d7bb9d9 3063 /* We only need to look at symtabs not already expanded. */
e254ef6a 3064 if (per_cu->v.quick->symtab)
9291a0cd
TT
3065 continue;
3066
7b9f3c50
DE
3067 file_data = dw2_get_file_names (objfile, per_cu);
3068 if (file_data == NULL)
9291a0cd
TT
3069 continue;
3070
7b9f3c50 3071 for (j = 0; j < file_data->num_file_names; ++j)
9291a0cd 3072 {
7b9f3c50 3073 const char *this_name = file_data->file_names[j];
9291a0cd 3074
af529f8f 3075 if (compare_filenames_for_search (this_name, name))
9291a0cd 3076 {
f5b95b50 3077 if (dw2_map_expand_apply (objfile, per_cu, name, real_path,
f8eba3c6
TT
3078 callback, data))
3079 return 1;
4aac40c8 3080 }
9291a0cd 3081
c011a4f4
DE
3082 /* Before we invoke realpath, which can get expensive when many
3083 files are involved, do a quick comparison of the basenames. */
3084 if (! basenames_may_differ
3085 && FILENAME_CMP (lbasename (this_name), name_basename) != 0)
3086 continue;
3087
9291a0cd
TT
3088 if (real_path != NULL)
3089 {
7b9f3c50
DE
3090 const char *this_real_name = dw2_get_real_path (objfile,
3091 file_data, j);
9291a0cd 3092
af529f8f
JK
3093 gdb_assert (IS_ABSOLUTE_PATH (real_path));
3094 gdb_assert (IS_ABSOLUTE_PATH (name));
7b9f3c50 3095 if (this_real_name != NULL
af529f8f 3096 && FILENAME_CMP (real_path, this_real_name) == 0)
9291a0cd 3097 {
f5b95b50 3098 if (dw2_map_expand_apply (objfile, per_cu, name, real_path,
f8eba3c6
TT
3099 callback, data))
3100 return 1;
9291a0cd
TT
3101 }
3102 }
3103 }
3104 }
3105
9291a0cd
TT
3106 return 0;
3107}
3108
da51c347
DE
3109/* Struct used to manage iterating over all CUs looking for a symbol. */
3110
3111struct dw2_symtab_iterator
9291a0cd 3112{
da51c347
DE
3113 /* The internalized form of .gdb_index. */
3114 struct mapped_index *index;
3115 /* If non-zero, only look for symbols that match BLOCK_INDEX. */
3116 int want_specific_block;
3117 /* One of GLOBAL_BLOCK or STATIC_BLOCK.
3118 Unused if !WANT_SPECIFIC_BLOCK. */
3119 int block_index;
3120 /* The kind of symbol we're looking for. */
3121 domain_enum domain;
3122 /* The list of CUs from the index entry of the symbol,
3123 or NULL if not found. */
3124 offset_type *vec;
3125 /* The next element in VEC to look at. */
3126 int next;
3127 /* The number of elements in VEC, or zero if there is no match. */
3128 int length;
3129};
9291a0cd 3130
da51c347
DE
3131/* Initialize the index symtab iterator ITER.
3132 If WANT_SPECIFIC_BLOCK is non-zero, only look for symbols
3133 in block BLOCK_INDEX. Otherwise BLOCK_INDEX is ignored. */
2fdf6df6 3134
9291a0cd 3135static void
da51c347
DE
3136dw2_symtab_iter_init (struct dw2_symtab_iterator *iter,
3137 struct mapped_index *index,
3138 int want_specific_block,
3139 int block_index,
3140 domain_enum domain,
3141 const char *name)
3142{
3143 iter->index = index;
3144 iter->want_specific_block = want_specific_block;
3145 iter->block_index = block_index;
3146 iter->domain = domain;
3147 iter->next = 0;
3148
3149 if (find_slot_in_mapped_hash (index, name, &iter->vec))
3150 iter->length = MAYBE_SWAP (*iter->vec);
3151 else
3152 {
3153 iter->vec = NULL;
3154 iter->length = 0;
3155 }
3156}
3157
3158/* Return the next matching CU or NULL if there are no more. */
3159
3160static struct dwarf2_per_cu_data *
3161dw2_symtab_iter_next (struct dw2_symtab_iterator *iter)
3162{
3163 for ( ; iter->next < iter->length; ++iter->next)
3164 {
3165 offset_type cu_index_and_attrs =
3166 MAYBE_SWAP (iter->vec[iter->next + 1]);
3167 offset_type cu_index = GDB_INDEX_CU_VALUE (cu_index_and_attrs);
3168 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (cu_index);
3169 int want_static = iter->block_index != GLOBAL_BLOCK;
3170 /* This value is only valid for index versions >= 7. */
3171 int is_static = GDB_INDEX_SYMBOL_STATIC_VALUE (cu_index_and_attrs);
3172 gdb_index_symbol_kind symbol_kind =
3173 GDB_INDEX_SYMBOL_KIND_VALUE (cu_index_and_attrs);
3174 /* Only check the symbol attributes if they're present.
3175 Indices prior to version 7 don't record them,
3176 and indices >= 7 may elide them for certain symbols
3177 (gold does this). */
3178 int attrs_valid =
3179 (iter->index->version >= 7
3180 && symbol_kind != GDB_INDEX_SYMBOL_KIND_NONE);
3181
3182 /* Skip if already read in. */
3183 if (per_cu->v.quick->symtab)
3184 continue;
3185
3186 if (attrs_valid
3187 && iter->want_specific_block
3188 && want_static != is_static)
3189 continue;
3190
3191 /* Only check the symbol's kind if it has one. */
3192 if (attrs_valid)
3193 {
3194 switch (iter->domain)
3195 {
3196 case VAR_DOMAIN:
3197 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_VARIABLE
3198 && symbol_kind != GDB_INDEX_SYMBOL_KIND_FUNCTION
3199 /* Some types are also in VAR_DOMAIN. */
3200 && symbol_kind != GDB_INDEX_SYMBOL_KIND_TYPE)
3201 continue;
3202 break;
3203 case STRUCT_DOMAIN:
3204 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_TYPE)
3205 continue;
3206 break;
3207 case LABEL_DOMAIN:
3208 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_OTHER)
3209 continue;
3210 break;
3211 default:
3212 break;
3213 }
3214 }
3215
3216 ++iter->next;
3217 return per_cu;
3218 }
3219
3220 return NULL;
3221}
3222
3223static struct symtab *
3224dw2_lookup_symbol (struct objfile *objfile, int block_index,
3225 const char *name, domain_enum domain)
9291a0cd 3226{
da51c347 3227 struct symtab *stab_best = NULL;
156942c7
DE
3228 struct mapped_index *index;
3229
9291a0cd
TT
3230 dw2_setup (objfile);
3231
156942c7
DE
3232 index = dwarf2_per_objfile->index_table;
3233
da51c347 3234 /* index is NULL if OBJF_READNOW. */
156942c7 3235 if (index)
9291a0cd 3236 {
da51c347
DE
3237 struct dw2_symtab_iterator iter;
3238 struct dwarf2_per_cu_data *per_cu;
3239
3240 dw2_symtab_iter_init (&iter, index, 1, block_index, domain, name);
9291a0cd 3241
da51c347 3242 while ((per_cu = dw2_symtab_iter_next (&iter)) != NULL)
9291a0cd 3243 {
da51c347
DE
3244 struct symbol *sym = NULL;
3245 struct symtab *stab = dw2_instantiate_symtab (per_cu);
3246
3247 /* Some caution must be observed with overloaded functions
3248 and methods, since the index will not contain any overload
3249 information (but NAME might contain it). */
3250 if (stab->primary)
9291a0cd 3251 {
da51c347
DE
3252 struct blockvector *bv = BLOCKVECTOR (stab);
3253 struct block *block = BLOCKVECTOR_BLOCK (bv, block_index);
156942c7 3254
da51c347
DE
3255 sym = lookup_block_symbol (block, name, domain);
3256 }
1fd400ff 3257
da51c347
DE
3258 if (sym && strcmp_iw (SYMBOL_SEARCH_NAME (sym), name) == 0)
3259 {
3260 if (!TYPE_IS_OPAQUE (SYMBOL_TYPE (sym)))
3261 return stab;
3262
3263 stab_best = stab;
9291a0cd 3264 }
da51c347
DE
3265
3266 /* Keep looking through other CUs. */
9291a0cd
TT
3267 }
3268 }
9291a0cd 3269
da51c347 3270 return stab_best;
9291a0cd
TT
3271}
3272
3273static void
3274dw2_print_stats (struct objfile *objfile)
3275{
3276 int i, count;
3277
3278 dw2_setup (objfile);
3279 count = 0;
1fd400ff 3280 for (i = 0; i < (dwarf2_per_objfile->n_comp_units
d467dd73 3281 + dwarf2_per_objfile->n_type_units); ++i)
9291a0cd 3282 {
e254ef6a 3283 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
9291a0cd 3284
e254ef6a 3285 if (!per_cu->v.quick->symtab)
9291a0cd
TT
3286 ++count;
3287 }
3288 printf_filtered (_(" Number of unread CUs: %d\n"), count);
3289}
3290
3291static void
3292dw2_dump (struct objfile *objfile)
3293{
3294 /* Nothing worth printing. */
3295}
3296
3297static void
3298dw2_relocate (struct objfile *objfile, struct section_offsets *new_offsets,
3299 struct section_offsets *delta)
3300{
3301 /* There's nothing to relocate here. */
3302}
3303
3304static void
3305dw2_expand_symtabs_for_function (struct objfile *objfile,
3306 const char *func_name)
3307{
da51c347
DE
3308 struct mapped_index *index;
3309
3310 dw2_setup (objfile);
3311
3312 index = dwarf2_per_objfile->index_table;
3313
3314 /* index is NULL if OBJF_READNOW. */
3315 if (index)
3316 {
3317 struct dw2_symtab_iterator iter;
3318 struct dwarf2_per_cu_data *per_cu;
3319
3320 /* Note: It doesn't matter what we pass for block_index here. */
3321 dw2_symtab_iter_init (&iter, index, 0, GLOBAL_BLOCK, VAR_DOMAIN,
3322 func_name);
3323
3324 while ((per_cu = dw2_symtab_iter_next (&iter)) != NULL)
3325 dw2_instantiate_symtab (per_cu);
3326 }
9291a0cd
TT
3327}
3328
3329static void
3330dw2_expand_all_symtabs (struct objfile *objfile)
3331{
3332 int i;
3333
3334 dw2_setup (objfile);
1fd400ff
TT
3335
3336 for (i = 0; i < (dwarf2_per_objfile->n_comp_units
d467dd73 3337 + dwarf2_per_objfile->n_type_units); ++i)
9291a0cd 3338 {
e254ef6a 3339 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
9291a0cd 3340
a0f42c21 3341 dw2_instantiate_symtab (per_cu);
9291a0cd
TT
3342 }
3343}
3344
3345static void
652a8996
JK
3346dw2_expand_symtabs_with_fullname (struct objfile *objfile,
3347 const char *fullname)
9291a0cd
TT
3348{
3349 int i;
3350
3351 dw2_setup (objfile);
d4637a04
DE
3352
3353 /* We don't need to consider type units here.
3354 This is only called for examining code, e.g. expand_line_sal.
3355 There can be an order of magnitude (or more) more type units
3356 than comp units, and we avoid them if we can. */
3357
3358 for (i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
9291a0cd
TT
3359 {
3360 int j;
e254ef6a 3361 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
7b9f3c50 3362 struct quick_file_names *file_data;
9291a0cd 3363
3d7bb9d9 3364 /* We only need to look at symtabs not already expanded. */
e254ef6a 3365 if (per_cu->v.quick->symtab)
9291a0cd
TT
3366 continue;
3367
7b9f3c50
DE
3368 file_data = dw2_get_file_names (objfile, per_cu);
3369 if (file_data == NULL)
9291a0cd
TT
3370 continue;
3371
7b9f3c50 3372 for (j = 0; j < file_data->num_file_names; ++j)
9291a0cd 3373 {
652a8996
JK
3374 const char *this_fullname = file_data->file_names[j];
3375
3376 if (filename_cmp (this_fullname, fullname) == 0)
9291a0cd 3377 {
a0f42c21 3378 dw2_instantiate_symtab (per_cu);
9291a0cd
TT
3379 break;
3380 }
3381 }
3382 }
3383}
3384
356d9f9d
TT
3385/* A helper function for dw2_find_symbol_file that finds the primary
3386 file name for a given CU. This is a die_reader_func. */
3387
3388static void
3389dw2_get_primary_filename_reader (const struct die_reader_specs *reader,
3390 gdb_byte *info_ptr,
3391 struct die_info *comp_unit_die,
3392 int has_children,
3393 void *data)
3394{
3395 const char **result_ptr = data;
3396 struct dwarf2_cu *cu = reader->cu;
3397 struct attribute *attr;
3398
3399 attr = dwarf2_attr (comp_unit_die, DW_AT_name, cu);
3400 if (attr == NULL)
3401 *result_ptr = NULL;
3402 else
3403 *result_ptr = DW_STRING (attr);
3404}
3405
dd786858 3406static const char *
9291a0cd
TT
3407dw2_find_symbol_file (struct objfile *objfile, const char *name)
3408{
e254ef6a 3409 struct dwarf2_per_cu_data *per_cu;
9291a0cd 3410 offset_type *vec;
356d9f9d 3411 const char *filename;
9291a0cd
TT
3412
3413 dw2_setup (objfile);
3414
ae2de4f8 3415 /* index_table is NULL if OBJF_READNOW. */
9291a0cd 3416 if (!dwarf2_per_objfile->index_table)
96408a79
SA
3417 {
3418 struct symtab *s;
3419
d790cf0a
DE
3420 ALL_OBJFILE_PRIMARY_SYMTABS (objfile, s)
3421 {
3422 struct blockvector *bv = BLOCKVECTOR (s);
3423 const struct block *block = BLOCKVECTOR_BLOCK (bv, GLOBAL_BLOCK);
3424 struct symbol *sym = lookup_block_symbol (block, name, VAR_DOMAIN);
3425
3426 if (sym)
652a8996
JK
3427 {
3428 /* Only file extension of returned filename is recognized. */
3429 return SYMBOL_SYMTAB (sym)->filename;
3430 }
d790cf0a 3431 }
96408a79
SA
3432 return NULL;
3433 }
9291a0cd
TT
3434
3435 if (!find_slot_in_mapped_hash (dwarf2_per_objfile->index_table,
3436 name, &vec))
3437 return NULL;
3438
3439 /* Note that this just looks at the very first one named NAME -- but
3440 actually we are looking for a function. find_main_filename
3441 should be rewritten so that it doesn't require a custom hook. It
3442 could just use the ordinary symbol tables. */
3443 /* vec[0] is the length, which must always be >0. */
156942c7 3444 per_cu = dw2_get_cu (GDB_INDEX_CU_VALUE (MAYBE_SWAP (vec[1])));
9291a0cd 3445
356d9f9d 3446 if (per_cu->v.quick->symtab != NULL)
652a8996
JK
3447 {
3448 /* Only file extension of returned filename is recognized. */
3449 return per_cu->v.quick->symtab->filename;
3450 }
356d9f9d 3451
f4dc4d17
DE
3452 init_cutu_and_read_dies (per_cu, NULL, 0, 0,
3453 dw2_get_primary_filename_reader, &filename);
9291a0cd 3454
652a8996 3455 /* Only file extension of returned filename is recognized. */
356d9f9d 3456 return filename;
9291a0cd
TT
3457}
3458
3459static void
40658b94
PH
3460dw2_map_matching_symbols (const char * name, domain_enum namespace,
3461 struct objfile *objfile, int global,
3462 int (*callback) (struct block *,
3463 struct symbol *, void *),
2edb89d3
JK
3464 void *data, symbol_compare_ftype *match,
3465 symbol_compare_ftype *ordered_compare)
9291a0cd 3466{
40658b94 3467 /* Currently unimplemented; used for Ada. The function can be called if the
a9e6a4bb
JK
3468 current language is Ada for a non-Ada objfile using GNU index. As Ada
3469 does not look for non-Ada symbols this function should just return. */
9291a0cd
TT
3470}
3471
3472static void
f8eba3c6
TT
3473dw2_expand_symtabs_matching
3474 (struct objfile *objfile,
3475 int (*file_matcher) (const char *, void *),
e078317b 3476 int (*name_matcher) (const char *, void *),
f8eba3c6
TT
3477 enum search_domain kind,
3478 void *data)
9291a0cd
TT
3479{
3480 int i;
3481 offset_type iter;
4b5246aa 3482 struct mapped_index *index;
9291a0cd
TT
3483
3484 dw2_setup (objfile);
ae2de4f8
DE
3485
3486 /* index_table is NULL if OBJF_READNOW. */
9291a0cd
TT
3487 if (!dwarf2_per_objfile->index_table)
3488 return;
4b5246aa 3489 index = dwarf2_per_objfile->index_table;
9291a0cd 3490
7b08b9eb 3491 if (file_matcher != NULL)
24c79950
TT
3492 {
3493 struct cleanup *cleanup;
3494 htab_t visited_found, visited_not_found;
3495
3496 visited_found = htab_create_alloc (10,
3497 htab_hash_pointer, htab_eq_pointer,
3498 NULL, xcalloc, xfree);
3499 cleanup = make_cleanup_htab_delete (visited_found);
3500 visited_not_found = htab_create_alloc (10,
3501 htab_hash_pointer, htab_eq_pointer,
3502 NULL, xcalloc, xfree);
3503 make_cleanup_htab_delete (visited_not_found);
3504
848e3e78
DE
3505 /* The rule is CUs specify all the files, including those used by
3506 any TU, so there's no need to scan TUs here. */
3507
3508 for (i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
24c79950
TT
3509 {
3510 int j;
f4dc4d17 3511 struct dwarf2_per_cu_data *per_cu = dw2_get_primary_cu (i);
24c79950
TT
3512 struct quick_file_names *file_data;
3513 void **slot;
7b08b9eb 3514
24c79950 3515 per_cu->v.quick->mark = 0;
3d7bb9d9 3516
24c79950
TT
3517 /* We only need to look at symtabs not already expanded. */
3518 if (per_cu->v.quick->symtab)
3519 continue;
7b08b9eb 3520
24c79950
TT
3521 file_data = dw2_get_file_names (objfile, per_cu);
3522 if (file_data == NULL)
3523 continue;
7b08b9eb 3524
24c79950
TT
3525 if (htab_find (visited_not_found, file_data) != NULL)
3526 continue;
3527 else if (htab_find (visited_found, file_data) != NULL)
3528 {
3529 per_cu->v.quick->mark = 1;
3530 continue;
3531 }
3532
3533 for (j = 0; j < file_data->num_file_names; ++j)
3534 {
3535 if (file_matcher (file_data->file_names[j], data))
3536 {
3537 per_cu->v.quick->mark = 1;
3538 break;
3539 }
3540 }
3541
3542 slot = htab_find_slot (per_cu->v.quick->mark
3543 ? visited_found
3544 : visited_not_found,
3545 file_data, INSERT);
3546 *slot = file_data;
3547 }
3548
3549 do_cleanups (cleanup);
3550 }
9291a0cd 3551
3876f04e 3552 for (iter = 0; iter < index->symbol_table_slots; ++iter)
9291a0cd
TT
3553 {
3554 offset_type idx = 2 * iter;
3555 const char *name;
3556 offset_type *vec, vec_len, vec_idx;
3557
3876f04e 3558 if (index->symbol_table[idx] == 0 && index->symbol_table[idx + 1] == 0)
9291a0cd
TT
3559 continue;
3560
3876f04e 3561 name = index->constant_pool + MAYBE_SWAP (index->symbol_table[idx]);
9291a0cd 3562
e078317b 3563 if (! (*name_matcher) (name, data))
9291a0cd
TT
3564 continue;
3565
3566 /* The name was matched, now expand corresponding CUs that were
3567 marked. */
4b5246aa 3568 vec = (offset_type *) (index->constant_pool
3876f04e 3569 + MAYBE_SWAP (index->symbol_table[idx + 1]));
9291a0cd
TT
3570 vec_len = MAYBE_SWAP (vec[0]);
3571 for (vec_idx = 0; vec_idx < vec_len; ++vec_idx)
3572 {
e254ef6a 3573 struct dwarf2_per_cu_data *per_cu;
156942c7
DE
3574 offset_type cu_index_and_attrs = MAYBE_SWAP (vec[vec_idx + 1]);
3575 gdb_index_symbol_kind symbol_kind =
3576 GDB_INDEX_SYMBOL_KIND_VALUE (cu_index_and_attrs);
3577 int cu_index = GDB_INDEX_CU_VALUE (cu_index_and_attrs);
3578
3579 /* Don't crash on bad data. */
3580 if (cu_index >= (dwarf2_per_objfile->n_comp_units
667e0a4b 3581 + dwarf2_per_objfile->n_type_units))
156942c7 3582 continue;
1fd400ff 3583
156942c7
DE
3584 /* Only check the symbol's kind if it has one.
3585 Indices prior to version 7 don't record it. */
3586 if (index->version >= 7)
3587 {
3588 switch (kind)
3589 {
3590 case VARIABLES_DOMAIN:
3591 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_VARIABLE)
3592 continue;
3593 break;
3594 case FUNCTIONS_DOMAIN:
3595 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_FUNCTION)
3596 continue;
3597 break;
3598 case TYPES_DOMAIN:
3599 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_TYPE)
3600 continue;
3601 break;
3602 default:
3603 break;
3604 }
3605 }
3606
3607 per_cu = dw2_get_cu (cu_index);
7b08b9eb 3608 if (file_matcher == NULL || per_cu->v.quick->mark)
a0f42c21 3609 dw2_instantiate_symtab (per_cu);
9291a0cd
TT
3610 }
3611 }
3612}
3613
9703b513
TT
3614/* A helper for dw2_find_pc_sect_symtab which finds the most specific
3615 symtab. */
3616
3617static struct symtab *
3618recursively_find_pc_sect_symtab (struct symtab *symtab, CORE_ADDR pc)
3619{
3620 int i;
3621
3622 if (BLOCKVECTOR (symtab) != NULL
3623 && blockvector_contains_pc (BLOCKVECTOR (symtab), pc))
3624 return symtab;
3625
a3ec0bb1
DE
3626 if (symtab->includes == NULL)
3627 return NULL;
3628
9703b513
TT
3629 for (i = 0; symtab->includes[i]; ++i)
3630 {
a3ec0bb1 3631 struct symtab *s = symtab->includes[i];
9703b513
TT
3632
3633 s = recursively_find_pc_sect_symtab (s, pc);
3634 if (s != NULL)
3635 return s;
3636 }
3637
3638 return NULL;
3639}
3640
9291a0cd
TT
3641static struct symtab *
3642dw2_find_pc_sect_symtab (struct objfile *objfile,
3643 struct minimal_symbol *msymbol,
3644 CORE_ADDR pc,
3645 struct obj_section *section,
3646 int warn_if_readin)
3647{
3648 struct dwarf2_per_cu_data *data;
9703b513 3649 struct symtab *result;
9291a0cd
TT
3650
3651 dw2_setup (objfile);
3652
3653 if (!objfile->psymtabs_addrmap)
3654 return NULL;
3655
3656 data = addrmap_find (objfile->psymtabs_addrmap, pc);
3657 if (!data)
3658 return NULL;
3659
3660 if (warn_if_readin && data->v.quick->symtab)
abebb8b0 3661 warning (_("(Internal error: pc %s in read in CU, but not in symtab.)"),
9291a0cd
TT
3662 paddress (get_objfile_arch (objfile), pc));
3663
9703b513
TT
3664 result = recursively_find_pc_sect_symtab (dw2_instantiate_symtab (data), pc);
3665 gdb_assert (result != NULL);
3666 return result;
9291a0cd
TT
3667}
3668
9291a0cd 3669static void
44b13c5a 3670dw2_map_symbol_filenames (struct objfile *objfile, symbol_filename_ftype *fun,
74e2f255 3671 void *data, int need_fullname)
9291a0cd
TT
3672{
3673 int i;
24c79950
TT
3674 struct cleanup *cleanup;
3675 htab_t visited = htab_create_alloc (10, htab_hash_pointer, htab_eq_pointer,
3676 NULL, xcalloc, xfree);
9291a0cd 3677
24c79950 3678 cleanup = make_cleanup_htab_delete (visited);
9291a0cd 3679 dw2_setup (objfile);
ae2de4f8 3680
848e3e78
DE
3681 /* The rule is CUs specify all the files, including those used by
3682 any TU, so there's no need to scan TUs here.
3683 We can ignore file names coming from already-expanded CUs. */
f4dc4d17 3684
848e3e78 3685 for (i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
24c79950
TT
3686 {
3687 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
3688
3689 if (per_cu->v.quick->symtab)
3690 {
3691 void **slot = htab_find_slot (visited, per_cu->v.quick->file_names,
3692 INSERT);
3693
3694 *slot = per_cu->v.quick->file_names;
3695 }
3696 }
3697
848e3e78 3698 for (i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
9291a0cd
TT
3699 {
3700 int j;
f4dc4d17 3701 struct dwarf2_per_cu_data *per_cu = dw2_get_primary_cu (i);
7b9f3c50 3702 struct quick_file_names *file_data;
24c79950 3703 void **slot;
9291a0cd 3704
3d7bb9d9 3705 /* We only need to look at symtabs not already expanded. */
e254ef6a 3706 if (per_cu->v.quick->symtab)
9291a0cd
TT
3707 continue;
3708
7b9f3c50
DE
3709 file_data = dw2_get_file_names (objfile, per_cu);
3710 if (file_data == NULL)
9291a0cd
TT
3711 continue;
3712
24c79950
TT
3713 slot = htab_find_slot (visited, file_data, INSERT);
3714 if (*slot)
3715 {
3716 /* Already visited. */
3717 continue;
3718 }
3719 *slot = file_data;
3720
7b9f3c50 3721 for (j = 0; j < file_data->num_file_names; ++j)
9291a0cd 3722 {
74e2f255
DE
3723 const char *this_real_name;
3724
3725 if (need_fullname)
3726 this_real_name = dw2_get_real_path (objfile, file_data, j);
3727 else
3728 this_real_name = NULL;
7b9f3c50 3729 (*fun) (file_data->file_names[j], this_real_name, data);
9291a0cd
TT
3730 }
3731 }
24c79950
TT
3732
3733 do_cleanups (cleanup);
9291a0cd
TT
3734}
3735
3736static int
3737dw2_has_symbols (struct objfile *objfile)
3738{
3739 return 1;
3740}
3741
3742const struct quick_symbol_functions dwarf2_gdb_index_functions =
3743{
3744 dw2_has_symbols,
3745 dw2_find_last_source_symtab,
3746 dw2_forget_cached_source_info,
f8eba3c6 3747 dw2_map_symtabs_matching_filename,
9291a0cd 3748 dw2_lookup_symbol,
9291a0cd
TT
3749 dw2_print_stats,
3750 dw2_dump,
3751 dw2_relocate,
3752 dw2_expand_symtabs_for_function,
3753 dw2_expand_all_symtabs,
652a8996 3754 dw2_expand_symtabs_with_fullname,
9291a0cd 3755 dw2_find_symbol_file,
40658b94 3756 dw2_map_matching_symbols,
9291a0cd
TT
3757 dw2_expand_symtabs_matching,
3758 dw2_find_pc_sect_symtab,
9291a0cd
TT
3759 dw2_map_symbol_filenames
3760};
3761
3762/* Initialize for reading DWARF for this objfile. Return 0 if this
3763 file will use psymtabs, or 1 if using the GNU index. */
3764
3765int
3766dwarf2_initialize_objfile (struct objfile *objfile)
3767{
3768 /* If we're about to read full symbols, don't bother with the
3769 indices. In this case we also don't care if some other debug
3770 format is making psymtabs, because they are all about to be
3771 expanded anyway. */
3772 if ((objfile->flags & OBJF_READNOW))
3773 {
3774 int i;
3775
3776 dwarf2_per_objfile->using_index = 1;
3777 create_all_comp_units (objfile);
0e50663e 3778 create_all_type_units (objfile);
7b9f3c50
DE
3779 dwarf2_per_objfile->quick_file_names_table =
3780 create_quick_file_names_table (dwarf2_per_objfile->n_comp_units);
9291a0cd 3781
1fd400ff 3782 for (i = 0; i < (dwarf2_per_objfile->n_comp_units
d467dd73 3783 + dwarf2_per_objfile->n_type_units); ++i)
9291a0cd 3784 {
e254ef6a 3785 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
9291a0cd 3786
e254ef6a
DE
3787 per_cu->v.quick = OBSTACK_ZALLOC (&objfile->objfile_obstack,
3788 struct dwarf2_per_cu_quick_data);
9291a0cd
TT
3789 }
3790
3791 /* Return 1 so that gdb sees the "quick" functions. However,
3792 these functions will be no-ops because we will have expanded
3793 all symtabs. */
3794 return 1;
3795 }
3796
3797 if (dwarf2_read_index (objfile))
3798 return 1;
3799
9291a0cd
TT
3800 return 0;
3801}
3802
3803\f
3804
dce234bc
PP
3805/* Build a partial symbol table. */
3806
3807void
f29dff0a 3808dwarf2_build_psymtabs (struct objfile *objfile)
dce234bc 3809{
c9bf0622
TT
3810 volatile struct gdb_exception except;
3811
f29dff0a 3812 if (objfile->global_psymbols.size == 0 && objfile->static_psymbols.size == 0)
c906108c
SS
3813 {
3814 init_psymbol_list (objfile, 1024);
3815 }
3816
c9bf0622
TT
3817 TRY_CATCH (except, RETURN_MASK_ERROR)
3818 {
3819 /* This isn't really ideal: all the data we allocate on the
3820 objfile's obstack is still uselessly kept around. However,
3821 freeing it seems unsafe. */
3822 struct cleanup *cleanups = make_cleanup_discard_psymtabs (objfile);
3823
3824 dwarf2_build_psymtabs_hard (objfile);
3825 discard_cleanups (cleanups);
3826 }
3827 if (except.reason < 0)
3828 exception_print (gdb_stderr, except);
c906108c 3829}
c906108c 3830
1ce1cefd
DE
3831/* Return the total length of the CU described by HEADER. */
3832
3833static unsigned int
3834get_cu_length (const struct comp_unit_head *header)
3835{
3836 return header->initial_length_size + header->length;
3837}
3838
45452591
DE
3839/* Return TRUE if OFFSET is within CU_HEADER. */
3840
3841static inline int
b64f50a1 3842offset_in_cu_p (const struct comp_unit_head *cu_header, sect_offset offset)
45452591 3843{
b64f50a1 3844 sect_offset bottom = { cu_header->offset.sect_off };
1ce1cefd 3845 sect_offset top = { cu_header->offset.sect_off + get_cu_length (cu_header) };
9a619af0 3846
b64f50a1 3847 return (offset.sect_off >= bottom.sect_off && offset.sect_off < top.sect_off);
45452591
DE
3848}
3849
3b80fe9b
DE
3850/* Find the base address of the compilation unit for range lists and
3851 location lists. It will normally be specified by DW_AT_low_pc.
3852 In DWARF-3 draft 4, the base address could be overridden by
3853 DW_AT_entry_pc. It's been removed, but GCC still uses this for
3854 compilation units with discontinuous ranges. */
3855
3856static void
3857dwarf2_find_base_address (struct die_info *die, struct dwarf2_cu *cu)
3858{
3859 struct attribute *attr;
3860
3861 cu->base_known = 0;
3862 cu->base_address = 0;
3863
3864 attr = dwarf2_attr (die, DW_AT_entry_pc, cu);
3865 if (attr)
3866 {
3867 cu->base_address = DW_ADDR (attr);
3868 cu->base_known = 1;
3869 }
3870 else
3871 {
3872 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
3873 if (attr)
3874 {
3875 cu->base_address = DW_ADDR (attr);
3876 cu->base_known = 1;
3877 }
3878 }
3879}
3880
93311388
DE
3881/* Read in the comp unit header information from the debug_info at info_ptr.
3882 NOTE: This leaves members offset, first_die_offset to be filled in
3883 by the caller. */
107d2387 3884
fe1b8b76 3885static gdb_byte *
107d2387 3886read_comp_unit_head (struct comp_unit_head *cu_header,
fe1b8b76 3887 gdb_byte *info_ptr, bfd *abfd)
107d2387
AC
3888{
3889 int signed_addr;
891d2f0b 3890 unsigned int bytes_read;
c764a876
DE
3891
3892 cu_header->length = read_initial_length (abfd, info_ptr, &bytes_read);
3893 cu_header->initial_length_size = bytes_read;
3894 cu_header->offset_size = (bytes_read == 4) ? 4 : 8;
613e1657 3895 info_ptr += bytes_read;
107d2387
AC
3896 cu_header->version = read_2_bytes (abfd, info_ptr);
3897 info_ptr += 2;
b64f50a1
JK
3898 cu_header->abbrev_offset.sect_off = read_offset (abfd, info_ptr, cu_header,
3899 &bytes_read);
613e1657 3900 info_ptr += bytes_read;
107d2387
AC
3901 cu_header->addr_size = read_1_byte (abfd, info_ptr);
3902 info_ptr += 1;
3903 signed_addr = bfd_get_sign_extend_vma (abfd);
3904 if (signed_addr < 0)
8e65ff28 3905 internal_error (__FILE__, __LINE__,
e2e0b3e5 3906 _("read_comp_unit_head: dwarf from non elf file"));
107d2387 3907 cu_header->signed_addr_p = signed_addr;
c764a876 3908
107d2387
AC
3909 return info_ptr;
3910}
3911
36586728
TT
3912/* Helper function that returns the proper abbrev section for
3913 THIS_CU. */
3914
3915static struct dwarf2_section_info *
3916get_abbrev_section_for_cu (struct dwarf2_per_cu_data *this_cu)
3917{
3918 struct dwarf2_section_info *abbrev;
3919
3920 if (this_cu->is_dwz)
3921 abbrev = &dwarf2_get_dwz_file ()->abbrev;
3922 else
3923 abbrev = &dwarf2_per_objfile->abbrev;
3924
3925 return abbrev;
3926}
3927
9ff913ba
DE
3928/* Subroutine of read_and_check_comp_unit_head and
3929 read_and_check_type_unit_head to simplify them.
3930 Perform various error checking on the header. */
3931
3932static void
3933error_check_comp_unit_head (struct comp_unit_head *header,
4bdcc0c1
DE
3934 struct dwarf2_section_info *section,
3935 struct dwarf2_section_info *abbrev_section)
9ff913ba
DE
3936{
3937 bfd *abfd = section->asection->owner;
3938 const char *filename = bfd_get_filename (abfd);
3939
3940 if (header->version != 2 && header->version != 3 && header->version != 4)
3941 error (_("Dwarf Error: wrong version in compilation unit header "
3942 "(is %d, should be 2, 3, or 4) [in module %s]"), header->version,
3943 filename);
3944
b64f50a1 3945 if (header->abbrev_offset.sect_off
36586728 3946 >= dwarf2_section_size (dwarf2_per_objfile->objfile, abbrev_section))
9ff913ba
DE
3947 error (_("Dwarf Error: bad offset (0x%lx) in compilation unit header "
3948 "(offset 0x%lx + 6) [in module %s]"),
b64f50a1 3949 (long) header->abbrev_offset.sect_off, (long) header->offset.sect_off,
9ff913ba
DE
3950 filename);
3951
3952 /* Cast to unsigned long to use 64-bit arithmetic when possible to
3953 avoid potential 32-bit overflow. */
1ce1cefd 3954 if (((unsigned long) header->offset.sect_off + get_cu_length (header))
9ff913ba
DE
3955 > section->size)
3956 error (_("Dwarf Error: bad length (0x%lx) in compilation unit header "
3957 "(offset 0x%lx + 0) [in module %s]"),
b64f50a1 3958 (long) header->length, (long) header->offset.sect_off,
9ff913ba
DE
3959 filename);
3960}
3961
3962/* Read in a CU/TU header and perform some basic error checking.
3963 The contents of the header are stored in HEADER.
3964 The result is a pointer to the start of the first DIE. */
adabb602 3965
fe1b8b76 3966static gdb_byte *
9ff913ba
DE
3967read_and_check_comp_unit_head (struct comp_unit_head *header,
3968 struct dwarf2_section_info *section,
4bdcc0c1 3969 struct dwarf2_section_info *abbrev_section,
9ff913ba
DE
3970 gdb_byte *info_ptr,
3971 int is_debug_types_section)
72bf9492 3972{
fe1b8b76 3973 gdb_byte *beg_of_comp_unit = info_ptr;
9ff913ba 3974 bfd *abfd = section->asection->owner;
72bf9492 3975
b64f50a1 3976 header->offset.sect_off = beg_of_comp_unit - section->buffer;
adabb602 3977
72bf9492
DJ
3978 info_ptr = read_comp_unit_head (header, info_ptr, abfd);
3979
460c1c54
CC
3980 /* If we're reading a type unit, skip over the signature and
3981 type_offset fields. */
b0df02fd 3982 if (is_debug_types_section)
460c1c54
CC
3983 info_ptr += 8 /*signature*/ + header->offset_size;
3984
b64f50a1 3985 header->first_die_offset.cu_off = info_ptr - beg_of_comp_unit;
adabb602 3986
4bdcc0c1 3987 error_check_comp_unit_head (header, section, abbrev_section);
72bf9492
DJ
3988
3989 return info_ptr;
3990}
3991
348e048f
DE
3992/* Read in the types comp unit header information from .debug_types entry at
3993 types_ptr. The result is a pointer to one past the end of the header. */
3994
3995static gdb_byte *
9ff913ba
DE
3996read_and_check_type_unit_head (struct comp_unit_head *header,
3997 struct dwarf2_section_info *section,
4bdcc0c1 3998 struct dwarf2_section_info *abbrev_section,
9ff913ba 3999 gdb_byte *info_ptr,
dee91e82
DE
4000 ULONGEST *signature,
4001 cu_offset *type_offset_in_tu)
348e048f 4002{
9ff913ba
DE
4003 gdb_byte *beg_of_comp_unit = info_ptr;
4004 bfd *abfd = section->asection->owner;
348e048f 4005
b64f50a1 4006 header->offset.sect_off = beg_of_comp_unit - section->buffer;
348e048f 4007
9ff913ba 4008 info_ptr = read_comp_unit_head (header, info_ptr, abfd);
348e048f 4009
9ff913ba
DE
4010 /* If we're reading a type unit, skip over the signature and
4011 type_offset fields. */
4012 if (signature != NULL)
4013 *signature = read_8_bytes (abfd, info_ptr);
4014 info_ptr += 8;
dee91e82
DE
4015 if (type_offset_in_tu != NULL)
4016 type_offset_in_tu->cu_off = read_offset_1 (abfd, info_ptr,
4017 header->offset_size);
9ff913ba
DE
4018 info_ptr += header->offset_size;
4019
b64f50a1 4020 header->first_die_offset.cu_off = info_ptr - beg_of_comp_unit;
348e048f 4021
4bdcc0c1 4022 error_check_comp_unit_head (header, section, abbrev_section);
9ff913ba
DE
4023
4024 return info_ptr;
348e048f
DE
4025}
4026
f4dc4d17
DE
4027/* Fetch the abbreviation table offset from a comp or type unit header. */
4028
4029static sect_offset
4030read_abbrev_offset (struct dwarf2_section_info *section,
4031 sect_offset offset)
4032{
4033 bfd *abfd = section->asection->owner;
4034 gdb_byte *info_ptr;
4035 unsigned int length, initial_length_size, offset_size;
4036 sect_offset abbrev_offset;
4037
4038 dwarf2_read_section (dwarf2_per_objfile->objfile, section);
4039 info_ptr = section->buffer + offset.sect_off;
4040 length = read_initial_length (abfd, info_ptr, &initial_length_size);
4041 offset_size = initial_length_size == 4 ? 4 : 8;
4042 info_ptr += initial_length_size + 2 /*version*/;
4043 abbrev_offset.sect_off = read_offset_1 (abfd, info_ptr, offset_size);
4044 return abbrev_offset;
4045}
4046
aaa75496
JB
4047/* Allocate a new partial symtab for file named NAME and mark this new
4048 partial symtab as being an include of PST. */
4049
4050static void
4051dwarf2_create_include_psymtab (char *name, struct partial_symtab *pst,
4052 struct objfile *objfile)
4053{
4054 struct partial_symtab *subpst = allocate_psymtab (name, objfile);
4055
4056 subpst->section_offsets = pst->section_offsets;
4057 subpst->textlow = 0;
4058 subpst->texthigh = 0;
4059
4060 subpst->dependencies = (struct partial_symtab **)
4061 obstack_alloc (&objfile->objfile_obstack,
4062 sizeof (struct partial_symtab *));
4063 subpst->dependencies[0] = pst;
4064 subpst->number_of_dependencies = 1;
4065
4066 subpst->globals_offset = 0;
4067 subpst->n_global_syms = 0;
4068 subpst->statics_offset = 0;
4069 subpst->n_static_syms = 0;
4070 subpst->symtab = NULL;
4071 subpst->read_symtab = pst->read_symtab;
4072 subpst->readin = 0;
4073
4074 /* No private part is necessary for include psymtabs. This property
4075 can be used to differentiate between such include psymtabs and
10b3939b 4076 the regular ones. */
58a9656e 4077 subpst->read_symtab_private = NULL;
aaa75496
JB
4078}
4079
4080/* Read the Line Number Program data and extract the list of files
4081 included by the source file represented by PST. Build an include
d85a05f0 4082 partial symtab for each of these included files. */
aaa75496
JB
4083
4084static void
4085dwarf2_build_include_psymtabs (struct dwarf2_cu *cu,
dee91e82
DE
4086 struct die_info *die,
4087 struct partial_symtab *pst)
aaa75496 4088{
d85a05f0
DJ
4089 struct line_header *lh = NULL;
4090 struct attribute *attr;
aaa75496 4091
d85a05f0
DJ
4092 attr = dwarf2_attr (die, DW_AT_stmt_list, cu);
4093 if (attr)
3019eac3 4094 lh = dwarf_decode_line_header (DW_UNSND (attr), cu);
aaa75496
JB
4095 if (lh == NULL)
4096 return; /* No linetable, so no includes. */
4097
c6da4cef 4098 /* NOTE: pst->dirname is DW_AT_comp_dir (if present). */
f3f5162e 4099 dwarf_decode_lines (lh, pst->dirname, cu, pst, 1);
aaa75496
JB
4100
4101 free_line_header (lh);
4102}
4103
348e048f 4104static hashval_t
52dc124a 4105hash_signatured_type (const void *item)
348e048f 4106{
52dc124a 4107 const struct signatured_type *sig_type = item;
9a619af0 4108
348e048f 4109 /* This drops the top 32 bits of the signature, but is ok for a hash. */
52dc124a 4110 return sig_type->signature;
348e048f
DE
4111}
4112
4113static int
52dc124a 4114eq_signatured_type (const void *item_lhs, const void *item_rhs)
348e048f
DE
4115{
4116 const struct signatured_type *lhs = item_lhs;
4117 const struct signatured_type *rhs = item_rhs;
9a619af0 4118
348e048f
DE
4119 return lhs->signature == rhs->signature;
4120}
4121
1fd400ff
TT
4122/* Allocate a hash table for signatured types. */
4123
4124static htab_t
673bfd45 4125allocate_signatured_type_table (struct objfile *objfile)
1fd400ff
TT
4126{
4127 return htab_create_alloc_ex (41,
52dc124a
DE
4128 hash_signatured_type,
4129 eq_signatured_type,
1fd400ff
TT
4130 NULL,
4131 &objfile->objfile_obstack,
4132 hashtab_obstack_allocate,
4133 dummy_obstack_deallocate);
4134}
4135
d467dd73 4136/* A helper function to add a signatured type CU to a table. */
1fd400ff
TT
4137
4138static int
d467dd73 4139add_signatured_type_cu_to_table (void **slot, void *datum)
1fd400ff
TT
4140{
4141 struct signatured_type *sigt = *slot;
b4dd5633 4142 struct signatured_type ***datap = datum;
1fd400ff 4143
b4dd5633 4144 **datap = sigt;
1fd400ff
TT
4145 ++*datap;
4146
4147 return 1;
4148}
4149
3019eac3 4150/* Create the hash table of all entries in the .debug_types section.
80626a55
DE
4151 DWO_FILE is a pointer to the DWO file for .debug_types.dwo,
4152 NULL otherwise.
4153 Note: This function processes DWO files only, not DWP files.
3019eac3
DE
4154 The result is a pointer to the hash table or NULL if there are
4155 no types. */
348e048f 4156
3019eac3
DE
4157static htab_t
4158create_debug_types_hash_table (struct dwo_file *dwo_file,
4159 VEC (dwarf2_section_info_def) *types)
348e048f 4160{
3019eac3 4161 struct objfile *objfile = dwarf2_per_objfile->objfile;
8b70b953 4162 htab_t types_htab = NULL;
8b70b953
TT
4163 int ix;
4164 struct dwarf2_section_info *section;
4bdcc0c1 4165 struct dwarf2_section_info *abbrev_section;
348e048f 4166
3019eac3
DE
4167 if (VEC_empty (dwarf2_section_info_def, types))
4168 return NULL;
348e048f 4169
4bdcc0c1
DE
4170 abbrev_section = (dwo_file != NULL
4171 ? &dwo_file->sections.abbrev
4172 : &dwarf2_per_objfile->abbrev);
4173
09406207
DE
4174 if (dwarf2_read_debug)
4175 fprintf_unfiltered (gdb_stdlog, "Reading .debug_types%s for %s:\n",
4176 dwo_file ? ".dwo" : "",
4177 bfd_get_filename (abbrev_section->asection->owner));
4178
8b70b953 4179 for (ix = 0;
3019eac3 4180 VEC_iterate (dwarf2_section_info_def, types, ix, section);
8b70b953
TT
4181 ++ix)
4182 {
3019eac3 4183 bfd *abfd;
8b70b953 4184 gdb_byte *info_ptr, *end_ptr;
36586728 4185 struct dwarf2_section_info *abbrev_section;
348e048f 4186
8b70b953
TT
4187 dwarf2_read_section (objfile, section);
4188 info_ptr = section->buffer;
348e048f 4189
8b70b953
TT
4190 if (info_ptr == NULL)
4191 continue;
348e048f 4192
3019eac3
DE
4193 /* We can't set abfd until now because the section may be empty or
4194 not present, in which case section->asection will be NULL. */
4195 abfd = section->asection->owner;
4196
36586728
TT
4197 if (dwo_file)
4198 abbrev_section = &dwo_file->sections.abbrev;
4199 else
4200 abbrev_section = &dwarf2_per_objfile->abbrev;
4201
8b70b953 4202 if (types_htab == NULL)
3019eac3
DE
4203 {
4204 if (dwo_file)
4205 types_htab = allocate_dwo_unit_table (objfile);
4206 else
4207 types_htab = allocate_signatured_type_table (objfile);
4208 }
348e048f 4209
dee91e82
DE
4210 /* We don't use init_cutu_and_read_dies_simple, or some such, here
4211 because we don't need to read any dies: the signature is in the
4212 header. */
8b70b953
TT
4213
4214 end_ptr = info_ptr + section->size;
4215 while (info_ptr < end_ptr)
4216 {
b64f50a1 4217 sect_offset offset;
3019eac3 4218 cu_offset type_offset_in_tu;
8b70b953 4219 ULONGEST signature;
52dc124a 4220 struct signatured_type *sig_type;
3019eac3 4221 struct dwo_unit *dwo_tu;
8b70b953
TT
4222 void **slot;
4223 gdb_byte *ptr = info_ptr;
9ff913ba 4224 struct comp_unit_head header;
dee91e82 4225 unsigned int length;
348e048f 4226
b64f50a1 4227 offset.sect_off = ptr - section->buffer;
348e048f 4228
8b70b953 4229 /* We need to read the type's signature in order to build the hash
9ff913ba 4230 table, but we don't need anything else just yet. */
348e048f 4231
4bdcc0c1
DE
4232 ptr = read_and_check_type_unit_head (&header, section,
4233 abbrev_section, ptr,
3019eac3 4234 &signature, &type_offset_in_tu);
6caca83c 4235
1ce1cefd 4236 length = get_cu_length (&header);
dee91e82 4237
6caca83c 4238 /* Skip dummy type units. */
dee91e82
DE
4239 if (ptr >= info_ptr + length
4240 || peek_abbrev_code (abfd, ptr) == 0)
6caca83c 4241 {
1ce1cefd 4242 info_ptr += length;
6caca83c
CC
4243 continue;
4244 }
8b70b953 4245
3019eac3
DE
4246 if (dwo_file)
4247 {
4248 sig_type = NULL;
4249 dwo_tu = OBSTACK_ZALLOC (&objfile->objfile_obstack,
4250 struct dwo_unit);
4251 dwo_tu->dwo_file = dwo_file;
4252 dwo_tu->signature = signature;
4253 dwo_tu->type_offset_in_tu = type_offset_in_tu;
4254 dwo_tu->info_or_types_section = section;
4255 dwo_tu->offset = offset;
4256 dwo_tu->length = length;
4257 }
4258 else
4259 {
4260 /* N.B.: type_offset is not usable if this type uses a DWO file.
4261 The real type_offset is in the DWO file. */
4262 dwo_tu = NULL;
4263 sig_type = OBSTACK_ZALLOC (&objfile->objfile_obstack,
4264 struct signatured_type);
4265 sig_type->signature = signature;
4266 sig_type->type_offset_in_tu = type_offset_in_tu;
4267 sig_type->per_cu.objfile = objfile;
4268 sig_type->per_cu.is_debug_types = 1;
4269 sig_type->per_cu.info_or_types_section = section;
4270 sig_type->per_cu.offset = offset;
4271 sig_type->per_cu.length = length;
4272 }
8b70b953 4273
3019eac3
DE
4274 slot = htab_find_slot (types_htab,
4275 dwo_file ? (void*) dwo_tu : (void *) sig_type,
4276 INSERT);
8b70b953
TT
4277 gdb_assert (slot != NULL);
4278 if (*slot != NULL)
4279 {
3019eac3
DE
4280 sect_offset dup_offset;
4281
4282 if (dwo_file)
4283 {
4284 const struct dwo_unit *dup_tu = *slot;
4285
4286 dup_offset = dup_tu->offset;
4287 }
4288 else
4289 {
4290 const struct signatured_type *dup_tu = *slot;
4291
4292 dup_offset = dup_tu->per_cu.offset;
4293 }
b3c8eb43 4294
8b70b953
TT
4295 complaint (&symfile_complaints,
4296 _("debug type entry at offset 0x%x is duplicate to the "
4297 "entry at offset 0x%x, signature 0x%s"),
3019eac3 4298 offset.sect_off, dup_offset.sect_off,
8b70b953 4299 phex (signature, sizeof (signature)));
8b70b953 4300 }
3019eac3 4301 *slot = dwo_file ? (void *) dwo_tu : (void *) sig_type;
348e048f 4302
09406207 4303 if (dwarf2_read_debug)
8b70b953 4304 fprintf_unfiltered (gdb_stdlog, " offset 0x%x, signature 0x%s\n",
b64f50a1
JK
4305 offset.sect_off,
4306 phex (signature, sizeof (signature)));
348e048f 4307
dee91e82 4308 info_ptr += length;
8b70b953 4309 }
348e048f
DE
4310 }
4311
3019eac3
DE
4312 return types_htab;
4313}
4314
4315/* Create the hash table of all entries in the .debug_types section,
4316 and initialize all_type_units.
4317 The result is zero if there is an error (e.g. missing .debug_types section),
4318 otherwise non-zero. */
4319
4320static int
4321create_all_type_units (struct objfile *objfile)
4322{
4323 htab_t types_htab;
b4dd5633 4324 struct signatured_type **iter;
3019eac3
DE
4325
4326 types_htab = create_debug_types_hash_table (NULL, dwarf2_per_objfile->types);
4327 if (types_htab == NULL)
4328 {
4329 dwarf2_per_objfile->signatured_types = NULL;
4330 return 0;
4331 }
4332
348e048f
DE
4333 dwarf2_per_objfile->signatured_types = types_htab;
4334
d467dd73
DE
4335 dwarf2_per_objfile->n_type_units = htab_elements (types_htab);
4336 dwarf2_per_objfile->all_type_units
1fd400ff 4337 = obstack_alloc (&objfile->objfile_obstack,
d467dd73 4338 dwarf2_per_objfile->n_type_units
b4dd5633 4339 * sizeof (struct signatured_type *));
d467dd73
DE
4340 iter = &dwarf2_per_objfile->all_type_units[0];
4341 htab_traverse_noresize (types_htab, add_signatured_type_cu_to_table, &iter);
4342 gdb_assert (iter - &dwarf2_per_objfile->all_type_units[0]
4343 == dwarf2_per_objfile->n_type_units);
1fd400ff 4344
348e048f
DE
4345 return 1;
4346}
4347
380bca97 4348/* Lookup a signature based type for DW_FORM_ref_sig8.
e319fa28 4349 Returns NULL if signature SIG is not present in the table. */
348e048f
DE
4350
4351static struct signatured_type *
e319fa28 4352lookup_signatured_type (ULONGEST sig)
348e048f
DE
4353{
4354 struct signatured_type find_entry, *entry;
4355
4356 if (dwarf2_per_objfile->signatured_types == NULL)
4357 {
4358 complaint (&symfile_complaints,
55f1336d 4359 _("missing `.debug_types' section for DW_FORM_ref_sig8 die"));
dcc07052 4360 return NULL;
348e048f
DE
4361 }
4362
4363 find_entry.signature = sig;
4364 entry = htab_find (dwarf2_per_objfile->signatured_types, &find_entry);
4365 return entry;
4366}
42e7ad6c
DE
4367\f
4368/* Low level DIE reading support. */
348e048f 4369
d85a05f0
DJ
4370/* Initialize a die_reader_specs struct from a dwarf2_cu struct. */
4371
4372static void
4373init_cu_die_reader (struct die_reader_specs *reader,
dee91e82 4374 struct dwarf2_cu *cu,
3019eac3
DE
4375 struct dwarf2_section_info *section,
4376 struct dwo_file *dwo_file)
d85a05f0 4377{
fceca515 4378 gdb_assert (section->readin && section->buffer != NULL);
dee91e82 4379 reader->abfd = section->asection->owner;
d85a05f0 4380 reader->cu = cu;
3019eac3 4381 reader->dwo_file = dwo_file;
dee91e82
DE
4382 reader->die_section = section;
4383 reader->buffer = section->buffer;
f664829e 4384 reader->buffer_end = section->buffer + section->size;
d85a05f0
DJ
4385}
4386
fd820528 4387/* Initialize a CU (or TU) and read its DIEs.
3019eac3 4388 If the CU defers to a DWO file, read the DWO file as well.
dee91e82 4389
f4dc4d17
DE
4390 ABBREV_TABLE, if non-NULL, is the abbreviation table to use.
4391 Otherwise the table specified in the comp unit header is read in and used.
4392 This is an optimization for when we already have the abbrev table.
4393
dee91e82
DE
4394 If USE_EXISTING_CU is non-zero, and THIS_CU->cu is non-NULL, then use it.
4395 Otherwise, a new CU is allocated with xmalloc.
4396
4397 If KEEP is non-zero, then if we allocated a dwarf2_cu we add it to
4398 read_in_chain. Otherwise the dwarf2_cu data is freed at the end.
4399
4400 WARNING: If THIS_CU is a "dummy CU" (used as filler by the incremental
fd820528 4401 linker) then DIE_READER_FUNC will not get called. */
aaa75496 4402
70221824 4403static void
fd820528 4404init_cutu_and_read_dies (struct dwarf2_per_cu_data *this_cu,
f4dc4d17 4405 struct abbrev_table *abbrev_table,
fd820528
DE
4406 int use_existing_cu, int keep,
4407 die_reader_func_ftype *die_reader_func,
4408 void *data)
c906108c 4409{
dee91e82 4410 struct objfile *objfile = dwarf2_per_objfile->objfile;
3019eac3
DE
4411 struct dwarf2_section_info *section = this_cu->info_or_types_section;
4412 bfd *abfd = section->asection->owner;
dee91e82
DE
4413 struct dwarf2_cu *cu;
4414 gdb_byte *begin_info_ptr, *info_ptr;
4415 struct die_reader_specs reader;
d85a05f0 4416 struct die_info *comp_unit_die;
dee91e82 4417 int has_children;
d85a05f0 4418 struct attribute *attr;
dee91e82
DE
4419 struct cleanup *cleanups, *free_cu_cleanup = NULL;
4420 struct signatured_type *sig_type = NULL;
4bdcc0c1 4421 struct dwarf2_section_info *abbrev_section;
42e7ad6c
DE
4422 /* Non-zero if CU currently points to a DWO file and we need to
4423 reread it. When this happens we need to reread the skeleton die
4424 before we can reread the DWO file. */
4425 int rereading_dwo_cu = 0;
c906108c 4426
09406207
DE
4427 if (dwarf2_die_debug)
4428 fprintf_unfiltered (gdb_stdlog, "Reading %s unit at offset 0x%x\n",
4429 this_cu->is_debug_types ? "type" : "comp",
4430 this_cu->offset.sect_off);
4431
dee91e82
DE
4432 if (use_existing_cu)
4433 gdb_assert (keep);
23745b47 4434
dee91e82
DE
4435 cleanups = make_cleanup (null_cleanup, NULL);
4436
4437 /* This is cheap if the section is already read in. */
4438 dwarf2_read_section (objfile, section);
4439
4440 begin_info_ptr = info_ptr = section->buffer + this_cu->offset.sect_off;
36586728
TT
4441
4442 abbrev_section = get_abbrev_section_for_cu (this_cu);
dee91e82
DE
4443
4444 if (use_existing_cu && this_cu->cu != NULL)
4445 {
4446 cu = this_cu->cu;
42e7ad6c
DE
4447
4448 /* If this CU is from a DWO file we need to start over, we need to
4449 refetch the attributes from the skeleton CU.
4450 This could be optimized by retrieving those attributes from when we
4451 were here the first time: the previous comp_unit_die was stored in
4452 comp_unit_obstack. But there's no data yet that we need this
4453 optimization. */
4454 if (cu->dwo_unit != NULL)
4455 rereading_dwo_cu = 1;
dee91e82
DE
4456 }
4457 else
4458 {
4459 /* If !use_existing_cu, this_cu->cu must be NULL. */
4460 gdb_assert (this_cu->cu == NULL);
4461
4462 cu = xmalloc (sizeof (*cu));
4463 init_one_comp_unit (cu, this_cu);
4464
4465 /* If an error occurs while loading, release our storage. */
4466 free_cu_cleanup = make_cleanup (free_heap_comp_unit, cu);
42e7ad6c 4467 }
dee91e82 4468
42e7ad6c
DE
4469 if (cu->header.first_die_offset.cu_off != 0 && ! rereading_dwo_cu)
4470 {
4471 /* We already have the header, there's no need to read it in again. */
4472 info_ptr += cu->header.first_die_offset.cu_off;
4473 }
4474 else
4475 {
3019eac3 4476 if (this_cu->is_debug_types)
dee91e82
DE
4477 {
4478 ULONGEST signature;
42e7ad6c 4479 cu_offset type_offset_in_tu;
dee91e82 4480
4bdcc0c1
DE
4481 info_ptr = read_and_check_type_unit_head (&cu->header, section,
4482 abbrev_section, info_ptr,
42e7ad6c
DE
4483 &signature,
4484 &type_offset_in_tu);
dee91e82 4485
42e7ad6c
DE
4486 /* Since per_cu is the first member of struct signatured_type,
4487 we can go from a pointer to one to a pointer to the other. */
4488 sig_type = (struct signatured_type *) this_cu;
4489 gdb_assert (sig_type->signature == signature);
4490 gdb_assert (sig_type->type_offset_in_tu.cu_off
4491 == type_offset_in_tu.cu_off);
dee91e82
DE
4492 gdb_assert (this_cu->offset.sect_off == cu->header.offset.sect_off);
4493
42e7ad6c
DE
4494 /* LENGTH has not been set yet for type units if we're
4495 using .gdb_index. */
1ce1cefd 4496 this_cu->length = get_cu_length (&cu->header);
3019eac3
DE
4497
4498 /* Establish the type offset that can be used to lookup the type. */
4499 sig_type->type_offset_in_section.sect_off =
4500 this_cu->offset.sect_off + sig_type->type_offset_in_tu.cu_off;
dee91e82
DE
4501 }
4502 else
4503 {
4bdcc0c1
DE
4504 info_ptr = read_and_check_comp_unit_head (&cu->header, section,
4505 abbrev_section,
4506 info_ptr, 0);
dee91e82
DE
4507
4508 gdb_assert (this_cu->offset.sect_off == cu->header.offset.sect_off);
1ce1cefd 4509 gdb_assert (this_cu->length == get_cu_length (&cu->header));
dee91e82
DE
4510 }
4511 }
10b3939b 4512
6caca83c 4513 /* Skip dummy compilation units. */
dee91e82 4514 if (info_ptr >= begin_info_ptr + this_cu->length
6caca83c
CC
4515 || peek_abbrev_code (abfd, info_ptr) == 0)
4516 {
dee91e82 4517 do_cleanups (cleanups);
21b2bd31 4518 return;
6caca83c
CC
4519 }
4520
433df2d4
DE
4521 /* If we don't have them yet, read the abbrevs for this compilation unit.
4522 And if we need to read them now, make sure they're freed when we're
42e7ad6c
DE
4523 done. Note that it's important that if the CU had an abbrev table
4524 on entry we don't free it when we're done: Somewhere up the call stack
4525 it may be in use. */
f4dc4d17
DE
4526 if (abbrev_table != NULL)
4527 {
4528 gdb_assert (cu->abbrev_table == NULL);
4529 gdb_assert (cu->header.abbrev_offset.sect_off
4530 == abbrev_table->offset.sect_off);
4531 cu->abbrev_table = abbrev_table;
4532 }
4533 else if (cu->abbrev_table == NULL)
dee91e82 4534 {
4bdcc0c1 4535 dwarf2_read_abbrevs (cu, abbrev_section);
dee91e82
DE
4536 make_cleanup (dwarf2_free_abbrev_table, cu);
4537 }
42e7ad6c
DE
4538 else if (rereading_dwo_cu)
4539 {
4540 dwarf2_free_abbrev_table (cu);
4541 dwarf2_read_abbrevs (cu, abbrev_section);
4542 }
af703f96 4543
dee91e82 4544 /* Read the top level CU/TU die. */
3019eac3 4545 init_cu_die_reader (&reader, cu, section, NULL);
dee91e82 4546 info_ptr = read_full_die (&reader, &comp_unit_die, info_ptr, &has_children);
93311388 4547
3019eac3
DE
4548 /* If we have a DWO stub, process it and then read in the DWO file.
4549 Note that if USE_EXISTING_OK != 0, and THIS_CU->cu already contains
4550 a DWO CU, that this test will fail. */
4551 attr = dwarf2_attr (comp_unit_die, DW_AT_GNU_dwo_name, cu);
4552 if (attr)
4553 {
15d034d0 4554 const char *dwo_name = DW_STRING (attr);
42e7ad6c 4555 const char *comp_dir_string;
3019eac3
DE
4556 struct dwo_unit *dwo_unit;
4557 ULONGEST signature; /* Or dwo_id. */
42e7ad6c 4558 struct attribute *comp_dir, *stmt_list, *low_pc, *high_pc, *ranges;
3019eac3 4559 int i,num_extra_attrs;
4bdcc0c1 4560 struct dwarf2_section_info *dwo_abbrev_section;
3019eac3
DE
4561
4562 if (has_children)
4563 error (_("Dwarf Error: compilation unit with DW_AT_GNU_dwo_name"
4564 " has children (offset 0x%x) [in module %s]"),
4565 this_cu->offset.sect_off, bfd_get_filename (abfd));
4566
4567 /* These attributes aren't processed until later:
4568 DW_AT_stmt_list, DW_AT_low_pc, DW_AT_high_pc, DW_AT_ranges.
4569 However, the attribute is found in the stub which we won't have later.
4570 In order to not impose this complication on the rest of the code,
4571 we read them here and copy them to the DWO CU/TU die. */
3019eac3
DE
4572
4573 /* For TUs in DWO files, the DW_AT_stmt_list attribute lives in the
4574 DWO file. */
42e7ad6c 4575 stmt_list = NULL;
3019eac3
DE
4576 if (! this_cu->is_debug_types)
4577 stmt_list = dwarf2_attr (comp_unit_die, DW_AT_stmt_list, cu);
4578 low_pc = dwarf2_attr (comp_unit_die, DW_AT_low_pc, cu);
4579 high_pc = dwarf2_attr (comp_unit_die, DW_AT_high_pc, cu);
4580 ranges = dwarf2_attr (comp_unit_die, DW_AT_ranges, cu);
42e7ad6c 4581 comp_dir = dwarf2_attr (comp_unit_die, DW_AT_comp_dir, cu);
3019eac3
DE
4582
4583 /* There should be a DW_AT_addr_base attribute here (if needed).
4584 We need the value before we can process DW_FORM_GNU_addr_index. */
4585 cu->addr_base = 0;
3019eac3
DE
4586 attr = dwarf2_attr (comp_unit_die, DW_AT_GNU_addr_base, cu);
4587 if (attr)
2e3cf129
DE
4588 cu->addr_base = DW_UNSND (attr);
4589
4590 /* There should be a DW_AT_ranges_base attribute here (if needed).
4591 We need the value before we can process DW_AT_ranges. */
4592 cu->ranges_base = 0;
4593 attr = dwarf2_attr (comp_unit_die, DW_AT_GNU_ranges_base, cu);
4594 if (attr)
4595 cu->ranges_base = DW_UNSND (attr);
3019eac3
DE
4596
4597 if (this_cu->is_debug_types)
4598 {
4599 gdb_assert (sig_type != NULL);
4600 signature = sig_type->signature;
4601 }
4602 else
4603 {
4604 attr = dwarf2_attr (comp_unit_die, DW_AT_GNU_dwo_id, cu);
4605 if (! attr)
4606 error (_("Dwarf Error: missing dwo_id [in module %s]"),
4607 dwo_name);
4608 signature = DW_UNSND (attr);
4609 }
4610
4611 /* We may need the comp_dir in order to find the DWO file. */
42e7ad6c
DE
4612 comp_dir_string = NULL;
4613 if (comp_dir)
4614 comp_dir_string = DW_STRING (comp_dir);
3019eac3
DE
4615
4616 if (this_cu->is_debug_types)
42e7ad6c 4617 dwo_unit = lookup_dwo_type_unit (sig_type, dwo_name, comp_dir_string);
3019eac3 4618 else
42e7ad6c 4619 dwo_unit = lookup_dwo_comp_unit (this_cu, dwo_name, comp_dir_string,
3019eac3
DE
4620 signature);
4621
4622 if (dwo_unit == NULL)
4623 {
4624 error (_("Dwarf Error: CU at offset 0x%x references unknown DWO"
4625 " with ID %s [in module %s]"),
4626 this_cu->offset.sect_off,
4627 phex (signature, sizeof (signature)),
4628 objfile->name);
4629 }
4630
4631 /* Set up for reading the DWO CU/TU. */
4632 cu->dwo_unit = dwo_unit;
4633 section = dwo_unit->info_or_types_section;
80626a55 4634 dwarf2_read_section (objfile, section);
3019eac3 4635 begin_info_ptr = info_ptr = section->buffer + dwo_unit->offset.sect_off;
4bdcc0c1 4636 dwo_abbrev_section = &dwo_unit->dwo_file->sections.abbrev;
3019eac3
DE
4637 init_cu_die_reader (&reader, cu, section, dwo_unit->dwo_file);
4638
4639 if (this_cu->is_debug_types)
4640 {
4641 ULONGEST signature;
80626a55 4642 cu_offset type_offset_in_tu;
3019eac3 4643
4bdcc0c1
DE
4644 info_ptr = read_and_check_type_unit_head (&cu->header, section,
4645 dwo_abbrev_section,
4646 info_ptr,
80626a55
DE
4647 &signature,
4648 &type_offset_in_tu);
3019eac3
DE
4649 gdb_assert (sig_type->signature == signature);
4650 gdb_assert (dwo_unit->offset.sect_off == cu->header.offset.sect_off);
80626a55
DE
4651 /* For DWOs coming from DWP files, we don't know the CU length
4652 nor the type's offset in the TU until now. */
4653 dwo_unit->length = get_cu_length (&cu->header);
4654 dwo_unit->type_offset_in_tu = type_offset_in_tu;
3019eac3
DE
4655
4656 /* Establish the type offset that can be used to lookup the type.
4657 For DWO files, we don't know it until now. */
4658 sig_type->type_offset_in_section.sect_off =
4659 dwo_unit->offset.sect_off + dwo_unit->type_offset_in_tu.cu_off;
4660 }
4661 else
4662 {
4bdcc0c1
DE
4663 info_ptr = read_and_check_comp_unit_head (&cu->header, section,
4664 dwo_abbrev_section,
4665 info_ptr, 0);
3019eac3 4666 gdb_assert (dwo_unit->offset.sect_off == cu->header.offset.sect_off);
80626a55
DE
4667 /* For DWOs coming from DWP files, we don't know the CU length
4668 until now. */
4669 dwo_unit->length = get_cu_length (&cu->header);
3019eac3
DE
4670 }
4671
4672 /* Discard the original CU's abbrev table, and read the DWO's. */
f4dc4d17
DE
4673 if (abbrev_table == NULL)
4674 {
4675 dwarf2_free_abbrev_table (cu);
4676 dwarf2_read_abbrevs (cu, dwo_abbrev_section);
4677 }
4678 else
4679 {
4680 dwarf2_read_abbrevs (cu, dwo_abbrev_section);
4681 make_cleanup (dwarf2_free_abbrev_table, cu);
4682 }
3019eac3
DE
4683
4684 /* Read in the die, but leave space to copy over the attributes
4685 from the stub. This has the benefit of simplifying the rest of
4686 the code - all the real work is done here. */
4687 num_extra_attrs = ((stmt_list != NULL)
4688 + (low_pc != NULL)
4689 + (high_pc != NULL)
42e7ad6c
DE
4690 + (ranges != NULL)
4691 + (comp_dir != NULL));
3019eac3
DE
4692 info_ptr = read_full_die_1 (&reader, &comp_unit_die, info_ptr,
4693 &has_children, num_extra_attrs);
4694
4695 /* Copy over the attributes from the stub to the DWO die. */
4696 i = comp_unit_die->num_attrs;
4697 if (stmt_list != NULL)
4698 comp_unit_die->attrs[i++] = *stmt_list;
4699 if (low_pc != NULL)
4700 comp_unit_die->attrs[i++] = *low_pc;
4701 if (high_pc != NULL)
4702 comp_unit_die->attrs[i++] = *high_pc;
4703 if (ranges != NULL)
4704 comp_unit_die->attrs[i++] = *ranges;
42e7ad6c
DE
4705 if (comp_dir != NULL)
4706 comp_unit_die->attrs[i++] = *comp_dir;
3019eac3
DE
4707 comp_unit_die->num_attrs += num_extra_attrs;
4708
4709 /* Skip dummy compilation units. */
4710 if (info_ptr >= begin_info_ptr + dwo_unit->length
4711 || peek_abbrev_code (abfd, info_ptr) == 0)
4712 {
4713 do_cleanups (cleanups);
4714 return;
4715 }
4716 }
4717
dee91e82
DE
4718 die_reader_func (&reader, info_ptr, comp_unit_die, has_children, data);
4719
4720 if (free_cu_cleanup != NULL)
348e048f 4721 {
dee91e82
DE
4722 if (keep)
4723 {
4724 /* We've successfully allocated this compilation unit. Let our
4725 caller clean it up when finished with it. */
4726 discard_cleanups (free_cu_cleanup);
4727
4728 /* We can only discard free_cu_cleanup and all subsequent cleanups.
4729 So we have to manually free the abbrev table. */
4730 dwarf2_free_abbrev_table (cu);
4731
4732 /* Link this CU into read_in_chain. */
4733 this_cu->cu->read_in_chain = dwarf2_per_objfile->read_in_chain;
4734 dwarf2_per_objfile->read_in_chain = this_cu;
4735 }
4736 else
4737 do_cleanups (free_cu_cleanup);
348e048f 4738 }
dee91e82
DE
4739
4740 do_cleanups (cleanups);
4741}
4742
3019eac3
DE
4743/* Read CU/TU THIS_CU in section SECTION,
4744 but do not follow DW_AT_GNU_dwo_name if present.
80626a55
DE
4745 DWOP_FILE, if non-NULL, is the DWO/DWP file to read (the caller is assumed
4746 to have already done the lookup to find the DWO/DWP file).
dee91e82
DE
4747
4748 The caller is required to fill in THIS_CU->section, THIS_CU->offset, and
3019eac3 4749 THIS_CU->is_debug_types, but nothing else.
dee91e82
DE
4750
4751 We fill in THIS_CU->length.
4752
4753 WARNING: If THIS_CU is a "dummy CU" (used as filler by the incremental
4754 linker) then DIE_READER_FUNC will not get called.
4755
4756 THIS_CU->cu is always freed when done.
3019eac3
DE
4757 This is done in order to not leave THIS_CU->cu in a state where we have
4758 to care whether it refers to the "main" CU or the DWO CU. */
dee91e82
DE
4759
4760static void
4761init_cutu_and_read_dies_no_follow (struct dwarf2_per_cu_data *this_cu,
4762 struct dwarf2_section_info *abbrev_section,
3019eac3 4763 struct dwo_file *dwo_file,
dee91e82
DE
4764 die_reader_func_ftype *die_reader_func,
4765 void *data)
4766{
4767 struct objfile *objfile = dwarf2_per_objfile->objfile;
3019eac3
DE
4768 struct dwarf2_section_info *section = this_cu->info_or_types_section;
4769 bfd *abfd = section->asection->owner;
dee91e82
DE
4770 struct dwarf2_cu cu;
4771 gdb_byte *begin_info_ptr, *info_ptr;
4772 struct die_reader_specs reader;
4773 struct cleanup *cleanups;
4774 struct die_info *comp_unit_die;
4775 int has_children;
4776
09406207
DE
4777 if (dwarf2_die_debug)
4778 fprintf_unfiltered (gdb_stdlog, "Reading %s unit at offset 0x%x\n",
4779 this_cu->is_debug_types ? "type" : "comp",
4780 this_cu->offset.sect_off);
4781
dee91e82
DE
4782 gdb_assert (this_cu->cu == NULL);
4783
dee91e82
DE
4784 /* This is cheap if the section is already read in. */
4785 dwarf2_read_section (objfile, section);
4786
4787 init_one_comp_unit (&cu, this_cu);
4788
4789 cleanups = make_cleanup (free_stack_comp_unit, &cu);
4790
4791 begin_info_ptr = info_ptr = section->buffer + this_cu->offset.sect_off;
4bdcc0c1
DE
4792 info_ptr = read_and_check_comp_unit_head (&cu.header, section,
4793 abbrev_section, info_ptr,
3019eac3 4794 this_cu->is_debug_types);
dee91e82 4795
1ce1cefd 4796 this_cu->length = get_cu_length (&cu.header);
dee91e82
DE
4797
4798 /* Skip dummy compilation units. */
4799 if (info_ptr >= begin_info_ptr + this_cu->length
4800 || peek_abbrev_code (abfd, info_ptr) == 0)
c906108c 4801 {
dee91e82 4802 do_cleanups (cleanups);
21b2bd31 4803 return;
93311388 4804 }
72bf9492 4805
dee91e82
DE
4806 dwarf2_read_abbrevs (&cu, abbrev_section);
4807 make_cleanup (dwarf2_free_abbrev_table, &cu);
4808
3019eac3 4809 init_cu_die_reader (&reader, &cu, section, dwo_file);
dee91e82
DE
4810 info_ptr = read_full_die (&reader, &comp_unit_die, info_ptr, &has_children);
4811
4812 die_reader_func (&reader, info_ptr, comp_unit_die, has_children, data);
4813
4814 do_cleanups (cleanups);
4815}
4816
3019eac3
DE
4817/* Read a CU/TU, except that this does not look for DW_AT_GNU_dwo_name and
4818 does not lookup the specified DWO file.
4819 This cannot be used to read DWO files.
dee91e82
DE
4820
4821 THIS_CU->cu is always freed when done.
3019eac3
DE
4822 This is done in order to not leave THIS_CU->cu in a state where we have
4823 to care whether it refers to the "main" CU or the DWO CU.
4824 We can revisit this if the data shows there's a performance issue. */
dee91e82
DE
4825
4826static void
4827init_cutu_and_read_dies_simple (struct dwarf2_per_cu_data *this_cu,
4828 die_reader_func_ftype *die_reader_func,
4829 void *data)
4830{
4831 init_cutu_and_read_dies_no_follow (this_cu,
36586728 4832 get_abbrev_section_for_cu (this_cu),
3019eac3 4833 NULL,
dee91e82
DE
4834 die_reader_func, data);
4835}
4836
f4dc4d17
DE
4837/* Create a psymtab named NAME and assign it to PER_CU.
4838
4839 The caller must fill in the following details:
4840 dirname, textlow, texthigh. */
4841
4842static struct partial_symtab *
4843create_partial_symtab (struct dwarf2_per_cu_data *per_cu, const char *name)
4844{
4845 struct objfile *objfile = per_cu->objfile;
4846 struct partial_symtab *pst;
4847
4848 pst = start_psymtab_common (objfile, objfile->section_offsets,
4849 name, 0,
4850 objfile->global_psymbols.next,
4851 objfile->static_psymbols.next);
4852
4853 pst->psymtabs_addrmap_supported = 1;
4854
4855 /* This is the glue that links PST into GDB's symbol API. */
4856 pst->read_symtab_private = per_cu;
257e7a09 4857 pst->read_symtab = dwarf2_read_symtab;
f4dc4d17
DE
4858 per_cu->v.psymtab = pst;
4859
4860 return pst;
4861}
4862
dee91e82
DE
4863/* die_reader_func for process_psymtab_comp_unit. */
4864
4865static void
4866process_psymtab_comp_unit_reader (const struct die_reader_specs *reader,
4867 gdb_byte *info_ptr,
4868 struct die_info *comp_unit_die,
4869 int has_children,
4870 void *data)
4871{
4872 struct dwarf2_cu *cu = reader->cu;
4873 struct objfile *objfile = cu->objfile;
4874 struct dwarf2_per_cu_data *per_cu = cu->per_cu;
dee91e82
DE
4875 struct attribute *attr;
4876 CORE_ADDR baseaddr;
4877 CORE_ADDR best_lowpc = 0, best_highpc = 0;
4878 struct partial_symtab *pst;
4879 int has_pc_info;
4880 const char *filename;
95554aad 4881 int *want_partial_unit_ptr = data;
dee91e82 4882
95554aad
TT
4883 if (comp_unit_die->tag == DW_TAG_partial_unit
4884 && (want_partial_unit_ptr == NULL
4885 || !*want_partial_unit_ptr))
dee91e82
DE
4886 return;
4887
f4dc4d17
DE
4888 gdb_assert (! per_cu->is_debug_types);
4889
95554aad 4890 prepare_one_comp_unit (cu, comp_unit_die, language_minimal);
dee91e82
DE
4891
4892 cu->list_in_scope = &file_symbols;
c906108c 4893
93311388 4894 /* Allocate a new partial symbol table structure. */
dee91e82 4895 attr = dwarf2_attr (comp_unit_die, DW_AT_name, cu);
3e2a0cee
TT
4896 if (attr == NULL || !DW_STRING (attr))
4897 filename = "";
4898 else
4899 filename = DW_STRING (attr);
72bf9492 4900
f4dc4d17
DE
4901 pst = create_partial_symtab (per_cu, filename);
4902
4903 /* This must be done before calling dwarf2_build_include_psymtabs. */
dee91e82 4904 attr = dwarf2_attr (comp_unit_die, DW_AT_comp_dir, cu);
d85a05f0
DJ
4905 if (attr != NULL)
4906 pst->dirname = DW_STRING (attr);
72bf9492 4907
93311388 4908 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
e7c27a73 4909
dee91e82 4910 dwarf2_find_base_address (comp_unit_die, cu);
d85a05f0 4911
93311388
DE
4912 /* Possibly set the default values of LOWPC and HIGHPC from
4913 `DW_AT_ranges'. */
d85a05f0 4914 has_pc_info = dwarf2_get_pc_bounds (comp_unit_die, &best_lowpc,
dee91e82 4915 &best_highpc, cu, pst);
d85a05f0 4916 if (has_pc_info == 1 && best_lowpc < best_highpc)
93311388
DE
4917 /* Store the contiguous range if it is not empty; it can be empty for
4918 CUs with no code. */
4919 addrmap_set_empty (objfile->psymtabs_addrmap,
d85a05f0
DJ
4920 best_lowpc + baseaddr,
4921 best_highpc + baseaddr - 1, pst);
93311388
DE
4922
4923 /* Check if comp unit has_children.
4924 If so, read the rest of the partial symbols from this comp unit.
0963b4bd 4925 If not, there's no more debug_info for this comp unit. */
d85a05f0 4926 if (has_children)
93311388
DE
4927 {
4928 struct partial_die_info *first_die;
4929 CORE_ADDR lowpc, highpc;
31ffec48 4930
93311388
DE
4931 lowpc = ((CORE_ADDR) -1);
4932 highpc = ((CORE_ADDR) 0);
c906108c 4933
dee91e82 4934 first_die = load_partial_dies (reader, info_ptr, 1);
c906108c 4935
93311388 4936 scan_partial_symbols (first_die, &lowpc, &highpc,
dee91e82 4937 ! has_pc_info, cu);
57c22c6c 4938
93311388
DE
4939 /* If we didn't find a lowpc, set it to highpc to avoid
4940 complaints from `maint check'. */
4941 if (lowpc == ((CORE_ADDR) -1))
4942 lowpc = highpc;
10b3939b 4943
93311388
DE
4944 /* If the compilation unit didn't have an explicit address range,
4945 then use the information extracted from its child dies. */
d85a05f0 4946 if (! has_pc_info)
93311388 4947 {
d85a05f0
DJ
4948 best_lowpc = lowpc;
4949 best_highpc = highpc;
93311388
DE
4950 }
4951 }
d85a05f0
DJ
4952 pst->textlow = best_lowpc + baseaddr;
4953 pst->texthigh = best_highpc + baseaddr;
c906108c 4954
93311388
DE
4955 pst->n_global_syms = objfile->global_psymbols.next -
4956 (objfile->global_psymbols.list + pst->globals_offset);
4957 pst->n_static_syms = objfile->static_psymbols.next -
4958 (objfile->static_psymbols.list + pst->statics_offset);
5c80ed9d 4959 sort_pst_symbols (objfile, pst);
c906108c 4960
796a7ff8 4961 if (!VEC_empty (dwarf2_per_cu_ptr, cu->per_cu->imported_symtabs))
95554aad
TT
4962 {
4963 int i;
796a7ff8 4964 int len = VEC_length (dwarf2_per_cu_ptr, cu->per_cu->imported_symtabs);
95554aad
TT
4965 struct dwarf2_per_cu_data *iter;
4966
4967 /* Fill in 'dependencies' here; we fill in 'users' in a
4968 post-pass. */
4969 pst->number_of_dependencies = len;
4970 pst->dependencies = obstack_alloc (&objfile->objfile_obstack,
4971 len * sizeof (struct symtab *));
4972 for (i = 0;
796a7ff8 4973 VEC_iterate (dwarf2_per_cu_ptr, cu->per_cu->imported_symtabs,
95554aad
TT
4974 i, iter);
4975 ++i)
4976 pst->dependencies[i] = iter->v.psymtab;
4977
796a7ff8 4978 VEC_free (dwarf2_per_cu_ptr, cu->per_cu->imported_symtabs);
95554aad
TT
4979 }
4980
f4dc4d17
DE
4981 /* Get the list of files included in the current compilation unit,
4982 and build a psymtab for each of them. */
4983 dwarf2_build_include_psymtabs (cu, comp_unit_die, pst);
09406207
DE
4984
4985 if (dwarf2_read_debug)
4986 {
4987 struct gdbarch *gdbarch = get_objfile_arch (objfile);
4988
4989 fprintf_unfiltered (gdb_stdlog,
844226d6 4990 "Psymtab for %s unit @0x%x: %s - %s"
09406207
DE
4991 ", %d global, %d static syms\n",
4992 per_cu->is_debug_types ? "type" : "comp",
4993 per_cu->offset.sect_off,
4994 paddress (gdbarch, pst->textlow),
4995 paddress (gdbarch, pst->texthigh),
4996 pst->n_global_syms, pst->n_static_syms);
4997 }
dee91e82 4998}
ae038cb0 4999
dee91e82
DE
5000/* Subroutine of dwarf2_build_psymtabs_hard to simplify it.
5001 Process compilation unit THIS_CU for a psymtab. */
5002
5003static void
95554aad
TT
5004process_psymtab_comp_unit (struct dwarf2_per_cu_data *this_cu,
5005 int want_partial_unit)
dee91e82
DE
5006{
5007 /* If this compilation unit was already read in, free the
5008 cached copy in order to read it in again. This is
5009 necessary because we skipped some symbols when we first
5010 read in the compilation unit (see load_partial_dies).
5011 This problem could be avoided, but the benefit is unclear. */
5012 if (this_cu->cu != NULL)
5013 free_one_cached_comp_unit (this_cu);
5014
3019eac3 5015 gdb_assert (! this_cu->is_debug_types);
f4dc4d17
DE
5016 init_cutu_and_read_dies (this_cu, NULL, 0, 0,
5017 process_psymtab_comp_unit_reader,
95554aad 5018 &want_partial_unit);
dee91e82
DE
5019
5020 /* Age out any secondary CUs. */
5021 age_cached_comp_units ();
93311388 5022}
ff013f42 5023
f4dc4d17
DE
5024static hashval_t
5025hash_type_unit_group (const void *item)
5026{
094b34ac 5027 const struct type_unit_group *tu_group = item;
f4dc4d17 5028
094b34ac 5029 return hash_stmt_list_entry (&tu_group->hash);
f4dc4d17 5030}
348e048f
DE
5031
5032static int
f4dc4d17 5033eq_type_unit_group (const void *item_lhs, const void *item_rhs)
348e048f 5034{
f4dc4d17
DE
5035 const struct type_unit_group *lhs = item_lhs;
5036 const struct type_unit_group *rhs = item_rhs;
348e048f 5037
094b34ac 5038 return eq_stmt_list_entry (&lhs->hash, &rhs->hash);
f4dc4d17 5039}
348e048f 5040
f4dc4d17
DE
5041/* Allocate a hash table for type unit groups. */
5042
5043static htab_t
5044allocate_type_unit_groups_table (void)
5045{
5046 return htab_create_alloc_ex (3,
5047 hash_type_unit_group,
5048 eq_type_unit_group,
5049 NULL,
5050 &dwarf2_per_objfile->objfile->objfile_obstack,
5051 hashtab_obstack_allocate,
5052 dummy_obstack_deallocate);
5053}
dee91e82 5054
f4dc4d17
DE
5055/* Type units that don't have DW_AT_stmt_list are grouped into their own
5056 partial symtabs. We combine several TUs per psymtab to not let the size
5057 of any one psymtab grow too big. */
5058#define NO_STMT_LIST_TYPE_UNIT_PSYMTAB (1 << 31)
5059#define NO_STMT_LIST_TYPE_UNIT_PSYMTAB_SIZE 10
dee91e82 5060
094b34ac 5061/* Helper routine for get_type_unit_group.
f4dc4d17
DE
5062 Create the type_unit_group object used to hold one or more TUs. */
5063
5064static struct type_unit_group *
094b34ac 5065create_type_unit_group (struct dwarf2_cu *cu, sect_offset line_offset_struct)
f4dc4d17
DE
5066{
5067 struct objfile *objfile = dwarf2_per_objfile->objfile;
094b34ac 5068 struct dwarf2_per_cu_data *per_cu;
f4dc4d17 5069 struct type_unit_group *tu_group;
f4dc4d17
DE
5070
5071 tu_group = OBSTACK_ZALLOC (&objfile->objfile_obstack,
5072 struct type_unit_group);
094b34ac 5073 per_cu = &tu_group->per_cu;
f4dc4d17
DE
5074 per_cu->objfile = objfile;
5075 per_cu->is_debug_types = 1;
796a7ff8 5076 per_cu->type_unit_group = tu_group;
f4dc4d17 5077
094b34ac
DE
5078 if (dwarf2_per_objfile->using_index)
5079 {
5080 per_cu->v.quick = OBSTACK_ZALLOC (&objfile->objfile_obstack,
5081 struct dwarf2_per_cu_quick_data);
5082 tu_group->t.first_tu = cu->per_cu;
5083 }
5084 else
5085 {
5086 unsigned int line_offset = line_offset_struct.sect_off;
5087 struct partial_symtab *pst;
5088 char *name;
5089
5090 /* Give the symtab a useful name for debug purposes. */
5091 if ((line_offset & NO_STMT_LIST_TYPE_UNIT_PSYMTAB) != 0)
5092 name = xstrprintf ("<type_units_%d>",
5093 (line_offset & ~NO_STMT_LIST_TYPE_UNIT_PSYMTAB));
5094 else
5095 name = xstrprintf ("<type_units_at_0x%x>", line_offset);
5096
5097 pst = create_partial_symtab (per_cu, name);
5098 pst->anonymous = 1;
f4dc4d17 5099
094b34ac
DE
5100 xfree (name);
5101 }
f4dc4d17 5102
094b34ac
DE
5103 tu_group->hash.dwo_unit = cu->dwo_unit;
5104 tu_group->hash.line_offset = line_offset_struct;
f4dc4d17
DE
5105
5106 return tu_group;
5107}
5108
094b34ac
DE
5109/* Look up the type_unit_group for type unit CU, and create it if necessary.
5110 STMT_LIST is a DW_AT_stmt_list attribute. */
f4dc4d17
DE
5111
5112static struct type_unit_group *
094b34ac 5113get_type_unit_group (struct dwarf2_cu *cu, struct attribute *stmt_list)
f4dc4d17
DE
5114{
5115 struct tu_stats *tu_stats = &dwarf2_per_objfile->tu_stats;
5116 struct type_unit_group *tu_group;
5117 void **slot;
5118 unsigned int line_offset;
5119 struct type_unit_group type_unit_group_for_lookup;
5120
5121 if (dwarf2_per_objfile->type_unit_groups == NULL)
5122 {
5123 dwarf2_per_objfile->type_unit_groups =
5124 allocate_type_unit_groups_table ();
5125 }
5126
5127 /* Do we need to create a new group, or can we use an existing one? */
5128
5129 if (stmt_list)
5130 {
5131 line_offset = DW_UNSND (stmt_list);
5132 ++tu_stats->nr_symtab_sharers;
5133 }
5134 else
5135 {
5136 /* Ugh, no stmt_list. Rare, but we have to handle it.
5137 We can do various things here like create one group per TU or
5138 spread them over multiple groups to split up the expansion work.
5139 To avoid worst case scenarios (too many groups or too large groups)
5140 we, umm, group them in bunches. */
5141 line_offset = (NO_STMT_LIST_TYPE_UNIT_PSYMTAB
5142 | (tu_stats->nr_stmt_less_type_units
5143 / NO_STMT_LIST_TYPE_UNIT_PSYMTAB_SIZE));
5144 ++tu_stats->nr_stmt_less_type_units;
5145 }
5146
094b34ac
DE
5147 type_unit_group_for_lookup.hash.dwo_unit = cu->dwo_unit;
5148 type_unit_group_for_lookup.hash.line_offset.sect_off = line_offset;
f4dc4d17
DE
5149 slot = htab_find_slot (dwarf2_per_objfile->type_unit_groups,
5150 &type_unit_group_for_lookup, INSERT);
5151 if (*slot != NULL)
5152 {
5153 tu_group = *slot;
5154 gdb_assert (tu_group != NULL);
5155 }
5156 else
5157 {
5158 sect_offset line_offset_struct;
5159
5160 line_offset_struct.sect_off = line_offset;
094b34ac 5161 tu_group = create_type_unit_group (cu, line_offset_struct);
f4dc4d17
DE
5162 *slot = tu_group;
5163 ++tu_stats->nr_symtabs;
5164 }
5165
5166 return tu_group;
5167}
5168
5169/* Struct used to sort TUs by their abbreviation table offset. */
5170
5171struct tu_abbrev_offset
5172{
5173 struct signatured_type *sig_type;
5174 sect_offset abbrev_offset;
5175};
5176
5177/* Helper routine for build_type_unit_groups, passed to qsort. */
5178
5179static int
5180sort_tu_by_abbrev_offset (const void *ap, const void *bp)
5181{
5182 const struct tu_abbrev_offset * const *a = ap;
5183 const struct tu_abbrev_offset * const *b = bp;
5184 unsigned int aoff = (*a)->abbrev_offset.sect_off;
5185 unsigned int boff = (*b)->abbrev_offset.sect_off;
5186
5187 return (aoff > boff) - (aoff < boff);
5188}
5189
5190/* A helper function to add a type_unit_group to a table. */
5191
5192static int
5193add_type_unit_group_to_table (void **slot, void *datum)
5194{
5195 struct type_unit_group *tu_group = *slot;
5196 struct type_unit_group ***datap = datum;
5197
5198 **datap = tu_group;
5199 ++*datap;
5200
5201 return 1;
5202}
5203
5204/* Efficiently read all the type units, calling init_cutu_and_read_dies on
5205 each one passing FUNC,DATA.
5206
5207 The efficiency is because we sort TUs by the abbrev table they use and
5208 only read each abbrev table once. In one program there are 200K TUs
5209 sharing 8K abbrev tables.
5210
5211 The main purpose of this function is to support building the
5212 dwarf2_per_objfile->type_unit_groups table.
5213 TUs typically share the DW_AT_stmt_list of the CU they came from, so we
5214 can collapse the search space by grouping them by stmt_list.
5215 The savings can be significant, in the same program from above the 200K TUs
5216 share 8K stmt_list tables.
5217
5218 FUNC is expected to call get_type_unit_group, which will create the
5219 struct type_unit_group if necessary and add it to
5220 dwarf2_per_objfile->type_unit_groups. */
5221
5222static void
5223build_type_unit_groups (die_reader_func_ftype *func, void *data)
5224{
5225 struct objfile *objfile = dwarf2_per_objfile->objfile;
5226 struct tu_stats *tu_stats = &dwarf2_per_objfile->tu_stats;
5227 struct cleanup *cleanups;
5228 struct abbrev_table *abbrev_table;
5229 sect_offset abbrev_offset;
5230 struct tu_abbrev_offset *sorted_by_abbrev;
5231 struct type_unit_group **iter;
5232 int i;
5233
5234 /* It's up to the caller to not call us multiple times. */
5235 gdb_assert (dwarf2_per_objfile->type_unit_groups == NULL);
5236
5237 if (dwarf2_per_objfile->n_type_units == 0)
5238 return;
5239
5240 /* TUs typically share abbrev tables, and there can be way more TUs than
5241 abbrev tables. Sort by abbrev table to reduce the number of times we
5242 read each abbrev table in.
5243 Alternatives are to punt or to maintain a cache of abbrev tables.
5244 This is simpler and efficient enough for now.
5245
5246 Later we group TUs by their DW_AT_stmt_list value (as this defines the
5247 symtab to use). Typically TUs with the same abbrev offset have the same
5248 stmt_list value too so in practice this should work well.
5249
5250 The basic algorithm here is:
5251
5252 sort TUs by abbrev table
5253 for each TU with same abbrev table:
5254 read abbrev table if first user
5255 read TU top level DIE
5256 [IWBN if DWO skeletons had DW_AT_stmt_list]
5257 call FUNC */
5258
5259 if (dwarf2_read_debug)
5260 fprintf_unfiltered (gdb_stdlog, "Building type unit groups ...\n");
5261
5262 /* Sort in a separate table to maintain the order of all_type_units
5263 for .gdb_index: TU indices directly index all_type_units. */
5264 sorted_by_abbrev = XNEWVEC (struct tu_abbrev_offset,
5265 dwarf2_per_objfile->n_type_units);
5266 for (i = 0; i < dwarf2_per_objfile->n_type_units; ++i)
5267 {
5268 struct signatured_type *sig_type = dwarf2_per_objfile->all_type_units[i];
5269
5270 sorted_by_abbrev[i].sig_type = sig_type;
5271 sorted_by_abbrev[i].abbrev_offset =
5272 read_abbrev_offset (sig_type->per_cu.info_or_types_section,
5273 sig_type->per_cu.offset);
5274 }
5275 cleanups = make_cleanup (xfree, sorted_by_abbrev);
5276 qsort (sorted_by_abbrev, dwarf2_per_objfile->n_type_units,
5277 sizeof (struct tu_abbrev_offset), sort_tu_by_abbrev_offset);
5278
094b34ac
DE
5279 /* Note: In the .gdb_index case, get_type_unit_group may have already been
5280 called any number of times, so we don't reset tu_stats here. */
5281
f4dc4d17
DE
5282 abbrev_offset.sect_off = ~(unsigned) 0;
5283 abbrev_table = NULL;
5284 make_cleanup (abbrev_table_free_cleanup, &abbrev_table);
5285
5286 for (i = 0; i < dwarf2_per_objfile->n_type_units; ++i)
5287 {
5288 const struct tu_abbrev_offset *tu = &sorted_by_abbrev[i];
5289
5290 /* Switch to the next abbrev table if necessary. */
5291 if (abbrev_table == NULL
5292 || tu->abbrev_offset.sect_off != abbrev_offset.sect_off)
5293 {
5294 if (abbrev_table != NULL)
5295 {
5296 abbrev_table_free (abbrev_table);
5297 /* Reset to NULL in case abbrev_table_read_table throws
5298 an error: abbrev_table_free_cleanup will get called. */
5299 abbrev_table = NULL;
5300 }
5301 abbrev_offset = tu->abbrev_offset;
5302 abbrev_table =
5303 abbrev_table_read_table (&dwarf2_per_objfile->abbrev,
5304 abbrev_offset);
5305 ++tu_stats->nr_uniq_abbrev_tables;
5306 }
5307
5308 init_cutu_and_read_dies (&tu->sig_type->per_cu, abbrev_table, 0, 0,
5309 func, data);
5310 }
5311
5312 /* Create a vector of pointers to primary type units to make it easy to
5313 iterate over them and CUs. See dw2_get_primary_cu. */
5314 dwarf2_per_objfile->n_type_unit_groups =
5315 htab_elements (dwarf2_per_objfile->type_unit_groups);
5316 dwarf2_per_objfile->all_type_unit_groups =
5317 obstack_alloc (&objfile->objfile_obstack,
5318 dwarf2_per_objfile->n_type_unit_groups
5319 * sizeof (struct type_unit_group *));
5320 iter = &dwarf2_per_objfile->all_type_unit_groups[0];
5321 htab_traverse_noresize (dwarf2_per_objfile->type_unit_groups,
5322 add_type_unit_group_to_table, &iter);
5323 gdb_assert (iter - &dwarf2_per_objfile->all_type_unit_groups[0]
5324 == dwarf2_per_objfile->n_type_unit_groups);
5325
5326 do_cleanups (cleanups);
5327
5328 if (dwarf2_read_debug)
5329 {
5330 fprintf_unfiltered (gdb_stdlog, "Done building type unit groups:\n");
5331 fprintf_unfiltered (gdb_stdlog, " %d TUs\n",
5332 dwarf2_per_objfile->n_type_units);
5333 fprintf_unfiltered (gdb_stdlog, " %d uniq abbrev tables\n",
5334 tu_stats->nr_uniq_abbrev_tables);
5335 fprintf_unfiltered (gdb_stdlog, " %d symtabs from stmt_list entries\n",
5336 tu_stats->nr_symtabs);
5337 fprintf_unfiltered (gdb_stdlog, " %d symtab sharers\n",
5338 tu_stats->nr_symtab_sharers);
5339 fprintf_unfiltered (gdb_stdlog, " %d type units without a stmt_list\n",
5340 tu_stats->nr_stmt_less_type_units);
5341 }
5342}
5343
5344/* Reader function for build_type_psymtabs. */
5345
5346static void
5347build_type_psymtabs_reader (const struct die_reader_specs *reader,
5348 gdb_byte *info_ptr,
5349 struct die_info *type_unit_die,
5350 int has_children,
5351 void *data)
5352{
5353 struct objfile *objfile = dwarf2_per_objfile->objfile;
5354 struct dwarf2_cu *cu = reader->cu;
5355 struct dwarf2_per_cu_data *per_cu = cu->per_cu;
5356 struct type_unit_group *tu_group;
5357 struct attribute *attr;
5358 struct partial_die_info *first_die;
5359 CORE_ADDR lowpc, highpc;
5360 struct partial_symtab *pst;
5361
5362 gdb_assert (data == NULL);
5363
5364 if (! has_children)
5365 return;
5366
5367 attr = dwarf2_attr_no_follow (type_unit_die, DW_AT_stmt_list);
094b34ac 5368 tu_group = get_type_unit_group (cu, attr);
f4dc4d17 5369
094b34ac 5370 VEC_safe_push (dwarf2_per_cu_ptr, tu_group->t.tus, per_cu);
f4dc4d17
DE
5371
5372 prepare_one_comp_unit (cu, type_unit_die, language_minimal);
5373 cu->list_in_scope = &file_symbols;
5374 pst = create_partial_symtab (per_cu, "");
5375 pst->anonymous = 1;
5376
5377 first_die = load_partial_dies (reader, info_ptr, 1);
5378
5379 lowpc = (CORE_ADDR) -1;
5380 highpc = (CORE_ADDR) 0;
5381 scan_partial_symbols (first_die, &lowpc, &highpc, 0, cu);
5382
5383 pst->n_global_syms = objfile->global_psymbols.next -
5384 (objfile->global_psymbols.list + pst->globals_offset);
5385 pst->n_static_syms = objfile->static_psymbols.next -
5386 (objfile->static_psymbols.list + pst->statics_offset);
5c80ed9d 5387 sort_pst_symbols (objfile, pst);
f4dc4d17
DE
5388}
5389
5390/* Traversal function for build_type_psymtabs. */
5391
5392static int
5393build_type_psymtab_dependencies (void **slot, void *info)
5394{
5395 struct objfile *objfile = dwarf2_per_objfile->objfile;
5396 struct type_unit_group *tu_group = (struct type_unit_group *) *slot;
094b34ac 5397 struct dwarf2_per_cu_data *per_cu = &tu_group->per_cu;
f4dc4d17 5398 struct partial_symtab *pst = per_cu->v.psymtab;
094b34ac 5399 int len = VEC_length (dwarf2_per_cu_ptr, tu_group->t.tus);
f4dc4d17
DE
5400 struct dwarf2_per_cu_data *iter;
5401 int i;
5402
5403 gdb_assert (len > 0);
5404
5405 pst->number_of_dependencies = len;
5406 pst->dependencies = obstack_alloc (&objfile->objfile_obstack,
5407 len * sizeof (struct psymtab *));
5408 for (i = 0;
094b34ac 5409 VEC_iterate (dwarf2_per_cu_ptr, tu_group->t.tus, i, iter);
f4dc4d17
DE
5410 ++i)
5411 {
5412 pst->dependencies[i] = iter->v.psymtab;
796a7ff8 5413 iter->type_unit_group = tu_group;
f4dc4d17
DE
5414 }
5415
094b34ac 5416 VEC_free (dwarf2_per_cu_ptr, tu_group->t.tus);
348e048f
DE
5417
5418 return 1;
5419}
5420
5421/* Subroutine of dwarf2_build_psymtabs_hard to simplify it.
5422 Build partial symbol tables for the .debug_types comp-units. */
5423
5424static void
5425build_type_psymtabs (struct objfile *objfile)
5426{
0e50663e 5427 if (! create_all_type_units (objfile))
348e048f
DE
5428 return;
5429
f4dc4d17
DE
5430 build_type_unit_groups (build_type_psymtabs_reader, NULL);
5431
5432 /* Now that all TUs have been processed we can fill in the dependencies. */
5433 htab_traverse_noresize (dwarf2_per_objfile->type_unit_groups,
5434 build_type_psymtab_dependencies, NULL);
348e048f
DE
5435}
5436
60606b2c
TT
5437/* A cleanup function that clears objfile's psymtabs_addrmap field. */
5438
5439static void
5440psymtabs_addrmap_cleanup (void *o)
5441{
5442 struct objfile *objfile = o;
ec61707d 5443
60606b2c
TT
5444 objfile->psymtabs_addrmap = NULL;
5445}
5446
95554aad
TT
5447/* Compute the 'user' field for each psymtab in OBJFILE. */
5448
5449static void
5450set_partial_user (struct objfile *objfile)
5451{
5452 int i;
5453
5454 for (i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
5455 {
5456 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
5457 struct partial_symtab *pst = per_cu->v.psymtab;
5458 int j;
5459
36586728
TT
5460 if (pst == NULL)
5461 continue;
5462
95554aad
TT
5463 for (j = 0; j < pst->number_of_dependencies; ++j)
5464 {
5465 /* Set the 'user' field only if it is not already set. */
5466 if (pst->dependencies[j]->user == NULL)
5467 pst->dependencies[j]->user = pst;
5468 }
5469 }
5470}
5471
93311388
DE
5472/* Build the partial symbol table by doing a quick pass through the
5473 .debug_info and .debug_abbrev sections. */
72bf9492 5474
93311388 5475static void
c67a9c90 5476dwarf2_build_psymtabs_hard (struct objfile *objfile)
93311388 5477{
60606b2c
TT
5478 struct cleanup *back_to, *addrmap_cleanup;
5479 struct obstack temp_obstack;
21b2bd31 5480 int i;
93311388 5481
45cfd468
DE
5482 if (dwarf2_read_debug)
5483 {
5484 fprintf_unfiltered (gdb_stdlog, "Building psymtabs of objfile %s ...\n",
5485 objfile->name);
5486 }
5487
98bfdba5
PA
5488 dwarf2_per_objfile->reading_partial_symbols = 1;
5489
be391dca 5490 dwarf2_read_section (objfile, &dwarf2_per_objfile->info);
91c24f0a 5491
93311388
DE
5492 /* Any cached compilation units will be linked by the per-objfile
5493 read_in_chain. Make sure to free them when we're done. */
5494 back_to = make_cleanup (free_cached_comp_units, NULL);
72bf9492 5495
348e048f
DE
5496 build_type_psymtabs (objfile);
5497
93311388 5498 create_all_comp_units (objfile);
c906108c 5499
60606b2c
TT
5500 /* Create a temporary address map on a temporary obstack. We later
5501 copy this to the final obstack. */
5502 obstack_init (&temp_obstack);
5503 make_cleanup_obstack_free (&temp_obstack);
5504 objfile->psymtabs_addrmap = addrmap_create_mutable (&temp_obstack);
5505 addrmap_cleanup = make_cleanup (psymtabs_addrmap_cleanup, objfile);
72bf9492 5506
21b2bd31 5507 for (i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
93311388 5508 {
21b2bd31 5509 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
aaa75496 5510
95554aad 5511 process_psymtab_comp_unit (per_cu, 0);
c906108c 5512 }
ff013f42 5513
95554aad
TT
5514 set_partial_user (objfile);
5515
ff013f42
JK
5516 objfile->psymtabs_addrmap = addrmap_create_fixed (objfile->psymtabs_addrmap,
5517 &objfile->objfile_obstack);
60606b2c 5518 discard_cleanups (addrmap_cleanup);
ff013f42 5519
ae038cb0 5520 do_cleanups (back_to);
45cfd468
DE
5521
5522 if (dwarf2_read_debug)
5523 fprintf_unfiltered (gdb_stdlog, "Done building psymtabs of %s\n",
5524 objfile->name);
ae038cb0
DJ
5525}
5526
3019eac3 5527/* die_reader_func for load_partial_comp_unit. */
ae038cb0
DJ
5528
5529static void
dee91e82
DE
5530load_partial_comp_unit_reader (const struct die_reader_specs *reader,
5531 gdb_byte *info_ptr,
5532 struct die_info *comp_unit_die,
5533 int has_children,
5534 void *data)
ae038cb0 5535{
dee91e82 5536 struct dwarf2_cu *cu = reader->cu;
ae038cb0 5537
95554aad 5538 prepare_one_comp_unit (cu, comp_unit_die, language_minimal);
ae038cb0 5539
ae038cb0
DJ
5540 /* Check if comp unit has_children.
5541 If so, read the rest of the partial symbols from this comp unit.
0963b4bd 5542 If not, there's no more debug_info for this comp unit. */
d85a05f0 5543 if (has_children)
dee91e82
DE
5544 load_partial_dies (reader, info_ptr, 0);
5545}
98bfdba5 5546
dee91e82
DE
5547/* Load the partial DIEs for a secondary CU into memory.
5548 This is also used when rereading a primary CU with load_all_dies. */
c5b7e1cb 5549
dee91e82
DE
5550static void
5551load_partial_comp_unit (struct dwarf2_per_cu_data *this_cu)
5552{
f4dc4d17
DE
5553 init_cutu_and_read_dies (this_cu, NULL, 1, 1,
5554 load_partial_comp_unit_reader, NULL);
ae038cb0
DJ
5555}
5556
ae038cb0 5557static void
36586728
TT
5558read_comp_units_from_section (struct objfile *objfile,
5559 struct dwarf2_section_info *section,
5560 unsigned int is_dwz,
5561 int *n_allocated,
5562 int *n_comp_units,
5563 struct dwarf2_per_cu_data ***all_comp_units)
ae038cb0 5564{
be391dca 5565 gdb_byte *info_ptr;
36586728 5566 bfd *abfd = section->asection->owner;
be391dca 5567
36586728 5568 dwarf2_read_section (objfile, section);
ae038cb0 5569
36586728 5570 info_ptr = section->buffer;
6e70227d 5571
36586728 5572 while (info_ptr < section->buffer + section->size)
ae038cb0 5573 {
c764a876 5574 unsigned int length, initial_length_size;
ae038cb0 5575 struct dwarf2_per_cu_data *this_cu;
b64f50a1 5576 sect_offset offset;
ae038cb0 5577
36586728 5578 offset.sect_off = info_ptr - section->buffer;
ae038cb0
DJ
5579
5580 /* Read just enough information to find out where the next
5581 compilation unit is. */
36586728 5582 length = read_initial_length (abfd, info_ptr, &initial_length_size);
ae038cb0
DJ
5583
5584 /* Save the compilation unit for later lookup. */
5585 this_cu = obstack_alloc (&objfile->objfile_obstack,
5586 sizeof (struct dwarf2_per_cu_data));
5587 memset (this_cu, 0, sizeof (*this_cu));
5588 this_cu->offset = offset;
c764a876 5589 this_cu->length = length + initial_length_size;
36586728 5590 this_cu->is_dwz = is_dwz;
9291a0cd 5591 this_cu->objfile = objfile;
36586728 5592 this_cu->info_or_types_section = section;
ae038cb0 5593
36586728 5594 if (*n_comp_units == *n_allocated)
ae038cb0 5595 {
36586728
TT
5596 *n_allocated *= 2;
5597 *all_comp_units = xrealloc (*all_comp_units,
5598 *n_allocated
5599 * sizeof (struct dwarf2_per_cu_data *));
ae038cb0 5600 }
36586728
TT
5601 (*all_comp_units)[*n_comp_units] = this_cu;
5602 ++*n_comp_units;
ae038cb0
DJ
5603
5604 info_ptr = info_ptr + this_cu->length;
5605 }
36586728
TT
5606}
5607
5608/* Create a list of all compilation units in OBJFILE.
5609 This is only done for -readnow and building partial symtabs. */
5610
5611static void
5612create_all_comp_units (struct objfile *objfile)
5613{
5614 int n_allocated;
5615 int n_comp_units;
5616 struct dwarf2_per_cu_data **all_comp_units;
5617
5618 n_comp_units = 0;
5619 n_allocated = 10;
5620 all_comp_units = xmalloc (n_allocated
5621 * sizeof (struct dwarf2_per_cu_data *));
5622
5623 read_comp_units_from_section (objfile, &dwarf2_per_objfile->info, 0,
5624 &n_allocated, &n_comp_units, &all_comp_units);
5625
5626 if (bfd_get_section_by_name (objfile->obfd, ".gnu_debugaltlink") != NULL)
5627 {
5628 struct dwz_file *dwz = dwarf2_get_dwz_file ();
5629
5630 read_comp_units_from_section (objfile, &dwz->info, 1,
5631 &n_allocated, &n_comp_units,
5632 &all_comp_units);
5633 }
ae038cb0
DJ
5634
5635 dwarf2_per_objfile->all_comp_units
5636 = obstack_alloc (&objfile->objfile_obstack,
5637 n_comp_units * sizeof (struct dwarf2_per_cu_data *));
5638 memcpy (dwarf2_per_objfile->all_comp_units, all_comp_units,
5639 n_comp_units * sizeof (struct dwarf2_per_cu_data *));
5640 xfree (all_comp_units);
5641 dwarf2_per_objfile->n_comp_units = n_comp_units;
c906108c
SS
5642}
5643
5734ee8b
DJ
5644/* Process all loaded DIEs for compilation unit CU, starting at
5645 FIRST_DIE. The caller should pass NEED_PC == 1 if the compilation
5646 unit DIE did not have PC info (DW_AT_low_pc and DW_AT_high_pc, or
5647 DW_AT_ranges). If NEED_PC is set, then this function will set
5648 *LOWPC and *HIGHPC to the lowest and highest PC values found in CU
5649 and record the covered ranges in the addrmap. */
c906108c 5650
72bf9492
DJ
5651static void
5652scan_partial_symbols (struct partial_die_info *first_die, CORE_ADDR *lowpc,
5734ee8b 5653 CORE_ADDR *highpc, int need_pc, struct dwarf2_cu *cu)
c906108c 5654{
72bf9492 5655 struct partial_die_info *pdi;
c906108c 5656
91c24f0a
DC
5657 /* Now, march along the PDI's, descending into ones which have
5658 interesting children but skipping the children of the other ones,
5659 until we reach the end of the compilation unit. */
c906108c 5660
72bf9492 5661 pdi = first_die;
91c24f0a 5662
72bf9492
DJ
5663 while (pdi != NULL)
5664 {
5665 fixup_partial_die (pdi, cu);
c906108c 5666
f55ee35c 5667 /* Anonymous namespaces or modules have no name but have interesting
91c24f0a
DC
5668 children, so we need to look at them. Ditto for anonymous
5669 enums. */
933c6fe4 5670
72bf9492 5671 if (pdi->name != NULL || pdi->tag == DW_TAG_namespace
95554aad
TT
5672 || pdi->tag == DW_TAG_module || pdi->tag == DW_TAG_enumeration_type
5673 || pdi->tag == DW_TAG_imported_unit)
c906108c 5674 {
72bf9492 5675 switch (pdi->tag)
c906108c
SS
5676 {
5677 case DW_TAG_subprogram:
5734ee8b 5678 add_partial_subprogram (pdi, lowpc, highpc, need_pc, cu);
c906108c 5679 break;
72929c62 5680 case DW_TAG_constant:
c906108c
SS
5681 case DW_TAG_variable:
5682 case DW_TAG_typedef:
91c24f0a 5683 case DW_TAG_union_type:
72bf9492 5684 if (!pdi->is_declaration)
63d06c5c 5685 {
72bf9492 5686 add_partial_symbol (pdi, cu);
63d06c5c
DC
5687 }
5688 break;
c906108c 5689 case DW_TAG_class_type:
680b30c7 5690 case DW_TAG_interface_type:
c906108c 5691 case DW_TAG_structure_type:
72bf9492 5692 if (!pdi->is_declaration)
c906108c 5693 {
72bf9492 5694 add_partial_symbol (pdi, cu);
c906108c
SS
5695 }
5696 break;
91c24f0a 5697 case DW_TAG_enumeration_type:
72bf9492
DJ
5698 if (!pdi->is_declaration)
5699 add_partial_enumeration (pdi, cu);
c906108c
SS
5700 break;
5701 case DW_TAG_base_type:
a02abb62 5702 case DW_TAG_subrange_type:
c906108c 5703 /* File scope base type definitions are added to the partial
c5aa993b 5704 symbol table. */
72bf9492 5705 add_partial_symbol (pdi, cu);
c906108c 5706 break;
d9fa45fe 5707 case DW_TAG_namespace:
5734ee8b 5708 add_partial_namespace (pdi, lowpc, highpc, need_pc, cu);
91c24f0a 5709 break;
5d7cb8df
JK
5710 case DW_TAG_module:
5711 add_partial_module (pdi, lowpc, highpc, need_pc, cu);
5712 break;
95554aad
TT
5713 case DW_TAG_imported_unit:
5714 {
5715 struct dwarf2_per_cu_data *per_cu;
5716
f4dc4d17
DE
5717 /* For now we don't handle imported units in type units. */
5718 if (cu->per_cu->is_debug_types)
5719 {
5720 error (_("Dwarf Error: DW_TAG_imported_unit is not"
5721 " supported in type units [in module %s]"),
5722 cu->objfile->name);
5723 }
5724
95554aad 5725 per_cu = dwarf2_find_containing_comp_unit (pdi->d.offset,
36586728 5726 pdi->is_dwz,
95554aad
TT
5727 cu->objfile);
5728
5729 /* Go read the partial unit, if needed. */
5730 if (per_cu->v.psymtab == NULL)
5731 process_psymtab_comp_unit (per_cu, 1);
5732
f4dc4d17 5733 VEC_safe_push (dwarf2_per_cu_ptr,
796a7ff8 5734 cu->per_cu->imported_symtabs, per_cu);
95554aad
TT
5735 }
5736 break;
c906108c
SS
5737 default:
5738 break;
5739 }
5740 }
5741
72bf9492
DJ
5742 /* If the die has a sibling, skip to the sibling. */
5743
5744 pdi = pdi->die_sibling;
5745 }
5746}
5747
5748/* Functions used to compute the fully scoped name of a partial DIE.
91c24f0a 5749
72bf9492 5750 Normally, this is simple. For C++, the parent DIE's fully scoped
987504bb
JJ
5751 name is concatenated with "::" and the partial DIE's name. For
5752 Java, the same thing occurs except that "." is used instead of "::".
72bf9492
DJ
5753 Enumerators are an exception; they use the scope of their parent
5754 enumeration type, i.e. the name of the enumeration type is not
5755 prepended to the enumerator.
91c24f0a 5756
72bf9492
DJ
5757 There are two complexities. One is DW_AT_specification; in this
5758 case "parent" means the parent of the target of the specification,
5759 instead of the direct parent of the DIE. The other is compilers
5760 which do not emit DW_TAG_namespace; in this case we try to guess
5761 the fully qualified name of structure types from their members'
5762 linkage names. This must be done using the DIE's children rather
5763 than the children of any DW_AT_specification target. We only need
5764 to do this for structures at the top level, i.e. if the target of
5765 any DW_AT_specification (if any; otherwise the DIE itself) does not
5766 have a parent. */
5767
5768/* Compute the scope prefix associated with PDI's parent, in
5769 compilation unit CU. The result will be allocated on CU's
5770 comp_unit_obstack, or a copy of the already allocated PDI->NAME
5771 field. NULL is returned if no prefix is necessary. */
15d034d0 5772static const char *
72bf9492
DJ
5773partial_die_parent_scope (struct partial_die_info *pdi,
5774 struct dwarf2_cu *cu)
5775{
15d034d0 5776 const char *grandparent_scope;
72bf9492 5777 struct partial_die_info *parent, *real_pdi;
91c24f0a 5778
72bf9492
DJ
5779 /* We need to look at our parent DIE; if we have a DW_AT_specification,
5780 then this means the parent of the specification DIE. */
5781
5782 real_pdi = pdi;
72bf9492 5783 while (real_pdi->has_specification)
36586728
TT
5784 real_pdi = find_partial_die (real_pdi->spec_offset,
5785 real_pdi->spec_is_dwz, cu);
72bf9492
DJ
5786
5787 parent = real_pdi->die_parent;
5788 if (parent == NULL)
5789 return NULL;
5790
5791 if (parent->scope_set)
5792 return parent->scope;
5793
5794 fixup_partial_die (parent, cu);
5795
10b3939b 5796 grandparent_scope = partial_die_parent_scope (parent, cu);
72bf9492 5797
acebe513
UW
5798 /* GCC 4.0 and 4.1 had a bug (PR c++/28460) where they generated bogus
5799 DW_TAG_namespace DIEs with a name of "::" for the global namespace.
5800 Work around this problem here. */
5801 if (cu->language == language_cplus
6e70227d 5802 && parent->tag == DW_TAG_namespace
acebe513
UW
5803 && strcmp (parent->name, "::") == 0
5804 && grandparent_scope == NULL)
5805 {
5806 parent->scope = NULL;
5807 parent->scope_set = 1;
5808 return NULL;
5809 }
5810
9c6c53f7
SA
5811 if (pdi->tag == DW_TAG_enumerator)
5812 /* Enumerators should not get the name of the enumeration as a prefix. */
5813 parent->scope = grandparent_scope;
5814 else if (parent->tag == DW_TAG_namespace
f55ee35c 5815 || parent->tag == DW_TAG_module
72bf9492
DJ
5816 || parent->tag == DW_TAG_structure_type
5817 || parent->tag == DW_TAG_class_type
680b30c7 5818 || parent->tag == DW_TAG_interface_type
ceeb3d5a
TT
5819 || parent->tag == DW_TAG_union_type
5820 || parent->tag == DW_TAG_enumeration_type)
72bf9492
DJ
5821 {
5822 if (grandparent_scope == NULL)
5823 parent->scope = parent->name;
5824 else
3e43a32a
MS
5825 parent->scope = typename_concat (&cu->comp_unit_obstack,
5826 grandparent_scope,
f55ee35c 5827 parent->name, 0, cu);
72bf9492 5828 }
72bf9492
DJ
5829 else
5830 {
5831 /* FIXME drow/2004-04-01: What should we be doing with
5832 function-local names? For partial symbols, we should probably be
5833 ignoring them. */
5834 complaint (&symfile_complaints,
e2e0b3e5 5835 _("unhandled containing DIE tag %d for DIE at %d"),
b64f50a1 5836 parent->tag, pdi->offset.sect_off);
72bf9492 5837 parent->scope = grandparent_scope;
c906108c
SS
5838 }
5839
72bf9492
DJ
5840 parent->scope_set = 1;
5841 return parent->scope;
5842}
5843
5844/* Return the fully scoped name associated with PDI, from compilation unit
5845 CU. The result will be allocated with malloc. */
4568ecf9 5846
72bf9492
DJ
5847static char *
5848partial_die_full_name (struct partial_die_info *pdi,
5849 struct dwarf2_cu *cu)
5850{
15d034d0 5851 const char *parent_scope;
72bf9492 5852
98bfdba5
PA
5853 /* If this is a template instantiation, we can not work out the
5854 template arguments from partial DIEs. So, unfortunately, we have
5855 to go through the full DIEs. At least any work we do building
5856 types here will be reused if full symbols are loaded later. */
5857 if (pdi->has_template_arguments)
5858 {
5859 fixup_partial_die (pdi, cu);
5860
5861 if (pdi->name != NULL && strchr (pdi->name, '<') == NULL)
5862 {
5863 struct die_info *die;
5864 struct attribute attr;
5865 struct dwarf2_cu *ref_cu = cu;
5866
b64f50a1 5867 /* DW_FORM_ref_addr is using section offset. */
98bfdba5
PA
5868 attr.name = 0;
5869 attr.form = DW_FORM_ref_addr;
4568ecf9 5870 attr.u.unsnd = pdi->offset.sect_off;
98bfdba5
PA
5871 die = follow_die_ref (NULL, &attr, &ref_cu);
5872
5873 return xstrdup (dwarf2_full_name (NULL, die, ref_cu));
5874 }
5875 }
5876
72bf9492
DJ
5877 parent_scope = partial_die_parent_scope (pdi, cu);
5878 if (parent_scope == NULL)
5879 return NULL;
5880 else
f55ee35c 5881 return typename_concat (NULL, parent_scope, pdi->name, 0, cu);
c906108c
SS
5882}
5883
5884static void
72bf9492 5885add_partial_symbol (struct partial_die_info *pdi, struct dwarf2_cu *cu)
c906108c 5886{
e7c27a73 5887 struct objfile *objfile = cu->objfile;
c906108c 5888 CORE_ADDR addr = 0;
15d034d0 5889 const char *actual_name = NULL;
e142c38c 5890 CORE_ADDR baseaddr;
15d034d0 5891 char *built_actual_name;
e142c38c
DJ
5892
5893 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
c906108c 5894
15d034d0
TT
5895 built_actual_name = partial_die_full_name (pdi, cu);
5896 if (built_actual_name != NULL)
5897 actual_name = built_actual_name;
63d06c5c 5898
72bf9492
DJ
5899 if (actual_name == NULL)
5900 actual_name = pdi->name;
5901
c906108c
SS
5902 switch (pdi->tag)
5903 {
5904 case DW_TAG_subprogram:
2cfa0c8d 5905 if (pdi->is_external || cu->language == language_ada)
c906108c 5906 {
2cfa0c8d
JB
5907 /* brobecker/2007-12-26: Normally, only "external" DIEs are part
5908 of the global scope. But in Ada, we want to be able to access
5909 nested procedures globally. So all Ada subprograms are stored
5910 in the global scope. */
f47fb265 5911 /* prim_record_minimal_symbol (actual_name, pdi->lowpc + baseaddr,
c5aa993b 5912 mst_text, objfile); */
f47fb265 5913 add_psymbol_to_list (actual_name, strlen (actual_name),
15d034d0 5914 built_actual_name != NULL,
f47fb265
MS
5915 VAR_DOMAIN, LOC_BLOCK,
5916 &objfile->global_psymbols,
5917 0, pdi->lowpc + baseaddr,
5918 cu->language, objfile);
c906108c
SS
5919 }
5920 else
5921 {
f47fb265 5922 /* prim_record_minimal_symbol (actual_name, pdi->lowpc + baseaddr,
c5aa993b 5923 mst_file_text, objfile); */
f47fb265 5924 add_psymbol_to_list (actual_name, strlen (actual_name),
15d034d0 5925 built_actual_name != NULL,
f47fb265
MS
5926 VAR_DOMAIN, LOC_BLOCK,
5927 &objfile->static_psymbols,
5928 0, pdi->lowpc + baseaddr,
5929 cu->language, objfile);
c906108c
SS
5930 }
5931 break;
72929c62
JB
5932 case DW_TAG_constant:
5933 {
5934 struct psymbol_allocation_list *list;
5935
5936 if (pdi->is_external)
5937 list = &objfile->global_psymbols;
5938 else
5939 list = &objfile->static_psymbols;
f47fb265 5940 add_psymbol_to_list (actual_name, strlen (actual_name),
15d034d0 5941 built_actual_name != NULL, VAR_DOMAIN, LOC_STATIC,
f47fb265 5942 list, 0, 0, cu->language, objfile);
72929c62
JB
5943 }
5944 break;
c906108c 5945 case DW_TAG_variable:
95554aad
TT
5946 if (pdi->d.locdesc)
5947 addr = decode_locdesc (pdi->d.locdesc, cu);
caac4577 5948
95554aad 5949 if (pdi->d.locdesc
caac4577
JG
5950 && addr == 0
5951 && !dwarf2_per_objfile->has_section_at_zero)
5952 {
5953 /* A global or static variable may also have been stripped
5954 out by the linker if unused, in which case its address
5955 will be nullified; do not add such variables into partial
5956 symbol table then. */
5957 }
5958 else if (pdi->is_external)
c906108c
SS
5959 {
5960 /* Global Variable.
5961 Don't enter into the minimal symbol tables as there is
5962 a minimal symbol table entry from the ELF symbols already.
5963 Enter into partial symbol table if it has a location
5964 descriptor or a type.
5965 If the location descriptor is missing, new_symbol will create
5966 a LOC_UNRESOLVED symbol, the address of the variable will then
5967 be determined from the minimal symbol table whenever the variable
5968 is referenced.
5969 The address for the partial symbol table entry is not
5970 used by GDB, but it comes in handy for debugging partial symbol
5971 table building. */
5972
95554aad 5973 if (pdi->d.locdesc || pdi->has_type)
f47fb265 5974 add_psymbol_to_list (actual_name, strlen (actual_name),
15d034d0 5975 built_actual_name != NULL,
f47fb265
MS
5976 VAR_DOMAIN, LOC_STATIC,
5977 &objfile->global_psymbols,
5978 0, addr + baseaddr,
5979 cu->language, objfile);
c906108c
SS
5980 }
5981 else
5982 {
0963b4bd 5983 /* Static Variable. Skip symbols without location descriptors. */
95554aad 5984 if (pdi->d.locdesc == NULL)
decbce07 5985 {
15d034d0 5986 xfree (built_actual_name);
decbce07
MS
5987 return;
5988 }
f47fb265 5989 /* prim_record_minimal_symbol (actual_name, addr + baseaddr,
c5aa993b 5990 mst_file_data, objfile); */
f47fb265 5991 add_psymbol_to_list (actual_name, strlen (actual_name),
15d034d0 5992 built_actual_name != NULL,
f47fb265
MS
5993 VAR_DOMAIN, LOC_STATIC,
5994 &objfile->static_psymbols,
5995 0, addr + baseaddr,
5996 cu->language, objfile);
c906108c
SS
5997 }
5998 break;
5999 case DW_TAG_typedef:
6000 case DW_TAG_base_type:
a02abb62 6001 case DW_TAG_subrange_type:
38d518c9 6002 add_psymbol_to_list (actual_name, strlen (actual_name),
15d034d0 6003 built_actual_name != NULL,
176620f1 6004 VAR_DOMAIN, LOC_TYPEDEF,
c906108c 6005 &objfile->static_psymbols,
e142c38c 6006 0, (CORE_ADDR) 0, cu->language, objfile);
c906108c 6007 break;
72bf9492
DJ
6008 case DW_TAG_namespace:
6009 add_psymbol_to_list (actual_name, strlen (actual_name),
15d034d0 6010 built_actual_name != NULL,
72bf9492
DJ
6011 VAR_DOMAIN, LOC_TYPEDEF,
6012 &objfile->global_psymbols,
6013 0, (CORE_ADDR) 0, cu->language, objfile);
6014 break;
c906108c 6015 case DW_TAG_class_type:
680b30c7 6016 case DW_TAG_interface_type:
c906108c
SS
6017 case DW_TAG_structure_type:
6018 case DW_TAG_union_type:
6019 case DW_TAG_enumeration_type:
fa4028e9
JB
6020 /* Skip external references. The DWARF standard says in the section
6021 about "Structure, Union, and Class Type Entries": "An incomplete
6022 structure, union or class type is represented by a structure,
6023 union or class entry that does not have a byte size attribute
6024 and that has a DW_AT_declaration attribute." */
6025 if (!pdi->has_byte_size && pdi->is_declaration)
decbce07 6026 {
15d034d0 6027 xfree (built_actual_name);
decbce07
MS
6028 return;
6029 }
fa4028e9 6030
63d06c5c
DC
6031 /* NOTE: carlton/2003-10-07: See comment in new_symbol about
6032 static vs. global. */
38d518c9 6033 add_psymbol_to_list (actual_name, strlen (actual_name),
15d034d0 6034 built_actual_name != NULL,
176620f1 6035 STRUCT_DOMAIN, LOC_TYPEDEF,
987504bb
JJ
6036 (cu->language == language_cplus
6037 || cu->language == language_java)
63d06c5c
DC
6038 ? &objfile->global_psymbols
6039 : &objfile->static_psymbols,
e142c38c 6040 0, (CORE_ADDR) 0, cu->language, objfile);
c906108c 6041
c906108c
SS
6042 break;
6043 case DW_TAG_enumerator:
38d518c9 6044 add_psymbol_to_list (actual_name, strlen (actual_name),
15d034d0 6045 built_actual_name != NULL,
176620f1 6046 VAR_DOMAIN, LOC_CONST,
987504bb
JJ
6047 (cu->language == language_cplus
6048 || cu->language == language_java)
f6fe98ef
DJ
6049 ? &objfile->global_psymbols
6050 : &objfile->static_psymbols,
e142c38c 6051 0, (CORE_ADDR) 0, cu->language, objfile);
c906108c
SS
6052 break;
6053 default:
6054 break;
6055 }
5c4e30ca 6056
15d034d0 6057 xfree (built_actual_name);
c906108c
SS
6058}
6059
5c4e30ca
DC
6060/* Read a partial die corresponding to a namespace; also, add a symbol
6061 corresponding to that namespace to the symbol table. NAMESPACE is
6062 the name of the enclosing namespace. */
91c24f0a 6063
72bf9492
DJ
6064static void
6065add_partial_namespace (struct partial_die_info *pdi,
91c24f0a 6066 CORE_ADDR *lowpc, CORE_ADDR *highpc,
5734ee8b 6067 int need_pc, struct dwarf2_cu *cu)
91c24f0a 6068{
72bf9492 6069 /* Add a symbol for the namespace. */
e7c27a73 6070
72bf9492 6071 add_partial_symbol (pdi, cu);
5c4e30ca
DC
6072
6073 /* Now scan partial symbols in that namespace. */
6074
91c24f0a 6075 if (pdi->has_children)
5734ee8b 6076 scan_partial_symbols (pdi->die_child, lowpc, highpc, need_pc, cu);
91c24f0a
DC
6077}
6078
5d7cb8df
JK
6079/* Read a partial die corresponding to a Fortran module. */
6080
6081static void
6082add_partial_module (struct partial_die_info *pdi, CORE_ADDR *lowpc,
6083 CORE_ADDR *highpc, int need_pc, struct dwarf2_cu *cu)
6084{
f55ee35c 6085 /* Now scan partial symbols in that module. */
5d7cb8df
JK
6086
6087 if (pdi->has_children)
6088 scan_partial_symbols (pdi->die_child, lowpc, highpc, need_pc, cu);
6089}
6090
bc30ff58
JB
6091/* Read a partial die corresponding to a subprogram and create a partial
6092 symbol for that subprogram. When the CU language allows it, this
6093 routine also defines a partial symbol for each nested subprogram
6094 that this subprogram contains.
6e70227d 6095
bc30ff58
JB
6096 DIE my also be a lexical block, in which case we simply search
6097 recursively for suprograms defined inside that lexical block.
6098 Again, this is only performed when the CU language allows this
6099 type of definitions. */
6100
6101static void
6102add_partial_subprogram (struct partial_die_info *pdi,
6103 CORE_ADDR *lowpc, CORE_ADDR *highpc,
5734ee8b 6104 int need_pc, struct dwarf2_cu *cu)
bc30ff58
JB
6105{
6106 if (pdi->tag == DW_TAG_subprogram)
6107 {
6108 if (pdi->has_pc_info)
6109 {
6110 if (pdi->lowpc < *lowpc)
6111 *lowpc = pdi->lowpc;
6112 if (pdi->highpc > *highpc)
6113 *highpc = pdi->highpc;
5734ee8b
DJ
6114 if (need_pc)
6115 {
6116 CORE_ADDR baseaddr;
6117 struct objfile *objfile = cu->objfile;
6118
6119 baseaddr = ANOFFSET (objfile->section_offsets,
6120 SECT_OFF_TEXT (objfile));
6121 addrmap_set_empty (objfile->psymtabs_addrmap,
01637564
DE
6122 pdi->lowpc + baseaddr,
6123 pdi->highpc - 1 + baseaddr,
9291a0cd 6124 cu->per_cu->v.psymtab);
5734ee8b 6125 }
481860b3
GB
6126 }
6127
6128 if (pdi->has_pc_info || (!pdi->is_external && pdi->may_be_inlined))
6129 {
bc30ff58 6130 if (!pdi->is_declaration)
e8d05480
JB
6131 /* Ignore subprogram DIEs that do not have a name, they are
6132 illegal. Do not emit a complaint at this point, we will
6133 do so when we convert this psymtab into a symtab. */
6134 if (pdi->name)
6135 add_partial_symbol (pdi, cu);
bc30ff58
JB
6136 }
6137 }
6e70227d 6138
bc30ff58
JB
6139 if (! pdi->has_children)
6140 return;
6141
6142 if (cu->language == language_ada)
6143 {
6144 pdi = pdi->die_child;
6145 while (pdi != NULL)
6146 {
6147 fixup_partial_die (pdi, cu);
6148 if (pdi->tag == DW_TAG_subprogram
6149 || pdi->tag == DW_TAG_lexical_block)
5734ee8b 6150 add_partial_subprogram (pdi, lowpc, highpc, need_pc, cu);
bc30ff58
JB
6151 pdi = pdi->die_sibling;
6152 }
6153 }
6154}
6155
91c24f0a
DC
6156/* Read a partial die corresponding to an enumeration type. */
6157
72bf9492
DJ
6158static void
6159add_partial_enumeration (struct partial_die_info *enum_pdi,
6160 struct dwarf2_cu *cu)
91c24f0a 6161{
72bf9492 6162 struct partial_die_info *pdi;
91c24f0a
DC
6163
6164 if (enum_pdi->name != NULL)
72bf9492
DJ
6165 add_partial_symbol (enum_pdi, cu);
6166
6167 pdi = enum_pdi->die_child;
6168 while (pdi)
91c24f0a 6169 {
72bf9492 6170 if (pdi->tag != DW_TAG_enumerator || pdi->name == NULL)
e2e0b3e5 6171 complaint (&symfile_complaints, _("malformed enumerator DIE ignored"));
91c24f0a 6172 else
72bf9492
DJ
6173 add_partial_symbol (pdi, cu);
6174 pdi = pdi->die_sibling;
91c24f0a 6175 }
91c24f0a
DC
6176}
6177
6caca83c
CC
6178/* Return the initial uleb128 in the die at INFO_PTR. */
6179
6180static unsigned int
6181peek_abbrev_code (bfd *abfd, gdb_byte *info_ptr)
6182{
6183 unsigned int bytes_read;
6184
6185 return read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
6186}
6187
4bb7a0a7
DJ
6188/* Read the initial uleb128 in the die at INFO_PTR in compilation unit CU.
6189 Return the corresponding abbrev, or NULL if the number is zero (indicating
6190 an empty DIE). In either case *BYTES_READ will be set to the length of
6191 the initial number. */
6192
6193static struct abbrev_info *
fe1b8b76 6194peek_die_abbrev (gdb_byte *info_ptr, unsigned int *bytes_read,
891d2f0b 6195 struct dwarf2_cu *cu)
4bb7a0a7
DJ
6196{
6197 bfd *abfd = cu->objfile->obfd;
6198 unsigned int abbrev_number;
6199 struct abbrev_info *abbrev;
6200
6201 abbrev_number = read_unsigned_leb128 (abfd, info_ptr, bytes_read);
6202
6203 if (abbrev_number == 0)
6204 return NULL;
6205
433df2d4 6206 abbrev = abbrev_table_lookup_abbrev (cu->abbrev_table, abbrev_number);
4bb7a0a7
DJ
6207 if (!abbrev)
6208 {
3e43a32a
MS
6209 error (_("Dwarf Error: Could not find abbrev number %d [in module %s]"),
6210 abbrev_number, bfd_get_filename (abfd));
4bb7a0a7
DJ
6211 }
6212
6213 return abbrev;
6214}
6215
93311388
DE
6216/* Scan the debug information for CU starting at INFO_PTR in buffer BUFFER.
6217 Returns a pointer to the end of a series of DIEs, terminated by an empty
4bb7a0a7
DJ
6218 DIE. Any children of the skipped DIEs will also be skipped. */
6219
fe1b8b76 6220static gdb_byte *
dee91e82 6221skip_children (const struct die_reader_specs *reader, gdb_byte *info_ptr)
4bb7a0a7 6222{
dee91e82 6223 struct dwarf2_cu *cu = reader->cu;
4bb7a0a7
DJ
6224 struct abbrev_info *abbrev;
6225 unsigned int bytes_read;
6226
6227 while (1)
6228 {
6229 abbrev = peek_die_abbrev (info_ptr, &bytes_read, cu);
6230 if (abbrev == NULL)
6231 return info_ptr + bytes_read;
6232 else
dee91e82 6233 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
4bb7a0a7
DJ
6234 }
6235}
6236
93311388
DE
6237/* Scan the debug information for CU starting at INFO_PTR in buffer BUFFER.
6238 INFO_PTR should point just after the initial uleb128 of a DIE, and the
4bb7a0a7
DJ
6239 abbrev corresponding to that skipped uleb128 should be passed in
6240 ABBREV. Returns a pointer to this DIE's sibling, skipping any
6241 children. */
6242
fe1b8b76 6243static gdb_byte *
dee91e82
DE
6244skip_one_die (const struct die_reader_specs *reader, gdb_byte *info_ptr,
6245 struct abbrev_info *abbrev)
4bb7a0a7
DJ
6246{
6247 unsigned int bytes_read;
6248 struct attribute attr;
dee91e82
DE
6249 bfd *abfd = reader->abfd;
6250 struct dwarf2_cu *cu = reader->cu;
6251 gdb_byte *buffer = reader->buffer;
f664829e
DE
6252 const gdb_byte *buffer_end = reader->buffer_end;
6253 gdb_byte *start_info_ptr = info_ptr;
4bb7a0a7
DJ
6254 unsigned int form, i;
6255
6256 for (i = 0; i < abbrev->num_attrs; i++)
6257 {
6258 /* The only abbrev we care about is DW_AT_sibling. */
6259 if (abbrev->attrs[i].name == DW_AT_sibling)
6260 {
dee91e82 6261 read_attribute (reader, &attr, &abbrev->attrs[i], info_ptr);
4bb7a0a7 6262 if (attr.form == DW_FORM_ref_addr)
3e43a32a
MS
6263 complaint (&symfile_complaints,
6264 _("ignoring absolute DW_AT_sibling"));
4bb7a0a7 6265 else
b64f50a1 6266 return buffer + dwarf2_get_ref_die_offset (&attr).sect_off;
4bb7a0a7
DJ
6267 }
6268
6269 /* If it isn't DW_AT_sibling, skip this attribute. */
6270 form = abbrev->attrs[i].form;
6271 skip_attribute:
6272 switch (form)
6273 {
4bb7a0a7 6274 case DW_FORM_ref_addr:
ae411497
TT
6275 /* In DWARF 2, DW_FORM_ref_addr is address sized; in DWARF 3
6276 and later it is offset sized. */
6277 if (cu->header.version == 2)
6278 info_ptr += cu->header.addr_size;
6279 else
6280 info_ptr += cu->header.offset_size;
6281 break;
36586728
TT
6282 case DW_FORM_GNU_ref_alt:
6283 info_ptr += cu->header.offset_size;
6284 break;
ae411497 6285 case DW_FORM_addr:
4bb7a0a7
DJ
6286 info_ptr += cu->header.addr_size;
6287 break;
6288 case DW_FORM_data1:
6289 case DW_FORM_ref1:
6290 case DW_FORM_flag:
6291 info_ptr += 1;
6292 break;
2dc7f7b3
TT
6293 case DW_FORM_flag_present:
6294 break;
4bb7a0a7
DJ
6295 case DW_FORM_data2:
6296 case DW_FORM_ref2:
6297 info_ptr += 2;
6298 break;
6299 case DW_FORM_data4:
6300 case DW_FORM_ref4:
6301 info_ptr += 4;
6302 break;
6303 case DW_FORM_data8:
6304 case DW_FORM_ref8:
55f1336d 6305 case DW_FORM_ref_sig8:
4bb7a0a7
DJ
6306 info_ptr += 8;
6307 break;
6308 case DW_FORM_string:
9b1c24c8 6309 read_direct_string (abfd, info_ptr, &bytes_read);
4bb7a0a7
DJ
6310 info_ptr += bytes_read;
6311 break;
2dc7f7b3 6312 case DW_FORM_sec_offset:
4bb7a0a7 6313 case DW_FORM_strp:
36586728 6314 case DW_FORM_GNU_strp_alt:
4bb7a0a7
DJ
6315 info_ptr += cu->header.offset_size;
6316 break;
2dc7f7b3 6317 case DW_FORM_exprloc:
4bb7a0a7
DJ
6318 case DW_FORM_block:
6319 info_ptr += read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
6320 info_ptr += bytes_read;
6321 break;
6322 case DW_FORM_block1:
6323 info_ptr += 1 + read_1_byte (abfd, info_ptr);
6324 break;
6325 case DW_FORM_block2:
6326 info_ptr += 2 + read_2_bytes (abfd, info_ptr);
6327 break;
6328 case DW_FORM_block4:
6329 info_ptr += 4 + read_4_bytes (abfd, info_ptr);
6330 break;
6331 case DW_FORM_sdata:
6332 case DW_FORM_udata:
6333 case DW_FORM_ref_udata:
3019eac3
DE
6334 case DW_FORM_GNU_addr_index:
6335 case DW_FORM_GNU_str_index:
f664829e 6336 info_ptr = (gdb_byte *) safe_skip_leb128 (info_ptr, buffer_end);
4bb7a0a7
DJ
6337 break;
6338 case DW_FORM_indirect:
6339 form = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
6340 info_ptr += bytes_read;
6341 /* We need to continue parsing from here, so just go back to
6342 the top. */
6343 goto skip_attribute;
6344
6345 default:
3e43a32a
MS
6346 error (_("Dwarf Error: Cannot handle %s "
6347 "in DWARF reader [in module %s]"),
4bb7a0a7
DJ
6348 dwarf_form_name (form),
6349 bfd_get_filename (abfd));
6350 }
6351 }
6352
6353 if (abbrev->has_children)
dee91e82 6354 return skip_children (reader, info_ptr);
4bb7a0a7
DJ
6355 else
6356 return info_ptr;
6357}
6358
93311388 6359/* Locate ORIG_PDI's sibling.
dee91e82 6360 INFO_PTR should point to the start of the next DIE after ORIG_PDI. */
91c24f0a 6361
fe1b8b76 6362static gdb_byte *
dee91e82
DE
6363locate_pdi_sibling (const struct die_reader_specs *reader,
6364 struct partial_die_info *orig_pdi,
6365 gdb_byte *info_ptr)
91c24f0a
DC
6366{
6367 /* Do we know the sibling already? */
72bf9492 6368
91c24f0a
DC
6369 if (orig_pdi->sibling)
6370 return orig_pdi->sibling;
6371
6372 /* Are there any children to deal with? */
6373
6374 if (!orig_pdi->has_children)
6375 return info_ptr;
6376
4bb7a0a7 6377 /* Skip the children the long way. */
91c24f0a 6378
dee91e82 6379 return skip_children (reader, info_ptr);
91c24f0a
DC
6380}
6381
257e7a09 6382/* Expand this partial symbol table into a full symbol table. SELF is
442e4d9c 6383 not NULL. */
c906108c
SS
6384
6385static void
257e7a09
YQ
6386dwarf2_read_symtab (struct partial_symtab *self,
6387 struct objfile *objfile)
c906108c 6388{
257e7a09 6389 if (self->readin)
c906108c 6390 {
442e4d9c 6391 warning (_("bug: psymtab for %s is already read in."),
257e7a09 6392 self->filename);
442e4d9c
YQ
6393 }
6394 else
6395 {
6396 if (info_verbose)
c906108c 6397 {
442e4d9c 6398 printf_filtered (_("Reading in symbols for %s..."),
257e7a09 6399 self->filename);
442e4d9c 6400 gdb_flush (gdb_stdout);
c906108c 6401 }
c906108c 6402
442e4d9c
YQ
6403 /* Restore our global data. */
6404 dwarf2_per_objfile = objfile_data (objfile, dwarf2_objfile_data_key);
10b3939b 6405
442e4d9c
YQ
6406 /* If this psymtab is constructed from a debug-only objfile, the
6407 has_section_at_zero flag will not necessarily be correct. We
6408 can get the correct value for this flag by looking at the data
6409 associated with the (presumably stripped) associated objfile. */
6410 if (objfile->separate_debug_objfile_backlink)
6411 {
6412 struct dwarf2_per_objfile *dpo_backlink
6413 = objfile_data (objfile->separate_debug_objfile_backlink,
6414 dwarf2_objfile_data_key);
9a619af0 6415
442e4d9c
YQ
6416 dwarf2_per_objfile->has_section_at_zero
6417 = dpo_backlink->has_section_at_zero;
6418 }
b2ab525c 6419
442e4d9c 6420 dwarf2_per_objfile->reading_partial_symbols = 0;
98bfdba5 6421
257e7a09 6422 psymtab_to_symtab_1 (self);
c906108c 6423
442e4d9c
YQ
6424 /* Finish up the debug error message. */
6425 if (info_verbose)
6426 printf_filtered (_("done.\n"));
c906108c 6427 }
95554aad
TT
6428
6429 process_cu_includes ();
c906108c 6430}
9cdd5dbd
DE
6431\f
6432/* Reading in full CUs. */
c906108c 6433
10b3939b
DJ
6434/* Add PER_CU to the queue. */
6435
6436static void
95554aad
TT
6437queue_comp_unit (struct dwarf2_per_cu_data *per_cu,
6438 enum language pretend_language)
10b3939b
DJ
6439{
6440 struct dwarf2_queue_item *item;
6441
6442 per_cu->queued = 1;
6443 item = xmalloc (sizeof (*item));
6444 item->per_cu = per_cu;
95554aad 6445 item->pretend_language = pretend_language;
10b3939b
DJ
6446 item->next = NULL;
6447
6448 if (dwarf2_queue == NULL)
6449 dwarf2_queue = item;
6450 else
6451 dwarf2_queue_tail->next = item;
6452
6453 dwarf2_queue_tail = item;
6454}
6455
0907af0c
DE
6456/* THIS_CU has a reference to PER_CU. If necessary, load the new compilation
6457 unit and add it to our queue.
6458 The result is non-zero if PER_CU was queued, otherwise the result is zero
6459 meaning either PER_CU is already queued or it is already loaded. */
6460
6461static int
6462maybe_queue_comp_unit (struct dwarf2_cu *this_cu,
6463 struct dwarf2_per_cu_data *per_cu,
6464 enum language pretend_language)
6465{
6466 /* We may arrive here during partial symbol reading, if we need full
6467 DIEs to process an unusual case (e.g. template arguments). Do
6468 not queue PER_CU, just tell our caller to load its DIEs. */
6469 if (dwarf2_per_objfile->reading_partial_symbols)
6470 {
6471 if (per_cu->cu == NULL || per_cu->cu->dies == NULL)
6472 return 1;
6473 return 0;
6474 }
6475
6476 /* Mark the dependence relation so that we don't flush PER_CU
6477 too early. */
6478 dwarf2_add_dependence (this_cu, per_cu);
6479
6480 /* If it's already on the queue, we have nothing to do. */
6481 if (per_cu->queued)
6482 return 0;
6483
6484 /* If the compilation unit is already loaded, just mark it as
6485 used. */
6486 if (per_cu->cu != NULL)
6487 {
6488 per_cu->cu->last_used = 0;
6489 return 0;
6490 }
6491
6492 /* Add it to the queue. */
6493 queue_comp_unit (per_cu, pretend_language);
6494
6495 return 1;
6496}
6497
10b3939b
DJ
6498/* Process the queue. */
6499
6500static void
a0f42c21 6501process_queue (void)
10b3939b
DJ
6502{
6503 struct dwarf2_queue_item *item, *next_item;
6504
45cfd468
DE
6505 if (dwarf2_read_debug)
6506 {
6507 fprintf_unfiltered (gdb_stdlog,
6508 "Expanding one or more symtabs of objfile %s ...\n",
6509 dwarf2_per_objfile->objfile->name);
6510 }
6511
03dd20cc
DJ
6512 /* The queue starts out with one item, but following a DIE reference
6513 may load a new CU, adding it to the end of the queue. */
10b3939b
DJ
6514 for (item = dwarf2_queue; item != NULL; dwarf2_queue = item = next_item)
6515 {
9291a0cd
TT
6516 if (dwarf2_per_objfile->using_index
6517 ? !item->per_cu->v.quick->symtab
6518 : (item->per_cu->v.psymtab && !item->per_cu->v.psymtab->readin))
f4dc4d17
DE
6519 {
6520 struct dwarf2_per_cu_data *per_cu = item->per_cu;
6521
6522 if (dwarf2_read_debug)
6523 {
6524 fprintf_unfiltered (gdb_stdlog,
6525 "Expanding symtab of %s at offset 0x%x\n",
6526 per_cu->is_debug_types ? "TU" : "CU",
6527 per_cu->offset.sect_off);
6528 }
6529
6530 if (per_cu->is_debug_types)
6531 process_full_type_unit (per_cu, item->pretend_language);
6532 else
6533 process_full_comp_unit (per_cu, item->pretend_language);
6534
6535 if (dwarf2_read_debug)
6536 {
6537 fprintf_unfiltered (gdb_stdlog,
6538 "Done expanding %s at offset 0x%x\n",
6539 per_cu->is_debug_types ? "TU" : "CU",
6540 per_cu->offset.sect_off);
6541 }
6542 }
10b3939b
DJ
6543
6544 item->per_cu->queued = 0;
6545 next_item = item->next;
6546 xfree (item);
6547 }
6548
6549 dwarf2_queue_tail = NULL;
45cfd468
DE
6550
6551 if (dwarf2_read_debug)
6552 {
6553 fprintf_unfiltered (gdb_stdlog, "Done expanding symtabs of %s.\n",
6554 dwarf2_per_objfile->objfile->name);
6555 }
10b3939b
DJ
6556}
6557
6558/* Free all allocated queue entries. This function only releases anything if
6559 an error was thrown; if the queue was processed then it would have been
6560 freed as we went along. */
6561
6562static void
6563dwarf2_release_queue (void *dummy)
6564{
6565 struct dwarf2_queue_item *item, *last;
6566
6567 item = dwarf2_queue;
6568 while (item)
6569 {
6570 /* Anything still marked queued is likely to be in an
6571 inconsistent state, so discard it. */
6572 if (item->per_cu->queued)
6573 {
6574 if (item->per_cu->cu != NULL)
dee91e82 6575 free_one_cached_comp_unit (item->per_cu);
10b3939b
DJ
6576 item->per_cu->queued = 0;
6577 }
6578
6579 last = item;
6580 item = item->next;
6581 xfree (last);
6582 }
6583
6584 dwarf2_queue = dwarf2_queue_tail = NULL;
6585}
6586
6587/* Read in full symbols for PST, and anything it depends on. */
6588
c906108c 6589static void
fba45db2 6590psymtab_to_symtab_1 (struct partial_symtab *pst)
c906108c 6591{
10b3939b 6592 struct dwarf2_per_cu_data *per_cu;
aaa75496
JB
6593 int i;
6594
95554aad
TT
6595 if (pst->readin)
6596 return;
6597
aaa75496 6598 for (i = 0; i < pst->number_of_dependencies; i++)
95554aad
TT
6599 if (!pst->dependencies[i]->readin
6600 && pst->dependencies[i]->user == NULL)
aaa75496
JB
6601 {
6602 /* Inform about additional files that need to be read in. */
6603 if (info_verbose)
6604 {
a3f17187 6605 /* FIXME: i18n: Need to make this a single string. */
aaa75496
JB
6606 fputs_filtered (" ", gdb_stdout);
6607 wrap_here ("");
6608 fputs_filtered ("and ", gdb_stdout);
6609 wrap_here ("");
6610 printf_filtered ("%s...", pst->dependencies[i]->filename);
0963b4bd 6611 wrap_here (""); /* Flush output. */
aaa75496
JB
6612 gdb_flush (gdb_stdout);
6613 }
6614 psymtab_to_symtab_1 (pst->dependencies[i]);
6615 }
6616
e38df1d0 6617 per_cu = pst->read_symtab_private;
10b3939b
DJ
6618
6619 if (per_cu == NULL)
aaa75496
JB
6620 {
6621 /* It's an include file, no symbols to read for it.
6622 Everything is in the parent symtab. */
6623 pst->readin = 1;
6624 return;
6625 }
c906108c 6626
a0f42c21 6627 dw2_do_instantiate_symtab (per_cu);
10b3939b
DJ
6628}
6629
dee91e82
DE
6630/* Trivial hash function for die_info: the hash value of a DIE
6631 is its offset in .debug_info for this objfile. */
10b3939b 6632
dee91e82
DE
6633static hashval_t
6634die_hash (const void *item)
10b3939b 6635{
dee91e82 6636 const struct die_info *die = item;
6502dd73 6637
dee91e82
DE
6638 return die->offset.sect_off;
6639}
63d06c5c 6640
dee91e82
DE
6641/* Trivial comparison function for die_info structures: two DIEs
6642 are equal if they have the same offset. */
98bfdba5 6643
dee91e82
DE
6644static int
6645die_eq (const void *item_lhs, const void *item_rhs)
6646{
6647 const struct die_info *die_lhs = item_lhs;
6648 const struct die_info *die_rhs = item_rhs;
c906108c 6649
dee91e82
DE
6650 return die_lhs->offset.sect_off == die_rhs->offset.sect_off;
6651}
c906108c 6652
dee91e82
DE
6653/* die_reader_func for load_full_comp_unit.
6654 This is identical to read_signatured_type_reader,
6655 but is kept separate for now. */
c906108c 6656
dee91e82
DE
6657static void
6658load_full_comp_unit_reader (const struct die_reader_specs *reader,
6659 gdb_byte *info_ptr,
6660 struct die_info *comp_unit_die,
6661 int has_children,
6662 void *data)
6663{
6664 struct dwarf2_cu *cu = reader->cu;
95554aad 6665 enum language *language_ptr = data;
6caca83c 6666
dee91e82
DE
6667 gdb_assert (cu->die_hash == NULL);
6668 cu->die_hash =
6669 htab_create_alloc_ex (cu->header.length / 12,
6670 die_hash,
6671 die_eq,
6672 NULL,
6673 &cu->comp_unit_obstack,
6674 hashtab_obstack_allocate,
6675 dummy_obstack_deallocate);
e142c38c 6676
dee91e82
DE
6677 if (has_children)
6678 comp_unit_die->child = read_die_and_siblings (reader, info_ptr,
6679 &info_ptr, comp_unit_die);
6680 cu->dies = comp_unit_die;
6681 /* comp_unit_die is not stored in die_hash, no need. */
10b3939b
DJ
6682
6683 /* We try not to read any attributes in this function, because not
9cdd5dbd 6684 all CUs needed for references have been loaded yet, and symbol
10b3939b 6685 table processing isn't initialized. But we have to set the CU language,
dee91e82
DE
6686 or we won't be able to build types correctly.
6687 Similarly, if we do not read the producer, we can not apply
6688 producer-specific interpretation. */
95554aad 6689 prepare_one_comp_unit (cu, cu->dies, *language_ptr);
dee91e82 6690}
10b3939b 6691
dee91e82 6692/* Load the DIEs associated with PER_CU into memory. */
a6c727b2 6693
dee91e82 6694static void
95554aad
TT
6695load_full_comp_unit (struct dwarf2_per_cu_data *this_cu,
6696 enum language pretend_language)
dee91e82 6697{
3019eac3 6698 gdb_assert (! this_cu->is_debug_types);
c5b7e1cb 6699
f4dc4d17
DE
6700 init_cutu_and_read_dies (this_cu, NULL, 1, 1,
6701 load_full_comp_unit_reader, &pretend_language);
10b3939b
DJ
6702}
6703
3da10d80
KS
6704/* Add a DIE to the delayed physname list. */
6705
6706static void
6707add_to_method_list (struct type *type, int fnfield_index, int index,
6708 const char *name, struct die_info *die,
6709 struct dwarf2_cu *cu)
6710{
6711 struct delayed_method_info mi;
6712 mi.type = type;
6713 mi.fnfield_index = fnfield_index;
6714 mi.index = index;
6715 mi.name = name;
6716 mi.die = die;
6717 VEC_safe_push (delayed_method_info, cu->method_list, &mi);
6718}
6719
6720/* A cleanup for freeing the delayed method list. */
6721
6722static void
6723free_delayed_list (void *ptr)
6724{
6725 struct dwarf2_cu *cu = (struct dwarf2_cu *) ptr;
6726 if (cu->method_list != NULL)
6727 {
6728 VEC_free (delayed_method_info, cu->method_list);
6729 cu->method_list = NULL;
6730 }
6731}
6732
6733/* Compute the physnames of any methods on the CU's method list.
6734
6735 The computation of method physnames is delayed in order to avoid the
6736 (bad) condition that one of the method's formal parameters is of an as yet
6737 incomplete type. */
6738
6739static void
6740compute_delayed_physnames (struct dwarf2_cu *cu)
6741{
6742 int i;
6743 struct delayed_method_info *mi;
6744 for (i = 0; VEC_iterate (delayed_method_info, cu->method_list, i, mi) ; ++i)
6745 {
1d06ead6 6746 const char *physname;
3da10d80
KS
6747 struct fn_fieldlist *fn_flp
6748 = &TYPE_FN_FIELDLIST (mi->type, mi->fnfield_index);
7d455152 6749 physname = dwarf2_physname (mi->name, mi->die, cu);
3da10d80
KS
6750 fn_flp->fn_fields[mi->index].physname = physname ? physname : "";
6751 }
6752}
6753
a766d390
DE
6754/* Go objects should be embedded in a DW_TAG_module DIE,
6755 and it's not clear if/how imported objects will appear.
6756 To keep Go support simple until that's worked out,
6757 go back through what we've read and create something usable.
6758 We could do this while processing each DIE, and feels kinda cleaner,
6759 but that way is more invasive.
6760 This is to, for example, allow the user to type "p var" or "b main"
6761 without having to specify the package name, and allow lookups
6762 of module.object to work in contexts that use the expression
6763 parser. */
6764
6765static void
6766fixup_go_packaging (struct dwarf2_cu *cu)
6767{
6768 char *package_name = NULL;
6769 struct pending *list;
6770 int i;
6771
6772 for (list = global_symbols; list != NULL; list = list->next)
6773 {
6774 for (i = 0; i < list->nsyms; ++i)
6775 {
6776 struct symbol *sym = list->symbol[i];
6777
6778 if (SYMBOL_LANGUAGE (sym) == language_go
6779 && SYMBOL_CLASS (sym) == LOC_BLOCK)
6780 {
6781 char *this_package_name = go_symbol_package_name (sym);
6782
6783 if (this_package_name == NULL)
6784 continue;
6785 if (package_name == NULL)
6786 package_name = this_package_name;
6787 else
6788 {
6789 if (strcmp (package_name, this_package_name) != 0)
6790 complaint (&symfile_complaints,
6791 _("Symtab %s has objects from two different Go packages: %s and %s"),
210bbc17 6792 (SYMBOL_SYMTAB (sym)
210bbc17 6793 ? SYMBOL_SYMTAB (sym)->filename
a766d390
DE
6794 : cu->objfile->name),
6795 this_package_name, package_name);
6796 xfree (this_package_name);
6797 }
6798 }
6799 }
6800 }
6801
6802 if (package_name != NULL)
6803 {
6804 struct objfile *objfile = cu->objfile;
10f0c4bb
TT
6805 const char *saved_package_name = obstack_copy0 (&objfile->objfile_obstack,
6806 package_name,
6807 strlen (package_name));
a766d390 6808 struct type *type = init_type (TYPE_CODE_MODULE, 0, 0,
86f62fd7 6809 saved_package_name, objfile);
a766d390
DE
6810 struct symbol *sym;
6811
6812 TYPE_TAG_NAME (type) = TYPE_NAME (type);
6813
6814 sym = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct symbol);
6815 SYMBOL_SET_LANGUAGE (sym, language_go);
86f62fd7
TT
6816 SYMBOL_SET_NAMES (sym, saved_package_name,
6817 strlen (saved_package_name), 0, objfile);
a766d390
DE
6818 /* This is not VAR_DOMAIN because we want a way to ensure a lookup of,
6819 e.g., "main" finds the "main" module and not C's main(). */
6820 SYMBOL_DOMAIN (sym) = STRUCT_DOMAIN;
6821 SYMBOL_CLASS (sym) = LOC_TYPEDEF;
6822 SYMBOL_TYPE (sym) = type;
6823
6824 add_symbol_to_list (sym, &global_symbols);
6825
6826 xfree (package_name);
6827 }
6828}
6829
95554aad
TT
6830static void compute_symtab_includes (struct dwarf2_per_cu_data *per_cu);
6831
6832/* Return the symtab for PER_CU. This works properly regardless of
6833 whether we're using the index or psymtabs. */
6834
6835static struct symtab *
6836get_symtab (struct dwarf2_per_cu_data *per_cu)
6837{
6838 return (dwarf2_per_objfile->using_index
6839 ? per_cu->v.quick->symtab
6840 : per_cu->v.psymtab->symtab);
6841}
6842
6843/* A helper function for computing the list of all symbol tables
6844 included by PER_CU. */
6845
6846static void
6847recursively_compute_inclusions (VEC (dwarf2_per_cu_ptr) **result,
6848 htab_t all_children,
6849 struct dwarf2_per_cu_data *per_cu)
6850{
6851 void **slot;
6852 int ix;
6853 struct dwarf2_per_cu_data *iter;
6854
6855 slot = htab_find_slot (all_children, per_cu, INSERT);
6856 if (*slot != NULL)
6857 {
6858 /* This inclusion and its children have been processed. */
6859 return;
6860 }
6861
6862 *slot = per_cu;
6863 /* Only add a CU if it has a symbol table. */
6864 if (get_symtab (per_cu) != NULL)
6865 VEC_safe_push (dwarf2_per_cu_ptr, *result, per_cu);
6866
6867 for (ix = 0;
796a7ff8 6868 VEC_iterate (dwarf2_per_cu_ptr, per_cu->imported_symtabs, ix, iter);
95554aad
TT
6869 ++ix)
6870 recursively_compute_inclusions (result, all_children, iter);
6871}
6872
6873/* Compute the symtab 'includes' fields for the symtab related to
6874 PER_CU. */
6875
6876static void
6877compute_symtab_includes (struct dwarf2_per_cu_data *per_cu)
6878{
f4dc4d17
DE
6879 gdb_assert (! per_cu->is_debug_types);
6880
796a7ff8 6881 if (!VEC_empty (dwarf2_per_cu_ptr, per_cu->imported_symtabs))
95554aad
TT
6882 {
6883 int ix, len;
6884 struct dwarf2_per_cu_data *iter;
6885 VEC (dwarf2_per_cu_ptr) *result_children = NULL;
6886 htab_t all_children;
6887 struct symtab *symtab = get_symtab (per_cu);
6888
6889 /* If we don't have a symtab, we can just skip this case. */
6890 if (symtab == NULL)
6891 return;
6892
6893 all_children = htab_create_alloc (1, htab_hash_pointer, htab_eq_pointer,
6894 NULL, xcalloc, xfree);
6895
6896 for (ix = 0;
796a7ff8 6897 VEC_iterate (dwarf2_per_cu_ptr, per_cu->imported_symtabs,
95554aad
TT
6898 ix, iter);
6899 ++ix)
6900 recursively_compute_inclusions (&result_children, all_children, iter);
6901
796a7ff8
DE
6902 /* Now we have a transitive closure of all the included CUs, and
6903 for .gdb_index version 7 the included TUs, so we can convert it
6904 to a list of symtabs. */
95554aad
TT
6905 len = VEC_length (dwarf2_per_cu_ptr, result_children);
6906 symtab->includes
6907 = obstack_alloc (&dwarf2_per_objfile->objfile->objfile_obstack,
6908 (len + 1) * sizeof (struct symtab *));
6909 for (ix = 0;
6910 VEC_iterate (dwarf2_per_cu_ptr, result_children, ix, iter);
6911 ++ix)
6912 symtab->includes[ix] = get_symtab (iter);
6913 symtab->includes[len] = NULL;
6914
6915 VEC_free (dwarf2_per_cu_ptr, result_children);
6916 htab_delete (all_children);
6917 }
6918}
6919
6920/* Compute the 'includes' field for the symtabs of all the CUs we just
6921 read. */
6922
6923static void
6924process_cu_includes (void)
6925{
6926 int ix;
6927 struct dwarf2_per_cu_data *iter;
6928
6929 for (ix = 0;
6930 VEC_iterate (dwarf2_per_cu_ptr, dwarf2_per_objfile->just_read_cus,
6931 ix, iter);
6932 ++ix)
f4dc4d17
DE
6933 {
6934 if (! iter->is_debug_types)
6935 compute_symtab_includes (iter);
6936 }
95554aad
TT
6937
6938 VEC_free (dwarf2_per_cu_ptr, dwarf2_per_objfile->just_read_cus);
6939}
6940
9cdd5dbd 6941/* Generate full symbol information for PER_CU, whose DIEs have
10b3939b
DJ
6942 already been loaded into memory. */
6943
6944static void
95554aad
TT
6945process_full_comp_unit (struct dwarf2_per_cu_data *per_cu,
6946 enum language pretend_language)
10b3939b 6947{
10b3939b 6948 struct dwarf2_cu *cu = per_cu->cu;
9291a0cd 6949 struct objfile *objfile = per_cu->objfile;
10b3939b
DJ
6950 CORE_ADDR lowpc, highpc;
6951 struct symtab *symtab;
3da10d80 6952 struct cleanup *back_to, *delayed_list_cleanup;
10b3939b 6953 CORE_ADDR baseaddr;
4359dff1 6954 struct block *static_block;
10b3939b
DJ
6955
6956 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
6957
10b3939b
DJ
6958 buildsym_init ();
6959 back_to = make_cleanup (really_free_pendings, NULL);
3da10d80 6960 delayed_list_cleanup = make_cleanup (free_delayed_list, cu);
10b3939b
DJ
6961
6962 cu->list_in_scope = &file_symbols;
c906108c 6963
95554aad
TT
6964 cu->language = pretend_language;
6965 cu->language_defn = language_def (cu->language);
6966
c906108c 6967 /* Do line number decoding in read_file_scope () */
10b3939b 6968 process_die (cu->dies, cu);
c906108c 6969
a766d390
DE
6970 /* For now fudge the Go package. */
6971 if (cu->language == language_go)
6972 fixup_go_packaging (cu);
6973
3da10d80
KS
6974 /* Now that we have processed all the DIEs in the CU, all the types
6975 should be complete, and it should now be safe to compute all of the
6976 physnames. */
6977 compute_delayed_physnames (cu);
6978 do_cleanups (delayed_list_cleanup);
6979
fae299cd
DC
6980 /* Some compilers don't define a DW_AT_high_pc attribute for the
6981 compilation unit. If the DW_AT_high_pc is missing, synthesize
6982 it, by scanning the DIE's below the compilation unit. */
10b3939b 6983 get_scope_pc_bounds (cu->dies, &lowpc, &highpc, cu);
c906108c 6984
36586728
TT
6985 static_block
6986 = end_symtab_get_static_block (highpc + baseaddr, objfile, 0,
796a7ff8 6987 per_cu->imported_symtabs != NULL);
4359dff1
JK
6988
6989 /* If the comp unit has DW_AT_ranges, it may have discontiguous ranges.
6990 Also, DW_AT_ranges may record ranges not belonging to any child DIEs
6991 (such as virtual method tables). Record the ranges in STATIC_BLOCK's
6992 addrmap to help ensure it has an accurate map of pc values belonging to
6993 this comp unit. */
6994 dwarf2_record_block_ranges (cu->dies, static_block, baseaddr, cu);
6995
6996 symtab = end_symtab_from_static_block (static_block, objfile,
6997 SECT_OFF_TEXT (objfile), 0);
c906108c 6998
8be455d7 6999 if (symtab != NULL)
c906108c 7000 {
df15bd07 7001 int gcc_4_minor = producer_is_gcc_ge_4 (cu->producer);
4632c0d0 7002
8be455d7
JK
7003 /* Set symtab language to language from DW_AT_language. If the
7004 compilation is from a C file generated by language preprocessors, do
7005 not set the language if it was already deduced by start_subfile. */
7006 if (!(cu->language == language_c && symtab->language != language_c))
7007 symtab->language = cu->language;
7008
7009 /* GCC-4.0 has started to support -fvar-tracking. GCC-3.x still can
7010 produce DW_AT_location with location lists but it can be possibly
ab260dad
JK
7011 invalid without -fvar-tracking. Still up to GCC-4.4.x incl. 4.4.0
7012 there were bugs in prologue debug info, fixed later in GCC-4.5
7013 by "unwind info for epilogues" patch (which is not directly related).
8be455d7
JK
7014
7015 For -gdwarf-4 type units LOCATIONS_VALID indication is fortunately not
7016 needed, it would be wrong due to missing DW_AT_producer there.
7017
7018 Still one can confuse GDB by using non-standard GCC compilation
7019 options - this waits on GCC PR other/32998 (-frecord-gcc-switches).
7020 */
ab260dad 7021 if (cu->has_loclist && gcc_4_minor >= 5)
8be455d7 7022 symtab->locations_valid = 1;
e0d00bc7
JK
7023
7024 if (gcc_4_minor >= 5)
7025 symtab->epilogue_unwind_valid = 1;
96408a79
SA
7026
7027 symtab->call_site_htab = cu->call_site_htab;
c906108c 7028 }
9291a0cd
TT
7029
7030 if (dwarf2_per_objfile->using_index)
7031 per_cu->v.quick->symtab = symtab;
7032 else
7033 {
7034 struct partial_symtab *pst = per_cu->v.psymtab;
7035 pst->symtab = symtab;
7036 pst->readin = 1;
7037 }
c906108c 7038
95554aad
TT
7039 /* Push it for inclusion processing later. */
7040 VEC_safe_push (dwarf2_per_cu_ptr, dwarf2_per_objfile->just_read_cus, per_cu);
7041
c906108c 7042 do_cleanups (back_to);
f4dc4d17 7043}
45cfd468 7044
f4dc4d17
DE
7045/* Generate full symbol information for type unit PER_CU, whose DIEs have
7046 already been loaded into memory. */
7047
7048static void
7049process_full_type_unit (struct dwarf2_per_cu_data *per_cu,
7050 enum language pretend_language)
7051{
7052 struct dwarf2_cu *cu = per_cu->cu;
7053 struct objfile *objfile = per_cu->objfile;
7054 struct symtab *symtab;
7055 struct cleanup *back_to, *delayed_list_cleanup;
7056
7057 buildsym_init ();
7058 back_to = make_cleanup (really_free_pendings, NULL);
7059 delayed_list_cleanup = make_cleanup (free_delayed_list, cu);
7060
7061 cu->list_in_scope = &file_symbols;
7062
7063 cu->language = pretend_language;
7064 cu->language_defn = language_def (cu->language);
7065
7066 /* The symbol tables are set up in read_type_unit_scope. */
7067 process_die (cu->dies, cu);
7068
7069 /* For now fudge the Go package. */
7070 if (cu->language == language_go)
7071 fixup_go_packaging (cu);
7072
7073 /* Now that we have processed all the DIEs in the CU, all the types
7074 should be complete, and it should now be safe to compute all of the
7075 physnames. */
7076 compute_delayed_physnames (cu);
7077 do_cleanups (delayed_list_cleanup);
7078
7079 /* TUs share symbol tables.
7080 If this is the first TU to use this symtab, complete the construction
094b34ac
DE
7081 of it with end_expandable_symtab. Otherwise, complete the addition of
7082 this TU's symbols to the existing symtab. */
796a7ff8 7083 if (per_cu->type_unit_group->primary_symtab == NULL)
45cfd468 7084 {
f4dc4d17 7085 symtab = end_expandable_symtab (0, objfile, SECT_OFF_TEXT (objfile));
796a7ff8 7086 per_cu->type_unit_group->primary_symtab = symtab;
f4dc4d17
DE
7087
7088 if (symtab != NULL)
7089 {
7090 /* Set symtab language to language from DW_AT_language. If the
7091 compilation is from a C file generated by language preprocessors,
7092 do not set the language if it was already deduced by
7093 start_subfile. */
7094 if (!(cu->language == language_c && symtab->language != language_c))
7095 symtab->language = cu->language;
7096 }
7097 }
7098 else
7099 {
7100 augment_type_symtab (objfile,
796a7ff8
DE
7101 per_cu->type_unit_group->primary_symtab);
7102 symtab = per_cu->type_unit_group->primary_symtab;
f4dc4d17
DE
7103 }
7104
7105 if (dwarf2_per_objfile->using_index)
7106 per_cu->v.quick->symtab = symtab;
7107 else
7108 {
7109 struct partial_symtab *pst = per_cu->v.psymtab;
7110 pst->symtab = symtab;
7111 pst->readin = 1;
45cfd468 7112 }
f4dc4d17
DE
7113
7114 do_cleanups (back_to);
c906108c
SS
7115}
7116
95554aad
TT
7117/* Process an imported unit DIE. */
7118
7119static void
7120process_imported_unit_die (struct die_info *die, struct dwarf2_cu *cu)
7121{
7122 struct attribute *attr;
7123
f4dc4d17
DE
7124 /* For now we don't handle imported units in type units. */
7125 if (cu->per_cu->is_debug_types)
7126 {
7127 error (_("Dwarf Error: DW_TAG_imported_unit is not"
7128 " supported in type units [in module %s]"),
7129 cu->objfile->name);
7130 }
7131
95554aad
TT
7132 attr = dwarf2_attr (die, DW_AT_import, cu);
7133 if (attr != NULL)
7134 {
7135 struct dwarf2_per_cu_data *per_cu;
7136 struct symtab *imported_symtab;
7137 sect_offset offset;
36586728 7138 int is_dwz;
95554aad
TT
7139
7140 offset = dwarf2_get_ref_die_offset (attr);
36586728
TT
7141 is_dwz = (attr->form == DW_FORM_GNU_ref_alt || cu->per_cu->is_dwz);
7142 per_cu = dwarf2_find_containing_comp_unit (offset, is_dwz, cu->objfile);
95554aad
TT
7143
7144 /* Queue the unit, if needed. */
7145 if (maybe_queue_comp_unit (cu, per_cu, cu->language))
7146 load_full_comp_unit (per_cu, cu->language);
7147
796a7ff8 7148 VEC_safe_push (dwarf2_per_cu_ptr, cu->per_cu->imported_symtabs,
95554aad
TT
7149 per_cu);
7150 }
7151}
7152
c906108c
SS
7153/* Process a die and its children. */
7154
7155static void
e7c27a73 7156process_die (struct die_info *die, struct dwarf2_cu *cu)
c906108c
SS
7157{
7158 switch (die->tag)
7159 {
7160 case DW_TAG_padding:
7161 break;
7162 case DW_TAG_compile_unit:
95554aad 7163 case DW_TAG_partial_unit:
e7c27a73 7164 read_file_scope (die, cu);
c906108c 7165 break;
348e048f
DE
7166 case DW_TAG_type_unit:
7167 read_type_unit_scope (die, cu);
7168 break;
c906108c 7169 case DW_TAG_subprogram:
c906108c 7170 case DW_TAG_inlined_subroutine:
edb3359d 7171 read_func_scope (die, cu);
c906108c
SS
7172 break;
7173 case DW_TAG_lexical_block:
14898363
L
7174 case DW_TAG_try_block:
7175 case DW_TAG_catch_block:
e7c27a73 7176 read_lexical_block_scope (die, cu);
c906108c 7177 break;
96408a79
SA
7178 case DW_TAG_GNU_call_site:
7179 read_call_site_scope (die, cu);
7180 break;
c906108c 7181 case DW_TAG_class_type:
680b30c7 7182 case DW_TAG_interface_type:
c906108c
SS
7183 case DW_TAG_structure_type:
7184 case DW_TAG_union_type:
134d01f1 7185 process_structure_scope (die, cu);
c906108c
SS
7186 break;
7187 case DW_TAG_enumeration_type:
134d01f1 7188 process_enumeration_scope (die, cu);
c906108c 7189 break;
134d01f1 7190
f792889a
DJ
7191 /* These dies have a type, but processing them does not create
7192 a symbol or recurse to process the children. Therefore we can
7193 read them on-demand through read_type_die. */
c906108c 7194 case DW_TAG_subroutine_type:
72019c9c 7195 case DW_TAG_set_type:
c906108c 7196 case DW_TAG_array_type:
c906108c 7197 case DW_TAG_pointer_type:
c906108c 7198 case DW_TAG_ptr_to_member_type:
c906108c 7199 case DW_TAG_reference_type:
c906108c 7200 case DW_TAG_string_type:
c906108c 7201 break;
134d01f1 7202
c906108c 7203 case DW_TAG_base_type:
a02abb62 7204 case DW_TAG_subrange_type:
cb249c71 7205 case DW_TAG_typedef:
134d01f1
DJ
7206 /* Add a typedef symbol for the type definition, if it has a
7207 DW_AT_name. */
f792889a 7208 new_symbol (die, read_type_die (die, cu), cu);
a02abb62 7209 break;
c906108c 7210 case DW_TAG_common_block:
e7c27a73 7211 read_common_block (die, cu);
c906108c
SS
7212 break;
7213 case DW_TAG_common_inclusion:
7214 break;
d9fa45fe 7215 case DW_TAG_namespace:
4d4ec4e5 7216 cu->processing_has_namespace_info = 1;
e7c27a73 7217 read_namespace (die, cu);
d9fa45fe 7218 break;
5d7cb8df 7219 case DW_TAG_module:
4d4ec4e5 7220 cu->processing_has_namespace_info = 1;
5d7cb8df
JK
7221 read_module (die, cu);
7222 break;
d9fa45fe
DC
7223 case DW_TAG_imported_declaration:
7224 case DW_TAG_imported_module:
4d4ec4e5 7225 cu->processing_has_namespace_info = 1;
27aa8d6a
SW
7226 if (die->child != NULL && (die->tag == DW_TAG_imported_declaration
7227 || cu->language != language_fortran))
7228 complaint (&symfile_complaints, _("Tag '%s' has unexpected children"),
7229 dwarf_tag_name (die->tag));
7230 read_import_statement (die, cu);
d9fa45fe 7231 break;
95554aad
TT
7232
7233 case DW_TAG_imported_unit:
7234 process_imported_unit_die (die, cu);
7235 break;
7236
c906108c 7237 default:
e7c27a73 7238 new_symbol (die, NULL, cu);
c906108c
SS
7239 break;
7240 }
7241}
7242
94af9270
KS
7243/* A helper function for dwarf2_compute_name which determines whether DIE
7244 needs to have the name of the scope prepended to the name listed in the
7245 die. */
7246
7247static int
7248die_needs_namespace (struct die_info *die, struct dwarf2_cu *cu)
7249{
1c809c68
TT
7250 struct attribute *attr;
7251
94af9270
KS
7252 switch (die->tag)
7253 {
7254 case DW_TAG_namespace:
7255 case DW_TAG_typedef:
7256 case DW_TAG_class_type:
7257 case DW_TAG_interface_type:
7258 case DW_TAG_structure_type:
7259 case DW_TAG_union_type:
7260 case DW_TAG_enumeration_type:
7261 case DW_TAG_enumerator:
7262 case DW_TAG_subprogram:
7263 case DW_TAG_member:
7264 return 1;
7265
7266 case DW_TAG_variable:
c2b0a229 7267 case DW_TAG_constant:
94af9270
KS
7268 /* We only need to prefix "globally" visible variables. These include
7269 any variable marked with DW_AT_external or any variable that
7270 lives in a namespace. [Variables in anonymous namespaces
7271 require prefixing, but they are not DW_AT_external.] */
7272
7273 if (dwarf2_attr (die, DW_AT_specification, cu))
7274 {
7275 struct dwarf2_cu *spec_cu = cu;
9a619af0 7276
94af9270
KS
7277 return die_needs_namespace (die_specification (die, &spec_cu),
7278 spec_cu);
7279 }
7280
1c809c68 7281 attr = dwarf2_attr (die, DW_AT_external, cu);
f55ee35c
JK
7282 if (attr == NULL && die->parent->tag != DW_TAG_namespace
7283 && die->parent->tag != DW_TAG_module)
1c809c68
TT
7284 return 0;
7285 /* A variable in a lexical block of some kind does not need a
7286 namespace, even though in C++ such variables may be external
7287 and have a mangled name. */
7288 if (die->parent->tag == DW_TAG_lexical_block
7289 || die->parent->tag == DW_TAG_try_block
1054b214
TT
7290 || die->parent->tag == DW_TAG_catch_block
7291 || die->parent->tag == DW_TAG_subprogram)
1c809c68
TT
7292 return 0;
7293 return 1;
94af9270
KS
7294
7295 default:
7296 return 0;
7297 }
7298}
7299
98bfdba5
PA
7300/* Retrieve the last character from a mem_file. */
7301
7302static void
7303do_ui_file_peek_last (void *object, const char *buffer, long length)
7304{
7305 char *last_char_p = (char *) object;
7306
7307 if (length > 0)
7308 *last_char_p = buffer[length - 1];
7309}
7310
94af9270 7311/* Compute the fully qualified name of DIE in CU. If PHYSNAME is nonzero,
a766d390
DE
7312 compute the physname for the object, which include a method's:
7313 - formal parameters (C++/Java),
7314 - receiver type (Go),
7315 - return type (Java).
7316
7317 The term "physname" is a bit confusing.
7318 For C++, for example, it is the demangled name.
7319 For Go, for example, it's the mangled name.
94af9270 7320
af6b7be1
JB
7321 For Ada, return the DIE's linkage name rather than the fully qualified
7322 name. PHYSNAME is ignored..
7323
94af9270
KS
7324 The result is allocated on the objfile_obstack and canonicalized. */
7325
7326static const char *
15d034d0
TT
7327dwarf2_compute_name (const char *name,
7328 struct die_info *die, struct dwarf2_cu *cu,
94af9270
KS
7329 int physname)
7330{
bb5ed363
DE
7331 struct objfile *objfile = cu->objfile;
7332
94af9270
KS
7333 if (name == NULL)
7334 name = dwarf2_name (die, cu);
7335
f55ee35c
JK
7336 /* For Fortran GDB prefers DW_AT_*linkage_name if present but otherwise
7337 compute it by typename_concat inside GDB. */
7338 if (cu->language == language_ada
7339 || (cu->language == language_fortran && physname))
7340 {
7341 /* For Ada unit, we prefer the linkage name over the name, as
7342 the former contains the exported name, which the user expects
7343 to be able to reference. Ideally, we want the user to be able
7344 to reference this entity using either natural or linkage name,
7345 but we haven't started looking at this enhancement yet. */
7346 struct attribute *attr;
7347
7348 attr = dwarf2_attr (die, DW_AT_linkage_name, cu);
7349 if (attr == NULL)
7350 attr = dwarf2_attr (die, DW_AT_MIPS_linkage_name, cu);
7351 if (attr && DW_STRING (attr))
7352 return DW_STRING (attr);
7353 }
7354
94af9270
KS
7355 /* These are the only languages we know how to qualify names in. */
7356 if (name != NULL
f55ee35c
JK
7357 && (cu->language == language_cplus || cu->language == language_java
7358 || cu->language == language_fortran))
94af9270
KS
7359 {
7360 if (die_needs_namespace (die, cu))
7361 {
7362 long length;
0d5cff50 7363 const char *prefix;
94af9270
KS
7364 struct ui_file *buf;
7365
7366 prefix = determine_prefix (die, cu);
7367 buf = mem_fileopen ();
7368 if (*prefix != '\0')
7369 {
f55ee35c
JK
7370 char *prefixed_name = typename_concat (NULL, prefix, name,
7371 physname, cu);
9a619af0 7372
94af9270
KS
7373 fputs_unfiltered (prefixed_name, buf);
7374 xfree (prefixed_name);
7375 }
7376 else
62d5b8da 7377 fputs_unfiltered (name, buf);
94af9270 7378
98bfdba5
PA
7379 /* Template parameters may be specified in the DIE's DW_AT_name, or
7380 as children with DW_TAG_template_type_param or
7381 DW_TAG_value_type_param. If the latter, add them to the name
7382 here. If the name already has template parameters, then
7383 skip this step; some versions of GCC emit both, and
7384 it is more efficient to use the pre-computed name.
7385
7386 Something to keep in mind about this process: it is very
7387 unlikely, or in some cases downright impossible, to produce
7388 something that will match the mangled name of a function.
7389 If the definition of the function has the same debug info,
7390 we should be able to match up with it anyway. But fallbacks
7391 using the minimal symbol, for instance to find a method
7392 implemented in a stripped copy of libstdc++, will not work.
7393 If we do not have debug info for the definition, we will have to
7394 match them up some other way.
7395
7396 When we do name matching there is a related problem with function
7397 templates; two instantiated function templates are allowed to
7398 differ only by their return types, which we do not add here. */
7399
7400 if (cu->language == language_cplus && strchr (name, '<') == NULL)
7401 {
7402 struct attribute *attr;
7403 struct die_info *child;
7404 int first = 1;
7405
7406 die->building_fullname = 1;
7407
7408 for (child = die->child; child != NULL; child = child->sibling)
7409 {
7410 struct type *type;
12df843f 7411 LONGEST value;
98bfdba5
PA
7412 gdb_byte *bytes;
7413 struct dwarf2_locexpr_baton *baton;
7414 struct value *v;
7415
7416 if (child->tag != DW_TAG_template_type_param
7417 && child->tag != DW_TAG_template_value_param)
7418 continue;
7419
7420 if (first)
7421 {
7422 fputs_unfiltered ("<", buf);
7423 first = 0;
7424 }
7425 else
7426 fputs_unfiltered (", ", buf);
7427
7428 attr = dwarf2_attr (child, DW_AT_type, cu);
7429 if (attr == NULL)
7430 {
7431 complaint (&symfile_complaints,
7432 _("template parameter missing DW_AT_type"));
7433 fputs_unfiltered ("UNKNOWN_TYPE", buf);
7434 continue;
7435 }
7436 type = die_type (child, cu);
7437
7438 if (child->tag == DW_TAG_template_type_param)
7439 {
79d43c61 7440 c_print_type (type, "", buf, -1, 0, &type_print_raw_options);
98bfdba5
PA
7441 continue;
7442 }
7443
7444 attr = dwarf2_attr (child, DW_AT_const_value, cu);
7445 if (attr == NULL)
7446 {
7447 complaint (&symfile_complaints,
3e43a32a
MS
7448 _("template parameter missing "
7449 "DW_AT_const_value"));
98bfdba5
PA
7450 fputs_unfiltered ("UNKNOWN_VALUE", buf);
7451 continue;
7452 }
7453
7454 dwarf2_const_value_attr (attr, type, name,
7455 &cu->comp_unit_obstack, cu,
7456 &value, &bytes, &baton);
7457
7458 if (TYPE_NOSIGN (type))
7459 /* GDB prints characters as NUMBER 'CHAR'. If that's
7460 changed, this can use value_print instead. */
7461 c_printchar (value, type, buf);
7462 else
7463 {
7464 struct value_print_options opts;
7465
7466 if (baton != NULL)
7467 v = dwarf2_evaluate_loc_desc (type, NULL,
7468 baton->data,
7469 baton->size,
7470 baton->per_cu);
7471 else if (bytes != NULL)
7472 {
7473 v = allocate_value (type);
7474 memcpy (value_contents_writeable (v), bytes,
7475 TYPE_LENGTH (type));
7476 }
7477 else
7478 v = value_from_longest (type, value);
7479
3e43a32a
MS
7480 /* Specify decimal so that we do not depend on
7481 the radix. */
98bfdba5
PA
7482 get_formatted_print_options (&opts, 'd');
7483 opts.raw = 1;
7484 value_print (v, buf, &opts);
7485 release_value (v);
7486 value_free (v);
7487 }
7488 }
7489
7490 die->building_fullname = 0;
7491
7492 if (!first)
7493 {
7494 /* Close the argument list, with a space if necessary
7495 (nested templates). */
7496 char last_char = '\0';
7497 ui_file_put (buf, do_ui_file_peek_last, &last_char);
7498 if (last_char == '>')
7499 fputs_unfiltered (" >", buf);
7500 else
7501 fputs_unfiltered (">", buf);
7502 }
7503 }
7504
94af9270
KS
7505 /* For Java and C++ methods, append formal parameter type
7506 information, if PHYSNAME. */
6e70227d 7507
94af9270
KS
7508 if (physname && die->tag == DW_TAG_subprogram
7509 && (cu->language == language_cplus
7510 || cu->language == language_java))
7511 {
7512 struct type *type = read_type_die (die, cu);
7513
79d43c61
TT
7514 c_type_print_args (type, buf, 1, cu->language,
7515 &type_print_raw_options);
94af9270
KS
7516
7517 if (cu->language == language_java)
7518 {
7519 /* For java, we must append the return type to method
0963b4bd 7520 names. */
94af9270
KS
7521 if (die->tag == DW_TAG_subprogram)
7522 java_print_type (TYPE_TARGET_TYPE (type), "", buf,
79d43c61 7523 0, 0, &type_print_raw_options);
94af9270
KS
7524 }
7525 else if (cu->language == language_cplus)
7526 {
60430eff
DJ
7527 /* Assume that an artificial first parameter is
7528 "this", but do not crash if it is not. RealView
7529 marks unnamed (and thus unused) parameters as
7530 artificial; there is no way to differentiate
7531 the two cases. */
94af9270
KS
7532 if (TYPE_NFIELDS (type) > 0
7533 && TYPE_FIELD_ARTIFICIAL (type, 0)
60430eff 7534 && TYPE_CODE (TYPE_FIELD_TYPE (type, 0)) == TYPE_CODE_PTR
3e43a32a
MS
7535 && TYPE_CONST (TYPE_TARGET_TYPE (TYPE_FIELD_TYPE (type,
7536 0))))
94af9270
KS
7537 fputs_unfiltered (" const", buf);
7538 }
7539 }
7540
bb5ed363 7541 name = ui_file_obsavestring (buf, &objfile->objfile_obstack,
94af9270
KS
7542 &length);
7543 ui_file_delete (buf);
7544
7545 if (cu->language == language_cplus)
7546 {
15d034d0 7547 const char *cname
94af9270 7548 = dwarf2_canonicalize_name (name, cu,
bb5ed363 7549 &objfile->objfile_obstack);
9a619af0 7550
94af9270
KS
7551 if (cname != NULL)
7552 name = cname;
7553 }
7554 }
7555 }
7556
7557 return name;
7558}
7559
0114d602
DJ
7560/* Return the fully qualified name of DIE, based on its DW_AT_name.
7561 If scope qualifiers are appropriate they will be added. The result
7562 will be allocated on the objfile_obstack, or NULL if the DIE does
94af9270
KS
7563 not have a name. NAME may either be from a previous call to
7564 dwarf2_name or NULL.
7565
0963b4bd 7566 The output string will be canonicalized (if C++/Java). */
0114d602
DJ
7567
7568static const char *
15d034d0 7569dwarf2_full_name (const char *name, struct die_info *die, struct dwarf2_cu *cu)
0114d602 7570{
94af9270
KS
7571 return dwarf2_compute_name (name, die, cu, 0);
7572}
0114d602 7573
94af9270
KS
7574/* Construct a physname for the given DIE in CU. NAME may either be
7575 from a previous call to dwarf2_name or NULL. The result will be
7576 allocated on the objfile_objstack or NULL if the DIE does not have a
7577 name.
0114d602 7578
94af9270 7579 The output string will be canonicalized (if C++/Java). */
0114d602 7580
94af9270 7581static const char *
15d034d0 7582dwarf2_physname (const char *name, struct die_info *die, struct dwarf2_cu *cu)
94af9270 7583{
bb5ed363 7584 struct objfile *objfile = cu->objfile;
900e11f9
JK
7585 struct attribute *attr;
7586 const char *retval, *mangled = NULL, *canon = NULL;
7587 struct cleanup *back_to;
7588 int need_copy = 1;
7589
7590 /* In this case dwarf2_compute_name is just a shortcut not building anything
7591 on its own. */
7592 if (!die_needs_namespace (die, cu))
7593 return dwarf2_compute_name (name, die, cu, 1);
7594
7595 back_to = make_cleanup (null_cleanup, NULL);
7596
7597 attr = dwarf2_attr (die, DW_AT_linkage_name, cu);
7598 if (!attr)
7599 attr = dwarf2_attr (die, DW_AT_MIPS_linkage_name, cu);
7600
7601 /* DW_AT_linkage_name is missing in some cases - depend on what GDB
7602 has computed. */
7603 if (attr && DW_STRING (attr))
7604 {
7605 char *demangled;
7606
7607 mangled = DW_STRING (attr);
7608
7609 /* Use DMGL_RET_DROP for C++ template functions to suppress their return
7610 type. It is easier for GDB users to search for such functions as
7611 `name(params)' than `long name(params)'. In such case the minimal
7612 symbol names do not match the full symbol names but for template
7613 functions there is never a need to look up their definition from their
7614 declaration so the only disadvantage remains the minimal symbol
7615 variant `long name(params)' does not have the proper inferior type.
7616 */
7617
a766d390
DE
7618 if (cu->language == language_go)
7619 {
7620 /* This is a lie, but we already lie to the caller new_symbol_full.
7621 new_symbol_full assumes we return the mangled name.
7622 This just undoes that lie until things are cleaned up. */
7623 demangled = NULL;
7624 }
7625 else
7626 {
7627 demangled = cplus_demangle (mangled,
7628 (DMGL_PARAMS | DMGL_ANSI
7629 | (cu->language == language_java
7630 ? DMGL_JAVA | DMGL_RET_POSTFIX
7631 : DMGL_RET_DROP)));
7632 }
900e11f9
JK
7633 if (demangled)
7634 {
7635 make_cleanup (xfree, demangled);
7636 canon = demangled;
7637 }
7638 else
7639 {
7640 canon = mangled;
7641 need_copy = 0;
7642 }
7643 }
7644
7645 if (canon == NULL || check_physname)
7646 {
7647 const char *physname = dwarf2_compute_name (name, die, cu, 1);
7648
7649 if (canon != NULL && strcmp (physname, canon) != 0)
7650 {
7651 /* It may not mean a bug in GDB. The compiler could also
7652 compute DW_AT_linkage_name incorrectly. But in such case
7653 GDB would need to be bug-to-bug compatible. */
7654
7655 complaint (&symfile_complaints,
7656 _("Computed physname <%s> does not match demangled <%s> "
7657 "(from linkage <%s>) - DIE at 0x%x [in module %s]"),
b64f50a1 7658 physname, canon, mangled, die->offset.sect_off, objfile->name);
900e11f9
JK
7659
7660 /* Prefer DW_AT_linkage_name (in the CANON form) - when it
7661 is available here - over computed PHYSNAME. It is safer
7662 against both buggy GDB and buggy compilers. */
7663
7664 retval = canon;
7665 }
7666 else
7667 {
7668 retval = physname;
7669 need_copy = 0;
7670 }
7671 }
7672 else
7673 retval = canon;
7674
7675 if (need_copy)
10f0c4bb 7676 retval = obstack_copy0 (&objfile->objfile_obstack, retval, strlen (retval));
900e11f9
JK
7677
7678 do_cleanups (back_to);
7679 return retval;
0114d602
DJ
7680}
7681
27aa8d6a
SW
7682/* Read the import statement specified by the given die and record it. */
7683
7684static void
7685read_import_statement (struct die_info *die, struct dwarf2_cu *cu)
7686{
bb5ed363 7687 struct objfile *objfile = cu->objfile;
27aa8d6a 7688 struct attribute *import_attr;
32019081 7689 struct die_info *imported_die, *child_die;
de4affc9 7690 struct dwarf2_cu *imported_cu;
27aa8d6a 7691 const char *imported_name;
794684b6 7692 const char *imported_name_prefix;
13387711
SW
7693 const char *canonical_name;
7694 const char *import_alias;
7695 const char *imported_declaration = NULL;
794684b6 7696 const char *import_prefix;
32019081
JK
7697 VEC (const_char_ptr) *excludes = NULL;
7698 struct cleanup *cleanups;
13387711 7699
27aa8d6a
SW
7700 import_attr = dwarf2_attr (die, DW_AT_import, cu);
7701 if (import_attr == NULL)
7702 {
7703 complaint (&symfile_complaints, _("Tag '%s' has no DW_AT_import"),
7704 dwarf_tag_name (die->tag));
7705 return;
7706 }
7707
de4affc9
CC
7708 imported_cu = cu;
7709 imported_die = follow_die_ref_or_sig (die, import_attr, &imported_cu);
7710 imported_name = dwarf2_name (imported_die, imported_cu);
27aa8d6a
SW
7711 if (imported_name == NULL)
7712 {
7713 /* GCC bug: https://bugzilla.redhat.com/show_bug.cgi?id=506524
7714
7715 The import in the following code:
7716 namespace A
7717 {
7718 typedef int B;
7719 }
7720
7721 int main ()
7722 {
7723 using A::B;
7724 B b;
7725 return b;
7726 }
7727
7728 ...
7729 <2><51>: Abbrev Number: 3 (DW_TAG_imported_declaration)
7730 <52> DW_AT_decl_file : 1
7731 <53> DW_AT_decl_line : 6
7732 <54> DW_AT_import : <0x75>
7733 <2><58>: Abbrev Number: 4 (DW_TAG_typedef)
7734 <59> DW_AT_name : B
7735 <5b> DW_AT_decl_file : 1
7736 <5c> DW_AT_decl_line : 2
7737 <5d> DW_AT_type : <0x6e>
7738 ...
7739 <1><75>: Abbrev Number: 7 (DW_TAG_base_type)
7740 <76> DW_AT_byte_size : 4
7741 <77> DW_AT_encoding : 5 (signed)
7742
7743 imports the wrong die ( 0x75 instead of 0x58 ).
7744 This case will be ignored until the gcc bug is fixed. */
7745 return;
7746 }
7747
82856980
SW
7748 /* Figure out the local name after import. */
7749 import_alias = dwarf2_name (die, cu);
27aa8d6a 7750
794684b6
SW
7751 /* Figure out where the statement is being imported to. */
7752 import_prefix = determine_prefix (die, cu);
7753
7754 /* Figure out what the scope of the imported die is and prepend it
7755 to the name of the imported die. */
de4affc9 7756 imported_name_prefix = determine_prefix (imported_die, imported_cu);
794684b6 7757
f55ee35c
JK
7758 if (imported_die->tag != DW_TAG_namespace
7759 && imported_die->tag != DW_TAG_module)
794684b6 7760 {
13387711
SW
7761 imported_declaration = imported_name;
7762 canonical_name = imported_name_prefix;
794684b6 7763 }
13387711 7764 else if (strlen (imported_name_prefix) > 0)
12aaed36
TT
7765 canonical_name = obconcat (&objfile->objfile_obstack,
7766 imported_name_prefix, "::", imported_name,
7767 (char *) NULL);
13387711
SW
7768 else
7769 canonical_name = imported_name;
794684b6 7770
32019081
JK
7771 cleanups = make_cleanup (VEC_cleanup (const_char_ptr), &excludes);
7772
7773 if (die->tag == DW_TAG_imported_module && cu->language == language_fortran)
7774 for (child_die = die->child; child_die && child_die->tag;
7775 child_die = sibling_die (child_die))
7776 {
7777 /* DWARF-4: A Fortran use statement with a “rename list” may be
7778 represented by an imported module entry with an import attribute
7779 referring to the module and owned entries corresponding to those
7780 entities that are renamed as part of being imported. */
7781
7782 if (child_die->tag != DW_TAG_imported_declaration)
7783 {
7784 complaint (&symfile_complaints,
7785 _("child DW_TAG_imported_declaration expected "
7786 "- DIE at 0x%x [in module %s]"),
b64f50a1 7787 child_die->offset.sect_off, objfile->name);
32019081
JK
7788 continue;
7789 }
7790
7791 import_attr = dwarf2_attr (child_die, DW_AT_import, cu);
7792 if (import_attr == NULL)
7793 {
7794 complaint (&symfile_complaints, _("Tag '%s' has no DW_AT_import"),
7795 dwarf_tag_name (child_die->tag));
7796 continue;
7797 }
7798
7799 imported_cu = cu;
7800 imported_die = follow_die_ref_or_sig (child_die, import_attr,
7801 &imported_cu);
7802 imported_name = dwarf2_name (imported_die, imported_cu);
7803 if (imported_name == NULL)
7804 {
7805 complaint (&symfile_complaints,
7806 _("child DW_TAG_imported_declaration has unknown "
7807 "imported name - DIE at 0x%x [in module %s]"),
b64f50a1 7808 child_die->offset.sect_off, objfile->name);
32019081
JK
7809 continue;
7810 }
7811
7812 VEC_safe_push (const_char_ptr, excludes, imported_name);
7813
7814 process_die (child_die, cu);
7815 }
7816
c0cc3a76
SW
7817 cp_add_using_directive (import_prefix,
7818 canonical_name,
7819 import_alias,
13387711 7820 imported_declaration,
32019081 7821 excludes,
12aaed36 7822 0,
bb5ed363 7823 &objfile->objfile_obstack);
32019081
JK
7824
7825 do_cleanups (cleanups);
27aa8d6a
SW
7826}
7827
f4dc4d17 7828/* Cleanup function for handle_DW_AT_stmt_list. */
ae2de4f8 7829
cb1df416
DJ
7830static void
7831free_cu_line_header (void *arg)
7832{
7833 struct dwarf2_cu *cu = arg;
7834
7835 free_line_header (cu->line_header);
7836 cu->line_header = NULL;
7837}
7838
1b80a9fa
JK
7839/* Check for possibly missing DW_AT_comp_dir with relative .debug_line
7840 directory paths. GCC SVN r127613 (new option -fdebug-prefix-map) fixed
7841 this, it was first present in GCC release 4.3.0. */
7842
7843static int
7844producer_is_gcc_lt_4_3 (struct dwarf2_cu *cu)
7845{
7846 if (!cu->checked_producer)
7847 check_producer (cu);
7848
7849 return cu->producer_is_gcc_lt_4_3;
7850}
7851
9291a0cd
TT
7852static void
7853find_file_and_directory (struct die_info *die, struct dwarf2_cu *cu,
15d034d0 7854 const char **name, const char **comp_dir)
9291a0cd
TT
7855{
7856 struct attribute *attr;
7857
7858 *name = NULL;
7859 *comp_dir = NULL;
7860
7861 /* Find the filename. Do not use dwarf2_name here, since the filename
7862 is not a source language identifier. */
7863 attr = dwarf2_attr (die, DW_AT_name, cu);
7864 if (attr)
7865 {
7866 *name = DW_STRING (attr);
7867 }
7868
7869 attr = dwarf2_attr (die, DW_AT_comp_dir, cu);
7870 if (attr)
7871 *comp_dir = DW_STRING (attr);
1b80a9fa
JK
7872 else if (producer_is_gcc_lt_4_3 (cu) && *name != NULL
7873 && IS_ABSOLUTE_PATH (*name))
9291a0cd 7874 {
15d034d0
TT
7875 char *d = ldirname (*name);
7876
7877 *comp_dir = d;
7878 if (d != NULL)
7879 make_cleanup (xfree, d);
9291a0cd
TT
7880 }
7881 if (*comp_dir != NULL)
7882 {
7883 /* Irix 6.2 native cc prepends <machine>.: to the compilation
7884 directory, get rid of it. */
7885 char *cp = strchr (*comp_dir, ':');
7886
7887 if (cp && cp != *comp_dir && cp[-1] == '.' && cp[1] == '/')
7888 *comp_dir = cp + 1;
7889 }
7890
7891 if (*name == NULL)
7892 *name = "<unknown>";
7893}
7894
f4dc4d17
DE
7895/* Handle DW_AT_stmt_list for a compilation unit.
7896 DIE is the DW_TAG_compile_unit die for CU.
f3f5162e
DE
7897 COMP_DIR is the compilation directory.
7898 WANT_LINE_INFO is non-zero if the pc/line-number mapping is needed. */
2ab95328
TT
7899
7900static void
7901handle_DW_AT_stmt_list (struct die_info *die, struct dwarf2_cu *cu,
f4dc4d17 7902 const char *comp_dir)
2ab95328
TT
7903{
7904 struct attribute *attr;
2ab95328 7905
f4dc4d17
DE
7906 gdb_assert (! cu->per_cu->is_debug_types);
7907
2ab95328
TT
7908 attr = dwarf2_attr (die, DW_AT_stmt_list, cu);
7909 if (attr)
7910 {
7911 unsigned int line_offset = DW_UNSND (attr);
7912 struct line_header *line_header
3019eac3 7913 = dwarf_decode_line_header (line_offset, cu);
2ab95328
TT
7914
7915 if (line_header)
dee91e82
DE
7916 {
7917 cu->line_header = line_header;
7918 make_cleanup (free_cu_line_header, cu);
f4dc4d17 7919 dwarf_decode_lines (line_header, comp_dir, cu, NULL, 1);
dee91e82 7920 }
2ab95328
TT
7921 }
7922}
7923
95554aad 7924/* Process DW_TAG_compile_unit or DW_TAG_partial_unit. */
ae2de4f8 7925
c906108c 7926static void
e7c27a73 7927read_file_scope (struct die_info *die, struct dwarf2_cu *cu)
c906108c 7928{
dee91e82 7929 struct objfile *objfile = dwarf2_per_objfile->objfile;
debd256d 7930 struct cleanup *back_to = make_cleanup (null_cleanup, 0);
2acceee2 7931 CORE_ADDR lowpc = ((CORE_ADDR) -1);
c906108c
SS
7932 CORE_ADDR highpc = ((CORE_ADDR) 0);
7933 struct attribute *attr;
15d034d0
TT
7934 const char *name = NULL;
7935 const char *comp_dir = NULL;
c906108c
SS
7936 struct die_info *child_die;
7937 bfd *abfd = objfile->obfd;
e142c38c 7938 CORE_ADDR baseaddr;
6e70227d 7939
e142c38c 7940 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
c906108c 7941
fae299cd 7942 get_scope_pc_bounds (die, &lowpc, &highpc, cu);
c906108c
SS
7943
7944 /* If we didn't find a lowpc, set it to highpc to avoid complaints
7945 from finish_block. */
2acceee2 7946 if (lowpc == ((CORE_ADDR) -1))
c906108c
SS
7947 lowpc = highpc;
7948 lowpc += baseaddr;
7949 highpc += baseaddr;
7950
9291a0cd 7951 find_file_and_directory (die, cu, &name, &comp_dir);
e1024ff1 7952
95554aad 7953 prepare_one_comp_unit (cu, die, cu->language);
303b6f5d 7954
f4b8a18d
KW
7955 /* The XLCL doesn't generate DW_LANG_OpenCL because this attribute is not
7956 standardised yet. As a workaround for the language detection we fall
7957 back to the DW_AT_producer string. */
7958 if (cu->producer && strstr (cu->producer, "IBM XL C for OpenCL") != NULL)
7959 cu->language = language_opencl;
7960
3019eac3
DE
7961 /* Similar hack for Go. */
7962 if (cu->producer && strstr (cu->producer, "GNU Go ") != NULL)
7963 set_cu_language (DW_LANG_Go, cu);
7964
f4dc4d17 7965 dwarf2_start_symtab (cu, name, comp_dir, lowpc);
3019eac3
DE
7966
7967 /* Decode line number information if present. We do this before
7968 processing child DIEs, so that the line header table is available
7969 for DW_AT_decl_file. */
f4dc4d17 7970 handle_DW_AT_stmt_list (die, cu, comp_dir);
3019eac3
DE
7971
7972 /* Process all dies in compilation unit. */
7973 if (die->child != NULL)
7974 {
7975 child_die = die->child;
7976 while (child_die && child_die->tag)
7977 {
7978 process_die (child_die, cu);
7979 child_die = sibling_die (child_die);
7980 }
7981 }
7982
7983 /* Decode macro information, if present. Dwarf 2 macro information
7984 refers to information in the line number info statement program
7985 header, so we can only read it if we've read the header
7986 successfully. */
7987 attr = dwarf2_attr (die, DW_AT_GNU_macros, cu);
7988 if (attr && cu->line_header)
7989 {
7990 if (dwarf2_attr (die, DW_AT_macro_info, cu))
7991 complaint (&symfile_complaints,
7992 _("CU refers to both DW_AT_GNU_macros and DW_AT_macro_info"));
7993
09262596 7994 dwarf_decode_macros (cu, DW_UNSND (attr), comp_dir, 1);
3019eac3
DE
7995 }
7996 else
7997 {
7998 attr = dwarf2_attr (die, DW_AT_macro_info, cu);
7999 if (attr && cu->line_header)
8000 {
8001 unsigned int macro_offset = DW_UNSND (attr);
8002
09262596 8003 dwarf_decode_macros (cu, macro_offset, comp_dir, 0);
3019eac3
DE
8004 }
8005 }
8006
8007 do_cleanups (back_to);
8008}
8009
f4dc4d17
DE
8010/* TU version of handle_DW_AT_stmt_list for read_type_unit_scope.
8011 Create the set of symtabs used by this TU, or if this TU is sharing
8012 symtabs with another TU and the symtabs have already been created
8013 then restore those symtabs in the line header.
8014 We don't need the pc/line-number mapping for type units. */
3019eac3
DE
8015
8016static void
f4dc4d17 8017setup_type_unit_groups (struct die_info *die, struct dwarf2_cu *cu)
3019eac3 8018{
f4dc4d17
DE
8019 struct objfile *objfile = dwarf2_per_objfile->objfile;
8020 struct dwarf2_per_cu_data *per_cu = cu->per_cu;
8021 struct type_unit_group *tu_group;
8022 int first_time;
8023 struct line_header *lh;
3019eac3 8024 struct attribute *attr;
f4dc4d17 8025 unsigned int i, line_offset;
3019eac3 8026
f4dc4d17 8027 gdb_assert (per_cu->is_debug_types);
3019eac3 8028
f4dc4d17 8029 attr = dwarf2_attr (die, DW_AT_stmt_list, cu);
3019eac3 8030
f4dc4d17
DE
8031 /* If we're using .gdb_index (includes -readnow) then
8032 per_cu->s.type_unit_group may not have been set up yet. */
796a7ff8
DE
8033 if (per_cu->type_unit_group == NULL)
8034 per_cu->type_unit_group = get_type_unit_group (cu, attr);
8035 tu_group = per_cu->type_unit_group;
f4dc4d17
DE
8036
8037 /* If we've already processed this stmt_list there's no real need to
8038 do it again, we could fake it and just recreate the part we need
8039 (file name,index -> symtab mapping). If data shows this optimization
8040 is useful we can do it then. */
8041 first_time = tu_group->primary_symtab == NULL;
8042
8043 /* We have to handle the case of both a missing DW_AT_stmt_list or bad
8044 debug info. */
8045 lh = NULL;
8046 if (attr != NULL)
3019eac3 8047 {
f4dc4d17
DE
8048 line_offset = DW_UNSND (attr);
8049 lh = dwarf_decode_line_header (line_offset, cu);
8050 }
8051 if (lh == NULL)
8052 {
8053 if (first_time)
8054 dwarf2_start_symtab (cu, "", NULL, 0);
8055 else
8056 {
8057 gdb_assert (tu_group->symtabs == NULL);
8058 restart_symtab (0);
8059 }
8060 /* Note: The primary symtab will get allocated at the end. */
8061 return;
3019eac3
DE
8062 }
8063
f4dc4d17
DE
8064 cu->line_header = lh;
8065 make_cleanup (free_cu_line_header, cu);
3019eac3 8066
f4dc4d17
DE
8067 if (first_time)
8068 {
8069 dwarf2_start_symtab (cu, "", NULL, 0);
3019eac3 8070
f4dc4d17
DE
8071 tu_group->num_symtabs = lh->num_file_names;
8072 tu_group->symtabs = XNEWVEC (struct symtab *, lh->num_file_names);
3019eac3 8073
f4dc4d17
DE
8074 for (i = 0; i < lh->num_file_names; ++i)
8075 {
8076 char *dir = NULL;
8077 struct file_entry *fe = &lh->file_names[i];
3019eac3 8078
f4dc4d17
DE
8079 if (fe->dir_index)
8080 dir = lh->include_dirs[fe->dir_index - 1];
8081 dwarf2_start_subfile (fe->name, dir, NULL);
3019eac3 8082
f4dc4d17
DE
8083 /* Note: We don't have to watch for the main subfile here, type units
8084 don't have DW_AT_name. */
3019eac3 8085
f4dc4d17
DE
8086 if (current_subfile->symtab == NULL)
8087 {
8088 /* NOTE: start_subfile will recognize when it's been passed
8089 a file it has already seen. So we can't assume there's a
8090 simple mapping from lh->file_names to subfiles,
8091 lh->file_names may contain dups. */
8092 current_subfile->symtab = allocate_symtab (current_subfile->name,
8093 objfile);
8094 }
8095
8096 fe->symtab = current_subfile->symtab;
8097 tu_group->symtabs[i] = fe->symtab;
8098 }
8099 }
8100 else
3019eac3 8101 {
f4dc4d17
DE
8102 restart_symtab (0);
8103
8104 for (i = 0; i < lh->num_file_names; ++i)
8105 {
8106 struct file_entry *fe = &lh->file_names[i];
8107
8108 fe->symtab = tu_group->symtabs[i];
8109 }
3019eac3
DE
8110 }
8111
f4dc4d17
DE
8112 /* The main symtab is allocated last. Type units don't have DW_AT_name
8113 so they don't have a "real" (so to speak) symtab anyway.
8114 There is later code that will assign the main symtab to all symbols
8115 that don't have one. We need to handle the case of a symbol with a
8116 missing symtab (DW_AT_decl_file) anyway. */
8117}
3019eac3 8118
f4dc4d17
DE
8119/* Process DW_TAG_type_unit.
8120 For TUs we want to skip the first top level sibling if it's not the
8121 actual type being defined by this TU. In this case the first top
8122 level sibling is there to provide context only. */
3019eac3 8123
f4dc4d17
DE
8124static void
8125read_type_unit_scope (struct die_info *die, struct dwarf2_cu *cu)
8126{
8127 struct die_info *child_die;
3019eac3 8128
f4dc4d17
DE
8129 prepare_one_comp_unit (cu, die, language_minimal);
8130
8131 /* Initialize (or reinitialize) the machinery for building symtabs.
8132 We do this before processing child DIEs, so that the line header table
8133 is available for DW_AT_decl_file. */
8134 setup_type_unit_groups (die, cu);
8135
8136 if (die->child != NULL)
8137 {
8138 child_die = die->child;
8139 while (child_die && child_die->tag)
8140 {
8141 process_die (child_die, cu);
8142 child_die = sibling_die (child_die);
8143 }
8144 }
3019eac3
DE
8145}
8146\f
80626a55
DE
8147/* DWO/DWP files.
8148
8149 http://gcc.gnu.org/wiki/DebugFission
8150 http://gcc.gnu.org/wiki/DebugFissionDWP
8151
8152 To simplify handling of both DWO files ("object" files with the DWARF info)
8153 and DWP files (a file with the DWOs packaged up into one file), we treat
8154 DWP files as having a collection of virtual DWO files. */
3019eac3
DE
8155
8156static hashval_t
8157hash_dwo_file (const void *item)
8158{
8159 const struct dwo_file *dwo_file = item;
8160
80626a55 8161 return htab_hash_string (dwo_file->name);
3019eac3
DE
8162}
8163
8164static int
8165eq_dwo_file (const void *item_lhs, const void *item_rhs)
8166{
8167 const struct dwo_file *lhs = item_lhs;
8168 const struct dwo_file *rhs = item_rhs;
8169
80626a55 8170 return strcmp (lhs->name, rhs->name) == 0;
3019eac3
DE
8171}
8172
8173/* Allocate a hash table for DWO files. */
8174
8175static htab_t
8176allocate_dwo_file_hash_table (void)
8177{
8178 struct objfile *objfile = dwarf2_per_objfile->objfile;
8179
8180 return htab_create_alloc_ex (41,
8181 hash_dwo_file,
8182 eq_dwo_file,
8183 NULL,
8184 &objfile->objfile_obstack,
8185 hashtab_obstack_allocate,
8186 dummy_obstack_deallocate);
8187}
8188
80626a55
DE
8189/* Lookup DWO file DWO_NAME. */
8190
8191static void **
8192lookup_dwo_file_slot (const char *dwo_name)
8193{
8194 struct dwo_file find_entry;
8195 void **slot;
8196
8197 if (dwarf2_per_objfile->dwo_files == NULL)
8198 dwarf2_per_objfile->dwo_files = allocate_dwo_file_hash_table ();
8199
8200 memset (&find_entry, 0, sizeof (find_entry));
8201 find_entry.name = dwo_name;
8202 slot = htab_find_slot (dwarf2_per_objfile->dwo_files, &find_entry, INSERT);
8203
8204 return slot;
8205}
8206
3019eac3
DE
8207static hashval_t
8208hash_dwo_unit (const void *item)
8209{
8210 const struct dwo_unit *dwo_unit = item;
8211
8212 /* This drops the top 32 bits of the id, but is ok for a hash. */
8213 return dwo_unit->signature;
8214}
8215
8216static int
8217eq_dwo_unit (const void *item_lhs, const void *item_rhs)
8218{
8219 const struct dwo_unit *lhs = item_lhs;
8220 const struct dwo_unit *rhs = item_rhs;
8221
8222 /* The signature is assumed to be unique within the DWO file.
8223 So while object file CU dwo_id's always have the value zero,
8224 that's OK, assuming each object file DWO file has only one CU,
8225 and that's the rule for now. */
8226 return lhs->signature == rhs->signature;
8227}
8228
8229/* Allocate a hash table for DWO CUs,TUs.
8230 There is one of these tables for each of CUs,TUs for each DWO file. */
8231
8232static htab_t
8233allocate_dwo_unit_table (struct objfile *objfile)
8234{
8235 /* Start out with a pretty small number.
8236 Generally DWO files contain only one CU and maybe some TUs. */
8237 return htab_create_alloc_ex (3,
8238 hash_dwo_unit,
8239 eq_dwo_unit,
8240 NULL,
8241 &objfile->objfile_obstack,
8242 hashtab_obstack_allocate,
8243 dummy_obstack_deallocate);
8244}
8245
80626a55 8246/* Structure used to pass data to create_dwo_debug_info_hash_table_reader. */
3019eac3
DE
8247
8248struct create_dwo_info_table_data
8249{
8250 struct dwo_file *dwo_file;
8251 htab_t cu_htab;
8252};
8253
80626a55 8254/* die_reader_func for create_dwo_debug_info_hash_table. */
3019eac3
DE
8255
8256static void
80626a55
DE
8257create_dwo_debug_info_hash_table_reader (const struct die_reader_specs *reader,
8258 gdb_byte *info_ptr,
8259 struct die_info *comp_unit_die,
8260 int has_children,
8261 void *datap)
3019eac3
DE
8262{
8263 struct dwarf2_cu *cu = reader->cu;
8264 struct objfile *objfile = dwarf2_per_objfile->objfile;
8265 sect_offset offset = cu->per_cu->offset;
8266 struct dwarf2_section_info *section = cu->per_cu->info_or_types_section;
8267 struct create_dwo_info_table_data *data = datap;
8268 struct dwo_file *dwo_file = data->dwo_file;
8269 htab_t cu_htab = data->cu_htab;
8270 void **slot;
8271 struct attribute *attr;
8272 struct dwo_unit *dwo_unit;
8273
8274 attr = dwarf2_attr (comp_unit_die, DW_AT_GNU_dwo_id, cu);
8275 if (attr == NULL)
8276 {
8277 error (_("Dwarf Error: debug entry at offset 0x%x is missing"
8278 " its dwo_id [in module %s]"),
80626a55 8279 offset.sect_off, dwo_file->name);
3019eac3
DE
8280 return;
8281 }
8282
8283 dwo_unit = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct dwo_unit);
8284 dwo_unit->dwo_file = dwo_file;
8285 dwo_unit->signature = DW_UNSND (attr);
8286 dwo_unit->info_or_types_section = section;
8287 dwo_unit->offset = offset;
8288 dwo_unit->length = cu->per_cu->length;
8289
8290 slot = htab_find_slot (cu_htab, dwo_unit, INSERT);
8291 gdb_assert (slot != NULL);
8292 if (*slot != NULL)
8293 {
8294 const struct dwo_unit *dup_dwo_unit = *slot;
8295
8296 complaint (&symfile_complaints,
8297 _("debug entry at offset 0x%x is duplicate to the entry at"
8298 " offset 0x%x, dwo_id 0x%s [in module %s]"),
8299 offset.sect_off, dup_dwo_unit->offset.sect_off,
8300 phex (dwo_unit->signature, sizeof (dwo_unit->signature)),
80626a55 8301 dwo_file->name);
3019eac3
DE
8302 }
8303 else
8304 *slot = dwo_unit;
8305
09406207 8306 if (dwarf2_read_debug)
3019eac3
DE
8307 fprintf_unfiltered (gdb_stdlog, " offset 0x%x, dwo_id 0x%s\n",
8308 offset.sect_off,
8309 phex (dwo_unit->signature,
8310 sizeof (dwo_unit->signature)));
8311}
8312
80626a55
DE
8313/* Create a hash table to map DWO IDs to their CU entry in
8314 .debug_info.dwo in DWO_FILE.
8315 Note: This function processes DWO files only, not DWP files. */
3019eac3
DE
8316
8317static htab_t
80626a55 8318create_dwo_debug_info_hash_table (struct dwo_file *dwo_file)
3019eac3
DE
8319{
8320 struct objfile *objfile = dwarf2_per_objfile->objfile;
8321 struct dwarf2_section_info *section = &dwo_file->sections.info;
8322 bfd *abfd;
8323 htab_t cu_htab;
8324 gdb_byte *info_ptr, *end_ptr;
8325 struct create_dwo_info_table_data create_dwo_info_table_data;
8326
8327 dwarf2_read_section (objfile, section);
8328 info_ptr = section->buffer;
8329
8330 if (info_ptr == NULL)
8331 return NULL;
8332
8333 /* We can't set abfd until now because the section may be empty or
8334 not present, in which case section->asection will be NULL. */
8335 abfd = section->asection->owner;
8336
09406207 8337 if (dwarf2_read_debug)
3019eac3
DE
8338 fprintf_unfiltered (gdb_stdlog, "Reading .debug_info.dwo for %s:\n",
8339 bfd_get_filename (abfd));
8340
8341 cu_htab = allocate_dwo_unit_table (objfile);
8342
8343 create_dwo_info_table_data.dwo_file = dwo_file;
8344 create_dwo_info_table_data.cu_htab = cu_htab;
8345
8346 end_ptr = info_ptr + section->size;
8347 while (info_ptr < end_ptr)
8348 {
8349 struct dwarf2_per_cu_data per_cu;
8350
8351 memset (&per_cu, 0, sizeof (per_cu));
8352 per_cu.objfile = objfile;
8353 per_cu.is_debug_types = 0;
8354 per_cu.offset.sect_off = info_ptr - section->buffer;
8355 per_cu.info_or_types_section = section;
8356
8357 init_cutu_and_read_dies_no_follow (&per_cu,
8358 &dwo_file->sections.abbrev,
8359 dwo_file,
80626a55 8360 create_dwo_debug_info_hash_table_reader,
3019eac3
DE
8361 &create_dwo_info_table_data);
8362
8363 info_ptr += per_cu.length;
8364 }
8365
8366 return cu_htab;
8367}
8368
80626a55
DE
8369/* DWP file .debug_{cu,tu}_index section format:
8370 [ref: http://gcc.gnu.org/wiki/DebugFissionDWP]
8371
8372 Both index sections have the same format, and serve to map a 64-bit
8373 signature to a set of section numbers. Each section begins with a header,
8374 followed by a hash table of 64-bit signatures, a parallel table of 32-bit
8375 indexes, and a pool of 32-bit section numbers. The index sections will be
8376 aligned at 8-byte boundaries in the file.
8377
8378 The index section header contains two unsigned 32-bit values (using the
8379 byte order of the application binary):
8380
8381 N, the number of compilation units or type units in the index
8382 M, the number of slots in the hash table
8383
8384 (We assume that N and M will not exceed 2^32 - 1.)
8385
8386 The size of the hash table, M, must be 2^k such that 2^k > 3*N/2.
8387
8388 The hash table begins at offset 8 in the section, and consists of an array
8389 of M 64-bit slots. Each slot contains a 64-bit signature (using the byte
8390 order of the application binary). Unused slots in the hash table are 0.
8391 (We rely on the extreme unlikeliness of a signature being exactly 0.)
8392
8393 The parallel table begins immediately after the hash table
8394 (at offset 8 + 8 * M from the beginning of the section), and consists of an
8395 array of 32-bit indexes (using the byte order of the application binary),
8396 corresponding 1-1 with slots in the hash table. Each entry in the parallel
8397 table contains a 32-bit index into the pool of section numbers. For unused
8398 hash table slots, the corresponding entry in the parallel table will be 0.
8399
8400 Given a 64-bit compilation unit signature or a type signature S, an entry
8401 in the hash table is located as follows:
8402
8403 1) Calculate a primary hash H = S & MASK(k), where MASK(k) is a mask with
8404 the low-order k bits all set to 1.
8405
8406 2) Calculate a secondary hash H' = (((S >> 32) & MASK(k)) | 1).
8407
8408 3) If the hash table entry at index H matches the signature, use that
8409 entry. If the hash table entry at index H is unused (all zeroes),
8410 terminate the search: the signature is not present in the table.
8411
8412 4) Let H = (H + H') modulo M. Repeat at Step 3.
8413
8414 Because M > N and H' and M are relatively prime, the search is guaranteed
8415 to stop at an unused slot or find the match.
8416
8417 The pool of section numbers begins immediately following the hash table
8418 (at offset 8 + 12 * M from the beginning of the section). The pool of
8419 section numbers consists of an array of 32-bit words (using the byte order
8420 of the application binary). Each item in the array is indexed starting
8421 from 0. The hash table entry provides the index of the first section
8422 number in the set. Additional section numbers in the set follow, and the
8423 set is terminated by a 0 entry (section number 0 is not used in ELF).
8424
8425 In each set of section numbers, the .debug_info.dwo or .debug_types.dwo
8426 section must be the first entry in the set, and the .debug_abbrev.dwo must
8427 be the second entry. Other members of the set may follow in any order. */
8428
8429/* Create a hash table to map DWO IDs to their CU/TU entry in
8430 .debug_{info,types}.dwo in DWP_FILE.
8431 Returns NULL if there isn't one.
8432 Note: This function processes DWP files only, not DWO files. */
8433
8434static struct dwp_hash_table *
8435create_dwp_hash_table (struct dwp_file *dwp_file, int is_debug_types)
8436{
8437 struct objfile *objfile = dwarf2_per_objfile->objfile;
8438 bfd *dbfd = dwp_file->dbfd;
8439 char *index_ptr, *index_end;
8440 struct dwarf2_section_info *index;
8441 uint32_t version, nr_units, nr_slots;
8442 struct dwp_hash_table *htab;
8443
8444 if (is_debug_types)
8445 index = &dwp_file->sections.tu_index;
8446 else
8447 index = &dwp_file->sections.cu_index;
8448
8449 if (dwarf2_section_empty_p (index))
8450 return NULL;
8451 dwarf2_read_section (objfile, index);
8452
8453 index_ptr = index->buffer;
8454 index_end = index_ptr + index->size;
8455
8456 version = read_4_bytes (dbfd, index_ptr);
8457 index_ptr += 8; /* Skip the unused word. */
8458 nr_units = read_4_bytes (dbfd, index_ptr);
8459 index_ptr += 4;
8460 nr_slots = read_4_bytes (dbfd, index_ptr);
8461 index_ptr += 4;
8462
8463 if (version != 1)
8464 {
8465 error (_("Dwarf Error: unsupported DWP file version (%u)"
8466 " [in module %s]"),
8467 version, dwp_file->name);
8468 }
8469 if (nr_slots != (nr_slots & -nr_slots))
8470 {
8471 error (_("Dwarf Error: number of slots in DWP hash table (%u)"
8472 " is not power of 2 [in module %s]"),
8473 nr_slots, dwp_file->name);
8474 }
8475
8476 htab = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct dwp_hash_table);
8477 htab->nr_units = nr_units;
8478 htab->nr_slots = nr_slots;
8479 htab->hash_table = index_ptr;
8480 htab->unit_table = htab->hash_table + sizeof (uint64_t) * nr_slots;
8481 htab->section_pool = htab->unit_table + sizeof (uint32_t) * nr_slots;
8482
8483 return htab;
8484}
8485
8486/* Update SECTIONS with the data from SECTP.
8487
8488 This function is like the other "locate" section routines that are
8489 passed to bfd_map_over_sections, but in this context the sections to
8490 read comes from the DWP hash table, not the full ELF section table.
8491
8492 The result is non-zero for success, or zero if an error was found. */
8493
8494static int
8495locate_virtual_dwo_sections (asection *sectp,
8496 struct virtual_dwo_sections *sections)
8497{
8498 const struct dwop_section_names *names = &dwop_section_names;
8499
8500 if (section_is_p (sectp->name, &names->abbrev_dwo))
8501 {
8502 /* There can be only one. */
8503 if (sections->abbrev.asection != NULL)
8504 return 0;
8505 sections->abbrev.asection = sectp;
8506 sections->abbrev.size = bfd_get_section_size (sectp);
8507 }
8508 else if (section_is_p (sectp->name, &names->info_dwo)
8509 || section_is_p (sectp->name, &names->types_dwo))
8510 {
8511 /* There can be only one. */
8512 if (sections->info_or_types.asection != NULL)
8513 return 0;
8514 sections->info_or_types.asection = sectp;
8515 sections->info_or_types.size = bfd_get_section_size (sectp);
8516 }
8517 else if (section_is_p (sectp->name, &names->line_dwo))
8518 {
8519 /* There can be only one. */
8520 if (sections->line.asection != NULL)
8521 return 0;
8522 sections->line.asection = sectp;
8523 sections->line.size = bfd_get_section_size (sectp);
8524 }
8525 else if (section_is_p (sectp->name, &names->loc_dwo))
8526 {
8527 /* There can be only one. */
8528 if (sections->loc.asection != NULL)
8529 return 0;
8530 sections->loc.asection = sectp;
8531 sections->loc.size = bfd_get_section_size (sectp);
8532 }
8533 else if (section_is_p (sectp->name, &names->macinfo_dwo))
8534 {
8535 /* There can be only one. */
8536 if (sections->macinfo.asection != NULL)
8537 return 0;
8538 sections->macinfo.asection = sectp;
8539 sections->macinfo.size = bfd_get_section_size (sectp);
8540 }
8541 else if (section_is_p (sectp->name, &names->macro_dwo))
8542 {
8543 /* There can be only one. */
8544 if (sections->macro.asection != NULL)
8545 return 0;
8546 sections->macro.asection = sectp;
8547 sections->macro.size = bfd_get_section_size (sectp);
8548 }
8549 else if (section_is_p (sectp->name, &names->str_offsets_dwo))
8550 {
8551 /* There can be only one. */
8552 if (sections->str_offsets.asection != NULL)
8553 return 0;
8554 sections->str_offsets.asection = sectp;
8555 sections->str_offsets.size = bfd_get_section_size (sectp);
8556 }
8557 else
8558 {
8559 /* No other kind of section is valid. */
8560 return 0;
8561 }
8562
8563 return 1;
8564}
8565
8566/* Create a dwo_unit object for the DWO with signature SIGNATURE.
8567 HTAB is the hash table from the DWP file.
8568 SECTION_INDEX is the index of the DWO in HTAB. */
8569
8570static struct dwo_unit *
8571create_dwo_in_dwp (struct dwp_file *dwp_file,
8572 const struct dwp_hash_table *htab,
8573 uint32_t section_index,
8574 ULONGEST signature, int is_debug_types)
8575{
8576 struct objfile *objfile = dwarf2_per_objfile->objfile;
8577 bfd *dbfd = dwp_file->dbfd;
8578 const char *kind = is_debug_types ? "TU" : "CU";
8579 struct dwo_file *dwo_file;
8580 struct dwo_unit *dwo_unit;
8581 struct virtual_dwo_sections sections;
8582 void **dwo_file_slot;
8583 char *virtual_dwo_name;
8584 struct dwarf2_section_info *cutu;
8585 struct cleanup *cleanups;
8586 int i;
8587
8588 if (dwarf2_read_debug)
8589 {
8590 fprintf_unfiltered (gdb_stdlog, "Reading %s %u/0x%s in DWP file: %s\n",
8591 kind,
8592 section_index, phex (signature, sizeof (signature)),
8593 dwp_file->name);
8594 }
8595
8596 /* Fetch the sections of this DWO.
8597 Put a limit on the number of sections we look for so that bad data
8598 doesn't cause us to loop forever. */
8599
8600#define MAX_NR_DWO_SECTIONS \
8601 (1 /* .debug_info or .debug_types */ \
8602 + 1 /* .debug_abbrev */ \
8603 + 1 /* .debug_line */ \
8604 + 1 /* .debug_loc */ \
8605 + 1 /* .debug_str_offsets */ \
8606 + 1 /* .debug_macro */ \
8607 + 1 /* .debug_macinfo */ \
8608 + 1 /* trailing zero */)
8609
8610 memset (&sections, 0, sizeof (sections));
8611 cleanups = make_cleanup (null_cleanup, 0);
8612
8613 for (i = 0; i < MAX_NR_DWO_SECTIONS; ++i)
8614 {
8615 asection *sectp;
8616 uint32_t section_nr =
8617 read_4_bytes (dbfd,
8618 htab->section_pool
8619 + (section_index + i) * sizeof (uint32_t));
8620
8621 if (section_nr == 0)
8622 break;
8623 if (section_nr >= dwp_file->num_sections)
8624 {
8625 error (_("Dwarf Error: bad DWP hash table, section number too large"
8626 " [in module %s]"),
8627 dwp_file->name);
8628 }
8629
8630 sectp = dwp_file->elf_sections[section_nr];
8631 if (! locate_virtual_dwo_sections (sectp, &sections))
8632 {
8633 error (_("Dwarf Error: bad DWP hash table, invalid section found"
8634 " [in module %s]"),
8635 dwp_file->name);
8636 }
8637 }
8638
8639 if (i < 2
8640 || sections.info_or_types.asection == NULL
8641 || sections.abbrev.asection == NULL)
8642 {
8643 error (_("Dwarf Error: bad DWP hash table, missing DWO sections"
8644 " [in module %s]"),
8645 dwp_file->name);
8646 }
8647 if (i == MAX_NR_DWO_SECTIONS)
8648 {
8649 error (_("Dwarf Error: bad DWP hash table, too many DWO sections"
8650 " [in module %s]"),
8651 dwp_file->name);
8652 }
8653
8654 /* It's easier for the rest of the code if we fake a struct dwo_file and
8655 have dwo_unit "live" in that. At least for now.
8656
8657 The DWP file can be made up of a random collection of CUs and TUs.
c766f7ec
DE
8658 However, for each CU + set of TUs that came from the same original DWO
8659 file, we want to combine them back into a virtual DWO file to save space
80626a55
DE
8660 (fewer struct dwo_file objects to allocated). Remember that for really
8661 large apps there can be on the order of 8K CUs and 200K TUs, or more. */
8662
2792b94d
PM
8663 virtual_dwo_name =
8664 xstrprintf ("virtual-dwo/%d-%d-%d-%d",
8665 sections.abbrev.asection ? sections.abbrev.asection->id : 0,
8666 sections.line.asection ? sections.line.asection->id : 0,
8667 sections.loc.asection ? sections.loc.asection->id : 0,
8668 (sections.str_offsets.asection
8669 ? sections.str_offsets.asection->id
8670 : 0));
80626a55
DE
8671 make_cleanup (xfree, virtual_dwo_name);
8672 /* Can we use an existing virtual DWO file? */
8673 dwo_file_slot = lookup_dwo_file_slot (virtual_dwo_name);
8674 /* Create one if necessary. */
8675 if (*dwo_file_slot == NULL)
8676 {
8677 if (dwarf2_read_debug)
8678 {
8679 fprintf_unfiltered (gdb_stdlog, "Creating virtual DWO: %s\n",
8680 virtual_dwo_name);
8681 }
8682 dwo_file = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct dwo_file);
8683 dwo_file->name = obstack_copy0 (&objfile->objfile_obstack,
8684 virtual_dwo_name,
8685 strlen (virtual_dwo_name));
8686 dwo_file->sections.abbrev = sections.abbrev;
8687 dwo_file->sections.line = sections.line;
8688 dwo_file->sections.loc = sections.loc;
8689 dwo_file->sections.macinfo = sections.macinfo;
8690 dwo_file->sections.macro = sections.macro;
8691 dwo_file->sections.str_offsets = sections.str_offsets;
8692 /* The "str" section is global to the entire DWP file. */
8693 dwo_file->sections.str = dwp_file->sections.str;
8694 /* The info or types section is assigned later to dwo_unit,
8695 there's no need to record it in dwo_file.
8696 Also, we can't simply record type sections in dwo_file because
8697 we record a pointer into the vector in dwo_unit. As we collect more
8698 types we'll grow the vector and eventually have to reallocate space
8699 for it, invalidating all the pointers into the current copy. */
8700 *dwo_file_slot = dwo_file;
8701 }
8702 else
8703 {
8704 if (dwarf2_read_debug)
8705 {
8706 fprintf_unfiltered (gdb_stdlog, "Using existing virtual DWO: %s\n",
8707 virtual_dwo_name);
8708 }
8709 dwo_file = *dwo_file_slot;
8710 }
8711 do_cleanups (cleanups);
8712
8713 dwo_unit = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct dwo_unit);
8714 dwo_unit->dwo_file = dwo_file;
8715 dwo_unit->signature = signature;
8716 dwo_unit->info_or_types_section =
8717 obstack_alloc (&objfile->objfile_obstack,
8718 sizeof (struct dwarf2_section_info));
8719 *dwo_unit->info_or_types_section = sections.info_or_types;
8720 /* offset, length, type_offset_in_tu are set later. */
8721
8722 return dwo_unit;
8723}
8724
8725/* Lookup the DWO with SIGNATURE in DWP_FILE. */
8726
8727static struct dwo_unit *
8728lookup_dwo_in_dwp (struct dwp_file *dwp_file,
8729 const struct dwp_hash_table *htab,
8730 ULONGEST signature, int is_debug_types)
8731{
8732 bfd *dbfd = dwp_file->dbfd;
8733 uint32_t mask = htab->nr_slots - 1;
8734 uint32_t hash = signature & mask;
8735 uint32_t hash2 = ((signature >> 32) & mask) | 1;
8736 unsigned int i;
8737 void **slot;
8738 struct dwo_unit find_dwo_cu, *dwo_cu;
8739
8740 memset (&find_dwo_cu, 0, sizeof (find_dwo_cu));
8741 find_dwo_cu.signature = signature;
8742 slot = htab_find_slot (dwp_file->loaded_cutus, &find_dwo_cu, INSERT);
8743
8744 if (*slot != NULL)
8745 return *slot;
8746
8747 /* Use a for loop so that we don't loop forever on bad debug info. */
8748 for (i = 0; i < htab->nr_slots; ++i)
8749 {
8750 ULONGEST signature_in_table;
8751
8752 signature_in_table =
8753 read_8_bytes (dbfd, htab->hash_table + hash * sizeof (uint64_t));
8754 if (signature_in_table == signature)
8755 {
8756 uint32_t section_index =
8757 read_4_bytes (dbfd, htab->unit_table + hash * sizeof (uint32_t));
8758
8759 *slot = create_dwo_in_dwp (dwp_file, htab, section_index,
8760 signature, is_debug_types);
8761 return *slot;
8762 }
8763 if (signature_in_table == 0)
8764 return NULL;
8765 hash = (hash + hash2) & mask;
8766 }
8767
8768 error (_("Dwarf Error: bad DWP hash table, lookup didn't terminate"
8769 " [in module %s]"),
8770 dwp_file->name);
8771}
8772
8773/* Subroutine of open_dwop_file to simplify it.
3019eac3
DE
8774 Open the file specified by FILE_NAME and hand it off to BFD for
8775 preliminary analysis. Return a newly initialized bfd *, which
8776 includes a canonicalized copy of FILE_NAME.
80626a55 8777 If IS_DWP is TRUE, we're opening a DWP file, otherwise a DWO file.
3019eac3
DE
8778 In case of trouble, return NULL.
8779 NOTE: This function is derived from symfile_bfd_open. */
8780
8781static bfd *
80626a55 8782try_open_dwop_file (const char *file_name, int is_dwp)
3019eac3
DE
8783{
8784 bfd *sym_bfd;
80626a55 8785 int desc, flags;
3019eac3 8786 char *absolute_name;
3019eac3 8787
80626a55
DE
8788 flags = OPF_TRY_CWD_FIRST;
8789 if (is_dwp)
8790 flags |= OPF_SEARCH_IN_PATH;
8791 desc = openp (debug_file_directory, flags, file_name,
3019eac3
DE
8792 O_RDONLY | O_BINARY, &absolute_name);
8793 if (desc < 0)
8794 return NULL;
8795
bb397797 8796 sym_bfd = gdb_bfd_open (absolute_name, gnutarget, desc);
3019eac3
DE
8797 if (!sym_bfd)
8798 {
3019eac3
DE
8799 xfree (absolute_name);
8800 return NULL;
8801 }
a4453b7e 8802 xfree (absolute_name);
3019eac3
DE
8803 bfd_set_cacheable (sym_bfd, 1);
8804
8805 if (!bfd_check_format (sym_bfd, bfd_object))
8806 {
cbb099e8 8807 gdb_bfd_unref (sym_bfd); /* This also closes desc. */
3019eac3
DE
8808 return NULL;
8809 }
8810
3019eac3
DE
8811 return sym_bfd;
8812}
8813
80626a55 8814/* Try to open DWO/DWP file FILE_NAME.
3019eac3 8815 COMP_DIR is the DW_AT_comp_dir attribute.
80626a55 8816 If IS_DWP is TRUE, we're opening a DWP file, otherwise a DWO file.
3019eac3
DE
8817 The result is the bfd handle of the file.
8818 If there is a problem finding or opening the file, return NULL.
8819 Upon success, the canonicalized path of the file is stored in the bfd,
8820 same as symfile_bfd_open. */
8821
8822static bfd *
80626a55 8823open_dwop_file (const char *file_name, const char *comp_dir, int is_dwp)
3019eac3
DE
8824{
8825 bfd *abfd;
3019eac3 8826
80626a55
DE
8827 if (IS_ABSOLUTE_PATH (file_name))
8828 return try_open_dwop_file (file_name, is_dwp);
3019eac3
DE
8829
8830 /* Before trying the search path, try DWO_NAME in COMP_DIR. */
8831
8832 if (comp_dir != NULL)
8833 {
80626a55 8834 char *path_to_try = concat (comp_dir, SLASH_STRING, file_name, NULL);
3019eac3
DE
8835
8836 /* NOTE: If comp_dir is a relative path, this will also try the
8837 search path, which seems useful. */
80626a55 8838 abfd = try_open_dwop_file (path_to_try, is_dwp);
3019eac3
DE
8839 xfree (path_to_try);
8840 if (abfd != NULL)
8841 return abfd;
8842 }
8843
8844 /* That didn't work, try debug-file-directory, which, despite its name,
8845 is a list of paths. */
8846
8847 if (*debug_file_directory == '\0')
8848 return NULL;
8849
80626a55 8850 return try_open_dwop_file (file_name, is_dwp);
3019eac3
DE
8851}
8852
80626a55
DE
8853/* This function is mapped across the sections and remembers the offset and
8854 size of each of the DWO debugging sections we are interested in. */
8855
8856static void
8857dwarf2_locate_dwo_sections (bfd *abfd, asection *sectp, void *dwo_sections_ptr)
8858{
8859 struct dwo_sections *dwo_sections = dwo_sections_ptr;
8860 const struct dwop_section_names *names = &dwop_section_names;
8861
8862 if (section_is_p (sectp->name, &names->abbrev_dwo))
8863 {
8864 dwo_sections->abbrev.asection = sectp;
8865 dwo_sections->abbrev.size = bfd_get_section_size (sectp);
8866 }
8867 else if (section_is_p (sectp->name, &names->info_dwo))
8868 {
8869 dwo_sections->info.asection = sectp;
8870 dwo_sections->info.size = bfd_get_section_size (sectp);
8871 }
8872 else if (section_is_p (sectp->name, &names->line_dwo))
8873 {
8874 dwo_sections->line.asection = sectp;
8875 dwo_sections->line.size = bfd_get_section_size (sectp);
8876 }
8877 else if (section_is_p (sectp->name, &names->loc_dwo))
8878 {
8879 dwo_sections->loc.asection = sectp;
8880 dwo_sections->loc.size = bfd_get_section_size (sectp);
8881 }
8882 else if (section_is_p (sectp->name, &names->macinfo_dwo))
8883 {
8884 dwo_sections->macinfo.asection = sectp;
8885 dwo_sections->macinfo.size = bfd_get_section_size (sectp);
8886 }
8887 else if (section_is_p (sectp->name, &names->macro_dwo))
8888 {
8889 dwo_sections->macro.asection = sectp;
8890 dwo_sections->macro.size = bfd_get_section_size (sectp);
8891 }
8892 else if (section_is_p (sectp->name, &names->str_dwo))
8893 {
8894 dwo_sections->str.asection = sectp;
8895 dwo_sections->str.size = bfd_get_section_size (sectp);
8896 }
8897 else if (section_is_p (sectp->name, &names->str_offsets_dwo))
8898 {
8899 dwo_sections->str_offsets.asection = sectp;
8900 dwo_sections->str_offsets.size = bfd_get_section_size (sectp);
8901 }
8902 else if (section_is_p (sectp->name, &names->types_dwo))
8903 {
8904 struct dwarf2_section_info type_section;
8905
8906 memset (&type_section, 0, sizeof (type_section));
8907 type_section.asection = sectp;
8908 type_section.size = bfd_get_section_size (sectp);
8909 VEC_safe_push (dwarf2_section_info_def, dwo_sections->types,
8910 &type_section);
8911 }
8912}
8913
8914/* Initialize the use of the DWO file specified by DWO_NAME.
8915 The result is NULL if DWO_NAME can't be found. */
3019eac3
DE
8916
8917static struct dwo_file *
80626a55 8918open_and_init_dwo_file (const char *dwo_name, const char *comp_dir)
3019eac3
DE
8919{
8920 struct objfile *objfile = dwarf2_per_objfile->objfile;
80626a55
DE
8921 struct dwo_file *dwo_file;
8922 bfd *dbfd;
3019eac3
DE
8923 struct cleanup *cleanups;
8924
80626a55
DE
8925 dbfd = open_dwop_file (dwo_name, comp_dir, 0);
8926 if (dbfd == NULL)
8927 {
8928 if (dwarf2_read_debug)
8929 fprintf_unfiltered (gdb_stdlog, "DWO file not found: %s\n", dwo_name);
8930 return NULL;
8931 }
8932 dwo_file = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct dwo_file);
8933 dwo_file->name = obstack_copy0 (&objfile->objfile_obstack,
8934 dwo_name, strlen (dwo_name));
8935 dwo_file->dbfd = dbfd;
3019eac3
DE
8936
8937 cleanups = make_cleanup (free_dwo_file_cleanup, dwo_file);
8938
80626a55 8939 bfd_map_over_sections (dbfd, dwarf2_locate_dwo_sections, &dwo_file->sections);
3019eac3 8940
80626a55 8941 dwo_file->cus = create_dwo_debug_info_hash_table (dwo_file);
3019eac3
DE
8942
8943 dwo_file->tus = create_debug_types_hash_table (dwo_file,
8944 dwo_file->sections.types);
8945
8946 discard_cleanups (cleanups);
8947
80626a55
DE
8948 if (dwarf2_read_debug)
8949 fprintf_unfiltered (gdb_stdlog, "DWO file found: %s\n", dwo_name);
8950
3019eac3
DE
8951 return dwo_file;
8952}
8953
80626a55
DE
8954/* This function is mapped across the sections and remembers the offset and
8955 size of each of the DWP debugging sections we are interested in. */
3019eac3 8956
80626a55
DE
8957static void
8958dwarf2_locate_dwp_sections (bfd *abfd, asection *sectp, void *dwp_file_ptr)
3019eac3 8959{
80626a55
DE
8960 struct dwp_file *dwp_file = dwp_file_ptr;
8961 const struct dwop_section_names *names = &dwop_section_names;
8962 unsigned int elf_section_nr = elf_section_data (sectp)->this_idx;
3019eac3 8963
80626a55
DE
8964 /* Record the ELF section number for later lookup: this is what the
8965 .debug_cu_index,.debug_tu_index tables use. */
8966 gdb_assert (elf_section_nr < dwp_file->num_sections);
8967 dwp_file->elf_sections[elf_section_nr] = sectp;
3019eac3 8968
80626a55
DE
8969 /* Look for specific sections that we need. */
8970 if (section_is_p (sectp->name, &names->str_dwo))
8971 {
8972 dwp_file->sections.str.asection = sectp;
8973 dwp_file->sections.str.size = bfd_get_section_size (sectp);
8974 }
8975 else if (section_is_p (sectp->name, &names->cu_index))
8976 {
8977 dwp_file->sections.cu_index.asection = sectp;
8978 dwp_file->sections.cu_index.size = bfd_get_section_size (sectp);
8979 }
8980 else if (section_is_p (sectp->name, &names->tu_index))
8981 {
8982 dwp_file->sections.tu_index.asection = sectp;
8983 dwp_file->sections.tu_index.size = bfd_get_section_size (sectp);
8984 }
8985}
3019eac3 8986
80626a55 8987/* Hash function for dwp_file loaded CUs/TUs. */
3019eac3 8988
80626a55
DE
8989static hashval_t
8990hash_dwp_loaded_cutus (const void *item)
8991{
8992 const struct dwo_unit *dwo_unit = item;
3019eac3 8993
80626a55
DE
8994 /* This drops the top 32 bits of the signature, but is ok for a hash. */
8995 return dwo_unit->signature;
3019eac3
DE
8996}
8997
80626a55 8998/* Equality function for dwp_file loaded CUs/TUs. */
3019eac3 8999
80626a55
DE
9000static int
9001eq_dwp_loaded_cutus (const void *a, const void *b)
3019eac3 9002{
80626a55
DE
9003 const struct dwo_unit *dua = a;
9004 const struct dwo_unit *dub = b;
3019eac3 9005
80626a55
DE
9006 return dua->signature == dub->signature;
9007}
3019eac3 9008
80626a55 9009/* Allocate a hash table for dwp_file loaded CUs/TUs. */
3019eac3 9010
80626a55
DE
9011static htab_t
9012allocate_dwp_loaded_cutus_table (struct objfile *objfile)
9013{
9014 return htab_create_alloc_ex (3,
9015 hash_dwp_loaded_cutus,
9016 eq_dwp_loaded_cutus,
9017 NULL,
9018 &objfile->objfile_obstack,
9019 hashtab_obstack_allocate,
9020 dummy_obstack_deallocate);
9021}
3019eac3 9022
80626a55
DE
9023/* Initialize the use of the DWP file for the current objfile.
9024 By convention the name of the DWP file is ${objfile}.dwp.
9025 The result is NULL if it can't be found. */
a766d390 9026
80626a55
DE
9027static struct dwp_file *
9028open_and_init_dwp_file (const char *comp_dir)
9029{
9030 struct objfile *objfile = dwarf2_per_objfile->objfile;
9031 struct dwp_file *dwp_file;
9032 char *dwp_name;
9033 bfd *dbfd;
9034 struct cleanup *cleanups;
9035
2792b94d 9036 dwp_name = xstrprintf ("%s.dwp", dwarf2_per_objfile->objfile->name);
80626a55
DE
9037 cleanups = make_cleanup (xfree, dwp_name);
9038
9039 dbfd = open_dwop_file (dwp_name, comp_dir, 1);
9040 if (dbfd == NULL)
9041 {
9042 if (dwarf2_read_debug)
9043 fprintf_unfiltered (gdb_stdlog, "DWP file not found: %s\n", dwp_name);
9044 do_cleanups (cleanups);
9045 return NULL;
3019eac3 9046 }
80626a55
DE
9047 dwp_file = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct dwp_file);
9048 dwp_file->name = obstack_copy0 (&objfile->objfile_obstack,
9049 dwp_name, strlen (dwp_name));
9050 dwp_file->dbfd = dbfd;
9051 do_cleanups (cleanups);
c906108c 9052
80626a55 9053 cleanups = make_cleanup (free_dwo_file_cleanup, dwp_file);
df8a16a1 9054
80626a55
DE
9055 /* +1: section 0 is unused */
9056 dwp_file->num_sections = bfd_count_sections (dbfd) + 1;
9057 dwp_file->elf_sections =
9058 OBSTACK_CALLOC (&objfile->objfile_obstack,
9059 dwp_file->num_sections, asection *);
9060
9061 bfd_map_over_sections (dbfd, dwarf2_locate_dwp_sections, dwp_file);
9062
9063 dwp_file->cus = create_dwp_hash_table (dwp_file, 0);
9064
9065 dwp_file->tus = create_dwp_hash_table (dwp_file, 1);
9066
9067 dwp_file->loaded_cutus = allocate_dwp_loaded_cutus_table (objfile);
9068
9069 discard_cleanups (cleanups);
9070
9071 if (dwarf2_read_debug)
9072 {
9073 fprintf_unfiltered (gdb_stdlog, "DWP file found: %s\n", dwp_file->name);
9074 fprintf_unfiltered (gdb_stdlog,
9075 " %u CUs, %u TUs\n",
9076 dwp_file->cus ? dwp_file->cus->nr_units : 0,
9077 dwp_file->tus ? dwp_file->tus->nr_units : 0);
9078 }
9079
9080 return dwp_file;
3019eac3 9081}
c906108c 9082
80626a55
DE
9083/* Subroutine of lookup_dwo_comp_unit, lookup_dwo_type_unit.
9084 Look up the CU/TU with signature SIGNATURE, either in DWO file DWO_NAME
9085 or in the DWP file for the objfile, referenced by THIS_UNIT.
3019eac3 9086 If non-NULL, comp_dir is the DW_AT_comp_dir attribute.
80626a55
DE
9087 IS_DEBUG_TYPES is non-zero if reading a TU, otherwise read a CU.
9088
9089 This is called, for example, when wanting to read a variable with a
9090 complex location. Therefore we don't want to do file i/o for every call.
9091 Therefore we don't want to look for a DWO file on every call.
9092 Therefore we first see if we've already seen SIGNATURE in a DWP file,
9093 then we check if we've already seen DWO_NAME, and only THEN do we check
9094 for a DWO file.
9095
1c658ad5 9096 The result is a pointer to the dwo_unit object or NULL if we didn't find it
80626a55 9097 (dwo_id mismatch or couldn't find the DWO/DWP file). */
debd256d 9098
3019eac3 9099static struct dwo_unit *
80626a55
DE
9100lookup_dwo_cutu (struct dwarf2_per_cu_data *this_unit,
9101 const char *dwo_name, const char *comp_dir,
9102 ULONGEST signature, int is_debug_types)
3019eac3
DE
9103{
9104 struct objfile *objfile = dwarf2_per_objfile->objfile;
80626a55
DE
9105 const char *kind = is_debug_types ? "TU" : "CU";
9106 void **dwo_file_slot;
3019eac3 9107 struct dwo_file *dwo_file;
80626a55 9108 struct dwp_file *dwp_file;
cb1df416 9109
80626a55 9110 /* Have we already read SIGNATURE from a DWP file? */
cf2c3c16 9111
80626a55
DE
9112 if (! dwarf2_per_objfile->dwp_checked)
9113 {
9114 dwarf2_per_objfile->dwp_file = open_and_init_dwp_file (comp_dir);
9115 dwarf2_per_objfile->dwp_checked = 1;
9116 }
9117 dwp_file = dwarf2_per_objfile->dwp_file;
3019eac3 9118
80626a55 9119 if (dwp_file != NULL)
cf2c3c16 9120 {
80626a55
DE
9121 const struct dwp_hash_table *dwp_htab =
9122 is_debug_types ? dwp_file->tus : dwp_file->cus;
9123
9124 if (dwp_htab != NULL)
9125 {
9126 struct dwo_unit *dwo_cutu =
9127 lookup_dwo_in_dwp (dwp_file, dwp_htab, signature, is_debug_types);
9128
9129 if (dwo_cutu != NULL)
9130 {
9131 if (dwarf2_read_debug)
9132 {
9133 fprintf_unfiltered (gdb_stdlog,
9134 "Virtual DWO %s %s found: @%s\n",
9135 kind, hex_string (signature),
9136 host_address_to_string (dwo_cutu));
9137 }
9138 return dwo_cutu;
9139 }
9140 }
9141 }
9142
9143 /* Have we already seen DWO_NAME? */
9144
9145 dwo_file_slot = lookup_dwo_file_slot (dwo_name);
9146 if (*dwo_file_slot == NULL)
9147 {
9148 /* Read in the file and build a table of the DWOs it contains. */
9149 *dwo_file_slot = open_and_init_dwo_file (dwo_name, comp_dir);
9150 }
9151 /* NOTE: This will be NULL if unable to open the file. */
9152 dwo_file = *dwo_file_slot;
9153
9154 if (dwo_file != NULL)
9155 {
9156 htab_t htab = is_debug_types ? dwo_file->tus : dwo_file->cus;
9157
9158 if (htab != NULL)
9159 {
9160 struct dwo_unit find_dwo_cutu, *dwo_cutu;
9a619af0 9161
80626a55
DE
9162 memset (&find_dwo_cutu, 0, sizeof (find_dwo_cutu));
9163 find_dwo_cutu.signature = signature;
9164 dwo_cutu = htab_find (htab, &find_dwo_cutu);
3019eac3 9165
80626a55
DE
9166 if (dwo_cutu != NULL)
9167 {
9168 if (dwarf2_read_debug)
9169 {
9170 fprintf_unfiltered (gdb_stdlog, "DWO %s %s(%s) found: @%s\n",
9171 kind, dwo_name, hex_string (signature),
9172 host_address_to_string (dwo_cutu));
9173 }
9174 return dwo_cutu;
9175 }
9176 }
2e276125 9177 }
9cdd5dbd 9178
80626a55
DE
9179 /* We didn't find it. This could mean a dwo_id mismatch, or
9180 someone deleted the DWO/DWP file, or the search path isn't set up
9181 correctly to find the file. */
9182
9183 if (dwarf2_read_debug)
9184 {
9185 fprintf_unfiltered (gdb_stdlog, "DWO %s %s(%s) not found\n",
9186 kind, dwo_name, hex_string (signature));
9187 }
3019eac3
DE
9188
9189 complaint (&symfile_complaints,
80626a55 9190 _("Could not find DWO CU referenced by CU at offset 0x%x"
3019eac3 9191 " [in module %s]"),
80626a55 9192 this_unit->offset.sect_off, objfile->name);
3019eac3 9193 return NULL;
5fb290d7
DJ
9194}
9195
80626a55
DE
9196/* Lookup the DWO CU DWO_NAME/SIGNATURE referenced from THIS_CU.
9197 See lookup_dwo_cutu_unit for details. */
9198
9199static struct dwo_unit *
9200lookup_dwo_comp_unit (struct dwarf2_per_cu_data *this_cu,
9201 const char *dwo_name, const char *comp_dir,
9202 ULONGEST signature)
9203{
9204 return lookup_dwo_cutu (this_cu, dwo_name, comp_dir, signature, 0);
9205}
9206
9207/* Lookup the DWO TU DWO_NAME/SIGNATURE referenced from THIS_TU.
9208 See lookup_dwo_cutu_unit for details. */
9209
9210static struct dwo_unit *
9211lookup_dwo_type_unit (struct signatured_type *this_tu,
9212 const char *dwo_name, const char *comp_dir)
9213{
9214 return lookup_dwo_cutu (&this_tu->per_cu, dwo_name, comp_dir, this_tu->signature, 1);
9215}
9216
3019eac3
DE
9217/* Free all resources associated with DWO_FILE.
9218 Close the DWO file and munmap the sections.
9219 All memory should be on the objfile obstack. */
348e048f
DE
9220
9221static void
3019eac3 9222free_dwo_file (struct dwo_file *dwo_file, struct objfile *objfile)
348e048f 9223{
3019eac3
DE
9224 int ix;
9225 struct dwarf2_section_info *section;
348e048f 9226
80626a55 9227 gdb_bfd_unref (dwo_file->dbfd);
348e048f 9228
3019eac3
DE
9229 VEC_free (dwarf2_section_info_def, dwo_file->sections.types);
9230}
348e048f 9231
3019eac3 9232/* Wrapper for free_dwo_file for use in cleanups. */
348e048f 9233
3019eac3
DE
9234static void
9235free_dwo_file_cleanup (void *arg)
9236{
9237 struct dwo_file *dwo_file = (struct dwo_file *) arg;
9238 struct objfile *objfile = dwarf2_per_objfile->objfile;
348e048f 9239
3019eac3
DE
9240 free_dwo_file (dwo_file, objfile);
9241}
348e048f 9242
3019eac3 9243/* Traversal function for free_dwo_files. */
2ab95328 9244
3019eac3
DE
9245static int
9246free_dwo_file_from_slot (void **slot, void *info)
9247{
9248 struct dwo_file *dwo_file = (struct dwo_file *) *slot;
9249 struct objfile *objfile = (struct objfile *) info;
348e048f 9250
3019eac3 9251 free_dwo_file (dwo_file, objfile);
348e048f 9252
3019eac3
DE
9253 return 1;
9254}
348e048f 9255
3019eac3 9256/* Free all resources associated with DWO_FILES. */
348e048f 9257
3019eac3
DE
9258static void
9259free_dwo_files (htab_t dwo_files, struct objfile *objfile)
9260{
9261 htab_traverse_noresize (dwo_files, free_dwo_file_from_slot, objfile);
348e048f 9262}
3019eac3
DE
9263\f
9264/* Read in various DIEs. */
348e048f 9265
d389af10
JK
9266/* qsort helper for inherit_abstract_dies. */
9267
9268static int
9269unsigned_int_compar (const void *ap, const void *bp)
9270{
9271 unsigned int a = *(unsigned int *) ap;
9272 unsigned int b = *(unsigned int *) bp;
9273
9274 return (a > b) - (b > a);
9275}
9276
9277/* DW_AT_abstract_origin inherits whole DIEs (not just their attributes).
3e43a32a
MS
9278 Inherit only the children of the DW_AT_abstract_origin DIE not being
9279 already referenced by DW_AT_abstract_origin from the children of the
9280 current DIE. */
d389af10
JK
9281
9282static void
9283inherit_abstract_dies (struct die_info *die, struct dwarf2_cu *cu)
9284{
9285 struct die_info *child_die;
9286 unsigned die_children_count;
9287 /* CU offsets which were referenced by children of the current DIE. */
b64f50a1
JK
9288 sect_offset *offsets;
9289 sect_offset *offsets_end, *offsetp;
d389af10
JK
9290 /* Parent of DIE - referenced by DW_AT_abstract_origin. */
9291 struct die_info *origin_die;
9292 /* Iterator of the ORIGIN_DIE children. */
9293 struct die_info *origin_child_die;
9294 struct cleanup *cleanups;
9295 struct attribute *attr;
cd02d79d
PA
9296 struct dwarf2_cu *origin_cu;
9297 struct pending **origin_previous_list_in_scope;
d389af10
JK
9298
9299 attr = dwarf2_attr (die, DW_AT_abstract_origin, cu);
9300 if (!attr)
9301 return;
9302
cd02d79d
PA
9303 /* Note that following die references may follow to a die in a
9304 different cu. */
9305
9306 origin_cu = cu;
9307 origin_die = follow_die_ref (die, attr, &origin_cu);
9308
9309 /* We're inheriting ORIGIN's children into the scope we'd put DIE's
9310 symbols in. */
9311 origin_previous_list_in_scope = origin_cu->list_in_scope;
9312 origin_cu->list_in_scope = cu->list_in_scope;
9313
edb3359d
DJ
9314 if (die->tag != origin_die->tag
9315 && !(die->tag == DW_TAG_inlined_subroutine
9316 && origin_die->tag == DW_TAG_subprogram))
d389af10
JK
9317 complaint (&symfile_complaints,
9318 _("DIE 0x%x and its abstract origin 0x%x have different tags"),
b64f50a1 9319 die->offset.sect_off, origin_die->offset.sect_off);
d389af10
JK
9320
9321 child_die = die->child;
9322 die_children_count = 0;
9323 while (child_die && child_die->tag)
9324 {
9325 child_die = sibling_die (child_die);
9326 die_children_count++;
9327 }
9328 offsets = xmalloc (sizeof (*offsets) * die_children_count);
9329 cleanups = make_cleanup (xfree, offsets);
9330
9331 offsets_end = offsets;
9332 child_die = die->child;
9333 while (child_die && child_die->tag)
9334 {
c38f313d
DJ
9335 /* For each CHILD_DIE, find the corresponding child of
9336 ORIGIN_DIE. If there is more than one layer of
9337 DW_AT_abstract_origin, follow them all; there shouldn't be,
9338 but GCC versions at least through 4.4 generate this (GCC PR
9339 40573). */
9340 struct die_info *child_origin_die = child_die;
cd02d79d 9341 struct dwarf2_cu *child_origin_cu = cu;
9a619af0 9342
c38f313d
DJ
9343 while (1)
9344 {
cd02d79d
PA
9345 attr = dwarf2_attr (child_origin_die, DW_AT_abstract_origin,
9346 child_origin_cu);
c38f313d
DJ
9347 if (attr == NULL)
9348 break;
cd02d79d
PA
9349 child_origin_die = follow_die_ref (child_origin_die, attr,
9350 &child_origin_cu);
c38f313d
DJ
9351 }
9352
d389af10
JK
9353 /* According to DWARF3 3.3.8.2 #3 new entries without their abstract
9354 counterpart may exist. */
c38f313d 9355 if (child_origin_die != child_die)
d389af10 9356 {
edb3359d
DJ
9357 if (child_die->tag != child_origin_die->tag
9358 && !(child_die->tag == DW_TAG_inlined_subroutine
9359 && child_origin_die->tag == DW_TAG_subprogram))
d389af10
JK
9360 complaint (&symfile_complaints,
9361 _("Child DIE 0x%x and its abstract origin 0x%x have "
b64f50a1
JK
9362 "different tags"), child_die->offset.sect_off,
9363 child_origin_die->offset.sect_off);
c38f313d
DJ
9364 if (child_origin_die->parent != origin_die)
9365 complaint (&symfile_complaints,
9366 _("Child DIE 0x%x and its abstract origin 0x%x have "
b64f50a1
JK
9367 "different parents"), child_die->offset.sect_off,
9368 child_origin_die->offset.sect_off);
c38f313d
DJ
9369 else
9370 *offsets_end++ = child_origin_die->offset;
d389af10
JK
9371 }
9372 child_die = sibling_die (child_die);
9373 }
9374 qsort (offsets, offsets_end - offsets, sizeof (*offsets),
9375 unsigned_int_compar);
9376 for (offsetp = offsets + 1; offsetp < offsets_end; offsetp++)
b64f50a1 9377 if (offsetp[-1].sect_off == offsetp->sect_off)
3e43a32a
MS
9378 complaint (&symfile_complaints,
9379 _("Multiple children of DIE 0x%x refer "
9380 "to DIE 0x%x as their abstract origin"),
b64f50a1 9381 die->offset.sect_off, offsetp->sect_off);
d389af10
JK
9382
9383 offsetp = offsets;
9384 origin_child_die = origin_die->child;
9385 while (origin_child_die && origin_child_die->tag)
9386 {
9387 /* Is ORIGIN_CHILD_DIE referenced by any of the DIE children? */
b64f50a1
JK
9388 while (offsetp < offsets_end
9389 && offsetp->sect_off < origin_child_die->offset.sect_off)
d389af10 9390 offsetp++;
b64f50a1
JK
9391 if (offsetp >= offsets_end
9392 || offsetp->sect_off > origin_child_die->offset.sect_off)
d389af10
JK
9393 {
9394 /* Found that ORIGIN_CHILD_DIE is really not referenced. */
cd02d79d 9395 process_die (origin_child_die, origin_cu);
d389af10
JK
9396 }
9397 origin_child_die = sibling_die (origin_child_die);
9398 }
cd02d79d 9399 origin_cu->list_in_scope = origin_previous_list_in_scope;
d389af10
JK
9400
9401 do_cleanups (cleanups);
9402}
9403
c906108c 9404static void
e7c27a73 9405read_func_scope (struct die_info *die, struct dwarf2_cu *cu)
c906108c 9406{
e7c27a73 9407 struct objfile *objfile = cu->objfile;
52f0bd74 9408 struct context_stack *new;
c906108c
SS
9409 CORE_ADDR lowpc;
9410 CORE_ADDR highpc;
9411 struct die_info *child_die;
edb3359d 9412 struct attribute *attr, *call_line, *call_file;
15d034d0 9413 const char *name;
e142c38c 9414 CORE_ADDR baseaddr;
801e3a5b 9415 struct block *block;
edb3359d 9416 int inlined_func = (die->tag == DW_TAG_inlined_subroutine);
34eaf542
TT
9417 VEC (symbolp) *template_args = NULL;
9418 struct template_symbol *templ_func = NULL;
edb3359d
DJ
9419
9420 if (inlined_func)
9421 {
9422 /* If we do not have call site information, we can't show the
9423 caller of this inlined function. That's too confusing, so
9424 only use the scope for local variables. */
9425 call_line = dwarf2_attr (die, DW_AT_call_line, cu);
9426 call_file = dwarf2_attr (die, DW_AT_call_file, cu);
9427 if (call_line == NULL || call_file == NULL)
9428 {
9429 read_lexical_block_scope (die, cu);
9430 return;
9431 }
9432 }
c906108c 9433
e142c38c
DJ
9434 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
9435
94af9270 9436 name = dwarf2_name (die, cu);
c906108c 9437
e8d05480
JB
9438 /* Ignore functions with missing or empty names. These are actually
9439 illegal according to the DWARF standard. */
9440 if (name == NULL)
9441 {
9442 complaint (&symfile_complaints,
b64f50a1
JK
9443 _("missing name for subprogram DIE at %d"),
9444 die->offset.sect_off);
e8d05480
JB
9445 return;
9446 }
9447
9448 /* Ignore functions with missing or invalid low and high pc attributes. */
9449 if (!dwarf2_get_pc_bounds (die, &lowpc, &highpc, cu, NULL))
9450 {
ae4d0c03
PM
9451 attr = dwarf2_attr (die, DW_AT_external, cu);
9452 if (!attr || !DW_UNSND (attr))
9453 complaint (&symfile_complaints,
3e43a32a
MS
9454 _("cannot get low and high bounds "
9455 "for subprogram DIE at %d"),
b64f50a1 9456 die->offset.sect_off);
e8d05480
JB
9457 return;
9458 }
c906108c
SS
9459
9460 lowpc += baseaddr;
9461 highpc += baseaddr;
9462
34eaf542
TT
9463 /* If we have any template arguments, then we must allocate a
9464 different sort of symbol. */
9465 for (child_die = die->child; child_die; child_die = sibling_die (child_die))
9466 {
9467 if (child_die->tag == DW_TAG_template_type_param
9468 || child_die->tag == DW_TAG_template_value_param)
9469 {
9470 templ_func = OBSTACK_ZALLOC (&objfile->objfile_obstack,
9471 struct template_symbol);
9472 templ_func->base.is_cplus_template_function = 1;
9473 break;
9474 }
9475 }
9476
c906108c 9477 new = push_context (0, lowpc);
34eaf542
TT
9478 new->name = new_symbol_full (die, read_type_die (die, cu), cu,
9479 (struct symbol *) templ_func);
4c2df51b 9480
4cecd739
DJ
9481 /* If there is a location expression for DW_AT_frame_base, record
9482 it. */
e142c38c 9483 attr = dwarf2_attr (die, DW_AT_frame_base, cu);
4c2df51b 9484 if (attr)
c034e007
AC
9485 /* FIXME: cagney/2004-01-26: The DW_AT_frame_base's location
9486 expression is being recorded directly in the function's symbol
9487 and not in a separate frame-base object. I guess this hack is
9488 to avoid adding some sort of frame-base adjunct/annex to the
9489 function's symbol :-(. The problem with doing this is that it
9490 results in a function symbol with a location expression that
9491 has nothing to do with the location of the function, ouch! The
9492 relationship should be: a function's symbol has-a frame base; a
9493 frame-base has-a location expression. */
e7c27a73 9494 dwarf2_symbol_mark_computed (attr, new->name, cu);
4c2df51b 9495
e142c38c 9496 cu->list_in_scope = &local_symbols;
c906108c 9497
639d11d3 9498 if (die->child != NULL)
c906108c 9499 {
639d11d3 9500 child_die = die->child;
c906108c
SS
9501 while (child_die && child_die->tag)
9502 {
34eaf542
TT
9503 if (child_die->tag == DW_TAG_template_type_param
9504 || child_die->tag == DW_TAG_template_value_param)
9505 {
9506 struct symbol *arg = new_symbol (child_die, NULL, cu);
9507
f1078f66
DJ
9508 if (arg != NULL)
9509 VEC_safe_push (symbolp, template_args, arg);
34eaf542
TT
9510 }
9511 else
9512 process_die (child_die, cu);
c906108c
SS
9513 child_die = sibling_die (child_die);
9514 }
9515 }
9516
d389af10
JK
9517 inherit_abstract_dies (die, cu);
9518
4a811a97
UW
9519 /* If we have a DW_AT_specification, we might need to import using
9520 directives from the context of the specification DIE. See the
9521 comment in determine_prefix. */
9522 if (cu->language == language_cplus
9523 && dwarf2_attr (die, DW_AT_specification, cu))
9524 {
9525 struct dwarf2_cu *spec_cu = cu;
9526 struct die_info *spec_die = die_specification (die, &spec_cu);
9527
9528 while (spec_die)
9529 {
9530 child_die = spec_die->child;
9531 while (child_die && child_die->tag)
9532 {
9533 if (child_die->tag == DW_TAG_imported_module)
9534 process_die (child_die, spec_cu);
9535 child_die = sibling_die (child_die);
9536 }
9537
9538 /* In some cases, GCC generates specification DIEs that
9539 themselves contain DW_AT_specification attributes. */
9540 spec_die = die_specification (spec_die, &spec_cu);
9541 }
9542 }
9543
c906108c
SS
9544 new = pop_context ();
9545 /* Make a block for the local symbols within. */
801e3a5b
JB
9546 block = finish_block (new->name, &local_symbols, new->old_blocks,
9547 lowpc, highpc, objfile);
9548
df8a16a1 9549 /* For C++, set the block's scope. */
195a3f6c 9550 if ((cu->language == language_cplus || cu->language == language_fortran)
4d4ec4e5 9551 && cu->processing_has_namespace_info)
195a3f6c
TT
9552 block_set_scope (block, determine_prefix (die, cu),
9553 &objfile->objfile_obstack);
df8a16a1 9554
801e3a5b
JB
9555 /* If we have address ranges, record them. */
9556 dwarf2_record_block_ranges (die, block, baseaddr, cu);
6e70227d 9557
34eaf542
TT
9558 /* Attach template arguments to function. */
9559 if (! VEC_empty (symbolp, template_args))
9560 {
9561 gdb_assert (templ_func != NULL);
9562
9563 templ_func->n_template_arguments = VEC_length (symbolp, template_args);
9564 templ_func->template_arguments
9565 = obstack_alloc (&objfile->objfile_obstack,
9566 (templ_func->n_template_arguments
9567 * sizeof (struct symbol *)));
9568 memcpy (templ_func->template_arguments,
9569 VEC_address (symbolp, template_args),
9570 (templ_func->n_template_arguments * sizeof (struct symbol *)));
9571 VEC_free (symbolp, template_args);
9572 }
9573
208d8187
JB
9574 /* In C++, we can have functions nested inside functions (e.g., when
9575 a function declares a class that has methods). This means that
9576 when we finish processing a function scope, we may need to go
9577 back to building a containing block's symbol lists. */
9578 local_symbols = new->locals;
27aa8d6a 9579 using_directives = new->using_directives;
208d8187 9580
921e78cf
JB
9581 /* If we've finished processing a top-level function, subsequent
9582 symbols go in the file symbol list. */
9583 if (outermost_context_p ())
e142c38c 9584 cu->list_in_scope = &file_symbols;
c906108c
SS
9585}
9586
9587/* Process all the DIES contained within a lexical block scope. Start
9588 a new scope, process the dies, and then close the scope. */
9589
9590static void
e7c27a73 9591read_lexical_block_scope (struct die_info *die, struct dwarf2_cu *cu)
c906108c 9592{
e7c27a73 9593 struct objfile *objfile = cu->objfile;
52f0bd74 9594 struct context_stack *new;
c906108c
SS
9595 CORE_ADDR lowpc, highpc;
9596 struct die_info *child_die;
e142c38c
DJ
9597 CORE_ADDR baseaddr;
9598
9599 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
c906108c
SS
9600
9601 /* Ignore blocks with missing or invalid low and high pc attributes. */
af34e669
DJ
9602 /* ??? Perhaps consider discontiguous blocks defined by DW_AT_ranges
9603 as multiple lexical blocks? Handling children in a sane way would
6e70227d 9604 be nasty. Might be easier to properly extend generic blocks to
af34e669 9605 describe ranges. */
d85a05f0 9606 if (!dwarf2_get_pc_bounds (die, &lowpc, &highpc, cu, NULL))
c906108c
SS
9607 return;
9608 lowpc += baseaddr;
9609 highpc += baseaddr;
9610
9611 push_context (0, lowpc);
639d11d3 9612 if (die->child != NULL)
c906108c 9613 {
639d11d3 9614 child_die = die->child;
c906108c
SS
9615 while (child_die && child_die->tag)
9616 {
e7c27a73 9617 process_die (child_die, cu);
c906108c
SS
9618 child_die = sibling_die (child_die);
9619 }
9620 }
9621 new = pop_context ();
9622
8540c487 9623 if (local_symbols != NULL || using_directives != NULL)
c906108c 9624 {
801e3a5b
JB
9625 struct block *block
9626 = finish_block (0, &local_symbols, new->old_blocks, new->start_addr,
9627 highpc, objfile);
9628
9629 /* Note that recording ranges after traversing children, as we
9630 do here, means that recording a parent's ranges entails
9631 walking across all its children's ranges as they appear in
9632 the address map, which is quadratic behavior.
9633
9634 It would be nicer to record the parent's ranges before
9635 traversing its children, simply overriding whatever you find
9636 there. But since we don't even decide whether to create a
9637 block until after we've traversed its children, that's hard
9638 to do. */
9639 dwarf2_record_block_ranges (die, block, baseaddr, cu);
c906108c
SS
9640 }
9641 local_symbols = new->locals;
27aa8d6a 9642 using_directives = new->using_directives;
c906108c
SS
9643}
9644
96408a79
SA
9645/* Read in DW_TAG_GNU_call_site and insert it to CU->call_site_htab. */
9646
9647static void
9648read_call_site_scope (struct die_info *die, struct dwarf2_cu *cu)
9649{
9650 struct objfile *objfile = cu->objfile;
9651 struct gdbarch *gdbarch = get_objfile_arch (objfile);
9652 CORE_ADDR pc, baseaddr;
9653 struct attribute *attr;
9654 struct call_site *call_site, call_site_local;
9655 void **slot;
9656 int nparams;
9657 struct die_info *child_die;
9658
9659 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
9660
9661 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
9662 if (!attr)
9663 {
9664 complaint (&symfile_complaints,
9665 _("missing DW_AT_low_pc for DW_TAG_GNU_call_site "
9666 "DIE 0x%x [in module %s]"),
b64f50a1 9667 die->offset.sect_off, objfile->name);
96408a79
SA
9668 return;
9669 }
9670 pc = DW_ADDR (attr) + baseaddr;
9671
9672 if (cu->call_site_htab == NULL)
9673 cu->call_site_htab = htab_create_alloc_ex (16, core_addr_hash, core_addr_eq,
9674 NULL, &objfile->objfile_obstack,
9675 hashtab_obstack_allocate, NULL);
9676 call_site_local.pc = pc;
9677 slot = htab_find_slot (cu->call_site_htab, &call_site_local, INSERT);
9678 if (*slot != NULL)
9679 {
9680 complaint (&symfile_complaints,
9681 _("Duplicate PC %s for DW_TAG_GNU_call_site "
9682 "DIE 0x%x [in module %s]"),
b64f50a1 9683 paddress (gdbarch, pc), die->offset.sect_off, objfile->name);
96408a79
SA
9684 return;
9685 }
9686
9687 /* Count parameters at the caller. */
9688
9689 nparams = 0;
9690 for (child_die = die->child; child_die && child_die->tag;
9691 child_die = sibling_die (child_die))
9692 {
9693 if (child_die->tag != DW_TAG_GNU_call_site_parameter)
9694 {
9695 complaint (&symfile_complaints,
9696 _("Tag %d is not DW_TAG_GNU_call_site_parameter in "
9697 "DW_TAG_GNU_call_site child DIE 0x%x [in module %s]"),
b64f50a1 9698 child_die->tag, child_die->offset.sect_off, objfile->name);
96408a79
SA
9699 continue;
9700 }
9701
9702 nparams++;
9703 }
9704
9705 call_site = obstack_alloc (&objfile->objfile_obstack,
9706 (sizeof (*call_site)
9707 + (sizeof (*call_site->parameter)
9708 * (nparams - 1))));
9709 *slot = call_site;
9710 memset (call_site, 0, sizeof (*call_site) - sizeof (*call_site->parameter));
9711 call_site->pc = pc;
9712
9713 if (dwarf2_flag_true_p (die, DW_AT_GNU_tail_call, cu))
9714 {
9715 struct die_info *func_die;
9716
9717 /* Skip also over DW_TAG_inlined_subroutine. */
9718 for (func_die = die->parent;
9719 func_die && func_die->tag != DW_TAG_subprogram
9720 && func_die->tag != DW_TAG_subroutine_type;
9721 func_die = func_die->parent);
9722
9723 /* DW_AT_GNU_all_call_sites is a superset
9724 of DW_AT_GNU_all_tail_call_sites. */
9725 if (func_die
9726 && !dwarf2_flag_true_p (func_die, DW_AT_GNU_all_call_sites, cu)
9727 && !dwarf2_flag_true_p (func_die, DW_AT_GNU_all_tail_call_sites, cu))
9728 {
9729 /* TYPE_TAIL_CALL_LIST is not interesting in functions where it is
9730 not complete. But keep CALL_SITE for look ups via call_site_htab,
9731 both the initial caller containing the real return address PC and
9732 the final callee containing the current PC of a chain of tail
9733 calls do not need to have the tail call list complete. But any
9734 function candidate for a virtual tail call frame searched via
9735 TYPE_TAIL_CALL_LIST must have the tail call list complete to be
9736 determined unambiguously. */
9737 }
9738 else
9739 {
9740 struct type *func_type = NULL;
9741
9742 if (func_die)
9743 func_type = get_die_type (func_die, cu);
9744 if (func_type != NULL)
9745 {
9746 gdb_assert (TYPE_CODE (func_type) == TYPE_CODE_FUNC);
9747
9748 /* Enlist this call site to the function. */
9749 call_site->tail_call_next = TYPE_TAIL_CALL_LIST (func_type);
9750 TYPE_TAIL_CALL_LIST (func_type) = call_site;
9751 }
9752 else
9753 complaint (&symfile_complaints,
9754 _("Cannot find function owning DW_TAG_GNU_call_site "
9755 "DIE 0x%x [in module %s]"),
b64f50a1 9756 die->offset.sect_off, objfile->name);
96408a79
SA
9757 }
9758 }
9759
9760 attr = dwarf2_attr (die, DW_AT_GNU_call_site_target, cu);
9761 if (attr == NULL)
9762 attr = dwarf2_attr (die, DW_AT_abstract_origin, cu);
9763 SET_FIELD_DWARF_BLOCK (call_site->target, NULL);
9764 if (!attr || (attr_form_is_block (attr) && DW_BLOCK (attr)->size == 0))
9765 /* Keep NULL DWARF_BLOCK. */;
9766 else if (attr_form_is_block (attr))
9767 {
9768 struct dwarf2_locexpr_baton *dlbaton;
9769
9770 dlbaton = obstack_alloc (&objfile->objfile_obstack, sizeof (*dlbaton));
9771 dlbaton->data = DW_BLOCK (attr)->data;
9772 dlbaton->size = DW_BLOCK (attr)->size;
9773 dlbaton->per_cu = cu->per_cu;
9774
9775 SET_FIELD_DWARF_BLOCK (call_site->target, dlbaton);
9776 }
9777 else if (is_ref_attr (attr))
9778 {
96408a79
SA
9779 struct dwarf2_cu *target_cu = cu;
9780 struct die_info *target_die;
9781
9782 target_die = follow_die_ref_or_sig (die, attr, &target_cu);
9783 gdb_assert (target_cu->objfile == objfile);
9784 if (die_is_declaration (target_die, target_cu))
9785 {
9786 const char *target_physname;
9787
9788 target_physname = dwarf2_physname (NULL, target_die, target_cu);
9789 if (target_physname == NULL)
9790 complaint (&symfile_complaints,
9791 _("DW_AT_GNU_call_site_target target DIE has invalid "
9792 "physname, for referencing DIE 0x%x [in module %s]"),
b64f50a1 9793 die->offset.sect_off, objfile->name);
96408a79 9794 else
7d455152 9795 SET_FIELD_PHYSNAME (call_site->target, target_physname);
96408a79
SA
9796 }
9797 else
9798 {
9799 CORE_ADDR lowpc;
9800
9801 /* DW_AT_entry_pc should be preferred. */
9802 if (!dwarf2_get_pc_bounds (target_die, &lowpc, NULL, target_cu, NULL))
9803 complaint (&symfile_complaints,
9804 _("DW_AT_GNU_call_site_target target DIE has invalid "
9805 "low pc, for referencing DIE 0x%x [in module %s]"),
b64f50a1 9806 die->offset.sect_off, objfile->name);
96408a79
SA
9807 else
9808 SET_FIELD_PHYSADDR (call_site->target, lowpc + baseaddr);
9809 }
9810 }
9811 else
9812 complaint (&symfile_complaints,
9813 _("DW_TAG_GNU_call_site DW_AT_GNU_call_site_target is neither "
9814 "block nor reference, for DIE 0x%x [in module %s]"),
b64f50a1 9815 die->offset.sect_off, objfile->name);
96408a79
SA
9816
9817 call_site->per_cu = cu->per_cu;
9818
9819 for (child_die = die->child;
9820 child_die && child_die->tag;
9821 child_die = sibling_die (child_die))
9822 {
96408a79 9823 struct call_site_parameter *parameter;
1788b2d3 9824 struct attribute *loc, *origin;
96408a79
SA
9825
9826 if (child_die->tag != DW_TAG_GNU_call_site_parameter)
9827 {
9828 /* Already printed the complaint above. */
9829 continue;
9830 }
9831
9832 gdb_assert (call_site->parameter_count < nparams);
9833 parameter = &call_site->parameter[call_site->parameter_count];
9834
1788b2d3
JK
9835 /* DW_AT_location specifies the register number or DW_AT_abstract_origin
9836 specifies DW_TAG_formal_parameter. Value of the data assumed for the
9837 register is contained in DW_AT_GNU_call_site_value. */
96408a79 9838
24c5c679 9839 loc = dwarf2_attr (child_die, DW_AT_location, cu);
1788b2d3
JK
9840 origin = dwarf2_attr (child_die, DW_AT_abstract_origin, cu);
9841 if (loc == NULL && origin != NULL && is_ref_attr (origin))
9842 {
9843 sect_offset offset;
9844
9845 parameter->kind = CALL_SITE_PARAMETER_PARAM_OFFSET;
9846 offset = dwarf2_get_ref_die_offset (origin);
d76b7dbc
JK
9847 if (!offset_in_cu_p (&cu->header, offset))
9848 {
9849 /* As DW_OP_GNU_parameter_ref uses CU-relative offset this
9850 binding can be done only inside one CU. Such referenced DIE
9851 therefore cannot be even moved to DW_TAG_partial_unit. */
9852 complaint (&symfile_complaints,
9853 _("DW_AT_abstract_origin offset is not in CU for "
9854 "DW_TAG_GNU_call_site child DIE 0x%x "
9855 "[in module %s]"),
9856 child_die->offset.sect_off, objfile->name);
9857 continue;
9858 }
1788b2d3
JK
9859 parameter->u.param_offset.cu_off = (offset.sect_off
9860 - cu->header.offset.sect_off);
9861 }
9862 else if (loc == NULL || origin != NULL || !attr_form_is_block (loc))
96408a79
SA
9863 {
9864 complaint (&symfile_complaints,
9865 _("No DW_FORM_block* DW_AT_location for "
9866 "DW_TAG_GNU_call_site child DIE 0x%x [in module %s]"),
b64f50a1 9867 child_die->offset.sect_off, objfile->name);
96408a79
SA
9868 continue;
9869 }
24c5c679 9870 else
96408a79 9871 {
24c5c679
JK
9872 parameter->u.dwarf_reg = dwarf_block_to_dwarf_reg
9873 (DW_BLOCK (loc)->data, &DW_BLOCK (loc)->data[DW_BLOCK (loc)->size]);
9874 if (parameter->u.dwarf_reg != -1)
9875 parameter->kind = CALL_SITE_PARAMETER_DWARF_REG;
9876 else if (dwarf_block_to_sp_offset (gdbarch, DW_BLOCK (loc)->data,
9877 &DW_BLOCK (loc)->data[DW_BLOCK (loc)->size],
9878 &parameter->u.fb_offset))
9879 parameter->kind = CALL_SITE_PARAMETER_FB_OFFSET;
9880 else
9881 {
9882 complaint (&symfile_complaints,
9883 _("Only single DW_OP_reg or DW_OP_fbreg is supported "
9884 "for DW_FORM_block* DW_AT_location is supported for "
9885 "DW_TAG_GNU_call_site child DIE 0x%x "
9886 "[in module %s]"),
9887 child_die->offset.sect_off, objfile->name);
9888 continue;
9889 }
96408a79
SA
9890 }
9891
9892 attr = dwarf2_attr (child_die, DW_AT_GNU_call_site_value, cu);
9893 if (!attr_form_is_block (attr))
9894 {
9895 complaint (&symfile_complaints,
9896 _("No DW_FORM_block* DW_AT_GNU_call_site_value for "
9897 "DW_TAG_GNU_call_site child DIE 0x%x [in module %s]"),
b64f50a1 9898 child_die->offset.sect_off, objfile->name);
96408a79
SA
9899 continue;
9900 }
9901 parameter->value = DW_BLOCK (attr)->data;
9902 parameter->value_size = DW_BLOCK (attr)->size;
9903
9904 /* Parameters are not pre-cleared by memset above. */
9905 parameter->data_value = NULL;
9906 parameter->data_value_size = 0;
9907 call_site->parameter_count++;
9908
9909 attr = dwarf2_attr (child_die, DW_AT_GNU_call_site_data_value, cu);
9910 if (attr)
9911 {
9912 if (!attr_form_is_block (attr))
9913 complaint (&symfile_complaints,
9914 _("No DW_FORM_block* DW_AT_GNU_call_site_data_value for "
9915 "DW_TAG_GNU_call_site child DIE 0x%x [in module %s]"),
b64f50a1 9916 child_die->offset.sect_off, objfile->name);
96408a79
SA
9917 else
9918 {
9919 parameter->data_value = DW_BLOCK (attr)->data;
9920 parameter->data_value_size = DW_BLOCK (attr)->size;
9921 }
9922 }
9923 }
9924}
9925
43039443 9926/* Get low and high pc attributes from DW_AT_ranges attribute value OFFSET.
ff013f42
JK
9927 Return 1 if the attributes are present and valid, otherwise, return 0.
9928 If RANGES_PST is not NULL we should setup `objfile->psymtabs_addrmap'. */
43039443
JK
9929
9930static int
9931dwarf2_ranges_read (unsigned offset, CORE_ADDR *low_return,
ff013f42
JK
9932 CORE_ADDR *high_return, struct dwarf2_cu *cu,
9933 struct partial_symtab *ranges_pst)
43039443
JK
9934{
9935 struct objfile *objfile = cu->objfile;
9936 struct comp_unit_head *cu_header = &cu->header;
9937 bfd *obfd = objfile->obfd;
9938 unsigned int addr_size = cu_header->addr_size;
9939 CORE_ADDR mask = ~(~(CORE_ADDR)1 << (addr_size * 8 - 1));
9940 /* Base address selection entry. */
9941 CORE_ADDR base;
9942 int found_base;
9943 unsigned int dummy;
9944 gdb_byte *buffer;
9945 CORE_ADDR marker;
9946 int low_set;
9947 CORE_ADDR low = 0;
9948 CORE_ADDR high = 0;
ff013f42 9949 CORE_ADDR baseaddr;
43039443 9950
d00adf39
DE
9951 found_base = cu->base_known;
9952 base = cu->base_address;
43039443 9953
be391dca 9954 dwarf2_read_section (objfile, &dwarf2_per_objfile->ranges);
dce234bc 9955 if (offset >= dwarf2_per_objfile->ranges.size)
43039443
JK
9956 {
9957 complaint (&symfile_complaints,
9958 _("Offset %d out of bounds for DW_AT_ranges attribute"),
9959 offset);
9960 return 0;
9961 }
dce234bc 9962 buffer = dwarf2_per_objfile->ranges.buffer + offset;
43039443
JK
9963
9964 /* Read in the largest possible address. */
9965 marker = read_address (obfd, buffer, cu, &dummy);
9966 if ((marker & mask) == mask)
9967 {
9968 /* If we found the largest possible address, then
9969 read the base address. */
9970 base = read_address (obfd, buffer + addr_size, cu, &dummy);
9971 buffer += 2 * addr_size;
9972 offset += 2 * addr_size;
9973 found_base = 1;
9974 }
9975
9976 low_set = 0;
9977
e7030f15 9978 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
ff013f42 9979
43039443
JK
9980 while (1)
9981 {
9982 CORE_ADDR range_beginning, range_end;
9983
9984 range_beginning = read_address (obfd, buffer, cu, &dummy);
9985 buffer += addr_size;
9986 range_end = read_address (obfd, buffer, cu, &dummy);
9987 buffer += addr_size;
9988 offset += 2 * addr_size;
9989
9990 /* An end of list marker is a pair of zero addresses. */
9991 if (range_beginning == 0 && range_end == 0)
9992 /* Found the end of list entry. */
9993 break;
9994
9995 /* Each base address selection entry is a pair of 2 values.
9996 The first is the largest possible address, the second is
9997 the base address. Check for a base address here. */
9998 if ((range_beginning & mask) == mask)
9999 {
10000 /* If we found the largest possible address, then
10001 read the base address. */
10002 base = read_address (obfd, buffer + addr_size, cu, &dummy);
10003 found_base = 1;
10004 continue;
10005 }
10006
10007 if (!found_base)
10008 {
10009 /* We have no valid base address for the ranges
10010 data. */
10011 complaint (&symfile_complaints,
10012 _("Invalid .debug_ranges data (no base address)"));
10013 return 0;
10014 }
10015
9277c30c
UW
10016 if (range_beginning > range_end)
10017 {
10018 /* Inverted range entries are invalid. */
10019 complaint (&symfile_complaints,
10020 _("Invalid .debug_ranges data (inverted range)"));
10021 return 0;
10022 }
10023
10024 /* Empty range entries have no effect. */
10025 if (range_beginning == range_end)
10026 continue;
10027
43039443
JK
10028 range_beginning += base;
10029 range_end += base;
10030
01093045
DE
10031 /* A not-uncommon case of bad debug info.
10032 Don't pollute the addrmap with bad data. */
10033 if (range_beginning + baseaddr == 0
10034 && !dwarf2_per_objfile->has_section_at_zero)
10035 {
10036 complaint (&symfile_complaints,
10037 _(".debug_ranges entry has start address of zero"
10038 " [in module %s]"), objfile->name);
10039 continue;
10040 }
10041
9277c30c 10042 if (ranges_pst != NULL)
ff013f42 10043 addrmap_set_empty (objfile->psymtabs_addrmap,
3e43a32a
MS
10044 range_beginning + baseaddr,
10045 range_end - 1 + baseaddr,
ff013f42
JK
10046 ranges_pst);
10047
43039443
JK
10048 /* FIXME: This is recording everything as a low-high
10049 segment of consecutive addresses. We should have a
10050 data structure for discontiguous block ranges
10051 instead. */
10052 if (! low_set)
10053 {
10054 low = range_beginning;
10055 high = range_end;
10056 low_set = 1;
10057 }
10058 else
10059 {
10060 if (range_beginning < low)
10061 low = range_beginning;
10062 if (range_end > high)
10063 high = range_end;
10064 }
10065 }
10066
10067 if (! low_set)
10068 /* If the first entry is an end-of-list marker, the range
10069 describes an empty scope, i.e. no instructions. */
10070 return 0;
10071
10072 if (low_return)
10073 *low_return = low;
10074 if (high_return)
10075 *high_return = high;
10076 return 1;
10077}
10078
af34e669
DJ
10079/* Get low and high pc attributes from a die. Return 1 if the attributes
10080 are present and valid, otherwise, return 0. Return -1 if the range is
10081 discontinuous, i.e. derived from DW_AT_ranges information. */
380bca97 10082
c906108c 10083static int
af34e669 10084dwarf2_get_pc_bounds (struct die_info *die, CORE_ADDR *lowpc,
d85a05f0
DJ
10085 CORE_ADDR *highpc, struct dwarf2_cu *cu,
10086 struct partial_symtab *pst)
c906108c
SS
10087{
10088 struct attribute *attr;
91da1414 10089 struct attribute *attr_high;
af34e669
DJ
10090 CORE_ADDR low = 0;
10091 CORE_ADDR high = 0;
10092 int ret = 0;
c906108c 10093
91da1414
MW
10094 attr_high = dwarf2_attr (die, DW_AT_high_pc, cu);
10095 if (attr_high)
af34e669 10096 {
e142c38c 10097 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
af34e669 10098 if (attr)
91da1414
MW
10099 {
10100 low = DW_ADDR (attr);
3019eac3
DE
10101 if (attr_high->form == DW_FORM_addr
10102 || attr_high->form == DW_FORM_GNU_addr_index)
91da1414
MW
10103 high = DW_ADDR (attr_high);
10104 else
10105 high = low + DW_UNSND (attr_high);
10106 }
af34e669
DJ
10107 else
10108 /* Found high w/o low attribute. */
10109 return 0;
10110
10111 /* Found consecutive range of addresses. */
10112 ret = 1;
10113 }
c906108c 10114 else
af34e669 10115 {
e142c38c 10116 attr = dwarf2_attr (die, DW_AT_ranges, cu);
af34e669
DJ
10117 if (attr != NULL)
10118 {
ab435259
DE
10119 /* DW_AT_ranges_base does not apply to DIEs from the DWO skeleton.
10120 We take advantage of the fact that DW_AT_ranges does not appear
10121 in DW_TAG_compile_unit of DWO files. */
10122 int need_ranges_base = die->tag != DW_TAG_compile_unit;
10123 unsigned int ranges_offset = (DW_UNSND (attr)
10124 + (need_ranges_base
10125 ? cu->ranges_base
10126 : 0));
2e3cf129 10127
af34e669 10128 /* Value of the DW_AT_ranges attribute is the offset in the
a604369a 10129 .debug_ranges section. */
2e3cf129 10130 if (!dwarf2_ranges_read (ranges_offset, &low, &high, cu, pst))
af34e669 10131 return 0;
43039443 10132 /* Found discontinuous range of addresses. */
af34e669
DJ
10133 ret = -1;
10134 }
10135 }
c906108c 10136
9373cf26
JK
10137 /* read_partial_die has also the strict LOW < HIGH requirement. */
10138 if (high <= low)
c906108c
SS
10139 return 0;
10140
10141 /* When using the GNU linker, .gnu.linkonce. sections are used to
10142 eliminate duplicate copies of functions and vtables and such.
10143 The linker will arbitrarily choose one and discard the others.
10144 The AT_*_pc values for such functions refer to local labels in
10145 these sections. If the section from that file was discarded, the
10146 labels are not in the output, so the relocs get a value of 0.
10147 If this is a discarded function, mark the pc bounds as invalid,
10148 so that GDB will ignore it. */
72dca2f5 10149 if (low == 0 && !dwarf2_per_objfile->has_section_at_zero)
c906108c
SS
10150 return 0;
10151
10152 *lowpc = low;
96408a79
SA
10153 if (highpc)
10154 *highpc = high;
af34e669 10155 return ret;
c906108c
SS
10156}
10157
b084d499
JB
10158/* Assuming that DIE represents a subprogram DIE or a lexical block, get
10159 its low and high PC addresses. Do nothing if these addresses could not
10160 be determined. Otherwise, set LOWPC to the low address if it is smaller,
10161 and HIGHPC to the high address if greater than HIGHPC. */
10162
10163static void
10164dwarf2_get_subprogram_pc_bounds (struct die_info *die,
10165 CORE_ADDR *lowpc, CORE_ADDR *highpc,
10166 struct dwarf2_cu *cu)
10167{
10168 CORE_ADDR low, high;
10169 struct die_info *child = die->child;
10170
d85a05f0 10171 if (dwarf2_get_pc_bounds (die, &low, &high, cu, NULL))
b084d499
JB
10172 {
10173 *lowpc = min (*lowpc, low);
10174 *highpc = max (*highpc, high);
10175 }
10176
10177 /* If the language does not allow nested subprograms (either inside
10178 subprograms or lexical blocks), we're done. */
10179 if (cu->language != language_ada)
10180 return;
6e70227d 10181
b084d499
JB
10182 /* Check all the children of the given DIE. If it contains nested
10183 subprograms, then check their pc bounds. Likewise, we need to
10184 check lexical blocks as well, as they may also contain subprogram
10185 definitions. */
10186 while (child && child->tag)
10187 {
10188 if (child->tag == DW_TAG_subprogram
10189 || child->tag == DW_TAG_lexical_block)
10190 dwarf2_get_subprogram_pc_bounds (child, lowpc, highpc, cu);
10191 child = sibling_die (child);
10192 }
10193}
10194
fae299cd
DC
10195/* Get the low and high pc's represented by the scope DIE, and store
10196 them in *LOWPC and *HIGHPC. If the correct values can't be
10197 determined, set *LOWPC to -1 and *HIGHPC to 0. */
10198
10199static void
10200get_scope_pc_bounds (struct die_info *die,
10201 CORE_ADDR *lowpc, CORE_ADDR *highpc,
10202 struct dwarf2_cu *cu)
10203{
10204 CORE_ADDR best_low = (CORE_ADDR) -1;
10205 CORE_ADDR best_high = (CORE_ADDR) 0;
10206 CORE_ADDR current_low, current_high;
10207
d85a05f0 10208 if (dwarf2_get_pc_bounds (die, &current_low, &current_high, cu, NULL))
fae299cd
DC
10209 {
10210 best_low = current_low;
10211 best_high = current_high;
10212 }
10213 else
10214 {
10215 struct die_info *child = die->child;
10216
10217 while (child && child->tag)
10218 {
10219 switch (child->tag) {
10220 case DW_TAG_subprogram:
b084d499 10221 dwarf2_get_subprogram_pc_bounds (child, &best_low, &best_high, cu);
fae299cd
DC
10222 break;
10223 case DW_TAG_namespace:
f55ee35c 10224 case DW_TAG_module:
fae299cd
DC
10225 /* FIXME: carlton/2004-01-16: Should we do this for
10226 DW_TAG_class_type/DW_TAG_structure_type, too? I think
10227 that current GCC's always emit the DIEs corresponding
10228 to definitions of methods of classes as children of a
10229 DW_TAG_compile_unit or DW_TAG_namespace (as opposed to
10230 the DIEs giving the declarations, which could be
10231 anywhere). But I don't see any reason why the
10232 standards says that they have to be there. */
10233 get_scope_pc_bounds (child, &current_low, &current_high, cu);
10234
10235 if (current_low != ((CORE_ADDR) -1))
10236 {
10237 best_low = min (best_low, current_low);
10238 best_high = max (best_high, current_high);
10239 }
10240 break;
10241 default:
0963b4bd 10242 /* Ignore. */
fae299cd
DC
10243 break;
10244 }
10245
10246 child = sibling_die (child);
10247 }
10248 }
10249
10250 *lowpc = best_low;
10251 *highpc = best_high;
10252}
10253
801e3a5b
JB
10254/* Record the address ranges for BLOCK, offset by BASEADDR, as given
10255 in DIE. */
380bca97 10256
801e3a5b
JB
10257static void
10258dwarf2_record_block_ranges (struct die_info *die, struct block *block,
10259 CORE_ADDR baseaddr, struct dwarf2_cu *cu)
10260{
bb5ed363 10261 struct objfile *objfile = cu->objfile;
801e3a5b 10262 struct attribute *attr;
91da1414 10263 struct attribute *attr_high;
801e3a5b 10264
91da1414
MW
10265 attr_high = dwarf2_attr (die, DW_AT_high_pc, cu);
10266 if (attr_high)
801e3a5b 10267 {
801e3a5b
JB
10268 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
10269 if (attr)
10270 {
10271 CORE_ADDR low = DW_ADDR (attr);
91da1414 10272 CORE_ADDR high;
3019eac3
DE
10273 if (attr_high->form == DW_FORM_addr
10274 || attr_high->form == DW_FORM_GNU_addr_index)
91da1414
MW
10275 high = DW_ADDR (attr_high);
10276 else
10277 high = low + DW_UNSND (attr_high);
9a619af0 10278
801e3a5b
JB
10279 record_block_range (block, baseaddr + low, baseaddr + high - 1);
10280 }
10281 }
10282
10283 attr = dwarf2_attr (die, DW_AT_ranges, cu);
10284 if (attr)
10285 {
bb5ed363 10286 bfd *obfd = objfile->obfd;
ab435259
DE
10287 /* DW_AT_ranges_base does not apply to DIEs from the DWO skeleton.
10288 We take advantage of the fact that DW_AT_ranges does not appear
10289 in DW_TAG_compile_unit of DWO files. */
10290 int need_ranges_base = die->tag != DW_TAG_compile_unit;
801e3a5b
JB
10291
10292 /* The value of the DW_AT_ranges attribute is the offset of the
10293 address range list in the .debug_ranges section. */
ab435259
DE
10294 unsigned long offset = (DW_UNSND (attr)
10295 + (need_ranges_base ? cu->ranges_base : 0));
dce234bc 10296 gdb_byte *buffer = dwarf2_per_objfile->ranges.buffer + offset;
801e3a5b
JB
10297
10298 /* For some target architectures, but not others, the
10299 read_address function sign-extends the addresses it returns.
10300 To recognize base address selection entries, we need a
10301 mask. */
10302 unsigned int addr_size = cu->header.addr_size;
10303 CORE_ADDR base_select_mask = ~(~(CORE_ADDR)1 << (addr_size * 8 - 1));
10304
10305 /* The base address, to which the next pair is relative. Note
10306 that this 'base' is a DWARF concept: most entries in a range
10307 list are relative, to reduce the number of relocs against the
10308 debugging information. This is separate from this function's
10309 'baseaddr' argument, which GDB uses to relocate debugging
10310 information from a shared library based on the address at
10311 which the library was loaded. */
d00adf39
DE
10312 CORE_ADDR base = cu->base_address;
10313 int base_known = cu->base_known;
801e3a5b 10314
be391dca 10315 gdb_assert (dwarf2_per_objfile->ranges.readin);
dce234bc 10316 if (offset >= dwarf2_per_objfile->ranges.size)
801e3a5b
JB
10317 {
10318 complaint (&symfile_complaints,
10319 _("Offset %lu out of bounds for DW_AT_ranges attribute"),
10320 offset);
10321 return;
10322 }
10323
10324 for (;;)
10325 {
10326 unsigned int bytes_read;
10327 CORE_ADDR start, end;
10328
10329 start = read_address (obfd, buffer, cu, &bytes_read);
10330 buffer += bytes_read;
10331 end = read_address (obfd, buffer, cu, &bytes_read);
10332 buffer += bytes_read;
10333
10334 /* Did we find the end of the range list? */
10335 if (start == 0 && end == 0)
10336 break;
10337
10338 /* Did we find a base address selection entry? */
10339 else if ((start & base_select_mask) == base_select_mask)
10340 {
10341 base = end;
10342 base_known = 1;
10343 }
10344
10345 /* We found an ordinary address range. */
10346 else
10347 {
10348 if (!base_known)
10349 {
10350 complaint (&symfile_complaints,
3e43a32a
MS
10351 _("Invalid .debug_ranges data "
10352 "(no base address)"));
801e3a5b
JB
10353 return;
10354 }
10355
9277c30c
UW
10356 if (start > end)
10357 {
10358 /* Inverted range entries are invalid. */
10359 complaint (&symfile_complaints,
10360 _("Invalid .debug_ranges data "
10361 "(inverted range)"));
10362 return;
10363 }
10364
10365 /* Empty range entries have no effect. */
10366 if (start == end)
10367 continue;
10368
01093045
DE
10369 start += base + baseaddr;
10370 end += base + baseaddr;
10371
10372 /* A not-uncommon case of bad debug info.
10373 Don't pollute the addrmap with bad data. */
10374 if (start == 0 && !dwarf2_per_objfile->has_section_at_zero)
10375 {
10376 complaint (&symfile_complaints,
10377 _(".debug_ranges entry has start address of zero"
10378 " [in module %s]"), objfile->name);
10379 continue;
10380 }
10381
10382 record_block_range (block, start, end - 1);
801e3a5b
JB
10383 }
10384 }
10385 }
10386}
10387
685b1105
JK
10388/* Check whether the producer field indicates either of GCC < 4.6, or the
10389 Intel C/C++ compiler, and cache the result in CU. */
60d5a603 10390
685b1105
JK
10391static void
10392check_producer (struct dwarf2_cu *cu)
60d5a603
JK
10393{
10394 const char *cs;
10395 int major, minor, release;
10396
10397 if (cu->producer == NULL)
10398 {
10399 /* For unknown compilers expect their behavior is DWARF version
10400 compliant.
10401
10402 GCC started to support .debug_types sections by -gdwarf-4 since
10403 gcc-4.5.x. As the .debug_types sections are missing DW_AT_producer
10404 for their space efficiency GDB cannot workaround gcc-4.5.x -gdwarf-4
10405 combination. gcc-4.5.x -gdwarf-4 binaries have DW_AT_accessibility
10406 interpreted incorrectly by GDB now - GCC PR debug/48229. */
60d5a603 10407 }
685b1105 10408 else if (strncmp (cu->producer, "GNU ", strlen ("GNU ")) == 0)
60d5a603 10409 {
685b1105
JK
10410 /* Skip any identifier after "GNU " - such as "C++" or "Java". */
10411
ba919b58
TT
10412 cs = &cu->producer[strlen ("GNU ")];
10413 while (*cs && !isdigit (*cs))
10414 cs++;
10415 if (sscanf (cs, "%d.%d.%d", &major, &minor, &release) != 3)
10416 {
10417 /* Not recognized as GCC. */
10418 }
10419 else
1b80a9fa
JK
10420 {
10421 cu->producer_is_gxx_lt_4_6 = major < 4 || (major == 4 && minor < 6);
10422 cu->producer_is_gcc_lt_4_3 = major < 4 || (major == 4 && minor < 3);
10423 }
685b1105
JK
10424 }
10425 else if (strncmp (cu->producer, "Intel(R) C", strlen ("Intel(R) C")) == 0)
10426 cu->producer_is_icc = 1;
10427 else
10428 {
10429 /* For other non-GCC compilers, expect their behavior is DWARF version
10430 compliant. */
60d5a603
JK
10431 }
10432
ba919b58 10433 cu->checked_producer = 1;
685b1105 10434}
ba919b58 10435
685b1105
JK
10436/* Check for GCC PR debug/45124 fix which is not present in any G++ version up
10437 to 4.5.any while it is present already in G++ 4.6.0 - the PR has been fixed
10438 during 4.6.0 experimental. */
10439
10440static int
10441producer_is_gxx_lt_4_6 (struct dwarf2_cu *cu)
10442{
10443 if (!cu->checked_producer)
10444 check_producer (cu);
10445
10446 return cu->producer_is_gxx_lt_4_6;
60d5a603
JK
10447}
10448
10449/* Return the default accessibility type if it is not overriden by
10450 DW_AT_accessibility. */
10451
10452static enum dwarf_access_attribute
10453dwarf2_default_access_attribute (struct die_info *die, struct dwarf2_cu *cu)
10454{
10455 if (cu->header.version < 3 || producer_is_gxx_lt_4_6 (cu))
10456 {
10457 /* The default DWARF 2 accessibility for members is public, the default
10458 accessibility for inheritance is private. */
10459
10460 if (die->tag != DW_TAG_inheritance)
10461 return DW_ACCESS_public;
10462 else
10463 return DW_ACCESS_private;
10464 }
10465 else
10466 {
10467 /* DWARF 3+ defines the default accessibility a different way. The same
10468 rules apply now for DW_TAG_inheritance as for the members and it only
10469 depends on the container kind. */
10470
10471 if (die->parent->tag == DW_TAG_class_type)
10472 return DW_ACCESS_private;
10473 else
10474 return DW_ACCESS_public;
10475 }
10476}
10477
74ac6d43
TT
10478/* Look for DW_AT_data_member_location. Set *OFFSET to the byte
10479 offset. If the attribute was not found return 0, otherwise return
10480 1. If it was found but could not properly be handled, set *OFFSET
10481 to 0. */
10482
10483static int
10484handle_data_member_location (struct die_info *die, struct dwarf2_cu *cu,
10485 LONGEST *offset)
10486{
10487 struct attribute *attr;
10488
10489 attr = dwarf2_attr (die, DW_AT_data_member_location, cu);
10490 if (attr != NULL)
10491 {
10492 *offset = 0;
10493
10494 /* Note that we do not check for a section offset first here.
10495 This is because DW_AT_data_member_location is new in DWARF 4,
10496 so if we see it, we can assume that a constant form is really
10497 a constant and not a section offset. */
10498 if (attr_form_is_constant (attr))
10499 *offset = dwarf2_get_attr_constant_value (attr, 0);
10500 else if (attr_form_is_section_offset (attr))
10501 dwarf2_complex_location_expr_complaint ();
10502 else if (attr_form_is_block (attr))
10503 *offset = decode_locdesc (DW_BLOCK (attr), cu);
10504 else
10505 dwarf2_complex_location_expr_complaint ();
10506
10507 return 1;
10508 }
10509
10510 return 0;
10511}
10512
c906108c
SS
10513/* Add an aggregate field to the field list. */
10514
10515static void
107d2387 10516dwarf2_add_field (struct field_info *fip, struct die_info *die,
e7c27a73 10517 struct dwarf2_cu *cu)
6e70227d 10518{
e7c27a73 10519 struct objfile *objfile = cu->objfile;
5e2b427d 10520 struct gdbarch *gdbarch = get_objfile_arch (objfile);
c906108c
SS
10521 struct nextfield *new_field;
10522 struct attribute *attr;
10523 struct field *fp;
15d034d0 10524 const char *fieldname = "";
c906108c
SS
10525
10526 /* Allocate a new field list entry and link it in. */
10527 new_field = (struct nextfield *) xmalloc (sizeof (struct nextfield));
b8c9b27d 10528 make_cleanup (xfree, new_field);
c906108c 10529 memset (new_field, 0, sizeof (struct nextfield));
7d0ccb61
DJ
10530
10531 if (die->tag == DW_TAG_inheritance)
10532 {
10533 new_field->next = fip->baseclasses;
10534 fip->baseclasses = new_field;
10535 }
10536 else
10537 {
10538 new_field->next = fip->fields;
10539 fip->fields = new_field;
10540 }
c906108c
SS
10541 fip->nfields++;
10542
e142c38c 10543 attr = dwarf2_attr (die, DW_AT_accessibility, cu);
c906108c
SS
10544 if (attr)
10545 new_field->accessibility = DW_UNSND (attr);
60d5a603
JK
10546 else
10547 new_field->accessibility = dwarf2_default_access_attribute (die, cu);
c906108c
SS
10548 if (new_field->accessibility != DW_ACCESS_public)
10549 fip->non_public_fields = 1;
60d5a603 10550
e142c38c 10551 attr = dwarf2_attr (die, DW_AT_virtuality, cu);
c906108c
SS
10552 if (attr)
10553 new_field->virtuality = DW_UNSND (attr);
60d5a603
JK
10554 else
10555 new_field->virtuality = DW_VIRTUALITY_none;
c906108c
SS
10556
10557 fp = &new_field->field;
a9a9bd0f 10558
e142c38c 10559 if (die->tag == DW_TAG_member && ! die_is_declaration (die, cu))
c906108c 10560 {
74ac6d43
TT
10561 LONGEST offset;
10562
a9a9bd0f 10563 /* Data member other than a C++ static data member. */
6e70227d 10564
c906108c 10565 /* Get type of field. */
e7c27a73 10566 fp->type = die_type (die, cu);
c906108c 10567
d6a843b5 10568 SET_FIELD_BITPOS (*fp, 0);
01ad7f36 10569
c906108c 10570 /* Get bit size of field (zero if none). */
e142c38c 10571 attr = dwarf2_attr (die, DW_AT_bit_size, cu);
c906108c
SS
10572 if (attr)
10573 {
10574 FIELD_BITSIZE (*fp) = DW_UNSND (attr);
10575 }
10576 else
10577 {
10578 FIELD_BITSIZE (*fp) = 0;
10579 }
10580
10581 /* Get bit offset of field. */
74ac6d43
TT
10582 if (handle_data_member_location (die, cu, &offset))
10583 SET_FIELD_BITPOS (*fp, offset * bits_per_byte);
e142c38c 10584 attr = dwarf2_attr (die, DW_AT_bit_offset, cu);
c906108c
SS
10585 if (attr)
10586 {
5e2b427d 10587 if (gdbarch_bits_big_endian (gdbarch))
c906108c
SS
10588 {
10589 /* For big endian bits, the DW_AT_bit_offset gives the
c5aa993b
JM
10590 additional bit offset from the MSB of the containing
10591 anonymous object to the MSB of the field. We don't
10592 have to do anything special since we don't need to
10593 know the size of the anonymous object. */
f41f5e61 10594 SET_FIELD_BITPOS (*fp, FIELD_BITPOS (*fp) + DW_UNSND (attr));
c906108c
SS
10595 }
10596 else
10597 {
10598 /* For little endian bits, compute the bit offset to the
c5aa993b
JM
10599 MSB of the anonymous object, subtract off the number of
10600 bits from the MSB of the field to the MSB of the
10601 object, and then subtract off the number of bits of
10602 the field itself. The result is the bit offset of
10603 the LSB of the field. */
c906108c
SS
10604 int anonymous_size;
10605 int bit_offset = DW_UNSND (attr);
10606
e142c38c 10607 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
c906108c
SS
10608 if (attr)
10609 {
10610 /* The size of the anonymous object containing
10611 the bit field is explicit, so use the
10612 indicated size (in bytes). */
10613 anonymous_size = DW_UNSND (attr);
10614 }
10615 else
10616 {
10617 /* The size of the anonymous object containing
10618 the bit field must be inferred from the type
10619 attribute of the data member containing the
10620 bit field. */
10621 anonymous_size = TYPE_LENGTH (fp->type);
10622 }
f41f5e61
PA
10623 SET_FIELD_BITPOS (*fp,
10624 (FIELD_BITPOS (*fp)
10625 + anonymous_size * bits_per_byte
10626 - bit_offset - FIELD_BITSIZE (*fp)));
c906108c
SS
10627 }
10628 }
10629
10630 /* Get name of field. */
39cbfefa
DJ
10631 fieldname = dwarf2_name (die, cu);
10632 if (fieldname == NULL)
10633 fieldname = "";
d8151005
DJ
10634
10635 /* The name is already allocated along with this objfile, so we don't
10636 need to duplicate it for the type. */
10637 fp->name = fieldname;
c906108c
SS
10638
10639 /* Change accessibility for artificial fields (e.g. virtual table
c5aa993b 10640 pointer or virtual base class pointer) to private. */
e142c38c 10641 if (dwarf2_attr (die, DW_AT_artificial, cu))
c906108c 10642 {
d48cc9dd 10643 FIELD_ARTIFICIAL (*fp) = 1;
c906108c
SS
10644 new_field->accessibility = DW_ACCESS_private;
10645 fip->non_public_fields = 1;
10646 }
10647 }
a9a9bd0f 10648 else if (die->tag == DW_TAG_member || die->tag == DW_TAG_variable)
c906108c 10649 {
a9a9bd0f
DC
10650 /* C++ static member. */
10651
10652 /* NOTE: carlton/2002-11-05: It should be a DW_TAG_member that
10653 is a declaration, but all versions of G++ as of this writing
10654 (so through at least 3.2.1) incorrectly generate
10655 DW_TAG_variable tags. */
6e70227d 10656
ff355380 10657 const char *physname;
c906108c 10658
a9a9bd0f 10659 /* Get name of field. */
39cbfefa
DJ
10660 fieldname = dwarf2_name (die, cu);
10661 if (fieldname == NULL)
c906108c
SS
10662 return;
10663
254e6b9e 10664 attr = dwarf2_attr (die, DW_AT_const_value, cu);
3863f96c
DE
10665 if (attr
10666 /* Only create a symbol if this is an external value.
10667 new_symbol checks this and puts the value in the global symbol
10668 table, which we want. If it is not external, new_symbol
10669 will try to put the value in cu->list_in_scope which is wrong. */
10670 && dwarf2_flag_true_p (die, DW_AT_external, cu))
254e6b9e
DE
10671 {
10672 /* A static const member, not much different than an enum as far as
10673 we're concerned, except that we can support more types. */
10674 new_symbol (die, NULL, cu);
10675 }
10676
2df3850c 10677 /* Get physical name. */
ff355380 10678 physname = dwarf2_physname (fieldname, die, cu);
c906108c 10679
d8151005
DJ
10680 /* The name is already allocated along with this objfile, so we don't
10681 need to duplicate it for the type. */
10682 SET_FIELD_PHYSNAME (*fp, physname ? physname : "");
e7c27a73 10683 FIELD_TYPE (*fp) = die_type (die, cu);
d8151005 10684 FIELD_NAME (*fp) = fieldname;
c906108c
SS
10685 }
10686 else if (die->tag == DW_TAG_inheritance)
10687 {
74ac6d43 10688 LONGEST offset;
d4b96c9a 10689
74ac6d43
TT
10690 /* C++ base class field. */
10691 if (handle_data_member_location (die, cu, &offset))
10692 SET_FIELD_BITPOS (*fp, offset * bits_per_byte);
c906108c 10693 FIELD_BITSIZE (*fp) = 0;
e7c27a73 10694 FIELD_TYPE (*fp) = die_type (die, cu);
c906108c
SS
10695 FIELD_NAME (*fp) = type_name_no_tag (fp->type);
10696 fip->nbaseclasses++;
10697 }
10698}
10699
98751a41
JK
10700/* Add a typedef defined in the scope of the FIP's class. */
10701
10702static void
10703dwarf2_add_typedef (struct field_info *fip, struct die_info *die,
10704 struct dwarf2_cu *cu)
6e70227d 10705{
98751a41 10706 struct objfile *objfile = cu->objfile;
98751a41
JK
10707 struct typedef_field_list *new_field;
10708 struct attribute *attr;
10709 struct typedef_field *fp;
10710 char *fieldname = "";
10711
10712 /* Allocate a new field list entry and link it in. */
10713 new_field = xzalloc (sizeof (*new_field));
10714 make_cleanup (xfree, new_field);
10715
10716 gdb_assert (die->tag == DW_TAG_typedef);
10717
10718 fp = &new_field->field;
10719
10720 /* Get name of field. */
10721 fp->name = dwarf2_name (die, cu);
10722 if (fp->name == NULL)
10723 return;
10724
10725 fp->type = read_type_die (die, cu);
10726
10727 new_field->next = fip->typedef_field_list;
10728 fip->typedef_field_list = new_field;
10729 fip->typedef_field_list_count++;
10730}
10731
c906108c
SS
10732/* Create the vector of fields, and attach it to the type. */
10733
10734static void
fba45db2 10735dwarf2_attach_fields_to_type (struct field_info *fip, struct type *type,
e7c27a73 10736 struct dwarf2_cu *cu)
c906108c
SS
10737{
10738 int nfields = fip->nfields;
10739
10740 /* Record the field count, allocate space for the array of fields,
10741 and create blank accessibility bitfields if necessary. */
10742 TYPE_NFIELDS (type) = nfields;
10743 TYPE_FIELDS (type) = (struct field *)
10744 TYPE_ALLOC (type, sizeof (struct field) * nfields);
10745 memset (TYPE_FIELDS (type), 0, sizeof (struct field) * nfields);
10746
b4ba55a1 10747 if (fip->non_public_fields && cu->language != language_ada)
c906108c
SS
10748 {
10749 ALLOCATE_CPLUS_STRUCT_TYPE (type);
10750
10751 TYPE_FIELD_PRIVATE_BITS (type) =
10752 (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
10753 B_CLRALL (TYPE_FIELD_PRIVATE_BITS (type), nfields);
10754
10755 TYPE_FIELD_PROTECTED_BITS (type) =
10756 (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
10757 B_CLRALL (TYPE_FIELD_PROTECTED_BITS (type), nfields);
10758
774b6a14
TT
10759 TYPE_FIELD_IGNORE_BITS (type) =
10760 (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
10761 B_CLRALL (TYPE_FIELD_IGNORE_BITS (type), nfields);
c906108c
SS
10762 }
10763
10764 /* If the type has baseclasses, allocate and clear a bit vector for
10765 TYPE_FIELD_VIRTUAL_BITS. */
b4ba55a1 10766 if (fip->nbaseclasses && cu->language != language_ada)
c906108c
SS
10767 {
10768 int num_bytes = B_BYTES (fip->nbaseclasses);
fe1b8b76 10769 unsigned char *pointer;
c906108c
SS
10770
10771 ALLOCATE_CPLUS_STRUCT_TYPE (type);
fe1b8b76
JB
10772 pointer = TYPE_ALLOC (type, num_bytes);
10773 TYPE_FIELD_VIRTUAL_BITS (type) = pointer;
c906108c
SS
10774 B_CLRALL (TYPE_FIELD_VIRTUAL_BITS (type), fip->nbaseclasses);
10775 TYPE_N_BASECLASSES (type) = fip->nbaseclasses;
10776 }
10777
3e43a32a
MS
10778 /* Copy the saved-up fields into the field vector. Start from the head of
10779 the list, adding to the tail of the field array, so that they end up in
10780 the same order in the array in which they were added to the list. */
c906108c
SS
10781 while (nfields-- > 0)
10782 {
7d0ccb61
DJ
10783 struct nextfield *fieldp;
10784
10785 if (fip->fields)
10786 {
10787 fieldp = fip->fields;
10788 fip->fields = fieldp->next;
10789 }
10790 else
10791 {
10792 fieldp = fip->baseclasses;
10793 fip->baseclasses = fieldp->next;
10794 }
10795
10796 TYPE_FIELD (type, nfields) = fieldp->field;
10797 switch (fieldp->accessibility)
c906108c 10798 {
c5aa993b 10799 case DW_ACCESS_private:
b4ba55a1
JB
10800 if (cu->language != language_ada)
10801 SET_TYPE_FIELD_PRIVATE (type, nfields);
c5aa993b 10802 break;
c906108c 10803
c5aa993b 10804 case DW_ACCESS_protected:
b4ba55a1
JB
10805 if (cu->language != language_ada)
10806 SET_TYPE_FIELD_PROTECTED (type, nfields);
c5aa993b 10807 break;
c906108c 10808
c5aa993b
JM
10809 case DW_ACCESS_public:
10810 break;
c906108c 10811
c5aa993b
JM
10812 default:
10813 /* Unknown accessibility. Complain and treat it as public. */
10814 {
e2e0b3e5 10815 complaint (&symfile_complaints, _("unsupported accessibility %d"),
7d0ccb61 10816 fieldp->accessibility);
c5aa993b
JM
10817 }
10818 break;
c906108c
SS
10819 }
10820 if (nfields < fip->nbaseclasses)
10821 {
7d0ccb61 10822 switch (fieldp->virtuality)
c906108c 10823 {
c5aa993b
JM
10824 case DW_VIRTUALITY_virtual:
10825 case DW_VIRTUALITY_pure_virtual:
b4ba55a1 10826 if (cu->language == language_ada)
a73c6dcd 10827 error (_("unexpected virtuality in component of Ada type"));
c5aa993b
JM
10828 SET_TYPE_FIELD_VIRTUAL (type, nfields);
10829 break;
c906108c
SS
10830 }
10831 }
c906108c
SS
10832 }
10833}
10834
7d27a96d
TT
10835/* Return true if this member function is a constructor, false
10836 otherwise. */
10837
10838static int
10839dwarf2_is_constructor (struct die_info *die, struct dwarf2_cu *cu)
10840{
10841 const char *fieldname;
10842 const char *typename;
10843 int len;
10844
10845 if (die->parent == NULL)
10846 return 0;
10847
10848 if (die->parent->tag != DW_TAG_structure_type
10849 && die->parent->tag != DW_TAG_union_type
10850 && die->parent->tag != DW_TAG_class_type)
10851 return 0;
10852
10853 fieldname = dwarf2_name (die, cu);
10854 typename = dwarf2_name (die->parent, cu);
10855 if (fieldname == NULL || typename == NULL)
10856 return 0;
10857
10858 len = strlen (fieldname);
10859 return (strncmp (fieldname, typename, len) == 0
10860 && (typename[len] == '\0' || typename[len] == '<'));
10861}
10862
c906108c
SS
10863/* Add a member function to the proper fieldlist. */
10864
10865static void
107d2387 10866dwarf2_add_member_fn (struct field_info *fip, struct die_info *die,
e7c27a73 10867 struct type *type, struct dwarf2_cu *cu)
c906108c 10868{
e7c27a73 10869 struct objfile *objfile = cu->objfile;
c906108c
SS
10870 struct attribute *attr;
10871 struct fnfieldlist *flp;
10872 int i;
10873 struct fn_field *fnp;
15d034d0 10874 const char *fieldname;
c906108c 10875 struct nextfnfield *new_fnfield;
f792889a 10876 struct type *this_type;
60d5a603 10877 enum dwarf_access_attribute accessibility;
c906108c 10878
b4ba55a1 10879 if (cu->language == language_ada)
a73c6dcd 10880 error (_("unexpected member function in Ada type"));
b4ba55a1 10881
2df3850c 10882 /* Get name of member function. */
39cbfefa
DJ
10883 fieldname = dwarf2_name (die, cu);
10884 if (fieldname == NULL)
2df3850c 10885 return;
c906108c 10886
c906108c
SS
10887 /* Look up member function name in fieldlist. */
10888 for (i = 0; i < fip->nfnfields; i++)
10889 {
27bfe10e 10890 if (strcmp (fip->fnfieldlists[i].name, fieldname) == 0)
c906108c
SS
10891 break;
10892 }
10893
10894 /* Create new list element if necessary. */
10895 if (i < fip->nfnfields)
10896 flp = &fip->fnfieldlists[i];
10897 else
10898 {
10899 if ((fip->nfnfields % DW_FIELD_ALLOC_CHUNK) == 0)
10900 {
10901 fip->fnfieldlists = (struct fnfieldlist *)
10902 xrealloc (fip->fnfieldlists,
10903 (fip->nfnfields + DW_FIELD_ALLOC_CHUNK)
c5aa993b 10904 * sizeof (struct fnfieldlist));
c906108c 10905 if (fip->nfnfields == 0)
c13c43fd 10906 make_cleanup (free_current_contents, &fip->fnfieldlists);
c906108c
SS
10907 }
10908 flp = &fip->fnfieldlists[fip->nfnfields];
10909 flp->name = fieldname;
10910 flp->length = 0;
10911 flp->head = NULL;
3da10d80 10912 i = fip->nfnfields++;
c906108c
SS
10913 }
10914
10915 /* Create a new member function field and chain it to the field list
0963b4bd 10916 entry. */
c906108c 10917 new_fnfield = (struct nextfnfield *) xmalloc (sizeof (struct nextfnfield));
b8c9b27d 10918 make_cleanup (xfree, new_fnfield);
c906108c
SS
10919 memset (new_fnfield, 0, sizeof (struct nextfnfield));
10920 new_fnfield->next = flp->head;
10921 flp->head = new_fnfield;
10922 flp->length++;
10923
10924 /* Fill in the member function field info. */
10925 fnp = &new_fnfield->fnfield;
3da10d80
KS
10926
10927 /* Delay processing of the physname until later. */
10928 if (cu->language == language_cplus || cu->language == language_java)
10929 {
10930 add_to_method_list (type, i, flp->length - 1, fieldname,
10931 die, cu);
10932 }
10933 else
10934 {
1d06ead6 10935 const char *physname = dwarf2_physname (fieldname, die, cu);
3da10d80
KS
10936 fnp->physname = physname ? physname : "";
10937 }
10938
c906108c 10939 fnp->type = alloc_type (objfile);
f792889a
DJ
10940 this_type = read_type_die (die, cu);
10941 if (this_type && TYPE_CODE (this_type) == TYPE_CODE_FUNC)
c906108c 10942 {
f792889a 10943 int nparams = TYPE_NFIELDS (this_type);
c906108c 10944
f792889a 10945 /* TYPE is the domain of this method, and THIS_TYPE is the type
e26fb1d7
DC
10946 of the method itself (TYPE_CODE_METHOD). */
10947 smash_to_method_type (fnp->type, type,
f792889a
DJ
10948 TYPE_TARGET_TYPE (this_type),
10949 TYPE_FIELDS (this_type),
10950 TYPE_NFIELDS (this_type),
10951 TYPE_VARARGS (this_type));
c906108c
SS
10952
10953 /* Handle static member functions.
c5aa993b 10954 Dwarf2 has no clean way to discern C++ static and non-static
0963b4bd
MS
10955 member functions. G++ helps GDB by marking the first
10956 parameter for non-static member functions (which is the this
10957 pointer) as artificial. We obtain this information from
10958 read_subroutine_type via TYPE_FIELD_ARTIFICIAL. */
f792889a 10959 if (nparams == 0 || TYPE_FIELD_ARTIFICIAL (this_type, 0) == 0)
c906108c
SS
10960 fnp->voffset = VOFFSET_STATIC;
10961 }
10962 else
e2e0b3e5 10963 complaint (&symfile_complaints, _("member function type missing for '%s'"),
3da10d80 10964 dwarf2_full_name (fieldname, die, cu));
c906108c
SS
10965
10966 /* Get fcontext from DW_AT_containing_type if present. */
e142c38c 10967 if (dwarf2_attr (die, DW_AT_containing_type, cu) != NULL)
e7c27a73 10968 fnp->fcontext = die_containing_type (die, cu);
c906108c 10969
3e43a32a
MS
10970 /* dwarf2 doesn't have stubbed physical names, so the setting of is_const and
10971 is_volatile is irrelevant, as it is needed by gdb_mangle_name only. */
c906108c
SS
10972
10973 /* Get accessibility. */
e142c38c 10974 attr = dwarf2_attr (die, DW_AT_accessibility, cu);
c906108c 10975 if (attr)
60d5a603
JK
10976 accessibility = DW_UNSND (attr);
10977 else
10978 accessibility = dwarf2_default_access_attribute (die, cu);
10979 switch (accessibility)
c906108c 10980 {
60d5a603
JK
10981 case DW_ACCESS_private:
10982 fnp->is_private = 1;
10983 break;
10984 case DW_ACCESS_protected:
10985 fnp->is_protected = 1;
10986 break;
c906108c
SS
10987 }
10988
b02dede2 10989 /* Check for artificial methods. */
e142c38c 10990 attr = dwarf2_attr (die, DW_AT_artificial, cu);
b02dede2
DJ
10991 if (attr && DW_UNSND (attr) != 0)
10992 fnp->is_artificial = 1;
10993
7d27a96d
TT
10994 fnp->is_constructor = dwarf2_is_constructor (die, cu);
10995
0d564a31 10996 /* Get index in virtual function table if it is a virtual member
aec5aa8b
TT
10997 function. For older versions of GCC, this is an offset in the
10998 appropriate virtual table, as specified by DW_AT_containing_type.
10999 For everyone else, it is an expression to be evaluated relative
0d564a31
DJ
11000 to the object address. */
11001
e142c38c 11002 attr = dwarf2_attr (die, DW_AT_vtable_elem_location, cu);
aec5aa8b 11003 if (attr)
8e19ed76 11004 {
aec5aa8b 11005 if (attr_form_is_block (attr) && DW_BLOCK (attr)->size > 0)
8e19ed76 11006 {
aec5aa8b
TT
11007 if (DW_BLOCK (attr)->data[0] == DW_OP_constu)
11008 {
11009 /* Old-style GCC. */
11010 fnp->voffset = decode_locdesc (DW_BLOCK (attr), cu) + 2;
11011 }
11012 else if (DW_BLOCK (attr)->data[0] == DW_OP_deref
11013 || (DW_BLOCK (attr)->size > 1
11014 && DW_BLOCK (attr)->data[0] == DW_OP_deref_size
11015 && DW_BLOCK (attr)->data[1] == cu->header.addr_size))
11016 {
11017 struct dwarf_block blk;
11018 int offset;
11019
11020 offset = (DW_BLOCK (attr)->data[0] == DW_OP_deref
11021 ? 1 : 2);
11022 blk.size = DW_BLOCK (attr)->size - offset;
11023 blk.data = DW_BLOCK (attr)->data + offset;
11024 fnp->voffset = decode_locdesc (DW_BLOCK (attr), cu);
11025 if ((fnp->voffset % cu->header.addr_size) != 0)
11026 dwarf2_complex_location_expr_complaint ();
11027 else
11028 fnp->voffset /= cu->header.addr_size;
11029 fnp->voffset += 2;
11030 }
11031 else
11032 dwarf2_complex_location_expr_complaint ();
11033
11034 if (!fnp->fcontext)
11035 fnp->fcontext = TYPE_TARGET_TYPE (TYPE_FIELD_TYPE (this_type, 0));
11036 }
3690dd37 11037 else if (attr_form_is_section_offset (attr))
8e19ed76 11038 {
4d3c2250 11039 dwarf2_complex_location_expr_complaint ();
8e19ed76
PS
11040 }
11041 else
11042 {
4d3c2250
KB
11043 dwarf2_invalid_attrib_class_complaint ("DW_AT_vtable_elem_location",
11044 fieldname);
8e19ed76 11045 }
0d564a31 11046 }
d48cc9dd
DJ
11047 else
11048 {
11049 attr = dwarf2_attr (die, DW_AT_virtuality, cu);
11050 if (attr && DW_UNSND (attr))
11051 {
11052 /* GCC does this, as of 2008-08-25; PR debug/37237. */
11053 complaint (&symfile_complaints,
3e43a32a
MS
11054 _("Member function \"%s\" (offset %d) is virtual "
11055 "but the vtable offset is not specified"),
b64f50a1 11056 fieldname, die->offset.sect_off);
9655fd1a 11057 ALLOCATE_CPLUS_STRUCT_TYPE (type);
d48cc9dd
DJ
11058 TYPE_CPLUS_DYNAMIC (type) = 1;
11059 }
11060 }
c906108c
SS
11061}
11062
11063/* Create the vector of member function fields, and attach it to the type. */
11064
11065static void
fba45db2 11066dwarf2_attach_fn_fields_to_type (struct field_info *fip, struct type *type,
e7c27a73 11067 struct dwarf2_cu *cu)
c906108c
SS
11068{
11069 struct fnfieldlist *flp;
c906108c
SS
11070 int i;
11071
b4ba55a1 11072 if (cu->language == language_ada)
a73c6dcd 11073 error (_("unexpected member functions in Ada type"));
b4ba55a1 11074
c906108c
SS
11075 ALLOCATE_CPLUS_STRUCT_TYPE (type);
11076 TYPE_FN_FIELDLISTS (type) = (struct fn_fieldlist *)
11077 TYPE_ALLOC (type, sizeof (struct fn_fieldlist) * fip->nfnfields);
11078
11079 for (i = 0, flp = fip->fnfieldlists; i < fip->nfnfields; i++, flp++)
11080 {
11081 struct nextfnfield *nfp = flp->head;
11082 struct fn_fieldlist *fn_flp = &TYPE_FN_FIELDLIST (type, i);
11083 int k;
11084
11085 TYPE_FN_FIELDLIST_NAME (type, i) = flp->name;
11086 TYPE_FN_FIELDLIST_LENGTH (type, i) = flp->length;
11087 fn_flp->fn_fields = (struct fn_field *)
11088 TYPE_ALLOC (type, sizeof (struct fn_field) * flp->length);
11089 for (k = flp->length; (k--, nfp); nfp = nfp->next)
c5aa993b 11090 fn_flp->fn_fields[k] = nfp->fnfield;
c906108c
SS
11091 }
11092
11093 TYPE_NFN_FIELDS (type) = fip->nfnfields;
c906108c
SS
11094}
11095
1168df01
JB
11096/* Returns non-zero if NAME is the name of a vtable member in CU's
11097 language, zero otherwise. */
11098static int
11099is_vtable_name (const char *name, struct dwarf2_cu *cu)
11100{
11101 static const char vptr[] = "_vptr";
987504bb 11102 static const char vtable[] = "vtable";
1168df01 11103
987504bb
JJ
11104 /* Look for the C++ and Java forms of the vtable. */
11105 if ((cu->language == language_java
11106 && strncmp (name, vtable, sizeof (vtable) - 1) == 0)
11107 || (strncmp (name, vptr, sizeof (vptr) - 1) == 0
11108 && is_cplus_marker (name[sizeof (vptr) - 1])))
1168df01
JB
11109 return 1;
11110
11111 return 0;
11112}
11113
c0dd20ea 11114/* GCC outputs unnamed structures that are really pointers to member
0b92b5bb
TT
11115 functions, with the ABI-specified layout. If TYPE describes
11116 such a structure, smash it into a member function type.
61049d3b
DJ
11117
11118 GCC shouldn't do this; it should just output pointer to member DIEs.
11119 This is GCC PR debug/28767. */
c0dd20ea 11120
0b92b5bb
TT
11121static void
11122quirk_gcc_member_function_pointer (struct type *type, struct objfile *objfile)
c0dd20ea 11123{
0b92b5bb 11124 struct type *pfn_type, *domain_type, *new_type;
c0dd20ea
DJ
11125
11126 /* Check for a structure with no name and two children. */
0b92b5bb
TT
11127 if (TYPE_CODE (type) != TYPE_CODE_STRUCT || TYPE_NFIELDS (type) != 2)
11128 return;
c0dd20ea
DJ
11129
11130 /* Check for __pfn and __delta members. */
0b92b5bb
TT
11131 if (TYPE_FIELD_NAME (type, 0) == NULL
11132 || strcmp (TYPE_FIELD_NAME (type, 0), "__pfn") != 0
11133 || TYPE_FIELD_NAME (type, 1) == NULL
11134 || strcmp (TYPE_FIELD_NAME (type, 1), "__delta") != 0)
11135 return;
c0dd20ea
DJ
11136
11137 /* Find the type of the method. */
0b92b5bb 11138 pfn_type = TYPE_FIELD_TYPE (type, 0);
c0dd20ea
DJ
11139 if (pfn_type == NULL
11140 || TYPE_CODE (pfn_type) != TYPE_CODE_PTR
11141 || TYPE_CODE (TYPE_TARGET_TYPE (pfn_type)) != TYPE_CODE_FUNC)
0b92b5bb 11142 return;
c0dd20ea
DJ
11143
11144 /* Look for the "this" argument. */
11145 pfn_type = TYPE_TARGET_TYPE (pfn_type);
11146 if (TYPE_NFIELDS (pfn_type) == 0
0b92b5bb 11147 /* || TYPE_FIELD_TYPE (pfn_type, 0) == NULL */
c0dd20ea 11148 || TYPE_CODE (TYPE_FIELD_TYPE (pfn_type, 0)) != TYPE_CODE_PTR)
0b92b5bb 11149 return;
c0dd20ea
DJ
11150
11151 domain_type = TYPE_TARGET_TYPE (TYPE_FIELD_TYPE (pfn_type, 0));
0b92b5bb
TT
11152 new_type = alloc_type (objfile);
11153 smash_to_method_type (new_type, domain_type, TYPE_TARGET_TYPE (pfn_type),
c0dd20ea
DJ
11154 TYPE_FIELDS (pfn_type), TYPE_NFIELDS (pfn_type),
11155 TYPE_VARARGS (pfn_type));
0b92b5bb 11156 smash_to_methodptr_type (type, new_type);
c0dd20ea 11157}
1168df01 11158
685b1105
JK
11159/* Return non-zero if the CU's PRODUCER string matches the Intel C/C++ compiler
11160 (icc). */
11161
11162static int
11163producer_is_icc (struct dwarf2_cu *cu)
11164{
11165 if (!cu->checked_producer)
11166 check_producer (cu);
11167
11168 return cu->producer_is_icc;
11169}
11170
c906108c 11171/* Called when we find the DIE that starts a structure or union scope
c767944b
DJ
11172 (definition) to create a type for the structure or union. Fill in
11173 the type's name and general properties; the members will not be
11174 processed until process_structure_type.
c906108c 11175
c767944b
DJ
11176 NOTE: we need to call these functions regardless of whether or not the
11177 DIE has a DW_AT_name attribute, since it might be an anonymous
c906108c
SS
11178 structure or union. This gets the type entered into our set of
11179 user defined types.
11180
11181 However, if the structure is incomplete (an opaque struct/union)
11182 then suppress creating a symbol table entry for it since gdb only
11183 wants to find the one with the complete definition. Note that if
11184 it is complete, we just call new_symbol, which does it's own
11185 checking about whether the struct/union is anonymous or not (and
11186 suppresses creating a symbol table entry itself). */
11187
f792889a 11188static struct type *
134d01f1 11189read_structure_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 11190{
e7c27a73 11191 struct objfile *objfile = cu->objfile;
c906108c
SS
11192 struct type *type;
11193 struct attribute *attr;
15d034d0 11194 const char *name;
c906108c 11195
348e048f
DE
11196 /* If the definition of this type lives in .debug_types, read that type.
11197 Don't follow DW_AT_specification though, that will take us back up
11198 the chain and we want to go down. */
45e58e77 11199 attr = dwarf2_attr_no_follow (die, DW_AT_signature);
348e048f
DE
11200 if (attr)
11201 {
11202 struct dwarf2_cu *type_cu = cu;
11203 struct die_info *type_die = follow_die_ref_or_sig (die, attr, &type_cu);
9a619af0 11204
348e048f
DE
11205 /* We could just recurse on read_structure_type, but we need to call
11206 get_die_type to ensure only one type for this DIE is created.
11207 This is important, for example, because for c++ classes we need
11208 TYPE_NAME set which is only done by new_symbol. Blech. */
11209 type = read_type_die (type_die, type_cu);
9dc481d3
DE
11210
11211 /* TYPE_CU may not be the same as CU.
11212 Ensure TYPE is recorded in CU's type_hash table. */
348e048f
DE
11213 return set_die_type (die, type, cu);
11214 }
11215
c0dd20ea 11216 type = alloc_type (objfile);
c906108c 11217 INIT_CPLUS_SPECIFIC (type);
93311388 11218
39cbfefa
DJ
11219 name = dwarf2_name (die, cu);
11220 if (name != NULL)
c906108c 11221 {
987504bb
JJ
11222 if (cu->language == language_cplus
11223 || cu->language == language_java)
63d06c5c 11224 {
15d034d0 11225 const char *full_name = dwarf2_full_name (name, die, cu);
3da10d80
KS
11226
11227 /* dwarf2_full_name might have already finished building the DIE's
11228 type. If so, there is no need to continue. */
11229 if (get_die_type (die, cu) != NULL)
11230 return get_die_type (die, cu);
11231
11232 TYPE_TAG_NAME (type) = full_name;
94af9270
KS
11233 if (die->tag == DW_TAG_structure_type
11234 || die->tag == DW_TAG_class_type)
11235 TYPE_NAME (type) = TYPE_TAG_NAME (type);
63d06c5c
DC
11236 }
11237 else
11238 {
d8151005
DJ
11239 /* The name is already allocated along with this objfile, so
11240 we don't need to duplicate it for the type. */
7d455152 11241 TYPE_TAG_NAME (type) = name;
94af9270
KS
11242 if (die->tag == DW_TAG_class_type)
11243 TYPE_NAME (type) = TYPE_TAG_NAME (type);
63d06c5c 11244 }
c906108c
SS
11245 }
11246
11247 if (die->tag == DW_TAG_structure_type)
11248 {
11249 TYPE_CODE (type) = TYPE_CODE_STRUCT;
11250 }
11251 else if (die->tag == DW_TAG_union_type)
11252 {
11253 TYPE_CODE (type) = TYPE_CODE_UNION;
11254 }
11255 else
11256 {
c906108c
SS
11257 TYPE_CODE (type) = TYPE_CODE_CLASS;
11258 }
11259
0cc2414c
TT
11260 if (cu->language == language_cplus && die->tag == DW_TAG_class_type)
11261 TYPE_DECLARED_CLASS (type) = 1;
11262
e142c38c 11263 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
c906108c
SS
11264 if (attr)
11265 {
11266 TYPE_LENGTH (type) = DW_UNSND (attr);
11267 }
11268 else
11269 {
11270 TYPE_LENGTH (type) = 0;
11271 }
11272
685b1105
JK
11273 if (producer_is_icc (cu))
11274 {
11275 /* ICC does not output the required DW_AT_declaration
11276 on incomplete types, but gives them a size of zero. */
11277 }
11278 else
11279 TYPE_STUB_SUPPORTED (type) = 1;
11280
dc718098 11281 if (die_is_declaration (die, cu))
876cecd0 11282 TYPE_STUB (type) = 1;
a6c727b2
DJ
11283 else if (attr == NULL && die->child == NULL
11284 && producer_is_realview (cu->producer))
11285 /* RealView does not output the required DW_AT_declaration
11286 on incomplete types. */
11287 TYPE_STUB (type) = 1;
dc718098 11288
c906108c
SS
11289 /* We need to add the type field to the die immediately so we don't
11290 infinitely recurse when dealing with pointers to the structure
0963b4bd 11291 type within the structure itself. */
1c379e20 11292 set_die_type (die, type, cu);
c906108c 11293
7e314c57
JK
11294 /* set_die_type should be already done. */
11295 set_descriptive_type (type, die, cu);
11296
c767944b
DJ
11297 return type;
11298}
11299
11300/* Finish creating a structure or union type, including filling in
11301 its members and creating a symbol for it. */
11302
11303static void
11304process_structure_scope (struct die_info *die, struct dwarf2_cu *cu)
11305{
11306 struct objfile *objfile = cu->objfile;
11307 struct die_info *child_die = die->child;
11308 struct type *type;
11309
11310 type = get_die_type (die, cu);
11311 if (type == NULL)
11312 type = read_structure_type (die, cu);
11313
e142c38c 11314 if (die->child != NULL && ! die_is_declaration (die, cu))
c906108c
SS
11315 {
11316 struct field_info fi;
11317 struct die_info *child_die;
34eaf542 11318 VEC (symbolp) *template_args = NULL;
c767944b 11319 struct cleanup *back_to = make_cleanup (null_cleanup, 0);
c906108c
SS
11320
11321 memset (&fi, 0, sizeof (struct field_info));
11322
639d11d3 11323 child_die = die->child;
c906108c
SS
11324
11325 while (child_die && child_die->tag)
11326 {
a9a9bd0f
DC
11327 if (child_die->tag == DW_TAG_member
11328 || child_die->tag == DW_TAG_variable)
c906108c 11329 {
a9a9bd0f
DC
11330 /* NOTE: carlton/2002-11-05: A C++ static data member
11331 should be a DW_TAG_member that is a declaration, but
11332 all versions of G++ as of this writing (so through at
11333 least 3.2.1) incorrectly generate DW_TAG_variable
11334 tags for them instead. */
e7c27a73 11335 dwarf2_add_field (&fi, child_die, cu);
c906108c 11336 }
8713b1b1 11337 else if (child_die->tag == DW_TAG_subprogram)
c906108c 11338 {
0963b4bd 11339 /* C++ member function. */
e7c27a73 11340 dwarf2_add_member_fn (&fi, child_die, type, cu);
c906108c
SS
11341 }
11342 else if (child_die->tag == DW_TAG_inheritance)
11343 {
11344 /* C++ base class field. */
e7c27a73 11345 dwarf2_add_field (&fi, child_die, cu);
c906108c 11346 }
98751a41
JK
11347 else if (child_die->tag == DW_TAG_typedef)
11348 dwarf2_add_typedef (&fi, child_die, cu);
34eaf542
TT
11349 else if (child_die->tag == DW_TAG_template_type_param
11350 || child_die->tag == DW_TAG_template_value_param)
11351 {
11352 struct symbol *arg = new_symbol (child_die, NULL, cu);
11353
f1078f66
DJ
11354 if (arg != NULL)
11355 VEC_safe_push (symbolp, template_args, arg);
34eaf542
TT
11356 }
11357
c906108c
SS
11358 child_die = sibling_die (child_die);
11359 }
11360
34eaf542
TT
11361 /* Attach template arguments to type. */
11362 if (! VEC_empty (symbolp, template_args))
11363 {
11364 ALLOCATE_CPLUS_STRUCT_TYPE (type);
11365 TYPE_N_TEMPLATE_ARGUMENTS (type)
11366 = VEC_length (symbolp, template_args);
11367 TYPE_TEMPLATE_ARGUMENTS (type)
11368 = obstack_alloc (&objfile->objfile_obstack,
11369 (TYPE_N_TEMPLATE_ARGUMENTS (type)
11370 * sizeof (struct symbol *)));
11371 memcpy (TYPE_TEMPLATE_ARGUMENTS (type),
11372 VEC_address (symbolp, template_args),
11373 (TYPE_N_TEMPLATE_ARGUMENTS (type)
11374 * sizeof (struct symbol *)));
11375 VEC_free (symbolp, template_args);
11376 }
11377
c906108c
SS
11378 /* Attach fields and member functions to the type. */
11379 if (fi.nfields)
e7c27a73 11380 dwarf2_attach_fields_to_type (&fi, type, cu);
c906108c
SS
11381 if (fi.nfnfields)
11382 {
e7c27a73 11383 dwarf2_attach_fn_fields_to_type (&fi, type, cu);
c906108c 11384
c5aa993b 11385 /* Get the type which refers to the base class (possibly this
c906108c 11386 class itself) which contains the vtable pointer for the current
0d564a31
DJ
11387 class from the DW_AT_containing_type attribute. This use of
11388 DW_AT_containing_type is a GNU extension. */
c906108c 11389
e142c38c 11390 if (dwarf2_attr (die, DW_AT_containing_type, cu) != NULL)
c906108c 11391 {
e7c27a73 11392 struct type *t = die_containing_type (die, cu);
c906108c
SS
11393
11394 TYPE_VPTR_BASETYPE (type) = t;
11395 if (type == t)
11396 {
c906108c
SS
11397 int i;
11398
11399 /* Our own class provides vtbl ptr. */
11400 for (i = TYPE_NFIELDS (t) - 1;
11401 i >= TYPE_N_BASECLASSES (t);
11402 --i)
11403 {
0d5cff50 11404 const char *fieldname = TYPE_FIELD_NAME (t, i);
c906108c 11405
1168df01 11406 if (is_vtable_name (fieldname, cu))
c906108c
SS
11407 {
11408 TYPE_VPTR_FIELDNO (type) = i;
11409 break;
11410 }
11411 }
11412
11413 /* Complain if virtual function table field not found. */
11414 if (i < TYPE_N_BASECLASSES (t))
4d3c2250 11415 complaint (&symfile_complaints,
3e43a32a
MS
11416 _("virtual function table pointer "
11417 "not found when defining class '%s'"),
4d3c2250
KB
11418 TYPE_TAG_NAME (type) ? TYPE_TAG_NAME (type) :
11419 "");
c906108c
SS
11420 }
11421 else
11422 {
11423 TYPE_VPTR_FIELDNO (type) = TYPE_VPTR_FIELDNO (t);
11424 }
11425 }
f6235d4c
EZ
11426 else if (cu->producer
11427 && strncmp (cu->producer,
11428 "IBM(R) XL C/C++ Advanced Edition", 32) == 0)
11429 {
11430 /* The IBM XLC compiler does not provide direct indication
11431 of the containing type, but the vtable pointer is
11432 always named __vfp. */
11433
11434 int i;
11435
11436 for (i = TYPE_NFIELDS (type) - 1;
11437 i >= TYPE_N_BASECLASSES (type);
11438 --i)
11439 {
11440 if (strcmp (TYPE_FIELD_NAME (type, i), "__vfp") == 0)
11441 {
11442 TYPE_VPTR_FIELDNO (type) = i;
11443 TYPE_VPTR_BASETYPE (type) = type;
11444 break;
11445 }
11446 }
11447 }
c906108c 11448 }
98751a41
JK
11449
11450 /* Copy fi.typedef_field_list linked list elements content into the
11451 allocated array TYPE_TYPEDEF_FIELD_ARRAY (type). */
11452 if (fi.typedef_field_list)
11453 {
11454 int i = fi.typedef_field_list_count;
11455
a0d7a4ff 11456 ALLOCATE_CPLUS_STRUCT_TYPE (type);
98751a41
JK
11457 TYPE_TYPEDEF_FIELD_ARRAY (type)
11458 = TYPE_ALLOC (type, sizeof (TYPE_TYPEDEF_FIELD (type, 0)) * i);
11459 TYPE_TYPEDEF_FIELD_COUNT (type) = i;
11460
11461 /* Reverse the list order to keep the debug info elements order. */
11462 while (--i >= 0)
11463 {
11464 struct typedef_field *dest, *src;
6e70227d 11465
98751a41
JK
11466 dest = &TYPE_TYPEDEF_FIELD (type, i);
11467 src = &fi.typedef_field_list->field;
11468 fi.typedef_field_list = fi.typedef_field_list->next;
11469 *dest = *src;
11470 }
11471 }
c767944b
DJ
11472
11473 do_cleanups (back_to);
eb2a6f42
TT
11474
11475 if (HAVE_CPLUS_STRUCT (type))
11476 TYPE_CPLUS_REALLY_JAVA (type) = cu->language == language_java;
c906108c 11477 }
63d06c5c 11478
bb5ed363 11479 quirk_gcc_member_function_pointer (type, objfile);
0b92b5bb 11480
90aeadfc
DC
11481 /* NOTE: carlton/2004-03-16: GCC 3.4 (or at least one of its
11482 snapshots) has been known to create a die giving a declaration
11483 for a class that has, as a child, a die giving a definition for a
11484 nested class. So we have to process our children even if the
11485 current die is a declaration. Normally, of course, a declaration
11486 won't have any children at all. */
134d01f1 11487
90aeadfc
DC
11488 while (child_die != NULL && child_die->tag)
11489 {
11490 if (child_die->tag == DW_TAG_member
11491 || child_die->tag == DW_TAG_variable
34eaf542
TT
11492 || child_die->tag == DW_TAG_inheritance
11493 || child_die->tag == DW_TAG_template_value_param
11494 || child_die->tag == DW_TAG_template_type_param)
134d01f1 11495 {
90aeadfc 11496 /* Do nothing. */
134d01f1 11497 }
90aeadfc
DC
11498 else
11499 process_die (child_die, cu);
134d01f1 11500
90aeadfc 11501 child_die = sibling_die (child_die);
134d01f1
DJ
11502 }
11503
fa4028e9
JB
11504 /* Do not consider external references. According to the DWARF standard,
11505 these DIEs are identified by the fact that they have no byte_size
11506 attribute, and a declaration attribute. */
11507 if (dwarf2_attr (die, DW_AT_byte_size, cu) != NULL
11508 || !die_is_declaration (die, cu))
c767944b 11509 new_symbol (die, type, cu);
134d01f1
DJ
11510}
11511
11512/* Given a DW_AT_enumeration_type die, set its type. We do not
11513 complete the type's fields yet, or create any symbols. */
c906108c 11514
f792889a 11515static struct type *
134d01f1 11516read_enumeration_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 11517{
e7c27a73 11518 struct objfile *objfile = cu->objfile;
c906108c 11519 struct type *type;
c906108c 11520 struct attribute *attr;
0114d602 11521 const char *name;
134d01f1 11522
348e048f
DE
11523 /* If the definition of this type lives in .debug_types, read that type.
11524 Don't follow DW_AT_specification though, that will take us back up
11525 the chain and we want to go down. */
45e58e77 11526 attr = dwarf2_attr_no_follow (die, DW_AT_signature);
348e048f
DE
11527 if (attr)
11528 {
11529 struct dwarf2_cu *type_cu = cu;
11530 struct die_info *type_die = follow_die_ref_or_sig (die, attr, &type_cu);
9a619af0 11531
348e048f 11532 type = read_type_die (type_die, type_cu);
9dc481d3
DE
11533
11534 /* TYPE_CU may not be the same as CU.
11535 Ensure TYPE is recorded in CU's type_hash table. */
348e048f
DE
11536 return set_die_type (die, type, cu);
11537 }
11538
c906108c
SS
11539 type = alloc_type (objfile);
11540
11541 TYPE_CODE (type) = TYPE_CODE_ENUM;
94af9270 11542 name = dwarf2_full_name (NULL, die, cu);
39cbfefa 11543 if (name != NULL)
7d455152 11544 TYPE_TAG_NAME (type) = name;
c906108c 11545
e142c38c 11546 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
c906108c
SS
11547 if (attr)
11548 {
11549 TYPE_LENGTH (type) = DW_UNSND (attr);
11550 }
11551 else
11552 {
11553 TYPE_LENGTH (type) = 0;
11554 }
11555
137033e9
JB
11556 /* The enumeration DIE can be incomplete. In Ada, any type can be
11557 declared as private in the package spec, and then defined only
11558 inside the package body. Such types are known as Taft Amendment
11559 Types. When another package uses such a type, an incomplete DIE
11560 may be generated by the compiler. */
02eb380e 11561 if (die_is_declaration (die, cu))
876cecd0 11562 TYPE_STUB (type) = 1;
02eb380e 11563
f792889a 11564 return set_die_type (die, type, cu);
134d01f1
DJ
11565}
11566
11567/* Given a pointer to a die which begins an enumeration, process all
11568 the dies that define the members of the enumeration, and create the
11569 symbol for the enumeration type.
11570
11571 NOTE: We reverse the order of the element list. */
11572
11573static void
11574process_enumeration_scope (struct die_info *die, struct dwarf2_cu *cu)
11575{
f792889a 11576 struct type *this_type;
134d01f1 11577
f792889a
DJ
11578 this_type = get_die_type (die, cu);
11579 if (this_type == NULL)
11580 this_type = read_enumeration_type (die, cu);
9dc481d3 11581
639d11d3 11582 if (die->child != NULL)
c906108c 11583 {
9dc481d3
DE
11584 struct die_info *child_die;
11585 struct symbol *sym;
11586 struct field *fields = NULL;
11587 int num_fields = 0;
11588 int unsigned_enum = 1;
15d034d0 11589 const char *name;
cafec441
TT
11590 int flag_enum = 1;
11591 ULONGEST mask = 0;
9dc481d3 11592
639d11d3 11593 child_die = die->child;
c906108c
SS
11594 while (child_die && child_die->tag)
11595 {
11596 if (child_die->tag != DW_TAG_enumerator)
11597 {
e7c27a73 11598 process_die (child_die, cu);
c906108c
SS
11599 }
11600 else
11601 {
39cbfefa
DJ
11602 name = dwarf2_name (child_die, cu);
11603 if (name)
c906108c 11604 {
f792889a 11605 sym = new_symbol (child_die, this_type, cu);
c906108c 11606 if (SYMBOL_VALUE (sym) < 0)
cafec441
TT
11607 {
11608 unsigned_enum = 0;
11609 flag_enum = 0;
11610 }
11611 else if ((mask & SYMBOL_VALUE (sym)) != 0)
11612 flag_enum = 0;
11613 else
11614 mask |= SYMBOL_VALUE (sym);
c906108c
SS
11615
11616 if ((num_fields % DW_FIELD_ALLOC_CHUNK) == 0)
11617 {
11618 fields = (struct field *)
11619 xrealloc (fields,
11620 (num_fields + DW_FIELD_ALLOC_CHUNK)
c5aa993b 11621 * sizeof (struct field));
c906108c
SS
11622 }
11623
3567439c 11624 FIELD_NAME (fields[num_fields]) = SYMBOL_LINKAGE_NAME (sym);
c906108c 11625 FIELD_TYPE (fields[num_fields]) = NULL;
14e75d8e 11626 SET_FIELD_ENUMVAL (fields[num_fields], SYMBOL_VALUE (sym));
c906108c
SS
11627 FIELD_BITSIZE (fields[num_fields]) = 0;
11628
11629 num_fields++;
11630 }
11631 }
11632
11633 child_die = sibling_die (child_die);
11634 }
11635
11636 if (num_fields)
11637 {
f792889a
DJ
11638 TYPE_NFIELDS (this_type) = num_fields;
11639 TYPE_FIELDS (this_type) = (struct field *)
11640 TYPE_ALLOC (this_type, sizeof (struct field) * num_fields);
11641 memcpy (TYPE_FIELDS (this_type), fields,
c906108c 11642 sizeof (struct field) * num_fields);
b8c9b27d 11643 xfree (fields);
c906108c
SS
11644 }
11645 if (unsigned_enum)
876cecd0 11646 TYPE_UNSIGNED (this_type) = 1;
cafec441
TT
11647 if (flag_enum)
11648 TYPE_FLAG_ENUM (this_type) = 1;
c906108c 11649 }
134d01f1 11650
6c83ed52
TT
11651 /* If we are reading an enum from a .debug_types unit, and the enum
11652 is a declaration, and the enum is not the signatured type in the
11653 unit, then we do not want to add a symbol for it. Adding a
11654 symbol would in some cases obscure the true definition of the
11655 enum, giving users an incomplete type when the definition is
11656 actually available. Note that we do not want to do this for all
11657 enums which are just declarations, because C++0x allows forward
11658 enum declarations. */
3019eac3 11659 if (cu->per_cu->is_debug_types
6c83ed52
TT
11660 && die_is_declaration (die, cu))
11661 {
52dc124a 11662 struct signatured_type *sig_type;
6c83ed52 11663
52dc124a 11664 sig_type
6c83ed52 11665 = lookup_signatured_type_at_offset (dwarf2_per_objfile->objfile,
3019eac3 11666 cu->per_cu->info_or_types_section,
6c83ed52 11667 cu->per_cu->offset);
3019eac3
DE
11668 gdb_assert (sig_type->type_offset_in_section.sect_off != 0);
11669 if (sig_type->type_offset_in_section.sect_off != die->offset.sect_off)
6c83ed52
TT
11670 return;
11671 }
11672
f792889a 11673 new_symbol (die, this_type, cu);
c906108c
SS
11674}
11675
11676/* Extract all information from a DW_TAG_array_type DIE and put it in
11677 the DIE's type field. For now, this only handles one dimensional
11678 arrays. */
11679
f792889a 11680static struct type *
e7c27a73 11681read_array_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 11682{
e7c27a73 11683 struct objfile *objfile = cu->objfile;
c906108c 11684 struct die_info *child_die;
7e314c57 11685 struct type *type;
c906108c
SS
11686 struct type *element_type, *range_type, *index_type;
11687 struct type **range_types = NULL;
11688 struct attribute *attr;
11689 int ndim = 0;
11690 struct cleanup *back_to;
15d034d0 11691 const char *name;
c906108c 11692
e7c27a73 11693 element_type = die_type (die, cu);
c906108c 11694
7e314c57
JK
11695 /* The die_type call above may have already set the type for this DIE. */
11696 type = get_die_type (die, cu);
11697 if (type)
11698 return type;
11699
c906108c
SS
11700 /* Irix 6.2 native cc creates array types without children for
11701 arrays with unspecified length. */
639d11d3 11702 if (die->child == NULL)
c906108c 11703 {
46bf5051 11704 index_type = objfile_type (objfile)->builtin_int;
c906108c 11705 range_type = create_range_type (NULL, index_type, 0, -1);
f792889a
DJ
11706 type = create_array_type (NULL, element_type, range_type);
11707 return set_die_type (die, type, cu);
c906108c
SS
11708 }
11709
11710 back_to = make_cleanup (null_cleanup, NULL);
639d11d3 11711 child_die = die->child;
c906108c
SS
11712 while (child_die && child_die->tag)
11713 {
11714 if (child_die->tag == DW_TAG_subrange_type)
11715 {
f792889a 11716 struct type *child_type = read_type_die (child_die, cu);
9a619af0 11717
f792889a 11718 if (child_type != NULL)
a02abb62 11719 {
0963b4bd
MS
11720 /* The range type was succesfully read. Save it for the
11721 array type creation. */
a02abb62
JB
11722 if ((ndim % DW_FIELD_ALLOC_CHUNK) == 0)
11723 {
11724 range_types = (struct type **)
11725 xrealloc (range_types, (ndim + DW_FIELD_ALLOC_CHUNK)
11726 * sizeof (struct type *));
11727 if (ndim == 0)
11728 make_cleanup (free_current_contents, &range_types);
11729 }
f792889a 11730 range_types[ndim++] = child_type;
a02abb62 11731 }
c906108c
SS
11732 }
11733 child_die = sibling_die (child_die);
11734 }
11735
11736 /* Dwarf2 dimensions are output from left to right, create the
11737 necessary array types in backwards order. */
7ca2d3a3 11738
c906108c 11739 type = element_type;
7ca2d3a3
DL
11740
11741 if (read_array_order (die, cu) == DW_ORD_col_major)
11742 {
11743 int i = 0;
9a619af0 11744
7ca2d3a3
DL
11745 while (i < ndim)
11746 type = create_array_type (NULL, type, range_types[i++]);
11747 }
11748 else
11749 {
11750 while (ndim-- > 0)
11751 type = create_array_type (NULL, type, range_types[ndim]);
11752 }
c906108c 11753
f5f8a009
EZ
11754 /* Understand Dwarf2 support for vector types (like they occur on
11755 the PowerPC w/ AltiVec). Gcc just adds another attribute to the
11756 array type. This is not part of the Dwarf2/3 standard yet, but a
11757 custom vendor extension. The main difference between a regular
11758 array and the vector variant is that vectors are passed by value
11759 to functions. */
e142c38c 11760 attr = dwarf2_attr (die, DW_AT_GNU_vector, cu);
f5f8a009 11761 if (attr)
ea37ba09 11762 make_vector_type (type);
f5f8a009 11763
dbc98a8b
KW
11764 /* The DIE may have DW_AT_byte_size set. For example an OpenCL
11765 implementation may choose to implement triple vectors using this
11766 attribute. */
11767 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
11768 if (attr)
11769 {
11770 if (DW_UNSND (attr) >= TYPE_LENGTH (type))
11771 TYPE_LENGTH (type) = DW_UNSND (attr);
11772 else
3e43a32a
MS
11773 complaint (&symfile_complaints,
11774 _("DW_AT_byte_size for array type smaller "
11775 "than the total size of elements"));
dbc98a8b
KW
11776 }
11777
39cbfefa
DJ
11778 name = dwarf2_name (die, cu);
11779 if (name)
11780 TYPE_NAME (type) = name;
6e70227d 11781
0963b4bd 11782 /* Install the type in the die. */
7e314c57
JK
11783 set_die_type (die, type, cu);
11784
11785 /* set_die_type should be already done. */
b4ba55a1
JB
11786 set_descriptive_type (type, die, cu);
11787
c906108c
SS
11788 do_cleanups (back_to);
11789
7e314c57 11790 return type;
c906108c
SS
11791}
11792
7ca2d3a3 11793static enum dwarf_array_dim_ordering
6e70227d 11794read_array_order (struct die_info *die, struct dwarf2_cu *cu)
7ca2d3a3
DL
11795{
11796 struct attribute *attr;
11797
11798 attr = dwarf2_attr (die, DW_AT_ordering, cu);
11799
11800 if (attr) return DW_SND (attr);
11801
0963b4bd
MS
11802 /* GNU F77 is a special case, as at 08/2004 array type info is the
11803 opposite order to the dwarf2 specification, but data is still
11804 laid out as per normal fortran.
7ca2d3a3 11805
0963b4bd
MS
11806 FIXME: dsl/2004-8-20: If G77 is ever fixed, this will also need
11807 version checking. */
7ca2d3a3 11808
905e0470
PM
11809 if (cu->language == language_fortran
11810 && cu->producer && strstr (cu->producer, "GNU F77"))
7ca2d3a3
DL
11811 {
11812 return DW_ORD_row_major;
11813 }
11814
6e70227d 11815 switch (cu->language_defn->la_array_ordering)
7ca2d3a3
DL
11816 {
11817 case array_column_major:
11818 return DW_ORD_col_major;
11819 case array_row_major:
11820 default:
11821 return DW_ORD_row_major;
11822 };
11823}
11824
72019c9c 11825/* Extract all information from a DW_TAG_set_type DIE and put it in
0963b4bd 11826 the DIE's type field. */
72019c9c 11827
f792889a 11828static struct type *
72019c9c
GM
11829read_set_type (struct die_info *die, struct dwarf2_cu *cu)
11830{
7e314c57
JK
11831 struct type *domain_type, *set_type;
11832 struct attribute *attr;
f792889a 11833
7e314c57
JK
11834 domain_type = die_type (die, cu);
11835
11836 /* The die_type call above may have already set the type for this DIE. */
11837 set_type = get_die_type (die, cu);
11838 if (set_type)
11839 return set_type;
11840
11841 set_type = create_set_type (NULL, domain_type);
11842
11843 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
d09039dd
PM
11844 if (attr)
11845 TYPE_LENGTH (set_type) = DW_UNSND (attr);
7e314c57 11846
f792889a 11847 return set_die_type (die, set_type, cu);
72019c9c 11848}
7ca2d3a3 11849
0971de02
TT
11850/* A helper for read_common_block that creates a locexpr baton.
11851 SYM is the symbol which we are marking as computed.
11852 COMMON_DIE is the DIE for the common block.
11853 COMMON_LOC is the location expression attribute for the common
11854 block itself.
11855 MEMBER_LOC is the location expression attribute for the particular
11856 member of the common block that we are processing.
11857 CU is the CU from which the above come. */
11858
11859static void
11860mark_common_block_symbol_computed (struct symbol *sym,
11861 struct die_info *common_die,
11862 struct attribute *common_loc,
11863 struct attribute *member_loc,
11864 struct dwarf2_cu *cu)
11865{
11866 struct objfile *objfile = dwarf2_per_objfile->objfile;
11867 struct dwarf2_locexpr_baton *baton;
11868 gdb_byte *ptr;
11869 unsigned int cu_off;
11870 enum bfd_endian byte_order = gdbarch_byte_order (get_objfile_arch (objfile));
11871 LONGEST offset = 0;
11872
11873 gdb_assert (common_loc && member_loc);
11874 gdb_assert (attr_form_is_block (common_loc));
11875 gdb_assert (attr_form_is_block (member_loc)
11876 || attr_form_is_constant (member_loc));
11877
11878 baton = obstack_alloc (&objfile->objfile_obstack,
11879 sizeof (struct dwarf2_locexpr_baton));
11880 baton->per_cu = cu->per_cu;
11881 gdb_assert (baton->per_cu);
11882
11883 baton->size = 5 /* DW_OP_call4 */ + 1 /* DW_OP_plus */;
11884
11885 if (attr_form_is_constant (member_loc))
11886 {
11887 offset = dwarf2_get_attr_constant_value (member_loc, 0);
11888 baton->size += 1 /* DW_OP_addr */ + cu->header.addr_size;
11889 }
11890 else
11891 baton->size += DW_BLOCK (member_loc)->size;
11892
11893 ptr = obstack_alloc (&objfile->objfile_obstack, baton->size);
11894 baton->data = ptr;
11895
11896 *ptr++ = DW_OP_call4;
11897 cu_off = common_die->offset.sect_off - cu->per_cu->offset.sect_off;
11898 store_unsigned_integer (ptr, 4, byte_order, cu_off);
11899 ptr += 4;
11900
11901 if (attr_form_is_constant (member_loc))
11902 {
11903 *ptr++ = DW_OP_addr;
11904 store_unsigned_integer (ptr, cu->header.addr_size, byte_order, offset);
11905 ptr += cu->header.addr_size;
11906 }
11907 else
11908 {
11909 /* We have to copy the data here, because DW_OP_call4 will only
11910 use a DW_AT_location attribute. */
11911 memcpy (ptr, DW_BLOCK (member_loc)->data, DW_BLOCK (member_loc)->size);
11912 ptr += DW_BLOCK (member_loc)->size;
11913 }
11914
11915 *ptr++ = DW_OP_plus;
11916 gdb_assert (ptr - baton->data == baton->size);
11917
11918 SYMBOL_COMPUTED_OPS (sym) = &dwarf2_locexpr_funcs;
11919 SYMBOL_LOCATION_BATON (sym) = baton;
11920 SYMBOL_CLASS (sym) = LOC_COMPUTED;
11921}
11922
4357ac6c
TT
11923/* Create appropriate locally-scoped variables for all the
11924 DW_TAG_common_block entries. Also create a struct common_block
11925 listing all such variables for `info common'. COMMON_BLOCK_DOMAIN
11926 is used to sepate the common blocks name namespace from regular
11927 variable names. */
c906108c
SS
11928
11929static void
e7c27a73 11930read_common_block (struct die_info *die, struct dwarf2_cu *cu)
c906108c 11931{
0971de02
TT
11932 struct attribute *attr;
11933
11934 attr = dwarf2_attr (die, DW_AT_location, cu);
11935 if (attr)
11936 {
11937 /* Support the .debug_loc offsets. */
11938 if (attr_form_is_block (attr))
11939 {
11940 /* Ok. */
11941 }
11942 else if (attr_form_is_section_offset (attr))
11943 {
11944 dwarf2_complex_location_expr_complaint ();
11945 attr = NULL;
11946 }
11947 else
11948 {
11949 dwarf2_invalid_attrib_class_complaint ("DW_AT_location",
11950 "common block member");
11951 attr = NULL;
11952 }
11953 }
11954
639d11d3 11955 if (die->child != NULL)
c906108c 11956 {
4357ac6c
TT
11957 struct objfile *objfile = cu->objfile;
11958 struct die_info *child_die;
11959 size_t n_entries = 0, size;
11960 struct common_block *common_block;
11961 struct symbol *sym;
74ac6d43 11962
4357ac6c
TT
11963 for (child_die = die->child;
11964 child_die && child_die->tag;
11965 child_die = sibling_die (child_die))
11966 ++n_entries;
11967
11968 size = (sizeof (struct common_block)
11969 + (n_entries - 1) * sizeof (struct symbol *));
11970 common_block = obstack_alloc (&objfile->objfile_obstack, size);
11971 memset (common_block->contents, 0, n_entries * sizeof (struct symbol *));
11972 common_block->n_entries = 0;
11973
11974 for (child_die = die->child;
11975 child_die && child_die->tag;
11976 child_die = sibling_die (child_die))
11977 {
11978 /* Create the symbol in the DW_TAG_common_block block in the current
11979 symbol scope. */
e7c27a73 11980 sym = new_symbol (child_die, NULL, cu);
0971de02
TT
11981 if (sym != NULL)
11982 {
11983 struct attribute *member_loc;
11984
11985 common_block->contents[common_block->n_entries++] = sym;
11986
11987 member_loc = dwarf2_attr (child_die, DW_AT_data_member_location,
11988 cu);
11989 if (member_loc)
11990 {
11991 /* GDB has handled this for a long time, but it is
11992 not specified by DWARF. It seems to have been
11993 emitted by gfortran at least as recently as:
11994 http://gcc.gnu.org/bugzilla/show_bug.cgi?id=23057. */
11995 complaint (&symfile_complaints,
11996 _("Variable in common block has "
11997 "DW_AT_data_member_location "
11998 "- DIE at 0x%x [in module %s]"),
11999 child_die->offset.sect_off, cu->objfile->name);
12000
12001 if (attr_form_is_section_offset (member_loc))
12002 dwarf2_complex_location_expr_complaint ();
12003 else if (attr_form_is_constant (member_loc)
12004 || attr_form_is_block (member_loc))
12005 {
12006 if (attr)
12007 mark_common_block_symbol_computed (sym, die, attr,
12008 member_loc, cu);
12009 }
12010 else
12011 dwarf2_complex_location_expr_complaint ();
12012 }
12013 }
c906108c 12014 }
4357ac6c
TT
12015
12016 sym = new_symbol (die, objfile_type (objfile)->builtin_void, cu);
12017 SYMBOL_VALUE_COMMON_BLOCK (sym) = common_block;
c906108c
SS
12018 }
12019}
12020
0114d602 12021/* Create a type for a C++ namespace. */
d9fa45fe 12022
0114d602
DJ
12023static struct type *
12024read_namespace_type (struct die_info *die, struct dwarf2_cu *cu)
d9fa45fe 12025{
e7c27a73 12026 struct objfile *objfile = cu->objfile;
0114d602 12027 const char *previous_prefix, *name;
9219021c 12028 int is_anonymous;
0114d602
DJ
12029 struct type *type;
12030
12031 /* For extensions, reuse the type of the original namespace. */
12032 if (dwarf2_attr (die, DW_AT_extension, cu) != NULL)
12033 {
12034 struct die_info *ext_die;
12035 struct dwarf2_cu *ext_cu = cu;
9a619af0 12036
0114d602
DJ
12037 ext_die = dwarf2_extension (die, &ext_cu);
12038 type = read_type_die (ext_die, ext_cu);
9dc481d3
DE
12039
12040 /* EXT_CU may not be the same as CU.
12041 Ensure TYPE is recorded in CU's type_hash table. */
0114d602
DJ
12042 return set_die_type (die, type, cu);
12043 }
9219021c 12044
e142c38c 12045 name = namespace_name (die, &is_anonymous, cu);
9219021c
DC
12046
12047 /* Now build the name of the current namespace. */
12048
0114d602
DJ
12049 previous_prefix = determine_prefix (die, cu);
12050 if (previous_prefix[0] != '\0')
12051 name = typename_concat (&objfile->objfile_obstack,
f55ee35c 12052 previous_prefix, name, 0, cu);
0114d602
DJ
12053
12054 /* Create the type. */
12055 type = init_type (TYPE_CODE_NAMESPACE, 0, 0, NULL,
12056 objfile);
abee88f2 12057 TYPE_NAME (type) = name;
0114d602
DJ
12058 TYPE_TAG_NAME (type) = TYPE_NAME (type);
12059
60531b24 12060 return set_die_type (die, type, cu);
0114d602
DJ
12061}
12062
12063/* Read a C++ namespace. */
12064
12065static void
12066read_namespace (struct die_info *die, struct dwarf2_cu *cu)
12067{
12068 struct objfile *objfile = cu->objfile;
0114d602 12069 int is_anonymous;
9219021c 12070
5c4e30ca
DC
12071 /* Add a symbol associated to this if we haven't seen the namespace
12072 before. Also, add a using directive if it's an anonymous
12073 namespace. */
9219021c 12074
f2f0e013 12075 if (dwarf2_attr (die, DW_AT_extension, cu) == NULL)
5c4e30ca
DC
12076 {
12077 struct type *type;
12078
0114d602 12079 type = read_type_die (die, cu);
e7c27a73 12080 new_symbol (die, type, cu);
5c4e30ca 12081
e8e80198 12082 namespace_name (die, &is_anonymous, cu);
5c4e30ca 12083 if (is_anonymous)
0114d602
DJ
12084 {
12085 const char *previous_prefix = determine_prefix (die, cu);
9a619af0 12086
c0cc3a76 12087 cp_add_using_directive (previous_prefix, TYPE_NAME (type), NULL,
12aaed36 12088 NULL, NULL, 0, &objfile->objfile_obstack);
0114d602 12089 }
5c4e30ca 12090 }
9219021c 12091
639d11d3 12092 if (die->child != NULL)
d9fa45fe 12093 {
639d11d3 12094 struct die_info *child_die = die->child;
6e70227d 12095
d9fa45fe
DC
12096 while (child_die && child_die->tag)
12097 {
e7c27a73 12098 process_die (child_die, cu);
d9fa45fe
DC
12099 child_die = sibling_die (child_die);
12100 }
12101 }
38d518c9
EZ
12102}
12103
f55ee35c
JK
12104/* Read a Fortran module as type. This DIE can be only a declaration used for
12105 imported module. Still we need that type as local Fortran "use ... only"
12106 declaration imports depend on the created type in determine_prefix. */
12107
12108static struct type *
12109read_module_type (struct die_info *die, struct dwarf2_cu *cu)
12110{
12111 struct objfile *objfile = cu->objfile;
15d034d0 12112 const char *module_name;
f55ee35c
JK
12113 struct type *type;
12114
12115 module_name = dwarf2_name (die, cu);
12116 if (!module_name)
3e43a32a
MS
12117 complaint (&symfile_complaints,
12118 _("DW_TAG_module has no name, offset 0x%x"),
b64f50a1 12119 die->offset.sect_off);
f55ee35c
JK
12120 type = init_type (TYPE_CODE_MODULE, 0, 0, module_name, objfile);
12121
12122 /* determine_prefix uses TYPE_TAG_NAME. */
12123 TYPE_TAG_NAME (type) = TYPE_NAME (type);
12124
12125 return set_die_type (die, type, cu);
12126}
12127
5d7cb8df
JK
12128/* Read a Fortran module. */
12129
12130static void
12131read_module (struct die_info *die, struct dwarf2_cu *cu)
12132{
12133 struct die_info *child_die = die->child;
12134
5d7cb8df
JK
12135 while (child_die && child_die->tag)
12136 {
12137 process_die (child_die, cu);
12138 child_die = sibling_die (child_die);
12139 }
12140}
12141
38d518c9
EZ
12142/* Return the name of the namespace represented by DIE. Set
12143 *IS_ANONYMOUS to tell whether or not the namespace is an anonymous
12144 namespace. */
12145
12146static const char *
e142c38c 12147namespace_name (struct die_info *die, int *is_anonymous, struct dwarf2_cu *cu)
38d518c9
EZ
12148{
12149 struct die_info *current_die;
12150 const char *name = NULL;
12151
12152 /* Loop through the extensions until we find a name. */
12153
12154 for (current_die = die;
12155 current_die != NULL;
f2f0e013 12156 current_die = dwarf2_extension (die, &cu))
38d518c9 12157 {
e142c38c 12158 name = dwarf2_name (current_die, cu);
38d518c9
EZ
12159 if (name != NULL)
12160 break;
12161 }
12162
12163 /* Is it an anonymous namespace? */
12164
12165 *is_anonymous = (name == NULL);
12166 if (*is_anonymous)
2b1dbab0 12167 name = CP_ANONYMOUS_NAMESPACE_STR;
38d518c9
EZ
12168
12169 return name;
d9fa45fe
DC
12170}
12171
c906108c
SS
12172/* Extract all information from a DW_TAG_pointer_type DIE and add to
12173 the user defined type vector. */
12174
f792889a 12175static struct type *
e7c27a73 12176read_tag_pointer_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 12177{
5e2b427d 12178 struct gdbarch *gdbarch = get_objfile_arch (cu->objfile);
e7c27a73 12179 struct comp_unit_head *cu_header = &cu->header;
c906108c 12180 struct type *type;
8b2dbe47
KB
12181 struct attribute *attr_byte_size;
12182 struct attribute *attr_address_class;
12183 int byte_size, addr_class;
7e314c57
JK
12184 struct type *target_type;
12185
12186 target_type = die_type (die, cu);
c906108c 12187
7e314c57
JK
12188 /* The die_type call above may have already set the type for this DIE. */
12189 type = get_die_type (die, cu);
12190 if (type)
12191 return type;
12192
12193 type = lookup_pointer_type (target_type);
8b2dbe47 12194
e142c38c 12195 attr_byte_size = dwarf2_attr (die, DW_AT_byte_size, cu);
8b2dbe47
KB
12196 if (attr_byte_size)
12197 byte_size = DW_UNSND (attr_byte_size);
c906108c 12198 else
8b2dbe47
KB
12199 byte_size = cu_header->addr_size;
12200
e142c38c 12201 attr_address_class = dwarf2_attr (die, DW_AT_address_class, cu);
8b2dbe47
KB
12202 if (attr_address_class)
12203 addr_class = DW_UNSND (attr_address_class);
12204 else
12205 addr_class = DW_ADDR_none;
12206
12207 /* If the pointer size or address class is different than the
12208 default, create a type variant marked as such and set the
12209 length accordingly. */
12210 if (TYPE_LENGTH (type) != byte_size || addr_class != DW_ADDR_none)
c906108c 12211 {
5e2b427d 12212 if (gdbarch_address_class_type_flags_p (gdbarch))
8b2dbe47
KB
12213 {
12214 int type_flags;
12215
849957d9 12216 type_flags = gdbarch_address_class_type_flags
5e2b427d 12217 (gdbarch, byte_size, addr_class);
876cecd0
TT
12218 gdb_assert ((type_flags & ~TYPE_INSTANCE_FLAG_ADDRESS_CLASS_ALL)
12219 == 0);
8b2dbe47
KB
12220 type = make_type_with_address_space (type, type_flags);
12221 }
12222 else if (TYPE_LENGTH (type) != byte_size)
12223 {
3e43a32a
MS
12224 complaint (&symfile_complaints,
12225 _("invalid pointer size %d"), byte_size);
8b2dbe47 12226 }
6e70227d 12227 else
9a619af0
MS
12228 {
12229 /* Should we also complain about unhandled address classes? */
12230 }
c906108c 12231 }
8b2dbe47
KB
12232
12233 TYPE_LENGTH (type) = byte_size;
f792889a 12234 return set_die_type (die, type, cu);
c906108c
SS
12235}
12236
12237/* Extract all information from a DW_TAG_ptr_to_member_type DIE and add to
12238 the user defined type vector. */
12239
f792889a 12240static struct type *
e7c27a73 12241read_tag_ptr_to_member_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c
SS
12242{
12243 struct type *type;
12244 struct type *to_type;
12245 struct type *domain;
12246
e7c27a73
DJ
12247 to_type = die_type (die, cu);
12248 domain = die_containing_type (die, cu);
0d5de010 12249
7e314c57
JK
12250 /* The calls above may have already set the type for this DIE. */
12251 type = get_die_type (die, cu);
12252 if (type)
12253 return type;
12254
0d5de010
DJ
12255 if (TYPE_CODE (check_typedef (to_type)) == TYPE_CODE_METHOD)
12256 type = lookup_methodptr_type (to_type);
7078baeb
TT
12257 else if (TYPE_CODE (check_typedef (to_type)) == TYPE_CODE_FUNC)
12258 {
12259 struct type *new_type = alloc_type (cu->objfile);
12260
12261 smash_to_method_type (new_type, domain, TYPE_TARGET_TYPE (to_type),
12262 TYPE_FIELDS (to_type), TYPE_NFIELDS (to_type),
12263 TYPE_VARARGS (to_type));
12264 type = lookup_methodptr_type (new_type);
12265 }
0d5de010
DJ
12266 else
12267 type = lookup_memberptr_type (to_type, domain);
c906108c 12268
f792889a 12269 return set_die_type (die, type, cu);
c906108c
SS
12270}
12271
12272/* Extract all information from a DW_TAG_reference_type DIE and add to
12273 the user defined type vector. */
12274
f792889a 12275static struct type *
e7c27a73 12276read_tag_reference_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 12277{
e7c27a73 12278 struct comp_unit_head *cu_header = &cu->header;
7e314c57 12279 struct type *type, *target_type;
c906108c
SS
12280 struct attribute *attr;
12281
7e314c57
JK
12282 target_type = die_type (die, cu);
12283
12284 /* The die_type call above may have already set the type for this DIE. */
12285 type = get_die_type (die, cu);
12286 if (type)
12287 return type;
12288
12289 type = lookup_reference_type (target_type);
e142c38c 12290 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
c906108c
SS
12291 if (attr)
12292 {
12293 TYPE_LENGTH (type) = DW_UNSND (attr);
12294 }
12295 else
12296 {
107d2387 12297 TYPE_LENGTH (type) = cu_header->addr_size;
c906108c 12298 }
f792889a 12299 return set_die_type (die, type, cu);
c906108c
SS
12300}
12301
f792889a 12302static struct type *
e7c27a73 12303read_tag_const_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 12304{
f792889a 12305 struct type *base_type, *cv_type;
c906108c 12306
e7c27a73 12307 base_type = die_type (die, cu);
7e314c57
JK
12308
12309 /* The die_type call above may have already set the type for this DIE. */
12310 cv_type = get_die_type (die, cu);
12311 if (cv_type)
12312 return cv_type;
12313
2f608a3a
KW
12314 /* In case the const qualifier is applied to an array type, the element type
12315 is so qualified, not the array type (section 6.7.3 of C99). */
12316 if (TYPE_CODE (base_type) == TYPE_CODE_ARRAY)
12317 {
12318 struct type *el_type, *inner_array;
12319
12320 base_type = copy_type (base_type);
12321 inner_array = base_type;
12322
12323 while (TYPE_CODE (TYPE_TARGET_TYPE (inner_array)) == TYPE_CODE_ARRAY)
12324 {
12325 TYPE_TARGET_TYPE (inner_array) =
12326 copy_type (TYPE_TARGET_TYPE (inner_array));
12327 inner_array = TYPE_TARGET_TYPE (inner_array);
12328 }
12329
12330 el_type = TYPE_TARGET_TYPE (inner_array);
12331 TYPE_TARGET_TYPE (inner_array) =
12332 make_cv_type (1, TYPE_VOLATILE (el_type), el_type, NULL);
12333
12334 return set_die_type (die, base_type, cu);
12335 }
12336
f792889a
DJ
12337 cv_type = make_cv_type (1, TYPE_VOLATILE (base_type), base_type, 0);
12338 return set_die_type (die, cv_type, cu);
c906108c
SS
12339}
12340
f792889a 12341static struct type *
e7c27a73 12342read_tag_volatile_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 12343{
f792889a 12344 struct type *base_type, *cv_type;
c906108c 12345
e7c27a73 12346 base_type = die_type (die, cu);
7e314c57
JK
12347
12348 /* The die_type call above may have already set the type for this DIE. */
12349 cv_type = get_die_type (die, cu);
12350 if (cv_type)
12351 return cv_type;
12352
f792889a
DJ
12353 cv_type = make_cv_type (TYPE_CONST (base_type), 1, base_type, 0);
12354 return set_die_type (die, cv_type, cu);
c906108c
SS
12355}
12356
06d66ee9
TT
12357/* Handle DW_TAG_restrict_type. */
12358
12359static struct type *
12360read_tag_restrict_type (struct die_info *die, struct dwarf2_cu *cu)
12361{
12362 struct type *base_type, *cv_type;
12363
12364 base_type = die_type (die, cu);
12365
12366 /* The die_type call above may have already set the type for this DIE. */
12367 cv_type = get_die_type (die, cu);
12368 if (cv_type)
12369 return cv_type;
12370
12371 cv_type = make_restrict_type (base_type);
12372 return set_die_type (die, cv_type, cu);
12373}
12374
c906108c
SS
12375/* Extract all information from a DW_TAG_string_type DIE and add to
12376 the user defined type vector. It isn't really a user defined type,
12377 but it behaves like one, with other DIE's using an AT_user_def_type
12378 attribute to reference it. */
12379
f792889a 12380static struct type *
e7c27a73 12381read_tag_string_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 12382{
e7c27a73 12383 struct objfile *objfile = cu->objfile;
3b7538c0 12384 struct gdbarch *gdbarch = get_objfile_arch (objfile);
c906108c
SS
12385 struct type *type, *range_type, *index_type, *char_type;
12386 struct attribute *attr;
12387 unsigned int length;
12388
e142c38c 12389 attr = dwarf2_attr (die, DW_AT_string_length, cu);
c906108c
SS
12390 if (attr)
12391 {
12392 length = DW_UNSND (attr);
12393 }
12394 else
12395 {
0963b4bd 12396 /* Check for the DW_AT_byte_size attribute. */
e142c38c 12397 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
b21b22e0
PS
12398 if (attr)
12399 {
12400 length = DW_UNSND (attr);
12401 }
12402 else
12403 {
12404 length = 1;
12405 }
c906108c 12406 }
6ccb9162 12407
46bf5051 12408 index_type = objfile_type (objfile)->builtin_int;
c906108c 12409 range_type = create_range_type (NULL, index_type, 1, length);
3b7538c0
UW
12410 char_type = language_string_char_type (cu->language_defn, gdbarch);
12411 type = create_string_type (NULL, char_type, range_type);
6ccb9162 12412
f792889a 12413 return set_die_type (die, type, cu);
c906108c
SS
12414}
12415
12416/* Handle DIES due to C code like:
12417
12418 struct foo
c5aa993b
JM
12419 {
12420 int (*funcp)(int a, long l);
12421 int b;
12422 };
c906108c 12423
0963b4bd 12424 ('funcp' generates a DW_TAG_subroutine_type DIE). */
c906108c 12425
f792889a 12426static struct type *
e7c27a73 12427read_subroutine_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 12428{
bb5ed363 12429 struct objfile *objfile = cu->objfile;
0963b4bd
MS
12430 struct type *type; /* Type that this function returns. */
12431 struct type *ftype; /* Function that returns above type. */
c906108c
SS
12432 struct attribute *attr;
12433
e7c27a73 12434 type = die_type (die, cu);
7e314c57
JK
12435
12436 /* The die_type call above may have already set the type for this DIE. */
12437 ftype = get_die_type (die, cu);
12438 if (ftype)
12439 return ftype;
12440
0c8b41f1 12441 ftype = lookup_function_type (type);
c906108c 12442
5b8101ae 12443 /* All functions in C++, Pascal and Java have prototypes. */
e142c38c 12444 attr = dwarf2_attr (die, DW_AT_prototyped, cu);
c906108c 12445 if ((attr && (DW_UNSND (attr) != 0))
987504bb 12446 || cu->language == language_cplus
5b8101ae
PM
12447 || cu->language == language_java
12448 || cu->language == language_pascal)
876cecd0 12449 TYPE_PROTOTYPED (ftype) = 1;
a6c727b2
DJ
12450 else if (producer_is_realview (cu->producer))
12451 /* RealView does not emit DW_AT_prototyped. We can not
12452 distinguish prototyped and unprototyped functions; default to
12453 prototyped, since that is more common in modern code (and
12454 RealView warns about unprototyped functions). */
12455 TYPE_PROTOTYPED (ftype) = 1;
c906108c 12456
c055b101
CV
12457 /* Store the calling convention in the type if it's available in
12458 the subroutine die. Otherwise set the calling convention to
12459 the default value DW_CC_normal. */
12460 attr = dwarf2_attr (die, DW_AT_calling_convention, cu);
54fcddd0
UW
12461 if (attr)
12462 TYPE_CALLING_CONVENTION (ftype) = DW_UNSND (attr);
12463 else if (cu->producer && strstr (cu->producer, "IBM XL C for OpenCL"))
12464 TYPE_CALLING_CONVENTION (ftype) = DW_CC_GDB_IBM_OpenCL;
12465 else
12466 TYPE_CALLING_CONVENTION (ftype) = DW_CC_normal;
76c10ea2
GM
12467
12468 /* We need to add the subroutine type to the die immediately so
12469 we don't infinitely recurse when dealing with parameters
0963b4bd 12470 declared as the same subroutine type. */
76c10ea2 12471 set_die_type (die, ftype, cu);
6e70227d 12472
639d11d3 12473 if (die->child != NULL)
c906108c 12474 {
bb5ed363 12475 struct type *void_type = objfile_type (objfile)->builtin_void;
c906108c 12476 struct die_info *child_die;
8072405b 12477 int nparams, iparams;
c906108c
SS
12478
12479 /* Count the number of parameters.
12480 FIXME: GDB currently ignores vararg functions, but knows about
12481 vararg member functions. */
8072405b 12482 nparams = 0;
639d11d3 12483 child_die = die->child;
c906108c
SS
12484 while (child_die && child_die->tag)
12485 {
12486 if (child_die->tag == DW_TAG_formal_parameter)
12487 nparams++;
12488 else if (child_die->tag == DW_TAG_unspecified_parameters)
876cecd0 12489 TYPE_VARARGS (ftype) = 1;
c906108c
SS
12490 child_die = sibling_die (child_die);
12491 }
12492
12493 /* Allocate storage for parameters and fill them in. */
12494 TYPE_NFIELDS (ftype) = nparams;
12495 TYPE_FIELDS (ftype) = (struct field *)
ae5a43e0 12496 TYPE_ZALLOC (ftype, nparams * sizeof (struct field));
c906108c 12497
8072405b
JK
12498 /* TYPE_FIELD_TYPE must never be NULL. Pre-fill the array to ensure it
12499 even if we error out during the parameters reading below. */
12500 for (iparams = 0; iparams < nparams; iparams++)
12501 TYPE_FIELD_TYPE (ftype, iparams) = void_type;
12502
12503 iparams = 0;
639d11d3 12504 child_die = die->child;
c906108c
SS
12505 while (child_die && child_die->tag)
12506 {
12507 if (child_die->tag == DW_TAG_formal_parameter)
12508 {
3ce3b1ba
PA
12509 struct type *arg_type;
12510
12511 /* DWARF version 2 has no clean way to discern C++
12512 static and non-static member functions. G++ helps
12513 GDB by marking the first parameter for non-static
12514 member functions (which is the this pointer) as
12515 artificial. We pass this information to
12516 dwarf2_add_member_fn via TYPE_FIELD_ARTIFICIAL.
12517
12518 DWARF version 3 added DW_AT_object_pointer, which GCC
12519 4.5 does not yet generate. */
e142c38c 12520 attr = dwarf2_attr (child_die, DW_AT_artificial, cu);
c906108c
SS
12521 if (attr)
12522 TYPE_FIELD_ARTIFICIAL (ftype, iparams) = DW_UNSND (attr);
12523 else
418835cc
KS
12524 {
12525 TYPE_FIELD_ARTIFICIAL (ftype, iparams) = 0;
12526
12527 /* GCC/43521: In java, the formal parameter
12528 "this" is sometimes not marked with DW_AT_artificial. */
12529 if (cu->language == language_java)
12530 {
12531 const char *name = dwarf2_name (child_die, cu);
9a619af0 12532
418835cc
KS
12533 if (name && !strcmp (name, "this"))
12534 TYPE_FIELD_ARTIFICIAL (ftype, iparams) = 1;
12535 }
12536 }
3ce3b1ba
PA
12537 arg_type = die_type (child_die, cu);
12538
12539 /* RealView does not mark THIS as const, which the testsuite
12540 expects. GCC marks THIS as const in method definitions,
12541 but not in the class specifications (GCC PR 43053). */
12542 if (cu->language == language_cplus && !TYPE_CONST (arg_type)
12543 && TYPE_FIELD_ARTIFICIAL (ftype, iparams))
12544 {
12545 int is_this = 0;
12546 struct dwarf2_cu *arg_cu = cu;
12547 const char *name = dwarf2_name (child_die, cu);
12548
12549 attr = dwarf2_attr (die, DW_AT_object_pointer, cu);
12550 if (attr)
12551 {
12552 /* If the compiler emits this, use it. */
12553 if (follow_die_ref (die, attr, &arg_cu) == child_die)
12554 is_this = 1;
12555 }
12556 else if (name && strcmp (name, "this") == 0)
12557 /* Function definitions will have the argument names. */
12558 is_this = 1;
12559 else if (name == NULL && iparams == 0)
12560 /* Declarations may not have the names, so like
12561 elsewhere in GDB, assume an artificial first
12562 argument is "this". */
12563 is_this = 1;
12564
12565 if (is_this)
12566 arg_type = make_cv_type (1, TYPE_VOLATILE (arg_type),
12567 arg_type, 0);
12568 }
12569
12570 TYPE_FIELD_TYPE (ftype, iparams) = arg_type;
c906108c
SS
12571 iparams++;
12572 }
12573 child_die = sibling_die (child_die);
12574 }
12575 }
12576
76c10ea2 12577 return ftype;
c906108c
SS
12578}
12579
f792889a 12580static struct type *
e7c27a73 12581read_typedef (struct die_info *die, struct dwarf2_cu *cu)
c906108c 12582{
e7c27a73 12583 struct objfile *objfile = cu->objfile;
0114d602 12584 const char *name = NULL;
3c8e0968 12585 struct type *this_type, *target_type;
c906108c 12586
94af9270 12587 name = dwarf2_full_name (NULL, die, cu);
f792889a 12588 this_type = init_type (TYPE_CODE_TYPEDEF, 0,
0114d602 12589 TYPE_FLAG_TARGET_STUB, NULL, objfile);
abee88f2 12590 TYPE_NAME (this_type) = name;
f792889a 12591 set_die_type (die, this_type, cu);
3c8e0968
DE
12592 target_type = die_type (die, cu);
12593 if (target_type != this_type)
12594 TYPE_TARGET_TYPE (this_type) = target_type;
12595 else
12596 {
12597 /* Self-referential typedefs are, it seems, not allowed by the DWARF
12598 spec and cause infinite loops in GDB. */
12599 complaint (&symfile_complaints,
12600 _("Self-referential DW_TAG_typedef "
12601 "- DIE at 0x%x [in module %s]"),
b64f50a1 12602 die->offset.sect_off, objfile->name);
3c8e0968
DE
12603 TYPE_TARGET_TYPE (this_type) = NULL;
12604 }
f792889a 12605 return this_type;
c906108c
SS
12606}
12607
12608/* Find a representation of a given base type and install
12609 it in the TYPE field of the die. */
12610
f792889a 12611static struct type *
e7c27a73 12612read_base_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 12613{
e7c27a73 12614 struct objfile *objfile = cu->objfile;
c906108c
SS
12615 struct type *type;
12616 struct attribute *attr;
12617 int encoding = 0, size = 0;
15d034d0 12618 const char *name;
6ccb9162
UW
12619 enum type_code code = TYPE_CODE_INT;
12620 int type_flags = 0;
12621 struct type *target_type = NULL;
c906108c 12622
e142c38c 12623 attr = dwarf2_attr (die, DW_AT_encoding, cu);
c906108c
SS
12624 if (attr)
12625 {
12626 encoding = DW_UNSND (attr);
12627 }
e142c38c 12628 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
c906108c
SS
12629 if (attr)
12630 {
12631 size = DW_UNSND (attr);
12632 }
39cbfefa 12633 name = dwarf2_name (die, cu);
6ccb9162 12634 if (!name)
c906108c 12635 {
6ccb9162
UW
12636 complaint (&symfile_complaints,
12637 _("DW_AT_name missing from DW_TAG_base_type"));
c906108c 12638 }
6ccb9162
UW
12639
12640 switch (encoding)
c906108c 12641 {
6ccb9162
UW
12642 case DW_ATE_address:
12643 /* Turn DW_ATE_address into a void * pointer. */
12644 code = TYPE_CODE_PTR;
12645 type_flags |= TYPE_FLAG_UNSIGNED;
12646 target_type = init_type (TYPE_CODE_VOID, 1, 0, NULL, objfile);
12647 break;
12648 case DW_ATE_boolean:
12649 code = TYPE_CODE_BOOL;
12650 type_flags |= TYPE_FLAG_UNSIGNED;
12651 break;
12652 case DW_ATE_complex_float:
12653 code = TYPE_CODE_COMPLEX;
12654 target_type = init_type (TYPE_CODE_FLT, size / 2, 0, NULL, objfile);
12655 break;
12656 case DW_ATE_decimal_float:
12657 code = TYPE_CODE_DECFLOAT;
12658 break;
12659 case DW_ATE_float:
12660 code = TYPE_CODE_FLT;
12661 break;
12662 case DW_ATE_signed:
12663 break;
12664 case DW_ATE_unsigned:
12665 type_flags |= TYPE_FLAG_UNSIGNED;
3b2b8fea
TT
12666 if (cu->language == language_fortran
12667 && name
12668 && strncmp (name, "character(", sizeof ("character(") - 1) == 0)
12669 code = TYPE_CODE_CHAR;
6ccb9162
UW
12670 break;
12671 case DW_ATE_signed_char:
6e70227d 12672 if (cu->language == language_ada || cu->language == language_m2
3b2b8fea
TT
12673 || cu->language == language_pascal
12674 || cu->language == language_fortran)
6ccb9162
UW
12675 code = TYPE_CODE_CHAR;
12676 break;
12677 case DW_ATE_unsigned_char:
868a0084 12678 if (cu->language == language_ada || cu->language == language_m2
3b2b8fea
TT
12679 || cu->language == language_pascal
12680 || cu->language == language_fortran)
6ccb9162
UW
12681 code = TYPE_CODE_CHAR;
12682 type_flags |= TYPE_FLAG_UNSIGNED;
12683 break;
75079b2b
TT
12684 case DW_ATE_UTF:
12685 /* We just treat this as an integer and then recognize the
12686 type by name elsewhere. */
12687 break;
12688
6ccb9162
UW
12689 default:
12690 complaint (&symfile_complaints, _("unsupported DW_AT_encoding: '%s'"),
12691 dwarf_type_encoding_name (encoding));
12692 break;
c906108c 12693 }
6ccb9162 12694
0114d602
DJ
12695 type = init_type (code, size, type_flags, NULL, objfile);
12696 TYPE_NAME (type) = name;
6ccb9162
UW
12697 TYPE_TARGET_TYPE (type) = target_type;
12698
0114d602 12699 if (name && strcmp (name, "char") == 0)
876cecd0 12700 TYPE_NOSIGN (type) = 1;
0114d602 12701
f792889a 12702 return set_die_type (die, type, cu);
c906108c
SS
12703}
12704
a02abb62
JB
12705/* Read the given DW_AT_subrange DIE. */
12706
f792889a 12707static struct type *
a02abb62
JB
12708read_subrange_type (struct die_info *die, struct dwarf2_cu *cu)
12709{
12710 struct type *base_type;
12711 struct type *range_type;
12712 struct attribute *attr;
4fae6e18
JK
12713 LONGEST low, high;
12714 int low_default_is_valid;
15d034d0 12715 const char *name;
43bbcdc2 12716 LONGEST negative_mask;
e77813c8 12717
a02abb62 12718 base_type = die_type (die, cu);
953ac07e
JK
12719 /* Preserve BASE_TYPE's original type, just set its LENGTH. */
12720 check_typedef (base_type);
a02abb62 12721
7e314c57
JK
12722 /* The die_type call above may have already set the type for this DIE. */
12723 range_type = get_die_type (die, cu);
12724 if (range_type)
12725 return range_type;
12726
4fae6e18
JK
12727 /* Set LOW_DEFAULT_IS_VALID if current language and DWARF version allow
12728 omitting DW_AT_lower_bound. */
12729 switch (cu->language)
6e70227d 12730 {
4fae6e18
JK
12731 case language_c:
12732 case language_cplus:
12733 low = 0;
12734 low_default_is_valid = 1;
12735 break;
12736 case language_fortran:
12737 low = 1;
12738 low_default_is_valid = 1;
12739 break;
12740 case language_d:
12741 case language_java:
12742 case language_objc:
12743 low = 0;
12744 low_default_is_valid = (cu->header.version >= 4);
12745 break;
12746 case language_ada:
12747 case language_m2:
12748 case language_pascal:
a02abb62 12749 low = 1;
4fae6e18
JK
12750 low_default_is_valid = (cu->header.version >= 4);
12751 break;
12752 default:
12753 low = 0;
12754 low_default_is_valid = 0;
12755 break;
a02abb62
JB
12756 }
12757
dd5e6932
DJ
12758 /* FIXME: For variable sized arrays either of these could be
12759 a variable rather than a constant value. We'll allow it,
12760 but we don't know how to handle it. */
e142c38c 12761 attr = dwarf2_attr (die, DW_AT_lower_bound, cu);
a02abb62 12762 if (attr)
4fae6e18
JK
12763 low = dwarf2_get_attr_constant_value (attr, low);
12764 else if (!low_default_is_valid)
12765 complaint (&symfile_complaints, _("Missing DW_AT_lower_bound "
12766 "- DIE at 0x%x [in module %s]"),
12767 die->offset.sect_off, cu->objfile->name);
a02abb62 12768
e142c38c 12769 attr = dwarf2_attr (die, DW_AT_upper_bound, cu);
a02abb62 12770 if (attr)
6e70227d 12771 {
d48323d8 12772 if (attr_form_is_block (attr) || is_ref_attr (attr))
a02abb62
JB
12773 {
12774 /* GCC encodes arrays with unspecified or dynamic length
e77813c8 12775 with a DW_FORM_block1 attribute or a reference attribute.
a02abb62
JB
12776 FIXME: GDB does not yet know how to handle dynamic
12777 arrays properly, treat them as arrays with unspecified
12778 length for now.
12779
12780 FIXME: jimb/2003-09-22: GDB does not really know
12781 how to handle arrays of unspecified length
12782 either; we just represent them as zero-length
12783 arrays. Choose an appropriate upper bound given
12784 the lower bound we've computed above. */
12785 high = low - 1;
12786 }
12787 else
12788 high = dwarf2_get_attr_constant_value (attr, 1);
12789 }
e77813c8
PM
12790 else
12791 {
12792 attr = dwarf2_attr (die, DW_AT_count, cu);
12793 if (attr)
12794 {
12795 int count = dwarf2_get_attr_constant_value (attr, 1);
12796 high = low + count - 1;
12797 }
c2ff108b
JK
12798 else
12799 {
12800 /* Unspecified array length. */
12801 high = low - 1;
12802 }
e77813c8
PM
12803 }
12804
12805 /* Dwarf-2 specifications explicitly allows to create subrange types
12806 without specifying a base type.
12807 In that case, the base type must be set to the type of
12808 the lower bound, upper bound or count, in that order, if any of these
12809 three attributes references an object that has a type.
12810 If no base type is found, the Dwarf-2 specifications say that
12811 a signed integer type of size equal to the size of an address should
12812 be used.
12813 For the following C code: `extern char gdb_int [];'
12814 GCC produces an empty range DIE.
12815 FIXME: muller/2010-05-28: Possible references to object for low bound,
0963b4bd 12816 high bound or count are not yet handled by this code. */
e77813c8
PM
12817 if (TYPE_CODE (base_type) == TYPE_CODE_VOID)
12818 {
12819 struct objfile *objfile = cu->objfile;
12820 struct gdbarch *gdbarch = get_objfile_arch (objfile);
12821 int addr_size = gdbarch_addr_bit (gdbarch) /8;
12822 struct type *int_type = objfile_type (objfile)->builtin_int;
12823
12824 /* Test "int", "long int", and "long long int" objfile types,
12825 and select the first one having a size above or equal to the
12826 architecture address size. */
12827 if (int_type && TYPE_LENGTH (int_type) >= addr_size)
12828 base_type = int_type;
12829 else
12830 {
12831 int_type = objfile_type (objfile)->builtin_long;
12832 if (int_type && TYPE_LENGTH (int_type) >= addr_size)
12833 base_type = int_type;
12834 else
12835 {
12836 int_type = objfile_type (objfile)->builtin_long_long;
12837 if (int_type && TYPE_LENGTH (int_type) >= addr_size)
12838 base_type = int_type;
12839 }
12840 }
12841 }
a02abb62 12842
6e70227d 12843 negative_mask =
43bbcdc2
PH
12844 (LONGEST) -1 << (TYPE_LENGTH (base_type) * TARGET_CHAR_BIT - 1);
12845 if (!TYPE_UNSIGNED (base_type) && (low & negative_mask))
12846 low |= negative_mask;
12847 if (!TYPE_UNSIGNED (base_type) && (high & negative_mask))
12848 high |= negative_mask;
12849
a02abb62
JB
12850 range_type = create_range_type (NULL, base_type, low, high);
12851
bbb0eef6
JK
12852 /* Mark arrays with dynamic length at least as an array of unspecified
12853 length. GDB could check the boundary but before it gets implemented at
12854 least allow accessing the array elements. */
d48323d8 12855 if (attr && attr_form_is_block (attr))
bbb0eef6
JK
12856 TYPE_HIGH_BOUND_UNDEFINED (range_type) = 1;
12857
c2ff108b
JK
12858 /* Ada expects an empty array on no boundary attributes. */
12859 if (attr == NULL && cu->language != language_ada)
12860 TYPE_HIGH_BOUND_UNDEFINED (range_type) = 1;
12861
39cbfefa
DJ
12862 name = dwarf2_name (die, cu);
12863 if (name)
12864 TYPE_NAME (range_type) = name;
6e70227d 12865
e142c38c 12866 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
a02abb62
JB
12867 if (attr)
12868 TYPE_LENGTH (range_type) = DW_UNSND (attr);
12869
7e314c57
JK
12870 set_die_type (die, range_type, cu);
12871
12872 /* set_die_type should be already done. */
b4ba55a1
JB
12873 set_descriptive_type (range_type, die, cu);
12874
7e314c57 12875 return range_type;
a02abb62 12876}
6e70227d 12877
f792889a 12878static struct type *
81a17f79
JB
12879read_unspecified_type (struct die_info *die, struct dwarf2_cu *cu)
12880{
12881 struct type *type;
81a17f79 12882
81a17f79
JB
12883 /* For now, we only support the C meaning of an unspecified type: void. */
12884
0114d602
DJ
12885 type = init_type (TYPE_CODE_VOID, 0, 0, NULL, cu->objfile);
12886 TYPE_NAME (type) = dwarf2_name (die, cu);
81a17f79 12887
f792889a 12888 return set_die_type (die, type, cu);
81a17f79 12889}
a02abb62 12890
639d11d3
DC
12891/* Read a single die and all its descendents. Set the die's sibling
12892 field to NULL; set other fields in the die correctly, and set all
12893 of the descendents' fields correctly. Set *NEW_INFO_PTR to the
12894 location of the info_ptr after reading all of those dies. PARENT
12895 is the parent of the die in question. */
12896
12897static struct die_info *
dee91e82
DE
12898read_die_and_children (const struct die_reader_specs *reader,
12899 gdb_byte *info_ptr,
12900 gdb_byte **new_info_ptr,
12901 struct die_info *parent)
639d11d3
DC
12902{
12903 struct die_info *die;
fe1b8b76 12904 gdb_byte *cur_ptr;
639d11d3
DC
12905 int has_children;
12906
93311388 12907 cur_ptr = read_full_die (reader, &die, info_ptr, &has_children);
1d325ec1
DJ
12908 if (die == NULL)
12909 {
12910 *new_info_ptr = cur_ptr;
12911 return NULL;
12912 }
93311388 12913 store_in_ref_table (die, reader->cu);
639d11d3
DC
12914
12915 if (has_children)
348e048f 12916 die->child = read_die_and_siblings (reader, cur_ptr, new_info_ptr, die);
639d11d3
DC
12917 else
12918 {
12919 die->child = NULL;
12920 *new_info_ptr = cur_ptr;
12921 }
12922
12923 die->sibling = NULL;
12924 die->parent = parent;
12925 return die;
12926}
12927
12928/* Read a die, all of its descendents, and all of its siblings; set
12929 all of the fields of all of the dies correctly. Arguments are as
12930 in read_die_and_children. */
12931
12932static struct die_info *
93311388
DE
12933read_die_and_siblings (const struct die_reader_specs *reader,
12934 gdb_byte *info_ptr,
fe1b8b76 12935 gdb_byte **new_info_ptr,
639d11d3
DC
12936 struct die_info *parent)
12937{
12938 struct die_info *first_die, *last_sibling;
fe1b8b76 12939 gdb_byte *cur_ptr;
639d11d3 12940
c906108c 12941 cur_ptr = info_ptr;
639d11d3
DC
12942 first_die = last_sibling = NULL;
12943
12944 while (1)
c906108c 12945 {
639d11d3 12946 struct die_info *die
dee91e82 12947 = read_die_and_children (reader, cur_ptr, &cur_ptr, parent);
639d11d3 12948
1d325ec1 12949 if (die == NULL)
c906108c 12950 {
639d11d3
DC
12951 *new_info_ptr = cur_ptr;
12952 return first_die;
c906108c 12953 }
1d325ec1
DJ
12954
12955 if (!first_die)
12956 first_die = die;
c906108c 12957 else
1d325ec1
DJ
12958 last_sibling->sibling = die;
12959
12960 last_sibling = die;
c906108c 12961 }
c906108c
SS
12962}
12963
3019eac3
DE
12964/* Read a die and all its attributes, leave space for NUM_EXTRA_ATTRS
12965 attributes.
12966 The caller is responsible for filling in the extra attributes
12967 and updating (*DIEP)->num_attrs.
12968 Set DIEP to point to a newly allocated die with its information,
12969 except for its child, sibling, and parent fields.
12970 Set HAS_CHILDREN to tell whether the die has children or not. */
93311388
DE
12971
12972static gdb_byte *
3019eac3
DE
12973read_full_die_1 (const struct die_reader_specs *reader,
12974 struct die_info **diep, gdb_byte *info_ptr,
12975 int *has_children, int num_extra_attrs)
93311388 12976{
b64f50a1
JK
12977 unsigned int abbrev_number, bytes_read, i;
12978 sect_offset offset;
93311388
DE
12979 struct abbrev_info *abbrev;
12980 struct die_info *die;
12981 struct dwarf2_cu *cu = reader->cu;
12982 bfd *abfd = reader->abfd;
12983
b64f50a1 12984 offset.sect_off = info_ptr - reader->buffer;
93311388
DE
12985 abbrev_number = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
12986 info_ptr += bytes_read;
12987 if (!abbrev_number)
12988 {
12989 *diep = NULL;
12990 *has_children = 0;
12991 return info_ptr;
12992 }
12993
433df2d4 12994 abbrev = abbrev_table_lookup_abbrev (cu->abbrev_table, abbrev_number);
93311388 12995 if (!abbrev)
348e048f
DE
12996 error (_("Dwarf Error: could not find abbrev number %d [in module %s]"),
12997 abbrev_number,
12998 bfd_get_filename (abfd));
12999
3019eac3 13000 die = dwarf_alloc_die (cu, abbrev->num_attrs + num_extra_attrs);
93311388
DE
13001 die->offset = offset;
13002 die->tag = abbrev->tag;
13003 die->abbrev = abbrev_number;
13004
3019eac3
DE
13005 /* Make the result usable.
13006 The caller needs to update num_attrs after adding the extra
13007 attributes. */
93311388
DE
13008 die->num_attrs = abbrev->num_attrs;
13009
13010 for (i = 0; i < abbrev->num_attrs; ++i)
dee91e82
DE
13011 info_ptr = read_attribute (reader, &die->attrs[i], &abbrev->attrs[i],
13012 info_ptr);
93311388
DE
13013
13014 *diep = die;
13015 *has_children = abbrev->has_children;
13016 return info_ptr;
13017}
13018
3019eac3
DE
13019/* Read a die and all its attributes.
13020 Set DIEP to point to a newly allocated die with its information,
13021 except for its child, sibling, and parent fields.
13022 Set HAS_CHILDREN to tell whether the die has children or not. */
13023
13024static gdb_byte *
13025read_full_die (const struct die_reader_specs *reader,
13026 struct die_info **diep, gdb_byte *info_ptr,
13027 int *has_children)
13028{
13029 return read_full_die_1 (reader, diep, info_ptr, has_children, 0);
13030}
433df2d4
DE
13031\f
13032/* Abbreviation tables.
3019eac3 13033
433df2d4 13034 In DWARF version 2, the description of the debugging information is
c906108c
SS
13035 stored in a separate .debug_abbrev section. Before we read any
13036 dies from a section we read in all abbreviations and install them
433df2d4
DE
13037 in a hash table. */
13038
13039/* Allocate space for a struct abbrev_info object in ABBREV_TABLE. */
13040
13041static struct abbrev_info *
13042abbrev_table_alloc_abbrev (struct abbrev_table *abbrev_table)
13043{
13044 struct abbrev_info *abbrev;
13045
13046 abbrev = (struct abbrev_info *)
13047 obstack_alloc (&abbrev_table->abbrev_obstack, sizeof (struct abbrev_info));
13048 memset (abbrev, 0, sizeof (struct abbrev_info));
13049 return abbrev;
13050}
13051
13052/* Add an abbreviation to the table. */
c906108c
SS
13053
13054static void
433df2d4
DE
13055abbrev_table_add_abbrev (struct abbrev_table *abbrev_table,
13056 unsigned int abbrev_number,
13057 struct abbrev_info *abbrev)
13058{
13059 unsigned int hash_number;
13060
13061 hash_number = abbrev_number % ABBREV_HASH_SIZE;
13062 abbrev->next = abbrev_table->abbrevs[hash_number];
13063 abbrev_table->abbrevs[hash_number] = abbrev;
13064}
dee91e82 13065
433df2d4
DE
13066/* Look up an abbrev in the table.
13067 Returns NULL if the abbrev is not found. */
13068
13069static struct abbrev_info *
13070abbrev_table_lookup_abbrev (const struct abbrev_table *abbrev_table,
13071 unsigned int abbrev_number)
c906108c 13072{
433df2d4
DE
13073 unsigned int hash_number;
13074 struct abbrev_info *abbrev;
13075
13076 hash_number = abbrev_number % ABBREV_HASH_SIZE;
13077 abbrev = abbrev_table->abbrevs[hash_number];
13078
13079 while (abbrev)
13080 {
13081 if (abbrev->number == abbrev_number)
13082 return abbrev;
13083 abbrev = abbrev->next;
13084 }
13085 return NULL;
13086}
13087
13088/* Read in an abbrev table. */
13089
13090static struct abbrev_table *
13091abbrev_table_read_table (struct dwarf2_section_info *section,
13092 sect_offset offset)
13093{
13094 struct objfile *objfile = dwarf2_per_objfile->objfile;
13095 bfd *abfd = section->asection->owner;
13096 struct abbrev_table *abbrev_table;
fe1b8b76 13097 gdb_byte *abbrev_ptr;
c906108c
SS
13098 struct abbrev_info *cur_abbrev;
13099 unsigned int abbrev_number, bytes_read, abbrev_name;
433df2d4 13100 unsigned int abbrev_form;
f3dd6933
DJ
13101 struct attr_abbrev *cur_attrs;
13102 unsigned int allocated_attrs;
c906108c 13103
433df2d4 13104 abbrev_table = XMALLOC (struct abbrev_table);
f4dc4d17 13105 abbrev_table->offset = offset;
433df2d4
DE
13106 obstack_init (&abbrev_table->abbrev_obstack);
13107 abbrev_table->abbrevs = obstack_alloc (&abbrev_table->abbrev_obstack,
13108 (ABBREV_HASH_SIZE
13109 * sizeof (struct abbrev_info *)));
13110 memset (abbrev_table->abbrevs, 0,
13111 ABBREV_HASH_SIZE * sizeof (struct abbrev_info *));
c906108c 13112
433df2d4
DE
13113 dwarf2_read_section (objfile, section);
13114 abbrev_ptr = section->buffer + offset.sect_off;
c906108c
SS
13115 abbrev_number = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
13116 abbrev_ptr += bytes_read;
13117
f3dd6933
DJ
13118 allocated_attrs = ATTR_ALLOC_CHUNK;
13119 cur_attrs = xmalloc (allocated_attrs * sizeof (struct attr_abbrev));
6e70227d 13120
0963b4bd 13121 /* Loop until we reach an abbrev number of 0. */
c906108c
SS
13122 while (abbrev_number)
13123 {
433df2d4 13124 cur_abbrev = abbrev_table_alloc_abbrev (abbrev_table);
c906108c
SS
13125
13126 /* read in abbrev header */
13127 cur_abbrev->number = abbrev_number;
13128 cur_abbrev->tag = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
13129 abbrev_ptr += bytes_read;
13130 cur_abbrev->has_children = read_1_byte (abfd, abbrev_ptr);
13131 abbrev_ptr += 1;
13132
13133 /* now read in declarations */
13134 abbrev_name = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
13135 abbrev_ptr += bytes_read;
13136 abbrev_form = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
13137 abbrev_ptr += bytes_read;
13138 while (abbrev_name)
13139 {
f3dd6933 13140 if (cur_abbrev->num_attrs == allocated_attrs)
c906108c 13141 {
f3dd6933
DJ
13142 allocated_attrs += ATTR_ALLOC_CHUNK;
13143 cur_attrs
13144 = xrealloc (cur_attrs, (allocated_attrs
13145 * sizeof (struct attr_abbrev)));
c906108c 13146 }
ae038cb0 13147
f3dd6933
DJ
13148 cur_attrs[cur_abbrev->num_attrs].name = abbrev_name;
13149 cur_attrs[cur_abbrev->num_attrs++].form = abbrev_form;
c906108c
SS
13150 abbrev_name = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
13151 abbrev_ptr += bytes_read;
13152 abbrev_form = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
13153 abbrev_ptr += bytes_read;
13154 }
13155
433df2d4 13156 cur_abbrev->attrs = obstack_alloc (&abbrev_table->abbrev_obstack,
f3dd6933
DJ
13157 (cur_abbrev->num_attrs
13158 * sizeof (struct attr_abbrev)));
13159 memcpy (cur_abbrev->attrs, cur_attrs,
13160 cur_abbrev->num_attrs * sizeof (struct attr_abbrev));
13161
433df2d4 13162 abbrev_table_add_abbrev (abbrev_table, abbrev_number, cur_abbrev);
c906108c
SS
13163
13164 /* Get next abbreviation.
13165 Under Irix6 the abbreviations for a compilation unit are not
c5aa993b
JM
13166 always properly terminated with an abbrev number of 0.
13167 Exit loop if we encounter an abbreviation which we have
13168 already read (which means we are about to read the abbreviations
13169 for the next compile unit) or if the end of the abbreviation
13170 table is reached. */
433df2d4 13171 if ((unsigned int) (abbrev_ptr - section->buffer) >= section->size)
c906108c
SS
13172 break;
13173 abbrev_number = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
13174 abbrev_ptr += bytes_read;
433df2d4 13175 if (abbrev_table_lookup_abbrev (abbrev_table, abbrev_number) != NULL)
c906108c
SS
13176 break;
13177 }
f3dd6933
DJ
13178
13179 xfree (cur_attrs);
433df2d4 13180 return abbrev_table;
c906108c
SS
13181}
13182
433df2d4 13183/* Free the resources held by ABBREV_TABLE. */
c906108c 13184
c906108c 13185static void
433df2d4 13186abbrev_table_free (struct abbrev_table *abbrev_table)
c906108c 13187{
433df2d4
DE
13188 obstack_free (&abbrev_table->abbrev_obstack, NULL);
13189 xfree (abbrev_table);
c906108c
SS
13190}
13191
f4dc4d17
DE
13192/* Same as abbrev_table_free but as a cleanup.
13193 We pass in a pointer to the pointer to the table so that we can
13194 set the pointer to NULL when we're done. It also simplifies
13195 build_type_unit_groups. */
13196
13197static void
13198abbrev_table_free_cleanup (void *table_ptr)
13199{
13200 struct abbrev_table **abbrev_table_ptr = table_ptr;
13201
13202 if (*abbrev_table_ptr != NULL)
13203 abbrev_table_free (*abbrev_table_ptr);
13204 *abbrev_table_ptr = NULL;
13205}
13206
433df2d4
DE
13207/* Read the abbrev table for CU from ABBREV_SECTION. */
13208
13209static void
13210dwarf2_read_abbrevs (struct dwarf2_cu *cu,
13211 struct dwarf2_section_info *abbrev_section)
c906108c 13212{
433df2d4
DE
13213 cu->abbrev_table =
13214 abbrev_table_read_table (abbrev_section, cu->header.abbrev_offset);
13215}
c906108c 13216
433df2d4 13217/* Release the memory used by the abbrev table for a compilation unit. */
c906108c 13218
433df2d4
DE
13219static void
13220dwarf2_free_abbrev_table (void *ptr_to_cu)
13221{
13222 struct dwarf2_cu *cu = ptr_to_cu;
c906108c 13223
433df2d4
DE
13224 abbrev_table_free (cu->abbrev_table);
13225 /* Set this to NULL so that we SEGV if we try to read it later,
13226 and also because free_comp_unit verifies this is NULL. */
13227 cu->abbrev_table = NULL;
13228}
13229\f
72bf9492
DJ
13230/* Returns nonzero if TAG represents a type that we might generate a partial
13231 symbol for. */
13232
13233static int
13234is_type_tag_for_partial (int tag)
13235{
13236 switch (tag)
13237 {
13238#if 0
13239 /* Some types that would be reasonable to generate partial symbols for,
13240 that we don't at present. */
13241 case DW_TAG_array_type:
13242 case DW_TAG_file_type:
13243 case DW_TAG_ptr_to_member_type:
13244 case DW_TAG_set_type:
13245 case DW_TAG_string_type:
13246 case DW_TAG_subroutine_type:
13247#endif
13248 case DW_TAG_base_type:
13249 case DW_TAG_class_type:
680b30c7 13250 case DW_TAG_interface_type:
72bf9492
DJ
13251 case DW_TAG_enumeration_type:
13252 case DW_TAG_structure_type:
13253 case DW_TAG_subrange_type:
13254 case DW_TAG_typedef:
13255 case DW_TAG_union_type:
13256 return 1;
13257 default:
13258 return 0;
13259 }
13260}
13261
13262/* Load all DIEs that are interesting for partial symbols into memory. */
13263
13264static struct partial_die_info *
dee91e82
DE
13265load_partial_dies (const struct die_reader_specs *reader,
13266 gdb_byte *info_ptr, int building_psymtab)
72bf9492 13267{
dee91e82 13268 struct dwarf2_cu *cu = reader->cu;
bb5ed363 13269 struct objfile *objfile = cu->objfile;
72bf9492
DJ
13270 struct partial_die_info *part_die;
13271 struct partial_die_info *parent_die, *last_die, *first_die = NULL;
13272 struct abbrev_info *abbrev;
13273 unsigned int bytes_read;
5afb4e99 13274 unsigned int load_all = 0;
72bf9492
DJ
13275 int nesting_level = 1;
13276
13277 parent_die = NULL;
13278 last_die = NULL;
13279
7adf1e79
DE
13280 gdb_assert (cu->per_cu != NULL);
13281 if (cu->per_cu->load_all_dies)
5afb4e99
DJ
13282 load_all = 1;
13283
72bf9492
DJ
13284 cu->partial_dies
13285 = htab_create_alloc_ex (cu->header.length / 12,
13286 partial_die_hash,
13287 partial_die_eq,
13288 NULL,
13289 &cu->comp_unit_obstack,
13290 hashtab_obstack_allocate,
13291 dummy_obstack_deallocate);
13292
13293 part_die = obstack_alloc (&cu->comp_unit_obstack,
13294 sizeof (struct partial_die_info));
13295
13296 while (1)
13297 {
13298 abbrev = peek_die_abbrev (info_ptr, &bytes_read, cu);
13299
13300 /* A NULL abbrev means the end of a series of children. */
13301 if (abbrev == NULL)
13302 {
13303 if (--nesting_level == 0)
13304 {
13305 /* PART_DIE was probably the last thing allocated on the
13306 comp_unit_obstack, so we could call obstack_free
13307 here. We don't do that because the waste is small,
13308 and will be cleaned up when we're done with this
13309 compilation unit. This way, we're also more robust
13310 against other users of the comp_unit_obstack. */
13311 return first_die;
13312 }
13313 info_ptr += bytes_read;
13314 last_die = parent_die;
13315 parent_die = parent_die->die_parent;
13316 continue;
13317 }
13318
98bfdba5
PA
13319 /* Check for template arguments. We never save these; if
13320 they're seen, we just mark the parent, and go on our way. */
13321 if (parent_die != NULL
13322 && cu->language == language_cplus
13323 && (abbrev->tag == DW_TAG_template_type_param
13324 || abbrev->tag == DW_TAG_template_value_param))
13325 {
13326 parent_die->has_template_arguments = 1;
13327
13328 if (!load_all)
13329 {
13330 /* We don't need a partial DIE for the template argument. */
dee91e82 13331 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
98bfdba5
PA
13332 continue;
13333 }
13334 }
13335
0d99eb77 13336 /* We only recurse into c++ subprograms looking for template arguments.
98bfdba5
PA
13337 Skip their other children. */
13338 if (!load_all
13339 && cu->language == language_cplus
13340 && parent_die != NULL
13341 && parent_die->tag == DW_TAG_subprogram)
13342 {
dee91e82 13343 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
98bfdba5
PA
13344 continue;
13345 }
13346
5afb4e99
DJ
13347 /* Check whether this DIE is interesting enough to save. Normally
13348 we would not be interested in members here, but there may be
13349 later variables referencing them via DW_AT_specification (for
13350 static members). */
13351 if (!load_all
13352 && !is_type_tag_for_partial (abbrev->tag)
72929c62 13353 && abbrev->tag != DW_TAG_constant
72bf9492
DJ
13354 && abbrev->tag != DW_TAG_enumerator
13355 && abbrev->tag != DW_TAG_subprogram
bc30ff58 13356 && abbrev->tag != DW_TAG_lexical_block
72bf9492 13357 && abbrev->tag != DW_TAG_variable
5afb4e99 13358 && abbrev->tag != DW_TAG_namespace
f55ee35c 13359 && abbrev->tag != DW_TAG_module
95554aad
TT
13360 && abbrev->tag != DW_TAG_member
13361 && abbrev->tag != DW_TAG_imported_unit)
72bf9492
DJ
13362 {
13363 /* Otherwise we skip to the next sibling, if any. */
dee91e82 13364 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
72bf9492
DJ
13365 continue;
13366 }
13367
dee91e82
DE
13368 info_ptr = read_partial_die (reader, part_die, abbrev, bytes_read,
13369 info_ptr);
72bf9492
DJ
13370
13371 /* This two-pass algorithm for processing partial symbols has a
13372 high cost in cache pressure. Thus, handle some simple cases
13373 here which cover the majority of C partial symbols. DIEs
13374 which neither have specification tags in them, nor could have
13375 specification tags elsewhere pointing at them, can simply be
13376 processed and discarded.
13377
13378 This segment is also optional; scan_partial_symbols and
13379 add_partial_symbol will handle these DIEs if we chain
13380 them in normally. When compilers which do not emit large
13381 quantities of duplicate debug information are more common,
13382 this code can probably be removed. */
13383
13384 /* Any complete simple types at the top level (pretty much all
13385 of them, for a language without namespaces), can be processed
13386 directly. */
13387 if (parent_die == NULL
13388 && part_die->has_specification == 0
13389 && part_die->is_declaration == 0
d8228535 13390 && ((part_die->tag == DW_TAG_typedef && !part_die->has_children)
72bf9492
DJ
13391 || part_die->tag == DW_TAG_base_type
13392 || part_die->tag == DW_TAG_subrange_type))
13393 {
13394 if (building_psymtab && part_die->name != NULL)
04a679b8 13395 add_psymbol_to_list (part_die->name, strlen (part_die->name), 0,
72bf9492 13396 VAR_DOMAIN, LOC_TYPEDEF,
bb5ed363
DE
13397 &objfile->static_psymbols,
13398 0, (CORE_ADDR) 0, cu->language, objfile);
dee91e82 13399 info_ptr = locate_pdi_sibling (reader, part_die, info_ptr);
72bf9492
DJ
13400 continue;
13401 }
13402
d8228535
JK
13403 /* The exception for DW_TAG_typedef with has_children above is
13404 a workaround of GCC PR debug/47510. In the case of this complaint
13405 type_name_no_tag_or_error will error on such types later.
13406
13407 GDB skipped children of DW_TAG_typedef by the shortcut above and then
13408 it could not find the child DIEs referenced later, this is checked
13409 above. In correct DWARF DW_TAG_typedef should have no children. */
13410
13411 if (part_die->tag == DW_TAG_typedef && part_die->has_children)
13412 complaint (&symfile_complaints,
13413 _("DW_TAG_typedef has childen - GCC PR debug/47510 bug "
13414 "- DIE at 0x%x [in module %s]"),
b64f50a1 13415 part_die->offset.sect_off, objfile->name);
d8228535 13416
72bf9492
DJ
13417 /* If we're at the second level, and we're an enumerator, and
13418 our parent has no specification (meaning possibly lives in a
13419 namespace elsewhere), then we can add the partial symbol now
13420 instead of queueing it. */
13421 if (part_die->tag == DW_TAG_enumerator
13422 && parent_die != NULL
13423 && parent_die->die_parent == NULL
13424 && parent_die->tag == DW_TAG_enumeration_type
13425 && parent_die->has_specification == 0)
13426 {
13427 if (part_die->name == NULL)
3e43a32a
MS
13428 complaint (&symfile_complaints,
13429 _("malformed enumerator DIE ignored"));
72bf9492 13430 else if (building_psymtab)
04a679b8 13431 add_psymbol_to_list (part_die->name, strlen (part_die->name), 0,
72bf9492 13432 VAR_DOMAIN, LOC_CONST,
987504bb
JJ
13433 (cu->language == language_cplus
13434 || cu->language == language_java)
bb5ed363
DE
13435 ? &objfile->global_psymbols
13436 : &objfile->static_psymbols,
13437 0, (CORE_ADDR) 0, cu->language, objfile);
72bf9492 13438
dee91e82 13439 info_ptr = locate_pdi_sibling (reader, part_die, info_ptr);
72bf9492
DJ
13440 continue;
13441 }
13442
13443 /* We'll save this DIE so link it in. */
13444 part_die->die_parent = parent_die;
13445 part_die->die_sibling = NULL;
13446 part_die->die_child = NULL;
13447
13448 if (last_die && last_die == parent_die)
13449 last_die->die_child = part_die;
13450 else if (last_die)
13451 last_die->die_sibling = part_die;
13452
13453 last_die = part_die;
13454
13455 if (first_die == NULL)
13456 first_die = part_die;
13457
13458 /* Maybe add the DIE to the hash table. Not all DIEs that we
13459 find interesting need to be in the hash table, because we
13460 also have the parent/sibling/child chains; only those that we
13461 might refer to by offset later during partial symbol reading.
13462
13463 For now this means things that might have be the target of a
13464 DW_AT_specification, DW_AT_abstract_origin, or
13465 DW_AT_extension. DW_AT_extension will refer only to
13466 namespaces; DW_AT_abstract_origin refers to functions (and
13467 many things under the function DIE, but we do not recurse
13468 into function DIEs during partial symbol reading) and
13469 possibly variables as well; DW_AT_specification refers to
13470 declarations. Declarations ought to have the DW_AT_declaration
13471 flag. It happens that GCC forgets to put it in sometimes, but
13472 only for functions, not for types.
13473
13474 Adding more things than necessary to the hash table is harmless
13475 except for the performance cost. Adding too few will result in
5afb4e99
DJ
13476 wasted time in find_partial_die, when we reread the compilation
13477 unit with load_all_dies set. */
72bf9492 13478
5afb4e99 13479 if (load_all
72929c62 13480 || abbrev->tag == DW_TAG_constant
5afb4e99 13481 || abbrev->tag == DW_TAG_subprogram
72bf9492
DJ
13482 || abbrev->tag == DW_TAG_variable
13483 || abbrev->tag == DW_TAG_namespace
13484 || part_die->is_declaration)
13485 {
13486 void **slot;
13487
13488 slot = htab_find_slot_with_hash (cu->partial_dies, part_die,
b64f50a1 13489 part_die->offset.sect_off, INSERT);
72bf9492
DJ
13490 *slot = part_die;
13491 }
13492
13493 part_die = obstack_alloc (&cu->comp_unit_obstack,
13494 sizeof (struct partial_die_info));
13495
13496 /* For some DIEs we want to follow their children (if any). For C
bc30ff58 13497 we have no reason to follow the children of structures; for other
98bfdba5
PA
13498 languages we have to, so that we can get at method physnames
13499 to infer fully qualified class names, for DW_AT_specification,
13500 and for C++ template arguments. For C++, we also look one level
13501 inside functions to find template arguments (if the name of the
13502 function does not already contain the template arguments).
bc30ff58
JB
13503
13504 For Ada, we need to scan the children of subprograms and lexical
13505 blocks as well because Ada allows the definition of nested
13506 entities that could be interesting for the debugger, such as
13507 nested subprograms for instance. */
72bf9492 13508 if (last_die->has_children
5afb4e99
DJ
13509 && (load_all
13510 || last_die->tag == DW_TAG_namespace
f55ee35c 13511 || last_die->tag == DW_TAG_module
72bf9492 13512 || last_die->tag == DW_TAG_enumeration_type
98bfdba5
PA
13513 || (cu->language == language_cplus
13514 && last_die->tag == DW_TAG_subprogram
13515 && (last_die->name == NULL
13516 || strchr (last_die->name, '<') == NULL))
72bf9492
DJ
13517 || (cu->language != language_c
13518 && (last_die->tag == DW_TAG_class_type
680b30c7 13519 || last_die->tag == DW_TAG_interface_type
72bf9492 13520 || last_die->tag == DW_TAG_structure_type
bc30ff58
JB
13521 || last_die->tag == DW_TAG_union_type))
13522 || (cu->language == language_ada
13523 && (last_die->tag == DW_TAG_subprogram
13524 || last_die->tag == DW_TAG_lexical_block))))
72bf9492
DJ
13525 {
13526 nesting_level++;
13527 parent_die = last_die;
13528 continue;
13529 }
13530
13531 /* Otherwise we skip to the next sibling, if any. */
dee91e82 13532 info_ptr = locate_pdi_sibling (reader, last_die, info_ptr);
72bf9492
DJ
13533
13534 /* Back to the top, do it again. */
13535 }
13536}
13537
c906108c
SS
13538/* Read a minimal amount of information into the minimal die structure. */
13539
fe1b8b76 13540static gdb_byte *
dee91e82
DE
13541read_partial_die (const struct die_reader_specs *reader,
13542 struct partial_die_info *part_die,
13543 struct abbrev_info *abbrev, unsigned int abbrev_len,
13544 gdb_byte *info_ptr)
c906108c 13545{
dee91e82 13546 struct dwarf2_cu *cu = reader->cu;
bb5ed363 13547 struct objfile *objfile = cu->objfile;
dee91e82 13548 gdb_byte *buffer = reader->buffer;
fa238c03 13549 unsigned int i;
c906108c 13550 struct attribute attr;
c5aa993b 13551 int has_low_pc_attr = 0;
c906108c 13552 int has_high_pc_attr = 0;
91da1414 13553 int high_pc_relative = 0;
c906108c 13554
72bf9492 13555 memset (part_die, 0, sizeof (struct partial_die_info));
c906108c 13556
b64f50a1 13557 part_die->offset.sect_off = info_ptr - buffer;
72bf9492
DJ
13558
13559 info_ptr += abbrev_len;
13560
13561 if (abbrev == NULL)
13562 return info_ptr;
13563
c906108c
SS
13564 part_die->tag = abbrev->tag;
13565 part_die->has_children = abbrev->has_children;
c906108c
SS
13566
13567 for (i = 0; i < abbrev->num_attrs; ++i)
13568 {
dee91e82 13569 info_ptr = read_attribute (reader, &attr, &abbrev->attrs[i], info_ptr);
c906108c
SS
13570
13571 /* Store the data if it is of an attribute we want to keep in a
c5aa993b 13572 partial symbol table. */
c906108c
SS
13573 switch (attr.name)
13574 {
13575 case DW_AT_name:
71c25dea
TT
13576 switch (part_die->tag)
13577 {
13578 case DW_TAG_compile_unit:
95554aad 13579 case DW_TAG_partial_unit:
348e048f 13580 case DW_TAG_type_unit:
71c25dea
TT
13581 /* Compilation units have a DW_AT_name that is a filename, not
13582 a source language identifier. */
13583 case DW_TAG_enumeration_type:
13584 case DW_TAG_enumerator:
13585 /* These tags always have simple identifiers already; no need
13586 to canonicalize them. */
13587 part_die->name = DW_STRING (&attr);
13588 break;
13589 default:
13590 part_die->name
13591 = dwarf2_canonicalize_name (DW_STRING (&attr), cu,
bb5ed363 13592 &objfile->objfile_obstack);
71c25dea
TT
13593 break;
13594 }
c906108c 13595 break;
31ef98ae 13596 case DW_AT_linkage_name:
c906108c 13597 case DW_AT_MIPS_linkage_name:
31ef98ae
TT
13598 /* Note that both forms of linkage name might appear. We
13599 assume they will be the same, and we only store the last
13600 one we see. */
94af9270
KS
13601 if (cu->language == language_ada)
13602 part_die->name = DW_STRING (&attr);
abc72ce4 13603 part_die->linkage_name = DW_STRING (&attr);
c906108c
SS
13604 break;
13605 case DW_AT_low_pc:
13606 has_low_pc_attr = 1;
13607 part_die->lowpc = DW_ADDR (&attr);
13608 break;
13609 case DW_AT_high_pc:
13610 has_high_pc_attr = 1;
3019eac3
DE
13611 if (attr.form == DW_FORM_addr
13612 || attr.form == DW_FORM_GNU_addr_index)
91da1414
MW
13613 part_die->highpc = DW_ADDR (&attr);
13614 else
13615 {
13616 high_pc_relative = 1;
13617 part_die->highpc = DW_UNSND (&attr);
13618 }
c906108c
SS
13619 break;
13620 case DW_AT_location:
0963b4bd 13621 /* Support the .debug_loc offsets. */
8e19ed76
PS
13622 if (attr_form_is_block (&attr))
13623 {
95554aad 13624 part_die->d.locdesc = DW_BLOCK (&attr);
8e19ed76 13625 }
3690dd37 13626 else if (attr_form_is_section_offset (&attr))
8e19ed76 13627 {
4d3c2250 13628 dwarf2_complex_location_expr_complaint ();
8e19ed76
PS
13629 }
13630 else
13631 {
4d3c2250
KB
13632 dwarf2_invalid_attrib_class_complaint ("DW_AT_location",
13633 "partial symbol information");
8e19ed76 13634 }
c906108c 13635 break;
c906108c
SS
13636 case DW_AT_external:
13637 part_die->is_external = DW_UNSND (&attr);
13638 break;
13639 case DW_AT_declaration:
13640 part_die->is_declaration = DW_UNSND (&attr);
13641 break;
13642 case DW_AT_type:
13643 part_die->has_type = 1;
13644 break;
13645 case DW_AT_abstract_origin:
13646 case DW_AT_specification:
72bf9492
DJ
13647 case DW_AT_extension:
13648 part_die->has_specification = 1;
c764a876 13649 part_die->spec_offset = dwarf2_get_ref_die_offset (&attr);
36586728
TT
13650 part_die->spec_is_dwz = (attr.form == DW_FORM_GNU_ref_alt
13651 || cu->per_cu->is_dwz);
c906108c
SS
13652 break;
13653 case DW_AT_sibling:
13654 /* Ignore absolute siblings, they might point outside of
13655 the current compile unit. */
13656 if (attr.form == DW_FORM_ref_addr)
3e43a32a
MS
13657 complaint (&symfile_complaints,
13658 _("ignoring absolute DW_AT_sibling"));
c906108c 13659 else
b64f50a1 13660 part_die->sibling = buffer + dwarf2_get_ref_die_offset (&attr).sect_off;
c906108c 13661 break;
fa4028e9
JB
13662 case DW_AT_byte_size:
13663 part_die->has_byte_size = 1;
13664 break;
68511cec
CES
13665 case DW_AT_calling_convention:
13666 /* DWARF doesn't provide a way to identify a program's source-level
13667 entry point. DW_AT_calling_convention attributes are only meant
13668 to describe functions' calling conventions.
13669
13670 However, because it's a necessary piece of information in
13671 Fortran, and because DW_CC_program is the only piece of debugging
13672 information whose definition refers to a 'main program' at all,
13673 several compilers have begun marking Fortran main programs with
13674 DW_CC_program --- even when those functions use the standard
13675 calling conventions.
13676
13677 So until DWARF specifies a way to provide this information and
13678 compilers pick up the new representation, we'll support this
13679 practice. */
13680 if (DW_UNSND (&attr) == DW_CC_program
13681 && cu->language == language_fortran)
01f8c46d
JK
13682 {
13683 set_main_name (part_die->name);
13684
13685 /* As this DIE has a static linkage the name would be difficult
13686 to look up later. */
13687 language_of_main = language_fortran;
13688 }
68511cec 13689 break;
481860b3
GB
13690 case DW_AT_inline:
13691 if (DW_UNSND (&attr) == DW_INL_inlined
13692 || DW_UNSND (&attr) == DW_INL_declared_inlined)
13693 part_die->may_be_inlined = 1;
13694 break;
95554aad
TT
13695
13696 case DW_AT_import:
13697 if (part_die->tag == DW_TAG_imported_unit)
36586728
TT
13698 {
13699 part_die->d.offset = dwarf2_get_ref_die_offset (&attr);
13700 part_die->is_dwz = (attr.form == DW_FORM_GNU_ref_alt
13701 || cu->per_cu->is_dwz);
13702 }
95554aad
TT
13703 break;
13704
c906108c
SS
13705 default:
13706 break;
13707 }
13708 }
13709
91da1414
MW
13710 if (high_pc_relative)
13711 part_die->highpc += part_die->lowpc;
13712
9373cf26
JK
13713 if (has_low_pc_attr && has_high_pc_attr)
13714 {
13715 /* When using the GNU linker, .gnu.linkonce. sections are used to
13716 eliminate duplicate copies of functions and vtables and such.
13717 The linker will arbitrarily choose one and discard the others.
13718 The AT_*_pc values for such functions refer to local labels in
13719 these sections. If the section from that file was discarded, the
13720 labels are not in the output, so the relocs get a value of 0.
13721 If this is a discarded function, mark the pc bounds as invalid,
13722 so that GDB will ignore it. */
13723 if (part_die->lowpc == 0 && !dwarf2_per_objfile->has_section_at_zero)
13724 {
bb5ed363 13725 struct gdbarch *gdbarch = get_objfile_arch (objfile);
9373cf26
JK
13726
13727 complaint (&symfile_complaints,
13728 _("DW_AT_low_pc %s is zero "
13729 "for DIE at 0x%x [in module %s]"),
13730 paddress (gdbarch, part_die->lowpc),
b64f50a1 13731 part_die->offset.sect_off, objfile->name);
9373cf26
JK
13732 }
13733 /* dwarf2_get_pc_bounds has also the strict low < high requirement. */
13734 else if (part_die->lowpc >= part_die->highpc)
13735 {
bb5ed363 13736 struct gdbarch *gdbarch = get_objfile_arch (objfile);
9373cf26
JK
13737
13738 complaint (&symfile_complaints,
13739 _("DW_AT_low_pc %s is not < DW_AT_high_pc %s "
13740 "for DIE at 0x%x [in module %s]"),
13741 paddress (gdbarch, part_die->lowpc),
13742 paddress (gdbarch, part_die->highpc),
b64f50a1 13743 part_die->offset.sect_off, objfile->name);
9373cf26
JK
13744 }
13745 else
13746 part_die->has_pc_info = 1;
13747 }
85cbf3d3 13748
c906108c
SS
13749 return info_ptr;
13750}
13751
72bf9492
DJ
13752/* Find a cached partial DIE at OFFSET in CU. */
13753
13754static struct partial_die_info *
b64f50a1 13755find_partial_die_in_comp_unit (sect_offset offset, struct dwarf2_cu *cu)
72bf9492
DJ
13756{
13757 struct partial_die_info *lookup_die = NULL;
13758 struct partial_die_info part_die;
13759
13760 part_die.offset = offset;
b64f50a1
JK
13761 lookup_die = htab_find_with_hash (cu->partial_dies, &part_die,
13762 offset.sect_off);
72bf9492 13763
72bf9492
DJ
13764 return lookup_die;
13765}
13766
348e048f
DE
13767/* Find a partial DIE at OFFSET, which may or may not be in CU,
13768 except in the case of .debug_types DIEs which do not reference
13769 outside their CU (they do however referencing other types via
55f1336d 13770 DW_FORM_ref_sig8). */
72bf9492
DJ
13771
13772static struct partial_die_info *
36586728 13773find_partial_die (sect_offset offset, int offset_in_dwz, struct dwarf2_cu *cu)
72bf9492 13774{
bb5ed363 13775 struct objfile *objfile = cu->objfile;
5afb4e99
DJ
13776 struct dwarf2_per_cu_data *per_cu = NULL;
13777 struct partial_die_info *pd = NULL;
72bf9492 13778
36586728
TT
13779 if (offset_in_dwz == cu->per_cu->is_dwz
13780 && offset_in_cu_p (&cu->header, offset))
5afb4e99
DJ
13781 {
13782 pd = find_partial_die_in_comp_unit (offset, cu);
13783 if (pd != NULL)
13784 return pd;
0d99eb77
DE
13785 /* We missed recording what we needed.
13786 Load all dies and try again. */
13787 per_cu = cu->per_cu;
5afb4e99 13788 }
0d99eb77
DE
13789 else
13790 {
13791 /* TUs don't reference other CUs/TUs (except via type signatures). */
3019eac3 13792 if (cu->per_cu->is_debug_types)
0d99eb77
DE
13793 {
13794 error (_("Dwarf Error: Type Unit at offset 0x%lx contains"
13795 " external reference to offset 0x%lx [in module %s].\n"),
13796 (long) cu->header.offset.sect_off, (long) offset.sect_off,
13797 bfd_get_filename (objfile->obfd));
13798 }
36586728
TT
13799 per_cu = dwarf2_find_containing_comp_unit (offset, offset_in_dwz,
13800 objfile);
72bf9492 13801
0d99eb77
DE
13802 if (per_cu->cu == NULL || per_cu->cu->partial_dies == NULL)
13803 load_partial_comp_unit (per_cu);
ae038cb0 13804
0d99eb77
DE
13805 per_cu->cu->last_used = 0;
13806 pd = find_partial_die_in_comp_unit (offset, per_cu->cu);
13807 }
5afb4e99 13808
dee91e82
DE
13809 /* If we didn't find it, and not all dies have been loaded,
13810 load them all and try again. */
13811
5afb4e99
DJ
13812 if (pd == NULL && per_cu->load_all_dies == 0)
13813 {
5afb4e99 13814 per_cu->load_all_dies = 1;
fd820528
DE
13815
13816 /* This is nasty. When we reread the DIEs, somewhere up the call chain
13817 THIS_CU->cu may already be in use. So we can't just free it and
13818 replace its DIEs with the ones we read in. Instead, we leave those
13819 DIEs alone (which can still be in use, e.g. in scan_partial_symbols),
13820 and clobber THIS_CU->cu->partial_dies with the hash table for the new
13821 set. */
dee91e82 13822 load_partial_comp_unit (per_cu);
5afb4e99
DJ
13823
13824 pd = find_partial_die_in_comp_unit (offset, per_cu->cu);
13825 }
13826
13827 if (pd == NULL)
13828 internal_error (__FILE__, __LINE__,
3e43a32a
MS
13829 _("could not find partial DIE 0x%x "
13830 "in cache [from module %s]\n"),
b64f50a1 13831 offset.sect_off, bfd_get_filename (objfile->obfd));
5afb4e99 13832 return pd;
72bf9492
DJ
13833}
13834
abc72ce4
DE
13835/* See if we can figure out if the class lives in a namespace. We do
13836 this by looking for a member function; its demangled name will
13837 contain namespace info, if there is any. */
13838
13839static void
13840guess_partial_die_structure_name (struct partial_die_info *struct_pdi,
13841 struct dwarf2_cu *cu)
13842{
13843 /* NOTE: carlton/2003-10-07: Getting the info this way changes
13844 what template types look like, because the demangler
13845 frequently doesn't give the same name as the debug info. We
13846 could fix this by only using the demangled name to get the
13847 prefix (but see comment in read_structure_type). */
13848
13849 struct partial_die_info *real_pdi;
13850 struct partial_die_info *child_pdi;
13851
13852 /* If this DIE (this DIE's specification, if any) has a parent, then
13853 we should not do this. We'll prepend the parent's fully qualified
13854 name when we create the partial symbol. */
13855
13856 real_pdi = struct_pdi;
13857 while (real_pdi->has_specification)
36586728
TT
13858 real_pdi = find_partial_die (real_pdi->spec_offset,
13859 real_pdi->spec_is_dwz, cu);
abc72ce4
DE
13860
13861 if (real_pdi->die_parent != NULL)
13862 return;
13863
13864 for (child_pdi = struct_pdi->die_child;
13865 child_pdi != NULL;
13866 child_pdi = child_pdi->die_sibling)
13867 {
13868 if (child_pdi->tag == DW_TAG_subprogram
13869 && child_pdi->linkage_name != NULL)
13870 {
13871 char *actual_class_name
13872 = language_class_name_from_physname (cu->language_defn,
13873 child_pdi->linkage_name);
13874 if (actual_class_name != NULL)
13875 {
13876 struct_pdi->name
10f0c4bb
TT
13877 = obstack_copy0 (&cu->objfile->objfile_obstack,
13878 actual_class_name,
13879 strlen (actual_class_name));
abc72ce4
DE
13880 xfree (actual_class_name);
13881 }
13882 break;
13883 }
13884 }
13885}
13886
72bf9492
DJ
13887/* Adjust PART_DIE before generating a symbol for it. This function
13888 may set the is_external flag or change the DIE's name. */
13889
13890static void
13891fixup_partial_die (struct partial_die_info *part_die,
13892 struct dwarf2_cu *cu)
13893{
abc72ce4
DE
13894 /* Once we've fixed up a die, there's no point in doing so again.
13895 This also avoids a memory leak if we were to call
13896 guess_partial_die_structure_name multiple times. */
13897 if (part_die->fixup_called)
13898 return;
13899
72bf9492
DJ
13900 /* If we found a reference attribute and the DIE has no name, try
13901 to find a name in the referred to DIE. */
13902
13903 if (part_die->name == NULL && part_die->has_specification)
13904 {
13905 struct partial_die_info *spec_die;
72bf9492 13906
36586728
TT
13907 spec_die = find_partial_die (part_die->spec_offset,
13908 part_die->spec_is_dwz, cu);
72bf9492 13909
10b3939b 13910 fixup_partial_die (spec_die, cu);
72bf9492
DJ
13911
13912 if (spec_die->name)
13913 {
13914 part_die->name = spec_die->name;
13915
13916 /* Copy DW_AT_external attribute if it is set. */
13917 if (spec_die->is_external)
13918 part_die->is_external = spec_die->is_external;
13919 }
13920 }
13921
13922 /* Set default names for some unnamed DIEs. */
72bf9492
DJ
13923
13924 if (part_die->name == NULL && part_die->tag == DW_TAG_namespace)
2b1dbab0 13925 part_die->name = CP_ANONYMOUS_NAMESPACE_STR;
72bf9492 13926
abc72ce4
DE
13927 /* If there is no parent die to provide a namespace, and there are
13928 children, see if we can determine the namespace from their linkage
122d1940 13929 name. */
abc72ce4 13930 if (cu->language == language_cplus
8b70b953 13931 && !VEC_empty (dwarf2_section_info_def, dwarf2_per_objfile->types)
abc72ce4
DE
13932 && part_die->die_parent == NULL
13933 && part_die->has_children
13934 && (part_die->tag == DW_TAG_class_type
13935 || part_die->tag == DW_TAG_structure_type
13936 || part_die->tag == DW_TAG_union_type))
13937 guess_partial_die_structure_name (part_die, cu);
13938
53832f31
TT
13939 /* GCC might emit a nameless struct or union that has a linkage
13940 name. See http://gcc.gnu.org/bugzilla/show_bug.cgi?id=47510. */
13941 if (part_die->name == NULL
96408a79
SA
13942 && (part_die->tag == DW_TAG_class_type
13943 || part_die->tag == DW_TAG_interface_type
13944 || part_die->tag == DW_TAG_structure_type
13945 || part_die->tag == DW_TAG_union_type)
53832f31
TT
13946 && part_die->linkage_name != NULL)
13947 {
13948 char *demangled;
13949
13950 demangled = cplus_demangle (part_die->linkage_name, DMGL_TYPES);
13951 if (demangled)
13952 {
96408a79
SA
13953 const char *base;
13954
13955 /* Strip any leading namespaces/classes, keep only the base name.
13956 DW_AT_name for named DIEs does not contain the prefixes. */
13957 base = strrchr (demangled, ':');
13958 if (base && base > demangled && base[-1] == ':')
13959 base++;
13960 else
13961 base = demangled;
13962
10f0c4bb
TT
13963 part_die->name = obstack_copy0 (&cu->objfile->objfile_obstack,
13964 base, strlen (base));
53832f31
TT
13965 xfree (demangled);
13966 }
13967 }
13968
abc72ce4 13969 part_die->fixup_called = 1;
72bf9492
DJ
13970}
13971
a8329558 13972/* Read an attribute value described by an attribute form. */
c906108c 13973
fe1b8b76 13974static gdb_byte *
dee91e82
DE
13975read_attribute_value (const struct die_reader_specs *reader,
13976 struct attribute *attr, unsigned form,
13977 gdb_byte *info_ptr)
c906108c 13978{
dee91e82
DE
13979 struct dwarf2_cu *cu = reader->cu;
13980 bfd *abfd = reader->abfd;
e7c27a73 13981 struct comp_unit_head *cu_header = &cu->header;
c906108c
SS
13982 unsigned int bytes_read;
13983 struct dwarf_block *blk;
13984
a8329558
KW
13985 attr->form = form;
13986 switch (form)
c906108c 13987 {
c906108c 13988 case DW_FORM_ref_addr:
ae411497 13989 if (cu->header.version == 2)
4568ecf9 13990 DW_UNSND (attr) = read_address (abfd, info_ptr, cu, &bytes_read);
ae411497 13991 else
4568ecf9
DE
13992 DW_UNSND (attr) = read_offset (abfd, info_ptr,
13993 &cu->header, &bytes_read);
ae411497
TT
13994 info_ptr += bytes_read;
13995 break;
36586728
TT
13996 case DW_FORM_GNU_ref_alt:
13997 DW_UNSND (attr) = read_offset (abfd, info_ptr, &cu->header, &bytes_read);
13998 info_ptr += bytes_read;
13999 break;
ae411497 14000 case DW_FORM_addr:
e7c27a73 14001 DW_ADDR (attr) = read_address (abfd, info_ptr, cu, &bytes_read);
107d2387 14002 info_ptr += bytes_read;
c906108c
SS
14003 break;
14004 case DW_FORM_block2:
7b5a2f43 14005 blk = dwarf_alloc_block (cu);
c906108c
SS
14006 blk->size = read_2_bytes (abfd, info_ptr);
14007 info_ptr += 2;
14008 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
14009 info_ptr += blk->size;
14010 DW_BLOCK (attr) = blk;
14011 break;
14012 case DW_FORM_block4:
7b5a2f43 14013 blk = dwarf_alloc_block (cu);
c906108c
SS
14014 blk->size = read_4_bytes (abfd, info_ptr);
14015 info_ptr += 4;
14016 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
14017 info_ptr += blk->size;
14018 DW_BLOCK (attr) = blk;
14019 break;
14020 case DW_FORM_data2:
14021 DW_UNSND (attr) = read_2_bytes (abfd, info_ptr);
14022 info_ptr += 2;
14023 break;
14024 case DW_FORM_data4:
14025 DW_UNSND (attr) = read_4_bytes (abfd, info_ptr);
14026 info_ptr += 4;
14027 break;
14028 case DW_FORM_data8:
14029 DW_UNSND (attr) = read_8_bytes (abfd, info_ptr);
14030 info_ptr += 8;
14031 break;
2dc7f7b3
TT
14032 case DW_FORM_sec_offset:
14033 DW_UNSND (attr) = read_offset (abfd, info_ptr, &cu->header, &bytes_read);
14034 info_ptr += bytes_read;
14035 break;
c906108c 14036 case DW_FORM_string:
9b1c24c8 14037 DW_STRING (attr) = read_direct_string (abfd, info_ptr, &bytes_read);
8285870a 14038 DW_STRING_IS_CANONICAL (attr) = 0;
c906108c
SS
14039 info_ptr += bytes_read;
14040 break;
4bdf3d34 14041 case DW_FORM_strp:
36586728
TT
14042 if (!cu->per_cu->is_dwz)
14043 {
14044 DW_STRING (attr) = read_indirect_string (abfd, info_ptr, cu_header,
14045 &bytes_read);
14046 DW_STRING_IS_CANONICAL (attr) = 0;
14047 info_ptr += bytes_read;
14048 break;
14049 }
14050 /* FALLTHROUGH */
14051 case DW_FORM_GNU_strp_alt:
14052 {
14053 struct dwz_file *dwz = dwarf2_get_dwz_file ();
14054 LONGEST str_offset = read_offset (abfd, info_ptr, cu_header,
14055 &bytes_read);
14056
14057 DW_STRING (attr) = read_indirect_string_from_dwz (dwz, str_offset);
14058 DW_STRING_IS_CANONICAL (attr) = 0;
14059 info_ptr += bytes_read;
14060 }
4bdf3d34 14061 break;
2dc7f7b3 14062 case DW_FORM_exprloc:
c906108c 14063 case DW_FORM_block:
7b5a2f43 14064 blk = dwarf_alloc_block (cu);
c906108c
SS
14065 blk->size = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
14066 info_ptr += bytes_read;
14067 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
14068 info_ptr += blk->size;
14069 DW_BLOCK (attr) = blk;
14070 break;
14071 case DW_FORM_block1:
7b5a2f43 14072 blk = dwarf_alloc_block (cu);
c906108c
SS
14073 blk->size = read_1_byte (abfd, info_ptr);
14074 info_ptr += 1;
14075 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
14076 info_ptr += blk->size;
14077 DW_BLOCK (attr) = blk;
14078 break;
14079 case DW_FORM_data1:
14080 DW_UNSND (attr) = read_1_byte (abfd, info_ptr);
14081 info_ptr += 1;
14082 break;
14083 case DW_FORM_flag:
14084 DW_UNSND (attr) = read_1_byte (abfd, info_ptr);
14085 info_ptr += 1;
14086 break;
2dc7f7b3
TT
14087 case DW_FORM_flag_present:
14088 DW_UNSND (attr) = 1;
14089 break;
c906108c
SS
14090 case DW_FORM_sdata:
14091 DW_SND (attr) = read_signed_leb128 (abfd, info_ptr, &bytes_read);
14092 info_ptr += bytes_read;
14093 break;
14094 case DW_FORM_udata:
14095 DW_UNSND (attr) = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
14096 info_ptr += bytes_read;
14097 break;
14098 case DW_FORM_ref1:
4568ecf9
DE
14099 DW_UNSND (attr) = (cu->header.offset.sect_off
14100 + read_1_byte (abfd, info_ptr));
c906108c
SS
14101 info_ptr += 1;
14102 break;
14103 case DW_FORM_ref2:
4568ecf9
DE
14104 DW_UNSND (attr) = (cu->header.offset.sect_off
14105 + read_2_bytes (abfd, info_ptr));
c906108c
SS
14106 info_ptr += 2;
14107 break;
14108 case DW_FORM_ref4:
4568ecf9
DE
14109 DW_UNSND (attr) = (cu->header.offset.sect_off
14110 + read_4_bytes (abfd, info_ptr));
c906108c
SS
14111 info_ptr += 4;
14112 break;
613e1657 14113 case DW_FORM_ref8:
4568ecf9
DE
14114 DW_UNSND (attr) = (cu->header.offset.sect_off
14115 + read_8_bytes (abfd, info_ptr));
613e1657
KB
14116 info_ptr += 8;
14117 break;
55f1336d 14118 case DW_FORM_ref_sig8:
348e048f
DE
14119 /* Convert the signature to something we can record in DW_UNSND
14120 for later lookup.
14121 NOTE: This is NULL if the type wasn't found. */
14122 DW_SIGNATURED_TYPE (attr) =
e319fa28 14123 lookup_signatured_type (read_8_bytes (abfd, info_ptr));
348e048f
DE
14124 info_ptr += 8;
14125 break;
c906108c 14126 case DW_FORM_ref_udata:
4568ecf9
DE
14127 DW_UNSND (attr) = (cu->header.offset.sect_off
14128 + read_unsigned_leb128 (abfd, info_ptr, &bytes_read));
c906108c
SS
14129 info_ptr += bytes_read;
14130 break;
c906108c 14131 case DW_FORM_indirect:
a8329558
KW
14132 form = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
14133 info_ptr += bytes_read;
dee91e82 14134 info_ptr = read_attribute_value (reader, attr, form, info_ptr);
a8329558 14135 break;
3019eac3
DE
14136 case DW_FORM_GNU_addr_index:
14137 if (reader->dwo_file == NULL)
14138 {
14139 /* For now flag a hard error.
14140 Later we can turn this into a complaint. */
14141 error (_("Dwarf Error: %s found in non-DWO CU [in module %s]"),
14142 dwarf_form_name (form),
14143 bfd_get_filename (abfd));
14144 }
14145 DW_ADDR (attr) = read_addr_index_from_leb128 (cu, info_ptr, &bytes_read);
14146 info_ptr += bytes_read;
14147 break;
14148 case DW_FORM_GNU_str_index:
14149 if (reader->dwo_file == NULL)
14150 {
14151 /* For now flag a hard error.
14152 Later we can turn this into a complaint if warranted. */
14153 error (_("Dwarf Error: %s found in non-DWO CU [in module %s]"),
14154 dwarf_form_name (form),
14155 bfd_get_filename (abfd));
14156 }
14157 {
14158 ULONGEST str_index =
14159 read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
14160
14161 DW_STRING (attr) = read_str_index (reader, cu, str_index);
14162 DW_STRING_IS_CANONICAL (attr) = 0;
14163 info_ptr += bytes_read;
14164 }
14165 break;
c906108c 14166 default:
8a3fe4f8 14167 error (_("Dwarf Error: Cannot handle %s in DWARF reader [in module %s]"),
659b0389
ML
14168 dwarf_form_name (form),
14169 bfd_get_filename (abfd));
c906108c 14170 }
28e94949 14171
36586728
TT
14172 /* Super hack. */
14173 if (cu->per_cu->is_dwz && is_ref_attr (attr))
14174 attr->form = DW_FORM_GNU_ref_alt;
14175
28e94949
JB
14176 /* We have seen instances where the compiler tried to emit a byte
14177 size attribute of -1 which ended up being encoded as an unsigned
14178 0xffffffff. Although 0xffffffff is technically a valid size value,
14179 an object of this size seems pretty unlikely so we can relatively
14180 safely treat these cases as if the size attribute was invalid and
14181 treat them as zero by default. */
14182 if (attr->name == DW_AT_byte_size
14183 && form == DW_FORM_data4
14184 && DW_UNSND (attr) >= 0xffffffff)
01c66ae6
JB
14185 {
14186 complaint
14187 (&symfile_complaints,
43bbcdc2
PH
14188 _("Suspicious DW_AT_byte_size value treated as zero instead of %s"),
14189 hex_string (DW_UNSND (attr)));
01c66ae6
JB
14190 DW_UNSND (attr) = 0;
14191 }
28e94949 14192
c906108c
SS
14193 return info_ptr;
14194}
14195
a8329558
KW
14196/* Read an attribute described by an abbreviated attribute. */
14197
fe1b8b76 14198static gdb_byte *
dee91e82
DE
14199read_attribute (const struct die_reader_specs *reader,
14200 struct attribute *attr, struct attr_abbrev *abbrev,
14201 gdb_byte *info_ptr)
a8329558
KW
14202{
14203 attr->name = abbrev->name;
dee91e82 14204 return read_attribute_value (reader, attr, abbrev->form, info_ptr);
a8329558
KW
14205}
14206
0963b4bd 14207/* Read dwarf information from a buffer. */
c906108c
SS
14208
14209static unsigned int
a1855c1d 14210read_1_byte (bfd *abfd, const gdb_byte *buf)
c906108c 14211{
fe1b8b76 14212 return bfd_get_8 (abfd, buf);
c906108c
SS
14213}
14214
14215static int
a1855c1d 14216read_1_signed_byte (bfd *abfd, const gdb_byte *buf)
c906108c 14217{
fe1b8b76 14218 return bfd_get_signed_8 (abfd, buf);
c906108c
SS
14219}
14220
14221static unsigned int
a1855c1d 14222read_2_bytes (bfd *abfd, const gdb_byte *buf)
c906108c 14223{
fe1b8b76 14224 return bfd_get_16 (abfd, buf);
c906108c
SS
14225}
14226
21ae7a4d 14227static int
a1855c1d 14228read_2_signed_bytes (bfd *abfd, const gdb_byte *buf)
21ae7a4d
JK
14229{
14230 return bfd_get_signed_16 (abfd, buf);
14231}
14232
c906108c 14233static unsigned int
a1855c1d 14234read_4_bytes (bfd *abfd, const gdb_byte *buf)
c906108c 14235{
fe1b8b76 14236 return bfd_get_32 (abfd, buf);
c906108c
SS
14237}
14238
21ae7a4d 14239static int
a1855c1d 14240read_4_signed_bytes (bfd *abfd, const gdb_byte *buf)
21ae7a4d
JK
14241{
14242 return bfd_get_signed_32 (abfd, buf);
14243}
14244
93311388 14245static ULONGEST
a1855c1d 14246read_8_bytes (bfd *abfd, const gdb_byte *buf)
c906108c 14247{
fe1b8b76 14248 return bfd_get_64 (abfd, buf);
c906108c
SS
14249}
14250
14251static CORE_ADDR
fe1b8b76 14252read_address (bfd *abfd, gdb_byte *buf, struct dwarf2_cu *cu,
891d2f0b 14253 unsigned int *bytes_read)
c906108c 14254{
e7c27a73 14255 struct comp_unit_head *cu_header = &cu->header;
c906108c
SS
14256 CORE_ADDR retval = 0;
14257
107d2387 14258 if (cu_header->signed_addr_p)
c906108c 14259 {
107d2387
AC
14260 switch (cu_header->addr_size)
14261 {
14262 case 2:
fe1b8b76 14263 retval = bfd_get_signed_16 (abfd, buf);
107d2387
AC
14264 break;
14265 case 4:
fe1b8b76 14266 retval = bfd_get_signed_32 (abfd, buf);
107d2387
AC
14267 break;
14268 case 8:
fe1b8b76 14269 retval = bfd_get_signed_64 (abfd, buf);
107d2387
AC
14270 break;
14271 default:
8e65ff28 14272 internal_error (__FILE__, __LINE__,
e2e0b3e5 14273 _("read_address: bad switch, signed [in module %s]"),
659b0389 14274 bfd_get_filename (abfd));
107d2387
AC
14275 }
14276 }
14277 else
14278 {
14279 switch (cu_header->addr_size)
14280 {
14281 case 2:
fe1b8b76 14282 retval = bfd_get_16 (abfd, buf);
107d2387
AC
14283 break;
14284 case 4:
fe1b8b76 14285 retval = bfd_get_32 (abfd, buf);
107d2387
AC
14286 break;
14287 case 8:
fe1b8b76 14288 retval = bfd_get_64 (abfd, buf);
107d2387
AC
14289 break;
14290 default:
8e65ff28 14291 internal_error (__FILE__, __LINE__,
a73c6dcd
MS
14292 _("read_address: bad switch, "
14293 "unsigned [in module %s]"),
659b0389 14294 bfd_get_filename (abfd));
107d2387 14295 }
c906108c 14296 }
64367e0a 14297
107d2387
AC
14298 *bytes_read = cu_header->addr_size;
14299 return retval;
c906108c
SS
14300}
14301
f7ef9339 14302/* Read the initial length from a section. The (draft) DWARF 3
613e1657
KB
14303 specification allows the initial length to take up either 4 bytes
14304 or 12 bytes. If the first 4 bytes are 0xffffffff, then the next 8
14305 bytes describe the length and all offsets will be 8 bytes in length
14306 instead of 4.
14307
f7ef9339
KB
14308 An older, non-standard 64-bit format is also handled by this
14309 function. The older format in question stores the initial length
14310 as an 8-byte quantity without an escape value. Lengths greater
14311 than 2^32 aren't very common which means that the initial 4 bytes
14312 is almost always zero. Since a length value of zero doesn't make
14313 sense for the 32-bit format, this initial zero can be considered to
14314 be an escape value which indicates the presence of the older 64-bit
14315 format. As written, the code can't detect (old format) lengths
917c78fc
MK
14316 greater than 4GB. If it becomes necessary to handle lengths
14317 somewhat larger than 4GB, we could allow other small values (such
14318 as the non-sensical values of 1, 2, and 3) to also be used as
14319 escape values indicating the presence of the old format.
f7ef9339 14320
917c78fc
MK
14321 The value returned via bytes_read should be used to increment the
14322 relevant pointer after calling read_initial_length().
c764a876 14323
613e1657
KB
14324 [ Note: read_initial_length() and read_offset() are based on the
14325 document entitled "DWARF Debugging Information Format", revision
f7ef9339 14326 3, draft 8, dated November 19, 2001. This document was obtained
613e1657
KB
14327 from:
14328
f7ef9339 14329 http://reality.sgiweb.org/davea/dwarf3-draft8-011125.pdf
6e70227d 14330
613e1657
KB
14331 This document is only a draft and is subject to change. (So beware.)
14332
f7ef9339 14333 Details regarding the older, non-standard 64-bit format were
917c78fc
MK
14334 determined empirically by examining 64-bit ELF files produced by
14335 the SGI toolchain on an IRIX 6.5 machine.
f7ef9339
KB
14336
14337 - Kevin, July 16, 2002
613e1657
KB
14338 ] */
14339
14340static LONGEST
c764a876 14341read_initial_length (bfd *abfd, gdb_byte *buf, unsigned int *bytes_read)
613e1657 14342{
fe1b8b76 14343 LONGEST length = bfd_get_32 (abfd, buf);
613e1657 14344
dd373385 14345 if (length == 0xffffffff)
613e1657 14346 {
fe1b8b76 14347 length = bfd_get_64 (abfd, buf + 4);
613e1657 14348 *bytes_read = 12;
613e1657 14349 }
dd373385 14350 else if (length == 0)
f7ef9339 14351 {
dd373385 14352 /* Handle the (non-standard) 64-bit DWARF2 format used by IRIX. */
fe1b8b76 14353 length = bfd_get_64 (abfd, buf);
f7ef9339 14354 *bytes_read = 8;
f7ef9339 14355 }
613e1657
KB
14356 else
14357 {
14358 *bytes_read = 4;
613e1657
KB
14359 }
14360
c764a876
DE
14361 return length;
14362}
dd373385 14363
c764a876
DE
14364/* Cover function for read_initial_length.
14365 Returns the length of the object at BUF, and stores the size of the
14366 initial length in *BYTES_READ and stores the size that offsets will be in
14367 *OFFSET_SIZE.
14368 If the initial length size is not equivalent to that specified in
14369 CU_HEADER then issue a complaint.
14370 This is useful when reading non-comp-unit headers. */
dd373385 14371
c764a876
DE
14372static LONGEST
14373read_checked_initial_length_and_offset (bfd *abfd, gdb_byte *buf,
14374 const struct comp_unit_head *cu_header,
14375 unsigned int *bytes_read,
14376 unsigned int *offset_size)
14377{
14378 LONGEST length = read_initial_length (abfd, buf, bytes_read);
14379
14380 gdb_assert (cu_header->initial_length_size == 4
14381 || cu_header->initial_length_size == 8
14382 || cu_header->initial_length_size == 12);
14383
14384 if (cu_header->initial_length_size != *bytes_read)
14385 complaint (&symfile_complaints,
14386 _("intermixed 32-bit and 64-bit DWARF sections"));
dd373385 14387
c764a876 14388 *offset_size = (*bytes_read == 4) ? 4 : 8;
dd373385 14389 return length;
613e1657
KB
14390}
14391
14392/* Read an offset from the data stream. The size of the offset is
917c78fc 14393 given by cu_header->offset_size. */
613e1657
KB
14394
14395static LONGEST
fe1b8b76 14396read_offset (bfd *abfd, gdb_byte *buf, const struct comp_unit_head *cu_header,
891d2f0b 14397 unsigned int *bytes_read)
c764a876
DE
14398{
14399 LONGEST offset = read_offset_1 (abfd, buf, cu_header->offset_size);
9a619af0 14400
c764a876
DE
14401 *bytes_read = cu_header->offset_size;
14402 return offset;
14403}
14404
14405/* Read an offset from the data stream. */
14406
14407static LONGEST
14408read_offset_1 (bfd *abfd, gdb_byte *buf, unsigned int offset_size)
613e1657
KB
14409{
14410 LONGEST retval = 0;
14411
c764a876 14412 switch (offset_size)
613e1657
KB
14413 {
14414 case 4:
fe1b8b76 14415 retval = bfd_get_32 (abfd, buf);
613e1657
KB
14416 break;
14417 case 8:
fe1b8b76 14418 retval = bfd_get_64 (abfd, buf);
613e1657
KB
14419 break;
14420 default:
8e65ff28 14421 internal_error (__FILE__, __LINE__,
c764a876 14422 _("read_offset_1: bad switch [in module %s]"),
659b0389 14423 bfd_get_filename (abfd));
613e1657
KB
14424 }
14425
917c78fc 14426 return retval;
613e1657
KB
14427}
14428
fe1b8b76
JB
14429static gdb_byte *
14430read_n_bytes (bfd *abfd, gdb_byte *buf, unsigned int size)
c906108c
SS
14431{
14432 /* If the size of a host char is 8 bits, we can return a pointer
14433 to the buffer, otherwise we have to copy the data to a buffer
14434 allocated on the temporary obstack. */
4bdf3d34 14435 gdb_assert (HOST_CHAR_BIT == 8);
c906108c 14436 return buf;
c906108c
SS
14437}
14438
14439static char *
9b1c24c8 14440read_direct_string (bfd *abfd, gdb_byte *buf, unsigned int *bytes_read_ptr)
c906108c
SS
14441{
14442 /* If the size of a host char is 8 bits, we can return a pointer
14443 to the string, otherwise we have to copy the string to a buffer
14444 allocated on the temporary obstack. */
4bdf3d34 14445 gdb_assert (HOST_CHAR_BIT == 8);
c906108c
SS
14446 if (*buf == '\0')
14447 {
14448 *bytes_read_ptr = 1;
14449 return NULL;
14450 }
fe1b8b76
JB
14451 *bytes_read_ptr = strlen ((char *) buf) + 1;
14452 return (char *) buf;
4bdf3d34
JJ
14453}
14454
14455static char *
cf2c3c16 14456read_indirect_string_at_offset (bfd *abfd, LONGEST str_offset)
4bdf3d34 14457{
be391dca 14458 dwarf2_read_section (dwarf2_per_objfile->objfile, &dwarf2_per_objfile->str);
dce234bc 14459 if (dwarf2_per_objfile->str.buffer == NULL)
cf2c3c16
TT
14460 error (_("DW_FORM_strp used without .debug_str section [in module %s]"),
14461 bfd_get_filename (abfd));
dce234bc 14462 if (str_offset >= dwarf2_per_objfile->str.size)
cf2c3c16
TT
14463 error (_("DW_FORM_strp pointing outside of "
14464 ".debug_str section [in module %s]"),
14465 bfd_get_filename (abfd));
4bdf3d34 14466 gdb_assert (HOST_CHAR_BIT == 8);
dce234bc 14467 if (dwarf2_per_objfile->str.buffer[str_offset] == '\0')
4bdf3d34 14468 return NULL;
dce234bc 14469 return (char *) (dwarf2_per_objfile->str.buffer + str_offset);
c906108c
SS
14470}
14471
36586728
TT
14472/* Read a string at offset STR_OFFSET in the .debug_str section from
14473 the .dwz file DWZ. Throw an error if the offset is too large. If
14474 the string consists of a single NUL byte, return NULL; otherwise
14475 return a pointer to the string. */
14476
14477static char *
14478read_indirect_string_from_dwz (struct dwz_file *dwz, LONGEST str_offset)
14479{
14480 dwarf2_read_section (dwarf2_per_objfile->objfile, &dwz->str);
14481
14482 if (dwz->str.buffer == NULL)
14483 error (_("DW_FORM_GNU_strp_alt used without .debug_str "
14484 "section [in module %s]"),
14485 bfd_get_filename (dwz->dwz_bfd));
14486 if (str_offset >= dwz->str.size)
14487 error (_("DW_FORM_GNU_strp_alt pointing outside of "
14488 ".debug_str section [in module %s]"),
14489 bfd_get_filename (dwz->dwz_bfd));
14490 gdb_assert (HOST_CHAR_BIT == 8);
14491 if (dwz->str.buffer[str_offset] == '\0')
14492 return NULL;
14493 return (char *) (dwz->str.buffer + str_offset);
14494}
14495
cf2c3c16
TT
14496static char *
14497read_indirect_string (bfd *abfd, gdb_byte *buf,
14498 const struct comp_unit_head *cu_header,
14499 unsigned int *bytes_read_ptr)
14500{
14501 LONGEST str_offset = read_offset (abfd, buf, cu_header, bytes_read_ptr);
14502
14503 return read_indirect_string_at_offset (abfd, str_offset);
14504}
14505
12df843f 14506static ULONGEST
fe1b8b76 14507read_unsigned_leb128 (bfd *abfd, gdb_byte *buf, unsigned int *bytes_read_ptr)
c906108c 14508{
12df843f 14509 ULONGEST result;
ce5d95e1 14510 unsigned int num_read;
c906108c
SS
14511 int i, shift;
14512 unsigned char byte;
14513
14514 result = 0;
14515 shift = 0;
14516 num_read = 0;
14517 i = 0;
14518 while (1)
14519 {
fe1b8b76 14520 byte = bfd_get_8 (abfd, buf);
c906108c
SS
14521 buf++;
14522 num_read++;
12df843f 14523 result |= ((ULONGEST) (byte & 127) << shift);
c906108c
SS
14524 if ((byte & 128) == 0)
14525 {
14526 break;
14527 }
14528 shift += 7;
14529 }
14530 *bytes_read_ptr = num_read;
14531 return result;
14532}
14533
12df843f 14534static LONGEST
fe1b8b76 14535read_signed_leb128 (bfd *abfd, gdb_byte *buf, unsigned int *bytes_read_ptr)
c906108c 14536{
12df843f 14537 LONGEST result;
77e0b926 14538 int i, shift, num_read;
c906108c
SS
14539 unsigned char byte;
14540
14541 result = 0;
14542 shift = 0;
c906108c
SS
14543 num_read = 0;
14544 i = 0;
14545 while (1)
14546 {
fe1b8b76 14547 byte = bfd_get_8 (abfd, buf);
c906108c
SS
14548 buf++;
14549 num_read++;
12df843f 14550 result |= ((LONGEST) (byte & 127) << shift);
c906108c
SS
14551 shift += 7;
14552 if ((byte & 128) == 0)
14553 {
14554 break;
14555 }
14556 }
77e0b926 14557 if ((shift < 8 * sizeof (result)) && (byte & 0x40))
12df843f 14558 result |= -(((LONGEST) 1) << shift);
c906108c
SS
14559 *bytes_read_ptr = num_read;
14560 return result;
14561}
14562
3019eac3
DE
14563/* Given index ADDR_INDEX in .debug_addr, fetch the value.
14564 ADDR_BASE is the DW_AT_GNU_addr_base attribute or zero.
14565 ADDR_SIZE is the size of addresses from the CU header. */
14566
14567static CORE_ADDR
14568read_addr_index_1 (unsigned int addr_index, ULONGEST addr_base, int addr_size)
14569{
14570 struct objfile *objfile = dwarf2_per_objfile->objfile;
14571 bfd *abfd = objfile->obfd;
14572 const gdb_byte *info_ptr;
14573
14574 dwarf2_read_section (objfile, &dwarf2_per_objfile->addr);
14575 if (dwarf2_per_objfile->addr.buffer == NULL)
14576 error (_("DW_FORM_addr_index used without .debug_addr section [in module %s]"),
14577 objfile->name);
14578 if (addr_base + addr_index * addr_size >= dwarf2_per_objfile->addr.size)
14579 error (_("DW_FORM_addr_index pointing outside of "
14580 ".debug_addr section [in module %s]"),
14581 objfile->name);
14582 info_ptr = (dwarf2_per_objfile->addr.buffer
14583 + addr_base + addr_index * addr_size);
14584 if (addr_size == 4)
14585 return bfd_get_32 (abfd, info_ptr);
14586 else
14587 return bfd_get_64 (abfd, info_ptr);
14588}
14589
14590/* Given index ADDR_INDEX in .debug_addr, fetch the value. */
14591
14592static CORE_ADDR
14593read_addr_index (struct dwarf2_cu *cu, unsigned int addr_index)
14594{
14595 return read_addr_index_1 (addr_index, cu->addr_base, cu->header.addr_size);
14596}
14597
14598/* Given a pointer to an leb128 value, fetch the value from .debug_addr. */
14599
14600static CORE_ADDR
14601read_addr_index_from_leb128 (struct dwarf2_cu *cu, gdb_byte *info_ptr,
14602 unsigned int *bytes_read)
14603{
14604 bfd *abfd = cu->objfile->obfd;
14605 unsigned int addr_index = read_unsigned_leb128 (abfd, info_ptr, bytes_read);
14606
14607 return read_addr_index (cu, addr_index);
14608}
14609
14610/* Data structure to pass results from dwarf2_read_addr_index_reader
14611 back to dwarf2_read_addr_index. */
14612
14613struct dwarf2_read_addr_index_data
14614{
14615 ULONGEST addr_base;
14616 int addr_size;
14617};
14618
14619/* die_reader_func for dwarf2_read_addr_index. */
14620
14621static void
14622dwarf2_read_addr_index_reader (const struct die_reader_specs *reader,
14623 gdb_byte *info_ptr,
14624 struct die_info *comp_unit_die,
14625 int has_children,
14626 void *data)
14627{
14628 struct dwarf2_cu *cu = reader->cu;
14629 struct dwarf2_read_addr_index_data *aidata =
14630 (struct dwarf2_read_addr_index_data *) data;
14631
14632 aidata->addr_base = cu->addr_base;
14633 aidata->addr_size = cu->header.addr_size;
14634}
14635
14636/* Given an index in .debug_addr, fetch the value.
14637 NOTE: This can be called during dwarf expression evaluation,
14638 long after the debug information has been read, and thus per_cu->cu
14639 may no longer exist. */
14640
14641CORE_ADDR
14642dwarf2_read_addr_index (struct dwarf2_per_cu_data *per_cu,
14643 unsigned int addr_index)
14644{
14645 struct objfile *objfile = per_cu->objfile;
14646 struct dwarf2_cu *cu = per_cu->cu;
14647 ULONGEST addr_base;
14648 int addr_size;
14649
14650 /* This is intended to be called from outside this file. */
14651 dw2_setup (objfile);
14652
14653 /* We need addr_base and addr_size.
14654 If we don't have PER_CU->cu, we have to get it.
14655 Nasty, but the alternative is storing the needed info in PER_CU,
14656 which at this point doesn't seem justified: it's not clear how frequently
14657 it would get used and it would increase the size of every PER_CU.
14658 Entry points like dwarf2_per_cu_addr_size do a similar thing
14659 so we're not in uncharted territory here.
14660 Alas we need to be a bit more complicated as addr_base is contained
14661 in the DIE.
14662
14663 We don't need to read the entire CU(/TU).
14664 We just need the header and top level die.
a1b64ce1 14665
3019eac3 14666 IWBN to use the aging mechanism to let us lazily later discard the CU.
a1b64ce1 14667 For now we skip this optimization. */
3019eac3
DE
14668
14669 if (cu != NULL)
14670 {
14671 addr_base = cu->addr_base;
14672 addr_size = cu->header.addr_size;
14673 }
14674 else
14675 {
14676 struct dwarf2_read_addr_index_data aidata;
14677
a1b64ce1
DE
14678 /* Note: We can't use init_cutu_and_read_dies_simple here,
14679 we need addr_base. */
14680 init_cutu_and_read_dies (per_cu, NULL, 0, 0,
14681 dwarf2_read_addr_index_reader, &aidata);
3019eac3
DE
14682 addr_base = aidata.addr_base;
14683 addr_size = aidata.addr_size;
14684 }
14685
14686 return read_addr_index_1 (addr_index, addr_base, addr_size);
14687}
14688
14689/* Given a DW_AT_str_index, fetch the string. */
14690
14691static char *
14692read_str_index (const struct die_reader_specs *reader,
14693 struct dwarf2_cu *cu, ULONGEST str_index)
14694{
14695 struct objfile *objfile = dwarf2_per_objfile->objfile;
14696 const char *dwo_name = objfile->name;
14697 bfd *abfd = objfile->obfd;
14698 struct dwo_sections *sections = &reader->dwo_file->sections;
14699 gdb_byte *info_ptr;
14700 ULONGEST str_offset;
14701
14702 dwarf2_read_section (objfile, &sections->str);
14703 dwarf2_read_section (objfile, &sections->str_offsets);
14704 if (sections->str.buffer == NULL)
14705 error (_("DW_FORM_str_index used without .debug_str.dwo section"
14706 " in CU at offset 0x%lx [in module %s]"),
14707 (long) cu->header.offset.sect_off, dwo_name);
14708 if (sections->str_offsets.buffer == NULL)
14709 error (_("DW_FORM_str_index used without .debug_str_offsets.dwo section"
14710 " in CU at offset 0x%lx [in module %s]"),
14711 (long) cu->header.offset.sect_off, dwo_name);
14712 if (str_index * cu->header.offset_size >= sections->str_offsets.size)
14713 error (_("DW_FORM_str_index pointing outside of .debug_str_offsets.dwo"
14714 " section in CU at offset 0x%lx [in module %s]"),
14715 (long) cu->header.offset.sect_off, dwo_name);
14716 info_ptr = (sections->str_offsets.buffer
14717 + str_index * cu->header.offset_size);
14718 if (cu->header.offset_size == 4)
14719 str_offset = bfd_get_32 (abfd, info_ptr);
14720 else
14721 str_offset = bfd_get_64 (abfd, info_ptr);
14722 if (str_offset >= sections->str.size)
14723 error (_("Offset from DW_FORM_str_index pointing outside of"
14724 " .debug_str.dwo section in CU at offset 0x%lx [in module %s]"),
14725 (long) cu->header.offset.sect_off, dwo_name);
14726 return (char *) (sections->str.buffer + str_offset);
14727}
14728
3019eac3
DE
14729/* Return the length of an LEB128 number in BUF. */
14730
14731static int
14732leb128_size (const gdb_byte *buf)
14733{
14734 const gdb_byte *begin = buf;
14735 gdb_byte byte;
14736
14737 while (1)
14738 {
14739 byte = *buf++;
14740 if ((byte & 128) == 0)
14741 return buf - begin;
14742 }
14743}
14744
c906108c 14745static void
e142c38c 14746set_cu_language (unsigned int lang, struct dwarf2_cu *cu)
c906108c
SS
14747{
14748 switch (lang)
14749 {
14750 case DW_LANG_C89:
76bee0cc 14751 case DW_LANG_C99:
c906108c 14752 case DW_LANG_C:
e142c38c 14753 cu->language = language_c;
c906108c
SS
14754 break;
14755 case DW_LANG_C_plus_plus:
e142c38c 14756 cu->language = language_cplus;
c906108c 14757 break;
6aecb9c2
JB
14758 case DW_LANG_D:
14759 cu->language = language_d;
14760 break;
c906108c
SS
14761 case DW_LANG_Fortran77:
14762 case DW_LANG_Fortran90:
b21b22e0 14763 case DW_LANG_Fortran95:
e142c38c 14764 cu->language = language_fortran;
c906108c 14765 break;
a766d390
DE
14766 case DW_LANG_Go:
14767 cu->language = language_go;
14768 break;
c906108c 14769 case DW_LANG_Mips_Assembler:
e142c38c 14770 cu->language = language_asm;
c906108c 14771 break;
bebd888e 14772 case DW_LANG_Java:
e142c38c 14773 cu->language = language_java;
bebd888e 14774 break;
c906108c 14775 case DW_LANG_Ada83:
8aaf0b47 14776 case DW_LANG_Ada95:
bc5f45f8
JB
14777 cu->language = language_ada;
14778 break;
72019c9c
GM
14779 case DW_LANG_Modula2:
14780 cu->language = language_m2;
14781 break;
fe8e67fd
PM
14782 case DW_LANG_Pascal83:
14783 cu->language = language_pascal;
14784 break;
22566fbd
DJ
14785 case DW_LANG_ObjC:
14786 cu->language = language_objc;
14787 break;
c906108c
SS
14788 case DW_LANG_Cobol74:
14789 case DW_LANG_Cobol85:
c906108c 14790 default:
e142c38c 14791 cu->language = language_minimal;
c906108c
SS
14792 break;
14793 }
e142c38c 14794 cu->language_defn = language_def (cu->language);
c906108c
SS
14795}
14796
14797/* Return the named attribute or NULL if not there. */
14798
14799static struct attribute *
e142c38c 14800dwarf2_attr (struct die_info *die, unsigned int name, struct dwarf2_cu *cu)
c906108c 14801{
a48e046c 14802 for (;;)
c906108c 14803 {
a48e046c
TT
14804 unsigned int i;
14805 struct attribute *spec = NULL;
14806
14807 for (i = 0; i < die->num_attrs; ++i)
14808 {
14809 if (die->attrs[i].name == name)
14810 return &die->attrs[i];
14811 if (die->attrs[i].name == DW_AT_specification
14812 || die->attrs[i].name == DW_AT_abstract_origin)
14813 spec = &die->attrs[i];
14814 }
14815
14816 if (!spec)
14817 break;
c906108c 14818
f2f0e013 14819 die = follow_die_ref (die, spec, &cu);
f2f0e013 14820 }
c5aa993b 14821
c906108c
SS
14822 return NULL;
14823}
14824
348e048f
DE
14825/* Return the named attribute or NULL if not there,
14826 but do not follow DW_AT_specification, etc.
14827 This is for use in contexts where we're reading .debug_types dies.
14828 Following DW_AT_specification, DW_AT_abstract_origin will take us
14829 back up the chain, and we want to go down. */
14830
14831static struct attribute *
45e58e77 14832dwarf2_attr_no_follow (struct die_info *die, unsigned int name)
348e048f
DE
14833{
14834 unsigned int i;
14835
14836 for (i = 0; i < die->num_attrs; ++i)
14837 if (die->attrs[i].name == name)
14838 return &die->attrs[i];
14839
14840 return NULL;
14841}
14842
05cf31d1
JB
14843/* Return non-zero iff the attribute NAME is defined for the given DIE,
14844 and holds a non-zero value. This function should only be used for
2dc7f7b3 14845 DW_FORM_flag or DW_FORM_flag_present attributes. */
05cf31d1
JB
14846
14847static int
14848dwarf2_flag_true_p (struct die_info *die, unsigned name, struct dwarf2_cu *cu)
14849{
14850 struct attribute *attr = dwarf2_attr (die, name, cu);
14851
14852 return (attr && DW_UNSND (attr));
14853}
14854
3ca72b44 14855static int
e142c38c 14856die_is_declaration (struct die_info *die, struct dwarf2_cu *cu)
3ca72b44 14857{
05cf31d1
JB
14858 /* A DIE is a declaration if it has a DW_AT_declaration attribute
14859 which value is non-zero. However, we have to be careful with
14860 DIEs having a DW_AT_specification attribute, because dwarf2_attr()
14861 (via dwarf2_flag_true_p) follows this attribute. So we may
14862 end up accidently finding a declaration attribute that belongs
14863 to a different DIE referenced by the specification attribute,
14864 even though the given DIE does not have a declaration attribute. */
14865 return (dwarf2_flag_true_p (die, DW_AT_declaration, cu)
14866 && dwarf2_attr (die, DW_AT_specification, cu) == NULL);
3ca72b44
AC
14867}
14868
63d06c5c 14869/* Return the die giving the specification for DIE, if there is
f2f0e013 14870 one. *SPEC_CU is the CU containing DIE on input, and the CU
edb3359d
DJ
14871 containing the return value on output. If there is no
14872 specification, but there is an abstract origin, that is
14873 returned. */
63d06c5c
DC
14874
14875static struct die_info *
f2f0e013 14876die_specification (struct die_info *die, struct dwarf2_cu **spec_cu)
63d06c5c 14877{
f2f0e013
DJ
14878 struct attribute *spec_attr = dwarf2_attr (die, DW_AT_specification,
14879 *spec_cu);
63d06c5c 14880
edb3359d
DJ
14881 if (spec_attr == NULL)
14882 spec_attr = dwarf2_attr (die, DW_AT_abstract_origin, *spec_cu);
14883
63d06c5c
DC
14884 if (spec_attr == NULL)
14885 return NULL;
14886 else
f2f0e013 14887 return follow_die_ref (die, spec_attr, spec_cu);
63d06c5c 14888}
c906108c 14889
debd256d 14890/* Free the line_header structure *LH, and any arrays and strings it
ae2de4f8
DE
14891 refers to.
14892 NOTE: This is also used as a "cleanup" function. */
14893
debd256d
JB
14894static void
14895free_line_header (struct line_header *lh)
14896{
14897 if (lh->standard_opcode_lengths)
a8bc7b56 14898 xfree (lh->standard_opcode_lengths);
debd256d
JB
14899
14900 /* Remember that all the lh->file_names[i].name pointers are
14901 pointers into debug_line_buffer, and don't need to be freed. */
14902 if (lh->file_names)
a8bc7b56 14903 xfree (lh->file_names);
debd256d
JB
14904
14905 /* Similarly for the include directory names. */
14906 if (lh->include_dirs)
a8bc7b56 14907 xfree (lh->include_dirs);
debd256d 14908
a8bc7b56 14909 xfree (lh);
debd256d
JB
14910}
14911
debd256d 14912/* Add an entry to LH's include directory table. */
ae2de4f8 14913
debd256d
JB
14914static void
14915add_include_dir (struct line_header *lh, char *include_dir)
c906108c 14916{
debd256d
JB
14917 /* Grow the array if necessary. */
14918 if (lh->include_dirs_size == 0)
c5aa993b 14919 {
debd256d
JB
14920 lh->include_dirs_size = 1; /* for testing */
14921 lh->include_dirs = xmalloc (lh->include_dirs_size
14922 * sizeof (*lh->include_dirs));
14923 }
14924 else if (lh->num_include_dirs >= lh->include_dirs_size)
14925 {
14926 lh->include_dirs_size *= 2;
14927 lh->include_dirs = xrealloc (lh->include_dirs,
14928 (lh->include_dirs_size
14929 * sizeof (*lh->include_dirs)));
c5aa993b 14930 }
c906108c 14931
debd256d
JB
14932 lh->include_dirs[lh->num_include_dirs++] = include_dir;
14933}
6e70227d 14934
debd256d 14935/* Add an entry to LH's file name table. */
ae2de4f8 14936
debd256d
JB
14937static void
14938add_file_name (struct line_header *lh,
14939 char *name,
14940 unsigned int dir_index,
14941 unsigned int mod_time,
14942 unsigned int length)
14943{
14944 struct file_entry *fe;
14945
14946 /* Grow the array if necessary. */
14947 if (lh->file_names_size == 0)
14948 {
14949 lh->file_names_size = 1; /* for testing */
14950 lh->file_names = xmalloc (lh->file_names_size
14951 * sizeof (*lh->file_names));
14952 }
14953 else if (lh->num_file_names >= lh->file_names_size)
14954 {
14955 lh->file_names_size *= 2;
14956 lh->file_names = xrealloc (lh->file_names,
14957 (lh->file_names_size
14958 * sizeof (*lh->file_names)));
14959 }
14960
14961 fe = &lh->file_names[lh->num_file_names++];
14962 fe->name = name;
14963 fe->dir_index = dir_index;
14964 fe->mod_time = mod_time;
14965 fe->length = length;
aaa75496 14966 fe->included_p = 0;
cb1df416 14967 fe->symtab = NULL;
debd256d 14968}
6e70227d 14969
36586728
TT
14970/* A convenience function to find the proper .debug_line section for a
14971 CU. */
14972
14973static struct dwarf2_section_info *
14974get_debug_line_section (struct dwarf2_cu *cu)
14975{
14976 struct dwarf2_section_info *section;
14977
14978 /* For TUs in DWO files, the DW_AT_stmt_list attribute lives in the
14979 DWO file. */
14980 if (cu->dwo_unit && cu->per_cu->is_debug_types)
14981 section = &cu->dwo_unit->dwo_file->sections.line;
14982 else if (cu->per_cu->is_dwz)
14983 {
14984 struct dwz_file *dwz = dwarf2_get_dwz_file ();
14985
14986 section = &dwz->line;
14987 }
14988 else
14989 section = &dwarf2_per_objfile->line;
14990
14991 return section;
14992}
14993
debd256d 14994/* Read the statement program header starting at OFFSET in
3019eac3 14995 .debug_line, or .debug_line.dwo. Return a pointer
6502dd73 14996 to a struct line_header, allocated using xmalloc.
debd256d
JB
14997
14998 NOTE: the strings in the include directory and file name tables of
3019eac3
DE
14999 the returned object point into the dwarf line section buffer,
15000 and must not be freed. */
ae2de4f8 15001
debd256d 15002static struct line_header *
3019eac3 15003dwarf_decode_line_header (unsigned int offset, struct dwarf2_cu *cu)
debd256d
JB
15004{
15005 struct cleanup *back_to;
15006 struct line_header *lh;
fe1b8b76 15007 gdb_byte *line_ptr;
c764a876 15008 unsigned int bytes_read, offset_size;
debd256d
JB
15009 int i;
15010 char *cur_dir, *cur_file;
3019eac3
DE
15011 struct dwarf2_section_info *section;
15012 bfd *abfd;
15013
36586728 15014 section = get_debug_line_section (cu);
3019eac3
DE
15015 dwarf2_read_section (dwarf2_per_objfile->objfile, section);
15016 if (section->buffer == NULL)
debd256d 15017 {
3019eac3
DE
15018 if (cu->dwo_unit && cu->per_cu->is_debug_types)
15019 complaint (&symfile_complaints, _("missing .debug_line.dwo section"));
15020 else
15021 complaint (&symfile_complaints, _("missing .debug_line section"));
debd256d
JB
15022 return 0;
15023 }
15024
fceca515
DE
15025 /* We can't do this until we know the section is non-empty.
15026 Only then do we know we have such a section. */
15027 abfd = section->asection->owner;
15028
a738430d
MK
15029 /* Make sure that at least there's room for the total_length field.
15030 That could be 12 bytes long, but we're just going to fudge that. */
3019eac3 15031 if (offset + 4 >= section->size)
debd256d 15032 {
4d3c2250 15033 dwarf2_statement_list_fits_in_line_number_section_complaint ();
debd256d
JB
15034 return 0;
15035 }
15036
15037 lh = xmalloc (sizeof (*lh));
15038 memset (lh, 0, sizeof (*lh));
15039 back_to = make_cleanup ((make_cleanup_ftype *) free_line_header,
15040 (void *) lh);
15041
3019eac3 15042 line_ptr = section->buffer + offset;
debd256d 15043
a738430d 15044 /* Read in the header. */
6e70227d 15045 lh->total_length =
c764a876
DE
15046 read_checked_initial_length_and_offset (abfd, line_ptr, &cu->header,
15047 &bytes_read, &offset_size);
debd256d 15048 line_ptr += bytes_read;
3019eac3 15049 if (line_ptr + lh->total_length > (section->buffer + section->size))
debd256d 15050 {
4d3c2250 15051 dwarf2_statement_list_fits_in_line_number_section_complaint ();
debd256d
JB
15052 return 0;
15053 }
15054 lh->statement_program_end = line_ptr + lh->total_length;
15055 lh->version = read_2_bytes (abfd, line_ptr);
15056 line_ptr += 2;
c764a876
DE
15057 lh->header_length = read_offset_1 (abfd, line_ptr, offset_size);
15058 line_ptr += offset_size;
debd256d
JB
15059 lh->minimum_instruction_length = read_1_byte (abfd, line_ptr);
15060 line_ptr += 1;
2dc7f7b3
TT
15061 if (lh->version >= 4)
15062 {
15063 lh->maximum_ops_per_instruction = read_1_byte (abfd, line_ptr);
15064 line_ptr += 1;
15065 }
15066 else
15067 lh->maximum_ops_per_instruction = 1;
15068
15069 if (lh->maximum_ops_per_instruction == 0)
15070 {
15071 lh->maximum_ops_per_instruction = 1;
15072 complaint (&symfile_complaints,
3e43a32a
MS
15073 _("invalid maximum_ops_per_instruction "
15074 "in `.debug_line' section"));
2dc7f7b3
TT
15075 }
15076
debd256d
JB
15077 lh->default_is_stmt = read_1_byte (abfd, line_ptr);
15078 line_ptr += 1;
15079 lh->line_base = read_1_signed_byte (abfd, line_ptr);
15080 line_ptr += 1;
15081 lh->line_range = read_1_byte (abfd, line_ptr);
15082 line_ptr += 1;
15083 lh->opcode_base = read_1_byte (abfd, line_ptr);
15084 line_ptr += 1;
15085 lh->standard_opcode_lengths
fe1b8b76 15086 = xmalloc (lh->opcode_base * sizeof (lh->standard_opcode_lengths[0]));
debd256d
JB
15087
15088 lh->standard_opcode_lengths[0] = 1; /* This should never be used anyway. */
15089 for (i = 1; i < lh->opcode_base; ++i)
15090 {
15091 lh->standard_opcode_lengths[i] = read_1_byte (abfd, line_ptr);
15092 line_ptr += 1;
15093 }
15094
a738430d 15095 /* Read directory table. */
9b1c24c8 15096 while ((cur_dir = read_direct_string (abfd, line_ptr, &bytes_read)) != NULL)
debd256d
JB
15097 {
15098 line_ptr += bytes_read;
15099 add_include_dir (lh, cur_dir);
15100 }
15101 line_ptr += bytes_read;
15102
a738430d 15103 /* Read file name table. */
9b1c24c8 15104 while ((cur_file = read_direct_string (abfd, line_ptr, &bytes_read)) != NULL)
debd256d
JB
15105 {
15106 unsigned int dir_index, mod_time, length;
15107
15108 line_ptr += bytes_read;
15109 dir_index = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
15110 line_ptr += bytes_read;
15111 mod_time = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
15112 line_ptr += bytes_read;
15113 length = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
15114 line_ptr += bytes_read;
15115
15116 add_file_name (lh, cur_file, dir_index, mod_time, length);
15117 }
15118 line_ptr += bytes_read;
6e70227d 15119 lh->statement_program_start = line_ptr;
debd256d 15120
3019eac3 15121 if (line_ptr > (section->buffer + section->size))
4d3c2250 15122 complaint (&symfile_complaints,
3e43a32a
MS
15123 _("line number info header doesn't "
15124 "fit in `.debug_line' section"));
debd256d
JB
15125
15126 discard_cleanups (back_to);
15127 return lh;
15128}
c906108c 15129
c6da4cef
DE
15130/* Subroutine of dwarf_decode_lines to simplify it.
15131 Return the file name of the psymtab for included file FILE_INDEX
15132 in line header LH of PST.
15133 COMP_DIR is the compilation directory (DW_AT_comp_dir) or NULL if unknown.
15134 If space for the result is malloc'd, it will be freed by a cleanup.
1ed59174
JK
15135 Returns NULL if FILE_INDEX should be ignored, i.e., it is pst->filename.
15136
15137 The function creates dangling cleanup registration. */
c6da4cef
DE
15138
15139static char *
15140psymtab_include_file_name (const struct line_header *lh, int file_index,
15141 const struct partial_symtab *pst,
15142 const char *comp_dir)
15143{
15144 const struct file_entry fe = lh->file_names [file_index];
15145 char *include_name = fe.name;
15146 char *include_name_to_compare = include_name;
15147 char *dir_name = NULL;
72b9f47f
TT
15148 const char *pst_filename;
15149 char *copied_name = NULL;
c6da4cef
DE
15150 int file_is_pst;
15151
15152 if (fe.dir_index)
15153 dir_name = lh->include_dirs[fe.dir_index - 1];
15154
15155 if (!IS_ABSOLUTE_PATH (include_name)
15156 && (dir_name != NULL || comp_dir != NULL))
15157 {
15158 /* Avoid creating a duplicate psymtab for PST.
15159 We do this by comparing INCLUDE_NAME and PST_FILENAME.
15160 Before we do the comparison, however, we need to account
15161 for DIR_NAME and COMP_DIR.
15162 First prepend dir_name (if non-NULL). If we still don't
15163 have an absolute path prepend comp_dir (if non-NULL).
15164 However, the directory we record in the include-file's
15165 psymtab does not contain COMP_DIR (to match the
15166 corresponding symtab(s)).
15167
15168 Example:
15169
15170 bash$ cd /tmp
15171 bash$ gcc -g ./hello.c
15172 include_name = "hello.c"
15173 dir_name = "."
15174 DW_AT_comp_dir = comp_dir = "/tmp"
15175 DW_AT_name = "./hello.c" */
15176
15177 if (dir_name != NULL)
15178 {
15179 include_name = concat (dir_name, SLASH_STRING,
15180 include_name, (char *)NULL);
15181 include_name_to_compare = include_name;
15182 make_cleanup (xfree, include_name);
15183 }
15184 if (!IS_ABSOLUTE_PATH (include_name) && comp_dir != NULL)
15185 {
15186 include_name_to_compare = concat (comp_dir, SLASH_STRING,
15187 include_name, (char *)NULL);
15188 }
15189 }
15190
15191 pst_filename = pst->filename;
15192 if (!IS_ABSOLUTE_PATH (pst_filename) && pst->dirname != NULL)
15193 {
72b9f47f
TT
15194 copied_name = concat (pst->dirname, SLASH_STRING,
15195 pst_filename, (char *)NULL);
15196 pst_filename = copied_name;
c6da4cef
DE
15197 }
15198
1e3fad37 15199 file_is_pst = FILENAME_CMP (include_name_to_compare, pst_filename) == 0;
c6da4cef
DE
15200
15201 if (include_name_to_compare != include_name)
15202 xfree (include_name_to_compare);
72b9f47f
TT
15203 if (copied_name != NULL)
15204 xfree (copied_name);
c6da4cef
DE
15205
15206 if (file_is_pst)
15207 return NULL;
15208 return include_name;
15209}
15210
c91513d8
PP
15211/* Ignore this record_line request. */
15212
15213static void
15214noop_record_line (struct subfile *subfile, int line, CORE_ADDR pc)
15215{
15216 return;
15217}
15218
f3f5162e
DE
15219/* Subroutine of dwarf_decode_lines to simplify it.
15220 Process the line number information in LH. */
debd256d 15221
c906108c 15222static void
f3f5162e
DE
15223dwarf_decode_lines_1 (struct line_header *lh, const char *comp_dir,
15224 struct dwarf2_cu *cu, struct partial_symtab *pst)
c906108c 15225{
a8c50c1f 15226 gdb_byte *line_ptr, *extended_end;
fe1b8b76 15227 gdb_byte *line_end;
a8c50c1f 15228 unsigned int bytes_read, extended_len;
c906108c 15229 unsigned char op_code, extended_op, adj_opcode;
e142c38c
DJ
15230 CORE_ADDR baseaddr;
15231 struct objfile *objfile = cu->objfile;
f3f5162e 15232 bfd *abfd = objfile->obfd;
fbf65064 15233 struct gdbarch *gdbarch = get_objfile_arch (objfile);
aaa75496 15234 const int decode_for_pst_p = (pst != NULL);
f3f5162e 15235 struct subfile *last_subfile = NULL;
c91513d8
PP
15236 void (*p_record_line) (struct subfile *subfile, int line, CORE_ADDR pc)
15237 = record_line;
e142c38c
DJ
15238
15239 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
c906108c 15240
debd256d
JB
15241 line_ptr = lh->statement_program_start;
15242 line_end = lh->statement_program_end;
c906108c
SS
15243
15244 /* Read the statement sequences until there's nothing left. */
15245 while (line_ptr < line_end)
15246 {
15247 /* state machine registers */
15248 CORE_ADDR address = 0;
15249 unsigned int file = 1;
15250 unsigned int line = 1;
15251 unsigned int column = 0;
debd256d 15252 int is_stmt = lh->default_is_stmt;
c906108c
SS
15253 int basic_block = 0;
15254 int end_sequence = 0;
fbf65064 15255 CORE_ADDR addr;
2dc7f7b3 15256 unsigned char op_index = 0;
c906108c 15257
aaa75496 15258 if (!decode_for_pst_p && lh->num_file_names >= file)
c906108c 15259 {
aaa75496 15260 /* Start a subfile for the current file of the state machine. */
debd256d
JB
15261 /* lh->include_dirs and lh->file_names are 0-based, but the
15262 directory and file name numbers in the statement program
15263 are 1-based. */
15264 struct file_entry *fe = &lh->file_names[file - 1];
4f1520fb 15265 char *dir = NULL;
a738430d 15266
debd256d
JB
15267 if (fe->dir_index)
15268 dir = lh->include_dirs[fe->dir_index - 1];
4f1520fb
FR
15269
15270 dwarf2_start_subfile (fe->name, dir, comp_dir);
c906108c
SS
15271 }
15272
a738430d 15273 /* Decode the table. */
c5aa993b 15274 while (!end_sequence)
c906108c
SS
15275 {
15276 op_code = read_1_byte (abfd, line_ptr);
15277 line_ptr += 1;
59205f5a
JB
15278 if (line_ptr > line_end)
15279 {
15280 dwarf2_debug_line_missing_end_sequence_complaint ();
15281 break;
15282 }
9aa1fe7e 15283
debd256d 15284 if (op_code >= lh->opcode_base)
6e70227d 15285 {
a738430d 15286 /* Special operand. */
debd256d 15287 adj_opcode = op_code - lh->opcode_base;
2dc7f7b3
TT
15288 address += (((op_index + (adj_opcode / lh->line_range))
15289 / lh->maximum_ops_per_instruction)
15290 * lh->minimum_instruction_length);
15291 op_index = ((op_index + (adj_opcode / lh->line_range))
15292 % lh->maximum_ops_per_instruction);
debd256d 15293 line += lh->line_base + (adj_opcode % lh->line_range);
59205f5a 15294 if (lh->num_file_names < file || file == 0)
25e43795 15295 dwarf2_debug_line_missing_file_complaint ();
2dc7f7b3
TT
15296 /* For now we ignore lines not starting on an
15297 instruction boundary. */
15298 else if (op_index == 0)
25e43795
DJ
15299 {
15300 lh->file_names[file - 1].included_p = 1;
ca5f395d 15301 if (!decode_for_pst_p && is_stmt)
fbf65064
UW
15302 {
15303 if (last_subfile != current_subfile)
15304 {
15305 addr = gdbarch_addr_bits_remove (gdbarch, address);
15306 if (last_subfile)
c91513d8 15307 (*p_record_line) (last_subfile, 0, addr);
fbf65064
UW
15308 last_subfile = current_subfile;
15309 }
25e43795 15310 /* Append row to matrix using current values. */
7019d805 15311 addr = gdbarch_addr_bits_remove (gdbarch, address);
c91513d8 15312 (*p_record_line) (current_subfile, line, addr);
366da635 15313 }
25e43795 15314 }
ca5f395d 15315 basic_block = 0;
9aa1fe7e
GK
15316 }
15317 else switch (op_code)
c906108c
SS
15318 {
15319 case DW_LNS_extended_op:
3e43a32a
MS
15320 extended_len = read_unsigned_leb128 (abfd, line_ptr,
15321 &bytes_read);
473b7be6 15322 line_ptr += bytes_read;
a8c50c1f 15323 extended_end = line_ptr + extended_len;
c906108c
SS
15324 extended_op = read_1_byte (abfd, line_ptr);
15325 line_ptr += 1;
15326 switch (extended_op)
15327 {
15328 case DW_LNE_end_sequence:
c91513d8 15329 p_record_line = record_line;
c906108c 15330 end_sequence = 1;
c906108c
SS
15331 break;
15332 case DW_LNE_set_address:
e7c27a73 15333 address = read_address (abfd, line_ptr, cu, &bytes_read);
c91513d8
PP
15334
15335 if (address == 0 && !dwarf2_per_objfile->has_section_at_zero)
15336 {
15337 /* This line table is for a function which has been
15338 GCd by the linker. Ignore it. PR gdb/12528 */
15339
15340 long line_offset
36586728 15341 = line_ptr - get_debug_line_section (cu)->buffer;
c91513d8
PP
15342
15343 complaint (&symfile_complaints,
15344 _(".debug_line address at offset 0x%lx is 0 "
15345 "[in module %s]"),
bb5ed363 15346 line_offset, objfile->name);
c91513d8
PP
15347 p_record_line = noop_record_line;
15348 }
15349
2dc7f7b3 15350 op_index = 0;
107d2387
AC
15351 line_ptr += bytes_read;
15352 address += baseaddr;
c906108c
SS
15353 break;
15354 case DW_LNE_define_file:
debd256d
JB
15355 {
15356 char *cur_file;
15357 unsigned int dir_index, mod_time, length;
6e70227d 15358
3e43a32a
MS
15359 cur_file = read_direct_string (abfd, line_ptr,
15360 &bytes_read);
debd256d
JB
15361 line_ptr += bytes_read;
15362 dir_index =
15363 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
15364 line_ptr += bytes_read;
15365 mod_time =
15366 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
15367 line_ptr += bytes_read;
15368 length =
15369 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
15370 line_ptr += bytes_read;
15371 add_file_name (lh, cur_file, dir_index, mod_time, length);
15372 }
c906108c 15373 break;
d0c6ba3d
CC
15374 case DW_LNE_set_discriminator:
15375 /* The discriminator is not interesting to the debugger;
15376 just ignore it. */
15377 line_ptr = extended_end;
15378 break;
c906108c 15379 default:
4d3c2250 15380 complaint (&symfile_complaints,
e2e0b3e5 15381 _("mangled .debug_line section"));
debd256d 15382 return;
c906108c 15383 }
a8c50c1f
DJ
15384 /* Make sure that we parsed the extended op correctly. If e.g.
15385 we expected a different address size than the producer used,
15386 we may have read the wrong number of bytes. */
15387 if (line_ptr != extended_end)
15388 {
15389 complaint (&symfile_complaints,
15390 _("mangled .debug_line section"));
15391 return;
15392 }
c906108c
SS
15393 break;
15394 case DW_LNS_copy:
59205f5a 15395 if (lh->num_file_names < file || file == 0)
25e43795
DJ
15396 dwarf2_debug_line_missing_file_complaint ();
15397 else
366da635 15398 {
25e43795 15399 lh->file_names[file - 1].included_p = 1;
ca5f395d 15400 if (!decode_for_pst_p && is_stmt)
fbf65064
UW
15401 {
15402 if (last_subfile != current_subfile)
15403 {
15404 addr = gdbarch_addr_bits_remove (gdbarch, address);
15405 if (last_subfile)
c91513d8 15406 (*p_record_line) (last_subfile, 0, addr);
fbf65064
UW
15407 last_subfile = current_subfile;
15408 }
7019d805 15409 addr = gdbarch_addr_bits_remove (gdbarch, address);
c91513d8 15410 (*p_record_line) (current_subfile, line, addr);
fbf65064 15411 }
366da635 15412 }
c906108c
SS
15413 basic_block = 0;
15414 break;
15415 case DW_LNS_advance_pc:
2dc7f7b3
TT
15416 {
15417 CORE_ADDR adjust
15418 = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
15419
15420 address += (((op_index + adjust)
15421 / lh->maximum_ops_per_instruction)
15422 * lh->minimum_instruction_length);
15423 op_index = ((op_index + adjust)
15424 % lh->maximum_ops_per_instruction);
15425 line_ptr += bytes_read;
15426 }
c906108c
SS
15427 break;
15428 case DW_LNS_advance_line:
15429 line += read_signed_leb128 (abfd, line_ptr, &bytes_read);
15430 line_ptr += bytes_read;
15431 break;
15432 case DW_LNS_set_file:
debd256d 15433 {
a738430d
MK
15434 /* The arrays lh->include_dirs and lh->file_names are
15435 0-based, but the directory and file name numbers in
15436 the statement program are 1-based. */
debd256d 15437 struct file_entry *fe;
4f1520fb 15438 char *dir = NULL;
a738430d 15439
debd256d
JB
15440 file = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
15441 line_ptr += bytes_read;
59205f5a 15442 if (lh->num_file_names < file || file == 0)
25e43795
DJ
15443 dwarf2_debug_line_missing_file_complaint ();
15444 else
15445 {
15446 fe = &lh->file_names[file - 1];
15447 if (fe->dir_index)
15448 dir = lh->include_dirs[fe->dir_index - 1];
15449 if (!decode_for_pst_p)
15450 {
15451 last_subfile = current_subfile;
15452 dwarf2_start_subfile (fe->name, dir, comp_dir);
15453 }
15454 }
debd256d 15455 }
c906108c
SS
15456 break;
15457 case DW_LNS_set_column:
15458 column = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
15459 line_ptr += bytes_read;
15460 break;
15461 case DW_LNS_negate_stmt:
15462 is_stmt = (!is_stmt);
15463 break;
15464 case DW_LNS_set_basic_block:
15465 basic_block = 1;
15466 break;
c2c6d25f
JM
15467 /* Add to the address register of the state machine the
15468 address increment value corresponding to special opcode
a738430d
MK
15469 255. I.e., this value is scaled by the minimum
15470 instruction length since special opcode 255 would have
b021a221 15471 scaled the increment. */
c906108c 15472 case DW_LNS_const_add_pc:
2dc7f7b3
TT
15473 {
15474 CORE_ADDR adjust = (255 - lh->opcode_base) / lh->line_range;
15475
15476 address += (((op_index + adjust)
15477 / lh->maximum_ops_per_instruction)
15478 * lh->minimum_instruction_length);
15479 op_index = ((op_index + adjust)
15480 % lh->maximum_ops_per_instruction);
15481 }
c906108c
SS
15482 break;
15483 case DW_LNS_fixed_advance_pc:
15484 address += read_2_bytes (abfd, line_ptr);
2dc7f7b3 15485 op_index = 0;
c906108c
SS
15486 line_ptr += 2;
15487 break;
9aa1fe7e 15488 default:
a738430d
MK
15489 {
15490 /* Unknown standard opcode, ignore it. */
9aa1fe7e 15491 int i;
a738430d 15492
debd256d 15493 for (i = 0; i < lh->standard_opcode_lengths[op_code]; i++)
9aa1fe7e
GK
15494 {
15495 (void) read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
15496 line_ptr += bytes_read;
15497 }
15498 }
c906108c
SS
15499 }
15500 }
59205f5a
JB
15501 if (lh->num_file_names < file || file == 0)
15502 dwarf2_debug_line_missing_file_complaint ();
15503 else
15504 {
15505 lh->file_names[file - 1].included_p = 1;
15506 if (!decode_for_pst_p)
fbf65064
UW
15507 {
15508 addr = gdbarch_addr_bits_remove (gdbarch, address);
c91513d8 15509 (*p_record_line) (current_subfile, 0, addr);
fbf65064 15510 }
59205f5a 15511 }
c906108c 15512 }
f3f5162e
DE
15513}
15514
15515/* Decode the Line Number Program (LNP) for the given line_header
15516 structure and CU. The actual information extracted and the type
15517 of structures created from the LNP depends on the value of PST.
15518
15519 1. If PST is NULL, then this procedure uses the data from the program
15520 to create all necessary symbol tables, and their linetables.
15521
15522 2. If PST is not NULL, this procedure reads the program to determine
15523 the list of files included by the unit represented by PST, and
15524 builds all the associated partial symbol tables.
15525
15526 COMP_DIR is the compilation directory (DW_AT_comp_dir) or NULL if unknown.
15527 It is used for relative paths in the line table.
15528 NOTE: When processing partial symtabs (pst != NULL),
15529 comp_dir == pst->dirname.
15530
15531 NOTE: It is important that psymtabs have the same file name (via strcmp)
15532 as the corresponding symtab. Since COMP_DIR is not used in the name of the
15533 symtab we don't use it in the name of the psymtabs we create.
15534 E.g. expand_line_sal requires this when finding psymtabs to expand.
15535 A good testcase for this is mb-inline.exp. */
15536
15537static void
15538dwarf_decode_lines (struct line_header *lh, const char *comp_dir,
15539 struct dwarf2_cu *cu, struct partial_symtab *pst,
15540 int want_line_info)
15541{
15542 struct objfile *objfile = cu->objfile;
15543 const int decode_for_pst_p = (pst != NULL);
15544 struct subfile *first_subfile = current_subfile;
15545
15546 if (want_line_info)
15547 dwarf_decode_lines_1 (lh, comp_dir, cu, pst);
aaa75496
JB
15548
15549 if (decode_for_pst_p)
15550 {
15551 int file_index;
15552
15553 /* Now that we're done scanning the Line Header Program, we can
15554 create the psymtab of each included file. */
15555 for (file_index = 0; file_index < lh->num_file_names; file_index++)
15556 if (lh->file_names[file_index].included_p == 1)
15557 {
c6da4cef
DE
15558 char *include_name =
15559 psymtab_include_file_name (lh, file_index, pst, comp_dir);
15560 if (include_name != NULL)
aaa75496
JB
15561 dwarf2_create_include_psymtab (include_name, pst, objfile);
15562 }
15563 }
cb1df416
DJ
15564 else
15565 {
15566 /* Make sure a symtab is created for every file, even files
15567 which contain only variables (i.e. no code with associated
15568 line numbers). */
cb1df416 15569 int i;
cb1df416
DJ
15570
15571 for (i = 0; i < lh->num_file_names; i++)
15572 {
15573 char *dir = NULL;
f3f5162e 15574 struct file_entry *fe;
9a619af0 15575
cb1df416
DJ
15576 fe = &lh->file_names[i];
15577 if (fe->dir_index)
15578 dir = lh->include_dirs[fe->dir_index - 1];
15579 dwarf2_start_subfile (fe->name, dir, comp_dir);
15580
15581 /* Skip the main file; we don't need it, and it must be
15582 allocated last, so that it will show up before the
15583 non-primary symtabs in the objfile's symtab list. */
15584 if (current_subfile == first_subfile)
15585 continue;
15586
15587 if (current_subfile->symtab == NULL)
15588 current_subfile->symtab = allocate_symtab (current_subfile->name,
bb5ed363 15589 objfile);
cb1df416
DJ
15590 fe->symtab = current_subfile->symtab;
15591 }
15592 }
c906108c
SS
15593}
15594
15595/* Start a subfile for DWARF. FILENAME is the name of the file and
15596 DIRNAME the name of the source directory which contains FILENAME
4f1520fb
FR
15597 or NULL if not known. COMP_DIR is the compilation directory for the
15598 linetable's compilation unit or NULL if not known.
c906108c
SS
15599 This routine tries to keep line numbers from identical absolute and
15600 relative file names in a common subfile.
15601
15602 Using the `list' example from the GDB testsuite, which resides in
15603 /srcdir and compiling it with Irix6.2 cc in /compdir using a filename
15604 of /srcdir/list0.c yields the following debugging information for list0.c:
15605
c5aa993b
JM
15606 DW_AT_name: /srcdir/list0.c
15607 DW_AT_comp_dir: /compdir
357e46e7 15608 files.files[0].name: list0.h
c5aa993b 15609 files.files[0].dir: /srcdir
357e46e7 15610 files.files[1].name: list0.c
c5aa993b 15611 files.files[1].dir: /srcdir
c906108c
SS
15612
15613 The line number information for list0.c has to end up in a single
4f1520fb
FR
15614 subfile, so that `break /srcdir/list0.c:1' works as expected.
15615 start_subfile will ensure that this happens provided that we pass the
15616 concatenation of files.files[1].dir and files.files[1].name as the
15617 subfile's name. */
c906108c
SS
15618
15619static void
3e43a32a
MS
15620dwarf2_start_subfile (char *filename, const char *dirname,
15621 const char *comp_dir)
c906108c 15622{
4f1520fb
FR
15623 char *fullname;
15624
15625 /* While reading the DIEs, we call start_symtab(DW_AT_name, DW_AT_comp_dir).
15626 `start_symtab' will always pass the contents of DW_AT_comp_dir as
15627 second argument to start_subfile. To be consistent, we do the
15628 same here. In order not to lose the line information directory,
15629 we concatenate it to the filename when it makes sense.
15630 Note that the Dwarf3 standard says (speaking of filenames in line
15631 information): ``The directory index is ignored for file names
15632 that represent full path names''. Thus ignoring dirname in the
15633 `else' branch below isn't an issue. */
c906108c 15634
d5166ae1 15635 if (!IS_ABSOLUTE_PATH (filename) && dirname != NULL)
4f1520fb
FR
15636 fullname = concat (dirname, SLASH_STRING, filename, (char *)NULL);
15637 else
15638 fullname = filename;
c906108c 15639
4f1520fb
FR
15640 start_subfile (fullname, comp_dir);
15641
15642 if (fullname != filename)
15643 xfree (fullname);
c906108c
SS
15644}
15645
f4dc4d17
DE
15646/* Start a symtab for DWARF.
15647 NAME, COMP_DIR, LOW_PC are passed to start_symtab. */
15648
15649static void
15650dwarf2_start_symtab (struct dwarf2_cu *cu,
15d034d0 15651 const char *name, const char *comp_dir, CORE_ADDR low_pc)
f4dc4d17
DE
15652{
15653 start_symtab (name, comp_dir, low_pc);
15654 record_debugformat ("DWARF 2");
15655 record_producer (cu->producer);
15656
15657 /* We assume that we're processing GCC output. */
15658 processing_gcc_compilation = 2;
15659
4d4ec4e5 15660 cu->processing_has_namespace_info = 0;
f4dc4d17
DE
15661}
15662
4c2df51b
DJ
15663static void
15664var_decode_location (struct attribute *attr, struct symbol *sym,
e7c27a73 15665 struct dwarf2_cu *cu)
4c2df51b 15666{
e7c27a73
DJ
15667 struct objfile *objfile = cu->objfile;
15668 struct comp_unit_head *cu_header = &cu->header;
15669
4c2df51b
DJ
15670 /* NOTE drow/2003-01-30: There used to be a comment and some special
15671 code here to turn a symbol with DW_AT_external and a
15672 SYMBOL_VALUE_ADDRESS of 0 into a LOC_UNRESOLVED symbol. This was
15673 necessary for platforms (maybe Alpha, certainly PowerPC GNU/Linux
15674 with some versions of binutils) where shared libraries could have
15675 relocations against symbols in their debug information - the
15676 minimal symbol would have the right address, but the debug info
15677 would not. It's no longer necessary, because we will explicitly
15678 apply relocations when we read in the debug information now. */
15679
15680 /* A DW_AT_location attribute with no contents indicates that a
15681 variable has been optimized away. */
15682 if (attr_form_is_block (attr) && DW_BLOCK (attr)->size == 0)
15683 {
15684 SYMBOL_CLASS (sym) = LOC_OPTIMIZED_OUT;
15685 return;
15686 }
15687
15688 /* Handle one degenerate form of location expression specially, to
15689 preserve GDB's previous behavior when section offsets are
3019eac3
DE
15690 specified. If this is just a DW_OP_addr or DW_OP_GNU_addr_index
15691 then mark this symbol as LOC_STATIC. */
4c2df51b
DJ
15692
15693 if (attr_form_is_block (attr)
3019eac3
DE
15694 && ((DW_BLOCK (attr)->data[0] == DW_OP_addr
15695 && DW_BLOCK (attr)->size == 1 + cu_header->addr_size)
15696 || (DW_BLOCK (attr)->data[0] == DW_OP_GNU_addr_index
15697 && (DW_BLOCK (attr)->size
15698 == 1 + leb128_size (&DW_BLOCK (attr)->data[1])))))
4c2df51b 15699 {
891d2f0b 15700 unsigned int dummy;
4c2df51b 15701
3019eac3
DE
15702 if (DW_BLOCK (attr)->data[0] == DW_OP_addr)
15703 SYMBOL_VALUE_ADDRESS (sym) =
15704 read_address (objfile->obfd, DW_BLOCK (attr)->data + 1, cu, &dummy);
15705 else
15706 SYMBOL_VALUE_ADDRESS (sym) =
15707 read_addr_index_from_leb128 (cu, DW_BLOCK (attr)->data + 1, &dummy);
907fc202 15708 SYMBOL_CLASS (sym) = LOC_STATIC;
4c2df51b
DJ
15709 fixup_symbol_section (sym, objfile);
15710 SYMBOL_VALUE_ADDRESS (sym) += ANOFFSET (objfile->section_offsets,
15711 SYMBOL_SECTION (sym));
4c2df51b
DJ
15712 return;
15713 }
15714
15715 /* NOTE drow/2002-01-30: It might be worthwhile to have a static
15716 expression evaluator, and use LOC_COMPUTED only when necessary
15717 (i.e. when the value of a register or memory location is
15718 referenced, or a thread-local block, etc.). Then again, it might
15719 not be worthwhile. I'm assuming that it isn't unless performance
15720 or memory numbers show me otherwise. */
15721
e7c27a73 15722 dwarf2_symbol_mark_computed (attr, sym, cu);
4c2df51b 15723 SYMBOL_CLASS (sym) = LOC_COMPUTED;
8be455d7
JK
15724
15725 if (SYMBOL_COMPUTED_OPS (sym) == &dwarf2_loclist_funcs)
15726 cu->has_loclist = 1;
4c2df51b
DJ
15727}
15728
c906108c
SS
15729/* Given a pointer to a DWARF information entry, figure out if we need
15730 to make a symbol table entry for it, and if so, create a new entry
15731 and return a pointer to it.
15732 If TYPE is NULL, determine symbol type from the die, otherwise
34eaf542
TT
15733 used the passed type.
15734 If SPACE is not NULL, use it to hold the new symbol. If it is
15735 NULL, allocate a new symbol on the objfile's obstack. */
c906108c
SS
15736
15737static struct symbol *
34eaf542
TT
15738new_symbol_full (struct die_info *die, struct type *type, struct dwarf2_cu *cu,
15739 struct symbol *space)
c906108c 15740{
e7c27a73 15741 struct objfile *objfile = cu->objfile;
c906108c 15742 struct symbol *sym = NULL;
15d034d0 15743 const char *name;
c906108c
SS
15744 struct attribute *attr = NULL;
15745 struct attribute *attr2 = NULL;
e142c38c 15746 CORE_ADDR baseaddr;
e37fd15a
SW
15747 struct pending **list_to_add = NULL;
15748
edb3359d 15749 int inlined_func = (die->tag == DW_TAG_inlined_subroutine);
e142c38c
DJ
15750
15751 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
c906108c 15752
94af9270 15753 name = dwarf2_name (die, cu);
c906108c
SS
15754 if (name)
15755 {
94af9270 15756 const char *linkagename;
34eaf542 15757 int suppress_add = 0;
94af9270 15758
34eaf542
TT
15759 if (space)
15760 sym = space;
15761 else
15762 sym = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct symbol);
c906108c 15763 OBJSTAT (objfile, n_syms++);
2de7ced7
DJ
15764
15765 /* Cache this symbol's name and the name's demangled form (if any). */
33e5013e 15766 SYMBOL_SET_LANGUAGE (sym, cu->language);
94af9270
KS
15767 linkagename = dwarf2_physname (name, die, cu);
15768 SYMBOL_SET_NAMES (sym, linkagename, strlen (linkagename), 0, objfile);
c906108c 15769
f55ee35c
JK
15770 /* Fortran does not have mangling standard and the mangling does differ
15771 between gfortran, iFort etc. */
15772 if (cu->language == language_fortran
b250c185 15773 && symbol_get_demangled_name (&(sym->ginfo)) == NULL)
29df156d 15774 symbol_set_demangled_name (&(sym->ginfo),
cfc594ee 15775 dwarf2_full_name (name, die, cu),
29df156d 15776 NULL);
f55ee35c 15777
c906108c 15778 /* Default assumptions.
c5aa993b 15779 Use the passed type or decode it from the die. */
176620f1 15780 SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
875dc2fc 15781 SYMBOL_CLASS (sym) = LOC_OPTIMIZED_OUT;
c906108c
SS
15782 if (type != NULL)
15783 SYMBOL_TYPE (sym) = type;
15784 else
e7c27a73 15785 SYMBOL_TYPE (sym) = die_type (die, cu);
edb3359d
DJ
15786 attr = dwarf2_attr (die,
15787 inlined_func ? DW_AT_call_line : DW_AT_decl_line,
15788 cu);
c906108c
SS
15789 if (attr)
15790 {
15791 SYMBOL_LINE (sym) = DW_UNSND (attr);
15792 }
cb1df416 15793
edb3359d
DJ
15794 attr = dwarf2_attr (die,
15795 inlined_func ? DW_AT_call_file : DW_AT_decl_file,
15796 cu);
cb1df416
DJ
15797 if (attr)
15798 {
15799 int file_index = DW_UNSND (attr);
9a619af0 15800
cb1df416
DJ
15801 if (cu->line_header == NULL
15802 || file_index > cu->line_header->num_file_names)
15803 complaint (&symfile_complaints,
15804 _("file index out of range"));
1c3d648d 15805 else if (file_index > 0)
cb1df416
DJ
15806 {
15807 struct file_entry *fe;
9a619af0 15808
cb1df416
DJ
15809 fe = &cu->line_header->file_names[file_index - 1];
15810 SYMBOL_SYMTAB (sym) = fe->symtab;
15811 }
15812 }
15813
c906108c
SS
15814 switch (die->tag)
15815 {
15816 case DW_TAG_label:
e142c38c 15817 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
c906108c
SS
15818 if (attr)
15819 {
15820 SYMBOL_VALUE_ADDRESS (sym) = DW_ADDR (attr) + baseaddr;
15821 }
0f5238ed
TT
15822 SYMBOL_TYPE (sym) = objfile_type (objfile)->builtin_core_addr;
15823 SYMBOL_DOMAIN (sym) = LABEL_DOMAIN;
c906108c 15824 SYMBOL_CLASS (sym) = LOC_LABEL;
0f5238ed 15825 add_symbol_to_list (sym, cu->list_in_scope);
c906108c
SS
15826 break;
15827 case DW_TAG_subprogram:
15828 /* SYMBOL_BLOCK_VALUE (sym) will be filled in later by
15829 finish_block. */
15830 SYMBOL_CLASS (sym) = LOC_BLOCK;
e142c38c 15831 attr2 = dwarf2_attr (die, DW_AT_external, cu);
2cfa0c8d
JB
15832 if ((attr2 && (DW_UNSND (attr2) != 0))
15833 || cu->language == language_ada)
c906108c 15834 {
2cfa0c8d
JB
15835 /* Subprograms marked external are stored as a global symbol.
15836 Ada subprograms, whether marked external or not, are always
15837 stored as a global symbol, because we want to be able to
15838 access them globally. For instance, we want to be able
15839 to break on a nested subprogram without having to
15840 specify the context. */
e37fd15a 15841 list_to_add = &global_symbols;
c906108c
SS
15842 }
15843 else
15844 {
e37fd15a 15845 list_to_add = cu->list_in_scope;
c906108c
SS
15846 }
15847 break;
edb3359d
DJ
15848 case DW_TAG_inlined_subroutine:
15849 /* SYMBOL_BLOCK_VALUE (sym) will be filled in later by
15850 finish_block. */
15851 SYMBOL_CLASS (sym) = LOC_BLOCK;
15852 SYMBOL_INLINED (sym) = 1;
481860b3 15853 list_to_add = cu->list_in_scope;
edb3359d 15854 break;
34eaf542
TT
15855 case DW_TAG_template_value_param:
15856 suppress_add = 1;
15857 /* Fall through. */
72929c62 15858 case DW_TAG_constant:
c906108c 15859 case DW_TAG_variable:
254e6b9e 15860 case DW_TAG_member:
0963b4bd
MS
15861 /* Compilation with minimal debug info may result in
15862 variables with missing type entries. Change the
15863 misleading `void' type to something sensible. */
c906108c 15864 if (TYPE_CODE (SYMBOL_TYPE (sym)) == TYPE_CODE_VOID)
64c50499 15865 SYMBOL_TYPE (sym)
46bf5051 15866 = objfile_type (objfile)->nodebug_data_symbol;
64c50499 15867
e142c38c 15868 attr = dwarf2_attr (die, DW_AT_const_value, cu);
254e6b9e
DE
15869 /* In the case of DW_TAG_member, we should only be called for
15870 static const members. */
15871 if (die->tag == DW_TAG_member)
15872 {
3863f96c
DE
15873 /* dwarf2_add_field uses die_is_declaration,
15874 so we do the same. */
254e6b9e
DE
15875 gdb_assert (die_is_declaration (die, cu));
15876 gdb_assert (attr);
15877 }
c906108c
SS
15878 if (attr)
15879 {
e7c27a73 15880 dwarf2_const_value (attr, sym, cu);
e142c38c 15881 attr2 = dwarf2_attr (die, DW_AT_external, cu);
e37fd15a 15882 if (!suppress_add)
34eaf542
TT
15883 {
15884 if (attr2 && (DW_UNSND (attr2) != 0))
e37fd15a 15885 list_to_add = &global_symbols;
34eaf542 15886 else
e37fd15a 15887 list_to_add = cu->list_in_scope;
34eaf542 15888 }
c906108c
SS
15889 break;
15890 }
e142c38c 15891 attr = dwarf2_attr (die, DW_AT_location, cu);
c906108c
SS
15892 if (attr)
15893 {
e7c27a73 15894 var_decode_location (attr, sym, cu);
e142c38c 15895 attr2 = dwarf2_attr (die, DW_AT_external, cu);
4357ac6c
TT
15896
15897 /* Fortran explicitly imports any global symbols to the local
15898 scope by DW_TAG_common_block. */
15899 if (cu->language == language_fortran && die->parent
15900 && die->parent->tag == DW_TAG_common_block)
15901 attr2 = NULL;
15902
caac4577
JG
15903 if (SYMBOL_CLASS (sym) == LOC_STATIC
15904 && SYMBOL_VALUE_ADDRESS (sym) == 0
15905 && !dwarf2_per_objfile->has_section_at_zero)
15906 {
15907 /* When a static variable is eliminated by the linker,
15908 the corresponding debug information is not stripped
15909 out, but the variable address is set to null;
15910 do not add such variables into symbol table. */
15911 }
15912 else if (attr2 && (DW_UNSND (attr2) != 0))
1c809c68 15913 {
f55ee35c
JK
15914 /* Workaround gfortran PR debug/40040 - it uses
15915 DW_AT_location for variables in -fPIC libraries which may
15916 get overriden by other libraries/executable and get
15917 a different address. Resolve it by the minimal symbol
15918 which may come from inferior's executable using copy
15919 relocation. Make this workaround only for gfortran as for
15920 other compilers GDB cannot guess the minimal symbol
15921 Fortran mangling kind. */
15922 if (cu->language == language_fortran && die->parent
15923 && die->parent->tag == DW_TAG_module
15924 && cu->producer
15925 && strncmp (cu->producer, "GNU Fortran ", 12) == 0)
15926 SYMBOL_CLASS (sym) = LOC_UNRESOLVED;
15927
1c809c68
TT
15928 /* A variable with DW_AT_external is never static,
15929 but it may be block-scoped. */
15930 list_to_add = (cu->list_in_scope == &file_symbols
15931 ? &global_symbols : cu->list_in_scope);
1c809c68 15932 }
c906108c 15933 else
e37fd15a 15934 list_to_add = cu->list_in_scope;
c906108c
SS
15935 }
15936 else
15937 {
15938 /* We do not know the address of this symbol.
c5aa993b
JM
15939 If it is an external symbol and we have type information
15940 for it, enter the symbol as a LOC_UNRESOLVED symbol.
15941 The address of the variable will then be determined from
15942 the minimal symbol table whenever the variable is
15943 referenced. */
e142c38c 15944 attr2 = dwarf2_attr (die, DW_AT_external, cu);
0971de02
TT
15945
15946 /* Fortran explicitly imports any global symbols to the local
15947 scope by DW_TAG_common_block. */
15948 if (cu->language == language_fortran && die->parent
15949 && die->parent->tag == DW_TAG_common_block)
15950 {
15951 /* SYMBOL_CLASS doesn't matter here because
15952 read_common_block is going to reset it. */
15953 if (!suppress_add)
15954 list_to_add = cu->list_in_scope;
15955 }
15956 else if (attr2 && (DW_UNSND (attr2) != 0)
15957 && dwarf2_attr (die, DW_AT_type, cu) != NULL)
c906108c 15958 {
0fe7935b
DJ
15959 /* A variable with DW_AT_external is never static, but it
15960 may be block-scoped. */
15961 list_to_add = (cu->list_in_scope == &file_symbols
15962 ? &global_symbols : cu->list_in_scope);
15963
c906108c 15964 SYMBOL_CLASS (sym) = LOC_UNRESOLVED;
c906108c 15965 }
442ddf59
JK
15966 else if (!die_is_declaration (die, cu))
15967 {
15968 /* Use the default LOC_OPTIMIZED_OUT class. */
15969 gdb_assert (SYMBOL_CLASS (sym) == LOC_OPTIMIZED_OUT);
e37fd15a
SW
15970 if (!suppress_add)
15971 list_to_add = cu->list_in_scope;
442ddf59 15972 }
c906108c
SS
15973 }
15974 break;
15975 case DW_TAG_formal_parameter:
edb3359d
DJ
15976 /* If we are inside a function, mark this as an argument. If
15977 not, we might be looking at an argument to an inlined function
15978 when we do not have enough information to show inlined frames;
15979 pretend it's a local variable in that case so that the user can
15980 still see it. */
15981 if (context_stack_depth > 0
15982 && context_stack[context_stack_depth - 1].name != NULL)
15983 SYMBOL_IS_ARGUMENT (sym) = 1;
e142c38c 15984 attr = dwarf2_attr (die, DW_AT_location, cu);
c906108c
SS
15985 if (attr)
15986 {
e7c27a73 15987 var_decode_location (attr, sym, cu);
c906108c 15988 }
e142c38c 15989 attr = dwarf2_attr (die, DW_AT_const_value, cu);
c906108c
SS
15990 if (attr)
15991 {
e7c27a73 15992 dwarf2_const_value (attr, sym, cu);
c906108c 15993 }
f346a30d 15994
e37fd15a 15995 list_to_add = cu->list_in_scope;
c906108c
SS
15996 break;
15997 case DW_TAG_unspecified_parameters:
15998 /* From varargs functions; gdb doesn't seem to have any
15999 interest in this information, so just ignore it for now.
16000 (FIXME?) */
16001 break;
34eaf542
TT
16002 case DW_TAG_template_type_param:
16003 suppress_add = 1;
16004 /* Fall through. */
c906108c 16005 case DW_TAG_class_type:
680b30c7 16006 case DW_TAG_interface_type:
c906108c
SS
16007 case DW_TAG_structure_type:
16008 case DW_TAG_union_type:
72019c9c 16009 case DW_TAG_set_type:
c906108c
SS
16010 case DW_TAG_enumeration_type:
16011 SYMBOL_CLASS (sym) = LOC_TYPEDEF;
176620f1 16012 SYMBOL_DOMAIN (sym) = STRUCT_DOMAIN;
c906108c 16013
63d06c5c 16014 {
987504bb 16015 /* NOTE: carlton/2003-11-10: C++ and Java class symbols shouldn't
63d06c5c
DC
16016 really ever be static objects: otherwise, if you try
16017 to, say, break of a class's method and you're in a file
16018 which doesn't mention that class, it won't work unless
16019 the check for all static symbols in lookup_symbol_aux
16020 saves you. See the OtherFileClass tests in
16021 gdb.c++/namespace.exp. */
16022
e37fd15a 16023 if (!suppress_add)
34eaf542 16024 {
34eaf542
TT
16025 list_to_add = (cu->list_in_scope == &file_symbols
16026 && (cu->language == language_cplus
16027 || cu->language == language_java)
16028 ? &global_symbols : cu->list_in_scope);
63d06c5c 16029
64382290
TT
16030 /* The semantics of C++ state that "struct foo {
16031 ... }" also defines a typedef for "foo". A Java
16032 class declaration also defines a typedef for the
16033 class. */
16034 if (cu->language == language_cplus
16035 || cu->language == language_java
16036 || cu->language == language_ada)
16037 {
16038 /* The symbol's name is already allocated along
16039 with this objfile, so we don't need to
16040 duplicate it for the type. */
16041 if (TYPE_NAME (SYMBOL_TYPE (sym)) == 0)
16042 TYPE_NAME (SYMBOL_TYPE (sym)) = SYMBOL_SEARCH_NAME (sym);
16043 }
63d06c5c
DC
16044 }
16045 }
c906108c
SS
16046 break;
16047 case DW_TAG_typedef:
63d06c5c
DC
16048 SYMBOL_CLASS (sym) = LOC_TYPEDEF;
16049 SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
e37fd15a 16050 list_to_add = cu->list_in_scope;
63d06c5c 16051 break;
c906108c 16052 case DW_TAG_base_type:
a02abb62 16053 case DW_TAG_subrange_type:
c906108c 16054 SYMBOL_CLASS (sym) = LOC_TYPEDEF;
176620f1 16055 SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
e37fd15a 16056 list_to_add = cu->list_in_scope;
c906108c
SS
16057 break;
16058 case DW_TAG_enumerator:
e142c38c 16059 attr = dwarf2_attr (die, DW_AT_const_value, cu);
c906108c
SS
16060 if (attr)
16061 {
e7c27a73 16062 dwarf2_const_value (attr, sym, cu);
c906108c 16063 }
63d06c5c
DC
16064 {
16065 /* NOTE: carlton/2003-11-10: See comment above in the
16066 DW_TAG_class_type, etc. block. */
16067
e142c38c 16068 list_to_add = (cu->list_in_scope == &file_symbols
987504bb
JJ
16069 && (cu->language == language_cplus
16070 || cu->language == language_java)
e142c38c 16071 ? &global_symbols : cu->list_in_scope);
63d06c5c 16072 }
c906108c 16073 break;
5c4e30ca
DC
16074 case DW_TAG_namespace:
16075 SYMBOL_CLASS (sym) = LOC_TYPEDEF;
e37fd15a 16076 list_to_add = &global_symbols;
5c4e30ca 16077 break;
4357ac6c 16078 case DW_TAG_common_block:
5a352474 16079 SYMBOL_CLASS (sym) = LOC_COMMON_BLOCK;
4357ac6c
TT
16080 SYMBOL_DOMAIN (sym) = COMMON_BLOCK_DOMAIN;
16081 add_symbol_to_list (sym, cu->list_in_scope);
16082 break;
c906108c
SS
16083 default:
16084 /* Not a tag we recognize. Hopefully we aren't processing
16085 trash data, but since we must specifically ignore things
16086 we don't recognize, there is nothing else we should do at
0963b4bd 16087 this point. */
e2e0b3e5 16088 complaint (&symfile_complaints, _("unsupported tag: '%s'"),
4d3c2250 16089 dwarf_tag_name (die->tag));
c906108c
SS
16090 break;
16091 }
df8a16a1 16092
e37fd15a
SW
16093 if (suppress_add)
16094 {
16095 sym->hash_next = objfile->template_symbols;
16096 objfile->template_symbols = sym;
16097 list_to_add = NULL;
16098 }
16099
16100 if (list_to_add != NULL)
16101 add_symbol_to_list (sym, list_to_add);
16102
df8a16a1
DJ
16103 /* For the benefit of old versions of GCC, check for anonymous
16104 namespaces based on the demangled name. */
4d4ec4e5 16105 if (!cu->processing_has_namespace_info
94af9270 16106 && cu->language == language_cplus)
a10964d1 16107 cp_scan_for_anonymous_namespaces (sym, objfile);
c906108c
SS
16108 }
16109 return (sym);
16110}
16111
34eaf542
TT
16112/* A wrapper for new_symbol_full that always allocates a new symbol. */
16113
16114static struct symbol *
16115new_symbol (struct die_info *die, struct type *type, struct dwarf2_cu *cu)
16116{
16117 return new_symbol_full (die, type, cu, NULL);
16118}
16119
98bfdba5
PA
16120/* Given an attr with a DW_FORM_dataN value in host byte order,
16121 zero-extend it as appropriate for the symbol's type. The DWARF
16122 standard (v4) is not entirely clear about the meaning of using
16123 DW_FORM_dataN for a constant with a signed type, where the type is
16124 wider than the data. The conclusion of a discussion on the DWARF
16125 list was that this is unspecified. We choose to always zero-extend
16126 because that is the interpretation long in use by GCC. */
c906108c 16127
98bfdba5
PA
16128static gdb_byte *
16129dwarf2_const_value_data (struct attribute *attr, struct type *type,
16130 const char *name, struct obstack *obstack,
12df843f 16131 struct dwarf2_cu *cu, LONGEST *value, int bits)
c906108c 16132{
e7c27a73 16133 struct objfile *objfile = cu->objfile;
e17a4113
UW
16134 enum bfd_endian byte_order = bfd_big_endian (objfile->obfd) ?
16135 BFD_ENDIAN_BIG : BFD_ENDIAN_LITTLE;
98bfdba5
PA
16136 LONGEST l = DW_UNSND (attr);
16137
16138 if (bits < sizeof (*value) * 8)
16139 {
16140 l &= ((LONGEST) 1 << bits) - 1;
16141 *value = l;
16142 }
16143 else if (bits == sizeof (*value) * 8)
16144 *value = l;
16145 else
16146 {
16147 gdb_byte *bytes = obstack_alloc (obstack, bits / 8);
16148 store_unsigned_integer (bytes, bits / 8, byte_order, l);
16149 return bytes;
16150 }
16151
16152 return NULL;
16153}
16154
16155/* Read a constant value from an attribute. Either set *VALUE, or if
16156 the value does not fit in *VALUE, set *BYTES - either already
16157 allocated on the objfile obstack, or newly allocated on OBSTACK,
16158 or, set *BATON, if we translated the constant to a location
16159 expression. */
16160
16161static void
16162dwarf2_const_value_attr (struct attribute *attr, struct type *type,
16163 const char *name, struct obstack *obstack,
16164 struct dwarf2_cu *cu,
12df843f 16165 LONGEST *value, gdb_byte **bytes,
98bfdba5
PA
16166 struct dwarf2_locexpr_baton **baton)
16167{
16168 struct objfile *objfile = cu->objfile;
16169 struct comp_unit_head *cu_header = &cu->header;
c906108c 16170 struct dwarf_block *blk;
98bfdba5
PA
16171 enum bfd_endian byte_order = (bfd_big_endian (objfile->obfd) ?
16172 BFD_ENDIAN_BIG : BFD_ENDIAN_LITTLE);
16173
16174 *value = 0;
16175 *bytes = NULL;
16176 *baton = NULL;
c906108c
SS
16177
16178 switch (attr->form)
16179 {
16180 case DW_FORM_addr:
3019eac3 16181 case DW_FORM_GNU_addr_index:
ac56253d 16182 {
ac56253d
TT
16183 gdb_byte *data;
16184
98bfdba5
PA
16185 if (TYPE_LENGTH (type) != cu_header->addr_size)
16186 dwarf2_const_value_length_mismatch_complaint (name,
ac56253d 16187 cu_header->addr_size,
98bfdba5 16188 TYPE_LENGTH (type));
ac56253d
TT
16189 /* Symbols of this form are reasonably rare, so we just
16190 piggyback on the existing location code rather than writing
16191 a new implementation of symbol_computed_ops. */
98bfdba5
PA
16192 *baton = obstack_alloc (&objfile->objfile_obstack,
16193 sizeof (struct dwarf2_locexpr_baton));
16194 (*baton)->per_cu = cu->per_cu;
16195 gdb_assert ((*baton)->per_cu);
ac56253d 16196
98bfdba5
PA
16197 (*baton)->size = 2 + cu_header->addr_size;
16198 data = obstack_alloc (&objfile->objfile_obstack, (*baton)->size);
16199 (*baton)->data = data;
ac56253d
TT
16200
16201 data[0] = DW_OP_addr;
16202 store_unsigned_integer (&data[1], cu_header->addr_size,
16203 byte_order, DW_ADDR (attr));
16204 data[cu_header->addr_size + 1] = DW_OP_stack_value;
ac56253d 16205 }
c906108c 16206 break;
4ac36638 16207 case DW_FORM_string:
93b5768b 16208 case DW_FORM_strp:
3019eac3 16209 case DW_FORM_GNU_str_index:
36586728 16210 case DW_FORM_GNU_strp_alt:
98bfdba5
PA
16211 /* DW_STRING is already allocated on the objfile obstack, point
16212 directly to it. */
16213 *bytes = (gdb_byte *) DW_STRING (attr);
93b5768b 16214 break;
c906108c
SS
16215 case DW_FORM_block1:
16216 case DW_FORM_block2:
16217 case DW_FORM_block4:
16218 case DW_FORM_block:
2dc7f7b3 16219 case DW_FORM_exprloc:
c906108c 16220 blk = DW_BLOCK (attr);
98bfdba5
PA
16221 if (TYPE_LENGTH (type) != blk->size)
16222 dwarf2_const_value_length_mismatch_complaint (name, blk->size,
16223 TYPE_LENGTH (type));
16224 *bytes = blk->data;
c906108c 16225 break;
2df3850c
JM
16226
16227 /* The DW_AT_const_value attributes are supposed to carry the
16228 symbol's value "represented as it would be on the target
16229 architecture." By the time we get here, it's already been
16230 converted to host endianness, so we just need to sign- or
16231 zero-extend it as appropriate. */
16232 case DW_FORM_data1:
3e43a32a
MS
16233 *bytes = dwarf2_const_value_data (attr, type, name,
16234 obstack, cu, value, 8);
2df3850c 16235 break;
c906108c 16236 case DW_FORM_data2:
3e43a32a
MS
16237 *bytes = dwarf2_const_value_data (attr, type, name,
16238 obstack, cu, value, 16);
2df3850c 16239 break;
c906108c 16240 case DW_FORM_data4:
3e43a32a
MS
16241 *bytes = dwarf2_const_value_data (attr, type, name,
16242 obstack, cu, value, 32);
2df3850c 16243 break;
c906108c 16244 case DW_FORM_data8:
3e43a32a
MS
16245 *bytes = dwarf2_const_value_data (attr, type, name,
16246 obstack, cu, value, 64);
2df3850c
JM
16247 break;
16248
c906108c 16249 case DW_FORM_sdata:
98bfdba5 16250 *value = DW_SND (attr);
2df3850c
JM
16251 break;
16252
c906108c 16253 case DW_FORM_udata:
98bfdba5 16254 *value = DW_UNSND (attr);
c906108c 16255 break;
2df3850c 16256
c906108c 16257 default:
4d3c2250 16258 complaint (&symfile_complaints,
e2e0b3e5 16259 _("unsupported const value attribute form: '%s'"),
4d3c2250 16260 dwarf_form_name (attr->form));
98bfdba5 16261 *value = 0;
c906108c
SS
16262 break;
16263 }
16264}
16265
2df3850c 16266
98bfdba5
PA
16267/* Copy constant value from an attribute to a symbol. */
16268
2df3850c 16269static void
98bfdba5
PA
16270dwarf2_const_value (struct attribute *attr, struct symbol *sym,
16271 struct dwarf2_cu *cu)
2df3850c 16272{
98bfdba5
PA
16273 struct objfile *objfile = cu->objfile;
16274 struct comp_unit_head *cu_header = &cu->header;
12df843f 16275 LONGEST value;
98bfdba5
PA
16276 gdb_byte *bytes;
16277 struct dwarf2_locexpr_baton *baton;
2df3850c 16278
98bfdba5
PA
16279 dwarf2_const_value_attr (attr, SYMBOL_TYPE (sym),
16280 SYMBOL_PRINT_NAME (sym),
16281 &objfile->objfile_obstack, cu,
16282 &value, &bytes, &baton);
2df3850c 16283
98bfdba5
PA
16284 if (baton != NULL)
16285 {
16286 SYMBOL_COMPUTED_OPS (sym) = &dwarf2_locexpr_funcs;
16287 SYMBOL_LOCATION_BATON (sym) = baton;
16288 SYMBOL_CLASS (sym) = LOC_COMPUTED;
16289 }
16290 else if (bytes != NULL)
16291 {
16292 SYMBOL_VALUE_BYTES (sym) = bytes;
16293 SYMBOL_CLASS (sym) = LOC_CONST_BYTES;
16294 }
16295 else
16296 {
16297 SYMBOL_VALUE (sym) = value;
16298 SYMBOL_CLASS (sym) = LOC_CONST;
16299 }
2df3850c
JM
16300}
16301
c906108c
SS
16302/* Return the type of the die in question using its DW_AT_type attribute. */
16303
16304static struct type *
e7c27a73 16305die_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 16306{
c906108c 16307 struct attribute *type_attr;
c906108c 16308
e142c38c 16309 type_attr = dwarf2_attr (die, DW_AT_type, cu);
c906108c
SS
16310 if (!type_attr)
16311 {
16312 /* A missing DW_AT_type represents a void type. */
46bf5051 16313 return objfile_type (cu->objfile)->builtin_void;
c906108c 16314 }
348e048f 16315
673bfd45 16316 return lookup_die_type (die, type_attr, cu);
c906108c
SS
16317}
16318
b4ba55a1
JB
16319/* True iff CU's producer generates GNAT Ada auxiliary information
16320 that allows to find parallel types through that information instead
16321 of having to do expensive parallel lookups by type name. */
16322
16323static int
16324need_gnat_info (struct dwarf2_cu *cu)
16325{
16326 /* FIXME: brobecker/2010-10-12: As of now, only the AdaCore version
16327 of GNAT produces this auxiliary information, without any indication
16328 that it is produced. Part of enhancing the FSF version of GNAT
16329 to produce that information will be to put in place an indicator
16330 that we can use in order to determine whether the descriptive type
16331 info is available or not. One suggestion that has been made is
16332 to use a new attribute, attached to the CU die. For now, assume
16333 that the descriptive type info is not available. */
16334 return 0;
16335}
16336
b4ba55a1
JB
16337/* Return the auxiliary type of the die in question using its
16338 DW_AT_GNAT_descriptive_type attribute. Returns NULL if the
16339 attribute is not present. */
16340
16341static struct type *
16342die_descriptive_type (struct die_info *die, struct dwarf2_cu *cu)
16343{
b4ba55a1 16344 struct attribute *type_attr;
b4ba55a1
JB
16345
16346 type_attr = dwarf2_attr (die, DW_AT_GNAT_descriptive_type, cu);
16347 if (!type_attr)
16348 return NULL;
16349
673bfd45 16350 return lookup_die_type (die, type_attr, cu);
b4ba55a1
JB
16351}
16352
16353/* If DIE has a descriptive_type attribute, then set the TYPE's
16354 descriptive type accordingly. */
16355
16356static void
16357set_descriptive_type (struct type *type, struct die_info *die,
16358 struct dwarf2_cu *cu)
16359{
16360 struct type *descriptive_type = die_descriptive_type (die, cu);
16361
16362 if (descriptive_type)
16363 {
16364 ALLOCATE_GNAT_AUX_TYPE (type);
16365 TYPE_DESCRIPTIVE_TYPE (type) = descriptive_type;
16366 }
16367}
16368
c906108c
SS
16369/* Return the containing type of the die in question using its
16370 DW_AT_containing_type attribute. */
16371
16372static struct type *
e7c27a73 16373die_containing_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 16374{
c906108c 16375 struct attribute *type_attr;
c906108c 16376
e142c38c 16377 type_attr = dwarf2_attr (die, DW_AT_containing_type, cu);
33ac96f0
JK
16378 if (!type_attr)
16379 error (_("Dwarf Error: Problem turning containing type into gdb type "
16380 "[in module %s]"), cu->objfile->name);
16381
673bfd45 16382 return lookup_die_type (die, type_attr, cu);
c906108c
SS
16383}
16384
673bfd45
DE
16385/* Look up the type of DIE in CU using its type attribute ATTR.
16386 If there is no type substitute an error marker. */
16387
c906108c 16388static struct type *
673bfd45
DE
16389lookup_die_type (struct die_info *die, struct attribute *attr,
16390 struct dwarf2_cu *cu)
c906108c 16391{
bb5ed363 16392 struct objfile *objfile = cu->objfile;
f792889a
DJ
16393 struct type *this_type;
16394
673bfd45
DE
16395 /* First see if we have it cached. */
16396
36586728
TT
16397 if (attr->form == DW_FORM_GNU_ref_alt)
16398 {
16399 struct dwarf2_per_cu_data *per_cu;
16400 sect_offset offset = dwarf2_get_ref_die_offset (attr);
16401
16402 per_cu = dwarf2_find_containing_comp_unit (offset, 1, cu->objfile);
16403 this_type = get_die_type_at_offset (offset, per_cu);
16404 }
16405 else if (is_ref_attr (attr))
673bfd45 16406 {
b64f50a1 16407 sect_offset offset = dwarf2_get_ref_die_offset (attr);
673bfd45
DE
16408
16409 this_type = get_die_type_at_offset (offset, cu->per_cu);
16410 }
55f1336d 16411 else if (attr->form == DW_FORM_ref_sig8)
673bfd45
DE
16412 {
16413 struct signatured_type *sig_type = DW_SIGNATURED_TYPE (attr);
673bfd45
DE
16414
16415 /* sig_type will be NULL if the signatured type is missing from
16416 the debug info. */
16417 if (sig_type == NULL)
16418 error (_("Dwarf Error: Cannot find signatured DIE referenced from DIE "
16419 "at 0x%x [in module %s]"),
b64f50a1 16420 die->offset.sect_off, objfile->name);
673bfd45 16421
3019eac3
DE
16422 gdb_assert (sig_type->per_cu.is_debug_types);
16423 /* If we haven't filled in type_offset_in_section yet, then we
16424 haven't read the type in yet. */
16425 this_type = NULL;
16426 if (sig_type->type_offset_in_section.sect_off != 0)
16427 {
16428 this_type =
16429 get_die_type_at_offset (sig_type->type_offset_in_section,
16430 &sig_type->per_cu);
16431 }
673bfd45
DE
16432 }
16433 else
16434 {
16435 dump_die_for_error (die);
16436 error (_("Dwarf Error: Bad type attribute %s [in module %s]"),
bb5ed363 16437 dwarf_attr_name (attr->name), objfile->name);
673bfd45
DE
16438 }
16439
16440 /* If not cached we need to read it in. */
16441
16442 if (this_type == NULL)
16443 {
16444 struct die_info *type_die;
16445 struct dwarf2_cu *type_cu = cu;
16446
16447 type_die = follow_die_ref_or_sig (die, attr, &type_cu);
3019eac3
DE
16448 /* If we found the type now, it's probably because the type came
16449 from an inter-CU reference and the type's CU got expanded before
16450 ours. */
16451 this_type = get_die_type (type_die, type_cu);
16452 if (this_type == NULL)
16453 this_type = read_type_die_1 (type_die, type_cu);
673bfd45
DE
16454 }
16455
16456 /* If we still don't have a type use an error marker. */
16457
16458 if (this_type == NULL)
c906108c 16459 {
b00fdb78
TT
16460 char *message, *saved;
16461
16462 /* read_type_die already issued a complaint. */
16463 message = xstrprintf (_("<unknown type in %s, CU 0x%x, DIE 0x%x>"),
bb5ed363 16464 objfile->name,
b64f50a1
JK
16465 cu->header.offset.sect_off,
16466 die->offset.sect_off);
bb5ed363 16467 saved = obstack_copy0 (&objfile->objfile_obstack,
b00fdb78
TT
16468 message, strlen (message));
16469 xfree (message);
16470
bb5ed363 16471 this_type = init_type (TYPE_CODE_ERROR, 0, 0, saved, objfile);
c906108c 16472 }
673bfd45 16473
f792889a 16474 return this_type;
c906108c
SS
16475}
16476
673bfd45
DE
16477/* Return the type in DIE, CU.
16478 Returns NULL for invalid types.
16479
16480 This first does a lookup in the appropriate type_hash table,
16481 and only reads the die in if necessary.
16482
16483 NOTE: This can be called when reading in partial or full symbols. */
16484
f792889a 16485static struct type *
e7c27a73 16486read_type_die (struct die_info *die, struct dwarf2_cu *cu)
c906108c 16487{
f792889a
DJ
16488 struct type *this_type;
16489
16490 this_type = get_die_type (die, cu);
16491 if (this_type)
16492 return this_type;
16493
673bfd45
DE
16494 return read_type_die_1 (die, cu);
16495}
16496
16497/* Read the type in DIE, CU.
16498 Returns NULL for invalid types. */
16499
16500static struct type *
16501read_type_die_1 (struct die_info *die, struct dwarf2_cu *cu)
16502{
16503 struct type *this_type = NULL;
16504
c906108c
SS
16505 switch (die->tag)
16506 {
16507 case DW_TAG_class_type:
680b30c7 16508 case DW_TAG_interface_type:
c906108c
SS
16509 case DW_TAG_structure_type:
16510 case DW_TAG_union_type:
f792889a 16511 this_type = read_structure_type (die, cu);
c906108c
SS
16512 break;
16513 case DW_TAG_enumeration_type:
f792889a 16514 this_type = read_enumeration_type (die, cu);
c906108c
SS
16515 break;
16516 case DW_TAG_subprogram:
16517 case DW_TAG_subroutine_type:
edb3359d 16518 case DW_TAG_inlined_subroutine:
f792889a 16519 this_type = read_subroutine_type (die, cu);
c906108c
SS
16520 break;
16521 case DW_TAG_array_type:
f792889a 16522 this_type = read_array_type (die, cu);
c906108c 16523 break;
72019c9c 16524 case DW_TAG_set_type:
f792889a 16525 this_type = read_set_type (die, cu);
72019c9c 16526 break;
c906108c 16527 case DW_TAG_pointer_type:
f792889a 16528 this_type = read_tag_pointer_type (die, cu);
c906108c
SS
16529 break;
16530 case DW_TAG_ptr_to_member_type:
f792889a 16531 this_type = read_tag_ptr_to_member_type (die, cu);
c906108c
SS
16532 break;
16533 case DW_TAG_reference_type:
f792889a 16534 this_type = read_tag_reference_type (die, cu);
c906108c
SS
16535 break;
16536 case DW_TAG_const_type:
f792889a 16537 this_type = read_tag_const_type (die, cu);
c906108c
SS
16538 break;
16539 case DW_TAG_volatile_type:
f792889a 16540 this_type = read_tag_volatile_type (die, cu);
c906108c 16541 break;
06d66ee9
TT
16542 case DW_TAG_restrict_type:
16543 this_type = read_tag_restrict_type (die, cu);
16544 break;
c906108c 16545 case DW_TAG_string_type:
f792889a 16546 this_type = read_tag_string_type (die, cu);
c906108c
SS
16547 break;
16548 case DW_TAG_typedef:
f792889a 16549 this_type = read_typedef (die, cu);
c906108c 16550 break;
a02abb62 16551 case DW_TAG_subrange_type:
f792889a 16552 this_type = read_subrange_type (die, cu);
a02abb62 16553 break;
c906108c 16554 case DW_TAG_base_type:
f792889a 16555 this_type = read_base_type (die, cu);
c906108c 16556 break;
81a17f79 16557 case DW_TAG_unspecified_type:
f792889a 16558 this_type = read_unspecified_type (die, cu);
81a17f79 16559 break;
0114d602
DJ
16560 case DW_TAG_namespace:
16561 this_type = read_namespace_type (die, cu);
16562 break;
f55ee35c
JK
16563 case DW_TAG_module:
16564 this_type = read_module_type (die, cu);
16565 break;
c906108c 16566 default:
3e43a32a
MS
16567 complaint (&symfile_complaints,
16568 _("unexpected tag in read_type_die: '%s'"),
4d3c2250 16569 dwarf_tag_name (die->tag));
c906108c
SS
16570 break;
16571 }
63d06c5c 16572
f792889a 16573 return this_type;
63d06c5c
DC
16574}
16575
abc72ce4
DE
16576/* See if we can figure out if the class lives in a namespace. We do
16577 this by looking for a member function; its demangled name will
16578 contain namespace info, if there is any.
16579 Return the computed name or NULL.
16580 Space for the result is allocated on the objfile's obstack.
16581 This is the full-die version of guess_partial_die_structure_name.
16582 In this case we know DIE has no useful parent. */
16583
16584static char *
16585guess_full_die_structure_name (struct die_info *die, struct dwarf2_cu *cu)
16586{
16587 struct die_info *spec_die;
16588 struct dwarf2_cu *spec_cu;
16589 struct die_info *child;
16590
16591 spec_cu = cu;
16592 spec_die = die_specification (die, &spec_cu);
16593 if (spec_die != NULL)
16594 {
16595 die = spec_die;
16596 cu = spec_cu;
16597 }
16598
16599 for (child = die->child;
16600 child != NULL;
16601 child = child->sibling)
16602 {
16603 if (child->tag == DW_TAG_subprogram)
16604 {
16605 struct attribute *attr;
16606
16607 attr = dwarf2_attr (child, DW_AT_linkage_name, cu);
16608 if (attr == NULL)
16609 attr = dwarf2_attr (child, DW_AT_MIPS_linkage_name, cu);
16610 if (attr != NULL)
16611 {
16612 char *actual_name
16613 = language_class_name_from_physname (cu->language_defn,
16614 DW_STRING (attr));
16615 char *name = NULL;
16616
16617 if (actual_name != NULL)
16618 {
15d034d0 16619 const char *die_name = dwarf2_name (die, cu);
abc72ce4
DE
16620
16621 if (die_name != NULL
16622 && strcmp (die_name, actual_name) != 0)
16623 {
16624 /* Strip off the class name from the full name.
16625 We want the prefix. */
16626 int die_name_len = strlen (die_name);
16627 int actual_name_len = strlen (actual_name);
16628
16629 /* Test for '::' as a sanity check. */
16630 if (actual_name_len > die_name_len + 2
3e43a32a
MS
16631 && actual_name[actual_name_len
16632 - die_name_len - 1] == ':')
abc72ce4 16633 name =
10f0c4bb
TT
16634 obstack_copy0 (&cu->objfile->objfile_obstack,
16635 actual_name,
16636 actual_name_len - die_name_len - 2);
abc72ce4
DE
16637 }
16638 }
16639 xfree (actual_name);
16640 return name;
16641 }
16642 }
16643 }
16644
16645 return NULL;
16646}
16647
96408a79
SA
16648/* GCC might emit a nameless typedef that has a linkage name. Determine the
16649 prefix part in such case. See
16650 http://gcc.gnu.org/bugzilla/show_bug.cgi?id=47510. */
16651
16652static char *
16653anonymous_struct_prefix (struct die_info *die, struct dwarf2_cu *cu)
16654{
16655 struct attribute *attr;
16656 char *base;
16657
16658 if (die->tag != DW_TAG_class_type && die->tag != DW_TAG_interface_type
16659 && die->tag != DW_TAG_structure_type && die->tag != DW_TAG_union_type)
16660 return NULL;
16661
16662 attr = dwarf2_attr (die, DW_AT_name, cu);
16663 if (attr != NULL && DW_STRING (attr) != NULL)
16664 return NULL;
16665
16666 attr = dwarf2_attr (die, DW_AT_linkage_name, cu);
16667 if (attr == NULL)
16668 attr = dwarf2_attr (die, DW_AT_MIPS_linkage_name, cu);
16669 if (attr == NULL || DW_STRING (attr) == NULL)
16670 return NULL;
16671
16672 /* dwarf2_name had to be already called. */
16673 gdb_assert (DW_STRING_IS_CANONICAL (attr));
16674
16675 /* Strip the base name, keep any leading namespaces/classes. */
16676 base = strrchr (DW_STRING (attr), ':');
16677 if (base == NULL || base == DW_STRING (attr) || base[-1] != ':')
16678 return "";
16679
10f0c4bb
TT
16680 return obstack_copy0 (&cu->objfile->objfile_obstack,
16681 DW_STRING (attr), &base[-1] - DW_STRING (attr));
96408a79
SA
16682}
16683
fdde2d81 16684/* Return the name of the namespace/class that DIE is defined within,
0114d602 16685 or "" if we can't tell. The caller should not xfree the result.
fdde2d81 16686
0114d602
DJ
16687 For example, if we're within the method foo() in the following
16688 code:
16689
16690 namespace N {
16691 class C {
16692 void foo () {
16693 }
16694 };
16695 }
16696
16697 then determine_prefix on foo's die will return "N::C". */
fdde2d81 16698
0d5cff50 16699static const char *
e142c38c 16700determine_prefix (struct die_info *die, struct dwarf2_cu *cu)
63d06c5c 16701{
0114d602
DJ
16702 struct die_info *parent, *spec_die;
16703 struct dwarf2_cu *spec_cu;
16704 struct type *parent_type;
96408a79 16705 char *retval;
63d06c5c 16706
f55ee35c
JK
16707 if (cu->language != language_cplus && cu->language != language_java
16708 && cu->language != language_fortran)
0114d602
DJ
16709 return "";
16710
96408a79
SA
16711 retval = anonymous_struct_prefix (die, cu);
16712 if (retval)
16713 return retval;
16714
0114d602
DJ
16715 /* We have to be careful in the presence of DW_AT_specification.
16716 For example, with GCC 3.4, given the code
16717
16718 namespace N {
16719 void foo() {
16720 // Definition of N::foo.
16721 }
16722 }
16723
16724 then we'll have a tree of DIEs like this:
16725
16726 1: DW_TAG_compile_unit
16727 2: DW_TAG_namespace // N
16728 3: DW_TAG_subprogram // declaration of N::foo
16729 4: DW_TAG_subprogram // definition of N::foo
16730 DW_AT_specification // refers to die #3
16731
16732 Thus, when processing die #4, we have to pretend that we're in
16733 the context of its DW_AT_specification, namely the contex of die
16734 #3. */
16735 spec_cu = cu;
16736 spec_die = die_specification (die, &spec_cu);
16737 if (spec_die == NULL)
16738 parent = die->parent;
16739 else
63d06c5c 16740 {
0114d602
DJ
16741 parent = spec_die->parent;
16742 cu = spec_cu;
63d06c5c 16743 }
0114d602
DJ
16744
16745 if (parent == NULL)
16746 return "";
98bfdba5
PA
16747 else if (parent->building_fullname)
16748 {
16749 const char *name;
16750 const char *parent_name;
16751
16752 /* It has been seen on RealView 2.2 built binaries,
16753 DW_TAG_template_type_param types actually _defined_ as
16754 children of the parent class:
16755
16756 enum E {};
16757 template class <class Enum> Class{};
16758 Class<enum E> class_e;
16759
16760 1: DW_TAG_class_type (Class)
16761 2: DW_TAG_enumeration_type (E)
16762 3: DW_TAG_enumerator (enum1:0)
16763 3: DW_TAG_enumerator (enum2:1)
16764 ...
16765 2: DW_TAG_template_type_param
16766 DW_AT_type DW_FORM_ref_udata (E)
16767
16768 Besides being broken debug info, it can put GDB into an
16769 infinite loop. Consider:
16770
16771 When we're building the full name for Class<E>, we'll start
16772 at Class, and go look over its template type parameters,
16773 finding E. We'll then try to build the full name of E, and
16774 reach here. We're now trying to build the full name of E,
16775 and look over the parent DIE for containing scope. In the
16776 broken case, if we followed the parent DIE of E, we'd again
16777 find Class, and once again go look at its template type
16778 arguments, etc., etc. Simply don't consider such parent die
16779 as source-level parent of this die (it can't be, the language
16780 doesn't allow it), and break the loop here. */
16781 name = dwarf2_name (die, cu);
16782 parent_name = dwarf2_name (parent, cu);
16783 complaint (&symfile_complaints,
16784 _("template param type '%s' defined within parent '%s'"),
16785 name ? name : "<unknown>",
16786 parent_name ? parent_name : "<unknown>");
16787 return "";
16788 }
63d06c5c 16789 else
0114d602
DJ
16790 switch (parent->tag)
16791 {
63d06c5c 16792 case DW_TAG_namespace:
0114d602 16793 parent_type = read_type_die (parent, cu);
acebe513
UW
16794 /* GCC 4.0 and 4.1 had a bug (PR c++/28460) where they generated bogus
16795 DW_TAG_namespace DIEs with a name of "::" for the global namespace.
16796 Work around this problem here. */
16797 if (cu->language == language_cplus
16798 && strcmp (TYPE_TAG_NAME (parent_type), "::") == 0)
16799 return "";
0114d602
DJ
16800 /* We give a name to even anonymous namespaces. */
16801 return TYPE_TAG_NAME (parent_type);
63d06c5c 16802 case DW_TAG_class_type:
680b30c7 16803 case DW_TAG_interface_type:
63d06c5c 16804 case DW_TAG_structure_type:
0114d602 16805 case DW_TAG_union_type:
f55ee35c 16806 case DW_TAG_module:
0114d602
DJ
16807 parent_type = read_type_die (parent, cu);
16808 if (TYPE_TAG_NAME (parent_type) != NULL)
16809 return TYPE_TAG_NAME (parent_type);
16810 else
16811 /* An anonymous structure is only allowed non-static data
16812 members; no typedefs, no member functions, et cetera.
16813 So it does not need a prefix. */
16814 return "";
abc72ce4 16815 case DW_TAG_compile_unit:
95554aad 16816 case DW_TAG_partial_unit:
abc72ce4
DE
16817 /* gcc-4.5 -gdwarf-4 can drop the enclosing namespace. Cope. */
16818 if (cu->language == language_cplus
8b70b953 16819 && !VEC_empty (dwarf2_section_info_def, dwarf2_per_objfile->types)
abc72ce4
DE
16820 && die->child != NULL
16821 && (die->tag == DW_TAG_class_type
16822 || die->tag == DW_TAG_structure_type
16823 || die->tag == DW_TAG_union_type))
16824 {
16825 char *name = guess_full_die_structure_name (die, cu);
16826 if (name != NULL)
16827 return name;
16828 }
16829 return "";
63d06c5c 16830 default:
8176b9b8 16831 return determine_prefix (parent, cu);
63d06c5c 16832 }
63d06c5c
DC
16833}
16834
3e43a32a
MS
16835/* Return a newly-allocated string formed by concatenating PREFIX and SUFFIX
16836 with appropriate separator. If PREFIX or SUFFIX is NULL or empty, then
16837 simply copy the SUFFIX or PREFIX, respectively. If OBS is non-null, perform
16838 an obconcat, otherwise allocate storage for the result. The CU argument is
16839 used to determine the language and hence, the appropriate separator. */
987504bb 16840
f55ee35c 16841#define MAX_SEP_LEN 7 /* strlen ("__") + strlen ("_MOD_") */
63d06c5c
DC
16842
16843static char *
f55ee35c
JK
16844typename_concat (struct obstack *obs, const char *prefix, const char *suffix,
16845 int physname, struct dwarf2_cu *cu)
63d06c5c 16846{
f55ee35c 16847 const char *lead = "";
5c315b68 16848 const char *sep;
63d06c5c 16849
3e43a32a
MS
16850 if (suffix == NULL || suffix[0] == '\0'
16851 || prefix == NULL || prefix[0] == '\0')
987504bb
JJ
16852 sep = "";
16853 else if (cu->language == language_java)
16854 sep = ".";
f55ee35c
JK
16855 else if (cu->language == language_fortran && physname)
16856 {
16857 /* This is gfortran specific mangling. Normally DW_AT_linkage_name or
16858 DW_AT_MIPS_linkage_name is preferred and used instead. */
16859
16860 lead = "__";
16861 sep = "_MOD_";
16862 }
987504bb
JJ
16863 else
16864 sep = "::";
63d06c5c 16865
6dd47d34
DE
16866 if (prefix == NULL)
16867 prefix = "";
16868 if (suffix == NULL)
16869 suffix = "";
16870
987504bb
JJ
16871 if (obs == NULL)
16872 {
3e43a32a
MS
16873 char *retval
16874 = xmalloc (strlen (prefix) + MAX_SEP_LEN + strlen (suffix) + 1);
9a619af0 16875
f55ee35c
JK
16876 strcpy (retval, lead);
16877 strcat (retval, prefix);
6dd47d34
DE
16878 strcat (retval, sep);
16879 strcat (retval, suffix);
63d06c5c
DC
16880 return retval;
16881 }
987504bb
JJ
16882 else
16883 {
16884 /* We have an obstack. */
f55ee35c 16885 return obconcat (obs, lead, prefix, sep, suffix, (char *) NULL);
987504bb 16886 }
63d06c5c
DC
16887}
16888
c906108c
SS
16889/* Return sibling of die, NULL if no sibling. */
16890
f9aca02d 16891static struct die_info *
fba45db2 16892sibling_die (struct die_info *die)
c906108c 16893{
639d11d3 16894 return die->sibling;
c906108c
SS
16895}
16896
71c25dea
TT
16897/* Get name of a die, return NULL if not found. */
16898
15d034d0
TT
16899static const char *
16900dwarf2_canonicalize_name (const char *name, struct dwarf2_cu *cu,
71c25dea
TT
16901 struct obstack *obstack)
16902{
16903 if (name && cu->language == language_cplus)
16904 {
16905 char *canon_name = cp_canonicalize_string (name);
16906
16907 if (canon_name != NULL)
16908 {
16909 if (strcmp (canon_name, name) != 0)
10f0c4bb 16910 name = obstack_copy0 (obstack, canon_name, strlen (canon_name));
71c25dea
TT
16911 xfree (canon_name);
16912 }
16913 }
16914
16915 return name;
c906108c
SS
16916}
16917
9219021c
DC
16918/* Get name of a die, return NULL if not found. */
16919
15d034d0 16920static const char *
e142c38c 16921dwarf2_name (struct die_info *die, struct dwarf2_cu *cu)
9219021c
DC
16922{
16923 struct attribute *attr;
16924
e142c38c 16925 attr = dwarf2_attr (die, DW_AT_name, cu);
53832f31
TT
16926 if ((!attr || !DW_STRING (attr))
16927 && die->tag != DW_TAG_class_type
16928 && die->tag != DW_TAG_interface_type
16929 && die->tag != DW_TAG_structure_type
16930 && die->tag != DW_TAG_union_type)
71c25dea
TT
16931 return NULL;
16932
16933 switch (die->tag)
16934 {
16935 case DW_TAG_compile_unit:
95554aad 16936 case DW_TAG_partial_unit:
71c25dea
TT
16937 /* Compilation units have a DW_AT_name that is a filename, not
16938 a source language identifier. */
16939 case DW_TAG_enumeration_type:
16940 case DW_TAG_enumerator:
16941 /* These tags always have simple identifiers already; no need
16942 to canonicalize them. */
16943 return DW_STRING (attr);
907af001 16944
418835cc
KS
16945 case DW_TAG_subprogram:
16946 /* Java constructors will all be named "<init>", so return
16947 the class name when we see this special case. */
16948 if (cu->language == language_java
16949 && DW_STRING (attr) != NULL
16950 && strcmp (DW_STRING (attr), "<init>") == 0)
16951 {
16952 struct dwarf2_cu *spec_cu = cu;
16953 struct die_info *spec_die;
16954
16955 /* GCJ will output '<init>' for Java constructor names.
16956 For this special case, return the name of the parent class. */
16957
16958 /* GCJ may output suprogram DIEs with AT_specification set.
16959 If so, use the name of the specified DIE. */
16960 spec_die = die_specification (die, &spec_cu);
16961 if (spec_die != NULL)
16962 return dwarf2_name (spec_die, spec_cu);
16963
16964 do
16965 {
16966 die = die->parent;
16967 if (die->tag == DW_TAG_class_type)
16968 return dwarf2_name (die, cu);
16969 }
95554aad
TT
16970 while (die->tag != DW_TAG_compile_unit
16971 && die->tag != DW_TAG_partial_unit);
418835cc 16972 }
907af001
UW
16973 break;
16974
16975 case DW_TAG_class_type:
16976 case DW_TAG_interface_type:
16977 case DW_TAG_structure_type:
16978 case DW_TAG_union_type:
16979 /* Some GCC versions emit spurious DW_AT_name attributes for unnamed
16980 structures or unions. These were of the form "._%d" in GCC 4.1,
16981 or simply "<anonymous struct>" or "<anonymous union>" in GCC 4.3
16982 and GCC 4.4. We work around this problem by ignoring these. */
53832f31
TT
16983 if (attr && DW_STRING (attr)
16984 && (strncmp (DW_STRING (attr), "._", 2) == 0
16985 || strncmp (DW_STRING (attr), "<anonymous", 10) == 0))
907af001 16986 return NULL;
53832f31
TT
16987
16988 /* GCC might emit a nameless typedef that has a linkage name. See
16989 http://gcc.gnu.org/bugzilla/show_bug.cgi?id=47510. */
16990 if (!attr || DW_STRING (attr) == NULL)
16991 {
df5c6c50 16992 char *demangled = NULL;
53832f31
TT
16993
16994 attr = dwarf2_attr (die, DW_AT_linkage_name, cu);
16995 if (attr == NULL)
16996 attr = dwarf2_attr (die, DW_AT_MIPS_linkage_name, cu);
16997
16998 if (attr == NULL || DW_STRING (attr) == NULL)
16999 return NULL;
17000
df5c6c50
JK
17001 /* Avoid demangling DW_STRING (attr) the second time on a second
17002 call for the same DIE. */
17003 if (!DW_STRING_IS_CANONICAL (attr))
17004 demangled = cplus_demangle (DW_STRING (attr), DMGL_TYPES);
53832f31
TT
17005
17006 if (demangled)
17007 {
96408a79
SA
17008 char *base;
17009
53832f31 17010 /* FIXME: we already did this for the partial symbol... */
10f0c4bb
TT
17011 DW_STRING (attr) = obstack_copy0 (&cu->objfile->objfile_obstack,
17012 demangled, strlen (demangled));
53832f31
TT
17013 DW_STRING_IS_CANONICAL (attr) = 1;
17014 xfree (demangled);
96408a79
SA
17015
17016 /* Strip any leading namespaces/classes, keep only the base name.
17017 DW_AT_name for named DIEs does not contain the prefixes. */
17018 base = strrchr (DW_STRING (attr), ':');
17019 if (base && base > DW_STRING (attr) && base[-1] == ':')
17020 return &base[1];
17021 else
17022 return DW_STRING (attr);
53832f31
TT
17023 }
17024 }
907af001
UW
17025 break;
17026
71c25dea 17027 default:
907af001
UW
17028 break;
17029 }
17030
17031 if (!DW_STRING_IS_CANONICAL (attr))
17032 {
17033 DW_STRING (attr)
17034 = dwarf2_canonicalize_name (DW_STRING (attr), cu,
17035 &cu->objfile->objfile_obstack);
17036 DW_STRING_IS_CANONICAL (attr) = 1;
71c25dea 17037 }
907af001 17038 return DW_STRING (attr);
9219021c
DC
17039}
17040
17041/* Return the die that this die in an extension of, or NULL if there
f2f0e013
DJ
17042 is none. *EXT_CU is the CU containing DIE on input, and the CU
17043 containing the return value on output. */
9219021c
DC
17044
17045static struct die_info *
f2f0e013 17046dwarf2_extension (struct die_info *die, struct dwarf2_cu **ext_cu)
9219021c
DC
17047{
17048 struct attribute *attr;
9219021c 17049
f2f0e013 17050 attr = dwarf2_attr (die, DW_AT_extension, *ext_cu);
9219021c
DC
17051 if (attr == NULL)
17052 return NULL;
17053
f2f0e013 17054 return follow_die_ref (die, attr, ext_cu);
9219021c
DC
17055}
17056
c906108c
SS
17057/* Convert a DIE tag into its string name. */
17058
f39c6ffd 17059static const char *
aa1ee363 17060dwarf_tag_name (unsigned tag)
c906108c 17061{
f39c6ffd
TT
17062 const char *name = get_DW_TAG_name (tag);
17063
17064 if (name == NULL)
17065 return "DW_TAG_<unknown>";
17066
17067 return name;
c906108c
SS
17068}
17069
17070/* Convert a DWARF attribute code into its string name. */
17071
f39c6ffd 17072static const char *
aa1ee363 17073dwarf_attr_name (unsigned attr)
c906108c 17074{
f39c6ffd
TT
17075 const char *name;
17076
c764a876 17077#ifdef MIPS /* collides with DW_AT_HP_block_index */
f39c6ffd
TT
17078 if (attr == DW_AT_MIPS_fde)
17079 return "DW_AT_MIPS_fde";
17080#else
17081 if (attr == DW_AT_HP_block_index)
17082 return "DW_AT_HP_block_index";
c764a876 17083#endif
f39c6ffd
TT
17084
17085 name = get_DW_AT_name (attr);
17086
17087 if (name == NULL)
17088 return "DW_AT_<unknown>";
17089
17090 return name;
c906108c
SS
17091}
17092
17093/* Convert a DWARF value form code into its string name. */
17094
f39c6ffd 17095static const char *
aa1ee363 17096dwarf_form_name (unsigned form)
c906108c 17097{
f39c6ffd
TT
17098 const char *name = get_DW_FORM_name (form);
17099
17100 if (name == NULL)
17101 return "DW_FORM_<unknown>";
17102
17103 return name;
c906108c
SS
17104}
17105
17106static char *
fba45db2 17107dwarf_bool_name (unsigned mybool)
c906108c
SS
17108{
17109 if (mybool)
17110 return "TRUE";
17111 else
17112 return "FALSE";
17113}
17114
17115/* Convert a DWARF type code into its string name. */
17116
f39c6ffd 17117static const char *
aa1ee363 17118dwarf_type_encoding_name (unsigned enc)
c906108c 17119{
f39c6ffd 17120 const char *name = get_DW_ATE_name (enc);
c906108c 17121
f39c6ffd
TT
17122 if (name == NULL)
17123 return "DW_ATE_<unknown>";
c906108c 17124
f39c6ffd 17125 return name;
c906108c 17126}
c906108c 17127
f9aca02d 17128static void
d97bc12b 17129dump_die_shallow (struct ui_file *f, int indent, struct die_info *die)
c906108c
SS
17130{
17131 unsigned int i;
17132
d97bc12b
DE
17133 print_spaces (indent, f);
17134 fprintf_unfiltered (f, "Die: %s (abbrev %d, offset 0x%x)\n",
b64f50a1 17135 dwarf_tag_name (die->tag), die->abbrev, die->offset.sect_off);
d97bc12b
DE
17136
17137 if (die->parent != NULL)
17138 {
17139 print_spaces (indent, f);
17140 fprintf_unfiltered (f, " parent at offset: 0x%x\n",
b64f50a1 17141 die->parent->offset.sect_off);
d97bc12b
DE
17142 }
17143
17144 print_spaces (indent, f);
17145 fprintf_unfiltered (f, " has children: %s\n",
639d11d3 17146 dwarf_bool_name (die->child != NULL));
c906108c 17147
d97bc12b
DE
17148 print_spaces (indent, f);
17149 fprintf_unfiltered (f, " attributes:\n");
17150
c906108c
SS
17151 for (i = 0; i < die->num_attrs; ++i)
17152 {
d97bc12b
DE
17153 print_spaces (indent, f);
17154 fprintf_unfiltered (f, " %s (%s) ",
c906108c
SS
17155 dwarf_attr_name (die->attrs[i].name),
17156 dwarf_form_name (die->attrs[i].form));
d97bc12b 17157
c906108c
SS
17158 switch (die->attrs[i].form)
17159 {
c906108c 17160 case DW_FORM_addr:
3019eac3 17161 case DW_FORM_GNU_addr_index:
d97bc12b 17162 fprintf_unfiltered (f, "address: ");
5af949e3 17163 fputs_filtered (hex_string (DW_ADDR (&die->attrs[i])), f);
c906108c
SS
17164 break;
17165 case DW_FORM_block2:
17166 case DW_FORM_block4:
17167 case DW_FORM_block:
17168 case DW_FORM_block1:
56eb65bd
SP
17169 fprintf_unfiltered (f, "block: size %s",
17170 pulongest (DW_BLOCK (&die->attrs[i])->size));
c906108c 17171 break;
2dc7f7b3 17172 case DW_FORM_exprloc:
56eb65bd
SP
17173 fprintf_unfiltered (f, "expression: size %s",
17174 pulongest (DW_BLOCK (&die->attrs[i])->size));
2dc7f7b3 17175 break;
4568ecf9
DE
17176 case DW_FORM_ref_addr:
17177 fprintf_unfiltered (f, "ref address: ");
17178 fputs_filtered (hex_string (DW_UNSND (&die->attrs[i])), f);
17179 break;
36586728
TT
17180 case DW_FORM_GNU_ref_alt:
17181 fprintf_unfiltered (f, "alt ref address: ");
17182 fputs_filtered (hex_string (DW_UNSND (&die->attrs[i])), f);
17183 break;
10b3939b
DJ
17184 case DW_FORM_ref1:
17185 case DW_FORM_ref2:
17186 case DW_FORM_ref4:
4568ecf9
DE
17187 case DW_FORM_ref8:
17188 case DW_FORM_ref_udata:
d97bc12b 17189 fprintf_unfiltered (f, "constant ref: 0x%lx (adjusted)",
4568ecf9 17190 (long) (DW_UNSND (&die->attrs[i])));
10b3939b 17191 break;
c906108c
SS
17192 case DW_FORM_data1:
17193 case DW_FORM_data2:
17194 case DW_FORM_data4:
ce5d95e1 17195 case DW_FORM_data8:
c906108c
SS
17196 case DW_FORM_udata:
17197 case DW_FORM_sdata:
43bbcdc2
PH
17198 fprintf_unfiltered (f, "constant: %s",
17199 pulongest (DW_UNSND (&die->attrs[i])));
c906108c 17200 break;
2dc7f7b3
TT
17201 case DW_FORM_sec_offset:
17202 fprintf_unfiltered (f, "section offset: %s",
17203 pulongest (DW_UNSND (&die->attrs[i])));
17204 break;
55f1336d 17205 case DW_FORM_ref_sig8:
348e048f
DE
17206 if (DW_SIGNATURED_TYPE (&die->attrs[i]) != NULL)
17207 fprintf_unfiltered (f, "signatured type, offset: 0x%x",
b64f50a1 17208 DW_SIGNATURED_TYPE (&die->attrs[i])->per_cu.offset.sect_off);
348e048f
DE
17209 else
17210 fprintf_unfiltered (f, "signatured type, offset: unknown");
17211 break;
c906108c 17212 case DW_FORM_string:
4bdf3d34 17213 case DW_FORM_strp:
3019eac3 17214 case DW_FORM_GNU_str_index:
36586728 17215 case DW_FORM_GNU_strp_alt:
8285870a 17216 fprintf_unfiltered (f, "string: \"%s\" (%s canonicalized)",
c906108c 17217 DW_STRING (&die->attrs[i])
8285870a
JK
17218 ? DW_STRING (&die->attrs[i]) : "",
17219 DW_STRING_IS_CANONICAL (&die->attrs[i]) ? "is" : "not");
c906108c
SS
17220 break;
17221 case DW_FORM_flag:
17222 if (DW_UNSND (&die->attrs[i]))
d97bc12b 17223 fprintf_unfiltered (f, "flag: TRUE");
c906108c 17224 else
d97bc12b 17225 fprintf_unfiltered (f, "flag: FALSE");
c906108c 17226 break;
2dc7f7b3
TT
17227 case DW_FORM_flag_present:
17228 fprintf_unfiltered (f, "flag: TRUE");
17229 break;
a8329558 17230 case DW_FORM_indirect:
0963b4bd
MS
17231 /* The reader will have reduced the indirect form to
17232 the "base form" so this form should not occur. */
3e43a32a
MS
17233 fprintf_unfiltered (f,
17234 "unexpected attribute form: DW_FORM_indirect");
a8329558 17235 break;
c906108c 17236 default:
d97bc12b 17237 fprintf_unfiltered (f, "unsupported attribute form: %d.",
c5aa993b 17238 die->attrs[i].form);
d97bc12b 17239 break;
c906108c 17240 }
d97bc12b 17241 fprintf_unfiltered (f, "\n");
c906108c
SS
17242 }
17243}
17244
f9aca02d 17245static void
d97bc12b 17246dump_die_for_error (struct die_info *die)
c906108c 17247{
d97bc12b
DE
17248 dump_die_shallow (gdb_stderr, 0, die);
17249}
17250
17251static void
17252dump_die_1 (struct ui_file *f, int level, int max_level, struct die_info *die)
17253{
17254 int indent = level * 4;
17255
17256 gdb_assert (die != NULL);
17257
17258 if (level >= max_level)
17259 return;
17260
17261 dump_die_shallow (f, indent, die);
17262
17263 if (die->child != NULL)
c906108c 17264 {
d97bc12b
DE
17265 print_spaces (indent, f);
17266 fprintf_unfiltered (f, " Children:");
17267 if (level + 1 < max_level)
17268 {
17269 fprintf_unfiltered (f, "\n");
17270 dump_die_1 (f, level + 1, max_level, die->child);
17271 }
17272 else
17273 {
3e43a32a
MS
17274 fprintf_unfiltered (f,
17275 " [not printed, max nesting level reached]\n");
d97bc12b
DE
17276 }
17277 }
17278
17279 if (die->sibling != NULL && level > 0)
17280 {
17281 dump_die_1 (f, level, max_level, die->sibling);
c906108c
SS
17282 }
17283}
17284
d97bc12b
DE
17285/* This is called from the pdie macro in gdbinit.in.
17286 It's not static so gcc will keep a copy callable from gdb. */
17287
17288void
17289dump_die (struct die_info *die, int max_level)
17290{
17291 dump_die_1 (gdb_stdlog, 0, max_level, die);
17292}
17293
f9aca02d 17294static void
51545339 17295store_in_ref_table (struct die_info *die, struct dwarf2_cu *cu)
c906108c 17296{
51545339 17297 void **slot;
c906108c 17298
b64f50a1
JK
17299 slot = htab_find_slot_with_hash (cu->die_hash, die, die->offset.sect_off,
17300 INSERT);
51545339
DJ
17301
17302 *slot = die;
c906108c
SS
17303}
17304
b64f50a1
JK
17305/* DW_ADDR is always stored already as sect_offset; despite for the forms
17306 besides DW_FORM_ref_addr it is stored as cu_offset in the DWARF file. */
17307
93311388
DE
17308static int
17309is_ref_attr (struct attribute *attr)
c906108c 17310{
c906108c
SS
17311 switch (attr->form)
17312 {
17313 case DW_FORM_ref_addr:
c906108c
SS
17314 case DW_FORM_ref1:
17315 case DW_FORM_ref2:
17316 case DW_FORM_ref4:
613e1657 17317 case DW_FORM_ref8:
c906108c 17318 case DW_FORM_ref_udata:
36586728 17319 case DW_FORM_GNU_ref_alt:
93311388 17320 return 1;
c906108c 17321 default:
93311388 17322 return 0;
c906108c 17323 }
93311388
DE
17324}
17325
b64f50a1
JK
17326/* Return DIE offset of ATTR. Return 0 with complaint if ATTR is not of the
17327 required kind. */
17328
17329static sect_offset
93311388
DE
17330dwarf2_get_ref_die_offset (struct attribute *attr)
17331{
4568ecf9 17332 sect_offset retval = { DW_UNSND (attr) };
b64f50a1 17333
93311388 17334 if (is_ref_attr (attr))
b64f50a1 17335 return retval;
93311388 17336
b64f50a1 17337 retval.sect_off = 0;
93311388
DE
17338 complaint (&symfile_complaints,
17339 _("unsupported die ref attribute form: '%s'"),
17340 dwarf_form_name (attr->form));
b64f50a1 17341 return retval;
c906108c
SS
17342}
17343
43bbcdc2
PH
17344/* Return the constant value held by ATTR. Return DEFAULT_VALUE if
17345 * the value held by the attribute is not constant. */
a02abb62 17346
43bbcdc2 17347static LONGEST
a02abb62
JB
17348dwarf2_get_attr_constant_value (struct attribute *attr, int default_value)
17349{
17350 if (attr->form == DW_FORM_sdata)
17351 return DW_SND (attr);
17352 else if (attr->form == DW_FORM_udata
17353 || attr->form == DW_FORM_data1
17354 || attr->form == DW_FORM_data2
17355 || attr->form == DW_FORM_data4
17356 || attr->form == DW_FORM_data8)
17357 return DW_UNSND (attr);
17358 else
17359 {
3e43a32a
MS
17360 complaint (&symfile_complaints,
17361 _("Attribute value is not a constant (%s)"),
a02abb62
JB
17362 dwarf_form_name (attr->form));
17363 return default_value;
17364 }
17365}
17366
348e048f
DE
17367/* Follow reference or signature attribute ATTR of SRC_DIE.
17368 On entry *REF_CU is the CU of SRC_DIE.
17369 On exit *REF_CU is the CU of the result. */
17370
17371static struct die_info *
17372follow_die_ref_or_sig (struct die_info *src_die, struct attribute *attr,
17373 struct dwarf2_cu **ref_cu)
17374{
17375 struct die_info *die;
17376
17377 if (is_ref_attr (attr))
17378 die = follow_die_ref (src_die, attr, ref_cu);
55f1336d 17379 else if (attr->form == DW_FORM_ref_sig8)
348e048f
DE
17380 die = follow_die_sig (src_die, attr, ref_cu);
17381 else
17382 {
17383 dump_die_for_error (src_die);
17384 error (_("Dwarf Error: Expected reference attribute [in module %s]"),
17385 (*ref_cu)->objfile->name);
17386 }
17387
17388 return die;
03dd20cc
DJ
17389}
17390
5c631832 17391/* Follow reference OFFSET.
673bfd45
DE
17392 On entry *REF_CU is the CU of the source die referencing OFFSET.
17393 On exit *REF_CU is the CU of the result.
17394 Returns NULL if OFFSET is invalid. */
f504f079 17395
f9aca02d 17396static struct die_info *
36586728
TT
17397follow_die_offset (sect_offset offset, int offset_in_dwz,
17398 struct dwarf2_cu **ref_cu)
c906108c 17399{
10b3939b 17400 struct die_info temp_die;
f2f0e013 17401 struct dwarf2_cu *target_cu, *cu = *ref_cu;
10b3939b 17402
348e048f
DE
17403 gdb_assert (cu->per_cu != NULL);
17404
98bfdba5
PA
17405 target_cu = cu;
17406
3019eac3 17407 if (cu->per_cu->is_debug_types)
348e048f
DE
17408 {
17409 /* .debug_types CUs cannot reference anything outside their CU.
17410 If they need to, they have to reference a signatured type via
55f1336d 17411 DW_FORM_ref_sig8. */
348e048f 17412 if (! offset_in_cu_p (&cu->header, offset))
5c631832 17413 return NULL;
348e048f 17414 }
36586728
TT
17415 else if (offset_in_dwz != cu->per_cu->is_dwz
17416 || ! offset_in_cu_p (&cu->header, offset))
10b3939b
DJ
17417 {
17418 struct dwarf2_per_cu_data *per_cu;
9a619af0 17419
36586728
TT
17420 per_cu = dwarf2_find_containing_comp_unit (offset, offset_in_dwz,
17421 cu->objfile);
03dd20cc
DJ
17422
17423 /* If necessary, add it to the queue and load its DIEs. */
95554aad
TT
17424 if (maybe_queue_comp_unit (cu, per_cu, cu->language))
17425 load_full_comp_unit (per_cu, cu->language);
03dd20cc 17426
10b3939b
DJ
17427 target_cu = per_cu->cu;
17428 }
98bfdba5
PA
17429 else if (cu->dies == NULL)
17430 {
17431 /* We're loading full DIEs during partial symbol reading. */
17432 gdb_assert (dwarf2_per_objfile->reading_partial_symbols);
95554aad 17433 load_full_comp_unit (cu->per_cu, language_minimal);
98bfdba5 17434 }
c906108c 17435
f2f0e013 17436 *ref_cu = target_cu;
51545339 17437 temp_die.offset = offset;
b64f50a1 17438 return htab_find_with_hash (target_cu->die_hash, &temp_die, offset.sect_off);
5c631832 17439}
10b3939b 17440
5c631832
JK
17441/* Follow reference attribute ATTR of SRC_DIE.
17442 On entry *REF_CU is the CU of SRC_DIE.
17443 On exit *REF_CU is the CU of the result. */
17444
17445static struct die_info *
17446follow_die_ref (struct die_info *src_die, struct attribute *attr,
17447 struct dwarf2_cu **ref_cu)
17448{
b64f50a1 17449 sect_offset offset = dwarf2_get_ref_die_offset (attr);
5c631832
JK
17450 struct dwarf2_cu *cu = *ref_cu;
17451 struct die_info *die;
17452
36586728
TT
17453 die = follow_die_offset (offset,
17454 (attr->form == DW_FORM_GNU_ref_alt
17455 || cu->per_cu->is_dwz),
17456 ref_cu);
5c631832
JK
17457 if (!die)
17458 error (_("Dwarf Error: Cannot find DIE at 0x%x referenced from DIE "
17459 "at 0x%x [in module %s]"),
b64f50a1 17460 offset.sect_off, src_die->offset.sect_off, cu->objfile->name);
348e048f 17461
5c631832
JK
17462 return die;
17463}
17464
d83e736b
JK
17465/* Return DWARF block referenced by DW_AT_location of DIE at OFFSET at PER_CU.
17466 Returned value is intended for DW_OP_call*. Returned
17467 dwarf2_locexpr_baton->data has lifetime of PER_CU->OBJFILE. */
5c631832
JK
17468
17469struct dwarf2_locexpr_baton
8b9737bf
TT
17470dwarf2_fetch_die_loc_sect_off (sect_offset offset,
17471 struct dwarf2_per_cu_data *per_cu,
17472 CORE_ADDR (*get_frame_pc) (void *baton),
17473 void *baton)
5c631832 17474{
918dd910 17475 struct dwarf2_cu *cu;
5c631832
JK
17476 struct die_info *die;
17477 struct attribute *attr;
17478 struct dwarf2_locexpr_baton retval;
17479
8cf6f0b1
TT
17480 dw2_setup (per_cu->objfile);
17481
918dd910
JK
17482 if (per_cu->cu == NULL)
17483 load_cu (per_cu);
17484 cu = per_cu->cu;
17485
36586728 17486 die = follow_die_offset (offset, per_cu->is_dwz, &cu);
5c631832
JK
17487 if (!die)
17488 error (_("Dwarf Error: Cannot find DIE at 0x%x referenced in module %s"),
b64f50a1 17489 offset.sect_off, per_cu->objfile->name);
5c631832
JK
17490
17491 attr = dwarf2_attr (die, DW_AT_location, cu);
17492 if (!attr)
17493 {
e103e986
JK
17494 /* DWARF: "If there is no such attribute, then there is no effect.".
17495 DATA is ignored if SIZE is 0. */
5c631832 17496
e103e986 17497 retval.data = NULL;
5c631832
JK
17498 retval.size = 0;
17499 }
8cf6f0b1
TT
17500 else if (attr_form_is_section_offset (attr))
17501 {
17502 struct dwarf2_loclist_baton loclist_baton;
17503 CORE_ADDR pc = (*get_frame_pc) (baton);
17504 size_t size;
17505
17506 fill_in_loclist_baton (cu, &loclist_baton, attr);
17507
17508 retval.data = dwarf2_find_location_expression (&loclist_baton,
17509 &size, pc);
17510 retval.size = size;
17511 }
5c631832
JK
17512 else
17513 {
17514 if (!attr_form_is_block (attr))
17515 error (_("Dwarf Error: DIE at 0x%x referenced in module %s "
17516 "is neither DW_FORM_block* nor DW_FORM_exprloc"),
b64f50a1 17517 offset.sect_off, per_cu->objfile->name);
5c631832
JK
17518
17519 retval.data = DW_BLOCK (attr)->data;
17520 retval.size = DW_BLOCK (attr)->size;
17521 }
17522 retval.per_cu = cu->per_cu;
918dd910 17523
918dd910
JK
17524 age_cached_comp_units ();
17525
5c631832 17526 return retval;
348e048f
DE
17527}
17528
8b9737bf
TT
17529/* Like dwarf2_fetch_die_loc_sect_off, but take a CU
17530 offset. */
17531
17532struct dwarf2_locexpr_baton
17533dwarf2_fetch_die_loc_cu_off (cu_offset offset_in_cu,
17534 struct dwarf2_per_cu_data *per_cu,
17535 CORE_ADDR (*get_frame_pc) (void *baton),
17536 void *baton)
17537{
17538 sect_offset offset = { per_cu->offset.sect_off + offset_in_cu.cu_off };
17539
17540 return dwarf2_fetch_die_loc_sect_off (offset, per_cu, get_frame_pc, baton);
17541}
17542
8a9b8146
TT
17543/* Return the type of the DIE at DIE_OFFSET in the CU named by
17544 PER_CU. */
17545
17546struct type *
b64f50a1 17547dwarf2_get_die_type (cu_offset die_offset,
8a9b8146
TT
17548 struct dwarf2_per_cu_data *per_cu)
17549{
b64f50a1
JK
17550 sect_offset die_offset_sect;
17551
8a9b8146 17552 dw2_setup (per_cu->objfile);
b64f50a1
JK
17553
17554 die_offset_sect.sect_off = per_cu->offset.sect_off + die_offset.cu_off;
17555 return get_die_type_at_offset (die_offset_sect, per_cu);
8a9b8146
TT
17556}
17557
348e048f
DE
17558/* Follow the signature attribute ATTR in SRC_DIE.
17559 On entry *REF_CU is the CU of SRC_DIE.
17560 On exit *REF_CU is the CU of the result. */
17561
17562static struct die_info *
17563follow_die_sig (struct die_info *src_die, struct attribute *attr,
17564 struct dwarf2_cu **ref_cu)
17565{
17566 struct objfile *objfile = (*ref_cu)->objfile;
17567 struct die_info temp_die;
17568 struct signatured_type *sig_type = DW_SIGNATURED_TYPE (attr);
17569 struct dwarf2_cu *sig_cu;
17570 struct die_info *die;
17571
17572 /* sig_type will be NULL if the signatured type is missing from
17573 the debug info. */
17574 if (sig_type == NULL)
17575 error (_("Dwarf Error: Cannot find signatured DIE referenced from DIE "
17576 "at 0x%x [in module %s]"),
b64f50a1 17577 src_die->offset.sect_off, objfile->name);
348e048f
DE
17578
17579 /* If necessary, add it to the queue and load its DIEs. */
17580
95554aad 17581 if (maybe_queue_comp_unit (*ref_cu, &sig_type->per_cu, language_minimal))
a0f42c21 17582 read_signatured_type (sig_type);
348e048f
DE
17583
17584 gdb_assert (sig_type->per_cu.cu != NULL);
17585
17586 sig_cu = sig_type->per_cu.cu;
3019eac3
DE
17587 gdb_assert (sig_type->type_offset_in_section.sect_off != 0);
17588 temp_die.offset = sig_type->type_offset_in_section;
b64f50a1
JK
17589 die = htab_find_with_hash (sig_cu->die_hash, &temp_die,
17590 temp_die.offset.sect_off);
348e048f
DE
17591 if (die)
17592 {
796a7ff8
DE
17593 /* For .gdb_index version 7 keep track of included TUs.
17594 http://sourceware.org/bugzilla/show_bug.cgi?id=15021. */
17595 if (dwarf2_per_objfile->index_table != NULL
17596 && dwarf2_per_objfile->index_table->version <= 7)
17597 {
17598 VEC_safe_push (dwarf2_per_cu_ptr,
17599 (*ref_cu)->per_cu->imported_symtabs,
17600 sig_cu->per_cu);
17601 }
17602
348e048f
DE
17603 *ref_cu = sig_cu;
17604 return die;
17605 }
17606
3e43a32a
MS
17607 error (_("Dwarf Error: Cannot find signatured DIE at 0x%x referenced "
17608 "from DIE at 0x%x [in module %s]"),
b64f50a1 17609 temp_die.offset.sect_off, src_die->offset.sect_off, objfile->name);
348e048f
DE
17610}
17611
17612/* Given an offset of a signatured type, return its signatured_type. */
17613
17614static struct signatured_type *
8b70b953
TT
17615lookup_signatured_type_at_offset (struct objfile *objfile,
17616 struct dwarf2_section_info *section,
b64f50a1 17617 sect_offset offset)
348e048f 17618{
b64f50a1 17619 gdb_byte *info_ptr = section->buffer + offset.sect_off;
348e048f
DE
17620 unsigned int length, initial_length_size;
17621 unsigned int sig_offset;
52dc124a 17622 struct signatured_type find_entry, *sig_type;
348e048f
DE
17623
17624 length = read_initial_length (objfile->obfd, info_ptr, &initial_length_size);
17625 sig_offset = (initial_length_size
17626 + 2 /*version*/
17627 + (initial_length_size == 4 ? 4 : 8) /*debug_abbrev_offset*/
17628 + 1 /*address_size*/);
17629 find_entry.signature = bfd_get_64 (objfile->obfd, info_ptr + sig_offset);
52dc124a 17630 sig_type = htab_find (dwarf2_per_objfile->signatured_types, &find_entry);
348e048f
DE
17631
17632 /* This is only used to lookup previously recorded types.
17633 If we didn't find it, it's our bug. */
52dc124a
DE
17634 gdb_assert (sig_type != NULL);
17635 gdb_assert (offset.sect_off == sig_type->per_cu.offset.sect_off);
348e048f 17636
52dc124a 17637 return sig_type;
348e048f
DE
17638}
17639
e5fe5e75 17640/* Load the DIEs associated with type unit PER_CU into memory. */
348e048f
DE
17641
17642static void
e5fe5e75 17643load_full_type_unit (struct dwarf2_per_cu_data *per_cu)
348e048f 17644{
52dc124a 17645 struct signatured_type *sig_type;
348e048f 17646
f4dc4d17
DE
17647 /* Caller is responsible for ensuring type_unit_groups don't get here. */
17648 gdb_assert (! IS_TYPE_UNIT_GROUP (per_cu));
17649
6721b2ec
DE
17650 /* We have the per_cu, but we need the signatured_type.
17651 Fortunately this is an easy translation. */
17652 gdb_assert (per_cu->is_debug_types);
17653 sig_type = (struct signatured_type *) per_cu;
348e048f 17654
6721b2ec 17655 gdb_assert (per_cu->cu == NULL);
348e048f 17656
52dc124a 17657 read_signatured_type (sig_type);
348e048f 17658
6721b2ec 17659 gdb_assert (per_cu->cu != NULL);
348e048f
DE
17660}
17661
dee91e82
DE
17662/* die_reader_func for read_signatured_type.
17663 This is identical to load_full_comp_unit_reader,
17664 but is kept separate for now. */
348e048f
DE
17665
17666static void
dee91e82
DE
17667read_signatured_type_reader (const struct die_reader_specs *reader,
17668 gdb_byte *info_ptr,
17669 struct die_info *comp_unit_die,
17670 int has_children,
17671 void *data)
348e048f 17672{
dee91e82 17673 struct dwarf2_cu *cu = reader->cu;
348e048f 17674
dee91e82
DE
17675 gdb_assert (cu->die_hash == NULL);
17676 cu->die_hash =
17677 htab_create_alloc_ex (cu->header.length / 12,
17678 die_hash,
17679 die_eq,
17680 NULL,
17681 &cu->comp_unit_obstack,
17682 hashtab_obstack_allocate,
17683 dummy_obstack_deallocate);
348e048f 17684
dee91e82
DE
17685 if (has_children)
17686 comp_unit_die->child = read_die_and_siblings (reader, info_ptr,
17687 &info_ptr, comp_unit_die);
17688 cu->dies = comp_unit_die;
17689 /* comp_unit_die is not stored in die_hash, no need. */
348e048f
DE
17690
17691 /* We try not to read any attributes in this function, because not
9cdd5dbd 17692 all CUs needed for references have been loaded yet, and symbol
348e048f 17693 table processing isn't initialized. But we have to set the CU language,
dee91e82
DE
17694 or we won't be able to build types correctly.
17695 Similarly, if we do not read the producer, we can not apply
17696 producer-specific interpretation. */
95554aad 17697 prepare_one_comp_unit (cu, cu->dies, language_minimal);
dee91e82 17698}
348e048f 17699
3019eac3
DE
17700/* Read in a signatured type and build its CU and DIEs.
17701 If the type is a stub for the real type in a DWO file,
17702 read in the real type from the DWO file as well. */
dee91e82
DE
17703
17704static void
17705read_signatured_type (struct signatured_type *sig_type)
17706{
17707 struct dwarf2_per_cu_data *per_cu = &sig_type->per_cu;
348e048f 17708
3019eac3 17709 gdb_assert (per_cu->is_debug_types);
dee91e82 17710 gdb_assert (per_cu->cu == NULL);
348e048f 17711
f4dc4d17
DE
17712 init_cutu_and_read_dies (per_cu, NULL, 0, 1,
17713 read_signatured_type_reader, NULL);
c906108c
SS
17714}
17715
c906108c
SS
17716/* Decode simple location descriptions.
17717 Given a pointer to a dwarf block that defines a location, compute
17718 the location and return the value.
17719
4cecd739
DJ
17720 NOTE drow/2003-11-18: This function is called in two situations
17721 now: for the address of static or global variables (partial symbols
17722 only) and for offsets into structures which are expected to be
17723 (more or less) constant. The partial symbol case should go away,
17724 and only the constant case should remain. That will let this
17725 function complain more accurately. A few special modes are allowed
17726 without complaint for global variables (for instance, global
17727 register values and thread-local values).
c906108c
SS
17728
17729 A location description containing no operations indicates that the
4cecd739 17730 object is optimized out. The return value is 0 for that case.
6b992462
DJ
17731 FIXME drow/2003-11-16: No callers check for this case any more; soon all
17732 callers will only want a very basic result and this can become a
21ae7a4d
JK
17733 complaint.
17734
17735 Note that stack[0] is unused except as a default error return. */
c906108c
SS
17736
17737static CORE_ADDR
e7c27a73 17738decode_locdesc (struct dwarf_block *blk, struct dwarf2_cu *cu)
c906108c 17739{
e7c27a73 17740 struct objfile *objfile = cu->objfile;
56eb65bd
SP
17741 size_t i;
17742 size_t size = blk->size;
21ae7a4d
JK
17743 gdb_byte *data = blk->data;
17744 CORE_ADDR stack[64];
17745 int stacki;
17746 unsigned int bytes_read, unsnd;
17747 gdb_byte op;
c906108c 17748
21ae7a4d
JK
17749 i = 0;
17750 stacki = 0;
17751 stack[stacki] = 0;
17752 stack[++stacki] = 0;
17753
17754 while (i < size)
17755 {
17756 op = data[i++];
17757 switch (op)
17758 {
17759 case DW_OP_lit0:
17760 case DW_OP_lit1:
17761 case DW_OP_lit2:
17762 case DW_OP_lit3:
17763 case DW_OP_lit4:
17764 case DW_OP_lit5:
17765 case DW_OP_lit6:
17766 case DW_OP_lit7:
17767 case DW_OP_lit8:
17768 case DW_OP_lit9:
17769 case DW_OP_lit10:
17770 case DW_OP_lit11:
17771 case DW_OP_lit12:
17772 case DW_OP_lit13:
17773 case DW_OP_lit14:
17774 case DW_OP_lit15:
17775 case DW_OP_lit16:
17776 case DW_OP_lit17:
17777 case DW_OP_lit18:
17778 case DW_OP_lit19:
17779 case DW_OP_lit20:
17780 case DW_OP_lit21:
17781 case DW_OP_lit22:
17782 case DW_OP_lit23:
17783 case DW_OP_lit24:
17784 case DW_OP_lit25:
17785 case DW_OP_lit26:
17786 case DW_OP_lit27:
17787 case DW_OP_lit28:
17788 case DW_OP_lit29:
17789 case DW_OP_lit30:
17790 case DW_OP_lit31:
17791 stack[++stacki] = op - DW_OP_lit0;
17792 break;
f1bea926 17793
21ae7a4d
JK
17794 case DW_OP_reg0:
17795 case DW_OP_reg1:
17796 case DW_OP_reg2:
17797 case DW_OP_reg3:
17798 case DW_OP_reg4:
17799 case DW_OP_reg5:
17800 case DW_OP_reg6:
17801 case DW_OP_reg7:
17802 case DW_OP_reg8:
17803 case DW_OP_reg9:
17804 case DW_OP_reg10:
17805 case DW_OP_reg11:
17806 case DW_OP_reg12:
17807 case DW_OP_reg13:
17808 case DW_OP_reg14:
17809 case DW_OP_reg15:
17810 case DW_OP_reg16:
17811 case DW_OP_reg17:
17812 case DW_OP_reg18:
17813 case DW_OP_reg19:
17814 case DW_OP_reg20:
17815 case DW_OP_reg21:
17816 case DW_OP_reg22:
17817 case DW_OP_reg23:
17818 case DW_OP_reg24:
17819 case DW_OP_reg25:
17820 case DW_OP_reg26:
17821 case DW_OP_reg27:
17822 case DW_OP_reg28:
17823 case DW_OP_reg29:
17824 case DW_OP_reg30:
17825 case DW_OP_reg31:
17826 stack[++stacki] = op - DW_OP_reg0;
17827 if (i < size)
17828 dwarf2_complex_location_expr_complaint ();
17829 break;
c906108c 17830
21ae7a4d
JK
17831 case DW_OP_regx:
17832 unsnd = read_unsigned_leb128 (NULL, (data + i), &bytes_read);
17833 i += bytes_read;
17834 stack[++stacki] = unsnd;
17835 if (i < size)
17836 dwarf2_complex_location_expr_complaint ();
17837 break;
c906108c 17838
21ae7a4d
JK
17839 case DW_OP_addr:
17840 stack[++stacki] = read_address (objfile->obfd, &data[i],
17841 cu, &bytes_read);
17842 i += bytes_read;
17843 break;
d53d4ac5 17844
21ae7a4d
JK
17845 case DW_OP_const1u:
17846 stack[++stacki] = read_1_byte (objfile->obfd, &data[i]);
17847 i += 1;
17848 break;
17849
17850 case DW_OP_const1s:
17851 stack[++stacki] = read_1_signed_byte (objfile->obfd, &data[i]);
17852 i += 1;
17853 break;
17854
17855 case DW_OP_const2u:
17856 stack[++stacki] = read_2_bytes (objfile->obfd, &data[i]);
17857 i += 2;
17858 break;
17859
17860 case DW_OP_const2s:
17861 stack[++stacki] = read_2_signed_bytes (objfile->obfd, &data[i]);
17862 i += 2;
17863 break;
d53d4ac5 17864
21ae7a4d
JK
17865 case DW_OP_const4u:
17866 stack[++stacki] = read_4_bytes (objfile->obfd, &data[i]);
17867 i += 4;
17868 break;
17869
17870 case DW_OP_const4s:
17871 stack[++stacki] = read_4_signed_bytes (objfile->obfd, &data[i]);
17872 i += 4;
17873 break;
17874
585861ea
JK
17875 case DW_OP_const8u:
17876 stack[++stacki] = read_8_bytes (objfile->obfd, &data[i]);
17877 i += 8;
17878 break;
17879
21ae7a4d
JK
17880 case DW_OP_constu:
17881 stack[++stacki] = read_unsigned_leb128 (NULL, (data + i),
17882 &bytes_read);
17883 i += bytes_read;
17884 break;
17885
17886 case DW_OP_consts:
17887 stack[++stacki] = read_signed_leb128 (NULL, (data + i), &bytes_read);
17888 i += bytes_read;
17889 break;
17890
17891 case DW_OP_dup:
17892 stack[stacki + 1] = stack[stacki];
17893 stacki++;
17894 break;
17895
17896 case DW_OP_plus:
17897 stack[stacki - 1] += stack[stacki];
17898 stacki--;
17899 break;
17900
17901 case DW_OP_plus_uconst:
17902 stack[stacki] += read_unsigned_leb128 (NULL, (data + i),
17903 &bytes_read);
17904 i += bytes_read;
17905 break;
17906
17907 case DW_OP_minus:
17908 stack[stacki - 1] -= stack[stacki];
17909 stacki--;
17910 break;
17911
17912 case DW_OP_deref:
17913 /* If we're not the last op, then we definitely can't encode
17914 this using GDB's address_class enum. This is valid for partial
17915 global symbols, although the variable's address will be bogus
17916 in the psymtab. */
17917 if (i < size)
17918 dwarf2_complex_location_expr_complaint ();
17919 break;
17920
17921 case DW_OP_GNU_push_tls_address:
17922 /* The top of the stack has the offset from the beginning
17923 of the thread control block at which the variable is located. */
17924 /* Nothing should follow this operator, so the top of stack would
17925 be returned. */
17926 /* This is valid for partial global symbols, but the variable's
585861ea
JK
17927 address will be bogus in the psymtab. Make it always at least
17928 non-zero to not look as a variable garbage collected by linker
17929 which have DW_OP_addr 0. */
21ae7a4d
JK
17930 if (i < size)
17931 dwarf2_complex_location_expr_complaint ();
585861ea 17932 stack[stacki]++;
21ae7a4d
JK
17933 break;
17934
17935 case DW_OP_GNU_uninit:
17936 break;
17937
3019eac3 17938 case DW_OP_GNU_addr_index:
49f6c839 17939 case DW_OP_GNU_const_index:
3019eac3
DE
17940 stack[++stacki] = read_addr_index_from_leb128 (cu, &data[i],
17941 &bytes_read);
17942 i += bytes_read;
17943 break;
17944
21ae7a4d
JK
17945 default:
17946 {
f39c6ffd 17947 const char *name = get_DW_OP_name (op);
21ae7a4d
JK
17948
17949 if (name)
17950 complaint (&symfile_complaints, _("unsupported stack op: '%s'"),
17951 name);
17952 else
17953 complaint (&symfile_complaints, _("unsupported stack op: '%02x'"),
17954 op);
17955 }
17956
17957 return (stack[stacki]);
d53d4ac5 17958 }
3c6e0cb3 17959
21ae7a4d
JK
17960 /* Enforce maximum stack depth of SIZE-1 to avoid writing
17961 outside of the allocated space. Also enforce minimum>0. */
17962 if (stacki >= ARRAY_SIZE (stack) - 1)
17963 {
17964 complaint (&symfile_complaints,
17965 _("location description stack overflow"));
17966 return 0;
17967 }
17968
17969 if (stacki <= 0)
17970 {
17971 complaint (&symfile_complaints,
17972 _("location description stack underflow"));
17973 return 0;
17974 }
17975 }
17976 return (stack[stacki]);
c906108c
SS
17977}
17978
17979/* memory allocation interface */
17980
c906108c 17981static struct dwarf_block *
7b5a2f43 17982dwarf_alloc_block (struct dwarf2_cu *cu)
c906108c
SS
17983{
17984 struct dwarf_block *blk;
17985
17986 blk = (struct dwarf_block *)
7b5a2f43 17987 obstack_alloc (&cu->comp_unit_obstack, sizeof (struct dwarf_block));
c906108c
SS
17988 return (blk);
17989}
17990
c906108c 17991static struct die_info *
b60c80d6 17992dwarf_alloc_die (struct dwarf2_cu *cu, int num_attrs)
c906108c
SS
17993{
17994 struct die_info *die;
b60c80d6
DJ
17995 size_t size = sizeof (struct die_info);
17996
17997 if (num_attrs > 1)
17998 size += (num_attrs - 1) * sizeof (struct attribute);
c906108c 17999
b60c80d6 18000 die = (struct die_info *) obstack_alloc (&cu->comp_unit_obstack, size);
c906108c
SS
18001 memset (die, 0, sizeof (struct die_info));
18002 return (die);
18003}
2e276125
JB
18004
18005\f
18006/* Macro support. */
18007
2e276125
JB
18008/* Return the full name of file number I in *LH's file name table.
18009 Use COMP_DIR as the name of the current directory of the
18010 compilation. The result is allocated using xmalloc; the caller is
18011 responsible for freeing it. */
18012static char *
18013file_full_name (int file, struct line_header *lh, const char *comp_dir)
18014{
6a83a1e6
EZ
18015 /* Is the file number a valid index into the line header's file name
18016 table? Remember that file numbers start with one, not zero. */
18017 if (1 <= file && file <= lh->num_file_names)
18018 {
18019 struct file_entry *fe = &lh->file_names[file - 1];
6e70227d 18020
6a83a1e6
EZ
18021 if (IS_ABSOLUTE_PATH (fe->name))
18022 return xstrdup (fe->name);
18023 else
18024 {
18025 const char *dir;
18026 int dir_len;
18027 char *full_name;
18028
18029 if (fe->dir_index)
18030 dir = lh->include_dirs[fe->dir_index - 1];
18031 else
18032 dir = comp_dir;
18033
18034 if (dir)
18035 {
18036 dir_len = strlen (dir);
18037 full_name = xmalloc (dir_len + 1 + strlen (fe->name) + 1);
18038 strcpy (full_name, dir);
18039 full_name[dir_len] = '/';
18040 strcpy (full_name + dir_len + 1, fe->name);
18041 return full_name;
18042 }
18043 else
18044 return xstrdup (fe->name);
18045 }
18046 }
2e276125
JB
18047 else
18048 {
6a83a1e6
EZ
18049 /* The compiler produced a bogus file number. We can at least
18050 record the macro definitions made in the file, even if we
18051 won't be able to find the file by name. */
18052 char fake_name[80];
9a619af0 18053
8c042590
PM
18054 xsnprintf (fake_name, sizeof (fake_name),
18055 "<bad macro file number %d>", file);
2e276125 18056
6e70227d 18057 complaint (&symfile_complaints,
6a83a1e6
EZ
18058 _("bad file number in macro information (%d)"),
18059 file);
2e276125 18060
6a83a1e6 18061 return xstrdup (fake_name);
2e276125
JB
18062 }
18063}
18064
18065
18066static struct macro_source_file *
18067macro_start_file (int file, int line,
18068 struct macro_source_file *current_file,
18069 const char *comp_dir,
18070 struct line_header *lh, struct objfile *objfile)
18071{
18072 /* The full name of this source file. */
18073 char *full_name = file_full_name (file, lh, comp_dir);
18074
18075 /* We don't create a macro table for this compilation unit
18076 at all until we actually get a filename. */
18077 if (! pending_macros)
6532ff36
TT
18078 pending_macros = new_macro_table (&objfile->per_bfd->storage_obstack,
18079 objfile->per_bfd->macro_cache);
2e276125
JB
18080
18081 if (! current_file)
abc9d0dc
TT
18082 {
18083 /* If we have no current file, then this must be the start_file
18084 directive for the compilation unit's main source file. */
18085 current_file = macro_set_main (pending_macros, full_name);
18086 macro_define_special (pending_macros);
18087 }
2e276125
JB
18088 else
18089 current_file = macro_include (current_file, line, full_name);
18090
18091 xfree (full_name);
6e70227d 18092
2e276125
JB
18093 return current_file;
18094}
18095
18096
18097/* Copy the LEN characters at BUF to a xmalloc'ed block of memory,
18098 followed by a null byte. */
18099static char *
18100copy_string (const char *buf, int len)
18101{
18102 char *s = xmalloc (len + 1);
9a619af0 18103
2e276125
JB
18104 memcpy (s, buf, len);
18105 s[len] = '\0';
2e276125
JB
18106 return s;
18107}
18108
18109
18110static const char *
18111consume_improper_spaces (const char *p, const char *body)
18112{
18113 if (*p == ' ')
18114 {
4d3c2250 18115 complaint (&symfile_complaints,
3e43a32a
MS
18116 _("macro definition contains spaces "
18117 "in formal argument list:\n`%s'"),
4d3c2250 18118 body);
2e276125
JB
18119
18120 while (*p == ' ')
18121 p++;
18122 }
18123
18124 return p;
18125}
18126
18127
18128static void
18129parse_macro_definition (struct macro_source_file *file, int line,
18130 const char *body)
18131{
18132 const char *p;
18133
18134 /* The body string takes one of two forms. For object-like macro
18135 definitions, it should be:
18136
18137 <macro name> " " <definition>
18138
18139 For function-like macro definitions, it should be:
18140
18141 <macro name> "() " <definition>
18142 or
18143 <macro name> "(" <arg name> ( "," <arg name> ) * ") " <definition>
18144
18145 Spaces may appear only where explicitly indicated, and in the
18146 <definition>.
18147
18148 The Dwarf 2 spec says that an object-like macro's name is always
18149 followed by a space, but versions of GCC around March 2002 omit
6e70227d 18150 the space when the macro's definition is the empty string.
2e276125
JB
18151
18152 The Dwarf 2 spec says that there should be no spaces between the
18153 formal arguments in a function-like macro's formal argument list,
18154 but versions of GCC around March 2002 include spaces after the
18155 commas. */
18156
18157
18158 /* Find the extent of the macro name. The macro name is terminated
18159 by either a space or null character (for an object-like macro) or
18160 an opening paren (for a function-like macro). */
18161 for (p = body; *p; p++)
18162 if (*p == ' ' || *p == '(')
18163 break;
18164
18165 if (*p == ' ' || *p == '\0')
18166 {
18167 /* It's an object-like macro. */
18168 int name_len = p - body;
18169 char *name = copy_string (body, name_len);
18170 const char *replacement;
18171
18172 if (*p == ' ')
18173 replacement = body + name_len + 1;
18174 else
18175 {
4d3c2250 18176 dwarf2_macro_malformed_definition_complaint (body);
2e276125
JB
18177 replacement = body + name_len;
18178 }
6e70227d 18179
2e276125
JB
18180 macro_define_object (file, line, name, replacement);
18181
18182 xfree (name);
18183 }
18184 else if (*p == '(')
18185 {
18186 /* It's a function-like macro. */
18187 char *name = copy_string (body, p - body);
18188 int argc = 0;
18189 int argv_size = 1;
18190 char **argv = xmalloc (argv_size * sizeof (*argv));
18191
18192 p++;
18193
18194 p = consume_improper_spaces (p, body);
18195
18196 /* Parse the formal argument list. */
18197 while (*p && *p != ')')
18198 {
18199 /* Find the extent of the current argument name. */
18200 const char *arg_start = p;
18201
18202 while (*p && *p != ',' && *p != ')' && *p != ' ')
18203 p++;
18204
18205 if (! *p || p == arg_start)
4d3c2250 18206 dwarf2_macro_malformed_definition_complaint (body);
2e276125
JB
18207 else
18208 {
18209 /* Make sure argv has room for the new argument. */
18210 if (argc >= argv_size)
18211 {
18212 argv_size *= 2;
18213 argv = xrealloc (argv, argv_size * sizeof (*argv));
18214 }
18215
18216 argv[argc++] = copy_string (arg_start, p - arg_start);
18217 }
18218
18219 p = consume_improper_spaces (p, body);
18220
18221 /* Consume the comma, if present. */
18222 if (*p == ',')
18223 {
18224 p++;
18225
18226 p = consume_improper_spaces (p, body);
18227 }
18228 }
18229
18230 if (*p == ')')
18231 {
18232 p++;
18233
18234 if (*p == ' ')
18235 /* Perfectly formed definition, no complaints. */
18236 macro_define_function (file, line, name,
6e70227d 18237 argc, (const char **) argv,
2e276125
JB
18238 p + 1);
18239 else if (*p == '\0')
18240 {
18241 /* Complain, but do define it. */
4d3c2250 18242 dwarf2_macro_malformed_definition_complaint (body);
2e276125 18243 macro_define_function (file, line, name,
6e70227d 18244 argc, (const char **) argv,
2e276125
JB
18245 p);
18246 }
18247 else
18248 /* Just complain. */
4d3c2250 18249 dwarf2_macro_malformed_definition_complaint (body);
2e276125
JB
18250 }
18251 else
18252 /* Just complain. */
4d3c2250 18253 dwarf2_macro_malformed_definition_complaint (body);
2e276125
JB
18254
18255 xfree (name);
18256 {
18257 int i;
18258
18259 for (i = 0; i < argc; i++)
18260 xfree (argv[i]);
18261 }
18262 xfree (argv);
18263 }
18264 else
4d3c2250 18265 dwarf2_macro_malformed_definition_complaint (body);
2e276125
JB
18266}
18267
cf2c3c16
TT
18268/* Skip some bytes from BYTES according to the form given in FORM.
18269 Returns the new pointer. */
2e276125 18270
cf2c3c16 18271static gdb_byte *
f664829e 18272skip_form_bytes (bfd *abfd, gdb_byte *bytes, gdb_byte *buffer_end,
cf2c3c16
TT
18273 enum dwarf_form form,
18274 unsigned int offset_size,
18275 struct dwarf2_section_info *section)
2e276125 18276{
cf2c3c16 18277 unsigned int bytes_read;
2e276125 18278
cf2c3c16 18279 switch (form)
2e276125 18280 {
cf2c3c16
TT
18281 case DW_FORM_data1:
18282 case DW_FORM_flag:
18283 ++bytes;
18284 break;
18285
18286 case DW_FORM_data2:
18287 bytes += 2;
18288 break;
18289
18290 case DW_FORM_data4:
18291 bytes += 4;
18292 break;
18293
18294 case DW_FORM_data8:
18295 bytes += 8;
18296 break;
18297
18298 case DW_FORM_string:
18299 read_direct_string (abfd, bytes, &bytes_read);
18300 bytes += bytes_read;
18301 break;
18302
18303 case DW_FORM_sec_offset:
18304 case DW_FORM_strp:
36586728 18305 case DW_FORM_GNU_strp_alt:
cf2c3c16
TT
18306 bytes += offset_size;
18307 break;
18308
18309 case DW_FORM_block:
18310 bytes += read_unsigned_leb128 (abfd, bytes, &bytes_read);
18311 bytes += bytes_read;
18312 break;
18313
18314 case DW_FORM_block1:
18315 bytes += 1 + read_1_byte (abfd, bytes);
18316 break;
18317 case DW_FORM_block2:
18318 bytes += 2 + read_2_bytes (abfd, bytes);
18319 break;
18320 case DW_FORM_block4:
18321 bytes += 4 + read_4_bytes (abfd, bytes);
18322 break;
18323
18324 case DW_FORM_sdata:
18325 case DW_FORM_udata:
3019eac3
DE
18326 case DW_FORM_GNU_addr_index:
18327 case DW_FORM_GNU_str_index:
f664829e
DE
18328 bytes = (gdb_byte *) gdb_skip_leb128 (bytes, buffer_end);
18329 if (bytes == NULL)
18330 {
18331 dwarf2_section_buffer_overflow_complaint (section);
18332 return NULL;
18333 }
cf2c3c16
TT
18334 break;
18335
18336 default:
18337 {
18338 complain:
18339 complaint (&symfile_complaints,
18340 _("invalid form 0x%x in `%s'"),
18341 form,
18342 section->asection->name);
18343 return NULL;
18344 }
2e276125
JB
18345 }
18346
cf2c3c16
TT
18347 return bytes;
18348}
757a13d0 18349
cf2c3c16
TT
18350/* A helper for dwarf_decode_macros that handles skipping an unknown
18351 opcode. Returns an updated pointer to the macro data buffer; or,
18352 on error, issues a complaint and returns NULL. */
757a13d0 18353
cf2c3c16
TT
18354static gdb_byte *
18355skip_unknown_opcode (unsigned int opcode,
18356 gdb_byte **opcode_definitions,
f664829e 18357 gdb_byte *mac_ptr, gdb_byte *mac_end,
cf2c3c16
TT
18358 bfd *abfd,
18359 unsigned int offset_size,
18360 struct dwarf2_section_info *section)
18361{
18362 unsigned int bytes_read, i;
18363 unsigned long arg;
18364 gdb_byte *defn;
2e276125 18365
cf2c3c16 18366 if (opcode_definitions[opcode] == NULL)
2e276125 18367 {
cf2c3c16
TT
18368 complaint (&symfile_complaints,
18369 _("unrecognized DW_MACFINO opcode 0x%x"),
18370 opcode);
18371 return NULL;
18372 }
2e276125 18373
cf2c3c16
TT
18374 defn = opcode_definitions[opcode];
18375 arg = read_unsigned_leb128 (abfd, defn, &bytes_read);
18376 defn += bytes_read;
2e276125 18377
cf2c3c16
TT
18378 for (i = 0; i < arg; ++i)
18379 {
f664829e
DE
18380 mac_ptr = skip_form_bytes (abfd, mac_ptr, mac_end, defn[i], offset_size,
18381 section);
cf2c3c16
TT
18382 if (mac_ptr == NULL)
18383 {
18384 /* skip_form_bytes already issued the complaint. */
18385 return NULL;
18386 }
18387 }
757a13d0 18388
cf2c3c16
TT
18389 return mac_ptr;
18390}
757a13d0 18391
cf2c3c16
TT
18392/* A helper function which parses the header of a macro section.
18393 If the macro section is the extended (for now called "GNU") type,
18394 then this updates *OFFSET_SIZE. Returns a pointer to just after
18395 the header, or issues a complaint and returns NULL on error. */
757a13d0 18396
cf2c3c16
TT
18397static gdb_byte *
18398dwarf_parse_macro_header (gdb_byte **opcode_definitions,
18399 bfd *abfd,
18400 gdb_byte *mac_ptr,
18401 unsigned int *offset_size,
18402 int section_is_gnu)
18403{
18404 memset (opcode_definitions, 0, 256 * sizeof (gdb_byte *));
757a13d0 18405
cf2c3c16
TT
18406 if (section_is_gnu)
18407 {
18408 unsigned int version, flags;
757a13d0 18409
cf2c3c16
TT
18410 version = read_2_bytes (abfd, mac_ptr);
18411 if (version != 4)
18412 {
18413 complaint (&symfile_complaints,
18414 _("unrecognized version `%d' in .debug_macro section"),
18415 version);
18416 return NULL;
18417 }
18418 mac_ptr += 2;
757a13d0 18419
cf2c3c16
TT
18420 flags = read_1_byte (abfd, mac_ptr);
18421 ++mac_ptr;
18422 *offset_size = (flags & 1) ? 8 : 4;
757a13d0 18423
cf2c3c16
TT
18424 if ((flags & 2) != 0)
18425 /* We don't need the line table offset. */
18426 mac_ptr += *offset_size;
757a13d0 18427
cf2c3c16
TT
18428 /* Vendor opcode descriptions. */
18429 if ((flags & 4) != 0)
18430 {
18431 unsigned int i, count;
757a13d0 18432
cf2c3c16
TT
18433 count = read_1_byte (abfd, mac_ptr);
18434 ++mac_ptr;
18435 for (i = 0; i < count; ++i)
18436 {
18437 unsigned int opcode, bytes_read;
18438 unsigned long arg;
18439
18440 opcode = read_1_byte (abfd, mac_ptr);
18441 ++mac_ptr;
18442 opcode_definitions[opcode] = mac_ptr;
18443 arg = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
18444 mac_ptr += bytes_read;
18445 mac_ptr += arg;
18446 }
757a13d0 18447 }
cf2c3c16 18448 }
757a13d0 18449
cf2c3c16
TT
18450 return mac_ptr;
18451}
757a13d0 18452
cf2c3c16 18453/* A helper for dwarf_decode_macros that handles the GNU extensions,
8fc3fc34 18454 including DW_MACRO_GNU_transparent_include. */
cf2c3c16
TT
18455
18456static void
18457dwarf_decode_macro_bytes (bfd *abfd, gdb_byte *mac_ptr, gdb_byte *mac_end,
18458 struct macro_source_file *current_file,
15d034d0 18459 struct line_header *lh, const char *comp_dir,
cf2c3c16 18460 struct dwarf2_section_info *section,
36586728 18461 int section_is_gnu, int section_is_dwz,
cf2c3c16 18462 unsigned int offset_size,
8fc3fc34
TT
18463 struct objfile *objfile,
18464 htab_t include_hash)
cf2c3c16
TT
18465{
18466 enum dwarf_macro_record_type macinfo_type;
18467 int at_commandline;
18468 gdb_byte *opcode_definitions[256];
757a13d0 18469
cf2c3c16
TT
18470 mac_ptr = dwarf_parse_macro_header (opcode_definitions, abfd, mac_ptr,
18471 &offset_size, section_is_gnu);
18472 if (mac_ptr == NULL)
18473 {
18474 /* We already issued a complaint. */
18475 return;
18476 }
757a13d0
JK
18477
18478 /* Determines if GDB is still before first DW_MACINFO_start_file. If true
18479 GDB is still reading the definitions from command line. First
18480 DW_MACINFO_start_file will need to be ignored as it was already executed
18481 to create CURRENT_FILE for the main source holding also the command line
18482 definitions. On first met DW_MACINFO_start_file this flag is reset to
18483 normally execute all the remaining DW_MACINFO_start_file macinfos. */
18484
18485 at_commandline = 1;
18486
18487 do
18488 {
18489 /* Do we at least have room for a macinfo type byte? */
18490 if (mac_ptr >= mac_end)
18491 {
f664829e 18492 dwarf2_section_buffer_overflow_complaint (section);
757a13d0
JK
18493 break;
18494 }
18495
18496 macinfo_type = read_1_byte (abfd, mac_ptr);
18497 mac_ptr++;
18498
cf2c3c16
TT
18499 /* Note that we rely on the fact that the corresponding GNU and
18500 DWARF constants are the same. */
757a13d0
JK
18501 switch (macinfo_type)
18502 {
18503 /* A zero macinfo type indicates the end of the macro
18504 information. */
18505 case 0:
18506 break;
2e276125 18507
cf2c3c16
TT
18508 case DW_MACRO_GNU_define:
18509 case DW_MACRO_GNU_undef:
18510 case DW_MACRO_GNU_define_indirect:
18511 case DW_MACRO_GNU_undef_indirect:
36586728
TT
18512 case DW_MACRO_GNU_define_indirect_alt:
18513 case DW_MACRO_GNU_undef_indirect_alt:
2e276125 18514 {
891d2f0b 18515 unsigned int bytes_read;
2e276125
JB
18516 int line;
18517 char *body;
cf2c3c16 18518 int is_define;
2e276125 18519
cf2c3c16
TT
18520 line = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
18521 mac_ptr += bytes_read;
18522
18523 if (macinfo_type == DW_MACRO_GNU_define
18524 || macinfo_type == DW_MACRO_GNU_undef)
18525 {
18526 body = read_direct_string (abfd, mac_ptr, &bytes_read);
18527 mac_ptr += bytes_read;
18528 }
18529 else
18530 {
18531 LONGEST str_offset;
18532
18533 str_offset = read_offset_1 (abfd, mac_ptr, offset_size);
18534 mac_ptr += offset_size;
2e276125 18535
36586728 18536 if (macinfo_type == DW_MACRO_GNU_define_indirect_alt
f7a35f02
TT
18537 || macinfo_type == DW_MACRO_GNU_undef_indirect_alt
18538 || section_is_dwz)
36586728
TT
18539 {
18540 struct dwz_file *dwz = dwarf2_get_dwz_file ();
18541
18542 body = read_indirect_string_from_dwz (dwz, str_offset);
18543 }
18544 else
18545 body = read_indirect_string_at_offset (abfd, str_offset);
cf2c3c16
TT
18546 }
18547
18548 is_define = (macinfo_type == DW_MACRO_GNU_define
36586728
TT
18549 || macinfo_type == DW_MACRO_GNU_define_indirect
18550 || macinfo_type == DW_MACRO_GNU_define_indirect_alt);
2e276125 18551 if (! current_file)
757a13d0
JK
18552 {
18553 /* DWARF violation as no main source is present. */
18554 complaint (&symfile_complaints,
18555 _("debug info with no main source gives macro %s "
18556 "on line %d: %s"),
cf2c3c16
TT
18557 is_define ? _("definition") : _("undefinition"),
18558 line, body);
757a13d0
JK
18559 break;
18560 }
3e43a32a
MS
18561 if ((line == 0 && !at_commandline)
18562 || (line != 0 && at_commandline))
4d3c2250 18563 complaint (&symfile_complaints,
757a13d0
JK
18564 _("debug info gives %s macro %s with %s line %d: %s"),
18565 at_commandline ? _("command-line") : _("in-file"),
cf2c3c16 18566 is_define ? _("definition") : _("undefinition"),
757a13d0
JK
18567 line == 0 ? _("zero") : _("non-zero"), line, body);
18568
cf2c3c16 18569 if (is_define)
757a13d0 18570 parse_macro_definition (current_file, line, body);
cf2c3c16
TT
18571 else
18572 {
18573 gdb_assert (macinfo_type == DW_MACRO_GNU_undef
36586728
TT
18574 || macinfo_type == DW_MACRO_GNU_undef_indirect
18575 || macinfo_type == DW_MACRO_GNU_undef_indirect_alt);
cf2c3c16
TT
18576 macro_undef (current_file, line, body);
18577 }
2e276125
JB
18578 }
18579 break;
18580
cf2c3c16 18581 case DW_MACRO_GNU_start_file:
2e276125 18582 {
891d2f0b 18583 unsigned int bytes_read;
2e276125
JB
18584 int line, file;
18585
18586 line = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
18587 mac_ptr += bytes_read;
18588 file = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
18589 mac_ptr += bytes_read;
18590
3e43a32a
MS
18591 if ((line == 0 && !at_commandline)
18592 || (line != 0 && at_commandline))
757a13d0
JK
18593 complaint (&symfile_complaints,
18594 _("debug info gives source %d included "
18595 "from %s at %s line %d"),
18596 file, at_commandline ? _("command-line") : _("file"),
18597 line == 0 ? _("zero") : _("non-zero"), line);
18598
18599 if (at_commandline)
18600 {
cf2c3c16
TT
18601 /* This DW_MACRO_GNU_start_file was executed in the
18602 pass one. */
757a13d0
JK
18603 at_commandline = 0;
18604 }
18605 else
18606 current_file = macro_start_file (file, line,
18607 current_file, comp_dir,
cf2c3c16 18608 lh, objfile);
2e276125
JB
18609 }
18610 break;
18611
cf2c3c16 18612 case DW_MACRO_GNU_end_file:
2e276125 18613 if (! current_file)
4d3c2250 18614 complaint (&symfile_complaints,
3e43a32a
MS
18615 _("macro debug info has an unmatched "
18616 "`close_file' directive"));
2e276125
JB
18617 else
18618 {
18619 current_file = current_file->included_by;
18620 if (! current_file)
18621 {
cf2c3c16 18622 enum dwarf_macro_record_type next_type;
2e276125
JB
18623
18624 /* GCC circa March 2002 doesn't produce the zero
18625 type byte marking the end of the compilation
18626 unit. Complain if it's not there, but exit no
18627 matter what. */
18628
18629 /* Do we at least have room for a macinfo type byte? */
18630 if (mac_ptr >= mac_end)
18631 {
f664829e 18632 dwarf2_section_buffer_overflow_complaint (section);
2e276125
JB
18633 return;
18634 }
18635
18636 /* We don't increment mac_ptr here, so this is just
18637 a look-ahead. */
18638 next_type = read_1_byte (abfd, mac_ptr);
18639 if (next_type != 0)
4d3c2250 18640 complaint (&symfile_complaints,
3e43a32a
MS
18641 _("no terminating 0-type entry for "
18642 "macros in `.debug_macinfo' section"));
2e276125
JB
18643
18644 return;
18645 }
18646 }
18647 break;
18648
cf2c3c16 18649 case DW_MACRO_GNU_transparent_include:
36586728 18650 case DW_MACRO_GNU_transparent_include_alt:
cf2c3c16
TT
18651 {
18652 LONGEST offset;
8fc3fc34 18653 void **slot;
a036ba48
TT
18654 bfd *include_bfd = abfd;
18655 struct dwarf2_section_info *include_section = section;
18656 struct dwarf2_section_info alt_section;
18657 gdb_byte *include_mac_end = mac_end;
18658 int is_dwz = section_is_dwz;
18659 gdb_byte *new_mac_ptr;
cf2c3c16
TT
18660
18661 offset = read_offset_1 (abfd, mac_ptr, offset_size);
18662 mac_ptr += offset_size;
18663
a036ba48
TT
18664 if (macinfo_type == DW_MACRO_GNU_transparent_include_alt)
18665 {
18666 struct dwz_file *dwz = dwarf2_get_dwz_file ();
18667
18668 dwarf2_read_section (dwarf2_per_objfile->objfile,
18669 &dwz->macro);
18670
18671 include_bfd = dwz->macro.asection->owner;
18672 include_section = &dwz->macro;
18673 include_mac_end = dwz->macro.buffer + dwz->macro.size;
18674 is_dwz = 1;
18675 }
18676
18677 new_mac_ptr = include_section->buffer + offset;
18678 slot = htab_find_slot (include_hash, new_mac_ptr, INSERT);
18679
8fc3fc34
TT
18680 if (*slot != NULL)
18681 {
18682 /* This has actually happened; see
18683 http://sourceware.org/bugzilla/show_bug.cgi?id=13568. */
18684 complaint (&symfile_complaints,
18685 _("recursive DW_MACRO_GNU_transparent_include in "
18686 ".debug_macro section"));
18687 }
18688 else
18689 {
a036ba48 18690 *slot = new_mac_ptr;
36586728 18691
a036ba48 18692 dwarf_decode_macro_bytes (include_bfd, new_mac_ptr,
36586728 18693 include_mac_end, current_file,
8fc3fc34 18694 lh, comp_dir,
36586728 18695 section, section_is_gnu, is_dwz,
8fc3fc34
TT
18696 offset_size, objfile, include_hash);
18697
a036ba48 18698 htab_remove_elt (include_hash, new_mac_ptr);
8fc3fc34 18699 }
cf2c3c16
TT
18700 }
18701 break;
18702
2e276125 18703 case DW_MACINFO_vendor_ext:
cf2c3c16
TT
18704 if (!section_is_gnu)
18705 {
18706 unsigned int bytes_read;
18707 int constant;
2e276125 18708
cf2c3c16
TT
18709 constant = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
18710 mac_ptr += bytes_read;
18711 read_direct_string (abfd, mac_ptr, &bytes_read);
18712 mac_ptr += bytes_read;
2e276125 18713
cf2c3c16
TT
18714 /* We don't recognize any vendor extensions. */
18715 break;
18716 }
18717 /* FALLTHROUGH */
18718
18719 default:
18720 mac_ptr = skip_unknown_opcode (macinfo_type, opcode_definitions,
f664829e 18721 mac_ptr, mac_end, abfd, offset_size,
cf2c3c16
TT
18722 section);
18723 if (mac_ptr == NULL)
18724 return;
18725 break;
2e276125 18726 }
757a13d0 18727 } while (macinfo_type != 0);
2e276125 18728}
8e19ed76 18729
cf2c3c16 18730static void
09262596 18731dwarf_decode_macros (struct dwarf2_cu *cu, unsigned int offset,
15d034d0 18732 const char *comp_dir, int section_is_gnu)
cf2c3c16 18733{
bb5ed363 18734 struct objfile *objfile = dwarf2_per_objfile->objfile;
09262596
DE
18735 struct line_header *lh = cu->line_header;
18736 bfd *abfd;
cf2c3c16
TT
18737 gdb_byte *mac_ptr, *mac_end;
18738 struct macro_source_file *current_file = 0;
18739 enum dwarf_macro_record_type macinfo_type;
18740 unsigned int offset_size = cu->header.offset_size;
18741 gdb_byte *opcode_definitions[256];
8fc3fc34
TT
18742 struct cleanup *cleanup;
18743 htab_t include_hash;
18744 void **slot;
09262596
DE
18745 struct dwarf2_section_info *section;
18746 const char *section_name;
18747
18748 if (cu->dwo_unit != NULL)
18749 {
18750 if (section_is_gnu)
18751 {
18752 section = &cu->dwo_unit->dwo_file->sections.macro;
18753 section_name = ".debug_macro.dwo";
18754 }
18755 else
18756 {
18757 section = &cu->dwo_unit->dwo_file->sections.macinfo;
18758 section_name = ".debug_macinfo.dwo";
18759 }
18760 }
18761 else
18762 {
18763 if (section_is_gnu)
18764 {
18765 section = &dwarf2_per_objfile->macro;
18766 section_name = ".debug_macro";
18767 }
18768 else
18769 {
18770 section = &dwarf2_per_objfile->macinfo;
18771 section_name = ".debug_macinfo";
18772 }
18773 }
cf2c3c16 18774
bb5ed363 18775 dwarf2_read_section (objfile, section);
cf2c3c16
TT
18776 if (section->buffer == NULL)
18777 {
fceca515 18778 complaint (&symfile_complaints, _("missing %s section"), section_name);
cf2c3c16
TT
18779 return;
18780 }
09262596 18781 abfd = section->asection->owner;
cf2c3c16
TT
18782
18783 /* First pass: Find the name of the base filename.
18784 This filename is needed in order to process all macros whose definition
18785 (or undefinition) comes from the command line. These macros are defined
18786 before the first DW_MACINFO_start_file entry, and yet still need to be
18787 associated to the base file.
18788
18789 To determine the base file name, we scan the macro definitions until we
18790 reach the first DW_MACINFO_start_file entry. We then initialize
18791 CURRENT_FILE accordingly so that any macro definition found before the
18792 first DW_MACINFO_start_file can still be associated to the base file. */
18793
18794 mac_ptr = section->buffer + offset;
18795 mac_end = section->buffer + section->size;
18796
18797 mac_ptr = dwarf_parse_macro_header (opcode_definitions, abfd, mac_ptr,
18798 &offset_size, section_is_gnu);
18799 if (mac_ptr == NULL)
18800 {
18801 /* We already issued a complaint. */
18802 return;
18803 }
18804
18805 do
18806 {
18807 /* Do we at least have room for a macinfo type byte? */
18808 if (mac_ptr >= mac_end)
18809 {
18810 /* Complaint is printed during the second pass as GDB will probably
18811 stop the first pass earlier upon finding
18812 DW_MACINFO_start_file. */
18813 break;
18814 }
18815
18816 macinfo_type = read_1_byte (abfd, mac_ptr);
18817 mac_ptr++;
18818
18819 /* Note that we rely on the fact that the corresponding GNU and
18820 DWARF constants are the same. */
18821 switch (macinfo_type)
18822 {
18823 /* A zero macinfo type indicates the end of the macro
18824 information. */
18825 case 0:
18826 break;
18827
18828 case DW_MACRO_GNU_define:
18829 case DW_MACRO_GNU_undef:
18830 /* Only skip the data by MAC_PTR. */
18831 {
18832 unsigned int bytes_read;
18833
18834 read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
18835 mac_ptr += bytes_read;
18836 read_direct_string (abfd, mac_ptr, &bytes_read);
18837 mac_ptr += bytes_read;
18838 }
18839 break;
18840
18841 case DW_MACRO_GNU_start_file:
18842 {
18843 unsigned int bytes_read;
18844 int line, file;
18845
18846 line = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
18847 mac_ptr += bytes_read;
18848 file = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
18849 mac_ptr += bytes_read;
18850
18851 current_file = macro_start_file (file, line, current_file,
bb5ed363 18852 comp_dir, lh, objfile);
cf2c3c16
TT
18853 }
18854 break;
18855
18856 case DW_MACRO_GNU_end_file:
18857 /* No data to skip by MAC_PTR. */
18858 break;
18859
18860 case DW_MACRO_GNU_define_indirect:
18861 case DW_MACRO_GNU_undef_indirect:
f7a35f02
TT
18862 case DW_MACRO_GNU_define_indirect_alt:
18863 case DW_MACRO_GNU_undef_indirect_alt:
cf2c3c16
TT
18864 {
18865 unsigned int bytes_read;
18866
18867 read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
18868 mac_ptr += bytes_read;
18869 mac_ptr += offset_size;
18870 }
18871 break;
18872
18873 case DW_MACRO_GNU_transparent_include:
f7a35f02 18874 case DW_MACRO_GNU_transparent_include_alt:
cf2c3c16
TT
18875 /* Note that, according to the spec, a transparent include
18876 chain cannot call DW_MACRO_GNU_start_file. So, we can just
18877 skip this opcode. */
18878 mac_ptr += offset_size;
18879 break;
18880
18881 case DW_MACINFO_vendor_ext:
18882 /* Only skip the data by MAC_PTR. */
18883 if (!section_is_gnu)
18884 {
18885 unsigned int bytes_read;
18886
18887 read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
18888 mac_ptr += bytes_read;
18889 read_direct_string (abfd, mac_ptr, &bytes_read);
18890 mac_ptr += bytes_read;
18891 }
18892 /* FALLTHROUGH */
18893
18894 default:
18895 mac_ptr = skip_unknown_opcode (macinfo_type, opcode_definitions,
f664829e 18896 mac_ptr, mac_end, abfd, offset_size,
cf2c3c16
TT
18897 section);
18898 if (mac_ptr == NULL)
18899 return;
18900 break;
18901 }
18902 } while (macinfo_type != 0 && current_file == NULL);
18903
18904 /* Second pass: Process all entries.
18905
18906 Use the AT_COMMAND_LINE flag to determine whether we are still processing
18907 command-line macro definitions/undefinitions. This flag is unset when we
18908 reach the first DW_MACINFO_start_file entry. */
18909
8fc3fc34
TT
18910 include_hash = htab_create_alloc (1, htab_hash_pointer, htab_eq_pointer,
18911 NULL, xcalloc, xfree);
18912 cleanup = make_cleanup_htab_delete (include_hash);
18913 mac_ptr = section->buffer + offset;
18914 slot = htab_find_slot (include_hash, mac_ptr, INSERT);
18915 *slot = mac_ptr;
18916 dwarf_decode_macro_bytes (abfd, mac_ptr, mac_end,
36586728
TT
18917 current_file, lh, comp_dir, section,
18918 section_is_gnu, 0,
8fc3fc34
TT
18919 offset_size, objfile, include_hash);
18920 do_cleanups (cleanup);
cf2c3c16
TT
18921}
18922
8e19ed76 18923/* Check if the attribute's form is a DW_FORM_block*
0963b4bd 18924 if so return true else false. */
380bca97 18925
8e19ed76
PS
18926static int
18927attr_form_is_block (struct attribute *attr)
18928{
18929 return (attr == NULL ? 0 :
18930 attr->form == DW_FORM_block1
18931 || attr->form == DW_FORM_block2
18932 || attr->form == DW_FORM_block4
2dc7f7b3
TT
18933 || attr->form == DW_FORM_block
18934 || attr->form == DW_FORM_exprloc);
8e19ed76 18935}
4c2df51b 18936
c6a0999f
JB
18937/* Return non-zero if ATTR's value is a section offset --- classes
18938 lineptr, loclistptr, macptr or rangelistptr --- or zero, otherwise.
18939 You may use DW_UNSND (attr) to retrieve such offsets.
18940
18941 Section 7.5.4, "Attribute Encodings", explains that no attribute
18942 may have a value that belongs to more than one of these classes; it
18943 would be ambiguous if we did, because we use the same forms for all
18944 of them. */
380bca97 18945
3690dd37
JB
18946static int
18947attr_form_is_section_offset (struct attribute *attr)
18948{
18949 return (attr->form == DW_FORM_data4
2dc7f7b3
TT
18950 || attr->form == DW_FORM_data8
18951 || attr->form == DW_FORM_sec_offset);
3690dd37
JB
18952}
18953
3690dd37
JB
18954/* Return non-zero if ATTR's value falls in the 'constant' class, or
18955 zero otherwise. When this function returns true, you can apply
18956 dwarf2_get_attr_constant_value to it.
18957
18958 However, note that for some attributes you must check
18959 attr_form_is_section_offset before using this test. DW_FORM_data4
18960 and DW_FORM_data8 are members of both the constant class, and of
18961 the classes that contain offsets into other debug sections
18962 (lineptr, loclistptr, macptr or rangelistptr). The DWARF spec says
18963 that, if an attribute's can be either a constant or one of the
18964 section offset classes, DW_FORM_data4 and DW_FORM_data8 should be
18965 taken as section offsets, not constants. */
380bca97 18966
3690dd37
JB
18967static int
18968attr_form_is_constant (struct attribute *attr)
18969{
18970 switch (attr->form)
18971 {
18972 case DW_FORM_sdata:
18973 case DW_FORM_udata:
18974 case DW_FORM_data1:
18975 case DW_FORM_data2:
18976 case DW_FORM_data4:
18977 case DW_FORM_data8:
18978 return 1;
18979 default:
18980 return 0;
18981 }
18982}
18983
3019eac3
DE
18984/* Return the .debug_loc section to use for CU.
18985 For DWO files use .debug_loc.dwo. */
18986
18987static struct dwarf2_section_info *
18988cu_debug_loc_section (struct dwarf2_cu *cu)
18989{
18990 if (cu->dwo_unit)
18991 return &cu->dwo_unit->dwo_file->sections.loc;
18992 return &dwarf2_per_objfile->loc;
18993}
18994
8cf6f0b1
TT
18995/* A helper function that fills in a dwarf2_loclist_baton. */
18996
18997static void
18998fill_in_loclist_baton (struct dwarf2_cu *cu,
18999 struct dwarf2_loclist_baton *baton,
19000 struct attribute *attr)
19001{
3019eac3
DE
19002 struct dwarf2_section_info *section = cu_debug_loc_section (cu);
19003
19004 dwarf2_read_section (dwarf2_per_objfile->objfile, section);
8cf6f0b1
TT
19005
19006 baton->per_cu = cu->per_cu;
19007 gdb_assert (baton->per_cu);
19008 /* We don't know how long the location list is, but make sure we
19009 don't run off the edge of the section. */
3019eac3
DE
19010 baton->size = section->size - DW_UNSND (attr);
19011 baton->data = section->buffer + DW_UNSND (attr);
8cf6f0b1 19012 baton->base_address = cu->base_address;
f664829e 19013 baton->from_dwo = cu->dwo_unit != NULL;
8cf6f0b1
TT
19014}
19015
4c2df51b
DJ
19016static void
19017dwarf2_symbol_mark_computed (struct attribute *attr, struct symbol *sym,
e7c27a73 19018 struct dwarf2_cu *cu)
4c2df51b 19019{
bb5ed363 19020 struct objfile *objfile = dwarf2_per_objfile->objfile;
3019eac3 19021 struct dwarf2_section_info *section = cu_debug_loc_section (cu);
bb5ed363 19022
3690dd37 19023 if (attr_form_is_section_offset (attr)
3019eac3 19024 /* .debug_loc{,.dwo} may not exist at all, or the offset may be outside
99bcc461
DJ
19025 the section. If so, fall through to the complaint in the
19026 other branch. */
3019eac3 19027 && DW_UNSND (attr) < dwarf2_section_size (objfile, section))
4c2df51b 19028 {
0d53c4c4 19029 struct dwarf2_loclist_baton *baton;
4c2df51b 19030
bb5ed363 19031 baton = obstack_alloc (&objfile->objfile_obstack,
0d53c4c4 19032 sizeof (struct dwarf2_loclist_baton));
4c2df51b 19033
8cf6f0b1 19034 fill_in_loclist_baton (cu, baton, attr);
be391dca 19035
d00adf39 19036 if (cu->base_known == 0)
0d53c4c4 19037 complaint (&symfile_complaints,
3e43a32a
MS
19038 _("Location list used without "
19039 "specifying the CU base address."));
4c2df51b 19040
768a979c 19041 SYMBOL_COMPUTED_OPS (sym) = &dwarf2_loclist_funcs;
0d53c4c4
DJ
19042 SYMBOL_LOCATION_BATON (sym) = baton;
19043 }
19044 else
19045 {
19046 struct dwarf2_locexpr_baton *baton;
19047
bb5ed363 19048 baton = obstack_alloc (&objfile->objfile_obstack,
0d53c4c4 19049 sizeof (struct dwarf2_locexpr_baton));
ae0d2f24
UW
19050 baton->per_cu = cu->per_cu;
19051 gdb_assert (baton->per_cu);
0d53c4c4
DJ
19052
19053 if (attr_form_is_block (attr))
19054 {
19055 /* Note that we're just copying the block's data pointer
19056 here, not the actual data. We're still pointing into the
6502dd73
DJ
19057 info_buffer for SYM's objfile; right now we never release
19058 that buffer, but when we do clean up properly this may
19059 need to change. */
0d53c4c4
DJ
19060 baton->size = DW_BLOCK (attr)->size;
19061 baton->data = DW_BLOCK (attr)->data;
19062 }
19063 else
19064 {
19065 dwarf2_invalid_attrib_class_complaint ("location description",
19066 SYMBOL_NATURAL_NAME (sym));
19067 baton->size = 0;
0d53c4c4 19068 }
6e70227d 19069
768a979c 19070 SYMBOL_COMPUTED_OPS (sym) = &dwarf2_locexpr_funcs;
0d53c4c4
DJ
19071 SYMBOL_LOCATION_BATON (sym) = baton;
19072 }
4c2df51b 19073}
6502dd73 19074
9aa1f1e3
TT
19075/* Return the OBJFILE associated with the compilation unit CU. If CU
19076 came from a separate debuginfo file, then the master objfile is
19077 returned. */
ae0d2f24
UW
19078
19079struct objfile *
19080dwarf2_per_cu_objfile (struct dwarf2_per_cu_data *per_cu)
19081{
9291a0cd 19082 struct objfile *objfile = per_cu->objfile;
ae0d2f24
UW
19083
19084 /* Return the master objfile, so that we can report and look up the
19085 correct file containing this variable. */
19086 if (objfile->separate_debug_objfile_backlink)
19087 objfile = objfile->separate_debug_objfile_backlink;
19088
19089 return objfile;
19090}
19091
96408a79
SA
19092/* Return comp_unit_head for PER_CU, either already available in PER_CU->CU
19093 (CU_HEADERP is unused in such case) or prepare a temporary copy at
19094 CU_HEADERP first. */
19095
19096static const struct comp_unit_head *
19097per_cu_header_read_in (struct comp_unit_head *cu_headerp,
19098 struct dwarf2_per_cu_data *per_cu)
19099{
96408a79
SA
19100 gdb_byte *info_ptr;
19101
19102 if (per_cu->cu)
19103 return &per_cu->cu->header;
19104
0bc3a05c 19105 info_ptr = per_cu->info_or_types_section->buffer + per_cu->offset.sect_off;
96408a79
SA
19106
19107 memset (cu_headerp, 0, sizeof (*cu_headerp));
0bc3a05c 19108 read_comp_unit_head (cu_headerp, info_ptr, per_cu->objfile->obfd);
96408a79
SA
19109
19110 return cu_headerp;
19111}
19112
ae0d2f24
UW
19113/* Return the address size given in the compilation unit header for CU. */
19114
98714339 19115int
ae0d2f24
UW
19116dwarf2_per_cu_addr_size (struct dwarf2_per_cu_data *per_cu)
19117{
96408a79
SA
19118 struct comp_unit_head cu_header_local;
19119 const struct comp_unit_head *cu_headerp;
c471e790 19120
96408a79
SA
19121 cu_headerp = per_cu_header_read_in (&cu_header_local, per_cu);
19122
19123 return cu_headerp->addr_size;
ae0d2f24
UW
19124}
19125
9eae7c52
TT
19126/* Return the offset size given in the compilation unit header for CU. */
19127
19128int
19129dwarf2_per_cu_offset_size (struct dwarf2_per_cu_data *per_cu)
19130{
96408a79
SA
19131 struct comp_unit_head cu_header_local;
19132 const struct comp_unit_head *cu_headerp;
9c6c53f7 19133
96408a79
SA
19134 cu_headerp = per_cu_header_read_in (&cu_header_local, per_cu);
19135
19136 return cu_headerp->offset_size;
19137}
19138
19139/* See its dwarf2loc.h declaration. */
19140
19141int
19142dwarf2_per_cu_ref_addr_size (struct dwarf2_per_cu_data *per_cu)
19143{
19144 struct comp_unit_head cu_header_local;
19145 const struct comp_unit_head *cu_headerp;
19146
19147 cu_headerp = per_cu_header_read_in (&cu_header_local, per_cu);
19148
19149 if (cu_headerp->version == 2)
19150 return cu_headerp->addr_size;
19151 else
19152 return cu_headerp->offset_size;
181cebd4
JK
19153}
19154
9aa1f1e3
TT
19155/* Return the text offset of the CU. The returned offset comes from
19156 this CU's objfile. If this objfile came from a separate debuginfo
19157 file, then the offset may be different from the corresponding
19158 offset in the parent objfile. */
19159
19160CORE_ADDR
19161dwarf2_per_cu_text_offset (struct dwarf2_per_cu_data *per_cu)
19162{
bb3fa9d0 19163 struct objfile *objfile = per_cu->objfile;
9aa1f1e3
TT
19164
19165 return ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
19166}
19167
348e048f
DE
19168/* Locate the .debug_info compilation unit from CU's objfile which contains
19169 the DIE at OFFSET. Raises an error on failure. */
ae038cb0
DJ
19170
19171static struct dwarf2_per_cu_data *
b64f50a1 19172dwarf2_find_containing_comp_unit (sect_offset offset,
36586728 19173 unsigned int offset_in_dwz,
ae038cb0
DJ
19174 struct objfile *objfile)
19175{
19176 struct dwarf2_per_cu_data *this_cu;
19177 int low, high;
36586728 19178 const sect_offset *cu_off;
ae038cb0 19179
ae038cb0
DJ
19180 low = 0;
19181 high = dwarf2_per_objfile->n_comp_units - 1;
19182 while (high > low)
19183 {
36586728 19184 struct dwarf2_per_cu_data *mid_cu;
ae038cb0 19185 int mid = low + (high - low) / 2;
9a619af0 19186
36586728
TT
19187 mid_cu = dwarf2_per_objfile->all_comp_units[mid];
19188 cu_off = &mid_cu->offset;
19189 if (mid_cu->is_dwz > offset_in_dwz
19190 || (mid_cu->is_dwz == offset_in_dwz
19191 && cu_off->sect_off >= offset.sect_off))
ae038cb0
DJ
19192 high = mid;
19193 else
19194 low = mid + 1;
19195 }
19196 gdb_assert (low == high);
36586728
TT
19197 this_cu = dwarf2_per_objfile->all_comp_units[low];
19198 cu_off = &this_cu->offset;
19199 if (this_cu->is_dwz != offset_in_dwz || cu_off->sect_off > offset.sect_off)
ae038cb0 19200 {
36586728 19201 if (low == 0 || this_cu->is_dwz != offset_in_dwz)
8a3fe4f8
AC
19202 error (_("Dwarf Error: could not find partial DIE containing "
19203 "offset 0x%lx [in module %s]"),
b64f50a1 19204 (long) offset.sect_off, bfd_get_filename (objfile->obfd));
10b3939b 19205
b64f50a1
JK
19206 gdb_assert (dwarf2_per_objfile->all_comp_units[low-1]->offset.sect_off
19207 <= offset.sect_off);
ae038cb0
DJ
19208 return dwarf2_per_objfile->all_comp_units[low-1];
19209 }
19210 else
19211 {
19212 this_cu = dwarf2_per_objfile->all_comp_units[low];
19213 if (low == dwarf2_per_objfile->n_comp_units - 1
b64f50a1
JK
19214 && offset.sect_off >= this_cu->offset.sect_off + this_cu->length)
19215 error (_("invalid dwarf2 offset %u"), offset.sect_off);
19216 gdb_assert (offset.sect_off < this_cu->offset.sect_off + this_cu->length);
ae038cb0
DJ
19217 return this_cu;
19218 }
19219}
19220
23745b47 19221/* Initialize dwarf2_cu CU, owned by PER_CU. */
93311388 19222
9816fde3 19223static void
23745b47 19224init_one_comp_unit (struct dwarf2_cu *cu, struct dwarf2_per_cu_data *per_cu)
93311388 19225{
9816fde3 19226 memset (cu, 0, sizeof (*cu));
23745b47
DE
19227 per_cu->cu = cu;
19228 cu->per_cu = per_cu;
19229 cu->objfile = per_cu->objfile;
93311388 19230 obstack_init (&cu->comp_unit_obstack);
9816fde3
JK
19231}
19232
19233/* Initialize basic fields of dwarf_cu CU according to DIE COMP_UNIT_DIE. */
19234
19235static void
95554aad
TT
19236prepare_one_comp_unit (struct dwarf2_cu *cu, struct die_info *comp_unit_die,
19237 enum language pretend_language)
9816fde3
JK
19238{
19239 struct attribute *attr;
19240
19241 /* Set the language we're debugging. */
19242 attr = dwarf2_attr (comp_unit_die, DW_AT_language, cu);
19243 if (attr)
19244 set_cu_language (DW_UNSND (attr), cu);
19245 else
9cded63f 19246 {
95554aad 19247 cu->language = pretend_language;
9cded63f
TT
19248 cu->language_defn = language_def (cu->language);
19249 }
dee91e82
DE
19250
19251 attr = dwarf2_attr (comp_unit_die, DW_AT_producer, cu);
19252 if (attr)
19253 cu->producer = DW_STRING (attr);
93311388
DE
19254}
19255
ae038cb0
DJ
19256/* Release one cached compilation unit, CU. We unlink it from the tree
19257 of compilation units, but we don't remove it from the read_in_chain;
93311388
DE
19258 the caller is responsible for that.
19259 NOTE: DATA is a void * because this function is also used as a
19260 cleanup routine. */
ae038cb0
DJ
19261
19262static void
68dc6402 19263free_heap_comp_unit (void *data)
ae038cb0
DJ
19264{
19265 struct dwarf2_cu *cu = data;
19266
23745b47
DE
19267 gdb_assert (cu->per_cu != NULL);
19268 cu->per_cu->cu = NULL;
ae038cb0
DJ
19269 cu->per_cu = NULL;
19270
19271 obstack_free (&cu->comp_unit_obstack, NULL);
19272
19273 xfree (cu);
19274}
19275
72bf9492 19276/* This cleanup function is passed the address of a dwarf2_cu on the stack
ae038cb0 19277 when we're finished with it. We can't free the pointer itself, but be
dee91e82 19278 sure to unlink it from the cache. Also release any associated storage. */
72bf9492
DJ
19279
19280static void
19281free_stack_comp_unit (void *data)
19282{
19283 struct dwarf2_cu *cu = data;
19284
23745b47
DE
19285 gdb_assert (cu->per_cu != NULL);
19286 cu->per_cu->cu = NULL;
19287 cu->per_cu = NULL;
19288
72bf9492
DJ
19289 obstack_free (&cu->comp_unit_obstack, NULL);
19290 cu->partial_dies = NULL;
ae038cb0
DJ
19291}
19292
19293/* Free all cached compilation units. */
19294
19295static void
19296free_cached_comp_units (void *data)
19297{
19298 struct dwarf2_per_cu_data *per_cu, **last_chain;
19299
19300 per_cu = dwarf2_per_objfile->read_in_chain;
19301 last_chain = &dwarf2_per_objfile->read_in_chain;
19302 while (per_cu != NULL)
19303 {
19304 struct dwarf2_per_cu_data *next_cu;
19305
19306 next_cu = per_cu->cu->read_in_chain;
19307
68dc6402 19308 free_heap_comp_unit (per_cu->cu);
ae038cb0
DJ
19309 *last_chain = next_cu;
19310
19311 per_cu = next_cu;
19312 }
19313}
19314
19315/* Increase the age counter on each cached compilation unit, and free
19316 any that are too old. */
19317
19318static void
19319age_cached_comp_units (void)
19320{
19321 struct dwarf2_per_cu_data *per_cu, **last_chain;
19322
19323 dwarf2_clear_marks (dwarf2_per_objfile->read_in_chain);
19324 per_cu = dwarf2_per_objfile->read_in_chain;
19325 while (per_cu != NULL)
19326 {
19327 per_cu->cu->last_used ++;
19328 if (per_cu->cu->last_used <= dwarf2_max_cache_age)
19329 dwarf2_mark (per_cu->cu);
19330 per_cu = per_cu->cu->read_in_chain;
19331 }
19332
19333 per_cu = dwarf2_per_objfile->read_in_chain;
19334 last_chain = &dwarf2_per_objfile->read_in_chain;
19335 while (per_cu != NULL)
19336 {
19337 struct dwarf2_per_cu_data *next_cu;
19338
19339 next_cu = per_cu->cu->read_in_chain;
19340
19341 if (!per_cu->cu->mark)
19342 {
68dc6402 19343 free_heap_comp_unit (per_cu->cu);
ae038cb0
DJ
19344 *last_chain = next_cu;
19345 }
19346 else
19347 last_chain = &per_cu->cu->read_in_chain;
19348
19349 per_cu = next_cu;
19350 }
19351}
19352
19353/* Remove a single compilation unit from the cache. */
19354
19355static void
dee91e82 19356free_one_cached_comp_unit (struct dwarf2_per_cu_data *target_per_cu)
ae038cb0
DJ
19357{
19358 struct dwarf2_per_cu_data *per_cu, **last_chain;
19359
19360 per_cu = dwarf2_per_objfile->read_in_chain;
19361 last_chain = &dwarf2_per_objfile->read_in_chain;
19362 while (per_cu != NULL)
19363 {
19364 struct dwarf2_per_cu_data *next_cu;
19365
19366 next_cu = per_cu->cu->read_in_chain;
19367
dee91e82 19368 if (per_cu == target_per_cu)
ae038cb0 19369 {
68dc6402 19370 free_heap_comp_unit (per_cu->cu);
dee91e82 19371 per_cu->cu = NULL;
ae038cb0
DJ
19372 *last_chain = next_cu;
19373 break;
19374 }
19375 else
19376 last_chain = &per_cu->cu->read_in_chain;
19377
19378 per_cu = next_cu;
19379 }
19380}
19381
fe3e1990
DJ
19382/* Release all extra memory associated with OBJFILE. */
19383
19384void
19385dwarf2_free_objfile (struct objfile *objfile)
19386{
19387 dwarf2_per_objfile = objfile_data (objfile, dwarf2_objfile_data_key);
19388
19389 if (dwarf2_per_objfile == NULL)
19390 return;
19391
19392 /* Cached DIE trees use xmalloc and the comp_unit_obstack. */
19393 free_cached_comp_units (NULL);
19394
7b9f3c50
DE
19395 if (dwarf2_per_objfile->quick_file_names_table)
19396 htab_delete (dwarf2_per_objfile->quick_file_names_table);
9291a0cd 19397
fe3e1990
DJ
19398 /* Everything else should be on the objfile obstack. */
19399}
19400
dee91e82
DE
19401/* A set of CU "per_cu" pointer, DIE offset, and GDB type pointer.
19402 We store these in a hash table separate from the DIEs, and preserve them
19403 when the DIEs are flushed out of cache.
19404
19405 The CU "per_cu" pointer is needed because offset alone is not enough to
3019eac3
DE
19406 uniquely identify the type. A file may have multiple .debug_types sections,
19407 or the type may come from a DWO file. We have to use something in
19408 dwarf2_per_cu_data (or the pointer to it) because we can enter the lookup
19409 routine, get_die_type_at_offset, from outside this file, and thus won't
19410 necessarily have PER_CU->cu. Fortunately, PER_CU is stable for the life
19411 of the objfile. */
1c379e20 19412
dee91e82 19413struct dwarf2_per_cu_offset_and_type
1c379e20 19414{
dee91e82 19415 const struct dwarf2_per_cu_data *per_cu;
b64f50a1 19416 sect_offset offset;
1c379e20
DJ
19417 struct type *type;
19418};
19419
dee91e82 19420/* Hash function for a dwarf2_per_cu_offset_and_type. */
1c379e20
DJ
19421
19422static hashval_t
dee91e82 19423per_cu_offset_and_type_hash (const void *item)
1c379e20 19424{
dee91e82 19425 const struct dwarf2_per_cu_offset_and_type *ofs = item;
9a619af0 19426
dee91e82 19427 return (uintptr_t) ofs->per_cu + ofs->offset.sect_off;
1c379e20
DJ
19428}
19429
dee91e82 19430/* Equality function for a dwarf2_per_cu_offset_and_type. */
1c379e20
DJ
19431
19432static int
dee91e82 19433per_cu_offset_and_type_eq (const void *item_lhs, const void *item_rhs)
1c379e20 19434{
dee91e82
DE
19435 const struct dwarf2_per_cu_offset_and_type *ofs_lhs = item_lhs;
19436 const struct dwarf2_per_cu_offset_and_type *ofs_rhs = item_rhs;
9a619af0 19437
dee91e82
DE
19438 return (ofs_lhs->per_cu == ofs_rhs->per_cu
19439 && ofs_lhs->offset.sect_off == ofs_rhs->offset.sect_off);
1c379e20
DJ
19440}
19441
19442/* Set the type associated with DIE to TYPE. Save it in CU's hash
7e314c57
JK
19443 table if necessary. For convenience, return TYPE.
19444
19445 The DIEs reading must have careful ordering to:
19446 * Not cause infite loops trying to read in DIEs as a prerequisite for
19447 reading current DIE.
19448 * Not trying to dereference contents of still incompletely read in types
19449 while reading in other DIEs.
19450 * Enable referencing still incompletely read in types just by a pointer to
19451 the type without accessing its fields.
19452
19453 Therefore caller should follow these rules:
19454 * Try to fetch any prerequisite types we may need to build this DIE type
19455 before building the type and calling set_die_type.
e71ec853 19456 * After building type call set_die_type for current DIE as soon as
7e314c57
JK
19457 possible before fetching more types to complete the current type.
19458 * Make the type as complete as possible before fetching more types. */
1c379e20 19459
f792889a 19460static struct type *
1c379e20
DJ
19461set_die_type (struct die_info *die, struct type *type, struct dwarf2_cu *cu)
19462{
dee91e82 19463 struct dwarf2_per_cu_offset_and_type **slot, ofs;
673bfd45 19464 struct objfile *objfile = cu->objfile;
1c379e20 19465
b4ba55a1
JB
19466 /* For Ada types, make sure that the gnat-specific data is always
19467 initialized (if not already set). There are a few types where
19468 we should not be doing so, because the type-specific area is
19469 already used to hold some other piece of info (eg: TYPE_CODE_FLT
19470 where the type-specific area is used to store the floatformat).
19471 But this is not a problem, because the gnat-specific information
19472 is actually not needed for these types. */
19473 if (need_gnat_info (cu)
19474 && TYPE_CODE (type) != TYPE_CODE_FUNC
19475 && TYPE_CODE (type) != TYPE_CODE_FLT
19476 && !HAVE_GNAT_AUX_INFO (type))
19477 INIT_GNAT_SPECIFIC (type);
19478
dee91e82 19479 if (dwarf2_per_objfile->die_type_hash == NULL)
f792889a 19480 {
dee91e82
DE
19481 dwarf2_per_objfile->die_type_hash =
19482 htab_create_alloc_ex (127,
19483 per_cu_offset_and_type_hash,
19484 per_cu_offset_and_type_eq,
19485 NULL,
19486 &objfile->objfile_obstack,
19487 hashtab_obstack_allocate,
19488 dummy_obstack_deallocate);
f792889a 19489 }
1c379e20 19490
dee91e82 19491 ofs.per_cu = cu->per_cu;
1c379e20
DJ
19492 ofs.offset = die->offset;
19493 ofs.type = type;
dee91e82
DE
19494 slot = (struct dwarf2_per_cu_offset_and_type **)
19495 htab_find_slot (dwarf2_per_objfile->die_type_hash, &ofs, INSERT);
7e314c57
JK
19496 if (*slot)
19497 complaint (&symfile_complaints,
19498 _("A problem internal to GDB: DIE 0x%x has type already set"),
b64f50a1 19499 die->offset.sect_off);
673bfd45 19500 *slot = obstack_alloc (&objfile->objfile_obstack, sizeof (**slot));
1c379e20 19501 **slot = ofs;
f792889a 19502 return type;
1c379e20
DJ
19503}
19504
380bca97 19505/* Look up the type for the die at OFFSET in the appropriate type_hash
673bfd45 19506 table, or return NULL if the die does not have a saved type. */
1c379e20
DJ
19507
19508static struct type *
b64f50a1 19509get_die_type_at_offset (sect_offset offset,
673bfd45 19510 struct dwarf2_per_cu_data *per_cu)
1c379e20 19511{
dee91e82 19512 struct dwarf2_per_cu_offset_and_type *slot, ofs;
f792889a 19513
dee91e82 19514 if (dwarf2_per_objfile->die_type_hash == NULL)
f792889a 19515 return NULL;
1c379e20 19516
dee91e82 19517 ofs.per_cu = per_cu;
673bfd45 19518 ofs.offset = offset;
dee91e82 19519 slot = htab_find (dwarf2_per_objfile->die_type_hash, &ofs);
1c379e20
DJ
19520 if (slot)
19521 return slot->type;
19522 else
19523 return NULL;
19524}
19525
673bfd45
DE
19526/* Look up the type for DIE in the appropriate type_hash table,
19527 or return NULL if DIE does not have a saved type. */
19528
19529static struct type *
19530get_die_type (struct die_info *die, struct dwarf2_cu *cu)
19531{
19532 return get_die_type_at_offset (die->offset, cu->per_cu);
19533}
19534
10b3939b
DJ
19535/* Add a dependence relationship from CU to REF_PER_CU. */
19536
19537static void
19538dwarf2_add_dependence (struct dwarf2_cu *cu,
19539 struct dwarf2_per_cu_data *ref_per_cu)
19540{
19541 void **slot;
19542
19543 if (cu->dependencies == NULL)
19544 cu->dependencies
19545 = htab_create_alloc_ex (5, htab_hash_pointer, htab_eq_pointer,
19546 NULL, &cu->comp_unit_obstack,
19547 hashtab_obstack_allocate,
19548 dummy_obstack_deallocate);
19549
19550 slot = htab_find_slot (cu->dependencies, ref_per_cu, INSERT);
19551 if (*slot == NULL)
19552 *slot = ref_per_cu;
19553}
1c379e20 19554
f504f079
DE
19555/* Subroutine of dwarf2_mark to pass to htab_traverse.
19556 Set the mark field in every compilation unit in the
ae038cb0
DJ
19557 cache that we must keep because we are keeping CU. */
19558
10b3939b
DJ
19559static int
19560dwarf2_mark_helper (void **slot, void *data)
19561{
19562 struct dwarf2_per_cu_data *per_cu;
19563
19564 per_cu = (struct dwarf2_per_cu_data *) *slot;
d07ed419
JK
19565
19566 /* cu->dependencies references may not yet have been ever read if QUIT aborts
19567 reading of the chain. As such dependencies remain valid it is not much
19568 useful to track and undo them during QUIT cleanups. */
19569 if (per_cu->cu == NULL)
19570 return 1;
19571
10b3939b
DJ
19572 if (per_cu->cu->mark)
19573 return 1;
19574 per_cu->cu->mark = 1;
19575
19576 if (per_cu->cu->dependencies != NULL)
19577 htab_traverse (per_cu->cu->dependencies, dwarf2_mark_helper, NULL);
19578
19579 return 1;
19580}
19581
f504f079
DE
19582/* Set the mark field in CU and in every other compilation unit in the
19583 cache that we must keep because we are keeping CU. */
19584
ae038cb0
DJ
19585static void
19586dwarf2_mark (struct dwarf2_cu *cu)
19587{
19588 if (cu->mark)
19589 return;
19590 cu->mark = 1;
10b3939b
DJ
19591 if (cu->dependencies != NULL)
19592 htab_traverse (cu->dependencies, dwarf2_mark_helper, NULL);
ae038cb0
DJ
19593}
19594
19595static void
19596dwarf2_clear_marks (struct dwarf2_per_cu_data *per_cu)
19597{
19598 while (per_cu)
19599 {
19600 per_cu->cu->mark = 0;
19601 per_cu = per_cu->cu->read_in_chain;
19602 }
72bf9492
DJ
19603}
19604
72bf9492
DJ
19605/* Trivial hash function for partial_die_info: the hash value of a DIE
19606 is its offset in .debug_info for this objfile. */
19607
19608static hashval_t
19609partial_die_hash (const void *item)
19610{
19611 const struct partial_die_info *part_die = item;
9a619af0 19612
b64f50a1 19613 return part_die->offset.sect_off;
72bf9492
DJ
19614}
19615
19616/* Trivial comparison function for partial_die_info structures: two DIEs
19617 are equal if they have the same offset. */
19618
19619static int
19620partial_die_eq (const void *item_lhs, const void *item_rhs)
19621{
19622 const struct partial_die_info *part_die_lhs = item_lhs;
19623 const struct partial_die_info *part_die_rhs = item_rhs;
9a619af0 19624
b64f50a1 19625 return part_die_lhs->offset.sect_off == part_die_rhs->offset.sect_off;
72bf9492
DJ
19626}
19627
ae038cb0
DJ
19628static struct cmd_list_element *set_dwarf2_cmdlist;
19629static struct cmd_list_element *show_dwarf2_cmdlist;
19630
19631static void
19632set_dwarf2_cmd (char *args, int from_tty)
19633{
19634 help_list (set_dwarf2_cmdlist, "maintenance set dwarf2 ", -1, gdb_stdout);
19635}
19636
19637static void
19638show_dwarf2_cmd (char *args, int from_tty)
6e70227d 19639{
ae038cb0
DJ
19640 cmd_show_list (show_dwarf2_cmdlist, from_tty, "");
19641}
19642
4bf44c1c 19643/* Free data associated with OBJFILE, if necessary. */
dce234bc
PP
19644
19645static void
c1bd65d0 19646dwarf2_per_objfile_free (struct objfile *objfile, void *d)
dce234bc
PP
19647{
19648 struct dwarf2_per_objfile *data = d;
8b70b953 19649 int ix;
8b70b953 19650
95554aad
TT
19651 for (ix = 0; ix < dwarf2_per_objfile->n_comp_units; ++ix)
19652 VEC_free (dwarf2_per_cu_ptr,
796a7ff8
DE
19653 dwarf2_per_objfile->all_comp_units[ix]->imported_symtabs);
19654
19655 for (ix = 0; ix < dwarf2_per_objfile->n_type_units; ++ix)
19656 VEC_free (dwarf2_per_cu_ptr,
19657 dwarf2_per_objfile->all_type_units[ix]->per_cu.imported_symtabs);
95554aad 19658
8b70b953 19659 VEC_free (dwarf2_section_info_def, data->types);
3019eac3
DE
19660
19661 if (data->dwo_files)
19662 free_dwo_files (data->dwo_files, objfile);
36586728
TT
19663
19664 if (data->dwz_file && data->dwz_file->dwz_bfd)
19665 gdb_bfd_unref (data->dwz_file->dwz_bfd);
9291a0cd
TT
19666}
19667
19668\f
ae2de4f8 19669/* The "save gdb-index" command. */
9291a0cd
TT
19670
19671/* The contents of the hash table we create when building the string
19672 table. */
19673struct strtab_entry
19674{
19675 offset_type offset;
19676 const char *str;
19677};
19678
559a7a62
JK
19679/* Hash function for a strtab_entry.
19680
19681 Function is used only during write_hash_table so no index format backward
19682 compatibility is needed. */
b89be57b 19683
9291a0cd
TT
19684static hashval_t
19685hash_strtab_entry (const void *e)
19686{
19687 const struct strtab_entry *entry = e;
559a7a62 19688 return mapped_index_string_hash (INT_MAX, entry->str);
9291a0cd
TT
19689}
19690
19691/* Equality function for a strtab_entry. */
b89be57b 19692
9291a0cd
TT
19693static int
19694eq_strtab_entry (const void *a, const void *b)
19695{
19696 const struct strtab_entry *ea = a;
19697 const struct strtab_entry *eb = b;
19698 return !strcmp (ea->str, eb->str);
19699}
19700
19701/* Create a strtab_entry hash table. */
b89be57b 19702
9291a0cd
TT
19703static htab_t
19704create_strtab (void)
19705{
19706 return htab_create_alloc (100, hash_strtab_entry, eq_strtab_entry,
19707 xfree, xcalloc, xfree);
19708}
19709
19710/* Add a string to the constant pool. Return the string's offset in
19711 host order. */
b89be57b 19712
9291a0cd
TT
19713static offset_type
19714add_string (htab_t table, struct obstack *cpool, const char *str)
19715{
19716 void **slot;
19717 struct strtab_entry entry;
19718 struct strtab_entry *result;
19719
19720 entry.str = str;
19721 slot = htab_find_slot (table, &entry, INSERT);
19722 if (*slot)
19723 result = *slot;
19724 else
19725 {
19726 result = XNEW (struct strtab_entry);
19727 result->offset = obstack_object_size (cpool);
19728 result->str = str;
19729 obstack_grow_str0 (cpool, str);
19730 *slot = result;
19731 }
19732 return result->offset;
19733}
19734
19735/* An entry in the symbol table. */
19736struct symtab_index_entry
19737{
19738 /* The name of the symbol. */
19739 const char *name;
19740 /* The offset of the name in the constant pool. */
19741 offset_type index_offset;
19742 /* A sorted vector of the indices of all the CUs that hold an object
19743 of this name. */
19744 VEC (offset_type) *cu_indices;
19745};
19746
19747/* The symbol table. This is a power-of-2-sized hash table. */
19748struct mapped_symtab
19749{
19750 offset_type n_elements;
19751 offset_type size;
19752 struct symtab_index_entry **data;
19753};
19754
19755/* Hash function for a symtab_index_entry. */
b89be57b 19756
9291a0cd
TT
19757static hashval_t
19758hash_symtab_entry (const void *e)
19759{
19760 const struct symtab_index_entry *entry = e;
19761 return iterative_hash (VEC_address (offset_type, entry->cu_indices),
19762 sizeof (offset_type) * VEC_length (offset_type,
19763 entry->cu_indices),
19764 0);
19765}
19766
19767/* Equality function for a symtab_index_entry. */
b89be57b 19768
9291a0cd
TT
19769static int
19770eq_symtab_entry (const void *a, const void *b)
19771{
19772 const struct symtab_index_entry *ea = a;
19773 const struct symtab_index_entry *eb = b;
19774 int len = VEC_length (offset_type, ea->cu_indices);
19775 if (len != VEC_length (offset_type, eb->cu_indices))
19776 return 0;
19777 return !memcmp (VEC_address (offset_type, ea->cu_indices),
19778 VEC_address (offset_type, eb->cu_indices),
19779 sizeof (offset_type) * len);
19780}
19781
19782/* Destroy a symtab_index_entry. */
b89be57b 19783
9291a0cd
TT
19784static void
19785delete_symtab_entry (void *p)
19786{
19787 struct symtab_index_entry *entry = p;
19788 VEC_free (offset_type, entry->cu_indices);
19789 xfree (entry);
19790}
19791
19792/* Create a hash table holding symtab_index_entry objects. */
b89be57b 19793
9291a0cd 19794static htab_t
3876f04e 19795create_symbol_hash_table (void)
9291a0cd
TT
19796{
19797 return htab_create_alloc (100, hash_symtab_entry, eq_symtab_entry,
19798 delete_symtab_entry, xcalloc, xfree);
19799}
19800
19801/* Create a new mapped symtab object. */
b89be57b 19802
9291a0cd
TT
19803static struct mapped_symtab *
19804create_mapped_symtab (void)
19805{
19806 struct mapped_symtab *symtab = XNEW (struct mapped_symtab);
19807 symtab->n_elements = 0;
19808 symtab->size = 1024;
19809 symtab->data = XCNEWVEC (struct symtab_index_entry *, symtab->size);
19810 return symtab;
19811}
19812
19813/* Destroy a mapped_symtab. */
b89be57b 19814
9291a0cd
TT
19815static void
19816cleanup_mapped_symtab (void *p)
19817{
19818 struct mapped_symtab *symtab = p;
19819 /* The contents of the array are freed when the other hash table is
19820 destroyed. */
19821 xfree (symtab->data);
19822 xfree (symtab);
19823}
19824
19825/* Find a slot in SYMTAB for the symbol NAME. Returns a pointer to
559a7a62
JK
19826 the slot.
19827
19828 Function is used only during write_hash_table so no index format backward
19829 compatibility is needed. */
b89be57b 19830
9291a0cd
TT
19831static struct symtab_index_entry **
19832find_slot (struct mapped_symtab *symtab, const char *name)
19833{
559a7a62 19834 offset_type index, step, hash = mapped_index_string_hash (INT_MAX, name);
9291a0cd
TT
19835
19836 index = hash & (symtab->size - 1);
19837 step = ((hash * 17) & (symtab->size - 1)) | 1;
19838
19839 for (;;)
19840 {
19841 if (!symtab->data[index] || !strcmp (name, symtab->data[index]->name))
19842 return &symtab->data[index];
19843 index = (index + step) & (symtab->size - 1);
19844 }
19845}
19846
19847/* Expand SYMTAB's hash table. */
b89be57b 19848
9291a0cd
TT
19849static void
19850hash_expand (struct mapped_symtab *symtab)
19851{
19852 offset_type old_size = symtab->size;
19853 offset_type i;
19854 struct symtab_index_entry **old_entries = symtab->data;
19855
19856 symtab->size *= 2;
19857 symtab->data = XCNEWVEC (struct symtab_index_entry *, symtab->size);
19858
19859 for (i = 0; i < old_size; ++i)
19860 {
19861 if (old_entries[i])
19862 {
19863 struct symtab_index_entry **slot = find_slot (symtab,
19864 old_entries[i]->name);
19865 *slot = old_entries[i];
19866 }
19867 }
19868
19869 xfree (old_entries);
19870}
19871
156942c7
DE
19872/* Add an entry to SYMTAB. NAME is the name of the symbol.
19873 CU_INDEX is the index of the CU in which the symbol appears.
19874 IS_STATIC is one if the symbol is static, otherwise zero (global). */
b89be57b 19875
9291a0cd
TT
19876static void
19877add_index_entry (struct mapped_symtab *symtab, const char *name,
156942c7 19878 int is_static, gdb_index_symbol_kind kind,
9291a0cd
TT
19879 offset_type cu_index)
19880{
19881 struct symtab_index_entry **slot;
156942c7 19882 offset_type cu_index_and_attrs;
9291a0cd
TT
19883
19884 ++symtab->n_elements;
19885 if (4 * symtab->n_elements / 3 >= symtab->size)
19886 hash_expand (symtab);
19887
19888 slot = find_slot (symtab, name);
19889 if (!*slot)
19890 {
19891 *slot = XNEW (struct symtab_index_entry);
19892 (*slot)->name = name;
156942c7 19893 /* index_offset is set later. */
9291a0cd
TT
19894 (*slot)->cu_indices = NULL;
19895 }
156942c7
DE
19896
19897 cu_index_and_attrs = 0;
19898 DW2_GDB_INDEX_CU_SET_VALUE (cu_index_and_attrs, cu_index);
19899 DW2_GDB_INDEX_SYMBOL_STATIC_SET_VALUE (cu_index_and_attrs, is_static);
19900 DW2_GDB_INDEX_SYMBOL_KIND_SET_VALUE (cu_index_and_attrs, kind);
19901
19902 /* We don't want to record an index value twice as we want to avoid the
19903 duplication.
19904 We process all global symbols and then all static symbols
19905 (which would allow us to avoid the duplication by only having to check
19906 the last entry pushed), but a symbol could have multiple kinds in one CU.
19907 To keep things simple we don't worry about the duplication here and
19908 sort and uniqufy the list after we've processed all symbols. */
19909 VEC_safe_push (offset_type, (*slot)->cu_indices, cu_index_and_attrs);
19910}
19911
19912/* qsort helper routine for uniquify_cu_indices. */
19913
19914static int
19915offset_type_compare (const void *ap, const void *bp)
19916{
19917 offset_type a = *(offset_type *) ap;
19918 offset_type b = *(offset_type *) bp;
19919
19920 return (a > b) - (b > a);
19921}
19922
19923/* Sort and remove duplicates of all symbols' cu_indices lists. */
19924
19925static void
19926uniquify_cu_indices (struct mapped_symtab *symtab)
19927{
19928 int i;
19929
19930 for (i = 0; i < symtab->size; ++i)
19931 {
19932 struct symtab_index_entry *entry = symtab->data[i];
19933
19934 if (entry
19935 && entry->cu_indices != NULL)
19936 {
19937 unsigned int next_to_insert, next_to_check;
19938 offset_type last_value;
19939
19940 qsort (VEC_address (offset_type, entry->cu_indices),
19941 VEC_length (offset_type, entry->cu_indices),
19942 sizeof (offset_type), offset_type_compare);
19943
19944 last_value = VEC_index (offset_type, entry->cu_indices, 0);
19945 next_to_insert = 1;
19946 for (next_to_check = 1;
19947 next_to_check < VEC_length (offset_type, entry->cu_indices);
19948 ++next_to_check)
19949 {
19950 if (VEC_index (offset_type, entry->cu_indices, next_to_check)
19951 != last_value)
19952 {
19953 last_value = VEC_index (offset_type, entry->cu_indices,
19954 next_to_check);
19955 VEC_replace (offset_type, entry->cu_indices, next_to_insert,
19956 last_value);
19957 ++next_to_insert;
19958 }
19959 }
19960 VEC_truncate (offset_type, entry->cu_indices, next_to_insert);
19961 }
19962 }
9291a0cd
TT
19963}
19964
19965/* Add a vector of indices to the constant pool. */
b89be57b 19966
9291a0cd 19967static offset_type
3876f04e 19968add_indices_to_cpool (htab_t symbol_hash_table, struct obstack *cpool,
9291a0cd
TT
19969 struct symtab_index_entry *entry)
19970{
19971 void **slot;
19972
3876f04e 19973 slot = htab_find_slot (symbol_hash_table, entry, INSERT);
9291a0cd
TT
19974 if (!*slot)
19975 {
19976 offset_type len = VEC_length (offset_type, entry->cu_indices);
19977 offset_type val = MAYBE_SWAP (len);
19978 offset_type iter;
19979 int i;
19980
19981 *slot = entry;
19982 entry->index_offset = obstack_object_size (cpool);
19983
19984 obstack_grow (cpool, &val, sizeof (val));
19985 for (i = 0;
19986 VEC_iterate (offset_type, entry->cu_indices, i, iter);
19987 ++i)
19988 {
19989 val = MAYBE_SWAP (iter);
19990 obstack_grow (cpool, &val, sizeof (val));
19991 }
19992 }
19993 else
19994 {
19995 struct symtab_index_entry *old_entry = *slot;
19996 entry->index_offset = old_entry->index_offset;
19997 entry = old_entry;
19998 }
19999 return entry->index_offset;
20000}
20001
20002/* Write the mapped hash table SYMTAB to the obstack OUTPUT, with
20003 constant pool entries going into the obstack CPOOL. */
b89be57b 20004
9291a0cd
TT
20005static void
20006write_hash_table (struct mapped_symtab *symtab,
20007 struct obstack *output, struct obstack *cpool)
20008{
20009 offset_type i;
3876f04e 20010 htab_t symbol_hash_table;
9291a0cd
TT
20011 htab_t str_table;
20012
3876f04e 20013 symbol_hash_table = create_symbol_hash_table ();
9291a0cd 20014 str_table = create_strtab ();
3876f04e 20015
9291a0cd
TT
20016 /* We add all the index vectors to the constant pool first, to
20017 ensure alignment is ok. */
20018 for (i = 0; i < symtab->size; ++i)
20019 {
20020 if (symtab->data[i])
3876f04e 20021 add_indices_to_cpool (symbol_hash_table, cpool, symtab->data[i]);
9291a0cd
TT
20022 }
20023
20024 /* Now write out the hash table. */
20025 for (i = 0; i < symtab->size; ++i)
20026 {
20027 offset_type str_off, vec_off;
20028
20029 if (symtab->data[i])
20030 {
20031 str_off = add_string (str_table, cpool, symtab->data[i]->name);
20032 vec_off = symtab->data[i]->index_offset;
20033 }
20034 else
20035 {
20036 /* While 0 is a valid constant pool index, it is not valid
20037 to have 0 for both offsets. */
20038 str_off = 0;
20039 vec_off = 0;
20040 }
20041
20042 str_off = MAYBE_SWAP (str_off);
20043 vec_off = MAYBE_SWAP (vec_off);
20044
20045 obstack_grow (output, &str_off, sizeof (str_off));
20046 obstack_grow (output, &vec_off, sizeof (vec_off));
20047 }
20048
20049 htab_delete (str_table);
3876f04e 20050 htab_delete (symbol_hash_table);
9291a0cd
TT
20051}
20052
0a5429f6
DE
20053/* Struct to map psymtab to CU index in the index file. */
20054struct psymtab_cu_index_map
20055{
20056 struct partial_symtab *psymtab;
20057 unsigned int cu_index;
20058};
20059
20060static hashval_t
20061hash_psymtab_cu_index (const void *item)
20062{
20063 const struct psymtab_cu_index_map *map = item;
20064
20065 return htab_hash_pointer (map->psymtab);
20066}
20067
20068static int
20069eq_psymtab_cu_index (const void *item_lhs, const void *item_rhs)
20070{
20071 const struct psymtab_cu_index_map *lhs = item_lhs;
20072 const struct psymtab_cu_index_map *rhs = item_rhs;
20073
20074 return lhs->psymtab == rhs->psymtab;
20075}
20076
20077/* Helper struct for building the address table. */
20078struct addrmap_index_data
20079{
20080 struct objfile *objfile;
20081 struct obstack *addr_obstack;
20082 htab_t cu_index_htab;
20083
20084 /* Non-zero if the previous_* fields are valid.
20085 We can't write an entry until we see the next entry (since it is only then
20086 that we know the end of the entry). */
20087 int previous_valid;
20088 /* Index of the CU in the table of all CUs in the index file. */
20089 unsigned int previous_cu_index;
0963b4bd 20090 /* Start address of the CU. */
0a5429f6
DE
20091 CORE_ADDR previous_cu_start;
20092};
20093
20094/* Write an address entry to OBSTACK. */
b89be57b 20095
9291a0cd 20096static void
0a5429f6
DE
20097add_address_entry (struct objfile *objfile, struct obstack *obstack,
20098 CORE_ADDR start, CORE_ADDR end, unsigned int cu_index)
9291a0cd 20099{
0a5429f6 20100 offset_type cu_index_to_write;
9291a0cd
TT
20101 char addr[8];
20102 CORE_ADDR baseaddr;
20103
20104 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
20105
0a5429f6
DE
20106 store_unsigned_integer (addr, 8, BFD_ENDIAN_LITTLE, start - baseaddr);
20107 obstack_grow (obstack, addr, 8);
20108 store_unsigned_integer (addr, 8, BFD_ENDIAN_LITTLE, end - baseaddr);
20109 obstack_grow (obstack, addr, 8);
20110 cu_index_to_write = MAYBE_SWAP (cu_index);
20111 obstack_grow (obstack, &cu_index_to_write, sizeof (offset_type));
20112}
20113
20114/* Worker function for traversing an addrmap to build the address table. */
20115
20116static int
20117add_address_entry_worker (void *datap, CORE_ADDR start_addr, void *obj)
20118{
20119 struct addrmap_index_data *data = datap;
20120 struct partial_symtab *pst = obj;
0a5429f6
DE
20121
20122 if (data->previous_valid)
20123 add_address_entry (data->objfile, data->addr_obstack,
20124 data->previous_cu_start, start_addr,
20125 data->previous_cu_index);
20126
20127 data->previous_cu_start = start_addr;
20128 if (pst != NULL)
20129 {
20130 struct psymtab_cu_index_map find_map, *map;
20131 find_map.psymtab = pst;
20132 map = htab_find (data->cu_index_htab, &find_map);
20133 gdb_assert (map != NULL);
20134 data->previous_cu_index = map->cu_index;
20135 data->previous_valid = 1;
20136 }
20137 else
20138 data->previous_valid = 0;
20139
20140 return 0;
20141}
20142
20143/* Write OBJFILE's address map to OBSTACK.
20144 CU_INDEX_HTAB is used to map addrmap entries to their CU indices
20145 in the index file. */
20146
20147static void
20148write_address_map (struct objfile *objfile, struct obstack *obstack,
20149 htab_t cu_index_htab)
20150{
20151 struct addrmap_index_data addrmap_index_data;
20152
20153 /* When writing the address table, we have to cope with the fact that
20154 the addrmap iterator only provides the start of a region; we have to
20155 wait until the next invocation to get the start of the next region. */
20156
20157 addrmap_index_data.objfile = objfile;
20158 addrmap_index_data.addr_obstack = obstack;
20159 addrmap_index_data.cu_index_htab = cu_index_htab;
20160 addrmap_index_data.previous_valid = 0;
20161
20162 addrmap_foreach (objfile->psymtabs_addrmap, add_address_entry_worker,
20163 &addrmap_index_data);
20164
20165 /* It's highly unlikely the last entry (end address = 0xff...ff)
20166 is valid, but we should still handle it.
20167 The end address is recorded as the start of the next region, but that
20168 doesn't work here. To cope we pass 0xff...ff, this is a rare situation
20169 anyway. */
20170 if (addrmap_index_data.previous_valid)
20171 add_address_entry (objfile, obstack,
20172 addrmap_index_data.previous_cu_start, (CORE_ADDR) -1,
20173 addrmap_index_data.previous_cu_index);
9291a0cd
TT
20174}
20175
156942c7
DE
20176/* Return the symbol kind of PSYM. */
20177
20178static gdb_index_symbol_kind
20179symbol_kind (struct partial_symbol *psym)
20180{
20181 domain_enum domain = PSYMBOL_DOMAIN (psym);
20182 enum address_class aclass = PSYMBOL_CLASS (psym);
20183
20184 switch (domain)
20185 {
20186 case VAR_DOMAIN:
20187 switch (aclass)
20188 {
20189 case LOC_BLOCK:
20190 return GDB_INDEX_SYMBOL_KIND_FUNCTION;
20191 case LOC_TYPEDEF:
20192 return GDB_INDEX_SYMBOL_KIND_TYPE;
20193 case LOC_COMPUTED:
20194 case LOC_CONST_BYTES:
20195 case LOC_OPTIMIZED_OUT:
20196 case LOC_STATIC:
20197 return GDB_INDEX_SYMBOL_KIND_VARIABLE;
20198 case LOC_CONST:
20199 /* Note: It's currently impossible to recognize psyms as enum values
20200 short of reading the type info. For now punt. */
20201 return GDB_INDEX_SYMBOL_KIND_VARIABLE;
20202 default:
20203 /* There are other LOC_FOO values that one might want to classify
20204 as variables, but dwarf2read.c doesn't currently use them. */
20205 return GDB_INDEX_SYMBOL_KIND_OTHER;
20206 }
20207 case STRUCT_DOMAIN:
20208 return GDB_INDEX_SYMBOL_KIND_TYPE;
20209 default:
20210 return GDB_INDEX_SYMBOL_KIND_OTHER;
20211 }
20212}
20213
9291a0cd 20214/* Add a list of partial symbols to SYMTAB. */
b89be57b 20215
9291a0cd
TT
20216static void
20217write_psymbols (struct mapped_symtab *symtab,
987d643c 20218 htab_t psyms_seen,
9291a0cd
TT
20219 struct partial_symbol **psymp,
20220 int count,
987d643c
TT
20221 offset_type cu_index,
20222 int is_static)
9291a0cd
TT
20223{
20224 for (; count-- > 0; ++psymp)
20225 {
156942c7
DE
20226 struct partial_symbol *psym = *psymp;
20227 void **slot;
987d643c 20228
156942c7 20229 if (SYMBOL_LANGUAGE (psym) == language_ada)
9291a0cd 20230 error (_("Ada is not currently supported by the index"));
987d643c 20231
987d643c 20232 /* Only add a given psymbol once. */
156942c7 20233 slot = htab_find_slot (psyms_seen, psym, INSERT);
987d643c
TT
20234 if (!*slot)
20235 {
156942c7
DE
20236 gdb_index_symbol_kind kind = symbol_kind (psym);
20237
20238 *slot = psym;
20239 add_index_entry (symtab, SYMBOL_SEARCH_NAME (psym),
20240 is_static, kind, cu_index);
987d643c 20241 }
9291a0cd
TT
20242 }
20243}
20244
20245/* Write the contents of an ("unfinished") obstack to FILE. Throw an
20246 exception if there is an error. */
b89be57b 20247
9291a0cd
TT
20248static void
20249write_obstack (FILE *file, struct obstack *obstack)
20250{
20251 if (fwrite (obstack_base (obstack), 1, obstack_object_size (obstack),
20252 file)
20253 != obstack_object_size (obstack))
20254 error (_("couldn't data write to file"));
20255}
20256
20257/* Unlink a file if the argument is not NULL. */
b89be57b 20258
9291a0cd
TT
20259static void
20260unlink_if_set (void *p)
20261{
20262 char **filename = p;
20263 if (*filename)
20264 unlink (*filename);
20265}
20266
1fd400ff
TT
20267/* A helper struct used when iterating over debug_types. */
20268struct signatured_type_index_data
20269{
20270 struct objfile *objfile;
20271 struct mapped_symtab *symtab;
20272 struct obstack *types_list;
987d643c 20273 htab_t psyms_seen;
1fd400ff
TT
20274 int cu_index;
20275};
20276
20277/* A helper function that writes a single signatured_type to an
20278 obstack. */
b89be57b 20279
1fd400ff
TT
20280static int
20281write_one_signatured_type (void **slot, void *d)
20282{
20283 struct signatured_type_index_data *info = d;
20284 struct signatured_type *entry = (struct signatured_type *) *slot;
e254ef6a
DE
20285 struct dwarf2_per_cu_data *per_cu = &entry->per_cu;
20286 struct partial_symtab *psymtab = per_cu->v.psymtab;
1fd400ff
TT
20287 gdb_byte val[8];
20288
20289 write_psymbols (info->symtab,
987d643c 20290 info->psyms_seen,
3e43a32a
MS
20291 info->objfile->global_psymbols.list
20292 + psymtab->globals_offset,
987d643c
TT
20293 psymtab->n_global_syms, info->cu_index,
20294 0);
1fd400ff 20295 write_psymbols (info->symtab,
987d643c 20296 info->psyms_seen,
3e43a32a
MS
20297 info->objfile->static_psymbols.list
20298 + psymtab->statics_offset,
987d643c
TT
20299 psymtab->n_static_syms, info->cu_index,
20300 1);
1fd400ff 20301
b64f50a1
JK
20302 store_unsigned_integer (val, 8, BFD_ENDIAN_LITTLE,
20303 entry->per_cu.offset.sect_off);
1fd400ff 20304 obstack_grow (info->types_list, val, 8);
3019eac3
DE
20305 store_unsigned_integer (val, 8, BFD_ENDIAN_LITTLE,
20306 entry->type_offset_in_tu.cu_off);
1fd400ff
TT
20307 obstack_grow (info->types_list, val, 8);
20308 store_unsigned_integer (val, 8, BFD_ENDIAN_LITTLE, entry->signature);
20309 obstack_grow (info->types_list, val, 8);
20310
20311 ++info->cu_index;
20312
20313 return 1;
20314}
20315
95554aad
TT
20316/* Recurse into all "included" dependencies and write their symbols as
20317 if they appeared in this psymtab. */
20318
20319static void
20320recursively_write_psymbols (struct objfile *objfile,
20321 struct partial_symtab *psymtab,
20322 struct mapped_symtab *symtab,
20323 htab_t psyms_seen,
20324 offset_type cu_index)
20325{
20326 int i;
20327
20328 for (i = 0; i < psymtab->number_of_dependencies; ++i)
20329 if (psymtab->dependencies[i]->user != NULL)
20330 recursively_write_psymbols (objfile, psymtab->dependencies[i],
20331 symtab, psyms_seen, cu_index);
20332
20333 write_psymbols (symtab,
20334 psyms_seen,
20335 objfile->global_psymbols.list + psymtab->globals_offset,
20336 psymtab->n_global_syms, cu_index,
20337 0);
20338 write_psymbols (symtab,
20339 psyms_seen,
20340 objfile->static_psymbols.list + psymtab->statics_offset,
20341 psymtab->n_static_syms, cu_index,
20342 1);
20343}
20344
9291a0cd 20345/* Create an index file for OBJFILE in the directory DIR. */
b89be57b 20346
9291a0cd
TT
20347static void
20348write_psymtabs_to_index (struct objfile *objfile, const char *dir)
20349{
20350 struct cleanup *cleanup;
20351 char *filename, *cleanup_filename;
1fd400ff
TT
20352 struct obstack contents, addr_obstack, constant_pool, symtab_obstack;
20353 struct obstack cu_list, types_cu_list;
9291a0cd
TT
20354 int i;
20355 FILE *out_file;
20356 struct mapped_symtab *symtab;
20357 offset_type val, size_of_contents, total_len;
20358 struct stat st;
987d643c 20359 htab_t psyms_seen;
0a5429f6
DE
20360 htab_t cu_index_htab;
20361 struct psymtab_cu_index_map *psymtab_cu_index_map;
9291a0cd 20362
b4f2f049 20363 if (!objfile->psymtabs || !objfile->psymtabs_addrmap)
9291a0cd 20364 return;
b4f2f049 20365
9291a0cd
TT
20366 if (dwarf2_per_objfile->using_index)
20367 error (_("Cannot use an index to create the index"));
20368
8b70b953
TT
20369 if (VEC_length (dwarf2_section_info_def, dwarf2_per_objfile->types) > 1)
20370 error (_("Cannot make an index when the file has multiple .debug_types sections"));
20371
9291a0cd 20372 if (stat (objfile->name, &st) < 0)
7e17e088 20373 perror_with_name (objfile->name);
9291a0cd
TT
20374
20375 filename = concat (dir, SLASH_STRING, lbasename (objfile->name),
20376 INDEX_SUFFIX, (char *) NULL);
20377 cleanup = make_cleanup (xfree, filename);
20378
20379 out_file = fopen (filename, "wb");
20380 if (!out_file)
20381 error (_("Can't open `%s' for writing"), filename);
20382
20383 cleanup_filename = filename;
20384 make_cleanup (unlink_if_set, &cleanup_filename);
20385
20386 symtab = create_mapped_symtab ();
20387 make_cleanup (cleanup_mapped_symtab, symtab);
20388
20389 obstack_init (&addr_obstack);
20390 make_cleanup_obstack_free (&addr_obstack);
20391
20392 obstack_init (&cu_list);
20393 make_cleanup_obstack_free (&cu_list);
20394
1fd400ff
TT
20395 obstack_init (&types_cu_list);
20396 make_cleanup_obstack_free (&types_cu_list);
20397
987d643c
TT
20398 psyms_seen = htab_create_alloc (100, htab_hash_pointer, htab_eq_pointer,
20399 NULL, xcalloc, xfree);
96408a79 20400 make_cleanup_htab_delete (psyms_seen);
987d643c 20401
0a5429f6
DE
20402 /* While we're scanning CU's create a table that maps a psymtab pointer
20403 (which is what addrmap records) to its index (which is what is recorded
20404 in the index file). This will later be needed to write the address
20405 table. */
20406 cu_index_htab = htab_create_alloc (100,
20407 hash_psymtab_cu_index,
20408 eq_psymtab_cu_index,
20409 NULL, xcalloc, xfree);
96408a79 20410 make_cleanup_htab_delete (cu_index_htab);
0a5429f6
DE
20411 psymtab_cu_index_map = (struct psymtab_cu_index_map *)
20412 xmalloc (sizeof (struct psymtab_cu_index_map)
20413 * dwarf2_per_objfile->n_comp_units);
20414 make_cleanup (xfree, psymtab_cu_index_map);
20415
20416 /* The CU list is already sorted, so we don't need to do additional
1fd400ff
TT
20417 work here. Also, the debug_types entries do not appear in
20418 all_comp_units, but only in their own hash table. */
9291a0cd
TT
20419 for (i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
20420 {
3e43a32a
MS
20421 struct dwarf2_per_cu_data *per_cu
20422 = dwarf2_per_objfile->all_comp_units[i];
e254ef6a 20423 struct partial_symtab *psymtab = per_cu->v.psymtab;
9291a0cd 20424 gdb_byte val[8];
0a5429f6
DE
20425 struct psymtab_cu_index_map *map;
20426 void **slot;
9291a0cd 20427
95554aad
TT
20428 if (psymtab->user == NULL)
20429 recursively_write_psymbols (objfile, psymtab, symtab, psyms_seen, i);
9291a0cd 20430
0a5429f6
DE
20431 map = &psymtab_cu_index_map[i];
20432 map->psymtab = psymtab;
20433 map->cu_index = i;
20434 slot = htab_find_slot (cu_index_htab, map, INSERT);
20435 gdb_assert (slot != NULL);
20436 gdb_assert (*slot == NULL);
20437 *slot = map;
9291a0cd 20438
b64f50a1
JK
20439 store_unsigned_integer (val, 8, BFD_ENDIAN_LITTLE,
20440 per_cu->offset.sect_off);
9291a0cd 20441 obstack_grow (&cu_list, val, 8);
e254ef6a 20442 store_unsigned_integer (val, 8, BFD_ENDIAN_LITTLE, per_cu->length);
9291a0cd
TT
20443 obstack_grow (&cu_list, val, 8);
20444 }
20445
0a5429f6
DE
20446 /* Dump the address map. */
20447 write_address_map (objfile, &addr_obstack, cu_index_htab);
20448
1fd400ff
TT
20449 /* Write out the .debug_type entries, if any. */
20450 if (dwarf2_per_objfile->signatured_types)
20451 {
20452 struct signatured_type_index_data sig_data;
20453
20454 sig_data.objfile = objfile;
20455 sig_data.symtab = symtab;
20456 sig_data.types_list = &types_cu_list;
987d643c 20457 sig_data.psyms_seen = psyms_seen;
1fd400ff
TT
20458 sig_data.cu_index = dwarf2_per_objfile->n_comp_units;
20459 htab_traverse_noresize (dwarf2_per_objfile->signatured_types,
20460 write_one_signatured_type, &sig_data);
20461 }
20462
156942c7
DE
20463 /* Now that we've processed all symbols we can shrink their cu_indices
20464 lists. */
20465 uniquify_cu_indices (symtab);
20466
9291a0cd
TT
20467 obstack_init (&constant_pool);
20468 make_cleanup_obstack_free (&constant_pool);
20469 obstack_init (&symtab_obstack);
20470 make_cleanup_obstack_free (&symtab_obstack);
20471 write_hash_table (symtab, &symtab_obstack, &constant_pool);
20472
20473 obstack_init (&contents);
20474 make_cleanup_obstack_free (&contents);
1fd400ff 20475 size_of_contents = 6 * sizeof (offset_type);
9291a0cd
TT
20476 total_len = size_of_contents;
20477
20478 /* The version number. */
796a7ff8 20479 val = MAYBE_SWAP (8);
9291a0cd
TT
20480 obstack_grow (&contents, &val, sizeof (val));
20481
20482 /* The offset of the CU list from the start of the file. */
20483 val = MAYBE_SWAP (total_len);
20484 obstack_grow (&contents, &val, sizeof (val));
20485 total_len += obstack_object_size (&cu_list);
20486
1fd400ff
TT
20487 /* The offset of the types CU list from the start of the file. */
20488 val = MAYBE_SWAP (total_len);
20489 obstack_grow (&contents, &val, sizeof (val));
20490 total_len += obstack_object_size (&types_cu_list);
20491
9291a0cd
TT
20492 /* The offset of the address table from the start of the file. */
20493 val = MAYBE_SWAP (total_len);
20494 obstack_grow (&contents, &val, sizeof (val));
20495 total_len += obstack_object_size (&addr_obstack);
20496
20497 /* The offset of the symbol table from the start of the file. */
20498 val = MAYBE_SWAP (total_len);
20499 obstack_grow (&contents, &val, sizeof (val));
20500 total_len += obstack_object_size (&symtab_obstack);
20501
20502 /* The offset of the constant pool from the start of the file. */
20503 val = MAYBE_SWAP (total_len);
20504 obstack_grow (&contents, &val, sizeof (val));
20505 total_len += obstack_object_size (&constant_pool);
20506
20507 gdb_assert (obstack_object_size (&contents) == size_of_contents);
20508
20509 write_obstack (out_file, &contents);
20510 write_obstack (out_file, &cu_list);
1fd400ff 20511 write_obstack (out_file, &types_cu_list);
9291a0cd
TT
20512 write_obstack (out_file, &addr_obstack);
20513 write_obstack (out_file, &symtab_obstack);
20514 write_obstack (out_file, &constant_pool);
20515
20516 fclose (out_file);
20517
20518 /* We want to keep the file, so we set cleanup_filename to NULL
20519 here. See unlink_if_set. */
20520 cleanup_filename = NULL;
20521
20522 do_cleanups (cleanup);
20523}
20524
90476074
TT
20525/* Implementation of the `save gdb-index' command.
20526
20527 Note that the file format used by this command is documented in the
20528 GDB manual. Any changes here must be documented there. */
11570e71 20529
9291a0cd
TT
20530static void
20531save_gdb_index_command (char *arg, int from_tty)
20532{
20533 struct objfile *objfile;
20534
20535 if (!arg || !*arg)
96d19272 20536 error (_("usage: save gdb-index DIRECTORY"));
9291a0cd
TT
20537
20538 ALL_OBJFILES (objfile)
20539 {
20540 struct stat st;
20541
20542 /* If the objfile does not correspond to an actual file, skip it. */
20543 if (stat (objfile->name, &st) < 0)
20544 continue;
20545
20546 dwarf2_per_objfile = objfile_data (objfile, dwarf2_objfile_data_key);
20547 if (dwarf2_per_objfile)
20548 {
20549 volatile struct gdb_exception except;
20550
20551 TRY_CATCH (except, RETURN_MASK_ERROR)
20552 {
20553 write_psymtabs_to_index (objfile, arg);
20554 }
20555 if (except.reason < 0)
20556 exception_fprintf (gdb_stderr, except,
20557 _("Error while writing index for `%s': "),
20558 objfile->name);
20559 }
20560 }
dce234bc
PP
20561}
20562
9291a0cd
TT
20563\f
20564
9eae7c52
TT
20565int dwarf2_always_disassemble;
20566
20567static void
20568show_dwarf2_always_disassemble (struct ui_file *file, int from_tty,
20569 struct cmd_list_element *c, const char *value)
20570{
3e43a32a
MS
20571 fprintf_filtered (file,
20572 _("Whether to always disassemble "
20573 "DWARF expressions is %s.\n"),
9eae7c52
TT
20574 value);
20575}
20576
900e11f9
JK
20577static void
20578show_check_physname (struct ui_file *file, int from_tty,
20579 struct cmd_list_element *c, const char *value)
20580{
20581 fprintf_filtered (file,
20582 _("Whether to check \"physname\" is %s.\n"),
20583 value);
20584}
20585
6502dd73
DJ
20586void _initialize_dwarf2_read (void);
20587
20588void
20589_initialize_dwarf2_read (void)
20590{
96d19272
JK
20591 struct cmd_list_element *c;
20592
dce234bc 20593 dwarf2_objfile_data_key
c1bd65d0 20594 = register_objfile_data_with_cleanup (NULL, dwarf2_per_objfile_free);
ae038cb0 20595
1bedd215
AC
20596 add_prefix_cmd ("dwarf2", class_maintenance, set_dwarf2_cmd, _("\
20597Set DWARF 2 specific variables.\n\
20598Configure DWARF 2 variables such as the cache size"),
ae038cb0
DJ
20599 &set_dwarf2_cmdlist, "maintenance set dwarf2 ",
20600 0/*allow-unknown*/, &maintenance_set_cmdlist);
20601
1bedd215
AC
20602 add_prefix_cmd ("dwarf2", class_maintenance, show_dwarf2_cmd, _("\
20603Show DWARF 2 specific variables\n\
20604Show DWARF 2 variables such as the cache size"),
ae038cb0
DJ
20605 &show_dwarf2_cmdlist, "maintenance show dwarf2 ",
20606 0/*allow-unknown*/, &maintenance_show_cmdlist);
20607
20608 add_setshow_zinteger_cmd ("max-cache-age", class_obscure,
7915a72c
AC
20609 &dwarf2_max_cache_age, _("\
20610Set the upper bound on the age of cached dwarf2 compilation units."), _("\
20611Show the upper bound on the age of cached dwarf2 compilation units."), _("\
20612A higher limit means that cached compilation units will be stored\n\
20613in memory longer, and more total memory will be used. Zero disables\n\
20614caching, which can slow down startup."),
2c5b56ce 20615 NULL,
920d2a44 20616 show_dwarf2_max_cache_age,
2c5b56ce 20617 &set_dwarf2_cmdlist,
ae038cb0 20618 &show_dwarf2_cmdlist);
d97bc12b 20619
9eae7c52
TT
20620 add_setshow_boolean_cmd ("always-disassemble", class_obscure,
20621 &dwarf2_always_disassemble, _("\
20622Set whether `info address' always disassembles DWARF expressions."), _("\
20623Show whether `info address' always disassembles DWARF expressions."), _("\
20624When enabled, DWARF expressions are always printed in an assembly-like\n\
20625syntax. When disabled, expressions will be printed in a more\n\
20626conversational style, when possible."),
20627 NULL,
20628 show_dwarf2_always_disassemble,
20629 &set_dwarf2_cmdlist,
20630 &show_dwarf2_cmdlist);
20631
45cfd468
DE
20632 add_setshow_boolean_cmd ("dwarf2-read", no_class, &dwarf2_read_debug, _("\
20633Set debugging of the dwarf2 reader."), _("\
20634Show debugging of the dwarf2 reader."), _("\
20635When enabled, debugging messages are printed during dwarf2 reading\n\
20636and symtab expansion."),
20637 NULL,
20638 NULL,
20639 &setdebuglist, &showdebuglist);
20640
ccce17b0 20641 add_setshow_zuinteger_cmd ("dwarf2-die", no_class, &dwarf2_die_debug, _("\
d97bc12b
DE
20642Set debugging of the dwarf2 DIE reader."), _("\
20643Show debugging of the dwarf2 DIE reader."), _("\
20644When enabled (non-zero), DIEs are dumped after they are read in.\n\
20645The value is the maximum depth to print."),
ccce17b0
YQ
20646 NULL,
20647 NULL,
20648 &setdebuglist, &showdebuglist);
9291a0cd 20649
900e11f9
JK
20650 add_setshow_boolean_cmd ("check-physname", no_class, &check_physname, _("\
20651Set cross-checking of \"physname\" code against demangler."), _("\
20652Show cross-checking of \"physname\" code against demangler."), _("\
20653When enabled, GDB's internal \"physname\" code is checked against\n\
20654the demangler."),
20655 NULL, show_check_physname,
20656 &setdebuglist, &showdebuglist);
20657
e615022a
DE
20658 add_setshow_boolean_cmd ("use-deprecated-index-sections",
20659 no_class, &use_deprecated_index_sections, _("\
20660Set whether to use deprecated gdb_index sections."), _("\
20661Show whether to use deprecated gdb_index sections."), _("\
20662When enabled, deprecated .gdb_index sections are used anyway.\n\
20663Normally they are ignored either because of a missing feature or\n\
20664performance issue.\n\
20665Warning: This option must be enabled before gdb reads the file."),
20666 NULL,
20667 NULL,
20668 &setlist, &showlist);
20669
96d19272 20670 c = add_cmd ("gdb-index", class_files, save_gdb_index_command,
11570e71 20671 _("\
fc1a9d6e 20672Save a gdb-index file.\n\
11570e71 20673Usage: save gdb-index DIRECTORY"),
96d19272
JK
20674 &save_cmdlist);
20675 set_cmd_completer (c, filename_completer);
6502dd73 20676}
This page took 3.638387 seconds and 4 git commands to generate.