2012-11-05 Andreas Krebbel <Andreas.Krebbel@de.ibm.com>
[deliverable/binutils-gdb.git] / gdb / dwarf2read.c
CommitLineData
c906108c 1/* DWARF 2 debugging format support for GDB.
917c78fc 2
0b302171 3 Copyright (C) 1994-2012 Free Software Foundation, Inc.
c906108c
SS
4
5 Adapted by Gary Funck (gary@intrepid.com), Intrepid Technology,
6 Inc. with support from Florida State University (under contract
7 with the Ada Joint Program Office), and Silicon Graphics, Inc.
8 Initial contribution by Brent Benson, Harris Computer Systems, Inc.,
9 based on Fred Fish's (Cygnus Support) implementation of DWARF 1
7ce59000 10 support.
c906108c 11
c5aa993b 12 This file is part of GDB.
c906108c 13
c5aa993b
JM
14 This program is free software; you can redistribute it and/or modify
15 it under the terms of the GNU General Public License as published by
a9762ec7
JB
16 the Free Software Foundation; either version 3 of the License, or
17 (at your option) any later version.
c906108c 18
a9762ec7
JB
19 This program is distributed in the hope that it will be useful,
20 but WITHOUT ANY WARRANTY; without even the implied warranty of
21 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22 GNU General Public License for more details.
c906108c 23
c5aa993b 24 You should have received a copy of the GNU General Public License
a9762ec7 25 along with this program. If not, see <http://www.gnu.org/licenses/>. */
c906108c 26
21b2bd31
DE
27/* FIXME: Various die-reading functions need to be more careful with
28 reading off the end of the section.
29 E.g., load_partial_dies, read_partial_die. */
30
c906108c
SS
31#include "defs.h"
32#include "bfd.h"
c906108c
SS
33#include "symtab.h"
34#include "gdbtypes.h"
c906108c 35#include "objfiles.h"
fa8f86ff 36#include "dwarf2.h"
c906108c
SS
37#include "buildsym.h"
38#include "demangle.h"
50f182aa 39#include "gdb-demangle.h"
c906108c 40#include "expression.h"
d5166ae1 41#include "filenames.h" /* for DOSish file names */
2e276125 42#include "macrotab.h"
c906108c
SS
43#include "language.h"
44#include "complaints.h"
357e46e7 45#include "bcache.h"
4c2df51b
DJ
46#include "dwarf2expr.h"
47#include "dwarf2loc.h"
9219021c 48#include "cp-support.h"
72bf9492 49#include "hashtab.h"
ae038cb0
DJ
50#include "command.h"
51#include "gdbcmd.h"
edb3359d 52#include "block.h"
ff013f42 53#include "addrmap.h"
94af9270
KS
54#include "typeprint.h"
55#include "jv-lang.h"
ccefe4c4 56#include "psympriv.h"
9291a0cd
TT
57#include "exceptions.h"
58#include "gdb_stat.h"
96d19272 59#include "completer.h"
34eaf542 60#include "vec.h"
98bfdba5 61#include "c-lang.h"
a766d390 62#include "go-lang.h"
98bfdba5 63#include "valprint.h"
3019eac3 64#include "gdbcore.h" /* for gnutarget */
156942c7 65#include "gdb/gdb-index.h"
60d5a603 66#include <ctype.h>
cbb099e8 67#include "gdb_bfd.h"
4357ac6c 68#include "f-lang.h"
4c2df51b 69
c906108c
SS
70#include <fcntl.h>
71#include "gdb_string.h"
4bdf3d34 72#include "gdb_assert.h"
c906108c 73#include <sys/types.h>
d8151005 74
34eaf542
TT
75typedef struct symbol *symbolp;
76DEF_VEC_P (symbolp);
77
45cfd468
DE
78/* When non-zero, print basic high level tracing messages.
79 This is in contrast to the low level DIE reading of dwarf2_die_debug. */
80static int dwarf2_read_debug = 0;
81
d97bc12b 82/* When non-zero, dump DIEs after they are read in. */
ccce17b0 83static unsigned int dwarf2_die_debug = 0;
d97bc12b 84
900e11f9
JK
85/* When non-zero, cross-check physname against demangler. */
86static int check_physname = 0;
87
481860b3 88/* When non-zero, do not reject deprecated .gdb_index sections. */
e615022a 89static int use_deprecated_index_sections = 0;
481860b3 90
df8a16a1
DJ
91/* When set, the file that we're processing is known to have debugging
92 info for C++ namespaces. GCC 3.3.x did not produce this information,
93 but later versions do. */
94
95static int processing_has_namespace_info;
96
6502dd73
DJ
97static const struct objfile_data *dwarf2_objfile_data_key;
98
dce234bc
PP
99struct dwarf2_section_info
100{
101 asection *asection;
102 gdb_byte *buffer;
103 bfd_size_type size;
be391dca
TT
104 /* True if we have tried to read this section. */
105 int readin;
dce234bc
PP
106};
107
8b70b953
TT
108typedef struct dwarf2_section_info dwarf2_section_info_def;
109DEF_VEC_O (dwarf2_section_info_def);
110
9291a0cd
TT
111/* All offsets in the index are of this type. It must be
112 architecture-independent. */
113typedef uint32_t offset_type;
114
115DEF_VEC_I (offset_type);
116
156942c7
DE
117/* Ensure only legit values are used. */
118#define DW2_GDB_INDEX_SYMBOL_STATIC_SET_VALUE(cu_index, value) \
119 do { \
120 gdb_assert ((unsigned int) (value) <= 1); \
121 GDB_INDEX_SYMBOL_STATIC_SET_VALUE((cu_index), (value)); \
122 } while (0)
123
124/* Ensure only legit values are used. */
125#define DW2_GDB_INDEX_SYMBOL_KIND_SET_VALUE(cu_index, value) \
126 do { \
127 gdb_assert ((value) >= GDB_INDEX_SYMBOL_KIND_TYPE \
128 && (value) <= GDB_INDEX_SYMBOL_KIND_OTHER); \
129 GDB_INDEX_SYMBOL_KIND_SET_VALUE((cu_index), (value)); \
130 } while (0)
131
132/* Ensure we don't use more than the alloted nuber of bits for the CU. */
133#define DW2_GDB_INDEX_CU_SET_VALUE(cu_index, value) \
134 do { \
135 gdb_assert (((value) & ~GDB_INDEX_CU_MASK) == 0); \
136 GDB_INDEX_CU_SET_VALUE((cu_index), (value)); \
137 } while (0)
138
9291a0cd
TT
139/* A description of the mapped index. The file format is described in
140 a comment by the code that writes the index. */
141struct mapped_index
142{
559a7a62
JK
143 /* Index data format version. */
144 int version;
145
9291a0cd
TT
146 /* The total length of the buffer. */
147 off_t total_size;
b11b1f88 148
9291a0cd
TT
149 /* A pointer to the address table data. */
150 const gdb_byte *address_table;
b11b1f88 151
9291a0cd
TT
152 /* Size of the address table data in bytes. */
153 offset_type address_table_size;
b11b1f88 154
3876f04e
DE
155 /* The symbol table, implemented as a hash table. */
156 const offset_type *symbol_table;
b11b1f88 157
9291a0cd 158 /* Size in slots, each slot is 2 offset_types. */
3876f04e 159 offset_type symbol_table_slots;
b11b1f88 160
9291a0cd
TT
161 /* A pointer to the constant pool. */
162 const char *constant_pool;
163};
164
95554aad
TT
165typedef struct dwarf2_per_cu_data *dwarf2_per_cu_ptr;
166DEF_VEC_P (dwarf2_per_cu_ptr);
167
9cdd5dbd
DE
168/* Collection of data recorded per objfile.
169 This hangs off of dwarf2_objfile_data_key. */
170
6502dd73
DJ
171struct dwarf2_per_objfile
172{
dce234bc
PP
173 struct dwarf2_section_info info;
174 struct dwarf2_section_info abbrev;
175 struct dwarf2_section_info line;
dce234bc
PP
176 struct dwarf2_section_info loc;
177 struct dwarf2_section_info macinfo;
cf2c3c16 178 struct dwarf2_section_info macro;
dce234bc
PP
179 struct dwarf2_section_info str;
180 struct dwarf2_section_info ranges;
3019eac3 181 struct dwarf2_section_info addr;
dce234bc
PP
182 struct dwarf2_section_info frame;
183 struct dwarf2_section_info eh_frame;
9291a0cd 184 struct dwarf2_section_info gdb_index;
ae038cb0 185
8b70b953
TT
186 VEC (dwarf2_section_info_def) *types;
187
be391dca
TT
188 /* Back link. */
189 struct objfile *objfile;
190
d467dd73 191 /* Table of all the compilation units. This is used to locate
10b3939b 192 the target compilation unit of a particular reference. */
ae038cb0
DJ
193 struct dwarf2_per_cu_data **all_comp_units;
194
195 /* The number of compilation units in ALL_COMP_UNITS. */
196 int n_comp_units;
197
1fd400ff 198 /* The number of .debug_types-related CUs. */
d467dd73 199 int n_type_units;
1fd400ff 200
d467dd73 201 /* The .debug_types-related CUs (TUs). */
b4dd5633 202 struct signatured_type **all_type_units;
1fd400ff 203
f4dc4d17
DE
204 /* The number of entries in all_type_unit_groups. */
205 int n_type_unit_groups;
206
207 /* Table of type unit groups.
208 This exists to make it easy to iterate over all CUs and TU groups. */
209 struct type_unit_group **all_type_unit_groups;
210
211 /* Table of struct type_unit_group objects.
212 The hash key is the DW_AT_stmt_list value. */
213 htab_t type_unit_groups;
72dca2f5 214
348e048f
DE
215 /* A table mapping .debug_types signatures to its signatured_type entry.
216 This is NULL if the .debug_types section hasn't been read in yet. */
217 htab_t signatured_types;
218
f4dc4d17
DE
219 /* Type unit statistics, to see how well the scaling improvements
220 are doing. */
221 struct tu_stats
222 {
223 int nr_uniq_abbrev_tables;
224 int nr_symtabs;
225 int nr_symtab_sharers;
226 int nr_stmt_less_type_units;
227 } tu_stats;
228
229 /* A chain of compilation units that are currently read in, so that
230 they can be freed later. */
231 struct dwarf2_per_cu_data *read_in_chain;
232
3019eac3
DE
233 /* A table mapping DW_AT_dwo_name values to struct dwo_file objects.
234 This is NULL if the table hasn't been allocated yet. */
235 htab_t dwo_files;
236
36586728
TT
237 /* The shared '.dwz' file, if one exists. This is used when the
238 original data was compressed using 'dwz -m'. */
239 struct dwz_file *dwz_file;
240
72dca2f5
FR
241 /* A flag indicating wether this objfile has a section loaded at a
242 VMA of 0. */
243 int has_section_at_zero;
9291a0cd 244
ae2de4f8
DE
245 /* True if we are using the mapped index,
246 or we are faking it for OBJF_READNOW's sake. */
9291a0cd
TT
247 unsigned char using_index;
248
ae2de4f8 249 /* The mapped index, or NULL if .gdb_index is missing or not being used. */
9291a0cd 250 struct mapped_index *index_table;
98bfdba5 251
7b9f3c50
DE
252 /* When using index_table, this keeps track of all quick_file_names entries.
253 TUs can share line table entries with CUs or other TUs, and there can be
254 a lot more TUs than unique line tables, so we maintain a separate table
255 of all line table entries to support the sharing. */
256 htab_t quick_file_names_table;
257
98bfdba5
PA
258 /* Set during partial symbol reading, to prevent queueing of full
259 symbols. */
260 int reading_partial_symbols;
673bfd45 261
dee91e82 262 /* Table mapping type DIEs to their struct type *.
673bfd45 263 This is NULL if not allocated yet.
dee91e82
DE
264 The mapping is done via (CU/TU signature + DIE offset) -> type. */
265 htab_t die_type_hash;
95554aad
TT
266
267 /* The CUs we recently read. */
268 VEC (dwarf2_per_cu_ptr) *just_read_cus;
6502dd73
DJ
269};
270
271static struct dwarf2_per_objfile *dwarf2_per_objfile;
c906108c 272
251d32d9 273/* Default names of the debugging sections. */
c906108c 274
233a11ab
CS
275/* Note that if the debugging section has been compressed, it might
276 have a name like .zdebug_info. */
277
9cdd5dbd
DE
278static const struct dwarf2_debug_sections dwarf2_elf_names =
279{
251d32d9
TG
280 { ".debug_info", ".zdebug_info" },
281 { ".debug_abbrev", ".zdebug_abbrev" },
282 { ".debug_line", ".zdebug_line" },
283 { ".debug_loc", ".zdebug_loc" },
284 { ".debug_macinfo", ".zdebug_macinfo" },
cf2c3c16 285 { ".debug_macro", ".zdebug_macro" },
251d32d9
TG
286 { ".debug_str", ".zdebug_str" },
287 { ".debug_ranges", ".zdebug_ranges" },
288 { ".debug_types", ".zdebug_types" },
3019eac3 289 { ".debug_addr", ".zdebug_addr" },
251d32d9
TG
290 { ".debug_frame", ".zdebug_frame" },
291 { ".eh_frame", NULL },
24d3216f
TT
292 { ".gdb_index", ".zgdb_index" },
293 23
251d32d9 294};
c906108c 295
3019eac3
DE
296/* List of DWO sections. */
297
298static const struct dwo_section_names
299{
300 struct dwarf2_section_names abbrev_dwo;
301 struct dwarf2_section_names info_dwo;
302 struct dwarf2_section_names line_dwo;
303 struct dwarf2_section_names loc_dwo;
09262596
DE
304 struct dwarf2_section_names macinfo_dwo;
305 struct dwarf2_section_names macro_dwo;
3019eac3
DE
306 struct dwarf2_section_names str_dwo;
307 struct dwarf2_section_names str_offsets_dwo;
308 struct dwarf2_section_names types_dwo;
309}
310dwo_section_names =
311{
312 { ".debug_abbrev.dwo", ".zdebug_abbrev.dwo" },
313 { ".debug_info.dwo", ".zdebug_info.dwo" },
314 { ".debug_line.dwo", ".zdebug_line.dwo" },
315 { ".debug_loc.dwo", ".zdebug_loc.dwo" },
09262596
DE
316 { ".debug_macinfo.dwo", ".zdebug_macinfo.dwo" },
317 { ".debug_macro.dwo", ".zdebug_macro.dwo" },
3019eac3
DE
318 { ".debug_str.dwo", ".zdebug_str.dwo" },
319 { ".debug_str_offsets.dwo", ".zdebug_str_offsets.dwo" },
320 { ".debug_types.dwo", ".zdebug_types.dwo" },
321};
322
c906108c
SS
323/* local data types */
324
107d2387
AC
325/* The data in a compilation unit header, after target2host
326 translation, looks like this. */
c906108c 327struct comp_unit_head
a738430d 328{
c764a876 329 unsigned int length;
a738430d 330 short version;
a738430d
MK
331 unsigned char addr_size;
332 unsigned char signed_addr_p;
b64f50a1 333 sect_offset abbrev_offset;
57349743 334
a738430d
MK
335 /* Size of file offsets; either 4 or 8. */
336 unsigned int offset_size;
57349743 337
a738430d
MK
338 /* Size of the length field; either 4 or 12. */
339 unsigned int initial_length_size;
57349743 340
a738430d
MK
341 /* Offset to the first byte of this compilation unit header in the
342 .debug_info section, for resolving relative reference dies. */
b64f50a1 343 sect_offset offset;
57349743 344
d00adf39
DE
345 /* Offset to first die in this cu from the start of the cu.
346 This will be the first byte following the compilation unit header. */
b64f50a1 347 cu_offset first_die_offset;
a738430d 348};
c906108c 349
3da10d80
KS
350/* Type used for delaying computation of method physnames.
351 See comments for compute_delayed_physnames. */
352struct delayed_method_info
353{
354 /* The type to which the method is attached, i.e., its parent class. */
355 struct type *type;
356
357 /* The index of the method in the type's function fieldlists. */
358 int fnfield_index;
359
360 /* The index of the method in the fieldlist. */
361 int index;
362
363 /* The name of the DIE. */
364 const char *name;
365
366 /* The DIE associated with this method. */
367 struct die_info *die;
368};
369
370typedef struct delayed_method_info delayed_method_info;
371DEF_VEC_O (delayed_method_info);
372
e7c27a73
DJ
373/* Internal state when decoding a particular compilation unit. */
374struct dwarf2_cu
375{
376 /* The objfile containing this compilation unit. */
377 struct objfile *objfile;
378
d00adf39 379 /* The header of the compilation unit. */
e7c27a73 380 struct comp_unit_head header;
e142c38c 381
d00adf39
DE
382 /* Base address of this compilation unit. */
383 CORE_ADDR base_address;
384
385 /* Non-zero if base_address has been set. */
386 int base_known;
387
e142c38c
DJ
388 /* The language we are debugging. */
389 enum language language;
390 const struct language_defn *language_defn;
391
b0f35d58
DL
392 const char *producer;
393
e142c38c
DJ
394 /* The generic symbol table building routines have separate lists for
395 file scope symbols and all all other scopes (local scopes). So
396 we need to select the right one to pass to add_symbol_to_list().
397 We do it by keeping a pointer to the correct list in list_in_scope.
398
399 FIXME: The original dwarf code just treated the file scope as the
400 first local scope, and all other local scopes as nested local
401 scopes, and worked fine. Check to see if we really need to
402 distinguish these in buildsym.c. */
403 struct pending **list_in_scope;
404
433df2d4
DE
405 /* The abbrev table for this CU.
406 Normally this points to the abbrev table in the objfile.
407 But if DWO_UNIT is non-NULL this is the abbrev table in the DWO file. */
408 struct abbrev_table *abbrev_table;
72bf9492 409
b64f50a1
JK
410 /* Hash table holding all the loaded partial DIEs
411 with partial_die->offset.SECT_OFF as hash. */
72bf9492
DJ
412 htab_t partial_dies;
413
414 /* Storage for things with the same lifetime as this read-in compilation
415 unit, including partial DIEs. */
416 struct obstack comp_unit_obstack;
417
ae038cb0
DJ
418 /* When multiple dwarf2_cu structures are living in memory, this field
419 chains them all together, so that they can be released efficiently.
420 We will probably also want a generation counter so that most-recently-used
421 compilation units are cached... */
422 struct dwarf2_per_cu_data *read_in_chain;
423
424 /* Backchain to our per_cu entry if the tree has been built. */
425 struct dwarf2_per_cu_data *per_cu;
426
427 /* How many compilation units ago was this CU last referenced? */
428 int last_used;
429
b64f50a1
JK
430 /* A hash table of DIE cu_offset for following references with
431 die_info->offset.sect_off as hash. */
51545339 432 htab_t die_hash;
10b3939b
DJ
433
434 /* Full DIEs if read in. */
435 struct die_info *dies;
436
437 /* A set of pointers to dwarf2_per_cu_data objects for compilation
438 units referenced by this one. Only set during full symbol processing;
439 partial symbol tables do not have dependencies. */
440 htab_t dependencies;
441
cb1df416
DJ
442 /* Header data from the line table, during full symbol processing. */
443 struct line_header *line_header;
444
3da10d80
KS
445 /* A list of methods which need to have physnames computed
446 after all type information has been read. */
447 VEC (delayed_method_info) *method_list;
448
96408a79
SA
449 /* To be copied to symtab->call_site_htab. */
450 htab_t call_site_htab;
451
034e5797
DE
452 /* Non-NULL if this CU came from a DWO file.
453 There is an invariant here that is important to remember:
454 Except for attributes copied from the top level DIE in the "main"
455 (or "stub") file in preparation for reading the DWO file
456 (e.g., DW_AT_GNU_addr_base), we KISS: there is only *one* CU.
457 Either there isn't a DWO file (in which case this is NULL and the point
458 is moot), or there is and either we're not going to read it (in which
459 case this is NULL) or there is and we are reading it (in which case this
460 is non-NULL). */
3019eac3
DE
461 struct dwo_unit *dwo_unit;
462
463 /* The DW_AT_addr_base attribute if present, zero otherwise
464 (zero is a valid value though).
465 Note this value comes from the stub CU/TU's DIE. */
466 ULONGEST addr_base;
467
2e3cf129
DE
468 /* The DW_AT_ranges_base attribute if present, zero otherwise
469 (zero is a valid value though).
470 Note this value comes from the stub CU/TU's DIE.
471 Also note that the value is zero in the non-DWO case so this value can
472 be used without needing to know whether DWO files are in use or not. */
473 ULONGEST ranges_base;
474
ae038cb0
DJ
475 /* Mark used when releasing cached dies. */
476 unsigned int mark : 1;
477
8be455d7
JK
478 /* This CU references .debug_loc. See the symtab->locations_valid field.
479 This test is imperfect as there may exist optimized debug code not using
480 any location list and still facing inlining issues if handled as
481 unoptimized code. For a future better test see GCC PR other/32998. */
8be455d7 482 unsigned int has_loclist : 1;
ba919b58 483
685b1105
JK
484 /* These cache the results for producer_is_gxx_lt_4_6 and producer_is_icc.
485 CHECKED_PRODUCER is set if both PRODUCER_IS_GXX_LT_4_6 and PRODUCER_IS_ICC
486 are valid. This information is cached because profiling CU expansion
487 showed excessive time spent in producer_is_gxx_lt_4_6. */
ba919b58
TT
488 unsigned int checked_producer : 1;
489 unsigned int producer_is_gxx_lt_4_6 : 1;
685b1105 490 unsigned int producer_is_icc : 1;
e7c27a73
DJ
491};
492
10b3939b
DJ
493/* Persistent data held for a compilation unit, even when not
494 processing it. We put a pointer to this structure in the
28dee7f5 495 read_symtab_private field of the psymtab. */
10b3939b 496
ae038cb0
DJ
497struct dwarf2_per_cu_data
498{
36586728 499 /* The start offset and length of this compilation unit.
45452591 500 NOTE: Unlike comp_unit_head.length, this length includes
3019eac3
DE
501 initial_length_size.
502 If the DIE refers to a DWO file, this is always of the original die,
503 not the DWO file. */
b64f50a1 504 sect_offset offset;
36586728 505 unsigned int length;
ae038cb0
DJ
506
507 /* Flag indicating this compilation unit will be read in before
508 any of the current compilation units are processed. */
c764a876 509 unsigned int queued : 1;
ae038cb0 510
0d99eb77
DE
511 /* This flag will be set when reading partial DIEs if we need to load
512 absolutely all DIEs for this compilation unit, instead of just the ones
513 we think are interesting. It gets set if we look for a DIE in the
5afb4e99
DJ
514 hash table and don't find it. */
515 unsigned int load_all_dies : 1;
516
3019eac3
DE
517 /* Non-zero if this CU is from .debug_types. */
518 unsigned int is_debug_types : 1;
519
36586728
TT
520 /* Non-zero if this CU is from the .dwz file. */
521 unsigned int is_dwz : 1;
522
3019eac3
DE
523 /* The section this CU/TU lives in.
524 If the DIE refers to a DWO file, this is always the original die,
525 not the DWO file. */
526 struct dwarf2_section_info *info_or_types_section;
348e048f 527
17ea53c3
JK
528 /* Set to non-NULL iff this CU is currently loaded. When it gets freed out
529 of the CU cache it gets reset to NULL again. */
ae038cb0 530 struct dwarf2_cu *cu;
1c379e20 531
9cdd5dbd
DE
532 /* The corresponding objfile.
533 Normally we can get the objfile from dwarf2_per_objfile.
534 However we can enter this file with just a "per_cu" handle. */
9291a0cd
TT
535 struct objfile *objfile;
536
537 /* When using partial symbol tables, the 'psymtab' field is active.
538 Otherwise the 'quick' field is active. */
539 union
540 {
541 /* The partial symbol table associated with this compilation unit,
95554aad 542 or NULL for unread partial units. */
9291a0cd
TT
543 struct partial_symtab *psymtab;
544
545 /* Data needed by the "quick" functions. */
546 struct dwarf2_per_cu_quick_data *quick;
547 } v;
95554aad 548
f4dc4d17
DE
549 union
550 {
551 /* The CUs we import using DW_TAG_imported_unit. This is filled in
552 while reading psymtabs, used to compute the psymtab dependencies,
553 and then cleared. Then it is filled in again while reading full
554 symbols, and only deleted when the objfile is destroyed. */
555 VEC (dwarf2_per_cu_ptr) *imported_symtabs;
556
557 /* Type units are grouped by their DW_AT_stmt_list entry so that they
558 can share them. If this is a TU, this points to the containing
559 symtab. */
560 struct type_unit_group *type_unit_group;
561 } s;
ae038cb0
DJ
562};
563
348e048f
DE
564/* Entry in the signatured_types hash table. */
565
566struct signatured_type
567{
42e7ad6c
DE
568 /* The "per_cu" object of this type.
569 N.B.: This is the first member so that it's easy to convert pointers
570 between them. */
571 struct dwarf2_per_cu_data per_cu;
572
3019eac3 573 /* The type's signature. */
348e048f
DE
574 ULONGEST signature;
575
3019eac3
DE
576 /* Offset in the TU of the type's DIE, as read from the TU header.
577 If the definition lives in a DWO file, this value is unusable. */
578 cu_offset type_offset_in_tu;
579
580 /* Offset in the section of the type's DIE.
581 If the definition lives in a DWO file, this is the offset in the
582 .debug_types.dwo section.
583 The value is zero until the actual value is known.
584 Zero is otherwise not a valid section offset. */
585 sect_offset type_offset_in_section;
348e048f
DE
586};
587
094b34ac
DE
588/* A struct that can be used as a hash key for tables based on DW_AT_stmt_list.
589 This includes type_unit_group and quick_file_names. */
590
591struct stmt_list_hash
592{
593 /* The DWO unit this table is from or NULL if there is none. */
594 struct dwo_unit *dwo_unit;
595
596 /* Offset in .debug_line or .debug_line.dwo. */
597 sect_offset line_offset;
598};
599
f4dc4d17
DE
600/* Each element of dwarf2_per_objfile->type_unit_groups is a pointer to
601 an object of this type. */
602
603struct type_unit_group
604{
605 /* dwarf2read.c's main "handle" on the symtab.
606 To simplify things we create an artificial CU that "includes" all the
607 type units using this stmt_list so that the rest of the code still has
608 a "per_cu" handle on the symtab.
609 This PER_CU is recognized by having no section. */
610#define IS_TYPE_UNIT_GROUP(per_cu) ((per_cu)->info_or_types_section == NULL)
094b34ac
DE
611 struct dwarf2_per_cu_data per_cu;
612
613 union
614 {
615 /* The TUs that share this DW_AT_stmt_list entry.
616 This is added to while parsing type units to build partial symtabs,
617 and is deleted afterwards and not used again. */
618 VEC (dwarf2_per_cu_ptr) *tus;
f4dc4d17 619
094b34ac
DE
620 /* When reading the line table in "quick" functions, we need a real TU.
621 Any will do, we know they all share the same DW_AT_stmt_list entry.
622 For simplicity's sake, we pick the first one. */
623 struct dwarf2_per_cu_data *first_tu;
624 } t;
f4dc4d17
DE
625
626 /* The primary symtab.
094b34ac
DE
627 Type units in a group needn't all be defined in the same source file,
628 so we create an essentially anonymous symtab as the primary symtab. */
f4dc4d17
DE
629 struct symtab *primary_symtab;
630
094b34ac
DE
631 /* The data used to construct the hash key. */
632 struct stmt_list_hash hash;
f4dc4d17
DE
633
634 /* The number of symtabs from the line header.
635 The value here must match line_header.num_file_names. */
636 unsigned int num_symtabs;
637
638 /* The symbol tables for this TU (obtained from the files listed in
639 DW_AT_stmt_list).
640 WARNING: The order of entries here must match the order of entries
641 in the line header. After the first TU using this type_unit_group, the
642 line header for the subsequent TUs is recreated from this. This is done
643 because we need to use the same symtabs for each TU using the same
644 DW_AT_stmt_list value. Also note that symtabs may be repeated here,
645 there's no guarantee the line header doesn't have duplicate entries. */
646 struct symtab **symtabs;
647};
648
3019eac3
DE
649/* These sections are what may appear in a "dwo" file. */
650
651struct dwo_sections
652{
653 struct dwarf2_section_info abbrev;
654 struct dwarf2_section_info info;
655 struct dwarf2_section_info line;
656 struct dwarf2_section_info loc;
09262596
DE
657 struct dwarf2_section_info macinfo;
658 struct dwarf2_section_info macro;
3019eac3
DE
659 struct dwarf2_section_info str;
660 struct dwarf2_section_info str_offsets;
661 VEC (dwarf2_section_info_def) *types;
662};
663
664/* Common bits of DWO CUs/TUs. */
665
666struct dwo_unit
667{
668 /* Backlink to the containing struct dwo_file. */
669 struct dwo_file *dwo_file;
670
671 /* The "id" that distinguishes this CU/TU.
672 .debug_info calls this "dwo_id", .debug_types calls this "signature".
673 Since signatures came first, we stick with it for consistency. */
674 ULONGEST signature;
675
676 /* The section this CU/TU lives in, in the DWO file. */
677 struct dwarf2_section_info *info_or_types_section;
678
679 /* Same as dwarf2_per_cu_data:{offset,length} but for the DWO section. */
680 sect_offset offset;
681 unsigned int length;
682
683 /* For types, offset in the type's DIE of the type defined by this TU. */
684 cu_offset type_offset_in_tu;
685};
686
687/* Data for one DWO file. */
688
689struct dwo_file
690{
691 /* The DW_AT_GNU_dwo_name attribute.
692 We don't manage space for this, it's an attribute. */
693 const char *dwo_name;
694
695 /* The bfd, when the file is open. Otherwise this is NULL. */
696 bfd *dwo_bfd;
697
698 /* Section info for this file. */
699 struct dwo_sections sections;
700
701 /* Table of CUs in the file.
702 Each element is a struct dwo_unit. */
703 htab_t cus;
704
705 /* Table of TUs in the file.
706 Each element is a struct dwo_unit. */
707 htab_t tus;
708};
709
36586728
TT
710/* This represents a '.dwz' file. */
711
712struct dwz_file
713{
714 /* A dwz file can only contain a few sections. */
715 struct dwarf2_section_info abbrev;
716 struct dwarf2_section_info info;
717 struct dwarf2_section_info str;
718 struct dwarf2_section_info line;
719 struct dwarf2_section_info macro;
2ec9a5e0 720 struct dwarf2_section_info gdb_index;
36586728
TT
721
722 /* The dwz's BFD. */
723 bfd *dwz_bfd;
724};
725
0963b4bd
MS
726/* Struct used to pass misc. parameters to read_die_and_children, et
727 al. which are used for both .debug_info and .debug_types dies.
728 All parameters here are unchanging for the life of the call. This
dee91e82 729 struct exists to abstract away the constant parameters of die reading. */
93311388
DE
730
731struct die_reader_specs
732{
dee91e82 733 /* die_section->asection->owner. */
93311388
DE
734 bfd* abfd;
735
736 /* The CU of the DIE we are parsing. */
737 struct dwarf2_cu *cu;
738
3019eac3
DE
739 /* Non-NULL if reading a DWO file. */
740 struct dwo_file *dwo_file;
741
dee91e82 742 /* The section the die comes from.
3019eac3 743 This is either .debug_info or .debug_types, or the .dwo variants. */
dee91e82
DE
744 struct dwarf2_section_info *die_section;
745
746 /* die_section->buffer. */
747 gdb_byte *buffer;
f664829e
DE
748
749 /* The end of the buffer. */
750 const gdb_byte *buffer_end;
93311388
DE
751};
752
fd820528 753/* Type of function passed to init_cutu_and_read_dies, et.al. */
dee91e82
DE
754typedef void (die_reader_func_ftype) (const struct die_reader_specs *reader,
755 gdb_byte *info_ptr,
756 struct die_info *comp_unit_die,
757 int has_children,
758 void *data);
759
debd256d
JB
760/* The line number information for a compilation unit (found in the
761 .debug_line section) begins with a "statement program header",
762 which contains the following information. */
763struct line_header
764{
765 unsigned int total_length;
766 unsigned short version;
767 unsigned int header_length;
768 unsigned char minimum_instruction_length;
2dc7f7b3 769 unsigned char maximum_ops_per_instruction;
debd256d
JB
770 unsigned char default_is_stmt;
771 int line_base;
772 unsigned char line_range;
773 unsigned char opcode_base;
774
775 /* standard_opcode_lengths[i] is the number of operands for the
776 standard opcode whose value is i. This means that
777 standard_opcode_lengths[0] is unused, and the last meaningful
778 element is standard_opcode_lengths[opcode_base - 1]. */
779 unsigned char *standard_opcode_lengths;
780
781 /* The include_directories table. NOTE! These strings are not
782 allocated with xmalloc; instead, they are pointers into
783 debug_line_buffer. If you try to free them, `free' will get
784 indigestion. */
785 unsigned int num_include_dirs, include_dirs_size;
786 char **include_dirs;
787
788 /* The file_names table. NOTE! These strings are not allocated
789 with xmalloc; instead, they are pointers into debug_line_buffer.
790 Don't try to free them directly. */
791 unsigned int num_file_names, file_names_size;
792 struct file_entry
c906108c 793 {
debd256d
JB
794 char *name;
795 unsigned int dir_index;
796 unsigned int mod_time;
797 unsigned int length;
aaa75496 798 int included_p; /* Non-zero if referenced by the Line Number Program. */
cb1df416 799 struct symtab *symtab; /* The associated symbol table, if any. */
debd256d
JB
800 } *file_names;
801
802 /* The start and end of the statement program following this
6502dd73 803 header. These point into dwarf2_per_objfile->line_buffer. */
fe1b8b76 804 gdb_byte *statement_program_start, *statement_program_end;
debd256d 805};
c906108c
SS
806
807/* When we construct a partial symbol table entry we only
0963b4bd 808 need this much information. */
c906108c
SS
809struct partial_die_info
810 {
72bf9492 811 /* Offset of this DIE. */
b64f50a1 812 sect_offset offset;
72bf9492
DJ
813
814 /* DWARF-2 tag for this DIE. */
815 ENUM_BITFIELD(dwarf_tag) tag : 16;
816
72bf9492
DJ
817 /* Assorted flags describing the data found in this DIE. */
818 unsigned int has_children : 1;
819 unsigned int is_external : 1;
820 unsigned int is_declaration : 1;
821 unsigned int has_type : 1;
822 unsigned int has_specification : 1;
823 unsigned int has_pc_info : 1;
481860b3 824 unsigned int may_be_inlined : 1;
72bf9492
DJ
825
826 /* Flag set if the SCOPE field of this structure has been
827 computed. */
828 unsigned int scope_set : 1;
829
fa4028e9
JB
830 /* Flag set if the DIE has a byte_size attribute. */
831 unsigned int has_byte_size : 1;
832
98bfdba5
PA
833 /* Flag set if any of the DIE's children are template arguments. */
834 unsigned int has_template_arguments : 1;
835
abc72ce4
DE
836 /* Flag set if fixup_partial_die has been called on this die. */
837 unsigned int fixup_called : 1;
838
36586728
TT
839 /* Flag set if DW_TAG_imported_unit uses DW_FORM_GNU_ref_alt. */
840 unsigned int is_dwz : 1;
841
842 /* Flag set if spec_offset uses DW_FORM_GNU_ref_alt. */
843 unsigned int spec_is_dwz : 1;
844
72bf9492 845 /* The name of this DIE. Normally the value of DW_AT_name, but
94af9270 846 sometimes a default name for unnamed DIEs. */
c906108c 847 char *name;
72bf9492 848
abc72ce4
DE
849 /* The linkage name, if present. */
850 const char *linkage_name;
851
72bf9492
DJ
852 /* The scope to prepend to our children. This is generally
853 allocated on the comp_unit_obstack, so will disappear
854 when this compilation unit leaves the cache. */
855 char *scope;
856
95554aad
TT
857 /* Some data associated with the partial DIE. The tag determines
858 which field is live. */
859 union
860 {
861 /* The location description associated with this DIE, if any. */
862 struct dwarf_block *locdesc;
863 /* The offset of an import, for DW_TAG_imported_unit. */
864 sect_offset offset;
865 } d;
72bf9492
DJ
866
867 /* If HAS_PC_INFO, the PC range associated with this DIE. */
c906108c
SS
868 CORE_ADDR lowpc;
869 CORE_ADDR highpc;
72bf9492 870
93311388 871 /* Pointer into the info_buffer (or types_buffer) pointing at the target of
72bf9492 872 DW_AT_sibling, if any. */
abc72ce4
DE
873 /* NOTE: This member isn't strictly necessary, read_partial_die could
874 return DW_AT_sibling values to its caller load_partial_dies. */
fe1b8b76 875 gdb_byte *sibling;
72bf9492
DJ
876
877 /* If HAS_SPECIFICATION, the offset of the DIE referred to by
878 DW_AT_specification (or DW_AT_abstract_origin or
879 DW_AT_extension). */
b64f50a1 880 sect_offset spec_offset;
72bf9492
DJ
881
882 /* Pointers to this DIE's parent, first child, and next sibling,
883 if any. */
884 struct partial_die_info *die_parent, *die_child, *die_sibling;
c906108c
SS
885 };
886
0963b4bd 887/* This data structure holds the information of an abbrev. */
c906108c
SS
888struct abbrev_info
889 {
890 unsigned int number; /* number identifying abbrev */
891 enum dwarf_tag tag; /* dwarf tag */
f3dd6933
DJ
892 unsigned short has_children; /* boolean */
893 unsigned short num_attrs; /* number of attributes */
c906108c
SS
894 struct attr_abbrev *attrs; /* an array of attribute descriptions */
895 struct abbrev_info *next; /* next in chain */
896 };
897
898struct attr_abbrev
899 {
9d25dd43
DE
900 ENUM_BITFIELD(dwarf_attribute) name : 16;
901 ENUM_BITFIELD(dwarf_form) form : 16;
c906108c
SS
902 };
903
433df2d4
DE
904/* Size of abbrev_table.abbrev_hash_table. */
905#define ABBREV_HASH_SIZE 121
906
907/* Top level data structure to contain an abbreviation table. */
908
909struct abbrev_table
910{
f4dc4d17
DE
911 /* Where the abbrev table came from.
912 This is used as a sanity check when the table is used. */
433df2d4
DE
913 sect_offset offset;
914
915 /* Storage for the abbrev table. */
916 struct obstack abbrev_obstack;
917
918 /* Hash table of abbrevs.
919 This is an array of size ABBREV_HASH_SIZE allocated in abbrev_obstack.
920 It could be statically allocated, but the previous code didn't so we
921 don't either. */
922 struct abbrev_info **abbrevs;
923};
924
0963b4bd 925/* Attributes have a name and a value. */
b60c80d6
DJ
926struct attribute
927 {
9d25dd43 928 ENUM_BITFIELD(dwarf_attribute) name : 16;
8285870a
JK
929 ENUM_BITFIELD(dwarf_form) form : 15;
930
931 /* Has DW_STRING already been updated by dwarf2_canonicalize_name? This
932 field should be in u.str (existing only for DW_STRING) but it is kept
933 here for better struct attribute alignment. */
934 unsigned int string_is_canonical : 1;
935
b60c80d6
DJ
936 union
937 {
938 char *str;
939 struct dwarf_block *blk;
43bbcdc2
PH
940 ULONGEST unsnd;
941 LONGEST snd;
b60c80d6 942 CORE_ADDR addr;
348e048f 943 struct signatured_type *signatured_type;
b60c80d6
DJ
944 }
945 u;
946 };
947
0963b4bd 948/* This data structure holds a complete die structure. */
c906108c
SS
949struct die_info
950 {
76815b17
DE
951 /* DWARF-2 tag for this DIE. */
952 ENUM_BITFIELD(dwarf_tag) tag : 16;
953
954 /* Number of attributes */
98bfdba5
PA
955 unsigned char num_attrs;
956
957 /* True if we're presently building the full type name for the
958 type derived from this DIE. */
959 unsigned char building_fullname : 1;
76815b17
DE
960
961 /* Abbrev number */
962 unsigned int abbrev;
963
93311388 964 /* Offset in .debug_info or .debug_types section. */
b64f50a1 965 sect_offset offset;
78ba4af6
JB
966
967 /* The dies in a compilation unit form an n-ary tree. PARENT
968 points to this die's parent; CHILD points to the first child of
969 this node; and all the children of a given node are chained
4950bc1c 970 together via their SIBLING fields. */
639d11d3
DC
971 struct die_info *child; /* Its first child, if any. */
972 struct die_info *sibling; /* Its next sibling, if any. */
973 struct die_info *parent; /* Its parent, if any. */
c906108c 974
b60c80d6
DJ
975 /* An array of attributes, with NUM_ATTRS elements. There may be
976 zero, but it's not common and zero-sized arrays are not
977 sufficiently portable C. */
978 struct attribute attrs[1];
c906108c
SS
979 };
980
0963b4bd 981/* Get at parts of an attribute structure. */
c906108c
SS
982
983#define DW_STRING(attr) ((attr)->u.str)
8285870a 984#define DW_STRING_IS_CANONICAL(attr) ((attr)->string_is_canonical)
c906108c
SS
985#define DW_UNSND(attr) ((attr)->u.unsnd)
986#define DW_BLOCK(attr) ((attr)->u.blk)
987#define DW_SND(attr) ((attr)->u.snd)
988#define DW_ADDR(attr) ((attr)->u.addr)
348e048f 989#define DW_SIGNATURED_TYPE(attr) ((attr)->u.signatured_type)
c906108c 990
0963b4bd 991/* Blocks are a bunch of untyped bytes. */
c906108c
SS
992struct dwarf_block
993 {
56eb65bd 994 size_t size;
1d6edc3c
JK
995
996 /* Valid only if SIZE is not zero. */
fe1b8b76 997 gdb_byte *data;
c906108c
SS
998 };
999
c906108c
SS
1000#ifndef ATTR_ALLOC_CHUNK
1001#define ATTR_ALLOC_CHUNK 4
1002#endif
1003
c906108c
SS
1004/* Allocate fields for structs, unions and enums in this size. */
1005#ifndef DW_FIELD_ALLOC_CHUNK
1006#define DW_FIELD_ALLOC_CHUNK 4
1007#endif
1008
c906108c
SS
1009/* FIXME: We might want to set this from BFD via bfd_arch_bits_per_byte,
1010 but this would require a corresponding change in unpack_field_as_long
1011 and friends. */
1012static int bits_per_byte = 8;
1013
1014/* The routines that read and process dies for a C struct or C++ class
1015 pass lists of data member fields and lists of member function fields
1016 in an instance of a field_info structure, as defined below. */
1017struct field_info
c5aa993b 1018 {
0963b4bd 1019 /* List of data member and baseclasses fields. */
c5aa993b
JM
1020 struct nextfield
1021 {
1022 struct nextfield *next;
1023 int accessibility;
1024 int virtuality;
1025 struct field field;
1026 }
7d0ccb61 1027 *fields, *baseclasses;
c906108c 1028
7d0ccb61 1029 /* Number of fields (including baseclasses). */
c5aa993b 1030 int nfields;
c906108c 1031
c5aa993b
JM
1032 /* Number of baseclasses. */
1033 int nbaseclasses;
c906108c 1034
c5aa993b
JM
1035 /* Set if the accesibility of one of the fields is not public. */
1036 int non_public_fields;
c906108c 1037
c5aa993b
JM
1038 /* Member function fields array, entries are allocated in the order they
1039 are encountered in the object file. */
1040 struct nextfnfield
1041 {
1042 struct nextfnfield *next;
1043 struct fn_field fnfield;
1044 }
1045 *fnfields;
c906108c 1046
c5aa993b
JM
1047 /* Member function fieldlist array, contains name of possibly overloaded
1048 member function, number of overloaded member functions and a pointer
1049 to the head of the member function field chain. */
1050 struct fnfieldlist
1051 {
1052 char *name;
1053 int length;
1054 struct nextfnfield *head;
1055 }
1056 *fnfieldlists;
c906108c 1057
c5aa993b
JM
1058 /* Number of entries in the fnfieldlists array. */
1059 int nfnfields;
98751a41
JK
1060
1061 /* typedefs defined inside this class. TYPEDEF_FIELD_LIST contains head of
1062 a NULL terminated list of TYPEDEF_FIELD_LIST_COUNT elements. */
1063 struct typedef_field_list
1064 {
1065 struct typedef_field field;
1066 struct typedef_field_list *next;
1067 }
1068 *typedef_field_list;
1069 unsigned typedef_field_list_count;
c5aa993b 1070 };
c906108c 1071
10b3939b
DJ
1072/* One item on the queue of compilation units to read in full symbols
1073 for. */
1074struct dwarf2_queue_item
1075{
1076 struct dwarf2_per_cu_data *per_cu;
95554aad 1077 enum language pretend_language;
10b3939b
DJ
1078 struct dwarf2_queue_item *next;
1079};
1080
1081/* The current queue. */
1082static struct dwarf2_queue_item *dwarf2_queue, *dwarf2_queue_tail;
1083
ae038cb0
DJ
1084/* Loaded secondary compilation units are kept in memory until they
1085 have not been referenced for the processing of this many
1086 compilation units. Set this to zero to disable caching. Cache
1087 sizes of up to at least twenty will improve startup time for
1088 typical inter-CU-reference binaries, at an obvious memory cost. */
1089static int dwarf2_max_cache_age = 5;
920d2a44
AC
1090static void
1091show_dwarf2_max_cache_age (struct ui_file *file, int from_tty,
1092 struct cmd_list_element *c, const char *value)
1093{
3e43a32a
MS
1094 fprintf_filtered (file, _("The upper bound on the age of cached "
1095 "dwarf2 compilation units is %s.\n"),
920d2a44
AC
1096 value);
1097}
1098
ae038cb0 1099
0963b4bd 1100/* Various complaints about symbol reading that don't abort the process. */
c906108c 1101
4d3c2250
KB
1102static void
1103dwarf2_statement_list_fits_in_line_number_section_complaint (void)
2e276125 1104{
4d3c2250 1105 complaint (&symfile_complaints,
e2e0b3e5 1106 _("statement list doesn't fit in .debug_line section"));
4d3c2250
KB
1107}
1108
25e43795
DJ
1109static void
1110dwarf2_debug_line_missing_file_complaint (void)
1111{
1112 complaint (&symfile_complaints,
1113 _(".debug_line section has line data without a file"));
1114}
1115
59205f5a
JB
1116static void
1117dwarf2_debug_line_missing_end_sequence_complaint (void)
1118{
1119 complaint (&symfile_complaints,
3e43a32a
MS
1120 _(".debug_line section has line "
1121 "program sequence without an end"));
59205f5a
JB
1122}
1123
4d3c2250
KB
1124static void
1125dwarf2_complex_location_expr_complaint (void)
2e276125 1126{
e2e0b3e5 1127 complaint (&symfile_complaints, _("location expression too complex"));
4d3c2250
KB
1128}
1129
4d3c2250
KB
1130static void
1131dwarf2_const_value_length_mismatch_complaint (const char *arg1, int arg2,
1132 int arg3)
2e276125 1133{
4d3c2250 1134 complaint (&symfile_complaints,
3e43a32a
MS
1135 _("const value length mismatch for '%s', got %d, expected %d"),
1136 arg1, arg2, arg3);
4d3c2250
KB
1137}
1138
1139static void
f664829e 1140dwarf2_section_buffer_overflow_complaint (struct dwarf2_section_info *section)
2e276125 1141{
4d3c2250 1142 complaint (&symfile_complaints,
f664829e
DE
1143 _("debug info runs off end of %s section"
1144 " [in module %s]"),
1145 section->asection->name,
1146 bfd_get_filename (section->asection->owner));
4d3c2250
KB
1147}
1148
1149static void
1150dwarf2_macro_malformed_definition_complaint (const char *arg1)
8e19ed76 1151{
4d3c2250 1152 complaint (&symfile_complaints,
3e43a32a
MS
1153 _("macro debug info contains a "
1154 "malformed macro definition:\n`%s'"),
4d3c2250
KB
1155 arg1);
1156}
1157
1158static void
1159dwarf2_invalid_attrib_class_complaint (const char *arg1, const char *arg2)
8b2dbe47 1160{
4d3c2250 1161 complaint (&symfile_complaints,
3e43a32a
MS
1162 _("invalid attribute class or form for '%s' in '%s'"),
1163 arg1, arg2);
4d3c2250 1164}
c906108c 1165
c906108c
SS
1166/* local function prototypes */
1167
4efb68b1 1168static void dwarf2_locate_sections (bfd *, asection *, void *);
c906108c 1169
aaa75496
JB
1170static void dwarf2_create_include_psymtab (char *, struct partial_symtab *,
1171 struct objfile *);
1172
918dd910
JK
1173static void dwarf2_find_base_address (struct die_info *die,
1174 struct dwarf2_cu *cu);
1175
c67a9c90 1176static void dwarf2_build_psymtabs_hard (struct objfile *);
c906108c 1177
72bf9492
DJ
1178static void scan_partial_symbols (struct partial_die_info *,
1179 CORE_ADDR *, CORE_ADDR *,
5734ee8b 1180 int, struct dwarf2_cu *);
c906108c 1181
72bf9492
DJ
1182static void add_partial_symbol (struct partial_die_info *,
1183 struct dwarf2_cu *);
63d06c5c 1184
72bf9492
DJ
1185static void add_partial_namespace (struct partial_die_info *pdi,
1186 CORE_ADDR *lowpc, CORE_ADDR *highpc,
5734ee8b 1187 int need_pc, struct dwarf2_cu *cu);
63d06c5c 1188
5d7cb8df
JK
1189static void add_partial_module (struct partial_die_info *pdi, CORE_ADDR *lowpc,
1190 CORE_ADDR *highpc, int need_pc,
1191 struct dwarf2_cu *cu);
1192
72bf9492
DJ
1193static void add_partial_enumeration (struct partial_die_info *enum_pdi,
1194 struct dwarf2_cu *cu);
91c24f0a 1195
bc30ff58
JB
1196static void add_partial_subprogram (struct partial_die_info *pdi,
1197 CORE_ADDR *lowpc, CORE_ADDR *highpc,
5734ee8b 1198 int need_pc, struct dwarf2_cu *cu);
bc30ff58 1199
a14ed312 1200static void dwarf2_psymtab_to_symtab (struct partial_symtab *);
c906108c 1201
a14ed312 1202static void psymtab_to_symtab_1 (struct partial_symtab *);
c906108c 1203
433df2d4
DE
1204static struct abbrev_info *abbrev_table_lookup_abbrev
1205 (const struct abbrev_table *, unsigned int);
1206
1207static struct abbrev_table *abbrev_table_read_table
1208 (struct dwarf2_section_info *, sect_offset);
1209
1210static void abbrev_table_free (struct abbrev_table *);
1211
f4dc4d17
DE
1212static void abbrev_table_free_cleanup (void *);
1213
dee91e82
DE
1214static void dwarf2_read_abbrevs (struct dwarf2_cu *,
1215 struct dwarf2_section_info *);
c906108c 1216
f3dd6933 1217static void dwarf2_free_abbrev_table (void *);
c906108c 1218
6caca83c
CC
1219static unsigned int peek_abbrev_code (bfd *, gdb_byte *);
1220
dee91e82
DE
1221static struct partial_die_info *load_partial_dies
1222 (const struct die_reader_specs *, gdb_byte *, int);
72bf9492 1223
dee91e82
DE
1224static gdb_byte *read_partial_die (const struct die_reader_specs *,
1225 struct partial_die_info *,
1226 struct abbrev_info *,
1227 unsigned int,
1228 gdb_byte *);
c906108c 1229
36586728 1230static struct partial_die_info *find_partial_die (sect_offset, int,
10b3939b 1231 struct dwarf2_cu *);
72bf9492
DJ
1232
1233static void fixup_partial_die (struct partial_die_info *,
1234 struct dwarf2_cu *);
1235
dee91e82
DE
1236static gdb_byte *read_attribute (const struct die_reader_specs *,
1237 struct attribute *, struct attr_abbrev *,
1238 gdb_byte *);
a8329558 1239
a1855c1d 1240static unsigned int read_1_byte (bfd *, const gdb_byte *);
c906108c 1241
a1855c1d 1242static int read_1_signed_byte (bfd *, const gdb_byte *);
c906108c 1243
a1855c1d 1244static unsigned int read_2_bytes (bfd *, const gdb_byte *);
c906108c 1245
a1855c1d 1246static unsigned int read_4_bytes (bfd *, const gdb_byte *);
c906108c 1247
a1855c1d 1248static ULONGEST read_8_bytes (bfd *, const gdb_byte *);
c906108c 1249
fe1b8b76 1250static CORE_ADDR read_address (bfd *, gdb_byte *ptr, struct dwarf2_cu *,
891d2f0b 1251 unsigned int *);
c906108c 1252
c764a876
DE
1253static LONGEST read_initial_length (bfd *, gdb_byte *, unsigned int *);
1254
1255static LONGEST read_checked_initial_length_and_offset
1256 (bfd *, gdb_byte *, const struct comp_unit_head *,
1257 unsigned int *, unsigned int *);
613e1657 1258
fe1b8b76 1259static LONGEST read_offset (bfd *, gdb_byte *, const struct comp_unit_head *,
c764a876
DE
1260 unsigned int *);
1261
1262static LONGEST read_offset_1 (bfd *, gdb_byte *, unsigned int);
613e1657 1263
f4dc4d17
DE
1264static sect_offset read_abbrev_offset (struct dwarf2_section_info *,
1265 sect_offset);
1266
fe1b8b76 1267static gdb_byte *read_n_bytes (bfd *, gdb_byte *, unsigned int);
c906108c 1268
9b1c24c8 1269static char *read_direct_string (bfd *, gdb_byte *, unsigned int *);
c906108c 1270
fe1b8b76
JB
1271static char *read_indirect_string (bfd *, gdb_byte *,
1272 const struct comp_unit_head *,
1273 unsigned int *);
4bdf3d34 1274
36586728
TT
1275static char *read_indirect_string_from_dwz (struct dwz_file *, LONGEST);
1276
12df843f 1277static ULONGEST read_unsigned_leb128 (bfd *, gdb_byte *, unsigned int *);
c906108c 1278
12df843f 1279static LONGEST read_signed_leb128 (bfd *, gdb_byte *, unsigned int *);
c906108c 1280
3019eac3
DE
1281static CORE_ADDR read_addr_index_from_leb128 (struct dwarf2_cu *, gdb_byte *,
1282 unsigned int *);
1283
1284static char *read_str_index (const struct die_reader_specs *reader,
1285 struct dwarf2_cu *cu, ULONGEST str_index);
1286
e142c38c 1287static void set_cu_language (unsigned int, struct dwarf2_cu *);
c906108c 1288
e142c38c
DJ
1289static struct attribute *dwarf2_attr (struct die_info *, unsigned int,
1290 struct dwarf2_cu *);
c906108c 1291
348e048f 1292static struct attribute *dwarf2_attr_no_follow (struct die_info *,
45e58e77 1293 unsigned int);
348e048f 1294
05cf31d1
JB
1295static int dwarf2_flag_true_p (struct die_info *die, unsigned name,
1296 struct dwarf2_cu *cu);
1297
e142c38c 1298static int die_is_declaration (struct die_info *, struct dwarf2_cu *cu);
3ca72b44 1299
e142c38c 1300static struct die_info *die_specification (struct die_info *die,
f2f0e013 1301 struct dwarf2_cu **);
63d06c5c 1302
debd256d
JB
1303static void free_line_header (struct line_header *lh);
1304
aaa75496
JB
1305static void add_file_name (struct line_header *, char *, unsigned int,
1306 unsigned int, unsigned int);
1307
3019eac3
DE
1308static struct line_header *dwarf_decode_line_header (unsigned int offset,
1309 struct dwarf2_cu *cu);
debd256d 1310
f3f5162e
DE
1311static void dwarf_decode_lines (struct line_header *, const char *,
1312 struct dwarf2_cu *, struct partial_symtab *,
1313 int);
c906108c 1314
72b9f47f 1315static void dwarf2_start_subfile (char *, const char *, const char *);
c906108c 1316
f4dc4d17
DE
1317static void dwarf2_start_symtab (struct dwarf2_cu *,
1318 char *, char *, CORE_ADDR);
1319
a14ed312 1320static struct symbol *new_symbol (struct die_info *, struct type *,
e7c27a73 1321 struct dwarf2_cu *);
c906108c 1322
34eaf542
TT
1323static struct symbol *new_symbol_full (struct die_info *, struct type *,
1324 struct dwarf2_cu *, struct symbol *);
1325
a14ed312 1326static void dwarf2_const_value (struct attribute *, struct symbol *,
e7c27a73 1327 struct dwarf2_cu *);
c906108c 1328
98bfdba5
PA
1329static void dwarf2_const_value_attr (struct attribute *attr,
1330 struct type *type,
1331 const char *name,
1332 struct obstack *obstack,
12df843f 1333 struct dwarf2_cu *cu, LONGEST *value,
98bfdba5
PA
1334 gdb_byte **bytes,
1335 struct dwarf2_locexpr_baton **baton);
2df3850c 1336
e7c27a73 1337static struct type *die_type (struct die_info *, struct dwarf2_cu *);
c906108c 1338
b4ba55a1
JB
1339static int need_gnat_info (struct dwarf2_cu *);
1340
3e43a32a
MS
1341static struct type *die_descriptive_type (struct die_info *,
1342 struct dwarf2_cu *);
b4ba55a1
JB
1343
1344static void set_descriptive_type (struct type *, struct die_info *,
1345 struct dwarf2_cu *);
1346
e7c27a73
DJ
1347static struct type *die_containing_type (struct die_info *,
1348 struct dwarf2_cu *);
c906108c 1349
673bfd45
DE
1350static struct type *lookup_die_type (struct die_info *, struct attribute *,
1351 struct dwarf2_cu *);
c906108c 1352
f792889a 1353static struct type *read_type_die (struct die_info *, struct dwarf2_cu *);
c906108c 1354
673bfd45
DE
1355static struct type *read_type_die_1 (struct die_info *, struct dwarf2_cu *);
1356
0d5cff50 1357static const char *determine_prefix (struct die_info *die, struct dwarf2_cu *);
63d06c5c 1358
6e70227d 1359static char *typename_concat (struct obstack *obs, const char *prefix,
f55ee35c
JK
1360 const char *suffix, int physname,
1361 struct dwarf2_cu *cu);
63d06c5c 1362
e7c27a73 1363static void read_file_scope (struct die_info *, struct dwarf2_cu *);
c906108c 1364
348e048f
DE
1365static void read_type_unit_scope (struct die_info *, struct dwarf2_cu *);
1366
e7c27a73 1367static void read_func_scope (struct die_info *, struct dwarf2_cu *);
c906108c 1368
e7c27a73 1369static void read_lexical_block_scope (struct die_info *, struct dwarf2_cu *);
c906108c 1370
96408a79
SA
1371static void read_call_site_scope (struct die_info *die, struct dwarf2_cu *cu);
1372
ff013f42
JK
1373static int dwarf2_ranges_read (unsigned, CORE_ADDR *, CORE_ADDR *,
1374 struct dwarf2_cu *, struct partial_symtab *);
1375
a14ed312 1376static int dwarf2_get_pc_bounds (struct die_info *,
d85a05f0
DJ
1377 CORE_ADDR *, CORE_ADDR *, struct dwarf2_cu *,
1378 struct partial_symtab *);
c906108c 1379
fae299cd
DC
1380static void get_scope_pc_bounds (struct die_info *,
1381 CORE_ADDR *, CORE_ADDR *,
1382 struct dwarf2_cu *);
1383
801e3a5b
JB
1384static void dwarf2_record_block_ranges (struct die_info *, struct block *,
1385 CORE_ADDR, struct dwarf2_cu *);
1386
a14ed312 1387static void dwarf2_add_field (struct field_info *, struct die_info *,
e7c27a73 1388 struct dwarf2_cu *);
c906108c 1389
a14ed312 1390static void dwarf2_attach_fields_to_type (struct field_info *,
e7c27a73 1391 struct type *, struct dwarf2_cu *);
c906108c 1392
a14ed312 1393static void dwarf2_add_member_fn (struct field_info *,
e26fb1d7 1394 struct die_info *, struct type *,
e7c27a73 1395 struct dwarf2_cu *);
c906108c 1396
a14ed312 1397static void dwarf2_attach_fn_fields_to_type (struct field_info *,
3e43a32a
MS
1398 struct type *,
1399 struct dwarf2_cu *);
c906108c 1400
134d01f1 1401static void process_structure_scope (struct die_info *, struct dwarf2_cu *);
c906108c 1402
e7c27a73 1403static void read_common_block (struct die_info *, struct dwarf2_cu *);
c906108c 1404
e7c27a73 1405static void read_namespace (struct die_info *die, struct dwarf2_cu *);
d9fa45fe 1406
5d7cb8df
JK
1407static void read_module (struct die_info *die, struct dwarf2_cu *cu);
1408
27aa8d6a
SW
1409static void read_import_statement (struct die_info *die, struct dwarf2_cu *);
1410
f55ee35c
JK
1411static struct type *read_module_type (struct die_info *die,
1412 struct dwarf2_cu *cu);
1413
38d518c9 1414static const char *namespace_name (struct die_info *die,
e142c38c 1415 int *is_anonymous, struct dwarf2_cu *);
38d518c9 1416
134d01f1 1417static void process_enumeration_scope (struct die_info *, struct dwarf2_cu *);
c906108c 1418
e7c27a73 1419static CORE_ADDR decode_locdesc (struct dwarf_block *, struct dwarf2_cu *);
c906108c 1420
6e70227d 1421static enum dwarf_array_dim_ordering read_array_order (struct die_info *,
7ca2d3a3
DL
1422 struct dwarf2_cu *);
1423
dee91e82 1424static struct die_info *read_die_and_children (const struct die_reader_specs *,
93311388 1425 gdb_byte *info_ptr,
fe1b8b76 1426 gdb_byte **new_info_ptr,
639d11d3
DC
1427 struct die_info *parent);
1428
dee91e82 1429static struct die_info *read_die_and_siblings (const struct die_reader_specs *,
93311388 1430 gdb_byte *info_ptr,
fe1b8b76 1431 gdb_byte **new_info_ptr,
639d11d3
DC
1432 struct die_info *parent);
1433
3019eac3
DE
1434static gdb_byte *read_full_die_1 (const struct die_reader_specs *,
1435 struct die_info **, gdb_byte *, int *, int);
1436
dee91e82
DE
1437static gdb_byte *read_full_die (const struct die_reader_specs *,
1438 struct die_info **, gdb_byte *, int *);
93311388 1439
e7c27a73 1440static void process_die (struct die_info *, struct dwarf2_cu *);
c906108c 1441
71c25dea
TT
1442static char *dwarf2_canonicalize_name (char *, struct dwarf2_cu *,
1443 struct obstack *);
1444
e142c38c 1445static char *dwarf2_name (struct die_info *die, struct dwarf2_cu *);
9219021c 1446
98bfdba5
PA
1447static const char *dwarf2_full_name (char *name,
1448 struct die_info *die,
1449 struct dwarf2_cu *cu);
1450
e142c38c 1451static struct die_info *dwarf2_extension (struct die_info *die,
f2f0e013 1452 struct dwarf2_cu **);
9219021c 1453
f39c6ffd 1454static const char *dwarf_tag_name (unsigned int);
c906108c 1455
f39c6ffd 1456static const char *dwarf_attr_name (unsigned int);
c906108c 1457
f39c6ffd 1458static const char *dwarf_form_name (unsigned int);
c906108c 1459
a14ed312 1460static char *dwarf_bool_name (unsigned int);
c906108c 1461
f39c6ffd 1462static const char *dwarf_type_encoding_name (unsigned int);
c906108c 1463
f9aca02d 1464static struct die_info *sibling_die (struct die_info *);
c906108c 1465
d97bc12b
DE
1466static void dump_die_shallow (struct ui_file *, int indent, struct die_info *);
1467
1468static void dump_die_for_error (struct die_info *);
1469
1470static void dump_die_1 (struct ui_file *, int level, int max_level,
1471 struct die_info *);
c906108c 1472
d97bc12b 1473/*static*/ void dump_die (struct die_info *, int max_level);
c906108c 1474
51545339 1475static void store_in_ref_table (struct die_info *,
10b3939b 1476 struct dwarf2_cu *);
c906108c 1477
93311388
DE
1478static int is_ref_attr (struct attribute *);
1479
b64f50a1 1480static sect_offset dwarf2_get_ref_die_offset (struct attribute *);
c906108c 1481
43bbcdc2 1482static LONGEST dwarf2_get_attr_constant_value (struct attribute *, int);
a02abb62 1483
348e048f
DE
1484static struct die_info *follow_die_ref_or_sig (struct die_info *,
1485 struct attribute *,
1486 struct dwarf2_cu **);
1487
10b3939b
DJ
1488static struct die_info *follow_die_ref (struct die_info *,
1489 struct attribute *,
f2f0e013 1490 struct dwarf2_cu **);
c906108c 1491
348e048f
DE
1492static struct die_info *follow_die_sig (struct die_info *,
1493 struct attribute *,
1494 struct dwarf2_cu **);
1495
6c83ed52
TT
1496static struct signatured_type *lookup_signatured_type_at_offset
1497 (struct objfile *objfile,
b64f50a1 1498 struct dwarf2_section_info *section, sect_offset offset);
6c83ed52 1499
e5fe5e75 1500static void load_full_type_unit (struct dwarf2_per_cu_data *per_cu);
348e048f 1501
52dc124a 1502static void read_signatured_type (struct signatured_type *);
348e048f 1503
f4dc4d17 1504static struct type_unit_group *get_type_unit_group
094b34ac 1505 (struct dwarf2_cu *, struct attribute *);
f4dc4d17
DE
1506
1507static void build_type_unit_groups (die_reader_func_ftype *, void *);
1508
c906108c
SS
1509/* memory allocation interface */
1510
7b5a2f43 1511static struct dwarf_block *dwarf_alloc_block (struct dwarf2_cu *);
c906108c 1512
b60c80d6 1513static struct die_info *dwarf_alloc_die (struct dwarf2_cu *, int);
c906108c 1514
09262596
DE
1515static void dwarf_decode_macros (struct dwarf2_cu *, unsigned int,
1516 char *, int);
2e276125 1517
8e19ed76
PS
1518static int attr_form_is_block (struct attribute *);
1519
3690dd37
JB
1520static int attr_form_is_section_offset (struct attribute *);
1521
1522static int attr_form_is_constant (struct attribute *);
1523
8cf6f0b1
TT
1524static void fill_in_loclist_baton (struct dwarf2_cu *cu,
1525 struct dwarf2_loclist_baton *baton,
1526 struct attribute *attr);
1527
93e7bd98
DJ
1528static void dwarf2_symbol_mark_computed (struct attribute *attr,
1529 struct symbol *sym,
1530 struct dwarf2_cu *cu);
4c2df51b 1531
dee91e82
DE
1532static gdb_byte *skip_one_die (const struct die_reader_specs *reader,
1533 gdb_byte *info_ptr,
1534 struct abbrev_info *abbrev);
4bb7a0a7 1535
72bf9492
DJ
1536static void free_stack_comp_unit (void *);
1537
72bf9492
DJ
1538static hashval_t partial_die_hash (const void *item);
1539
1540static int partial_die_eq (const void *item_lhs, const void *item_rhs);
1541
ae038cb0 1542static struct dwarf2_per_cu_data *dwarf2_find_containing_comp_unit
36586728 1543 (sect_offset offset, unsigned int offset_in_dwz, struct objfile *objfile);
ae038cb0 1544
9816fde3 1545static void init_one_comp_unit (struct dwarf2_cu *cu,
23745b47 1546 struct dwarf2_per_cu_data *per_cu);
9816fde3
JK
1547
1548static void prepare_one_comp_unit (struct dwarf2_cu *cu,
95554aad
TT
1549 struct die_info *comp_unit_die,
1550 enum language pretend_language);
93311388 1551
68dc6402 1552static void free_heap_comp_unit (void *);
ae038cb0
DJ
1553
1554static void free_cached_comp_units (void *);
1555
1556static void age_cached_comp_units (void);
1557
dee91e82 1558static void free_one_cached_comp_unit (struct dwarf2_per_cu_data *);
ae038cb0 1559
f792889a
DJ
1560static struct type *set_die_type (struct die_info *, struct type *,
1561 struct dwarf2_cu *);
1c379e20 1562
ae038cb0
DJ
1563static void create_all_comp_units (struct objfile *);
1564
0e50663e 1565static int create_all_type_units (struct objfile *);
1fd400ff 1566
95554aad
TT
1567static void load_full_comp_unit (struct dwarf2_per_cu_data *,
1568 enum language);
10b3939b 1569
95554aad
TT
1570static void process_full_comp_unit (struct dwarf2_per_cu_data *,
1571 enum language);
10b3939b 1572
f4dc4d17
DE
1573static void process_full_type_unit (struct dwarf2_per_cu_data *,
1574 enum language);
1575
10b3939b
DJ
1576static void dwarf2_add_dependence (struct dwarf2_cu *,
1577 struct dwarf2_per_cu_data *);
1578
ae038cb0
DJ
1579static void dwarf2_mark (struct dwarf2_cu *);
1580
1581static void dwarf2_clear_marks (struct dwarf2_per_cu_data *);
1582
b64f50a1 1583static struct type *get_die_type_at_offset (sect_offset,
673bfd45
DE
1584 struct dwarf2_per_cu_data *per_cu);
1585
f792889a 1586static struct type *get_die_type (struct die_info *die, struct dwarf2_cu *cu);
72019c9c 1587
9291a0cd
TT
1588static void dwarf2_release_queue (void *dummy);
1589
95554aad
TT
1590static void queue_comp_unit (struct dwarf2_per_cu_data *per_cu,
1591 enum language pretend_language);
1592
1593static int maybe_queue_comp_unit (struct dwarf2_cu *this_cu,
1594 struct dwarf2_per_cu_data *per_cu,
1595 enum language pretend_language);
9291a0cd 1596
a0f42c21 1597static void process_queue (void);
9291a0cd
TT
1598
1599static void find_file_and_directory (struct die_info *die,
1600 struct dwarf2_cu *cu,
1601 char **name, char **comp_dir);
1602
1603static char *file_full_name (int file, struct line_header *lh,
1604 const char *comp_dir);
1605
36586728
TT
1606static gdb_byte *read_and_check_comp_unit_head
1607 (struct comp_unit_head *header,
1608 struct dwarf2_section_info *section,
1609 struct dwarf2_section_info *abbrev_section, gdb_byte *info_ptr,
1610 int is_debug_types_section);
1611
fd820528 1612static void init_cutu_and_read_dies
f4dc4d17
DE
1613 (struct dwarf2_per_cu_data *this_cu, struct abbrev_table *abbrev_table,
1614 int use_existing_cu, int keep,
3019eac3
DE
1615 die_reader_func_ftype *die_reader_func, void *data);
1616
dee91e82
DE
1617static void init_cutu_and_read_dies_simple
1618 (struct dwarf2_per_cu_data *this_cu,
1619 die_reader_func_ftype *die_reader_func, void *data);
9291a0cd 1620
673bfd45 1621static htab_t allocate_signatured_type_table (struct objfile *objfile);
1fd400ff 1622
3019eac3
DE
1623static htab_t allocate_dwo_unit_table (struct objfile *objfile);
1624
1625static struct dwo_unit *lookup_dwo_comp_unit
a1855c1d 1626 (struct dwarf2_per_cu_data *, const char *, const char *, ULONGEST);
3019eac3
DE
1627
1628static struct dwo_unit *lookup_dwo_type_unit
a1855c1d 1629 (struct signatured_type *, const char *, const char *);
3019eac3
DE
1630
1631static void free_dwo_file_cleanup (void *);
1632
95554aad
TT
1633static void process_cu_includes (void);
1634
9291a0cd
TT
1635#if WORDS_BIGENDIAN
1636
1637/* Convert VALUE between big- and little-endian. */
1638static offset_type
1639byte_swap (offset_type value)
1640{
1641 offset_type result;
1642
1643 result = (value & 0xff) << 24;
1644 result |= (value & 0xff00) << 8;
1645 result |= (value & 0xff0000) >> 8;
1646 result |= (value & 0xff000000) >> 24;
1647 return result;
1648}
1649
1650#define MAYBE_SWAP(V) byte_swap (V)
1651
1652#else
1653#define MAYBE_SWAP(V) (V)
1654#endif /* WORDS_BIGENDIAN */
1655
1656/* The suffix for an index file. */
1657#define INDEX_SUFFIX ".gdb-index"
1658
3da10d80
KS
1659static const char *dwarf2_physname (char *name, struct die_info *die,
1660 struct dwarf2_cu *cu);
1661
c906108c 1662/* Try to locate the sections we need for DWARF 2 debugging
251d32d9
TG
1663 information and return true if we have enough to do something.
1664 NAMES points to the dwarf2 section names, or is NULL if the standard
1665 ELF names are used. */
c906108c
SS
1666
1667int
251d32d9
TG
1668dwarf2_has_info (struct objfile *objfile,
1669 const struct dwarf2_debug_sections *names)
c906108c 1670{
be391dca
TT
1671 dwarf2_per_objfile = objfile_data (objfile, dwarf2_objfile_data_key);
1672 if (!dwarf2_per_objfile)
1673 {
1674 /* Initialize per-objfile state. */
1675 struct dwarf2_per_objfile *data
1676 = obstack_alloc (&objfile->objfile_obstack, sizeof (*data));
9a619af0 1677
be391dca
TT
1678 memset (data, 0, sizeof (*data));
1679 set_objfile_data (objfile, dwarf2_objfile_data_key, data);
1680 dwarf2_per_objfile = data;
6502dd73 1681
251d32d9
TG
1682 bfd_map_over_sections (objfile->obfd, dwarf2_locate_sections,
1683 (void *) names);
be391dca
TT
1684 dwarf2_per_objfile->objfile = objfile;
1685 }
1686 return (dwarf2_per_objfile->info.asection != NULL
1687 && dwarf2_per_objfile->abbrev.asection != NULL);
c906108c
SS
1688}
1689
251d32d9
TG
1690/* When loading sections, we look either for uncompressed section or for
1691 compressed section names. */
233a11ab
CS
1692
1693static int
251d32d9
TG
1694section_is_p (const char *section_name,
1695 const struct dwarf2_section_names *names)
233a11ab 1696{
251d32d9
TG
1697 if (names->normal != NULL
1698 && strcmp (section_name, names->normal) == 0)
1699 return 1;
1700 if (names->compressed != NULL
1701 && strcmp (section_name, names->compressed) == 0)
1702 return 1;
1703 return 0;
233a11ab
CS
1704}
1705
c906108c
SS
1706/* This function is mapped across the sections and remembers the
1707 offset and size of each of the debugging sections we are interested
1708 in. */
1709
1710static void
251d32d9 1711dwarf2_locate_sections (bfd *abfd, asection *sectp, void *vnames)
c906108c 1712{
251d32d9 1713 const struct dwarf2_debug_sections *names;
dc7650b8 1714 flagword aflag = bfd_get_section_flags (abfd, sectp);
251d32d9
TG
1715
1716 if (vnames == NULL)
1717 names = &dwarf2_elf_names;
1718 else
1719 names = (const struct dwarf2_debug_sections *) vnames;
1720
dc7650b8
JK
1721 if ((aflag & SEC_HAS_CONTENTS) == 0)
1722 {
1723 }
1724 else if (section_is_p (sectp->name, &names->info))
c906108c 1725 {
dce234bc
PP
1726 dwarf2_per_objfile->info.asection = sectp;
1727 dwarf2_per_objfile->info.size = bfd_get_section_size (sectp);
c906108c 1728 }
251d32d9 1729 else if (section_is_p (sectp->name, &names->abbrev))
c906108c 1730 {
dce234bc
PP
1731 dwarf2_per_objfile->abbrev.asection = sectp;
1732 dwarf2_per_objfile->abbrev.size = bfd_get_section_size (sectp);
c906108c 1733 }
251d32d9 1734 else if (section_is_p (sectp->name, &names->line))
c906108c 1735 {
dce234bc
PP
1736 dwarf2_per_objfile->line.asection = sectp;
1737 dwarf2_per_objfile->line.size = bfd_get_section_size (sectp);
c906108c 1738 }
251d32d9 1739 else if (section_is_p (sectp->name, &names->loc))
c906108c 1740 {
dce234bc
PP
1741 dwarf2_per_objfile->loc.asection = sectp;
1742 dwarf2_per_objfile->loc.size = bfd_get_section_size (sectp);
c906108c 1743 }
251d32d9 1744 else if (section_is_p (sectp->name, &names->macinfo))
c906108c 1745 {
dce234bc
PP
1746 dwarf2_per_objfile->macinfo.asection = sectp;
1747 dwarf2_per_objfile->macinfo.size = bfd_get_section_size (sectp);
c906108c 1748 }
cf2c3c16
TT
1749 else if (section_is_p (sectp->name, &names->macro))
1750 {
1751 dwarf2_per_objfile->macro.asection = sectp;
1752 dwarf2_per_objfile->macro.size = bfd_get_section_size (sectp);
1753 }
251d32d9 1754 else if (section_is_p (sectp->name, &names->str))
c906108c 1755 {
dce234bc
PP
1756 dwarf2_per_objfile->str.asection = sectp;
1757 dwarf2_per_objfile->str.size = bfd_get_section_size (sectp);
c906108c 1758 }
3019eac3
DE
1759 else if (section_is_p (sectp->name, &names->addr))
1760 {
1761 dwarf2_per_objfile->addr.asection = sectp;
1762 dwarf2_per_objfile->addr.size = bfd_get_section_size (sectp);
1763 }
251d32d9 1764 else if (section_is_p (sectp->name, &names->frame))
b6af0555 1765 {
dce234bc
PP
1766 dwarf2_per_objfile->frame.asection = sectp;
1767 dwarf2_per_objfile->frame.size = bfd_get_section_size (sectp);
b6af0555 1768 }
251d32d9 1769 else if (section_is_p (sectp->name, &names->eh_frame))
b6af0555 1770 {
dc7650b8
JK
1771 dwarf2_per_objfile->eh_frame.asection = sectp;
1772 dwarf2_per_objfile->eh_frame.size = bfd_get_section_size (sectp);
b6af0555 1773 }
251d32d9 1774 else if (section_is_p (sectp->name, &names->ranges))
af34e669 1775 {
dce234bc
PP
1776 dwarf2_per_objfile->ranges.asection = sectp;
1777 dwarf2_per_objfile->ranges.size = bfd_get_section_size (sectp);
af34e669 1778 }
251d32d9 1779 else if (section_is_p (sectp->name, &names->types))
348e048f 1780 {
8b70b953
TT
1781 struct dwarf2_section_info type_section;
1782
1783 memset (&type_section, 0, sizeof (type_section));
1784 type_section.asection = sectp;
1785 type_section.size = bfd_get_section_size (sectp);
1786
1787 VEC_safe_push (dwarf2_section_info_def, dwarf2_per_objfile->types,
1788 &type_section);
348e048f 1789 }
251d32d9 1790 else if (section_is_p (sectp->name, &names->gdb_index))
9291a0cd
TT
1791 {
1792 dwarf2_per_objfile->gdb_index.asection = sectp;
1793 dwarf2_per_objfile->gdb_index.size = bfd_get_section_size (sectp);
1794 }
dce234bc 1795
72dca2f5
FR
1796 if ((bfd_get_section_flags (abfd, sectp) & SEC_LOAD)
1797 && bfd_section_vma (abfd, sectp) == 0)
1798 dwarf2_per_objfile->has_section_at_zero = 1;
c906108c
SS
1799}
1800
fceca515
DE
1801/* A helper function that decides whether a section is empty,
1802 or not present. */
9e0ac564
TT
1803
1804static int
1805dwarf2_section_empty_p (struct dwarf2_section_info *info)
1806{
1807 return info->asection == NULL || info->size == 0;
1808}
1809
3019eac3
DE
1810/* Read the contents of the section INFO.
1811 OBJFILE is the main object file, but not necessarily the file where
1812 the section comes from. E.g., for DWO files INFO->asection->owner
1813 is the bfd of the DWO file.
dce234bc 1814 If the section is compressed, uncompress it before returning. */
c906108c 1815
dce234bc
PP
1816static void
1817dwarf2_read_section (struct objfile *objfile, struct dwarf2_section_info *info)
c906108c 1818{
dce234bc 1819 asection *sectp = info->asection;
3019eac3 1820 bfd *abfd;
dce234bc
PP
1821 gdb_byte *buf, *retbuf;
1822 unsigned char header[4];
c906108c 1823
be391dca
TT
1824 if (info->readin)
1825 return;
dce234bc 1826 info->buffer = NULL;
be391dca 1827 info->readin = 1;
188dd5d6 1828
9e0ac564 1829 if (dwarf2_section_empty_p (info))
dce234bc 1830 return;
c906108c 1831
3019eac3
DE
1832 abfd = sectp->owner;
1833
4bf44c1c
TT
1834 /* If the section has relocations, we must read it ourselves.
1835 Otherwise we attach it to the BFD. */
1836 if ((sectp->flags & SEC_RELOC) == 0)
dce234bc 1837 {
4bf44c1c 1838 const gdb_byte *bytes = gdb_bfd_map_section (sectp, &info->size);
dce234bc 1839
4bf44c1c
TT
1840 /* We have to cast away const here for historical reasons.
1841 Fixing dwarf2read to be const-correct would be quite nice. */
1842 info->buffer = (gdb_byte *) bytes;
1843 return;
dce234bc 1844 }
dce234bc 1845
4bf44c1c
TT
1846 buf = obstack_alloc (&objfile->objfile_obstack, info->size);
1847 info->buffer = buf;
dce234bc
PP
1848
1849 /* When debugging .o files, we may need to apply relocations; see
1850 http://sourceware.org/ml/gdb-patches/2002-04/msg00136.html .
1851 We never compress sections in .o files, so we only need to
1852 try this when the section is not compressed. */
ac8035ab 1853 retbuf = symfile_relocate_debug_section (objfile, sectp, buf);
dce234bc
PP
1854 if (retbuf != NULL)
1855 {
1856 info->buffer = retbuf;
1857 return;
1858 }
1859
1860 if (bfd_seek (abfd, sectp->filepos, SEEK_SET) != 0
1861 || bfd_bread (buf, info->size, abfd) != info->size)
1862 error (_("Dwarf Error: Can't read DWARF data from '%s'"),
1863 bfd_get_filename (abfd));
1864}
1865
9e0ac564
TT
1866/* A helper function that returns the size of a section in a safe way.
1867 If you are positive that the section has been read before using the
1868 size, then it is safe to refer to the dwarf2_section_info object's
1869 "size" field directly. In other cases, you must call this
1870 function, because for compressed sections the size field is not set
1871 correctly until the section has been read. */
1872
1873static bfd_size_type
1874dwarf2_section_size (struct objfile *objfile,
1875 struct dwarf2_section_info *info)
1876{
1877 if (!info->readin)
1878 dwarf2_read_section (objfile, info);
1879 return info->size;
1880}
1881
dce234bc 1882/* Fill in SECTP, BUFP and SIZEP with section info, given OBJFILE and
0963b4bd 1883 SECTION_NAME. */
af34e669 1884
dce234bc 1885void
3017a003
TG
1886dwarf2_get_section_info (struct objfile *objfile,
1887 enum dwarf2_section_enum sect,
dce234bc
PP
1888 asection **sectp, gdb_byte **bufp,
1889 bfd_size_type *sizep)
1890{
1891 struct dwarf2_per_objfile *data
1892 = objfile_data (objfile, dwarf2_objfile_data_key);
1893 struct dwarf2_section_info *info;
a3b2a86b
TT
1894
1895 /* We may see an objfile without any DWARF, in which case we just
1896 return nothing. */
1897 if (data == NULL)
1898 {
1899 *sectp = NULL;
1900 *bufp = NULL;
1901 *sizep = 0;
1902 return;
1903 }
3017a003
TG
1904 switch (sect)
1905 {
1906 case DWARF2_DEBUG_FRAME:
1907 info = &data->frame;
1908 break;
1909 case DWARF2_EH_FRAME:
1910 info = &data->eh_frame;
1911 break;
1912 default:
1913 gdb_assert_not_reached ("unexpected section");
1914 }
dce234bc 1915
9e0ac564 1916 dwarf2_read_section (objfile, info);
dce234bc
PP
1917
1918 *sectp = info->asection;
1919 *bufp = info->buffer;
1920 *sizep = info->size;
1921}
1922
36586728
TT
1923/* A helper function to find the sections for a .dwz file. */
1924
1925static void
1926locate_dwz_sections (bfd *abfd, asection *sectp, void *arg)
1927{
1928 struct dwz_file *dwz_file = arg;
1929
1930 /* Note that we only support the standard ELF names, because .dwz
1931 is ELF-only (at the time of writing). */
1932 if (section_is_p (sectp->name, &dwarf2_elf_names.abbrev))
1933 {
1934 dwz_file->abbrev.asection = sectp;
1935 dwz_file->abbrev.size = bfd_get_section_size (sectp);
1936 }
1937 else if (section_is_p (sectp->name, &dwarf2_elf_names.info))
1938 {
1939 dwz_file->info.asection = sectp;
1940 dwz_file->info.size = bfd_get_section_size (sectp);
1941 }
1942 else if (section_is_p (sectp->name, &dwarf2_elf_names.str))
1943 {
1944 dwz_file->str.asection = sectp;
1945 dwz_file->str.size = bfd_get_section_size (sectp);
1946 }
1947 else if (section_is_p (sectp->name, &dwarf2_elf_names.line))
1948 {
1949 dwz_file->line.asection = sectp;
1950 dwz_file->line.size = bfd_get_section_size (sectp);
1951 }
1952 else if (section_is_p (sectp->name, &dwarf2_elf_names.macro))
1953 {
1954 dwz_file->macro.asection = sectp;
1955 dwz_file->macro.size = bfd_get_section_size (sectp);
1956 }
2ec9a5e0
TT
1957 else if (section_is_p (sectp->name, &dwarf2_elf_names.gdb_index))
1958 {
1959 dwz_file->gdb_index.asection = sectp;
1960 dwz_file->gdb_index.size = bfd_get_section_size (sectp);
1961 }
36586728
TT
1962}
1963
1964/* Open the separate '.dwz' debug file, if needed. Error if the file
1965 cannot be found. */
1966
1967static struct dwz_file *
1968dwarf2_get_dwz_file (void)
1969{
1970 bfd *abfd, *dwz_bfd;
1971 asection *section;
1972 gdb_byte *data;
1973 struct cleanup *cleanup;
1974 const char *filename;
1975 struct dwz_file *result;
1976
1977 if (dwarf2_per_objfile->dwz_file != NULL)
1978 return dwarf2_per_objfile->dwz_file;
1979
1980 abfd = dwarf2_per_objfile->objfile->obfd;
1981 section = bfd_get_section_by_name (abfd, ".gnu_debugaltlink");
1982 if (section == NULL)
1983 error (_("could not find '.gnu_debugaltlink' section"));
1984 if (!bfd_malloc_and_get_section (abfd, section, &data))
1985 error (_("could not read '.gnu_debugaltlink' section: %s"),
1986 bfd_errmsg (bfd_get_error ()));
1987 cleanup = make_cleanup (xfree, data);
1988
1989 filename = data;
1990 if (!IS_ABSOLUTE_PATH (filename))
1991 {
1992 char *abs = gdb_realpath (dwarf2_per_objfile->objfile->name);
1993 char *rel;
1994
1995 make_cleanup (xfree, abs);
1996 abs = ldirname (abs);
1997 make_cleanup (xfree, abs);
1998
1999 rel = concat (abs, SLASH_STRING, filename, (char *) NULL);
2000 make_cleanup (xfree, rel);
2001 filename = rel;
2002 }
2003
2004 /* The format is just a NUL-terminated file name, followed by the
2005 build-id. For now, though, we ignore the build-id. */
2006 dwz_bfd = gdb_bfd_open (filename, gnutarget, -1);
2007 if (dwz_bfd == NULL)
2008 error (_("could not read '%s': %s"), filename,
2009 bfd_errmsg (bfd_get_error ()));
2010
2011 if (!bfd_check_format (dwz_bfd, bfd_object))
2012 {
2013 gdb_bfd_unref (dwz_bfd);
2014 error (_("file '%s' was not usable: %s"), filename,
2015 bfd_errmsg (bfd_get_error ()));
2016 }
2017
2018 result = OBSTACK_ZALLOC (&dwarf2_per_objfile->objfile->objfile_obstack,
2019 struct dwz_file);
2020 result->dwz_bfd = dwz_bfd;
2021
2022 bfd_map_over_sections (dwz_bfd, locate_dwz_sections, result);
2023
2024 do_cleanups (cleanup);
2025
2026 return result;
2027}
9291a0cd 2028\f
7b9f3c50
DE
2029/* DWARF quick_symbols_functions support. */
2030
2031/* TUs can share .debug_line entries, and there can be a lot more TUs than
2032 unique line tables, so we maintain a separate table of all .debug_line
2033 derived entries to support the sharing.
2034 All the quick functions need is the list of file names. We discard the
2035 line_header when we're done and don't need to record it here. */
2036struct quick_file_names
2037{
094b34ac
DE
2038 /* The data used to construct the hash key. */
2039 struct stmt_list_hash hash;
7b9f3c50
DE
2040
2041 /* The number of entries in file_names, real_names. */
2042 unsigned int num_file_names;
2043
2044 /* The file names from the line table, after being run through
2045 file_full_name. */
2046 const char **file_names;
2047
2048 /* The file names from the line table after being run through
2049 gdb_realpath. These are computed lazily. */
2050 const char **real_names;
2051};
2052
2053/* When using the index (and thus not using psymtabs), each CU has an
2054 object of this type. This is used to hold information needed by
2055 the various "quick" methods. */
2056struct dwarf2_per_cu_quick_data
2057{
2058 /* The file table. This can be NULL if there was no file table
2059 or it's currently not read in.
2060 NOTE: This points into dwarf2_per_objfile->quick_file_names_table. */
2061 struct quick_file_names *file_names;
2062
2063 /* The corresponding symbol table. This is NULL if symbols for this
2064 CU have not yet been read. */
2065 struct symtab *symtab;
2066
2067 /* A temporary mark bit used when iterating over all CUs in
2068 expand_symtabs_matching. */
2069 unsigned int mark : 1;
2070
2071 /* True if we've tried to read the file table and found there isn't one.
2072 There will be no point in trying to read it again next time. */
2073 unsigned int no_file_data : 1;
2074};
2075
094b34ac
DE
2076/* Utility hash function for a stmt_list_hash. */
2077
2078static hashval_t
2079hash_stmt_list_entry (const struct stmt_list_hash *stmt_list_hash)
2080{
2081 hashval_t v = 0;
2082
2083 if (stmt_list_hash->dwo_unit != NULL)
2084 v += (uintptr_t) stmt_list_hash->dwo_unit->dwo_file;
2085 v += stmt_list_hash->line_offset.sect_off;
2086 return v;
2087}
2088
2089/* Utility equality function for a stmt_list_hash. */
2090
2091static int
2092eq_stmt_list_entry (const struct stmt_list_hash *lhs,
2093 const struct stmt_list_hash *rhs)
2094{
2095 if ((lhs->dwo_unit != NULL) != (rhs->dwo_unit != NULL))
2096 return 0;
2097 if (lhs->dwo_unit != NULL
2098 && lhs->dwo_unit->dwo_file != rhs->dwo_unit->dwo_file)
2099 return 0;
2100
2101 return lhs->line_offset.sect_off == rhs->line_offset.sect_off;
2102}
2103
7b9f3c50
DE
2104/* Hash function for a quick_file_names. */
2105
2106static hashval_t
2107hash_file_name_entry (const void *e)
2108{
2109 const struct quick_file_names *file_data = e;
2110
094b34ac 2111 return hash_stmt_list_entry (&file_data->hash);
7b9f3c50
DE
2112}
2113
2114/* Equality function for a quick_file_names. */
2115
2116static int
2117eq_file_name_entry (const void *a, const void *b)
2118{
2119 const struct quick_file_names *ea = a;
2120 const struct quick_file_names *eb = b;
2121
094b34ac 2122 return eq_stmt_list_entry (&ea->hash, &eb->hash);
7b9f3c50
DE
2123}
2124
2125/* Delete function for a quick_file_names. */
2126
2127static void
2128delete_file_name_entry (void *e)
2129{
2130 struct quick_file_names *file_data = e;
2131 int i;
2132
2133 for (i = 0; i < file_data->num_file_names; ++i)
2134 {
2135 xfree ((void*) file_data->file_names[i]);
2136 if (file_data->real_names)
2137 xfree ((void*) file_data->real_names[i]);
2138 }
2139
2140 /* The space for the struct itself lives on objfile_obstack,
2141 so we don't free it here. */
2142}
2143
2144/* Create a quick_file_names hash table. */
2145
2146static htab_t
2147create_quick_file_names_table (unsigned int nr_initial_entries)
2148{
2149 return htab_create_alloc (nr_initial_entries,
2150 hash_file_name_entry, eq_file_name_entry,
2151 delete_file_name_entry, xcalloc, xfree);
2152}
9291a0cd 2153
918dd910
JK
2154/* Read in PER_CU->CU. This function is unrelated to symtabs, symtab would
2155 have to be created afterwards. You should call age_cached_comp_units after
2156 processing PER_CU->CU. dw2_setup must have been already called. */
2157
2158static void
2159load_cu (struct dwarf2_per_cu_data *per_cu)
2160{
3019eac3 2161 if (per_cu->is_debug_types)
e5fe5e75 2162 load_full_type_unit (per_cu);
918dd910 2163 else
95554aad 2164 load_full_comp_unit (per_cu, language_minimal);
918dd910 2165
918dd910 2166 gdb_assert (per_cu->cu != NULL);
2dc860c0
DE
2167
2168 dwarf2_find_base_address (per_cu->cu->dies, per_cu->cu);
918dd910
JK
2169}
2170
a0f42c21 2171/* Read in the symbols for PER_CU. */
2fdf6df6 2172
9291a0cd 2173static void
a0f42c21 2174dw2_do_instantiate_symtab (struct dwarf2_per_cu_data *per_cu)
9291a0cd
TT
2175{
2176 struct cleanup *back_to;
2177
f4dc4d17
DE
2178 /* Skip type_unit_groups, reading the type units they contain
2179 is handled elsewhere. */
2180 if (IS_TYPE_UNIT_GROUP (per_cu))
2181 return;
2182
9291a0cd
TT
2183 back_to = make_cleanup (dwarf2_release_queue, NULL);
2184
95554aad
TT
2185 if (dwarf2_per_objfile->using_index
2186 ? per_cu->v.quick->symtab == NULL
2187 : (per_cu->v.psymtab == NULL || !per_cu->v.psymtab->readin))
2188 {
2189 queue_comp_unit (per_cu, language_minimal);
2190 load_cu (per_cu);
2191 }
9291a0cd 2192
a0f42c21 2193 process_queue ();
9291a0cd
TT
2194
2195 /* Age the cache, releasing compilation units that have not
2196 been used recently. */
2197 age_cached_comp_units ();
2198
2199 do_cleanups (back_to);
2200}
2201
2202/* Ensure that the symbols for PER_CU have been read in. OBJFILE is
2203 the objfile from which this CU came. Returns the resulting symbol
2204 table. */
2fdf6df6 2205
9291a0cd 2206static struct symtab *
a0f42c21 2207dw2_instantiate_symtab (struct dwarf2_per_cu_data *per_cu)
9291a0cd 2208{
95554aad 2209 gdb_assert (dwarf2_per_objfile->using_index);
9291a0cd
TT
2210 if (!per_cu->v.quick->symtab)
2211 {
2212 struct cleanup *back_to = make_cleanup (free_cached_comp_units, NULL);
2213 increment_reading_symtab ();
a0f42c21 2214 dw2_do_instantiate_symtab (per_cu);
95554aad 2215 process_cu_includes ();
9291a0cd
TT
2216 do_cleanups (back_to);
2217 }
2218 return per_cu->v.quick->symtab;
2219}
2220
f4dc4d17
DE
2221/* Return the CU given its index.
2222
2223 This is intended for loops like:
2224
2225 for (i = 0; i < (dwarf2_per_objfile->n_comp_units
2226 + dwarf2_per_objfile->n_type_units); ++i)
2227 {
2228 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
2229
2230 ...;
2231 }
2232*/
2fdf6df6 2233
1fd400ff
TT
2234static struct dwarf2_per_cu_data *
2235dw2_get_cu (int index)
2236{
2237 if (index >= dwarf2_per_objfile->n_comp_units)
2238 {
f4dc4d17 2239 index -= dwarf2_per_objfile->n_comp_units;
094b34ac
DE
2240 gdb_assert (index < dwarf2_per_objfile->n_type_units);
2241 return &dwarf2_per_objfile->all_type_units[index]->per_cu;
f4dc4d17
DE
2242 }
2243
2244 return dwarf2_per_objfile->all_comp_units[index];
2245}
2246
2247/* Return the primary CU given its index.
2248 The difference between this function and dw2_get_cu is in the handling
2249 of type units (TUs). Here we return the type_unit_group object.
2250
2251 This is intended for loops like:
2252
2253 for (i = 0; i < (dwarf2_per_objfile->n_comp_units
2254 + dwarf2_per_objfile->n_type_unit_groups); ++i)
2255 {
2256 struct dwarf2_per_cu_data *per_cu = dw2_get_primary_cu (i);
2257
2258 ...;
2259 }
2260*/
2261
2262static struct dwarf2_per_cu_data *
2263dw2_get_primary_cu (int index)
2264{
2265 if (index >= dwarf2_per_objfile->n_comp_units)
2266 {
1fd400ff 2267 index -= dwarf2_per_objfile->n_comp_units;
094b34ac
DE
2268 gdb_assert (index < dwarf2_per_objfile->n_type_unit_groups);
2269 return &dwarf2_per_objfile->all_type_unit_groups[index]->per_cu;
1fd400ff 2270 }
f4dc4d17 2271
1fd400ff
TT
2272 return dwarf2_per_objfile->all_comp_units[index];
2273}
2274
9291a0cd
TT
2275/* A helper function that knows how to read a 64-bit value in a way
2276 that doesn't make gdb die. Returns 1 if the conversion went ok, 0
2277 otherwise. */
2fdf6df6 2278
9291a0cd
TT
2279static int
2280extract_cu_value (const char *bytes, ULONGEST *result)
2281{
2282 if (sizeof (ULONGEST) < 8)
2283 {
2284 int i;
2285
2286 /* Ignore the upper 4 bytes if they are all zero. */
2287 for (i = 0; i < 4; ++i)
2288 if (bytes[i + 4] != 0)
2289 return 0;
2290
2291 *result = extract_unsigned_integer (bytes, 4, BFD_ENDIAN_LITTLE);
2292 }
2293 else
2294 *result = extract_unsigned_integer (bytes, 8, BFD_ENDIAN_LITTLE);
2295 return 1;
2296}
2297
2ec9a5e0
TT
2298/* A helper for create_cus_from_index that handles a given list of
2299 CUs. */
2fdf6df6 2300
9291a0cd 2301static int
2ec9a5e0
TT
2302create_cus_from_index_list (struct objfile *objfile,
2303 const gdb_byte *cu_list, offset_type n_elements,
2304 struct dwarf2_section_info *section,
2305 int is_dwz,
2306 int base_offset)
9291a0cd
TT
2307{
2308 offset_type i;
9291a0cd 2309
2ec9a5e0 2310 for (i = 0; i < n_elements; i += 2)
9291a0cd
TT
2311 {
2312 struct dwarf2_per_cu_data *the_cu;
2313 ULONGEST offset, length;
2314
2315 if (!extract_cu_value (cu_list, &offset)
2316 || !extract_cu_value (cu_list + 8, &length))
2317 return 0;
2318 cu_list += 2 * 8;
2319
2320 the_cu = OBSTACK_ZALLOC (&objfile->objfile_obstack,
2321 struct dwarf2_per_cu_data);
b64f50a1 2322 the_cu->offset.sect_off = offset;
9291a0cd
TT
2323 the_cu->length = length;
2324 the_cu->objfile = objfile;
2ec9a5e0 2325 the_cu->info_or_types_section = section;
9291a0cd
TT
2326 the_cu->v.quick = OBSTACK_ZALLOC (&objfile->objfile_obstack,
2327 struct dwarf2_per_cu_quick_data);
2ec9a5e0
TT
2328 the_cu->is_dwz = is_dwz;
2329 dwarf2_per_objfile->all_comp_units[base_offset + i / 2] = the_cu;
9291a0cd
TT
2330 }
2331
2332 return 1;
2333}
2334
2ec9a5e0
TT
2335/* Read the CU list from the mapped index, and use it to create all
2336 the CU objects for this objfile. Return 0 if something went wrong,
2337 1 if everything went ok. */
2338
2339static int
2340create_cus_from_index (struct objfile *objfile,
2341 const gdb_byte *cu_list, offset_type cu_list_elements,
2342 const gdb_byte *dwz_list, offset_type dwz_elements)
2343{
2344 struct dwz_file *dwz;
2345
2346 dwarf2_per_objfile->n_comp_units = (cu_list_elements + dwz_elements) / 2;
2347 dwarf2_per_objfile->all_comp_units
2348 = obstack_alloc (&objfile->objfile_obstack,
2349 dwarf2_per_objfile->n_comp_units
2350 * sizeof (struct dwarf2_per_cu_data *));
2351
2352 if (!create_cus_from_index_list (objfile, cu_list, cu_list_elements,
2353 &dwarf2_per_objfile->info, 0, 0))
2354 return 0;
2355
2356 if (dwz_elements == 0)
2357 return 1;
2358
2359 dwz = dwarf2_get_dwz_file ();
2360 return create_cus_from_index_list (objfile, dwz_list, dwz_elements,
2361 &dwz->info, 1, cu_list_elements / 2);
2362}
2363
1fd400ff 2364/* Create the signatured type hash table from the index. */
673bfd45 2365
1fd400ff 2366static int
673bfd45 2367create_signatured_type_table_from_index (struct objfile *objfile,
8b70b953 2368 struct dwarf2_section_info *section,
673bfd45
DE
2369 const gdb_byte *bytes,
2370 offset_type elements)
1fd400ff
TT
2371{
2372 offset_type i;
673bfd45 2373 htab_t sig_types_hash;
1fd400ff 2374
d467dd73
DE
2375 dwarf2_per_objfile->n_type_units = elements / 3;
2376 dwarf2_per_objfile->all_type_units
1fd400ff 2377 = obstack_alloc (&objfile->objfile_obstack,
d467dd73 2378 dwarf2_per_objfile->n_type_units
b4dd5633 2379 * sizeof (struct signatured_type *));
1fd400ff 2380
673bfd45 2381 sig_types_hash = allocate_signatured_type_table (objfile);
1fd400ff
TT
2382
2383 for (i = 0; i < elements; i += 3)
2384 {
52dc124a
DE
2385 struct signatured_type *sig_type;
2386 ULONGEST offset, type_offset_in_tu, signature;
1fd400ff
TT
2387 void **slot;
2388
2389 if (!extract_cu_value (bytes, &offset)
52dc124a 2390 || !extract_cu_value (bytes + 8, &type_offset_in_tu))
1fd400ff
TT
2391 return 0;
2392 signature = extract_unsigned_integer (bytes + 16, 8, BFD_ENDIAN_LITTLE);
2393 bytes += 3 * 8;
2394
52dc124a 2395 sig_type = OBSTACK_ZALLOC (&objfile->objfile_obstack,
1fd400ff 2396 struct signatured_type);
52dc124a 2397 sig_type->signature = signature;
3019eac3
DE
2398 sig_type->type_offset_in_tu.cu_off = type_offset_in_tu;
2399 sig_type->per_cu.is_debug_types = 1;
2400 sig_type->per_cu.info_or_types_section = section;
52dc124a
DE
2401 sig_type->per_cu.offset.sect_off = offset;
2402 sig_type->per_cu.objfile = objfile;
2403 sig_type->per_cu.v.quick
1fd400ff
TT
2404 = OBSTACK_ZALLOC (&objfile->objfile_obstack,
2405 struct dwarf2_per_cu_quick_data);
2406
52dc124a
DE
2407 slot = htab_find_slot (sig_types_hash, sig_type, INSERT);
2408 *slot = sig_type;
1fd400ff 2409
b4dd5633 2410 dwarf2_per_objfile->all_type_units[i / 3] = sig_type;
1fd400ff
TT
2411 }
2412
673bfd45 2413 dwarf2_per_objfile->signatured_types = sig_types_hash;
1fd400ff
TT
2414
2415 return 1;
2416}
2417
9291a0cd
TT
2418/* Read the address map data from the mapped index, and use it to
2419 populate the objfile's psymtabs_addrmap. */
2fdf6df6 2420
9291a0cd
TT
2421static void
2422create_addrmap_from_index (struct objfile *objfile, struct mapped_index *index)
2423{
2424 const gdb_byte *iter, *end;
2425 struct obstack temp_obstack;
2426 struct addrmap *mutable_map;
2427 struct cleanup *cleanup;
2428 CORE_ADDR baseaddr;
2429
2430 obstack_init (&temp_obstack);
2431 cleanup = make_cleanup_obstack_free (&temp_obstack);
2432 mutable_map = addrmap_create_mutable (&temp_obstack);
2433
2434 iter = index->address_table;
2435 end = iter + index->address_table_size;
2436
2437 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
2438
2439 while (iter < end)
2440 {
2441 ULONGEST hi, lo, cu_index;
2442 lo = extract_unsigned_integer (iter, 8, BFD_ENDIAN_LITTLE);
2443 iter += 8;
2444 hi = extract_unsigned_integer (iter, 8, BFD_ENDIAN_LITTLE);
2445 iter += 8;
2446 cu_index = extract_unsigned_integer (iter, 4, BFD_ENDIAN_LITTLE);
2447 iter += 4;
2448
2449 addrmap_set_empty (mutable_map, lo + baseaddr, hi + baseaddr - 1,
1fd400ff 2450 dw2_get_cu (cu_index));
9291a0cd
TT
2451 }
2452
2453 objfile->psymtabs_addrmap = addrmap_create_fixed (mutable_map,
2454 &objfile->objfile_obstack);
2455 do_cleanups (cleanup);
2456}
2457
59d7bcaf
JK
2458/* The hash function for strings in the mapped index. This is the same as
2459 SYMBOL_HASH_NEXT, but we keep a separate copy to maintain control over the
2460 implementation. This is necessary because the hash function is tied to the
2461 format of the mapped index file. The hash values do not have to match with
559a7a62
JK
2462 SYMBOL_HASH_NEXT.
2463
2464 Use INT_MAX for INDEX_VERSION if you generate the current index format. */
2fdf6df6 2465
9291a0cd 2466static hashval_t
559a7a62 2467mapped_index_string_hash (int index_version, const void *p)
9291a0cd
TT
2468{
2469 const unsigned char *str = (const unsigned char *) p;
2470 hashval_t r = 0;
2471 unsigned char c;
2472
2473 while ((c = *str++) != 0)
559a7a62
JK
2474 {
2475 if (index_version >= 5)
2476 c = tolower (c);
2477 r = r * 67 + c - 113;
2478 }
9291a0cd
TT
2479
2480 return r;
2481}
2482
2483/* Find a slot in the mapped index INDEX for the object named NAME.
2484 If NAME is found, set *VEC_OUT to point to the CU vector in the
2485 constant pool and return 1. If NAME cannot be found, return 0. */
2fdf6df6 2486
9291a0cd
TT
2487static int
2488find_slot_in_mapped_hash (struct mapped_index *index, const char *name,
2489 offset_type **vec_out)
2490{
0cf03b49
JK
2491 struct cleanup *back_to = make_cleanup (null_cleanup, 0);
2492 offset_type hash;
9291a0cd 2493 offset_type slot, step;
559a7a62 2494 int (*cmp) (const char *, const char *);
9291a0cd 2495
0cf03b49
JK
2496 if (current_language->la_language == language_cplus
2497 || current_language->la_language == language_java
2498 || current_language->la_language == language_fortran)
2499 {
2500 /* NAME is already canonical. Drop any qualifiers as .gdb_index does
2501 not contain any. */
2502 const char *paren = strchr (name, '(');
2503
2504 if (paren)
2505 {
2506 char *dup;
2507
2508 dup = xmalloc (paren - name + 1);
2509 memcpy (dup, name, paren - name);
2510 dup[paren - name] = 0;
2511
2512 make_cleanup (xfree, dup);
2513 name = dup;
2514 }
2515 }
2516
559a7a62 2517 /* Index version 4 did not support case insensitive searches. But the
feea76c2 2518 indices for case insensitive languages are built in lowercase, therefore
559a7a62
JK
2519 simulate our NAME being searched is also lowercased. */
2520 hash = mapped_index_string_hash ((index->version == 4
2521 && case_sensitivity == case_sensitive_off
2522 ? 5 : index->version),
2523 name);
2524
3876f04e
DE
2525 slot = hash & (index->symbol_table_slots - 1);
2526 step = ((hash * 17) & (index->symbol_table_slots - 1)) | 1;
559a7a62 2527 cmp = (case_sensitivity == case_sensitive_on ? strcmp : strcasecmp);
9291a0cd
TT
2528
2529 for (;;)
2530 {
2531 /* Convert a slot number to an offset into the table. */
2532 offset_type i = 2 * slot;
2533 const char *str;
3876f04e 2534 if (index->symbol_table[i] == 0 && index->symbol_table[i + 1] == 0)
0cf03b49
JK
2535 {
2536 do_cleanups (back_to);
2537 return 0;
2538 }
9291a0cd 2539
3876f04e 2540 str = index->constant_pool + MAYBE_SWAP (index->symbol_table[i]);
559a7a62 2541 if (!cmp (name, str))
9291a0cd
TT
2542 {
2543 *vec_out = (offset_type *) (index->constant_pool
3876f04e 2544 + MAYBE_SWAP (index->symbol_table[i + 1]));
0cf03b49 2545 do_cleanups (back_to);
9291a0cd
TT
2546 return 1;
2547 }
2548
3876f04e 2549 slot = (slot + step) & (index->symbol_table_slots - 1);
9291a0cd
TT
2550 }
2551}
2552
2ec9a5e0
TT
2553/* A helper function that reads the .gdb_index from SECTION and fills
2554 in MAP. FILENAME is the name of the file containing the section;
2555 it is used for error reporting. DEPRECATED_OK is nonzero if it is
2556 ok to use deprecated sections.
2557
2558 CU_LIST, CU_LIST_ELEMENTS, TYPES_LIST, and TYPES_LIST_ELEMENTS are
2559 out parameters that are filled in with information about the CU and
2560 TU lists in the section.
2561
2562 Returns 1 if all went well, 0 otherwise. */
2fdf6df6 2563
9291a0cd 2564static int
2ec9a5e0
TT
2565read_index_from_section (struct objfile *objfile,
2566 const char *filename,
2567 int deprecated_ok,
2568 struct dwarf2_section_info *section,
2569 struct mapped_index *map,
2570 const gdb_byte **cu_list,
2571 offset_type *cu_list_elements,
2572 const gdb_byte **types_list,
2573 offset_type *types_list_elements)
9291a0cd 2574{
9291a0cd 2575 char *addr;
2ec9a5e0 2576 offset_type version;
b3b272e1 2577 offset_type *metadata;
1fd400ff 2578 int i;
9291a0cd 2579
2ec9a5e0 2580 if (dwarf2_section_empty_p (section))
9291a0cd 2581 return 0;
82430852
JK
2582
2583 /* Older elfutils strip versions could keep the section in the main
2584 executable while splitting it for the separate debug info file. */
2ec9a5e0 2585 if ((bfd_get_file_flags (section->asection) & SEC_HAS_CONTENTS) == 0)
82430852
JK
2586 return 0;
2587
2ec9a5e0 2588 dwarf2_read_section (objfile, section);
9291a0cd 2589
2ec9a5e0 2590 addr = section->buffer;
9291a0cd 2591 /* Version check. */
1fd400ff 2592 version = MAYBE_SWAP (*(offset_type *) addr);
987d643c 2593 /* Versions earlier than 3 emitted every copy of a psymbol. This
a6e293d1 2594 causes the index to behave very poorly for certain requests. Version 3
831adc1f 2595 contained incomplete addrmap. So, it seems better to just ignore such
481860b3 2596 indices. */
831adc1f 2597 if (version < 4)
481860b3
GB
2598 {
2599 static int warning_printed = 0;
2600 if (!warning_printed)
2601 {
2602 warning (_("Skipping obsolete .gdb_index section in %s."),
2ec9a5e0 2603 filename);
481860b3
GB
2604 warning_printed = 1;
2605 }
2606 return 0;
2607 }
2608 /* Index version 4 uses a different hash function than index version
2609 5 and later.
2610
2611 Versions earlier than 6 did not emit psymbols for inlined
2612 functions. Using these files will cause GDB not to be able to
2613 set breakpoints on inlined functions by name, so we ignore these
e615022a
DE
2614 indices unless the user has done
2615 "set use-deprecated-index-sections on". */
2ec9a5e0 2616 if (version < 6 && !deprecated_ok)
481860b3
GB
2617 {
2618 static int warning_printed = 0;
2619 if (!warning_printed)
2620 {
e615022a
DE
2621 warning (_("\
2622Skipping deprecated .gdb_index section in %s.\n\
2623Do \"set use-deprecated-index-sections on\" before the file is read\n\
2624to use the section anyway."),
2ec9a5e0 2625 filename);
481860b3
GB
2626 warning_printed = 1;
2627 }
2628 return 0;
2629 }
2630 /* Indexes with higher version than the one supported by GDB may be no
594e8718 2631 longer backward compatible. */
156942c7 2632 if (version > 7)
594e8718 2633 return 0;
9291a0cd 2634
559a7a62 2635 map->version = version;
2ec9a5e0 2636 map->total_size = section->size;
9291a0cd
TT
2637
2638 metadata = (offset_type *) (addr + sizeof (offset_type));
1fd400ff
TT
2639
2640 i = 0;
2ec9a5e0
TT
2641 *cu_list = addr + MAYBE_SWAP (metadata[i]);
2642 *cu_list_elements = ((MAYBE_SWAP (metadata[i + 1]) - MAYBE_SWAP (metadata[i]))
2643 / 8);
1fd400ff
TT
2644 ++i;
2645
2ec9a5e0
TT
2646 *types_list = addr + MAYBE_SWAP (metadata[i]);
2647 *types_list_elements = ((MAYBE_SWAP (metadata[i + 1])
2648 - MAYBE_SWAP (metadata[i]))
2649 / 8);
987d643c 2650 ++i;
1fd400ff
TT
2651
2652 map->address_table = addr + MAYBE_SWAP (metadata[i]);
2653 map->address_table_size = (MAYBE_SWAP (metadata[i + 1])
2654 - MAYBE_SWAP (metadata[i]));
2655 ++i;
2656
3876f04e
DE
2657 map->symbol_table = (offset_type *) (addr + MAYBE_SWAP (metadata[i]));
2658 map->symbol_table_slots = ((MAYBE_SWAP (metadata[i + 1])
2659 - MAYBE_SWAP (metadata[i]))
2660 / (2 * sizeof (offset_type)));
1fd400ff 2661 ++i;
9291a0cd 2662
1fd400ff
TT
2663 map->constant_pool = addr + MAYBE_SWAP (metadata[i]);
2664
2ec9a5e0
TT
2665 return 1;
2666}
2667
2668
2669/* Read the index file. If everything went ok, initialize the "quick"
2670 elements of all the CUs and return 1. Otherwise, return 0. */
2671
2672static int
2673dwarf2_read_index (struct objfile *objfile)
2674{
2675 struct mapped_index local_map, *map;
2676 const gdb_byte *cu_list, *types_list, *dwz_list = NULL;
2677 offset_type cu_list_elements, types_list_elements, dwz_list_elements = 0;
2678
2679 if (!read_index_from_section (objfile, objfile->name,
2680 use_deprecated_index_sections,
2681 &dwarf2_per_objfile->gdb_index, &local_map,
2682 &cu_list, &cu_list_elements,
2683 &types_list, &types_list_elements))
2684 return 0;
2685
0fefef59 2686 /* Don't use the index if it's empty. */
2ec9a5e0 2687 if (local_map.symbol_table_slots == 0)
0fefef59
DE
2688 return 0;
2689
2ec9a5e0
TT
2690 /* If there is a .dwz file, read it so we can get its CU list as
2691 well. */
2692 if (bfd_get_section_by_name (objfile->obfd, ".gnu_debugaltlink") != NULL)
2693 {
2694 struct dwz_file *dwz = dwarf2_get_dwz_file ();
2695 struct mapped_index dwz_map;
2696 const gdb_byte *dwz_types_ignore;
2697 offset_type dwz_types_elements_ignore;
2698
2699 if (!read_index_from_section (objfile, bfd_get_filename (dwz->dwz_bfd),
2700 1,
2701 &dwz->gdb_index, &dwz_map,
2702 &dwz_list, &dwz_list_elements,
2703 &dwz_types_ignore,
2704 &dwz_types_elements_ignore))
2705 {
2706 warning (_("could not read '.gdb_index' section from %s; skipping"),
2707 bfd_get_filename (dwz->dwz_bfd));
2708 return 0;
2709 }
2710 }
2711
2712 if (!create_cus_from_index (objfile, cu_list, cu_list_elements,
2713 dwz_list, dwz_list_elements))
1fd400ff
TT
2714 return 0;
2715
8b70b953
TT
2716 if (types_list_elements)
2717 {
2718 struct dwarf2_section_info *section;
2719
2720 /* We can only handle a single .debug_types when we have an
2721 index. */
2722 if (VEC_length (dwarf2_section_info_def, dwarf2_per_objfile->types) != 1)
2723 return 0;
2724
2725 section = VEC_index (dwarf2_section_info_def,
2726 dwarf2_per_objfile->types, 0);
2727
2728 if (!create_signatured_type_table_from_index (objfile, section,
2729 types_list,
2730 types_list_elements))
2731 return 0;
2732 }
9291a0cd 2733
2ec9a5e0
TT
2734 create_addrmap_from_index (objfile, &local_map);
2735
2736 map = obstack_alloc (&objfile->objfile_obstack, sizeof (struct mapped_index));
2737 *map = local_map;
9291a0cd
TT
2738
2739 dwarf2_per_objfile->index_table = map;
2740 dwarf2_per_objfile->using_index = 1;
7b9f3c50
DE
2741 dwarf2_per_objfile->quick_file_names_table =
2742 create_quick_file_names_table (dwarf2_per_objfile->n_comp_units);
9291a0cd
TT
2743
2744 return 1;
2745}
2746
2747/* A helper for the "quick" functions which sets the global
2748 dwarf2_per_objfile according to OBJFILE. */
2fdf6df6 2749
9291a0cd
TT
2750static void
2751dw2_setup (struct objfile *objfile)
2752{
2753 dwarf2_per_objfile = objfile_data (objfile, dwarf2_objfile_data_key);
2754 gdb_assert (dwarf2_per_objfile);
2755}
2756
f4dc4d17
DE
2757/* Reader function for dw2_build_type_unit_groups. */
2758
2759static void
2760dw2_build_type_unit_groups_reader (const struct die_reader_specs *reader,
2761 gdb_byte *info_ptr,
2762 struct die_info *type_unit_die,
2763 int has_children,
2764 void *data)
2765{
2766 struct dwarf2_cu *cu = reader->cu;
f4dc4d17
DE
2767 struct attribute *attr;
2768 struct type_unit_group *tu_group;
2769
2770 gdb_assert (data == NULL);
2771
2772 if (! has_children)
2773 return;
2774
2775 attr = dwarf2_attr_no_follow (type_unit_die, DW_AT_stmt_list);
2776 /* Call this for its side-effect of creating the associated
2777 struct type_unit_group if it doesn't already exist. */
094b34ac 2778 tu_group = get_type_unit_group (cu, attr);
f4dc4d17
DE
2779}
2780
2781/* Build dwarf2_per_objfile->type_unit_groups.
2782 This function may be called multiple times. */
2783
2784static void
2785dw2_build_type_unit_groups (void)
2786{
2787 if (dwarf2_per_objfile->type_unit_groups == NULL)
2788 build_type_unit_groups (dw2_build_type_unit_groups_reader, NULL);
2789}
2790
dee91e82 2791/* die_reader_func for dw2_get_file_names. */
2fdf6df6 2792
dee91e82
DE
2793static void
2794dw2_get_file_names_reader (const struct die_reader_specs *reader,
2795 gdb_byte *info_ptr,
2796 struct die_info *comp_unit_die,
2797 int has_children,
2798 void *data)
9291a0cd 2799{
dee91e82
DE
2800 struct dwarf2_cu *cu = reader->cu;
2801 struct dwarf2_per_cu_data *this_cu = cu->per_cu;
2802 struct objfile *objfile = dwarf2_per_objfile->objfile;
094b34ac 2803 struct dwarf2_per_cu_data *lh_cu;
7b9f3c50 2804 struct line_header *lh;
9291a0cd 2805 struct attribute *attr;
dee91e82 2806 int i;
9291a0cd 2807 char *name, *comp_dir;
7b9f3c50
DE
2808 void **slot;
2809 struct quick_file_names *qfn;
2810 unsigned int line_offset;
9291a0cd 2811
07261596
TT
2812 /* Our callers never want to match partial units -- instead they
2813 will match the enclosing full CU. */
2814 if (comp_unit_die->tag == DW_TAG_partial_unit)
2815 {
2816 this_cu->v.quick->no_file_data = 1;
2817 return;
2818 }
2819
094b34ac
DE
2820 /* If we're reading the line header for TUs, store it in the "per_cu"
2821 for tu_group. */
2822 if (this_cu->is_debug_types)
2823 {
2824 struct type_unit_group *tu_group = data;
2825
2826 gdb_assert (tu_group != NULL);
2827 lh_cu = &tu_group->per_cu;
2828 }
2829 else
2830 lh_cu = this_cu;
2831
7b9f3c50
DE
2832 lh = NULL;
2833 slot = NULL;
2834 line_offset = 0;
dee91e82
DE
2835
2836 attr = dwarf2_attr (comp_unit_die, DW_AT_stmt_list, cu);
9291a0cd
TT
2837 if (attr)
2838 {
7b9f3c50
DE
2839 struct quick_file_names find_entry;
2840
2841 line_offset = DW_UNSND (attr);
2842
2843 /* We may have already read in this line header (TU line header sharing).
2844 If we have we're done. */
094b34ac
DE
2845 find_entry.hash.dwo_unit = cu->dwo_unit;
2846 find_entry.hash.line_offset.sect_off = line_offset;
7b9f3c50
DE
2847 slot = htab_find_slot (dwarf2_per_objfile->quick_file_names_table,
2848 &find_entry, INSERT);
2849 if (*slot != NULL)
2850 {
094b34ac 2851 lh_cu->v.quick->file_names = *slot;
dee91e82 2852 return;
7b9f3c50
DE
2853 }
2854
3019eac3 2855 lh = dwarf_decode_line_header (line_offset, cu);
9291a0cd
TT
2856 }
2857 if (lh == NULL)
2858 {
094b34ac 2859 lh_cu->v.quick->no_file_data = 1;
dee91e82 2860 return;
9291a0cd
TT
2861 }
2862
7b9f3c50 2863 qfn = obstack_alloc (&objfile->objfile_obstack, sizeof (*qfn));
094b34ac
DE
2864 qfn->hash.dwo_unit = cu->dwo_unit;
2865 qfn->hash.line_offset.sect_off = line_offset;
7b9f3c50
DE
2866 gdb_assert (slot != NULL);
2867 *slot = qfn;
9291a0cd 2868
dee91e82 2869 find_file_and_directory (comp_unit_die, cu, &name, &comp_dir);
9291a0cd 2870
7b9f3c50
DE
2871 qfn->num_file_names = lh->num_file_names;
2872 qfn->file_names = obstack_alloc (&objfile->objfile_obstack,
2873 lh->num_file_names * sizeof (char *));
9291a0cd 2874 for (i = 0; i < lh->num_file_names; ++i)
7b9f3c50
DE
2875 qfn->file_names[i] = file_full_name (i + 1, lh, comp_dir);
2876 qfn->real_names = NULL;
9291a0cd 2877
7b9f3c50 2878 free_line_header (lh);
7b9f3c50 2879
094b34ac 2880 lh_cu->v.quick->file_names = qfn;
dee91e82
DE
2881}
2882
2883/* A helper for the "quick" functions which attempts to read the line
2884 table for THIS_CU. */
2885
2886static struct quick_file_names *
2887dw2_get_file_names (struct objfile *objfile,
2888 struct dwarf2_per_cu_data *this_cu)
2889{
f4dc4d17
DE
2890 /* For TUs this should only be called on the parent group. */
2891 if (this_cu->is_debug_types)
2892 gdb_assert (IS_TYPE_UNIT_GROUP (this_cu));
2893
dee91e82
DE
2894 if (this_cu->v.quick->file_names != NULL)
2895 return this_cu->v.quick->file_names;
2896 /* If we know there is no line data, no point in looking again. */
2897 if (this_cu->v.quick->no_file_data)
2898 return NULL;
2899
3019eac3
DE
2900 /* If DWO files are in use, we can still find the DW_AT_stmt_list attribute
2901 in the stub for CUs, there's is no need to lookup the DWO file.
2902 However, that's not the case for TUs where DW_AT_stmt_list lives in the
2903 DWO file. */
2904 if (this_cu->is_debug_types)
094b34ac
DE
2905 {
2906 struct type_unit_group *tu_group = this_cu->s.type_unit_group;
2907
2908 init_cutu_and_read_dies (tu_group->t.first_tu, NULL, 0, 0,
2909 dw2_get_file_names_reader, tu_group);
2910 }
3019eac3
DE
2911 else
2912 init_cutu_and_read_dies_simple (this_cu, dw2_get_file_names_reader, NULL);
dee91e82
DE
2913
2914 if (this_cu->v.quick->no_file_data)
2915 return NULL;
2916 return this_cu->v.quick->file_names;
9291a0cd
TT
2917}
2918
2919/* A helper for the "quick" functions which computes and caches the
7b9f3c50 2920 real path for a given file name from the line table. */
2fdf6df6 2921
9291a0cd 2922static const char *
7b9f3c50
DE
2923dw2_get_real_path (struct objfile *objfile,
2924 struct quick_file_names *qfn, int index)
9291a0cd 2925{
7b9f3c50
DE
2926 if (qfn->real_names == NULL)
2927 qfn->real_names = OBSTACK_CALLOC (&objfile->objfile_obstack,
2928 qfn->num_file_names, sizeof (char *));
9291a0cd 2929
7b9f3c50
DE
2930 if (qfn->real_names[index] == NULL)
2931 qfn->real_names[index] = gdb_realpath (qfn->file_names[index]);
9291a0cd 2932
7b9f3c50 2933 return qfn->real_names[index];
9291a0cd
TT
2934}
2935
2936static struct symtab *
2937dw2_find_last_source_symtab (struct objfile *objfile)
2938{
2939 int index;
ae2de4f8 2940
9291a0cd
TT
2941 dw2_setup (objfile);
2942 index = dwarf2_per_objfile->n_comp_units - 1;
a0f42c21 2943 return dw2_instantiate_symtab (dw2_get_cu (index));
9291a0cd
TT
2944}
2945
7b9f3c50
DE
2946/* Traversal function for dw2_forget_cached_source_info. */
2947
2948static int
2949dw2_free_cached_file_names (void **slot, void *info)
9291a0cd 2950{
7b9f3c50 2951 struct quick_file_names *file_data = (struct quick_file_names *) *slot;
9291a0cd 2952
7b9f3c50 2953 if (file_data->real_names)
9291a0cd 2954 {
7b9f3c50 2955 int i;
9291a0cd 2956
7b9f3c50 2957 for (i = 0; i < file_data->num_file_names; ++i)
9291a0cd 2958 {
7b9f3c50
DE
2959 xfree ((void*) file_data->real_names[i]);
2960 file_data->real_names[i] = NULL;
9291a0cd
TT
2961 }
2962 }
7b9f3c50
DE
2963
2964 return 1;
2965}
2966
2967static void
2968dw2_forget_cached_source_info (struct objfile *objfile)
2969{
2970 dw2_setup (objfile);
2971
2972 htab_traverse_noresize (dwarf2_per_objfile->quick_file_names_table,
2973 dw2_free_cached_file_names, NULL);
9291a0cd
TT
2974}
2975
f8eba3c6
TT
2976/* Helper function for dw2_map_symtabs_matching_filename that expands
2977 the symtabs and calls the iterator. */
2978
2979static int
2980dw2_map_expand_apply (struct objfile *objfile,
2981 struct dwarf2_per_cu_data *per_cu,
2982 const char *name,
2983 const char *full_path, const char *real_path,
2984 int (*callback) (struct symtab *, void *),
2985 void *data)
2986{
2987 struct symtab *last_made = objfile->symtabs;
2988
2989 /* Don't visit already-expanded CUs. */
2990 if (per_cu->v.quick->symtab)
2991 return 0;
2992
2993 /* This may expand more than one symtab, and we want to iterate over
2994 all of them. */
a0f42c21 2995 dw2_instantiate_symtab (per_cu);
f8eba3c6
TT
2996
2997 return iterate_over_some_symtabs (name, full_path, real_path, callback, data,
2998 objfile->symtabs, last_made);
2999}
3000
3001/* Implementation of the map_symtabs_matching_filename method. */
3002
9291a0cd 3003static int
f8eba3c6
TT
3004dw2_map_symtabs_matching_filename (struct objfile *objfile, const char *name,
3005 const char *full_path, const char *real_path,
3006 int (*callback) (struct symtab *, void *),
3007 void *data)
9291a0cd
TT
3008{
3009 int i;
c011a4f4 3010 const char *name_basename = lbasename (name);
4aac40c8
TT
3011 int name_len = strlen (name);
3012 int is_abs = IS_ABSOLUTE_PATH (name);
9291a0cd
TT
3013
3014 dw2_setup (objfile);
ae2de4f8 3015
f4dc4d17
DE
3016 dw2_build_type_unit_groups ();
3017
1fd400ff 3018 for (i = 0; i < (dwarf2_per_objfile->n_comp_units
f4dc4d17 3019 + dwarf2_per_objfile->n_type_unit_groups); ++i)
9291a0cd
TT
3020 {
3021 int j;
f4dc4d17 3022 struct dwarf2_per_cu_data *per_cu = dw2_get_primary_cu (i);
7b9f3c50 3023 struct quick_file_names *file_data;
9291a0cd 3024
3d7bb9d9 3025 /* We only need to look at symtabs not already expanded. */
e254ef6a 3026 if (per_cu->v.quick->symtab)
9291a0cd
TT
3027 continue;
3028
7b9f3c50
DE
3029 file_data = dw2_get_file_names (objfile, per_cu);
3030 if (file_data == NULL)
9291a0cd
TT
3031 continue;
3032
7b9f3c50 3033 for (j = 0; j < file_data->num_file_names; ++j)
9291a0cd 3034 {
7b9f3c50 3035 const char *this_name = file_data->file_names[j];
9291a0cd 3036
4aac40c8
TT
3037 if (FILENAME_CMP (name, this_name) == 0
3038 || (!is_abs && compare_filenames_for_search (this_name,
3039 name, name_len)))
9291a0cd 3040 {
f8eba3c6
TT
3041 if (dw2_map_expand_apply (objfile, per_cu,
3042 name, full_path, real_path,
3043 callback, data))
3044 return 1;
4aac40c8 3045 }
9291a0cd 3046
c011a4f4
DE
3047 /* Before we invoke realpath, which can get expensive when many
3048 files are involved, do a quick comparison of the basenames. */
3049 if (! basenames_may_differ
3050 && FILENAME_CMP (lbasename (this_name), name_basename) != 0)
3051 continue;
3052
9291a0cd
TT
3053 if (full_path != NULL)
3054 {
7b9f3c50
DE
3055 const char *this_real_name = dw2_get_real_path (objfile,
3056 file_data, j);
9291a0cd 3057
7b9f3c50 3058 if (this_real_name != NULL
4aac40c8
TT
3059 && (FILENAME_CMP (full_path, this_real_name) == 0
3060 || (!is_abs
3061 && compare_filenames_for_search (this_real_name,
3062 name, name_len))))
9291a0cd 3063 {
f8eba3c6
TT
3064 if (dw2_map_expand_apply (objfile, per_cu,
3065 name, full_path, real_path,
3066 callback, data))
3067 return 1;
9291a0cd
TT
3068 }
3069 }
3070
3071 if (real_path != NULL)
3072 {
7b9f3c50
DE
3073 const char *this_real_name = dw2_get_real_path (objfile,
3074 file_data, j);
9291a0cd 3075
7b9f3c50 3076 if (this_real_name != NULL
4aac40c8
TT
3077 && (FILENAME_CMP (real_path, this_real_name) == 0
3078 || (!is_abs
3079 && compare_filenames_for_search (this_real_name,
3080 name, name_len))))
9291a0cd 3081 {
f8eba3c6
TT
3082 if (dw2_map_expand_apply (objfile, per_cu,
3083 name, full_path, real_path,
3084 callback, data))
3085 return 1;
9291a0cd
TT
3086 }
3087 }
3088 }
3089 }
3090
9291a0cd
TT
3091 return 0;
3092}
3093
3094static struct symtab *
3095dw2_lookup_symbol (struct objfile *objfile, int block_index,
3096 const char *name, domain_enum domain)
3097{
774b6a14 3098 /* We do all the work in the pre_expand_symtabs_matching hook
9291a0cd
TT
3099 instead. */
3100 return NULL;
3101}
3102
3103/* A helper function that expands all symtabs that hold an object
156942c7
DE
3104 named NAME. If WANT_SPECIFIC_BLOCK is non-zero, only look for
3105 symbols in block BLOCK_KIND. */
2fdf6df6 3106
9291a0cd 3107static void
156942c7
DE
3108dw2_do_expand_symtabs_matching (struct objfile *objfile,
3109 int want_specific_block,
3110 enum block_enum block_kind,
3111 const char *name, domain_enum domain)
9291a0cd 3112{
156942c7
DE
3113 struct mapped_index *index;
3114
9291a0cd
TT
3115 dw2_setup (objfile);
3116
156942c7
DE
3117 index = dwarf2_per_objfile->index_table;
3118
ae2de4f8 3119 /* index_table is NULL if OBJF_READNOW. */
156942c7 3120 if (index)
9291a0cd
TT
3121 {
3122 offset_type *vec;
3123
156942c7 3124 if (find_slot_in_mapped_hash (index, name, &vec))
9291a0cd
TT
3125 {
3126 offset_type i, len = MAYBE_SWAP (*vec);
3127 for (i = 0; i < len; ++i)
3128 {
156942c7
DE
3129 offset_type cu_index_and_attrs = MAYBE_SWAP (vec[i + 1]);
3130 offset_type cu_index = GDB_INDEX_CU_VALUE (cu_index_and_attrs);
e254ef6a 3131 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (cu_index);
156942c7
DE
3132 int want_static = block_kind != GLOBAL_BLOCK;
3133 /* This value is only valid for index versions >= 7. */
3134 int is_static = GDB_INDEX_SYMBOL_STATIC_VALUE (cu_index_and_attrs);
3135 gdb_index_symbol_kind symbol_kind =
3136 GDB_INDEX_SYMBOL_KIND_VALUE (cu_index_and_attrs);
83a788b8
DE
3137 /* Only check the symbol attributes if they're present.
3138 Indices prior to version 7 don't record them,
3139 and indices >= 7 may elide them for certain symbols
3140 (gold does this). */
3141 int attrs_valid =
3142 (index->version >= 7
3143 && symbol_kind != GDB_INDEX_SYMBOL_KIND_NONE);
3144
3145 if (attrs_valid
3146 && want_specific_block
156942c7
DE
3147 && want_static != is_static)
3148 continue;
3149
83a788b8
DE
3150 /* Only check the symbol's kind if it has one. */
3151 if (attrs_valid)
156942c7
DE
3152 {
3153 switch (domain)
3154 {
3155 case VAR_DOMAIN:
3156 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_VARIABLE
3157 && symbol_kind != GDB_INDEX_SYMBOL_KIND_FUNCTION
3158 /* Some types are also in VAR_DOMAIN. */
3159 && symbol_kind != GDB_INDEX_SYMBOL_KIND_TYPE)
3160 continue;
3161 break;
3162 case STRUCT_DOMAIN:
3163 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_TYPE)
3164 continue;
3165 break;
3166 case LABEL_DOMAIN:
3167 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_OTHER)
3168 continue;
3169 break;
3170 default:
3171 break;
3172 }
3173 }
1fd400ff 3174
a0f42c21 3175 dw2_instantiate_symtab (per_cu);
9291a0cd
TT
3176 }
3177 }
3178 }
3179}
3180
774b6a14
TT
3181static void
3182dw2_pre_expand_symtabs_matching (struct objfile *objfile,
8903c50d 3183 enum block_enum block_kind, const char *name,
774b6a14 3184 domain_enum domain)
9291a0cd 3185{
156942c7 3186 dw2_do_expand_symtabs_matching (objfile, 1, block_kind, name, domain);
9291a0cd
TT
3187}
3188
3189static void
3190dw2_print_stats (struct objfile *objfile)
3191{
3192 int i, count;
3193
3194 dw2_setup (objfile);
3195 count = 0;
1fd400ff 3196 for (i = 0; i < (dwarf2_per_objfile->n_comp_units
d467dd73 3197 + dwarf2_per_objfile->n_type_units); ++i)
9291a0cd 3198 {
e254ef6a 3199 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
9291a0cd 3200
e254ef6a 3201 if (!per_cu->v.quick->symtab)
9291a0cd
TT
3202 ++count;
3203 }
3204 printf_filtered (_(" Number of unread CUs: %d\n"), count);
3205}
3206
3207static void
3208dw2_dump (struct objfile *objfile)
3209{
3210 /* Nothing worth printing. */
3211}
3212
3213static void
3214dw2_relocate (struct objfile *objfile, struct section_offsets *new_offsets,
3215 struct section_offsets *delta)
3216{
3217 /* There's nothing to relocate here. */
3218}
3219
3220static void
3221dw2_expand_symtabs_for_function (struct objfile *objfile,
3222 const char *func_name)
3223{
156942c7
DE
3224 /* Note: It doesn't matter what we pass for block_kind here. */
3225 dw2_do_expand_symtabs_matching (objfile, 0, GLOBAL_BLOCK, func_name,
3226 VAR_DOMAIN);
9291a0cd
TT
3227}
3228
3229static void
3230dw2_expand_all_symtabs (struct objfile *objfile)
3231{
3232 int i;
3233
3234 dw2_setup (objfile);
1fd400ff
TT
3235
3236 for (i = 0; i < (dwarf2_per_objfile->n_comp_units
d467dd73 3237 + dwarf2_per_objfile->n_type_units); ++i)
9291a0cd 3238 {
e254ef6a 3239 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
9291a0cd 3240
a0f42c21 3241 dw2_instantiate_symtab (per_cu);
9291a0cd
TT
3242 }
3243}
3244
3245static void
3246dw2_expand_symtabs_with_filename (struct objfile *objfile,
3247 const char *filename)
3248{
3249 int i;
3250
3251 dw2_setup (objfile);
d4637a04
DE
3252
3253 /* We don't need to consider type units here.
3254 This is only called for examining code, e.g. expand_line_sal.
3255 There can be an order of magnitude (or more) more type units
3256 than comp units, and we avoid them if we can. */
3257
3258 for (i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
9291a0cd
TT
3259 {
3260 int j;
e254ef6a 3261 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
7b9f3c50 3262 struct quick_file_names *file_data;
9291a0cd 3263
3d7bb9d9 3264 /* We only need to look at symtabs not already expanded. */
e254ef6a 3265 if (per_cu->v.quick->symtab)
9291a0cd
TT
3266 continue;
3267
7b9f3c50
DE
3268 file_data = dw2_get_file_names (objfile, per_cu);
3269 if (file_data == NULL)
9291a0cd
TT
3270 continue;
3271
7b9f3c50 3272 for (j = 0; j < file_data->num_file_names; ++j)
9291a0cd 3273 {
7b9f3c50 3274 const char *this_name = file_data->file_names[j];
1ef75ecc 3275 if (FILENAME_CMP (this_name, filename) == 0)
9291a0cd 3276 {
a0f42c21 3277 dw2_instantiate_symtab (per_cu);
9291a0cd
TT
3278 break;
3279 }
3280 }
3281 }
3282}
3283
356d9f9d
TT
3284/* A helper function for dw2_find_symbol_file that finds the primary
3285 file name for a given CU. This is a die_reader_func. */
3286
3287static void
3288dw2_get_primary_filename_reader (const struct die_reader_specs *reader,
3289 gdb_byte *info_ptr,
3290 struct die_info *comp_unit_die,
3291 int has_children,
3292 void *data)
3293{
3294 const char **result_ptr = data;
3295 struct dwarf2_cu *cu = reader->cu;
3296 struct attribute *attr;
3297
3298 attr = dwarf2_attr (comp_unit_die, DW_AT_name, cu);
3299 if (attr == NULL)
3300 *result_ptr = NULL;
3301 else
3302 *result_ptr = DW_STRING (attr);
3303}
3304
dd786858 3305static const char *
9291a0cd
TT
3306dw2_find_symbol_file (struct objfile *objfile, const char *name)
3307{
e254ef6a 3308 struct dwarf2_per_cu_data *per_cu;
9291a0cd 3309 offset_type *vec;
7b9f3c50 3310 struct quick_file_names *file_data;
356d9f9d 3311 const char *filename;
9291a0cd
TT
3312
3313 dw2_setup (objfile);
3314
ae2de4f8 3315 /* index_table is NULL if OBJF_READNOW. */
9291a0cd 3316 if (!dwarf2_per_objfile->index_table)
96408a79
SA
3317 {
3318 struct symtab *s;
3319
d790cf0a
DE
3320 ALL_OBJFILE_PRIMARY_SYMTABS (objfile, s)
3321 {
3322 struct blockvector *bv = BLOCKVECTOR (s);
3323 const struct block *block = BLOCKVECTOR_BLOCK (bv, GLOBAL_BLOCK);
3324 struct symbol *sym = lookup_block_symbol (block, name, VAR_DOMAIN);
3325
3326 if (sym)
3327 return sym->symtab->filename;
3328 }
96408a79
SA
3329 return NULL;
3330 }
9291a0cd
TT
3331
3332 if (!find_slot_in_mapped_hash (dwarf2_per_objfile->index_table,
3333 name, &vec))
3334 return NULL;
3335
3336 /* Note that this just looks at the very first one named NAME -- but
3337 actually we are looking for a function. find_main_filename
3338 should be rewritten so that it doesn't require a custom hook. It
3339 could just use the ordinary symbol tables. */
3340 /* vec[0] is the length, which must always be >0. */
156942c7 3341 per_cu = dw2_get_cu (GDB_INDEX_CU_VALUE (MAYBE_SWAP (vec[1])));
9291a0cd 3342
356d9f9d
TT
3343 if (per_cu->v.quick->symtab != NULL)
3344 return per_cu->v.quick->symtab->filename;
3345
f4dc4d17
DE
3346 init_cutu_and_read_dies (per_cu, NULL, 0, 0,
3347 dw2_get_primary_filename_reader, &filename);
9291a0cd 3348
356d9f9d 3349 return filename;
9291a0cd
TT
3350}
3351
3352static void
40658b94
PH
3353dw2_map_matching_symbols (const char * name, domain_enum namespace,
3354 struct objfile *objfile, int global,
3355 int (*callback) (struct block *,
3356 struct symbol *, void *),
2edb89d3
JK
3357 void *data, symbol_compare_ftype *match,
3358 symbol_compare_ftype *ordered_compare)
9291a0cd 3359{
40658b94 3360 /* Currently unimplemented; used for Ada. The function can be called if the
a9e6a4bb
JK
3361 current language is Ada for a non-Ada objfile using GNU index. As Ada
3362 does not look for non-Ada symbols this function should just return. */
9291a0cd
TT
3363}
3364
3365static void
f8eba3c6
TT
3366dw2_expand_symtabs_matching
3367 (struct objfile *objfile,
3368 int (*file_matcher) (const char *, void *),
e078317b 3369 int (*name_matcher) (const char *, void *),
f8eba3c6
TT
3370 enum search_domain kind,
3371 void *data)
9291a0cd
TT
3372{
3373 int i;
3374 offset_type iter;
4b5246aa 3375 struct mapped_index *index;
9291a0cd
TT
3376
3377 dw2_setup (objfile);
ae2de4f8
DE
3378
3379 /* index_table is NULL if OBJF_READNOW. */
9291a0cd
TT
3380 if (!dwarf2_per_objfile->index_table)
3381 return;
4b5246aa 3382 index = dwarf2_per_objfile->index_table;
9291a0cd 3383
7b08b9eb 3384 if (file_matcher != NULL)
24c79950
TT
3385 {
3386 struct cleanup *cleanup;
3387 htab_t visited_found, visited_not_found;
3388
f4dc4d17
DE
3389 dw2_build_type_unit_groups ();
3390
24c79950
TT
3391 visited_found = htab_create_alloc (10,
3392 htab_hash_pointer, htab_eq_pointer,
3393 NULL, xcalloc, xfree);
3394 cleanup = make_cleanup_htab_delete (visited_found);
3395 visited_not_found = htab_create_alloc (10,
3396 htab_hash_pointer, htab_eq_pointer,
3397 NULL, xcalloc, xfree);
3398 make_cleanup_htab_delete (visited_not_found);
3399
3400 for (i = 0; i < (dwarf2_per_objfile->n_comp_units
f4dc4d17 3401 + dwarf2_per_objfile->n_type_unit_groups); ++i)
24c79950
TT
3402 {
3403 int j;
f4dc4d17 3404 struct dwarf2_per_cu_data *per_cu = dw2_get_primary_cu (i);
24c79950
TT
3405 struct quick_file_names *file_data;
3406 void **slot;
7b08b9eb 3407
24c79950 3408 per_cu->v.quick->mark = 0;
3d7bb9d9 3409
24c79950
TT
3410 /* We only need to look at symtabs not already expanded. */
3411 if (per_cu->v.quick->symtab)
3412 continue;
7b08b9eb 3413
24c79950
TT
3414 file_data = dw2_get_file_names (objfile, per_cu);
3415 if (file_data == NULL)
3416 continue;
7b08b9eb 3417
24c79950
TT
3418 if (htab_find (visited_not_found, file_data) != NULL)
3419 continue;
3420 else if (htab_find (visited_found, file_data) != NULL)
3421 {
3422 per_cu->v.quick->mark = 1;
3423 continue;
3424 }
3425
3426 for (j = 0; j < file_data->num_file_names; ++j)
3427 {
3428 if (file_matcher (file_data->file_names[j], data))
3429 {
3430 per_cu->v.quick->mark = 1;
3431 break;
3432 }
3433 }
3434
3435 slot = htab_find_slot (per_cu->v.quick->mark
3436 ? visited_found
3437 : visited_not_found,
3438 file_data, INSERT);
3439 *slot = file_data;
3440 }
3441
3442 do_cleanups (cleanup);
3443 }
9291a0cd 3444
3876f04e 3445 for (iter = 0; iter < index->symbol_table_slots; ++iter)
9291a0cd
TT
3446 {
3447 offset_type idx = 2 * iter;
3448 const char *name;
3449 offset_type *vec, vec_len, vec_idx;
3450
3876f04e 3451 if (index->symbol_table[idx] == 0 && index->symbol_table[idx + 1] == 0)
9291a0cd
TT
3452 continue;
3453
3876f04e 3454 name = index->constant_pool + MAYBE_SWAP (index->symbol_table[idx]);
9291a0cd 3455
e078317b 3456 if (! (*name_matcher) (name, data))
9291a0cd
TT
3457 continue;
3458
3459 /* The name was matched, now expand corresponding CUs that were
3460 marked. */
4b5246aa 3461 vec = (offset_type *) (index->constant_pool
3876f04e 3462 + MAYBE_SWAP (index->symbol_table[idx + 1]));
9291a0cd
TT
3463 vec_len = MAYBE_SWAP (vec[0]);
3464 for (vec_idx = 0; vec_idx < vec_len; ++vec_idx)
3465 {
e254ef6a 3466 struct dwarf2_per_cu_data *per_cu;
156942c7
DE
3467 offset_type cu_index_and_attrs = MAYBE_SWAP (vec[vec_idx + 1]);
3468 gdb_index_symbol_kind symbol_kind =
3469 GDB_INDEX_SYMBOL_KIND_VALUE (cu_index_and_attrs);
3470 int cu_index = GDB_INDEX_CU_VALUE (cu_index_and_attrs);
3471
3472 /* Don't crash on bad data. */
3473 if (cu_index >= (dwarf2_per_objfile->n_comp_units
667e0a4b 3474 + dwarf2_per_objfile->n_type_units))
156942c7 3475 continue;
1fd400ff 3476
156942c7
DE
3477 /* Only check the symbol's kind if it has one.
3478 Indices prior to version 7 don't record it. */
3479 if (index->version >= 7)
3480 {
3481 switch (kind)
3482 {
3483 case VARIABLES_DOMAIN:
3484 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_VARIABLE)
3485 continue;
3486 break;
3487 case FUNCTIONS_DOMAIN:
3488 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_FUNCTION)
3489 continue;
3490 break;
3491 case TYPES_DOMAIN:
3492 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_TYPE)
3493 continue;
3494 break;
3495 default:
3496 break;
3497 }
3498 }
3499
3500 per_cu = dw2_get_cu (cu_index);
7b08b9eb 3501 if (file_matcher == NULL || per_cu->v.quick->mark)
a0f42c21 3502 dw2_instantiate_symtab (per_cu);
9291a0cd
TT
3503 }
3504 }
3505}
3506
9703b513
TT
3507/* A helper for dw2_find_pc_sect_symtab which finds the most specific
3508 symtab. */
3509
3510static struct symtab *
3511recursively_find_pc_sect_symtab (struct symtab *symtab, CORE_ADDR pc)
3512{
3513 int i;
3514
3515 if (BLOCKVECTOR (symtab) != NULL
3516 && blockvector_contains_pc (BLOCKVECTOR (symtab), pc))
3517 return symtab;
3518
a3ec0bb1
DE
3519 if (symtab->includes == NULL)
3520 return NULL;
3521
9703b513
TT
3522 for (i = 0; symtab->includes[i]; ++i)
3523 {
a3ec0bb1 3524 struct symtab *s = symtab->includes[i];
9703b513
TT
3525
3526 s = recursively_find_pc_sect_symtab (s, pc);
3527 if (s != NULL)
3528 return s;
3529 }
3530
3531 return NULL;
3532}
3533
9291a0cd
TT
3534static struct symtab *
3535dw2_find_pc_sect_symtab (struct objfile *objfile,
3536 struct minimal_symbol *msymbol,
3537 CORE_ADDR pc,
3538 struct obj_section *section,
3539 int warn_if_readin)
3540{
3541 struct dwarf2_per_cu_data *data;
9703b513 3542 struct symtab *result;
9291a0cd
TT
3543
3544 dw2_setup (objfile);
3545
3546 if (!objfile->psymtabs_addrmap)
3547 return NULL;
3548
3549 data = addrmap_find (objfile->psymtabs_addrmap, pc);
3550 if (!data)
3551 return NULL;
3552
3553 if (warn_if_readin && data->v.quick->symtab)
abebb8b0 3554 warning (_("(Internal error: pc %s in read in CU, but not in symtab.)"),
9291a0cd
TT
3555 paddress (get_objfile_arch (objfile), pc));
3556
9703b513
TT
3557 result = recursively_find_pc_sect_symtab (dw2_instantiate_symtab (data), pc);
3558 gdb_assert (result != NULL);
3559 return result;
9291a0cd
TT
3560}
3561
9291a0cd 3562static void
44b13c5a 3563dw2_map_symbol_filenames (struct objfile *objfile, symbol_filename_ftype *fun,
74e2f255 3564 void *data, int need_fullname)
9291a0cd
TT
3565{
3566 int i;
24c79950
TT
3567 struct cleanup *cleanup;
3568 htab_t visited = htab_create_alloc (10, htab_hash_pointer, htab_eq_pointer,
3569 NULL, xcalloc, xfree);
9291a0cd 3570
24c79950 3571 cleanup = make_cleanup_htab_delete (visited);
9291a0cd 3572 dw2_setup (objfile);
ae2de4f8 3573
f4dc4d17
DE
3574 dw2_build_type_unit_groups ();
3575
24c79950
TT
3576 /* We can ignore file names coming from already-expanded CUs. */
3577 for (i = 0; i < (dwarf2_per_objfile->n_comp_units
3578 + dwarf2_per_objfile->n_type_units); ++i)
3579 {
3580 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
3581
3582 if (per_cu->v.quick->symtab)
3583 {
3584 void **slot = htab_find_slot (visited, per_cu->v.quick->file_names,
3585 INSERT);
3586
3587 *slot = per_cu->v.quick->file_names;
3588 }
3589 }
3590
1fd400ff 3591 for (i = 0; i < (dwarf2_per_objfile->n_comp_units
f4dc4d17 3592 + dwarf2_per_objfile->n_type_unit_groups); ++i)
9291a0cd
TT
3593 {
3594 int j;
f4dc4d17 3595 struct dwarf2_per_cu_data *per_cu = dw2_get_primary_cu (i);
7b9f3c50 3596 struct quick_file_names *file_data;
24c79950 3597 void **slot;
9291a0cd 3598
3d7bb9d9 3599 /* We only need to look at symtabs not already expanded. */
e254ef6a 3600 if (per_cu->v.quick->symtab)
9291a0cd
TT
3601 continue;
3602
7b9f3c50
DE
3603 file_data = dw2_get_file_names (objfile, per_cu);
3604 if (file_data == NULL)
9291a0cd
TT
3605 continue;
3606
24c79950
TT
3607 slot = htab_find_slot (visited, file_data, INSERT);
3608 if (*slot)
3609 {
3610 /* Already visited. */
3611 continue;
3612 }
3613 *slot = file_data;
3614
7b9f3c50 3615 for (j = 0; j < file_data->num_file_names; ++j)
9291a0cd 3616 {
74e2f255
DE
3617 const char *this_real_name;
3618
3619 if (need_fullname)
3620 this_real_name = dw2_get_real_path (objfile, file_data, j);
3621 else
3622 this_real_name = NULL;
7b9f3c50 3623 (*fun) (file_data->file_names[j], this_real_name, data);
9291a0cd
TT
3624 }
3625 }
24c79950
TT
3626
3627 do_cleanups (cleanup);
9291a0cd
TT
3628}
3629
3630static int
3631dw2_has_symbols (struct objfile *objfile)
3632{
3633 return 1;
3634}
3635
3636const struct quick_symbol_functions dwarf2_gdb_index_functions =
3637{
3638 dw2_has_symbols,
3639 dw2_find_last_source_symtab,
3640 dw2_forget_cached_source_info,
f8eba3c6 3641 dw2_map_symtabs_matching_filename,
9291a0cd 3642 dw2_lookup_symbol,
774b6a14 3643 dw2_pre_expand_symtabs_matching,
9291a0cd
TT
3644 dw2_print_stats,
3645 dw2_dump,
3646 dw2_relocate,
3647 dw2_expand_symtabs_for_function,
3648 dw2_expand_all_symtabs,
3649 dw2_expand_symtabs_with_filename,
3650 dw2_find_symbol_file,
40658b94 3651 dw2_map_matching_symbols,
9291a0cd
TT
3652 dw2_expand_symtabs_matching,
3653 dw2_find_pc_sect_symtab,
9291a0cd
TT
3654 dw2_map_symbol_filenames
3655};
3656
3657/* Initialize for reading DWARF for this objfile. Return 0 if this
3658 file will use psymtabs, or 1 if using the GNU index. */
3659
3660int
3661dwarf2_initialize_objfile (struct objfile *objfile)
3662{
3663 /* If we're about to read full symbols, don't bother with the
3664 indices. In this case we also don't care if some other debug
3665 format is making psymtabs, because they are all about to be
3666 expanded anyway. */
3667 if ((objfile->flags & OBJF_READNOW))
3668 {
3669 int i;
3670
3671 dwarf2_per_objfile->using_index = 1;
3672 create_all_comp_units (objfile);
0e50663e 3673 create_all_type_units (objfile);
7b9f3c50
DE
3674 dwarf2_per_objfile->quick_file_names_table =
3675 create_quick_file_names_table (dwarf2_per_objfile->n_comp_units);
9291a0cd 3676
1fd400ff 3677 for (i = 0; i < (dwarf2_per_objfile->n_comp_units
d467dd73 3678 + dwarf2_per_objfile->n_type_units); ++i)
9291a0cd 3679 {
e254ef6a 3680 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
9291a0cd 3681
e254ef6a
DE
3682 per_cu->v.quick = OBSTACK_ZALLOC (&objfile->objfile_obstack,
3683 struct dwarf2_per_cu_quick_data);
9291a0cd
TT
3684 }
3685
3686 /* Return 1 so that gdb sees the "quick" functions. However,
3687 these functions will be no-ops because we will have expanded
3688 all symtabs. */
3689 return 1;
3690 }
3691
3692 if (dwarf2_read_index (objfile))
3693 return 1;
3694
9291a0cd
TT
3695 return 0;
3696}
3697
3698\f
3699
dce234bc
PP
3700/* Build a partial symbol table. */
3701
3702void
f29dff0a 3703dwarf2_build_psymtabs (struct objfile *objfile)
dce234bc 3704{
f29dff0a 3705 if (objfile->global_psymbols.size == 0 && objfile->static_psymbols.size == 0)
c906108c
SS
3706 {
3707 init_psymbol_list (objfile, 1024);
3708 }
3709
d146bf1e 3710 dwarf2_build_psymtabs_hard (objfile);
c906108c 3711}
c906108c 3712
1ce1cefd
DE
3713/* Return the total length of the CU described by HEADER. */
3714
3715static unsigned int
3716get_cu_length (const struct comp_unit_head *header)
3717{
3718 return header->initial_length_size + header->length;
3719}
3720
45452591
DE
3721/* Return TRUE if OFFSET is within CU_HEADER. */
3722
3723static inline int
b64f50a1 3724offset_in_cu_p (const struct comp_unit_head *cu_header, sect_offset offset)
45452591 3725{
b64f50a1 3726 sect_offset bottom = { cu_header->offset.sect_off };
1ce1cefd 3727 sect_offset top = { cu_header->offset.sect_off + get_cu_length (cu_header) };
9a619af0 3728
b64f50a1 3729 return (offset.sect_off >= bottom.sect_off && offset.sect_off < top.sect_off);
45452591
DE
3730}
3731
3b80fe9b
DE
3732/* Find the base address of the compilation unit for range lists and
3733 location lists. It will normally be specified by DW_AT_low_pc.
3734 In DWARF-3 draft 4, the base address could be overridden by
3735 DW_AT_entry_pc. It's been removed, but GCC still uses this for
3736 compilation units with discontinuous ranges. */
3737
3738static void
3739dwarf2_find_base_address (struct die_info *die, struct dwarf2_cu *cu)
3740{
3741 struct attribute *attr;
3742
3743 cu->base_known = 0;
3744 cu->base_address = 0;
3745
3746 attr = dwarf2_attr (die, DW_AT_entry_pc, cu);
3747 if (attr)
3748 {
3749 cu->base_address = DW_ADDR (attr);
3750 cu->base_known = 1;
3751 }
3752 else
3753 {
3754 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
3755 if (attr)
3756 {
3757 cu->base_address = DW_ADDR (attr);
3758 cu->base_known = 1;
3759 }
3760 }
3761}
3762
93311388
DE
3763/* Read in the comp unit header information from the debug_info at info_ptr.
3764 NOTE: This leaves members offset, first_die_offset to be filled in
3765 by the caller. */
107d2387 3766
fe1b8b76 3767static gdb_byte *
107d2387 3768read_comp_unit_head (struct comp_unit_head *cu_header,
fe1b8b76 3769 gdb_byte *info_ptr, bfd *abfd)
107d2387
AC
3770{
3771 int signed_addr;
891d2f0b 3772 unsigned int bytes_read;
c764a876
DE
3773
3774 cu_header->length = read_initial_length (abfd, info_ptr, &bytes_read);
3775 cu_header->initial_length_size = bytes_read;
3776 cu_header->offset_size = (bytes_read == 4) ? 4 : 8;
613e1657 3777 info_ptr += bytes_read;
107d2387
AC
3778 cu_header->version = read_2_bytes (abfd, info_ptr);
3779 info_ptr += 2;
b64f50a1
JK
3780 cu_header->abbrev_offset.sect_off = read_offset (abfd, info_ptr, cu_header,
3781 &bytes_read);
613e1657 3782 info_ptr += bytes_read;
107d2387
AC
3783 cu_header->addr_size = read_1_byte (abfd, info_ptr);
3784 info_ptr += 1;
3785 signed_addr = bfd_get_sign_extend_vma (abfd);
3786 if (signed_addr < 0)
8e65ff28 3787 internal_error (__FILE__, __LINE__,
e2e0b3e5 3788 _("read_comp_unit_head: dwarf from non elf file"));
107d2387 3789 cu_header->signed_addr_p = signed_addr;
c764a876 3790
107d2387
AC
3791 return info_ptr;
3792}
3793
36586728
TT
3794/* Helper function that returns the proper abbrev section for
3795 THIS_CU. */
3796
3797static struct dwarf2_section_info *
3798get_abbrev_section_for_cu (struct dwarf2_per_cu_data *this_cu)
3799{
3800 struct dwarf2_section_info *abbrev;
3801
3802 if (this_cu->is_dwz)
3803 abbrev = &dwarf2_get_dwz_file ()->abbrev;
3804 else
3805 abbrev = &dwarf2_per_objfile->abbrev;
3806
3807 return abbrev;
3808}
3809
9ff913ba
DE
3810/* Subroutine of read_and_check_comp_unit_head and
3811 read_and_check_type_unit_head to simplify them.
3812 Perform various error checking on the header. */
3813
3814static void
3815error_check_comp_unit_head (struct comp_unit_head *header,
4bdcc0c1
DE
3816 struct dwarf2_section_info *section,
3817 struct dwarf2_section_info *abbrev_section)
9ff913ba
DE
3818{
3819 bfd *abfd = section->asection->owner;
3820 const char *filename = bfd_get_filename (abfd);
3821
3822 if (header->version != 2 && header->version != 3 && header->version != 4)
3823 error (_("Dwarf Error: wrong version in compilation unit header "
3824 "(is %d, should be 2, 3, or 4) [in module %s]"), header->version,
3825 filename);
3826
b64f50a1 3827 if (header->abbrev_offset.sect_off
36586728 3828 >= dwarf2_section_size (dwarf2_per_objfile->objfile, abbrev_section))
9ff913ba
DE
3829 error (_("Dwarf Error: bad offset (0x%lx) in compilation unit header "
3830 "(offset 0x%lx + 6) [in module %s]"),
b64f50a1 3831 (long) header->abbrev_offset.sect_off, (long) header->offset.sect_off,
9ff913ba
DE
3832 filename);
3833
3834 /* Cast to unsigned long to use 64-bit arithmetic when possible to
3835 avoid potential 32-bit overflow. */
1ce1cefd 3836 if (((unsigned long) header->offset.sect_off + get_cu_length (header))
9ff913ba
DE
3837 > section->size)
3838 error (_("Dwarf Error: bad length (0x%lx) in compilation unit header "
3839 "(offset 0x%lx + 0) [in module %s]"),
b64f50a1 3840 (long) header->length, (long) header->offset.sect_off,
9ff913ba
DE
3841 filename);
3842}
3843
3844/* Read in a CU/TU header and perform some basic error checking.
3845 The contents of the header are stored in HEADER.
3846 The result is a pointer to the start of the first DIE. */
adabb602 3847
fe1b8b76 3848static gdb_byte *
9ff913ba
DE
3849read_and_check_comp_unit_head (struct comp_unit_head *header,
3850 struct dwarf2_section_info *section,
4bdcc0c1 3851 struct dwarf2_section_info *abbrev_section,
9ff913ba
DE
3852 gdb_byte *info_ptr,
3853 int is_debug_types_section)
72bf9492 3854{
fe1b8b76 3855 gdb_byte *beg_of_comp_unit = info_ptr;
9ff913ba 3856 bfd *abfd = section->asection->owner;
72bf9492 3857
b64f50a1 3858 header->offset.sect_off = beg_of_comp_unit - section->buffer;
adabb602 3859
72bf9492
DJ
3860 info_ptr = read_comp_unit_head (header, info_ptr, abfd);
3861
460c1c54
CC
3862 /* If we're reading a type unit, skip over the signature and
3863 type_offset fields. */
b0df02fd 3864 if (is_debug_types_section)
460c1c54
CC
3865 info_ptr += 8 /*signature*/ + header->offset_size;
3866
b64f50a1 3867 header->first_die_offset.cu_off = info_ptr - beg_of_comp_unit;
adabb602 3868
4bdcc0c1 3869 error_check_comp_unit_head (header, section, abbrev_section);
72bf9492
DJ
3870
3871 return info_ptr;
3872}
3873
348e048f
DE
3874/* Read in the types comp unit header information from .debug_types entry at
3875 types_ptr. The result is a pointer to one past the end of the header. */
3876
3877static gdb_byte *
9ff913ba
DE
3878read_and_check_type_unit_head (struct comp_unit_head *header,
3879 struct dwarf2_section_info *section,
4bdcc0c1 3880 struct dwarf2_section_info *abbrev_section,
9ff913ba 3881 gdb_byte *info_ptr,
dee91e82
DE
3882 ULONGEST *signature,
3883 cu_offset *type_offset_in_tu)
348e048f 3884{
9ff913ba
DE
3885 gdb_byte *beg_of_comp_unit = info_ptr;
3886 bfd *abfd = section->asection->owner;
348e048f 3887
b64f50a1 3888 header->offset.sect_off = beg_of_comp_unit - section->buffer;
348e048f 3889
9ff913ba 3890 info_ptr = read_comp_unit_head (header, info_ptr, abfd);
348e048f 3891
9ff913ba
DE
3892 /* If we're reading a type unit, skip over the signature and
3893 type_offset fields. */
3894 if (signature != NULL)
3895 *signature = read_8_bytes (abfd, info_ptr);
3896 info_ptr += 8;
dee91e82
DE
3897 if (type_offset_in_tu != NULL)
3898 type_offset_in_tu->cu_off = read_offset_1 (abfd, info_ptr,
3899 header->offset_size);
9ff913ba
DE
3900 info_ptr += header->offset_size;
3901
b64f50a1 3902 header->first_die_offset.cu_off = info_ptr - beg_of_comp_unit;
348e048f 3903
4bdcc0c1 3904 error_check_comp_unit_head (header, section, abbrev_section);
9ff913ba
DE
3905
3906 return info_ptr;
348e048f
DE
3907}
3908
f4dc4d17
DE
3909/* Fetch the abbreviation table offset from a comp or type unit header. */
3910
3911static sect_offset
3912read_abbrev_offset (struct dwarf2_section_info *section,
3913 sect_offset offset)
3914{
3915 bfd *abfd = section->asection->owner;
3916 gdb_byte *info_ptr;
3917 unsigned int length, initial_length_size, offset_size;
3918 sect_offset abbrev_offset;
3919
3920 dwarf2_read_section (dwarf2_per_objfile->objfile, section);
3921 info_ptr = section->buffer + offset.sect_off;
3922 length = read_initial_length (abfd, info_ptr, &initial_length_size);
3923 offset_size = initial_length_size == 4 ? 4 : 8;
3924 info_ptr += initial_length_size + 2 /*version*/;
3925 abbrev_offset.sect_off = read_offset_1 (abfd, info_ptr, offset_size);
3926 return abbrev_offset;
3927}
3928
aaa75496
JB
3929/* Allocate a new partial symtab for file named NAME and mark this new
3930 partial symtab as being an include of PST. */
3931
3932static void
3933dwarf2_create_include_psymtab (char *name, struct partial_symtab *pst,
3934 struct objfile *objfile)
3935{
3936 struct partial_symtab *subpst = allocate_psymtab (name, objfile);
3937
3938 subpst->section_offsets = pst->section_offsets;
3939 subpst->textlow = 0;
3940 subpst->texthigh = 0;
3941
3942 subpst->dependencies = (struct partial_symtab **)
3943 obstack_alloc (&objfile->objfile_obstack,
3944 sizeof (struct partial_symtab *));
3945 subpst->dependencies[0] = pst;
3946 subpst->number_of_dependencies = 1;
3947
3948 subpst->globals_offset = 0;
3949 subpst->n_global_syms = 0;
3950 subpst->statics_offset = 0;
3951 subpst->n_static_syms = 0;
3952 subpst->symtab = NULL;
3953 subpst->read_symtab = pst->read_symtab;
3954 subpst->readin = 0;
3955
3956 /* No private part is necessary for include psymtabs. This property
3957 can be used to differentiate between such include psymtabs and
10b3939b 3958 the regular ones. */
58a9656e 3959 subpst->read_symtab_private = NULL;
aaa75496
JB
3960}
3961
3962/* Read the Line Number Program data and extract the list of files
3963 included by the source file represented by PST. Build an include
d85a05f0 3964 partial symtab for each of these included files. */
aaa75496
JB
3965
3966static void
3967dwarf2_build_include_psymtabs (struct dwarf2_cu *cu,
dee91e82
DE
3968 struct die_info *die,
3969 struct partial_symtab *pst)
aaa75496 3970{
d85a05f0
DJ
3971 struct line_header *lh = NULL;
3972 struct attribute *attr;
aaa75496 3973
d85a05f0
DJ
3974 attr = dwarf2_attr (die, DW_AT_stmt_list, cu);
3975 if (attr)
3019eac3 3976 lh = dwarf_decode_line_header (DW_UNSND (attr), cu);
aaa75496
JB
3977 if (lh == NULL)
3978 return; /* No linetable, so no includes. */
3979
c6da4cef 3980 /* NOTE: pst->dirname is DW_AT_comp_dir (if present). */
f3f5162e 3981 dwarf_decode_lines (lh, pst->dirname, cu, pst, 1);
aaa75496
JB
3982
3983 free_line_header (lh);
3984}
3985
348e048f 3986static hashval_t
52dc124a 3987hash_signatured_type (const void *item)
348e048f 3988{
52dc124a 3989 const struct signatured_type *sig_type = item;
9a619af0 3990
348e048f 3991 /* This drops the top 32 bits of the signature, but is ok for a hash. */
52dc124a 3992 return sig_type->signature;
348e048f
DE
3993}
3994
3995static int
52dc124a 3996eq_signatured_type (const void *item_lhs, const void *item_rhs)
348e048f
DE
3997{
3998 const struct signatured_type *lhs = item_lhs;
3999 const struct signatured_type *rhs = item_rhs;
9a619af0 4000
348e048f
DE
4001 return lhs->signature == rhs->signature;
4002}
4003
1fd400ff
TT
4004/* Allocate a hash table for signatured types. */
4005
4006static htab_t
673bfd45 4007allocate_signatured_type_table (struct objfile *objfile)
1fd400ff
TT
4008{
4009 return htab_create_alloc_ex (41,
52dc124a
DE
4010 hash_signatured_type,
4011 eq_signatured_type,
1fd400ff
TT
4012 NULL,
4013 &objfile->objfile_obstack,
4014 hashtab_obstack_allocate,
4015 dummy_obstack_deallocate);
4016}
4017
d467dd73 4018/* A helper function to add a signatured type CU to a table. */
1fd400ff
TT
4019
4020static int
d467dd73 4021add_signatured_type_cu_to_table (void **slot, void *datum)
1fd400ff
TT
4022{
4023 struct signatured_type *sigt = *slot;
b4dd5633 4024 struct signatured_type ***datap = datum;
1fd400ff 4025
b4dd5633 4026 **datap = sigt;
1fd400ff
TT
4027 ++*datap;
4028
4029 return 1;
4030}
4031
3019eac3
DE
4032/* Create the hash table of all entries in the .debug_types section.
4033 DWO_FILE is a pointer to the DWO file for .debug_types.dwo, NULL otherwise.
4034 The result is a pointer to the hash table or NULL if there are
4035 no types. */
348e048f 4036
3019eac3
DE
4037static htab_t
4038create_debug_types_hash_table (struct dwo_file *dwo_file,
4039 VEC (dwarf2_section_info_def) *types)
348e048f 4040{
3019eac3 4041 struct objfile *objfile = dwarf2_per_objfile->objfile;
8b70b953 4042 htab_t types_htab = NULL;
8b70b953
TT
4043 int ix;
4044 struct dwarf2_section_info *section;
4bdcc0c1 4045 struct dwarf2_section_info *abbrev_section;
348e048f 4046
3019eac3
DE
4047 if (VEC_empty (dwarf2_section_info_def, types))
4048 return NULL;
348e048f 4049
4bdcc0c1
DE
4050 abbrev_section = (dwo_file != NULL
4051 ? &dwo_file->sections.abbrev
4052 : &dwarf2_per_objfile->abbrev);
4053
09406207
DE
4054 if (dwarf2_read_debug)
4055 fprintf_unfiltered (gdb_stdlog, "Reading .debug_types%s for %s:\n",
4056 dwo_file ? ".dwo" : "",
4057 bfd_get_filename (abbrev_section->asection->owner));
4058
8b70b953 4059 for (ix = 0;
3019eac3 4060 VEC_iterate (dwarf2_section_info_def, types, ix, section);
8b70b953
TT
4061 ++ix)
4062 {
3019eac3 4063 bfd *abfd;
8b70b953 4064 gdb_byte *info_ptr, *end_ptr;
36586728 4065 struct dwarf2_section_info *abbrev_section;
348e048f 4066
8b70b953
TT
4067 dwarf2_read_section (objfile, section);
4068 info_ptr = section->buffer;
348e048f 4069
8b70b953
TT
4070 if (info_ptr == NULL)
4071 continue;
348e048f 4072
3019eac3
DE
4073 /* We can't set abfd until now because the section may be empty or
4074 not present, in which case section->asection will be NULL. */
4075 abfd = section->asection->owner;
4076
36586728
TT
4077 if (dwo_file)
4078 abbrev_section = &dwo_file->sections.abbrev;
4079 else
4080 abbrev_section = &dwarf2_per_objfile->abbrev;
4081
8b70b953 4082 if (types_htab == NULL)
3019eac3
DE
4083 {
4084 if (dwo_file)
4085 types_htab = allocate_dwo_unit_table (objfile);
4086 else
4087 types_htab = allocate_signatured_type_table (objfile);
4088 }
348e048f 4089
dee91e82
DE
4090 /* We don't use init_cutu_and_read_dies_simple, or some such, here
4091 because we don't need to read any dies: the signature is in the
4092 header. */
8b70b953
TT
4093
4094 end_ptr = info_ptr + section->size;
4095 while (info_ptr < end_ptr)
4096 {
b64f50a1 4097 sect_offset offset;
3019eac3 4098 cu_offset type_offset_in_tu;
8b70b953 4099 ULONGEST signature;
52dc124a 4100 struct signatured_type *sig_type;
3019eac3 4101 struct dwo_unit *dwo_tu;
8b70b953
TT
4102 void **slot;
4103 gdb_byte *ptr = info_ptr;
9ff913ba 4104 struct comp_unit_head header;
dee91e82 4105 unsigned int length;
348e048f 4106
b64f50a1 4107 offset.sect_off = ptr - section->buffer;
348e048f 4108
8b70b953 4109 /* We need to read the type's signature in order to build the hash
9ff913ba 4110 table, but we don't need anything else just yet. */
348e048f 4111
4bdcc0c1
DE
4112 ptr = read_and_check_type_unit_head (&header, section,
4113 abbrev_section, ptr,
3019eac3 4114 &signature, &type_offset_in_tu);
6caca83c 4115
1ce1cefd 4116 length = get_cu_length (&header);
dee91e82 4117
6caca83c 4118 /* Skip dummy type units. */
dee91e82
DE
4119 if (ptr >= info_ptr + length
4120 || peek_abbrev_code (abfd, ptr) == 0)
6caca83c 4121 {
1ce1cefd 4122 info_ptr += length;
6caca83c
CC
4123 continue;
4124 }
8b70b953 4125
3019eac3
DE
4126 if (dwo_file)
4127 {
4128 sig_type = NULL;
4129 dwo_tu = OBSTACK_ZALLOC (&objfile->objfile_obstack,
4130 struct dwo_unit);
4131 dwo_tu->dwo_file = dwo_file;
4132 dwo_tu->signature = signature;
4133 dwo_tu->type_offset_in_tu = type_offset_in_tu;
4134 dwo_tu->info_or_types_section = section;
4135 dwo_tu->offset = offset;
4136 dwo_tu->length = length;
4137 }
4138 else
4139 {
4140 /* N.B.: type_offset is not usable if this type uses a DWO file.
4141 The real type_offset is in the DWO file. */
4142 dwo_tu = NULL;
4143 sig_type = OBSTACK_ZALLOC (&objfile->objfile_obstack,
4144 struct signatured_type);
4145 sig_type->signature = signature;
4146 sig_type->type_offset_in_tu = type_offset_in_tu;
4147 sig_type->per_cu.objfile = objfile;
4148 sig_type->per_cu.is_debug_types = 1;
4149 sig_type->per_cu.info_or_types_section = section;
4150 sig_type->per_cu.offset = offset;
4151 sig_type->per_cu.length = length;
4152 }
8b70b953 4153
3019eac3
DE
4154 slot = htab_find_slot (types_htab,
4155 dwo_file ? (void*) dwo_tu : (void *) sig_type,
4156 INSERT);
8b70b953
TT
4157 gdb_assert (slot != NULL);
4158 if (*slot != NULL)
4159 {
3019eac3
DE
4160 sect_offset dup_offset;
4161
4162 if (dwo_file)
4163 {
4164 const struct dwo_unit *dup_tu = *slot;
4165
4166 dup_offset = dup_tu->offset;
4167 }
4168 else
4169 {
4170 const struct signatured_type *dup_tu = *slot;
4171
4172 dup_offset = dup_tu->per_cu.offset;
4173 }
b3c8eb43 4174
8b70b953
TT
4175 complaint (&symfile_complaints,
4176 _("debug type entry at offset 0x%x is duplicate to the "
4177 "entry at offset 0x%x, signature 0x%s"),
3019eac3 4178 offset.sect_off, dup_offset.sect_off,
8b70b953 4179 phex (signature, sizeof (signature)));
8b70b953 4180 }
3019eac3 4181 *slot = dwo_file ? (void *) dwo_tu : (void *) sig_type;
348e048f 4182
09406207 4183 if (dwarf2_read_debug)
8b70b953 4184 fprintf_unfiltered (gdb_stdlog, " offset 0x%x, signature 0x%s\n",
b64f50a1
JK
4185 offset.sect_off,
4186 phex (signature, sizeof (signature)));
348e048f 4187
dee91e82 4188 info_ptr += length;
8b70b953 4189 }
348e048f
DE
4190 }
4191
3019eac3
DE
4192 return types_htab;
4193}
4194
4195/* Create the hash table of all entries in the .debug_types section,
4196 and initialize all_type_units.
4197 The result is zero if there is an error (e.g. missing .debug_types section),
4198 otherwise non-zero. */
4199
4200static int
4201create_all_type_units (struct objfile *objfile)
4202{
4203 htab_t types_htab;
b4dd5633 4204 struct signatured_type **iter;
3019eac3
DE
4205
4206 types_htab = create_debug_types_hash_table (NULL, dwarf2_per_objfile->types);
4207 if (types_htab == NULL)
4208 {
4209 dwarf2_per_objfile->signatured_types = NULL;
4210 return 0;
4211 }
4212
348e048f
DE
4213 dwarf2_per_objfile->signatured_types = types_htab;
4214
d467dd73
DE
4215 dwarf2_per_objfile->n_type_units = htab_elements (types_htab);
4216 dwarf2_per_objfile->all_type_units
1fd400ff 4217 = obstack_alloc (&objfile->objfile_obstack,
d467dd73 4218 dwarf2_per_objfile->n_type_units
b4dd5633 4219 * sizeof (struct signatured_type *));
d467dd73
DE
4220 iter = &dwarf2_per_objfile->all_type_units[0];
4221 htab_traverse_noresize (types_htab, add_signatured_type_cu_to_table, &iter);
4222 gdb_assert (iter - &dwarf2_per_objfile->all_type_units[0]
4223 == dwarf2_per_objfile->n_type_units);
1fd400ff 4224
348e048f
DE
4225 return 1;
4226}
4227
380bca97 4228/* Lookup a signature based type for DW_FORM_ref_sig8.
e319fa28 4229 Returns NULL if signature SIG is not present in the table. */
348e048f
DE
4230
4231static struct signatured_type *
e319fa28 4232lookup_signatured_type (ULONGEST sig)
348e048f
DE
4233{
4234 struct signatured_type find_entry, *entry;
4235
4236 if (dwarf2_per_objfile->signatured_types == NULL)
4237 {
4238 complaint (&symfile_complaints,
55f1336d 4239 _("missing `.debug_types' section for DW_FORM_ref_sig8 die"));
dcc07052 4240 return NULL;
348e048f
DE
4241 }
4242
4243 find_entry.signature = sig;
4244 entry = htab_find (dwarf2_per_objfile->signatured_types, &find_entry);
4245 return entry;
4246}
42e7ad6c
DE
4247\f
4248/* Low level DIE reading support. */
348e048f 4249
d85a05f0
DJ
4250/* Initialize a die_reader_specs struct from a dwarf2_cu struct. */
4251
4252static void
4253init_cu_die_reader (struct die_reader_specs *reader,
dee91e82 4254 struct dwarf2_cu *cu,
3019eac3
DE
4255 struct dwarf2_section_info *section,
4256 struct dwo_file *dwo_file)
d85a05f0 4257{
fceca515 4258 gdb_assert (section->readin && section->buffer != NULL);
dee91e82 4259 reader->abfd = section->asection->owner;
d85a05f0 4260 reader->cu = cu;
3019eac3 4261 reader->dwo_file = dwo_file;
dee91e82
DE
4262 reader->die_section = section;
4263 reader->buffer = section->buffer;
f664829e 4264 reader->buffer_end = section->buffer + section->size;
d85a05f0
DJ
4265}
4266
fd820528 4267/* Initialize a CU (or TU) and read its DIEs.
3019eac3 4268 If the CU defers to a DWO file, read the DWO file as well.
dee91e82 4269
f4dc4d17
DE
4270 ABBREV_TABLE, if non-NULL, is the abbreviation table to use.
4271 Otherwise the table specified in the comp unit header is read in and used.
4272 This is an optimization for when we already have the abbrev table.
4273
dee91e82
DE
4274 If USE_EXISTING_CU is non-zero, and THIS_CU->cu is non-NULL, then use it.
4275 Otherwise, a new CU is allocated with xmalloc.
4276
4277 If KEEP is non-zero, then if we allocated a dwarf2_cu we add it to
4278 read_in_chain. Otherwise the dwarf2_cu data is freed at the end.
4279
4280 WARNING: If THIS_CU is a "dummy CU" (used as filler by the incremental
fd820528 4281 linker) then DIE_READER_FUNC will not get called. */
aaa75496 4282
70221824 4283static void
fd820528 4284init_cutu_and_read_dies (struct dwarf2_per_cu_data *this_cu,
f4dc4d17 4285 struct abbrev_table *abbrev_table,
fd820528
DE
4286 int use_existing_cu, int keep,
4287 die_reader_func_ftype *die_reader_func,
4288 void *data)
c906108c 4289{
dee91e82 4290 struct objfile *objfile = dwarf2_per_objfile->objfile;
3019eac3
DE
4291 struct dwarf2_section_info *section = this_cu->info_or_types_section;
4292 bfd *abfd = section->asection->owner;
dee91e82
DE
4293 struct dwarf2_cu *cu;
4294 gdb_byte *begin_info_ptr, *info_ptr;
4295 struct die_reader_specs reader;
d85a05f0 4296 struct die_info *comp_unit_die;
dee91e82 4297 int has_children;
d85a05f0 4298 struct attribute *attr;
dee91e82
DE
4299 struct cleanup *cleanups, *free_cu_cleanup = NULL;
4300 struct signatured_type *sig_type = NULL;
4bdcc0c1 4301 struct dwarf2_section_info *abbrev_section;
42e7ad6c
DE
4302 /* Non-zero if CU currently points to a DWO file and we need to
4303 reread it. When this happens we need to reread the skeleton die
4304 before we can reread the DWO file. */
4305 int rereading_dwo_cu = 0;
c906108c 4306
09406207
DE
4307 if (dwarf2_die_debug)
4308 fprintf_unfiltered (gdb_stdlog, "Reading %s unit at offset 0x%x\n",
4309 this_cu->is_debug_types ? "type" : "comp",
4310 this_cu->offset.sect_off);
4311
dee91e82
DE
4312 if (use_existing_cu)
4313 gdb_assert (keep);
23745b47 4314
dee91e82
DE
4315 cleanups = make_cleanup (null_cleanup, NULL);
4316
4317 /* This is cheap if the section is already read in. */
4318 dwarf2_read_section (objfile, section);
4319
4320 begin_info_ptr = info_ptr = section->buffer + this_cu->offset.sect_off;
36586728
TT
4321
4322 abbrev_section = get_abbrev_section_for_cu (this_cu);
dee91e82
DE
4323
4324 if (use_existing_cu && this_cu->cu != NULL)
4325 {
4326 cu = this_cu->cu;
42e7ad6c
DE
4327
4328 /* If this CU is from a DWO file we need to start over, we need to
4329 refetch the attributes from the skeleton CU.
4330 This could be optimized by retrieving those attributes from when we
4331 were here the first time: the previous comp_unit_die was stored in
4332 comp_unit_obstack. But there's no data yet that we need this
4333 optimization. */
4334 if (cu->dwo_unit != NULL)
4335 rereading_dwo_cu = 1;
dee91e82
DE
4336 }
4337 else
4338 {
4339 /* If !use_existing_cu, this_cu->cu must be NULL. */
4340 gdb_assert (this_cu->cu == NULL);
4341
4342 cu = xmalloc (sizeof (*cu));
4343 init_one_comp_unit (cu, this_cu);
4344
4345 /* If an error occurs while loading, release our storage. */
4346 free_cu_cleanup = make_cleanup (free_heap_comp_unit, cu);
42e7ad6c 4347 }
dee91e82 4348
42e7ad6c
DE
4349 if (cu->header.first_die_offset.cu_off != 0 && ! rereading_dwo_cu)
4350 {
4351 /* We already have the header, there's no need to read it in again. */
4352 info_ptr += cu->header.first_die_offset.cu_off;
4353 }
4354 else
4355 {
3019eac3 4356 if (this_cu->is_debug_types)
dee91e82
DE
4357 {
4358 ULONGEST signature;
42e7ad6c 4359 cu_offset type_offset_in_tu;
dee91e82 4360
4bdcc0c1
DE
4361 info_ptr = read_and_check_type_unit_head (&cu->header, section,
4362 abbrev_section, info_ptr,
42e7ad6c
DE
4363 &signature,
4364 &type_offset_in_tu);
dee91e82 4365
42e7ad6c
DE
4366 /* Since per_cu is the first member of struct signatured_type,
4367 we can go from a pointer to one to a pointer to the other. */
4368 sig_type = (struct signatured_type *) this_cu;
4369 gdb_assert (sig_type->signature == signature);
4370 gdb_assert (sig_type->type_offset_in_tu.cu_off
4371 == type_offset_in_tu.cu_off);
dee91e82
DE
4372 gdb_assert (this_cu->offset.sect_off == cu->header.offset.sect_off);
4373
42e7ad6c
DE
4374 /* LENGTH has not been set yet for type units if we're
4375 using .gdb_index. */
1ce1cefd 4376 this_cu->length = get_cu_length (&cu->header);
3019eac3
DE
4377
4378 /* Establish the type offset that can be used to lookup the type. */
4379 sig_type->type_offset_in_section.sect_off =
4380 this_cu->offset.sect_off + sig_type->type_offset_in_tu.cu_off;
dee91e82
DE
4381 }
4382 else
4383 {
4bdcc0c1
DE
4384 info_ptr = read_and_check_comp_unit_head (&cu->header, section,
4385 abbrev_section,
4386 info_ptr, 0);
dee91e82
DE
4387
4388 gdb_assert (this_cu->offset.sect_off == cu->header.offset.sect_off);
1ce1cefd 4389 gdb_assert (this_cu->length == get_cu_length (&cu->header));
dee91e82
DE
4390 }
4391 }
10b3939b 4392
6caca83c 4393 /* Skip dummy compilation units. */
dee91e82 4394 if (info_ptr >= begin_info_ptr + this_cu->length
6caca83c
CC
4395 || peek_abbrev_code (abfd, info_ptr) == 0)
4396 {
dee91e82 4397 do_cleanups (cleanups);
21b2bd31 4398 return;
6caca83c
CC
4399 }
4400
433df2d4
DE
4401 /* If we don't have them yet, read the abbrevs for this compilation unit.
4402 And if we need to read them now, make sure they're freed when we're
42e7ad6c
DE
4403 done. Note that it's important that if the CU had an abbrev table
4404 on entry we don't free it when we're done: Somewhere up the call stack
4405 it may be in use. */
f4dc4d17
DE
4406 if (abbrev_table != NULL)
4407 {
4408 gdb_assert (cu->abbrev_table == NULL);
4409 gdb_assert (cu->header.abbrev_offset.sect_off
4410 == abbrev_table->offset.sect_off);
4411 cu->abbrev_table = abbrev_table;
4412 }
4413 else if (cu->abbrev_table == NULL)
dee91e82 4414 {
4bdcc0c1 4415 dwarf2_read_abbrevs (cu, abbrev_section);
dee91e82
DE
4416 make_cleanup (dwarf2_free_abbrev_table, cu);
4417 }
42e7ad6c
DE
4418 else if (rereading_dwo_cu)
4419 {
4420 dwarf2_free_abbrev_table (cu);
4421 dwarf2_read_abbrevs (cu, abbrev_section);
4422 }
af703f96 4423
dee91e82 4424 /* Read the top level CU/TU die. */
3019eac3 4425 init_cu_die_reader (&reader, cu, section, NULL);
dee91e82 4426 info_ptr = read_full_die (&reader, &comp_unit_die, info_ptr, &has_children);
93311388 4427
3019eac3
DE
4428 /* If we have a DWO stub, process it and then read in the DWO file.
4429 Note that if USE_EXISTING_OK != 0, and THIS_CU->cu already contains
4430 a DWO CU, that this test will fail. */
4431 attr = dwarf2_attr (comp_unit_die, DW_AT_GNU_dwo_name, cu);
4432 if (attr)
4433 {
4434 char *dwo_name = DW_STRING (attr);
42e7ad6c 4435 const char *comp_dir_string;
3019eac3
DE
4436 struct dwo_unit *dwo_unit;
4437 ULONGEST signature; /* Or dwo_id. */
42e7ad6c 4438 struct attribute *comp_dir, *stmt_list, *low_pc, *high_pc, *ranges;
3019eac3 4439 int i,num_extra_attrs;
4bdcc0c1 4440 struct dwarf2_section_info *dwo_abbrev_section;
3019eac3
DE
4441
4442 if (has_children)
4443 error (_("Dwarf Error: compilation unit with DW_AT_GNU_dwo_name"
4444 " has children (offset 0x%x) [in module %s]"),
4445 this_cu->offset.sect_off, bfd_get_filename (abfd));
4446
4447 /* These attributes aren't processed until later:
4448 DW_AT_stmt_list, DW_AT_low_pc, DW_AT_high_pc, DW_AT_ranges.
4449 However, the attribute is found in the stub which we won't have later.
4450 In order to not impose this complication on the rest of the code,
4451 we read them here and copy them to the DWO CU/TU die. */
3019eac3
DE
4452
4453 /* For TUs in DWO files, the DW_AT_stmt_list attribute lives in the
4454 DWO file. */
42e7ad6c 4455 stmt_list = NULL;
3019eac3
DE
4456 if (! this_cu->is_debug_types)
4457 stmt_list = dwarf2_attr (comp_unit_die, DW_AT_stmt_list, cu);
4458 low_pc = dwarf2_attr (comp_unit_die, DW_AT_low_pc, cu);
4459 high_pc = dwarf2_attr (comp_unit_die, DW_AT_high_pc, cu);
4460 ranges = dwarf2_attr (comp_unit_die, DW_AT_ranges, cu);
42e7ad6c 4461 comp_dir = dwarf2_attr (comp_unit_die, DW_AT_comp_dir, cu);
3019eac3
DE
4462
4463 /* There should be a DW_AT_addr_base attribute here (if needed).
4464 We need the value before we can process DW_FORM_GNU_addr_index. */
4465 cu->addr_base = 0;
3019eac3
DE
4466 attr = dwarf2_attr (comp_unit_die, DW_AT_GNU_addr_base, cu);
4467 if (attr)
2e3cf129
DE
4468 cu->addr_base = DW_UNSND (attr);
4469
4470 /* There should be a DW_AT_ranges_base attribute here (if needed).
4471 We need the value before we can process DW_AT_ranges. */
4472 cu->ranges_base = 0;
4473 attr = dwarf2_attr (comp_unit_die, DW_AT_GNU_ranges_base, cu);
4474 if (attr)
4475 cu->ranges_base = DW_UNSND (attr);
3019eac3
DE
4476
4477 if (this_cu->is_debug_types)
4478 {
4479 gdb_assert (sig_type != NULL);
4480 signature = sig_type->signature;
4481 }
4482 else
4483 {
4484 attr = dwarf2_attr (comp_unit_die, DW_AT_GNU_dwo_id, cu);
4485 if (! attr)
4486 error (_("Dwarf Error: missing dwo_id [in module %s]"),
4487 dwo_name);
4488 signature = DW_UNSND (attr);
4489 }
4490
4491 /* We may need the comp_dir in order to find the DWO file. */
42e7ad6c
DE
4492 comp_dir_string = NULL;
4493 if (comp_dir)
4494 comp_dir_string = DW_STRING (comp_dir);
3019eac3
DE
4495
4496 if (this_cu->is_debug_types)
42e7ad6c 4497 dwo_unit = lookup_dwo_type_unit (sig_type, dwo_name, comp_dir_string);
3019eac3 4498 else
42e7ad6c 4499 dwo_unit = lookup_dwo_comp_unit (this_cu, dwo_name, comp_dir_string,
3019eac3
DE
4500 signature);
4501
4502 if (dwo_unit == NULL)
4503 {
4504 error (_("Dwarf Error: CU at offset 0x%x references unknown DWO"
4505 " with ID %s [in module %s]"),
4506 this_cu->offset.sect_off,
4507 phex (signature, sizeof (signature)),
4508 objfile->name);
4509 }
4510
4511 /* Set up for reading the DWO CU/TU. */
4512 cu->dwo_unit = dwo_unit;
4513 section = dwo_unit->info_or_types_section;
4514 begin_info_ptr = info_ptr = section->buffer + dwo_unit->offset.sect_off;
4bdcc0c1 4515 dwo_abbrev_section = &dwo_unit->dwo_file->sections.abbrev;
3019eac3
DE
4516 init_cu_die_reader (&reader, cu, section, dwo_unit->dwo_file);
4517
4518 if (this_cu->is_debug_types)
4519 {
4520 ULONGEST signature;
4521
4bdcc0c1
DE
4522 info_ptr = read_and_check_type_unit_head (&cu->header, section,
4523 dwo_abbrev_section,
4524 info_ptr,
3019eac3
DE
4525 &signature, NULL);
4526 gdb_assert (sig_type->signature == signature);
4527 gdb_assert (dwo_unit->offset.sect_off == cu->header.offset.sect_off);
1ce1cefd 4528 gdb_assert (dwo_unit->length == get_cu_length (&cu->header));
3019eac3
DE
4529
4530 /* Establish the type offset that can be used to lookup the type.
4531 For DWO files, we don't know it until now. */
4532 sig_type->type_offset_in_section.sect_off =
4533 dwo_unit->offset.sect_off + dwo_unit->type_offset_in_tu.cu_off;
4534 }
4535 else
4536 {
4bdcc0c1
DE
4537 info_ptr = read_and_check_comp_unit_head (&cu->header, section,
4538 dwo_abbrev_section,
4539 info_ptr, 0);
3019eac3 4540 gdb_assert (dwo_unit->offset.sect_off == cu->header.offset.sect_off);
1ce1cefd 4541 gdb_assert (dwo_unit->length == get_cu_length (&cu->header));
3019eac3
DE
4542 }
4543
4544 /* Discard the original CU's abbrev table, and read the DWO's. */
f4dc4d17
DE
4545 if (abbrev_table == NULL)
4546 {
4547 dwarf2_free_abbrev_table (cu);
4548 dwarf2_read_abbrevs (cu, dwo_abbrev_section);
4549 }
4550 else
4551 {
4552 dwarf2_read_abbrevs (cu, dwo_abbrev_section);
4553 make_cleanup (dwarf2_free_abbrev_table, cu);
4554 }
3019eac3
DE
4555
4556 /* Read in the die, but leave space to copy over the attributes
4557 from the stub. This has the benefit of simplifying the rest of
4558 the code - all the real work is done here. */
4559 num_extra_attrs = ((stmt_list != NULL)
4560 + (low_pc != NULL)
4561 + (high_pc != NULL)
42e7ad6c
DE
4562 + (ranges != NULL)
4563 + (comp_dir != NULL));
3019eac3
DE
4564 info_ptr = read_full_die_1 (&reader, &comp_unit_die, info_ptr,
4565 &has_children, num_extra_attrs);
4566
4567 /* Copy over the attributes from the stub to the DWO die. */
4568 i = comp_unit_die->num_attrs;
4569 if (stmt_list != NULL)
4570 comp_unit_die->attrs[i++] = *stmt_list;
4571 if (low_pc != NULL)
4572 comp_unit_die->attrs[i++] = *low_pc;
4573 if (high_pc != NULL)
4574 comp_unit_die->attrs[i++] = *high_pc;
4575 if (ranges != NULL)
4576 comp_unit_die->attrs[i++] = *ranges;
42e7ad6c
DE
4577 if (comp_dir != NULL)
4578 comp_unit_die->attrs[i++] = *comp_dir;
3019eac3
DE
4579 comp_unit_die->num_attrs += num_extra_attrs;
4580
4581 /* Skip dummy compilation units. */
4582 if (info_ptr >= begin_info_ptr + dwo_unit->length
4583 || peek_abbrev_code (abfd, info_ptr) == 0)
4584 {
4585 do_cleanups (cleanups);
4586 return;
4587 }
4588 }
4589
dee91e82
DE
4590 die_reader_func (&reader, info_ptr, comp_unit_die, has_children, data);
4591
4592 if (free_cu_cleanup != NULL)
348e048f 4593 {
dee91e82
DE
4594 if (keep)
4595 {
4596 /* We've successfully allocated this compilation unit. Let our
4597 caller clean it up when finished with it. */
4598 discard_cleanups (free_cu_cleanup);
4599
4600 /* We can only discard free_cu_cleanup and all subsequent cleanups.
4601 So we have to manually free the abbrev table. */
4602 dwarf2_free_abbrev_table (cu);
4603
4604 /* Link this CU into read_in_chain. */
4605 this_cu->cu->read_in_chain = dwarf2_per_objfile->read_in_chain;
4606 dwarf2_per_objfile->read_in_chain = this_cu;
4607 }
4608 else
4609 do_cleanups (free_cu_cleanup);
348e048f 4610 }
dee91e82
DE
4611
4612 do_cleanups (cleanups);
4613}
4614
3019eac3
DE
4615/* Read CU/TU THIS_CU in section SECTION,
4616 but do not follow DW_AT_GNU_dwo_name if present.
4617 DWO_FILE, if non-NULL, is the DWO file to read (the caller is assumed to
4618 have already done the lookup to find the DWO file).
dee91e82
DE
4619
4620 The caller is required to fill in THIS_CU->section, THIS_CU->offset, and
3019eac3 4621 THIS_CU->is_debug_types, but nothing else.
dee91e82
DE
4622
4623 We fill in THIS_CU->length.
4624
4625 WARNING: If THIS_CU is a "dummy CU" (used as filler by the incremental
4626 linker) then DIE_READER_FUNC will not get called.
4627
4628 THIS_CU->cu is always freed when done.
3019eac3
DE
4629 This is done in order to not leave THIS_CU->cu in a state where we have
4630 to care whether it refers to the "main" CU or the DWO CU. */
dee91e82
DE
4631
4632static void
4633init_cutu_and_read_dies_no_follow (struct dwarf2_per_cu_data *this_cu,
4634 struct dwarf2_section_info *abbrev_section,
3019eac3 4635 struct dwo_file *dwo_file,
dee91e82
DE
4636 die_reader_func_ftype *die_reader_func,
4637 void *data)
4638{
4639 struct objfile *objfile = dwarf2_per_objfile->objfile;
3019eac3
DE
4640 struct dwarf2_section_info *section = this_cu->info_or_types_section;
4641 bfd *abfd = section->asection->owner;
dee91e82
DE
4642 struct dwarf2_cu cu;
4643 gdb_byte *begin_info_ptr, *info_ptr;
4644 struct die_reader_specs reader;
4645 struct cleanup *cleanups;
4646 struct die_info *comp_unit_die;
4647 int has_children;
4648
09406207
DE
4649 if (dwarf2_die_debug)
4650 fprintf_unfiltered (gdb_stdlog, "Reading %s unit at offset 0x%x\n",
4651 this_cu->is_debug_types ? "type" : "comp",
4652 this_cu->offset.sect_off);
4653
dee91e82
DE
4654 gdb_assert (this_cu->cu == NULL);
4655
dee91e82
DE
4656 /* This is cheap if the section is already read in. */
4657 dwarf2_read_section (objfile, section);
4658
4659 init_one_comp_unit (&cu, this_cu);
4660
4661 cleanups = make_cleanup (free_stack_comp_unit, &cu);
4662
4663 begin_info_ptr = info_ptr = section->buffer + this_cu->offset.sect_off;
4bdcc0c1
DE
4664 info_ptr = read_and_check_comp_unit_head (&cu.header, section,
4665 abbrev_section, info_ptr,
3019eac3 4666 this_cu->is_debug_types);
dee91e82 4667
1ce1cefd 4668 this_cu->length = get_cu_length (&cu.header);
dee91e82
DE
4669
4670 /* Skip dummy compilation units. */
4671 if (info_ptr >= begin_info_ptr + this_cu->length
4672 || peek_abbrev_code (abfd, info_ptr) == 0)
c906108c 4673 {
dee91e82 4674 do_cleanups (cleanups);
21b2bd31 4675 return;
93311388 4676 }
72bf9492 4677
dee91e82
DE
4678 dwarf2_read_abbrevs (&cu, abbrev_section);
4679 make_cleanup (dwarf2_free_abbrev_table, &cu);
4680
3019eac3 4681 init_cu_die_reader (&reader, &cu, section, dwo_file);
dee91e82
DE
4682 info_ptr = read_full_die (&reader, &comp_unit_die, info_ptr, &has_children);
4683
4684 die_reader_func (&reader, info_ptr, comp_unit_die, has_children, data);
4685
4686 do_cleanups (cleanups);
4687}
4688
3019eac3
DE
4689/* Read a CU/TU, except that this does not look for DW_AT_GNU_dwo_name and
4690 does not lookup the specified DWO file.
4691 This cannot be used to read DWO files.
dee91e82
DE
4692
4693 THIS_CU->cu is always freed when done.
3019eac3
DE
4694 This is done in order to not leave THIS_CU->cu in a state where we have
4695 to care whether it refers to the "main" CU or the DWO CU.
4696 We can revisit this if the data shows there's a performance issue. */
dee91e82
DE
4697
4698static void
4699init_cutu_and_read_dies_simple (struct dwarf2_per_cu_data *this_cu,
4700 die_reader_func_ftype *die_reader_func,
4701 void *data)
4702{
4703 init_cutu_and_read_dies_no_follow (this_cu,
36586728 4704 get_abbrev_section_for_cu (this_cu),
3019eac3 4705 NULL,
dee91e82
DE
4706 die_reader_func, data);
4707}
4708
f4dc4d17
DE
4709/* Create a psymtab named NAME and assign it to PER_CU.
4710
4711 The caller must fill in the following details:
4712 dirname, textlow, texthigh. */
4713
4714static struct partial_symtab *
4715create_partial_symtab (struct dwarf2_per_cu_data *per_cu, const char *name)
4716{
4717 struct objfile *objfile = per_cu->objfile;
4718 struct partial_symtab *pst;
4719
4720 pst = start_psymtab_common (objfile, objfile->section_offsets,
4721 name, 0,
4722 objfile->global_psymbols.next,
4723 objfile->static_psymbols.next);
4724
4725 pst->psymtabs_addrmap_supported = 1;
4726
4727 /* This is the glue that links PST into GDB's symbol API. */
4728 pst->read_symtab_private = per_cu;
4729 pst->read_symtab = dwarf2_psymtab_to_symtab;
4730 per_cu->v.psymtab = pst;
4731
4732 return pst;
4733}
4734
dee91e82
DE
4735/* die_reader_func for process_psymtab_comp_unit. */
4736
4737static void
4738process_psymtab_comp_unit_reader (const struct die_reader_specs *reader,
4739 gdb_byte *info_ptr,
4740 struct die_info *comp_unit_die,
4741 int has_children,
4742 void *data)
4743{
4744 struct dwarf2_cu *cu = reader->cu;
4745 struct objfile *objfile = cu->objfile;
4746 struct dwarf2_per_cu_data *per_cu = cu->per_cu;
dee91e82
DE
4747 struct attribute *attr;
4748 CORE_ADDR baseaddr;
4749 CORE_ADDR best_lowpc = 0, best_highpc = 0;
4750 struct partial_symtab *pst;
4751 int has_pc_info;
4752 const char *filename;
95554aad 4753 int *want_partial_unit_ptr = data;
dee91e82 4754
95554aad
TT
4755 if (comp_unit_die->tag == DW_TAG_partial_unit
4756 && (want_partial_unit_ptr == NULL
4757 || !*want_partial_unit_ptr))
dee91e82
DE
4758 return;
4759
f4dc4d17
DE
4760 gdb_assert (! per_cu->is_debug_types);
4761
95554aad 4762 prepare_one_comp_unit (cu, comp_unit_die, language_minimal);
dee91e82
DE
4763
4764 cu->list_in_scope = &file_symbols;
c906108c 4765
93311388 4766 /* Allocate a new partial symbol table structure. */
dee91e82 4767 attr = dwarf2_attr (comp_unit_die, DW_AT_name, cu);
3e2a0cee
TT
4768 if (attr == NULL || !DW_STRING (attr))
4769 filename = "";
4770 else
4771 filename = DW_STRING (attr);
72bf9492 4772
f4dc4d17
DE
4773 pst = create_partial_symtab (per_cu, filename);
4774
4775 /* This must be done before calling dwarf2_build_include_psymtabs. */
dee91e82 4776 attr = dwarf2_attr (comp_unit_die, DW_AT_comp_dir, cu);
d85a05f0
DJ
4777 if (attr != NULL)
4778 pst->dirname = DW_STRING (attr);
72bf9492 4779
93311388 4780 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
e7c27a73 4781
dee91e82 4782 dwarf2_find_base_address (comp_unit_die, cu);
d85a05f0 4783
93311388
DE
4784 /* Possibly set the default values of LOWPC and HIGHPC from
4785 `DW_AT_ranges'. */
d85a05f0 4786 has_pc_info = dwarf2_get_pc_bounds (comp_unit_die, &best_lowpc,
dee91e82 4787 &best_highpc, cu, pst);
d85a05f0 4788 if (has_pc_info == 1 && best_lowpc < best_highpc)
93311388
DE
4789 /* Store the contiguous range if it is not empty; it can be empty for
4790 CUs with no code. */
4791 addrmap_set_empty (objfile->psymtabs_addrmap,
d85a05f0
DJ
4792 best_lowpc + baseaddr,
4793 best_highpc + baseaddr - 1, pst);
93311388
DE
4794
4795 /* Check if comp unit has_children.
4796 If so, read the rest of the partial symbols from this comp unit.
0963b4bd 4797 If not, there's no more debug_info for this comp unit. */
d85a05f0 4798 if (has_children)
93311388
DE
4799 {
4800 struct partial_die_info *first_die;
4801 CORE_ADDR lowpc, highpc;
31ffec48 4802
93311388
DE
4803 lowpc = ((CORE_ADDR) -1);
4804 highpc = ((CORE_ADDR) 0);
c906108c 4805
dee91e82 4806 first_die = load_partial_dies (reader, info_ptr, 1);
c906108c 4807
93311388 4808 scan_partial_symbols (first_die, &lowpc, &highpc,
dee91e82 4809 ! has_pc_info, cu);
57c22c6c 4810
93311388
DE
4811 /* If we didn't find a lowpc, set it to highpc to avoid
4812 complaints from `maint check'. */
4813 if (lowpc == ((CORE_ADDR) -1))
4814 lowpc = highpc;
10b3939b 4815
93311388
DE
4816 /* If the compilation unit didn't have an explicit address range,
4817 then use the information extracted from its child dies. */
d85a05f0 4818 if (! has_pc_info)
93311388 4819 {
d85a05f0
DJ
4820 best_lowpc = lowpc;
4821 best_highpc = highpc;
93311388
DE
4822 }
4823 }
d85a05f0
DJ
4824 pst->textlow = best_lowpc + baseaddr;
4825 pst->texthigh = best_highpc + baseaddr;
c906108c 4826
93311388
DE
4827 pst->n_global_syms = objfile->global_psymbols.next -
4828 (objfile->global_psymbols.list + pst->globals_offset);
4829 pst->n_static_syms = objfile->static_psymbols.next -
4830 (objfile->static_psymbols.list + pst->statics_offset);
4831 sort_pst_symbols (pst);
c906108c 4832
f4dc4d17 4833 if (!VEC_empty (dwarf2_per_cu_ptr, cu->per_cu->s.imported_symtabs))
95554aad
TT
4834 {
4835 int i;
f4dc4d17 4836 int len = VEC_length (dwarf2_per_cu_ptr, cu->per_cu->s.imported_symtabs);
95554aad
TT
4837 struct dwarf2_per_cu_data *iter;
4838
4839 /* Fill in 'dependencies' here; we fill in 'users' in a
4840 post-pass. */
4841 pst->number_of_dependencies = len;
4842 pst->dependencies = obstack_alloc (&objfile->objfile_obstack,
4843 len * sizeof (struct symtab *));
4844 for (i = 0;
f4dc4d17 4845 VEC_iterate (dwarf2_per_cu_ptr, cu->per_cu->s.imported_symtabs,
95554aad
TT
4846 i, iter);
4847 ++i)
4848 pst->dependencies[i] = iter->v.psymtab;
4849
f4dc4d17 4850 VEC_free (dwarf2_per_cu_ptr, cu->per_cu->s.imported_symtabs);
95554aad
TT
4851 }
4852
f4dc4d17
DE
4853 /* Get the list of files included in the current compilation unit,
4854 and build a psymtab for each of them. */
4855 dwarf2_build_include_psymtabs (cu, comp_unit_die, pst);
09406207
DE
4856
4857 if (dwarf2_read_debug)
4858 {
4859 struct gdbarch *gdbarch = get_objfile_arch (objfile);
4860
4861 fprintf_unfiltered (gdb_stdlog,
844226d6 4862 "Psymtab for %s unit @0x%x: %s - %s"
09406207
DE
4863 ", %d global, %d static syms\n",
4864 per_cu->is_debug_types ? "type" : "comp",
4865 per_cu->offset.sect_off,
4866 paddress (gdbarch, pst->textlow),
4867 paddress (gdbarch, pst->texthigh),
4868 pst->n_global_syms, pst->n_static_syms);
4869 }
dee91e82 4870}
ae038cb0 4871
dee91e82
DE
4872/* Subroutine of dwarf2_build_psymtabs_hard to simplify it.
4873 Process compilation unit THIS_CU for a psymtab. */
4874
4875static void
95554aad
TT
4876process_psymtab_comp_unit (struct dwarf2_per_cu_data *this_cu,
4877 int want_partial_unit)
dee91e82
DE
4878{
4879 /* If this compilation unit was already read in, free the
4880 cached copy in order to read it in again. This is
4881 necessary because we skipped some symbols when we first
4882 read in the compilation unit (see load_partial_dies).
4883 This problem could be avoided, but the benefit is unclear. */
4884 if (this_cu->cu != NULL)
4885 free_one_cached_comp_unit (this_cu);
4886
3019eac3 4887 gdb_assert (! this_cu->is_debug_types);
f4dc4d17
DE
4888 init_cutu_and_read_dies (this_cu, NULL, 0, 0,
4889 process_psymtab_comp_unit_reader,
95554aad 4890 &want_partial_unit);
dee91e82
DE
4891
4892 /* Age out any secondary CUs. */
4893 age_cached_comp_units ();
93311388 4894}
ff013f42 4895
f4dc4d17
DE
4896static hashval_t
4897hash_type_unit_group (const void *item)
4898{
094b34ac 4899 const struct type_unit_group *tu_group = item;
f4dc4d17 4900
094b34ac 4901 return hash_stmt_list_entry (&tu_group->hash);
f4dc4d17 4902}
348e048f
DE
4903
4904static int
f4dc4d17 4905eq_type_unit_group (const void *item_lhs, const void *item_rhs)
348e048f 4906{
f4dc4d17
DE
4907 const struct type_unit_group *lhs = item_lhs;
4908 const struct type_unit_group *rhs = item_rhs;
348e048f 4909
094b34ac 4910 return eq_stmt_list_entry (&lhs->hash, &rhs->hash);
f4dc4d17 4911}
348e048f 4912
f4dc4d17
DE
4913/* Allocate a hash table for type unit groups. */
4914
4915static htab_t
4916allocate_type_unit_groups_table (void)
4917{
4918 return htab_create_alloc_ex (3,
4919 hash_type_unit_group,
4920 eq_type_unit_group,
4921 NULL,
4922 &dwarf2_per_objfile->objfile->objfile_obstack,
4923 hashtab_obstack_allocate,
4924 dummy_obstack_deallocate);
4925}
dee91e82 4926
f4dc4d17
DE
4927/* Type units that don't have DW_AT_stmt_list are grouped into their own
4928 partial symtabs. We combine several TUs per psymtab to not let the size
4929 of any one psymtab grow too big. */
4930#define NO_STMT_LIST_TYPE_UNIT_PSYMTAB (1 << 31)
4931#define NO_STMT_LIST_TYPE_UNIT_PSYMTAB_SIZE 10
dee91e82 4932
094b34ac 4933/* Helper routine for get_type_unit_group.
f4dc4d17
DE
4934 Create the type_unit_group object used to hold one or more TUs. */
4935
4936static struct type_unit_group *
094b34ac 4937create_type_unit_group (struct dwarf2_cu *cu, sect_offset line_offset_struct)
f4dc4d17
DE
4938{
4939 struct objfile *objfile = dwarf2_per_objfile->objfile;
094b34ac 4940 struct dwarf2_per_cu_data *per_cu;
f4dc4d17 4941 struct type_unit_group *tu_group;
f4dc4d17
DE
4942
4943 tu_group = OBSTACK_ZALLOC (&objfile->objfile_obstack,
4944 struct type_unit_group);
094b34ac 4945 per_cu = &tu_group->per_cu;
f4dc4d17
DE
4946 per_cu->objfile = objfile;
4947 per_cu->is_debug_types = 1;
4948 per_cu->s.type_unit_group = tu_group;
4949
094b34ac
DE
4950 if (dwarf2_per_objfile->using_index)
4951 {
4952 per_cu->v.quick = OBSTACK_ZALLOC (&objfile->objfile_obstack,
4953 struct dwarf2_per_cu_quick_data);
4954 tu_group->t.first_tu = cu->per_cu;
4955 }
4956 else
4957 {
4958 unsigned int line_offset = line_offset_struct.sect_off;
4959 struct partial_symtab *pst;
4960 char *name;
4961
4962 /* Give the symtab a useful name for debug purposes. */
4963 if ((line_offset & NO_STMT_LIST_TYPE_UNIT_PSYMTAB) != 0)
4964 name = xstrprintf ("<type_units_%d>",
4965 (line_offset & ~NO_STMT_LIST_TYPE_UNIT_PSYMTAB));
4966 else
4967 name = xstrprintf ("<type_units_at_0x%x>", line_offset);
4968
4969 pst = create_partial_symtab (per_cu, name);
4970 pst->anonymous = 1;
f4dc4d17 4971
094b34ac
DE
4972 xfree (name);
4973 }
f4dc4d17 4974
094b34ac
DE
4975 tu_group->hash.dwo_unit = cu->dwo_unit;
4976 tu_group->hash.line_offset = line_offset_struct;
f4dc4d17
DE
4977
4978 return tu_group;
4979}
4980
094b34ac
DE
4981/* Look up the type_unit_group for type unit CU, and create it if necessary.
4982 STMT_LIST is a DW_AT_stmt_list attribute. */
f4dc4d17
DE
4983
4984static struct type_unit_group *
094b34ac 4985get_type_unit_group (struct dwarf2_cu *cu, struct attribute *stmt_list)
f4dc4d17
DE
4986{
4987 struct tu_stats *tu_stats = &dwarf2_per_objfile->tu_stats;
4988 struct type_unit_group *tu_group;
4989 void **slot;
4990 unsigned int line_offset;
4991 struct type_unit_group type_unit_group_for_lookup;
4992
4993 if (dwarf2_per_objfile->type_unit_groups == NULL)
4994 {
4995 dwarf2_per_objfile->type_unit_groups =
4996 allocate_type_unit_groups_table ();
4997 }
4998
4999 /* Do we need to create a new group, or can we use an existing one? */
5000
5001 if (stmt_list)
5002 {
5003 line_offset = DW_UNSND (stmt_list);
5004 ++tu_stats->nr_symtab_sharers;
5005 }
5006 else
5007 {
5008 /* Ugh, no stmt_list. Rare, but we have to handle it.
5009 We can do various things here like create one group per TU or
5010 spread them over multiple groups to split up the expansion work.
5011 To avoid worst case scenarios (too many groups or too large groups)
5012 we, umm, group them in bunches. */
5013 line_offset = (NO_STMT_LIST_TYPE_UNIT_PSYMTAB
5014 | (tu_stats->nr_stmt_less_type_units
5015 / NO_STMT_LIST_TYPE_UNIT_PSYMTAB_SIZE));
5016 ++tu_stats->nr_stmt_less_type_units;
5017 }
5018
094b34ac
DE
5019 type_unit_group_for_lookup.hash.dwo_unit = cu->dwo_unit;
5020 type_unit_group_for_lookup.hash.line_offset.sect_off = line_offset;
f4dc4d17
DE
5021 slot = htab_find_slot (dwarf2_per_objfile->type_unit_groups,
5022 &type_unit_group_for_lookup, INSERT);
5023 if (*slot != NULL)
5024 {
5025 tu_group = *slot;
5026 gdb_assert (tu_group != NULL);
5027 }
5028 else
5029 {
5030 sect_offset line_offset_struct;
5031
5032 line_offset_struct.sect_off = line_offset;
094b34ac 5033 tu_group = create_type_unit_group (cu, line_offset_struct);
f4dc4d17
DE
5034 *slot = tu_group;
5035 ++tu_stats->nr_symtabs;
5036 }
5037
5038 return tu_group;
5039}
5040
5041/* Struct used to sort TUs by their abbreviation table offset. */
5042
5043struct tu_abbrev_offset
5044{
5045 struct signatured_type *sig_type;
5046 sect_offset abbrev_offset;
5047};
5048
5049/* Helper routine for build_type_unit_groups, passed to qsort. */
5050
5051static int
5052sort_tu_by_abbrev_offset (const void *ap, const void *bp)
5053{
5054 const struct tu_abbrev_offset * const *a = ap;
5055 const struct tu_abbrev_offset * const *b = bp;
5056 unsigned int aoff = (*a)->abbrev_offset.sect_off;
5057 unsigned int boff = (*b)->abbrev_offset.sect_off;
5058
5059 return (aoff > boff) - (aoff < boff);
5060}
5061
5062/* A helper function to add a type_unit_group to a table. */
5063
5064static int
5065add_type_unit_group_to_table (void **slot, void *datum)
5066{
5067 struct type_unit_group *tu_group = *slot;
5068 struct type_unit_group ***datap = datum;
5069
5070 **datap = tu_group;
5071 ++*datap;
5072
5073 return 1;
5074}
5075
5076/* Efficiently read all the type units, calling init_cutu_and_read_dies on
5077 each one passing FUNC,DATA.
5078
5079 The efficiency is because we sort TUs by the abbrev table they use and
5080 only read each abbrev table once. In one program there are 200K TUs
5081 sharing 8K abbrev tables.
5082
5083 The main purpose of this function is to support building the
5084 dwarf2_per_objfile->type_unit_groups table.
5085 TUs typically share the DW_AT_stmt_list of the CU they came from, so we
5086 can collapse the search space by grouping them by stmt_list.
5087 The savings can be significant, in the same program from above the 200K TUs
5088 share 8K stmt_list tables.
5089
5090 FUNC is expected to call get_type_unit_group, which will create the
5091 struct type_unit_group if necessary and add it to
5092 dwarf2_per_objfile->type_unit_groups. */
5093
5094static void
5095build_type_unit_groups (die_reader_func_ftype *func, void *data)
5096{
5097 struct objfile *objfile = dwarf2_per_objfile->objfile;
5098 struct tu_stats *tu_stats = &dwarf2_per_objfile->tu_stats;
5099 struct cleanup *cleanups;
5100 struct abbrev_table *abbrev_table;
5101 sect_offset abbrev_offset;
5102 struct tu_abbrev_offset *sorted_by_abbrev;
5103 struct type_unit_group **iter;
5104 int i;
5105
5106 /* It's up to the caller to not call us multiple times. */
5107 gdb_assert (dwarf2_per_objfile->type_unit_groups == NULL);
5108
5109 if (dwarf2_per_objfile->n_type_units == 0)
5110 return;
5111
5112 /* TUs typically share abbrev tables, and there can be way more TUs than
5113 abbrev tables. Sort by abbrev table to reduce the number of times we
5114 read each abbrev table in.
5115 Alternatives are to punt or to maintain a cache of abbrev tables.
5116 This is simpler and efficient enough for now.
5117
5118 Later we group TUs by their DW_AT_stmt_list value (as this defines the
5119 symtab to use). Typically TUs with the same abbrev offset have the same
5120 stmt_list value too so in practice this should work well.
5121
5122 The basic algorithm here is:
5123
5124 sort TUs by abbrev table
5125 for each TU with same abbrev table:
5126 read abbrev table if first user
5127 read TU top level DIE
5128 [IWBN if DWO skeletons had DW_AT_stmt_list]
5129 call FUNC */
5130
5131 if (dwarf2_read_debug)
5132 fprintf_unfiltered (gdb_stdlog, "Building type unit groups ...\n");
5133
5134 /* Sort in a separate table to maintain the order of all_type_units
5135 for .gdb_index: TU indices directly index all_type_units. */
5136 sorted_by_abbrev = XNEWVEC (struct tu_abbrev_offset,
5137 dwarf2_per_objfile->n_type_units);
5138 for (i = 0; i < dwarf2_per_objfile->n_type_units; ++i)
5139 {
5140 struct signatured_type *sig_type = dwarf2_per_objfile->all_type_units[i];
5141
5142 sorted_by_abbrev[i].sig_type = sig_type;
5143 sorted_by_abbrev[i].abbrev_offset =
5144 read_abbrev_offset (sig_type->per_cu.info_or_types_section,
5145 sig_type->per_cu.offset);
5146 }
5147 cleanups = make_cleanup (xfree, sorted_by_abbrev);
5148 qsort (sorted_by_abbrev, dwarf2_per_objfile->n_type_units,
5149 sizeof (struct tu_abbrev_offset), sort_tu_by_abbrev_offset);
5150
094b34ac
DE
5151 /* Note: In the .gdb_index case, get_type_unit_group may have already been
5152 called any number of times, so we don't reset tu_stats here. */
5153
f4dc4d17
DE
5154 abbrev_offset.sect_off = ~(unsigned) 0;
5155 abbrev_table = NULL;
5156 make_cleanup (abbrev_table_free_cleanup, &abbrev_table);
5157
5158 for (i = 0; i < dwarf2_per_objfile->n_type_units; ++i)
5159 {
5160 const struct tu_abbrev_offset *tu = &sorted_by_abbrev[i];
5161
5162 /* Switch to the next abbrev table if necessary. */
5163 if (abbrev_table == NULL
5164 || tu->abbrev_offset.sect_off != abbrev_offset.sect_off)
5165 {
5166 if (abbrev_table != NULL)
5167 {
5168 abbrev_table_free (abbrev_table);
5169 /* Reset to NULL in case abbrev_table_read_table throws
5170 an error: abbrev_table_free_cleanup will get called. */
5171 abbrev_table = NULL;
5172 }
5173 abbrev_offset = tu->abbrev_offset;
5174 abbrev_table =
5175 abbrev_table_read_table (&dwarf2_per_objfile->abbrev,
5176 abbrev_offset);
5177 ++tu_stats->nr_uniq_abbrev_tables;
5178 }
5179
5180 init_cutu_and_read_dies (&tu->sig_type->per_cu, abbrev_table, 0, 0,
5181 func, data);
5182 }
5183
5184 /* Create a vector of pointers to primary type units to make it easy to
5185 iterate over them and CUs. See dw2_get_primary_cu. */
5186 dwarf2_per_objfile->n_type_unit_groups =
5187 htab_elements (dwarf2_per_objfile->type_unit_groups);
5188 dwarf2_per_objfile->all_type_unit_groups =
5189 obstack_alloc (&objfile->objfile_obstack,
5190 dwarf2_per_objfile->n_type_unit_groups
5191 * sizeof (struct type_unit_group *));
5192 iter = &dwarf2_per_objfile->all_type_unit_groups[0];
5193 htab_traverse_noresize (dwarf2_per_objfile->type_unit_groups,
5194 add_type_unit_group_to_table, &iter);
5195 gdb_assert (iter - &dwarf2_per_objfile->all_type_unit_groups[0]
5196 == dwarf2_per_objfile->n_type_unit_groups);
5197
5198 do_cleanups (cleanups);
5199
5200 if (dwarf2_read_debug)
5201 {
5202 fprintf_unfiltered (gdb_stdlog, "Done building type unit groups:\n");
5203 fprintf_unfiltered (gdb_stdlog, " %d TUs\n",
5204 dwarf2_per_objfile->n_type_units);
5205 fprintf_unfiltered (gdb_stdlog, " %d uniq abbrev tables\n",
5206 tu_stats->nr_uniq_abbrev_tables);
5207 fprintf_unfiltered (gdb_stdlog, " %d symtabs from stmt_list entries\n",
5208 tu_stats->nr_symtabs);
5209 fprintf_unfiltered (gdb_stdlog, " %d symtab sharers\n",
5210 tu_stats->nr_symtab_sharers);
5211 fprintf_unfiltered (gdb_stdlog, " %d type units without a stmt_list\n",
5212 tu_stats->nr_stmt_less_type_units);
5213 }
5214}
5215
5216/* Reader function for build_type_psymtabs. */
5217
5218static void
5219build_type_psymtabs_reader (const struct die_reader_specs *reader,
5220 gdb_byte *info_ptr,
5221 struct die_info *type_unit_die,
5222 int has_children,
5223 void *data)
5224{
5225 struct objfile *objfile = dwarf2_per_objfile->objfile;
5226 struct dwarf2_cu *cu = reader->cu;
5227 struct dwarf2_per_cu_data *per_cu = cu->per_cu;
5228 struct type_unit_group *tu_group;
5229 struct attribute *attr;
5230 struct partial_die_info *first_die;
5231 CORE_ADDR lowpc, highpc;
5232 struct partial_symtab *pst;
5233
5234 gdb_assert (data == NULL);
5235
5236 if (! has_children)
5237 return;
5238
5239 attr = dwarf2_attr_no_follow (type_unit_die, DW_AT_stmt_list);
094b34ac 5240 tu_group = get_type_unit_group (cu, attr);
f4dc4d17 5241
094b34ac 5242 VEC_safe_push (dwarf2_per_cu_ptr, tu_group->t.tus, per_cu);
f4dc4d17
DE
5243
5244 prepare_one_comp_unit (cu, type_unit_die, language_minimal);
5245 cu->list_in_scope = &file_symbols;
5246 pst = create_partial_symtab (per_cu, "");
5247 pst->anonymous = 1;
5248
5249 first_die = load_partial_dies (reader, info_ptr, 1);
5250
5251 lowpc = (CORE_ADDR) -1;
5252 highpc = (CORE_ADDR) 0;
5253 scan_partial_symbols (first_die, &lowpc, &highpc, 0, cu);
5254
5255 pst->n_global_syms = objfile->global_psymbols.next -
5256 (objfile->global_psymbols.list + pst->globals_offset);
5257 pst->n_static_syms = objfile->static_psymbols.next -
5258 (objfile->static_psymbols.list + pst->statics_offset);
5259 sort_pst_symbols (pst);
5260}
5261
5262/* Traversal function for build_type_psymtabs. */
5263
5264static int
5265build_type_psymtab_dependencies (void **slot, void *info)
5266{
5267 struct objfile *objfile = dwarf2_per_objfile->objfile;
5268 struct type_unit_group *tu_group = (struct type_unit_group *) *slot;
094b34ac 5269 struct dwarf2_per_cu_data *per_cu = &tu_group->per_cu;
f4dc4d17 5270 struct partial_symtab *pst = per_cu->v.psymtab;
094b34ac 5271 int len = VEC_length (dwarf2_per_cu_ptr, tu_group->t.tus);
f4dc4d17
DE
5272 struct dwarf2_per_cu_data *iter;
5273 int i;
5274
5275 gdb_assert (len > 0);
5276
5277 pst->number_of_dependencies = len;
5278 pst->dependencies = obstack_alloc (&objfile->objfile_obstack,
5279 len * sizeof (struct psymtab *));
5280 for (i = 0;
094b34ac 5281 VEC_iterate (dwarf2_per_cu_ptr, tu_group->t.tus, i, iter);
f4dc4d17
DE
5282 ++i)
5283 {
5284 pst->dependencies[i] = iter->v.psymtab;
5285 iter->s.type_unit_group = tu_group;
5286 }
5287
094b34ac 5288 VEC_free (dwarf2_per_cu_ptr, tu_group->t.tus);
348e048f
DE
5289
5290 return 1;
5291}
5292
5293/* Subroutine of dwarf2_build_psymtabs_hard to simplify it.
5294 Build partial symbol tables for the .debug_types comp-units. */
5295
5296static void
5297build_type_psymtabs (struct objfile *objfile)
5298{
0e50663e 5299 if (! create_all_type_units (objfile))
348e048f
DE
5300 return;
5301
f4dc4d17
DE
5302 build_type_unit_groups (build_type_psymtabs_reader, NULL);
5303
5304 /* Now that all TUs have been processed we can fill in the dependencies. */
5305 htab_traverse_noresize (dwarf2_per_objfile->type_unit_groups,
5306 build_type_psymtab_dependencies, NULL);
348e048f
DE
5307}
5308
60606b2c
TT
5309/* A cleanup function that clears objfile's psymtabs_addrmap field. */
5310
5311static void
5312psymtabs_addrmap_cleanup (void *o)
5313{
5314 struct objfile *objfile = o;
ec61707d 5315
60606b2c
TT
5316 objfile->psymtabs_addrmap = NULL;
5317}
5318
95554aad
TT
5319/* Compute the 'user' field for each psymtab in OBJFILE. */
5320
5321static void
5322set_partial_user (struct objfile *objfile)
5323{
5324 int i;
5325
5326 for (i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
5327 {
5328 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
5329 struct partial_symtab *pst = per_cu->v.psymtab;
5330 int j;
5331
36586728
TT
5332 if (pst == NULL)
5333 continue;
5334
95554aad
TT
5335 for (j = 0; j < pst->number_of_dependencies; ++j)
5336 {
5337 /* Set the 'user' field only if it is not already set. */
5338 if (pst->dependencies[j]->user == NULL)
5339 pst->dependencies[j]->user = pst;
5340 }
5341 }
5342}
5343
93311388
DE
5344/* Build the partial symbol table by doing a quick pass through the
5345 .debug_info and .debug_abbrev sections. */
72bf9492 5346
93311388 5347static void
c67a9c90 5348dwarf2_build_psymtabs_hard (struct objfile *objfile)
93311388 5349{
60606b2c
TT
5350 struct cleanup *back_to, *addrmap_cleanup;
5351 struct obstack temp_obstack;
21b2bd31 5352 int i;
93311388 5353
45cfd468
DE
5354 if (dwarf2_read_debug)
5355 {
5356 fprintf_unfiltered (gdb_stdlog, "Building psymtabs of objfile %s ...\n",
5357 objfile->name);
5358 }
5359
98bfdba5
PA
5360 dwarf2_per_objfile->reading_partial_symbols = 1;
5361
be391dca 5362 dwarf2_read_section (objfile, &dwarf2_per_objfile->info);
91c24f0a 5363
93311388
DE
5364 /* Any cached compilation units will be linked by the per-objfile
5365 read_in_chain. Make sure to free them when we're done. */
5366 back_to = make_cleanup (free_cached_comp_units, NULL);
72bf9492 5367
348e048f
DE
5368 build_type_psymtabs (objfile);
5369
93311388 5370 create_all_comp_units (objfile);
c906108c 5371
60606b2c
TT
5372 /* Create a temporary address map on a temporary obstack. We later
5373 copy this to the final obstack. */
5374 obstack_init (&temp_obstack);
5375 make_cleanup_obstack_free (&temp_obstack);
5376 objfile->psymtabs_addrmap = addrmap_create_mutable (&temp_obstack);
5377 addrmap_cleanup = make_cleanup (psymtabs_addrmap_cleanup, objfile);
72bf9492 5378
21b2bd31 5379 for (i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
93311388 5380 {
21b2bd31 5381 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
aaa75496 5382
95554aad 5383 process_psymtab_comp_unit (per_cu, 0);
c906108c 5384 }
ff013f42 5385
95554aad
TT
5386 set_partial_user (objfile);
5387
ff013f42
JK
5388 objfile->psymtabs_addrmap = addrmap_create_fixed (objfile->psymtabs_addrmap,
5389 &objfile->objfile_obstack);
60606b2c 5390 discard_cleanups (addrmap_cleanup);
ff013f42 5391
ae038cb0 5392 do_cleanups (back_to);
45cfd468
DE
5393
5394 if (dwarf2_read_debug)
5395 fprintf_unfiltered (gdb_stdlog, "Done building psymtabs of %s\n",
5396 objfile->name);
ae038cb0
DJ
5397}
5398
3019eac3 5399/* die_reader_func for load_partial_comp_unit. */
ae038cb0
DJ
5400
5401static void
dee91e82
DE
5402load_partial_comp_unit_reader (const struct die_reader_specs *reader,
5403 gdb_byte *info_ptr,
5404 struct die_info *comp_unit_die,
5405 int has_children,
5406 void *data)
ae038cb0 5407{
dee91e82 5408 struct dwarf2_cu *cu = reader->cu;
ae038cb0 5409
95554aad 5410 prepare_one_comp_unit (cu, comp_unit_die, language_minimal);
ae038cb0 5411
ae038cb0
DJ
5412 /* Check if comp unit has_children.
5413 If so, read the rest of the partial symbols from this comp unit.
0963b4bd 5414 If not, there's no more debug_info for this comp unit. */
d85a05f0 5415 if (has_children)
dee91e82
DE
5416 load_partial_dies (reader, info_ptr, 0);
5417}
98bfdba5 5418
dee91e82
DE
5419/* Load the partial DIEs for a secondary CU into memory.
5420 This is also used when rereading a primary CU with load_all_dies. */
c5b7e1cb 5421
dee91e82
DE
5422static void
5423load_partial_comp_unit (struct dwarf2_per_cu_data *this_cu)
5424{
f4dc4d17
DE
5425 init_cutu_and_read_dies (this_cu, NULL, 1, 1,
5426 load_partial_comp_unit_reader, NULL);
ae038cb0
DJ
5427}
5428
ae038cb0 5429static void
36586728
TT
5430read_comp_units_from_section (struct objfile *objfile,
5431 struct dwarf2_section_info *section,
5432 unsigned int is_dwz,
5433 int *n_allocated,
5434 int *n_comp_units,
5435 struct dwarf2_per_cu_data ***all_comp_units)
ae038cb0 5436{
be391dca 5437 gdb_byte *info_ptr;
36586728 5438 bfd *abfd = section->asection->owner;
be391dca 5439
36586728 5440 dwarf2_read_section (objfile, section);
ae038cb0 5441
36586728 5442 info_ptr = section->buffer;
6e70227d 5443
36586728 5444 while (info_ptr < section->buffer + section->size)
ae038cb0 5445 {
c764a876 5446 unsigned int length, initial_length_size;
ae038cb0 5447 struct dwarf2_per_cu_data *this_cu;
b64f50a1 5448 sect_offset offset;
ae038cb0 5449
36586728 5450 offset.sect_off = info_ptr - section->buffer;
ae038cb0
DJ
5451
5452 /* Read just enough information to find out where the next
5453 compilation unit is. */
36586728 5454 length = read_initial_length (abfd, info_ptr, &initial_length_size);
ae038cb0
DJ
5455
5456 /* Save the compilation unit for later lookup. */
5457 this_cu = obstack_alloc (&objfile->objfile_obstack,
5458 sizeof (struct dwarf2_per_cu_data));
5459 memset (this_cu, 0, sizeof (*this_cu));
5460 this_cu->offset = offset;
c764a876 5461 this_cu->length = length + initial_length_size;
36586728 5462 this_cu->is_dwz = is_dwz;
9291a0cd 5463 this_cu->objfile = objfile;
36586728 5464 this_cu->info_or_types_section = section;
ae038cb0 5465
36586728 5466 if (*n_comp_units == *n_allocated)
ae038cb0 5467 {
36586728
TT
5468 *n_allocated *= 2;
5469 *all_comp_units = xrealloc (*all_comp_units,
5470 *n_allocated
5471 * sizeof (struct dwarf2_per_cu_data *));
ae038cb0 5472 }
36586728
TT
5473 (*all_comp_units)[*n_comp_units] = this_cu;
5474 ++*n_comp_units;
ae038cb0
DJ
5475
5476 info_ptr = info_ptr + this_cu->length;
5477 }
36586728
TT
5478}
5479
5480/* Create a list of all compilation units in OBJFILE.
5481 This is only done for -readnow and building partial symtabs. */
5482
5483static void
5484create_all_comp_units (struct objfile *objfile)
5485{
5486 int n_allocated;
5487 int n_comp_units;
5488 struct dwarf2_per_cu_data **all_comp_units;
5489
5490 n_comp_units = 0;
5491 n_allocated = 10;
5492 all_comp_units = xmalloc (n_allocated
5493 * sizeof (struct dwarf2_per_cu_data *));
5494
5495 read_comp_units_from_section (objfile, &dwarf2_per_objfile->info, 0,
5496 &n_allocated, &n_comp_units, &all_comp_units);
5497
5498 if (bfd_get_section_by_name (objfile->obfd, ".gnu_debugaltlink") != NULL)
5499 {
5500 struct dwz_file *dwz = dwarf2_get_dwz_file ();
5501
5502 read_comp_units_from_section (objfile, &dwz->info, 1,
5503 &n_allocated, &n_comp_units,
5504 &all_comp_units);
5505 }
ae038cb0
DJ
5506
5507 dwarf2_per_objfile->all_comp_units
5508 = obstack_alloc (&objfile->objfile_obstack,
5509 n_comp_units * sizeof (struct dwarf2_per_cu_data *));
5510 memcpy (dwarf2_per_objfile->all_comp_units, all_comp_units,
5511 n_comp_units * sizeof (struct dwarf2_per_cu_data *));
5512 xfree (all_comp_units);
5513 dwarf2_per_objfile->n_comp_units = n_comp_units;
c906108c
SS
5514}
5515
5734ee8b
DJ
5516/* Process all loaded DIEs for compilation unit CU, starting at
5517 FIRST_DIE. The caller should pass NEED_PC == 1 if the compilation
5518 unit DIE did not have PC info (DW_AT_low_pc and DW_AT_high_pc, or
5519 DW_AT_ranges). If NEED_PC is set, then this function will set
5520 *LOWPC and *HIGHPC to the lowest and highest PC values found in CU
5521 and record the covered ranges in the addrmap. */
c906108c 5522
72bf9492
DJ
5523static void
5524scan_partial_symbols (struct partial_die_info *first_die, CORE_ADDR *lowpc,
5734ee8b 5525 CORE_ADDR *highpc, int need_pc, struct dwarf2_cu *cu)
c906108c 5526{
72bf9492 5527 struct partial_die_info *pdi;
c906108c 5528
91c24f0a
DC
5529 /* Now, march along the PDI's, descending into ones which have
5530 interesting children but skipping the children of the other ones,
5531 until we reach the end of the compilation unit. */
c906108c 5532
72bf9492 5533 pdi = first_die;
91c24f0a 5534
72bf9492
DJ
5535 while (pdi != NULL)
5536 {
5537 fixup_partial_die (pdi, cu);
c906108c 5538
f55ee35c 5539 /* Anonymous namespaces or modules have no name but have interesting
91c24f0a
DC
5540 children, so we need to look at them. Ditto for anonymous
5541 enums. */
933c6fe4 5542
72bf9492 5543 if (pdi->name != NULL || pdi->tag == DW_TAG_namespace
95554aad
TT
5544 || pdi->tag == DW_TAG_module || pdi->tag == DW_TAG_enumeration_type
5545 || pdi->tag == DW_TAG_imported_unit)
c906108c 5546 {
72bf9492 5547 switch (pdi->tag)
c906108c
SS
5548 {
5549 case DW_TAG_subprogram:
5734ee8b 5550 add_partial_subprogram (pdi, lowpc, highpc, need_pc, cu);
c906108c 5551 break;
72929c62 5552 case DW_TAG_constant:
c906108c
SS
5553 case DW_TAG_variable:
5554 case DW_TAG_typedef:
91c24f0a 5555 case DW_TAG_union_type:
72bf9492 5556 if (!pdi->is_declaration)
63d06c5c 5557 {
72bf9492 5558 add_partial_symbol (pdi, cu);
63d06c5c
DC
5559 }
5560 break;
c906108c 5561 case DW_TAG_class_type:
680b30c7 5562 case DW_TAG_interface_type:
c906108c 5563 case DW_TAG_structure_type:
72bf9492 5564 if (!pdi->is_declaration)
c906108c 5565 {
72bf9492 5566 add_partial_symbol (pdi, cu);
c906108c
SS
5567 }
5568 break;
91c24f0a 5569 case DW_TAG_enumeration_type:
72bf9492
DJ
5570 if (!pdi->is_declaration)
5571 add_partial_enumeration (pdi, cu);
c906108c
SS
5572 break;
5573 case DW_TAG_base_type:
a02abb62 5574 case DW_TAG_subrange_type:
c906108c 5575 /* File scope base type definitions are added to the partial
c5aa993b 5576 symbol table. */
72bf9492 5577 add_partial_symbol (pdi, cu);
c906108c 5578 break;
d9fa45fe 5579 case DW_TAG_namespace:
5734ee8b 5580 add_partial_namespace (pdi, lowpc, highpc, need_pc, cu);
91c24f0a 5581 break;
5d7cb8df
JK
5582 case DW_TAG_module:
5583 add_partial_module (pdi, lowpc, highpc, need_pc, cu);
5584 break;
95554aad
TT
5585 case DW_TAG_imported_unit:
5586 {
5587 struct dwarf2_per_cu_data *per_cu;
5588
f4dc4d17
DE
5589 /* For now we don't handle imported units in type units. */
5590 if (cu->per_cu->is_debug_types)
5591 {
5592 error (_("Dwarf Error: DW_TAG_imported_unit is not"
5593 " supported in type units [in module %s]"),
5594 cu->objfile->name);
5595 }
5596
95554aad 5597 per_cu = dwarf2_find_containing_comp_unit (pdi->d.offset,
36586728 5598 pdi->is_dwz,
95554aad
TT
5599 cu->objfile);
5600
5601 /* Go read the partial unit, if needed. */
5602 if (per_cu->v.psymtab == NULL)
5603 process_psymtab_comp_unit (per_cu, 1);
5604
f4dc4d17
DE
5605 VEC_safe_push (dwarf2_per_cu_ptr,
5606 cu->per_cu->s.imported_symtabs, per_cu);
95554aad
TT
5607 }
5608 break;
c906108c
SS
5609 default:
5610 break;
5611 }
5612 }
5613
72bf9492
DJ
5614 /* If the die has a sibling, skip to the sibling. */
5615
5616 pdi = pdi->die_sibling;
5617 }
5618}
5619
5620/* Functions used to compute the fully scoped name of a partial DIE.
91c24f0a 5621
72bf9492 5622 Normally, this is simple. For C++, the parent DIE's fully scoped
987504bb
JJ
5623 name is concatenated with "::" and the partial DIE's name. For
5624 Java, the same thing occurs except that "." is used instead of "::".
72bf9492
DJ
5625 Enumerators are an exception; they use the scope of their parent
5626 enumeration type, i.e. the name of the enumeration type is not
5627 prepended to the enumerator.
91c24f0a 5628
72bf9492
DJ
5629 There are two complexities. One is DW_AT_specification; in this
5630 case "parent" means the parent of the target of the specification,
5631 instead of the direct parent of the DIE. The other is compilers
5632 which do not emit DW_TAG_namespace; in this case we try to guess
5633 the fully qualified name of structure types from their members'
5634 linkage names. This must be done using the DIE's children rather
5635 than the children of any DW_AT_specification target. We only need
5636 to do this for structures at the top level, i.e. if the target of
5637 any DW_AT_specification (if any; otherwise the DIE itself) does not
5638 have a parent. */
5639
5640/* Compute the scope prefix associated with PDI's parent, in
5641 compilation unit CU. The result will be allocated on CU's
5642 comp_unit_obstack, or a copy of the already allocated PDI->NAME
5643 field. NULL is returned if no prefix is necessary. */
5644static char *
5645partial_die_parent_scope (struct partial_die_info *pdi,
5646 struct dwarf2_cu *cu)
5647{
5648 char *grandparent_scope;
5649 struct partial_die_info *parent, *real_pdi;
91c24f0a 5650
72bf9492
DJ
5651 /* We need to look at our parent DIE; if we have a DW_AT_specification,
5652 then this means the parent of the specification DIE. */
5653
5654 real_pdi = pdi;
72bf9492 5655 while (real_pdi->has_specification)
36586728
TT
5656 real_pdi = find_partial_die (real_pdi->spec_offset,
5657 real_pdi->spec_is_dwz, cu);
72bf9492
DJ
5658
5659 parent = real_pdi->die_parent;
5660 if (parent == NULL)
5661 return NULL;
5662
5663 if (parent->scope_set)
5664 return parent->scope;
5665
5666 fixup_partial_die (parent, cu);
5667
10b3939b 5668 grandparent_scope = partial_die_parent_scope (parent, cu);
72bf9492 5669
acebe513
UW
5670 /* GCC 4.0 and 4.1 had a bug (PR c++/28460) where they generated bogus
5671 DW_TAG_namespace DIEs with a name of "::" for the global namespace.
5672 Work around this problem here. */
5673 if (cu->language == language_cplus
6e70227d 5674 && parent->tag == DW_TAG_namespace
acebe513
UW
5675 && strcmp (parent->name, "::") == 0
5676 && grandparent_scope == NULL)
5677 {
5678 parent->scope = NULL;
5679 parent->scope_set = 1;
5680 return NULL;
5681 }
5682
9c6c53f7
SA
5683 if (pdi->tag == DW_TAG_enumerator)
5684 /* Enumerators should not get the name of the enumeration as a prefix. */
5685 parent->scope = grandparent_scope;
5686 else if (parent->tag == DW_TAG_namespace
f55ee35c 5687 || parent->tag == DW_TAG_module
72bf9492
DJ
5688 || parent->tag == DW_TAG_structure_type
5689 || parent->tag == DW_TAG_class_type
680b30c7 5690 || parent->tag == DW_TAG_interface_type
ceeb3d5a
TT
5691 || parent->tag == DW_TAG_union_type
5692 || parent->tag == DW_TAG_enumeration_type)
72bf9492
DJ
5693 {
5694 if (grandparent_scope == NULL)
5695 parent->scope = parent->name;
5696 else
3e43a32a
MS
5697 parent->scope = typename_concat (&cu->comp_unit_obstack,
5698 grandparent_scope,
f55ee35c 5699 parent->name, 0, cu);
72bf9492 5700 }
72bf9492
DJ
5701 else
5702 {
5703 /* FIXME drow/2004-04-01: What should we be doing with
5704 function-local names? For partial symbols, we should probably be
5705 ignoring them. */
5706 complaint (&symfile_complaints,
e2e0b3e5 5707 _("unhandled containing DIE tag %d for DIE at %d"),
b64f50a1 5708 parent->tag, pdi->offset.sect_off);
72bf9492 5709 parent->scope = grandparent_scope;
c906108c
SS
5710 }
5711
72bf9492
DJ
5712 parent->scope_set = 1;
5713 return parent->scope;
5714}
5715
5716/* Return the fully scoped name associated with PDI, from compilation unit
5717 CU. The result will be allocated with malloc. */
4568ecf9 5718
72bf9492
DJ
5719static char *
5720partial_die_full_name (struct partial_die_info *pdi,
5721 struct dwarf2_cu *cu)
5722{
5723 char *parent_scope;
5724
98bfdba5
PA
5725 /* If this is a template instantiation, we can not work out the
5726 template arguments from partial DIEs. So, unfortunately, we have
5727 to go through the full DIEs. At least any work we do building
5728 types here will be reused if full symbols are loaded later. */
5729 if (pdi->has_template_arguments)
5730 {
5731 fixup_partial_die (pdi, cu);
5732
5733 if (pdi->name != NULL && strchr (pdi->name, '<') == NULL)
5734 {
5735 struct die_info *die;
5736 struct attribute attr;
5737 struct dwarf2_cu *ref_cu = cu;
5738
b64f50a1 5739 /* DW_FORM_ref_addr is using section offset. */
98bfdba5
PA
5740 attr.name = 0;
5741 attr.form = DW_FORM_ref_addr;
4568ecf9 5742 attr.u.unsnd = pdi->offset.sect_off;
98bfdba5
PA
5743 die = follow_die_ref (NULL, &attr, &ref_cu);
5744
5745 return xstrdup (dwarf2_full_name (NULL, die, ref_cu));
5746 }
5747 }
5748
72bf9492
DJ
5749 parent_scope = partial_die_parent_scope (pdi, cu);
5750 if (parent_scope == NULL)
5751 return NULL;
5752 else
f55ee35c 5753 return typename_concat (NULL, parent_scope, pdi->name, 0, cu);
c906108c
SS
5754}
5755
5756static void
72bf9492 5757add_partial_symbol (struct partial_die_info *pdi, struct dwarf2_cu *cu)
c906108c 5758{
e7c27a73 5759 struct objfile *objfile = cu->objfile;
c906108c 5760 CORE_ADDR addr = 0;
decbce07 5761 char *actual_name = NULL;
e142c38c 5762 CORE_ADDR baseaddr;
72bf9492 5763 int built_actual_name = 0;
e142c38c
DJ
5764
5765 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
c906108c 5766
94af9270
KS
5767 actual_name = partial_die_full_name (pdi, cu);
5768 if (actual_name)
5769 built_actual_name = 1;
63d06c5c 5770
72bf9492
DJ
5771 if (actual_name == NULL)
5772 actual_name = pdi->name;
5773
c906108c
SS
5774 switch (pdi->tag)
5775 {
5776 case DW_TAG_subprogram:
2cfa0c8d 5777 if (pdi->is_external || cu->language == language_ada)
c906108c 5778 {
2cfa0c8d
JB
5779 /* brobecker/2007-12-26: Normally, only "external" DIEs are part
5780 of the global scope. But in Ada, we want to be able to access
5781 nested procedures globally. So all Ada subprograms are stored
5782 in the global scope. */
f47fb265 5783 /* prim_record_minimal_symbol (actual_name, pdi->lowpc + baseaddr,
c5aa993b 5784 mst_text, objfile); */
f47fb265
MS
5785 add_psymbol_to_list (actual_name, strlen (actual_name),
5786 built_actual_name,
5787 VAR_DOMAIN, LOC_BLOCK,
5788 &objfile->global_psymbols,
5789 0, pdi->lowpc + baseaddr,
5790 cu->language, objfile);
c906108c
SS
5791 }
5792 else
5793 {
f47fb265 5794 /* prim_record_minimal_symbol (actual_name, pdi->lowpc + baseaddr,
c5aa993b 5795 mst_file_text, objfile); */
f47fb265
MS
5796 add_psymbol_to_list (actual_name, strlen (actual_name),
5797 built_actual_name,
5798 VAR_DOMAIN, LOC_BLOCK,
5799 &objfile->static_psymbols,
5800 0, pdi->lowpc + baseaddr,
5801 cu->language, objfile);
c906108c
SS
5802 }
5803 break;
72929c62
JB
5804 case DW_TAG_constant:
5805 {
5806 struct psymbol_allocation_list *list;
5807
5808 if (pdi->is_external)
5809 list = &objfile->global_psymbols;
5810 else
5811 list = &objfile->static_psymbols;
f47fb265
MS
5812 add_psymbol_to_list (actual_name, strlen (actual_name),
5813 built_actual_name, VAR_DOMAIN, LOC_STATIC,
5814 list, 0, 0, cu->language, objfile);
72929c62
JB
5815 }
5816 break;
c906108c 5817 case DW_TAG_variable:
95554aad
TT
5818 if (pdi->d.locdesc)
5819 addr = decode_locdesc (pdi->d.locdesc, cu);
caac4577 5820
95554aad 5821 if (pdi->d.locdesc
caac4577
JG
5822 && addr == 0
5823 && !dwarf2_per_objfile->has_section_at_zero)
5824 {
5825 /* A global or static variable may also have been stripped
5826 out by the linker if unused, in which case its address
5827 will be nullified; do not add such variables into partial
5828 symbol table then. */
5829 }
5830 else if (pdi->is_external)
c906108c
SS
5831 {
5832 /* Global Variable.
5833 Don't enter into the minimal symbol tables as there is
5834 a minimal symbol table entry from the ELF symbols already.
5835 Enter into partial symbol table if it has a location
5836 descriptor or a type.
5837 If the location descriptor is missing, new_symbol will create
5838 a LOC_UNRESOLVED symbol, the address of the variable will then
5839 be determined from the minimal symbol table whenever the variable
5840 is referenced.
5841 The address for the partial symbol table entry is not
5842 used by GDB, but it comes in handy for debugging partial symbol
5843 table building. */
5844
95554aad 5845 if (pdi->d.locdesc || pdi->has_type)
f47fb265
MS
5846 add_psymbol_to_list (actual_name, strlen (actual_name),
5847 built_actual_name,
5848 VAR_DOMAIN, LOC_STATIC,
5849 &objfile->global_psymbols,
5850 0, addr + baseaddr,
5851 cu->language, objfile);
c906108c
SS
5852 }
5853 else
5854 {
0963b4bd 5855 /* Static Variable. Skip symbols without location descriptors. */
95554aad 5856 if (pdi->d.locdesc == NULL)
decbce07
MS
5857 {
5858 if (built_actual_name)
5859 xfree (actual_name);
5860 return;
5861 }
f47fb265 5862 /* prim_record_minimal_symbol (actual_name, addr + baseaddr,
c5aa993b 5863 mst_file_data, objfile); */
f47fb265
MS
5864 add_psymbol_to_list (actual_name, strlen (actual_name),
5865 built_actual_name,
5866 VAR_DOMAIN, LOC_STATIC,
5867 &objfile->static_psymbols,
5868 0, addr + baseaddr,
5869 cu->language, objfile);
c906108c
SS
5870 }
5871 break;
5872 case DW_TAG_typedef:
5873 case DW_TAG_base_type:
a02abb62 5874 case DW_TAG_subrange_type:
38d518c9 5875 add_psymbol_to_list (actual_name, strlen (actual_name),
04a679b8 5876 built_actual_name,
176620f1 5877 VAR_DOMAIN, LOC_TYPEDEF,
c906108c 5878 &objfile->static_psymbols,
e142c38c 5879 0, (CORE_ADDR) 0, cu->language, objfile);
c906108c 5880 break;
72bf9492
DJ
5881 case DW_TAG_namespace:
5882 add_psymbol_to_list (actual_name, strlen (actual_name),
04a679b8 5883 built_actual_name,
72bf9492
DJ
5884 VAR_DOMAIN, LOC_TYPEDEF,
5885 &objfile->global_psymbols,
5886 0, (CORE_ADDR) 0, cu->language, objfile);
5887 break;
c906108c 5888 case DW_TAG_class_type:
680b30c7 5889 case DW_TAG_interface_type:
c906108c
SS
5890 case DW_TAG_structure_type:
5891 case DW_TAG_union_type:
5892 case DW_TAG_enumeration_type:
fa4028e9
JB
5893 /* Skip external references. The DWARF standard says in the section
5894 about "Structure, Union, and Class Type Entries": "An incomplete
5895 structure, union or class type is represented by a structure,
5896 union or class entry that does not have a byte size attribute
5897 and that has a DW_AT_declaration attribute." */
5898 if (!pdi->has_byte_size && pdi->is_declaration)
decbce07
MS
5899 {
5900 if (built_actual_name)
5901 xfree (actual_name);
5902 return;
5903 }
fa4028e9 5904
63d06c5c
DC
5905 /* NOTE: carlton/2003-10-07: See comment in new_symbol about
5906 static vs. global. */
38d518c9 5907 add_psymbol_to_list (actual_name, strlen (actual_name),
04a679b8 5908 built_actual_name,
176620f1 5909 STRUCT_DOMAIN, LOC_TYPEDEF,
987504bb
JJ
5910 (cu->language == language_cplus
5911 || cu->language == language_java)
63d06c5c
DC
5912 ? &objfile->global_psymbols
5913 : &objfile->static_psymbols,
e142c38c 5914 0, (CORE_ADDR) 0, cu->language, objfile);
c906108c 5915
c906108c
SS
5916 break;
5917 case DW_TAG_enumerator:
38d518c9 5918 add_psymbol_to_list (actual_name, strlen (actual_name),
04a679b8 5919 built_actual_name,
176620f1 5920 VAR_DOMAIN, LOC_CONST,
987504bb
JJ
5921 (cu->language == language_cplus
5922 || cu->language == language_java)
f6fe98ef
DJ
5923 ? &objfile->global_psymbols
5924 : &objfile->static_psymbols,
e142c38c 5925 0, (CORE_ADDR) 0, cu->language, objfile);
c906108c
SS
5926 break;
5927 default:
5928 break;
5929 }
5c4e30ca 5930
72bf9492
DJ
5931 if (built_actual_name)
5932 xfree (actual_name);
c906108c
SS
5933}
5934
5c4e30ca
DC
5935/* Read a partial die corresponding to a namespace; also, add a symbol
5936 corresponding to that namespace to the symbol table. NAMESPACE is
5937 the name of the enclosing namespace. */
91c24f0a 5938
72bf9492
DJ
5939static void
5940add_partial_namespace (struct partial_die_info *pdi,
91c24f0a 5941 CORE_ADDR *lowpc, CORE_ADDR *highpc,
5734ee8b 5942 int need_pc, struct dwarf2_cu *cu)
91c24f0a 5943{
72bf9492 5944 /* Add a symbol for the namespace. */
e7c27a73 5945
72bf9492 5946 add_partial_symbol (pdi, cu);
5c4e30ca
DC
5947
5948 /* Now scan partial symbols in that namespace. */
5949
91c24f0a 5950 if (pdi->has_children)
5734ee8b 5951 scan_partial_symbols (pdi->die_child, lowpc, highpc, need_pc, cu);
91c24f0a
DC
5952}
5953
5d7cb8df
JK
5954/* Read a partial die corresponding to a Fortran module. */
5955
5956static void
5957add_partial_module (struct partial_die_info *pdi, CORE_ADDR *lowpc,
5958 CORE_ADDR *highpc, int need_pc, struct dwarf2_cu *cu)
5959{
f55ee35c 5960 /* Now scan partial symbols in that module. */
5d7cb8df
JK
5961
5962 if (pdi->has_children)
5963 scan_partial_symbols (pdi->die_child, lowpc, highpc, need_pc, cu);
5964}
5965
bc30ff58
JB
5966/* Read a partial die corresponding to a subprogram and create a partial
5967 symbol for that subprogram. When the CU language allows it, this
5968 routine also defines a partial symbol for each nested subprogram
5969 that this subprogram contains.
6e70227d 5970
bc30ff58
JB
5971 DIE my also be a lexical block, in which case we simply search
5972 recursively for suprograms defined inside that lexical block.
5973 Again, this is only performed when the CU language allows this
5974 type of definitions. */
5975
5976static void
5977add_partial_subprogram (struct partial_die_info *pdi,
5978 CORE_ADDR *lowpc, CORE_ADDR *highpc,
5734ee8b 5979 int need_pc, struct dwarf2_cu *cu)
bc30ff58
JB
5980{
5981 if (pdi->tag == DW_TAG_subprogram)
5982 {
5983 if (pdi->has_pc_info)
5984 {
5985 if (pdi->lowpc < *lowpc)
5986 *lowpc = pdi->lowpc;
5987 if (pdi->highpc > *highpc)
5988 *highpc = pdi->highpc;
5734ee8b
DJ
5989 if (need_pc)
5990 {
5991 CORE_ADDR baseaddr;
5992 struct objfile *objfile = cu->objfile;
5993
5994 baseaddr = ANOFFSET (objfile->section_offsets,
5995 SECT_OFF_TEXT (objfile));
5996 addrmap_set_empty (objfile->psymtabs_addrmap,
01637564
DE
5997 pdi->lowpc + baseaddr,
5998 pdi->highpc - 1 + baseaddr,
9291a0cd 5999 cu->per_cu->v.psymtab);
5734ee8b 6000 }
481860b3
GB
6001 }
6002
6003 if (pdi->has_pc_info || (!pdi->is_external && pdi->may_be_inlined))
6004 {
bc30ff58 6005 if (!pdi->is_declaration)
e8d05480
JB
6006 /* Ignore subprogram DIEs that do not have a name, they are
6007 illegal. Do not emit a complaint at this point, we will
6008 do so when we convert this psymtab into a symtab. */
6009 if (pdi->name)
6010 add_partial_symbol (pdi, cu);
bc30ff58
JB
6011 }
6012 }
6e70227d 6013
bc30ff58
JB
6014 if (! pdi->has_children)
6015 return;
6016
6017 if (cu->language == language_ada)
6018 {
6019 pdi = pdi->die_child;
6020 while (pdi != NULL)
6021 {
6022 fixup_partial_die (pdi, cu);
6023 if (pdi->tag == DW_TAG_subprogram
6024 || pdi->tag == DW_TAG_lexical_block)
5734ee8b 6025 add_partial_subprogram (pdi, lowpc, highpc, need_pc, cu);
bc30ff58
JB
6026 pdi = pdi->die_sibling;
6027 }
6028 }
6029}
6030
91c24f0a
DC
6031/* Read a partial die corresponding to an enumeration type. */
6032
72bf9492
DJ
6033static void
6034add_partial_enumeration (struct partial_die_info *enum_pdi,
6035 struct dwarf2_cu *cu)
91c24f0a 6036{
72bf9492 6037 struct partial_die_info *pdi;
91c24f0a
DC
6038
6039 if (enum_pdi->name != NULL)
72bf9492
DJ
6040 add_partial_symbol (enum_pdi, cu);
6041
6042 pdi = enum_pdi->die_child;
6043 while (pdi)
91c24f0a 6044 {
72bf9492 6045 if (pdi->tag != DW_TAG_enumerator || pdi->name == NULL)
e2e0b3e5 6046 complaint (&symfile_complaints, _("malformed enumerator DIE ignored"));
91c24f0a 6047 else
72bf9492
DJ
6048 add_partial_symbol (pdi, cu);
6049 pdi = pdi->die_sibling;
91c24f0a 6050 }
91c24f0a
DC
6051}
6052
6caca83c
CC
6053/* Return the initial uleb128 in the die at INFO_PTR. */
6054
6055static unsigned int
6056peek_abbrev_code (bfd *abfd, gdb_byte *info_ptr)
6057{
6058 unsigned int bytes_read;
6059
6060 return read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
6061}
6062
4bb7a0a7
DJ
6063/* Read the initial uleb128 in the die at INFO_PTR in compilation unit CU.
6064 Return the corresponding abbrev, or NULL if the number is zero (indicating
6065 an empty DIE). In either case *BYTES_READ will be set to the length of
6066 the initial number. */
6067
6068static struct abbrev_info *
fe1b8b76 6069peek_die_abbrev (gdb_byte *info_ptr, unsigned int *bytes_read,
891d2f0b 6070 struct dwarf2_cu *cu)
4bb7a0a7
DJ
6071{
6072 bfd *abfd = cu->objfile->obfd;
6073 unsigned int abbrev_number;
6074 struct abbrev_info *abbrev;
6075
6076 abbrev_number = read_unsigned_leb128 (abfd, info_ptr, bytes_read);
6077
6078 if (abbrev_number == 0)
6079 return NULL;
6080
433df2d4 6081 abbrev = abbrev_table_lookup_abbrev (cu->abbrev_table, abbrev_number);
4bb7a0a7
DJ
6082 if (!abbrev)
6083 {
3e43a32a
MS
6084 error (_("Dwarf Error: Could not find abbrev number %d [in module %s]"),
6085 abbrev_number, bfd_get_filename (abfd));
4bb7a0a7
DJ
6086 }
6087
6088 return abbrev;
6089}
6090
93311388
DE
6091/* Scan the debug information for CU starting at INFO_PTR in buffer BUFFER.
6092 Returns a pointer to the end of a series of DIEs, terminated by an empty
4bb7a0a7
DJ
6093 DIE. Any children of the skipped DIEs will also be skipped. */
6094
fe1b8b76 6095static gdb_byte *
dee91e82 6096skip_children (const struct die_reader_specs *reader, gdb_byte *info_ptr)
4bb7a0a7 6097{
dee91e82 6098 struct dwarf2_cu *cu = reader->cu;
4bb7a0a7
DJ
6099 struct abbrev_info *abbrev;
6100 unsigned int bytes_read;
6101
6102 while (1)
6103 {
6104 abbrev = peek_die_abbrev (info_ptr, &bytes_read, cu);
6105 if (abbrev == NULL)
6106 return info_ptr + bytes_read;
6107 else
dee91e82 6108 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
4bb7a0a7
DJ
6109 }
6110}
6111
93311388
DE
6112/* Scan the debug information for CU starting at INFO_PTR in buffer BUFFER.
6113 INFO_PTR should point just after the initial uleb128 of a DIE, and the
4bb7a0a7
DJ
6114 abbrev corresponding to that skipped uleb128 should be passed in
6115 ABBREV. Returns a pointer to this DIE's sibling, skipping any
6116 children. */
6117
fe1b8b76 6118static gdb_byte *
dee91e82
DE
6119skip_one_die (const struct die_reader_specs *reader, gdb_byte *info_ptr,
6120 struct abbrev_info *abbrev)
4bb7a0a7
DJ
6121{
6122 unsigned int bytes_read;
6123 struct attribute attr;
dee91e82
DE
6124 bfd *abfd = reader->abfd;
6125 struct dwarf2_cu *cu = reader->cu;
6126 gdb_byte *buffer = reader->buffer;
f664829e
DE
6127 const gdb_byte *buffer_end = reader->buffer_end;
6128 gdb_byte *start_info_ptr = info_ptr;
4bb7a0a7
DJ
6129 unsigned int form, i;
6130
6131 for (i = 0; i < abbrev->num_attrs; i++)
6132 {
6133 /* The only abbrev we care about is DW_AT_sibling. */
6134 if (abbrev->attrs[i].name == DW_AT_sibling)
6135 {
dee91e82 6136 read_attribute (reader, &attr, &abbrev->attrs[i], info_ptr);
4bb7a0a7 6137 if (attr.form == DW_FORM_ref_addr)
3e43a32a
MS
6138 complaint (&symfile_complaints,
6139 _("ignoring absolute DW_AT_sibling"));
4bb7a0a7 6140 else
b64f50a1 6141 return buffer + dwarf2_get_ref_die_offset (&attr).sect_off;
4bb7a0a7
DJ
6142 }
6143
6144 /* If it isn't DW_AT_sibling, skip this attribute. */
6145 form = abbrev->attrs[i].form;
6146 skip_attribute:
6147 switch (form)
6148 {
4bb7a0a7 6149 case DW_FORM_ref_addr:
ae411497
TT
6150 /* In DWARF 2, DW_FORM_ref_addr is address sized; in DWARF 3
6151 and later it is offset sized. */
6152 if (cu->header.version == 2)
6153 info_ptr += cu->header.addr_size;
6154 else
6155 info_ptr += cu->header.offset_size;
6156 break;
36586728
TT
6157 case DW_FORM_GNU_ref_alt:
6158 info_ptr += cu->header.offset_size;
6159 break;
ae411497 6160 case DW_FORM_addr:
4bb7a0a7
DJ
6161 info_ptr += cu->header.addr_size;
6162 break;
6163 case DW_FORM_data1:
6164 case DW_FORM_ref1:
6165 case DW_FORM_flag:
6166 info_ptr += 1;
6167 break;
2dc7f7b3
TT
6168 case DW_FORM_flag_present:
6169 break;
4bb7a0a7
DJ
6170 case DW_FORM_data2:
6171 case DW_FORM_ref2:
6172 info_ptr += 2;
6173 break;
6174 case DW_FORM_data4:
6175 case DW_FORM_ref4:
6176 info_ptr += 4;
6177 break;
6178 case DW_FORM_data8:
6179 case DW_FORM_ref8:
55f1336d 6180 case DW_FORM_ref_sig8:
4bb7a0a7
DJ
6181 info_ptr += 8;
6182 break;
6183 case DW_FORM_string:
9b1c24c8 6184 read_direct_string (abfd, info_ptr, &bytes_read);
4bb7a0a7
DJ
6185 info_ptr += bytes_read;
6186 break;
2dc7f7b3 6187 case DW_FORM_sec_offset:
4bb7a0a7 6188 case DW_FORM_strp:
36586728 6189 case DW_FORM_GNU_strp_alt:
4bb7a0a7
DJ
6190 info_ptr += cu->header.offset_size;
6191 break;
2dc7f7b3 6192 case DW_FORM_exprloc:
4bb7a0a7
DJ
6193 case DW_FORM_block:
6194 info_ptr += read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
6195 info_ptr += bytes_read;
6196 break;
6197 case DW_FORM_block1:
6198 info_ptr += 1 + read_1_byte (abfd, info_ptr);
6199 break;
6200 case DW_FORM_block2:
6201 info_ptr += 2 + read_2_bytes (abfd, info_ptr);
6202 break;
6203 case DW_FORM_block4:
6204 info_ptr += 4 + read_4_bytes (abfd, info_ptr);
6205 break;
6206 case DW_FORM_sdata:
6207 case DW_FORM_udata:
6208 case DW_FORM_ref_udata:
3019eac3
DE
6209 case DW_FORM_GNU_addr_index:
6210 case DW_FORM_GNU_str_index:
f664829e 6211 info_ptr = (gdb_byte *) safe_skip_leb128 (info_ptr, buffer_end);
4bb7a0a7
DJ
6212 break;
6213 case DW_FORM_indirect:
6214 form = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
6215 info_ptr += bytes_read;
6216 /* We need to continue parsing from here, so just go back to
6217 the top. */
6218 goto skip_attribute;
6219
6220 default:
3e43a32a
MS
6221 error (_("Dwarf Error: Cannot handle %s "
6222 "in DWARF reader [in module %s]"),
4bb7a0a7
DJ
6223 dwarf_form_name (form),
6224 bfd_get_filename (abfd));
6225 }
6226 }
6227
6228 if (abbrev->has_children)
dee91e82 6229 return skip_children (reader, info_ptr);
4bb7a0a7
DJ
6230 else
6231 return info_ptr;
6232}
6233
93311388 6234/* Locate ORIG_PDI's sibling.
dee91e82 6235 INFO_PTR should point to the start of the next DIE after ORIG_PDI. */
91c24f0a 6236
fe1b8b76 6237static gdb_byte *
dee91e82
DE
6238locate_pdi_sibling (const struct die_reader_specs *reader,
6239 struct partial_die_info *orig_pdi,
6240 gdb_byte *info_ptr)
91c24f0a
DC
6241{
6242 /* Do we know the sibling already? */
72bf9492 6243
91c24f0a
DC
6244 if (orig_pdi->sibling)
6245 return orig_pdi->sibling;
6246
6247 /* Are there any children to deal with? */
6248
6249 if (!orig_pdi->has_children)
6250 return info_ptr;
6251
4bb7a0a7 6252 /* Skip the children the long way. */
91c24f0a 6253
dee91e82 6254 return skip_children (reader, info_ptr);
91c24f0a
DC
6255}
6256
c906108c
SS
6257/* Expand this partial symbol table into a full symbol table. */
6258
6259static void
fba45db2 6260dwarf2_psymtab_to_symtab (struct partial_symtab *pst)
c906108c 6261{
c906108c
SS
6262 if (pst != NULL)
6263 {
6264 if (pst->readin)
6265 {
3e43a32a
MS
6266 warning (_("bug: psymtab for %s is already read in."),
6267 pst->filename);
c906108c
SS
6268 }
6269 else
6270 {
6271 if (info_verbose)
6272 {
3e43a32a
MS
6273 printf_filtered (_("Reading in symbols for %s..."),
6274 pst->filename);
c906108c
SS
6275 gdb_flush (gdb_stdout);
6276 }
6277
10b3939b
DJ
6278 /* Restore our global data. */
6279 dwarf2_per_objfile = objfile_data (pst->objfile,
6280 dwarf2_objfile_data_key);
6281
b2ab525c
KB
6282 /* If this psymtab is constructed from a debug-only objfile, the
6283 has_section_at_zero flag will not necessarily be correct. We
6284 can get the correct value for this flag by looking at the data
6285 associated with the (presumably stripped) associated objfile. */
6286 if (pst->objfile->separate_debug_objfile_backlink)
6287 {
6288 struct dwarf2_per_objfile *dpo_backlink
6289 = objfile_data (pst->objfile->separate_debug_objfile_backlink,
6290 dwarf2_objfile_data_key);
9a619af0 6291
b2ab525c
KB
6292 dwarf2_per_objfile->has_section_at_zero
6293 = dpo_backlink->has_section_at_zero;
6294 }
6295
98bfdba5
PA
6296 dwarf2_per_objfile->reading_partial_symbols = 0;
6297
c906108c
SS
6298 psymtab_to_symtab_1 (pst);
6299
6300 /* Finish up the debug error message. */
6301 if (info_verbose)
a3f17187 6302 printf_filtered (_("done.\n"));
c906108c
SS
6303 }
6304 }
95554aad
TT
6305
6306 process_cu_includes ();
c906108c 6307}
9cdd5dbd
DE
6308\f
6309/* Reading in full CUs. */
c906108c 6310
10b3939b
DJ
6311/* Add PER_CU to the queue. */
6312
6313static void
95554aad
TT
6314queue_comp_unit (struct dwarf2_per_cu_data *per_cu,
6315 enum language pretend_language)
10b3939b
DJ
6316{
6317 struct dwarf2_queue_item *item;
6318
6319 per_cu->queued = 1;
6320 item = xmalloc (sizeof (*item));
6321 item->per_cu = per_cu;
95554aad 6322 item->pretend_language = pretend_language;
10b3939b
DJ
6323 item->next = NULL;
6324
6325 if (dwarf2_queue == NULL)
6326 dwarf2_queue = item;
6327 else
6328 dwarf2_queue_tail->next = item;
6329
6330 dwarf2_queue_tail = item;
6331}
6332
0907af0c
DE
6333/* THIS_CU has a reference to PER_CU. If necessary, load the new compilation
6334 unit and add it to our queue.
6335 The result is non-zero if PER_CU was queued, otherwise the result is zero
6336 meaning either PER_CU is already queued or it is already loaded. */
6337
6338static int
6339maybe_queue_comp_unit (struct dwarf2_cu *this_cu,
6340 struct dwarf2_per_cu_data *per_cu,
6341 enum language pretend_language)
6342{
6343 /* We may arrive here during partial symbol reading, if we need full
6344 DIEs to process an unusual case (e.g. template arguments). Do
6345 not queue PER_CU, just tell our caller to load its DIEs. */
6346 if (dwarf2_per_objfile->reading_partial_symbols)
6347 {
6348 if (per_cu->cu == NULL || per_cu->cu->dies == NULL)
6349 return 1;
6350 return 0;
6351 }
6352
6353 /* Mark the dependence relation so that we don't flush PER_CU
6354 too early. */
6355 dwarf2_add_dependence (this_cu, per_cu);
6356
6357 /* If it's already on the queue, we have nothing to do. */
6358 if (per_cu->queued)
6359 return 0;
6360
6361 /* If the compilation unit is already loaded, just mark it as
6362 used. */
6363 if (per_cu->cu != NULL)
6364 {
6365 per_cu->cu->last_used = 0;
6366 return 0;
6367 }
6368
6369 /* Add it to the queue. */
6370 queue_comp_unit (per_cu, pretend_language);
6371
6372 return 1;
6373}
6374
10b3939b
DJ
6375/* Process the queue. */
6376
6377static void
a0f42c21 6378process_queue (void)
10b3939b
DJ
6379{
6380 struct dwarf2_queue_item *item, *next_item;
6381
45cfd468
DE
6382 if (dwarf2_read_debug)
6383 {
6384 fprintf_unfiltered (gdb_stdlog,
6385 "Expanding one or more symtabs of objfile %s ...\n",
6386 dwarf2_per_objfile->objfile->name);
6387 }
6388
03dd20cc
DJ
6389 /* The queue starts out with one item, but following a DIE reference
6390 may load a new CU, adding it to the end of the queue. */
10b3939b
DJ
6391 for (item = dwarf2_queue; item != NULL; dwarf2_queue = item = next_item)
6392 {
9291a0cd
TT
6393 if (dwarf2_per_objfile->using_index
6394 ? !item->per_cu->v.quick->symtab
6395 : (item->per_cu->v.psymtab && !item->per_cu->v.psymtab->readin))
f4dc4d17
DE
6396 {
6397 struct dwarf2_per_cu_data *per_cu = item->per_cu;
6398
6399 if (dwarf2_read_debug)
6400 {
6401 fprintf_unfiltered (gdb_stdlog,
6402 "Expanding symtab of %s at offset 0x%x\n",
6403 per_cu->is_debug_types ? "TU" : "CU",
6404 per_cu->offset.sect_off);
6405 }
6406
6407 if (per_cu->is_debug_types)
6408 process_full_type_unit (per_cu, item->pretend_language);
6409 else
6410 process_full_comp_unit (per_cu, item->pretend_language);
6411
6412 if (dwarf2_read_debug)
6413 {
6414 fprintf_unfiltered (gdb_stdlog,
6415 "Done expanding %s at offset 0x%x\n",
6416 per_cu->is_debug_types ? "TU" : "CU",
6417 per_cu->offset.sect_off);
6418 }
6419 }
10b3939b
DJ
6420
6421 item->per_cu->queued = 0;
6422 next_item = item->next;
6423 xfree (item);
6424 }
6425
6426 dwarf2_queue_tail = NULL;
45cfd468
DE
6427
6428 if (dwarf2_read_debug)
6429 {
6430 fprintf_unfiltered (gdb_stdlog, "Done expanding symtabs of %s.\n",
6431 dwarf2_per_objfile->objfile->name);
6432 }
10b3939b
DJ
6433}
6434
6435/* Free all allocated queue entries. This function only releases anything if
6436 an error was thrown; if the queue was processed then it would have been
6437 freed as we went along. */
6438
6439static void
6440dwarf2_release_queue (void *dummy)
6441{
6442 struct dwarf2_queue_item *item, *last;
6443
6444 item = dwarf2_queue;
6445 while (item)
6446 {
6447 /* Anything still marked queued is likely to be in an
6448 inconsistent state, so discard it. */
6449 if (item->per_cu->queued)
6450 {
6451 if (item->per_cu->cu != NULL)
dee91e82 6452 free_one_cached_comp_unit (item->per_cu);
10b3939b
DJ
6453 item->per_cu->queued = 0;
6454 }
6455
6456 last = item;
6457 item = item->next;
6458 xfree (last);
6459 }
6460
6461 dwarf2_queue = dwarf2_queue_tail = NULL;
6462}
6463
6464/* Read in full symbols for PST, and anything it depends on. */
6465
c906108c 6466static void
fba45db2 6467psymtab_to_symtab_1 (struct partial_symtab *pst)
c906108c 6468{
10b3939b 6469 struct dwarf2_per_cu_data *per_cu;
aaa75496
JB
6470 int i;
6471
95554aad
TT
6472 if (pst->readin)
6473 return;
6474
aaa75496 6475 for (i = 0; i < pst->number_of_dependencies; i++)
95554aad
TT
6476 if (!pst->dependencies[i]->readin
6477 && pst->dependencies[i]->user == NULL)
aaa75496
JB
6478 {
6479 /* Inform about additional files that need to be read in. */
6480 if (info_verbose)
6481 {
a3f17187 6482 /* FIXME: i18n: Need to make this a single string. */
aaa75496
JB
6483 fputs_filtered (" ", gdb_stdout);
6484 wrap_here ("");
6485 fputs_filtered ("and ", gdb_stdout);
6486 wrap_here ("");
6487 printf_filtered ("%s...", pst->dependencies[i]->filename);
0963b4bd 6488 wrap_here (""); /* Flush output. */
aaa75496
JB
6489 gdb_flush (gdb_stdout);
6490 }
6491 psymtab_to_symtab_1 (pst->dependencies[i]);
6492 }
6493
e38df1d0 6494 per_cu = pst->read_symtab_private;
10b3939b
DJ
6495
6496 if (per_cu == NULL)
aaa75496
JB
6497 {
6498 /* It's an include file, no symbols to read for it.
6499 Everything is in the parent symtab. */
6500 pst->readin = 1;
6501 return;
6502 }
c906108c 6503
a0f42c21 6504 dw2_do_instantiate_symtab (per_cu);
10b3939b
DJ
6505}
6506
dee91e82
DE
6507/* Trivial hash function for die_info: the hash value of a DIE
6508 is its offset in .debug_info for this objfile. */
10b3939b 6509
dee91e82
DE
6510static hashval_t
6511die_hash (const void *item)
10b3939b 6512{
dee91e82 6513 const struct die_info *die = item;
6502dd73 6514
dee91e82
DE
6515 return die->offset.sect_off;
6516}
63d06c5c 6517
dee91e82
DE
6518/* Trivial comparison function for die_info structures: two DIEs
6519 are equal if they have the same offset. */
98bfdba5 6520
dee91e82
DE
6521static int
6522die_eq (const void *item_lhs, const void *item_rhs)
6523{
6524 const struct die_info *die_lhs = item_lhs;
6525 const struct die_info *die_rhs = item_rhs;
c906108c 6526
dee91e82
DE
6527 return die_lhs->offset.sect_off == die_rhs->offset.sect_off;
6528}
c906108c 6529
dee91e82
DE
6530/* die_reader_func for load_full_comp_unit.
6531 This is identical to read_signatured_type_reader,
6532 but is kept separate for now. */
c906108c 6533
dee91e82
DE
6534static void
6535load_full_comp_unit_reader (const struct die_reader_specs *reader,
6536 gdb_byte *info_ptr,
6537 struct die_info *comp_unit_die,
6538 int has_children,
6539 void *data)
6540{
6541 struct dwarf2_cu *cu = reader->cu;
95554aad 6542 enum language *language_ptr = data;
6caca83c 6543
dee91e82
DE
6544 gdb_assert (cu->die_hash == NULL);
6545 cu->die_hash =
6546 htab_create_alloc_ex (cu->header.length / 12,
6547 die_hash,
6548 die_eq,
6549 NULL,
6550 &cu->comp_unit_obstack,
6551 hashtab_obstack_allocate,
6552 dummy_obstack_deallocate);
e142c38c 6553
dee91e82
DE
6554 if (has_children)
6555 comp_unit_die->child = read_die_and_siblings (reader, info_ptr,
6556 &info_ptr, comp_unit_die);
6557 cu->dies = comp_unit_die;
6558 /* comp_unit_die is not stored in die_hash, no need. */
10b3939b
DJ
6559
6560 /* We try not to read any attributes in this function, because not
9cdd5dbd 6561 all CUs needed for references have been loaded yet, and symbol
10b3939b 6562 table processing isn't initialized. But we have to set the CU language,
dee91e82
DE
6563 or we won't be able to build types correctly.
6564 Similarly, if we do not read the producer, we can not apply
6565 producer-specific interpretation. */
95554aad 6566 prepare_one_comp_unit (cu, cu->dies, *language_ptr);
dee91e82 6567}
10b3939b 6568
dee91e82 6569/* Load the DIEs associated with PER_CU into memory. */
a6c727b2 6570
dee91e82 6571static void
95554aad
TT
6572load_full_comp_unit (struct dwarf2_per_cu_data *this_cu,
6573 enum language pretend_language)
dee91e82 6574{
3019eac3 6575 gdb_assert (! this_cu->is_debug_types);
c5b7e1cb 6576
f4dc4d17
DE
6577 init_cutu_and_read_dies (this_cu, NULL, 1, 1,
6578 load_full_comp_unit_reader, &pretend_language);
10b3939b
DJ
6579}
6580
3da10d80
KS
6581/* Add a DIE to the delayed physname list. */
6582
6583static void
6584add_to_method_list (struct type *type, int fnfield_index, int index,
6585 const char *name, struct die_info *die,
6586 struct dwarf2_cu *cu)
6587{
6588 struct delayed_method_info mi;
6589 mi.type = type;
6590 mi.fnfield_index = fnfield_index;
6591 mi.index = index;
6592 mi.name = name;
6593 mi.die = die;
6594 VEC_safe_push (delayed_method_info, cu->method_list, &mi);
6595}
6596
6597/* A cleanup for freeing the delayed method list. */
6598
6599static void
6600free_delayed_list (void *ptr)
6601{
6602 struct dwarf2_cu *cu = (struct dwarf2_cu *) ptr;
6603 if (cu->method_list != NULL)
6604 {
6605 VEC_free (delayed_method_info, cu->method_list);
6606 cu->method_list = NULL;
6607 }
6608}
6609
6610/* Compute the physnames of any methods on the CU's method list.
6611
6612 The computation of method physnames is delayed in order to avoid the
6613 (bad) condition that one of the method's formal parameters is of an as yet
6614 incomplete type. */
6615
6616static void
6617compute_delayed_physnames (struct dwarf2_cu *cu)
6618{
6619 int i;
6620 struct delayed_method_info *mi;
6621 for (i = 0; VEC_iterate (delayed_method_info, cu->method_list, i, mi) ; ++i)
6622 {
1d06ead6 6623 const char *physname;
3da10d80
KS
6624 struct fn_fieldlist *fn_flp
6625 = &TYPE_FN_FIELDLIST (mi->type, mi->fnfield_index);
1d06ead6 6626 physname = dwarf2_physname ((char *) mi->name, mi->die, cu);
3da10d80
KS
6627 fn_flp->fn_fields[mi->index].physname = physname ? physname : "";
6628 }
6629}
6630
a766d390
DE
6631/* Go objects should be embedded in a DW_TAG_module DIE,
6632 and it's not clear if/how imported objects will appear.
6633 To keep Go support simple until that's worked out,
6634 go back through what we've read and create something usable.
6635 We could do this while processing each DIE, and feels kinda cleaner,
6636 but that way is more invasive.
6637 This is to, for example, allow the user to type "p var" or "b main"
6638 without having to specify the package name, and allow lookups
6639 of module.object to work in contexts that use the expression
6640 parser. */
6641
6642static void
6643fixup_go_packaging (struct dwarf2_cu *cu)
6644{
6645 char *package_name = NULL;
6646 struct pending *list;
6647 int i;
6648
6649 for (list = global_symbols; list != NULL; list = list->next)
6650 {
6651 for (i = 0; i < list->nsyms; ++i)
6652 {
6653 struct symbol *sym = list->symbol[i];
6654
6655 if (SYMBOL_LANGUAGE (sym) == language_go
6656 && SYMBOL_CLASS (sym) == LOC_BLOCK)
6657 {
6658 char *this_package_name = go_symbol_package_name (sym);
6659
6660 if (this_package_name == NULL)
6661 continue;
6662 if (package_name == NULL)
6663 package_name = this_package_name;
6664 else
6665 {
6666 if (strcmp (package_name, this_package_name) != 0)
6667 complaint (&symfile_complaints,
6668 _("Symtab %s has objects from two different Go packages: %s and %s"),
6669 (sym->symtab && sym->symtab->filename
6670 ? sym->symtab->filename
6671 : cu->objfile->name),
6672 this_package_name, package_name);
6673 xfree (this_package_name);
6674 }
6675 }
6676 }
6677 }
6678
6679 if (package_name != NULL)
6680 {
6681 struct objfile *objfile = cu->objfile;
6682 struct type *type = init_type (TYPE_CODE_MODULE, 0, 0,
6683 package_name, objfile);
6684 struct symbol *sym;
6685
6686 TYPE_TAG_NAME (type) = TYPE_NAME (type);
6687
6688 sym = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct symbol);
6689 SYMBOL_SET_LANGUAGE (sym, language_go);
6690 SYMBOL_SET_NAMES (sym, package_name, strlen (package_name), 1, objfile);
6691 /* This is not VAR_DOMAIN because we want a way to ensure a lookup of,
6692 e.g., "main" finds the "main" module and not C's main(). */
6693 SYMBOL_DOMAIN (sym) = STRUCT_DOMAIN;
6694 SYMBOL_CLASS (sym) = LOC_TYPEDEF;
6695 SYMBOL_TYPE (sym) = type;
6696
6697 add_symbol_to_list (sym, &global_symbols);
6698
6699 xfree (package_name);
6700 }
6701}
6702
95554aad
TT
6703static void compute_symtab_includes (struct dwarf2_per_cu_data *per_cu);
6704
6705/* Return the symtab for PER_CU. This works properly regardless of
6706 whether we're using the index or psymtabs. */
6707
6708static struct symtab *
6709get_symtab (struct dwarf2_per_cu_data *per_cu)
6710{
6711 return (dwarf2_per_objfile->using_index
6712 ? per_cu->v.quick->symtab
6713 : per_cu->v.psymtab->symtab);
6714}
6715
6716/* A helper function for computing the list of all symbol tables
6717 included by PER_CU. */
6718
6719static void
6720recursively_compute_inclusions (VEC (dwarf2_per_cu_ptr) **result,
6721 htab_t all_children,
6722 struct dwarf2_per_cu_data *per_cu)
6723{
6724 void **slot;
6725 int ix;
6726 struct dwarf2_per_cu_data *iter;
6727
6728 slot = htab_find_slot (all_children, per_cu, INSERT);
6729 if (*slot != NULL)
6730 {
6731 /* This inclusion and its children have been processed. */
6732 return;
6733 }
6734
6735 *slot = per_cu;
6736 /* Only add a CU if it has a symbol table. */
6737 if (get_symtab (per_cu) != NULL)
6738 VEC_safe_push (dwarf2_per_cu_ptr, *result, per_cu);
6739
6740 for (ix = 0;
f4dc4d17 6741 VEC_iterate (dwarf2_per_cu_ptr, per_cu->s.imported_symtabs, ix, iter);
95554aad
TT
6742 ++ix)
6743 recursively_compute_inclusions (result, all_children, iter);
6744}
6745
6746/* Compute the symtab 'includes' fields for the symtab related to
6747 PER_CU. */
6748
6749static void
6750compute_symtab_includes (struct dwarf2_per_cu_data *per_cu)
6751{
f4dc4d17
DE
6752 gdb_assert (! per_cu->is_debug_types);
6753
6754 if (!VEC_empty (dwarf2_per_cu_ptr, per_cu->s.imported_symtabs))
95554aad
TT
6755 {
6756 int ix, len;
6757 struct dwarf2_per_cu_data *iter;
6758 VEC (dwarf2_per_cu_ptr) *result_children = NULL;
6759 htab_t all_children;
6760 struct symtab *symtab = get_symtab (per_cu);
6761
6762 /* If we don't have a symtab, we can just skip this case. */
6763 if (symtab == NULL)
6764 return;
6765
6766 all_children = htab_create_alloc (1, htab_hash_pointer, htab_eq_pointer,
6767 NULL, xcalloc, xfree);
6768
6769 for (ix = 0;
f4dc4d17 6770 VEC_iterate (dwarf2_per_cu_ptr, per_cu->s.imported_symtabs,
95554aad
TT
6771 ix, iter);
6772 ++ix)
6773 recursively_compute_inclusions (&result_children, all_children, iter);
6774
6775 /* Now we have a transitive closure of all the included CUs, so
6776 we can convert it to a list of symtabs. */
6777 len = VEC_length (dwarf2_per_cu_ptr, result_children);
6778 symtab->includes
6779 = obstack_alloc (&dwarf2_per_objfile->objfile->objfile_obstack,
6780 (len + 1) * sizeof (struct symtab *));
6781 for (ix = 0;
6782 VEC_iterate (dwarf2_per_cu_ptr, result_children, ix, iter);
6783 ++ix)
6784 symtab->includes[ix] = get_symtab (iter);
6785 symtab->includes[len] = NULL;
6786
6787 VEC_free (dwarf2_per_cu_ptr, result_children);
6788 htab_delete (all_children);
6789 }
6790}
6791
6792/* Compute the 'includes' field for the symtabs of all the CUs we just
6793 read. */
6794
6795static void
6796process_cu_includes (void)
6797{
6798 int ix;
6799 struct dwarf2_per_cu_data *iter;
6800
6801 for (ix = 0;
6802 VEC_iterate (dwarf2_per_cu_ptr, dwarf2_per_objfile->just_read_cus,
6803 ix, iter);
6804 ++ix)
f4dc4d17
DE
6805 {
6806 if (! iter->is_debug_types)
6807 compute_symtab_includes (iter);
6808 }
95554aad
TT
6809
6810 VEC_free (dwarf2_per_cu_ptr, dwarf2_per_objfile->just_read_cus);
6811}
6812
9cdd5dbd 6813/* Generate full symbol information for PER_CU, whose DIEs have
10b3939b
DJ
6814 already been loaded into memory. */
6815
6816static void
95554aad
TT
6817process_full_comp_unit (struct dwarf2_per_cu_data *per_cu,
6818 enum language pretend_language)
10b3939b 6819{
10b3939b 6820 struct dwarf2_cu *cu = per_cu->cu;
9291a0cd 6821 struct objfile *objfile = per_cu->objfile;
10b3939b
DJ
6822 CORE_ADDR lowpc, highpc;
6823 struct symtab *symtab;
3da10d80 6824 struct cleanup *back_to, *delayed_list_cleanup;
10b3939b 6825 CORE_ADDR baseaddr;
4359dff1 6826 struct block *static_block;
10b3939b
DJ
6827
6828 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
6829
10b3939b
DJ
6830 buildsym_init ();
6831 back_to = make_cleanup (really_free_pendings, NULL);
3da10d80 6832 delayed_list_cleanup = make_cleanup (free_delayed_list, cu);
10b3939b
DJ
6833
6834 cu->list_in_scope = &file_symbols;
c906108c 6835
95554aad
TT
6836 cu->language = pretend_language;
6837 cu->language_defn = language_def (cu->language);
6838
c906108c 6839 /* Do line number decoding in read_file_scope () */
10b3939b 6840 process_die (cu->dies, cu);
c906108c 6841
a766d390
DE
6842 /* For now fudge the Go package. */
6843 if (cu->language == language_go)
6844 fixup_go_packaging (cu);
6845
3da10d80
KS
6846 /* Now that we have processed all the DIEs in the CU, all the types
6847 should be complete, and it should now be safe to compute all of the
6848 physnames. */
6849 compute_delayed_physnames (cu);
6850 do_cleanups (delayed_list_cleanup);
6851
fae299cd
DC
6852 /* Some compilers don't define a DW_AT_high_pc attribute for the
6853 compilation unit. If the DW_AT_high_pc is missing, synthesize
6854 it, by scanning the DIE's below the compilation unit. */
10b3939b 6855 get_scope_pc_bounds (cu->dies, &lowpc, &highpc, cu);
c906108c 6856
36586728
TT
6857 static_block
6858 = end_symtab_get_static_block (highpc + baseaddr, objfile, 0,
6859 per_cu->s.imported_symtabs != NULL);
4359dff1
JK
6860
6861 /* If the comp unit has DW_AT_ranges, it may have discontiguous ranges.
6862 Also, DW_AT_ranges may record ranges not belonging to any child DIEs
6863 (such as virtual method tables). Record the ranges in STATIC_BLOCK's
6864 addrmap to help ensure it has an accurate map of pc values belonging to
6865 this comp unit. */
6866 dwarf2_record_block_ranges (cu->dies, static_block, baseaddr, cu);
6867
6868 symtab = end_symtab_from_static_block (static_block, objfile,
6869 SECT_OFF_TEXT (objfile), 0);
c906108c 6870
8be455d7 6871 if (symtab != NULL)
c906108c 6872 {
df15bd07 6873 int gcc_4_minor = producer_is_gcc_ge_4 (cu->producer);
4632c0d0 6874
8be455d7
JK
6875 /* Set symtab language to language from DW_AT_language. If the
6876 compilation is from a C file generated by language preprocessors, do
6877 not set the language if it was already deduced by start_subfile. */
6878 if (!(cu->language == language_c && symtab->language != language_c))
6879 symtab->language = cu->language;
6880
6881 /* GCC-4.0 has started to support -fvar-tracking. GCC-3.x still can
6882 produce DW_AT_location with location lists but it can be possibly
ab260dad
JK
6883 invalid without -fvar-tracking. Still up to GCC-4.4.x incl. 4.4.0
6884 there were bugs in prologue debug info, fixed later in GCC-4.5
6885 by "unwind info for epilogues" patch (which is not directly related).
8be455d7
JK
6886
6887 For -gdwarf-4 type units LOCATIONS_VALID indication is fortunately not
6888 needed, it would be wrong due to missing DW_AT_producer there.
6889
6890 Still one can confuse GDB by using non-standard GCC compilation
6891 options - this waits on GCC PR other/32998 (-frecord-gcc-switches).
6892 */
ab260dad 6893 if (cu->has_loclist && gcc_4_minor >= 5)
8be455d7 6894 symtab->locations_valid = 1;
e0d00bc7
JK
6895
6896 if (gcc_4_minor >= 5)
6897 symtab->epilogue_unwind_valid = 1;
96408a79
SA
6898
6899 symtab->call_site_htab = cu->call_site_htab;
c906108c 6900 }
9291a0cd
TT
6901
6902 if (dwarf2_per_objfile->using_index)
6903 per_cu->v.quick->symtab = symtab;
6904 else
6905 {
6906 struct partial_symtab *pst = per_cu->v.psymtab;
6907 pst->symtab = symtab;
6908 pst->readin = 1;
6909 }
c906108c 6910
95554aad
TT
6911 /* Push it for inclusion processing later. */
6912 VEC_safe_push (dwarf2_per_cu_ptr, dwarf2_per_objfile->just_read_cus, per_cu);
6913
c906108c 6914 do_cleanups (back_to);
f4dc4d17 6915}
45cfd468 6916
f4dc4d17
DE
6917/* Generate full symbol information for type unit PER_CU, whose DIEs have
6918 already been loaded into memory. */
6919
6920static void
6921process_full_type_unit (struct dwarf2_per_cu_data *per_cu,
6922 enum language pretend_language)
6923{
6924 struct dwarf2_cu *cu = per_cu->cu;
6925 struct objfile *objfile = per_cu->objfile;
6926 struct symtab *symtab;
6927 struct cleanup *back_to, *delayed_list_cleanup;
6928
6929 buildsym_init ();
6930 back_to = make_cleanup (really_free_pendings, NULL);
6931 delayed_list_cleanup = make_cleanup (free_delayed_list, cu);
6932
6933 cu->list_in_scope = &file_symbols;
6934
6935 cu->language = pretend_language;
6936 cu->language_defn = language_def (cu->language);
6937
6938 /* The symbol tables are set up in read_type_unit_scope. */
6939 process_die (cu->dies, cu);
6940
6941 /* For now fudge the Go package. */
6942 if (cu->language == language_go)
6943 fixup_go_packaging (cu);
6944
6945 /* Now that we have processed all the DIEs in the CU, all the types
6946 should be complete, and it should now be safe to compute all of the
6947 physnames. */
6948 compute_delayed_physnames (cu);
6949 do_cleanups (delayed_list_cleanup);
6950
6951 /* TUs share symbol tables.
6952 If this is the first TU to use this symtab, complete the construction
094b34ac
DE
6953 of it with end_expandable_symtab. Otherwise, complete the addition of
6954 this TU's symbols to the existing symtab. */
f4dc4d17 6955 if (per_cu->s.type_unit_group->primary_symtab == NULL)
45cfd468 6956 {
f4dc4d17
DE
6957 symtab = end_expandable_symtab (0, objfile, SECT_OFF_TEXT (objfile));
6958 per_cu->s.type_unit_group->primary_symtab = symtab;
6959
6960 if (symtab != NULL)
6961 {
6962 /* Set symtab language to language from DW_AT_language. If the
6963 compilation is from a C file generated by language preprocessors,
6964 do not set the language if it was already deduced by
6965 start_subfile. */
6966 if (!(cu->language == language_c && symtab->language != language_c))
6967 symtab->language = cu->language;
6968 }
6969 }
6970 else
6971 {
6972 augment_type_symtab (objfile,
6973 per_cu->s.type_unit_group->primary_symtab);
6974 symtab = per_cu->s.type_unit_group->primary_symtab;
6975 }
6976
6977 if (dwarf2_per_objfile->using_index)
6978 per_cu->v.quick->symtab = symtab;
6979 else
6980 {
6981 struct partial_symtab *pst = per_cu->v.psymtab;
6982 pst->symtab = symtab;
6983 pst->readin = 1;
45cfd468 6984 }
f4dc4d17
DE
6985
6986 do_cleanups (back_to);
c906108c
SS
6987}
6988
95554aad
TT
6989/* Process an imported unit DIE. */
6990
6991static void
6992process_imported_unit_die (struct die_info *die, struct dwarf2_cu *cu)
6993{
6994 struct attribute *attr;
6995
f4dc4d17
DE
6996 /* For now we don't handle imported units in type units. */
6997 if (cu->per_cu->is_debug_types)
6998 {
6999 error (_("Dwarf Error: DW_TAG_imported_unit is not"
7000 " supported in type units [in module %s]"),
7001 cu->objfile->name);
7002 }
7003
95554aad
TT
7004 attr = dwarf2_attr (die, DW_AT_import, cu);
7005 if (attr != NULL)
7006 {
7007 struct dwarf2_per_cu_data *per_cu;
7008 struct symtab *imported_symtab;
7009 sect_offset offset;
36586728 7010 int is_dwz;
95554aad
TT
7011
7012 offset = dwarf2_get_ref_die_offset (attr);
36586728
TT
7013 is_dwz = (attr->form == DW_FORM_GNU_ref_alt || cu->per_cu->is_dwz);
7014 per_cu = dwarf2_find_containing_comp_unit (offset, is_dwz, cu->objfile);
95554aad
TT
7015
7016 /* Queue the unit, if needed. */
7017 if (maybe_queue_comp_unit (cu, per_cu, cu->language))
7018 load_full_comp_unit (per_cu, cu->language);
7019
f4dc4d17 7020 VEC_safe_push (dwarf2_per_cu_ptr, cu->per_cu->s.imported_symtabs,
95554aad
TT
7021 per_cu);
7022 }
7023}
7024
c906108c
SS
7025/* Process a die and its children. */
7026
7027static void
e7c27a73 7028process_die (struct die_info *die, struct dwarf2_cu *cu)
c906108c
SS
7029{
7030 switch (die->tag)
7031 {
7032 case DW_TAG_padding:
7033 break;
7034 case DW_TAG_compile_unit:
95554aad 7035 case DW_TAG_partial_unit:
e7c27a73 7036 read_file_scope (die, cu);
c906108c 7037 break;
348e048f
DE
7038 case DW_TAG_type_unit:
7039 read_type_unit_scope (die, cu);
7040 break;
c906108c 7041 case DW_TAG_subprogram:
c906108c 7042 case DW_TAG_inlined_subroutine:
edb3359d 7043 read_func_scope (die, cu);
c906108c
SS
7044 break;
7045 case DW_TAG_lexical_block:
14898363
L
7046 case DW_TAG_try_block:
7047 case DW_TAG_catch_block:
e7c27a73 7048 read_lexical_block_scope (die, cu);
c906108c 7049 break;
96408a79
SA
7050 case DW_TAG_GNU_call_site:
7051 read_call_site_scope (die, cu);
7052 break;
c906108c 7053 case DW_TAG_class_type:
680b30c7 7054 case DW_TAG_interface_type:
c906108c
SS
7055 case DW_TAG_structure_type:
7056 case DW_TAG_union_type:
134d01f1 7057 process_structure_scope (die, cu);
c906108c
SS
7058 break;
7059 case DW_TAG_enumeration_type:
134d01f1 7060 process_enumeration_scope (die, cu);
c906108c 7061 break;
134d01f1 7062
f792889a
DJ
7063 /* These dies have a type, but processing them does not create
7064 a symbol or recurse to process the children. Therefore we can
7065 read them on-demand through read_type_die. */
c906108c 7066 case DW_TAG_subroutine_type:
72019c9c 7067 case DW_TAG_set_type:
c906108c 7068 case DW_TAG_array_type:
c906108c 7069 case DW_TAG_pointer_type:
c906108c 7070 case DW_TAG_ptr_to_member_type:
c906108c 7071 case DW_TAG_reference_type:
c906108c 7072 case DW_TAG_string_type:
c906108c 7073 break;
134d01f1 7074
c906108c 7075 case DW_TAG_base_type:
a02abb62 7076 case DW_TAG_subrange_type:
cb249c71 7077 case DW_TAG_typedef:
134d01f1
DJ
7078 /* Add a typedef symbol for the type definition, if it has a
7079 DW_AT_name. */
f792889a 7080 new_symbol (die, read_type_die (die, cu), cu);
a02abb62 7081 break;
c906108c 7082 case DW_TAG_common_block:
e7c27a73 7083 read_common_block (die, cu);
c906108c
SS
7084 break;
7085 case DW_TAG_common_inclusion:
7086 break;
d9fa45fe 7087 case DW_TAG_namespace:
63d06c5c 7088 processing_has_namespace_info = 1;
e7c27a73 7089 read_namespace (die, cu);
d9fa45fe 7090 break;
5d7cb8df 7091 case DW_TAG_module:
f55ee35c 7092 processing_has_namespace_info = 1;
5d7cb8df
JK
7093 read_module (die, cu);
7094 break;
d9fa45fe
DC
7095 case DW_TAG_imported_declaration:
7096 case DW_TAG_imported_module:
63d06c5c 7097 processing_has_namespace_info = 1;
27aa8d6a
SW
7098 if (die->child != NULL && (die->tag == DW_TAG_imported_declaration
7099 || cu->language != language_fortran))
7100 complaint (&symfile_complaints, _("Tag '%s' has unexpected children"),
7101 dwarf_tag_name (die->tag));
7102 read_import_statement (die, cu);
d9fa45fe 7103 break;
95554aad
TT
7104
7105 case DW_TAG_imported_unit:
7106 process_imported_unit_die (die, cu);
7107 break;
7108
c906108c 7109 default:
e7c27a73 7110 new_symbol (die, NULL, cu);
c906108c
SS
7111 break;
7112 }
7113}
7114
94af9270
KS
7115/* A helper function for dwarf2_compute_name which determines whether DIE
7116 needs to have the name of the scope prepended to the name listed in the
7117 die. */
7118
7119static int
7120die_needs_namespace (struct die_info *die, struct dwarf2_cu *cu)
7121{
1c809c68
TT
7122 struct attribute *attr;
7123
94af9270
KS
7124 switch (die->tag)
7125 {
7126 case DW_TAG_namespace:
7127 case DW_TAG_typedef:
7128 case DW_TAG_class_type:
7129 case DW_TAG_interface_type:
7130 case DW_TAG_structure_type:
7131 case DW_TAG_union_type:
7132 case DW_TAG_enumeration_type:
7133 case DW_TAG_enumerator:
7134 case DW_TAG_subprogram:
7135 case DW_TAG_member:
7136 return 1;
7137
7138 case DW_TAG_variable:
c2b0a229 7139 case DW_TAG_constant:
94af9270
KS
7140 /* We only need to prefix "globally" visible variables. These include
7141 any variable marked with DW_AT_external or any variable that
7142 lives in a namespace. [Variables in anonymous namespaces
7143 require prefixing, but they are not DW_AT_external.] */
7144
7145 if (dwarf2_attr (die, DW_AT_specification, cu))
7146 {
7147 struct dwarf2_cu *spec_cu = cu;
9a619af0 7148
94af9270
KS
7149 return die_needs_namespace (die_specification (die, &spec_cu),
7150 spec_cu);
7151 }
7152
1c809c68 7153 attr = dwarf2_attr (die, DW_AT_external, cu);
f55ee35c
JK
7154 if (attr == NULL && die->parent->tag != DW_TAG_namespace
7155 && die->parent->tag != DW_TAG_module)
1c809c68
TT
7156 return 0;
7157 /* A variable in a lexical block of some kind does not need a
7158 namespace, even though in C++ such variables may be external
7159 and have a mangled name. */
7160 if (die->parent->tag == DW_TAG_lexical_block
7161 || die->parent->tag == DW_TAG_try_block
1054b214
TT
7162 || die->parent->tag == DW_TAG_catch_block
7163 || die->parent->tag == DW_TAG_subprogram)
1c809c68
TT
7164 return 0;
7165 return 1;
94af9270
KS
7166
7167 default:
7168 return 0;
7169 }
7170}
7171
98bfdba5
PA
7172/* Retrieve the last character from a mem_file. */
7173
7174static void
7175do_ui_file_peek_last (void *object, const char *buffer, long length)
7176{
7177 char *last_char_p = (char *) object;
7178
7179 if (length > 0)
7180 *last_char_p = buffer[length - 1];
7181}
7182
94af9270 7183/* Compute the fully qualified name of DIE in CU. If PHYSNAME is nonzero,
a766d390
DE
7184 compute the physname for the object, which include a method's:
7185 - formal parameters (C++/Java),
7186 - receiver type (Go),
7187 - return type (Java).
7188
7189 The term "physname" is a bit confusing.
7190 For C++, for example, it is the demangled name.
7191 For Go, for example, it's the mangled name.
94af9270 7192
af6b7be1
JB
7193 For Ada, return the DIE's linkage name rather than the fully qualified
7194 name. PHYSNAME is ignored..
7195
94af9270
KS
7196 The result is allocated on the objfile_obstack and canonicalized. */
7197
7198static const char *
7199dwarf2_compute_name (char *name, struct die_info *die, struct dwarf2_cu *cu,
7200 int physname)
7201{
bb5ed363
DE
7202 struct objfile *objfile = cu->objfile;
7203
94af9270
KS
7204 if (name == NULL)
7205 name = dwarf2_name (die, cu);
7206
f55ee35c
JK
7207 /* For Fortran GDB prefers DW_AT_*linkage_name if present but otherwise
7208 compute it by typename_concat inside GDB. */
7209 if (cu->language == language_ada
7210 || (cu->language == language_fortran && physname))
7211 {
7212 /* For Ada unit, we prefer the linkage name over the name, as
7213 the former contains the exported name, which the user expects
7214 to be able to reference. Ideally, we want the user to be able
7215 to reference this entity using either natural or linkage name,
7216 but we haven't started looking at this enhancement yet. */
7217 struct attribute *attr;
7218
7219 attr = dwarf2_attr (die, DW_AT_linkage_name, cu);
7220 if (attr == NULL)
7221 attr = dwarf2_attr (die, DW_AT_MIPS_linkage_name, cu);
7222 if (attr && DW_STRING (attr))
7223 return DW_STRING (attr);
7224 }
7225
94af9270
KS
7226 /* These are the only languages we know how to qualify names in. */
7227 if (name != NULL
f55ee35c
JK
7228 && (cu->language == language_cplus || cu->language == language_java
7229 || cu->language == language_fortran))
94af9270
KS
7230 {
7231 if (die_needs_namespace (die, cu))
7232 {
7233 long length;
0d5cff50 7234 const char *prefix;
94af9270
KS
7235 struct ui_file *buf;
7236
7237 prefix = determine_prefix (die, cu);
7238 buf = mem_fileopen ();
7239 if (*prefix != '\0')
7240 {
f55ee35c
JK
7241 char *prefixed_name = typename_concat (NULL, prefix, name,
7242 physname, cu);
9a619af0 7243
94af9270
KS
7244 fputs_unfiltered (prefixed_name, buf);
7245 xfree (prefixed_name);
7246 }
7247 else
62d5b8da 7248 fputs_unfiltered (name, buf);
94af9270 7249
98bfdba5
PA
7250 /* Template parameters may be specified in the DIE's DW_AT_name, or
7251 as children with DW_TAG_template_type_param or
7252 DW_TAG_value_type_param. If the latter, add them to the name
7253 here. If the name already has template parameters, then
7254 skip this step; some versions of GCC emit both, and
7255 it is more efficient to use the pre-computed name.
7256
7257 Something to keep in mind about this process: it is very
7258 unlikely, or in some cases downright impossible, to produce
7259 something that will match the mangled name of a function.
7260 If the definition of the function has the same debug info,
7261 we should be able to match up with it anyway. But fallbacks
7262 using the minimal symbol, for instance to find a method
7263 implemented in a stripped copy of libstdc++, will not work.
7264 If we do not have debug info for the definition, we will have to
7265 match them up some other way.
7266
7267 When we do name matching there is a related problem with function
7268 templates; two instantiated function templates are allowed to
7269 differ only by their return types, which we do not add here. */
7270
7271 if (cu->language == language_cplus && strchr (name, '<') == NULL)
7272 {
7273 struct attribute *attr;
7274 struct die_info *child;
7275 int first = 1;
7276
7277 die->building_fullname = 1;
7278
7279 for (child = die->child; child != NULL; child = child->sibling)
7280 {
7281 struct type *type;
12df843f 7282 LONGEST value;
98bfdba5
PA
7283 gdb_byte *bytes;
7284 struct dwarf2_locexpr_baton *baton;
7285 struct value *v;
7286
7287 if (child->tag != DW_TAG_template_type_param
7288 && child->tag != DW_TAG_template_value_param)
7289 continue;
7290
7291 if (first)
7292 {
7293 fputs_unfiltered ("<", buf);
7294 first = 0;
7295 }
7296 else
7297 fputs_unfiltered (", ", buf);
7298
7299 attr = dwarf2_attr (child, DW_AT_type, cu);
7300 if (attr == NULL)
7301 {
7302 complaint (&symfile_complaints,
7303 _("template parameter missing DW_AT_type"));
7304 fputs_unfiltered ("UNKNOWN_TYPE", buf);
7305 continue;
7306 }
7307 type = die_type (child, cu);
7308
7309 if (child->tag == DW_TAG_template_type_param)
7310 {
7311 c_print_type (type, "", buf, -1, 0);
7312 continue;
7313 }
7314
7315 attr = dwarf2_attr (child, DW_AT_const_value, cu);
7316 if (attr == NULL)
7317 {
7318 complaint (&symfile_complaints,
3e43a32a
MS
7319 _("template parameter missing "
7320 "DW_AT_const_value"));
98bfdba5
PA
7321 fputs_unfiltered ("UNKNOWN_VALUE", buf);
7322 continue;
7323 }
7324
7325 dwarf2_const_value_attr (attr, type, name,
7326 &cu->comp_unit_obstack, cu,
7327 &value, &bytes, &baton);
7328
7329 if (TYPE_NOSIGN (type))
7330 /* GDB prints characters as NUMBER 'CHAR'. If that's
7331 changed, this can use value_print instead. */
7332 c_printchar (value, type, buf);
7333 else
7334 {
7335 struct value_print_options opts;
7336
7337 if (baton != NULL)
7338 v = dwarf2_evaluate_loc_desc (type, NULL,
7339 baton->data,
7340 baton->size,
7341 baton->per_cu);
7342 else if (bytes != NULL)
7343 {
7344 v = allocate_value (type);
7345 memcpy (value_contents_writeable (v), bytes,
7346 TYPE_LENGTH (type));
7347 }
7348 else
7349 v = value_from_longest (type, value);
7350
3e43a32a
MS
7351 /* Specify decimal so that we do not depend on
7352 the radix. */
98bfdba5
PA
7353 get_formatted_print_options (&opts, 'd');
7354 opts.raw = 1;
7355 value_print (v, buf, &opts);
7356 release_value (v);
7357 value_free (v);
7358 }
7359 }
7360
7361 die->building_fullname = 0;
7362
7363 if (!first)
7364 {
7365 /* Close the argument list, with a space if necessary
7366 (nested templates). */
7367 char last_char = '\0';
7368 ui_file_put (buf, do_ui_file_peek_last, &last_char);
7369 if (last_char == '>')
7370 fputs_unfiltered (" >", buf);
7371 else
7372 fputs_unfiltered (">", buf);
7373 }
7374 }
7375
94af9270
KS
7376 /* For Java and C++ methods, append formal parameter type
7377 information, if PHYSNAME. */
6e70227d 7378
94af9270
KS
7379 if (physname && die->tag == DW_TAG_subprogram
7380 && (cu->language == language_cplus
7381 || cu->language == language_java))
7382 {
7383 struct type *type = read_type_die (die, cu);
7384
3167638f 7385 c_type_print_args (type, buf, 1, cu->language);
94af9270
KS
7386
7387 if (cu->language == language_java)
7388 {
7389 /* For java, we must append the return type to method
0963b4bd 7390 names. */
94af9270
KS
7391 if (die->tag == DW_TAG_subprogram)
7392 java_print_type (TYPE_TARGET_TYPE (type), "", buf,
7393 0, 0);
7394 }
7395 else if (cu->language == language_cplus)
7396 {
60430eff
DJ
7397 /* Assume that an artificial first parameter is
7398 "this", but do not crash if it is not. RealView
7399 marks unnamed (and thus unused) parameters as
7400 artificial; there is no way to differentiate
7401 the two cases. */
94af9270
KS
7402 if (TYPE_NFIELDS (type) > 0
7403 && TYPE_FIELD_ARTIFICIAL (type, 0)
60430eff 7404 && TYPE_CODE (TYPE_FIELD_TYPE (type, 0)) == TYPE_CODE_PTR
3e43a32a
MS
7405 && TYPE_CONST (TYPE_TARGET_TYPE (TYPE_FIELD_TYPE (type,
7406 0))))
94af9270
KS
7407 fputs_unfiltered (" const", buf);
7408 }
7409 }
7410
bb5ed363 7411 name = ui_file_obsavestring (buf, &objfile->objfile_obstack,
94af9270
KS
7412 &length);
7413 ui_file_delete (buf);
7414
7415 if (cu->language == language_cplus)
7416 {
7417 char *cname
7418 = dwarf2_canonicalize_name (name, cu,
bb5ed363 7419 &objfile->objfile_obstack);
9a619af0 7420
94af9270
KS
7421 if (cname != NULL)
7422 name = cname;
7423 }
7424 }
7425 }
7426
7427 return name;
7428}
7429
0114d602
DJ
7430/* Return the fully qualified name of DIE, based on its DW_AT_name.
7431 If scope qualifiers are appropriate they will be added. The result
7432 will be allocated on the objfile_obstack, or NULL if the DIE does
94af9270
KS
7433 not have a name. NAME may either be from a previous call to
7434 dwarf2_name or NULL.
7435
0963b4bd 7436 The output string will be canonicalized (if C++/Java). */
0114d602
DJ
7437
7438static const char *
94af9270 7439dwarf2_full_name (char *name, struct die_info *die, struct dwarf2_cu *cu)
0114d602 7440{
94af9270
KS
7441 return dwarf2_compute_name (name, die, cu, 0);
7442}
0114d602 7443
94af9270
KS
7444/* Construct a physname for the given DIE in CU. NAME may either be
7445 from a previous call to dwarf2_name or NULL. The result will be
7446 allocated on the objfile_objstack or NULL if the DIE does not have a
7447 name.
0114d602 7448
94af9270 7449 The output string will be canonicalized (if C++/Java). */
0114d602 7450
94af9270
KS
7451static const char *
7452dwarf2_physname (char *name, struct die_info *die, struct dwarf2_cu *cu)
7453{
bb5ed363 7454 struct objfile *objfile = cu->objfile;
900e11f9
JK
7455 struct attribute *attr;
7456 const char *retval, *mangled = NULL, *canon = NULL;
7457 struct cleanup *back_to;
7458 int need_copy = 1;
7459
7460 /* In this case dwarf2_compute_name is just a shortcut not building anything
7461 on its own. */
7462 if (!die_needs_namespace (die, cu))
7463 return dwarf2_compute_name (name, die, cu, 1);
7464
7465 back_to = make_cleanup (null_cleanup, NULL);
7466
7467 attr = dwarf2_attr (die, DW_AT_linkage_name, cu);
7468 if (!attr)
7469 attr = dwarf2_attr (die, DW_AT_MIPS_linkage_name, cu);
7470
7471 /* DW_AT_linkage_name is missing in some cases - depend on what GDB
7472 has computed. */
7473 if (attr && DW_STRING (attr))
7474 {
7475 char *demangled;
7476
7477 mangled = DW_STRING (attr);
7478
7479 /* Use DMGL_RET_DROP for C++ template functions to suppress their return
7480 type. It is easier for GDB users to search for such functions as
7481 `name(params)' than `long name(params)'. In such case the minimal
7482 symbol names do not match the full symbol names but for template
7483 functions there is never a need to look up their definition from their
7484 declaration so the only disadvantage remains the minimal symbol
7485 variant `long name(params)' does not have the proper inferior type.
7486 */
7487
a766d390
DE
7488 if (cu->language == language_go)
7489 {
7490 /* This is a lie, but we already lie to the caller new_symbol_full.
7491 new_symbol_full assumes we return the mangled name.
7492 This just undoes that lie until things are cleaned up. */
7493 demangled = NULL;
7494 }
7495 else
7496 {
7497 demangled = cplus_demangle (mangled,
7498 (DMGL_PARAMS | DMGL_ANSI
7499 | (cu->language == language_java
7500 ? DMGL_JAVA | DMGL_RET_POSTFIX
7501 : DMGL_RET_DROP)));
7502 }
900e11f9
JK
7503 if (demangled)
7504 {
7505 make_cleanup (xfree, demangled);
7506 canon = demangled;
7507 }
7508 else
7509 {
7510 canon = mangled;
7511 need_copy = 0;
7512 }
7513 }
7514
7515 if (canon == NULL || check_physname)
7516 {
7517 const char *physname = dwarf2_compute_name (name, die, cu, 1);
7518
7519 if (canon != NULL && strcmp (physname, canon) != 0)
7520 {
7521 /* It may not mean a bug in GDB. The compiler could also
7522 compute DW_AT_linkage_name incorrectly. But in such case
7523 GDB would need to be bug-to-bug compatible. */
7524
7525 complaint (&symfile_complaints,
7526 _("Computed physname <%s> does not match demangled <%s> "
7527 "(from linkage <%s>) - DIE at 0x%x [in module %s]"),
b64f50a1 7528 physname, canon, mangled, die->offset.sect_off, objfile->name);
900e11f9
JK
7529
7530 /* Prefer DW_AT_linkage_name (in the CANON form) - when it
7531 is available here - over computed PHYSNAME. It is safer
7532 against both buggy GDB and buggy compilers. */
7533
7534 retval = canon;
7535 }
7536 else
7537 {
7538 retval = physname;
7539 need_copy = 0;
7540 }
7541 }
7542 else
7543 retval = canon;
7544
7545 if (need_copy)
7546 retval = obsavestring (retval, strlen (retval),
bb5ed363 7547 &objfile->objfile_obstack);
900e11f9
JK
7548
7549 do_cleanups (back_to);
7550 return retval;
0114d602
DJ
7551}
7552
27aa8d6a
SW
7553/* Read the import statement specified by the given die and record it. */
7554
7555static void
7556read_import_statement (struct die_info *die, struct dwarf2_cu *cu)
7557{
bb5ed363 7558 struct objfile *objfile = cu->objfile;
27aa8d6a 7559 struct attribute *import_attr;
32019081 7560 struct die_info *imported_die, *child_die;
de4affc9 7561 struct dwarf2_cu *imported_cu;
27aa8d6a 7562 const char *imported_name;
794684b6 7563 const char *imported_name_prefix;
13387711
SW
7564 const char *canonical_name;
7565 const char *import_alias;
7566 const char *imported_declaration = NULL;
794684b6 7567 const char *import_prefix;
32019081
JK
7568 VEC (const_char_ptr) *excludes = NULL;
7569 struct cleanup *cleanups;
13387711
SW
7570
7571 char *temp;
27aa8d6a
SW
7572
7573 import_attr = dwarf2_attr (die, DW_AT_import, cu);
7574 if (import_attr == NULL)
7575 {
7576 complaint (&symfile_complaints, _("Tag '%s' has no DW_AT_import"),
7577 dwarf_tag_name (die->tag));
7578 return;
7579 }
7580
de4affc9
CC
7581 imported_cu = cu;
7582 imported_die = follow_die_ref_or_sig (die, import_attr, &imported_cu);
7583 imported_name = dwarf2_name (imported_die, imported_cu);
27aa8d6a
SW
7584 if (imported_name == NULL)
7585 {
7586 /* GCC bug: https://bugzilla.redhat.com/show_bug.cgi?id=506524
7587
7588 The import in the following code:
7589 namespace A
7590 {
7591 typedef int B;
7592 }
7593
7594 int main ()
7595 {
7596 using A::B;
7597 B b;
7598 return b;
7599 }
7600
7601 ...
7602 <2><51>: Abbrev Number: 3 (DW_TAG_imported_declaration)
7603 <52> DW_AT_decl_file : 1
7604 <53> DW_AT_decl_line : 6
7605 <54> DW_AT_import : <0x75>
7606 <2><58>: Abbrev Number: 4 (DW_TAG_typedef)
7607 <59> DW_AT_name : B
7608 <5b> DW_AT_decl_file : 1
7609 <5c> DW_AT_decl_line : 2
7610 <5d> DW_AT_type : <0x6e>
7611 ...
7612 <1><75>: Abbrev Number: 7 (DW_TAG_base_type)
7613 <76> DW_AT_byte_size : 4
7614 <77> DW_AT_encoding : 5 (signed)
7615
7616 imports the wrong die ( 0x75 instead of 0x58 ).
7617 This case will be ignored until the gcc bug is fixed. */
7618 return;
7619 }
7620
82856980
SW
7621 /* Figure out the local name after import. */
7622 import_alias = dwarf2_name (die, cu);
27aa8d6a 7623
794684b6
SW
7624 /* Figure out where the statement is being imported to. */
7625 import_prefix = determine_prefix (die, cu);
7626
7627 /* Figure out what the scope of the imported die is and prepend it
7628 to the name of the imported die. */
de4affc9 7629 imported_name_prefix = determine_prefix (imported_die, imported_cu);
794684b6 7630
f55ee35c
JK
7631 if (imported_die->tag != DW_TAG_namespace
7632 && imported_die->tag != DW_TAG_module)
794684b6 7633 {
13387711
SW
7634 imported_declaration = imported_name;
7635 canonical_name = imported_name_prefix;
794684b6 7636 }
13387711 7637 else if (strlen (imported_name_prefix) > 0)
794684b6 7638 {
13387711
SW
7639 temp = alloca (strlen (imported_name_prefix)
7640 + 2 + strlen (imported_name) + 1);
7641 strcpy (temp, imported_name_prefix);
7642 strcat (temp, "::");
7643 strcat (temp, imported_name);
7644 canonical_name = temp;
794684b6 7645 }
13387711
SW
7646 else
7647 canonical_name = imported_name;
794684b6 7648
32019081
JK
7649 cleanups = make_cleanup (VEC_cleanup (const_char_ptr), &excludes);
7650
7651 if (die->tag == DW_TAG_imported_module && cu->language == language_fortran)
7652 for (child_die = die->child; child_die && child_die->tag;
7653 child_die = sibling_die (child_die))
7654 {
7655 /* DWARF-4: A Fortran use statement with a “rename list” may be
7656 represented by an imported module entry with an import attribute
7657 referring to the module and owned entries corresponding to those
7658 entities that are renamed as part of being imported. */
7659
7660 if (child_die->tag != DW_TAG_imported_declaration)
7661 {
7662 complaint (&symfile_complaints,
7663 _("child DW_TAG_imported_declaration expected "
7664 "- DIE at 0x%x [in module %s]"),
b64f50a1 7665 child_die->offset.sect_off, objfile->name);
32019081
JK
7666 continue;
7667 }
7668
7669 import_attr = dwarf2_attr (child_die, DW_AT_import, cu);
7670 if (import_attr == NULL)
7671 {
7672 complaint (&symfile_complaints, _("Tag '%s' has no DW_AT_import"),
7673 dwarf_tag_name (child_die->tag));
7674 continue;
7675 }
7676
7677 imported_cu = cu;
7678 imported_die = follow_die_ref_or_sig (child_die, import_attr,
7679 &imported_cu);
7680 imported_name = dwarf2_name (imported_die, imported_cu);
7681 if (imported_name == NULL)
7682 {
7683 complaint (&symfile_complaints,
7684 _("child DW_TAG_imported_declaration has unknown "
7685 "imported name - DIE at 0x%x [in module %s]"),
b64f50a1 7686 child_die->offset.sect_off, objfile->name);
32019081
JK
7687 continue;
7688 }
7689
7690 VEC_safe_push (const_char_ptr, excludes, imported_name);
7691
7692 process_die (child_die, cu);
7693 }
7694
c0cc3a76
SW
7695 cp_add_using_directive (import_prefix,
7696 canonical_name,
7697 import_alias,
13387711 7698 imported_declaration,
32019081 7699 excludes,
bb5ed363 7700 &objfile->objfile_obstack);
32019081
JK
7701
7702 do_cleanups (cleanups);
27aa8d6a
SW
7703}
7704
f4dc4d17 7705/* Cleanup function for handle_DW_AT_stmt_list. */
ae2de4f8 7706
cb1df416
DJ
7707static void
7708free_cu_line_header (void *arg)
7709{
7710 struct dwarf2_cu *cu = arg;
7711
7712 free_line_header (cu->line_header);
7713 cu->line_header = NULL;
7714}
7715
9291a0cd
TT
7716static void
7717find_file_and_directory (struct die_info *die, struct dwarf2_cu *cu,
7718 char **name, char **comp_dir)
7719{
7720 struct attribute *attr;
7721
7722 *name = NULL;
7723 *comp_dir = NULL;
7724
7725 /* Find the filename. Do not use dwarf2_name here, since the filename
7726 is not a source language identifier. */
7727 attr = dwarf2_attr (die, DW_AT_name, cu);
7728 if (attr)
7729 {
7730 *name = DW_STRING (attr);
7731 }
7732
7733 attr = dwarf2_attr (die, DW_AT_comp_dir, cu);
7734 if (attr)
7735 *comp_dir = DW_STRING (attr);
7736 else if (*name != NULL && IS_ABSOLUTE_PATH (*name))
7737 {
7738 *comp_dir = ldirname (*name);
7739 if (*comp_dir != NULL)
7740 make_cleanup (xfree, *comp_dir);
7741 }
7742 if (*comp_dir != NULL)
7743 {
7744 /* Irix 6.2 native cc prepends <machine>.: to the compilation
7745 directory, get rid of it. */
7746 char *cp = strchr (*comp_dir, ':');
7747
7748 if (cp && cp != *comp_dir && cp[-1] == '.' && cp[1] == '/')
7749 *comp_dir = cp + 1;
7750 }
7751
7752 if (*name == NULL)
7753 *name = "<unknown>";
7754}
7755
f4dc4d17
DE
7756/* Handle DW_AT_stmt_list for a compilation unit.
7757 DIE is the DW_TAG_compile_unit die for CU.
f3f5162e
DE
7758 COMP_DIR is the compilation directory.
7759 WANT_LINE_INFO is non-zero if the pc/line-number mapping is needed. */
2ab95328
TT
7760
7761static void
7762handle_DW_AT_stmt_list (struct die_info *die, struct dwarf2_cu *cu,
f4dc4d17 7763 const char *comp_dir)
2ab95328
TT
7764{
7765 struct attribute *attr;
2ab95328 7766
f4dc4d17
DE
7767 gdb_assert (! cu->per_cu->is_debug_types);
7768
2ab95328
TT
7769 attr = dwarf2_attr (die, DW_AT_stmt_list, cu);
7770 if (attr)
7771 {
7772 unsigned int line_offset = DW_UNSND (attr);
7773 struct line_header *line_header
3019eac3 7774 = dwarf_decode_line_header (line_offset, cu);
2ab95328
TT
7775
7776 if (line_header)
dee91e82
DE
7777 {
7778 cu->line_header = line_header;
7779 make_cleanup (free_cu_line_header, cu);
f4dc4d17 7780 dwarf_decode_lines (line_header, comp_dir, cu, NULL, 1);
dee91e82 7781 }
2ab95328
TT
7782 }
7783}
7784
95554aad 7785/* Process DW_TAG_compile_unit or DW_TAG_partial_unit. */
ae2de4f8 7786
c906108c 7787static void
e7c27a73 7788read_file_scope (struct die_info *die, struct dwarf2_cu *cu)
c906108c 7789{
dee91e82 7790 struct objfile *objfile = dwarf2_per_objfile->objfile;
debd256d 7791 struct cleanup *back_to = make_cleanup (null_cleanup, 0);
2acceee2 7792 CORE_ADDR lowpc = ((CORE_ADDR) -1);
c906108c
SS
7793 CORE_ADDR highpc = ((CORE_ADDR) 0);
7794 struct attribute *attr;
e1024ff1 7795 char *name = NULL;
c906108c
SS
7796 char *comp_dir = NULL;
7797 struct die_info *child_die;
7798 bfd *abfd = objfile->obfd;
e142c38c 7799 CORE_ADDR baseaddr;
6e70227d 7800
e142c38c 7801 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
c906108c 7802
fae299cd 7803 get_scope_pc_bounds (die, &lowpc, &highpc, cu);
c906108c
SS
7804
7805 /* If we didn't find a lowpc, set it to highpc to avoid complaints
7806 from finish_block. */
2acceee2 7807 if (lowpc == ((CORE_ADDR) -1))
c906108c
SS
7808 lowpc = highpc;
7809 lowpc += baseaddr;
7810 highpc += baseaddr;
7811
9291a0cd 7812 find_file_and_directory (die, cu, &name, &comp_dir);
e1024ff1 7813
95554aad 7814 prepare_one_comp_unit (cu, die, cu->language);
303b6f5d 7815
f4b8a18d
KW
7816 /* The XLCL doesn't generate DW_LANG_OpenCL because this attribute is not
7817 standardised yet. As a workaround for the language detection we fall
7818 back to the DW_AT_producer string. */
7819 if (cu->producer && strstr (cu->producer, "IBM XL C for OpenCL") != NULL)
7820 cu->language = language_opencl;
7821
3019eac3
DE
7822 /* Similar hack for Go. */
7823 if (cu->producer && strstr (cu->producer, "GNU Go ") != NULL)
7824 set_cu_language (DW_LANG_Go, cu);
7825
f4dc4d17 7826 dwarf2_start_symtab (cu, name, comp_dir, lowpc);
3019eac3
DE
7827
7828 /* Decode line number information if present. We do this before
7829 processing child DIEs, so that the line header table is available
7830 for DW_AT_decl_file. */
f4dc4d17 7831 handle_DW_AT_stmt_list (die, cu, comp_dir);
3019eac3
DE
7832
7833 /* Process all dies in compilation unit. */
7834 if (die->child != NULL)
7835 {
7836 child_die = die->child;
7837 while (child_die && child_die->tag)
7838 {
7839 process_die (child_die, cu);
7840 child_die = sibling_die (child_die);
7841 }
7842 }
7843
7844 /* Decode macro information, if present. Dwarf 2 macro information
7845 refers to information in the line number info statement program
7846 header, so we can only read it if we've read the header
7847 successfully. */
7848 attr = dwarf2_attr (die, DW_AT_GNU_macros, cu);
7849 if (attr && cu->line_header)
7850 {
7851 if (dwarf2_attr (die, DW_AT_macro_info, cu))
7852 complaint (&symfile_complaints,
7853 _("CU refers to both DW_AT_GNU_macros and DW_AT_macro_info"));
7854
09262596 7855 dwarf_decode_macros (cu, DW_UNSND (attr), comp_dir, 1);
3019eac3
DE
7856 }
7857 else
7858 {
7859 attr = dwarf2_attr (die, DW_AT_macro_info, cu);
7860 if (attr && cu->line_header)
7861 {
7862 unsigned int macro_offset = DW_UNSND (attr);
7863
09262596 7864 dwarf_decode_macros (cu, macro_offset, comp_dir, 0);
3019eac3
DE
7865 }
7866 }
7867
7868 do_cleanups (back_to);
7869}
7870
f4dc4d17
DE
7871/* TU version of handle_DW_AT_stmt_list for read_type_unit_scope.
7872 Create the set of symtabs used by this TU, or if this TU is sharing
7873 symtabs with another TU and the symtabs have already been created
7874 then restore those symtabs in the line header.
7875 We don't need the pc/line-number mapping for type units. */
3019eac3
DE
7876
7877static void
f4dc4d17 7878setup_type_unit_groups (struct die_info *die, struct dwarf2_cu *cu)
3019eac3 7879{
f4dc4d17
DE
7880 struct objfile *objfile = dwarf2_per_objfile->objfile;
7881 struct dwarf2_per_cu_data *per_cu = cu->per_cu;
7882 struct type_unit_group *tu_group;
7883 int first_time;
7884 struct line_header *lh;
3019eac3 7885 struct attribute *attr;
f4dc4d17 7886 unsigned int i, line_offset;
3019eac3 7887
f4dc4d17 7888 gdb_assert (per_cu->is_debug_types);
3019eac3 7889
f4dc4d17 7890 attr = dwarf2_attr (die, DW_AT_stmt_list, cu);
3019eac3 7891
f4dc4d17
DE
7892 /* If we're using .gdb_index (includes -readnow) then
7893 per_cu->s.type_unit_group may not have been set up yet. */
7894 if (per_cu->s.type_unit_group == NULL)
094b34ac 7895 per_cu->s.type_unit_group = get_type_unit_group (cu, attr);
f4dc4d17
DE
7896 tu_group = per_cu->s.type_unit_group;
7897
7898 /* If we've already processed this stmt_list there's no real need to
7899 do it again, we could fake it and just recreate the part we need
7900 (file name,index -> symtab mapping). If data shows this optimization
7901 is useful we can do it then. */
7902 first_time = tu_group->primary_symtab == NULL;
7903
7904 /* We have to handle the case of both a missing DW_AT_stmt_list or bad
7905 debug info. */
7906 lh = NULL;
7907 if (attr != NULL)
3019eac3 7908 {
f4dc4d17
DE
7909 line_offset = DW_UNSND (attr);
7910 lh = dwarf_decode_line_header (line_offset, cu);
7911 }
7912 if (lh == NULL)
7913 {
7914 if (first_time)
7915 dwarf2_start_symtab (cu, "", NULL, 0);
7916 else
7917 {
7918 gdb_assert (tu_group->symtabs == NULL);
7919 restart_symtab (0);
7920 }
7921 /* Note: The primary symtab will get allocated at the end. */
7922 return;
3019eac3
DE
7923 }
7924
f4dc4d17
DE
7925 cu->line_header = lh;
7926 make_cleanup (free_cu_line_header, cu);
3019eac3 7927
f4dc4d17
DE
7928 if (first_time)
7929 {
7930 dwarf2_start_symtab (cu, "", NULL, 0);
3019eac3 7931
f4dc4d17
DE
7932 tu_group->num_symtabs = lh->num_file_names;
7933 tu_group->symtabs = XNEWVEC (struct symtab *, lh->num_file_names);
3019eac3 7934
f4dc4d17
DE
7935 for (i = 0; i < lh->num_file_names; ++i)
7936 {
7937 char *dir = NULL;
7938 struct file_entry *fe = &lh->file_names[i];
3019eac3 7939
f4dc4d17
DE
7940 if (fe->dir_index)
7941 dir = lh->include_dirs[fe->dir_index - 1];
7942 dwarf2_start_subfile (fe->name, dir, NULL);
3019eac3 7943
f4dc4d17
DE
7944 /* Note: We don't have to watch for the main subfile here, type units
7945 don't have DW_AT_name. */
3019eac3 7946
f4dc4d17
DE
7947 if (current_subfile->symtab == NULL)
7948 {
7949 /* NOTE: start_subfile will recognize when it's been passed
7950 a file it has already seen. So we can't assume there's a
7951 simple mapping from lh->file_names to subfiles,
7952 lh->file_names may contain dups. */
7953 current_subfile->symtab = allocate_symtab (current_subfile->name,
7954 objfile);
7955 }
7956
7957 fe->symtab = current_subfile->symtab;
7958 tu_group->symtabs[i] = fe->symtab;
7959 }
7960 }
7961 else
3019eac3 7962 {
f4dc4d17
DE
7963 restart_symtab (0);
7964
7965 for (i = 0; i < lh->num_file_names; ++i)
7966 {
7967 struct file_entry *fe = &lh->file_names[i];
7968
7969 fe->symtab = tu_group->symtabs[i];
7970 }
3019eac3
DE
7971 }
7972
f4dc4d17
DE
7973 /* The main symtab is allocated last. Type units don't have DW_AT_name
7974 so they don't have a "real" (so to speak) symtab anyway.
7975 There is later code that will assign the main symtab to all symbols
7976 that don't have one. We need to handle the case of a symbol with a
7977 missing symtab (DW_AT_decl_file) anyway. */
7978}
3019eac3 7979
f4dc4d17
DE
7980/* Process DW_TAG_type_unit.
7981 For TUs we want to skip the first top level sibling if it's not the
7982 actual type being defined by this TU. In this case the first top
7983 level sibling is there to provide context only. */
3019eac3 7984
f4dc4d17
DE
7985static void
7986read_type_unit_scope (struct die_info *die, struct dwarf2_cu *cu)
7987{
7988 struct die_info *child_die;
3019eac3 7989
f4dc4d17
DE
7990 prepare_one_comp_unit (cu, die, language_minimal);
7991
7992 /* Initialize (or reinitialize) the machinery for building symtabs.
7993 We do this before processing child DIEs, so that the line header table
7994 is available for DW_AT_decl_file. */
7995 setup_type_unit_groups (die, cu);
7996
7997 if (die->child != NULL)
7998 {
7999 child_die = die->child;
8000 while (child_die && child_die->tag)
8001 {
8002 process_die (child_die, cu);
8003 child_die = sibling_die (child_die);
8004 }
8005 }
3019eac3
DE
8006}
8007\f
8008/* DWO files. */
8009
8010static hashval_t
8011hash_dwo_file (const void *item)
8012{
8013 const struct dwo_file *dwo_file = item;
8014
8015 return htab_hash_string (dwo_file->dwo_name);
8016}
8017
8018static int
8019eq_dwo_file (const void *item_lhs, const void *item_rhs)
8020{
8021 const struct dwo_file *lhs = item_lhs;
8022 const struct dwo_file *rhs = item_rhs;
8023
8024 return strcmp (lhs->dwo_name, rhs->dwo_name) == 0;
8025}
8026
8027/* Allocate a hash table for DWO files. */
8028
8029static htab_t
8030allocate_dwo_file_hash_table (void)
8031{
8032 struct objfile *objfile = dwarf2_per_objfile->objfile;
8033
8034 return htab_create_alloc_ex (41,
8035 hash_dwo_file,
8036 eq_dwo_file,
8037 NULL,
8038 &objfile->objfile_obstack,
8039 hashtab_obstack_allocate,
8040 dummy_obstack_deallocate);
8041}
8042
8043static hashval_t
8044hash_dwo_unit (const void *item)
8045{
8046 const struct dwo_unit *dwo_unit = item;
8047
8048 /* This drops the top 32 bits of the id, but is ok for a hash. */
8049 return dwo_unit->signature;
8050}
8051
8052static int
8053eq_dwo_unit (const void *item_lhs, const void *item_rhs)
8054{
8055 const struct dwo_unit *lhs = item_lhs;
8056 const struct dwo_unit *rhs = item_rhs;
8057
8058 /* The signature is assumed to be unique within the DWO file.
8059 So while object file CU dwo_id's always have the value zero,
8060 that's OK, assuming each object file DWO file has only one CU,
8061 and that's the rule for now. */
8062 return lhs->signature == rhs->signature;
8063}
8064
8065/* Allocate a hash table for DWO CUs,TUs.
8066 There is one of these tables for each of CUs,TUs for each DWO file. */
8067
8068static htab_t
8069allocate_dwo_unit_table (struct objfile *objfile)
8070{
8071 /* Start out with a pretty small number.
8072 Generally DWO files contain only one CU and maybe some TUs. */
8073 return htab_create_alloc_ex (3,
8074 hash_dwo_unit,
8075 eq_dwo_unit,
8076 NULL,
8077 &objfile->objfile_obstack,
8078 hashtab_obstack_allocate,
8079 dummy_obstack_deallocate);
8080}
8081
8082/* This function is mapped across the sections and remembers the offset and
8083 size of each of the DWO debugging sections we are interested in. */
8084
8085static void
8086dwarf2_locate_dwo_sections (bfd *abfd, asection *sectp, void *dwo_file_ptr)
8087{
8088 struct dwo_file *dwo_file = dwo_file_ptr;
8089 const struct dwo_section_names *names = &dwo_section_names;
8090
8091 if (section_is_p (sectp->name, &names->abbrev_dwo))
8092 {
8093 dwo_file->sections.abbrev.asection = sectp;
8094 dwo_file->sections.abbrev.size = bfd_get_section_size (sectp);
8095 }
8096 else if (section_is_p (sectp->name, &names->info_dwo))
8097 {
8098 dwo_file->sections.info.asection = sectp;
8099 dwo_file->sections.info.size = bfd_get_section_size (sectp);
8100 }
8101 else if (section_is_p (sectp->name, &names->line_dwo))
8102 {
8103 dwo_file->sections.line.asection = sectp;
8104 dwo_file->sections.line.size = bfd_get_section_size (sectp);
8105 }
8106 else if (section_is_p (sectp->name, &names->loc_dwo))
8107 {
8108 dwo_file->sections.loc.asection = sectp;
8109 dwo_file->sections.loc.size = bfd_get_section_size (sectp);
8110 }
09262596
DE
8111 else if (section_is_p (sectp->name, &names->macinfo_dwo))
8112 {
8113 dwo_file->sections.macinfo.asection = sectp;
8114 dwo_file->sections.macinfo.size = bfd_get_section_size (sectp);
8115 }
8116 else if (section_is_p (sectp->name, &names->macro_dwo))
8117 {
8118 dwo_file->sections.macro.asection = sectp;
8119 dwo_file->sections.macro.size = bfd_get_section_size (sectp);
8120 }
3019eac3
DE
8121 else if (section_is_p (sectp->name, &names->str_dwo))
8122 {
8123 dwo_file->sections.str.asection = sectp;
8124 dwo_file->sections.str.size = bfd_get_section_size (sectp);
8125 }
8126 else if (section_is_p (sectp->name, &names->str_offsets_dwo))
8127 {
8128 dwo_file->sections.str_offsets.asection = sectp;
8129 dwo_file->sections.str_offsets.size = bfd_get_section_size (sectp);
8130 }
8131 else if (section_is_p (sectp->name, &names->types_dwo))
8132 {
8133 struct dwarf2_section_info type_section;
8134
8135 memset (&type_section, 0, sizeof (type_section));
8136 type_section.asection = sectp;
8137 type_section.size = bfd_get_section_size (sectp);
8138 VEC_safe_push (dwarf2_section_info_def, dwo_file->sections.types,
8139 &type_section);
8140 }
8141}
8142
8143/* Structure used to pass data to create_debug_info_hash_table_reader. */
8144
8145struct create_dwo_info_table_data
8146{
8147 struct dwo_file *dwo_file;
8148 htab_t cu_htab;
8149};
8150
8151/* die_reader_func for create_debug_info_hash_table. */
8152
8153static void
8154create_debug_info_hash_table_reader (const struct die_reader_specs *reader,
8155 gdb_byte *info_ptr,
8156 struct die_info *comp_unit_die,
8157 int has_children,
8158 void *datap)
8159{
8160 struct dwarf2_cu *cu = reader->cu;
8161 struct objfile *objfile = dwarf2_per_objfile->objfile;
8162 sect_offset offset = cu->per_cu->offset;
8163 struct dwarf2_section_info *section = cu->per_cu->info_or_types_section;
8164 struct create_dwo_info_table_data *data = datap;
8165 struct dwo_file *dwo_file = data->dwo_file;
8166 htab_t cu_htab = data->cu_htab;
8167 void **slot;
8168 struct attribute *attr;
8169 struct dwo_unit *dwo_unit;
8170
8171 attr = dwarf2_attr (comp_unit_die, DW_AT_GNU_dwo_id, cu);
8172 if (attr == NULL)
8173 {
8174 error (_("Dwarf Error: debug entry at offset 0x%x is missing"
8175 " its dwo_id [in module %s]"),
8176 offset.sect_off, dwo_file->dwo_name);
8177 return;
8178 }
8179
8180 dwo_unit = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct dwo_unit);
8181 dwo_unit->dwo_file = dwo_file;
8182 dwo_unit->signature = DW_UNSND (attr);
8183 dwo_unit->info_or_types_section = section;
8184 dwo_unit->offset = offset;
8185 dwo_unit->length = cu->per_cu->length;
8186
8187 slot = htab_find_slot (cu_htab, dwo_unit, INSERT);
8188 gdb_assert (slot != NULL);
8189 if (*slot != NULL)
8190 {
8191 const struct dwo_unit *dup_dwo_unit = *slot;
8192
8193 complaint (&symfile_complaints,
8194 _("debug entry at offset 0x%x is duplicate to the entry at"
8195 " offset 0x%x, dwo_id 0x%s [in module %s]"),
8196 offset.sect_off, dup_dwo_unit->offset.sect_off,
8197 phex (dwo_unit->signature, sizeof (dwo_unit->signature)),
8198 dwo_file->dwo_name);
8199 }
8200 else
8201 *slot = dwo_unit;
8202
09406207 8203 if (dwarf2_read_debug)
3019eac3
DE
8204 fprintf_unfiltered (gdb_stdlog, " offset 0x%x, dwo_id 0x%s\n",
8205 offset.sect_off,
8206 phex (dwo_unit->signature,
8207 sizeof (dwo_unit->signature)));
8208}
8209
8210/* Create a hash table to map DWO IDs to their CU entry in .debug_info.dwo. */
8211
8212static htab_t
8213create_debug_info_hash_table (struct dwo_file *dwo_file)
8214{
8215 struct objfile *objfile = dwarf2_per_objfile->objfile;
8216 struct dwarf2_section_info *section = &dwo_file->sections.info;
8217 bfd *abfd;
8218 htab_t cu_htab;
8219 gdb_byte *info_ptr, *end_ptr;
8220 struct create_dwo_info_table_data create_dwo_info_table_data;
8221
8222 dwarf2_read_section (objfile, section);
8223 info_ptr = section->buffer;
8224
8225 if (info_ptr == NULL)
8226 return NULL;
8227
8228 /* We can't set abfd until now because the section may be empty or
8229 not present, in which case section->asection will be NULL. */
8230 abfd = section->asection->owner;
8231
09406207 8232 if (dwarf2_read_debug)
3019eac3
DE
8233 fprintf_unfiltered (gdb_stdlog, "Reading .debug_info.dwo for %s:\n",
8234 bfd_get_filename (abfd));
8235
8236 cu_htab = allocate_dwo_unit_table (objfile);
8237
8238 create_dwo_info_table_data.dwo_file = dwo_file;
8239 create_dwo_info_table_data.cu_htab = cu_htab;
8240
8241 end_ptr = info_ptr + section->size;
8242 while (info_ptr < end_ptr)
8243 {
8244 struct dwarf2_per_cu_data per_cu;
8245
8246 memset (&per_cu, 0, sizeof (per_cu));
8247 per_cu.objfile = objfile;
8248 per_cu.is_debug_types = 0;
8249 per_cu.offset.sect_off = info_ptr - section->buffer;
8250 per_cu.info_or_types_section = section;
8251
8252 init_cutu_and_read_dies_no_follow (&per_cu,
8253 &dwo_file->sections.abbrev,
8254 dwo_file,
8255 create_debug_info_hash_table_reader,
8256 &create_dwo_info_table_data);
8257
8258 info_ptr += per_cu.length;
8259 }
8260
8261 return cu_htab;
8262}
8263
8264/* Subroutine of open_dwo_file to simplify it.
8265 Open the file specified by FILE_NAME and hand it off to BFD for
8266 preliminary analysis. Return a newly initialized bfd *, which
8267 includes a canonicalized copy of FILE_NAME.
8268 In case of trouble, return NULL.
8269 NOTE: This function is derived from symfile_bfd_open. */
8270
8271static bfd *
8272try_open_dwo_file (const char *file_name)
8273{
8274 bfd *sym_bfd;
8275 int desc;
8276 char *absolute_name;
3019eac3
DE
8277
8278 desc = openp (debug_file_directory, OPF_TRY_CWD_FIRST, file_name,
8279 O_RDONLY | O_BINARY, &absolute_name);
8280 if (desc < 0)
8281 return NULL;
8282
bb397797 8283 sym_bfd = gdb_bfd_open (absolute_name, gnutarget, desc);
3019eac3
DE
8284 if (!sym_bfd)
8285 {
3019eac3
DE
8286 xfree (absolute_name);
8287 return NULL;
8288 }
a4453b7e 8289 xfree (absolute_name);
3019eac3
DE
8290 bfd_set_cacheable (sym_bfd, 1);
8291
8292 if (!bfd_check_format (sym_bfd, bfd_object))
8293 {
cbb099e8 8294 gdb_bfd_unref (sym_bfd); /* This also closes desc. */
3019eac3
DE
8295 return NULL;
8296 }
8297
3019eac3
DE
8298 return sym_bfd;
8299}
8300
8301/* Try to open DWO file DWO_NAME.
8302 COMP_DIR is the DW_AT_comp_dir attribute.
8303 The result is the bfd handle of the file.
8304 If there is a problem finding or opening the file, return NULL.
8305 Upon success, the canonicalized path of the file is stored in the bfd,
8306 same as symfile_bfd_open. */
8307
8308static bfd *
8309open_dwo_file (const char *dwo_name, const char *comp_dir)
8310{
8311 bfd *abfd;
3019eac3
DE
8312
8313 if (IS_ABSOLUTE_PATH (dwo_name))
8314 return try_open_dwo_file (dwo_name);
8315
8316 /* Before trying the search path, try DWO_NAME in COMP_DIR. */
8317
8318 if (comp_dir != NULL)
8319 {
8320 char *path_to_try = concat (comp_dir, SLASH_STRING, dwo_name, NULL);
8321
8322 /* NOTE: If comp_dir is a relative path, this will also try the
8323 search path, which seems useful. */
8324 abfd = try_open_dwo_file (path_to_try);
8325 xfree (path_to_try);
8326 if (abfd != NULL)
8327 return abfd;
8328 }
8329
8330 /* That didn't work, try debug-file-directory, which, despite its name,
8331 is a list of paths. */
8332
8333 if (*debug_file_directory == '\0')
8334 return NULL;
8335
8336 return try_open_dwo_file (dwo_name);
8337}
8338
8339/* Initialize the use of the DWO file specified by DWO_NAME. */
8340
8341static struct dwo_file *
8342init_dwo_file (const char *dwo_name, const char *comp_dir)
8343{
8344 struct objfile *objfile = dwarf2_per_objfile->objfile;
8345 struct dwo_file *dwo_file = OBSTACK_ZALLOC (&objfile->objfile_obstack,
8346 struct dwo_file);
8347 bfd *abfd;
8348 struct cleanup *cleanups;
8349
09406207 8350 if (dwarf2_read_debug)
3019eac3
DE
8351 fprintf_unfiltered (gdb_stdlog, "Reading DWO file %s:\n", dwo_name);
8352
8353 abfd = open_dwo_file (dwo_name, comp_dir);
8354 if (abfd == NULL)
8355 return NULL;
8356 dwo_file->dwo_name = dwo_name;
8357 dwo_file->dwo_bfd = abfd;
8358
8359 cleanups = make_cleanup (free_dwo_file_cleanup, dwo_file);
8360
8361 bfd_map_over_sections (abfd, dwarf2_locate_dwo_sections, dwo_file);
8362
8363 dwo_file->cus = create_debug_info_hash_table (dwo_file);
8364
8365 dwo_file->tus = create_debug_types_hash_table (dwo_file,
8366 dwo_file->sections.types);
8367
8368 discard_cleanups (cleanups);
8369
8370 return dwo_file;
8371}
8372
8373/* Lookup DWO file DWO_NAME. */
8374
8375static struct dwo_file *
a1855c1d 8376lookup_dwo_file (const char *dwo_name, const char *comp_dir)
3019eac3
DE
8377{
8378 struct dwo_file *dwo_file;
8379 struct dwo_file find_entry;
8380 void **slot;
8381
8382 if (dwarf2_per_objfile->dwo_files == NULL)
8383 dwarf2_per_objfile->dwo_files = allocate_dwo_file_hash_table ();
8384
8385 /* Have we already seen this DWO file? */
8386 find_entry.dwo_name = dwo_name;
8387 slot = htab_find_slot (dwarf2_per_objfile->dwo_files, &find_entry, INSERT);
8388
8389 /* If not, read it in and build a table of the DWOs it contains. */
8390 if (*slot == NULL)
8391 *slot = init_dwo_file (dwo_name, comp_dir);
8392
8393 /* NOTE: This will be NULL if unable to open the file. */
8394 dwo_file = *slot;
8395
8396 return dwo_file;
8397}
8398
8399/* Lookup the DWO CU referenced from THIS_CU in DWO file DWO_NAME.
8400 If non-NULL, comp_dir is the DW_AT_comp_dir attribute.
8401 SIGNATURE is the "dwo_id" of the CU (for consistency we use the same
8402 nomenclature as TUs).
1c658ad5 8403 The result is a pointer to the dwo_unit object or NULL if we didn't find it
3019eac3
DE
8404 (dwo_id mismatch or couldn't find the DWO file). */
8405
8406static struct dwo_unit *
8407lookup_dwo_comp_unit (struct dwarf2_per_cu_data *this_cu,
a1855c1d 8408 const char *dwo_name, const char *comp_dir,
3019eac3
DE
8409 ULONGEST signature)
8410{
8411 struct objfile *objfile = dwarf2_per_objfile->objfile;
8412 struct dwo_file *dwo_file;
8413
8414 dwo_file = lookup_dwo_file (dwo_name, comp_dir);
8415 if (dwo_file == NULL)
8416 return NULL;
8417
8418 /* Look up the DWO using its signature(dwo_id). */
8419
8420 if (dwo_file->cus != NULL)
8421 {
8422 struct dwo_unit find_dwo_cu, *dwo_cu;
8423
8424 find_dwo_cu.signature = signature;
8425 dwo_cu = htab_find (dwo_file->cus, &find_dwo_cu);
a766d390 8426
3019eac3
DE
8427 if (dwo_cu != NULL)
8428 return dwo_cu;
8429 }
c906108c 8430
3019eac3 8431 /* We didn't find it. This must mean a dwo_id mismatch. */
df8a16a1 8432
3019eac3
DE
8433 complaint (&symfile_complaints,
8434 _("Could not find DWO CU referenced by CU at offset 0x%x"
8435 " [in module %s]"),
8436 this_cu->offset.sect_off, objfile->name);
8437 return NULL;
8438}
c906108c 8439
3019eac3
DE
8440/* Lookup the DWO TU referenced from THIS_TU in DWO file DWO_NAME.
8441 If non-NULL, comp_dir is the DW_AT_comp_dir attribute.
1c658ad5 8442 The result is a pointer to the dwo_unit object or NULL if we didn't find it
3019eac3 8443 (dwo_id mismatch or couldn't find the DWO file). */
debd256d 8444
3019eac3
DE
8445static struct dwo_unit *
8446lookup_dwo_type_unit (struct signatured_type *this_tu,
a1855c1d 8447 const char *dwo_name, const char *comp_dir)
3019eac3
DE
8448{
8449 struct objfile *objfile = dwarf2_per_objfile->objfile;
8450 struct dwo_file *dwo_file;
cb1df416 8451
3019eac3
DE
8452 dwo_file = lookup_dwo_file (dwo_name, comp_dir);
8453 if (dwo_file == NULL)
8454 return NULL;
cf2c3c16 8455
3019eac3
DE
8456 /* Look up the DWO using its signature(dwo_id). */
8457
8458 if (dwo_file->tus != NULL)
cf2c3c16 8459 {
3019eac3 8460 struct dwo_unit find_dwo_tu, *dwo_tu;
9a619af0 8461
3019eac3
DE
8462 find_dwo_tu.signature = this_tu->signature;
8463 dwo_tu = htab_find (dwo_file->tus, &find_dwo_tu);
8464
8465 if (dwo_tu != NULL)
8466 return dwo_tu;
2e276125 8467 }
9cdd5dbd 8468
3019eac3
DE
8469 /* We didn't find it. This must mean a dwo_id mismatch. */
8470
8471 complaint (&symfile_complaints,
8472 _("Could not find DWO TU referenced by TU at offset 0x%x"
8473 " [in module %s]"),
8474 this_tu->per_cu.offset.sect_off, objfile->name);
8475 return NULL;
5fb290d7
DJ
8476}
8477
3019eac3
DE
8478/* Free all resources associated with DWO_FILE.
8479 Close the DWO file and munmap the sections.
8480 All memory should be on the objfile obstack. */
348e048f
DE
8481
8482static void
3019eac3 8483free_dwo_file (struct dwo_file *dwo_file, struct objfile *objfile)
348e048f 8484{
3019eac3
DE
8485 int ix;
8486 struct dwarf2_section_info *section;
348e048f 8487
3019eac3 8488 gdb_assert (dwo_file->dwo_bfd != objfile->obfd);
cbb099e8 8489 gdb_bfd_unref (dwo_file->dwo_bfd);
348e048f 8490
3019eac3
DE
8491 VEC_free (dwarf2_section_info_def, dwo_file->sections.types);
8492}
348e048f 8493
3019eac3 8494/* Wrapper for free_dwo_file for use in cleanups. */
348e048f 8495
3019eac3
DE
8496static void
8497free_dwo_file_cleanup (void *arg)
8498{
8499 struct dwo_file *dwo_file = (struct dwo_file *) arg;
8500 struct objfile *objfile = dwarf2_per_objfile->objfile;
348e048f 8501
3019eac3
DE
8502 free_dwo_file (dwo_file, objfile);
8503}
348e048f 8504
3019eac3 8505/* Traversal function for free_dwo_files. */
2ab95328 8506
3019eac3
DE
8507static int
8508free_dwo_file_from_slot (void **slot, void *info)
8509{
8510 struct dwo_file *dwo_file = (struct dwo_file *) *slot;
8511 struct objfile *objfile = (struct objfile *) info;
348e048f 8512
3019eac3 8513 free_dwo_file (dwo_file, objfile);
348e048f 8514
3019eac3
DE
8515 return 1;
8516}
348e048f 8517
3019eac3 8518/* Free all resources associated with DWO_FILES. */
348e048f 8519
3019eac3
DE
8520static void
8521free_dwo_files (htab_t dwo_files, struct objfile *objfile)
8522{
8523 htab_traverse_noresize (dwo_files, free_dwo_file_from_slot, objfile);
348e048f 8524}
3019eac3
DE
8525\f
8526/* Read in various DIEs. */
348e048f 8527
d389af10
JK
8528/* qsort helper for inherit_abstract_dies. */
8529
8530static int
8531unsigned_int_compar (const void *ap, const void *bp)
8532{
8533 unsigned int a = *(unsigned int *) ap;
8534 unsigned int b = *(unsigned int *) bp;
8535
8536 return (a > b) - (b > a);
8537}
8538
8539/* DW_AT_abstract_origin inherits whole DIEs (not just their attributes).
3e43a32a
MS
8540 Inherit only the children of the DW_AT_abstract_origin DIE not being
8541 already referenced by DW_AT_abstract_origin from the children of the
8542 current DIE. */
d389af10
JK
8543
8544static void
8545inherit_abstract_dies (struct die_info *die, struct dwarf2_cu *cu)
8546{
8547 struct die_info *child_die;
8548 unsigned die_children_count;
8549 /* CU offsets which were referenced by children of the current DIE. */
b64f50a1
JK
8550 sect_offset *offsets;
8551 sect_offset *offsets_end, *offsetp;
d389af10
JK
8552 /* Parent of DIE - referenced by DW_AT_abstract_origin. */
8553 struct die_info *origin_die;
8554 /* Iterator of the ORIGIN_DIE children. */
8555 struct die_info *origin_child_die;
8556 struct cleanup *cleanups;
8557 struct attribute *attr;
cd02d79d
PA
8558 struct dwarf2_cu *origin_cu;
8559 struct pending **origin_previous_list_in_scope;
d389af10
JK
8560
8561 attr = dwarf2_attr (die, DW_AT_abstract_origin, cu);
8562 if (!attr)
8563 return;
8564
cd02d79d
PA
8565 /* Note that following die references may follow to a die in a
8566 different cu. */
8567
8568 origin_cu = cu;
8569 origin_die = follow_die_ref (die, attr, &origin_cu);
8570
8571 /* We're inheriting ORIGIN's children into the scope we'd put DIE's
8572 symbols in. */
8573 origin_previous_list_in_scope = origin_cu->list_in_scope;
8574 origin_cu->list_in_scope = cu->list_in_scope;
8575
edb3359d
DJ
8576 if (die->tag != origin_die->tag
8577 && !(die->tag == DW_TAG_inlined_subroutine
8578 && origin_die->tag == DW_TAG_subprogram))
d389af10
JK
8579 complaint (&symfile_complaints,
8580 _("DIE 0x%x and its abstract origin 0x%x have different tags"),
b64f50a1 8581 die->offset.sect_off, origin_die->offset.sect_off);
d389af10
JK
8582
8583 child_die = die->child;
8584 die_children_count = 0;
8585 while (child_die && child_die->tag)
8586 {
8587 child_die = sibling_die (child_die);
8588 die_children_count++;
8589 }
8590 offsets = xmalloc (sizeof (*offsets) * die_children_count);
8591 cleanups = make_cleanup (xfree, offsets);
8592
8593 offsets_end = offsets;
8594 child_die = die->child;
8595 while (child_die && child_die->tag)
8596 {
c38f313d
DJ
8597 /* For each CHILD_DIE, find the corresponding child of
8598 ORIGIN_DIE. If there is more than one layer of
8599 DW_AT_abstract_origin, follow them all; there shouldn't be,
8600 but GCC versions at least through 4.4 generate this (GCC PR
8601 40573). */
8602 struct die_info *child_origin_die = child_die;
cd02d79d 8603 struct dwarf2_cu *child_origin_cu = cu;
9a619af0 8604
c38f313d
DJ
8605 while (1)
8606 {
cd02d79d
PA
8607 attr = dwarf2_attr (child_origin_die, DW_AT_abstract_origin,
8608 child_origin_cu);
c38f313d
DJ
8609 if (attr == NULL)
8610 break;
cd02d79d
PA
8611 child_origin_die = follow_die_ref (child_origin_die, attr,
8612 &child_origin_cu);
c38f313d
DJ
8613 }
8614
d389af10
JK
8615 /* According to DWARF3 3.3.8.2 #3 new entries without their abstract
8616 counterpart may exist. */
c38f313d 8617 if (child_origin_die != child_die)
d389af10 8618 {
edb3359d
DJ
8619 if (child_die->tag != child_origin_die->tag
8620 && !(child_die->tag == DW_TAG_inlined_subroutine
8621 && child_origin_die->tag == DW_TAG_subprogram))
d389af10
JK
8622 complaint (&symfile_complaints,
8623 _("Child DIE 0x%x and its abstract origin 0x%x have "
b64f50a1
JK
8624 "different tags"), child_die->offset.sect_off,
8625 child_origin_die->offset.sect_off);
c38f313d
DJ
8626 if (child_origin_die->parent != origin_die)
8627 complaint (&symfile_complaints,
8628 _("Child DIE 0x%x and its abstract origin 0x%x have "
b64f50a1
JK
8629 "different parents"), child_die->offset.sect_off,
8630 child_origin_die->offset.sect_off);
c38f313d
DJ
8631 else
8632 *offsets_end++ = child_origin_die->offset;
d389af10
JK
8633 }
8634 child_die = sibling_die (child_die);
8635 }
8636 qsort (offsets, offsets_end - offsets, sizeof (*offsets),
8637 unsigned_int_compar);
8638 for (offsetp = offsets + 1; offsetp < offsets_end; offsetp++)
b64f50a1 8639 if (offsetp[-1].sect_off == offsetp->sect_off)
3e43a32a
MS
8640 complaint (&symfile_complaints,
8641 _("Multiple children of DIE 0x%x refer "
8642 "to DIE 0x%x as their abstract origin"),
b64f50a1 8643 die->offset.sect_off, offsetp->sect_off);
d389af10
JK
8644
8645 offsetp = offsets;
8646 origin_child_die = origin_die->child;
8647 while (origin_child_die && origin_child_die->tag)
8648 {
8649 /* Is ORIGIN_CHILD_DIE referenced by any of the DIE children? */
b64f50a1
JK
8650 while (offsetp < offsets_end
8651 && offsetp->sect_off < origin_child_die->offset.sect_off)
d389af10 8652 offsetp++;
b64f50a1
JK
8653 if (offsetp >= offsets_end
8654 || offsetp->sect_off > origin_child_die->offset.sect_off)
d389af10
JK
8655 {
8656 /* Found that ORIGIN_CHILD_DIE is really not referenced. */
cd02d79d 8657 process_die (origin_child_die, origin_cu);
d389af10
JK
8658 }
8659 origin_child_die = sibling_die (origin_child_die);
8660 }
cd02d79d 8661 origin_cu->list_in_scope = origin_previous_list_in_scope;
d389af10
JK
8662
8663 do_cleanups (cleanups);
8664}
8665
c906108c 8666static void
e7c27a73 8667read_func_scope (struct die_info *die, struct dwarf2_cu *cu)
c906108c 8668{
e7c27a73 8669 struct objfile *objfile = cu->objfile;
52f0bd74 8670 struct context_stack *new;
c906108c
SS
8671 CORE_ADDR lowpc;
8672 CORE_ADDR highpc;
8673 struct die_info *child_die;
edb3359d 8674 struct attribute *attr, *call_line, *call_file;
c906108c 8675 char *name;
e142c38c 8676 CORE_ADDR baseaddr;
801e3a5b 8677 struct block *block;
edb3359d 8678 int inlined_func = (die->tag == DW_TAG_inlined_subroutine);
34eaf542
TT
8679 VEC (symbolp) *template_args = NULL;
8680 struct template_symbol *templ_func = NULL;
edb3359d
DJ
8681
8682 if (inlined_func)
8683 {
8684 /* If we do not have call site information, we can't show the
8685 caller of this inlined function. That's too confusing, so
8686 only use the scope for local variables. */
8687 call_line = dwarf2_attr (die, DW_AT_call_line, cu);
8688 call_file = dwarf2_attr (die, DW_AT_call_file, cu);
8689 if (call_line == NULL || call_file == NULL)
8690 {
8691 read_lexical_block_scope (die, cu);
8692 return;
8693 }
8694 }
c906108c 8695
e142c38c
DJ
8696 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
8697
94af9270 8698 name = dwarf2_name (die, cu);
c906108c 8699
e8d05480
JB
8700 /* Ignore functions with missing or empty names. These are actually
8701 illegal according to the DWARF standard. */
8702 if (name == NULL)
8703 {
8704 complaint (&symfile_complaints,
b64f50a1
JK
8705 _("missing name for subprogram DIE at %d"),
8706 die->offset.sect_off);
e8d05480
JB
8707 return;
8708 }
8709
8710 /* Ignore functions with missing or invalid low and high pc attributes. */
8711 if (!dwarf2_get_pc_bounds (die, &lowpc, &highpc, cu, NULL))
8712 {
ae4d0c03
PM
8713 attr = dwarf2_attr (die, DW_AT_external, cu);
8714 if (!attr || !DW_UNSND (attr))
8715 complaint (&symfile_complaints,
3e43a32a
MS
8716 _("cannot get low and high bounds "
8717 "for subprogram DIE at %d"),
b64f50a1 8718 die->offset.sect_off);
e8d05480
JB
8719 return;
8720 }
c906108c
SS
8721
8722 lowpc += baseaddr;
8723 highpc += baseaddr;
8724
34eaf542
TT
8725 /* If we have any template arguments, then we must allocate a
8726 different sort of symbol. */
8727 for (child_die = die->child; child_die; child_die = sibling_die (child_die))
8728 {
8729 if (child_die->tag == DW_TAG_template_type_param
8730 || child_die->tag == DW_TAG_template_value_param)
8731 {
8732 templ_func = OBSTACK_ZALLOC (&objfile->objfile_obstack,
8733 struct template_symbol);
8734 templ_func->base.is_cplus_template_function = 1;
8735 break;
8736 }
8737 }
8738
c906108c 8739 new = push_context (0, lowpc);
34eaf542
TT
8740 new->name = new_symbol_full (die, read_type_die (die, cu), cu,
8741 (struct symbol *) templ_func);
4c2df51b 8742
4cecd739
DJ
8743 /* If there is a location expression for DW_AT_frame_base, record
8744 it. */
e142c38c 8745 attr = dwarf2_attr (die, DW_AT_frame_base, cu);
4c2df51b 8746 if (attr)
c034e007
AC
8747 /* FIXME: cagney/2004-01-26: The DW_AT_frame_base's location
8748 expression is being recorded directly in the function's symbol
8749 and not in a separate frame-base object. I guess this hack is
8750 to avoid adding some sort of frame-base adjunct/annex to the
8751 function's symbol :-(. The problem with doing this is that it
8752 results in a function symbol with a location expression that
8753 has nothing to do with the location of the function, ouch! The
8754 relationship should be: a function's symbol has-a frame base; a
8755 frame-base has-a location expression. */
e7c27a73 8756 dwarf2_symbol_mark_computed (attr, new->name, cu);
4c2df51b 8757
e142c38c 8758 cu->list_in_scope = &local_symbols;
c906108c 8759
639d11d3 8760 if (die->child != NULL)
c906108c 8761 {
639d11d3 8762 child_die = die->child;
c906108c
SS
8763 while (child_die && child_die->tag)
8764 {
34eaf542
TT
8765 if (child_die->tag == DW_TAG_template_type_param
8766 || child_die->tag == DW_TAG_template_value_param)
8767 {
8768 struct symbol *arg = new_symbol (child_die, NULL, cu);
8769
f1078f66
DJ
8770 if (arg != NULL)
8771 VEC_safe_push (symbolp, template_args, arg);
34eaf542
TT
8772 }
8773 else
8774 process_die (child_die, cu);
c906108c
SS
8775 child_die = sibling_die (child_die);
8776 }
8777 }
8778
d389af10
JK
8779 inherit_abstract_dies (die, cu);
8780
4a811a97
UW
8781 /* If we have a DW_AT_specification, we might need to import using
8782 directives from the context of the specification DIE. See the
8783 comment in determine_prefix. */
8784 if (cu->language == language_cplus
8785 && dwarf2_attr (die, DW_AT_specification, cu))
8786 {
8787 struct dwarf2_cu *spec_cu = cu;
8788 struct die_info *spec_die = die_specification (die, &spec_cu);
8789
8790 while (spec_die)
8791 {
8792 child_die = spec_die->child;
8793 while (child_die && child_die->tag)
8794 {
8795 if (child_die->tag == DW_TAG_imported_module)
8796 process_die (child_die, spec_cu);
8797 child_die = sibling_die (child_die);
8798 }
8799
8800 /* In some cases, GCC generates specification DIEs that
8801 themselves contain DW_AT_specification attributes. */
8802 spec_die = die_specification (spec_die, &spec_cu);
8803 }
8804 }
8805
c906108c
SS
8806 new = pop_context ();
8807 /* Make a block for the local symbols within. */
801e3a5b
JB
8808 block = finish_block (new->name, &local_symbols, new->old_blocks,
8809 lowpc, highpc, objfile);
8810
df8a16a1 8811 /* For C++, set the block's scope. */
f55ee35c 8812 if (cu->language == language_cplus || cu->language == language_fortran)
df8a16a1 8813 cp_set_block_scope (new->name, block, &objfile->objfile_obstack,
0114d602 8814 determine_prefix (die, cu),
df8a16a1
DJ
8815 processing_has_namespace_info);
8816
801e3a5b
JB
8817 /* If we have address ranges, record them. */
8818 dwarf2_record_block_ranges (die, block, baseaddr, cu);
6e70227d 8819
34eaf542
TT
8820 /* Attach template arguments to function. */
8821 if (! VEC_empty (symbolp, template_args))
8822 {
8823 gdb_assert (templ_func != NULL);
8824
8825 templ_func->n_template_arguments = VEC_length (symbolp, template_args);
8826 templ_func->template_arguments
8827 = obstack_alloc (&objfile->objfile_obstack,
8828 (templ_func->n_template_arguments
8829 * sizeof (struct symbol *)));
8830 memcpy (templ_func->template_arguments,
8831 VEC_address (symbolp, template_args),
8832 (templ_func->n_template_arguments * sizeof (struct symbol *)));
8833 VEC_free (symbolp, template_args);
8834 }
8835
208d8187
JB
8836 /* In C++, we can have functions nested inside functions (e.g., when
8837 a function declares a class that has methods). This means that
8838 when we finish processing a function scope, we may need to go
8839 back to building a containing block's symbol lists. */
8840 local_symbols = new->locals;
27aa8d6a 8841 using_directives = new->using_directives;
208d8187 8842
921e78cf
JB
8843 /* If we've finished processing a top-level function, subsequent
8844 symbols go in the file symbol list. */
8845 if (outermost_context_p ())
e142c38c 8846 cu->list_in_scope = &file_symbols;
c906108c
SS
8847}
8848
8849/* Process all the DIES contained within a lexical block scope. Start
8850 a new scope, process the dies, and then close the scope. */
8851
8852static void
e7c27a73 8853read_lexical_block_scope (struct die_info *die, struct dwarf2_cu *cu)
c906108c 8854{
e7c27a73 8855 struct objfile *objfile = cu->objfile;
52f0bd74 8856 struct context_stack *new;
c906108c
SS
8857 CORE_ADDR lowpc, highpc;
8858 struct die_info *child_die;
e142c38c
DJ
8859 CORE_ADDR baseaddr;
8860
8861 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
c906108c
SS
8862
8863 /* Ignore blocks with missing or invalid low and high pc attributes. */
af34e669
DJ
8864 /* ??? Perhaps consider discontiguous blocks defined by DW_AT_ranges
8865 as multiple lexical blocks? Handling children in a sane way would
6e70227d 8866 be nasty. Might be easier to properly extend generic blocks to
af34e669 8867 describe ranges. */
d85a05f0 8868 if (!dwarf2_get_pc_bounds (die, &lowpc, &highpc, cu, NULL))
c906108c
SS
8869 return;
8870 lowpc += baseaddr;
8871 highpc += baseaddr;
8872
8873 push_context (0, lowpc);
639d11d3 8874 if (die->child != NULL)
c906108c 8875 {
639d11d3 8876 child_die = die->child;
c906108c
SS
8877 while (child_die && child_die->tag)
8878 {
e7c27a73 8879 process_die (child_die, cu);
c906108c
SS
8880 child_die = sibling_die (child_die);
8881 }
8882 }
8883 new = pop_context ();
8884
8540c487 8885 if (local_symbols != NULL || using_directives != NULL)
c906108c 8886 {
801e3a5b
JB
8887 struct block *block
8888 = finish_block (0, &local_symbols, new->old_blocks, new->start_addr,
8889 highpc, objfile);
8890
8891 /* Note that recording ranges after traversing children, as we
8892 do here, means that recording a parent's ranges entails
8893 walking across all its children's ranges as they appear in
8894 the address map, which is quadratic behavior.
8895
8896 It would be nicer to record the parent's ranges before
8897 traversing its children, simply overriding whatever you find
8898 there. But since we don't even decide whether to create a
8899 block until after we've traversed its children, that's hard
8900 to do. */
8901 dwarf2_record_block_ranges (die, block, baseaddr, cu);
c906108c
SS
8902 }
8903 local_symbols = new->locals;
27aa8d6a 8904 using_directives = new->using_directives;
c906108c
SS
8905}
8906
96408a79
SA
8907/* Read in DW_TAG_GNU_call_site and insert it to CU->call_site_htab. */
8908
8909static void
8910read_call_site_scope (struct die_info *die, struct dwarf2_cu *cu)
8911{
8912 struct objfile *objfile = cu->objfile;
8913 struct gdbarch *gdbarch = get_objfile_arch (objfile);
8914 CORE_ADDR pc, baseaddr;
8915 struct attribute *attr;
8916 struct call_site *call_site, call_site_local;
8917 void **slot;
8918 int nparams;
8919 struct die_info *child_die;
8920
8921 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
8922
8923 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
8924 if (!attr)
8925 {
8926 complaint (&symfile_complaints,
8927 _("missing DW_AT_low_pc for DW_TAG_GNU_call_site "
8928 "DIE 0x%x [in module %s]"),
b64f50a1 8929 die->offset.sect_off, objfile->name);
96408a79
SA
8930 return;
8931 }
8932 pc = DW_ADDR (attr) + baseaddr;
8933
8934 if (cu->call_site_htab == NULL)
8935 cu->call_site_htab = htab_create_alloc_ex (16, core_addr_hash, core_addr_eq,
8936 NULL, &objfile->objfile_obstack,
8937 hashtab_obstack_allocate, NULL);
8938 call_site_local.pc = pc;
8939 slot = htab_find_slot (cu->call_site_htab, &call_site_local, INSERT);
8940 if (*slot != NULL)
8941 {
8942 complaint (&symfile_complaints,
8943 _("Duplicate PC %s for DW_TAG_GNU_call_site "
8944 "DIE 0x%x [in module %s]"),
b64f50a1 8945 paddress (gdbarch, pc), die->offset.sect_off, objfile->name);
96408a79
SA
8946 return;
8947 }
8948
8949 /* Count parameters at the caller. */
8950
8951 nparams = 0;
8952 for (child_die = die->child; child_die && child_die->tag;
8953 child_die = sibling_die (child_die))
8954 {
8955 if (child_die->tag != DW_TAG_GNU_call_site_parameter)
8956 {
8957 complaint (&symfile_complaints,
8958 _("Tag %d is not DW_TAG_GNU_call_site_parameter in "
8959 "DW_TAG_GNU_call_site child DIE 0x%x [in module %s]"),
b64f50a1 8960 child_die->tag, child_die->offset.sect_off, objfile->name);
96408a79
SA
8961 continue;
8962 }
8963
8964 nparams++;
8965 }
8966
8967 call_site = obstack_alloc (&objfile->objfile_obstack,
8968 (sizeof (*call_site)
8969 + (sizeof (*call_site->parameter)
8970 * (nparams - 1))));
8971 *slot = call_site;
8972 memset (call_site, 0, sizeof (*call_site) - sizeof (*call_site->parameter));
8973 call_site->pc = pc;
8974
8975 if (dwarf2_flag_true_p (die, DW_AT_GNU_tail_call, cu))
8976 {
8977 struct die_info *func_die;
8978
8979 /* Skip also over DW_TAG_inlined_subroutine. */
8980 for (func_die = die->parent;
8981 func_die && func_die->tag != DW_TAG_subprogram
8982 && func_die->tag != DW_TAG_subroutine_type;
8983 func_die = func_die->parent);
8984
8985 /* DW_AT_GNU_all_call_sites is a superset
8986 of DW_AT_GNU_all_tail_call_sites. */
8987 if (func_die
8988 && !dwarf2_flag_true_p (func_die, DW_AT_GNU_all_call_sites, cu)
8989 && !dwarf2_flag_true_p (func_die, DW_AT_GNU_all_tail_call_sites, cu))
8990 {
8991 /* TYPE_TAIL_CALL_LIST is not interesting in functions where it is
8992 not complete. But keep CALL_SITE for look ups via call_site_htab,
8993 both the initial caller containing the real return address PC and
8994 the final callee containing the current PC of a chain of tail
8995 calls do not need to have the tail call list complete. But any
8996 function candidate for a virtual tail call frame searched via
8997 TYPE_TAIL_CALL_LIST must have the tail call list complete to be
8998 determined unambiguously. */
8999 }
9000 else
9001 {
9002 struct type *func_type = NULL;
9003
9004 if (func_die)
9005 func_type = get_die_type (func_die, cu);
9006 if (func_type != NULL)
9007 {
9008 gdb_assert (TYPE_CODE (func_type) == TYPE_CODE_FUNC);
9009
9010 /* Enlist this call site to the function. */
9011 call_site->tail_call_next = TYPE_TAIL_CALL_LIST (func_type);
9012 TYPE_TAIL_CALL_LIST (func_type) = call_site;
9013 }
9014 else
9015 complaint (&symfile_complaints,
9016 _("Cannot find function owning DW_TAG_GNU_call_site "
9017 "DIE 0x%x [in module %s]"),
b64f50a1 9018 die->offset.sect_off, objfile->name);
96408a79
SA
9019 }
9020 }
9021
9022 attr = dwarf2_attr (die, DW_AT_GNU_call_site_target, cu);
9023 if (attr == NULL)
9024 attr = dwarf2_attr (die, DW_AT_abstract_origin, cu);
9025 SET_FIELD_DWARF_BLOCK (call_site->target, NULL);
9026 if (!attr || (attr_form_is_block (attr) && DW_BLOCK (attr)->size == 0))
9027 /* Keep NULL DWARF_BLOCK. */;
9028 else if (attr_form_is_block (attr))
9029 {
9030 struct dwarf2_locexpr_baton *dlbaton;
9031
9032 dlbaton = obstack_alloc (&objfile->objfile_obstack, sizeof (*dlbaton));
9033 dlbaton->data = DW_BLOCK (attr)->data;
9034 dlbaton->size = DW_BLOCK (attr)->size;
9035 dlbaton->per_cu = cu->per_cu;
9036
9037 SET_FIELD_DWARF_BLOCK (call_site->target, dlbaton);
9038 }
9039 else if (is_ref_attr (attr))
9040 {
96408a79
SA
9041 struct dwarf2_cu *target_cu = cu;
9042 struct die_info *target_die;
9043
9044 target_die = follow_die_ref_or_sig (die, attr, &target_cu);
9045 gdb_assert (target_cu->objfile == objfile);
9046 if (die_is_declaration (target_die, target_cu))
9047 {
9048 const char *target_physname;
9049
9050 target_physname = dwarf2_physname (NULL, target_die, target_cu);
9051 if (target_physname == NULL)
9052 complaint (&symfile_complaints,
9053 _("DW_AT_GNU_call_site_target target DIE has invalid "
9054 "physname, for referencing DIE 0x%x [in module %s]"),
b64f50a1 9055 die->offset.sect_off, objfile->name);
96408a79
SA
9056 else
9057 SET_FIELD_PHYSNAME (call_site->target, (char *) target_physname);
9058 }
9059 else
9060 {
9061 CORE_ADDR lowpc;
9062
9063 /* DW_AT_entry_pc should be preferred. */
9064 if (!dwarf2_get_pc_bounds (target_die, &lowpc, NULL, target_cu, NULL))
9065 complaint (&symfile_complaints,
9066 _("DW_AT_GNU_call_site_target target DIE has invalid "
9067 "low pc, for referencing DIE 0x%x [in module %s]"),
b64f50a1 9068 die->offset.sect_off, objfile->name);
96408a79
SA
9069 else
9070 SET_FIELD_PHYSADDR (call_site->target, lowpc + baseaddr);
9071 }
9072 }
9073 else
9074 complaint (&symfile_complaints,
9075 _("DW_TAG_GNU_call_site DW_AT_GNU_call_site_target is neither "
9076 "block nor reference, for DIE 0x%x [in module %s]"),
b64f50a1 9077 die->offset.sect_off, objfile->name);
96408a79
SA
9078
9079 call_site->per_cu = cu->per_cu;
9080
9081 for (child_die = die->child;
9082 child_die && child_die->tag;
9083 child_die = sibling_die (child_die))
9084 {
96408a79 9085 struct call_site_parameter *parameter;
1788b2d3 9086 struct attribute *loc, *origin;
96408a79
SA
9087
9088 if (child_die->tag != DW_TAG_GNU_call_site_parameter)
9089 {
9090 /* Already printed the complaint above. */
9091 continue;
9092 }
9093
9094 gdb_assert (call_site->parameter_count < nparams);
9095 parameter = &call_site->parameter[call_site->parameter_count];
9096
1788b2d3
JK
9097 /* DW_AT_location specifies the register number or DW_AT_abstract_origin
9098 specifies DW_TAG_formal_parameter. Value of the data assumed for the
9099 register is contained in DW_AT_GNU_call_site_value. */
96408a79 9100
24c5c679 9101 loc = dwarf2_attr (child_die, DW_AT_location, cu);
1788b2d3
JK
9102 origin = dwarf2_attr (child_die, DW_AT_abstract_origin, cu);
9103 if (loc == NULL && origin != NULL && is_ref_attr (origin))
9104 {
9105 sect_offset offset;
9106
9107 parameter->kind = CALL_SITE_PARAMETER_PARAM_OFFSET;
9108 offset = dwarf2_get_ref_die_offset (origin);
d76b7dbc
JK
9109 if (!offset_in_cu_p (&cu->header, offset))
9110 {
9111 /* As DW_OP_GNU_parameter_ref uses CU-relative offset this
9112 binding can be done only inside one CU. Such referenced DIE
9113 therefore cannot be even moved to DW_TAG_partial_unit. */
9114 complaint (&symfile_complaints,
9115 _("DW_AT_abstract_origin offset is not in CU for "
9116 "DW_TAG_GNU_call_site child DIE 0x%x "
9117 "[in module %s]"),
9118 child_die->offset.sect_off, objfile->name);
9119 continue;
9120 }
1788b2d3
JK
9121 parameter->u.param_offset.cu_off = (offset.sect_off
9122 - cu->header.offset.sect_off);
9123 }
9124 else if (loc == NULL || origin != NULL || !attr_form_is_block (loc))
96408a79
SA
9125 {
9126 complaint (&symfile_complaints,
9127 _("No DW_FORM_block* DW_AT_location for "
9128 "DW_TAG_GNU_call_site child DIE 0x%x [in module %s]"),
b64f50a1 9129 child_die->offset.sect_off, objfile->name);
96408a79
SA
9130 continue;
9131 }
24c5c679 9132 else
96408a79 9133 {
24c5c679
JK
9134 parameter->u.dwarf_reg = dwarf_block_to_dwarf_reg
9135 (DW_BLOCK (loc)->data, &DW_BLOCK (loc)->data[DW_BLOCK (loc)->size]);
9136 if (parameter->u.dwarf_reg != -1)
9137 parameter->kind = CALL_SITE_PARAMETER_DWARF_REG;
9138 else if (dwarf_block_to_sp_offset (gdbarch, DW_BLOCK (loc)->data,
9139 &DW_BLOCK (loc)->data[DW_BLOCK (loc)->size],
9140 &parameter->u.fb_offset))
9141 parameter->kind = CALL_SITE_PARAMETER_FB_OFFSET;
9142 else
9143 {
9144 complaint (&symfile_complaints,
9145 _("Only single DW_OP_reg or DW_OP_fbreg is supported "
9146 "for DW_FORM_block* DW_AT_location is supported for "
9147 "DW_TAG_GNU_call_site child DIE 0x%x "
9148 "[in module %s]"),
9149 child_die->offset.sect_off, objfile->name);
9150 continue;
9151 }
96408a79
SA
9152 }
9153
9154 attr = dwarf2_attr (child_die, DW_AT_GNU_call_site_value, cu);
9155 if (!attr_form_is_block (attr))
9156 {
9157 complaint (&symfile_complaints,
9158 _("No DW_FORM_block* DW_AT_GNU_call_site_value for "
9159 "DW_TAG_GNU_call_site child DIE 0x%x [in module %s]"),
b64f50a1 9160 child_die->offset.sect_off, objfile->name);
96408a79
SA
9161 continue;
9162 }
9163 parameter->value = DW_BLOCK (attr)->data;
9164 parameter->value_size = DW_BLOCK (attr)->size;
9165
9166 /* Parameters are not pre-cleared by memset above. */
9167 parameter->data_value = NULL;
9168 parameter->data_value_size = 0;
9169 call_site->parameter_count++;
9170
9171 attr = dwarf2_attr (child_die, DW_AT_GNU_call_site_data_value, cu);
9172 if (attr)
9173 {
9174 if (!attr_form_is_block (attr))
9175 complaint (&symfile_complaints,
9176 _("No DW_FORM_block* DW_AT_GNU_call_site_data_value for "
9177 "DW_TAG_GNU_call_site child DIE 0x%x [in module %s]"),
b64f50a1 9178 child_die->offset.sect_off, objfile->name);
96408a79
SA
9179 else
9180 {
9181 parameter->data_value = DW_BLOCK (attr)->data;
9182 parameter->data_value_size = DW_BLOCK (attr)->size;
9183 }
9184 }
9185 }
9186}
9187
43039443 9188/* Get low and high pc attributes from DW_AT_ranges attribute value OFFSET.
ff013f42
JK
9189 Return 1 if the attributes are present and valid, otherwise, return 0.
9190 If RANGES_PST is not NULL we should setup `objfile->psymtabs_addrmap'. */
43039443
JK
9191
9192static int
9193dwarf2_ranges_read (unsigned offset, CORE_ADDR *low_return,
ff013f42
JK
9194 CORE_ADDR *high_return, struct dwarf2_cu *cu,
9195 struct partial_symtab *ranges_pst)
43039443
JK
9196{
9197 struct objfile *objfile = cu->objfile;
9198 struct comp_unit_head *cu_header = &cu->header;
9199 bfd *obfd = objfile->obfd;
9200 unsigned int addr_size = cu_header->addr_size;
9201 CORE_ADDR mask = ~(~(CORE_ADDR)1 << (addr_size * 8 - 1));
9202 /* Base address selection entry. */
9203 CORE_ADDR base;
9204 int found_base;
9205 unsigned int dummy;
9206 gdb_byte *buffer;
9207 CORE_ADDR marker;
9208 int low_set;
9209 CORE_ADDR low = 0;
9210 CORE_ADDR high = 0;
ff013f42 9211 CORE_ADDR baseaddr;
43039443 9212
d00adf39
DE
9213 found_base = cu->base_known;
9214 base = cu->base_address;
43039443 9215
be391dca 9216 dwarf2_read_section (objfile, &dwarf2_per_objfile->ranges);
dce234bc 9217 if (offset >= dwarf2_per_objfile->ranges.size)
43039443
JK
9218 {
9219 complaint (&symfile_complaints,
9220 _("Offset %d out of bounds for DW_AT_ranges attribute"),
9221 offset);
9222 return 0;
9223 }
dce234bc 9224 buffer = dwarf2_per_objfile->ranges.buffer + offset;
43039443
JK
9225
9226 /* Read in the largest possible address. */
9227 marker = read_address (obfd, buffer, cu, &dummy);
9228 if ((marker & mask) == mask)
9229 {
9230 /* If we found the largest possible address, then
9231 read the base address. */
9232 base = read_address (obfd, buffer + addr_size, cu, &dummy);
9233 buffer += 2 * addr_size;
9234 offset += 2 * addr_size;
9235 found_base = 1;
9236 }
9237
9238 low_set = 0;
9239
e7030f15 9240 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
ff013f42 9241
43039443
JK
9242 while (1)
9243 {
9244 CORE_ADDR range_beginning, range_end;
9245
9246 range_beginning = read_address (obfd, buffer, cu, &dummy);
9247 buffer += addr_size;
9248 range_end = read_address (obfd, buffer, cu, &dummy);
9249 buffer += addr_size;
9250 offset += 2 * addr_size;
9251
9252 /* An end of list marker is a pair of zero addresses. */
9253 if (range_beginning == 0 && range_end == 0)
9254 /* Found the end of list entry. */
9255 break;
9256
9257 /* Each base address selection entry is a pair of 2 values.
9258 The first is the largest possible address, the second is
9259 the base address. Check for a base address here. */
9260 if ((range_beginning & mask) == mask)
9261 {
9262 /* If we found the largest possible address, then
9263 read the base address. */
9264 base = read_address (obfd, buffer + addr_size, cu, &dummy);
9265 found_base = 1;
9266 continue;
9267 }
9268
9269 if (!found_base)
9270 {
9271 /* We have no valid base address for the ranges
9272 data. */
9273 complaint (&symfile_complaints,
9274 _("Invalid .debug_ranges data (no base address)"));
9275 return 0;
9276 }
9277
9277c30c
UW
9278 if (range_beginning > range_end)
9279 {
9280 /* Inverted range entries are invalid. */
9281 complaint (&symfile_complaints,
9282 _("Invalid .debug_ranges data (inverted range)"));
9283 return 0;
9284 }
9285
9286 /* Empty range entries have no effect. */
9287 if (range_beginning == range_end)
9288 continue;
9289
43039443
JK
9290 range_beginning += base;
9291 range_end += base;
9292
01093045
DE
9293 /* A not-uncommon case of bad debug info.
9294 Don't pollute the addrmap with bad data. */
9295 if (range_beginning + baseaddr == 0
9296 && !dwarf2_per_objfile->has_section_at_zero)
9297 {
9298 complaint (&symfile_complaints,
9299 _(".debug_ranges entry has start address of zero"
9300 " [in module %s]"), objfile->name);
9301 continue;
9302 }
9303
9277c30c 9304 if (ranges_pst != NULL)
ff013f42 9305 addrmap_set_empty (objfile->psymtabs_addrmap,
3e43a32a
MS
9306 range_beginning + baseaddr,
9307 range_end - 1 + baseaddr,
ff013f42
JK
9308 ranges_pst);
9309
43039443
JK
9310 /* FIXME: This is recording everything as a low-high
9311 segment of consecutive addresses. We should have a
9312 data structure for discontiguous block ranges
9313 instead. */
9314 if (! low_set)
9315 {
9316 low = range_beginning;
9317 high = range_end;
9318 low_set = 1;
9319 }
9320 else
9321 {
9322 if (range_beginning < low)
9323 low = range_beginning;
9324 if (range_end > high)
9325 high = range_end;
9326 }
9327 }
9328
9329 if (! low_set)
9330 /* If the first entry is an end-of-list marker, the range
9331 describes an empty scope, i.e. no instructions. */
9332 return 0;
9333
9334 if (low_return)
9335 *low_return = low;
9336 if (high_return)
9337 *high_return = high;
9338 return 1;
9339}
9340
af34e669
DJ
9341/* Get low and high pc attributes from a die. Return 1 if the attributes
9342 are present and valid, otherwise, return 0. Return -1 if the range is
9343 discontinuous, i.e. derived from DW_AT_ranges information. */
380bca97 9344
c906108c 9345static int
af34e669 9346dwarf2_get_pc_bounds (struct die_info *die, CORE_ADDR *lowpc,
d85a05f0
DJ
9347 CORE_ADDR *highpc, struct dwarf2_cu *cu,
9348 struct partial_symtab *pst)
c906108c
SS
9349{
9350 struct attribute *attr;
91da1414 9351 struct attribute *attr_high;
af34e669
DJ
9352 CORE_ADDR low = 0;
9353 CORE_ADDR high = 0;
9354 int ret = 0;
c906108c 9355
91da1414
MW
9356 attr_high = dwarf2_attr (die, DW_AT_high_pc, cu);
9357 if (attr_high)
af34e669 9358 {
e142c38c 9359 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
af34e669 9360 if (attr)
91da1414
MW
9361 {
9362 low = DW_ADDR (attr);
3019eac3
DE
9363 if (attr_high->form == DW_FORM_addr
9364 || attr_high->form == DW_FORM_GNU_addr_index)
91da1414
MW
9365 high = DW_ADDR (attr_high);
9366 else
9367 high = low + DW_UNSND (attr_high);
9368 }
af34e669
DJ
9369 else
9370 /* Found high w/o low attribute. */
9371 return 0;
9372
9373 /* Found consecutive range of addresses. */
9374 ret = 1;
9375 }
c906108c 9376 else
af34e669 9377 {
e142c38c 9378 attr = dwarf2_attr (die, DW_AT_ranges, cu);
af34e669
DJ
9379 if (attr != NULL)
9380 {
2e3cf129
DE
9381 unsigned int ranges_offset = DW_UNSND (attr) + cu->ranges_base;
9382
af34e669 9383 /* Value of the DW_AT_ranges attribute is the offset in the
a604369a 9384 .debug_ranges section. */
2e3cf129 9385 if (!dwarf2_ranges_read (ranges_offset, &low, &high, cu, pst))
af34e669 9386 return 0;
43039443 9387 /* Found discontinuous range of addresses. */
af34e669
DJ
9388 ret = -1;
9389 }
9390 }
c906108c 9391
9373cf26
JK
9392 /* read_partial_die has also the strict LOW < HIGH requirement. */
9393 if (high <= low)
c906108c
SS
9394 return 0;
9395
9396 /* When using the GNU linker, .gnu.linkonce. sections are used to
9397 eliminate duplicate copies of functions and vtables and such.
9398 The linker will arbitrarily choose one and discard the others.
9399 The AT_*_pc values for such functions refer to local labels in
9400 these sections. If the section from that file was discarded, the
9401 labels are not in the output, so the relocs get a value of 0.
9402 If this is a discarded function, mark the pc bounds as invalid,
9403 so that GDB will ignore it. */
72dca2f5 9404 if (low == 0 && !dwarf2_per_objfile->has_section_at_zero)
c906108c
SS
9405 return 0;
9406
9407 *lowpc = low;
96408a79
SA
9408 if (highpc)
9409 *highpc = high;
af34e669 9410 return ret;
c906108c
SS
9411}
9412
b084d499
JB
9413/* Assuming that DIE represents a subprogram DIE or a lexical block, get
9414 its low and high PC addresses. Do nothing if these addresses could not
9415 be determined. Otherwise, set LOWPC to the low address if it is smaller,
9416 and HIGHPC to the high address if greater than HIGHPC. */
9417
9418static void
9419dwarf2_get_subprogram_pc_bounds (struct die_info *die,
9420 CORE_ADDR *lowpc, CORE_ADDR *highpc,
9421 struct dwarf2_cu *cu)
9422{
9423 CORE_ADDR low, high;
9424 struct die_info *child = die->child;
9425
d85a05f0 9426 if (dwarf2_get_pc_bounds (die, &low, &high, cu, NULL))
b084d499
JB
9427 {
9428 *lowpc = min (*lowpc, low);
9429 *highpc = max (*highpc, high);
9430 }
9431
9432 /* If the language does not allow nested subprograms (either inside
9433 subprograms or lexical blocks), we're done. */
9434 if (cu->language != language_ada)
9435 return;
6e70227d 9436
b084d499
JB
9437 /* Check all the children of the given DIE. If it contains nested
9438 subprograms, then check their pc bounds. Likewise, we need to
9439 check lexical blocks as well, as they may also contain subprogram
9440 definitions. */
9441 while (child && child->tag)
9442 {
9443 if (child->tag == DW_TAG_subprogram
9444 || child->tag == DW_TAG_lexical_block)
9445 dwarf2_get_subprogram_pc_bounds (child, lowpc, highpc, cu);
9446 child = sibling_die (child);
9447 }
9448}
9449
fae299cd
DC
9450/* Get the low and high pc's represented by the scope DIE, and store
9451 them in *LOWPC and *HIGHPC. If the correct values can't be
9452 determined, set *LOWPC to -1 and *HIGHPC to 0. */
9453
9454static void
9455get_scope_pc_bounds (struct die_info *die,
9456 CORE_ADDR *lowpc, CORE_ADDR *highpc,
9457 struct dwarf2_cu *cu)
9458{
9459 CORE_ADDR best_low = (CORE_ADDR) -1;
9460 CORE_ADDR best_high = (CORE_ADDR) 0;
9461 CORE_ADDR current_low, current_high;
9462
d85a05f0 9463 if (dwarf2_get_pc_bounds (die, &current_low, &current_high, cu, NULL))
fae299cd
DC
9464 {
9465 best_low = current_low;
9466 best_high = current_high;
9467 }
9468 else
9469 {
9470 struct die_info *child = die->child;
9471
9472 while (child && child->tag)
9473 {
9474 switch (child->tag) {
9475 case DW_TAG_subprogram:
b084d499 9476 dwarf2_get_subprogram_pc_bounds (child, &best_low, &best_high, cu);
fae299cd
DC
9477 break;
9478 case DW_TAG_namespace:
f55ee35c 9479 case DW_TAG_module:
fae299cd
DC
9480 /* FIXME: carlton/2004-01-16: Should we do this for
9481 DW_TAG_class_type/DW_TAG_structure_type, too? I think
9482 that current GCC's always emit the DIEs corresponding
9483 to definitions of methods of classes as children of a
9484 DW_TAG_compile_unit or DW_TAG_namespace (as opposed to
9485 the DIEs giving the declarations, which could be
9486 anywhere). But I don't see any reason why the
9487 standards says that they have to be there. */
9488 get_scope_pc_bounds (child, &current_low, &current_high, cu);
9489
9490 if (current_low != ((CORE_ADDR) -1))
9491 {
9492 best_low = min (best_low, current_low);
9493 best_high = max (best_high, current_high);
9494 }
9495 break;
9496 default:
0963b4bd 9497 /* Ignore. */
fae299cd
DC
9498 break;
9499 }
9500
9501 child = sibling_die (child);
9502 }
9503 }
9504
9505 *lowpc = best_low;
9506 *highpc = best_high;
9507}
9508
801e3a5b
JB
9509/* Record the address ranges for BLOCK, offset by BASEADDR, as given
9510 in DIE. */
380bca97 9511
801e3a5b
JB
9512static void
9513dwarf2_record_block_ranges (struct die_info *die, struct block *block,
9514 CORE_ADDR baseaddr, struct dwarf2_cu *cu)
9515{
bb5ed363 9516 struct objfile *objfile = cu->objfile;
801e3a5b 9517 struct attribute *attr;
91da1414 9518 struct attribute *attr_high;
801e3a5b 9519
91da1414
MW
9520 attr_high = dwarf2_attr (die, DW_AT_high_pc, cu);
9521 if (attr_high)
801e3a5b 9522 {
801e3a5b
JB
9523 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
9524 if (attr)
9525 {
9526 CORE_ADDR low = DW_ADDR (attr);
91da1414 9527 CORE_ADDR high;
3019eac3
DE
9528 if (attr_high->form == DW_FORM_addr
9529 || attr_high->form == DW_FORM_GNU_addr_index)
91da1414
MW
9530 high = DW_ADDR (attr_high);
9531 else
9532 high = low + DW_UNSND (attr_high);
9a619af0 9533
801e3a5b
JB
9534 record_block_range (block, baseaddr + low, baseaddr + high - 1);
9535 }
9536 }
9537
9538 attr = dwarf2_attr (die, DW_AT_ranges, cu);
9539 if (attr)
9540 {
bb5ed363 9541 bfd *obfd = objfile->obfd;
801e3a5b
JB
9542
9543 /* The value of the DW_AT_ranges attribute is the offset of the
9544 address range list in the .debug_ranges section. */
2e3cf129 9545 unsigned long offset = DW_UNSND (attr) + cu->ranges_base;
dce234bc 9546 gdb_byte *buffer = dwarf2_per_objfile->ranges.buffer + offset;
801e3a5b
JB
9547
9548 /* For some target architectures, but not others, the
9549 read_address function sign-extends the addresses it returns.
9550 To recognize base address selection entries, we need a
9551 mask. */
9552 unsigned int addr_size = cu->header.addr_size;
9553 CORE_ADDR base_select_mask = ~(~(CORE_ADDR)1 << (addr_size * 8 - 1));
9554
9555 /* The base address, to which the next pair is relative. Note
9556 that this 'base' is a DWARF concept: most entries in a range
9557 list are relative, to reduce the number of relocs against the
9558 debugging information. This is separate from this function's
9559 'baseaddr' argument, which GDB uses to relocate debugging
9560 information from a shared library based on the address at
9561 which the library was loaded. */
d00adf39
DE
9562 CORE_ADDR base = cu->base_address;
9563 int base_known = cu->base_known;
801e3a5b 9564
be391dca 9565 gdb_assert (dwarf2_per_objfile->ranges.readin);
dce234bc 9566 if (offset >= dwarf2_per_objfile->ranges.size)
801e3a5b
JB
9567 {
9568 complaint (&symfile_complaints,
9569 _("Offset %lu out of bounds for DW_AT_ranges attribute"),
9570 offset);
9571 return;
9572 }
9573
9574 for (;;)
9575 {
9576 unsigned int bytes_read;
9577 CORE_ADDR start, end;
9578
9579 start = read_address (obfd, buffer, cu, &bytes_read);
9580 buffer += bytes_read;
9581 end = read_address (obfd, buffer, cu, &bytes_read);
9582 buffer += bytes_read;
9583
9584 /* Did we find the end of the range list? */
9585 if (start == 0 && end == 0)
9586 break;
9587
9588 /* Did we find a base address selection entry? */
9589 else if ((start & base_select_mask) == base_select_mask)
9590 {
9591 base = end;
9592 base_known = 1;
9593 }
9594
9595 /* We found an ordinary address range. */
9596 else
9597 {
9598 if (!base_known)
9599 {
9600 complaint (&symfile_complaints,
3e43a32a
MS
9601 _("Invalid .debug_ranges data "
9602 "(no base address)"));
801e3a5b
JB
9603 return;
9604 }
9605
9277c30c
UW
9606 if (start > end)
9607 {
9608 /* Inverted range entries are invalid. */
9609 complaint (&symfile_complaints,
9610 _("Invalid .debug_ranges data "
9611 "(inverted range)"));
9612 return;
9613 }
9614
9615 /* Empty range entries have no effect. */
9616 if (start == end)
9617 continue;
9618
01093045
DE
9619 start += base + baseaddr;
9620 end += base + baseaddr;
9621
9622 /* A not-uncommon case of bad debug info.
9623 Don't pollute the addrmap with bad data. */
9624 if (start == 0 && !dwarf2_per_objfile->has_section_at_zero)
9625 {
9626 complaint (&symfile_complaints,
9627 _(".debug_ranges entry has start address of zero"
9628 " [in module %s]"), objfile->name);
9629 continue;
9630 }
9631
9632 record_block_range (block, start, end - 1);
801e3a5b
JB
9633 }
9634 }
9635 }
9636}
9637
685b1105
JK
9638/* Check whether the producer field indicates either of GCC < 4.6, or the
9639 Intel C/C++ compiler, and cache the result in CU. */
60d5a603 9640
685b1105
JK
9641static void
9642check_producer (struct dwarf2_cu *cu)
60d5a603
JK
9643{
9644 const char *cs;
9645 int major, minor, release;
9646
9647 if (cu->producer == NULL)
9648 {
9649 /* For unknown compilers expect their behavior is DWARF version
9650 compliant.
9651
9652 GCC started to support .debug_types sections by -gdwarf-4 since
9653 gcc-4.5.x. As the .debug_types sections are missing DW_AT_producer
9654 for their space efficiency GDB cannot workaround gcc-4.5.x -gdwarf-4
9655 combination. gcc-4.5.x -gdwarf-4 binaries have DW_AT_accessibility
9656 interpreted incorrectly by GDB now - GCC PR debug/48229. */
60d5a603 9657 }
685b1105 9658 else if (strncmp (cu->producer, "GNU ", strlen ("GNU ")) == 0)
60d5a603 9659 {
685b1105
JK
9660 /* Skip any identifier after "GNU " - such as "C++" or "Java". */
9661
ba919b58
TT
9662 cs = &cu->producer[strlen ("GNU ")];
9663 while (*cs && !isdigit (*cs))
9664 cs++;
9665 if (sscanf (cs, "%d.%d.%d", &major, &minor, &release) != 3)
9666 {
9667 /* Not recognized as GCC. */
9668 }
9669 else
685b1105
JK
9670 cu->producer_is_gxx_lt_4_6 = major < 4 || (major == 4 && minor < 6);
9671 }
9672 else if (strncmp (cu->producer, "Intel(R) C", strlen ("Intel(R) C")) == 0)
9673 cu->producer_is_icc = 1;
9674 else
9675 {
9676 /* For other non-GCC compilers, expect their behavior is DWARF version
9677 compliant. */
60d5a603
JK
9678 }
9679
ba919b58 9680 cu->checked_producer = 1;
685b1105 9681}
ba919b58 9682
685b1105
JK
9683/* Check for GCC PR debug/45124 fix which is not present in any G++ version up
9684 to 4.5.any while it is present already in G++ 4.6.0 - the PR has been fixed
9685 during 4.6.0 experimental. */
9686
9687static int
9688producer_is_gxx_lt_4_6 (struct dwarf2_cu *cu)
9689{
9690 if (!cu->checked_producer)
9691 check_producer (cu);
9692
9693 return cu->producer_is_gxx_lt_4_6;
60d5a603
JK
9694}
9695
9696/* Return the default accessibility type if it is not overriden by
9697 DW_AT_accessibility. */
9698
9699static enum dwarf_access_attribute
9700dwarf2_default_access_attribute (struct die_info *die, struct dwarf2_cu *cu)
9701{
9702 if (cu->header.version < 3 || producer_is_gxx_lt_4_6 (cu))
9703 {
9704 /* The default DWARF 2 accessibility for members is public, the default
9705 accessibility for inheritance is private. */
9706
9707 if (die->tag != DW_TAG_inheritance)
9708 return DW_ACCESS_public;
9709 else
9710 return DW_ACCESS_private;
9711 }
9712 else
9713 {
9714 /* DWARF 3+ defines the default accessibility a different way. The same
9715 rules apply now for DW_TAG_inheritance as for the members and it only
9716 depends on the container kind. */
9717
9718 if (die->parent->tag == DW_TAG_class_type)
9719 return DW_ACCESS_private;
9720 else
9721 return DW_ACCESS_public;
9722 }
9723}
9724
74ac6d43
TT
9725/* Look for DW_AT_data_member_location. Set *OFFSET to the byte
9726 offset. If the attribute was not found return 0, otherwise return
9727 1. If it was found but could not properly be handled, set *OFFSET
9728 to 0. */
9729
9730static int
9731handle_data_member_location (struct die_info *die, struct dwarf2_cu *cu,
9732 LONGEST *offset)
9733{
9734 struct attribute *attr;
9735
9736 attr = dwarf2_attr (die, DW_AT_data_member_location, cu);
9737 if (attr != NULL)
9738 {
9739 *offset = 0;
9740
9741 /* Note that we do not check for a section offset first here.
9742 This is because DW_AT_data_member_location is new in DWARF 4,
9743 so if we see it, we can assume that a constant form is really
9744 a constant and not a section offset. */
9745 if (attr_form_is_constant (attr))
9746 *offset = dwarf2_get_attr_constant_value (attr, 0);
9747 else if (attr_form_is_section_offset (attr))
9748 dwarf2_complex_location_expr_complaint ();
9749 else if (attr_form_is_block (attr))
9750 *offset = decode_locdesc (DW_BLOCK (attr), cu);
9751 else
9752 dwarf2_complex_location_expr_complaint ();
9753
9754 return 1;
9755 }
9756
9757 return 0;
9758}
9759
c906108c
SS
9760/* Add an aggregate field to the field list. */
9761
9762static void
107d2387 9763dwarf2_add_field (struct field_info *fip, struct die_info *die,
e7c27a73 9764 struct dwarf2_cu *cu)
6e70227d 9765{
e7c27a73 9766 struct objfile *objfile = cu->objfile;
5e2b427d 9767 struct gdbarch *gdbarch = get_objfile_arch (objfile);
c906108c
SS
9768 struct nextfield *new_field;
9769 struct attribute *attr;
9770 struct field *fp;
9771 char *fieldname = "";
9772
9773 /* Allocate a new field list entry and link it in. */
9774 new_field = (struct nextfield *) xmalloc (sizeof (struct nextfield));
b8c9b27d 9775 make_cleanup (xfree, new_field);
c906108c 9776 memset (new_field, 0, sizeof (struct nextfield));
7d0ccb61
DJ
9777
9778 if (die->tag == DW_TAG_inheritance)
9779 {
9780 new_field->next = fip->baseclasses;
9781 fip->baseclasses = new_field;
9782 }
9783 else
9784 {
9785 new_field->next = fip->fields;
9786 fip->fields = new_field;
9787 }
c906108c
SS
9788 fip->nfields++;
9789
e142c38c 9790 attr = dwarf2_attr (die, DW_AT_accessibility, cu);
c906108c
SS
9791 if (attr)
9792 new_field->accessibility = DW_UNSND (attr);
60d5a603
JK
9793 else
9794 new_field->accessibility = dwarf2_default_access_attribute (die, cu);
c906108c
SS
9795 if (new_field->accessibility != DW_ACCESS_public)
9796 fip->non_public_fields = 1;
60d5a603 9797
e142c38c 9798 attr = dwarf2_attr (die, DW_AT_virtuality, cu);
c906108c
SS
9799 if (attr)
9800 new_field->virtuality = DW_UNSND (attr);
60d5a603
JK
9801 else
9802 new_field->virtuality = DW_VIRTUALITY_none;
c906108c
SS
9803
9804 fp = &new_field->field;
a9a9bd0f 9805
e142c38c 9806 if (die->tag == DW_TAG_member && ! die_is_declaration (die, cu))
c906108c 9807 {
74ac6d43
TT
9808 LONGEST offset;
9809
a9a9bd0f 9810 /* Data member other than a C++ static data member. */
6e70227d 9811
c906108c 9812 /* Get type of field. */
e7c27a73 9813 fp->type = die_type (die, cu);
c906108c 9814
d6a843b5 9815 SET_FIELD_BITPOS (*fp, 0);
01ad7f36 9816
c906108c 9817 /* Get bit size of field (zero if none). */
e142c38c 9818 attr = dwarf2_attr (die, DW_AT_bit_size, cu);
c906108c
SS
9819 if (attr)
9820 {
9821 FIELD_BITSIZE (*fp) = DW_UNSND (attr);
9822 }
9823 else
9824 {
9825 FIELD_BITSIZE (*fp) = 0;
9826 }
9827
9828 /* Get bit offset of field. */
74ac6d43
TT
9829 if (handle_data_member_location (die, cu, &offset))
9830 SET_FIELD_BITPOS (*fp, offset * bits_per_byte);
e142c38c 9831 attr = dwarf2_attr (die, DW_AT_bit_offset, cu);
c906108c
SS
9832 if (attr)
9833 {
5e2b427d 9834 if (gdbarch_bits_big_endian (gdbarch))
c906108c
SS
9835 {
9836 /* For big endian bits, the DW_AT_bit_offset gives the
c5aa993b
JM
9837 additional bit offset from the MSB of the containing
9838 anonymous object to the MSB of the field. We don't
9839 have to do anything special since we don't need to
9840 know the size of the anonymous object. */
f41f5e61 9841 SET_FIELD_BITPOS (*fp, FIELD_BITPOS (*fp) + DW_UNSND (attr));
c906108c
SS
9842 }
9843 else
9844 {
9845 /* For little endian bits, compute the bit offset to the
c5aa993b
JM
9846 MSB of the anonymous object, subtract off the number of
9847 bits from the MSB of the field to the MSB of the
9848 object, and then subtract off the number of bits of
9849 the field itself. The result is the bit offset of
9850 the LSB of the field. */
c906108c
SS
9851 int anonymous_size;
9852 int bit_offset = DW_UNSND (attr);
9853
e142c38c 9854 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
c906108c
SS
9855 if (attr)
9856 {
9857 /* The size of the anonymous object containing
9858 the bit field is explicit, so use the
9859 indicated size (in bytes). */
9860 anonymous_size = DW_UNSND (attr);
9861 }
9862 else
9863 {
9864 /* The size of the anonymous object containing
9865 the bit field must be inferred from the type
9866 attribute of the data member containing the
9867 bit field. */
9868 anonymous_size = TYPE_LENGTH (fp->type);
9869 }
f41f5e61
PA
9870 SET_FIELD_BITPOS (*fp,
9871 (FIELD_BITPOS (*fp)
9872 + anonymous_size * bits_per_byte
9873 - bit_offset - FIELD_BITSIZE (*fp)));
c906108c
SS
9874 }
9875 }
9876
9877 /* Get name of field. */
39cbfefa
DJ
9878 fieldname = dwarf2_name (die, cu);
9879 if (fieldname == NULL)
9880 fieldname = "";
d8151005
DJ
9881
9882 /* The name is already allocated along with this objfile, so we don't
9883 need to duplicate it for the type. */
9884 fp->name = fieldname;
c906108c
SS
9885
9886 /* Change accessibility for artificial fields (e.g. virtual table
c5aa993b 9887 pointer or virtual base class pointer) to private. */
e142c38c 9888 if (dwarf2_attr (die, DW_AT_artificial, cu))
c906108c 9889 {
d48cc9dd 9890 FIELD_ARTIFICIAL (*fp) = 1;
c906108c
SS
9891 new_field->accessibility = DW_ACCESS_private;
9892 fip->non_public_fields = 1;
9893 }
9894 }
a9a9bd0f 9895 else if (die->tag == DW_TAG_member || die->tag == DW_TAG_variable)
c906108c 9896 {
a9a9bd0f
DC
9897 /* C++ static member. */
9898
9899 /* NOTE: carlton/2002-11-05: It should be a DW_TAG_member that
9900 is a declaration, but all versions of G++ as of this writing
9901 (so through at least 3.2.1) incorrectly generate
9902 DW_TAG_variable tags. */
6e70227d 9903
ff355380 9904 const char *physname;
c906108c 9905
a9a9bd0f 9906 /* Get name of field. */
39cbfefa
DJ
9907 fieldname = dwarf2_name (die, cu);
9908 if (fieldname == NULL)
c906108c
SS
9909 return;
9910
254e6b9e 9911 attr = dwarf2_attr (die, DW_AT_const_value, cu);
3863f96c
DE
9912 if (attr
9913 /* Only create a symbol if this is an external value.
9914 new_symbol checks this and puts the value in the global symbol
9915 table, which we want. If it is not external, new_symbol
9916 will try to put the value in cu->list_in_scope which is wrong. */
9917 && dwarf2_flag_true_p (die, DW_AT_external, cu))
254e6b9e
DE
9918 {
9919 /* A static const member, not much different than an enum as far as
9920 we're concerned, except that we can support more types. */
9921 new_symbol (die, NULL, cu);
9922 }
9923
2df3850c 9924 /* Get physical name. */
ff355380 9925 physname = dwarf2_physname (fieldname, die, cu);
c906108c 9926
d8151005
DJ
9927 /* The name is already allocated along with this objfile, so we don't
9928 need to duplicate it for the type. */
9929 SET_FIELD_PHYSNAME (*fp, physname ? physname : "");
e7c27a73 9930 FIELD_TYPE (*fp) = die_type (die, cu);
d8151005 9931 FIELD_NAME (*fp) = fieldname;
c906108c
SS
9932 }
9933 else if (die->tag == DW_TAG_inheritance)
9934 {
74ac6d43 9935 LONGEST offset;
d4b96c9a 9936
74ac6d43
TT
9937 /* C++ base class field. */
9938 if (handle_data_member_location (die, cu, &offset))
9939 SET_FIELD_BITPOS (*fp, offset * bits_per_byte);
c906108c 9940 FIELD_BITSIZE (*fp) = 0;
e7c27a73 9941 FIELD_TYPE (*fp) = die_type (die, cu);
c906108c
SS
9942 FIELD_NAME (*fp) = type_name_no_tag (fp->type);
9943 fip->nbaseclasses++;
9944 }
9945}
9946
98751a41
JK
9947/* Add a typedef defined in the scope of the FIP's class. */
9948
9949static void
9950dwarf2_add_typedef (struct field_info *fip, struct die_info *die,
9951 struct dwarf2_cu *cu)
6e70227d 9952{
98751a41 9953 struct objfile *objfile = cu->objfile;
98751a41
JK
9954 struct typedef_field_list *new_field;
9955 struct attribute *attr;
9956 struct typedef_field *fp;
9957 char *fieldname = "";
9958
9959 /* Allocate a new field list entry and link it in. */
9960 new_field = xzalloc (sizeof (*new_field));
9961 make_cleanup (xfree, new_field);
9962
9963 gdb_assert (die->tag == DW_TAG_typedef);
9964
9965 fp = &new_field->field;
9966
9967 /* Get name of field. */
9968 fp->name = dwarf2_name (die, cu);
9969 if (fp->name == NULL)
9970 return;
9971
9972 fp->type = read_type_die (die, cu);
9973
9974 new_field->next = fip->typedef_field_list;
9975 fip->typedef_field_list = new_field;
9976 fip->typedef_field_list_count++;
9977}
9978
c906108c
SS
9979/* Create the vector of fields, and attach it to the type. */
9980
9981static void
fba45db2 9982dwarf2_attach_fields_to_type (struct field_info *fip, struct type *type,
e7c27a73 9983 struct dwarf2_cu *cu)
c906108c
SS
9984{
9985 int nfields = fip->nfields;
9986
9987 /* Record the field count, allocate space for the array of fields,
9988 and create blank accessibility bitfields if necessary. */
9989 TYPE_NFIELDS (type) = nfields;
9990 TYPE_FIELDS (type) = (struct field *)
9991 TYPE_ALLOC (type, sizeof (struct field) * nfields);
9992 memset (TYPE_FIELDS (type), 0, sizeof (struct field) * nfields);
9993
b4ba55a1 9994 if (fip->non_public_fields && cu->language != language_ada)
c906108c
SS
9995 {
9996 ALLOCATE_CPLUS_STRUCT_TYPE (type);
9997
9998 TYPE_FIELD_PRIVATE_BITS (type) =
9999 (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
10000 B_CLRALL (TYPE_FIELD_PRIVATE_BITS (type), nfields);
10001
10002 TYPE_FIELD_PROTECTED_BITS (type) =
10003 (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
10004 B_CLRALL (TYPE_FIELD_PROTECTED_BITS (type), nfields);
10005
774b6a14
TT
10006 TYPE_FIELD_IGNORE_BITS (type) =
10007 (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
10008 B_CLRALL (TYPE_FIELD_IGNORE_BITS (type), nfields);
c906108c
SS
10009 }
10010
10011 /* If the type has baseclasses, allocate and clear a bit vector for
10012 TYPE_FIELD_VIRTUAL_BITS. */
b4ba55a1 10013 if (fip->nbaseclasses && cu->language != language_ada)
c906108c
SS
10014 {
10015 int num_bytes = B_BYTES (fip->nbaseclasses);
fe1b8b76 10016 unsigned char *pointer;
c906108c
SS
10017
10018 ALLOCATE_CPLUS_STRUCT_TYPE (type);
fe1b8b76
JB
10019 pointer = TYPE_ALLOC (type, num_bytes);
10020 TYPE_FIELD_VIRTUAL_BITS (type) = pointer;
c906108c
SS
10021 B_CLRALL (TYPE_FIELD_VIRTUAL_BITS (type), fip->nbaseclasses);
10022 TYPE_N_BASECLASSES (type) = fip->nbaseclasses;
10023 }
10024
3e43a32a
MS
10025 /* Copy the saved-up fields into the field vector. Start from the head of
10026 the list, adding to the tail of the field array, so that they end up in
10027 the same order in the array in which they were added to the list. */
c906108c
SS
10028 while (nfields-- > 0)
10029 {
7d0ccb61
DJ
10030 struct nextfield *fieldp;
10031
10032 if (fip->fields)
10033 {
10034 fieldp = fip->fields;
10035 fip->fields = fieldp->next;
10036 }
10037 else
10038 {
10039 fieldp = fip->baseclasses;
10040 fip->baseclasses = fieldp->next;
10041 }
10042
10043 TYPE_FIELD (type, nfields) = fieldp->field;
10044 switch (fieldp->accessibility)
c906108c 10045 {
c5aa993b 10046 case DW_ACCESS_private:
b4ba55a1
JB
10047 if (cu->language != language_ada)
10048 SET_TYPE_FIELD_PRIVATE (type, nfields);
c5aa993b 10049 break;
c906108c 10050
c5aa993b 10051 case DW_ACCESS_protected:
b4ba55a1
JB
10052 if (cu->language != language_ada)
10053 SET_TYPE_FIELD_PROTECTED (type, nfields);
c5aa993b 10054 break;
c906108c 10055
c5aa993b
JM
10056 case DW_ACCESS_public:
10057 break;
c906108c 10058
c5aa993b
JM
10059 default:
10060 /* Unknown accessibility. Complain and treat it as public. */
10061 {
e2e0b3e5 10062 complaint (&symfile_complaints, _("unsupported accessibility %d"),
7d0ccb61 10063 fieldp->accessibility);
c5aa993b
JM
10064 }
10065 break;
c906108c
SS
10066 }
10067 if (nfields < fip->nbaseclasses)
10068 {
7d0ccb61 10069 switch (fieldp->virtuality)
c906108c 10070 {
c5aa993b
JM
10071 case DW_VIRTUALITY_virtual:
10072 case DW_VIRTUALITY_pure_virtual:
b4ba55a1 10073 if (cu->language == language_ada)
a73c6dcd 10074 error (_("unexpected virtuality in component of Ada type"));
c5aa993b
JM
10075 SET_TYPE_FIELD_VIRTUAL (type, nfields);
10076 break;
c906108c
SS
10077 }
10078 }
c906108c
SS
10079 }
10080}
10081
c906108c
SS
10082/* Add a member function to the proper fieldlist. */
10083
10084static void
107d2387 10085dwarf2_add_member_fn (struct field_info *fip, struct die_info *die,
e7c27a73 10086 struct type *type, struct dwarf2_cu *cu)
c906108c 10087{
e7c27a73 10088 struct objfile *objfile = cu->objfile;
c906108c
SS
10089 struct attribute *attr;
10090 struct fnfieldlist *flp;
10091 int i;
10092 struct fn_field *fnp;
10093 char *fieldname;
c906108c 10094 struct nextfnfield *new_fnfield;
f792889a 10095 struct type *this_type;
60d5a603 10096 enum dwarf_access_attribute accessibility;
c906108c 10097
b4ba55a1 10098 if (cu->language == language_ada)
a73c6dcd 10099 error (_("unexpected member function in Ada type"));
b4ba55a1 10100
2df3850c 10101 /* Get name of member function. */
39cbfefa
DJ
10102 fieldname = dwarf2_name (die, cu);
10103 if (fieldname == NULL)
2df3850c 10104 return;
c906108c 10105
c906108c
SS
10106 /* Look up member function name in fieldlist. */
10107 for (i = 0; i < fip->nfnfields; i++)
10108 {
27bfe10e 10109 if (strcmp (fip->fnfieldlists[i].name, fieldname) == 0)
c906108c
SS
10110 break;
10111 }
10112
10113 /* Create new list element if necessary. */
10114 if (i < fip->nfnfields)
10115 flp = &fip->fnfieldlists[i];
10116 else
10117 {
10118 if ((fip->nfnfields % DW_FIELD_ALLOC_CHUNK) == 0)
10119 {
10120 fip->fnfieldlists = (struct fnfieldlist *)
10121 xrealloc (fip->fnfieldlists,
10122 (fip->nfnfields + DW_FIELD_ALLOC_CHUNK)
c5aa993b 10123 * sizeof (struct fnfieldlist));
c906108c 10124 if (fip->nfnfields == 0)
c13c43fd 10125 make_cleanup (free_current_contents, &fip->fnfieldlists);
c906108c
SS
10126 }
10127 flp = &fip->fnfieldlists[fip->nfnfields];
10128 flp->name = fieldname;
10129 flp->length = 0;
10130 flp->head = NULL;
3da10d80 10131 i = fip->nfnfields++;
c906108c
SS
10132 }
10133
10134 /* Create a new member function field and chain it to the field list
0963b4bd 10135 entry. */
c906108c 10136 new_fnfield = (struct nextfnfield *) xmalloc (sizeof (struct nextfnfield));
b8c9b27d 10137 make_cleanup (xfree, new_fnfield);
c906108c
SS
10138 memset (new_fnfield, 0, sizeof (struct nextfnfield));
10139 new_fnfield->next = flp->head;
10140 flp->head = new_fnfield;
10141 flp->length++;
10142
10143 /* Fill in the member function field info. */
10144 fnp = &new_fnfield->fnfield;
3da10d80
KS
10145
10146 /* Delay processing of the physname until later. */
10147 if (cu->language == language_cplus || cu->language == language_java)
10148 {
10149 add_to_method_list (type, i, flp->length - 1, fieldname,
10150 die, cu);
10151 }
10152 else
10153 {
1d06ead6 10154 const char *physname = dwarf2_physname (fieldname, die, cu);
3da10d80
KS
10155 fnp->physname = physname ? physname : "";
10156 }
10157
c906108c 10158 fnp->type = alloc_type (objfile);
f792889a
DJ
10159 this_type = read_type_die (die, cu);
10160 if (this_type && TYPE_CODE (this_type) == TYPE_CODE_FUNC)
c906108c 10161 {
f792889a 10162 int nparams = TYPE_NFIELDS (this_type);
c906108c 10163
f792889a 10164 /* TYPE is the domain of this method, and THIS_TYPE is the type
e26fb1d7
DC
10165 of the method itself (TYPE_CODE_METHOD). */
10166 smash_to_method_type (fnp->type, type,
f792889a
DJ
10167 TYPE_TARGET_TYPE (this_type),
10168 TYPE_FIELDS (this_type),
10169 TYPE_NFIELDS (this_type),
10170 TYPE_VARARGS (this_type));
c906108c
SS
10171
10172 /* Handle static member functions.
c5aa993b 10173 Dwarf2 has no clean way to discern C++ static and non-static
0963b4bd
MS
10174 member functions. G++ helps GDB by marking the first
10175 parameter for non-static member functions (which is the this
10176 pointer) as artificial. We obtain this information from
10177 read_subroutine_type via TYPE_FIELD_ARTIFICIAL. */
f792889a 10178 if (nparams == 0 || TYPE_FIELD_ARTIFICIAL (this_type, 0) == 0)
c906108c
SS
10179 fnp->voffset = VOFFSET_STATIC;
10180 }
10181 else
e2e0b3e5 10182 complaint (&symfile_complaints, _("member function type missing for '%s'"),
3da10d80 10183 dwarf2_full_name (fieldname, die, cu));
c906108c
SS
10184
10185 /* Get fcontext from DW_AT_containing_type if present. */
e142c38c 10186 if (dwarf2_attr (die, DW_AT_containing_type, cu) != NULL)
e7c27a73 10187 fnp->fcontext = die_containing_type (die, cu);
c906108c 10188
3e43a32a
MS
10189 /* dwarf2 doesn't have stubbed physical names, so the setting of is_const and
10190 is_volatile is irrelevant, as it is needed by gdb_mangle_name only. */
c906108c
SS
10191
10192 /* Get accessibility. */
e142c38c 10193 attr = dwarf2_attr (die, DW_AT_accessibility, cu);
c906108c 10194 if (attr)
60d5a603
JK
10195 accessibility = DW_UNSND (attr);
10196 else
10197 accessibility = dwarf2_default_access_attribute (die, cu);
10198 switch (accessibility)
c906108c 10199 {
60d5a603
JK
10200 case DW_ACCESS_private:
10201 fnp->is_private = 1;
10202 break;
10203 case DW_ACCESS_protected:
10204 fnp->is_protected = 1;
10205 break;
c906108c
SS
10206 }
10207
b02dede2 10208 /* Check for artificial methods. */
e142c38c 10209 attr = dwarf2_attr (die, DW_AT_artificial, cu);
b02dede2
DJ
10210 if (attr && DW_UNSND (attr) != 0)
10211 fnp->is_artificial = 1;
10212
0d564a31 10213 /* Get index in virtual function table if it is a virtual member
aec5aa8b
TT
10214 function. For older versions of GCC, this is an offset in the
10215 appropriate virtual table, as specified by DW_AT_containing_type.
10216 For everyone else, it is an expression to be evaluated relative
0d564a31
DJ
10217 to the object address. */
10218
e142c38c 10219 attr = dwarf2_attr (die, DW_AT_vtable_elem_location, cu);
aec5aa8b 10220 if (attr)
8e19ed76 10221 {
aec5aa8b 10222 if (attr_form_is_block (attr) && DW_BLOCK (attr)->size > 0)
8e19ed76 10223 {
aec5aa8b
TT
10224 if (DW_BLOCK (attr)->data[0] == DW_OP_constu)
10225 {
10226 /* Old-style GCC. */
10227 fnp->voffset = decode_locdesc (DW_BLOCK (attr), cu) + 2;
10228 }
10229 else if (DW_BLOCK (attr)->data[0] == DW_OP_deref
10230 || (DW_BLOCK (attr)->size > 1
10231 && DW_BLOCK (attr)->data[0] == DW_OP_deref_size
10232 && DW_BLOCK (attr)->data[1] == cu->header.addr_size))
10233 {
10234 struct dwarf_block blk;
10235 int offset;
10236
10237 offset = (DW_BLOCK (attr)->data[0] == DW_OP_deref
10238 ? 1 : 2);
10239 blk.size = DW_BLOCK (attr)->size - offset;
10240 blk.data = DW_BLOCK (attr)->data + offset;
10241 fnp->voffset = decode_locdesc (DW_BLOCK (attr), cu);
10242 if ((fnp->voffset % cu->header.addr_size) != 0)
10243 dwarf2_complex_location_expr_complaint ();
10244 else
10245 fnp->voffset /= cu->header.addr_size;
10246 fnp->voffset += 2;
10247 }
10248 else
10249 dwarf2_complex_location_expr_complaint ();
10250
10251 if (!fnp->fcontext)
10252 fnp->fcontext = TYPE_TARGET_TYPE (TYPE_FIELD_TYPE (this_type, 0));
10253 }
3690dd37 10254 else if (attr_form_is_section_offset (attr))
8e19ed76 10255 {
4d3c2250 10256 dwarf2_complex_location_expr_complaint ();
8e19ed76
PS
10257 }
10258 else
10259 {
4d3c2250
KB
10260 dwarf2_invalid_attrib_class_complaint ("DW_AT_vtable_elem_location",
10261 fieldname);
8e19ed76 10262 }
0d564a31 10263 }
d48cc9dd
DJ
10264 else
10265 {
10266 attr = dwarf2_attr (die, DW_AT_virtuality, cu);
10267 if (attr && DW_UNSND (attr))
10268 {
10269 /* GCC does this, as of 2008-08-25; PR debug/37237. */
10270 complaint (&symfile_complaints,
3e43a32a
MS
10271 _("Member function \"%s\" (offset %d) is virtual "
10272 "but the vtable offset is not specified"),
b64f50a1 10273 fieldname, die->offset.sect_off);
9655fd1a 10274 ALLOCATE_CPLUS_STRUCT_TYPE (type);
d48cc9dd
DJ
10275 TYPE_CPLUS_DYNAMIC (type) = 1;
10276 }
10277 }
c906108c
SS
10278}
10279
10280/* Create the vector of member function fields, and attach it to the type. */
10281
10282static void
fba45db2 10283dwarf2_attach_fn_fields_to_type (struct field_info *fip, struct type *type,
e7c27a73 10284 struct dwarf2_cu *cu)
c906108c
SS
10285{
10286 struct fnfieldlist *flp;
c906108c
SS
10287 int i;
10288
b4ba55a1 10289 if (cu->language == language_ada)
a73c6dcd 10290 error (_("unexpected member functions in Ada type"));
b4ba55a1 10291
c906108c
SS
10292 ALLOCATE_CPLUS_STRUCT_TYPE (type);
10293 TYPE_FN_FIELDLISTS (type) = (struct fn_fieldlist *)
10294 TYPE_ALLOC (type, sizeof (struct fn_fieldlist) * fip->nfnfields);
10295
10296 for (i = 0, flp = fip->fnfieldlists; i < fip->nfnfields; i++, flp++)
10297 {
10298 struct nextfnfield *nfp = flp->head;
10299 struct fn_fieldlist *fn_flp = &TYPE_FN_FIELDLIST (type, i);
10300 int k;
10301
10302 TYPE_FN_FIELDLIST_NAME (type, i) = flp->name;
10303 TYPE_FN_FIELDLIST_LENGTH (type, i) = flp->length;
10304 fn_flp->fn_fields = (struct fn_field *)
10305 TYPE_ALLOC (type, sizeof (struct fn_field) * flp->length);
10306 for (k = flp->length; (k--, nfp); nfp = nfp->next)
c5aa993b 10307 fn_flp->fn_fields[k] = nfp->fnfield;
c906108c
SS
10308 }
10309
10310 TYPE_NFN_FIELDS (type) = fip->nfnfields;
c906108c
SS
10311}
10312
1168df01
JB
10313/* Returns non-zero if NAME is the name of a vtable member in CU's
10314 language, zero otherwise. */
10315static int
10316is_vtable_name (const char *name, struct dwarf2_cu *cu)
10317{
10318 static const char vptr[] = "_vptr";
987504bb 10319 static const char vtable[] = "vtable";
1168df01 10320
987504bb
JJ
10321 /* Look for the C++ and Java forms of the vtable. */
10322 if ((cu->language == language_java
10323 && strncmp (name, vtable, sizeof (vtable) - 1) == 0)
10324 || (strncmp (name, vptr, sizeof (vptr) - 1) == 0
10325 && is_cplus_marker (name[sizeof (vptr) - 1])))
1168df01
JB
10326 return 1;
10327
10328 return 0;
10329}
10330
c0dd20ea 10331/* GCC outputs unnamed structures that are really pointers to member
0b92b5bb
TT
10332 functions, with the ABI-specified layout. If TYPE describes
10333 such a structure, smash it into a member function type.
61049d3b
DJ
10334
10335 GCC shouldn't do this; it should just output pointer to member DIEs.
10336 This is GCC PR debug/28767. */
c0dd20ea 10337
0b92b5bb
TT
10338static void
10339quirk_gcc_member_function_pointer (struct type *type, struct objfile *objfile)
c0dd20ea 10340{
0b92b5bb 10341 struct type *pfn_type, *domain_type, *new_type;
c0dd20ea
DJ
10342
10343 /* Check for a structure with no name and two children. */
0b92b5bb
TT
10344 if (TYPE_CODE (type) != TYPE_CODE_STRUCT || TYPE_NFIELDS (type) != 2)
10345 return;
c0dd20ea
DJ
10346
10347 /* Check for __pfn and __delta members. */
0b92b5bb
TT
10348 if (TYPE_FIELD_NAME (type, 0) == NULL
10349 || strcmp (TYPE_FIELD_NAME (type, 0), "__pfn") != 0
10350 || TYPE_FIELD_NAME (type, 1) == NULL
10351 || strcmp (TYPE_FIELD_NAME (type, 1), "__delta") != 0)
10352 return;
c0dd20ea
DJ
10353
10354 /* Find the type of the method. */
0b92b5bb 10355 pfn_type = TYPE_FIELD_TYPE (type, 0);
c0dd20ea
DJ
10356 if (pfn_type == NULL
10357 || TYPE_CODE (pfn_type) != TYPE_CODE_PTR
10358 || TYPE_CODE (TYPE_TARGET_TYPE (pfn_type)) != TYPE_CODE_FUNC)
0b92b5bb 10359 return;
c0dd20ea
DJ
10360
10361 /* Look for the "this" argument. */
10362 pfn_type = TYPE_TARGET_TYPE (pfn_type);
10363 if (TYPE_NFIELDS (pfn_type) == 0
0b92b5bb 10364 /* || TYPE_FIELD_TYPE (pfn_type, 0) == NULL */
c0dd20ea 10365 || TYPE_CODE (TYPE_FIELD_TYPE (pfn_type, 0)) != TYPE_CODE_PTR)
0b92b5bb 10366 return;
c0dd20ea
DJ
10367
10368 domain_type = TYPE_TARGET_TYPE (TYPE_FIELD_TYPE (pfn_type, 0));
0b92b5bb
TT
10369 new_type = alloc_type (objfile);
10370 smash_to_method_type (new_type, domain_type, TYPE_TARGET_TYPE (pfn_type),
c0dd20ea
DJ
10371 TYPE_FIELDS (pfn_type), TYPE_NFIELDS (pfn_type),
10372 TYPE_VARARGS (pfn_type));
0b92b5bb 10373 smash_to_methodptr_type (type, new_type);
c0dd20ea 10374}
1168df01 10375
685b1105
JK
10376/* Return non-zero if the CU's PRODUCER string matches the Intel C/C++ compiler
10377 (icc). */
10378
10379static int
10380producer_is_icc (struct dwarf2_cu *cu)
10381{
10382 if (!cu->checked_producer)
10383 check_producer (cu);
10384
10385 return cu->producer_is_icc;
10386}
10387
c906108c 10388/* Called when we find the DIE that starts a structure or union scope
c767944b
DJ
10389 (definition) to create a type for the structure or union. Fill in
10390 the type's name and general properties; the members will not be
10391 processed until process_structure_type.
c906108c 10392
c767944b
DJ
10393 NOTE: we need to call these functions regardless of whether or not the
10394 DIE has a DW_AT_name attribute, since it might be an anonymous
c906108c
SS
10395 structure or union. This gets the type entered into our set of
10396 user defined types.
10397
10398 However, if the structure is incomplete (an opaque struct/union)
10399 then suppress creating a symbol table entry for it since gdb only
10400 wants to find the one with the complete definition. Note that if
10401 it is complete, we just call new_symbol, which does it's own
10402 checking about whether the struct/union is anonymous or not (and
10403 suppresses creating a symbol table entry itself). */
10404
f792889a 10405static struct type *
134d01f1 10406read_structure_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 10407{
e7c27a73 10408 struct objfile *objfile = cu->objfile;
c906108c
SS
10409 struct type *type;
10410 struct attribute *attr;
39cbfefa 10411 char *name;
c906108c 10412
348e048f
DE
10413 /* If the definition of this type lives in .debug_types, read that type.
10414 Don't follow DW_AT_specification though, that will take us back up
10415 the chain and we want to go down. */
45e58e77 10416 attr = dwarf2_attr_no_follow (die, DW_AT_signature);
348e048f
DE
10417 if (attr)
10418 {
10419 struct dwarf2_cu *type_cu = cu;
10420 struct die_info *type_die = follow_die_ref_or_sig (die, attr, &type_cu);
9a619af0 10421
348e048f
DE
10422 /* We could just recurse on read_structure_type, but we need to call
10423 get_die_type to ensure only one type for this DIE is created.
10424 This is important, for example, because for c++ classes we need
10425 TYPE_NAME set which is only done by new_symbol. Blech. */
10426 type = read_type_die (type_die, type_cu);
9dc481d3
DE
10427
10428 /* TYPE_CU may not be the same as CU.
10429 Ensure TYPE is recorded in CU's type_hash table. */
348e048f
DE
10430 return set_die_type (die, type, cu);
10431 }
10432
c0dd20ea 10433 type = alloc_type (objfile);
c906108c 10434 INIT_CPLUS_SPECIFIC (type);
93311388 10435
39cbfefa
DJ
10436 name = dwarf2_name (die, cu);
10437 if (name != NULL)
c906108c 10438 {
987504bb
JJ
10439 if (cu->language == language_cplus
10440 || cu->language == language_java)
63d06c5c 10441 {
3da10d80
KS
10442 char *full_name = (char *) dwarf2_full_name (name, die, cu);
10443
10444 /* dwarf2_full_name might have already finished building the DIE's
10445 type. If so, there is no need to continue. */
10446 if (get_die_type (die, cu) != NULL)
10447 return get_die_type (die, cu);
10448
10449 TYPE_TAG_NAME (type) = full_name;
94af9270
KS
10450 if (die->tag == DW_TAG_structure_type
10451 || die->tag == DW_TAG_class_type)
10452 TYPE_NAME (type) = TYPE_TAG_NAME (type);
63d06c5c
DC
10453 }
10454 else
10455 {
d8151005
DJ
10456 /* The name is already allocated along with this objfile, so
10457 we don't need to duplicate it for the type. */
94af9270
KS
10458 TYPE_TAG_NAME (type) = (char *) name;
10459 if (die->tag == DW_TAG_class_type)
10460 TYPE_NAME (type) = TYPE_TAG_NAME (type);
63d06c5c 10461 }
c906108c
SS
10462 }
10463
10464 if (die->tag == DW_TAG_structure_type)
10465 {
10466 TYPE_CODE (type) = TYPE_CODE_STRUCT;
10467 }
10468 else if (die->tag == DW_TAG_union_type)
10469 {
10470 TYPE_CODE (type) = TYPE_CODE_UNION;
10471 }
10472 else
10473 {
c906108c
SS
10474 TYPE_CODE (type) = TYPE_CODE_CLASS;
10475 }
10476
0cc2414c
TT
10477 if (cu->language == language_cplus && die->tag == DW_TAG_class_type)
10478 TYPE_DECLARED_CLASS (type) = 1;
10479
e142c38c 10480 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
c906108c
SS
10481 if (attr)
10482 {
10483 TYPE_LENGTH (type) = DW_UNSND (attr);
10484 }
10485 else
10486 {
10487 TYPE_LENGTH (type) = 0;
10488 }
10489
685b1105
JK
10490 if (producer_is_icc (cu))
10491 {
10492 /* ICC does not output the required DW_AT_declaration
10493 on incomplete types, but gives them a size of zero. */
10494 }
10495 else
10496 TYPE_STUB_SUPPORTED (type) = 1;
10497
dc718098 10498 if (die_is_declaration (die, cu))
876cecd0 10499 TYPE_STUB (type) = 1;
a6c727b2
DJ
10500 else if (attr == NULL && die->child == NULL
10501 && producer_is_realview (cu->producer))
10502 /* RealView does not output the required DW_AT_declaration
10503 on incomplete types. */
10504 TYPE_STUB (type) = 1;
dc718098 10505
c906108c
SS
10506 /* We need to add the type field to the die immediately so we don't
10507 infinitely recurse when dealing with pointers to the structure
0963b4bd 10508 type within the structure itself. */
1c379e20 10509 set_die_type (die, type, cu);
c906108c 10510
7e314c57
JK
10511 /* set_die_type should be already done. */
10512 set_descriptive_type (type, die, cu);
10513
c767944b
DJ
10514 return type;
10515}
10516
10517/* Finish creating a structure or union type, including filling in
10518 its members and creating a symbol for it. */
10519
10520static void
10521process_structure_scope (struct die_info *die, struct dwarf2_cu *cu)
10522{
10523 struct objfile *objfile = cu->objfile;
10524 struct die_info *child_die = die->child;
10525 struct type *type;
10526
10527 type = get_die_type (die, cu);
10528 if (type == NULL)
10529 type = read_structure_type (die, cu);
10530
e142c38c 10531 if (die->child != NULL && ! die_is_declaration (die, cu))
c906108c
SS
10532 {
10533 struct field_info fi;
10534 struct die_info *child_die;
34eaf542 10535 VEC (symbolp) *template_args = NULL;
c767944b 10536 struct cleanup *back_to = make_cleanup (null_cleanup, 0);
c906108c
SS
10537
10538 memset (&fi, 0, sizeof (struct field_info));
10539
639d11d3 10540 child_die = die->child;
c906108c
SS
10541
10542 while (child_die && child_die->tag)
10543 {
a9a9bd0f
DC
10544 if (child_die->tag == DW_TAG_member
10545 || child_die->tag == DW_TAG_variable)
c906108c 10546 {
a9a9bd0f
DC
10547 /* NOTE: carlton/2002-11-05: A C++ static data member
10548 should be a DW_TAG_member that is a declaration, but
10549 all versions of G++ as of this writing (so through at
10550 least 3.2.1) incorrectly generate DW_TAG_variable
10551 tags for them instead. */
e7c27a73 10552 dwarf2_add_field (&fi, child_die, cu);
c906108c 10553 }
8713b1b1 10554 else if (child_die->tag == DW_TAG_subprogram)
c906108c 10555 {
0963b4bd 10556 /* C++ member function. */
e7c27a73 10557 dwarf2_add_member_fn (&fi, child_die, type, cu);
c906108c
SS
10558 }
10559 else if (child_die->tag == DW_TAG_inheritance)
10560 {
10561 /* C++ base class field. */
e7c27a73 10562 dwarf2_add_field (&fi, child_die, cu);
c906108c 10563 }
98751a41
JK
10564 else if (child_die->tag == DW_TAG_typedef)
10565 dwarf2_add_typedef (&fi, child_die, cu);
34eaf542
TT
10566 else if (child_die->tag == DW_TAG_template_type_param
10567 || child_die->tag == DW_TAG_template_value_param)
10568 {
10569 struct symbol *arg = new_symbol (child_die, NULL, cu);
10570
f1078f66
DJ
10571 if (arg != NULL)
10572 VEC_safe_push (symbolp, template_args, arg);
34eaf542
TT
10573 }
10574
c906108c
SS
10575 child_die = sibling_die (child_die);
10576 }
10577
34eaf542
TT
10578 /* Attach template arguments to type. */
10579 if (! VEC_empty (symbolp, template_args))
10580 {
10581 ALLOCATE_CPLUS_STRUCT_TYPE (type);
10582 TYPE_N_TEMPLATE_ARGUMENTS (type)
10583 = VEC_length (symbolp, template_args);
10584 TYPE_TEMPLATE_ARGUMENTS (type)
10585 = obstack_alloc (&objfile->objfile_obstack,
10586 (TYPE_N_TEMPLATE_ARGUMENTS (type)
10587 * sizeof (struct symbol *)));
10588 memcpy (TYPE_TEMPLATE_ARGUMENTS (type),
10589 VEC_address (symbolp, template_args),
10590 (TYPE_N_TEMPLATE_ARGUMENTS (type)
10591 * sizeof (struct symbol *)));
10592 VEC_free (symbolp, template_args);
10593 }
10594
c906108c
SS
10595 /* Attach fields and member functions to the type. */
10596 if (fi.nfields)
e7c27a73 10597 dwarf2_attach_fields_to_type (&fi, type, cu);
c906108c
SS
10598 if (fi.nfnfields)
10599 {
e7c27a73 10600 dwarf2_attach_fn_fields_to_type (&fi, type, cu);
c906108c 10601
c5aa993b 10602 /* Get the type which refers to the base class (possibly this
c906108c 10603 class itself) which contains the vtable pointer for the current
0d564a31
DJ
10604 class from the DW_AT_containing_type attribute. This use of
10605 DW_AT_containing_type is a GNU extension. */
c906108c 10606
e142c38c 10607 if (dwarf2_attr (die, DW_AT_containing_type, cu) != NULL)
c906108c 10608 {
e7c27a73 10609 struct type *t = die_containing_type (die, cu);
c906108c
SS
10610
10611 TYPE_VPTR_BASETYPE (type) = t;
10612 if (type == t)
10613 {
c906108c
SS
10614 int i;
10615
10616 /* Our own class provides vtbl ptr. */
10617 for (i = TYPE_NFIELDS (t) - 1;
10618 i >= TYPE_N_BASECLASSES (t);
10619 --i)
10620 {
0d5cff50 10621 const char *fieldname = TYPE_FIELD_NAME (t, i);
c906108c 10622
1168df01 10623 if (is_vtable_name (fieldname, cu))
c906108c
SS
10624 {
10625 TYPE_VPTR_FIELDNO (type) = i;
10626 break;
10627 }
10628 }
10629
10630 /* Complain if virtual function table field not found. */
10631 if (i < TYPE_N_BASECLASSES (t))
4d3c2250 10632 complaint (&symfile_complaints,
3e43a32a
MS
10633 _("virtual function table pointer "
10634 "not found when defining class '%s'"),
4d3c2250
KB
10635 TYPE_TAG_NAME (type) ? TYPE_TAG_NAME (type) :
10636 "");
c906108c
SS
10637 }
10638 else
10639 {
10640 TYPE_VPTR_FIELDNO (type) = TYPE_VPTR_FIELDNO (t);
10641 }
10642 }
f6235d4c
EZ
10643 else if (cu->producer
10644 && strncmp (cu->producer,
10645 "IBM(R) XL C/C++ Advanced Edition", 32) == 0)
10646 {
10647 /* The IBM XLC compiler does not provide direct indication
10648 of the containing type, but the vtable pointer is
10649 always named __vfp. */
10650
10651 int i;
10652
10653 for (i = TYPE_NFIELDS (type) - 1;
10654 i >= TYPE_N_BASECLASSES (type);
10655 --i)
10656 {
10657 if (strcmp (TYPE_FIELD_NAME (type, i), "__vfp") == 0)
10658 {
10659 TYPE_VPTR_FIELDNO (type) = i;
10660 TYPE_VPTR_BASETYPE (type) = type;
10661 break;
10662 }
10663 }
10664 }
c906108c 10665 }
98751a41
JK
10666
10667 /* Copy fi.typedef_field_list linked list elements content into the
10668 allocated array TYPE_TYPEDEF_FIELD_ARRAY (type). */
10669 if (fi.typedef_field_list)
10670 {
10671 int i = fi.typedef_field_list_count;
10672
a0d7a4ff 10673 ALLOCATE_CPLUS_STRUCT_TYPE (type);
98751a41
JK
10674 TYPE_TYPEDEF_FIELD_ARRAY (type)
10675 = TYPE_ALLOC (type, sizeof (TYPE_TYPEDEF_FIELD (type, 0)) * i);
10676 TYPE_TYPEDEF_FIELD_COUNT (type) = i;
10677
10678 /* Reverse the list order to keep the debug info elements order. */
10679 while (--i >= 0)
10680 {
10681 struct typedef_field *dest, *src;
6e70227d 10682
98751a41
JK
10683 dest = &TYPE_TYPEDEF_FIELD (type, i);
10684 src = &fi.typedef_field_list->field;
10685 fi.typedef_field_list = fi.typedef_field_list->next;
10686 *dest = *src;
10687 }
10688 }
c767944b
DJ
10689
10690 do_cleanups (back_to);
eb2a6f42
TT
10691
10692 if (HAVE_CPLUS_STRUCT (type))
10693 TYPE_CPLUS_REALLY_JAVA (type) = cu->language == language_java;
c906108c 10694 }
63d06c5c 10695
bb5ed363 10696 quirk_gcc_member_function_pointer (type, objfile);
0b92b5bb 10697
90aeadfc
DC
10698 /* NOTE: carlton/2004-03-16: GCC 3.4 (or at least one of its
10699 snapshots) has been known to create a die giving a declaration
10700 for a class that has, as a child, a die giving a definition for a
10701 nested class. So we have to process our children even if the
10702 current die is a declaration. Normally, of course, a declaration
10703 won't have any children at all. */
134d01f1 10704
90aeadfc
DC
10705 while (child_die != NULL && child_die->tag)
10706 {
10707 if (child_die->tag == DW_TAG_member
10708 || child_die->tag == DW_TAG_variable
34eaf542
TT
10709 || child_die->tag == DW_TAG_inheritance
10710 || child_die->tag == DW_TAG_template_value_param
10711 || child_die->tag == DW_TAG_template_type_param)
134d01f1 10712 {
90aeadfc 10713 /* Do nothing. */
134d01f1 10714 }
90aeadfc
DC
10715 else
10716 process_die (child_die, cu);
134d01f1 10717
90aeadfc 10718 child_die = sibling_die (child_die);
134d01f1
DJ
10719 }
10720
fa4028e9
JB
10721 /* Do not consider external references. According to the DWARF standard,
10722 these DIEs are identified by the fact that they have no byte_size
10723 attribute, and a declaration attribute. */
10724 if (dwarf2_attr (die, DW_AT_byte_size, cu) != NULL
10725 || !die_is_declaration (die, cu))
c767944b 10726 new_symbol (die, type, cu);
134d01f1
DJ
10727}
10728
10729/* Given a DW_AT_enumeration_type die, set its type. We do not
10730 complete the type's fields yet, or create any symbols. */
c906108c 10731
f792889a 10732static struct type *
134d01f1 10733read_enumeration_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 10734{
e7c27a73 10735 struct objfile *objfile = cu->objfile;
c906108c 10736 struct type *type;
c906108c 10737 struct attribute *attr;
0114d602 10738 const char *name;
134d01f1 10739
348e048f
DE
10740 /* If the definition of this type lives in .debug_types, read that type.
10741 Don't follow DW_AT_specification though, that will take us back up
10742 the chain and we want to go down. */
45e58e77 10743 attr = dwarf2_attr_no_follow (die, DW_AT_signature);
348e048f
DE
10744 if (attr)
10745 {
10746 struct dwarf2_cu *type_cu = cu;
10747 struct die_info *type_die = follow_die_ref_or_sig (die, attr, &type_cu);
9a619af0 10748
348e048f 10749 type = read_type_die (type_die, type_cu);
9dc481d3
DE
10750
10751 /* TYPE_CU may not be the same as CU.
10752 Ensure TYPE is recorded in CU's type_hash table. */
348e048f
DE
10753 return set_die_type (die, type, cu);
10754 }
10755
c906108c
SS
10756 type = alloc_type (objfile);
10757
10758 TYPE_CODE (type) = TYPE_CODE_ENUM;
94af9270 10759 name = dwarf2_full_name (NULL, die, cu);
39cbfefa 10760 if (name != NULL)
0114d602 10761 TYPE_TAG_NAME (type) = (char *) name;
c906108c 10762
e142c38c 10763 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
c906108c
SS
10764 if (attr)
10765 {
10766 TYPE_LENGTH (type) = DW_UNSND (attr);
10767 }
10768 else
10769 {
10770 TYPE_LENGTH (type) = 0;
10771 }
10772
137033e9
JB
10773 /* The enumeration DIE can be incomplete. In Ada, any type can be
10774 declared as private in the package spec, and then defined only
10775 inside the package body. Such types are known as Taft Amendment
10776 Types. When another package uses such a type, an incomplete DIE
10777 may be generated by the compiler. */
02eb380e 10778 if (die_is_declaration (die, cu))
876cecd0 10779 TYPE_STUB (type) = 1;
02eb380e 10780
f792889a 10781 return set_die_type (die, type, cu);
134d01f1
DJ
10782}
10783
10784/* Given a pointer to a die which begins an enumeration, process all
10785 the dies that define the members of the enumeration, and create the
10786 symbol for the enumeration type.
10787
10788 NOTE: We reverse the order of the element list. */
10789
10790static void
10791process_enumeration_scope (struct die_info *die, struct dwarf2_cu *cu)
10792{
f792889a 10793 struct type *this_type;
134d01f1 10794
f792889a
DJ
10795 this_type = get_die_type (die, cu);
10796 if (this_type == NULL)
10797 this_type = read_enumeration_type (die, cu);
9dc481d3 10798
639d11d3 10799 if (die->child != NULL)
c906108c 10800 {
9dc481d3
DE
10801 struct die_info *child_die;
10802 struct symbol *sym;
10803 struct field *fields = NULL;
10804 int num_fields = 0;
10805 int unsigned_enum = 1;
10806 char *name;
cafec441
TT
10807 int flag_enum = 1;
10808 ULONGEST mask = 0;
9dc481d3 10809
639d11d3 10810 child_die = die->child;
c906108c
SS
10811 while (child_die && child_die->tag)
10812 {
10813 if (child_die->tag != DW_TAG_enumerator)
10814 {
e7c27a73 10815 process_die (child_die, cu);
c906108c
SS
10816 }
10817 else
10818 {
39cbfefa
DJ
10819 name = dwarf2_name (child_die, cu);
10820 if (name)
c906108c 10821 {
f792889a 10822 sym = new_symbol (child_die, this_type, cu);
c906108c 10823 if (SYMBOL_VALUE (sym) < 0)
cafec441
TT
10824 {
10825 unsigned_enum = 0;
10826 flag_enum = 0;
10827 }
10828 else if ((mask & SYMBOL_VALUE (sym)) != 0)
10829 flag_enum = 0;
10830 else
10831 mask |= SYMBOL_VALUE (sym);
c906108c
SS
10832
10833 if ((num_fields % DW_FIELD_ALLOC_CHUNK) == 0)
10834 {
10835 fields = (struct field *)
10836 xrealloc (fields,
10837 (num_fields + DW_FIELD_ALLOC_CHUNK)
c5aa993b 10838 * sizeof (struct field));
c906108c
SS
10839 }
10840
3567439c 10841 FIELD_NAME (fields[num_fields]) = SYMBOL_LINKAGE_NAME (sym);
c906108c 10842 FIELD_TYPE (fields[num_fields]) = NULL;
14e75d8e 10843 SET_FIELD_ENUMVAL (fields[num_fields], SYMBOL_VALUE (sym));
c906108c
SS
10844 FIELD_BITSIZE (fields[num_fields]) = 0;
10845
10846 num_fields++;
10847 }
10848 }
10849
10850 child_die = sibling_die (child_die);
10851 }
10852
10853 if (num_fields)
10854 {
f792889a
DJ
10855 TYPE_NFIELDS (this_type) = num_fields;
10856 TYPE_FIELDS (this_type) = (struct field *)
10857 TYPE_ALLOC (this_type, sizeof (struct field) * num_fields);
10858 memcpy (TYPE_FIELDS (this_type), fields,
c906108c 10859 sizeof (struct field) * num_fields);
b8c9b27d 10860 xfree (fields);
c906108c
SS
10861 }
10862 if (unsigned_enum)
876cecd0 10863 TYPE_UNSIGNED (this_type) = 1;
cafec441
TT
10864 if (flag_enum)
10865 TYPE_FLAG_ENUM (this_type) = 1;
c906108c 10866 }
134d01f1 10867
6c83ed52
TT
10868 /* If we are reading an enum from a .debug_types unit, and the enum
10869 is a declaration, and the enum is not the signatured type in the
10870 unit, then we do not want to add a symbol for it. Adding a
10871 symbol would in some cases obscure the true definition of the
10872 enum, giving users an incomplete type when the definition is
10873 actually available. Note that we do not want to do this for all
10874 enums which are just declarations, because C++0x allows forward
10875 enum declarations. */
3019eac3 10876 if (cu->per_cu->is_debug_types
6c83ed52
TT
10877 && die_is_declaration (die, cu))
10878 {
52dc124a 10879 struct signatured_type *sig_type;
6c83ed52 10880
52dc124a 10881 sig_type
6c83ed52 10882 = lookup_signatured_type_at_offset (dwarf2_per_objfile->objfile,
3019eac3 10883 cu->per_cu->info_or_types_section,
6c83ed52 10884 cu->per_cu->offset);
3019eac3
DE
10885 gdb_assert (sig_type->type_offset_in_section.sect_off != 0);
10886 if (sig_type->type_offset_in_section.sect_off != die->offset.sect_off)
6c83ed52
TT
10887 return;
10888 }
10889
f792889a 10890 new_symbol (die, this_type, cu);
c906108c
SS
10891}
10892
10893/* Extract all information from a DW_TAG_array_type DIE and put it in
10894 the DIE's type field. For now, this only handles one dimensional
10895 arrays. */
10896
f792889a 10897static struct type *
e7c27a73 10898read_array_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 10899{
e7c27a73 10900 struct objfile *objfile = cu->objfile;
c906108c 10901 struct die_info *child_die;
7e314c57 10902 struct type *type;
c906108c
SS
10903 struct type *element_type, *range_type, *index_type;
10904 struct type **range_types = NULL;
10905 struct attribute *attr;
10906 int ndim = 0;
10907 struct cleanup *back_to;
39cbfefa 10908 char *name;
c906108c 10909
e7c27a73 10910 element_type = die_type (die, cu);
c906108c 10911
7e314c57
JK
10912 /* The die_type call above may have already set the type for this DIE. */
10913 type = get_die_type (die, cu);
10914 if (type)
10915 return type;
10916
c906108c
SS
10917 /* Irix 6.2 native cc creates array types without children for
10918 arrays with unspecified length. */
639d11d3 10919 if (die->child == NULL)
c906108c 10920 {
46bf5051 10921 index_type = objfile_type (objfile)->builtin_int;
c906108c 10922 range_type = create_range_type (NULL, index_type, 0, -1);
f792889a
DJ
10923 type = create_array_type (NULL, element_type, range_type);
10924 return set_die_type (die, type, cu);
c906108c
SS
10925 }
10926
10927 back_to = make_cleanup (null_cleanup, NULL);
639d11d3 10928 child_die = die->child;
c906108c
SS
10929 while (child_die && child_die->tag)
10930 {
10931 if (child_die->tag == DW_TAG_subrange_type)
10932 {
f792889a 10933 struct type *child_type = read_type_die (child_die, cu);
9a619af0 10934
f792889a 10935 if (child_type != NULL)
a02abb62 10936 {
0963b4bd
MS
10937 /* The range type was succesfully read. Save it for the
10938 array type creation. */
a02abb62
JB
10939 if ((ndim % DW_FIELD_ALLOC_CHUNK) == 0)
10940 {
10941 range_types = (struct type **)
10942 xrealloc (range_types, (ndim + DW_FIELD_ALLOC_CHUNK)
10943 * sizeof (struct type *));
10944 if (ndim == 0)
10945 make_cleanup (free_current_contents, &range_types);
10946 }
f792889a 10947 range_types[ndim++] = child_type;
a02abb62 10948 }
c906108c
SS
10949 }
10950 child_die = sibling_die (child_die);
10951 }
10952
10953 /* Dwarf2 dimensions are output from left to right, create the
10954 necessary array types in backwards order. */
7ca2d3a3 10955
c906108c 10956 type = element_type;
7ca2d3a3
DL
10957
10958 if (read_array_order (die, cu) == DW_ORD_col_major)
10959 {
10960 int i = 0;
9a619af0 10961
7ca2d3a3
DL
10962 while (i < ndim)
10963 type = create_array_type (NULL, type, range_types[i++]);
10964 }
10965 else
10966 {
10967 while (ndim-- > 0)
10968 type = create_array_type (NULL, type, range_types[ndim]);
10969 }
c906108c 10970
f5f8a009
EZ
10971 /* Understand Dwarf2 support for vector types (like they occur on
10972 the PowerPC w/ AltiVec). Gcc just adds another attribute to the
10973 array type. This is not part of the Dwarf2/3 standard yet, but a
10974 custom vendor extension. The main difference between a regular
10975 array and the vector variant is that vectors are passed by value
10976 to functions. */
e142c38c 10977 attr = dwarf2_attr (die, DW_AT_GNU_vector, cu);
f5f8a009 10978 if (attr)
ea37ba09 10979 make_vector_type (type);
f5f8a009 10980
dbc98a8b
KW
10981 /* The DIE may have DW_AT_byte_size set. For example an OpenCL
10982 implementation may choose to implement triple vectors using this
10983 attribute. */
10984 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
10985 if (attr)
10986 {
10987 if (DW_UNSND (attr) >= TYPE_LENGTH (type))
10988 TYPE_LENGTH (type) = DW_UNSND (attr);
10989 else
3e43a32a
MS
10990 complaint (&symfile_complaints,
10991 _("DW_AT_byte_size for array type smaller "
10992 "than the total size of elements"));
dbc98a8b
KW
10993 }
10994
39cbfefa
DJ
10995 name = dwarf2_name (die, cu);
10996 if (name)
10997 TYPE_NAME (type) = name;
6e70227d 10998
0963b4bd 10999 /* Install the type in the die. */
7e314c57
JK
11000 set_die_type (die, type, cu);
11001
11002 /* set_die_type should be already done. */
b4ba55a1
JB
11003 set_descriptive_type (type, die, cu);
11004
c906108c
SS
11005 do_cleanups (back_to);
11006
7e314c57 11007 return type;
c906108c
SS
11008}
11009
7ca2d3a3 11010static enum dwarf_array_dim_ordering
6e70227d 11011read_array_order (struct die_info *die, struct dwarf2_cu *cu)
7ca2d3a3
DL
11012{
11013 struct attribute *attr;
11014
11015 attr = dwarf2_attr (die, DW_AT_ordering, cu);
11016
11017 if (attr) return DW_SND (attr);
11018
0963b4bd
MS
11019 /* GNU F77 is a special case, as at 08/2004 array type info is the
11020 opposite order to the dwarf2 specification, but data is still
11021 laid out as per normal fortran.
7ca2d3a3 11022
0963b4bd
MS
11023 FIXME: dsl/2004-8-20: If G77 is ever fixed, this will also need
11024 version checking. */
7ca2d3a3 11025
905e0470
PM
11026 if (cu->language == language_fortran
11027 && cu->producer && strstr (cu->producer, "GNU F77"))
7ca2d3a3
DL
11028 {
11029 return DW_ORD_row_major;
11030 }
11031
6e70227d 11032 switch (cu->language_defn->la_array_ordering)
7ca2d3a3
DL
11033 {
11034 case array_column_major:
11035 return DW_ORD_col_major;
11036 case array_row_major:
11037 default:
11038 return DW_ORD_row_major;
11039 };
11040}
11041
72019c9c 11042/* Extract all information from a DW_TAG_set_type DIE and put it in
0963b4bd 11043 the DIE's type field. */
72019c9c 11044
f792889a 11045static struct type *
72019c9c
GM
11046read_set_type (struct die_info *die, struct dwarf2_cu *cu)
11047{
7e314c57
JK
11048 struct type *domain_type, *set_type;
11049 struct attribute *attr;
f792889a 11050
7e314c57
JK
11051 domain_type = die_type (die, cu);
11052
11053 /* The die_type call above may have already set the type for this DIE. */
11054 set_type = get_die_type (die, cu);
11055 if (set_type)
11056 return set_type;
11057
11058 set_type = create_set_type (NULL, domain_type);
11059
11060 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
d09039dd
PM
11061 if (attr)
11062 TYPE_LENGTH (set_type) = DW_UNSND (attr);
7e314c57 11063
f792889a 11064 return set_die_type (die, set_type, cu);
72019c9c 11065}
7ca2d3a3 11066
0971de02
TT
11067/* A helper for read_common_block that creates a locexpr baton.
11068 SYM is the symbol which we are marking as computed.
11069 COMMON_DIE is the DIE for the common block.
11070 COMMON_LOC is the location expression attribute for the common
11071 block itself.
11072 MEMBER_LOC is the location expression attribute for the particular
11073 member of the common block that we are processing.
11074 CU is the CU from which the above come. */
11075
11076static void
11077mark_common_block_symbol_computed (struct symbol *sym,
11078 struct die_info *common_die,
11079 struct attribute *common_loc,
11080 struct attribute *member_loc,
11081 struct dwarf2_cu *cu)
11082{
11083 struct objfile *objfile = dwarf2_per_objfile->objfile;
11084 struct dwarf2_locexpr_baton *baton;
11085 gdb_byte *ptr;
11086 unsigned int cu_off;
11087 enum bfd_endian byte_order = gdbarch_byte_order (get_objfile_arch (objfile));
11088 LONGEST offset = 0;
11089
11090 gdb_assert (common_loc && member_loc);
11091 gdb_assert (attr_form_is_block (common_loc));
11092 gdb_assert (attr_form_is_block (member_loc)
11093 || attr_form_is_constant (member_loc));
11094
11095 baton = obstack_alloc (&objfile->objfile_obstack,
11096 sizeof (struct dwarf2_locexpr_baton));
11097 baton->per_cu = cu->per_cu;
11098 gdb_assert (baton->per_cu);
11099
11100 baton->size = 5 /* DW_OP_call4 */ + 1 /* DW_OP_plus */;
11101
11102 if (attr_form_is_constant (member_loc))
11103 {
11104 offset = dwarf2_get_attr_constant_value (member_loc, 0);
11105 baton->size += 1 /* DW_OP_addr */ + cu->header.addr_size;
11106 }
11107 else
11108 baton->size += DW_BLOCK (member_loc)->size;
11109
11110 ptr = obstack_alloc (&objfile->objfile_obstack, baton->size);
11111 baton->data = ptr;
11112
11113 *ptr++ = DW_OP_call4;
11114 cu_off = common_die->offset.sect_off - cu->per_cu->offset.sect_off;
11115 store_unsigned_integer (ptr, 4, byte_order, cu_off);
11116 ptr += 4;
11117
11118 if (attr_form_is_constant (member_loc))
11119 {
11120 *ptr++ = DW_OP_addr;
11121 store_unsigned_integer (ptr, cu->header.addr_size, byte_order, offset);
11122 ptr += cu->header.addr_size;
11123 }
11124 else
11125 {
11126 /* We have to copy the data here, because DW_OP_call4 will only
11127 use a DW_AT_location attribute. */
11128 memcpy (ptr, DW_BLOCK (member_loc)->data, DW_BLOCK (member_loc)->size);
11129 ptr += DW_BLOCK (member_loc)->size;
11130 }
11131
11132 *ptr++ = DW_OP_plus;
11133 gdb_assert (ptr - baton->data == baton->size);
11134
11135 SYMBOL_COMPUTED_OPS (sym) = &dwarf2_locexpr_funcs;
11136 SYMBOL_LOCATION_BATON (sym) = baton;
11137 SYMBOL_CLASS (sym) = LOC_COMPUTED;
11138}
11139
4357ac6c
TT
11140/* Create appropriate locally-scoped variables for all the
11141 DW_TAG_common_block entries. Also create a struct common_block
11142 listing all such variables for `info common'. COMMON_BLOCK_DOMAIN
11143 is used to sepate the common blocks name namespace from regular
11144 variable names. */
c906108c
SS
11145
11146static void
e7c27a73 11147read_common_block (struct die_info *die, struct dwarf2_cu *cu)
c906108c 11148{
0971de02
TT
11149 struct attribute *attr;
11150
11151 attr = dwarf2_attr (die, DW_AT_location, cu);
11152 if (attr)
11153 {
11154 /* Support the .debug_loc offsets. */
11155 if (attr_form_is_block (attr))
11156 {
11157 /* Ok. */
11158 }
11159 else if (attr_form_is_section_offset (attr))
11160 {
11161 dwarf2_complex_location_expr_complaint ();
11162 attr = NULL;
11163 }
11164 else
11165 {
11166 dwarf2_invalid_attrib_class_complaint ("DW_AT_location",
11167 "common block member");
11168 attr = NULL;
11169 }
11170 }
11171
639d11d3 11172 if (die->child != NULL)
c906108c 11173 {
4357ac6c
TT
11174 struct objfile *objfile = cu->objfile;
11175 struct die_info *child_die;
11176 size_t n_entries = 0, size;
11177 struct common_block *common_block;
11178 struct symbol *sym;
74ac6d43 11179
4357ac6c
TT
11180 for (child_die = die->child;
11181 child_die && child_die->tag;
11182 child_die = sibling_die (child_die))
11183 ++n_entries;
11184
11185 size = (sizeof (struct common_block)
11186 + (n_entries - 1) * sizeof (struct symbol *));
11187 common_block = obstack_alloc (&objfile->objfile_obstack, size);
11188 memset (common_block->contents, 0, n_entries * sizeof (struct symbol *));
11189 common_block->n_entries = 0;
11190
11191 for (child_die = die->child;
11192 child_die && child_die->tag;
11193 child_die = sibling_die (child_die))
11194 {
11195 /* Create the symbol in the DW_TAG_common_block block in the current
11196 symbol scope. */
e7c27a73 11197 sym = new_symbol (child_die, NULL, cu);
0971de02
TT
11198 if (sym != NULL)
11199 {
11200 struct attribute *member_loc;
11201
11202 common_block->contents[common_block->n_entries++] = sym;
11203
11204 member_loc = dwarf2_attr (child_die, DW_AT_data_member_location,
11205 cu);
11206 if (member_loc)
11207 {
11208 /* GDB has handled this for a long time, but it is
11209 not specified by DWARF. It seems to have been
11210 emitted by gfortran at least as recently as:
11211 http://gcc.gnu.org/bugzilla/show_bug.cgi?id=23057. */
11212 complaint (&symfile_complaints,
11213 _("Variable in common block has "
11214 "DW_AT_data_member_location "
11215 "- DIE at 0x%x [in module %s]"),
11216 child_die->offset.sect_off, cu->objfile->name);
11217
11218 if (attr_form_is_section_offset (member_loc))
11219 dwarf2_complex_location_expr_complaint ();
11220 else if (attr_form_is_constant (member_loc)
11221 || attr_form_is_block (member_loc))
11222 {
11223 if (attr)
11224 mark_common_block_symbol_computed (sym, die, attr,
11225 member_loc, cu);
11226 }
11227 else
11228 dwarf2_complex_location_expr_complaint ();
11229 }
11230 }
c906108c 11231 }
4357ac6c
TT
11232
11233 sym = new_symbol (die, objfile_type (objfile)->builtin_void, cu);
11234 SYMBOL_VALUE_COMMON_BLOCK (sym) = common_block;
c906108c
SS
11235 }
11236}
11237
0114d602 11238/* Create a type for a C++ namespace. */
d9fa45fe 11239
0114d602
DJ
11240static struct type *
11241read_namespace_type (struct die_info *die, struct dwarf2_cu *cu)
d9fa45fe 11242{
e7c27a73 11243 struct objfile *objfile = cu->objfile;
0114d602 11244 const char *previous_prefix, *name;
9219021c 11245 int is_anonymous;
0114d602
DJ
11246 struct type *type;
11247
11248 /* For extensions, reuse the type of the original namespace. */
11249 if (dwarf2_attr (die, DW_AT_extension, cu) != NULL)
11250 {
11251 struct die_info *ext_die;
11252 struct dwarf2_cu *ext_cu = cu;
9a619af0 11253
0114d602
DJ
11254 ext_die = dwarf2_extension (die, &ext_cu);
11255 type = read_type_die (ext_die, ext_cu);
9dc481d3
DE
11256
11257 /* EXT_CU may not be the same as CU.
11258 Ensure TYPE is recorded in CU's type_hash table. */
0114d602
DJ
11259 return set_die_type (die, type, cu);
11260 }
9219021c 11261
e142c38c 11262 name = namespace_name (die, &is_anonymous, cu);
9219021c
DC
11263
11264 /* Now build the name of the current namespace. */
11265
0114d602
DJ
11266 previous_prefix = determine_prefix (die, cu);
11267 if (previous_prefix[0] != '\0')
11268 name = typename_concat (&objfile->objfile_obstack,
f55ee35c 11269 previous_prefix, name, 0, cu);
0114d602
DJ
11270
11271 /* Create the type. */
11272 type = init_type (TYPE_CODE_NAMESPACE, 0, 0, NULL,
11273 objfile);
11274 TYPE_NAME (type) = (char *) name;
11275 TYPE_TAG_NAME (type) = TYPE_NAME (type);
11276
60531b24 11277 return set_die_type (die, type, cu);
0114d602
DJ
11278}
11279
11280/* Read a C++ namespace. */
11281
11282static void
11283read_namespace (struct die_info *die, struct dwarf2_cu *cu)
11284{
11285 struct objfile *objfile = cu->objfile;
0114d602 11286 int is_anonymous;
9219021c 11287
5c4e30ca
DC
11288 /* Add a symbol associated to this if we haven't seen the namespace
11289 before. Also, add a using directive if it's an anonymous
11290 namespace. */
9219021c 11291
f2f0e013 11292 if (dwarf2_attr (die, DW_AT_extension, cu) == NULL)
5c4e30ca
DC
11293 {
11294 struct type *type;
11295
0114d602 11296 type = read_type_die (die, cu);
e7c27a73 11297 new_symbol (die, type, cu);
5c4e30ca 11298
e8e80198 11299 namespace_name (die, &is_anonymous, cu);
5c4e30ca 11300 if (is_anonymous)
0114d602
DJ
11301 {
11302 const char *previous_prefix = determine_prefix (die, cu);
9a619af0 11303
c0cc3a76 11304 cp_add_using_directive (previous_prefix, TYPE_NAME (type), NULL,
32019081 11305 NULL, NULL, &objfile->objfile_obstack);
0114d602 11306 }
5c4e30ca 11307 }
9219021c 11308
639d11d3 11309 if (die->child != NULL)
d9fa45fe 11310 {
639d11d3 11311 struct die_info *child_die = die->child;
6e70227d 11312
d9fa45fe
DC
11313 while (child_die && child_die->tag)
11314 {
e7c27a73 11315 process_die (child_die, cu);
d9fa45fe
DC
11316 child_die = sibling_die (child_die);
11317 }
11318 }
38d518c9
EZ
11319}
11320
f55ee35c
JK
11321/* Read a Fortran module as type. This DIE can be only a declaration used for
11322 imported module. Still we need that type as local Fortran "use ... only"
11323 declaration imports depend on the created type in determine_prefix. */
11324
11325static struct type *
11326read_module_type (struct die_info *die, struct dwarf2_cu *cu)
11327{
11328 struct objfile *objfile = cu->objfile;
11329 char *module_name;
11330 struct type *type;
11331
11332 module_name = dwarf2_name (die, cu);
11333 if (!module_name)
3e43a32a
MS
11334 complaint (&symfile_complaints,
11335 _("DW_TAG_module has no name, offset 0x%x"),
b64f50a1 11336 die->offset.sect_off);
f55ee35c
JK
11337 type = init_type (TYPE_CODE_MODULE, 0, 0, module_name, objfile);
11338
11339 /* determine_prefix uses TYPE_TAG_NAME. */
11340 TYPE_TAG_NAME (type) = TYPE_NAME (type);
11341
11342 return set_die_type (die, type, cu);
11343}
11344
5d7cb8df
JK
11345/* Read a Fortran module. */
11346
11347static void
11348read_module (struct die_info *die, struct dwarf2_cu *cu)
11349{
11350 struct die_info *child_die = die->child;
11351
5d7cb8df
JK
11352 while (child_die && child_die->tag)
11353 {
11354 process_die (child_die, cu);
11355 child_die = sibling_die (child_die);
11356 }
11357}
11358
38d518c9
EZ
11359/* Return the name of the namespace represented by DIE. Set
11360 *IS_ANONYMOUS to tell whether or not the namespace is an anonymous
11361 namespace. */
11362
11363static const char *
e142c38c 11364namespace_name (struct die_info *die, int *is_anonymous, struct dwarf2_cu *cu)
38d518c9
EZ
11365{
11366 struct die_info *current_die;
11367 const char *name = NULL;
11368
11369 /* Loop through the extensions until we find a name. */
11370
11371 for (current_die = die;
11372 current_die != NULL;
f2f0e013 11373 current_die = dwarf2_extension (die, &cu))
38d518c9 11374 {
e142c38c 11375 name = dwarf2_name (current_die, cu);
38d518c9
EZ
11376 if (name != NULL)
11377 break;
11378 }
11379
11380 /* Is it an anonymous namespace? */
11381
11382 *is_anonymous = (name == NULL);
11383 if (*is_anonymous)
2b1dbab0 11384 name = CP_ANONYMOUS_NAMESPACE_STR;
38d518c9
EZ
11385
11386 return name;
d9fa45fe
DC
11387}
11388
c906108c
SS
11389/* Extract all information from a DW_TAG_pointer_type DIE and add to
11390 the user defined type vector. */
11391
f792889a 11392static struct type *
e7c27a73 11393read_tag_pointer_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 11394{
5e2b427d 11395 struct gdbarch *gdbarch = get_objfile_arch (cu->objfile);
e7c27a73 11396 struct comp_unit_head *cu_header = &cu->header;
c906108c 11397 struct type *type;
8b2dbe47
KB
11398 struct attribute *attr_byte_size;
11399 struct attribute *attr_address_class;
11400 int byte_size, addr_class;
7e314c57
JK
11401 struct type *target_type;
11402
11403 target_type = die_type (die, cu);
c906108c 11404
7e314c57
JK
11405 /* The die_type call above may have already set the type for this DIE. */
11406 type = get_die_type (die, cu);
11407 if (type)
11408 return type;
11409
11410 type = lookup_pointer_type (target_type);
8b2dbe47 11411
e142c38c 11412 attr_byte_size = dwarf2_attr (die, DW_AT_byte_size, cu);
8b2dbe47
KB
11413 if (attr_byte_size)
11414 byte_size = DW_UNSND (attr_byte_size);
c906108c 11415 else
8b2dbe47
KB
11416 byte_size = cu_header->addr_size;
11417
e142c38c 11418 attr_address_class = dwarf2_attr (die, DW_AT_address_class, cu);
8b2dbe47
KB
11419 if (attr_address_class)
11420 addr_class = DW_UNSND (attr_address_class);
11421 else
11422 addr_class = DW_ADDR_none;
11423
11424 /* If the pointer size or address class is different than the
11425 default, create a type variant marked as such and set the
11426 length accordingly. */
11427 if (TYPE_LENGTH (type) != byte_size || addr_class != DW_ADDR_none)
c906108c 11428 {
5e2b427d 11429 if (gdbarch_address_class_type_flags_p (gdbarch))
8b2dbe47
KB
11430 {
11431 int type_flags;
11432
849957d9 11433 type_flags = gdbarch_address_class_type_flags
5e2b427d 11434 (gdbarch, byte_size, addr_class);
876cecd0
TT
11435 gdb_assert ((type_flags & ~TYPE_INSTANCE_FLAG_ADDRESS_CLASS_ALL)
11436 == 0);
8b2dbe47
KB
11437 type = make_type_with_address_space (type, type_flags);
11438 }
11439 else if (TYPE_LENGTH (type) != byte_size)
11440 {
3e43a32a
MS
11441 complaint (&symfile_complaints,
11442 _("invalid pointer size %d"), byte_size);
8b2dbe47 11443 }
6e70227d 11444 else
9a619af0
MS
11445 {
11446 /* Should we also complain about unhandled address classes? */
11447 }
c906108c 11448 }
8b2dbe47
KB
11449
11450 TYPE_LENGTH (type) = byte_size;
f792889a 11451 return set_die_type (die, type, cu);
c906108c
SS
11452}
11453
11454/* Extract all information from a DW_TAG_ptr_to_member_type DIE and add to
11455 the user defined type vector. */
11456
f792889a 11457static struct type *
e7c27a73 11458read_tag_ptr_to_member_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c
SS
11459{
11460 struct type *type;
11461 struct type *to_type;
11462 struct type *domain;
11463
e7c27a73
DJ
11464 to_type = die_type (die, cu);
11465 domain = die_containing_type (die, cu);
0d5de010 11466
7e314c57
JK
11467 /* The calls above may have already set the type for this DIE. */
11468 type = get_die_type (die, cu);
11469 if (type)
11470 return type;
11471
0d5de010
DJ
11472 if (TYPE_CODE (check_typedef (to_type)) == TYPE_CODE_METHOD)
11473 type = lookup_methodptr_type (to_type);
11474 else
11475 type = lookup_memberptr_type (to_type, domain);
c906108c 11476
f792889a 11477 return set_die_type (die, type, cu);
c906108c
SS
11478}
11479
11480/* Extract all information from a DW_TAG_reference_type DIE and add to
11481 the user defined type vector. */
11482
f792889a 11483static struct type *
e7c27a73 11484read_tag_reference_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 11485{
e7c27a73 11486 struct comp_unit_head *cu_header = &cu->header;
7e314c57 11487 struct type *type, *target_type;
c906108c
SS
11488 struct attribute *attr;
11489
7e314c57
JK
11490 target_type = die_type (die, cu);
11491
11492 /* The die_type call above may have already set the type for this DIE. */
11493 type = get_die_type (die, cu);
11494 if (type)
11495 return type;
11496
11497 type = lookup_reference_type (target_type);
e142c38c 11498 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
c906108c
SS
11499 if (attr)
11500 {
11501 TYPE_LENGTH (type) = DW_UNSND (attr);
11502 }
11503 else
11504 {
107d2387 11505 TYPE_LENGTH (type) = cu_header->addr_size;
c906108c 11506 }
f792889a 11507 return set_die_type (die, type, cu);
c906108c
SS
11508}
11509
f792889a 11510static struct type *
e7c27a73 11511read_tag_const_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 11512{
f792889a 11513 struct type *base_type, *cv_type;
c906108c 11514
e7c27a73 11515 base_type = die_type (die, cu);
7e314c57
JK
11516
11517 /* The die_type call above may have already set the type for this DIE. */
11518 cv_type = get_die_type (die, cu);
11519 if (cv_type)
11520 return cv_type;
11521
2f608a3a
KW
11522 /* In case the const qualifier is applied to an array type, the element type
11523 is so qualified, not the array type (section 6.7.3 of C99). */
11524 if (TYPE_CODE (base_type) == TYPE_CODE_ARRAY)
11525 {
11526 struct type *el_type, *inner_array;
11527
11528 base_type = copy_type (base_type);
11529 inner_array = base_type;
11530
11531 while (TYPE_CODE (TYPE_TARGET_TYPE (inner_array)) == TYPE_CODE_ARRAY)
11532 {
11533 TYPE_TARGET_TYPE (inner_array) =
11534 copy_type (TYPE_TARGET_TYPE (inner_array));
11535 inner_array = TYPE_TARGET_TYPE (inner_array);
11536 }
11537
11538 el_type = TYPE_TARGET_TYPE (inner_array);
11539 TYPE_TARGET_TYPE (inner_array) =
11540 make_cv_type (1, TYPE_VOLATILE (el_type), el_type, NULL);
11541
11542 return set_die_type (die, base_type, cu);
11543 }
11544
f792889a
DJ
11545 cv_type = make_cv_type (1, TYPE_VOLATILE (base_type), base_type, 0);
11546 return set_die_type (die, cv_type, cu);
c906108c
SS
11547}
11548
f792889a 11549static struct type *
e7c27a73 11550read_tag_volatile_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 11551{
f792889a 11552 struct type *base_type, *cv_type;
c906108c 11553
e7c27a73 11554 base_type = die_type (die, cu);
7e314c57
JK
11555
11556 /* The die_type call above may have already set the type for this DIE. */
11557 cv_type = get_die_type (die, cu);
11558 if (cv_type)
11559 return cv_type;
11560
f792889a
DJ
11561 cv_type = make_cv_type (TYPE_CONST (base_type), 1, base_type, 0);
11562 return set_die_type (die, cv_type, cu);
c906108c
SS
11563}
11564
11565/* Extract all information from a DW_TAG_string_type DIE and add to
11566 the user defined type vector. It isn't really a user defined type,
11567 but it behaves like one, with other DIE's using an AT_user_def_type
11568 attribute to reference it. */
11569
f792889a 11570static struct type *
e7c27a73 11571read_tag_string_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 11572{
e7c27a73 11573 struct objfile *objfile = cu->objfile;
3b7538c0 11574 struct gdbarch *gdbarch = get_objfile_arch (objfile);
c906108c
SS
11575 struct type *type, *range_type, *index_type, *char_type;
11576 struct attribute *attr;
11577 unsigned int length;
11578
e142c38c 11579 attr = dwarf2_attr (die, DW_AT_string_length, cu);
c906108c
SS
11580 if (attr)
11581 {
11582 length = DW_UNSND (attr);
11583 }
11584 else
11585 {
0963b4bd 11586 /* Check for the DW_AT_byte_size attribute. */
e142c38c 11587 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
b21b22e0
PS
11588 if (attr)
11589 {
11590 length = DW_UNSND (attr);
11591 }
11592 else
11593 {
11594 length = 1;
11595 }
c906108c 11596 }
6ccb9162 11597
46bf5051 11598 index_type = objfile_type (objfile)->builtin_int;
c906108c 11599 range_type = create_range_type (NULL, index_type, 1, length);
3b7538c0
UW
11600 char_type = language_string_char_type (cu->language_defn, gdbarch);
11601 type = create_string_type (NULL, char_type, range_type);
6ccb9162 11602
f792889a 11603 return set_die_type (die, type, cu);
c906108c
SS
11604}
11605
11606/* Handle DIES due to C code like:
11607
11608 struct foo
c5aa993b
JM
11609 {
11610 int (*funcp)(int a, long l);
11611 int b;
11612 };
c906108c 11613
0963b4bd 11614 ('funcp' generates a DW_TAG_subroutine_type DIE). */
c906108c 11615
f792889a 11616static struct type *
e7c27a73 11617read_subroutine_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 11618{
bb5ed363 11619 struct objfile *objfile = cu->objfile;
0963b4bd
MS
11620 struct type *type; /* Type that this function returns. */
11621 struct type *ftype; /* Function that returns above type. */
c906108c
SS
11622 struct attribute *attr;
11623
e7c27a73 11624 type = die_type (die, cu);
7e314c57
JK
11625
11626 /* The die_type call above may have already set the type for this DIE. */
11627 ftype = get_die_type (die, cu);
11628 if (ftype)
11629 return ftype;
11630
0c8b41f1 11631 ftype = lookup_function_type (type);
c906108c 11632
5b8101ae 11633 /* All functions in C++, Pascal and Java have prototypes. */
e142c38c 11634 attr = dwarf2_attr (die, DW_AT_prototyped, cu);
c906108c 11635 if ((attr && (DW_UNSND (attr) != 0))
987504bb 11636 || cu->language == language_cplus
5b8101ae
PM
11637 || cu->language == language_java
11638 || cu->language == language_pascal)
876cecd0 11639 TYPE_PROTOTYPED (ftype) = 1;
a6c727b2
DJ
11640 else if (producer_is_realview (cu->producer))
11641 /* RealView does not emit DW_AT_prototyped. We can not
11642 distinguish prototyped and unprototyped functions; default to
11643 prototyped, since that is more common in modern code (and
11644 RealView warns about unprototyped functions). */
11645 TYPE_PROTOTYPED (ftype) = 1;
c906108c 11646
c055b101
CV
11647 /* Store the calling convention in the type if it's available in
11648 the subroutine die. Otherwise set the calling convention to
11649 the default value DW_CC_normal. */
11650 attr = dwarf2_attr (die, DW_AT_calling_convention, cu);
54fcddd0
UW
11651 if (attr)
11652 TYPE_CALLING_CONVENTION (ftype) = DW_UNSND (attr);
11653 else if (cu->producer && strstr (cu->producer, "IBM XL C for OpenCL"))
11654 TYPE_CALLING_CONVENTION (ftype) = DW_CC_GDB_IBM_OpenCL;
11655 else
11656 TYPE_CALLING_CONVENTION (ftype) = DW_CC_normal;
76c10ea2
GM
11657
11658 /* We need to add the subroutine type to the die immediately so
11659 we don't infinitely recurse when dealing with parameters
0963b4bd 11660 declared as the same subroutine type. */
76c10ea2 11661 set_die_type (die, ftype, cu);
6e70227d 11662
639d11d3 11663 if (die->child != NULL)
c906108c 11664 {
bb5ed363 11665 struct type *void_type = objfile_type (objfile)->builtin_void;
c906108c 11666 struct die_info *child_die;
8072405b 11667 int nparams, iparams;
c906108c
SS
11668
11669 /* Count the number of parameters.
11670 FIXME: GDB currently ignores vararg functions, but knows about
11671 vararg member functions. */
8072405b 11672 nparams = 0;
639d11d3 11673 child_die = die->child;
c906108c
SS
11674 while (child_die && child_die->tag)
11675 {
11676 if (child_die->tag == DW_TAG_formal_parameter)
11677 nparams++;
11678 else if (child_die->tag == DW_TAG_unspecified_parameters)
876cecd0 11679 TYPE_VARARGS (ftype) = 1;
c906108c
SS
11680 child_die = sibling_die (child_die);
11681 }
11682
11683 /* Allocate storage for parameters and fill them in. */
11684 TYPE_NFIELDS (ftype) = nparams;
11685 TYPE_FIELDS (ftype) = (struct field *)
ae5a43e0 11686 TYPE_ZALLOC (ftype, nparams * sizeof (struct field));
c906108c 11687
8072405b
JK
11688 /* TYPE_FIELD_TYPE must never be NULL. Pre-fill the array to ensure it
11689 even if we error out during the parameters reading below. */
11690 for (iparams = 0; iparams < nparams; iparams++)
11691 TYPE_FIELD_TYPE (ftype, iparams) = void_type;
11692
11693 iparams = 0;
639d11d3 11694 child_die = die->child;
c906108c
SS
11695 while (child_die && child_die->tag)
11696 {
11697 if (child_die->tag == DW_TAG_formal_parameter)
11698 {
3ce3b1ba
PA
11699 struct type *arg_type;
11700
11701 /* DWARF version 2 has no clean way to discern C++
11702 static and non-static member functions. G++ helps
11703 GDB by marking the first parameter for non-static
11704 member functions (which is the this pointer) as
11705 artificial. We pass this information to
11706 dwarf2_add_member_fn via TYPE_FIELD_ARTIFICIAL.
11707
11708 DWARF version 3 added DW_AT_object_pointer, which GCC
11709 4.5 does not yet generate. */
e142c38c 11710 attr = dwarf2_attr (child_die, DW_AT_artificial, cu);
c906108c
SS
11711 if (attr)
11712 TYPE_FIELD_ARTIFICIAL (ftype, iparams) = DW_UNSND (attr);
11713 else
418835cc
KS
11714 {
11715 TYPE_FIELD_ARTIFICIAL (ftype, iparams) = 0;
11716
11717 /* GCC/43521: In java, the formal parameter
11718 "this" is sometimes not marked with DW_AT_artificial. */
11719 if (cu->language == language_java)
11720 {
11721 const char *name = dwarf2_name (child_die, cu);
9a619af0 11722
418835cc
KS
11723 if (name && !strcmp (name, "this"))
11724 TYPE_FIELD_ARTIFICIAL (ftype, iparams) = 1;
11725 }
11726 }
3ce3b1ba
PA
11727 arg_type = die_type (child_die, cu);
11728
11729 /* RealView does not mark THIS as const, which the testsuite
11730 expects. GCC marks THIS as const in method definitions,
11731 but not in the class specifications (GCC PR 43053). */
11732 if (cu->language == language_cplus && !TYPE_CONST (arg_type)
11733 && TYPE_FIELD_ARTIFICIAL (ftype, iparams))
11734 {
11735 int is_this = 0;
11736 struct dwarf2_cu *arg_cu = cu;
11737 const char *name = dwarf2_name (child_die, cu);
11738
11739 attr = dwarf2_attr (die, DW_AT_object_pointer, cu);
11740 if (attr)
11741 {
11742 /* If the compiler emits this, use it. */
11743 if (follow_die_ref (die, attr, &arg_cu) == child_die)
11744 is_this = 1;
11745 }
11746 else if (name && strcmp (name, "this") == 0)
11747 /* Function definitions will have the argument names. */
11748 is_this = 1;
11749 else if (name == NULL && iparams == 0)
11750 /* Declarations may not have the names, so like
11751 elsewhere in GDB, assume an artificial first
11752 argument is "this". */
11753 is_this = 1;
11754
11755 if (is_this)
11756 arg_type = make_cv_type (1, TYPE_VOLATILE (arg_type),
11757 arg_type, 0);
11758 }
11759
11760 TYPE_FIELD_TYPE (ftype, iparams) = arg_type;
c906108c
SS
11761 iparams++;
11762 }
11763 child_die = sibling_die (child_die);
11764 }
11765 }
11766
76c10ea2 11767 return ftype;
c906108c
SS
11768}
11769
f792889a 11770static struct type *
e7c27a73 11771read_typedef (struct die_info *die, struct dwarf2_cu *cu)
c906108c 11772{
e7c27a73 11773 struct objfile *objfile = cu->objfile;
0114d602 11774 const char *name = NULL;
3c8e0968 11775 struct type *this_type, *target_type;
c906108c 11776
94af9270 11777 name = dwarf2_full_name (NULL, die, cu);
f792889a 11778 this_type = init_type (TYPE_CODE_TYPEDEF, 0,
0114d602
DJ
11779 TYPE_FLAG_TARGET_STUB, NULL, objfile);
11780 TYPE_NAME (this_type) = (char *) name;
f792889a 11781 set_die_type (die, this_type, cu);
3c8e0968
DE
11782 target_type = die_type (die, cu);
11783 if (target_type != this_type)
11784 TYPE_TARGET_TYPE (this_type) = target_type;
11785 else
11786 {
11787 /* Self-referential typedefs are, it seems, not allowed by the DWARF
11788 spec and cause infinite loops in GDB. */
11789 complaint (&symfile_complaints,
11790 _("Self-referential DW_TAG_typedef "
11791 "- DIE at 0x%x [in module %s]"),
b64f50a1 11792 die->offset.sect_off, objfile->name);
3c8e0968
DE
11793 TYPE_TARGET_TYPE (this_type) = NULL;
11794 }
f792889a 11795 return this_type;
c906108c
SS
11796}
11797
11798/* Find a representation of a given base type and install
11799 it in the TYPE field of the die. */
11800
f792889a 11801static struct type *
e7c27a73 11802read_base_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 11803{
e7c27a73 11804 struct objfile *objfile = cu->objfile;
c906108c
SS
11805 struct type *type;
11806 struct attribute *attr;
11807 int encoding = 0, size = 0;
39cbfefa 11808 char *name;
6ccb9162
UW
11809 enum type_code code = TYPE_CODE_INT;
11810 int type_flags = 0;
11811 struct type *target_type = NULL;
c906108c 11812
e142c38c 11813 attr = dwarf2_attr (die, DW_AT_encoding, cu);
c906108c
SS
11814 if (attr)
11815 {
11816 encoding = DW_UNSND (attr);
11817 }
e142c38c 11818 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
c906108c
SS
11819 if (attr)
11820 {
11821 size = DW_UNSND (attr);
11822 }
39cbfefa 11823 name = dwarf2_name (die, cu);
6ccb9162 11824 if (!name)
c906108c 11825 {
6ccb9162
UW
11826 complaint (&symfile_complaints,
11827 _("DW_AT_name missing from DW_TAG_base_type"));
c906108c 11828 }
6ccb9162
UW
11829
11830 switch (encoding)
c906108c 11831 {
6ccb9162
UW
11832 case DW_ATE_address:
11833 /* Turn DW_ATE_address into a void * pointer. */
11834 code = TYPE_CODE_PTR;
11835 type_flags |= TYPE_FLAG_UNSIGNED;
11836 target_type = init_type (TYPE_CODE_VOID, 1, 0, NULL, objfile);
11837 break;
11838 case DW_ATE_boolean:
11839 code = TYPE_CODE_BOOL;
11840 type_flags |= TYPE_FLAG_UNSIGNED;
11841 break;
11842 case DW_ATE_complex_float:
11843 code = TYPE_CODE_COMPLEX;
11844 target_type = init_type (TYPE_CODE_FLT, size / 2, 0, NULL, objfile);
11845 break;
11846 case DW_ATE_decimal_float:
11847 code = TYPE_CODE_DECFLOAT;
11848 break;
11849 case DW_ATE_float:
11850 code = TYPE_CODE_FLT;
11851 break;
11852 case DW_ATE_signed:
11853 break;
11854 case DW_ATE_unsigned:
11855 type_flags |= TYPE_FLAG_UNSIGNED;
3b2b8fea
TT
11856 if (cu->language == language_fortran
11857 && name
11858 && strncmp (name, "character(", sizeof ("character(") - 1) == 0)
11859 code = TYPE_CODE_CHAR;
6ccb9162
UW
11860 break;
11861 case DW_ATE_signed_char:
6e70227d 11862 if (cu->language == language_ada || cu->language == language_m2
3b2b8fea
TT
11863 || cu->language == language_pascal
11864 || cu->language == language_fortran)
6ccb9162
UW
11865 code = TYPE_CODE_CHAR;
11866 break;
11867 case DW_ATE_unsigned_char:
868a0084 11868 if (cu->language == language_ada || cu->language == language_m2
3b2b8fea
TT
11869 || cu->language == language_pascal
11870 || cu->language == language_fortran)
6ccb9162
UW
11871 code = TYPE_CODE_CHAR;
11872 type_flags |= TYPE_FLAG_UNSIGNED;
11873 break;
75079b2b
TT
11874 case DW_ATE_UTF:
11875 /* We just treat this as an integer and then recognize the
11876 type by name elsewhere. */
11877 break;
11878
6ccb9162
UW
11879 default:
11880 complaint (&symfile_complaints, _("unsupported DW_AT_encoding: '%s'"),
11881 dwarf_type_encoding_name (encoding));
11882 break;
c906108c 11883 }
6ccb9162 11884
0114d602
DJ
11885 type = init_type (code, size, type_flags, NULL, objfile);
11886 TYPE_NAME (type) = name;
6ccb9162
UW
11887 TYPE_TARGET_TYPE (type) = target_type;
11888
0114d602 11889 if (name && strcmp (name, "char") == 0)
876cecd0 11890 TYPE_NOSIGN (type) = 1;
0114d602 11891
f792889a 11892 return set_die_type (die, type, cu);
c906108c
SS
11893}
11894
a02abb62
JB
11895/* Read the given DW_AT_subrange DIE. */
11896
f792889a 11897static struct type *
a02abb62
JB
11898read_subrange_type (struct die_info *die, struct dwarf2_cu *cu)
11899{
11900 struct type *base_type;
11901 struct type *range_type;
11902 struct attribute *attr;
4fae6e18
JK
11903 LONGEST low, high;
11904 int low_default_is_valid;
39cbfefa 11905 char *name;
43bbcdc2 11906 LONGEST negative_mask;
e77813c8 11907
a02abb62 11908 base_type = die_type (die, cu);
953ac07e
JK
11909 /* Preserve BASE_TYPE's original type, just set its LENGTH. */
11910 check_typedef (base_type);
a02abb62 11911
7e314c57
JK
11912 /* The die_type call above may have already set the type for this DIE. */
11913 range_type = get_die_type (die, cu);
11914 if (range_type)
11915 return range_type;
11916
4fae6e18
JK
11917 /* Set LOW_DEFAULT_IS_VALID if current language and DWARF version allow
11918 omitting DW_AT_lower_bound. */
11919 switch (cu->language)
6e70227d 11920 {
4fae6e18
JK
11921 case language_c:
11922 case language_cplus:
11923 low = 0;
11924 low_default_is_valid = 1;
11925 break;
11926 case language_fortran:
11927 low = 1;
11928 low_default_is_valid = 1;
11929 break;
11930 case language_d:
11931 case language_java:
11932 case language_objc:
11933 low = 0;
11934 low_default_is_valid = (cu->header.version >= 4);
11935 break;
11936 case language_ada:
11937 case language_m2:
11938 case language_pascal:
a02abb62 11939 low = 1;
4fae6e18
JK
11940 low_default_is_valid = (cu->header.version >= 4);
11941 break;
11942 default:
11943 low = 0;
11944 low_default_is_valid = 0;
11945 break;
a02abb62
JB
11946 }
11947
dd5e6932
DJ
11948 /* FIXME: For variable sized arrays either of these could be
11949 a variable rather than a constant value. We'll allow it,
11950 but we don't know how to handle it. */
e142c38c 11951 attr = dwarf2_attr (die, DW_AT_lower_bound, cu);
a02abb62 11952 if (attr)
4fae6e18
JK
11953 low = dwarf2_get_attr_constant_value (attr, low);
11954 else if (!low_default_is_valid)
11955 complaint (&symfile_complaints, _("Missing DW_AT_lower_bound "
11956 "- DIE at 0x%x [in module %s]"),
11957 die->offset.sect_off, cu->objfile->name);
a02abb62 11958
e142c38c 11959 attr = dwarf2_attr (die, DW_AT_upper_bound, cu);
a02abb62 11960 if (attr)
6e70227d 11961 {
d48323d8 11962 if (attr_form_is_block (attr) || is_ref_attr (attr))
a02abb62
JB
11963 {
11964 /* GCC encodes arrays with unspecified or dynamic length
e77813c8 11965 with a DW_FORM_block1 attribute or a reference attribute.
a02abb62
JB
11966 FIXME: GDB does not yet know how to handle dynamic
11967 arrays properly, treat them as arrays with unspecified
11968 length for now.
11969
11970 FIXME: jimb/2003-09-22: GDB does not really know
11971 how to handle arrays of unspecified length
11972 either; we just represent them as zero-length
11973 arrays. Choose an appropriate upper bound given
11974 the lower bound we've computed above. */
11975 high = low - 1;
11976 }
11977 else
11978 high = dwarf2_get_attr_constant_value (attr, 1);
11979 }
e77813c8
PM
11980 else
11981 {
11982 attr = dwarf2_attr (die, DW_AT_count, cu);
11983 if (attr)
11984 {
11985 int count = dwarf2_get_attr_constant_value (attr, 1);
11986 high = low + count - 1;
11987 }
c2ff108b
JK
11988 else
11989 {
11990 /* Unspecified array length. */
11991 high = low - 1;
11992 }
e77813c8
PM
11993 }
11994
11995 /* Dwarf-2 specifications explicitly allows to create subrange types
11996 without specifying a base type.
11997 In that case, the base type must be set to the type of
11998 the lower bound, upper bound or count, in that order, if any of these
11999 three attributes references an object that has a type.
12000 If no base type is found, the Dwarf-2 specifications say that
12001 a signed integer type of size equal to the size of an address should
12002 be used.
12003 For the following C code: `extern char gdb_int [];'
12004 GCC produces an empty range DIE.
12005 FIXME: muller/2010-05-28: Possible references to object for low bound,
0963b4bd 12006 high bound or count are not yet handled by this code. */
e77813c8
PM
12007 if (TYPE_CODE (base_type) == TYPE_CODE_VOID)
12008 {
12009 struct objfile *objfile = cu->objfile;
12010 struct gdbarch *gdbarch = get_objfile_arch (objfile);
12011 int addr_size = gdbarch_addr_bit (gdbarch) /8;
12012 struct type *int_type = objfile_type (objfile)->builtin_int;
12013
12014 /* Test "int", "long int", and "long long int" objfile types,
12015 and select the first one having a size above or equal to the
12016 architecture address size. */
12017 if (int_type && TYPE_LENGTH (int_type) >= addr_size)
12018 base_type = int_type;
12019 else
12020 {
12021 int_type = objfile_type (objfile)->builtin_long;
12022 if (int_type && TYPE_LENGTH (int_type) >= addr_size)
12023 base_type = int_type;
12024 else
12025 {
12026 int_type = objfile_type (objfile)->builtin_long_long;
12027 if (int_type && TYPE_LENGTH (int_type) >= addr_size)
12028 base_type = int_type;
12029 }
12030 }
12031 }
a02abb62 12032
6e70227d 12033 negative_mask =
43bbcdc2
PH
12034 (LONGEST) -1 << (TYPE_LENGTH (base_type) * TARGET_CHAR_BIT - 1);
12035 if (!TYPE_UNSIGNED (base_type) && (low & negative_mask))
12036 low |= negative_mask;
12037 if (!TYPE_UNSIGNED (base_type) && (high & negative_mask))
12038 high |= negative_mask;
12039
a02abb62
JB
12040 range_type = create_range_type (NULL, base_type, low, high);
12041
bbb0eef6
JK
12042 /* Mark arrays with dynamic length at least as an array of unspecified
12043 length. GDB could check the boundary but before it gets implemented at
12044 least allow accessing the array elements. */
d48323d8 12045 if (attr && attr_form_is_block (attr))
bbb0eef6
JK
12046 TYPE_HIGH_BOUND_UNDEFINED (range_type) = 1;
12047
c2ff108b
JK
12048 /* Ada expects an empty array on no boundary attributes. */
12049 if (attr == NULL && cu->language != language_ada)
12050 TYPE_HIGH_BOUND_UNDEFINED (range_type) = 1;
12051
39cbfefa
DJ
12052 name = dwarf2_name (die, cu);
12053 if (name)
12054 TYPE_NAME (range_type) = name;
6e70227d 12055
e142c38c 12056 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
a02abb62
JB
12057 if (attr)
12058 TYPE_LENGTH (range_type) = DW_UNSND (attr);
12059
7e314c57
JK
12060 set_die_type (die, range_type, cu);
12061
12062 /* set_die_type should be already done. */
b4ba55a1
JB
12063 set_descriptive_type (range_type, die, cu);
12064
7e314c57 12065 return range_type;
a02abb62 12066}
6e70227d 12067
f792889a 12068static struct type *
81a17f79
JB
12069read_unspecified_type (struct die_info *die, struct dwarf2_cu *cu)
12070{
12071 struct type *type;
81a17f79 12072
81a17f79
JB
12073 /* For now, we only support the C meaning of an unspecified type: void. */
12074
0114d602
DJ
12075 type = init_type (TYPE_CODE_VOID, 0, 0, NULL, cu->objfile);
12076 TYPE_NAME (type) = dwarf2_name (die, cu);
81a17f79 12077
f792889a 12078 return set_die_type (die, type, cu);
81a17f79 12079}
a02abb62 12080
639d11d3
DC
12081/* Read a single die and all its descendents. Set the die's sibling
12082 field to NULL; set other fields in the die correctly, and set all
12083 of the descendents' fields correctly. Set *NEW_INFO_PTR to the
12084 location of the info_ptr after reading all of those dies. PARENT
12085 is the parent of the die in question. */
12086
12087static struct die_info *
dee91e82
DE
12088read_die_and_children (const struct die_reader_specs *reader,
12089 gdb_byte *info_ptr,
12090 gdb_byte **new_info_ptr,
12091 struct die_info *parent)
639d11d3
DC
12092{
12093 struct die_info *die;
fe1b8b76 12094 gdb_byte *cur_ptr;
639d11d3
DC
12095 int has_children;
12096
93311388 12097 cur_ptr = read_full_die (reader, &die, info_ptr, &has_children);
1d325ec1
DJ
12098 if (die == NULL)
12099 {
12100 *new_info_ptr = cur_ptr;
12101 return NULL;
12102 }
93311388 12103 store_in_ref_table (die, reader->cu);
639d11d3
DC
12104
12105 if (has_children)
348e048f 12106 die->child = read_die_and_siblings (reader, cur_ptr, new_info_ptr, die);
639d11d3
DC
12107 else
12108 {
12109 die->child = NULL;
12110 *new_info_ptr = cur_ptr;
12111 }
12112
12113 die->sibling = NULL;
12114 die->parent = parent;
12115 return die;
12116}
12117
12118/* Read a die, all of its descendents, and all of its siblings; set
12119 all of the fields of all of the dies correctly. Arguments are as
12120 in read_die_and_children. */
12121
12122static struct die_info *
93311388
DE
12123read_die_and_siblings (const struct die_reader_specs *reader,
12124 gdb_byte *info_ptr,
fe1b8b76 12125 gdb_byte **new_info_ptr,
639d11d3
DC
12126 struct die_info *parent)
12127{
12128 struct die_info *first_die, *last_sibling;
fe1b8b76 12129 gdb_byte *cur_ptr;
639d11d3 12130
c906108c 12131 cur_ptr = info_ptr;
639d11d3
DC
12132 first_die = last_sibling = NULL;
12133
12134 while (1)
c906108c 12135 {
639d11d3 12136 struct die_info *die
dee91e82 12137 = read_die_and_children (reader, cur_ptr, &cur_ptr, parent);
639d11d3 12138
1d325ec1 12139 if (die == NULL)
c906108c 12140 {
639d11d3
DC
12141 *new_info_ptr = cur_ptr;
12142 return first_die;
c906108c 12143 }
1d325ec1
DJ
12144
12145 if (!first_die)
12146 first_die = die;
c906108c 12147 else
1d325ec1
DJ
12148 last_sibling->sibling = die;
12149
12150 last_sibling = die;
c906108c 12151 }
c906108c
SS
12152}
12153
3019eac3
DE
12154/* Read a die and all its attributes, leave space for NUM_EXTRA_ATTRS
12155 attributes.
12156 The caller is responsible for filling in the extra attributes
12157 and updating (*DIEP)->num_attrs.
12158 Set DIEP to point to a newly allocated die with its information,
12159 except for its child, sibling, and parent fields.
12160 Set HAS_CHILDREN to tell whether the die has children or not. */
93311388
DE
12161
12162static gdb_byte *
3019eac3
DE
12163read_full_die_1 (const struct die_reader_specs *reader,
12164 struct die_info **diep, gdb_byte *info_ptr,
12165 int *has_children, int num_extra_attrs)
93311388 12166{
b64f50a1
JK
12167 unsigned int abbrev_number, bytes_read, i;
12168 sect_offset offset;
93311388
DE
12169 struct abbrev_info *abbrev;
12170 struct die_info *die;
12171 struct dwarf2_cu *cu = reader->cu;
12172 bfd *abfd = reader->abfd;
12173
b64f50a1 12174 offset.sect_off = info_ptr - reader->buffer;
93311388
DE
12175 abbrev_number = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
12176 info_ptr += bytes_read;
12177 if (!abbrev_number)
12178 {
12179 *diep = NULL;
12180 *has_children = 0;
12181 return info_ptr;
12182 }
12183
433df2d4 12184 abbrev = abbrev_table_lookup_abbrev (cu->abbrev_table, abbrev_number);
93311388 12185 if (!abbrev)
348e048f
DE
12186 error (_("Dwarf Error: could not find abbrev number %d [in module %s]"),
12187 abbrev_number,
12188 bfd_get_filename (abfd));
12189
3019eac3 12190 die = dwarf_alloc_die (cu, abbrev->num_attrs + num_extra_attrs);
93311388
DE
12191 die->offset = offset;
12192 die->tag = abbrev->tag;
12193 die->abbrev = abbrev_number;
12194
3019eac3
DE
12195 /* Make the result usable.
12196 The caller needs to update num_attrs after adding the extra
12197 attributes. */
93311388
DE
12198 die->num_attrs = abbrev->num_attrs;
12199
12200 for (i = 0; i < abbrev->num_attrs; ++i)
dee91e82
DE
12201 info_ptr = read_attribute (reader, &die->attrs[i], &abbrev->attrs[i],
12202 info_ptr);
93311388
DE
12203
12204 *diep = die;
12205 *has_children = abbrev->has_children;
12206 return info_ptr;
12207}
12208
3019eac3
DE
12209/* Read a die and all its attributes.
12210 Set DIEP to point to a newly allocated die with its information,
12211 except for its child, sibling, and parent fields.
12212 Set HAS_CHILDREN to tell whether the die has children or not. */
12213
12214static gdb_byte *
12215read_full_die (const struct die_reader_specs *reader,
12216 struct die_info **diep, gdb_byte *info_ptr,
12217 int *has_children)
12218{
12219 return read_full_die_1 (reader, diep, info_ptr, has_children, 0);
12220}
433df2d4
DE
12221\f
12222/* Abbreviation tables.
3019eac3 12223
433df2d4 12224 In DWARF version 2, the description of the debugging information is
c906108c
SS
12225 stored in a separate .debug_abbrev section. Before we read any
12226 dies from a section we read in all abbreviations and install them
433df2d4
DE
12227 in a hash table. */
12228
12229/* Allocate space for a struct abbrev_info object in ABBREV_TABLE. */
12230
12231static struct abbrev_info *
12232abbrev_table_alloc_abbrev (struct abbrev_table *abbrev_table)
12233{
12234 struct abbrev_info *abbrev;
12235
12236 abbrev = (struct abbrev_info *)
12237 obstack_alloc (&abbrev_table->abbrev_obstack, sizeof (struct abbrev_info));
12238 memset (abbrev, 0, sizeof (struct abbrev_info));
12239 return abbrev;
12240}
12241
12242/* Add an abbreviation to the table. */
c906108c
SS
12243
12244static void
433df2d4
DE
12245abbrev_table_add_abbrev (struct abbrev_table *abbrev_table,
12246 unsigned int abbrev_number,
12247 struct abbrev_info *abbrev)
12248{
12249 unsigned int hash_number;
12250
12251 hash_number = abbrev_number % ABBREV_HASH_SIZE;
12252 abbrev->next = abbrev_table->abbrevs[hash_number];
12253 abbrev_table->abbrevs[hash_number] = abbrev;
12254}
dee91e82 12255
433df2d4
DE
12256/* Look up an abbrev in the table.
12257 Returns NULL if the abbrev is not found. */
12258
12259static struct abbrev_info *
12260abbrev_table_lookup_abbrev (const struct abbrev_table *abbrev_table,
12261 unsigned int abbrev_number)
c906108c 12262{
433df2d4
DE
12263 unsigned int hash_number;
12264 struct abbrev_info *abbrev;
12265
12266 hash_number = abbrev_number % ABBREV_HASH_SIZE;
12267 abbrev = abbrev_table->abbrevs[hash_number];
12268
12269 while (abbrev)
12270 {
12271 if (abbrev->number == abbrev_number)
12272 return abbrev;
12273 abbrev = abbrev->next;
12274 }
12275 return NULL;
12276}
12277
12278/* Read in an abbrev table. */
12279
12280static struct abbrev_table *
12281abbrev_table_read_table (struct dwarf2_section_info *section,
12282 sect_offset offset)
12283{
12284 struct objfile *objfile = dwarf2_per_objfile->objfile;
12285 bfd *abfd = section->asection->owner;
12286 struct abbrev_table *abbrev_table;
fe1b8b76 12287 gdb_byte *abbrev_ptr;
c906108c
SS
12288 struct abbrev_info *cur_abbrev;
12289 unsigned int abbrev_number, bytes_read, abbrev_name;
433df2d4 12290 unsigned int abbrev_form;
f3dd6933
DJ
12291 struct attr_abbrev *cur_attrs;
12292 unsigned int allocated_attrs;
c906108c 12293
433df2d4 12294 abbrev_table = XMALLOC (struct abbrev_table);
f4dc4d17 12295 abbrev_table->offset = offset;
433df2d4
DE
12296 obstack_init (&abbrev_table->abbrev_obstack);
12297 abbrev_table->abbrevs = obstack_alloc (&abbrev_table->abbrev_obstack,
12298 (ABBREV_HASH_SIZE
12299 * sizeof (struct abbrev_info *)));
12300 memset (abbrev_table->abbrevs, 0,
12301 ABBREV_HASH_SIZE * sizeof (struct abbrev_info *));
c906108c 12302
433df2d4
DE
12303 dwarf2_read_section (objfile, section);
12304 abbrev_ptr = section->buffer + offset.sect_off;
c906108c
SS
12305 abbrev_number = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
12306 abbrev_ptr += bytes_read;
12307
f3dd6933
DJ
12308 allocated_attrs = ATTR_ALLOC_CHUNK;
12309 cur_attrs = xmalloc (allocated_attrs * sizeof (struct attr_abbrev));
6e70227d 12310
0963b4bd 12311 /* Loop until we reach an abbrev number of 0. */
c906108c
SS
12312 while (abbrev_number)
12313 {
433df2d4 12314 cur_abbrev = abbrev_table_alloc_abbrev (abbrev_table);
c906108c
SS
12315
12316 /* read in abbrev header */
12317 cur_abbrev->number = abbrev_number;
12318 cur_abbrev->tag = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
12319 abbrev_ptr += bytes_read;
12320 cur_abbrev->has_children = read_1_byte (abfd, abbrev_ptr);
12321 abbrev_ptr += 1;
12322
12323 /* now read in declarations */
12324 abbrev_name = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
12325 abbrev_ptr += bytes_read;
12326 abbrev_form = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
12327 abbrev_ptr += bytes_read;
12328 while (abbrev_name)
12329 {
f3dd6933 12330 if (cur_abbrev->num_attrs == allocated_attrs)
c906108c 12331 {
f3dd6933
DJ
12332 allocated_attrs += ATTR_ALLOC_CHUNK;
12333 cur_attrs
12334 = xrealloc (cur_attrs, (allocated_attrs
12335 * sizeof (struct attr_abbrev)));
c906108c 12336 }
ae038cb0 12337
f3dd6933
DJ
12338 cur_attrs[cur_abbrev->num_attrs].name = abbrev_name;
12339 cur_attrs[cur_abbrev->num_attrs++].form = abbrev_form;
c906108c
SS
12340 abbrev_name = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
12341 abbrev_ptr += bytes_read;
12342 abbrev_form = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
12343 abbrev_ptr += bytes_read;
12344 }
12345
433df2d4 12346 cur_abbrev->attrs = obstack_alloc (&abbrev_table->abbrev_obstack,
f3dd6933
DJ
12347 (cur_abbrev->num_attrs
12348 * sizeof (struct attr_abbrev)));
12349 memcpy (cur_abbrev->attrs, cur_attrs,
12350 cur_abbrev->num_attrs * sizeof (struct attr_abbrev));
12351
433df2d4 12352 abbrev_table_add_abbrev (abbrev_table, abbrev_number, cur_abbrev);
c906108c
SS
12353
12354 /* Get next abbreviation.
12355 Under Irix6 the abbreviations for a compilation unit are not
c5aa993b
JM
12356 always properly terminated with an abbrev number of 0.
12357 Exit loop if we encounter an abbreviation which we have
12358 already read (which means we are about to read the abbreviations
12359 for the next compile unit) or if the end of the abbreviation
12360 table is reached. */
433df2d4 12361 if ((unsigned int) (abbrev_ptr - section->buffer) >= section->size)
c906108c
SS
12362 break;
12363 abbrev_number = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
12364 abbrev_ptr += bytes_read;
433df2d4 12365 if (abbrev_table_lookup_abbrev (abbrev_table, abbrev_number) != NULL)
c906108c
SS
12366 break;
12367 }
f3dd6933
DJ
12368
12369 xfree (cur_attrs);
433df2d4 12370 return abbrev_table;
c906108c
SS
12371}
12372
433df2d4 12373/* Free the resources held by ABBREV_TABLE. */
c906108c 12374
c906108c 12375static void
433df2d4 12376abbrev_table_free (struct abbrev_table *abbrev_table)
c906108c 12377{
433df2d4
DE
12378 obstack_free (&abbrev_table->abbrev_obstack, NULL);
12379 xfree (abbrev_table);
c906108c
SS
12380}
12381
f4dc4d17
DE
12382/* Same as abbrev_table_free but as a cleanup.
12383 We pass in a pointer to the pointer to the table so that we can
12384 set the pointer to NULL when we're done. It also simplifies
12385 build_type_unit_groups. */
12386
12387static void
12388abbrev_table_free_cleanup (void *table_ptr)
12389{
12390 struct abbrev_table **abbrev_table_ptr = table_ptr;
12391
12392 if (*abbrev_table_ptr != NULL)
12393 abbrev_table_free (*abbrev_table_ptr);
12394 *abbrev_table_ptr = NULL;
12395}
12396
433df2d4
DE
12397/* Read the abbrev table for CU from ABBREV_SECTION. */
12398
12399static void
12400dwarf2_read_abbrevs (struct dwarf2_cu *cu,
12401 struct dwarf2_section_info *abbrev_section)
c906108c 12402{
433df2d4
DE
12403 cu->abbrev_table =
12404 abbrev_table_read_table (abbrev_section, cu->header.abbrev_offset);
12405}
c906108c 12406
433df2d4 12407/* Release the memory used by the abbrev table for a compilation unit. */
c906108c 12408
433df2d4
DE
12409static void
12410dwarf2_free_abbrev_table (void *ptr_to_cu)
12411{
12412 struct dwarf2_cu *cu = ptr_to_cu;
c906108c 12413
433df2d4
DE
12414 abbrev_table_free (cu->abbrev_table);
12415 /* Set this to NULL so that we SEGV if we try to read it later,
12416 and also because free_comp_unit verifies this is NULL. */
12417 cu->abbrev_table = NULL;
12418}
12419\f
72bf9492
DJ
12420/* Returns nonzero if TAG represents a type that we might generate a partial
12421 symbol for. */
12422
12423static int
12424is_type_tag_for_partial (int tag)
12425{
12426 switch (tag)
12427 {
12428#if 0
12429 /* Some types that would be reasonable to generate partial symbols for,
12430 that we don't at present. */
12431 case DW_TAG_array_type:
12432 case DW_TAG_file_type:
12433 case DW_TAG_ptr_to_member_type:
12434 case DW_TAG_set_type:
12435 case DW_TAG_string_type:
12436 case DW_TAG_subroutine_type:
12437#endif
12438 case DW_TAG_base_type:
12439 case DW_TAG_class_type:
680b30c7 12440 case DW_TAG_interface_type:
72bf9492
DJ
12441 case DW_TAG_enumeration_type:
12442 case DW_TAG_structure_type:
12443 case DW_TAG_subrange_type:
12444 case DW_TAG_typedef:
12445 case DW_TAG_union_type:
12446 return 1;
12447 default:
12448 return 0;
12449 }
12450}
12451
12452/* Load all DIEs that are interesting for partial symbols into memory. */
12453
12454static struct partial_die_info *
dee91e82
DE
12455load_partial_dies (const struct die_reader_specs *reader,
12456 gdb_byte *info_ptr, int building_psymtab)
72bf9492 12457{
dee91e82 12458 struct dwarf2_cu *cu = reader->cu;
bb5ed363 12459 struct objfile *objfile = cu->objfile;
72bf9492
DJ
12460 struct partial_die_info *part_die;
12461 struct partial_die_info *parent_die, *last_die, *first_die = NULL;
12462 struct abbrev_info *abbrev;
12463 unsigned int bytes_read;
5afb4e99 12464 unsigned int load_all = 0;
72bf9492
DJ
12465 int nesting_level = 1;
12466
12467 parent_die = NULL;
12468 last_die = NULL;
12469
7adf1e79
DE
12470 gdb_assert (cu->per_cu != NULL);
12471 if (cu->per_cu->load_all_dies)
5afb4e99
DJ
12472 load_all = 1;
12473
72bf9492
DJ
12474 cu->partial_dies
12475 = htab_create_alloc_ex (cu->header.length / 12,
12476 partial_die_hash,
12477 partial_die_eq,
12478 NULL,
12479 &cu->comp_unit_obstack,
12480 hashtab_obstack_allocate,
12481 dummy_obstack_deallocate);
12482
12483 part_die = obstack_alloc (&cu->comp_unit_obstack,
12484 sizeof (struct partial_die_info));
12485
12486 while (1)
12487 {
12488 abbrev = peek_die_abbrev (info_ptr, &bytes_read, cu);
12489
12490 /* A NULL abbrev means the end of a series of children. */
12491 if (abbrev == NULL)
12492 {
12493 if (--nesting_level == 0)
12494 {
12495 /* PART_DIE was probably the last thing allocated on the
12496 comp_unit_obstack, so we could call obstack_free
12497 here. We don't do that because the waste is small,
12498 and will be cleaned up when we're done with this
12499 compilation unit. This way, we're also more robust
12500 against other users of the comp_unit_obstack. */
12501 return first_die;
12502 }
12503 info_ptr += bytes_read;
12504 last_die = parent_die;
12505 parent_die = parent_die->die_parent;
12506 continue;
12507 }
12508
98bfdba5
PA
12509 /* Check for template arguments. We never save these; if
12510 they're seen, we just mark the parent, and go on our way. */
12511 if (parent_die != NULL
12512 && cu->language == language_cplus
12513 && (abbrev->tag == DW_TAG_template_type_param
12514 || abbrev->tag == DW_TAG_template_value_param))
12515 {
12516 parent_die->has_template_arguments = 1;
12517
12518 if (!load_all)
12519 {
12520 /* We don't need a partial DIE for the template argument. */
dee91e82 12521 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
98bfdba5
PA
12522 continue;
12523 }
12524 }
12525
0d99eb77 12526 /* We only recurse into c++ subprograms looking for template arguments.
98bfdba5
PA
12527 Skip their other children. */
12528 if (!load_all
12529 && cu->language == language_cplus
12530 && parent_die != NULL
12531 && parent_die->tag == DW_TAG_subprogram)
12532 {
dee91e82 12533 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
98bfdba5
PA
12534 continue;
12535 }
12536
5afb4e99
DJ
12537 /* Check whether this DIE is interesting enough to save. Normally
12538 we would not be interested in members here, but there may be
12539 later variables referencing them via DW_AT_specification (for
12540 static members). */
12541 if (!load_all
12542 && !is_type_tag_for_partial (abbrev->tag)
72929c62 12543 && abbrev->tag != DW_TAG_constant
72bf9492
DJ
12544 && abbrev->tag != DW_TAG_enumerator
12545 && abbrev->tag != DW_TAG_subprogram
bc30ff58 12546 && abbrev->tag != DW_TAG_lexical_block
72bf9492 12547 && abbrev->tag != DW_TAG_variable
5afb4e99 12548 && abbrev->tag != DW_TAG_namespace
f55ee35c 12549 && abbrev->tag != DW_TAG_module
95554aad
TT
12550 && abbrev->tag != DW_TAG_member
12551 && abbrev->tag != DW_TAG_imported_unit)
72bf9492
DJ
12552 {
12553 /* Otherwise we skip to the next sibling, if any. */
dee91e82 12554 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
72bf9492
DJ
12555 continue;
12556 }
12557
dee91e82
DE
12558 info_ptr = read_partial_die (reader, part_die, abbrev, bytes_read,
12559 info_ptr);
72bf9492
DJ
12560
12561 /* This two-pass algorithm for processing partial symbols has a
12562 high cost in cache pressure. Thus, handle some simple cases
12563 here which cover the majority of C partial symbols. DIEs
12564 which neither have specification tags in them, nor could have
12565 specification tags elsewhere pointing at them, can simply be
12566 processed and discarded.
12567
12568 This segment is also optional; scan_partial_symbols and
12569 add_partial_symbol will handle these DIEs if we chain
12570 them in normally. When compilers which do not emit large
12571 quantities of duplicate debug information are more common,
12572 this code can probably be removed. */
12573
12574 /* Any complete simple types at the top level (pretty much all
12575 of them, for a language without namespaces), can be processed
12576 directly. */
12577 if (parent_die == NULL
12578 && part_die->has_specification == 0
12579 && part_die->is_declaration == 0
d8228535 12580 && ((part_die->tag == DW_TAG_typedef && !part_die->has_children)
72bf9492
DJ
12581 || part_die->tag == DW_TAG_base_type
12582 || part_die->tag == DW_TAG_subrange_type))
12583 {
12584 if (building_psymtab && part_die->name != NULL)
04a679b8 12585 add_psymbol_to_list (part_die->name, strlen (part_die->name), 0,
72bf9492 12586 VAR_DOMAIN, LOC_TYPEDEF,
bb5ed363
DE
12587 &objfile->static_psymbols,
12588 0, (CORE_ADDR) 0, cu->language, objfile);
dee91e82 12589 info_ptr = locate_pdi_sibling (reader, part_die, info_ptr);
72bf9492
DJ
12590 continue;
12591 }
12592
d8228535
JK
12593 /* The exception for DW_TAG_typedef with has_children above is
12594 a workaround of GCC PR debug/47510. In the case of this complaint
12595 type_name_no_tag_or_error will error on such types later.
12596
12597 GDB skipped children of DW_TAG_typedef by the shortcut above and then
12598 it could not find the child DIEs referenced later, this is checked
12599 above. In correct DWARF DW_TAG_typedef should have no children. */
12600
12601 if (part_die->tag == DW_TAG_typedef && part_die->has_children)
12602 complaint (&symfile_complaints,
12603 _("DW_TAG_typedef has childen - GCC PR debug/47510 bug "
12604 "- DIE at 0x%x [in module %s]"),
b64f50a1 12605 part_die->offset.sect_off, objfile->name);
d8228535 12606
72bf9492
DJ
12607 /* If we're at the second level, and we're an enumerator, and
12608 our parent has no specification (meaning possibly lives in a
12609 namespace elsewhere), then we can add the partial symbol now
12610 instead of queueing it. */
12611 if (part_die->tag == DW_TAG_enumerator
12612 && parent_die != NULL
12613 && parent_die->die_parent == NULL
12614 && parent_die->tag == DW_TAG_enumeration_type
12615 && parent_die->has_specification == 0)
12616 {
12617 if (part_die->name == NULL)
3e43a32a
MS
12618 complaint (&symfile_complaints,
12619 _("malformed enumerator DIE ignored"));
72bf9492 12620 else if (building_psymtab)
04a679b8 12621 add_psymbol_to_list (part_die->name, strlen (part_die->name), 0,
72bf9492 12622 VAR_DOMAIN, LOC_CONST,
987504bb
JJ
12623 (cu->language == language_cplus
12624 || cu->language == language_java)
bb5ed363
DE
12625 ? &objfile->global_psymbols
12626 : &objfile->static_psymbols,
12627 0, (CORE_ADDR) 0, cu->language, objfile);
72bf9492 12628
dee91e82 12629 info_ptr = locate_pdi_sibling (reader, part_die, info_ptr);
72bf9492
DJ
12630 continue;
12631 }
12632
12633 /* We'll save this DIE so link it in. */
12634 part_die->die_parent = parent_die;
12635 part_die->die_sibling = NULL;
12636 part_die->die_child = NULL;
12637
12638 if (last_die && last_die == parent_die)
12639 last_die->die_child = part_die;
12640 else if (last_die)
12641 last_die->die_sibling = part_die;
12642
12643 last_die = part_die;
12644
12645 if (first_die == NULL)
12646 first_die = part_die;
12647
12648 /* Maybe add the DIE to the hash table. Not all DIEs that we
12649 find interesting need to be in the hash table, because we
12650 also have the parent/sibling/child chains; only those that we
12651 might refer to by offset later during partial symbol reading.
12652
12653 For now this means things that might have be the target of a
12654 DW_AT_specification, DW_AT_abstract_origin, or
12655 DW_AT_extension. DW_AT_extension will refer only to
12656 namespaces; DW_AT_abstract_origin refers to functions (and
12657 many things under the function DIE, but we do not recurse
12658 into function DIEs during partial symbol reading) and
12659 possibly variables as well; DW_AT_specification refers to
12660 declarations. Declarations ought to have the DW_AT_declaration
12661 flag. It happens that GCC forgets to put it in sometimes, but
12662 only for functions, not for types.
12663
12664 Adding more things than necessary to the hash table is harmless
12665 except for the performance cost. Adding too few will result in
5afb4e99
DJ
12666 wasted time in find_partial_die, when we reread the compilation
12667 unit with load_all_dies set. */
72bf9492 12668
5afb4e99 12669 if (load_all
72929c62 12670 || abbrev->tag == DW_TAG_constant
5afb4e99 12671 || abbrev->tag == DW_TAG_subprogram
72bf9492
DJ
12672 || abbrev->tag == DW_TAG_variable
12673 || abbrev->tag == DW_TAG_namespace
12674 || part_die->is_declaration)
12675 {
12676 void **slot;
12677
12678 slot = htab_find_slot_with_hash (cu->partial_dies, part_die,
b64f50a1 12679 part_die->offset.sect_off, INSERT);
72bf9492
DJ
12680 *slot = part_die;
12681 }
12682
12683 part_die = obstack_alloc (&cu->comp_unit_obstack,
12684 sizeof (struct partial_die_info));
12685
12686 /* For some DIEs we want to follow their children (if any). For C
bc30ff58 12687 we have no reason to follow the children of structures; for other
98bfdba5
PA
12688 languages we have to, so that we can get at method physnames
12689 to infer fully qualified class names, for DW_AT_specification,
12690 and for C++ template arguments. For C++, we also look one level
12691 inside functions to find template arguments (if the name of the
12692 function does not already contain the template arguments).
bc30ff58
JB
12693
12694 For Ada, we need to scan the children of subprograms and lexical
12695 blocks as well because Ada allows the definition of nested
12696 entities that could be interesting for the debugger, such as
12697 nested subprograms for instance. */
72bf9492 12698 if (last_die->has_children
5afb4e99
DJ
12699 && (load_all
12700 || last_die->tag == DW_TAG_namespace
f55ee35c 12701 || last_die->tag == DW_TAG_module
72bf9492 12702 || last_die->tag == DW_TAG_enumeration_type
98bfdba5
PA
12703 || (cu->language == language_cplus
12704 && last_die->tag == DW_TAG_subprogram
12705 && (last_die->name == NULL
12706 || strchr (last_die->name, '<') == NULL))
72bf9492
DJ
12707 || (cu->language != language_c
12708 && (last_die->tag == DW_TAG_class_type
680b30c7 12709 || last_die->tag == DW_TAG_interface_type
72bf9492 12710 || last_die->tag == DW_TAG_structure_type
bc30ff58
JB
12711 || last_die->tag == DW_TAG_union_type))
12712 || (cu->language == language_ada
12713 && (last_die->tag == DW_TAG_subprogram
12714 || last_die->tag == DW_TAG_lexical_block))))
72bf9492
DJ
12715 {
12716 nesting_level++;
12717 parent_die = last_die;
12718 continue;
12719 }
12720
12721 /* Otherwise we skip to the next sibling, if any. */
dee91e82 12722 info_ptr = locate_pdi_sibling (reader, last_die, info_ptr);
72bf9492
DJ
12723
12724 /* Back to the top, do it again. */
12725 }
12726}
12727
c906108c
SS
12728/* Read a minimal amount of information into the minimal die structure. */
12729
fe1b8b76 12730static gdb_byte *
dee91e82
DE
12731read_partial_die (const struct die_reader_specs *reader,
12732 struct partial_die_info *part_die,
12733 struct abbrev_info *abbrev, unsigned int abbrev_len,
12734 gdb_byte *info_ptr)
c906108c 12735{
dee91e82 12736 struct dwarf2_cu *cu = reader->cu;
bb5ed363 12737 struct objfile *objfile = cu->objfile;
dee91e82 12738 gdb_byte *buffer = reader->buffer;
fa238c03 12739 unsigned int i;
c906108c 12740 struct attribute attr;
c5aa993b 12741 int has_low_pc_attr = 0;
c906108c 12742 int has_high_pc_attr = 0;
91da1414 12743 int high_pc_relative = 0;
c906108c 12744
72bf9492 12745 memset (part_die, 0, sizeof (struct partial_die_info));
c906108c 12746
b64f50a1 12747 part_die->offset.sect_off = info_ptr - buffer;
72bf9492
DJ
12748
12749 info_ptr += abbrev_len;
12750
12751 if (abbrev == NULL)
12752 return info_ptr;
12753
c906108c
SS
12754 part_die->tag = abbrev->tag;
12755 part_die->has_children = abbrev->has_children;
c906108c
SS
12756
12757 for (i = 0; i < abbrev->num_attrs; ++i)
12758 {
dee91e82 12759 info_ptr = read_attribute (reader, &attr, &abbrev->attrs[i], info_ptr);
c906108c
SS
12760
12761 /* Store the data if it is of an attribute we want to keep in a
c5aa993b 12762 partial symbol table. */
c906108c
SS
12763 switch (attr.name)
12764 {
12765 case DW_AT_name:
71c25dea
TT
12766 switch (part_die->tag)
12767 {
12768 case DW_TAG_compile_unit:
95554aad 12769 case DW_TAG_partial_unit:
348e048f 12770 case DW_TAG_type_unit:
71c25dea
TT
12771 /* Compilation units have a DW_AT_name that is a filename, not
12772 a source language identifier. */
12773 case DW_TAG_enumeration_type:
12774 case DW_TAG_enumerator:
12775 /* These tags always have simple identifiers already; no need
12776 to canonicalize them. */
12777 part_die->name = DW_STRING (&attr);
12778 break;
12779 default:
12780 part_die->name
12781 = dwarf2_canonicalize_name (DW_STRING (&attr), cu,
bb5ed363 12782 &objfile->objfile_obstack);
71c25dea
TT
12783 break;
12784 }
c906108c 12785 break;
31ef98ae 12786 case DW_AT_linkage_name:
c906108c 12787 case DW_AT_MIPS_linkage_name:
31ef98ae
TT
12788 /* Note that both forms of linkage name might appear. We
12789 assume they will be the same, and we only store the last
12790 one we see. */
94af9270
KS
12791 if (cu->language == language_ada)
12792 part_die->name = DW_STRING (&attr);
abc72ce4 12793 part_die->linkage_name = DW_STRING (&attr);
c906108c
SS
12794 break;
12795 case DW_AT_low_pc:
12796 has_low_pc_attr = 1;
12797 part_die->lowpc = DW_ADDR (&attr);
12798 break;
12799 case DW_AT_high_pc:
12800 has_high_pc_attr = 1;
3019eac3
DE
12801 if (attr.form == DW_FORM_addr
12802 || attr.form == DW_FORM_GNU_addr_index)
91da1414
MW
12803 part_die->highpc = DW_ADDR (&attr);
12804 else
12805 {
12806 high_pc_relative = 1;
12807 part_die->highpc = DW_UNSND (&attr);
12808 }
c906108c
SS
12809 break;
12810 case DW_AT_location:
0963b4bd 12811 /* Support the .debug_loc offsets. */
8e19ed76
PS
12812 if (attr_form_is_block (&attr))
12813 {
95554aad 12814 part_die->d.locdesc = DW_BLOCK (&attr);
8e19ed76 12815 }
3690dd37 12816 else if (attr_form_is_section_offset (&attr))
8e19ed76 12817 {
4d3c2250 12818 dwarf2_complex_location_expr_complaint ();
8e19ed76
PS
12819 }
12820 else
12821 {
4d3c2250
KB
12822 dwarf2_invalid_attrib_class_complaint ("DW_AT_location",
12823 "partial symbol information");
8e19ed76 12824 }
c906108c 12825 break;
c906108c
SS
12826 case DW_AT_external:
12827 part_die->is_external = DW_UNSND (&attr);
12828 break;
12829 case DW_AT_declaration:
12830 part_die->is_declaration = DW_UNSND (&attr);
12831 break;
12832 case DW_AT_type:
12833 part_die->has_type = 1;
12834 break;
12835 case DW_AT_abstract_origin:
12836 case DW_AT_specification:
72bf9492
DJ
12837 case DW_AT_extension:
12838 part_die->has_specification = 1;
c764a876 12839 part_die->spec_offset = dwarf2_get_ref_die_offset (&attr);
36586728
TT
12840 part_die->spec_is_dwz = (attr.form == DW_FORM_GNU_ref_alt
12841 || cu->per_cu->is_dwz);
c906108c
SS
12842 break;
12843 case DW_AT_sibling:
12844 /* Ignore absolute siblings, they might point outside of
12845 the current compile unit. */
12846 if (attr.form == DW_FORM_ref_addr)
3e43a32a
MS
12847 complaint (&symfile_complaints,
12848 _("ignoring absolute DW_AT_sibling"));
c906108c 12849 else
b64f50a1 12850 part_die->sibling = buffer + dwarf2_get_ref_die_offset (&attr).sect_off;
c906108c 12851 break;
fa4028e9
JB
12852 case DW_AT_byte_size:
12853 part_die->has_byte_size = 1;
12854 break;
68511cec
CES
12855 case DW_AT_calling_convention:
12856 /* DWARF doesn't provide a way to identify a program's source-level
12857 entry point. DW_AT_calling_convention attributes are only meant
12858 to describe functions' calling conventions.
12859
12860 However, because it's a necessary piece of information in
12861 Fortran, and because DW_CC_program is the only piece of debugging
12862 information whose definition refers to a 'main program' at all,
12863 several compilers have begun marking Fortran main programs with
12864 DW_CC_program --- even when those functions use the standard
12865 calling conventions.
12866
12867 So until DWARF specifies a way to provide this information and
12868 compilers pick up the new representation, we'll support this
12869 practice. */
12870 if (DW_UNSND (&attr) == DW_CC_program
12871 && cu->language == language_fortran)
01f8c46d
JK
12872 {
12873 set_main_name (part_die->name);
12874
12875 /* As this DIE has a static linkage the name would be difficult
12876 to look up later. */
12877 language_of_main = language_fortran;
12878 }
68511cec 12879 break;
481860b3
GB
12880 case DW_AT_inline:
12881 if (DW_UNSND (&attr) == DW_INL_inlined
12882 || DW_UNSND (&attr) == DW_INL_declared_inlined)
12883 part_die->may_be_inlined = 1;
12884 break;
95554aad
TT
12885
12886 case DW_AT_import:
12887 if (part_die->tag == DW_TAG_imported_unit)
36586728
TT
12888 {
12889 part_die->d.offset = dwarf2_get_ref_die_offset (&attr);
12890 part_die->is_dwz = (attr.form == DW_FORM_GNU_ref_alt
12891 || cu->per_cu->is_dwz);
12892 }
95554aad
TT
12893 break;
12894
c906108c
SS
12895 default:
12896 break;
12897 }
12898 }
12899
91da1414
MW
12900 if (high_pc_relative)
12901 part_die->highpc += part_die->lowpc;
12902
9373cf26
JK
12903 if (has_low_pc_attr && has_high_pc_attr)
12904 {
12905 /* When using the GNU linker, .gnu.linkonce. sections are used to
12906 eliminate duplicate copies of functions and vtables and such.
12907 The linker will arbitrarily choose one and discard the others.
12908 The AT_*_pc values for such functions refer to local labels in
12909 these sections. If the section from that file was discarded, the
12910 labels are not in the output, so the relocs get a value of 0.
12911 If this is a discarded function, mark the pc bounds as invalid,
12912 so that GDB will ignore it. */
12913 if (part_die->lowpc == 0 && !dwarf2_per_objfile->has_section_at_zero)
12914 {
bb5ed363 12915 struct gdbarch *gdbarch = get_objfile_arch (objfile);
9373cf26
JK
12916
12917 complaint (&symfile_complaints,
12918 _("DW_AT_low_pc %s is zero "
12919 "for DIE at 0x%x [in module %s]"),
12920 paddress (gdbarch, part_die->lowpc),
b64f50a1 12921 part_die->offset.sect_off, objfile->name);
9373cf26
JK
12922 }
12923 /* dwarf2_get_pc_bounds has also the strict low < high requirement. */
12924 else if (part_die->lowpc >= part_die->highpc)
12925 {
bb5ed363 12926 struct gdbarch *gdbarch = get_objfile_arch (objfile);
9373cf26
JK
12927
12928 complaint (&symfile_complaints,
12929 _("DW_AT_low_pc %s is not < DW_AT_high_pc %s "
12930 "for DIE at 0x%x [in module %s]"),
12931 paddress (gdbarch, part_die->lowpc),
12932 paddress (gdbarch, part_die->highpc),
b64f50a1 12933 part_die->offset.sect_off, objfile->name);
9373cf26
JK
12934 }
12935 else
12936 part_die->has_pc_info = 1;
12937 }
85cbf3d3 12938
c906108c
SS
12939 return info_ptr;
12940}
12941
72bf9492
DJ
12942/* Find a cached partial DIE at OFFSET in CU. */
12943
12944static struct partial_die_info *
b64f50a1 12945find_partial_die_in_comp_unit (sect_offset offset, struct dwarf2_cu *cu)
72bf9492
DJ
12946{
12947 struct partial_die_info *lookup_die = NULL;
12948 struct partial_die_info part_die;
12949
12950 part_die.offset = offset;
b64f50a1
JK
12951 lookup_die = htab_find_with_hash (cu->partial_dies, &part_die,
12952 offset.sect_off);
72bf9492 12953
72bf9492
DJ
12954 return lookup_die;
12955}
12956
348e048f
DE
12957/* Find a partial DIE at OFFSET, which may or may not be in CU,
12958 except in the case of .debug_types DIEs which do not reference
12959 outside their CU (they do however referencing other types via
55f1336d 12960 DW_FORM_ref_sig8). */
72bf9492
DJ
12961
12962static struct partial_die_info *
36586728 12963find_partial_die (sect_offset offset, int offset_in_dwz, struct dwarf2_cu *cu)
72bf9492 12964{
bb5ed363 12965 struct objfile *objfile = cu->objfile;
5afb4e99
DJ
12966 struct dwarf2_per_cu_data *per_cu = NULL;
12967 struct partial_die_info *pd = NULL;
72bf9492 12968
36586728
TT
12969 if (offset_in_dwz == cu->per_cu->is_dwz
12970 && offset_in_cu_p (&cu->header, offset))
5afb4e99
DJ
12971 {
12972 pd = find_partial_die_in_comp_unit (offset, cu);
12973 if (pd != NULL)
12974 return pd;
0d99eb77
DE
12975 /* We missed recording what we needed.
12976 Load all dies and try again. */
12977 per_cu = cu->per_cu;
5afb4e99 12978 }
0d99eb77
DE
12979 else
12980 {
12981 /* TUs don't reference other CUs/TUs (except via type signatures). */
3019eac3 12982 if (cu->per_cu->is_debug_types)
0d99eb77
DE
12983 {
12984 error (_("Dwarf Error: Type Unit at offset 0x%lx contains"
12985 " external reference to offset 0x%lx [in module %s].\n"),
12986 (long) cu->header.offset.sect_off, (long) offset.sect_off,
12987 bfd_get_filename (objfile->obfd));
12988 }
36586728
TT
12989 per_cu = dwarf2_find_containing_comp_unit (offset, offset_in_dwz,
12990 objfile);
72bf9492 12991
0d99eb77
DE
12992 if (per_cu->cu == NULL || per_cu->cu->partial_dies == NULL)
12993 load_partial_comp_unit (per_cu);
ae038cb0 12994
0d99eb77
DE
12995 per_cu->cu->last_used = 0;
12996 pd = find_partial_die_in_comp_unit (offset, per_cu->cu);
12997 }
5afb4e99 12998
dee91e82
DE
12999 /* If we didn't find it, and not all dies have been loaded,
13000 load them all and try again. */
13001
5afb4e99
DJ
13002 if (pd == NULL && per_cu->load_all_dies == 0)
13003 {
5afb4e99 13004 per_cu->load_all_dies = 1;
fd820528
DE
13005
13006 /* This is nasty. When we reread the DIEs, somewhere up the call chain
13007 THIS_CU->cu may already be in use. So we can't just free it and
13008 replace its DIEs with the ones we read in. Instead, we leave those
13009 DIEs alone (which can still be in use, e.g. in scan_partial_symbols),
13010 and clobber THIS_CU->cu->partial_dies with the hash table for the new
13011 set. */
dee91e82 13012 load_partial_comp_unit (per_cu);
5afb4e99
DJ
13013
13014 pd = find_partial_die_in_comp_unit (offset, per_cu->cu);
13015 }
13016
13017 if (pd == NULL)
13018 internal_error (__FILE__, __LINE__,
3e43a32a
MS
13019 _("could not find partial DIE 0x%x "
13020 "in cache [from module %s]\n"),
b64f50a1 13021 offset.sect_off, bfd_get_filename (objfile->obfd));
5afb4e99 13022 return pd;
72bf9492
DJ
13023}
13024
abc72ce4
DE
13025/* See if we can figure out if the class lives in a namespace. We do
13026 this by looking for a member function; its demangled name will
13027 contain namespace info, if there is any. */
13028
13029static void
13030guess_partial_die_structure_name (struct partial_die_info *struct_pdi,
13031 struct dwarf2_cu *cu)
13032{
13033 /* NOTE: carlton/2003-10-07: Getting the info this way changes
13034 what template types look like, because the demangler
13035 frequently doesn't give the same name as the debug info. We
13036 could fix this by only using the demangled name to get the
13037 prefix (but see comment in read_structure_type). */
13038
13039 struct partial_die_info *real_pdi;
13040 struct partial_die_info *child_pdi;
13041
13042 /* If this DIE (this DIE's specification, if any) has a parent, then
13043 we should not do this. We'll prepend the parent's fully qualified
13044 name when we create the partial symbol. */
13045
13046 real_pdi = struct_pdi;
13047 while (real_pdi->has_specification)
36586728
TT
13048 real_pdi = find_partial_die (real_pdi->spec_offset,
13049 real_pdi->spec_is_dwz, cu);
abc72ce4
DE
13050
13051 if (real_pdi->die_parent != NULL)
13052 return;
13053
13054 for (child_pdi = struct_pdi->die_child;
13055 child_pdi != NULL;
13056 child_pdi = child_pdi->die_sibling)
13057 {
13058 if (child_pdi->tag == DW_TAG_subprogram
13059 && child_pdi->linkage_name != NULL)
13060 {
13061 char *actual_class_name
13062 = language_class_name_from_physname (cu->language_defn,
13063 child_pdi->linkage_name);
13064 if (actual_class_name != NULL)
13065 {
13066 struct_pdi->name
13067 = obsavestring (actual_class_name,
13068 strlen (actual_class_name),
13069 &cu->objfile->objfile_obstack);
13070 xfree (actual_class_name);
13071 }
13072 break;
13073 }
13074 }
13075}
13076
72bf9492
DJ
13077/* Adjust PART_DIE before generating a symbol for it. This function
13078 may set the is_external flag or change the DIE's name. */
13079
13080static void
13081fixup_partial_die (struct partial_die_info *part_die,
13082 struct dwarf2_cu *cu)
13083{
abc72ce4
DE
13084 /* Once we've fixed up a die, there's no point in doing so again.
13085 This also avoids a memory leak if we were to call
13086 guess_partial_die_structure_name multiple times. */
13087 if (part_die->fixup_called)
13088 return;
13089
72bf9492
DJ
13090 /* If we found a reference attribute and the DIE has no name, try
13091 to find a name in the referred to DIE. */
13092
13093 if (part_die->name == NULL && part_die->has_specification)
13094 {
13095 struct partial_die_info *spec_die;
72bf9492 13096
36586728
TT
13097 spec_die = find_partial_die (part_die->spec_offset,
13098 part_die->spec_is_dwz, cu);
72bf9492 13099
10b3939b 13100 fixup_partial_die (spec_die, cu);
72bf9492
DJ
13101
13102 if (spec_die->name)
13103 {
13104 part_die->name = spec_die->name;
13105
13106 /* Copy DW_AT_external attribute if it is set. */
13107 if (spec_die->is_external)
13108 part_die->is_external = spec_die->is_external;
13109 }
13110 }
13111
13112 /* Set default names for some unnamed DIEs. */
72bf9492
DJ
13113
13114 if (part_die->name == NULL && part_die->tag == DW_TAG_namespace)
2b1dbab0 13115 part_die->name = CP_ANONYMOUS_NAMESPACE_STR;
72bf9492 13116
abc72ce4
DE
13117 /* If there is no parent die to provide a namespace, and there are
13118 children, see if we can determine the namespace from their linkage
122d1940 13119 name. */
abc72ce4 13120 if (cu->language == language_cplus
8b70b953 13121 && !VEC_empty (dwarf2_section_info_def, dwarf2_per_objfile->types)
abc72ce4
DE
13122 && part_die->die_parent == NULL
13123 && part_die->has_children
13124 && (part_die->tag == DW_TAG_class_type
13125 || part_die->tag == DW_TAG_structure_type
13126 || part_die->tag == DW_TAG_union_type))
13127 guess_partial_die_structure_name (part_die, cu);
13128
53832f31
TT
13129 /* GCC might emit a nameless struct or union that has a linkage
13130 name. See http://gcc.gnu.org/bugzilla/show_bug.cgi?id=47510. */
13131 if (part_die->name == NULL
96408a79
SA
13132 && (part_die->tag == DW_TAG_class_type
13133 || part_die->tag == DW_TAG_interface_type
13134 || part_die->tag == DW_TAG_structure_type
13135 || part_die->tag == DW_TAG_union_type)
53832f31
TT
13136 && part_die->linkage_name != NULL)
13137 {
13138 char *demangled;
13139
13140 demangled = cplus_demangle (part_die->linkage_name, DMGL_TYPES);
13141 if (demangled)
13142 {
96408a79
SA
13143 const char *base;
13144
13145 /* Strip any leading namespaces/classes, keep only the base name.
13146 DW_AT_name for named DIEs does not contain the prefixes. */
13147 base = strrchr (demangled, ':');
13148 if (base && base > demangled && base[-1] == ':')
13149 base++;
13150 else
13151 base = demangled;
13152
13153 part_die->name = obsavestring (base, strlen (base),
53832f31
TT
13154 &cu->objfile->objfile_obstack);
13155 xfree (demangled);
13156 }
13157 }
13158
abc72ce4 13159 part_die->fixup_called = 1;
72bf9492
DJ
13160}
13161
a8329558 13162/* Read an attribute value described by an attribute form. */
c906108c 13163
fe1b8b76 13164static gdb_byte *
dee91e82
DE
13165read_attribute_value (const struct die_reader_specs *reader,
13166 struct attribute *attr, unsigned form,
13167 gdb_byte *info_ptr)
c906108c 13168{
dee91e82
DE
13169 struct dwarf2_cu *cu = reader->cu;
13170 bfd *abfd = reader->abfd;
e7c27a73 13171 struct comp_unit_head *cu_header = &cu->header;
c906108c
SS
13172 unsigned int bytes_read;
13173 struct dwarf_block *blk;
13174
a8329558
KW
13175 attr->form = form;
13176 switch (form)
c906108c 13177 {
c906108c 13178 case DW_FORM_ref_addr:
ae411497 13179 if (cu->header.version == 2)
4568ecf9 13180 DW_UNSND (attr) = read_address (abfd, info_ptr, cu, &bytes_read);
ae411497 13181 else
4568ecf9
DE
13182 DW_UNSND (attr) = read_offset (abfd, info_ptr,
13183 &cu->header, &bytes_read);
ae411497
TT
13184 info_ptr += bytes_read;
13185 break;
36586728
TT
13186 case DW_FORM_GNU_ref_alt:
13187 DW_UNSND (attr) = read_offset (abfd, info_ptr, &cu->header, &bytes_read);
13188 info_ptr += bytes_read;
13189 break;
ae411497 13190 case DW_FORM_addr:
e7c27a73 13191 DW_ADDR (attr) = read_address (abfd, info_ptr, cu, &bytes_read);
107d2387 13192 info_ptr += bytes_read;
c906108c
SS
13193 break;
13194 case DW_FORM_block2:
7b5a2f43 13195 blk = dwarf_alloc_block (cu);
c906108c
SS
13196 blk->size = read_2_bytes (abfd, info_ptr);
13197 info_ptr += 2;
13198 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
13199 info_ptr += blk->size;
13200 DW_BLOCK (attr) = blk;
13201 break;
13202 case DW_FORM_block4:
7b5a2f43 13203 blk = dwarf_alloc_block (cu);
c906108c
SS
13204 blk->size = read_4_bytes (abfd, info_ptr);
13205 info_ptr += 4;
13206 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
13207 info_ptr += blk->size;
13208 DW_BLOCK (attr) = blk;
13209 break;
13210 case DW_FORM_data2:
13211 DW_UNSND (attr) = read_2_bytes (abfd, info_ptr);
13212 info_ptr += 2;
13213 break;
13214 case DW_FORM_data4:
13215 DW_UNSND (attr) = read_4_bytes (abfd, info_ptr);
13216 info_ptr += 4;
13217 break;
13218 case DW_FORM_data8:
13219 DW_UNSND (attr) = read_8_bytes (abfd, info_ptr);
13220 info_ptr += 8;
13221 break;
2dc7f7b3
TT
13222 case DW_FORM_sec_offset:
13223 DW_UNSND (attr) = read_offset (abfd, info_ptr, &cu->header, &bytes_read);
13224 info_ptr += bytes_read;
13225 break;
c906108c 13226 case DW_FORM_string:
9b1c24c8 13227 DW_STRING (attr) = read_direct_string (abfd, info_ptr, &bytes_read);
8285870a 13228 DW_STRING_IS_CANONICAL (attr) = 0;
c906108c
SS
13229 info_ptr += bytes_read;
13230 break;
4bdf3d34 13231 case DW_FORM_strp:
36586728
TT
13232 if (!cu->per_cu->is_dwz)
13233 {
13234 DW_STRING (attr) = read_indirect_string (abfd, info_ptr, cu_header,
13235 &bytes_read);
13236 DW_STRING_IS_CANONICAL (attr) = 0;
13237 info_ptr += bytes_read;
13238 break;
13239 }
13240 /* FALLTHROUGH */
13241 case DW_FORM_GNU_strp_alt:
13242 {
13243 struct dwz_file *dwz = dwarf2_get_dwz_file ();
13244 LONGEST str_offset = read_offset (abfd, info_ptr, cu_header,
13245 &bytes_read);
13246
13247 DW_STRING (attr) = read_indirect_string_from_dwz (dwz, str_offset);
13248 DW_STRING_IS_CANONICAL (attr) = 0;
13249 info_ptr += bytes_read;
13250 }
4bdf3d34 13251 break;
2dc7f7b3 13252 case DW_FORM_exprloc:
c906108c 13253 case DW_FORM_block:
7b5a2f43 13254 blk = dwarf_alloc_block (cu);
c906108c
SS
13255 blk->size = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
13256 info_ptr += bytes_read;
13257 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
13258 info_ptr += blk->size;
13259 DW_BLOCK (attr) = blk;
13260 break;
13261 case DW_FORM_block1:
7b5a2f43 13262 blk = dwarf_alloc_block (cu);
c906108c
SS
13263 blk->size = read_1_byte (abfd, info_ptr);
13264 info_ptr += 1;
13265 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
13266 info_ptr += blk->size;
13267 DW_BLOCK (attr) = blk;
13268 break;
13269 case DW_FORM_data1:
13270 DW_UNSND (attr) = read_1_byte (abfd, info_ptr);
13271 info_ptr += 1;
13272 break;
13273 case DW_FORM_flag:
13274 DW_UNSND (attr) = read_1_byte (abfd, info_ptr);
13275 info_ptr += 1;
13276 break;
2dc7f7b3
TT
13277 case DW_FORM_flag_present:
13278 DW_UNSND (attr) = 1;
13279 break;
c906108c
SS
13280 case DW_FORM_sdata:
13281 DW_SND (attr) = read_signed_leb128 (abfd, info_ptr, &bytes_read);
13282 info_ptr += bytes_read;
13283 break;
13284 case DW_FORM_udata:
13285 DW_UNSND (attr) = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
13286 info_ptr += bytes_read;
13287 break;
13288 case DW_FORM_ref1:
4568ecf9
DE
13289 DW_UNSND (attr) = (cu->header.offset.sect_off
13290 + read_1_byte (abfd, info_ptr));
c906108c
SS
13291 info_ptr += 1;
13292 break;
13293 case DW_FORM_ref2:
4568ecf9
DE
13294 DW_UNSND (attr) = (cu->header.offset.sect_off
13295 + read_2_bytes (abfd, info_ptr));
c906108c
SS
13296 info_ptr += 2;
13297 break;
13298 case DW_FORM_ref4:
4568ecf9
DE
13299 DW_UNSND (attr) = (cu->header.offset.sect_off
13300 + read_4_bytes (abfd, info_ptr));
c906108c
SS
13301 info_ptr += 4;
13302 break;
613e1657 13303 case DW_FORM_ref8:
4568ecf9
DE
13304 DW_UNSND (attr) = (cu->header.offset.sect_off
13305 + read_8_bytes (abfd, info_ptr));
613e1657
KB
13306 info_ptr += 8;
13307 break;
55f1336d 13308 case DW_FORM_ref_sig8:
348e048f
DE
13309 /* Convert the signature to something we can record in DW_UNSND
13310 for later lookup.
13311 NOTE: This is NULL if the type wasn't found. */
13312 DW_SIGNATURED_TYPE (attr) =
e319fa28 13313 lookup_signatured_type (read_8_bytes (abfd, info_ptr));
348e048f
DE
13314 info_ptr += 8;
13315 break;
c906108c 13316 case DW_FORM_ref_udata:
4568ecf9
DE
13317 DW_UNSND (attr) = (cu->header.offset.sect_off
13318 + read_unsigned_leb128 (abfd, info_ptr, &bytes_read));
c906108c
SS
13319 info_ptr += bytes_read;
13320 break;
c906108c 13321 case DW_FORM_indirect:
a8329558
KW
13322 form = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
13323 info_ptr += bytes_read;
dee91e82 13324 info_ptr = read_attribute_value (reader, attr, form, info_ptr);
a8329558 13325 break;
3019eac3
DE
13326 case DW_FORM_GNU_addr_index:
13327 if (reader->dwo_file == NULL)
13328 {
13329 /* For now flag a hard error.
13330 Later we can turn this into a complaint. */
13331 error (_("Dwarf Error: %s found in non-DWO CU [in module %s]"),
13332 dwarf_form_name (form),
13333 bfd_get_filename (abfd));
13334 }
13335 DW_ADDR (attr) = read_addr_index_from_leb128 (cu, info_ptr, &bytes_read);
13336 info_ptr += bytes_read;
13337 break;
13338 case DW_FORM_GNU_str_index:
13339 if (reader->dwo_file == NULL)
13340 {
13341 /* For now flag a hard error.
13342 Later we can turn this into a complaint if warranted. */
13343 error (_("Dwarf Error: %s found in non-DWO CU [in module %s]"),
13344 dwarf_form_name (form),
13345 bfd_get_filename (abfd));
13346 }
13347 {
13348 ULONGEST str_index =
13349 read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
13350
13351 DW_STRING (attr) = read_str_index (reader, cu, str_index);
13352 DW_STRING_IS_CANONICAL (attr) = 0;
13353 info_ptr += bytes_read;
13354 }
13355 break;
c906108c 13356 default:
8a3fe4f8 13357 error (_("Dwarf Error: Cannot handle %s in DWARF reader [in module %s]"),
659b0389
ML
13358 dwarf_form_name (form),
13359 bfd_get_filename (abfd));
c906108c 13360 }
28e94949 13361
36586728
TT
13362 /* Super hack. */
13363 if (cu->per_cu->is_dwz && is_ref_attr (attr))
13364 attr->form = DW_FORM_GNU_ref_alt;
13365
28e94949
JB
13366 /* We have seen instances where the compiler tried to emit a byte
13367 size attribute of -1 which ended up being encoded as an unsigned
13368 0xffffffff. Although 0xffffffff is technically a valid size value,
13369 an object of this size seems pretty unlikely so we can relatively
13370 safely treat these cases as if the size attribute was invalid and
13371 treat them as zero by default. */
13372 if (attr->name == DW_AT_byte_size
13373 && form == DW_FORM_data4
13374 && DW_UNSND (attr) >= 0xffffffff)
01c66ae6
JB
13375 {
13376 complaint
13377 (&symfile_complaints,
43bbcdc2
PH
13378 _("Suspicious DW_AT_byte_size value treated as zero instead of %s"),
13379 hex_string (DW_UNSND (attr)));
01c66ae6
JB
13380 DW_UNSND (attr) = 0;
13381 }
28e94949 13382
c906108c
SS
13383 return info_ptr;
13384}
13385
a8329558
KW
13386/* Read an attribute described by an abbreviated attribute. */
13387
fe1b8b76 13388static gdb_byte *
dee91e82
DE
13389read_attribute (const struct die_reader_specs *reader,
13390 struct attribute *attr, struct attr_abbrev *abbrev,
13391 gdb_byte *info_ptr)
a8329558
KW
13392{
13393 attr->name = abbrev->name;
dee91e82 13394 return read_attribute_value (reader, attr, abbrev->form, info_ptr);
a8329558
KW
13395}
13396
0963b4bd 13397/* Read dwarf information from a buffer. */
c906108c
SS
13398
13399static unsigned int
a1855c1d 13400read_1_byte (bfd *abfd, const gdb_byte *buf)
c906108c 13401{
fe1b8b76 13402 return bfd_get_8 (abfd, buf);
c906108c
SS
13403}
13404
13405static int
a1855c1d 13406read_1_signed_byte (bfd *abfd, const gdb_byte *buf)
c906108c 13407{
fe1b8b76 13408 return bfd_get_signed_8 (abfd, buf);
c906108c
SS
13409}
13410
13411static unsigned int
a1855c1d 13412read_2_bytes (bfd *abfd, const gdb_byte *buf)
c906108c 13413{
fe1b8b76 13414 return bfd_get_16 (abfd, buf);
c906108c
SS
13415}
13416
21ae7a4d 13417static int
a1855c1d 13418read_2_signed_bytes (bfd *abfd, const gdb_byte *buf)
21ae7a4d
JK
13419{
13420 return bfd_get_signed_16 (abfd, buf);
13421}
13422
c906108c 13423static unsigned int
a1855c1d 13424read_4_bytes (bfd *abfd, const gdb_byte *buf)
c906108c 13425{
fe1b8b76 13426 return bfd_get_32 (abfd, buf);
c906108c
SS
13427}
13428
21ae7a4d 13429static int
a1855c1d 13430read_4_signed_bytes (bfd *abfd, const gdb_byte *buf)
21ae7a4d
JK
13431{
13432 return bfd_get_signed_32 (abfd, buf);
13433}
13434
93311388 13435static ULONGEST
a1855c1d 13436read_8_bytes (bfd *abfd, const gdb_byte *buf)
c906108c 13437{
fe1b8b76 13438 return bfd_get_64 (abfd, buf);
c906108c
SS
13439}
13440
13441static CORE_ADDR
fe1b8b76 13442read_address (bfd *abfd, gdb_byte *buf, struct dwarf2_cu *cu,
891d2f0b 13443 unsigned int *bytes_read)
c906108c 13444{
e7c27a73 13445 struct comp_unit_head *cu_header = &cu->header;
c906108c
SS
13446 CORE_ADDR retval = 0;
13447
107d2387 13448 if (cu_header->signed_addr_p)
c906108c 13449 {
107d2387
AC
13450 switch (cu_header->addr_size)
13451 {
13452 case 2:
fe1b8b76 13453 retval = bfd_get_signed_16 (abfd, buf);
107d2387
AC
13454 break;
13455 case 4:
fe1b8b76 13456 retval = bfd_get_signed_32 (abfd, buf);
107d2387
AC
13457 break;
13458 case 8:
fe1b8b76 13459 retval = bfd_get_signed_64 (abfd, buf);
107d2387
AC
13460 break;
13461 default:
8e65ff28 13462 internal_error (__FILE__, __LINE__,
e2e0b3e5 13463 _("read_address: bad switch, signed [in module %s]"),
659b0389 13464 bfd_get_filename (abfd));
107d2387
AC
13465 }
13466 }
13467 else
13468 {
13469 switch (cu_header->addr_size)
13470 {
13471 case 2:
fe1b8b76 13472 retval = bfd_get_16 (abfd, buf);
107d2387
AC
13473 break;
13474 case 4:
fe1b8b76 13475 retval = bfd_get_32 (abfd, buf);
107d2387
AC
13476 break;
13477 case 8:
fe1b8b76 13478 retval = bfd_get_64 (abfd, buf);
107d2387
AC
13479 break;
13480 default:
8e65ff28 13481 internal_error (__FILE__, __LINE__,
a73c6dcd
MS
13482 _("read_address: bad switch, "
13483 "unsigned [in module %s]"),
659b0389 13484 bfd_get_filename (abfd));
107d2387 13485 }
c906108c 13486 }
64367e0a 13487
107d2387
AC
13488 *bytes_read = cu_header->addr_size;
13489 return retval;
c906108c
SS
13490}
13491
f7ef9339 13492/* Read the initial length from a section. The (draft) DWARF 3
613e1657
KB
13493 specification allows the initial length to take up either 4 bytes
13494 or 12 bytes. If the first 4 bytes are 0xffffffff, then the next 8
13495 bytes describe the length and all offsets will be 8 bytes in length
13496 instead of 4.
13497
f7ef9339
KB
13498 An older, non-standard 64-bit format is also handled by this
13499 function. The older format in question stores the initial length
13500 as an 8-byte quantity without an escape value. Lengths greater
13501 than 2^32 aren't very common which means that the initial 4 bytes
13502 is almost always zero. Since a length value of zero doesn't make
13503 sense for the 32-bit format, this initial zero can be considered to
13504 be an escape value which indicates the presence of the older 64-bit
13505 format. As written, the code can't detect (old format) lengths
917c78fc
MK
13506 greater than 4GB. If it becomes necessary to handle lengths
13507 somewhat larger than 4GB, we could allow other small values (such
13508 as the non-sensical values of 1, 2, and 3) to also be used as
13509 escape values indicating the presence of the old format.
f7ef9339 13510
917c78fc
MK
13511 The value returned via bytes_read should be used to increment the
13512 relevant pointer after calling read_initial_length().
c764a876 13513
613e1657
KB
13514 [ Note: read_initial_length() and read_offset() are based on the
13515 document entitled "DWARF Debugging Information Format", revision
f7ef9339 13516 3, draft 8, dated November 19, 2001. This document was obtained
613e1657
KB
13517 from:
13518
f7ef9339 13519 http://reality.sgiweb.org/davea/dwarf3-draft8-011125.pdf
6e70227d 13520
613e1657
KB
13521 This document is only a draft and is subject to change. (So beware.)
13522
f7ef9339 13523 Details regarding the older, non-standard 64-bit format were
917c78fc
MK
13524 determined empirically by examining 64-bit ELF files produced by
13525 the SGI toolchain on an IRIX 6.5 machine.
f7ef9339
KB
13526
13527 - Kevin, July 16, 2002
613e1657
KB
13528 ] */
13529
13530static LONGEST
c764a876 13531read_initial_length (bfd *abfd, gdb_byte *buf, unsigned int *bytes_read)
613e1657 13532{
fe1b8b76 13533 LONGEST length = bfd_get_32 (abfd, buf);
613e1657 13534
dd373385 13535 if (length == 0xffffffff)
613e1657 13536 {
fe1b8b76 13537 length = bfd_get_64 (abfd, buf + 4);
613e1657 13538 *bytes_read = 12;
613e1657 13539 }
dd373385 13540 else if (length == 0)
f7ef9339 13541 {
dd373385 13542 /* Handle the (non-standard) 64-bit DWARF2 format used by IRIX. */
fe1b8b76 13543 length = bfd_get_64 (abfd, buf);
f7ef9339 13544 *bytes_read = 8;
f7ef9339 13545 }
613e1657
KB
13546 else
13547 {
13548 *bytes_read = 4;
613e1657
KB
13549 }
13550
c764a876
DE
13551 return length;
13552}
dd373385 13553
c764a876
DE
13554/* Cover function for read_initial_length.
13555 Returns the length of the object at BUF, and stores the size of the
13556 initial length in *BYTES_READ and stores the size that offsets will be in
13557 *OFFSET_SIZE.
13558 If the initial length size is not equivalent to that specified in
13559 CU_HEADER then issue a complaint.
13560 This is useful when reading non-comp-unit headers. */
dd373385 13561
c764a876
DE
13562static LONGEST
13563read_checked_initial_length_and_offset (bfd *abfd, gdb_byte *buf,
13564 const struct comp_unit_head *cu_header,
13565 unsigned int *bytes_read,
13566 unsigned int *offset_size)
13567{
13568 LONGEST length = read_initial_length (abfd, buf, bytes_read);
13569
13570 gdb_assert (cu_header->initial_length_size == 4
13571 || cu_header->initial_length_size == 8
13572 || cu_header->initial_length_size == 12);
13573
13574 if (cu_header->initial_length_size != *bytes_read)
13575 complaint (&symfile_complaints,
13576 _("intermixed 32-bit and 64-bit DWARF sections"));
dd373385 13577
c764a876 13578 *offset_size = (*bytes_read == 4) ? 4 : 8;
dd373385 13579 return length;
613e1657
KB
13580}
13581
13582/* Read an offset from the data stream. The size of the offset is
917c78fc 13583 given by cu_header->offset_size. */
613e1657
KB
13584
13585static LONGEST
fe1b8b76 13586read_offset (bfd *abfd, gdb_byte *buf, const struct comp_unit_head *cu_header,
891d2f0b 13587 unsigned int *bytes_read)
c764a876
DE
13588{
13589 LONGEST offset = read_offset_1 (abfd, buf, cu_header->offset_size);
9a619af0 13590
c764a876
DE
13591 *bytes_read = cu_header->offset_size;
13592 return offset;
13593}
13594
13595/* Read an offset from the data stream. */
13596
13597static LONGEST
13598read_offset_1 (bfd *abfd, gdb_byte *buf, unsigned int offset_size)
613e1657
KB
13599{
13600 LONGEST retval = 0;
13601
c764a876 13602 switch (offset_size)
613e1657
KB
13603 {
13604 case 4:
fe1b8b76 13605 retval = bfd_get_32 (abfd, buf);
613e1657
KB
13606 break;
13607 case 8:
fe1b8b76 13608 retval = bfd_get_64 (abfd, buf);
613e1657
KB
13609 break;
13610 default:
8e65ff28 13611 internal_error (__FILE__, __LINE__,
c764a876 13612 _("read_offset_1: bad switch [in module %s]"),
659b0389 13613 bfd_get_filename (abfd));
613e1657
KB
13614 }
13615
917c78fc 13616 return retval;
613e1657
KB
13617}
13618
fe1b8b76
JB
13619static gdb_byte *
13620read_n_bytes (bfd *abfd, gdb_byte *buf, unsigned int size)
c906108c
SS
13621{
13622 /* If the size of a host char is 8 bits, we can return a pointer
13623 to the buffer, otherwise we have to copy the data to a buffer
13624 allocated on the temporary obstack. */
4bdf3d34 13625 gdb_assert (HOST_CHAR_BIT == 8);
c906108c 13626 return buf;
c906108c
SS
13627}
13628
13629static char *
9b1c24c8 13630read_direct_string (bfd *abfd, gdb_byte *buf, unsigned int *bytes_read_ptr)
c906108c
SS
13631{
13632 /* If the size of a host char is 8 bits, we can return a pointer
13633 to the string, otherwise we have to copy the string to a buffer
13634 allocated on the temporary obstack. */
4bdf3d34 13635 gdb_assert (HOST_CHAR_BIT == 8);
c906108c
SS
13636 if (*buf == '\0')
13637 {
13638 *bytes_read_ptr = 1;
13639 return NULL;
13640 }
fe1b8b76
JB
13641 *bytes_read_ptr = strlen ((char *) buf) + 1;
13642 return (char *) buf;
4bdf3d34
JJ
13643}
13644
13645static char *
cf2c3c16 13646read_indirect_string_at_offset (bfd *abfd, LONGEST str_offset)
4bdf3d34 13647{
be391dca 13648 dwarf2_read_section (dwarf2_per_objfile->objfile, &dwarf2_per_objfile->str);
dce234bc 13649 if (dwarf2_per_objfile->str.buffer == NULL)
cf2c3c16
TT
13650 error (_("DW_FORM_strp used without .debug_str section [in module %s]"),
13651 bfd_get_filename (abfd));
dce234bc 13652 if (str_offset >= dwarf2_per_objfile->str.size)
cf2c3c16
TT
13653 error (_("DW_FORM_strp pointing outside of "
13654 ".debug_str section [in module %s]"),
13655 bfd_get_filename (abfd));
4bdf3d34 13656 gdb_assert (HOST_CHAR_BIT == 8);
dce234bc 13657 if (dwarf2_per_objfile->str.buffer[str_offset] == '\0')
4bdf3d34 13658 return NULL;
dce234bc 13659 return (char *) (dwarf2_per_objfile->str.buffer + str_offset);
c906108c
SS
13660}
13661
36586728
TT
13662/* Read a string at offset STR_OFFSET in the .debug_str section from
13663 the .dwz file DWZ. Throw an error if the offset is too large. If
13664 the string consists of a single NUL byte, return NULL; otherwise
13665 return a pointer to the string. */
13666
13667static char *
13668read_indirect_string_from_dwz (struct dwz_file *dwz, LONGEST str_offset)
13669{
13670 dwarf2_read_section (dwarf2_per_objfile->objfile, &dwz->str);
13671
13672 if (dwz->str.buffer == NULL)
13673 error (_("DW_FORM_GNU_strp_alt used without .debug_str "
13674 "section [in module %s]"),
13675 bfd_get_filename (dwz->dwz_bfd));
13676 if (str_offset >= dwz->str.size)
13677 error (_("DW_FORM_GNU_strp_alt pointing outside of "
13678 ".debug_str section [in module %s]"),
13679 bfd_get_filename (dwz->dwz_bfd));
13680 gdb_assert (HOST_CHAR_BIT == 8);
13681 if (dwz->str.buffer[str_offset] == '\0')
13682 return NULL;
13683 return (char *) (dwz->str.buffer + str_offset);
13684}
13685
cf2c3c16
TT
13686static char *
13687read_indirect_string (bfd *abfd, gdb_byte *buf,
13688 const struct comp_unit_head *cu_header,
13689 unsigned int *bytes_read_ptr)
13690{
13691 LONGEST str_offset = read_offset (abfd, buf, cu_header, bytes_read_ptr);
13692
13693 return read_indirect_string_at_offset (abfd, str_offset);
13694}
13695
12df843f 13696static ULONGEST
fe1b8b76 13697read_unsigned_leb128 (bfd *abfd, gdb_byte *buf, unsigned int *bytes_read_ptr)
c906108c 13698{
12df843f 13699 ULONGEST result;
ce5d95e1 13700 unsigned int num_read;
c906108c
SS
13701 int i, shift;
13702 unsigned char byte;
13703
13704 result = 0;
13705 shift = 0;
13706 num_read = 0;
13707 i = 0;
13708 while (1)
13709 {
fe1b8b76 13710 byte = bfd_get_8 (abfd, buf);
c906108c
SS
13711 buf++;
13712 num_read++;
12df843f 13713 result |= ((ULONGEST) (byte & 127) << shift);
c906108c
SS
13714 if ((byte & 128) == 0)
13715 {
13716 break;
13717 }
13718 shift += 7;
13719 }
13720 *bytes_read_ptr = num_read;
13721 return result;
13722}
13723
12df843f 13724static LONGEST
fe1b8b76 13725read_signed_leb128 (bfd *abfd, gdb_byte *buf, unsigned int *bytes_read_ptr)
c906108c 13726{
12df843f 13727 LONGEST result;
77e0b926 13728 int i, shift, num_read;
c906108c
SS
13729 unsigned char byte;
13730
13731 result = 0;
13732 shift = 0;
c906108c
SS
13733 num_read = 0;
13734 i = 0;
13735 while (1)
13736 {
fe1b8b76 13737 byte = bfd_get_8 (abfd, buf);
c906108c
SS
13738 buf++;
13739 num_read++;
12df843f 13740 result |= ((LONGEST) (byte & 127) << shift);
c906108c
SS
13741 shift += 7;
13742 if ((byte & 128) == 0)
13743 {
13744 break;
13745 }
13746 }
77e0b926 13747 if ((shift < 8 * sizeof (result)) && (byte & 0x40))
12df843f 13748 result |= -(((LONGEST) 1) << shift);
c906108c
SS
13749 *bytes_read_ptr = num_read;
13750 return result;
13751}
13752
3019eac3
DE
13753/* Given index ADDR_INDEX in .debug_addr, fetch the value.
13754 ADDR_BASE is the DW_AT_GNU_addr_base attribute or zero.
13755 ADDR_SIZE is the size of addresses from the CU header. */
13756
13757static CORE_ADDR
13758read_addr_index_1 (unsigned int addr_index, ULONGEST addr_base, int addr_size)
13759{
13760 struct objfile *objfile = dwarf2_per_objfile->objfile;
13761 bfd *abfd = objfile->obfd;
13762 const gdb_byte *info_ptr;
13763
13764 dwarf2_read_section (objfile, &dwarf2_per_objfile->addr);
13765 if (dwarf2_per_objfile->addr.buffer == NULL)
13766 error (_("DW_FORM_addr_index used without .debug_addr section [in module %s]"),
13767 objfile->name);
13768 if (addr_base + addr_index * addr_size >= dwarf2_per_objfile->addr.size)
13769 error (_("DW_FORM_addr_index pointing outside of "
13770 ".debug_addr section [in module %s]"),
13771 objfile->name);
13772 info_ptr = (dwarf2_per_objfile->addr.buffer
13773 + addr_base + addr_index * addr_size);
13774 if (addr_size == 4)
13775 return bfd_get_32 (abfd, info_ptr);
13776 else
13777 return bfd_get_64 (abfd, info_ptr);
13778}
13779
13780/* Given index ADDR_INDEX in .debug_addr, fetch the value. */
13781
13782static CORE_ADDR
13783read_addr_index (struct dwarf2_cu *cu, unsigned int addr_index)
13784{
13785 return read_addr_index_1 (addr_index, cu->addr_base, cu->header.addr_size);
13786}
13787
13788/* Given a pointer to an leb128 value, fetch the value from .debug_addr. */
13789
13790static CORE_ADDR
13791read_addr_index_from_leb128 (struct dwarf2_cu *cu, gdb_byte *info_ptr,
13792 unsigned int *bytes_read)
13793{
13794 bfd *abfd = cu->objfile->obfd;
13795 unsigned int addr_index = read_unsigned_leb128 (abfd, info_ptr, bytes_read);
13796
13797 return read_addr_index (cu, addr_index);
13798}
13799
13800/* Data structure to pass results from dwarf2_read_addr_index_reader
13801 back to dwarf2_read_addr_index. */
13802
13803struct dwarf2_read_addr_index_data
13804{
13805 ULONGEST addr_base;
13806 int addr_size;
13807};
13808
13809/* die_reader_func for dwarf2_read_addr_index. */
13810
13811static void
13812dwarf2_read_addr_index_reader (const struct die_reader_specs *reader,
13813 gdb_byte *info_ptr,
13814 struct die_info *comp_unit_die,
13815 int has_children,
13816 void *data)
13817{
13818 struct dwarf2_cu *cu = reader->cu;
13819 struct dwarf2_read_addr_index_data *aidata =
13820 (struct dwarf2_read_addr_index_data *) data;
13821
13822 aidata->addr_base = cu->addr_base;
13823 aidata->addr_size = cu->header.addr_size;
13824}
13825
13826/* Given an index in .debug_addr, fetch the value.
13827 NOTE: This can be called during dwarf expression evaluation,
13828 long after the debug information has been read, and thus per_cu->cu
13829 may no longer exist. */
13830
13831CORE_ADDR
13832dwarf2_read_addr_index (struct dwarf2_per_cu_data *per_cu,
13833 unsigned int addr_index)
13834{
13835 struct objfile *objfile = per_cu->objfile;
13836 struct dwarf2_cu *cu = per_cu->cu;
13837 ULONGEST addr_base;
13838 int addr_size;
13839
13840 /* This is intended to be called from outside this file. */
13841 dw2_setup (objfile);
13842
13843 /* We need addr_base and addr_size.
13844 If we don't have PER_CU->cu, we have to get it.
13845 Nasty, but the alternative is storing the needed info in PER_CU,
13846 which at this point doesn't seem justified: it's not clear how frequently
13847 it would get used and it would increase the size of every PER_CU.
13848 Entry points like dwarf2_per_cu_addr_size do a similar thing
13849 so we're not in uncharted territory here.
13850 Alas we need to be a bit more complicated as addr_base is contained
13851 in the DIE.
13852
13853 We don't need to read the entire CU(/TU).
13854 We just need the header and top level die.
a1b64ce1 13855
3019eac3 13856 IWBN to use the aging mechanism to let us lazily later discard the CU.
a1b64ce1 13857 For now we skip this optimization. */
3019eac3
DE
13858
13859 if (cu != NULL)
13860 {
13861 addr_base = cu->addr_base;
13862 addr_size = cu->header.addr_size;
13863 }
13864 else
13865 {
13866 struct dwarf2_read_addr_index_data aidata;
13867
a1b64ce1
DE
13868 /* Note: We can't use init_cutu_and_read_dies_simple here,
13869 we need addr_base. */
13870 init_cutu_and_read_dies (per_cu, NULL, 0, 0,
13871 dwarf2_read_addr_index_reader, &aidata);
3019eac3
DE
13872 addr_base = aidata.addr_base;
13873 addr_size = aidata.addr_size;
13874 }
13875
13876 return read_addr_index_1 (addr_index, addr_base, addr_size);
13877}
13878
13879/* Given a DW_AT_str_index, fetch the string. */
13880
13881static char *
13882read_str_index (const struct die_reader_specs *reader,
13883 struct dwarf2_cu *cu, ULONGEST str_index)
13884{
13885 struct objfile *objfile = dwarf2_per_objfile->objfile;
13886 const char *dwo_name = objfile->name;
13887 bfd *abfd = objfile->obfd;
13888 struct dwo_sections *sections = &reader->dwo_file->sections;
13889 gdb_byte *info_ptr;
13890 ULONGEST str_offset;
13891
13892 dwarf2_read_section (objfile, &sections->str);
13893 dwarf2_read_section (objfile, &sections->str_offsets);
13894 if (sections->str.buffer == NULL)
13895 error (_("DW_FORM_str_index used without .debug_str.dwo section"
13896 " in CU at offset 0x%lx [in module %s]"),
13897 (long) cu->header.offset.sect_off, dwo_name);
13898 if (sections->str_offsets.buffer == NULL)
13899 error (_("DW_FORM_str_index used without .debug_str_offsets.dwo section"
13900 " in CU at offset 0x%lx [in module %s]"),
13901 (long) cu->header.offset.sect_off, dwo_name);
13902 if (str_index * cu->header.offset_size >= sections->str_offsets.size)
13903 error (_("DW_FORM_str_index pointing outside of .debug_str_offsets.dwo"
13904 " section in CU at offset 0x%lx [in module %s]"),
13905 (long) cu->header.offset.sect_off, dwo_name);
13906 info_ptr = (sections->str_offsets.buffer
13907 + str_index * cu->header.offset_size);
13908 if (cu->header.offset_size == 4)
13909 str_offset = bfd_get_32 (abfd, info_ptr);
13910 else
13911 str_offset = bfd_get_64 (abfd, info_ptr);
13912 if (str_offset >= sections->str.size)
13913 error (_("Offset from DW_FORM_str_index pointing outside of"
13914 " .debug_str.dwo section in CU at offset 0x%lx [in module %s]"),
13915 (long) cu->header.offset.sect_off, dwo_name);
13916 return (char *) (sections->str.buffer + str_offset);
13917}
13918
3019eac3
DE
13919/* Return the length of an LEB128 number in BUF. */
13920
13921static int
13922leb128_size (const gdb_byte *buf)
13923{
13924 const gdb_byte *begin = buf;
13925 gdb_byte byte;
13926
13927 while (1)
13928 {
13929 byte = *buf++;
13930 if ((byte & 128) == 0)
13931 return buf - begin;
13932 }
13933}
13934
c906108c 13935static void
e142c38c 13936set_cu_language (unsigned int lang, struct dwarf2_cu *cu)
c906108c
SS
13937{
13938 switch (lang)
13939 {
13940 case DW_LANG_C89:
76bee0cc 13941 case DW_LANG_C99:
c906108c 13942 case DW_LANG_C:
e142c38c 13943 cu->language = language_c;
c906108c
SS
13944 break;
13945 case DW_LANG_C_plus_plus:
e142c38c 13946 cu->language = language_cplus;
c906108c 13947 break;
6aecb9c2
JB
13948 case DW_LANG_D:
13949 cu->language = language_d;
13950 break;
c906108c
SS
13951 case DW_LANG_Fortran77:
13952 case DW_LANG_Fortran90:
b21b22e0 13953 case DW_LANG_Fortran95:
e142c38c 13954 cu->language = language_fortran;
c906108c 13955 break;
a766d390
DE
13956 case DW_LANG_Go:
13957 cu->language = language_go;
13958 break;
c906108c 13959 case DW_LANG_Mips_Assembler:
e142c38c 13960 cu->language = language_asm;
c906108c 13961 break;
bebd888e 13962 case DW_LANG_Java:
e142c38c 13963 cu->language = language_java;
bebd888e 13964 break;
c906108c 13965 case DW_LANG_Ada83:
8aaf0b47 13966 case DW_LANG_Ada95:
bc5f45f8
JB
13967 cu->language = language_ada;
13968 break;
72019c9c
GM
13969 case DW_LANG_Modula2:
13970 cu->language = language_m2;
13971 break;
fe8e67fd
PM
13972 case DW_LANG_Pascal83:
13973 cu->language = language_pascal;
13974 break;
22566fbd
DJ
13975 case DW_LANG_ObjC:
13976 cu->language = language_objc;
13977 break;
c906108c
SS
13978 case DW_LANG_Cobol74:
13979 case DW_LANG_Cobol85:
c906108c 13980 default:
e142c38c 13981 cu->language = language_minimal;
c906108c
SS
13982 break;
13983 }
e142c38c 13984 cu->language_defn = language_def (cu->language);
c906108c
SS
13985}
13986
13987/* Return the named attribute or NULL if not there. */
13988
13989static struct attribute *
e142c38c 13990dwarf2_attr (struct die_info *die, unsigned int name, struct dwarf2_cu *cu)
c906108c 13991{
a48e046c 13992 for (;;)
c906108c 13993 {
a48e046c
TT
13994 unsigned int i;
13995 struct attribute *spec = NULL;
13996
13997 for (i = 0; i < die->num_attrs; ++i)
13998 {
13999 if (die->attrs[i].name == name)
14000 return &die->attrs[i];
14001 if (die->attrs[i].name == DW_AT_specification
14002 || die->attrs[i].name == DW_AT_abstract_origin)
14003 spec = &die->attrs[i];
14004 }
14005
14006 if (!spec)
14007 break;
c906108c 14008
f2f0e013 14009 die = follow_die_ref (die, spec, &cu);
f2f0e013 14010 }
c5aa993b 14011
c906108c
SS
14012 return NULL;
14013}
14014
348e048f
DE
14015/* Return the named attribute or NULL if not there,
14016 but do not follow DW_AT_specification, etc.
14017 This is for use in contexts where we're reading .debug_types dies.
14018 Following DW_AT_specification, DW_AT_abstract_origin will take us
14019 back up the chain, and we want to go down. */
14020
14021static struct attribute *
45e58e77 14022dwarf2_attr_no_follow (struct die_info *die, unsigned int name)
348e048f
DE
14023{
14024 unsigned int i;
14025
14026 for (i = 0; i < die->num_attrs; ++i)
14027 if (die->attrs[i].name == name)
14028 return &die->attrs[i];
14029
14030 return NULL;
14031}
14032
05cf31d1
JB
14033/* Return non-zero iff the attribute NAME is defined for the given DIE,
14034 and holds a non-zero value. This function should only be used for
2dc7f7b3 14035 DW_FORM_flag or DW_FORM_flag_present attributes. */
05cf31d1
JB
14036
14037static int
14038dwarf2_flag_true_p (struct die_info *die, unsigned name, struct dwarf2_cu *cu)
14039{
14040 struct attribute *attr = dwarf2_attr (die, name, cu);
14041
14042 return (attr && DW_UNSND (attr));
14043}
14044
3ca72b44 14045static int
e142c38c 14046die_is_declaration (struct die_info *die, struct dwarf2_cu *cu)
3ca72b44 14047{
05cf31d1
JB
14048 /* A DIE is a declaration if it has a DW_AT_declaration attribute
14049 which value is non-zero. However, we have to be careful with
14050 DIEs having a DW_AT_specification attribute, because dwarf2_attr()
14051 (via dwarf2_flag_true_p) follows this attribute. So we may
14052 end up accidently finding a declaration attribute that belongs
14053 to a different DIE referenced by the specification attribute,
14054 even though the given DIE does not have a declaration attribute. */
14055 return (dwarf2_flag_true_p (die, DW_AT_declaration, cu)
14056 && dwarf2_attr (die, DW_AT_specification, cu) == NULL);
3ca72b44
AC
14057}
14058
63d06c5c 14059/* Return the die giving the specification for DIE, if there is
f2f0e013 14060 one. *SPEC_CU is the CU containing DIE on input, and the CU
edb3359d
DJ
14061 containing the return value on output. If there is no
14062 specification, but there is an abstract origin, that is
14063 returned. */
63d06c5c
DC
14064
14065static struct die_info *
f2f0e013 14066die_specification (struct die_info *die, struct dwarf2_cu **spec_cu)
63d06c5c 14067{
f2f0e013
DJ
14068 struct attribute *spec_attr = dwarf2_attr (die, DW_AT_specification,
14069 *spec_cu);
63d06c5c 14070
edb3359d
DJ
14071 if (spec_attr == NULL)
14072 spec_attr = dwarf2_attr (die, DW_AT_abstract_origin, *spec_cu);
14073
63d06c5c
DC
14074 if (spec_attr == NULL)
14075 return NULL;
14076 else
f2f0e013 14077 return follow_die_ref (die, spec_attr, spec_cu);
63d06c5c 14078}
c906108c 14079
debd256d 14080/* Free the line_header structure *LH, and any arrays and strings it
ae2de4f8
DE
14081 refers to.
14082 NOTE: This is also used as a "cleanup" function. */
14083
debd256d
JB
14084static void
14085free_line_header (struct line_header *lh)
14086{
14087 if (lh->standard_opcode_lengths)
a8bc7b56 14088 xfree (lh->standard_opcode_lengths);
debd256d
JB
14089
14090 /* Remember that all the lh->file_names[i].name pointers are
14091 pointers into debug_line_buffer, and don't need to be freed. */
14092 if (lh->file_names)
a8bc7b56 14093 xfree (lh->file_names);
debd256d
JB
14094
14095 /* Similarly for the include directory names. */
14096 if (lh->include_dirs)
a8bc7b56 14097 xfree (lh->include_dirs);
debd256d 14098
a8bc7b56 14099 xfree (lh);
debd256d
JB
14100}
14101
debd256d 14102/* Add an entry to LH's include directory table. */
ae2de4f8 14103
debd256d
JB
14104static void
14105add_include_dir (struct line_header *lh, char *include_dir)
c906108c 14106{
debd256d
JB
14107 /* Grow the array if necessary. */
14108 if (lh->include_dirs_size == 0)
c5aa993b 14109 {
debd256d
JB
14110 lh->include_dirs_size = 1; /* for testing */
14111 lh->include_dirs = xmalloc (lh->include_dirs_size
14112 * sizeof (*lh->include_dirs));
14113 }
14114 else if (lh->num_include_dirs >= lh->include_dirs_size)
14115 {
14116 lh->include_dirs_size *= 2;
14117 lh->include_dirs = xrealloc (lh->include_dirs,
14118 (lh->include_dirs_size
14119 * sizeof (*lh->include_dirs)));
c5aa993b 14120 }
c906108c 14121
debd256d
JB
14122 lh->include_dirs[lh->num_include_dirs++] = include_dir;
14123}
6e70227d 14124
debd256d 14125/* Add an entry to LH's file name table. */
ae2de4f8 14126
debd256d
JB
14127static void
14128add_file_name (struct line_header *lh,
14129 char *name,
14130 unsigned int dir_index,
14131 unsigned int mod_time,
14132 unsigned int length)
14133{
14134 struct file_entry *fe;
14135
14136 /* Grow the array if necessary. */
14137 if (lh->file_names_size == 0)
14138 {
14139 lh->file_names_size = 1; /* for testing */
14140 lh->file_names = xmalloc (lh->file_names_size
14141 * sizeof (*lh->file_names));
14142 }
14143 else if (lh->num_file_names >= lh->file_names_size)
14144 {
14145 lh->file_names_size *= 2;
14146 lh->file_names = xrealloc (lh->file_names,
14147 (lh->file_names_size
14148 * sizeof (*lh->file_names)));
14149 }
14150
14151 fe = &lh->file_names[lh->num_file_names++];
14152 fe->name = name;
14153 fe->dir_index = dir_index;
14154 fe->mod_time = mod_time;
14155 fe->length = length;
aaa75496 14156 fe->included_p = 0;
cb1df416 14157 fe->symtab = NULL;
debd256d 14158}
6e70227d 14159
36586728
TT
14160/* A convenience function to find the proper .debug_line section for a
14161 CU. */
14162
14163static struct dwarf2_section_info *
14164get_debug_line_section (struct dwarf2_cu *cu)
14165{
14166 struct dwarf2_section_info *section;
14167
14168 /* For TUs in DWO files, the DW_AT_stmt_list attribute lives in the
14169 DWO file. */
14170 if (cu->dwo_unit && cu->per_cu->is_debug_types)
14171 section = &cu->dwo_unit->dwo_file->sections.line;
14172 else if (cu->per_cu->is_dwz)
14173 {
14174 struct dwz_file *dwz = dwarf2_get_dwz_file ();
14175
14176 section = &dwz->line;
14177 }
14178 else
14179 section = &dwarf2_per_objfile->line;
14180
14181 return section;
14182}
14183
debd256d 14184/* Read the statement program header starting at OFFSET in
3019eac3 14185 .debug_line, or .debug_line.dwo. Return a pointer
6502dd73 14186 to a struct line_header, allocated using xmalloc.
debd256d
JB
14187
14188 NOTE: the strings in the include directory and file name tables of
3019eac3
DE
14189 the returned object point into the dwarf line section buffer,
14190 and must not be freed. */
ae2de4f8 14191
debd256d 14192static struct line_header *
3019eac3 14193dwarf_decode_line_header (unsigned int offset, struct dwarf2_cu *cu)
debd256d
JB
14194{
14195 struct cleanup *back_to;
14196 struct line_header *lh;
fe1b8b76 14197 gdb_byte *line_ptr;
c764a876 14198 unsigned int bytes_read, offset_size;
debd256d
JB
14199 int i;
14200 char *cur_dir, *cur_file;
3019eac3
DE
14201 struct dwarf2_section_info *section;
14202 bfd *abfd;
14203
36586728 14204 section = get_debug_line_section (cu);
3019eac3
DE
14205 dwarf2_read_section (dwarf2_per_objfile->objfile, section);
14206 if (section->buffer == NULL)
debd256d 14207 {
3019eac3
DE
14208 if (cu->dwo_unit && cu->per_cu->is_debug_types)
14209 complaint (&symfile_complaints, _("missing .debug_line.dwo section"));
14210 else
14211 complaint (&symfile_complaints, _("missing .debug_line section"));
debd256d
JB
14212 return 0;
14213 }
14214
fceca515
DE
14215 /* We can't do this until we know the section is non-empty.
14216 Only then do we know we have such a section. */
14217 abfd = section->asection->owner;
14218
a738430d
MK
14219 /* Make sure that at least there's room for the total_length field.
14220 That could be 12 bytes long, but we're just going to fudge that. */
3019eac3 14221 if (offset + 4 >= section->size)
debd256d 14222 {
4d3c2250 14223 dwarf2_statement_list_fits_in_line_number_section_complaint ();
debd256d
JB
14224 return 0;
14225 }
14226
14227 lh = xmalloc (sizeof (*lh));
14228 memset (lh, 0, sizeof (*lh));
14229 back_to = make_cleanup ((make_cleanup_ftype *) free_line_header,
14230 (void *) lh);
14231
3019eac3 14232 line_ptr = section->buffer + offset;
debd256d 14233
a738430d 14234 /* Read in the header. */
6e70227d 14235 lh->total_length =
c764a876
DE
14236 read_checked_initial_length_and_offset (abfd, line_ptr, &cu->header,
14237 &bytes_read, &offset_size);
debd256d 14238 line_ptr += bytes_read;
3019eac3 14239 if (line_ptr + lh->total_length > (section->buffer + section->size))
debd256d 14240 {
4d3c2250 14241 dwarf2_statement_list_fits_in_line_number_section_complaint ();
debd256d
JB
14242 return 0;
14243 }
14244 lh->statement_program_end = line_ptr + lh->total_length;
14245 lh->version = read_2_bytes (abfd, line_ptr);
14246 line_ptr += 2;
c764a876
DE
14247 lh->header_length = read_offset_1 (abfd, line_ptr, offset_size);
14248 line_ptr += offset_size;
debd256d
JB
14249 lh->minimum_instruction_length = read_1_byte (abfd, line_ptr);
14250 line_ptr += 1;
2dc7f7b3
TT
14251 if (lh->version >= 4)
14252 {
14253 lh->maximum_ops_per_instruction = read_1_byte (abfd, line_ptr);
14254 line_ptr += 1;
14255 }
14256 else
14257 lh->maximum_ops_per_instruction = 1;
14258
14259 if (lh->maximum_ops_per_instruction == 0)
14260 {
14261 lh->maximum_ops_per_instruction = 1;
14262 complaint (&symfile_complaints,
3e43a32a
MS
14263 _("invalid maximum_ops_per_instruction "
14264 "in `.debug_line' section"));
2dc7f7b3
TT
14265 }
14266
debd256d
JB
14267 lh->default_is_stmt = read_1_byte (abfd, line_ptr);
14268 line_ptr += 1;
14269 lh->line_base = read_1_signed_byte (abfd, line_ptr);
14270 line_ptr += 1;
14271 lh->line_range = read_1_byte (abfd, line_ptr);
14272 line_ptr += 1;
14273 lh->opcode_base = read_1_byte (abfd, line_ptr);
14274 line_ptr += 1;
14275 lh->standard_opcode_lengths
fe1b8b76 14276 = xmalloc (lh->opcode_base * sizeof (lh->standard_opcode_lengths[0]));
debd256d
JB
14277
14278 lh->standard_opcode_lengths[0] = 1; /* This should never be used anyway. */
14279 for (i = 1; i < lh->opcode_base; ++i)
14280 {
14281 lh->standard_opcode_lengths[i] = read_1_byte (abfd, line_ptr);
14282 line_ptr += 1;
14283 }
14284
a738430d 14285 /* Read directory table. */
9b1c24c8 14286 while ((cur_dir = read_direct_string (abfd, line_ptr, &bytes_read)) != NULL)
debd256d
JB
14287 {
14288 line_ptr += bytes_read;
14289 add_include_dir (lh, cur_dir);
14290 }
14291 line_ptr += bytes_read;
14292
a738430d 14293 /* Read file name table. */
9b1c24c8 14294 while ((cur_file = read_direct_string (abfd, line_ptr, &bytes_read)) != NULL)
debd256d
JB
14295 {
14296 unsigned int dir_index, mod_time, length;
14297
14298 line_ptr += bytes_read;
14299 dir_index = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
14300 line_ptr += bytes_read;
14301 mod_time = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
14302 line_ptr += bytes_read;
14303 length = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
14304 line_ptr += bytes_read;
14305
14306 add_file_name (lh, cur_file, dir_index, mod_time, length);
14307 }
14308 line_ptr += bytes_read;
6e70227d 14309 lh->statement_program_start = line_ptr;
debd256d 14310
3019eac3 14311 if (line_ptr > (section->buffer + section->size))
4d3c2250 14312 complaint (&symfile_complaints,
3e43a32a
MS
14313 _("line number info header doesn't "
14314 "fit in `.debug_line' section"));
debd256d
JB
14315
14316 discard_cleanups (back_to);
14317 return lh;
14318}
c906108c 14319
c6da4cef
DE
14320/* Subroutine of dwarf_decode_lines to simplify it.
14321 Return the file name of the psymtab for included file FILE_INDEX
14322 in line header LH of PST.
14323 COMP_DIR is the compilation directory (DW_AT_comp_dir) or NULL if unknown.
14324 If space for the result is malloc'd, it will be freed by a cleanup.
14325 Returns NULL if FILE_INDEX should be ignored, i.e., it is pst->filename. */
14326
14327static char *
14328psymtab_include_file_name (const struct line_header *lh, int file_index,
14329 const struct partial_symtab *pst,
14330 const char *comp_dir)
14331{
14332 const struct file_entry fe = lh->file_names [file_index];
14333 char *include_name = fe.name;
14334 char *include_name_to_compare = include_name;
14335 char *dir_name = NULL;
72b9f47f
TT
14336 const char *pst_filename;
14337 char *copied_name = NULL;
c6da4cef
DE
14338 int file_is_pst;
14339
14340 if (fe.dir_index)
14341 dir_name = lh->include_dirs[fe.dir_index - 1];
14342
14343 if (!IS_ABSOLUTE_PATH (include_name)
14344 && (dir_name != NULL || comp_dir != NULL))
14345 {
14346 /* Avoid creating a duplicate psymtab for PST.
14347 We do this by comparing INCLUDE_NAME and PST_FILENAME.
14348 Before we do the comparison, however, we need to account
14349 for DIR_NAME and COMP_DIR.
14350 First prepend dir_name (if non-NULL). If we still don't
14351 have an absolute path prepend comp_dir (if non-NULL).
14352 However, the directory we record in the include-file's
14353 psymtab does not contain COMP_DIR (to match the
14354 corresponding symtab(s)).
14355
14356 Example:
14357
14358 bash$ cd /tmp
14359 bash$ gcc -g ./hello.c
14360 include_name = "hello.c"
14361 dir_name = "."
14362 DW_AT_comp_dir = comp_dir = "/tmp"
14363 DW_AT_name = "./hello.c" */
14364
14365 if (dir_name != NULL)
14366 {
14367 include_name = concat (dir_name, SLASH_STRING,
14368 include_name, (char *)NULL);
14369 include_name_to_compare = include_name;
14370 make_cleanup (xfree, include_name);
14371 }
14372 if (!IS_ABSOLUTE_PATH (include_name) && comp_dir != NULL)
14373 {
14374 include_name_to_compare = concat (comp_dir, SLASH_STRING,
14375 include_name, (char *)NULL);
14376 }
14377 }
14378
14379 pst_filename = pst->filename;
14380 if (!IS_ABSOLUTE_PATH (pst_filename) && pst->dirname != NULL)
14381 {
72b9f47f
TT
14382 copied_name = concat (pst->dirname, SLASH_STRING,
14383 pst_filename, (char *)NULL);
14384 pst_filename = copied_name;
c6da4cef
DE
14385 }
14386
1e3fad37 14387 file_is_pst = FILENAME_CMP (include_name_to_compare, pst_filename) == 0;
c6da4cef
DE
14388
14389 if (include_name_to_compare != include_name)
14390 xfree (include_name_to_compare);
72b9f47f
TT
14391 if (copied_name != NULL)
14392 xfree (copied_name);
c6da4cef
DE
14393
14394 if (file_is_pst)
14395 return NULL;
14396 return include_name;
14397}
14398
c91513d8
PP
14399/* Ignore this record_line request. */
14400
14401static void
14402noop_record_line (struct subfile *subfile, int line, CORE_ADDR pc)
14403{
14404 return;
14405}
14406
f3f5162e
DE
14407/* Subroutine of dwarf_decode_lines to simplify it.
14408 Process the line number information in LH. */
debd256d 14409
c906108c 14410static void
f3f5162e
DE
14411dwarf_decode_lines_1 (struct line_header *lh, const char *comp_dir,
14412 struct dwarf2_cu *cu, struct partial_symtab *pst)
c906108c 14413{
a8c50c1f 14414 gdb_byte *line_ptr, *extended_end;
fe1b8b76 14415 gdb_byte *line_end;
a8c50c1f 14416 unsigned int bytes_read, extended_len;
c906108c 14417 unsigned char op_code, extended_op, adj_opcode;
e142c38c
DJ
14418 CORE_ADDR baseaddr;
14419 struct objfile *objfile = cu->objfile;
f3f5162e 14420 bfd *abfd = objfile->obfd;
fbf65064 14421 struct gdbarch *gdbarch = get_objfile_arch (objfile);
aaa75496 14422 const int decode_for_pst_p = (pst != NULL);
f3f5162e 14423 struct subfile *last_subfile = NULL;
c91513d8
PP
14424 void (*p_record_line) (struct subfile *subfile, int line, CORE_ADDR pc)
14425 = record_line;
e142c38c
DJ
14426
14427 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
c906108c 14428
debd256d
JB
14429 line_ptr = lh->statement_program_start;
14430 line_end = lh->statement_program_end;
c906108c
SS
14431
14432 /* Read the statement sequences until there's nothing left. */
14433 while (line_ptr < line_end)
14434 {
14435 /* state machine registers */
14436 CORE_ADDR address = 0;
14437 unsigned int file = 1;
14438 unsigned int line = 1;
14439 unsigned int column = 0;
debd256d 14440 int is_stmt = lh->default_is_stmt;
c906108c
SS
14441 int basic_block = 0;
14442 int end_sequence = 0;
fbf65064 14443 CORE_ADDR addr;
2dc7f7b3 14444 unsigned char op_index = 0;
c906108c 14445
aaa75496 14446 if (!decode_for_pst_p && lh->num_file_names >= file)
c906108c 14447 {
aaa75496 14448 /* Start a subfile for the current file of the state machine. */
debd256d
JB
14449 /* lh->include_dirs and lh->file_names are 0-based, but the
14450 directory and file name numbers in the statement program
14451 are 1-based. */
14452 struct file_entry *fe = &lh->file_names[file - 1];
4f1520fb 14453 char *dir = NULL;
a738430d 14454
debd256d
JB
14455 if (fe->dir_index)
14456 dir = lh->include_dirs[fe->dir_index - 1];
4f1520fb
FR
14457
14458 dwarf2_start_subfile (fe->name, dir, comp_dir);
c906108c
SS
14459 }
14460
a738430d 14461 /* Decode the table. */
c5aa993b 14462 while (!end_sequence)
c906108c
SS
14463 {
14464 op_code = read_1_byte (abfd, line_ptr);
14465 line_ptr += 1;
59205f5a
JB
14466 if (line_ptr > line_end)
14467 {
14468 dwarf2_debug_line_missing_end_sequence_complaint ();
14469 break;
14470 }
9aa1fe7e 14471
debd256d 14472 if (op_code >= lh->opcode_base)
6e70227d 14473 {
a738430d 14474 /* Special operand. */
debd256d 14475 adj_opcode = op_code - lh->opcode_base;
2dc7f7b3
TT
14476 address += (((op_index + (adj_opcode / lh->line_range))
14477 / lh->maximum_ops_per_instruction)
14478 * lh->minimum_instruction_length);
14479 op_index = ((op_index + (adj_opcode / lh->line_range))
14480 % lh->maximum_ops_per_instruction);
debd256d 14481 line += lh->line_base + (adj_opcode % lh->line_range);
59205f5a 14482 if (lh->num_file_names < file || file == 0)
25e43795 14483 dwarf2_debug_line_missing_file_complaint ();
2dc7f7b3
TT
14484 /* For now we ignore lines not starting on an
14485 instruction boundary. */
14486 else if (op_index == 0)
25e43795
DJ
14487 {
14488 lh->file_names[file - 1].included_p = 1;
ca5f395d 14489 if (!decode_for_pst_p && is_stmt)
fbf65064
UW
14490 {
14491 if (last_subfile != current_subfile)
14492 {
14493 addr = gdbarch_addr_bits_remove (gdbarch, address);
14494 if (last_subfile)
c91513d8 14495 (*p_record_line) (last_subfile, 0, addr);
fbf65064
UW
14496 last_subfile = current_subfile;
14497 }
25e43795 14498 /* Append row to matrix using current values. */
7019d805 14499 addr = gdbarch_addr_bits_remove (gdbarch, address);
c91513d8 14500 (*p_record_line) (current_subfile, line, addr);
366da635 14501 }
25e43795 14502 }
ca5f395d 14503 basic_block = 0;
9aa1fe7e
GK
14504 }
14505 else switch (op_code)
c906108c
SS
14506 {
14507 case DW_LNS_extended_op:
3e43a32a
MS
14508 extended_len = read_unsigned_leb128 (abfd, line_ptr,
14509 &bytes_read);
473b7be6 14510 line_ptr += bytes_read;
a8c50c1f 14511 extended_end = line_ptr + extended_len;
c906108c
SS
14512 extended_op = read_1_byte (abfd, line_ptr);
14513 line_ptr += 1;
14514 switch (extended_op)
14515 {
14516 case DW_LNE_end_sequence:
c91513d8 14517 p_record_line = record_line;
c906108c 14518 end_sequence = 1;
c906108c
SS
14519 break;
14520 case DW_LNE_set_address:
e7c27a73 14521 address = read_address (abfd, line_ptr, cu, &bytes_read);
c91513d8
PP
14522
14523 if (address == 0 && !dwarf2_per_objfile->has_section_at_zero)
14524 {
14525 /* This line table is for a function which has been
14526 GCd by the linker. Ignore it. PR gdb/12528 */
14527
14528 long line_offset
36586728 14529 = line_ptr - get_debug_line_section (cu)->buffer;
c91513d8
PP
14530
14531 complaint (&symfile_complaints,
14532 _(".debug_line address at offset 0x%lx is 0 "
14533 "[in module %s]"),
bb5ed363 14534 line_offset, objfile->name);
c91513d8
PP
14535 p_record_line = noop_record_line;
14536 }
14537
2dc7f7b3 14538 op_index = 0;
107d2387
AC
14539 line_ptr += bytes_read;
14540 address += baseaddr;
c906108c
SS
14541 break;
14542 case DW_LNE_define_file:
debd256d
JB
14543 {
14544 char *cur_file;
14545 unsigned int dir_index, mod_time, length;
6e70227d 14546
3e43a32a
MS
14547 cur_file = read_direct_string (abfd, line_ptr,
14548 &bytes_read);
debd256d
JB
14549 line_ptr += bytes_read;
14550 dir_index =
14551 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
14552 line_ptr += bytes_read;
14553 mod_time =
14554 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
14555 line_ptr += bytes_read;
14556 length =
14557 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
14558 line_ptr += bytes_read;
14559 add_file_name (lh, cur_file, dir_index, mod_time, length);
14560 }
c906108c 14561 break;
d0c6ba3d
CC
14562 case DW_LNE_set_discriminator:
14563 /* The discriminator is not interesting to the debugger;
14564 just ignore it. */
14565 line_ptr = extended_end;
14566 break;
c906108c 14567 default:
4d3c2250 14568 complaint (&symfile_complaints,
e2e0b3e5 14569 _("mangled .debug_line section"));
debd256d 14570 return;
c906108c 14571 }
a8c50c1f
DJ
14572 /* Make sure that we parsed the extended op correctly. If e.g.
14573 we expected a different address size than the producer used,
14574 we may have read the wrong number of bytes. */
14575 if (line_ptr != extended_end)
14576 {
14577 complaint (&symfile_complaints,
14578 _("mangled .debug_line section"));
14579 return;
14580 }
c906108c
SS
14581 break;
14582 case DW_LNS_copy:
59205f5a 14583 if (lh->num_file_names < file || file == 0)
25e43795
DJ
14584 dwarf2_debug_line_missing_file_complaint ();
14585 else
366da635 14586 {
25e43795 14587 lh->file_names[file - 1].included_p = 1;
ca5f395d 14588 if (!decode_for_pst_p && is_stmt)
fbf65064
UW
14589 {
14590 if (last_subfile != current_subfile)
14591 {
14592 addr = gdbarch_addr_bits_remove (gdbarch, address);
14593 if (last_subfile)
c91513d8 14594 (*p_record_line) (last_subfile, 0, addr);
fbf65064
UW
14595 last_subfile = current_subfile;
14596 }
7019d805 14597 addr = gdbarch_addr_bits_remove (gdbarch, address);
c91513d8 14598 (*p_record_line) (current_subfile, line, addr);
fbf65064 14599 }
366da635 14600 }
c906108c
SS
14601 basic_block = 0;
14602 break;
14603 case DW_LNS_advance_pc:
2dc7f7b3
TT
14604 {
14605 CORE_ADDR adjust
14606 = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
14607
14608 address += (((op_index + adjust)
14609 / lh->maximum_ops_per_instruction)
14610 * lh->minimum_instruction_length);
14611 op_index = ((op_index + adjust)
14612 % lh->maximum_ops_per_instruction);
14613 line_ptr += bytes_read;
14614 }
c906108c
SS
14615 break;
14616 case DW_LNS_advance_line:
14617 line += read_signed_leb128 (abfd, line_ptr, &bytes_read);
14618 line_ptr += bytes_read;
14619 break;
14620 case DW_LNS_set_file:
debd256d 14621 {
a738430d
MK
14622 /* The arrays lh->include_dirs and lh->file_names are
14623 0-based, but the directory and file name numbers in
14624 the statement program are 1-based. */
debd256d 14625 struct file_entry *fe;
4f1520fb 14626 char *dir = NULL;
a738430d 14627
debd256d
JB
14628 file = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
14629 line_ptr += bytes_read;
59205f5a 14630 if (lh->num_file_names < file || file == 0)
25e43795
DJ
14631 dwarf2_debug_line_missing_file_complaint ();
14632 else
14633 {
14634 fe = &lh->file_names[file - 1];
14635 if (fe->dir_index)
14636 dir = lh->include_dirs[fe->dir_index - 1];
14637 if (!decode_for_pst_p)
14638 {
14639 last_subfile = current_subfile;
14640 dwarf2_start_subfile (fe->name, dir, comp_dir);
14641 }
14642 }
debd256d 14643 }
c906108c
SS
14644 break;
14645 case DW_LNS_set_column:
14646 column = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
14647 line_ptr += bytes_read;
14648 break;
14649 case DW_LNS_negate_stmt:
14650 is_stmt = (!is_stmt);
14651 break;
14652 case DW_LNS_set_basic_block:
14653 basic_block = 1;
14654 break;
c2c6d25f
JM
14655 /* Add to the address register of the state machine the
14656 address increment value corresponding to special opcode
a738430d
MK
14657 255. I.e., this value is scaled by the minimum
14658 instruction length since special opcode 255 would have
b021a221 14659 scaled the increment. */
c906108c 14660 case DW_LNS_const_add_pc:
2dc7f7b3
TT
14661 {
14662 CORE_ADDR adjust = (255 - lh->opcode_base) / lh->line_range;
14663
14664 address += (((op_index + adjust)
14665 / lh->maximum_ops_per_instruction)
14666 * lh->minimum_instruction_length);
14667 op_index = ((op_index + adjust)
14668 % lh->maximum_ops_per_instruction);
14669 }
c906108c
SS
14670 break;
14671 case DW_LNS_fixed_advance_pc:
14672 address += read_2_bytes (abfd, line_ptr);
2dc7f7b3 14673 op_index = 0;
c906108c
SS
14674 line_ptr += 2;
14675 break;
9aa1fe7e 14676 default:
a738430d
MK
14677 {
14678 /* Unknown standard opcode, ignore it. */
9aa1fe7e 14679 int i;
a738430d 14680
debd256d 14681 for (i = 0; i < lh->standard_opcode_lengths[op_code]; i++)
9aa1fe7e
GK
14682 {
14683 (void) read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
14684 line_ptr += bytes_read;
14685 }
14686 }
c906108c
SS
14687 }
14688 }
59205f5a
JB
14689 if (lh->num_file_names < file || file == 0)
14690 dwarf2_debug_line_missing_file_complaint ();
14691 else
14692 {
14693 lh->file_names[file - 1].included_p = 1;
14694 if (!decode_for_pst_p)
fbf65064
UW
14695 {
14696 addr = gdbarch_addr_bits_remove (gdbarch, address);
c91513d8 14697 (*p_record_line) (current_subfile, 0, addr);
fbf65064 14698 }
59205f5a 14699 }
c906108c 14700 }
f3f5162e
DE
14701}
14702
14703/* Decode the Line Number Program (LNP) for the given line_header
14704 structure and CU. The actual information extracted and the type
14705 of structures created from the LNP depends on the value of PST.
14706
14707 1. If PST is NULL, then this procedure uses the data from the program
14708 to create all necessary symbol tables, and their linetables.
14709
14710 2. If PST is not NULL, this procedure reads the program to determine
14711 the list of files included by the unit represented by PST, and
14712 builds all the associated partial symbol tables.
14713
14714 COMP_DIR is the compilation directory (DW_AT_comp_dir) or NULL if unknown.
14715 It is used for relative paths in the line table.
14716 NOTE: When processing partial symtabs (pst != NULL),
14717 comp_dir == pst->dirname.
14718
14719 NOTE: It is important that psymtabs have the same file name (via strcmp)
14720 as the corresponding symtab. Since COMP_DIR is not used in the name of the
14721 symtab we don't use it in the name of the psymtabs we create.
14722 E.g. expand_line_sal requires this when finding psymtabs to expand.
14723 A good testcase for this is mb-inline.exp. */
14724
14725static void
14726dwarf_decode_lines (struct line_header *lh, const char *comp_dir,
14727 struct dwarf2_cu *cu, struct partial_symtab *pst,
14728 int want_line_info)
14729{
14730 struct objfile *objfile = cu->objfile;
14731 const int decode_for_pst_p = (pst != NULL);
14732 struct subfile *first_subfile = current_subfile;
14733
14734 if (want_line_info)
14735 dwarf_decode_lines_1 (lh, comp_dir, cu, pst);
aaa75496
JB
14736
14737 if (decode_for_pst_p)
14738 {
14739 int file_index;
14740
14741 /* Now that we're done scanning the Line Header Program, we can
14742 create the psymtab of each included file. */
14743 for (file_index = 0; file_index < lh->num_file_names; file_index++)
14744 if (lh->file_names[file_index].included_p == 1)
14745 {
c6da4cef
DE
14746 char *include_name =
14747 psymtab_include_file_name (lh, file_index, pst, comp_dir);
14748 if (include_name != NULL)
aaa75496
JB
14749 dwarf2_create_include_psymtab (include_name, pst, objfile);
14750 }
14751 }
cb1df416
DJ
14752 else
14753 {
14754 /* Make sure a symtab is created for every file, even files
14755 which contain only variables (i.e. no code with associated
14756 line numbers). */
cb1df416 14757 int i;
cb1df416
DJ
14758
14759 for (i = 0; i < lh->num_file_names; i++)
14760 {
14761 char *dir = NULL;
f3f5162e 14762 struct file_entry *fe;
9a619af0 14763
cb1df416
DJ
14764 fe = &lh->file_names[i];
14765 if (fe->dir_index)
14766 dir = lh->include_dirs[fe->dir_index - 1];
14767 dwarf2_start_subfile (fe->name, dir, comp_dir);
14768
14769 /* Skip the main file; we don't need it, and it must be
14770 allocated last, so that it will show up before the
14771 non-primary symtabs in the objfile's symtab list. */
14772 if (current_subfile == first_subfile)
14773 continue;
14774
14775 if (current_subfile->symtab == NULL)
14776 current_subfile->symtab = allocate_symtab (current_subfile->name,
bb5ed363 14777 objfile);
cb1df416
DJ
14778 fe->symtab = current_subfile->symtab;
14779 }
14780 }
c906108c
SS
14781}
14782
14783/* Start a subfile for DWARF. FILENAME is the name of the file and
14784 DIRNAME the name of the source directory which contains FILENAME
4f1520fb
FR
14785 or NULL if not known. COMP_DIR is the compilation directory for the
14786 linetable's compilation unit or NULL if not known.
c906108c
SS
14787 This routine tries to keep line numbers from identical absolute and
14788 relative file names in a common subfile.
14789
14790 Using the `list' example from the GDB testsuite, which resides in
14791 /srcdir and compiling it with Irix6.2 cc in /compdir using a filename
14792 of /srcdir/list0.c yields the following debugging information for list0.c:
14793
c5aa993b
JM
14794 DW_AT_name: /srcdir/list0.c
14795 DW_AT_comp_dir: /compdir
357e46e7 14796 files.files[0].name: list0.h
c5aa993b 14797 files.files[0].dir: /srcdir
357e46e7 14798 files.files[1].name: list0.c
c5aa993b 14799 files.files[1].dir: /srcdir
c906108c
SS
14800
14801 The line number information for list0.c has to end up in a single
4f1520fb
FR
14802 subfile, so that `break /srcdir/list0.c:1' works as expected.
14803 start_subfile will ensure that this happens provided that we pass the
14804 concatenation of files.files[1].dir and files.files[1].name as the
14805 subfile's name. */
c906108c
SS
14806
14807static void
3e43a32a
MS
14808dwarf2_start_subfile (char *filename, const char *dirname,
14809 const char *comp_dir)
c906108c 14810{
4f1520fb
FR
14811 char *fullname;
14812
14813 /* While reading the DIEs, we call start_symtab(DW_AT_name, DW_AT_comp_dir).
14814 `start_symtab' will always pass the contents of DW_AT_comp_dir as
14815 second argument to start_subfile. To be consistent, we do the
14816 same here. In order not to lose the line information directory,
14817 we concatenate it to the filename when it makes sense.
14818 Note that the Dwarf3 standard says (speaking of filenames in line
14819 information): ``The directory index is ignored for file names
14820 that represent full path names''. Thus ignoring dirname in the
14821 `else' branch below isn't an issue. */
c906108c 14822
d5166ae1 14823 if (!IS_ABSOLUTE_PATH (filename) && dirname != NULL)
4f1520fb
FR
14824 fullname = concat (dirname, SLASH_STRING, filename, (char *)NULL);
14825 else
14826 fullname = filename;
c906108c 14827
4f1520fb
FR
14828 start_subfile (fullname, comp_dir);
14829
14830 if (fullname != filename)
14831 xfree (fullname);
c906108c
SS
14832}
14833
f4dc4d17
DE
14834/* Start a symtab for DWARF.
14835 NAME, COMP_DIR, LOW_PC are passed to start_symtab. */
14836
14837static void
14838dwarf2_start_symtab (struct dwarf2_cu *cu,
14839 char *name, char *comp_dir, CORE_ADDR low_pc)
14840{
14841 start_symtab (name, comp_dir, low_pc);
14842 record_debugformat ("DWARF 2");
14843 record_producer (cu->producer);
14844
14845 /* We assume that we're processing GCC output. */
14846 processing_gcc_compilation = 2;
14847
14848 processing_has_namespace_info = 0;
14849}
14850
4c2df51b
DJ
14851static void
14852var_decode_location (struct attribute *attr, struct symbol *sym,
e7c27a73 14853 struct dwarf2_cu *cu)
4c2df51b 14854{
e7c27a73
DJ
14855 struct objfile *objfile = cu->objfile;
14856 struct comp_unit_head *cu_header = &cu->header;
14857
4c2df51b
DJ
14858 /* NOTE drow/2003-01-30: There used to be a comment and some special
14859 code here to turn a symbol with DW_AT_external and a
14860 SYMBOL_VALUE_ADDRESS of 0 into a LOC_UNRESOLVED symbol. This was
14861 necessary for platforms (maybe Alpha, certainly PowerPC GNU/Linux
14862 with some versions of binutils) where shared libraries could have
14863 relocations against symbols in their debug information - the
14864 minimal symbol would have the right address, but the debug info
14865 would not. It's no longer necessary, because we will explicitly
14866 apply relocations when we read in the debug information now. */
14867
14868 /* A DW_AT_location attribute with no contents indicates that a
14869 variable has been optimized away. */
14870 if (attr_form_is_block (attr) && DW_BLOCK (attr)->size == 0)
14871 {
14872 SYMBOL_CLASS (sym) = LOC_OPTIMIZED_OUT;
14873 return;
14874 }
14875
14876 /* Handle one degenerate form of location expression specially, to
14877 preserve GDB's previous behavior when section offsets are
3019eac3
DE
14878 specified. If this is just a DW_OP_addr or DW_OP_GNU_addr_index
14879 then mark this symbol as LOC_STATIC. */
4c2df51b
DJ
14880
14881 if (attr_form_is_block (attr)
3019eac3
DE
14882 && ((DW_BLOCK (attr)->data[0] == DW_OP_addr
14883 && DW_BLOCK (attr)->size == 1 + cu_header->addr_size)
14884 || (DW_BLOCK (attr)->data[0] == DW_OP_GNU_addr_index
14885 && (DW_BLOCK (attr)->size
14886 == 1 + leb128_size (&DW_BLOCK (attr)->data[1])))))
4c2df51b 14887 {
891d2f0b 14888 unsigned int dummy;
4c2df51b 14889
3019eac3
DE
14890 if (DW_BLOCK (attr)->data[0] == DW_OP_addr)
14891 SYMBOL_VALUE_ADDRESS (sym) =
14892 read_address (objfile->obfd, DW_BLOCK (attr)->data + 1, cu, &dummy);
14893 else
14894 SYMBOL_VALUE_ADDRESS (sym) =
14895 read_addr_index_from_leb128 (cu, DW_BLOCK (attr)->data + 1, &dummy);
907fc202 14896 SYMBOL_CLASS (sym) = LOC_STATIC;
4c2df51b
DJ
14897 fixup_symbol_section (sym, objfile);
14898 SYMBOL_VALUE_ADDRESS (sym) += ANOFFSET (objfile->section_offsets,
14899 SYMBOL_SECTION (sym));
4c2df51b
DJ
14900 return;
14901 }
14902
14903 /* NOTE drow/2002-01-30: It might be worthwhile to have a static
14904 expression evaluator, and use LOC_COMPUTED only when necessary
14905 (i.e. when the value of a register or memory location is
14906 referenced, or a thread-local block, etc.). Then again, it might
14907 not be worthwhile. I'm assuming that it isn't unless performance
14908 or memory numbers show me otherwise. */
14909
e7c27a73 14910 dwarf2_symbol_mark_computed (attr, sym, cu);
4c2df51b 14911 SYMBOL_CLASS (sym) = LOC_COMPUTED;
8be455d7
JK
14912
14913 if (SYMBOL_COMPUTED_OPS (sym) == &dwarf2_loclist_funcs)
14914 cu->has_loclist = 1;
4c2df51b
DJ
14915}
14916
c906108c
SS
14917/* Given a pointer to a DWARF information entry, figure out if we need
14918 to make a symbol table entry for it, and if so, create a new entry
14919 and return a pointer to it.
14920 If TYPE is NULL, determine symbol type from the die, otherwise
34eaf542
TT
14921 used the passed type.
14922 If SPACE is not NULL, use it to hold the new symbol. If it is
14923 NULL, allocate a new symbol on the objfile's obstack. */
c906108c
SS
14924
14925static struct symbol *
34eaf542
TT
14926new_symbol_full (struct die_info *die, struct type *type, struct dwarf2_cu *cu,
14927 struct symbol *space)
c906108c 14928{
e7c27a73 14929 struct objfile *objfile = cu->objfile;
c906108c
SS
14930 struct symbol *sym = NULL;
14931 char *name;
14932 struct attribute *attr = NULL;
14933 struct attribute *attr2 = NULL;
e142c38c 14934 CORE_ADDR baseaddr;
e37fd15a
SW
14935 struct pending **list_to_add = NULL;
14936
edb3359d 14937 int inlined_func = (die->tag == DW_TAG_inlined_subroutine);
e142c38c
DJ
14938
14939 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
c906108c 14940
94af9270 14941 name = dwarf2_name (die, cu);
c906108c
SS
14942 if (name)
14943 {
94af9270 14944 const char *linkagename;
34eaf542 14945 int suppress_add = 0;
94af9270 14946
34eaf542
TT
14947 if (space)
14948 sym = space;
14949 else
14950 sym = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct symbol);
c906108c 14951 OBJSTAT (objfile, n_syms++);
2de7ced7
DJ
14952
14953 /* Cache this symbol's name and the name's demangled form (if any). */
33e5013e 14954 SYMBOL_SET_LANGUAGE (sym, cu->language);
94af9270
KS
14955 linkagename = dwarf2_physname (name, die, cu);
14956 SYMBOL_SET_NAMES (sym, linkagename, strlen (linkagename), 0, objfile);
c906108c 14957
f55ee35c
JK
14958 /* Fortran does not have mangling standard and the mangling does differ
14959 between gfortran, iFort etc. */
14960 if (cu->language == language_fortran
b250c185 14961 && symbol_get_demangled_name (&(sym->ginfo)) == NULL)
29df156d
SW
14962 symbol_set_demangled_name (&(sym->ginfo),
14963 (char *) dwarf2_full_name (name, die, cu),
14964 NULL);
f55ee35c 14965
c906108c 14966 /* Default assumptions.
c5aa993b 14967 Use the passed type or decode it from the die. */
176620f1 14968 SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
875dc2fc 14969 SYMBOL_CLASS (sym) = LOC_OPTIMIZED_OUT;
c906108c
SS
14970 if (type != NULL)
14971 SYMBOL_TYPE (sym) = type;
14972 else
e7c27a73 14973 SYMBOL_TYPE (sym) = die_type (die, cu);
edb3359d
DJ
14974 attr = dwarf2_attr (die,
14975 inlined_func ? DW_AT_call_line : DW_AT_decl_line,
14976 cu);
c906108c
SS
14977 if (attr)
14978 {
14979 SYMBOL_LINE (sym) = DW_UNSND (attr);
14980 }
cb1df416 14981
edb3359d
DJ
14982 attr = dwarf2_attr (die,
14983 inlined_func ? DW_AT_call_file : DW_AT_decl_file,
14984 cu);
cb1df416
DJ
14985 if (attr)
14986 {
14987 int file_index = DW_UNSND (attr);
9a619af0 14988
cb1df416
DJ
14989 if (cu->line_header == NULL
14990 || file_index > cu->line_header->num_file_names)
14991 complaint (&symfile_complaints,
14992 _("file index out of range"));
1c3d648d 14993 else if (file_index > 0)
cb1df416
DJ
14994 {
14995 struct file_entry *fe;
9a619af0 14996
cb1df416
DJ
14997 fe = &cu->line_header->file_names[file_index - 1];
14998 SYMBOL_SYMTAB (sym) = fe->symtab;
14999 }
15000 }
15001
c906108c
SS
15002 switch (die->tag)
15003 {
15004 case DW_TAG_label:
e142c38c 15005 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
c906108c
SS
15006 if (attr)
15007 {
15008 SYMBOL_VALUE_ADDRESS (sym) = DW_ADDR (attr) + baseaddr;
15009 }
0f5238ed
TT
15010 SYMBOL_TYPE (sym) = objfile_type (objfile)->builtin_core_addr;
15011 SYMBOL_DOMAIN (sym) = LABEL_DOMAIN;
c906108c 15012 SYMBOL_CLASS (sym) = LOC_LABEL;
0f5238ed 15013 add_symbol_to_list (sym, cu->list_in_scope);
c906108c
SS
15014 break;
15015 case DW_TAG_subprogram:
15016 /* SYMBOL_BLOCK_VALUE (sym) will be filled in later by
15017 finish_block. */
15018 SYMBOL_CLASS (sym) = LOC_BLOCK;
e142c38c 15019 attr2 = dwarf2_attr (die, DW_AT_external, cu);
2cfa0c8d
JB
15020 if ((attr2 && (DW_UNSND (attr2) != 0))
15021 || cu->language == language_ada)
c906108c 15022 {
2cfa0c8d
JB
15023 /* Subprograms marked external are stored as a global symbol.
15024 Ada subprograms, whether marked external or not, are always
15025 stored as a global symbol, because we want to be able to
15026 access them globally. For instance, we want to be able
15027 to break on a nested subprogram without having to
15028 specify the context. */
e37fd15a 15029 list_to_add = &global_symbols;
c906108c
SS
15030 }
15031 else
15032 {
e37fd15a 15033 list_to_add = cu->list_in_scope;
c906108c
SS
15034 }
15035 break;
edb3359d
DJ
15036 case DW_TAG_inlined_subroutine:
15037 /* SYMBOL_BLOCK_VALUE (sym) will be filled in later by
15038 finish_block. */
15039 SYMBOL_CLASS (sym) = LOC_BLOCK;
15040 SYMBOL_INLINED (sym) = 1;
481860b3 15041 list_to_add = cu->list_in_scope;
edb3359d 15042 break;
34eaf542
TT
15043 case DW_TAG_template_value_param:
15044 suppress_add = 1;
15045 /* Fall through. */
72929c62 15046 case DW_TAG_constant:
c906108c 15047 case DW_TAG_variable:
254e6b9e 15048 case DW_TAG_member:
0963b4bd
MS
15049 /* Compilation with minimal debug info may result in
15050 variables with missing type entries. Change the
15051 misleading `void' type to something sensible. */
c906108c 15052 if (TYPE_CODE (SYMBOL_TYPE (sym)) == TYPE_CODE_VOID)
64c50499 15053 SYMBOL_TYPE (sym)
46bf5051 15054 = objfile_type (objfile)->nodebug_data_symbol;
64c50499 15055
e142c38c 15056 attr = dwarf2_attr (die, DW_AT_const_value, cu);
254e6b9e
DE
15057 /* In the case of DW_TAG_member, we should only be called for
15058 static const members. */
15059 if (die->tag == DW_TAG_member)
15060 {
3863f96c
DE
15061 /* dwarf2_add_field uses die_is_declaration,
15062 so we do the same. */
254e6b9e
DE
15063 gdb_assert (die_is_declaration (die, cu));
15064 gdb_assert (attr);
15065 }
c906108c
SS
15066 if (attr)
15067 {
e7c27a73 15068 dwarf2_const_value (attr, sym, cu);
e142c38c 15069 attr2 = dwarf2_attr (die, DW_AT_external, cu);
e37fd15a 15070 if (!suppress_add)
34eaf542
TT
15071 {
15072 if (attr2 && (DW_UNSND (attr2) != 0))
e37fd15a 15073 list_to_add = &global_symbols;
34eaf542 15074 else
e37fd15a 15075 list_to_add = cu->list_in_scope;
34eaf542 15076 }
c906108c
SS
15077 break;
15078 }
e142c38c 15079 attr = dwarf2_attr (die, DW_AT_location, cu);
c906108c
SS
15080 if (attr)
15081 {
e7c27a73 15082 var_decode_location (attr, sym, cu);
e142c38c 15083 attr2 = dwarf2_attr (die, DW_AT_external, cu);
4357ac6c
TT
15084
15085 /* Fortran explicitly imports any global symbols to the local
15086 scope by DW_TAG_common_block. */
15087 if (cu->language == language_fortran && die->parent
15088 && die->parent->tag == DW_TAG_common_block)
15089 attr2 = NULL;
15090
caac4577
JG
15091 if (SYMBOL_CLASS (sym) == LOC_STATIC
15092 && SYMBOL_VALUE_ADDRESS (sym) == 0
15093 && !dwarf2_per_objfile->has_section_at_zero)
15094 {
15095 /* When a static variable is eliminated by the linker,
15096 the corresponding debug information is not stripped
15097 out, but the variable address is set to null;
15098 do not add such variables into symbol table. */
15099 }
15100 else if (attr2 && (DW_UNSND (attr2) != 0))
1c809c68 15101 {
f55ee35c
JK
15102 /* Workaround gfortran PR debug/40040 - it uses
15103 DW_AT_location for variables in -fPIC libraries which may
15104 get overriden by other libraries/executable and get
15105 a different address. Resolve it by the minimal symbol
15106 which may come from inferior's executable using copy
15107 relocation. Make this workaround only for gfortran as for
15108 other compilers GDB cannot guess the minimal symbol
15109 Fortran mangling kind. */
15110 if (cu->language == language_fortran && die->parent
15111 && die->parent->tag == DW_TAG_module
15112 && cu->producer
15113 && strncmp (cu->producer, "GNU Fortran ", 12) == 0)
15114 SYMBOL_CLASS (sym) = LOC_UNRESOLVED;
15115
1c809c68
TT
15116 /* A variable with DW_AT_external is never static,
15117 but it may be block-scoped. */
15118 list_to_add = (cu->list_in_scope == &file_symbols
15119 ? &global_symbols : cu->list_in_scope);
1c809c68 15120 }
c906108c 15121 else
e37fd15a 15122 list_to_add = cu->list_in_scope;
c906108c
SS
15123 }
15124 else
15125 {
15126 /* We do not know the address of this symbol.
c5aa993b
JM
15127 If it is an external symbol and we have type information
15128 for it, enter the symbol as a LOC_UNRESOLVED symbol.
15129 The address of the variable will then be determined from
15130 the minimal symbol table whenever the variable is
15131 referenced. */
e142c38c 15132 attr2 = dwarf2_attr (die, DW_AT_external, cu);
0971de02
TT
15133
15134 /* Fortran explicitly imports any global symbols to the local
15135 scope by DW_TAG_common_block. */
15136 if (cu->language == language_fortran && die->parent
15137 && die->parent->tag == DW_TAG_common_block)
15138 {
15139 /* SYMBOL_CLASS doesn't matter here because
15140 read_common_block is going to reset it. */
15141 if (!suppress_add)
15142 list_to_add = cu->list_in_scope;
15143 }
15144 else if (attr2 && (DW_UNSND (attr2) != 0)
15145 && dwarf2_attr (die, DW_AT_type, cu) != NULL)
c906108c 15146 {
0fe7935b
DJ
15147 /* A variable with DW_AT_external is never static, but it
15148 may be block-scoped. */
15149 list_to_add = (cu->list_in_scope == &file_symbols
15150 ? &global_symbols : cu->list_in_scope);
15151
c906108c 15152 SYMBOL_CLASS (sym) = LOC_UNRESOLVED;
c906108c 15153 }
442ddf59
JK
15154 else if (!die_is_declaration (die, cu))
15155 {
15156 /* Use the default LOC_OPTIMIZED_OUT class. */
15157 gdb_assert (SYMBOL_CLASS (sym) == LOC_OPTIMIZED_OUT);
e37fd15a
SW
15158 if (!suppress_add)
15159 list_to_add = cu->list_in_scope;
442ddf59 15160 }
c906108c
SS
15161 }
15162 break;
15163 case DW_TAG_formal_parameter:
edb3359d
DJ
15164 /* If we are inside a function, mark this as an argument. If
15165 not, we might be looking at an argument to an inlined function
15166 when we do not have enough information to show inlined frames;
15167 pretend it's a local variable in that case so that the user can
15168 still see it. */
15169 if (context_stack_depth > 0
15170 && context_stack[context_stack_depth - 1].name != NULL)
15171 SYMBOL_IS_ARGUMENT (sym) = 1;
e142c38c 15172 attr = dwarf2_attr (die, DW_AT_location, cu);
c906108c
SS
15173 if (attr)
15174 {
e7c27a73 15175 var_decode_location (attr, sym, cu);
c906108c 15176 }
e142c38c 15177 attr = dwarf2_attr (die, DW_AT_const_value, cu);
c906108c
SS
15178 if (attr)
15179 {
e7c27a73 15180 dwarf2_const_value (attr, sym, cu);
c906108c 15181 }
f346a30d 15182
e37fd15a 15183 list_to_add = cu->list_in_scope;
c906108c
SS
15184 break;
15185 case DW_TAG_unspecified_parameters:
15186 /* From varargs functions; gdb doesn't seem to have any
15187 interest in this information, so just ignore it for now.
15188 (FIXME?) */
15189 break;
34eaf542
TT
15190 case DW_TAG_template_type_param:
15191 suppress_add = 1;
15192 /* Fall through. */
c906108c 15193 case DW_TAG_class_type:
680b30c7 15194 case DW_TAG_interface_type:
c906108c
SS
15195 case DW_TAG_structure_type:
15196 case DW_TAG_union_type:
72019c9c 15197 case DW_TAG_set_type:
c906108c
SS
15198 case DW_TAG_enumeration_type:
15199 SYMBOL_CLASS (sym) = LOC_TYPEDEF;
176620f1 15200 SYMBOL_DOMAIN (sym) = STRUCT_DOMAIN;
c906108c 15201
63d06c5c 15202 {
987504bb 15203 /* NOTE: carlton/2003-11-10: C++ and Java class symbols shouldn't
63d06c5c
DC
15204 really ever be static objects: otherwise, if you try
15205 to, say, break of a class's method and you're in a file
15206 which doesn't mention that class, it won't work unless
15207 the check for all static symbols in lookup_symbol_aux
15208 saves you. See the OtherFileClass tests in
15209 gdb.c++/namespace.exp. */
15210
e37fd15a 15211 if (!suppress_add)
34eaf542 15212 {
34eaf542
TT
15213 list_to_add = (cu->list_in_scope == &file_symbols
15214 && (cu->language == language_cplus
15215 || cu->language == language_java)
15216 ? &global_symbols : cu->list_in_scope);
63d06c5c 15217
64382290
TT
15218 /* The semantics of C++ state that "struct foo {
15219 ... }" also defines a typedef for "foo". A Java
15220 class declaration also defines a typedef for the
15221 class. */
15222 if (cu->language == language_cplus
15223 || cu->language == language_java
15224 || cu->language == language_ada)
15225 {
15226 /* The symbol's name is already allocated along
15227 with this objfile, so we don't need to
15228 duplicate it for the type. */
15229 if (TYPE_NAME (SYMBOL_TYPE (sym)) == 0)
15230 TYPE_NAME (SYMBOL_TYPE (sym)) = SYMBOL_SEARCH_NAME (sym);
15231 }
63d06c5c
DC
15232 }
15233 }
c906108c
SS
15234 break;
15235 case DW_TAG_typedef:
63d06c5c
DC
15236 SYMBOL_CLASS (sym) = LOC_TYPEDEF;
15237 SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
e37fd15a 15238 list_to_add = cu->list_in_scope;
63d06c5c 15239 break;
c906108c 15240 case DW_TAG_base_type:
a02abb62 15241 case DW_TAG_subrange_type:
c906108c 15242 SYMBOL_CLASS (sym) = LOC_TYPEDEF;
176620f1 15243 SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
e37fd15a 15244 list_to_add = cu->list_in_scope;
c906108c
SS
15245 break;
15246 case DW_TAG_enumerator:
e142c38c 15247 attr = dwarf2_attr (die, DW_AT_const_value, cu);
c906108c
SS
15248 if (attr)
15249 {
e7c27a73 15250 dwarf2_const_value (attr, sym, cu);
c906108c 15251 }
63d06c5c
DC
15252 {
15253 /* NOTE: carlton/2003-11-10: See comment above in the
15254 DW_TAG_class_type, etc. block. */
15255
e142c38c 15256 list_to_add = (cu->list_in_scope == &file_symbols
987504bb
JJ
15257 && (cu->language == language_cplus
15258 || cu->language == language_java)
e142c38c 15259 ? &global_symbols : cu->list_in_scope);
63d06c5c 15260 }
c906108c 15261 break;
5c4e30ca
DC
15262 case DW_TAG_namespace:
15263 SYMBOL_CLASS (sym) = LOC_TYPEDEF;
e37fd15a 15264 list_to_add = &global_symbols;
5c4e30ca 15265 break;
4357ac6c
TT
15266 case DW_TAG_common_block:
15267 SYMBOL_CLASS (sym) = LOC_STATIC;
15268 SYMBOL_DOMAIN (sym) = COMMON_BLOCK_DOMAIN;
15269 add_symbol_to_list (sym, cu->list_in_scope);
15270 break;
c906108c
SS
15271 default:
15272 /* Not a tag we recognize. Hopefully we aren't processing
15273 trash data, but since we must specifically ignore things
15274 we don't recognize, there is nothing else we should do at
0963b4bd 15275 this point. */
e2e0b3e5 15276 complaint (&symfile_complaints, _("unsupported tag: '%s'"),
4d3c2250 15277 dwarf_tag_name (die->tag));
c906108c
SS
15278 break;
15279 }
df8a16a1 15280
e37fd15a
SW
15281 if (suppress_add)
15282 {
15283 sym->hash_next = objfile->template_symbols;
15284 objfile->template_symbols = sym;
15285 list_to_add = NULL;
15286 }
15287
15288 if (list_to_add != NULL)
15289 add_symbol_to_list (sym, list_to_add);
15290
df8a16a1
DJ
15291 /* For the benefit of old versions of GCC, check for anonymous
15292 namespaces based on the demangled name. */
15293 if (!processing_has_namespace_info
94af9270 15294 && cu->language == language_cplus)
a10964d1 15295 cp_scan_for_anonymous_namespaces (sym, objfile);
c906108c
SS
15296 }
15297 return (sym);
15298}
15299
34eaf542
TT
15300/* A wrapper for new_symbol_full that always allocates a new symbol. */
15301
15302static struct symbol *
15303new_symbol (struct die_info *die, struct type *type, struct dwarf2_cu *cu)
15304{
15305 return new_symbol_full (die, type, cu, NULL);
15306}
15307
98bfdba5
PA
15308/* Given an attr with a DW_FORM_dataN value in host byte order,
15309 zero-extend it as appropriate for the symbol's type. The DWARF
15310 standard (v4) is not entirely clear about the meaning of using
15311 DW_FORM_dataN for a constant with a signed type, where the type is
15312 wider than the data. The conclusion of a discussion on the DWARF
15313 list was that this is unspecified. We choose to always zero-extend
15314 because that is the interpretation long in use by GCC. */
c906108c 15315
98bfdba5
PA
15316static gdb_byte *
15317dwarf2_const_value_data (struct attribute *attr, struct type *type,
15318 const char *name, struct obstack *obstack,
12df843f 15319 struct dwarf2_cu *cu, LONGEST *value, int bits)
c906108c 15320{
e7c27a73 15321 struct objfile *objfile = cu->objfile;
e17a4113
UW
15322 enum bfd_endian byte_order = bfd_big_endian (objfile->obfd) ?
15323 BFD_ENDIAN_BIG : BFD_ENDIAN_LITTLE;
98bfdba5
PA
15324 LONGEST l = DW_UNSND (attr);
15325
15326 if (bits < sizeof (*value) * 8)
15327 {
15328 l &= ((LONGEST) 1 << bits) - 1;
15329 *value = l;
15330 }
15331 else if (bits == sizeof (*value) * 8)
15332 *value = l;
15333 else
15334 {
15335 gdb_byte *bytes = obstack_alloc (obstack, bits / 8);
15336 store_unsigned_integer (bytes, bits / 8, byte_order, l);
15337 return bytes;
15338 }
15339
15340 return NULL;
15341}
15342
15343/* Read a constant value from an attribute. Either set *VALUE, or if
15344 the value does not fit in *VALUE, set *BYTES - either already
15345 allocated on the objfile obstack, or newly allocated on OBSTACK,
15346 or, set *BATON, if we translated the constant to a location
15347 expression. */
15348
15349static void
15350dwarf2_const_value_attr (struct attribute *attr, struct type *type,
15351 const char *name, struct obstack *obstack,
15352 struct dwarf2_cu *cu,
12df843f 15353 LONGEST *value, gdb_byte **bytes,
98bfdba5
PA
15354 struct dwarf2_locexpr_baton **baton)
15355{
15356 struct objfile *objfile = cu->objfile;
15357 struct comp_unit_head *cu_header = &cu->header;
c906108c 15358 struct dwarf_block *blk;
98bfdba5
PA
15359 enum bfd_endian byte_order = (bfd_big_endian (objfile->obfd) ?
15360 BFD_ENDIAN_BIG : BFD_ENDIAN_LITTLE);
15361
15362 *value = 0;
15363 *bytes = NULL;
15364 *baton = NULL;
c906108c
SS
15365
15366 switch (attr->form)
15367 {
15368 case DW_FORM_addr:
3019eac3 15369 case DW_FORM_GNU_addr_index:
ac56253d 15370 {
ac56253d
TT
15371 gdb_byte *data;
15372
98bfdba5
PA
15373 if (TYPE_LENGTH (type) != cu_header->addr_size)
15374 dwarf2_const_value_length_mismatch_complaint (name,
ac56253d 15375 cu_header->addr_size,
98bfdba5 15376 TYPE_LENGTH (type));
ac56253d
TT
15377 /* Symbols of this form are reasonably rare, so we just
15378 piggyback on the existing location code rather than writing
15379 a new implementation of symbol_computed_ops. */
98bfdba5
PA
15380 *baton = obstack_alloc (&objfile->objfile_obstack,
15381 sizeof (struct dwarf2_locexpr_baton));
15382 (*baton)->per_cu = cu->per_cu;
15383 gdb_assert ((*baton)->per_cu);
ac56253d 15384
98bfdba5
PA
15385 (*baton)->size = 2 + cu_header->addr_size;
15386 data = obstack_alloc (&objfile->objfile_obstack, (*baton)->size);
15387 (*baton)->data = data;
ac56253d
TT
15388
15389 data[0] = DW_OP_addr;
15390 store_unsigned_integer (&data[1], cu_header->addr_size,
15391 byte_order, DW_ADDR (attr));
15392 data[cu_header->addr_size + 1] = DW_OP_stack_value;
ac56253d 15393 }
c906108c 15394 break;
4ac36638 15395 case DW_FORM_string:
93b5768b 15396 case DW_FORM_strp:
3019eac3 15397 case DW_FORM_GNU_str_index:
36586728 15398 case DW_FORM_GNU_strp_alt:
98bfdba5
PA
15399 /* DW_STRING is already allocated on the objfile obstack, point
15400 directly to it. */
15401 *bytes = (gdb_byte *) DW_STRING (attr);
93b5768b 15402 break;
c906108c
SS
15403 case DW_FORM_block1:
15404 case DW_FORM_block2:
15405 case DW_FORM_block4:
15406 case DW_FORM_block:
2dc7f7b3 15407 case DW_FORM_exprloc:
c906108c 15408 blk = DW_BLOCK (attr);
98bfdba5
PA
15409 if (TYPE_LENGTH (type) != blk->size)
15410 dwarf2_const_value_length_mismatch_complaint (name, blk->size,
15411 TYPE_LENGTH (type));
15412 *bytes = blk->data;
c906108c 15413 break;
2df3850c
JM
15414
15415 /* The DW_AT_const_value attributes are supposed to carry the
15416 symbol's value "represented as it would be on the target
15417 architecture." By the time we get here, it's already been
15418 converted to host endianness, so we just need to sign- or
15419 zero-extend it as appropriate. */
15420 case DW_FORM_data1:
3e43a32a
MS
15421 *bytes = dwarf2_const_value_data (attr, type, name,
15422 obstack, cu, value, 8);
2df3850c 15423 break;
c906108c 15424 case DW_FORM_data2:
3e43a32a
MS
15425 *bytes = dwarf2_const_value_data (attr, type, name,
15426 obstack, cu, value, 16);
2df3850c 15427 break;
c906108c 15428 case DW_FORM_data4:
3e43a32a
MS
15429 *bytes = dwarf2_const_value_data (attr, type, name,
15430 obstack, cu, value, 32);
2df3850c 15431 break;
c906108c 15432 case DW_FORM_data8:
3e43a32a
MS
15433 *bytes = dwarf2_const_value_data (attr, type, name,
15434 obstack, cu, value, 64);
2df3850c
JM
15435 break;
15436
c906108c 15437 case DW_FORM_sdata:
98bfdba5 15438 *value = DW_SND (attr);
2df3850c
JM
15439 break;
15440
c906108c 15441 case DW_FORM_udata:
98bfdba5 15442 *value = DW_UNSND (attr);
c906108c 15443 break;
2df3850c 15444
c906108c 15445 default:
4d3c2250 15446 complaint (&symfile_complaints,
e2e0b3e5 15447 _("unsupported const value attribute form: '%s'"),
4d3c2250 15448 dwarf_form_name (attr->form));
98bfdba5 15449 *value = 0;
c906108c
SS
15450 break;
15451 }
15452}
15453
2df3850c 15454
98bfdba5
PA
15455/* Copy constant value from an attribute to a symbol. */
15456
2df3850c 15457static void
98bfdba5
PA
15458dwarf2_const_value (struct attribute *attr, struct symbol *sym,
15459 struct dwarf2_cu *cu)
2df3850c 15460{
98bfdba5
PA
15461 struct objfile *objfile = cu->objfile;
15462 struct comp_unit_head *cu_header = &cu->header;
12df843f 15463 LONGEST value;
98bfdba5
PA
15464 gdb_byte *bytes;
15465 struct dwarf2_locexpr_baton *baton;
2df3850c 15466
98bfdba5
PA
15467 dwarf2_const_value_attr (attr, SYMBOL_TYPE (sym),
15468 SYMBOL_PRINT_NAME (sym),
15469 &objfile->objfile_obstack, cu,
15470 &value, &bytes, &baton);
2df3850c 15471
98bfdba5
PA
15472 if (baton != NULL)
15473 {
15474 SYMBOL_COMPUTED_OPS (sym) = &dwarf2_locexpr_funcs;
15475 SYMBOL_LOCATION_BATON (sym) = baton;
15476 SYMBOL_CLASS (sym) = LOC_COMPUTED;
15477 }
15478 else if (bytes != NULL)
15479 {
15480 SYMBOL_VALUE_BYTES (sym) = bytes;
15481 SYMBOL_CLASS (sym) = LOC_CONST_BYTES;
15482 }
15483 else
15484 {
15485 SYMBOL_VALUE (sym) = value;
15486 SYMBOL_CLASS (sym) = LOC_CONST;
15487 }
2df3850c
JM
15488}
15489
c906108c
SS
15490/* Return the type of the die in question using its DW_AT_type attribute. */
15491
15492static struct type *
e7c27a73 15493die_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 15494{
c906108c 15495 struct attribute *type_attr;
c906108c 15496
e142c38c 15497 type_attr = dwarf2_attr (die, DW_AT_type, cu);
c906108c
SS
15498 if (!type_attr)
15499 {
15500 /* A missing DW_AT_type represents a void type. */
46bf5051 15501 return objfile_type (cu->objfile)->builtin_void;
c906108c 15502 }
348e048f 15503
673bfd45 15504 return lookup_die_type (die, type_attr, cu);
c906108c
SS
15505}
15506
b4ba55a1
JB
15507/* True iff CU's producer generates GNAT Ada auxiliary information
15508 that allows to find parallel types through that information instead
15509 of having to do expensive parallel lookups by type name. */
15510
15511static int
15512need_gnat_info (struct dwarf2_cu *cu)
15513{
15514 /* FIXME: brobecker/2010-10-12: As of now, only the AdaCore version
15515 of GNAT produces this auxiliary information, without any indication
15516 that it is produced. Part of enhancing the FSF version of GNAT
15517 to produce that information will be to put in place an indicator
15518 that we can use in order to determine whether the descriptive type
15519 info is available or not. One suggestion that has been made is
15520 to use a new attribute, attached to the CU die. For now, assume
15521 that the descriptive type info is not available. */
15522 return 0;
15523}
15524
b4ba55a1
JB
15525/* Return the auxiliary type of the die in question using its
15526 DW_AT_GNAT_descriptive_type attribute. Returns NULL if the
15527 attribute is not present. */
15528
15529static struct type *
15530die_descriptive_type (struct die_info *die, struct dwarf2_cu *cu)
15531{
b4ba55a1 15532 struct attribute *type_attr;
b4ba55a1
JB
15533
15534 type_attr = dwarf2_attr (die, DW_AT_GNAT_descriptive_type, cu);
15535 if (!type_attr)
15536 return NULL;
15537
673bfd45 15538 return lookup_die_type (die, type_attr, cu);
b4ba55a1
JB
15539}
15540
15541/* If DIE has a descriptive_type attribute, then set the TYPE's
15542 descriptive type accordingly. */
15543
15544static void
15545set_descriptive_type (struct type *type, struct die_info *die,
15546 struct dwarf2_cu *cu)
15547{
15548 struct type *descriptive_type = die_descriptive_type (die, cu);
15549
15550 if (descriptive_type)
15551 {
15552 ALLOCATE_GNAT_AUX_TYPE (type);
15553 TYPE_DESCRIPTIVE_TYPE (type) = descriptive_type;
15554 }
15555}
15556
c906108c
SS
15557/* Return the containing type of the die in question using its
15558 DW_AT_containing_type attribute. */
15559
15560static struct type *
e7c27a73 15561die_containing_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 15562{
c906108c 15563 struct attribute *type_attr;
c906108c 15564
e142c38c 15565 type_attr = dwarf2_attr (die, DW_AT_containing_type, cu);
33ac96f0
JK
15566 if (!type_attr)
15567 error (_("Dwarf Error: Problem turning containing type into gdb type "
15568 "[in module %s]"), cu->objfile->name);
15569
673bfd45 15570 return lookup_die_type (die, type_attr, cu);
c906108c
SS
15571}
15572
673bfd45
DE
15573/* Look up the type of DIE in CU using its type attribute ATTR.
15574 If there is no type substitute an error marker. */
15575
c906108c 15576static struct type *
673bfd45
DE
15577lookup_die_type (struct die_info *die, struct attribute *attr,
15578 struct dwarf2_cu *cu)
c906108c 15579{
bb5ed363 15580 struct objfile *objfile = cu->objfile;
f792889a
DJ
15581 struct type *this_type;
15582
673bfd45
DE
15583 /* First see if we have it cached. */
15584
36586728
TT
15585 if (attr->form == DW_FORM_GNU_ref_alt)
15586 {
15587 struct dwarf2_per_cu_data *per_cu;
15588 sect_offset offset = dwarf2_get_ref_die_offset (attr);
15589
15590 per_cu = dwarf2_find_containing_comp_unit (offset, 1, cu->objfile);
15591 this_type = get_die_type_at_offset (offset, per_cu);
15592 }
15593 else if (is_ref_attr (attr))
673bfd45 15594 {
b64f50a1 15595 sect_offset offset = dwarf2_get_ref_die_offset (attr);
673bfd45
DE
15596
15597 this_type = get_die_type_at_offset (offset, cu->per_cu);
15598 }
55f1336d 15599 else if (attr->form == DW_FORM_ref_sig8)
673bfd45
DE
15600 {
15601 struct signatured_type *sig_type = DW_SIGNATURED_TYPE (attr);
673bfd45
DE
15602
15603 /* sig_type will be NULL if the signatured type is missing from
15604 the debug info. */
15605 if (sig_type == NULL)
15606 error (_("Dwarf Error: Cannot find signatured DIE referenced from DIE "
15607 "at 0x%x [in module %s]"),
b64f50a1 15608 die->offset.sect_off, objfile->name);
673bfd45 15609
3019eac3
DE
15610 gdb_assert (sig_type->per_cu.is_debug_types);
15611 /* If we haven't filled in type_offset_in_section yet, then we
15612 haven't read the type in yet. */
15613 this_type = NULL;
15614 if (sig_type->type_offset_in_section.sect_off != 0)
15615 {
15616 this_type =
15617 get_die_type_at_offset (sig_type->type_offset_in_section,
15618 &sig_type->per_cu);
15619 }
673bfd45
DE
15620 }
15621 else
15622 {
15623 dump_die_for_error (die);
15624 error (_("Dwarf Error: Bad type attribute %s [in module %s]"),
bb5ed363 15625 dwarf_attr_name (attr->name), objfile->name);
673bfd45
DE
15626 }
15627
15628 /* If not cached we need to read it in. */
15629
15630 if (this_type == NULL)
15631 {
15632 struct die_info *type_die;
15633 struct dwarf2_cu *type_cu = cu;
15634
15635 type_die = follow_die_ref_or_sig (die, attr, &type_cu);
3019eac3
DE
15636 /* If we found the type now, it's probably because the type came
15637 from an inter-CU reference and the type's CU got expanded before
15638 ours. */
15639 this_type = get_die_type (type_die, type_cu);
15640 if (this_type == NULL)
15641 this_type = read_type_die_1 (type_die, type_cu);
673bfd45
DE
15642 }
15643
15644 /* If we still don't have a type use an error marker. */
15645
15646 if (this_type == NULL)
c906108c 15647 {
b00fdb78
TT
15648 char *message, *saved;
15649
15650 /* read_type_die already issued a complaint. */
15651 message = xstrprintf (_("<unknown type in %s, CU 0x%x, DIE 0x%x>"),
bb5ed363 15652 objfile->name,
b64f50a1
JK
15653 cu->header.offset.sect_off,
15654 die->offset.sect_off);
bb5ed363 15655 saved = obstack_copy0 (&objfile->objfile_obstack,
b00fdb78
TT
15656 message, strlen (message));
15657 xfree (message);
15658
bb5ed363 15659 this_type = init_type (TYPE_CODE_ERROR, 0, 0, saved, objfile);
c906108c 15660 }
673bfd45 15661
f792889a 15662 return this_type;
c906108c
SS
15663}
15664
673bfd45
DE
15665/* Return the type in DIE, CU.
15666 Returns NULL for invalid types.
15667
15668 This first does a lookup in the appropriate type_hash table,
15669 and only reads the die in if necessary.
15670
15671 NOTE: This can be called when reading in partial or full symbols. */
15672
f792889a 15673static struct type *
e7c27a73 15674read_type_die (struct die_info *die, struct dwarf2_cu *cu)
c906108c 15675{
f792889a
DJ
15676 struct type *this_type;
15677
15678 this_type = get_die_type (die, cu);
15679 if (this_type)
15680 return this_type;
15681
673bfd45
DE
15682 return read_type_die_1 (die, cu);
15683}
15684
15685/* Read the type in DIE, CU.
15686 Returns NULL for invalid types. */
15687
15688static struct type *
15689read_type_die_1 (struct die_info *die, struct dwarf2_cu *cu)
15690{
15691 struct type *this_type = NULL;
15692
c906108c
SS
15693 switch (die->tag)
15694 {
15695 case DW_TAG_class_type:
680b30c7 15696 case DW_TAG_interface_type:
c906108c
SS
15697 case DW_TAG_structure_type:
15698 case DW_TAG_union_type:
f792889a 15699 this_type = read_structure_type (die, cu);
c906108c
SS
15700 break;
15701 case DW_TAG_enumeration_type:
f792889a 15702 this_type = read_enumeration_type (die, cu);
c906108c
SS
15703 break;
15704 case DW_TAG_subprogram:
15705 case DW_TAG_subroutine_type:
edb3359d 15706 case DW_TAG_inlined_subroutine:
f792889a 15707 this_type = read_subroutine_type (die, cu);
c906108c
SS
15708 break;
15709 case DW_TAG_array_type:
f792889a 15710 this_type = read_array_type (die, cu);
c906108c 15711 break;
72019c9c 15712 case DW_TAG_set_type:
f792889a 15713 this_type = read_set_type (die, cu);
72019c9c 15714 break;
c906108c 15715 case DW_TAG_pointer_type:
f792889a 15716 this_type = read_tag_pointer_type (die, cu);
c906108c
SS
15717 break;
15718 case DW_TAG_ptr_to_member_type:
f792889a 15719 this_type = read_tag_ptr_to_member_type (die, cu);
c906108c
SS
15720 break;
15721 case DW_TAG_reference_type:
f792889a 15722 this_type = read_tag_reference_type (die, cu);
c906108c
SS
15723 break;
15724 case DW_TAG_const_type:
f792889a 15725 this_type = read_tag_const_type (die, cu);
c906108c
SS
15726 break;
15727 case DW_TAG_volatile_type:
f792889a 15728 this_type = read_tag_volatile_type (die, cu);
c906108c
SS
15729 break;
15730 case DW_TAG_string_type:
f792889a 15731 this_type = read_tag_string_type (die, cu);
c906108c
SS
15732 break;
15733 case DW_TAG_typedef:
f792889a 15734 this_type = read_typedef (die, cu);
c906108c 15735 break;
a02abb62 15736 case DW_TAG_subrange_type:
f792889a 15737 this_type = read_subrange_type (die, cu);
a02abb62 15738 break;
c906108c 15739 case DW_TAG_base_type:
f792889a 15740 this_type = read_base_type (die, cu);
c906108c 15741 break;
81a17f79 15742 case DW_TAG_unspecified_type:
f792889a 15743 this_type = read_unspecified_type (die, cu);
81a17f79 15744 break;
0114d602
DJ
15745 case DW_TAG_namespace:
15746 this_type = read_namespace_type (die, cu);
15747 break;
f55ee35c
JK
15748 case DW_TAG_module:
15749 this_type = read_module_type (die, cu);
15750 break;
c906108c 15751 default:
3e43a32a
MS
15752 complaint (&symfile_complaints,
15753 _("unexpected tag in read_type_die: '%s'"),
4d3c2250 15754 dwarf_tag_name (die->tag));
c906108c
SS
15755 break;
15756 }
63d06c5c 15757
f792889a 15758 return this_type;
63d06c5c
DC
15759}
15760
abc72ce4
DE
15761/* See if we can figure out if the class lives in a namespace. We do
15762 this by looking for a member function; its demangled name will
15763 contain namespace info, if there is any.
15764 Return the computed name or NULL.
15765 Space for the result is allocated on the objfile's obstack.
15766 This is the full-die version of guess_partial_die_structure_name.
15767 In this case we know DIE has no useful parent. */
15768
15769static char *
15770guess_full_die_structure_name (struct die_info *die, struct dwarf2_cu *cu)
15771{
15772 struct die_info *spec_die;
15773 struct dwarf2_cu *spec_cu;
15774 struct die_info *child;
15775
15776 spec_cu = cu;
15777 spec_die = die_specification (die, &spec_cu);
15778 if (spec_die != NULL)
15779 {
15780 die = spec_die;
15781 cu = spec_cu;
15782 }
15783
15784 for (child = die->child;
15785 child != NULL;
15786 child = child->sibling)
15787 {
15788 if (child->tag == DW_TAG_subprogram)
15789 {
15790 struct attribute *attr;
15791
15792 attr = dwarf2_attr (child, DW_AT_linkage_name, cu);
15793 if (attr == NULL)
15794 attr = dwarf2_attr (child, DW_AT_MIPS_linkage_name, cu);
15795 if (attr != NULL)
15796 {
15797 char *actual_name
15798 = language_class_name_from_physname (cu->language_defn,
15799 DW_STRING (attr));
15800 char *name = NULL;
15801
15802 if (actual_name != NULL)
15803 {
15804 char *die_name = dwarf2_name (die, cu);
15805
15806 if (die_name != NULL
15807 && strcmp (die_name, actual_name) != 0)
15808 {
15809 /* Strip off the class name from the full name.
15810 We want the prefix. */
15811 int die_name_len = strlen (die_name);
15812 int actual_name_len = strlen (actual_name);
15813
15814 /* Test for '::' as a sanity check. */
15815 if (actual_name_len > die_name_len + 2
3e43a32a
MS
15816 && actual_name[actual_name_len
15817 - die_name_len - 1] == ':')
abc72ce4
DE
15818 name =
15819 obsavestring (actual_name,
15820 actual_name_len - die_name_len - 2,
15821 &cu->objfile->objfile_obstack);
15822 }
15823 }
15824 xfree (actual_name);
15825 return name;
15826 }
15827 }
15828 }
15829
15830 return NULL;
15831}
15832
96408a79
SA
15833/* GCC might emit a nameless typedef that has a linkage name. Determine the
15834 prefix part in such case. See
15835 http://gcc.gnu.org/bugzilla/show_bug.cgi?id=47510. */
15836
15837static char *
15838anonymous_struct_prefix (struct die_info *die, struct dwarf2_cu *cu)
15839{
15840 struct attribute *attr;
15841 char *base;
15842
15843 if (die->tag != DW_TAG_class_type && die->tag != DW_TAG_interface_type
15844 && die->tag != DW_TAG_structure_type && die->tag != DW_TAG_union_type)
15845 return NULL;
15846
15847 attr = dwarf2_attr (die, DW_AT_name, cu);
15848 if (attr != NULL && DW_STRING (attr) != NULL)
15849 return NULL;
15850
15851 attr = dwarf2_attr (die, DW_AT_linkage_name, cu);
15852 if (attr == NULL)
15853 attr = dwarf2_attr (die, DW_AT_MIPS_linkage_name, cu);
15854 if (attr == NULL || DW_STRING (attr) == NULL)
15855 return NULL;
15856
15857 /* dwarf2_name had to be already called. */
15858 gdb_assert (DW_STRING_IS_CANONICAL (attr));
15859
15860 /* Strip the base name, keep any leading namespaces/classes. */
15861 base = strrchr (DW_STRING (attr), ':');
15862 if (base == NULL || base == DW_STRING (attr) || base[-1] != ':')
15863 return "";
15864
15865 return obsavestring (DW_STRING (attr), &base[-1] - DW_STRING (attr),
15866 &cu->objfile->objfile_obstack);
15867}
15868
fdde2d81 15869/* Return the name of the namespace/class that DIE is defined within,
0114d602 15870 or "" if we can't tell. The caller should not xfree the result.
fdde2d81 15871
0114d602
DJ
15872 For example, if we're within the method foo() in the following
15873 code:
15874
15875 namespace N {
15876 class C {
15877 void foo () {
15878 }
15879 };
15880 }
15881
15882 then determine_prefix on foo's die will return "N::C". */
fdde2d81 15883
0d5cff50 15884static const char *
e142c38c 15885determine_prefix (struct die_info *die, struct dwarf2_cu *cu)
63d06c5c 15886{
0114d602
DJ
15887 struct die_info *parent, *spec_die;
15888 struct dwarf2_cu *spec_cu;
15889 struct type *parent_type;
96408a79 15890 char *retval;
63d06c5c 15891
f55ee35c
JK
15892 if (cu->language != language_cplus && cu->language != language_java
15893 && cu->language != language_fortran)
0114d602
DJ
15894 return "";
15895
96408a79
SA
15896 retval = anonymous_struct_prefix (die, cu);
15897 if (retval)
15898 return retval;
15899
0114d602
DJ
15900 /* We have to be careful in the presence of DW_AT_specification.
15901 For example, with GCC 3.4, given the code
15902
15903 namespace N {
15904 void foo() {
15905 // Definition of N::foo.
15906 }
15907 }
15908
15909 then we'll have a tree of DIEs like this:
15910
15911 1: DW_TAG_compile_unit
15912 2: DW_TAG_namespace // N
15913 3: DW_TAG_subprogram // declaration of N::foo
15914 4: DW_TAG_subprogram // definition of N::foo
15915 DW_AT_specification // refers to die #3
15916
15917 Thus, when processing die #4, we have to pretend that we're in
15918 the context of its DW_AT_specification, namely the contex of die
15919 #3. */
15920 spec_cu = cu;
15921 spec_die = die_specification (die, &spec_cu);
15922 if (spec_die == NULL)
15923 parent = die->parent;
15924 else
63d06c5c 15925 {
0114d602
DJ
15926 parent = spec_die->parent;
15927 cu = spec_cu;
63d06c5c 15928 }
0114d602
DJ
15929
15930 if (parent == NULL)
15931 return "";
98bfdba5
PA
15932 else if (parent->building_fullname)
15933 {
15934 const char *name;
15935 const char *parent_name;
15936
15937 /* It has been seen on RealView 2.2 built binaries,
15938 DW_TAG_template_type_param types actually _defined_ as
15939 children of the parent class:
15940
15941 enum E {};
15942 template class <class Enum> Class{};
15943 Class<enum E> class_e;
15944
15945 1: DW_TAG_class_type (Class)
15946 2: DW_TAG_enumeration_type (E)
15947 3: DW_TAG_enumerator (enum1:0)
15948 3: DW_TAG_enumerator (enum2:1)
15949 ...
15950 2: DW_TAG_template_type_param
15951 DW_AT_type DW_FORM_ref_udata (E)
15952
15953 Besides being broken debug info, it can put GDB into an
15954 infinite loop. Consider:
15955
15956 When we're building the full name for Class<E>, we'll start
15957 at Class, and go look over its template type parameters,
15958 finding E. We'll then try to build the full name of E, and
15959 reach here. We're now trying to build the full name of E,
15960 and look over the parent DIE for containing scope. In the
15961 broken case, if we followed the parent DIE of E, we'd again
15962 find Class, and once again go look at its template type
15963 arguments, etc., etc. Simply don't consider such parent die
15964 as source-level parent of this die (it can't be, the language
15965 doesn't allow it), and break the loop here. */
15966 name = dwarf2_name (die, cu);
15967 parent_name = dwarf2_name (parent, cu);
15968 complaint (&symfile_complaints,
15969 _("template param type '%s' defined within parent '%s'"),
15970 name ? name : "<unknown>",
15971 parent_name ? parent_name : "<unknown>");
15972 return "";
15973 }
63d06c5c 15974 else
0114d602
DJ
15975 switch (parent->tag)
15976 {
63d06c5c 15977 case DW_TAG_namespace:
0114d602 15978 parent_type = read_type_die (parent, cu);
acebe513
UW
15979 /* GCC 4.0 and 4.1 had a bug (PR c++/28460) where they generated bogus
15980 DW_TAG_namespace DIEs with a name of "::" for the global namespace.
15981 Work around this problem here. */
15982 if (cu->language == language_cplus
15983 && strcmp (TYPE_TAG_NAME (parent_type), "::") == 0)
15984 return "";
0114d602
DJ
15985 /* We give a name to even anonymous namespaces. */
15986 return TYPE_TAG_NAME (parent_type);
63d06c5c 15987 case DW_TAG_class_type:
680b30c7 15988 case DW_TAG_interface_type:
63d06c5c 15989 case DW_TAG_structure_type:
0114d602 15990 case DW_TAG_union_type:
f55ee35c 15991 case DW_TAG_module:
0114d602
DJ
15992 parent_type = read_type_die (parent, cu);
15993 if (TYPE_TAG_NAME (parent_type) != NULL)
15994 return TYPE_TAG_NAME (parent_type);
15995 else
15996 /* An anonymous structure is only allowed non-static data
15997 members; no typedefs, no member functions, et cetera.
15998 So it does not need a prefix. */
15999 return "";
abc72ce4 16000 case DW_TAG_compile_unit:
95554aad 16001 case DW_TAG_partial_unit:
abc72ce4
DE
16002 /* gcc-4.5 -gdwarf-4 can drop the enclosing namespace. Cope. */
16003 if (cu->language == language_cplus
8b70b953 16004 && !VEC_empty (dwarf2_section_info_def, dwarf2_per_objfile->types)
abc72ce4
DE
16005 && die->child != NULL
16006 && (die->tag == DW_TAG_class_type
16007 || die->tag == DW_TAG_structure_type
16008 || die->tag == DW_TAG_union_type))
16009 {
16010 char *name = guess_full_die_structure_name (die, cu);
16011 if (name != NULL)
16012 return name;
16013 }
16014 return "";
63d06c5c 16015 default:
8176b9b8 16016 return determine_prefix (parent, cu);
63d06c5c 16017 }
63d06c5c
DC
16018}
16019
3e43a32a
MS
16020/* Return a newly-allocated string formed by concatenating PREFIX and SUFFIX
16021 with appropriate separator. If PREFIX or SUFFIX is NULL or empty, then
16022 simply copy the SUFFIX or PREFIX, respectively. If OBS is non-null, perform
16023 an obconcat, otherwise allocate storage for the result. The CU argument is
16024 used to determine the language and hence, the appropriate separator. */
987504bb 16025
f55ee35c 16026#define MAX_SEP_LEN 7 /* strlen ("__") + strlen ("_MOD_") */
63d06c5c
DC
16027
16028static char *
f55ee35c
JK
16029typename_concat (struct obstack *obs, const char *prefix, const char *suffix,
16030 int physname, struct dwarf2_cu *cu)
63d06c5c 16031{
f55ee35c 16032 const char *lead = "";
5c315b68 16033 const char *sep;
63d06c5c 16034
3e43a32a
MS
16035 if (suffix == NULL || suffix[0] == '\0'
16036 || prefix == NULL || prefix[0] == '\0')
987504bb
JJ
16037 sep = "";
16038 else if (cu->language == language_java)
16039 sep = ".";
f55ee35c
JK
16040 else if (cu->language == language_fortran && physname)
16041 {
16042 /* This is gfortran specific mangling. Normally DW_AT_linkage_name or
16043 DW_AT_MIPS_linkage_name is preferred and used instead. */
16044
16045 lead = "__";
16046 sep = "_MOD_";
16047 }
987504bb
JJ
16048 else
16049 sep = "::";
63d06c5c 16050
6dd47d34
DE
16051 if (prefix == NULL)
16052 prefix = "";
16053 if (suffix == NULL)
16054 suffix = "";
16055
987504bb
JJ
16056 if (obs == NULL)
16057 {
3e43a32a
MS
16058 char *retval
16059 = xmalloc (strlen (prefix) + MAX_SEP_LEN + strlen (suffix) + 1);
9a619af0 16060
f55ee35c
JK
16061 strcpy (retval, lead);
16062 strcat (retval, prefix);
6dd47d34
DE
16063 strcat (retval, sep);
16064 strcat (retval, suffix);
63d06c5c
DC
16065 return retval;
16066 }
987504bb
JJ
16067 else
16068 {
16069 /* We have an obstack. */
f55ee35c 16070 return obconcat (obs, lead, prefix, sep, suffix, (char *) NULL);
987504bb 16071 }
63d06c5c
DC
16072}
16073
c906108c
SS
16074/* Return sibling of die, NULL if no sibling. */
16075
f9aca02d 16076static struct die_info *
fba45db2 16077sibling_die (struct die_info *die)
c906108c 16078{
639d11d3 16079 return die->sibling;
c906108c
SS
16080}
16081
71c25dea
TT
16082/* Get name of a die, return NULL if not found. */
16083
16084static char *
16085dwarf2_canonicalize_name (char *name, struct dwarf2_cu *cu,
16086 struct obstack *obstack)
16087{
16088 if (name && cu->language == language_cplus)
16089 {
16090 char *canon_name = cp_canonicalize_string (name);
16091
16092 if (canon_name != NULL)
16093 {
16094 if (strcmp (canon_name, name) != 0)
16095 name = obsavestring (canon_name, strlen (canon_name),
16096 obstack);
16097 xfree (canon_name);
16098 }
16099 }
16100
16101 return name;
c906108c
SS
16102}
16103
9219021c
DC
16104/* Get name of a die, return NULL if not found. */
16105
16106static char *
e142c38c 16107dwarf2_name (struct die_info *die, struct dwarf2_cu *cu)
9219021c
DC
16108{
16109 struct attribute *attr;
16110
e142c38c 16111 attr = dwarf2_attr (die, DW_AT_name, cu);
53832f31
TT
16112 if ((!attr || !DW_STRING (attr))
16113 && die->tag != DW_TAG_class_type
16114 && die->tag != DW_TAG_interface_type
16115 && die->tag != DW_TAG_structure_type
16116 && die->tag != DW_TAG_union_type)
71c25dea
TT
16117 return NULL;
16118
16119 switch (die->tag)
16120 {
16121 case DW_TAG_compile_unit:
95554aad 16122 case DW_TAG_partial_unit:
71c25dea
TT
16123 /* Compilation units have a DW_AT_name that is a filename, not
16124 a source language identifier. */
16125 case DW_TAG_enumeration_type:
16126 case DW_TAG_enumerator:
16127 /* These tags always have simple identifiers already; no need
16128 to canonicalize them. */
16129 return DW_STRING (attr);
907af001 16130
418835cc
KS
16131 case DW_TAG_subprogram:
16132 /* Java constructors will all be named "<init>", so return
16133 the class name when we see this special case. */
16134 if (cu->language == language_java
16135 && DW_STRING (attr) != NULL
16136 && strcmp (DW_STRING (attr), "<init>") == 0)
16137 {
16138 struct dwarf2_cu *spec_cu = cu;
16139 struct die_info *spec_die;
16140
16141 /* GCJ will output '<init>' for Java constructor names.
16142 For this special case, return the name of the parent class. */
16143
16144 /* GCJ may output suprogram DIEs with AT_specification set.
16145 If so, use the name of the specified DIE. */
16146 spec_die = die_specification (die, &spec_cu);
16147 if (spec_die != NULL)
16148 return dwarf2_name (spec_die, spec_cu);
16149
16150 do
16151 {
16152 die = die->parent;
16153 if (die->tag == DW_TAG_class_type)
16154 return dwarf2_name (die, cu);
16155 }
95554aad
TT
16156 while (die->tag != DW_TAG_compile_unit
16157 && die->tag != DW_TAG_partial_unit);
418835cc 16158 }
907af001
UW
16159 break;
16160
16161 case DW_TAG_class_type:
16162 case DW_TAG_interface_type:
16163 case DW_TAG_structure_type:
16164 case DW_TAG_union_type:
16165 /* Some GCC versions emit spurious DW_AT_name attributes for unnamed
16166 structures or unions. These were of the form "._%d" in GCC 4.1,
16167 or simply "<anonymous struct>" or "<anonymous union>" in GCC 4.3
16168 and GCC 4.4. We work around this problem by ignoring these. */
53832f31
TT
16169 if (attr && DW_STRING (attr)
16170 && (strncmp (DW_STRING (attr), "._", 2) == 0
16171 || strncmp (DW_STRING (attr), "<anonymous", 10) == 0))
907af001 16172 return NULL;
53832f31
TT
16173
16174 /* GCC might emit a nameless typedef that has a linkage name. See
16175 http://gcc.gnu.org/bugzilla/show_bug.cgi?id=47510. */
16176 if (!attr || DW_STRING (attr) == NULL)
16177 {
df5c6c50 16178 char *demangled = NULL;
53832f31
TT
16179
16180 attr = dwarf2_attr (die, DW_AT_linkage_name, cu);
16181 if (attr == NULL)
16182 attr = dwarf2_attr (die, DW_AT_MIPS_linkage_name, cu);
16183
16184 if (attr == NULL || DW_STRING (attr) == NULL)
16185 return NULL;
16186
df5c6c50
JK
16187 /* Avoid demangling DW_STRING (attr) the second time on a second
16188 call for the same DIE. */
16189 if (!DW_STRING_IS_CANONICAL (attr))
16190 demangled = cplus_demangle (DW_STRING (attr), DMGL_TYPES);
53832f31
TT
16191
16192 if (demangled)
16193 {
96408a79
SA
16194 char *base;
16195
53832f31 16196 /* FIXME: we already did this for the partial symbol... */
96408a79
SA
16197 DW_STRING (attr) = obsavestring (demangled, strlen (demangled),
16198 &cu->objfile->objfile_obstack);
53832f31
TT
16199 DW_STRING_IS_CANONICAL (attr) = 1;
16200 xfree (demangled);
96408a79
SA
16201
16202 /* Strip any leading namespaces/classes, keep only the base name.
16203 DW_AT_name for named DIEs does not contain the prefixes. */
16204 base = strrchr (DW_STRING (attr), ':');
16205 if (base && base > DW_STRING (attr) && base[-1] == ':')
16206 return &base[1];
16207 else
16208 return DW_STRING (attr);
53832f31
TT
16209 }
16210 }
907af001
UW
16211 break;
16212
71c25dea 16213 default:
907af001
UW
16214 break;
16215 }
16216
16217 if (!DW_STRING_IS_CANONICAL (attr))
16218 {
16219 DW_STRING (attr)
16220 = dwarf2_canonicalize_name (DW_STRING (attr), cu,
16221 &cu->objfile->objfile_obstack);
16222 DW_STRING_IS_CANONICAL (attr) = 1;
71c25dea 16223 }
907af001 16224 return DW_STRING (attr);
9219021c
DC
16225}
16226
16227/* Return the die that this die in an extension of, or NULL if there
f2f0e013
DJ
16228 is none. *EXT_CU is the CU containing DIE on input, and the CU
16229 containing the return value on output. */
9219021c
DC
16230
16231static struct die_info *
f2f0e013 16232dwarf2_extension (struct die_info *die, struct dwarf2_cu **ext_cu)
9219021c
DC
16233{
16234 struct attribute *attr;
9219021c 16235
f2f0e013 16236 attr = dwarf2_attr (die, DW_AT_extension, *ext_cu);
9219021c
DC
16237 if (attr == NULL)
16238 return NULL;
16239
f2f0e013 16240 return follow_die_ref (die, attr, ext_cu);
9219021c
DC
16241}
16242
c906108c
SS
16243/* Convert a DIE tag into its string name. */
16244
f39c6ffd 16245static const char *
aa1ee363 16246dwarf_tag_name (unsigned tag)
c906108c 16247{
f39c6ffd
TT
16248 const char *name = get_DW_TAG_name (tag);
16249
16250 if (name == NULL)
16251 return "DW_TAG_<unknown>";
16252
16253 return name;
c906108c
SS
16254}
16255
16256/* Convert a DWARF attribute code into its string name. */
16257
f39c6ffd 16258static const char *
aa1ee363 16259dwarf_attr_name (unsigned attr)
c906108c 16260{
f39c6ffd
TT
16261 const char *name;
16262
c764a876 16263#ifdef MIPS /* collides with DW_AT_HP_block_index */
f39c6ffd
TT
16264 if (attr == DW_AT_MIPS_fde)
16265 return "DW_AT_MIPS_fde";
16266#else
16267 if (attr == DW_AT_HP_block_index)
16268 return "DW_AT_HP_block_index";
c764a876 16269#endif
f39c6ffd
TT
16270
16271 name = get_DW_AT_name (attr);
16272
16273 if (name == NULL)
16274 return "DW_AT_<unknown>";
16275
16276 return name;
c906108c
SS
16277}
16278
16279/* Convert a DWARF value form code into its string name. */
16280
f39c6ffd 16281static const char *
aa1ee363 16282dwarf_form_name (unsigned form)
c906108c 16283{
f39c6ffd
TT
16284 const char *name = get_DW_FORM_name (form);
16285
16286 if (name == NULL)
16287 return "DW_FORM_<unknown>";
16288
16289 return name;
c906108c
SS
16290}
16291
16292static char *
fba45db2 16293dwarf_bool_name (unsigned mybool)
c906108c
SS
16294{
16295 if (mybool)
16296 return "TRUE";
16297 else
16298 return "FALSE";
16299}
16300
16301/* Convert a DWARF type code into its string name. */
16302
f39c6ffd 16303static const char *
aa1ee363 16304dwarf_type_encoding_name (unsigned enc)
c906108c 16305{
f39c6ffd 16306 const char *name = get_DW_ATE_name (enc);
c906108c 16307
f39c6ffd
TT
16308 if (name == NULL)
16309 return "DW_ATE_<unknown>";
c906108c 16310
f39c6ffd 16311 return name;
c906108c 16312}
c906108c 16313
f9aca02d 16314static void
d97bc12b 16315dump_die_shallow (struct ui_file *f, int indent, struct die_info *die)
c906108c
SS
16316{
16317 unsigned int i;
16318
d97bc12b
DE
16319 print_spaces (indent, f);
16320 fprintf_unfiltered (f, "Die: %s (abbrev %d, offset 0x%x)\n",
b64f50a1 16321 dwarf_tag_name (die->tag), die->abbrev, die->offset.sect_off);
d97bc12b
DE
16322
16323 if (die->parent != NULL)
16324 {
16325 print_spaces (indent, f);
16326 fprintf_unfiltered (f, " parent at offset: 0x%x\n",
b64f50a1 16327 die->parent->offset.sect_off);
d97bc12b
DE
16328 }
16329
16330 print_spaces (indent, f);
16331 fprintf_unfiltered (f, " has children: %s\n",
639d11d3 16332 dwarf_bool_name (die->child != NULL));
c906108c 16333
d97bc12b
DE
16334 print_spaces (indent, f);
16335 fprintf_unfiltered (f, " attributes:\n");
16336
c906108c
SS
16337 for (i = 0; i < die->num_attrs; ++i)
16338 {
d97bc12b
DE
16339 print_spaces (indent, f);
16340 fprintf_unfiltered (f, " %s (%s) ",
c906108c
SS
16341 dwarf_attr_name (die->attrs[i].name),
16342 dwarf_form_name (die->attrs[i].form));
d97bc12b 16343
c906108c
SS
16344 switch (die->attrs[i].form)
16345 {
c906108c 16346 case DW_FORM_addr:
3019eac3 16347 case DW_FORM_GNU_addr_index:
d97bc12b 16348 fprintf_unfiltered (f, "address: ");
5af949e3 16349 fputs_filtered (hex_string (DW_ADDR (&die->attrs[i])), f);
c906108c
SS
16350 break;
16351 case DW_FORM_block2:
16352 case DW_FORM_block4:
16353 case DW_FORM_block:
16354 case DW_FORM_block1:
56eb65bd
SP
16355 fprintf_unfiltered (f, "block: size %s",
16356 pulongest (DW_BLOCK (&die->attrs[i])->size));
c906108c 16357 break;
2dc7f7b3 16358 case DW_FORM_exprloc:
56eb65bd
SP
16359 fprintf_unfiltered (f, "expression: size %s",
16360 pulongest (DW_BLOCK (&die->attrs[i])->size));
2dc7f7b3 16361 break;
4568ecf9
DE
16362 case DW_FORM_ref_addr:
16363 fprintf_unfiltered (f, "ref address: ");
16364 fputs_filtered (hex_string (DW_UNSND (&die->attrs[i])), f);
16365 break;
36586728
TT
16366 case DW_FORM_GNU_ref_alt:
16367 fprintf_unfiltered (f, "alt ref address: ");
16368 fputs_filtered (hex_string (DW_UNSND (&die->attrs[i])), f);
16369 break;
10b3939b
DJ
16370 case DW_FORM_ref1:
16371 case DW_FORM_ref2:
16372 case DW_FORM_ref4:
4568ecf9
DE
16373 case DW_FORM_ref8:
16374 case DW_FORM_ref_udata:
d97bc12b 16375 fprintf_unfiltered (f, "constant ref: 0x%lx (adjusted)",
4568ecf9 16376 (long) (DW_UNSND (&die->attrs[i])));
10b3939b 16377 break;
c906108c
SS
16378 case DW_FORM_data1:
16379 case DW_FORM_data2:
16380 case DW_FORM_data4:
ce5d95e1 16381 case DW_FORM_data8:
c906108c
SS
16382 case DW_FORM_udata:
16383 case DW_FORM_sdata:
43bbcdc2
PH
16384 fprintf_unfiltered (f, "constant: %s",
16385 pulongest (DW_UNSND (&die->attrs[i])));
c906108c 16386 break;
2dc7f7b3
TT
16387 case DW_FORM_sec_offset:
16388 fprintf_unfiltered (f, "section offset: %s",
16389 pulongest (DW_UNSND (&die->attrs[i])));
16390 break;
55f1336d 16391 case DW_FORM_ref_sig8:
348e048f
DE
16392 if (DW_SIGNATURED_TYPE (&die->attrs[i]) != NULL)
16393 fprintf_unfiltered (f, "signatured type, offset: 0x%x",
b64f50a1 16394 DW_SIGNATURED_TYPE (&die->attrs[i])->per_cu.offset.sect_off);
348e048f
DE
16395 else
16396 fprintf_unfiltered (f, "signatured type, offset: unknown");
16397 break;
c906108c 16398 case DW_FORM_string:
4bdf3d34 16399 case DW_FORM_strp:
3019eac3 16400 case DW_FORM_GNU_str_index:
36586728 16401 case DW_FORM_GNU_strp_alt:
8285870a 16402 fprintf_unfiltered (f, "string: \"%s\" (%s canonicalized)",
c906108c 16403 DW_STRING (&die->attrs[i])
8285870a
JK
16404 ? DW_STRING (&die->attrs[i]) : "",
16405 DW_STRING_IS_CANONICAL (&die->attrs[i]) ? "is" : "not");
c906108c
SS
16406 break;
16407 case DW_FORM_flag:
16408 if (DW_UNSND (&die->attrs[i]))
d97bc12b 16409 fprintf_unfiltered (f, "flag: TRUE");
c906108c 16410 else
d97bc12b 16411 fprintf_unfiltered (f, "flag: FALSE");
c906108c 16412 break;
2dc7f7b3
TT
16413 case DW_FORM_flag_present:
16414 fprintf_unfiltered (f, "flag: TRUE");
16415 break;
a8329558 16416 case DW_FORM_indirect:
0963b4bd
MS
16417 /* The reader will have reduced the indirect form to
16418 the "base form" so this form should not occur. */
3e43a32a
MS
16419 fprintf_unfiltered (f,
16420 "unexpected attribute form: DW_FORM_indirect");
a8329558 16421 break;
c906108c 16422 default:
d97bc12b 16423 fprintf_unfiltered (f, "unsupported attribute form: %d.",
c5aa993b 16424 die->attrs[i].form);
d97bc12b 16425 break;
c906108c 16426 }
d97bc12b 16427 fprintf_unfiltered (f, "\n");
c906108c
SS
16428 }
16429}
16430
f9aca02d 16431static void
d97bc12b 16432dump_die_for_error (struct die_info *die)
c906108c 16433{
d97bc12b
DE
16434 dump_die_shallow (gdb_stderr, 0, die);
16435}
16436
16437static void
16438dump_die_1 (struct ui_file *f, int level, int max_level, struct die_info *die)
16439{
16440 int indent = level * 4;
16441
16442 gdb_assert (die != NULL);
16443
16444 if (level >= max_level)
16445 return;
16446
16447 dump_die_shallow (f, indent, die);
16448
16449 if (die->child != NULL)
c906108c 16450 {
d97bc12b
DE
16451 print_spaces (indent, f);
16452 fprintf_unfiltered (f, " Children:");
16453 if (level + 1 < max_level)
16454 {
16455 fprintf_unfiltered (f, "\n");
16456 dump_die_1 (f, level + 1, max_level, die->child);
16457 }
16458 else
16459 {
3e43a32a
MS
16460 fprintf_unfiltered (f,
16461 " [not printed, max nesting level reached]\n");
d97bc12b
DE
16462 }
16463 }
16464
16465 if (die->sibling != NULL && level > 0)
16466 {
16467 dump_die_1 (f, level, max_level, die->sibling);
c906108c
SS
16468 }
16469}
16470
d97bc12b
DE
16471/* This is called from the pdie macro in gdbinit.in.
16472 It's not static so gcc will keep a copy callable from gdb. */
16473
16474void
16475dump_die (struct die_info *die, int max_level)
16476{
16477 dump_die_1 (gdb_stdlog, 0, max_level, die);
16478}
16479
f9aca02d 16480static void
51545339 16481store_in_ref_table (struct die_info *die, struct dwarf2_cu *cu)
c906108c 16482{
51545339 16483 void **slot;
c906108c 16484
b64f50a1
JK
16485 slot = htab_find_slot_with_hash (cu->die_hash, die, die->offset.sect_off,
16486 INSERT);
51545339
DJ
16487
16488 *slot = die;
c906108c
SS
16489}
16490
b64f50a1
JK
16491/* DW_ADDR is always stored already as sect_offset; despite for the forms
16492 besides DW_FORM_ref_addr it is stored as cu_offset in the DWARF file. */
16493
93311388
DE
16494static int
16495is_ref_attr (struct attribute *attr)
c906108c 16496{
c906108c
SS
16497 switch (attr->form)
16498 {
16499 case DW_FORM_ref_addr:
c906108c
SS
16500 case DW_FORM_ref1:
16501 case DW_FORM_ref2:
16502 case DW_FORM_ref4:
613e1657 16503 case DW_FORM_ref8:
c906108c 16504 case DW_FORM_ref_udata:
36586728 16505 case DW_FORM_GNU_ref_alt:
93311388 16506 return 1;
c906108c 16507 default:
93311388 16508 return 0;
c906108c 16509 }
93311388
DE
16510}
16511
b64f50a1
JK
16512/* Return DIE offset of ATTR. Return 0 with complaint if ATTR is not of the
16513 required kind. */
16514
16515static sect_offset
93311388
DE
16516dwarf2_get_ref_die_offset (struct attribute *attr)
16517{
4568ecf9 16518 sect_offset retval = { DW_UNSND (attr) };
b64f50a1 16519
93311388 16520 if (is_ref_attr (attr))
b64f50a1 16521 return retval;
93311388 16522
b64f50a1 16523 retval.sect_off = 0;
93311388
DE
16524 complaint (&symfile_complaints,
16525 _("unsupported die ref attribute form: '%s'"),
16526 dwarf_form_name (attr->form));
b64f50a1 16527 return retval;
c906108c
SS
16528}
16529
43bbcdc2
PH
16530/* Return the constant value held by ATTR. Return DEFAULT_VALUE if
16531 * the value held by the attribute is not constant. */
a02abb62 16532
43bbcdc2 16533static LONGEST
a02abb62
JB
16534dwarf2_get_attr_constant_value (struct attribute *attr, int default_value)
16535{
16536 if (attr->form == DW_FORM_sdata)
16537 return DW_SND (attr);
16538 else if (attr->form == DW_FORM_udata
16539 || attr->form == DW_FORM_data1
16540 || attr->form == DW_FORM_data2
16541 || attr->form == DW_FORM_data4
16542 || attr->form == DW_FORM_data8)
16543 return DW_UNSND (attr);
16544 else
16545 {
3e43a32a
MS
16546 complaint (&symfile_complaints,
16547 _("Attribute value is not a constant (%s)"),
a02abb62
JB
16548 dwarf_form_name (attr->form));
16549 return default_value;
16550 }
16551}
16552
348e048f
DE
16553/* Follow reference or signature attribute ATTR of SRC_DIE.
16554 On entry *REF_CU is the CU of SRC_DIE.
16555 On exit *REF_CU is the CU of the result. */
16556
16557static struct die_info *
16558follow_die_ref_or_sig (struct die_info *src_die, struct attribute *attr,
16559 struct dwarf2_cu **ref_cu)
16560{
16561 struct die_info *die;
16562
16563 if (is_ref_attr (attr))
16564 die = follow_die_ref (src_die, attr, ref_cu);
55f1336d 16565 else if (attr->form == DW_FORM_ref_sig8)
348e048f
DE
16566 die = follow_die_sig (src_die, attr, ref_cu);
16567 else
16568 {
16569 dump_die_for_error (src_die);
16570 error (_("Dwarf Error: Expected reference attribute [in module %s]"),
16571 (*ref_cu)->objfile->name);
16572 }
16573
16574 return die;
03dd20cc
DJ
16575}
16576
5c631832 16577/* Follow reference OFFSET.
673bfd45
DE
16578 On entry *REF_CU is the CU of the source die referencing OFFSET.
16579 On exit *REF_CU is the CU of the result.
16580 Returns NULL if OFFSET is invalid. */
f504f079 16581
f9aca02d 16582static struct die_info *
36586728
TT
16583follow_die_offset (sect_offset offset, int offset_in_dwz,
16584 struct dwarf2_cu **ref_cu)
c906108c 16585{
10b3939b 16586 struct die_info temp_die;
f2f0e013 16587 struct dwarf2_cu *target_cu, *cu = *ref_cu;
10b3939b 16588
348e048f
DE
16589 gdb_assert (cu->per_cu != NULL);
16590
98bfdba5
PA
16591 target_cu = cu;
16592
3019eac3 16593 if (cu->per_cu->is_debug_types)
348e048f
DE
16594 {
16595 /* .debug_types CUs cannot reference anything outside their CU.
16596 If they need to, they have to reference a signatured type via
55f1336d 16597 DW_FORM_ref_sig8. */
348e048f 16598 if (! offset_in_cu_p (&cu->header, offset))
5c631832 16599 return NULL;
348e048f 16600 }
36586728
TT
16601 else if (offset_in_dwz != cu->per_cu->is_dwz
16602 || ! offset_in_cu_p (&cu->header, offset))
10b3939b
DJ
16603 {
16604 struct dwarf2_per_cu_data *per_cu;
9a619af0 16605
36586728
TT
16606 per_cu = dwarf2_find_containing_comp_unit (offset, offset_in_dwz,
16607 cu->objfile);
03dd20cc
DJ
16608
16609 /* If necessary, add it to the queue and load its DIEs. */
95554aad
TT
16610 if (maybe_queue_comp_unit (cu, per_cu, cu->language))
16611 load_full_comp_unit (per_cu, cu->language);
03dd20cc 16612
10b3939b
DJ
16613 target_cu = per_cu->cu;
16614 }
98bfdba5
PA
16615 else if (cu->dies == NULL)
16616 {
16617 /* We're loading full DIEs during partial symbol reading. */
16618 gdb_assert (dwarf2_per_objfile->reading_partial_symbols);
95554aad 16619 load_full_comp_unit (cu->per_cu, language_minimal);
98bfdba5 16620 }
c906108c 16621
f2f0e013 16622 *ref_cu = target_cu;
51545339 16623 temp_die.offset = offset;
b64f50a1 16624 return htab_find_with_hash (target_cu->die_hash, &temp_die, offset.sect_off);
5c631832 16625}
10b3939b 16626
5c631832
JK
16627/* Follow reference attribute ATTR of SRC_DIE.
16628 On entry *REF_CU is the CU of SRC_DIE.
16629 On exit *REF_CU is the CU of the result. */
16630
16631static struct die_info *
16632follow_die_ref (struct die_info *src_die, struct attribute *attr,
16633 struct dwarf2_cu **ref_cu)
16634{
b64f50a1 16635 sect_offset offset = dwarf2_get_ref_die_offset (attr);
5c631832
JK
16636 struct dwarf2_cu *cu = *ref_cu;
16637 struct die_info *die;
16638
36586728
TT
16639 die = follow_die_offset (offset,
16640 (attr->form == DW_FORM_GNU_ref_alt
16641 || cu->per_cu->is_dwz),
16642 ref_cu);
5c631832
JK
16643 if (!die)
16644 error (_("Dwarf Error: Cannot find DIE at 0x%x referenced from DIE "
16645 "at 0x%x [in module %s]"),
b64f50a1 16646 offset.sect_off, src_die->offset.sect_off, cu->objfile->name);
348e048f 16647
5c631832
JK
16648 return die;
16649}
16650
d83e736b
JK
16651/* Return DWARF block referenced by DW_AT_location of DIE at OFFSET at PER_CU.
16652 Returned value is intended for DW_OP_call*. Returned
16653 dwarf2_locexpr_baton->data has lifetime of PER_CU->OBJFILE. */
5c631832
JK
16654
16655struct dwarf2_locexpr_baton
b64f50a1 16656dwarf2_fetch_die_location_block (cu_offset offset_in_cu,
8cf6f0b1
TT
16657 struct dwarf2_per_cu_data *per_cu,
16658 CORE_ADDR (*get_frame_pc) (void *baton),
16659 void *baton)
5c631832 16660{
b64f50a1 16661 sect_offset offset = { per_cu->offset.sect_off + offset_in_cu.cu_off };
918dd910 16662 struct dwarf2_cu *cu;
5c631832
JK
16663 struct die_info *die;
16664 struct attribute *attr;
16665 struct dwarf2_locexpr_baton retval;
16666
8cf6f0b1
TT
16667 dw2_setup (per_cu->objfile);
16668
918dd910
JK
16669 if (per_cu->cu == NULL)
16670 load_cu (per_cu);
16671 cu = per_cu->cu;
16672
36586728 16673 die = follow_die_offset (offset, per_cu->is_dwz, &cu);
5c631832
JK
16674 if (!die)
16675 error (_("Dwarf Error: Cannot find DIE at 0x%x referenced in module %s"),
b64f50a1 16676 offset.sect_off, per_cu->objfile->name);
5c631832
JK
16677
16678 attr = dwarf2_attr (die, DW_AT_location, cu);
16679 if (!attr)
16680 {
e103e986
JK
16681 /* DWARF: "If there is no such attribute, then there is no effect.".
16682 DATA is ignored if SIZE is 0. */
5c631832 16683
e103e986 16684 retval.data = NULL;
5c631832
JK
16685 retval.size = 0;
16686 }
8cf6f0b1
TT
16687 else if (attr_form_is_section_offset (attr))
16688 {
16689 struct dwarf2_loclist_baton loclist_baton;
16690 CORE_ADDR pc = (*get_frame_pc) (baton);
16691 size_t size;
16692
16693 fill_in_loclist_baton (cu, &loclist_baton, attr);
16694
16695 retval.data = dwarf2_find_location_expression (&loclist_baton,
16696 &size, pc);
16697 retval.size = size;
16698 }
5c631832
JK
16699 else
16700 {
16701 if (!attr_form_is_block (attr))
16702 error (_("Dwarf Error: DIE at 0x%x referenced in module %s "
16703 "is neither DW_FORM_block* nor DW_FORM_exprloc"),
b64f50a1 16704 offset.sect_off, per_cu->objfile->name);
5c631832
JK
16705
16706 retval.data = DW_BLOCK (attr)->data;
16707 retval.size = DW_BLOCK (attr)->size;
16708 }
16709 retval.per_cu = cu->per_cu;
918dd910 16710
918dd910
JK
16711 age_cached_comp_units ();
16712
5c631832 16713 return retval;
348e048f
DE
16714}
16715
8a9b8146
TT
16716/* Return the type of the DIE at DIE_OFFSET in the CU named by
16717 PER_CU. */
16718
16719struct type *
b64f50a1 16720dwarf2_get_die_type (cu_offset die_offset,
8a9b8146
TT
16721 struct dwarf2_per_cu_data *per_cu)
16722{
b64f50a1
JK
16723 sect_offset die_offset_sect;
16724
8a9b8146 16725 dw2_setup (per_cu->objfile);
b64f50a1
JK
16726
16727 die_offset_sect.sect_off = per_cu->offset.sect_off + die_offset.cu_off;
16728 return get_die_type_at_offset (die_offset_sect, per_cu);
8a9b8146
TT
16729}
16730
348e048f
DE
16731/* Follow the signature attribute ATTR in SRC_DIE.
16732 On entry *REF_CU is the CU of SRC_DIE.
16733 On exit *REF_CU is the CU of the result. */
16734
16735static struct die_info *
16736follow_die_sig (struct die_info *src_die, struct attribute *attr,
16737 struct dwarf2_cu **ref_cu)
16738{
16739 struct objfile *objfile = (*ref_cu)->objfile;
16740 struct die_info temp_die;
16741 struct signatured_type *sig_type = DW_SIGNATURED_TYPE (attr);
16742 struct dwarf2_cu *sig_cu;
16743 struct die_info *die;
16744
16745 /* sig_type will be NULL if the signatured type is missing from
16746 the debug info. */
16747 if (sig_type == NULL)
16748 error (_("Dwarf Error: Cannot find signatured DIE referenced from DIE "
16749 "at 0x%x [in module %s]"),
b64f50a1 16750 src_die->offset.sect_off, objfile->name);
348e048f
DE
16751
16752 /* If necessary, add it to the queue and load its DIEs. */
16753
95554aad 16754 if (maybe_queue_comp_unit (*ref_cu, &sig_type->per_cu, language_minimal))
a0f42c21 16755 read_signatured_type (sig_type);
348e048f
DE
16756
16757 gdb_assert (sig_type->per_cu.cu != NULL);
16758
16759 sig_cu = sig_type->per_cu.cu;
3019eac3
DE
16760 gdb_assert (sig_type->type_offset_in_section.sect_off != 0);
16761 temp_die.offset = sig_type->type_offset_in_section;
b64f50a1
JK
16762 die = htab_find_with_hash (sig_cu->die_hash, &temp_die,
16763 temp_die.offset.sect_off);
348e048f
DE
16764 if (die)
16765 {
16766 *ref_cu = sig_cu;
16767 return die;
16768 }
16769
3e43a32a
MS
16770 error (_("Dwarf Error: Cannot find signatured DIE at 0x%x referenced "
16771 "from DIE at 0x%x [in module %s]"),
b64f50a1 16772 temp_die.offset.sect_off, src_die->offset.sect_off, objfile->name);
348e048f
DE
16773}
16774
16775/* Given an offset of a signatured type, return its signatured_type. */
16776
16777static struct signatured_type *
8b70b953
TT
16778lookup_signatured_type_at_offset (struct objfile *objfile,
16779 struct dwarf2_section_info *section,
b64f50a1 16780 sect_offset offset)
348e048f 16781{
b64f50a1 16782 gdb_byte *info_ptr = section->buffer + offset.sect_off;
348e048f
DE
16783 unsigned int length, initial_length_size;
16784 unsigned int sig_offset;
52dc124a 16785 struct signatured_type find_entry, *sig_type;
348e048f
DE
16786
16787 length = read_initial_length (objfile->obfd, info_ptr, &initial_length_size);
16788 sig_offset = (initial_length_size
16789 + 2 /*version*/
16790 + (initial_length_size == 4 ? 4 : 8) /*debug_abbrev_offset*/
16791 + 1 /*address_size*/);
16792 find_entry.signature = bfd_get_64 (objfile->obfd, info_ptr + sig_offset);
52dc124a 16793 sig_type = htab_find (dwarf2_per_objfile->signatured_types, &find_entry);
348e048f
DE
16794
16795 /* This is only used to lookup previously recorded types.
16796 If we didn't find it, it's our bug. */
52dc124a
DE
16797 gdb_assert (sig_type != NULL);
16798 gdb_assert (offset.sect_off == sig_type->per_cu.offset.sect_off);
348e048f 16799
52dc124a 16800 return sig_type;
348e048f
DE
16801}
16802
e5fe5e75 16803/* Load the DIEs associated with type unit PER_CU into memory. */
348e048f
DE
16804
16805static void
e5fe5e75 16806load_full_type_unit (struct dwarf2_per_cu_data *per_cu)
348e048f 16807{
52dc124a 16808 struct signatured_type *sig_type;
348e048f 16809
f4dc4d17
DE
16810 /* Caller is responsible for ensuring type_unit_groups don't get here. */
16811 gdb_assert (! IS_TYPE_UNIT_GROUP (per_cu));
16812
6721b2ec
DE
16813 /* We have the per_cu, but we need the signatured_type.
16814 Fortunately this is an easy translation. */
16815 gdb_assert (per_cu->is_debug_types);
16816 sig_type = (struct signatured_type *) per_cu;
348e048f 16817
6721b2ec 16818 gdb_assert (per_cu->cu == NULL);
348e048f 16819
52dc124a 16820 read_signatured_type (sig_type);
348e048f 16821
6721b2ec 16822 gdb_assert (per_cu->cu != NULL);
348e048f
DE
16823}
16824
dee91e82
DE
16825/* die_reader_func for read_signatured_type.
16826 This is identical to load_full_comp_unit_reader,
16827 but is kept separate for now. */
348e048f
DE
16828
16829static void
dee91e82
DE
16830read_signatured_type_reader (const struct die_reader_specs *reader,
16831 gdb_byte *info_ptr,
16832 struct die_info *comp_unit_die,
16833 int has_children,
16834 void *data)
348e048f 16835{
dee91e82 16836 struct dwarf2_cu *cu = reader->cu;
348e048f 16837
dee91e82
DE
16838 gdb_assert (cu->die_hash == NULL);
16839 cu->die_hash =
16840 htab_create_alloc_ex (cu->header.length / 12,
16841 die_hash,
16842 die_eq,
16843 NULL,
16844 &cu->comp_unit_obstack,
16845 hashtab_obstack_allocate,
16846 dummy_obstack_deallocate);
348e048f 16847
dee91e82
DE
16848 if (has_children)
16849 comp_unit_die->child = read_die_and_siblings (reader, info_ptr,
16850 &info_ptr, comp_unit_die);
16851 cu->dies = comp_unit_die;
16852 /* comp_unit_die is not stored in die_hash, no need. */
348e048f
DE
16853
16854 /* We try not to read any attributes in this function, because not
9cdd5dbd 16855 all CUs needed for references have been loaded yet, and symbol
348e048f 16856 table processing isn't initialized. But we have to set the CU language,
dee91e82
DE
16857 or we won't be able to build types correctly.
16858 Similarly, if we do not read the producer, we can not apply
16859 producer-specific interpretation. */
95554aad 16860 prepare_one_comp_unit (cu, cu->dies, language_minimal);
dee91e82 16861}
348e048f 16862
3019eac3
DE
16863/* Read in a signatured type and build its CU and DIEs.
16864 If the type is a stub for the real type in a DWO file,
16865 read in the real type from the DWO file as well. */
dee91e82
DE
16866
16867static void
16868read_signatured_type (struct signatured_type *sig_type)
16869{
16870 struct dwarf2_per_cu_data *per_cu = &sig_type->per_cu;
348e048f 16871
3019eac3 16872 gdb_assert (per_cu->is_debug_types);
dee91e82 16873 gdb_assert (per_cu->cu == NULL);
348e048f 16874
f4dc4d17
DE
16875 init_cutu_and_read_dies (per_cu, NULL, 0, 1,
16876 read_signatured_type_reader, NULL);
c906108c
SS
16877}
16878
c906108c
SS
16879/* Decode simple location descriptions.
16880 Given a pointer to a dwarf block that defines a location, compute
16881 the location and return the value.
16882
4cecd739
DJ
16883 NOTE drow/2003-11-18: This function is called in two situations
16884 now: for the address of static or global variables (partial symbols
16885 only) and for offsets into structures which are expected to be
16886 (more or less) constant. The partial symbol case should go away,
16887 and only the constant case should remain. That will let this
16888 function complain more accurately. A few special modes are allowed
16889 without complaint for global variables (for instance, global
16890 register values and thread-local values).
c906108c
SS
16891
16892 A location description containing no operations indicates that the
4cecd739 16893 object is optimized out. The return value is 0 for that case.
6b992462
DJ
16894 FIXME drow/2003-11-16: No callers check for this case any more; soon all
16895 callers will only want a very basic result and this can become a
21ae7a4d
JK
16896 complaint.
16897
16898 Note that stack[0] is unused except as a default error return. */
c906108c
SS
16899
16900static CORE_ADDR
e7c27a73 16901decode_locdesc (struct dwarf_block *blk, struct dwarf2_cu *cu)
c906108c 16902{
e7c27a73 16903 struct objfile *objfile = cu->objfile;
56eb65bd
SP
16904 size_t i;
16905 size_t size = blk->size;
21ae7a4d
JK
16906 gdb_byte *data = blk->data;
16907 CORE_ADDR stack[64];
16908 int stacki;
16909 unsigned int bytes_read, unsnd;
16910 gdb_byte op;
c906108c 16911
21ae7a4d
JK
16912 i = 0;
16913 stacki = 0;
16914 stack[stacki] = 0;
16915 stack[++stacki] = 0;
16916
16917 while (i < size)
16918 {
16919 op = data[i++];
16920 switch (op)
16921 {
16922 case DW_OP_lit0:
16923 case DW_OP_lit1:
16924 case DW_OP_lit2:
16925 case DW_OP_lit3:
16926 case DW_OP_lit4:
16927 case DW_OP_lit5:
16928 case DW_OP_lit6:
16929 case DW_OP_lit7:
16930 case DW_OP_lit8:
16931 case DW_OP_lit9:
16932 case DW_OP_lit10:
16933 case DW_OP_lit11:
16934 case DW_OP_lit12:
16935 case DW_OP_lit13:
16936 case DW_OP_lit14:
16937 case DW_OP_lit15:
16938 case DW_OP_lit16:
16939 case DW_OP_lit17:
16940 case DW_OP_lit18:
16941 case DW_OP_lit19:
16942 case DW_OP_lit20:
16943 case DW_OP_lit21:
16944 case DW_OP_lit22:
16945 case DW_OP_lit23:
16946 case DW_OP_lit24:
16947 case DW_OP_lit25:
16948 case DW_OP_lit26:
16949 case DW_OP_lit27:
16950 case DW_OP_lit28:
16951 case DW_OP_lit29:
16952 case DW_OP_lit30:
16953 case DW_OP_lit31:
16954 stack[++stacki] = op - DW_OP_lit0;
16955 break;
f1bea926 16956
21ae7a4d
JK
16957 case DW_OP_reg0:
16958 case DW_OP_reg1:
16959 case DW_OP_reg2:
16960 case DW_OP_reg3:
16961 case DW_OP_reg4:
16962 case DW_OP_reg5:
16963 case DW_OP_reg6:
16964 case DW_OP_reg7:
16965 case DW_OP_reg8:
16966 case DW_OP_reg9:
16967 case DW_OP_reg10:
16968 case DW_OP_reg11:
16969 case DW_OP_reg12:
16970 case DW_OP_reg13:
16971 case DW_OP_reg14:
16972 case DW_OP_reg15:
16973 case DW_OP_reg16:
16974 case DW_OP_reg17:
16975 case DW_OP_reg18:
16976 case DW_OP_reg19:
16977 case DW_OP_reg20:
16978 case DW_OP_reg21:
16979 case DW_OP_reg22:
16980 case DW_OP_reg23:
16981 case DW_OP_reg24:
16982 case DW_OP_reg25:
16983 case DW_OP_reg26:
16984 case DW_OP_reg27:
16985 case DW_OP_reg28:
16986 case DW_OP_reg29:
16987 case DW_OP_reg30:
16988 case DW_OP_reg31:
16989 stack[++stacki] = op - DW_OP_reg0;
16990 if (i < size)
16991 dwarf2_complex_location_expr_complaint ();
16992 break;
c906108c 16993
21ae7a4d
JK
16994 case DW_OP_regx:
16995 unsnd = read_unsigned_leb128 (NULL, (data + i), &bytes_read);
16996 i += bytes_read;
16997 stack[++stacki] = unsnd;
16998 if (i < size)
16999 dwarf2_complex_location_expr_complaint ();
17000 break;
c906108c 17001
21ae7a4d
JK
17002 case DW_OP_addr:
17003 stack[++stacki] = read_address (objfile->obfd, &data[i],
17004 cu, &bytes_read);
17005 i += bytes_read;
17006 break;
d53d4ac5 17007
21ae7a4d
JK
17008 case DW_OP_const1u:
17009 stack[++stacki] = read_1_byte (objfile->obfd, &data[i]);
17010 i += 1;
17011 break;
17012
17013 case DW_OP_const1s:
17014 stack[++stacki] = read_1_signed_byte (objfile->obfd, &data[i]);
17015 i += 1;
17016 break;
17017
17018 case DW_OP_const2u:
17019 stack[++stacki] = read_2_bytes (objfile->obfd, &data[i]);
17020 i += 2;
17021 break;
17022
17023 case DW_OP_const2s:
17024 stack[++stacki] = read_2_signed_bytes (objfile->obfd, &data[i]);
17025 i += 2;
17026 break;
d53d4ac5 17027
21ae7a4d
JK
17028 case DW_OP_const4u:
17029 stack[++stacki] = read_4_bytes (objfile->obfd, &data[i]);
17030 i += 4;
17031 break;
17032
17033 case DW_OP_const4s:
17034 stack[++stacki] = read_4_signed_bytes (objfile->obfd, &data[i]);
17035 i += 4;
17036 break;
17037
585861ea
JK
17038 case DW_OP_const8u:
17039 stack[++stacki] = read_8_bytes (objfile->obfd, &data[i]);
17040 i += 8;
17041 break;
17042
21ae7a4d
JK
17043 case DW_OP_constu:
17044 stack[++stacki] = read_unsigned_leb128 (NULL, (data + i),
17045 &bytes_read);
17046 i += bytes_read;
17047 break;
17048
17049 case DW_OP_consts:
17050 stack[++stacki] = read_signed_leb128 (NULL, (data + i), &bytes_read);
17051 i += bytes_read;
17052 break;
17053
17054 case DW_OP_dup:
17055 stack[stacki + 1] = stack[stacki];
17056 stacki++;
17057 break;
17058
17059 case DW_OP_plus:
17060 stack[stacki - 1] += stack[stacki];
17061 stacki--;
17062 break;
17063
17064 case DW_OP_plus_uconst:
17065 stack[stacki] += read_unsigned_leb128 (NULL, (data + i),
17066 &bytes_read);
17067 i += bytes_read;
17068 break;
17069
17070 case DW_OP_minus:
17071 stack[stacki - 1] -= stack[stacki];
17072 stacki--;
17073 break;
17074
17075 case DW_OP_deref:
17076 /* If we're not the last op, then we definitely can't encode
17077 this using GDB's address_class enum. This is valid for partial
17078 global symbols, although the variable's address will be bogus
17079 in the psymtab. */
17080 if (i < size)
17081 dwarf2_complex_location_expr_complaint ();
17082 break;
17083
17084 case DW_OP_GNU_push_tls_address:
17085 /* The top of the stack has the offset from the beginning
17086 of the thread control block at which the variable is located. */
17087 /* Nothing should follow this operator, so the top of stack would
17088 be returned. */
17089 /* This is valid for partial global symbols, but the variable's
585861ea
JK
17090 address will be bogus in the psymtab. Make it always at least
17091 non-zero to not look as a variable garbage collected by linker
17092 which have DW_OP_addr 0. */
21ae7a4d
JK
17093 if (i < size)
17094 dwarf2_complex_location_expr_complaint ();
585861ea 17095 stack[stacki]++;
21ae7a4d
JK
17096 break;
17097
17098 case DW_OP_GNU_uninit:
17099 break;
17100
3019eac3 17101 case DW_OP_GNU_addr_index:
49f6c839 17102 case DW_OP_GNU_const_index:
3019eac3
DE
17103 stack[++stacki] = read_addr_index_from_leb128 (cu, &data[i],
17104 &bytes_read);
17105 i += bytes_read;
17106 break;
17107
21ae7a4d
JK
17108 default:
17109 {
f39c6ffd 17110 const char *name = get_DW_OP_name (op);
21ae7a4d
JK
17111
17112 if (name)
17113 complaint (&symfile_complaints, _("unsupported stack op: '%s'"),
17114 name);
17115 else
17116 complaint (&symfile_complaints, _("unsupported stack op: '%02x'"),
17117 op);
17118 }
17119
17120 return (stack[stacki]);
d53d4ac5 17121 }
3c6e0cb3 17122
21ae7a4d
JK
17123 /* Enforce maximum stack depth of SIZE-1 to avoid writing
17124 outside of the allocated space. Also enforce minimum>0. */
17125 if (stacki >= ARRAY_SIZE (stack) - 1)
17126 {
17127 complaint (&symfile_complaints,
17128 _("location description stack overflow"));
17129 return 0;
17130 }
17131
17132 if (stacki <= 0)
17133 {
17134 complaint (&symfile_complaints,
17135 _("location description stack underflow"));
17136 return 0;
17137 }
17138 }
17139 return (stack[stacki]);
c906108c
SS
17140}
17141
17142/* memory allocation interface */
17143
c906108c 17144static struct dwarf_block *
7b5a2f43 17145dwarf_alloc_block (struct dwarf2_cu *cu)
c906108c
SS
17146{
17147 struct dwarf_block *blk;
17148
17149 blk = (struct dwarf_block *)
7b5a2f43 17150 obstack_alloc (&cu->comp_unit_obstack, sizeof (struct dwarf_block));
c906108c
SS
17151 return (blk);
17152}
17153
c906108c 17154static struct die_info *
b60c80d6 17155dwarf_alloc_die (struct dwarf2_cu *cu, int num_attrs)
c906108c
SS
17156{
17157 struct die_info *die;
b60c80d6
DJ
17158 size_t size = sizeof (struct die_info);
17159
17160 if (num_attrs > 1)
17161 size += (num_attrs - 1) * sizeof (struct attribute);
c906108c 17162
b60c80d6 17163 die = (struct die_info *) obstack_alloc (&cu->comp_unit_obstack, size);
c906108c
SS
17164 memset (die, 0, sizeof (struct die_info));
17165 return (die);
17166}
2e276125
JB
17167
17168\f
17169/* Macro support. */
17170
2e276125
JB
17171/* Return the full name of file number I in *LH's file name table.
17172 Use COMP_DIR as the name of the current directory of the
17173 compilation. The result is allocated using xmalloc; the caller is
17174 responsible for freeing it. */
17175static char *
17176file_full_name (int file, struct line_header *lh, const char *comp_dir)
17177{
6a83a1e6
EZ
17178 /* Is the file number a valid index into the line header's file name
17179 table? Remember that file numbers start with one, not zero. */
17180 if (1 <= file && file <= lh->num_file_names)
17181 {
17182 struct file_entry *fe = &lh->file_names[file - 1];
6e70227d 17183
6a83a1e6
EZ
17184 if (IS_ABSOLUTE_PATH (fe->name))
17185 return xstrdup (fe->name);
17186 else
17187 {
17188 const char *dir;
17189 int dir_len;
17190 char *full_name;
17191
17192 if (fe->dir_index)
17193 dir = lh->include_dirs[fe->dir_index - 1];
17194 else
17195 dir = comp_dir;
17196
17197 if (dir)
17198 {
17199 dir_len = strlen (dir);
17200 full_name = xmalloc (dir_len + 1 + strlen (fe->name) + 1);
17201 strcpy (full_name, dir);
17202 full_name[dir_len] = '/';
17203 strcpy (full_name + dir_len + 1, fe->name);
17204 return full_name;
17205 }
17206 else
17207 return xstrdup (fe->name);
17208 }
17209 }
2e276125
JB
17210 else
17211 {
6a83a1e6
EZ
17212 /* The compiler produced a bogus file number. We can at least
17213 record the macro definitions made in the file, even if we
17214 won't be able to find the file by name. */
17215 char fake_name[80];
9a619af0 17216
6a83a1e6 17217 sprintf (fake_name, "<bad macro file number %d>", file);
2e276125 17218
6e70227d 17219 complaint (&symfile_complaints,
6a83a1e6
EZ
17220 _("bad file number in macro information (%d)"),
17221 file);
2e276125 17222
6a83a1e6 17223 return xstrdup (fake_name);
2e276125
JB
17224 }
17225}
17226
17227
17228static struct macro_source_file *
17229macro_start_file (int file, int line,
17230 struct macro_source_file *current_file,
17231 const char *comp_dir,
17232 struct line_header *lh, struct objfile *objfile)
17233{
17234 /* The full name of this source file. */
17235 char *full_name = file_full_name (file, lh, comp_dir);
17236
17237 /* We don't create a macro table for this compilation unit
17238 at all until we actually get a filename. */
17239 if (! pending_macros)
6532ff36
TT
17240 pending_macros = new_macro_table (&objfile->per_bfd->storage_obstack,
17241 objfile->per_bfd->macro_cache);
2e276125
JB
17242
17243 if (! current_file)
abc9d0dc
TT
17244 {
17245 /* If we have no current file, then this must be the start_file
17246 directive for the compilation unit's main source file. */
17247 current_file = macro_set_main (pending_macros, full_name);
17248 macro_define_special (pending_macros);
17249 }
2e276125
JB
17250 else
17251 current_file = macro_include (current_file, line, full_name);
17252
17253 xfree (full_name);
6e70227d 17254
2e276125
JB
17255 return current_file;
17256}
17257
17258
17259/* Copy the LEN characters at BUF to a xmalloc'ed block of memory,
17260 followed by a null byte. */
17261static char *
17262copy_string (const char *buf, int len)
17263{
17264 char *s = xmalloc (len + 1);
9a619af0 17265
2e276125
JB
17266 memcpy (s, buf, len);
17267 s[len] = '\0';
2e276125
JB
17268 return s;
17269}
17270
17271
17272static const char *
17273consume_improper_spaces (const char *p, const char *body)
17274{
17275 if (*p == ' ')
17276 {
4d3c2250 17277 complaint (&symfile_complaints,
3e43a32a
MS
17278 _("macro definition contains spaces "
17279 "in formal argument list:\n`%s'"),
4d3c2250 17280 body);
2e276125
JB
17281
17282 while (*p == ' ')
17283 p++;
17284 }
17285
17286 return p;
17287}
17288
17289
17290static void
17291parse_macro_definition (struct macro_source_file *file, int line,
17292 const char *body)
17293{
17294 const char *p;
17295
17296 /* The body string takes one of two forms. For object-like macro
17297 definitions, it should be:
17298
17299 <macro name> " " <definition>
17300
17301 For function-like macro definitions, it should be:
17302
17303 <macro name> "() " <definition>
17304 or
17305 <macro name> "(" <arg name> ( "," <arg name> ) * ") " <definition>
17306
17307 Spaces may appear only where explicitly indicated, and in the
17308 <definition>.
17309
17310 The Dwarf 2 spec says that an object-like macro's name is always
17311 followed by a space, but versions of GCC around March 2002 omit
6e70227d 17312 the space when the macro's definition is the empty string.
2e276125
JB
17313
17314 The Dwarf 2 spec says that there should be no spaces between the
17315 formal arguments in a function-like macro's formal argument list,
17316 but versions of GCC around March 2002 include spaces after the
17317 commas. */
17318
17319
17320 /* Find the extent of the macro name. The macro name is terminated
17321 by either a space or null character (for an object-like macro) or
17322 an opening paren (for a function-like macro). */
17323 for (p = body; *p; p++)
17324 if (*p == ' ' || *p == '(')
17325 break;
17326
17327 if (*p == ' ' || *p == '\0')
17328 {
17329 /* It's an object-like macro. */
17330 int name_len = p - body;
17331 char *name = copy_string (body, name_len);
17332 const char *replacement;
17333
17334 if (*p == ' ')
17335 replacement = body + name_len + 1;
17336 else
17337 {
4d3c2250 17338 dwarf2_macro_malformed_definition_complaint (body);
2e276125
JB
17339 replacement = body + name_len;
17340 }
6e70227d 17341
2e276125
JB
17342 macro_define_object (file, line, name, replacement);
17343
17344 xfree (name);
17345 }
17346 else if (*p == '(')
17347 {
17348 /* It's a function-like macro. */
17349 char *name = copy_string (body, p - body);
17350 int argc = 0;
17351 int argv_size = 1;
17352 char **argv = xmalloc (argv_size * sizeof (*argv));
17353
17354 p++;
17355
17356 p = consume_improper_spaces (p, body);
17357
17358 /* Parse the formal argument list. */
17359 while (*p && *p != ')')
17360 {
17361 /* Find the extent of the current argument name. */
17362 const char *arg_start = p;
17363
17364 while (*p && *p != ',' && *p != ')' && *p != ' ')
17365 p++;
17366
17367 if (! *p || p == arg_start)
4d3c2250 17368 dwarf2_macro_malformed_definition_complaint (body);
2e276125
JB
17369 else
17370 {
17371 /* Make sure argv has room for the new argument. */
17372 if (argc >= argv_size)
17373 {
17374 argv_size *= 2;
17375 argv = xrealloc (argv, argv_size * sizeof (*argv));
17376 }
17377
17378 argv[argc++] = copy_string (arg_start, p - arg_start);
17379 }
17380
17381 p = consume_improper_spaces (p, body);
17382
17383 /* Consume the comma, if present. */
17384 if (*p == ',')
17385 {
17386 p++;
17387
17388 p = consume_improper_spaces (p, body);
17389 }
17390 }
17391
17392 if (*p == ')')
17393 {
17394 p++;
17395
17396 if (*p == ' ')
17397 /* Perfectly formed definition, no complaints. */
17398 macro_define_function (file, line, name,
6e70227d 17399 argc, (const char **) argv,
2e276125
JB
17400 p + 1);
17401 else if (*p == '\0')
17402 {
17403 /* Complain, but do define it. */
4d3c2250 17404 dwarf2_macro_malformed_definition_complaint (body);
2e276125 17405 macro_define_function (file, line, name,
6e70227d 17406 argc, (const char **) argv,
2e276125
JB
17407 p);
17408 }
17409 else
17410 /* Just complain. */
4d3c2250 17411 dwarf2_macro_malformed_definition_complaint (body);
2e276125
JB
17412 }
17413 else
17414 /* Just complain. */
4d3c2250 17415 dwarf2_macro_malformed_definition_complaint (body);
2e276125
JB
17416
17417 xfree (name);
17418 {
17419 int i;
17420
17421 for (i = 0; i < argc; i++)
17422 xfree (argv[i]);
17423 }
17424 xfree (argv);
17425 }
17426 else
4d3c2250 17427 dwarf2_macro_malformed_definition_complaint (body);
2e276125
JB
17428}
17429
cf2c3c16
TT
17430/* Skip some bytes from BYTES according to the form given in FORM.
17431 Returns the new pointer. */
2e276125 17432
cf2c3c16 17433static gdb_byte *
f664829e 17434skip_form_bytes (bfd *abfd, gdb_byte *bytes, gdb_byte *buffer_end,
cf2c3c16
TT
17435 enum dwarf_form form,
17436 unsigned int offset_size,
17437 struct dwarf2_section_info *section)
2e276125 17438{
cf2c3c16 17439 unsigned int bytes_read;
2e276125 17440
cf2c3c16 17441 switch (form)
2e276125 17442 {
cf2c3c16
TT
17443 case DW_FORM_data1:
17444 case DW_FORM_flag:
17445 ++bytes;
17446 break;
17447
17448 case DW_FORM_data2:
17449 bytes += 2;
17450 break;
17451
17452 case DW_FORM_data4:
17453 bytes += 4;
17454 break;
17455
17456 case DW_FORM_data8:
17457 bytes += 8;
17458 break;
17459
17460 case DW_FORM_string:
17461 read_direct_string (abfd, bytes, &bytes_read);
17462 bytes += bytes_read;
17463 break;
17464
17465 case DW_FORM_sec_offset:
17466 case DW_FORM_strp:
36586728 17467 case DW_FORM_GNU_strp_alt:
cf2c3c16
TT
17468 bytes += offset_size;
17469 break;
17470
17471 case DW_FORM_block:
17472 bytes += read_unsigned_leb128 (abfd, bytes, &bytes_read);
17473 bytes += bytes_read;
17474 break;
17475
17476 case DW_FORM_block1:
17477 bytes += 1 + read_1_byte (abfd, bytes);
17478 break;
17479 case DW_FORM_block2:
17480 bytes += 2 + read_2_bytes (abfd, bytes);
17481 break;
17482 case DW_FORM_block4:
17483 bytes += 4 + read_4_bytes (abfd, bytes);
17484 break;
17485
17486 case DW_FORM_sdata:
17487 case DW_FORM_udata:
3019eac3
DE
17488 case DW_FORM_GNU_addr_index:
17489 case DW_FORM_GNU_str_index:
f664829e
DE
17490 bytes = (gdb_byte *) gdb_skip_leb128 (bytes, buffer_end);
17491 if (bytes == NULL)
17492 {
17493 dwarf2_section_buffer_overflow_complaint (section);
17494 return NULL;
17495 }
cf2c3c16
TT
17496 break;
17497
17498 default:
17499 {
17500 complain:
17501 complaint (&symfile_complaints,
17502 _("invalid form 0x%x in `%s'"),
17503 form,
17504 section->asection->name);
17505 return NULL;
17506 }
2e276125
JB
17507 }
17508
cf2c3c16
TT
17509 return bytes;
17510}
757a13d0 17511
cf2c3c16
TT
17512/* A helper for dwarf_decode_macros that handles skipping an unknown
17513 opcode. Returns an updated pointer to the macro data buffer; or,
17514 on error, issues a complaint and returns NULL. */
757a13d0 17515
cf2c3c16
TT
17516static gdb_byte *
17517skip_unknown_opcode (unsigned int opcode,
17518 gdb_byte **opcode_definitions,
f664829e 17519 gdb_byte *mac_ptr, gdb_byte *mac_end,
cf2c3c16
TT
17520 bfd *abfd,
17521 unsigned int offset_size,
17522 struct dwarf2_section_info *section)
17523{
17524 unsigned int bytes_read, i;
17525 unsigned long arg;
17526 gdb_byte *defn;
2e276125 17527
cf2c3c16 17528 if (opcode_definitions[opcode] == NULL)
2e276125 17529 {
cf2c3c16
TT
17530 complaint (&symfile_complaints,
17531 _("unrecognized DW_MACFINO opcode 0x%x"),
17532 opcode);
17533 return NULL;
17534 }
2e276125 17535
cf2c3c16
TT
17536 defn = opcode_definitions[opcode];
17537 arg = read_unsigned_leb128 (abfd, defn, &bytes_read);
17538 defn += bytes_read;
2e276125 17539
cf2c3c16
TT
17540 for (i = 0; i < arg; ++i)
17541 {
f664829e
DE
17542 mac_ptr = skip_form_bytes (abfd, mac_ptr, mac_end, defn[i], offset_size,
17543 section);
cf2c3c16
TT
17544 if (mac_ptr == NULL)
17545 {
17546 /* skip_form_bytes already issued the complaint. */
17547 return NULL;
17548 }
17549 }
757a13d0 17550
cf2c3c16
TT
17551 return mac_ptr;
17552}
757a13d0 17553
cf2c3c16
TT
17554/* A helper function which parses the header of a macro section.
17555 If the macro section is the extended (for now called "GNU") type,
17556 then this updates *OFFSET_SIZE. Returns a pointer to just after
17557 the header, or issues a complaint and returns NULL on error. */
757a13d0 17558
cf2c3c16
TT
17559static gdb_byte *
17560dwarf_parse_macro_header (gdb_byte **opcode_definitions,
17561 bfd *abfd,
17562 gdb_byte *mac_ptr,
17563 unsigned int *offset_size,
17564 int section_is_gnu)
17565{
17566 memset (opcode_definitions, 0, 256 * sizeof (gdb_byte *));
757a13d0 17567
cf2c3c16
TT
17568 if (section_is_gnu)
17569 {
17570 unsigned int version, flags;
757a13d0 17571
cf2c3c16
TT
17572 version = read_2_bytes (abfd, mac_ptr);
17573 if (version != 4)
17574 {
17575 complaint (&symfile_complaints,
17576 _("unrecognized version `%d' in .debug_macro section"),
17577 version);
17578 return NULL;
17579 }
17580 mac_ptr += 2;
757a13d0 17581
cf2c3c16
TT
17582 flags = read_1_byte (abfd, mac_ptr);
17583 ++mac_ptr;
17584 *offset_size = (flags & 1) ? 8 : 4;
757a13d0 17585
cf2c3c16
TT
17586 if ((flags & 2) != 0)
17587 /* We don't need the line table offset. */
17588 mac_ptr += *offset_size;
757a13d0 17589
cf2c3c16
TT
17590 /* Vendor opcode descriptions. */
17591 if ((flags & 4) != 0)
17592 {
17593 unsigned int i, count;
757a13d0 17594
cf2c3c16
TT
17595 count = read_1_byte (abfd, mac_ptr);
17596 ++mac_ptr;
17597 for (i = 0; i < count; ++i)
17598 {
17599 unsigned int opcode, bytes_read;
17600 unsigned long arg;
17601
17602 opcode = read_1_byte (abfd, mac_ptr);
17603 ++mac_ptr;
17604 opcode_definitions[opcode] = mac_ptr;
17605 arg = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
17606 mac_ptr += bytes_read;
17607 mac_ptr += arg;
17608 }
757a13d0 17609 }
cf2c3c16 17610 }
757a13d0 17611
cf2c3c16
TT
17612 return mac_ptr;
17613}
757a13d0 17614
cf2c3c16 17615/* A helper for dwarf_decode_macros that handles the GNU extensions,
8fc3fc34 17616 including DW_MACRO_GNU_transparent_include. */
cf2c3c16
TT
17617
17618static void
17619dwarf_decode_macro_bytes (bfd *abfd, gdb_byte *mac_ptr, gdb_byte *mac_end,
17620 struct macro_source_file *current_file,
17621 struct line_header *lh, char *comp_dir,
17622 struct dwarf2_section_info *section,
36586728 17623 int section_is_gnu, int section_is_dwz,
cf2c3c16 17624 unsigned int offset_size,
8fc3fc34
TT
17625 struct objfile *objfile,
17626 htab_t include_hash)
cf2c3c16
TT
17627{
17628 enum dwarf_macro_record_type macinfo_type;
17629 int at_commandline;
17630 gdb_byte *opcode_definitions[256];
757a13d0 17631
cf2c3c16
TT
17632 mac_ptr = dwarf_parse_macro_header (opcode_definitions, abfd, mac_ptr,
17633 &offset_size, section_is_gnu);
17634 if (mac_ptr == NULL)
17635 {
17636 /* We already issued a complaint. */
17637 return;
17638 }
757a13d0
JK
17639
17640 /* Determines if GDB is still before first DW_MACINFO_start_file. If true
17641 GDB is still reading the definitions from command line. First
17642 DW_MACINFO_start_file will need to be ignored as it was already executed
17643 to create CURRENT_FILE for the main source holding also the command line
17644 definitions. On first met DW_MACINFO_start_file this flag is reset to
17645 normally execute all the remaining DW_MACINFO_start_file macinfos. */
17646
17647 at_commandline = 1;
17648
17649 do
17650 {
17651 /* Do we at least have room for a macinfo type byte? */
17652 if (mac_ptr >= mac_end)
17653 {
f664829e 17654 dwarf2_section_buffer_overflow_complaint (section);
757a13d0
JK
17655 break;
17656 }
17657
17658 macinfo_type = read_1_byte (abfd, mac_ptr);
17659 mac_ptr++;
17660
cf2c3c16
TT
17661 /* Note that we rely on the fact that the corresponding GNU and
17662 DWARF constants are the same. */
757a13d0
JK
17663 switch (macinfo_type)
17664 {
17665 /* A zero macinfo type indicates the end of the macro
17666 information. */
17667 case 0:
17668 break;
2e276125 17669
cf2c3c16
TT
17670 case DW_MACRO_GNU_define:
17671 case DW_MACRO_GNU_undef:
17672 case DW_MACRO_GNU_define_indirect:
17673 case DW_MACRO_GNU_undef_indirect:
36586728
TT
17674 case DW_MACRO_GNU_define_indirect_alt:
17675 case DW_MACRO_GNU_undef_indirect_alt:
2e276125 17676 {
891d2f0b 17677 unsigned int bytes_read;
2e276125
JB
17678 int line;
17679 char *body;
cf2c3c16 17680 int is_define;
2e276125 17681
cf2c3c16
TT
17682 line = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
17683 mac_ptr += bytes_read;
17684
17685 if (macinfo_type == DW_MACRO_GNU_define
17686 || macinfo_type == DW_MACRO_GNU_undef)
17687 {
17688 body = read_direct_string (abfd, mac_ptr, &bytes_read);
17689 mac_ptr += bytes_read;
17690 }
17691 else
17692 {
17693 LONGEST str_offset;
17694
17695 str_offset = read_offset_1 (abfd, mac_ptr, offset_size);
17696 mac_ptr += offset_size;
2e276125 17697
36586728 17698 if (macinfo_type == DW_MACRO_GNU_define_indirect_alt
f7a35f02
TT
17699 || macinfo_type == DW_MACRO_GNU_undef_indirect_alt
17700 || section_is_dwz)
36586728
TT
17701 {
17702 struct dwz_file *dwz = dwarf2_get_dwz_file ();
17703
17704 body = read_indirect_string_from_dwz (dwz, str_offset);
17705 }
17706 else
17707 body = read_indirect_string_at_offset (abfd, str_offset);
cf2c3c16
TT
17708 }
17709
17710 is_define = (macinfo_type == DW_MACRO_GNU_define
36586728
TT
17711 || macinfo_type == DW_MACRO_GNU_define_indirect
17712 || macinfo_type == DW_MACRO_GNU_define_indirect_alt);
2e276125 17713 if (! current_file)
757a13d0
JK
17714 {
17715 /* DWARF violation as no main source is present. */
17716 complaint (&symfile_complaints,
17717 _("debug info with no main source gives macro %s "
17718 "on line %d: %s"),
cf2c3c16
TT
17719 is_define ? _("definition") : _("undefinition"),
17720 line, body);
757a13d0
JK
17721 break;
17722 }
3e43a32a
MS
17723 if ((line == 0 && !at_commandline)
17724 || (line != 0 && at_commandline))
4d3c2250 17725 complaint (&symfile_complaints,
757a13d0
JK
17726 _("debug info gives %s macro %s with %s line %d: %s"),
17727 at_commandline ? _("command-line") : _("in-file"),
cf2c3c16 17728 is_define ? _("definition") : _("undefinition"),
757a13d0
JK
17729 line == 0 ? _("zero") : _("non-zero"), line, body);
17730
cf2c3c16 17731 if (is_define)
757a13d0 17732 parse_macro_definition (current_file, line, body);
cf2c3c16
TT
17733 else
17734 {
17735 gdb_assert (macinfo_type == DW_MACRO_GNU_undef
36586728
TT
17736 || macinfo_type == DW_MACRO_GNU_undef_indirect
17737 || macinfo_type == DW_MACRO_GNU_undef_indirect_alt);
cf2c3c16
TT
17738 macro_undef (current_file, line, body);
17739 }
2e276125
JB
17740 }
17741 break;
17742
cf2c3c16 17743 case DW_MACRO_GNU_start_file:
2e276125 17744 {
891d2f0b 17745 unsigned int bytes_read;
2e276125
JB
17746 int line, file;
17747
17748 line = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
17749 mac_ptr += bytes_read;
17750 file = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
17751 mac_ptr += bytes_read;
17752
3e43a32a
MS
17753 if ((line == 0 && !at_commandline)
17754 || (line != 0 && at_commandline))
757a13d0
JK
17755 complaint (&symfile_complaints,
17756 _("debug info gives source %d included "
17757 "from %s at %s line %d"),
17758 file, at_commandline ? _("command-line") : _("file"),
17759 line == 0 ? _("zero") : _("non-zero"), line);
17760
17761 if (at_commandline)
17762 {
cf2c3c16
TT
17763 /* This DW_MACRO_GNU_start_file was executed in the
17764 pass one. */
757a13d0
JK
17765 at_commandline = 0;
17766 }
17767 else
17768 current_file = macro_start_file (file, line,
17769 current_file, comp_dir,
cf2c3c16 17770 lh, objfile);
2e276125
JB
17771 }
17772 break;
17773
cf2c3c16 17774 case DW_MACRO_GNU_end_file:
2e276125 17775 if (! current_file)
4d3c2250 17776 complaint (&symfile_complaints,
3e43a32a
MS
17777 _("macro debug info has an unmatched "
17778 "`close_file' directive"));
2e276125
JB
17779 else
17780 {
17781 current_file = current_file->included_by;
17782 if (! current_file)
17783 {
cf2c3c16 17784 enum dwarf_macro_record_type next_type;
2e276125
JB
17785
17786 /* GCC circa March 2002 doesn't produce the zero
17787 type byte marking the end of the compilation
17788 unit. Complain if it's not there, but exit no
17789 matter what. */
17790
17791 /* Do we at least have room for a macinfo type byte? */
17792 if (mac_ptr >= mac_end)
17793 {
f664829e 17794 dwarf2_section_buffer_overflow_complaint (section);
2e276125
JB
17795 return;
17796 }
17797
17798 /* We don't increment mac_ptr here, so this is just
17799 a look-ahead. */
17800 next_type = read_1_byte (abfd, mac_ptr);
17801 if (next_type != 0)
4d3c2250 17802 complaint (&symfile_complaints,
3e43a32a
MS
17803 _("no terminating 0-type entry for "
17804 "macros in `.debug_macinfo' section"));
2e276125
JB
17805
17806 return;
17807 }
17808 }
17809 break;
17810
cf2c3c16 17811 case DW_MACRO_GNU_transparent_include:
36586728 17812 case DW_MACRO_GNU_transparent_include_alt:
cf2c3c16
TT
17813 {
17814 LONGEST offset;
8fc3fc34 17815 void **slot;
a036ba48
TT
17816 bfd *include_bfd = abfd;
17817 struct dwarf2_section_info *include_section = section;
17818 struct dwarf2_section_info alt_section;
17819 gdb_byte *include_mac_end = mac_end;
17820 int is_dwz = section_is_dwz;
17821 gdb_byte *new_mac_ptr;
cf2c3c16
TT
17822
17823 offset = read_offset_1 (abfd, mac_ptr, offset_size);
17824 mac_ptr += offset_size;
17825
a036ba48
TT
17826 if (macinfo_type == DW_MACRO_GNU_transparent_include_alt)
17827 {
17828 struct dwz_file *dwz = dwarf2_get_dwz_file ();
17829
17830 dwarf2_read_section (dwarf2_per_objfile->objfile,
17831 &dwz->macro);
17832
17833 include_bfd = dwz->macro.asection->owner;
17834 include_section = &dwz->macro;
17835 include_mac_end = dwz->macro.buffer + dwz->macro.size;
17836 is_dwz = 1;
17837 }
17838
17839 new_mac_ptr = include_section->buffer + offset;
17840 slot = htab_find_slot (include_hash, new_mac_ptr, INSERT);
17841
8fc3fc34
TT
17842 if (*slot != NULL)
17843 {
17844 /* This has actually happened; see
17845 http://sourceware.org/bugzilla/show_bug.cgi?id=13568. */
17846 complaint (&symfile_complaints,
17847 _("recursive DW_MACRO_GNU_transparent_include in "
17848 ".debug_macro section"));
17849 }
17850 else
17851 {
a036ba48 17852 *slot = new_mac_ptr;
36586728 17853
a036ba48 17854 dwarf_decode_macro_bytes (include_bfd, new_mac_ptr,
36586728 17855 include_mac_end, current_file,
8fc3fc34 17856 lh, comp_dir,
36586728 17857 section, section_is_gnu, is_dwz,
8fc3fc34
TT
17858 offset_size, objfile, include_hash);
17859
a036ba48 17860 htab_remove_elt (include_hash, new_mac_ptr);
8fc3fc34 17861 }
cf2c3c16
TT
17862 }
17863 break;
17864
2e276125 17865 case DW_MACINFO_vendor_ext:
cf2c3c16
TT
17866 if (!section_is_gnu)
17867 {
17868 unsigned int bytes_read;
17869 int constant;
2e276125 17870
cf2c3c16
TT
17871 constant = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
17872 mac_ptr += bytes_read;
17873 read_direct_string (abfd, mac_ptr, &bytes_read);
17874 mac_ptr += bytes_read;
2e276125 17875
cf2c3c16
TT
17876 /* We don't recognize any vendor extensions. */
17877 break;
17878 }
17879 /* FALLTHROUGH */
17880
17881 default:
17882 mac_ptr = skip_unknown_opcode (macinfo_type, opcode_definitions,
f664829e 17883 mac_ptr, mac_end, abfd, offset_size,
cf2c3c16
TT
17884 section);
17885 if (mac_ptr == NULL)
17886 return;
17887 break;
2e276125 17888 }
757a13d0 17889 } while (macinfo_type != 0);
2e276125 17890}
8e19ed76 17891
cf2c3c16 17892static void
09262596
DE
17893dwarf_decode_macros (struct dwarf2_cu *cu, unsigned int offset,
17894 char *comp_dir, int section_is_gnu)
cf2c3c16 17895{
bb5ed363 17896 struct objfile *objfile = dwarf2_per_objfile->objfile;
09262596
DE
17897 struct line_header *lh = cu->line_header;
17898 bfd *abfd;
cf2c3c16
TT
17899 gdb_byte *mac_ptr, *mac_end;
17900 struct macro_source_file *current_file = 0;
17901 enum dwarf_macro_record_type macinfo_type;
17902 unsigned int offset_size = cu->header.offset_size;
17903 gdb_byte *opcode_definitions[256];
8fc3fc34
TT
17904 struct cleanup *cleanup;
17905 htab_t include_hash;
17906 void **slot;
09262596
DE
17907 struct dwarf2_section_info *section;
17908 const char *section_name;
17909
17910 if (cu->dwo_unit != NULL)
17911 {
17912 if (section_is_gnu)
17913 {
17914 section = &cu->dwo_unit->dwo_file->sections.macro;
17915 section_name = ".debug_macro.dwo";
17916 }
17917 else
17918 {
17919 section = &cu->dwo_unit->dwo_file->sections.macinfo;
17920 section_name = ".debug_macinfo.dwo";
17921 }
17922 }
17923 else
17924 {
17925 if (section_is_gnu)
17926 {
17927 section = &dwarf2_per_objfile->macro;
17928 section_name = ".debug_macro";
17929 }
17930 else
17931 {
17932 section = &dwarf2_per_objfile->macinfo;
17933 section_name = ".debug_macinfo";
17934 }
17935 }
cf2c3c16 17936
bb5ed363 17937 dwarf2_read_section (objfile, section);
cf2c3c16
TT
17938 if (section->buffer == NULL)
17939 {
fceca515 17940 complaint (&symfile_complaints, _("missing %s section"), section_name);
cf2c3c16
TT
17941 return;
17942 }
09262596 17943 abfd = section->asection->owner;
cf2c3c16
TT
17944
17945 /* First pass: Find the name of the base filename.
17946 This filename is needed in order to process all macros whose definition
17947 (or undefinition) comes from the command line. These macros are defined
17948 before the first DW_MACINFO_start_file entry, and yet still need to be
17949 associated to the base file.
17950
17951 To determine the base file name, we scan the macro definitions until we
17952 reach the first DW_MACINFO_start_file entry. We then initialize
17953 CURRENT_FILE accordingly so that any macro definition found before the
17954 first DW_MACINFO_start_file can still be associated to the base file. */
17955
17956 mac_ptr = section->buffer + offset;
17957 mac_end = section->buffer + section->size;
17958
17959 mac_ptr = dwarf_parse_macro_header (opcode_definitions, abfd, mac_ptr,
17960 &offset_size, section_is_gnu);
17961 if (mac_ptr == NULL)
17962 {
17963 /* We already issued a complaint. */
17964 return;
17965 }
17966
17967 do
17968 {
17969 /* Do we at least have room for a macinfo type byte? */
17970 if (mac_ptr >= mac_end)
17971 {
17972 /* Complaint is printed during the second pass as GDB will probably
17973 stop the first pass earlier upon finding
17974 DW_MACINFO_start_file. */
17975 break;
17976 }
17977
17978 macinfo_type = read_1_byte (abfd, mac_ptr);
17979 mac_ptr++;
17980
17981 /* Note that we rely on the fact that the corresponding GNU and
17982 DWARF constants are the same. */
17983 switch (macinfo_type)
17984 {
17985 /* A zero macinfo type indicates the end of the macro
17986 information. */
17987 case 0:
17988 break;
17989
17990 case DW_MACRO_GNU_define:
17991 case DW_MACRO_GNU_undef:
17992 /* Only skip the data by MAC_PTR. */
17993 {
17994 unsigned int bytes_read;
17995
17996 read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
17997 mac_ptr += bytes_read;
17998 read_direct_string (abfd, mac_ptr, &bytes_read);
17999 mac_ptr += bytes_read;
18000 }
18001 break;
18002
18003 case DW_MACRO_GNU_start_file:
18004 {
18005 unsigned int bytes_read;
18006 int line, file;
18007
18008 line = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
18009 mac_ptr += bytes_read;
18010 file = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
18011 mac_ptr += bytes_read;
18012
18013 current_file = macro_start_file (file, line, current_file,
bb5ed363 18014 comp_dir, lh, objfile);
cf2c3c16
TT
18015 }
18016 break;
18017
18018 case DW_MACRO_GNU_end_file:
18019 /* No data to skip by MAC_PTR. */
18020 break;
18021
18022 case DW_MACRO_GNU_define_indirect:
18023 case DW_MACRO_GNU_undef_indirect:
f7a35f02
TT
18024 case DW_MACRO_GNU_define_indirect_alt:
18025 case DW_MACRO_GNU_undef_indirect_alt:
cf2c3c16
TT
18026 {
18027 unsigned int bytes_read;
18028
18029 read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
18030 mac_ptr += bytes_read;
18031 mac_ptr += offset_size;
18032 }
18033 break;
18034
18035 case DW_MACRO_GNU_transparent_include:
f7a35f02 18036 case DW_MACRO_GNU_transparent_include_alt:
cf2c3c16
TT
18037 /* Note that, according to the spec, a transparent include
18038 chain cannot call DW_MACRO_GNU_start_file. So, we can just
18039 skip this opcode. */
18040 mac_ptr += offset_size;
18041 break;
18042
18043 case DW_MACINFO_vendor_ext:
18044 /* Only skip the data by MAC_PTR. */
18045 if (!section_is_gnu)
18046 {
18047 unsigned int bytes_read;
18048
18049 read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
18050 mac_ptr += bytes_read;
18051 read_direct_string (abfd, mac_ptr, &bytes_read);
18052 mac_ptr += bytes_read;
18053 }
18054 /* FALLTHROUGH */
18055
18056 default:
18057 mac_ptr = skip_unknown_opcode (macinfo_type, opcode_definitions,
f664829e 18058 mac_ptr, mac_end, abfd, offset_size,
cf2c3c16
TT
18059 section);
18060 if (mac_ptr == NULL)
18061 return;
18062 break;
18063 }
18064 } while (macinfo_type != 0 && current_file == NULL);
18065
18066 /* Second pass: Process all entries.
18067
18068 Use the AT_COMMAND_LINE flag to determine whether we are still processing
18069 command-line macro definitions/undefinitions. This flag is unset when we
18070 reach the first DW_MACINFO_start_file entry. */
18071
8fc3fc34
TT
18072 include_hash = htab_create_alloc (1, htab_hash_pointer, htab_eq_pointer,
18073 NULL, xcalloc, xfree);
18074 cleanup = make_cleanup_htab_delete (include_hash);
18075 mac_ptr = section->buffer + offset;
18076 slot = htab_find_slot (include_hash, mac_ptr, INSERT);
18077 *slot = mac_ptr;
18078 dwarf_decode_macro_bytes (abfd, mac_ptr, mac_end,
36586728
TT
18079 current_file, lh, comp_dir, section,
18080 section_is_gnu, 0,
8fc3fc34
TT
18081 offset_size, objfile, include_hash);
18082 do_cleanups (cleanup);
cf2c3c16
TT
18083}
18084
8e19ed76 18085/* Check if the attribute's form is a DW_FORM_block*
0963b4bd 18086 if so return true else false. */
380bca97 18087
8e19ed76
PS
18088static int
18089attr_form_is_block (struct attribute *attr)
18090{
18091 return (attr == NULL ? 0 :
18092 attr->form == DW_FORM_block1
18093 || attr->form == DW_FORM_block2
18094 || attr->form == DW_FORM_block4
2dc7f7b3
TT
18095 || attr->form == DW_FORM_block
18096 || attr->form == DW_FORM_exprloc);
8e19ed76 18097}
4c2df51b 18098
c6a0999f
JB
18099/* Return non-zero if ATTR's value is a section offset --- classes
18100 lineptr, loclistptr, macptr or rangelistptr --- or zero, otherwise.
18101 You may use DW_UNSND (attr) to retrieve such offsets.
18102
18103 Section 7.5.4, "Attribute Encodings", explains that no attribute
18104 may have a value that belongs to more than one of these classes; it
18105 would be ambiguous if we did, because we use the same forms for all
18106 of them. */
380bca97 18107
3690dd37
JB
18108static int
18109attr_form_is_section_offset (struct attribute *attr)
18110{
18111 return (attr->form == DW_FORM_data4
2dc7f7b3
TT
18112 || attr->form == DW_FORM_data8
18113 || attr->form == DW_FORM_sec_offset);
3690dd37
JB
18114}
18115
3690dd37
JB
18116/* Return non-zero if ATTR's value falls in the 'constant' class, or
18117 zero otherwise. When this function returns true, you can apply
18118 dwarf2_get_attr_constant_value to it.
18119
18120 However, note that for some attributes you must check
18121 attr_form_is_section_offset before using this test. DW_FORM_data4
18122 and DW_FORM_data8 are members of both the constant class, and of
18123 the classes that contain offsets into other debug sections
18124 (lineptr, loclistptr, macptr or rangelistptr). The DWARF spec says
18125 that, if an attribute's can be either a constant or one of the
18126 section offset classes, DW_FORM_data4 and DW_FORM_data8 should be
18127 taken as section offsets, not constants. */
380bca97 18128
3690dd37
JB
18129static int
18130attr_form_is_constant (struct attribute *attr)
18131{
18132 switch (attr->form)
18133 {
18134 case DW_FORM_sdata:
18135 case DW_FORM_udata:
18136 case DW_FORM_data1:
18137 case DW_FORM_data2:
18138 case DW_FORM_data4:
18139 case DW_FORM_data8:
18140 return 1;
18141 default:
18142 return 0;
18143 }
18144}
18145
3019eac3
DE
18146/* Return the .debug_loc section to use for CU.
18147 For DWO files use .debug_loc.dwo. */
18148
18149static struct dwarf2_section_info *
18150cu_debug_loc_section (struct dwarf2_cu *cu)
18151{
18152 if (cu->dwo_unit)
18153 return &cu->dwo_unit->dwo_file->sections.loc;
18154 return &dwarf2_per_objfile->loc;
18155}
18156
8cf6f0b1
TT
18157/* A helper function that fills in a dwarf2_loclist_baton. */
18158
18159static void
18160fill_in_loclist_baton (struct dwarf2_cu *cu,
18161 struct dwarf2_loclist_baton *baton,
18162 struct attribute *attr)
18163{
3019eac3
DE
18164 struct dwarf2_section_info *section = cu_debug_loc_section (cu);
18165
18166 dwarf2_read_section (dwarf2_per_objfile->objfile, section);
8cf6f0b1
TT
18167
18168 baton->per_cu = cu->per_cu;
18169 gdb_assert (baton->per_cu);
18170 /* We don't know how long the location list is, but make sure we
18171 don't run off the edge of the section. */
3019eac3
DE
18172 baton->size = section->size - DW_UNSND (attr);
18173 baton->data = section->buffer + DW_UNSND (attr);
8cf6f0b1 18174 baton->base_address = cu->base_address;
f664829e 18175 baton->from_dwo = cu->dwo_unit != NULL;
8cf6f0b1
TT
18176}
18177
4c2df51b
DJ
18178static void
18179dwarf2_symbol_mark_computed (struct attribute *attr, struct symbol *sym,
e7c27a73 18180 struct dwarf2_cu *cu)
4c2df51b 18181{
bb5ed363 18182 struct objfile *objfile = dwarf2_per_objfile->objfile;
3019eac3 18183 struct dwarf2_section_info *section = cu_debug_loc_section (cu);
bb5ed363 18184
3690dd37 18185 if (attr_form_is_section_offset (attr)
3019eac3 18186 /* .debug_loc{,.dwo} may not exist at all, or the offset may be outside
99bcc461
DJ
18187 the section. If so, fall through to the complaint in the
18188 other branch. */
3019eac3 18189 && DW_UNSND (attr) < dwarf2_section_size (objfile, section))
4c2df51b 18190 {
0d53c4c4 18191 struct dwarf2_loclist_baton *baton;
4c2df51b 18192
bb5ed363 18193 baton = obstack_alloc (&objfile->objfile_obstack,
0d53c4c4 18194 sizeof (struct dwarf2_loclist_baton));
4c2df51b 18195
8cf6f0b1 18196 fill_in_loclist_baton (cu, baton, attr);
be391dca 18197
d00adf39 18198 if (cu->base_known == 0)
0d53c4c4 18199 complaint (&symfile_complaints,
3e43a32a
MS
18200 _("Location list used without "
18201 "specifying the CU base address."));
4c2df51b 18202
768a979c 18203 SYMBOL_COMPUTED_OPS (sym) = &dwarf2_loclist_funcs;
0d53c4c4
DJ
18204 SYMBOL_LOCATION_BATON (sym) = baton;
18205 }
18206 else
18207 {
18208 struct dwarf2_locexpr_baton *baton;
18209
bb5ed363 18210 baton = obstack_alloc (&objfile->objfile_obstack,
0d53c4c4 18211 sizeof (struct dwarf2_locexpr_baton));
ae0d2f24
UW
18212 baton->per_cu = cu->per_cu;
18213 gdb_assert (baton->per_cu);
0d53c4c4
DJ
18214
18215 if (attr_form_is_block (attr))
18216 {
18217 /* Note that we're just copying the block's data pointer
18218 here, not the actual data. We're still pointing into the
6502dd73
DJ
18219 info_buffer for SYM's objfile; right now we never release
18220 that buffer, but when we do clean up properly this may
18221 need to change. */
0d53c4c4
DJ
18222 baton->size = DW_BLOCK (attr)->size;
18223 baton->data = DW_BLOCK (attr)->data;
18224 }
18225 else
18226 {
18227 dwarf2_invalid_attrib_class_complaint ("location description",
18228 SYMBOL_NATURAL_NAME (sym));
18229 baton->size = 0;
0d53c4c4 18230 }
6e70227d 18231
768a979c 18232 SYMBOL_COMPUTED_OPS (sym) = &dwarf2_locexpr_funcs;
0d53c4c4
DJ
18233 SYMBOL_LOCATION_BATON (sym) = baton;
18234 }
4c2df51b 18235}
6502dd73 18236
9aa1f1e3
TT
18237/* Return the OBJFILE associated with the compilation unit CU. If CU
18238 came from a separate debuginfo file, then the master objfile is
18239 returned. */
ae0d2f24
UW
18240
18241struct objfile *
18242dwarf2_per_cu_objfile (struct dwarf2_per_cu_data *per_cu)
18243{
9291a0cd 18244 struct objfile *objfile = per_cu->objfile;
ae0d2f24
UW
18245
18246 /* Return the master objfile, so that we can report and look up the
18247 correct file containing this variable. */
18248 if (objfile->separate_debug_objfile_backlink)
18249 objfile = objfile->separate_debug_objfile_backlink;
18250
18251 return objfile;
18252}
18253
96408a79
SA
18254/* Return comp_unit_head for PER_CU, either already available in PER_CU->CU
18255 (CU_HEADERP is unused in such case) or prepare a temporary copy at
18256 CU_HEADERP first. */
18257
18258static const struct comp_unit_head *
18259per_cu_header_read_in (struct comp_unit_head *cu_headerp,
18260 struct dwarf2_per_cu_data *per_cu)
18261{
96408a79
SA
18262 gdb_byte *info_ptr;
18263
18264 if (per_cu->cu)
18265 return &per_cu->cu->header;
18266
0bc3a05c 18267 info_ptr = per_cu->info_or_types_section->buffer + per_cu->offset.sect_off;
96408a79
SA
18268
18269 memset (cu_headerp, 0, sizeof (*cu_headerp));
0bc3a05c 18270 read_comp_unit_head (cu_headerp, info_ptr, per_cu->objfile->obfd);
96408a79
SA
18271
18272 return cu_headerp;
18273}
18274
ae0d2f24
UW
18275/* Return the address size given in the compilation unit header for CU. */
18276
98714339 18277int
ae0d2f24
UW
18278dwarf2_per_cu_addr_size (struct dwarf2_per_cu_data *per_cu)
18279{
96408a79
SA
18280 struct comp_unit_head cu_header_local;
18281 const struct comp_unit_head *cu_headerp;
c471e790 18282
96408a79
SA
18283 cu_headerp = per_cu_header_read_in (&cu_header_local, per_cu);
18284
18285 return cu_headerp->addr_size;
ae0d2f24
UW
18286}
18287
9eae7c52
TT
18288/* Return the offset size given in the compilation unit header for CU. */
18289
18290int
18291dwarf2_per_cu_offset_size (struct dwarf2_per_cu_data *per_cu)
18292{
96408a79
SA
18293 struct comp_unit_head cu_header_local;
18294 const struct comp_unit_head *cu_headerp;
9c6c53f7 18295
96408a79
SA
18296 cu_headerp = per_cu_header_read_in (&cu_header_local, per_cu);
18297
18298 return cu_headerp->offset_size;
18299}
18300
18301/* See its dwarf2loc.h declaration. */
18302
18303int
18304dwarf2_per_cu_ref_addr_size (struct dwarf2_per_cu_data *per_cu)
18305{
18306 struct comp_unit_head cu_header_local;
18307 const struct comp_unit_head *cu_headerp;
18308
18309 cu_headerp = per_cu_header_read_in (&cu_header_local, per_cu);
18310
18311 if (cu_headerp->version == 2)
18312 return cu_headerp->addr_size;
18313 else
18314 return cu_headerp->offset_size;
181cebd4
JK
18315}
18316
9aa1f1e3
TT
18317/* Return the text offset of the CU. The returned offset comes from
18318 this CU's objfile. If this objfile came from a separate debuginfo
18319 file, then the offset may be different from the corresponding
18320 offset in the parent objfile. */
18321
18322CORE_ADDR
18323dwarf2_per_cu_text_offset (struct dwarf2_per_cu_data *per_cu)
18324{
bb3fa9d0 18325 struct objfile *objfile = per_cu->objfile;
9aa1f1e3
TT
18326
18327 return ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
18328}
18329
348e048f
DE
18330/* Locate the .debug_info compilation unit from CU's objfile which contains
18331 the DIE at OFFSET. Raises an error on failure. */
ae038cb0
DJ
18332
18333static struct dwarf2_per_cu_data *
b64f50a1 18334dwarf2_find_containing_comp_unit (sect_offset offset,
36586728 18335 unsigned int offset_in_dwz,
ae038cb0
DJ
18336 struct objfile *objfile)
18337{
18338 struct dwarf2_per_cu_data *this_cu;
18339 int low, high;
36586728 18340 const sect_offset *cu_off;
ae038cb0 18341
ae038cb0
DJ
18342 low = 0;
18343 high = dwarf2_per_objfile->n_comp_units - 1;
18344 while (high > low)
18345 {
36586728 18346 struct dwarf2_per_cu_data *mid_cu;
ae038cb0 18347 int mid = low + (high - low) / 2;
9a619af0 18348
36586728
TT
18349 mid_cu = dwarf2_per_objfile->all_comp_units[mid];
18350 cu_off = &mid_cu->offset;
18351 if (mid_cu->is_dwz > offset_in_dwz
18352 || (mid_cu->is_dwz == offset_in_dwz
18353 && cu_off->sect_off >= offset.sect_off))
ae038cb0
DJ
18354 high = mid;
18355 else
18356 low = mid + 1;
18357 }
18358 gdb_assert (low == high);
36586728
TT
18359 this_cu = dwarf2_per_objfile->all_comp_units[low];
18360 cu_off = &this_cu->offset;
18361 if (this_cu->is_dwz != offset_in_dwz || cu_off->sect_off > offset.sect_off)
ae038cb0 18362 {
36586728 18363 if (low == 0 || this_cu->is_dwz != offset_in_dwz)
8a3fe4f8
AC
18364 error (_("Dwarf Error: could not find partial DIE containing "
18365 "offset 0x%lx [in module %s]"),
b64f50a1 18366 (long) offset.sect_off, bfd_get_filename (objfile->obfd));
10b3939b 18367
b64f50a1
JK
18368 gdb_assert (dwarf2_per_objfile->all_comp_units[low-1]->offset.sect_off
18369 <= offset.sect_off);
ae038cb0
DJ
18370 return dwarf2_per_objfile->all_comp_units[low-1];
18371 }
18372 else
18373 {
18374 this_cu = dwarf2_per_objfile->all_comp_units[low];
18375 if (low == dwarf2_per_objfile->n_comp_units - 1
b64f50a1
JK
18376 && offset.sect_off >= this_cu->offset.sect_off + this_cu->length)
18377 error (_("invalid dwarf2 offset %u"), offset.sect_off);
18378 gdb_assert (offset.sect_off < this_cu->offset.sect_off + this_cu->length);
ae038cb0
DJ
18379 return this_cu;
18380 }
18381}
18382
23745b47 18383/* Initialize dwarf2_cu CU, owned by PER_CU. */
93311388 18384
9816fde3 18385static void
23745b47 18386init_one_comp_unit (struct dwarf2_cu *cu, struct dwarf2_per_cu_data *per_cu)
93311388 18387{
9816fde3 18388 memset (cu, 0, sizeof (*cu));
23745b47
DE
18389 per_cu->cu = cu;
18390 cu->per_cu = per_cu;
18391 cu->objfile = per_cu->objfile;
93311388 18392 obstack_init (&cu->comp_unit_obstack);
9816fde3
JK
18393}
18394
18395/* Initialize basic fields of dwarf_cu CU according to DIE COMP_UNIT_DIE. */
18396
18397static void
95554aad
TT
18398prepare_one_comp_unit (struct dwarf2_cu *cu, struct die_info *comp_unit_die,
18399 enum language pretend_language)
9816fde3
JK
18400{
18401 struct attribute *attr;
18402
18403 /* Set the language we're debugging. */
18404 attr = dwarf2_attr (comp_unit_die, DW_AT_language, cu);
18405 if (attr)
18406 set_cu_language (DW_UNSND (attr), cu);
18407 else
9cded63f 18408 {
95554aad 18409 cu->language = pretend_language;
9cded63f
TT
18410 cu->language_defn = language_def (cu->language);
18411 }
dee91e82
DE
18412
18413 attr = dwarf2_attr (comp_unit_die, DW_AT_producer, cu);
18414 if (attr)
18415 cu->producer = DW_STRING (attr);
93311388
DE
18416}
18417
ae038cb0
DJ
18418/* Release one cached compilation unit, CU. We unlink it from the tree
18419 of compilation units, but we don't remove it from the read_in_chain;
93311388
DE
18420 the caller is responsible for that.
18421 NOTE: DATA is a void * because this function is also used as a
18422 cleanup routine. */
ae038cb0
DJ
18423
18424static void
68dc6402 18425free_heap_comp_unit (void *data)
ae038cb0
DJ
18426{
18427 struct dwarf2_cu *cu = data;
18428
23745b47
DE
18429 gdb_assert (cu->per_cu != NULL);
18430 cu->per_cu->cu = NULL;
ae038cb0
DJ
18431 cu->per_cu = NULL;
18432
18433 obstack_free (&cu->comp_unit_obstack, NULL);
18434
18435 xfree (cu);
18436}
18437
72bf9492 18438/* This cleanup function is passed the address of a dwarf2_cu on the stack
ae038cb0 18439 when we're finished with it. We can't free the pointer itself, but be
dee91e82 18440 sure to unlink it from the cache. Also release any associated storage. */
72bf9492
DJ
18441
18442static void
18443free_stack_comp_unit (void *data)
18444{
18445 struct dwarf2_cu *cu = data;
18446
23745b47
DE
18447 gdb_assert (cu->per_cu != NULL);
18448 cu->per_cu->cu = NULL;
18449 cu->per_cu = NULL;
18450
72bf9492
DJ
18451 obstack_free (&cu->comp_unit_obstack, NULL);
18452 cu->partial_dies = NULL;
ae038cb0
DJ
18453}
18454
18455/* Free all cached compilation units. */
18456
18457static void
18458free_cached_comp_units (void *data)
18459{
18460 struct dwarf2_per_cu_data *per_cu, **last_chain;
18461
18462 per_cu = dwarf2_per_objfile->read_in_chain;
18463 last_chain = &dwarf2_per_objfile->read_in_chain;
18464 while (per_cu != NULL)
18465 {
18466 struct dwarf2_per_cu_data *next_cu;
18467
18468 next_cu = per_cu->cu->read_in_chain;
18469
68dc6402 18470 free_heap_comp_unit (per_cu->cu);
ae038cb0
DJ
18471 *last_chain = next_cu;
18472
18473 per_cu = next_cu;
18474 }
18475}
18476
18477/* Increase the age counter on each cached compilation unit, and free
18478 any that are too old. */
18479
18480static void
18481age_cached_comp_units (void)
18482{
18483 struct dwarf2_per_cu_data *per_cu, **last_chain;
18484
18485 dwarf2_clear_marks (dwarf2_per_objfile->read_in_chain);
18486 per_cu = dwarf2_per_objfile->read_in_chain;
18487 while (per_cu != NULL)
18488 {
18489 per_cu->cu->last_used ++;
18490 if (per_cu->cu->last_used <= dwarf2_max_cache_age)
18491 dwarf2_mark (per_cu->cu);
18492 per_cu = per_cu->cu->read_in_chain;
18493 }
18494
18495 per_cu = dwarf2_per_objfile->read_in_chain;
18496 last_chain = &dwarf2_per_objfile->read_in_chain;
18497 while (per_cu != NULL)
18498 {
18499 struct dwarf2_per_cu_data *next_cu;
18500
18501 next_cu = per_cu->cu->read_in_chain;
18502
18503 if (!per_cu->cu->mark)
18504 {
68dc6402 18505 free_heap_comp_unit (per_cu->cu);
ae038cb0
DJ
18506 *last_chain = next_cu;
18507 }
18508 else
18509 last_chain = &per_cu->cu->read_in_chain;
18510
18511 per_cu = next_cu;
18512 }
18513}
18514
18515/* Remove a single compilation unit from the cache. */
18516
18517static void
dee91e82 18518free_one_cached_comp_unit (struct dwarf2_per_cu_data *target_per_cu)
ae038cb0
DJ
18519{
18520 struct dwarf2_per_cu_data *per_cu, **last_chain;
18521
18522 per_cu = dwarf2_per_objfile->read_in_chain;
18523 last_chain = &dwarf2_per_objfile->read_in_chain;
18524 while (per_cu != NULL)
18525 {
18526 struct dwarf2_per_cu_data *next_cu;
18527
18528 next_cu = per_cu->cu->read_in_chain;
18529
dee91e82 18530 if (per_cu == target_per_cu)
ae038cb0 18531 {
68dc6402 18532 free_heap_comp_unit (per_cu->cu);
dee91e82 18533 per_cu->cu = NULL;
ae038cb0
DJ
18534 *last_chain = next_cu;
18535 break;
18536 }
18537 else
18538 last_chain = &per_cu->cu->read_in_chain;
18539
18540 per_cu = next_cu;
18541 }
18542}
18543
fe3e1990
DJ
18544/* Release all extra memory associated with OBJFILE. */
18545
18546void
18547dwarf2_free_objfile (struct objfile *objfile)
18548{
18549 dwarf2_per_objfile = objfile_data (objfile, dwarf2_objfile_data_key);
18550
18551 if (dwarf2_per_objfile == NULL)
18552 return;
18553
18554 /* Cached DIE trees use xmalloc and the comp_unit_obstack. */
18555 free_cached_comp_units (NULL);
18556
7b9f3c50
DE
18557 if (dwarf2_per_objfile->quick_file_names_table)
18558 htab_delete (dwarf2_per_objfile->quick_file_names_table);
9291a0cd 18559
fe3e1990
DJ
18560 /* Everything else should be on the objfile obstack. */
18561}
18562
dee91e82
DE
18563/* A set of CU "per_cu" pointer, DIE offset, and GDB type pointer.
18564 We store these in a hash table separate from the DIEs, and preserve them
18565 when the DIEs are flushed out of cache.
18566
18567 The CU "per_cu" pointer is needed because offset alone is not enough to
3019eac3
DE
18568 uniquely identify the type. A file may have multiple .debug_types sections,
18569 or the type may come from a DWO file. We have to use something in
18570 dwarf2_per_cu_data (or the pointer to it) because we can enter the lookup
18571 routine, get_die_type_at_offset, from outside this file, and thus won't
18572 necessarily have PER_CU->cu. Fortunately, PER_CU is stable for the life
18573 of the objfile. */
1c379e20 18574
dee91e82 18575struct dwarf2_per_cu_offset_and_type
1c379e20 18576{
dee91e82 18577 const struct dwarf2_per_cu_data *per_cu;
b64f50a1 18578 sect_offset offset;
1c379e20
DJ
18579 struct type *type;
18580};
18581
dee91e82 18582/* Hash function for a dwarf2_per_cu_offset_and_type. */
1c379e20
DJ
18583
18584static hashval_t
dee91e82 18585per_cu_offset_and_type_hash (const void *item)
1c379e20 18586{
dee91e82 18587 const struct dwarf2_per_cu_offset_and_type *ofs = item;
9a619af0 18588
dee91e82 18589 return (uintptr_t) ofs->per_cu + ofs->offset.sect_off;
1c379e20
DJ
18590}
18591
dee91e82 18592/* Equality function for a dwarf2_per_cu_offset_and_type. */
1c379e20
DJ
18593
18594static int
dee91e82 18595per_cu_offset_and_type_eq (const void *item_lhs, const void *item_rhs)
1c379e20 18596{
dee91e82
DE
18597 const struct dwarf2_per_cu_offset_and_type *ofs_lhs = item_lhs;
18598 const struct dwarf2_per_cu_offset_and_type *ofs_rhs = item_rhs;
9a619af0 18599
dee91e82
DE
18600 return (ofs_lhs->per_cu == ofs_rhs->per_cu
18601 && ofs_lhs->offset.sect_off == ofs_rhs->offset.sect_off);
1c379e20
DJ
18602}
18603
18604/* Set the type associated with DIE to TYPE. Save it in CU's hash
7e314c57
JK
18605 table if necessary. For convenience, return TYPE.
18606
18607 The DIEs reading must have careful ordering to:
18608 * Not cause infite loops trying to read in DIEs as a prerequisite for
18609 reading current DIE.
18610 * Not trying to dereference contents of still incompletely read in types
18611 while reading in other DIEs.
18612 * Enable referencing still incompletely read in types just by a pointer to
18613 the type without accessing its fields.
18614
18615 Therefore caller should follow these rules:
18616 * Try to fetch any prerequisite types we may need to build this DIE type
18617 before building the type and calling set_die_type.
e71ec853 18618 * After building type call set_die_type for current DIE as soon as
7e314c57
JK
18619 possible before fetching more types to complete the current type.
18620 * Make the type as complete as possible before fetching more types. */
1c379e20 18621
f792889a 18622static struct type *
1c379e20
DJ
18623set_die_type (struct die_info *die, struct type *type, struct dwarf2_cu *cu)
18624{
dee91e82 18625 struct dwarf2_per_cu_offset_and_type **slot, ofs;
673bfd45 18626 struct objfile *objfile = cu->objfile;
1c379e20 18627
b4ba55a1
JB
18628 /* For Ada types, make sure that the gnat-specific data is always
18629 initialized (if not already set). There are a few types where
18630 we should not be doing so, because the type-specific area is
18631 already used to hold some other piece of info (eg: TYPE_CODE_FLT
18632 where the type-specific area is used to store the floatformat).
18633 But this is not a problem, because the gnat-specific information
18634 is actually not needed for these types. */
18635 if (need_gnat_info (cu)
18636 && TYPE_CODE (type) != TYPE_CODE_FUNC
18637 && TYPE_CODE (type) != TYPE_CODE_FLT
18638 && !HAVE_GNAT_AUX_INFO (type))
18639 INIT_GNAT_SPECIFIC (type);
18640
dee91e82 18641 if (dwarf2_per_objfile->die_type_hash == NULL)
f792889a 18642 {
dee91e82
DE
18643 dwarf2_per_objfile->die_type_hash =
18644 htab_create_alloc_ex (127,
18645 per_cu_offset_and_type_hash,
18646 per_cu_offset_and_type_eq,
18647 NULL,
18648 &objfile->objfile_obstack,
18649 hashtab_obstack_allocate,
18650 dummy_obstack_deallocate);
f792889a 18651 }
1c379e20 18652
dee91e82 18653 ofs.per_cu = cu->per_cu;
1c379e20
DJ
18654 ofs.offset = die->offset;
18655 ofs.type = type;
dee91e82
DE
18656 slot = (struct dwarf2_per_cu_offset_and_type **)
18657 htab_find_slot (dwarf2_per_objfile->die_type_hash, &ofs, INSERT);
7e314c57
JK
18658 if (*slot)
18659 complaint (&symfile_complaints,
18660 _("A problem internal to GDB: DIE 0x%x has type already set"),
b64f50a1 18661 die->offset.sect_off);
673bfd45 18662 *slot = obstack_alloc (&objfile->objfile_obstack, sizeof (**slot));
1c379e20 18663 **slot = ofs;
f792889a 18664 return type;
1c379e20
DJ
18665}
18666
380bca97 18667/* Look up the type for the die at OFFSET in the appropriate type_hash
673bfd45 18668 table, or return NULL if the die does not have a saved type. */
1c379e20
DJ
18669
18670static struct type *
b64f50a1 18671get_die_type_at_offset (sect_offset offset,
673bfd45 18672 struct dwarf2_per_cu_data *per_cu)
1c379e20 18673{
dee91e82 18674 struct dwarf2_per_cu_offset_and_type *slot, ofs;
f792889a 18675
dee91e82 18676 if (dwarf2_per_objfile->die_type_hash == NULL)
f792889a 18677 return NULL;
1c379e20 18678
dee91e82 18679 ofs.per_cu = per_cu;
673bfd45 18680 ofs.offset = offset;
dee91e82 18681 slot = htab_find (dwarf2_per_objfile->die_type_hash, &ofs);
1c379e20
DJ
18682 if (slot)
18683 return slot->type;
18684 else
18685 return NULL;
18686}
18687
673bfd45
DE
18688/* Look up the type for DIE in the appropriate type_hash table,
18689 or return NULL if DIE does not have a saved type. */
18690
18691static struct type *
18692get_die_type (struct die_info *die, struct dwarf2_cu *cu)
18693{
18694 return get_die_type_at_offset (die->offset, cu->per_cu);
18695}
18696
10b3939b
DJ
18697/* Add a dependence relationship from CU to REF_PER_CU. */
18698
18699static void
18700dwarf2_add_dependence (struct dwarf2_cu *cu,
18701 struct dwarf2_per_cu_data *ref_per_cu)
18702{
18703 void **slot;
18704
18705 if (cu->dependencies == NULL)
18706 cu->dependencies
18707 = htab_create_alloc_ex (5, htab_hash_pointer, htab_eq_pointer,
18708 NULL, &cu->comp_unit_obstack,
18709 hashtab_obstack_allocate,
18710 dummy_obstack_deallocate);
18711
18712 slot = htab_find_slot (cu->dependencies, ref_per_cu, INSERT);
18713 if (*slot == NULL)
18714 *slot = ref_per_cu;
18715}
1c379e20 18716
f504f079
DE
18717/* Subroutine of dwarf2_mark to pass to htab_traverse.
18718 Set the mark field in every compilation unit in the
ae038cb0
DJ
18719 cache that we must keep because we are keeping CU. */
18720
10b3939b
DJ
18721static int
18722dwarf2_mark_helper (void **slot, void *data)
18723{
18724 struct dwarf2_per_cu_data *per_cu;
18725
18726 per_cu = (struct dwarf2_per_cu_data *) *slot;
d07ed419
JK
18727
18728 /* cu->dependencies references may not yet have been ever read if QUIT aborts
18729 reading of the chain. As such dependencies remain valid it is not much
18730 useful to track and undo them during QUIT cleanups. */
18731 if (per_cu->cu == NULL)
18732 return 1;
18733
10b3939b
DJ
18734 if (per_cu->cu->mark)
18735 return 1;
18736 per_cu->cu->mark = 1;
18737
18738 if (per_cu->cu->dependencies != NULL)
18739 htab_traverse (per_cu->cu->dependencies, dwarf2_mark_helper, NULL);
18740
18741 return 1;
18742}
18743
f504f079
DE
18744/* Set the mark field in CU and in every other compilation unit in the
18745 cache that we must keep because we are keeping CU. */
18746
ae038cb0
DJ
18747static void
18748dwarf2_mark (struct dwarf2_cu *cu)
18749{
18750 if (cu->mark)
18751 return;
18752 cu->mark = 1;
10b3939b
DJ
18753 if (cu->dependencies != NULL)
18754 htab_traverse (cu->dependencies, dwarf2_mark_helper, NULL);
ae038cb0
DJ
18755}
18756
18757static void
18758dwarf2_clear_marks (struct dwarf2_per_cu_data *per_cu)
18759{
18760 while (per_cu)
18761 {
18762 per_cu->cu->mark = 0;
18763 per_cu = per_cu->cu->read_in_chain;
18764 }
72bf9492
DJ
18765}
18766
72bf9492
DJ
18767/* Trivial hash function for partial_die_info: the hash value of a DIE
18768 is its offset in .debug_info for this objfile. */
18769
18770static hashval_t
18771partial_die_hash (const void *item)
18772{
18773 const struct partial_die_info *part_die = item;
9a619af0 18774
b64f50a1 18775 return part_die->offset.sect_off;
72bf9492
DJ
18776}
18777
18778/* Trivial comparison function for partial_die_info structures: two DIEs
18779 are equal if they have the same offset. */
18780
18781static int
18782partial_die_eq (const void *item_lhs, const void *item_rhs)
18783{
18784 const struct partial_die_info *part_die_lhs = item_lhs;
18785 const struct partial_die_info *part_die_rhs = item_rhs;
9a619af0 18786
b64f50a1 18787 return part_die_lhs->offset.sect_off == part_die_rhs->offset.sect_off;
72bf9492
DJ
18788}
18789
ae038cb0
DJ
18790static struct cmd_list_element *set_dwarf2_cmdlist;
18791static struct cmd_list_element *show_dwarf2_cmdlist;
18792
18793static void
18794set_dwarf2_cmd (char *args, int from_tty)
18795{
18796 help_list (set_dwarf2_cmdlist, "maintenance set dwarf2 ", -1, gdb_stdout);
18797}
18798
18799static void
18800show_dwarf2_cmd (char *args, int from_tty)
6e70227d 18801{
ae038cb0
DJ
18802 cmd_show_list (show_dwarf2_cmdlist, from_tty, "");
18803}
18804
4bf44c1c 18805/* Free data associated with OBJFILE, if necessary. */
dce234bc
PP
18806
18807static void
c1bd65d0 18808dwarf2_per_objfile_free (struct objfile *objfile, void *d)
dce234bc
PP
18809{
18810 struct dwarf2_per_objfile *data = d;
8b70b953 18811 int ix;
8b70b953 18812
95554aad
TT
18813 for (ix = 0; ix < dwarf2_per_objfile->n_comp_units; ++ix)
18814 VEC_free (dwarf2_per_cu_ptr,
f4dc4d17 18815 dwarf2_per_objfile->all_comp_units[ix]->s.imported_symtabs);
95554aad 18816
8b70b953 18817 VEC_free (dwarf2_section_info_def, data->types);
3019eac3
DE
18818
18819 if (data->dwo_files)
18820 free_dwo_files (data->dwo_files, objfile);
36586728
TT
18821
18822 if (data->dwz_file && data->dwz_file->dwz_bfd)
18823 gdb_bfd_unref (data->dwz_file->dwz_bfd);
9291a0cd
TT
18824}
18825
18826\f
ae2de4f8 18827/* The "save gdb-index" command. */
9291a0cd
TT
18828
18829/* The contents of the hash table we create when building the string
18830 table. */
18831struct strtab_entry
18832{
18833 offset_type offset;
18834 const char *str;
18835};
18836
559a7a62
JK
18837/* Hash function for a strtab_entry.
18838
18839 Function is used only during write_hash_table so no index format backward
18840 compatibility is needed. */
b89be57b 18841
9291a0cd
TT
18842static hashval_t
18843hash_strtab_entry (const void *e)
18844{
18845 const struct strtab_entry *entry = e;
559a7a62 18846 return mapped_index_string_hash (INT_MAX, entry->str);
9291a0cd
TT
18847}
18848
18849/* Equality function for a strtab_entry. */
b89be57b 18850
9291a0cd
TT
18851static int
18852eq_strtab_entry (const void *a, const void *b)
18853{
18854 const struct strtab_entry *ea = a;
18855 const struct strtab_entry *eb = b;
18856 return !strcmp (ea->str, eb->str);
18857}
18858
18859/* Create a strtab_entry hash table. */
b89be57b 18860
9291a0cd
TT
18861static htab_t
18862create_strtab (void)
18863{
18864 return htab_create_alloc (100, hash_strtab_entry, eq_strtab_entry,
18865 xfree, xcalloc, xfree);
18866}
18867
18868/* Add a string to the constant pool. Return the string's offset in
18869 host order. */
b89be57b 18870
9291a0cd
TT
18871static offset_type
18872add_string (htab_t table, struct obstack *cpool, const char *str)
18873{
18874 void **slot;
18875 struct strtab_entry entry;
18876 struct strtab_entry *result;
18877
18878 entry.str = str;
18879 slot = htab_find_slot (table, &entry, INSERT);
18880 if (*slot)
18881 result = *slot;
18882 else
18883 {
18884 result = XNEW (struct strtab_entry);
18885 result->offset = obstack_object_size (cpool);
18886 result->str = str;
18887 obstack_grow_str0 (cpool, str);
18888 *slot = result;
18889 }
18890 return result->offset;
18891}
18892
18893/* An entry in the symbol table. */
18894struct symtab_index_entry
18895{
18896 /* The name of the symbol. */
18897 const char *name;
18898 /* The offset of the name in the constant pool. */
18899 offset_type index_offset;
18900 /* A sorted vector of the indices of all the CUs that hold an object
18901 of this name. */
18902 VEC (offset_type) *cu_indices;
18903};
18904
18905/* The symbol table. This is a power-of-2-sized hash table. */
18906struct mapped_symtab
18907{
18908 offset_type n_elements;
18909 offset_type size;
18910 struct symtab_index_entry **data;
18911};
18912
18913/* Hash function for a symtab_index_entry. */
b89be57b 18914
9291a0cd
TT
18915static hashval_t
18916hash_symtab_entry (const void *e)
18917{
18918 const struct symtab_index_entry *entry = e;
18919 return iterative_hash (VEC_address (offset_type, entry->cu_indices),
18920 sizeof (offset_type) * VEC_length (offset_type,
18921 entry->cu_indices),
18922 0);
18923}
18924
18925/* Equality function for a symtab_index_entry. */
b89be57b 18926
9291a0cd
TT
18927static int
18928eq_symtab_entry (const void *a, const void *b)
18929{
18930 const struct symtab_index_entry *ea = a;
18931 const struct symtab_index_entry *eb = b;
18932 int len = VEC_length (offset_type, ea->cu_indices);
18933 if (len != VEC_length (offset_type, eb->cu_indices))
18934 return 0;
18935 return !memcmp (VEC_address (offset_type, ea->cu_indices),
18936 VEC_address (offset_type, eb->cu_indices),
18937 sizeof (offset_type) * len);
18938}
18939
18940/* Destroy a symtab_index_entry. */
b89be57b 18941
9291a0cd
TT
18942static void
18943delete_symtab_entry (void *p)
18944{
18945 struct symtab_index_entry *entry = p;
18946 VEC_free (offset_type, entry->cu_indices);
18947 xfree (entry);
18948}
18949
18950/* Create a hash table holding symtab_index_entry objects. */
b89be57b 18951
9291a0cd 18952static htab_t
3876f04e 18953create_symbol_hash_table (void)
9291a0cd
TT
18954{
18955 return htab_create_alloc (100, hash_symtab_entry, eq_symtab_entry,
18956 delete_symtab_entry, xcalloc, xfree);
18957}
18958
18959/* Create a new mapped symtab object. */
b89be57b 18960
9291a0cd
TT
18961static struct mapped_symtab *
18962create_mapped_symtab (void)
18963{
18964 struct mapped_symtab *symtab = XNEW (struct mapped_symtab);
18965 symtab->n_elements = 0;
18966 symtab->size = 1024;
18967 symtab->data = XCNEWVEC (struct symtab_index_entry *, symtab->size);
18968 return symtab;
18969}
18970
18971/* Destroy a mapped_symtab. */
b89be57b 18972
9291a0cd
TT
18973static void
18974cleanup_mapped_symtab (void *p)
18975{
18976 struct mapped_symtab *symtab = p;
18977 /* The contents of the array are freed when the other hash table is
18978 destroyed. */
18979 xfree (symtab->data);
18980 xfree (symtab);
18981}
18982
18983/* Find a slot in SYMTAB for the symbol NAME. Returns a pointer to
559a7a62
JK
18984 the slot.
18985
18986 Function is used only during write_hash_table so no index format backward
18987 compatibility is needed. */
b89be57b 18988
9291a0cd
TT
18989static struct symtab_index_entry **
18990find_slot (struct mapped_symtab *symtab, const char *name)
18991{
559a7a62 18992 offset_type index, step, hash = mapped_index_string_hash (INT_MAX, name);
9291a0cd
TT
18993
18994 index = hash & (symtab->size - 1);
18995 step = ((hash * 17) & (symtab->size - 1)) | 1;
18996
18997 for (;;)
18998 {
18999 if (!symtab->data[index] || !strcmp (name, symtab->data[index]->name))
19000 return &symtab->data[index];
19001 index = (index + step) & (symtab->size - 1);
19002 }
19003}
19004
19005/* Expand SYMTAB's hash table. */
b89be57b 19006
9291a0cd
TT
19007static void
19008hash_expand (struct mapped_symtab *symtab)
19009{
19010 offset_type old_size = symtab->size;
19011 offset_type i;
19012 struct symtab_index_entry **old_entries = symtab->data;
19013
19014 symtab->size *= 2;
19015 symtab->data = XCNEWVEC (struct symtab_index_entry *, symtab->size);
19016
19017 for (i = 0; i < old_size; ++i)
19018 {
19019 if (old_entries[i])
19020 {
19021 struct symtab_index_entry **slot = find_slot (symtab,
19022 old_entries[i]->name);
19023 *slot = old_entries[i];
19024 }
19025 }
19026
19027 xfree (old_entries);
19028}
19029
156942c7
DE
19030/* Add an entry to SYMTAB. NAME is the name of the symbol.
19031 CU_INDEX is the index of the CU in which the symbol appears.
19032 IS_STATIC is one if the symbol is static, otherwise zero (global). */
b89be57b 19033
9291a0cd
TT
19034static void
19035add_index_entry (struct mapped_symtab *symtab, const char *name,
156942c7 19036 int is_static, gdb_index_symbol_kind kind,
9291a0cd
TT
19037 offset_type cu_index)
19038{
19039 struct symtab_index_entry **slot;
156942c7 19040 offset_type cu_index_and_attrs;
9291a0cd
TT
19041
19042 ++symtab->n_elements;
19043 if (4 * symtab->n_elements / 3 >= symtab->size)
19044 hash_expand (symtab);
19045
19046 slot = find_slot (symtab, name);
19047 if (!*slot)
19048 {
19049 *slot = XNEW (struct symtab_index_entry);
19050 (*slot)->name = name;
156942c7 19051 /* index_offset is set later. */
9291a0cd
TT
19052 (*slot)->cu_indices = NULL;
19053 }
156942c7
DE
19054
19055 cu_index_and_attrs = 0;
19056 DW2_GDB_INDEX_CU_SET_VALUE (cu_index_and_attrs, cu_index);
19057 DW2_GDB_INDEX_SYMBOL_STATIC_SET_VALUE (cu_index_and_attrs, is_static);
19058 DW2_GDB_INDEX_SYMBOL_KIND_SET_VALUE (cu_index_and_attrs, kind);
19059
19060 /* We don't want to record an index value twice as we want to avoid the
19061 duplication.
19062 We process all global symbols and then all static symbols
19063 (which would allow us to avoid the duplication by only having to check
19064 the last entry pushed), but a symbol could have multiple kinds in one CU.
19065 To keep things simple we don't worry about the duplication here and
19066 sort and uniqufy the list after we've processed all symbols. */
19067 VEC_safe_push (offset_type, (*slot)->cu_indices, cu_index_and_attrs);
19068}
19069
19070/* qsort helper routine for uniquify_cu_indices. */
19071
19072static int
19073offset_type_compare (const void *ap, const void *bp)
19074{
19075 offset_type a = *(offset_type *) ap;
19076 offset_type b = *(offset_type *) bp;
19077
19078 return (a > b) - (b > a);
19079}
19080
19081/* Sort and remove duplicates of all symbols' cu_indices lists. */
19082
19083static void
19084uniquify_cu_indices (struct mapped_symtab *symtab)
19085{
19086 int i;
19087
19088 for (i = 0; i < symtab->size; ++i)
19089 {
19090 struct symtab_index_entry *entry = symtab->data[i];
19091
19092 if (entry
19093 && entry->cu_indices != NULL)
19094 {
19095 unsigned int next_to_insert, next_to_check;
19096 offset_type last_value;
19097
19098 qsort (VEC_address (offset_type, entry->cu_indices),
19099 VEC_length (offset_type, entry->cu_indices),
19100 sizeof (offset_type), offset_type_compare);
19101
19102 last_value = VEC_index (offset_type, entry->cu_indices, 0);
19103 next_to_insert = 1;
19104 for (next_to_check = 1;
19105 next_to_check < VEC_length (offset_type, entry->cu_indices);
19106 ++next_to_check)
19107 {
19108 if (VEC_index (offset_type, entry->cu_indices, next_to_check)
19109 != last_value)
19110 {
19111 last_value = VEC_index (offset_type, entry->cu_indices,
19112 next_to_check);
19113 VEC_replace (offset_type, entry->cu_indices, next_to_insert,
19114 last_value);
19115 ++next_to_insert;
19116 }
19117 }
19118 VEC_truncate (offset_type, entry->cu_indices, next_to_insert);
19119 }
19120 }
9291a0cd
TT
19121}
19122
19123/* Add a vector of indices to the constant pool. */
b89be57b 19124
9291a0cd 19125static offset_type
3876f04e 19126add_indices_to_cpool (htab_t symbol_hash_table, struct obstack *cpool,
9291a0cd
TT
19127 struct symtab_index_entry *entry)
19128{
19129 void **slot;
19130
3876f04e 19131 slot = htab_find_slot (symbol_hash_table, entry, INSERT);
9291a0cd
TT
19132 if (!*slot)
19133 {
19134 offset_type len = VEC_length (offset_type, entry->cu_indices);
19135 offset_type val = MAYBE_SWAP (len);
19136 offset_type iter;
19137 int i;
19138
19139 *slot = entry;
19140 entry->index_offset = obstack_object_size (cpool);
19141
19142 obstack_grow (cpool, &val, sizeof (val));
19143 for (i = 0;
19144 VEC_iterate (offset_type, entry->cu_indices, i, iter);
19145 ++i)
19146 {
19147 val = MAYBE_SWAP (iter);
19148 obstack_grow (cpool, &val, sizeof (val));
19149 }
19150 }
19151 else
19152 {
19153 struct symtab_index_entry *old_entry = *slot;
19154 entry->index_offset = old_entry->index_offset;
19155 entry = old_entry;
19156 }
19157 return entry->index_offset;
19158}
19159
19160/* Write the mapped hash table SYMTAB to the obstack OUTPUT, with
19161 constant pool entries going into the obstack CPOOL. */
b89be57b 19162
9291a0cd
TT
19163static void
19164write_hash_table (struct mapped_symtab *symtab,
19165 struct obstack *output, struct obstack *cpool)
19166{
19167 offset_type i;
3876f04e 19168 htab_t symbol_hash_table;
9291a0cd
TT
19169 htab_t str_table;
19170
3876f04e 19171 symbol_hash_table = create_symbol_hash_table ();
9291a0cd 19172 str_table = create_strtab ();
3876f04e 19173
9291a0cd
TT
19174 /* We add all the index vectors to the constant pool first, to
19175 ensure alignment is ok. */
19176 for (i = 0; i < symtab->size; ++i)
19177 {
19178 if (symtab->data[i])
3876f04e 19179 add_indices_to_cpool (symbol_hash_table, cpool, symtab->data[i]);
9291a0cd
TT
19180 }
19181
19182 /* Now write out the hash table. */
19183 for (i = 0; i < symtab->size; ++i)
19184 {
19185 offset_type str_off, vec_off;
19186
19187 if (symtab->data[i])
19188 {
19189 str_off = add_string (str_table, cpool, symtab->data[i]->name);
19190 vec_off = symtab->data[i]->index_offset;
19191 }
19192 else
19193 {
19194 /* While 0 is a valid constant pool index, it is not valid
19195 to have 0 for both offsets. */
19196 str_off = 0;
19197 vec_off = 0;
19198 }
19199
19200 str_off = MAYBE_SWAP (str_off);
19201 vec_off = MAYBE_SWAP (vec_off);
19202
19203 obstack_grow (output, &str_off, sizeof (str_off));
19204 obstack_grow (output, &vec_off, sizeof (vec_off));
19205 }
19206
19207 htab_delete (str_table);
3876f04e 19208 htab_delete (symbol_hash_table);
9291a0cd
TT
19209}
19210
0a5429f6
DE
19211/* Struct to map psymtab to CU index in the index file. */
19212struct psymtab_cu_index_map
19213{
19214 struct partial_symtab *psymtab;
19215 unsigned int cu_index;
19216};
19217
19218static hashval_t
19219hash_psymtab_cu_index (const void *item)
19220{
19221 const struct psymtab_cu_index_map *map = item;
19222
19223 return htab_hash_pointer (map->psymtab);
19224}
19225
19226static int
19227eq_psymtab_cu_index (const void *item_lhs, const void *item_rhs)
19228{
19229 const struct psymtab_cu_index_map *lhs = item_lhs;
19230 const struct psymtab_cu_index_map *rhs = item_rhs;
19231
19232 return lhs->psymtab == rhs->psymtab;
19233}
19234
19235/* Helper struct for building the address table. */
19236struct addrmap_index_data
19237{
19238 struct objfile *objfile;
19239 struct obstack *addr_obstack;
19240 htab_t cu_index_htab;
19241
19242 /* Non-zero if the previous_* fields are valid.
19243 We can't write an entry until we see the next entry (since it is only then
19244 that we know the end of the entry). */
19245 int previous_valid;
19246 /* Index of the CU in the table of all CUs in the index file. */
19247 unsigned int previous_cu_index;
0963b4bd 19248 /* Start address of the CU. */
0a5429f6
DE
19249 CORE_ADDR previous_cu_start;
19250};
19251
19252/* Write an address entry to OBSTACK. */
b89be57b 19253
9291a0cd 19254static void
0a5429f6
DE
19255add_address_entry (struct objfile *objfile, struct obstack *obstack,
19256 CORE_ADDR start, CORE_ADDR end, unsigned int cu_index)
9291a0cd 19257{
0a5429f6 19258 offset_type cu_index_to_write;
9291a0cd
TT
19259 char addr[8];
19260 CORE_ADDR baseaddr;
19261
19262 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
19263
0a5429f6
DE
19264 store_unsigned_integer (addr, 8, BFD_ENDIAN_LITTLE, start - baseaddr);
19265 obstack_grow (obstack, addr, 8);
19266 store_unsigned_integer (addr, 8, BFD_ENDIAN_LITTLE, end - baseaddr);
19267 obstack_grow (obstack, addr, 8);
19268 cu_index_to_write = MAYBE_SWAP (cu_index);
19269 obstack_grow (obstack, &cu_index_to_write, sizeof (offset_type));
19270}
19271
19272/* Worker function for traversing an addrmap to build the address table. */
19273
19274static int
19275add_address_entry_worker (void *datap, CORE_ADDR start_addr, void *obj)
19276{
19277 struct addrmap_index_data *data = datap;
19278 struct partial_symtab *pst = obj;
0a5429f6
DE
19279
19280 if (data->previous_valid)
19281 add_address_entry (data->objfile, data->addr_obstack,
19282 data->previous_cu_start, start_addr,
19283 data->previous_cu_index);
19284
19285 data->previous_cu_start = start_addr;
19286 if (pst != NULL)
19287 {
19288 struct psymtab_cu_index_map find_map, *map;
19289 find_map.psymtab = pst;
19290 map = htab_find (data->cu_index_htab, &find_map);
19291 gdb_assert (map != NULL);
19292 data->previous_cu_index = map->cu_index;
19293 data->previous_valid = 1;
19294 }
19295 else
19296 data->previous_valid = 0;
19297
19298 return 0;
19299}
19300
19301/* Write OBJFILE's address map to OBSTACK.
19302 CU_INDEX_HTAB is used to map addrmap entries to their CU indices
19303 in the index file. */
19304
19305static void
19306write_address_map (struct objfile *objfile, struct obstack *obstack,
19307 htab_t cu_index_htab)
19308{
19309 struct addrmap_index_data addrmap_index_data;
19310
19311 /* When writing the address table, we have to cope with the fact that
19312 the addrmap iterator only provides the start of a region; we have to
19313 wait until the next invocation to get the start of the next region. */
19314
19315 addrmap_index_data.objfile = objfile;
19316 addrmap_index_data.addr_obstack = obstack;
19317 addrmap_index_data.cu_index_htab = cu_index_htab;
19318 addrmap_index_data.previous_valid = 0;
19319
19320 addrmap_foreach (objfile->psymtabs_addrmap, add_address_entry_worker,
19321 &addrmap_index_data);
19322
19323 /* It's highly unlikely the last entry (end address = 0xff...ff)
19324 is valid, but we should still handle it.
19325 The end address is recorded as the start of the next region, but that
19326 doesn't work here. To cope we pass 0xff...ff, this is a rare situation
19327 anyway. */
19328 if (addrmap_index_data.previous_valid)
19329 add_address_entry (objfile, obstack,
19330 addrmap_index_data.previous_cu_start, (CORE_ADDR) -1,
19331 addrmap_index_data.previous_cu_index);
9291a0cd
TT
19332}
19333
156942c7
DE
19334/* Return the symbol kind of PSYM. */
19335
19336static gdb_index_symbol_kind
19337symbol_kind (struct partial_symbol *psym)
19338{
19339 domain_enum domain = PSYMBOL_DOMAIN (psym);
19340 enum address_class aclass = PSYMBOL_CLASS (psym);
19341
19342 switch (domain)
19343 {
19344 case VAR_DOMAIN:
19345 switch (aclass)
19346 {
19347 case LOC_BLOCK:
19348 return GDB_INDEX_SYMBOL_KIND_FUNCTION;
19349 case LOC_TYPEDEF:
19350 return GDB_INDEX_SYMBOL_KIND_TYPE;
19351 case LOC_COMPUTED:
19352 case LOC_CONST_BYTES:
19353 case LOC_OPTIMIZED_OUT:
19354 case LOC_STATIC:
19355 return GDB_INDEX_SYMBOL_KIND_VARIABLE;
19356 case LOC_CONST:
19357 /* Note: It's currently impossible to recognize psyms as enum values
19358 short of reading the type info. For now punt. */
19359 return GDB_INDEX_SYMBOL_KIND_VARIABLE;
19360 default:
19361 /* There are other LOC_FOO values that one might want to classify
19362 as variables, but dwarf2read.c doesn't currently use them. */
19363 return GDB_INDEX_SYMBOL_KIND_OTHER;
19364 }
19365 case STRUCT_DOMAIN:
19366 return GDB_INDEX_SYMBOL_KIND_TYPE;
19367 default:
19368 return GDB_INDEX_SYMBOL_KIND_OTHER;
19369 }
19370}
19371
9291a0cd 19372/* Add a list of partial symbols to SYMTAB. */
b89be57b 19373
9291a0cd
TT
19374static void
19375write_psymbols (struct mapped_symtab *symtab,
987d643c 19376 htab_t psyms_seen,
9291a0cd
TT
19377 struct partial_symbol **psymp,
19378 int count,
987d643c
TT
19379 offset_type cu_index,
19380 int is_static)
9291a0cd
TT
19381{
19382 for (; count-- > 0; ++psymp)
19383 {
156942c7
DE
19384 struct partial_symbol *psym = *psymp;
19385 void **slot;
987d643c 19386
156942c7 19387 if (SYMBOL_LANGUAGE (psym) == language_ada)
9291a0cd 19388 error (_("Ada is not currently supported by the index"));
987d643c 19389
987d643c 19390 /* Only add a given psymbol once. */
156942c7 19391 slot = htab_find_slot (psyms_seen, psym, INSERT);
987d643c
TT
19392 if (!*slot)
19393 {
156942c7
DE
19394 gdb_index_symbol_kind kind = symbol_kind (psym);
19395
19396 *slot = psym;
19397 add_index_entry (symtab, SYMBOL_SEARCH_NAME (psym),
19398 is_static, kind, cu_index);
987d643c 19399 }
9291a0cd
TT
19400 }
19401}
19402
19403/* Write the contents of an ("unfinished") obstack to FILE. Throw an
19404 exception if there is an error. */
b89be57b 19405
9291a0cd
TT
19406static void
19407write_obstack (FILE *file, struct obstack *obstack)
19408{
19409 if (fwrite (obstack_base (obstack), 1, obstack_object_size (obstack),
19410 file)
19411 != obstack_object_size (obstack))
19412 error (_("couldn't data write to file"));
19413}
19414
19415/* Unlink a file if the argument is not NULL. */
b89be57b 19416
9291a0cd
TT
19417static void
19418unlink_if_set (void *p)
19419{
19420 char **filename = p;
19421 if (*filename)
19422 unlink (*filename);
19423}
19424
1fd400ff
TT
19425/* A helper struct used when iterating over debug_types. */
19426struct signatured_type_index_data
19427{
19428 struct objfile *objfile;
19429 struct mapped_symtab *symtab;
19430 struct obstack *types_list;
987d643c 19431 htab_t psyms_seen;
1fd400ff
TT
19432 int cu_index;
19433};
19434
19435/* A helper function that writes a single signatured_type to an
19436 obstack. */
b89be57b 19437
1fd400ff
TT
19438static int
19439write_one_signatured_type (void **slot, void *d)
19440{
19441 struct signatured_type_index_data *info = d;
19442 struct signatured_type *entry = (struct signatured_type *) *slot;
e254ef6a
DE
19443 struct dwarf2_per_cu_data *per_cu = &entry->per_cu;
19444 struct partial_symtab *psymtab = per_cu->v.psymtab;
1fd400ff
TT
19445 gdb_byte val[8];
19446
19447 write_psymbols (info->symtab,
987d643c 19448 info->psyms_seen,
3e43a32a
MS
19449 info->objfile->global_psymbols.list
19450 + psymtab->globals_offset,
987d643c
TT
19451 psymtab->n_global_syms, info->cu_index,
19452 0);
1fd400ff 19453 write_psymbols (info->symtab,
987d643c 19454 info->psyms_seen,
3e43a32a
MS
19455 info->objfile->static_psymbols.list
19456 + psymtab->statics_offset,
987d643c
TT
19457 psymtab->n_static_syms, info->cu_index,
19458 1);
1fd400ff 19459
b64f50a1
JK
19460 store_unsigned_integer (val, 8, BFD_ENDIAN_LITTLE,
19461 entry->per_cu.offset.sect_off);
1fd400ff 19462 obstack_grow (info->types_list, val, 8);
3019eac3
DE
19463 store_unsigned_integer (val, 8, BFD_ENDIAN_LITTLE,
19464 entry->type_offset_in_tu.cu_off);
1fd400ff
TT
19465 obstack_grow (info->types_list, val, 8);
19466 store_unsigned_integer (val, 8, BFD_ENDIAN_LITTLE, entry->signature);
19467 obstack_grow (info->types_list, val, 8);
19468
19469 ++info->cu_index;
19470
19471 return 1;
19472}
19473
95554aad
TT
19474/* Recurse into all "included" dependencies and write their symbols as
19475 if they appeared in this psymtab. */
19476
19477static void
19478recursively_write_psymbols (struct objfile *objfile,
19479 struct partial_symtab *psymtab,
19480 struct mapped_symtab *symtab,
19481 htab_t psyms_seen,
19482 offset_type cu_index)
19483{
19484 int i;
19485
19486 for (i = 0; i < psymtab->number_of_dependencies; ++i)
19487 if (psymtab->dependencies[i]->user != NULL)
19488 recursively_write_psymbols (objfile, psymtab->dependencies[i],
19489 symtab, psyms_seen, cu_index);
19490
19491 write_psymbols (symtab,
19492 psyms_seen,
19493 objfile->global_psymbols.list + psymtab->globals_offset,
19494 psymtab->n_global_syms, cu_index,
19495 0);
19496 write_psymbols (symtab,
19497 psyms_seen,
19498 objfile->static_psymbols.list + psymtab->statics_offset,
19499 psymtab->n_static_syms, cu_index,
19500 1);
19501}
19502
9291a0cd 19503/* Create an index file for OBJFILE in the directory DIR. */
b89be57b 19504
9291a0cd
TT
19505static void
19506write_psymtabs_to_index (struct objfile *objfile, const char *dir)
19507{
19508 struct cleanup *cleanup;
19509 char *filename, *cleanup_filename;
1fd400ff
TT
19510 struct obstack contents, addr_obstack, constant_pool, symtab_obstack;
19511 struct obstack cu_list, types_cu_list;
9291a0cd
TT
19512 int i;
19513 FILE *out_file;
19514 struct mapped_symtab *symtab;
19515 offset_type val, size_of_contents, total_len;
19516 struct stat st;
987d643c 19517 htab_t psyms_seen;
0a5429f6
DE
19518 htab_t cu_index_htab;
19519 struct psymtab_cu_index_map *psymtab_cu_index_map;
9291a0cd 19520
b4f2f049 19521 if (!objfile->psymtabs || !objfile->psymtabs_addrmap)
9291a0cd 19522 return;
b4f2f049 19523
9291a0cd
TT
19524 if (dwarf2_per_objfile->using_index)
19525 error (_("Cannot use an index to create the index"));
19526
8b70b953
TT
19527 if (VEC_length (dwarf2_section_info_def, dwarf2_per_objfile->types) > 1)
19528 error (_("Cannot make an index when the file has multiple .debug_types sections"));
19529
9291a0cd 19530 if (stat (objfile->name, &st) < 0)
7e17e088 19531 perror_with_name (objfile->name);
9291a0cd
TT
19532
19533 filename = concat (dir, SLASH_STRING, lbasename (objfile->name),
19534 INDEX_SUFFIX, (char *) NULL);
19535 cleanup = make_cleanup (xfree, filename);
19536
19537 out_file = fopen (filename, "wb");
19538 if (!out_file)
19539 error (_("Can't open `%s' for writing"), filename);
19540
19541 cleanup_filename = filename;
19542 make_cleanup (unlink_if_set, &cleanup_filename);
19543
19544 symtab = create_mapped_symtab ();
19545 make_cleanup (cleanup_mapped_symtab, symtab);
19546
19547 obstack_init (&addr_obstack);
19548 make_cleanup_obstack_free (&addr_obstack);
19549
19550 obstack_init (&cu_list);
19551 make_cleanup_obstack_free (&cu_list);
19552
1fd400ff
TT
19553 obstack_init (&types_cu_list);
19554 make_cleanup_obstack_free (&types_cu_list);
19555
987d643c
TT
19556 psyms_seen = htab_create_alloc (100, htab_hash_pointer, htab_eq_pointer,
19557 NULL, xcalloc, xfree);
96408a79 19558 make_cleanup_htab_delete (psyms_seen);
987d643c 19559
0a5429f6
DE
19560 /* While we're scanning CU's create a table that maps a psymtab pointer
19561 (which is what addrmap records) to its index (which is what is recorded
19562 in the index file). This will later be needed to write the address
19563 table. */
19564 cu_index_htab = htab_create_alloc (100,
19565 hash_psymtab_cu_index,
19566 eq_psymtab_cu_index,
19567 NULL, xcalloc, xfree);
96408a79 19568 make_cleanup_htab_delete (cu_index_htab);
0a5429f6
DE
19569 psymtab_cu_index_map = (struct psymtab_cu_index_map *)
19570 xmalloc (sizeof (struct psymtab_cu_index_map)
19571 * dwarf2_per_objfile->n_comp_units);
19572 make_cleanup (xfree, psymtab_cu_index_map);
19573
19574 /* The CU list is already sorted, so we don't need to do additional
1fd400ff
TT
19575 work here. Also, the debug_types entries do not appear in
19576 all_comp_units, but only in their own hash table. */
9291a0cd
TT
19577 for (i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
19578 {
3e43a32a
MS
19579 struct dwarf2_per_cu_data *per_cu
19580 = dwarf2_per_objfile->all_comp_units[i];
e254ef6a 19581 struct partial_symtab *psymtab = per_cu->v.psymtab;
9291a0cd 19582 gdb_byte val[8];
0a5429f6
DE
19583 struct psymtab_cu_index_map *map;
19584 void **slot;
9291a0cd 19585
95554aad
TT
19586 if (psymtab->user == NULL)
19587 recursively_write_psymbols (objfile, psymtab, symtab, psyms_seen, i);
9291a0cd 19588
0a5429f6
DE
19589 map = &psymtab_cu_index_map[i];
19590 map->psymtab = psymtab;
19591 map->cu_index = i;
19592 slot = htab_find_slot (cu_index_htab, map, INSERT);
19593 gdb_assert (slot != NULL);
19594 gdb_assert (*slot == NULL);
19595 *slot = map;
9291a0cd 19596
b64f50a1
JK
19597 store_unsigned_integer (val, 8, BFD_ENDIAN_LITTLE,
19598 per_cu->offset.sect_off);
9291a0cd 19599 obstack_grow (&cu_list, val, 8);
e254ef6a 19600 store_unsigned_integer (val, 8, BFD_ENDIAN_LITTLE, per_cu->length);
9291a0cd
TT
19601 obstack_grow (&cu_list, val, 8);
19602 }
19603
0a5429f6
DE
19604 /* Dump the address map. */
19605 write_address_map (objfile, &addr_obstack, cu_index_htab);
19606
1fd400ff
TT
19607 /* Write out the .debug_type entries, if any. */
19608 if (dwarf2_per_objfile->signatured_types)
19609 {
19610 struct signatured_type_index_data sig_data;
19611
19612 sig_data.objfile = objfile;
19613 sig_data.symtab = symtab;
19614 sig_data.types_list = &types_cu_list;
987d643c 19615 sig_data.psyms_seen = psyms_seen;
1fd400ff
TT
19616 sig_data.cu_index = dwarf2_per_objfile->n_comp_units;
19617 htab_traverse_noresize (dwarf2_per_objfile->signatured_types,
19618 write_one_signatured_type, &sig_data);
19619 }
19620
156942c7
DE
19621 /* Now that we've processed all symbols we can shrink their cu_indices
19622 lists. */
19623 uniquify_cu_indices (symtab);
19624
9291a0cd
TT
19625 obstack_init (&constant_pool);
19626 make_cleanup_obstack_free (&constant_pool);
19627 obstack_init (&symtab_obstack);
19628 make_cleanup_obstack_free (&symtab_obstack);
19629 write_hash_table (symtab, &symtab_obstack, &constant_pool);
19630
19631 obstack_init (&contents);
19632 make_cleanup_obstack_free (&contents);
1fd400ff 19633 size_of_contents = 6 * sizeof (offset_type);
9291a0cd
TT
19634 total_len = size_of_contents;
19635
19636 /* The version number. */
156942c7 19637 val = MAYBE_SWAP (7);
9291a0cd
TT
19638 obstack_grow (&contents, &val, sizeof (val));
19639
19640 /* The offset of the CU list from the start of the file. */
19641 val = MAYBE_SWAP (total_len);
19642 obstack_grow (&contents, &val, sizeof (val));
19643 total_len += obstack_object_size (&cu_list);
19644
1fd400ff
TT
19645 /* The offset of the types CU list from the start of the file. */
19646 val = MAYBE_SWAP (total_len);
19647 obstack_grow (&contents, &val, sizeof (val));
19648 total_len += obstack_object_size (&types_cu_list);
19649
9291a0cd
TT
19650 /* The offset of the address table from the start of the file. */
19651 val = MAYBE_SWAP (total_len);
19652 obstack_grow (&contents, &val, sizeof (val));
19653 total_len += obstack_object_size (&addr_obstack);
19654
19655 /* The offset of the symbol table from the start of the file. */
19656 val = MAYBE_SWAP (total_len);
19657 obstack_grow (&contents, &val, sizeof (val));
19658 total_len += obstack_object_size (&symtab_obstack);
19659
19660 /* The offset of the constant pool from the start of the file. */
19661 val = MAYBE_SWAP (total_len);
19662 obstack_grow (&contents, &val, sizeof (val));
19663 total_len += obstack_object_size (&constant_pool);
19664
19665 gdb_assert (obstack_object_size (&contents) == size_of_contents);
19666
19667 write_obstack (out_file, &contents);
19668 write_obstack (out_file, &cu_list);
1fd400ff 19669 write_obstack (out_file, &types_cu_list);
9291a0cd
TT
19670 write_obstack (out_file, &addr_obstack);
19671 write_obstack (out_file, &symtab_obstack);
19672 write_obstack (out_file, &constant_pool);
19673
19674 fclose (out_file);
19675
19676 /* We want to keep the file, so we set cleanup_filename to NULL
19677 here. See unlink_if_set. */
19678 cleanup_filename = NULL;
19679
19680 do_cleanups (cleanup);
19681}
19682
90476074
TT
19683/* Implementation of the `save gdb-index' command.
19684
19685 Note that the file format used by this command is documented in the
19686 GDB manual. Any changes here must be documented there. */
11570e71 19687
9291a0cd
TT
19688static void
19689save_gdb_index_command (char *arg, int from_tty)
19690{
19691 struct objfile *objfile;
19692
19693 if (!arg || !*arg)
96d19272 19694 error (_("usage: save gdb-index DIRECTORY"));
9291a0cd
TT
19695
19696 ALL_OBJFILES (objfile)
19697 {
19698 struct stat st;
19699
19700 /* If the objfile does not correspond to an actual file, skip it. */
19701 if (stat (objfile->name, &st) < 0)
19702 continue;
19703
19704 dwarf2_per_objfile = objfile_data (objfile, dwarf2_objfile_data_key);
19705 if (dwarf2_per_objfile)
19706 {
19707 volatile struct gdb_exception except;
19708
19709 TRY_CATCH (except, RETURN_MASK_ERROR)
19710 {
19711 write_psymtabs_to_index (objfile, arg);
19712 }
19713 if (except.reason < 0)
19714 exception_fprintf (gdb_stderr, except,
19715 _("Error while writing index for `%s': "),
19716 objfile->name);
19717 }
19718 }
dce234bc
PP
19719}
19720
9291a0cd
TT
19721\f
19722
9eae7c52
TT
19723int dwarf2_always_disassemble;
19724
19725static void
19726show_dwarf2_always_disassemble (struct ui_file *file, int from_tty,
19727 struct cmd_list_element *c, const char *value)
19728{
3e43a32a
MS
19729 fprintf_filtered (file,
19730 _("Whether to always disassemble "
19731 "DWARF expressions is %s.\n"),
9eae7c52
TT
19732 value);
19733}
19734
900e11f9
JK
19735static void
19736show_check_physname (struct ui_file *file, int from_tty,
19737 struct cmd_list_element *c, const char *value)
19738{
19739 fprintf_filtered (file,
19740 _("Whether to check \"physname\" is %s.\n"),
19741 value);
19742}
19743
6502dd73
DJ
19744void _initialize_dwarf2_read (void);
19745
19746void
19747_initialize_dwarf2_read (void)
19748{
96d19272
JK
19749 struct cmd_list_element *c;
19750
dce234bc 19751 dwarf2_objfile_data_key
c1bd65d0 19752 = register_objfile_data_with_cleanup (NULL, dwarf2_per_objfile_free);
ae038cb0 19753
1bedd215
AC
19754 add_prefix_cmd ("dwarf2", class_maintenance, set_dwarf2_cmd, _("\
19755Set DWARF 2 specific variables.\n\
19756Configure DWARF 2 variables such as the cache size"),
ae038cb0
DJ
19757 &set_dwarf2_cmdlist, "maintenance set dwarf2 ",
19758 0/*allow-unknown*/, &maintenance_set_cmdlist);
19759
1bedd215
AC
19760 add_prefix_cmd ("dwarf2", class_maintenance, show_dwarf2_cmd, _("\
19761Show DWARF 2 specific variables\n\
19762Show DWARF 2 variables such as the cache size"),
ae038cb0
DJ
19763 &show_dwarf2_cmdlist, "maintenance show dwarf2 ",
19764 0/*allow-unknown*/, &maintenance_show_cmdlist);
19765
19766 add_setshow_zinteger_cmd ("max-cache-age", class_obscure,
7915a72c
AC
19767 &dwarf2_max_cache_age, _("\
19768Set the upper bound on the age of cached dwarf2 compilation units."), _("\
19769Show the upper bound on the age of cached dwarf2 compilation units."), _("\
19770A higher limit means that cached compilation units will be stored\n\
19771in memory longer, and more total memory will be used. Zero disables\n\
19772caching, which can slow down startup."),
2c5b56ce 19773 NULL,
920d2a44 19774 show_dwarf2_max_cache_age,
2c5b56ce 19775 &set_dwarf2_cmdlist,
ae038cb0 19776 &show_dwarf2_cmdlist);
d97bc12b 19777
9eae7c52
TT
19778 add_setshow_boolean_cmd ("always-disassemble", class_obscure,
19779 &dwarf2_always_disassemble, _("\
19780Set whether `info address' always disassembles DWARF expressions."), _("\
19781Show whether `info address' always disassembles DWARF expressions."), _("\
19782When enabled, DWARF expressions are always printed in an assembly-like\n\
19783syntax. When disabled, expressions will be printed in a more\n\
19784conversational style, when possible."),
19785 NULL,
19786 show_dwarf2_always_disassemble,
19787 &set_dwarf2_cmdlist,
19788 &show_dwarf2_cmdlist);
19789
45cfd468
DE
19790 add_setshow_boolean_cmd ("dwarf2-read", no_class, &dwarf2_read_debug, _("\
19791Set debugging of the dwarf2 reader."), _("\
19792Show debugging of the dwarf2 reader."), _("\
19793When enabled, debugging messages are printed during dwarf2 reading\n\
19794and symtab expansion."),
19795 NULL,
19796 NULL,
19797 &setdebuglist, &showdebuglist);
19798
ccce17b0 19799 add_setshow_zuinteger_cmd ("dwarf2-die", no_class, &dwarf2_die_debug, _("\
d97bc12b
DE
19800Set debugging of the dwarf2 DIE reader."), _("\
19801Show debugging of the dwarf2 DIE reader."), _("\
19802When enabled (non-zero), DIEs are dumped after they are read in.\n\
19803The value is the maximum depth to print."),
ccce17b0
YQ
19804 NULL,
19805 NULL,
19806 &setdebuglist, &showdebuglist);
9291a0cd 19807
900e11f9
JK
19808 add_setshow_boolean_cmd ("check-physname", no_class, &check_physname, _("\
19809Set cross-checking of \"physname\" code against demangler."), _("\
19810Show cross-checking of \"physname\" code against demangler."), _("\
19811When enabled, GDB's internal \"physname\" code is checked against\n\
19812the demangler."),
19813 NULL, show_check_physname,
19814 &setdebuglist, &showdebuglist);
19815
e615022a
DE
19816 add_setshow_boolean_cmd ("use-deprecated-index-sections",
19817 no_class, &use_deprecated_index_sections, _("\
19818Set whether to use deprecated gdb_index sections."), _("\
19819Show whether to use deprecated gdb_index sections."), _("\
19820When enabled, deprecated .gdb_index sections are used anyway.\n\
19821Normally they are ignored either because of a missing feature or\n\
19822performance issue.\n\
19823Warning: This option must be enabled before gdb reads the file."),
19824 NULL,
19825 NULL,
19826 &setlist, &showlist);
19827
96d19272 19828 c = add_cmd ("gdb-index", class_files, save_gdb_index_command,
11570e71 19829 _("\
fc1a9d6e 19830Save a gdb-index file.\n\
11570e71 19831Usage: save gdb-index DIRECTORY"),
96d19272
JK
19832 &save_cmdlist);
19833 set_cmd_completer (c, filename_completer);
6502dd73 19834}
This page took 3.233451 seconds and 4 git commands to generate.