* dwarf2read.c (signatured_type): Make "per_cu" member first.
[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>
4c2df51b 67
c906108c
SS
68#include <fcntl.h>
69#include "gdb_string.h"
4bdf3d34 70#include "gdb_assert.h"
c906108c 71#include <sys/types.h>
233a11ab
CS
72#ifdef HAVE_ZLIB_H
73#include <zlib.h>
74#endif
dce234bc
PP
75#ifdef HAVE_MMAP
76#include <sys/mman.h>
85d9bd0e
TT
77#ifndef MAP_FAILED
78#define MAP_FAILED ((void *) -1)
79#endif
dce234bc 80#endif
d8151005 81
34eaf542
TT
82typedef struct symbol *symbolp;
83DEF_VEC_P (symbolp);
84
45cfd468
DE
85/* When non-zero, print basic high level tracing messages.
86 This is in contrast to the low level DIE reading of dwarf2_die_debug. */
87static int dwarf2_read_debug = 0;
88
d97bc12b
DE
89/* When non-zero, dump DIEs after they are read in. */
90static int dwarf2_die_debug = 0;
91
900e11f9
JK
92/* When non-zero, cross-check physname against demangler. */
93static int check_physname = 0;
94
481860b3
GB
95/* When non-zero, do not reject deprecated .gdb_index sections. */
96int use_deprecated_index_sections = 0;
97
dce234bc
PP
98static int pagesize;
99
df8a16a1
DJ
100/* When set, the file that we're processing is known to have debugging
101 info for C++ namespaces. GCC 3.3.x did not produce this information,
102 but later versions do. */
103
104static int processing_has_namespace_info;
105
6502dd73
DJ
106static const struct objfile_data *dwarf2_objfile_data_key;
107
dce234bc
PP
108struct dwarf2_section_info
109{
110 asection *asection;
111 gdb_byte *buffer;
112 bfd_size_type size;
b315ab21
TG
113 /* Not NULL if the section was actually mmapped. */
114 void *map_addr;
115 /* Page aligned size of mmapped area. */
116 bfd_size_type map_len;
be391dca
TT
117 /* True if we have tried to read this section. */
118 int readin;
dce234bc
PP
119};
120
8b70b953
TT
121typedef struct dwarf2_section_info dwarf2_section_info_def;
122DEF_VEC_O (dwarf2_section_info_def);
123
9291a0cd
TT
124/* All offsets in the index are of this type. It must be
125 architecture-independent. */
126typedef uint32_t offset_type;
127
128DEF_VEC_I (offset_type);
129
156942c7
DE
130/* Ensure only legit values are used. */
131#define DW2_GDB_INDEX_SYMBOL_STATIC_SET_VALUE(cu_index, value) \
132 do { \
133 gdb_assert ((unsigned int) (value) <= 1); \
134 GDB_INDEX_SYMBOL_STATIC_SET_VALUE((cu_index), (value)); \
135 } while (0)
136
137/* Ensure only legit values are used. */
138#define DW2_GDB_INDEX_SYMBOL_KIND_SET_VALUE(cu_index, value) \
139 do { \
140 gdb_assert ((value) >= GDB_INDEX_SYMBOL_KIND_TYPE \
141 && (value) <= GDB_INDEX_SYMBOL_KIND_OTHER); \
142 GDB_INDEX_SYMBOL_KIND_SET_VALUE((cu_index), (value)); \
143 } while (0)
144
145/* Ensure we don't use more than the alloted nuber of bits for the CU. */
146#define DW2_GDB_INDEX_CU_SET_VALUE(cu_index, value) \
147 do { \
148 gdb_assert (((value) & ~GDB_INDEX_CU_MASK) == 0); \
149 GDB_INDEX_CU_SET_VALUE((cu_index), (value)); \
150 } while (0)
151
9291a0cd
TT
152/* A description of the mapped index. The file format is described in
153 a comment by the code that writes the index. */
154struct mapped_index
155{
559a7a62
JK
156 /* Index data format version. */
157 int version;
158
9291a0cd
TT
159 /* The total length of the buffer. */
160 off_t total_size;
b11b1f88 161
9291a0cd
TT
162 /* A pointer to the address table data. */
163 const gdb_byte *address_table;
b11b1f88 164
9291a0cd
TT
165 /* Size of the address table data in bytes. */
166 offset_type address_table_size;
b11b1f88 167
3876f04e
DE
168 /* The symbol table, implemented as a hash table. */
169 const offset_type *symbol_table;
b11b1f88 170
9291a0cd 171 /* Size in slots, each slot is 2 offset_types. */
3876f04e 172 offset_type symbol_table_slots;
b11b1f88 173
9291a0cd
TT
174 /* A pointer to the constant pool. */
175 const char *constant_pool;
176};
177
95554aad
TT
178typedef struct dwarf2_per_cu_data *dwarf2_per_cu_ptr;
179DEF_VEC_P (dwarf2_per_cu_ptr);
180
9cdd5dbd
DE
181/* Collection of data recorded per objfile.
182 This hangs off of dwarf2_objfile_data_key. */
183
6502dd73
DJ
184struct dwarf2_per_objfile
185{
dce234bc
PP
186 struct dwarf2_section_info info;
187 struct dwarf2_section_info abbrev;
188 struct dwarf2_section_info line;
dce234bc
PP
189 struct dwarf2_section_info loc;
190 struct dwarf2_section_info macinfo;
cf2c3c16 191 struct dwarf2_section_info macro;
dce234bc
PP
192 struct dwarf2_section_info str;
193 struct dwarf2_section_info ranges;
3019eac3 194 struct dwarf2_section_info addr;
dce234bc
PP
195 struct dwarf2_section_info frame;
196 struct dwarf2_section_info eh_frame;
9291a0cd 197 struct dwarf2_section_info gdb_index;
ae038cb0 198
8b70b953
TT
199 VEC (dwarf2_section_info_def) *types;
200
be391dca
TT
201 /* Back link. */
202 struct objfile *objfile;
203
d467dd73 204 /* Table of all the compilation units. This is used to locate
10b3939b 205 the target compilation unit of a particular reference. */
ae038cb0
DJ
206 struct dwarf2_per_cu_data **all_comp_units;
207
208 /* The number of compilation units in ALL_COMP_UNITS. */
209 int n_comp_units;
210
1fd400ff 211 /* The number of .debug_types-related CUs. */
d467dd73 212 int n_type_units;
1fd400ff 213
d467dd73
DE
214 /* The .debug_types-related CUs (TUs). */
215 struct dwarf2_per_cu_data **all_type_units;
1fd400ff 216
ae038cb0
DJ
217 /* A chain of compilation units that are currently read in, so that
218 they can be freed later. */
219 struct dwarf2_per_cu_data *read_in_chain;
72dca2f5 220
348e048f
DE
221 /* A table mapping .debug_types signatures to its signatured_type entry.
222 This is NULL if the .debug_types section hasn't been read in yet. */
223 htab_t signatured_types;
224
3019eac3
DE
225 /* A table mapping DW_AT_dwo_name values to struct dwo_file objects.
226 This is NULL if the table hasn't been allocated yet. */
227 htab_t dwo_files;
228
72dca2f5
FR
229 /* A flag indicating wether this objfile has a section loaded at a
230 VMA of 0. */
231 int has_section_at_zero;
9291a0cd 232
ae2de4f8
DE
233 /* True if we are using the mapped index,
234 or we are faking it for OBJF_READNOW's sake. */
9291a0cd
TT
235 unsigned char using_index;
236
ae2de4f8 237 /* The mapped index, or NULL if .gdb_index is missing or not being used. */
9291a0cd 238 struct mapped_index *index_table;
98bfdba5 239
7b9f3c50
DE
240 /* When using index_table, this keeps track of all quick_file_names entries.
241 TUs can share line table entries with CUs or other TUs, and there can be
242 a lot more TUs than unique line tables, so we maintain a separate table
243 of all line table entries to support the sharing. */
244 htab_t quick_file_names_table;
245
98bfdba5
PA
246 /* Set during partial symbol reading, to prevent queueing of full
247 symbols. */
248 int reading_partial_symbols;
673bfd45 249
dee91e82 250 /* Table mapping type DIEs to their struct type *.
673bfd45 251 This is NULL if not allocated yet.
dee91e82
DE
252 The mapping is done via (CU/TU signature + DIE offset) -> type. */
253 htab_t die_type_hash;
95554aad
TT
254
255 /* The CUs we recently read. */
256 VEC (dwarf2_per_cu_ptr) *just_read_cus;
6502dd73
DJ
257};
258
259static struct dwarf2_per_objfile *dwarf2_per_objfile;
c906108c 260
251d32d9 261/* Default names of the debugging sections. */
c906108c 262
233a11ab
CS
263/* Note that if the debugging section has been compressed, it might
264 have a name like .zdebug_info. */
265
9cdd5dbd
DE
266static const struct dwarf2_debug_sections dwarf2_elf_names =
267{
251d32d9
TG
268 { ".debug_info", ".zdebug_info" },
269 { ".debug_abbrev", ".zdebug_abbrev" },
270 { ".debug_line", ".zdebug_line" },
271 { ".debug_loc", ".zdebug_loc" },
272 { ".debug_macinfo", ".zdebug_macinfo" },
cf2c3c16 273 { ".debug_macro", ".zdebug_macro" },
251d32d9
TG
274 { ".debug_str", ".zdebug_str" },
275 { ".debug_ranges", ".zdebug_ranges" },
276 { ".debug_types", ".zdebug_types" },
3019eac3 277 { ".debug_addr", ".zdebug_addr" },
251d32d9
TG
278 { ".debug_frame", ".zdebug_frame" },
279 { ".eh_frame", NULL },
24d3216f
TT
280 { ".gdb_index", ".zgdb_index" },
281 23
251d32d9 282};
c906108c 283
3019eac3
DE
284/* List of DWO sections. */
285
286static const struct dwo_section_names
287{
288 struct dwarf2_section_names abbrev_dwo;
289 struct dwarf2_section_names info_dwo;
290 struct dwarf2_section_names line_dwo;
291 struct dwarf2_section_names loc_dwo;
09262596
DE
292 struct dwarf2_section_names macinfo_dwo;
293 struct dwarf2_section_names macro_dwo;
3019eac3
DE
294 struct dwarf2_section_names str_dwo;
295 struct dwarf2_section_names str_offsets_dwo;
296 struct dwarf2_section_names types_dwo;
297}
298dwo_section_names =
299{
300 { ".debug_abbrev.dwo", ".zdebug_abbrev.dwo" },
301 { ".debug_info.dwo", ".zdebug_info.dwo" },
302 { ".debug_line.dwo", ".zdebug_line.dwo" },
303 { ".debug_loc.dwo", ".zdebug_loc.dwo" },
09262596
DE
304 { ".debug_macinfo.dwo", ".zdebug_macinfo.dwo" },
305 { ".debug_macro.dwo", ".zdebug_macro.dwo" },
3019eac3
DE
306 { ".debug_str.dwo", ".zdebug_str.dwo" },
307 { ".debug_str_offsets.dwo", ".zdebug_str_offsets.dwo" },
308 { ".debug_types.dwo", ".zdebug_types.dwo" },
309};
310
c906108c
SS
311/* local data types */
312
107d2387
AC
313/* The data in a compilation unit header, after target2host
314 translation, looks like this. */
c906108c 315struct comp_unit_head
a738430d 316{
c764a876 317 unsigned int length;
a738430d 318 short version;
a738430d
MK
319 unsigned char addr_size;
320 unsigned char signed_addr_p;
b64f50a1 321 sect_offset abbrev_offset;
57349743 322
a738430d
MK
323 /* Size of file offsets; either 4 or 8. */
324 unsigned int offset_size;
57349743 325
a738430d
MK
326 /* Size of the length field; either 4 or 12. */
327 unsigned int initial_length_size;
57349743 328
a738430d
MK
329 /* Offset to the first byte of this compilation unit header in the
330 .debug_info section, for resolving relative reference dies. */
b64f50a1 331 sect_offset offset;
57349743 332
d00adf39
DE
333 /* Offset to first die in this cu from the start of the cu.
334 This will be the first byte following the compilation unit header. */
b64f50a1 335 cu_offset first_die_offset;
a738430d 336};
c906108c 337
3da10d80
KS
338/* Type used for delaying computation of method physnames.
339 See comments for compute_delayed_physnames. */
340struct delayed_method_info
341{
342 /* The type to which the method is attached, i.e., its parent class. */
343 struct type *type;
344
345 /* The index of the method in the type's function fieldlists. */
346 int fnfield_index;
347
348 /* The index of the method in the fieldlist. */
349 int index;
350
351 /* The name of the DIE. */
352 const char *name;
353
354 /* The DIE associated with this method. */
355 struct die_info *die;
356};
357
358typedef struct delayed_method_info delayed_method_info;
359DEF_VEC_O (delayed_method_info);
360
e7c27a73
DJ
361/* Internal state when decoding a particular compilation unit. */
362struct dwarf2_cu
363{
364 /* The objfile containing this compilation unit. */
365 struct objfile *objfile;
366
d00adf39 367 /* The header of the compilation unit. */
e7c27a73 368 struct comp_unit_head header;
e142c38c 369
d00adf39
DE
370 /* Base address of this compilation unit. */
371 CORE_ADDR base_address;
372
373 /* Non-zero if base_address has been set. */
374 int base_known;
375
e142c38c
DJ
376 /* The language we are debugging. */
377 enum language language;
378 const struct language_defn *language_defn;
379
b0f35d58
DL
380 const char *producer;
381
e142c38c
DJ
382 /* The generic symbol table building routines have separate lists for
383 file scope symbols and all all other scopes (local scopes). So
384 we need to select the right one to pass to add_symbol_to_list().
385 We do it by keeping a pointer to the correct list in list_in_scope.
386
387 FIXME: The original dwarf code just treated the file scope as the
388 first local scope, and all other local scopes as nested local
389 scopes, and worked fine. Check to see if we really need to
390 distinguish these in buildsym.c. */
391 struct pending **list_in_scope;
392
433df2d4
DE
393 /* The abbrev table for this CU.
394 Normally this points to the abbrev table in the objfile.
395 But if DWO_UNIT is non-NULL this is the abbrev table in the DWO file. */
396 struct abbrev_table *abbrev_table;
72bf9492 397
b64f50a1
JK
398 /* Hash table holding all the loaded partial DIEs
399 with partial_die->offset.SECT_OFF as hash. */
72bf9492
DJ
400 htab_t partial_dies;
401
402 /* Storage for things with the same lifetime as this read-in compilation
403 unit, including partial DIEs. */
404 struct obstack comp_unit_obstack;
405
ae038cb0
DJ
406 /* When multiple dwarf2_cu structures are living in memory, this field
407 chains them all together, so that they can be released efficiently.
408 We will probably also want a generation counter so that most-recently-used
409 compilation units are cached... */
410 struct dwarf2_per_cu_data *read_in_chain;
411
412 /* Backchain to our per_cu entry if the tree has been built. */
413 struct dwarf2_per_cu_data *per_cu;
414
415 /* How many compilation units ago was this CU last referenced? */
416 int last_used;
417
b64f50a1
JK
418 /* A hash table of DIE cu_offset for following references with
419 die_info->offset.sect_off as hash. */
51545339 420 htab_t die_hash;
10b3939b
DJ
421
422 /* Full DIEs if read in. */
423 struct die_info *dies;
424
425 /* A set of pointers to dwarf2_per_cu_data objects for compilation
426 units referenced by this one. Only set during full symbol processing;
427 partial symbol tables do not have dependencies. */
428 htab_t dependencies;
429
cb1df416
DJ
430 /* Header data from the line table, during full symbol processing. */
431 struct line_header *line_header;
432
3da10d80
KS
433 /* A list of methods which need to have physnames computed
434 after all type information has been read. */
435 VEC (delayed_method_info) *method_list;
436
96408a79
SA
437 /* To be copied to symtab->call_site_htab. */
438 htab_t call_site_htab;
439
034e5797
DE
440 /* Non-NULL if this CU came from a DWO file.
441 There is an invariant here that is important to remember:
442 Except for attributes copied from the top level DIE in the "main"
443 (or "stub") file in preparation for reading the DWO file
444 (e.g., DW_AT_GNU_addr_base), we KISS: there is only *one* CU.
445 Either there isn't a DWO file (in which case this is NULL and the point
446 is moot), or there is and either we're not going to read it (in which
447 case this is NULL) or there is and we are reading it (in which case this
448 is non-NULL). */
3019eac3
DE
449 struct dwo_unit *dwo_unit;
450
451 /* The DW_AT_addr_base attribute if present, zero otherwise
452 (zero is a valid value though).
453 Note this value comes from the stub CU/TU's DIE. */
454 ULONGEST addr_base;
455
2e3cf129
DE
456 /* The DW_AT_ranges_base attribute if present, zero otherwise
457 (zero is a valid value though).
458 Note this value comes from the stub CU/TU's DIE.
459 Also note that the value is zero in the non-DWO case so this value can
460 be used without needing to know whether DWO files are in use or not. */
461 ULONGEST ranges_base;
462
ae038cb0
DJ
463 /* Mark used when releasing cached dies. */
464 unsigned int mark : 1;
465
8be455d7
JK
466 /* This CU references .debug_loc. See the symtab->locations_valid field.
467 This test is imperfect as there may exist optimized debug code not using
468 any location list and still facing inlining issues if handled as
469 unoptimized code. For a future better test see GCC PR other/32998. */
8be455d7 470 unsigned int has_loclist : 1;
ba919b58 471
685b1105
JK
472 /* These cache the results for producer_is_gxx_lt_4_6 and producer_is_icc.
473 CHECKED_PRODUCER is set if both PRODUCER_IS_GXX_LT_4_6 and PRODUCER_IS_ICC
474 are valid. This information is cached because profiling CU expansion
475 showed excessive time spent in producer_is_gxx_lt_4_6. */
ba919b58
TT
476 unsigned int checked_producer : 1;
477 unsigned int producer_is_gxx_lt_4_6 : 1;
685b1105 478 unsigned int producer_is_icc : 1;
e7c27a73
DJ
479};
480
10b3939b
DJ
481/* Persistent data held for a compilation unit, even when not
482 processing it. We put a pointer to this structure in the
28dee7f5 483 read_symtab_private field of the psymtab. */
10b3939b 484
ae038cb0
DJ
485struct dwarf2_per_cu_data
486{
348e048f 487 /* The start offset and length of this compilation unit. 2**29-1
ae038cb0 488 bytes should suffice to store the length of any compilation unit
45452591
DE
489 - if it doesn't, GDB will fall over anyway.
490 NOTE: Unlike comp_unit_head.length, this length includes
3019eac3
DE
491 initial_length_size.
492 If the DIE refers to a DWO file, this is always of the original die,
493 not the DWO file. */
b64f50a1 494 sect_offset offset;
348e048f 495 unsigned int length : 29;
ae038cb0
DJ
496
497 /* Flag indicating this compilation unit will be read in before
498 any of the current compilation units are processed. */
c764a876 499 unsigned int queued : 1;
ae038cb0 500
0d99eb77
DE
501 /* This flag will be set when reading partial DIEs if we need to load
502 absolutely all DIEs for this compilation unit, instead of just the ones
503 we think are interesting. It gets set if we look for a DIE in the
5afb4e99
DJ
504 hash table and don't find it. */
505 unsigned int load_all_dies : 1;
506
3019eac3
DE
507 /* Non-zero if this CU is from .debug_types. */
508 unsigned int is_debug_types : 1;
509
510 /* The section this CU/TU lives in.
511 If the DIE refers to a DWO file, this is always the original die,
512 not the DWO file. */
513 struct dwarf2_section_info *info_or_types_section;
348e048f 514
17ea53c3
JK
515 /* Set to non-NULL iff this CU is currently loaded. When it gets freed out
516 of the CU cache it gets reset to NULL again. */
ae038cb0 517 struct dwarf2_cu *cu;
1c379e20 518
9cdd5dbd
DE
519 /* The corresponding objfile.
520 Normally we can get the objfile from dwarf2_per_objfile.
521 However we can enter this file with just a "per_cu" handle. */
9291a0cd
TT
522 struct objfile *objfile;
523
524 /* When using partial symbol tables, the 'psymtab' field is active.
525 Otherwise the 'quick' field is active. */
526 union
527 {
528 /* The partial symbol table associated with this compilation unit,
95554aad 529 or NULL for unread partial units. */
9291a0cd
TT
530 struct partial_symtab *psymtab;
531
532 /* Data needed by the "quick" functions. */
533 struct dwarf2_per_cu_quick_data *quick;
534 } v;
95554aad
TT
535
536 /* The CUs we import using DW_TAG_imported_unit. This is filled in
537 while reading psymtabs, used to compute the psymtab dependencies,
538 and then cleared. Then it is filled in again while reading full
539 symbols, and only deleted when the objfile is destroyed. */
540 VEC (dwarf2_per_cu_ptr) *imported_symtabs;
ae038cb0
DJ
541};
542
348e048f
DE
543/* Entry in the signatured_types hash table. */
544
545struct signatured_type
546{
42e7ad6c
DE
547 /* The "per_cu" object of this type.
548 N.B.: This is the first member so that it's easy to convert pointers
549 between them. */
550 struct dwarf2_per_cu_data per_cu;
551
3019eac3 552 /* The type's signature. */
348e048f
DE
553 ULONGEST signature;
554
3019eac3
DE
555 /* Offset in the TU of the type's DIE, as read from the TU header.
556 If the definition lives in a DWO file, this value is unusable. */
557 cu_offset type_offset_in_tu;
558
559 /* Offset in the section of the type's DIE.
560 If the definition lives in a DWO file, this is the offset in the
561 .debug_types.dwo section.
562 The value is zero until the actual value is known.
563 Zero is otherwise not a valid section offset. */
564 sect_offset type_offset_in_section;
348e048f
DE
565};
566
3019eac3
DE
567/* These sections are what may appear in a "dwo" file. */
568
569struct dwo_sections
570{
571 struct dwarf2_section_info abbrev;
572 struct dwarf2_section_info info;
573 struct dwarf2_section_info line;
574 struct dwarf2_section_info loc;
09262596
DE
575 struct dwarf2_section_info macinfo;
576 struct dwarf2_section_info macro;
3019eac3
DE
577 struct dwarf2_section_info str;
578 struct dwarf2_section_info str_offsets;
579 VEC (dwarf2_section_info_def) *types;
580};
581
582/* Common bits of DWO CUs/TUs. */
583
584struct dwo_unit
585{
586 /* Backlink to the containing struct dwo_file. */
587 struct dwo_file *dwo_file;
588
589 /* The "id" that distinguishes this CU/TU.
590 .debug_info calls this "dwo_id", .debug_types calls this "signature".
591 Since signatures came first, we stick with it for consistency. */
592 ULONGEST signature;
593
594 /* The section this CU/TU lives in, in the DWO file. */
595 struct dwarf2_section_info *info_or_types_section;
596
597 /* Same as dwarf2_per_cu_data:{offset,length} but for the DWO section. */
598 sect_offset offset;
599 unsigned int length;
600
601 /* For types, offset in the type's DIE of the type defined by this TU. */
602 cu_offset type_offset_in_tu;
603};
604
605/* Data for one DWO file. */
606
607struct dwo_file
608{
609 /* The DW_AT_GNU_dwo_name attribute.
610 We don't manage space for this, it's an attribute. */
611 const char *dwo_name;
612
613 /* The bfd, when the file is open. Otherwise this is NULL. */
614 bfd *dwo_bfd;
615
616 /* Section info for this file. */
617 struct dwo_sections sections;
618
619 /* Table of CUs in the file.
620 Each element is a struct dwo_unit. */
621 htab_t cus;
622
623 /* Table of TUs in the file.
624 Each element is a struct dwo_unit. */
625 htab_t tus;
626};
627
0963b4bd
MS
628/* Struct used to pass misc. parameters to read_die_and_children, et
629 al. which are used for both .debug_info and .debug_types dies.
630 All parameters here are unchanging for the life of the call. This
dee91e82 631 struct exists to abstract away the constant parameters of die reading. */
93311388
DE
632
633struct die_reader_specs
634{
dee91e82 635 /* die_section->asection->owner. */
93311388
DE
636 bfd* abfd;
637
638 /* The CU of the DIE we are parsing. */
639 struct dwarf2_cu *cu;
640
3019eac3
DE
641 /* Non-NULL if reading a DWO file. */
642 struct dwo_file *dwo_file;
643
dee91e82 644 /* The section the die comes from.
3019eac3 645 This is either .debug_info or .debug_types, or the .dwo variants. */
dee91e82
DE
646 struct dwarf2_section_info *die_section;
647
648 /* die_section->buffer. */
649 gdb_byte *buffer;
f664829e
DE
650
651 /* The end of the buffer. */
652 const gdb_byte *buffer_end;
93311388
DE
653};
654
fd820528 655/* Type of function passed to init_cutu_and_read_dies, et.al. */
dee91e82
DE
656typedef void (die_reader_func_ftype) (const struct die_reader_specs *reader,
657 gdb_byte *info_ptr,
658 struct die_info *comp_unit_die,
659 int has_children,
660 void *data);
661
debd256d
JB
662/* The line number information for a compilation unit (found in the
663 .debug_line section) begins with a "statement program header",
664 which contains the following information. */
665struct line_header
666{
667 unsigned int total_length;
668 unsigned short version;
669 unsigned int header_length;
670 unsigned char minimum_instruction_length;
2dc7f7b3 671 unsigned char maximum_ops_per_instruction;
debd256d
JB
672 unsigned char default_is_stmt;
673 int line_base;
674 unsigned char line_range;
675 unsigned char opcode_base;
676
677 /* standard_opcode_lengths[i] is the number of operands for the
678 standard opcode whose value is i. This means that
679 standard_opcode_lengths[0] is unused, and the last meaningful
680 element is standard_opcode_lengths[opcode_base - 1]. */
681 unsigned char *standard_opcode_lengths;
682
683 /* The include_directories table. NOTE! These strings are not
684 allocated with xmalloc; instead, they are pointers into
685 debug_line_buffer. If you try to free them, `free' will get
686 indigestion. */
687 unsigned int num_include_dirs, include_dirs_size;
688 char **include_dirs;
689
690 /* The file_names table. NOTE! These strings are not allocated
691 with xmalloc; instead, they are pointers into debug_line_buffer.
692 Don't try to free them directly. */
693 unsigned int num_file_names, file_names_size;
694 struct file_entry
c906108c 695 {
debd256d
JB
696 char *name;
697 unsigned int dir_index;
698 unsigned int mod_time;
699 unsigned int length;
aaa75496 700 int included_p; /* Non-zero if referenced by the Line Number Program. */
cb1df416 701 struct symtab *symtab; /* The associated symbol table, if any. */
debd256d
JB
702 } *file_names;
703
704 /* The start and end of the statement program following this
6502dd73 705 header. These point into dwarf2_per_objfile->line_buffer. */
fe1b8b76 706 gdb_byte *statement_program_start, *statement_program_end;
debd256d 707};
c906108c
SS
708
709/* When we construct a partial symbol table entry we only
0963b4bd 710 need this much information. */
c906108c
SS
711struct partial_die_info
712 {
72bf9492 713 /* Offset of this DIE. */
b64f50a1 714 sect_offset offset;
72bf9492
DJ
715
716 /* DWARF-2 tag for this DIE. */
717 ENUM_BITFIELD(dwarf_tag) tag : 16;
718
72bf9492
DJ
719 /* Assorted flags describing the data found in this DIE. */
720 unsigned int has_children : 1;
721 unsigned int is_external : 1;
722 unsigned int is_declaration : 1;
723 unsigned int has_type : 1;
724 unsigned int has_specification : 1;
725 unsigned int has_pc_info : 1;
481860b3 726 unsigned int may_be_inlined : 1;
72bf9492
DJ
727
728 /* Flag set if the SCOPE field of this structure has been
729 computed. */
730 unsigned int scope_set : 1;
731
fa4028e9
JB
732 /* Flag set if the DIE has a byte_size attribute. */
733 unsigned int has_byte_size : 1;
734
98bfdba5
PA
735 /* Flag set if any of the DIE's children are template arguments. */
736 unsigned int has_template_arguments : 1;
737
abc72ce4
DE
738 /* Flag set if fixup_partial_die has been called on this die. */
739 unsigned int fixup_called : 1;
740
72bf9492 741 /* The name of this DIE. Normally the value of DW_AT_name, but
94af9270 742 sometimes a default name for unnamed DIEs. */
c906108c 743 char *name;
72bf9492 744
abc72ce4
DE
745 /* The linkage name, if present. */
746 const char *linkage_name;
747
72bf9492
DJ
748 /* The scope to prepend to our children. This is generally
749 allocated on the comp_unit_obstack, so will disappear
750 when this compilation unit leaves the cache. */
751 char *scope;
752
95554aad
TT
753 /* Some data associated with the partial DIE. The tag determines
754 which field is live. */
755 union
756 {
757 /* The location description associated with this DIE, if any. */
758 struct dwarf_block *locdesc;
759 /* The offset of an import, for DW_TAG_imported_unit. */
760 sect_offset offset;
761 } d;
72bf9492
DJ
762
763 /* If HAS_PC_INFO, the PC range associated with this DIE. */
c906108c
SS
764 CORE_ADDR lowpc;
765 CORE_ADDR highpc;
72bf9492 766
93311388 767 /* Pointer into the info_buffer (or types_buffer) pointing at the target of
72bf9492 768 DW_AT_sibling, if any. */
abc72ce4
DE
769 /* NOTE: This member isn't strictly necessary, read_partial_die could
770 return DW_AT_sibling values to its caller load_partial_dies. */
fe1b8b76 771 gdb_byte *sibling;
72bf9492
DJ
772
773 /* If HAS_SPECIFICATION, the offset of the DIE referred to by
774 DW_AT_specification (or DW_AT_abstract_origin or
775 DW_AT_extension). */
b64f50a1 776 sect_offset spec_offset;
72bf9492
DJ
777
778 /* Pointers to this DIE's parent, first child, and next sibling,
779 if any. */
780 struct partial_die_info *die_parent, *die_child, *die_sibling;
c906108c
SS
781 };
782
0963b4bd 783/* This data structure holds the information of an abbrev. */
c906108c
SS
784struct abbrev_info
785 {
786 unsigned int number; /* number identifying abbrev */
787 enum dwarf_tag tag; /* dwarf tag */
f3dd6933
DJ
788 unsigned short has_children; /* boolean */
789 unsigned short num_attrs; /* number of attributes */
c906108c
SS
790 struct attr_abbrev *attrs; /* an array of attribute descriptions */
791 struct abbrev_info *next; /* next in chain */
792 };
793
794struct attr_abbrev
795 {
9d25dd43
DE
796 ENUM_BITFIELD(dwarf_attribute) name : 16;
797 ENUM_BITFIELD(dwarf_form) form : 16;
c906108c
SS
798 };
799
433df2d4
DE
800/* Size of abbrev_table.abbrev_hash_table. */
801#define ABBREV_HASH_SIZE 121
802
803/* Top level data structure to contain an abbreviation table. */
804
805struct abbrev_table
806{
807 /* Where the abbrev table came from. */
808 struct dwarf2_section_info *section;
809 sect_offset offset;
810
811 /* Storage for the abbrev table. */
812 struct obstack abbrev_obstack;
813
814 /* Hash table of abbrevs.
815 This is an array of size ABBREV_HASH_SIZE allocated in abbrev_obstack.
816 It could be statically allocated, but the previous code didn't so we
817 don't either. */
818 struct abbrev_info **abbrevs;
819};
820
0963b4bd 821/* Attributes have a name and a value. */
b60c80d6
DJ
822struct attribute
823 {
9d25dd43 824 ENUM_BITFIELD(dwarf_attribute) name : 16;
8285870a
JK
825 ENUM_BITFIELD(dwarf_form) form : 15;
826
827 /* Has DW_STRING already been updated by dwarf2_canonicalize_name? This
828 field should be in u.str (existing only for DW_STRING) but it is kept
829 here for better struct attribute alignment. */
830 unsigned int string_is_canonical : 1;
831
b60c80d6
DJ
832 union
833 {
834 char *str;
835 struct dwarf_block *blk;
43bbcdc2
PH
836 ULONGEST unsnd;
837 LONGEST snd;
b60c80d6 838 CORE_ADDR addr;
348e048f 839 struct signatured_type *signatured_type;
b60c80d6
DJ
840 }
841 u;
842 };
843
0963b4bd 844/* This data structure holds a complete die structure. */
c906108c
SS
845struct die_info
846 {
76815b17
DE
847 /* DWARF-2 tag for this DIE. */
848 ENUM_BITFIELD(dwarf_tag) tag : 16;
849
850 /* Number of attributes */
98bfdba5
PA
851 unsigned char num_attrs;
852
853 /* True if we're presently building the full type name for the
854 type derived from this DIE. */
855 unsigned char building_fullname : 1;
76815b17
DE
856
857 /* Abbrev number */
858 unsigned int abbrev;
859
93311388 860 /* Offset in .debug_info or .debug_types section. */
b64f50a1 861 sect_offset offset;
78ba4af6
JB
862
863 /* The dies in a compilation unit form an n-ary tree. PARENT
864 points to this die's parent; CHILD points to the first child of
865 this node; and all the children of a given node are chained
4950bc1c 866 together via their SIBLING fields. */
639d11d3
DC
867 struct die_info *child; /* Its first child, if any. */
868 struct die_info *sibling; /* Its next sibling, if any. */
869 struct die_info *parent; /* Its parent, if any. */
c906108c 870
b60c80d6
DJ
871 /* An array of attributes, with NUM_ATTRS elements. There may be
872 zero, but it's not common and zero-sized arrays are not
873 sufficiently portable C. */
874 struct attribute attrs[1];
c906108c
SS
875 };
876
0963b4bd 877/* Get at parts of an attribute structure. */
c906108c
SS
878
879#define DW_STRING(attr) ((attr)->u.str)
8285870a 880#define DW_STRING_IS_CANONICAL(attr) ((attr)->string_is_canonical)
c906108c
SS
881#define DW_UNSND(attr) ((attr)->u.unsnd)
882#define DW_BLOCK(attr) ((attr)->u.blk)
883#define DW_SND(attr) ((attr)->u.snd)
884#define DW_ADDR(attr) ((attr)->u.addr)
348e048f 885#define DW_SIGNATURED_TYPE(attr) ((attr)->u.signatured_type)
c906108c 886
0963b4bd 887/* Blocks are a bunch of untyped bytes. */
c906108c
SS
888struct dwarf_block
889 {
890 unsigned int size;
1d6edc3c
JK
891
892 /* Valid only if SIZE is not zero. */
fe1b8b76 893 gdb_byte *data;
c906108c
SS
894 };
895
c906108c
SS
896#ifndef ATTR_ALLOC_CHUNK
897#define ATTR_ALLOC_CHUNK 4
898#endif
899
c906108c
SS
900/* Allocate fields for structs, unions and enums in this size. */
901#ifndef DW_FIELD_ALLOC_CHUNK
902#define DW_FIELD_ALLOC_CHUNK 4
903#endif
904
c906108c
SS
905/* FIXME: We might want to set this from BFD via bfd_arch_bits_per_byte,
906 but this would require a corresponding change in unpack_field_as_long
907 and friends. */
908static int bits_per_byte = 8;
909
910/* The routines that read and process dies for a C struct or C++ class
911 pass lists of data member fields and lists of member function fields
912 in an instance of a field_info structure, as defined below. */
913struct field_info
c5aa993b 914 {
0963b4bd 915 /* List of data member and baseclasses fields. */
c5aa993b
JM
916 struct nextfield
917 {
918 struct nextfield *next;
919 int accessibility;
920 int virtuality;
921 struct field field;
922 }
7d0ccb61 923 *fields, *baseclasses;
c906108c 924
7d0ccb61 925 /* Number of fields (including baseclasses). */
c5aa993b 926 int nfields;
c906108c 927
c5aa993b
JM
928 /* Number of baseclasses. */
929 int nbaseclasses;
c906108c 930
c5aa993b
JM
931 /* Set if the accesibility of one of the fields is not public. */
932 int non_public_fields;
c906108c 933
c5aa993b
JM
934 /* Member function fields array, entries are allocated in the order they
935 are encountered in the object file. */
936 struct nextfnfield
937 {
938 struct nextfnfield *next;
939 struct fn_field fnfield;
940 }
941 *fnfields;
c906108c 942
c5aa993b
JM
943 /* Member function fieldlist array, contains name of possibly overloaded
944 member function, number of overloaded member functions and a pointer
945 to the head of the member function field chain. */
946 struct fnfieldlist
947 {
948 char *name;
949 int length;
950 struct nextfnfield *head;
951 }
952 *fnfieldlists;
c906108c 953
c5aa993b
JM
954 /* Number of entries in the fnfieldlists array. */
955 int nfnfields;
98751a41
JK
956
957 /* typedefs defined inside this class. TYPEDEF_FIELD_LIST contains head of
958 a NULL terminated list of TYPEDEF_FIELD_LIST_COUNT elements. */
959 struct typedef_field_list
960 {
961 struct typedef_field field;
962 struct typedef_field_list *next;
963 }
964 *typedef_field_list;
965 unsigned typedef_field_list_count;
c5aa993b 966 };
c906108c 967
10b3939b
DJ
968/* One item on the queue of compilation units to read in full symbols
969 for. */
970struct dwarf2_queue_item
971{
972 struct dwarf2_per_cu_data *per_cu;
95554aad 973 enum language pretend_language;
10b3939b
DJ
974 struct dwarf2_queue_item *next;
975};
976
977/* The current queue. */
978static struct dwarf2_queue_item *dwarf2_queue, *dwarf2_queue_tail;
979
ae038cb0
DJ
980/* Loaded secondary compilation units are kept in memory until they
981 have not been referenced for the processing of this many
982 compilation units. Set this to zero to disable caching. Cache
983 sizes of up to at least twenty will improve startup time for
984 typical inter-CU-reference binaries, at an obvious memory cost. */
985static int dwarf2_max_cache_age = 5;
920d2a44
AC
986static void
987show_dwarf2_max_cache_age (struct ui_file *file, int from_tty,
988 struct cmd_list_element *c, const char *value)
989{
3e43a32a
MS
990 fprintf_filtered (file, _("The upper bound on the age of cached "
991 "dwarf2 compilation units is %s.\n"),
920d2a44
AC
992 value);
993}
994
ae038cb0 995
0963b4bd 996/* Various complaints about symbol reading that don't abort the process. */
c906108c 997
4d3c2250
KB
998static void
999dwarf2_statement_list_fits_in_line_number_section_complaint (void)
2e276125 1000{
4d3c2250 1001 complaint (&symfile_complaints,
e2e0b3e5 1002 _("statement list doesn't fit in .debug_line section"));
4d3c2250
KB
1003}
1004
25e43795
DJ
1005static void
1006dwarf2_debug_line_missing_file_complaint (void)
1007{
1008 complaint (&symfile_complaints,
1009 _(".debug_line section has line data without a file"));
1010}
1011
59205f5a
JB
1012static void
1013dwarf2_debug_line_missing_end_sequence_complaint (void)
1014{
1015 complaint (&symfile_complaints,
3e43a32a
MS
1016 _(".debug_line section has line "
1017 "program sequence without an end"));
59205f5a
JB
1018}
1019
4d3c2250
KB
1020static void
1021dwarf2_complex_location_expr_complaint (void)
2e276125 1022{
e2e0b3e5 1023 complaint (&symfile_complaints, _("location expression too complex"));
4d3c2250
KB
1024}
1025
4d3c2250
KB
1026static void
1027dwarf2_const_value_length_mismatch_complaint (const char *arg1, int arg2,
1028 int arg3)
2e276125 1029{
4d3c2250 1030 complaint (&symfile_complaints,
3e43a32a
MS
1031 _("const value length mismatch for '%s', got %d, expected %d"),
1032 arg1, arg2, arg3);
4d3c2250
KB
1033}
1034
1035static void
f664829e 1036dwarf2_section_buffer_overflow_complaint (struct dwarf2_section_info *section)
2e276125 1037{
4d3c2250 1038 complaint (&symfile_complaints,
f664829e
DE
1039 _("debug info runs off end of %s section"
1040 " [in module %s]"),
1041 section->asection->name,
1042 bfd_get_filename (section->asection->owner));
4d3c2250
KB
1043}
1044
1045static void
1046dwarf2_macro_malformed_definition_complaint (const char *arg1)
8e19ed76 1047{
4d3c2250 1048 complaint (&symfile_complaints,
3e43a32a
MS
1049 _("macro debug info contains a "
1050 "malformed macro definition:\n`%s'"),
4d3c2250
KB
1051 arg1);
1052}
1053
1054static void
1055dwarf2_invalid_attrib_class_complaint (const char *arg1, const char *arg2)
8b2dbe47 1056{
4d3c2250 1057 complaint (&symfile_complaints,
3e43a32a
MS
1058 _("invalid attribute class or form for '%s' in '%s'"),
1059 arg1, arg2);
4d3c2250 1060}
c906108c 1061
c906108c
SS
1062/* local function prototypes */
1063
4efb68b1 1064static void dwarf2_locate_sections (bfd *, asection *, void *);
c906108c 1065
aaa75496
JB
1066static void dwarf2_create_include_psymtab (char *, struct partial_symtab *,
1067 struct objfile *);
1068
918dd910
JK
1069static void dwarf2_find_base_address (struct die_info *die,
1070 struct dwarf2_cu *cu);
1071
c67a9c90 1072static void dwarf2_build_psymtabs_hard (struct objfile *);
c906108c 1073
72bf9492
DJ
1074static void scan_partial_symbols (struct partial_die_info *,
1075 CORE_ADDR *, CORE_ADDR *,
5734ee8b 1076 int, struct dwarf2_cu *);
c906108c 1077
72bf9492
DJ
1078static void add_partial_symbol (struct partial_die_info *,
1079 struct dwarf2_cu *);
63d06c5c 1080
72bf9492
DJ
1081static void add_partial_namespace (struct partial_die_info *pdi,
1082 CORE_ADDR *lowpc, CORE_ADDR *highpc,
5734ee8b 1083 int need_pc, struct dwarf2_cu *cu);
63d06c5c 1084
5d7cb8df
JK
1085static void add_partial_module (struct partial_die_info *pdi, CORE_ADDR *lowpc,
1086 CORE_ADDR *highpc, int need_pc,
1087 struct dwarf2_cu *cu);
1088
72bf9492
DJ
1089static void add_partial_enumeration (struct partial_die_info *enum_pdi,
1090 struct dwarf2_cu *cu);
91c24f0a 1091
bc30ff58
JB
1092static void add_partial_subprogram (struct partial_die_info *pdi,
1093 CORE_ADDR *lowpc, CORE_ADDR *highpc,
5734ee8b 1094 int need_pc, struct dwarf2_cu *cu);
bc30ff58 1095
a14ed312 1096static void dwarf2_psymtab_to_symtab (struct partial_symtab *);
c906108c 1097
a14ed312 1098static void psymtab_to_symtab_1 (struct partial_symtab *);
c906108c 1099
433df2d4
DE
1100static struct abbrev_info *abbrev_table_lookup_abbrev
1101 (const struct abbrev_table *, unsigned int);
1102
1103static struct abbrev_table *abbrev_table_read_table
1104 (struct dwarf2_section_info *, sect_offset);
1105
1106static void abbrev_table_free (struct abbrev_table *);
1107
dee91e82
DE
1108static void dwarf2_read_abbrevs (struct dwarf2_cu *,
1109 struct dwarf2_section_info *);
c906108c 1110
f3dd6933 1111static void dwarf2_free_abbrev_table (void *);
c906108c 1112
6caca83c
CC
1113static unsigned int peek_abbrev_code (bfd *, gdb_byte *);
1114
dee91e82
DE
1115static struct partial_die_info *load_partial_dies
1116 (const struct die_reader_specs *, gdb_byte *, int);
72bf9492 1117
dee91e82
DE
1118static gdb_byte *read_partial_die (const struct die_reader_specs *,
1119 struct partial_die_info *,
1120 struct abbrev_info *,
1121 unsigned int,
1122 gdb_byte *);
c906108c 1123
b64f50a1 1124static struct partial_die_info *find_partial_die (sect_offset,
10b3939b 1125 struct dwarf2_cu *);
72bf9492
DJ
1126
1127static void fixup_partial_die (struct partial_die_info *,
1128 struct dwarf2_cu *);
1129
dee91e82
DE
1130static gdb_byte *read_attribute (const struct die_reader_specs *,
1131 struct attribute *, struct attr_abbrev *,
1132 gdb_byte *);
a8329558 1133
fe1b8b76 1134static unsigned int read_1_byte (bfd *, gdb_byte *);
c906108c 1135
fe1b8b76 1136static int read_1_signed_byte (bfd *, gdb_byte *);
c906108c 1137
fe1b8b76 1138static unsigned int read_2_bytes (bfd *, gdb_byte *);
c906108c 1139
fe1b8b76 1140static unsigned int read_4_bytes (bfd *, gdb_byte *);
c906108c 1141
93311388 1142static ULONGEST read_8_bytes (bfd *, gdb_byte *);
c906108c 1143
fe1b8b76 1144static CORE_ADDR read_address (bfd *, gdb_byte *ptr, struct dwarf2_cu *,
891d2f0b 1145 unsigned int *);
c906108c 1146
c764a876
DE
1147static LONGEST read_initial_length (bfd *, gdb_byte *, unsigned int *);
1148
1149static LONGEST read_checked_initial_length_and_offset
1150 (bfd *, gdb_byte *, const struct comp_unit_head *,
1151 unsigned int *, unsigned int *);
613e1657 1152
fe1b8b76 1153static LONGEST read_offset (bfd *, gdb_byte *, const struct comp_unit_head *,
c764a876
DE
1154 unsigned int *);
1155
1156static LONGEST read_offset_1 (bfd *, gdb_byte *, unsigned int);
613e1657 1157
fe1b8b76 1158static gdb_byte *read_n_bytes (bfd *, gdb_byte *, unsigned int);
c906108c 1159
9b1c24c8 1160static char *read_direct_string (bfd *, gdb_byte *, unsigned int *);
c906108c 1161
fe1b8b76
JB
1162static char *read_indirect_string (bfd *, gdb_byte *,
1163 const struct comp_unit_head *,
1164 unsigned int *);
4bdf3d34 1165
12df843f 1166static ULONGEST read_unsigned_leb128 (bfd *, gdb_byte *, unsigned int *);
c906108c 1167
12df843f 1168static LONGEST read_signed_leb128 (bfd *, gdb_byte *, unsigned int *);
c906108c 1169
3019eac3
DE
1170static CORE_ADDR read_addr_index_from_leb128 (struct dwarf2_cu *, gdb_byte *,
1171 unsigned int *);
1172
1173static char *read_str_index (const struct die_reader_specs *reader,
1174 struct dwarf2_cu *cu, ULONGEST str_index);
1175
e142c38c 1176static void set_cu_language (unsigned int, struct dwarf2_cu *);
c906108c 1177
e142c38c
DJ
1178static struct attribute *dwarf2_attr (struct die_info *, unsigned int,
1179 struct dwarf2_cu *);
c906108c 1180
348e048f
DE
1181static struct attribute *dwarf2_attr_no_follow (struct die_info *,
1182 unsigned int,
1183 struct dwarf2_cu *);
1184
05cf31d1
JB
1185static int dwarf2_flag_true_p (struct die_info *die, unsigned name,
1186 struct dwarf2_cu *cu);
1187
e142c38c 1188static int die_is_declaration (struct die_info *, struct dwarf2_cu *cu);
3ca72b44 1189
e142c38c 1190static struct die_info *die_specification (struct die_info *die,
f2f0e013 1191 struct dwarf2_cu **);
63d06c5c 1192
debd256d
JB
1193static void free_line_header (struct line_header *lh);
1194
aaa75496
JB
1195static void add_file_name (struct line_header *, char *, unsigned int,
1196 unsigned int, unsigned int);
1197
3019eac3
DE
1198static struct line_header *dwarf_decode_line_header (unsigned int offset,
1199 struct dwarf2_cu *cu);
debd256d 1200
f3f5162e
DE
1201static void dwarf_decode_lines (struct line_header *, const char *,
1202 struct dwarf2_cu *, struct partial_symtab *,
1203 int);
c906108c 1204
72b9f47f 1205static void dwarf2_start_subfile (char *, const char *, const char *);
c906108c 1206
a14ed312 1207static struct symbol *new_symbol (struct die_info *, struct type *,
e7c27a73 1208 struct dwarf2_cu *);
c906108c 1209
34eaf542
TT
1210static struct symbol *new_symbol_full (struct die_info *, struct type *,
1211 struct dwarf2_cu *, struct symbol *);
1212
a14ed312 1213static void dwarf2_const_value (struct attribute *, struct symbol *,
e7c27a73 1214 struct dwarf2_cu *);
c906108c 1215
98bfdba5
PA
1216static void dwarf2_const_value_attr (struct attribute *attr,
1217 struct type *type,
1218 const char *name,
1219 struct obstack *obstack,
12df843f 1220 struct dwarf2_cu *cu, LONGEST *value,
98bfdba5
PA
1221 gdb_byte **bytes,
1222 struct dwarf2_locexpr_baton **baton);
2df3850c 1223
e7c27a73 1224static struct type *die_type (struct die_info *, struct dwarf2_cu *);
c906108c 1225
b4ba55a1
JB
1226static int need_gnat_info (struct dwarf2_cu *);
1227
3e43a32a
MS
1228static struct type *die_descriptive_type (struct die_info *,
1229 struct dwarf2_cu *);
b4ba55a1
JB
1230
1231static void set_descriptive_type (struct type *, struct die_info *,
1232 struct dwarf2_cu *);
1233
e7c27a73
DJ
1234static struct type *die_containing_type (struct die_info *,
1235 struct dwarf2_cu *);
c906108c 1236
673bfd45
DE
1237static struct type *lookup_die_type (struct die_info *, struct attribute *,
1238 struct dwarf2_cu *);
c906108c 1239
f792889a 1240static struct type *read_type_die (struct die_info *, struct dwarf2_cu *);
c906108c 1241
673bfd45
DE
1242static struct type *read_type_die_1 (struct die_info *, struct dwarf2_cu *);
1243
0d5cff50 1244static const char *determine_prefix (struct die_info *die, struct dwarf2_cu *);
63d06c5c 1245
6e70227d 1246static char *typename_concat (struct obstack *obs, const char *prefix,
f55ee35c
JK
1247 const char *suffix, int physname,
1248 struct dwarf2_cu *cu);
63d06c5c 1249
e7c27a73 1250static void read_file_scope (struct die_info *, struct dwarf2_cu *);
c906108c 1251
348e048f
DE
1252static void read_type_unit_scope (struct die_info *, struct dwarf2_cu *);
1253
e7c27a73 1254static void read_func_scope (struct die_info *, struct dwarf2_cu *);
c906108c 1255
e7c27a73 1256static void read_lexical_block_scope (struct die_info *, struct dwarf2_cu *);
c906108c 1257
96408a79
SA
1258static void read_call_site_scope (struct die_info *die, struct dwarf2_cu *cu);
1259
ff013f42
JK
1260static int dwarf2_ranges_read (unsigned, CORE_ADDR *, CORE_ADDR *,
1261 struct dwarf2_cu *, struct partial_symtab *);
1262
a14ed312 1263static int dwarf2_get_pc_bounds (struct die_info *,
d85a05f0
DJ
1264 CORE_ADDR *, CORE_ADDR *, struct dwarf2_cu *,
1265 struct partial_symtab *);
c906108c 1266
fae299cd
DC
1267static void get_scope_pc_bounds (struct die_info *,
1268 CORE_ADDR *, CORE_ADDR *,
1269 struct dwarf2_cu *);
1270
801e3a5b
JB
1271static void dwarf2_record_block_ranges (struct die_info *, struct block *,
1272 CORE_ADDR, struct dwarf2_cu *);
1273
a14ed312 1274static void dwarf2_add_field (struct field_info *, struct die_info *,
e7c27a73 1275 struct dwarf2_cu *);
c906108c 1276
a14ed312 1277static void dwarf2_attach_fields_to_type (struct field_info *,
e7c27a73 1278 struct type *, struct dwarf2_cu *);
c906108c 1279
a14ed312 1280static void dwarf2_add_member_fn (struct field_info *,
e26fb1d7 1281 struct die_info *, struct type *,
e7c27a73 1282 struct dwarf2_cu *);
c906108c 1283
a14ed312 1284static void dwarf2_attach_fn_fields_to_type (struct field_info *,
3e43a32a
MS
1285 struct type *,
1286 struct dwarf2_cu *);
c906108c 1287
134d01f1 1288static void process_structure_scope (struct die_info *, struct dwarf2_cu *);
c906108c 1289
e7c27a73 1290static void read_common_block (struct die_info *, struct dwarf2_cu *);
c906108c 1291
e7c27a73 1292static void read_namespace (struct die_info *die, struct dwarf2_cu *);
d9fa45fe 1293
5d7cb8df
JK
1294static void read_module (struct die_info *die, struct dwarf2_cu *cu);
1295
27aa8d6a
SW
1296static void read_import_statement (struct die_info *die, struct dwarf2_cu *);
1297
f55ee35c
JK
1298static struct type *read_module_type (struct die_info *die,
1299 struct dwarf2_cu *cu);
1300
38d518c9 1301static const char *namespace_name (struct die_info *die,
e142c38c 1302 int *is_anonymous, struct dwarf2_cu *);
38d518c9 1303
134d01f1 1304static void process_enumeration_scope (struct die_info *, struct dwarf2_cu *);
c906108c 1305
e7c27a73 1306static CORE_ADDR decode_locdesc (struct dwarf_block *, struct dwarf2_cu *);
c906108c 1307
6e70227d 1308static enum dwarf_array_dim_ordering read_array_order (struct die_info *,
7ca2d3a3
DL
1309 struct dwarf2_cu *);
1310
dee91e82 1311static struct die_info *read_die_and_children (const struct die_reader_specs *,
93311388 1312 gdb_byte *info_ptr,
fe1b8b76 1313 gdb_byte **new_info_ptr,
639d11d3
DC
1314 struct die_info *parent);
1315
dee91e82 1316static struct die_info *read_die_and_siblings (const struct die_reader_specs *,
93311388 1317 gdb_byte *info_ptr,
fe1b8b76 1318 gdb_byte **new_info_ptr,
639d11d3
DC
1319 struct die_info *parent);
1320
3019eac3
DE
1321static gdb_byte *read_full_die_1 (const struct die_reader_specs *,
1322 struct die_info **, gdb_byte *, int *, int);
1323
dee91e82
DE
1324static gdb_byte *read_full_die (const struct die_reader_specs *,
1325 struct die_info **, gdb_byte *, int *);
93311388 1326
e7c27a73 1327static void process_die (struct die_info *, struct dwarf2_cu *);
c906108c 1328
71c25dea
TT
1329static char *dwarf2_canonicalize_name (char *, struct dwarf2_cu *,
1330 struct obstack *);
1331
e142c38c 1332static char *dwarf2_name (struct die_info *die, struct dwarf2_cu *);
9219021c 1333
98bfdba5
PA
1334static const char *dwarf2_full_name (char *name,
1335 struct die_info *die,
1336 struct dwarf2_cu *cu);
1337
e142c38c 1338static struct die_info *dwarf2_extension (struct die_info *die,
f2f0e013 1339 struct dwarf2_cu **);
9219021c 1340
f39c6ffd 1341static const char *dwarf_tag_name (unsigned int);
c906108c 1342
f39c6ffd 1343static const char *dwarf_attr_name (unsigned int);
c906108c 1344
f39c6ffd 1345static const char *dwarf_form_name (unsigned int);
c906108c 1346
a14ed312 1347static char *dwarf_bool_name (unsigned int);
c906108c 1348
f39c6ffd 1349static const char *dwarf_type_encoding_name (unsigned int);
c906108c 1350
f9aca02d 1351static struct die_info *sibling_die (struct die_info *);
c906108c 1352
d97bc12b
DE
1353static void dump_die_shallow (struct ui_file *, int indent, struct die_info *);
1354
1355static void dump_die_for_error (struct die_info *);
1356
1357static void dump_die_1 (struct ui_file *, int level, int max_level,
1358 struct die_info *);
c906108c 1359
d97bc12b 1360/*static*/ void dump_die (struct die_info *, int max_level);
c906108c 1361
51545339 1362static void store_in_ref_table (struct die_info *,
10b3939b 1363 struct dwarf2_cu *);
c906108c 1364
93311388
DE
1365static int is_ref_attr (struct attribute *);
1366
b64f50a1 1367static sect_offset dwarf2_get_ref_die_offset (struct attribute *);
c906108c 1368
43bbcdc2 1369static LONGEST dwarf2_get_attr_constant_value (struct attribute *, int);
a02abb62 1370
348e048f
DE
1371static struct die_info *follow_die_ref_or_sig (struct die_info *,
1372 struct attribute *,
1373 struct dwarf2_cu **);
1374
10b3939b
DJ
1375static struct die_info *follow_die_ref (struct die_info *,
1376 struct attribute *,
f2f0e013 1377 struct dwarf2_cu **);
c906108c 1378
348e048f
DE
1379static struct die_info *follow_die_sig (struct die_info *,
1380 struct attribute *,
1381 struct dwarf2_cu **);
1382
6c83ed52
TT
1383static struct signatured_type *lookup_signatured_type_at_offset
1384 (struct objfile *objfile,
b64f50a1 1385 struct dwarf2_section_info *section, sect_offset offset);
6c83ed52 1386
e5fe5e75 1387static void load_full_type_unit (struct dwarf2_per_cu_data *per_cu);
348e048f 1388
52dc124a 1389static void read_signatured_type (struct signatured_type *);
348e048f 1390
c906108c
SS
1391/* memory allocation interface */
1392
7b5a2f43 1393static struct dwarf_block *dwarf_alloc_block (struct dwarf2_cu *);
c906108c 1394
b60c80d6 1395static struct die_info *dwarf_alloc_die (struct dwarf2_cu *, int);
c906108c 1396
09262596
DE
1397static void dwarf_decode_macros (struct dwarf2_cu *, unsigned int,
1398 char *, int);
2e276125 1399
8e19ed76
PS
1400static int attr_form_is_block (struct attribute *);
1401
3690dd37
JB
1402static int attr_form_is_section_offset (struct attribute *);
1403
1404static int attr_form_is_constant (struct attribute *);
1405
8cf6f0b1
TT
1406static void fill_in_loclist_baton (struct dwarf2_cu *cu,
1407 struct dwarf2_loclist_baton *baton,
1408 struct attribute *attr);
1409
93e7bd98
DJ
1410static void dwarf2_symbol_mark_computed (struct attribute *attr,
1411 struct symbol *sym,
1412 struct dwarf2_cu *cu);
4c2df51b 1413
dee91e82
DE
1414static gdb_byte *skip_one_die (const struct die_reader_specs *reader,
1415 gdb_byte *info_ptr,
1416 struct abbrev_info *abbrev);
4bb7a0a7 1417
72bf9492
DJ
1418static void free_stack_comp_unit (void *);
1419
72bf9492
DJ
1420static hashval_t partial_die_hash (const void *item);
1421
1422static int partial_die_eq (const void *item_lhs, const void *item_rhs);
1423
ae038cb0 1424static struct dwarf2_per_cu_data *dwarf2_find_containing_comp_unit
b64f50a1 1425 (sect_offset offset, struct objfile *objfile);
ae038cb0 1426
9816fde3 1427static void init_one_comp_unit (struct dwarf2_cu *cu,
23745b47 1428 struct dwarf2_per_cu_data *per_cu);
9816fde3
JK
1429
1430static void prepare_one_comp_unit (struct dwarf2_cu *cu,
95554aad
TT
1431 struct die_info *comp_unit_die,
1432 enum language pretend_language);
93311388 1433
68dc6402 1434static void free_heap_comp_unit (void *);
ae038cb0
DJ
1435
1436static void free_cached_comp_units (void *);
1437
1438static void age_cached_comp_units (void);
1439
dee91e82 1440static void free_one_cached_comp_unit (struct dwarf2_per_cu_data *);
ae038cb0 1441
f792889a
DJ
1442static struct type *set_die_type (struct die_info *, struct type *,
1443 struct dwarf2_cu *);
1c379e20 1444
ae038cb0
DJ
1445static void create_all_comp_units (struct objfile *);
1446
0e50663e 1447static int create_all_type_units (struct objfile *);
1fd400ff 1448
95554aad
TT
1449static void load_full_comp_unit (struct dwarf2_per_cu_data *,
1450 enum language);
10b3939b 1451
95554aad
TT
1452static void process_full_comp_unit (struct dwarf2_per_cu_data *,
1453 enum language);
10b3939b
DJ
1454
1455static void dwarf2_add_dependence (struct dwarf2_cu *,
1456 struct dwarf2_per_cu_data *);
1457
ae038cb0
DJ
1458static void dwarf2_mark (struct dwarf2_cu *);
1459
1460static void dwarf2_clear_marks (struct dwarf2_per_cu_data *);
1461
b64f50a1 1462static struct type *get_die_type_at_offset (sect_offset,
673bfd45
DE
1463 struct dwarf2_per_cu_data *per_cu);
1464
f792889a 1465static struct type *get_die_type (struct die_info *die, struct dwarf2_cu *cu);
72019c9c 1466
9291a0cd
TT
1467static void dwarf2_release_queue (void *dummy);
1468
95554aad
TT
1469static void queue_comp_unit (struct dwarf2_per_cu_data *per_cu,
1470 enum language pretend_language);
1471
1472static int maybe_queue_comp_unit (struct dwarf2_cu *this_cu,
1473 struct dwarf2_per_cu_data *per_cu,
1474 enum language pretend_language);
9291a0cd 1475
a0f42c21 1476static void process_queue (void);
9291a0cd
TT
1477
1478static void find_file_and_directory (struct die_info *die,
1479 struct dwarf2_cu *cu,
1480 char **name, char **comp_dir);
1481
1482static char *file_full_name (int file, struct line_header *lh,
1483 const char *comp_dir);
1484
fd820528
DE
1485static void init_cutu_and_read_dies
1486 (struct dwarf2_per_cu_data *this_cu, int use_existing_cu, int keep,
3019eac3
DE
1487 die_reader_func_ftype *die_reader_func, void *data);
1488
dee91e82
DE
1489static void init_cutu_and_read_dies_simple
1490 (struct dwarf2_per_cu_data *this_cu,
1491 die_reader_func_ftype *die_reader_func, void *data);
9291a0cd 1492
673bfd45 1493static htab_t allocate_signatured_type_table (struct objfile *objfile);
1fd400ff 1494
95554aad 1495static void process_psymtab_comp_unit (struct dwarf2_per_cu_data *, int);
dee91e82 1496
3019eac3
DE
1497static htab_t allocate_dwo_unit_table (struct objfile *objfile);
1498
1499static struct dwo_unit *lookup_dwo_comp_unit
1500 (struct dwarf2_per_cu_data *, char *, const char *, ULONGEST);
1501
1502static struct dwo_unit *lookup_dwo_type_unit
1503 (struct signatured_type *, char *, const char *);
1504
1505static void free_dwo_file_cleanup (void *);
1506
1507static void munmap_section_buffer (struct dwarf2_section_info *);
1508
95554aad
TT
1509static void process_cu_includes (void);
1510
9291a0cd
TT
1511#if WORDS_BIGENDIAN
1512
1513/* Convert VALUE between big- and little-endian. */
1514static offset_type
1515byte_swap (offset_type value)
1516{
1517 offset_type result;
1518
1519 result = (value & 0xff) << 24;
1520 result |= (value & 0xff00) << 8;
1521 result |= (value & 0xff0000) >> 8;
1522 result |= (value & 0xff000000) >> 24;
1523 return result;
1524}
1525
1526#define MAYBE_SWAP(V) byte_swap (V)
1527
1528#else
1529#define MAYBE_SWAP(V) (V)
1530#endif /* WORDS_BIGENDIAN */
1531
1532/* The suffix for an index file. */
1533#define INDEX_SUFFIX ".gdb-index"
1534
3da10d80
KS
1535static const char *dwarf2_physname (char *name, struct die_info *die,
1536 struct dwarf2_cu *cu);
1537
c906108c 1538/* Try to locate the sections we need for DWARF 2 debugging
251d32d9
TG
1539 information and return true if we have enough to do something.
1540 NAMES points to the dwarf2 section names, or is NULL if the standard
1541 ELF names are used. */
c906108c
SS
1542
1543int
251d32d9
TG
1544dwarf2_has_info (struct objfile *objfile,
1545 const struct dwarf2_debug_sections *names)
c906108c 1546{
be391dca
TT
1547 dwarf2_per_objfile = objfile_data (objfile, dwarf2_objfile_data_key);
1548 if (!dwarf2_per_objfile)
1549 {
1550 /* Initialize per-objfile state. */
1551 struct dwarf2_per_objfile *data
1552 = obstack_alloc (&objfile->objfile_obstack, sizeof (*data));
9a619af0 1553
be391dca
TT
1554 memset (data, 0, sizeof (*data));
1555 set_objfile_data (objfile, dwarf2_objfile_data_key, data);
1556 dwarf2_per_objfile = data;
6502dd73 1557
251d32d9
TG
1558 bfd_map_over_sections (objfile->obfd, dwarf2_locate_sections,
1559 (void *) names);
be391dca
TT
1560 dwarf2_per_objfile->objfile = objfile;
1561 }
1562 return (dwarf2_per_objfile->info.asection != NULL
1563 && dwarf2_per_objfile->abbrev.asection != NULL);
c906108c
SS
1564}
1565
251d32d9
TG
1566/* When loading sections, we look either for uncompressed section or for
1567 compressed section names. */
233a11ab
CS
1568
1569static int
251d32d9
TG
1570section_is_p (const char *section_name,
1571 const struct dwarf2_section_names *names)
233a11ab 1572{
251d32d9
TG
1573 if (names->normal != NULL
1574 && strcmp (section_name, names->normal) == 0)
1575 return 1;
1576 if (names->compressed != NULL
1577 && strcmp (section_name, names->compressed) == 0)
1578 return 1;
1579 return 0;
233a11ab
CS
1580}
1581
c906108c
SS
1582/* This function is mapped across the sections and remembers the
1583 offset and size of each of the debugging sections we are interested
1584 in. */
1585
1586static void
251d32d9 1587dwarf2_locate_sections (bfd *abfd, asection *sectp, void *vnames)
c906108c 1588{
251d32d9
TG
1589 const struct dwarf2_debug_sections *names;
1590
1591 if (vnames == NULL)
1592 names = &dwarf2_elf_names;
1593 else
1594 names = (const struct dwarf2_debug_sections *) vnames;
1595
1596 if (section_is_p (sectp->name, &names->info))
c906108c 1597 {
dce234bc
PP
1598 dwarf2_per_objfile->info.asection = sectp;
1599 dwarf2_per_objfile->info.size = bfd_get_section_size (sectp);
c906108c 1600 }
251d32d9 1601 else if (section_is_p (sectp->name, &names->abbrev))
c906108c 1602 {
dce234bc
PP
1603 dwarf2_per_objfile->abbrev.asection = sectp;
1604 dwarf2_per_objfile->abbrev.size = bfd_get_section_size (sectp);
c906108c 1605 }
251d32d9 1606 else if (section_is_p (sectp->name, &names->line))
c906108c 1607 {
dce234bc
PP
1608 dwarf2_per_objfile->line.asection = sectp;
1609 dwarf2_per_objfile->line.size = bfd_get_section_size (sectp);
c906108c 1610 }
251d32d9 1611 else if (section_is_p (sectp->name, &names->loc))
c906108c 1612 {
dce234bc
PP
1613 dwarf2_per_objfile->loc.asection = sectp;
1614 dwarf2_per_objfile->loc.size = bfd_get_section_size (sectp);
c906108c 1615 }
251d32d9 1616 else if (section_is_p (sectp->name, &names->macinfo))
c906108c 1617 {
dce234bc
PP
1618 dwarf2_per_objfile->macinfo.asection = sectp;
1619 dwarf2_per_objfile->macinfo.size = bfd_get_section_size (sectp);
c906108c 1620 }
cf2c3c16
TT
1621 else if (section_is_p (sectp->name, &names->macro))
1622 {
1623 dwarf2_per_objfile->macro.asection = sectp;
1624 dwarf2_per_objfile->macro.size = bfd_get_section_size (sectp);
1625 }
251d32d9 1626 else if (section_is_p (sectp->name, &names->str))
c906108c 1627 {
dce234bc
PP
1628 dwarf2_per_objfile->str.asection = sectp;
1629 dwarf2_per_objfile->str.size = bfd_get_section_size (sectp);
c906108c 1630 }
3019eac3
DE
1631 else if (section_is_p (sectp->name, &names->addr))
1632 {
1633 dwarf2_per_objfile->addr.asection = sectp;
1634 dwarf2_per_objfile->addr.size = bfd_get_section_size (sectp);
1635 }
251d32d9 1636 else if (section_is_p (sectp->name, &names->frame))
b6af0555 1637 {
dce234bc
PP
1638 dwarf2_per_objfile->frame.asection = sectp;
1639 dwarf2_per_objfile->frame.size = bfd_get_section_size (sectp);
b6af0555 1640 }
251d32d9 1641 else if (section_is_p (sectp->name, &names->eh_frame))
b6af0555 1642 {
81b9b86e 1643 flagword aflag = bfd_get_section_flags (abfd, sectp);
9a619af0 1644
3799ccc6
EZ
1645 if (aflag & SEC_HAS_CONTENTS)
1646 {
dce234bc
PP
1647 dwarf2_per_objfile->eh_frame.asection = sectp;
1648 dwarf2_per_objfile->eh_frame.size = bfd_get_section_size (sectp);
3799ccc6 1649 }
b6af0555 1650 }
251d32d9 1651 else if (section_is_p (sectp->name, &names->ranges))
af34e669 1652 {
dce234bc
PP
1653 dwarf2_per_objfile->ranges.asection = sectp;
1654 dwarf2_per_objfile->ranges.size = bfd_get_section_size (sectp);
af34e669 1655 }
251d32d9 1656 else if (section_is_p (sectp->name, &names->types))
348e048f 1657 {
8b70b953
TT
1658 struct dwarf2_section_info type_section;
1659
1660 memset (&type_section, 0, sizeof (type_section));
1661 type_section.asection = sectp;
1662 type_section.size = bfd_get_section_size (sectp);
1663
1664 VEC_safe_push (dwarf2_section_info_def, dwarf2_per_objfile->types,
1665 &type_section);
348e048f 1666 }
251d32d9 1667 else if (section_is_p (sectp->name, &names->gdb_index))
9291a0cd
TT
1668 {
1669 dwarf2_per_objfile->gdb_index.asection = sectp;
1670 dwarf2_per_objfile->gdb_index.size = bfd_get_section_size (sectp);
1671 }
dce234bc 1672
72dca2f5
FR
1673 if ((bfd_get_section_flags (abfd, sectp) & SEC_LOAD)
1674 && bfd_section_vma (abfd, sectp) == 0)
1675 dwarf2_per_objfile->has_section_at_zero = 1;
c906108c
SS
1676}
1677
dce234bc
PP
1678/* Decompress a section that was compressed using zlib. Store the
1679 decompressed buffer, and its size, in OUTBUF and OUTSIZE. */
233a11ab
CS
1680
1681static void
dce234bc
PP
1682zlib_decompress_section (struct objfile *objfile, asection *sectp,
1683 gdb_byte **outbuf, bfd_size_type *outsize)
1684{
3019eac3 1685 bfd *abfd = sectp->owner;
dce234bc
PP
1686#ifndef HAVE_ZLIB_H
1687 error (_("Support for zlib-compressed DWARF data (from '%s') "
1688 "is disabled in this copy of GDB"),
1689 bfd_get_filename (abfd));
1690#else
1691 bfd_size_type compressed_size = bfd_get_section_size (sectp);
1692 gdb_byte *compressed_buffer = xmalloc (compressed_size);
affddf13 1693 struct cleanup *cleanup = make_cleanup (xfree, compressed_buffer);
dce234bc
PP
1694 bfd_size_type uncompressed_size;
1695 gdb_byte *uncompressed_buffer;
1696 z_stream strm;
1697 int rc;
1698 int header_size = 12;
1699
1700 if (bfd_seek (abfd, sectp->filepos, SEEK_SET) != 0
3e43a32a
MS
1701 || bfd_bread (compressed_buffer,
1702 compressed_size, abfd) != compressed_size)
dce234bc
PP
1703 error (_("Dwarf Error: Can't read DWARF data from '%s'"),
1704 bfd_get_filename (abfd));
1705
1706 /* Read the zlib header. In this case, it should be "ZLIB" followed
1707 by the uncompressed section size, 8 bytes in big-endian order. */
1708 if (compressed_size < header_size
1709 || strncmp (compressed_buffer, "ZLIB", 4) != 0)
1710 error (_("Dwarf Error: Corrupt DWARF ZLIB header from '%s'"),
1711 bfd_get_filename (abfd));
1712 uncompressed_size = compressed_buffer[4]; uncompressed_size <<= 8;
1713 uncompressed_size += compressed_buffer[5]; uncompressed_size <<= 8;
1714 uncompressed_size += compressed_buffer[6]; uncompressed_size <<= 8;
1715 uncompressed_size += compressed_buffer[7]; uncompressed_size <<= 8;
1716 uncompressed_size += compressed_buffer[8]; uncompressed_size <<= 8;
1717 uncompressed_size += compressed_buffer[9]; uncompressed_size <<= 8;
1718 uncompressed_size += compressed_buffer[10]; uncompressed_size <<= 8;
1719 uncompressed_size += compressed_buffer[11];
1720
1721 /* It is possible the section consists of several compressed
1722 buffers concatenated together, so we uncompress in a loop. */
1723 strm.zalloc = NULL;
1724 strm.zfree = NULL;
1725 strm.opaque = NULL;
1726 strm.avail_in = compressed_size - header_size;
1727 strm.next_in = (Bytef*) compressed_buffer + header_size;
1728 strm.avail_out = uncompressed_size;
1729 uncompressed_buffer = obstack_alloc (&objfile->objfile_obstack,
1730 uncompressed_size);
1731 rc = inflateInit (&strm);
1732 while (strm.avail_in > 0)
1733 {
1734 if (rc != Z_OK)
1735 error (_("Dwarf Error: setting up DWARF uncompression in '%s': %d"),
1736 bfd_get_filename (abfd), rc);
1737 strm.next_out = ((Bytef*) uncompressed_buffer
1738 + (uncompressed_size - strm.avail_out));
1739 rc = inflate (&strm, Z_FINISH);
1740 if (rc != Z_STREAM_END)
1741 error (_("Dwarf Error: zlib error uncompressing from '%s': %d"),
1742 bfd_get_filename (abfd), rc);
1743 rc = inflateReset (&strm);
1744 }
1745 rc = inflateEnd (&strm);
1746 if (rc != Z_OK
1747 || strm.avail_out != 0)
1748 error (_("Dwarf Error: concluding DWARF uncompression in '%s': %d"),
1749 bfd_get_filename (abfd), rc);
1750
affddf13 1751 do_cleanups (cleanup);
dce234bc
PP
1752 *outbuf = uncompressed_buffer;
1753 *outsize = uncompressed_size;
1754#endif
233a11ab
CS
1755}
1756
fceca515
DE
1757/* A helper function that decides whether a section is empty,
1758 or not present. */
9e0ac564
TT
1759
1760static int
1761dwarf2_section_empty_p (struct dwarf2_section_info *info)
1762{
1763 return info->asection == NULL || info->size == 0;
1764}
1765
3019eac3
DE
1766/* Read the contents of the section INFO.
1767 OBJFILE is the main object file, but not necessarily the file where
1768 the section comes from. E.g., for DWO files INFO->asection->owner
1769 is the bfd of the DWO file.
dce234bc 1770 If the section is compressed, uncompress it before returning. */
c906108c 1771
dce234bc
PP
1772static void
1773dwarf2_read_section (struct objfile *objfile, struct dwarf2_section_info *info)
c906108c 1774{
dce234bc 1775 asection *sectp = info->asection;
3019eac3 1776 bfd *abfd;
dce234bc
PP
1777 gdb_byte *buf, *retbuf;
1778 unsigned char header[4];
c906108c 1779
be391dca
TT
1780 if (info->readin)
1781 return;
dce234bc 1782 info->buffer = NULL;
b315ab21 1783 info->map_addr = NULL;
be391dca 1784 info->readin = 1;
188dd5d6 1785
9e0ac564 1786 if (dwarf2_section_empty_p (info))
dce234bc 1787 return;
c906108c 1788
3019eac3
DE
1789 /* Note that ABFD may not be from OBJFILE, e.g. a DWO section. */
1790 abfd = sectp->owner;
1791
dce234bc
PP
1792 /* Check if the file has a 4-byte header indicating compression. */
1793 if (info->size > sizeof (header)
1794 && bfd_seek (abfd, sectp->filepos, SEEK_SET) == 0
1795 && bfd_bread (header, sizeof (header), abfd) == sizeof (header))
1796 {
1797 /* Upon decompression, update the buffer and its size. */
1798 if (strncmp (header, "ZLIB", sizeof (header)) == 0)
1799 {
1800 zlib_decompress_section (objfile, sectp, &info->buffer,
1801 &info->size);
1802 return;
1803 }
1804 }
4bdf3d34 1805
dce234bc
PP
1806#ifdef HAVE_MMAP
1807 if (pagesize == 0)
1808 pagesize = getpagesize ();
2e276125 1809
dce234bc
PP
1810 /* Only try to mmap sections which are large enough: we don't want to
1811 waste space due to fragmentation. Also, only try mmap for sections
1812 without relocations. */
1813
1814 if (info->size > 4 * pagesize && (sectp->flags & SEC_RELOC) == 0)
1815 {
b315ab21
TG
1816 info->buffer = bfd_mmap (abfd, 0, info->size, PROT_READ,
1817 MAP_PRIVATE, sectp->filepos,
1818 &info->map_addr, &info->map_len);
dce234bc 1819
b315ab21 1820 if ((caddr_t)info->buffer != MAP_FAILED)
dce234bc 1821 {
be391dca 1822#if HAVE_POSIX_MADVISE
b315ab21 1823 posix_madvise (info->map_addr, info->map_len, POSIX_MADV_WILLNEED);
be391dca 1824#endif
dce234bc
PP
1825 return;
1826 }
1827 }
1828#endif
1829
1830 /* If we get here, we are a normal, not-compressed section. */
1831 info->buffer = buf
1832 = obstack_alloc (&objfile->objfile_obstack, info->size);
1833
1834 /* When debugging .o files, we may need to apply relocations; see
1835 http://sourceware.org/ml/gdb-patches/2002-04/msg00136.html .
1836 We never compress sections in .o files, so we only need to
1837 try this when the section is not compressed. */
ac8035ab 1838 retbuf = symfile_relocate_debug_section (objfile, sectp, buf);
dce234bc
PP
1839 if (retbuf != NULL)
1840 {
1841 info->buffer = retbuf;
1842 return;
1843 }
1844
1845 if (bfd_seek (abfd, sectp->filepos, SEEK_SET) != 0
1846 || bfd_bread (buf, info->size, abfd) != info->size)
1847 error (_("Dwarf Error: Can't read DWARF data from '%s'"),
1848 bfd_get_filename (abfd));
1849}
1850
9e0ac564
TT
1851/* A helper function that returns the size of a section in a safe way.
1852 If you are positive that the section has been read before using the
1853 size, then it is safe to refer to the dwarf2_section_info object's
1854 "size" field directly. In other cases, you must call this
1855 function, because for compressed sections the size field is not set
1856 correctly until the section has been read. */
1857
1858static bfd_size_type
1859dwarf2_section_size (struct objfile *objfile,
1860 struct dwarf2_section_info *info)
1861{
1862 if (!info->readin)
1863 dwarf2_read_section (objfile, info);
1864 return info->size;
1865}
1866
dce234bc 1867/* Fill in SECTP, BUFP and SIZEP with section info, given OBJFILE and
0963b4bd 1868 SECTION_NAME. */
af34e669 1869
dce234bc 1870void
3017a003
TG
1871dwarf2_get_section_info (struct objfile *objfile,
1872 enum dwarf2_section_enum sect,
dce234bc
PP
1873 asection **sectp, gdb_byte **bufp,
1874 bfd_size_type *sizep)
1875{
1876 struct dwarf2_per_objfile *data
1877 = objfile_data (objfile, dwarf2_objfile_data_key);
1878 struct dwarf2_section_info *info;
a3b2a86b
TT
1879
1880 /* We may see an objfile without any DWARF, in which case we just
1881 return nothing. */
1882 if (data == NULL)
1883 {
1884 *sectp = NULL;
1885 *bufp = NULL;
1886 *sizep = 0;
1887 return;
1888 }
3017a003
TG
1889 switch (sect)
1890 {
1891 case DWARF2_DEBUG_FRAME:
1892 info = &data->frame;
1893 break;
1894 case DWARF2_EH_FRAME:
1895 info = &data->eh_frame;
1896 break;
1897 default:
1898 gdb_assert_not_reached ("unexpected section");
1899 }
dce234bc 1900
9e0ac564 1901 dwarf2_read_section (objfile, info);
dce234bc
PP
1902
1903 *sectp = info->asection;
1904 *bufp = info->buffer;
1905 *sizep = info->size;
1906}
1907
9291a0cd 1908\f
7b9f3c50
DE
1909/* DWARF quick_symbols_functions support. */
1910
1911/* TUs can share .debug_line entries, and there can be a lot more TUs than
1912 unique line tables, so we maintain a separate table of all .debug_line
1913 derived entries to support the sharing.
1914 All the quick functions need is the list of file names. We discard the
1915 line_header when we're done and don't need to record it here. */
1916struct quick_file_names
1917{
1918 /* The offset in .debug_line of the line table. We hash on this. */
1919 unsigned int offset;
1920
1921 /* The number of entries in file_names, real_names. */
1922 unsigned int num_file_names;
1923
1924 /* The file names from the line table, after being run through
1925 file_full_name. */
1926 const char **file_names;
1927
1928 /* The file names from the line table after being run through
1929 gdb_realpath. These are computed lazily. */
1930 const char **real_names;
1931};
1932
1933/* When using the index (and thus not using psymtabs), each CU has an
1934 object of this type. This is used to hold information needed by
1935 the various "quick" methods. */
1936struct dwarf2_per_cu_quick_data
1937{
1938 /* The file table. This can be NULL if there was no file table
1939 or it's currently not read in.
1940 NOTE: This points into dwarf2_per_objfile->quick_file_names_table. */
1941 struct quick_file_names *file_names;
1942
1943 /* The corresponding symbol table. This is NULL if symbols for this
1944 CU have not yet been read. */
1945 struct symtab *symtab;
1946
1947 /* A temporary mark bit used when iterating over all CUs in
1948 expand_symtabs_matching. */
1949 unsigned int mark : 1;
1950
1951 /* True if we've tried to read the file table and found there isn't one.
1952 There will be no point in trying to read it again next time. */
1953 unsigned int no_file_data : 1;
1954};
1955
1956/* Hash function for a quick_file_names. */
1957
1958static hashval_t
1959hash_file_name_entry (const void *e)
1960{
1961 const struct quick_file_names *file_data = e;
1962
1963 return file_data->offset;
1964}
1965
1966/* Equality function for a quick_file_names. */
1967
1968static int
1969eq_file_name_entry (const void *a, const void *b)
1970{
1971 const struct quick_file_names *ea = a;
1972 const struct quick_file_names *eb = b;
1973
1974 return ea->offset == eb->offset;
1975}
1976
1977/* Delete function for a quick_file_names. */
1978
1979static void
1980delete_file_name_entry (void *e)
1981{
1982 struct quick_file_names *file_data = e;
1983 int i;
1984
1985 for (i = 0; i < file_data->num_file_names; ++i)
1986 {
1987 xfree ((void*) file_data->file_names[i]);
1988 if (file_data->real_names)
1989 xfree ((void*) file_data->real_names[i]);
1990 }
1991
1992 /* The space for the struct itself lives on objfile_obstack,
1993 so we don't free it here. */
1994}
1995
1996/* Create a quick_file_names hash table. */
1997
1998static htab_t
1999create_quick_file_names_table (unsigned int nr_initial_entries)
2000{
2001 return htab_create_alloc (nr_initial_entries,
2002 hash_file_name_entry, eq_file_name_entry,
2003 delete_file_name_entry, xcalloc, xfree);
2004}
9291a0cd 2005
918dd910
JK
2006/* Read in PER_CU->CU. This function is unrelated to symtabs, symtab would
2007 have to be created afterwards. You should call age_cached_comp_units after
2008 processing PER_CU->CU. dw2_setup must have been already called. */
2009
2010static void
2011load_cu (struct dwarf2_per_cu_data *per_cu)
2012{
3019eac3 2013 if (per_cu->is_debug_types)
e5fe5e75 2014 load_full_type_unit (per_cu);
918dd910 2015 else
95554aad 2016 load_full_comp_unit (per_cu, language_minimal);
918dd910 2017
918dd910 2018 gdb_assert (per_cu->cu != NULL);
2dc860c0
DE
2019
2020 dwarf2_find_base_address (per_cu->cu->dies, per_cu->cu);
918dd910
JK
2021}
2022
a0f42c21 2023/* Read in the symbols for PER_CU. */
2fdf6df6 2024
9291a0cd 2025static void
a0f42c21 2026dw2_do_instantiate_symtab (struct dwarf2_per_cu_data *per_cu)
9291a0cd
TT
2027{
2028 struct cleanup *back_to;
2029
2030 back_to = make_cleanup (dwarf2_release_queue, NULL);
2031
95554aad
TT
2032 if (dwarf2_per_objfile->using_index
2033 ? per_cu->v.quick->symtab == NULL
2034 : (per_cu->v.psymtab == NULL || !per_cu->v.psymtab->readin))
2035 {
2036 queue_comp_unit (per_cu, language_minimal);
2037 load_cu (per_cu);
2038 }
9291a0cd 2039
a0f42c21 2040 process_queue ();
9291a0cd
TT
2041
2042 /* Age the cache, releasing compilation units that have not
2043 been used recently. */
2044 age_cached_comp_units ();
2045
2046 do_cleanups (back_to);
2047}
2048
2049/* Ensure that the symbols for PER_CU have been read in. OBJFILE is
2050 the objfile from which this CU came. Returns the resulting symbol
2051 table. */
2fdf6df6 2052
9291a0cd 2053static struct symtab *
a0f42c21 2054dw2_instantiate_symtab (struct dwarf2_per_cu_data *per_cu)
9291a0cd 2055{
95554aad 2056 gdb_assert (dwarf2_per_objfile->using_index);
9291a0cd
TT
2057 if (!per_cu->v.quick->symtab)
2058 {
2059 struct cleanup *back_to = make_cleanup (free_cached_comp_units, NULL);
2060 increment_reading_symtab ();
a0f42c21 2061 dw2_do_instantiate_symtab (per_cu);
95554aad 2062 process_cu_includes ();
9291a0cd
TT
2063 do_cleanups (back_to);
2064 }
2065 return per_cu->v.quick->symtab;
2066}
2067
1fd400ff 2068/* Return the CU given its index. */
2fdf6df6 2069
1fd400ff
TT
2070static struct dwarf2_per_cu_data *
2071dw2_get_cu (int index)
2072{
2073 if (index >= dwarf2_per_objfile->n_comp_units)
2074 {
2075 index -= dwarf2_per_objfile->n_comp_units;
d467dd73 2076 return dwarf2_per_objfile->all_type_units[index];
1fd400ff
TT
2077 }
2078 return dwarf2_per_objfile->all_comp_units[index];
2079}
2080
9291a0cd
TT
2081/* A helper function that knows how to read a 64-bit value in a way
2082 that doesn't make gdb die. Returns 1 if the conversion went ok, 0
2083 otherwise. */
2fdf6df6 2084
9291a0cd
TT
2085static int
2086extract_cu_value (const char *bytes, ULONGEST *result)
2087{
2088 if (sizeof (ULONGEST) < 8)
2089 {
2090 int i;
2091
2092 /* Ignore the upper 4 bytes if they are all zero. */
2093 for (i = 0; i < 4; ++i)
2094 if (bytes[i + 4] != 0)
2095 return 0;
2096
2097 *result = extract_unsigned_integer (bytes, 4, BFD_ENDIAN_LITTLE);
2098 }
2099 else
2100 *result = extract_unsigned_integer (bytes, 8, BFD_ENDIAN_LITTLE);
2101 return 1;
2102}
2103
2104/* Read the CU list from the mapped index, and use it to create all
2105 the CU objects for this objfile. Return 0 if something went wrong,
2106 1 if everything went ok. */
2fdf6df6 2107
9291a0cd 2108static int
1fd400ff
TT
2109create_cus_from_index (struct objfile *objfile, const gdb_byte *cu_list,
2110 offset_type cu_list_elements)
9291a0cd
TT
2111{
2112 offset_type i;
9291a0cd
TT
2113
2114 dwarf2_per_objfile->n_comp_units = cu_list_elements / 2;
2115 dwarf2_per_objfile->all_comp_units
2116 = obstack_alloc (&objfile->objfile_obstack,
2117 dwarf2_per_objfile->n_comp_units
2118 * sizeof (struct dwarf2_per_cu_data *));
2119
2120 for (i = 0; i < cu_list_elements; i += 2)
2121 {
2122 struct dwarf2_per_cu_data *the_cu;
2123 ULONGEST offset, length;
2124
2125 if (!extract_cu_value (cu_list, &offset)
2126 || !extract_cu_value (cu_list + 8, &length))
2127 return 0;
2128 cu_list += 2 * 8;
2129
2130 the_cu = OBSTACK_ZALLOC (&objfile->objfile_obstack,
2131 struct dwarf2_per_cu_data);
b64f50a1 2132 the_cu->offset.sect_off = offset;
9291a0cd
TT
2133 the_cu->length = length;
2134 the_cu->objfile = objfile;
3019eac3 2135 the_cu->info_or_types_section = &dwarf2_per_objfile->info;
9291a0cd
TT
2136 the_cu->v.quick = OBSTACK_ZALLOC (&objfile->objfile_obstack,
2137 struct dwarf2_per_cu_quick_data);
2138 dwarf2_per_objfile->all_comp_units[i / 2] = the_cu;
2139 }
2140
2141 return 1;
2142}
2143
1fd400ff 2144/* Create the signatured type hash table from the index. */
673bfd45 2145
1fd400ff 2146static int
673bfd45 2147create_signatured_type_table_from_index (struct objfile *objfile,
8b70b953 2148 struct dwarf2_section_info *section,
673bfd45
DE
2149 const gdb_byte *bytes,
2150 offset_type elements)
1fd400ff
TT
2151{
2152 offset_type i;
673bfd45 2153 htab_t sig_types_hash;
1fd400ff 2154
d467dd73
DE
2155 dwarf2_per_objfile->n_type_units = elements / 3;
2156 dwarf2_per_objfile->all_type_units
1fd400ff 2157 = obstack_alloc (&objfile->objfile_obstack,
d467dd73 2158 dwarf2_per_objfile->n_type_units
1fd400ff
TT
2159 * sizeof (struct dwarf2_per_cu_data *));
2160
673bfd45 2161 sig_types_hash = allocate_signatured_type_table (objfile);
1fd400ff
TT
2162
2163 for (i = 0; i < elements; i += 3)
2164 {
52dc124a
DE
2165 struct signatured_type *sig_type;
2166 ULONGEST offset, type_offset_in_tu, signature;
1fd400ff
TT
2167 void **slot;
2168
2169 if (!extract_cu_value (bytes, &offset)
52dc124a 2170 || !extract_cu_value (bytes + 8, &type_offset_in_tu))
1fd400ff
TT
2171 return 0;
2172 signature = extract_unsigned_integer (bytes + 16, 8, BFD_ENDIAN_LITTLE);
2173 bytes += 3 * 8;
2174
52dc124a 2175 sig_type = OBSTACK_ZALLOC (&objfile->objfile_obstack,
1fd400ff 2176 struct signatured_type);
52dc124a 2177 sig_type->signature = signature;
3019eac3
DE
2178 sig_type->type_offset_in_tu.cu_off = type_offset_in_tu;
2179 sig_type->per_cu.is_debug_types = 1;
2180 sig_type->per_cu.info_or_types_section = section;
52dc124a
DE
2181 sig_type->per_cu.offset.sect_off = offset;
2182 sig_type->per_cu.objfile = objfile;
2183 sig_type->per_cu.v.quick
1fd400ff
TT
2184 = OBSTACK_ZALLOC (&objfile->objfile_obstack,
2185 struct dwarf2_per_cu_quick_data);
2186
52dc124a
DE
2187 slot = htab_find_slot (sig_types_hash, sig_type, INSERT);
2188 *slot = sig_type;
1fd400ff 2189
52dc124a 2190 dwarf2_per_objfile->all_type_units[i / 3] = &sig_type->per_cu;
1fd400ff
TT
2191 }
2192
673bfd45 2193 dwarf2_per_objfile->signatured_types = sig_types_hash;
1fd400ff
TT
2194
2195 return 1;
2196}
2197
9291a0cd
TT
2198/* Read the address map data from the mapped index, and use it to
2199 populate the objfile's psymtabs_addrmap. */
2fdf6df6 2200
9291a0cd
TT
2201static void
2202create_addrmap_from_index (struct objfile *objfile, struct mapped_index *index)
2203{
2204 const gdb_byte *iter, *end;
2205 struct obstack temp_obstack;
2206 struct addrmap *mutable_map;
2207 struct cleanup *cleanup;
2208 CORE_ADDR baseaddr;
2209
2210 obstack_init (&temp_obstack);
2211 cleanup = make_cleanup_obstack_free (&temp_obstack);
2212 mutable_map = addrmap_create_mutable (&temp_obstack);
2213
2214 iter = index->address_table;
2215 end = iter + index->address_table_size;
2216
2217 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
2218
2219 while (iter < end)
2220 {
2221 ULONGEST hi, lo, cu_index;
2222 lo = extract_unsigned_integer (iter, 8, BFD_ENDIAN_LITTLE);
2223 iter += 8;
2224 hi = extract_unsigned_integer (iter, 8, BFD_ENDIAN_LITTLE);
2225 iter += 8;
2226 cu_index = extract_unsigned_integer (iter, 4, BFD_ENDIAN_LITTLE);
2227 iter += 4;
2228
2229 addrmap_set_empty (mutable_map, lo + baseaddr, hi + baseaddr - 1,
1fd400ff 2230 dw2_get_cu (cu_index));
9291a0cd
TT
2231 }
2232
2233 objfile->psymtabs_addrmap = addrmap_create_fixed (mutable_map,
2234 &objfile->objfile_obstack);
2235 do_cleanups (cleanup);
2236}
2237
59d7bcaf
JK
2238/* The hash function for strings in the mapped index. This is the same as
2239 SYMBOL_HASH_NEXT, but we keep a separate copy to maintain control over the
2240 implementation. This is necessary because the hash function is tied to the
2241 format of the mapped index file. The hash values do not have to match with
559a7a62
JK
2242 SYMBOL_HASH_NEXT.
2243
2244 Use INT_MAX for INDEX_VERSION if you generate the current index format. */
2fdf6df6 2245
9291a0cd 2246static hashval_t
559a7a62 2247mapped_index_string_hash (int index_version, const void *p)
9291a0cd
TT
2248{
2249 const unsigned char *str = (const unsigned char *) p;
2250 hashval_t r = 0;
2251 unsigned char c;
2252
2253 while ((c = *str++) != 0)
559a7a62
JK
2254 {
2255 if (index_version >= 5)
2256 c = tolower (c);
2257 r = r * 67 + c - 113;
2258 }
9291a0cd
TT
2259
2260 return r;
2261}
2262
2263/* Find a slot in the mapped index INDEX for the object named NAME.
2264 If NAME is found, set *VEC_OUT to point to the CU vector in the
2265 constant pool and return 1. If NAME cannot be found, return 0. */
2fdf6df6 2266
9291a0cd
TT
2267static int
2268find_slot_in_mapped_hash (struct mapped_index *index, const char *name,
2269 offset_type **vec_out)
2270{
0cf03b49
JK
2271 struct cleanup *back_to = make_cleanup (null_cleanup, 0);
2272 offset_type hash;
9291a0cd 2273 offset_type slot, step;
559a7a62 2274 int (*cmp) (const char *, const char *);
9291a0cd 2275
0cf03b49
JK
2276 if (current_language->la_language == language_cplus
2277 || current_language->la_language == language_java
2278 || current_language->la_language == language_fortran)
2279 {
2280 /* NAME is already canonical. Drop any qualifiers as .gdb_index does
2281 not contain any. */
2282 const char *paren = strchr (name, '(');
2283
2284 if (paren)
2285 {
2286 char *dup;
2287
2288 dup = xmalloc (paren - name + 1);
2289 memcpy (dup, name, paren - name);
2290 dup[paren - name] = 0;
2291
2292 make_cleanup (xfree, dup);
2293 name = dup;
2294 }
2295 }
2296
559a7a62 2297 /* Index version 4 did not support case insensitive searches. But the
feea76c2 2298 indices for case insensitive languages are built in lowercase, therefore
559a7a62
JK
2299 simulate our NAME being searched is also lowercased. */
2300 hash = mapped_index_string_hash ((index->version == 4
2301 && case_sensitivity == case_sensitive_off
2302 ? 5 : index->version),
2303 name);
2304
3876f04e
DE
2305 slot = hash & (index->symbol_table_slots - 1);
2306 step = ((hash * 17) & (index->symbol_table_slots - 1)) | 1;
559a7a62 2307 cmp = (case_sensitivity == case_sensitive_on ? strcmp : strcasecmp);
9291a0cd
TT
2308
2309 for (;;)
2310 {
2311 /* Convert a slot number to an offset into the table. */
2312 offset_type i = 2 * slot;
2313 const char *str;
3876f04e 2314 if (index->symbol_table[i] == 0 && index->symbol_table[i + 1] == 0)
0cf03b49
JK
2315 {
2316 do_cleanups (back_to);
2317 return 0;
2318 }
9291a0cd 2319
3876f04e 2320 str = index->constant_pool + MAYBE_SWAP (index->symbol_table[i]);
559a7a62 2321 if (!cmp (name, str))
9291a0cd
TT
2322 {
2323 *vec_out = (offset_type *) (index->constant_pool
3876f04e 2324 + MAYBE_SWAP (index->symbol_table[i + 1]));
0cf03b49 2325 do_cleanups (back_to);
9291a0cd
TT
2326 return 1;
2327 }
2328
3876f04e 2329 slot = (slot + step) & (index->symbol_table_slots - 1);
9291a0cd
TT
2330 }
2331}
2332
2333/* Read the index file. If everything went ok, initialize the "quick"
2334 elements of all the CUs and return 1. Otherwise, return 0. */
2fdf6df6 2335
9291a0cd
TT
2336static int
2337dwarf2_read_index (struct objfile *objfile)
2338{
9291a0cd
TT
2339 char *addr;
2340 struct mapped_index *map;
b3b272e1 2341 offset_type *metadata;
ac0b195c
KW
2342 const gdb_byte *cu_list;
2343 const gdb_byte *types_list = NULL;
2344 offset_type version, cu_list_elements;
2345 offset_type types_list_elements = 0;
1fd400ff 2346 int i;
9291a0cd 2347
9e0ac564 2348 if (dwarf2_section_empty_p (&dwarf2_per_objfile->gdb_index))
9291a0cd 2349 return 0;
82430852
JK
2350
2351 /* Older elfutils strip versions could keep the section in the main
2352 executable while splitting it for the separate debug info file. */
2353 if ((bfd_get_file_flags (dwarf2_per_objfile->gdb_index.asection)
2354 & SEC_HAS_CONTENTS) == 0)
2355 return 0;
2356
9291a0cd
TT
2357 dwarf2_read_section (objfile, &dwarf2_per_objfile->gdb_index);
2358
2359 addr = dwarf2_per_objfile->gdb_index.buffer;
2360 /* Version check. */
1fd400ff 2361 version = MAYBE_SWAP (*(offset_type *) addr);
987d643c 2362 /* Versions earlier than 3 emitted every copy of a psymbol. This
a6e293d1 2363 causes the index to behave very poorly for certain requests. Version 3
831adc1f 2364 contained incomplete addrmap. So, it seems better to just ignore such
481860b3 2365 indices. */
831adc1f 2366 if (version < 4)
481860b3
GB
2367 {
2368 static int warning_printed = 0;
2369 if (!warning_printed)
2370 {
2371 warning (_("Skipping obsolete .gdb_index section in %s."),
2372 objfile->name);
2373 warning_printed = 1;
2374 }
2375 return 0;
2376 }
2377 /* Index version 4 uses a different hash function than index version
2378 5 and later.
2379
2380 Versions earlier than 6 did not emit psymbols for inlined
2381 functions. Using these files will cause GDB not to be able to
2382 set breakpoints on inlined functions by name, so we ignore these
2383 indices unless the --use-deprecated-index-sections command line
2384 option was supplied. */
2385 if (version < 6 && !use_deprecated_index_sections)
2386 {
2387 static int warning_printed = 0;
2388 if (!warning_printed)
2389 {
2390 warning (_("Skipping deprecated .gdb_index section in %s, pass "
2391 "--use-deprecated-index-sections to use them anyway"),
2392 objfile->name);
2393 warning_printed = 1;
2394 }
2395 return 0;
2396 }
2397 /* Indexes with higher version than the one supported by GDB may be no
594e8718 2398 longer backward compatible. */
156942c7 2399 if (version > 7)
594e8718 2400 return 0;
9291a0cd
TT
2401
2402 map = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct mapped_index);
559a7a62 2403 map->version = version;
b3b272e1 2404 map->total_size = dwarf2_per_objfile->gdb_index.size;
9291a0cd
TT
2405
2406 metadata = (offset_type *) (addr + sizeof (offset_type));
1fd400ff
TT
2407
2408 i = 0;
2409 cu_list = addr + MAYBE_SWAP (metadata[i]);
2410 cu_list_elements = ((MAYBE_SWAP (metadata[i + 1]) - MAYBE_SWAP (metadata[i]))
9291a0cd 2411 / 8);
1fd400ff
TT
2412 ++i;
2413
987d643c
TT
2414 types_list = addr + MAYBE_SWAP (metadata[i]);
2415 types_list_elements = ((MAYBE_SWAP (metadata[i + 1])
2416 - MAYBE_SWAP (metadata[i]))
2417 / 8);
2418 ++i;
1fd400ff
TT
2419
2420 map->address_table = addr + MAYBE_SWAP (metadata[i]);
2421 map->address_table_size = (MAYBE_SWAP (metadata[i + 1])
2422 - MAYBE_SWAP (metadata[i]));
2423 ++i;
2424
3876f04e
DE
2425 map->symbol_table = (offset_type *) (addr + MAYBE_SWAP (metadata[i]));
2426 map->symbol_table_slots = ((MAYBE_SWAP (metadata[i + 1])
2427 - MAYBE_SWAP (metadata[i]))
2428 / (2 * sizeof (offset_type)));
1fd400ff 2429 ++i;
9291a0cd 2430
1fd400ff
TT
2431 map->constant_pool = addr + MAYBE_SWAP (metadata[i]);
2432
0fefef59
DE
2433 /* Don't use the index if it's empty. */
2434 if (map->symbol_table_slots == 0)
2435 return 0;
2436
1fd400ff
TT
2437 if (!create_cus_from_index (objfile, cu_list, cu_list_elements))
2438 return 0;
2439
8b70b953
TT
2440 if (types_list_elements)
2441 {
2442 struct dwarf2_section_info *section;
2443
2444 /* We can only handle a single .debug_types when we have an
2445 index. */
2446 if (VEC_length (dwarf2_section_info_def, dwarf2_per_objfile->types) != 1)
2447 return 0;
2448
2449 section = VEC_index (dwarf2_section_info_def,
2450 dwarf2_per_objfile->types, 0);
2451
2452 if (!create_signatured_type_table_from_index (objfile, section,
2453 types_list,
2454 types_list_elements))
2455 return 0;
2456 }
9291a0cd
TT
2457
2458 create_addrmap_from_index (objfile, map);
2459
2460 dwarf2_per_objfile->index_table = map;
2461 dwarf2_per_objfile->using_index = 1;
7b9f3c50
DE
2462 dwarf2_per_objfile->quick_file_names_table =
2463 create_quick_file_names_table (dwarf2_per_objfile->n_comp_units);
9291a0cd
TT
2464
2465 return 1;
2466}
2467
2468/* A helper for the "quick" functions which sets the global
2469 dwarf2_per_objfile according to OBJFILE. */
2fdf6df6 2470
9291a0cd
TT
2471static void
2472dw2_setup (struct objfile *objfile)
2473{
2474 dwarf2_per_objfile = objfile_data (objfile, dwarf2_objfile_data_key);
2475 gdb_assert (dwarf2_per_objfile);
2476}
2477
dee91e82 2478/* die_reader_func for dw2_get_file_names. */
2fdf6df6 2479
dee91e82
DE
2480static void
2481dw2_get_file_names_reader (const struct die_reader_specs *reader,
2482 gdb_byte *info_ptr,
2483 struct die_info *comp_unit_die,
2484 int has_children,
2485 void *data)
9291a0cd 2486{
dee91e82
DE
2487 struct dwarf2_cu *cu = reader->cu;
2488 struct dwarf2_per_cu_data *this_cu = cu->per_cu;
2489 struct objfile *objfile = dwarf2_per_objfile->objfile;
7b9f3c50 2490 struct line_header *lh;
9291a0cd 2491 struct attribute *attr;
dee91e82 2492 int i;
9291a0cd 2493 char *name, *comp_dir;
7b9f3c50
DE
2494 void **slot;
2495 struct quick_file_names *qfn;
2496 unsigned int line_offset;
9291a0cd 2497
07261596
TT
2498 /* Our callers never want to match partial units -- instead they
2499 will match the enclosing full CU. */
2500 if (comp_unit_die->tag == DW_TAG_partial_unit)
2501 {
2502 this_cu->v.quick->no_file_data = 1;
2503 return;
2504 }
2505
7b9f3c50
DE
2506 lh = NULL;
2507 slot = NULL;
2508 line_offset = 0;
dee91e82
DE
2509
2510 attr = dwarf2_attr (comp_unit_die, DW_AT_stmt_list, cu);
9291a0cd
TT
2511 if (attr)
2512 {
7b9f3c50
DE
2513 struct quick_file_names find_entry;
2514
2515 line_offset = DW_UNSND (attr);
2516
2517 /* We may have already read in this line header (TU line header sharing).
2518 If we have we're done. */
2519 find_entry.offset = line_offset;
2520 slot = htab_find_slot (dwarf2_per_objfile->quick_file_names_table,
2521 &find_entry, INSERT);
2522 if (*slot != NULL)
2523 {
7b9f3c50 2524 this_cu->v.quick->file_names = *slot;
dee91e82 2525 return;
7b9f3c50
DE
2526 }
2527
3019eac3 2528 lh = dwarf_decode_line_header (line_offset, cu);
9291a0cd
TT
2529 }
2530 if (lh == NULL)
2531 {
7b9f3c50 2532 this_cu->v.quick->no_file_data = 1;
dee91e82 2533 return;
9291a0cd
TT
2534 }
2535
7b9f3c50
DE
2536 qfn = obstack_alloc (&objfile->objfile_obstack, sizeof (*qfn));
2537 qfn->offset = line_offset;
2538 gdb_assert (slot != NULL);
2539 *slot = qfn;
9291a0cd 2540
dee91e82 2541 find_file_and_directory (comp_unit_die, cu, &name, &comp_dir);
9291a0cd 2542
7b9f3c50
DE
2543 qfn->num_file_names = lh->num_file_names;
2544 qfn->file_names = obstack_alloc (&objfile->objfile_obstack,
2545 lh->num_file_names * sizeof (char *));
9291a0cd 2546 for (i = 0; i < lh->num_file_names; ++i)
7b9f3c50
DE
2547 qfn->file_names[i] = file_full_name (i + 1, lh, comp_dir);
2548 qfn->real_names = NULL;
9291a0cd 2549
7b9f3c50 2550 free_line_header (lh);
7b9f3c50
DE
2551
2552 this_cu->v.quick->file_names = qfn;
dee91e82
DE
2553}
2554
2555/* A helper for the "quick" functions which attempts to read the line
2556 table for THIS_CU. */
2557
2558static struct quick_file_names *
2559dw2_get_file_names (struct objfile *objfile,
2560 struct dwarf2_per_cu_data *this_cu)
2561{
2562 if (this_cu->v.quick->file_names != NULL)
2563 return this_cu->v.quick->file_names;
2564 /* If we know there is no line data, no point in looking again. */
2565 if (this_cu->v.quick->no_file_data)
2566 return NULL;
2567
3019eac3
DE
2568 /* If DWO files are in use, we can still find the DW_AT_stmt_list attribute
2569 in the stub for CUs, there's is no need to lookup the DWO file.
2570 However, that's not the case for TUs where DW_AT_stmt_list lives in the
2571 DWO file. */
2572 if (this_cu->is_debug_types)
fd820528 2573 init_cutu_and_read_dies (this_cu, 0, 0, dw2_get_file_names_reader, NULL);
3019eac3
DE
2574 else
2575 init_cutu_and_read_dies_simple (this_cu, dw2_get_file_names_reader, NULL);
dee91e82
DE
2576
2577 if (this_cu->v.quick->no_file_data)
2578 return NULL;
2579 return this_cu->v.quick->file_names;
9291a0cd
TT
2580}
2581
2582/* A helper for the "quick" functions which computes and caches the
7b9f3c50 2583 real path for a given file name from the line table. */
2fdf6df6 2584
9291a0cd 2585static const char *
7b9f3c50
DE
2586dw2_get_real_path (struct objfile *objfile,
2587 struct quick_file_names *qfn, int index)
9291a0cd 2588{
7b9f3c50
DE
2589 if (qfn->real_names == NULL)
2590 qfn->real_names = OBSTACK_CALLOC (&objfile->objfile_obstack,
2591 qfn->num_file_names, sizeof (char *));
9291a0cd 2592
7b9f3c50
DE
2593 if (qfn->real_names[index] == NULL)
2594 qfn->real_names[index] = gdb_realpath (qfn->file_names[index]);
9291a0cd 2595
7b9f3c50 2596 return qfn->real_names[index];
9291a0cd
TT
2597}
2598
2599static struct symtab *
2600dw2_find_last_source_symtab (struct objfile *objfile)
2601{
2602 int index;
ae2de4f8 2603
9291a0cd
TT
2604 dw2_setup (objfile);
2605 index = dwarf2_per_objfile->n_comp_units - 1;
a0f42c21 2606 return dw2_instantiate_symtab (dw2_get_cu (index));
9291a0cd
TT
2607}
2608
7b9f3c50
DE
2609/* Traversal function for dw2_forget_cached_source_info. */
2610
2611static int
2612dw2_free_cached_file_names (void **slot, void *info)
9291a0cd 2613{
7b9f3c50 2614 struct quick_file_names *file_data = (struct quick_file_names *) *slot;
9291a0cd 2615
7b9f3c50 2616 if (file_data->real_names)
9291a0cd 2617 {
7b9f3c50 2618 int i;
9291a0cd 2619
7b9f3c50 2620 for (i = 0; i < file_data->num_file_names; ++i)
9291a0cd 2621 {
7b9f3c50
DE
2622 xfree ((void*) file_data->real_names[i]);
2623 file_data->real_names[i] = NULL;
9291a0cd
TT
2624 }
2625 }
7b9f3c50
DE
2626
2627 return 1;
2628}
2629
2630static void
2631dw2_forget_cached_source_info (struct objfile *objfile)
2632{
2633 dw2_setup (objfile);
2634
2635 htab_traverse_noresize (dwarf2_per_objfile->quick_file_names_table,
2636 dw2_free_cached_file_names, NULL);
9291a0cd
TT
2637}
2638
f8eba3c6
TT
2639/* Helper function for dw2_map_symtabs_matching_filename that expands
2640 the symtabs and calls the iterator. */
2641
2642static int
2643dw2_map_expand_apply (struct objfile *objfile,
2644 struct dwarf2_per_cu_data *per_cu,
2645 const char *name,
2646 const char *full_path, const char *real_path,
2647 int (*callback) (struct symtab *, void *),
2648 void *data)
2649{
2650 struct symtab *last_made = objfile->symtabs;
2651
2652 /* Don't visit already-expanded CUs. */
2653 if (per_cu->v.quick->symtab)
2654 return 0;
2655
2656 /* This may expand more than one symtab, and we want to iterate over
2657 all of them. */
a0f42c21 2658 dw2_instantiate_symtab (per_cu);
f8eba3c6
TT
2659
2660 return iterate_over_some_symtabs (name, full_path, real_path, callback, data,
2661 objfile->symtabs, last_made);
2662}
2663
2664/* Implementation of the map_symtabs_matching_filename method. */
2665
9291a0cd 2666static int
f8eba3c6
TT
2667dw2_map_symtabs_matching_filename (struct objfile *objfile, const char *name,
2668 const char *full_path, const char *real_path,
2669 int (*callback) (struct symtab *, void *),
2670 void *data)
9291a0cd
TT
2671{
2672 int i;
c011a4f4 2673 const char *name_basename = lbasename (name);
4aac40c8
TT
2674 int name_len = strlen (name);
2675 int is_abs = IS_ABSOLUTE_PATH (name);
9291a0cd
TT
2676
2677 dw2_setup (objfile);
ae2de4f8 2678
1fd400ff 2679 for (i = 0; i < (dwarf2_per_objfile->n_comp_units
d467dd73 2680 + dwarf2_per_objfile->n_type_units); ++i)
9291a0cd
TT
2681 {
2682 int j;
e254ef6a 2683 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
7b9f3c50 2684 struct quick_file_names *file_data;
9291a0cd 2685
3d7bb9d9 2686 /* We only need to look at symtabs not already expanded. */
e254ef6a 2687 if (per_cu->v.quick->symtab)
9291a0cd
TT
2688 continue;
2689
7b9f3c50
DE
2690 file_data = dw2_get_file_names (objfile, per_cu);
2691 if (file_data == NULL)
9291a0cd
TT
2692 continue;
2693
7b9f3c50 2694 for (j = 0; j < file_data->num_file_names; ++j)
9291a0cd 2695 {
7b9f3c50 2696 const char *this_name = file_data->file_names[j];
9291a0cd 2697
4aac40c8
TT
2698 if (FILENAME_CMP (name, this_name) == 0
2699 || (!is_abs && compare_filenames_for_search (this_name,
2700 name, name_len)))
9291a0cd 2701 {
f8eba3c6
TT
2702 if (dw2_map_expand_apply (objfile, per_cu,
2703 name, full_path, real_path,
2704 callback, data))
2705 return 1;
4aac40c8 2706 }
9291a0cd 2707
c011a4f4
DE
2708 /* Before we invoke realpath, which can get expensive when many
2709 files are involved, do a quick comparison of the basenames. */
2710 if (! basenames_may_differ
2711 && FILENAME_CMP (lbasename (this_name), name_basename) != 0)
2712 continue;
2713
9291a0cd
TT
2714 if (full_path != NULL)
2715 {
7b9f3c50
DE
2716 const char *this_real_name = dw2_get_real_path (objfile,
2717 file_data, j);
9291a0cd 2718
7b9f3c50 2719 if (this_real_name != NULL
4aac40c8
TT
2720 && (FILENAME_CMP (full_path, this_real_name) == 0
2721 || (!is_abs
2722 && compare_filenames_for_search (this_real_name,
2723 name, name_len))))
9291a0cd 2724 {
f8eba3c6
TT
2725 if (dw2_map_expand_apply (objfile, per_cu,
2726 name, full_path, real_path,
2727 callback, data))
2728 return 1;
9291a0cd
TT
2729 }
2730 }
2731
2732 if (real_path != NULL)
2733 {
7b9f3c50
DE
2734 const char *this_real_name = dw2_get_real_path (objfile,
2735 file_data, j);
9291a0cd 2736
7b9f3c50 2737 if (this_real_name != NULL
4aac40c8
TT
2738 && (FILENAME_CMP (real_path, this_real_name) == 0
2739 || (!is_abs
2740 && compare_filenames_for_search (this_real_name,
2741 name, name_len))))
9291a0cd 2742 {
f8eba3c6
TT
2743 if (dw2_map_expand_apply (objfile, per_cu,
2744 name, full_path, real_path,
2745 callback, data))
2746 return 1;
9291a0cd
TT
2747 }
2748 }
2749 }
2750 }
2751
9291a0cd
TT
2752 return 0;
2753}
2754
2755static struct symtab *
2756dw2_lookup_symbol (struct objfile *objfile, int block_index,
2757 const char *name, domain_enum domain)
2758{
774b6a14 2759 /* We do all the work in the pre_expand_symtabs_matching hook
9291a0cd
TT
2760 instead. */
2761 return NULL;
2762}
2763
2764/* A helper function that expands all symtabs that hold an object
156942c7
DE
2765 named NAME. If WANT_SPECIFIC_BLOCK is non-zero, only look for
2766 symbols in block BLOCK_KIND. */
2fdf6df6 2767
9291a0cd 2768static void
156942c7
DE
2769dw2_do_expand_symtabs_matching (struct objfile *objfile,
2770 int want_specific_block,
2771 enum block_enum block_kind,
2772 const char *name, domain_enum domain)
9291a0cd 2773{
156942c7
DE
2774 struct mapped_index *index;
2775
9291a0cd
TT
2776 dw2_setup (objfile);
2777
156942c7
DE
2778 index = dwarf2_per_objfile->index_table;
2779
ae2de4f8 2780 /* index_table is NULL if OBJF_READNOW. */
156942c7 2781 if (index)
9291a0cd
TT
2782 {
2783 offset_type *vec;
2784
156942c7 2785 if (find_slot_in_mapped_hash (index, name, &vec))
9291a0cd
TT
2786 {
2787 offset_type i, len = MAYBE_SWAP (*vec);
2788 for (i = 0; i < len; ++i)
2789 {
156942c7
DE
2790 offset_type cu_index_and_attrs = MAYBE_SWAP (vec[i + 1]);
2791 offset_type cu_index = GDB_INDEX_CU_VALUE (cu_index_and_attrs);
e254ef6a 2792 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (cu_index);
156942c7
DE
2793 int want_static = block_kind != GLOBAL_BLOCK;
2794 /* This value is only valid for index versions >= 7. */
2795 int is_static = GDB_INDEX_SYMBOL_STATIC_VALUE (cu_index_and_attrs);
2796 gdb_index_symbol_kind symbol_kind =
2797 GDB_INDEX_SYMBOL_KIND_VALUE (cu_index_and_attrs);
2798
2799 if (want_specific_block
2800 && index->version >= 7
2801 && want_static != is_static)
2802 continue;
2803
2804 /* Only check the symbol's kind if it has one.
2805 Indices prior to version 7 don't record it. */
2806 if (index->version >= 7)
2807 {
2808 switch (domain)
2809 {
2810 case VAR_DOMAIN:
2811 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_VARIABLE
2812 && symbol_kind != GDB_INDEX_SYMBOL_KIND_FUNCTION
2813 /* Some types are also in VAR_DOMAIN. */
2814 && symbol_kind != GDB_INDEX_SYMBOL_KIND_TYPE)
2815 continue;
2816 break;
2817 case STRUCT_DOMAIN:
2818 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_TYPE)
2819 continue;
2820 break;
2821 case LABEL_DOMAIN:
2822 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_OTHER)
2823 continue;
2824 break;
2825 default:
2826 break;
2827 }
2828 }
1fd400ff 2829
a0f42c21 2830 dw2_instantiate_symtab (per_cu);
9291a0cd
TT
2831 }
2832 }
2833 }
2834}
2835
774b6a14
TT
2836static void
2837dw2_pre_expand_symtabs_matching (struct objfile *objfile,
8903c50d 2838 enum block_enum block_kind, const char *name,
774b6a14 2839 domain_enum domain)
9291a0cd 2840{
156942c7 2841 dw2_do_expand_symtabs_matching (objfile, 1, block_kind, name, domain);
9291a0cd
TT
2842}
2843
2844static void
2845dw2_print_stats (struct objfile *objfile)
2846{
2847 int i, count;
2848
2849 dw2_setup (objfile);
2850 count = 0;
1fd400ff 2851 for (i = 0; i < (dwarf2_per_objfile->n_comp_units
d467dd73 2852 + dwarf2_per_objfile->n_type_units); ++i)
9291a0cd 2853 {
e254ef6a 2854 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
9291a0cd 2855
e254ef6a 2856 if (!per_cu->v.quick->symtab)
9291a0cd
TT
2857 ++count;
2858 }
2859 printf_filtered (_(" Number of unread CUs: %d\n"), count);
2860}
2861
2862static void
2863dw2_dump (struct objfile *objfile)
2864{
2865 /* Nothing worth printing. */
2866}
2867
2868static void
2869dw2_relocate (struct objfile *objfile, struct section_offsets *new_offsets,
2870 struct section_offsets *delta)
2871{
2872 /* There's nothing to relocate here. */
2873}
2874
2875static void
2876dw2_expand_symtabs_for_function (struct objfile *objfile,
2877 const char *func_name)
2878{
156942c7
DE
2879 /* Note: It doesn't matter what we pass for block_kind here. */
2880 dw2_do_expand_symtabs_matching (objfile, 0, GLOBAL_BLOCK, func_name,
2881 VAR_DOMAIN);
9291a0cd
TT
2882}
2883
2884static void
2885dw2_expand_all_symtabs (struct objfile *objfile)
2886{
2887 int i;
2888
2889 dw2_setup (objfile);
1fd400ff
TT
2890
2891 for (i = 0; i < (dwarf2_per_objfile->n_comp_units
d467dd73 2892 + dwarf2_per_objfile->n_type_units); ++i)
9291a0cd 2893 {
e254ef6a 2894 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
9291a0cd 2895
a0f42c21 2896 dw2_instantiate_symtab (per_cu);
9291a0cd
TT
2897 }
2898}
2899
2900static void
2901dw2_expand_symtabs_with_filename (struct objfile *objfile,
2902 const char *filename)
2903{
2904 int i;
2905
2906 dw2_setup (objfile);
d4637a04
DE
2907
2908 /* We don't need to consider type units here.
2909 This is only called for examining code, e.g. expand_line_sal.
2910 There can be an order of magnitude (or more) more type units
2911 than comp units, and we avoid them if we can. */
2912
2913 for (i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
9291a0cd
TT
2914 {
2915 int j;
e254ef6a 2916 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
7b9f3c50 2917 struct quick_file_names *file_data;
9291a0cd 2918
3d7bb9d9 2919 /* We only need to look at symtabs not already expanded. */
e254ef6a 2920 if (per_cu->v.quick->symtab)
9291a0cd
TT
2921 continue;
2922
7b9f3c50
DE
2923 file_data = dw2_get_file_names (objfile, per_cu);
2924 if (file_data == NULL)
9291a0cd
TT
2925 continue;
2926
7b9f3c50 2927 for (j = 0; j < file_data->num_file_names; ++j)
9291a0cd 2928 {
7b9f3c50 2929 const char *this_name = file_data->file_names[j];
1ef75ecc 2930 if (FILENAME_CMP (this_name, filename) == 0)
9291a0cd 2931 {
a0f42c21 2932 dw2_instantiate_symtab (per_cu);
9291a0cd
TT
2933 break;
2934 }
2935 }
2936 }
2937}
2938
356d9f9d
TT
2939/* A helper function for dw2_find_symbol_file that finds the primary
2940 file name for a given CU. This is a die_reader_func. */
2941
2942static void
2943dw2_get_primary_filename_reader (const struct die_reader_specs *reader,
2944 gdb_byte *info_ptr,
2945 struct die_info *comp_unit_die,
2946 int has_children,
2947 void *data)
2948{
2949 const char **result_ptr = data;
2950 struct dwarf2_cu *cu = reader->cu;
2951 struct attribute *attr;
2952
2953 attr = dwarf2_attr (comp_unit_die, DW_AT_name, cu);
2954 if (attr == NULL)
2955 *result_ptr = NULL;
2956 else
2957 *result_ptr = DW_STRING (attr);
2958}
2959
dd786858 2960static const char *
9291a0cd
TT
2961dw2_find_symbol_file (struct objfile *objfile, const char *name)
2962{
e254ef6a 2963 struct dwarf2_per_cu_data *per_cu;
9291a0cd 2964 offset_type *vec;
7b9f3c50 2965 struct quick_file_names *file_data;
356d9f9d 2966 const char *filename;
9291a0cd
TT
2967
2968 dw2_setup (objfile);
2969
ae2de4f8 2970 /* index_table is NULL if OBJF_READNOW. */
9291a0cd 2971 if (!dwarf2_per_objfile->index_table)
96408a79
SA
2972 {
2973 struct symtab *s;
2974
d790cf0a
DE
2975 ALL_OBJFILE_PRIMARY_SYMTABS (objfile, s)
2976 {
2977 struct blockvector *bv = BLOCKVECTOR (s);
2978 const struct block *block = BLOCKVECTOR_BLOCK (bv, GLOBAL_BLOCK);
2979 struct symbol *sym = lookup_block_symbol (block, name, VAR_DOMAIN);
2980
2981 if (sym)
2982 return sym->symtab->filename;
2983 }
96408a79
SA
2984 return NULL;
2985 }
9291a0cd
TT
2986
2987 if (!find_slot_in_mapped_hash (dwarf2_per_objfile->index_table,
2988 name, &vec))
2989 return NULL;
2990
2991 /* Note that this just looks at the very first one named NAME -- but
2992 actually we are looking for a function. find_main_filename
2993 should be rewritten so that it doesn't require a custom hook. It
2994 could just use the ordinary symbol tables. */
2995 /* vec[0] is the length, which must always be >0. */
156942c7 2996 per_cu = dw2_get_cu (GDB_INDEX_CU_VALUE (MAYBE_SWAP (vec[1])));
9291a0cd 2997
356d9f9d
TT
2998 if (per_cu->v.quick->symtab != NULL)
2999 return per_cu->v.quick->symtab->filename;
3000
663b969e
TT
3001 init_cutu_and_read_dies (per_cu, 0, 0, dw2_get_primary_filename_reader,
3002 &filename);
9291a0cd 3003
356d9f9d 3004 return filename;
9291a0cd
TT
3005}
3006
3007static void
40658b94
PH
3008dw2_map_matching_symbols (const char * name, domain_enum namespace,
3009 struct objfile *objfile, int global,
3010 int (*callback) (struct block *,
3011 struct symbol *, void *),
2edb89d3
JK
3012 void *data, symbol_compare_ftype *match,
3013 symbol_compare_ftype *ordered_compare)
9291a0cd 3014{
40658b94 3015 /* Currently unimplemented; used for Ada. The function can be called if the
a9e6a4bb
JK
3016 current language is Ada for a non-Ada objfile using GNU index. As Ada
3017 does not look for non-Ada symbols this function should just return. */
9291a0cd
TT
3018}
3019
3020static void
f8eba3c6
TT
3021dw2_expand_symtabs_matching
3022 (struct objfile *objfile,
3023 int (*file_matcher) (const char *, void *),
e078317b 3024 int (*name_matcher) (const char *, void *),
f8eba3c6
TT
3025 enum search_domain kind,
3026 void *data)
9291a0cd
TT
3027{
3028 int i;
3029 offset_type iter;
4b5246aa 3030 struct mapped_index *index;
9291a0cd
TT
3031
3032 dw2_setup (objfile);
ae2de4f8
DE
3033
3034 /* index_table is NULL if OBJF_READNOW. */
9291a0cd
TT
3035 if (!dwarf2_per_objfile->index_table)
3036 return;
4b5246aa 3037 index = dwarf2_per_objfile->index_table;
9291a0cd 3038
7b08b9eb 3039 if (file_matcher != NULL)
24c79950
TT
3040 {
3041 struct cleanup *cleanup;
3042 htab_t visited_found, visited_not_found;
3043
3044 visited_found = htab_create_alloc (10,
3045 htab_hash_pointer, htab_eq_pointer,
3046 NULL, xcalloc, xfree);
3047 cleanup = make_cleanup_htab_delete (visited_found);
3048 visited_not_found = htab_create_alloc (10,
3049 htab_hash_pointer, htab_eq_pointer,
3050 NULL, xcalloc, xfree);
3051 make_cleanup_htab_delete (visited_not_found);
3052
3053 for (i = 0; i < (dwarf2_per_objfile->n_comp_units
3054 + dwarf2_per_objfile->n_type_units); ++i)
3055 {
3056 int j;
3057 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
3058 struct quick_file_names *file_data;
3059 void **slot;
7b08b9eb 3060
24c79950 3061 per_cu->v.quick->mark = 0;
3d7bb9d9 3062
24c79950
TT
3063 /* We only need to look at symtabs not already expanded. */
3064 if (per_cu->v.quick->symtab)
3065 continue;
7b08b9eb 3066
24c79950
TT
3067 file_data = dw2_get_file_names (objfile, per_cu);
3068 if (file_data == NULL)
3069 continue;
7b08b9eb 3070
24c79950
TT
3071 if (htab_find (visited_not_found, file_data) != NULL)
3072 continue;
3073 else if (htab_find (visited_found, file_data) != NULL)
3074 {
3075 per_cu->v.quick->mark = 1;
3076 continue;
3077 }
3078
3079 for (j = 0; j < file_data->num_file_names; ++j)
3080 {
3081 if (file_matcher (file_data->file_names[j], data))
3082 {
3083 per_cu->v.quick->mark = 1;
3084 break;
3085 }
3086 }
3087
3088 slot = htab_find_slot (per_cu->v.quick->mark
3089 ? visited_found
3090 : visited_not_found,
3091 file_data, INSERT);
3092 *slot = file_data;
3093 }
3094
3095 do_cleanups (cleanup);
3096 }
9291a0cd 3097
3876f04e 3098 for (iter = 0; iter < index->symbol_table_slots; ++iter)
9291a0cd
TT
3099 {
3100 offset_type idx = 2 * iter;
3101 const char *name;
3102 offset_type *vec, vec_len, vec_idx;
3103
3876f04e 3104 if (index->symbol_table[idx] == 0 && index->symbol_table[idx + 1] == 0)
9291a0cd
TT
3105 continue;
3106
3876f04e 3107 name = index->constant_pool + MAYBE_SWAP (index->symbol_table[idx]);
9291a0cd 3108
e078317b 3109 if (! (*name_matcher) (name, data))
9291a0cd
TT
3110 continue;
3111
3112 /* The name was matched, now expand corresponding CUs that were
3113 marked. */
4b5246aa 3114 vec = (offset_type *) (index->constant_pool
3876f04e 3115 + MAYBE_SWAP (index->symbol_table[idx + 1]));
9291a0cd
TT
3116 vec_len = MAYBE_SWAP (vec[0]);
3117 for (vec_idx = 0; vec_idx < vec_len; ++vec_idx)
3118 {
e254ef6a 3119 struct dwarf2_per_cu_data *per_cu;
156942c7
DE
3120 offset_type cu_index_and_attrs = MAYBE_SWAP (vec[vec_idx + 1]);
3121 gdb_index_symbol_kind symbol_kind =
3122 GDB_INDEX_SYMBOL_KIND_VALUE (cu_index_and_attrs);
3123 int cu_index = GDB_INDEX_CU_VALUE (cu_index_and_attrs);
3124
3125 /* Don't crash on bad data. */
3126 if (cu_index >= (dwarf2_per_objfile->n_comp_units
3127 + dwarf2_per_objfile->n_comp_units))
3128 continue;
1fd400ff 3129
156942c7
DE
3130 /* Only check the symbol's kind if it has one.
3131 Indices prior to version 7 don't record it. */
3132 if (index->version >= 7)
3133 {
3134 switch (kind)
3135 {
3136 case VARIABLES_DOMAIN:
3137 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_VARIABLE)
3138 continue;
3139 break;
3140 case FUNCTIONS_DOMAIN:
3141 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_FUNCTION)
3142 continue;
3143 break;
3144 case TYPES_DOMAIN:
3145 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_TYPE)
3146 continue;
3147 break;
3148 default:
3149 break;
3150 }
3151 }
3152
3153 per_cu = dw2_get_cu (cu_index);
7b08b9eb 3154 if (file_matcher == NULL || per_cu->v.quick->mark)
a0f42c21 3155 dw2_instantiate_symtab (per_cu);
9291a0cd
TT
3156 }
3157 }
3158}
3159
9703b513
TT
3160/* A helper for dw2_find_pc_sect_symtab which finds the most specific
3161 symtab. */
3162
3163static struct symtab *
3164recursively_find_pc_sect_symtab (struct symtab *symtab, CORE_ADDR pc)
3165{
3166 int i;
3167
3168 if (BLOCKVECTOR (symtab) != NULL
3169 && blockvector_contains_pc (BLOCKVECTOR (symtab), pc))
3170 return symtab;
3171
a3ec0bb1
DE
3172 if (symtab->includes == NULL)
3173 return NULL;
3174
9703b513
TT
3175 for (i = 0; symtab->includes[i]; ++i)
3176 {
a3ec0bb1 3177 struct symtab *s = symtab->includes[i];
9703b513
TT
3178
3179 s = recursively_find_pc_sect_symtab (s, pc);
3180 if (s != NULL)
3181 return s;
3182 }
3183
3184 return NULL;
3185}
3186
9291a0cd
TT
3187static struct symtab *
3188dw2_find_pc_sect_symtab (struct objfile *objfile,
3189 struct minimal_symbol *msymbol,
3190 CORE_ADDR pc,
3191 struct obj_section *section,
3192 int warn_if_readin)
3193{
3194 struct dwarf2_per_cu_data *data;
9703b513 3195 struct symtab *result;
9291a0cd
TT
3196
3197 dw2_setup (objfile);
3198
3199 if (!objfile->psymtabs_addrmap)
3200 return NULL;
3201
3202 data = addrmap_find (objfile->psymtabs_addrmap, pc);
3203 if (!data)
3204 return NULL;
3205
3206 if (warn_if_readin && data->v.quick->symtab)
abebb8b0 3207 warning (_("(Internal error: pc %s in read in CU, but not in symtab.)"),
9291a0cd
TT
3208 paddress (get_objfile_arch (objfile), pc));
3209
9703b513
TT
3210 result = recursively_find_pc_sect_symtab (dw2_instantiate_symtab (data), pc);
3211 gdb_assert (result != NULL);
3212 return result;
9291a0cd
TT
3213}
3214
9291a0cd 3215static void
44b13c5a 3216dw2_map_symbol_filenames (struct objfile *objfile, symbol_filename_ftype *fun,
74e2f255 3217 void *data, int need_fullname)
9291a0cd
TT
3218{
3219 int i;
24c79950
TT
3220 struct cleanup *cleanup;
3221 htab_t visited = htab_create_alloc (10, htab_hash_pointer, htab_eq_pointer,
3222 NULL, xcalloc, xfree);
9291a0cd 3223
24c79950 3224 cleanup = make_cleanup_htab_delete (visited);
9291a0cd 3225 dw2_setup (objfile);
ae2de4f8 3226
24c79950
TT
3227 /* We can ignore file names coming from already-expanded CUs. */
3228 for (i = 0; i < (dwarf2_per_objfile->n_comp_units
3229 + dwarf2_per_objfile->n_type_units); ++i)
3230 {
3231 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
3232
3233 if (per_cu->v.quick->symtab)
3234 {
3235 void **slot = htab_find_slot (visited, per_cu->v.quick->file_names,
3236 INSERT);
3237
3238 *slot = per_cu->v.quick->file_names;
3239 }
3240 }
3241
1fd400ff 3242 for (i = 0; i < (dwarf2_per_objfile->n_comp_units
d467dd73 3243 + dwarf2_per_objfile->n_type_units); ++i)
9291a0cd
TT
3244 {
3245 int j;
e254ef6a 3246 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
7b9f3c50 3247 struct quick_file_names *file_data;
24c79950 3248 void **slot;
9291a0cd 3249
3d7bb9d9 3250 /* We only need to look at symtabs not already expanded. */
e254ef6a 3251 if (per_cu->v.quick->symtab)
9291a0cd
TT
3252 continue;
3253
7b9f3c50
DE
3254 file_data = dw2_get_file_names (objfile, per_cu);
3255 if (file_data == NULL)
9291a0cd
TT
3256 continue;
3257
24c79950
TT
3258 slot = htab_find_slot (visited, file_data, INSERT);
3259 if (*slot)
3260 {
3261 /* Already visited. */
3262 continue;
3263 }
3264 *slot = file_data;
3265
7b9f3c50 3266 for (j = 0; j < file_data->num_file_names; ++j)
9291a0cd 3267 {
74e2f255
DE
3268 const char *this_real_name;
3269
3270 if (need_fullname)
3271 this_real_name = dw2_get_real_path (objfile, file_data, j);
3272 else
3273 this_real_name = NULL;
7b9f3c50 3274 (*fun) (file_data->file_names[j], this_real_name, data);
9291a0cd
TT
3275 }
3276 }
24c79950
TT
3277
3278 do_cleanups (cleanup);
9291a0cd
TT
3279}
3280
3281static int
3282dw2_has_symbols (struct objfile *objfile)
3283{
3284 return 1;
3285}
3286
3287const struct quick_symbol_functions dwarf2_gdb_index_functions =
3288{
3289 dw2_has_symbols,
3290 dw2_find_last_source_symtab,
3291 dw2_forget_cached_source_info,
f8eba3c6 3292 dw2_map_symtabs_matching_filename,
9291a0cd 3293 dw2_lookup_symbol,
774b6a14 3294 dw2_pre_expand_symtabs_matching,
9291a0cd
TT
3295 dw2_print_stats,
3296 dw2_dump,
3297 dw2_relocate,
3298 dw2_expand_symtabs_for_function,
3299 dw2_expand_all_symtabs,
3300 dw2_expand_symtabs_with_filename,
3301 dw2_find_symbol_file,
40658b94 3302 dw2_map_matching_symbols,
9291a0cd
TT
3303 dw2_expand_symtabs_matching,
3304 dw2_find_pc_sect_symtab,
9291a0cd
TT
3305 dw2_map_symbol_filenames
3306};
3307
3308/* Initialize for reading DWARF for this objfile. Return 0 if this
3309 file will use psymtabs, or 1 if using the GNU index. */
3310
3311int
3312dwarf2_initialize_objfile (struct objfile *objfile)
3313{
3314 /* If we're about to read full symbols, don't bother with the
3315 indices. In this case we also don't care if some other debug
3316 format is making psymtabs, because they are all about to be
3317 expanded anyway. */
3318 if ((objfile->flags & OBJF_READNOW))
3319 {
3320 int i;
3321
3322 dwarf2_per_objfile->using_index = 1;
3323 create_all_comp_units (objfile);
0e50663e 3324 create_all_type_units (objfile);
7b9f3c50
DE
3325 dwarf2_per_objfile->quick_file_names_table =
3326 create_quick_file_names_table (dwarf2_per_objfile->n_comp_units);
9291a0cd 3327
1fd400ff 3328 for (i = 0; i < (dwarf2_per_objfile->n_comp_units
d467dd73 3329 + dwarf2_per_objfile->n_type_units); ++i)
9291a0cd 3330 {
e254ef6a 3331 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
9291a0cd 3332
e254ef6a
DE
3333 per_cu->v.quick = OBSTACK_ZALLOC (&objfile->objfile_obstack,
3334 struct dwarf2_per_cu_quick_data);
9291a0cd
TT
3335 }
3336
3337 /* Return 1 so that gdb sees the "quick" functions. However,
3338 these functions will be no-ops because we will have expanded
3339 all symtabs. */
3340 return 1;
3341 }
3342
3343 if (dwarf2_read_index (objfile))
3344 return 1;
3345
9291a0cd
TT
3346 return 0;
3347}
3348
3349\f
3350
dce234bc
PP
3351/* Build a partial symbol table. */
3352
3353void
f29dff0a 3354dwarf2_build_psymtabs (struct objfile *objfile)
dce234bc 3355{
f29dff0a 3356 if (objfile->global_psymbols.size == 0 && objfile->static_psymbols.size == 0)
c906108c
SS
3357 {
3358 init_psymbol_list (objfile, 1024);
3359 }
3360
d146bf1e 3361 dwarf2_build_psymtabs_hard (objfile);
c906108c 3362}
c906108c 3363
1ce1cefd
DE
3364/* Return the total length of the CU described by HEADER. */
3365
3366static unsigned int
3367get_cu_length (const struct comp_unit_head *header)
3368{
3369 return header->initial_length_size + header->length;
3370}
3371
45452591
DE
3372/* Return TRUE if OFFSET is within CU_HEADER. */
3373
3374static inline int
b64f50a1 3375offset_in_cu_p (const struct comp_unit_head *cu_header, sect_offset offset)
45452591 3376{
b64f50a1 3377 sect_offset bottom = { cu_header->offset.sect_off };
1ce1cefd 3378 sect_offset top = { cu_header->offset.sect_off + get_cu_length (cu_header) };
9a619af0 3379
b64f50a1 3380 return (offset.sect_off >= bottom.sect_off && offset.sect_off < top.sect_off);
45452591
DE
3381}
3382
3b80fe9b
DE
3383/* Find the base address of the compilation unit for range lists and
3384 location lists. It will normally be specified by DW_AT_low_pc.
3385 In DWARF-3 draft 4, the base address could be overridden by
3386 DW_AT_entry_pc. It's been removed, but GCC still uses this for
3387 compilation units with discontinuous ranges. */
3388
3389static void
3390dwarf2_find_base_address (struct die_info *die, struct dwarf2_cu *cu)
3391{
3392 struct attribute *attr;
3393
3394 cu->base_known = 0;
3395 cu->base_address = 0;
3396
3397 attr = dwarf2_attr (die, DW_AT_entry_pc, cu);
3398 if (attr)
3399 {
3400 cu->base_address = DW_ADDR (attr);
3401 cu->base_known = 1;
3402 }
3403 else
3404 {
3405 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
3406 if (attr)
3407 {
3408 cu->base_address = DW_ADDR (attr);
3409 cu->base_known = 1;
3410 }
3411 }
3412}
3413
93311388
DE
3414/* Read in the comp unit header information from the debug_info at info_ptr.
3415 NOTE: This leaves members offset, first_die_offset to be filled in
3416 by the caller. */
107d2387 3417
fe1b8b76 3418static gdb_byte *
107d2387 3419read_comp_unit_head (struct comp_unit_head *cu_header,
fe1b8b76 3420 gdb_byte *info_ptr, bfd *abfd)
107d2387
AC
3421{
3422 int signed_addr;
891d2f0b 3423 unsigned int bytes_read;
c764a876
DE
3424
3425 cu_header->length = read_initial_length (abfd, info_ptr, &bytes_read);
3426 cu_header->initial_length_size = bytes_read;
3427 cu_header->offset_size = (bytes_read == 4) ? 4 : 8;
613e1657 3428 info_ptr += bytes_read;
107d2387
AC
3429 cu_header->version = read_2_bytes (abfd, info_ptr);
3430 info_ptr += 2;
b64f50a1
JK
3431 cu_header->abbrev_offset.sect_off = read_offset (abfd, info_ptr, cu_header,
3432 &bytes_read);
613e1657 3433 info_ptr += bytes_read;
107d2387
AC
3434 cu_header->addr_size = read_1_byte (abfd, info_ptr);
3435 info_ptr += 1;
3436 signed_addr = bfd_get_sign_extend_vma (abfd);
3437 if (signed_addr < 0)
8e65ff28 3438 internal_error (__FILE__, __LINE__,
e2e0b3e5 3439 _("read_comp_unit_head: dwarf from non elf file"));
107d2387 3440 cu_header->signed_addr_p = signed_addr;
c764a876 3441
107d2387
AC
3442 return info_ptr;
3443}
3444
9ff913ba
DE
3445/* Subroutine of read_and_check_comp_unit_head and
3446 read_and_check_type_unit_head to simplify them.
3447 Perform various error checking on the header. */
3448
3449static void
3450error_check_comp_unit_head (struct comp_unit_head *header,
4bdcc0c1
DE
3451 struct dwarf2_section_info *section,
3452 struct dwarf2_section_info *abbrev_section)
9ff913ba
DE
3453{
3454 bfd *abfd = section->asection->owner;
3455 const char *filename = bfd_get_filename (abfd);
3456
3457 if (header->version != 2 && header->version != 3 && header->version != 4)
3458 error (_("Dwarf Error: wrong version in compilation unit header "
3459 "(is %d, should be 2, 3, or 4) [in module %s]"), header->version,
3460 filename);
3461
b64f50a1 3462 if (header->abbrev_offset.sect_off
9ff913ba
DE
3463 >= dwarf2_section_size (dwarf2_per_objfile->objfile,
3464 &dwarf2_per_objfile->abbrev))
3465 error (_("Dwarf Error: bad offset (0x%lx) in compilation unit header "
3466 "(offset 0x%lx + 6) [in module %s]"),
b64f50a1 3467 (long) header->abbrev_offset.sect_off, (long) header->offset.sect_off,
9ff913ba
DE
3468 filename);
3469
3470 /* Cast to unsigned long to use 64-bit arithmetic when possible to
3471 avoid potential 32-bit overflow. */
1ce1cefd 3472 if (((unsigned long) header->offset.sect_off + get_cu_length (header))
9ff913ba
DE
3473 > section->size)
3474 error (_("Dwarf Error: bad length (0x%lx) in compilation unit header "
3475 "(offset 0x%lx + 0) [in module %s]"),
b64f50a1 3476 (long) header->length, (long) header->offset.sect_off,
9ff913ba
DE
3477 filename);
3478}
3479
3480/* Read in a CU/TU header and perform some basic error checking.
3481 The contents of the header are stored in HEADER.
3482 The result is a pointer to the start of the first DIE. */
adabb602 3483
fe1b8b76 3484static gdb_byte *
9ff913ba
DE
3485read_and_check_comp_unit_head (struct comp_unit_head *header,
3486 struct dwarf2_section_info *section,
4bdcc0c1 3487 struct dwarf2_section_info *abbrev_section,
9ff913ba
DE
3488 gdb_byte *info_ptr,
3489 int is_debug_types_section)
72bf9492 3490{
fe1b8b76 3491 gdb_byte *beg_of_comp_unit = info_ptr;
9ff913ba 3492 bfd *abfd = section->asection->owner;
72bf9492 3493
b64f50a1 3494 header->offset.sect_off = beg_of_comp_unit - section->buffer;
adabb602 3495
72bf9492
DJ
3496 info_ptr = read_comp_unit_head (header, info_ptr, abfd);
3497
460c1c54
CC
3498 /* If we're reading a type unit, skip over the signature and
3499 type_offset fields. */
b0df02fd 3500 if (is_debug_types_section)
460c1c54
CC
3501 info_ptr += 8 /*signature*/ + header->offset_size;
3502
b64f50a1 3503 header->first_die_offset.cu_off = info_ptr - beg_of_comp_unit;
adabb602 3504
4bdcc0c1 3505 error_check_comp_unit_head (header, section, abbrev_section);
72bf9492
DJ
3506
3507 return info_ptr;
3508}
3509
348e048f
DE
3510/* Read in the types comp unit header information from .debug_types entry at
3511 types_ptr. The result is a pointer to one past the end of the header. */
3512
3513static gdb_byte *
9ff913ba
DE
3514read_and_check_type_unit_head (struct comp_unit_head *header,
3515 struct dwarf2_section_info *section,
4bdcc0c1 3516 struct dwarf2_section_info *abbrev_section,
9ff913ba 3517 gdb_byte *info_ptr,
dee91e82
DE
3518 ULONGEST *signature,
3519 cu_offset *type_offset_in_tu)
348e048f 3520{
9ff913ba
DE
3521 gdb_byte *beg_of_comp_unit = info_ptr;
3522 bfd *abfd = section->asection->owner;
348e048f 3523
b64f50a1 3524 header->offset.sect_off = beg_of_comp_unit - section->buffer;
348e048f 3525
9ff913ba 3526 info_ptr = read_comp_unit_head (header, info_ptr, abfd);
348e048f 3527
9ff913ba
DE
3528 /* If we're reading a type unit, skip over the signature and
3529 type_offset fields. */
3530 if (signature != NULL)
3531 *signature = read_8_bytes (abfd, info_ptr);
3532 info_ptr += 8;
dee91e82
DE
3533 if (type_offset_in_tu != NULL)
3534 type_offset_in_tu->cu_off = read_offset_1 (abfd, info_ptr,
3535 header->offset_size);
9ff913ba
DE
3536 info_ptr += header->offset_size;
3537
b64f50a1 3538 header->first_die_offset.cu_off = info_ptr - beg_of_comp_unit;
348e048f 3539
4bdcc0c1 3540 error_check_comp_unit_head (header, section, abbrev_section);
9ff913ba
DE
3541
3542 return info_ptr;
348e048f
DE
3543}
3544
aaa75496
JB
3545/* Allocate a new partial symtab for file named NAME and mark this new
3546 partial symtab as being an include of PST. */
3547
3548static void
3549dwarf2_create_include_psymtab (char *name, struct partial_symtab *pst,
3550 struct objfile *objfile)
3551{
3552 struct partial_symtab *subpst = allocate_psymtab (name, objfile);
3553
3554 subpst->section_offsets = pst->section_offsets;
3555 subpst->textlow = 0;
3556 subpst->texthigh = 0;
3557
3558 subpst->dependencies = (struct partial_symtab **)
3559 obstack_alloc (&objfile->objfile_obstack,
3560 sizeof (struct partial_symtab *));
3561 subpst->dependencies[0] = pst;
3562 subpst->number_of_dependencies = 1;
3563
3564 subpst->globals_offset = 0;
3565 subpst->n_global_syms = 0;
3566 subpst->statics_offset = 0;
3567 subpst->n_static_syms = 0;
3568 subpst->symtab = NULL;
3569 subpst->read_symtab = pst->read_symtab;
3570 subpst->readin = 0;
3571
3572 /* No private part is necessary for include psymtabs. This property
3573 can be used to differentiate between such include psymtabs and
10b3939b 3574 the regular ones. */
58a9656e 3575 subpst->read_symtab_private = NULL;
aaa75496
JB
3576}
3577
3578/* Read the Line Number Program data and extract the list of files
3579 included by the source file represented by PST. Build an include
d85a05f0 3580 partial symtab for each of these included files. */
aaa75496
JB
3581
3582static void
3583dwarf2_build_include_psymtabs (struct dwarf2_cu *cu,
dee91e82
DE
3584 struct die_info *die,
3585 struct partial_symtab *pst)
aaa75496 3586{
d85a05f0
DJ
3587 struct line_header *lh = NULL;
3588 struct attribute *attr;
aaa75496 3589
d85a05f0
DJ
3590 attr = dwarf2_attr (die, DW_AT_stmt_list, cu);
3591 if (attr)
3019eac3 3592 lh = dwarf_decode_line_header (DW_UNSND (attr), cu);
aaa75496
JB
3593 if (lh == NULL)
3594 return; /* No linetable, so no includes. */
3595
c6da4cef 3596 /* NOTE: pst->dirname is DW_AT_comp_dir (if present). */
f3f5162e 3597 dwarf_decode_lines (lh, pst->dirname, cu, pst, 1);
aaa75496
JB
3598
3599 free_line_header (lh);
3600}
3601
348e048f 3602static hashval_t
52dc124a 3603hash_signatured_type (const void *item)
348e048f 3604{
52dc124a 3605 const struct signatured_type *sig_type = item;
9a619af0 3606
348e048f 3607 /* This drops the top 32 bits of the signature, but is ok for a hash. */
52dc124a 3608 return sig_type->signature;
348e048f
DE
3609}
3610
3611static int
52dc124a 3612eq_signatured_type (const void *item_lhs, const void *item_rhs)
348e048f
DE
3613{
3614 const struct signatured_type *lhs = item_lhs;
3615 const struct signatured_type *rhs = item_rhs;
9a619af0 3616
348e048f
DE
3617 return lhs->signature == rhs->signature;
3618}
3619
1fd400ff
TT
3620/* Allocate a hash table for signatured types. */
3621
3622static htab_t
673bfd45 3623allocate_signatured_type_table (struct objfile *objfile)
1fd400ff
TT
3624{
3625 return htab_create_alloc_ex (41,
52dc124a
DE
3626 hash_signatured_type,
3627 eq_signatured_type,
1fd400ff
TT
3628 NULL,
3629 &objfile->objfile_obstack,
3630 hashtab_obstack_allocate,
3631 dummy_obstack_deallocate);
3632}
3633
d467dd73 3634/* A helper function to add a signatured type CU to a table. */
1fd400ff
TT
3635
3636static int
d467dd73 3637add_signatured_type_cu_to_table (void **slot, void *datum)
1fd400ff
TT
3638{
3639 struct signatured_type *sigt = *slot;
3640 struct dwarf2_per_cu_data ***datap = datum;
3641
3642 **datap = &sigt->per_cu;
3643 ++*datap;
3644
3645 return 1;
3646}
3647
3019eac3
DE
3648/* Create the hash table of all entries in the .debug_types section.
3649 DWO_FILE is a pointer to the DWO file for .debug_types.dwo, NULL otherwise.
3650 The result is a pointer to the hash table or NULL if there are
3651 no types. */
348e048f 3652
3019eac3
DE
3653static htab_t
3654create_debug_types_hash_table (struct dwo_file *dwo_file,
3655 VEC (dwarf2_section_info_def) *types)
348e048f 3656{
3019eac3 3657 struct objfile *objfile = dwarf2_per_objfile->objfile;
8b70b953 3658 htab_t types_htab = NULL;
8b70b953
TT
3659 int ix;
3660 struct dwarf2_section_info *section;
4bdcc0c1 3661 struct dwarf2_section_info *abbrev_section;
348e048f 3662
3019eac3
DE
3663 if (VEC_empty (dwarf2_section_info_def, types))
3664 return NULL;
348e048f 3665
4bdcc0c1
DE
3666 abbrev_section = (dwo_file != NULL
3667 ? &dwo_file->sections.abbrev
3668 : &dwarf2_per_objfile->abbrev);
3669
8b70b953 3670 for (ix = 0;
3019eac3 3671 VEC_iterate (dwarf2_section_info_def, types, ix, section);
8b70b953
TT
3672 ++ix)
3673 {
3019eac3 3674 bfd *abfd;
8b70b953 3675 gdb_byte *info_ptr, *end_ptr;
348e048f 3676
8b70b953
TT
3677 dwarf2_read_section (objfile, section);
3678 info_ptr = section->buffer;
348e048f 3679
8b70b953
TT
3680 if (info_ptr == NULL)
3681 continue;
348e048f 3682
3019eac3
DE
3683 /* We can't set abfd until now because the section may be empty or
3684 not present, in which case section->asection will be NULL. */
3685 abfd = section->asection->owner;
3686
8b70b953 3687 if (types_htab == NULL)
3019eac3
DE
3688 {
3689 if (dwo_file)
3690 types_htab = allocate_dwo_unit_table (objfile);
3691 else
3692 types_htab = allocate_signatured_type_table (objfile);
3693 }
348e048f 3694
8b70b953 3695 if (dwarf2_die_debug)
dee91e82
DE
3696 fprintf_unfiltered (gdb_stdlog, "Reading signatured types for %s:\n",
3697 bfd_get_filename (abfd));
3698
3699 /* We don't use init_cutu_and_read_dies_simple, or some such, here
3700 because we don't need to read any dies: the signature is in the
3701 header. */
8b70b953
TT
3702
3703 end_ptr = info_ptr + section->size;
3704 while (info_ptr < end_ptr)
3705 {
b64f50a1 3706 sect_offset offset;
3019eac3 3707 cu_offset type_offset_in_tu;
8b70b953 3708 ULONGEST signature;
52dc124a 3709 struct signatured_type *sig_type;
3019eac3 3710 struct dwo_unit *dwo_tu;
8b70b953
TT
3711 void **slot;
3712 gdb_byte *ptr = info_ptr;
9ff913ba 3713 struct comp_unit_head header;
dee91e82 3714 unsigned int length;
348e048f 3715
b64f50a1 3716 offset.sect_off = ptr - section->buffer;
348e048f 3717
8b70b953 3718 /* We need to read the type's signature in order to build the hash
9ff913ba 3719 table, but we don't need anything else just yet. */
348e048f 3720
4bdcc0c1
DE
3721 ptr = read_and_check_type_unit_head (&header, section,
3722 abbrev_section, ptr,
3019eac3 3723 &signature, &type_offset_in_tu);
6caca83c 3724
1ce1cefd 3725 length = get_cu_length (&header);
dee91e82 3726
6caca83c 3727 /* Skip dummy type units. */
dee91e82
DE
3728 if (ptr >= info_ptr + length
3729 || peek_abbrev_code (abfd, ptr) == 0)
6caca83c 3730 {
1ce1cefd 3731 info_ptr += length;
6caca83c
CC
3732 continue;
3733 }
8b70b953 3734
3019eac3
DE
3735 if (dwo_file)
3736 {
3737 sig_type = NULL;
3738 dwo_tu = OBSTACK_ZALLOC (&objfile->objfile_obstack,
3739 struct dwo_unit);
3740 dwo_tu->dwo_file = dwo_file;
3741 dwo_tu->signature = signature;
3742 dwo_tu->type_offset_in_tu = type_offset_in_tu;
3743 dwo_tu->info_or_types_section = section;
3744 dwo_tu->offset = offset;
3745 dwo_tu->length = length;
3746 }
3747 else
3748 {
3749 /* N.B.: type_offset is not usable if this type uses a DWO file.
3750 The real type_offset is in the DWO file. */
3751 dwo_tu = NULL;
3752 sig_type = OBSTACK_ZALLOC (&objfile->objfile_obstack,
3753 struct signatured_type);
3754 sig_type->signature = signature;
3755 sig_type->type_offset_in_tu = type_offset_in_tu;
3756 sig_type->per_cu.objfile = objfile;
3757 sig_type->per_cu.is_debug_types = 1;
3758 sig_type->per_cu.info_or_types_section = section;
3759 sig_type->per_cu.offset = offset;
3760 sig_type->per_cu.length = length;
3761 }
8b70b953 3762
3019eac3
DE
3763 slot = htab_find_slot (types_htab,
3764 dwo_file ? (void*) dwo_tu : (void *) sig_type,
3765 INSERT);
8b70b953
TT
3766 gdb_assert (slot != NULL);
3767 if (*slot != NULL)
3768 {
3019eac3
DE
3769 sect_offset dup_offset;
3770
3771 if (dwo_file)
3772 {
3773 const struct dwo_unit *dup_tu = *slot;
3774
3775 dup_offset = dup_tu->offset;
3776 }
3777 else
3778 {
3779 const struct signatured_type *dup_tu = *slot;
3780
3781 dup_offset = dup_tu->per_cu.offset;
3782 }
b3c8eb43 3783
8b70b953
TT
3784 complaint (&symfile_complaints,
3785 _("debug type entry at offset 0x%x is duplicate to the "
3786 "entry at offset 0x%x, signature 0x%s"),
3019eac3 3787 offset.sect_off, dup_offset.sect_off,
8b70b953 3788 phex (signature, sizeof (signature)));
8b70b953 3789 }
3019eac3 3790 *slot = dwo_file ? (void *) dwo_tu : (void *) sig_type;
348e048f 3791
8b70b953
TT
3792 if (dwarf2_die_debug)
3793 fprintf_unfiltered (gdb_stdlog, " offset 0x%x, signature 0x%s\n",
b64f50a1
JK
3794 offset.sect_off,
3795 phex (signature, sizeof (signature)));
348e048f 3796
dee91e82 3797 info_ptr += length;
8b70b953 3798 }
348e048f
DE
3799 }
3800
3019eac3
DE
3801 return types_htab;
3802}
3803
3804/* Create the hash table of all entries in the .debug_types section,
3805 and initialize all_type_units.
3806 The result is zero if there is an error (e.g. missing .debug_types section),
3807 otherwise non-zero. */
3808
3809static int
3810create_all_type_units (struct objfile *objfile)
3811{
3812 htab_t types_htab;
3813 struct dwarf2_per_cu_data **iter;
3814
3815 types_htab = create_debug_types_hash_table (NULL, dwarf2_per_objfile->types);
3816 if (types_htab == NULL)
3817 {
3818 dwarf2_per_objfile->signatured_types = NULL;
3819 return 0;
3820 }
3821
348e048f
DE
3822 dwarf2_per_objfile->signatured_types = types_htab;
3823
d467dd73
DE
3824 dwarf2_per_objfile->n_type_units = htab_elements (types_htab);
3825 dwarf2_per_objfile->all_type_units
1fd400ff 3826 = obstack_alloc (&objfile->objfile_obstack,
d467dd73 3827 dwarf2_per_objfile->n_type_units
1fd400ff 3828 * sizeof (struct dwarf2_per_cu_data *));
d467dd73
DE
3829 iter = &dwarf2_per_objfile->all_type_units[0];
3830 htab_traverse_noresize (types_htab, add_signatured_type_cu_to_table, &iter);
3831 gdb_assert (iter - &dwarf2_per_objfile->all_type_units[0]
3832 == dwarf2_per_objfile->n_type_units);
1fd400ff 3833
348e048f
DE
3834 return 1;
3835}
3836
380bca97 3837/* Lookup a signature based type for DW_FORM_ref_sig8.
e319fa28 3838 Returns NULL if signature SIG is not present in the table. */
348e048f
DE
3839
3840static struct signatured_type *
e319fa28 3841lookup_signatured_type (ULONGEST sig)
348e048f
DE
3842{
3843 struct signatured_type find_entry, *entry;
3844
3845 if (dwarf2_per_objfile->signatured_types == NULL)
3846 {
3847 complaint (&symfile_complaints,
55f1336d 3848 _("missing `.debug_types' section for DW_FORM_ref_sig8 die"));
dcc07052 3849 return NULL;
348e048f
DE
3850 }
3851
3852 find_entry.signature = sig;
3853 entry = htab_find (dwarf2_per_objfile->signatured_types, &find_entry);
3854 return entry;
3855}
42e7ad6c
DE
3856\f
3857/* Low level DIE reading support. */
348e048f 3858
d85a05f0
DJ
3859/* Initialize a die_reader_specs struct from a dwarf2_cu struct. */
3860
3861static void
3862init_cu_die_reader (struct die_reader_specs *reader,
dee91e82 3863 struct dwarf2_cu *cu,
3019eac3
DE
3864 struct dwarf2_section_info *section,
3865 struct dwo_file *dwo_file)
d85a05f0 3866{
fceca515 3867 gdb_assert (section->readin && section->buffer != NULL);
dee91e82 3868 reader->abfd = section->asection->owner;
d85a05f0 3869 reader->cu = cu;
3019eac3 3870 reader->dwo_file = dwo_file;
dee91e82
DE
3871 reader->die_section = section;
3872 reader->buffer = section->buffer;
f664829e 3873 reader->buffer_end = section->buffer + section->size;
d85a05f0
DJ
3874}
3875
fd820528 3876/* Initialize a CU (or TU) and read its DIEs.
3019eac3 3877 If the CU defers to a DWO file, read the DWO file as well.
dee91e82
DE
3878
3879 If USE_EXISTING_CU is non-zero, and THIS_CU->cu is non-NULL, then use it.
3880 Otherwise, a new CU is allocated with xmalloc.
3881
3882 If KEEP is non-zero, then if we allocated a dwarf2_cu we add it to
3883 read_in_chain. Otherwise the dwarf2_cu data is freed at the end.
3884
3885 WARNING: If THIS_CU is a "dummy CU" (used as filler by the incremental
fd820528 3886 linker) then DIE_READER_FUNC will not get called. */
aaa75496 3887
70221824 3888static void
fd820528
DE
3889init_cutu_and_read_dies (struct dwarf2_per_cu_data *this_cu,
3890 int use_existing_cu, int keep,
3891 die_reader_func_ftype *die_reader_func,
3892 void *data)
c906108c 3893{
dee91e82 3894 struct objfile *objfile = dwarf2_per_objfile->objfile;
3019eac3
DE
3895 struct dwarf2_section_info *section = this_cu->info_or_types_section;
3896 bfd *abfd = section->asection->owner;
dee91e82
DE
3897 struct dwarf2_cu *cu;
3898 gdb_byte *begin_info_ptr, *info_ptr;
3899 struct die_reader_specs reader;
d85a05f0 3900 struct die_info *comp_unit_die;
dee91e82 3901 int has_children;
d85a05f0 3902 struct attribute *attr;
dee91e82
DE
3903 struct cleanup *cleanups, *free_cu_cleanup = NULL;
3904 struct signatured_type *sig_type = NULL;
4bdcc0c1 3905 struct dwarf2_section_info *abbrev_section;
42e7ad6c
DE
3906 /* Non-zero if CU currently points to a DWO file and we need to
3907 reread it. When this happens we need to reread the skeleton die
3908 before we can reread the DWO file. */
3909 int rereading_dwo_cu = 0;
c906108c 3910
dee91e82
DE
3911 if (use_existing_cu)
3912 gdb_assert (keep);
23745b47 3913
dee91e82
DE
3914 cleanups = make_cleanup (null_cleanup, NULL);
3915
3916 /* This is cheap if the section is already read in. */
3917 dwarf2_read_section (objfile, section);
3918
3919 begin_info_ptr = info_ptr = section->buffer + this_cu->offset.sect_off;
4bdcc0c1 3920 abbrev_section = &dwarf2_per_objfile->abbrev;
dee91e82
DE
3921
3922 if (use_existing_cu && this_cu->cu != NULL)
3923 {
3924 cu = this_cu->cu;
42e7ad6c
DE
3925
3926 /* If this CU is from a DWO file we need to start over, we need to
3927 refetch the attributes from the skeleton CU.
3928 This could be optimized by retrieving those attributes from when we
3929 were here the first time: the previous comp_unit_die was stored in
3930 comp_unit_obstack. But there's no data yet that we need this
3931 optimization. */
3932 if (cu->dwo_unit != NULL)
3933 rereading_dwo_cu = 1;
dee91e82
DE
3934 }
3935 else
3936 {
3937 /* If !use_existing_cu, this_cu->cu must be NULL. */
3938 gdb_assert (this_cu->cu == NULL);
3939
3940 cu = xmalloc (sizeof (*cu));
3941 init_one_comp_unit (cu, this_cu);
3942
3943 /* If an error occurs while loading, release our storage. */
3944 free_cu_cleanup = make_cleanup (free_heap_comp_unit, cu);
42e7ad6c 3945 }
dee91e82 3946
42e7ad6c
DE
3947 if (cu->header.first_die_offset.cu_off != 0 && ! rereading_dwo_cu)
3948 {
3949 /* We already have the header, there's no need to read it in again. */
3950 info_ptr += cu->header.first_die_offset.cu_off;
3951 }
3952 else
3953 {
3019eac3 3954 if (this_cu->is_debug_types)
dee91e82
DE
3955 {
3956 ULONGEST signature;
42e7ad6c 3957 cu_offset type_offset_in_tu;
dee91e82 3958
4bdcc0c1
DE
3959 info_ptr = read_and_check_type_unit_head (&cu->header, section,
3960 abbrev_section, info_ptr,
42e7ad6c
DE
3961 &signature,
3962 &type_offset_in_tu);
dee91e82 3963
42e7ad6c
DE
3964 /* Since per_cu is the first member of struct signatured_type,
3965 we can go from a pointer to one to a pointer to the other. */
3966 sig_type = (struct signatured_type *) this_cu;
3967 gdb_assert (sig_type->signature == signature);
3968 gdb_assert (sig_type->type_offset_in_tu.cu_off
3969 == type_offset_in_tu.cu_off);
dee91e82
DE
3970 gdb_assert (this_cu->offset.sect_off == cu->header.offset.sect_off);
3971
42e7ad6c
DE
3972 /* LENGTH has not been set yet for type units if we're
3973 using .gdb_index. */
1ce1cefd 3974 this_cu->length = get_cu_length (&cu->header);
3019eac3
DE
3975
3976 /* Establish the type offset that can be used to lookup the type. */
3977 sig_type->type_offset_in_section.sect_off =
3978 this_cu->offset.sect_off + sig_type->type_offset_in_tu.cu_off;
dee91e82
DE
3979 }
3980 else
3981 {
4bdcc0c1
DE
3982 info_ptr = read_and_check_comp_unit_head (&cu->header, section,
3983 abbrev_section,
3984 info_ptr, 0);
dee91e82
DE
3985
3986 gdb_assert (this_cu->offset.sect_off == cu->header.offset.sect_off);
1ce1cefd 3987 gdb_assert (this_cu->length == get_cu_length (&cu->header));
dee91e82
DE
3988 }
3989 }
10b3939b 3990
6caca83c 3991 /* Skip dummy compilation units. */
dee91e82 3992 if (info_ptr >= begin_info_ptr + this_cu->length
6caca83c
CC
3993 || peek_abbrev_code (abfd, info_ptr) == 0)
3994 {
dee91e82 3995 do_cleanups (cleanups);
21b2bd31 3996 return;
6caca83c
CC
3997 }
3998
433df2d4
DE
3999 /* If we don't have them yet, read the abbrevs for this compilation unit.
4000 And if we need to read them now, make sure they're freed when we're
42e7ad6c
DE
4001 done. Note that it's important that if the CU had an abbrev table
4002 on entry we don't free it when we're done: Somewhere up the call stack
4003 it may be in use. */
433df2d4 4004 if (cu->abbrev_table == NULL)
dee91e82 4005 {
4bdcc0c1 4006 dwarf2_read_abbrevs (cu, abbrev_section);
dee91e82
DE
4007 make_cleanup (dwarf2_free_abbrev_table, cu);
4008 }
42e7ad6c
DE
4009 else if (rereading_dwo_cu)
4010 {
4011 dwarf2_free_abbrev_table (cu);
4012 dwarf2_read_abbrevs (cu, abbrev_section);
4013 }
af703f96 4014
dee91e82 4015 /* Read the top level CU/TU die. */
3019eac3 4016 init_cu_die_reader (&reader, cu, section, NULL);
dee91e82 4017 info_ptr = read_full_die (&reader, &comp_unit_die, info_ptr, &has_children);
93311388 4018
3019eac3
DE
4019 /* If we have a DWO stub, process it and then read in the DWO file.
4020 Note that if USE_EXISTING_OK != 0, and THIS_CU->cu already contains
4021 a DWO CU, that this test will fail. */
4022 attr = dwarf2_attr (comp_unit_die, DW_AT_GNU_dwo_name, cu);
4023 if (attr)
4024 {
4025 char *dwo_name = DW_STRING (attr);
42e7ad6c 4026 const char *comp_dir_string;
3019eac3
DE
4027 struct dwo_unit *dwo_unit;
4028 ULONGEST signature; /* Or dwo_id. */
42e7ad6c 4029 struct attribute *comp_dir, *stmt_list, *low_pc, *high_pc, *ranges;
3019eac3 4030 int i,num_extra_attrs;
4bdcc0c1 4031 struct dwarf2_section_info *dwo_abbrev_section;
3019eac3
DE
4032
4033 if (has_children)
4034 error (_("Dwarf Error: compilation unit with DW_AT_GNU_dwo_name"
4035 " has children (offset 0x%x) [in module %s]"),
4036 this_cu->offset.sect_off, bfd_get_filename (abfd));
4037
4038 /* These attributes aren't processed until later:
4039 DW_AT_stmt_list, DW_AT_low_pc, DW_AT_high_pc, DW_AT_ranges.
4040 However, the attribute is found in the stub which we won't have later.
4041 In order to not impose this complication on the rest of the code,
4042 we read them here and copy them to the DWO CU/TU die. */
3019eac3
DE
4043
4044 /* For TUs in DWO files, the DW_AT_stmt_list attribute lives in the
4045 DWO file. */
42e7ad6c 4046 stmt_list = NULL;
3019eac3
DE
4047 if (! this_cu->is_debug_types)
4048 stmt_list = dwarf2_attr (comp_unit_die, DW_AT_stmt_list, cu);
4049 low_pc = dwarf2_attr (comp_unit_die, DW_AT_low_pc, cu);
4050 high_pc = dwarf2_attr (comp_unit_die, DW_AT_high_pc, cu);
4051 ranges = dwarf2_attr (comp_unit_die, DW_AT_ranges, cu);
42e7ad6c 4052 comp_dir = dwarf2_attr (comp_unit_die, DW_AT_comp_dir, cu);
3019eac3
DE
4053
4054 /* There should be a DW_AT_addr_base attribute here (if needed).
4055 We need the value before we can process DW_FORM_GNU_addr_index. */
4056 cu->addr_base = 0;
3019eac3
DE
4057 attr = dwarf2_attr (comp_unit_die, DW_AT_GNU_addr_base, cu);
4058 if (attr)
2e3cf129
DE
4059 cu->addr_base = DW_UNSND (attr);
4060
4061 /* There should be a DW_AT_ranges_base attribute here (if needed).
4062 We need the value before we can process DW_AT_ranges. */
4063 cu->ranges_base = 0;
4064 attr = dwarf2_attr (comp_unit_die, DW_AT_GNU_ranges_base, cu);
4065 if (attr)
4066 cu->ranges_base = DW_UNSND (attr);
3019eac3
DE
4067
4068 if (this_cu->is_debug_types)
4069 {
4070 gdb_assert (sig_type != NULL);
4071 signature = sig_type->signature;
4072 }
4073 else
4074 {
4075 attr = dwarf2_attr (comp_unit_die, DW_AT_GNU_dwo_id, cu);
4076 if (! attr)
4077 error (_("Dwarf Error: missing dwo_id [in module %s]"),
4078 dwo_name);
4079 signature = DW_UNSND (attr);
4080 }
4081
4082 /* We may need the comp_dir in order to find the DWO file. */
42e7ad6c
DE
4083 comp_dir_string = NULL;
4084 if (comp_dir)
4085 comp_dir_string = DW_STRING (comp_dir);
3019eac3
DE
4086
4087 if (this_cu->is_debug_types)
42e7ad6c 4088 dwo_unit = lookup_dwo_type_unit (sig_type, dwo_name, comp_dir_string);
3019eac3 4089 else
42e7ad6c 4090 dwo_unit = lookup_dwo_comp_unit (this_cu, dwo_name, comp_dir_string,
3019eac3
DE
4091 signature);
4092
4093 if (dwo_unit == NULL)
4094 {
4095 error (_("Dwarf Error: CU at offset 0x%x references unknown DWO"
4096 " with ID %s [in module %s]"),
4097 this_cu->offset.sect_off,
4098 phex (signature, sizeof (signature)),
4099 objfile->name);
4100 }
4101
4102 /* Set up for reading the DWO CU/TU. */
4103 cu->dwo_unit = dwo_unit;
4104 section = dwo_unit->info_or_types_section;
4105 begin_info_ptr = info_ptr = section->buffer + dwo_unit->offset.sect_off;
4bdcc0c1 4106 dwo_abbrev_section = &dwo_unit->dwo_file->sections.abbrev;
3019eac3
DE
4107 init_cu_die_reader (&reader, cu, section, dwo_unit->dwo_file);
4108
4109 if (this_cu->is_debug_types)
4110 {
4111 ULONGEST signature;
4112
4bdcc0c1
DE
4113 info_ptr = read_and_check_type_unit_head (&cu->header, section,
4114 dwo_abbrev_section,
4115 info_ptr,
3019eac3
DE
4116 &signature, NULL);
4117 gdb_assert (sig_type->signature == signature);
4118 gdb_assert (dwo_unit->offset.sect_off == cu->header.offset.sect_off);
1ce1cefd 4119 gdb_assert (dwo_unit->length == get_cu_length (&cu->header));
3019eac3
DE
4120
4121 /* Establish the type offset that can be used to lookup the type.
4122 For DWO files, we don't know it until now. */
4123 sig_type->type_offset_in_section.sect_off =
4124 dwo_unit->offset.sect_off + dwo_unit->type_offset_in_tu.cu_off;
4125 }
4126 else
4127 {
4bdcc0c1
DE
4128 info_ptr = read_and_check_comp_unit_head (&cu->header, section,
4129 dwo_abbrev_section,
4130 info_ptr, 0);
3019eac3 4131 gdb_assert (dwo_unit->offset.sect_off == cu->header.offset.sect_off);
1ce1cefd 4132 gdb_assert (dwo_unit->length == get_cu_length (&cu->header));
3019eac3
DE
4133 }
4134
4135 /* Discard the original CU's abbrev table, and read the DWO's. */
4136 dwarf2_free_abbrev_table (cu);
4bdcc0c1 4137 dwarf2_read_abbrevs (cu, dwo_abbrev_section);
3019eac3
DE
4138
4139 /* Read in the die, but leave space to copy over the attributes
4140 from the stub. This has the benefit of simplifying the rest of
4141 the code - all the real work is done here. */
4142 num_extra_attrs = ((stmt_list != NULL)
4143 + (low_pc != NULL)
4144 + (high_pc != NULL)
42e7ad6c
DE
4145 + (ranges != NULL)
4146 + (comp_dir != NULL));
3019eac3
DE
4147 info_ptr = read_full_die_1 (&reader, &comp_unit_die, info_ptr,
4148 &has_children, num_extra_attrs);
4149
4150 /* Copy over the attributes from the stub to the DWO die. */
4151 i = comp_unit_die->num_attrs;
4152 if (stmt_list != NULL)
4153 comp_unit_die->attrs[i++] = *stmt_list;
4154 if (low_pc != NULL)
4155 comp_unit_die->attrs[i++] = *low_pc;
4156 if (high_pc != NULL)
4157 comp_unit_die->attrs[i++] = *high_pc;
4158 if (ranges != NULL)
4159 comp_unit_die->attrs[i++] = *ranges;
42e7ad6c
DE
4160 if (comp_dir != NULL)
4161 comp_unit_die->attrs[i++] = *comp_dir;
3019eac3
DE
4162 comp_unit_die->num_attrs += num_extra_attrs;
4163
4164 /* Skip dummy compilation units. */
4165 if (info_ptr >= begin_info_ptr + dwo_unit->length
4166 || peek_abbrev_code (abfd, info_ptr) == 0)
4167 {
4168 do_cleanups (cleanups);
4169 return;
4170 }
4171 }
4172
dee91e82
DE
4173 die_reader_func (&reader, info_ptr, comp_unit_die, has_children, data);
4174
4175 if (free_cu_cleanup != NULL)
348e048f 4176 {
dee91e82
DE
4177 if (keep)
4178 {
4179 /* We've successfully allocated this compilation unit. Let our
4180 caller clean it up when finished with it. */
4181 discard_cleanups (free_cu_cleanup);
4182
4183 /* We can only discard free_cu_cleanup and all subsequent cleanups.
4184 So we have to manually free the abbrev table. */
4185 dwarf2_free_abbrev_table (cu);
4186
4187 /* Link this CU into read_in_chain. */
4188 this_cu->cu->read_in_chain = dwarf2_per_objfile->read_in_chain;
4189 dwarf2_per_objfile->read_in_chain = this_cu;
4190 }
4191 else
4192 do_cleanups (free_cu_cleanup);
348e048f 4193 }
dee91e82
DE
4194
4195 do_cleanups (cleanups);
4196}
4197
3019eac3
DE
4198/* Read CU/TU THIS_CU in section SECTION,
4199 but do not follow DW_AT_GNU_dwo_name if present.
4200 DWO_FILE, if non-NULL, is the DWO file to read (the caller is assumed to
4201 have already done the lookup to find the DWO file).
dee91e82
DE
4202
4203 The caller is required to fill in THIS_CU->section, THIS_CU->offset, and
3019eac3 4204 THIS_CU->is_debug_types, but nothing else.
dee91e82
DE
4205
4206 We fill in THIS_CU->length.
4207
4208 WARNING: If THIS_CU is a "dummy CU" (used as filler by the incremental
4209 linker) then DIE_READER_FUNC will not get called.
4210
4211 THIS_CU->cu is always freed when done.
3019eac3
DE
4212 This is done in order to not leave THIS_CU->cu in a state where we have
4213 to care whether it refers to the "main" CU or the DWO CU. */
dee91e82
DE
4214
4215static void
4216init_cutu_and_read_dies_no_follow (struct dwarf2_per_cu_data *this_cu,
4217 struct dwarf2_section_info *abbrev_section,
3019eac3 4218 struct dwo_file *dwo_file,
dee91e82
DE
4219 die_reader_func_ftype *die_reader_func,
4220 void *data)
4221{
4222 struct objfile *objfile = dwarf2_per_objfile->objfile;
3019eac3
DE
4223 struct dwarf2_section_info *section = this_cu->info_or_types_section;
4224 bfd *abfd = section->asection->owner;
dee91e82
DE
4225 struct dwarf2_cu cu;
4226 gdb_byte *begin_info_ptr, *info_ptr;
4227 struct die_reader_specs reader;
4228 struct cleanup *cleanups;
4229 struct die_info *comp_unit_die;
4230 int has_children;
4231
4232 gdb_assert (this_cu->cu == NULL);
4233
dee91e82
DE
4234 /* This is cheap if the section is already read in. */
4235 dwarf2_read_section (objfile, section);
4236
4237 init_one_comp_unit (&cu, this_cu);
4238
4239 cleanups = make_cleanup (free_stack_comp_unit, &cu);
4240
4241 begin_info_ptr = info_ptr = section->buffer + this_cu->offset.sect_off;
4bdcc0c1
DE
4242 info_ptr = read_and_check_comp_unit_head (&cu.header, section,
4243 abbrev_section, info_ptr,
3019eac3 4244 this_cu->is_debug_types);
dee91e82 4245
1ce1cefd 4246 this_cu->length = get_cu_length (&cu.header);
dee91e82
DE
4247
4248 /* Skip dummy compilation units. */
4249 if (info_ptr >= begin_info_ptr + this_cu->length
4250 || peek_abbrev_code (abfd, info_ptr) == 0)
c906108c 4251 {
dee91e82 4252 do_cleanups (cleanups);
21b2bd31 4253 return;
93311388 4254 }
72bf9492 4255
dee91e82
DE
4256 dwarf2_read_abbrevs (&cu, abbrev_section);
4257 make_cleanup (dwarf2_free_abbrev_table, &cu);
4258
3019eac3 4259 init_cu_die_reader (&reader, &cu, section, dwo_file);
dee91e82
DE
4260 info_ptr = read_full_die (&reader, &comp_unit_die, info_ptr, &has_children);
4261
4262 die_reader_func (&reader, info_ptr, comp_unit_die, has_children, data);
4263
4264 do_cleanups (cleanups);
4265}
4266
3019eac3
DE
4267/* Read a CU/TU, except that this does not look for DW_AT_GNU_dwo_name and
4268 does not lookup the specified DWO file.
4269 This cannot be used to read DWO files.
dee91e82
DE
4270
4271 THIS_CU->cu is always freed when done.
3019eac3
DE
4272 This is done in order to not leave THIS_CU->cu in a state where we have
4273 to care whether it refers to the "main" CU or the DWO CU.
4274 We can revisit this if the data shows there's a performance issue. */
dee91e82
DE
4275
4276static void
4277init_cutu_and_read_dies_simple (struct dwarf2_per_cu_data *this_cu,
4278 die_reader_func_ftype *die_reader_func,
4279 void *data)
4280{
4281 init_cutu_and_read_dies_no_follow (this_cu,
4282 &dwarf2_per_objfile->abbrev,
3019eac3 4283 NULL,
dee91e82
DE
4284 die_reader_func, data);
4285}
4286
4287/* die_reader_func for process_psymtab_comp_unit. */
4288
4289static void
4290process_psymtab_comp_unit_reader (const struct die_reader_specs *reader,
4291 gdb_byte *info_ptr,
4292 struct die_info *comp_unit_die,
4293 int has_children,
4294 void *data)
4295{
4296 struct dwarf2_cu *cu = reader->cu;
4297 struct objfile *objfile = cu->objfile;
4298 struct dwarf2_per_cu_data *per_cu = cu->per_cu;
dee91e82
DE
4299 struct attribute *attr;
4300 CORE_ADDR baseaddr;
4301 CORE_ADDR best_lowpc = 0, best_highpc = 0;
4302 struct partial_symtab *pst;
4303 int has_pc_info;
4304 const char *filename;
95554aad 4305 int *want_partial_unit_ptr = data;
dee91e82 4306
95554aad
TT
4307 if (comp_unit_die->tag == DW_TAG_partial_unit
4308 && (want_partial_unit_ptr == NULL
4309 || !*want_partial_unit_ptr))
dee91e82
DE
4310 return;
4311
95554aad 4312 prepare_one_comp_unit (cu, comp_unit_die, language_minimal);
dee91e82
DE
4313
4314 cu->list_in_scope = &file_symbols;
c906108c 4315
93311388 4316 /* Allocate a new partial symbol table structure. */
dee91e82 4317 attr = dwarf2_attr (comp_unit_die, DW_AT_name, cu);
3e2a0cee
TT
4318 if (attr == NULL || !DW_STRING (attr))
4319 filename = "";
4320 else
4321 filename = DW_STRING (attr);
93311388 4322 pst = start_psymtab_common (objfile, objfile->section_offsets,
3e2a0cee 4323 filename,
93311388
DE
4324 /* TEXTLOW and TEXTHIGH are set below. */
4325 0,
4326 objfile->global_psymbols.next,
4327 objfile->static_psymbols.next);
9750bca9 4328 pst->psymtabs_addrmap_supported = 1;
72bf9492 4329
dee91e82 4330 attr = dwarf2_attr (comp_unit_die, DW_AT_comp_dir, cu);
d85a05f0
DJ
4331 if (attr != NULL)
4332 pst->dirname = DW_STRING (attr);
72bf9492 4333
dee91e82 4334 pst->read_symtab_private = per_cu;
72bf9492 4335
93311388 4336 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
e7c27a73 4337
0963b4bd 4338 /* Store the function that reads in the rest of the symbol table. */
93311388 4339 pst->read_symtab = dwarf2_psymtab_to_symtab;
57349743 4340
dee91e82 4341 per_cu->v.psymtab = pst;
c906108c 4342
dee91e82 4343 dwarf2_find_base_address (comp_unit_die, cu);
d85a05f0 4344
93311388
DE
4345 /* Possibly set the default values of LOWPC and HIGHPC from
4346 `DW_AT_ranges'. */
d85a05f0 4347 has_pc_info = dwarf2_get_pc_bounds (comp_unit_die, &best_lowpc,
dee91e82 4348 &best_highpc, cu, pst);
d85a05f0 4349 if (has_pc_info == 1 && best_lowpc < best_highpc)
93311388
DE
4350 /* Store the contiguous range if it is not empty; it can be empty for
4351 CUs with no code. */
4352 addrmap_set_empty (objfile->psymtabs_addrmap,
d85a05f0
DJ
4353 best_lowpc + baseaddr,
4354 best_highpc + baseaddr - 1, pst);
93311388
DE
4355
4356 /* Check if comp unit has_children.
4357 If so, read the rest of the partial symbols from this comp unit.
0963b4bd 4358 If not, there's no more debug_info for this comp unit. */
d85a05f0 4359 if (has_children)
93311388
DE
4360 {
4361 struct partial_die_info *first_die;
4362 CORE_ADDR lowpc, highpc;
31ffec48 4363
93311388
DE
4364 lowpc = ((CORE_ADDR) -1);
4365 highpc = ((CORE_ADDR) 0);
c906108c 4366
dee91e82 4367 first_die = load_partial_dies (reader, info_ptr, 1);
c906108c 4368
93311388 4369 scan_partial_symbols (first_die, &lowpc, &highpc,
dee91e82 4370 ! has_pc_info, cu);
57c22c6c 4371
93311388
DE
4372 /* If we didn't find a lowpc, set it to highpc to avoid
4373 complaints from `maint check'. */
4374 if (lowpc == ((CORE_ADDR) -1))
4375 lowpc = highpc;
10b3939b 4376
93311388
DE
4377 /* If the compilation unit didn't have an explicit address range,
4378 then use the information extracted from its child dies. */
d85a05f0 4379 if (! has_pc_info)
93311388 4380 {
d85a05f0
DJ
4381 best_lowpc = lowpc;
4382 best_highpc = highpc;
93311388
DE
4383 }
4384 }
d85a05f0
DJ
4385 pst->textlow = best_lowpc + baseaddr;
4386 pst->texthigh = best_highpc + baseaddr;
c906108c 4387
93311388
DE
4388 pst->n_global_syms = objfile->global_psymbols.next -
4389 (objfile->global_psymbols.list + pst->globals_offset);
4390 pst->n_static_syms = objfile->static_psymbols.next -
4391 (objfile->static_psymbols.list + pst->statics_offset);
4392 sort_pst_symbols (pst);
c906108c 4393
95554aad
TT
4394 if (!VEC_empty (dwarf2_per_cu_ptr, cu->per_cu->imported_symtabs))
4395 {
4396 int i;
4397 int len = VEC_length (dwarf2_per_cu_ptr, cu->per_cu->imported_symtabs);
4398 struct dwarf2_per_cu_data *iter;
4399
4400 /* Fill in 'dependencies' here; we fill in 'users' in a
4401 post-pass. */
4402 pst->number_of_dependencies = len;
4403 pst->dependencies = obstack_alloc (&objfile->objfile_obstack,
4404 len * sizeof (struct symtab *));
4405 for (i = 0;
4406 VEC_iterate (dwarf2_per_cu_ptr, cu->per_cu->imported_symtabs,
4407 i, iter);
4408 ++i)
4409 pst->dependencies[i] = iter->v.psymtab;
4410
4411 VEC_free (dwarf2_per_cu_ptr, cu->per_cu->imported_symtabs);
4412 }
4413
3019eac3 4414 if (per_cu->is_debug_types)
348e048f
DE
4415 {
4416 /* It's not clear we want to do anything with stmt lists here.
4417 Waiting to see what gcc ultimately does. */
4418 }
d85a05f0 4419 else
93311388
DE
4420 {
4421 /* Get the list of files included in the current compilation unit,
4422 and build a psymtab for each of them. */
dee91e82 4423 dwarf2_build_include_psymtabs (cu, comp_unit_die, pst);
93311388 4424 }
dee91e82 4425}
ae038cb0 4426
dee91e82
DE
4427/* Subroutine of dwarf2_build_psymtabs_hard to simplify it.
4428 Process compilation unit THIS_CU for a psymtab. */
4429
4430static void
95554aad
TT
4431process_psymtab_comp_unit (struct dwarf2_per_cu_data *this_cu,
4432 int want_partial_unit)
dee91e82
DE
4433{
4434 /* If this compilation unit was already read in, free the
4435 cached copy in order to read it in again. This is
4436 necessary because we skipped some symbols when we first
4437 read in the compilation unit (see load_partial_dies).
4438 This problem could be avoided, but the benefit is unclear. */
4439 if (this_cu->cu != NULL)
4440 free_one_cached_comp_unit (this_cu);
4441
3019eac3 4442 gdb_assert (! this_cu->is_debug_types);
fd820528 4443 init_cutu_and_read_dies (this_cu, 0, 0, process_psymtab_comp_unit_reader,
95554aad 4444 &want_partial_unit);
dee91e82
DE
4445
4446 /* Age out any secondary CUs. */
4447 age_cached_comp_units ();
93311388 4448}
ff013f42 4449
348e048f
DE
4450/* Traversal function for htab_traverse_noresize.
4451 Process one .debug_types comp-unit. */
4452
4453static int
dee91e82 4454process_psymtab_type_unit (void **slot, void *info)
348e048f 4455{
dee91e82
DE
4456 struct signatured_type *sig_type = (struct signatured_type *) *slot;
4457 struct dwarf2_per_cu_data *per_cu = &sig_type->per_cu;
348e048f 4458
fd820528 4459 gdb_assert (per_cu->is_debug_types);
a0f42c21 4460 gdb_assert (info == NULL);
348e048f 4461
dee91e82
DE
4462 /* If this compilation unit was already read in, free the
4463 cached copy in order to read it in again. This is
4464 necessary because we skipped some symbols when we first
4465 read in the compilation unit (see load_partial_dies).
4466 This problem could be avoided, but the benefit is unclear. */
4467 if (per_cu->cu != NULL)
4468 free_one_cached_comp_unit (per_cu);
4469
fd820528
DE
4470 init_cutu_and_read_dies (per_cu, 0, 0, process_psymtab_comp_unit_reader,
4471 NULL);
dee91e82
DE
4472
4473 /* Age out any secondary CUs. */
4474 age_cached_comp_units ();
348e048f
DE
4475
4476 return 1;
4477}
4478
4479/* Subroutine of dwarf2_build_psymtabs_hard to simplify it.
4480 Build partial symbol tables for the .debug_types comp-units. */
4481
4482static void
4483build_type_psymtabs (struct objfile *objfile)
4484{
0e50663e 4485 if (! create_all_type_units (objfile))
348e048f
DE
4486 return;
4487
4488 htab_traverse_noresize (dwarf2_per_objfile->signatured_types,
dee91e82 4489 process_psymtab_type_unit, NULL);
348e048f
DE
4490}
4491
60606b2c
TT
4492/* A cleanup function that clears objfile's psymtabs_addrmap field. */
4493
4494static void
4495psymtabs_addrmap_cleanup (void *o)
4496{
4497 struct objfile *objfile = o;
ec61707d 4498
60606b2c
TT
4499 objfile->psymtabs_addrmap = NULL;
4500}
4501
95554aad
TT
4502/* Compute the 'user' field for each psymtab in OBJFILE. */
4503
4504static void
4505set_partial_user (struct objfile *objfile)
4506{
4507 int i;
4508
4509 for (i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
4510 {
4511 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
4512 struct partial_symtab *pst = per_cu->v.psymtab;
4513 int j;
4514
4515 for (j = 0; j < pst->number_of_dependencies; ++j)
4516 {
4517 /* Set the 'user' field only if it is not already set. */
4518 if (pst->dependencies[j]->user == NULL)
4519 pst->dependencies[j]->user = pst;
4520 }
4521 }
4522}
4523
93311388
DE
4524/* Build the partial symbol table by doing a quick pass through the
4525 .debug_info and .debug_abbrev sections. */
72bf9492 4526
93311388 4527static void
c67a9c90 4528dwarf2_build_psymtabs_hard (struct objfile *objfile)
93311388 4529{
60606b2c
TT
4530 struct cleanup *back_to, *addrmap_cleanup;
4531 struct obstack temp_obstack;
21b2bd31 4532 int i;
93311388 4533
45cfd468
DE
4534 if (dwarf2_read_debug)
4535 {
4536 fprintf_unfiltered (gdb_stdlog, "Building psymtabs of objfile %s ...\n",
4537 objfile->name);
4538 }
4539
98bfdba5
PA
4540 dwarf2_per_objfile->reading_partial_symbols = 1;
4541
be391dca 4542 dwarf2_read_section (objfile, &dwarf2_per_objfile->info);
91c24f0a 4543
93311388
DE
4544 /* Any cached compilation units will be linked by the per-objfile
4545 read_in_chain. Make sure to free them when we're done. */
4546 back_to = make_cleanup (free_cached_comp_units, NULL);
72bf9492 4547
348e048f
DE
4548 build_type_psymtabs (objfile);
4549
93311388 4550 create_all_comp_units (objfile);
c906108c 4551
60606b2c
TT
4552 /* Create a temporary address map on a temporary obstack. We later
4553 copy this to the final obstack. */
4554 obstack_init (&temp_obstack);
4555 make_cleanup_obstack_free (&temp_obstack);
4556 objfile->psymtabs_addrmap = addrmap_create_mutable (&temp_obstack);
4557 addrmap_cleanup = make_cleanup (psymtabs_addrmap_cleanup, objfile);
72bf9492 4558
21b2bd31 4559 for (i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
93311388 4560 {
21b2bd31 4561 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
aaa75496 4562
95554aad 4563 process_psymtab_comp_unit (per_cu, 0);
c906108c 4564 }
ff013f42 4565
95554aad
TT
4566 set_partial_user (objfile);
4567
ff013f42
JK
4568 objfile->psymtabs_addrmap = addrmap_create_fixed (objfile->psymtabs_addrmap,
4569 &objfile->objfile_obstack);
60606b2c 4570 discard_cleanups (addrmap_cleanup);
ff013f42 4571
ae038cb0 4572 do_cleanups (back_to);
45cfd468
DE
4573
4574 if (dwarf2_read_debug)
4575 fprintf_unfiltered (gdb_stdlog, "Done building psymtabs of %s\n",
4576 objfile->name);
ae038cb0
DJ
4577}
4578
3019eac3 4579/* die_reader_func for load_partial_comp_unit. */
ae038cb0
DJ
4580
4581static void
dee91e82
DE
4582load_partial_comp_unit_reader (const struct die_reader_specs *reader,
4583 gdb_byte *info_ptr,
4584 struct die_info *comp_unit_die,
4585 int has_children,
4586 void *data)
ae038cb0 4587{
dee91e82 4588 struct dwarf2_cu *cu = reader->cu;
ae038cb0 4589
95554aad 4590 prepare_one_comp_unit (cu, comp_unit_die, language_minimal);
ae038cb0 4591
ae038cb0
DJ
4592 /* Check if comp unit has_children.
4593 If so, read the rest of the partial symbols from this comp unit.
0963b4bd 4594 If not, there's no more debug_info for this comp unit. */
d85a05f0 4595 if (has_children)
dee91e82
DE
4596 load_partial_dies (reader, info_ptr, 0);
4597}
98bfdba5 4598
dee91e82
DE
4599/* Load the partial DIEs for a secondary CU into memory.
4600 This is also used when rereading a primary CU with load_all_dies. */
c5b7e1cb 4601
dee91e82
DE
4602static void
4603load_partial_comp_unit (struct dwarf2_per_cu_data *this_cu)
4604{
fd820528 4605 init_cutu_and_read_dies (this_cu, 1, 1, load_partial_comp_unit_reader, NULL);
ae038cb0
DJ
4606}
4607
9cdd5dbd
DE
4608/* Create a list of all compilation units in OBJFILE.
4609 This is only done for -readnow and building partial symtabs. */
ae038cb0
DJ
4610
4611static void
4612create_all_comp_units (struct objfile *objfile)
4613{
4614 int n_allocated;
4615 int n_comp_units;
4616 struct dwarf2_per_cu_data **all_comp_units;
be391dca
TT
4617 gdb_byte *info_ptr;
4618
4619 dwarf2_read_section (objfile, &dwarf2_per_objfile->info);
4620 info_ptr = dwarf2_per_objfile->info.buffer;
ae038cb0
DJ
4621
4622 n_comp_units = 0;
4623 n_allocated = 10;
4624 all_comp_units = xmalloc (n_allocated
4625 * sizeof (struct dwarf2_per_cu_data *));
6e70227d 4626
3e43a32a
MS
4627 while (info_ptr < dwarf2_per_objfile->info.buffer
4628 + dwarf2_per_objfile->info.size)
ae038cb0 4629 {
c764a876 4630 unsigned int length, initial_length_size;
ae038cb0 4631 struct dwarf2_per_cu_data *this_cu;
b64f50a1 4632 sect_offset offset;
ae038cb0 4633
b64f50a1 4634 offset.sect_off = info_ptr - dwarf2_per_objfile->info.buffer;
ae038cb0
DJ
4635
4636 /* Read just enough information to find out where the next
4637 compilation unit is. */
c764a876
DE
4638 length = read_initial_length (objfile->obfd, info_ptr,
4639 &initial_length_size);
ae038cb0
DJ
4640
4641 /* Save the compilation unit for later lookup. */
4642 this_cu = obstack_alloc (&objfile->objfile_obstack,
4643 sizeof (struct dwarf2_per_cu_data));
4644 memset (this_cu, 0, sizeof (*this_cu));
4645 this_cu->offset = offset;
c764a876 4646 this_cu->length = length + initial_length_size;
9291a0cd 4647 this_cu->objfile = objfile;
3019eac3 4648 this_cu->info_or_types_section = &dwarf2_per_objfile->info;
ae038cb0
DJ
4649
4650 if (n_comp_units == n_allocated)
4651 {
4652 n_allocated *= 2;
4653 all_comp_units = xrealloc (all_comp_units,
4654 n_allocated
4655 * sizeof (struct dwarf2_per_cu_data *));
4656 }
4657 all_comp_units[n_comp_units++] = this_cu;
4658
4659 info_ptr = info_ptr + this_cu->length;
4660 }
4661
4662 dwarf2_per_objfile->all_comp_units
4663 = obstack_alloc (&objfile->objfile_obstack,
4664 n_comp_units * sizeof (struct dwarf2_per_cu_data *));
4665 memcpy (dwarf2_per_objfile->all_comp_units, all_comp_units,
4666 n_comp_units * sizeof (struct dwarf2_per_cu_data *));
4667 xfree (all_comp_units);
4668 dwarf2_per_objfile->n_comp_units = n_comp_units;
c906108c
SS
4669}
4670
5734ee8b
DJ
4671/* Process all loaded DIEs for compilation unit CU, starting at
4672 FIRST_DIE. The caller should pass NEED_PC == 1 if the compilation
4673 unit DIE did not have PC info (DW_AT_low_pc and DW_AT_high_pc, or
4674 DW_AT_ranges). If NEED_PC is set, then this function will set
4675 *LOWPC and *HIGHPC to the lowest and highest PC values found in CU
4676 and record the covered ranges in the addrmap. */
c906108c 4677
72bf9492
DJ
4678static void
4679scan_partial_symbols (struct partial_die_info *first_die, CORE_ADDR *lowpc,
5734ee8b 4680 CORE_ADDR *highpc, int need_pc, struct dwarf2_cu *cu)
c906108c 4681{
72bf9492 4682 struct partial_die_info *pdi;
c906108c 4683
91c24f0a
DC
4684 /* Now, march along the PDI's, descending into ones which have
4685 interesting children but skipping the children of the other ones,
4686 until we reach the end of the compilation unit. */
c906108c 4687
72bf9492 4688 pdi = first_die;
91c24f0a 4689
72bf9492
DJ
4690 while (pdi != NULL)
4691 {
4692 fixup_partial_die (pdi, cu);
c906108c 4693
f55ee35c 4694 /* Anonymous namespaces or modules have no name but have interesting
91c24f0a
DC
4695 children, so we need to look at them. Ditto for anonymous
4696 enums. */
933c6fe4 4697
72bf9492 4698 if (pdi->name != NULL || pdi->tag == DW_TAG_namespace
95554aad
TT
4699 || pdi->tag == DW_TAG_module || pdi->tag == DW_TAG_enumeration_type
4700 || pdi->tag == DW_TAG_imported_unit)
c906108c 4701 {
72bf9492 4702 switch (pdi->tag)
c906108c
SS
4703 {
4704 case DW_TAG_subprogram:
5734ee8b 4705 add_partial_subprogram (pdi, lowpc, highpc, need_pc, cu);
c906108c 4706 break;
72929c62 4707 case DW_TAG_constant:
c906108c
SS
4708 case DW_TAG_variable:
4709 case DW_TAG_typedef:
91c24f0a 4710 case DW_TAG_union_type:
72bf9492 4711 if (!pdi->is_declaration)
63d06c5c 4712 {
72bf9492 4713 add_partial_symbol (pdi, cu);
63d06c5c
DC
4714 }
4715 break;
c906108c 4716 case DW_TAG_class_type:
680b30c7 4717 case DW_TAG_interface_type:
c906108c 4718 case DW_TAG_structure_type:
72bf9492 4719 if (!pdi->is_declaration)
c906108c 4720 {
72bf9492 4721 add_partial_symbol (pdi, cu);
c906108c
SS
4722 }
4723 break;
91c24f0a 4724 case DW_TAG_enumeration_type:
72bf9492
DJ
4725 if (!pdi->is_declaration)
4726 add_partial_enumeration (pdi, cu);
c906108c
SS
4727 break;
4728 case DW_TAG_base_type:
a02abb62 4729 case DW_TAG_subrange_type:
c906108c 4730 /* File scope base type definitions are added to the partial
c5aa993b 4731 symbol table. */
72bf9492 4732 add_partial_symbol (pdi, cu);
c906108c 4733 break;
d9fa45fe 4734 case DW_TAG_namespace:
5734ee8b 4735 add_partial_namespace (pdi, lowpc, highpc, need_pc, cu);
91c24f0a 4736 break;
5d7cb8df
JK
4737 case DW_TAG_module:
4738 add_partial_module (pdi, lowpc, highpc, need_pc, cu);
4739 break;
95554aad
TT
4740 case DW_TAG_imported_unit:
4741 {
4742 struct dwarf2_per_cu_data *per_cu;
4743
4744 per_cu = dwarf2_find_containing_comp_unit (pdi->d.offset,
4745 cu->objfile);
4746
4747 /* Go read the partial unit, if needed. */
4748 if (per_cu->v.psymtab == NULL)
4749 process_psymtab_comp_unit (per_cu, 1);
4750
4751 VEC_safe_push (dwarf2_per_cu_ptr, cu->per_cu->imported_symtabs,
4752 per_cu);
4753 }
4754 break;
c906108c
SS
4755 default:
4756 break;
4757 }
4758 }
4759
72bf9492
DJ
4760 /* If the die has a sibling, skip to the sibling. */
4761
4762 pdi = pdi->die_sibling;
4763 }
4764}
4765
4766/* Functions used to compute the fully scoped name of a partial DIE.
91c24f0a 4767
72bf9492 4768 Normally, this is simple. For C++, the parent DIE's fully scoped
987504bb
JJ
4769 name is concatenated with "::" and the partial DIE's name. For
4770 Java, the same thing occurs except that "." is used instead of "::".
72bf9492
DJ
4771 Enumerators are an exception; they use the scope of their parent
4772 enumeration type, i.e. the name of the enumeration type is not
4773 prepended to the enumerator.
91c24f0a 4774
72bf9492
DJ
4775 There are two complexities. One is DW_AT_specification; in this
4776 case "parent" means the parent of the target of the specification,
4777 instead of the direct parent of the DIE. The other is compilers
4778 which do not emit DW_TAG_namespace; in this case we try to guess
4779 the fully qualified name of structure types from their members'
4780 linkage names. This must be done using the DIE's children rather
4781 than the children of any DW_AT_specification target. We only need
4782 to do this for structures at the top level, i.e. if the target of
4783 any DW_AT_specification (if any; otherwise the DIE itself) does not
4784 have a parent. */
4785
4786/* Compute the scope prefix associated with PDI's parent, in
4787 compilation unit CU. The result will be allocated on CU's
4788 comp_unit_obstack, or a copy of the already allocated PDI->NAME
4789 field. NULL is returned if no prefix is necessary. */
4790static char *
4791partial_die_parent_scope (struct partial_die_info *pdi,
4792 struct dwarf2_cu *cu)
4793{
4794 char *grandparent_scope;
4795 struct partial_die_info *parent, *real_pdi;
91c24f0a 4796
72bf9492
DJ
4797 /* We need to look at our parent DIE; if we have a DW_AT_specification,
4798 then this means the parent of the specification DIE. */
4799
4800 real_pdi = pdi;
72bf9492 4801 while (real_pdi->has_specification)
10b3939b 4802 real_pdi = find_partial_die (real_pdi->spec_offset, cu);
72bf9492
DJ
4803
4804 parent = real_pdi->die_parent;
4805 if (parent == NULL)
4806 return NULL;
4807
4808 if (parent->scope_set)
4809 return parent->scope;
4810
4811 fixup_partial_die (parent, cu);
4812
10b3939b 4813 grandparent_scope = partial_die_parent_scope (parent, cu);
72bf9492 4814
acebe513
UW
4815 /* GCC 4.0 and 4.1 had a bug (PR c++/28460) where they generated bogus
4816 DW_TAG_namespace DIEs with a name of "::" for the global namespace.
4817 Work around this problem here. */
4818 if (cu->language == language_cplus
6e70227d 4819 && parent->tag == DW_TAG_namespace
acebe513
UW
4820 && strcmp (parent->name, "::") == 0
4821 && grandparent_scope == NULL)
4822 {
4823 parent->scope = NULL;
4824 parent->scope_set = 1;
4825 return NULL;
4826 }
4827
9c6c53f7
SA
4828 if (pdi->tag == DW_TAG_enumerator)
4829 /* Enumerators should not get the name of the enumeration as a prefix. */
4830 parent->scope = grandparent_scope;
4831 else if (parent->tag == DW_TAG_namespace
f55ee35c 4832 || parent->tag == DW_TAG_module
72bf9492
DJ
4833 || parent->tag == DW_TAG_structure_type
4834 || parent->tag == DW_TAG_class_type
680b30c7 4835 || parent->tag == DW_TAG_interface_type
ceeb3d5a
TT
4836 || parent->tag == DW_TAG_union_type
4837 || parent->tag == DW_TAG_enumeration_type)
72bf9492
DJ
4838 {
4839 if (grandparent_scope == NULL)
4840 parent->scope = parent->name;
4841 else
3e43a32a
MS
4842 parent->scope = typename_concat (&cu->comp_unit_obstack,
4843 grandparent_scope,
f55ee35c 4844 parent->name, 0, cu);
72bf9492 4845 }
72bf9492
DJ
4846 else
4847 {
4848 /* FIXME drow/2004-04-01: What should we be doing with
4849 function-local names? For partial symbols, we should probably be
4850 ignoring them. */
4851 complaint (&symfile_complaints,
e2e0b3e5 4852 _("unhandled containing DIE tag %d for DIE at %d"),
b64f50a1 4853 parent->tag, pdi->offset.sect_off);
72bf9492 4854 parent->scope = grandparent_scope;
c906108c
SS
4855 }
4856
72bf9492
DJ
4857 parent->scope_set = 1;
4858 return parent->scope;
4859}
4860
4861/* Return the fully scoped name associated with PDI, from compilation unit
4862 CU. The result will be allocated with malloc. */
4568ecf9 4863
72bf9492
DJ
4864static char *
4865partial_die_full_name (struct partial_die_info *pdi,
4866 struct dwarf2_cu *cu)
4867{
4868 char *parent_scope;
4869
98bfdba5
PA
4870 /* If this is a template instantiation, we can not work out the
4871 template arguments from partial DIEs. So, unfortunately, we have
4872 to go through the full DIEs. At least any work we do building
4873 types here will be reused if full symbols are loaded later. */
4874 if (pdi->has_template_arguments)
4875 {
4876 fixup_partial_die (pdi, cu);
4877
4878 if (pdi->name != NULL && strchr (pdi->name, '<') == NULL)
4879 {
4880 struct die_info *die;
4881 struct attribute attr;
4882 struct dwarf2_cu *ref_cu = cu;
4883
b64f50a1 4884 /* DW_FORM_ref_addr is using section offset. */
98bfdba5
PA
4885 attr.name = 0;
4886 attr.form = DW_FORM_ref_addr;
4568ecf9 4887 attr.u.unsnd = pdi->offset.sect_off;
98bfdba5
PA
4888 die = follow_die_ref (NULL, &attr, &ref_cu);
4889
4890 return xstrdup (dwarf2_full_name (NULL, die, ref_cu));
4891 }
4892 }
4893
72bf9492
DJ
4894 parent_scope = partial_die_parent_scope (pdi, cu);
4895 if (parent_scope == NULL)
4896 return NULL;
4897 else
f55ee35c 4898 return typename_concat (NULL, parent_scope, pdi->name, 0, cu);
c906108c
SS
4899}
4900
4901static void
72bf9492 4902add_partial_symbol (struct partial_die_info *pdi, struct dwarf2_cu *cu)
c906108c 4903{
e7c27a73 4904 struct objfile *objfile = cu->objfile;
c906108c 4905 CORE_ADDR addr = 0;
decbce07 4906 char *actual_name = NULL;
e142c38c 4907 CORE_ADDR baseaddr;
72bf9492 4908 int built_actual_name = 0;
e142c38c
DJ
4909
4910 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
c906108c 4911
94af9270
KS
4912 actual_name = partial_die_full_name (pdi, cu);
4913 if (actual_name)
4914 built_actual_name = 1;
63d06c5c 4915
72bf9492
DJ
4916 if (actual_name == NULL)
4917 actual_name = pdi->name;
4918
c906108c
SS
4919 switch (pdi->tag)
4920 {
4921 case DW_TAG_subprogram:
2cfa0c8d 4922 if (pdi->is_external || cu->language == language_ada)
c906108c 4923 {
2cfa0c8d
JB
4924 /* brobecker/2007-12-26: Normally, only "external" DIEs are part
4925 of the global scope. But in Ada, we want to be able to access
4926 nested procedures globally. So all Ada subprograms are stored
4927 in the global scope. */
f47fb265 4928 /* prim_record_minimal_symbol (actual_name, pdi->lowpc + baseaddr,
c5aa993b 4929 mst_text, objfile); */
f47fb265
MS
4930 add_psymbol_to_list (actual_name, strlen (actual_name),
4931 built_actual_name,
4932 VAR_DOMAIN, LOC_BLOCK,
4933 &objfile->global_psymbols,
4934 0, pdi->lowpc + baseaddr,
4935 cu->language, objfile);
c906108c
SS
4936 }
4937 else
4938 {
f47fb265 4939 /* prim_record_minimal_symbol (actual_name, pdi->lowpc + baseaddr,
c5aa993b 4940 mst_file_text, objfile); */
f47fb265
MS
4941 add_psymbol_to_list (actual_name, strlen (actual_name),
4942 built_actual_name,
4943 VAR_DOMAIN, LOC_BLOCK,
4944 &objfile->static_psymbols,
4945 0, pdi->lowpc + baseaddr,
4946 cu->language, objfile);
c906108c
SS
4947 }
4948 break;
72929c62
JB
4949 case DW_TAG_constant:
4950 {
4951 struct psymbol_allocation_list *list;
4952
4953 if (pdi->is_external)
4954 list = &objfile->global_psymbols;
4955 else
4956 list = &objfile->static_psymbols;
f47fb265
MS
4957 add_psymbol_to_list (actual_name, strlen (actual_name),
4958 built_actual_name, VAR_DOMAIN, LOC_STATIC,
4959 list, 0, 0, cu->language, objfile);
72929c62
JB
4960 }
4961 break;
c906108c 4962 case DW_TAG_variable:
95554aad
TT
4963 if (pdi->d.locdesc)
4964 addr = decode_locdesc (pdi->d.locdesc, cu);
caac4577 4965
95554aad 4966 if (pdi->d.locdesc
caac4577
JG
4967 && addr == 0
4968 && !dwarf2_per_objfile->has_section_at_zero)
4969 {
4970 /* A global or static variable may also have been stripped
4971 out by the linker if unused, in which case its address
4972 will be nullified; do not add such variables into partial
4973 symbol table then. */
4974 }
4975 else if (pdi->is_external)
c906108c
SS
4976 {
4977 /* Global Variable.
4978 Don't enter into the minimal symbol tables as there is
4979 a minimal symbol table entry from the ELF symbols already.
4980 Enter into partial symbol table if it has a location
4981 descriptor or a type.
4982 If the location descriptor is missing, new_symbol will create
4983 a LOC_UNRESOLVED symbol, the address of the variable will then
4984 be determined from the minimal symbol table whenever the variable
4985 is referenced.
4986 The address for the partial symbol table entry is not
4987 used by GDB, but it comes in handy for debugging partial symbol
4988 table building. */
4989
95554aad 4990 if (pdi->d.locdesc || pdi->has_type)
f47fb265
MS
4991 add_psymbol_to_list (actual_name, strlen (actual_name),
4992 built_actual_name,
4993 VAR_DOMAIN, LOC_STATIC,
4994 &objfile->global_psymbols,
4995 0, addr + baseaddr,
4996 cu->language, objfile);
c906108c
SS
4997 }
4998 else
4999 {
0963b4bd 5000 /* Static Variable. Skip symbols without location descriptors. */
95554aad 5001 if (pdi->d.locdesc == NULL)
decbce07
MS
5002 {
5003 if (built_actual_name)
5004 xfree (actual_name);
5005 return;
5006 }
f47fb265 5007 /* prim_record_minimal_symbol (actual_name, addr + baseaddr,
c5aa993b 5008 mst_file_data, objfile); */
f47fb265
MS
5009 add_psymbol_to_list (actual_name, strlen (actual_name),
5010 built_actual_name,
5011 VAR_DOMAIN, LOC_STATIC,
5012 &objfile->static_psymbols,
5013 0, addr + baseaddr,
5014 cu->language, objfile);
c906108c
SS
5015 }
5016 break;
5017 case DW_TAG_typedef:
5018 case DW_TAG_base_type:
a02abb62 5019 case DW_TAG_subrange_type:
38d518c9 5020 add_psymbol_to_list (actual_name, strlen (actual_name),
04a679b8 5021 built_actual_name,
176620f1 5022 VAR_DOMAIN, LOC_TYPEDEF,
c906108c 5023 &objfile->static_psymbols,
e142c38c 5024 0, (CORE_ADDR) 0, cu->language, objfile);
c906108c 5025 break;
72bf9492
DJ
5026 case DW_TAG_namespace:
5027 add_psymbol_to_list (actual_name, strlen (actual_name),
04a679b8 5028 built_actual_name,
72bf9492
DJ
5029 VAR_DOMAIN, LOC_TYPEDEF,
5030 &objfile->global_psymbols,
5031 0, (CORE_ADDR) 0, cu->language, objfile);
5032 break;
c906108c 5033 case DW_TAG_class_type:
680b30c7 5034 case DW_TAG_interface_type:
c906108c
SS
5035 case DW_TAG_structure_type:
5036 case DW_TAG_union_type:
5037 case DW_TAG_enumeration_type:
fa4028e9
JB
5038 /* Skip external references. The DWARF standard says in the section
5039 about "Structure, Union, and Class Type Entries": "An incomplete
5040 structure, union or class type is represented by a structure,
5041 union or class entry that does not have a byte size attribute
5042 and that has a DW_AT_declaration attribute." */
5043 if (!pdi->has_byte_size && pdi->is_declaration)
decbce07
MS
5044 {
5045 if (built_actual_name)
5046 xfree (actual_name);
5047 return;
5048 }
fa4028e9 5049
63d06c5c
DC
5050 /* NOTE: carlton/2003-10-07: See comment in new_symbol about
5051 static vs. global. */
38d518c9 5052 add_psymbol_to_list (actual_name, strlen (actual_name),
04a679b8 5053 built_actual_name,
176620f1 5054 STRUCT_DOMAIN, LOC_TYPEDEF,
987504bb
JJ
5055 (cu->language == language_cplus
5056 || cu->language == language_java)
63d06c5c
DC
5057 ? &objfile->global_psymbols
5058 : &objfile->static_psymbols,
e142c38c 5059 0, (CORE_ADDR) 0, cu->language, objfile);
c906108c 5060
c906108c
SS
5061 break;
5062 case DW_TAG_enumerator:
38d518c9 5063 add_psymbol_to_list (actual_name, strlen (actual_name),
04a679b8 5064 built_actual_name,
176620f1 5065 VAR_DOMAIN, LOC_CONST,
987504bb
JJ
5066 (cu->language == language_cplus
5067 || cu->language == language_java)
f6fe98ef
DJ
5068 ? &objfile->global_psymbols
5069 : &objfile->static_psymbols,
e142c38c 5070 0, (CORE_ADDR) 0, cu->language, objfile);
c906108c
SS
5071 break;
5072 default:
5073 break;
5074 }
5c4e30ca 5075
72bf9492
DJ
5076 if (built_actual_name)
5077 xfree (actual_name);
c906108c
SS
5078}
5079
5c4e30ca
DC
5080/* Read a partial die corresponding to a namespace; also, add a symbol
5081 corresponding to that namespace to the symbol table. NAMESPACE is
5082 the name of the enclosing namespace. */
91c24f0a 5083
72bf9492
DJ
5084static void
5085add_partial_namespace (struct partial_die_info *pdi,
91c24f0a 5086 CORE_ADDR *lowpc, CORE_ADDR *highpc,
5734ee8b 5087 int need_pc, struct dwarf2_cu *cu)
91c24f0a 5088{
72bf9492 5089 /* Add a symbol for the namespace. */
e7c27a73 5090
72bf9492 5091 add_partial_symbol (pdi, cu);
5c4e30ca
DC
5092
5093 /* Now scan partial symbols in that namespace. */
5094
91c24f0a 5095 if (pdi->has_children)
5734ee8b 5096 scan_partial_symbols (pdi->die_child, lowpc, highpc, need_pc, cu);
91c24f0a
DC
5097}
5098
5d7cb8df
JK
5099/* Read a partial die corresponding to a Fortran module. */
5100
5101static void
5102add_partial_module (struct partial_die_info *pdi, CORE_ADDR *lowpc,
5103 CORE_ADDR *highpc, int need_pc, struct dwarf2_cu *cu)
5104{
f55ee35c 5105 /* Now scan partial symbols in that module. */
5d7cb8df
JK
5106
5107 if (pdi->has_children)
5108 scan_partial_symbols (pdi->die_child, lowpc, highpc, need_pc, cu);
5109}
5110
bc30ff58
JB
5111/* Read a partial die corresponding to a subprogram and create a partial
5112 symbol for that subprogram. When the CU language allows it, this
5113 routine also defines a partial symbol for each nested subprogram
5114 that this subprogram contains.
6e70227d 5115
bc30ff58
JB
5116 DIE my also be a lexical block, in which case we simply search
5117 recursively for suprograms defined inside that lexical block.
5118 Again, this is only performed when the CU language allows this
5119 type of definitions. */
5120
5121static void
5122add_partial_subprogram (struct partial_die_info *pdi,
5123 CORE_ADDR *lowpc, CORE_ADDR *highpc,
5734ee8b 5124 int need_pc, struct dwarf2_cu *cu)
bc30ff58
JB
5125{
5126 if (pdi->tag == DW_TAG_subprogram)
5127 {
5128 if (pdi->has_pc_info)
5129 {
5130 if (pdi->lowpc < *lowpc)
5131 *lowpc = pdi->lowpc;
5132 if (pdi->highpc > *highpc)
5133 *highpc = pdi->highpc;
5734ee8b
DJ
5134 if (need_pc)
5135 {
5136 CORE_ADDR baseaddr;
5137 struct objfile *objfile = cu->objfile;
5138
5139 baseaddr = ANOFFSET (objfile->section_offsets,
5140 SECT_OFF_TEXT (objfile));
5141 addrmap_set_empty (objfile->psymtabs_addrmap,
01637564
DE
5142 pdi->lowpc + baseaddr,
5143 pdi->highpc - 1 + baseaddr,
9291a0cd 5144 cu->per_cu->v.psymtab);
5734ee8b 5145 }
481860b3
GB
5146 }
5147
5148 if (pdi->has_pc_info || (!pdi->is_external && pdi->may_be_inlined))
5149 {
bc30ff58 5150 if (!pdi->is_declaration)
e8d05480
JB
5151 /* Ignore subprogram DIEs that do not have a name, they are
5152 illegal. Do not emit a complaint at this point, we will
5153 do so when we convert this psymtab into a symtab. */
5154 if (pdi->name)
5155 add_partial_symbol (pdi, cu);
bc30ff58
JB
5156 }
5157 }
6e70227d 5158
bc30ff58
JB
5159 if (! pdi->has_children)
5160 return;
5161
5162 if (cu->language == language_ada)
5163 {
5164 pdi = pdi->die_child;
5165 while (pdi != NULL)
5166 {
5167 fixup_partial_die (pdi, cu);
5168 if (pdi->tag == DW_TAG_subprogram
5169 || pdi->tag == DW_TAG_lexical_block)
5734ee8b 5170 add_partial_subprogram (pdi, lowpc, highpc, need_pc, cu);
bc30ff58
JB
5171 pdi = pdi->die_sibling;
5172 }
5173 }
5174}
5175
91c24f0a
DC
5176/* Read a partial die corresponding to an enumeration type. */
5177
72bf9492
DJ
5178static void
5179add_partial_enumeration (struct partial_die_info *enum_pdi,
5180 struct dwarf2_cu *cu)
91c24f0a 5181{
72bf9492 5182 struct partial_die_info *pdi;
91c24f0a
DC
5183
5184 if (enum_pdi->name != NULL)
72bf9492
DJ
5185 add_partial_symbol (enum_pdi, cu);
5186
5187 pdi = enum_pdi->die_child;
5188 while (pdi)
91c24f0a 5189 {
72bf9492 5190 if (pdi->tag != DW_TAG_enumerator || pdi->name == NULL)
e2e0b3e5 5191 complaint (&symfile_complaints, _("malformed enumerator DIE ignored"));
91c24f0a 5192 else
72bf9492
DJ
5193 add_partial_symbol (pdi, cu);
5194 pdi = pdi->die_sibling;
91c24f0a 5195 }
91c24f0a
DC
5196}
5197
6caca83c
CC
5198/* Return the initial uleb128 in the die at INFO_PTR. */
5199
5200static unsigned int
5201peek_abbrev_code (bfd *abfd, gdb_byte *info_ptr)
5202{
5203 unsigned int bytes_read;
5204
5205 return read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
5206}
5207
4bb7a0a7
DJ
5208/* Read the initial uleb128 in the die at INFO_PTR in compilation unit CU.
5209 Return the corresponding abbrev, or NULL if the number is zero (indicating
5210 an empty DIE). In either case *BYTES_READ will be set to the length of
5211 the initial number. */
5212
5213static struct abbrev_info *
fe1b8b76 5214peek_die_abbrev (gdb_byte *info_ptr, unsigned int *bytes_read,
891d2f0b 5215 struct dwarf2_cu *cu)
4bb7a0a7
DJ
5216{
5217 bfd *abfd = cu->objfile->obfd;
5218 unsigned int abbrev_number;
5219 struct abbrev_info *abbrev;
5220
5221 abbrev_number = read_unsigned_leb128 (abfd, info_ptr, bytes_read);
5222
5223 if (abbrev_number == 0)
5224 return NULL;
5225
433df2d4 5226 abbrev = abbrev_table_lookup_abbrev (cu->abbrev_table, abbrev_number);
4bb7a0a7
DJ
5227 if (!abbrev)
5228 {
3e43a32a
MS
5229 error (_("Dwarf Error: Could not find abbrev number %d [in module %s]"),
5230 abbrev_number, bfd_get_filename (abfd));
4bb7a0a7
DJ
5231 }
5232
5233 return abbrev;
5234}
5235
93311388
DE
5236/* Scan the debug information for CU starting at INFO_PTR in buffer BUFFER.
5237 Returns a pointer to the end of a series of DIEs, terminated by an empty
4bb7a0a7
DJ
5238 DIE. Any children of the skipped DIEs will also be skipped. */
5239
fe1b8b76 5240static gdb_byte *
dee91e82 5241skip_children (const struct die_reader_specs *reader, gdb_byte *info_ptr)
4bb7a0a7 5242{
dee91e82 5243 struct dwarf2_cu *cu = reader->cu;
4bb7a0a7
DJ
5244 struct abbrev_info *abbrev;
5245 unsigned int bytes_read;
5246
5247 while (1)
5248 {
5249 abbrev = peek_die_abbrev (info_ptr, &bytes_read, cu);
5250 if (abbrev == NULL)
5251 return info_ptr + bytes_read;
5252 else
dee91e82 5253 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
4bb7a0a7
DJ
5254 }
5255}
5256
93311388
DE
5257/* Scan the debug information for CU starting at INFO_PTR in buffer BUFFER.
5258 INFO_PTR should point just after the initial uleb128 of a DIE, and the
4bb7a0a7
DJ
5259 abbrev corresponding to that skipped uleb128 should be passed in
5260 ABBREV. Returns a pointer to this DIE's sibling, skipping any
5261 children. */
5262
fe1b8b76 5263static gdb_byte *
dee91e82
DE
5264skip_one_die (const struct die_reader_specs *reader, gdb_byte *info_ptr,
5265 struct abbrev_info *abbrev)
4bb7a0a7
DJ
5266{
5267 unsigned int bytes_read;
5268 struct attribute attr;
dee91e82
DE
5269 bfd *abfd = reader->abfd;
5270 struct dwarf2_cu *cu = reader->cu;
5271 gdb_byte *buffer = reader->buffer;
f664829e
DE
5272 const gdb_byte *buffer_end = reader->buffer_end;
5273 gdb_byte *start_info_ptr = info_ptr;
4bb7a0a7
DJ
5274 unsigned int form, i;
5275
5276 for (i = 0; i < abbrev->num_attrs; i++)
5277 {
5278 /* The only abbrev we care about is DW_AT_sibling. */
5279 if (abbrev->attrs[i].name == DW_AT_sibling)
5280 {
dee91e82 5281 read_attribute (reader, &attr, &abbrev->attrs[i], info_ptr);
4bb7a0a7 5282 if (attr.form == DW_FORM_ref_addr)
3e43a32a
MS
5283 complaint (&symfile_complaints,
5284 _("ignoring absolute DW_AT_sibling"));
4bb7a0a7 5285 else
b64f50a1 5286 return buffer + dwarf2_get_ref_die_offset (&attr).sect_off;
4bb7a0a7
DJ
5287 }
5288
5289 /* If it isn't DW_AT_sibling, skip this attribute. */
5290 form = abbrev->attrs[i].form;
5291 skip_attribute:
5292 switch (form)
5293 {
4bb7a0a7 5294 case DW_FORM_ref_addr:
ae411497
TT
5295 /* In DWARF 2, DW_FORM_ref_addr is address sized; in DWARF 3
5296 and later it is offset sized. */
5297 if (cu->header.version == 2)
5298 info_ptr += cu->header.addr_size;
5299 else
5300 info_ptr += cu->header.offset_size;
5301 break;
5302 case DW_FORM_addr:
4bb7a0a7
DJ
5303 info_ptr += cu->header.addr_size;
5304 break;
5305 case DW_FORM_data1:
5306 case DW_FORM_ref1:
5307 case DW_FORM_flag:
5308 info_ptr += 1;
5309 break;
2dc7f7b3
TT
5310 case DW_FORM_flag_present:
5311 break;
4bb7a0a7
DJ
5312 case DW_FORM_data2:
5313 case DW_FORM_ref2:
5314 info_ptr += 2;
5315 break;
5316 case DW_FORM_data4:
5317 case DW_FORM_ref4:
5318 info_ptr += 4;
5319 break;
5320 case DW_FORM_data8:
5321 case DW_FORM_ref8:
55f1336d 5322 case DW_FORM_ref_sig8:
4bb7a0a7
DJ
5323 info_ptr += 8;
5324 break;
5325 case DW_FORM_string:
9b1c24c8 5326 read_direct_string (abfd, info_ptr, &bytes_read);
4bb7a0a7
DJ
5327 info_ptr += bytes_read;
5328 break;
2dc7f7b3 5329 case DW_FORM_sec_offset:
4bb7a0a7
DJ
5330 case DW_FORM_strp:
5331 info_ptr += cu->header.offset_size;
5332 break;
2dc7f7b3 5333 case DW_FORM_exprloc:
4bb7a0a7
DJ
5334 case DW_FORM_block:
5335 info_ptr += read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
5336 info_ptr += bytes_read;
5337 break;
5338 case DW_FORM_block1:
5339 info_ptr += 1 + read_1_byte (abfd, info_ptr);
5340 break;
5341 case DW_FORM_block2:
5342 info_ptr += 2 + read_2_bytes (abfd, info_ptr);
5343 break;
5344 case DW_FORM_block4:
5345 info_ptr += 4 + read_4_bytes (abfd, info_ptr);
5346 break;
5347 case DW_FORM_sdata:
5348 case DW_FORM_udata:
5349 case DW_FORM_ref_udata:
3019eac3
DE
5350 case DW_FORM_GNU_addr_index:
5351 case DW_FORM_GNU_str_index:
f664829e 5352 info_ptr = (gdb_byte *) safe_skip_leb128 (info_ptr, buffer_end);
4bb7a0a7
DJ
5353 break;
5354 case DW_FORM_indirect:
5355 form = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
5356 info_ptr += bytes_read;
5357 /* We need to continue parsing from here, so just go back to
5358 the top. */
5359 goto skip_attribute;
5360
5361 default:
3e43a32a
MS
5362 error (_("Dwarf Error: Cannot handle %s "
5363 "in DWARF reader [in module %s]"),
4bb7a0a7
DJ
5364 dwarf_form_name (form),
5365 bfd_get_filename (abfd));
5366 }
5367 }
5368
5369 if (abbrev->has_children)
dee91e82 5370 return skip_children (reader, info_ptr);
4bb7a0a7
DJ
5371 else
5372 return info_ptr;
5373}
5374
93311388 5375/* Locate ORIG_PDI's sibling.
dee91e82 5376 INFO_PTR should point to the start of the next DIE after ORIG_PDI. */
91c24f0a 5377
fe1b8b76 5378static gdb_byte *
dee91e82
DE
5379locate_pdi_sibling (const struct die_reader_specs *reader,
5380 struct partial_die_info *orig_pdi,
5381 gdb_byte *info_ptr)
91c24f0a
DC
5382{
5383 /* Do we know the sibling already? */
72bf9492 5384
91c24f0a
DC
5385 if (orig_pdi->sibling)
5386 return orig_pdi->sibling;
5387
5388 /* Are there any children to deal with? */
5389
5390 if (!orig_pdi->has_children)
5391 return info_ptr;
5392
4bb7a0a7 5393 /* Skip the children the long way. */
91c24f0a 5394
dee91e82 5395 return skip_children (reader, info_ptr);
91c24f0a
DC
5396}
5397
c906108c
SS
5398/* Expand this partial symbol table into a full symbol table. */
5399
5400static void
fba45db2 5401dwarf2_psymtab_to_symtab (struct partial_symtab *pst)
c906108c 5402{
c906108c
SS
5403 if (pst != NULL)
5404 {
5405 if (pst->readin)
5406 {
3e43a32a
MS
5407 warning (_("bug: psymtab for %s is already read in."),
5408 pst->filename);
c906108c
SS
5409 }
5410 else
5411 {
5412 if (info_verbose)
5413 {
3e43a32a
MS
5414 printf_filtered (_("Reading in symbols for %s..."),
5415 pst->filename);
c906108c
SS
5416 gdb_flush (gdb_stdout);
5417 }
5418
10b3939b
DJ
5419 /* Restore our global data. */
5420 dwarf2_per_objfile = objfile_data (pst->objfile,
5421 dwarf2_objfile_data_key);
5422
b2ab525c
KB
5423 /* If this psymtab is constructed from a debug-only objfile, the
5424 has_section_at_zero flag will not necessarily be correct. We
5425 can get the correct value for this flag by looking at the data
5426 associated with the (presumably stripped) associated objfile. */
5427 if (pst->objfile->separate_debug_objfile_backlink)
5428 {
5429 struct dwarf2_per_objfile *dpo_backlink
5430 = objfile_data (pst->objfile->separate_debug_objfile_backlink,
5431 dwarf2_objfile_data_key);
9a619af0 5432
b2ab525c
KB
5433 dwarf2_per_objfile->has_section_at_zero
5434 = dpo_backlink->has_section_at_zero;
5435 }
5436
98bfdba5
PA
5437 dwarf2_per_objfile->reading_partial_symbols = 0;
5438
c906108c
SS
5439 psymtab_to_symtab_1 (pst);
5440
5441 /* Finish up the debug error message. */
5442 if (info_verbose)
a3f17187 5443 printf_filtered (_("done.\n"));
c906108c
SS
5444 }
5445 }
95554aad
TT
5446
5447 process_cu_includes ();
c906108c 5448}
9cdd5dbd
DE
5449\f
5450/* Reading in full CUs. */
c906108c 5451
10b3939b
DJ
5452/* Add PER_CU to the queue. */
5453
5454static void
95554aad
TT
5455queue_comp_unit (struct dwarf2_per_cu_data *per_cu,
5456 enum language pretend_language)
10b3939b
DJ
5457{
5458 struct dwarf2_queue_item *item;
5459
5460 per_cu->queued = 1;
5461 item = xmalloc (sizeof (*item));
5462 item->per_cu = per_cu;
95554aad 5463 item->pretend_language = pretend_language;
10b3939b
DJ
5464 item->next = NULL;
5465
5466 if (dwarf2_queue == NULL)
5467 dwarf2_queue = item;
5468 else
5469 dwarf2_queue_tail->next = item;
5470
5471 dwarf2_queue_tail = item;
5472}
5473
5474/* Process the queue. */
5475
5476static void
a0f42c21 5477process_queue (void)
10b3939b
DJ
5478{
5479 struct dwarf2_queue_item *item, *next_item;
5480
45cfd468
DE
5481 if (dwarf2_read_debug)
5482 {
5483 fprintf_unfiltered (gdb_stdlog,
5484 "Expanding one or more symtabs of objfile %s ...\n",
5485 dwarf2_per_objfile->objfile->name);
5486 }
5487
03dd20cc
DJ
5488 /* The queue starts out with one item, but following a DIE reference
5489 may load a new CU, adding it to the end of the queue. */
10b3939b
DJ
5490 for (item = dwarf2_queue; item != NULL; dwarf2_queue = item = next_item)
5491 {
9291a0cd
TT
5492 if (dwarf2_per_objfile->using_index
5493 ? !item->per_cu->v.quick->symtab
5494 : (item->per_cu->v.psymtab && !item->per_cu->v.psymtab->readin))
95554aad 5495 process_full_comp_unit (item->per_cu, item->pretend_language);
10b3939b
DJ
5496
5497 item->per_cu->queued = 0;
5498 next_item = item->next;
5499 xfree (item);
5500 }
5501
5502 dwarf2_queue_tail = NULL;
45cfd468
DE
5503
5504 if (dwarf2_read_debug)
5505 {
5506 fprintf_unfiltered (gdb_stdlog, "Done expanding symtabs of %s.\n",
5507 dwarf2_per_objfile->objfile->name);
5508 }
10b3939b
DJ
5509}
5510
5511/* Free all allocated queue entries. This function only releases anything if
5512 an error was thrown; if the queue was processed then it would have been
5513 freed as we went along. */
5514
5515static void
5516dwarf2_release_queue (void *dummy)
5517{
5518 struct dwarf2_queue_item *item, *last;
5519
5520 item = dwarf2_queue;
5521 while (item)
5522 {
5523 /* Anything still marked queued is likely to be in an
5524 inconsistent state, so discard it. */
5525 if (item->per_cu->queued)
5526 {
5527 if (item->per_cu->cu != NULL)
dee91e82 5528 free_one_cached_comp_unit (item->per_cu);
10b3939b
DJ
5529 item->per_cu->queued = 0;
5530 }
5531
5532 last = item;
5533 item = item->next;
5534 xfree (last);
5535 }
5536
5537 dwarf2_queue = dwarf2_queue_tail = NULL;
5538}
5539
5540/* Read in full symbols for PST, and anything it depends on. */
5541
c906108c 5542static void
fba45db2 5543psymtab_to_symtab_1 (struct partial_symtab *pst)
c906108c 5544{
10b3939b 5545 struct dwarf2_per_cu_data *per_cu;
aaa75496
JB
5546 int i;
5547
95554aad
TT
5548 if (pst->readin)
5549 return;
5550
aaa75496 5551 for (i = 0; i < pst->number_of_dependencies; i++)
95554aad
TT
5552 if (!pst->dependencies[i]->readin
5553 && pst->dependencies[i]->user == NULL)
aaa75496
JB
5554 {
5555 /* Inform about additional files that need to be read in. */
5556 if (info_verbose)
5557 {
a3f17187 5558 /* FIXME: i18n: Need to make this a single string. */
aaa75496
JB
5559 fputs_filtered (" ", gdb_stdout);
5560 wrap_here ("");
5561 fputs_filtered ("and ", gdb_stdout);
5562 wrap_here ("");
5563 printf_filtered ("%s...", pst->dependencies[i]->filename);
0963b4bd 5564 wrap_here (""); /* Flush output. */
aaa75496
JB
5565 gdb_flush (gdb_stdout);
5566 }
5567 psymtab_to_symtab_1 (pst->dependencies[i]);
5568 }
5569
e38df1d0 5570 per_cu = pst->read_symtab_private;
10b3939b
DJ
5571
5572 if (per_cu == NULL)
aaa75496
JB
5573 {
5574 /* It's an include file, no symbols to read for it.
5575 Everything is in the parent symtab. */
5576 pst->readin = 1;
5577 return;
5578 }
c906108c 5579
a0f42c21 5580 dw2_do_instantiate_symtab (per_cu);
10b3939b
DJ
5581}
5582
dee91e82
DE
5583/* Trivial hash function for die_info: the hash value of a DIE
5584 is its offset in .debug_info for this objfile. */
10b3939b 5585
dee91e82
DE
5586static hashval_t
5587die_hash (const void *item)
10b3939b 5588{
dee91e82 5589 const struct die_info *die = item;
6502dd73 5590
dee91e82
DE
5591 return die->offset.sect_off;
5592}
63d06c5c 5593
dee91e82
DE
5594/* Trivial comparison function for die_info structures: two DIEs
5595 are equal if they have the same offset. */
98bfdba5 5596
dee91e82
DE
5597static int
5598die_eq (const void *item_lhs, const void *item_rhs)
5599{
5600 const struct die_info *die_lhs = item_lhs;
5601 const struct die_info *die_rhs = item_rhs;
c906108c 5602
dee91e82
DE
5603 return die_lhs->offset.sect_off == die_rhs->offset.sect_off;
5604}
c906108c 5605
dee91e82
DE
5606/* die_reader_func for load_full_comp_unit.
5607 This is identical to read_signatured_type_reader,
5608 but is kept separate for now. */
c906108c 5609
dee91e82
DE
5610static void
5611load_full_comp_unit_reader (const struct die_reader_specs *reader,
5612 gdb_byte *info_ptr,
5613 struct die_info *comp_unit_die,
5614 int has_children,
5615 void *data)
5616{
5617 struct dwarf2_cu *cu = reader->cu;
95554aad 5618 enum language *language_ptr = data;
6caca83c 5619
dee91e82
DE
5620 gdb_assert (cu->die_hash == NULL);
5621 cu->die_hash =
5622 htab_create_alloc_ex (cu->header.length / 12,
5623 die_hash,
5624 die_eq,
5625 NULL,
5626 &cu->comp_unit_obstack,
5627 hashtab_obstack_allocate,
5628 dummy_obstack_deallocate);
e142c38c 5629
dee91e82
DE
5630 if (has_children)
5631 comp_unit_die->child = read_die_and_siblings (reader, info_ptr,
5632 &info_ptr, comp_unit_die);
5633 cu->dies = comp_unit_die;
5634 /* comp_unit_die is not stored in die_hash, no need. */
10b3939b
DJ
5635
5636 /* We try not to read any attributes in this function, because not
9cdd5dbd 5637 all CUs needed for references have been loaded yet, and symbol
10b3939b 5638 table processing isn't initialized. But we have to set the CU language,
dee91e82
DE
5639 or we won't be able to build types correctly.
5640 Similarly, if we do not read the producer, we can not apply
5641 producer-specific interpretation. */
95554aad 5642 prepare_one_comp_unit (cu, cu->dies, *language_ptr);
dee91e82 5643}
10b3939b 5644
dee91e82 5645/* Load the DIEs associated with PER_CU into memory. */
a6c727b2 5646
dee91e82 5647static void
95554aad
TT
5648load_full_comp_unit (struct dwarf2_per_cu_data *this_cu,
5649 enum language pretend_language)
dee91e82 5650{
3019eac3 5651 gdb_assert (! this_cu->is_debug_types);
c5b7e1cb 5652
95554aad
TT
5653 init_cutu_and_read_dies (this_cu, 1, 1, load_full_comp_unit_reader,
5654 &pretend_language);
10b3939b
DJ
5655}
5656
3da10d80
KS
5657/* Add a DIE to the delayed physname list. */
5658
5659static void
5660add_to_method_list (struct type *type, int fnfield_index, int index,
5661 const char *name, struct die_info *die,
5662 struct dwarf2_cu *cu)
5663{
5664 struct delayed_method_info mi;
5665 mi.type = type;
5666 mi.fnfield_index = fnfield_index;
5667 mi.index = index;
5668 mi.name = name;
5669 mi.die = die;
5670 VEC_safe_push (delayed_method_info, cu->method_list, &mi);
5671}
5672
5673/* A cleanup for freeing the delayed method list. */
5674
5675static void
5676free_delayed_list (void *ptr)
5677{
5678 struct dwarf2_cu *cu = (struct dwarf2_cu *) ptr;
5679 if (cu->method_list != NULL)
5680 {
5681 VEC_free (delayed_method_info, cu->method_list);
5682 cu->method_list = NULL;
5683 }
5684}
5685
5686/* Compute the physnames of any methods on the CU's method list.
5687
5688 The computation of method physnames is delayed in order to avoid the
5689 (bad) condition that one of the method's formal parameters is of an as yet
5690 incomplete type. */
5691
5692static void
5693compute_delayed_physnames (struct dwarf2_cu *cu)
5694{
5695 int i;
5696 struct delayed_method_info *mi;
5697 for (i = 0; VEC_iterate (delayed_method_info, cu->method_list, i, mi) ; ++i)
5698 {
1d06ead6 5699 const char *physname;
3da10d80
KS
5700 struct fn_fieldlist *fn_flp
5701 = &TYPE_FN_FIELDLIST (mi->type, mi->fnfield_index);
1d06ead6 5702 physname = dwarf2_physname ((char *) mi->name, mi->die, cu);
3da10d80
KS
5703 fn_flp->fn_fields[mi->index].physname = physname ? physname : "";
5704 }
5705}
5706
a766d390
DE
5707/* Go objects should be embedded in a DW_TAG_module DIE,
5708 and it's not clear if/how imported objects will appear.
5709 To keep Go support simple until that's worked out,
5710 go back through what we've read and create something usable.
5711 We could do this while processing each DIE, and feels kinda cleaner,
5712 but that way is more invasive.
5713 This is to, for example, allow the user to type "p var" or "b main"
5714 without having to specify the package name, and allow lookups
5715 of module.object to work in contexts that use the expression
5716 parser. */
5717
5718static void
5719fixup_go_packaging (struct dwarf2_cu *cu)
5720{
5721 char *package_name = NULL;
5722 struct pending *list;
5723 int i;
5724
5725 for (list = global_symbols; list != NULL; list = list->next)
5726 {
5727 for (i = 0; i < list->nsyms; ++i)
5728 {
5729 struct symbol *sym = list->symbol[i];
5730
5731 if (SYMBOL_LANGUAGE (sym) == language_go
5732 && SYMBOL_CLASS (sym) == LOC_BLOCK)
5733 {
5734 char *this_package_name = go_symbol_package_name (sym);
5735
5736 if (this_package_name == NULL)
5737 continue;
5738 if (package_name == NULL)
5739 package_name = this_package_name;
5740 else
5741 {
5742 if (strcmp (package_name, this_package_name) != 0)
5743 complaint (&symfile_complaints,
5744 _("Symtab %s has objects from two different Go packages: %s and %s"),
5745 (sym->symtab && sym->symtab->filename
5746 ? sym->symtab->filename
5747 : cu->objfile->name),
5748 this_package_name, package_name);
5749 xfree (this_package_name);
5750 }
5751 }
5752 }
5753 }
5754
5755 if (package_name != NULL)
5756 {
5757 struct objfile *objfile = cu->objfile;
5758 struct type *type = init_type (TYPE_CODE_MODULE, 0, 0,
5759 package_name, objfile);
5760 struct symbol *sym;
5761
5762 TYPE_TAG_NAME (type) = TYPE_NAME (type);
5763
5764 sym = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct symbol);
5765 SYMBOL_SET_LANGUAGE (sym, language_go);
5766 SYMBOL_SET_NAMES (sym, package_name, strlen (package_name), 1, objfile);
5767 /* This is not VAR_DOMAIN because we want a way to ensure a lookup of,
5768 e.g., "main" finds the "main" module and not C's main(). */
5769 SYMBOL_DOMAIN (sym) = STRUCT_DOMAIN;
5770 SYMBOL_CLASS (sym) = LOC_TYPEDEF;
5771 SYMBOL_TYPE (sym) = type;
5772
5773 add_symbol_to_list (sym, &global_symbols);
5774
5775 xfree (package_name);
5776 }
5777}
5778
95554aad
TT
5779static void compute_symtab_includes (struct dwarf2_per_cu_data *per_cu);
5780
5781/* Return the symtab for PER_CU. This works properly regardless of
5782 whether we're using the index or psymtabs. */
5783
5784static struct symtab *
5785get_symtab (struct dwarf2_per_cu_data *per_cu)
5786{
5787 return (dwarf2_per_objfile->using_index
5788 ? per_cu->v.quick->symtab
5789 : per_cu->v.psymtab->symtab);
5790}
5791
5792/* A helper function for computing the list of all symbol tables
5793 included by PER_CU. */
5794
5795static void
5796recursively_compute_inclusions (VEC (dwarf2_per_cu_ptr) **result,
5797 htab_t all_children,
5798 struct dwarf2_per_cu_data *per_cu)
5799{
5800 void **slot;
5801 int ix;
5802 struct dwarf2_per_cu_data *iter;
5803
5804 slot = htab_find_slot (all_children, per_cu, INSERT);
5805 if (*slot != NULL)
5806 {
5807 /* This inclusion and its children have been processed. */
5808 return;
5809 }
5810
5811 *slot = per_cu;
5812 /* Only add a CU if it has a symbol table. */
5813 if (get_symtab (per_cu) != NULL)
5814 VEC_safe_push (dwarf2_per_cu_ptr, *result, per_cu);
5815
5816 for (ix = 0;
5817 VEC_iterate (dwarf2_per_cu_ptr, per_cu->imported_symtabs, ix, iter);
5818 ++ix)
5819 recursively_compute_inclusions (result, all_children, iter);
5820}
5821
5822/* Compute the symtab 'includes' fields for the symtab related to
5823 PER_CU. */
5824
5825static void
5826compute_symtab_includes (struct dwarf2_per_cu_data *per_cu)
5827{
5828 if (!VEC_empty (dwarf2_per_cu_ptr, per_cu->imported_symtabs))
5829 {
5830 int ix, len;
5831 struct dwarf2_per_cu_data *iter;
5832 VEC (dwarf2_per_cu_ptr) *result_children = NULL;
5833 htab_t all_children;
5834 struct symtab *symtab = get_symtab (per_cu);
5835
5836 /* If we don't have a symtab, we can just skip this case. */
5837 if (symtab == NULL)
5838 return;
5839
5840 all_children = htab_create_alloc (1, htab_hash_pointer, htab_eq_pointer,
5841 NULL, xcalloc, xfree);
5842
5843 for (ix = 0;
5844 VEC_iterate (dwarf2_per_cu_ptr, per_cu->imported_symtabs,
5845 ix, iter);
5846 ++ix)
5847 recursively_compute_inclusions (&result_children, all_children, iter);
5848
5849 /* Now we have a transitive closure of all the included CUs, so
5850 we can convert it to a list of symtabs. */
5851 len = VEC_length (dwarf2_per_cu_ptr, result_children);
5852 symtab->includes
5853 = obstack_alloc (&dwarf2_per_objfile->objfile->objfile_obstack,
5854 (len + 1) * sizeof (struct symtab *));
5855 for (ix = 0;
5856 VEC_iterate (dwarf2_per_cu_ptr, result_children, ix, iter);
5857 ++ix)
5858 symtab->includes[ix] = get_symtab (iter);
5859 symtab->includes[len] = NULL;
5860
5861 VEC_free (dwarf2_per_cu_ptr, result_children);
5862 htab_delete (all_children);
5863 }
5864}
5865
5866/* Compute the 'includes' field for the symtabs of all the CUs we just
5867 read. */
5868
5869static void
5870process_cu_includes (void)
5871{
5872 int ix;
5873 struct dwarf2_per_cu_data *iter;
5874
5875 for (ix = 0;
5876 VEC_iterate (dwarf2_per_cu_ptr, dwarf2_per_objfile->just_read_cus,
5877 ix, iter);
5878 ++ix)
5879 compute_symtab_includes (iter);
5880
5881 VEC_free (dwarf2_per_cu_ptr, dwarf2_per_objfile->just_read_cus);
5882}
5883
9cdd5dbd 5884/* Generate full symbol information for PER_CU, whose DIEs have
10b3939b
DJ
5885 already been loaded into memory. */
5886
5887static void
95554aad
TT
5888process_full_comp_unit (struct dwarf2_per_cu_data *per_cu,
5889 enum language pretend_language)
10b3939b 5890{
10b3939b 5891 struct dwarf2_cu *cu = per_cu->cu;
9291a0cd 5892 struct objfile *objfile = per_cu->objfile;
10b3939b
DJ
5893 CORE_ADDR lowpc, highpc;
5894 struct symtab *symtab;
3da10d80 5895 struct cleanup *back_to, *delayed_list_cleanup;
10b3939b
DJ
5896 CORE_ADDR baseaddr;
5897
45cfd468
DE
5898 if (dwarf2_read_debug)
5899 {
5900 fprintf_unfiltered (gdb_stdlog,
5901 "Expanding symtab of %s at offset 0x%x\n",
5902 per_cu->is_debug_types ? "TU" : "CU",
5903 per_cu->offset.sect_off);
5904 }
5905
10b3939b
DJ
5906 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
5907
10b3939b
DJ
5908 buildsym_init ();
5909 back_to = make_cleanup (really_free_pendings, NULL);
3da10d80 5910 delayed_list_cleanup = make_cleanup (free_delayed_list, cu);
10b3939b
DJ
5911
5912 cu->list_in_scope = &file_symbols;
c906108c 5913
95554aad
TT
5914 cu->language = pretend_language;
5915 cu->language_defn = language_def (cu->language);
5916
c906108c 5917 /* Do line number decoding in read_file_scope () */
10b3939b 5918 process_die (cu->dies, cu);
c906108c 5919
a766d390
DE
5920 /* For now fudge the Go package. */
5921 if (cu->language == language_go)
5922 fixup_go_packaging (cu);
5923
3da10d80
KS
5924 /* Now that we have processed all the DIEs in the CU, all the types
5925 should be complete, and it should now be safe to compute all of the
5926 physnames. */
5927 compute_delayed_physnames (cu);
5928 do_cleanups (delayed_list_cleanup);
5929
fae299cd
DC
5930 /* Some compilers don't define a DW_AT_high_pc attribute for the
5931 compilation unit. If the DW_AT_high_pc is missing, synthesize
5932 it, by scanning the DIE's below the compilation unit. */
10b3939b 5933 get_scope_pc_bounds (cu->dies, &lowpc, &highpc, cu);
c906108c 5934
613e1657 5935 symtab = end_symtab (highpc + baseaddr, objfile, SECT_OFF_TEXT (objfile));
c906108c 5936
8be455d7 5937 if (symtab != NULL)
c906108c 5938 {
df15bd07 5939 int gcc_4_minor = producer_is_gcc_ge_4 (cu->producer);
4632c0d0 5940
8be455d7
JK
5941 /* Set symtab language to language from DW_AT_language. If the
5942 compilation is from a C file generated by language preprocessors, do
5943 not set the language if it was already deduced by start_subfile. */
5944 if (!(cu->language == language_c && symtab->language != language_c))
5945 symtab->language = cu->language;
5946
5947 /* GCC-4.0 has started to support -fvar-tracking. GCC-3.x still can
5948 produce DW_AT_location with location lists but it can be possibly
ab260dad
JK
5949 invalid without -fvar-tracking. Still up to GCC-4.4.x incl. 4.4.0
5950 there were bugs in prologue debug info, fixed later in GCC-4.5
5951 by "unwind info for epilogues" patch (which is not directly related).
8be455d7
JK
5952
5953 For -gdwarf-4 type units LOCATIONS_VALID indication is fortunately not
5954 needed, it would be wrong due to missing DW_AT_producer there.
5955
5956 Still one can confuse GDB by using non-standard GCC compilation
5957 options - this waits on GCC PR other/32998 (-frecord-gcc-switches).
5958 */
ab260dad 5959 if (cu->has_loclist && gcc_4_minor >= 5)
8be455d7 5960 symtab->locations_valid = 1;
e0d00bc7
JK
5961
5962 if (gcc_4_minor >= 5)
5963 symtab->epilogue_unwind_valid = 1;
96408a79
SA
5964
5965 symtab->call_site_htab = cu->call_site_htab;
c906108c 5966 }
9291a0cd
TT
5967
5968 if (dwarf2_per_objfile->using_index)
5969 per_cu->v.quick->symtab = symtab;
5970 else
5971 {
5972 struct partial_symtab *pst = per_cu->v.psymtab;
5973 pst->symtab = symtab;
5974 pst->readin = 1;
5975 }
c906108c 5976
95554aad
TT
5977 /* Push it for inclusion processing later. */
5978 VEC_safe_push (dwarf2_per_cu_ptr, dwarf2_per_objfile->just_read_cus, per_cu);
5979
c906108c 5980 do_cleanups (back_to);
45cfd468
DE
5981
5982 if (dwarf2_read_debug)
5983 {
5984 fprintf_unfiltered (gdb_stdlog,
5985 "Done expanding symtab of %s at offset 0x%x\n",
5986 per_cu->is_debug_types ? "TU" : "CU",
5987 per_cu->offset.sect_off);
5988 }
c906108c
SS
5989}
5990
95554aad
TT
5991/* Process an imported unit DIE. */
5992
5993static void
5994process_imported_unit_die (struct die_info *die, struct dwarf2_cu *cu)
5995{
5996 struct attribute *attr;
5997
5998 attr = dwarf2_attr (die, DW_AT_import, cu);
5999 if (attr != NULL)
6000 {
6001 struct dwarf2_per_cu_data *per_cu;
6002 struct symtab *imported_symtab;
6003 sect_offset offset;
6004
6005 offset = dwarf2_get_ref_die_offset (attr);
6006 per_cu = dwarf2_find_containing_comp_unit (offset, cu->objfile);
6007
6008 /* Queue the unit, if needed. */
6009 if (maybe_queue_comp_unit (cu, per_cu, cu->language))
6010 load_full_comp_unit (per_cu, cu->language);
6011
6012 VEC_safe_push (dwarf2_per_cu_ptr, cu->per_cu->imported_symtabs,
6013 per_cu);
6014 }
6015}
6016
c906108c
SS
6017/* Process a die and its children. */
6018
6019static void
e7c27a73 6020process_die (struct die_info *die, struct dwarf2_cu *cu)
c906108c
SS
6021{
6022 switch (die->tag)
6023 {
6024 case DW_TAG_padding:
6025 break;
6026 case DW_TAG_compile_unit:
95554aad 6027 case DW_TAG_partial_unit:
e7c27a73 6028 read_file_scope (die, cu);
c906108c 6029 break;
348e048f
DE
6030 case DW_TAG_type_unit:
6031 read_type_unit_scope (die, cu);
6032 break;
c906108c 6033 case DW_TAG_subprogram:
c906108c 6034 case DW_TAG_inlined_subroutine:
edb3359d 6035 read_func_scope (die, cu);
c906108c
SS
6036 break;
6037 case DW_TAG_lexical_block:
14898363
L
6038 case DW_TAG_try_block:
6039 case DW_TAG_catch_block:
e7c27a73 6040 read_lexical_block_scope (die, cu);
c906108c 6041 break;
96408a79
SA
6042 case DW_TAG_GNU_call_site:
6043 read_call_site_scope (die, cu);
6044 break;
c906108c 6045 case DW_TAG_class_type:
680b30c7 6046 case DW_TAG_interface_type:
c906108c
SS
6047 case DW_TAG_structure_type:
6048 case DW_TAG_union_type:
134d01f1 6049 process_structure_scope (die, cu);
c906108c
SS
6050 break;
6051 case DW_TAG_enumeration_type:
134d01f1 6052 process_enumeration_scope (die, cu);
c906108c 6053 break;
134d01f1 6054
f792889a
DJ
6055 /* These dies have a type, but processing them does not create
6056 a symbol or recurse to process the children. Therefore we can
6057 read them on-demand through read_type_die. */
c906108c 6058 case DW_TAG_subroutine_type:
72019c9c 6059 case DW_TAG_set_type:
c906108c 6060 case DW_TAG_array_type:
c906108c 6061 case DW_TAG_pointer_type:
c906108c 6062 case DW_TAG_ptr_to_member_type:
c906108c 6063 case DW_TAG_reference_type:
c906108c 6064 case DW_TAG_string_type:
c906108c 6065 break;
134d01f1 6066
c906108c 6067 case DW_TAG_base_type:
a02abb62 6068 case DW_TAG_subrange_type:
cb249c71 6069 case DW_TAG_typedef:
134d01f1
DJ
6070 /* Add a typedef symbol for the type definition, if it has a
6071 DW_AT_name. */
f792889a 6072 new_symbol (die, read_type_die (die, cu), cu);
a02abb62 6073 break;
c906108c 6074 case DW_TAG_common_block:
e7c27a73 6075 read_common_block (die, cu);
c906108c
SS
6076 break;
6077 case DW_TAG_common_inclusion:
6078 break;
d9fa45fe 6079 case DW_TAG_namespace:
63d06c5c 6080 processing_has_namespace_info = 1;
e7c27a73 6081 read_namespace (die, cu);
d9fa45fe 6082 break;
5d7cb8df 6083 case DW_TAG_module:
f55ee35c 6084 processing_has_namespace_info = 1;
5d7cb8df
JK
6085 read_module (die, cu);
6086 break;
d9fa45fe
DC
6087 case DW_TAG_imported_declaration:
6088 case DW_TAG_imported_module:
63d06c5c 6089 processing_has_namespace_info = 1;
27aa8d6a
SW
6090 if (die->child != NULL && (die->tag == DW_TAG_imported_declaration
6091 || cu->language != language_fortran))
6092 complaint (&symfile_complaints, _("Tag '%s' has unexpected children"),
6093 dwarf_tag_name (die->tag));
6094 read_import_statement (die, cu);
d9fa45fe 6095 break;
95554aad
TT
6096
6097 case DW_TAG_imported_unit:
6098 process_imported_unit_die (die, cu);
6099 break;
6100
c906108c 6101 default:
e7c27a73 6102 new_symbol (die, NULL, cu);
c906108c
SS
6103 break;
6104 }
6105}
6106
94af9270
KS
6107/* A helper function for dwarf2_compute_name which determines whether DIE
6108 needs to have the name of the scope prepended to the name listed in the
6109 die. */
6110
6111static int
6112die_needs_namespace (struct die_info *die, struct dwarf2_cu *cu)
6113{
1c809c68
TT
6114 struct attribute *attr;
6115
94af9270
KS
6116 switch (die->tag)
6117 {
6118 case DW_TAG_namespace:
6119 case DW_TAG_typedef:
6120 case DW_TAG_class_type:
6121 case DW_TAG_interface_type:
6122 case DW_TAG_structure_type:
6123 case DW_TAG_union_type:
6124 case DW_TAG_enumeration_type:
6125 case DW_TAG_enumerator:
6126 case DW_TAG_subprogram:
6127 case DW_TAG_member:
6128 return 1;
6129
6130 case DW_TAG_variable:
c2b0a229 6131 case DW_TAG_constant:
94af9270
KS
6132 /* We only need to prefix "globally" visible variables. These include
6133 any variable marked with DW_AT_external or any variable that
6134 lives in a namespace. [Variables in anonymous namespaces
6135 require prefixing, but they are not DW_AT_external.] */
6136
6137 if (dwarf2_attr (die, DW_AT_specification, cu))
6138 {
6139 struct dwarf2_cu *spec_cu = cu;
9a619af0 6140
94af9270
KS
6141 return die_needs_namespace (die_specification (die, &spec_cu),
6142 spec_cu);
6143 }
6144
1c809c68 6145 attr = dwarf2_attr (die, DW_AT_external, cu);
f55ee35c
JK
6146 if (attr == NULL && die->parent->tag != DW_TAG_namespace
6147 && die->parent->tag != DW_TAG_module)
1c809c68
TT
6148 return 0;
6149 /* A variable in a lexical block of some kind does not need a
6150 namespace, even though in C++ such variables may be external
6151 and have a mangled name. */
6152 if (die->parent->tag == DW_TAG_lexical_block
6153 || die->parent->tag == DW_TAG_try_block
1054b214
TT
6154 || die->parent->tag == DW_TAG_catch_block
6155 || die->parent->tag == DW_TAG_subprogram)
1c809c68
TT
6156 return 0;
6157 return 1;
94af9270
KS
6158
6159 default:
6160 return 0;
6161 }
6162}
6163
98bfdba5
PA
6164/* Retrieve the last character from a mem_file. */
6165
6166static void
6167do_ui_file_peek_last (void *object, const char *buffer, long length)
6168{
6169 char *last_char_p = (char *) object;
6170
6171 if (length > 0)
6172 *last_char_p = buffer[length - 1];
6173}
6174
94af9270 6175/* Compute the fully qualified name of DIE in CU. If PHYSNAME is nonzero,
a766d390
DE
6176 compute the physname for the object, which include a method's:
6177 - formal parameters (C++/Java),
6178 - receiver type (Go),
6179 - return type (Java).
6180
6181 The term "physname" is a bit confusing.
6182 For C++, for example, it is the demangled name.
6183 For Go, for example, it's the mangled name.
94af9270 6184
af6b7be1
JB
6185 For Ada, return the DIE's linkage name rather than the fully qualified
6186 name. PHYSNAME is ignored..
6187
94af9270
KS
6188 The result is allocated on the objfile_obstack and canonicalized. */
6189
6190static const char *
6191dwarf2_compute_name (char *name, struct die_info *die, struct dwarf2_cu *cu,
6192 int physname)
6193{
bb5ed363
DE
6194 struct objfile *objfile = cu->objfile;
6195
94af9270
KS
6196 if (name == NULL)
6197 name = dwarf2_name (die, cu);
6198
f55ee35c
JK
6199 /* For Fortran GDB prefers DW_AT_*linkage_name if present but otherwise
6200 compute it by typename_concat inside GDB. */
6201 if (cu->language == language_ada
6202 || (cu->language == language_fortran && physname))
6203 {
6204 /* For Ada unit, we prefer the linkage name over the name, as
6205 the former contains the exported name, which the user expects
6206 to be able to reference. Ideally, we want the user to be able
6207 to reference this entity using either natural or linkage name,
6208 but we haven't started looking at this enhancement yet. */
6209 struct attribute *attr;
6210
6211 attr = dwarf2_attr (die, DW_AT_linkage_name, cu);
6212 if (attr == NULL)
6213 attr = dwarf2_attr (die, DW_AT_MIPS_linkage_name, cu);
6214 if (attr && DW_STRING (attr))
6215 return DW_STRING (attr);
6216 }
6217
94af9270
KS
6218 /* These are the only languages we know how to qualify names in. */
6219 if (name != NULL
f55ee35c
JK
6220 && (cu->language == language_cplus || cu->language == language_java
6221 || cu->language == language_fortran))
94af9270
KS
6222 {
6223 if (die_needs_namespace (die, cu))
6224 {
6225 long length;
0d5cff50 6226 const char *prefix;
94af9270
KS
6227 struct ui_file *buf;
6228
6229 prefix = determine_prefix (die, cu);
6230 buf = mem_fileopen ();
6231 if (*prefix != '\0')
6232 {
f55ee35c
JK
6233 char *prefixed_name = typename_concat (NULL, prefix, name,
6234 physname, cu);
9a619af0 6235
94af9270
KS
6236 fputs_unfiltered (prefixed_name, buf);
6237 xfree (prefixed_name);
6238 }
6239 else
62d5b8da 6240 fputs_unfiltered (name, buf);
94af9270 6241
98bfdba5
PA
6242 /* Template parameters may be specified in the DIE's DW_AT_name, or
6243 as children with DW_TAG_template_type_param or
6244 DW_TAG_value_type_param. If the latter, add them to the name
6245 here. If the name already has template parameters, then
6246 skip this step; some versions of GCC emit both, and
6247 it is more efficient to use the pre-computed name.
6248
6249 Something to keep in mind about this process: it is very
6250 unlikely, or in some cases downright impossible, to produce
6251 something that will match the mangled name of a function.
6252 If the definition of the function has the same debug info,
6253 we should be able to match up with it anyway. But fallbacks
6254 using the minimal symbol, for instance to find a method
6255 implemented in a stripped copy of libstdc++, will not work.
6256 If we do not have debug info for the definition, we will have to
6257 match them up some other way.
6258
6259 When we do name matching there is a related problem with function
6260 templates; two instantiated function templates are allowed to
6261 differ only by their return types, which we do not add here. */
6262
6263 if (cu->language == language_cplus && strchr (name, '<') == NULL)
6264 {
6265 struct attribute *attr;
6266 struct die_info *child;
6267 int first = 1;
6268
6269 die->building_fullname = 1;
6270
6271 for (child = die->child; child != NULL; child = child->sibling)
6272 {
6273 struct type *type;
12df843f 6274 LONGEST value;
98bfdba5
PA
6275 gdb_byte *bytes;
6276 struct dwarf2_locexpr_baton *baton;
6277 struct value *v;
6278
6279 if (child->tag != DW_TAG_template_type_param
6280 && child->tag != DW_TAG_template_value_param)
6281 continue;
6282
6283 if (first)
6284 {
6285 fputs_unfiltered ("<", buf);
6286 first = 0;
6287 }
6288 else
6289 fputs_unfiltered (", ", buf);
6290
6291 attr = dwarf2_attr (child, DW_AT_type, cu);
6292 if (attr == NULL)
6293 {
6294 complaint (&symfile_complaints,
6295 _("template parameter missing DW_AT_type"));
6296 fputs_unfiltered ("UNKNOWN_TYPE", buf);
6297 continue;
6298 }
6299 type = die_type (child, cu);
6300
6301 if (child->tag == DW_TAG_template_type_param)
6302 {
6303 c_print_type (type, "", buf, -1, 0);
6304 continue;
6305 }
6306
6307 attr = dwarf2_attr (child, DW_AT_const_value, cu);
6308 if (attr == NULL)
6309 {
6310 complaint (&symfile_complaints,
3e43a32a
MS
6311 _("template parameter missing "
6312 "DW_AT_const_value"));
98bfdba5
PA
6313 fputs_unfiltered ("UNKNOWN_VALUE", buf);
6314 continue;
6315 }
6316
6317 dwarf2_const_value_attr (attr, type, name,
6318 &cu->comp_unit_obstack, cu,
6319 &value, &bytes, &baton);
6320
6321 if (TYPE_NOSIGN (type))
6322 /* GDB prints characters as NUMBER 'CHAR'. If that's
6323 changed, this can use value_print instead. */
6324 c_printchar (value, type, buf);
6325 else
6326 {
6327 struct value_print_options opts;
6328
6329 if (baton != NULL)
6330 v = dwarf2_evaluate_loc_desc (type, NULL,
6331 baton->data,
6332 baton->size,
6333 baton->per_cu);
6334 else if (bytes != NULL)
6335 {
6336 v = allocate_value (type);
6337 memcpy (value_contents_writeable (v), bytes,
6338 TYPE_LENGTH (type));
6339 }
6340 else
6341 v = value_from_longest (type, value);
6342
3e43a32a
MS
6343 /* Specify decimal so that we do not depend on
6344 the radix. */
98bfdba5
PA
6345 get_formatted_print_options (&opts, 'd');
6346 opts.raw = 1;
6347 value_print (v, buf, &opts);
6348 release_value (v);
6349 value_free (v);
6350 }
6351 }
6352
6353 die->building_fullname = 0;
6354
6355 if (!first)
6356 {
6357 /* Close the argument list, with a space if necessary
6358 (nested templates). */
6359 char last_char = '\0';
6360 ui_file_put (buf, do_ui_file_peek_last, &last_char);
6361 if (last_char == '>')
6362 fputs_unfiltered (" >", buf);
6363 else
6364 fputs_unfiltered (">", buf);
6365 }
6366 }
6367
94af9270
KS
6368 /* For Java and C++ methods, append formal parameter type
6369 information, if PHYSNAME. */
6e70227d 6370
94af9270
KS
6371 if (physname && die->tag == DW_TAG_subprogram
6372 && (cu->language == language_cplus
6373 || cu->language == language_java))
6374 {
6375 struct type *type = read_type_die (die, cu);
6376
3167638f 6377 c_type_print_args (type, buf, 1, cu->language);
94af9270
KS
6378
6379 if (cu->language == language_java)
6380 {
6381 /* For java, we must append the return type to method
0963b4bd 6382 names. */
94af9270
KS
6383 if (die->tag == DW_TAG_subprogram)
6384 java_print_type (TYPE_TARGET_TYPE (type), "", buf,
6385 0, 0);
6386 }
6387 else if (cu->language == language_cplus)
6388 {
60430eff
DJ
6389 /* Assume that an artificial first parameter is
6390 "this", but do not crash if it is not. RealView
6391 marks unnamed (and thus unused) parameters as
6392 artificial; there is no way to differentiate
6393 the two cases. */
94af9270
KS
6394 if (TYPE_NFIELDS (type) > 0
6395 && TYPE_FIELD_ARTIFICIAL (type, 0)
60430eff 6396 && TYPE_CODE (TYPE_FIELD_TYPE (type, 0)) == TYPE_CODE_PTR
3e43a32a
MS
6397 && TYPE_CONST (TYPE_TARGET_TYPE (TYPE_FIELD_TYPE (type,
6398 0))))
94af9270
KS
6399 fputs_unfiltered (" const", buf);
6400 }
6401 }
6402
bb5ed363 6403 name = ui_file_obsavestring (buf, &objfile->objfile_obstack,
94af9270
KS
6404 &length);
6405 ui_file_delete (buf);
6406
6407 if (cu->language == language_cplus)
6408 {
6409 char *cname
6410 = dwarf2_canonicalize_name (name, cu,
bb5ed363 6411 &objfile->objfile_obstack);
9a619af0 6412
94af9270
KS
6413 if (cname != NULL)
6414 name = cname;
6415 }
6416 }
6417 }
6418
6419 return name;
6420}
6421
0114d602
DJ
6422/* Return the fully qualified name of DIE, based on its DW_AT_name.
6423 If scope qualifiers are appropriate they will be added. The result
6424 will be allocated on the objfile_obstack, or NULL if the DIE does
94af9270
KS
6425 not have a name. NAME may either be from a previous call to
6426 dwarf2_name or NULL.
6427
0963b4bd 6428 The output string will be canonicalized (if C++/Java). */
0114d602
DJ
6429
6430static const char *
94af9270 6431dwarf2_full_name (char *name, struct die_info *die, struct dwarf2_cu *cu)
0114d602 6432{
94af9270
KS
6433 return dwarf2_compute_name (name, die, cu, 0);
6434}
0114d602 6435
94af9270
KS
6436/* Construct a physname for the given DIE in CU. NAME may either be
6437 from a previous call to dwarf2_name or NULL. The result will be
6438 allocated on the objfile_objstack or NULL if the DIE does not have a
6439 name.
0114d602 6440
94af9270 6441 The output string will be canonicalized (if C++/Java). */
0114d602 6442
94af9270
KS
6443static const char *
6444dwarf2_physname (char *name, struct die_info *die, struct dwarf2_cu *cu)
6445{
bb5ed363 6446 struct objfile *objfile = cu->objfile;
900e11f9
JK
6447 struct attribute *attr;
6448 const char *retval, *mangled = NULL, *canon = NULL;
6449 struct cleanup *back_to;
6450 int need_copy = 1;
6451
6452 /* In this case dwarf2_compute_name is just a shortcut not building anything
6453 on its own. */
6454 if (!die_needs_namespace (die, cu))
6455 return dwarf2_compute_name (name, die, cu, 1);
6456
6457 back_to = make_cleanup (null_cleanup, NULL);
6458
6459 attr = dwarf2_attr (die, DW_AT_linkage_name, cu);
6460 if (!attr)
6461 attr = dwarf2_attr (die, DW_AT_MIPS_linkage_name, cu);
6462
6463 /* DW_AT_linkage_name is missing in some cases - depend on what GDB
6464 has computed. */
6465 if (attr && DW_STRING (attr))
6466 {
6467 char *demangled;
6468
6469 mangled = DW_STRING (attr);
6470
6471 /* Use DMGL_RET_DROP for C++ template functions to suppress their return
6472 type. It is easier for GDB users to search for such functions as
6473 `name(params)' than `long name(params)'. In such case the minimal
6474 symbol names do not match the full symbol names but for template
6475 functions there is never a need to look up their definition from their
6476 declaration so the only disadvantage remains the minimal symbol
6477 variant `long name(params)' does not have the proper inferior type.
6478 */
6479
a766d390
DE
6480 if (cu->language == language_go)
6481 {
6482 /* This is a lie, but we already lie to the caller new_symbol_full.
6483 new_symbol_full assumes we return the mangled name.
6484 This just undoes that lie until things are cleaned up. */
6485 demangled = NULL;
6486 }
6487 else
6488 {
6489 demangled = cplus_demangle (mangled,
6490 (DMGL_PARAMS | DMGL_ANSI
6491 | (cu->language == language_java
6492 ? DMGL_JAVA | DMGL_RET_POSTFIX
6493 : DMGL_RET_DROP)));
6494 }
900e11f9
JK
6495 if (demangled)
6496 {
6497 make_cleanup (xfree, demangled);
6498 canon = demangled;
6499 }
6500 else
6501 {
6502 canon = mangled;
6503 need_copy = 0;
6504 }
6505 }
6506
6507 if (canon == NULL || check_physname)
6508 {
6509 const char *physname = dwarf2_compute_name (name, die, cu, 1);
6510
6511 if (canon != NULL && strcmp (physname, canon) != 0)
6512 {
6513 /* It may not mean a bug in GDB. The compiler could also
6514 compute DW_AT_linkage_name incorrectly. But in such case
6515 GDB would need to be bug-to-bug compatible. */
6516
6517 complaint (&symfile_complaints,
6518 _("Computed physname <%s> does not match demangled <%s> "
6519 "(from linkage <%s>) - DIE at 0x%x [in module %s]"),
b64f50a1 6520 physname, canon, mangled, die->offset.sect_off, objfile->name);
900e11f9
JK
6521
6522 /* Prefer DW_AT_linkage_name (in the CANON form) - when it
6523 is available here - over computed PHYSNAME. It is safer
6524 against both buggy GDB and buggy compilers. */
6525
6526 retval = canon;
6527 }
6528 else
6529 {
6530 retval = physname;
6531 need_copy = 0;
6532 }
6533 }
6534 else
6535 retval = canon;
6536
6537 if (need_copy)
6538 retval = obsavestring (retval, strlen (retval),
bb5ed363 6539 &objfile->objfile_obstack);
900e11f9
JK
6540
6541 do_cleanups (back_to);
6542 return retval;
0114d602
DJ
6543}
6544
27aa8d6a
SW
6545/* Read the import statement specified by the given die and record it. */
6546
6547static void
6548read_import_statement (struct die_info *die, struct dwarf2_cu *cu)
6549{
bb5ed363 6550 struct objfile *objfile = cu->objfile;
27aa8d6a 6551 struct attribute *import_attr;
32019081 6552 struct die_info *imported_die, *child_die;
de4affc9 6553 struct dwarf2_cu *imported_cu;
27aa8d6a 6554 const char *imported_name;
794684b6 6555 const char *imported_name_prefix;
13387711
SW
6556 const char *canonical_name;
6557 const char *import_alias;
6558 const char *imported_declaration = NULL;
794684b6 6559 const char *import_prefix;
32019081
JK
6560 VEC (const_char_ptr) *excludes = NULL;
6561 struct cleanup *cleanups;
13387711
SW
6562
6563 char *temp;
27aa8d6a
SW
6564
6565 import_attr = dwarf2_attr (die, DW_AT_import, cu);
6566 if (import_attr == NULL)
6567 {
6568 complaint (&symfile_complaints, _("Tag '%s' has no DW_AT_import"),
6569 dwarf_tag_name (die->tag));
6570 return;
6571 }
6572
de4affc9
CC
6573 imported_cu = cu;
6574 imported_die = follow_die_ref_or_sig (die, import_attr, &imported_cu);
6575 imported_name = dwarf2_name (imported_die, imported_cu);
27aa8d6a
SW
6576 if (imported_name == NULL)
6577 {
6578 /* GCC bug: https://bugzilla.redhat.com/show_bug.cgi?id=506524
6579
6580 The import in the following code:
6581 namespace A
6582 {
6583 typedef int B;
6584 }
6585
6586 int main ()
6587 {
6588 using A::B;
6589 B b;
6590 return b;
6591 }
6592
6593 ...
6594 <2><51>: Abbrev Number: 3 (DW_TAG_imported_declaration)
6595 <52> DW_AT_decl_file : 1
6596 <53> DW_AT_decl_line : 6
6597 <54> DW_AT_import : <0x75>
6598 <2><58>: Abbrev Number: 4 (DW_TAG_typedef)
6599 <59> DW_AT_name : B
6600 <5b> DW_AT_decl_file : 1
6601 <5c> DW_AT_decl_line : 2
6602 <5d> DW_AT_type : <0x6e>
6603 ...
6604 <1><75>: Abbrev Number: 7 (DW_TAG_base_type)
6605 <76> DW_AT_byte_size : 4
6606 <77> DW_AT_encoding : 5 (signed)
6607
6608 imports the wrong die ( 0x75 instead of 0x58 ).
6609 This case will be ignored until the gcc bug is fixed. */
6610 return;
6611 }
6612
82856980
SW
6613 /* Figure out the local name after import. */
6614 import_alias = dwarf2_name (die, cu);
27aa8d6a 6615
794684b6
SW
6616 /* Figure out where the statement is being imported to. */
6617 import_prefix = determine_prefix (die, cu);
6618
6619 /* Figure out what the scope of the imported die is and prepend it
6620 to the name of the imported die. */
de4affc9 6621 imported_name_prefix = determine_prefix (imported_die, imported_cu);
794684b6 6622
f55ee35c
JK
6623 if (imported_die->tag != DW_TAG_namespace
6624 && imported_die->tag != DW_TAG_module)
794684b6 6625 {
13387711
SW
6626 imported_declaration = imported_name;
6627 canonical_name = imported_name_prefix;
794684b6 6628 }
13387711 6629 else if (strlen (imported_name_prefix) > 0)
794684b6 6630 {
13387711
SW
6631 temp = alloca (strlen (imported_name_prefix)
6632 + 2 + strlen (imported_name) + 1);
6633 strcpy (temp, imported_name_prefix);
6634 strcat (temp, "::");
6635 strcat (temp, imported_name);
6636 canonical_name = temp;
794684b6 6637 }
13387711
SW
6638 else
6639 canonical_name = imported_name;
794684b6 6640
32019081
JK
6641 cleanups = make_cleanup (VEC_cleanup (const_char_ptr), &excludes);
6642
6643 if (die->tag == DW_TAG_imported_module && cu->language == language_fortran)
6644 for (child_die = die->child; child_die && child_die->tag;
6645 child_die = sibling_die (child_die))
6646 {
6647 /* DWARF-4: A Fortran use statement with a “rename list” may be
6648 represented by an imported module entry with an import attribute
6649 referring to the module and owned entries corresponding to those
6650 entities that are renamed as part of being imported. */
6651
6652 if (child_die->tag != DW_TAG_imported_declaration)
6653 {
6654 complaint (&symfile_complaints,
6655 _("child DW_TAG_imported_declaration expected "
6656 "- DIE at 0x%x [in module %s]"),
b64f50a1 6657 child_die->offset.sect_off, objfile->name);
32019081
JK
6658 continue;
6659 }
6660
6661 import_attr = dwarf2_attr (child_die, DW_AT_import, cu);
6662 if (import_attr == NULL)
6663 {
6664 complaint (&symfile_complaints, _("Tag '%s' has no DW_AT_import"),
6665 dwarf_tag_name (child_die->tag));
6666 continue;
6667 }
6668
6669 imported_cu = cu;
6670 imported_die = follow_die_ref_or_sig (child_die, import_attr,
6671 &imported_cu);
6672 imported_name = dwarf2_name (imported_die, imported_cu);
6673 if (imported_name == NULL)
6674 {
6675 complaint (&symfile_complaints,
6676 _("child DW_TAG_imported_declaration has unknown "
6677 "imported name - DIE at 0x%x [in module %s]"),
b64f50a1 6678 child_die->offset.sect_off, objfile->name);
32019081
JK
6679 continue;
6680 }
6681
6682 VEC_safe_push (const_char_ptr, excludes, imported_name);
6683
6684 process_die (child_die, cu);
6685 }
6686
c0cc3a76
SW
6687 cp_add_using_directive (import_prefix,
6688 canonical_name,
6689 import_alias,
13387711 6690 imported_declaration,
32019081 6691 excludes,
bb5ed363 6692 &objfile->objfile_obstack);
32019081
JK
6693
6694 do_cleanups (cleanups);
27aa8d6a
SW
6695}
6696
ae2de4f8
DE
6697/* Cleanup function for read_file_scope. */
6698
cb1df416
DJ
6699static void
6700free_cu_line_header (void *arg)
6701{
6702 struct dwarf2_cu *cu = arg;
6703
6704 free_line_header (cu->line_header);
6705 cu->line_header = NULL;
6706}
6707
9291a0cd
TT
6708static void
6709find_file_and_directory (struct die_info *die, struct dwarf2_cu *cu,
6710 char **name, char **comp_dir)
6711{
6712 struct attribute *attr;
6713
6714 *name = NULL;
6715 *comp_dir = NULL;
6716
6717 /* Find the filename. Do not use dwarf2_name here, since the filename
6718 is not a source language identifier. */
6719 attr = dwarf2_attr (die, DW_AT_name, cu);
6720 if (attr)
6721 {
6722 *name = DW_STRING (attr);
6723 }
6724
6725 attr = dwarf2_attr (die, DW_AT_comp_dir, cu);
6726 if (attr)
6727 *comp_dir = DW_STRING (attr);
6728 else if (*name != NULL && IS_ABSOLUTE_PATH (*name))
6729 {
6730 *comp_dir = ldirname (*name);
6731 if (*comp_dir != NULL)
6732 make_cleanup (xfree, *comp_dir);
6733 }
6734 if (*comp_dir != NULL)
6735 {
6736 /* Irix 6.2 native cc prepends <machine>.: to the compilation
6737 directory, get rid of it. */
6738 char *cp = strchr (*comp_dir, ':');
6739
6740 if (cp && cp != *comp_dir && cp[-1] == '.' && cp[1] == '/')
6741 *comp_dir = cp + 1;
6742 }
6743
6744 if (*name == NULL)
6745 *name = "<unknown>";
6746}
6747
f3f5162e
DE
6748/* Handle DW_AT_stmt_list for a compilation unit or type unit.
6749 DIE is the DW_TAG_compile_unit or DW_TAG_type_unit die for CU.
6750 COMP_DIR is the compilation directory.
6751 WANT_LINE_INFO is non-zero if the pc/line-number mapping is needed. */
2ab95328
TT
6752
6753static void
6754handle_DW_AT_stmt_list (struct die_info *die, struct dwarf2_cu *cu,
f3f5162e 6755 const char *comp_dir, int want_line_info)
2ab95328
TT
6756{
6757 struct attribute *attr;
2ab95328 6758
2ab95328
TT
6759 attr = dwarf2_attr (die, DW_AT_stmt_list, cu);
6760 if (attr)
6761 {
6762 unsigned int line_offset = DW_UNSND (attr);
6763 struct line_header *line_header
3019eac3 6764 = dwarf_decode_line_header (line_offset, cu);
2ab95328
TT
6765
6766 if (line_header)
dee91e82
DE
6767 {
6768 cu->line_header = line_header;
6769 make_cleanup (free_cu_line_header, cu);
f3f5162e 6770 dwarf_decode_lines (line_header, comp_dir, cu, NULL, want_line_info);
dee91e82 6771 }
2ab95328
TT
6772 }
6773}
6774
95554aad 6775/* Process DW_TAG_compile_unit or DW_TAG_partial_unit. */
ae2de4f8 6776
c906108c 6777static void
e7c27a73 6778read_file_scope (struct die_info *die, struct dwarf2_cu *cu)
c906108c 6779{
dee91e82 6780 struct objfile *objfile = dwarf2_per_objfile->objfile;
debd256d 6781 struct cleanup *back_to = make_cleanup (null_cleanup, 0);
2acceee2 6782 CORE_ADDR lowpc = ((CORE_ADDR) -1);
c906108c
SS
6783 CORE_ADDR highpc = ((CORE_ADDR) 0);
6784 struct attribute *attr;
e1024ff1 6785 char *name = NULL;
c906108c
SS
6786 char *comp_dir = NULL;
6787 struct die_info *child_die;
6788 bfd *abfd = objfile->obfd;
e142c38c 6789 CORE_ADDR baseaddr;
6e70227d 6790
e142c38c 6791 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
c906108c 6792
fae299cd 6793 get_scope_pc_bounds (die, &lowpc, &highpc, cu);
c906108c
SS
6794
6795 /* If we didn't find a lowpc, set it to highpc to avoid complaints
6796 from finish_block. */
2acceee2 6797 if (lowpc == ((CORE_ADDR) -1))
c906108c
SS
6798 lowpc = highpc;
6799 lowpc += baseaddr;
6800 highpc += baseaddr;
6801
9291a0cd 6802 find_file_and_directory (die, cu, &name, &comp_dir);
e1024ff1 6803
95554aad 6804 prepare_one_comp_unit (cu, die, cu->language);
303b6f5d 6805
f4b8a18d
KW
6806 /* The XLCL doesn't generate DW_LANG_OpenCL because this attribute is not
6807 standardised yet. As a workaround for the language detection we fall
6808 back to the DW_AT_producer string. */
6809 if (cu->producer && strstr (cu->producer, "IBM XL C for OpenCL") != NULL)
6810 cu->language = language_opencl;
6811
3019eac3
DE
6812 /* Similar hack for Go. */
6813 if (cu->producer && strstr (cu->producer, "GNU Go ") != NULL)
6814 set_cu_language (DW_LANG_Go, cu);
6815
6816 /* We assume that we're processing GCC output. */
6817 processing_gcc_compilation = 2;
6818
6819 processing_has_namespace_info = 0;
6820
6821 start_symtab (name, comp_dir, lowpc);
6822 record_debugformat ("DWARF 2");
6823 record_producer (cu->producer);
6824
6825 /* Decode line number information if present. We do this before
6826 processing child DIEs, so that the line header table is available
6827 for DW_AT_decl_file. */
6828 handle_DW_AT_stmt_list (die, cu, comp_dir, 1);
6829
6830 /* Process all dies in compilation unit. */
6831 if (die->child != NULL)
6832 {
6833 child_die = die->child;
6834 while (child_die && child_die->tag)
6835 {
6836 process_die (child_die, cu);
6837 child_die = sibling_die (child_die);
6838 }
6839 }
6840
6841 /* Decode macro information, if present. Dwarf 2 macro information
6842 refers to information in the line number info statement program
6843 header, so we can only read it if we've read the header
6844 successfully. */
6845 attr = dwarf2_attr (die, DW_AT_GNU_macros, cu);
6846 if (attr && cu->line_header)
6847 {
6848 if (dwarf2_attr (die, DW_AT_macro_info, cu))
6849 complaint (&symfile_complaints,
6850 _("CU refers to both DW_AT_GNU_macros and DW_AT_macro_info"));
6851
09262596 6852 dwarf_decode_macros (cu, DW_UNSND (attr), comp_dir, 1);
3019eac3
DE
6853 }
6854 else
6855 {
6856 attr = dwarf2_attr (die, DW_AT_macro_info, cu);
6857 if (attr && cu->line_header)
6858 {
6859 unsigned int macro_offset = DW_UNSND (attr);
6860
09262596 6861 dwarf_decode_macros (cu, macro_offset, comp_dir, 0);
3019eac3
DE
6862 }
6863 }
6864
6865 do_cleanups (back_to);
6866}
6867
6868/* Process DW_TAG_type_unit.
6869 For TUs we want to skip the first top level sibling if it's not the
6870 actual type being defined by this TU. In this case the first top
6871 level sibling is there to provide context only. */
6872
6873static void
6874read_type_unit_scope (struct die_info *die, struct dwarf2_cu *cu)
6875{
6876 struct objfile *objfile = cu->objfile;
6877 struct cleanup *back_to = make_cleanup (null_cleanup, 0);
6878 CORE_ADDR lowpc;
6879 struct attribute *attr;
6880 char *name = NULL;
6881 char *comp_dir = NULL;
6882 struct die_info *child_die;
6883 bfd *abfd = objfile->obfd;
6884
6885 /* start_symtab needs a low pc, but we don't really have one.
6886 Do what read_file_scope would do in the absence of such info. */
6887 lowpc = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
6888
6889 /* Find the filename. Do not use dwarf2_name here, since the filename
6890 is not a source language identifier. */
6891 attr = dwarf2_attr (die, DW_AT_name, cu);
6892 if (attr)
6893 name = DW_STRING (attr);
6894
6895 attr = dwarf2_attr (die, DW_AT_comp_dir, cu);
6896 if (attr)
6897 comp_dir = DW_STRING (attr);
6898 else if (name != NULL && IS_ABSOLUTE_PATH (name))
6899 {
6900 comp_dir = ldirname (name);
6901 if (comp_dir != NULL)
6902 make_cleanup (xfree, comp_dir);
6903 }
6904
6905 if (name == NULL)
6906 name = "<unknown>";
6907
95554aad 6908 prepare_one_comp_unit (cu, die, language_minimal);
3019eac3
DE
6909
6910 /* We assume that we're processing GCC output. */
6911 processing_gcc_compilation = 2;
6912
6913 processing_has_namespace_info = 0;
6914
6915 start_symtab (name, comp_dir, lowpc);
6916 record_debugformat ("DWARF 2");
6917 record_producer (cu->producer);
6918
6919 /* Decode line number information if present. We do this before
6920 processing child DIEs, so that the line header table is available
6921 for DW_AT_decl_file.
6922 We don't need the pc/line-number mapping for type units. */
6923 handle_DW_AT_stmt_list (die, cu, comp_dir, 0);
6924
6925 /* Process the dies in the type unit. */
6926 if (die->child == NULL)
6927 {
6928 dump_die_for_error (die);
6929 error (_("Dwarf Error: Missing children for type unit [in module %s]"),
6930 bfd_get_filename (abfd));
6931 }
6932
6933 child_die = die->child;
6934
6935 while (child_die && child_die->tag)
6936 {
6937 process_die (child_die, cu);
6938
6939 child_die = sibling_die (child_die);
6940 }
6941
6942 do_cleanups (back_to);
6943}
6944\f
6945/* DWO files. */
6946
6947static hashval_t
6948hash_dwo_file (const void *item)
6949{
6950 const struct dwo_file *dwo_file = item;
6951
6952 return htab_hash_string (dwo_file->dwo_name);
6953}
6954
6955static int
6956eq_dwo_file (const void *item_lhs, const void *item_rhs)
6957{
6958 const struct dwo_file *lhs = item_lhs;
6959 const struct dwo_file *rhs = item_rhs;
6960
6961 return strcmp (lhs->dwo_name, rhs->dwo_name) == 0;
6962}
6963
6964/* Allocate a hash table for DWO files. */
6965
6966static htab_t
6967allocate_dwo_file_hash_table (void)
6968{
6969 struct objfile *objfile = dwarf2_per_objfile->objfile;
6970
6971 return htab_create_alloc_ex (41,
6972 hash_dwo_file,
6973 eq_dwo_file,
6974 NULL,
6975 &objfile->objfile_obstack,
6976 hashtab_obstack_allocate,
6977 dummy_obstack_deallocate);
6978}
6979
6980static hashval_t
6981hash_dwo_unit (const void *item)
6982{
6983 const struct dwo_unit *dwo_unit = item;
6984
6985 /* This drops the top 32 bits of the id, but is ok for a hash. */
6986 return dwo_unit->signature;
6987}
6988
6989static int
6990eq_dwo_unit (const void *item_lhs, const void *item_rhs)
6991{
6992 const struct dwo_unit *lhs = item_lhs;
6993 const struct dwo_unit *rhs = item_rhs;
6994
6995 /* The signature is assumed to be unique within the DWO file.
6996 So while object file CU dwo_id's always have the value zero,
6997 that's OK, assuming each object file DWO file has only one CU,
6998 and that's the rule for now. */
6999 return lhs->signature == rhs->signature;
7000}
7001
7002/* Allocate a hash table for DWO CUs,TUs.
7003 There is one of these tables for each of CUs,TUs for each DWO file. */
7004
7005static htab_t
7006allocate_dwo_unit_table (struct objfile *objfile)
7007{
7008 /* Start out with a pretty small number.
7009 Generally DWO files contain only one CU and maybe some TUs. */
7010 return htab_create_alloc_ex (3,
7011 hash_dwo_unit,
7012 eq_dwo_unit,
7013 NULL,
7014 &objfile->objfile_obstack,
7015 hashtab_obstack_allocate,
7016 dummy_obstack_deallocate);
7017}
7018
7019/* This function is mapped across the sections and remembers the offset and
7020 size of each of the DWO debugging sections we are interested in. */
7021
7022static void
7023dwarf2_locate_dwo_sections (bfd *abfd, asection *sectp, void *dwo_file_ptr)
7024{
7025 struct dwo_file *dwo_file = dwo_file_ptr;
7026 const struct dwo_section_names *names = &dwo_section_names;
7027
7028 if (section_is_p (sectp->name, &names->abbrev_dwo))
7029 {
7030 dwo_file->sections.abbrev.asection = sectp;
7031 dwo_file->sections.abbrev.size = bfd_get_section_size (sectp);
7032 }
7033 else if (section_is_p (sectp->name, &names->info_dwo))
7034 {
7035 dwo_file->sections.info.asection = sectp;
7036 dwo_file->sections.info.size = bfd_get_section_size (sectp);
7037 }
7038 else if (section_is_p (sectp->name, &names->line_dwo))
7039 {
7040 dwo_file->sections.line.asection = sectp;
7041 dwo_file->sections.line.size = bfd_get_section_size (sectp);
7042 }
7043 else if (section_is_p (sectp->name, &names->loc_dwo))
7044 {
7045 dwo_file->sections.loc.asection = sectp;
7046 dwo_file->sections.loc.size = bfd_get_section_size (sectp);
7047 }
09262596
DE
7048 else if (section_is_p (sectp->name, &names->macinfo_dwo))
7049 {
7050 dwo_file->sections.macinfo.asection = sectp;
7051 dwo_file->sections.macinfo.size = bfd_get_section_size (sectp);
7052 }
7053 else if (section_is_p (sectp->name, &names->macro_dwo))
7054 {
7055 dwo_file->sections.macro.asection = sectp;
7056 dwo_file->sections.macro.size = bfd_get_section_size (sectp);
7057 }
3019eac3
DE
7058 else if (section_is_p (sectp->name, &names->str_dwo))
7059 {
7060 dwo_file->sections.str.asection = sectp;
7061 dwo_file->sections.str.size = bfd_get_section_size (sectp);
7062 }
7063 else if (section_is_p (sectp->name, &names->str_offsets_dwo))
7064 {
7065 dwo_file->sections.str_offsets.asection = sectp;
7066 dwo_file->sections.str_offsets.size = bfd_get_section_size (sectp);
7067 }
7068 else if (section_is_p (sectp->name, &names->types_dwo))
7069 {
7070 struct dwarf2_section_info type_section;
7071
7072 memset (&type_section, 0, sizeof (type_section));
7073 type_section.asection = sectp;
7074 type_section.size = bfd_get_section_size (sectp);
7075 VEC_safe_push (dwarf2_section_info_def, dwo_file->sections.types,
7076 &type_section);
7077 }
7078}
7079
7080/* Structure used to pass data to create_debug_info_hash_table_reader. */
7081
7082struct create_dwo_info_table_data
7083{
7084 struct dwo_file *dwo_file;
7085 htab_t cu_htab;
7086};
7087
7088/* die_reader_func for create_debug_info_hash_table. */
7089
7090static void
7091create_debug_info_hash_table_reader (const struct die_reader_specs *reader,
7092 gdb_byte *info_ptr,
7093 struct die_info *comp_unit_die,
7094 int has_children,
7095 void *datap)
7096{
7097 struct dwarf2_cu *cu = reader->cu;
7098 struct objfile *objfile = dwarf2_per_objfile->objfile;
7099 sect_offset offset = cu->per_cu->offset;
7100 struct dwarf2_section_info *section = cu->per_cu->info_or_types_section;
7101 struct create_dwo_info_table_data *data = datap;
7102 struct dwo_file *dwo_file = data->dwo_file;
7103 htab_t cu_htab = data->cu_htab;
7104 void **slot;
7105 struct attribute *attr;
7106 struct dwo_unit *dwo_unit;
7107
7108 attr = dwarf2_attr (comp_unit_die, DW_AT_GNU_dwo_id, cu);
7109 if (attr == NULL)
7110 {
7111 error (_("Dwarf Error: debug entry at offset 0x%x is missing"
7112 " its dwo_id [in module %s]"),
7113 offset.sect_off, dwo_file->dwo_name);
7114 return;
7115 }
7116
7117 dwo_unit = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct dwo_unit);
7118 dwo_unit->dwo_file = dwo_file;
7119 dwo_unit->signature = DW_UNSND (attr);
7120 dwo_unit->info_or_types_section = section;
7121 dwo_unit->offset = offset;
7122 dwo_unit->length = cu->per_cu->length;
7123
7124 slot = htab_find_slot (cu_htab, dwo_unit, INSERT);
7125 gdb_assert (slot != NULL);
7126 if (*slot != NULL)
7127 {
7128 const struct dwo_unit *dup_dwo_unit = *slot;
7129
7130 complaint (&symfile_complaints,
7131 _("debug entry at offset 0x%x is duplicate to the entry at"
7132 " offset 0x%x, dwo_id 0x%s [in module %s]"),
7133 offset.sect_off, dup_dwo_unit->offset.sect_off,
7134 phex (dwo_unit->signature, sizeof (dwo_unit->signature)),
7135 dwo_file->dwo_name);
7136 }
7137 else
7138 *slot = dwo_unit;
7139
7140 if (dwarf2_die_debug)
7141 fprintf_unfiltered (gdb_stdlog, " offset 0x%x, dwo_id 0x%s\n",
7142 offset.sect_off,
7143 phex (dwo_unit->signature,
7144 sizeof (dwo_unit->signature)));
7145}
7146
7147/* Create a hash table to map DWO IDs to their CU entry in .debug_info.dwo. */
7148
7149static htab_t
7150create_debug_info_hash_table (struct dwo_file *dwo_file)
7151{
7152 struct objfile *objfile = dwarf2_per_objfile->objfile;
7153 struct dwarf2_section_info *section = &dwo_file->sections.info;
7154 bfd *abfd;
7155 htab_t cu_htab;
7156 gdb_byte *info_ptr, *end_ptr;
7157 struct create_dwo_info_table_data create_dwo_info_table_data;
7158
7159 dwarf2_read_section (objfile, section);
7160 info_ptr = section->buffer;
7161
7162 if (info_ptr == NULL)
7163 return NULL;
7164
7165 /* We can't set abfd until now because the section may be empty or
7166 not present, in which case section->asection will be NULL. */
7167 abfd = section->asection->owner;
7168
7169 if (dwarf2_die_debug)
7170 fprintf_unfiltered (gdb_stdlog, "Reading .debug_info.dwo for %s:\n",
7171 bfd_get_filename (abfd));
7172
7173 cu_htab = allocate_dwo_unit_table (objfile);
7174
7175 create_dwo_info_table_data.dwo_file = dwo_file;
7176 create_dwo_info_table_data.cu_htab = cu_htab;
7177
7178 end_ptr = info_ptr + section->size;
7179 while (info_ptr < end_ptr)
7180 {
7181 struct dwarf2_per_cu_data per_cu;
7182
7183 memset (&per_cu, 0, sizeof (per_cu));
7184 per_cu.objfile = objfile;
7185 per_cu.is_debug_types = 0;
7186 per_cu.offset.sect_off = info_ptr - section->buffer;
7187 per_cu.info_or_types_section = section;
7188
7189 init_cutu_and_read_dies_no_follow (&per_cu,
7190 &dwo_file->sections.abbrev,
7191 dwo_file,
7192 create_debug_info_hash_table_reader,
7193 &create_dwo_info_table_data);
7194
7195 info_ptr += per_cu.length;
7196 }
7197
7198 return cu_htab;
7199}
7200
7201/* Subroutine of open_dwo_file to simplify it.
7202 Open the file specified by FILE_NAME and hand it off to BFD for
7203 preliminary analysis. Return a newly initialized bfd *, which
7204 includes a canonicalized copy of FILE_NAME.
7205 In case of trouble, return NULL.
7206 NOTE: This function is derived from symfile_bfd_open. */
7207
7208static bfd *
7209try_open_dwo_file (const char *file_name)
7210{
7211 bfd *sym_bfd;
7212 int desc;
7213 char *absolute_name;
3019eac3
DE
7214
7215 desc = openp (debug_file_directory, OPF_TRY_CWD_FIRST, file_name,
7216 O_RDONLY | O_BINARY, &absolute_name);
7217 if (desc < 0)
7218 return NULL;
7219
7220 sym_bfd = bfd_fopen (absolute_name, gnutarget, FOPEN_RB, desc);
7221 if (!sym_bfd)
7222 {
3019eac3
DE
7223 xfree (absolute_name);
7224 return NULL;
7225 }
7226 bfd_set_cacheable (sym_bfd, 1);
7227
7228 if (!bfd_check_format (sym_bfd, bfd_object))
7229 {
7230 bfd_close (sym_bfd); /* This also closes desc. */
7231 xfree (absolute_name);
7232 return NULL;
7233 }
7234
7235 /* bfd_usrdata exists for applications and libbfd must not touch it. */
7236 gdb_assert (bfd_usrdata (sym_bfd) == NULL);
7237
7238 return sym_bfd;
7239}
7240
7241/* Try to open DWO file DWO_NAME.
7242 COMP_DIR is the DW_AT_comp_dir attribute.
7243 The result is the bfd handle of the file.
7244 If there is a problem finding or opening the file, return NULL.
7245 Upon success, the canonicalized path of the file is stored in the bfd,
7246 same as symfile_bfd_open. */
7247
7248static bfd *
7249open_dwo_file (const char *dwo_name, const char *comp_dir)
7250{
7251 bfd *abfd;
3019eac3
DE
7252
7253 if (IS_ABSOLUTE_PATH (dwo_name))
7254 return try_open_dwo_file (dwo_name);
7255
7256 /* Before trying the search path, try DWO_NAME in COMP_DIR. */
7257
7258 if (comp_dir != NULL)
7259 {
7260 char *path_to_try = concat (comp_dir, SLASH_STRING, dwo_name, NULL);
7261
7262 /* NOTE: If comp_dir is a relative path, this will also try the
7263 search path, which seems useful. */
7264 abfd = try_open_dwo_file (path_to_try);
7265 xfree (path_to_try);
7266 if (abfd != NULL)
7267 return abfd;
7268 }
7269
7270 /* That didn't work, try debug-file-directory, which, despite its name,
7271 is a list of paths. */
7272
7273 if (*debug_file_directory == '\0')
7274 return NULL;
7275
7276 return try_open_dwo_file (dwo_name);
7277}
7278
7279/* Initialize the use of the DWO file specified by DWO_NAME. */
7280
7281static struct dwo_file *
7282init_dwo_file (const char *dwo_name, const char *comp_dir)
7283{
7284 struct objfile *objfile = dwarf2_per_objfile->objfile;
7285 struct dwo_file *dwo_file = OBSTACK_ZALLOC (&objfile->objfile_obstack,
7286 struct dwo_file);
7287 bfd *abfd;
7288 struct cleanup *cleanups;
7289
7290 if (dwarf2_die_debug)
7291 fprintf_unfiltered (gdb_stdlog, "Reading DWO file %s:\n", dwo_name);
7292
7293 abfd = open_dwo_file (dwo_name, comp_dir);
7294 if (abfd == NULL)
7295 return NULL;
7296 dwo_file->dwo_name = dwo_name;
7297 dwo_file->dwo_bfd = abfd;
7298
7299 cleanups = make_cleanup (free_dwo_file_cleanup, dwo_file);
7300
7301 bfd_map_over_sections (abfd, dwarf2_locate_dwo_sections, dwo_file);
7302
7303 dwo_file->cus = create_debug_info_hash_table (dwo_file);
7304
7305 dwo_file->tus = create_debug_types_hash_table (dwo_file,
7306 dwo_file->sections.types);
7307
7308 discard_cleanups (cleanups);
7309
7310 return dwo_file;
7311}
7312
7313/* Lookup DWO file DWO_NAME. */
7314
7315static struct dwo_file *
7316lookup_dwo_file (char *dwo_name, const char *comp_dir)
7317{
7318 struct dwo_file *dwo_file;
7319 struct dwo_file find_entry;
7320 void **slot;
7321
7322 if (dwarf2_per_objfile->dwo_files == NULL)
7323 dwarf2_per_objfile->dwo_files = allocate_dwo_file_hash_table ();
7324
7325 /* Have we already seen this DWO file? */
7326 find_entry.dwo_name = dwo_name;
7327 slot = htab_find_slot (dwarf2_per_objfile->dwo_files, &find_entry, INSERT);
7328
7329 /* If not, read it in and build a table of the DWOs it contains. */
7330 if (*slot == NULL)
7331 *slot = init_dwo_file (dwo_name, comp_dir);
7332
7333 /* NOTE: This will be NULL if unable to open the file. */
7334 dwo_file = *slot;
7335
7336 return dwo_file;
7337}
7338
7339/* Lookup the DWO CU referenced from THIS_CU in DWO file DWO_NAME.
7340 If non-NULL, comp_dir is the DW_AT_comp_dir attribute.
7341 SIGNATURE is the "dwo_id" of the CU (for consistency we use the same
7342 nomenclature as TUs).
1c658ad5 7343 The result is a pointer to the dwo_unit object or NULL if we didn't find it
3019eac3
DE
7344 (dwo_id mismatch or couldn't find the DWO file). */
7345
7346static struct dwo_unit *
7347lookup_dwo_comp_unit (struct dwarf2_per_cu_data *this_cu,
7348 char *dwo_name, const char *comp_dir,
7349 ULONGEST signature)
7350{
7351 struct objfile *objfile = dwarf2_per_objfile->objfile;
7352 struct dwo_file *dwo_file;
7353
7354 dwo_file = lookup_dwo_file (dwo_name, comp_dir);
7355 if (dwo_file == NULL)
7356 return NULL;
7357
7358 /* Look up the DWO using its signature(dwo_id). */
7359
7360 if (dwo_file->cus != NULL)
7361 {
7362 struct dwo_unit find_dwo_cu, *dwo_cu;
7363
7364 find_dwo_cu.signature = signature;
7365 dwo_cu = htab_find (dwo_file->cus, &find_dwo_cu);
a766d390 7366
3019eac3
DE
7367 if (dwo_cu != NULL)
7368 return dwo_cu;
7369 }
c906108c 7370
3019eac3 7371 /* We didn't find it. This must mean a dwo_id mismatch. */
df8a16a1 7372
3019eac3
DE
7373 complaint (&symfile_complaints,
7374 _("Could not find DWO CU referenced by CU at offset 0x%x"
7375 " [in module %s]"),
7376 this_cu->offset.sect_off, objfile->name);
7377 return NULL;
7378}
c906108c 7379
3019eac3
DE
7380/* Lookup the DWO TU referenced from THIS_TU in DWO file DWO_NAME.
7381 If non-NULL, comp_dir is the DW_AT_comp_dir attribute.
1c658ad5 7382 The result is a pointer to the dwo_unit object or NULL if we didn't find it
3019eac3 7383 (dwo_id mismatch or couldn't find the DWO file). */
debd256d 7384
3019eac3
DE
7385static struct dwo_unit *
7386lookup_dwo_type_unit (struct signatured_type *this_tu,
7387 char *dwo_name, const char *comp_dir)
7388{
7389 struct objfile *objfile = dwarf2_per_objfile->objfile;
7390 struct dwo_file *dwo_file;
cb1df416 7391
3019eac3
DE
7392 dwo_file = lookup_dwo_file (dwo_name, comp_dir);
7393 if (dwo_file == NULL)
7394 return NULL;
cf2c3c16 7395
3019eac3
DE
7396 /* Look up the DWO using its signature(dwo_id). */
7397
7398 if (dwo_file->tus != NULL)
cf2c3c16 7399 {
3019eac3 7400 struct dwo_unit find_dwo_tu, *dwo_tu;
9a619af0 7401
3019eac3
DE
7402 find_dwo_tu.signature = this_tu->signature;
7403 dwo_tu = htab_find (dwo_file->tus, &find_dwo_tu);
7404
7405 if (dwo_tu != NULL)
7406 return dwo_tu;
2e276125 7407 }
9cdd5dbd 7408
3019eac3
DE
7409 /* We didn't find it. This must mean a dwo_id mismatch. */
7410
7411 complaint (&symfile_complaints,
7412 _("Could not find DWO TU referenced by TU at offset 0x%x"
7413 " [in module %s]"),
7414 this_tu->per_cu.offset.sect_off, objfile->name);
7415 return NULL;
5fb290d7
DJ
7416}
7417
3019eac3
DE
7418/* Free all resources associated with DWO_FILE.
7419 Close the DWO file and munmap the sections.
7420 All memory should be on the objfile obstack. */
348e048f
DE
7421
7422static void
3019eac3 7423free_dwo_file (struct dwo_file *dwo_file, struct objfile *objfile)
348e048f 7424{
3019eac3
DE
7425 int ix;
7426 struct dwarf2_section_info *section;
348e048f 7427
3019eac3
DE
7428 gdb_assert (dwo_file->dwo_bfd != objfile->obfd);
7429 bfd_close (dwo_file->dwo_bfd);
348e048f 7430
3019eac3
DE
7431 munmap_section_buffer (&dwo_file->sections.abbrev);
7432 munmap_section_buffer (&dwo_file->sections.info);
7433 munmap_section_buffer (&dwo_file->sections.line);
7434 munmap_section_buffer (&dwo_file->sections.loc);
7435 munmap_section_buffer (&dwo_file->sections.str);
7436 munmap_section_buffer (&dwo_file->sections.str_offsets);
348e048f 7437
3019eac3
DE
7438 for (ix = 0;
7439 VEC_iterate (dwarf2_section_info_def, dwo_file->sections.types,
7440 ix, section);
7441 ++ix)
7442 munmap_section_buffer (section);
348e048f 7443
3019eac3
DE
7444 VEC_free (dwarf2_section_info_def, dwo_file->sections.types);
7445}
348e048f 7446
3019eac3 7447/* Wrapper for free_dwo_file for use in cleanups. */
348e048f 7448
3019eac3
DE
7449static void
7450free_dwo_file_cleanup (void *arg)
7451{
7452 struct dwo_file *dwo_file = (struct dwo_file *) arg;
7453 struct objfile *objfile = dwarf2_per_objfile->objfile;
348e048f 7454
3019eac3
DE
7455 free_dwo_file (dwo_file, objfile);
7456}
348e048f 7457
3019eac3 7458/* Traversal function for free_dwo_files. */
2ab95328 7459
3019eac3
DE
7460static int
7461free_dwo_file_from_slot (void **slot, void *info)
7462{
7463 struct dwo_file *dwo_file = (struct dwo_file *) *slot;
7464 struct objfile *objfile = (struct objfile *) info;
348e048f 7465
3019eac3 7466 free_dwo_file (dwo_file, objfile);
348e048f 7467
3019eac3
DE
7468 return 1;
7469}
348e048f 7470
3019eac3 7471/* Free all resources associated with DWO_FILES. */
348e048f 7472
3019eac3
DE
7473static void
7474free_dwo_files (htab_t dwo_files, struct objfile *objfile)
7475{
7476 htab_traverse_noresize (dwo_files, free_dwo_file_from_slot, objfile);
348e048f 7477}
3019eac3
DE
7478\f
7479/* Read in various DIEs. */
348e048f 7480
d389af10
JK
7481/* qsort helper for inherit_abstract_dies. */
7482
7483static int
7484unsigned_int_compar (const void *ap, const void *bp)
7485{
7486 unsigned int a = *(unsigned int *) ap;
7487 unsigned int b = *(unsigned int *) bp;
7488
7489 return (a > b) - (b > a);
7490}
7491
7492/* DW_AT_abstract_origin inherits whole DIEs (not just their attributes).
3e43a32a
MS
7493 Inherit only the children of the DW_AT_abstract_origin DIE not being
7494 already referenced by DW_AT_abstract_origin from the children of the
7495 current DIE. */
d389af10
JK
7496
7497static void
7498inherit_abstract_dies (struct die_info *die, struct dwarf2_cu *cu)
7499{
7500 struct die_info *child_die;
7501 unsigned die_children_count;
7502 /* CU offsets which were referenced by children of the current DIE. */
b64f50a1
JK
7503 sect_offset *offsets;
7504 sect_offset *offsets_end, *offsetp;
d389af10
JK
7505 /* Parent of DIE - referenced by DW_AT_abstract_origin. */
7506 struct die_info *origin_die;
7507 /* Iterator of the ORIGIN_DIE children. */
7508 struct die_info *origin_child_die;
7509 struct cleanup *cleanups;
7510 struct attribute *attr;
cd02d79d
PA
7511 struct dwarf2_cu *origin_cu;
7512 struct pending **origin_previous_list_in_scope;
d389af10
JK
7513
7514 attr = dwarf2_attr (die, DW_AT_abstract_origin, cu);
7515 if (!attr)
7516 return;
7517
cd02d79d
PA
7518 /* Note that following die references may follow to a die in a
7519 different cu. */
7520
7521 origin_cu = cu;
7522 origin_die = follow_die_ref (die, attr, &origin_cu);
7523
7524 /* We're inheriting ORIGIN's children into the scope we'd put DIE's
7525 symbols in. */
7526 origin_previous_list_in_scope = origin_cu->list_in_scope;
7527 origin_cu->list_in_scope = cu->list_in_scope;
7528
edb3359d
DJ
7529 if (die->tag != origin_die->tag
7530 && !(die->tag == DW_TAG_inlined_subroutine
7531 && origin_die->tag == DW_TAG_subprogram))
d389af10
JK
7532 complaint (&symfile_complaints,
7533 _("DIE 0x%x and its abstract origin 0x%x have different tags"),
b64f50a1 7534 die->offset.sect_off, origin_die->offset.sect_off);
d389af10
JK
7535
7536 child_die = die->child;
7537 die_children_count = 0;
7538 while (child_die && child_die->tag)
7539 {
7540 child_die = sibling_die (child_die);
7541 die_children_count++;
7542 }
7543 offsets = xmalloc (sizeof (*offsets) * die_children_count);
7544 cleanups = make_cleanup (xfree, offsets);
7545
7546 offsets_end = offsets;
7547 child_die = die->child;
7548 while (child_die && child_die->tag)
7549 {
c38f313d
DJ
7550 /* For each CHILD_DIE, find the corresponding child of
7551 ORIGIN_DIE. If there is more than one layer of
7552 DW_AT_abstract_origin, follow them all; there shouldn't be,
7553 but GCC versions at least through 4.4 generate this (GCC PR
7554 40573). */
7555 struct die_info *child_origin_die = child_die;
cd02d79d 7556 struct dwarf2_cu *child_origin_cu = cu;
9a619af0 7557
c38f313d
DJ
7558 while (1)
7559 {
cd02d79d
PA
7560 attr = dwarf2_attr (child_origin_die, DW_AT_abstract_origin,
7561 child_origin_cu);
c38f313d
DJ
7562 if (attr == NULL)
7563 break;
cd02d79d
PA
7564 child_origin_die = follow_die_ref (child_origin_die, attr,
7565 &child_origin_cu);
c38f313d
DJ
7566 }
7567
d389af10
JK
7568 /* According to DWARF3 3.3.8.2 #3 new entries without their abstract
7569 counterpart may exist. */
c38f313d 7570 if (child_origin_die != child_die)
d389af10 7571 {
edb3359d
DJ
7572 if (child_die->tag != child_origin_die->tag
7573 && !(child_die->tag == DW_TAG_inlined_subroutine
7574 && child_origin_die->tag == DW_TAG_subprogram))
d389af10
JK
7575 complaint (&symfile_complaints,
7576 _("Child DIE 0x%x and its abstract origin 0x%x have "
b64f50a1
JK
7577 "different tags"), child_die->offset.sect_off,
7578 child_origin_die->offset.sect_off);
c38f313d
DJ
7579 if (child_origin_die->parent != origin_die)
7580 complaint (&symfile_complaints,
7581 _("Child DIE 0x%x and its abstract origin 0x%x have "
b64f50a1
JK
7582 "different parents"), child_die->offset.sect_off,
7583 child_origin_die->offset.sect_off);
c38f313d
DJ
7584 else
7585 *offsets_end++ = child_origin_die->offset;
d389af10
JK
7586 }
7587 child_die = sibling_die (child_die);
7588 }
7589 qsort (offsets, offsets_end - offsets, sizeof (*offsets),
7590 unsigned_int_compar);
7591 for (offsetp = offsets + 1; offsetp < offsets_end; offsetp++)
b64f50a1 7592 if (offsetp[-1].sect_off == offsetp->sect_off)
3e43a32a
MS
7593 complaint (&symfile_complaints,
7594 _("Multiple children of DIE 0x%x refer "
7595 "to DIE 0x%x as their abstract origin"),
b64f50a1 7596 die->offset.sect_off, offsetp->sect_off);
d389af10
JK
7597
7598 offsetp = offsets;
7599 origin_child_die = origin_die->child;
7600 while (origin_child_die && origin_child_die->tag)
7601 {
7602 /* Is ORIGIN_CHILD_DIE referenced by any of the DIE children? */
b64f50a1
JK
7603 while (offsetp < offsets_end
7604 && offsetp->sect_off < origin_child_die->offset.sect_off)
d389af10 7605 offsetp++;
b64f50a1
JK
7606 if (offsetp >= offsets_end
7607 || offsetp->sect_off > origin_child_die->offset.sect_off)
d389af10
JK
7608 {
7609 /* Found that ORIGIN_CHILD_DIE is really not referenced. */
cd02d79d 7610 process_die (origin_child_die, origin_cu);
d389af10
JK
7611 }
7612 origin_child_die = sibling_die (origin_child_die);
7613 }
cd02d79d 7614 origin_cu->list_in_scope = origin_previous_list_in_scope;
d389af10
JK
7615
7616 do_cleanups (cleanups);
7617}
7618
c906108c 7619static void
e7c27a73 7620read_func_scope (struct die_info *die, struct dwarf2_cu *cu)
c906108c 7621{
e7c27a73 7622 struct objfile *objfile = cu->objfile;
52f0bd74 7623 struct context_stack *new;
c906108c
SS
7624 CORE_ADDR lowpc;
7625 CORE_ADDR highpc;
7626 struct die_info *child_die;
edb3359d 7627 struct attribute *attr, *call_line, *call_file;
c906108c 7628 char *name;
e142c38c 7629 CORE_ADDR baseaddr;
801e3a5b 7630 struct block *block;
edb3359d 7631 int inlined_func = (die->tag == DW_TAG_inlined_subroutine);
34eaf542
TT
7632 VEC (symbolp) *template_args = NULL;
7633 struct template_symbol *templ_func = NULL;
edb3359d
DJ
7634
7635 if (inlined_func)
7636 {
7637 /* If we do not have call site information, we can't show the
7638 caller of this inlined function. That's too confusing, so
7639 only use the scope for local variables. */
7640 call_line = dwarf2_attr (die, DW_AT_call_line, cu);
7641 call_file = dwarf2_attr (die, DW_AT_call_file, cu);
7642 if (call_line == NULL || call_file == NULL)
7643 {
7644 read_lexical_block_scope (die, cu);
7645 return;
7646 }
7647 }
c906108c 7648
e142c38c
DJ
7649 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
7650
94af9270 7651 name = dwarf2_name (die, cu);
c906108c 7652
e8d05480
JB
7653 /* Ignore functions with missing or empty names. These are actually
7654 illegal according to the DWARF standard. */
7655 if (name == NULL)
7656 {
7657 complaint (&symfile_complaints,
b64f50a1
JK
7658 _("missing name for subprogram DIE at %d"),
7659 die->offset.sect_off);
e8d05480
JB
7660 return;
7661 }
7662
7663 /* Ignore functions with missing or invalid low and high pc attributes. */
7664 if (!dwarf2_get_pc_bounds (die, &lowpc, &highpc, cu, NULL))
7665 {
ae4d0c03
PM
7666 attr = dwarf2_attr (die, DW_AT_external, cu);
7667 if (!attr || !DW_UNSND (attr))
7668 complaint (&symfile_complaints,
3e43a32a
MS
7669 _("cannot get low and high bounds "
7670 "for subprogram DIE at %d"),
b64f50a1 7671 die->offset.sect_off);
e8d05480
JB
7672 return;
7673 }
c906108c
SS
7674
7675 lowpc += baseaddr;
7676 highpc += baseaddr;
7677
34eaf542
TT
7678 /* If we have any template arguments, then we must allocate a
7679 different sort of symbol. */
7680 for (child_die = die->child; child_die; child_die = sibling_die (child_die))
7681 {
7682 if (child_die->tag == DW_TAG_template_type_param
7683 || child_die->tag == DW_TAG_template_value_param)
7684 {
7685 templ_func = OBSTACK_ZALLOC (&objfile->objfile_obstack,
7686 struct template_symbol);
7687 templ_func->base.is_cplus_template_function = 1;
7688 break;
7689 }
7690 }
7691
c906108c 7692 new = push_context (0, lowpc);
34eaf542
TT
7693 new->name = new_symbol_full (die, read_type_die (die, cu), cu,
7694 (struct symbol *) templ_func);
4c2df51b 7695
4cecd739
DJ
7696 /* If there is a location expression for DW_AT_frame_base, record
7697 it. */
e142c38c 7698 attr = dwarf2_attr (die, DW_AT_frame_base, cu);
4c2df51b 7699 if (attr)
c034e007
AC
7700 /* FIXME: cagney/2004-01-26: The DW_AT_frame_base's location
7701 expression is being recorded directly in the function's symbol
7702 and not in a separate frame-base object. I guess this hack is
7703 to avoid adding some sort of frame-base adjunct/annex to the
7704 function's symbol :-(. The problem with doing this is that it
7705 results in a function symbol with a location expression that
7706 has nothing to do with the location of the function, ouch! The
7707 relationship should be: a function's symbol has-a frame base; a
7708 frame-base has-a location expression. */
e7c27a73 7709 dwarf2_symbol_mark_computed (attr, new->name, cu);
4c2df51b 7710
e142c38c 7711 cu->list_in_scope = &local_symbols;
c906108c 7712
639d11d3 7713 if (die->child != NULL)
c906108c 7714 {
639d11d3 7715 child_die = die->child;
c906108c
SS
7716 while (child_die && child_die->tag)
7717 {
34eaf542
TT
7718 if (child_die->tag == DW_TAG_template_type_param
7719 || child_die->tag == DW_TAG_template_value_param)
7720 {
7721 struct symbol *arg = new_symbol (child_die, NULL, cu);
7722
f1078f66
DJ
7723 if (arg != NULL)
7724 VEC_safe_push (symbolp, template_args, arg);
34eaf542
TT
7725 }
7726 else
7727 process_die (child_die, cu);
c906108c
SS
7728 child_die = sibling_die (child_die);
7729 }
7730 }
7731
d389af10
JK
7732 inherit_abstract_dies (die, cu);
7733
4a811a97
UW
7734 /* If we have a DW_AT_specification, we might need to import using
7735 directives from the context of the specification DIE. See the
7736 comment in determine_prefix. */
7737 if (cu->language == language_cplus
7738 && dwarf2_attr (die, DW_AT_specification, cu))
7739 {
7740 struct dwarf2_cu *spec_cu = cu;
7741 struct die_info *spec_die = die_specification (die, &spec_cu);
7742
7743 while (spec_die)
7744 {
7745 child_die = spec_die->child;
7746 while (child_die && child_die->tag)
7747 {
7748 if (child_die->tag == DW_TAG_imported_module)
7749 process_die (child_die, spec_cu);
7750 child_die = sibling_die (child_die);
7751 }
7752
7753 /* In some cases, GCC generates specification DIEs that
7754 themselves contain DW_AT_specification attributes. */
7755 spec_die = die_specification (spec_die, &spec_cu);
7756 }
7757 }
7758
c906108c
SS
7759 new = pop_context ();
7760 /* Make a block for the local symbols within. */
801e3a5b
JB
7761 block = finish_block (new->name, &local_symbols, new->old_blocks,
7762 lowpc, highpc, objfile);
7763
df8a16a1 7764 /* For C++, set the block's scope. */
f55ee35c 7765 if (cu->language == language_cplus || cu->language == language_fortran)
df8a16a1 7766 cp_set_block_scope (new->name, block, &objfile->objfile_obstack,
0114d602 7767 determine_prefix (die, cu),
df8a16a1
DJ
7768 processing_has_namespace_info);
7769
801e3a5b
JB
7770 /* If we have address ranges, record them. */
7771 dwarf2_record_block_ranges (die, block, baseaddr, cu);
6e70227d 7772
34eaf542
TT
7773 /* Attach template arguments to function. */
7774 if (! VEC_empty (symbolp, template_args))
7775 {
7776 gdb_assert (templ_func != NULL);
7777
7778 templ_func->n_template_arguments = VEC_length (symbolp, template_args);
7779 templ_func->template_arguments
7780 = obstack_alloc (&objfile->objfile_obstack,
7781 (templ_func->n_template_arguments
7782 * sizeof (struct symbol *)));
7783 memcpy (templ_func->template_arguments,
7784 VEC_address (symbolp, template_args),
7785 (templ_func->n_template_arguments * sizeof (struct symbol *)));
7786 VEC_free (symbolp, template_args);
7787 }
7788
208d8187
JB
7789 /* In C++, we can have functions nested inside functions (e.g., when
7790 a function declares a class that has methods). This means that
7791 when we finish processing a function scope, we may need to go
7792 back to building a containing block's symbol lists. */
7793 local_symbols = new->locals;
7794 param_symbols = new->params;
27aa8d6a 7795 using_directives = new->using_directives;
208d8187 7796
921e78cf
JB
7797 /* If we've finished processing a top-level function, subsequent
7798 symbols go in the file symbol list. */
7799 if (outermost_context_p ())
e142c38c 7800 cu->list_in_scope = &file_symbols;
c906108c
SS
7801}
7802
7803/* Process all the DIES contained within a lexical block scope. Start
7804 a new scope, process the dies, and then close the scope. */
7805
7806static void
e7c27a73 7807read_lexical_block_scope (struct die_info *die, struct dwarf2_cu *cu)
c906108c 7808{
e7c27a73 7809 struct objfile *objfile = cu->objfile;
52f0bd74 7810 struct context_stack *new;
c906108c
SS
7811 CORE_ADDR lowpc, highpc;
7812 struct die_info *child_die;
e142c38c
DJ
7813 CORE_ADDR baseaddr;
7814
7815 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
c906108c
SS
7816
7817 /* Ignore blocks with missing or invalid low and high pc attributes. */
af34e669
DJ
7818 /* ??? Perhaps consider discontiguous blocks defined by DW_AT_ranges
7819 as multiple lexical blocks? Handling children in a sane way would
6e70227d 7820 be nasty. Might be easier to properly extend generic blocks to
af34e669 7821 describe ranges. */
d85a05f0 7822 if (!dwarf2_get_pc_bounds (die, &lowpc, &highpc, cu, NULL))
c906108c
SS
7823 return;
7824 lowpc += baseaddr;
7825 highpc += baseaddr;
7826
7827 push_context (0, lowpc);
639d11d3 7828 if (die->child != NULL)
c906108c 7829 {
639d11d3 7830 child_die = die->child;
c906108c
SS
7831 while (child_die && child_die->tag)
7832 {
e7c27a73 7833 process_die (child_die, cu);
c906108c
SS
7834 child_die = sibling_die (child_die);
7835 }
7836 }
7837 new = pop_context ();
7838
8540c487 7839 if (local_symbols != NULL || using_directives != NULL)
c906108c 7840 {
801e3a5b
JB
7841 struct block *block
7842 = finish_block (0, &local_symbols, new->old_blocks, new->start_addr,
7843 highpc, objfile);
7844
7845 /* Note that recording ranges after traversing children, as we
7846 do here, means that recording a parent's ranges entails
7847 walking across all its children's ranges as they appear in
7848 the address map, which is quadratic behavior.
7849
7850 It would be nicer to record the parent's ranges before
7851 traversing its children, simply overriding whatever you find
7852 there. But since we don't even decide whether to create a
7853 block until after we've traversed its children, that's hard
7854 to do. */
7855 dwarf2_record_block_ranges (die, block, baseaddr, cu);
c906108c
SS
7856 }
7857 local_symbols = new->locals;
27aa8d6a 7858 using_directives = new->using_directives;
c906108c
SS
7859}
7860
96408a79
SA
7861/* Read in DW_TAG_GNU_call_site and insert it to CU->call_site_htab. */
7862
7863static void
7864read_call_site_scope (struct die_info *die, struct dwarf2_cu *cu)
7865{
7866 struct objfile *objfile = cu->objfile;
7867 struct gdbarch *gdbarch = get_objfile_arch (objfile);
7868 CORE_ADDR pc, baseaddr;
7869 struct attribute *attr;
7870 struct call_site *call_site, call_site_local;
7871 void **slot;
7872 int nparams;
7873 struct die_info *child_die;
7874
7875 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
7876
7877 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
7878 if (!attr)
7879 {
7880 complaint (&symfile_complaints,
7881 _("missing DW_AT_low_pc for DW_TAG_GNU_call_site "
7882 "DIE 0x%x [in module %s]"),
b64f50a1 7883 die->offset.sect_off, objfile->name);
96408a79
SA
7884 return;
7885 }
7886 pc = DW_ADDR (attr) + baseaddr;
7887
7888 if (cu->call_site_htab == NULL)
7889 cu->call_site_htab = htab_create_alloc_ex (16, core_addr_hash, core_addr_eq,
7890 NULL, &objfile->objfile_obstack,
7891 hashtab_obstack_allocate, NULL);
7892 call_site_local.pc = pc;
7893 slot = htab_find_slot (cu->call_site_htab, &call_site_local, INSERT);
7894 if (*slot != NULL)
7895 {
7896 complaint (&symfile_complaints,
7897 _("Duplicate PC %s for DW_TAG_GNU_call_site "
7898 "DIE 0x%x [in module %s]"),
b64f50a1 7899 paddress (gdbarch, pc), die->offset.sect_off, objfile->name);
96408a79
SA
7900 return;
7901 }
7902
7903 /* Count parameters at the caller. */
7904
7905 nparams = 0;
7906 for (child_die = die->child; child_die && child_die->tag;
7907 child_die = sibling_die (child_die))
7908 {
7909 if (child_die->tag != DW_TAG_GNU_call_site_parameter)
7910 {
7911 complaint (&symfile_complaints,
7912 _("Tag %d is not DW_TAG_GNU_call_site_parameter in "
7913 "DW_TAG_GNU_call_site child DIE 0x%x [in module %s]"),
b64f50a1 7914 child_die->tag, child_die->offset.sect_off, objfile->name);
96408a79
SA
7915 continue;
7916 }
7917
7918 nparams++;
7919 }
7920
7921 call_site = obstack_alloc (&objfile->objfile_obstack,
7922 (sizeof (*call_site)
7923 + (sizeof (*call_site->parameter)
7924 * (nparams - 1))));
7925 *slot = call_site;
7926 memset (call_site, 0, sizeof (*call_site) - sizeof (*call_site->parameter));
7927 call_site->pc = pc;
7928
7929 if (dwarf2_flag_true_p (die, DW_AT_GNU_tail_call, cu))
7930 {
7931 struct die_info *func_die;
7932
7933 /* Skip also over DW_TAG_inlined_subroutine. */
7934 for (func_die = die->parent;
7935 func_die && func_die->tag != DW_TAG_subprogram
7936 && func_die->tag != DW_TAG_subroutine_type;
7937 func_die = func_die->parent);
7938
7939 /* DW_AT_GNU_all_call_sites is a superset
7940 of DW_AT_GNU_all_tail_call_sites. */
7941 if (func_die
7942 && !dwarf2_flag_true_p (func_die, DW_AT_GNU_all_call_sites, cu)
7943 && !dwarf2_flag_true_p (func_die, DW_AT_GNU_all_tail_call_sites, cu))
7944 {
7945 /* TYPE_TAIL_CALL_LIST is not interesting in functions where it is
7946 not complete. But keep CALL_SITE for look ups via call_site_htab,
7947 both the initial caller containing the real return address PC and
7948 the final callee containing the current PC of a chain of tail
7949 calls do not need to have the tail call list complete. But any
7950 function candidate for a virtual tail call frame searched via
7951 TYPE_TAIL_CALL_LIST must have the tail call list complete to be
7952 determined unambiguously. */
7953 }
7954 else
7955 {
7956 struct type *func_type = NULL;
7957
7958 if (func_die)
7959 func_type = get_die_type (func_die, cu);
7960 if (func_type != NULL)
7961 {
7962 gdb_assert (TYPE_CODE (func_type) == TYPE_CODE_FUNC);
7963
7964 /* Enlist this call site to the function. */
7965 call_site->tail_call_next = TYPE_TAIL_CALL_LIST (func_type);
7966 TYPE_TAIL_CALL_LIST (func_type) = call_site;
7967 }
7968 else
7969 complaint (&symfile_complaints,
7970 _("Cannot find function owning DW_TAG_GNU_call_site "
7971 "DIE 0x%x [in module %s]"),
b64f50a1 7972 die->offset.sect_off, objfile->name);
96408a79
SA
7973 }
7974 }
7975
7976 attr = dwarf2_attr (die, DW_AT_GNU_call_site_target, cu);
7977 if (attr == NULL)
7978 attr = dwarf2_attr (die, DW_AT_abstract_origin, cu);
7979 SET_FIELD_DWARF_BLOCK (call_site->target, NULL);
7980 if (!attr || (attr_form_is_block (attr) && DW_BLOCK (attr)->size == 0))
7981 /* Keep NULL DWARF_BLOCK. */;
7982 else if (attr_form_is_block (attr))
7983 {
7984 struct dwarf2_locexpr_baton *dlbaton;
7985
7986 dlbaton = obstack_alloc (&objfile->objfile_obstack, sizeof (*dlbaton));
7987 dlbaton->data = DW_BLOCK (attr)->data;
7988 dlbaton->size = DW_BLOCK (attr)->size;
7989 dlbaton->per_cu = cu->per_cu;
7990
7991 SET_FIELD_DWARF_BLOCK (call_site->target, dlbaton);
7992 }
7993 else if (is_ref_attr (attr))
7994 {
96408a79
SA
7995 struct dwarf2_cu *target_cu = cu;
7996 struct die_info *target_die;
7997
7998 target_die = follow_die_ref_or_sig (die, attr, &target_cu);
7999 gdb_assert (target_cu->objfile == objfile);
8000 if (die_is_declaration (target_die, target_cu))
8001 {
8002 const char *target_physname;
8003
8004 target_physname = dwarf2_physname (NULL, target_die, target_cu);
8005 if (target_physname == NULL)
8006 complaint (&symfile_complaints,
8007 _("DW_AT_GNU_call_site_target target DIE has invalid "
8008 "physname, for referencing DIE 0x%x [in module %s]"),
b64f50a1 8009 die->offset.sect_off, objfile->name);
96408a79
SA
8010 else
8011 SET_FIELD_PHYSNAME (call_site->target, (char *) target_physname);
8012 }
8013 else
8014 {
8015 CORE_ADDR lowpc;
8016
8017 /* DW_AT_entry_pc should be preferred. */
8018 if (!dwarf2_get_pc_bounds (target_die, &lowpc, NULL, target_cu, NULL))
8019 complaint (&symfile_complaints,
8020 _("DW_AT_GNU_call_site_target target DIE has invalid "
8021 "low pc, for referencing DIE 0x%x [in module %s]"),
b64f50a1 8022 die->offset.sect_off, objfile->name);
96408a79
SA
8023 else
8024 SET_FIELD_PHYSADDR (call_site->target, lowpc + baseaddr);
8025 }
8026 }
8027 else
8028 complaint (&symfile_complaints,
8029 _("DW_TAG_GNU_call_site DW_AT_GNU_call_site_target is neither "
8030 "block nor reference, for DIE 0x%x [in module %s]"),
b64f50a1 8031 die->offset.sect_off, objfile->name);
96408a79
SA
8032
8033 call_site->per_cu = cu->per_cu;
8034
8035 for (child_die = die->child;
8036 child_die && child_die->tag;
8037 child_die = sibling_die (child_die))
8038 {
96408a79 8039 struct call_site_parameter *parameter;
1788b2d3 8040 struct attribute *loc, *origin;
96408a79
SA
8041
8042 if (child_die->tag != DW_TAG_GNU_call_site_parameter)
8043 {
8044 /* Already printed the complaint above. */
8045 continue;
8046 }
8047
8048 gdb_assert (call_site->parameter_count < nparams);
8049 parameter = &call_site->parameter[call_site->parameter_count];
8050
1788b2d3
JK
8051 /* DW_AT_location specifies the register number or DW_AT_abstract_origin
8052 specifies DW_TAG_formal_parameter. Value of the data assumed for the
8053 register is contained in DW_AT_GNU_call_site_value. */
96408a79 8054
24c5c679 8055 loc = dwarf2_attr (child_die, DW_AT_location, cu);
1788b2d3
JK
8056 origin = dwarf2_attr (child_die, DW_AT_abstract_origin, cu);
8057 if (loc == NULL && origin != NULL && is_ref_attr (origin))
8058 {
8059 sect_offset offset;
8060
8061 parameter->kind = CALL_SITE_PARAMETER_PARAM_OFFSET;
8062 offset = dwarf2_get_ref_die_offset (origin);
8063 gdb_assert (offset.sect_off >= cu->header.offset.sect_off);
8064 parameter->u.param_offset.cu_off = (offset.sect_off
8065 - cu->header.offset.sect_off);
8066 }
8067 else if (loc == NULL || origin != NULL || !attr_form_is_block (loc))
96408a79
SA
8068 {
8069 complaint (&symfile_complaints,
8070 _("No DW_FORM_block* DW_AT_location for "
8071 "DW_TAG_GNU_call_site child DIE 0x%x [in module %s]"),
b64f50a1 8072 child_die->offset.sect_off, objfile->name);
96408a79
SA
8073 continue;
8074 }
24c5c679 8075 else
96408a79 8076 {
24c5c679
JK
8077 parameter->u.dwarf_reg = dwarf_block_to_dwarf_reg
8078 (DW_BLOCK (loc)->data, &DW_BLOCK (loc)->data[DW_BLOCK (loc)->size]);
8079 if (parameter->u.dwarf_reg != -1)
8080 parameter->kind = CALL_SITE_PARAMETER_DWARF_REG;
8081 else if (dwarf_block_to_sp_offset (gdbarch, DW_BLOCK (loc)->data,
8082 &DW_BLOCK (loc)->data[DW_BLOCK (loc)->size],
8083 &parameter->u.fb_offset))
8084 parameter->kind = CALL_SITE_PARAMETER_FB_OFFSET;
8085 else
8086 {
8087 complaint (&symfile_complaints,
8088 _("Only single DW_OP_reg or DW_OP_fbreg is supported "
8089 "for DW_FORM_block* DW_AT_location is supported for "
8090 "DW_TAG_GNU_call_site child DIE 0x%x "
8091 "[in module %s]"),
8092 child_die->offset.sect_off, objfile->name);
8093 continue;
8094 }
96408a79
SA
8095 }
8096
8097 attr = dwarf2_attr (child_die, DW_AT_GNU_call_site_value, cu);
8098 if (!attr_form_is_block (attr))
8099 {
8100 complaint (&symfile_complaints,
8101 _("No DW_FORM_block* DW_AT_GNU_call_site_value for "
8102 "DW_TAG_GNU_call_site child DIE 0x%x [in module %s]"),
b64f50a1 8103 child_die->offset.sect_off, objfile->name);
96408a79
SA
8104 continue;
8105 }
8106 parameter->value = DW_BLOCK (attr)->data;
8107 parameter->value_size = DW_BLOCK (attr)->size;
8108
8109 /* Parameters are not pre-cleared by memset above. */
8110 parameter->data_value = NULL;
8111 parameter->data_value_size = 0;
8112 call_site->parameter_count++;
8113
8114 attr = dwarf2_attr (child_die, DW_AT_GNU_call_site_data_value, cu);
8115 if (attr)
8116 {
8117 if (!attr_form_is_block (attr))
8118 complaint (&symfile_complaints,
8119 _("No DW_FORM_block* DW_AT_GNU_call_site_data_value for "
8120 "DW_TAG_GNU_call_site child DIE 0x%x [in module %s]"),
b64f50a1 8121 child_die->offset.sect_off, objfile->name);
96408a79
SA
8122 else
8123 {
8124 parameter->data_value = DW_BLOCK (attr)->data;
8125 parameter->data_value_size = DW_BLOCK (attr)->size;
8126 }
8127 }
8128 }
8129}
8130
43039443 8131/* Get low and high pc attributes from DW_AT_ranges attribute value OFFSET.
ff013f42
JK
8132 Return 1 if the attributes are present and valid, otherwise, return 0.
8133 If RANGES_PST is not NULL we should setup `objfile->psymtabs_addrmap'. */
43039443
JK
8134
8135static int
8136dwarf2_ranges_read (unsigned offset, CORE_ADDR *low_return,
ff013f42
JK
8137 CORE_ADDR *high_return, struct dwarf2_cu *cu,
8138 struct partial_symtab *ranges_pst)
43039443
JK
8139{
8140 struct objfile *objfile = cu->objfile;
8141 struct comp_unit_head *cu_header = &cu->header;
8142 bfd *obfd = objfile->obfd;
8143 unsigned int addr_size = cu_header->addr_size;
8144 CORE_ADDR mask = ~(~(CORE_ADDR)1 << (addr_size * 8 - 1));
8145 /* Base address selection entry. */
8146 CORE_ADDR base;
8147 int found_base;
8148 unsigned int dummy;
8149 gdb_byte *buffer;
8150 CORE_ADDR marker;
8151 int low_set;
8152 CORE_ADDR low = 0;
8153 CORE_ADDR high = 0;
ff013f42 8154 CORE_ADDR baseaddr;
43039443 8155
d00adf39
DE
8156 found_base = cu->base_known;
8157 base = cu->base_address;
43039443 8158
be391dca 8159 dwarf2_read_section (objfile, &dwarf2_per_objfile->ranges);
dce234bc 8160 if (offset >= dwarf2_per_objfile->ranges.size)
43039443
JK
8161 {
8162 complaint (&symfile_complaints,
8163 _("Offset %d out of bounds for DW_AT_ranges attribute"),
8164 offset);
8165 return 0;
8166 }
dce234bc 8167 buffer = dwarf2_per_objfile->ranges.buffer + offset;
43039443
JK
8168
8169 /* Read in the largest possible address. */
8170 marker = read_address (obfd, buffer, cu, &dummy);
8171 if ((marker & mask) == mask)
8172 {
8173 /* If we found the largest possible address, then
8174 read the base address. */
8175 base = read_address (obfd, buffer + addr_size, cu, &dummy);
8176 buffer += 2 * addr_size;
8177 offset += 2 * addr_size;
8178 found_base = 1;
8179 }
8180
8181 low_set = 0;
8182
e7030f15 8183 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
ff013f42 8184
43039443
JK
8185 while (1)
8186 {
8187 CORE_ADDR range_beginning, range_end;
8188
8189 range_beginning = read_address (obfd, buffer, cu, &dummy);
8190 buffer += addr_size;
8191 range_end = read_address (obfd, buffer, cu, &dummy);
8192 buffer += addr_size;
8193 offset += 2 * addr_size;
8194
8195 /* An end of list marker is a pair of zero addresses. */
8196 if (range_beginning == 0 && range_end == 0)
8197 /* Found the end of list entry. */
8198 break;
8199
8200 /* Each base address selection entry is a pair of 2 values.
8201 The first is the largest possible address, the second is
8202 the base address. Check for a base address here. */
8203 if ((range_beginning & mask) == mask)
8204 {
8205 /* If we found the largest possible address, then
8206 read the base address. */
8207 base = read_address (obfd, buffer + addr_size, cu, &dummy);
8208 found_base = 1;
8209 continue;
8210 }
8211
8212 if (!found_base)
8213 {
8214 /* We have no valid base address for the ranges
8215 data. */
8216 complaint (&symfile_complaints,
8217 _("Invalid .debug_ranges data (no base address)"));
8218 return 0;
8219 }
8220
9277c30c
UW
8221 if (range_beginning > range_end)
8222 {
8223 /* Inverted range entries are invalid. */
8224 complaint (&symfile_complaints,
8225 _("Invalid .debug_ranges data (inverted range)"));
8226 return 0;
8227 }
8228
8229 /* Empty range entries have no effect. */
8230 if (range_beginning == range_end)
8231 continue;
8232
43039443
JK
8233 range_beginning += base;
8234 range_end += base;
8235
9277c30c 8236 if (ranges_pst != NULL)
ff013f42 8237 addrmap_set_empty (objfile->psymtabs_addrmap,
3e43a32a
MS
8238 range_beginning + baseaddr,
8239 range_end - 1 + baseaddr,
ff013f42
JK
8240 ranges_pst);
8241
43039443
JK
8242 /* FIXME: This is recording everything as a low-high
8243 segment of consecutive addresses. We should have a
8244 data structure for discontiguous block ranges
8245 instead. */
8246 if (! low_set)
8247 {
8248 low = range_beginning;
8249 high = range_end;
8250 low_set = 1;
8251 }
8252 else
8253 {
8254 if (range_beginning < low)
8255 low = range_beginning;
8256 if (range_end > high)
8257 high = range_end;
8258 }
8259 }
8260
8261 if (! low_set)
8262 /* If the first entry is an end-of-list marker, the range
8263 describes an empty scope, i.e. no instructions. */
8264 return 0;
8265
8266 if (low_return)
8267 *low_return = low;
8268 if (high_return)
8269 *high_return = high;
8270 return 1;
8271}
8272
af34e669
DJ
8273/* Get low and high pc attributes from a die. Return 1 if the attributes
8274 are present and valid, otherwise, return 0. Return -1 if the range is
8275 discontinuous, i.e. derived from DW_AT_ranges information. */
380bca97 8276
c906108c 8277static int
af34e669 8278dwarf2_get_pc_bounds (struct die_info *die, CORE_ADDR *lowpc,
d85a05f0
DJ
8279 CORE_ADDR *highpc, struct dwarf2_cu *cu,
8280 struct partial_symtab *pst)
c906108c
SS
8281{
8282 struct attribute *attr;
91da1414 8283 struct attribute *attr_high;
af34e669
DJ
8284 CORE_ADDR low = 0;
8285 CORE_ADDR high = 0;
8286 int ret = 0;
c906108c 8287
91da1414
MW
8288 attr_high = dwarf2_attr (die, DW_AT_high_pc, cu);
8289 if (attr_high)
af34e669 8290 {
e142c38c 8291 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
af34e669 8292 if (attr)
91da1414
MW
8293 {
8294 low = DW_ADDR (attr);
3019eac3
DE
8295 if (attr_high->form == DW_FORM_addr
8296 || attr_high->form == DW_FORM_GNU_addr_index)
91da1414
MW
8297 high = DW_ADDR (attr_high);
8298 else
8299 high = low + DW_UNSND (attr_high);
8300 }
af34e669
DJ
8301 else
8302 /* Found high w/o low attribute. */
8303 return 0;
8304
8305 /* Found consecutive range of addresses. */
8306 ret = 1;
8307 }
c906108c 8308 else
af34e669 8309 {
e142c38c 8310 attr = dwarf2_attr (die, DW_AT_ranges, cu);
af34e669
DJ
8311 if (attr != NULL)
8312 {
2e3cf129
DE
8313 unsigned int ranges_offset = DW_UNSND (attr) + cu->ranges_base;
8314
af34e669 8315 /* Value of the DW_AT_ranges attribute is the offset in the
a604369a 8316 .debug_ranges section. */
2e3cf129 8317 if (!dwarf2_ranges_read (ranges_offset, &low, &high, cu, pst))
af34e669 8318 return 0;
43039443 8319 /* Found discontinuous range of addresses. */
af34e669
DJ
8320 ret = -1;
8321 }
8322 }
c906108c 8323
9373cf26
JK
8324 /* read_partial_die has also the strict LOW < HIGH requirement. */
8325 if (high <= low)
c906108c
SS
8326 return 0;
8327
8328 /* When using the GNU linker, .gnu.linkonce. sections are used to
8329 eliminate duplicate copies of functions and vtables and such.
8330 The linker will arbitrarily choose one and discard the others.
8331 The AT_*_pc values for such functions refer to local labels in
8332 these sections. If the section from that file was discarded, the
8333 labels are not in the output, so the relocs get a value of 0.
8334 If this is a discarded function, mark the pc bounds as invalid,
8335 so that GDB will ignore it. */
72dca2f5 8336 if (low == 0 && !dwarf2_per_objfile->has_section_at_zero)
c906108c
SS
8337 return 0;
8338
8339 *lowpc = low;
96408a79
SA
8340 if (highpc)
8341 *highpc = high;
af34e669 8342 return ret;
c906108c
SS
8343}
8344
b084d499
JB
8345/* Assuming that DIE represents a subprogram DIE or a lexical block, get
8346 its low and high PC addresses. Do nothing if these addresses could not
8347 be determined. Otherwise, set LOWPC to the low address if it is smaller,
8348 and HIGHPC to the high address if greater than HIGHPC. */
8349
8350static void
8351dwarf2_get_subprogram_pc_bounds (struct die_info *die,
8352 CORE_ADDR *lowpc, CORE_ADDR *highpc,
8353 struct dwarf2_cu *cu)
8354{
8355 CORE_ADDR low, high;
8356 struct die_info *child = die->child;
8357
d85a05f0 8358 if (dwarf2_get_pc_bounds (die, &low, &high, cu, NULL))
b084d499
JB
8359 {
8360 *lowpc = min (*lowpc, low);
8361 *highpc = max (*highpc, high);
8362 }
8363
8364 /* If the language does not allow nested subprograms (either inside
8365 subprograms or lexical blocks), we're done. */
8366 if (cu->language != language_ada)
8367 return;
6e70227d 8368
b084d499
JB
8369 /* Check all the children of the given DIE. If it contains nested
8370 subprograms, then check their pc bounds. Likewise, we need to
8371 check lexical blocks as well, as they may also contain subprogram
8372 definitions. */
8373 while (child && child->tag)
8374 {
8375 if (child->tag == DW_TAG_subprogram
8376 || child->tag == DW_TAG_lexical_block)
8377 dwarf2_get_subprogram_pc_bounds (child, lowpc, highpc, cu);
8378 child = sibling_die (child);
8379 }
8380}
8381
fae299cd
DC
8382/* Get the low and high pc's represented by the scope DIE, and store
8383 them in *LOWPC and *HIGHPC. If the correct values can't be
8384 determined, set *LOWPC to -1 and *HIGHPC to 0. */
8385
8386static void
8387get_scope_pc_bounds (struct die_info *die,
8388 CORE_ADDR *lowpc, CORE_ADDR *highpc,
8389 struct dwarf2_cu *cu)
8390{
8391 CORE_ADDR best_low = (CORE_ADDR) -1;
8392 CORE_ADDR best_high = (CORE_ADDR) 0;
8393 CORE_ADDR current_low, current_high;
8394
d85a05f0 8395 if (dwarf2_get_pc_bounds (die, &current_low, &current_high, cu, NULL))
fae299cd
DC
8396 {
8397 best_low = current_low;
8398 best_high = current_high;
8399 }
8400 else
8401 {
8402 struct die_info *child = die->child;
8403
8404 while (child && child->tag)
8405 {
8406 switch (child->tag) {
8407 case DW_TAG_subprogram:
b084d499 8408 dwarf2_get_subprogram_pc_bounds (child, &best_low, &best_high, cu);
fae299cd
DC
8409 break;
8410 case DW_TAG_namespace:
f55ee35c 8411 case DW_TAG_module:
fae299cd
DC
8412 /* FIXME: carlton/2004-01-16: Should we do this for
8413 DW_TAG_class_type/DW_TAG_structure_type, too? I think
8414 that current GCC's always emit the DIEs corresponding
8415 to definitions of methods of classes as children of a
8416 DW_TAG_compile_unit or DW_TAG_namespace (as opposed to
8417 the DIEs giving the declarations, which could be
8418 anywhere). But I don't see any reason why the
8419 standards says that they have to be there. */
8420 get_scope_pc_bounds (child, &current_low, &current_high, cu);
8421
8422 if (current_low != ((CORE_ADDR) -1))
8423 {
8424 best_low = min (best_low, current_low);
8425 best_high = max (best_high, current_high);
8426 }
8427 break;
8428 default:
0963b4bd 8429 /* Ignore. */
fae299cd
DC
8430 break;
8431 }
8432
8433 child = sibling_die (child);
8434 }
8435 }
8436
8437 *lowpc = best_low;
8438 *highpc = best_high;
8439}
8440
801e3a5b
JB
8441/* Record the address ranges for BLOCK, offset by BASEADDR, as given
8442 in DIE. */
380bca97 8443
801e3a5b
JB
8444static void
8445dwarf2_record_block_ranges (struct die_info *die, struct block *block,
8446 CORE_ADDR baseaddr, struct dwarf2_cu *cu)
8447{
bb5ed363 8448 struct objfile *objfile = cu->objfile;
801e3a5b 8449 struct attribute *attr;
91da1414 8450 struct attribute *attr_high;
801e3a5b 8451
91da1414
MW
8452 attr_high = dwarf2_attr (die, DW_AT_high_pc, cu);
8453 if (attr_high)
801e3a5b 8454 {
801e3a5b
JB
8455 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
8456 if (attr)
8457 {
8458 CORE_ADDR low = DW_ADDR (attr);
91da1414 8459 CORE_ADDR high;
3019eac3
DE
8460 if (attr_high->form == DW_FORM_addr
8461 || attr_high->form == DW_FORM_GNU_addr_index)
91da1414
MW
8462 high = DW_ADDR (attr_high);
8463 else
8464 high = low + DW_UNSND (attr_high);
9a619af0 8465
801e3a5b
JB
8466 record_block_range (block, baseaddr + low, baseaddr + high - 1);
8467 }
8468 }
8469
8470 attr = dwarf2_attr (die, DW_AT_ranges, cu);
8471 if (attr)
8472 {
bb5ed363 8473 bfd *obfd = objfile->obfd;
801e3a5b
JB
8474
8475 /* The value of the DW_AT_ranges attribute is the offset of the
8476 address range list in the .debug_ranges section. */
2e3cf129 8477 unsigned long offset = DW_UNSND (attr) + cu->ranges_base;
dce234bc 8478 gdb_byte *buffer = dwarf2_per_objfile->ranges.buffer + offset;
801e3a5b
JB
8479
8480 /* For some target architectures, but not others, the
8481 read_address function sign-extends the addresses it returns.
8482 To recognize base address selection entries, we need a
8483 mask. */
8484 unsigned int addr_size = cu->header.addr_size;
8485 CORE_ADDR base_select_mask = ~(~(CORE_ADDR)1 << (addr_size * 8 - 1));
8486
8487 /* The base address, to which the next pair is relative. Note
8488 that this 'base' is a DWARF concept: most entries in a range
8489 list are relative, to reduce the number of relocs against the
8490 debugging information. This is separate from this function's
8491 'baseaddr' argument, which GDB uses to relocate debugging
8492 information from a shared library based on the address at
8493 which the library was loaded. */
d00adf39
DE
8494 CORE_ADDR base = cu->base_address;
8495 int base_known = cu->base_known;
801e3a5b 8496
be391dca 8497 gdb_assert (dwarf2_per_objfile->ranges.readin);
dce234bc 8498 if (offset >= dwarf2_per_objfile->ranges.size)
801e3a5b
JB
8499 {
8500 complaint (&symfile_complaints,
8501 _("Offset %lu out of bounds for DW_AT_ranges attribute"),
8502 offset);
8503 return;
8504 }
8505
8506 for (;;)
8507 {
8508 unsigned int bytes_read;
8509 CORE_ADDR start, end;
8510
8511 start = read_address (obfd, buffer, cu, &bytes_read);
8512 buffer += bytes_read;
8513 end = read_address (obfd, buffer, cu, &bytes_read);
8514 buffer += bytes_read;
8515
8516 /* Did we find the end of the range list? */
8517 if (start == 0 && end == 0)
8518 break;
8519
8520 /* Did we find a base address selection entry? */
8521 else if ((start & base_select_mask) == base_select_mask)
8522 {
8523 base = end;
8524 base_known = 1;
8525 }
8526
8527 /* We found an ordinary address range. */
8528 else
8529 {
8530 if (!base_known)
8531 {
8532 complaint (&symfile_complaints,
3e43a32a
MS
8533 _("Invalid .debug_ranges data "
8534 "(no base address)"));
801e3a5b
JB
8535 return;
8536 }
8537
9277c30c
UW
8538 if (start > end)
8539 {
8540 /* Inverted range entries are invalid. */
8541 complaint (&symfile_complaints,
8542 _("Invalid .debug_ranges data "
8543 "(inverted range)"));
8544 return;
8545 }
8546
8547 /* Empty range entries have no effect. */
8548 if (start == end)
8549 continue;
8550
6e70227d
DE
8551 record_block_range (block,
8552 baseaddr + base + start,
801e3a5b
JB
8553 baseaddr + base + end - 1);
8554 }
8555 }
8556 }
8557}
8558
685b1105
JK
8559/* Check whether the producer field indicates either of GCC < 4.6, or the
8560 Intel C/C++ compiler, and cache the result in CU. */
60d5a603 8561
685b1105
JK
8562static void
8563check_producer (struct dwarf2_cu *cu)
60d5a603
JK
8564{
8565 const char *cs;
8566 int major, minor, release;
8567
8568 if (cu->producer == NULL)
8569 {
8570 /* For unknown compilers expect their behavior is DWARF version
8571 compliant.
8572
8573 GCC started to support .debug_types sections by -gdwarf-4 since
8574 gcc-4.5.x. As the .debug_types sections are missing DW_AT_producer
8575 for their space efficiency GDB cannot workaround gcc-4.5.x -gdwarf-4
8576 combination. gcc-4.5.x -gdwarf-4 binaries have DW_AT_accessibility
8577 interpreted incorrectly by GDB now - GCC PR debug/48229. */
60d5a603 8578 }
685b1105 8579 else if (strncmp (cu->producer, "GNU ", strlen ("GNU ")) == 0)
60d5a603 8580 {
685b1105
JK
8581 /* Skip any identifier after "GNU " - such as "C++" or "Java". */
8582
ba919b58
TT
8583 cs = &cu->producer[strlen ("GNU ")];
8584 while (*cs && !isdigit (*cs))
8585 cs++;
8586 if (sscanf (cs, "%d.%d.%d", &major, &minor, &release) != 3)
8587 {
8588 /* Not recognized as GCC. */
8589 }
8590 else
685b1105
JK
8591 cu->producer_is_gxx_lt_4_6 = major < 4 || (major == 4 && minor < 6);
8592 }
8593 else if (strncmp (cu->producer, "Intel(R) C", strlen ("Intel(R) C")) == 0)
8594 cu->producer_is_icc = 1;
8595 else
8596 {
8597 /* For other non-GCC compilers, expect their behavior is DWARF version
8598 compliant. */
60d5a603
JK
8599 }
8600
ba919b58 8601 cu->checked_producer = 1;
685b1105 8602}
ba919b58 8603
685b1105
JK
8604/* Check for GCC PR debug/45124 fix which is not present in any G++ version up
8605 to 4.5.any while it is present already in G++ 4.6.0 - the PR has been fixed
8606 during 4.6.0 experimental. */
8607
8608static int
8609producer_is_gxx_lt_4_6 (struct dwarf2_cu *cu)
8610{
8611 if (!cu->checked_producer)
8612 check_producer (cu);
8613
8614 return cu->producer_is_gxx_lt_4_6;
60d5a603
JK
8615}
8616
8617/* Return the default accessibility type if it is not overriden by
8618 DW_AT_accessibility. */
8619
8620static enum dwarf_access_attribute
8621dwarf2_default_access_attribute (struct die_info *die, struct dwarf2_cu *cu)
8622{
8623 if (cu->header.version < 3 || producer_is_gxx_lt_4_6 (cu))
8624 {
8625 /* The default DWARF 2 accessibility for members is public, the default
8626 accessibility for inheritance is private. */
8627
8628 if (die->tag != DW_TAG_inheritance)
8629 return DW_ACCESS_public;
8630 else
8631 return DW_ACCESS_private;
8632 }
8633 else
8634 {
8635 /* DWARF 3+ defines the default accessibility a different way. The same
8636 rules apply now for DW_TAG_inheritance as for the members and it only
8637 depends on the container kind. */
8638
8639 if (die->parent->tag == DW_TAG_class_type)
8640 return DW_ACCESS_private;
8641 else
8642 return DW_ACCESS_public;
8643 }
8644}
8645
74ac6d43
TT
8646/* Look for DW_AT_data_member_location. Set *OFFSET to the byte
8647 offset. If the attribute was not found return 0, otherwise return
8648 1. If it was found but could not properly be handled, set *OFFSET
8649 to 0. */
8650
8651static int
8652handle_data_member_location (struct die_info *die, struct dwarf2_cu *cu,
8653 LONGEST *offset)
8654{
8655 struct attribute *attr;
8656
8657 attr = dwarf2_attr (die, DW_AT_data_member_location, cu);
8658 if (attr != NULL)
8659 {
8660 *offset = 0;
8661
8662 /* Note that we do not check for a section offset first here.
8663 This is because DW_AT_data_member_location is new in DWARF 4,
8664 so if we see it, we can assume that a constant form is really
8665 a constant and not a section offset. */
8666 if (attr_form_is_constant (attr))
8667 *offset = dwarf2_get_attr_constant_value (attr, 0);
8668 else if (attr_form_is_section_offset (attr))
8669 dwarf2_complex_location_expr_complaint ();
8670 else if (attr_form_is_block (attr))
8671 *offset = decode_locdesc (DW_BLOCK (attr), cu);
8672 else
8673 dwarf2_complex_location_expr_complaint ();
8674
8675 return 1;
8676 }
8677
8678 return 0;
8679}
8680
c906108c
SS
8681/* Add an aggregate field to the field list. */
8682
8683static void
107d2387 8684dwarf2_add_field (struct field_info *fip, struct die_info *die,
e7c27a73 8685 struct dwarf2_cu *cu)
6e70227d 8686{
e7c27a73 8687 struct objfile *objfile = cu->objfile;
5e2b427d 8688 struct gdbarch *gdbarch = get_objfile_arch (objfile);
c906108c
SS
8689 struct nextfield *new_field;
8690 struct attribute *attr;
8691 struct field *fp;
8692 char *fieldname = "";
8693
8694 /* Allocate a new field list entry and link it in. */
8695 new_field = (struct nextfield *) xmalloc (sizeof (struct nextfield));
b8c9b27d 8696 make_cleanup (xfree, new_field);
c906108c 8697 memset (new_field, 0, sizeof (struct nextfield));
7d0ccb61
DJ
8698
8699 if (die->tag == DW_TAG_inheritance)
8700 {
8701 new_field->next = fip->baseclasses;
8702 fip->baseclasses = new_field;
8703 }
8704 else
8705 {
8706 new_field->next = fip->fields;
8707 fip->fields = new_field;
8708 }
c906108c
SS
8709 fip->nfields++;
8710
e142c38c 8711 attr = dwarf2_attr (die, DW_AT_accessibility, cu);
c906108c
SS
8712 if (attr)
8713 new_field->accessibility = DW_UNSND (attr);
60d5a603
JK
8714 else
8715 new_field->accessibility = dwarf2_default_access_attribute (die, cu);
c906108c
SS
8716 if (new_field->accessibility != DW_ACCESS_public)
8717 fip->non_public_fields = 1;
60d5a603 8718
e142c38c 8719 attr = dwarf2_attr (die, DW_AT_virtuality, cu);
c906108c
SS
8720 if (attr)
8721 new_field->virtuality = DW_UNSND (attr);
60d5a603
JK
8722 else
8723 new_field->virtuality = DW_VIRTUALITY_none;
c906108c
SS
8724
8725 fp = &new_field->field;
a9a9bd0f 8726
e142c38c 8727 if (die->tag == DW_TAG_member && ! die_is_declaration (die, cu))
c906108c 8728 {
74ac6d43
TT
8729 LONGEST offset;
8730
a9a9bd0f 8731 /* Data member other than a C++ static data member. */
6e70227d 8732
c906108c 8733 /* Get type of field. */
e7c27a73 8734 fp->type = die_type (die, cu);
c906108c 8735
d6a843b5 8736 SET_FIELD_BITPOS (*fp, 0);
01ad7f36 8737
c906108c 8738 /* Get bit size of field (zero if none). */
e142c38c 8739 attr = dwarf2_attr (die, DW_AT_bit_size, cu);
c906108c
SS
8740 if (attr)
8741 {
8742 FIELD_BITSIZE (*fp) = DW_UNSND (attr);
8743 }
8744 else
8745 {
8746 FIELD_BITSIZE (*fp) = 0;
8747 }
8748
8749 /* Get bit offset of field. */
74ac6d43
TT
8750 if (handle_data_member_location (die, cu, &offset))
8751 SET_FIELD_BITPOS (*fp, offset * bits_per_byte);
e142c38c 8752 attr = dwarf2_attr (die, DW_AT_bit_offset, cu);
c906108c
SS
8753 if (attr)
8754 {
5e2b427d 8755 if (gdbarch_bits_big_endian (gdbarch))
c906108c
SS
8756 {
8757 /* For big endian bits, the DW_AT_bit_offset gives the
c5aa993b
JM
8758 additional bit offset from the MSB of the containing
8759 anonymous object to the MSB of the field. We don't
8760 have to do anything special since we don't need to
8761 know the size of the anonymous object. */
f41f5e61 8762 SET_FIELD_BITPOS (*fp, FIELD_BITPOS (*fp) + DW_UNSND (attr));
c906108c
SS
8763 }
8764 else
8765 {
8766 /* For little endian bits, compute the bit offset to the
c5aa993b
JM
8767 MSB of the anonymous object, subtract off the number of
8768 bits from the MSB of the field to the MSB of the
8769 object, and then subtract off the number of bits of
8770 the field itself. The result is the bit offset of
8771 the LSB of the field. */
c906108c
SS
8772 int anonymous_size;
8773 int bit_offset = DW_UNSND (attr);
8774
e142c38c 8775 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
c906108c
SS
8776 if (attr)
8777 {
8778 /* The size of the anonymous object containing
8779 the bit field is explicit, so use the
8780 indicated size (in bytes). */
8781 anonymous_size = DW_UNSND (attr);
8782 }
8783 else
8784 {
8785 /* The size of the anonymous object containing
8786 the bit field must be inferred from the type
8787 attribute of the data member containing the
8788 bit field. */
8789 anonymous_size = TYPE_LENGTH (fp->type);
8790 }
f41f5e61
PA
8791 SET_FIELD_BITPOS (*fp,
8792 (FIELD_BITPOS (*fp)
8793 + anonymous_size * bits_per_byte
8794 - bit_offset - FIELD_BITSIZE (*fp)));
c906108c
SS
8795 }
8796 }
8797
8798 /* Get name of field. */
39cbfefa
DJ
8799 fieldname = dwarf2_name (die, cu);
8800 if (fieldname == NULL)
8801 fieldname = "";
d8151005
DJ
8802
8803 /* The name is already allocated along with this objfile, so we don't
8804 need to duplicate it for the type. */
8805 fp->name = fieldname;
c906108c
SS
8806
8807 /* Change accessibility for artificial fields (e.g. virtual table
c5aa993b 8808 pointer or virtual base class pointer) to private. */
e142c38c 8809 if (dwarf2_attr (die, DW_AT_artificial, cu))
c906108c 8810 {
d48cc9dd 8811 FIELD_ARTIFICIAL (*fp) = 1;
c906108c
SS
8812 new_field->accessibility = DW_ACCESS_private;
8813 fip->non_public_fields = 1;
8814 }
8815 }
a9a9bd0f 8816 else if (die->tag == DW_TAG_member || die->tag == DW_TAG_variable)
c906108c 8817 {
a9a9bd0f
DC
8818 /* C++ static member. */
8819
8820 /* NOTE: carlton/2002-11-05: It should be a DW_TAG_member that
8821 is a declaration, but all versions of G++ as of this writing
8822 (so through at least 3.2.1) incorrectly generate
8823 DW_TAG_variable tags. */
6e70227d 8824
ff355380 8825 const char *physname;
c906108c 8826
a9a9bd0f 8827 /* Get name of field. */
39cbfefa
DJ
8828 fieldname = dwarf2_name (die, cu);
8829 if (fieldname == NULL)
c906108c
SS
8830 return;
8831
254e6b9e 8832 attr = dwarf2_attr (die, DW_AT_const_value, cu);
3863f96c
DE
8833 if (attr
8834 /* Only create a symbol if this is an external value.
8835 new_symbol checks this and puts the value in the global symbol
8836 table, which we want. If it is not external, new_symbol
8837 will try to put the value in cu->list_in_scope which is wrong. */
8838 && dwarf2_flag_true_p (die, DW_AT_external, cu))
254e6b9e
DE
8839 {
8840 /* A static const member, not much different than an enum as far as
8841 we're concerned, except that we can support more types. */
8842 new_symbol (die, NULL, cu);
8843 }
8844
2df3850c 8845 /* Get physical name. */
ff355380 8846 physname = dwarf2_physname (fieldname, die, cu);
c906108c 8847
d8151005
DJ
8848 /* The name is already allocated along with this objfile, so we don't
8849 need to duplicate it for the type. */
8850 SET_FIELD_PHYSNAME (*fp, physname ? physname : "");
e7c27a73 8851 FIELD_TYPE (*fp) = die_type (die, cu);
d8151005 8852 FIELD_NAME (*fp) = fieldname;
c906108c
SS
8853 }
8854 else if (die->tag == DW_TAG_inheritance)
8855 {
74ac6d43 8856 LONGEST offset;
d4b96c9a 8857
74ac6d43
TT
8858 /* C++ base class field. */
8859 if (handle_data_member_location (die, cu, &offset))
8860 SET_FIELD_BITPOS (*fp, offset * bits_per_byte);
c906108c 8861 FIELD_BITSIZE (*fp) = 0;
e7c27a73 8862 FIELD_TYPE (*fp) = die_type (die, cu);
c906108c
SS
8863 FIELD_NAME (*fp) = type_name_no_tag (fp->type);
8864 fip->nbaseclasses++;
8865 }
8866}
8867
98751a41
JK
8868/* Add a typedef defined in the scope of the FIP's class. */
8869
8870static void
8871dwarf2_add_typedef (struct field_info *fip, struct die_info *die,
8872 struct dwarf2_cu *cu)
6e70227d 8873{
98751a41 8874 struct objfile *objfile = cu->objfile;
98751a41
JK
8875 struct typedef_field_list *new_field;
8876 struct attribute *attr;
8877 struct typedef_field *fp;
8878 char *fieldname = "";
8879
8880 /* Allocate a new field list entry and link it in. */
8881 new_field = xzalloc (sizeof (*new_field));
8882 make_cleanup (xfree, new_field);
8883
8884 gdb_assert (die->tag == DW_TAG_typedef);
8885
8886 fp = &new_field->field;
8887
8888 /* Get name of field. */
8889 fp->name = dwarf2_name (die, cu);
8890 if (fp->name == NULL)
8891 return;
8892
8893 fp->type = read_type_die (die, cu);
8894
8895 new_field->next = fip->typedef_field_list;
8896 fip->typedef_field_list = new_field;
8897 fip->typedef_field_list_count++;
8898}
8899
c906108c
SS
8900/* Create the vector of fields, and attach it to the type. */
8901
8902static void
fba45db2 8903dwarf2_attach_fields_to_type (struct field_info *fip, struct type *type,
e7c27a73 8904 struct dwarf2_cu *cu)
c906108c
SS
8905{
8906 int nfields = fip->nfields;
8907
8908 /* Record the field count, allocate space for the array of fields,
8909 and create blank accessibility bitfields if necessary. */
8910 TYPE_NFIELDS (type) = nfields;
8911 TYPE_FIELDS (type) = (struct field *)
8912 TYPE_ALLOC (type, sizeof (struct field) * nfields);
8913 memset (TYPE_FIELDS (type), 0, sizeof (struct field) * nfields);
8914
b4ba55a1 8915 if (fip->non_public_fields && cu->language != language_ada)
c906108c
SS
8916 {
8917 ALLOCATE_CPLUS_STRUCT_TYPE (type);
8918
8919 TYPE_FIELD_PRIVATE_BITS (type) =
8920 (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
8921 B_CLRALL (TYPE_FIELD_PRIVATE_BITS (type), nfields);
8922
8923 TYPE_FIELD_PROTECTED_BITS (type) =
8924 (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
8925 B_CLRALL (TYPE_FIELD_PROTECTED_BITS (type), nfields);
8926
774b6a14
TT
8927 TYPE_FIELD_IGNORE_BITS (type) =
8928 (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
8929 B_CLRALL (TYPE_FIELD_IGNORE_BITS (type), nfields);
c906108c
SS
8930 }
8931
8932 /* If the type has baseclasses, allocate and clear a bit vector for
8933 TYPE_FIELD_VIRTUAL_BITS. */
b4ba55a1 8934 if (fip->nbaseclasses && cu->language != language_ada)
c906108c
SS
8935 {
8936 int num_bytes = B_BYTES (fip->nbaseclasses);
fe1b8b76 8937 unsigned char *pointer;
c906108c
SS
8938
8939 ALLOCATE_CPLUS_STRUCT_TYPE (type);
fe1b8b76
JB
8940 pointer = TYPE_ALLOC (type, num_bytes);
8941 TYPE_FIELD_VIRTUAL_BITS (type) = pointer;
c906108c
SS
8942 B_CLRALL (TYPE_FIELD_VIRTUAL_BITS (type), fip->nbaseclasses);
8943 TYPE_N_BASECLASSES (type) = fip->nbaseclasses;
8944 }
8945
3e43a32a
MS
8946 /* Copy the saved-up fields into the field vector. Start from the head of
8947 the list, adding to the tail of the field array, so that they end up in
8948 the same order in the array in which they were added to the list. */
c906108c
SS
8949 while (nfields-- > 0)
8950 {
7d0ccb61
DJ
8951 struct nextfield *fieldp;
8952
8953 if (fip->fields)
8954 {
8955 fieldp = fip->fields;
8956 fip->fields = fieldp->next;
8957 }
8958 else
8959 {
8960 fieldp = fip->baseclasses;
8961 fip->baseclasses = fieldp->next;
8962 }
8963
8964 TYPE_FIELD (type, nfields) = fieldp->field;
8965 switch (fieldp->accessibility)
c906108c 8966 {
c5aa993b 8967 case DW_ACCESS_private:
b4ba55a1
JB
8968 if (cu->language != language_ada)
8969 SET_TYPE_FIELD_PRIVATE (type, nfields);
c5aa993b 8970 break;
c906108c 8971
c5aa993b 8972 case DW_ACCESS_protected:
b4ba55a1
JB
8973 if (cu->language != language_ada)
8974 SET_TYPE_FIELD_PROTECTED (type, nfields);
c5aa993b 8975 break;
c906108c 8976
c5aa993b
JM
8977 case DW_ACCESS_public:
8978 break;
c906108c 8979
c5aa993b
JM
8980 default:
8981 /* Unknown accessibility. Complain and treat it as public. */
8982 {
e2e0b3e5 8983 complaint (&symfile_complaints, _("unsupported accessibility %d"),
7d0ccb61 8984 fieldp->accessibility);
c5aa993b
JM
8985 }
8986 break;
c906108c
SS
8987 }
8988 if (nfields < fip->nbaseclasses)
8989 {
7d0ccb61 8990 switch (fieldp->virtuality)
c906108c 8991 {
c5aa993b
JM
8992 case DW_VIRTUALITY_virtual:
8993 case DW_VIRTUALITY_pure_virtual:
b4ba55a1 8994 if (cu->language == language_ada)
a73c6dcd 8995 error (_("unexpected virtuality in component of Ada type"));
c5aa993b
JM
8996 SET_TYPE_FIELD_VIRTUAL (type, nfields);
8997 break;
c906108c
SS
8998 }
8999 }
c906108c
SS
9000 }
9001}
9002
c906108c
SS
9003/* Add a member function to the proper fieldlist. */
9004
9005static void
107d2387 9006dwarf2_add_member_fn (struct field_info *fip, struct die_info *die,
e7c27a73 9007 struct type *type, struct dwarf2_cu *cu)
c906108c 9008{
e7c27a73 9009 struct objfile *objfile = cu->objfile;
c906108c
SS
9010 struct attribute *attr;
9011 struct fnfieldlist *flp;
9012 int i;
9013 struct fn_field *fnp;
9014 char *fieldname;
c906108c 9015 struct nextfnfield *new_fnfield;
f792889a 9016 struct type *this_type;
60d5a603 9017 enum dwarf_access_attribute accessibility;
c906108c 9018
b4ba55a1 9019 if (cu->language == language_ada)
a73c6dcd 9020 error (_("unexpected member function in Ada type"));
b4ba55a1 9021
2df3850c 9022 /* Get name of member function. */
39cbfefa
DJ
9023 fieldname = dwarf2_name (die, cu);
9024 if (fieldname == NULL)
2df3850c 9025 return;
c906108c 9026
c906108c
SS
9027 /* Look up member function name in fieldlist. */
9028 for (i = 0; i < fip->nfnfields; i++)
9029 {
27bfe10e 9030 if (strcmp (fip->fnfieldlists[i].name, fieldname) == 0)
c906108c
SS
9031 break;
9032 }
9033
9034 /* Create new list element if necessary. */
9035 if (i < fip->nfnfields)
9036 flp = &fip->fnfieldlists[i];
9037 else
9038 {
9039 if ((fip->nfnfields % DW_FIELD_ALLOC_CHUNK) == 0)
9040 {
9041 fip->fnfieldlists = (struct fnfieldlist *)
9042 xrealloc (fip->fnfieldlists,
9043 (fip->nfnfields + DW_FIELD_ALLOC_CHUNK)
c5aa993b 9044 * sizeof (struct fnfieldlist));
c906108c 9045 if (fip->nfnfields == 0)
c13c43fd 9046 make_cleanup (free_current_contents, &fip->fnfieldlists);
c906108c
SS
9047 }
9048 flp = &fip->fnfieldlists[fip->nfnfields];
9049 flp->name = fieldname;
9050 flp->length = 0;
9051 flp->head = NULL;
3da10d80 9052 i = fip->nfnfields++;
c906108c
SS
9053 }
9054
9055 /* Create a new member function field and chain it to the field list
0963b4bd 9056 entry. */
c906108c 9057 new_fnfield = (struct nextfnfield *) xmalloc (sizeof (struct nextfnfield));
b8c9b27d 9058 make_cleanup (xfree, new_fnfield);
c906108c
SS
9059 memset (new_fnfield, 0, sizeof (struct nextfnfield));
9060 new_fnfield->next = flp->head;
9061 flp->head = new_fnfield;
9062 flp->length++;
9063
9064 /* Fill in the member function field info. */
9065 fnp = &new_fnfield->fnfield;
3da10d80
KS
9066
9067 /* Delay processing of the physname until later. */
9068 if (cu->language == language_cplus || cu->language == language_java)
9069 {
9070 add_to_method_list (type, i, flp->length - 1, fieldname,
9071 die, cu);
9072 }
9073 else
9074 {
1d06ead6 9075 const char *physname = dwarf2_physname (fieldname, die, cu);
3da10d80
KS
9076 fnp->physname = physname ? physname : "";
9077 }
9078
c906108c 9079 fnp->type = alloc_type (objfile);
f792889a
DJ
9080 this_type = read_type_die (die, cu);
9081 if (this_type && TYPE_CODE (this_type) == TYPE_CODE_FUNC)
c906108c 9082 {
f792889a 9083 int nparams = TYPE_NFIELDS (this_type);
c906108c 9084
f792889a 9085 /* TYPE is the domain of this method, and THIS_TYPE is the type
e26fb1d7
DC
9086 of the method itself (TYPE_CODE_METHOD). */
9087 smash_to_method_type (fnp->type, type,
f792889a
DJ
9088 TYPE_TARGET_TYPE (this_type),
9089 TYPE_FIELDS (this_type),
9090 TYPE_NFIELDS (this_type),
9091 TYPE_VARARGS (this_type));
c906108c
SS
9092
9093 /* Handle static member functions.
c5aa993b 9094 Dwarf2 has no clean way to discern C++ static and non-static
0963b4bd
MS
9095 member functions. G++ helps GDB by marking the first
9096 parameter for non-static member functions (which is the this
9097 pointer) as artificial. We obtain this information from
9098 read_subroutine_type via TYPE_FIELD_ARTIFICIAL. */
f792889a 9099 if (nparams == 0 || TYPE_FIELD_ARTIFICIAL (this_type, 0) == 0)
c906108c
SS
9100 fnp->voffset = VOFFSET_STATIC;
9101 }
9102 else
e2e0b3e5 9103 complaint (&symfile_complaints, _("member function type missing for '%s'"),
3da10d80 9104 dwarf2_full_name (fieldname, die, cu));
c906108c
SS
9105
9106 /* Get fcontext from DW_AT_containing_type if present. */
e142c38c 9107 if (dwarf2_attr (die, DW_AT_containing_type, cu) != NULL)
e7c27a73 9108 fnp->fcontext = die_containing_type (die, cu);
c906108c 9109
3e43a32a
MS
9110 /* dwarf2 doesn't have stubbed physical names, so the setting of is_const and
9111 is_volatile is irrelevant, as it is needed by gdb_mangle_name only. */
c906108c
SS
9112
9113 /* Get accessibility. */
e142c38c 9114 attr = dwarf2_attr (die, DW_AT_accessibility, cu);
c906108c 9115 if (attr)
60d5a603
JK
9116 accessibility = DW_UNSND (attr);
9117 else
9118 accessibility = dwarf2_default_access_attribute (die, cu);
9119 switch (accessibility)
c906108c 9120 {
60d5a603
JK
9121 case DW_ACCESS_private:
9122 fnp->is_private = 1;
9123 break;
9124 case DW_ACCESS_protected:
9125 fnp->is_protected = 1;
9126 break;
c906108c
SS
9127 }
9128
b02dede2 9129 /* Check for artificial methods. */
e142c38c 9130 attr = dwarf2_attr (die, DW_AT_artificial, cu);
b02dede2
DJ
9131 if (attr && DW_UNSND (attr) != 0)
9132 fnp->is_artificial = 1;
9133
0d564a31 9134 /* Get index in virtual function table if it is a virtual member
aec5aa8b
TT
9135 function. For older versions of GCC, this is an offset in the
9136 appropriate virtual table, as specified by DW_AT_containing_type.
9137 For everyone else, it is an expression to be evaluated relative
0d564a31
DJ
9138 to the object address. */
9139
e142c38c 9140 attr = dwarf2_attr (die, DW_AT_vtable_elem_location, cu);
aec5aa8b 9141 if (attr)
8e19ed76 9142 {
aec5aa8b 9143 if (attr_form_is_block (attr) && DW_BLOCK (attr)->size > 0)
8e19ed76 9144 {
aec5aa8b
TT
9145 if (DW_BLOCK (attr)->data[0] == DW_OP_constu)
9146 {
9147 /* Old-style GCC. */
9148 fnp->voffset = decode_locdesc (DW_BLOCK (attr), cu) + 2;
9149 }
9150 else if (DW_BLOCK (attr)->data[0] == DW_OP_deref
9151 || (DW_BLOCK (attr)->size > 1
9152 && DW_BLOCK (attr)->data[0] == DW_OP_deref_size
9153 && DW_BLOCK (attr)->data[1] == cu->header.addr_size))
9154 {
9155 struct dwarf_block blk;
9156 int offset;
9157
9158 offset = (DW_BLOCK (attr)->data[0] == DW_OP_deref
9159 ? 1 : 2);
9160 blk.size = DW_BLOCK (attr)->size - offset;
9161 blk.data = DW_BLOCK (attr)->data + offset;
9162 fnp->voffset = decode_locdesc (DW_BLOCK (attr), cu);
9163 if ((fnp->voffset % cu->header.addr_size) != 0)
9164 dwarf2_complex_location_expr_complaint ();
9165 else
9166 fnp->voffset /= cu->header.addr_size;
9167 fnp->voffset += 2;
9168 }
9169 else
9170 dwarf2_complex_location_expr_complaint ();
9171
9172 if (!fnp->fcontext)
9173 fnp->fcontext = TYPE_TARGET_TYPE (TYPE_FIELD_TYPE (this_type, 0));
9174 }
3690dd37 9175 else if (attr_form_is_section_offset (attr))
8e19ed76 9176 {
4d3c2250 9177 dwarf2_complex_location_expr_complaint ();
8e19ed76
PS
9178 }
9179 else
9180 {
4d3c2250
KB
9181 dwarf2_invalid_attrib_class_complaint ("DW_AT_vtable_elem_location",
9182 fieldname);
8e19ed76 9183 }
0d564a31 9184 }
d48cc9dd
DJ
9185 else
9186 {
9187 attr = dwarf2_attr (die, DW_AT_virtuality, cu);
9188 if (attr && DW_UNSND (attr))
9189 {
9190 /* GCC does this, as of 2008-08-25; PR debug/37237. */
9191 complaint (&symfile_complaints,
3e43a32a
MS
9192 _("Member function \"%s\" (offset %d) is virtual "
9193 "but the vtable offset is not specified"),
b64f50a1 9194 fieldname, die->offset.sect_off);
9655fd1a 9195 ALLOCATE_CPLUS_STRUCT_TYPE (type);
d48cc9dd
DJ
9196 TYPE_CPLUS_DYNAMIC (type) = 1;
9197 }
9198 }
c906108c
SS
9199}
9200
9201/* Create the vector of member function fields, and attach it to the type. */
9202
9203static void
fba45db2 9204dwarf2_attach_fn_fields_to_type (struct field_info *fip, struct type *type,
e7c27a73 9205 struct dwarf2_cu *cu)
c906108c
SS
9206{
9207 struct fnfieldlist *flp;
c906108c
SS
9208 int i;
9209
b4ba55a1 9210 if (cu->language == language_ada)
a73c6dcd 9211 error (_("unexpected member functions in Ada type"));
b4ba55a1 9212
c906108c
SS
9213 ALLOCATE_CPLUS_STRUCT_TYPE (type);
9214 TYPE_FN_FIELDLISTS (type) = (struct fn_fieldlist *)
9215 TYPE_ALLOC (type, sizeof (struct fn_fieldlist) * fip->nfnfields);
9216
9217 for (i = 0, flp = fip->fnfieldlists; i < fip->nfnfields; i++, flp++)
9218 {
9219 struct nextfnfield *nfp = flp->head;
9220 struct fn_fieldlist *fn_flp = &TYPE_FN_FIELDLIST (type, i);
9221 int k;
9222
9223 TYPE_FN_FIELDLIST_NAME (type, i) = flp->name;
9224 TYPE_FN_FIELDLIST_LENGTH (type, i) = flp->length;
9225 fn_flp->fn_fields = (struct fn_field *)
9226 TYPE_ALLOC (type, sizeof (struct fn_field) * flp->length);
9227 for (k = flp->length; (k--, nfp); nfp = nfp->next)
c5aa993b 9228 fn_flp->fn_fields[k] = nfp->fnfield;
c906108c
SS
9229 }
9230
9231 TYPE_NFN_FIELDS (type) = fip->nfnfields;
c906108c
SS
9232}
9233
1168df01
JB
9234/* Returns non-zero if NAME is the name of a vtable member in CU's
9235 language, zero otherwise. */
9236static int
9237is_vtable_name (const char *name, struct dwarf2_cu *cu)
9238{
9239 static const char vptr[] = "_vptr";
987504bb 9240 static const char vtable[] = "vtable";
1168df01 9241
987504bb
JJ
9242 /* Look for the C++ and Java forms of the vtable. */
9243 if ((cu->language == language_java
9244 && strncmp (name, vtable, sizeof (vtable) - 1) == 0)
9245 || (strncmp (name, vptr, sizeof (vptr) - 1) == 0
9246 && is_cplus_marker (name[sizeof (vptr) - 1])))
1168df01
JB
9247 return 1;
9248
9249 return 0;
9250}
9251
c0dd20ea 9252/* GCC outputs unnamed structures that are really pointers to member
0b92b5bb
TT
9253 functions, with the ABI-specified layout. If TYPE describes
9254 such a structure, smash it into a member function type.
61049d3b
DJ
9255
9256 GCC shouldn't do this; it should just output pointer to member DIEs.
9257 This is GCC PR debug/28767. */
c0dd20ea 9258
0b92b5bb
TT
9259static void
9260quirk_gcc_member_function_pointer (struct type *type, struct objfile *objfile)
c0dd20ea 9261{
0b92b5bb 9262 struct type *pfn_type, *domain_type, *new_type;
c0dd20ea
DJ
9263
9264 /* Check for a structure with no name and two children. */
0b92b5bb
TT
9265 if (TYPE_CODE (type) != TYPE_CODE_STRUCT || TYPE_NFIELDS (type) != 2)
9266 return;
c0dd20ea
DJ
9267
9268 /* Check for __pfn and __delta members. */
0b92b5bb
TT
9269 if (TYPE_FIELD_NAME (type, 0) == NULL
9270 || strcmp (TYPE_FIELD_NAME (type, 0), "__pfn") != 0
9271 || TYPE_FIELD_NAME (type, 1) == NULL
9272 || strcmp (TYPE_FIELD_NAME (type, 1), "__delta") != 0)
9273 return;
c0dd20ea
DJ
9274
9275 /* Find the type of the method. */
0b92b5bb 9276 pfn_type = TYPE_FIELD_TYPE (type, 0);
c0dd20ea
DJ
9277 if (pfn_type == NULL
9278 || TYPE_CODE (pfn_type) != TYPE_CODE_PTR
9279 || TYPE_CODE (TYPE_TARGET_TYPE (pfn_type)) != TYPE_CODE_FUNC)
0b92b5bb 9280 return;
c0dd20ea
DJ
9281
9282 /* Look for the "this" argument. */
9283 pfn_type = TYPE_TARGET_TYPE (pfn_type);
9284 if (TYPE_NFIELDS (pfn_type) == 0
0b92b5bb 9285 /* || TYPE_FIELD_TYPE (pfn_type, 0) == NULL */
c0dd20ea 9286 || TYPE_CODE (TYPE_FIELD_TYPE (pfn_type, 0)) != TYPE_CODE_PTR)
0b92b5bb 9287 return;
c0dd20ea
DJ
9288
9289 domain_type = TYPE_TARGET_TYPE (TYPE_FIELD_TYPE (pfn_type, 0));
0b92b5bb
TT
9290 new_type = alloc_type (objfile);
9291 smash_to_method_type (new_type, domain_type, TYPE_TARGET_TYPE (pfn_type),
c0dd20ea
DJ
9292 TYPE_FIELDS (pfn_type), TYPE_NFIELDS (pfn_type),
9293 TYPE_VARARGS (pfn_type));
0b92b5bb 9294 smash_to_methodptr_type (type, new_type);
c0dd20ea 9295}
1168df01 9296
685b1105
JK
9297/* Return non-zero if the CU's PRODUCER string matches the Intel C/C++ compiler
9298 (icc). */
9299
9300static int
9301producer_is_icc (struct dwarf2_cu *cu)
9302{
9303 if (!cu->checked_producer)
9304 check_producer (cu);
9305
9306 return cu->producer_is_icc;
9307}
9308
c906108c 9309/* Called when we find the DIE that starts a structure or union scope
c767944b
DJ
9310 (definition) to create a type for the structure or union. Fill in
9311 the type's name and general properties; the members will not be
9312 processed until process_structure_type.
c906108c 9313
c767944b
DJ
9314 NOTE: we need to call these functions regardless of whether or not the
9315 DIE has a DW_AT_name attribute, since it might be an anonymous
c906108c
SS
9316 structure or union. This gets the type entered into our set of
9317 user defined types.
9318
9319 However, if the structure is incomplete (an opaque struct/union)
9320 then suppress creating a symbol table entry for it since gdb only
9321 wants to find the one with the complete definition. Note that if
9322 it is complete, we just call new_symbol, which does it's own
9323 checking about whether the struct/union is anonymous or not (and
9324 suppresses creating a symbol table entry itself). */
9325
f792889a 9326static struct type *
134d01f1 9327read_structure_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 9328{
e7c27a73 9329 struct objfile *objfile = cu->objfile;
c906108c
SS
9330 struct type *type;
9331 struct attribute *attr;
39cbfefa 9332 char *name;
c906108c 9333
348e048f
DE
9334 /* If the definition of this type lives in .debug_types, read that type.
9335 Don't follow DW_AT_specification though, that will take us back up
9336 the chain and we want to go down. */
9337 attr = dwarf2_attr_no_follow (die, DW_AT_signature, cu);
9338 if (attr)
9339 {
9340 struct dwarf2_cu *type_cu = cu;
9341 struct die_info *type_die = follow_die_ref_or_sig (die, attr, &type_cu);
9a619af0 9342
348e048f
DE
9343 /* We could just recurse on read_structure_type, but we need to call
9344 get_die_type to ensure only one type for this DIE is created.
9345 This is important, for example, because for c++ classes we need
9346 TYPE_NAME set which is only done by new_symbol. Blech. */
9347 type = read_type_die (type_die, type_cu);
9dc481d3
DE
9348
9349 /* TYPE_CU may not be the same as CU.
9350 Ensure TYPE is recorded in CU's type_hash table. */
348e048f
DE
9351 return set_die_type (die, type, cu);
9352 }
9353
c0dd20ea 9354 type = alloc_type (objfile);
c906108c 9355 INIT_CPLUS_SPECIFIC (type);
93311388 9356
39cbfefa
DJ
9357 name = dwarf2_name (die, cu);
9358 if (name != NULL)
c906108c 9359 {
987504bb
JJ
9360 if (cu->language == language_cplus
9361 || cu->language == language_java)
63d06c5c 9362 {
3da10d80
KS
9363 char *full_name = (char *) dwarf2_full_name (name, die, cu);
9364
9365 /* dwarf2_full_name might have already finished building the DIE's
9366 type. If so, there is no need to continue. */
9367 if (get_die_type (die, cu) != NULL)
9368 return get_die_type (die, cu);
9369
9370 TYPE_TAG_NAME (type) = full_name;
94af9270
KS
9371 if (die->tag == DW_TAG_structure_type
9372 || die->tag == DW_TAG_class_type)
9373 TYPE_NAME (type) = TYPE_TAG_NAME (type);
63d06c5c
DC
9374 }
9375 else
9376 {
d8151005
DJ
9377 /* The name is already allocated along with this objfile, so
9378 we don't need to duplicate it for the type. */
94af9270
KS
9379 TYPE_TAG_NAME (type) = (char *) name;
9380 if (die->tag == DW_TAG_class_type)
9381 TYPE_NAME (type) = TYPE_TAG_NAME (type);
63d06c5c 9382 }
c906108c
SS
9383 }
9384
9385 if (die->tag == DW_TAG_structure_type)
9386 {
9387 TYPE_CODE (type) = TYPE_CODE_STRUCT;
9388 }
9389 else if (die->tag == DW_TAG_union_type)
9390 {
9391 TYPE_CODE (type) = TYPE_CODE_UNION;
9392 }
9393 else
9394 {
c906108c
SS
9395 TYPE_CODE (type) = TYPE_CODE_CLASS;
9396 }
9397
0cc2414c
TT
9398 if (cu->language == language_cplus && die->tag == DW_TAG_class_type)
9399 TYPE_DECLARED_CLASS (type) = 1;
9400
e142c38c 9401 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
c906108c
SS
9402 if (attr)
9403 {
9404 TYPE_LENGTH (type) = DW_UNSND (attr);
9405 }
9406 else
9407 {
9408 TYPE_LENGTH (type) = 0;
9409 }
9410
685b1105
JK
9411 if (producer_is_icc (cu))
9412 {
9413 /* ICC does not output the required DW_AT_declaration
9414 on incomplete types, but gives them a size of zero. */
9415 }
9416 else
9417 TYPE_STUB_SUPPORTED (type) = 1;
9418
dc718098 9419 if (die_is_declaration (die, cu))
876cecd0 9420 TYPE_STUB (type) = 1;
a6c727b2
DJ
9421 else if (attr == NULL && die->child == NULL
9422 && producer_is_realview (cu->producer))
9423 /* RealView does not output the required DW_AT_declaration
9424 on incomplete types. */
9425 TYPE_STUB (type) = 1;
dc718098 9426
c906108c
SS
9427 /* We need to add the type field to the die immediately so we don't
9428 infinitely recurse when dealing with pointers to the structure
0963b4bd 9429 type within the structure itself. */
1c379e20 9430 set_die_type (die, type, cu);
c906108c 9431
7e314c57
JK
9432 /* set_die_type should be already done. */
9433 set_descriptive_type (type, die, cu);
9434
c767944b
DJ
9435 return type;
9436}
9437
9438/* Finish creating a structure or union type, including filling in
9439 its members and creating a symbol for it. */
9440
9441static void
9442process_structure_scope (struct die_info *die, struct dwarf2_cu *cu)
9443{
9444 struct objfile *objfile = cu->objfile;
9445 struct die_info *child_die = die->child;
9446 struct type *type;
9447
9448 type = get_die_type (die, cu);
9449 if (type == NULL)
9450 type = read_structure_type (die, cu);
9451
e142c38c 9452 if (die->child != NULL && ! die_is_declaration (die, cu))
c906108c
SS
9453 {
9454 struct field_info fi;
9455 struct die_info *child_die;
34eaf542 9456 VEC (symbolp) *template_args = NULL;
c767944b 9457 struct cleanup *back_to = make_cleanup (null_cleanup, 0);
c906108c
SS
9458
9459 memset (&fi, 0, sizeof (struct field_info));
9460
639d11d3 9461 child_die = die->child;
c906108c
SS
9462
9463 while (child_die && child_die->tag)
9464 {
a9a9bd0f
DC
9465 if (child_die->tag == DW_TAG_member
9466 || child_die->tag == DW_TAG_variable)
c906108c 9467 {
a9a9bd0f
DC
9468 /* NOTE: carlton/2002-11-05: A C++ static data member
9469 should be a DW_TAG_member that is a declaration, but
9470 all versions of G++ as of this writing (so through at
9471 least 3.2.1) incorrectly generate DW_TAG_variable
9472 tags for them instead. */
e7c27a73 9473 dwarf2_add_field (&fi, child_die, cu);
c906108c 9474 }
8713b1b1 9475 else if (child_die->tag == DW_TAG_subprogram)
c906108c 9476 {
0963b4bd 9477 /* C++ member function. */
e7c27a73 9478 dwarf2_add_member_fn (&fi, child_die, type, cu);
c906108c
SS
9479 }
9480 else if (child_die->tag == DW_TAG_inheritance)
9481 {
9482 /* C++ base class field. */
e7c27a73 9483 dwarf2_add_field (&fi, child_die, cu);
c906108c 9484 }
98751a41
JK
9485 else if (child_die->tag == DW_TAG_typedef)
9486 dwarf2_add_typedef (&fi, child_die, cu);
34eaf542
TT
9487 else if (child_die->tag == DW_TAG_template_type_param
9488 || child_die->tag == DW_TAG_template_value_param)
9489 {
9490 struct symbol *arg = new_symbol (child_die, NULL, cu);
9491
f1078f66
DJ
9492 if (arg != NULL)
9493 VEC_safe_push (symbolp, template_args, arg);
34eaf542
TT
9494 }
9495
c906108c
SS
9496 child_die = sibling_die (child_die);
9497 }
9498
34eaf542
TT
9499 /* Attach template arguments to type. */
9500 if (! VEC_empty (symbolp, template_args))
9501 {
9502 ALLOCATE_CPLUS_STRUCT_TYPE (type);
9503 TYPE_N_TEMPLATE_ARGUMENTS (type)
9504 = VEC_length (symbolp, template_args);
9505 TYPE_TEMPLATE_ARGUMENTS (type)
9506 = obstack_alloc (&objfile->objfile_obstack,
9507 (TYPE_N_TEMPLATE_ARGUMENTS (type)
9508 * sizeof (struct symbol *)));
9509 memcpy (TYPE_TEMPLATE_ARGUMENTS (type),
9510 VEC_address (symbolp, template_args),
9511 (TYPE_N_TEMPLATE_ARGUMENTS (type)
9512 * sizeof (struct symbol *)));
9513 VEC_free (symbolp, template_args);
9514 }
9515
c906108c
SS
9516 /* Attach fields and member functions to the type. */
9517 if (fi.nfields)
e7c27a73 9518 dwarf2_attach_fields_to_type (&fi, type, cu);
c906108c
SS
9519 if (fi.nfnfields)
9520 {
e7c27a73 9521 dwarf2_attach_fn_fields_to_type (&fi, type, cu);
c906108c 9522
c5aa993b 9523 /* Get the type which refers to the base class (possibly this
c906108c 9524 class itself) which contains the vtable pointer for the current
0d564a31
DJ
9525 class from the DW_AT_containing_type attribute. This use of
9526 DW_AT_containing_type is a GNU extension. */
c906108c 9527
e142c38c 9528 if (dwarf2_attr (die, DW_AT_containing_type, cu) != NULL)
c906108c 9529 {
e7c27a73 9530 struct type *t = die_containing_type (die, cu);
c906108c
SS
9531
9532 TYPE_VPTR_BASETYPE (type) = t;
9533 if (type == t)
9534 {
c906108c
SS
9535 int i;
9536
9537 /* Our own class provides vtbl ptr. */
9538 for (i = TYPE_NFIELDS (t) - 1;
9539 i >= TYPE_N_BASECLASSES (t);
9540 --i)
9541 {
0d5cff50 9542 const char *fieldname = TYPE_FIELD_NAME (t, i);
c906108c 9543
1168df01 9544 if (is_vtable_name (fieldname, cu))
c906108c
SS
9545 {
9546 TYPE_VPTR_FIELDNO (type) = i;
9547 break;
9548 }
9549 }
9550
9551 /* Complain if virtual function table field not found. */
9552 if (i < TYPE_N_BASECLASSES (t))
4d3c2250 9553 complaint (&symfile_complaints,
3e43a32a
MS
9554 _("virtual function table pointer "
9555 "not found when defining class '%s'"),
4d3c2250
KB
9556 TYPE_TAG_NAME (type) ? TYPE_TAG_NAME (type) :
9557 "");
c906108c
SS
9558 }
9559 else
9560 {
9561 TYPE_VPTR_FIELDNO (type) = TYPE_VPTR_FIELDNO (t);
9562 }
9563 }
f6235d4c
EZ
9564 else if (cu->producer
9565 && strncmp (cu->producer,
9566 "IBM(R) XL C/C++ Advanced Edition", 32) == 0)
9567 {
9568 /* The IBM XLC compiler does not provide direct indication
9569 of the containing type, but the vtable pointer is
9570 always named __vfp. */
9571
9572 int i;
9573
9574 for (i = TYPE_NFIELDS (type) - 1;
9575 i >= TYPE_N_BASECLASSES (type);
9576 --i)
9577 {
9578 if (strcmp (TYPE_FIELD_NAME (type, i), "__vfp") == 0)
9579 {
9580 TYPE_VPTR_FIELDNO (type) = i;
9581 TYPE_VPTR_BASETYPE (type) = type;
9582 break;
9583 }
9584 }
9585 }
c906108c 9586 }
98751a41
JK
9587
9588 /* Copy fi.typedef_field_list linked list elements content into the
9589 allocated array TYPE_TYPEDEF_FIELD_ARRAY (type). */
9590 if (fi.typedef_field_list)
9591 {
9592 int i = fi.typedef_field_list_count;
9593
a0d7a4ff 9594 ALLOCATE_CPLUS_STRUCT_TYPE (type);
98751a41
JK
9595 TYPE_TYPEDEF_FIELD_ARRAY (type)
9596 = TYPE_ALLOC (type, sizeof (TYPE_TYPEDEF_FIELD (type, 0)) * i);
9597 TYPE_TYPEDEF_FIELD_COUNT (type) = i;
9598
9599 /* Reverse the list order to keep the debug info elements order. */
9600 while (--i >= 0)
9601 {
9602 struct typedef_field *dest, *src;
6e70227d 9603
98751a41
JK
9604 dest = &TYPE_TYPEDEF_FIELD (type, i);
9605 src = &fi.typedef_field_list->field;
9606 fi.typedef_field_list = fi.typedef_field_list->next;
9607 *dest = *src;
9608 }
9609 }
c767944b
DJ
9610
9611 do_cleanups (back_to);
eb2a6f42
TT
9612
9613 if (HAVE_CPLUS_STRUCT (type))
9614 TYPE_CPLUS_REALLY_JAVA (type) = cu->language == language_java;
c906108c 9615 }
63d06c5c 9616
bb5ed363 9617 quirk_gcc_member_function_pointer (type, objfile);
0b92b5bb 9618
90aeadfc
DC
9619 /* NOTE: carlton/2004-03-16: GCC 3.4 (or at least one of its
9620 snapshots) has been known to create a die giving a declaration
9621 for a class that has, as a child, a die giving a definition for a
9622 nested class. So we have to process our children even if the
9623 current die is a declaration. Normally, of course, a declaration
9624 won't have any children at all. */
134d01f1 9625
90aeadfc
DC
9626 while (child_die != NULL && child_die->tag)
9627 {
9628 if (child_die->tag == DW_TAG_member
9629 || child_die->tag == DW_TAG_variable
34eaf542
TT
9630 || child_die->tag == DW_TAG_inheritance
9631 || child_die->tag == DW_TAG_template_value_param
9632 || child_die->tag == DW_TAG_template_type_param)
134d01f1 9633 {
90aeadfc 9634 /* Do nothing. */
134d01f1 9635 }
90aeadfc
DC
9636 else
9637 process_die (child_die, cu);
134d01f1 9638
90aeadfc 9639 child_die = sibling_die (child_die);
134d01f1
DJ
9640 }
9641
fa4028e9
JB
9642 /* Do not consider external references. According to the DWARF standard,
9643 these DIEs are identified by the fact that they have no byte_size
9644 attribute, and a declaration attribute. */
9645 if (dwarf2_attr (die, DW_AT_byte_size, cu) != NULL
9646 || !die_is_declaration (die, cu))
c767944b 9647 new_symbol (die, type, cu);
134d01f1
DJ
9648}
9649
9650/* Given a DW_AT_enumeration_type die, set its type. We do not
9651 complete the type's fields yet, or create any symbols. */
c906108c 9652
f792889a 9653static struct type *
134d01f1 9654read_enumeration_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 9655{
e7c27a73 9656 struct objfile *objfile = cu->objfile;
c906108c 9657 struct type *type;
c906108c 9658 struct attribute *attr;
0114d602 9659 const char *name;
134d01f1 9660
348e048f
DE
9661 /* If the definition of this type lives in .debug_types, read that type.
9662 Don't follow DW_AT_specification though, that will take us back up
9663 the chain and we want to go down. */
9664 attr = dwarf2_attr_no_follow (die, DW_AT_signature, cu);
9665 if (attr)
9666 {
9667 struct dwarf2_cu *type_cu = cu;
9668 struct die_info *type_die = follow_die_ref_or_sig (die, attr, &type_cu);
9a619af0 9669
348e048f 9670 type = read_type_die (type_die, type_cu);
9dc481d3
DE
9671
9672 /* TYPE_CU may not be the same as CU.
9673 Ensure TYPE is recorded in CU's type_hash table. */
348e048f
DE
9674 return set_die_type (die, type, cu);
9675 }
9676
c906108c
SS
9677 type = alloc_type (objfile);
9678
9679 TYPE_CODE (type) = TYPE_CODE_ENUM;
94af9270 9680 name = dwarf2_full_name (NULL, die, cu);
39cbfefa 9681 if (name != NULL)
0114d602 9682 TYPE_TAG_NAME (type) = (char *) name;
c906108c 9683
e142c38c 9684 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
c906108c
SS
9685 if (attr)
9686 {
9687 TYPE_LENGTH (type) = DW_UNSND (attr);
9688 }
9689 else
9690 {
9691 TYPE_LENGTH (type) = 0;
9692 }
9693
137033e9
JB
9694 /* The enumeration DIE can be incomplete. In Ada, any type can be
9695 declared as private in the package spec, and then defined only
9696 inside the package body. Such types are known as Taft Amendment
9697 Types. When another package uses such a type, an incomplete DIE
9698 may be generated by the compiler. */
02eb380e 9699 if (die_is_declaration (die, cu))
876cecd0 9700 TYPE_STUB (type) = 1;
02eb380e 9701
f792889a 9702 return set_die_type (die, type, cu);
134d01f1
DJ
9703}
9704
9705/* Given a pointer to a die which begins an enumeration, process all
9706 the dies that define the members of the enumeration, and create the
9707 symbol for the enumeration type.
9708
9709 NOTE: We reverse the order of the element list. */
9710
9711static void
9712process_enumeration_scope (struct die_info *die, struct dwarf2_cu *cu)
9713{
f792889a 9714 struct type *this_type;
134d01f1 9715
f792889a
DJ
9716 this_type = get_die_type (die, cu);
9717 if (this_type == NULL)
9718 this_type = read_enumeration_type (die, cu);
9dc481d3 9719
639d11d3 9720 if (die->child != NULL)
c906108c 9721 {
9dc481d3
DE
9722 struct die_info *child_die;
9723 struct symbol *sym;
9724 struct field *fields = NULL;
9725 int num_fields = 0;
9726 int unsigned_enum = 1;
9727 char *name;
cafec441
TT
9728 int flag_enum = 1;
9729 ULONGEST mask = 0;
9dc481d3 9730
639d11d3 9731 child_die = die->child;
c906108c
SS
9732 while (child_die && child_die->tag)
9733 {
9734 if (child_die->tag != DW_TAG_enumerator)
9735 {
e7c27a73 9736 process_die (child_die, cu);
c906108c
SS
9737 }
9738 else
9739 {
39cbfefa
DJ
9740 name = dwarf2_name (child_die, cu);
9741 if (name)
c906108c 9742 {
f792889a 9743 sym = new_symbol (child_die, this_type, cu);
c906108c 9744 if (SYMBOL_VALUE (sym) < 0)
cafec441
TT
9745 {
9746 unsigned_enum = 0;
9747 flag_enum = 0;
9748 }
9749 else if ((mask & SYMBOL_VALUE (sym)) != 0)
9750 flag_enum = 0;
9751 else
9752 mask |= SYMBOL_VALUE (sym);
c906108c
SS
9753
9754 if ((num_fields % DW_FIELD_ALLOC_CHUNK) == 0)
9755 {
9756 fields = (struct field *)
9757 xrealloc (fields,
9758 (num_fields + DW_FIELD_ALLOC_CHUNK)
c5aa993b 9759 * sizeof (struct field));
c906108c
SS
9760 }
9761
3567439c 9762 FIELD_NAME (fields[num_fields]) = SYMBOL_LINKAGE_NAME (sym);
c906108c 9763 FIELD_TYPE (fields[num_fields]) = NULL;
14e75d8e 9764 SET_FIELD_ENUMVAL (fields[num_fields], SYMBOL_VALUE (sym));
c906108c
SS
9765 FIELD_BITSIZE (fields[num_fields]) = 0;
9766
9767 num_fields++;
9768 }
9769 }
9770
9771 child_die = sibling_die (child_die);
9772 }
9773
9774 if (num_fields)
9775 {
f792889a
DJ
9776 TYPE_NFIELDS (this_type) = num_fields;
9777 TYPE_FIELDS (this_type) = (struct field *)
9778 TYPE_ALLOC (this_type, sizeof (struct field) * num_fields);
9779 memcpy (TYPE_FIELDS (this_type), fields,
c906108c 9780 sizeof (struct field) * num_fields);
b8c9b27d 9781 xfree (fields);
c906108c
SS
9782 }
9783 if (unsigned_enum)
876cecd0 9784 TYPE_UNSIGNED (this_type) = 1;
cafec441
TT
9785 if (flag_enum)
9786 TYPE_FLAG_ENUM (this_type) = 1;
c906108c 9787 }
134d01f1 9788
6c83ed52
TT
9789 /* If we are reading an enum from a .debug_types unit, and the enum
9790 is a declaration, and the enum is not the signatured type in the
9791 unit, then we do not want to add a symbol for it. Adding a
9792 symbol would in some cases obscure the true definition of the
9793 enum, giving users an incomplete type when the definition is
9794 actually available. Note that we do not want to do this for all
9795 enums which are just declarations, because C++0x allows forward
9796 enum declarations. */
3019eac3 9797 if (cu->per_cu->is_debug_types
6c83ed52
TT
9798 && die_is_declaration (die, cu))
9799 {
52dc124a 9800 struct signatured_type *sig_type;
6c83ed52 9801
52dc124a 9802 sig_type
6c83ed52 9803 = lookup_signatured_type_at_offset (dwarf2_per_objfile->objfile,
3019eac3 9804 cu->per_cu->info_or_types_section,
6c83ed52 9805 cu->per_cu->offset);
3019eac3
DE
9806 gdb_assert (sig_type->type_offset_in_section.sect_off != 0);
9807 if (sig_type->type_offset_in_section.sect_off != die->offset.sect_off)
6c83ed52
TT
9808 return;
9809 }
9810
f792889a 9811 new_symbol (die, this_type, cu);
c906108c
SS
9812}
9813
9814/* Extract all information from a DW_TAG_array_type DIE and put it in
9815 the DIE's type field. For now, this only handles one dimensional
9816 arrays. */
9817
f792889a 9818static struct type *
e7c27a73 9819read_array_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 9820{
e7c27a73 9821 struct objfile *objfile = cu->objfile;
c906108c 9822 struct die_info *child_die;
7e314c57 9823 struct type *type;
c906108c
SS
9824 struct type *element_type, *range_type, *index_type;
9825 struct type **range_types = NULL;
9826 struct attribute *attr;
9827 int ndim = 0;
9828 struct cleanup *back_to;
39cbfefa 9829 char *name;
c906108c 9830
e7c27a73 9831 element_type = die_type (die, cu);
c906108c 9832
7e314c57
JK
9833 /* The die_type call above may have already set the type for this DIE. */
9834 type = get_die_type (die, cu);
9835 if (type)
9836 return type;
9837
c906108c
SS
9838 /* Irix 6.2 native cc creates array types without children for
9839 arrays with unspecified length. */
639d11d3 9840 if (die->child == NULL)
c906108c 9841 {
46bf5051 9842 index_type = objfile_type (objfile)->builtin_int;
c906108c 9843 range_type = create_range_type (NULL, index_type, 0, -1);
f792889a
DJ
9844 type = create_array_type (NULL, element_type, range_type);
9845 return set_die_type (die, type, cu);
c906108c
SS
9846 }
9847
9848 back_to = make_cleanup (null_cleanup, NULL);
639d11d3 9849 child_die = die->child;
c906108c
SS
9850 while (child_die && child_die->tag)
9851 {
9852 if (child_die->tag == DW_TAG_subrange_type)
9853 {
f792889a 9854 struct type *child_type = read_type_die (child_die, cu);
9a619af0 9855
f792889a 9856 if (child_type != NULL)
a02abb62 9857 {
0963b4bd
MS
9858 /* The range type was succesfully read. Save it for the
9859 array type creation. */
a02abb62
JB
9860 if ((ndim % DW_FIELD_ALLOC_CHUNK) == 0)
9861 {
9862 range_types = (struct type **)
9863 xrealloc (range_types, (ndim + DW_FIELD_ALLOC_CHUNK)
9864 * sizeof (struct type *));
9865 if (ndim == 0)
9866 make_cleanup (free_current_contents, &range_types);
9867 }
f792889a 9868 range_types[ndim++] = child_type;
a02abb62 9869 }
c906108c
SS
9870 }
9871 child_die = sibling_die (child_die);
9872 }
9873
9874 /* Dwarf2 dimensions are output from left to right, create the
9875 necessary array types in backwards order. */
7ca2d3a3 9876
c906108c 9877 type = element_type;
7ca2d3a3
DL
9878
9879 if (read_array_order (die, cu) == DW_ORD_col_major)
9880 {
9881 int i = 0;
9a619af0 9882
7ca2d3a3
DL
9883 while (i < ndim)
9884 type = create_array_type (NULL, type, range_types[i++]);
9885 }
9886 else
9887 {
9888 while (ndim-- > 0)
9889 type = create_array_type (NULL, type, range_types[ndim]);
9890 }
c906108c 9891
f5f8a009
EZ
9892 /* Understand Dwarf2 support for vector types (like they occur on
9893 the PowerPC w/ AltiVec). Gcc just adds another attribute to the
9894 array type. This is not part of the Dwarf2/3 standard yet, but a
9895 custom vendor extension. The main difference between a regular
9896 array and the vector variant is that vectors are passed by value
9897 to functions. */
e142c38c 9898 attr = dwarf2_attr (die, DW_AT_GNU_vector, cu);
f5f8a009 9899 if (attr)
ea37ba09 9900 make_vector_type (type);
f5f8a009 9901
dbc98a8b
KW
9902 /* The DIE may have DW_AT_byte_size set. For example an OpenCL
9903 implementation may choose to implement triple vectors using this
9904 attribute. */
9905 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
9906 if (attr)
9907 {
9908 if (DW_UNSND (attr) >= TYPE_LENGTH (type))
9909 TYPE_LENGTH (type) = DW_UNSND (attr);
9910 else
3e43a32a
MS
9911 complaint (&symfile_complaints,
9912 _("DW_AT_byte_size for array type smaller "
9913 "than the total size of elements"));
dbc98a8b
KW
9914 }
9915
39cbfefa
DJ
9916 name = dwarf2_name (die, cu);
9917 if (name)
9918 TYPE_NAME (type) = name;
6e70227d 9919
0963b4bd 9920 /* Install the type in the die. */
7e314c57
JK
9921 set_die_type (die, type, cu);
9922
9923 /* set_die_type should be already done. */
b4ba55a1
JB
9924 set_descriptive_type (type, die, cu);
9925
c906108c
SS
9926 do_cleanups (back_to);
9927
7e314c57 9928 return type;
c906108c
SS
9929}
9930
7ca2d3a3 9931static enum dwarf_array_dim_ordering
6e70227d 9932read_array_order (struct die_info *die, struct dwarf2_cu *cu)
7ca2d3a3
DL
9933{
9934 struct attribute *attr;
9935
9936 attr = dwarf2_attr (die, DW_AT_ordering, cu);
9937
9938 if (attr) return DW_SND (attr);
9939
0963b4bd
MS
9940 /* GNU F77 is a special case, as at 08/2004 array type info is the
9941 opposite order to the dwarf2 specification, but data is still
9942 laid out as per normal fortran.
7ca2d3a3 9943
0963b4bd
MS
9944 FIXME: dsl/2004-8-20: If G77 is ever fixed, this will also need
9945 version checking. */
7ca2d3a3 9946
905e0470
PM
9947 if (cu->language == language_fortran
9948 && cu->producer && strstr (cu->producer, "GNU F77"))
7ca2d3a3
DL
9949 {
9950 return DW_ORD_row_major;
9951 }
9952
6e70227d 9953 switch (cu->language_defn->la_array_ordering)
7ca2d3a3
DL
9954 {
9955 case array_column_major:
9956 return DW_ORD_col_major;
9957 case array_row_major:
9958 default:
9959 return DW_ORD_row_major;
9960 };
9961}
9962
72019c9c 9963/* Extract all information from a DW_TAG_set_type DIE and put it in
0963b4bd 9964 the DIE's type field. */
72019c9c 9965
f792889a 9966static struct type *
72019c9c
GM
9967read_set_type (struct die_info *die, struct dwarf2_cu *cu)
9968{
7e314c57
JK
9969 struct type *domain_type, *set_type;
9970 struct attribute *attr;
f792889a 9971
7e314c57
JK
9972 domain_type = die_type (die, cu);
9973
9974 /* The die_type call above may have already set the type for this DIE. */
9975 set_type = get_die_type (die, cu);
9976 if (set_type)
9977 return set_type;
9978
9979 set_type = create_set_type (NULL, domain_type);
9980
9981 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
d09039dd
PM
9982 if (attr)
9983 TYPE_LENGTH (set_type) = DW_UNSND (attr);
7e314c57 9984
f792889a 9985 return set_die_type (die, set_type, cu);
72019c9c 9986}
7ca2d3a3 9987
c906108c
SS
9988/* First cut: install each common block member as a global variable. */
9989
9990static void
e7c27a73 9991read_common_block (struct die_info *die, struct dwarf2_cu *cu)
c906108c
SS
9992{
9993 struct die_info *child_die;
9994 struct attribute *attr;
9995 struct symbol *sym;
9996 CORE_ADDR base = (CORE_ADDR) 0;
9997
e142c38c 9998 attr = dwarf2_attr (die, DW_AT_location, cu);
c906108c
SS
9999 if (attr)
10000 {
0963b4bd 10001 /* Support the .debug_loc offsets. */
8e19ed76
PS
10002 if (attr_form_is_block (attr))
10003 {
e7c27a73 10004 base = decode_locdesc (DW_BLOCK (attr), cu);
8e19ed76 10005 }
3690dd37 10006 else if (attr_form_is_section_offset (attr))
8e19ed76 10007 {
4d3c2250 10008 dwarf2_complex_location_expr_complaint ();
8e19ed76
PS
10009 }
10010 else
10011 {
4d3c2250
KB
10012 dwarf2_invalid_attrib_class_complaint ("DW_AT_location",
10013 "common block member");
8e19ed76 10014 }
c906108c 10015 }
639d11d3 10016 if (die->child != NULL)
c906108c 10017 {
639d11d3 10018 child_die = die->child;
c906108c
SS
10019 while (child_die && child_die->tag)
10020 {
74ac6d43
TT
10021 LONGEST offset;
10022
e7c27a73 10023 sym = new_symbol (child_die, NULL, cu);
e8d28ef4
TT
10024 if (sym != NULL
10025 && handle_data_member_location (child_die, cu, &offset))
c906108c 10026 {
74ac6d43 10027 SYMBOL_VALUE_ADDRESS (sym) = base + offset;
c906108c
SS
10028 add_symbol_to_list (sym, &global_symbols);
10029 }
10030 child_die = sibling_die (child_die);
10031 }
10032 }
10033}
10034
0114d602 10035/* Create a type for a C++ namespace. */
d9fa45fe 10036
0114d602
DJ
10037static struct type *
10038read_namespace_type (struct die_info *die, struct dwarf2_cu *cu)
d9fa45fe 10039{
e7c27a73 10040 struct objfile *objfile = cu->objfile;
0114d602 10041 const char *previous_prefix, *name;
9219021c 10042 int is_anonymous;
0114d602
DJ
10043 struct type *type;
10044
10045 /* For extensions, reuse the type of the original namespace. */
10046 if (dwarf2_attr (die, DW_AT_extension, cu) != NULL)
10047 {
10048 struct die_info *ext_die;
10049 struct dwarf2_cu *ext_cu = cu;
9a619af0 10050
0114d602
DJ
10051 ext_die = dwarf2_extension (die, &ext_cu);
10052 type = read_type_die (ext_die, ext_cu);
9dc481d3
DE
10053
10054 /* EXT_CU may not be the same as CU.
10055 Ensure TYPE is recorded in CU's type_hash table. */
0114d602
DJ
10056 return set_die_type (die, type, cu);
10057 }
9219021c 10058
e142c38c 10059 name = namespace_name (die, &is_anonymous, cu);
9219021c
DC
10060
10061 /* Now build the name of the current namespace. */
10062
0114d602
DJ
10063 previous_prefix = determine_prefix (die, cu);
10064 if (previous_prefix[0] != '\0')
10065 name = typename_concat (&objfile->objfile_obstack,
f55ee35c 10066 previous_prefix, name, 0, cu);
0114d602
DJ
10067
10068 /* Create the type. */
10069 type = init_type (TYPE_CODE_NAMESPACE, 0, 0, NULL,
10070 objfile);
10071 TYPE_NAME (type) = (char *) name;
10072 TYPE_TAG_NAME (type) = TYPE_NAME (type);
10073
60531b24 10074 return set_die_type (die, type, cu);
0114d602
DJ
10075}
10076
10077/* Read a C++ namespace. */
10078
10079static void
10080read_namespace (struct die_info *die, struct dwarf2_cu *cu)
10081{
10082 struct objfile *objfile = cu->objfile;
0114d602 10083 int is_anonymous;
9219021c 10084
5c4e30ca
DC
10085 /* Add a symbol associated to this if we haven't seen the namespace
10086 before. Also, add a using directive if it's an anonymous
10087 namespace. */
9219021c 10088
f2f0e013 10089 if (dwarf2_attr (die, DW_AT_extension, cu) == NULL)
5c4e30ca
DC
10090 {
10091 struct type *type;
10092
0114d602 10093 type = read_type_die (die, cu);
e7c27a73 10094 new_symbol (die, type, cu);
5c4e30ca 10095
e8e80198 10096 namespace_name (die, &is_anonymous, cu);
5c4e30ca 10097 if (is_anonymous)
0114d602
DJ
10098 {
10099 const char *previous_prefix = determine_prefix (die, cu);
9a619af0 10100
c0cc3a76 10101 cp_add_using_directive (previous_prefix, TYPE_NAME (type), NULL,
32019081 10102 NULL, NULL, &objfile->objfile_obstack);
0114d602 10103 }
5c4e30ca 10104 }
9219021c 10105
639d11d3 10106 if (die->child != NULL)
d9fa45fe 10107 {
639d11d3 10108 struct die_info *child_die = die->child;
6e70227d 10109
d9fa45fe
DC
10110 while (child_die && child_die->tag)
10111 {
e7c27a73 10112 process_die (child_die, cu);
d9fa45fe
DC
10113 child_die = sibling_die (child_die);
10114 }
10115 }
38d518c9
EZ
10116}
10117
f55ee35c
JK
10118/* Read a Fortran module as type. This DIE can be only a declaration used for
10119 imported module. Still we need that type as local Fortran "use ... only"
10120 declaration imports depend on the created type in determine_prefix. */
10121
10122static struct type *
10123read_module_type (struct die_info *die, struct dwarf2_cu *cu)
10124{
10125 struct objfile *objfile = cu->objfile;
10126 char *module_name;
10127 struct type *type;
10128
10129 module_name = dwarf2_name (die, cu);
10130 if (!module_name)
3e43a32a
MS
10131 complaint (&symfile_complaints,
10132 _("DW_TAG_module has no name, offset 0x%x"),
b64f50a1 10133 die->offset.sect_off);
f55ee35c
JK
10134 type = init_type (TYPE_CODE_MODULE, 0, 0, module_name, objfile);
10135
10136 /* determine_prefix uses TYPE_TAG_NAME. */
10137 TYPE_TAG_NAME (type) = TYPE_NAME (type);
10138
10139 return set_die_type (die, type, cu);
10140}
10141
5d7cb8df
JK
10142/* Read a Fortran module. */
10143
10144static void
10145read_module (struct die_info *die, struct dwarf2_cu *cu)
10146{
10147 struct die_info *child_die = die->child;
10148
5d7cb8df
JK
10149 while (child_die && child_die->tag)
10150 {
10151 process_die (child_die, cu);
10152 child_die = sibling_die (child_die);
10153 }
10154}
10155
38d518c9
EZ
10156/* Return the name of the namespace represented by DIE. Set
10157 *IS_ANONYMOUS to tell whether or not the namespace is an anonymous
10158 namespace. */
10159
10160static const char *
e142c38c 10161namespace_name (struct die_info *die, int *is_anonymous, struct dwarf2_cu *cu)
38d518c9
EZ
10162{
10163 struct die_info *current_die;
10164 const char *name = NULL;
10165
10166 /* Loop through the extensions until we find a name. */
10167
10168 for (current_die = die;
10169 current_die != NULL;
f2f0e013 10170 current_die = dwarf2_extension (die, &cu))
38d518c9 10171 {
e142c38c 10172 name = dwarf2_name (current_die, cu);
38d518c9
EZ
10173 if (name != NULL)
10174 break;
10175 }
10176
10177 /* Is it an anonymous namespace? */
10178
10179 *is_anonymous = (name == NULL);
10180 if (*is_anonymous)
2b1dbab0 10181 name = CP_ANONYMOUS_NAMESPACE_STR;
38d518c9
EZ
10182
10183 return name;
d9fa45fe
DC
10184}
10185
c906108c
SS
10186/* Extract all information from a DW_TAG_pointer_type DIE and add to
10187 the user defined type vector. */
10188
f792889a 10189static struct type *
e7c27a73 10190read_tag_pointer_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 10191{
5e2b427d 10192 struct gdbarch *gdbarch = get_objfile_arch (cu->objfile);
e7c27a73 10193 struct comp_unit_head *cu_header = &cu->header;
c906108c 10194 struct type *type;
8b2dbe47
KB
10195 struct attribute *attr_byte_size;
10196 struct attribute *attr_address_class;
10197 int byte_size, addr_class;
7e314c57
JK
10198 struct type *target_type;
10199
10200 target_type = die_type (die, cu);
c906108c 10201
7e314c57
JK
10202 /* The die_type call above may have already set the type for this DIE. */
10203 type = get_die_type (die, cu);
10204 if (type)
10205 return type;
10206
10207 type = lookup_pointer_type (target_type);
8b2dbe47 10208
e142c38c 10209 attr_byte_size = dwarf2_attr (die, DW_AT_byte_size, cu);
8b2dbe47
KB
10210 if (attr_byte_size)
10211 byte_size = DW_UNSND (attr_byte_size);
c906108c 10212 else
8b2dbe47
KB
10213 byte_size = cu_header->addr_size;
10214
e142c38c 10215 attr_address_class = dwarf2_attr (die, DW_AT_address_class, cu);
8b2dbe47
KB
10216 if (attr_address_class)
10217 addr_class = DW_UNSND (attr_address_class);
10218 else
10219 addr_class = DW_ADDR_none;
10220
10221 /* If the pointer size or address class is different than the
10222 default, create a type variant marked as such and set the
10223 length accordingly. */
10224 if (TYPE_LENGTH (type) != byte_size || addr_class != DW_ADDR_none)
c906108c 10225 {
5e2b427d 10226 if (gdbarch_address_class_type_flags_p (gdbarch))
8b2dbe47
KB
10227 {
10228 int type_flags;
10229
849957d9 10230 type_flags = gdbarch_address_class_type_flags
5e2b427d 10231 (gdbarch, byte_size, addr_class);
876cecd0
TT
10232 gdb_assert ((type_flags & ~TYPE_INSTANCE_FLAG_ADDRESS_CLASS_ALL)
10233 == 0);
8b2dbe47
KB
10234 type = make_type_with_address_space (type, type_flags);
10235 }
10236 else if (TYPE_LENGTH (type) != byte_size)
10237 {
3e43a32a
MS
10238 complaint (&symfile_complaints,
10239 _("invalid pointer size %d"), byte_size);
8b2dbe47 10240 }
6e70227d 10241 else
9a619af0
MS
10242 {
10243 /* Should we also complain about unhandled address classes? */
10244 }
c906108c 10245 }
8b2dbe47
KB
10246
10247 TYPE_LENGTH (type) = byte_size;
f792889a 10248 return set_die_type (die, type, cu);
c906108c
SS
10249}
10250
10251/* Extract all information from a DW_TAG_ptr_to_member_type DIE and add to
10252 the user defined type vector. */
10253
f792889a 10254static struct type *
e7c27a73 10255read_tag_ptr_to_member_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c
SS
10256{
10257 struct type *type;
10258 struct type *to_type;
10259 struct type *domain;
10260
e7c27a73
DJ
10261 to_type = die_type (die, cu);
10262 domain = die_containing_type (die, cu);
0d5de010 10263
7e314c57
JK
10264 /* The calls above may have already set the type for this DIE. */
10265 type = get_die_type (die, cu);
10266 if (type)
10267 return type;
10268
0d5de010
DJ
10269 if (TYPE_CODE (check_typedef (to_type)) == TYPE_CODE_METHOD)
10270 type = lookup_methodptr_type (to_type);
10271 else
10272 type = lookup_memberptr_type (to_type, domain);
c906108c 10273
f792889a 10274 return set_die_type (die, type, cu);
c906108c
SS
10275}
10276
10277/* Extract all information from a DW_TAG_reference_type DIE and add to
10278 the user defined type vector. */
10279
f792889a 10280static struct type *
e7c27a73 10281read_tag_reference_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 10282{
e7c27a73 10283 struct comp_unit_head *cu_header = &cu->header;
7e314c57 10284 struct type *type, *target_type;
c906108c
SS
10285 struct attribute *attr;
10286
7e314c57
JK
10287 target_type = die_type (die, cu);
10288
10289 /* The die_type call above may have already set the type for this DIE. */
10290 type = get_die_type (die, cu);
10291 if (type)
10292 return type;
10293
10294 type = lookup_reference_type (target_type);
e142c38c 10295 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
c906108c
SS
10296 if (attr)
10297 {
10298 TYPE_LENGTH (type) = DW_UNSND (attr);
10299 }
10300 else
10301 {
107d2387 10302 TYPE_LENGTH (type) = cu_header->addr_size;
c906108c 10303 }
f792889a 10304 return set_die_type (die, type, cu);
c906108c
SS
10305}
10306
f792889a 10307static struct type *
e7c27a73 10308read_tag_const_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 10309{
f792889a 10310 struct type *base_type, *cv_type;
c906108c 10311
e7c27a73 10312 base_type = die_type (die, cu);
7e314c57
JK
10313
10314 /* The die_type call above may have already set the type for this DIE. */
10315 cv_type = get_die_type (die, cu);
10316 if (cv_type)
10317 return cv_type;
10318
2f608a3a
KW
10319 /* In case the const qualifier is applied to an array type, the element type
10320 is so qualified, not the array type (section 6.7.3 of C99). */
10321 if (TYPE_CODE (base_type) == TYPE_CODE_ARRAY)
10322 {
10323 struct type *el_type, *inner_array;
10324
10325 base_type = copy_type (base_type);
10326 inner_array = base_type;
10327
10328 while (TYPE_CODE (TYPE_TARGET_TYPE (inner_array)) == TYPE_CODE_ARRAY)
10329 {
10330 TYPE_TARGET_TYPE (inner_array) =
10331 copy_type (TYPE_TARGET_TYPE (inner_array));
10332 inner_array = TYPE_TARGET_TYPE (inner_array);
10333 }
10334
10335 el_type = TYPE_TARGET_TYPE (inner_array);
10336 TYPE_TARGET_TYPE (inner_array) =
10337 make_cv_type (1, TYPE_VOLATILE (el_type), el_type, NULL);
10338
10339 return set_die_type (die, base_type, cu);
10340 }
10341
f792889a
DJ
10342 cv_type = make_cv_type (1, TYPE_VOLATILE (base_type), base_type, 0);
10343 return set_die_type (die, cv_type, cu);
c906108c
SS
10344}
10345
f792889a 10346static struct type *
e7c27a73 10347read_tag_volatile_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 10348{
f792889a 10349 struct type *base_type, *cv_type;
c906108c 10350
e7c27a73 10351 base_type = die_type (die, cu);
7e314c57
JK
10352
10353 /* The die_type call above may have already set the type for this DIE. */
10354 cv_type = get_die_type (die, cu);
10355 if (cv_type)
10356 return cv_type;
10357
f792889a
DJ
10358 cv_type = make_cv_type (TYPE_CONST (base_type), 1, base_type, 0);
10359 return set_die_type (die, cv_type, cu);
c906108c
SS
10360}
10361
10362/* Extract all information from a DW_TAG_string_type DIE and add to
10363 the user defined type vector. It isn't really a user defined type,
10364 but it behaves like one, with other DIE's using an AT_user_def_type
10365 attribute to reference it. */
10366
f792889a 10367static struct type *
e7c27a73 10368read_tag_string_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 10369{
e7c27a73 10370 struct objfile *objfile = cu->objfile;
3b7538c0 10371 struct gdbarch *gdbarch = get_objfile_arch (objfile);
c906108c
SS
10372 struct type *type, *range_type, *index_type, *char_type;
10373 struct attribute *attr;
10374 unsigned int length;
10375
e142c38c 10376 attr = dwarf2_attr (die, DW_AT_string_length, cu);
c906108c
SS
10377 if (attr)
10378 {
10379 length = DW_UNSND (attr);
10380 }
10381 else
10382 {
0963b4bd 10383 /* Check for the DW_AT_byte_size attribute. */
e142c38c 10384 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
b21b22e0
PS
10385 if (attr)
10386 {
10387 length = DW_UNSND (attr);
10388 }
10389 else
10390 {
10391 length = 1;
10392 }
c906108c 10393 }
6ccb9162 10394
46bf5051 10395 index_type = objfile_type (objfile)->builtin_int;
c906108c 10396 range_type = create_range_type (NULL, index_type, 1, length);
3b7538c0
UW
10397 char_type = language_string_char_type (cu->language_defn, gdbarch);
10398 type = create_string_type (NULL, char_type, range_type);
6ccb9162 10399
f792889a 10400 return set_die_type (die, type, cu);
c906108c
SS
10401}
10402
10403/* Handle DIES due to C code like:
10404
10405 struct foo
c5aa993b
JM
10406 {
10407 int (*funcp)(int a, long l);
10408 int b;
10409 };
c906108c 10410
0963b4bd 10411 ('funcp' generates a DW_TAG_subroutine_type DIE). */
c906108c 10412
f792889a 10413static struct type *
e7c27a73 10414read_subroutine_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 10415{
bb5ed363 10416 struct objfile *objfile = cu->objfile;
0963b4bd
MS
10417 struct type *type; /* Type that this function returns. */
10418 struct type *ftype; /* Function that returns above type. */
c906108c
SS
10419 struct attribute *attr;
10420
e7c27a73 10421 type = die_type (die, cu);
7e314c57
JK
10422
10423 /* The die_type call above may have already set the type for this DIE. */
10424 ftype = get_die_type (die, cu);
10425 if (ftype)
10426 return ftype;
10427
0c8b41f1 10428 ftype = lookup_function_type (type);
c906108c 10429
5b8101ae 10430 /* All functions in C++, Pascal and Java have prototypes. */
e142c38c 10431 attr = dwarf2_attr (die, DW_AT_prototyped, cu);
c906108c 10432 if ((attr && (DW_UNSND (attr) != 0))
987504bb 10433 || cu->language == language_cplus
5b8101ae
PM
10434 || cu->language == language_java
10435 || cu->language == language_pascal)
876cecd0 10436 TYPE_PROTOTYPED (ftype) = 1;
a6c727b2
DJ
10437 else if (producer_is_realview (cu->producer))
10438 /* RealView does not emit DW_AT_prototyped. We can not
10439 distinguish prototyped and unprototyped functions; default to
10440 prototyped, since that is more common in modern code (and
10441 RealView warns about unprototyped functions). */
10442 TYPE_PROTOTYPED (ftype) = 1;
c906108c 10443
c055b101
CV
10444 /* Store the calling convention in the type if it's available in
10445 the subroutine die. Otherwise set the calling convention to
10446 the default value DW_CC_normal. */
10447 attr = dwarf2_attr (die, DW_AT_calling_convention, cu);
54fcddd0
UW
10448 if (attr)
10449 TYPE_CALLING_CONVENTION (ftype) = DW_UNSND (attr);
10450 else if (cu->producer && strstr (cu->producer, "IBM XL C for OpenCL"))
10451 TYPE_CALLING_CONVENTION (ftype) = DW_CC_GDB_IBM_OpenCL;
10452 else
10453 TYPE_CALLING_CONVENTION (ftype) = DW_CC_normal;
76c10ea2
GM
10454
10455 /* We need to add the subroutine type to the die immediately so
10456 we don't infinitely recurse when dealing with parameters
0963b4bd 10457 declared as the same subroutine type. */
76c10ea2 10458 set_die_type (die, ftype, cu);
6e70227d 10459
639d11d3 10460 if (die->child != NULL)
c906108c 10461 {
bb5ed363 10462 struct type *void_type = objfile_type (objfile)->builtin_void;
c906108c 10463 struct die_info *child_die;
8072405b 10464 int nparams, iparams;
c906108c
SS
10465
10466 /* Count the number of parameters.
10467 FIXME: GDB currently ignores vararg functions, but knows about
10468 vararg member functions. */
8072405b 10469 nparams = 0;
639d11d3 10470 child_die = die->child;
c906108c
SS
10471 while (child_die && child_die->tag)
10472 {
10473 if (child_die->tag == DW_TAG_formal_parameter)
10474 nparams++;
10475 else if (child_die->tag == DW_TAG_unspecified_parameters)
876cecd0 10476 TYPE_VARARGS (ftype) = 1;
c906108c
SS
10477 child_die = sibling_die (child_die);
10478 }
10479
10480 /* Allocate storage for parameters and fill them in. */
10481 TYPE_NFIELDS (ftype) = nparams;
10482 TYPE_FIELDS (ftype) = (struct field *)
ae5a43e0 10483 TYPE_ZALLOC (ftype, nparams * sizeof (struct field));
c906108c 10484
8072405b
JK
10485 /* TYPE_FIELD_TYPE must never be NULL. Pre-fill the array to ensure it
10486 even if we error out during the parameters reading below. */
10487 for (iparams = 0; iparams < nparams; iparams++)
10488 TYPE_FIELD_TYPE (ftype, iparams) = void_type;
10489
10490 iparams = 0;
639d11d3 10491 child_die = die->child;
c906108c
SS
10492 while (child_die && child_die->tag)
10493 {
10494 if (child_die->tag == DW_TAG_formal_parameter)
10495 {
3ce3b1ba
PA
10496 struct type *arg_type;
10497
10498 /* DWARF version 2 has no clean way to discern C++
10499 static and non-static member functions. G++ helps
10500 GDB by marking the first parameter for non-static
10501 member functions (which is the this pointer) as
10502 artificial. We pass this information to
10503 dwarf2_add_member_fn via TYPE_FIELD_ARTIFICIAL.
10504
10505 DWARF version 3 added DW_AT_object_pointer, which GCC
10506 4.5 does not yet generate. */
e142c38c 10507 attr = dwarf2_attr (child_die, DW_AT_artificial, cu);
c906108c
SS
10508 if (attr)
10509 TYPE_FIELD_ARTIFICIAL (ftype, iparams) = DW_UNSND (attr);
10510 else
418835cc
KS
10511 {
10512 TYPE_FIELD_ARTIFICIAL (ftype, iparams) = 0;
10513
10514 /* GCC/43521: In java, the formal parameter
10515 "this" is sometimes not marked with DW_AT_artificial. */
10516 if (cu->language == language_java)
10517 {
10518 const char *name = dwarf2_name (child_die, cu);
9a619af0 10519
418835cc
KS
10520 if (name && !strcmp (name, "this"))
10521 TYPE_FIELD_ARTIFICIAL (ftype, iparams) = 1;
10522 }
10523 }
3ce3b1ba
PA
10524 arg_type = die_type (child_die, cu);
10525
10526 /* RealView does not mark THIS as const, which the testsuite
10527 expects. GCC marks THIS as const in method definitions,
10528 but not in the class specifications (GCC PR 43053). */
10529 if (cu->language == language_cplus && !TYPE_CONST (arg_type)
10530 && TYPE_FIELD_ARTIFICIAL (ftype, iparams))
10531 {
10532 int is_this = 0;
10533 struct dwarf2_cu *arg_cu = cu;
10534 const char *name = dwarf2_name (child_die, cu);
10535
10536 attr = dwarf2_attr (die, DW_AT_object_pointer, cu);
10537 if (attr)
10538 {
10539 /* If the compiler emits this, use it. */
10540 if (follow_die_ref (die, attr, &arg_cu) == child_die)
10541 is_this = 1;
10542 }
10543 else if (name && strcmp (name, "this") == 0)
10544 /* Function definitions will have the argument names. */
10545 is_this = 1;
10546 else if (name == NULL && iparams == 0)
10547 /* Declarations may not have the names, so like
10548 elsewhere in GDB, assume an artificial first
10549 argument is "this". */
10550 is_this = 1;
10551
10552 if (is_this)
10553 arg_type = make_cv_type (1, TYPE_VOLATILE (arg_type),
10554 arg_type, 0);
10555 }
10556
10557 TYPE_FIELD_TYPE (ftype, iparams) = arg_type;
c906108c
SS
10558 iparams++;
10559 }
10560 child_die = sibling_die (child_die);
10561 }
10562 }
10563
76c10ea2 10564 return ftype;
c906108c
SS
10565}
10566
f792889a 10567static struct type *
e7c27a73 10568read_typedef (struct die_info *die, struct dwarf2_cu *cu)
c906108c 10569{
e7c27a73 10570 struct objfile *objfile = cu->objfile;
0114d602 10571 const char *name = NULL;
3c8e0968 10572 struct type *this_type, *target_type;
c906108c 10573
94af9270 10574 name = dwarf2_full_name (NULL, die, cu);
f792889a 10575 this_type = init_type (TYPE_CODE_TYPEDEF, 0,
0114d602
DJ
10576 TYPE_FLAG_TARGET_STUB, NULL, objfile);
10577 TYPE_NAME (this_type) = (char *) name;
f792889a 10578 set_die_type (die, this_type, cu);
3c8e0968
DE
10579 target_type = die_type (die, cu);
10580 if (target_type != this_type)
10581 TYPE_TARGET_TYPE (this_type) = target_type;
10582 else
10583 {
10584 /* Self-referential typedefs are, it seems, not allowed by the DWARF
10585 spec and cause infinite loops in GDB. */
10586 complaint (&symfile_complaints,
10587 _("Self-referential DW_TAG_typedef "
10588 "- DIE at 0x%x [in module %s]"),
b64f50a1 10589 die->offset.sect_off, objfile->name);
3c8e0968
DE
10590 TYPE_TARGET_TYPE (this_type) = NULL;
10591 }
f792889a 10592 return this_type;
c906108c
SS
10593}
10594
10595/* Find a representation of a given base type and install
10596 it in the TYPE field of the die. */
10597
f792889a 10598static struct type *
e7c27a73 10599read_base_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 10600{
e7c27a73 10601 struct objfile *objfile = cu->objfile;
c906108c
SS
10602 struct type *type;
10603 struct attribute *attr;
10604 int encoding = 0, size = 0;
39cbfefa 10605 char *name;
6ccb9162
UW
10606 enum type_code code = TYPE_CODE_INT;
10607 int type_flags = 0;
10608 struct type *target_type = NULL;
c906108c 10609
e142c38c 10610 attr = dwarf2_attr (die, DW_AT_encoding, cu);
c906108c
SS
10611 if (attr)
10612 {
10613 encoding = DW_UNSND (attr);
10614 }
e142c38c 10615 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
c906108c
SS
10616 if (attr)
10617 {
10618 size = DW_UNSND (attr);
10619 }
39cbfefa 10620 name = dwarf2_name (die, cu);
6ccb9162 10621 if (!name)
c906108c 10622 {
6ccb9162
UW
10623 complaint (&symfile_complaints,
10624 _("DW_AT_name missing from DW_TAG_base_type"));
c906108c 10625 }
6ccb9162
UW
10626
10627 switch (encoding)
c906108c 10628 {
6ccb9162
UW
10629 case DW_ATE_address:
10630 /* Turn DW_ATE_address into a void * pointer. */
10631 code = TYPE_CODE_PTR;
10632 type_flags |= TYPE_FLAG_UNSIGNED;
10633 target_type = init_type (TYPE_CODE_VOID, 1, 0, NULL, objfile);
10634 break;
10635 case DW_ATE_boolean:
10636 code = TYPE_CODE_BOOL;
10637 type_flags |= TYPE_FLAG_UNSIGNED;
10638 break;
10639 case DW_ATE_complex_float:
10640 code = TYPE_CODE_COMPLEX;
10641 target_type = init_type (TYPE_CODE_FLT, size / 2, 0, NULL, objfile);
10642 break;
10643 case DW_ATE_decimal_float:
10644 code = TYPE_CODE_DECFLOAT;
10645 break;
10646 case DW_ATE_float:
10647 code = TYPE_CODE_FLT;
10648 break;
10649 case DW_ATE_signed:
10650 break;
10651 case DW_ATE_unsigned:
10652 type_flags |= TYPE_FLAG_UNSIGNED;
3b2b8fea
TT
10653 if (cu->language == language_fortran
10654 && name
10655 && strncmp (name, "character(", sizeof ("character(") - 1) == 0)
10656 code = TYPE_CODE_CHAR;
6ccb9162
UW
10657 break;
10658 case DW_ATE_signed_char:
6e70227d 10659 if (cu->language == language_ada || cu->language == language_m2
3b2b8fea
TT
10660 || cu->language == language_pascal
10661 || cu->language == language_fortran)
6ccb9162
UW
10662 code = TYPE_CODE_CHAR;
10663 break;
10664 case DW_ATE_unsigned_char:
868a0084 10665 if (cu->language == language_ada || cu->language == language_m2
3b2b8fea
TT
10666 || cu->language == language_pascal
10667 || cu->language == language_fortran)
6ccb9162
UW
10668 code = TYPE_CODE_CHAR;
10669 type_flags |= TYPE_FLAG_UNSIGNED;
10670 break;
75079b2b
TT
10671 case DW_ATE_UTF:
10672 /* We just treat this as an integer and then recognize the
10673 type by name elsewhere. */
10674 break;
10675
6ccb9162
UW
10676 default:
10677 complaint (&symfile_complaints, _("unsupported DW_AT_encoding: '%s'"),
10678 dwarf_type_encoding_name (encoding));
10679 break;
c906108c 10680 }
6ccb9162 10681
0114d602
DJ
10682 type = init_type (code, size, type_flags, NULL, objfile);
10683 TYPE_NAME (type) = name;
6ccb9162
UW
10684 TYPE_TARGET_TYPE (type) = target_type;
10685
0114d602 10686 if (name && strcmp (name, "char") == 0)
876cecd0 10687 TYPE_NOSIGN (type) = 1;
0114d602 10688
f792889a 10689 return set_die_type (die, type, cu);
c906108c
SS
10690}
10691
a02abb62
JB
10692/* Read the given DW_AT_subrange DIE. */
10693
f792889a 10694static struct type *
a02abb62
JB
10695read_subrange_type (struct die_info *die, struct dwarf2_cu *cu)
10696{
10697 struct type *base_type;
10698 struct type *range_type;
10699 struct attribute *attr;
4fae6e18
JK
10700 LONGEST low, high;
10701 int low_default_is_valid;
39cbfefa 10702 char *name;
43bbcdc2 10703 LONGEST negative_mask;
e77813c8 10704
a02abb62 10705 base_type = die_type (die, cu);
953ac07e
JK
10706 /* Preserve BASE_TYPE's original type, just set its LENGTH. */
10707 check_typedef (base_type);
a02abb62 10708
7e314c57
JK
10709 /* The die_type call above may have already set the type for this DIE. */
10710 range_type = get_die_type (die, cu);
10711 if (range_type)
10712 return range_type;
10713
4fae6e18
JK
10714 /* Set LOW_DEFAULT_IS_VALID if current language and DWARF version allow
10715 omitting DW_AT_lower_bound. */
10716 switch (cu->language)
6e70227d 10717 {
4fae6e18
JK
10718 case language_c:
10719 case language_cplus:
10720 low = 0;
10721 low_default_is_valid = 1;
10722 break;
10723 case language_fortran:
10724 low = 1;
10725 low_default_is_valid = 1;
10726 break;
10727 case language_d:
10728 case language_java:
10729 case language_objc:
10730 low = 0;
10731 low_default_is_valid = (cu->header.version >= 4);
10732 break;
10733 case language_ada:
10734 case language_m2:
10735 case language_pascal:
a02abb62 10736 low = 1;
4fae6e18
JK
10737 low_default_is_valid = (cu->header.version >= 4);
10738 break;
10739 default:
10740 low = 0;
10741 low_default_is_valid = 0;
10742 break;
a02abb62
JB
10743 }
10744
dd5e6932
DJ
10745 /* FIXME: For variable sized arrays either of these could be
10746 a variable rather than a constant value. We'll allow it,
10747 but we don't know how to handle it. */
e142c38c 10748 attr = dwarf2_attr (die, DW_AT_lower_bound, cu);
a02abb62 10749 if (attr)
4fae6e18
JK
10750 low = dwarf2_get_attr_constant_value (attr, low);
10751 else if (!low_default_is_valid)
10752 complaint (&symfile_complaints, _("Missing DW_AT_lower_bound "
10753 "- DIE at 0x%x [in module %s]"),
10754 die->offset.sect_off, cu->objfile->name);
a02abb62 10755
e142c38c 10756 attr = dwarf2_attr (die, DW_AT_upper_bound, cu);
a02abb62 10757 if (attr)
6e70227d 10758 {
d48323d8 10759 if (attr_form_is_block (attr) || is_ref_attr (attr))
a02abb62
JB
10760 {
10761 /* GCC encodes arrays with unspecified or dynamic length
e77813c8 10762 with a DW_FORM_block1 attribute or a reference attribute.
a02abb62
JB
10763 FIXME: GDB does not yet know how to handle dynamic
10764 arrays properly, treat them as arrays with unspecified
10765 length for now.
10766
10767 FIXME: jimb/2003-09-22: GDB does not really know
10768 how to handle arrays of unspecified length
10769 either; we just represent them as zero-length
10770 arrays. Choose an appropriate upper bound given
10771 the lower bound we've computed above. */
10772 high = low - 1;
10773 }
10774 else
10775 high = dwarf2_get_attr_constant_value (attr, 1);
10776 }
e77813c8
PM
10777 else
10778 {
10779 attr = dwarf2_attr (die, DW_AT_count, cu);
10780 if (attr)
10781 {
10782 int count = dwarf2_get_attr_constant_value (attr, 1);
10783 high = low + count - 1;
10784 }
c2ff108b
JK
10785 else
10786 {
10787 /* Unspecified array length. */
10788 high = low - 1;
10789 }
e77813c8
PM
10790 }
10791
10792 /* Dwarf-2 specifications explicitly allows to create subrange types
10793 without specifying a base type.
10794 In that case, the base type must be set to the type of
10795 the lower bound, upper bound or count, in that order, if any of these
10796 three attributes references an object that has a type.
10797 If no base type is found, the Dwarf-2 specifications say that
10798 a signed integer type of size equal to the size of an address should
10799 be used.
10800 For the following C code: `extern char gdb_int [];'
10801 GCC produces an empty range DIE.
10802 FIXME: muller/2010-05-28: Possible references to object for low bound,
0963b4bd 10803 high bound or count are not yet handled by this code. */
e77813c8
PM
10804 if (TYPE_CODE (base_type) == TYPE_CODE_VOID)
10805 {
10806 struct objfile *objfile = cu->objfile;
10807 struct gdbarch *gdbarch = get_objfile_arch (objfile);
10808 int addr_size = gdbarch_addr_bit (gdbarch) /8;
10809 struct type *int_type = objfile_type (objfile)->builtin_int;
10810
10811 /* Test "int", "long int", and "long long int" objfile types,
10812 and select the first one having a size above or equal to the
10813 architecture address size. */
10814 if (int_type && TYPE_LENGTH (int_type) >= addr_size)
10815 base_type = int_type;
10816 else
10817 {
10818 int_type = objfile_type (objfile)->builtin_long;
10819 if (int_type && TYPE_LENGTH (int_type) >= addr_size)
10820 base_type = int_type;
10821 else
10822 {
10823 int_type = objfile_type (objfile)->builtin_long_long;
10824 if (int_type && TYPE_LENGTH (int_type) >= addr_size)
10825 base_type = int_type;
10826 }
10827 }
10828 }
a02abb62 10829
6e70227d 10830 negative_mask =
43bbcdc2
PH
10831 (LONGEST) -1 << (TYPE_LENGTH (base_type) * TARGET_CHAR_BIT - 1);
10832 if (!TYPE_UNSIGNED (base_type) && (low & negative_mask))
10833 low |= negative_mask;
10834 if (!TYPE_UNSIGNED (base_type) && (high & negative_mask))
10835 high |= negative_mask;
10836
a02abb62
JB
10837 range_type = create_range_type (NULL, base_type, low, high);
10838
bbb0eef6
JK
10839 /* Mark arrays with dynamic length at least as an array of unspecified
10840 length. GDB could check the boundary but before it gets implemented at
10841 least allow accessing the array elements. */
d48323d8 10842 if (attr && attr_form_is_block (attr))
bbb0eef6
JK
10843 TYPE_HIGH_BOUND_UNDEFINED (range_type) = 1;
10844
c2ff108b
JK
10845 /* Ada expects an empty array on no boundary attributes. */
10846 if (attr == NULL && cu->language != language_ada)
10847 TYPE_HIGH_BOUND_UNDEFINED (range_type) = 1;
10848
39cbfefa
DJ
10849 name = dwarf2_name (die, cu);
10850 if (name)
10851 TYPE_NAME (range_type) = name;
6e70227d 10852
e142c38c 10853 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
a02abb62
JB
10854 if (attr)
10855 TYPE_LENGTH (range_type) = DW_UNSND (attr);
10856
7e314c57
JK
10857 set_die_type (die, range_type, cu);
10858
10859 /* set_die_type should be already done. */
b4ba55a1
JB
10860 set_descriptive_type (range_type, die, cu);
10861
7e314c57 10862 return range_type;
a02abb62 10863}
6e70227d 10864
f792889a 10865static struct type *
81a17f79
JB
10866read_unspecified_type (struct die_info *die, struct dwarf2_cu *cu)
10867{
10868 struct type *type;
81a17f79 10869
81a17f79
JB
10870 /* For now, we only support the C meaning of an unspecified type: void. */
10871
0114d602
DJ
10872 type = init_type (TYPE_CODE_VOID, 0, 0, NULL, cu->objfile);
10873 TYPE_NAME (type) = dwarf2_name (die, cu);
81a17f79 10874
f792889a 10875 return set_die_type (die, type, cu);
81a17f79 10876}
a02abb62 10877
639d11d3
DC
10878/* Read a single die and all its descendents. Set the die's sibling
10879 field to NULL; set other fields in the die correctly, and set all
10880 of the descendents' fields correctly. Set *NEW_INFO_PTR to the
10881 location of the info_ptr after reading all of those dies. PARENT
10882 is the parent of the die in question. */
10883
10884static struct die_info *
dee91e82
DE
10885read_die_and_children (const struct die_reader_specs *reader,
10886 gdb_byte *info_ptr,
10887 gdb_byte **new_info_ptr,
10888 struct die_info *parent)
639d11d3
DC
10889{
10890 struct die_info *die;
fe1b8b76 10891 gdb_byte *cur_ptr;
639d11d3
DC
10892 int has_children;
10893
93311388 10894 cur_ptr = read_full_die (reader, &die, info_ptr, &has_children);
1d325ec1
DJ
10895 if (die == NULL)
10896 {
10897 *new_info_ptr = cur_ptr;
10898 return NULL;
10899 }
93311388 10900 store_in_ref_table (die, reader->cu);
639d11d3
DC
10901
10902 if (has_children)
348e048f 10903 die->child = read_die_and_siblings (reader, cur_ptr, new_info_ptr, die);
639d11d3
DC
10904 else
10905 {
10906 die->child = NULL;
10907 *new_info_ptr = cur_ptr;
10908 }
10909
10910 die->sibling = NULL;
10911 die->parent = parent;
10912 return die;
10913}
10914
10915/* Read a die, all of its descendents, and all of its siblings; set
10916 all of the fields of all of the dies correctly. Arguments are as
10917 in read_die_and_children. */
10918
10919static struct die_info *
93311388
DE
10920read_die_and_siblings (const struct die_reader_specs *reader,
10921 gdb_byte *info_ptr,
fe1b8b76 10922 gdb_byte **new_info_ptr,
639d11d3
DC
10923 struct die_info *parent)
10924{
10925 struct die_info *first_die, *last_sibling;
fe1b8b76 10926 gdb_byte *cur_ptr;
639d11d3 10927
c906108c 10928 cur_ptr = info_ptr;
639d11d3
DC
10929 first_die = last_sibling = NULL;
10930
10931 while (1)
c906108c 10932 {
639d11d3 10933 struct die_info *die
dee91e82 10934 = read_die_and_children (reader, cur_ptr, &cur_ptr, parent);
639d11d3 10935
1d325ec1 10936 if (die == NULL)
c906108c 10937 {
639d11d3
DC
10938 *new_info_ptr = cur_ptr;
10939 return first_die;
c906108c 10940 }
1d325ec1
DJ
10941
10942 if (!first_die)
10943 first_die = die;
c906108c 10944 else
1d325ec1
DJ
10945 last_sibling->sibling = die;
10946
10947 last_sibling = die;
c906108c 10948 }
c906108c
SS
10949}
10950
3019eac3
DE
10951/* Read a die and all its attributes, leave space for NUM_EXTRA_ATTRS
10952 attributes.
10953 The caller is responsible for filling in the extra attributes
10954 and updating (*DIEP)->num_attrs.
10955 Set DIEP to point to a newly allocated die with its information,
10956 except for its child, sibling, and parent fields.
10957 Set HAS_CHILDREN to tell whether the die has children or not. */
93311388
DE
10958
10959static gdb_byte *
3019eac3
DE
10960read_full_die_1 (const struct die_reader_specs *reader,
10961 struct die_info **diep, gdb_byte *info_ptr,
10962 int *has_children, int num_extra_attrs)
93311388 10963{
b64f50a1
JK
10964 unsigned int abbrev_number, bytes_read, i;
10965 sect_offset offset;
93311388
DE
10966 struct abbrev_info *abbrev;
10967 struct die_info *die;
10968 struct dwarf2_cu *cu = reader->cu;
10969 bfd *abfd = reader->abfd;
10970
b64f50a1 10971 offset.sect_off = info_ptr - reader->buffer;
93311388
DE
10972 abbrev_number = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
10973 info_ptr += bytes_read;
10974 if (!abbrev_number)
10975 {
10976 *diep = NULL;
10977 *has_children = 0;
10978 return info_ptr;
10979 }
10980
433df2d4 10981 abbrev = abbrev_table_lookup_abbrev (cu->abbrev_table, abbrev_number);
93311388 10982 if (!abbrev)
348e048f
DE
10983 error (_("Dwarf Error: could not find abbrev number %d [in module %s]"),
10984 abbrev_number,
10985 bfd_get_filename (abfd));
10986
3019eac3 10987 die = dwarf_alloc_die (cu, abbrev->num_attrs + num_extra_attrs);
93311388
DE
10988 die->offset = offset;
10989 die->tag = abbrev->tag;
10990 die->abbrev = abbrev_number;
10991
3019eac3
DE
10992 /* Make the result usable.
10993 The caller needs to update num_attrs after adding the extra
10994 attributes. */
93311388
DE
10995 die->num_attrs = abbrev->num_attrs;
10996
10997 for (i = 0; i < abbrev->num_attrs; ++i)
dee91e82
DE
10998 info_ptr = read_attribute (reader, &die->attrs[i], &abbrev->attrs[i],
10999 info_ptr);
93311388
DE
11000
11001 *diep = die;
11002 *has_children = abbrev->has_children;
11003 return info_ptr;
11004}
11005
3019eac3
DE
11006/* Read a die and all its attributes.
11007 Set DIEP to point to a newly allocated die with its information,
11008 except for its child, sibling, and parent fields.
11009 Set HAS_CHILDREN to tell whether the die has children or not. */
11010
11011static gdb_byte *
11012read_full_die (const struct die_reader_specs *reader,
11013 struct die_info **diep, gdb_byte *info_ptr,
11014 int *has_children)
11015{
11016 return read_full_die_1 (reader, diep, info_ptr, has_children, 0);
11017}
433df2d4
DE
11018\f
11019/* Abbreviation tables.
3019eac3 11020
433df2d4 11021 In DWARF version 2, the description of the debugging information is
c906108c
SS
11022 stored in a separate .debug_abbrev section. Before we read any
11023 dies from a section we read in all abbreviations and install them
433df2d4
DE
11024 in a hash table. */
11025
11026/* Allocate space for a struct abbrev_info object in ABBREV_TABLE. */
11027
11028static struct abbrev_info *
11029abbrev_table_alloc_abbrev (struct abbrev_table *abbrev_table)
11030{
11031 struct abbrev_info *abbrev;
11032
11033 abbrev = (struct abbrev_info *)
11034 obstack_alloc (&abbrev_table->abbrev_obstack, sizeof (struct abbrev_info));
11035 memset (abbrev, 0, sizeof (struct abbrev_info));
11036 return abbrev;
11037}
11038
11039/* Add an abbreviation to the table. */
c906108c
SS
11040
11041static void
433df2d4
DE
11042abbrev_table_add_abbrev (struct abbrev_table *abbrev_table,
11043 unsigned int abbrev_number,
11044 struct abbrev_info *abbrev)
11045{
11046 unsigned int hash_number;
11047
11048 hash_number = abbrev_number % ABBREV_HASH_SIZE;
11049 abbrev->next = abbrev_table->abbrevs[hash_number];
11050 abbrev_table->abbrevs[hash_number] = abbrev;
11051}
dee91e82 11052
433df2d4
DE
11053/* Look up an abbrev in the table.
11054 Returns NULL if the abbrev is not found. */
11055
11056static struct abbrev_info *
11057abbrev_table_lookup_abbrev (const struct abbrev_table *abbrev_table,
11058 unsigned int abbrev_number)
c906108c 11059{
433df2d4
DE
11060 unsigned int hash_number;
11061 struct abbrev_info *abbrev;
11062
11063 hash_number = abbrev_number % ABBREV_HASH_SIZE;
11064 abbrev = abbrev_table->abbrevs[hash_number];
11065
11066 while (abbrev)
11067 {
11068 if (abbrev->number == abbrev_number)
11069 return abbrev;
11070 abbrev = abbrev->next;
11071 }
11072 return NULL;
11073}
11074
11075/* Read in an abbrev table. */
11076
11077static struct abbrev_table *
11078abbrev_table_read_table (struct dwarf2_section_info *section,
11079 sect_offset offset)
11080{
11081 struct objfile *objfile = dwarf2_per_objfile->objfile;
11082 bfd *abfd = section->asection->owner;
11083 struct abbrev_table *abbrev_table;
fe1b8b76 11084 gdb_byte *abbrev_ptr;
c906108c
SS
11085 struct abbrev_info *cur_abbrev;
11086 unsigned int abbrev_number, bytes_read, abbrev_name;
433df2d4 11087 unsigned int abbrev_form;
f3dd6933
DJ
11088 struct attr_abbrev *cur_attrs;
11089 unsigned int allocated_attrs;
c906108c 11090
433df2d4
DE
11091 abbrev_table = XMALLOC (struct abbrev_table);
11092 obstack_init (&abbrev_table->abbrev_obstack);
11093 abbrev_table->abbrevs = obstack_alloc (&abbrev_table->abbrev_obstack,
11094 (ABBREV_HASH_SIZE
11095 * sizeof (struct abbrev_info *)));
11096 memset (abbrev_table->abbrevs, 0,
11097 ABBREV_HASH_SIZE * sizeof (struct abbrev_info *));
c906108c 11098
433df2d4
DE
11099 dwarf2_read_section (objfile, section);
11100 abbrev_ptr = section->buffer + offset.sect_off;
c906108c
SS
11101 abbrev_number = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
11102 abbrev_ptr += bytes_read;
11103
f3dd6933
DJ
11104 allocated_attrs = ATTR_ALLOC_CHUNK;
11105 cur_attrs = xmalloc (allocated_attrs * sizeof (struct attr_abbrev));
6e70227d 11106
0963b4bd 11107 /* Loop until we reach an abbrev number of 0. */
c906108c
SS
11108 while (abbrev_number)
11109 {
433df2d4 11110 cur_abbrev = abbrev_table_alloc_abbrev (abbrev_table);
c906108c
SS
11111
11112 /* read in abbrev header */
11113 cur_abbrev->number = abbrev_number;
11114 cur_abbrev->tag = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
11115 abbrev_ptr += bytes_read;
11116 cur_abbrev->has_children = read_1_byte (abfd, abbrev_ptr);
11117 abbrev_ptr += 1;
11118
11119 /* now read in declarations */
11120 abbrev_name = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
11121 abbrev_ptr += bytes_read;
11122 abbrev_form = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
11123 abbrev_ptr += bytes_read;
11124 while (abbrev_name)
11125 {
f3dd6933 11126 if (cur_abbrev->num_attrs == allocated_attrs)
c906108c 11127 {
f3dd6933
DJ
11128 allocated_attrs += ATTR_ALLOC_CHUNK;
11129 cur_attrs
11130 = xrealloc (cur_attrs, (allocated_attrs
11131 * sizeof (struct attr_abbrev)));
c906108c 11132 }
ae038cb0 11133
f3dd6933
DJ
11134 cur_attrs[cur_abbrev->num_attrs].name = abbrev_name;
11135 cur_attrs[cur_abbrev->num_attrs++].form = abbrev_form;
c906108c
SS
11136 abbrev_name = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
11137 abbrev_ptr += bytes_read;
11138 abbrev_form = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
11139 abbrev_ptr += bytes_read;
11140 }
11141
433df2d4 11142 cur_abbrev->attrs = obstack_alloc (&abbrev_table->abbrev_obstack,
f3dd6933
DJ
11143 (cur_abbrev->num_attrs
11144 * sizeof (struct attr_abbrev)));
11145 memcpy (cur_abbrev->attrs, cur_attrs,
11146 cur_abbrev->num_attrs * sizeof (struct attr_abbrev));
11147
433df2d4 11148 abbrev_table_add_abbrev (abbrev_table, abbrev_number, cur_abbrev);
c906108c
SS
11149
11150 /* Get next abbreviation.
11151 Under Irix6 the abbreviations for a compilation unit are not
c5aa993b
JM
11152 always properly terminated with an abbrev number of 0.
11153 Exit loop if we encounter an abbreviation which we have
11154 already read (which means we are about to read the abbreviations
11155 for the next compile unit) or if the end of the abbreviation
11156 table is reached. */
433df2d4 11157 if ((unsigned int) (abbrev_ptr - section->buffer) >= section->size)
c906108c
SS
11158 break;
11159 abbrev_number = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
11160 abbrev_ptr += bytes_read;
433df2d4 11161 if (abbrev_table_lookup_abbrev (abbrev_table, abbrev_number) != NULL)
c906108c
SS
11162 break;
11163 }
f3dd6933
DJ
11164
11165 xfree (cur_attrs);
433df2d4 11166 return abbrev_table;
c906108c
SS
11167}
11168
433df2d4 11169/* Free the resources held by ABBREV_TABLE. */
c906108c 11170
c906108c 11171static void
433df2d4 11172abbrev_table_free (struct abbrev_table *abbrev_table)
c906108c 11173{
433df2d4
DE
11174 obstack_free (&abbrev_table->abbrev_obstack, NULL);
11175 xfree (abbrev_table);
c906108c
SS
11176}
11177
433df2d4
DE
11178/* Read the abbrev table for CU from ABBREV_SECTION. */
11179
11180static void
11181dwarf2_read_abbrevs (struct dwarf2_cu *cu,
11182 struct dwarf2_section_info *abbrev_section)
c906108c 11183{
433df2d4
DE
11184 cu->abbrev_table =
11185 abbrev_table_read_table (abbrev_section, cu->header.abbrev_offset);
11186}
c906108c 11187
433df2d4 11188/* Release the memory used by the abbrev table for a compilation unit. */
c906108c 11189
433df2d4
DE
11190static void
11191dwarf2_free_abbrev_table (void *ptr_to_cu)
11192{
11193 struct dwarf2_cu *cu = ptr_to_cu;
c906108c 11194
433df2d4
DE
11195 abbrev_table_free (cu->abbrev_table);
11196 /* Set this to NULL so that we SEGV if we try to read it later,
11197 and also because free_comp_unit verifies this is NULL. */
11198 cu->abbrev_table = NULL;
11199}
11200\f
72bf9492
DJ
11201/* Returns nonzero if TAG represents a type that we might generate a partial
11202 symbol for. */
11203
11204static int
11205is_type_tag_for_partial (int tag)
11206{
11207 switch (tag)
11208 {
11209#if 0
11210 /* Some types that would be reasonable to generate partial symbols for,
11211 that we don't at present. */
11212 case DW_TAG_array_type:
11213 case DW_TAG_file_type:
11214 case DW_TAG_ptr_to_member_type:
11215 case DW_TAG_set_type:
11216 case DW_TAG_string_type:
11217 case DW_TAG_subroutine_type:
11218#endif
11219 case DW_TAG_base_type:
11220 case DW_TAG_class_type:
680b30c7 11221 case DW_TAG_interface_type:
72bf9492
DJ
11222 case DW_TAG_enumeration_type:
11223 case DW_TAG_structure_type:
11224 case DW_TAG_subrange_type:
11225 case DW_TAG_typedef:
11226 case DW_TAG_union_type:
11227 return 1;
11228 default:
11229 return 0;
11230 }
11231}
11232
11233/* Load all DIEs that are interesting for partial symbols into memory. */
11234
11235static struct partial_die_info *
dee91e82
DE
11236load_partial_dies (const struct die_reader_specs *reader,
11237 gdb_byte *info_ptr, int building_psymtab)
72bf9492 11238{
dee91e82 11239 struct dwarf2_cu *cu = reader->cu;
bb5ed363 11240 struct objfile *objfile = cu->objfile;
72bf9492
DJ
11241 struct partial_die_info *part_die;
11242 struct partial_die_info *parent_die, *last_die, *first_die = NULL;
11243 struct abbrev_info *abbrev;
11244 unsigned int bytes_read;
5afb4e99 11245 unsigned int load_all = 0;
72bf9492
DJ
11246 int nesting_level = 1;
11247
11248 parent_die = NULL;
11249 last_die = NULL;
11250
7adf1e79
DE
11251 gdb_assert (cu->per_cu != NULL);
11252 if (cu->per_cu->load_all_dies)
5afb4e99
DJ
11253 load_all = 1;
11254
72bf9492
DJ
11255 cu->partial_dies
11256 = htab_create_alloc_ex (cu->header.length / 12,
11257 partial_die_hash,
11258 partial_die_eq,
11259 NULL,
11260 &cu->comp_unit_obstack,
11261 hashtab_obstack_allocate,
11262 dummy_obstack_deallocate);
11263
11264 part_die = obstack_alloc (&cu->comp_unit_obstack,
11265 sizeof (struct partial_die_info));
11266
11267 while (1)
11268 {
11269 abbrev = peek_die_abbrev (info_ptr, &bytes_read, cu);
11270
11271 /* A NULL abbrev means the end of a series of children. */
11272 if (abbrev == NULL)
11273 {
11274 if (--nesting_level == 0)
11275 {
11276 /* PART_DIE was probably the last thing allocated on the
11277 comp_unit_obstack, so we could call obstack_free
11278 here. We don't do that because the waste is small,
11279 and will be cleaned up when we're done with this
11280 compilation unit. This way, we're also more robust
11281 against other users of the comp_unit_obstack. */
11282 return first_die;
11283 }
11284 info_ptr += bytes_read;
11285 last_die = parent_die;
11286 parent_die = parent_die->die_parent;
11287 continue;
11288 }
11289
98bfdba5
PA
11290 /* Check for template arguments. We never save these; if
11291 they're seen, we just mark the parent, and go on our way. */
11292 if (parent_die != NULL
11293 && cu->language == language_cplus
11294 && (abbrev->tag == DW_TAG_template_type_param
11295 || abbrev->tag == DW_TAG_template_value_param))
11296 {
11297 parent_die->has_template_arguments = 1;
11298
11299 if (!load_all)
11300 {
11301 /* We don't need a partial DIE for the template argument. */
dee91e82 11302 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
98bfdba5
PA
11303 continue;
11304 }
11305 }
11306
0d99eb77 11307 /* We only recurse into c++ subprograms looking for template arguments.
98bfdba5
PA
11308 Skip their other children. */
11309 if (!load_all
11310 && cu->language == language_cplus
11311 && parent_die != NULL
11312 && parent_die->tag == DW_TAG_subprogram)
11313 {
dee91e82 11314 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
98bfdba5
PA
11315 continue;
11316 }
11317
5afb4e99
DJ
11318 /* Check whether this DIE is interesting enough to save. Normally
11319 we would not be interested in members here, but there may be
11320 later variables referencing them via DW_AT_specification (for
11321 static members). */
11322 if (!load_all
11323 && !is_type_tag_for_partial (abbrev->tag)
72929c62 11324 && abbrev->tag != DW_TAG_constant
72bf9492
DJ
11325 && abbrev->tag != DW_TAG_enumerator
11326 && abbrev->tag != DW_TAG_subprogram
bc30ff58 11327 && abbrev->tag != DW_TAG_lexical_block
72bf9492 11328 && abbrev->tag != DW_TAG_variable
5afb4e99 11329 && abbrev->tag != DW_TAG_namespace
f55ee35c 11330 && abbrev->tag != DW_TAG_module
95554aad
TT
11331 && abbrev->tag != DW_TAG_member
11332 && abbrev->tag != DW_TAG_imported_unit)
72bf9492
DJ
11333 {
11334 /* Otherwise we skip to the next sibling, if any. */
dee91e82 11335 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
72bf9492
DJ
11336 continue;
11337 }
11338
dee91e82
DE
11339 info_ptr = read_partial_die (reader, part_die, abbrev, bytes_read,
11340 info_ptr);
72bf9492
DJ
11341
11342 /* This two-pass algorithm for processing partial symbols has a
11343 high cost in cache pressure. Thus, handle some simple cases
11344 here which cover the majority of C partial symbols. DIEs
11345 which neither have specification tags in them, nor could have
11346 specification tags elsewhere pointing at them, can simply be
11347 processed and discarded.
11348
11349 This segment is also optional; scan_partial_symbols and
11350 add_partial_symbol will handle these DIEs if we chain
11351 them in normally. When compilers which do not emit large
11352 quantities of duplicate debug information are more common,
11353 this code can probably be removed. */
11354
11355 /* Any complete simple types at the top level (pretty much all
11356 of them, for a language without namespaces), can be processed
11357 directly. */
11358 if (parent_die == NULL
11359 && part_die->has_specification == 0
11360 && part_die->is_declaration == 0
d8228535 11361 && ((part_die->tag == DW_TAG_typedef && !part_die->has_children)
72bf9492
DJ
11362 || part_die->tag == DW_TAG_base_type
11363 || part_die->tag == DW_TAG_subrange_type))
11364 {
11365 if (building_psymtab && part_die->name != NULL)
04a679b8 11366 add_psymbol_to_list (part_die->name, strlen (part_die->name), 0,
72bf9492 11367 VAR_DOMAIN, LOC_TYPEDEF,
bb5ed363
DE
11368 &objfile->static_psymbols,
11369 0, (CORE_ADDR) 0, cu->language, objfile);
dee91e82 11370 info_ptr = locate_pdi_sibling (reader, part_die, info_ptr);
72bf9492
DJ
11371 continue;
11372 }
11373
d8228535
JK
11374 /* The exception for DW_TAG_typedef with has_children above is
11375 a workaround of GCC PR debug/47510. In the case of this complaint
11376 type_name_no_tag_or_error will error on such types later.
11377
11378 GDB skipped children of DW_TAG_typedef by the shortcut above and then
11379 it could not find the child DIEs referenced later, this is checked
11380 above. In correct DWARF DW_TAG_typedef should have no children. */
11381
11382 if (part_die->tag == DW_TAG_typedef && part_die->has_children)
11383 complaint (&symfile_complaints,
11384 _("DW_TAG_typedef has childen - GCC PR debug/47510 bug "
11385 "- DIE at 0x%x [in module %s]"),
b64f50a1 11386 part_die->offset.sect_off, objfile->name);
d8228535 11387
72bf9492
DJ
11388 /* If we're at the second level, and we're an enumerator, and
11389 our parent has no specification (meaning possibly lives in a
11390 namespace elsewhere), then we can add the partial symbol now
11391 instead of queueing it. */
11392 if (part_die->tag == DW_TAG_enumerator
11393 && parent_die != NULL
11394 && parent_die->die_parent == NULL
11395 && parent_die->tag == DW_TAG_enumeration_type
11396 && parent_die->has_specification == 0)
11397 {
11398 if (part_die->name == NULL)
3e43a32a
MS
11399 complaint (&symfile_complaints,
11400 _("malformed enumerator DIE ignored"));
72bf9492 11401 else if (building_psymtab)
04a679b8 11402 add_psymbol_to_list (part_die->name, strlen (part_die->name), 0,
72bf9492 11403 VAR_DOMAIN, LOC_CONST,
987504bb
JJ
11404 (cu->language == language_cplus
11405 || cu->language == language_java)
bb5ed363
DE
11406 ? &objfile->global_psymbols
11407 : &objfile->static_psymbols,
11408 0, (CORE_ADDR) 0, cu->language, objfile);
72bf9492 11409
dee91e82 11410 info_ptr = locate_pdi_sibling (reader, part_die, info_ptr);
72bf9492
DJ
11411 continue;
11412 }
11413
11414 /* We'll save this DIE so link it in. */
11415 part_die->die_parent = parent_die;
11416 part_die->die_sibling = NULL;
11417 part_die->die_child = NULL;
11418
11419 if (last_die && last_die == parent_die)
11420 last_die->die_child = part_die;
11421 else if (last_die)
11422 last_die->die_sibling = part_die;
11423
11424 last_die = part_die;
11425
11426 if (first_die == NULL)
11427 first_die = part_die;
11428
11429 /* Maybe add the DIE to the hash table. Not all DIEs that we
11430 find interesting need to be in the hash table, because we
11431 also have the parent/sibling/child chains; only those that we
11432 might refer to by offset later during partial symbol reading.
11433
11434 For now this means things that might have be the target of a
11435 DW_AT_specification, DW_AT_abstract_origin, or
11436 DW_AT_extension. DW_AT_extension will refer only to
11437 namespaces; DW_AT_abstract_origin refers to functions (and
11438 many things under the function DIE, but we do not recurse
11439 into function DIEs during partial symbol reading) and
11440 possibly variables as well; DW_AT_specification refers to
11441 declarations. Declarations ought to have the DW_AT_declaration
11442 flag. It happens that GCC forgets to put it in sometimes, but
11443 only for functions, not for types.
11444
11445 Adding more things than necessary to the hash table is harmless
11446 except for the performance cost. Adding too few will result in
5afb4e99
DJ
11447 wasted time in find_partial_die, when we reread the compilation
11448 unit with load_all_dies set. */
72bf9492 11449
5afb4e99 11450 if (load_all
72929c62 11451 || abbrev->tag == DW_TAG_constant
5afb4e99 11452 || abbrev->tag == DW_TAG_subprogram
72bf9492
DJ
11453 || abbrev->tag == DW_TAG_variable
11454 || abbrev->tag == DW_TAG_namespace
11455 || part_die->is_declaration)
11456 {
11457 void **slot;
11458
11459 slot = htab_find_slot_with_hash (cu->partial_dies, part_die,
b64f50a1 11460 part_die->offset.sect_off, INSERT);
72bf9492
DJ
11461 *slot = part_die;
11462 }
11463
11464 part_die = obstack_alloc (&cu->comp_unit_obstack,
11465 sizeof (struct partial_die_info));
11466
11467 /* For some DIEs we want to follow their children (if any). For C
bc30ff58 11468 we have no reason to follow the children of structures; for other
98bfdba5
PA
11469 languages we have to, so that we can get at method physnames
11470 to infer fully qualified class names, for DW_AT_specification,
11471 and for C++ template arguments. For C++, we also look one level
11472 inside functions to find template arguments (if the name of the
11473 function does not already contain the template arguments).
bc30ff58
JB
11474
11475 For Ada, we need to scan the children of subprograms and lexical
11476 blocks as well because Ada allows the definition of nested
11477 entities that could be interesting for the debugger, such as
11478 nested subprograms for instance. */
72bf9492 11479 if (last_die->has_children
5afb4e99
DJ
11480 && (load_all
11481 || last_die->tag == DW_TAG_namespace
f55ee35c 11482 || last_die->tag == DW_TAG_module
72bf9492 11483 || last_die->tag == DW_TAG_enumeration_type
98bfdba5
PA
11484 || (cu->language == language_cplus
11485 && last_die->tag == DW_TAG_subprogram
11486 && (last_die->name == NULL
11487 || strchr (last_die->name, '<') == NULL))
72bf9492
DJ
11488 || (cu->language != language_c
11489 && (last_die->tag == DW_TAG_class_type
680b30c7 11490 || last_die->tag == DW_TAG_interface_type
72bf9492 11491 || last_die->tag == DW_TAG_structure_type
bc30ff58
JB
11492 || last_die->tag == DW_TAG_union_type))
11493 || (cu->language == language_ada
11494 && (last_die->tag == DW_TAG_subprogram
11495 || last_die->tag == DW_TAG_lexical_block))))
72bf9492
DJ
11496 {
11497 nesting_level++;
11498 parent_die = last_die;
11499 continue;
11500 }
11501
11502 /* Otherwise we skip to the next sibling, if any. */
dee91e82 11503 info_ptr = locate_pdi_sibling (reader, last_die, info_ptr);
72bf9492
DJ
11504
11505 /* Back to the top, do it again. */
11506 }
11507}
11508
c906108c
SS
11509/* Read a minimal amount of information into the minimal die structure. */
11510
fe1b8b76 11511static gdb_byte *
dee91e82
DE
11512read_partial_die (const struct die_reader_specs *reader,
11513 struct partial_die_info *part_die,
11514 struct abbrev_info *abbrev, unsigned int abbrev_len,
11515 gdb_byte *info_ptr)
c906108c 11516{
dee91e82 11517 struct dwarf2_cu *cu = reader->cu;
bb5ed363 11518 struct objfile *objfile = cu->objfile;
dee91e82 11519 gdb_byte *buffer = reader->buffer;
fa238c03 11520 unsigned int i;
c906108c 11521 struct attribute attr;
c5aa993b 11522 int has_low_pc_attr = 0;
c906108c 11523 int has_high_pc_attr = 0;
91da1414 11524 int high_pc_relative = 0;
c906108c 11525
72bf9492 11526 memset (part_die, 0, sizeof (struct partial_die_info));
c906108c 11527
b64f50a1 11528 part_die->offset.sect_off = info_ptr - buffer;
72bf9492
DJ
11529
11530 info_ptr += abbrev_len;
11531
11532 if (abbrev == NULL)
11533 return info_ptr;
11534
c906108c
SS
11535 part_die->tag = abbrev->tag;
11536 part_die->has_children = abbrev->has_children;
c906108c
SS
11537
11538 for (i = 0; i < abbrev->num_attrs; ++i)
11539 {
dee91e82 11540 info_ptr = read_attribute (reader, &attr, &abbrev->attrs[i], info_ptr);
c906108c
SS
11541
11542 /* Store the data if it is of an attribute we want to keep in a
c5aa993b 11543 partial symbol table. */
c906108c
SS
11544 switch (attr.name)
11545 {
11546 case DW_AT_name:
71c25dea
TT
11547 switch (part_die->tag)
11548 {
11549 case DW_TAG_compile_unit:
95554aad 11550 case DW_TAG_partial_unit:
348e048f 11551 case DW_TAG_type_unit:
71c25dea
TT
11552 /* Compilation units have a DW_AT_name that is a filename, not
11553 a source language identifier. */
11554 case DW_TAG_enumeration_type:
11555 case DW_TAG_enumerator:
11556 /* These tags always have simple identifiers already; no need
11557 to canonicalize them. */
11558 part_die->name = DW_STRING (&attr);
11559 break;
11560 default:
11561 part_die->name
11562 = dwarf2_canonicalize_name (DW_STRING (&attr), cu,
bb5ed363 11563 &objfile->objfile_obstack);
71c25dea
TT
11564 break;
11565 }
c906108c 11566 break;
31ef98ae 11567 case DW_AT_linkage_name:
c906108c 11568 case DW_AT_MIPS_linkage_name:
31ef98ae
TT
11569 /* Note that both forms of linkage name might appear. We
11570 assume they will be the same, and we only store the last
11571 one we see. */
94af9270
KS
11572 if (cu->language == language_ada)
11573 part_die->name = DW_STRING (&attr);
abc72ce4 11574 part_die->linkage_name = DW_STRING (&attr);
c906108c
SS
11575 break;
11576 case DW_AT_low_pc:
11577 has_low_pc_attr = 1;
11578 part_die->lowpc = DW_ADDR (&attr);
11579 break;
11580 case DW_AT_high_pc:
11581 has_high_pc_attr = 1;
3019eac3
DE
11582 if (attr.form == DW_FORM_addr
11583 || attr.form == DW_FORM_GNU_addr_index)
91da1414
MW
11584 part_die->highpc = DW_ADDR (&attr);
11585 else
11586 {
11587 high_pc_relative = 1;
11588 part_die->highpc = DW_UNSND (&attr);
11589 }
c906108c
SS
11590 break;
11591 case DW_AT_location:
0963b4bd 11592 /* Support the .debug_loc offsets. */
8e19ed76
PS
11593 if (attr_form_is_block (&attr))
11594 {
95554aad 11595 part_die->d.locdesc = DW_BLOCK (&attr);
8e19ed76 11596 }
3690dd37 11597 else if (attr_form_is_section_offset (&attr))
8e19ed76 11598 {
4d3c2250 11599 dwarf2_complex_location_expr_complaint ();
8e19ed76
PS
11600 }
11601 else
11602 {
4d3c2250
KB
11603 dwarf2_invalid_attrib_class_complaint ("DW_AT_location",
11604 "partial symbol information");
8e19ed76 11605 }
c906108c 11606 break;
c906108c
SS
11607 case DW_AT_external:
11608 part_die->is_external = DW_UNSND (&attr);
11609 break;
11610 case DW_AT_declaration:
11611 part_die->is_declaration = DW_UNSND (&attr);
11612 break;
11613 case DW_AT_type:
11614 part_die->has_type = 1;
11615 break;
11616 case DW_AT_abstract_origin:
11617 case DW_AT_specification:
72bf9492
DJ
11618 case DW_AT_extension:
11619 part_die->has_specification = 1;
c764a876 11620 part_die->spec_offset = dwarf2_get_ref_die_offset (&attr);
c906108c
SS
11621 break;
11622 case DW_AT_sibling:
11623 /* Ignore absolute siblings, they might point outside of
11624 the current compile unit. */
11625 if (attr.form == DW_FORM_ref_addr)
3e43a32a
MS
11626 complaint (&symfile_complaints,
11627 _("ignoring absolute DW_AT_sibling"));
c906108c 11628 else
b64f50a1 11629 part_die->sibling = buffer + dwarf2_get_ref_die_offset (&attr).sect_off;
c906108c 11630 break;
fa4028e9
JB
11631 case DW_AT_byte_size:
11632 part_die->has_byte_size = 1;
11633 break;
68511cec
CES
11634 case DW_AT_calling_convention:
11635 /* DWARF doesn't provide a way to identify a program's source-level
11636 entry point. DW_AT_calling_convention attributes are only meant
11637 to describe functions' calling conventions.
11638
11639 However, because it's a necessary piece of information in
11640 Fortran, and because DW_CC_program is the only piece of debugging
11641 information whose definition refers to a 'main program' at all,
11642 several compilers have begun marking Fortran main programs with
11643 DW_CC_program --- even when those functions use the standard
11644 calling conventions.
11645
11646 So until DWARF specifies a way to provide this information and
11647 compilers pick up the new representation, we'll support this
11648 practice. */
11649 if (DW_UNSND (&attr) == DW_CC_program
11650 && cu->language == language_fortran)
01f8c46d
JK
11651 {
11652 set_main_name (part_die->name);
11653
11654 /* As this DIE has a static linkage the name would be difficult
11655 to look up later. */
11656 language_of_main = language_fortran;
11657 }
68511cec 11658 break;
481860b3
GB
11659 case DW_AT_inline:
11660 if (DW_UNSND (&attr) == DW_INL_inlined
11661 || DW_UNSND (&attr) == DW_INL_declared_inlined)
11662 part_die->may_be_inlined = 1;
11663 break;
95554aad
TT
11664
11665 case DW_AT_import:
11666 if (part_die->tag == DW_TAG_imported_unit)
11667 part_die->d.offset = dwarf2_get_ref_die_offset (&attr);
11668 break;
11669
c906108c
SS
11670 default:
11671 break;
11672 }
11673 }
11674
91da1414
MW
11675 if (high_pc_relative)
11676 part_die->highpc += part_die->lowpc;
11677
9373cf26
JK
11678 if (has_low_pc_attr && has_high_pc_attr)
11679 {
11680 /* When using the GNU linker, .gnu.linkonce. sections are used to
11681 eliminate duplicate copies of functions and vtables and such.
11682 The linker will arbitrarily choose one and discard the others.
11683 The AT_*_pc values for such functions refer to local labels in
11684 these sections. If the section from that file was discarded, the
11685 labels are not in the output, so the relocs get a value of 0.
11686 If this is a discarded function, mark the pc bounds as invalid,
11687 so that GDB will ignore it. */
11688 if (part_die->lowpc == 0 && !dwarf2_per_objfile->has_section_at_zero)
11689 {
bb5ed363 11690 struct gdbarch *gdbarch = get_objfile_arch (objfile);
9373cf26
JK
11691
11692 complaint (&symfile_complaints,
11693 _("DW_AT_low_pc %s is zero "
11694 "for DIE at 0x%x [in module %s]"),
11695 paddress (gdbarch, part_die->lowpc),
b64f50a1 11696 part_die->offset.sect_off, objfile->name);
9373cf26
JK
11697 }
11698 /* dwarf2_get_pc_bounds has also the strict low < high requirement. */
11699 else if (part_die->lowpc >= part_die->highpc)
11700 {
bb5ed363 11701 struct gdbarch *gdbarch = get_objfile_arch (objfile);
9373cf26
JK
11702
11703 complaint (&symfile_complaints,
11704 _("DW_AT_low_pc %s is not < DW_AT_high_pc %s "
11705 "for DIE at 0x%x [in module %s]"),
11706 paddress (gdbarch, part_die->lowpc),
11707 paddress (gdbarch, part_die->highpc),
b64f50a1 11708 part_die->offset.sect_off, objfile->name);
9373cf26
JK
11709 }
11710 else
11711 part_die->has_pc_info = 1;
11712 }
85cbf3d3 11713
c906108c
SS
11714 return info_ptr;
11715}
11716
72bf9492
DJ
11717/* Find a cached partial DIE at OFFSET in CU. */
11718
11719static struct partial_die_info *
b64f50a1 11720find_partial_die_in_comp_unit (sect_offset offset, struct dwarf2_cu *cu)
72bf9492
DJ
11721{
11722 struct partial_die_info *lookup_die = NULL;
11723 struct partial_die_info part_die;
11724
11725 part_die.offset = offset;
b64f50a1
JK
11726 lookup_die = htab_find_with_hash (cu->partial_dies, &part_die,
11727 offset.sect_off);
72bf9492 11728
72bf9492
DJ
11729 return lookup_die;
11730}
11731
348e048f
DE
11732/* Find a partial DIE at OFFSET, which may or may not be in CU,
11733 except in the case of .debug_types DIEs which do not reference
11734 outside their CU (they do however referencing other types via
55f1336d 11735 DW_FORM_ref_sig8). */
72bf9492
DJ
11736
11737static struct partial_die_info *
b64f50a1 11738find_partial_die (sect_offset offset, struct dwarf2_cu *cu)
72bf9492 11739{
bb5ed363 11740 struct objfile *objfile = cu->objfile;
5afb4e99
DJ
11741 struct dwarf2_per_cu_data *per_cu = NULL;
11742 struct partial_die_info *pd = NULL;
72bf9492 11743
45452591 11744 if (offset_in_cu_p (&cu->header, offset))
5afb4e99
DJ
11745 {
11746 pd = find_partial_die_in_comp_unit (offset, cu);
11747 if (pd != NULL)
11748 return pd;
0d99eb77
DE
11749 /* We missed recording what we needed.
11750 Load all dies and try again. */
11751 per_cu = cu->per_cu;
5afb4e99 11752 }
0d99eb77
DE
11753 else
11754 {
11755 /* TUs don't reference other CUs/TUs (except via type signatures). */
3019eac3 11756 if (cu->per_cu->is_debug_types)
0d99eb77
DE
11757 {
11758 error (_("Dwarf Error: Type Unit at offset 0x%lx contains"
11759 " external reference to offset 0x%lx [in module %s].\n"),
11760 (long) cu->header.offset.sect_off, (long) offset.sect_off,
11761 bfd_get_filename (objfile->obfd));
11762 }
11763 per_cu = dwarf2_find_containing_comp_unit (offset, objfile);
72bf9492 11764
0d99eb77
DE
11765 if (per_cu->cu == NULL || per_cu->cu->partial_dies == NULL)
11766 load_partial_comp_unit (per_cu);
ae038cb0 11767
0d99eb77
DE
11768 per_cu->cu->last_used = 0;
11769 pd = find_partial_die_in_comp_unit (offset, per_cu->cu);
11770 }
5afb4e99 11771
dee91e82
DE
11772 /* If we didn't find it, and not all dies have been loaded,
11773 load them all and try again. */
11774
5afb4e99
DJ
11775 if (pd == NULL && per_cu->load_all_dies == 0)
11776 {
5afb4e99 11777 per_cu->load_all_dies = 1;
fd820528
DE
11778
11779 /* This is nasty. When we reread the DIEs, somewhere up the call chain
11780 THIS_CU->cu may already be in use. So we can't just free it and
11781 replace its DIEs with the ones we read in. Instead, we leave those
11782 DIEs alone (which can still be in use, e.g. in scan_partial_symbols),
11783 and clobber THIS_CU->cu->partial_dies with the hash table for the new
11784 set. */
dee91e82 11785 load_partial_comp_unit (per_cu);
5afb4e99
DJ
11786
11787 pd = find_partial_die_in_comp_unit (offset, per_cu->cu);
11788 }
11789
11790 if (pd == NULL)
11791 internal_error (__FILE__, __LINE__,
3e43a32a
MS
11792 _("could not find partial DIE 0x%x "
11793 "in cache [from module %s]\n"),
b64f50a1 11794 offset.sect_off, bfd_get_filename (objfile->obfd));
5afb4e99 11795 return pd;
72bf9492
DJ
11796}
11797
abc72ce4
DE
11798/* See if we can figure out if the class lives in a namespace. We do
11799 this by looking for a member function; its demangled name will
11800 contain namespace info, if there is any. */
11801
11802static void
11803guess_partial_die_structure_name (struct partial_die_info *struct_pdi,
11804 struct dwarf2_cu *cu)
11805{
11806 /* NOTE: carlton/2003-10-07: Getting the info this way changes
11807 what template types look like, because the demangler
11808 frequently doesn't give the same name as the debug info. We
11809 could fix this by only using the demangled name to get the
11810 prefix (but see comment in read_structure_type). */
11811
11812 struct partial_die_info *real_pdi;
11813 struct partial_die_info *child_pdi;
11814
11815 /* If this DIE (this DIE's specification, if any) has a parent, then
11816 we should not do this. We'll prepend the parent's fully qualified
11817 name when we create the partial symbol. */
11818
11819 real_pdi = struct_pdi;
11820 while (real_pdi->has_specification)
11821 real_pdi = find_partial_die (real_pdi->spec_offset, cu);
11822
11823 if (real_pdi->die_parent != NULL)
11824 return;
11825
11826 for (child_pdi = struct_pdi->die_child;
11827 child_pdi != NULL;
11828 child_pdi = child_pdi->die_sibling)
11829 {
11830 if (child_pdi->tag == DW_TAG_subprogram
11831 && child_pdi->linkage_name != NULL)
11832 {
11833 char *actual_class_name
11834 = language_class_name_from_physname (cu->language_defn,
11835 child_pdi->linkage_name);
11836 if (actual_class_name != NULL)
11837 {
11838 struct_pdi->name
11839 = obsavestring (actual_class_name,
11840 strlen (actual_class_name),
11841 &cu->objfile->objfile_obstack);
11842 xfree (actual_class_name);
11843 }
11844 break;
11845 }
11846 }
11847}
11848
72bf9492
DJ
11849/* Adjust PART_DIE before generating a symbol for it. This function
11850 may set the is_external flag or change the DIE's name. */
11851
11852static void
11853fixup_partial_die (struct partial_die_info *part_die,
11854 struct dwarf2_cu *cu)
11855{
abc72ce4
DE
11856 /* Once we've fixed up a die, there's no point in doing so again.
11857 This also avoids a memory leak if we were to call
11858 guess_partial_die_structure_name multiple times. */
11859 if (part_die->fixup_called)
11860 return;
11861
72bf9492
DJ
11862 /* If we found a reference attribute and the DIE has no name, try
11863 to find a name in the referred to DIE. */
11864
11865 if (part_die->name == NULL && part_die->has_specification)
11866 {
11867 struct partial_die_info *spec_die;
72bf9492 11868
10b3939b 11869 spec_die = find_partial_die (part_die->spec_offset, cu);
72bf9492 11870
10b3939b 11871 fixup_partial_die (spec_die, cu);
72bf9492
DJ
11872
11873 if (spec_die->name)
11874 {
11875 part_die->name = spec_die->name;
11876
11877 /* Copy DW_AT_external attribute if it is set. */
11878 if (spec_die->is_external)
11879 part_die->is_external = spec_die->is_external;
11880 }
11881 }
11882
11883 /* Set default names for some unnamed DIEs. */
72bf9492
DJ
11884
11885 if (part_die->name == NULL && part_die->tag == DW_TAG_namespace)
2b1dbab0 11886 part_die->name = CP_ANONYMOUS_NAMESPACE_STR;
72bf9492 11887
abc72ce4
DE
11888 /* If there is no parent die to provide a namespace, and there are
11889 children, see if we can determine the namespace from their linkage
122d1940 11890 name. */
abc72ce4 11891 if (cu->language == language_cplus
8b70b953 11892 && !VEC_empty (dwarf2_section_info_def, dwarf2_per_objfile->types)
abc72ce4
DE
11893 && part_die->die_parent == NULL
11894 && part_die->has_children
11895 && (part_die->tag == DW_TAG_class_type
11896 || part_die->tag == DW_TAG_structure_type
11897 || part_die->tag == DW_TAG_union_type))
11898 guess_partial_die_structure_name (part_die, cu);
11899
53832f31
TT
11900 /* GCC might emit a nameless struct or union that has a linkage
11901 name. See http://gcc.gnu.org/bugzilla/show_bug.cgi?id=47510. */
11902 if (part_die->name == NULL
96408a79
SA
11903 && (part_die->tag == DW_TAG_class_type
11904 || part_die->tag == DW_TAG_interface_type
11905 || part_die->tag == DW_TAG_structure_type
11906 || part_die->tag == DW_TAG_union_type)
53832f31
TT
11907 && part_die->linkage_name != NULL)
11908 {
11909 char *demangled;
11910
11911 demangled = cplus_demangle (part_die->linkage_name, DMGL_TYPES);
11912 if (demangled)
11913 {
96408a79
SA
11914 const char *base;
11915
11916 /* Strip any leading namespaces/classes, keep only the base name.
11917 DW_AT_name for named DIEs does not contain the prefixes. */
11918 base = strrchr (demangled, ':');
11919 if (base && base > demangled && base[-1] == ':')
11920 base++;
11921 else
11922 base = demangled;
11923
11924 part_die->name = obsavestring (base, strlen (base),
53832f31
TT
11925 &cu->objfile->objfile_obstack);
11926 xfree (demangled);
11927 }
11928 }
11929
abc72ce4 11930 part_die->fixup_called = 1;
72bf9492
DJ
11931}
11932
a8329558 11933/* Read an attribute value described by an attribute form. */
c906108c 11934
fe1b8b76 11935static gdb_byte *
dee91e82
DE
11936read_attribute_value (const struct die_reader_specs *reader,
11937 struct attribute *attr, unsigned form,
11938 gdb_byte *info_ptr)
c906108c 11939{
dee91e82
DE
11940 struct dwarf2_cu *cu = reader->cu;
11941 bfd *abfd = reader->abfd;
e7c27a73 11942 struct comp_unit_head *cu_header = &cu->header;
c906108c
SS
11943 unsigned int bytes_read;
11944 struct dwarf_block *blk;
11945
a8329558
KW
11946 attr->form = form;
11947 switch (form)
c906108c 11948 {
c906108c 11949 case DW_FORM_ref_addr:
ae411497 11950 if (cu->header.version == 2)
4568ecf9 11951 DW_UNSND (attr) = read_address (abfd, info_ptr, cu, &bytes_read);
ae411497 11952 else
4568ecf9
DE
11953 DW_UNSND (attr) = read_offset (abfd, info_ptr,
11954 &cu->header, &bytes_read);
ae411497
TT
11955 info_ptr += bytes_read;
11956 break;
11957 case DW_FORM_addr:
e7c27a73 11958 DW_ADDR (attr) = read_address (abfd, info_ptr, cu, &bytes_read);
107d2387 11959 info_ptr += bytes_read;
c906108c
SS
11960 break;
11961 case DW_FORM_block2:
7b5a2f43 11962 blk = dwarf_alloc_block (cu);
c906108c
SS
11963 blk->size = read_2_bytes (abfd, info_ptr);
11964 info_ptr += 2;
11965 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
11966 info_ptr += blk->size;
11967 DW_BLOCK (attr) = blk;
11968 break;
11969 case DW_FORM_block4:
7b5a2f43 11970 blk = dwarf_alloc_block (cu);
c906108c
SS
11971 blk->size = read_4_bytes (abfd, info_ptr);
11972 info_ptr += 4;
11973 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
11974 info_ptr += blk->size;
11975 DW_BLOCK (attr) = blk;
11976 break;
11977 case DW_FORM_data2:
11978 DW_UNSND (attr) = read_2_bytes (abfd, info_ptr);
11979 info_ptr += 2;
11980 break;
11981 case DW_FORM_data4:
11982 DW_UNSND (attr) = read_4_bytes (abfd, info_ptr);
11983 info_ptr += 4;
11984 break;
11985 case DW_FORM_data8:
11986 DW_UNSND (attr) = read_8_bytes (abfd, info_ptr);
11987 info_ptr += 8;
11988 break;
2dc7f7b3
TT
11989 case DW_FORM_sec_offset:
11990 DW_UNSND (attr) = read_offset (abfd, info_ptr, &cu->header, &bytes_read);
11991 info_ptr += bytes_read;
11992 break;
c906108c 11993 case DW_FORM_string:
9b1c24c8 11994 DW_STRING (attr) = read_direct_string (abfd, info_ptr, &bytes_read);
8285870a 11995 DW_STRING_IS_CANONICAL (attr) = 0;
c906108c
SS
11996 info_ptr += bytes_read;
11997 break;
4bdf3d34
JJ
11998 case DW_FORM_strp:
11999 DW_STRING (attr) = read_indirect_string (abfd, info_ptr, cu_header,
12000 &bytes_read);
8285870a 12001 DW_STRING_IS_CANONICAL (attr) = 0;
4bdf3d34
JJ
12002 info_ptr += bytes_read;
12003 break;
2dc7f7b3 12004 case DW_FORM_exprloc:
c906108c 12005 case DW_FORM_block:
7b5a2f43 12006 blk = dwarf_alloc_block (cu);
c906108c
SS
12007 blk->size = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
12008 info_ptr += bytes_read;
12009 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
12010 info_ptr += blk->size;
12011 DW_BLOCK (attr) = blk;
12012 break;
12013 case DW_FORM_block1:
7b5a2f43 12014 blk = dwarf_alloc_block (cu);
c906108c
SS
12015 blk->size = read_1_byte (abfd, info_ptr);
12016 info_ptr += 1;
12017 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
12018 info_ptr += blk->size;
12019 DW_BLOCK (attr) = blk;
12020 break;
12021 case DW_FORM_data1:
12022 DW_UNSND (attr) = read_1_byte (abfd, info_ptr);
12023 info_ptr += 1;
12024 break;
12025 case DW_FORM_flag:
12026 DW_UNSND (attr) = read_1_byte (abfd, info_ptr);
12027 info_ptr += 1;
12028 break;
2dc7f7b3
TT
12029 case DW_FORM_flag_present:
12030 DW_UNSND (attr) = 1;
12031 break;
c906108c
SS
12032 case DW_FORM_sdata:
12033 DW_SND (attr) = read_signed_leb128 (abfd, info_ptr, &bytes_read);
12034 info_ptr += bytes_read;
12035 break;
12036 case DW_FORM_udata:
12037 DW_UNSND (attr) = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
12038 info_ptr += bytes_read;
12039 break;
12040 case DW_FORM_ref1:
4568ecf9
DE
12041 DW_UNSND (attr) = (cu->header.offset.sect_off
12042 + read_1_byte (abfd, info_ptr));
c906108c
SS
12043 info_ptr += 1;
12044 break;
12045 case DW_FORM_ref2:
4568ecf9
DE
12046 DW_UNSND (attr) = (cu->header.offset.sect_off
12047 + read_2_bytes (abfd, info_ptr));
c906108c
SS
12048 info_ptr += 2;
12049 break;
12050 case DW_FORM_ref4:
4568ecf9
DE
12051 DW_UNSND (attr) = (cu->header.offset.sect_off
12052 + read_4_bytes (abfd, info_ptr));
c906108c
SS
12053 info_ptr += 4;
12054 break;
613e1657 12055 case DW_FORM_ref8:
4568ecf9
DE
12056 DW_UNSND (attr) = (cu->header.offset.sect_off
12057 + read_8_bytes (abfd, info_ptr));
613e1657
KB
12058 info_ptr += 8;
12059 break;
55f1336d 12060 case DW_FORM_ref_sig8:
348e048f
DE
12061 /* Convert the signature to something we can record in DW_UNSND
12062 for later lookup.
12063 NOTE: This is NULL if the type wasn't found. */
12064 DW_SIGNATURED_TYPE (attr) =
e319fa28 12065 lookup_signatured_type (read_8_bytes (abfd, info_ptr));
348e048f
DE
12066 info_ptr += 8;
12067 break;
c906108c 12068 case DW_FORM_ref_udata:
4568ecf9
DE
12069 DW_UNSND (attr) = (cu->header.offset.sect_off
12070 + read_unsigned_leb128 (abfd, info_ptr, &bytes_read));
c906108c
SS
12071 info_ptr += bytes_read;
12072 break;
c906108c 12073 case DW_FORM_indirect:
a8329558
KW
12074 form = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
12075 info_ptr += bytes_read;
dee91e82 12076 info_ptr = read_attribute_value (reader, attr, form, info_ptr);
a8329558 12077 break;
3019eac3
DE
12078 case DW_FORM_GNU_addr_index:
12079 if (reader->dwo_file == NULL)
12080 {
12081 /* For now flag a hard error.
12082 Later we can turn this into a complaint. */
12083 error (_("Dwarf Error: %s found in non-DWO CU [in module %s]"),
12084 dwarf_form_name (form),
12085 bfd_get_filename (abfd));
12086 }
12087 DW_ADDR (attr) = read_addr_index_from_leb128 (cu, info_ptr, &bytes_read);
12088 info_ptr += bytes_read;
12089 break;
12090 case DW_FORM_GNU_str_index:
12091 if (reader->dwo_file == NULL)
12092 {
12093 /* For now flag a hard error.
12094 Later we can turn this into a complaint if warranted. */
12095 error (_("Dwarf Error: %s found in non-DWO CU [in module %s]"),
12096 dwarf_form_name (form),
12097 bfd_get_filename (abfd));
12098 }
12099 {
12100 ULONGEST str_index =
12101 read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
12102
12103 DW_STRING (attr) = read_str_index (reader, cu, str_index);
12104 DW_STRING_IS_CANONICAL (attr) = 0;
12105 info_ptr += bytes_read;
12106 }
12107 break;
c906108c 12108 default:
8a3fe4f8 12109 error (_("Dwarf Error: Cannot handle %s in DWARF reader [in module %s]"),
659b0389
ML
12110 dwarf_form_name (form),
12111 bfd_get_filename (abfd));
c906108c 12112 }
28e94949
JB
12113
12114 /* We have seen instances where the compiler tried to emit a byte
12115 size attribute of -1 which ended up being encoded as an unsigned
12116 0xffffffff. Although 0xffffffff is technically a valid size value,
12117 an object of this size seems pretty unlikely so we can relatively
12118 safely treat these cases as if the size attribute was invalid and
12119 treat them as zero by default. */
12120 if (attr->name == DW_AT_byte_size
12121 && form == DW_FORM_data4
12122 && DW_UNSND (attr) >= 0xffffffff)
01c66ae6
JB
12123 {
12124 complaint
12125 (&symfile_complaints,
43bbcdc2
PH
12126 _("Suspicious DW_AT_byte_size value treated as zero instead of %s"),
12127 hex_string (DW_UNSND (attr)));
01c66ae6
JB
12128 DW_UNSND (attr) = 0;
12129 }
28e94949 12130
c906108c
SS
12131 return info_ptr;
12132}
12133
a8329558
KW
12134/* Read an attribute described by an abbreviated attribute. */
12135
fe1b8b76 12136static gdb_byte *
dee91e82
DE
12137read_attribute (const struct die_reader_specs *reader,
12138 struct attribute *attr, struct attr_abbrev *abbrev,
12139 gdb_byte *info_ptr)
a8329558
KW
12140{
12141 attr->name = abbrev->name;
dee91e82 12142 return read_attribute_value (reader, attr, abbrev->form, info_ptr);
a8329558
KW
12143}
12144
0963b4bd 12145/* Read dwarf information from a buffer. */
c906108c
SS
12146
12147static unsigned int
fe1b8b76 12148read_1_byte (bfd *abfd, gdb_byte *buf)
c906108c 12149{
fe1b8b76 12150 return bfd_get_8 (abfd, buf);
c906108c
SS
12151}
12152
12153static int
fe1b8b76 12154read_1_signed_byte (bfd *abfd, gdb_byte *buf)
c906108c 12155{
fe1b8b76 12156 return bfd_get_signed_8 (abfd, buf);
c906108c
SS
12157}
12158
12159static unsigned int
fe1b8b76 12160read_2_bytes (bfd *abfd, gdb_byte *buf)
c906108c 12161{
fe1b8b76 12162 return bfd_get_16 (abfd, buf);
c906108c
SS
12163}
12164
21ae7a4d
JK
12165static int
12166read_2_signed_bytes (bfd *abfd, gdb_byte *buf)
12167{
12168 return bfd_get_signed_16 (abfd, buf);
12169}
12170
c906108c 12171static unsigned int
fe1b8b76 12172read_4_bytes (bfd *abfd, gdb_byte *buf)
c906108c 12173{
fe1b8b76 12174 return bfd_get_32 (abfd, buf);
c906108c
SS
12175}
12176
21ae7a4d
JK
12177static int
12178read_4_signed_bytes (bfd *abfd, gdb_byte *buf)
12179{
12180 return bfd_get_signed_32 (abfd, buf);
12181}
12182
93311388 12183static ULONGEST
fe1b8b76 12184read_8_bytes (bfd *abfd, gdb_byte *buf)
c906108c 12185{
fe1b8b76 12186 return bfd_get_64 (abfd, buf);
c906108c
SS
12187}
12188
12189static CORE_ADDR
fe1b8b76 12190read_address (bfd *abfd, gdb_byte *buf, struct dwarf2_cu *cu,
891d2f0b 12191 unsigned int *bytes_read)
c906108c 12192{
e7c27a73 12193 struct comp_unit_head *cu_header = &cu->header;
c906108c
SS
12194 CORE_ADDR retval = 0;
12195
107d2387 12196 if (cu_header->signed_addr_p)
c906108c 12197 {
107d2387
AC
12198 switch (cu_header->addr_size)
12199 {
12200 case 2:
fe1b8b76 12201 retval = bfd_get_signed_16 (abfd, buf);
107d2387
AC
12202 break;
12203 case 4:
fe1b8b76 12204 retval = bfd_get_signed_32 (abfd, buf);
107d2387
AC
12205 break;
12206 case 8:
fe1b8b76 12207 retval = bfd_get_signed_64 (abfd, buf);
107d2387
AC
12208 break;
12209 default:
8e65ff28 12210 internal_error (__FILE__, __LINE__,
e2e0b3e5 12211 _("read_address: bad switch, signed [in module %s]"),
659b0389 12212 bfd_get_filename (abfd));
107d2387
AC
12213 }
12214 }
12215 else
12216 {
12217 switch (cu_header->addr_size)
12218 {
12219 case 2:
fe1b8b76 12220 retval = bfd_get_16 (abfd, buf);
107d2387
AC
12221 break;
12222 case 4:
fe1b8b76 12223 retval = bfd_get_32 (abfd, buf);
107d2387
AC
12224 break;
12225 case 8:
fe1b8b76 12226 retval = bfd_get_64 (abfd, buf);
107d2387
AC
12227 break;
12228 default:
8e65ff28 12229 internal_error (__FILE__, __LINE__,
a73c6dcd
MS
12230 _("read_address: bad switch, "
12231 "unsigned [in module %s]"),
659b0389 12232 bfd_get_filename (abfd));
107d2387 12233 }
c906108c 12234 }
64367e0a 12235
107d2387
AC
12236 *bytes_read = cu_header->addr_size;
12237 return retval;
c906108c
SS
12238}
12239
f7ef9339 12240/* Read the initial length from a section. The (draft) DWARF 3
613e1657
KB
12241 specification allows the initial length to take up either 4 bytes
12242 or 12 bytes. If the first 4 bytes are 0xffffffff, then the next 8
12243 bytes describe the length and all offsets will be 8 bytes in length
12244 instead of 4.
12245
f7ef9339
KB
12246 An older, non-standard 64-bit format is also handled by this
12247 function. The older format in question stores the initial length
12248 as an 8-byte quantity without an escape value. Lengths greater
12249 than 2^32 aren't very common which means that the initial 4 bytes
12250 is almost always zero. Since a length value of zero doesn't make
12251 sense for the 32-bit format, this initial zero can be considered to
12252 be an escape value which indicates the presence of the older 64-bit
12253 format. As written, the code can't detect (old format) lengths
917c78fc
MK
12254 greater than 4GB. If it becomes necessary to handle lengths
12255 somewhat larger than 4GB, we could allow other small values (such
12256 as the non-sensical values of 1, 2, and 3) to also be used as
12257 escape values indicating the presence of the old format.
f7ef9339 12258
917c78fc
MK
12259 The value returned via bytes_read should be used to increment the
12260 relevant pointer after calling read_initial_length().
c764a876 12261
613e1657
KB
12262 [ Note: read_initial_length() and read_offset() are based on the
12263 document entitled "DWARF Debugging Information Format", revision
f7ef9339 12264 3, draft 8, dated November 19, 2001. This document was obtained
613e1657
KB
12265 from:
12266
f7ef9339 12267 http://reality.sgiweb.org/davea/dwarf3-draft8-011125.pdf
6e70227d 12268
613e1657
KB
12269 This document is only a draft and is subject to change. (So beware.)
12270
f7ef9339 12271 Details regarding the older, non-standard 64-bit format were
917c78fc
MK
12272 determined empirically by examining 64-bit ELF files produced by
12273 the SGI toolchain on an IRIX 6.5 machine.
f7ef9339
KB
12274
12275 - Kevin, July 16, 2002
613e1657
KB
12276 ] */
12277
12278static LONGEST
c764a876 12279read_initial_length (bfd *abfd, gdb_byte *buf, unsigned int *bytes_read)
613e1657 12280{
fe1b8b76 12281 LONGEST length = bfd_get_32 (abfd, buf);
613e1657 12282
dd373385 12283 if (length == 0xffffffff)
613e1657 12284 {
fe1b8b76 12285 length = bfd_get_64 (abfd, buf + 4);
613e1657 12286 *bytes_read = 12;
613e1657 12287 }
dd373385 12288 else if (length == 0)
f7ef9339 12289 {
dd373385 12290 /* Handle the (non-standard) 64-bit DWARF2 format used by IRIX. */
fe1b8b76 12291 length = bfd_get_64 (abfd, buf);
f7ef9339 12292 *bytes_read = 8;
f7ef9339 12293 }
613e1657
KB
12294 else
12295 {
12296 *bytes_read = 4;
613e1657
KB
12297 }
12298
c764a876
DE
12299 return length;
12300}
dd373385 12301
c764a876
DE
12302/* Cover function for read_initial_length.
12303 Returns the length of the object at BUF, and stores the size of the
12304 initial length in *BYTES_READ and stores the size that offsets will be in
12305 *OFFSET_SIZE.
12306 If the initial length size is not equivalent to that specified in
12307 CU_HEADER then issue a complaint.
12308 This is useful when reading non-comp-unit headers. */
dd373385 12309
c764a876
DE
12310static LONGEST
12311read_checked_initial_length_and_offset (bfd *abfd, gdb_byte *buf,
12312 const struct comp_unit_head *cu_header,
12313 unsigned int *bytes_read,
12314 unsigned int *offset_size)
12315{
12316 LONGEST length = read_initial_length (abfd, buf, bytes_read);
12317
12318 gdb_assert (cu_header->initial_length_size == 4
12319 || cu_header->initial_length_size == 8
12320 || cu_header->initial_length_size == 12);
12321
12322 if (cu_header->initial_length_size != *bytes_read)
12323 complaint (&symfile_complaints,
12324 _("intermixed 32-bit and 64-bit DWARF sections"));
dd373385 12325
c764a876 12326 *offset_size = (*bytes_read == 4) ? 4 : 8;
dd373385 12327 return length;
613e1657
KB
12328}
12329
12330/* Read an offset from the data stream. The size of the offset is
917c78fc 12331 given by cu_header->offset_size. */
613e1657
KB
12332
12333static LONGEST
fe1b8b76 12334read_offset (bfd *abfd, gdb_byte *buf, const struct comp_unit_head *cu_header,
891d2f0b 12335 unsigned int *bytes_read)
c764a876
DE
12336{
12337 LONGEST offset = read_offset_1 (abfd, buf, cu_header->offset_size);
9a619af0 12338
c764a876
DE
12339 *bytes_read = cu_header->offset_size;
12340 return offset;
12341}
12342
12343/* Read an offset from the data stream. */
12344
12345static LONGEST
12346read_offset_1 (bfd *abfd, gdb_byte *buf, unsigned int offset_size)
613e1657
KB
12347{
12348 LONGEST retval = 0;
12349
c764a876 12350 switch (offset_size)
613e1657
KB
12351 {
12352 case 4:
fe1b8b76 12353 retval = bfd_get_32 (abfd, buf);
613e1657
KB
12354 break;
12355 case 8:
fe1b8b76 12356 retval = bfd_get_64 (abfd, buf);
613e1657
KB
12357 break;
12358 default:
8e65ff28 12359 internal_error (__FILE__, __LINE__,
c764a876 12360 _("read_offset_1: bad switch [in module %s]"),
659b0389 12361 bfd_get_filename (abfd));
613e1657
KB
12362 }
12363
917c78fc 12364 return retval;
613e1657
KB
12365}
12366
fe1b8b76
JB
12367static gdb_byte *
12368read_n_bytes (bfd *abfd, gdb_byte *buf, unsigned int size)
c906108c
SS
12369{
12370 /* If the size of a host char is 8 bits, we can return a pointer
12371 to the buffer, otherwise we have to copy the data to a buffer
12372 allocated on the temporary obstack. */
4bdf3d34 12373 gdb_assert (HOST_CHAR_BIT == 8);
c906108c 12374 return buf;
c906108c
SS
12375}
12376
12377static char *
9b1c24c8 12378read_direct_string (bfd *abfd, gdb_byte *buf, unsigned int *bytes_read_ptr)
c906108c
SS
12379{
12380 /* If the size of a host char is 8 bits, we can return a pointer
12381 to the string, otherwise we have to copy the string to a buffer
12382 allocated on the temporary obstack. */
4bdf3d34 12383 gdb_assert (HOST_CHAR_BIT == 8);
c906108c
SS
12384 if (*buf == '\0')
12385 {
12386 *bytes_read_ptr = 1;
12387 return NULL;
12388 }
fe1b8b76
JB
12389 *bytes_read_ptr = strlen ((char *) buf) + 1;
12390 return (char *) buf;
4bdf3d34
JJ
12391}
12392
12393static char *
cf2c3c16 12394read_indirect_string_at_offset (bfd *abfd, LONGEST str_offset)
4bdf3d34 12395{
be391dca 12396 dwarf2_read_section (dwarf2_per_objfile->objfile, &dwarf2_per_objfile->str);
dce234bc 12397 if (dwarf2_per_objfile->str.buffer == NULL)
cf2c3c16
TT
12398 error (_("DW_FORM_strp used without .debug_str section [in module %s]"),
12399 bfd_get_filename (abfd));
dce234bc 12400 if (str_offset >= dwarf2_per_objfile->str.size)
cf2c3c16
TT
12401 error (_("DW_FORM_strp pointing outside of "
12402 ".debug_str section [in module %s]"),
12403 bfd_get_filename (abfd));
4bdf3d34 12404 gdb_assert (HOST_CHAR_BIT == 8);
dce234bc 12405 if (dwarf2_per_objfile->str.buffer[str_offset] == '\0')
4bdf3d34 12406 return NULL;
dce234bc 12407 return (char *) (dwarf2_per_objfile->str.buffer + str_offset);
c906108c
SS
12408}
12409
cf2c3c16
TT
12410static char *
12411read_indirect_string (bfd *abfd, gdb_byte *buf,
12412 const struct comp_unit_head *cu_header,
12413 unsigned int *bytes_read_ptr)
12414{
12415 LONGEST str_offset = read_offset (abfd, buf, cu_header, bytes_read_ptr);
12416
12417 return read_indirect_string_at_offset (abfd, str_offset);
12418}
12419
12df843f 12420static ULONGEST
fe1b8b76 12421read_unsigned_leb128 (bfd *abfd, gdb_byte *buf, unsigned int *bytes_read_ptr)
c906108c 12422{
12df843f 12423 ULONGEST result;
ce5d95e1 12424 unsigned int num_read;
c906108c
SS
12425 int i, shift;
12426 unsigned char byte;
12427
12428 result = 0;
12429 shift = 0;
12430 num_read = 0;
12431 i = 0;
12432 while (1)
12433 {
fe1b8b76 12434 byte = bfd_get_8 (abfd, buf);
c906108c
SS
12435 buf++;
12436 num_read++;
12df843f 12437 result |= ((ULONGEST) (byte & 127) << shift);
c906108c
SS
12438 if ((byte & 128) == 0)
12439 {
12440 break;
12441 }
12442 shift += 7;
12443 }
12444 *bytes_read_ptr = num_read;
12445 return result;
12446}
12447
12df843f 12448static LONGEST
fe1b8b76 12449read_signed_leb128 (bfd *abfd, gdb_byte *buf, unsigned int *bytes_read_ptr)
c906108c 12450{
12df843f 12451 LONGEST result;
77e0b926 12452 int i, shift, num_read;
c906108c
SS
12453 unsigned char byte;
12454
12455 result = 0;
12456 shift = 0;
c906108c
SS
12457 num_read = 0;
12458 i = 0;
12459 while (1)
12460 {
fe1b8b76 12461 byte = bfd_get_8 (abfd, buf);
c906108c
SS
12462 buf++;
12463 num_read++;
12df843f 12464 result |= ((LONGEST) (byte & 127) << shift);
c906108c
SS
12465 shift += 7;
12466 if ((byte & 128) == 0)
12467 {
12468 break;
12469 }
12470 }
77e0b926 12471 if ((shift < 8 * sizeof (result)) && (byte & 0x40))
12df843f 12472 result |= -(((LONGEST) 1) << shift);
c906108c
SS
12473 *bytes_read_ptr = num_read;
12474 return result;
12475}
12476
3019eac3
DE
12477/* Given index ADDR_INDEX in .debug_addr, fetch the value.
12478 ADDR_BASE is the DW_AT_GNU_addr_base attribute or zero.
12479 ADDR_SIZE is the size of addresses from the CU header. */
12480
12481static CORE_ADDR
12482read_addr_index_1 (unsigned int addr_index, ULONGEST addr_base, int addr_size)
12483{
12484 struct objfile *objfile = dwarf2_per_objfile->objfile;
12485 bfd *abfd = objfile->obfd;
12486 const gdb_byte *info_ptr;
12487
12488 dwarf2_read_section (objfile, &dwarf2_per_objfile->addr);
12489 if (dwarf2_per_objfile->addr.buffer == NULL)
12490 error (_("DW_FORM_addr_index used without .debug_addr section [in module %s]"),
12491 objfile->name);
12492 if (addr_base + addr_index * addr_size >= dwarf2_per_objfile->addr.size)
12493 error (_("DW_FORM_addr_index pointing outside of "
12494 ".debug_addr section [in module %s]"),
12495 objfile->name);
12496 info_ptr = (dwarf2_per_objfile->addr.buffer
12497 + addr_base + addr_index * addr_size);
12498 if (addr_size == 4)
12499 return bfd_get_32 (abfd, info_ptr);
12500 else
12501 return bfd_get_64 (abfd, info_ptr);
12502}
12503
12504/* Given index ADDR_INDEX in .debug_addr, fetch the value. */
12505
12506static CORE_ADDR
12507read_addr_index (struct dwarf2_cu *cu, unsigned int addr_index)
12508{
12509 return read_addr_index_1 (addr_index, cu->addr_base, cu->header.addr_size);
12510}
12511
12512/* Given a pointer to an leb128 value, fetch the value from .debug_addr. */
12513
12514static CORE_ADDR
12515read_addr_index_from_leb128 (struct dwarf2_cu *cu, gdb_byte *info_ptr,
12516 unsigned int *bytes_read)
12517{
12518 bfd *abfd = cu->objfile->obfd;
12519 unsigned int addr_index = read_unsigned_leb128 (abfd, info_ptr, bytes_read);
12520
12521 return read_addr_index (cu, addr_index);
12522}
12523
12524/* Data structure to pass results from dwarf2_read_addr_index_reader
12525 back to dwarf2_read_addr_index. */
12526
12527struct dwarf2_read_addr_index_data
12528{
12529 ULONGEST addr_base;
12530 int addr_size;
12531};
12532
12533/* die_reader_func for dwarf2_read_addr_index. */
12534
12535static void
12536dwarf2_read_addr_index_reader (const struct die_reader_specs *reader,
12537 gdb_byte *info_ptr,
12538 struct die_info *comp_unit_die,
12539 int has_children,
12540 void *data)
12541{
12542 struct dwarf2_cu *cu = reader->cu;
12543 struct dwarf2_read_addr_index_data *aidata =
12544 (struct dwarf2_read_addr_index_data *) data;
12545
12546 aidata->addr_base = cu->addr_base;
12547 aidata->addr_size = cu->header.addr_size;
12548}
12549
12550/* Given an index in .debug_addr, fetch the value.
12551 NOTE: This can be called during dwarf expression evaluation,
12552 long after the debug information has been read, and thus per_cu->cu
12553 may no longer exist. */
12554
12555CORE_ADDR
12556dwarf2_read_addr_index (struct dwarf2_per_cu_data *per_cu,
12557 unsigned int addr_index)
12558{
12559 struct objfile *objfile = per_cu->objfile;
12560 struct dwarf2_cu *cu = per_cu->cu;
12561 ULONGEST addr_base;
12562 int addr_size;
12563
12564 /* This is intended to be called from outside this file. */
12565 dw2_setup (objfile);
12566
12567 /* We need addr_base and addr_size.
12568 If we don't have PER_CU->cu, we have to get it.
12569 Nasty, but the alternative is storing the needed info in PER_CU,
12570 which at this point doesn't seem justified: it's not clear how frequently
12571 it would get used and it would increase the size of every PER_CU.
12572 Entry points like dwarf2_per_cu_addr_size do a similar thing
12573 so we're not in uncharted territory here.
12574 Alas we need to be a bit more complicated as addr_base is contained
12575 in the DIE.
12576
12577 We don't need to read the entire CU(/TU).
12578 We just need the header and top level die.
12579 IWBN to use the aging mechanism to let us lazily later discard the CU.
12580 See however init_cutu_and_read_dies_simple. */
12581
12582 if (cu != NULL)
12583 {
12584 addr_base = cu->addr_base;
12585 addr_size = cu->header.addr_size;
12586 }
12587 else
12588 {
12589 struct dwarf2_read_addr_index_data aidata;
12590
12591 init_cutu_and_read_dies_simple (per_cu, dwarf2_read_addr_index_reader,
12592 &aidata);
12593 addr_base = aidata.addr_base;
12594 addr_size = aidata.addr_size;
12595 }
12596
12597 return read_addr_index_1 (addr_index, addr_base, addr_size);
12598}
12599
12600/* Given a DW_AT_str_index, fetch the string. */
12601
12602static char *
12603read_str_index (const struct die_reader_specs *reader,
12604 struct dwarf2_cu *cu, ULONGEST str_index)
12605{
12606 struct objfile *objfile = dwarf2_per_objfile->objfile;
12607 const char *dwo_name = objfile->name;
12608 bfd *abfd = objfile->obfd;
12609 struct dwo_sections *sections = &reader->dwo_file->sections;
12610 gdb_byte *info_ptr;
12611 ULONGEST str_offset;
12612
12613 dwarf2_read_section (objfile, &sections->str);
12614 dwarf2_read_section (objfile, &sections->str_offsets);
12615 if (sections->str.buffer == NULL)
12616 error (_("DW_FORM_str_index used without .debug_str.dwo section"
12617 " in CU at offset 0x%lx [in module %s]"),
12618 (long) cu->header.offset.sect_off, dwo_name);
12619 if (sections->str_offsets.buffer == NULL)
12620 error (_("DW_FORM_str_index used without .debug_str_offsets.dwo section"
12621 " in CU at offset 0x%lx [in module %s]"),
12622 (long) cu->header.offset.sect_off, dwo_name);
12623 if (str_index * cu->header.offset_size >= sections->str_offsets.size)
12624 error (_("DW_FORM_str_index pointing outside of .debug_str_offsets.dwo"
12625 " section in CU at offset 0x%lx [in module %s]"),
12626 (long) cu->header.offset.sect_off, dwo_name);
12627 info_ptr = (sections->str_offsets.buffer
12628 + str_index * cu->header.offset_size);
12629 if (cu->header.offset_size == 4)
12630 str_offset = bfd_get_32 (abfd, info_ptr);
12631 else
12632 str_offset = bfd_get_64 (abfd, info_ptr);
12633 if (str_offset >= sections->str.size)
12634 error (_("Offset from DW_FORM_str_index pointing outside of"
12635 " .debug_str.dwo section in CU at offset 0x%lx [in module %s]"),
12636 (long) cu->header.offset.sect_off, dwo_name);
12637 return (char *) (sections->str.buffer + str_offset);
12638}
12639
3019eac3
DE
12640/* Return the length of an LEB128 number in BUF. */
12641
12642static int
12643leb128_size (const gdb_byte *buf)
12644{
12645 const gdb_byte *begin = buf;
12646 gdb_byte byte;
12647
12648 while (1)
12649 {
12650 byte = *buf++;
12651 if ((byte & 128) == 0)
12652 return buf - begin;
12653 }
12654}
12655
c906108c 12656static void
e142c38c 12657set_cu_language (unsigned int lang, struct dwarf2_cu *cu)
c906108c
SS
12658{
12659 switch (lang)
12660 {
12661 case DW_LANG_C89:
76bee0cc 12662 case DW_LANG_C99:
c906108c 12663 case DW_LANG_C:
e142c38c 12664 cu->language = language_c;
c906108c
SS
12665 break;
12666 case DW_LANG_C_plus_plus:
e142c38c 12667 cu->language = language_cplus;
c906108c 12668 break;
6aecb9c2
JB
12669 case DW_LANG_D:
12670 cu->language = language_d;
12671 break;
c906108c
SS
12672 case DW_LANG_Fortran77:
12673 case DW_LANG_Fortran90:
b21b22e0 12674 case DW_LANG_Fortran95:
e142c38c 12675 cu->language = language_fortran;
c906108c 12676 break;
a766d390
DE
12677 case DW_LANG_Go:
12678 cu->language = language_go;
12679 break;
c906108c 12680 case DW_LANG_Mips_Assembler:
e142c38c 12681 cu->language = language_asm;
c906108c 12682 break;
bebd888e 12683 case DW_LANG_Java:
e142c38c 12684 cu->language = language_java;
bebd888e 12685 break;
c906108c 12686 case DW_LANG_Ada83:
8aaf0b47 12687 case DW_LANG_Ada95:
bc5f45f8
JB
12688 cu->language = language_ada;
12689 break;
72019c9c
GM
12690 case DW_LANG_Modula2:
12691 cu->language = language_m2;
12692 break;
fe8e67fd
PM
12693 case DW_LANG_Pascal83:
12694 cu->language = language_pascal;
12695 break;
22566fbd
DJ
12696 case DW_LANG_ObjC:
12697 cu->language = language_objc;
12698 break;
c906108c
SS
12699 case DW_LANG_Cobol74:
12700 case DW_LANG_Cobol85:
c906108c 12701 default:
e142c38c 12702 cu->language = language_minimal;
c906108c
SS
12703 break;
12704 }
e142c38c 12705 cu->language_defn = language_def (cu->language);
c906108c
SS
12706}
12707
12708/* Return the named attribute or NULL if not there. */
12709
12710static struct attribute *
e142c38c 12711dwarf2_attr (struct die_info *die, unsigned int name, struct dwarf2_cu *cu)
c906108c 12712{
a48e046c 12713 for (;;)
c906108c 12714 {
a48e046c
TT
12715 unsigned int i;
12716 struct attribute *spec = NULL;
12717
12718 for (i = 0; i < die->num_attrs; ++i)
12719 {
12720 if (die->attrs[i].name == name)
12721 return &die->attrs[i];
12722 if (die->attrs[i].name == DW_AT_specification
12723 || die->attrs[i].name == DW_AT_abstract_origin)
12724 spec = &die->attrs[i];
12725 }
12726
12727 if (!spec)
12728 break;
c906108c 12729
f2f0e013 12730 die = follow_die_ref (die, spec, &cu);
f2f0e013 12731 }
c5aa993b 12732
c906108c
SS
12733 return NULL;
12734}
12735
348e048f
DE
12736/* Return the named attribute or NULL if not there,
12737 but do not follow DW_AT_specification, etc.
12738 This is for use in contexts where we're reading .debug_types dies.
12739 Following DW_AT_specification, DW_AT_abstract_origin will take us
12740 back up the chain, and we want to go down. */
12741
12742static struct attribute *
12743dwarf2_attr_no_follow (struct die_info *die, unsigned int name,
12744 struct dwarf2_cu *cu)
12745{
12746 unsigned int i;
12747
12748 for (i = 0; i < die->num_attrs; ++i)
12749 if (die->attrs[i].name == name)
12750 return &die->attrs[i];
12751
12752 return NULL;
12753}
12754
05cf31d1
JB
12755/* Return non-zero iff the attribute NAME is defined for the given DIE,
12756 and holds a non-zero value. This function should only be used for
2dc7f7b3 12757 DW_FORM_flag or DW_FORM_flag_present attributes. */
05cf31d1
JB
12758
12759static int
12760dwarf2_flag_true_p (struct die_info *die, unsigned name, struct dwarf2_cu *cu)
12761{
12762 struct attribute *attr = dwarf2_attr (die, name, cu);
12763
12764 return (attr && DW_UNSND (attr));
12765}
12766
3ca72b44 12767static int
e142c38c 12768die_is_declaration (struct die_info *die, struct dwarf2_cu *cu)
3ca72b44 12769{
05cf31d1
JB
12770 /* A DIE is a declaration if it has a DW_AT_declaration attribute
12771 which value is non-zero. However, we have to be careful with
12772 DIEs having a DW_AT_specification attribute, because dwarf2_attr()
12773 (via dwarf2_flag_true_p) follows this attribute. So we may
12774 end up accidently finding a declaration attribute that belongs
12775 to a different DIE referenced by the specification attribute,
12776 even though the given DIE does not have a declaration attribute. */
12777 return (dwarf2_flag_true_p (die, DW_AT_declaration, cu)
12778 && dwarf2_attr (die, DW_AT_specification, cu) == NULL);
3ca72b44
AC
12779}
12780
63d06c5c 12781/* Return the die giving the specification for DIE, if there is
f2f0e013 12782 one. *SPEC_CU is the CU containing DIE on input, and the CU
edb3359d
DJ
12783 containing the return value on output. If there is no
12784 specification, but there is an abstract origin, that is
12785 returned. */
63d06c5c
DC
12786
12787static struct die_info *
f2f0e013 12788die_specification (struct die_info *die, struct dwarf2_cu **spec_cu)
63d06c5c 12789{
f2f0e013
DJ
12790 struct attribute *spec_attr = dwarf2_attr (die, DW_AT_specification,
12791 *spec_cu);
63d06c5c 12792
edb3359d
DJ
12793 if (spec_attr == NULL)
12794 spec_attr = dwarf2_attr (die, DW_AT_abstract_origin, *spec_cu);
12795
63d06c5c
DC
12796 if (spec_attr == NULL)
12797 return NULL;
12798 else
f2f0e013 12799 return follow_die_ref (die, spec_attr, spec_cu);
63d06c5c 12800}
c906108c 12801
debd256d 12802/* Free the line_header structure *LH, and any arrays and strings it
ae2de4f8
DE
12803 refers to.
12804 NOTE: This is also used as a "cleanup" function. */
12805
debd256d
JB
12806static void
12807free_line_header (struct line_header *lh)
12808{
12809 if (lh->standard_opcode_lengths)
a8bc7b56 12810 xfree (lh->standard_opcode_lengths);
debd256d
JB
12811
12812 /* Remember that all the lh->file_names[i].name pointers are
12813 pointers into debug_line_buffer, and don't need to be freed. */
12814 if (lh->file_names)
a8bc7b56 12815 xfree (lh->file_names);
debd256d
JB
12816
12817 /* Similarly for the include directory names. */
12818 if (lh->include_dirs)
a8bc7b56 12819 xfree (lh->include_dirs);
debd256d 12820
a8bc7b56 12821 xfree (lh);
debd256d
JB
12822}
12823
debd256d 12824/* Add an entry to LH's include directory table. */
ae2de4f8 12825
debd256d
JB
12826static void
12827add_include_dir (struct line_header *lh, char *include_dir)
c906108c 12828{
debd256d
JB
12829 /* Grow the array if necessary. */
12830 if (lh->include_dirs_size == 0)
c5aa993b 12831 {
debd256d
JB
12832 lh->include_dirs_size = 1; /* for testing */
12833 lh->include_dirs = xmalloc (lh->include_dirs_size
12834 * sizeof (*lh->include_dirs));
12835 }
12836 else if (lh->num_include_dirs >= lh->include_dirs_size)
12837 {
12838 lh->include_dirs_size *= 2;
12839 lh->include_dirs = xrealloc (lh->include_dirs,
12840 (lh->include_dirs_size
12841 * sizeof (*lh->include_dirs)));
c5aa993b 12842 }
c906108c 12843
debd256d
JB
12844 lh->include_dirs[lh->num_include_dirs++] = include_dir;
12845}
6e70227d 12846
debd256d 12847/* Add an entry to LH's file name table. */
ae2de4f8 12848
debd256d
JB
12849static void
12850add_file_name (struct line_header *lh,
12851 char *name,
12852 unsigned int dir_index,
12853 unsigned int mod_time,
12854 unsigned int length)
12855{
12856 struct file_entry *fe;
12857
12858 /* Grow the array if necessary. */
12859 if (lh->file_names_size == 0)
12860 {
12861 lh->file_names_size = 1; /* for testing */
12862 lh->file_names = xmalloc (lh->file_names_size
12863 * sizeof (*lh->file_names));
12864 }
12865 else if (lh->num_file_names >= lh->file_names_size)
12866 {
12867 lh->file_names_size *= 2;
12868 lh->file_names = xrealloc (lh->file_names,
12869 (lh->file_names_size
12870 * sizeof (*lh->file_names)));
12871 }
12872
12873 fe = &lh->file_names[lh->num_file_names++];
12874 fe->name = name;
12875 fe->dir_index = dir_index;
12876 fe->mod_time = mod_time;
12877 fe->length = length;
aaa75496 12878 fe->included_p = 0;
cb1df416 12879 fe->symtab = NULL;
debd256d 12880}
6e70227d 12881
debd256d 12882/* Read the statement program header starting at OFFSET in
3019eac3 12883 .debug_line, or .debug_line.dwo. Return a pointer
6502dd73 12884 to a struct line_header, allocated using xmalloc.
debd256d
JB
12885
12886 NOTE: the strings in the include directory and file name tables of
3019eac3
DE
12887 the returned object point into the dwarf line section buffer,
12888 and must not be freed. */
ae2de4f8 12889
debd256d 12890static struct line_header *
3019eac3 12891dwarf_decode_line_header (unsigned int offset, struct dwarf2_cu *cu)
debd256d
JB
12892{
12893 struct cleanup *back_to;
12894 struct line_header *lh;
fe1b8b76 12895 gdb_byte *line_ptr;
c764a876 12896 unsigned int bytes_read, offset_size;
debd256d
JB
12897 int i;
12898 char *cur_dir, *cur_file;
3019eac3
DE
12899 struct dwarf2_section_info *section;
12900 bfd *abfd;
12901
12902 /* For TUs in DWO files, the DW_AT_stmt_list attribute lives in the
12903 DWO file. */
12904 if (cu->dwo_unit && cu->per_cu->is_debug_types)
12905 section = &cu->dwo_unit->dwo_file->sections.line;
12906 else
12907 section = &dwarf2_per_objfile->line;
debd256d 12908
3019eac3
DE
12909 dwarf2_read_section (dwarf2_per_objfile->objfile, section);
12910 if (section->buffer == NULL)
debd256d 12911 {
3019eac3
DE
12912 if (cu->dwo_unit && cu->per_cu->is_debug_types)
12913 complaint (&symfile_complaints, _("missing .debug_line.dwo section"));
12914 else
12915 complaint (&symfile_complaints, _("missing .debug_line section"));
debd256d
JB
12916 return 0;
12917 }
12918
fceca515
DE
12919 /* We can't do this until we know the section is non-empty.
12920 Only then do we know we have such a section. */
12921 abfd = section->asection->owner;
12922
a738430d
MK
12923 /* Make sure that at least there's room for the total_length field.
12924 That could be 12 bytes long, but we're just going to fudge that. */
3019eac3 12925 if (offset + 4 >= section->size)
debd256d 12926 {
4d3c2250 12927 dwarf2_statement_list_fits_in_line_number_section_complaint ();
debd256d
JB
12928 return 0;
12929 }
12930
12931 lh = xmalloc (sizeof (*lh));
12932 memset (lh, 0, sizeof (*lh));
12933 back_to = make_cleanup ((make_cleanup_ftype *) free_line_header,
12934 (void *) lh);
12935
3019eac3 12936 line_ptr = section->buffer + offset;
debd256d 12937
a738430d 12938 /* Read in the header. */
6e70227d 12939 lh->total_length =
c764a876
DE
12940 read_checked_initial_length_and_offset (abfd, line_ptr, &cu->header,
12941 &bytes_read, &offset_size);
debd256d 12942 line_ptr += bytes_read;
3019eac3 12943 if (line_ptr + lh->total_length > (section->buffer + section->size))
debd256d 12944 {
4d3c2250 12945 dwarf2_statement_list_fits_in_line_number_section_complaint ();
debd256d
JB
12946 return 0;
12947 }
12948 lh->statement_program_end = line_ptr + lh->total_length;
12949 lh->version = read_2_bytes (abfd, line_ptr);
12950 line_ptr += 2;
c764a876
DE
12951 lh->header_length = read_offset_1 (abfd, line_ptr, offset_size);
12952 line_ptr += offset_size;
debd256d
JB
12953 lh->minimum_instruction_length = read_1_byte (abfd, line_ptr);
12954 line_ptr += 1;
2dc7f7b3
TT
12955 if (lh->version >= 4)
12956 {
12957 lh->maximum_ops_per_instruction = read_1_byte (abfd, line_ptr);
12958 line_ptr += 1;
12959 }
12960 else
12961 lh->maximum_ops_per_instruction = 1;
12962
12963 if (lh->maximum_ops_per_instruction == 0)
12964 {
12965 lh->maximum_ops_per_instruction = 1;
12966 complaint (&symfile_complaints,
3e43a32a
MS
12967 _("invalid maximum_ops_per_instruction "
12968 "in `.debug_line' section"));
2dc7f7b3
TT
12969 }
12970
debd256d
JB
12971 lh->default_is_stmt = read_1_byte (abfd, line_ptr);
12972 line_ptr += 1;
12973 lh->line_base = read_1_signed_byte (abfd, line_ptr);
12974 line_ptr += 1;
12975 lh->line_range = read_1_byte (abfd, line_ptr);
12976 line_ptr += 1;
12977 lh->opcode_base = read_1_byte (abfd, line_ptr);
12978 line_ptr += 1;
12979 lh->standard_opcode_lengths
fe1b8b76 12980 = xmalloc (lh->opcode_base * sizeof (lh->standard_opcode_lengths[0]));
debd256d
JB
12981
12982 lh->standard_opcode_lengths[0] = 1; /* This should never be used anyway. */
12983 for (i = 1; i < lh->opcode_base; ++i)
12984 {
12985 lh->standard_opcode_lengths[i] = read_1_byte (abfd, line_ptr);
12986 line_ptr += 1;
12987 }
12988
a738430d 12989 /* Read directory table. */
9b1c24c8 12990 while ((cur_dir = read_direct_string (abfd, line_ptr, &bytes_read)) != NULL)
debd256d
JB
12991 {
12992 line_ptr += bytes_read;
12993 add_include_dir (lh, cur_dir);
12994 }
12995 line_ptr += bytes_read;
12996
a738430d 12997 /* Read file name table. */
9b1c24c8 12998 while ((cur_file = read_direct_string (abfd, line_ptr, &bytes_read)) != NULL)
debd256d
JB
12999 {
13000 unsigned int dir_index, mod_time, length;
13001
13002 line_ptr += bytes_read;
13003 dir_index = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
13004 line_ptr += bytes_read;
13005 mod_time = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
13006 line_ptr += bytes_read;
13007 length = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
13008 line_ptr += bytes_read;
13009
13010 add_file_name (lh, cur_file, dir_index, mod_time, length);
13011 }
13012 line_ptr += bytes_read;
6e70227d 13013 lh->statement_program_start = line_ptr;
debd256d 13014
3019eac3 13015 if (line_ptr > (section->buffer + section->size))
4d3c2250 13016 complaint (&symfile_complaints,
3e43a32a
MS
13017 _("line number info header doesn't "
13018 "fit in `.debug_line' section"));
debd256d
JB
13019
13020 discard_cleanups (back_to);
13021 return lh;
13022}
c906108c 13023
c6da4cef
DE
13024/* Subroutine of dwarf_decode_lines to simplify it.
13025 Return the file name of the psymtab for included file FILE_INDEX
13026 in line header LH of PST.
13027 COMP_DIR is the compilation directory (DW_AT_comp_dir) or NULL if unknown.
13028 If space for the result is malloc'd, it will be freed by a cleanup.
13029 Returns NULL if FILE_INDEX should be ignored, i.e., it is pst->filename. */
13030
13031static char *
13032psymtab_include_file_name (const struct line_header *lh, int file_index,
13033 const struct partial_symtab *pst,
13034 const char *comp_dir)
13035{
13036 const struct file_entry fe = lh->file_names [file_index];
13037 char *include_name = fe.name;
13038 char *include_name_to_compare = include_name;
13039 char *dir_name = NULL;
72b9f47f
TT
13040 const char *pst_filename;
13041 char *copied_name = NULL;
c6da4cef
DE
13042 int file_is_pst;
13043
13044 if (fe.dir_index)
13045 dir_name = lh->include_dirs[fe.dir_index - 1];
13046
13047 if (!IS_ABSOLUTE_PATH (include_name)
13048 && (dir_name != NULL || comp_dir != NULL))
13049 {
13050 /* Avoid creating a duplicate psymtab for PST.
13051 We do this by comparing INCLUDE_NAME and PST_FILENAME.
13052 Before we do the comparison, however, we need to account
13053 for DIR_NAME and COMP_DIR.
13054 First prepend dir_name (if non-NULL). If we still don't
13055 have an absolute path prepend comp_dir (if non-NULL).
13056 However, the directory we record in the include-file's
13057 psymtab does not contain COMP_DIR (to match the
13058 corresponding symtab(s)).
13059
13060 Example:
13061
13062 bash$ cd /tmp
13063 bash$ gcc -g ./hello.c
13064 include_name = "hello.c"
13065 dir_name = "."
13066 DW_AT_comp_dir = comp_dir = "/tmp"
13067 DW_AT_name = "./hello.c" */
13068
13069 if (dir_name != NULL)
13070 {
13071 include_name = concat (dir_name, SLASH_STRING,
13072 include_name, (char *)NULL);
13073 include_name_to_compare = include_name;
13074 make_cleanup (xfree, include_name);
13075 }
13076 if (!IS_ABSOLUTE_PATH (include_name) && comp_dir != NULL)
13077 {
13078 include_name_to_compare = concat (comp_dir, SLASH_STRING,
13079 include_name, (char *)NULL);
13080 }
13081 }
13082
13083 pst_filename = pst->filename;
13084 if (!IS_ABSOLUTE_PATH (pst_filename) && pst->dirname != NULL)
13085 {
72b9f47f
TT
13086 copied_name = concat (pst->dirname, SLASH_STRING,
13087 pst_filename, (char *)NULL);
13088 pst_filename = copied_name;
c6da4cef
DE
13089 }
13090
1e3fad37 13091 file_is_pst = FILENAME_CMP (include_name_to_compare, pst_filename) == 0;
c6da4cef
DE
13092
13093 if (include_name_to_compare != include_name)
13094 xfree (include_name_to_compare);
72b9f47f
TT
13095 if (copied_name != NULL)
13096 xfree (copied_name);
c6da4cef
DE
13097
13098 if (file_is_pst)
13099 return NULL;
13100 return include_name;
13101}
13102
c91513d8
PP
13103/* Ignore this record_line request. */
13104
13105static void
13106noop_record_line (struct subfile *subfile, int line, CORE_ADDR pc)
13107{
13108 return;
13109}
13110
f3f5162e
DE
13111/* Subroutine of dwarf_decode_lines to simplify it.
13112 Process the line number information in LH. */
debd256d 13113
c906108c 13114static void
f3f5162e
DE
13115dwarf_decode_lines_1 (struct line_header *lh, const char *comp_dir,
13116 struct dwarf2_cu *cu, struct partial_symtab *pst)
c906108c 13117{
a8c50c1f 13118 gdb_byte *line_ptr, *extended_end;
fe1b8b76 13119 gdb_byte *line_end;
a8c50c1f 13120 unsigned int bytes_read, extended_len;
c906108c 13121 unsigned char op_code, extended_op, adj_opcode;
e142c38c
DJ
13122 CORE_ADDR baseaddr;
13123 struct objfile *objfile = cu->objfile;
f3f5162e 13124 bfd *abfd = objfile->obfd;
fbf65064 13125 struct gdbarch *gdbarch = get_objfile_arch (objfile);
aaa75496 13126 const int decode_for_pst_p = (pst != NULL);
f3f5162e 13127 struct subfile *last_subfile = NULL;
c91513d8
PP
13128 void (*p_record_line) (struct subfile *subfile, int line, CORE_ADDR pc)
13129 = record_line;
e142c38c
DJ
13130
13131 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
c906108c 13132
debd256d
JB
13133 line_ptr = lh->statement_program_start;
13134 line_end = lh->statement_program_end;
c906108c
SS
13135
13136 /* Read the statement sequences until there's nothing left. */
13137 while (line_ptr < line_end)
13138 {
13139 /* state machine registers */
13140 CORE_ADDR address = 0;
13141 unsigned int file = 1;
13142 unsigned int line = 1;
13143 unsigned int column = 0;
debd256d 13144 int is_stmt = lh->default_is_stmt;
c906108c
SS
13145 int basic_block = 0;
13146 int end_sequence = 0;
fbf65064 13147 CORE_ADDR addr;
2dc7f7b3 13148 unsigned char op_index = 0;
c906108c 13149
aaa75496 13150 if (!decode_for_pst_p && lh->num_file_names >= file)
c906108c 13151 {
aaa75496 13152 /* Start a subfile for the current file of the state machine. */
debd256d
JB
13153 /* lh->include_dirs and lh->file_names are 0-based, but the
13154 directory and file name numbers in the statement program
13155 are 1-based. */
13156 struct file_entry *fe = &lh->file_names[file - 1];
4f1520fb 13157 char *dir = NULL;
a738430d 13158
debd256d
JB
13159 if (fe->dir_index)
13160 dir = lh->include_dirs[fe->dir_index - 1];
4f1520fb
FR
13161
13162 dwarf2_start_subfile (fe->name, dir, comp_dir);
c906108c
SS
13163 }
13164
a738430d 13165 /* Decode the table. */
c5aa993b 13166 while (!end_sequence)
c906108c
SS
13167 {
13168 op_code = read_1_byte (abfd, line_ptr);
13169 line_ptr += 1;
59205f5a
JB
13170 if (line_ptr > line_end)
13171 {
13172 dwarf2_debug_line_missing_end_sequence_complaint ();
13173 break;
13174 }
9aa1fe7e 13175
debd256d 13176 if (op_code >= lh->opcode_base)
6e70227d 13177 {
a738430d 13178 /* Special operand. */
debd256d 13179 adj_opcode = op_code - lh->opcode_base;
2dc7f7b3
TT
13180 address += (((op_index + (adj_opcode / lh->line_range))
13181 / lh->maximum_ops_per_instruction)
13182 * lh->minimum_instruction_length);
13183 op_index = ((op_index + (adj_opcode / lh->line_range))
13184 % lh->maximum_ops_per_instruction);
debd256d 13185 line += lh->line_base + (adj_opcode % lh->line_range);
59205f5a 13186 if (lh->num_file_names < file || file == 0)
25e43795 13187 dwarf2_debug_line_missing_file_complaint ();
2dc7f7b3
TT
13188 /* For now we ignore lines not starting on an
13189 instruction boundary. */
13190 else if (op_index == 0)
25e43795
DJ
13191 {
13192 lh->file_names[file - 1].included_p = 1;
ca5f395d 13193 if (!decode_for_pst_p && is_stmt)
fbf65064
UW
13194 {
13195 if (last_subfile != current_subfile)
13196 {
13197 addr = gdbarch_addr_bits_remove (gdbarch, address);
13198 if (last_subfile)
c91513d8 13199 (*p_record_line) (last_subfile, 0, addr);
fbf65064
UW
13200 last_subfile = current_subfile;
13201 }
25e43795 13202 /* Append row to matrix using current values. */
7019d805 13203 addr = gdbarch_addr_bits_remove (gdbarch, address);
c91513d8 13204 (*p_record_line) (current_subfile, line, addr);
366da635 13205 }
25e43795 13206 }
ca5f395d 13207 basic_block = 0;
9aa1fe7e
GK
13208 }
13209 else switch (op_code)
c906108c
SS
13210 {
13211 case DW_LNS_extended_op:
3e43a32a
MS
13212 extended_len = read_unsigned_leb128 (abfd, line_ptr,
13213 &bytes_read);
473b7be6 13214 line_ptr += bytes_read;
a8c50c1f 13215 extended_end = line_ptr + extended_len;
c906108c
SS
13216 extended_op = read_1_byte (abfd, line_ptr);
13217 line_ptr += 1;
13218 switch (extended_op)
13219 {
13220 case DW_LNE_end_sequence:
c91513d8 13221 p_record_line = record_line;
c906108c 13222 end_sequence = 1;
c906108c
SS
13223 break;
13224 case DW_LNE_set_address:
e7c27a73 13225 address = read_address (abfd, line_ptr, cu, &bytes_read);
c91513d8
PP
13226
13227 if (address == 0 && !dwarf2_per_objfile->has_section_at_zero)
13228 {
13229 /* This line table is for a function which has been
13230 GCd by the linker. Ignore it. PR gdb/12528 */
13231
13232 long line_offset
13233 = line_ptr - dwarf2_per_objfile->line.buffer;
13234
13235 complaint (&symfile_complaints,
13236 _(".debug_line address at offset 0x%lx is 0 "
13237 "[in module %s]"),
bb5ed363 13238 line_offset, objfile->name);
c91513d8
PP
13239 p_record_line = noop_record_line;
13240 }
13241
2dc7f7b3 13242 op_index = 0;
107d2387
AC
13243 line_ptr += bytes_read;
13244 address += baseaddr;
c906108c
SS
13245 break;
13246 case DW_LNE_define_file:
debd256d
JB
13247 {
13248 char *cur_file;
13249 unsigned int dir_index, mod_time, length;
6e70227d 13250
3e43a32a
MS
13251 cur_file = read_direct_string (abfd, line_ptr,
13252 &bytes_read);
debd256d
JB
13253 line_ptr += bytes_read;
13254 dir_index =
13255 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
13256 line_ptr += bytes_read;
13257 mod_time =
13258 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
13259 line_ptr += bytes_read;
13260 length =
13261 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
13262 line_ptr += bytes_read;
13263 add_file_name (lh, cur_file, dir_index, mod_time, length);
13264 }
c906108c 13265 break;
d0c6ba3d
CC
13266 case DW_LNE_set_discriminator:
13267 /* The discriminator is not interesting to the debugger;
13268 just ignore it. */
13269 line_ptr = extended_end;
13270 break;
c906108c 13271 default:
4d3c2250 13272 complaint (&symfile_complaints,
e2e0b3e5 13273 _("mangled .debug_line section"));
debd256d 13274 return;
c906108c 13275 }
a8c50c1f
DJ
13276 /* Make sure that we parsed the extended op correctly. If e.g.
13277 we expected a different address size than the producer used,
13278 we may have read the wrong number of bytes. */
13279 if (line_ptr != extended_end)
13280 {
13281 complaint (&symfile_complaints,
13282 _("mangled .debug_line section"));
13283 return;
13284 }
c906108c
SS
13285 break;
13286 case DW_LNS_copy:
59205f5a 13287 if (lh->num_file_names < file || file == 0)
25e43795
DJ
13288 dwarf2_debug_line_missing_file_complaint ();
13289 else
366da635 13290 {
25e43795 13291 lh->file_names[file - 1].included_p = 1;
ca5f395d 13292 if (!decode_for_pst_p && is_stmt)
fbf65064
UW
13293 {
13294 if (last_subfile != current_subfile)
13295 {
13296 addr = gdbarch_addr_bits_remove (gdbarch, address);
13297 if (last_subfile)
c91513d8 13298 (*p_record_line) (last_subfile, 0, addr);
fbf65064
UW
13299 last_subfile = current_subfile;
13300 }
7019d805 13301 addr = gdbarch_addr_bits_remove (gdbarch, address);
c91513d8 13302 (*p_record_line) (current_subfile, line, addr);
fbf65064 13303 }
366da635 13304 }
c906108c
SS
13305 basic_block = 0;
13306 break;
13307 case DW_LNS_advance_pc:
2dc7f7b3
TT
13308 {
13309 CORE_ADDR adjust
13310 = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
13311
13312 address += (((op_index + adjust)
13313 / lh->maximum_ops_per_instruction)
13314 * lh->minimum_instruction_length);
13315 op_index = ((op_index + adjust)
13316 % lh->maximum_ops_per_instruction);
13317 line_ptr += bytes_read;
13318 }
c906108c
SS
13319 break;
13320 case DW_LNS_advance_line:
13321 line += read_signed_leb128 (abfd, line_ptr, &bytes_read);
13322 line_ptr += bytes_read;
13323 break;
13324 case DW_LNS_set_file:
debd256d 13325 {
a738430d
MK
13326 /* The arrays lh->include_dirs and lh->file_names are
13327 0-based, but the directory and file name numbers in
13328 the statement program are 1-based. */
debd256d 13329 struct file_entry *fe;
4f1520fb 13330 char *dir = NULL;
a738430d 13331
debd256d
JB
13332 file = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
13333 line_ptr += bytes_read;
59205f5a 13334 if (lh->num_file_names < file || file == 0)
25e43795
DJ
13335 dwarf2_debug_line_missing_file_complaint ();
13336 else
13337 {
13338 fe = &lh->file_names[file - 1];
13339 if (fe->dir_index)
13340 dir = lh->include_dirs[fe->dir_index - 1];
13341 if (!decode_for_pst_p)
13342 {
13343 last_subfile = current_subfile;
13344 dwarf2_start_subfile (fe->name, dir, comp_dir);
13345 }
13346 }
debd256d 13347 }
c906108c
SS
13348 break;
13349 case DW_LNS_set_column:
13350 column = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
13351 line_ptr += bytes_read;
13352 break;
13353 case DW_LNS_negate_stmt:
13354 is_stmt = (!is_stmt);
13355 break;
13356 case DW_LNS_set_basic_block:
13357 basic_block = 1;
13358 break;
c2c6d25f
JM
13359 /* Add to the address register of the state machine the
13360 address increment value corresponding to special opcode
a738430d
MK
13361 255. I.e., this value is scaled by the minimum
13362 instruction length since special opcode 255 would have
b021a221 13363 scaled the increment. */
c906108c 13364 case DW_LNS_const_add_pc:
2dc7f7b3
TT
13365 {
13366 CORE_ADDR adjust = (255 - lh->opcode_base) / lh->line_range;
13367
13368 address += (((op_index + adjust)
13369 / lh->maximum_ops_per_instruction)
13370 * lh->minimum_instruction_length);
13371 op_index = ((op_index + adjust)
13372 % lh->maximum_ops_per_instruction);
13373 }
c906108c
SS
13374 break;
13375 case DW_LNS_fixed_advance_pc:
13376 address += read_2_bytes (abfd, line_ptr);
2dc7f7b3 13377 op_index = 0;
c906108c
SS
13378 line_ptr += 2;
13379 break;
9aa1fe7e 13380 default:
a738430d
MK
13381 {
13382 /* Unknown standard opcode, ignore it. */
9aa1fe7e 13383 int i;
a738430d 13384
debd256d 13385 for (i = 0; i < lh->standard_opcode_lengths[op_code]; i++)
9aa1fe7e
GK
13386 {
13387 (void) read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
13388 line_ptr += bytes_read;
13389 }
13390 }
c906108c
SS
13391 }
13392 }
59205f5a
JB
13393 if (lh->num_file_names < file || file == 0)
13394 dwarf2_debug_line_missing_file_complaint ();
13395 else
13396 {
13397 lh->file_names[file - 1].included_p = 1;
13398 if (!decode_for_pst_p)
fbf65064
UW
13399 {
13400 addr = gdbarch_addr_bits_remove (gdbarch, address);
c91513d8 13401 (*p_record_line) (current_subfile, 0, addr);
fbf65064 13402 }
59205f5a 13403 }
c906108c 13404 }
f3f5162e
DE
13405}
13406
13407/* Decode the Line Number Program (LNP) for the given line_header
13408 structure and CU. The actual information extracted and the type
13409 of structures created from the LNP depends on the value of PST.
13410
13411 1. If PST is NULL, then this procedure uses the data from the program
13412 to create all necessary symbol tables, and their linetables.
13413
13414 2. If PST is not NULL, this procedure reads the program to determine
13415 the list of files included by the unit represented by PST, and
13416 builds all the associated partial symbol tables.
13417
13418 COMP_DIR is the compilation directory (DW_AT_comp_dir) or NULL if unknown.
13419 It is used for relative paths in the line table.
13420 NOTE: When processing partial symtabs (pst != NULL),
13421 comp_dir == pst->dirname.
13422
13423 NOTE: It is important that psymtabs have the same file name (via strcmp)
13424 as the corresponding symtab. Since COMP_DIR is not used in the name of the
13425 symtab we don't use it in the name of the psymtabs we create.
13426 E.g. expand_line_sal requires this when finding psymtabs to expand.
13427 A good testcase for this is mb-inline.exp. */
13428
13429static void
13430dwarf_decode_lines (struct line_header *lh, const char *comp_dir,
13431 struct dwarf2_cu *cu, struct partial_symtab *pst,
13432 int want_line_info)
13433{
13434 struct objfile *objfile = cu->objfile;
13435 const int decode_for_pst_p = (pst != NULL);
13436 struct subfile *first_subfile = current_subfile;
13437
13438 if (want_line_info)
13439 dwarf_decode_lines_1 (lh, comp_dir, cu, pst);
aaa75496
JB
13440
13441 if (decode_for_pst_p)
13442 {
13443 int file_index;
13444
13445 /* Now that we're done scanning the Line Header Program, we can
13446 create the psymtab of each included file. */
13447 for (file_index = 0; file_index < lh->num_file_names; file_index++)
13448 if (lh->file_names[file_index].included_p == 1)
13449 {
c6da4cef
DE
13450 char *include_name =
13451 psymtab_include_file_name (lh, file_index, pst, comp_dir);
13452 if (include_name != NULL)
aaa75496
JB
13453 dwarf2_create_include_psymtab (include_name, pst, objfile);
13454 }
13455 }
cb1df416
DJ
13456 else
13457 {
13458 /* Make sure a symtab is created for every file, even files
13459 which contain only variables (i.e. no code with associated
13460 line numbers). */
cb1df416 13461 int i;
cb1df416
DJ
13462
13463 for (i = 0; i < lh->num_file_names; i++)
13464 {
13465 char *dir = NULL;
f3f5162e 13466 struct file_entry *fe;
9a619af0 13467
cb1df416
DJ
13468 fe = &lh->file_names[i];
13469 if (fe->dir_index)
13470 dir = lh->include_dirs[fe->dir_index - 1];
13471 dwarf2_start_subfile (fe->name, dir, comp_dir);
13472
13473 /* Skip the main file; we don't need it, and it must be
13474 allocated last, so that it will show up before the
13475 non-primary symtabs in the objfile's symtab list. */
13476 if (current_subfile == first_subfile)
13477 continue;
13478
13479 if (current_subfile->symtab == NULL)
13480 current_subfile->symtab = allocate_symtab (current_subfile->name,
bb5ed363 13481 objfile);
cb1df416
DJ
13482 fe->symtab = current_subfile->symtab;
13483 }
13484 }
c906108c
SS
13485}
13486
13487/* Start a subfile for DWARF. FILENAME is the name of the file and
13488 DIRNAME the name of the source directory which contains FILENAME
4f1520fb
FR
13489 or NULL if not known. COMP_DIR is the compilation directory for the
13490 linetable's compilation unit or NULL if not known.
c906108c
SS
13491 This routine tries to keep line numbers from identical absolute and
13492 relative file names in a common subfile.
13493
13494 Using the `list' example from the GDB testsuite, which resides in
13495 /srcdir and compiling it with Irix6.2 cc in /compdir using a filename
13496 of /srcdir/list0.c yields the following debugging information for list0.c:
13497
c5aa993b
JM
13498 DW_AT_name: /srcdir/list0.c
13499 DW_AT_comp_dir: /compdir
357e46e7 13500 files.files[0].name: list0.h
c5aa993b 13501 files.files[0].dir: /srcdir
357e46e7 13502 files.files[1].name: list0.c
c5aa993b 13503 files.files[1].dir: /srcdir
c906108c
SS
13504
13505 The line number information for list0.c has to end up in a single
4f1520fb
FR
13506 subfile, so that `break /srcdir/list0.c:1' works as expected.
13507 start_subfile will ensure that this happens provided that we pass the
13508 concatenation of files.files[1].dir and files.files[1].name as the
13509 subfile's name. */
c906108c
SS
13510
13511static void
3e43a32a
MS
13512dwarf2_start_subfile (char *filename, const char *dirname,
13513 const char *comp_dir)
c906108c 13514{
4f1520fb
FR
13515 char *fullname;
13516
13517 /* While reading the DIEs, we call start_symtab(DW_AT_name, DW_AT_comp_dir).
13518 `start_symtab' will always pass the contents of DW_AT_comp_dir as
13519 second argument to start_subfile. To be consistent, we do the
13520 same here. In order not to lose the line information directory,
13521 we concatenate it to the filename when it makes sense.
13522 Note that the Dwarf3 standard says (speaking of filenames in line
13523 information): ``The directory index is ignored for file names
13524 that represent full path names''. Thus ignoring dirname in the
13525 `else' branch below isn't an issue. */
c906108c 13526
d5166ae1 13527 if (!IS_ABSOLUTE_PATH (filename) && dirname != NULL)
4f1520fb
FR
13528 fullname = concat (dirname, SLASH_STRING, filename, (char *)NULL);
13529 else
13530 fullname = filename;
c906108c 13531
4f1520fb
FR
13532 start_subfile (fullname, comp_dir);
13533
13534 if (fullname != filename)
13535 xfree (fullname);
c906108c
SS
13536}
13537
4c2df51b
DJ
13538static void
13539var_decode_location (struct attribute *attr, struct symbol *sym,
e7c27a73 13540 struct dwarf2_cu *cu)
4c2df51b 13541{
e7c27a73
DJ
13542 struct objfile *objfile = cu->objfile;
13543 struct comp_unit_head *cu_header = &cu->header;
13544
4c2df51b
DJ
13545 /* NOTE drow/2003-01-30: There used to be a comment and some special
13546 code here to turn a symbol with DW_AT_external and a
13547 SYMBOL_VALUE_ADDRESS of 0 into a LOC_UNRESOLVED symbol. This was
13548 necessary for platforms (maybe Alpha, certainly PowerPC GNU/Linux
13549 with some versions of binutils) where shared libraries could have
13550 relocations against symbols in their debug information - the
13551 minimal symbol would have the right address, but the debug info
13552 would not. It's no longer necessary, because we will explicitly
13553 apply relocations when we read in the debug information now. */
13554
13555 /* A DW_AT_location attribute with no contents indicates that a
13556 variable has been optimized away. */
13557 if (attr_form_is_block (attr) && DW_BLOCK (attr)->size == 0)
13558 {
13559 SYMBOL_CLASS (sym) = LOC_OPTIMIZED_OUT;
13560 return;
13561 }
13562
13563 /* Handle one degenerate form of location expression specially, to
13564 preserve GDB's previous behavior when section offsets are
3019eac3
DE
13565 specified. If this is just a DW_OP_addr or DW_OP_GNU_addr_index
13566 then mark this symbol as LOC_STATIC. */
4c2df51b
DJ
13567
13568 if (attr_form_is_block (attr)
3019eac3
DE
13569 && ((DW_BLOCK (attr)->data[0] == DW_OP_addr
13570 && DW_BLOCK (attr)->size == 1 + cu_header->addr_size)
13571 || (DW_BLOCK (attr)->data[0] == DW_OP_GNU_addr_index
13572 && (DW_BLOCK (attr)->size
13573 == 1 + leb128_size (&DW_BLOCK (attr)->data[1])))))
4c2df51b 13574 {
891d2f0b 13575 unsigned int dummy;
4c2df51b 13576
3019eac3
DE
13577 if (DW_BLOCK (attr)->data[0] == DW_OP_addr)
13578 SYMBOL_VALUE_ADDRESS (sym) =
13579 read_address (objfile->obfd, DW_BLOCK (attr)->data + 1, cu, &dummy);
13580 else
13581 SYMBOL_VALUE_ADDRESS (sym) =
13582 read_addr_index_from_leb128 (cu, DW_BLOCK (attr)->data + 1, &dummy);
907fc202 13583 SYMBOL_CLASS (sym) = LOC_STATIC;
4c2df51b
DJ
13584 fixup_symbol_section (sym, objfile);
13585 SYMBOL_VALUE_ADDRESS (sym) += ANOFFSET (objfile->section_offsets,
13586 SYMBOL_SECTION (sym));
4c2df51b
DJ
13587 return;
13588 }
13589
13590 /* NOTE drow/2002-01-30: It might be worthwhile to have a static
13591 expression evaluator, and use LOC_COMPUTED only when necessary
13592 (i.e. when the value of a register or memory location is
13593 referenced, or a thread-local block, etc.). Then again, it might
13594 not be worthwhile. I'm assuming that it isn't unless performance
13595 or memory numbers show me otherwise. */
13596
e7c27a73 13597 dwarf2_symbol_mark_computed (attr, sym, cu);
4c2df51b 13598 SYMBOL_CLASS (sym) = LOC_COMPUTED;
8be455d7
JK
13599
13600 if (SYMBOL_COMPUTED_OPS (sym) == &dwarf2_loclist_funcs)
13601 cu->has_loclist = 1;
4c2df51b
DJ
13602}
13603
c906108c
SS
13604/* Given a pointer to a DWARF information entry, figure out if we need
13605 to make a symbol table entry for it, and if so, create a new entry
13606 and return a pointer to it.
13607 If TYPE is NULL, determine symbol type from the die, otherwise
34eaf542
TT
13608 used the passed type.
13609 If SPACE is not NULL, use it to hold the new symbol. If it is
13610 NULL, allocate a new symbol on the objfile's obstack. */
c906108c
SS
13611
13612static struct symbol *
34eaf542
TT
13613new_symbol_full (struct die_info *die, struct type *type, struct dwarf2_cu *cu,
13614 struct symbol *space)
c906108c 13615{
e7c27a73 13616 struct objfile *objfile = cu->objfile;
c906108c
SS
13617 struct symbol *sym = NULL;
13618 char *name;
13619 struct attribute *attr = NULL;
13620 struct attribute *attr2 = NULL;
e142c38c 13621 CORE_ADDR baseaddr;
e37fd15a
SW
13622 struct pending **list_to_add = NULL;
13623
edb3359d 13624 int inlined_func = (die->tag == DW_TAG_inlined_subroutine);
e142c38c
DJ
13625
13626 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
c906108c 13627
94af9270 13628 name = dwarf2_name (die, cu);
c906108c
SS
13629 if (name)
13630 {
94af9270 13631 const char *linkagename;
34eaf542 13632 int suppress_add = 0;
94af9270 13633
34eaf542
TT
13634 if (space)
13635 sym = space;
13636 else
13637 sym = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct symbol);
c906108c 13638 OBJSTAT (objfile, n_syms++);
2de7ced7
DJ
13639
13640 /* Cache this symbol's name and the name's demangled form (if any). */
33e5013e 13641 SYMBOL_SET_LANGUAGE (sym, cu->language);
94af9270
KS
13642 linkagename = dwarf2_physname (name, die, cu);
13643 SYMBOL_SET_NAMES (sym, linkagename, strlen (linkagename), 0, objfile);
c906108c 13644
f55ee35c
JK
13645 /* Fortran does not have mangling standard and the mangling does differ
13646 between gfortran, iFort etc. */
13647 if (cu->language == language_fortran
b250c185 13648 && symbol_get_demangled_name (&(sym->ginfo)) == NULL)
29df156d
SW
13649 symbol_set_demangled_name (&(sym->ginfo),
13650 (char *) dwarf2_full_name (name, die, cu),
13651 NULL);
f55ee35c 13652
c906108c 13653 /* Default assumptions.
c5aa993b 13654 Use the passed type or decode it from the die. */
176620f1 13655 SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
875dc2fc 13656 SYMBOL_CLASS (sym) = LOC_OPTIMIZED_OUT;
c906108c
SS
13657 if (type != NULL)
13658 SYMBOL_TYPE (sym) = type;
13659 else
e7c27a73 13660 SYMBOL_TYPE (sym) = die_type (die, cu);
edb3359d
DJ
13661 attr = dwarf2_attr (die,
13662 inlined_func ? DW_AT_call_line : DW_AT_decl_line,
13663 cu);
c906108c
SS
13664 if (attr)
13665 {
13666 SYMBOL_LINE (sym) = DW_UNSND (attr);
13667 }
cb1df416 13668
edb3359d
DJ
13669 attr = dwarf2_attr (die,
13670 inlined_func ? DW_AT_call_file : DW_AT_decl_file,
13671 cu);
cb1df416
DJ
13672 if (attr)
13673 {
13674 int file_index = DW_UNSND (attr);
9a619af0 13675
cb1df416
DJ
13676 if (cu->line_header == NULL
13677 || file_index > cu->line_header->num_file_names)
13678 complaint (&symfile_complaints,
13679 _("file index out of range"));
1c3d648d 13680 else if (file_index > 0)
cb1df416
DJ
13681 {
13682 struct file_entry *fe;
9a619af0 13683
cb1df416
DJ
13684 fe = &cu->line_header->file_names[file_index - 1];
13685 SYMBOL_SYMTAB (sym) = fe->symtab;
13686 }
13687 }
13688
c906108c
SS
13689 switch (die->tag)
13690 {
13691 case DW_TAG_label:
e142c38c 13692 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
c906108c
SS
13693 if (attr)
13694 {
13695 SYMBOL_VALUE_ADDRESS (sym) = DW_ADDR (attr) + baseaddr;
13696 }
0f5238ed
TT
13697 SYMBOL_TYPE (sym) = objfile_type (objfile)->builtin_core_addr;
13698 SYMBOL_DOMAIN (sym) = LABEL_DOMAIN;
c906108c 13699 SYMBOL_CLASS (sym) = LOC_LABEL;
0f5238ed 13700 add_symbol_to_list (sym, cu->list_in_scope);
c906108c
SS
13701 break;
13702 case DW_TAG_subprogram:
13703 /* SYMBOL_BLOCK_VALUE (sym) will be filled in later by
13704 finish_block. */
13705 SYMBOL_CLASS (sym) = LOC_BLOCK;
e142c38c 13706 attr2 = dwarf2_attr (die, DW_AT_external, cu);
2cfa0c8d
JB
13707 if ((attr2 && (DW_UNSND (attr2) != 0))
13708 || cu->language == language_ada)
c906108c 13709 {
2cfa0c8d
JB
13710 /* Subprograms marked external are stored as a global symbol.
13711 Ada subprograms, whether marked external or not, are always
13712 stored as a global symbol, because we want to be able to
13713 access them globally. For instance, we want to be able
13714 to break on a nested subprogram without having to
13715 specify the context. */
e37fd15a 13716 list_to_add = &global_symbols;
c906108c
SS
13717 }
13718 else
13719 {
e37fd15a 13720 list_to_add = cu->list_in_scope;
c906108c
SS
13721 }
13722 break;
edb3359d
DJ
13723 case DW_TAG_inlined_subroutine:
13724 /* SYMBOL_BLOCK_VALUE (sym) will be filled in later by
13725 finish_block. */
13726 SYMBOL_CLASS (sym) = LOC_BLOCK;
13727 SYMBOL_INLINED (sym) = 1;
481860b3 13728 list_to_add = cu->list_in_scope;
edb3359d 13729 break;
34eaf542
TT
13730 case DW_TAG_template_value_param:
13731 suppress_add = 1;
13732 /* Fall through. */
72929c62 13733 case DW_TAG_constant:
c906108c 13734 case DW_TAG_variable:
254e6b9e 13735 case DW_TAG_member:
0963b4bd
MS
13736 /* Compilation with minimal debug info may result in
13737 variables with missing type entries. Change the
13738 misleading `void' type to something sensible. */
c906108c 13739 if (TYPE_CODE (SYMBOL_TYPE (sym)) == TYPE_CODE_VOID)
64c50499 13740 SYMBOL_TYPE (sym)
46bf5051 13741 = objfile_type (objfile)->nodebug_data_symbol;
64c50499 13742
e142c38c 13743 attr = dwarf2_attr (die, DW_AT_const_value, cu);
254e6b9e
DE
13744 /* In the case of DW_TAG_member, we should only be called for
13745 static const members. */
13746 if (die->tag == DW_TAG_member)
13747 {
3863f96c
DE
13748 /* dwarf2_add_field uses die_is_declaration,
13749 so we do the same. */
254e6b9e
DE
13750 gdb_assert (die_is_declaration (die, cu));
13751 gdb_assert (attr);
13752 }
c906108c
SS
13753 if (attr)
13754 {
e7c27a73 13755 dwarf2_const_value (attr, sym, cu);
e142c38c 13756 attr2 = dwarf2_attr (die, DW_AT_external, cu);
e37fd15a 13757 if (!suppress_add)
34eaf542
TT
13758 {
13759 if (attr2 && (DW_UNSND (attr2) != 0))
e37fd15a 13760 list_to_add = &global_symbols;
34eaf542 13761 else
e37fd15a 13762 list_to_add = cu->list_in_scope;
34eaf542 13763 }
c906108c
SS
13764 break;
13765 }
e142c38c 13766 attr = dwarf2_attr (die, DW_AT_location, cu);
c906108c
SS
13767 if (attr)
13768 {
e7c27a73 13769 var_decode_location (attr, sym, cu);
e142c38c 13770 attr2 = dwarf2_attr (die, DW_AT_external, cu);
caac4577
JG
13771 if (SYMBOL_CLASS (sym) == LOC_STATIC
13772 && SYMBOL_VALUE_ADDRESS (sym) == 0
13773 && !dwarf2_per_objfile->has_section_at_zero)
13774 {
13775 /* When a static variable is eliminated by the linker,
13776 the corresponding debug information is not stripped
13777 out, but the variable address is set to null;
13778 do not add such variables into symbol table. */
13779 }
13780 else if (attr2 && (DW_UNSND (attr2) != 0))
1c809c68 13781 {
f55ee35c
JK
13782 /* Workaround gfortran PR debug/40040 - it uses
13783 DW_AT_location for variables in -fPIC libraries which may
13784 get overriden by other libraries/executable and get
13785 a different address. Resolve it by the minimal symbol
13786 which may come from inferior's executable using copy
13787 relocation. Make this workaround only for gfortran as for
13788 other compilers GDB cannot guess the minimal symbol
13789 Fortran mangling kind. */
13790 if (cu->language == language_fortran && die->parent
13791 && die->parent->tag == DW_TAG_module
13792 && cu->producer
13793 && strncmp (cu->producer, "GNU Fortran ", 12) == 0)
13794 SYMBOL_CLASS (sym) = LOC_UNRESOLVED;
13795
1c809c68
TT
13796 /* A variable with DW_AT_external is never static,
13797 but it may be block-scoped. */
13798 list_to_add = (cu->list_in_scope == &file_symbols
13799 ? &global_symbols : cu->list_in_scope);
1c809c68 13800 }
c906108c 13801 else
e37fd15a 13802 list_to_add = cu->list_in_scope;
c906108c
SS
13803 }
13804 else
13805 {
13806 /* We do not know the address of this symbol.
c5aa993b
JM
13807 If it is an external symbol and we have type information
13808 for it, enter the symbol as a LOC_UNRESOLVED symbol.
13809 The address of the variable will then be determined from
13810 the minimal symbol table whenever the variable is
13811 referenced. */
e142c38c 13812 attr2 = dwarf2_attr (die, DW_AT_external, cu);
c906108c 13813 if (attr2 && (DW_UNSND (attr2) != 0)
e142c38c 13814 && dwarf2_attr (die, DW_AT_type, cu) != NULL)
c906108c 13815 {
0fe7935b
DJ
13816 /* A variable with DW_AT_external is never static, but it
13817 may be block-scoped. */
13818 list_to_add = (cu->list_in_scope == &file_symbols
13819 ? &global_symbols : cu->list_in_scope);
13820
c906108c 13821 SYMBOL_CLASS (sym) = LOC_UNRESOLVED;
c906108c 13822 }
442ddf59
JK
13823 else if (!die_is_declaration (die, cu))
13824 {
13825 /* Use the default LOC_OPTIMIZED_OUT class. */
13826 gdb_assert (SYMBOL_CLASS (sym) == LOC_OPTIMIZED_OUT);
e37fd15a
SW
13827 if (!suppress_add)
13828 list_to_add = cu->list_in_scope;
442ddf59 13829 }
c906108c
SS
13830 }
13831 break;
13832 case DW_TAG_formal_parameter:
edb3359d
DJ
13833 /* If we are inside a function, mark this as an argument. If
13834 not, we might be looking at an argument to an inlined function
13835 when we do not have enough information to show inlined frames;
13836 pretend it's a local variable in that case so that the user can
13837 still see it. */
13838 if (context_stack_depth > 0
13839 && context_stack[context_stack_depth - 1].name != NULL)
13840 SYMBOL_IS_ARGUMENT (sym) = 1;
e142c38c 13841 attr = dwarf2_attr (die, DW_AT_location, cu);
c906108c
SS
13842 if (attr)
13843 {
e7c27a73 13844 var_decode_location (attr, sym, cu);
c906108c 13845 }
e142c38c 13846 attr = dwarf2_attr (die, DW_AT_const_value, cu);
c906108c
SS
13847 if (attr)
13848 {
e7c27a73 13849 dwarf2_const_value (attr, sym, cu);
c906108c 13850 }
f346a30d 13851
e37fd15a 13852 list_to_add = cu->list_in_scope;
c906108c
SS
13853 break;
13854 case DW_TAG_unspecified_parameters:
13855 /* From varargs functions; gdb doesn't seem to have any
13856 interest in this information, so just ignore it for now.
13857 (FIXME?) */
13858 break;
34eaf542
TT
13859 case DW_TAG_template_type_param:
13860 suppress_add = 1;
13861 /* Fall through. */
c906108c 13862 case DW_TAG_class_type:
680b30c7 13863 case DW_TAG_interface_type:
c906108c
SS
13864 case DW_TAG_structure_type:
13865 case DW_TAG_union_type:
72019c9c 13866 case DW_TAG_set_type:
c906108c
SS
13867 case DW_TAG_enumeration_type:
13868 SYMBOL_CLASS (sym) = LOC_TYPEDEF;
176620f1 13869 SYMBOL_DOMAIN (sym) = STRUCT_DOMAIN;
c906108c 13870
63d06c5c 13871 {
987504bb 13872 /* NOTE: carlton/2003-11-10: C++ and Java class symbols shouldn't
63d06c5c
DC
13873 really ever be static objects: otherwise, if you try
13874 to, say, break of a class's method and you're in a file
13875 which doesn't mention that class, it won't work unless
13876 the check for all static symbols in lookup_symbol_aux
13877 saves you. See the OtherFileClass tests in
13878 gdb.c++/namespace.exp. */
13879
e37fd15a 13880 if (!suppress_add)
34eaf542 13881 {
34eaf542
TT
13882 list_to_add = (cu->list_in_scope == &file_symbols
13883 && (cu->language == language_cplus
13884 || cu->language == language_java)
13885 ? &global_symbols : cu->list_in_scope);
63d06c5c 13886
64382290
TT
13887 /* The semantics of C++ state that "struct foo {
13888 ... }" also defines a typedef for "foo". A Java
13889 class declaration also defines a typedef for the
13890 class. */
13891 if (cu->language == language_cplus
13892 || cu->language == language_java
13893 || cu->language == language_ada)
13894 {
13895 /* The symbol's name is already allocated along
13896 with this objfile, so we don't need to
13897 duplicate it for the type. */
13898 if (TYPE_NAME (SYMBOL_TYPE (sym)) == 0)
13899 TYPE_NAME (SYMBOL_TYPE (sym)) = SYMBOL_SEARCH_NAME (sym);
13900 }
63d06c5c
DC
13901 }
13902 }
c906108c
SS
13903 break;
13904 case DW_TAG_typedef:
63d06c5c
DC
13905 SYMBOL_CLASS (sym) = LOC_TYPEDEF;
13906 SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
e37fd15a 13907 list_to_add = cu->list_in_scope;
63d06c5c 13908 break;
c906108c 13909 case DW_TAG_base_type:
a02abb62 13910 case DW_TAG_subrange_type:
c906108c 13911 SYMBOL_CLASS (sym) = LOC_TYPEDEF;
176620f1 13912 SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
e37fd15a 13913 list_to_add = cu->list_in_scope;
c906108c
SS
13914 break;
13915 case DW_TAG_enumerator:
e142c38c 13916 attr = dwarf2_attr (die, DW_AT_const_value, cu);
c906108c
SS
13917 if (attr)
13918 {
e7c27a73 13919 dwarf2_const_value (attr, sym, cu);
c906108c 13920 }
63d06c5c
DC
13921 {
13922 /* NOTE: carlton/2003-11-10: See comment above in the
13923 DW_TAG_class_type, etc. block. */
13924
e142c38c 13925 list_to_add = (cu->list_in_scope == &file_symbols
987504bb
JJ
13926 && (cu->language == language_cplus
13927 || cu->language == language_java)
e142c38c 13928 ? &global_symbols : cu->list_in_scope);
63d06c5c 13929 }
c906108c 13930 break;
5c4e30ca
DC
13931 case DW_TAG_namespace:
13932 SYMBOL_CLASS (sym) = LOC_TYPEDEF;
e37fd15a 13933 list_to_add = &global_symbols;
5c4e30ca 13934 break;
c906108c
SS
13935 default:
13936 /* Not a tag we recognize. Hopefully we aren't processing
13937 trash data, but since we must specifically ignore things
13938 we don't recognize, there is nothing else we should do at
0963b4bd 13939 this point. */
e2e0b3e5 13940 complaint (&symfile_complaints, _("unsupported tag: '%s'"),
4d3c2250 13941 dwarf_tag_name (die->tag));
c906108c
SS
13942 break;
13943 }
df8a16a1 13944
e37fd15a
SW
13945 if (suppress_add)
13946 {
13947 sym->hash_next = objfile->template_symbols;
13948 objfile->template_symbols = sym;
13949 list_to_add = NULL;
13950 }
13951
13952 if (list_to_add != NULL)
13953 add_symbol_to_list (sym, list_to_add);
13954
df8a16a1
DJ
13955 /* For the benefit of old versions of GCC, check for anonymous
13956 namespaces based on the demangled name. */
13957 if (!processing_has_namespace_info
94af9270 13958 && cu->language == language_cplus)
a10964d1 13959 cp_scan_for_anonymous_namespaces (sym, objfile);
c906108c
SS
13960 }
13961 return (sym);
13962}
13963
34eaf542
TT
13964/* A wrapper for new_symbol_full that always allocates a new symbol. */
13965
13966static struct symbol *
13967new_symbol (struct die_info *die, struct type *type, struct dwarf2_cu *cu)
13968{
13969 return new_symbol_full (die, type, cu, NULL);
13970}
13971
98bfdba5
PA
13972/* Given an attr with a DW_FORM_dataN value in host byte order,
13973 zero-extend it as appropriate for the symbol's type. The DWARF
13974 standard (v4) is not entirely clear about the meaning of using
13975 DW_FORM_dataN for a constant with a signed type, where the type is
13976 wider than the data. The conclusion of a discussion on the DWARF
13977 list was that this is unspecified. We choose to always zero-extend
13978 because that is the interpretation long in use by GCC. */
c906108c 13979
98bfdba5
PA
13980static gdb_byte *
13981dwarf2_const_value_data (struct attribute *attr, struct type *type,
13982 const char *name, struct obstack *obstack,
12df843f 13983 struct dwarf2_cu *cu, LONGEST *value, int bits)
c906108c 13984{
e7c27a73 13985 struct objfile *objfile = cu->objfile;
e17a4113
UW
13986 enum bfd_endian byte_order = bfd_big_endian (objfile->obfd) ?
13987 BFD_ENDIAN_BIG : BFD_ENDIAN_LITTLE;
98bfdba5
PA
13988 LONGEST l = DW_UNSND (attr);
13989
13990 if (bits < sizeof (*value) * 8)
13991 {
13992 l &= ((LONGEST) 1 << bits) - 1;
13993 *value = l;
13994 }
13995 else if (bits == sizeof (*value) * 8)
13996 *value = l;
13997 else
13998 {
13999 gdb_byte *bytes = obstack_alloc (obstack, bits / 8);
14000 store_unsigned_integer (bytes, bits / 8, byte_order, l);
14001 return bytes;
14002 }
14003
14004 return NULL;
14005}
14006
14007/* Read a constant value from an attribute. Either set *VALUE, or if
14008 the value does not fit in *VALUE, set *BYTES - either already
14009 allocated on the objfile obstack, or newly allocated on OBSTACK,
14010 or, set *BATON, if we translated the constant to a location
14011 expression. */
14012
14013static void
14014dwarf2_const_value_attr (struct attribute *attr, struct type *type,
14015 const char *name, struct obstack *obstack,
14016 struct dwarf2_cu *cu,
12df843f 14017 LONGEST *value, gdb_byte **bytes,
98bfdba5
PA
14018 struct dwarf2_locexpr_baton **baton)
14019{
14020 struct objfile *objfile = cu->objfile;
14021 struct comp_unit_head *cu_header = &cu->header;
c906108c 14022 struct dwarf_block *blk;
98bfdba5
PA
14023 enum bfd_endian byte_order = (bfd_big_endian (objfile->obfd) ?
14024 BFD_ENDIAN_BIG : BFD_ENDIAN_LITTLE);
14025
14026 *value = 0;
14027 *bytes = NULL;
14028 *baton = NULL;
c906108c
SS
14029
14030 switch (attr->form)
14031 {
14032 case DW_FORM_addr:
3019eac3 14033 case DW_FORM_GNU_addr_index:
ac56253d 14034 {
ac56253d
TT
14035 gdb_byte *data;
14036
98bfdba5
PA
14037 if (TYPE_LENGTH (type) != cu_header->addr_size)
14038 dwarf2_const_value_length_mismatch_complaint (name,
ac56253d 14039 cu_header->addr_size,
98bfdba5 14040 TYPE_LENGTH (type));
ac56253d
TT
14041 /* Symbols of this form are reasonably rare, so we just
14042 piggyback on the existing location code rather than writing
14043 a new implementation of symbol_computed_ops. */
98bfdba5
PA
14044 *baton = obstack_alloc (&objfile->objfile_obstack,
14045 sizeof (struct dwarf2_locexpr_baton));
14046 (*baton)->per_cu = cu->per_cu;
14047 gdb_assert ((*baton)->per_cu);
ac56253d 14048
98bfdba5
PA
14049 (*baton)->size = 2 + cu_header->addr_size;
14050 data = obstack_alloc (&objfile->objfile_obstack, (*baton)->size);
14051 (*baton)->data = data;
ac56253d
TT
14052
14053 data[0] = DW_OP_addr;
14054 store_unsigned_integer (&data[1], cu_header->addr_size,
14055 byte_order, DW_ADDR (attr));
14056 data[cu_header->addr_size + 1] = DW_OP_stack_value;
ac56253d 14057 }
c906108c 14058 break;
4ac36638 14059 case DW_FORM_string:
93b5768b 14060 case DW_FORM_strp:
3019eac3 14061 case DW_FORM_GNU_str_index:
98bfdba5
PA
14062 /* DW_STRING is already allocated on the objfile obstack, point
14063 directly to it. */
14064 *bytes = (gdb_byte *) DW_STRING (attr);
93b5768b 14065 break;
c906108c
SS
14066 case DW_FORM_block1:
14067 case DW_FORM_block2:
14068 case DW_FORM_block4:
14069 case DW_FORM_block:
2dc7f7b3 14070 case DW_FORM_exprloc:
c906108c 14071 blk = DW_BLOCK (attr);
98bfdba5
PA
14072 if (TYPE_LENGTH (type) != blk->size)
14073 dwarf2_const_value_length_mismatch_complaint (name, blk->size,
14074 TYPE_LENGTH (type));
14075 *bytes = blk->data;
c906108c 14076 break;
2df3850c
JM
14077
14078 /* The DW_AT_const_value attributes are supposed to carry the
14079 symbol's value "represented as it would be on the target
14080 architecture." By the time we get here, it's already been
14081 converted to host endianness, so we just need to sign- or
14082 zero-extend it as appropriate. */
14083 case DW_FORM_data1:
3e43a32a
MS
14084 *bytes = dwarf2_const_value_data (attr, type, name,
14085 obstack, cu, value, 8);
2df3850c 14086 break;
c906108c 14087 case DW_FORM_data2:
3e43a32a
MS
14088 *bytes = dwarf2_const_value_data (attr, type, name,
14089 obstack, cu, value, 16);
2df3850c 14090 break;
c906108c 14091 case DW_FORM_data4:
3e43a32a
MS
14092 *bytes = dwarf2_const_value_data (attr, type, name,
14093 obstack, cu, value, 32);
2df3850c 14094 break;
c906108c 14095 case DW_FORM_data8:
3e43a32a
MS
14096 *bytes = dwarf2_const_value_data (attr, type, name,
14097 obstack, cu, value, 64);
2df3850c
JM
14098 break;
14099
c906108c 14100 case DW_FORM_sdata:
98bfdba5 14101 *value = DW_SND (attr);
2df3850c
JM
14102 break;
14103
c906108c 14104 case DW_FORM_udata:
98bfdba5 14105 *value = DW_UNSND (attr);
c906108c 14106 break;
2df3850c 14107
c906108c 14108 default:
4d3c2250 14109 complaint (&symfile_complaints,
e2e0b3e5 14110 _("unsupported const value attribute form: '%s'"),
4d3c2250 14111 dwarf_form_name (attr->form));
98bfdba5 14112 *value = 0;
c906108c
SS
14113 break;
14114 }
14115}
14116
2df3850c 14117
98bfdba5
PA
14118/* Copy constant value from an attribute to a symbol. */
14119
2df3850c 14120static void
98bfdba5
PA
14121dwarf2_const_value (struct attribute *attr, struct symbol *sym,
14122 struct dwarf2_cu *cu)
2df3850c 14123{
98bfdba5
PA
14124 struct objfile *objfile = cu->objfile;
14125 struct comp_unit_head *cu_header = &cu->header;
12df843f 14126 LONGEST value;
98bfdba5
PA
14127 gdb_byte *bytes;
14128 struct dwarf2_locexpr_baton *baton;
2df3850c 14129
98bfdba5
PA
14130 dwarf2_const_value_attr (attr, SYMBOL_TYPE (sym),
14131 SYMBOL_PRINT_NAME (sym),
14132 &objfile->objfile_obstack, cu,
14133 &value, &bytes, &baton);
2df3850c 14134
98bfdba5
PA
14135 if (baton != NULL)
14136 {
14137 SYMBOL_COMPUTED_OPS (sym) = &dwarf2_locexpr_funcs;
14138 SYMBOL_LOCATION_BATON (sym) = baton;
14139 SYMBOL_CLASS (sym) = LOC_COMPUTED;
14140 }
14141 else if (bytes != NULL)
14142 {
14143 SYMBOL_VALUE_BYTES (sym) = bytes;
14144 SYMBOL_CLASS (sym) = LOC_CONST_BYTES;
14145 }
14146 else
14147 {
14148 SYMBOL_VALUE (sym) = value;
14149 SYMBOL_CLASS (sym) = LOC_CONST;
14150 }
2df3850c
JM
14151}
14152
c906108c
SS
14153/* Return the type of the die in question using its DW_AT_type attribute. */
14154
14155static struct type *
e7c27a73 14156die_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 14157{
c906108c 14158 struct attribute *type_attr;
c906108c 14159
e142c38c 14160 type_attr = dwarf2_attr (die, DW_AT_type, cu);
c906108c
SS
14161 if (!type_attr)
14162 {
14163 /* A missing DW_AT_type represents a void type. */
46bf5051 14164 return objfile_type (cu->objfile)->builtin_void;
c906108c 14165 }
348e048f 14166
673bfd45 14167 return lookup_die_type (die, type_attr, cu);
c906108c
SS
14168}
14169
b4ba55a1
JB
14170/* True iff CU's producer generates GNAT Ada auxiliary information
14171 that allows to find parallel types through that information instead
14172 of having to do expensive parallel lookups by type name. */
14173
14174static int
14175need_gnat_info (struct dwarf2_cu *cu)
14176{
14177 /* FIXME: brobecker/2010-10-12: As of now, only the AdaCore version
14178 of GNAT produces this auxiliary information, without any indication
14179 that it is produced. Part of enhancing the FSF version of GNAT
14180 to produce that information will be to put in place an indicator
14181 that we can use in order to determine whether the descriptive type
14182 info is available or not. One suggestion that has been made is
14183 to use a new attribute, attached to the CU die. For now, assume
14184 that the descriptive type info is not available. */
14185 return 0;
14186}
14187
b4ba55a1
JB
14188/* Return the auxiliary type of the die in question using its
14189 DW_AT_GNAT_descriptive_type attribute. Returns NULL if the
14190 attribute is not present. */
14191
14192static struct type *
14193die_descriptive_type (struct die_info *die, struct dwarf2_cu *cu)
14194{
b4ba55a1 14195 struct attribute *type_attr;
b4ba55a1
JB
14196
14197 type_attr = dwarf2_attr (die, DW_AT_GNAT_descriptive_type, cu);
14198 if (!type_attr)
14199 return NULL;
14200
673bfd45 14201 return lookup_die_type (die, type_attr, cu);
b4ba55a1
JB
14202}
14203
14204/* If DIE has a descriptive_type attribute, then set the TYPE's
14205 descriptive type accordingly. */
14206
14207static void
14208set_descriptive_type (struct type *type, struct die_info *die,
14209 struct dwarf2_cu *cu)
14210{
14211 struct type *descriptive_type = die_descriptive_type (die, cu);
14212
14213 if (descriptive_type)
14214 {
14215 ALLOCATE_GNAT_AUX_TYPE (type);
14216 TYPE_DESCRIPTIVE_TYPE (type) = descriptive_type;
14217 }
14218}
14219
c906108c
SS
14220/* Return the containing type of the die in question using its
14221 DW_AT_containing_type attribute. */
14222
14223static struct type *
e7c27a73 14224die_containing_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 14225{
c906108c 14226 struct attribute *type_attr;
c906108c 14227
e142c38c 14228 type_attr = dwarf2_attr (die, DW_AT_containing_type, cu);
33ac96f0
JK
14229 if (!type_attr)
14230 error (_("Dwarf Error: Problem turning containing type into gdb type "
14231 "[in module %s]"), cu->objfile->name);
14232
673bfd45 14233 return lookup_die_type (die, type_attr, cu);
c906108c
SS
14234}
14235
673bfd45
DE
14236/* Look up the type of DIE in CU using its type attribute ATTR.
14237 If there is no type substitute an error marker. */
14238
c906108c 14239static struct type *
673bfd45
DE
14240lookup_die_type (struct die_info *die, struct attribute *attr,
14241 struct dwarf2_cu *cu)
c906108c 14242{
bb5ed363 14243 struct objfile *objfile = cu->objfile;
f792889a
DJ
14244 struct type *this_type;
14245
673bfd45
DE
14246 /* First see if we have it cached. */
14247
14248 if (is_ref_attr (attr))
14249 {
b64f50a1 14250 sect_offset offset = dwarf2_get_ref_die_offset (attr);
673bfd45
DE
14251
14252 this_type = get_die_type_at_offset (offset, cu->per_cu);
14253 }
55f1336d 14254 else if (attr->form == DW_FORM_ref_sig8)
673bfd45
DE
14255 {
14256 struct signatured_type *sig_type = DW_SIGNATURED_TYPE (attr);
673bfd45
DE
14257
14258 /* sig_type will be NULL if the signatured type is missing from
14259 the debug info. */
14260 if (sig_type == NULL)
14261 error (_("Dwarf Error: Cannot find signatured DIE referenced from DIE "
14262 "at 0x%x [in module %s]"),
b64f50a1 14263 die->offset.sect_off, objfile->name);
673bfd45 14264
3019eac3
DE
14265 gdb_assert (sig_type->per_cu.is_debug_types);
14266 /* If we haven't filled in type_offset_in_section yet, then we
14267 haven't read the type in yet. */
14268 this_type = NULL;
14269 if (sig_type->type_offset_in_section.sect_off != 0)
14270 {
14271 this_type =
14272 get_die_type_at_offset (sig_type->type_offset_in_section,
14273 &sig_type->per_cu);
14274 }
673bfd45
DE
14275 }
14276 else
14277 {
14278 dump_die_for_error (die);
14279 error (_("Dwarf Error: Bad type attribute %s [in module %s]"),
bb5ed363 14280 dwarf_attr_name (attr->name), objfile->name);
673bfd45
DE
14281 }
14282
14283 /* If not cached we need to read it in. */
14284
14285 if (this_type == NULL)
14286 {
14287 struct die_info *type_die;
14288 struct dwarf2_cu *type_cu = cu;
14289
14290 type_die = follow_die_ref_or_sig (die, attr, &type_cu);
3019eac3
DE
14291 /* If we found the type now, it's probably because the type came
14292 from an inter-CU reference and the type's CU got expanded before
14293 ours. */
14294 this_type = get_die_type (type_die, type_cu);
14295 if (this_type == NULL)
14296 this_type = read_type_die_1 (type_die, type_cu);
673bfd45
DE
14297 }
14298
14299 /* If we still don't have a type use an error marker. */
14300
14301 if (this_type == NULL)
c906108c 14302 {
b00fdb78
TT
14303 char *message, *saved;
14304
14305 /* read_type_die already issued a complaint. */
14306 message = xstrprintf (_("<unknown type in %s, CU 0x%x, DIE 0x%x>"),
bb5ed363 14307 objfile->name,
b64f50a1
JK
14308 cu->header.offset.sect_off,
14309 die->offset.sect_off);
bb5ed363 14310 saved = obstack_copy0 (&objfile->objfile_obstack,
b00fdb78
TT
14311 message, strlen (message));
14312 xfree (message);
14313
bb5ed363 14314 this_type = init_type (TYPE_CODE_ERROR, 0, 0, saved, objfile);
c906108c 14315 }
673bfd45 14316
f792889a 14317 return this_type;
c906108c
SS
14318}
14319
673bfd45
DE
14320/* Return the type in DIE, CU.
14321 Returns NULL for invalid types.
14322
14323 This first does a lookup in the appropriate type_hash table,
14324 and only reads the die in if necessary.
14325
14326 NOTE: This can be called when reading in partial or full symbols. */
14327
f792889a 14328static struct type *
e7c27a73 14329read_type_die (struct die_info *die, struct dwarf2_cu *cu)
c906108c 14330{
f792889a
DJ
14331 struct type *this_type;
14332
14333 this_type = get_die_type (die, cu);
14334 if (this_type)
14335 return this_type;
14336
673bfd45
DE
14337 return read_type_die_1 (die, cu);
14338}
14339
14340/* Read the type in DIE, CU.
14341 Returns NULL for invalid types. */
14342
14343static struct type *
14344read_type_die_1 (struct die_info *die, struct dwarf2_cu *cu)
14345{
14346 struct type *this_type = NULL;
14347
c906108c
SS
14348 switch (die->tag)
14349 {
14350 case DW_TAG_class_type:
680b30c7 14351 case DW_TAG_interface_type:
c906108c
SS
14352 case DW_TAG_structure_type:
14353 case DW_TAG_union_type:
f792889a 14354 this_type = read_structure_type (die, cu);
c906108c
SS
14355 break;
14356 case DW_TAG_enumeration_type:
f792889a 14357 this_type = read_enumeration_type (die, cu);
c906108c
SS
14358 break;
14359 case DW_TAG_subprogram:
14360 case DW_TAG_subroutine_type:
edb3359d 14361 case DW_TAG_inlined_subroutine:
f792889a 14362 this_type = read_subroutine_type (die, cu);
c906108c
SS
14363 break;
14364 case DW_TAG_array_type:
f792889a 14365 this_type = read_array_type (die, cu);
c906108c 14366 break;
72019c9c 14367 case DW_TAG_set_type:
f792889a 14368 this_type = read_set_type (die, cu);
72019c9c 14369 break;
c906108c 14370 case DW_TAG_pointer_type:
f792889a 14371 this_type = read_tag_pointer_type (die, cu);
c906108c
SS
14372 break;
14373 case DW_TAG_ptr_to_member_type:
f792889a 14374 this_type = read_tag_ptr_to_member_type (die, cu);
c906108c
SS
14375 break;
14376 case DW_TAG_reference_type:
f792889a 14377 this_type = read_tag_reference_type (die, cu);
c906108c
SS
14378 break;
14379 case DW_TAG_const_type:
f792889a 14380 this_type = read_tag_const_type (die, cu);
c906108c
SS
14381 break;
14382 case DW_TAG_volatile_type:
f792889a 14383 this_type = read_tag_volatile_type (die, cu);
c906108c
SS
14384 break;
14385 case DW_TAG_string_type:
f792889a 14386 this_type = read_tag_string_type (die, cu);
c906108c
SS
14387 break;
14388 case DW_TAG_typedef:
f792889a 14389 this_type = read_typedef (die, cu);
c906108c 14390 break;
a02abb62 14391 case DW_TAG_subrange_type:
f792889a 14392 this_type = read_subrange_type (die, cu);
a02abb62 14393 break;
c906108c 14394 case DW_TAG_base_type:
f792889a 14395 this_type = read_base_type (die, cu);
c906108c 14396 break;
81a17f79 14397 case DW_TAG_unspecified_type:
f792889a 14398 this_type = read_unspecified_type (die, cu);
81a17f79 14399 break;
0114d602
DJ
14400 case DW_TAG_namespace:
14401 this_type = read_namespace_type (die, cu);
14402 break;
f55ee35c
JK
14403 case DW_TAG_module:
14404 this_type = read_module_type (die, cu);
14405 break;
c906108c 14406 default:
3e43a32a
MS
14407 complaint (&symfile_complaints,
14408 _("unexpected tag in read_type_die: '%s'"),
4d3c2250 14409 dwarf_tag_name (die->tag));
c906108c
SS
14410 break;
14411 }
63d06c5c 14412
f792889a 14413 return this_type;
63d06c5c
DC
14414}
14415
abc72ce4
DE
14416/* See if we can figure out if the class lives in a namespace. We do
14417 this by looking for a member function; its demangled name will
14418 contain namespace info, if there is any.
14419 Return the computed name or NULL.
14420 Space for the result is allocated on the objfile's obstack.
14421 This is the full-die version of guess_partial_die_structure_name.
14422 In this case we know DIE has no useful parent. */
14423
14424static char *
14425guess_full_die_structure_name (struct die_info *die, struct dwarf2_cu *cu)
14426{
14427 struct die_info *spec_die;
14428 struct dwarf2_cu *spec_cu;
14429 struct die_info *child;
14430
14431 spec_cu = cu;
14432 spec_die = die_specification (die, &spec_cu);
14433 if (spec_die != NULL)
14434 {
14435 die = spec_die;
14436 cu = spec_cu;
14437 }
14438
14439 for (child = die->child;
14440 child != NULL;
14441 child = child->sibling)
14442 {
14443 if (child->tag == DW_TAG_subprogram)
14444 {
14445 struct attribute *attr;
14446
14447 attr = dwarf2_attr (child, DW_AT_linkage_name, cu);
14448 if (attr == NULL)
14449 attr = dwarf2_attr (child, DW_AT_MIPS_linkage_name, cu);
14450 if (attr != NULL)
14451 {
14452 char *actual_name
14453 = language_class_name_from_physname (cu->language_defn,
14454 DW_STRING (attr));
14455 char *name = NULL;
14456
14457 if (actual_name != NULL)
14458 {
14459 char *die_name = dwarf2_name (die, cu);
14460
14461 if (die_name != NULL
14462 && strcmp (die_name, actual_name) != 0)
14463 {
14464 /* Strip off the class name from the full name.
14465 We want the prefix. */
14466 int die_name_len = strlen (die_name);
14467 int actual_name_len = strlen (actual_name);
14468
14469 /* Test for '::' as a sanity check. */
14470 if (actual_name_len > die_name_len + 2
3e43a32a
MS
14471 && actual_name[actual_name_len
14472 - die_name_len - 1] == ':')
abc72ce4
DE
14473 name =
14474 obsavestring (actual_name,
14475 actual_name_len - die_name_len - 2,
14476 &cu->objfile->objfile_obstack);
14477 }
14478 }
14479 xfree (actual_name);
14480 return name;
14481 }
14482 }
14483 }
14484
14485 return NULL;
14486}
14487
96408a79
SA
14488/* GCC might emit a nameless typedef that has a linkage name. Determine the
14489 prefix part in such case. See
14490 http://gcc.gnu.org/bugzilla/show_bug.cgi?id=47510. */
14491
14492static char *
14493anonymous_struct_prefix (struct die_info *die, struct dwarf2_cu *cu)
14494{
14495 struct attribute *attr;
14496 char *base;
14497
14498 if (die->tag != DW_TAG_class_type && die->tag != DW_TAG_interface_type
14499 && die->tag != DW_TAG_structure_type && die->tag != DW_TAG_union_type)
14500 return NULL;
14501
14502 attr = dwarf2_attr (die, DW_AT_name, cu);
14503 if (attr != NULL && DW_STRING (attr) != NULL)
14504 return NULL;
14505
14506 attr = dwarf2_attr (die, DW_AT_linkage_name, cu);
14507 if (attr == NULL)
14508 attr = dwarf2_attr (die, DW_AT_MIPS_linkage_name, cu);
14509 if (attr == NULL || DW_STRING (attr) == NULL)
14510 return NULL;
14511
14512 /* dwarf2_name had to be already called. */
14513 gdb_assert (DW_STRING_IS_CANONICAL (attr));
14514
14515 /* Strip the base name, keep any leading namespaces/classes. */
14516 base = strrchr (DW_STRING (attr), ':');
14517 if (base == NULL || base == DW_STRING (attr) || base[-1] != ':')
14518 return "";
14519
14520 return obsavestring (DW_STRING (attr), &base[-1] - DW_STRING (attr),
14521 &cu->objfile->objfile_obstack);
14522}
14523
fdde2d81 14524/* Return the name of the namespace/class that DIE is defined within,
0114d602 14525 or "" if we can't tell. The caller should not xfree the result.
fdde2d81 14526
0114d602
DJ
14527 For example, if we're within the method foo() in the following
14528 code:
14529
14530 namespace N {
14531 class C {
14532 void foo () {
14533 }
14534 };
14535 }
14536
14537 then determine_prefix on foo's die will return "N::C". */
fdde2d81 14538
0d5cff50 14539static const char *
e142c38c 14540determine_prefix (struct die_info *die, struct dwarf2_cu *cu)
63d06c5c 14541{
0114d602
DJ
14542 struct die_info *parent, *spec_die;
14543 struct dwarf2_cu *spec_cu;
14544 struct type *parent_type;
96408a79 14545 char *retval;
63d06c5c 14546
f55ee35c
JK
14547 if (cu->language != language_cplus && cu->language != language_java
14548 && cu->language != language_fortran)
0114d602
DJ
14549 return "";
14550
96408a79
SA
14551 retval = anonymous_struct_prefix (die, cu);
14552 if (retval)
14553 return retval;
14554
0114d602
DJ
14555 /* We have to be careful in the presence of DW_AT_specification.
14556 For example, with GCC 3.4, given the code
14557
14558 namespace N {
14559 void foo() {
14560 // Definition of N::foo.
14561 }
14562 }
14563
14564 then we'll have a tree of DIEs like this:
14565
14566 1: DW_TAG_compile_unit
14567 2: DW_TAG_namespace // N
14568 3: DW_TAG_subprogram // declaration of N::foo
14569 4: DW_TAG_subprogram // definition of N::foo
14570 DW_AT_specification // refers to die #3
14571
14572 Thus, when processing die #4, we have to pretend that we're in
14573 the context of its DW_AT_specification, namely the contex of die
14574 #3. */
14575 spec_cu = cu;
14576 spec_die = die_specification (die, &spec_cu);
14577 if (spec_die == NULL)
14578 parent = die->parent;
14579 else
63d06c5c 14580 {
0114d602
DJ
14581 parent = spec_die->parent;
14582 cu = spec_cu;
63d06c5c 14583 }
0114d602
DJ
14584
14585 if (parent == NULL)
14586 return "";
98bfdba5
PA
14587 else if (parent->building_fullname)
14588 {
14589 const char *name;
14590 const char *parent_name;
14591
14592 /* It has been seen on RealView 2.2 built binaries,
14593 DW_TAG_template_type_param types actually _defined_ as
14594 children of the parent class:
14595
14596 enum E {};
14597 template class <class Enum> Class{};
14598 Class<enum E> class_e;
14599
14600 1: DW_TAG_class_type (Class)
14601 2: DW_TAG_enumeration_type (E)
14602 3: DW_TAG_enumerator (enum1:0)
14603 3: DW_TAG_enumerator (enum2:1)
14604 ...
14605 2: DW_TAG_template_type_param
14606 DW_AT_type DW_FORM_ref_udata (E)
14607
14608 Besides being broken debug info, it can put GDB into an
14609 infinite loop. Consider:
14610
14611 When we're building the full name for Class<E>, we'll start
14612 at Class, and go look over its template type parameters,
14613 finding E. We'll then try to build the full name of E, and
14614 reach here. We're now trying to build the full name of E,
14615 and look over the parent DIE for containing scope. In the
14616 broken case, if we followed the parent DIE of E, we'd again
14617 find Class, and once again go look at its template type
14618 arguments, etc., etc. Simply don't consider such parent die
14619 as source-level parent of this die (it can't be, the language
14620 doesn't allow it), and break the loop here. */
14621 name = dwarf2_name (die, cu);
14622 parent_name = dwarf2_name (parent, cu);
14623 complaint (&symfile_complaints,
14624 _("template param type '%s' defined within parent '%s'"),
14625 name ? name : "<unknown>",
14626 parent_name ? parent_name : "<unknown>");
14627 return "";
14628 }
63d06c5c 14629 else
0114d602
DJ
14630 switch (parent->tag)
14631 {
63d06c5c 14632 case DW_TAG_namespace:
0114d602 14633 parent_type = read_type_die (parent, cu);
acebe513
UW
14634 /* GCC 4.0 and 4.1 had a bug (PR c++/28460) where they generated bogus
14635 DW_TAG_namespace DIEs with a name of "::" for the global namespace.
14636 Work around this problem here. */
14637 if (cu->language == language_cplus
14638 && strcmp (TYPE_TAG_NAME (parent_type), "::") == 0)
14639 return "";
0114d602
DJ
14640 /* We give a name to even anonymous namespaces. */
14641 return TYPE_TAG_NAME (parent_type);
63d06c5c 14642 case DW_TAG_class_type:
680b30c7 14643 case DW_TAG_interface_type:
63d06c5c 14644 case DW_TAG_structure_type:
0114d602 14645 case DW_TAG_union_type:
f55ee35c 14646 case DW_TAG_module:
0114d602
DJ
14647 parent_type = read_type_die (parent, cu);
14648 if (TYPE_TAG_NAME (parent_type) != NULL)
14649 return TYPE_TAG_NAME (parent_type);
14650 else
14651 /* An anonymous structure is only allowed non-static data
14652 members; no typedefs, no member functions, et cetera.
14653 So it does not need a prefix. */
14654 return "";
abc72ce4 14655 case DW_TAG_compile_unit:
95554aad 14656 case DW_TAG_partial_unit:
abc72ce4
DE
14657 /* gcc-4.5 -gdwarf-4 can drop the enclosing namespace. Cope. */
14658 if (cu->language == language_cplus
8b70b953 14659 && !VEC_empty (dwarf2_section_info_def, dwarf2_per_objfile->types)
abc72ce4
DE
14660 && die->child != NULL
14661 && (die->tag == DW_TAG_class_type
14662 || die->tag == DW_TAG_structure_type
14663 || die->tag == DW_TAG_union_type))
14664 {
14665 char *name = guess_full_die_structure_name (die, cu);
14666 if (name != NULL)
14667 return name;
14668 }
14669 return "";
63d06c5c 14670 default:
8176b9b8 14671 return determine_prefix (parent, cu);
63d06c5c 14672 }
63d06c5c
DC
14673}
14674
3e43a32a
MS
14675/* Return a newly-allocated string formed by concatenating PREFIX and SUFFIX
14676 with appropriate separator. If PREFIX or SUFFIX is NULL or empty, then
14677 simply copy the SUFFIX or PREFIX, respectively. If OBS is non-null, perform
14678 an obconcat, otherwise allocate storage for the result. The CU argument is
14679 used to determine the language and hence, the appropriate separator. */
987504bb 14680
f55ee35c 14681#define MAX_SEP_LEN 7 /* strlen ("__") + strlen ("_MOD_") */
63d06c5c
DC
14682
14683static char *
f55ee35c
JK
14684typename_concat (struct obstack *obs, const char *prefix, const char *suffix,
14685 int physname, struct dwarf2_cu *cu)
63d06c5c 14686{
f55ee35c 14687 const char *lead = "";
5c315b68 14688 const char *sep;
63d06c5c 14689
3e43a32a
MS
14690 if (suffix == NULL || suffix[0] == '\0'
14691 || prefix == NULL || prefix[0] == '\0')
987504bb
JJ
14692 sep = "";
14693 else if (cu->language == language_java)
14694 sep = ".";
f55ee35c
JK
14695 else if (cu->language == language_fortran && physname)
14696 {
14697 /* This is gfortran specific mangling. Normally DW_AT_linkage_name or
14698 DW_AT_MIPS_linkage_name is preferred and used instead. */
14699
14700 lead = "__";
14701 sep = "_MOD_";
14702 }
987504bb
JJ
14703 else
14704 sep = "::";
63d06c5c 14705
6dd47d34
DE
14706 if (prefix == NULL)
14707 prefix = "";
14708 if (suffix == NULL)
14709 suffix = "";
14710
987504bb
JJ
14711 if (obs == NULL)
14712 {
3e43a32a
MS
14713 char *retval
14714 = xmalloc (strlen (prefix) + MAX_SEP_LEN + strlen (suffix) + 1);
9a619af0 14715
f55ee35c
JK
14716 strcpy (retval, lead);
14717 strcat (retval, prefix);
6dd47d34
DE
14718 strcat (retval, sep);
14719 strcat (retval, suffix);
63d06c5c
DC
14720 return retval;
14721 }
987504bb
JJ
14722 else
14723 {
14724 /* We have an obstack. */
f55ee35c 14725 return obconcat (obs, lead, prefix, sep, suffix, (char *) NULL);
987504bb 14726 }
63d06c5c
DC
14727}
14728
c906108c
SS
14729/* Return sibling of die, NULL if no sibling. */
14730
f9aca02d 14731static struct die_info *
fba45db2 14732sibling_die (struct die_info *die)
c906108c 14733{
639d11d3 14734 return die->sibling;
c906108c
SS
14735}
14736
71c25dea
TT
14737/* Get name of a die, return NULL if not found. */
14738
14739static char *
14740dwarf2_canonicalize_name (char *name, struct dwarf2_cu *cu,
14741 struct obstack *obstack)
14742{
14743 if (name && cu->language == language_cplus)
14744 {
14745 char *canon_name = cp_canonicalize_string (name);
14746
14747 if (canon_name != NULL)
14748 {
14749 if (strcmp (canon_name, name) != 0)
14750 name = obsavestring (canon_name, strlen (canon_name),
14751 obstack);
14752 xfree (canon_name);
14753 }
14754 }
14755
14756 return name;
c906108c
SS
14757}
14758
9219021c
DC
14759/* Get name of a die, return NULL if not found. */
14760
14761static char *
e142c38c 14762dwarf2_name (struct die_info *die, struct dwarf2_cu *cu)
9219021c
DC
14763{
14764 struct attribute *attr;
14765
e142c38c 14766 attr = dwarf2_attr (die, DW_AT_name, cu);
53832f31
TT
14767 if ((!attr || !DW_STRING (attr))
14768 && die->tag != DW_TAG_class_type
14769 && die->tag != DW_TAG_interface_type
14770 && die->tag != DW_TAG_structure_type
14771 && die->tag != DW_TAG_union_type)
71c25dea
TT
14772 return NULL;
14773
14774 switch (die->tag)
14775 {
14776 case DW_TAG_compile_unit:
95554aad 14777 case DW_TAG_partial_unit:
71c25dea
TT
14778 /* Compilation units have a DW_AT_name that is a filename, not
14779 a source language identifier. */
14780 case DW_TAG_enumeration_type:
14781 case DW_TAG_enumerator:
14782 /* These tags always have simple identifiers already; no need
14783 to canonicalize them. */
14784 return DW_STRING (attr);
907af001 14785
418835cc
KS
14786 case DW_TAG_subprogram:
14787 /* Java constructors will all be named "<init>", so return
14788 the class name when we see this special case. */
14789 if (cu->language == language_java
14790 && DW_STRING (attr) != NULL
14791 && strcmp (DW_STRING (attr), "<init>") == 0)
14792 {
14793 struct dwarf2_cu *spec_cu = cu;
14794 struct die_info *spec_die;
14795
14796 /* GCJ will output '<init>' for Java constructor names.
14797 For this special case, return the name of the parent class. */
14798
14799 /* GCJ may output suprogram DIEs with AT_specification set.
14800 If so, use the name of the specified DIE. */
14801 spec_die = die_specification (die, &spec_cu);
14802 if (spec_die != NULL)
14803 return dwarf2_name (spec_die, spec_cu);
14804
14805 do
14806 {
14807 die = die->parent;
14808 if (die->tag == DW_TAG_class_type)
14809 return dwarf2_name (die, cu);
14810 }
95554aad
TT
14811 while (die->tag != DW_TAG_compile_unit
14812 && die->tag != DW_TAG_partial_unit);
418835cc 14813 }
907af001
UW
14814 break;
14815
14816 case DW_TAG_class_type:
14817 case DW_TAG_interface_type:
14818 case DW_TAG_structure_type:
14819 case DW_TAG_union_type:
14820 /* Some GCC versions emit spurious DW_AT_name attributes for unnamed
14821 structures or unions. These were of the form "._%d" in GCC 4.1,
14822 or simply "<anonymous struct>" or "<anonymous union>" in GCC 4.3
14823 and GCC 4.4. We work around this problem by ignoring these. */
53832f31
TT
14824 if (attr && DW_STRING (attr)
14825 && (strncmp (DW_STRING (attr), "._", 2) == 0
14826 || strncmp (DW_STRING (attr), "<anonymous", 10) == 0))
907af001 14827 return NULL;
53832f31
TT
14828
14829 /* GCC might emit a nameless typedef that has a linkage name. See
14830 http://gcc.gnu.org/bugzilla/show_bug.cgi?id=47510. */
14831 if (!attr || DW_STRING (attr) == NULL)
14832 {
df5c6c50 14833 char *demangled = NULL;
53832f31
TT
14834
14835 attr = dwarf2_attr (die, DW_AT_linkage_name, cu);
14836 if (attr == NULL)
14837 attr = dwarf2_attr (die, DW_AT_MIPS_linkage_name, cu);
14838
14839 if (attr == NULL || DW_STRING (attr) == NULL)
14840 return NULL;
14841
df5c6c50
JK
14842 /* Avoid demangling DW_STRING (attr) the second time on a second
14843 call for the same DIE. */
14844 if (!DW_STRING_IS_CANONICAL (attr))
14845 demangled = cplus_demangle (DW_STRING (attr), DMGL_TYPES);
53832f31
TT
14846
14847 if (demangled)
14848 {
96408a79
SA
14849 char *base;
14850
53832f31 14851 /* FIXME: we already did this for the partial symbol... */
96408a79
SA
14852 DW_STRING (attr) = obsavestring (demangled, strlen (demangled),
14853 &cu->objfile->objfile_obstack);
53832f31
TT
14854 DW_STRING_IS_CANONICAL (attr) = 1;
14855 xfree (demangled);
96408a79
SA
14856
14857 /* Strip any leading namespaces/classes, keep only the base name.
14858 DW_AT_name for named DIEs does not contain the prefixes. */
14859 base = strrchr (DW_STRING (attr), ':');
14860 if (base && base > DW_STRING (attr) && base[-1] == ':')
14861 return &base[1];
14862 else
14863 return DW_STRING (attr);
53832f31
TT
14864 }
14865 }
907af001
UW
14866 break;
14867
71c25dea 14868 default:
907af001
UW
14869 break;
14870 }
14871
14872 if (!DW_STRING_IS_CANONICAL (attr))
14873 {
14874 DW_STRING (attr)
14875 = dwarf2_canonicalize_name (DW_STRING (attr), cu,
14876 &cu->objfile->objfile_obstack);
14877 DW_STRING_IS_CANONICAL (attr) = 1;
71c25dea 14878 }
907af001 14879 return DW_STRING (attr);
9219021c
DC
14880}
14881
14882/* Return the die that this die in an extension of, or NULL if there
f2f0e013
DJ
14883 is none. *EXT_CU is the CU containing DIE on input, and the CU
14884 containing the return value on output. */
9219021c
DC
14885
14886static struct die_info *
f2f0e013 14887dwarf2_extension (struct die_info *die, struct dwarf2_cu **ext_cu)
9219021c
DC
14888{
14889 struct attribute *attr;
9219021c 14890
f2f0e013 14891 attr = dwarf2_attr (die, DW_AT_extension, *ext_cu);
9219021c
DC
14892 if (attr == NULL)
14893 return NULL;
14894
f2f0e013 14895 return follow_die_ref (die, attr, ext_cu);
9219021c
DC
14896}
14897
c906108c
SS
14898/* Convert a DIE tag into its string name. */
14899
f39c6ffd 14900static const char *
aa1ee363 14901dwarf_tag_name (unsigned tag)
c906108c 14902{
f39c6ffd
TT
14903 const char *name = get_DW_TAG_name (tag);
14904
14905 if (name == NULL)
14906 return "DW_TAG_<unknown>";
14907
14908 return name;
c906108c
SS
14909}
14910
14911/* Convert a DWARF attribute code into its string name. */
14912
f39c6ffd 14913static const char *
aa1ee363 14914dwarf_attr_name (unsigned attr)
c906108c 14915{
f39c6ffd
TT
14916 const char *name;
14917
c764a876 14918#ifdef MIPS /* collides with DW_AT_HP_block_index */
f39c6ffd
TT
14919 if (attr == DW_AT_MIPS_fde)
14920 return "DW_AT_MIPS_fde";
14921#else
14922 if (attr == DW_AT_HP_block_index)
14923 return "DW_AT_HP_block_index";
c764a876 14924#endif
f39c6ffd
TT
14925
14926 name = get_DW_AT_name (attr);
14927
14928 if (name == NULL)
14929 return "DW_AT_<unknown>";
14930
14931 return name;
c906108c
SS
14932}
14933
14934/* Convert a DWARF value form code into its string name. */
14935
f39c6ffd 14936static const char *
aa1ee363 14937dwarf_form_name (unsigned form)
c906108c 14938{
f39c6ffd
TT
14939 const char *name = get_DW_FORM_name (form);
14940
14941 if (name == NULL)
14942 return "DW_FORM_<unknown>";
14943
14944 return name;
c906108c
SS
14945}
14946
14947static char *
fba45db2 14948dwarf_bool_name (unsigned mybool)
c906108c
SS
14949{
14950 if (mybool)
14951 return "TRUE";
14952 else
14953 return "FALSE";
14954}
14955
14956/* Convert a DWARF type code into its string name. */
14957
f39c6ffd 14958static const char *
aa1ee363 14959dwarf_type_encoding_name (unsigned enc)
c906108c 14960{
f39c6ffd 14961 const char *name = get_DW_ATE_name (enc);
c906108c 14962
f39c6ffd
TT
14963 if (name == NULL)
14964 return "DW_ATE_<unknown>";
c906108c 14965
f39c6ffd 14966 return name;
c906108c 14967}
c906108c 14968
f9aca02d 14969static void
d97bc12b 14970dump_die_shallow (struct ui_file *f, int indent, struct die_info *die)
c906108c
SS
14971{
14972 unsigned int i;
14973
d97bc12b
DE
14974 print_spaces (indent, f);
14975 fprintf_unfiltered (f, "Die: %s (abbrev %d, offset 0x%x)\n",
b64f50a1 14976 dwarf_tag_name (die->tag), die->abbrev, die->offset.sect_off);
d97bc12b
DE
14977
14978 if (die->parent != NULL)
14979 {
14980 print_spaces (indent, f);
14981 fprintf_unfiltered (f, " parent at offset: 0x%x\n",
b64f50a1 14982 die->parent->offset.sect_off);
d97bc12b
DE
14983 }
14984
14985 print_spaces (indent, f);
14986 fprintf_unfiltered (f, " has children: %s\n",
639d11d3 14987 dwarf_bool_name (die->child != NULL));
c906108c 14988
d97bc12b
DE
14989 print_spaces (indent, f);
14990 fprintf_unfiltered (f, " attributes:\n");
14991
c906108c
SS
14992 for (i = 0; i < die->num_attrs; ++i)
14993 {
d97bc12b
DE
14994 print_spaces (indent, f);
14995 fprintf_unfiltered (f, " %s (%s) ",
c906108c
SS
14996 dwarf_attr_name (die->attrs[i].name),
14997 dwarf_form_name (die->attrs[i].form));
d97bc12b 14998
c906108c
SS
14999 switch (die->attrs[i].form)
15000 {
c906108c 15001 case DW_FORM_addr:
3019eac3 15002 case DW_FORM_GNU_addr_index:
d97bc12b 15003 fprintf_unfiltered (f, "address: ");
5af949e3 15004 fputs_filtered (hex_string (DW_ADDR (&die->attrs[i])), f);
c906108c
SS
15005 break;
15006 case DW_FORM_block2:
15007 case DW_FORM_block4:
15008 case DW_FORM_block:
15009 case DW_FORM_block1:
3e43a32a
MS
15010 fprintf_unfiltered (f, "block: size %d",
15011 DW_BLOCK (&die->attrs[i])->size);
c906108c 15012 break;
2dc7f7b3
TT
15013 case DW_FORM_exprloc:
15014 fprintf_unfiltered (f, "expression: size %u",
15015 DW_BLOCK (&die->attrs[i])->size);
15016 break;
4568ecf9
DE
15017 case DW_FORM_ref_addr:
15018 fprintf_unfiltered (f, "ref address: ");
15019 fputs_filtered (hex_string (DW_UNSND (&die->attrs[i])), f);
15020 break;
10b3939b
DJ
15021 case DW_FORM_ref1:
15022 case DW_FORM_ref2:
15023 case DW_FORM_ref4:
4568ecf9
DE
15024 case DW_FORM_ref8:
15025 case DW_FORM_ref_udata:
d97bc12b 15026 fprintf_unfiltered (f, "constant ref: 0x%lx (adjusted)",
4568ecf9 15027 (long) (DW_UNSND (&die->attrs[i])));
10b3939b 15028 break;
c906108c
SS
15029 case DW_FORM_data1:
15030 case DW_FORM_data2:
15031 case DW_FORM_data4:
ce5d95e1 15032 case DW_FORM_data8:
c906108c
SS
15033 case DW_FORM_udata:
15034 case DW_FORM_sdata:
43bbcdc2
PH
15035 fprintf_unfiltered (f, "constant: %s",
15036 pulongest (DW_UNSND (&die->attrs[i])));
c906108c 15037 break;
2dc7f7b3
TT
15038 case DW_FORM_sec_offset:
15039 fprintf_unfiltered (f, "section offset: %s",
15040 pulongest (DW_UNSND (&die->attrs[i])));
15041 break;
55f1336d 15042 case DW_FORM_ref_sig8:
348e048f
DE
15043 if (DW_SIGNATURED_TYPE (&die->attrs[i]) != NULL)
15044 fprintf_unfiltered (f, "signatured type, offset: 0x%x",
b64f50a1 15045 DW_SIGNATURED_TYPE (&die->attrs[i])->per_cu.offset.sect_off);
348e048f
DE
15046 else
15047 fprintf_unfiltered (f, "signatured type, offset: unknown");
15048 break;
c906108c 15049 case DW_FORM_string:
4bdf3d34 15050 case DW_FORM_strp:
3019eac3 15051 case DW_FORM_GNU_str_index:
8285870a 15052 fprintf_unfiltered (f, "string: \"%s\" (%s canonicalized)",
c906108c 15053 DW_STRING (&die->attrs[i])
8285870a
JK
15054 ? DW_STRING (&die->attrs[i]) : "",
15055 DW_STRING_IS_CANONICAL (&die->attrs[i]) ? "is" : "not");
c906108c
SS
15056 break;
15057 case DW_FORM_flag:
15058 if (DW_UNSND (&die->attrs[i]))
d97bc12b 15059 fprintf_unfiltered (f, "flag: TRUE");
c906108c 15060 else
d97bc12b 15061 fprintf_unfiltered (f, "flag: FALSE");
c906108c 15062 break;
2dc7f7b3
TT
15063 case DW_FORM_flag_present:
15064 fprintf_unfiltered (f, "flag: TRUE");
15065 break;
a8329558 15066 case DW_FORM_indirect:
0963b4bd
MS
15067 /* The reader will have reduced the indirect form to
15068 the "base form" so this form should not occur. */
3e43a32a
MS
15069 fprintf_unfiltered (f,
15070 "unexpected attribute form: DW_FORM_indirect");
a8329558 15071 break;
c906108c 15072 default:
d97bc12b 15073 fprintf_unfiltered (f, "unsupported attribute form: %d.",
c5aa993b 15074 die->attrs[i].form);
d97bc12b 15075 break;
c906108c 15076 }
d97bc12b 15077 fprintf_unfiltered (f, "\n");
c906108c
SS
15078 }
15079}
15080
f9aca02d 15081static void
d97bc12b 15082dump_die_for_error (struct die_info *die)
c906108c 15083{
d97bc12b
DE
15084 dump_die_shallow (gdb_stderr, 0, die);
15085}
15086
15087static void
15088dump_die_1 (struct ui_file *f, int level, int max_level, struct die_info *die)
15089{
15090 int indent = level * 4;
15091
15092 gdb_assert (die != NULL);
15093
15094 if (level >= max_level)
15095 return;
15096
15097 dump_die_shallow (f, indent, die);
15098
15099 if (die->child != NULL)
c906108c 15100 {
d97bc12b
DE
15101 print_spaces (indent, f);
15102 fprintf_unfiltered (f, " Children:");
15103 if (level + 1 < max_level)
15104 {
15105 fprintf_unfiltered (f, "\n");
15106 dump_die_1 (f, level + 1, max_level, die->child);
15107 }
15108 else
15109 {
3e43a32a
MS
15110 fprintf_unfiltered (f,
15111 " [not printed, max nesting level reached]\n");
d97bc12b
DE
15112 }
15113 }
15114
15115 if (die->sibling != NULL && level > 0)
15116 {
15117 dump_die_1 (f, level, max_level, die->sibling);
c906108c
SS
15118 }
15119}
15120
d97bc12b
DE
15121/* This is called from the pdie macro in gdbinit.in.
15122 It's not static so gcc will keep a copy callable from gdb. */
15123
15124void
15125dump_die (struct die_info *die, int max_level)
15126{
15127 dump_die_1 (gdb_stdlog, 0, max_level, die);
15128}
15129
f9aca02d 15130static void
51545339 15131store_in_ref_table (struct die_info *die, struct dwarf2_cu *cu)
c906108c 15132{
51545339 15133 void **slot;
c906108c 15134
b64f50a1
JK
15135 slot = htab_find_slot_with_hash (cu->die_hash, die, die->offset.sect_off,
15136 INSERT);
51545339
DJ
15137
15138 *slot = die;
c906108c
SS
15139}
15140
b64f50a1
JK
15141/* DW_ADDR is always stored already as sect_offset; despite for the forms
15142 besides DW_FORM_ref_addr it is stored as cu_offset in the DWARF file. */
15143
93311388
DE
15144static int
15145is_ref_attr (struct attribute *attr)
c906108c 15146{
c906108c
SS
15147 switch (attr->form)
15148 {
15149 case DW_FORM_ref_addr:
c906108c
SS
15150 case DW_FORM_ref1:
15151 case DW_FORM_ref2:
15152 case DW_FORM_ref4:
613e1657 15153 case DW_FORM_ref8:
c906108c 15154 case DW_FORM_ref_udata:
93311388 15155 return 1;
c906108c 15156 default:
93311388 15157 return 0;
c906108c 15158 }
93311388
DE
15159}
15160
b64f50a1
JK
15161/* Return DIE offset of ATTR. Return 0 with complaint if ATTR is not of the
15162 required kind. */
15163
15164static sect_offset
93311388
DE
15165dwarf2_get_ref_die_offset (struct attribute *attr)
15166{
4568ecf9 15167 sect_offset retval = { DW_UNSND (attr) };
b64f50a1 15168
93311388 15169 if (is_ref_attr (attr))
b64f50a1 15170 return retval;
93311388 15171
b64f50a1 15172 retval.sect_off = 0;
93311388
DE
15173 complaint (&symfile_complaints,
15174 _("unsupported die ref attribute form: '%s'"),
15175 dwarf_form_name (attr->form));
b64f50a1 15176 return retval;
c906108c
SS
15177}
15178
43bbcdc2
PH
15179/* Return the constant value held by ATTR. Return DEFAULT_VALUE if
15180 * the value held by the attribute is not constant. */
a02abb62 15181
43bbcdc2 15182static LONGEST
a02abb62
JB
15183dwarf2_get_attr_constant_value (struct attribute *attr, int default_value)
15184{
15185 if (attr->form == DW_FORM_sdata)
15186 return DW_SND (attr);
15187 else if (attr->form == DW_FORM_udata
15188 || attr->form == DW_FORM_data1
15189 || attr->form == DW_FORM_data2
15190 || attr->form == DW_FORM_data4
15191 || attr->form == DW_FORM_data8)
15192 return DW_UNSND (attr);
15193 else
15194 {
3e43a32a
MS
15195 complaint (&symfile_complaints,
15196 _("Attribute value is not a constant (%s)"),
a02abb62
JB
15197 dwarf_form_name (attr->form));
15198 return default_value;
15199 }
15200}
15201
03dd20cc 15202/* THIS_CU has a reference to PER_CU. If necessary, load the new compilation
348e048f
DE
15203 unit and add it to our queue.
15204 The result is non-zero if PER_CU was queued, otherwise the result is zero
15205 meaning either PER_CU is already queued or it is already loaded. */
03dd20cc 15206
348e048f 15207static int
03dd20cc 15208maybe_queue_comp_unit (struct dwarf2_cu *this_cu,
95554aad
TT
15209 struct dwarf2_per_cu_data *per_cu,
15210 enum language pretend_language)
03dd20cc 15211{
98bfdba5
PA
15212 /* We may arrive here during partial symbol reading, if we need full
15213 DIEs to process an unusual case (e.g. template arguments). Do
15214 not queue PER_CU, just tell our caller to load its DIEs. */
15215 if (dwarf2_per_objfile->reading_partial_symbols)
15216 {
15217 if (per_cu->cu == NULL || per_cu->cu->dies == NULL)
15218 return 1;
15219 return 0;
15220 }
15221
03dd20cc
DJ
15222 /* Mark the dependence relation so that we don't flush PER_CU
15223 too early. */
15224 dwarf2_add_dependence (this_cu, per_cu);
15225
15226 /* If it's already on the queue, we have nothing to do. */
15227 if (per_cu->queued)
348e048f 15228 return 0;
03dd20cc
DJ
15229
15230 /* If the compilation unit is already loaded, just mark it as
15231 used. */
15232 if (per_cu->cu != NULL)
15233 {
15234 per_cu->cu->last_used = 0;
348e048f 15235 return 0;
03dd20cc
DJ
15236 }
15237
15238 /* Add it to the queue. */
95554aad 15239 queue_comp_unit (per_cu, pretend_language);
348e048f
DE
15240
15241 return 1;
15242}
15243
15244/* Follow reference or signature attribute ATTR of SRC_DIE.
15245 On entry *REF_CU is the CU of SRC_DIE.
15246 On exit *REF_CU is the CU of the result. */
15247
15248static struct die_info *
15249follow_die_ref_or_sig (struct die_info *src_die, struct attribute *attr,
15250 struct dwarf2_cu **ref_cu)
15251{
15252 struct die_info *die;
15253
15254 if (is_ref_attr (attr))
15255 die = follow_die_ref (src_die, attr, ref_cu);
55f1336d 15256 else if (attr->form == DW_FORM_ref_sig8)
348e048f
DE
15257 die = follow_die_sig (src_die, attr, ref_cu);
15258 else
15259 {
15260 dump_die_for_error (src_die);
15261 error (_("Dwarf Error: Expected reference attribute [in module %s]"),
15262 (*ref_cu)->objfile->name);
15263 }
15264
15265 return die;
03dd20cc
DJ
15266}
15267
5c631832 15268/* Follow reference OFFSET.
673bfd45
DE
15269 On entry *REF_CU is the CU of the source die referencing OFFSET.
15270 On exit *REF_CU is the CU of the result.
15271 Returns NULL if OFFSET is invalid. */
f504f079 15272
f9aca02d 15273static struct die_info *
b64f50a1 15274follow_die_offset (sect_offset offset, struct dwarf2_cu **ref_cu)
c906108c 15275{
10b3939b 15276 struct die_info temp_die;
f2f0e013 15277 struct dwarf2_cu *target_cu, *cu = *ref_cu;
10b3939b 15278
348e048f
DE
15279 gdb_assert (cu->per_cu != NULL);
15280
98bfdba5
PA
15281 target_cu = cu;
15282
3019eac3 15283 if (cu->per_cu->is_debug_types)
348e048f
DE
15284 {
15285 /* .debug_types CUs cannot reference anything outside their CU.
15286 If they need to, they have to reference a signatured type via
55f1336d 15287 DW_FORM_ref_sig8. */
348e048f 15288 if (! offset_in_cu_p (&cu->header, offset))
5c631832 15289 return NULL;
348e048f
DE
15290 }
15291 else if (! offset_in_cu_p (&cu->header, offset))
10b3939b
DJ
15292 {
15293 struct dwarf2_per_cu_data *per_cu;
9a619af0 15294
45452591 15295 per_cu = dwarf2_find_containing_comp_unit (offset, cu->objfile);
03dd20cc
DJ
15296
15297 /* If necessary, add it to the queue and load its DIEs. */
95554aad
TT
15298 if (maybe_queue_comp_unit (cu, per_cu, cu->language))
15299 load_full_comp_unit (per_cu, cu->language);
03dd20cc 15300
10b3939b
DJ
15301 target_cu = per_cu->cu;
15302 }
98bfdba5
PA
15303 else if (cu->dies == NULL)
15304 {
15305 /* We're loading full DIEs during partial symbol reading. */
15306 gdb_assert (dwarf2_per_objfile->reading_partial_symbols);
95554aad 15307 load_full_comp_unit (cu->per_cu, language_minimal);
98bfdba5 15308 }
c906108c 15309
f2f0e013 15310 *ref_cu = target_cu;
51545339 15311 temp_die.offset = offset;
b64f50a1 15312 return htab_find_with_hash (target_cu->die_hash, &temp_die, offset.sect_off);
5c631832 15313}
10b3939b 15314
5c631832
JK
15315/* Follow reference attribute ATTR of SRC_DIE.
15316 On entry *REF_CU is the CU of SRC_DIE.
15317 On exit *REF_CU is the CU of the result. */
15318
15319static struct die_info *
15320follow_die_ref (struct die_info *src_die, struct attribute *attr,
15321 struct dwarf2_cu **ref_cu)
15322{
b64f50a1 15323 sect_offset offset = dwarf2_get_ref_die_offset (attr);
5c631832
JK
15324 struct dwarf2_cu *cu = *ref_cu;
15325 struct die_info *die;
15326
15327 die = follow_die_offset (offset, ref_cu);
15328 if (!die)
15329 error (_("Dwarf Error: Cannot find DIE at 0x%x referenced from DIE "
15330 "at 0x%x [in module %s]"),
b64f50a1 15331 offset.sect_off, src_die->offset.sect_off, cu->objfile->name);
348e048f 15332
5c631832
JK
15333 return die;
15334}
15335
d83e736b
JK
15336/* Return DWARF block referenced by DW_AT_location of DIE at OFFSET at PER_CU.
15337 Returned value is intended for DW_OP_call*. Returned
15338 dwarf2_locexpr_baton->data has lifetime of PER_CU->OBJFILE. */
5c631832
JK
15339
15340struct dwarf2_locexpr_baton
b64f50a1 15341dwarf2_fetch_die_location_block (cu_offset offset_in_cu,
8cf6f0b1
TT
15342 struct dwarf2_per_cu_data *per_cu,
15343 CORE_ADDR (*get_frame_pc) (void *baton),
15344 void *baton)
5c631832 15345{
b64f50a1 15346 sect_offset offset = { per_cu->offset.sect_off + offset_in_cu.cu_off };
918dd910 15347 struct dwarf2_cu *cu;
5c631832
JK
15348 struct die_info *die;
15349 struct attribute *attr;
15350 struct dwarf2_locexpr_baton retval;
15351
8cf6f0b1
TT
15352 dw2_setup (per_cu->objfile);
15353
918dd910
JK
15354 if (per_cu->cu == NULL)
15355 load_cu (per_cu);
15356 cu = per_cu->cu;
15357
5c631832
JK
15358 die = follow_die_offset (offset, &cu);
15359 if (!die)
15360 error (_("Dwarf Error: Cannot find DIE at 0x%x referenced in module %s"),
b64f50a1 15361 offset.sect_off, per_cu->objfile->name);
5c631832
JK
15362
15363 attr = dwarf2_attr (die, DW_AT_location, cu);
15364 if (!attr)
15365 {
e103e986
JK
15366 /* DWARF: "If there is no such attribute, then there is no effect.".
15367 DATA is ignored if SIZE is 0. */
5c631832 15368
e103e986 15369 retval.data = NULL;
5c631832
JK
15370 retval.size = 0;
15371 }
8cf6f0b1
TT
15372 else if (attr_form_is_section_offset (attr))
15373 {
15374 struct dwarf2_loclist_baton loclist_baton;
15375 CORE_ADDR pc = (*get_frame_pc) (baton);
15376 size_t size;
15377
15378 fill_in_loclist_baton (cu, &loclist_baton, attr);
15379
15380 retval.data = dwarf2_find_location_expression (&loclist_baton,
15381 &size, pc);
15382 retval.size = size;
15383 }
5c631832
JK
15384 else
15385 {
15386 if (!attr_form_is_block (attr))
15387 error (_("Dwarf Error: DIE at 0x%x referenced in module %s "
15388 "is neither DW_FORM_block* nor DW_FORM_exprloc"),
b64f50a1 15389 offset.sect_off, per_cu->objfile->name);
5c631832
JK
15390
15391 retval.data = DW_BLOCK (attr)->data;
15392 retval.size = DW_BLOCK (attr)->size;
15393 }
15394 retval.per_cu = cu->per_cu;
918dd910 15395
918dd910
JK
15396 age_cached_comp_units ();
15397
5c631832 15398 return retval;
348e048f
DE
15399}
15400
8a9b8146
TT
15401/* Return the type of the DIE at DIE_OFFSET in the CU named by
15402 PER_CU. */
15403
15404struct type *
b64f50a1 15405dwarf2_get_die_type (cu_offset die_offset,
8a9b8146
TT
15406 struct dwarf2_per_cu_data *per_cu)
15407{
b64f50a1
JK
15408 sect_offset die_offset_sect;
15409
8a9b8146 15410 dw2_setup (per_cu->objfile);
b64f50a1
JK
15411
15412 die_offset_sect.sect_off = per_cu->offset.sect_off + die_offset.cu_off;
15413 return get_die_type_at_offset (die_offset_sect, per_cu);
8a9b8146
TT
15414}
15415
348e048f
DE
15416/* Follow the signature attribute ATTR in SRC_DIE.
15417 On entry *REF_CU is the CU of SRC_DIE.
15418 On exit *REF_CU is the CU of the result. */
15419
15420static struct die_info *
15421follow_die_sig (struct die_info *src_die, struct attribute *attr,
15422 struct dwarf2_cu **ref_cu)
15423{
15424 struct objfile *objfile = (*ref_cu)->objfile;
15425 struct die_info temp_die;
15426 struct signatured_type *sig_type = DW_SIGNATURED_TYPE (attr);
15427 struct dwarf2_cu *sig_cu;
15428 struct die_info *die;
15429
15430 /* sig_type will be NULL if the signatured type is missing from
15431 the debug info. */
15432 if (sig_type == NULL)
15433 error (_("Dwarf Error: Cannot find signatured DIE referenced from DIE "
15434 "at 0x%x [in module %s]"),
b64f50a1 15435 src_die->offset.sect_off, objfile->name);
348e048f
DE
15436
15437 /* If necessary, add it to the queue and load its DIEs. */
15438
95554aad 15439 if (maybe_queue_comp_unit (*ref_cu, &sig_type->per_cu, language_minimal))
a0f42c21 15440 read_signatured_type (sig_type);
348e048f
DE
15441
15442 gdb_assert (sig_type->per_cu.cu != NULL);
15443
15444 sig_cu = sig_type->per_cu.cu;
3019eac3
DE
15445 gdb_assert (sig_type->type_offset_in_section.sect_off != 0);
15446 temp_die.offset = sig_type->type_offset_in_section;
b64f50a1
JK
15447 die = htab_find_with_hash (sig_cu->die_hash, &temp_die,
15448 temp_die.offset.sect_off);
348e048f
DE
15449 if (die)
15450 {
15451 *ref_cu = sig_cu;
15452 return die;
15453 }
15454
3e43a32a
MS
15455 error (_("Dwarf Error: Cannot find signatured DIE at 0x%x referenced "
15456 "from DIE at 0x%x [in module %s]"),
b64f50a1 15457 temp_die.offset.sect_off, src_die->offset.sect_off, objfile->name);
348e048f
DE
15458}
15459
15460/* Given an offset of a signatured type, return its signatured_type. */
15461
15462static struct signatured_type *
8b70b953
TT
15463lookup_signatured_type_at_offset (struct objfile *objfile,
15464 struct dwarf2_section_info *section,
b64f50a1 15465 sect_offset offset)
348e048f 15466{
b64f50a1 15467 gdb_byte *info_ptr = section->buffer + offset.sect_off;
348e048f
DE
15468 unsigned int length, initial_length_size;
15469 unsigned int sig_offset;
52dc124a 15470 struct signatured_type find_entry, *sig_type;
348e048f
DE
15471
15472 length = read_initial_length (objfile->obfd, info_ptr, &initial_length_size);
15473 sig_offset = (initial_length_size
15474 + 2 /*version*/
15475 + (initial_length_size == 4 ? 4 : 8) /*debug_abbrev_offset*/
15476 + 1 /*address_size*/);
15477 find_entry.signature = bfd_get_64 (objfile->obfd, info_ptr + sig_offset);
52dc124a 15478 sig_type = htab_find (dwarf2_per_objfile->signatured_types, &find_entry);
348e048f
DE
15479
15480 /* This is only used to lookup previously recorded types.
15481 If we didn't find it, it's our bug. */
52dc124a
DE
15482 gdb_assert (sig_type != NULL);
15483 gdb_assert (offset.sect_off == sig_type->per_cu.offset.sect_off);
348e048f 15484
52dc124a 15485 return sig_type;
348e048f
DE
15486}
15487
e5fe5e75 15488/* Load the DIEs associated with type unit PER_CU into memory. */
348e048f
DE
15489
15490static void
e5fe5e75 15491load_full_type_unit (struct dwarf2_per_cu_data *per_cu)
348e048f 15492{
e5fe5e75 15493 struct objfile *objfile = per_cu->objfile;
3019eac3 15494 struct dwarf2_section_info *sect = per_cu->info_or_types_section;
b64f50a1 15495 sect_offset offset = per_cu->offset;
52dc124a 15496 struct signatured_type *sig_type;
348e048f 15497
8b70b953 15498 dwarf2_read_section (objfile, sect);
be391dca 15499
348e048f 15500 /* We have the section offset, but we need the signature to do the
e5fe5e75
DE
15501 hash table lookup. */
15502 /* FIXME: This is sorta unnecessary, read_signatured_type only uses
15503 the signature to assert we found the right one.
15504 Ok, but it's a lot of work. We should simplify things so any needed
15505 assert doesn't require all this clumsiness. */
52dc124a 15506 sig_type = lookup_signatured_type_at_offset (objfile, sect, offset);
348e048f 15507
dee91e82 15508 gdb_assert (&sig_type->per_cu == per_cu);
52dc124a 15509 gdb_assert (sig_type->per_cu.cu == NULL);
348e048f 15510
52dc124a 15511 read_signatured_type (sig_type);
348e048f 15512
52dc124a 15513 gdb_assert (sig_type->per_cu.cu != NULL);
348e048f
DE
15514}
15515
dee91e82
DE
15516/* die_reader_func for read_signatured_type.
15517 This is identical to load_full_comp_unit_reader,
15518 but is kept separate for now. */
348e048f
DE
15519
15520static void
dee91e82
DE
15521read_signatured_type_reader (const struct die_reader_specs *reader,
15522 gdb_byte *info_ptr,
15523 struct die_info *comp_unit_die,
15524 int has_children,
15525 void *data)
348e048f 15526{
dee91e82 15527 struct dwarf2_cu *cu = reader->cu;
348e048f 15528
dee91e82
DE
15529 gdb_assert (cu->die_hash == NULL);
15530 cu->die_hash =
15531 htab_create_alloc_ex (cu->header.length / 12,
15532 die_hash,
15533 die_eq,
15534 NULL,
15535 &cu->comp_unit_obstack,
15536 hashtab_obstack_allocate,
15537 dummy_obstack_deallocate);
348e048f 15538
dee91e82
DE
15539 if (has_children)
15540 comp_unit_die->child = read_die_and_siblings (reader, info_ptr,
15541 &info_ptr, comp_unit_die);
15542 cu->dies = comp_unit_die;
15543 /* comp_unit_die is not stored in die_hash, no need. */
348e048f
DE
15544
15545 /* We try not to read any attributes in this function, because not
9cdd5dbd 15546 all CUs needed for references have been loaded yet, and symbol
348e048f 15547 table processing isn't initialized. But we have to set the CU language,
dee91e82
DE
15548 or we won't be able to build types correctly.
15549 Similarly, if we do not read the producer, we can not apply
15550 producer-specific interpretation. */
95554aad 15551 prepare_one_comp_unit (cu, cu->dies, language_minimal);
dee91e82 15552}
348e048f 15553
3019eac3
DE
15554/* Read in a signatured type and build its CU and DIEs.
15555 If the type is a stub for the real type in a DWO file,
15556 read in the real type from the DWO file as well. */
dee91e82
DE
15557
15558static void
15559read_signatured_type (struct signatured_type *sig_type)
15560{
15561 struct dwarf2_per_cu_data *per_cu = &sig_type->per_cu;
348e048f 15562
3019eac3 15563 gdb_assert (per_cu->is_debug_types);
dee91e82 15564 gdb_assert (per_cu->cu == NULL);
348e048f 15565
fd820528 15566 init_cutu_and_read_dies (per_cu, 0, 1, read_signatured_type_reader, NULL);
c906108c
SS
15567}
15568
c906108c
SS
15569/* Decode simple location descriptions.
15570 Given a pointer to a dwarf block that defines a location, compute
15571 the location and return the value.
15572
4cecd739
DJ
15573 NOTE drow/2003-11-18: This function is called in two situations
15574 now: for the address of static or global variables (partial symbols
15575 only) and for offsets into structures which are expected to be
15576 (more or less) constant. The partial symbol case should go away,
15577 and only the constant case should remain. That will let this
15578 function complain more accurately. A few special modes are allowed
15579 without complaint for global variables (for instance, global
15580 register values and thread-local values).
c906108c
SS
15581
15582 A location description containing no operations indicates that the
4cecd739 15583 object is optimized out. The return value is 0 for that case.
6b992462
DJ
15584 FIXME drow/2003-11-16: No callers check for this case any more; soon all
15585 callers will only want a very basic result and this can become a
21ae7a4d
JK
15586 complaint.
15587
15588 Note that stack[0] is unused except as a default error return. */
c906108c
SS
15589
15590static CORE_ADDR
e7c27a73 15591decode_locdesc (struct dwarf_block *blk, struct dwarf2_cu *cu)
c906108c 15592{
e7c27a73 15593 struct objfile *objfile = cu->objfile;
21ae7a4d
JK
15594 int i;
15595 int size = blk->size;
15596 gdb_byte *data = blk->data;
15597 CORE_ADDR stack[64];
15598 int stacki;
15599 unsigned int bytes_read, unsnd;
15600 gdb_byte op;
c906108c 15601
21ae7a4d
JK
15602 i = 0;
15603 stacki = 0;
15604 stack[stacki] = 0;
15605 stack[++stacki] = 0;
15606
15607 while (i < size)
15608 {
15609 op = data[i++];
15610 switch (op)
15611 {
15612 case DW_OP_lit0:
15613 case DW_OP_lit1:
15614 case DW_OP_lit2:
15615 case DW_OP_lit3:
15616 case DW_OP_lit4:
15617 case DW_OP_lit5:
15618 case DW_OP_lit6:
15619 case DW_OP_lit7:
15620 case DW_OP_lit8:
15621 case DW_OP_lit9:
15622 case DW_OP_lit10:
15623 case DW_OP_lit11:
15624 case DW_OP_lit12:
15625 case DW_OP_lit13:
15626 case DW_OP_lit14:
15627 case DW_OP_lit15:
15628 case DW_OP_lit16:
15629 case DW_OP_lit17:
15630 case DW_OP_lit18:
15631 case DW_OP_lit19:
15632 case DW_OP_lit20:
15633 case DW_OP_lit21:
15634 case DW_OP_lit22:
15635 case DW_OP_lit23:
15636 case DW_OP_lit24:
15637 case DW_OP_lit25:
15638 case DW_OP_lit26:
15639 case DW_OP_lit27:
15640 case DW_OP_lit28:
15641 case DW_OP_lit29:
15642 case DW_OP_lit30:
15643 case DW_OP_lit31:
15644 stack[++stacki] = op - DW_OP_lit0;
15645 break;
f1bea926 15646
21ae7a4d
JK
15647 case DW_OP_reg0:
15648 case DW_OP_reg1:
15649 case DW_OP_reg2:
15650 case DW_OP_reg3:
15651 case DW_OP_reg4:
15652 case DW_OP_reg5:
15653 case DW_OP_reg6:
15654 case DW_OP_reg7:
15655 case DW_OP_reg8:
15656 case DW_OP_reg9:
15657 case DW_OP_reg10:
15658 case DW_OP_reg11:
15659 case DW_OP_reg12:
15660 case DW_OP_reg13:
15661 case DW_OP_reg14:
15662 case DW_OP_reg15:
15663 case DW_OP_reg16:
15664 case DW_OP_reg17:
15665 case DW_OP_reg18:
15666 case DW_OP_reg19:
15667 case DW_OP_reg20:
15668 case DW_OP_reg21:
15669 case DW_OP_reg22:
15670 case DW_OP_reg23:
15671 case DW_OP_reg24:
15672 case DW_OP_reg25:
15673 case DW_OP_reg26:
15674 case DW_OP_reg27:
15675 case DW_OP_reg28:
15676 case DW_OP_reg29:
15677 case DW_OP_reg30:
15678 case DW_OP_reg31:
15679 stack[++stacki] = op - DW_OP_reg0;
15680 if (i < size)
15681 dwarf2_complex_location_expr_complaint ();
15682 break;
c906108c 15683
21ae7a4d
JK
15684 case DW_OP_regx:
15685 unsnd = read_unsigned_leb128 (NULL, (data + i), &bytes_read);
15686 i += bytes_read;
15687 stack[++stacki] = unsnd;
15688 if (i < size)
15689 dwarf2_complex_location_expr_complaint ();
15690 break;
c906108c 15691
21ae7a4d
JK
15692 case DW_OP_addr:
15693 stack[++stacki] = read_address (objfile->obfd, &data[i],
15694 cu, &bytes_read);
15695 i += bytes_read;
15696 break;
d53d4ac5 15697
21ae7a4d
JK
15698 case DW_OP_const1u:
15699 stack[++stacki] = read_1_byte (objfile->obfd, &data[i]);
15700 i += 1;
15701 break;
15702
15703 case DW_OP_const1s:
15704 stack[++stacki] = read_1_signed_byte (objfile->obfd, &data[i]);
15705 i += 1;
15706 break;
15707
15708 case DW_OP_const2u:
15709 stack[++stacki] = read_2_bytes (objfile->obfd, &data[i]);
15710 i += 2;
15711 break;
15712
15713 case DW_OP_const2s:
15714 stack[++stacki] = read_2_signed_bytes (objfile->obfd, &data[i]);
15715 i += 2;
15716 break;
d53d4ac5 15717
21ae7a4d
JK
15718 case DW_OP_const4u:
15719 stack[++stacki] = read_4_bytes (objfile->obfd, &data[i]);
15720 i += 4;
15721 break;
15722
15723 case DW_OP_const4s:
15724 stack[++stacki] = read_4_signed_bytes (objfile->obfd, &data[i]);
15725 i += 4;
15726 break;
15727
585861ea
JK
15728 case DW_OP_const8u:
15729 stack[++stacki] = read_8_bytes (objfile->obfd, &data[i]);
15730 i += 8;
15731 break;
15732
21ae7a4d
JK
15733 case DW_OP_constu:
15734 stack[++stacki] = read_unsigned_leb128 (NULL, (data + i),
15735 &bytes_read);
15736 i += bytes_read;
15737 break;
15738
15739 case DW_OP_consts:
15740 stack[++stacki] = read_signed_leb128 (NULL, (data + i), &bytes_read);
15741 i += bytes_read;
15742 break;
15743
15744 case DW_OP_dup:
15745 stack[stacki + 1] = stack[stacki];
15746 stacki++;
15747 break;
15748
15749 case DW_OP_plus:
15750 stack[stacki - 1] += stack[stacki];
15751 stacki--;
15752 break;
15753
15754 case DW_OP_plus_uconst:
15755 stack[stacki] += read_unsigned_leb128 (NULL, (data + i),
15756 &bytes_read);
15757 i += bytes_read;
15758 break;
15759
15760 case DW_OP_minus:
15761 stack[stacki - 1] -= stack[stacki];
15762 stacki--;
15763 break;
15764
15765 case DW_OP_deref:
15766 /* If we're not the last op, then we definitely can't encode
15767 this using GDB's address_class enum. This is valid for partial
15768 global symbols, although the variable's address will be bogus
15769 in the psymtab. */
15770 if (i < size)
15771 dwarf2_complex_location_expr_complaint ();
15772 break;
15773
15774 case DW_OP_GNU_push_tls_address:
15775 /* The top of the stack has the offset from the beginning
15776 of the thread control block at which the variable is located. */
15777 /* Nothing should follow this operator, so the top of stack would
15778 be returned. */
15779 /* This is valid for partial global symbols, but the variable's
585861ea
JK
15780 address will be bogus in the psymtab. Make it always at least
15781 non-zero to not look as a variable garbage collected by linker
15782 which have DW_OP_addr 0. */
21ae7a4d
JK
15783 if (i < size)
15784 dwarf2_complex_location_expr_complaint ();
585861ea 15785 stack[stacki]++;
21ae7a4d
JK
15786 break;
15787
15788 case DW_OP_GNU_uninit:
15789 break;
15790
3019eac3 15791 case DW_OP_GNU_addr_index:
49f6c839 15792 case DW_OP_GNU_const_index:
3019eac3
DE
15793 stack[++stacki] = read_addr_index_from_leb128 (cu, &data[i],
15794 &bytes_read);
15795 i += bytes_read;
15796 break;
15797
21ae7a4d
JK
15798 default:
15799 {
f39c6ffd 15800 const char *name = get_DW_OP_name (op);
21ae7a4d
JK
15801
15802 if (name)
15803 complaint (&symfile_complaints, _("unsupported stack op: '%s'"),
15804 name);
15805 else
15806 complaint (&symfile_complaints, _("unsupported stack op: '%02x'"),
15807 op);
15808 }
15809
15810 return (stack[stacki]);
d53d4ac5 15811 }
3c6e0cb3 15812
21ae7a4d
JK
15813 /* Enforce maximum stack depth of SIZE-1 to avoid writing
15814 outside of the allocated space. Also enforce minimum>0. */
15815 if (stacki >= ARRAY_SIZE (stack) - 1)
15816 {
15817 complaint (&symfile_complaints,
15818 _("location description stack overflow"));
15819 return 0;
15820 }
15821
15822 if (stacki <= 0)
15823 {
15824 complaint (&symfile_complaints,
15825 _("location description stack underflow"));
15826 return 0;
15827 }
15828 }
15829 return (stack[stacki]);
c906108c
SS
15830}
15831
15832/* memory allocation interface */
15833
c906108c 15834static struct dwarf_block *
7b5a2f43 15835dwarf_alloc_block (struct dwarf2_cu *cu)
c906108c
SS
15836{
15837 struct dwarf_block *blk;
15838
15839 blk = (struct dwarf_block *)
7b5a2f43 15840 obstack_alloc (&cu->comp_unit_obstack, sizeof (struct dwarf_block));
c906108c
SS
15841 return (blk);
15842}
15843
c906108c 15844static struct die_info *
b60c80d6 15845dwarf_alloc_die (struct dwarf2_cu *cu, int num_attrs)
c906108c
SS
15846{
15847 struct die_info *die;
b60c80d6
DJ
15848 size_t size = sizeof (struct die_info);
15849
15850 if (num_attrs > 1)
15851 size += (num_attrs - 1) * sizeof (struct attribute);
c906108c 15852
b60c80d6 15853 die = (struct die_info *) obstack_alloc (&cu->comp_unit_obstack, size);
c906108c
SS
15854 memset (die, 0, sizeof (struct die_info));
15855 return (die);
15856}
2e276125
JB
15857
15858\f
15859/* Macro support. */
15860
2e276125
JB
15861/* Return the full name of file number I in *LH's file name table.
15862 Use COMP_DIR as the name of the current directory of the
15863 compilation. The result is allocated using xmalloc; the caller is
15864 responsible for freeing it. */
15865static char *
15866file_full_name (int file, struct line_header *lh, const char *comp_dir)
15867{
6a83a1e6
EZ
15868 /* Is the file number a valid index into the line header's file name
15869 table? Remember that file numbers start with one, not zero. */
15870 if (1 <= file && file <= lh->num_file_names)
15871 {
15872 struct file_entry *fe = &lh->file_names[file - 1];
6e70227d 15873
6a83a1e6
EZ
15874 if (IS_ABSOLUTE_PATH (fe->name))
15875 return xstrdup (fe->name);
15876 else
15877 {
15878 const char *dir;
15879 int dir_len;
15880 char *full_name;
15881
15882 if (fe->dir_index)
15883 dir = lh->include_dirs[fe->dir_index - 1];
15884 else
15885 dir = comp_dir;
15886
15887 if (dir)
15888 {
15889 dir_len = strlen (dir);
15890 full_name = xmalloc (dir_len + 1 + strlen (fe->name) + 1);
15891 strcpy (full_name, dir);
15892 full_name[dir_len] = '/';
15893 strcpy (full_name + dir_len + 1, fe->name);
15894 return full_name;
15895 }
15896 else
15897 return xstrdup (fe->name);
15898 }
15899 }
2e276125
JB
15900 else
15901 {
6a83a1e6
EZ
15902 /* The compiler produced a bogus file number. We can at least
15903 record the macro definitions made in the file, even if we
15904 won't be able to find the file by name. */
15905 char fake_name[80];
9a619af0 15906
6a83a1e6 15907 sprintf (fake_name, "<bad macro file number %d>", file);
2e276125 15908
6e70227d 15909 complaint (&symfile_complaints,
6a83a1e6
EZ
15910 _("bad file number in macro information (%d)"),
15911 file);
2e276125 15912
6a83a1e6 15913 return xstrdup (fake_name);
2e276125
JB
15914 }
15915}
15916
15917
15918static struct macro_source_file *
15919macro_start_file (int file, int line,
15920 struct macro_source_file *current_file,
15921 const char *comp_dir,
15922 struct line_header *lh, struct objfile *objfile)
15923{
15924 /* The full name of this source file. */
15925 char *full_name = file_full_name (file, lh, comp_dir);
15926
15927 /* We don't create a macro table for this compilation unit
15928 at all until we actually get a filename. */
15929 if (! pending_macros)
4a146b47 15930 pending_macros = new_macro_table (&objfile->objfile_obstack,
af5f3db6 15931 objfile->macro_cache);
2e276125
JB
15932
15933 if (! current_file)
abc9d0dc
TT
15934 {
15935 /* If we have no current file, then this must be the start_file
15936 directive for the compilation unit's main source file. */
15937 current_file = macro_set_main (pending_macros, full_name);
15938 macro_define_special (pending_macros);
15939 }
2e276125
JB
15940 else
15941 current_file = macro_include (current_file, line, full_name);
15942
15943 xfree (full_name);
6e70227d 15944
2e276125
JB
15945 return current_file;
15946}
15947
15948
15949/* Copy the LEN characters at BUF to a xmalloc'ed block of memory,
15950 followed by a null byte. */
15951static char *
15952copy_string (const char *buf, int len)
15953{
15954 char *s = xmalloc (len + 1);
9a619af0 15955
2e276125
JB
15956 memcpy (s, buf, len);
15957 s[len] = '\0';
2e276125
JB
15958 return s;
15959}
15960
15961
15962static const char *
15963consume_improper_spaces (const char *p, const char *body)
15964{
15965 if (*p == ' ')
15966 {
4d3c2250 15967 complaint (&symfile_complaints,
3e43a32a
MS
15968 _("macro definition contains spaces "
15969 "in formal argument list:\n`%s'"),
4d3c2250 15970 body);
2e276125
JB
15971
15972 while (*p == ' ')
15973 p++;
15974 }
15975
15976 return p;
15977}
15978
15979
15980static void
15981parse_macro_definition (struct macro_source_file *file, int line,
15982 const char *body)
15983{
15984 const char *p;
15985
15986 /* The body string takes one of two forms. For object-like macro
15987 definitions, it should be:
15988
15989 <macro name> " " <definition>
15990
15991 For function-like macro definitions, it should be:
15992
15993 <macro name> "() " <definition>
15994 or
15995 <macro name> "(" <arg name> ( "," <arg name> ) * ") " <definition>
15996
15997 Spaces may appear only where explicitly indicated, and in the
15998 <definition>.
15999
16000 The Dwarf 2 spec says that an object-like macro's name is always
16001 followed by a space, but versions of GCC around March 2002 omit
6e70227d 16002 the space when the macro's definition is the empty string.
2e276125
JB
16003
16004 The Dwarf 2 spec says that there should be no spaces between the
16005 formal arguments in a function-like macro's formal argument list,
16006 but versions of GCC around March 2002 include spaces after the
16007 commas. */
16008
16009
16010 /* Find the extent of the macro name. The macro name is terminated
16011 by either a space or null character (for an object-like macro) or
16012 an opening paren (for a function-like macro). */
16013 for (p = body; *p; p++)
16014 if (*p == ' ' || *p == '(')
16015 break;
16016
16017 if (*p == ' ' || *p == '\0')
16018 {
16019 /* It's an object-like macro. */
16020 int name_len = p - body;
16021 char *name = copy_string (body, name_len);
16022 const char *replacement;
16023
16024 if (*p == ' ')
16025 replacement = body + name_len + 1;
16026 else
16027 {
4d3c2250 16028 dwarf2_macro_malformed_definition_complaint (body);
2e276125
JB
16029 replacement = body + name_len;
16030 }
6e70227d 16031
2e276125
JB
16032 macro_define_object (file, line, name, replacement);
16033
16034 xfree (name);
16035 }
16036 else if (*p == '(')
16037 {
16038 /* It's a function-like macro. */
16039 char *name = copy_string (body, p - body);
16040 int argc = 0;
16041 int argv_size = 1;
16042 char **argv = xmalloc (argv_size * sizeof (*argv));
16043
16044 p++;
16045
16046 p = consume_improper_spaces (p, body);
16047
16048 /* Parse the formal argument list. */
16049 while (*p && *p != ')')
16050 {
16051 /* Find the extent of the current argument name. */
16052 const char *arg_start = p;
16053
16054 while (*p && *p != ',' && *p != ')' && *p != ' ')
16055 p++;
16056
16057 if (! *p || p == arg_start)
4d3c2250 16058 dwarf2_macro_malformed_definition_complaint (body);
2e276125
JB
16059 else
16060 {
16061 /* Make sure argv has room for the new argument. */
16062 if (argc >= argv_size)
16063 {
16064 argv_size *= 2;
16065 argv = xrealloc (argv, argv_size * sizeof (*argv));
16066 }
16067
16068 argv[argc++] = copy_string (arg_start, p - arg_start);
16069 }
16070
16071 p = consume_improper_spaces (p, body);
16072
16073 /* Consume the comma, if present. */
16074 if (*p == ',')
16075 {
16076 p++;
16077
16078 p = consume_improper_spaces (p, body);
16079 }
16080 }
16081
16082 if (*p == ')')
16083 {
16084 p++;
16085
16086 if (*p == ' ')
16087 /* Perfectly formed definition, no complaints. */
16088 macro_define_function (file, line, name,
6e70227d 16089 argc, (const char **) argv,
2e276125
JB
16090 p + 1);
16091 else if (*p == '\0')
16092 {
16093 /* Complain, but do define it. */
4d3c2250 16094 dwarf2_macro_malformed_definition_complaint (body);
2e276125 16095 macro_define_function (file, line, name,
6e70227d 16096 argc, (const char **) argv,
2e276125
JB
16097 p);
16098 }
16099 else
16100 /* Just complain. */
4d3c2250 16101 dwarf2_macro_malformed_definition_complaint (body);
2e276125
JB
16102 }
16103 else
16104 /* Just complain. */
4d3c2250 16105 dwarf2_macro_malformed_definition_complaint (body);
2e276125
JB
16106
16107 xfree (name);
16108 {
16109 int i;
16110
16111 for (i = 0; i < argc; i++)
16112 xfree (argv[i]);
16113 }
16114 xfree (argv);
16115 }
16116 else
4d3c2250 16117 dwarf2_macro_malformed_definition_complaint (body);
2e276125
JB
16118}
16119
cf2c3c16
TT
16120/* Skip some bytes from BYTES according to the form given in FORM.
16121 Returns the new pointer. */
2e276125 16122
cf2c3c16 16123static gdb_byte *
f664829e 16124skip_form_bytes (bfd *abfd, gdb_byte *bytes, gdb_byte *buffer_end,
cf2c3c16
TT
16125 enum dwarf_form form,
16126 unsigned int offset_size,
16127 struct dwarf2_section_info *section)
2e276125 16128{
cf2c3c16 16129 unsigned int bytes_read;
2e276125 16130
cf2c3c16 16131 switch (form)
2e276125 16132 {
cf2c3c16
TT
16133 case DW_FORM_data1:
16134 case DW_FORM_flag:
16135 ++bytes;
16136 break;
16137
16138 case DW_FORM_data2:
16139 bytes += 2;
16140 break;
16141
16142 case DW_FORM_data4:
16143 bytes += 4;
16144 break;
16145
16146 case DW_FORM_data8:
16147 bytes += 8;
16148 break;
16149
16150 case DW_FORM_string:
16151 read_direct_string (abfd, bytes, &bytes_read);
16152 bytes += bytes_read;
16153 break;
16154
16155 case DW_FORM_sec_offset:
16156 case DW_FORM_strp:
16157 bytes += offset_size;
16158 break;
16159
16160 case DW_FORM_block:
16161 bytes += read_unsigned_leb128 (abfd, bytes, &bytes_read);
16162 bytes += bytes_read;
16163 break;
16164
16165 case DW_FORM_block1:
16166 bytes += 1 + read_1_byte (abfd, bytes);
16167 break;
16168 case DW_FORM_block2:
16169 bytes += 2 + read_2_bytes (abfd, bytes);
16170 break;
16171 case DW_FORM_block4:
16172 bytes += 4 + read_4_bytes (abfd, bytes);
16173 break;
16174
16175 case DW_FORM_sdata:
16176 case DW_FORM_udata:
3019eac3
DE
16177 case DW_FORM_GNU_addr_index:
16178 case DW_FORM_GNU_str_index:
f664829e
DE
16179 bytes = (gdb_byte *) gdb_skip_leb128 (bytes, buffer_end);
16180 if (bytes == NULL)
16181 {
16182 dwarf2_section_buffer_overflow_complaint (section);
16183 return NULL;
16184 }
cf2c3c16
TT
16185 break;
16186
16187 default:
16188 {
16189 complain:
16190 complaint (&symfile_complaints,
16191 _("invalid form 0x%x in `%s'"),
16192 form,
16193 section->asection->name);
16194 return NULL;
16195 }
2e276125
JB
16196 }
16197
cf2c3c16
TT
16198 return bytes;
16199}
757a13d0 16200
cf2c3c16
TT
16201/* A helper for dwarf_decode_macros that handles skipping an unknown
16202 opcode. Returns an updated pointer to the macro data buffer; or,
16203 on error, issues a complaint and returns NULL. */
757a13d0 16204
cf2c3c16
TT
16205static gdb_byte *
16206skip_unknown_opcode (unsigned int opcode,
16207 gdb_byte **opcode_definitions,
f664829e 16208 gdb_byte *mac_ptr, gdb_byte *mac_end,
cf2c3c16
TT
16209 bfd *abfd,
16210 unsigned int offset_size,
16211 struct dwarf2_section_info *section)
16212{
16213 unsigned int bytes_read, i;
16214 unsigned long arg;
16215 gdb_byte *defn;
2e276125 16216
cf2c3c16 16217 if (opcode_definitions[opcode] == NULL)
2e276125 16218 {
cf2c3c16
TT
16219 complaint (&symfile_complaints,
16220 _("unrecognized DW_MACFINO opcode 0x%x"),
16221 opcode);
16222 return NULL;
16223 }
2e276125 16224
cf2c3c16
TT
16225 defn = opcode_definitions[opcode];
16226 arg = read_unsigned_leb128 (abfd, defn, &bytes_read);
16227 defn += bytes_read;
2e276125 16228
cf2c3c16
TT
16229 for (i = 0; i < arg; ++i)
16230 {
f664829e
DE
16231 mac_ptr = skip_form_bytes (abfd, mac_ptr, mac_end, defn[i], offset_size,
16232 section);
cf2c3c16
TT
16233 if (mac_ptr == NULL)
16234 {
16235 /* skip_form_bytes already issued the complaint. */
16236 return NULL;
16237 }
16238 }
757a13d0 16239
cf2c3c16
TT
16240 return mac_ptr;
16241}
757a13d0 16242
cf2c3c16
TT
16243/* A helper function which parses the header of a macro section.
16244 If the macro section is the extended (for now called "GNU") type,
16245 then this updates *OFFSET_SIZE. Returns a pointer to just after
16246 the header, or issues a complaint and returns NULL on error. */
757a13d0 16247
cf2c3c16
TT
16248static gdb_byte *
16249dwarf_parse_macro_header (gdb_byte **opcode_definitions,
16250 bfd *abfd,
16251 gdb_byte *mac_ptr,
16252 unsigned int *offset_size,
16253 int section_is_gnu)
16254{
16255 memset (opcode_definitions, 0, 256 * sizeof (gdb_byte *));
757a13d0 16256
cf2c3c16
TT
16257 if (section_is_gnu)
16258 {
16259 unsigned int version, flags;
757a13d0 16260
cf2c3c16
TT
16261 version = read_2_bytes (abfd, mac_ptr);
16262 if (version != 4)
16263 {
16264 complaint (&symfile_complaints,
16265 _("unrecognized version `%d' in .debug_macro section"),
16266 version);
16267 return NULL;
16268 }
16269 mac_ptr += 2;
757a13d0 16270
cf2c3c16
TT
16271 flags = read_1_byte (abfd, mac_ptr);
16272 ++mac_ptr;
16273 *offset_size = (flags & 1) ? 8 : 4;
757a13d0 16274
cf2c3c16
TT
16275 if ((flags & 2) != 0)
16276 /* We don't need the line table offset. */
16277 mac_ptr += *offset_size;
757a13d0 16278
cf2c3c16
TT
16279 /* Vendor opcode descriptions. */
16280 if ((flags & 4) != 0)
16281 {
16282 unsigned int i, count;
757a13d0 16283
cf2c3c16
TT
16284 count = read_1_byte (abfd, mac_ptr);
16285 ++mac_ptr;
16286 for (i = 0; i < count; ++i)
16287 {
16288 unsigned int opcode, bytes_read;
16289 unsigned long arg;
16290
16291 opcode = read_1_byte (abfd, mac_ptr);
16292 ++mac_ptr;
16293 opcode_definitions[opcode] = mac_ptr;
16294 arg = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
16295 mac_ptr += bytes_read;
16296 mac_ptr += arg;
16297 }
757a13d0 16298 }
cf2c3c16 16299 }
757a13d0 16300
cf2c3c16
TT
16301 return mac_ptr;
16302}
757a13d0 16303
cf2c3c16 16304/* A helper for dwarf_decode_macros that handles the GNU extensions,
8fc3fc34 16305 including DW_MACRO_GNU_transparent_include. */
cf2c3c16
TT
16306
16307static void
16308dwarf_decode_macro_bytes (bfd *abfd, gdb_byte *mac_ptr, gdb_byte *mac_end,
16309 struct macro_source_file *current_file,
16310 struct line_header *lh, char *comp_dir,
16311 struct dwarf2_section_info *section,
16312 int section_is_gnu,
16313 unsigned int offset_size,
8fc3fc34
TT
16314 struct objfile *objfile,
16315 htab_t include_hash)
cf2c3c16
TT
16316{
16317 enum dwarf_macro_record_type macinfo_type;
16318 int at_commandline;
16319 gdb_byte *opcode_definitions[256];
757a13d0 16320
cf2c3c16
TT
16321 mac_ptr = dwarf_parse_macro_header (opcode_definitions, abfd, mac_ptr,
16322 &offset_size, section_is_gnu);
16323 if (mac_ptr == NULL)
16324 {
16325 /* We already issued a complaint. */
16326 return;
16327 }
757a13d0
JK
16328
16329 /* Determines if GDB is still before first DW_MACINFO_start_file. If true
16330 GDB is still reading the definitions from command line. First
16331 DW_MACINFO_start_file will need to be ignored as it was already executed
16332 to create CURRENT_FILE for the main source holding also the command line
16333 definitions. On first met DW_MACINFO_start_file this flag is reset to
16334 normally execute all the remaining DW_MACINFO_start_file macinfos. */
16335
16336 at_commandline = 1;
16337
16338 do
16339 {
16340 /* Do we at least have room for a macinfo type byte? */
16341 if (mac_ptr >= mac_end)
16342 {
f664829e 16343 dwarf2_section_buffer_overflow_complaint (section);
757a13d0
JK
16344 break;
16345 }
16346
16347 macinfo_type = read_1_byte (abfd, mac_ptr);
16348 mac_ptr++;
16349
cf2c3c16
TT
16350 /* Note that we rely on the fact that the corresponding GNU and
16351 DWARF constants are the same. */
757a13d0
JK
16352 switch (macinfo_type)
16353 {
16354 /* A zero macinfo type indicates the end of the macro
16355 information. */
16356 case 0:
16357 break;
2e276125 16358
cf2c3c16
TT
16359 case DW_MACRO_GNU_define:
16360 case DW_MACRO_GNU_undef:
16361 case DW_MACRO_GNU_define_indirect:
16362 case DW_MACRO_GNU_undef_indirect:
2e276125 16363 {
891d2f0b 16364 unsigned int bytes_read;
2e276125
JB
16365 int line;
16366 char *body;
cf2c3c16 16367 int is_define;
2e276125 16368
cf2c3c16
TT
16369 line = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
16370 mac_ptr += bytes_read;
16371
16372 if (macinfo_type == DW_MACRO_GNU_define
16373 || macinfo_type == DW_MACRO_GNU_undef)
16374 {
16375 body = read_direct_string (abfd, mac_ptr, &bytes_read);
16376 mac_ptr += bytes_read;
16377 }
16378 else
16379 {
16380 LONGEST str_offset;
16381
16382 str_offset = read_offset_1 (abfd, mac_ptr, offset_size);
16383 mac_ptr += offset_size;
2e276125 16384
cf2c3c16
TT
16385 body = read_indirect_string_at_offset (abfd, str_offset);
16386 }
16387
16388 is_define = (macinfo_type == DW_MACRO_GNU_define
16389 || macinfo_type == DW_MACRO_GNU_define_indirect);
2e276125 16390 if (! current_file)
757a13d0
JK
16391 {
16392 /* DWARF violation as no main source is present. */
16393 complaint (&symfile_complaints,
16394 _("debug info with no main source gives macro %s "
16395 "on line %d: %s"),
cf2c3c16
TT
16396 is_define ? _("definition") : _("undefinition"),
16397 line, body);
757a13d0
JK
16398 break;
16399 }
3e43a32a
MS
16400 if ((line == 0 && !at_commandline)
16401 || (line != 0 && at_commandline))
4d3c2250 16402 complaint (&symfile_complaints,
757a13d0
JK
16403 _("debug info gives %s macro %s with %s line %d: %s"),
16404 at_commandline ? _("command-line") : _("in-file"),
cf2c3c16 16405 is_define ? _("definition") : _("undefinition"),
757a13d0
JK
16406 line == 0 ? _("zero") : _("non-zero"), line, body);
16407
cf2c3c16 16408 if (is_define)
757a13d0 16409 parse_macro_definition (current_file, line, body);
cf2c3c16
TT
16410 else
16411 {
16412 gdb_assert (macinfo_type == DW_MACRO_GNU_undef
16413 || macinfo_type == DW_MACRO_GNU_undef_indirect);
16414 macro_undef (current_file, line, body);
16415 }
2e276125
JB
16416 }
16417 break;
16418
cf2c3c16 16419 case DW_MACRO_GNU_start_file:
2e276125 16420 {
891d2f0b 16421 unsigned int bytes_read;
2e276125
JB
16422 int line, file;
16423
16424 line = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
16425 mac_ptr += bytes_read;
16426 file = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
16427 mac_ptr += bytes_read;
16428
3e43a32a
MS
16429 if ((line == 0 && !at_commandline)
16430 || (line != 0 && at_commandline))
757a13d0
JK
16431 complaint (&symfile_complaints,
16432 _("debug info gives source %d included "
16433 "from %s at %s line %d"),
16434 file, at_commandline ? _("command-line") : _("file"),
16435 line == 0 ? _("zero") : _("non-zero"), line);
16436
16437 if (at_commandline)
16438 {
cf2c3c16
TT
16439 /* This DW_MACRO_GNU_start_file was executed in the
16440 pass one. */
757a13d0
JK
16441 at_commandline = 0;
16442 }
16443 else
16444 current_file = macro_start_file (file, line,
16445 current_file, comp_dir,
cf2c3c16 16446 lh, objfile);
2e276125
JB
16447 }
16448 break;
16449
cf2c3c16 16450 case DW_MACRO_GNU_end_file:
2e276125 16451 if (! current_file)
4d3c2250 16452 complaint (&symfile_complaints,
3e43a32a
MS
16453 _("macro debug info has an unmatched "
16454 "`close_file' directive"));
2e276125
JB
16455 else
16456 {
16457 current_file = current_file->included_by;
16458 if (! current_file)
16459 {
cf2c3c16 16460 enum dwarf_macro_record_type next_type;
2e276125
JB
16461
16462 /* GCC circa March 2002 doesn't produce the zero
16463 type byte marking the end of the compilation
16464 unit. Complain if it's not there, but exit no
16465 matter what. */
16466
16467 /* Do we at least have room for a macinfo type byte? */
16468 if (mac_ptr >= mac_end)
16469 {
f664829e 16470 dwarf2_section_buffer_overflow_complaint (section);
2e276125
JB
16471 return;
16472 }
16473
16474 /* We don't increment mac_ptr here, so this is just
16475 a look-ahead. */
16476 next_type = read_1_byte (abfd, mac_ptr);
16477 if (next_type != 0)
4d3c2250 16478 complaint (&symfile_complaints,
3e43a32a
MS
16479 _("no terminating 0-type entry for "
16480 "macros in `.debug_macinfo' section"));
2e276125
JB
16481
16482 return;
16483 }
16484 }
16485 break;
16486
cf2c3c16
TT
16487 case DW_MACRO_GNU_transparent_include:
16488 {
16489 LONGEST offset;
8fc3fc34 16490 void **slot;
cf2c3c16
TT
16491
16492 offset = read_offset_1 (abfd, mac_ptr, offset_size);
16493 mac_ptr += offset_size;
16494
8fc3fc34
TT
16495 slot = htab_find_slot (include_hash, mac_ptr, INSERT);
16496 if (*slot != NULL)
16497 {
16498 /* This has actually happened; see
16499 http://sourceware.org/bugzilla/show_bug.cgi?id=13568. */
16500 complaint (&symfile_complaints,
16501 _("recursive DW_MACRO_GNU_transparent_include in "
16502 ".debug_macro section"));
16503 }
16504 else
16505 {
16506 *slot = mac_ptr;
16507
16508 dwarf_decode_macro_bytes (abfd,
16509 section->buffer + offset,
16510 mac_end, current_file,
16511 lh, comp_dir,
16512 section, section_is_gnu,
16513 offset_size, objfile, include_hash);
16514
16515 htab_remove_elt (include_hash, mac_ptr);
16516 }
cf2c3c16
TT
16517 }
16518 break;
16519
2e276125 16520 case DW_MACINFO_vendor_ext:
cf2c3c16
TT
16521 if (!section_is_gnu)
16522 {
16523 unsigned int bytes_read;
16524 int constant;
2e276125 16525
cf2c3c16
TT
16526 constant = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
16527 mac_ptr += bytes_read;
16528 read_direct_string (abfd, mac_ptr, &bytes_read);
16529 mac_ptr += bytes_read;
2e276125 16530
cf2c3c16
TT
16531 /* We don't recognize any vendor extensions. */
16532 break;
16533 }
16534 /* FALLTHROUGH */
16535
16536 default:
16537 mac_ptr = skip_unknown_opcode (macinfo_type, opcode_definitions,
f664829e 16538 mac_ptr, mac_end, abfd, offset_size,
cf2c3c16
TT
16539 section);
16540 if (mac_ptr == NULL)
16541 return;
16542 break;
2e276125 16543 }
757a13d0 16544 } while (macinfo_type != 0);
2e276125 16545}
8e19ed76 16546
cf2c3c16 16547static void
09262596
DE
16548dwarf_decode_macros (struct dwarf2_cu *cu, unsigned int offset,
16549 char *comp_dir, int section_is_gnu)
cf2c3c16 16550{
bb5ed363 16551 struct objfile *objfile = dwarf2_per_objfile->objfile;
09262596
DE
16552 struct line_header *lh = cu->line_header;
16553 bfd *abfd;
cf2c3c16
TT
16554 gdb_byte *mac_ptr, *mac_end;
16555 struct macro_source_file *current_file = 0;
16556 enum dwarf_macro_record_type macinfo_type;
16557 unsigned int offset_size = cu->header.offset_size;
16558 gdb_byte *opcode_definitions[256];
8fc3fc34
TT
16559 struct cleanup *cleanup;
16560 htab_t include_hash;
16561 void **slot;
09262596
DE
16562 struct dwarf2_section_info *section;
16563 const char *section_name;
16564
16565 if (cu->dwo_unit != NULL)
16566 {
16567 if (section_is_gnu)
16568 {
16569 section = &cu->dwo_unit->dwo_file->sections.macro;
16570 section_name = ".debug_macro.dwo";
16571 }
16572 else
16573 {
16574 section = &cu->dwo_unit->dwo_file->sections.macinfo;
16575 section_name = ".debug_macinfo.dwo";
16576 }
16577 }
16578 else
16579 {
16580 if (section_is_gnu)
16581 {
16582 section = &dwarf2_per_objfile->macro;
16583 section_name = ".debug_macro";
16584 }
16585 else
16586 {
16587 section = &dwarf2_per_objfile->macinfo;
16588 section_name = ".debug_macinfo";
16589 }
16590 }
cf2c3c16 16591
bb5ed363 16592 dwarf2_read_section (objfile, section);
cf2c3c16
TT
16593 if (section->buffer == NULL)
16594 {
fceca515 16595 complaint (&symfile_complaints, _("missing %s section"), section_name);
cf2c3c16
TT
16596 return;
16597 }
09262596 16598 abfd = section->asection->owner;
cf2c3c16
TT
16599
16600 /* First pass: Find the name of the base filename.
16601 This filename is needed in order to process all macros whose definition
16602 (or undefinition) comes from the command line. These macros are defined
16603 before the first DW_MACINFO_start_file entry, and yet still need to be
16604 associated to the base file.
16605
16606 To determine the base file name, we scan the macro definitions until we
16607 reach the first DW_MACINFO_start_file entry. We then initialize
16608 CURRENT_FILE accordingly so that any macro definition found before the
16609 first DW_MACINFO_start_file can still be associated to the base file. */
16610
16611 mac_ptr = section->buffer + offset;
16612 mac_end = section->buffer + section->size;
16613
16614 mac_ptr = dwarf_parse_macro_header (opcode_definitions, abfd, mac_ptr,
16615 &offset_size, section_is_gnu);
16616 if (mac_ptr == NULL)
16617 {
16618 /* We already issued a complaint. */
16619 return;
16620 }
16621
16622 do
16623 {
16624 /* Do we at least have room for a macinfo type byte? */
16625 if (mac_ptr >= mac_end)
16626 {
16627 /* Complaint is printed during the second pass as GDB will probably
16628 stop the first pass earlier upon finding
16629 DW_MACINFO_start_file. */
16630 break;
16631 }
16632
16633 macinfo_type = read_1_byte (abfd, mac_ptr);
16634 mac_ptr++;
16635
16636 /* Note that we rely on the fact that the corresponding GNU and
16637 DWARF constants are the same. */
16638 switch (macinfo_type)
16639 {
16640 /* A zero macinfo type indicates the end of the macro
16641 information. */
16642 case 0:
16643 break;
16644
16645 case DW_MACRO_GNU_define:
16646 case DW_MACRO_GNU_undef:
16647 /* Only skip the data by MAC_PTR. */
16648 {
16649 unsigned int bytes_read;
16650
16651 read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
16652 mac_ptr += bytes_read;
16653 read_direct_string (abfd, mac_ptr, &bytes_read);
16654 mac_ptr += bytes_read;
16655 }
16656 break;
16657
16658 case DW_MACRO_GNU_start_file:
16659 {
16660 unsigned int bytes_read;
16661 int line, file;
16662
16663 line = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
16664 mac_ptr += bytes_read;
16665 file = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
16666 mac_ptr += bytes_read;
16667
16668 current_file = macro_start_file (file, line, current_file,
bb5ed363 16669 comp_dir, lh, objfile);
cf2c3c16
TT
16670 }
16671 break;
16672
16673 case DW_MACRO_GNU_end_file:
16674 /* No data to skip by MAC_PTR. */
16675 break;
16676
16677 case DW_MACRO_GNU_define_indirect:
16678 case DW_MACRO_GNU_undef_indirect:
16679 {
16680 unsigned int bytes_read;
16681
16682 read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
16683 mac_ptr += bytes_read;
16684 mac_ptr += offset_size;
16685 }
16686 break;
16687
16688 case DW_MACRO_GNU_transparent_include:
16689 /* Note that, according to the spec, a transparent include
16690 chain cannot call DW_MACRO_GNU_start_file. So, we can just
16691 skip this opcode. */
16692 mac_ptr += offset_size;
16693 break;
16694
16695 case DW_MACINFO_vendor_ext:
16696 /* Only skip the data by MAC_PTR. */
16697 if (!section_is_gnu)
16698 {
16699 unsigned int bytes_read;
16700
16701 read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
16702 mac_ptr += bytes_read;
16703 read_direct_string (abfd, mac_ptr, &bytes_read);
16704 mac_ptr += bytes_read;
16705 }
16706 /* FALLTHROUGH */
16707
16708 default:
16709 mac_ptr = skip_unknown_opcode (macinfo_type, opcode_definitions,
f664829e 16710 mac_ptr, mac_end, abfd, offset_size,
cf2c3c16
TT
16711 section);
16712 if (mac_ptr == NULL)
16713 return;
16714 break;
16715 }
16716 } while (macinfo_type != 0 && current_file == NULL);
16717
16718 /* Second pass: Process all entries.
16719
16720 Use the AT_COMMAND_LINE flag to determine whether we are still processing
16721 command-line macro definitions/undefinitions. This flag is unset when we
16722 reach the first DW_MACINFO_start_file entry. */
16723
8fc3fc34
TT
16724 include_hash = htab_create_alloc (1, htab_hash_pointer, htab_eq_pointer,
16725 NULL, xcalloc, xfree);
16726 cleanup = make_cleanup_htab_delete (include_hash);
16727 mac_ptr = section->buffer + offset;
16728 slot = htab_find_slot (include_hash, mac_ptr, INSERT);
16729 *slot = mac_ptr;
16730 dwarf_decode_macro_bytes (abfd, mac_ptr, mac_end,
cf2c3c16 16731 current_file, lh, comp_dir, section, section_is_gnu,
8fc3fc34
TT
16732 offset_size, objfile, include_hash);
16733 do_cleanups (cleanup);
cf2c3c16
TT
16734}
16735
8e19ed76 16736/* Check if the attribute's form is a DW_FORM_block*
0963b4bd 16737 if so return true else false. */
380bca97 16738
8e19ed76
PS
16739static int
16740attr_form_is_block (struct attribute *attr)
16741{
16742 return (attr == NULL ? 0 :
16743 attr->form == DW_FORM_block1
16744 || attr->form == DW_FORM_block2
16745 || attr->form == DW_FORM_block4
2dc7f7b3
TT
16746 || attr->form == DW_FORM_block
16747 || attr->form == DW_FORM_exprloc);
8e19ed76 16748}
4c2df51b 16749
c6a0999f
JB
16750/* Return non-zero if ATTR's value is a section offset --- classes
16751 lineptr, loclistptr, macptr or rangelistptr --- or zero, otherwise.
16752 You may use DW_UNSND (attr) to retrieve such offsets.
16753
16754 Section 7.5.4, "Attribute Encodings", explains that no attribute
16755 may have a value that belongs to more than one of these classes; it
16756 would be ambiguous if we did, because we use the same forms for all
16757 of them. */
380bca97 16758
3690dd37
JB
16759static int
16760attr_form_is_section_offset (struct attribute *attr)
16761{
16762 return (attr->form == DW_FORM_data4
2dc7f7b3
TT
16763 || attr->form == DW_FORM_data8
16764 || attr->form == DW_FORM_sec_offset);
3690dd37
JB
16765}
16766
3690dd37
JB
16767/* Return non-zero if ATTR's value falls in the 'constant' class, or
16768 zero otherwise. When this function returns true, you can apply
16769 dwarf2_get_attr_constant_value to it.
16770
16771 However, note that for some attributes you must check
16772 attr_form_is_section_offset before using this test. DW_FORM_data4
16773 and DW_FORM_data8 are members of both the constant class, and of
16774 the classes that contain offsets into other debug sections
16775 (lineptr, loclistptr, macptr or rangelistptr). The DWARF spec says
16776 that, if an attribute's can be either a constant or one of the
16777 section offset classes, DW_FORM_data4 and DW_FORM_data8 should be
16778 taken as section offsets, not constants. */
380bca97 16779
3690dd37
JB
16780static int
16781attr_form_is_constant (struct attribute *attr)
16782{
16783 switch (attr->form)
16784 {
16785 case DW_FORM_sdata:
16786 case DW_FORM_udata:
16787 case DW_FORM_data1:
16788 case DW_FORM_data2:
16789 case DW_FORM_data4:
16790 case DW_FORM_data8:
16791 return 1;
16792 default:
16793 return 0;
16794 }
16795}
16796
3019eac3
DE
16797/* Return the .debug_loc section to use for CU.
16798 For DWO files use .debug_loc.dwo. */
16799
16800static struct dwarf2_section_info *
16801cu_debug_loc_section (struct dwarf2_cu *cu)
16802{
16803 if (cu->dwo_unit)
16804 return &cu->dwo_unit->dwo_file->sections.loc;
16805 return &dwarf2_per_objfile->loc;
16806}
16807
8cf6f0b1
TT
16808/* A helper function that fills in a dwarf2_loclist_baton. */
16809
16810static void
16811fill_in_loclist_baton (struct dwarf2_cu *cu,
16812 struct dwarf2_loclist_baton *baton,
16813 struct attribute *attr)
16814{
3019eac3
DE
16815 struct dwarf2_section_info *section = cu_debug_loc_section (cu);
16816
16817 dwarf2_read_section (dwarf2_per_objfile->objfile, section);
8cf6f0b1
TT
16818
16819 baton->per_cu = cu->per_cu;
16820 gdb_assert (baton->per_cu);
16821 /* We don't know how long the location list is, but make sure we
16822 don't run off the edge of the section. */
3019eac3
DE
16823 baton->size = section->size - DW_UNSND (attr);
16824 baton->data = section->buffer + DW_UNSND (attr);
8cf6f0b1 16825 baton->base_address = cu->base_address;
f664829e 16826 baton->from_dwo = cu->dwo_unit != NULL;
8cf6f0b1
TT
16827}
16828
4c2df51b
DJ
16829static void
16830dwarf2_symbol_mark_computed (struct attribute *attr, struct symbol *sym,
e7c27a73 16831 struct dwarf2_cu *cu)
4c2df51b 16832{
bb5ed363 16833 struct objfile *objfile = dwarf2_per_objfile->objfile;
3019eac3 16834 struct dwarf2_section_info *section = cu_debug_loc_section (cu);
bb5ed363 16835
3690dd37 16836 if (attr_form_is_section_offset (attr)
3019eac3 16837 /* .debug_loc{,.dwo} may not exist at all, or the offset may be outside
99bcc461
DJ
16838 the section. If so, fall through to the complaint in the
16839 other branch. */
3019eac3 16840 && DW_UNSND (attr) < dwarf2_section_size (objfile, section))
4c2df51b 16841 {
0d53c4c4 16842 struct dwarf2_loclist_baton *baton;
4c2df51b 16843
bb5ed363 16844 baton = obstack_alloc (&objfile->objfile_obstack,
0d53c4c4 16845 sizeof (struct dwarf2_loclist_baton));
4c2df51b 16846
8cf6f0b1 16847 fill_in_loclist_baton (cu, baton, attr);
be391dca 16848
d00adf39 16849 if (cu->base_known == 0)
0d53c4c4 16850 complaint (&symfile_complaints,
3e43a32a
MS
16851 _("Location list used without "
16852 "specifying the CU base address."));
4c2df51b 16853
768a979c 16854 SYMBOL_COMPUTED_OPS (sym) = &dwarf2_loclist_funcs;
0d53c4c4
DJ
16855 SYMBOL_LOCATION_BATON (sym) = baton;
16856 }
16857 else
16858 {
16859 struct dwarf2_locexpr_baton *baton;
16860
bb5ed363 16861 baton = obstack_alloc (&objfile->objfile_obstack,
0d53c4c4 16862 sizeof (struct dwarf2_locexpr_baton));
ae0d2f24
UW
16863 baton->per_cu = cu->per_cu;
16864 gdb_assert (baton->per_cu);
0d53c4c4
DJ
16865
16866 if (attr_form_is_block (attr))
16867 {
16868 /* Note that we're just copying the block's data pointer
16869 here, not the actual data. We're still pointing into the
6502dd73
DJ
16870 info_buffer for SYM's objfile; right now we never release
16871 that buffer, but when we do clean up properly this may
16872 need to change. */
0d53c4c4
DJ
16873 baton->size = DW_BLOCK (attr)->size;
16874 baton->data = DW_BLOCK (attr)->data;
16875 }
16876 else
16877 {
16878 dwarf2_invalid_attrib_class_complaint ("location description",
16879 SYMBOL_NATURAL_NAME (sym));
16880 baton->size = 0;
0d53c4c4 16881 }
6e70227d 16882
768a979c 16883 SYMBOL_COMPUTED_OPS (sym) = &dwarf2_locexpr_funcs;
0d53c4c4
DJ
16884 SYMBOL_LOCATION_BATON (sym) = baton;
16885 }
4c2df51b 16886}
6502dd73 16887
9aa1f1e3
TT
16888/* Return the OBJFILE associated with the compilation unit CU. If CU
16889 came from a separate debuginfo file, then the master objfile is
16890 returned. */
ae0d2f24
UW
16891
16892struct objfile *
16893dwarf2_per_cu_objfile (struct dwarf2_per_cu_data *per_cu)
16894{
9291a0cd 16895 struct objfile *objfile = per_cu->objfile;
ae0d2f24
UW
16896
16897 /* Return the master objfile, so that we can report and look up the
16898 correct file containing this variable. */
16899 if (objfile->separate_debug_objfile_backlink)
16900 objfile = objfile->separate_debug_objfile_backlink;
16901
16902 return objfile;
16903}
16904
96408a79
SA
16905/* Return comp_unit_head for PER_CU, either already available in PER_CU->CU
16906 (CU_HEADERP is unused in such case) or prepare a temporary copy at
16907 CU_HEADERP first. */
16908
16909static const struct comp_unit_head *
16910per_cu_header_read_in (struct comp_unit_head *cu_headerp,
16911 struct dwarf2_per_cu_data *per_cu)
16912{
96408a79
SA
16913 gdb_byte *info_ptr;
16914
16915 if (per_cu->cu)
16916 return &per_cu->cu->header;
16917
0bc3a05c 16918 info_ptr = per_cu->info_or_types_section->buffer + per_cu->offset.sect_off;
96408a79
SA
16919
16920 memset (cu_headerp, 0, sizeof (*cu_headerp));
0bc3a05c 16921 read_comp_unit_head (cu_headerp, info_ptr, per_cu->objfile->obfd);
96408a79
SA
16922
16923 return cu_headerp;
16924}
16925
ae0d2f24
UW
16926/* Return the address size given in the compilation unit header for CU. */
16927
98714339 16928int
ae0d2f24
UW
16929dwarf2_per_cu_addr_size (struct dwarf2_per_cu_data *per_cu)
16930{
96408a79
SA
16931 struct comp_unit_head cu_header_local;
16932 const struct comp_unit_head *cu_headerp;
c471e790 16933
96408a79
SA
16934 cu_headerp = per_cu_header_read_in (&cu_header_local, per_cu);
16935
16936 return cu_headerp->addr_size;
ae0d2f24
UW
16937}
16938
9eae7c52
TT
16939/* Return the offset size given in the compilation unit header for CU. */
16940
16941int
16942dwarf2_per_cu_offset_size (struct dwarf2_per_cu_data *per_cu)
16943{
96408a79
SA
16944 struct comp_unit_head cu_header_local;
16945 const struct comp_unit_head *cu_headerp;
9c6c53f7 16946
96408a79
SA
16947 cu_headerp = per_cu_header_read_in (&cu_header_local, per_cu);
16948
16949 return cu_headerp->offset_size;
16950}
16951
16952/* See its dwarf2loc.h declaration. */
16953
16954int
16955dwarf2_per_cu_ref_addr_size (struct dwarf2_per_cu_data *per_cu)
16956{
16957 struct comp_unit_head cu_header_local;
16958 const struct comp_unit_head *cu_headerp;
16959
16960 cu_headerp = per_cu_header_read_in (&cu_header_local, per_cu);
16961
16962 if (cu_headerp->version == 2)
16963 return cu_headerp->addr_size;
16964 else
16965 return cu_headerp->offset_size;
181cebd4
JK
16966}
16967
9aa1f1e3
TT
16968/* Return the text offset of the CU. The returned offset comes from
16969 this CU's objfile. If this objfile came from a separate debuginfo
16970 file, then the offset may be different from the corresponding
16971 offset in the parent objfile. */
16972
16973CORE_ADDR
16974dwarf2_per_cu_text_offset (struct dwarf2_per_cu_data *per_cu)
16975{
bb3fa9d0 16976 struct objfile *objfile = per_cu->objfile;
9aa1f1e3
TT
16977
16978 return ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
16979}
16980
348e048f
DE
16981/* Locate the .debug_info compilation unit from CU's objfile which contains
16982 the DIE at OFFSET. Raises an error on failure. */
ae038cb0
DJ
16983
16984static struct dwarf2_per_cu_data *
b64f50a1 16985dwarf2_find_containing_comp_unit (sect_offset offset,
ae038cb0
DJ
16986 struct objfile *objfile)
16987{
16988 struct dwarf2_per_cu_data *this_cu;
16989 int low, high;
16990
ae038cb0
DJ
16991 low = 0;
16992 high = dwarf2_per_objfile->n_comp_units - 1;
16993 while (high > low)
16994 {
16995 int mid = low + (high - low) / 2;
9a619af0 16996
b64f50a1
JK
16997 if (dwarf2_per_objfile->all_comp_units[mid]->offset.sect_off
16998 >= offset.sect_off)
ae038cb0
DJ
16999 high = mid;
17000 else
17001 low = mid + 1;
17002 }
17003 gdb_assert (low == high);
b64f50a1
JK
17004 if (dwarf2_per_objfile->all_comp_units[low]->offset.sect_off
17005 > offset.sect_off)
ae038cb0 17006 {
10b3939b 17007 if (low == 0)
8a3fe4f8
AC
17008 error (_("Dwarf Error: could not find partial DIE containing "
17009 "offset 0x%lx [in module %s]"),
b64f50a1 17010 (long) offset.sect_off, bfd_get_filename (objfile->obfd));
10b3939b 17011
b64f50a1
JK
17012 gdb_assert (dwarf2_per_objfile->all_comp_units[low-1]->offset.sect_off
17013 <= offset.sect_off);
ae038cb0
DJ
17014 return dwarf2_per_objfile->all_comp_units[low-1];
17015 }
17016 else
17017 {
17018 this_cu = dwarf2_per_objfile->all_comp_units[low];
17019 if (low == dwarf2_per_objfile->n_comp_units - 1
b64f50a1
JK
17020 && offset.sect_off >= this_cu->offset.sect_off + this_cu->length)
17021 error (_("invalid dwarf2 offset %u"), offset.sect_off);
17022 gdb_assert (offset.sect_off < this_cu->offset.sect_off + this_cu->length);
ae038cb0
DJ
17023 return this_cu;
17024 }
17025}
17026
23745b47 17027/* Initialize dwarf2_cu CU, owned by PER_CU. */
93311388 17028
9816fde3 17029static void
23745b47 17030init_one_comp_unit (struct dwarf2_cu *cu, struct dwarf2_per_cu_data *per_cu)
93311388 17031{
9816fde3 17032 memset (cu, 0, sizeof (*cu));
23745b47
DE
17033 per_cu->cu = cu;
17034 cu->per_cu = per_cu;
17035 cu->objfile = per_cu->objfile;
93311388 17036 obstack_init (&cu->comp_unit_obstack);
9816fde3
JK
17037}
17038
17039/* Initialize basic fields of dwarf_cu CU according to DIE COMP_UNIT_DIE. */
17040
17041static void
95554aad
TT
17042prepare_one_comp_unit (struct dwarf2_cu *cu, struct die_info *comp_unit_die,
17043 enum language pretend_language)
9816fde3
JK
17044{
17045 struct attribute *attr;
17046
17047 /* Set the language we're debugging. */
17048 attr = dwarf2_attr (comp_unit_die, DW_AT_language, cu);
17049 if (attr)
17050 set_cu_language (DW_UNSND (attr), cu);
17051 else
9cded63f 17052 {
95554aad 17053 cu->language = pretend_language;
9cded63f
TT
17054 cu->language_defn = language_def (cu->language);
17055 }
dee91e82
DE
17056
17057 attr = dwarf2_attr (comp_unit_die, DW_AT_producer, cu);
17058 if (attr)
17059 cu->producer = DW_STRING (attr);
93311388
DE
17060}
17061
ae038cb0
DJ
17062/* Release one cached compilation unit, CU. We unlink it from the tree
17063 of compilation units, but we don't remove it from the read_in_chain;
93311388
DE
17064 the caller is responsible for that.
17065 NOTE: DATA is a void * because this function is also used as a
17066 cleanup routine. */
ae038cb0
DJ
17067
17068static void
68dc6402 17069free_heap_comp_unit (void *data)
ae038cb0
DJ
17070{
17071 struct dwarf2_cu *cu = data;
17072
23745b47
DE
17073 gdb_assert (cu->per_cu != NULL);
17074 cu->per_cu->cu = NULL;
ae038cb0
DJ
17075 cu->per_cu = NULL;
17076
17077 obstack_free (&cu->comp_unit_obstack, NULL);
17078
17079 xfree (cu);
17080}
17081
72bf9492 17082/* This cleanup function is passed the address of a dwarf2_cu on the stack
ae038cb0 17083 when we're finished with it. We can't free the pointer itself, but be
dee91e82 17084 sure to unlink it from the cache. Also release any associated storage. */
72bf9492
DJ
17085
17086static void
17087free_stack_comp_unit (void *data)
17088{
17089 struct dwarf2_cu *cu = data;
17090
23745b47
DE
17091 gdb_assert (cu->per_cu != NULL);
17092 cu->per_cu->cu = NULL;
17093 cu->per_cu = NULL;
17094
72bf9492
DJ
17095 obstack_free (&cu->comp_unit_obstack, NULL);
17096 cu->partial_dies = NULL;
ae038cb0
DJ
17097}
17098
17099/* Free all cached compilation units. */
17100
17101static void
17102free_cached_comp_units (void *data)
17103{
17104 struct dwarf2_per_cu_data *per_cu, **last_chain;
17105
17106 per_cu = dwarf2_per_objfile->read_in_chain;
17107 last_chain = &dwarf2_per_objfile->read_in_chain;
17108 while (per_cu != NULL)
17109 {
17110 struct dwarf2_per_cu_data *next_cu;
17111
17112 next_cu = per_cu->cu->read_in_chain;
17113
68dc6402 17114 free_heap_comp_unit (per_cu->cu);
ae038cb0
DJ
17115 *last_chain = next_cu;
17116
17117 per_cu = next_cu;
17118 }
17119}
17120
17121/* Increase the age counter on each cached compilation unit, and free
17122 any that are too old. */
17123
17124static void
17125age_cached_comp_units (void)
17126{
17127 struct dwarf2_per_cu_data *per_cu, **last_chain;
17128
17129 dwarf2_clear_marks (dwarf2_per_objfile->read_in_chain);
17130 per_cu = dwarf2_per_objfile->read_in_chain;
17131 while (per_cu != NULL)
17132 {
17133 per_cu->cu->last_used ++;
17134 if (per_cu->cu->last_used <= dwarf2_max_cache_age)
17135 dwarf2_mark (per_cu->cu);
17136 per_cu = per_cu->cu->read_in_chain;
17137 }
17138
17139 per_cu = dwarf2_per_objfile->read_in_chain;
17140 last_chain = &dwarf2_per_objfile->read_in_chain;
17141 while (per_cu != NULL)
17142 {
17143 struct dwarf2_per_cu_data *next_cu;
17144
17145 next_cu = per_cu->cu->read_in_chain;
17146
17147 if (!per_cu->cu->mark)
17148 {
68dc6402 17149 free_heap_comp_unit (per_cu->cu);
ae038cb0
DJ
17150 *last_chain = next_cu;
17151 }
17152 else
17153 last_chain = &per_cu->cu->read_in_chain;
17154
17155 per_cu = next_cu;
17156 }
17157}
17158
17159/* Remove a single compilation unit from the cache. */
17160
17161static void
dee91e82 17162free_one_cached_comp_unit (struct dwarf2_per_cu_data *target_per_cu)
ae038cb0
DJ
17163{
17164 struct dwarf2_per_cu_data *per_cu, **last_chain;
17165
17166 per_cu = dwarf2_per_objfile->read_in_chain;
17167 last_chain = &dwarf2_per_objfile->read_in_chain;
17168 while (per_cu != NULL)
17169 {
17170 struct dwarf2_per_cu_data *next_cu;
17171
17172 next_cu = per_cu->cu->read_in_chain;
17173
dee91e82 17174 if (per_cu == target_per_cu)
ae038cb0 17175 {
68dc6402 17176 free_heap_comp_unit (per_cu->cu);
dee91e82 17177 per_cu->cu = NULL;
ae038cb0
DJ
17178 *last_chain = next_cu;
17179 break;
17180 }
17181 else
17182 last_chain = &per_cu->cu->read_in_chain;
17183
17184 per_cu = next_cu;
17185 }
17186}
17187
fe3e1990
DJ
17188/* Release all extra memory associated with OBJFILE. */
17189
17190void
17191dwarf2_free_objfile (struct objfile *objfile)
17192{
17193 dwarf2_per_objfile = objfile_data (objfile, dwarf2_objfile_data_key);
17194
17195 if (dwarf2_per_objfile == NULL)
17196 return;
17197
17198 /* Cached DIE trees use xmalloc and the comp_unit_obstack. */
17199 free_cached_comp_units (NULL);
17200
7b9f3c50
DE
17201 if (dwarf2_per_objfile->quick_file_names_table)
17202 htab_delete (dwarf2_per_objfile->quick_file_names_table);
9291a0cd 17203
fe3e1990
DJ
17204 /* Everything else should be on the objfile obstack. */
17205}
17206
dee91e82
DE
17207/* A set of CU "per_cu" pointer, DIE offset, and GDB type pointer.
17208 We store these in a hash table separate from the DIEs, and preserve them
17209 when the DIEs are flushed out of cache.
17210
17211 The CU "per_cu" pointer is needed because offset alone is not enough to
3019eac3
DE
17212 uniquely identify the type. A file may have multiple .debug_types sections,
17213 or the type may come from a DWO file. We have to use something in
17214 dwarf2_per_cu_data (or the pointer to it) because we can enter the lookup
17215 routine, get_die_type_at_offset, from outside this file, and thus won't
17216 necessarily have PER_CU->cu. Fortunately, PER_CU is stable for the life
17217 of the objfile. */
1c379e20 17218
dee91e82 17219struct dwarf2_per_cu_offset_and_type
1c379e20 17220{
dee91e82 17221 const struct dwarf2_per_cu_data *per_cu;
b64f50a1 17222 sect_offset offset;
1c379e20
DJ
17223 struct type *type;
17224};
17225
dee91e82 17226/* Hash function for a dwarf2_per_cu_offset_and_type. */
1c379e20
DJ
17227
17228static hashval_t
dee91e82 17229per_cu_offset_and_type_hash (const void *item)
1c379e20 17230{
dee91e82 17231 const struct dwarf2_per_cu_offset_and_type *ofs = item;
9a619af0 17232
dee91e82 17233 return (uintptr_t) ofs->per_cu + ofs->offset.sect_off;
1c379e20
DJ
17234}
17235
dee91e82 17236/* Equality function for a dwarf2_per_cu_offset_and_type. */
1c379e20
DJ
17237
17238static int
dee91e82 17239per_cu_offset_and_type_eq (const void *item_lhs, const void *item_rhs)
1c379e20 17240{
dee91e82
DE
17241 const struct dwarf2_per_cu_offset_and_type *ofs_lhs = item_lhs;
17242 const struct dwarf2_per_cu_offset_and_type *ofs_rhs = item_rhs;
9a619af0 17243
dee91e82
DE
17244 return (ofs_lhs->per_cu == ofs_rhs->per_cu
17245 && ofs_lhs->offset.sect_off == ofs_rhs->offset.sect_off);
1c379e20
DJ
17246}
17247
17248/* Set the type associated with DIE to TYPE. Save it in CU's hash
7e314c57
JK
17249 table if necessary. For convenience, return TYPE.
17250
17251 The DIEs reading must have careful ordering to:
17252 * Not cause infite loops trying to read in DIEs as a prerequisite for
17253 reading current DIE.
17254 * Not trying to dereference contents of still incompletely read in types
17255 while reading in other DIEs.
17256 * Enable referencing still incompletely read in types just by a pointer to
17257 the type without accessing its fields.
17258
17259 Therefore caller should follow these rules:
17260 * Try to fetch any prerequisite types we may need to build this DIE type
17261 before building the type and calling set_die_type.
e71ec853 17262 * After building type call set_die_type for current DIE as soon as
7e314c57
JK
17263 possible before fetching more types to complete the current type.
17264 * Make the type as complete as possible before fetching more types. */
1c379e20 17265
f792889a 17266static struct type *
1c379e20
DJ
17267set_die_type (struct die_info *die, struct type *type, struct dwarf2_cu *cu)
17268{
dee91e82 17269 struct dwarf2_per_cu_offset_and_type **slot, ofs;
673bfd45 17270 struct objfile *objfile = cu->objfile;
1c379e20 17271
b4ba55a1
JB
17272 /* For Ada types, make sure that the gnat-specific data is always
17273 initialized (if not already set). There are a few types where
17274 we should not be doing so, because the type-specific area is
17275 already used to hold some other piece of info (eg: TYPE_CODE_FLT
17276 where the type-specific area is used to store the floatformat).
17277 But this is not a problem, because the gnat-specific information
17278 is actually not needed for these types. */
17279 if (need_gnat_info (cu)
17280 && TYPE_CODE (type) != TYPE_CODE_FUNC
17281 && TYPE_CODE (type) != TYPE_CODE_FLT
17282 && !HAVE_GNAT_AUX_INFO (type))
17283 INIT_GNAT_SPECIFIC (type);
17284
dee91e82 17285 if (dwarf2_per_objfile->die_type_hash == NULL)
f792889a 17286 {
dee91e82
DE
17287 dwarf2_per_objfile->die_type_hash =
17288 htab_create_alloc_ex (127,
17289 per_cu_offset_and_type_hash,
17290 per_cu_offset_and_type_eq,
17291 NULL,
17292 &objfile->objfile_obstack,
17293 hashtab_obstack_allocate,
17294 dummy_obstack_deallocate);
f792889a 17295 }
1c379e20 17296
dee91e82 17297 ofs.per_cu = cu->per_cu;
1c379e20
DJ
17298 ofs.offset = die->offset;
17299 ofs.type = type;
dee91e82
DE
17300 slot = (struct dwarf2_per_cu_offset_and_type **)
17301 htab_find_slot (dwarf2_per_objfile->die_type_hash, &ofs, INSERT);
7e314c57
JK
17302 if (*slot)
17303 complaint (&symfile_complaints,
17304 _("A problem internal to GDB: DIE 0x%x has type already set"),
b64f50a1 17305 die->offset.sect_off);
673bfd45 17306 *slot = obstack_alloc (&objfile->objfile_obstack, sizeof (**slot));
1c379e20 17307 **slot = ofs;
f792889a 17308 return type;
1c379e20
DJ
17309}
17310
380bca97 17311/* Look up the type for the die at OFFSET in the appropriate type_hash
673bfd45 17312 table, or return NULL if the die does not have a saved type. */
1c379e20
DJ
17313
17314static struct type *
b64f50a1 17315get_die_type_at_offset (sect_offset offset,
673bfd45 17316 struct dwarf2_per_cu_data *per_cu)
1c379e20 17317{
dee91e82 17318 struct dwarf2_per_cu_offset_and_type *slot, ofs;
f792889a 17319
dee91e82 17320 if (dwarf2_per_objfile->die_type_hash == NULL)
f792889a 17321 return NULL;
1c379e20 17322
dee91e82 17323 ofs.per_cu = per_cu;
673bfd45 17324 ofs.offset = offset;
dee91e82 17325 slot = htab_find (dwarf2_per_objfile->die_type_hash, &ofs);
1c379e20
DJ
17326 if (slot)
17327 return slot->type;
17328 else
17329 return NULL;
17330}
17331
673bfd45
DE
17332/* Look up the type for DIE in the appropriate type_hash table,
17333 or return NULL if DIE does not have a saved type. */
17334
17335static struct type *
17336get_die_type (struct die_info *die, struct dwarf2_cu *cu)
17337{
17338 return get_die_type_at_offset (die->offset, cu->per_cu);
17339}
17340
10b3939b
DJ
17341/* Add a dependence relationship from CU to REF_PER_CU. */
17342
17343static void
17344dwarf2_add_dependence (struct dwarf2_cu *cu,
17345 struct dwarf2_per_cu_data *ref_per_cu)
17346{
17347 void **slot;
17348
17349 if (cu->dependencies == NULL)
17350 cu->dependencies
17351 = htab_create_alloc_ex (5, htab_hash_pointer, htab_eq_pointer,
17352 NULL, &cu->comp_unit_obstack,
17353 hashtab_obstack_allocate,
17354 dummy_obstack_deallocate);
17355
17356 slot = htab_find_slot (cu->dependencies, ref_per_cu, INSERT);
17357 if (*slot == NULL)
17358 *slot = ref_per_cu;
17359}
1c379e20 17360
f504f079
DE
17361/* Subroutine of dwarf2_mark to pass to htab_traverse.
17362 Set the mark field in every compilation unit in the
ae038cb0
DJ
17363 cache that we must keep because we are keeping CU. */
17364
10b3939b
DJ
17365static int
17366dwarf2_mark_helper (void **slot, void *data)
17367{
17368 struct dwarf2_per_cu_data *per_cu;
17369
17370 per_cu = (struct dwarf2_per_cu_data *) *slot;
d07ed419
JK
17371
17372 /* cu->dependencies references may not yet have been ever read if QUIT aborts
17373 reading of the chain. As such dependencies remain valid it is not much
17374 useful to track and undo them during QUIT cleanups. */
17375 if (per_cu->cu == NULL)
17376 return 1;
17377
10b3939b
DJ
17378 if (per_cu->cu->mark)
17379 return 1;
17380 per_cu->cu->mark = 1;
17381
17382 if (per_cu->cu->dependencies != NULL)
17383 htab_traverse (per_cu->cu->dependencies, dwarf2_mark_helper, NULL);
17384
17385 return 1;
17386}
17387
f504f079
DE
17388/* Set the mark field in CU and in every other compilation unit in the
17389 cache that we must keep because we are keeping CU. */
17390
ae038cb0
DJ
17391static void
17392dwarf2_mark (struct dwarf2_cu *cu)
17393{
17394 if (cu->mark)
17395 return;
17396 cu->mark = 1;
10b3939b
DJ
17397 if (cu->dependencies != NULL)
17398 htab_traverse (cu->dependencies, dwarf2_mark_helper, NULL);
ae038cb0
DJ
17399}
17400
17401static void
17402dwarf2_clear_marks (struct dwarf2_per_cu_data *per_cu)
17403{
17404 while (per_cu)
17405 {
17406 per_cu->cu->mark = 0;
17407 per_cu = per_cu->cu->read_in_chain;
17408 }
72bf9492
DJ
17409}
17410
72bf9492
DJ
17411/* Trivial hash function for partial_die_info: the hash value of a DIE
17412 is its offset in .debug_info for this objfile. */
17413
17414static hashval_t
17415partial_die_hash (const void *item)
17416{
17417 const struct partial_die_info *part_die = item;
9a619af0 17418
b64f50a1 17419 return part_die->offset.sect_off;
72bf9492
DJ
17420}
17421
17422/* Trivial comparison function for partial_die_info structures: two DIEs
17423 are equal if they have the same offset. */
17424
17425static int
17426partial_die_eq (const void *item_lhs, const void *item_rhs)
17427{
17428 const struct partial_die_info *part_die_lhs = item_lhs;
17429 const struct partial_die_info *part_die_rhs = item_rhs;
9a619af0 17430
b64f50a1 17431 return part_die_lhs->offset.sect_off == part_die_rhs->offset.sect_off;
72bf9492
DJ
17432}
17433
ae038cb0
DJ
17434static struct cmd_list_element *set_dwarf2_cmdlist;
17435static struct cmd_list_element *show_dwarf2_cmdlist;
17436
17437static void
17438set_dwarf2_cmd (char *args, int from_tty)
17439{
17440 help_list (set_dwarf2_cmdlist, "maintenance set dwarf2 ", -1, gdb_stdout);
17441}
17442
17443static void
17444show_dwarf2_cmd (char *args, int from_tty)
6e70227d 17445{
ae038cb0
DJ
17446 cmd_show_list (show_dwarf2_cmdlist, from_tty, "");
17447}
17448
dce234bc
PP
17449/* If section described by INFO was mmapped, munmap it now. */
17450
17451static void
17452munmap_section_buffer (struct dwarf2_section_info *info)
17453{
b315ab21 17454 if (info->map_addr != NULL)
dce234bc
PP
17455 {
17456#ifdef HAVE_MMAP
b315ab21 17457 int res;
9a619af0 17458
b315ab21
TG
17459 res = munmap (info->map_addr, info->map_len);
17460 gdb_assert (res == 0);
dce234bc
PP
17461#else
17462 /* Without HAVE_MMAP, we should never be here to begin with. */
f3574227 17463 gdb_assert_not_reached ("no mmap support");
dce234bc
PP
17464#endif
17465 }
17466}
17467
17468/* munmap debug sections for OBJFILE, if necessary. */
17469
17470static void
c1bd65d0 17471dwarf2_per_objfile_free (struct objfile *objfile, void *d)
dce234bc
PP
17472{
17473 struct dwarf2_per_objfile *data = d;
8b70b953
TT
17474 int ix;
17475 struct dwarf2_section_info *section;
9a619af0 17476
16be1145
DE
17477 /* This is sorted according to the order they're defined in to make it easier
17478 to keep in sync. */
dce234bc
PP
17479 munmap_section_buffer (&data->info);
17480 munmap_section_buffer (&data->abbrev);
17481 munmap_section_buffer (&data->line);
16be1145 17482 munmap_section_buffer (&data->loc);
dce234bc 17483 munmap_section_buffer (&data->macinfo);
cf2c3c16 17484 munmap_section_buffer (&data->macro);
16be1145 17485 munmap_section_buffer (&data->str);
dce234bc 17486 munmap_section_buffer (&data->ranges);
3019eac3 17487 munmap_section_buffer (&data->addr);
dce234bc
PP
17488 munmap_section_buffer (&data->frame);
17489 munmap_section_buffer (&data->eh_frame);
9291a0cd 17490 munmap_section_buffer (&data->gdb_index);
8b70b953
TT
17491
17492 for (ix = 0;
17493 VEC_iterate (dwarf2_section_info_def, data->types, ix, section);
17494 ++ix)
17495 munmap_section_buffer (section);
17496
95554aad
TT
17497 for (ix = 0; ix < dwarf2_per_objfile->n_comp_units; ++ix)
17498 VEC_free (dwarf2_per_cu_ptr,
17499 dwarf2_per_objfile->all_comp_units[ix]->imported_symtabs);
17500
8b70b953 17501 VEC_free (dwarf2_section_info_def, data->types);
3019eac3
DE
17502
17503 if (data->dwo_files)
17504 free_dwo_files (data->dwo_files, objfile);
9291a0cd
TT
17505}
17506
17507\f
ae2de4f8 17508/* The "save gdb-index" command. */
9291a0cd
TT
17509
17510/* The contents of the hash table we create when building the string
17511 table. */
17512struct strtab_entry
17513{
17514 offset_type offset;
17515 const char *str;
17516};
17517
559a7a62
JK
17518/* Hash function for a strtab_entry.
17519
17520 Function is used only during write_hash_table so no index format backward
17521 compatibility is needed. */
b89be57b 17522
9291a0cd
TT
17523static hashval_t
17524hash_strtab_entry (const void *e)
17525{
17526 const struct strtab_entry *entry = e;
559a7a62 17527 return mapped_index_string_hash (INT_MAX, entry->str);
9291a0cd
TT
17528}
17529
17530/* Equality function for a strtab_entry. */
b89be57b 17531
9291a0cd
TT
17532static int
17533eq_strtab_entry (const void *a, const void *b)
17534{
17535 const struct strtab_entry *ea = a;
17536 const struct strtab_entry *eb = b;
17537 return !strcmp (ea->str, eb->str);
17538}
17539
17540/* Create a strtab_entry hash table. */
b89be57b 17541
9291a0cd
TT
17542static htab_t
17543create_strtab (void)
17544{
17545 return htab_create_alloc (100, hash_strtab_entry, eq_strtab_entry,
17546 xfree, xcalloc, xfree);
17547}
17548
17549/* Add a string to the constant pool. Return the string's offset in
17550 host order. */
b89be57b 17551
9291a0cd
TT
17552static offset_type
17553add_string (htab_t table, struct obstack *cpool, const char *str)
17554{
17555 void **slot;
17556 struct strtab_entry entry;
17557 struct strtab_entry *result;
17558
17559 entry.str = str;
17560 slot = htab_find_slot (table, &entry, INSERT);
17561 if (*slot)
17562 result = *slot;
17563 else
17564 {
17565 result = XNEW (struct strtab_entry);
17566 result->offset = obstack_object_size (cpool);
17567 result->str = str;
17568 obstack_grow_str0 (cpool, str);
17569 *slot = result;
17570 }
17571 return result->offset;
17572}
17573
17574/* An entry in the symbol table. */
17575struct symtab_index_entry
17576{
17577 /* The name of the symbol. */
17578 const char *name;
17579 /* The offset of the name in the constant pool. */
17580 offset_type index_offset;
17581 /* A sorted vector of the indices of all the CUs that hold an object
17582 of this name. */
17583 VEC (offset_type) *cu_indices;
17584};
17585
17586/* The symbol table. This is a power-of-2-sized hash table. */
17587struct mapped_symtab
17588{
17589 offset_type n_elements;
17590 offset_type size;
17591 struct symtab_index_entry **data;
17592};
17593
17594/* Hash function for a symtab_index_entry. */
b89be57b 17595
9291a0cd
TT
17596static hashval_t
17597hash_symtab_entry (const void *e)
17598{
17599 const struct symtab_index_entry *entry = e;
17600 return iterative_hash (VEC_address (offset_type, entry->cu_indices),
17601 sizeof (offset_type) * VEC_length (offset_type,
17602 entry->cu_indices),
17603 0);
17604}
17605
17606/* Equality function for a symtab_index_entry. */
b89be57b 17607
9291a0cd
TT
17608static int
17609eq_symtab_entry (const void *a, const void *b)
17610{
17611 const struct symtab_index_entry *ea = a;
17612 const struct symtab_index_entry *eb = b;
17613 int len = VEC_length (offset_type, ea->cu_indices);
17614 if (len != VEC_length (offset_type, eb->cu_indices))
17615 return 0;
17616 return !memcmp (VEC_address (offset_type, ea->cu_indices),
17617 VEC_address (offset_type, eb->cu_indices),
17618 sizeof (offset_type) * len);
17619}
17620
17621/* Destroy a symtab_index_entry. */
b89be57b 17622
9291a0cd
TT
17623static void
17624delete_symtab_entry (void *p)
17625{
17626 struct symtab_index_entry *entry = p;
17627 VEC_free (offset_type, entry->cu_indices);
17628 xfree (entry);
17629}
17630
17631/* Create a hash table holding symtab_index_entry objects. */
b89be57b 17632
9291a0cd 17633static htab_t
3876f04e 17634create_symbol_hash_table (void)
9291a0cd
TT
17635{
17636 return htab_create_alloc (100, hash_symtab_entry, eq_symtab_entry,
17637 delete_symtab_entry, xcalloc, xfree);
17638}
17639
17640/* Create a new mapped symtab object. */
b89be57b 17641
9291a0cd
TT
17642static struct mapped_symtab *
17643create_mapped_symtab (void)
17644{
17645 struct mapped_symtab *symtab = XNEW (struct mapped_symtab);
17646 symtab->n_elements = 0;
17647 symtab->size = 1024;
17648 symtab->data = XCNEWVEC (struct symtab_index_entry *, symtab->size);
17649 return symtab;
17650}
17651
17652/* Destroy a mapped_symtab. */
b89be57b 17653
9291a0cd
TT
17654static void
17655cleanup_mapped_symtab (void *p)
17656{
17657 struct mapped_symtab *symtab = p;
17658 /* The contents of the array are freed when the other hash table is
17659 destroyed. */
17660 xfree (symtab->data);
17661 xfree (symtab);
17662}
17663
17664/* Find a slot in SYMTAB for the symbol NAME. Returns a pointer to
559a7a62
JK
17665 the slot.
17666
17667 Function is used only during write_hash_table so no index format backward
17668 compatibility is needed. */
b89be57b 17669
9291a0cd
TT
17670static struct symtab_index_entry **
17671find_slot (struct mapped_symtab *symtab, const char *name)
17672{
559a7a62 17673 offset_type index, step, hash = mapped_index_string_hash (INT_MAX, name);
9291a0cd
TT
17674
17675 index = hash & (symtab->size - 1);
17676 step = ((hash * 17) & (symtab->size - 1)) | 1;
17677
17678 for (;;)
17679 {
17680 if (!symtab->data[index] || !strcmp (name, symtab->data[index]->name))
17681 return &symtab->data[index];
17682 index = (index + step) & (symtab->size - 1);
17683 }
17684}
17685
17686/* Expand SYMTAB's hash table. */
b89be57b 17687
9291a0cd
TT
17688static void
17689hash_expand (struct mapped_symtab *symtab)
17690{
17691 offset_type old_size = symtab->size;
17692 offset_type i;
17693 struct symtab_index_entry **old_entries = symtab->data;
17694
17695 symtab->size *= 2;
17696 symtab->data = XCNEWVEC (struct symtab_index_entry *, symtab->size);
17697
17698 for (i = 0; i < old_size; ++i)
17699 {
17700 if (old_entries[i])
17701 {
17702 struct symtab_index_entry **slot = find_slot (symtab,
17703 old_entries[i]->name);
17704 *slot = old_entries[i];
17705 }
17706 }
17707
17708 xfree (old_entries);
17709}
17710
156942c7
DE
17711/* Add an entry to SYMTAB. NAME is the name of the symbol.
17712 CU_INDEX is the index of the CU in which the symbol appears.
17713 IS_STATIC is one if the symbol is static, otherwise zero (global). */
b89be57b 17714
9291a0cd
TT
17715static void
17716add_index_entry (struct mapped_symtab *symtab, const char *name,
156942c7 17717 int is_static, gdb_index_symbol_kind kind,
9291a0cd
TT
17718 offset_type cu_index)
17719{
17720 struct symtab_index_entry **slot;
156942c7 17721 offset_type cu_index_and_attrs;
9291a0cd
TT
17722
17723 ++symtab->n_elements;
17724 if (4 * symtab->n_elements / 3 >= symtab->size)
17725 hash_expand (symtab);
17726
17727 slot = find_slot (symtab, name);
17728 if (!*slot)
17729 {
17730 *slot = XNEW (struct symtab_index_entry);
17731 (*slot)->name = name;
156942c7 17732 /* index_offset is set later. */
9291a0cd
TT
17733 (*slot)->cu_indices = NULL;
17734 }
156942c7
DE
17735
17736 cu_index_and_attrs = 0;
17737 DW2_GDB_INDEX_CU_SET_VALUE (cu_index_and_attrs, cu_index);
17738 DW2_GDB_INDEX_SYMBOL_STATIC_SET_VALUE (cu_index_and_attrs, is_static);
17739 DW2_GDB_INDEX_SYMBOL_KIND_SET_VALUE (cu_index_and_attrs, kind);
17740
17741 /* We don't want to record an index value twice as we want to avoid the
17742 duplication.
17743 We process all global symbols and then all static symbols
17744 (which would allow us to avoid the duplication by only having to check
17745 the last entry pushed), but a symbol could have multiple kinds in one CU.
17746 To keep things simple we don't worry about the duplication here and
17747 sort and uniqufy the list after we've processed all symbols. */
17748 VEC_safe_push (offset_type, (*slot)->cu_indices, cu_index_and_attrs);
17749}
17750
17751/* qsort helper routine for uniquify_cu_indices. */
17752
17753static int
17754offset_type_compare (const void *ap, const void *bp)
17755{
17756 offset_type a = *(offset_type *) ap;
17757 offset_type b = *(offset_type *) bp;
17758
17759 return (a > b) - (b > a);
17760}
17761
17762/* Sort and remove duplicates of all symbols' cu_indices lists. */
17763
17764static void
17765uniquify_cu_indices (struct mapped_symtab *symtab)
17766{
17767 int i;
17768
17769 for (i = 0; i < symtab->size; ++i)
17770 {
17771 struct symtab_index_entry *entry = symtab->data[i];
17772
17773 if (entry
17774 && entry->cu_indices != NULL)
17775 {
17776 unsigned int next_to_insert, next_to_check;
17777 offset_type last_value;
17778
17779 qsort (VEC_address (offset_type, entry->cu_indices),
17780 VEC_length (offset_type, entry->cu_indices),
17781 sizeof (offset_type), offset_type_compare);
17782
17783 last_value = VEC_index (offset_type, entry->cu_indices, 0);
17784 next_to_insert = 1;
17785 for (next_to_check = 1;
17786 next_to_check < VEC_length (offset_type, entry->cu_indices);
17787 ++next_to_check)
17788 {
17789 if (VEC_index (offset_type, entry->cu_indices, next_to_check)
17790 != last_value)
17791 {
17792 last_value = VEC_index (offset_type, entry->cu_indices,
17793 next_to_check);
17794 VEC_replace (offset_type, entry->cu_indices, next_to_insert,
17795 last_value);
17796 ++next_to_insert;
17797 }
17798 }
17799 VEC_truncate (offset_type, entry->cu_indices, next_to_insert);
17800 }
17801 }
9291a0cd
TT
17802}
17803
17804/* Add a vector of indices to the constant pool. */
b89be57b 17805
9291a0cd 17806static offset_type
3876f04e 17807add_indices_to_cpool (htab_t symbol_hash_table, struct obstack *cpool,
9291a0cd
TT
17808 struct symtab_index_entry *entry)
17809{
17810 void **slot;
17811
3876f04e 17812 slot = htab_find_slot (symbol_hash_table, entry, INSERT);
9291a0cd
TT
17813 if (!*slot)
17814 {
17815 offset_type len = VEC_length (offset_type, entry->cu_indices);
17816 offset_type val = MAYBE_SWAP (len);
17817 offset_type iter;
17818 int i;
17819
17820 *slot = entry;
17821 entry->index_offset = obstack_object_size (cpool);
17822
17823 obstack_grow (cpool, &val, sizeof (val));
17824 for (i = 0;
17825 VEC_iterate (offset_type, entry->cu_indices, i, iter);
17826 ++i)
17827 {
17828 val = MAYBE_SWAP (iter);
17829 obstack_grow (cpool, &val, sizeof (val));
17830 }
17831 }
17832 else
17833 {
17834 struct symtab_index_entry *old_entry = *slot;
17835 entry->index_offset = old_entry->index_offset;
17836 entry = old_entry;
17837 }
17838 return entry->index_offset;
17839}
17840
17841/* Write the mapped hash table SYMTAB to the obstack OUTPUT, with
17842 constant pool entries going into the obstack CPOOL. */
b89be57b 17843
9291a0cd
TT
17844static void
17845write_hash_table (struct mapped_symtab *symtab,
17846 struct obstack *output, struct obstack *cpool)
17847{
17848 offset_type i;
3876f04e 17849 htab_t symbol_hash_table;
9291a0cd
TT
17850 htab_t str_table;
17851
3876f04e 17852 symbol_hash_table = create_symbol_hash_table ();
9291a0cd 17853 str_table = create_strtab ();
3876f04e 17854
9291a0cd
TT
17855 /* We add all the index vectors to the constant pool first, to
17856 ensure alignment is ok. */
17857 for (i = 0; i < symtab->size; ++i)
17858 {
17859 if (symtab->data[i])
3876f04e 17860 add_indices_to_cpool (symbol_hash_table, cpool, symtab->data[i]);
9291a0cd
TT
17861 }
17862
17863 /* Now write out the hash table. */
17864 for (i = 0; i < symtab->size; ++i)
17865 {
17866 offset_type str_off, vec_off;
17867
17868 if (symtab->data[i])
17869 {
17870 str_off = add_string (str_table, cpool, symtab->data[i]->name);
17871 vec_off = symtab->data[i]->index_offset;
17872 }
17873 else
17874 {
17875 /* While 0 is a valid constant pool index, it is not valid
17876 to have 0 for both offsets. */
17877 str_off = 0;
17878 vec_off = 0;
17879 }
17880
17881 str_off = MAYBE_SWAP (str_off);
17882 vec_off = MAYBE_SWAP (vec_off);
17883
17884 obstack_grow (output, &str_off, sizeof (str_off));
17885 obstack_grow (output, &vec_off, sizeof (vec_off));
17886 }
17887
17888 htab_delete (str_table);
3876f04e 17889 htab_delete (symbol_hash_table);
9291a0cd
TT
17890}
17891
0a5429f6
DE
17892/* Struct to map psymtab to CU index in the index file. */
17893struct psymtab_cu_index_map
17894{
17895 struct partial_symtab *psymtab;
17896 unsigned int cu_index;
17897};
17898
17899static hashval_t
17900hash_psymtab_cu_index (const void *item)
17901{
17902 const struct psymtab_cu_index_map *map = item;
17903
17904 return htab_hash_pointer (map->psymtab);
17905}
17906
17907static int
17908eq_psymtab_cu_index (const void *item_lhs, const void *item_rhs)
17909{
17910 const struct psymtab_cu_index_map *lhs = item_lhs;
17911 const struct psymtab_cu_index_map *rhs = item_rhs;
17912
17913 return lhs->psymtab == rhs->psymtab;
17914}
17915
17916/* Helper struct for building the address table. */
17917struct addrmap_index_data
17918{
17919 struct objfile *objfile;
17920 struct obstack *addr_obstack;
17921 htab_t cu_index_htab;
17922
17923 /* Non-zero if the previous_* fields are valid.
17924 We can't write an entry until we see the next entry (since it is only then
17925 that we know the end of the entry). */
17926 int previous_valid;
17927 /* Index of the CU in the table of all CUs in the index file. */
17928 unsigned int previous_cu_index;
0963b4bd 17929 /* Start address of the CU. */
0a5429f6
DE
17930 CORE_ADDR previous_cu_start;
17931};
17932
17933/* Write an address entry to OBSTACK. */
b89be57b 17934
9291a0cd 17935static void
0a5429f6
DE
17936add_address_entry (struct objfile *objfile, struct obstack *obstack,
17937 CORE_ADDR start, CORE_ADDR end, unsigned int cu_index)
9291a0cd 17938{
0a5429f6 17939 offset_type cu_index_to_write;
9291a0cd
TT
17940 char addr[8];
17941 CORE_ADDR baseaddr;
17942
17943 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
17944
0a5429f6
DE
17945 store_unsigned_integer (addr, 8, BFD_ENDIAN_LITTLE, start - baseaddr);
17946 obstack_grow (obstack, addr, 8);
17947 store_unsigned_integer (addr, 8, BFD_ENDIAN_LITTLE, end - baseaddr);
17948 obstack_grow (obstack, addr, 8);
17949 cu_index_to_write = MAYBE_SWAP (cu_index);
17950 obstack_grow (obstack, &cu_index_to_write, sizeof (offset_type));
17951}
17952
17953/* Worker function for traversing an addrmap to build the address table. */
17954
17955static int
17956add_address_entry_worker (void *datap, CORE_ADDR start_addr, void *obj)
17957{
17958 struct addrmap_index_data *data = datap;
17959 struct partial_symtab *pst = obj;
0a5429f6
DE
17960
17961 if (data->previous_valid)
17962 add_address_entry (data->objfile, data->addr_obstack,
17963 data->previous_cu_start, start_addr,
17964 data->previous_cu_index);
17965
17966 data->previous_cu_start = start_addr;
17967 if (pst != NULL)
17968 {
17969 struct psymtab_cu_index_map find_map, *map;
17970 find_map.psymtab = pst;
17971 map = htab_find (data->cu_index_htab, &find_map);
17972 gdb_assert (map != NULL);
17973 data->previous_cu_index = map->cu_index;
17974 data->previous_valid = 1;
17975 }
17976 else
17977 data->previous_valid = 0;
17978
17979 return 0;
17980}
17981
17982/* Write OBJFILE's address map to OBSTACK.
17983 CU_INDEX_HTAB is used to map addrmap entries to their CU indices
17984 in the index file. */
17985
17986static void
17987write_address_map (struct objfile *objfile, struct obstack *obstack,
17988 htab_t cu_index_htab)
17989{
17990 struct addrmap_index_data addrmap_index_data;
17991
17992 /* When writing the address table, we have to cope with the fact that
17993 the addrmap iterator only provides the start of a region; we have to
17994 wait until the next invocation to get the start of the next region. */
17995
17996 addrmap_index_data.objfile = objfile;
17997 addrmap_index_data.addr_obstack = obstack;
17998 addrmap_index_data.cu_index_htab = cu_index_htab;
17999 addrmap_index_data.previous_valid = 0;
18000
18001 addrmap_foreach (objfile->psymtabs_addrmap, add_address_entry_worker,
18002 &addrmap_index_data);
18003
18004 /* It's highly unlikely the last entry (end address = 0xff...ff)
18005 is valid, but we should still handle it.
18006 The end address is recorded as the start of the next region, but that
18007 doesn't work here. To cope we pass 0xff...ff, this is a rare situation
18008 anyway. */
18009 if (addrmap_index_data.previous_valid)
18010 add_address_entry (objfile, obstack,
18011 addrmap_index_data.previous_cu_start, (CORE_ADDR) -1,
18012 addrmap_index_data.previous_cu_index);
9291a0cd
TT
18013}
18014
156942c7
DE
18015/* Return the symbol kind of PSYM. */
18016
18017static gdb_index_symbol_kind
18018symbol_kind (struct partial_symbol *psym)
18019{
18020 domain_enum domain = PSYMBOL_DOMAIN (psym);
18021 enum address_class aclass = PSYMBOL_CLASS (psym);
18022
18023 switch (domain)
18024 {
18025 case VAR_DOMAIN:
18026 switch (aclass)
18027 {
18028 case LOC_BLOCK:
18029 return GDB_INDEX_SYMBOL_KIND_FUNCTION;
18030 case LOC_TYPEDEF:
18031 return GDB_INDEX_SYMBOL_KIND_TYPE;
18032 case LOC_COMPUTED:
18033 case LOC_CONST_BYTES:
18034 case LOC_OPTIMIZED_OUT:
18035 case LOC_STATIC:
18036 return GDB_INDEX_SYMBOL_KIND_VARIABLE;
18037 case LOC_CONST:
18038 /* Note: It's currently impossible to recognize psyms as enum values
18039 short of reading the type info. For now punt. */
18040 return GDB_INDEX_SYMBOL_KIND_VARIABLE;
18041 default:
18042 /* There are other LOC_FOO values that one might want to classify
18043 as variables, but dwarf2read.c doesn't currently use them. */
18044 return GDB_INDEX_SYMBOL_KIND_OTHER;
18045 }
18046 case STRUCT_DOMAIN:
18047 return GDB_INDEX_SYMBOL_KIND_TYPE;
18048 default:
18049 return GDB_INDEX_SYMBOL_KIND_OTHER;
18050 }
18051}
18052
9291a0cd 18053/* Add a list of partial symbols to SYMTAB. */
b89be57b 18054
9291a0cd
TT
18055static void
18056write_psymbols (struct mapped_symtab *symtab,
987d643c 18057 htab_t psyms_seen,
9291a0cd
TT
18058 struct partial_symbol **psymp,
18059 int count,
987d643c
TT
18060 offset_type cu_index,
18061 int is_static)
9291a0cd
TT
18062{
18063 for (; count-- > 0; ++psymp)
18064 {
156942c7
DE
18065 struct partial_symbol *psym = *psymp;
18066 void **slot;
987d643c 18067
156942c7 18068 if (SYMBOL_LANGUAGE (psym) == language_ada)
9291a0cd 18069 error (_("Ada is not currently supported by the index"));
987d643c 18070
987d643c 18071 /* Only add a given psymbol once. */
156942c7 18072 slot = htab_find_slot (psyms_seen, psym, INSERT);
987d643c
TT
18073 if (!*slot)
18074 {
156942c7
DE
18075 gdb_index_symbol_kind kind = symbol_kind (psym);
18076
18077 *slot = psym;
18078 add_index_entry (symtab, SYMBOL_SEARCH_NAME (psym),
18079 is_static, kind, cu_index);
987d643c 18080 }
9291a0cd
TT
18081 }
18082}
18083
18084/* Write the contents of an ("unfinished") obstack to FILE. Throw an
18085 exception if there is an error. */
b89be57b 18086
9291a0cd
TT
18087static void
18088write_obstack (FILE *file, struct obstack *obstack)
18089{
18090 if (fwrite (obstack_base (obstack), 1, obstack_object_size (obstack),
18091 file)
18092 != obstack_object_size (obstack))
18093 error (_("couldn't data write to file"));
18094}
18095
18096/* Unlink a file if the argument is not NULL. */
b89be57b 18097
9291a0cd
TT
18098static void
18099unlink_if_set (void *p)
18100{
18101 char **filename = p;
18102 if (*filename)
18103 unlink (*filename);
18104}
18105
1fd400ff
TT
18106/* A helper struct used when iterating over debug_types. */
18107struct signatured_type_index_data
18108{
18109 struct objfile *objfile;
18110 struct mapped_symtab *symtab;
18111 struct obstack *types_list;
987d643c 18112 htab_t psyms_seen;
1fd400ff
TT
18113 int cu_index;
18114};
18115
18116/* A helper function that writes a single signatured_type to an
18117 obstack. */
b89be57b 18118
1fd400ff
TT
18119static int
18120write_one_signatured_type (void **slot, void *d)
18121{
18122 struct signatured_type_index_data *info = d;
18123 struct signatured_type *entry = (struct signatured_type *) *slot;
e254ef6a
DE
18124 struct dwarf2_per_cu_data *per_cu = &entry->per_cu;
18125 struct partial_symtab *psymtab = per_cu->v.psymtab;
1fd400ff
TT
18126 gdb_byte val[8];
18127
18128 write_psymbols (info->symtab,
987d643c 18129 info->psyms_seen,
3e43a32a
MS
18130 info->objfile->global_psymbols.list
18131 + psymtab->globals_offset,
987d643c
TT
18132 psymtab->n_global_syms, info->cu_index,
18133 0);
1fd400ff 18134 write_psymbols (info->symtab,
987d643c 18135 info->psyms_seen,
3e43a32a
MS
18136 info->objfile->static_psymbols.list
18137 + psymtab->statics_offset,
987d643c
TT
18138 psymtab->n_static_syms, info->cu_index,
18139 1);
1fd400ff 18140
b64f50a1
JK
18141 store_unsigned_integer (val, 8, BFD_ENDIAN_LITTLE,
18142 entry->per_cu.offset.sect_off);
1fd400ff 18143 obstack_grow (info->types_list, val, 8);
3019eac3
DE
18144 store_unsigned_integer (val, 8, BFD_ENDIAN_LITTLE,
18145 entry->type_offset_in_tu.cu_off);
1fd400ff
TT
18146 obstack_grow (info->types_list, val, 8);
18147 store_unsigned_integer (val, 8, BFD_ENDIAN_LITTLE, entry->signature);
18148 obstack_grow (info->types_list, val, 8);
18149
18150 ++info->cu_index;
18151
18152 return 1;
18153}
18154
95554aad
TT
18155/* Recurse into all "included" dependencies and write their symbols as
18156 if they appeared in this psymtab. */
18157
18158static void
18159recursively_write_psymbols (struct objfile *objfile,
18160 struct partial_symtab *psymtab,
18161 struct mapped_symtab *symtab,
18162 htab_t psyms_seen,
18163 offset_type cu_index)
18164{
18165 int i;
18166
18167 for (i = 0; i < psymtab->number_of_dependencies; ++i)
18168 if (psymtab->dependencies[i]->user != NULL)
18169 recursively_write_psymbols (objfile, psymtab->dependencies[i],
18170 symtab, psyms_seen, cu_index);
18171
18172 write_psymbols (symtab,
18173 psyms_seen,
18174 objfile->global_psymbols.list + psymtab->globals_offset,
18175 psymtab->n_global_syms, cu_index,
18176 0);
18177 write_psymbols (symtab,
18178 psyms_seen,
18179 objfile->static_psymbols.list + psymtab->statics_offset,
18180 psymtab->n_static_syms, cu_index,
18181 1);
18182}
18183
9291a0cd 18184/* Create an index file for OBJFILE in the directory DIR. */
b89be57b 18185
9291a0cd
TT
18186static void
18187write_psymtabs_to_index (struct objfile *objfile, const char *dir)
18188{
18189 struct cleanup *cleanup;
18190 char *filename, *cleanup_filename;
1fd400ff
TT
18191 struct obstack contents, addr_obstack, constant_pool, symtab_obstack;
18192 struct obstack cu_list, types_cu_list;
9291a0cd
TT
18193 int i;
18194 FILE *out_file;
18195 struct mapped_symtab *symtab;
18196 offset_type val, size_of_contents, total_len;
18197 struct stat st;
987d643c 18198 htab_t psyms_seen;
0a5429f6
DE
18199 htab_t cu_index_htab;
18200 struct psymtab_cu_index_map *psymtab_cu_index_map;
9291a0cd 18201
b4f2f049 18202 if (!objfile->psymtabs || !objfile->psymtabs_addrmap)
9291a0cd 18203 return;
b4f2f049 18204
9291a0cd
TT
18205 if (dwarf2_per_objfile->using_index)
18206 error (_("Cannot use an index to create the index"));
18207
8b70b953
TT
18208 if (VEC_length (dwarf2_section_info_def, dwarf2_per_objfile->types) > 1)
18209 error (_("Cannot make an index when the file has multiple .debug_types sections"));
18210
9291a0cd 18211 if (stat (objfile->name, &st) < 0)
7e17e088 18212 perror_with_name (objfile->name);
9291a0cd
TT
18213
18214 filename = concat (dir, SLASH_STRING, lbasename (objfile->name),
18215 INDEX_SUFFIX, (char *) NULL);
18216 cleanup = make_cleanup (xfree, filename);
18217
18218 out_file = fopen (filename, "wb");
18219 if (!out_file)
18220 error (_("Can't open `%s' for writing"), filename);
18221
18222 cleanup_filename = filename;
18223 make_cleanup (unlink_if_set, &cleanup_filename);
18224
18225 symtab = create_mapped_symtab ();
18226 make_cleanup (cleanup_mapped_symtab, symtab);
18227
18228 obstack_init (&addr_obstack);
18229 make_cleanup_obstack_free (&addr_obstack);
18230
18231 obstack_init (&cu_list);
18232 make_cleanup_obstack_free (&cu_list);
18233
1fd400ff
TT
18234 obstack_init (&types_cu_list);
18235 make_cleanup_obstack_free (&types_cu_list);
18236
987d643c
TT
18237 psyms_seen = htab_create_alloc (100, htab_hash_pointer, htab_eq_pointer,
18238 NULL, xcalloc, xfree);
96408a79 18239 make_cleanup_htab_delete (psyms_seen);
987d643c 18240
0a5429f6
DE
18241 /* While we're scanning CU's create a table that maps a psymtab pointer
18242 (which is what addrmap records) to its index (which is what is recorded
18243 in the index file). This will later be needed to write the address
18244 table. */
18245 cu_index_htab = htab_create_alloc (100,
18246 hash_psymtab_cu_index,
18247 eq_psymtab_cu_index,
18248 NULL, xcalloc, xfree);
96408a79 18249 make_cleanup_htab_delete (cu_index_htab);
0a5429f6
DE
18250 psymtab_cu_index_map = (struct psymtab_cu_index_map *)
18251 xmalloc (sizeof (struct psymtab_cu_index_map)
18252 * dwarf2_per_objfile->n_comp_units);
18253 make_cleanup (xfree, psymtab_cu_index_map);
18254
18255 /* The CU list is already sorted, so we don't need to do additional
1fd400ff
TT
18256 work here. Also, the debug_types entries do not appear in
18257 all_comp_units, but only in their own hash table. */
9291a0cd
TT
18258 for (i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
18259 {
3e43a32a
MS
18260 struct dwarf2_per_cu_data *per_cu
18261 = dwarf2_per_objfile->all_comp_units[i];
e254ef6a 18262 struct partial_symtab *psymtab = per_cu->v.psymtab;
9291a0cd 18263 gdb_byte val[8];
0a5429f6
DE
18264 struct psymtab_cu_index_map *map;
18265 void **slot;
9291a0cd 18266
95554aad
TT
18267 if (psymtab->user == NULL)
18268 recursively_write_psymbols (objfile, psymtab, symtab, psyms_seen, i);
9291a0cd 18269
0a5429f6
DE
18270 map = &psymtab_cu_index_map[i];
18271 map->psymtab = psymtab;
18272 map->cu_index = i;
18273 slot = htab_find_slot (cu_index_htab, map, INSERT);
18274 gdb_assert (slot != NULL);
18275 gdb_assert (*slot == NULL);
18276 *slot = map;
9291a0cd 18277
b64f50a1
JK
18278 store_unsigned_integer (val, 8, BFD_ENDIAN_LITTLE,
18279 per_cu->offset.sect_off);
9291a0cd 18280 obstack_grow (&cu_list, val, 8);
e254ef6a 18281 store_unsigned_integer (val, 8, BFD_ENDIAN_LITTLE, per_cu->length);
9291a0cd
TT
18282 obstack_grow (&cu_list, val, 8);
18283 }
18284
0a5429f6
DE
18285 /* Dump the address map. */
18286 write_address_map (objfile, &addr_obstack, cu_index_htab);
18287
1fd400ff
TT
18288 /* Write out the .debug_type entries, if any. */
18289 if (dwarf2_per_objfile->signatured_types)
18290 {
18291 struct signatured_type_index_data sig_data;
18292
18293 sig_data.objfile = objfile;
18294 sig_data.symtab = symtab;
18295 sig_data.types_list = &types_cu_list;
987d643c 18296 sig_data.psyms_seen = psyms_seen;
1fd400ff
TT
18297 sig_data.cu_index = dwarf2_per_objfile->n_comp_units;
18298 htab_traverse_noresize (dwarf2_per_objfile->signatured_types,
18299 write_one_signatured_type, &sig_data);
18300 }
18301
156942c7
DE
18302 /* Now that we've processed all symbols we can shrink their cu_indices
18303 lists. */
18304 uniquify_cu_indices (symtab);
18305
9291a0cd
TT
18306 obstack_init (&constant_pool);
18307 make_cleanup_obstack_free (&constant_pool);
18308 obstack_init (&symtab_obstack);
18309 make_cleanup_obstack_free (&symtab_obstack);
18310 write_hash_table (symtab, &symtab_obstack, &constant_pool);
18311
18312 obstack_init (&contents);
18313 make_cleanup_obstack_free (&contents);
1fd400ff 18314 size_of_contents = 6 * sizeof (offset_type);
9291a0cd
TT
18315 total_len = size_of_contents;
18316
18317 /* The version number. */
156942c7 18318 val = MAYBE_SWAP (7);
9291a0cd
TT
18319 obstack_grow (&contents, &val, sizeof (val));
18320
18321 /* The offset of the CU list from the start of the file. */
18322 val = MAYBE_SWAP (total_len);
18323 obstack_grow (&contents, &val, sizeof (val));
18324 total_len += obstack_object_size (&cu_list);
18325
1fd400ff
TT
18326 /* The offset of the types CU list from the start of the file. */
18327 val = MAYBE_SWAP (total_len);
18328 obstack_grow (&contents, &val, sizeof (val));
18329 total_len += obstack_object_size (&types_cu_list);
18330
9291a0cd
TT
18331 /* The offset of the address table from the start of the file. */
18332 val = MAYBE_SWAP (total_len);
18333 obstack_grow (&contents, &val, sizeof (val));
18334 total_len += obstack_object_size (&addr_obstack);
18335
18336 /* The offset of the symbol table from the start of the file. */
18337 val = MAYBE_SWAP (total_len);
18338 obstack_grow (&contents, &val, sizeof (val));
18339 total_len += obstack_object_size (&symtab_obstack);
18340
18341 /* The offset of the constant pool from the start of the file. */
18342 val = MAYBE_SWAP (total_len);
18343 obstack_grow (&contents, &val, sizeof (val));
18344 total_len += obstack_object_size (&constant_pool);
18345
18346 gdb_assert (obstack_object_size (&contents) == size_of_contents);
18347
18348 write_obstack (out_file, &contents);
18349 write_obstack (out_file, &cu_list);
1fd400ff 18350 write_obstack (out_file, &types_cu_list);
9291a0cd
TT
18351 write_obstack (out_file, &addr_obstack);
18352 write_obstack (out_file, &symtab_obstack);
18353 write_obstack (out_file, &constant_pool);
18354
18355 fclose (out_file);
18356
18357 /* We want to keep the file, so we set cleanup_filename to NULL
18358 here. See unlink_if_set. */
18359 cleanup_filename = NULL;
18360
18361 do_cleanups (cleanup);
18362}
18363
90476074
TT
18364/* Implementation of the `save gdb-index' command.
18365
18366 Note that the file format used by this command is documented in the
18367 GDB manual. Any changes here must be documented there. */
11570e71 18368
9291a0cd
TT
18369static void
18370save_gdb_index_command (char *arg, int from_tty)
18371{
18372 struct objfile *objfile;
18373
18374 if (!arg || !*arg)
96d19272 18375 error (_("usage: save gdb-index DIRECTORY"));
9291a0cd
TT
18376
18377 ALL_OBJFILES (objfile)
18378 {
18379 struct stat st;
18380
18381 /* If the objfile does not correspond to an actual file, skip it. */
18382 if (stat (objfile->name, &st) < 0)
18383 continue;
18384
18385 dwarf2_per_objfile = objfile_data (objfile, dwarf2_objfile_data_key);
18386 if (dwarf2_per_objfile)
18387 {
18388 volatile struct gdb_exception except;
18389
18390 TRY_CATCH (except, RETURN_MASK_ERROR)
18391 {
18392 write_psymtabs_to_index (objfile, arg);
18393 }
18394 if (except.reason < 0)
18395 exception_fprintf (gdb_stderr, except,
18396 _("Error while writing index for `%s': "),
18397 objfile->name);
18398 }
18399 }
dce234bc
PP
18400}
18401
9291a0cd
TT
18402\f
18403
9eae7c52
TT
18404int dwarf2_always_disassemble;
18405
18406static void
18407show_dwarf2_always_disassemble (struct ui_file *file, int from_tty,
18408 struct cmd_list_element *c, const char *value)
18409{
3e43a32a
MS
18410 fprintf_filtered (file,
18411 _("Whether to always disassemble "
18412 "DWARF expressions is %s.\n"),
9eae7c52
TT
18413 value);
18414}
18415
900e11f9
JK
18416static void
18417show_check_physname (struct ui_file *file, int from_tty,
18418 struct cmd_list_element *c, const char *value)
18419{
18420 fprintf_filtered (file,
18421 _("Whether to check \"physname\" is %s.\n"),
18422 value);
18423}
18424
6502dd73
DJ
18425void _initialize_dwarf2_read (void);
18426
18427void
18428_initialize_dwarf2_read (void)
18429{
96d19272
JK
18430 struct cmd_list_element *c;
18431
dce234bc 18432 dwarf2_objfile_data_key
c1bd65d0 18433 = register_objfile_data_with_cleanup (NULL, dwarf2_per_objfile_free);
ae038cb0 18434
1bedd215
AC
18435 add_prefix_cmd ("dwarf2", class_maintenance, set_dwarf2_cmd, _("\
18436Set DWARF 2 specific variables.\n\
18437Configure DWARF 2 variables such as the cache size"),
ae038cb0
DJ
18438 &set_dwarf2_cmdlist, "maintenance set dwarf2 ",
18439 0/*allow-unknown*/, &maintenance_set_cmdlist);
18440
1bedd215
AC
18441 add_prefix_cmd ("dwarf2", class_maintenance, show_dwarf2_cmd, _("\
18442Show DWARF 2 specific variables\n\
18443Show DWARF 2 variables such as the cache size"),
ae038cb0
DJ
18444 &show_dwarf2_cmdlist, "maintenance show dwarf2 ",
18445 0/*allow-unknown*/, &maintenance_show_cmdlist);
18446
18447 add_setshow_zinteger_cmd ("max-cache-age", class_obscure,
7915a72c
AC
18448 &dwarf2_max_cache_age, _("\
18449Set the upper bound on the age of cached dwarf2 compilation units."), _("\
18450Show the upper bound on the age of cached dwarf2 compilation units."), _("\
18451A higher limit means that cached compilation units will be stored\n\
18452in memory longer, and more total memory will be used. Zero disables\n\
18453caching, which can slow down startup."),
2c5b56ce 18454 NULL,
920d2a44 18455 show_dwarf2_max_cache_age,
2c5b56ce 18456 &set_dwarf2_cmdlist,
ae038cb0 18457 &show_dwarf2_cmdlist);
d97bc12b 18458
9eae7c52
TT
18459 add_setshow_boolean_cmd ("always-disassemble", class_obscure,
18460 &dwarf2_always_disassemble, _("\
18461Set whether `info address' always disassembles DWARF expressions."), _("\
18462Show whether `info address' always disassembles DWARF expressions."), _("\
18463When enabled, DWARF expressions are always printed in an assembly-like\n\
18464syntax. When disabled, expressions will be printed in a more\n\
18465conversational style, when possible."),
18466 NULL,
18467 show_dwarf2_always_disassemble,
18468 &set_dwarf2_cmdlist,
18469 &show_dwarf2_cmdlist);
18470
45cfd468
DE
18471 add_setshow_boolean_cmd ("dwarf2-read", no_class, &dwarf2_read_debug, _("\
18472Set debugging of the dwarf2 reader."), _("\
18473Show debugging of the dwarf2 reader."), _("\
18474When enabled, debugging messages are printed during dwarf2 reading\n\
18475and symtab expansion."),
18476 NULL,
18477 NULL,
18478 &setdebuglist, &showdebuglist);
18479
d97bc12b
DE
18480 add_setshow_zinteger_cmd ("dwarf2-die", no_class, &dwarf2_die_debug, _("\
18481Set debugging of the dwarf2 DIE reader."), _("\
18482Show debugging of the dwarf2 DIE reader."), _("\
18483When enabled (non-zero), DIEs are dumped after they are read in.\n\
18484The value is the maximum depth to print."),
18485 NULL,
18486 NULL,
18487 &setdebuglist, &showdebuglist);
9291a0cd 18488
900e11f9
JK
18489 add_setshow_boolean_cmd ("check-physname", no_class, &check_physname, _("\
18490Set cross-checking of \"physname\" code against demangler."), _("\
18491Show cross-checking of \"physname\" code against demangler."), _("\
18492When enabled, GDB's internal \"physname\" code is checked against\n\
18493the demangler."),
18494 NULL, show_check_physname,
18495 &setdebuglist, &showdebuglist);
18496
96d19272 18497 c = add_cmd ("gdb-index", class_files, save_gdb_index_command,
11570e71 18498 _("\
fc1a9d6e 18499Save a gdb-index file.\n\
11570e71 18500Usage: save gdb-index DIRECTORY"),
96d19272
JK
18501 &save_cmdlist);
18502 set_cmd_completer (c, filename_completer);
6502dd73 18503}
This page took 2.628048 seconds and 4 git commands to generate.