* dwarf2read.c (dwarf2_find_base_address): Move definition.
[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{
3019eac3 547 /* The type's signature. */
348e048f
DE
548 ULONGEST signature;
549
3019eac3
DE
550 /* Offset in the TU of the type's DIE, as read from the TU header.
551 If the definition lives in a DWO file, this value is unusable. */
552 cu_offset type_offset_in_tu;
553
554 /* Offset in the section of the type's DIE.
555 If the definition lives in a DWO file, this is the offset in the
556 .debug_types.dwo section.
557 The value is zero until the actual value is known.
558 Zero is otherwise not a valid section offset. */
559 sect_offset type_offset_in_section;
348e048f
DE
560
561 /* The CU(/TU) of this type. */
562 struct dwarf2_per_cu_data per_cu;
563};
564
3019eac3
DE
565/* These sections are what may appear in a "dwo" file. */
566
567struct dwo_sections
568{
569 struct dwarf2_section_info abbrev;
570 struct dwarf2_section_info info;
571 struct dwarf2_section_info line;
572 struct dwarf2_section_info loc;
09262596
DE
573 struct dwarf2_section_info macinfo;
574 struct dwarf2_section_info macro;
3019eac3
DE
575 struct dwarf2_section_info str;
576 struct dwarf2_section_info str_offsets;
577 VEC (dwarf2_section_info_def) *types;
578};
579
580/* Common bits of DWO CUs/TUs. */
581
582struct dwo_unit
583{
584 /* Backlink to the containing struct dwo_file. */
585 struct dwo_file *dwo_file;
586
587 /* The "id" that distinguishes this CU/TU.
588 .debug_info calls this "dwo_id", .debug_types calls this "signature".
589 Since signatures came first, we stick with it for consistency. */
590 ULONGEST signature;
591
592 /* The section this CU/TU lives in, in the DWO file. */
593 struct dwarf2_section_info *info_or_types_section;
594
595 /* Same as dwarf2_per_cu_data:{offset,length} but for the DWO section. */
596 sect_offset offset;
597 unsigned int length;
598
599 /* For types, offset in the type's DIE of the type defined by this TU. */
600 cu_offset type_offset_in_tu;
601};
602
603/* Data for one DWO file. */
604
605struct dwo_file
606{
607 /* The DW_AT_GNU_dwo_name attribute.
608 We don't manage space for this, it's an attribute. */
609 const char *dwo_name;
610
611 /* The bfd, when the file is open. Otherwise this is NULL. */
612 bfd *dwo_bfd;
613
614 /* Section info for this file. */
615 struct dwo_sections sections;
616
617 /* Table of CUs in the file.
618 Each element is a struct dwo_unit. */
619 htab_t cus;
620
621 /* Table of TUs in the file.
622 Each element is a struct dwo_unit. */
623 htab_t tus;
624};
625
0963b4bd
MS
626/* Struct used to pass misc. parameters to read_die_and_children, et
627 al. which are used for both .debug_info and .debug_types dies.
628 All parameters here are unchanging for the life of the call. This
dee91e82 629 struct exists to abstract away the constant parameters of die reading. */
93311388
DE
630
631struct die_reader_specs
632{
dee91e82 633 /* die_section->asection->owner. */
93311388
DE
634 bfd* abfd;
635
636 /* The CU of the DIE we are parsing. */
637 struct dwarf2_cu *cu;
638
3019eac3
DE
639 /* Non-NULL if reading a DWO file. */
640 struct dwo_file *dwo_file;
641
dee91e82 642 /* The section the die comes from.
3019eac3 643 This is either .debug_info or .debug_types, or the .dwo variants. */
dee91e82
DE
644 struct dwarf2_section_info *die_section;
645
646 /* die_section->buffer. */
647 gdb_byte *buffer;
f664829e
DE
648
649 /* The end of the buffer. */
650 const gdb_byte *buffer_end;
93311388
DE
651};
652
fd820528 653/* Type of function passed to init_cutu_and_read_dies, et.al. */
dee91e82
DE
654typedef void (die_reader_func_ftype) (const struct die_reader_specs *reader,
655 gdb_byte *info_ptr,
656 struct die_info *comp_unit_die,
657 int has_children,
658 void *data);
659
debd256d
JB
660/* The line number information for a compilation unit (found in the
661 .debug_line section) begins with a "statement program header",
662 which contains the following information. */
663struct line_header
664{
665 unsigned int total_length;
666 unsigned short version;
667 unsigned int header_length;
668 unsigned char minimum_instruction_length;
2dc7f7b3 669 unsigned char maximum_ops_per_instruction;
debd256d
JB
670 unsigned char default_is_stmt;
671 int line_base;
672 unsigned char line_range;
673 unsigned char opcode_base;
674
675 /* standard_opcode_lengths[i] is the number of operands for the
676 standard opcode whose value is i. This means that
677 standard_opcode_lengths[0] is unused, and the last meaningful
678 element is standard_opcode_lengths[opcode_base - 1]. */
679 unsigned char *standard_opcode_lengths;
680
681 /* The include_directories table. NOTE! These strings are not
682 allocated with xmalloc; instead, they are pointers into
683 debug_line_buffer. If you try to free them, `free' will get
684 indigestion. */
685 unsigned int num_include_dirs, include_dirs_size;
686 char **include_dirs;
687
688 /* The file_names table. NOTE! These strings are not allocated
689 with xmalloc; instead, they are pointers into debug_line_buffer.
690 Don't try to free them directly. */
691 unsigned int num_file_names, file_names_size;
692 struct file_entry
c906108c 693 {
debd256d
JB
694 char *name;
695 unsigned int dir_index;
696 unsigned int mod_time;
697 unsigned int length;
aaa75496 698 int included_p; /* Non-zero if referenced by the Line Number Program. */
cb1df416 699 struct symtab *symtab; /* The associated symbol table, if any. */
debd256d
JB
700 } *file_names;
701
702 /* The start and end of the statement program following this
6502dd73 703 header. These point into dwarf2_per_objfile->line_buffer. */
fe1b8b76 704 gdb_byte *statement_program_start, *statement_program_end;
debd256d 705};
c906108c
SS
706
707/* When we construct a partial symbol table entry we only
0963b4bd 708 need this much information. */
c906108c
SS
709struct partial_die_info
710 {
72bf9492 711 /* Offset of this DIE. */
b64f50a1 712 sect_offset offset;
72bf9492
DJ
713
714 /* DWARF-2 tag for this DIE. */
715 ENUM_BITFIELD(dwarf_tag) tag : 16;
716
72bf9492
DJ
717 /* Assorted flags describing the data found in this DIE. */
718 unsigned int has_children : 1;
719 unsigned int is_external : 1;
720 unsigned int is_declaration : 1;
721 unsigned int has_type : 1;
722 unsigned int has_specification : 1;
723 unsigned int has_pc_info : 1;
481860b3 724 unsigned int may_be_inlined : 1;
72bf9492
DJ
725
726 /* Flag set if the SCOPE field of this structure has been
727 computed. */
728 unsigned int scope_set : 1;
729
fa4028e9
JB
730 /* Flag set if the DIE has a byte_size attribute. */
731 unsigned int has_byte_size : 1;
732
98bfdba5
PA
733 /* Flag set if any of the DIE's children are template arguments. */
734 unsigned int has_template_arguments : 1;
735
abc72ce4
DE
736 /* Flag set if fixup_partial_die has been called on this die. */
737 unsigned int fixup_called : 1;
738
72bf9492 739 /* The name of this DIE. Normally the value of DW_AT_name, but
94af9270 740 sometimes a default name for unnamed DIEs. */
c906108c 741 char *name;
72bf9492 742
abc72ce4
DE
743 /* The linkage name, if present. */
744 const char *linkage_name;
745
72bf9492
DJ
746 /* The scope to prepend to our children. This is generally
747 allocated on the comp_unit_obstack, so will disappear
748 when this compilation unit leaves the cache. */
749 char *scope;
750
95554aad
TT
751 /* Some data associated with the partial DIE. The tag determines
752 which field is live. */
753 union
754 {
755 /* The location description associated with this DIE, if any. */
756 struct dwarf_block *locdesc;
757 /* The offset of an import, for DW_TAG_imported_unit. */
758 sect_offset offset;
759 } d;
72bf9492
DJ
760
761 /* If HAS_PC_INFO, the PC range associated with this DIE. */
c906108c
SS
762 CORE_ADDR lowpc;
763 CORE_ADDR highpc;
72bf9492 764
93311388 765 /* Pointer into the info_buffer (or types_buffer) pointing at the target of
72bf9492 766 DW_AT_sibling, if any. */
abc72ce4
DE
767 /* NOTE: This member isn't strictly necessary, read_partial_die could
768 return DW_AT_sibling values to its caller load_partial_dies. */
fe1b8b76 769 gdb_byte *sibling;
72bf9492
DJ
770
771 /* If HAS_SPECIFICATION, the offset of the DIE referred to by
772 DW_AT_specification (or DW_AT_abstract_origin or
773 DW_AT_extension). */
b64f50a1 774 sect_offset spec_offset;
72bf9492
DJ
775
776 /* Pointers to this DIE's parent, first child, and next sibling,
777 if any. */
778 struct partial_die_info *die_parent, *die_child, *die_sibling;
c906108c
SS
779 };
780
0963b4bd 781/* This data structure holds the information of an abbrev. */
c906108c
SS
782struct abbrev_info
783 {
784 unsigned int number; /* number identifying abbrev */
785 enum dwarf_tag tag; /* dwarf tag */
f3dd6933
DJ
786 unsigned short has_children; /* boolean */
787 unsigned short num_attrs; /* number of attributes */
c906108c
SS
788 struct attr_abbrev *attrs; /* an array of attribute descriptions */
789 struct abbrev_info *next; /* next in chain */
790 };
791
792struct attr_abbrev
793 {
9d25dd43
DE
794 ENUM_BITFIELD(dwarf_attribute) name : 16;
795 ENUM_BITFIELD(dwarf_form) form : 16;
c906108c
SS
796 };
797
433df2d4
DE
798/* Size of abbrev_table.abbrev_hash_table. */
799#define ABBREV_HASH_SIZE 121
800
801/* Top level data structure to contain an abbreviation table. */
802
803struct abbrev_table
804{
805 /* Where the abbrev table came from. */
806 struct dwarf2_section_info *section;
807 sect_offset offset;
808
809 /* Storage for the abbrev table. */
810 struct obstack abbrev_obstack;
811
812 /* Hash table of abbrevs.
813 This is an array of size ABBREV_HASH_SIZE allocated in abbrev_obstack.
814 It could be statically allocated, but the previous code didn't so we
815 don't either. */
816 struct abbrev_info **abbrevs;
817};
818
0963b4bd 819/* Attributes have a name and a value. */
b60c80d6
DJ
820struct attribute
821 {
9d25dd43 822 ENUM_BITFIELD(dwarf_attribute) name : 16;
8285870a
JK
823 ENUM_BITFIELD(dwarf_form) form : 15;
824
825 /* Has DW_STRING already been updated by dwarf2_canonicalize_name? This
826 field should be in u.str (existing only for DW_STRING) but it is kept
827 here for better struct attribute alignment. */
828 unsigned int string_is_canonical : 1;
829
b60c80d6
DJ
830 union
831 {
832 char *str;
833 struct dwarf_block *blk;
43bbcdc2
PH
834 ULONGEST unsnd;
835 LONGEST snd;
b60c80d6 836 CORE_ADDR addr;
348e048f 837 struct signatured_type *signatured_type;
b60c80d6
DJ
838 }
839 u;
840 };
841
0963b4bd 842/* This data structure holds a complete die structure. */
c906108c
SS
843struct die_info
844 {
76815b17
DE
845 /* DWARF-2 tag for this DIE. */
846 ENUM_BITFIELD(dwarf_tag) tag : 16;
847
848 /* Number of attributes */
98bfdba5
PA
849 unsigned char num_attrs;
850
851 /* True if we're presently building the full type name for the
852 type derived from this DIE. */
853 unsigned char building_fullname : 1;
76815b17
DE
854
855 /* Abbrev number */
856 unsigned int abbrev;
857
93311388 858 /* Offset in .debug_info or .debug_types section. */
b64f50a1 859 sect_offset offset;
78ba4af6
JB
860
861 /* The dies in a compilation unit form an n-ary tree. PARENT
862 points to this die's parent; CHILD points to the first child of
863 this node; and all the children of a given node are chained
4950bc1c 864 together via their SIBLING fields. */
639d11d3
DC
865 struct die_info *child; /* Its first child, if any. */
866 struct die_info *sibling; /* Its next sibling, if any. */
867 struct die_info *parent; /* Its parent, if any. */
c906108c 868
b60c80d6
DJ
869 /* An array of attributes, with NUM_ATTRS elements. There may be
870 zero, but it's not common and zero-sized arrays are not
871 sufficiently portable C. */
872 struct attribute attrs[1];
c906108c
SS
873 };
874
0963b4bd 875/* Get at parts of an attribute structure. */
c906108c
SS
876
877#define DW_STRING(attr) ((attr)->u.str)
8285870a 878#define DW_STRING_IS_CANONICAL(attr) ((attr)->string_is_canonical)
c906108c
SS
879#define DW_UNSND(attr) ((attr)->u.unsnd)
880#define DW_BLOCK(attr) ((attr)->u.blk)
881#define DW_SND(attr) ((attr)->u.snd)
882#define DW_ADDR(attr) ((attr)->u.addr)
348e048f 883#define DW_SIGNATURED_TYPE(attr) ((attr)->u.signatured_type)
c906108c 884
0963b4bd 885/* Blocks are a bunch of untyped bytes. */
c906108c
SS
886struct dwarf_block
887 {
888 unsigned int size;
1d6edc3c
JK
889
890 /* Valid only if SIZE is not zero. */
fe1b8b76 891 gdb_byte *data;
c906108c
SS
892 };
893
c906108c
SS
894#ifndef ATTR_ALLOC_CHUNK
895#define ATTR_ALLOC_CHUNK 4
896#endif
897
c906108c
SS
898/* Allocate fields for structs, unions and enums in this size. */
899#ifndef DW_FIELD_ALLOC_CHUNK
900#define DW_FIELD_ALLOC_CHUNK 4
901#endif
902
c906108c
SS
903/* FIXME: We might want to set this from BFD via bfd_arch_bits_per_byte,
904 but this would require a corresponding change in unpack_field_as_long
905 and friends. */
906static int bits_per_byte = 8;
907
908/* The routines that read and process dies for a C struct or C++ class
909 pass lists of data member fields and lists of member function fields
910 in an instance of a field_info structure, as defined below. */
911struct field_info
c5aa993b 912 {
0963b4bd 913 /* List of data member and baseclasses fields. */
c5aa993b
JM
914 struct nextfield
915 {
916 struct nextfield *next;
917 int accessibility;
918 int virtuality;
919 struct field field;
920 }
7d0ccb61 921 *fields, *baseclasses;
c906108c 922
7d0ccb61 923 /* Number of fields (including baseclasses). */
c5aa993b 924 int nfields;
c906108c 925
c5aa993b
JM
926 /* Number of baseclasses. */
927 int nbaseclasses;
c906108c 928
c5aa993b
JM
929 /* Set if the accesibility of one of the fields is not public. */
930 int non_public_fields;
c906108c 931
c5aa993b
JM
932 /* Member function fields array, entries are allocated in the order they
933 are encountered in the object file. */
934 struct nextfnfield
935 {
936 struct nextfnfield *next;
937 struct fn_field fnfield;
938 }
939 *fnfields;
c906108c 940
c5aa993b
JM
941 /* Member function fieldlist array, contains name of possibly overloaded
942 member function, number of overloaded member functions and a pointer
943 to the head of the member function field chain. */
944 struct fnfieldlist
945 {
946 char *name;
947 int length;
948 struct nextfnfield *head;
949 }
950 *fnfieldlists;
c906108c 951
c5aa993b
JM
952 /* Number of entries in the fnfieldlists array. */
953 int nfnfields;
98751a41
JK
954
955 /* typedefs defined inside this class. TYPEDEF_FIELD_LIST contains head of
956 a NULL terminated list of TYPEDEF_FIELD_LIST_COUNT elements. */
957 struct typedef_field_list
958 {
959 struct typedef_field field;
960 struct typedef_field_list *next;
961 }
962 *typedef_field_list;
963 unsigned typedef_field_list_count;
c5aa993b 964 };
c906108c 965
10b3939b
DJ
966/* One item on the queue of compilation units to read in full symbols
967 for. */
968struct dwarf2_queue_item
969{
970 struct dwarf2_per_cu_data *per_cu;
95554aad 971 enum language pretend_language;
10b3939b
DJ
972 struct dwarf2_queue_item *next;
973};
974
975/* The current queue. */
976static struct dwarf2_queue_item *dwarf2_queue, *dwarf2_queue_tail;
977
ae038cb0
DJ
978/* Loaded secondary compilation units are kept in memory until they
979 have not been referenced for the processing of this many
980 compilation units. Set this to zero to disable caching. Cache
981 sizes of up to at least twenty will improve startup time for
982 typical inter-CU-reference binaries, at an obvious memory cost. */
983static int dwarf2_max_cache_age = 5;
920d2a44
AC
984static void
985show_dwarf2_max_cache_age (struct ui_file *file, int from_tty,
986 struct cmd_list_element *c, const char *value)
987{
3e43a32a
MS
988 fprintf_filtered (file, _("The upper bound on the age of cached "
989 "dwarf2 compilation units is %s.\n"),
920d2a44
AC
990 value);
991}
992
ae038cb0 993
0963b4bd 994/* Various complaints about symbol reading that don't abort the process. */
c906108c 995
4d3c2250
KB
996static void
997dwarf2_statement_list_fits_in_line_number_section_complaint (void)
2e276125 998{
4d3c2250 999 complaint (&symfile_complaints,
e2e0b3e5 1000 _("statement list doesn't fit in .debug_line section"));
4d3c2250
KB
1001}
1002
25e43795
DJ
1003static void
1004dwarf2_debug_line_missing_file_complaint (void)
1005{
1006 complaint (&symfile_complaints,
1007 _(".debug_line section has line data without a file"));
1008}
1009
59205f5a
JB
1010static void
1011dwarf2_debug_line_missing_end_sequence_complaint (void)
1012{
1013 complaint (&symfile_complaints,
3e43a32a
MS
1014 _(".debug_line section has line "
1015 "program sequence without an end"));
59205f5a
JB
1016}
1017
4d3c2250
KB
1018static void
1019dwarf2_complex_location_expr_complaint (void)
2e276125 1020{
e2e0b3e5 1021 complaint (&symfile_complaints, _("location expression too complex"));
4d3c2250
KB
1022}
1023
4d3c2250
KB
1024static void
1025dwarf2_const_value_length_mismatch_complaint (const char *arg1, int arg2,
1026 int arg3)
2e276125 1027{
4d3c2250 1028 complaint (&symfile_complaints,
3e43a32a
MS
1029 _("const value length mismatch for '%s', got %d, expected %d"),
1030 arg1, arg2, arg3);
4d3c2250
KB
1031}
1032
1033static void
f664829e 1034dwarf2_section_buffer_overflow_complaint (struct dwarf2_section_info *section)
2e276125 1035{
4d3c2250 1036 complaint (&symfile_complaints,
f664829e
DE
1037 _("debug info runs off end of %s section"
1038 " [in module %s]"),
1039 section->asection->name,
1040 bfd_get_filename (section->asection->owner));
4d3c2250
KB
1041}
1042
1043static void
1044dwarf2_macro_malformed_definition_complaint (const char *arg1)
8e19ed76 1045{
4d3c2250 1046 complaint (&symfile_complaints,
3e43a32a
MS
1047 _("macro debug info contains a "
1048 "malformed macro definition:\n`%s'"),
4d3c2250
KB
1049 arg1);
1050}
1051
1052static void
1053dwarf2_invalid_attrib_class_complaint (const char *arg1, const char *arg2)
8b2dbe47 1054{
4d3c2250 1055 complaint (&symfile_complaints,
3e43a32a
MS
1056 _("invalid attribute class or form for '%s' in '%s'"),
1057 arg1, arg2);
4d3c2250 1058}
c906108c 1059
c906108c
SS
1060/* local function prototypes */
1061
4efb68b1 1062static void dwarf2_locate_sections (bfd *, asection *, void *);
c906108c 1063
aaa75496
JB
1064static void dwarf2_create_include_psymtab (char *, struct partial_symtab *,
1065 struct objfile *);
1066
918dd910
JK
1067static void dwarf2_find_base_address (struct die_info *die,
1068 struct dwarf2_cu *cu);
1069
c67a9c90 1070static void dwarf2_build_psymtabs_hard (struct objfile *);
c906108c 1071
72bf9492
DJ
1072static void scan_partial_symbols (struct partial_die_info *,
1073 CORE_ADDR *, CORE_ADDR *,
5734ee8b 1074 int, struct dwarf2_cu *);
c906108c 1075
72bf9492
DJ
1076static void add_partial_symbol (struct partial_die_info *,
1077 struct dwarf2_cu *);
63d06c5c 1078
72bf9492
DJ
1079static void add_partial_namespace (struct partial_die_info *pdi,
1080 CORE_ADDR *lowpc, CORE_ADDR *highpc,
5734ee8b 1081 int need_pc, struct dwarf2_cu *cu);
63d06c5c 1082
5d7cb8df
JK
1083static void add_partial_module (struct partial_die_info *pdi, CORE_ADDR *lowpc,
1084 CORE_ADDR *highpc, int need_pc,
1085 struct dwarf2_cu *cu);
1086
72bf9492
DJ
1087static void add_partial_enumeration (struct partial_die_info *enum_pdi,
1088 struct dwarf2_cu *cu);
91c24f0a 1089
bc30ff58
JB
1090static void add_partial_subprogram (struct partial_die_info *pdi,
1091 CORE_ADDR *lowpc, CORE_ADDR *highpc,
5734ee8b 1092 int need_pc, struct dwarf2_cu *cu);
bc30ff58 1093
a14ed312 1094static void dwarf2_psymtab_to_symtab (struct partial_symtab *);
c906108c 1095
a14ed312 1096static void psymtab_to_symtab_1 (struct partial_symtab *);
c906108c 1097
433df2d4
DE
1098static struct abbrev_info *abbrev_table_lookup_abbrev
1099 (const struct abbrev_table *, unsigned int);
1100
1101static struct abbrev_table *abbrev_table_read_table
1102 (struct dwarf2_section_info *, sect_offset);
1103
1104static void abbrev_table_free (struct abbrev_table *);
1105
dee91e82
DE
1106static void dwarf2_read_abbrevs (struct dwarf2_cu *,
1107 struct dwarf2_section_info *);
c906108c 1108
f3dd6933 1109static void dwarf2_free_abbrev_table (void *);
c906108c 1110
6caca83c
CC
1111static unsigned int peek_abbrev_code (bfd *, gdb_byte *);
1112
dee91e82
DE
1113static struct partial_die_info *load_partial_dies
1114 (const struct die_reader_specs *, gdb_byte *, int);
72bf9492 1115
dee91e82
DE
1116static gdb_byte *read_partial_die (const struct die_reader_specs *,
1117 struct partial_die_info *,
1118 struct abbrev_info *,
1119 unsigned int,
1120 gdb_byte *);
c906108c 1121
b64f50a1 1122static struct partial_die_info *find_partial_die (sect_offset,
10b3939b 1123 struct dwarf2_cu *);
72bf9492
DJ
1124
1125static void fixup_partial_die (struct partial_die_info *,
1126 struct dwarf2_cu *);
1127
dee91e82
DE
1128static gdb_byte *read_attribute (const struct die_reader_specs *,
1129 struct attribute *, struct attr_abbrev *,
1130 gdb_byte *);
a8329558 1131
fe1b8b76 1132static unsigned int read_1_byte (bfd *, gdb_byte *);
c906108c 1133
fe1b8b76 1134static int read_1_signed_byte (bfd *, gdb_byte *);
c906108c 1135
fe1b8b76 1136static unsigned int read_2_bytes (bfd *, gdb_byte *);
c906108c 1137
fe1b8b76 1138static unsigned int read_4_bytes (bfd *, gdb_byte *);
c906108c 1139
93311388 1140static ULONGEST read_8_bytes (bfd *, gdb_byte *);
c906108c 1141
fe1b8b76 1142static CORE_ADDR read_address (bfd *, gdb_byte *ptr, struct dwarf2_cu *,
891d2f0b 1143 unsigned int *);
c906108c 1144
c764a876
DE
1145static LONGEST read_initial_length (bfd *, gdb_byte *, unsigned int *);
1146
1147static LONGEST read_checked_initial_length_and_offset
1148 (bfd *, gdb_byte *, const struct comp_unit_head *,
1149 unsigned int *, unsigned int *);
613e1657 1150
fe1b8b76 1151static LONGEST read_offset (bfd *, gdb_byte *, const struct comp_unit_head *,
c764a876
DE
1152 unsigned int *);
1153
1154static LONGEST read_offset_1 (bfd *, gdb_byte *, unsigned int);
613e1657 1155
fe1b8b76 1156static gdb_byte *read_n_bytes (bfd *, gdb_byte *, unsigned int);
c906108c 1157
9b1c24c8 1158static char *read_direct_string (bfd *, gdb_byte *, unsigned int *);
c906108c 1159
fe1b8b76
JB
1160static char *read_indirect_string (bfd *, gdb_byte *,
1161 const struct comp_unit_head *,
1162 unsigned int *);
4bdf3d34 1163
12df843f 1164static ULONGEST read_unsigned_leb128 (bfd *, gdb_byte *, unsigned int *);
c906108c 1165
12df843f 1166static LONGEST read_signed_leb128 (bfd *, gdb_byte *, unsigned int *);
c906108c 1167
3019eac3
DE
1168static CORE_ADDR read_addr_index_from_leb128 (struct dwarf2_cu *, gdb_byte *,
1169 unsigned int *);
1170
1171static char *read_str_index (const struct die_reader_specs *reader,
1172 struct dwarf2_cu *cu, ULONGEST str_index);
1173
e142c38c 1174static void set_cu_language (unsigned int, struct dwarf2_cu *);
c906108c 1175
e142c38c
DJ
1176static struct attribute *dwarf2_attr (struct die_info *, unsigned int,
1177 struct dwarf2_cu *);
c906108c 1178
348e048f
DE
1179static struct attribute *dwarf2_attr_no_follow (struct die_info *,
1180 unsigned int,
1181 struct dwarf2_cu *);
1182
05cf31d1
JB
1183static int dwarf2_flag_true_p (struct die_info *die, unsigned name,
1184 struct dwarf2_cu *cu);
1185
e142c38c 1186static int die_is_declaration (struct die_info *, struct dwarf2_cu *cu);
3ca72b44 1187
e142c38c 1188static struct die_info *die_specification (struct die_info *die,
f2f0e013 1189 struct dwarf2_cu **);
63d06c5c 1190
debd256d
JB
1191static void free_line_header (struct line_header *lh);
1192
aaa75496
JB
1193static void add_file_name (struct line_header *, char *, unsigned int,
1194 unsigned int, unsigned int);
1195
3019eac3
DE
1196static struct line_header *dwarf_decode_line_header (unsigned int offset,
1197 struct dwarf2_cu *cu);
debd256d 1198
f3f5162e
DE
1199static void dwarf_decode_lines (struct line_header *, const char *,
1200 struct dwarf2_cu *, struct partial_symtab *,
1201 int);
c906108c 1202
72b9f47f 1203static void dwarf2_start_subfile (char *, const char *, const char *);
c906108c 1204
a14ed312 1205static struct symbol *new_symbol (struct die_info *, struct type *,
e7c27a73 1206 struct dwarf2_cu *);
c906108c 1207
34eaf542
TT
1208static struct symbol *new_symbol_full (struct die_info *, struct type *,
1209 struct dwarf2_cu *, struct symbol *);
1210
a14ed312 1211static void dwarf2_const_value (struct attribute *, struct symbol *,
e7c27a73 1212 struct dwarf2_cu *);
c906108c 1213
98bfdba5
PA
1214static void dwarf2_const_value_attr (struct attribute *attr,
1215 struct type *type,
1216 const char *name,
1217 struct obstack *obstack,
12df843f 1218 struct dwarf2_cu *cu, LONGEST *value,
98bfdba5
PA
1219 gdb_byte **bytes,
1220 struct dwarf2_locexpr_baton **baton);
2df3850c 1221
e7c27a73 1222static struct type *die_type (struct die_info *, struct dwarf2_cu *);
c906108c 1223
b4ba55a1
JB
1224static int need_gnat_info (struct dwarf2_cu *);
1225
3e43a32a
MS
1226static struct type *die_descriptive_type (struct die_info *,
1227 struct dwarf2_cu *);
b4ba55a1
JB
1228
1229static void set_descriptive_type (struct type *, struct die_info *,
1230 struct dwarf2_cu *);
1231
e7c27a73
DJ
1232static struct type *die_containing_type (struct die_info *,
1233 struct dwarf2_cu *);
c906108c 1234
673bfd45
DE
1235static struct type *lookup_die_type (struct die_info *, struct attribute *,
1236 struct dwarf2_cu *);
c906108c 1237
f792889a 1238static struct type *read_type_die (struct die_info *, struct dwarf2_cu *);
c906108c 1239
673bfd45
DE
1240static struct type *read_type_die_1 (struct die_info *, struct dwarf2_cu *);
1241
0d5cff50 1242static const char *determine_prefix (struct die_info *die, struct dwarf2_cu *);
63d06c5c 1243
6e70227d 1244static char *typename_concat (struct obstack *obs, const char *prefix,
f55ee35c
JK
1245 const char *suffix, int physname,
1246 struct dwarf2_cu *cu);
63d06c5c 1247
e7c27a73 1248static void read_file_scope (struct die_info *, struct dwarf2_cu *);
c906108c 1249
348e048f
DE
1250static void read_type_unit_scope (struct die_info *, struct dwarf2_cu *);
1251
e7c27a73 1252static void read_func_scope (struct die_info *, struct dwarf2_cu *);
c906108c 1253
e7c27a73 1254static void read_lexical_block_scope (struct die_info *, struct dwarf2_cu *);
c906108c 1255
96408a79
SA
1256static void read_call_site_scope (struct die_info *die, struct dwarf2_cu *cu);
1257
ff013f42
JK
1258static int dwarf2_ranges_read (unsigned, CORE_ADDR *, CORE_ADDR *,
1259 struct dwarf2_cu *, struct partial_symtab *);
1260
a14ed312 1261static int dwarf2_get_pc_bounds (struct die_info *,
d85a05f0
DJ
1262 CORE_ADDR *, CORE_ADDR *, struct dwarf2_cu *,
1263 struct partial_symtab *);
c906108c 1264
fae299cd
DC
1265static void get_scope_pc_bounds (struct die_info *,
1266 CORE_ADDR *, CORE_ADDR *,
1267 struct dwarf2_cu *);
1268
801e3a5b
JB
1269static void dwarf2_record_block_ranges (struct die_info *, struct block *,
1270 CORE_ADDR, struct dwarf2_cu *);
1271
a14ed312 1272static void dwarf2_add_field (struct field_info *, struct die_info *,
e7c27a73 1273 struct dwarf2_cu *);
c906108c 1274
a14ed312 1275static void dwarf2_attach_fields_to_type (struct field_info *,
e7c27a73 1276 struct type *, struct dwarf2_cu *);
c906108c 1277
a14ed312 1278static void dwarf2_add_member_fn (struct field_info *,
e26fb1d7 1279 struct die_info *, struct type *,
e7c27a73 1280 struct dwarf2_cu *);
c906108c 1281
a14ed312 1282static void dwarf2_attach_fn_fields_to_type (struct field_info *,
3e43a32a
MS
1283 struct type *,
1284 struct dwarf2_cu *);
c906108c 1285
134d01f1 1286static void process_structure_scope (struct die_info *, struct dwarf2_cu *);
c906108c 1287
e7c27a73 1288static void read_common_block (struct die_info *, struct dwarf2_cu *);
c906108c 1289
e7c27a73 1290static void read_namespace (struct die_info *die, struct dwarf2_cu *);
d9fa45fe 1291
5d7cb8df
JK
1292static void read_module (struct die_info *die, struct dwarf2_cu *cu);
1293
27aa8d6a
SW
1294static void read_import_statement (struct die_info *die, struct dwarf2_cu *);
1295
f55ee35c
JK
1296static struct type *read_module_type (struct die_info *die,
1297 struct dwarf2_cu *cu);
1298
38d518c9 1299static const char *namespace_name (struct die_info *die,
e142c38c 1300 int *is_anonymous, struct dwarf2_cu *);
38d518c9 1301
134d01f1 1302static void process_enumeration_scope (struct die_info *, struct dwarf2_cu *);
c906108c 1303
e7c27a73 1304static CORE_ADDR decode_locdesc (struct dwarf_block *, struct dwarf2_cu *);
c906108c 1305
6e70227d 1306static enum dwarf_array_dim_ordering read_array_order (struct die_info *,
7ca2d3a3
DL
1307 struct dwarf2_cu *);
1308
dee91e82 1309static struct die_info *read_die_and_children (const struct die_reader_specs *,
93311388 1310 gdb_byte *info_ptr,
fe1b8b76 1311 gdb_byte **new_info_ptr,
639d11d3
DC
1312 struct die_info *parent);
1313
dee91e82 1314static struct die_info *read_die_and_siblings (const struct die_reader_specs *,
93311388 1315 gdb_byte *info_ptr,
fe1b8b76 1316 gdb_byte **new_info_ptr,
639d11d3
DC
1317 struct die_info *parent);
1318
3019eac3
DE
1319static gdb_byte *read_full_die_1 (const struct die_reader_specs *,
1320 struct die_info **, gdb_byte *, int *, int);
1321
dee91e82
DE
1322static gdb_byte *read_full_die (const struct die_reader_specs *,
1323 struct die_info **, gdb_byte *, int *);
93311388 1324
e7c27a73 1325static void process_die (struct die_info *, struct dwarf2_cu *);
c906108c 1326
71c25dea
TT
1327static char *dwarf2_canonicalize_name (char *, struct dwarf2_cu *,
1328 struct obstack *);
1329
e142c38c 1330static char *dwarf2_name (struct die_info *die, struct dwarf2_cu *);
9219021c 1331
98bfdba5
PA
1332static const char *dwarf2_full_name (char *name,
1333 struct die_info *die,
1334 struct dwarf2_cu *cu);
1335
e142c38c 1336static struct die_info *dwarf2_extension (struct die_info *die,
f2f0e013 1337 struct dwarf2_cu **);
9219021c 1338
f39c6ffd 1339static const char *dwarf_tag_name (unsigned int);
c906108c 1340
f39c6ffd 1341static const char *dwarf_attr_name (unsigned int);
c906108c 1342
f39c6ffd 1343static const char *dwarf_form_name (unsigned int);
c906108c 1344
a14ed312 1345static char *dwarf_bool_name (unsigned int);
c906108c 1346
f39c6ffd 1347static const char *dwarf_type_encoding_name (unsigned int);
c906108c 1348
f9aca02d 1349static struct die_info *sibling_die (struct die_info *);
c906108c 1350
d97bc12b
DE
1351static void dump_die_shallow (struct ui_file *, int indent, struct die_info *);
1352
1353static void dump_die_for_error (struct die_info *);
1354
1355static void dump_die_1 (struct ui_file *, int level, int max_level,
1356 struct die_info *);
c906108c 1357
d97bc12b 1358/*static*/ void dump_die (struct die_info *, int max_level);
c906108c 1359
51545339 1360static void store_in_ref_table (struct die_info *,
10b3939b 1361 struct dwarf2_cu *);
c906108c 1362
93311388
DE
1363static int is_ref_attr (struct attribute *);
1364
b64f50a1 1365static sect_offset dwarf2_get_ref_die_offset (struct attribute *);
c906108c 1366
43bbcdc2 1367static LONGEST dwarf2_get_attr_constant_value (struct attribute *, int);
a02abb62 1368
348e048f
DE
1369static struct die_info *follow_die_ref_or_sig (struct die_info *,
1370 struct attribute *,
1371 struct dwarf2_cu **);
1372
10b3939b
DJ
1373static struct die_info *follow_die_ref (struct die_info *,
1374 struct attribute *,
f2f0e013 1375 struct dwarf2_cu **);
c906108c 1376
348e048f
DE
1377static struct die_info *follow_die_sig (struct die_info *,
1378 struct attribute *,
1379 struct dwarf2_cu **);
1380
6c83ed52
TT
1381static struct signatured_type *lookup_signatured_type_at_offset
1382 (struct objfile *objfile,
b64f50a1 1383 struct dwarf2_section_info *section, sect_offset offset);
6c83ed52 1384
e5fe5e75 1385static void load_full_type_unit (struct dwarf2_per_cu_data *per_cu);
348e048f 1386
52dc124a 1387static void read_signatured_type (struct signatured_type *);
348e048f 1388
c906108c
SS
1389/* memory allocation interface */
1390
7b5a2f43 1391static struct dwarf_block *dwarf_alloc_block (struct dwarf2_cu *);
c906108c 1392
b60c80d6 1393static struct die_info *dwarf_alloc_die (struct dwarf2_cu *, int);
c906108c 1394
09262596
DE
1395static void dwarf_decode_macros (struct dwarf2_cu *, unsigned int,
1396 char *, int);
2e276125 1397
8e19ed76
PS
1398static int attr_form_is_block (struct attribute *);
1399
3690dd37
JB
1400static int attr_form_is_section_offset (struct attribute *);
1401
1402static int attr_form_is_constant (struct attribute *);
1403
8cf6f0b1
TT
1404static void fill_in_loclist_baton (struct dwarf2_cu *cu,
1405 struct dwarf2_loclist_baton *baton,
1406 struct attribute *attr);
1407
93e7bd98
DJ
1408static void dwarf2_symbol_mark_computed (struct attribute *attr,
1409 struct symbol *sym,
1410 struct dwarf2_cu *cu);
4c2df51b 1411
dee91e82
DE
1412static gdb_byte *skip_one_die (const struct die_reader_specs *reader,
1413 gdb_byte *info_ptr,
1414 struct abbrev_info *abbrev);
4bb7a0a7 1415
72bf9492
DJ
1416static void free_stack_comp_unit (void *);
1417
72bf9492
DJ
1418static hashval_t partial_die_hash (const void *item);
1419
1420static int partial_die_eq (const void *item_lhs, const void *item_rhs);
1421
ae038cb0 1422static struct dwarf2_per_cu_data *dwarf2_find_containing_comp_unit
b64f50a1 1423 (sect_offset offset, struct objfile *objfile);
ae038cb0 1424
9816fde3 1425static void init_one_comp_unit (struct dwarf2_cu *cu,
23745b47 1426 struct dwarf2_per_cu_data *per_cu);
9816fde3
JK
1427
1428static void prepare_one_comp_unit (struct dwarf2_cu *cu,
95554aad
TT
1429 struct die_info *comp_unit_die,
1430 enum language pretend_language);
93311388 1431
68dc6402 1432static void free_heap_comp_unit (void *);
ae038cb0
DJ
1433
1434static void free_cached_comp_units (void *);
1435
1436static void age_cached_comp_units (void);
1437
dee91e82 1438static void free_one_cached_comp_unit (struct dwarf2_per_cu_data *);
ae038cb0 1439
f792889a
DJ
1440static struct type *set_die_type (struct die_info *, struct type *,
1441 struct dwarf2_cu *);
1c379e20 1442
ae038cb0
DJ
1443static void create_all_comp_units (struct objfile *);
1444
0e50663e 1445static int create_all_type_units (struct objfile *);
1fd400ff 1446
95554aad
TT
1447static void load_full_comp_unit (struct dwarf2_per_cu_data *,
1448 enum language);
10b3939b 1449
95554aad
TT
1450static void process_full_comp_unit (struct dwarf2_per_cu_data *,
1451 enum language);
10b3939b
DJ
1452
1453static void dwarf2_add_dependence (struct dwarf2_cu *,
1454 struct dwarf2_per_cu_data *);
1455
ae038cb0
DJ
1456static void dwarf2_mark (struct dwarf2_cu *);
1457
1458static void dwarf2_clear_marks (struct dwarf2_per_cu_data *);
1459
b64f50a1 1460static struct type *get_die_type_at_offset (sect_offset,
673bfd45
DE
1461 struct dwarf2_per_cu_data *per_cu);
1462
f792889a 1463static struct type *get_die_type (struct die_info *die, struct dwarf2_cu *cu);
72019c9c 1464
9291a0cd
TT
1465static void dwarf2_release_queue (void *dummy);
1466
95554aad
TT
1467static void queue_comp_unit (struct dwarf2_per_cu_data *per_cu,
1468 enum language pretend_language);
1469
1470static int maybe_queue_comp_unit (struct dwarf2_cu *this_cu,
1471 struct dwarf2_per_cu_data *per_cu,
1472 enum language pretend_language);
9291a0cd 1473
a0f42c21 1474static void process_queue (void);
9291a0cd
TT
1475
1476static void find_file_and_directory (struct die_info *die,
1477 struct dwarf2_cu *cu,
1478 char **name, char **comp_dir);
1479
1480static char *file_full_name (int file, struct line_header *lh,
1481 const char *comp_dir);
1482
fd820528
DE
1483static void init_cutu_and_read_dies
1484 (struct dwarf2_per_cu_data *this_cu, int use_existing_cu, int keep,
3019eac3
DE
1485 die_reader_func_ftype *die_reader_func, void *data);
1486
dee91e82
DE
1487static void init_cutu_and_read_dies_simple
1488 (struct dwarf2_per_cu_data *this_cu,
1489 die_reader_func_ftype *die_reader_func, void *data);
9291a0cd 1490
673bfd45 1491static htab_t allocate_signatured_type_table (struct objfile *objfile);
1fd400ff 1492
95554aad 1493static void process_psymtab_comp_unit (struct dwarf2_per_cu_data *, int);
dee91e82 1494
3019eac3
DE
1495static htab_t allocate_dwo_unit_table (struct objfile *objfile);
1496
1497static struct dwo_unit *lookup_dwo_comp_unit
1498 (struct dwarf2_per_cu_data *, char *, const char *, ULONGEST);
1499
1500static struct dwo_unit *lookup_dwo_type_unit
1501 (struct signatured_type *, char *, const char *);
1502
1503static void free_dwo_file_cleanup (void *);
1504
1505static void munmap_section_buffer (struct dwarf2_section_info *);
1506
95554aad
TT
1507static void process_cu_includes (void);
1508
9291a0cd
TT
1509#if WORDS_BIGENDIAN
1510
1511/* Convert VALUE between big- and little-endian. */
1512static offset_type
1513byte_swap (offset_type value)
1514{
1515 offset_type result;
1516
1517 result = (value & 0xff) << 24;
1518 result |= (value & 0xff00) << 8;
1519 result |= (value & 0xff0000) >> 8;
1520 result |= (value & 0xff000000) >> 24;
1521 return result;
1522}
1523
1524#define MAYBE_SWAP(V) byte_swap (V)
1525
1526#else
1527#define MAYBE_SWAP(V) (V)
1528#endif /* WORDS_BIGENDIAN */
1529
1530/* The suffix for an index file. */
1531#define INDEX_SUFFIX ".gdb-index"
1532
3da10d80
KS
1533static const char *dwarf2_physname (char *name, struct die_info *die,
1534 struct dwarf2_cu *cu);
1535
c906108c 1536/* Try to locate the sections we need for DWARF 2 debugging
251d32d9
TG
1537 information and return true if we have enough to do something.
1538 NAMES points to the dwarf2 section names, or is NULL if the standard
1539 ELF names are used. */
c906108c
SS
1540
1541int
251d32d9
TG
1542dwarf2_has_info (struct objfile *objfile,
1543 const struct dwarf2_debug_sections *names)
c906108c 1544{
be391dca
TT
1545 dwarf2_per_objfile = objfile_data (objfile, dwarf2_objfile_data_key);
1546 if (!dwarf2_per_objfile)
1547 {
1548 /* Initialize per-objfile state. */
1549 struct dwarf2_per_objfile *data
1550 = obstack_alloc (&objfile->objfile_obstack, sizeof (*data));
9a619af0 1551
be391dca
TT
1552 memset (data, 0, sizeof (*data));
1553 set_objfile_data (objfile, dwarf2_objfile_data_key, data);
1554 dwarf2_per_objfile = data;
6502dd73 1555
251d32d9
TG
1556 bfd_map_over_sections (objfile->obfd, dwarf2_locate_sections,
1557 (void *) names);
be391dca
TT
1558 dwarf2_per_objfile->objfile = objfile;
1559 }
1560 return (dwarf2_per_objfile->info.asection != NULL
1561 && dwarf2_per_objfile->abbrev.asection != NULL);
c906108c
SS
1562}
1563
251d32d9
TG
1564/* When loading sections, we look either for uncompressed section or for
1565 compressed section names. */
233a11ab
CS
1566
1567static int
251d32d9
TG
1568section_is_p (const char *section_name,
1569 const struct dwarf2_section_names *names)
233a11ab 1570{
251d32d9
TG
1571 if (names->normal != NULL
1572 && strcmp (section_name, names->normal) == 0)
1573 return 1;
1574 if (names->compressed != NULL
1575 && strcmp (section_name, names->compressed) == 0)
1576 return 1;
1577 return 0;
233a11ab
CS
1578}
1579
c906108c
SS
1580/* This function is mapped across the sections and remembers the
1581 offset and size of each of the debugging sections we are interested
1582 in. */
1583
1584static void
251d32d9 1585dwarf2_locate_sections (bfd *abfd, asection *sectp, void *vnames)
c906108c 1586{
251d32d9
TG
1587 const struct dwarf2_debug_sections *names;
1588
1589 if (vnames == NULL)
1590 names = &dwarf2_elf_names;
1591 else
1592 names = (const struct dwarf2_debug_sections *) vnames;
1593
1594 if (section_is_p (sectp->name, &names->info))
c906108c 1595 {
dce234bc
PP
1596 dwarf2_per_objfile->info.asection = sectp;
1597 dwarf2_per_objfile->info.size = bfd_get_section_size (sectp);
c906108c 1598 }
251d32d9 1599 else if (section_is_p (sectp->name, &names->abbrev))
c906108c 1600 {
dce234bc
PP
1601 dwarf2_per_objfile->abbrev.asection = sectp;
1602 dwarf2_per_objfile->abbrev.size = bfd_get_section_size (sectp);
c906108c 1603 }
251d32d9 1604 else if (section_is_p (sectp->name, &names->line))
c906108c 1605 {
dce234bc
PP
1606 dwarf2_per_objfile->line.asection = sectp;
1607 dwarf2_per_objfile->line.size = bfd_get_section_size (sectp);
c906108c 1608 }
251d32d9 1609 else if (section_is_p (sectp->name, &names->loc))
c906108c 1610 {
dce234bc
PP
1611 dwarf2_per_objfile->loc.asection = sectp;
1612 dwarf2_per_objfile->loc.size = bfd_get_section_size (sectp);
c906108c 1613 }
251d32d9 1614 else if (section_is_p (sectp->name, &names->macinfo))
c906108c 1615 {
dce234bc
PP
1616 dwarf2_per_objfile->macinfo.asection = sectp;
1617 dwarf2_per_objfile->macinfo.size = bfd_get_section_size (sectp);
c906108c 1618 }
cf2c3c16
TT
1619 else if (section_is_p (sectp->name, &names->macro))
1620 {
1621 dwarf2_per_objfile->macro.asection = sectp;
1622 dwarf2_per_objfile->macro.size = bfd_get_section_size (sectp);
1623 }
251d32d9 1624 else if (section_is_p (sectp->name, &names->str))
c906108c 1625 {
dce234bc
PP
1626 dwarf2_per_objfile->str.asection = sectp;
1627 dwarf2_per_objfile->str.size = bfd_get_section_size (sectp);
c906108c 1628 }
3019eac3
DE
1629 else if (section_is_p (sectp->name, &names->addr))
1630 {
1631 dwarf2_per_objfile->addr.asection = sectp;
1632 dwarf2_per_objfile->addr.size = bfd_get_section_size (sectp);
1633 }
251d32d9 1634 else if (section_is_p (sectp->name, &names->frame))
b6af0555 1635 {
dce234bc
PP
1636 dwarf2_per_objfile->frame.asection = sectp;
1637 dwarf2_per_objfile->frame.size = bfd_get_section_size (sectp);
b6af0555 1638 }
251d32d9 1639 else if (section_is_p (sectp->name, &names->eh_frame))
b6af0555 1640 {
81b9b86e 1641 flagword aflag = bfd_get_section_flags (abfd, sectp);
9a619af0 1642
3799ccc6
EZ
1643 if (aflag & SEC_HAS_CONTENTS)
1644 {
dce234bc
PP
1645 dwarf2_per_objfile->eh_frame.asection = sectp;
1646 dwarf2_per_objfile->eh_frame.size = bfd_get_section_size (sectp);
3799ccc6 1647 }
b6af0555 1648 }
251d32d9 1649 else if (section_is_p (sectp->name, &names->ranges))
af34e669 1650 {
dce234bc
PP
1651 dwarf2_per_objfile->ranges.asection = sectp;
1652 dwarf2_per_objfile->ranges.size = bfd_get_section_size (sectp);
af34e669 1653 }
251d32d9 1654 else if (section_is_p (sectp->name, &names->types))
348e048f 1655 {
8b70b953
TT
1656 struct dwarf2_section_info type_section;
1657
1658 memset (&type_section, 0, sizeof (type_section));
1659 type_section.asection = sectp;
1660 type_section.size = bfd_get_section_size (sectp);
1661
1662 VEC_safe_push (dwarf2_section_info_def, dwarf2_per_objfile->types,
1663 &type_section);
348e048f 1664 }
251d32d9 1665 else if (section_is_p (sectp->name, &names->gdb_index))
9291a0cd
TT
1666 {
1667 dwarf2_per_objfile->gdb_index.asection = sectp;
1668 dwarf2_per_objfile->gdb_index.size = bfd_get_section_size (sectp);
1669 }
dce234bc 1670
72dca2f5
FR
1671 if ((bfd_get_section_flags (abfd, sectp) & SEC_LOAD)
1672 && bfd_section_vma (abfd, sectp) == 0)
1673 dwarf2_per_objfile->has_section_at_zero = 1;
c906108c
SS
1674}
1675
dce234bc
PP
1676/* Decompress a section that was compressed using zlib. Store the
1677 decompressed buffer, and its size, in OUTBUF and OUTSIZE. */
233a11ab
CS
1678
1679static void
dce234bc
PP
1680zlib_decompress_section (struct objfile *objfile, asection *sectp,
1681 gdb_byte **outbuf, bfd_size_type *outsize)
1682{
3019eac3 1683 bfd *abfd = sectp->owner;
dce234bc
PP
1684#ifndef HAVE_ZLIB_H
1685 error (_("Support for zlib-compressed DWARF data (from '%s') "
1686 "is disabled in this copy of GDB"),
1687 bfd_get_filename (abfd));
1688#else
1689 bfd_size_type compressed_size = bfd_get_section_size (sectp);
1690 gdb_byte *compressed_buffer = xmalloc (compressed_size);
affddf13 1691 struct cleanup *cleanup = make_cleanup (xfree, compressed_buffer);
dce234bc
PP
1692 bfd_size_type uncompressed_size;
1693 gdb_byte *uncompressed_buffer;
1694 z_stream strm;
1695 int rc;
1696 int header_size = 12;
1697
1698 if (bfd_seek (abfd, sectp->filepos, SEEK_SET) != 0
3e43a32a
MS
1699 || bfd_bread (compressed_buffer,
1700 compressed_size, abfd) != compressed_size)
dce234bc
PP
1701 error (_("Dwarf Error: Can't read DWARF data from '%s'"),
1702 bfd_get_filename (abfd));
1703
1704 /* Read the zlib header. In this case, it should be "ZLIB" followed
1705 by the uncompressed section size, 8 bytes in big-endian order. */
1706 if (compressed_size < header_size
1707 || strncmp (compressed_buffer, "ZLIB", 4) != 0)
1708 error (_("Dwarf Error: Corrupt DWARF ZLIB header from '%s'"),
1709 bfd_get_filename (abfd));
1710 uncompressed_size = compressed_buffer[4]; uncompressed_size <<= 8;
1711 uncompressed_size += compressed_buffer[5]; uncompressed_size <<= 8;
1712 uncompressed_size += compressed_buffer[6]; uncompressed_size <<= 8;
1713 uncompressed_size += compressed_buffer[7]; uncompressed_size <<= 8;
1714 uncompressed_size += compressed_buffer[8]; uncompressed_size <<= 8;
1715 uncompressed_size += compressed_buffer[9]; uncompressed_size <<= 8;
1716 uncompressed_size += compressed_buffer[10]; uncompressed_size <<= 8;
1717 uncompressed_size += compressed_buffer[11];
1718
1719 /* It is possible the section consists of several compressed
1720 buffers concatenated together, so we uncompress in a loop. */
1721 strm.zalloc = NULL;
1722 strm.zfree = NULL;
1723 strm.opaque = NULL;
1724 strm.avail_in = compressed_size - header_size;
1725 strm.next_in = (Bytef*) compressed_buffer + header_size;
1726 strm.avail_out = uncompressed_size;
1727 uncompressed_buffer = obstack_alloc (&objfile->objfile_obstack,
1728 uncompressed_size);
1729 rc = inflateInit (&strm);
1730 while (strm.avail_in > 0)
1731 {
1732 if (rc != Z_OK)
1733 error (_("Dwarf Error: setting up DWARF uncompression in '%s': %d"),
1734 bfd_get_filename (abfd), rc);
1735 strm.next_out = ((Bytef*) uncompressed_buffer
1736 + (uncompressed_size - strm.avail_out));
1737 rc = inflate (&strm, Z_FINISH);
1738 if (rc != Z_STREAM_END)
1739 error (_("Dwarf Error: zlib error uncompressing from '%s': %d"),
1740 bfd_get_filename (abfd), rc);
1741 rc = inflateReset (&strm);
1742 }
1743 rc = inflateEnd (&strm);
1744 if (rc != Z_OK
1745 || strm.avail_out != 0)
1746 error (_("Dwarf Error: concluding DWARF uncompression in '%s': %d"),
1747 bfd_get_filename (abfd), rc);
1748
affddf13 1749 do_cleanups (cleanup);
dce234bc
PP
1750 *outbuf = uncompressed_buffer;
1751 *outsize = uncompressed_size;
1752#endif
233a11ab
CS
1753}
1754
fceca515
DE
1755/* A helper function that decides whether a section is empty,
1756 or not present. */
9e0ac564
TT
1757
1758static int
1759dwarf2_section_empty_p (struct dwarf2_section_info *info)
1760{
1761 return info->asection == NULL || info->size == 0;
1762}
1763
3019eac3
DE
1764/* Read the contents of the section INFO.
1765 OBJFILE is the main object file, but not necessarily the file where
1766 the section comes from. E.g., for DWO files INFO->asection->owner
1767 is the bfd of the DWO file.
dce234bc 1768 If the section is compressed, uncompress it before returning. */
c906108c 1769
dce234bc
PP
1770static void
1771dwarf2_read_section (struct objfile *objfile, struct dwarf2_section_info *info)
c906108c 1772{
dce234bc 1773 asection *sectp = info->asection;
3019eac3 1774 bfd *abfd;
dce234bc
PP
1775 gdb_byte *buf, *retbuf;
1776 unsigned char header[4];
c906108c 1777
be391dca
TT
1778 if (info->readin)
1779 return;
dce234bc 1780 info->buffer = NULL;
b315ab21 1781 info->map_addr = NULL;
be391dca 1782 info->readin = 1;
188dd5d6 1783
9e0ac564 1784 if (dwarf2_section_empty_p (info))
dce234bc 1785 return;
c906108c 1786
3019eac3
DE
1787 /* Note that ABFD may not be from OBJFILE, e.g. a DWO section. */
1788 abfd = sectp->owner;
1789
dce234bc
PP
1790 /* Check if the file has a 4-byte header indicating compression. */
1791 if (info->size > sizeof (header)
1792 && bfd_seek (abfd, sectp->filepos, SEEK_SET) == 0
1793 && bfd_bread (header, sizeof (header), abfd) == sizeof (header))
1794 {
1795 /* Upon decompression, update the buffer and its size. */
1796 if (strncmp (header, "ZLIB", sizeof (header)) == 0)
1797 {
1798 zlib_decompress_section (objfile, sectp, &info->buffer,
1799 &info->size);
1800 return;
1801 }
1802 }
4bdf3d34 1803
dce234bc
PP
1804#ifdef HAVE_MMAP
1805 if (pagesize == 0)
1806 pagesize = getpagesize ();
2e276125 1807
dce234bc
PP
1808 /* Only try to mmap sections which are large enough: we don't want to
1809 waste space due to fragmentation. Also, only try mmap for sections
1810 without relocations. */
1811
1812 if (info->size > 4 * pagesize && (sectp->flags & SEC_RELOC) == 0)
1813 {
b315ab21
TG
1814 info->buffer = bfd_mmap (abfd, 0, info->size, PROT_READ,
1815 MAP_PRIVATE, sectp->filepos,
1816 &info->map_addr, &info->map_len);
dce234bc 1817
b315ab21 1818 if ((caddr_t)info->buffer != MAP_FAILED)
dce234bc 1819 {
be391dca 1820#if HAVE_POSIX_MADVISE
b315ab21 1821 posix_madvise (info->map_addr, info->map_len, POSIX_MADV_WILLNEED);
be391dca 1822#endif
dce234bc
PP
1823 return;
1824 }
1825 }
1826#endif
1827
1828 /* If we get here, we are a normal, not-compressed section. */
1829 info->buffer = buf
1830 = obstack_alloc (&objfile->objfile_obstack, info->size);
1831
1832 /* When debugging .o files, we may need to apply relocations; see
1833 http://sourceware.org/ml/gdb-patches/2002-04/msg00136.html .
1834 We never compress sections in .o files, so we only need to
1835 try this when the section is not compressed. */
ac8035ab 1836 retbuf = symfile_relocate_debug_section (objfile, sectp, buf);
dce234bc
PP
1837 if (retbuf != NULL)
1838 {
1839 info->buffer = retbuf;
1840 return;
1841 }
1842
1843 if (bfd_seek (abfd, sectp->filepos, SEEK_SET) != 0
1844 || bfd_bread (buf, info->size, abfd) != info->size)
1845 error (_("Dwarf Error: Can't read DWARF data from '%s'"),
1846 bfd_get_filename (abfd));
1847}
1848
9e0ac564
TT
1849/* A helper function that returns the size of a section in a safe way.
1850 If you are positive that the section has been read before using the
1851 size, then it is safe to refer to the dwarf2_section_info object's
1852 "size" field directly. In other cases, you must call this
1853 function, because for compressed sections the size field is not set
1854 correctly until the section has been read. */
1855
1856static bfd_size_type
1857dwarf2_section_size (struct objfile *objfile,
1858 struct dwarf2_section_info *info)
1859{
1860 if (!info->readin)
1861 dwarf2_read_section (objfile, info);
1862 return info->size;
1863}
1864
dce234bc 1865/* Fill in SECTP, BUFP and SIZEP with section info, given OBJFILE and
0963b4bd 1866 SECTION_NAME. */
af34e669 1867
dce234bc 1868void
3017a003
TG
1869dwarf2_get_section_info (struct objfile *objfile,
1870 enum dwarf2_section_enum sect,
dce234bc
PP
1871 asection **sectp, gdb_byte **bufp,
1872 bfd_size_type *sizep)
1873{
1874 struct dwarf2_per_objfile *data
1875 = objfile_data (objfile, dwarf2_objfile_data_key);
1876 struct dwarf2_section_info *info;
a3b2a86b
TT
1877
1878 /* We may see an objfile without any DWARF, in which case we just
1879 return nothing. */
1880 if (data == NULL)
1881 {
1882 *sectp = NULL;
1883 *bufp = NULL;
1884 *sizep = 0;
1885 return;
1886 }
3017a003
TG
1887 switch (sect)
1888 {
1889 case DWARF2_DEBUG_FRAME:
1890 info = &data->frame;
1891 break;
1892 case DWARF2_EH_FRAME:
1893 info = &data->eh_frame;
1894 break;
1895 default:
1896 gdb_assert_not_reached ("unexpected section");
1897 }
dce234bc 1898
9e0ac564 1899 dwarf2_read_section (objfile, info);
dce234bc
PP
1900
1901 *sectp = info->asection;
1902 *bufp = info->buffer;
1903 *sizep = info->size;
1904}
1905
9291a0cd 1906\f
7b9f3c50
DE
1907/* DWARF quick_symbols_functions support. */
1908
1909/* TUs can share .debug_line entries, and there can be a lot more TUs than
1910 unique line tables, so we maintain a separate table of all .debug_line
1911 derived entries to support the sharing.
1912 All the quick functions need is the list of file names. We discard the
1913 line_header when we're done and don't need to record it here. */
1914struct quick_file_names
1915{
1916 /* The offset in .debug_line of the line table. We hash on this. */
1917 unsigned int offset;
1918
1919 /* The number of entries in file_names, real_names. */
1920 unsigned int num_file_names;
1921
1922 /* The file names from the line table, after being run through
1923 file_full_name. */
1924 const char **file_names;
1925
1926 /* The file names from the line table after being run through
1927 gdb_realpath. These are computed lazily. */
1928 const char **real_names;
1929};
1930
1931/* When using the index (and thus not using psymtabs), each CU has an
1932 object of this type. This is used to hold information needed by
1933 the various "quick" methods. */
1934struct dwarf2_per_cu_quick_data
1935{
1936 /* The file table. This can be NULL if there was no file table
1937 or it's currently not read in.
1938 NOTE: This points into dwarf2_per_objfile->quick_file_names_table. */
1939 struct quick_file_names *file_names;
1940
1941 /* The corresponding symbol table. This is NULL if symbols for this
1942 CU have not yet been read. */
1943 struct symtab *symtab;
1944
1945 /* A temporary mark bit used when iterating over all CUs in
1946 expand_symtabs_matching. */
1947 unsigned int mark : 1;
1948
1949 /* True if we've tried to read the file table and found there isn't one.
1950 There will be no point in trying to read it again next time. */
1951 unsigned int no_file_data : 1;
1952};
1953
1954/* Hash function for a quick_file_names. */
1955
1956static hashval_t
1957hash_file_name_entry (const void *e)
1958{
1959 const struct quick_file_names *file_data = e;
1960
1961 return file_data->offset;
1962}
1963
1964/* Equality function for a quick_file_names. */
1965
1966static int
1967eq_file_name_entry (const void *a, const void *b)
1968{
1969 const struct quick_file_names *ea = a;
1970 const struct quick_file_names *eb = b;
1971
1972 return ea->offset == eb->offset;
1973}
1974
1975/* Delete function for a quick_file_names. */
1976
1977static void
1978delete_file_name_entry (void *e)
1979{
1980 struct quick_file_names *file_data = e;
1981 int i;
1982
1983 for (i = 0; i < file_data->num_file_names; ++i)
1984 {
1985 xfree ((void*) file_data->file_names[i]);
1986 if (file_data->real_names)
1987 xfree ((void*) file_data->real_names[i]);
1988 }
1989
1990 /* The space for the struct itself lives on objfile_obstack,
1991 so we don't free it here. */
1992}
1993
1994/* Create a quick_file_names hash table. */
1995
1996static htab_t
1997create_quick_file_names_table (unsigned int nr_initial_entries)
1998{
1999 return htab_create_alloc (nr_initial_entries,
2000 hash_file_name_entry, eq_file_name_entry,
2001 delete_file_name_entry, xcalloc, xfree);
2002}
9291a0cd 2003
918dd910
JK
2004/* Read in PER_CU->CU. This function is unrelated to symtabs, symtab would
2005 have to be created afterwards. You should call age_cached_comp_units after
2006 processing PER_CU->CU. dw2_setup must have been already called. */
2007
2008static void
2009load_cu (struct dwarf2_per_cu_data *per_cu)
2010{
3019eac3 2011 if (per_cu->is_debug_types)
e5fe5e75 2012 load_full_type_unit (per_cu);
918dd910 2013 else
95554aad 2014 load_full_comp_unit (per_cu, language_minimal);
918dd910 2015
918dd910 2016 gdb_assert (per_cu->cu != NULL);
2dc860c0
DE
2017
2018 dwarf2_find_base_address (per_cu->cu->dies, per_cu->cu);
918dd910
JK
2019}
2020
a0f42c21 2021/* Read in the symbols for PER_CU. */
2fdf6df6 2022
9291a0cd 2023static void
a0f42c21 2024dw2_do_instantiate_symtab (struct dwarf2_per_cu_data *per_cu)
9291a0cd
TT
2025{
2026 struct cleanup *back_to;
2027
2028 back_to = make_cleanup (dwarf2_release_queue, NULL);
2029
95554aad
TT
2030 if (dwarf2_per_objfile->using_index
2031 ? per_cu->v.quick->symtab == NULL
2032 : (per_cu->v.psymtab == NULL || !per_cu->v.psymtab->readin))
2033 {
2034 queue_comp_unit (per_cu, language_minimal);
2035 load_cu (per_cu);
2036 }
9291a0cd 2037
a0f42c21 2038 process_queue ();
9291a0cd
TT
2039
2040 /* Age the cache, releasing compilation units that have not
2041 been used recently. */
2042 age_cached_comp_units ();
2043
2044 do_cleanups (back_to);
2045}
2046
2047/* Ensure that the symbols for PER_CU have been read in. OBJFILE is
2048 the objfile from which this CU came. Returns the resulting symbol
2049 table. */
2fdf6df6 2050
9291a0cd 2051static struct symtab *
a0f42c21 2052dw2_instantiate_symtab (struct dwarf2_per_cu_data *per_cu)
9291a0cd 2053{
95554aad 2054 gdb_assert (dwarf2_per_objfile->using_index);
9291a0cd
TT
2055 if (!per_cu->v.quick->symtab)
2056 {
2057 struct cleanup *back_to = make_cleanup (free_cached_comp_units, NULL);
2058 increment_reading_symtab ();
a0f42c21 2059 dw2_do_instantiate_symtab (per_cu);
95554aad 2060 process_cu_includes ();
9291a0cd
TT
2061 do_cleanups (back_to);
2062 }
2063 return per_cu->v.quick->symtab;
2064}
2065
1fd400ff 2066/* Return the CU given its index. */
2fdf6df6 2067
1fd400ff
TT
2068static struct dwarf2_per_cu_data *
2069dw2_get_cu (int index)
2070{
2071 if (index >= dwarf2_per_objfile->n_comp_units)
2072 {
2073 index -= dwarf2_per_objfile->n_comp_units;
d467dd73 2074 return dwarf2_per_objfile->all_type_units[index];
1fd400ff
TT
2075 }
2076 return dwarf2_per_objfile->all_comp_units[index];
2077}
2078
9291a0cd
TT
2079/* A helper function that knows how to read a 64-bit value in a way
2080 that doesn't make gdb die. Returns 1 if the conversion went ok, 0
2081 otherwise. */
2fdf6df6 2082
9291a0cd
TT
2083static int
2084extract_cu_value (const char *bytes, ULONGEST *result)
2085{
2086 if (sizeof (ULONGEST) < 8)
2087 {
2088 int i;
2089
2090 /* Ignore the upper 4 bytes if they are all zero. */
2091 for (i = 0; i < 4; ++i)
2092 if (bytes[i + 4] != 0)
2093 return 0;
2094
2095 *result = extract_unsigned_integer (bytes, 4, BFD_ENDIAN_LITTLE);
2096 }
2097 else
2098 *result = extract_unsigned_integer (bytes, 8, BFD_ENDIAN_LITTLE);
2099 return 1;
2100}
2101
2102/* Read the CU list from the mapped index, and use it to create all
2103 the CU objects for this objfile. Return 0 if something went wrong,
2104 1 if everything went ok. */
2fdf6df6 2105
9291a0cd 2106static int
1fd400ff
TT
2107create_cus_from_index (struct objfile *objfile, const gdb_byte *cu_list,
2108 offset_type cu_list_elements)
9291a0cd
TT
2109{
2110 offset_type i;
9291a0cd
TT
2111
2112 dwarf2_per_objfile->n_comp_units = cu_list_elements / 2;
2113 dwarf2_per_objfile->all_comp_units
2114 = obstack_alloc (&objfile->objfile_obstack,
2115 dwarf2_per_objfile->n_comp_units
2116 * sizeof (struct dwarf2_per_cu_data *));
2117
2118 for (i = 0; i < cu_list_elements; i += 2)
2119 {
2120 struct dwarf2_per_cu_data *the_cu;
2121 ULONGEST offset, length;
2122
2123 if (!extract_cu_value (cu_list, &offset)
2124 || !extract_cu_value (cu_list + 8, &length))
2125 return 0;
2126 cu_list += 2 * 8;
2127
2128 the_cu = OBSTACK_ZALLOC (&objfile->objfile_obstack,
2129 struct dwarf2_per_cu_data);
b64f50a1 2130 the_cu->offset.sect_off = offset;
9291a0cd
TT
2131 the_cu->length = length;
2132 the_cu->objfile = objfile;
3019eac3 2133 the_cu->info_or_types_section = &dwarf2_per_objfile->info;
9291a0cd
TT
2134 the_cu->v.quick = OBSTACK_ZALLOC (&objfile->objfile_obstack,
2135 struct dwarf2_per_cu_quick_data);
2136 dwarf2_per_objfile->all_comp_units[i / 2] = the_cu;
2137 }
2138
2139 return 1;
2140}
2141
1fd400ff 2142/* Create the signatured type hash table from the index. */
673bfd45 2143
1fd400ff 2144static int
673bfd45 2145create_signatured_type_table_from_index (struct objfile *objfile,
8b70b953 2146 struct dwarf2_section_info *section,
673bfd45
DE
2147 const gdb_byte *bytes,
2148 offset_type elements)
1fd400ff
TT
2149{
2150 offset_type i;
673bfd45 2151 htab_t sig_types_hash;
1fd400ff 2152
d467dd73
DE
2153 dwarf2_per_objfile->n_type_units = elements / 3;
2154 dwarf2_per_objfile->all_type_units
1fd400ff 2155 = obstack_alloc (&objfile->objfile_obstack,
d467dd73 2156 dwarf2_per_objfile->n_type_units
1fd400ff
TT
2157 * sizeof (struct dwarf2_per_cu_data *));
2158
673bfd45 2159 sig_types_hash = allocate_signatured_type_table (objfile);
1fd400ff
TT
2160
2161 for (i = 0; i < elements; i += 3)
2162 {
52dc124a
DE
2163 struct signatured_type *sig_type;
2164 ULONGEST offset, type_offset_in_tu, signature;
1fd400ff
TT
2165 void **slot;
2166
2167 if (!extract_cu_value (bytes, &offset)
52dc124a 2168 || !extract_cu_value (bytes + 8, &type_offset_in_tu))
1fd400ff
TT
2169 return 0;
2170 signature = extract_unsigned_integer (bytes + 16, 8, BFD_ENDIAN_LITTLE);
2171 bytes += 3 * 8;
2172
52dc124a 2173 sig_type = OBSTACK_ZALLOC (&objfile->objfile_obstack,
1fd400ff 2174 struct signatured_type);
52dc124a 2175 sig_type->signature = signature;
3019eac3
DE
2176 sig_type->type_offset_in_tu.cu_off = type_offset_in_tu;
2177 sig_type->per_cu.is_debug_types = 1;
2178 sig_type->per_cu.info_or_types_section = section;
52dc124a
DE
2179 sig_type->per_cu.offset.sect_off = offset;
2180 sig_type->per_cu.objfile = objfile;
2181 sig_type->per_cu.v.quick
1fd400ff
TT
2182 = OBSTACK_ZALLOC (&objfile->objfile_obstack,
2183 struct dwarf2_per_cu_quick_data);
2184
52dc124a
DE
2185 slot = htab_find_slot (sig_types_hash, sig_type, INSERT);
2186 *slot = sig_type;
1fd400ff 2187
52dc124a 2188 dwarf2_per_objfile->all_type_units[i / 3] = &sig_type->per_cu;
1fd400ff
TT
2189 }
2190
673bfd45 2191 dwarf2_per_objfile->signatured_types = sig_types_hash;
1fd400ff
TT
2192
2193 return 1;
2194}
2195
9291a0cd
TT
2196/* Read the address map data from the mapped index, and use it to
2197 populate the objfile's psymtabs_addrmap. */
2fdf6df6 2198
9291a0cd
TT
2199static void
2200create_addrmap_from_index (struct objfile *objfile, struct mapped_index *index)
2201{
2202 const gdb_byte *iter, *end;
2203 struct obstack temp_obstack;
2204 struct addrmap *mutable_map;
2205 struct cleanup *cleanup;
2206 CORE_ADDR baseaddr;
2207
2208 obstack_init (&temp_obstack);
2209 cleanup = make_cleanup_obstack_free (&temp_obstack);
2210 mutable_map = addrmap_create_mutable (&temp_obstack);
2211
2212 iter = index->address_table;
2213 end = iter + index->address_table_size;
2214
2215 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
2216
2217 while (iter < end)
2218 {
2219 ULONGEST hi, lo, cu_index;
2220 lo = extract_unsigned_integer (iter, 8, BFD_ENDIAN_LITTLE);
2221 iter += 8;
2222 hi = extract_unsigned_integer (iter, 8, BFD_ENDIAN_LITTLE);
2223 iter += 8;
2224 cu_index = extract_unsigned_integer (iter, 4, BFD_ENDIAN_LITTLE);
2225 iter += 4;
2226
2227 addrmap_set_empty (mutable_map, lo + baseaddr, hi + baseaddr - 1,
1fd400ff 2228 dw2_get_cu (cu_index));
9291a0cd
TT
2229 }
2230
2231 objfile->psymtabs_addrmap = addrmap_create_fixed (mutable_map,
2232 &objfile->objfile_obstack);
2233 do_cleanups (cleanup);
2234}
2235
59d7bcaf
JK
2236/* The hash function for strings in the mapped index. This is the same as
2237 SYMBOL_HASH_NEXT, but we keep a separate copy to maintain control over the
2238 implementation. This is necessary because the hash function is tied to the
2239 format of the mapped index file. The hash values do not have to match with
559a7a62
JK
2240 SYMBOL_HASH_NEXT.
2241
2242 Use INT_MAX for INDEX_VERSION if you generate the current index format. */
2fdf6df6 2243
9291a0cd 2244static hashval_t
559a7a62 2245mapped_index_string_hash (int index_version, const void *p)
9291a0cd
TT
2246{
2247 const unsigned char *str = (const unsigned char *) p;
2248 hashval_t r = 0;
2249 unsigned char c;
2250
2251 while ((c = *str++) != 0)
559a7a62
JK
2252 {
2253 if (index_version >= 5)
2254 c = tolower (c);
2255 r = r * 67 + c - 113;
2256 }
9291a0cd
TT
2257
2258 return r;
2259}
2260
2261/* Find a slot in the mapped index INDEX for the object named NAME.
2262 If NAME is found, set *VEC_OUT to point to the CU vector in the
2263 constant pool and return 1. If NAME cannot be found, return 0. */
2fdf6df6 2264
9291a0cd
TT
2265static int
2266find_slot_in_mapped_hash (struct mapped_index *index, const char *name,
2267 offset_type **vec_out)
2268{
0cf03b49
JK
2269 struct cleanup *back_to = make_cleanup (null_cleanup, 0);
2270 offset_type hash;
9291a0cd 2271 offset_type slot, step;
559a7a62 2272 int (*cmp) (const char *, const char *);
9291a0cd 2273
0cf03b49
JK
2274 if (current_language->la_language == language_cplus
2275 || current_language->la_language == language_java
2276 || current_language->la_language == language_fortran)
2277 {
2278 /* NAME is already canonical. Drop any qualifiers as .gdb_index does
2279 not contain any. */
2280 const char *paren = strchr (name, '(');
2281
2282 if (paren)
2283 {
2284 char *dup;
2285
2286 dup = xmalloc (paren - name + 1);
2287 memcpy (dup, name, paren - name);
2288 dup[paren - name] = 0;
2289
2290 make_cleanup (xfree, dup);
2291 name = dup;
2292 }
2293 }
2294
559a7a62 2295 /* Index version 4 did not support case insensitive searches. But the
feea76c2 2296 indices for case insensitive languages are built in lowercase, therefore
559a7a62
JK
2297 simulate our NAME being searched is also lowercased. */
2298 hash = mapped_index_string_hash ((index->version == 4
2299 && case_sensitivity == case_sensitive_off
2300 ? 5 : index->version),
2301 name);
2302
3876f04e
DE
2303 slot = hash & (index->symbol_table_slots - 1);
2304 step = ((hash * 17) & (index->symbol_table_slots - 1)) | 1;
559a7a62 2305 cmp = (case_sensitivity == case_sensitive_on ? strcmp : strcasecmp);
9291a0cd
TT
2306
2307 for (;;)
2308 {
2309 /* Convert a slot number to an offset into the table. */
2310 offset_type i = 2 * slot;
2311 const char *str;
3876f04e 2312 if (index->symbol_table[i] == 0 && index->symbol_table[i + 1] == 0)
0cf03b49
JK
2313 {
2314 do_cleanups (back_to);
2315 return 0;
2316 }
9291a0cd 2317
3876f04e 2318 str = index->constant_pool + MAYBE_SWAP (index->symbol_table[i]);
559a7a62 2319 if (!cmp (name, str))
9291a0cd
TT
2320 {
2321 *vec_out = (offset_type *) (index->constant_pool
3876f04e 2322 + MAYBE_SWAP (index->symbol_table[i + 1]));
0cf03b49 2323 do_cleanups (back_to);
9291a0cd
TT
2324 return 1;
2325 }
2326
3876f04e 2327 slot = (slot + step) & (index->symbol_table_slots - 1);
9291a0cd
TT
2328 }
2329}
2330
2331/* Read the index file. If everything went ok, initialize the "quick"
2332 elements of all the CUs and return 1. Otherwise, return 0. */
2fdf6df6 2333
9291a0cd
TT
2334static int
2335dwarf2_read_index (struct objfile *objfile)
2336{
9291a0cd
TT
2337 char *addr;
2338 struct mapped_index *map;
b3b272e1 2339 offset_type *metadata;
ac0b195c
KW
2340 const gdb_byte *cu_list;
2341 const gdb_byte *types_list = NULL;
2342 offset_type version, cu_list_elements;
2343 offset_type types_list_elements = 0;
1fd400ff 2344 int i;
9291a0cd 2345
9e0ac564 2346 if (dwarf2_section_empty_p (&dwarf2_per_objfile->gdb_index))
9291a0cd 2347 return 0;
82430852
JK
2348
2349 /* Older elfutils strip versions could keep the section in the main
2350 executable while splitting it for the separate debug info file. */
2351 if ((bfd_get_file_flags (dwarf2_per_objfile->gdb_index.asection)
2352 & SEC_HAS_CONTENTS) == 0)
2353 return 0;
2354
9291a0cd
TT
2355 dwarf2_read_section (objfile, &dwarf2_per_objfile->gdb_index);
2356
2357 addr = dwarf2_per_objfile->gdb_index.buffer;
2358 /* Version check. */
1fd400ff 2359 version = MAYBE_SWAP (*(offset_type *) addr);
987d643c 2360 /* Versions earlier than 3 emitted every copy of a psymbol. This
a6e293d1 2361 causes the index to behave very poorly for certain requests. Version 3
831adc1f 2362 contained incomplete addrmap. So, it seems better to just ignore such
481860b3 2363 indices. */
831adc1f 2364 if (version < 4)
481860b3
GB
2365 {
2366 static int warning_printed = 0;
2367 if (!warning_printed)
2368 {
2369 warning (_("Skipping obsolete .gdb_index section in %s."),
2370 objfile->name);
2371 warning_printed = 1;
2372 }
2373 return 0;
2374 }
2375 /* Index version 4 uses a different hash function than index version
2376 5 and later.
2377
2378 Versions earlier than 6 did not emit psymbols for inlined
2379 functions. Using these files will cause GDB not to be able to
2380 set breakpoints on inlined functions by name, so we ignore these
2381 indices unless the --use-deprecated-index-sections command line
2382 option was supplied. */
2383 if (version < 6 && !use_deprecated_index_sections)
2384 {
2385 static int warning_printed = 0;
2386 if (!warning_printed)
2387 {
2388 warning (_("Skipping deprecated .gdb_index section in %s, pass "
2389 "--use-deprecated-index-sections to use them anyway"),
2390 objfile->name);
2391 warning_printed = 1;
2392 }
2393 return 0;
2394 }
2395 /* Indexes with higher version than the one supported by GDB may be no
594e8718 2396 longer backward compatible. */
156942c7 2397 if (version > 7)
594e8718 2398 return 0;
9291a0cd
TT
2399
2400 map = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct mapped_index);
559a7a62 2401 map->version = version;
b3b272e1 2402 map->total_size = dwarf2_per_objfile->gdb_index.size;
9291a0cd
TT
2403
2404 metadata = (offset_type *) (addr + sizeof (offset_type));
1fd400ff
TT
2405
2406 i = 0;
2407 cu_list = addr + MAYBE_SWAP (metadata[i]);
2408 cu_list_elements = ((MAYBE_SWAP (metadata[i + 1]) - MAYBE_SWAP (metadata[i]))
9291a0cd 2409 / 8);
1fd400ff
TT
2410 ++i;
2411
987d643c
TT
2412 types_list = addr + MAYBE_SWAP (metadata[i]);
2413 types_list_elements = ((MAYBE_SWAP (metadata[i + 1])
2414 - MAYBE_SWAP (metadata[i]))
2415 / 8);
2416 ++i;
1fd400ff
TT
2417
2418 map->address_table = addr + MAYBE_SWAP (metadata[i]);
2419 map->address_table_size = (MAYBE_SWAP (metadata[i + 1])
2420 - MAYBE_SWAP (metadata[i]));
2421 ++i;
2422
3876f04e
DE
2423 map->symbol_table = (offset_type *) (addr + MAYBE_SWAP (metadata[i]));
2424 map->symbol_table_slots = ((MAYBE_SWAP (metadata[i + 1])
2425 - MAYBE_SWAP (metadata[i]))
2426 / (2 * sizeof (offset_type)));
1fd400ff 2427 ++i;
9291a0cd 2428
1fd400ff
TT
2429 map->constant_pool = addr + MAYBE_SWAP (metadata[i]);
2430
0fefef59
DE
2431 /* Don't use the index if it's empty. */
2432 if (map->symbol_table_slots == 0)
2433 return 0;
2434
1fd400ff
TT
2435 if (!create_cus_from_index (objfile, cu_list, cu_list_elements))
2436 return 0;
2437
8b70b953
TT
2438 if (types_list_elements)
2439 {
2440 struct dwarf2_section_info *section;
2441
2442 /* We can only handle a single .debug_types when we have an
2443 index. */
2444 if (VEC_length (dwarf2_section_info_def, dwarf2_per_objfile->types) != 1)
2445 return 0;
2446
2447 section = VEC_index (dwarf2_section_info_def,
2448 dwarf2_per_objfile->types, 0);
2449
2450 if (!create_signatured_type_table_from_index (objfile, section,
2451 types_list,
2452 types_list_elements))
2453 return 0;
2454 }
9291a0cd
TT
2455
2456 create_addrmap_from_index (objfile, map);
2457
2458 dwarf2_per_objfile->index_table = map;
2459 dwarf2_per_objfile->using_index = 1;
7b9f3c50
DE
2460 dwarf2_per_objfile->quick_file_names_table =
2461 create_quick_file_names_table (dwarf2_per_objfile->n_comp_units);
9291a0cd
TT
2462
2463 return 1;
2464}
2465
2466/* A helper for the "quick" functions which sets the global
2467 dwarf2_per_objfile according to OBJFILE. */
2fdf6df6 2468
9291a0cd
TT
2469static void
2470dw2_setup (struct objfile *objfile)
2471{
2472 dwarf2_per_objfile = objfile_data (objfile, dwarf2_objfile_data_key);
2473 gdb_assert (dwarf2_per_objfile);
2474}
2475
dee91e82 2476/* die_reader_func for dw2_get_file_names. */
2fdf6df6 2477
dee91e82
DE
2478static void
2479dw2_get_file_names_reader (const struct die_reader_specs *reader,
2480 gdb_byte *info_ptr,
2481 struct die_info *comp_unit_die,
2482 int has_children,
2483 void *data)
9291a0cd 2484{
dee91e82
DE
2485 struct dwarf2_cu *cu = reader->cu;
2486 struct dwarf2_per_cu_data *this_cu = cu->per_cu;
2487 struct objfile *objfile = dwarf2_per_objfile->objfile;
7b9f3c50 2488 struct line_header *lh;
9291a0cd 2489 struct attribute *attr;
dee91e82 2490 int i;
9291a0cd 2491 char *name, *comp_dir;
7b9f3c50
DE
2492 void **slot;
2493 struct quick_file_names *qfn;
2494 unsigned int line_offset;
9291a0cd 2495
07261596
TT
2496 /* Our callers never want to match partial units -- instead they
2497 will match the enclosing full CU. */
2498 if (comp_unit_die->tag == DW_TAG_partial_unit)
2499 {
2500 this_cu->v.quick->no_file_data = 1;
2501 return;
2502 }
2503
7b9f3c50
DE
2504 lh = NULL;
2505 slot = NULL;
2506 line_offset = 0;
dee91e82
DE
2507
2508 attr = dwarf2_attr (comp_unit_die, DW_AT_stmt_list, cu);
9291a0cd
TT
2509 if (attr)
2510 {
7b9f3c50
DE
2511 struct quick_file_names find_entry;
2512
2513 line_offset = DW_UNSND (attr);
2514
2515 /* We may have already read in this line header (TU line header sharing).
2516 If we have we're done. */
2517 find_entry.offset = line_offset;
2518 slot = htab_find_slot (dwarf2_per_objfile->quick_file_names_table,
2519 &find_entry, INSERT);
2520 if (*slot != NULL)
2521 {
7b9f3c50 2522 this_cu->v.quick->file_names = *slot;
dee91e82 2523 return;
7b9f3c50
DE
2524 }
2525
3019eac3 2526 lh = dwarf_decode_line_header (line_offset, cu);
9291a0cd
TT
2527 }
2528 if (lh == NULL)
2529 {
7b9f3c50 2530 this_cu->v.quick->no_file_data = 1;
dee91e82 2531 return;
9291a0cd
TT
2532 }
2533
7b9f3c50
DE
2534 qfn = obstack_alloc (&objfile->objfile_obstack, sizeof (*qfn));
2535 qfn->offset = line_offset;
2536 gdb_assert (slot != NULL);
2537 *slot = qfn;
9291a0cd 2538
dee91e82 2539 find_file_and_directory (comp_unit_die, cu, &name, &comp_dir);
9291a0cd 2540
7b9f3c50
DE
2541 qfn->num_file_names = lh->num_file_names;
2542 qfn->file_names = obstack_alloc (&objfile->objfile_obstack,
2543 lh->num_file_names * sizeof (char *));
9291a0cd 2544 for (i = 0; i < lh->num_file_names; ++i)
7b9f3c50
DE
2545 qfn->file_names[i] = file_full_name (i + 1, lh, comp_dir);
2546 qfn->real_names = NULL;
9291a0cd 2547
7b9f3c50 2548 free_line_header (lh);
7b9f3c50
DE
2549
2550 this_cu->v.quick->file_names = qfn;
dee91e82
DE
2551}
2552
2553/* A helper for the "quick" functions which attempts to read the line
2554 table for THIS_CU. */
2555
2556static struct quick_file_names *
2557dw2_get_file_names (struct objfile *objfile,
2558 struct dwarf2_per_cu_data *this_cu)
2559{
2560 if (this_cu->v.quick->file_names != NULL)
2561 return this_cu->v.quick->file_names;
2562 /* If we know there is no line data, no point in looking again. */
2563 if (this_cu->v.quick->no_file_data)
2564 return NULL;
2565
3019eac3
DE
2566 /* If DWO files are in use, we can still find the DW_AT_stmt_list attribute
2567 in the stub for CUs, there's is no need to lookup the DWO file.
2568 However, that's not the case for TUs where DW_AT_stmt_list lives in the
2569 DWO file. */
2570 if (this_cu->is_debug_types)
fd820528 2571 init_cutu_and_read_dies (this_cu, 0, 0, dw2_get_file_names_reader, NULL);
3019eac3
DE
2572 else
2573 init_cutu_and_read_dies_simple (this_cu, dw2_get_file_names_reader, NULL);
dee91e82
DE
2574
2575 if (this_cu->v.quick->no_file_data)
2576 return NULL;
2577 return this_cu->v.quick->file_names;
9291a0cd
TT
2578}
2579
2580/* A helper for the "quick" functions which computes and caches the
7b9f3c50 2581 real path for a given file name from the line table. */
2fdf6df6 2582
9291a0cd 2583static const char *
7b9f3c50
DE
2584dw2_get_real_path (struct objfile *objfile,
2585 struct quick_file_names *qfn, int index)
9291a0cd 2586{
7b9f3c50
DE
2587 if (qfn->real_names == NULL)
2588 qfn->real_names = OBSTACK_CALLOC (&objfile->objfile_obstack,
2589 qfn->num_file_names, sizeof (char *));
9291a0cd 2590
7b9f3c50
DE
2591 if (qfn->real_names[index] == NULL)
2592 qfn->real_names[index] = gdb_realpath (qfn->file_names[index]);
9291a0cd 2593
7b9f3c50 2594 return qfn->real_names[index];
9291a0cd
TT
2595}
2596
2597static struct symtab *
2598dw2_find_last_source_symtab (struct objfile *objfile)
2599{
2600 int index;
ae2de4f8 2601
9291a0cd
TT
2602 dw2_setup (objfile);
2603 index = dwarf2_per_objfile->n_comp_units - 1;
a0f42c21 2604 return dw2_instantiate_symtab (dw2_get_cu (index));
9291a0cd
TT
2605}
2606
7b9f3c50
DE
2607/* Traversal function for dw2_forget_cached_source_info. */
2608
2609static int
2610dw2_free_cached_file_names (void **slot, void *info)
9291a0cd 2611{
7b9f3c50 2612 struct quick_file_names *file_data = (struct quick_file_names *) *slot;
9291a0cd 2613
7b9f3c50 2614 if (file_data->real_names)
9291a0cd 2615 {
7b9f3c50 2616 int i;
9291a0cd 2617
7b9f3c50 2618 for (i = 0; i < file_data->num_file_names; ++i)
9291a0cd 2619 {
7b9f3c50
DE
2620 xfree ((void*) file_data->real_names[i]);
2621 file_data->real_names[i] = NULL;
9291a0cd
TT
2622 }
2623 }
7b9f3c50
DE
2624
2625 return 1;
2626}
2627
2628static void
2629dw2_forget_cached_source_info (struct objfile *objfile)
2630{
2631 dw2_setup (objfile);
2632
2633 htab_traverse_noresize (dwarf2_per_objfile->quick_file_names_table,
2634 dw2_free_cached_file_names, NULL);
9291a0cd
TT
2635}
2636
f8eba3c6
TT
2637/* Helper function for dw2_map_symtabs_matching_filename that expands
2638 the symtabs and calls the iterator. */
2639
2640static int
2641dw2_map_expand_apply (struct objfile *objfile,
2642 struct dwarf2_per_cu_data *per_cu,
2643 const char *name,
2644 const char *full_path, const char *real_path,
2645 int (*callback) (struct symtab *, void *),
2646 void *data)
2647{
2648 struct symtab *last_made = objfile->symtabs;
2649
2650 /* Don't visit already-expanded CUs. */
2651 if (per_cu->v.quick->symtab)
2652 return 0;
2653
2654 /* This may expand more than one symtab, and we want to iterate over
2655 all of them. */
a0f42c21 2656 dw2_instantiate_symtab (per_cu);
f8eba3c6
TT
2657
2658 return iterate_over_some_symtabs (name, full_path, real_path, callback, data,
2659 objfile->symtabs, last_made);
2660}
2661
2662/* Implementation of the map_symtabs_matching_filename method. */
2663
9291a0cd 2664static int
f8eba3c6
TT
2665dw2_map_symtabs_matching_filename (struct objfile *objfile, const char *name,
2666 const char *full_path, const char *real_path,
2667 int (*callback) (struct symtab *, void *),
2668 void *data)
9291a0cd
TT
2669{
2670 int i;
c011a4f4 2671 const char *name_basename = lbasename (name);
4aac40c8
TT
2672 int name_len = strlen (name);
2673 int is_abs = IS_ABSOLUTE_PATH (name);
9291a0cd
TT
2674
2675 dw2_setup (objfile);
ae2de4f8 2676
1fd400ff 2677 for (i = 0; i < (dwarf2_per_objfile->n_comp_units
d467dd73 2678 + dwarf2_per_objfile->n_type_units); ++i)
9291a0cd
TT
2679 {
2680 int j;
e254ef6a 2681 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
7b9f3c50 2682 struct quick_file_names *file_data;
9291a0cd 2683
3d7bb9d9 2684 /* We only need to look at symtabs not already expanded. */
e254ef6a 2685 if (per_cu->v.quick->symtab)
9291a0cd
TT
2686 continue;
2687
7b9f3c50
DE
2688 file_data = dw2_get_file_names (objfile, per_cu);
2689 if (file_data == NULL)
9291a0cd
TT
2690 continue;
2691
7b9f3c50 2692 for (j = 0; j < file_data->num_file_names; ++j)
9291a0cd 2693 {
7b9f3c50 2694 const char *this_name = file_data->file_names[j];
9291a0cd 2695
4aac40c8
TT
2696 if (FILENAME_CMP (name, this_name) == 0
2697 || (!is_abs && compare_filenames_for_search (this_name,
2698 name, name_len)))
9291a0cd 2699 {
f8eba3c6
TT
2700 if (dw2_map_expand_apply (objfile, per_cu,
2701 name, full_path, real_path,
2702 callback, data))
2703 return 1;
4aac40c8 2704 }
9291a0cd 2705
c011a4f4
DE
2706 /* Before we invoke realpath, which can get expensive when many
2707 files are involved, do a quick comparison of the basenames. */
2708 if (! basenames_may_differ
2709 && FILENAME_CMP (lbasename (this_name), name_basename) != 0)
2710 continue;
2711
9291a0cd
TT
2712 if (full_path != NULL)
2713 {
7b9f3c50
DE
2714 const char *this_real_name = dw2_get_real_path (objfile,
2715 file_data, j);
9291a0cd 2716
7b9f3c50 2717 if (this_real_name != NULL
4aac40c8
TT
2718 && (FILENAME_CMP (full_path, this_real_name) == 0
2719 || (!is_abs
2720 && compare_filenames_for_search (this_real_name,
2721 name, name_len))))
9291a0cd 2722 {
f8eba3c6
TT
2723 if (dw2_map_expand_apply (objfile, per_cu,
2724 name, full_path, real_path,
2725 callback, data))
2726 return 1;
9291a0cd
TT
2727 }
2728 }
2729
2730 if (real_path != NULL)
2731 {
7b9f3c50
DE
2732 const char *this_real_name = dw2_get_real_path (objfile,
2733 file_data, j);
9291a0cd 2734
7b9f3c50 2735 if (this_real_name != NULL
4aac40c8
TT
2736 && (FILENAME_CMP (real_path, this_real_name) == 0
2737 || (!is_abs
2738 && compare_filenames_for_search (this_real_name,
2739 name, name_len))))
9291a0cd 2740 {
f8eba3c6
TT
2741 if (dw2_map_expand_apply (objfile, per_cu,
2742 name, full_path, real_path,
2743 callback, data))
2744 return 1;
9291a0cd
TT
2745 }
2746 }
2747 }
2748 }
2749
9291a0cd
TT
2750 return 0;
2751}
2752
2753static struct symtab *
2754dw2_lookup_symbol (struct objfile *objfile, int block_index,
2755 const char *name, domain_enum domain)
2756{
774b6a14 2757 /* We do all the work in the pre_expand_symtabs_matching hook
9291a0cd
TT
2758 instead. */
2759 return NULL;
2760}
2761
2762/* A helper function that expands all symtabs that hold an object
156942c7
DE
2763 named NAME. If WANT_SPECIFIC_BLOCK is non-zero, only look for
2764 symbols in block BLOCK_KIND. */
2fdf6df6 2765
9291a0cd 2766static void
156942c7
DE
2767dw2_do_expand_symtabs_matching (struct objfile *objfile,
2768 int want_specific_block,
2769 enum block_enum block_kind,
2770 const char *name, domain_enum domain)
9291a0cd 2771{
156942c7
DE
2772 struct mapped_index *index;
2773
9291a0cd
TT
2774 dw2_setup (objfile);
2775
156942c7
DE
2776 index = dwarf2_per_objfile->index_table;
2777
ae2de4f8 2778 /* index_table is NULL if OBJF_READNOW. */
156942c7 2779 if (index)
9291a0cd
TT
2780 {
2781 offset_type *vec;
2782
156942c7 2783 if (find_slot_in_mapped_hash (index, name, &vec))
9291a0cd
TT
2784 {
2785 offset_type i, len = MAYBE_SWAP (*vec);
2786 for (i = 0; i < len; ++i)
2787 {
156942c7
DE
2788 offset_type cu_index_and_attrs = MAYBE_SWAP (vec[i + 1]);
2789 offset_type cu_index = GDB_INDEX_CU_VALUE (cu_index_and_attrs);
e254ef6a 2790 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (cu_index);
156942c7
DE
2791 int want_static = block_kind != GLOBAL_BLOCK;
2792 /* This value is only valid for index versions >= 7. */
2793 int is_static = GDB_INDEX_SYMBOL_STATIC_VALUE (cu_index_and_attrs);
2794 gdb_index_symbol_kind symbol_kind =
2795 GDB_INDEX_SYMBOL_KIND_VALUE (cu_index_and_attrs);
2796
2797 if (want_specific_block
2798 && index->version >= 7
2799 && want_static != is_static)
2800 continue;
2801
2802 /* Only check the symbol's kind if it has one.
2803 Indices prior to version 7 don't record it. */
2804 if (index->version >= 7)
2805 {
2806 switch (domain)
2807 {
2808 case VAR_DOMAIN:
2809 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_VARIABLE
2810 && symbol_kind != GDB_INDEX_SYMBOL_KIND_FUNCTION
2811 /* Some types are also in VAR_DOMAIN. */
2812 && symbol_kind != GDB_INDEX_SYMBOL_KIND_TYPE)
2813 continue;
2814 break;
2815 case STRUCT_DOMAIN:
2816 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_TYPE)
2817 continue;
2818 break;
2819 case LABEL_DOMAIN:
2820 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_OTHER)
2821 continue;
2822 break;
2823 default:
2824 break;
2825 }
2826 }
1fd400ff 2827
a0f42c21 2828 dw2_instantiate_symtab (per_cu);
9291a0cd
TT
2829 }
2830 }
2831 }
2832}
2833
774b6a14
TT
2834static void
2835dw2_pre_expand_symtabs_matching (struct objfile *objfile,
8903c50d 2836 enum block_enum block_kind, const char *name,
774b6a14 2837 domain_enum domain)
9291a0cd 2838{
156942c7 2839 dw2_do_expand_symtabs_matching (objfile, 1, block_kind, name, domain);
9291a0cd
TT
2840}
2841
2842static void
2843dw2_print_stats (struct objfile *objfile)
2844{
2845 int i, count;
2846
2847 dw2_setup (objfile);
2848 count = 0;
1fd400ff 2849 for (i = 0; i < (dwarf2_per_objfile->n_comp_units
d467dd73 2850 + dwarf2_per_objfile->n_type_units); ++i)
9291a0cd 2851 {
e254ef6a 2852 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
9291a0cd 2853
e254ef6a 2854 if (!per_cu->v.quick->symtab)
9291a0cd
TT
2855 ++count;
2856 }
2857 printf_filtered (_(" Number of unread CUs: %d\n"), count);
2858}
2859
2860static void
2861dw2_dump (struct objfile *objfile)
2862{
2863 /* Nothing worth printing. */
2864}
2865
2866static void
2867dw2_relocate (struct objfile *objfile, struct section_offsets *new_offsets,
2868 struct section_offsets *delta)
2869{
2870 /* There's nothing to relocate here. */
2871}
2872
2873static void
2874dw2_expand_symtabs_for_function (struct objfile *objfile,
2875 const char *func_name)
2876{
156942c7
DE
2877 /* Note: It doesn't matter what we pass for block_kind here. */
2878 dw2_do_expand_symtabs_matching (objfile, 0, GLOBAL_BLOCK, func_name,
2879 VAR_DOMAIN);
9291a0cd
TT
2880}
2881
2882static void
2883dw2_expand_all_symtabs (struct objfile *objfile)
2884{
2885 int i;
2886
2887 dw2_setup (objfile);
1fd400ff
TT
2888
2889 for (i = 0; i < (dwarf2_per_objfile->n_comp_units
d467dd73 2890 + dwarf2_per_objfile->n_type_units); ++i)
9291a0cd 2891 {
e254ef6a 2892 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
9291a0cd 2893
a0f42c21 2894 dw2_instantiate_symtab (per_cu);
9291a0cd
TT
2895 }
2896}
2897
2898static void
2899dw2_expand_symtabs_with_filename (struct objfile *objfile,
2900 const char *filename)
2901{
2902 int i;
2903
2904 dw2_setup (objfile);
d4637a04
DE
2905
2906 /* We don't need to consider type units here.
2907 This is only called for examining code, e.g. expand_line_sal.
2908 There can be an order of magnitude (or more) more type units
2909 than comp units, and we avoid them if we can. */
2910
2911 for (i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
9291a0cd
TT
2912 {
2913 int j;
e254ef6a 2914 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
7b9f3c50 2915 struct quick_file_names *file_data;
9291a0cd 2916
3d7bb9d9 2917 /* We only need to look at symtabs not already expanded. */
e254ef6a 2918 if (per_cu->v.quick->symtab)
9291a0cd
TT
2919 continue;
2920
7b9f3c50
DE
2921 file_data = dw2_get_file_names (objfile, per_cu);
2922 if (file_data == NULL)
9291a0cd
TT
2923 continue;
2924
7b9f3c50 2925 for (j = 0; j < file_data->num_file_names; ++j)
9291a0cd 2926 {
7b9f3c50 2927 const char *this_name = file_data->file_names[j];
1ef75ecc 2928 if (FILENAME_CMP (this_name, filename) == 0)
9291a0cd 2929 {
a0f42c21 2930 dw2_instantiate_symtab (per_cu);
9291a0cd
TT
2931 break;
2932 }
2933 }
2934 }
2935}
2936
356d9f9d
TT
2937/* A helper function for dw2_find_symbol_file that finds the primary
2938 file name for a given CU. This is a die_reader_func. */
2939
2940static void
2941dw2_get_primary_filename_reader (const struct die_reader_specs *reader,
2942 gdb_byte *info_ptr,
2943 struct die_info *comp_unit_die,
2944 int has_children,
2945 void *data)
2946{
2947 const char **result_ptr = data;
2948 struct dwarf2_cu *cu = reader->cu;
2949 struct attribute *attr;
2950
2951 attr = dwarf2_attr (comp_unit_die, DW_AT_name, cu);
2952 if (attr == NULL)
2953 *result_ptr = NULL;
2954 else
2955 *result_ptr = DW_STRING (attr);
2956}
2957
dd786858 2958static const char *
9291a0cd
TT
2959dw2_find_symbol_file (struct objfile *objfile, const char *name)
2960{
e254ef6a 2961 struct dwarf2_per_cu_data *per_cu;
9291a0cd 2962 offset_type *vec;
7b9f3c50 2963 struct quick_file_names *file_data;
356d9f9d 2964 const char *filename;
9291a0cd
TT
2965
2966 dw2_setup (objfile);
2967
ae2de4f8 2968 /* index_table is NULL if OBJF_READNOW. */
9291a0cd 2969 if (!dwarf2_per_objfile->index_table)
96408a79
SA
2970 {
2971 struct symtab *s;
2972
d790cf0a
DE
2973 ALL_OBJFILE_PRIMARY_SYMTABS (objfile, s)
2974 {
2975 struct blockvector *bv = BLOCKVECTOR (s);
2976 const struct block *block = BLOCKVECTOR_BLOCK (bv, GLOBAL_BLOCK);
2977 struct symbol *sym = lookup_block_symbol (block, name, VAR_DOMAIN);
2978
2979 if (sym)
2980 return sym->symtab->filename;
2981 }
96408a79
SA
2982 return NULL;
2983 }
9291a0cd
TT
2984
2985 if (!find_slot_in_mapped_hash (dwarf2_per_objfile->index_table,
2986 name, &vec))
2987 return NULL;
2988
2989 /* Note that this just looks at the very first one named NAME -- but
2990 actually we are looking for a function. find_main_filename
2991 should be rewritten so that it doesn't require a custom hook. It
2992 could just use the ordinary symbol tables. */
2993 /* vec[0] is the length, which must always be >0. */
156942c7 2994 per_cu = dw2_get_cu (GDB_INDEX_CU_VALUE (MAYBE_SWAP (vec[1])));
9291a0cd 2995
356d9f9d
TT
2996 if (per_cu->v.quick->symtab != NULL)
2997 return per_cu->v.quick->symtab->filename;
2998
663b969e
TT
2999 init_cutu_and_read_dies (per_cu, 0, 0, dw2_get_primary_filename_reader,
3000 &filename);
9291a0cd 3001
356d9f9d 3002 return filename;
9291a0cd
TT
3003}
3004
3005static void
40658b94
PH
3006dw2_map_matching_symbols (const char * name, domain_enum namespace,
3007 struct objfile *objfile, int global,
3008 int (*callback) (struct block *,
3009 struct symbol *, void *),
2edb89d3
JK
3010 void *data, symbol_compare_ftype *match,
3011 symbol_compare_ftype *ordered_compare)
9291a0cd 3012{
40658b94 3013 /* Currently unimplemented; used for Ada. The function can be called if the
a9e6a4bb
JK
3014 current language is Ada for a non-Ada objfile using GNU index. As Ada
3015 does not look for non-Ada symbols this function should just return. */
9291a0cd
TT
3016}
3017
3018static void
f8eba3c6
TT
3019dw2_expand_symtabs_matching
3020 (struct objfile *objfile,
3021 int (*file_matcher) (const char *, void *),
e078317b 3022 int (*name_matcher) (const char *, void *),
f8eba3c6
TT
3023 enum search_domain kind,
3024 void *data)
9291a0cd
TT
3025{
3026 int i;
3027 offset_type iter;
4b5246aa 3028 struct mapped_index *index;
9291a0cd
TT
3029
3030 dw2_setup (objfile);
ae2de4f8
DE
3031
3032 /* index_table is NULL if OBJF_READNOW. */
9291a0cd
TT
3033 if (!dwarf2_per_objfile->index_table)
3034 return;
4b5246aa 3035 index = dwarf2_per_objfile->index_table;
9291a0cd 3036
7b08b9eb 3037 if (file_matcher != NULL)
24c79950
TT
3038 {
3039 struct cleanup *cleanup;
3040 htab_t visited_found, visited_not_found;
3041
3042 visited_found = htab_create_alloc (10,
3043 htab_hash_pointer, htab_eq_pointer,
3044 NULL, xcalloc, xfree);
3045 cleanup = make_cleanup_htab_delete (visited_found);
3046 visited_not_found = htab_create_alloc (10,
3047 htab_hash_pointer, htab_eq_pointer,
3048 NULL, xcalloc, xfree);
3049 make_cleanup_htab_delete (visited_not_found);
3050
3051 for (i = 0; i < (dwarf2_per_objfile->n_comp_units
3052 + dwarf2_per_objfile->n_type_units); ++i)
3053 {
3054 int j;
3055 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
3056 struct quick_file_names *file_data;
3057 void **slot;
7b08b9eb 3058
24c79950 3059 per_cu->v.quick->mark = 0;
3d7bb9d9 3060
24c79950
TT
3061 /* We only need to look at symtabs not already expanded. */
3062 if (per_cu->v.quick->symtab)
3063 continue;
7b08b9eb 3064
24c79950
TT
3065 file_data = dw2_get_file_names (objfile, per_cu);
3066 if (file_data == NULL)
3067 continue;
7b08b9eb 3068
24c79950
TT
3069 if (htab_find (visited_not_found, file_data) != NULL)
3070 continue;
3071 else if (htab_find (visited_found, file_data) != NULL)
3072 {
3073 per_cu->v.quick->mark = 1;
3074 continue;
3075 }
3076
3077 for (j = 0; j < file_data->num_file_names; ++j)
3078 {
3079 if (file_matcher (file_data->file_names[j], data))
3080 {
3081 per_cu->v.quick->mark = 1;
3082 break;
3083 }
3084 }
3085
3086 slot = htab_find_slot (per_cu->v.quick->mark
3087 ? visited_found
3088 : visited_not_found,
3089 file_data, INSERT);
3090 *slot = file_data;
3091 }
3092
3093 do_cleanups (cleanup);
3094 }
9291a0cd 3095
3876f04e 3096 for (iter = 0; iter < index->symbol_table_slots; ++iter)
9291a0cd
TT
3097 {
3098 offset_type idx = 2 * iter;
3099 const char *name;
3100 offset_type *vec, vec_len, vec_idx;
3101
3876f04e 3102 if (index->symbol_table[idx] == 0 && index->symbol_table[idx + 1] == 0)
9291a0cd
TT
3103 continue;
3104
3876f04e 3105 name = index->constant_pool + MAYBE_SWAP (index->symbol_table[idx]);
9291a0cd 3106
e078317b 3107 if (! (*name_matcher) (name, data))
9291a0cd
TT
3108 continue;
3109
3110 /* The name was matched, now expand corresponding CUs that were
3111 marked. */
4b5246aa 3112 vec = (offset_type *) (index->constant_pool
3876f04e 3113 + MAYBE_SWAP (index->symbol_table[idx + 1]));
9291a0cd
TT
3114 vec_len = MAYBE_SWAP (vec[0]);
3115 for (vec_idx = 0; vec_idx < vec_len; ++vec_idx)
3116 {
e254ef6a 3117 struct dwarf2_per_cu_data *per_cu;
156942c7
DE
3118 offset_type cu_index_and_attrs = MAYBE_SWAP (vec[vec_idx + 1]);
3119 gdb_index_symbol_kind symbol_kind =
3120 GDB_INDEX_SYMBOL_KIND_VALUE (cu_index_and_attrs);
3121 int cu_index = GDB_INDEX_CU_VALUE (cu_index_and_attrs);
3122
3123 /* Don't crash on bad data. */
3124 if (cu_index >= (dwarf2_per_objfile->n_comp_units
3125 + dwarf2_per_objfile->n_comp_units))
3126 continue;
1fd400ff 3127
156942c7
DE
3128 /* Only check the symbol's kind if it has one.
3129 Indices prior to version 7 don't record it. */
3130 if (index->version >= 7)
3131 {
3132 switch (kind)
3133 {
3134 case VARIABLES_DOMAIN:
3135 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_VARIABLE)
3136 continue;
3137 break;
3138 case FUNCTIONS_DOMAIN:
3139 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_FUNCTION)
3140 continue;
3141 break;
3142 case TYPES_DOMAIN:
3143 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_TYPE)
3144 continue;
3145 break;
3146 default:
3147 break;
3148 }
3149 }
3150
3151 per_cu = dw2_get_cu (cu_index);
7b08b9eb 3152 if (file_matcher == NULL || per_cu->v.quick->mark)
a0f42c21 3153 dw2_instantiate_symtab (per_cu);
9291a0cd
TT
3154 }
3155 }
3156}
3157
9703b513
TT
3158/* A helper for dw2_find_pc_sect_symtab which finds the most specific
3159 symtab. */
3160
3161static struct symtab *
3162recursively_find_pc_sect_symtab (struct symtab *symtab, CORE_ADDR pc)
3163{
3164 int i;
3165
3166 if (BLOCKVECTOR (symtab) != NULL
3167 && blockvector_contains_pc (BLOCKVECTOR (symtab), pc))
3168 return symtab;
3169
a3ec0bb1
DE
3170 if (symtab->includes == NULL)
3171 return NULL;
3172
9703b513
TT
3173 for (i = 0; symtab->includes[i]; ++i)
3174 {
a3ec0bb1 3175 struct symtab *s = symtab->includes[i];
9703b513
TT
3176
3177 s = recursively_find_pc_sect_symtab (s, pc);
3178 if (s != NULL)
3179 return s;
3180 }
3181
3182 return NULL;
3183}
3184
9291a0cd
TT
3185static struct symtab *
3186dw2_find_pc_sect_symtab (struct objfile *objfile,
3187 struct minimal_symbol *msymbol,
3188 CORE_ADDR pc,
3189 struct obj_section *section,
3190 int warn_if_readin)
3191{
3192 struct dwarf2_per_cu_data *data;
9703b513 3193 struct symtab *result;
9291a0cd
TT
3194
3195 dw2_setup (objfile);
3196
3197 if (!objfile->psymtabs_addrmap)
3198 return NULL;
3199
3200 data = addrmap_find (objfile->psymtabs_addrmap, pc);
3201 if (!data)
3202 return NULL;
3203
3204 if (warn_if_readin && data->v.quick->symtab)
abebb8b0 3205 warning (_("(Internal error: pc %s in read in CU, but not in symtab.)"),
9291a0cd
TT
3206 paddress (get_objfile_arch (objfile), pc));
3207
9703b513
TT
3208 result = recursively_find_pc_sect_symtab (dw2_instantiate_symtab (data), pc);
3209 gdb_assert (result != NULL);
3210 return result;
9291a0cd
TT
3211}
3212
9291a0cd 3213static void
44b13c5a 3214dw2_map_symbol_filenames (struct objfile *objfile, symbol_filename_ftype *fun,
74e2f255 3215 void *data, int need_fullname)
9291a0cd
TT
3216{
3217 int i;
24c79950
TT
3218 struct cleanup *cleanup;
3219 htab_t visited = htab_create_alloc (10, htab_hash_pointer, htab_eq_pointer,
3220 NULL, xcalloc, xfree);
9291a0cd 3221
24c79950 3222 cleanup = make_cleanup_htab_delete (visited);
9291a0cd 3223 dw2_setup (objfile);
ae2de4f8 3224
24c79950
TT
3225 /* We can ignore file names coming from already-expanded CUs. */
3226 for (i = 0; i < (dwarf2_per_objfile->n_comp_units
3227 + dwarf2_per_objfile->n_type_units); ++i)
3228 {
3229 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
3230
3231 if (per_cu->v.quick->symtab)
3232 {
3233 void **slot = htab_find_slot (visited, per_cu->v.quick->file_names,
3234 INSERT);
3235
3236 *slot = per_cu->v.quick->file_names;
3237 }
3238 }
3239
1fd400ff 3240 for (i = 0; i < (dwarf2_per_objfile->n_comp_units
d467dd73 3241 + dwarf2_per_objfile->n_type_units); ++i)
9291a0cd
TT
3242 {
3243 int j;
e254ef6a 3244 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
7b9f3c50 3245 struct quick_file_names *file_data;
24c79950 3246 void **slot;
9291a0cd 3247
3d7bb9d9 3248 /* We only need to look at symtabs not already expanded. */
e254ef6a 3249 if (per_cu->v.quick->symtab)
9291a0cd
TT
3250 continue;
3251
7b9f3c50
DE
3252 file_data = dw2_get_file_names (objfile, per_cu);
3253 if (file_data == NULL)
9291a0cd
TT
3254 continue;
3255
24c79950
TT
3256 slot = htab_find_slot (visited, file_data, INSERT);
3257 if (*slot)
3258 {
3259 /* Already visited. */
3260 continue;
3261 }
3262 *slot = file_data;
3263
7b9f3c50 3264 for (j = 0; j < file_data->num_file_names; ++j)
9291a0cd 3265 {
74e2f255
DE
3266 const char *this_real_name;
3267
3268 if (need_fullname)
3269 this_real_name = dw2_get_real_path (objfile, file_data, j);
3270 else
3271 this_real_name = NULL;
7b9f3c50 3272 (*fun) (file_data->file_names[j], this_real_name, data);
9291a0cd
TT
3273 }
3274 }
24c79950
TT
3275
3276 do_cleanups (cleanup);
9291a0cd
TT
3277}
3278
3279static int
3280dw2_has_symbols (struct objfile *objfile)
3281{
3282 return 1;
3283}
3284
3285const struct quick_symbol_functions dwarf2_gdb_index_functions =
3286{
3287 dw2_has_symbols,
3288 dw2_find_last_source_symtab,
3289 dw2_forget_cached_source_info,
f8eba3c6 3290 dw2_map_symtabs_matching_filename,
9291a0cd 3291 dw2_lookup_symbol,
774b6a14 3292 dw2_pre_expand_symtabs_matching,
9291a0cd
TT
3293 dw2_print_stats,
3294 dw2_dump,
3295 dw2_relocate,
3296 dw2_expand_symtabs_for_function,
3297 dw2_expand_all_symtabs,
3298 dw2_expand_symtabs_with_filename,
3299 dw2_find_symbol_file,
40658b94 3300 dw2_map_matching_symbols,
9291a0cd
TT
3301 dw2_expand_symtabs_matching,
3302 dw2_find_pc_sect_symtab,
9291a0cd
TT
3303 dw2_map_symbol_filenames
3304};
3305
3306/* Initialize for reading DWARF for this objfile. Return 0 if this
3307 file will use psymtabs, or 1 if using the GNU index. */
3308
3309int
3310dwarf2_initialize_objfile (struct objfile *objfile)
3311{
3312 /* If we're about to read full symbols, don't bother with the
3313 indices. In this case we also don't care if some other debug
3314 format is making psymtabs, because they are all about to be
3315 expanded anyway. */
3316 if ((objfile->flags & OBJF_READNOW))
3317 {
3318 int i;
3319
3320 dwarf2_per_objfile->using_index = 1;
3321 create_all_comp_units (objfile);
0e50663e 3322 create_all_type_units (objfile);
7b9f3c50
DE
3323 dwarf2_per_objfile->quick_file_names_table =
3324 create_quick_file_names_table (dwarf2_per_objfile->n_comp_units);
9291a0cd 3325
1fd400ff 3326 for (i = 0; i < (dwarf2_per_objfile->n_comp_units
d467dd73 3327 + dwarf2_per_objfile->n_type_units); ++i)
9291a0cd 3328 {
e254ef6a 3329 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
9291a0cd 3330
e254ef6a
DE
3331 per_cu->v.quick = OBSTACK_ZALLOC (&objfile->objfile_obstack,
3332 struct dwarf2_per_cu_quick_data);
9291a0cd
TT
3333 }
3334
3335 /* Return 1 so that gdb sees the "quick" functions. However,
3336 these functions will be no-ops because we will have expanded
3337 all symtabs. */
3338 return 1;
3339 }
3340
3341 if (dwarf2_read_index (objfile))
3342 return 1;
3343
9291a0cd
TT
3344 return 0;
3345}
3346
3347\f
3348
dce234bc
PP
3349/* Build a partial symbol table. */
3350
3351void
f29dff0a 3352dwarf2_build_psymtabs (struct objfile *objfile)
dce234bc 3353{
f29dff0a 3354 if (objfile->global_psymbols.size == 0 && objfile->static_psymbols.size == 0)
c906108c
SS
3355 {
3356 init_psymbol_list (objfile, 1024);
3357 }
3358
d146bf1e 3359 dwarf2_build_psymtabs_hard (objfile);
c906108c 3360}
c906108c 3361
45452591
DE
3362/* Return TRUE if OFFSET is within CU_HEADER. */
3363
3364static inline int
b64f50a1 3365offset_in_cu_p (const struct comp_unit_head *cu_header, sect_offset offset)
45452591 3366{
b64f50a1
JK
3367 sect_offset bottom = { cu_header->offset.sect_off };
3368 sect_offset top = { (cu_header->offset.sect_off + cu_header->length
3369 + cu_header->initial_length_size) };
9a619af0 3370
b64f50a1 3371 return (offset.sect_off >= bottom.sect_off && offset.sect_off < top.sect_off);
45452591
DE
3372}
3373
3b80fe9b
DE
3374/* Find the base address of the compilation unit for range lists and
3375 location lists. It will normally be specified by DW_AT_low_pc.
3376 In DWARF-3 draft 4, the base address could be overridden by
3377 DW_AT_entry_pc. It's been removed, but GCC still uses this for
3378 compilation units with discontinuous ranges. */
3379
3380static void
3381dwarf2_find_base_address (struct die_info *die, struct dwarf2_cu *cu)
3382{
3383 struct attribute *attr;
3384
3385 cu->base_known = 0;
3386 cu->base_address = 0;
3387
3388 attr = dwarf2_attr (die, DW_AT_entry_pc, cu);
3389 if (attr)
3390 {
3391 cu->base_address = DW_ADDR (attr);
3392 cu->base_known = 1;
3393 }
3394 else
3395 {
3396 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
3397 if (attr)
3398 {
3399 cu->base_address = DW_ADDR (attr);
3400 cu->base_known = 1;
3401 }
3402 }
3403}
3404
93311388
DE
3405/* Read in the comp unit header information from the debug_info at info_ptr.
3406 NOTE: This leaves members offset, first_die_offset to be filled in
3407 by the caller. */
107d2387 3408
fe1b8b76 3409static gdb_byte *
107d2387 3410read_comp_unit_head (struct comp_unit_head *cu_header,
fe1b8b76 3411 gdb_byte *info_ptr, bfd *abfd)
107d2387
AC
3412{
3413 int signed_addr;
891d2f0b 3414 unsigned int bytes_read;
c764a876
DE
3415
3416 cu_header->length = read_initial_length (abfd, info_ptr, &bytes_read);
3417 cu_header->initial_length_size = bytes_read;
3418 cu_header->offset_size = (bytes_read == 4) ? 4 : 8;
613e1657 3419 info_ptr += bytes_read;
107d2387
AC
3420 cu_header->version = read_2_bytes (abfd, info_ptr);
3421 info_ptr += 2;
b64f50a1
JK
3422 cu_header->abbrev_offset.sect_off = read_offset (abfd, info_ptr, cu_header,
3423 &bytes_read);
613e1657 3424 info_ptr += bytes_read;
107d2387
AC
3425 cu_header->addr_size = read_1_byte (abfd, info_ptr);
3426 info_ptr += 1;
3427 signed_addr = bfd_get_sign_extend_vma (abfd);
3428 if (signed_addr < 0)
8e65ff28 3429 internal_error (__FILE__, __LINE__,
e2e0b3e5 3430 _("read_comp_unit_head: dwarf from non elf file"));
107d2387 3431 cu_header->signed_addr_p = signed_addr;
c764a876 3432
107d2387
AC
3433 return info_ptr;
3434}
3435
9ff913ba
DE
3436/* Subroutine of read_and_check_comp_unit_head and
3437 read_and_check_type_unit_head to simplify them.
3438 Perform various error checking on the header. */
3439
3440static void
3441error_check_comp_unit_head (struct comp_unit_head *header,
4bdcc0c1
DE
3442 struct dwarf2_section_info *section,
3443 struct dwarf2_section_info *abbrev_section)
9ff913ba
DE
3444{
3445 bfd *abfd = section->asection->owner;
3446 const char *filename = bfd_get_filename (abfd);
3447
3448 if (header->version != 2 && header->version != 3 && header->version != 4)
3449 error (_("Dwarf Error: wrong version in compilation unit header "
3450 "(is %d, should be 2, 3, or 4) [in module %s]"), header->version,
3451 filename);
3452
b64f50a1 3453 if (header->abbrev_offset.sect_off
9ff913ba
DE
3454 >= dwarf2_section_size (dwarf2_per_objfile->objfile,
3455 &dwarf2_per_objfile->abbrev))
3456 error (_("Dwarf Error: bad offset (0x%lx) in compilation unit header "
3457 "(offset 0x%lx + 6) [in module %s]"),
b64f50a1 3458 (long) header->abbrev_offset.sect_off, (long) header->offset.sect_off,
9ff913ba
DE
3459 filename);
3460
3461 /* Cast to unsigned long to use 64-bit arithmetic when possible to
3462 avoid potential 32-bit overflow. */
b64f50a1 3463 if (((unsigned long) header->offset.sect_off
9ff913ba
DE
3464 + header->length + header->initial_length_size)
3465 > section->size)
3466 error (_("Dwarf Error: bad length (0x%lx) in compilation unit header "
3467 "(offset 0x%lx + 0) [in module %s]"),
b64f50a1 3468 (long) header->length, (long) header->offset.sect_off,
9ff913ba
DE
3469 filename);
3470}
3471
3472/* Read in a CU/TU header and perform some basic error checking.
3473 The contents of the header are stored in HEADER.
3474 The result is a pointer to the start of the first DIE. */
adabb602 3475
fe1b8b76 3476static gdb_byte *
9ff913ba
DE
3477read_and_check_comp_unit_head (struct comp_unit_head *header,
3478 struct dwarf2_section_info *section,
4bdcc0c1 3479 struct dwarf2_section_info *abbrev_section,
9ff913ba
DE
3480 gdb_byte *info_ptr,
3481 int is_debug_types_section)
72bf9492 3482{
fe1b8b76 3483 gdb_byte *beg_of_comp_unit = info_ptr;
9ff913ba 3484 bfd *abfd = section->asection->owner;
72bf9492 3485
b64f50a1 3486 header->offset.sect_off = beg_of_comp_unit - section->buffer;
adabb602 3487
72bf9492
DJ
3488 info_ptr = read_comp_unit_head (header, info_ptr, abfd);
3489
460c1c54
CC
3490 /* If we're reading a type unit, skip over the signature and
3491 type_offset fields. */
b0df02fd 3492 if (is_debug_types_section)
460c1c54
CC
3493 info_ptr += 8 /*signature*/ + header->offset_size;
3494
b64f50a1 3495 header->first_die_offset.cu_off = info_ptr - beg_of_comp_unit;
adabb602 3496
4bdcc0c1 3497 error_check_comp_unit_head (header, section, abbrev_section);
72bf9492
DJ
3498
3499 return info_ptr;
3500}
3501
348e048f
DE
3502/* Read in the types comp unit header information from .debug_types entry at
3503 types_ptr. The result is a pointer to one past the end of the header. */
3504
3505static gdb_byte *
9ff913ba
DE
3506read_and_check_type_unit_head (struct comp_unit_head *header,
3507 struct dwarf2_section_info *section,
4bdcc0c1 3508 struct dwarf2_section_info *abbrev_section,
9ff913ba 3509 gdb_byte *info_ptr,
dee91e82
DE
3510 ULONGEST *signature,
3511 cu_offset *type_offset_in_tu)
348e048f 3512{
9ff913ba
DE
3513 gdb_byte *beg_of_comp_unit = info_ptr;
3514 bfd *abfd = section->asection->owner;
348e048f 3515
b64f50a1 3516 header->offset.sect_off = beg_of_comp_unit - section->buffer;
348e048f 3517
9ff913ba 3518 info_ptr = read_comp_unit_head (header, info_ptr, abfd);
348e048f 3519
9ff913ba
DE
3520 /* If we're reading a type unit, skip over the signature and
3521 type_offset fields. */
3522 if (signature != NULL)
3523 *signature = read_8_bytes (abfd, info_ptr);
3524 info_ptr += 8;
dee91e82
DE
3525 if (type_offset_in_tu != NULL)
3526 type_offset_in_tu->cu_off = read_offset_1 (abfd, info_ptr,
3527 header->offset_size);
9ff913ba
DE
3528 info_ptr += header->offset_size;
3529
b64f50a1 3530 header->first_die_offset.cu_off = info_ptr - beg_of_comp_unit;
348e048f 3531
4bdcc0c1 3532 error_check_comp_unit_head (header, section, abbrev_section);
9ff913ba
DE
3533
3534 return info_ptr;
348e048f
DE
3535}
3536
aaa75496
JB
3537/* Allocate a new partial symtab for file named NAME and mark this new
3538 partial symtab as being an include of PST. */
3539
3540static void
3541dwarf2_create_include_psymtab (char *name, struct partial_symtab *pst,
3542 struct objfile *objfile)
3543{
3544 struct partial_symtab *subpst = allocate_psymtab (name, objfile);
3545
3546 subpst->section_offsets = pst->section_offsets;
3547 subpst->textlow = 0;
3548 subpst->texthigh = 0;
3549
3550 subpst->dependencies = (struct partial_symtab **)
3551 obstack_alloc (&objfile->objfile_obstack,
3552 sizeof (struct partial_symtab *));
3553 subpst->dependencies[0] = pst;
3554 subpst->number_of_dependencies = 1;
3555
3556 subpst->globals_offset = 0;
3557 subpst->n_global_syms = 0;
3558 subpst->statics_offset = 0;
3559 subpst->n_static_syms = 0;
3560 subpst->symtab = NULL;
3561 subpst->read_symtab = pst->read_symtab;
3562 subpst->readin = 0;
3563
3564 /* No private part is necessary for include psymtabs. This property
3565 can be used to differentiate between such include psymtabs and
10b3939b 3566 the regular ones. */
58a9656e 3567 subpst->read_symtab_private = NULL;
aaa75496
JB
3568}
3569
3570/* Read the Line Number Program data and extract the list of files
3571 included by the source file represented by PST. Build an include
d85a05f0 3572 partial symtab for each of these included files. */
aaa75496
JB
3573
3574static void
3575dwarf2_build_include_psymtabs (struct dwarf2_cu *cu,
dee91e82
DE
3576 struct die_info *die,
3577 struct partial_symtab *pst)
aaa75496 3578{
d85a05f0
DJ
3579 struct line_header *lh = NULL;
3580 struct attribute *attr;
aaa75496 3581
d85a05f0
DJ
3582 attr = dwarf2_attr (die, DW_AT_stmt_list, cu);
3583 if (attr)
3019eac3 3584 lh = dwarf_decode_line_header (DW_UNSND (attr), cu);
aaa75496
JB
3585 if (lh == NULL)
3586 return; /* No linetable, so no includes. */
3587
c6da4cef 3588 /* NOTE: pst->dirname is DW_AT_comp_dir (if present). */
f3f5162e 3589 dwarf_decode_lines (lh, pst->dirname, cu, pst, 1);
aaa75496
JB
3590
3591 free_line_header (lh);
3592}
3593
348e048f 3594static hashval_t
52dc124a 3595hash_signatured_type (const void *item)
348e048f 3596{
52dc124a 3597 const struct signatured_type *sig_type = item;
9a619af0 3598
348e048f 3599 /* This drops the top 32 bits of the signature, but is ok for a hash. */
52dc124a 3600 return sig_type->signature;
348e048f
DE
3601}
3602
3603static int
52dc124a 3604eq_signatured_type (const void *item_lhs, const void *item_rhs)
348e048f
DE
3605{
3606 const struct signatured_type *lhs = item_lhs;
3607 const struct signatured_type *rhs = item_rhs;
9a619af0 3608
348e048f
DE
3609 return lhs->signature == rhs->signature;
3610}
3611
1fd400ff
TT
3612/* Allocate a hash table for signatured types. */
3613
3614static htab_t
673bfd45 3615allocate_signatured_type_table (struct objfile *objfile)
1fd400ff
TT
3616{
3617 return htab_create_alloc_ex (41,
52dc124a
DE
3618 hash_signatured_type,
3619 eq_signatured_type,
1fd400ff
TT
3620 NULL,
3621 &objfile->objfile_obstack,
3622 hashtab_obstack_allocate,
3623 dummy_obstack_deallocate);
3624}
3625
d467dd73 3626/* A helper function to add a signatured type CU to a table. */
1fd400ff
TT
3627
3628static int
d467dd73 3629add_signatured_type_cu_to_table (void **slot, void *datum)
1fd400ff
TT
3630{
3631 struct signatured_type *sigt = *slot;
3632 struct dwarf2_per_cu_data ***datap = datum;
3633
3634 **datap = &sigt->per_cu;
3635 ++*datap;
3636
3637 return 1;
3638}
3639
3019eac3
DE
3640/* Create the hash table of all entries in the .debug_types section.
3641 DWO_FILE is a pointer to the DWO file for .debug_types.dwo, NULL otherwise.
3642 The result is a pointer to the hash table or NULL if there are
3643 no types. */
348e048f 3644
3019eac3
DE
3645static htab_t
3646create_debug_types_hash_table (struct dwo_file *dwo_file,
3647 VEC (dwarf2_section_info_def) *types)
348e048f 3648{
3019eac3 3649 struct objfile *objfile = dwarf2_per_objfile->objfile;
8b70b953 3650 htab_t types_htab = NULL;
8b70b953
TT
3651 int ix;
3652 struct dwarf2_section_info *section;
4bdcc0c1 3653 struct dwarf2_section_info *abbrev_section;
348e048f 3654
3019eac3
DE
3655 if (VEC_empty (dwarf2_section_info_def, types))
3656 return NULL;
348e048f 3657
4bdcc0c1
DE
3658 abbrev_section = (dwo_file != NULL
3659 ? &dwo_file->sections.abbrev
3660 : &dwarf2_per_objfile->abbrev);
3661
8b70b953 3662 for (ix = 0;
3019eac3 3663 VEC_iterate (dwarf2_section_info_def, types, ix, section);
8b70b953
TT
3664 ++ix)
3665 {
3019eac3 3666 bfd *abfd;
8b70b953 3667 gdb_byte *info_ptr, *end_ptr;
348e048f 3668
8b70b953
TT
3669 dwarf2_read_section (objfile, section);
3670 info_ptr = section->buffer;
348e048f 3671
8b70b953
TT
3672 if (info_ptr == NULL)
3673 continue;
348e048f 3674
3019eac3
DE
3675 /* We can't set abfd until now because the section may be empty or
3676 not present, in which case section->asection will be NULL. */
3677 abfd = section->asection->owner;
3678
8b70b953 3679 if (types_htab == NULL)
3019eac3
DE
3680 {
3681 if (dwo_file)
3682 types_htab = allocate_dwo_unit_table (objfile);
3683 else
3684 types_htab = allocate_signatured_type_table (objfile);
3685 }
348e048f 3686
8b70b953 3687 if (dwarf2_die_debug)
dee91e82
DE
3688 fprintf_unfiltered (gdb_stdlog, "Reading signatured types for %s:\n",
3689 bfd_get_filename (abfd));
3690
3691 /* We don't use init_cutu_and_read_dies_simple, or some such, here
3692 because we don't need to read any dies: the signature is in the
3693 header. */
8b70b953
TT
3694
3695 end_ptr = info_ptr + section->size;
3696 while (info_ptr < end_ptr)
3697 {
b64f50a1 3698 sect_offset offset;
3019eac3 3699 cu_offset type_offset_in_tu;
8b70b953 3700 ULONGEST signature;
52dc124a 3701 struct signatured_type *sig_type;
3019eac3 3702 struct dwo_unit *dwo_tu;
8b70b953
TT
3703 void **slot;
3704 gdb_byte *ptr = info_ptr;
9ff913ba 3705 struct comp_unit_head header;
dee91e82 3706 unsigned int length;
348e048f 3707
b64f50a1 3708 offset.sect_off = ptr - section->buffer;
348e048f 3709
8b70b953 3710 /* We need to read the type's signature in order to build the hash
9ff913ba 3711 table, but we don't need anything else just yet. */
348e048f 3712
4bdcc0c1
DE
3713 ptr = read_and_check_type_unit_head (&header, section,
3714 abbrev_section, ptr,
3019eac3 3715 &signature, &type_offset_in_tu);
6caca83c 3716
dee91e82
DE
3717 length = header.initial_length_size + header.length;
3718
6caca83c 3719 /* Skip dummy type units. */
dee91e82
DE
3720 if (ptr >= info_ptr + length
3721 || peek_abbrev_code (abfd, ptr) == 0)
6caca83c 3722 {
dee91e82 3723 info_ptr += header.initial_length_size + header.length;
6caca83c
CC
3724 continue;
3725 }
8b70b953 3726
3019eac3
DE
3727 if (dwo_file)
3728 {
3729 sig_type = NULL;
3730 dwo_tu = OBSTACK_ZALLOC (&objfile->objfile_obstack,
3731 struct dwo_unit);
3732 dwo_tu->dwo_file = dwo_file;
3733 dwo_tu->signature = signature;
3734 dwo_tu->type_offset_in_tu = type_offset_in_tu;
3735 dwo_tu->info_or_types_section = section;
3736 dwo_tu->offset = offset;
3737 dwo_tu->length = length;
3738 }
3739 else
3740 {
3741 /* N.B.: type_offset is not usable if this type uses a DWO file.
3742 The real type_offset is in the DWO file. */
3743 dwo_tu = NULL;
3744 sig_type = OBSTACK_ZALLOC (&objfile->objfile_obstack,
3745 struct signatured_type);
3746 sig_type->signature = signature;
3747 sig_type->type_offset_in_tu = type_offset_in_tu;
3748 sig_type->per_cu.objfile = objfile;
3749 sig_type->per_cu.is_debug_types = 1;
3750 sig_type->per_cu.info_or_types_section = section;
3751 sig_type->per_cu.offset = offset;
3752 sig_type->per_cu.length = length;
3753 }
8b70b953 3754
3019eac3
DE
3755 slot = htab_find_slot (types_htab,
3756 dwo_file ? (void*) dwo_tu : (void *) sig_type,
3757 INSERT);
8b70b953
TT
3758 gdb_assert (slot != NULL);
3759 if (*slot != NULL)
3760 {
3019eac3
DE
3761 sect_offset dup_offset;
3762
3763 if (dwo_file)
3764 {
3765 const struct dwo_unit *dup_tu = *slot;
3766
3767 dup_offset = dup_tu->offset;
3768 }
3769 else
3770 {
3771 const struct signatured_type *dup_tu = *slot;
3772
3773 dup_offset = dup_tu->per_cu.offset;
3774 }
b3c8eb43 3775
8b70b953
TT
3776 complaint (&symfile_complaints,
3777 _("debug type entry at offset 0x%x is duplicate to the "
3778 "entry at offset 0x%x, signature 0x%s"),
3019eac3 3779 offset.sect_off, dup_offset.sect_off,
8b70b953 3780 phex (signature, sizeof (signature)));
8b70b953 3781 }
3019eac3 3782 *slot = dwo_file ? (void *) dwo_tu : (void *) sig_type;
348e048f 3783
8b70b953
TT
3784 if (dwarf2_die_debug)
3785 fprintf_unfiltered (gdb_stdlog, " offset 0x%x, signature 0x%s\n",
b64f50a1
JK
3786 offset.sect_off,
3787 phex (signature, sizeof (signature)));
348e048f 3788
dee91e82 3789 info_ptr += length;
8b70b953 3790 }
348e048f
DE
3791 }
3792
3019eac3
DE
3793 return types_htab;
3794}
3795
3796/* Create the hash table of all entries in the .debug_types section,
3797 and initialize all_type_units.
3798 The result is zero if there is an error (e.g. missing .debug_types section),
3799 otherwise non-zero. */
3800
3801static int
3802create_all_type_units (struct objfile *objfile)
3803{
3804 htab_t types_htab;
3805 struct dwarf2_per_cu_data **iter;
3806
3807 types_htab = create_debug_types_hash_table (NULL, dwarf2_per_objfile->types);
3808 if (types_htab == NULL)
3809 {
3810 dwarf2_per_objfile->signatured_types = NULL;
3811 return 0;
3812 }
3813
348e048f
DE
3814 dwarf2_per_objfile->signatured_types = types_htab;
3815
d467dd73
DE
3816 dwarf2_per_objfile->n_type_units = htab_elements (types_htab);
3817 dwarf2_per_objfile->all_type_units
1fd400ff 3818 = obstack_alloc (&objfile->objfile_obstack,
d467dd73 3819 dwarf2_per_objfile->n_type_units
1fd400ff 3820 * sizeof (struct dwarf2_per_cu_data *));
d467dd73
DE
3821 iter = &dwarf2_per_objfile->all_type_units[0];
3822 htab_traverse_noresize (types_htab, add_signatured_type_cu_to_table, &iter);
3823 gdb_assert (iter - &dwarf2_per_objfile->all_type_units[0]
3824 == dwarf2_per_objfile->n_type_units);
1fd400ff 3825
348e048f
DE
3826 return 1;
3827}
3828
380bca97 3829/* Lookup a signature based type for DW_FORM_ref_sig8.
e319fa28 3830 Returns NULL if signature SIG is not present in the table. */
348e048f
DE
3831
3832static struct signatured_type *
e319fa28 3833lookup_signatured_type (ULONGEST sig)
348e048f
DE
3834{
3835 struct signatured_type find_entry, *entry;
3836
3837 if (dwarf2_per_objfile->signatured_types == NULL)
3838 {
3839 complaint (&symfile_complaints,
55f1336d 3840 _("missing `.debug_types' section for DW_FORM_ref_sig8 die"));
dcc07052 3841 return NULL;
348e048f
DE
3842 }
3843
3844 find_entry.signature = sig;
3845 entry = htab_find (dwarf2_per_objfile->signatured_types, &find_entry);
3846 return entry;
3847}
3848
d85a05f0
DJ
3849/* Initialize a die_reader_specs struct from a dwarf2_cu struct. */
3850
3851static void
3852init_cu_die_reader (struct die_reader_specs *reader,
dee91e82 3853 struct dwarf2_cu *cu,
3019eac3
DE
3854 struct dwarf2_section_info *section,
3855 struct dwo_file *dwo_file)
d85a05f0 3856{
fceca515 3857 gdb_assert (section->readin && section->buffer != NULL);
dee91e82 3858 reader->abfd = section->asection->owner;
d85a05f0 3859 reader->cu = cu;
3019eac3 3860 reader->dwo_file = dwo_file;
dee91e82
DE
3861 reader->die_section = section;
3862 reader->buffer = section->buffer;
f664829e 3863 reader->buffer_end = section->buffer + section->size;
d85a05f0
DJ
3864}
3865
fd820528 3866/* Initialize a CU (or TU) and read its DIEs.
3019eac3 3867 If the CU defers to a DWO file, read the DWO file as well.
dee91e82
DE
3868
3869 If USE_EXISTING_CU is non-zero, and THIS_CU->cu is non-NULL, then use it.
3870 Otherwise, a new CU is allocated with xmalloc.
3871
3872 If KEEP is non-zero, then if we allocated a dwarf2_cu we add it to
3873 read_in_chain. Otherwise the dwarf2_cu data is freed at the end.
3874
3875 WARNING: If THIS_CU is a "dummy CU" (used as filler by the incremental
fd820528 3876 linker) then DIE_READER_FUNC will not get called. */
aaa75496 3877
70221824 3878static void
fd820528
DE
3879init_cutu_and_read_dies (struct dwarf2_per_cu_data *this_cu,
3880 int use_existing_cu, int keep,
3881 die_reader_func_ftype *die_reader_func,
3882 void *data)
c906108c 3883{
dee91e82 3884 struct objfile *objfile = dwarf2_per_objfile->objfile;
3019eac3
DE
3885 struct dwarf2_section_info *section = this_cu->info_or_types_section;
3886 bfd *abfd = section->asection->owner;
dee91e82
DE
3887 struct dwarf2_cu *cu;
3888 gdb_byte *begin_info_ptr, *info_ptr;
3889 struct die_reader_specs reader;
d85a05f0 3890 struct die_info *comp_unit_die;
dee91e82 3891 int has_children;
d85a05f0 3892 struct attribute *attr;
dee91e82
DE
3893 struct cleanup *cleanups, *free_cu_cleanup = NULL;
3894 struct signatured_type *sig_type = NULL;
4bdcc0c1 3895 struct dwarf2_section_info *abbrev_section;
c906108c 3896
dee91e82
DE
3897 if (use_existing_cu)
3898 gdb_assert (keep);
23745b47 3899
dee91e82
DE
3900 cleanups = make_cleanup (null_cleanup, NULL);
3901
3902 /* This is cheap if the section is already read in. */
3903 dwarf2_read_section (objfile, section);
3904
3905 begin_info_ptr = info_ptr = section->buffer + this_cu->offset.sect_off;
4bdcc0c1 3906 abbrev_section = &dwarf2_per_objfile->abbrev;
dee91e82
DE
3907
3908 if (use_existing_cu && this_cu->cu != NULL)
3909 {
3910 cu = this_cu->cu;
3911 info_ptr += cu->header.first_die_offset.cu_off;
3912 }
3913 else
3914 {
3915 /* If !use_existing_cu, this_cu->cu must be NULL. */
3916 gdb_assert (this_cu->cu == NULL);
3917
3918 cu = xmalloc (sizeof (*cu));
3919 init_one_comp_unit (cu, this_cu);
3920
3921 /* If an error occurs while loading, release our storage. */
3922 free_cu_cleanup = make_cleanup (free_heap_comp_unit, cu);
3923
3019eac3 3924 if (this_cu->is_debug_types)
dee91e82
DE
3925 {
3926 ULONGEST signature;
3927
4bdcc0c1
DE
3928 info_ptr = read_and_check_type_unit_head (&cu->header, section,
3929 abbrev_section, info_ptr,
dee91e82
DE
3930 &signature, NULL);
3931
3932 /* There's no way to get from PER_CU to its containing
3933 struct signatured_type.
3934 But we have the signature so we can use that. */
3935 sig_type = lookup_signatured_type (signature);
3936 /* We've already scanned all the signatured types,
3937 this must succeed. */
3938 gdb_assert (sig_type != NULL);
3939 gdb_assert (&sig_type->per_cu == this_cu);
3940 gdb_assert (this_cu->offset.sect_off == cu->header.offset.sect_off);
3941
3942 /* LENGTH has not been set yet for type units. */
3943 this_cu->length = cu->header.length + cu->header.initial_length_size;
3019eac3
DE
3944
3945 /* Establish the type offset that can be used to lookup the type. */
3946 sig_type->type_offset_in_section.sect_off =
3947 this_cu->offset.sect_off + sig_type->type_offset_in_tu.cu_off;
dee91e82
DE
3948 }
3949 else
3950 {
4bdcc0c1
DE
3951 info_ptr = read_and_check_comp_unit_head (&cu->header, section,
3952 abbrev_section,
3953 info_ptr, 0);
dee91e82
DE
3954
3955 gdb_assert (this_cu->offset.sect_off == cu->header.offset.sect_off);
3956 gdb_assert (this_cu->length
3957 == cu->header.length + cu->header.initial_length_size);
3958 }
3959 }
10b3939b 3960
6caca83c 3961 /* Skip dummy compilation units. */
dee91e82 3962 if (info_ptr >= begin_info_ptr + this_cu->length
6caca83c
CC
3963 || peek_abbrev_code (abfd, info_ptr) == 0)
3964 {
dee91e82 3965 do_cleanups (cleanups);
21b2bd31 3966 return;
6caca83c
CC
3967 }
3968
433df2d4
DE
3969 /* If we don't have them yet, read the abbrevs for this compilation unit.
3970 And if we need to read them now, make sure they're freed when we're
3971 done. */
3972 if (cu->abbrev_table == NULL)
dee91e82 3973 {
4bdcc0c1 3974 dwarf2_read_abbrevs (cu, abbrev_section);
dee91e82
DE
3975 make_cleanup (dwarf2_free_abbrev_table, cu);
3976 }
af703f96 3977
dee91e82 3978 /* Read the top level CU/TU die. */
3019eac3 3979 init_cu_die_reader (&reader, cu, section, NULL);
dee91e82 3980 info_ptr = read_full_die (&reader, &comp_unit_die, info_ptr, &has_children);
93311388 3981
3019eac3
DE
3982 /* If we have a DWO stub, process it and then read in the DWO file.
3983 Note that if USE_EXISTING_OK != 0, and THIS_CU->cu already contains
3984 a DWO CU, that this test will fail. */
3985 attr = dwarf2_attr (comp_unit_die, DW_AT_GNU_dwo_name, cu);
3986 if (attr)
3987 {
3988 char *dwo_name = DW_STRING (attr);
3989 const char *comp_dir;
3990 struct dwo_unit *dwo_unit;
3991 ULONGEST signature; /* Or dwo_id. */
3992 struct attribute *stmt_list, *low_pc, *high_pc, *ranges;
3993 int i,num_extra_attrs;
4bdcc0c1 3994 struct dwarf2_section_info *dwo_abbrev_section;
3019eac3
DE
3995
3996 if (has_children)
3997 error (_("Dwarf Error: compilation unit with DW_AT_GNU_dwo_name"
3998 " has children (offset 0x%x) [in module %s]"),
3999 this_cu->offset.sect_off, bfd_get_filename (abfd));
4000
4001 /* These attributes aren't processed until later:
4002 DW_AT_stmt_list, DW_AT_low_pc, DW_AT_high_pc, DW_AT_ranges.
4003 However, the attribute is found in the stub which we won't have later.
4004 In order to not impose this complication on the rest of the code,
4005 we read them here and copy them to the DWO CU/TU die. */
4006 stmt_list = low_pc = high_pc = ranges = NULL;
4007
4008 /* For TUs in DWO files, the DW_AT_stmt_list attribute lives in the
4009 DWO file. */
4010 if (! this_cu->is_debug_types)
4011 stmt_list = dwarf2_attr (comp_unit_die, DW_AT_stmt_list, cu);
4012 low_pc = dwarf2_attr (comp_unit_die, DW_AT_low_pc, cu);
4013 high_pc = dwarf2_attr (comp_unit_die, DW_AT_high_pc, cu);
4014 ranges = dwarf2_attr (comp_unit_die, DW_AT_ranges, cu);
4015
4016 /* There should be a DW_AT_addr_base attribute here (if needed).
4017 We need the value before we can process DW_FORM_GNU_addr_index. */
4018 cu->addr_base = 0;
3019eac3
DE
4019 attr = dwarf2_attr (comp_unit_die, DW_AT_GNU_addr_base, cu);
4020 if (attr)
2e3cf129
DE
4021 cu->addr_base = DW_UNSND (attr);
4022
4023 /* There should be a DW_AT_ranges_base attribute here (if needed).
4024 We need the value before we can process DW_AT_ranges. */
4025 cu->ranges_base = 0;
4026 attr = dwarf2_attr (comp_unit_die, DW_AT_GNU_ranges_base, cu);
4027 if (attr)
4028 cu->ranges_base = DW_UNSND (attr);
3019eac3
DE
4029
4030 if (this_cu->is_debug_types)
4031 {
4032 gdb_assert (sig_type != NULL);
4033 signature = sig_type->signature;
4034 }
4035 else
4036 {
4037 attr = dwarf2_attr (comp_unit_die, DW_AT_GNU_dwo_id, cu);
4038 if (! attr)
4039 error (_("Dwarf Error: missing dwo_id [in module %s]"),
4040 dwo_name);
4041 signature = DW_UNSND (attr);
4042 }
4043
4044 /* We may need the comp_dir in order to find the DWO file. */
4045 comp_dir = NULL;
4046 attr = dwarf2_attr (comp_unit_die, DW_AT_comp_dir, cu);
4047 if (attr)
4048 comp_dir = DW_STRING (attr);
4049
4050 if (this_cu->is_debug_types)
4051 dwo_unit = lookup_dwo_type_unit (sig_type, dwo_name, comp_dir);
4052 else
4053 dwo_unit = lookup_dwo_comp_unit (this_cu, dwo_name, comp_dir,
4054 signature);
4055
4056 if (dwo_unit == NULL)
4057 {
4058 error (_("Dwarf Error: CU at offset 0x%x references unknown DWO"
4059 " with ID %s [in module %s]"),
4060 this_cu->offset.sect_off,
4061 phex (signature, sizeof (signature)),
4062 objfile->name);
4063 }
4064
4065 /* Set up for reading the DWO CU/TU. */
4066 cu->dwo_unit = dwo_unit;
4067 section = dwo_unit->info_or_types_section;
4068 begin_info_ptr = info_ptr = section->buffer + dwo_unit->offset.sect_off;
4bdcc0c1 4069 dwo_abbrev_section = &dwo_unit->dwo_file->sections.abbrev;
3019eac3
DE
4070 init_cu_die_reader (&reader, cu, section, dwo_unit->dwo_file);
4071
4072 if (this_cu->is_debug_types)
4073 {
4074 ULONGEST signature;
4075
4bdcc0c1
DE
4076 info_ptr = read_and_check_type_unit_head (&cu->header, section,
4077 dwo_abbrev_section,
4078 info_ptr,
3019eac3
DE
4079 &signature, NULL);
4080 gdb_assert (sig_type->signature == signature);
4081 gdb_assert (dwo_unit->offset.sect_off == cu->header.offset.sect_off);
4082 gdb_assert (dwo_unit->length
4083 == cu->header.length + cu->header.initial_length_size);
4084
4085 /* Establish the type offset that can be used to lookup the type.
4086 For DWO files, we don't know it until now. */
4087 sig_type->type_offset_in_section.sect_off =
4088 dwo_unit->offset.sect_off + dwo_unit->type_offset_in_tu.cu_off;
4089 }
4090 else
4091 {
4bdcc0c1
DE
4092 info_ptr = read_and_check_comp_unit_head (&cu->header, section,
4093 dwo_abbrev_section,
4094 info_ptr, 0);
3019eac3
DE
4095 gdb_assert (dwo_unit->offset.sect_off == cu->header.offset.sect_off);
4096 gdb_assert (dwo_unit->length
4097 == cu->header.length + cu->header.initial_length_size);
4098 }
4099
4100 /* Discard the original CU's abbrev table, and read the DWO's. */
4101 dwarf2_free_abbrev_table (cu);
4bdcc0c1 4102 dwarf2_read_abbrevs (cu, dwo_abbrev_section);
3019eac3
DE
4103
4104 /* Read in the die, but leave space to copy over the attributes
4105 from the stub. This has the benefit of simplifying the rest of
4106 the code - all the real work is done here. */
4107 num_extra_attrs = ((stmt_list != NULL)
4108 + (low_pc != NULL)
4109 + (high_pc != NULL)
4110 + (ranges != NULL));
4111 info_ptr = read_full_die_1 (&reader, &comp_unit_die, info_ptr,
4112 &has_children, num_extra_attrs);
4113
4114 /* Copy over the attributes from the stub to the DWO die. */
4115 i = comp_unit_die->num_attrs;
4116 if (stmt_list != NULL)
4117 comp_unit_die->attrs[i++] = *stmt_list;
4118 if (low_pc != NULL)
4119 comp_unit_die->attrs[i++] = *low_pc;
4120 if (high_pc != NULL)
4121 comp_unit_die->attrs[i++] = *high_pc;
4122 if (ranges != NULL)
4123 comp_unit_die->attrs[i++] = *ranges;
4124 comp_unit_die->num_attrs += num_extra_attrs;
4125
4126 /* Skip dummy compilation units. */
4127 if (info_ptr >= begin_info_ptr + dwo_unit->length
4128 || peek_abbrev_code (abfd, info_ptr) == 0)
4129 {
4130 do_cleanups (cleanups);
4131 return;
4132 }
4133 }
4134
dee91e82
DE
4135 die_reader_func (&reader, info_ptr, comp_unit_die, has_children, data);
4136
4137 if (free_cu_cleanup != NULL)
348e048f 4138 {
dee91e82
DE
4139 if (keep)
4140 {
4141 /* We've successfully allocated this compilation unit. Let our
4142 caller clean it up when finished with it. */
4143 discard_cleanups (free_cu_cleanup);
4144
4145 /* We can only discard free_cu_cleanup and all subsequent cleanups.
4146 So we have to manually free the abbrev table. */
4147 dwarf2_free_abbrev_table (cu);
4148
4149 /* Link this CU into read_in_chain. */
4150 this_cu->cu->read_in_chain = dwarf2_per_objfile->read_in_chain;
4151 dwarf2_per_objfile->read_in_chain = this_cu;
4152 }
4153 else
4154 do_cleanups (free_cu_cleanup);
348e048f 4155 }
dee91e82
DE
4156
4157 do_cleanups (cleanups);
4158}
4159
3019eac3
DE
4160/* Read CU/TU THIS_CU in section SECTION,
4161 but do not follow DW_AT_GNU_dwo_name if present.
4162 DWO_FILE, if non-NULL, is the DWO file to read (the caller is assumed to
4163 have already done the lookup to find the DWO file).
dee91e82
DE
4164
4165 The caller is required to fill in THIS_CU->section, THIS_CU->offset, and
3019eac3 4166 THIS_CU->is_debug_types, but nothing else.
dee91e82
DE
4167
4168 We fill in THIS_CU->length.
4169
4170 WARNING: If THIS_CU is a "dummy CU" (used as filler by the incremental
4171 linker) then DIE_READER_FUNC will not get called.
4172
4173 THIS_CU->cu is always freed when done.
3019eac3
DE
4174 This is done in order to not leave THIS_CU->cu in a state where we have
4175 to care whether it refers to the "main" CU or the DWO CU. */
dee91e82
DE
4176
4177static void
4178init_cutu_and_read_dies_no_follow (struct dwarf2_per_cu_data *this_cu,
4179 struct dwarf2_section_info *abbrev_section,
3019eac3 4180 struct dwo_file *dwo_file,
dee91e82
DE
4181 die_reader_func_ftype *die_reader_func,
4182 void *data)
4183{
4184 struct objfile *objfile = dwarf2_per_objfile->objfile;
3019eac3
DE
4185 struct dwarf2_section_info *section = this_cu->info_or_types_section;
4186 bfd *abfd = section->asection->owner;
dee91e82
DE
4187 struct dwarf2_cu cu;
4188 gdb_byte *begin_info_ptr, *info_ptr;
4189 struct die_reader_specs reader;
4190 struct cleanup *cleanups;
4191 struct die_info *comp_unit_die;
4192 int has_children;
4193
4194 gdb_assert (this_cu->cu == NULL);
4195
dee91e82
DE
4196 /* This is cheap if the section is already read in. */
4197 dwarf2_read_section (objfile, section);
4198
4199 init_one_comp_unit (&cu, this_cu);
4200
4201 cleanups = make_cleanup (free_stack_comp_unit, &cu);
4202
4203 begin_info_ptr = info_ptr = section->buffer + this_cu->offset.sect_off;
4bdcc0c1
DE
4204 info_ptr = read_and_check_comp_unit_head (&cu.header, section,
4205 abbrev_section, info_ptr,
3019eac3 4206 this_cu->is_debug_types);
dee91e82
DE
4207
4208 this_cu->length = cu.header.length + cu.header.initial_length_size;
4209
4210 /* Skip dummy compilation units. */
4211 if (info_ptr >= begin_info_ptr + this_cu->length
4212 || peek_abbrev_code (abfd, info_ptr) == 0)
c906108c 4213 {
dee91e82 4214 do_cleanups (cleanups);
21b2bd31 4215 return;
93311388 4216 }
72bf9492 4217
dee91e82
DE
4218 dwarf2_read_abbrevs (&cu, abbrev_section);
4219 make_cleanup (dwarf2_free_abbrev_table, &cu);
4220
3019eac3 4221 init_cu_die_reader (&reader, &cu, section, dwo_file);
dee91e82
DE
4222 info_ptr = read_full_die (&reader, &comp_unit_die, info_ptr, &has_children);
4223
4224 die_reader_func (&reader, info_ptr, comp_unit_die, has_children, data);
4225
4226 do_cleanups (cleanups);
4227}
4228
3019eac3
DE
4229/* Read a CU/TU, except that this does not look for DW_AT_GNU_dwo_name and
4230 does not lookup the specified DWO file.
4231 This cannot be used to read DWO files.
dee91e82
DE
4232
4233 THIS_CU->cu is always freed when done.
3019eac3
DE
4234 This is done in order to not leave THIS_CU->cu in a state where we have
4235 to care whether it refers to the "main" CU or the DWO CU.
4236 We can revisit this if the data shows there's a performance issue. */
dee91e82
DE
4237
4238static void
4239init_cutu_and_read_dies_simple (struct dwarf2_per_cu_data *this_cu,
4240 die_reader_func_ftype *die_reader_func,
4241 void *data)
4242{
4243 init_cutu_and_read_dies_no_follow (this_cu,
4244 &dwarf2_per_objfile->abbrev,
3019eac3 4245 NULL,
dee91e82
DE
4246 die_reader_func, data);
4247}
4248
4249/* die_reader_func for process_psymtab_comp_unit. */
4250
4251static void
4252process_psymtab_comp_unit_reader (const struct die_reader_specs *reader,
4253 gdb_byte *info_ptr,
4254 struct die_info *comp_unit_die,
4255 int has_children,
4256 void *data)
4257{
4258 struct dwarf2_cu *cu = reader->cu;
4259 struct objfile *objfile = cu->objfile;
4260 struct dwarf2_per_cu_data *per_cu = cu->per_cu;
dee91e82
DE
4261 struct attribute *attr;
4262 CORE_ADDR baseaddr;
4263 CORE_ADDR best_lowpc = 0, best_highpc = 0;
4264 struct partial_symtab *pst;
4265 int has_pc_info;
4266 const char *filename;
95554aad 4267 int *want_partial_unit_ptr = data;
dee91e82 4268
95554aad
TT
4269 if (comp_unit_die->tag == DW_TAG_partial_unit
4270 && (want_partial_unit_ptr == NULL
4271 || !*want_partial_unit_ptr))
dee91e82
DE
4272 return;
4273
95554aad 4274 prepare_one_comp_unit (cu, comp_unit_die, language_minimal);
dee91e82
DE
4275
4276 cu->list_in_scope = &file_symbols;
c906108c 4277
93311388 4278 /* Allocate a new partial symbol table structure. */
dee91e82 4279 attr = dwarf2_attr (comp_unit_die, DW_AT_name, cu);
3e2a0cee
TT
4280 if (attr == NULL || !DW_STRING (attr))
4281 filename = "";
4282 else
4283 filename = DW_STRING (attr);
93311388 4284 pst = start_psymtab_common (objfile, objfile->section_offsets,
3e2a0cee 4285 filename,
93311388
DE
4286 /* TEXTLOW and TEXTHIGH are set below. */
4287 0,
4288 objfile->global_psymbols.next,
4289 objfile->static_psymbols.next);
9750bca9 4290 pst->psymtabs_addrmap_supported = 1;
72bf9492 4291
dee91e82 4292 attr = dwarf2_attr (comp_unit_die, DW_AT_comp_dir, cu);
d85a05f0
DJ
4293 if (attr != NULL)
4294 pst->dirname = DW_STRING (attr);
72bf9492 4295
dee91e82 4296 pst->read_symtab_private = per_cu;
72bf9492 4297
93311388 4298 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
e7c27a73 4299
0963b4bd 4300 /* Store the function that reads in the rest of the symbol table. */
93311388 4301 pst->read_symtab = dwarf2_psymtab_to_symtab;
57349743 4302
dee91e82 4303 per_cu->v.psymtab = pst;
c906108c 4304
dee91e82 4305 dwarf2_find_base_address (comp_unit_die, cu);
d85a05f0 4306
93311388
DE
4307 /* Possibly set the default values of LOWPC and HIGHPC from
4308 `DW_AT_ranges'. */
d85a05f0 4309 has_pc_info = dwarf2_get_pc_bounds (comp_unit_die, &best_lowpc,
dee91e82 4310 &best_highpc, cu, pst);
d85a05f0 4311 if (has_pc_info == 1 && best_lowpc < best_highpc)
93311388
DE
4312 /* Store the contiguous range if it is not empty; it can be empty for
4313 CUs with no code. */
4314 addrmap_set_empty (objfile->psymtabs_addrmap,
d85a05f0
DJ
4315 best_lowpc + baseaddr,
4316 best_highpc + baseaddr - 1, pst);
93311388
DE
4317
4318 /* Check if comp unit has_children.
4319 If so, read the rest of the partial symbols from this comp unit.
0963b4bd 4320 If not, there's no more debug_info for this comp unit. */
d85a05f0 4321 if (has_children)
93311388
DE
4322 {
4323 struct partial_die_info *first_die;
4324 CORE_ADDR lowpc, highpc;
31ffec48 4325
93311388
DE
4326 lowpc = ((CORE_ADDR) -1);
4327 highpc = ((CORE_ADDR) 0);
c906108c 4328
dee91e82 4329 first_die = load_partial_dies (reader, info_ptr, 1);
c906108c 4330
93311388 4331 scan_partial_symbols (first_die, &lowpc, &highpc,
dee91e82 4332 ! has_pc_info, cu);
57c22c6c 4333
93311388
DE
4334 /* If we didn't find a lowpc, set it to highpc to avoid
4335 complaints from `maint check'. */
4336 if (lowpc == ((CORE_ADDR) -1))
4337 lowpc = highpc;
10b3939b 4338
93311388
DE
4339 /* If the compilation unit didn't have an explicit address range,
4340 then use the information extracted from its child dies. */
d85a05f0 4341 if (! has_pc_info)
93311388 4342 {
d85a05f0
DJ
4343 best_lowpc = lowpc;
4344 best_highpc = highpc;
93311388
DE
4345 }
4346 }
d85a05f0
DJ
4347 pst->textlow = best_lowpc + baseaddr;
4348 pst->texthigh = best_highpc + baseaddr;
c906108c 4349
93311388
DE
4350 pst->n_global_syms = objfile->global_psymbols.next -
4351 (objfile->global_psymbols.list + pst->globals_offset);
4352 pst->n_static_syms = objfile->static_psymbols.next -
4353 (objfile->static_psymbols.list + pst->statics_offset);
4354 sort_pst_symbols (pst);
c906108c 4355
95554aad
TT
4356 if (!VEC_empty (dwarf2_per_cu_ptr, cu->per_cu->imported_symtabs))
4357 {
4358 int i;
4359 int len = VEC_length (dwarf2_per_cu_ptr, cu->per_cu->imported_symtabs);
4360 struct dwarf2_per_cu_data *iter;
4361
4362 /* Fill in 'dependencies' here; we fill in 'users' in a
4363 post-pass. */
4364 pst->number_of_dependencies = len;
4365 pst->dependencies = obstack_alloc (&objfile->objfile_obstack,
4366 len * sizeof (struct symtab *));
4367 for (i = 0;
4368 VEC_iterate (dwarf2_per_cu_ptr, cu->per_cu->imported_symtabs,
4369 i, iter);
4370 ++i)
4371 pst->dependencies[i] = iter->v.psymtab;
4372
4373 VEC_free (dwarf2_per_cu_ptr, cu->per_cu->imported_symtabs);
4374 }
4375
3019eac3 4376 if (per_cu->is_debug_types)
348e048f
DE
4377 {
4378 /* It's not clear we want to do anything with stmt lists here.
4379 Waiting to see what gcc ultimately does. */
4380 }
d85a05f0 4381 else
93311388
DE
4382 {
4383 /* Get the list of files included in the current compilation unit,
4384 and build a psymtab for each of them. */
dee91e82 4385 dwarf2_build_include_psymtabs (cu, comp_unit_die, pst);
93311388 4386 }
dee91e82 4387}
ae038cb0 4388
dee91e82
DE
4389/* Subroutine of dwarf2_build_psymtabs_hard to simplify it.
4390 Process compilation unit THIS_CU for a psymtab. */
4391
4392static void
95554aad
TT
4393process_psymtab_comp_unit (struct dwarf2_per_cu_data *this_cu,
4394 int want_partial_unit)
dee91e82
DE
4395{
4396 /* If this compilation unit was already read in, free the
4397 cached copy in order to read it in again. This is
4398 necessary because we skipped some symbols when we first
4399 read in the compilation unit (see load_partial_dies).
4400 This problem could be avoided, but the benefit is unclear. */
4401 if (this_cu->cu != NULL)
4402 free_one_cached_comp_unit (this_cu);
4403
3019eac3 4404 gdb_assert (! this_cu->is_debug_types);
fd820528 4405 init_cutu_and_read_dies (this_cu, 0, 0, process_psymtab_comp_unit_reader,
95554aad 4406 &want_partial_unit);
dee91e82
DE
4407
4408 /* Age out any secondary CUs. */
4409 age_cached_comp_units ();
93311388 4410}
ff013f42 4411
348e048f
DE
4412/* Traversal function for htab_traverse_noresize.
4413 Process one .debug_types comp-unit. */
4414
4415static int
dee91e82 4416process_psymtab_type_unit (void **slot, void *info)
348e048f 4417{
dee91e82
DE
4418 struct signatured_type *sig_type = (struct signatured_type *) *slot;
4419 struct dwarf2_per_cu_data *per_cu = &sig_type->per_cu;
348e048f 4420
fd820528 4421 gdb_assert (per_cu->is_debug_types);
a0f42c21 4422 gdb_assert (info == NULL);
348e048f 4423
dee91e82
DE
4424 /* If this compilation unit was already read in, free the
4425 cached copy in order to read it in again. This is
4426 necessary because we skipped some symbols when we first
4427 read in the compilation unit (see load_partial_dies).
4428 This problem could be avoided, but the benefit is unclear. */
4429 if (per_cu->cu != NULL)
4430 free_one_cached_comp_unit (per_cu);
4431
fd820528
DE
4432 init_cutu_and_read_dies (per_cu, 0, 0, process_psymtab_comp_unit_reader,
4433 NULL);
dee91e82
DE
4434
4435 /* Age out any secondary CUs. */
4436 age_cached_comp_units ();
348e048f
DE
4437
4438 return 1;
4439}
4440
4441/* Subroutine of dwarf2_build_psymtabs_hard to simplify it.
4442 Build partial symbol tables for the .debug_types comp-units. */
4443
4444static void
4445build_type_psymtabs (struct objfile *objfile)
4446{
0e50663e 4447 if (! create_all_type_units (objfile))
348e048f
DE
4448 return;
4449
4450 htab_traverse_noresize (dwarf2_per_objfile->signatured_types,
dee91e82 4451 process_psymtab_type_unit, NULL);
348e048f
DE
4452}
4453
60606b2c
TT
4454/* A cleanup function that clears objfile's psymtabs_addrmap field. */
4455
4456static void
4457psymtabs_addrmap_cleanup (void *o)
4458{
4459 struct objfile *objfile = o;
ec61707d 4460
60606b2c
TT
4461 objfile->psymtabs_addrmap = NULL;
4462}
4463
95554aad
TT
4464/* Compute the 'user' field for each psymtab in OBJFILE. */
4465
4466static void
4467set_partial_user (struct objfile *objfile)
4468{
4469 int i;
4470
4471 for (i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
4472 {
4473 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
4474 struct partial_symtab *pst = per_cu->v.psymtab;
4475 int j;
4476
4477 for (j = 0; j < pst->number_of_dependencies; ++j)
4478 {
4479 /* Set the 'user' field only if it is not already set. */
4480 if (pst->dependencies[j]->user == NULL)
4481 pst->dependencies[j]->user = pst;
4482 }
4483 }
4484}
4485
93311388
DE
4486/* Build the partial symbol table by doing a quick pass through the
4487 .debug_info and .debug_abbrev sections. */
72bf9492 4488
93311388 4489static void
c67a9c90 4490dwarf2_build_psymtabs_hard (struct objfile *objfile)
93311388 4491{
60606b2c
TT
4492 struct cleanup *back_to, *addrmap_cleanup;
4493 struct obstack temp_obstack;
21b2bd31 4494 int i;
93311388 4495
45cfd468
DE
4496 if (dwarf2_read_debug)
4497 {
4498 fprintf_unfiltered (gdb_stdlog, "Building psymtabs of objfile %s ...\n",
4499 objfile->name);
4500 }
4501
98bfdba5
PA
4502 dwarf2_per_objfile->reading_partial_symbols = 1;
4503
be391dca 4504 dwarf2_read_section (objfile, &dwarf2_per_objfile->info);
91c24f0a 4505
93311388
DE
4506 /* Any cached compilation units will be linked by the per-objfile
4507 read_in_chain. Make sure to free them when we're done. */
4508 back_to = make_cleanup (free_cached_comp_units, NULL);
72bf9492 4509
348e048f
DE
4510 build_type_psymtabs (objfile);
4511
93311388 4512 create_all_comp_units (objfile);
c906108c 4513
60606b2c
TT
4514 /* Create a temporary address map on a temporary obstack. We later
4515 copy this to the final obstack. */
4516 obstack_init (&temp_obstack);
4517 make_cleanup_obstack_free (&temp_obstack);
4518 objfile->psymtabs_addrmap = addrmap_create_mutable (&temp_obstack);
4519 addrmap_cleanup = make_cleanup (psymtabs_addrmap_cleanup, objfile);
72bf9492 4520
21b2bd31 4521 for (i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
93311388 4522 {
21b2bd31 4523 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
aaa75496 4524
95554aad 4525 process_psymtab_comp_unit (per_cu, 0);
c906108c 4526 }
ff013f42 4527
95554aad
TT
4528 set_partial_user (objfile);
4529
ff013f42
JK
4530 objfile->psymtabs_addrmap = addrmap_create_fixed (objfile->psymtabs_addrmap,
4531 &objfile->objfile_obstack);
60606b2c 4532 discard_cleanups (addrmap_cleanup);
ff013f42 4533
ae038cb0 4534 do_cleanups (back_to);
45cfd468
DE
4535
4536 if (dwarf2_read_debug)
4537 fprintf_unfiltered (gdb_stdlog, "Done building psymtabs of %s\n",
4538 objfile->name);
ae038cb0
DJ
4539}
4540
3019eac3 4541/* die_reader_func for load_partial_comp_unit. */
ae038cb0
DJ
4542
4543static void
dee91e82
DE
4544load_partial_comp_unit_reader (const struct die_reader_specs *reader,
4545 gdb_byte *info_ptr,
4546 struct die_info *comp_unit_die,
4547 int has_children,
4548 void *data)
ae038cb0 4549{
dee91e82 4550 struct dwarf2_cu *cu = reader->cu;
ae038cb0 4551
95554aad 4552 prepare_one_comp_unit (cu, comp_unit_die, language_minimal);
ae038cb0 4553
ae038cb0
DJ
4554 /* Check if comp unit has_children.
4555 If so, read the rest of the partial symbols from this comp unit.
0963b4bd 4556 If not, there's no more debug_info for this comp unit. */
d85a05f0 4557 if (has_children)
dee91e82
DE
4558 load_partial_dies (reader, info_ptr, 0);
4559}
98bfdba5 4560
dee91e82
DE
4561/* Load the partial DIEs for a secondary CU into memory.
4562 This is also used when rereading a primary CU with load_all_dies. */
c5b7e1cb 4563
dee91e82
DE
4564static void
4565load_partial_comp_unit (struct dwarf2_per_cu_data *this_cu)
4566{
fd820528 4567 init_cutu_and_read_dies (this_cu, 1, 1, load_partial_comp_unit_reader, NULL);
ae038cb0
DJ
4568}
4569
9cdd5dbd
DE
4570/* Create a list of all compilation units in OBJFILE.
4571 This is only done for -readnow and building partial symtabs. */
ae038cb0
DJ
4572
4573static void
4574create_all_comp_units (struct objfile *objfile)
4575{
4576 int n_allocated;
4577 int n_comp_units;
4578 struct dwarf2_per_cu_data **all_comp_units;
be391dca
TT
4579 gdb_byte *info_ptr;
4580
4581 dwarf2_read_section (objfile, &dwarf2_per_objfile->info);
4582 info_ptr = dwarf2_per_objfile->info.buffer;
ae038cb0
DJ
4583
4584 n_comp_units = 0;
4585 n_allocated = 10;
4586 all_comp_units = xmalloc (n_allocated
4587 * sizeof (struct dwarf2_per_cu_data *));
6e70227d 4588
3e43a32a
MS
4589 while (info_ptr < dwarf2_per_objfile->info.buffer
4590 + dwarf2_per_objfile->info.size)
ae038cb0 4591 {
c764a876 4592 unsigned int length, initial_length_size;
ae038cb0 4593 struct dwarf2_per_cu_data *this_cu;
b64f50a1 4594 sect_offset offset;
ae038cb0 4595
b64f50a1 4596 offset.sect_off = info_ptr - dwarf2_per_objfile->info.buffer;
ae038cb0
DJ
4597
4598 /* Read just enough information to find out where the next
4599 compilation unit is. */
c764a876
DE
4600 length = read_initial_length (objfile->obfd, info_ptr,
4601 &initial_length_size);
ae038cb0
DJ
4602
4603 /* Save the compilation unit for later lookup. */
4604 this_cu = obstack_alloc (&objfile->objfile_obstack,
4605 sizeof (struct dwarf2_per_cu_data));
4606 memset (this_cu, 0, sizeof (*this_cu));
4607 this_cu->offset = offset;
c764a876 4608 this_cu->length = length + initial_length_size;
9291a0cd 4609 this_cu->objfile = objfile;
3019eac3 4610 this_cu->info_or_types_section = &dwarf2_per_objfile->info;
ae038cb0
DJ
4611
4612 if (n_comp_units == n_allocated)
4613 {
4614 n_allocated *= 2;
4615 all_comp_units = xrealloc (all_comp_units,
4616 n_allocated
4617 * sizeof (struct dwarf2_per_cu_data *));
4618 }
4619 all_comp_units[n_comp_units++] = this_cu;
4620
4621 info_ptr = info_ptr + this_cu->length;
4622 }
4623
4624 dwarf2_per_objfile->all_comp_units
4625 = obstack_alloc (&objfile->objfile_obstack,
4626 n_comp_units * sizeof (struct dwarf2_per_cu_data *));
4627 memcpy (dwarf2_per_objfile->all_comp_units, all_comp_units,
4628 n_comp_units * sizeof (struct dwarf2_per_cu_data *));
4629 xfree (all_comp_units);
4630 dwarf2_per_objfile->n_comp_units = n_comp_units;
c906108c
SS
4631}
4632
5734ee8b
DJ
4633/* Process all loaded DIEs for compilation unit CU, starting at
4634 FIRST_DIE. The caller should pass NEED_PC == 1 if the compilation
4635 unit DIE did not have PC info (DW_AT_low_pc and DW_AT_high_pc, or
4636 DW_AT_ranges). If NEED_PC is set, then this function will set
4637 *LOWPC and *HIGHPC to the lowest and highest PC values found in CU
4638 and record the covered ranges in the addrmap. */
c906108c 4639
72bf9492
DJ
4640static void
4641scan_partial_symbols (struct partial_die_info *first_die, CORE_ADDR *lowpc,
5734ee8b 4642 CORE_ADDR *highpc, int need_pc, struct dwarf2_cu *cu)
c906108c 4643{
72bf9492 4644 struct partial_die_info *pdi;
c906108c 4645
91c24f0a
DC
4646 /* Now, march along the PDI's, descending into ones which have
4647 interesting children but skipping the children of the other ones,
4648 until we reach the end of the compilation unit. */
c906108c 4649
72bf9492 4650 pdi = first_die;
91c24f0a 4651
72bf9492
DJ
4652 while (pdi != NULL)
4653 {
4654 fixup_partial_die (pdi, cu);
c906108c 4655
f55ee35c 4656 /* Anonymous namespaces or modules have no name but have interesting
91c24f0a
DC
4657 children, so we need to look at them. Ditto for anonymous
4658 enums. */
933c6fe4 4659
72bf9492 4660 if (pdi->name != NULL || pdi->tag == DW_TAG_namespace
95554aad
TT
4661 || pdi->tag == DW_TAG_module || pdi->tag == DW_TAG_enumeration_type
4662 || pdi->tag == DW_TAG_imported_unit)
c906108c 4663 {
72bf9492 4664 switch (pdi->tag)
c906108c
SS
4665 {
4666 case DW_TAG_subprogram:
5734ee8b 4667 add_partial_subprogram (pdi, lowpc, highpc, need_pc, cu);
c906108c 4668 break;
72929c62 4669 case DW_TAG_constant:
c906108c
SS
4670 case DW_TAG_variable:
4671 case DW_TAG_typedef:
91c24f0a 4672 case DW_TAG_union_type:
72bf9492 4673 if (!pdi->is_declaration)
63d06c5c 4674 {
72bf9492 4675 add_partial_symbol (pdi, cu);
63d06c5c
DC
4676 }
4677 break;
c906108c 4678 case DW_TAG_class_type:
680b30c7 4679 case DW_TAG_interface_type:
c906108c 4680 case DW_TAG_structure_type:
72bf9492 4681 if (!pdi->is_declaration)
c906108c 4682 {
72bf9492 4683 add_partial_symbol (pdi, cu);
c906108c
SS
4684 }
4685 break;
91c24f0a 4686 case DW_TAG_enumeration_type:
72bf9492
DJ
4687 if (!pdi->is_declaration)
4688 add_partial_enumeration (pdi, cu);
c906108c
SS
4689 break;
4690 case DW_TAG_base_type:
a02abb62 4691 case DW_TAG_subrange_type:
c906108c 4692 /* File scope base type definitions are added to the partial
c5aa993b 4693 symbol table. */
72bf9492 4694 add_partial_symbol (pdi, cu);
c906108c 4695 break;
d9fa45fe 4696 case DW_TAG_namespace:
5734ee8b 4697 add_partial_namespace (pdi, lowpc, highpc, need_pc, cu);
91c24f0a 4698 break;
5d7cb8df
JK
4699 case DW_TAG_module:
4700 add_partial_module (pdi, lowpc, highpc, need_pc, cu);
4701 break;
95554aad
TT
4702 case DW_TAG_imported_unit:
4703 {
4704 struct dwarf2_per_cu_data *per_cu;
4705
4706 per_cu = dwarf2_find_containing_comp_unit (pdi->d.offset,
4707 cu->objfile);
4708
4709 /* Go read the partial unit, if needed. */
4710 if (per_cu->v.psymtab == NULL)
4711 process_psymtab_comp_unit (per_cu, 1);
4712
4713 VEC_safe_push (dwarf2_per_cu_ptr, cu->per_cu->imported_symtabs,
4714 per_cu);
4715 }
4716 break;
c906108c
SS
4717 default:
4718 break;
4719 }
4720 }
4721
72bf9492
DJ
4722 /* If the die has a sibling, skip to the sibling. */
4723
4724 pdi = pdi->die_sibling;
4725 }
4726}
4727
4728/* Functions used to compute the fully scoped name of a partial DIE.
91c24f0a 4729
72bf9492 4730 Normally, this is simple. For C++, the parent DIE's fully scoped
987504bb
JJ
4731 name is concatenated with "::" and the partial DIE's name. For
4732 Java, the same thing occurs except that "." is used instead of "::".
72bf9492
DJ
4733 Enumerators are an exception; they use the scope of their parent
4734 enumeration type, i.e. the name of the enumeration type is not
4735 prepended to the enumerator.
91c24f0a 4736
72bf9492
DJ
4737 There are two complexities. One is DW_AT_specification; in this
4738 case "parent" means the parent of the target of the specification,
4739 instead of the direct parent of the DIE. The other is compilers
4740 which do not emit DW_TAG_namespace; in this case we try to guess
4741 the fully qualified name of structure types from their members'
4742 linkage names. This must be done using the DIE's children rather
4743 than the children of any DW_AT_specification target. We only need
4744 to do this for structures at the top level, i.e. if the target of
4745 any DW_AT_specification (if any; otherwise the DIE itself) does not
4746 have a parent. */
4747
4748/* Compute the scope prefix associated with PDI's parent, in
4749 compilation unit CU. The result will be allocated on CU's
4750 comp_unit_obstack, or a copy of the already allocated PDI->NAME
4751 field. NULL is returned if no prefix is necessary. */
4752static char *
4753partial_die_parent_scope (struct partial_die_info *pdi,
4754 struct dwarf2_cu *cu)
4755{
4756 char *grandparent_scope;
4757 struct partial_die_info *parent, *real_pdi;
91c24f0a 4758
72bf9492
DJ
4759 /* We need to look at our parent DIE; if we have a DW_AT_specification,
4760 then this means the parent of the specification DIE. */
4761
4762 real_pdi = pdi;
72bf9492 4763 while (real_pdi->has_specification)
10b3939b 4764 real_pdi = find_partial_die (real_pdi->spec_offset, cu);
72bf9492
DJ
4765
4766 parent = real_pdi->die_parent;
4767 if (parent == NULL)
4768 return NULL;
4769
4770 if (parent->scope_set)
4771 return parent->scope;
4772
4773 fixup_partial_die (parent, cu);
4774
10b3939b 4775 grandparent_scope = partial_die_parent_scope (parent, cu);
72bf9492 4776
acebe513
UW
4777 /* GCC 4.0 and 4.1 had a bug (PR c++/28460) where they generated bogus
4778 DW_TAG_namespace DIEs with a name of "::" for the global namespace.
4779 Work around this problem here. */
4780 if (cu->language == language_cplus
6e70227d 4781 && parent->tag == DW_TAG_namespace
acebe513
UW
4782 && strcmp (parent->name, "::") == 0
4783 && grandparent_scope == NULL)
4784 {
4785 parent->scope = NULL;
4786 parent->scope_set = 1;
4787 return NULL;
4788 }
4789
9c6c53f7
SA
4790 if (pdi->tag == DW_TAG_enumerator)
4791 /* Enumerators should not get the name of the enumeration as a prefix. */
4792 parent->scope = grandparent_scope;
4793 else if (parent->tag == DW_TAG_namespace
f55ee35c 4794 || parent->tag == DW_TAG_module
72bf9492
DJ
4795 || parent->tag == DW_TAG_structure_type
4796 || parent->tag == DW_TAG_class_type
680b30c7 4797 || parent->tag == DW_TAG_interface_type
ceeb3d5a
TT
4798 || parent->tag == DW_TAG_union_type
4799 || parent->tag == DW_TAG_enumeration_type)
72bf9492
DJ
4800 {
4801 if (grandparent_scope == NULL)
4802 parent->scope = parent->name;
4803 else
3e43a32a
MS
4804 parent->scope = typename_concat (&cu->comp_unit_obstack,
4805 grandparent_scope,
f55ee35c 4806 parent->name, 0, cu);
72bf9492 4807 }
72bf9492
DJ
4808 else
4809 {
4810 /* FIXME drow/2004-04-01: What should we be doing with
4811 function-local names? For partial symbols, we should probably be
4812 ignoring them. */
4813 complaint (&symfile_complaints,
e2e0b3e5 4814 _("unhandled containing DIE tag %d for DIE at %d"),
b64f50a1 4815 parent->tag, pdi->offset.sect_off);
72bf9492 4816 parent->scope = grandparent_scope;
c906108c
SS
4817 }
4818
72bf9492
DJ
4819 parent->scope_set = 1;
4820 return parent->scope;
4821}
4822
4823/* Return the fully scoped name associated with PDI, from compilation unit
4824 CU. The result will be allocated with malloc. */
4568ecf9 4825
72bf9492
DJ
4826static char *
4827partial_die_full_name (struct partial_die_info *pdi,
4828 struct dwarf2_cu *cu)
4829{
4830 char *parent_scope;
4831
98bfdba5
PA
4832 /* If this is a template instantiation, we can not work out the
4833 template arguments from partial DIEs. So, unfortunately, we have
4834 to go through the full DIEs. At least any work we do building
4835 types here will be reused if full symbols are loaded later. */
4836 if (pdi->has_template_arguments)
4837 {
4838 fixup_partial_die (pdi, cu);
4839
4840 if (pdi->name != NULL && strchr (pdi->name, '<') == NULL)
4841 {
4842 struct die_info *die;
4843 struct attribute attr;
4844 struct dwarf2_cu *ref_cu = cu;
4845
b64f50a1 4846 /* DW_FORM_ref_addr is using section offset. */
98bfdba5
PA
4847 attr.name = 0;
4848 attr.form = DW_FORM_ref_addr;
4568ecf9 4849 attr.u.unsnd = pdi->offset.sect_off;
98bfdba5
PA
4850 die = follow_die_ref (NULL, &attr, &ref_cu);
4851
4852 return xstrdup (dwarf2_full_name (NULL, die, ref_cu));
4853 }
4854 }
4855
72bf9492
DJ
4856 parent_scope = partial_die_parent_scope (pdi, cu);
4857 if (parent_scope == NULL)
4858 return NULL;
4859 else
f55ee35c 4860 return typename_concat (NULL, parent_scope, pdi->name, 0, cu);
c906108c
SS
4861}
4862
4863static void
72bf9492 4864add_partial_symbol (struct partial_die_info *pdi, struct dwarf2_cu *cu)
c906108c 4865{
e7c27a73 4866 struct objfile *objfile = cu->objfile;
c906108c 4867 CORE_ADDR addr = 0;
decbce07 4868 char *actual_name = NULL;
e142c38c 4869 CORE_ADDR baseaddr;
72bf9492 4870 int built_actual_name = 0;
e142c38c
DJ
4871
4872 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
c906108c 4873
94af9270
KS
4874 actual_name = partial_die_full_name (pdi, cu);
4875 if (actual_name)
4876 built_actual_name = 1;
63d06c5c 4877
72bf9492
DJ
4878 if (actual_name == NULL)
4879 actual_name = pdi->name;
4880
c906108c
SS
4881 switch (pdi->tag)
4882 {
4883 case DW_TAG_subprogram:
2cfa0c8d 4884 if (pdi->is_external || cu->language == language_ada)
c906108c 4885 {
2cfa0c8d
JB
4886 /* brobecker/2007-12-26: Normally, only "external" DIEs are part
4887 of the global scope. But in Ada, we want to be able to access
4888 nested procedures globally. So all Ada subprograms are stored
4889 in the global scope. */
f47fb265 4890 /* prim_record_minimal_symbol (actual_name, pdi->lowpc + baseaddr,
c5aa993b 4891 mst_text, objfile); */
f47fb265
MS
4892 add_psymbol_to_list (actual_name, strlen (actual_name),
4893 built_actual_name,
4894 VAR_DOMAIN, LOC_BLOCK,
4895 &objfile->global_psymbols,
4896 0, pdi->lowpc + baseaddr,
4897 cu->language, objfile);
c906108c
SS
4898 }
4899 else
4900 {
f47fb265 4901 /* prim_record_minimal_symbol (actual_name, pdi->lowpc + baseaddr,
c5aa993b 4902 mst_file_text, objfile); */
f47fb265
MS
4903 add_psymbol_to_list (actual_name, strlen (actual_name),
4904 built_actual_name,
4905 VAR_DOMAIN, LOC_BLOCK,
4906 &objfile->static_psymbols,
4907 0, pdi->lowpc + baseaddr,
4908 cu->language, objfile);
c906108c
SS
4909 }
4910 break;
72929c62
JB
4911 case DW_TAG_constant:
4912 {
4913 struct psymbol_allocation_list *list;
4914
4915 if (pdi->is_external)
4916 list = &objfile->global_psymbols;
4917 else
4918 list = &objfile->static_psymbols;
f47fb265
MS
4919 add_psymbol_to_list (actual_name, strlen (actual_name),
4920 built_actual_name, VAR_DOMAIN, LOC_STATIC,
4921 list, 0, 0, cu->language, objfile);
72929c62
JB
4922 }
4923 break;
c906108c 4924 case DW_TAG_variable:
95554aad
TT
4925 if (pdi->d.locdesc)
4926 addr = decode_locdesc (pdi->d.locdesc, cu);
caac4577 4927
95554aad 4928 if (pdi->d.locdesc
caac4577
JG
4929 && addr == 0
4930 && !dwarf2_per_objfile->has_section_at_zero)
4931 {
4932 /* A global or static variable may also have been stripped
4933 out by the linker if unused, in which case its address
4934 will be nullified; do not add such variables into partial
4935 symbol table then. */
4936 }
4937 else if (pdi->is_external)
c906108c
SS
4938 {
4939 /* Global Variable.
4940 Don't enter into the minimal symbol tables as there is
4941 a minimal symbol table entry from the ELF symbols already.
4942 Enter into partial symbol table if it has a location
4943 descriptor or a type.
4944 If the location descriptor is missing, new_symbol will create
4945 a LOC_UNRESOLVED symbol, the address of the variable will then
4946 be determined from the minimal symbol table whenever the variable
4947 is referenced.
4948 The address for the partial symbol table entry is not
4949 used by GDB, but it comes in handy for debugging partial symbol
4950 table building. */
4951
95554aad 4952 if (pdi->d.locdesc || pdi->has_type)
f47fb265
MS
4953 add_psymbol_to_list (actual_name, strlen (actual_name),
4954 built_actual_name,
4955 VAR_DOMAIN, LOC_STATIC,
4956 &objfile->global_psymbols,
4957 0, addr + baseaddr,
4958 cu->language, objfile);
c906108c
SS
4959 }
4960 else
4961 {
0963b4bd 4962 /* Static Variable. Skip symbols without location descriptors. */
95554aad 4963 if (pdi->d.locdesc == NULL)
decbce07
MS
4964 {
4965 if (built_actual_name)
4966 xfree (actual_name);
4967 return;
4968 }
f47fb265 4969 /* prim_record_minimal_symbol (actual_name, addr + baseaddr,
c5aa993b 4970 mst_file_data, objfile); */
f47fb265
MS
4971 add_psymbol_to_list (actual_name, strlen (actual_name),
4972 built_actual_name,
4973 VAR_DOMAIN, LOC_STATIC,
4974 &objfile->static_psymbols,
4975 0, addr + baseaddr,
4976 cu->language, objfile);
c906108c
SS
4977 }
4978 break;
4979 case DW_TAG_typedef:
4980 case DW_TAG_base_type:
a02abb62 4981 case DW_TAG_subrange_type:
38d518c9 4982 add_psymbol_to_list (actual_name, strlen (actual_name),
04a679b8 4983 built_actual_name,
176620f1 4984 VAR_DOMAIN, LOC_TYPEDEF,
c906108c 4985 &objfile->static_psymbols,
e142c38c 4986 0, (CORE_ADDR) 0, cu->language, objfile);
c906108c 4987 break;
72bf9492
DJ
4988 case DW_TAG_namespace:
4989 add_psymbol_to_list (actual_name, strlen (actual_name),
04a679b8 4990 built_actual_name,
72bf9492
DJ
4991 VAR_DOMAIN, LOC_TYPEDEF,
4992 &objfile->global_psymbols,
4993 0, (CORE_ADDR) 0, cu->language, objfile);
4994 break;
c906108c 4995 case DW_TAG_class_type:
680b30c7 4996 case DW_TAG_interface_type:
c906108c
SS
4997 case DW_TAG_structure_type:
4998 case DW_TAG_union_type:
4999 case DW_TAG_enumeration_type:
fa4028e9
JB
5000 /* Skip external references. The DWARF standard says in the section
5001 about "Structure, Union, and Class Type Entries": "An incomplete
5002 structure, union or class type is represented by a structure,
5003 union or class entry that does not have a byte size attribute
5004 and that has a DW_AT_declaration attribute." */
5005 if (!pdi->has_byte_size && pdi->is_declaration)
decbce07
MS
5006 {
5007 if (built_actual_name)
5008 xfree (actual_name);
5009 return;
5010 }
fa4028e9 5011
63d06c5c
DC
5012 /* NOTE: carlton/2003-10-07: See comment in new_symbol about
5013 static vs. global. */
38d518c9 5014 add_psymbol_to_list (actual_name, strlen (actual_name),
04a679b8 5015 built_actual_name,
176620f1 5016 STRUCT_DOMAIN, LOC_TYPEDEF,
987504bb
JJ
5017 (cu->language == language_cplus
5018 || cu->language == language_java)
63d06c5c
DC
5019 ? &objfile->global_psymbols
5020 : &objfile->static_psymbols,
e142c38c 5021 0, (CORE_ADDR) 0, cu->language, objfile);
c906108c 5022
c906108c
SS
5023 break;
5024 case DW_TAG_enumerator:
38d518c9 5025 add_psymbol_to_list (actual_name, strlen (actual_name),
04a679b8 5026 built_actual_name,
176620f1 5027 VAR_DOMAIN, LOC_CONST,
987504bb
JJ
5028 (cu->language == language_cplus
5029 || cu->language == language_java)
f6fe98ef
DJ
5030 ? &objfile->global_psymbols
5031 : &objfile->static_psymbols,
e142c38c 5032 0, (CORE_ADDR) 0, cu->language, objfile);
c906108c
SS
5033 break;
5034 default:
5035 break;
5036 }
5c4e30ca 5037
72bf9492
DJ
5038 if (built_actual_name)
5039 xfree (actual_name);
c906108c
SS
5040}
5041
5c4e30ca
DC
5042/* Read a partial die corresponding to a namespace; also, add a symbol
5043 corresponding to that namespace to the symbol table. NAMESPACE is
5044 the name of the enclosing namespace. */
91c24f0a 5045
72bf9492
DJ
5046static void
5047add_partial_namespace (struct partial_die_info *pdi,
91c24f0a 5048 CORE_ADDR *lowpc, CORE_ADDR *highpc,
5734ee8b 5049 int need_pc, struct dwarf2_cu *cu)
91c24f0a 5050{
72bf9492 5051 /* Add a symbol for the namespace. */
e7c27a73 5052
72bf9492 5053 add_partial_symbol (pdi, cu);
5c4e30ca
DC
5054
5055 /* Now scan partial symbols in that namespace. */
5056
91c24f0a 5057 if (pdi->has_children)
5734ee8b 5058 scan_partial_symbols (pdi->die_child, lowpc, highpc, need_pc, cu);
91c24f0a
DC
5059}
5060
5d7cb8df
JK
5061/* Read a partial die corresponding to a Fortran module. */
5062
5063static void
5064add_partial_module (struct partial_die_info *pdi, CORE_ADDR *lowpc,
5065 CORE_ADDR *highpc, int need_pc, struct dwarf2_cu *cu)
5066{
f55ee35c 5067 /* Now scan partial symbols in that module. */
5d7cb8df
JK
5068
5069 if (pdi->has_children)
5070 scan_partial_symbols (pdi->die_child, lowpc, highpc, need_pc, cu);
5071}
5072
bc30ff58
JB
5073/* Read a partial die corresponding to a subprogram and create a partial
5074 symbol for that subprogram. When the CU language allows it, this
5075 routine also defines a partial symbol for each nested subprogram
5076 that this subprogram contains.
6e70227d 5077
bc30ff58
JB
5078 DIE my also be a lexical block, in which case we simply search
5079 recursively for suprograms defined inside that lexical block.
5080 Again, this is only performed when the CU language allows this
5081 type of definitions. */
5082
5083static void
5084add_partial_subprogram (struct partial_die_info *pdi,
5085 CORE_ADDR *lowpc, CORE_ADDR *highpc,
5734ee8b 5086 int need_pc, struct dwarf2_cu *cu)
bc30ff58
JB
5087{
5088 if (pdi->tag == DW_TAG_subprogram)
5089 {
5090 if (pdi->has_pc_info)
5091 {
5092 if (pdi->lowpc < *lowpc)
5093 *lowpc = pdi->lowpc;
5094 if (pdi->highpc > *highpc)
5095 *highpc = pdi->highpc;
5734ee8b
DJ
5096 if (need_pc)
5097 {
5098 CORE_ADDR baseaddr;
5099 struct objfile *objfile = cu->objfile;
5100
5101 baseaddr = ANOFFSET (objfile->section_offsets,
5102 SECT_OFF_TEXT (objfile));
5103 addrmap_set_empty (objfile->psymtabs_addrmap,
01637564
DE
5104 pdi->lowpc + baseaddr,
5105 pdi->highpc - 1 + baseaddr,
9291a0cd 5106 cu->per_cu->v.psymtab);
5734ee8b 5107 }
481860b3
GB
5108 }
5109
5110 if (pdi->has_pc_info || (!pdi->is_external && pdi->may_be_inlined))
5111 {
bc30ff58 5112 if (!pdi->is_declaration)
e8d05480
JB
5113 /* Ignore subprogram DIEs that do not have a name, they are
5114 illegal. Do not emit a complaint at this point, we will
5115 do so when we convert this psymtab into a symtab. */
5116 if (pdi->name)
5117 add_partial_symbol (pdi, cu);
bc30ff58
JB
5118 }
5119 }
6e70227d 5120
bc30ff58
JB
5121 if (! pdi->has_children)
5122 return;
5123
5124 if (cu->language == language_ada)
5125 {
5126 pdi = pdi->die_child;
5127 while (pdi != NULL)
5128 {
5129 fixup_partial_die (pdi, cu);
5130 if (pdi->tag == DW_TAG_subprogram
5131 || pdi->tag == DW_TAG_lexical_block)
5734ee8b 5132 add_partial_subprogram (pdi, lowpc, highpc, need_pc, cu);
bc30ff58
JB
5133 pdi = pdi->die_sibling;
5134 }
5135 }
5136}
5137
91c24f0a
DC
5138/* Read a partial die corresponding to an enumeration type. */
5139
72bf9492
DJ
5140static void
5141add_partial_enumeration (struct partial_die_info *enum_pdi,
5142 struct dwarf2_cu *cu)
91c24f0a 5143{
72bf9492 5144 struct partial_die_info *pdi;
91c24f0a
DC
5145
5146 if (enum_pdi->name != NULL)
72bf9492
DJ
5147 add_partial_symbol (enum_pdi, cu);
5148
5149 pdi = enum_pdi->die_child;
5150 while (pdi)
91c24f0a 5151 {
72bf9492 5152 if (pdi->tag != DW_TAG_enumerator || pdi->name == NULL)
e2e0b3e5 5153 complaint (&symfile_complaints, _("malformed enumerator DIE ignored"));
91c24f0a 5154 else
72bf9492
DJ
5155 add_partial_symbol (pdi, cu);
5156 pdi = pdi->die_sibling;
91c24f0a 5157 }
91c24f0a
DC
5158}
5159
6caca83c
CC
5160/* Return the initial uleb128 in the die at INFO_PTR. */
5161
5162static unsigned int
5163peek_abbrev_code (bfd *abfd, gdb_byte *info_ptr)
5164{
5165 unsigned int bytes_read;
5166
5167 return read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
5168}
5169
4bb7a0a7
DJ
5170/* Read the initial uleb128 in the die at INFO_PTR in compilation unit CU.
5171 Return the corresponding abbrev, or NULL if the number is zero (indicating
5172 an empty DIE). In either case *BYTES_READ will be set to the length of
5173 the initial number. */
5174
5175static struct abbrev_info *
fe1b8b76 5176peek_die_abbrev (gdb_byte *info_ptr, unsigned int *bytes_read,
891d2f0b 5177 struct dwarf2_cu *cu)
4bb7a0a7
DJ
5178{
5179 bfd *abfd = cu->objfile->obfd;
5180 unsigned int abbrev_number;
5181 struct abbrev_info *abbrev;
5182
5183 abbrev_number = read_unsigned_leb128 (abfd, info_ptr, bytes_read);
5184
5185 if (abbrev_number == 0)
5186 return NULL;
5187
433df2d4 5188 abbrev = abbrev_table_lookup_abbrev (cu->abbrev_table, abbrev_number);
4bb7a0a7
DJ
5189 if (!abbrev)
5190 {
3e43a32a
MS
5191 error (_("Dwarf Error: Could not find abbrev number %d [in module %s]"),
5192 abbrev_number, bfd_get_filename (abfd));
4bb7a0a7
DJ
5193 }
5194
5195 return abbrev;
5196}
5197
93311388
DE
5198/* Scan the debug information for CU starting at INFO_PTR in buffer BUFFER.
5199 Returns a pointer to the end of a series of DIEs, terminated by an empty
4bb7a0a7
DJ
5200 DIE. Any children of the skipped DIEs will also be skipped. */
5201
fe1b8b76 5202static gdb_byte *
dee91e82 5203skip_children (const struct die_reader_specs *reader, gdb_byte *info_ptr)
4bb7a0a7 5204{
dee91e82 5205 struct dwarf2_cu *cu = reader->cu;
4bb7a0a7
DJ
5206 struct abbrev_info *abbrev;
5207 unsigned int bytes_read;
5208
5209 while (1)
5210 {
5211 abbrev = peek_die_abbrev (info_ptr, &bytes_read, cu);
5212 if (abbrev == NULL)
5213 return info_ptr + bytes_read;
5214 else
dee91e82 5215 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
4bb7a0a7
DJ
5216 }
5217}
5218
93311388
DE
5219/* Scan the debug information for CU starting at INFO_PTR in buffer BUFFER.
5220 INFO_PTR should point just after the initial uleb128 of a DIE, and the
4bb7a0a7
DJ
5221 abbrev corresponding to that skipped uleb128 should be passed in
5222 ABBREV. Returns a pointer to this DIE's sibling, skipping any
5223 children. */
5224
fe1b8b76 5225static gdb_byte *
dee91e82
DE
5226skip_one_die (const struct die_reader_specs *reader, gdb_byte *info_ptr,
5227 struct abbrev_info *abbrev)
4bb7a0a7
DJ
5228{
5229 unsigned int bytes_read;
5230 struct attribute attr;
dee91e82
DE
5231 bfd *abfd = reader->abfd;
5232 struct dwarf2_cu *cu = reader->cu;
5233 gdb_byte *buffer = reader->buffer;
f664829e
DE
5234 const gdb_byte *buffer_end = reader->buffer_end;
5235 gdb_byte *start_info_ptr = info_ptr;
4bb7a0a7
DJ
5236 unsigned int form, i;
5237
5238 for (i = 0; i < abbrev->num_attrs; i++)
5239 {
5240 /* The only abbrev we care about is DW_AT_sibling. */
5241 if (abbrev->attrs[i].name == DW_AT_sibling)
5242 {
dee91e82 5243 read_attribute (reader, &attr, &abbrev->attrs[i], info_ptr);
4bb7a0a7 5244 if (attr.form == DW_FORM_ref_addr)
3e43a32a
MS
5245 complaint (&symfile_complaints,
5246 _("ignoring absolute DW_AT_sibling"));
4bb7a0a7 5247 else
b64f50a1 5248 return buffer + dwarf2_get_ref_die_offset (&attr).sect_off;
4bb7a0a7
DJ
5249 }
5250
5251 /* If it isn't DW_AT_sibling, skip this attribute. */
5252 form = abbrev->attrs[i].form;
5253 skip_attribute:
5254 switch (form)
5255 {
4bb7a0a7 5256 case DW_FORM_ref_addr:
ae411497
TT
5257 /* In DWARF 2, DW_FORM_ref_addr is address sized; in DWARF 3
5258 and later it is offset sized. */
5259 if (cu->header.version == 2)
5260 info_ptr += cu->header.addr_size;
5261 else
5262 info_ptr += cu->header.offset_size;
5263 break;
5264 case DW_FORM_addr:
4bb7a0a7
DJ
5265 info_ptr += cu->header.addr_size;
5266 break;
5267 case DW_FORM_data1:
5268 case DW_FORM_ref1:
5269 case DW_FORM_flag:
5270 info_ptr += 1;
5271 break;
2dc7f7b3
TT
5272 case DW_FORM_flag_present:
5273 break;
4bb7a0a7
DJ
5274 case DW_FORM_data2:
5275 case DW_FORM_ref2:
5276 info_ptr += 2;
5277 break;
5278 case DW_FORM_data4:
5279 case DW_FORM_ref4:
5280 info_ptr += 4;
5281 break;
5282 case DW_FORM_data8:
5283 case DW_FORM_ref8:
55f1336d 5284 case DW_FORM_ref_sig8:
4bb7a0a7
DJ
5285 info_ptr += 8;
5286 break;
5287 case DW_FORM_string:
9b1c24c8 5288 read_direct_string (abfd, info_ptr, &bytes_read);
4bb7a0a7
DJ
5289 info_ptr += bytes_read;
5290 break;
2dc7f7b3 5291 case DW_FORM_sec_offset:
4bb7a0a7
DJ
5292 case DW_FORM_strp:
5293 info_ptr += cu->header.offset_size;
5294 break;
2dc7f7b3 5295 case DW_FORM_exprloc:
4bb7a0a7
DJ
5296 case DW_FORM_block:
5297 info_ptr += read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
5298 info_ptr += bytes_read;
5299 break;
5300 case DW_FORM_block1:
5301 info_ptr += 1 + read_1_byte (abfd, info_ptr);
5302 break;
5303 case DW_FORM_block2:
5304 info_ptr += 2 + read_2_bytes (abfd, info_ptr);
5305 break;
5306 case DW_FORM_block4:
5307 info_ptr += 4 + read_4_bytes (abfd, info_ptr);
5308 break;
5309 case DW_FORM_sdata:
5310 case DW_FORM_udata:
5311 case DW_FORM_ref_udata:
3019eac3
DE
5312 case DW_FORM_GNU_addr_index:
5313 case DW_FORM_GNU_str_index:
f664829e 5314 info_ptr = (gdb_byte *) safe_skip_leb128 (info_ptr, buffer_end);
4bb7a0a7
DJ
5315 break;
5316 case DW_FORM_indirect:
5317 form = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
5318 info_ptr += bytes_read;
5319 /* We need to continue parsing from here, so just go back to
5320 the top. */
5321 goto skip_attribute;
5322
5323 default:
3e43a32a
MS
5324 error (_("Dwarf Error: Cannot handle %s "
5325 "in DWARF reader [in module %s]"),
4bb7a0a7
DJ
5326 dwarf_form_name (form),
5327 bfd_get_filename (abfd));
5328 }
5329 }
5330
5331 if (abbrev->has_children)
dee91e82 5332 return skip_children (reader, info_ptr);
4bb7a0a7
DJ
5333 else
5334 return info_ptr;
5335}
5336
93311388 5337/* Locate ORIG_PDI's sibling.
dee91e82 5338 INFO_PTR should point to the start of the next DIE after ORIG_PDI. */
91c24f0a 5339
fe1b8b76 5340static gdb_byte *
dee91e82
DE
5341locate_pdi_sibling (const struct die_reader_specs *reader,
5342 struct partial_die_info *orig_pdi,
5343 gdb_byte *info_ptr)
91c24f0a
DC
5344{
5345 /* Do we know the sibling already? */
72bf9492 5346
91c24f0a
DC
5347 if (orig_pdi->sibling)
5348 return orig_pdi->sibling;
5349
5350 /* Are there any children to deal with? */
5351
5352 if (!orig_pdi->has_children)
5353 return info_ptr;
5354
4bb7a0a7 5355 /* Skip the children the long way. */
91c24f0a 5356
dee91e82 5357 return skip_children (reader, info_ptr);
91c24f0a
DC
5358}
5359
c906108c
SS
5360/* Expand this partial symbol table into a full symbol table. */
5361
5362static void
fba45db2 5363dwarf2_psymtab_to_symtab (struct partial_symtab *pst)
c906108c 5364{
c906108c
SS
5365 if (pst != NULL)
5366 {
5367 if (pst->readin)
5368 {
3e43a32a
MS
5369 warning (_("bug: psymtab for %s is already read in."),
5370 pst->filename);
c906108c
SS
5371 }
5372 else
5373 {
5374 if (info_verbose)
5375 {
3e43a32a
MS
5376 printf_filtered (_("Reading in symbols for %s..."),
5377 pst->filename);
c906108c
SS
5378 gdb_flush (gdb_stdout);
5379 }
5380
10b3939b
DJ
5381 /* Restore our global data. */
5382 dwarf2_per_objfile = objfile_data (pst->objfile,
5383 dwarf2_objfile_data_key);
5384
b2ab525c
KB
5385 /* If this psymtab is constructed from a debug-only objfile, the
5386 has_section_at_zero flag will not necessarily be correct. We
5387 can get the correct value for this flag by looking at the data
5388 associated with the (presumably stripped) associated objfile. */
5389 if (pst->objfile->separate_debug_objfile_backlink)
5390 {
5391 struct dwarf2_per_objfile *dpo_backlink
5392 = objfile_data (pst->objfile->separate_debug_objfile_backlink,
5393 dwarf2_objfile_data_key);
9a619af0 5394
b2ab525c
KB
5395 dwarf2_per_objfile->has_section_at_zero
5396 = dpo_backlink->has_section_at_zero;
5397 }
5398
98bfdba5
PA
5399 dwarf2_per_objfile->reading_partial_symbols = 0;
5400
c906108c
SS
5401 psymtab_to_symtab_1 (pst);
5402
5403 /* Finish up the debug error message. */
5404 if (info_verbose)
a3f17187 5405 printf_filtered (_("done.\n"));
c906108c
SS
5406 }
5407 }
95554aad
TT
5408
5409 process_cu_includes ();
c906108c 5410}
9cdd5dbd
DE
5411\f
5412/* Reading in full CUs. */
c906108c 5413
10b3939b
DJ
5414/* Add PER_CU to the queue. */
5415
5416static void
95554aad
TT
5417queue_comp_unit (struct dwarf2_per_cu_data *per_cu,
5418 enum language pretend_language)
10b3939b
DJ
5419{
5420 struct dwarf2_queue_item *item;
5421
5422 per_cu->queued = 1;
5423 item = xmalloc (sizeof (*item));
5424 item->per_cu = per_cu;
95554aad 5425 item->pretend_language = pretend_language;
10b3939b
DJ
5426 item->next = NULL;
5427
5428 if (dwarf2_queue == NULL)
5429 dwarf2_queue = item;
5430 else
5431 dwarf2_queue_tail->next = item;
5432
5433 dwarf2_queue_tail = item;
5434}
5435
5436/* Process the queue. */
5437
5438static void
a0f42c21 5439process_queue (void)
10b3939b
DJ
5440{
5441 struct dwarf2_queue_item *item, *next_item;
5442
45cfd468
DE
5443 if (dwarf2_read_debug)
5444 {
5445 fprintf_unfiltered (gdb_stdlog,
5446 "Expanding one or more symtabs of objfile %s ...\n",
5447 dwarf2_per_objfile->objfile->name);
5448 }
5449
03dd20cc
DJ
5450 /* The queue starts out with one item, but following a DIE reference
5451 may load a new CU, adding it to the end of the queue. */
10b3939b
DJ
5452 for (item = dwarf2_queue; item != NULL; dwarf2_queue = item = next_item)
5453 {
9291a0cd
TT
5454 if (dwarf2_per_objfile->using_index
5455 ? !item->per_cu->v.quick->symtab
5456 : (item->per_cu->v.psymtab && !item->per_cu->v.psymtab->readin))
95554aad 5457 process_full_comp_unit (item->per_cu, item->pretend_language);
10b3939b
DJ
5458
5459 item->per_cu->queued = 0;
5460 next_item = item->next;
5461 xfree (item);
5462 }
5463
5464 dwarf2_queue_tail = NULL;
45cfd468
DE
5465
5466 if (dwarf2_read_debug)
5467 {
5468 fprintf_unfiltered (gdb_stdlog, "Done expanding symtabs of %s.\n",
5469 dwarf2_per_objfile->objfile->name);
5470 }
10b3939b
DJ
5471}
5472
5473/* Free all allocated queue entries. This function only releases anything if
5474 an error was thrown; if the queue was processed then it would have been
5475 freed as we went along. */
5476
5477static void
5478dwarf2_release_queue (void *dummy)
5479{
5480 struct dwarf2_queue_item *item, *last;
5481
5482 item = dwarf2_queue;
5483 while (item)
5484 {
5485 /* Anything still marked queued is likely to be in an
5486 inconsistent state, so discard it. */
5487 if (item->per_cu->queued)
5488 {
5489 if (item->per_cu->cu != NULL)
dee91e82 5490 free_one_cached_comp_unit (item->per_cu);
10b3939b
DJ
5491 item->per_cu->queued = 0;
5492 }
5493
5494 last = item;
5495 item = item->next;
5496 xfree (last);
5497 }
5498
5499 dwarf2_queue = dwarf2_queue_tail = NULL;
5500}
5501
5502/* Read in full symbols for PST, and anything it depends on. */
5503
c906108c 5504static void
fba45db2 5505psymtab_to_symtab_1 (struct partial_symtab *pst)
c906108c 5506{
10b3939b 5507 struct dwarf2_per_cu_data *per_cu;
aaa75496
JB
5508 int i;
5509
95554aad
TT
5510 if (pst->readin)
5511 return;
5512
aaa75496 5513 for (i = 0; i < pst->number_of_dependencies; i++)
95554aad
TT
5514 if (!pst->dependencies[i]->readin
5515 && pst->dependencies[i]->user == NULL)
aaa75496
JB
5516 {
5517 /* Inform about additional files that need to be read in. */
5518 if (info_verbose)
5519 {
a3f17187 5520 /* FIXME: i18n: Need to make this a single string. */
aaa75496
JB
5521 fputs_filtered (" ", gdb_stdout);
5522 wrap_here ("");
5523 fputs_filtered ("and ", gdb_stdout);
5524 wrap_here ("");
5525 printf_filtered ("%s...", pst->dependencies[i]->filename);
0963b4bd 5526 wrap_here (""); /* Flush output. */
aaa75496
JB
5527 gdb_flush (gdb_stdout);
5528 }
5529 psymtab_to_symtab_1 (pst->dependencies[i]);
5530 }
5531
e38df1d0 5532 per_cu = pst->read_symtab_private;
10b3939b
DJ
5533
5534 if (per_cu == NULL)
aaa75496
JB
5535 {
5536 /* It's an include file, no symbols to read for it.
5537 Everything is in the parent symtab. */
5538 pst->readin = 1;
5539 return;
5540 }
c906108c 5541
a0f42c21 5542 dw2_do_instantiate_symtab (per_cu);
10b3939b
DJ
5543}
5544
dee91e82
DE
5545/* Trivial hash function for die_info: the hash value of a DIE
5546 is its offset in .debug_info for this objfile. */
10b3939b 5547
dee91e82
DE
5548static hashval_t
5549die_hash (const void *item)
10b3939b 5550{
dee91e82 5551 const struct die_info *die = item;
6502dd73 5552
dee91e82
DE
5553 return die->offset.sect_off;
5554}
63d06c5c 5555
dee91e82
DE
5556/* Trivial comparison function for die_info structures: two DIEs
5557 are equal if they have the same offset. */
98bfdba5 5558
dee91e82
DE
5559static int
5560die_eq (const void *item_lhs, const void *item_rhs)
5561{
5562 const struct die_info *die_lhs = item_lhs;
5563 const struct die_info *die_rhs = item_rhs;
c906108c 5564
dee91e82
DE
5565 return die_lhs->offset.sect_off == die_rhs->offset.sect_off;
5566}
c906108c 5567
dee91e82
DE
5568/* die_reader_func for load_full_comp_unit.
5569 This is identical to read_signatured_type_reader,
5570 but is kept separate for now. */
c906108c 5571
dee91e82
DE
5572static void
5573load_full_comp_unit_reader (const struct die_reader_specs *reader,
5574 gdb_byte *info_ptr,
5575 struct die_info *comp_unit_die,
5576 int has_children,
5577 void *data)
5578{
5579 struct dwarf2_cu *cu = reader->cu;
95554aad 5580 enum language *language_ptr = data;
6caca83c 5581
dee91e82
DE
5582 gdb_assert (cu->die_hash == NULL);
5583 cu->die_hash =
5584 htab_create_alloc_ex (cu->header.length / 12,
5585 die_hash,
5586 die_eq,
5587 NULL,
5588 &cu->comp_unit_obstack,
5589 hashtab_obstack_allocate,
5590 dummy_obstack_deallocate);
e142c38c 5591
dee91e82
DE
5592 if (has_children)
5593 comp_unit_die->child = read_die_and_siblings (reader, info_ptr,
5594 &info_ptr, comp_unit_die);
5595 cu->dies = comp_unit_die;
5596 /* comp_unit_die is not stored in die_hash, no need. */
10b3939b
DJ
5597
5598 /* We try not to read any attributes in this function, because not
9cdd5dbd 5599 all CUs needed for references have been loaded yet, and symbol
10b3939b 5600 table processing isn't initialized. But we have to set the CU language,
dee91e82
DE
5601 or we won't be able to build types correctly.
5602 Similarly, if we do not read the producer, we can not apply
5603 producer-specific interpretation. */
95554aad 5604 prepare_one_comp_unit (cu, cu->dies, *language_ptr);
dee91e82 5605}
10b3939b 5606
dee91e82 5607/* Load the DIEs associated with PER_CU into memory. */
a6c727b2 5608
dee91e82 5609static void
95554aad
TT
5610load_full_comp_unit (struct dwarf2_per_cu_data *this_cu,
5611 enum language pretend_language)
dee91e82 5612{
3019eac3 5613 gdb_assert (! this_cu->is_debug_types);
c5b7e1cb 5614
95554aad
TT
5615 init_cutu_and_read_dies (this_cu, 1, 1, load_full_comp_unit_reader,
5616 &pretend_language);
10b3939b
DJ
5617}
5618
3da10d80
KS
5619/* Add a DIE to the delayed physname list. */
5620
5621static void
5622add_to_method_list (struct type *type, int fnfield_index, int index,
5623 const char *name, struct die_info *die,
5624 struct dwarf2_cu *cu)
5625{
5626 struct delayed_method_info mi;
5627 mi.type = type;
5628 mi.fnfield_index = fnfield_index;
5629 mi.index = index;
5630 mi.name = name;
5631 mi.die = die;
5632 VEC_safe_push (delayed_method_info, cu->method_list, &mi);
5633}
5634
5635/* A cleanup for freeing the delayed method list. */
5636
5637static void
5638free_delayed_list (void *ptr)
5639{
5640 struct dwarf2_cu *cu = (struct dwarf2_cu *) ptr;
5641 if (cu->method_list != NULL)
5642 {
5643 VEC_free (delayed_method_info, cu->method_list);
5644 cu->method_list = NULL;
5645 }
5646}
5647
5648/* Compute the physnames of any methods on the CU's method list.
5649
5650 The computation of method physnames is delayed in order to avoid the
5651 (bad) condition that one of the method's formal parameters is of an as yet
5652 incomplete type. */
5653
5654static void
5655compute_delayed_physnames (struct dwarf2_cu *cu)
5656{
5657 int i;
5658 struct delayed_method_info *mi;
5659 for (i = 0; VEC_iterate (delayed_method_info, cu->method_list, i, mi) ; ++i)
5660 {
1d06ead6 5661 const char *physname;
3da10d80
KS
5662 struct fn_fieldlist *fn_flp
5663 = &TYPE_FN_FIELDLIST (mi->type, mi->fnfield_index);
1d06ead6 5664 physname = dwarf2_physname ((char *) mi->name, mi->die, cu);
3da10d80
KS
5665 fn_flp->fn_fields[mi->index].physname = physname ? physname : "";
5666 }
5667}
5668
a766d390
DE
5669/* Go objects should be embedded in a DW_TAG_module DIE,
5670 and it's not clear if/how imported objects will appear.
5671 To keep Go support simple until that's worked out,
5672 go back through what we've read and create something usable.
5673 We could do this while processing each DIE, and feels kinda cleaner,
5674 but that way is more invasive.
5675 This is to, for example, allow the user to type "p var" or "b main"
5676 without having to specify the package name, and allow lookups
5677 of module.object to work in contexts that use the expression
5678 parser. */
5679
5680static void
5681fixup_go_packaging (struct dwarf2_cu *cu)
5682{
5683 char *package_name = NULL;
5684 struct pending *list;
5685 int i;
5686
5687 for (list = global_symbols; list != NULL; list = list->next)
5688 {
5689 for (i = 0; i < list->nsyms; ++i)
5690 {
5691 struct symbol *sym = list->symbol[i];
5692
5693 if (SYMBOL_LANGUAGE (sym) == language_go
5694 && SYMBOL_CLASS (sym) == LOC_BLOCK)
5695 {
5696 char *this_package_name = go_symbol_package_name (sym);
5697
5698 if (this_package_name == NULL)
5699 continue;
5700 if (package_name == NULL)
5701 package_name = this_package_name;
5702 else
5703 {
5704 if (strcmp (package_name, this_package_name) != 0)
5705 complaint (&symfile_complaints,
5706 _("Symtab %s has objects from two different Go packages: %s and %s"),
5707 (sym->symtab && sym->symtab->filename
5708 ? sym->symtab->filename
5709 : cu->objfile->name),
5710 this_package_name, package_name);
5711 xfree (this_package_name);
5712 }
5713 }
5714 }
5715 }
5716
5717 if (package_name != NULL)
5718 {
5719 struct objfile *objfile = cu->objfile;
5720 struct type *type = init_type (TYPE_CODE_MODULE, 0, 0,
5721 package_name, objfile);
5722 struct symbol *sym;
5723
5724 TYPE_TAG_NAME (type) = TYPE_NAME (type);
5725
5726 sym = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct symbol);
5727 SYMBOL_SET_LANGUAGE (sym, language_go);
5728 SYMBOL_SET_NAMES (sym, package_name, strlen (package_name), 1, objfile);
5729 /* This is not VAR_DOMAIN because we want a way to ensure a lookup of,
5730 e.g., "main" finds the "main" module and not C's main(). */
5731 SYMBOL_DOMAIN (sym) = STRUCT_DOMAIN;
5732 SYMBOL_CLASS (sym) = LOC_TYPEDEF;
5733 SYMBOL_TYPE (sym) = type;
5734
5735 add_symbol_to_list (sym, &global_symbols);
5736
5737 xfree (package_name);
5738 }
5739}
5740
95554aad
TT
5741static void compute_symtab_includes (struct dwarf2_per_cu_data *per_cu);
5742
5743/* Return the symtab for PER_CU. This works properly regardless of
5744 whether we're using the index or psymtabs. */
5745
5746static struct symtab *
5747get_symtab (struct dwarf2_per_cu_data *per_cu)
5748{
5749 return (dwarf2_per_objfile->using_index
5750 ? per_cu->v.quick->symtab
5751 : per_cu->v.psymtab->symtab);
5752}
5753
5754/* A helper function for computing the list of all symbol tables
5755 included by PER_CU. */
5756
5757static void
5758recursively_compute_inclusions (VEC (dwarf2_per_cu_ptr) **result,
5759 htab_t all_children,
5760 struct dwarf2_per_cu_data *per_cu)
5761{
5762 void **slot;
5763 int ix;
5764 struct dwarf2_per_cu_data *iter;
5765
5766 slot = htab_find_slot (all_children, per_cu, INSERT);
5767 if (*slot != NULL)
5768 {
5769 /* This inclusion and its children have been processed. */
5770 return;
5771 }
5772
5773 *slot = per_cu;
5774 /* Only add a CU if it has a symbol table. */
5775 if (get_symtab (per_cu) != NULL)
5776 VEC_safe_push (dwarf2_per_cu_ptr, *result, per_cu);
5777
5778 for (ix = 0;
5779 VEC_iterate (dwarf2_per_cu_ptr, per_cu->imported_symtabs, ix, iter);
5780 ++ix)
5781 recursively_compute_inclusions (result, all_children, iter);
5782}
5783
5784/* Compute the symtab 'includes' fields for the symtab related to
5785 PER_CU. */
5786
5787static void
5788compute_symtab_includes (struct dwarf2_per_cu_data *per_cu)
5789{
5790 if (!VEC_empty (dwarf2_per_cu_ptr, per_cu->imported_symtabs))
5791 {
5792 int ix, len;
5793 struct dwarf2_per_cu_data *iter;
5794 VEC (dwarf2_per_cu_ptr) *result_children = NULL;
5795 htab_t all_children;
5796 struct symtab *symtab = get_symtab (per_cu);
5797
5798 /* If we don't have a symtab, we can just skip this case. */
5799 if (symtab == NULL)
5800 return;
5801
5802 all_children = htab_create_alloc (1, htab_hash_pointer, htab_eq_pointer,
5803 NULL, xcalloc, xfree);
5804
5805 for (ix = 0;
5806 VEC_iterate (dwarf2_per_cu_ptr, per_cu->imported_symtabs,
5807 ix, iter);
5808 ++ix)
5809 recursively_compute_inclusions (&result_children, all_children, iter);
5810
5811 /* Now we have a transitive closure of all the included CUs, so
5812 we can convert it to a list of symtabs. */
5813 len = VEC_length (dwarf2_per_cu_ptr, result_children);
5814 symtab->includes
5815 = obstack_alloc (&dwarf2_per_objfile->objfile->objfile_obstack,
5816 (len + 1) * sizeof (struct symtab *));
5817 for (ix = 0;
5818 VEC_iterate (dwarf2_per_cu_ptr, result_children, ix, iter);
5819 ++ix)
5820 symtab->includes[ix] = get_symtab (iter);
5821 symtab->includes[len] = NULL;
5822
5823 VEC_free (dwarf2_per_cu_ptr, result_children);
5824 htab_delete (all_children);
5825 }
5826}
5827
5828/* Compute the 'includes' field for the symtabs of all the CUs we just
5829 read. */
5830
5831static void
5832process_cu_includes (void)
5833{
5834 int ix;
5835 struct dwarf2_per_cu_data *iter;
5836
5837 for (ix = 0;
5838 VEC_iterate (dwarf2_per_cu_ptr, dwarf2_per_objfile->just_read_cus,
5839 ix, iter);
5840 ++ix)
5841 compute_symtab_includes (iter);
5842
5843 VEC_free (dwarf2_per_cu_ptr, dwarf2_per_objfile->just_read_cus);
5844}
5845
9cdd5dbd 5846/* Generate full symbol information for PER_CU, whose DIEs have
10b3939b
DJ
5847 already been loaded into memory. */
5848
5849static void
95554aad
TT
5850process_full_comp_unit (struct dwarf2_per_cu_data *per_cu,
5851 enum language pretend_language)
10b3939b 5852{
10b3939b 5853 struct dwarf2_cu *cu = per_cu->cu;
9291a0cd 5854 struct objfile *objfile = per_cu->objfile;
10b3939b
DJ
5855 CORE_ADDR lowpc, highpc;
5856 struct symtab *symtab;
3da10d80 5857 struct cleanup *back_to, *delayed_list_cleanup;
10b3939b
DJ
5858 CORE_ADDR baseaddr;
5859
45cfd468
DE
5860 if (dwarf2_read_debug)
5861 {
5862 fprintf_unfiltered (gdb_stdlog,
5863 "Expanding symtab of %s at offset 0x%x\n",
5864 per_cu->is_debug_types ? "TU" : "CU",
5865 per_cu->offset.sect_off);
5866 }
5867
10b3939b
DJ
5868 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
5869
10b3939b
DJ
5870 buildsym_init ();
5871 back_to = make_cleanup (really_free_pendings, NULL);
3da10d80 5872 delayed_list_cleanup = make_cleanup (free_delayed_list, cu);
10b3939b
DJ
5873
5874 cu->list_in_scope = &file_symbols;
c906108c 5875
95554aad
TT
5876 cu->language = pretend_language;
5877 cu->language_defn = language_def (cu->language);
5878
c906108c 5879 /* Do line number decoding in read_file_scope () */
10b3939b 5880 process_die (cu->dies, cu);
c906108c 5881
a766d390
DE
5882 /* For now fudge the Go package. */
5883 if (cu->language == language_go)
5884 fixup_go_packaging (cu);
5885
3da10d80
KS
5886 /* Now that we have processed all the DIEs in the CU, all the types
5887 should be complete, and it should now be safe to compute all of the
5888 physnames. */
5889 compute_delayed_physnames (cu);
5890 do_cleanups (delayed_list_cleanup);
5891
fae299cd
DC
5892 /* Some compilers don't define a DW_AT_high_pc attribute for the
5893 compilation unit. If the DW_AT_high_pc is missing, synthesize
5894 it, by scanning the DIE's below the compilation unit. */
10b3939b 5895 get_scope_pc_bounds (cu->dies, &lowpc, &highpc, cu);
c906108c 5896
613e1657 5897 symtab = end_symtab (highpc + baseaddr, objfile, SECT_OFF_TEXT (objfile));
c906108c 5898
8be455d7 5899 if (symtab != NULL)
c906108c 5900 {
df15bd07 5901 int gcc_4_minor = producer_is_gcc_ge_4 (cu->producer);
4632c0d0 5902
8be455d7
JK
5903 /* Set symtab language to language from DW_AT_language. If the
5904 compilation is from a C file generated by language preprocessors, do
5905 not set the language if it was already deduced by start_subfile. */
5906 if (!(cu->language == language_c && symtab->language != language_c))
5907 symtab->language = cu->language;
5908
5909 /* GCC-4.0 has started to support -fvar-tracking. GCC-3.x still can
5910 produce DW_AT_location with location lists but it can be possibly
ab260dad
JK
5911 invalid without -fvar-tracking. Still up to GCC-4.4.x incl. 4.4.0
5912 there were bugs in prologue debug info, fixed later in GCC-4.5
5913 by "unwind info for epilogues" patch (which is not directly related).
8be455d7
JK
5914
5915 For -gdwarf-4 type units LOCATIONS_VALID indication is fortunately not
5916 needed, it would be wrong due to missing DW_AT_producer there.
5917
5918 Still one can confuse GDB by using non-standard GCC compilation
5919 options - this waits on GCC PR other/32998 (-frecord-gcc-switches).
5920 */
ab260dad 5921 if (cu->has_loclist && gcc_4_minor >= 5)
8be455d7 5922 symtab->locations_valid = 1;
e0d00bc7
JK
5923
5924 if (gcc_4_minor >= 5)
5925 symtab->epilogue_unwind_valid = 1;
96408a79
SA
5926
5927 symtab->call_site_htab = cu->call_site_htab;
c906108c 5928 }
9291a0cd
TT
5929
5930 if (dwarf2_per_objfile->using_index)
5931 per_cu->v.quick->symtab = symtab;
5932 else
5933 {
5934 struct partial_symtab *pst = per_cu->v.psymtab;
5935 pst->symtab = symtab;
5936 pst->readin = 1;
5937 }
c906108c 5938
95554aad
TT
5939 /* Push it for inclusion processing later. */
5940 VEC_safe_push (dwarf2_per_cu_ptr, dwarf2_per_objfile->just_read_cus, per_cu);
5941
c906108c 5942 do_cleanups (back_to);
45cfd468
DE
5943
5944 if (dwarf2_read_debug)
5945 {
5946 fprintf_unfiltered (gdb_stdlog,
5947 "Done expanding symtab of %s at offset 0x%x\n",
5948 per_cu->is_debug_types ? "TU" : "CU",
5949 per_cu->offset.sect_off);
5950 }
c906108c
SS
5951}
5952
95554aad
TT
5953/* Process an imported unit DIE. */
5954
5955static void
5956process_imported_unit_die (struct die_info *die, struct dwarf2_cu *cu)
5957{
5958 struct attribute *attr;
5959
5960 attr = dwarf2_attr (die, DW_AT_import, cu);
5961 if (attr != NULL)
5962 {
5963 struct dwarf2_per_cu_data *per_cu;
5964 struct symtab *imported_symtab;
5965 sect_offset offset;
5966
5967 offset = dwarf2_get_ref_die_offset (attr);
5968 per_cu = dwarf2_find_containing_comp_unit (offset, cu->objfile);
5969
5970 /* Queue the unit, if needed. */
5971 if (maybe_queue_comp_unit (cu, per_cu, cu->language))
5972 load_full_comp_unit (per_cu, cu->language);
5973
5974 VEC_safe_push (dwarf2_per_cu_ptr, cu->per_cu->imported_symtabs,
5975 per_cu);
5976 }
5977}
5978
c906108c
SS
5979/* Process a die and its children. */
5980
5981static void
e7c27a73 5982process_die (struct die_info *die, struct dwarf2_cu *cu)
c906108c
SS
5983{
5984 switch (die->tag)
5985 {
5986 case DW_TAG_padding:
5987 break;
5988 case DW_TAG_compile_unit:
95554aad 5989 case DW_TAG_partial_unit:
e7c27a73 5990 read_file_scope (die, cu);
c906108c 5991 break;
348e048f
DE
5992 case DW_TAG_type_unit:
5993 read_type_unit_scope (die, cu);
5994 break;
c906108c 5995 case DW_TAG_subprogram:
c906108c 5996 case DW_TAG_inlined_subroutine:
edb3359d 5997 read_func_scope (die, cu);
c906108c
SS
5998 break;
5999 case DW_TAG_lexical_block:
14898363
L
6000 case DW_TAG_try_block:
6001 case DW_TAG_catch_block:
e7c27a73 6002 read_lexical_block_scope (die, cu);
c906108c 6003 break;
96408a79
SA
6004 case DW_TAG_GNU_call_site:
6005 read_call_site_scope (die, cu);
6006 break;
c906108c 6007 case DW_TAG_class_type:
680b30c7 6008 case DW_TAG_interface_type:
c906108c
SS
6009 case DW_TAG_structure_type:
6010 case DW_TAG_union_type:
134d01f1 6011 process_structure_scope (die, cu);
c906108c
SS
6012 break;
6013 case DW_TAG_enumeration_type:
134d01f1 6014 process_enumeration_scope (die, cu);
c906108c 6015 break;
134d01f1 6016
f792889a
DJ
6017 /* These dies have a type, but processing them does not create
6018 a symbol or recurse to process the children. Therefore we can
6019 read them on-demand through read_type_die. */
c906108c 6020 case DW_TAG_subroutine_type:
72019c9c 6021 case DW_TAG_set_type:
c906108c 6022 case DW_TAG_array_type:
c906108c 6023 case DW_TAG_pointer_type:
c906108c 6024 case DW_TAG_ptr_to_member_type:
c906108c 6025 case DW_TAG_reference_type:
c906108c 6026 case DW_TAG_string_type:
c906108c 6027 break;
134d01f1 6028
c906108c 6029 case DW_TAG_base_type:
a02abb62 6030 case DW_TAG_subrange_type:
cb249c71 6031 case DW_TAG_typedef:
134d01f1
DJ
6032 /* Add a typedef symbol for the type definition, if it has a
6033 DW_AT_name. */
f792889a 6034 new_symbol (die, read_type_die (die, cu), cu);
a02abb62 6035 break;
c906108c 6036 case DW_TAG_common_block:
e7c27a73 6037 read_common_block (die, cu);
c906108c
SS
6038 break;
6039 case DW_TAG_common_inclusion:
6040 break;
d9fa45fe 6041 case DW_TAG_namespace:
63d06c5c 6042 processing_has_namespace_info = 1;
e7c27a73 6043 read_namespace (die, cu);
d9fa45fe 6044 break;
5d7cb8df 6045 case DW_TAG_module:
f55ee35c 6046 processing_has_namespace_info = 1;
5d7cb8df
JK
6047 read_module (die, cu);
6048 break;
d9fa45fe
DC
6049 case DW_TAG_imported_declaration:
6050 case DW_TAG_imported_module:
63d06c5c 6051 processing_has_namespace_info = 1;
27aa8d6a
SW
6052 if (die->child != NULL && (die->tag == DW_TAG_imported_declaration
6053 || cu->language != language_fortran))
6054 complaint (&symfile_complaints, _("Tag '%s' has unexpected children"),
6055 dwarf_tag_name (die->tag));
6056 read_import_statement (die, cu);
d9fa45fe 6057 break;
95554aad
TT
6058
6059 case DW_TAG_imported_unit:
6060 process_imported_unit_die (die, cu);
6061 break;
6062
c906108c 6063 default:
e7c27a73 6064 new_symbol (die, NULL, cu);
c906108c
SS
6065 break;
6066 }
6067}
6068
94af9270
KS
6069/* A helper function for dwarf2_compute_name which determines whether DIE
6070 needs to have the name of the scope prepended to the name listed in the
6071 die. */
6072
6073static int
6074die_needs_namespace (struct die_info *die, struct dwarf2_cu *cu)
6075{
1c809c68
TT
6076 struct attribute *attr;
6077
94af9270
KS
6078 switch (die->tag)
6079 {
6080 case DW_TAG_namespace:
6081 case DW_TAG_typedef:
6082 case DW_TAG_class_type:
6083 case DW_TAG_interface_type:
6084 case DW_TAG_structure_type:
6085 case DW_TAG_union_type:
6086 case DW_TAG_enumeration_type:
6087 case DW_TAG_enumerator:
6088 case DW_TAG_subprogram:
6089 case DW_TAG_member:
6090 return 1;
6091
6092 case DW_TAG_variable:
c2b0a229 6093 case DW_TAG_constant:
94af9270
KS
6094 /* We only need to prefix "globally" visible variables. These include
6095 any variable marked with DW_AT_external or any variable that
6096 lives in a namespace. [Variables in anonymous namespaces
6097 require prefixing, but they are not DW_AT_external.] */
6098
6099 if (dwarf2_attr (die, DW_AT_specification, cu))
6100 {
6101 struct dwarf2_cu *spec_cu = cu;
9a619af0 6102
94af9270
KS
6103 return die_needs_namespace (die_specification (die, &spec_cu),
6104 spec_cu);
6105 }
6106
1c809c68 6107 attr = dwarf2_attr (die, DW_AT_external, cu);
f55ee35c
JK
6108 if (attr == NULL && die->parent->tag != DW_TAG_namespace
6109 && die->parent->tag != DW_TAG_module)
1c809c68
TT
6110 return 0;
6111 /* A variable in a lexical block of some kind does not need a
6112 namespace, even though in C++ such variables may be external
6113 and have a mangled name. */
6114 if (die->parent->tag == DW_TAG_lexical_block
6115 || die->parent->tag == DW_TAG_try_block
1054b214
TT
6116 || die->parent->tag == DW_TAG_catch_block
6117 || die->parent->tag == DW_TAG_subprogram)
1c809c68
TT
6118 return 0;
6119 return 1;
94af9270
KS
6120
6121 default:
6122 return 0;
6123 }
6124}
6125
98bfdba5
PA
6126/* Retrieve the last character from a mem_file. */
6127
6128static void
6129do_ui_file_peek_last (void *object, const char *buffer, long length)
6130{
6131 char *last_char_p = (char *) object;
6132
6133 if (length > 0)
6134 *last_char_p = buffer[length - 1];
6135}
6136
94af9270 6137/* Compute the fully qualified name of DIE in CU. If PHYSNAME is nonzero,
a766d390
DE
6138 compute the physname for the object, which include a method's:
6139 - formal parameters (C++/Java),
6140 - receiver type (Go),
6141 - return type (Java).
6142
6143 The term "physname" is a bit confusing.
6144 For C++, for example, it is the demangled name.
6145 For Go, for example, it's the mangled name.
94af9270 6146
af6b7be1
JB
6147 For Ada, return the DIE's linkage name rather than the fully qualified
6148 name. PHYSNAME is ignored..
6149
94af9270
KS
6150 The result is allocated on the objfile_obstack and canonicalized. */
6151
6152static const char *
6153dwarf2_compute_name (char *name, struct die_info *die, struct dwarf2_cu *cu,
6154 int physname)
6155{
bb5ed363
DE
6156 struct objfile *objfile = cu->objfile;
6157
94af9270
KS
6158 if (name == NULL)
6159 name = dwarf2_name (die, cu);
6160
f55ee35c
JK
6161 /* For Fortran GDB prefers DW_AT_*linkage_name if present but otherwise
6162 compute it by typename_concat inside GDB. */
6163 if (cu->language == language_ada
6164 || (cu->language == language_fortran && physname))
6165 {
6166 /* For Ada unit, we prefer the linkage name over the name, as
6167 the former contains the exported name, which the user expects
6168 to be able to reference. Ideally, we want the user to be able
6169 to reference this entity using either natural or linkage name,
6170 but we haven't started looking at this enhancement yet. */
6171 struct attribute *attr;
6172
6173 attr = dwarf2_attr (die, DW_AT_linkage_name, cu);
6174 if (attr == NULL)
6175 attr = dwarf2_attr (die, DW_AT_MIPS_linkage_name, cu);
6176 if (attr && DW_STRING (attr))
6177 return DW_STRING (attr);
6178 }
6179
94af9270
KS
6180 /* These are the only languages we know how to qualify names in. */
6181 if (name != NULL
f55ee35c
JK
6182 && (cu->language == language_cplus || cu->language == language_java
6183 || cu->language == language_fortran))
94af9270
KS
6184 {
6185 if (die_needs_namespace (die, cu))
6186 {
6187 long length;
0d5cff50 6188 const char *prefix;
94af9270
KS
6189 struct ui_file *buf;
6190
6191 prefix = determine_prefix (die, cu);
6192 buf = mem_fileopen ();
6193 if (*prefix != '\0')
6194 {
f55ee35c
JK
6195 char *prefixed_name = typename_concat (NULL, prefix, name,
6196 physname, cu);
9a619af0 6197
94af9270
KS
6198 fputs_unfiltered (prefixed_name, buf);
6199 xfree (prefixed_name);
6200 }
6201 else
62d5b8da 6202 fputs_unfiltered (name, buf);
94af9270 6203
98bfdba5
PA
6204 /* Template parameters may be specified in the DIE's DW_AT_name, or
6205 as children with DW_TAG_template_type_param or
6206 DW_TAG_value_type_param. If the latter, add them to the name
6207 here. If the name already has template parameters, then
6208 skip this step; some versions of GCC emit both, and
6209 it is more efficient to use the pre-computed name.
6210
6211 Something to keep in mind about this process: it is very
6212 unlikely, or in some cases downright impossible, to produce
6213 something that will match the mangled name of a function.
6214 If the definition of the function has the same debug info,
6215 we should be able to match up with it anyway. But fallbacks
6216 using the minimal symbol, for instance to find a method
6217 implemented in a stripped copy of libstdc++, will not work.
6218 If we do not have debug info for the definition, we will have to
6219 match them up some other way.
6220
6221 When we do name matching there is a related problem with function
6222 templates; two instantiated function templates are allowed to
6223 differ only by their return types, which we do not add here. */
6224
6225 if (cu->language == language_cplus && strchr (name, '<') == NULL)
6226 {
6227 struct attribute *attr;
6228 struct die_info *child;
6229 int first = 1;
6230
6231 die->building_fullname = 1;
6232
6233 for (child = die->child; child != NULL; child = child->sibling)
6234 {
6235 struct type *type;
12df843f 6236 LONGEST value;
98bfdba5
PA
6237 gdb_byte *bytes;
6238 struct dwarf2_locexpr_baton *baton;
6239 struct value *v;
6240
6241 if (child->tag != DW_TAG_template_type_param
6242 && child->tag != DW_TAG_template_value_param)
6243 continue;
6244
6245 if (first)
6246 {
6247 fputs_unfiltered ("<", buf);
6248 first = 0;
6249 }
6250 else
6251 fputs_unfiltered (", ", buf);
6252
6253 attr = dwarf2_attr (child, DW_AT_type, cu);
6254 if (attr == NULL)
6255 {
6256 complaint (&symfile_complaints,
6257 _("template parameter missing DW_AT_type"));
6258 fputs_unfiltered ("UNKNOWN_TYPE", buf);
6259 continue;
6260 }
6261 type = die_type (child, cu);
6262
6263 if (child->tag == DW_TAG_template_type_param)
6264 {
6265 c_print_type (type, "", buf, -1, 0);
6266 continue;
6267 }
6268
6269 attr = dwarf2_attr (child, DW_AT_const_value, cu);
6270 if (attr == NULL)
6271 {
6272 complaint (&symfile_complaints,
3e43a32a
MS
6273 _("template parameter missing "
6274 "DW_AT_const_value"));
98bfdba5
PA
6275 fputs_unfiltered ("UNKNOWN_VALUE", buf);
6276 continue;
6277 }
6278
6279 dwarf2_const_value_attr (attr, type, name,
6280 &cu->comp_unit_obstack, cu,
6281 &value, &bytes, &baton);
6282
6283 if (TYPE_NOSIGN (type))
6284 /* GDB prints characters as NUMBER 'CHAR'. If that's
6285 changed, this can use value_print instead. */
6286 c_printchar (value, type, buf);
6287 else
6288 {
6289 struct value_print_options opts;
6290
6291 if (baton != NULL)
6292 v = dwarf2_evaluate_loc_desc (type, NULL,
6293 baton->data,
6294 baton->size,
6295 baton->per_cu);
6296 else if (bytes != NULL)
6297 {
6298 v = allocate_value (type);
6299 memcpy (value_contents_writeable (v), bytes,
6300 TYPE_LENGTH (type));
6301 }
6302 else
6303 v = value_from_longest (type, value);
6304
3e43a32a
MS
6305 /* Specify decimal so that we do not depend on
6306 the radix. */
98bfdba5
PA
6307 get_formatted_print_options (&opts, 'd');
6308 opts.raw = 1;
6309 value_print (v, buf, &opts);
6310 release_value (v);
6311 value_free (v);
6312 }
6313 }
6314
6315 die->building_fullname = 0;
6316
6317 if (!first)
6318 {
6319 /* Close the argument list, with a space if necessary
6320 (nested templates). */
6321 char last_char = '\0';
6322 ui_file_put (buf, do_ui_file_peek_last, &last_char);
6323 if (last_char == '>')
6324 fputs_unfiltered (" >", buf);
6325 else
6326 fputs_unfiltered (">", buf);
6327 }
6328 }
6329
94af9270
KS
6330 /* For Java and C++ methods, append formal parameter type
6331 information, if PHYSNAME. */
6e70227d 6332
94af9270
KS
6333 if (physname && die->tag == DW_TAG_subprogram
6334 && (cu->language == language_cplus
6335 || cu->language == language_java))
6336 {
6337 struct type *type = read_type_die (die, cu);
6338
3167638f 6339 c_type_print_args (type, buf, 1, cu->language);
94af9270
KS
6340
6341 if (cu->language == language_java)
6342 {
6343 /* For java, we must append the return type to method
0963b4bd 6344 names. */
94af9270
KS
6345 if (die->tag == DW_TAG_subprogram)
6346 java_print_type (TYPE_TARGET_TYPE (type), "", buf,
6347 0, 0);
6348 }
6349 else if (cu->language == language_cplus)
6350 {
60430eff
DJ
6351 /* Assume that an artificial first parameter is
6352 "this", but do not crash if it is not. RealView
6353 marks unnamed (and thus unused) parameters as
6354 artificial; there is no way to differentiate
6355 the two cases. */
94af9270
KS
6356 if (TYPE_NFIELDS (type) > 0
6357 && TYPE_FIELD_ARTIFICIAL (type, 0)
60430eff 6358 && TYPE_CODE (TYPE_FIELD_TYPE (type, 0)) == TYPE_CODE_PTR
3e43a32a
MS
6359 && TYPE_CONST (TYPE_TARGET_TYPE (TYPE_FIELD_TYPE (type,
6360 0))))
94af9270
KS
6361 fputs_unfiltered (" const", buf);
6362 }
6363 }
6364
bb5ed363 6365 name = ui_file_obsavestring (buf, &objfile->objfile_obstack,
94af9270
KS
6366 &length);
6367 ui_file_delete (buf);
6368
6369 if (cu->language == language_cplus)
6370 {
6371 char *cname
6372 = dwarf2_canonicalize_name (name, cu,
bb5ed363 6373 &objfile->objfile_obstack);
9a619af0 6374
94af9270
KS
6375 if (cname != NULL)
6376 name = cname;
6377 }
6378 }
6379 }
6380
6381 return name;
6382}
6383
0114d602
DJ
6384/* Return the fully qualified name of DIE, based on its DW_AT_name.
6385 If scope qualifiers are appropriate they will be added. The result
6386 will be allocated on the objfile_obstack, or NULL if the DIE does
94af9270
KS
6387 not have a name. NAME may either be from a previous call to
6388 dwarf2_name or NULL.
6389
0963b4bd 6390 The output string will be canonicalized (if C++/Java). */
0114d602
DJ
6391
6392static const char *
94af9270 6393dwarf2_full_name (char *name, struct die_info *die, struct dwarf2_cu *cu)
0114d602 6394{
94af9270
KS
6395 return dwarf2_compute_name (name, die, cu, 0);
6396}
0114d602 6397
94af9270
KS
6398/* Construct a physname for the given DIE in CU. NAME may either be
6399 from a previous call to dwarf2_name or NULL. The result will be
6400 allocated on the objfile_objstack or NULL if the DIE does not have a
6401 name.
0114d602 6402
94af9270 6403 The output string will be canonicalized (if C++/Java). */
0114d602 6404
94af9270
KS
6405static const char *
6406dwarf2_physname (char *name, struct die_info *die, struct dwarf2_cu *cu)
6407{
bb5ed363 6408 struct objfile *objfile = cu->objfile;
900e11f9
JK
6409 struct attribute *attr;
6410 const char *retval, *mangled = NULL, *canon = NULL;
6411 struct cleanup *back_to;
6412 int need_copy = 1;
6413
6414 /* In this case dwarf2_compute_name is just a shortcut not building anything
6415 on its own. */
6416 if (!die_needs_namespace (die, cu))
6417 return dwarf2_compute_name (name, die, cu, 1);
6418
6419 back_to = make_cleanup (null_cleanup, NULL);
6420
6421 attr = dwarf2_attr (die, DW_AT_linkage_name, cu);
6422 if (!attr)
6423 attr = dwarf2_attr (die, DW_AT_MIPS_linkage_name, cu);
6424
6425 /* DW_AT_linkage_name is missing in some cases - depend on what GDB
6426 has computed. */
6427 if (attr && DW_STRING (attr))
6428 {
6429 char *demangled;
6430
6431 mangled = DW_STRING (attr);
6432
6433 /* Use DMGL_RET_DROP for C++ template functions to suppress their return
6434 type. It is easier for GDB users to search for such functions as
6435 `name(params)' than `long name(params)'. In such case the minimal
6436 symbol names do not match the full symbol names but for template
6437 functions there is never a need to look up their definition from their
6438 declaration so the only disadvantage remains the minimal symbol
6439 variant `long name(params)' does not have the proper inferior type.
6440 */
6441
a766d390
DE
6442 if (cu->language == language_go)
6443 {
6444 /* This is a lie, but we already lie to the caller new_symbol_full.
6445 new_symbol_full assumes we return the mangled name.
6446 This just undoes that lie until things are cleaned up. */
6447 demangled = NULL;
6448 }
6449 else
6450 {
6451 demangled = cplus_demangle (mangled,
6452 (DMGL_PARAMS | DMGL_ANSI
6453 | (cu->language == language_java
6454 ? DMGL_JAVA | DMGL_RET_POSTFIX
6455 : DMGL_RET_DROP)));
6456 }
900e11f9
JK
6457 if (demangled)
6458 {
6459 make_cleanup (xfree, demangled);
6460 canon = demangled;
6461 }
6462 else
6463 {
6464 canon = mangled;
6465 need_copy = 0;
6466 }
6467 }
6468
6469 if (canon == NULL || check_physname)
6470 {
6471 const char *physname = dwarf2_compute_name (name, die, cu, 1);
6472
6473 if (canon != NULL && strcmp (physname, canon) != 0)
6474 {
6475 /* It may not mean a bug in GDB. The compiler could also
6476 compute DW_AT_linkage_name incorrectly. But in such case
6477 GDB would need to be bug-to-bug compatible. */
6478
6479 complaint (&symfile_complaints,
6480 _("Computed physname <%s> does not match demangled <%s> "
6481 "(from linkage <%s>) - DIE at 0x%x [in module %s]"),
b64f50a1 6482 physname, canon, mangled, die->offset.sect_off, objfile->name);
900e11f9
JK
6483
6484 /* Prefer DW_AT_linkage_name (in the CANON form) - when it
6485 is available here - over computed PHYSNAME. It is safer
6486 against both buggy GDB and buggy compilers. */
6487
6488 retval = canon;
6489 }
6490 else
6491 {
6492 retval = physname;
6493 need_copy = 0;
6494 }
6495 }
6496 else
6497 retval = canon;
6498
6499 if (need_copy)
6500 retval = obsavestring (retval, strlen (retval),
bb5ed363 6501 &objfile->objfile_obstack);
900e11f9
JK
6502
6503 do_cleanups (back_to);
6504 return retval;
0114d602
DJ
6505}
6506
27aa8d6a
SW
6507/* Read the import statement specified by the given die and record it. */
6508
6509static void
6510read_import_statement (struct die_info *die, struct dwarf2_cu *cu)
6511{
bb5ed363 6512 struct objfile *objfile = cu->objfile;
27aa8d6a 6513 struct attribute *import_attr;
32019081 6514 struct die_info *imported_die, *child_die;
de4affc9 6515 struct dwarf2_cu *imported_cu;
27aa8d6a 6516 const char *imported_name;
794684b6 6517 const char *imported_name_prefix;
13387711
SW
6518 const char *canonical_name;
6519 const char *import_alias;
6520 const char *imported_declaration = NULL;
794684b6 6521 const char *import_prefix;
32019081
JK
6522 VEC (const_char_ptr) *excludes = NULL;
6523 struct cleanup *cleanups;
13387711
SW
6524
6525 char *temp;
27aa8d6a
SW
6526
6527 import_attr = dwarf2_attr (die, DW_AT_import, cu);
6528 if (import_attr == NULL)
6529 {
6530 complaint (&symfile_complaints, _("Tag '%s' has no DW_AT_import"),
6531 dwarf_tag_name (die->tag));
6532 return;
6533 }
6534
de4affc9
CC
6535 imported_cu = cu;
6536 imported_die = follow_die_ref_or_sig (die, import_attr, &imported_cu);
6537 imported_name = dwarf2_name (imported_die, imported_cu);
27aa8d6a
SW
6538 if (imported_name == NULL)
6539 {
6540 /* GCC bug: https://bugzilla.redhat.com/show_bug.cgi?id=506524
6541
6542 The import in the following code:
6543 namespace A
6544 {
6545 typedef int B;
6546 }
6547
6548 int main ()
6549 {
6550 using A::B;
6551 B b;
6552 return b;
6553 }
6554
6555 ...
6556 <2><51>: Abbrev Number: 3 (DW_TAG_imported_declaration)
6557 <52> DW_AT_decl_file : 1
6558 <53> DW_AT_decl_line : 6
6559 <54> DW_AT_import : <0x75>
6560 <2><58>: Abbrev Number: 4 (DW_TAG_typedef)
6561 <59> DW_AT_name : B
6562 <5b> DW_AT_decl_file : 1
6563 <5c> DW_AT_decl_line : 2
6564 <5d> DW_AT_type : <0x6e>
6565 ...
6566 <1><75>: Abbrev Number: 7 (DW_TAG_base_type)
6567 <76> DW_AT_byte_size : 4
6568 <77> DW_AT_encoding : 5 (signed)
6569
6570 imports the wrong die ( 0x75 instead of 0x58 ).
6571 This case will be ignored until the gcc bug is fixed. */
6572 return;
6573 }
6574
82856980
SW
6575 /* Figure out the local name after import. */
6576 import_alias = dwarf2_name (die, cu);
27aa8d6a 6577
794684b6
SW
6578 /* Figure out where the statement is being imported to. */
6579 import_prefix = determine_prefix (die, cu);
6580
6581 /* Figure out what the scope of the imported die is and prepend it
6582 to the name of the imported die. */
de4affc9 6583 imported_name_prefix = determine_prefix (imported_die, imported_cu);
794684b6 6584
f55ee35c
JK
6585 if (imported_die->tag != DW_TAG_namespace
6586 && imported_die->tag != DW_TAG_module)
794684b6 6587 {
13387711
SW
6588 imported_declaration = imported_name;
6589 canonical_name = imported_name_prefix;
794684b6 6590 }
13387711 6591 else if (strlen (imported_name_prefix) > 0)
794684b6 6592 {
13387711
SW
6593 temp = alloca (strlen (imported_name_prefix)
6594 + 2 + strlen (imported_name) + 1);
6595 strcpy (temp, imported_name_prefix);
6596 strcat (temp, "::");
6597 strcat (temp, imported_name);
6598 canonical_name = temp;
794684b6 6599 }
13387711
SW
6600 else
6601 canonical_name = imported_name;
794684b6 6602
32019081
JK
6603 cleanups = make_cleanup (VEC_cleanup (const_char_ptr), &excludes);
6604
6605 if (die->tag == DW_TAG_imported_module && cu->language == language_fortran)
6606 for (child_die = die->child; child_die && child_die->tag;
6607 child_die = sibling_die (child_die))
6608 {
6609 /* DWARF-4: A Fortran use statement with a “rename list” may be
6610 represented by an imported module entry with an import attribute
6611 referring to the module and owned entries corresponding to those
6612 entities that are renamed as part of being imported. */
6613
6614 if (child_die->tag != DW_TAG_imported_declaration)
6615 {
6616 complaint (&symfile_complaints,
6617 _("child DW_TAG_imported_declaration expected "
6618 "- DIE at 0x%x [in module %s]"),
b64f50a1 6619 child_die->offset.sect_off, objfile->name);
32019081
JK
6620 continue;
6621 }
6622
6623 import_attr = dwarf2_attr (child_die, DW_AT_import, cu);
6624 if (import_attr == NULL)
6625 {
6626 complaint (&symfile_complaints, _("Tag '%s' has no DW_AT_import"),
6627 dwarf_tag_name (child_die->tag));
6628 continue;
6629 }
6630
6631 imported_cu = cu;
6632 imported_die = follow_die_ref_or_sig (child_die, import_attr,
6633 &imported_cu);
6634 imported_name = dwarf2_name (imported_die, imported_cu);
6635 if (imported_name == NULL)
6636 {
6637 complaint (&symfile_complaints,
6638 _("child DW_TAG_imported_declaration has unknown "
6639 "imported name - DIE at 0x%x [in module %s]"),
b64f50a1 6640 child_die->offset.sect_off, objfile->name);
32019081
JK
6641 continue;
6642 }
6643
6644 VEC_safe_push (const_char_ptr, excludes, imported_name);
6645
6646 process_die (child_die, cu);
6647 }
6648
c0cc3a76
SW
6649 cp_add_using_directive (import_prefix,
6650 canonical_name,
6651 import_alias,
13387711 6652 imported_declaration,
32019081 6653 excludes,
bb5ed363 6654 &objfile->objfile_obstack);
32019081
JK
6655
6656 do_cleanups (cleanups);
27aa8d6a
SW
6657}
6658
ae2de4f8
DE
6659/* Cleanup function for read_file_scope. */
6660
cb1df416
DJ
6661static void
6662free_cu_line_header (void *arg)
6663{
6664 struct dwarf2_cu *cu = arg;
6665
6666 free_line_header (cu->line_header);
6667 cu->line_header = NULL;
6668}
6669
9291a0cd
TT
6670static void
6671find_file_and_directory (struct die_info *die, struct dwarf2_cu *cu,
6672 char **name, char **comp_dir)
6673{
6674 struct attribute *attr;
6675
6676 *name = NULL;
6677 *comp_dir = NULL;
6678
6679 /* Find the filename. Do not use dwarf2_name here, since the filename
6680 is not a source language identifier. */
6681 attr = dwarf2_attr (die, DW_AT_name, cu);
6682 if (attr)
6683 {
6684 *name = DW_STRING (attr);
6685 }
6686
6687 attr = dwarf2_attr (die, DW_AT_comp_dir, cu);
6688 if (attr)
6689 *comp_dir = DW_STRING (attr);
6690 else if (*name != NULL && IS_ABSOLUTE_PATH (*name))
6691 {
6692 *comp_dir = ldirname (*name);
6693 if (*comp_dir != NULL)
6694 make_cleanup (xfree, *comp_dir);
6695 }
6696 if (*comp_dir != NULL)
6697 {
6698 /* Irix 6.2 native cc prepends <machine>.: to the compilation
6699 directory, get rid of it. */
6700 char *cp = strchr (*comp_dir, ':');
6701
6702 if (cp && cp != *comp_dir && cp[-1] == '.' && cp[1] == '/')
6703 *comp_dir = cp + 1;
6704 }
6705
6706 if (*name == NULL)
6707 *name = "<unknown>";
6708}
6709
f3f5162e
DE
6710/* Handle DW_AT_stmt_list for a compilation unit or type unit.
6711 DIE is the DW_TAG_compile_unit or DW_TAG_type_unit die for CU.
6712 COMP_DIR is the compilation directory.
6713 WANT_LINE_INFO is non-zero if the pc/line-number mapping is needed. */
2ab95328
TT
6714
6715static void
6716handle_DW_AT_stmt_list (struct die_info *die, struct dwarf2_cu *cu,
f3f5162e 6717 const char *comp_dir, int want_line_info)
2ab95328
TT
6718{
6719 struct attribute *attr;
2ab95328 6720
2ab95328
TT
6721 attr = dwarf2_attr (die, DW_AT_stmt_list, cu);
6722 if (attr)
6723 {
6724 unsigned int line_offset = DW_UNSND (attr);
6725 struct line_header *line_header
3019eac3 6726 = dwarf_decode_line_header (line_offset, cu);
2ab95328
TT
6727
6728 if (line_header)
dee91e82
DE
6729 {
6730 cu->line_header = line_header;
6731 make_cleanup (free_cu_line_header, cu);
f3f5162e 6732 dwarf_decode_lines (line_header, comp_dir, cu, NULL, want_line_info);
dee91e82 6733 }
2ab95328
TT
6734 }
6735}
6736
95554aad 6737/* Process DW_TAG_compile_unit or DW_TAG_partial_unit. */
ae2de4f8 6738
c906108c 6739static void
e7c27a73 6740read_file_scope (struct die_info *die, struct dwarf2_cu *cu)
c906108c 6741{
dee91e82 6742 struct objfile *objfile = dwarf2_per_objfile->objfile;
debd256d 6743 struct cleanup *back_to = make_cleanup (null_cleanup, 0);
2acceee2 6744 CORE_ADDR lowpc = ((CORE_ADDR) -1);
c906108c
SS
6745 CORE_ADDR highpc = ((CORE_ADDR) 0);
6746 struct attribute *attr;
e1024ff1 6747 char *name = NULL;
c906108c
SS
6748 char *comp_dir = NULL;
6749 struct die_info *child_die;
6750 bfd *abfd = objfile->obfd;
e142c38c 6751 CORE_ADDR baseaddr;
6e70227d 6752
e142c38c 6753 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
c906108c 6754
fae299cd 6755 get_scope_pc_bounds (die, &lowpc, &highpc, cu);
c906108c
SS
6756
6757 /* If we didn't find a lowpc, set it to highpc to avoid complaints
6758 from finish_block. */
2acceee2 6759 if (lowpc == ((CORE_ADDR) -1))
c906108c
SS
6760 lowpc = highpc;
6761 lowpc += baseaddr;
6762 highpc += baseaddr;
6763
9291a0cd 6764 find_file_and_directory (die, cu, &name, &comp_dir);
e1024ff1 6765
95554aad 6766 prepare_one_comp_unit (cu, die, cu->language);
303b6f5d 6767
f4b8a18d
KW
6768 /* The XLCL doesn't generate DW_LANG_OpenCL because this attribute is not
6769 standardised yet. As a workaround for the language detection we fall
6770 back to the DW_AT_producer string. */
6771 if (cu->producer && strstr (cu->producer, "IBM XL C for OpenCL") != NULL)
6772 cu->language = language_opencl;
6773
3019eac3
DE
6774 /* Similar hack for Go. */
6775 if (cu->producer && strstr (cu->producer, "GNU Go ") != NULL)
6776 set_cu_language (DW_LANG_Go, cu);
6777
6778 /* We assume that we're processing GCC output. */
6779 processing_gcc_compilation = 2;
6780
6781 processing_has_namespace_info = 0;
6782
6783 start_symtab (name, comp_dir, lowpc);
6784 record_debugformat ("DWARF 2");
6785 record_producer (cu->producer);
6786
6787 /* Decode line number information if present. We do this before
6788 processing child DIEs, so that the line header table is available
6789 for DW_AT_decl_file. */
6790 handle_DW_AT_stmt_list (die, cu, comp_dir, 1);
6791
6792 /* Process all dies in compilation unit. */
6793 if (die->child != NULL)
6794 {
6795 child_die = die->child;
6796 while (child_die && child_die->tag)
6797 {
6798 process_die (child_die, cu);
6799 child_die = sibling_die (child_die);
6800 }
6801 }
6802
6803 /* Decode macro information, if present. Dwarf 2 macro information
6804 refers to information in the line number info statement program
6805 header, so we can only read it if we've read the header
6806 successfully. */
6807 attr = dwarf2_attr (die, DW_AT_GNU_macros, cu);
6808 if (attr && cu->line_header)
6809 {
6810 if (dwarf2_attr (die, DW_AT_macro_info, cu))
6811 complaint (&symfile_complaints,
6812 _("CU refers to both DW_AT_GNU_macros and DW_AT_macro_info"));
6813
09262596 6814 dwarf_decode_macros (cu, DW_UNSND (attr), comp_dir, 1);
3019eac3
DE
6815 }
6816 else
6817 {
6818 attr = dwarf2_attr (die, DW_AT_macro_info, cu);
6819 if (attr && cu->line_header)
6820 {
6821 unsigned int macro_offset = DW_UNSND (attr);
6822
09262596 6823 dwarf_decode_macros (cu, macro_offset, comp_dir, 0);
3019eac3
DE
6824 }
6825 }
6826
6827 do_cleanups (back_to);
6828}
6829
6830/* Process DW_TAG_type_unit.
6831 For TUs we want to skip the first top level sibling if it's not the
6832 actual type being defined by this TU. In this case the first top
6833 level sibling is there to provide context only. */
6834
6835static void
6836read_type_unit_scope (struct die_info *die, struct dwarf2_cu *cu)
6837{
6838 struct objfile *objfile = cu->objfile;
6839 struct cleanup *back_to = make_cleanup (null_cleanup, 0);
6840 CORE_ADDR lowpc;
6841 struct attribute *attr;
6842 char *name = NULL;
6843 char *comp_dir = NULL;
6844 struct die_info *child_die;
6845 bfd *abfd = objfile->obfd;
6846
6847 /* start_symtab needs a low pc, but we don't really have one.
6848 Do what read_file_scope would do in the absence of such info. */
6849 lowpc = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
6850
6851 /* Find the filename. Do not use dwarf2_name here, since the filename
6852 is not a source language identifier. */
6853 attr = dwarf2_attr (die, DW_AT_name, cu);
6854 if (attr)
6855 name = DW_STRING (attr);
6856
6857 attr = dwarf2_attr (die, DW_AT_comp_dir, cu);
6858 if (attr)
6859 comp_dir = DW_STRING (attr);
6860 else if (name != NULL && IS_ABSOLUTE_PATH (name))
6861 {
6862 comp_dir = ldirname (name);
6863 if (comp_dir != NULL)
6864 make_cleanup (xfree, comp_dir);
6865 }
6866
6867 if (name == NULL)
6868 name = "<unknown>";
6869
95554aad 6870 prepare_one_comp_unit (cu, die, language_minimal);
3019eac3
DE
6871
6872 /* We assume that we're processing GCC output. */
6873 processing_gcc_compilation = 2;
6874
6875 processing_has_namespace_info = 0;
6876
6877 start_symtab (name, comp_dir, lowpc);
6878 record_debugformat ("DWARF 2");
6879 record_producer (cu->producer);
6880
6881 /* Decode line number information if present. We do this before
6882 processing child DIEs, so that the line header table is available
6883 for DW_AT_decl_file.
6884 We don't need the pc/line-number mapping for type units. */
6885 handle_DW_AT_stmt_list (die, cu, comp_dir, 0);
6886
6887 /* Process the dies in the type unit. */
6888 if (die->child == NULL)
6889 {
6890 dump_die_for_error (die);
6891 error (_("Dwarf Error: Missing children for type unit [in module %s]"),
6892 bfd_get_filename (abfd));
6893 }
6894
6895 child_die = die->child;
6896
6897 while (child_die && child_die->tag)
6898 {
6899 process_die (child_die, cu);
6900
6901 child_die = sibling_die (child_die);
6902 }
6903
6904 do_cleanups (back_to);
6905}
6906\f
6907/* DWO files. */
6908
6909static hashval_t
6910hash_dwo_file (const void *item)
6911{
6912 const struct dwo_file *dwo_file = item;
6913
6914 return htab_hash_string (dwo_file->dwo_name);
6915}
6916
6917static int
6918eq_dwo_file (const void *item_lhs, const void *item_rhs)
6919{
6920 const struct dwo_file *lhs = item_lhs;
6921 const struct dwo_file *rhs = item_rhs;
6922
6923 return strcmp (lhs->dwo_name, rhs->dwo_name) == 0;
6924}
6925
6926/* Allocate a hash table for DWO files. */
6927
6928static htab_t
6929allocate_dwo_file_hash_table (void)
6930{
6931 struct objfile *objfile = dwarf2_per_objfile->objfile;
6932
6933 return htab_create_alloc_ex (41,
6934 hash_dwo_file,
6935 eq_dwo_file,
6936 NULL,
6937 &objfile->objfile_obstack,
6938 hashtab_obstack_allocate,
6939 dummy_obstack_deallocate);
6940}
6941
6942static hashval_t
6943hash_dwo_unit (const void *item)
6944{
6945 const struct dwo_unit *dwo_unit = item;
6946
6947 /* This drops the top 32 bits of the id, but is ok for a hash. */
6948 return dwo_unit->signature;
6949}
6950
6951static int
6952eq_dwo_unit (const void *item_lhs, const void *item_rhs)
6953{
6954 const struct dwo_unit *lhs = item_lhs;
6955 const struct dwo_unit *rhs = item_rhs;
6956
6957 /* The signature is assumed to be unique within the DWO file.
6958 So while object file CU dwo_id's always have the value zero,
6959 that's OK, assuming each object file DWO file has only one CU,
6960 and that's the rule for now. */
6961 return lhs->signature == rhs->signature;
6962}
6963
6964/* Allocate a hash table for DWO CUs,TUs.
6965 There is one of these tables for each of CUs,TUs for each DWO file. */
6966
6967static htab_t
6968allocate_dwo_unit_table (struct objfile *objfile)
6969{
6970 /* Start out with a pretty small number.
6971 Generally DWO files contain only one CU and maybe some TUs. */
6972 return htab_create_alloc_ex (3,
6973 hash_dwo_unit,
6974 eq_dwo_unit,
6975 NULL,
6976 &objfile->objfile_obstack,
6977 hashtab_obstack_allocate,
6978 dummy_obstack_deallocate);
6979}
6980
6981/* This function is mapped across the sections and remembers the offset and
6982 size of each of the DWO debugging sections we are interested in. */
6983
6984static void
6985dwarf2_locate_dwo_sections (bfd *abfd, asection *sectp, void *dwo_file_ptr)
6986{
6987 struct dwo_file *dwo_file = dwo_file_ptr;
6988 const struct dwo_section_names *names = &dwo_section_names;
6989
6990 if (section_is_p (sectp->name, &names->abbrev_dwo))
6991 {
6992 dwo_file->sections.abbrev.asection = sectp;
6993 dwo_file->sections.abbrev.size = bfd_get_section_size (sectp);
6994 }
6995 else if (section_is_p (sectp->name, &names->info_dwo))
6996 {
6997 dwo_file->sections.info.asection = sectp;
6998 dwo_file->sections.info.size = bfd_get_section_size (sectp);
6999 }
7000 else if (section_is_p (sectp->name, &names->line_dwo))
7001 {
7002 dwo_file->sections.line.asection = sectp;
7003 dwo_file->sections.line.size = bfd_get_section_size (sectp);
7004 }
7005 else if (section_is_p (sectp->name, &names->loc_dwo))
7006 {
7007 dwo_file->sections.loc.asection = sectp;
7008 dwo_file->sections.loc.size = bfd_get_section_size (sectp);
7009 }
09262596
DE
7010 else if (section_is_p (sectp->name, &names->macinfo_dwo))
7011 {
7012 dwo_file->sections.macinfo.asection = sectp;
7013 dwo_file->sections.macinfo.size = bfd_get_section_size (sectp);
7014 }
7015 else if (section_is_p (sectp->name, &names->macro_dwo))
7016 {
7017 dwo_file->sections.macro.asection = sectp;
7018 dwo_file->sections.macro.size = bfd_get_section_size (sectp);
7019 }
3019eac3
DE
7020 else if (section_is_p (sectp->name, &names->str_dwo))
7021 {
7022 dwo_file->sections.str.asection = sectp;
7023 dwo_file->sections.str.size = bfd_get_section_size (sectp);
7024 }
7025 else if (section_is_p (sectp->name, &names->str_offsets_dwo))
7026 {
7027 dwo_file->sections.str_offsets.asection = sectp;
7028 dwo_file->sections.str_offsets.size = bfd_get_section_size (sectp);
7029 }
7030 else if (section_is_p (sectp->name, &names->types_dwo))
7031 {
7032 struct dwarf2_section_info type_section;
7033
7034 memset (&type_section, 0, sizeof (type_section));
7035 type_section.asection = sectp;
7036 type_section.size = bfd_get_section_size (sectp);
7037 VEC_safe_push (dwarf2_section_info_def, dwo_file->sections.types,
7038 &type_section);
7039 }
7040}
7041
7042/* Structure used to pass data to create_debug_info_hash_table_reader. */
7043
7044struct create_dwo_info_table_data
7045{
7046 struct dwo_file *dwo_file;
7047 htab_t cu_htab;
7048};
7049
7050/* die_reader_func for create_debug_info_hash_table. */
7051
7052static void
7053create_debug_info_hash_table_reader (const struct die_reader_specs *reader,
7054 gdb_byte *info_ptr,
7055 struct die_info *comp_unit_die,
7056 int has_children,
7057 void *datap)
7058{
7059 struct dwarf2_cu *cu = reader->cu;
7060 struct objfile *objfile = dwarf2_per_objfile->objfile;
7061 sect_offset offset = cu->per_cu->offset;
7062 struct dwarf2_section_info *section = cu->per_cu->info_or_types_section;
7063 struct create_dwo_info_table_data *data = datap;
7064 struct dwo_file *dwo_file = data->dwo_file;
7065 htab_t cu_htab = data->cu_htab;
7066 void **slot;
7067 struct attribute *attr;
7068 struct dwo_unit *dwo_unit;
7069
7070 attr = dwarf2_attr (comp_unit_die, DW_AT_GNU_dwo_id, cu);
7071 if (attr == NULL)
7072 {
7073 error (_("Dwarf Error: debug entry at offset 0x%x is missing"
7074 " its dwo_id [in module %s]"),
7075 offset.sect_off, dwo_file->dwo_name);
7076 return;
7077 }
7078
7079 dwo_unit = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct dwo_unit);
7080 dwo_unit->dwo_file = dwo_file;
7081 dwo_unit->signature = DW_UNSND (attr);
7082 dwo_unit->info_or_types_section = section;
7083 dwo_unit->offset = offset;
7084 dwo_unit->length = cu->per_cu->length;
7085
7086 slot = htab_find_slot (cu_htab, dwo_unit, INSERT);
7087 gdb_assert (slot != NULL);
7088 if (*slot != NULL)
7089 {
7090 const struct dwo_unit *dup_dwo_unit = *slot;
7091
7092 complaint (&symfile_complaints,
7093 _("debug entry at offset 0x%x is duplicate to the entry at"
7094 " offset 0x%x, dwo_id 0x%s [in module %s]"),
7095 offset.sect_off, dup_dwo_unit->offset.sect_off,
7096 phex (dwo_unit->signature, sizeof (dwo_unit->signature)),
7097 dwo_file->dwo_name);
7098 }
7099 else
7100 *slot = dwo_unit;
7101
7102 if (dwarf2_die_debug)
7103 fprintf_unfiltered (gdb_stdlog, " offset 0x%x, dwo_id 0x%s\n",
7104 offset.sect_off,
7105 phex (dwo_unit->signature,
7106 sizeof (dwo_unit->signature)));
7107}
7108
7109/* Create a hash table to map DWO IDs to their CU entry in .debug_info.dwo. */
7110
7111static htab_t
7112create_debug_info_hash_table (struct dwo_file *dwo_file)
7113{
7114 struct objfile *objfile = dwarf2_per_objfile->objfile;
7115 struct dwarf2_section_info *section = &dwo_file->sections.info;
7116 bfd *abfd;
7117 htab_t cu_htab;
7118 gdb_byte *info_ptr, *end_ptr;
7119 struct create_dwo_info_table_data create_dwo_info_table_data;
7120
7121 dwarf2_read_section (objfile, section);
7122 info_ptr = section->buffer;
7123
7124 if (info_ptr == NULL)
7125 return NULL;
7126
7127 /* We can't set abfd until now because the section may be empty or
7128 not present, in which case section->asection will be NULL. */
7129 abfd = section->asection->owner;
7130
7131 if (dwarf2_die_debug)
7132 fprintf_unfiltered (gdb_stdlog, "Reading .debug_info.dwo for %s:\n",
7133 bfd_get_filename (abfd));
7134
7135 cu_htab = allocate_dwo_unit_table (objfile);
7136
7137 create_dwo_info_table_data.dwo_file = dwo_file;
7138 create_dwo_info_table_data.cu_htab = cu_htab;
7139
7140 end_ptr = info_ptr + section->size;
7141 while (info_ptr < end_ptr)
7142 {
7143 struct dwarf2_per_cu_data per_cu;
7144
7145 memset (&per_cu, 0, sizeof (per_cu));
7146 per_cu.objfile = objfile;
7147 per_cu.is_debug_types = 0;
7148 per_cu.offset.sect_off = info_ptr - section->buffer;
7149 per_cu.info_or_types_section = section;
7150
7151 init_cutu_and_read_dies_no_follow (&per_cu,
7152 &dwo_file->sections.abbrev,
7153 dwo_file,
7154 create_debug_info_hash_table_reader,
7155 &create_dwo_info_table_data);
7156
7157 info_ptr += per_cu.length;
7158 }
7159
7160 return cu_htab;
7161}
7162
7163/* Subroutine of open_dwo_file to simplify it.
7164 Open the file specified by FILE_NAME and hand it off to BFD for
7165 preliminary analysis. Return a newly initialized bfd *, which
7166 includes a canonicalized copy of FILE_NAME.
7167 In case of trouble, return NULL.
7168 NOTE: This function is derived from symfile_bfd_open. */
7169
7170static bfd *
7171try_open_dwo_file (const char *file_name)
7172{
7173 bfd *sym_bfd;
7174 int desc;
7175 char *absolute_name;
3019eac3
DE
7176
7177 desc = openp (debug_file_directory, OPF_TRY_CWD_FIRST, file_name,
7178 O_RDONLY | O_BINARY, &absolute_name);
7179 if (desc < 0)
7180 return NULL;
7181
7182 sym_bfd = bfd_fopen (absolute_name, gnutarget, FOPEN_RB, desc);
7183 if (!sym_bfd)
7184 {
3019eac3
DE
7185 xfree (absolute_name);
7186 return NULL;
7187 }
7188 bfd_set_cacheable (sym_bfd, 1);
7189
7190 if (!bfd_check_format (sym_bfd, bfd_object))
7191 {
7192 bfd_close (sym_bfd); /* This also closes desc. */
7193 xfree (absolute_name);
7194 return NULL;
7195 }
7196
7197 /* bfd_usrdata exists for applications and libbfd must not touch it. */
7198 gdb_assert (bfd_usrdata (sym_bfd) == NULL);
7199
7200 return sym_bfd;
7201}
7202
7203/* Try to open DWO file DWO_NAME.
7204 COMP_DIR is the DW_AT_comp_dir attribute.
7205 The result is the bfd handle of the file.
7206 If there is a problem finding or opening the file, return NULL.
7207 Upon success, the canonicalized path of the file is stored in the bfd,
7208 same as symfile_bfd_open. */
7209
7210static bfd *
7211open_dwo_file (const char *dwo_name, const char *comp_dir)
7212{
7213 bfd *abfd;
3019eac3
DE
7214
7215 if (IS_ABSOLUTE_PATH (dwo_name))
7216 return try_open_dwo_file (dwo_name);
7217
7218 /* Before trying the search path, try DWO_NAME in COMP_DIR. */
7219
7220 if (comp_dir != NULL)
7221 {
7222 char *path_to_try = concat (comp_dir, SLASH_STRING, dwo_name, NULL);
7223
7224 /* NOTE: If comp_dir is a relative path, this will also try the
7225 search path, which seems useful. */
7226 abfd = try_open_dwo_file (path_to_try);
7227 xfree (path_to_try);
7228 if (abfd != NULL)
7229 return abfd;
7230 }
7231
7232 /* That didn't work, try debug-file-directory, which, despite its name,
7233 is a list of paths. */
7234
7235 if (*debug_file_directory == '\0')
7236 return NULL;
7237
7238 return try_open_dwo_file (dwo_name);
7239}
7240
7241/* Initialize the use of the DWO file specified by DWO_NAME. */
7242
7243static struct dwo_file *
7244init_dwo_file (const char *dwo_name, const char *comp_dir)
7245{
7246 struct objfile *objfile = dwarf2_per_objfile->objfile;
7247 struct dwo_file *dwo_file = OBSTACK_ZALLOC (&objfile->objfile_obstack,
7248 struct dwo_file);
7249 bfd *abfd;
7250 struct cleanup *cleanups;
7251
7252 if (dwarf2_die_debug)
7253 fprintf_unfiltered (gdb_stdlog, "Reading DWO file %s:\n", dwo_name);
7254
7255 abfd = open_dwo_file (dwo_name, comp_dir);
7256 if (abfd == NULL)
7257 return NULL;
7258 dwo_file->dwo_name = dwo_name;
7259 dwo_file->dwo_bfd = abfd;
7260
7261 cleanups = make_cleanup (free_dwo_file_cleanup, dwo_file);
7262
7263 bfd_map_over_sections (abfd, dwarf2_locate_dwo_sections, dwo_file);
7264
7265 dwo_file->cus = create_debug_info_hash_table (dwo_file);
7266
7267 dwo_file->tus = create_debug_types_hash_table (dwo_file,
7268 dwo_file->sections.types);
7269
7270 discard_cleanups (cleanups);
7271
7272 return dwo_file;
7273}
7274
7275/* Lookup DWO file DWO_NAME. */
7276
7277static struct dwo_file *
7278lookup_dwo_file (char *dwo_name, const char *comp_dir)
7279{
7280 struct dwo_file *dwo_file;
7281 struct dwo_file find_entry;
7282 void **slot;
7283
7284 if (dwarf2_per_objfile->dwo_files == NULL)
7285 dwarf2_per_objfile->dwo_files = allocate_dwo_file_hash_table ();
7286
7287 /* Have we already seen this DWO file? */
7288 find_entry.dwo_name = dwo_name;
7289 slot = htab_find_slot (dwarf2_per_objfile->dwo_files, &find_entry, INSERT);
7290
7291 /* If not, read it in and build a table of the DWOs it contains. */
7292 if (*slot == NULL)
7293 *slot = init_dwo_file (dwo_name, comp_dir);
7294
7295 /* NOTE: This will be NULL if unable to open the file. */
7296 dwo_file = *slot;
7297
7298 return dwo_file;
7299}
7300
7301/* Lookup the DWO CU referenced from THIS_CU in DWO file DWO_NAME.
7302 If non-NULL, comp_dir is the DW_AT_comp_dir attribute.
7303 SIGNATURE is the "dwo_id" of the CU (for consistency we use the same
7304 nomenclature as TUs).
1c658ad5 7305 The result is a pointer to the dwo_unit object or NULL if we didn't find it
3019eac3
DE
7306 (dwo_id mismatch or couldn't find the DWO file). */
7307
7308static struct dwo_unit *
7309lookup_dwo_comp_unit (struct dwarf2_per_cu_data *this_cu,
7310 char *dwo_name, const char *comp_dir,
7311 ULONGEST signature)
7312{
7313 struct objfile *objfile = dwarf2_per_objfile->objfile;
7314 struct dwo_file *dwo_file;
7315
7316 dwo_file = lookup_dwo_file (dwo_name, comp_dir);
7317 if (dwo_file == NULL)
7318 return NULL;
7319
7320 /* Look up the DWO using its signature(dwo_id). */
7321
7322 if (dwo_file->cus != NULL)
7323 {
7324 struct dwo_unit find_dwo_cu, *dwo_cu;
7325
7326 find_dwo_cu.signature = signature;
7327 dwo_cu = htab_find (dwo_file->cus, &find_dwo_cu);
a766d390 7328
3019eac3
DE
7329 if (dwo_cu != NULL)
7330 return dwo_cu;
7331 }
c906108c 7332
3019eac3 7333 /* We didn't find it. This must mean a dwo_id mismatch. */
df8a16a1 7334
3019eac3
DE
7335 complaint (&symfile_complaints,
7336 _("Could not find DWO CU referenced by CU at offset 0x%x"
7337 " [in module %s]"),
7338 this_cu->offset.sect_off, objfile->name);
7339 return NULL;
7340}
c906108c 7341
3019eac3
DE
7342/* Lookup the DWO TU referenced from THIS_TU in DWO file DWO_NAME.
7343 If non-NULL, comp_dir is the DW_AT_comp_dir attribute.
1c658ad5 7344 The result is a pointer to the dwo_unit object or NULL if we didn't find it
3019eac3 7345 (dwo_id mismatch or couldn't find the DWO file). */
debd256d 7346
3019eac3
DE
7347static struct dwo_unit *
7348lookup_dwo_type_unit (struct signatured_type *this_tu,
7349 char *dwo_name, const char *comp_dir)
7350{
7351 struct objfile *objfile = dwarf2_per_objfile->objfile;
7352 struct dwo_file *dwo_file;
cb1df416 7353
3019eac3
DE
7354 dwo_file = lookup_dwo_file (dwo_name, comp_dir);
7355 if (dwo_file == NULL)
7356 return NULL;
cf2c3c16 7357
3019eac3
DE
7358 /* Look up the DWO using its signature(dwo_id). */
7359
7360 if (dwo_file->tus != NULL)
cf2c3c16 7361 {
3019eac3 7362 struct dwo_unit find_dwo_tu, *dwo_tu;
9a619af0 7363
3019eac3
DE
7364 find_dwo_tu.signature = this_tu->signature;
7365 dwo_tu = htab_find (dwo_file->tus, &find_dwo_tu);
7366
7367 if (dwo_tu != NULL)
7368 return dwo_tu;
2e276125 7369 }
9cdd5dbd 7370
3019eac3
DE
7371 /* We didn't find it. This must mean a dwo_id mismatch. */
7372
7373 complaint (&symfile_complaints,
7374 _("Could not find DWO TU referenced by TU at offset 0x%x"
7375 " [in module %s]"),
7376 this_tu->per_cu.offset.sect_off, objfile->name);
7377 return NULL;
5fb290d7
DJ
7378}
7379
3019eac3
DE
7380/* Free all resources associated with DWO_FILE.
7381 Close the DWO file and munmap the sections.
7382 All memory should be on the objfile obstack. */
348e048f
DE
7383
7384static void
3019eac3 7385free_dwo_file (struct dwo_file *dwo_file, struct objfile *objfile)
348e048f 7386{
3019eac3
DE
7387 int ix;
7388 struct dwarf2_section_info *section;
348e048f 7389
3019eac3
DE
7390 gdb_assert (dwo_file->dwo_bfd != objfile->obfd);
7391 bfd_close (dwo_file->dwo_bfd);
348e048f 7392
3019eac3
DE
7393 munmap_section_buffer (&dwo_file->sections.abbrev);
7394 munmap_section_buffer (&dwo_file->sections.info);
7395 munmap_section_buffer (&dwo_file->sections.line);
7396 munmap_section_buffer (&dwo_file->sections.loc);
7397 munmap_section_buffer (&dwo_file->sections.str);
7398 munmap_section_buffer (&dwo_file->sections.str_offsets);
348e048f 7399
3019eac3
DE
7400 for (ix = 0;
7401 VEC_iterate (dwarf2_section_info_def, dwo_file->sections.types,
7402 ix, section);
7403 ++ix)
7404 munmap_section_buffer (section);
348e048f 7405
3019eac3
DE
7406 VEC_free (dwarf2_section_info_def, dwo_file->sections.types);
7407}
348e048f 7408
3019eac3 7409/* Wrapper for free_dwo_file for use in cleanups. */
348e048f 7410
3019eac3
DE
7411static void
7412free_dwo_file_cleanup (void *arg)
7413{
7414 struct dwo_file *dwo_file = (struct dwo_file *) arg;
7415 struct objfile *objfile = dwarf2_per_objfile->objfile;
348e048f 7416
3019eac3
DE
7417 free_dwo_file (dwo_file, objfile);
7418}
348e048f 7419
3019eac3 7420/* Traversal function for free_dwo_files. */
2ab95328 7421
3019eac3
DE
7422static int
7423free_dwo_file_from_slot (void **slot, void *info)
7424{
7425 struct dwo_file *dwo_file = (struct dwo_file *) *slot;
7426 struct objfile *objfile = (struct objfile *) info;
348e048f 7427
3019eac3 7428 free_dwo_file (dwo_file, objfile);
348e048f 7429
3019eac3
DE
7430 return 1;
7431}
348e048f 7432
3019eac3 7433/* Free all resources associated with DWO_FILES. */
348e048f 7434
3019eac3
DE
7435static void
7436free_dwo_files (htab_t dwo_files, struct objfile *objfile)
7437{
7438 htab_traverse_noresize (dwo_files, free_dwo_file_from_slot, objfile);
348e048f 7439}
3019eac3
DE
7440\f
7441/* Read in various DIEs. */
348e048f 7442
d389af10
JK
7443/* qsort helper for inherit_abstract_dies. */
7444
7445static int
7446unsigned_int_compar (const void *ap, const void *bp)
7447{
7448 unsigned int a = *(unsigned int *) ap;
7449 unsigned int b = *(unsigned int *) bp;
7450
7451 return (a > b) - (b > a);
7452}
7453
7454/* DW_AT_abstract_origin inherits whole DIEs (not just their attributes).
3e43a32a
MS
7455 Inherit only the children of the DW_AT_abstract_origin DIE not being
7456 already referenced by DW_AT_abstract_origin from the children of the
7457 current DIE. */
d389af10
JK
7458
7459static void
7460inherit_abstract_dies (struct die_info *die, struct dwarf2_cu *cu)
7461{
7462 struct die_info *child_die;
7463 unsigned die_children_count;
7464 /* CU offsets which were referenced by children of the current DIE. */
b64f50a1
JK
7465 sect_offset *offsets;
7466 sect_offset *offsets_end, *offsetp;
d389af10
JK
7467 /* Parent of DIE - referenced by DW_AT_abstract_origin. */
7468 struct die_info *origin_die;
7469 /* Iterator of the ORIGIN_DIE children. */
7470 struct die_info *origin_child_die;
7471 struct cleanup *cleanups;
7472 struct attribute *attr;
cd02d79d
PA
7473 struct dwarf2_cu *origin_cu;
7474 struct pending **origin_previous_list_in_scope;
d389af10
JK
7475
7476 attr = dwarf2_attr (die, DW_AT_abstract_origin, cu);
7477 if (!attr)
7478 return;
7479
cd02d79d
PA
7480 /* Note that following die references may follow to a die in a
7481 different cu. */
7482
7483 origin_cu = cu;
7484 origin_die = follow_die_ref (die, attr, &origin_cu);
7485
7486 /* We're inheriting ORIGIN's children into the scope we'd put DIE's
7487 symbols in. */
7488 origin_previous_list_in_scope = origin_cu->list_in_scope;
7489 origin_cu->list_in_scope = cu->list_in_scope;
7490
edb3359d
DJ
7491 if (die->tag != origin_die->tag
7492 && !(die->tag == DW_TAG_inlined_subroutine
7493 && origin_die->tag == DW_TAG_subprogram))
d389af10
JK
7494 complaint (&symfile_complaints,
7495 _("DIE 0x%x and its abstract origin 0x%x have different tags"),
b64f50a1 7496 die->offset.sect_off, origin_die->offset.sect_off);
d389af10
JK
7497
7498 child_die = die->child;
7499 die_children_count = 0;
7500 while (child_die && child_die->tag)
7501 {
7502 child_die = sibling_die (child_die);
7503 die_children_count++;
7504 }
7505 offsets = xmalloc (sizeof (*offsets) * die_children_count);
7506 cleanups = make_cleanup (xfree, offsets);
7507
7508 offsets_end = offsets;
7509 child_die = die->child;
7510 while (child_die && child_die->tag)
7511 {
c38f313d
DJ
7512 /* For each CHILD_DIE, find the corresponding child of
7513 ORIGIN_DIE. If there is more than one layer of
7514 DW_AT_abstract_origin, follow them all; there shouldn't be,
7515 but GCC versions at least through 4.4 generate this (GCC PR
7516 40573). */
7517 struct die_info *child_origin_die = child_die;
cd02d79d 7518 struct dwarf2_cu *child_origin_cu = cu;
9a619af0 7519
c38f313d
DJ
7520 while (1)
7521 {
cd02d79d
PA
7522 attr = dwarf2_attr (child_origin_die, DW_AT_abstract_origin,
7523 child_origin_cu);
c38f313d
DJ
7524 if (attr == NULL)
7525 break;
cd02d79d
PA
7526 child_origin_die = follow_die_ref (child_origin_die, attr,
7527 &child_origin_cu);
c38f313d
DJ
7528 }
7529
d389af10
JK
7530 /* According to DWARF3 3.3.8.2 #3 new entries without their abstract
7531 counterpart may exist. */
c38f313d 7532 if (child_origin_die != child_die)
d389af10 7533 {
edb3359d
DJ
7534 if (child_die->tag != child_origin_die->tag
7535 && !(child_die->tag == DW_TAG_inlined_subroutine
7536 && child_origin_die->tag == DW_TAG_subprogram))
d389af10
JK
7537 complaint (&symfile_complaints,
7538 _("Child DIE 0x%x and its abstract origin 0x%x have "
b64f50a1
JK
7539 "different tags"), child_die->offset.sect_off,
7540 child_origin_die->offset.sect_off);
c38f313d
DJ
7541 if (child_origin_die->parent != origin_die)
7542 complaint (&symfile_complaints,
7543 _("Child DIE 0x%x and its abstract origin 0x%x have "
b64f50a1
JK
7544 "different parents"), child_die->offset.sect_off,
7545 child_origin_die->offset.sect_off);
c38f313d
DJ
7546 else
7547 *offsets_end++ = child_origin_die->offset;
d389af10
JK
7548 }
7549 child_die = sibling_die (child_die);
7550 }
7551 qsort (offsets, offsets_end - offsets, sizeof (*offsets),
7552 unsigned_int_compar);
7553 for (offsetp = offsets + 1; offsetp < offsets_end; offsetp++)
b64f50a1 7554 if (offsetp[-1].sect_off == offsetp->sect_off)
3e43a32a
MS
7555 complaint (&symfile_complaints,
7556 _("Multiple children of DIE 0x%x refer "
7557 "to DIE 0x%x as their abstract origin"),
b64f50a1 7558 die->offset.sect_off, offsetp->sect_off);
d389af10
JK
7559
7560 offsetp = offsets;
7561 origin_child_die = origin_die->child;
7562 while (origin_child_die && origin_child_die->tag)
7563 {
7564 /* Is ORIGIN_CHILD_DIE referenced by any of the DIE children? */
b64f50a1
JK
7565 while (offsetp < offsets_end
7566 && offsetp->sect_off < origin_child_die->offset.sect_off)
d389af10 7567 offsetp++;
b64f50a1
JK
7568 if (offsetp >= offsets_end
7569 || offsetp->sect_off > origin_child_die->offset.sect_off)
d389af10
JK
7570 {
7571 /* Found that ORIGIN_CHILD_DIE is really not referenced. */
cd02d79d 7572 process_die (origin_child_die, origin_cu);
d389af10
JK
7573 }
7574 origin_child_die = sibling_die (origin_child_die);
7575 }
cd02d79d 7576 origin_cu->list_in_scope = origin_previous_list_in_scope;
d389af10
JK
7577
7578 do_cleanups (cleanups);
7579}
7580
c906108c 7581static void
e7c27a73 7582read_func_scope (struct die_info *die, struct dwarf2_cu *cu)
c906108c 7583{
e7c27a73 7584 struct objfile *objfile = cu->objfile;
52f0bd74 7585 struct context_stack *new;
c906108c
SS
7586 CORE_ADDR lowpc;
7587 CORE_ADDR highpc;
7588 struct die_info *child_die;
edb3359d 7589 struct attribute *attr, *call_line, *call_file;
c906108c 7590 char *name;
e142c38c 7591 CORE_ADDR baseaddr;
801e3a5b 7592 struct block *block;
edb3359d 7593 int inlined_func = (die->tag == DW_TAG_inlined_subroutine);
34eaf542
TT
7594 VEC (symbolp) *template_args = NULL;
7595 struct template_symbol *templ_func = NULL;
edb3359d
DJ
7596
7597 if (inlined_func)
7598 {
7599 /* If we do not have call site information, we can't show the
7600 caller of this inlined function. That's too confusing, so
7601 only use the scope for local variables. */
7602 call_line = dwarf2_attr (die, DW_AT_call_line, cu);
7603 call_file = dwarf2_attr (die, DW_AT_call_file, cu);
7604 if (call_line == NULL || call_file == NULL)
7605 {
7606 read_lexical_block_scope (die, cu);
7607 return;
7608 }
7609 }
c906108c 7610
e142c38c
DJ
7611 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
7612
94af9270 7613 name = dwarf2_name (die, cu);
c906108c 7614
e8d05480
JB
7615 /* Ignore functions with missing or empty names. These are actually
7616 illegal according to the DWARF standard. */
7617 if (name == NULL)
7618 {
7619 complaint (&symfile_complaints,
b64f50a1
JK
7620 _("missing name for subprogram DIE at %d"),
7621 die->offset.sect_off);
e8d05480
JB
7622 return;
7623 }
7624
7625 /* Ignore functions with missing or invalid low and high pc attributes. */
7626 if (!dwarf2_get_pc_bounds (die, &lowpc, &highpc, cu, NULL))
7627 {
ae4d0c03
PM
7628 attr = dwarf2_attr (die, DW_AT_external, cu);
7629 if (!attr || !DW_UNSND (attr))
7630 complaint (&symfile_complaints,
3e43a32a
MS
7631 _("cannot get low and high bounds "
7632 "for subprogram DIE at %d"),
b64f50a1 7633 die->offset.sect_off);
e8d05480
JB
7634 return;
7635 }
c906108c
SS
7636
7637 lowpc += baseaddr;
7638 highpc += baseaddr;
7639
34eaf542
TT
7640 /* If we have any template arguments, then we must allocate a
7641 different sort of symbol. */
7642 for (child_die = die->child; child_die; child_die = sibling_die (child_die))
7643 {
7644 if (child_die->tag == DW_TAG_template_type_param
7645 || child_die->tag == DW_TAG_template_value_param)
7646 {
7647 templ_func = OBSTACK_ZALLOC (&objfile->objfile_obstack,
7648 struct template_symbol);
7649 templ_func->base.is_cplus_template_function = 1;
7650 break;
7651 }
7652 }
7653
c906108c 7654 new = push_context (0, lowpc);
34eaf542
TT
7655 new->name = new_symbol_full (die, read_type_die (die, cu), cu,
7656 (struct symbol *) templ_func);
4c2df51b 7657
4cecd739
DJ
7658 /* If there is a location expression for DW_AT_frame_base, record
7659 it. */
e142c38c 7660 attr = dwarf2_attr (die, DW_AT_frame_base, cu);
4c2df51b 7661 if (attr)
c034e007
AC
7662 /* FIXME: cagney/2004-01-26: The DW_AT_frame_base's location
7663 expression is being recorded directly in the function's symbol
7664 and not in a separate frame-base object. I guess this hack is
7665 to avoid adding some sort of frame-base adjunct/annex to the
7666 function's symbol :-(. The problem with doing this is that it
7667 results in a function symbol with a location expression that
7668 has nothing to do with the location of the function, ouch! The
7669 relationship should be: a function's symbol has-a frame base; a
7670 frame-base has-a location expression. */
e7c27a73 7671 dwarf2_symbol_mark_computed (attr, new->name, cu);
4c2df51b 7672
e142c38c 7673 cu->list_in_scope = &local_symbols;
c906108c 7674
639d11d3 7675 if (die->child != NULL)
c906108c 7676 {
639d11d3 7677 child_die = die->child;
c906108c
SS
7678 while (child_die && child_die->tag)
7679 {
34eaf542
TT
7680 if (child_die->tag == DW_TAG_template_type_param
7681 || child_die->tag == DW_TAG_template_value_param)
7682 {
7683 struct symbol *arg = new_symbol (child_die, NULL, cu);
7684
f1078f66
DJ
7685 if (arg != NULL)
7686 VEC_safe_push (symbolp, template_args, arg);
34eaf542
TT
7687 }
7688 else
7689 process_die (child_die, cu);
c906108c
SS
7690 child_die = sibling_die (child_die);
7691 }
7692 }
7693
d389af10
JK
7694 inherit_abstract_dies (die, cu);
7695
4a811a97
UW
7696 /* If we have a DW_AT_specification, we might need to import using
7697 directives from the context of the specification DIE. See the
7698 comment in determine_prefix. */
7699 if (cu->language == language_cplus
7700 && dwarf2_attr (die, DW_AT_specification, cu))
7701 {
7702 struct dwarf2_cu *spec_cu = cu;
7703 struct die_info *spec_die = die_specification (die, &spec_cu);
7704
7705 while (spec_die)
7706 {
7707 child_die = spec_die->child;
7708 while (child_die && child_die->tag)
7709 {
7710 if (child_die->tag == DW_TAG_imported_module)
7711 process_die (child_die, spec_cu);
7712 child_die = sibling_die (child_die);
7713 }
7714
7715 /* In some cases, GCC generates specification DIEs that
7716 themselves contain DW_AT_specification attributes. */
7717 spec_die = die_specification (spec_die, &spec_cu);
7718 }
7719 }
7720
c906108c
SS
7721 new = pop_context ();
7722 /* Make a block for the local symbols within. */
801e3a5b
JB
7723 block = finish_block (new->name, &local_symbols, new->old_blocks,
7724 lowpc, highpc, objfile);
7725
df8a16a1 7726 /* For C++, set the block's scope. */
f55ee35c 7727 if (cu->language == language_cplus || cu->language == language_fortran)
df8a16a1 7728 cp_set_block_scope (new->name, block, &objfile->objfile_obstack,
0114d602 7729 determine_prefix (die, cu),
df8a16a1
DJ
7730 processing_has_namespace_info);
7731
801e3a5b
JB
7732 /* If we have address ranges, record them. */
7733 dwarf2_record_block_ranges (die, block, baseaddr, cu);
6e70227d 7734
34eaf542
TT
7735 /* Attach template arguments to function. */
7736 if (! VEC_empty (symbolp, template_args))
7737 {
7738 gdb_assert (templ_func != NULL);
7739
7740 templ_func->n_template_arguments = VEC_length (symbolp, template_args);
7741 templ_func->template_arguments
7742 = obstack_alloc (&objfile->objfile_obstack,
7743 (templ_func->n_template_arguments
7744 * sizeof (struct symbol *)));
7745 memcpy (templ_func->template_arguments,
7746 VEC_address (symbolp, template_args),
7747 (templ_func->n_template_arguments * sizeof (struct symbol *)));
7748 VEC_free (symbolp, template_args);
7749 }
7750
208d8187
JB
7751 /* In C++, we can have functions nested inside functions (e.g., when
7752 a function declares a class that has methods). This means that
7753 when we finish processing a function scope, we may need to go
7754 back to building a containing block's symbol lists. */
7755 local_symbols = new->locals;
7756 param_symbols = new->params;
27aa8d6a 7757 using_directives = new->using_directives;
208d8187 7758
921e78cf
JB
7759 /* If we've finished processing a top-level function, subsequent
7760 symbols go in the file symbol list. */
7761 if (outermost_context_p ())
e142c38c 7762 cu->list_in_scope = &file_symbols;
c906108c
SS
7763}
7764
7765/* Process all the DIES contained within a lexical block scope. Start
7766 a new scope, process the dies, and then close the scope. */
7767
7768static void
e7c27a73 7769read_lexical_block_scope (struct die_info *die, struct dwarf2_cu *cu)
c906108c 7770{
e7c27a73 7771 struct objfile *objfile = cu->objfile;
52f0bd74 7772 struct context_stack *new;
c906108c
SS
7773 CORE_ADDR lowpc, highpc;
7774 struct die_info *child_die;
e142c38c
DJ
7775 CORE_ADDR baseaddr;
7776
7777 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
c906108c
SS
7778
7779 /* Ignore blocks with missing or invalid low and high pc attributes. */
af34e669
DJ
7780 /* ??? Perhaps consider discontiguous blocks defined by DW_AT_ranges
7781 as multiple lexical blocks? Handling children in a sane way would
6e70227d 7782 be nasty. Might be easier to properly extend generic blocks to
af34e669 7783 describe ranges. */
d85a05f0 7784 if (!dwarf2_get_pc_bounds (die, &lowpc, &highpc, cu, NULL))
c906108c
SS
7785 return;
7786 lowpc += baseaddr;
7787 highpc += baseaddr;
7788
7789 push_context (0, lowpc);
639d11d3 7790 if (die->child != NULL)
c906108c 7791 {
639d11d3 7792 child_die = die->child;
c906108c
SS
7793 while (child_die && child_die->tag)
7794 {
e7c27a73 7795 process_die (child_die, cu);
c906108c
SS
7796 child_die = sibling_die (child_die);
7797 }
7798 }
7799 new = pop_context ();
7800
8540c487 7801 if (local_symbols != NULL || using_directives != NULL)
c906108c 7802 {
801e3a5b
JB
7803 struct block *block
7804 = finish_block (0, &local_symbols, new->old_blocks, new->start_addr,
7805 highpc, objfile);
7806
7807 /* Note that recording ranges after traversing children, as we
7808 do here, means that recording a parent's ranges entails
7809 walking across all its children's ranges as they appear in
7810 the address map, which is quadratic behavior.
7811
7812 It would be nicer to record the parent's ranges before
7813 traversing its children, simply overriding whatever you find
7814 there. But since we don't even decide whether to create a
7815 block until after we've traversed its children, that's hard
7816 to do. */
7817 dwarf2_record_block_ranges (die, block, baseaddr, cu);
c906108c
SS
7818 }
7819 local_symbols = new->locals;
27aa8d6a 7820 using_directives = new->using_directives;
c906108c
SS
7821}
7822
96408a79
SA
7823/* Read in DW_TAG_GNU_call_site and insert it to CU->call_site_htab. */
7824
7825static void
7826read_call_site_scope (struct die_info *die, struct dwarf2_cu *cu)
7827{
7828 struct objfile *objfile = cu->objfile;
7829 struct gdbarch *gdbarch = get_objfile_arch (objfile);
7830 CORE_ADDR pc, baseaddr;
7831 struct attribute *attr;
7832 struct call_site *call_site, call_site_local;
7833 void **slot;
7834 int nparams;
7835 struct die_info *child_die;
7836
7837 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
7838
7839 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
7840 if (!attr)
7841 {
7842 complaint (&symfile_complaints,
7843 _("missing DW_AT_low_pc for DW_TAG_GNU_call_site "
7844 "DIE 0x%x [in module %s]"),
b64f50a1 7845 die->offset.sect_off, objfile->name);
96408a79
SA
7846 return;
7847 }
7848 pc = DW_ADDR (attr) + baseaddr;
7849
7850 if (cu->call_site_htab == NULL)
7851 cu->call_site_htab = htab_create_alloc_ex (16, core_addr_hash, core_addr_eq,
7852 NULL, &objfile->objfile_obstack,
7853 hashtab_obstack_allocate, NULL);
7854 call_site_local.pc = pc;
7855 slot = htab_find_slot (cu->call_site_htab, &call_site_local, INSERT);
7856 if (*slot != NULL)
7857 {
7858 complaint (&symfile_complaints,
7859 _("Duplicate PC %s for DW_TAG_GNU_call_site "
7860 "DIE 0x%x [in module %s]"),
b64f50a1 7861 paddress (gdbarch, pc), die->offset.sect_off, objfile->name);
96408a79
SA
7862 return;
7863 }
7864
7865 /* Count parameters at the caller. */
7866
7867 nparams = 0;
7868 for (child_die = die->child; child_die && child_die->tag;
7869 child_die = sibling_die (child_die))
7870 {
7871 if (child_die->tag != DW_TAG_GNU_call_site_parameter)
7872 {
7873 complaint (&symfile_complaints,
7874 _("Tag %d is not DW_TAG_GNU_call_site_parameter in "
7875 "DW_TAG_GNU_call_site child DIE 0x%x [in module %s]"),
b64f50a1 7876 child_die->tag, child_die->offset.sect_off, objfile->name);
96408a79
SA
7877 continue;
7878 }
7879
7880 nparams++;
7881 }
7882
7883 call_site = obstack_alloc (&objfile->objfile_obstack,
7884 (sizeof (*call_site)
7885 + (sizeof (*call_site->parameter)
7886 * (nparams - 1))));
7887 *slot = call_site;
7888 memset (call_site, 0, sizeof (*call_site) - sizeof (*call_site->parameter));
7889 call_site->pc = pc;
7890
7891 if (dwarf2_flag_true_p (die, DW_AT_GNU_tail_call, cu))
7892 {
7893 struct die_info *func_die;
7894
7895 /* Skip also over DW_TAG_inlined_subroutine. */
7896 for (func_die = die->parent;
7897 func_die && func_die->tag != DW_TAG_subprogram
7898 && func_die->tag != DW_TAG_subroutine_type;
7899 func_die = func_die->parent);
7900
7901 /* DW_AT_GNU_all_call_sites is a superset
7902 of DW_AT_GNU_all_tail_call_sites. */
7903 if (func_die
7904 && !dwarf2_flag_true_p (func_die, DW_AT_GNU_all_call_sites, cu)
7905 && !dwarf2_flag_true_p (func_die, DW_AT_GNU_all_tail_call_sites, cu))
7906 {
7907 /* TYPE_TAIL_CALL_LIST is not interesting in functions where it is
7908 not complete. But keep CALL_SITE for look ups via call_site_htab,
7909 both the initial caller containing the real return address PC and
7910 the final callee containing the current PC of a chain of tail
7911 calls do not need to have the tail call list complete. But any
7912 function candidate for a virtual tail call frame searched via
7913 TYPE_TAIL_CALL_LIST must have the tail call list complete to be
7914 determined unambiguously. */
7915 }
7916 else
7917 {
7918 struct type *func_type = NULL;
7919
7920 if (func_die)
7921 func_type = get_die_type (func_die, cu);
7922 if (func_type != NULL)
7923 {
7924 gdb_assert (TYPE_CODE (func_type) == TYPE_CODE_FUNC);
7925
7926 /* Enlist this call site to the function. */
7927 call_site->tail_call_next = TYPE_TAIL_CALL_LIST (func_type);
7928 TYPE_TAIL_CALL_LIST (func_type) = call_site;
7929 }
7930 else
7931 complaint (&symfile_complaints,
7932 _("Cannot find function owning DW_TAG_GNU_call_site "
7933 "DIE 0x%x [in module %s]"),
b64f50a1 7934 die->offset.sect_off, objfile->name);
96408a79
SA
7935 }
7936 }
7937
7938 attr = dwarf2_attr (die, DW_AT_GNU_call_site_target, cu);
7939 if (attr == NULL)
7940 attr = dwarf2_attr (die, DW_AT_abstract_origin, cu);
7941 SET_FIELD_DWARF_BLOCK (call_site->target, NULL);
7942 if (!attr || (attr_form_is_block (attr) && DW_BLOCK (attr)->size == 0))
7943 /* Keep NULL DWARF_BLOCK. */;
7944 else if (attr_form_is_block (attr))
7945 {
7946 struct dwarf2_locexpr_baton *dlbaton;
7947
7948 dlbaton = obstack_alloc (&objfile->objfile_obstack, sizeof (*dlbaton));
7949 dlbaton->data = DW_BLOCK (attr)->data;
7950 dlbaton->size = DW_BLOCK (attr)->size;
7951 dlbaton->per_cu = cu->per_cu;
7952
7953 SET_FIELD_DWARF_BLOCK (call_site->target, dlbaton);
7954 }
7955 else if (is_ref_attr (attr))
7956 {
96408a79
SA
7957 struct dwarf2_cu *target_cu = cu;
7958 struct die_info *target_die;
7959
7960 target_die = follow_die_ref_or_sig (die, attr, &target_cu);
7961 gdb_assert (target_cu->objfile == objfile);
7962 if (die_is_declaration (target_die, target_cu))
7963 {
7964 const char *target_physname;
7965
7966 target_physname = dwarf2_physname (NULL, target_die, target_cu);
7967 if (target_physname == NULL)
7968 complaint (&symfile_complaints,
7969 _("DW_AT_GNU_call_site_target target DIE has invalid "
7970 "physname, for referencing DIE 0x%x [in module %s]"),
b64f50a1 7971 die->offset.sect_off, objfile->name);
96408a79
SA
7972 else
7973 SET_FIELD_PHYSNAME (call_site->target, (char *) target_physname);
7974 }
7975 else
7976 {
7977 CORE_ADDR lowpc;
7978
7979 /* DW_AT_entry_pc should be preferred. */
7980 if (!dwarf2_get_pc_bounds (target_die, &lowpc, NULL, target_cu, NULL))
7981 complaint (&symfile_complaints,
7982 _("DW_AT_GNU_call_site_target target DIE has invalid "
7983 "low pc, for referencing DIE 0x%x [in module %s]"),
b64f50a1 7984 die->offset.sect_off, objfile->name);
96408a79
SA
7985 else
7986 SET_FIELD_PHYSADDR (call_site->target, lowpc + baseaddr);
7987 }
7988 }
7989 else
7990 complaint (&symfile_complaints,
7991 _("DW_TAG_GNU_call_site DW_AT_GNU_call_site_target is neither "
7992 "block nor reference, for DIE 0x%x [in module %s]"),
b64f50a1 7993 die->offset.sect_off, objfile->name);
96408a79
SA
7994
7995 call_site->per_cu = cu->per_cu;
7996
7997 for (child_die = die->child;
7998 child_die && child_die->tag;
7999 child_die = sibling_die (child_die))
8000 {
96408a79 8001 struct call_site_parameter *parameter;
1788b2d3 8002 struct attribute *loc, *origin;
96408a79
SA
8003
8004 if (child_die->tag != DW_TAG_GNU_call_site_parameter)
8005 {
8006 /* Already printed the complaint above. */
8007 continue;
8008 }
8009
8010 gdb_assert (call_site->parameter_count < nparams);
8011 parameter = &call_site->parameter[call_site->parameter_count];
8012
1788b2d3
JK
8013 /* DW_AT_location specifies the register number or DW_AT_abstract_origin
8014 specifies DW_TAG_formal_parameter. Value of the data assumed for the
8015 register is contained in DW_AT_GNU_call_site_value. */
96408a79 8016
24c5c679 8017 loc = dwarf2_attr (child_die, DW_AT_location, cu);
1788b2d3
JK
8018 origin = dwarf2_attr (child_die, DW_AT_abstract_origin, cu);
8019 if (loc == NULL && origin != NULL && is_ref_attr (origin))
8020 {
8021 sect_offset offset;
8022
8023 parameter->kind = CALL_SITE_PARAMETER_PARAM_OFFSET;
8024 offset = dwarf2_get_ref_die_offset (origin);
8025 gdb_assert (offset.sect_off >= cu->header.offset.sect_off);
8026 parameter->u.param_offset.cu_off = (offset.sect_off
8027 - cu->header.offset.sect_off);
8028 }
8029 else if (loc == NULL || origin != NULL || !attr_form_is_block (loc))
96408a79
SA
8030 {
8031 complaint (&symfile_complaints,
8032 _("No DW_FORM_block* DW_AT_location for "
8033 "DW_TAG_GNU_call_site child DIE 0x%x [in module %s]"),
b64f50a1 8034 child_die->offset.sect_off, objfile->name);
96408a79
SA
8035 continue;
8036 }
24c5c679 8037 else
96408a79 8038 {
24c5c679
JK
8039 parameter->u.dwarf_reg = dwarf_block_to_dwarf_reg
8040 (DW_BLOCK (loc)->data, &DW_BLOCK (loc)->data[DW_BLOCK (loc)->size]);
8041 if (parameter->u.dwarf_reg != -1)
8042 parameter->kind = CALL_SITE_PARAMETER_DWARF_REG;
8043 else if (dwarf_block_to_sp_offset (gdbarch, DW_BLOCK (loc)->data,
8044 &DW_BLOCK (loc)->data[DW_BLOCK (loc)->size],
8045 &parameter->u.fb_offset))
8046 parameter->kind = CALL_SITE_PARAMETER_FB_OFFSET;
8047 else
8048 {
8049 complaint (&symfile_complaints,
8050 _("Only single DW_OP_reg or DW_OP_fbreg is supported "
8051 "for DW_FORM_block* DW_AT_location is supported for "
8052 "DW_TAG_GNU_call_site child DIE 0x%x "
8053 "[in module %s]"),
8054 child_die->offset.sect_off, objfile->name);
8055 continue;
8056 }
96408a79
SA
8057 }
8058
8059 attr = dwarf2_attr (child_die, DW_AT_GNU_call_site_value, cu);
8060 if (!attr_form_is_block (attr))
8061 {
8062 complaint (&symfile_complaints,
8063 _("No DW_FORM_block* DW_AT_GNU_call_site_value for "
8064 "DW_TAG_GNU_call_site child DIE 0x%x [in module %s]"),
b64f50a1 8065 child_die->offset.sect_off, objfile->name);
96408a79
SA
8066 continue;
8067 }
8068 parameter->value = DW_BLOCK (attr)->data;
8069 parameter->value_size = DW_BLOCK (attr)->size;
8070
8071 /* Parameters are not pre-cleared by memset above. */
8072 parameter->data_value = NULL;
8073 parameter->data_value_size = 0;
8074 call_site->parameter_count++;
8075
8076 attr = dwarf2_attr (child_die, DW_AT_GNU_call_site_data_value, cu);
8077 if (attr)
8078 {
8079 if (!attr_form_is_block (attr))
8080 complaint (&symfile_complaints,
8081 _("No DW_FORM_block* DW_AT_GNU_call_site_data_value for "
8082 "DW_TAG_GNU_call_site child DIE 0x%x [in module %s]"),
b64f50a1 8083 child_die->offset.sect_off, objfile->name);
96408a79
SA
8084 else
8085 {
8086 parameter->data_value = DW_BLOCK (attr)->data;
8087 parameter->data_value_size = DW_BLOCK (attr)->size;
8088 }
8089 }
8090 }
8091}
8092
43039443 8093/* Get low and high pc attributes from DW_AT_ranges attribute value OFFSET.
ff013f42
JK
8094 Return 1 if the attributes are present and valid, otherwise, return 0.
8095 If RANGES_PST is not NULL we should setup `objfile->psymtabs_addrmap'. */
43039443
JK
8096
8097static int
8098dwarf2_ranges_read (unsigned offset, CORE_ADDR *low_return,
ff013f42
JK
8099 CORE_ADDR *high_return, struct dwarf2_cu *cu,
8100 struct partial_symtab *ranges_pst)
43039443
JK
8101{
8102 struct objfile *objfile = cu->objfile;
8103 struct comp_unit_head *cu_header = &cu->header;
8104 bfd *obfd = objfile->obfd;
8105 unsigned int addr_size = cu_header->addr_size;
8106 CORE_ADDR mask = ~(~(CORE_ADDR)1 << (addr_size * 8 - 1));
8107 /* Base address selection entry. */
8108 CORE_ADDR base;
8109 int found_base;
8110 unsigned int dummy;
8111 gdb_byte *buffer;
8112 CORE_ADDR marker;
8113 int low_set;
8114 CORE_ADDR low = 0;
8115 CORE_ADDR high = 0;
ff013f42 8116 CORE_ADDR baseaddr;
43039443 8117
d00adf39
DE
8118 found_base = cu->base_known;
8119 base = cu->base_address;
43039443 8120
be391dca 8121 dwarf2_read_section (objfile, &dwarf2_per_objfile->ranges);
dce234bc 8122 if (offset >= dwarf2_per_objfile->ranges.size)
43039443
JK
8123 {
8124 complaint (&symfile_complaints,
8125 _("Offset %d out of bounds for DW_AT_ranges attribute"),
8126 offset);
8127 return 0;
8128 }
dce234bc 8129 buffer = dwarf2_per_objfile->ranges.buffer + offset;
43039443
JK
8130
8131 /* Read in the largest possible address. */
8132 marker = read_address (obfd, buffer, cu, &dummy);
8133 if ((marker & mask) == mask)
8134 {
8135 /* If we found the largest possible address, then
8136 read the base address. */
8137 base = read_address (obfd, buffer + addr_size, cu, &dummy);
8138 buffer += 2 * addr_size;
8139 offset += 2 * addr_size;
8140 found_base = 1;
8141 }
8142
8143 low_set = 0;
8144
e7030f15 8145 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
ff013f42 8146
43039443
JK
8147 while (1)
8148 {
8149 CORE_ADDR range_beginning, range_end;
8150
8151 range_beginning = read_address (obfd, buffer, cu, &dummy);
8152 buffer += addr_size;
8153 range_end = read_address (obfd, buffer, cu, &dummy);
8154 buffer += addr_size;
8155 offset += 2 * addr_size;
8156
8157 /* An end of list marker is a pair of zero addresses. */
8158 if (range_beginning == 0 && range_end == 0)
8159 /* Found the end of list entry. */
8160 break;
8161
8162 /* Each base address selection entry is a pair of 2 values.
8163 The first is the largest possible address, the second is
8164 the base address. Check for a base address here. */
8165 if ((range_beginning & mask) == mask)
8166 {
8167 /* If we found the largest possible address, then
8168 read the base address. */
8169 base = read_address (obfd, buffer + addr_size, cu, &dummy);
8170 found_base = 1;
8171 continue;
8172 }
8173
8174 if (!found_base)
8175 {
8176 /* We have no valid base address for the ranges
8177 data. */
8178 complaint (&symfile_complaints,
8179 _("Invalid .debug_ranges data (no base address)"));
8180 return 0;
8181 }
8182
9277c30c
UW
8183 if (range_beginning > range_end)
8184 {
8185 /* Inverted range entries are invalid. */
8186 complaint (&symfile_complaints,
8187 _("Invalid .debug_ranges data (inverted range)"));
8188 return 0;
8189 }
8190
8191 /* Empty range entries have no effect. */
8192 if (range_beginning == range_end)
8193 continue;
8194
43039443
JK
8195 range_beginning += base;
8196 range_end += base;
8197
9277c30c 8198 if (ranges_pst != NULL)
ff013f42 8199 addrmap_set_empty (objfile->psymtabs_addrmap,
3e43a32a
MS
8200 range_beginning + baseaddr,
8201 range_end - 1 + baseaddr,
ff013f42
JK
8202 ranges_pst);
8203
43039443
JK
8204 /* FIXME: This is recording everything as a low-high
8205 segment of consecutive addresses. We should have a
8206 data structure for discontiguous block ranges
8207 instead. */
8208 if (! low_set)
8209 {
8210 low = range_beginning;
8211 high = range_end;
8212 low_set = 1;
8213 }
8214 else
8215 {
8216 if (range_beginning < low)
8217 low = range_beginning;
8218 if (range_end > high)
8219 high = range_end;
8220 }
8221 }
8222
8223 if (! low_set)
8224 /* If the first entry is an end-of-list marker, the range
8225 describes an empty scope, i.e. no instructions. */
8226 return 0;
8227
8228 if (low_return)
8229 *low_return = low;
8230 if (high_return)
8231 *high_return = high;
8232 return 1;
8233}
8234
af34e669
DJ
8235/* Get low and high pc attributes from a die. Return 1 if the attributes
8236 are present and valid, otherwise, return 0. Return -1 if the range is
8237 discontinuous, i.e. derived from DW_AT_ranges information. */
380bca97 8238
c906108c 8239static int
af34e669 8240dwarf2_get_pc_bounds (struct die_info *die, CORE_ADDR *lowpc,
d85a05f0
DJ
8241 CORE_ADDR *highpc, struct dwarf2_cu *cu,
8242 struct partial_symtab *pst)
c906108c
SS
8243{
8244 struct attribute *attr;
91da1414 8245 struct attribute *attr_high;
af34e669
DJ
8246 CORE_ADDR low = 0;
8247 CORE_ADDR high = 0;
8248 int ret = 0;
c906108c 8249
91da1414
MW
8250 attr_high = dwarf2_attr (die, DW_AT_high_pc, cu);
8251 if (attr_high)
af34e669 8252 {
e142c38c 8253 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
af34e669 8254 if (attr)
91da1414
MW
8255 {
8256 low = DW_ADDR (attr);
3019eac3
DE
8257 if (attr_high->form == DW_FORM_addr
8258 || attr_high->form == DW_FORM_GNU_addr_index)
91da1414
MW
8259 high = DW_ADDR (attr_high);
8260 else
8261 high = low + DW_UNSND (attr_high);
8262 }
af34e669
DJ
8263 else
8264 /* Found high w/o low attribute. */
8265 return 0;
8266
8267 /* Found consecutive range of addresses. */
8268 ret = 1;
8269 }
c906108c 8270 else
af34e669 8271 {
e142c38c 8272 attr = dwarf2_attr (die, DW_AT_ranges, cu);
af34e669
DJ
8273 if (attr != NULL)
8274 {
2e3cf129
DE
8275 unsigned int ranges_offset = DW_UNSND (attr) + cu->ranges_base;
8276
af34e669 8277 /* Value of the DW_AT_ranges attribute is the offset in the
a604369a 8278 .debug_ranges section. */
2e3cf129 8279 if (!dwarf2_ranges_read (ranges_offset, &low, &high, cu, pst))
af34e669 8280 return 0;
43039443 8281 /* Found discontinuous range of addresses. */
af34e669
DJ
8282 ret = -1;
8283 }
8284 }
c906108c 8285
9373cf26
JK
8286 /* read_partial_die has also the strict LOW < HIGH requirement. */
8287 if (high <= low)
c906108c
SS
8288 return 0;
8289
8290 /* When using the GNU linker, .gnu.linkonce. sections are used to
8291 eliminate duplicate copies of functions and vtables and such.
8292 The linker will arbitrarily choose one and discard the others.
8293 The AT_*_pc values for such functions refer to local labels in
8294 these sections. If the section from that file was discarded, the
8295 labels are not in the output, so the relocs get a value of 0.
8296 If this is a discarded function, mark the pc bounds as invalid,
8297 so that GDB will ignore it. */
72dca2f5 8298 if (low == 0 && !dwarf2_per_objfile->has_section_at_zero)
c906108c
SS
8299 return 0;
8300
8301 *lowpc = low;
96408a79
SA
8302 if (highpc)
8303 *highpc = high;
af34e669 8304 return ret;
c906108c
SS
8305}
8306
b084d499
JB
8307/* Assuming that DIE represents a subprogram DIE or a lexical block, get
8308 its low and high PC addresses. Do nothing if these addresses could not
8309 be determined. Otherwise, set LOWPC to the low address if it is smaller,
8310 and HIGHPC to the high address if greater than HIGHPC. */
8311
8312static void
8313dwarf2_get_subprogram_pc_bounds (struct die_info *die,
8314 CORE_ADDR *lowpc, CORE_ADDR *highpc,
8315 struct dwarf2_cu *cu)
8316{
8317 CORE_ADDR low, high;
8318 struct die_info *child = die->child;
8319
d85a05f0 8320 if (dwarf2_get_pc_bounds (die, &low, &high, cu, NULL))
b084d499
JB
8321 {
8322 *lowpc = min (*lowpc, low);
8323 *highpc = max (*highpc, high);
8324 }
8325
8326 /* If the language does not allow nested subprograms (either inside
8327 subprograms or lexical blocks), we're done. */
8328 if (cu->language != language_ada)
8329 return;
6e70227d 8330
b084d499
JB
8331 /* Check all the children of the given DIE. If it contains nested
8332 subprograms, then check their pc bounds. Likewise, we need to
8333 check lexical blocks as well, as they may also contain subprogram
8334 definitions. */
8335 while (child && child->tag)
8336 {
8337 if (child->tag == DW_TAG_subprogram
8338 || child->tag == DW_TAG_lexical_block)
8339 dwarf2_get_subprogram_pc_bounds (child, lowpc, highpc, cu);
8340 child = sibling_die (child);
8341 }
8342}
8343
fae299cd
DC
8344/* Get the low and high pc's represented by the scope DIE, and store
8345 them in *LOWPC and *HIGHPC. If the correct values can't be
8346 determined, set *LOWPC to -1 and *HIGHPC to 0. */
8347
8348static void
8349get_scope_pc_bounds (struct die_info *die,
8350 CORE_ADDR *lowpc, CORE_ADDR *highpc,
8351 struct dwarf2_cu *cu)
8352{
8353 CORE_ADDR best_low = (CORE_ADDR) -1;
8354 CORE_ADDR best_high = (CORE_ADDR) 0;
8355 CORE_ADDR current_low, current_high;
8356
d85a05f0 8357 if (dwarf2_get_pc_bounds (die, &current_low, &current_high, cu, NULL))
fae299cd
DC
8358 {
8359 best_low = current_low;
8360 best_high = current_high;
8361 }
8362 else
8363 {
8364 struct die_info *child = die->child;
8365
8366 while (child && child->tag)
8367 {
8368 switch (child->tag) {
8369 case DW_TAG_subprogram:
b084d499 8370 dwarf2_get_subprogram_pc_bounds (child, &best_low, &best_high, cu);
fae299cd
DC
8371 break;
8372 case DW_TAG_namespace:
f55ee35c 8373 case DW_TAG_module:
fae299cd
DC
8374 /* FIXME: carlton/2004-01-16: Should we do this for
8375 DW_TAG_class_type/DW_TAG_structure_type, too? I think
8376 that current GCC's always emit the DIEs corresponding
8377 to definitions of methods of classes as children of a
8378 DW_TAG_compile_unit or DW_TAG_namespace (as opposed to
8379 the DIEs giving the declarations, which could be
8380 anywhere). But I don't see any reason why the
8381 standards says that they have to be there. */
8382 get_scope_pc_bounds (child, &current_low, &current_high, cu);
8383
8384 if (current_low != ((CORE_ADDR) -1))
8385 {
8386 best_low = min (best_low, current_low);
8387 best_high = max (best_high, current_high);
8388 }
8389 break;
8390 default:
0963b4bd 8391 /* Ignore. */
fae299cd
DC
8392 break;
8393 }
8394
8395 child = sibling_die (child);
8396 }
8397 }
8398
8399 *lowpc = best_low;
8400 *highpc = best_high;
8401}
8402
801e3a5b
JB
8403/* Record the address ranges for BLOCK, offset by BASEADDR, as given
8404 in DIE. */
380bca97 8405
801e3a5b
JB
8406static void
8407dwarf2_record_block_ranges (struct die_info *die, struct block *block,
8408 CORE_ADDR baseaddr, struct dwarf2_cu *cu)
8409{
bb5ed363 8410 struct objfile *objfile = cu->objfile;
801e3a5b 8411 struct attribute *attr;
91da1414 8412 struct attribute *attr_high;
801e3a5b 8413
91da1414
MW
8414 attr_high = dwarf2_attr (die, DW_AT_high_pc, cu);
8415 if (attr_high)
801e3a5b 8416 {
801e3a5b
JB
8417 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
8418 if (attr)
8419 {
8420 CORE_ADDR low = DW_ADDR (attr);
91da1414 8421 CORE_ADDR high;
3019eac3
DE
8422 if (attr_high->form == DW_FORM_addr
8423 || attr_high->form == DW_FORM_GNU_addr_index)
91da1414
MW
8424 high = DW_ADDR (attr_high);
8425 else
8426 high = low + DW_UNSND (attr_high);
9a619af0 8427
801e3a5b
JB
8428 record_block_range (block, baseaddr + low, baseaddr + high - 1);
8429 }
8430 }
8431
8432 attr = dwarf2_attr (die, DW_AT_ranges, cu);
8433 if (attr)
8434 {
bb5ed363 8435 bfd *obfd = objfile->obfd;
801e3a5b
JB
8436
8437 /* The value of the DW_AT_ranges attribute is the offset of the
8438 address range list in the .debug_ranges section. */
2e3cf129 8439 unsigned long offset = DW_UNSND (attr) + cu->ranges_base;
dce234bc 8440 gdb_byte *buffer = dwarf2_per_objfile->ranges.buffer + offset;
801e3a5b
JB
8441
8442 /* For some target architectures, but not others, the
8443 read_address function sign-extends the addresses it returns.
8444 To recognize base address selection entries, we need a
8445 mask. */
8446 unsigned int addr_size = cu->header.addr_size;
8447 CORE_ADDR base_select_mask = ~(~(CORE_ADDR)1 << (addr_size * 8 - 1));
8448
8449 /* The base address, to which the next pair is relative. Note
8450 that this 'base' is a DWARF concept: most entries in a range
8451 list are relative, to reduce the number of relocs against the
8452 debugging information. This is separate from this function's
8453 'baseaddr' argument, which GDB uses to relocate debugging
8454 information from a shared library based on the address at
8455 which the library was loaded. */
d00adf39
DE
8456 CORE_ADDR base = cu->base_address;
8457 int base_known = cu->base_known;
801e3a5b 8458
be391dca 8459 gdb_assert (dwarf2_per_objfile->ranges.readin);
dce234bc 8460 if (offset >= dwarf2_per_objfile->ranges.size)
801e3a5b
JB
8461 {
8462 complaint (&symfile_complaints,
8463 _("Offset %lu out of bounds for DW_AT_ranges attribute"),
8464 offset);
8465 return;
8466 }
8467
8468 for (;;)
8469 {
8470 unsigned int bytes_read;
8471 CORE_ADDR start, end;
8472
8473 start = read_address (obfd, buffer, cu, &bytes_read);
8474 buffer += bytes_read;
8475 end = read_address (obfd, buffer, cu, &bytes_read);
8476 buffer += bytes_read;
8477
8478 /* Did we find the end of the range list? */
8479 if (start == 0 && end == 0)
8480 break;
8481
8482 /* Did we find a base address selection entry? */
8483 else if ((start & base_select_mask) == base_select_mask)
8484 {
8485 base = end;
8486 base_known = 1;
8487 }
8488
8489 /* We found an ordinary address range. */
8490 else
8491 {
8492 if (!base_known)
8493 {
8494 complaint (&symfile_complaints,
3e43a32a
MS
8495 _("Invalid .debug_ranges data "
8496 "(no base address)"));
801e3a5b
JB
8497 return;
8498 }
8499
9277c30c
UW
8500 if (start > end)
8501 {
8502 /* Inverted range entries are invalid. */
8503 complaint (&symfile_complaints,
8504 _("Invalid .debug_ranges data "
8505 "(inverted range)"));
8506 return;
8507 }
8508
8509 /* Empty range entries have no effect. */
8510 if (start == end)
8511 continue;
8512
6e70227d
DE
8513 record_block_range (block,
8514 baseaddr + base + start,
801e3a5b
JB
8515 baseaddr + base + end - 1);
8516 }
8517 }
8518 }
8519}
8520
685b1105
JK
8521/* Check whether the producer field indicates either of GCC < 4.6, or the
8522 Intel C/C++ compiler, and cache the result in CU. */
60d5a603 8523
685b1105
JK
8524static void
8525check_producer (struct dwarf2_cu *cu)
60d5a603
JK
8526{
8527 const char *cs;
8528 int major, minor, release;
8529
8530 if (cu->producer == NULL)
8531 {
8532 /* For unknown compilers expect their behavior is DWARF version
8533 compliant.
8534
8535 GCC started to support .debug_types sections by -gdwarf-4 since
8536 gcc-4.5.x. As the .debug_types sections are missing DW_AT_producer
8537 for their space efficiency GDB cannot workaround gcc-4.5.x -gdwarf-4
8538 combination. gcc-4.5.x -gdwarf-4 binaries have DW_AT_accessibility
8539 interpreted incorrectly by GDB now - GCC PR debug/48229. */
60d5a603 8540 }
685b1105 8541 else if (strncmp (cu->producer, "GNU ", strlen ("GNU ")) == 0)
60d5a603 8542 {
685b1105
JK
8543 /* Skip any identifier after "GNU " - such as "C++" or "Java". */
8544
ba919b58
TT
8545 cs = &cu->producer[strlen ("GNU ")];
8546 while (*cs && !isdigit (*cs))
8547 cs++;
8548 if (sscanf (cs, "%d.%d.%d", &major, &minor, &release) != 3)
8549 {
8550 /* Not recognized as GCC. */
8551 }
8552 else
685b1105
JK
8553 cu->producer_is_gxx_lt_4_6 = major < 4 || (major == 4 && minor < 6);
8554 }
8555 else if (strncmp (cu->producer, "Intel(R) C", strlen ("Intel(R) C")) == 0)
8556 cu->producer_is_icc = 1;
8557 else
8558 {
8559 /* For other non-GCC compilers, expect their behavior is DWARF version
8560 compliant. */
60d5a603
JK
8561 }
8562
ba919b58 8563 cu->checked_producer = 1;
685b1105 8564}
ba919b58 8565
685b1105
JK
8566/* Check for GCC PR debug/45124 fix which is not present in any G++ version up
8567 to 4.5.any while it is present already in G++ 4.6.0 - the PR has been fixed
8568 during 4.6.0 experimental. */
8569
8570static int
8571producer_is_gxx_lt_4_6 (struct dwarf2_cu *cu)
8572{
8573 if (!cu->checked_producer)
8574 check_producer (cu);
8575
8576 return cu->producer_is_gxx_lt_4_6;
60d5a603
JK
8577}
8578
8579/* Return the default accessibility type if it is not overriden by
8580 DW_AT_accessibility. */
8581
8582static enum dwarf_access_attribute
8583dwarf2_default_access_attribute (struct die_info *die, struct dwarf2_cu *cu)
8584{
8585 if (cu->header.version < 3 || producer_is_gxx_lt_4_6 (cu))
8586 {
8587 /* The default DWARF 2 accessibility for members is public, the default
8588 accessibility for inheritance is private. */
8589
8590 if (die->tag != DW_TAG_inheritance)
8591 return DW_ACCESS_public;
8592 else
8593 return DW_ACCESS_private;
8594 }
8595 else
8596 {
8597 /* DWARF 3+ defines the default accessibility a different way. The same
8598 rules apply now for DW_TAG_inheritance as for the members and it only
8599 depends on the container kind. */
8600
8601 if (die->parent->tag == DW_TAG_class_type)
8602 return DW_ACCESS_private;
8603 else
8604 return DW_ACCESS_public;
8605 }
8606}
8607
74ac6d43
TT
8608/* Look for DW_AT_data_member_location. Set *OFFSET to the byte
8609 offset. If the attribute was not found return 0, otherwise return
8610 1. If it was found but could not properly be handled, set *OFFSET
8611 to 0. */
8612
8613static int
8614handle_data_member_location (struct die_info *die, struct dwarf2_cu *cu,
8615 LONGEST *offset)
8616{
8617 struct attribute *attr;
8618
8619 attr = dwarf2_attr (die, DW_AT_data_member_location, cu);
8620 if (attr != NULL)
8621 {
8622 *offset = 0;
8623
8624 /* Note that we do not check for a section offset first here.
8625 This is because DW_AT_data_member_location is new in DWARF 4,
8626 so if we see it, we can assume that a constant form is really
8627 a constant and not a section offset. */
8628 if (attr_form_is_constant (attr))
8629 *offset = dwarf2_get_attr_constant_value (attr, 0);
8630 else if (attr_form_is_section_offset (attr))
8631 dwarf2_complex_location_expr_complaint ();
8632 else if (attr_form_is_block (attr))
8633 *offset = decode_locdesc (DW_BLOCK (attr), cu);
8634 else
8635 dwarf2_complex_location_expr_complaint ();
8636
8637 return 1;
8638 }
8639
8640 return 0;
8641}
8642
c906108c
SS
8643/* Add an aggregate field to the field list. */
8644
8645static void
107d2387 8646dwarf2_add_field (struct field_info *fip, struct die_info *die,
e7c27a73 8647 struct dwarf2_cu *cu)
6e70227d 8648{
e7c27a73 8649 struct objfile *objfile = cu->objfile;
5e2b427d 8650 struct gdbarch *gdbarch = get_objfile_arch (objfile);
c906108c
SS
8651 struct nextfield *new_field;
8652 struct attribute *attr;
8653 struct field *fp;
8654 char *fieldname = "";
8655
8656 /* Allocate a new field list entry and link it in. */
8657 new_field = (struct nextfield *) xmalloc (sizeof (struct nextfield));
b8c9b27d 8658 make_cleanup (xfree, new_field);
c906108c 8659 memset (new_field, 0, sizeof (struct nextfield));
7d0ccb61
DJ
8660
8661 if (die->tag == DW_TAG_inheritance)
8662 {
8663 new_field->next = fip->baseclasses;
8664 fip->baseclasses = new_field;
8665 }
8666 else
8667 {
8668 new_field->next = fip->fields;
8669 fip->fields = new_field;
8670 }
c906108c
SS
8671 fip->nfields++;
8672
e142c38c 8673 attr = dwarf2_attr (die, DW_AT_accessibility, cu);
c906108c
SS
8674 if (attr)
8675 new_field->accessibility = DW_UNSND (attr);
60d5a603
JK
8676 else
8677 new_field->accessibility = dwarf2_default_access_attribute (die, cu);
c906108c
SS
8678 if (new_field->accessibility != DW_ACCESS_public)
8679 fip->non_public_fields = 1;
60d5a603 8680
e142c38c 8681 attr = dwarf2_attr (die, DW_AT_virtuality, cu);
c906108c
SS
8682 if (attr)
8683 new_field->virtuality = DW_UNSND (attr);
60d5a603
JK
8684 else
8685 new_field->virtuality = DW_VIRTUALITY_none;
c906108c
SS
8686
8687 fp = &new_field->field;
a9a9bd0f 8688
e142c38c 8689 if (die->tag == DW_TAG_member && ! die_is_declaration (die, cu))
c906108c 8690 {
74ac6d43
TT
8691 LONGEST offset;
8692
a9a9bd0f 8693 /* Data member other than a C++ static data member. */
6e70227d 8694
c906108c 8695 /* Get type of field. */
e7c27a73 8696 fp->type = die_type (die, cu);
c906108c 8697
d6a843b5 8698 SET_FIELD_BITPOS (*fp, 0);
01ad7f36 8699
c906108c 8700 /* Get bit size of field (zero if none). */
e142c38c 8701 attr = dwarf2_attr (die, DW_AT_bit_size, cu);
c906108c
SS
8702 if (attr)
8703 {
8704 FIELD_BITSIZE (*fp) = DW_UNSND (attr);
8705 }
8706 else
8707 {
8708 FIELD_BITSIZE (*fp) = 0;
8709 }
8710
8711 /* Get bit offset of field. */
74ac6d43
TT
8712 if (handle_data_member_location (die, cu, &offset))
8713 SET_FIELD_BITPOS (*fp, offset * bits_per_byte);
e142c38c 8714 attr = dwarf2_attr (die, DW_AT_bit_offset, cu);
c906108c
SS
8715 if (attr)
8716 {
5e2b427d 8717 if (gdbarch_bits_big_endian (gdbarch))
c906108c
SS
8718 {
8719 /* For big endian bits, the DW_AT_bit_offset gives the
c5aa993b
JM
8720 additional bit offset from the MSB of the containing
8721 anonymous object to the MSB of the field. We don't
8722 have to do anything special since we don't need to
8723 know the size of the anonymous object. */
f41f5e61 8724 SET_FIELD_BITPOS (*fp, FIELD_BITPOS (*fp) + DW_UNSND (attr));
c906108c
SS
8725 }
8726 else
8727 {
8728 /* For little endian bits, compute the bit offset to the
c5aa993b
JM
8729 MSB of the anonymous object, subtract off the number of
8730 bits from the MSB of the field to the MSB of the
8731 object, and then subtract off the number of bits of
8732 the field itself. The result is the bit offset of
8733 the LSB of the field. */
c906108c
SS
8734 int anonymous_size;
8735 int bit_offset = DW_UNSND (attr);
8736
e142c38c 8737 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
c906108c
SS
8738 if (attr)
8739 {
8740 /* The size of the anonymous object containing
8741 the bit field is explicit, so use the
8742 indicated size (in bytes). */
8743 anonymous_size = DW_UNSND (attr);
8744 }
8745 else
8746 {
8747 /* The size of the anonymous object containing
8748 the bit field must be inferred from the type
8749 attribute of the data member containing the
8750 bit field. */
8751 anonymous_size = TYPE_LENGTH (fp->type);
8752 }
f41f5e61
PA
8753 SET_FIELD_BITPOS (*fp,
8754 (FIELD_BITPOS (*fp)
8755 + anonymous_size * bits_per_byte
8756 - bit_offset - FIELD_BITSIZE (*fp)));
c906108c
SS
8757 }
8758 }
8759
8760 /* Get name of field. */
39cbfefa
DJ
8761 fieldname = dwarf2_name (die, cu);
8762 if (fieldname == NULL)
8763 fieldname = "";
d8151005
DJ
8764
8765 /* The name is already allocated along with this objfile, so we don't
8766 need to duplicate it for the type. */
8767 fp->name = fieldname;
c906108c
SS
8768
8769 /* Change accessibility for artificial fields (e.g. virtual table
c5aa993b 8770 pointer or virtual base class pointer) to private. */
e142c38c 8771 if (dwarf2_attr (die, DW_AT_artificial, cu))
c906108c 8772 {
d48cc9dd 8773 FIELD_ARTIFICIAL (*fp) = 1;
c906108c
SS
8774 new_field->accessibility = DW_ACCESS_private;
8775 fip->non_public_fields = 1;
8776 }
8777 }
a9a9bd0f 8778 else if (die->tag == DW_TAG_member || die->tag == DW_TAG_variable)
c906108c 8779 {
a9a9bd0f
DC
8780 /* C++ static member. */
8781
8782 /* NOTE: carlton/2002-11-05: It should be a DW_TAG_member that
8783 is a declaration, but all versions of G++ as of this writing
8784 (so through at least 3.2.1) incorrectly generate
8785 DW_TAG_variable tags. */
6e70227d 8786
ff355380 8787 const char *physname;
c906108c 8788
a9a9bd0f 8789 /* Get name of field. */
39cbfefa
DJ
8790 fieldname = dwarf2_name (die, cu);
8791 if (fieldname == NULL)
c906108c
SS
8792 return;
8793
254e6b9e 8794 attr = dwarf2_attr (die, DW_AT_const_value, cu);
3863f96c
DE
8795 if (attr
8796 /* Only create a symbol if this is an external value.
8797 new_symbol checks this and puts the value in the global symbol
8798 table, which we want. If it is not external, new_symbol
8799 will try to put the value in cu->list_in_scope which is wrong. */
8800 && dwarf2_flag_true_p (die, DW_AT_external, cu))
254e6b9e
DE
8801 {
8802 /* A static const member, not much different than an enum as far as
8803 we're concerned, except that we can support more types. */
8804 new_symbol (die, NULL, cu);
8805 }
8806
2df3850c 8807 /* Get physical name. */
ff355380 8808 physname = dwarf2_physname (fieldname, die, cu);
c906108c 8809
d8151005
DJ
8810 /* The name is already allocated along with this objfile, so we don't
8811 need to duplicate it for the type. */
8812 SET_FIELD_PHYSNAME (*fp, physname ? physname : "");
e7c27a73 8813 FIELD_TYPE (*fp) = die_type (die, cu);
d8151005 8814 FIELD_NAME (*fp) = fieldname;
c906108c
SS
8815 }
8816 else if (die->tag == DW_TAG_inheritance)
8817 {
74ac6d43 8818 LONGEST offset;
d4b96c9a 8819
74ac6d43
TT
8820 /* C++ base class field. */
8821 if (handle_data_member_location (die, cu, &offset))
8822 SET_FIELD_BITPOS (*fp, offset * bits_per_byte);
c906108c 8823 FIELD_BITSIZE (*fp) = 0;
e7c27a73 8824 FIELD_TYPE (*fp) = die_type (die, cu);
c906108c
SS
8825 FIELD_NAME (*fp) = type_name_no_tag (fp->type);
8826 fip->nbaseclasses++;
8827 }
8828}
8829
98751a41
JK
8830/* Add a typedef defined in the scope of the FIP's class. */
8831
8832static void
8833dwarf2_add_typedef (struct field_info *fip, struct die_info *die,
8834 struct dwarf2_cu *cu)
6e70227d 8835{
98751a41 8836 struct objfile *objfile = cu->objfile;
98751a41
JK
8837 struct typedef_field_list *new_field;
8838 struct attribute *attr;
8839 struct typedef_field *fp;
8840 char *fieldname = "";
8841
8842 /* Allocate a new field list entry and link it in. */
8843 new_field = xzalloc (sizeof (*new_field));
8844 make_cleanup (xfree, new_field);
8845
8846 gdb_assert (die->tag == DW_TAG_typedef);
8847
8848 fp = &new_field->field;
8849
8850 /* Get name of field. */
8851 fp->name = dwarf2_name (die, cu);
8852 if (fp->name == NULL)
8853 return;
8854
8855 fp->type = read_type_die (die, cu);
8856
8857 new_field->next = fip->typedef_field_list;
8858 fip->typedef_field_list = new_field;
8859 fip->typedef_field_list_count++;
8860}
8861
c906108c
SS
8862/* Create the vector of fields, and attach it to the type. */
8863
8864static void
fba45db2 8865dwarf2_attach_fields_to_type (struct field_info *fip, struct type *type,
e7c27a73 8866 struct dwarf2_cu *cu)
c906108c
SS
8867{
8868 int nfields = fip->nfields;
8869
8870 /* Record the field count, allocate space for the array of fields,
8871 and create blank accessibility bitfields if necessary. */
8872 TYPE_NFIELDS (type) = nfields;
8873 TYPE_FIELDS (type) = (struct field *)
8874 TYPE_ALLOC (type, sizeof (struct field) * nfields);
8875 memset (TYPE_FIELDS (type), 0, sizeof (struct field) * nfields);
8876
b4ba55a1 8877 if (fip->non_public_fields && cu->language != language_ada)
c906108c
SS
8878 {
8879 ALLOCATE_CPLUS_STRUCT_TYPE (type);
8880
8881 TYPE_FIELD_PRIVATE_BITS (type) =
8882 (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
8883 B_CLRALL (TYPE_FIELD_PRIVATE_BITS (type), nfields);
8884
8885 TYPE_FIELD_PROTECTED_BITS (type) =
8886 (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
8887 B_CLRALL (TYPE_FIELD_PROTECTED_BITS (type), nfields);
8888
774b6a14
TT
8889 TYPE_FIELD_IGNORE_BITS (type) =
8890 (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
8891 B_CLRALL (TYPE_FIELD_IGNORE_BITS (type), nfields);
c906108c
SS
8892 }
8893
8894 /* If the type has baseclasses, allocate and clear a bit vector for
8895 TYPE_FIELD_VIRTUAL_BITS. */
b4ba55a1 8896 if (fip->nbaseclasses && cu->language != language_ada)
c906108c
SS
8897 {
8898 int num_bytes = B_BYTES (fip->nbaseclasses);
fe1b8b76 8899 unsigned char *pointer;
c906108c
SS
8900
8901 ALLOCATE_CPLUS_STRUCT_TYPE (type);
fe1b8b76
JB
8902 pointer = TYPE_ALLOC (type, num_bytes);
8903 TYPE_FIELD_VIRTUAL_BITS (type) = pointer;
c906108c
SS
8904 B_CLRALL (TYPE_FIELD_VIRTUAL_BITS (type), fip->nbaseclasses);
8905 TYPE_N_BASECLASSES (type) = fip->nbaseclasses;
8906 }
8907
3e43a32a
MS
8908 /* Copy the saved-up fields into the field vector. Start from the head of
8909 the list, adding to the tail of the field array, so that they end up in
8910 the same order in the array in which they were added to the list. */
c906108c
SS
8911 while (nfields-- > 0)
8912 {
7d0ccb61
DJ
8913 struct nextfield *fieldp;
8914
8915 if (fip->fields)
8916 {
8917 fieldp = fip->fields;
8918 fip->fields = fieldp->next;
8919 }
8920 else
8921 {
8922 fieldp = fip->baseclasses;
8923 fip->baseclasses = fieldp->next;
8924 }
8925
8926 TYPE_FIELD (type, nfields) = fieldp->field;
8927 switch (fieldp->accessibility)
c906108c 8928 {
c5aa993b 8929 case DW_ACCESS_private:
b4ba55a1
JB
8930 if (cu->language != language_ada)
8931 SET_TYPE_FIELD_PRIVATE (type, nfields);
c5aa993b 8932 break;
c906108c 8933
c5aa993b 8934 case DW_ACCESS_protected:
b4ba55a1
JB
8935 if (cu->language != language_ada)
8936 SET_TYPE_FIELD_PROTECTED (type, nfields);
c5aa993b 8937 break;
c906108c 8938
c5aa993b
JM
8939 case DW_ACCESS_public:
8940 break;
c906108c 8941
c5aa993b
JM
8942 default:
8943 /* Unknown accessibility. Complain and treat it as public. */
8944 {
e2e0b3e5 8945 complaint (&symfile_complaints, _("unsupported accessibility %d"),
7d0ccb61 8946 fieldp->accessibility);
c5aa993b
JM
8947 }
8948 break;
c906108c
SS
8949 }
8950 if (nfields < fip->nbaseclasses)
8951 {
7d0ccb61 8952 switch (fieldp->virtuality)
c906108c 8953 {
c5aa993b
JM
8954 case DW_VIRTUALITY_virtual:
8955 case DW_VIRTUALITY_pure_virtual:
b4ba55a1 8956 if (cu->language == language_ada)
a73c6dcd 8957 error (_("unexpected virtuality in component of Ada type"));
c5aa993b
JM
8958 SET_TYPE_FIELD_VIRTUAL (type, nfields);
8959 break;
c906108c
SS
8960 }
8961 }
c906108c
SS
8962 }
8963}
8964
c906108c
SS
8965/* Add a member function to the proper fieldlist. */
8966
8967static void
107d2387 8968dwarf2_add_member_fn (struct field_info *fip, struct die_info *die,
e7c27a73 8969 struct type *type, struct dwarf2_cu *cu)
c906108c 8970{
e7c27a73 8971 struct objfile *objfile = cu->objfile;
c906108c
SS
8972 struct attribute *attr;
8973 struct fnfieldlist *flp;
8974 int i;
8975 struct fn_field *fnp;
8976 char *fieldname;
c906108c 8977 struct nextfnfield *new_fnfield;
f792889a 8978 struct type *this_type;
60d5a603 8979 enum dwarf_access_attribute accessibility;
c906108c 8980
b4ba55a1 8981 if (cu->language == language_ada)
a73c6dcd 8982 error (_("unexpected member function in Ada type"));
b4ba55a1 8983
2df3850c 8984 /* Get name of member function. */
39cbfefa
DJ
8985 fieldname = dwarf2_name (die, cu);
8986 if (fieldname == NULL)
2df3850c 8987 return;
c906108c 8988
c906108c
SS
8989 /* Look up member function name in fieldlist. */
8990 for (i = 0; i < fip->nfnfields; i++)
8991 {
27bfe10e 8992 if (strcmp (fip->fnfieldlists[i].name, fieldname) == 0)
c906108c
SS
8993 break;
8994 }
8995
8996 /* Create new list element if necessary. */
8997 if (i < fip->nfnfields)
8998 flp = &fip->fnfieldlists[i];
8999 else
9000 {
9001 if ((fip->nfnfields % DW_FIELD_ALLOC_CHUNK) == 0)
9002 {
9003 fip->fnfieldlists = (struct fnfieldlist *)
9004 xrealloc (fip->fnfieldlists,
9005 (fip->nfnfields + DW_FIELD_ALLOC_CHUNK)
c5aa993b 9006 * sizeof (struct fnfieldlist));
c906108c 9007 if (fip->nfnfields == 0)
c13c43fd 9008 make_cleanup (free_current_contents, &fip->fnfieldlists);
c906108c
SS
9009 }
9010 flp = &fip->fnfieldlists[fip->nfnfields];
9011 flp->name = fieldname;
9012 flp->length = 0;
9013 flp->head = NULL;
3da10d80 9014 i = fip->nfnfields++;
c906108c
SS
9015 }
9016
9017 /* Create a new member function field and chain it to the field list
0963b4bd 9018 entry. */
c906108c 9019 new_fnfield = (struct nextfnfield *) xmalloc (sizeof (struct nextfnfield));
b8c9b27d 9020 make_cleanup (xfree, new_fnfield);
c906108c
SS
9021 memset (new_fnfield, 0, sizeof (struct nextfnfield));
9022 new_fnfield->next = flp->head;
9023 flp->head = new_fnfield;
9024 flp->length++;
9025
9026 /* Fill in the member function field info. */
9027 fnp = &new_fnfield->fnfield;
3da10d80
KS
9028
9029 /* Delay processing of the physname until later. */
9030 if (cu->language == language_cplus || cu->language == language_java)
9031 {
9032 add_to_method_list (type, i, flp->length - 1, fieldname,
9033 die, cu);
9034 }
9035 else
9036 {
1d06ead6 9037 const char *physname = dwarf2_physname (fieldname, die, cu);
3da10d80
KS
9038 fnp->physname = physname ? physname : "";
9039 }
9040
c906108c 9041 fnp->type = alloc_type (objfile);
f792889a
DJ
9042 this_type = read_type_die (die, cu);
9043 if (this_type && TYPE_CODE (this_type) == TYPE_CODE_FUNC)
c906108c 9044 {
f792889a 9045 int nparams = TYPE_NFIELDS (this_type);
c906108c 9046
f792889a 9047 /* TYPE is the domain of this method, and THIS_TYPE is the type
e26fb1d7
DC
9048 of the method itself (TYPE_CODE_METHOD). */
9049 smash_to_method_type (fnp->type, type,
f792889a
DJ
9050 TYPE_TARGET_TYPE (this_type),
9051 TYPE_FIELDS (this_type),
9052 TYPE_NFIELDS (this_type),
9053 TYPE_VARARGS (this_type));
c906108c
SS
9054
9055 /* Handle static member functions.
c5aa993b 9056 Dwarf2 has no clean way to discern C++ static and non-static
0963b4bd
MS
9057 member functions. G++ helps GDB by marking the first
9058 parameter for non-static member functions (which is the this
9059 pointer) as artificial. We obtain this information from
9060 read_subroutine_type via TYPE_FIELD_ARTIFICIAL. */
f792889a 9061 if (nparams == 0 || TYPE_FIELD_ARTIFICIAL (this_type, 0) == 0)
c906108c
SS
9062 fnp->voffset = VOFFSET_STATIC;
9063 }
9064 else
e2e0b3e5 9065 complaint (&symfile_complaints, _("member function type missing for '%s'"),
3da10d80 9066 dwarf2_full_name (fieldname, die, cu));
c906108c
SS
9067
9068 /* Get fcontext from DW_AT_containing_type if present. */
e142c38c 9069 if (dwarf2_attr (die, DW_AT_containing_type, cu) != NULL)
e7c27a73 9070 fnp->fcontext = die_containing_type (die, cu);
c906108c 9071
3e43a32a
MS
9072 /* dwarf2 doesn't have stubbed physical names, so the setting of is_const and
9073 is_volatile is irrelevant, as it is needed by gdb_mangle_name only. */
c906108c
SS
9074
9075 /* Get accessibility. */
e142c38c 9076 attr = dwarf2_attr (die, DW_AT_accessibility, cu);
c906108c 9077 if (attr)
60d5a603
JK
9078 accessibility = DW_UNSND (attr);
9079 else
9080 accessibility = dwarf2_default_access_attribute (die, cu);
9081 switch (accessibility)
c906108c 9082 {
60d5a603
JK
9083 case DW_ACCESS_private:
9084 fnp->is_private = 1;
9085 break;
9086 case DW_ACCESS_protected:
9087 fnp->is_protected = 1;
9088 break;
c906108c
SS
9089 }
9090
b02dede2 9091 /* Check for artificial methods. */
e142c38c 9092 attr = dwarf2_attr (die, DW_AT_artificial, cu);
b02dede2
DJ
9093 if (attr && DW_UNSND (attr) != 0)
9094 fnp->is_artificial = 1;
9095
0d564a31 9096 /* Get index in virtual function table if it is a virtual member
aec5aa8b
TT
9097 function. For older versions of GCC, this is an offset in the
9098 appropriate virtual table, as specified by DW_AT_containing_type.
9099 For everyone else, it is an expression to be evaluated relative
0d564a31
DJ
9100 to the object address. */
9101
e142c38c 9102 attr = dwarf2_attr (die, DW_AT_vtable_elem_location, cu);
aec5aa8b 9103 if (attr)
8e19ed76 9104 {
aec5aa8b 9105 if (attr_form_is_block (attr) && DW_BLOCK (attr)->size > 0)
8e19ed76 9106 {
aec5aa8b
TT
9107 if (DW_BLOCK (attr)->data[0] == DW_OP_constu)
9108 {
9109 /* Old-style GCC. */
9110 fnp->voffset = decode_locdesc (DW_BLOCK (attr), cu) + 2;
9111 }
9112 else if (DW_BLOCK (attr)->data[0] == DW_OP_deref
9113 || (DW_BLOCK (attr)->size > 1
9114 && DW_BLOCK (attr)->data[0] == DW_OP_deref_size
9115 && DW_BLOCK (attr)->data[1] == cu->header.addr_size))
9116 {
9117 struct dwarf_block blk;
9118 int offset;
9119
9120 offset = (DW_BLOCK (attr)->data[0] == DW_OP_deref
9121 ? 1 : 2);
9122 blk.size = DW_BLOCK (attr)->size - offset;
9123 blk.data = DW_BLOCK (attr)->data + offset;
9124 fnp->voffset = decode_locdesc (DW_BLOCK (attr), cu);
9125 if ((fnp->voffset % cu->header.addr_size) != 0)
9126 dwarf2_complex_location_expr_complaint ();
9127 else
9128 fnp->voffset /= cu->header.addr_size;
9129 fnp->voffset += 2;
9130 }
9131 else
9132 dwarf2_complex_location_expr_complaint ();
9133
9134 if (!fnp->fcontext)
9135 fnp->fcontext = TYPE_TARGET_TYPE (TYPE_FIELD_TYPE (this_type, 0));
9136 }
3690dd37 9137 else if (attr_form_is_section_offset (attr))
8e19ed76 9138 {
4d3c2250 9139 dwarf2_complex_location_expr_complaint ();
8e19ed76
PS
9140 }
9141 else
9142 {
4d3c2250
KB
9143 dwarf2_invalid_attrib_class_complaint ("DW_AT_vtable_elem_location",
9144 fieldname);
8e19ed76 9145 }
0d564a31 9146 }
d48cc9dd
DJ
9147 else
9148 {
9149 attr = dwarf2_attr (die, DW_AT_virtuality, cu);
9150 if (attr && DW_UNSND (attr))
9151 {
9152 /* GCC does this, as of 2008-08-25; PR debug/37237. */
9153 complaint (&symfile_complaints,
3e43a32a
MS
9154 _("Member function \"%s\" (offset %d) is virtual "
9155 "but the vtable offset is not specified"),
b64f50a1 9156 fieldname, die->offset.sect_off);
9655fd1a 9157 ALLOCATE_CPLUS_STRUCT_TYPE (type);
d48cc9dd
DJ
9158 TYPE_CPLUS_DYNAMIC (type) = 1;
9159 }
9160 }
c906108c
SS
9161}
9162
9163/* Create the vector of member function fields, and attach it to the type. */
9164
9165static void
fba45db2 9166dwarf2_attach_fn_fields_to_type (struct field_info *fip, struct type *type,
e7c27a73 9167 struct dwarf2_cu *cu)
c906108c
SS
9168{
9169 struct fnfieldlist *flp;
c906108c
SS
9170 int i;
9171
b4ba55a1 9172 if (cu->language == language_ada)
a73c6dcd 9173 error (_("unexpected member functions in Ada type"));
b4ba55a1 9174
c906108c
SS
9175 ALLOCATE_CPLUS_STRUCT_TYPE (type);
9176 TYPE_FN_FIELDLISTS (type) = (struct fn_fieldlist *)
9177 TYPE_ALLOC (type, sizeof (struct fn_fieldlist) * fip->nfnfields);
9178
9179 for (i = 0, flp = fip->fnfieldlists; i < fip->nfnfields; i++, flp++)
9180 {
9181 struct nextfnfield *nfp = flp->head;
9182 struct fn_fieldlist *fn_flp = &TYPE_FN_FIELDLIST (type, i);
9183 int k;
9184
9185 TYPE_FN_FIELDLIST_NAME (type, i) = flp->name;
9186 TYPE_FN_FIELDLIST_LENGTH (type, i) = flp->length;
9187 fn_flp->fn_fields = (struct fn_field *)
9188 TYPE_ALLOC (type, sizeof (struct fn_field) * flp->length);
9189 for (k = flp->length; (k--, nfp); nfp = nfp->next)
c5aa993b 9190 fn_flp->fn_fields[k] = nfp->fnfield;
c906108c
SS
9191 }
9192
9193 TYPE_NFN_FIELDS (type) = fip->nfnfields;
c906108c
SS
9194}
9195
1168df01
JB
9196/* Returns non-zero if NAME is the name of a vtable member in CU's
9197 language, zero otherwise. */
9198static int
9199is_vtable_name (const char *name, struct dwarf2_cu *cu)
9200{
9201 static const char vptr[] = "_vptr";
987504bb 9202 static const char vtable[] = "vtable";
1168df01 9203
987504bb
JJ
9204 /* Look for the C++ and Java forms of the vtable. */
9205 if ((cu->language == language_java
9206 && strncmp (name, vtable, sizeof (vtable) - 1) == 0)
9207 || (strncmp (name, vptr, sizeof (vptr) - 1) == 0
9208 && is_cplus_marker (name[sizeof (vptr) - 1])))
1168df01
JB
9209 return 1;
9210
9211 return 0;
9212}
9213
c0dd20ea 9214/* GCC outputs unnamed structures that are really pointers to member
0b92b5bb
TT
9215 functions, with the ABI-specified layout. If TYPE describes
9216 such a structure, smash it into a member function type.
61049d3b
DJ
9217
9218 GCC shouldn't do this; it should just output pointer to member DIEs.
9219 This is GCC PR debug/28767. */
c0dd20ea 9220
0b92b5bb
TT
9221static void
9222quirk_gcc_member_function_pointer (struct type *type, struct objfile *objfile)
c0dd20ea 9223{
0b92b5bb 9224 struct type *pfn_type, *domain_type, *new_type;
c0dd20ea
DJ
9225
9226 /* Check for a structure with no name and two children. */
0b92b5bb
TT
9227 if (TYPE_CODE (type) != TYPE_CODE_STRUCT || TYPE_NFIELDS (type) != 2)
9228 return;
c0dd20ea
DJ
9229
9230 /* Check for __pfn and __delta members. */
0b92b5bb
TT
9231 if (TYPE_FIELD_NAME (type, 0) == NULL
9232 || strcmp (TYPE_FIELD_NAME (type, 0), "__pfn") != 0
9233 || TYPE_FIELD_NAME (type, 1) == NULL
9234 || strcmp (TYPE_FIELD_NAME (type, 1), "__delta") != 0)
9235 return;
c0dd20ea
DJ
9236
9237 /* Find the type of the method. */
0b92b5bb 9238 pfn_type = TYPE_FIELD_TYPE (type, 0);
c0dd20ea
DJ
9239 if (pfn_type == NULL
9240 || TYPE_CODE (pfn_type) != TYPE_CODE_PTR
9241 || TYPE_CODE (TYPE_TARGET_TYPE (pfn_type)) != TYPE_CODE_FUNC)
0b92b5bb 9242 return;
c0dd20ea
DJ
9243
9244 /* Look for the "this" argument. */
9245 pfn_type = TYPE_TARGET_TYPE (pfn_type);
9246 if (TYPE_NFIELDS (pfn_type) == 0
0b92b5bb 9247 /* || TYPE_FIELD_TYPE (pfn_type, 0) == NULL */
c0dd20ea 9248 || TYPE_CODE (TYPE_FIELD_TYPE (pfn_type, 0)) != TYPE_CODE_PTR)
0b92b5bb 9249 return;
c0dd20ea
DJ
9250
9251 domain_type = TYPE_TARGET_TYPE (TYPE_FIELD_TYPE (pfn_type, 0));
0b92b5bb
TT
9252 new_type = alloc_type (objfile);
9253 smash_to_method_type (new_type, domain_type, TYPE_TARGET_TYPE (pfn_type),
c0dd20ea
DJ
9254 TYPE_FIELDS (pfn_type), TYPE_NFIELDS (pfn_type),
9255 TYPE_VARARGS (pfn_type));
0b92b5bb 9256 smash_to_methodptr_type (type, new_type);
c0dd20ea 9257}
1168df01 9258
685b1105
JK
9259/* Return non-zero if the CU's PRODUCER string matches the Intel C/C++ compiler
9260 (icc). */
9261
9262static int
9263producer_is_icc (struct dwarf2_cu *cu)
9264{
9265 if (!cu->checked_producer)
9266 check_producer (cu);
9267
9268 return cu->producer_is_icc;
9269}
9270
c906108c 9271/* Called when we find the DIE that starts a structure or union scope
c767944b
DJ
9272 (definition) to create a type for the structure or union. Fill in
9273 the type's name and general properties; the members will not be
9274 processed until process_structure_type.
c906108c 9275
c767944b
DJ
9276 NOTE: we need to call these functions regardless of whether or not the
9277 DIE has a DW_AT_name attribute, since it might be an anonymous
c906108c
SS
9278 structure or union. This gets the type entered into our set of
9279 user defined types.
9280
9281 However, if the structure is incomplete (an opaque struct/union)
9282 then suppress creating a symbol table entry for it since gdb only
9283 wants to find the one with the complete definition. Note that if
9284 it is complete, we just call new_symbol, which does it's own
9285 checking about whether the struct/union is anonymous or not (and
9286 suppresses creating a symbol table entry itself). */
9287
f792889a 9288static struct type *
134d01f1 9289read_structure_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 9290{
e7c27a73 9291 struct objfile *objfile = cu->objfile;
c906108c
SS
9292 struct type *type;
9293 struct attribute *attr;
39cbfefa 9294 char *name;
c906108c 9295
348e048f
DE
9296 /* If the definition of this type lives in .debug_types, read that type.
9297 Don't follow DW_AT_specification though, that will take us back up
9298 the chain and we want to go down. */
9299 attr = dwarf2_attr_no_follow (die, DW_AT_signature, cu);
9300 if (attr)
9301 {
9302 struct dwarf2_cu *type_cu = cu;
9303 struct die_info *type_die = follow_die_ref_or_sig (die, attr, &type_cu);
9a619af0 9304
348e048f
DE
9305 /* We could just recurse on read_structure_type, but we need to call
9306 get_die_type to ensure only one type for this DIE is created.
9307 This is important, for example, because for c++ classes we need
9308 TYPE_NAME set which is only done by new_symbol. Blech. */
9309 type = read_type_die (type_die, type_cu);
9dc481d3
DE
9310
9311 /* TYPE_CU may not be the same as CU.
9312 Ensure TYPE is recorded in CU's type_hash table. */
348e048f
DE
9313 return set_die_type (die, type, cu);
9314 }
9315
c0dd20ea 9316 type = alloc_type (objfile);
c906108c 9317 INIT_CPLUS_SPECIFIC (type);
93311388 9318
39cbfefa
DJ
9319 name = dwarf2_name (die, cu);
9320 if (name != NULL)
c906108c 9321 {
987504bb
JJ
9322 if (cu->language == language_cplus
9323 || cu->language == language_java)
63d06c5c 9324 {
3da10d80
KS
9325 char *full_name = (char *) dwarf2_full_name (name, die, cu);
9326
9327 /* dwarf2_full_name might have already finished building the DIE's
9328 type. If so, there is no need to continue. */
9329 if (get_die_type (die, cu) != NULL)
9330 return get_die_type (die, cu);
9331
9332 TYPE_TAG_NAME (type) = full_name;
94af9270
KS
9333 if (die->tag == DW_TAG_structure_type
9334 || die->tag == DW_TAG_class_type)
9335 TYPE_NAME (type) = TYPE_TAG_NAME (type);
63d06c5c
DC
9336 }
9337 else
9338 {
d8151005
DJ
9339 /* The name is already allocated along with this objfile, so
9340 we don't need to duplicate it for the type. */
94af9270
KS
9341 TYPE_TAG_NAME (type) = (char *) name;
9342 if (die->tag == DW_TAG_class_type)
9343 TYPE_NAME (type) = TYPE_TAG_NAME (type);
63d06c5c 9344 }
c906108c
SS
9345 }
9346
9347 if (die->tag == DW_TAG_structure_type)
9348 {
9349 TYPE_CODE (type) = TYPE_CODE_STRUCT;
9350 }
9351 else if (die->tag == DW_TAG_union_type)
9352 {
9353 TYPE_CODE (type) = TYPE_CODE_UNION;
9354 }
9355 else
9356 {
c906108c
SS
9357 TYPE_CODE (type) = TYPE_CODE_CLASS;
9358 }
9359
0cc2414c
TT
9360 if (cu->language == language_cplus && die->tag == DW_TAG_class_type)
9361 TYPE_DECLARED_CLASS (type) = 1;
9362
e142c38c 9363 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
c906108c
SS
9364 if (attr)
9365 {
9366 TYPE_LENGTH (type) = DW_UNSND (attr);
9367 }
9368 else
9369 {
9370 TYPE_LENGTH (type) = 0;
9371 }
9372
685b1105
JK
9373 if (producer_is_icc (cu))
9374 {
9375 /* ICC does not output the required DW_AT_declaration
9376 on incomplete types, but gives them a size of zero. */
9377 }
9378 else
9379 TYPE_STUB_SUPPORTED (type) = 1;
9380
dc718098 9381 if (die_is_declaration (die, cu))
876cecd0 9382 TYPE_STUB (type) = 1;
a6c727b2
DJ
9383 else if (attr == NULL && die->child == NULL
9384 && producer_is_realview (cu->producer))
9385 /* RealView does not output the required DW_AT_declaration
9386 on incomplete types. */
9387 TYPE_STUB (type) = 1;
dc718098 9388
c906108c
SS
9389 /* We need to add the type field to the die immediately so we don't
9390 infinitely recurse when dealing with pointers to the structure
0963b4bd 9391 type within the structure itself. */
1c379e20 9392 set_die_type (die, type, cu);
c906108c 9393
7e314c57
JK
9394 /* set_die_type should be already done. */
9395 set_descriptive_type (type, die, cu);
9396
c767944b
DJ
9397 return type;
9398}
9399
9400/* Finish creating a structure or union type, including filling in
9401 its members and creating a symbol for it. */
9402
9403static void
9404process_structure_scope (struct die_info *die, struct dwarf2_cu *cu)
9405{
9406 struct objfile *objfile = cu->objfile;
9407 struct die_info *child_die = die->child;
9408 struct type *type;
9409
9410 type = get_die_type (die, cu);
9411 if (type == NULL)
9412 type = read_structure_type (die, cu);
9413
e142c38c 9414 if (die->child != NULL && ! die_is_declaration (die, cu))
c906108c
SS
9415 {
9416 struct field_info fi;
9417 struct die_info *child_die;
34eaf542 9418 VEC (symbolp) *template_args = NULL;
c767944b 9419 struct cleanup *back_to = make_cleanup (null_cleanup, 0);
c906108c
SS
9420
9421 memset (&fi, 0, sizeof (struct field_info));
9422
639d11d3 9423 child_die = die->child;
c906108c
SS
9424
9425 while (child_die && child_die->tag)
9426 {
a9a9bd0f
DC
9427 if (child_die->tag == DW_TAG_member
9428 || child_die->tag == DW_TAG_variable)
c906108c 9429 {
a9a9bd0f
DC
9430 /* NOTE: carlton/2002-11-05: A C++ static data member
9431 should be a DW_TAG_member that is a declaration, but
9432 all versions of G++ as of this writing (so through at
9433 least 3.2.1) incorrectly generate DW_TAG_variable
9434 tags for them instead. */
e7c27a73 9435 dwarf2_add_field (&fi, child_die, cu);
c906108c 9436 }
8713b1b1 9437 else if (child_die->tag == DW_TAG_subprogram)
c906108c 9438 {
0963b4bd 9439 /* C++ member function. */
e7c27a73 9440 dwarf2_add_member_fn (&fi, child_die, type, cu);
c906108c
SS
9441 }
9442 else if (child_die->tag == DW_TAG_inheritance)
9443 {
9444 /* C++ base class field. */
e7c27a73 9445 dwarf2_add_field (&fi, child_die, cu);
c906108c 9446 }
98751a41
JK
9447 else if (child_die->tag == DW_TAG_typedef)
9448 dwarf2_add_typedef (&fi, child_die, cu);
34eaf542
TT
9449 else if (child_die->tag == DW_TAG_template_type_param
9450 || child_die->tag == DW_TAG_template_value_param)
9451 {
9452 struct symbol *arg = new_symbol (child_die, NULL, cu);
9453
f1078f66
DJ
9454 if (arg != NULL)
9455 VEC_safe_push (symbolp, template_args, arg);
34eaf542
TT
9456 }
9457
c906108c
SS
9458 child_die = sibling_die (child_die);
9459 }
9460
34eaf542
TT
9461 /* Attach template arguments to type. */
9462 if (! VEC_empty (symbolp, template_args))
9463 {
9464 ALLOCATE_CPLUS_STRUCT_TYPE (type);
9465 TYPE_N_TEMPLATE_ARGUMENTS (type)
9466 = VEC_length (symbolp, template_args);
9467 TYPE_TEMPLATE_ARGUMENTS (type)
9468 = obstack_alloc (&objfile->objfile_obstack,
9469 (TYPE_N_TEMPLATE_ARGUMENTS (type)
9470 * sizeof (struct symbol *)));
9471 memcpy (TYPE_TEMPLATE_ARGUMENTS (type),
9472 VEC_address (symbolp, template_args),
9473 (TYPE_N_TEMPLATE_ARGUMENTS (type)
9474 * sizeof (struct symbol *)));
9475 VEC_free (symbolp, template_args);
9476 }
9477
c906108c
SS
9478 /* Attach fields and member functions to the type. */
9479 if (fi.nfields)
e7c27a73 9480 dwarf2_attach_fields_to_type (&fi, type, cu);
c906108c
SS
9481 if (fi.nfnfields)
9482 {
e7c27a73 9483 dwarf2_attach_fn_fields_to_type (&fi, type, cu);
c906108c 9484
c5aa993b 9485 /* Get the type which refers to the base class (possibly this
c906108c 9486 class itself) which contains the vtable pointer for the current
0d564a31
DJ
9487 class from the DW_AT_containing_type attribute. This use of
9488 DW_AT_containing_type is a GNU extension. */
c906108c 9489
e142c38c 9490 if (dwarf2_attr (die, DW_AT_containing_type, cu) != NULL)
c906108c 9491 {
e7c27a73 9492 struct type *t = die_containing_type (die, cu);
c906108c
SS
9493
9494 TYPE_VPTR_BASETYPE (type) = t;
9495 if (type == t)
9496 {
c906108c
SS
9497 int i;
9498
9499 /* Our own class provides vtbl ptr. */
9500 for (i = TYPE_NFIELDS (t) - 1;
9501 i >= TYPE_N_BASECLASSES (t);
9502 --i)
9503 {
0d5cff50 9504 const char *fieldname = TYPE_FIELD_NAME (t, i);
c906108c 9505
1168df01 9506 if (is_vtable_name (fieldname, cu))
c906108c
SS
9507 {
9508 TYPE_VPTR_FIELDNO (type) = i;
9509 break;
9510 }
9511 }
9512
9513 /* Complain if virtual function table field not found. */
9514 if (i < TYPE_N_BASECLASSES (t))
4d3c2250 9515 complaint (&symfile_complaints,
3e43a32a
MS
9516 _("virtual function table pointer "
9517 "not found when defining class '%s'"),
4d3c2250
KB
9518 TYPE_TAG_NAME (type) ? TYPE_TAG_NAME (type) :
9519 "");
c906108c
SS
9520 }
9521 else
9522 {
9523 TYPE_VPTR_FIELDNO (type) = TYPE_VPTR_FIELDNO (t);
9524 }
9525 }
f6235d4c
EZ
9526 else if (cu->producer
9527 && strncmp (cu->producer,
9528 "IBM(R) XL C/C++ Advanced Edition", 32) == 0)
9529 {
9530 /* The IBM XLC compiler does not provide direct indication
9531 of the containing type, but the vtable pointer is
9532 always named __vfp. */
9533
9534 int i;
9535
9536 for (i = TYPE_NFIELDS (type) - 1;
9537 i >= TYPE_N_BASECLASSES (type);
9538 --i)
9539 {
9540 if (strcmp (TYPE_FIELD_NAME (type, i), "__vfp") == 0)
9541 {
9542 TYPE_VPTR_FIELDNO (type) = i;
9543 TYPE_VPTR_BASETYPE (type) = type;
9544 break;
9545 }
9546 }
9547 }
c906108c 9548 }
98751a41
JK
9549
9550 /* Copy fi.typedef_field_list linked list elements content into the
9551 allocated array TYPE_TYPEDEF_FIELD_ARRAY (type). */
9552 if (fi.typedef_field_list)
9553 {
9554 int i = fi.typedef_field_list_count;
9555
a0d7a4ff 9556 ALLOCATE_CPLUS_STRUCT_TYPE (type);
98751a41
JK
9557 TYPE_TYPEDEF_FIELD_ARRAY (type)
9558 = TYPE_ALLOC (type, sizeof (TYPE_TYPEDEF_FIELD (type, 0)) * i);
9559 TYPE_TYPEDEF_FIELD_COUNT (type) = i;
9560
9561 /* Reverse the list order to keep the debug info elements order. */
9562 while (--i >= 0)
9563 {
9564 struct typedef_field *dest, *src;
6e70227d 9565
98751a41
JK
9566 dest = &TYPE_TYPEDEF_FIELD (type, i);
9567 src = &fi.typedef_field_list->field;
9568 fi.typedef_field_list = fi.typedef_field_list->next;
9569 *dest = *src;
9570 }
9571 }
c767944b
DJ
9572
9573 do_cleanups (back_to);
eb2a6f42
TT
9574
9575 if (HAVE_CPLUS_STRUCT (type))
9576 TYPE_CPLUS_REALLY_JAVA (type) = cu->language == language_java;
c906108c 9577 }
63d06c5c 9578
bb5ed363 9579 quirk_gcc_member_function_pointer (type, objfile);
0b92b5bb 9580
90aeadfc
DC
9581 /* NOTE: carlton/2004-03-16: GCC 3.4 (or at least one of its
9582 snapshots) has been known to create a die giving a declaration
9583 for a class that has, as a child, a die giving a definition for a
9584 nested class. So we have to process our children even if the
9585 current die is a declaration. Normally, of course, a declaration
9586 won't have any children at all. */
134d01f1 9587
90aeadfc
DC
9588 while (child_die != NULL && child_die->tag)
9589 {
9590 if (child_die->tag == DW_TAG_member
9591 || child_die->tag == DW_TAG_variable
34eaf542
TT
9592 || child_die->tag == DW_TAG_inheritance
9593 || child_die->tag == DW_TAG_template_value_param
9594 || child_die->tag == DW_TAG_template_type_param)
134d01f1 9595 {
90aeadfc 9596 /* Do nothing. */
134d01f1 9597 }
90aeadfc
DC
9598 else
9599 process_die (child_die, cu);
134d01f1 9600
90aeadfc 9601 child_die = sibling_die (child_die);
134d01f1
DJ
9602 }
9603
fa4028e9
JB
9604 /* Do not consider external references. According to the DWARF standard,
9605 these DIEs are identified by the fact that they have no byte_size
9606 attribute, and a declaration attribute. */
9607 if (dwarf2_attr (die, DW_AT_byte_size, cu) != NULL
9608 || !die_is_declaration (die, cu))
c767944b 9609 new_symbol (die, type, cu);
134d01f1
DJ
9610}
9611
9612/* Given a DW_AT_enumeration_type die, set its type. We do not
9613 complete the type's fields yet, or create any symbols. */
c906108c 9614
f792889a 9615static struct type *
134d01f1 9616read_enumeration_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 9617{
e7c27a73 9618 struct objfile *objfile = cu->objfile;
c906108c 9619 struct type *type;
c906108c 9620 struct attribute *attr;
0114d602 9621 const char *name;
134d01f1 9622
348e048f
DE
9623 /* If the definition of this type lives in .debug_types, read that type.
9624 Don't follow DW_AT_specification though, that will take us back up
9625 the chain and we want to go down. */
9626 attr = dwarf2_attr_no_follow (die, DW_AT_signature, cu);
9627 if (attr)
9628 {
9629 struct dwarf2_cu *type_cu = cu;
9630 struct die_info *type_die = follow_die_ref_or_sig (die, attr, &type_cu);
9a619af0 9631
348e048f 9632 type = read_type_die (type_die, type_cu);
9dc481d3
DE
9633
9634 /* TYPE_CU may not be the same as CU.
9635 Ensure TYPE is recorded in CU's type_hash table. */
348e048f
DE
9636 return set_die_type (die, type, cu);
9637 }
9638
c906108c
SS
9639 type = alloc_type (objfile);
9640
9641 TYPE_CODE (type) = TYPE_CODE_ENUM;
94af9270 9642 name = dwarf2_full_name (NULL, die, cu);
39cbfefa 9643 if (name != NULL)
0114d602 9644 TYPE_TAG_NAME (type) = (char *) name;
c906108c 9645
e142c38c 9646 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
c906108c
SS
9647 if (attr)
9648 {
9649 TYPE_LENGTH (type) = DW_UNSND (attr);
9650 }
9651 else
9652 {
9653 TYPE_LENGTH (type) = 0;
9654 }
9655
137033e9
JB
9656 /* The enumeration DIE can be incomplete. In Ada, any type can be
9657 declared as private in the package spec, and then defined only
9658 inside the package body. Such types are known as Taft Amendment
9659 Types. When another package uses such a type, an incomplete DIE
9660 may be generated by the compiler. */
02eb380e 9661 if (die_is_declaration (die, cu))
876cecd0 9662 TYPE_STUB (type) = 1;
02eb380e 9663
f792889a 9664 return set_die_type (die, type, cu);
134d01f1
DJ
9665}
9666
9667/* Given a pointer to a die which begins an enumeration, process all
9668 the dies that define the members of the enumeration, and create the
9669 symbol for the enumeration type.
9670
9671 NOTE: We reverse the order of the element list. */
9672
9673static void
9674process_enumeration_scope (struct die_info *die, struct dwarf2_cu *cu)
9675{
f792889a 9676 struct type *this_type;
134d01f1 9677
f792889a
DJ
9678 this_type = get_die_type (die, cu);
9679 if (this_type == NULL)
9680 this_type = read_enumeration_type (die, cu);
9dc481d3 9681
639d11d3 9682 if (die->child != NULL)
c906108c 9683 {
9dc481d3
DE
9684 struct die_info *child_die;
9685 struct symbol *sym;
9686 struct field *fields = NULL;
9687 int num_fields = 0;
9688 int unsigned_enum = 1;
9689 char *name;
cafec441
TT
9690 int flag_enum = 1;
9691 ULONGEST mask = 0;
9dc481d3 9692
639d11d3 9693 child_die = die->child;
c906108c
SS
9694 while (child_die && child_die->tag)
9695 {
9696 if (child_die->tag != DW_TAG_enumerator)
9697 {
e7c27a73 9698 process_die (child_die, cu);
c906108c
SS
9699 }
9700 else
9701 {
39cbfefa
DJ
9702 name = dwarf2_name (child_die, cu);
9703 if (name)
c906108c 9704 {
f792889a 9705 sym = new_symbol (child_die, this_type, cu);
c906108c 9706 if (SYMBOL_VALUE (sym) < 0)
cafec441
TT
9707 {
9708 unsigned_enum = 0;
9709 flag_enum = 0;
9710 }
9711 else if ((mask & SYMBOL_VALUE (sym)) != 0)
9712 flag_enum = 0;
9713 else
9714 mask |= SYMBOL_VALUE (sym);
c906108c
SS
9715
9716 if ((num_fields % DW_FIELD_ALLOC_CHUNK) == 0)
9717 {
9718 fields = (struct field *)
9719 xrealloc (fields,
9720 (num_fields + DW_FIELD_ALLOC_CHUNK)
c5aa993b 9721 * sizeof (struct field));
c906108c
SS
9722 }
9723
3567439c 9724 FIELD_NAME (fields[num_fields]) = SYMBOL_LINKAGE_NAME (sym);
c906108c 9725 FIELD_TYPE (fields[num_fields]) = NULL;
14e75d8e 9726 SET_FIELD_ENUMVAL (fields[num_fields], SYMBOL_VALUE (sym));
c906108c
SS
9727 FIELD_BITSIZE (fields[num_fields]) = 0;
9728
9729 num_fields++;
9730 }
9731 }
9732
9733 child_die = sibling_die (child_die);
9734 }
9735
9736 if (num_fields)
9737 {
f792889a
DJ
9738 TYPE_NFIELDS (this_type) = num_fields;
9739 TYPE_FIELDS (this_type) = (struct field *)
9740 TYPE_ALLOC (this_type, sizeof (struct field) * num_fields);
9741 memcpy (TYPE_FIELDS (this_type), fields,
c906108c 9742 sizeof (struct field) * num_fields);
b8c9b27d 9743 xfree (fields);
c906108c
SS
9744 }
9745 if (unsigned_enum)
876cecd0 9746 TYPE_UNSIGNED (this_type) = 1;
cafec441
TT
9747 if (flag_enum)
9748 TYPE_FLAG_ENUM (this_type) = 1;
c906108c 9749 }
134d01f1 9750
6c83ed52
TT
9751 /* If we are reading an enum from a .debug_types unit, and the enum
9752 is a declaration, and the enum is not the signatured type in the
9753 unit, then we do not want to add a symbol for it. Adding a
9754 symbol would in some cases obscure the true definition of the
9755 enum, giving users an incomplete type when the definition is
9756 actually available. Note that we do not want to do this for all
9757 enums which are just declarations, because C++0x allows forward
9758 enum declarations. */
3019eac3 9759 if (cu->per_cu->is_debug_types
6c83ed52
TT
9760 && die_is_declaration (die, cu))
9761 {
52dc124a 9762 struct signatured_type *sig_type;
6c83ed52 9763
52dc124a 9764 sig_type
6c83ed52 9765 = lookup_signatured_type_at_offset (dwarf2_per_objfile->objfile,
3019eac3 9766 cu->per_cu->info_or_types_section,
6c83ed52 9767 cu->per_cu->offset);
3019eac3
DE
9768 gdb_assert (sig_type->type_offset_in_section.sect_off != 0);
9769 if (sig_type->type_offset_in_section.sect_off != die->offset.sect_off)
6c83ed52
TT
9770 return;
9771 }
9772
f792889a 9773 new_symbol (die, this_type, cu);
c906108c
SS
9774}
9775
9776/* Extract all information from a DW_TAG_array_type DIE and put it in
9777 the DIE's type field. For now, this only handles one dimensional
9778 arrays. */
9779
f792889a 9780static struct type *
e7c27a73 9781read_array_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 9782{
e7c27a73 9783 struct objfile *objfile = cu->objfile;
c906108c 9784 struct die_info *child_die;
7e314c57 9785 struct type *type;
c906108c
SS
9786 struct type *element_type, *range_type, *index_type;
9787 struct type **range_types = NULL;
9788 struct attribute *attr;
9789 int ndim = 0;
9790 struct cleanup *back_to;
39cbfefa 9791 char *name;
c906108c 9792
e7c27a73 9793 element_type = die_type (die, cu);
c906108c 9794
7e314c57
JK
9795 /* The die_type call above may have already set the type for this DIE. */
9796 type = get_die_type (die, cu);
9797 if (type)
9798 return type;
9799
c906108c
SS
9800 /* Irix 6.2 native cc creates array types without children for
9801 arrays with unspecified length. */
639d11d3 9802 if (die->child == NULL)
c906108c 9803 {
46bf5051 9804 index_type = objfile_type (objfile)->builtin_int;
c906108c 9805 range_type = create_range_type (NULL, index_type, 0, -1);
f792889a
DJ
9806 type = create_array_type (NULL, element_type, range_type);
9807 return set_die_type (die, type, cu);
c906108c
SS
9808 }
9809
9810 back_to = make_cleanup (null_cleanup, NULL);
639d11d3 9811 child_die = die->child;
c906108c
SS
9812 while (child_die && child_die->tag)
9813 {
9814 if (child_die->tag == DW_TAG_subrange_type)
9815 {
f792889a 9816 struct type *child_type = read_type_die (child_die, cu);
9a619af0 9817
f792889a 9818 if (child_type != NULL)
a02abb62 9819 {
0963b4bd
MS
9820 /* The range type was succesfully read. Save it for the
9821 array type creation. */
a02abb62
JB
9822 if ((ndim % DW_FIELD_ALLOC_CHUNK) == 0)
9823 {
9824 range_types = (struct type **)
9825 xrealloc (range_types, (ndim + DW_FIELD_ALLOC_CHUNK)
9826 * sizeof (struct type *));
9827 if (ndim == 0)
9828 make_cleanup (free_current_contents, &range_types);
9829 }
f792889a 9830 range_types[ndim++] = child_type;
a02abb62 9831 }
c906108c
SS
9832 }
9833 child_die = sibling_die (child_die);
9834 }
9835
9836 /* Dwarf2 dimensions are output from left to right, create the
9837 necessary array types in backwards order. */
7ca2d3a3 9838
c906108c 9839 type = element_type;
7ca2d3a3
DL
9840
9841 if (read_array_order (die, cu) == DW_ORD_col_major)
9842 {
9843 int i = 0;
9a619af0 9844
7ca2d3a3
DL
9845 while (i < ndim)
9846 type = create_array_type (NULL, type, range_types[i++]);
9847 }
9848 else
9849 {
9850 while (ndim-- > 0)
9851 type = create_array_type (NULL, type, range_types[ndim]);
9852 }
c906108c 9853
f5f8a009
EZ
9854 /* Understand Dwarf2 support for vector types (like they occur on
9855 the PowerPC w/ AltiVec). Gcc just adds another attribute to the
9856 array type. This is not part of the Dwarf2/3 standard yet, but a
9857 custom vendor extension. The main difference between a regular
9858 array and the vector variant is that vectors are passed by value
9859 to functions. */
e142c38c 9860 attr = dwarf2_attr (die, DW_AT_GNU_vector, cu);
f5f8a009 9861 if (attr)
ea37ba09 9862 make_vector_type (type);
f5f8a009 9863
dbc98a8b
KW
9864 /* The DIE may have DW_AT_byte_size set. For example an OpenCL
9865 implementation may choose to implement triple vectors using this
9866 attribute. */
9867 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
9868 if (attr)
9869 {
9870 if (DW_UNSND (attr) >= TYPE_LENGTH (type))
9871 TYPE_LENGTH (type) = DW_UNSND (attr);
9872 else
3e43a32a
MS
9873 complaint (&symfile_complaints,
9874 _("DW_AT_byte_size for array type smaller "
9875 "than the total size of elements"));
dbc98a8b
KW
9876 }
9877
39cbfefa
DJ
9878 name = dwarf2_name (die, cu);
9879 if (name)
9880 TYPE_NAME (type) = name;
6e70227d 9881
0963b4bd 9882 /* Install the type in the die. */
7e314c57
JK
9883 set_die_type (die, type, cu);
9884
9885 /* set_die_type should be already done. */
b4ba55a1
JB
9886 set_descriptive_type (type, die, cu);
9887
c906108c
SS
9888 do_cleanups (back_to);
9889
7e314c57 9890 return type;
c906108c
SS
9891}
9892
7ca2d3a3 9893static enum dwarf_array_dim_ordering
6e70227d 9894read_array_order (struct die_info *die, struct dwarf2_cu *cu)
7ca2d3a3
DL
9895{
9896 struct attribute *attr;
9897
9898 attr = dwarf2_attr (die, DW_AT_ordering, cu);
9899
9900 if (attr) return DW_SND (attr);
9901
0963b4bd
MS
9902 /* GNU F77 is a special case, as at 08/2004 array type info is the
9903 opposite order to the dwarf2 specification, but data is still
9904 laid out as per normal fortran.
7ca2d3a3 9905
0963b4bd
MS
9906 FIXME: dsl/2004-8-20: If G77 is ever fixed, this will also need
9907 version checking. */
7ca2d3a3 9908
905e0470
PM
9909 if (cu->language == language_fortran
9910 && cu->producer && strstr (cu->producer, "GNU F77"))
7ca2d3a3
DL
9911 {
9912 return DW_ORD_row_major;
9913 }
9914
6e70227d 9915 switch (cu->language_defn->la_array_ordering)
7ca2d3a3
DL
9916 {
9917 case array_column_major:
9918 return DW_ORD_col_major;
9919 case array_row_major:
9920 default:
9921 return DW_ORD_row_major;
9922 };
9923}
9924
72019c9c 9925/* Extract all information from a DW_TAG_set_type DIE and put it in
0963b4bd 9926 the DIE's type field. */
72019c9c 9927
f792889a 9928static struct type *
72019c9c
GM
9929read_set_type (struct die_info *die, struct dwarf2_cu *cu)
9930{
7e314c57
JK
9931 struct type *domain_type, *set_type;
9932 struct attribute *attr;
f792889a 9933
7e314c57
JK
9934 domain_type = die_type (die, cu);
9935
9936 /* The die_type call above may have already set the type for this DIE. */
9937 set_type = get_die_type (die, cu);
9938 if (set_type)
9939 return set_type;
9940
9941 set_type = create_set_type (NULL, domain_type);
9942
9943 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
d09039dd
PM
9944 if (attr)
9945 TYPE_LENGTH (set_type) = DW_UNSND (attr);
7e314c57 9946
f792889a 9947 return set_die_type (die, set_type, cu);
72019c9c 9948}
7ca2d3a3 9949
c906108c
SS
9950/* First cut: install each common block member as a global variable. */
9951
9952static void
e7c27a73 9953read_common_block (struct die_info *die, struct dwarf2_cu *cu)
c906108c
SS
9954{
9955 struct die_info *child_die;
9956 struct attribute *attr;
9957 struct symbol *sym;
9958 CORE_ADDR base = (CORE_ADDR) 0;
9959
e142c38c 9960 attr = dwarf2_attr (die, DW_AT_location, cu);
c906108c
SS
9961 if (attr)
9962 {
0963b4bd 9963 /* Support the .debug_loc offsets. */
8e19ed76
PS
9964 if (attr_form_is_block (attr))
9965 {
e7c27a73 9966 base = decode_locdesc (DW_BLOCK (attr), cu);
8e19ed76 9967 }
3690dd37 9968 else if (attr_form_is_section_offset (attr))
8e19ed76 9969 {
4d3c2250 9970 dwarf2_complex_location_expr_complaint ();
8e19ed76
PS
9971 }
9972 else
9973 {
4d3c2250
KB
9974 dwarf2_invalid_attrib_class_complaint ("DW_AT_location",
9975 "common block member");
8e19ed76 9976 }
c906108c 9977 }
639d11d3 9978 if (die->child != NULL)
c906108c 9979 {
639d11d3 9980 child_die = die->child;
c906108c
SS
9981 while (child_die && child_die->tag)
9982 {
74ac6d43
TT
9983 LONGEST offset;
9984
e7c27a73 9985 sym = new_symbol (child_die, NULL, cu);
e8d28ef4
TT
9986 if (sym != NULL
9987 && handle_data_member_location (child_die, cu, &offset))
c906108c 9988 {
74ac6d43 9989 SYMBOL_VALUE_ADDRESS (sym) = base + offset;
c906108c
SS
9990 add_symbol_to_list (sym, &global_symbols);
9991 }
9992 child_die = sibling_die (child_die);
9993 }
9994 }
9995}
9996
0114d602 9997/* Create a type for a C++ namespace. */
d9fa45fe 9998
0114d602
DJ
9999static struct type *
10000read_namespace_type (struct die_info *die, struct dwarf2_cu *cu)
d9fa45fe 10001{
e7c27a73 10002 struct objfile *objfile = cu->objfile;
0114d602 10003 const char *previous_prefix, *name;
9219021c 10004 int is_anonymous;
0114d602
DJ
10005 struct type *type;
10006
10007 /* For extensions, reuse the type of the original namespace. */
10008 if (dwarf2_attr (die, DW_AT_extension, cu) != NULL)
10009 {
10010 struct die_info *ext_die;
10011 struct dwarf2_cu *ext_cu = cu;
9a619af0 10012
0114d602
DJ
10013 ext_die = dwarf2_extension (die, &ext_cu);
10014 type = read_type_die (ext_die, ext_cu);
9dc481d3
DE
10015
10016 /* EXT_CU may not be the same as CU.
10017 Ensure TYPE is recorded in CU's type_hash table. */
0114d602
DJ
10018 return set_die_type (die, type, cu);
10019 }
9219021c 10020
e142c38c 10021 name = namespace_name (die, &is_anonymous, cu);
9219021c
DC
10022
10023 /* Now build the name of the current namespace. */
10024
0114d602
DJ
10025 previous_prefix = determine_prefix (die, cu);
10026 if (previous_prefix[0] != '\0')
10027 name = typename_concat (&objfile->objfile_obstack,
f55ee35c 10028 previous_prefix, name, 0, cu);
0114d602
DJ
10029
10030 /* Create the type. */
10031 type = init_type (TYPE_CODE_NAMESPACE, 0, 0, NULL,
10032 objfile);
10033 TYPE_NAME (type) = (char *) name;
10034 TYPE_TAG_NAME (type) = TYPE_NAME (type);
10035
60531b24 10036 return set_die_type (die, type, cu);
0114d602
DJ
10037}
10038
10039/* Read a C++ namespace. */
10040
10041static void
10042read_namespace (struct die_info *die, struct dwarf2_cu *cu)
10043{
10044 struct objfile *objfile = cu->objfile;
0114d602 10045 int is_anonymous;
9219021c 10046
5c4e30ca
DC
10047 /* Add a symbol associated to this if we haven't seen the namespace
10048 before. Also, add a using directive if it's an anonymous
10049 namespace. */
9219021c 10050
f2f0e013 10051 if (dwarf2_attr (die, DW_AT_extension, cu) == NULL)
5c4e30ca
DC
10052 {
10053 struct type *type;
10054
0114d602 10055 type = read_type_die (die, cu);
e7c27a73 10056 new_symbol (die, type, cu);
5c4e30ca 10057
e8e80198 10058 namespace_name (die, &is_anonymous, cu);
5c4e30ca 10059 if (is_anonymous)
0114d602
DJ
10060 {
10061 const char *previous_prefix = determine_prefix (die, cu);
9a619af0 10062
c0cc3a76 10063 cp_add_using_directive (previous_prefix, TYPE_NAME (type), NULL,
32019081 10064 NULL, NULL, &objfile->objfile_obstack);
0114d602 10065 }
5c4e30ca 10066 }
9219021c 10067
639d11d3 10068 if (die->child != NULL)
d9fa45fe 10069 {
639d11d3 10070 struct die_info *child_die = die->child;
6e70227d 10071
d9fa45fe
DC
10072 while (child_die && child_die->tag)
10073 {
e7c27a73 10074 process_die (child_die, cu);
d9fa45fe
DC
10075 child_die = sibling_die (child_die);
10076 }
10077 }
38d518c9
EZ
10078}
10079
f55ee35c
JK
10080/* Read a Fortran module as type. This DIE can be only a declaration used for
10081 imported module. Still we need that type as local Fortran "use ... only"
10082 declaration imports depend on the created type in determine_prefix. */
10083
10084static struct type *
10085read_module_type (struct die_info *die, struct dwarf2_cu *cu)
10086{
10087 struct objfile *objfile = cu->objfile;
10088 char *module_name;
10089 struct type *type;
10090
10091 module_name = dwarf2_name (die, cu);
10092 if (!module_name)
3e43a32a
MS
10093 complaint (&symfile_complaints,
10094 _("DW_TAG_module has no name, offset 0x%x"),
b64f50a1 10095 die->offset.sect_off);
f55ee35c
JK
10096 type = init_type (TYPE_CODE_MODULE, 0, 0, module_name, objfile);
10097
10098 /* determine_prefix uses TYPE_TAG_NAME. */
10099 TYPE_TAG_NAME (type) = TYPE_NAME (type);
10100
10101 return set_die_type (die, type, cu);
10102}
10103
5d7cb8df
JK
10104/* Read a Fortran module. */
10105
10106static void
10107read_module (struct die_info *die, struct dwarf2_cu *cu)
10108{
10109 struct die_info *child_die = die->child;
10110
5d7cb8df
JK
10111 while (child_die && child_die->tag)
10112 {
10113 process_die (child_die, cu);
10114 child_die = sibling_die (child_die);
10115 }
10116}
10117
38d518c9
EZ
10118/* Return the name of the namespace represented by DIE. Set
10119 *IS_ANONYMOUS to tell whether or not the namespace is an anonymous
10120 namespace. */
10121
10122static const char *
e142c38c 10123namespace_name (struct die_info *die, int *is_anonymous, struct dwarf2_cu *cu)
38d518c9
EZ
10124{
10125 struct die_info *current_die;
10126 const char *name = NULL;
10127
10128 /* Loop through the extensions until we find a name. */
10129
10130 for (current_die = die;
10131 current_die != NULL;
f2f0e013 10132 current_die = dwarf2_extension (die, &cu))
38d518c9 10133 {
e142c38c 10134 name = dwarf2_name (current_die, cu);
38d518c9
EZ
10135 if (name != NULL)
10136 break;
10137 }
10138
10139 /* Is it an anonymous namespace? */
10140
10141 *is_anonymous = (name == NULL);
10142 if (*is_anonymous)
2b1dbab0 10143 name = CP_ANONYMOUS_NAMESPACE_STR;
38d518c9
EZ
10144
10145 return name;
d9fa45fe
DC
10146}
10147
c906108c
SS
10148/* Extract all information from a DW_TAG_pointer_type DIE and add to
10149 the user defined type vector. */
10150
f792889a 10151static struct type *
e7c27a73 10152read_tag_pointer_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 10153{
5e2b427d 10154 struct gdbarch *gdbarch = get_objfile_arch (cu->objfile);
e7c27a73 10155 struct comp_unit_head *cu_header = &cu->header;
c906108c 10156 struct type *type;
8b2dbe47
KB
10157 struct attribute *attr_byte_size;
10158 struct attribute *attr_address_class;
10159 int byte_size, addr_class;
7e314c57
JK
10160 struct type *target_type;
10161
10162 target_type = die_type (die, cu);
c906108c 10163
7e314c57
JK
10164 /* The die_type call above may have already set the type for this DIE. */
10165 type = get_die_type (die, cu);
10166 if (type)
10167 return type;
10168
10169 type = lookup_pointer_type (target_type);
8b2dbe47 10170
e142c38c 10171 attr_byte_size = dwarf2_attr (die, DW_AT_byte_size, cu);
8b2dbe47
KB
10172 if (attr_byte_size)
10173 byte_size = DW_UNSND (attr_byte_size);
c906108c 10174 else
8b2dbe47
KB
10175 byte_size = cu_header->addr_size;
10176
e142c38c 10177 attr_address_class = dwarf2_attr (die, DW_AT_address_class, cu);
8b2dbe47
KB
10178 if (attr_address_class)
10179 addr_class = DW_UNSND (attr_address_class);
10180 else
10181 addr_class = DW_ADDR_none;
10182
10183 /* If the pointer size or address class is different than the
10184 default, create a type variant marked as such and set the
10185 length accordingly. */
10186 if (TYPE_LENGTH (type) != byte_size || addr_class != DW_ADDR_none)
c906108c 10187 {
5e2b427d 10188 if (gdbarch_address_class_type_flags_p (gdbarch))
8b2dbe47
KB
10189 {
10190 int type_flags;
10191
849957d9 10192 type_flags = gdbarch_address_class_type_flags
5e2b427d 10193 (gdbarch, byte_size, addr_class);
876cecd0
TT
10194 gdb_assert ((type_flags & ~TYPE_INSTANCE_FLAG_ADDRESS_CLASS_ALL)
10195 == 0);
8b2dbe47
KB
10196 type = make_type_with_address_space (type, type_flags);
10197 }
10198 else if (TYPE_LENGTH (type) != byte_size)
10199 {
3e43a32a
MS
10200 complaint (&symfile_complaints,
10201 _("invalid pointer size %d"), byte_size);
8b2dbe47 10202 }
6e70227d 10203 else
9a619af0
MS
10204 {
10205 /* Should we also complain about unhandled address classes? */
10206 }
c906108c 10207 }
8b2dbe47
KB
10208
10209 TYPE_LENGTH (type) = byte_size;
f792889a 10210 return set_die_type (die, type, cu);
c906108c
SS
10211}
10212
10213/* Extract all information from a DW_TAG_ptr_to_member_type DIE and add to
10214 the user defined type vector. */
10215
f792889a 10216static struct type *
e7c27a73 10217read_tag_ptr_to_member_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c
SS
10218{
10219 struct type *type;
10220 struct type *to_type;
10221 struct type *domain;
10222
e7c27a73
DJ
10223 to_type = die_type (die, cu);
10224 domain = die_containing_type (die, cu);
0d5de010 10225
7e314c57
JK
10226 /* The calls above may have already set the type for this DIE. */
10227 type = get_die_type (die, cu);
10228 if (type)
10229 return type;
10230
0d5de010
DJ
10231 if (TYPE_CODE (check_typedef (to_type)) == TYPE_CODE_METHOD)
10232 type = lookup_methodptr_type (to_type);
10233 else
10234 type = lookup_memberptr_type (to_type, domain);
c906108c 10235
f792889a 10236 return set_die_type (die, type, cu);
c906108c
SS
10237}
10238
10239/* Extract all information from a DW_TAG_reference_type DIE and add to
10240 the user defined type vector. */
10241
f792889a 10242static struct type *
e7c27a73 10243read_tag_reference_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 10244{
e7c27a73 10245 struct comp_unit_head *cu_header = &cu->header;
7e314c57 10246 struct type *type, *target_type;
c906108c
SS
10247 struct attribute *attr;
10248
7e314c57
JK
10249 target_type = die_type (die, cu);
10250
10251 /* The die_type call above may have already set the type for this DIE. */
10252 type = get_die_type (die, cu);
10253 if (type)
10254 return type;
10255
10256 type = lookup_reference_type (target_type);
e142c38c 10257 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
c906108c
SS
10258 if (attr)
10259 {
10260 TYPE_LENGTH (type) = DW_UNSND (attr);
10261 }
10262 else
10263 {
107d2387 10264 TYPE_LENGTH (type) = cu_header->addr_size;
c906108c 10265 }
f792889a 10266 return set_die_type (die, type, cu);
c906108c
SS
10267}
10268
f792889a 10269static struct type *
e7c27a73 10270read_tag_const_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 10271{
f792889a 10272 struct type *base_type, *cv_type;
c906108c 10273
e7c27a73 10274 base_type = die_type (die, cu);
7e314c57
JK
10275
10276 /* The die_type call above may have already set the type for this DIE. */
10277 cv_type = get_die_type (die, cu);
10278 if (cv_type)
10279 return cv_type;
10280
2f608a3a
KW
10281 /* In case the const qualifier is applied to an array type, the element type
10282 is so qualified, not the array type (section 6.7.3 of C99). */
10283 if (TYPE_CODE (base_type) == TYPE_CODE_ARRAY)
10284 {
10285 struct type *el_type, *inner_array;
10286
10287 base_type = copy_type (base_type);
10288 inner_array = base_type;
10289
10290 while (TYPE_CODE (TYPE_TARGET_TYPE (inner_array)) == TYPE_CODE_ARRAY)
10291 {
10292 TYPE_TARGET_TYPE (inner_array) =
10293 copy_type (TYPE_TARGET_TYPE (inner_array));
10294 inner_array = TYPE_TARGET_TYPE (inner_array);
10295 }
10296
10297 el_type = TYPE_TARGET_TYPE (inner_array);
10298 TYPE_TARGET_TYPE (inner_array) =
10299 make_cv_type (1, TYPE_VOLATILE (el_type), el_type, NULL);
10300
10301 return set_die_type (die, base_type, cu);
10302 }
10303
f792889a
DJ
10304 cv_type = make_cv_type (1, TYPE_VOLATILE (base_type), base_type, 0);
10305 return set_die_type (die, cv_type, cu);
c906108c
SS
10306}
10307
f792889a 10308static struct type *
e7c27a73 10309read_tag_volatile_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 10310{
f792889a 10311 struct type *base_type, *cv_type;
c906108c 10312
e7c27a73 10313 base_type = die_type (die, cu);
7e314c57
JK
10314
10315 /* The die_type call above may have already set the type for this DIE. */
10316 cv_type = get_die_type (die, cu);
10317 if (cv_type)
10318 return cv_type;
10319
f792889a
DJ
10320 cv_type = make_cv_type (TYPE_CONST (base_type), 1, base_type, 0);
10321 return set_die_type (die, cv_type, cu);
c906108c
SS
10322}
10323
10324/* Extract all information from a DW_TAG_string_type DIE and add to
10325 the user defined type vector. It isn't really a user defined type,
10326 but it behaves like one, with other DIE's using an AT_user_def_type
10327 attribute to reference it. */
10328
f792889a 10329static struct type *
e7c27a73 10330read_tag_string_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 10331{
e7c27a73 10332 struct objfile *objfile = cu->objfile;
3b7538c0 10333 struct gdbarch *gdbarch = get_objfile_arch (objfile);
c906108c
SS
10334 struct type *type, *range_type, *index_type, *char_type;
10335 struct attribute *attr;
10336 unsigned int length;
10337
e142c38c 10338 attr = dwarf2_attr (die, DW_AT_string_length, cu);
c906108c
SS
10339 if (attr)
10340 {
10341 length = DW_UNSND (attr);
10342 }
10343 else
10344 {
0963b4bd 10345 /* Check for the DW_AT_byte_size attribute. */
e142c38c 10346 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
b21b22e0
PS
10347 if (attr)
10348 {
10349 length = DW_UNSND (attr);
10350 }
10351 else
10352 {
10353 length = 1;
10354 }
c906108c 10355 }
6ccb9162 10356
46bf5051 10357 index_type = objfile_type (objfile)->builtin_int;
c906108c 10358 range_type = create_range_type (NULL, index_type, 1, length);
3b7538c0
UW
10359 char_type = language_string_char_type (cu->language_defn, gdbarch);
10360 type = create_string_type (NULL, char_type, range_type);
6ccb9162 10361
f792889a 10362 return set_die_type (die, type, cu);
c906108c
SS
10363}
10364
10365/* Handle DIES due to C code like:
10366
10367 struct foo
c5aa993b
JM
10368 {
10369 int (*funcp)(int a, long l);
10370 int b;
10371 };
c906108c 10372
0963b4bd 10373 ('funcp' generates a DW_TAG_subroutine_type DIE). */
c906108c 10374
f792889a 10375static struct type *
e7c27a73 10376read_subroutine_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 10377{
bb5ed363 10378 struct objfile *objfile = cu->objfile;
0963b4bd
MS
10379 struct type *type; /* Type that this function returns. */
10380 struct type *ftype; /* Function that returns above type. */
c906108c
SS
10381 struct attribute *attr;
10382
e7c27a73 10383 type = die_type (die, cu);
7e314c57
JK
10384
10385 /* The die_type call above may have already set the type for this DIE. */
10386 ftype = get_die_type (die, cu);
10387 if (ftype)
10388 return ftype;
10389
0c8b41f1 10390 ftype = lookup_function_type (type);
c906108c 10391
5b8101ae 10392 /* All functions in C++, Pascal and Java have prototypes. */
e142c38c 10393 attr = dwarf2_attr (die, DW_AT_prototyped, cu);
c906108c 10394 if ((attr && (DW_UNSND (attr) != 0))
987504bb 10395 || cu->language == language_cplus
5b8101ae
PM
10396 || cu->language == language_java
10397 || cu->language == language_pascal)
876cecd0 10398 TYPE_PROTOTYPED (ftype) = 1;
a6c727b2
DJ
10399 else if (producer_is_realview (cu->producer))
10400 /* RealView does not emit DW_AT_prototyped. We can not
10401 distinguish prototyped and unprototyped functions; default to
10402 prototyped, since that is more common in modern code (and
10403 RealView warns about unprototyped functions). */
10404 TYPE_PROTOTYPED (ftype) = 1;
c906108c 10405
c055b101
CV
10406 /* Store the calling convention in the type if it's available in
10407 the subroutine die. Otherwise set the calling convention to
10408 the default value DW_CC_normal. */
10409 attr = dwarf2_attr (die, DW_AT_calling_convention, cu);
54fcddd0
UW
10410 if (attr)
10411 TYPE_CALLING_CONVENTION (ftype) = DW_UNSND (attr);
10412 else if (cu->producer && strstr (cu->producer, "IBM XL C for OpenCL"))
10413 TYPE_CALLING_CONVENTION (ftype) = DW_CC_GDB_IBM_OpenCL;
10414 else
10415 TYPE_CALLING_CONVENTION (ftype) = DW_CC_normal;
76c10ea2
GM
10416
10417 /* We need to add the subroutine type to the die immediately so
10418 we don't infinitely recurse when dealing with parameters
0963b4bd 10419 declared as the same subroutine type. */
76c10ea2 10420 set_die_type (die, ftype, cu);
6e70227d 10421
639d11d3 10422 if (die->child != NULL)
c906108c 10423 {
bb5ed363 10424 struct type *void_type = objfile_type (objfile)->builtin_void;
c906108c 10425 struct die_info *child_die;
8072405b 10426 int nparams, iparams;
c906108c
SS
10427
10428 /* Count the number of parameters.
10429 FIXME: GDB currently ignores vararg functions, but knows about
10430 vararg member functions. */
8072405b 10431 nparams = 0;
639d11d3 10432 child_die = die->child;
c906108c
SS
10433 while (child_die && child_die->tag)
10434 {
10435 if (child_die->tag == DW_TAG_formal_parameter)
10436 nparams++;
10437 else if (child_die->tag == DW_TAG_unspecified_parameters)
876cecd0 10438 TYPE_VARARGS (ftype) = 1;
c906108c
SS
10439 child_die = sibling_die (child_die);
10440 }
10441
10442 /* Allocate storage for parameters and fill them in. */
10443 TYPE_NFIELDS (ftype) = nparams;
10444 TYPE_FIELDS (ftype) = (struct field *)
ae5a43e0 10445 TYPE_ZALLOC (ftype, nparams * sizeof (struct field));
c906108c 10446
8072405b
JK
10447 /* TYPE_FIELD_TYPE must never be NULL. Pre-fill the array to ensure it
10448 even if we error out during the parameters reading below. */
10449 for (iparams = 0; iparams < nparams; iparams++)
10450 TYPE_FIELD_TYPE (ftype, iparams) = void_type;
10451
10452 iparams = 0;
639d11d3 10453 child_die = die->child;
c906108c
SS
10454 while (child_die && child_die->tag)
10455 {
10456 if (child_die->tag == DW_TAG_formal_parameter)
10457 {
3ce3b1ba
PA
10458 struct type *arg_type;
10459
10460 /* DWARF version 2 has no clean way to discern C++
10461 static and non-static member functions. G++ helps
10462 GDB by marking the first parameter for non-static
10463 member functions (which is the this pointer) as
10464 artificial. We pass this information to
10465 dwarf2_add_member_fn via TYPE_FIELD_ARTIFICIAL.
10466
10467 DWARF version 3 added DW_AT_object_pointer, which GCC
10468 4.5 does not yet generate. */
e142c38c 10469 attr = dwarf2_attr (child_die, DW_AT_artificial, cu);
c906108c
SS
10470 if (attr)
10471 TYPE_FIELD_ARTIFICIAL (ftype, iparams) = DW_UNSND (attr);
10472 else
418835cc
KS
10473 {
10474 TYPE_FIELD_ARTIFICIAL (ftype, iparams) = 0;
10475
10476 /* GCC/43521: In java, the formal parameter
10477 "this" is sometimes not marked with DW_AT_artificial. */
10478 if (cu->language == language_java)
10479 {
10480 const char *name = dwarf2_name (child_die, cu);
9a619af0 10481
418835cc
KS
10482 if (name && !strcmp (name, "this"))
10483 TYPE_FIELD_ARTIFICIAL (ftype, iparams) = 1;
10484 }
10485 }
3ce3b1ba
PA
10486 arg_type = die_type (child_die, cu);
10487
10488 /* RealView does not mark THIS as const, which the testsuite
10489 expects. GCC marks THIS as const in method definitions,
10490 but not in the class specifications (GCC PR 43053). */
10491 if (cu->language == language_cplus && !TYPE_CONST (arg_type)
10492 && TYPE_FIELD_ARTIFICIAL (ftype, iparams))
10493 {
10494 int is_this = 0;
10495 struct dwarf2_cu *arg_cu = cu;
10496 const char *name = dwarf2_name (child_die, cu);
10497
10498 attr = dwarf2_attr (die, DW_AT_object_pointer, cu);
10499 if (attr)
10500 {
10501 /* If the compiler emits this, use it. */
10502 if (follow_die_ref (die, attr, &arg_cu) == child_die)
10503 is_this = 1;
10504 }
10505 else if (name && strcmp (name, "this") == 0)
10506 /* Function definitions will have the argument names. */
10507 is_this = 1;
10508 else if (name == NULL && iparams == 0)
10509 /* Declarations may not have the names, so like
10510 elsewhere in GDB, assume an artificial first
10511 argument is "this". */
10512 is_this = 1;
10513
10514 if (is_this)
10515 arg_type = make_cv_type (1, TYPE_VOLATILE (arg_type),
10516 arg_type, 0);
10517 }
10518
10519 TYPE_FIELD_TYPE (ftype, iparams) = arg_type;
c906108c
SS
10520 iparams++;
10521 }
10522 child_die = sibling_die (child_die);
10523 }
10524 }
10525
76c10ea2 10526 return ftype;
c906108c
SS
10527}
10528
f792889a 10529static struct type *
e7c27a73 10530read_typedef (struct die_info *die, struct dwarf2_cu *cu)
c906108c 10531{
e7c27a73 10532 struct objfile *objfile = cu->objfile;
0114d602 10533 const char *name = NULL;
3c8e0968 10534 struct type *this_type, *target_type;
c906108c 10535
94af9270 10536 name = dwarf2_full_name (NULL, die, cu);
f792889a 10537 this_type = init_type (TYPE_CODE_TYPEDEF, 0,
0114d602
DJ
10538 TYPE_FLAG_TARGET_STUB, NULL, objfile);
10539 TYPE_NAME (this_type) = (char *) name;
f792889a 10540 set_die_type (die, this_type, cu);
3c8e0968
DE
10541 target_type = die_type (die, cu);
10542 if (target_type != this_type)
10543 TYPE_TARGET_TYPE (this_type) = target_type;
10544 else
10545 {
10546 /* Self-referential typedefs are, it seems, not allowed by the DWARF
10547 spec and cause infinite loops in GDB. */
10548 complaint (&symfile_complaints,
10549 _("Self-referential DW_TAG_typedef "
10550 "- DIE at 0x%x [in module %s]"),
b64f50a1 10551 die->offset.sect_off, objfile->name);
3c8e0968
DE
10552 TYPE_TARGET_TYPE (this_type) = NULL;
10553 }
f792889a 10554 return this_type;
c906108c
SS
10555}
10556
10557/* Find a representation of a given base type and install
10558 it in the TYPE field of the die. */
10559
f792889a 10560static struct type *
e7c27a73 10561read_base_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 10562{
e7c27a73 10563 struct objfile *objfile = cu->objfile;
c906108c
SS
10564 struct type *type;
10565 struct attribute *attr;
10566 int encoding = 0, size = 0;
39cbfefa 10567 char *name;
6ccb9162
UW
10568 enum type_code code = TYPE_CODE_INT;
10569 int type_flags = 0;
10570 struct type *target_type = NULL;
c906108c 10571
e142c38c 10572 attr = dwarf2_attr (die, DW_AT_encoding, cu);
c906108c
SS
10573 if (attr)
10574 {
10575 encoding = DW_UNSND (attr);
10576 }
e142c38c 10577 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
c906108c
SS
10578 if (attr)
10579 {
10580 size = DW_UNSND (attr);
10581 }
39cbfefa 10582 name = dwarf2_name (die, cu);
6ccb9162 10583 if (!name)
c906108c 10584 {
6ccb9162
UW
10585 complaint (&symfile_complaints,
10586 _("DW_AT_name missing from DW_TAG_base_type"));
c906108c 10587 }
6ccb9162
UW
10588
10589 switch (encoding)
c906108c 10590 {
6ccb9162
UW
10591 case DW_ATE_address:
10592 /* Turn DW_ATE_address into a void * pointer. */
10593 code = TYPE_CODE_PTR;
10594 type_flags |= TYPE_FLAG_UNSIGNED;
10595 target_type = init_type (TYPE_CODE_VOID, 1, 0, NULL, objfile);
10596 break;
10597 case DW_ATE_boolean:
10598 code = TYPE_CODE_BOOL;
10599 type_flags |= TYPE_FLAG_UNSIGNED;
10600 break;
10601 case DW_ATE_complex_float:
10602 code = TYPE_CODE_COMPLEX;
10603 target_type = init_type (TYPE_CODE_FLT, size / 2, 0, NULL, objfile);
10604 break;
10605 case DW_ATE_decimal_float:
10606 code = TYPE_CODE_DECFLOAT;
10607 break;
10608 case DW_ATE_float:
10609 code = TYPE_CODE_FLT;
10610 break;
10611 case DW_ATE_signed:
10612 break;
10613 case DW_ATE_unsigned:
10614 type_flags |= TYPE_FLAG_UNSIGNED;
3b2b8fea
TT
10615 if (cu->language == language_fortran
10616 && name
10617 && strncmp (name, "character(", sizeof ("character(") - 1) == 0)
10618 code = TYPE_CODE_CHAR;
6ccb9162
UW
10619 break;
10620 case DW_ATE_signed_char:
6e70227d 10621 if (cu->language == language_ada || cu->language == language_m2
3b2b8fea
TT
10622 || cu->language == language_pascal
10623 || cu->language == language_fortran)
6ccb9162
UW
10624 code = TYPE_CODE_CHAR;
10625 break;
10626 case DW_ATE_unsigned_char:
868a0084 10627 if (cu->language == language_ada || cu->language == language_m2
3b2b8fea
TT
10628 || cu->language == language_pascal
10629 || cu->language == language_fortran)
6ccb9162
UW
10630 code = TYPE_CODE_CHAR;
10631 type_flags |= TYPE_FLAG_UNSIGNED;
10632 break;
75079b2b
TT
10633 case DW_ATE_UTF:
10634 /* We just treat this as an integer and then recognize the
10635 type by name elsewhere. */
10636 break;
10637
6ccb9162
UW
10638 default:
10639 complaint (&symfile_complaints, _("unsupported DW_AT_encoding: '%s'"),
10640 dwarf_type_encoding_name (encoding));
10641 break;
c906108c 10642 }
6ccb9162 10643
0114d602
DJ
10644 type = init_type (code, size, type_flags, NULL, objfile);
10645 TYPE_NAME (type) = name;
6ccb9162
UW
10646 TYPE_TARGET_TYPE (type) = target_type;
10647
0114d602 10648 if (name && strcmp (name, "char") == 0)
876cecd0 10649 TYPE_NOSIGN (type) = 1;
0114d602 10650
f792889a 10651 return set_die_type (die, type, cu);
c906108c
SS
10652}
10653
a02abb62
JB
10654/* Read the given DW_AT_subrange DIE. */
10655
f792889a 10656static struct type *
a02abb62
JB
10657read_subrange_type (struct die_info *die, struct dwarf2_cu *cu)
10658{
10659 struct type *base_type;
10660 struct type *range_type;
10661 struct attribute *attr;
4fae6e18
JK
10662 LONGEST low, high;
10663 int low_default_is_valid;
39cbfefa 10664 char *name;
43bbcdc2 10665 LONGEST negative_mask;
e77813c8 10666
a02abb62 10667 base_type = die_type (die, cu);
953ac07e
JK
10668 /* Preserve BASE_TYPE's original type, just set its LENGTH. */
10669 check_typedef (base_type);
a02abb62 10670
7e314c57
JK
10671 /* The die_type call above may have already set the type for this DIE. */
10672 range_type = get_die_type (die, cu);
10673 if (range_type)
10674 return range_type;
10675
4fae6e18
JK
10676 /* Set LOW_DEFAULT_IS_VALID if current language and DWARF version allow
10677 omitting DW_AT_lower_bound. */
10678 switch (cu->language)
6e70227d 10679 {
4fae6e18
JK
10680 case language_c:
10681 case language_cplus:
10682 low = 0;
10683 low_default_is_valid = 1;
10684 break;
10685 case language_fortran:
10686 low = 1;
10687 low_default_is_valid = 1;
10688 break;
10689 case language_d:
10690 case language_java:
10691 case language_objc:
10692 low = 0;
10693 low_default_is_valid = (cu->header.version >= 4);
10694 break;
10695 case language_ada:
10696 case language_m2:
10697 case language_pascal:
a02abb62 10698 low = 1;
4fae6e18
JK
10699 low_default_is_valid = (cu->header.version >= 4);
10700 break;
10701 default:
10702 low = 0;
10703 low_default_is_valid = 0;
10704 break;
a02abb62
JB
10705 }
10706
dd5e6932
DJ
10707 /* FIXME: For variable sized arrays either of these could be
10708 a variable rather than a constant value. We'll allow it,
10709 but we don't know how to handle it. */
e142c38c 10710 attr = dwarf2_attr (die, DW_AT_lower_bound, cu);
a02abb62 10711 if (attr)
4fae6e18
JK
10712 low = dwarf2_get_attr_constant_value (attr, low);
10713 else if (!low_default_is_valid)
10714 complaint (&symfile_complaints, _("Missing DW_AT_lower_bound "
10715 "- DIE at 0x%x [in module %s]"),
10716 die->offset.sect_off, cu->objfile->name);
a02abb62 10717
e142c38c 10718 attr = dwarf2_attr (die, DW_AT_upper_bound, cu);
a02abb62 10719 if (attr)
6e70227d 10720 {
d48323d8 10721 if (attr_form_is_block (attr) || is_ref_attr (attr))
a02abb62
JB
10722 {
10723 /* GCC encodes arrays with unspecified or dynamic length
e77813c8 10724 with a DW_FORM_block1 attribute or a reference attribute.
a02abb62
JB
10725 FIXME: GDB does not yet know how to handle dynamic
10726 arrays properly, treat them as arrays with unspecified
10727 length for now.
10728
10729 FIXME: jimb/2003-09-22: GDB does not really know
10730 how to handle arrays of unspecified length
10731 either; we just represent them as zero-length
10732 arrays. Choose an appropriate upper bound given
10733 the lower bound we've computed above. */
10734 high = low - 1;
10735 }
10736 else
10737 high = dwarf2_get_attr_constant_value (attr, 1);
10738 }
e77813c8
PM
10739 else
10740 {
10741 attr = dwarf2_attr (die, DW_AT_count, cu);
10742 if (attr)
10743 {
10744 int count = dwarf2_get_attr_constant_value (attr, 1);
10745 high = low + count - 1;
10746 }
c2ff108b
JK
10747 else
10748 {
10749 /* Unspecified array length. */
10750 high = low - 1;
10751 }
e77813c8
PM
10752 }
10753
10754 /* Dwarf-2 specifications explicitly allows to create subrange types
10755 without specifying a base type.
10756 In that case, the base type must be set to the type of
10757 the lower bound, upper bound or count, in that order, if any of these
10758 three attributes references an object that has a type.
10759 If no base type is found, the Dwarf-2 specifications say that
10760 a signed integer type of size equal to the size of an address should
10761 be used.
10762 For the following C code: `extern char gdb_int [];'
10763 GCC produces an empty range DIE.
10764 FIXME: muller/2010-05-28: Possible references to object for low bound,
0963b4bd 10765 high bound or count are not yet handled by this code. */
e77813c8
PM
10766 if (TYPE_CODE (base_type) == TYPE_CODE_VOID)
10767 {
10768 struct objfile *objfile = cu->objfile;
10769 struct gdbarch *gdbarch = get_objfile_arch (objfile);
10770 int addr_size = gdbarch_addr_bit (gdbarch) /8;
10771 struct type *int_type = objfile_type (objfile)->builtin_int;
10772
10773 /* Test "int", "long int", and "long long int" objfile types,
10774 and select the first one having a size above or equal to the
10775 architecture address size. */
10776 if (int_type && TYPE_LENGTH (int_type) >= addr_size)
10777 base_type = int_type;
10778 else
10779 {
10780 int_type = objfile_type (objfile)->builtin_long;
10781 if (int_type && TYPE_LENGTH (int_type) >= addr_size)
10782 base_type = int_type;
10783 else
10784 {
10785 int_type = objfile_type (objfile)->builtin_long_long;
10786 if (int_type && TYPE_LENGTH (int_type) >= addr_size)
10787 base_type = int_type;
10788 }
10789 }
10790 }
a02abb62 10791
6e70227d 10792 negative_mask =
43bbcdc2
PH
10793 (LONGEST) -1 << (TYPE_LENGTH (base_type) * TARGET_CHAR_BIT - 1);
10794 if (!TYPE_UNSIGNED (base_type) && (low & negative_mask))
10795 low |= negative_mask;
10796 if (!TYPE_UNSIGNED (base_type) && (high & negative_mask))
10797 high |= negative_mask;
10798
a02abb62
JB
10799 range_type = create_range_type (NULL, base_type, low, high);
10800
bbb0eef6
JK
10801 /* Mark arrays with dynamic length at least as an array of unspecified
10802 length. GDB could check the boundary but before it gets implemented at
10803 least allow accessing the array elements. */
d48323d8 10804 if (attr && attr_form_is_block (attr))
bbb0eef6
JK
10805 TYPE_HIGH_BOUND_UNDEFINED (range_type) = 1;
10806
c2ff108b
JK
10807 /* Ada expects an empty array on no boundary attributes. */
10808 if (attr == NULL && cu->language != language_ada)
10809 TYPE_HIGH_BOUND_UNDEFINED (range_type) = 1;
10810
39cbfefa
DJ
10811 name = dwarf2_name (die, cu);
10812 if (name)
10813 TYPE_NAME (range_type) = name;
6e70227d 10814
e142c38c 10815 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
a02abb62
JB
10816 if (attr)
10817 TYPE_LENGTH (range_type) = DW_UNSND (attr);
10818
7e314c57
JK
10819 set_die_type (die, range_type, cu);
10820
10821 /* set_die_type should be already done. */
b4ba55a1
JB
10822 set_descriptive_type (range_type, die, cu);
10823
7e314c57 10824 return range_type;
a02abb62 10825}
6e70227d 10826
f792889a 10827static struct type *
81a17f79
JB
10828read_unspecified_type (struct die_info *die, struct dwarf2_cu *cu)
10829{
10830 struct type *type;
81a17f79 10831
81a17f79
JB
10832 /* For now, we only support the C meaning of an unspecified type: void. */
10833
0114d602
DJ
10834 type = init_type (TYPE_CODE_VOID, 0, 0, NULL, cu->objfile);
10835 TYPE_NAME (type) = dwarf2_name (die, cu);
81a17f79 10836
f792889a 10837 return set_die_type (die, type, cu);
81a17f79 10838}
a02abb62 10839
639d11d3
DC
10840/* Read a single die and all its descendents. Set the die's sibling
10841 field to NULL; set other fields in the die correctly, and set all
10842 of the descendents' fields correctly. Set *NEW_INFO_PTR to the
10843 location of the info_ptr after reading all of those dies. PARENT
10844 is the parent of the die in question. */
10845
10846static struct die_info *
dee91e82
DE
10847read_die_and_children (const struct die_reader_specs *reader,
10848 gdb_byte *info_ptr,
10849 gdb_byte **new_info_ptr,
10850 struct die_info *parent)
639d11d3
DC
10851{
10852 struct die_info *die;
fe1b8b76 10853 gdb_byte *cur_ptr;
639d11d3
DC
10854 int has_children;
10855
93311388 10856 cur_ptr = read_full_die (reader, &die, info_ptr, &has_children);
1d325ec1
DJ
10857 if (die == NULL)
10858 {
10859 *new_info_ptr = cur_ptr;
10860 return NULL;
10861 }
93311388 10862 store_in_ref_table (die, reader->cu);
639d11d3
DC
10863
10864 if (has_children)
348e048f 10865 die->child = read_die_and_siblings (reader, cur_ptr, new_info_ptr, die);
639d11d3
DC
10866 else
10867 {
10868 die->child = NULL;
10869 *new_info_ptr = cur_ptr;
10870 }
10871
10872 die->sibling = NULL;
10873 die->parent = parent;
10874 return die;
10875}
10876
10877/* Read a die, all of its descendents, and all of its siblings; set
10878 all of the fields of all of the dies correctly. Arguments are as
10879 in read_die_and_children. */
10880
10881static struct die_info *
93311388
DE
10882read_die_and_siblings (const struct die_reader_specs *reader,
10883 gdb_byte *info_ptr,
fe1b8b76 10884 gdb_byte **new_info_ptr,
639d11d3
DC
10885 struct die_info *parent)
10886{
10887 struct die_info *first_die, *last_sibling;
fe1b8b76 10888 gdb_byte *cur_ptr;
639d11d3 10889
c906108c 10890 cur_ptr = info_ptr;
639d11d3
DC
10891 first_die = last_sibling = NULL;
10892
10893 while (1)
c906108c 10894 {
639d11d3 10895 struct die_info *die
dee91e82 10896 = read_die_and_children (reader, cur_ptr, &cur_ptr, parent);
639d11d3 10897
1d325ec1 10898 if (die == NULL)
c906108c 10899 {
639d11d3
DC
10900 *new_info_ptr = cur_ptr;
10901 return first_die;
c906108c 10902 }
1d325ec1
DJ
10903
10904 if (!first_die)
10905 first_die = die;
c906108c 10906 else
1d325ec1
DJ
10907 last_sibling->sibling = die;
10908
10909 last_sibling = die;
c906108c 10910 }
c906108c
SS
10911}
10912
3019eac3
DE
10913/* Read a die and all its attributes, leave space for NUM_EXTRA_ATTRS
10914 attributes.
10915 The caller is responsible for filling in the extra attributes
10916 and updating (*DIEP)->num_attrs.
10917 Set DIEP to point to a newly allocated die with its information,
10918 except for its child, sibling, and parent fields.
10919 Set HAS_CHILDREN to tell whether the die has children or not. */
93311388
DE
10920
10921static gdb_byte *
3019eac3
DE
10922read_full_die_1 (const struct die_reader_specs *reader,
10923 struct die_info **diep, gdb_byte *info_ptr,
10924 int *has_children, int num_extra_attrs)
93311388 10925{
b64f50a1
JK
10926 unsigned int abbrev_number, bytes_read, i;
10927 sect_offset offset;
93311388
DE
10928 struct abbrev_info *abbrev;
10929 struct die_info *die;
10930 struct dwarf2_cu *cu = reader->cu;
10931 bfd *abfd = reader->abfd;
10932
b64f50a1 10933 offset.sect_off = info_ptr - reader->buffer;
93311388
DE
10934 abbrev_number = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
10935 info_ptr += bytes_read;
10936 if (!abbrev_number)
10937 {
10938 *diep = NULL;
10939 *has_children = 0;
10940 return info_ptr;
10941 }
10942
433df2d4 10943 abbrev = abbrev_table_lookup_abbrev (cu->abbrev_table, abbrev_number);
93311388 10944 if (!abbrev)
348e048f
DE
10945 error (_("Dwarf Error: could not find abbrev number %d [in module %s]"),
10946 abbrev_number,
10947 bfd_get_filename (abfd));
10948
3019eac3 10949 die = dwarf_alloc_die (cu, abbrev->num_attrs + num_extra_attrs);
93311388
DE
10950 die->offset = offset;
10951 die->tag = abbrev->tag;
10952 die->abbrev = abbrev_number;
10953
3019eac3
DE
10954 /* Make the result usable.
10955 The caller needs to update num_attrs after adding the extra
10956 attributes. */
93311388
DE
10957 die->num_attrs = abbrev->num_attrs;
10958
10959 for (i = 0; i < abbrev->num_attrs; ++i)
dee91e82
DE
10960 info_ptr = read_attribute (reader, &die->attrs[i], &abbrev->attrs[i],
10961 info_ptr);
93311388
DE
10962
10963 *diep = die;
10964 *has_children = abbrev->has_children;
10965 return info_ptr;
10966}
10967
3019eac3
DE
10968/* Read a die and all its attributes.
10969 Set DIEP to point to a newly allocated die with its information,
10970 except for its child, sibling, and parent fields.
10971 Set HAS_CHILDREN to tell whether the die has children or not. */
10972
10973static gdb_byte *
10974read_full_die (const struct die_reader_specs *reader,
10975 struct die_info **diep, gdb_byte *info_ptr,
10976 int *has_children)
10977{
10978 return read_full_die_1 (reader, diep, info_ptr, has_children, 0);
10979}
433df2d4
DE
10980\f
10981/* Abbreviation tables.
3019eac3 10982
433df2d4 10983 In DWARF version 2, the description of the debugging information is
c906108c
SS
10984 stored in a separate .debug_abbrev section. Before we read any
10985 dies from a section we read in all abbreviations and install them
433df2d4
DE
10986 in a hash table. */
10987
10988/* Allocate space for a struct abbrev_info object in ABBREV_TABLE. */
10989
10990static struct abbrev_info *
10991abbrev_table_alloc_abbrev (struct abbrev_table *abbrev_table)
10992{
10993 struct abbrev_info *abbrev;
10994
10995 abbrev = (struct abbrev_info *)
10996 obstack_alloc (&abbrev_table->abbrev_obstack, sizeof (struct abbrev_info));
10997 memset (abbrev, 0, sizeof (struct abbrev_info));
10998 return abbrev;
10999}
11000
11001/* Add an abbreviation to the table. */
c906108c
SS
11002
11003static void
433df2d4
DE
11004abbrev_table_add_abbrev (struct abbrev_table *abbrev_table,
11005 unsigned int abbrev_number,
11006 struct abbrev_info *abbrev)
11007{
11008 unsigned int hash_number;
11009
11010 hash_number = abbrev_number % ABBREV_HASH_SIZE;
11011 abbrev->next = abbrev_table->abbrevs[hash_number];
11012 abbrev_table->abbrevs[hash_number] = abbrev;
11013}
dee91e82 11014
433df2d4
DE
11015/* Look up an abbrev in the table.
11016 Returns NULL if the abbrev is not found. */
11017
11018static struct abbrev_info *
11019abbrev_table_lookup_abbrev (const struct abbrev_table *abbrev_table,
11020 unsigned int abbrev_number)
c906108c 11021{
433df2d4
DE
11022 unsigned int hash_number;
11023 struct abbrev_info *abbrev;
11024
11025 hash_number = abbrev_number % ABBREV_HASH_SIZE;
11026 abbrev = abbrev_table->abbrevs[hash_number];
11027
11028 while (abbrev)
11029 {
11030 if (abbrev->number == abbrev_number)
11031 return abbrev;
11032 abbrev = abbrev->next;
11033 }
11034 return NULL;
11035}
11036
11037/* Read in an abbrev table. */
11038
11039static struct abbrev_table *
11040abbrev_table_read_table (struct dwarf2_section_info *section,
11041 sect_offset offset)
11042{
11043 struct objfile *objfile = dwarf2_per_objfile->objfile;
11044 bfd *abfd = section->asection->owner;
11045 struct abbrev_table *abbrev_table;
fe1b8b76 11046 gdb_byte *abbrev_ptr;
c906108c
SS
11047 struct abbrev_info *cur_abbrev;
11048 unsigned int abbrev_number, bytes_read, abbrev_name;
433df2d4 11049 unsigned int abbrev_form;
f3dd6933
DJ
11050 struct attr_abbrev *cur_attrs;
11051 unsigned int allocated_attrs;
c906108c 11052
433df2d4
DE
11053 abbrev_table = XMALLOC (struct abbrev_table);
11054 obstack_init (&abbrev_table->abbrev_obstack);
11055 abbrev_table->abbrevs = obstack_alloc (&abbrev_table->abbrev_obstack,
11056 (ABBREV_HASH_SIZE
11057 * sizeof (struct abbrev_info *)));
11058 memset (abbrev_table->abbrevs, 0,
11059 ABBREV_HASH_SIZE * sizeof (struct abbrev_info *));
c906108c 11060
433df2d4
DE
11061 dwarf2_read_section (objfile, section);
11062 abbrev_ptr = section->buffer + offset.sect_off;
c906108c
SS
11063 abbrev_number = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
11064 abbrev_ptr += bytes_read;
11065
f3dd6933
DJ
11066 allocated_attrs = ATTR_ALLOC_CHUNK;
11067 cur_attrs = xmalloc (allocated_attrs * sizeof (struct attr_abbrev));
6e70227d 11068
0963b4bd 11069 /* Loop until we reach an abbrev number of 0. */
c906108c
SS
11070 while (abbrev_number)
11071 {
433df2d4 11072 cur_abbrev = abbrev_table_alloc_abbrev (abbrev_table);
c906108c
SS
11073
11074 /* read in abbrev header */
11075 cur_abbrev->number = abbrev_number;
11076 cur_abbrev->tag = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
11077 abbrev_ptr += bytes_read;
11078 cur_abbrev->has_children = read_1_byte (abfd, abbrev_ptr);
11079 abbrev_ptr += 1;
11080
11081 /* now read in declarations */
11082 abbrev_name = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
11083 abbrev_ptr += bytes_read;
11084 abbrev_form = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
11085 abbrev_ptr += bytes_read;
11086 while (abbrev_name)
11087 {
f3dd6933 11088 if (cur_abbrev->num_attrs == allocated_attrs)
c906108c 11089 {
f3dd6933
DJ
11090 allocated_attrs += ATTR_ALLOC_CHUNK;
11091 cur_attrs
11092 = xrealloc (cur_attrs, (allocated_attrs
11093 * sizeof (struct attr_abbrev)));
c906108c 11094 }
ae038cb0 11095
f3dd6933
DJ
11096 cur_attrs[cur_abbrev->num_attrs].name = abbrev_name;
11097 cur_attrs[cur_abbrev->num_attrs++].form = abbrev_form;
c906108c
SS
11098 abbrev_name = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
11099 abbrev_ptr += bytes_read;
11100 abbrev_form = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
11101 abbrev_ptr += bytes_read;
11102 }
11103
433df2d4 11104 cur_abbrev->attrs = obstack_alloc (&abbrev_table->abbrev_obstack,
f3dd6933
DJ
11105 (cur_abbrev->num_attrs
11106 * sizeof (struct attr_abbrev)));
11107 memcpy (cur_abbrev->attrs, cur_attrs,
11108 cur_abbrev->num_attrs * sizeof (struct attr_abbrev));
11109
433df2d4 11110 abbrev_table_add_abbrev (abbrev_table, abbrev_number, cur_abbrev);
c906108c
SS
11111
11112 /* Get next abbreviation.
11113 Under Irix6 the abbreviations for a compilation unit are not
c5aa993b
JM
11114 always properly terminated with an abbrev number of 0.
11115 Exit loop if we encounter an abbreviation which we have
11116 already read (which means we are about to read the abbreviations
11117 for the next compile unit) or if the end of the abbreviation
11118 table is reached. */
433df2d4 11119 if ((unsigned int) (abbrev_ptr - section->buffer) >= section->size)
c906108c
SS
11120 break;
11121 abbrev_number = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
11122 abbrev_ptr += bytes_read;
433df2d4 11123 if (abbrev_table_lookup_abbrev (abbrev_table, abbrev_number) != NULL)
c906108c
SS
11124 break;
11125 }
f3dd6933
DJ
11126
11127 xfree (cur_attrs);
433df2d4 11128 return abbrev_table;
c906108c
SS
11129}
11130
433df2d4 11131/* Free the resources held by ABBREV_TABLE. */
c906108c 11132
c906108c 11133static void
433df2d4 11134abbrev_table_free (struct abbrev_table *abbrev_table)
c906108c 11135{
433df2d4
DE
11136 obstack_free (&abbrev_table->abbrev_obstack, NULL);
11137 xfree (abbrev_table);
c906108c
SS
11138}
11139
433df2d4
DE
11140/* Read the abbrev table for CU from ABBREV_SECTION. */
11141
11142static void
11143dwarf2_read_abbrevs (struct dwarf2_cu *cu,
11144 struct dwarf2_section_info *abbrev_section)
c906108c 11145{
433df2d4
DE
11146 cu->abbrev_table =
11147 abbrev_table_read_table (abbrev_section, cu->header.abbrev_offset);
11148}
c906108c 11149
433df2d4 11150/* Release the memory used by the abbrev table for a compilation unit. */
c906108c 11151
433df2d4
DE
11152static void
11153dwarf2_free_abbrev_table (void *ptr_to_cu)
11154{
11155 struct dwarf2_cu *cu = ptr_to_cu;
c906108c 11156
433df2d4
DE
11157 abbrev_table_free (cu->abbrev_table);
11158 /* Set this to NULL so that we SEGV if we try to read it later,
11159 and also because free_comp_unit verifies this is NULL. */
11160 cu->abbrev_table = NULL;
11161}
11162\f
72bf9492
DJ
11163/* Returns nonzero if TAG represents a type that we might generate a partial
11164 symbol for. */
11165
11166static int
11167is_type_tag_for_partial (int tag)
11168{
11169 switch (tag)
11170 {
11171#if 0
11172 /* Some types that would be reasonable to generate partial symbols for,
11173 that we don't at present. */
11174 case DW_TAG_array_type:
11175 case DW_TAG_file_type:
11176 case DW_TAG_ptr_to_member_type:
11177 case DW_TAG_set_type:
11178 case DW_TAG_string_type:
11179 case DW_TAG_subroutine_type:
11180#endif
11181 case DW_TAG_base_type:
11182 case DW_TAG_class_type:
680b30c7 11183 case DW_TAG_interface_type:
72bf9492
DJ
11184 case DW_TAG_enumeration_type:
11185 case DW_TAG_structure_type:
11186 case DW_TAG_subrange_type:
11187 case DW_TAG_typedef:
11188 case DW_TAG_union_type:
11189 return 1;
11190 default:
11191 return 0;
11192 }
11193}
11194
11195/* Load all DIEs that are interesting for partial symbols into memory. */
11196
11197static struct partial_die_info *
dee91e82
DE
11198load_partial_dies (const struct die_reader_specs *reader,
11199 gdb_byte *info_ptr, int building_psymtab)
72bf9492 11200{
dee91e82 11201 struct dwarf2_cu *cu = reader->cu;
bb5ed363 11202 struct objfile *objfile = cu->objfile;
72bf9492
DJ
11203 struct partial_die_info *part_die;
11204 struct partial_die_info *parent_die, *last_die, *first_die = NULL;
11205 struct abbrev_info *abbrev;
11206 unsigned int bytes_read;
5afb4e99 11207 unsigned int load_all = 0;
72bf9492
DJ
11208 int nesting_level = 1;
11209
11210 parent_die = NULL;
11211 last_die = NULL;
11212
7adf1e79
DE
11213 gdb_assert (cu->per_cu != NULL);
11214 if (cu->per_cu->load_all_dies)
5afb4e99
DJ
11215 load_all = 1;
11216
72bf9492
DJ
11217 cu->partial_dies
11218 = htab_create_alloc_ex (cu->header.length / 12,
11219 partial_die_hash,
11220 partial_die_eq,
11221 NULL,
11222 &cu->comp_unit_obstack,
11223 hashtab_obstack_allocate,
11224 dummy_obstack_deallocate);
11225
11226 part_die = obstack_alloc (&cu->comp_unit_obstack,
11227 sizeof (struct partial_die_info));
11228
11229 while (1)
11230 {
11231 abbrev = peek_die_abbrev (info_ptr, &bytes_read, cu);
11232
11233 /* A NULL abbrev means the end of a series of children. */
11234 if (abbrev == NULL)
11235 {
11236 if (--nesting_level == 0)
11237 {
11238 /* PART_DIE was probably the last thing allocated on the
11239 comp_unit_obstack, so we could call obstack_free
11240 here. We don't do that because the waste is small,
11241 and will be cleaned up when we're done with this
11242 compilation unit. This way, we're also more robust
11243 against other users of the comp_unit_obstack. */
11244 return first_die;
11245 }
11246 info_ptr += bytes_read;
11247 last_die = parent_die;
11248 parent_die = parent_die->die_parent;
11249 continue;
11250 }
11251
98bfdba5
PA
11252 /* Check for template arguments. We never save these; if
11253 they're seen, we just mark the parent, and go on our way. */
11254 if (parent_die != NULL
11255 && cu->language == language_cplus
11256 && (abbrev->tag == DW_TAG_template_type_param
11257 || abbrev->tag == DW_TAG_template_value_param))
11258 {
11259 parent_die->has_template_arguments = 1;
11260
11261 if (!load_all)
11262 {
11263 /* We don't need a partial DIE for the template argument. */
dee91e82 11264 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
98bfdba5
PA
11265 continue;
11266 }
11267 }
11268
0d99eb77 11269 /* We only recurse into c++ subprograms looking for template arguments.
98bfdba5
PA
11270 Skip their other children. */
11271 if (!load_all
11272 && cu->language == language_cplus
11273 && parent_die != NULL
11274 && parent_die->tag == DW_TAG_subprogram)
11275 {
dee91e82 11276 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
98bfdba5
PA
11277 continue;
11278 }
11279
5afb4e99
DJ
11280 /* Check whether this DIE is interesting enough to save. Normally
11281 we would not be interested in members here, but there may be
11282 later variables referencing them via DW_AT_specification (for
11283 static members). */
11284 if (!load_all
11285 && !is_type_tag_for_partial (abbrev->tag)
72929c62 11286 && abbrev->tag != DW_TAG_constant
72bf9492
DJ
11287 && abbrev->tag != DW_TAG_enumerator
11288 && abbrev->tag != DW_TAG_subprogram
bc30ff58 11289 && abbrev->tag != DW_TAG_lexical_block
72bf9492 11290 && abbrev->tag != DW_TAG_variable
5afb4e99 11291 && abbrev->tag != DW_TAG_namespace
f55ee35c 11292 && abbrev->tag != DW_TAG_module
95554aad
TT
11293 && abbrev->tag != DW_TAG_member
11294 && abbrev->tag != DW_TAG_imported_unit)
72bf9492
DJ
11295 {
11296 /* Otherwise we skip to the next sibling, if any. */
dee91e82 11297 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
72bf9492
DJ
11298 continue;
11299 }
11300
dee91e82
DE
11301 info_ptr = read_partial_die (reader, part_die, abbrev, bytes_read,
11302 info_ptr);
72bf9492
DJ
11303
11304 /* This two-pass algorithm for processing partial symbols has a
11305 high cost in cache pressure. Thus, handle some simple cases
11306 here which cover the majority of C partial symbols. DIEs
11307 which neither have specification tags in them, nor could have
11308 specification tags elsewhere pointing at them, can simply be
11309 processed and discarded.
11310
11311 This segment is also optional; scan_partial_symbols and
11312 add_partial_symbol will handle these DIEs if we chain
11313 them in normally. When compilers which do not emit large
11314 quantities of duplicate debug information are more common,
11315 this code can probably be removed. */
11316
11317 /* Any complete simple types at the top level (pretty much all
11318 of them, for a language without namespaces), can be processed
11319 directly. */
11320 if (parent_die == NULL
11321 && part_die->has_specification == 0
11322 && part_die->is_declaration == 0
d8228535 11323 && ((part_die->tag == DW_TAG_typedef && !part_die->has_children)
72bf9492
DJ
11324 || part_die->tag == DW_TAG_base_type
11325 || part_die->tag == DW_TAG_subrange_type))
11326 {
11327 if (building_psymtab && part_die->name != NULL)
04a679b8 11328 add_psymbol_to_list (part_die->name, strlen (part_die->name), 0,
72bf9492 11329 VAR_DOMAIN, LOC_TYPEDEF,
bb5ed363
DE
11330 &objfile->static_psymbols,
11331 0, (CORE_ADDR) 0, cu->language, objfile);
dee91e82 11332 info_ptr = locate_pdi_sibling (reader, part_die, info_ptr);
72bf9492
DJ
11333 continue;
11334 }
11335
d8228535
JK
11336 /* The exception for DW_TAG_typedef with has_children above is
11337 a workaround of GCC PR debug/47510. In the case of this complaint
11338 type_name_no_tag_or_error will error on such types later.
11339
11340 GDB skipped children of DW_TAG_typedef by the shortcut above and then
11341 it could not find the child DIEs referenced later, this is checked
11342 above. In correct DWARF DW_TAG_typedef should have no children. */
11343
11344 if (part_die->tag == DW_TAG_typedef && part_die->has_children)
11345 complaint (&symfile_complaints,
11346 _("DW_TAG_typedef has childen - GCC PR debug/47510 bug "
11347 "- DIE at 0x%x [in module %s]"),
b64f50a1 11348 part_die->offset.sect_off, objfile->name);
d8228535 11349
72bf9492
DJ
11350 /* If we're at the second level, and we're an enumerator, and
11351 our parent has no specification (meaning possibly lives in a
11352 namespace elsewhere), then we can add the partial symbol now
11353 instead of queueing it. */
11354 if (part_die->tag == DW_TAG_enumerator
11355 && parent_die != NULL
11356 && parent_die->die_parent == NULL
11357 && parent_die->tag == DW_TAG_enumeration_type
11358 && parent_die->has_specification == 0)
11359 {
11360 if (part_die->name == NULL)
3e43a32a
MS
11361 complaint (&symfile_complaints,
11362 _("malformed enumerator DIE ignored"));
72bf9492 11363 else if (building_psymtab)
04a679b8 11364 add_psymbol_to_list (part_die->name, strlen (part_die->name), 0,
72bf9492 11365 VAR_DOMAIN, LOC_CONST,
987504bb
JJ
11366 (cu->language == language_cplus
11367 || cu->language == language_java)
bb5ed363
DE
11368 ? &objfile->global_psymbols
11369 : &objfile->static_psymbols,
11370 0, (CORE_ADDR) 0, cu->language, objfile);
72bf9492 11371
dee91e82 11372 info_ptr = locate_pdi_sibling (reader, part_die, info_ptr);
72bf9492
DJ
11373 continue;
11374 }
11375
11376 /* We'll save this DIE so link it in. */
11377 part_die->die_parent = parent_die;
11378 part_die->die_sibling = NULL;
11379 part_die->die_child = NULL;
11380
11381 if (last_die && last_die == parent_die)
11382 last_die->die_child = part_die;
11383 else if (last_die)
11384 last_die->die_sibling = part_die;
11385
11386 last_die = part_die;
11387
11388 if (first_die == NULL)
11389 first_die = part_die;
11390
11391 /* Maybe add the DIE to the hash table. Not all DIEs that we
11392 find interesting need to be in the hash table, because we
11393 also have the parent/sibling/child chains; only those that we
11394 might refer to by offset later during partial symbol reading.
11395
11396 For now this means things that might have be the target of a
11397 DW_AT_specification, DW_AT_abstract_origin, or
11398 DW_AT_extension. DW_AT_extension will refer only to
11399 namespaces; DW_AT_abstract_origin refers to functions (and
11400 many things under the function DIE, but we do not recurse
11401 into function DIEs during partial symbol reading) and
11402 possibly variables as well; DW_AT_specification refers to
11403 declarations. Declarations ought to have the DW_AT_declaration
11404 flag. It happens that GCC forgets to put it in sometimes, but
11405 only for functions, not for types.
11406
11407 Adding more things than necessary to the hash table is harmless
11408 except for the performance cost. Adding too few will result in
5afb4e99
DJ
11409 wasted time in find_partial_die, when we reread the compilation
11410 unit with load_all_dies set. */
72bf9492 11411
5afb4e99 11412 if (load_all
72929c62 11413 || abbrev->tag == DW_TAG_constant
5afb4e99 11414 || abbrev->tag == DW_TAG_subprogram
72bf9492
DJ
11415 || abbrev->tag == DW_TAG_variable
11416 || abbrev->tag == DW_TAG_namespace
11417 || part_die->is_declaration)
11418 {
11419 void **slot;
11420
11421 slot = htab_find_slot_with_hash (cu->partial_dies, part_die,
b64f50a1 11422 part_die->offset.sect_off, INSERT);
72bf9492
DJ
11423 *slot = part_die;
11424 }
11425
11426 part_die = obstack_alloc (&cu->comp_unit_obstack,
11427 sizeof (struct partial_die_info));
11428
11429 /* For some DIEs we want to follow their children (if any). For C
bc30ff58 11430 we have no reason to follow the children of structures; for other
98bfdba5
PA
11431 languages we have to, so that we can get at method physnames
11432 to infer fully qualified class names, for DW_AT_specification,
11433 and for C++ template arguments. For C++, we also look one level
11434 inside functions to find template arguments (if the name of the
11435 function does not already contain the template arguments).
bc30ff58
JB
11436
11437 For Ada, we need to scan the children of subprograms and lexical
11438 blocks as well because Ada allows the definition of nested
11439 entities that could be interesting for the debugger, such as
11440 nested subprograms for instance. */
72bf9492 11441 if (last_die->has_children
5afb4e99
DJ
11442 && (load_all
11443 || last_die->tag == DW_TAG_namespace
f55ee35c 11444 || last_die->tag == DW_TAG_module
72bf9492 11445 || last_die->tag == DW_TAG_enumeration_type
98bfdba5
PA
11446 || (cu->language == language_cplus
11447 && last_die->tag == DW_TAG_subprogram
11448 && (last_die->name == NULL
11449 || strchr (last_die->name, '<') == NULL))
72bf9492
DJ
11450 || (cu->language != language_c
11451 && (last_die->tag == DW_TAG_class_type
680b30c7 11452 || last_die->tag == DW_TAG_interface_type
72bf9492 11453 || last_die->tag == DW_TAG_structure_type
bc30ff58
JB
11454 || last_die->tag == DW_TAG_union_type))
11455 || (cu->language == language_ada
11456 && (last_die->tag == DW_TAG_subprogram
11457 || last_die->tag == DW_TAG_lexical_block))))
72bf9492
DJ
11458 {
11459 nesting_level++;
11460 parent_die = last_die;
11461 continue;
11462 }
11463
11464 /* Otherwise we skip to the next sibling, if any. */
dee91e82 11465 info_ptr = locate_pdi_sibling (reader, last_die, info_ptr);
72bf9492
DJ
11466
11467 /* Back to the top, do it again. */
11468 }
11469}
11470
c906108c
SS
11471/* Read a minimal amount of information into the minimal die structure. */
11472
fe1b8b76 11473static gdb_byte *
dee91e82
DE
11474read_partial_die (const struct die_reader_specs *reader,
11475 struct partial_die_info *part_die,
11476 struct abbrev_info *abbrev, unsigned int abbrev_len,
11477 gdb_byte *info_ptr)
c906108c 11478{
dee91e82 11479 struct dwarf2_cu *cu = reader->cu;
bb5ed363 11480 struct objfile *objfile = cu->objfile;
dee91e82 11481 gdb_byte *buffer = reader->buffer;
fa238c03 11482 unsigned int i;
c906108c 11483 struct attribute attr;
c5aa993b 11484 int has_low_pc_attr = 0;
c906108c 11485 int has_high_pc_attr = 0;
91da1414 11486 int high_pc_relative = 0;
c906108c 11487
72bf9492 11488 memset (part_die, 0, sizeof (struct partial_die_info));
c906108c 11489
b64f50a1 11490 part_die->offset.sect_off = info_ptr - buffer;
72bf9492
DJ
11491
11492 info_ptr += abbrev_len;
11493
11494 if (abbrev == NULL)
11495 return info_ptr;
11496
c906108c
SS
11497 part_die->tag = abbrev->tag;
11498 part_die->has_children = abbrev->has_children;
c906108c
SS
11499
11500 for (i = 0; i < abbrev->num_attrs; ++i)
11501 {
dee91e82 11502 info_ptr = read_attribute (reader, &attr, &abbrev->attrs[i], info_ptr);
c906108c
SS
11503
11504 /* Store the data if it is of an attribute we want to keep in a
c5aa993b 11505 partial symbol table. */
c906108c
SS
11506 switch (attr.name)
11507 {
11508 case DW_AT_name:
71c25dea
TT
11509 switch (part_die->tag)
11510 {
11511 case DW_TAG_compile_unit:
95554aad 11512 case DW_TAG_partial_unit:
348e048f 11513 case DW_TAG_type_unit:
71c25dea
TT
11514 /* Compilation units have a DW_AT_name that is a filename, not
11515 a source language identifier. */
11516 case DW_TAG_enumeration_type:
11517 case DW_TAG_enumerator:
11518 /* These tags always have simple identifiers already; no need
11519 to canonicalize them. */
11520 part_die->name = DW_STRING (&attr);
11521 break;
11522 default:
11523 part_die->name
11524 = dwarf2_canonicalize_name (DW_STRING (&attr), cu,
bb5ed363 11525 &objfile->objfile_obstack);
71c25dea
TT
11526 break;
11527 }
c906108c 11528 break;
31ef98ae 11529 case DW_AT_linkage_name:
c906108c 11530 case DW_AT_MIPS_linkage_name:
31ef98ae
TT
11531 /* Note that both forms of linkage name might appear. We
11532 assume they will be the same, and we only store the last
11533 one we see. */
94af9270
KS
11534 if (cu->language == language_ada)
11535 part_die->name = DW_STRING (&attr);
abc72ce4 11536 part_die->linkage_name = DW_STRING (&attr);
c906108c
SS
11537 break;
11538 case DW_AT_low_pc:
11539 has_low_pc_attr = 1;
11540 part_die->lowpc = DW_ADDR (&attr);
11541 break;
11542 case DW_AT_high_pc:
11543 has_high_pc_attr = 1;
3019eac3
DE
11544 if (attr.form == DW_FORM_addr
11545 || attr.form == DW_FORM_GNU_addr_index)
91da1414
MW
11546 part_die->highpc = DW_ADDR (&attr);
11547 else
11548 {
11549 high_pc_relative = 1;
11550 part_die->highpc = DW_UNSND (&attr);
11551 }
c906108c
SS
11552 break;
11553 case DW_AT_location:
0963b4bd 11554 /* Support the .debug_loc offsets. */
8e19ed76
PS
11555 if (attr_form_is_block (&attr))
11556 {
95554aad 11557 part_die->d.locdesc = DW_BLOCK (&attr);
8e19ed76 11558 }
3690dd37 11559 else if (attr_form_is_section_offset (&attr))
8e19ed76 11560 {
4d3c2250 11561 dwarf2_complex_location_expr_complaint ();
8e19ed76
PS
11562 }
11563 else
11564 {
4d3c2250
KB
11565 dwarf2_invalid_attrib_class_complaint ("DW_AT_location",
11566 "partial symbol information");
8e19ed76 11567 }
c906108c 11568 break;
c906108c
SS
11569 case DW_AT_external:
11570 part_die->is_external = DW_UNSND (&attr);
11571 break;
11572 case DW_AT_declaration:
11573 part_die->is_declaration = DW_UNSND (&attr);
11574 break;
11575 case DW_AT_type:
11576 part_die->has_type = 1;
11577 break;
11578 case DW_AT_abstract_origin:
11579 case DW_AT_specification:
72bf9492
DJ
11580 case DW_AT_extension:
11581 part_die->has_specification = 1;
c764a876 11582 part_die->spec_offset = dwarf2_get_ref_die_offset (&attr);
c906108c
SS
11583 break;
11584 case DW_AT_sibling:
11585 /* Ignore absolute siblings, they might point outside of
11586 the current compile unit. */
11587 if (attr.form == DW_FORM_ref_addr)
3e43a32a
MS
11588 complaint (&symfile_complaints,
11589 _("ignoring absolute DW_AT_sibling"));
c906108c 11590 else
b64f50a1 11591 part_die->sibling = buffer + dwarf2_get_ref_die_offset (&attr).sect_off;
c906108c 11592 break;
fa4028e9
JB
11593 case DW_AT_byte_size:
11594 part_die->has_byte_size = 1;
11595 break;
68511cec
CES
11596 case DW_AT_calling_convention:
11597 /* DWARF doesn't provide a way to identify a program's source-level
11598 entry point. DW_AT_calling_convention attributes are only meant
11599 to describe functions' calling conventions.
11600
11601 However, because it's a necessary piece of information in
11602 Fortran, and because DW_CC_program is the only piece of debugging
11603 information whose definition refers to a 'main program' at all,
11604 several compilers have begun marking Fortran main programs with
11605 DW_CC_program --- even when those functions use the standard
11606 calling conventions.
11607
11608 So until DWARF specifies a way to provide this information and
11609 compilers pick up the new representation, we'll support this
11610 practice. */
11611 if (DW_UNSND (&attr) == DW_CC_program
11612 && cu->language == language_fortran)
01f8c46d
JK
11613 {
11614 set_main_name (part_die->name);
11615
11616 /* As this DIE has a static linkage the name would be difficult
11617 to look up later. */
11618 language_of_main = language_fortran;
11619 }
68511cec 11620 break;
481860b3
GB
11621 case DW_AT_inline:
11622 if (DW_UNSND (&attr) == DW_INL_inlined
11623 || DW_UNSND (&attr) == DW_INL_declared_inlined)
11624 part_die->may_be_inlined = 1;
11625 break;
95554aad
TT
11626
11627 case DW_AT_import:
11628 if (part_die->tag == DW_TAG_imported_unit)
11629 part_die->d.offset = dwarf2_get_ref_die_offset (&attr);
11630 break;
11631
c906108c
SS
11632 default:
11633 break;
11634 }
11635 }
11636
91da1414
MW
11637 if (high_pc_relative)
11638 part_die->highpc += part_die->lowpc;
11639
9373cf26
JK
11640 if (has_low_pc_attr && has_high_pc_attr)
11641 {
11642 /* When using the GNU linker, .gnu.linkonce. sections are used to
11643 eliminate duplicate copies of functions and vtables and such.
11644 The linker will arbitrarily choose one and discard the others.
11645 The AT_*_pc values for such functions refer to local labels in
11646 these sections. If the section from that file was discarded, the
11647 labels are not in the output, so the relocs get a value of 0.
11648 If this is a discarded function, mark the pc bounds as invalid,
11649 so that GDB will ignore it. */
11650 if (part_die->lowpc == 0 && !dwarf2_per_objfile->has_section_at_zero)
11651 {
bb5ed363 11652 struct gdbarch *gdbarch = get_objfile_arch (objfile);
9373cf26
JK
11653
11654 complaint (&symfile_complaints,
11655 _("DW_AT_low_pc %s is zero "
11656 "for DIE at 0x%x [in module %s]"),
11657 paddress (gdbarch, part_die->lowpc),
b64f50a1 11658 part_die->offset.sect_off, objfile->name);
9373cf26
JK
11659 }
11660 /* dwarf2_get_pc_bounds has also the strict low < high requirement. */
11661 else if (part_die->lowpc >= part_die->highpc)
11662 {
bb5ed363 11663 struct gdbarch *gdbarch = get_objfile_arch (objfile);
9373cf26
JK
11664
11665 complaint (&symfile_complaints,
11666 _("DW_AT_low_pc %s is not < DW_AT_high_pc %s "
11667 "for DIE at 0x%x [in module %s]"),
11668 paddress (gdbarch, part_die->lowpc),
11669 paddress (gdbarch, part_die->highpc),
b64f50a1 11670 part_die->offset.sect_off, objfile->name);
9373cf26
JK
11671 }
11672 else
11673 part_die->has_pc_info = 1;
11674 }
85cbf3d3 11675
c906108c
SS
11676 return info_ptr;
11677}
11678
72bf9492
DJ
11679/* Find a cached partial DIE at OFFSET in CU. */
11680
11681static struct partial_die_info *
b64f50a1 11682find_partial_die_in_comp_unit (sect_offset offset, struct dwarf2_cu *cu)
72bf9492
DJ
11683{
11684 struct partial_die_info *lookup_die = NULL;
11685 struct partial_die_info part_die;
11686
11687 part_die.offset = offset;
b64f50a1
JK
11688 lookup_die = htab_find_with_hash (cu->partial_dies, &part_die,
11689 offset.sect_off);
72bf9492 11690
72bf9492
DJ
11691 return lookup_die;
11692}
11693
348e048f
DE
11694/* Find a partial DIE at OFFSET, which may or may not be in CU,
11695 except in the case of .debug_types DIEs which do not reference
11696 outside their CU (they do however referencing other types via
55f1336d 11697 DW_FORM_ref_sig8). */
72bf9492
DJ
11698
11699static struct partial_die_info *
b64f50a1 11700find_partial_die (sect_offset offset, struct dwarf2_cu *cu)
72bf9492 11701{
bb5ed363 11702 struct objfile *objfile = cu->objfile;
5afb4e99
DJ
11703 struct dwarf2_per_cu_data *per_cu = NULL;
11704 struct partial_die_info *pd = NULL;
72bf9492 11705
45452591 11706 if (offset_in_cu_p (&cu->header, offset))
5afb4e99
DJ
11707 {
11708 pd = find_partial_die_in_comp_unit (offset, cu);
11709 if (pd != NULL)
11710 return pd;
0d99eb77
DE
11711 /* We missed recording what we needed.
11712 Load all dies and try again. */
11713 per_cu = cu->per_cu;
5afb4e99 11714 }
0d99eb77
DE
11715 else
11716 {
11717 /* TUs don't reference other CUs/TUs (except via type signatures). */
3019eac3 11718 if (cu->per_cu->is_debug_types)
0d99eb77
DE
11719 {
11720 error (_("Dwarf Error: Type Unit at offset 0x%lx contains"
11721 " external reference to offset 0x%lx [in module %s].\n"),
11722 (long) cu->header.offset.sect_off, (long) offset.sect_off,
11723 bfd_get_filename (objfile->obfd));
11724 }
11725 per_cu = dwarf2_find_containing_comp_unit (offset, objfile);
72bf9492 11726
0d99eb77
DE
11727 if (per_cu->cu == NULL || per_cu->cu->partial_dies == NULL)
11728 load_partial_comp_unit (per_cu);
ae038cb0 11729
0d99eb77
DE
11730 per_cu->cu->last_used = 0;
11731 pd = find_partial_die_in_comp_unit (offset, per_cu->cu);
11732 }
5afb4e99 11733
dee91e82
DE
11734 /* If we didn't find it, and not all dies have been loaded,
11735 load them all and try again. */
11736
5afb4e99
DJ
11737 if (pd == NULL && per_cu->load_all_dies == 0)
11738 {
5afb4e99 11739 per_cu->load_all_dies = 1;
fd820528
DE
11740
11741 /* This is nasty. When we reread the DIEs, somewhere up the call chain
11742 THIS_CU->cu may already be in use. So we can't just free it and
11743 replace its DIEs with the ones we read in. Instead, we leave those
11744 DIEs alone (which can still be in use, e.g. in scan_partial_symbols),
11745 and clobber THIS_CU->cu->partial_dies with the hash table for the new
11746 set. */
dee91e82 11747 load_partial_comp_unit (per_cu);
5afb4e99
DJ
11748
11749 pd = find_partial_die_in_comp_unit (offset, per_cu->cu);
11750 }
11751
11752 if (pd == NULL)
11753 internal_error (__FILE__, __LINE__,
3e43a32a
MS
11754 _("could not find partial DIE 0x%x "
11755 "in cache [from module %s]\n"),
b64f50a1 11756 offset.sect_off, bfd_get_filename (objfile->obfd));
5afb4e99 11757 return pd;
72bf9492
DJ
11758}
11759
abc72ce4
DE
11760/* See if we can figure out if the class lives in a namespace. We do
11761 this by looking for a member function; its demangled name will
11762 contain namespace info, if there is any. */
11763
11764static void
11765guess_partial_die_structure_name (struct partial_die_info *struct_pdi,
11766 struct dwarf2_cu *cu)
11767{
11768 /* NOTE: carlton/2003-10-07: Getting the info this way changes
11769 what template types look like, because the demangler
11770 frequently doesn't give the same name as the debug info. We
11771 could fix this by only using the demangled name to get the
11772 prefix (but see comment in read_structure_type). */
11773
11774 struct partial_die_info *real_pdi;
11775 struct partial_die_info *child_pdi;
11776
11777 /* If this DIE (this DIE's specification, if any) has a parent, then
11778 we should not do this. We'll prepend the parent's fully qualified
11779 name when we create the partial symbol. */
11780
11781 real_pdi = struct_pdi;
11782 while (real_pdi->has_specification)
11783 real_pdi = find_partial_die (real_pdi->spec_offset, cu);
11784
11785 if (real_pdi->die_parent != NULL)
11786 return;
11787
11788 for (child_pdi = struct_pdi->die_child;
11789 child_pdi != NULL;
11790 child_pdi = child_pdi->die_sibling)
11791 {
11792 if (child_pdi->tag == DW_TAG_subprogram
11793 && child_pdi->linkage_name != NULL)
11794 {
11795 char *actual_class_name
11796 = language_class_name_from_physname (cu->language_defn,
11797 child_pdi->linkage_name);
11798 if (actual_class_name != NULL)
11799 {
11800 struct_pdi->name
11801 = obsavestring (actual_class_name,
11802 strlen (actual_class_name),
11803 &cu->objfile->objfile_obstack);
11804 xfree (actual_class_name);
11805 }
11806 break;
11807 }
11808 }
11809}
11810
72bf9492
DJ
11811/* Adjust PART_DIE before generating a symbol for it. This function
11812 may set the is_external flag or change the DIE's name. */
11813
11814static void
11815fixup_partial_die (struct partial_die_info *part_die,
11816 struct dwarf2_cu *cu)
11817{
abc72ce4
DE
11818 /* Once we've fixed up a die, there's no point in doing so again.
11819 This also avoids a memory leak if we were to call
11820 guess_partial_die_structure_name multiple times. */
11821 if (part_die->fixup_called)
11822 return;
11823
72bf9492
DJ
11824 /* If we found a reference attribute and the DIE has no name, try
11825 to find a name in the referred to DIE. */
11826
11827 if (part_die->name == NULL && part_die->has_specification)
11828 {
11829 struct partial_die_info *spec_die;
72bf9492 11830
10b3939b 11831 spec_die = find_partial_die (part_die->spec_offset, cu);
72bf9492 11832
10b3939b 11833 fixup_partial_die (spec_die, cu);
72bf9492
DJ
11834
11835 if (spec_die->name)
11836 {
11837 part_die->name = spec_die->name;
11838
11839 /* Copy DW_AT_external attribute if it is set. */
11840 if (spec_die->is_external)
11841 part_die->is_external = spec_die->is_external;
11842 }
11843 }
11844
11845 /* Set default names for some unnamed DIEs. */
72bf9492
DJ
11846
11847 if (part_die->name == NULL && part_die->tag == DW_TAG_namespace)
2b1dbab0 11848 part_die->name = CP_ANONYMOUS_NAMESPACE_STR;
72bf9492 11849
abc72ce4
DE
11850 /* If there is no parent die to provide a namespace, and there are
11851 children, see if we can determine the namespace from their linkage
122d1940 11852 name. */
abc72ce4 11853 if (cu->language == language_cplus
8b70b953 11854 && !VEC_empty (dwarf2_section_info_def, dwarf2_per_objfile->types)
abc72ce4
DE
11855 && part_die->die_parent == NULL
11856 && part_die->has_children
11857 && (part_die->tag == DW_TAG_class_type
11858 || part_die->tag == DW_TAG_structure_type
11859 || part_die->tag == DW_TAG_union_type))
11860 guess_partial_die_structure_name (part_die, cu);
11861
53832f31
TT
11862 /* GCC might emit a nameless struct or union that has a linkage
11863 name. See http://gcc.gnu.org/bugzilla/show_bug.cgi?id=47510. */
11864 if (part_die->name == NULL
96408a79
SA
11865 && (part_die->tag == DW_TAG_class_type
11866 || part_die->tag == DW_TAG_interface_type
11867 || part_die->tag == DW_TAG_structure_type
11868 || part_die->tag == DW_TAG_union_type)
53832f31
TT
11869 && part_die->linkage_name != NULL)
11870 {
11871 char *demangled;
11872
11873 demangled = cplus_demangle (part_die->linkage_name, DMGL_TYPES);
11874 if (demangled)
11875 {
96408a79
SA
11876 const char *base;
11877
11878 /* Strip any leading namespaces/classes, keep only the base name.
11879 DW_AT_name for named DIEs does not contain the prefixes. */
11880 base = strrchr (demangled, ':');
11881 if (base && base > demangled && base[-1] == ':')
11882 base++;
11883 else
11884 base = demangled;
11885
11886 part_die->name = obsavestring (base, strlen (base),
53832f31
TT
11887 &cu->objfile->objfile_obstack);
11888 xfree (demangled);
11889 }
11890 }
11891
abc72ce4 11892 part_die->fixup_called = 1;
72bf9492
DJ
11893}
11894
a8329558 11895/* Read an attribute value described by an attribute form. */
c906108c 11896
fe1b8b76 11897static gdb_byte *
dee91e82
DE
11898read_attribute_value (const struct die_reader_specs *reader,
11899 struct attribute *attr, unsigned form,
11900 gdb_byte *info_ptr)
c906108c 11901{
dee91e82
DE
11902 struct dwarf2_cu *cu = reader->cu;
11903 bfd *abfd = reader->abfd;
e7c27a73 11904 struct comp_unit_head *cu_header = &cu->header;
c906108c
SS
11905 unsigned int bytes_read;
11906 struct dwarf_block *blk;
11907
a8329558
KW
11908 attr->form = form;
11909 switch (form)
c906108c 11910 {
c906108c 11911 case DW_FORM_ref_addr:
ae411497 11912 if (cu->header.version == 2)
4568ecf9 11913 DW_UNSND (attr) = read_address (abfd, info_ptr, cu, &bytes_read);
ae411497 11914 else
4568ecf9
DE
11915 DW_UNSND (attr) = read_offset (abfd, info_ptr,
11916 &cu->header, &bytes_read);
ae411497
TT
11917 info_ptr += bytes_read;
11918 break;
11919 case DW_FORM_addr:
e7c27a73 11920 DW_ADDR (attr) = read_address (abfd, info_ptr, cu, &bytes_read);
107d2387 11921 info_ptr += bytes_read;
c906108c
SS
11922 break;
11923 case DW_FORM_block2:
7b5a2f43 11924 blk = dwarf_alloc_block (cu);
c906108c
SS
11925 blk->size = read_2_bytes (abfd, info_ptr);
11926 info_ptr += 2;
11927 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
11928 info_ptr += blk->size;
11929 DW_BLOCK (attr) = blk;
11930 break;
11931 case DW_FORM_block4:
7b5a2f43 11932 blk = dwarf_alloc_block (cu);
c906108c
SS
11933 blk->size = read_4_bytes (abfd, info_ptr);
11934 info_ptr += 4;
11935 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
11936 info_ptr += blk->size;
11937 DW_BLOCK (attr) = blk;
11938 break;
11939 case DW_FORM_data2:
11940 DW_UNSND (attr) = read_2_bytes (abfd, info_ptr);
11941 info_ptr += 2;
11942 break;
11943 case DW_FORM_data4:
11944 DW_UNSND (attr) = read_4_bytes (abfd, info_ptr);
11945 info_ptr += 4;
11946 break;
11947 case DW_FORM_data8:
11948 DW_UNSND (attr) = read_8_bytes (abfd, info_ptr);
11949 info_ptr += 8;
11950 break;
2dc7f7b3
TT
11951 case DW_FORM_sec_offset:
11952 DW_UNSND (attr) = read_offset (abfd, info_ptr, &cu->header, &bytes_read);
11953 info_ptr += bytes_read;
11954 break;
c906108c 11955 case DW_FORM_string:
9b1c24c8 11956 DW_STRING (attr) = read_direct_string (abfd, info_ptr, &bytes_read);
8285870a 11957 DW_STRING_IS_CANONICAL (attr) = 0;
c906108c
SS
11958 info_ptr += bytes_read;
11959 break;
4bdf3d34
JJ
11960 case DW_FORM_strp:
11961 DW_STRING (attr) = read_indirect_string (abfd, info_ptr, cu_header,
11962 &bytes_read);
8285870a 11963 DW_STRING_IS_CANONICAL (attr) = 0;
4bdf3d34
JJ
11964 info_ptr += bytes_read;
11965 break;
2dc7f7b3 11966 case DW_FORM_exprloc:
c906108c 11967 case DW_FORM_block:
7b5a2f43 11968 blk = dwarf_alloc_block (cu);
c906108c
SS
11969 blk->size = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
11970 info_ptr += bytes_read;
11971 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
11972 info_ptr += blk->size;
11973 DW_BLOCK (attr) = blk;
11974 break;
11975 case DW_FORM_block1:
7b5a2f43 11976 blk = dwarf_alloc_block (cu);
c906108c
SS
11977 blk->size = read_1_byte (abfd, info_ptr);
11978 info_ptr += 1;
11979 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
11980 info_ptr += blk->size;
11981 DW_BLOCK (attr) = blk;
11982 break;
11983 case DW_FORM_data1:
11984 DW_UNSND (attr) = read_1_byte (abfd, info_ptr);
11985 info_ptr += 1;
11986 break;
11987 case DW_FORM_flag:
11988 DW_UNSND (attr) = read_1_byte (abfd, info_ptr);
11989 info_ptr += 1;
11990 break;
2dc7f7b3
TT
11991 case DW_FORM_flag_present:
11992 DW_UNSND (attr) = 1;
11993 break;
c906108c
SS
11994 case DW_FORM_sdata:
11995 DW_SND (attr) = read_signed_leb128 (abfd, info_ptr, &bytes_read);
11996 info_ptr += bytes_read;
11997 break;
11998 case DW_FORM_udata:
11999 DW_UNSND (attr) = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
12000 info_ptr += bytes_read;
12001 break;
12002 case DW_FORM_ref1:
4568ecf9
DE
12003 DW_UNSND (attr) = (cu->header.offset.sect_off
12004 + read_1_byte (abfd, info_ptr));
c906108c
SS
12005 info_ptr += 1;
12006 break;
12007 case DW_FORM_ref2:
4568ecf9
DE
12008 DW_UNSND (attr) = (cu->header.offset.sect_off
12009 + read_2_bytes (abfd, info_ptr));
c906108c
SS
12010 info_ptr += 2;
12011 break;
12012 case DW_FORM_ref4:
4568ecf9
DE
12013 DW_UNSND (attr) = (cu->header.offset.sect_off
12014 + read_4_bytes (abfd, info_ptr));
c906108c
SS
12015 info_ptr += 4;
12016 break;
613e1657 12017 case DW_FORM_ref8:
4568ecf9
DE
12018 DW_UNSND (attr) = (cu->header.offset.sect_off
12019 + read_8_bytes (abfd, info_ptr));
613e1657
KB
12020 info_ptr += 8;
12021 break;
55f1336d 12022 case DW_FORM_ref_sig8:
348e048f
DE
12023 /* Convert the signature to something we can record in DW_UNSND
12024 for later lookup.
12025 NOTE: This is NULL if the type wasn't found. */
12026 DW_SIGNATURED_TYPE (attr) =
e319fa28 12027 lookup_signatured_type (read_8_bytes (abfd, info_ptr));
348e048f
DE
12028 info_ptr += 8;
12029 break;
c906108c 12030 case DW_FORM_ref_udata:
4568ecf9
DE
12031 DW_UNSND (attr) = (cu->header.offset.sect_off
12032 + read_unsigned_leb128 (abfd, info_ptr, &bytes_read));
c906108c
SS
12033 info_ptr += bytes_read;
12034 break;
c906108c 12035 case DW_FORM_indirect:
a8329558
KW
12036 form = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
12037 info_ptr += bytes_read;
dee91e82 12038 info_ptr = read_attribute_value (reader, attr, form, info_ptr);
a8329558 12039 break;
3019eac3
DE
12040 case DW_FORM_GNU_addr_index:
12041 if (reader->dwo_file == NULL)
12042 {
12043 /* For now flag a hard error.
12044 Later we can turn this into a complaint. */
12045 error (_("Dwarf Error: %s found in non-DWO CU [in module %s]"),
12046 dwarf_form_name (form),
12047 bfd_get_filename (abfd));
12048 }
12049 DW_ADDR (attr) = read_addr_index_from_leb128 (cu, info_ptr, &bytes_read);
12050 info_ptr += bytes_read;
12051 break;
12052 case DW_FORM_GNU_str_index:
12053 if (reader->dwo_file == NULL)
12054 {
12055 /* For now flag a hard error.
12056 Later we can turn this into a complaint if warranted. */
12057 error (_("Dwarf Error: %s found in non-DWO CU [in module %s]"),
12058 dwarf_form_name (form),
12059 bfd_get_filename (abfd));
12060 }
12061 {
12062 ULONGEST str_index =
12063 read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
12064
12065 DW_STRING (attr) = read_str_index (reader, cu, str_index);
12066 DW_STRING_IS_CANONICAL (attr) = 0;
12067 info_ptr += bytes_read;
12068 }
12069 break;
c906108c 12070 default:
8a3fe4f8 12071 error (_("Dwarf Error: Cannot handle %s in DWARF reader [in module %s]"),
659b0389
ML
12072 dwarf_form_name (form),
12073 bfd_get_filename (abfd));
c906108c 12074 }
28e94949
JB
12075
12076 /* We have seen instances where the compiler tried to emit a byte
12077 size attribute of -1 which ended up being encoded as an unsigned
12078 0xffffffff. Although 0xffffffff is technically a valid size value,
12079 an object of this size seems pretty unlikely so we can relatively
12080 safely treat these cases as if the size attribute was invalid and
12081 treat them as zero by default. */
12082 if (attr->name == DW_AT_byte_size
12083 && form == DW_FORM_data4
12084 && DW_UNSND (attr) >= 0xffffffff)
01c66ae6
JB
12085 {
12086 complaint
12087 (&symfile_complaints,
43bbcdc2
PH
12088 _("Suspicious DW_AT_byte_size value treated as zero instead of %s"),
12089 hex_string (DW_UNSND (attr)));
01c66ae6
JB
12090 DW_UNSND (attr) = 0;
12091 }
28e94949 12092
c906108c
SS
12093 return info_ptr;
12094}
12095
a8329558
KW
12096/* Read an attribute described by an abbreviated attribute. */
12097
fe1b8b76 12098static gdb_byte *
dee91e82
DE
12099read_attribute (const struct die_reader_specs *reader,
12100 struct attribute *attr, struct attr_abbrev *abbrev,
12101 gdb_byte *info_ptr)
a8329558
KW
12102{
12103 attr->name = abbrev->name;
dee91e82 12104 return read_attribute_value (reader, attr, abbrev->form, info_ptr);
a8329558
KW
12105}
12106
0963b4bd 12107/* Read dwarf information from a buffer. */
c906108c
SS
12108
12109static unsigned int
fe1b8b76 12110read_1_byte (bfd *abfd, gdb_byte *buf)
c906108c 12111{
fe1b8b76 12112 return bfd_get_8 (abfd, buf);
c906108c
SS
12113}
12114
12115static int
fe1b8b76 12116read_1_signed_byte (bfd *abfd, gdb_byte *buf)
c906108c 12117{
fe1b8b76 12118 return bfd_get_signed_8 (abfd, buf);
c906108c
SS
12119}
12120
12121static unsigned int
fe1b8b76 12122read_2_bytes (bfd *abfd, gdb_byte *buf)
c906108c 12123{
fe1b8b76 12124 return bfd_get_16 (abfd, buf);
c906108c
SS
12125}
12126
21ae7a4d
JK
12127static int
12128read_2_signed_bytes (bfd *abfd, gdb_byte *buf)
12129{
12130 return bfd_get_signed_16 (abfd, buf);
12131}
12132
c906108c 12133static unsigned int
fe1b8b76 12134read_4_bytes (bfd *abfd, gdb_byte *buf)
c906108c 12135{
fe1b8b76 12136 return bfd_get_32 (abfd, buf);
c906108c
SS
12137}
12138
21ae7a4d
JK
12139static int
12140read_4_signed_bytes (bfd *abfd, gdb_byte *buf)
12141{
12142 return bfd_get_signed_32 (abfd, buf);
12143}
12144
93311388 12145static ULONGEST
fe1b8b76 12146read_8_bytes (bfd *abfd, gdb_byte *buf)
c906108c 12147{
fe1b8b76 12148 return bfd_get_64 (abfd, buf);
c906108c
SS
12149}
12150
12151static CORE_ADDR
fe1b8b76 12152read_address (bfd *abfd, gdb_byte *buf, struct dwarf2_cu *cu,
891d2f0b 12153 unsigned int *bytes_read)
c906108c 12154{
e7c27a73 12155 struct comp_unit_head *cu_header = &cu->header;
c906108c
SS
12156 CORE_ADDR retval = 0;
12157
107d2387 12158 if (cu_header->signed_addr_p)
c906108c 12159 {
107d2387
AC
12160 switch (cu_header->addr_size)
12161 {
12162 case 2:
fe1b8b76 12163 retval = bfd_get_signed_16 (abfd, buf);
107d2387
AC
12164 break;
12165 case 4:
fe1b8b76 12166 retval = bfd_get_signed_32 (abfd, buf);
107d2387
AC
12167 break;
12168 case 8:
fe1b8b76 12169 retval = bfd_get_signed_64 (abfd, buf);
107d2387
AC
12170 break;
12171 default:
8e65ff28 12172 internal_error (__FILE__, __LINE__,
e2e0b3e5 12173 _("read_address: bad switch, signed [in module %s]"),
659b0389 12174 bfd_get_filename (abfd));
107d2387
AC
12175 }
12176 }
12177 else
12178 {
12179 switch (cu_header->addr_size)
12180 {
12181 case 2:
fe1b8b76 12182 retval = bfd_get_16 (abfd, buf);
107d2387
AC
12183 break;
12184 case 4:
fe1b8b76 12185 retval = bfd_get_32 (abfd, buf);
107d2387
AC
12186 break;
12187 case 8:
fe1b8b76 12188 retval = bfd_get_64 (abfd, buf);
107d2387
AC
12189 break;
12190 default:
8e65ff28 12191 internal_error (__FILE__, __LINE__,
a73c6dcd
MS
12192 _("read_address: bad switch, "
12193 "unsigned [in module %s]"),
659b0389 12194 bfd_get_filename (abfd));
107d2387 12195 }
c906108c 12196 }
64367e0a 12197
107d2387
AC
12198 *bytes_read = cu_header->addr_size;
12199 return retval;
c906108c
SS
12200}
12201
f7ef9339 12202/* Read the initial length from a section. The (draft) DWARF 3
613e1657
KB
12203 specification allows the initial length to take up either 4 bytes
12204 or 12 bytes. If the first 4 bytes are 0xffffffff, then the next 8
12205 bytes describe the length and all offsets will be 8 bytes in length
12206 instead of 4.
12207
f7ef9339
KB
12208 An older, non-standard 64-bit format is also handled by this
12209 function. The older format in question stores the initial length
12210 as an 8-byte quantity without an escape value. Lengths greater
12211 than 2^32 aren't very common which means that the initial 4 bytes
12212 is almost always zero. Since a length value of zero doesn't make
12213 sense for the 32-bit format, this initial zero can be considered to
12214 be an escape value which indicates the presence of the older 64-bit
12215 format. As written, the code can't detect (old format) lengths
917c78fc
MK
12216 greater than 4GB. If it becomes necessary to handle lengths
12217 somewhat larger than 4GB, we could allow other small values (such
12218 as the non-sensical values of 1, 2, and 3) to also be used as
12219 escape values indicating the presence of the old format.
f7ef9339 12220
917c78fc
MK
12221 The value returned via bytes_read should be used to increment the
12222 relevant pointer after calling read_initial_length().
c764a876 12223
613e1657
KB
12224 [ Note: read_initial_length() and read_offset() are based on the
12225 document entitled "DWARF Debugging Information Format", revision
f7ef9339 12226 3, draft 8, dated November 19, 2001. This document was obtained
613e1657
KB
12227 from:
12228
f7ef9339 12229 http://reality.sgiweb.org/davea/dwarf3-draft8-011125.pdf
6e70227d 12230
613e1657
KB
12231 This document is only a draft and is subject to change. (So beware.)
12232
f7ef9339 12233 Details regarding the older, non-standard 64-bit format were
917c78fc
MK
12234 determined empirically by examining 64-bit ELF files produced by
12235 the SGI toolchain on an IRIX 6.5 machine.
f7ef9339
KB
12236
12237 - Kevin, July 16, 2002
613e1657
KB
12238 ] */
12239
12240static LONGEST
c764a876 12241read_initial_length (bfd *abfd, gdb_byte *buf, unsigned int *bytes_read)
613e1657 12242{
fe1b8b76 12243 LONGEST length = bfd_get_32 (abfd, buf);
613e1657 12244
dd373385 12245 if (length == 0xffffffff)
613e1657 12246 {
fe1b8b76 12247 length = bfd_get_64 (abfd, buf + 4);
613e1657 12248 *bytes_read = 12;
613e1657 12249 }
dd373385 12250 else if (length == 0)
f7ef9339 12251 {
dd373385 12252 /* Handle the (non-standard) 64-bit DWARF2 format used by IRIX. */
fe1b8b76 12253 length = bfd_get_64 (abfd, buf);
f7ef9339 12254 *bytes_read = 8;
f7ef9339 12255 }
613e1657
KB
12256 else
12257 {
12258 *bytes_read = 4;
613e1657
KB
12259 }
12260
c764a876
DE
12261 return length;
12262}
dd373385 12263
c764a876
DE
12264/* Cover function for read_initial_length.
12265 Returns the length of the object at BUF, and stores the size of the
12266 initial length in *BYTES_READ and stores the size that offsets will be in
12267 *OFFSET_SIZE.
12268 If the initial length size is not equivalent to that specified in
12269 CU_HEADER then issue a complaint.
12270 This is useful when reading non-comp-unit headers. */
dd373385 12271
c764a876
DE
12272static LONGEST
12273read_checked_initial_length_and_offset (bfd *abfd, gdb_byte *buf,
12274 const struct comp_unit_head *cu_header,
12275 unsigned int *bytes_read,
12276 unsigned int *offset_size)
12277{
12278 LONGEST length = read_initial_length (abfd, buf, bytes_read);
12279
12280 gdb_assert (cu_header->initial_length_size == 4
12281 || cu_header->initial_length_size == 8
12282 || cu_header->initial_length_size == 12);
12283
12284 if (cu_header->initial_length_size != *bytes_read)
12285 complaint (&symfile_complaints,
12286 _("intermixed 32-bit and 64-bit DWARF sections"));
dd373385 12287
c764a876 12288 *offset_size = (*bytes_read == 4) ? 4 : 8;
dd373385 12289 return length;
613e1657
KB
12290}
12291
12292/* Read an offset from the data stream. The size of the offset is
917c78fc 12293 given by cu_header->offset_size. */
613e1657
KB
12294
12295static LONGEST
fe1b8b76 12296read_offset (bfd *abfd, gdb_byte *buf, const struct comp_unit_head *cu_header,
891d2f0b 12297 unsigned int *bytes_read)
c764a876
DE
12298{
12299 LONGEST offset = read_offset_1 (abfd, buf, cu_header->offset_size);
9a619af0 12300
c764a876
DE
12301 *bytes_read = cu_header->offset_size;
12302 return offset;
12303}
12304
12305/* Read an offset from the data stream. */
12306
12307static LONGEST
12308read_offset_1 (bfd *abfd, gdb_byte *buf, unsigned int offset_size)
613e1657
KB
12309{
12310 LONGEST retval = 0;
12311
c764a876 12312 switch (offset_size)
613e1657
KB
12313 {
12314 case 4:
fe1b8b76 12315 retval = bfd_get_32 (abfd, buf);
613e1657
KB
12316 break;
12317 case 8:
fe1b8b76 12318 retval = bfd_get_64 (abfd, buf);
613e1657
KB
12319 break;
12320 default:
8e65ff28 12321 internal_error (__FILE__, __LINE__,
c764a876 12322 _("read_offset_1: bad switch [in module %s]"),
659b0389 12323 bfd_get_filename (abfd));
613e1657
KB
12324 }
12325
917c78fc 12326 return retval;
613e1657
KB
12327}
12328
fe1b8b76
JB
12329static gdb_byte *
12330read_n_bytes (bfd *abfd, gdb_byte *buf, unsigned int size)
c906108c
SS
12331{
12332 /* If the size of a host char is 8 bits, we can return a pointer
12333 to the buffer, otherwise we have to copy the data to a buffer
12334 allocated on the temporary obstack. */
4bdf3d34 12335 gdb_assert (HOST_CHAR_BIT == 8);
c906108c 12336 return buf;
c906108c
SS
12337}
12338
12339static char *
9b1c24c8 12340read_direct_string (bfd *abfd, gdb_byte *buf, unsigned int *bytes_read_ptr)
c906108c
SS
12341{
12342 /* If the size of a host char is 8 bits, we can return a pointer
12343 to the string, otherwise we have to copy the string to a buffer
12344 allocated on the temporary obstack. */
4bdf3d34 12345 gdb_assert (HOST_CHAR_BIT == 8);
c906108c
SS
12346 if (*buf == '\0')
12347 {
12348 *bytes_read_ptr = 1;
12349 return NULL;
12350 }
fe1b8b76
JB
12351 *bytes_read_ptr = strlen ((char *) buf) + 1;
12352 return (char *) buf;
4bdf3d34
JJ
12353}
12354
12355static char *
cf2c3c16 12356read_indirect_string_at_offset (bfd *abfd, LONGEST str_offset)
4bdf3d34 12357{
be391dca 12358 dwarf2_read_section (dwarf2_per_objfile->objfile, &dwarf2_per_objfile->str);
dce234bc 12359 if (dwarf2_per_objfile->str.buffer == NULL)
cf2c3c16
TT
12360 error (_("DW_FORM_strp used without .debug_str section [in module %s]"),
12361 bfd_get_filename (abfd));
dce234bc 12362 if (str_offset >= dwarf2_per_objfile->str.size)
cf2c3c16
TT
12363 error (_("DW_FORM_strp pointing outside of "
12364 ".debug_str section [in module %s]"),
12365 bfd_get_filename (abfd));
4bdf3d34 12366 gdb_assert (HOST_CHAR_BIT == 8);
dce234bc 12367 if (dwarf2_per_objfile->str.buffer[str_offset] == '\0')
4bdf3d34 12368 return NULL;
dce234bc 12369 return (char *) (dwarf2_per_objfile->str.buffer + str_offset);
c906108c
SS
12370}
12371
cf2c3c16
TT
12372static char *
12373read_indirect_string (bfd *abfd, gdb_byte *buf,
12374 const struct comp_unit_head *cu_header,
12375 unsigned int *bytes_read_ptr)
12376{
12377 LONGEST str_offset = read_offset (abfd, buf, cu_header, bytes_read_ptr);
12378
12379 return read_indirect_string_at_offset (abfd, str_offset);
12380}
12381
12df843f 12382static ULONGEST
fe1b8b76 12383read_unsigned_leb128 (bfd *abfd, gdb_byte *buf, unsigned int *bytes_read_ptr)
c906108c 12384{
12df843f 12385 ULONGEST result;
ce5d95e1 12386 unsigned int num_read;
c906108c
SS
12387 int i, shift;
12388 unsigned char byte;
12389
12390 result = 0;
12391 shift = 0;
12392 num_read = 0;
12393 i = 0;
12394 while (1)
12395 {
fe1b8b76 12396 byte = bfd_get_8 (abfd, buf);
c906108c
SS
12397 buf++;
12398 num_read++;
12df843f 12399 result |= ((ULONGEST) (byte & 127) << shift);
c906108c
SS
12400 if ((byte & 128) == 0)
12401 {
12402 break;
12403 }
12404 shift += 7;
12405 }
12406 *bytes_read_ptr = num_read;
12407 return result;
12408}
12409
12df843f 12410static LONGEST
fe1b8b76 12411read_signed_leb128 (bfd *abfd, gdb_byte *buf, unsigned int *bytes_read_ptr)
c906108c 12412{
12df843f 12413 LONGEST result;
77e0b926 12414 int i, shift, num_read;
c906108c
SS
12415 unsigned char byte;
12416
12417 result = 0;
12418 shift = 0;
c906108c
SS
12419 num_read = 0;
12420 i = 0;
12421 while (1)
12422 {
fe1b8b76 12423 byte = bfd_get_8 (abfd, buf);
c906108c
SS
12424 buf++;
12425 num_read++;
12df843f 12426 result |= ((LONGEST) (byte & 127) << shift);
c906108c
SS
12427 shift += 7;
12428 if ((byte & 128) == 0)
12429 {
12430 break;
12431 }
12432 }
77e0b926 12433 if ((shift < 8 * sizeof (result)) && (byte & 0x40))
12df843f 12434 result |= -(((LONGEST) 1) << shift);
c906108c
SS
12435 *bytes_read_ptr = num_read;
12436 return result;
12437}
12438
3019eac3
DE
12439/* Given index ADDR_INDEX in .debug_addr, fetch the value.
12440 ADDR_BASE is the DW_AT_GNU_addr_base attribute or zero.
12441 ADDR_SIZE is the size of addresses from the CU header. */
12442
12443static CORE_ADDR
12444read_addr_index_1 (unsigned int addr_index, ULONGEST addr_base, int addr_size)
12445{
12446 struct objfile *objfile = dwarf2_per_objfile->objfile;
12447 bfd *abfd = objfile->obfd;
12448 const gdb_byte *info_ptr;
12449
12450 dwarf2_read_section (objfile, &dwarf2_per_objfile->addr);
12451 if (dwarf2_per_objfile->addr.buffer == NULL)
12452 error (_("DW_FORM_addr_index used without .debug_addr section [in module %s]"),
12453 objfile->name);
12454 if (addr_base + addr_index * addr_size >= dwarf2_per_objfile->addr.size)
12455 error (_("DW_FORM_addr_index pointing outside of "
12456 ".debug_addr section [in module %s]"),
12457 objfile->name);
12458 info_ptr = (dwarf2_per_objfile->addr.buffer
12459 + addr_base + addr_index * addr_size);
12460 if (addr_size == 4)
12461 return bfd_get_32 (abfd, info_ptr);
12462 else
12463 return bfd_get_64 (abfd, info_ptr);
12464}
12465
12466/* Given index ADDR_INDEX in .debug_addr, fetch the value. */
12467
12468static CORE_ADDR
12469read_addr_index (struct dwarf2_cu *cu, unsigned int addr_index)
12470{
12471 return read_addr_index_1 (addr_index, cu->addr_base, cu->header.addr_size);
12472}
12473
12474/* Given a pointer to an leb128 value, fetch the value from .debug_addr. */
12475
12476static CORE_ADDR
12477read_addr_index_from_leb128 (struct dwarf2_cu *cu, gdb_byte *info_ptr,
12478 unsigned int *bytes_read)
12479{
12480 bfd *abfd = cu->objfile->obfd;
12481 unsigned int addr_index = read_unsigned_leb128 (abfd, info_ptr, bytes_read);
12482
12483 return read_addr_index (cu, addr_index);
12484}
12485
12486/* Data structure to pass results from dwarf2_read_addr_index_reader
12487 back to dwarf2_read_addr_index. */
12488
12489struct dwarf2_read_addr_index_data
12490{
12491 ULONGEST addr_base;
12492 int addr_size;
12493};
12494
12495/* die_reader_func for dwarf2_read_addr_index. */
12496
12497static void
12498dwarf2_read_addr_index_reader (const struct die_reader_specs *reader,
12499 gdb_byte *info_ptr,
12500 struct die_info *comp_unit_die,
12501 int has_children,
12502 void *data)
12503{
12504 struct dwarf2_cu *cu = reader->cu;
12505 struct dwarf2_read_addr_index_data *aidata =
12506 (struct dwarf2_read_addr_index_data *) data;
12507
12508 aidata->addr_base = cu->addr_base;
12509 aidata->addr_size = cu->header.addr_size;
12510}
12511
12512/* Given an index in .debug_addr, fetch the value.
12513 NOTE: This can be called during dwarf expression evaluation,
12514 long after the debug information has been read, and thus per_cu->cu
12515 may no longer exist. */
12516
12517CORE_ADDR
12518dwarf2_read_addr_index (struct dwarf2_per_cu_data *per_cu,
12519 unsigned int addr_index)
12520{
12521 struct objfile *objfile = per_cu->objfile;
12522 struct dwarf2_cu *cu = per_cu->cu;
12523 ULONGEST addr_base;
12524 int addr_size;
12525
12526 /* This is intended to be called from outside this file. */
12527 dw2_setup (objfile);
12528
12529 /* We need addr_base and addr_size.
12530 If we don't have PER_CU->cu, we have to get it.
12531 Nasty, but the alternative is storing the needed info in PER_CU,
12532 which at this point doesn't seem justified: it's not clear how frequently
12533 it would get used and it would increase the size of every PER_CU.
12534 Entry points like dwarf2_per_cu_addr_size do a similar thing
12535 so we're not in uncharted territory here.
12536 Alas we need to be a bit more complicated as addr_base is contained
12537 in the DIE.
12538
12539 We don't need to read the entire CU(/TU).
12540 We just need the header and top level die.
12541 IWBN to use the aging mechanism to let us lazily later discard the CU.
12542 See however init_cutu_and_read_dies_simple. */
12543
12544 if (cu != NULL)
12545 {
12546 addr_base = cu->addr_base;
12547 addr_size = cu->header.addr_size;
12548 }
12549 else
12550 {
12551 struct dwarf2_read_addr_index_data aidata;
12552
12553 init_cutu_and_read_dies_simple (per_cu, dwarf2_read_addr_index_reader,
12554 &aidata);
12555 addr_base = aidata.addr_base;
12556 addr_size = aidata.addr_size;
12557 }
12558
12559 return read_addr_index_1 (addr_index, addr_base, addr_size);
12560}
12561
12562/* Given a DW_AT_str_index, fetch the string. */
12563
12564static char *
12565read_str_index (const struct die_reader_specs *reader,
12566 struct dwarf2_cu *cu, ULONGEST str_index)
12567{
12568 struct objfile *objfile = dwarf2_per_objfile->objfile;
12569 const char *dwo_name = objfile->name;
12570 bfd *abfd = objfile->obfd;
12571 struct dwo_sections *sections = &reader->dwo_file->sections;
12572 gdb_byte *info_ptr;
12573 ULONGEST str_offset;
12574
12575 dwarf2_read_section (objfile, &sections->str);
12576 dwarf2_read_section (objfile, &sections->str_offsets);
12577 if (sections->str.buffer == NULL)
12578 error (_("DW_FORM_str_index used without .debug_str.dwo section"
12579 " in CU at offset 0x%lx [in module %s]"),
12580 (long) cu->header.offset.sect_off, dwo_name);
12581 if (sections->str_offsets.buffer == NULL)
12582 error (_("DW_FORM_str_index used without .debug_str_offsets.dwo section"
12583 " in CU at offset 0x%lx [in module %s]"),
12584 (long) cu->header.offset.sect_off, dwo_name);
12585 if (str_index * cu->header.offset_size >= sections->str_offsets.size)
12586 error (_("DW_FORM_str_index pointing outside of .debug_str_offsets.dwo"
12587 " section in CU at offset 0x%lx [in module %s]"),
12588 (long) cu->header.offset.sect_off, dwo_name);
12589 info_ptr = (sections->str_offsets.buffer
12590 + str_index * cu->header.offset_size);
12591 if (cu->header.offset_size == 4)
12592 str_offset = bfd_get_32 (abfd, info_ptr);
12593 else
12594 str_offset = bfd_get_64 (abfd, info_ptr);
12595 if (str_offset >= sections->str.size)
12596 error (_("Offset from DW_FORM_str_index pointing outside of"
12597 " .debug_str.dwo section in CU at offset 0x%lx [in module %s]"),
12598 (long) cu->header.offset.sect_off, dwo_name);
12599 return (char *) (sections->str.buffer + str_offset);
12600}
12601
3019eac3
DE
12602/* Return the length of an LEB128 number in BUF. */
12603
12604static int
12605leb128_size (const gdb_byte *buf)
12606{
12607 const gdb_byte *begin = buf;
12608 gdb_byte byte;
12609
12610 while (1)
12611 {
12612 byte = *buf++;
12613 if ((byte & 128) == 0)
12614 return buf - begin;
12615 }
12616}
12617
c906108c 12618static void
e142c38c 12619set_cu_language (unsigned int lang, struct dwarf2_cu *cu)
c906108c
SS
12620{
12621 switch (lang)
12622 {
12623 case DW_LANG_C89:
76bee0cc 12624 case DW_LANG_C99:
c906108c 12625 case DW_LANG_C:
e142c38c 12626 cu->language = language_c;
c906108c
SS
12627 break;
12628 case DW_LANG_C_plus_plus:
e142c38c 12629 cu->language = language_cplus;
c906108c 12630 break;
6aecb9c2
JB
12631 case DW_LANG_D:
12632 cu->language = language_d;
12633 break;
c906108c
SS
12634 case DW_LANG_Fortran77:
12635 case DW_LANG_Fortran90:
b21b22e0 12636 case DW_LANG_Fortran95:
e142c38c 12637 cu->language = language_fortran;
c906108c 12638 break;
a766d390
DE
12639 case DW_LANG_Go:
12640 cu->language = language_go;
12641 break;
c906108c 12642 case DW_LANG_Mips_Assembler:
e142c38c 12643 cu->language = language_asm;
c906108c 12644 break;
bebd888e 12645 case DW_LANG_Java:
e142c38c 12646 cu->language = language_java;
bebd888e 12647 break;
c906108c 12648 case DW_LANG_Ada83:
8aaf0b47 12649 case DW_LANG_Ada95:
bc5f45f8
JB
12650 cu->language = language_ada;
12651 break;
72019c9c
GM
12652 case DW_LANG_Modula2:
12653 cu->language = language_m2;
12654 break;
fe8e67fd
PM
12655 case DW_LANG_Pascal83:
12656 cu->language = language_pascal;
12657 break;
22566fbd
DJ
12658 case DW_LANG_ObjC:
12659 cu->language = language_objc;
12660 break;
c906108c
SS
12661 case DW_LANG_Cobol74:
12662 case DW_LANG_Cobol85:
c906108c 12663 default:
e142c38c 12664 cu->language = language_minimal;
c906108c
SS
12665 break;
12666 }
e142c38c 12667 cu->language_defn = language_def (cu->language);
c906108c
SS
12668}
12669
12670/* Return the named attribute or NULL if not there. */
12671
12672static struct attribute *
e142c38c 12673dwarf2_attr (struct die_info *die, unsigned int name, struct dwarf2_cu *cu)
c906108c 12674{
a48e046c 12675 for (;;)
c906108c 12676 {
a48e046c
TT
12677 unsigned int i;
12678 struct attribute *spec = NULL;
12679
12680 for (i = 0; i < die->num_attrs; ++i)
12681 {
12682 if (die->attrs[i].name == name)
12683 return &die->attrs[i];
12684 if (die->attrs[i].name == DW_AT_specification
12685 || die->attrs[i].name == DW_AT_abstract_origin)
12686 spec = &die->attrs[i];
12687 }
12688
12689 if (!spec)
12690 break;
c906108c 12691
f2f0e013 12692 die = follow_die_ref (die, spec, &cu);
f2f0e013 12693 }
c5aa993b 12694
c906108c
SS
12695 return NULL;
12696}
12697
348e048f
DE
12698/* Return the named attribute or NULL if not there,
12699 but do not follow DW_AT_specification, etc.
12700 This is for use in contexts where we're reading .debug_types dies.
12701 Following DW_AT_specification, DW_AT_abstract_origin will take us
12702 back up the chain, and we want to go down. */
12703
12704static struct attribute *
12705dwarf2_attr_no_follow (struct die_info *die, unsigned int name,
12706 struct dwarf2_cu *cu)
12707{
12708 unsigned int i;
12709
12710 for (i = 0; i < die->num_attrs; ++i)
12711 if (die->attrs[i].name == name)
12712 return &die->attrs[i];
12713
12714 return NULL;
12715}
12716
05cf31d1
JB
12717/* Return non-zero iff the attribute NAME is defined for the given DIE,
12718 and holds a non-zero value. This function should only be used for
2dc7f7b3 12719 DW_FORM_flag or DW_FORM_flag_present attributes. */
05cf31d1
JB
12720
12721static int
12722dwarf2_flag_true_p (struct die_info *die, unsigned name, struct dwarf2_cu *cu)
12723{
12724 struct attribute *attr = dwarf2_attr (die, name, cu);
12725
12726 return (attr && DW_UNSND (attr));
12727}
12728
3ca72b44 12729static int
e142c38c 12730die_is_declaration (struct die_info *die, struct dwarf2_cu *cu)
3ca72b44 12731{
05cf31d1
JB
12732 /* A DIE is a declaration if it has a DW_AT_declaration attribute
12733 which value is non-zero. However, we have to be careful with
12734 DIEs having a DW_AT_specification attribute, because dwarf2_attr()
12735 (via dwarf2_flag_true_p) follows this attribute. So we may
12736 end up accidently finding a declaration attribute that belongs
12737 to a different DIE referenced by the specification attribute,
12738 even though the given DIE does not have a declaration attribute. */
12739 return (dwarf2_flag_true_p (die, DW_AT_declaration, cu)
12740 && dwarf2_attr (die, DW_AT_specification, cu) == NULL);
3ca72b44
AC
12741}
12742
63d06c5c 12743/* Return the die giving the specification for DIE, if there is
f2f0e013 12744 one. *SPEC_CU is the CU containing DIE on input, and the CU
edb3359d
DJ
12745 containing the return value on output. If there is no
12746 specification, but there is an abstract origin, that is
12747 returned. */
63d06c5c
DC
12748
12749static struct die_info *
f2f0e013 12750die_specification (struct die_info *die, struct dwarf2_cu **spec_cu)
63d06c5c 12751{
f2f0e013
DJ
12752 struct attribute *spec_attr = dwarf2_attr (die, DW_AT_specification,
12753 *spec_cu);
63d06c5c 12754
edb3359d
DJ
12755 if (spec_attr == NULL)
12756 spec_attr = dwarf2_attr (die, DW_AT_abstract_origin, *spec_cu);
12757
63d06c5c
DC
12758 if (spec_attr == NULL)
12759 return NULL;
12760 else
f2f0e013 12761 return follow_die_ref (die, spec_attr, spec_cu);
63d06c5c 12762}
c906108c 12763
debd256d 12764/* Free the line_header structure *LH, and any arrays and strings it
ae2de4f8
DE
12765 refers to.
12766 NOTE: This is also used as a "cleanup" function. */
12767
debd256d
JB
12768static void
12769free_line_header (struct line_header *lh)
12770{
12771 if (lh->standard_opcode_lengths)
a8bc7b56 12772 xfree (lh->standard_opcode_lengths);
debd256d
JB
12773
12774 /* Remember that all the lh->file_names[i].name pointers are
12775 pointers into debug_line_buffer, and don't need to be freed. */
12776 if (lh->file_names)
a8bc7b56 12777 xfree (lh->file_names);
debd256d
JB
12778
12779 /* Similarly for the include directory names. */
12780 if (lh->include_dirs)
a8bc7b56 12781 xfree (lh->include_dirs);
debd256d 12782
a8bc7b56 12783 xfree (lh);
debd256d
JB
12784}
12785
debd256d 12786/* Add an entry to LH's include directory table. */
ae2de4f8 12787
debd256d
JB
12788static void
12789add_include_dir (struct line_header *lh, char *include_dir)
c906108c 12790{
debd256d
JB
12791 /* Grow the array if necessary. */
12792 if (lh->include_dirs_size == 0)
c5aa993b 12793 {
debd256d
JB
12794 lh->include_dirs_size = 1; /* for testing */
12795 lh->include_dirs = xmalloc (lh->include_dirs_size
12796 * sizeof (*lh->include_dirs));
12797 }
12798 else if (lh->num_include_dirs >= lh->include_dirs_size)
12799 {
12800 lh->include_dirs_size *= 2;
12801 lh->include_dirs = xrealloc (lh->include_dirs,
12802 (lh->include_dirs_size
12803 * sizeof (*lh->include_dirs)));
c5aa993b 12804 }
c906108c 12805
debd256d
JB
12806 lh->include_dirs[lh->num_include_dirs++] = include_dir;
12807}
6e70227d 12808
debd256d 12809/* Add an entry to LH's file name table. */
ae2de4f8 12810
debd256d
JB
12811static void
12812add_file_name (struct line_header *lh,
12813 char *name,
12814 unsigned int dir_index,
12815 unsigned int mod_time,
12816 unsigned int length)
12817{
12818 struct file_entry *fe;
12819
12820 /* Grow the array if necessary. */
12821 if (lh->file_names_size == 0)
12822 {
12823 lh->file_names_size = 1; /* for testing */
12824 lh->file_names = xmalloc (lh->file_names_size
12825 * sizeof (*lh->file_names));
12826 }
12827 else if (lh->num_file_names >= lh->file_names_size)
12828 {
12829 lh->file_names_size *= 2;
12830 lh->file_names = xrealloc (lh->file_names,
12831 (lh->file_names_size
12832 * sizeof (*lh->file_names)));
12833 }
12834
12835 fe = &lh->file_names[lh->num_file_names++];
12836 fe->name = name;
12837 fe->dir_index = dir_index;
12838 fe->mod_time = mod_time;
12839 fe->length = length;
aaa75496 12840 fe->included_p = 0;
cb1df416 12841 fe->symtab = NULL;
debd256d 12842}
6e70227d 12843
debd256d 12844/* Read the statement program header starting at OFFSET in
3019eac3 12845 .debug_line, or .debug_line.dwo. Return a pointer
6502dd73 12846 to a struct line_header, allocated using xmalloc.
debd256d
JB
12847
12848 NOTE: the strings in the include directory and file name tables of
3019eac3
DE
12849 the returned object point into the dwarf line section buffer,
12850 and must not be freed. */
ae2de4f8 12851
debd256d 12852static struct line_header *
3019eac3 12853dwarf_decode_line_header (unsigned int offset, struct dwarf2_cu *cu)
debd256d
JB
12854{
12855 struct cleanup *back_to;
12856 struct line_header *lh;
fe1b8b76 12857 gdb_byte *line_ptr;
c764a876 12858 unsigned int bytes_read, offset_size;
debd256d
JB
12859 int i;
12860 char *cur_dir, *cur_file;
3019eac3
DE
12861 struct dwarf2_section_info *section;
12862 bfd *abfd;
12863
12864 /* For TUs in DWO files, the DW_AT_stmt_list attribute lives in the
12865 DWO file. */
12866 if (cu->dwo_unit && cu->per_cu->is_debug_types)
12867 section = &cu->dwo_unit->dwo_file->sections.line;
12868 else
12869 section = &dwarf2_per_objfile->line;
debd256d 12870
3019eac3
DE
12871 dwarf2_read_section (dwarf2_per_objfile->objfile, section);
12872 if (section->buffer == NULL)
debd256d 12873 {
3019eac3
DE
12874 if (cu->dwo_unit && cu->per_cu->is_debug_types)
12875 complaint (&symfile_complaints, _("missing .debug_line.dwo section"));
12876 else
12877 complaint (&symfile_complaints, _("missing .debug_line section"));
debd256d
JB
12878 return 0;
12879 }
12880
fceca515
DE
12881 /* We can't do this until we know the section is non-empty.
12882 Only then do we know we have such a section. */
12883 abfd = section->asection->owner;
12884
a738430d
MK
12885 /* Make sure that at least there's room for the total_length field.
12886 That could be 12 bytes long, but we're just going to fudge that. */
3019eac3 12887 if (offset + 4 >= section->size)
debd256d 12888 {
4d3c2250 12889 dwarf2_statement_list_fits_in_line_number_section_complaint ();
debd256d
JB
12890 return 0;
12891 }
12892
12893 lh = xmalloc (sizeof (*lh));
12894 memset (lh, 0, sizeof (*lh));
12895 back_to = make_cleanup ((make_cleanup_ftype *) free_line_header,
12896 (void *) lh);
12897
3019eac3 12898 line_ptr = section->buffer + offset;
debd256d 12899
a738430d 12900 /* Read in the header. */
6e70227d 12901 lh->total_length =
c764a876
DE
12902 read_checked_initial_length_and_offset (abfd, line_ptr, &cu->header,
12903 &bytes_read, &offset_size);
debd256d 12904 line_ptr += bytes_read;
3019eac3 12905 if (line_ptr + lh->total_length > (section->buffer + section->size))
debd256d 12906 {
4d3c2250 12907 dwarf2_statement_list_fits_in_line_number_section_complaint ();
debd256d
JB
12908 return 0;
12909 }
12910 lh->statement_program_end = line_ptr + lh->total_length;
12911 lh->version = read_2_bytes (abfd, line_ptr);
12912 line_ptr += 2;
c764a876
DE
12913 lh->header_length = read_offset_1 (abfd, line_ptr, offset_size);
12914 line_ptr += offset_size;
debd256d
JB
12915 lh->minimum_instruction_length = read_1_byte (abfd, line_ptr);
12916 line_ptr += 1;
2dc7f7b3
TT
12917 if (lh->version >= 4)
12918 {
12919 lh->maximum_ops_per_instruction = read_1_byte (abfd, line_ptr);
12920 line_ptr += 1;
12921 }
12922 else
12923 lh->maximum_ops_per_instruction = 1;
12924
12925 if (lh->maximum_ops_per_instruction == 0)
12926 {
12927 lh->maximum_ops_per_instruction = 1;
12928 complaint (&symfile_complaints,
3e43a32a
MS
12929 _("invalid maximum_ops_per_instruction "
12930 "in `.debug_line' section"));
2dc7f7b3
TT
12931 }
12932
debd256d
JB
12933 lh->default_is_stmt = read_1_byte (abfd, line_ptr);
12934 line_ptr += 1;
12935 lh->line_base = read_1_signed_byte (abfd, line_ptr);
12936 line_ptr += 1;
12937 lh->line_range = read_1_byte (abfd, line_ptr);
12938 line_ptr += 1;
12939 lh->opcode_base = read_1_byte (abfd, line_ptr);
12940 line_ptr += 1;
12941 lh->standard_opcode_lengths
fe1b8b76 12942 = xmalloc (lh->opcode_base * sizeof (lh->standard_opcode_lengths[0]));
debd256d
JB
12943
12944 lh->standard_opcode_lengths[0] = 1; /* This should never be used anyway. */
12945 for (i = 1; i < lh->opcode_base; ++i)
12946 {
12947 lh->standard_opcode_lengths[i] = read_1_byte (abfd, line_ptr);
12948 line_ptr += 1;
12949 }
12950
a738430d 12951 /* Read directory table. */
9b1c24c8 12952 while ((cur_dir = read_direct_string (abfd, line_ptr, &bytes_read)) != NULL)
debd256d
JB
12953 {
12954 line_ptr += bytes_read;
12955 add_include_dir (lh, cur_dir);
12956 }
12957 line_ptr += bytes_read;
12958
a738430d 12959 /* Read file name table. */
9b1c24c8 12960 while ((cur_file = read_direct_string (abfd, line_ptr, &bytes_read)) != NULL)
debd256d
JB
12961 {
12962 unsigned int dir_index, mod_time, length;
12963
12964 line_ptr += bytes_read;
12965 dir_index = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
12966 line_ptr += bytes_read;
12967 mod_time = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
12968 line_ptr += bytes_read;
12969 length = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
12970 line_ptr += bytes_read;
12971
12972 add_file_name (lh, cur_file, dir_index, mod_time, length);
12973 }
12974 line_ptr += bytes_read;
6e70227d 12975 lh->statement_program_start = line_ptr;
debd256d 12976
3019eac3 12977 if (line_ptr > (section->buffer + section->size))
4d3c2250 12978 complaint (&symfile_complaints,
3e43a32a
MS
12979 _("line number info header doesn't "
12980 "fit in `.debug_line' section"));
debd256d
JB
12981
12982 discard_cleanups (back_to);
12983 return lh;
12984}
c906108c 12985
c6da4cef
DE
12986/* Subroutine of dwarf_decode_lines to simplify it.
12987 Return the file name of the psymtab for included file FILE_INDEX
12988 in line header LH of PST.
12989 COMP_DIR is the compilation directory (DW_AT_comp_dir) or NULL if unknown.
12990 If space for the result is malloc'd, it will be freed by a cleanup.
12991 Returns NULL if FILE_INDEX should be ignored, i.e., it is pst->filename. */
12992
12993static char *
12994psymtab_include_file_name (const struct line_header *lh, int file_index,
12995 const struct partial_symtab *pst,
12996 const char *comp_dir)
12997{
12998 const struct file_entry fe = lh->file_names [file_index];
12999 char *include_name = fe.name;
13000 char *include_name_to_compare = include_name;
13001 char *dir_name = NULL;
72b9f47f
TT
13002 const char *pst_filename;
13003 char *copied_name = NULL;
c6da4cef
DE
13004 int file_is_pst;
13005
13006 if (fe.dir_index)
13007 dir_name = lh->include_dirs[fe.dir_index - 1];
13008
13009 if (!IS_ABSOLUTE_PATH (include_name)
13010 && (dir_name != NULL || comp_dir != NULL))
13011 {
13012 /* Avoid creating a duplicate psymtab for PST.
13013 We do this by comparing INCLUDE_NAME and PST_FILENAME.
13014 Before we do the comparison, however, we need to account
13015 for DIR_NAME and COMP_DIR.
13016 First prepend dir_name (if non-NULL). If we still don't
13017 have an absolute path prepend comp_dir (if non-NULL).
13018 However, the directory we record in the include-file's
13019 psymtab does not contain COMP_DIR (to match the
13020 corresponding symtab(s)).
13021
13022 Example:
13023
13024 bash$ cd /tmp
13025 bash$ gcc -g ./hello.c
13026 include_name = "hello.c"
13027 dir_name = "."
13028 DW_AT_comp_dir = comp_dir = "/tmp"
13029 DW_AT_name = "./hello.c" */
13030
13031 if (dir_name != NULL)
13032 {
13033 include_name = concat (dir_name, SLASH_STRING,
13034 include_name, (char *)NULL);
13035 include_name_to_compare = include_name;
13036 make_cleanup (xfree, include_name);
13037 }
13038 if (!IS_ABSOLUTE_PATH (include_name) && comp_dir != NULL)
13039 {
13040 include_name_to_compare = concat (comp_dir, SLASH_STRING,
13041 include_name, (char *)NULL);
13042 }
13043 }
13044
13045 pst_filename = pst->filename;
13046 if (!IS_ABSOLUTE_PATH (pst_filename) && pst->dirname != NULL)
13047 {
72b9f47f
TT
13048 copied_name = concat (pst->dirname, SLASH_STRING,
13049 pst_filename, (char *)NULL);
13050 pst_filename = copied_name;
c6da4cef
DE
13051 }
13052
1e3fad37 13053 file_is_pst = FILENAME_CMP (include_name_to_compare, pst_filename) == 0;
c6da4cef
DE
13054
13055 if (include_name_to_compare != include_name)
13056 xfree (include_name_to_compare);
72b9f47f
TT
13057 if (copied_name != NULL)
13058 xfree (copied_name);
c6da4cef
DE
13059
13060 if (file_is_pst)
13061 return NULL;
13062 return include_name;
13063}
13064
c91513d8
PP
13065/* Ignore this record_line request. */
13066
13067static void
13068noop_record_line (struct subfile *subfile, int line, CORE_ADDR pc)
13069{
13070 return;
13071}
13072
f3f5162e
DE
13073/* Subroutine of dwarf_decode_lines to simplify it.
13074 Process the line number information in LH. */
debd256d 13075
c906108c 13076static void
f3f5162e
DE
13077dwarf_decode_lines_1 (struct line_header *lh, const char *comp_dir,
13078 struct dwarf2_cu *cu, struct partial_symtab *pst)
c906108c 13079{
a8c50c1f 13080 gdb_byte *line_ptr, *extended_end;
fe1b8b76 13081 gdb_byte *line_end;
a8c50c1f 13082 unsigned int bytes_read, extended_len;
c906108c 13083 unsigned char op_code, extended_op, adj_opcode;
e142c38c
DJ
13084 CORE_ADDR baseaddr;
13085 struct objfile *objfile = cu->objfile;
f3f5162e 13086 bfd *abfd = objfile->obfd;
fbf65064 13087 struct gdbarch *gdbarch = get_objfile_arch (objfile);
aaa75496 13088 const int decode_for_pst_p = (pst != NULL);
f3f5162e 13089 struct subfile *last_subfile = NULL;
c91513d8
PP
13090 void (*p_record_line) (struct subfile *subfile, int line, CORE_ADDR pc)
13091 = record_line;
e142c38c
DJ
13092
13093 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
c906108c 13094
debd256d
JB
13095 line_ptr = lh->statement_program_start;
13096 line_end = lh->statement_program_end;
c906108c
SS
13097
13098 /* Read the statement sequences until there's nothing left. */
13099 while (line_ptr < line_end)
13100 {
13101 /* state machine registers */
13102 CORE_ADDR address = 0;
13103 unsigned int file = 1;
13104 unsigned int line = 1;
13105 unsigned int column = 0;
debd256d 13106 int is_stmt = lh->default_is_stmt;
c906108c
SS
13107 int basic_block = 0;
13108 int end_sequence = 0;
fbf65064 13109 CORE_ADDR addr;
2dc7f7b3 13110 unsigned char op_index = 0;
c906108c 13111
aaa75496 13112 if (!decode_for_pst_p && lh->num_file_names >= file)
c906108c 13113 {
aaa75496 13114 /* Start a subfile for the current file of the state machine. */
debd256d
JB
13115 /* lh->include_dirs and lh->file_names are 0-based, but the
13116 directory and file name numbers in the statement program
13117 are 1-based. */
13118 struct file_entry *fe = &lh->file_names[file - 1];
4f1520fb 13119 char *dir = NULL;
a738430d 13120
debd256d
JB
13121 if (fe->dir_index)
13122 dir = lh->include_dirs[fe->dir_index - 1];
4f1520fb
FR
13123
13124 dwarf2_start_subfile (fe->name, dir, comp_dir);
c906108c
SS
13125 }
13126
a738430d 13127 /* Decode the table. */
c5aa993b 13128 while (!end_sequence)
c906108c
SS
13129 {
13130 op_code = read_1_byte (abfd, line_ptr);
13131 line_ptr += 1;
59205f5a
JB
13132 if (line_ptr > line_end)
13133 {
13134 dwarf2_debug_line_missing_end_sequence_complaint ();
13135 break;
13136 }
9aa1fe7e 13137
debd256d 13138 if (op_code >= lh->opcode_base)
6e70227d 13139 {
a738430d 13140 /* Special operand. */
debd256d 13141 adj_opcode = op_code - lh->opcode_base;
2dc7f7b3
TT
13142 address += (((op_index + (adj_opcode / lh->line_range))
13143 / lh->maximum_ops_per_instruction)
13144 * lh->minimum_instruction_length);
13145 op_index = ((op_index + (adj_opcode / lh->line_range))
13146 % lh->maximum_ops_per_instruction);
debd256d 13147 line += lh->line_base + (adj_opcode % lh->line_range);
59205f5a 13148 if (lh->num_file_names < file || file == 0)
25e43795 13149 dwarf2_debug_line_missing_file_complaint ();
2dc7f7b3
TT
13150 /* For now we ignore lines not starting on an
13151 instruction boundary. */
13152 else if (op_index == 0)
25e43795
DJ
13153 {
13154 lh->file_names[file - 1].included_p = 1;
ca5f395d 13155 if (!decode_for_pst_p && is_stmt)
fbf65064
UW
13156 {
13157 if (last_subfile != current_subfile)
13158 {
13159 addr = gdbarch_addr_bits_remove (gdbarch, address);
13160 if (last_subfile)
c91513d8 13161 (*p_record_line) (last_subfile, 0, addr);
fbf65064
UW
13162 last_subfile = current_subfile;
13163 }
25e43795 13164 /* Append row to matrix using current values. */
7019d805 13165 addr = gdbarch_addr_bits_remove (gdbarch, address);
c91513d8 13166 (*p_record_line) (current_subfile, line, addr);
366da635 13167 }
25e43795 13168 }
ca5f395d 13169 basic_block = 0;
9aa1fe7e
GK
13170 }
13171 else switch (op_code)
c906108c
SS
13172 {
13173 case DW_LNS_extended_op:
3e43a32a
MS
13174 extended_len = read_unsigned_leb128 (abfd, line_ptr,
13175 &bytes_read);
473b7be6 13176 line_ptr += bytes_read;
a8c50c1f 13177 extended_end = line_ptr + extended_len;
c906108c
SS
13178 extended_op = read_1_byte (abfd, line_ptr);
13179 line_ptr += 1;
13180 switch (extended_op)
13181 {
13182 case DW_LNE_end_sequence:
c91513d8 13183 p_record_line = record_line;
c906108c 13184 end_sequence = 1;
c906108c
SS
13185 break;
13186 case DW_LNE_set_address:
e7c27a73 13187 address = read_address (abfd, line_ptr, cu, &bytes_read);
c91513d8
PP
13188
13189 if (address == 0 && !dwarf2_per_objfile->has_section_at_zero)
13190 {
13191 /* This line table is for a function which has been
13192 GCd by the linker. Ignore it. PR gdb/12528 */
13193
13194 long line_offset
13195 = line_ptr - dwarf2_per_objfile->line.buffer;
13196
13197 complaint (&symfile_complaints,
13198 _(".debug_line address at offset 0x%lx is 0 "
13199 "[in module %s]"),
bb5ed363 13200 line_offset, objfile->name);
c91513d8
PP
13201 p_record_line = noop_record_line;
13202 }
13203
2dc7f7b3 13204 op_index = 0;
107d2387
AC
13205 line_ptr += bytes_read;
13206 address += baseaddr;
c906108c
SS
13207 break;
13208 case DW_LNE_define_file:
debd256d
JB
13209 {
13210 char *cur_file;
13211 unsigned int dir_index, mod_time, length;
6e70227d 13212
3e43a32a
MS
13213 cur_file = read_direct_string (abfd, line_ptr,
13214 &bytes_read);
debd256d
JB
13215 line_ptr += bytes_read;
13216 dir_index =
13217 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
13218 line_ptr += bytes_read;
13219 mod_time =
13220 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
13221 line_ptr += bytes_read;
13222 length =
13223 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
13224 line_ptr += bytes_read;
13225 add_file_name (lh, cur_file, dir_index, mod_time, length);
13226 }
c906108c 13227 break;
d0c6ba3d
CC
13228 case DW_LNE_set_discriminator:
13229 /* The discriminator is not interesting to the debugger;
13230 just ignore it. */
13231 line_ptr = extended_end;
13232 break;
c906108c 13233 default:
4d3c2250 13234 complaint (&symfile_complaints,
e2e0b3e5 13235 _("mangled .debug_line section"));
debd256d 13236 return;
c906108c 13237 }
a8c50c1f
DJ
13238 /* Make sure that we parsed the extended op correctly. If e.g.
13239 we expected a different address size than the producer used,
13240 we may have read the wrong number of bytes. */
13241 if (line_ptr != extended_end)
13242 {
13243 complaint (&symfile_complaints,
13244 _("mangled .debug_line section"));
13245 return;
13246 }
c906108c
SS
13247 break;
13248 case DW_LNS_copy:
59205f5a 13249 if (lh->num_file_names < file || file == 0)
25e43795
DJ
13250 dwarf2_debug_line_missing_file_complaint ();
13251 else
366da635 13252 {
25e43795 13253 lh->file_names[file - 1].included_p = 1;
ca5f395d 13254 if (!decode_for_pst_p && is_stmt)
fbf65064
UW
13255 {
13256 if (last_subfile != current_subfile)
13257 {
13258 addr = gdbarch_addr_bits_remove (gdbarch, address);
13259 if (last_subfile)
c91513d8 13260 (*p_record_line) (last_subfile, 0, addr);
fbf65064
UW
13261 last_subfile = current_subfile;
13262 }
7019d805 13263 addr = gdbarch_addr_bits_remove (gdbarch, address);
c91513d8 13264 (*p_record_line) (current_subfile, line, addr);
fbf65064 13265 }
366da635 13266 }
c906108c
SS
13267 basic_block = 0;
13268 break;
13269 case DW_LNS_advance_pc:
2dc7f7b3
TT
13270 {
13271 CORE_ADDR adjust
13272 = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
13273
13274 address += (((op_index + adjust)
13275 / lh->maximum_ops_per_instruction)
13276 * lh->minimum_instruction_length);
13277 op_index = ((op_index + adjust)
13278 % lh->maximum_ops_per_instruction);
13279 line_ptr += bytes_read;
13280 }
c906108c
SS
13281 break;
13282 case DW_LNS_advance_line:
13283 line += read_signed_leb128 (abfd, line_ptr, &bytes_read);
13284 line_ptr += bytes_read;
13285 break;
13286 case DW_LNS_set_file:
debd256d 13287 {
a738430d
MK
13288 /* The arrays lh->include_dirs and lh->file_names are
13289 0-based, but the directory and file name numbers in
13290 the statement program are 1-based. */
debd256d 13291 struct file_entry *fe;
4f1520fb 13292 char *dir = NULL;
a738430d 13293
debd256d
JB
13294 file = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
13295 line_ptr += bytes_read;
59205f5a 13296 if (lh->num_file_names < file || file == 0)
25e43795
DJ
13297 dwarf2_debug_line_missing_file_complaint ();
13298 else
13299 {
13300 fe = &lh->file_names[file - 1];
13301 if (fe->dir_index)
13302 dir = lh->include_dirs[fe->dir_index - 1];
13303 if (!decode_for_pst_p)
13304 {
13305 last_subfile = current_subfile;
13306 dwarf2_start_subfile (fe->name, dir, comp_dir);
13307 }
13308 }
debd256d 13309 }
c906108c
SS
13310 break;
13311 case DW_LNS_set_column:
13312 column = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
13313 line_ptr += bytes_read;
13314 break;
13315 case DW_LNS_negate_stmt:
13316 is_stmt = (!is_stmt);
13317 break;
13318 case DW_LNS_set_basic_block:
13319 basic_block = 1;
13320 break;
c2c6d25f
JM
13321 /* Add to the address register of the state machine the
13322 address increment value corresponding to special opcode
a738430d
MK
13323 255. I.e., this value is scaled by the minimum
13324 instruction length since special opcode 255 would have
b021a221 13325 scaled the increment. */
c906108c 13326 case DW_LNS_const_add_pc:
2dc7f7b3
TT
13327 {
13328 CORE_ADDR adjust = (255 - lh->opcode_base) / lh->line_range;
13329
13330 address += (((op_index + adjust)
13331 / lh->maximum_ops_per_instruction)
13332 * lh->minimum_instruction_length);
13333 op_index = ((op_index + adjust)
13334 % lh->maximum_ops_per_instruction);
13335 }
c906108c
SS
13336 break;
13337 case DW_LNS_fixed_advance_pc:
13338 address += read_2_bytes (abfd, line_ptr);
2dc7f7b3 13339 op_index = 0;
c906108c
SS
13340 line_ptr += 2;
13341 break;
9aa1fe7e 13342 default:
a738430d
MK
13343 {
13344 /* Unknown standard opcode, ignore it. */
9aa1fe7e 13345 int i;
a738430d 13346
debd256d 13347 for (i = 0; i < lh->standard_opcode_lengths[op_code]; i++)
9aa1fe7e
GK
13348 {
13349 (void) read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
13350 line_ptr += bytes_read;
13351 }
13352 }
c906108c
SS
13353 }
13354 }
59205f5a
JB
13355 if (lh->num_file_names < file || file == 0)
13356 dwarf2_debug_line_missing_file_complaint ();
13357 else
13358 {
13359 lh->file_names[file - 1].included_p = 1;
13360 if (!decode_for_pst_p)
fbf65064
UW
13361 {
13362 addr = gdbarch_addr_bits_remove (gdbarch, address);
c91513d8 13363 (*p_record_line) (current_subfile, 0, addr);
fbf65064 13364 }
59205f5a 13365 }
c906108c 13366 }
f3f5162e
DE
13367}
13368
13369/* Decode the Line Number Program (LNP) for the given line_header
13370 structure and CU. The actual information extracted and the type
13371 of structures created from the LNP depends on the value of PST.
13372
13373 1. If PST is NULL, then this procedure uses the data from the program
13374 to create all necessary symbol tables, and their linetables.
13375
13376 2. If PST is not NULL, this procedure reads the program to determine
13377 the list of files included by the unit represented by PST, and
13378 builds all the associated partial symbol tables.
13379
13380 COMP_DIR is the compilation directory (DW_AT_comp_dir) or NULL if unknown.
13381 It is used for relative paths in the line table.
13382 NOTE: When processing partial symtabs (pst != NULL),
13383 comp_dir == pst->dirname.
13384
13385 NOTE: It is important that psymtabs have the same file name (via strcmp)
13386 as the corresponding symtab. Since COMP_DIR is not used in the name of the
13387 symtab we don't use it in the name of the psymtabs we create.
13388 E.g. expand_line_sal requires this when finding psymtabs to expand.
13389 A good testcase for this is mb-inline.exp. */
13390
13391static void
13392dwarf_decode_lines (struct line_header *lh, const char *comp_dir,
13393 struct dwarf2_cu *cu, struct partial_symtab *pst,
13394 int want_line_info)
13395{
13396 struct objfile *objfile = cu->objfile;
13397 const int decode_for_pst_p = (pst != NULL);
13398 struct subfile *first_subfile = current_subfile;
13399
13400 if (want_line_info)
13401 dwarf_decode_lines_1 (lh, comp_dir, cu, pst);
aaa75496
JB
13402
13403 if (decode_for_pst_p)
13404 {
13405 int file_index;
13406
13407 /* Now that we're done scanning the Line Header Program, we can
13408 create the psymtab of each included file. */
13409 for (file_index = 0; file_index < lh->num_file_names; file_index++)
13410 if (lh->file_names[file_index].included_p == 1)
13411 {
c6da4cef
DE
13412 char *include_name =
13413 psymtab_include_file_name (lh, file_index, pst, comp_dir);
13414 if (include_name != NULL)
aaa75496
JB
13415 dwarf2_create_include_psymtab (include_name, pst, objfile);
13416 }
13417 }
cb1df416
DJ
13418 else
13419 {
13420 /* Make sure a symtab is created for every file, even files
13421 which contain only variables (i.e. no code with associated
13422 line numbers). */
cb1df416 13423 int i;
cb1df416
DJ
13424
13425 for (i = 0; i < lh->num_file_names; i++)
13426 {
13427 char *dir = NULL;
f3f5162e 13428 struct file_entry *fe;
9a619af0 13429
cb1df416
DJ
13430 fe = &lh->file_names[i];
13431 if (fe->dir_index)
13432 dir = lh->include_dirs[fe->dir_index - 1];
13433 dwarf2_start_subfile (fe->name, dir, comp_dir);
13434
13435 /* Skip the main file; we don't need it, and it must be
13436 allocated last, so that it will show up before the
13437 non-primary symtabs in the objfile's symtab list. */
13438 if (current_subfile == first_subfile)
13439 continue;
13440
13441 if (current_subfile->symtab == NULL)
13442 current_subfile->symtab = allocate_symtab (current_subfile->name,
bb5ed363 13443 objfile);
cb1df416
DJ
13444 fe->symtab = current_subfile->symtab;
13445 }
13446 }
c906108c
SS
13447}
13448
13449/* Start a subfile for DWARF. FILENAME is the name of the file and
13450 DIRNAME the name of the source directory which contains FILENAME
4f1520fb
FR
13451 or NULL if not known. COMP_DIR is the compilation directory for the
13452 linetable's compilation unit or NULL if not known.
c906108c
SS
13453 This routine tries to keep line numbers from identical absolute and
13454 relative file names in a common subfile.
13455
13456 Using the `list' example from the GDB testsuite, which resides in
13457 /srcdir and compiling it with Irix6.2 cc in /compdir using a filename
13458 of /srcdir/list0.c yields the following debugging information for list0.c:
13459
c5aa993b
JM
13460 DW_AT_name: /srcdir/list0.c
13461 DW_AT_comp_dir: /compdir
357e46e7 13462 files.files[0].name: list0.h
c5aa993b 13463 files.files[0].dir: /srcdir
357e46e7 13464 files.files[1].name: list0.c
c5aa993b 13465 files.files[1].dir: /srcdir
c906108c
SS
13466
13467 The line number information for list0.c has to end up in a single
4f1520fb
FR
13468 subfile, so that `break /srcdir/list0.c:1' works as expected.
13469 start_subfile will ensure that this happens provided that we pass the
13470 concatenation of files.files[1].dir and files.files[1].name as the
13471 subfile's name. */
c906108c
SS
13472
13473static void
3e43a32a
MS
13474dwarf2_start_subfile (char *filename, const char *dirname,
13475 const char *comp_dir)
c906108c 13476{
4f1520fb
FR
13477 char *fullname;
13478
13479 /* While reading the DIEs, we call start_symtab(DW_AT_name, DW_AT_comp_dir).
13480 `start_symtab' will always pass the contents of DW_AT_comp_dir as
13481 second argument to start_subfile. To be consistent, we do the
13482 same here. In order not to lose the line information directory,
13483 we concatenate it to the filename when it makes sense.
13484 Note that the Dwarf3 standard says (speaking of filenames in line
13485 information): ``The directory index is ignored for file names
13486 that represent full path names''. Thus ignoring dirname in the
13487 `else' branch below isn't an issue. */
c906108c 13488
d5166ae1 13489 if (!IS_ABSOLUTE_PATH (filename) && dirname != NULL)
4f1520fb
FR
13490 fullname = concat (dirname, SLASH_STRING, filename, (char *)NULL);
13491 else
13492 fullname = filename;
c906108c 13493
4f1520fb
FR
13494 start_subfile (fullname, comp_dir);
13495
13496 if (fullname != filename)
13497 xfree (fullname);
c906108c
SS
13498}
13499
4c2df51b
DJ
13500static void
13501var_decode_location (struct attribute *attr, struct symbol *sym,
e7c27a73 13502 struct dwarf2_cu *cu)
4c2df51b 13503{
e7c27a73
DJ
13504 struct objfile *objfile = cu->objfile;
13505 struct comp_unit_head *cu_header = &cu->header;
13506
4c2df51b
DJ
13507 /* NOTE drow/2003-01-30: There used to be a comment and some special
13508 code here to turn a symbol with DW_AT_external and a
13509 SYMBOL_VALUE_ADDRESS of 0 into a LOC_UNRESOLVED symbol. This was
13510 necessary for platforms (maybe Alpha, certainly PowerPC GNU/Linux
13511 with some versions of binutils) where shared libraries could have
13512 relocations against symbols in their debug information - the
13513 minimal symbol would have the right address, but the debug info
13514 would not. It's no longer necessary, because we will explicitly
13515 apply relocations when we read in the debug information now. */
13516
13517 /* A DW_AT_location attribute with no contents indicates that a
13518 variable has been optimized away. */
13519 if (attr_form_is_block (attr) && DW_BLOCK (attr)->size == 0)
13520 {
13521 SYMBOL_CLASS (sym) = LOC_OPTIMIZED_OUT;
13522 return;
13523 }
13524
13525 /* Handle one degenerate form of location expression specially, to
13526 preserve GDB's previous behavior when section offsets are
3019eac3
DE
13527 specified. If this is just a DW_OP_addr or DW_OP_GNU_addr_index
13528 then mark this symbol as LOC_STATIC. */
4c2df51b
DJ
13529
13530 if (attr_form_is_block (attr)
3019eac3
DE
13531 && ((DW_BLOCK (attr)->data[0] == DW_OP_addr
13532 && DW_BLOCK (attr)->size == 1 + cu_header->addr_size)
13533 || (DW_BLOCK (attr)->data[0] == DW_OP_GNU_addr_index
13534 && (DW_BLOCK (attr)->size
13535 == 1 + leb128_size (&DW_BLOCK (attr)->data[1])))))
4c2df51b 13536 {
891d2f0b 13537 unsigned int dummy;
4c2df51b 13538
3019eac3
DE
13539 if (DW_BLOCK (attr)->data[0] == DW_OP_addr)
13540 SYMBOL_VALUE_ADDRESS (sym) =
13541 read_address (objfile->obfd, DW_BLOCK (attr)->data + 1, cu, &dummy);
13542 else
13543 SYMBOL_VALUE_ADDRESS (sym) =
13544 read_addr_index_from_leb128 (cu, DW_BLOCK (attr)->data + 1, &dummy);
907fc202 13545 SYMBOL_CLASS (sym) = LOC_STATIC;
4c2df51b
DJ
13546 fixup_symbol_section (sym, objfile);
13547 SYMBOL_VALUE_ADDRESS (sym) += ANOFFSET (objfile->section_offsets,
13548 SYMBOL_SECTION (sym));
4c2df51b
DJ
13549 return;
13550 }
13551
13552 /* NOTE drow/2002-01-30: It might be worthwhile to have a static
13553 expression evaluator, and use LOC_COMPUTED only when necessary
13554 (i.e. when the value of a register or memory location is
13555 referenced, or a thread-local block, etc.). Then again, it might
13556 not be worthwhile. I'm assuming that it isn't unless performance
13557 or memory numbers show me otherwise. */
13558
e7c27a73 13559 dwarf2_symbol_mark_computed (attr, sym, cu);
4c2df51b 13560 SYMBOL_CLASS (sym) = LOC_COMPUTED;
8be455d7
JK
13561
13562 if (SYMBOL_COMPUTED_OPS (sym) == &dwarf2_loclist_funcs)
13563 cu->has_loclist = 1;
4c2df51b
DJ
13564}
13565
c906108c
SS
13566/* Given a pointer to a DWARF information entry, figure out if we need
13567 to make a symbol table entry for it, and if so, create a new entry
13568 and return a pointer to it.
13569 If TYPE is NULL, determine symbol type from the die, otherwise
34eaf542
TT
13570 used the passed type.
13571 If SPACE is not NULL, use it to hold the new symbol. If it is
13572 NULL, allocate a new symbol on the objfile's obstack. */
c906108c
SS
13573
13574static struct symbol *
34eaf542
TT
13575new_symbol_full (struct die_info *die, struct type *type, struct dwarf2_cu *cu,
13576 struct symbol *space)
c906108c 13577{
e7c27a73 13578 struct objfile *objfile = cu->objfile;
c906108c
SS
13579 struct symbol *sym = NULL;
13580 char *name;
13581 struct attribute *attr = NULL;
13582 struct attribute *attr2 = NULL;
e142c38c 13583 CORE_ADDR baseaddr;
e37fd15a
SW
13584 struct pending **list_to_add = NULL;
13585
edb3359d 13586 int inlined_func = (die->tag == DW_TAG_inlined_subroutine);
e142c38c
DJ
13587
13588 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
c906108c 13589
94af9270 13590 name = dwarf2_name (die, cu);
c906108c
SS
13591 if (name)
13592 {
94af9270 13593 const char *linkagename;
34eaf542 13594 int suppress_add = 0;
94af9270 13595
34eaf542
TT
13596 if (space)
13597 sym = space;
13598 else
13599 sym = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct symbol);
c906108c 13600 OBJSTAT (objfile, n_syms++);
2de7ced7
DJ
13601
13602 /* Cache this symbol's name and the name's demangled form (if any). */
33e5013e 13603 SYMBOL_SET_LANGUAGE (sym, cu->language);
94af9270
KS
13604 linkagename = dwarf2_physname (name, die, cu);
13605 SYMBOL_SET_NAMES (sym, linkagename, strlen (linkagename), 0, objfile);
c906108c 13606
f55ee35c
JK
13607 /* Fortran does not have mangling standard and the mangling does differ
13608 between gfortran, iFort etc. */
13609 if (cu->language == language_fortran
b250c185 13610 && symbol_get_demangled_name (&(sym->ginfo)) == NULL)
29df156d
SW
13611 symbol_set_demangled_name (&(sym->ginfo),
13612 (char *) dwarf2_full_name (name, die, cu),
13613 NULL);
f55ee35c 13614
c906108c 13615 /* Default assumptions.
c5aa993b 13616 Use the passed type or decode it from the die. */
176620f1 13617 SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
875dc2fc 13618 SYMBOL_CLASS (sym) = LOC_OPTIMIZED_OUT;
c906108c
SS
13619 if (type != NULL)
13620 SYMBOL_TYPE (sym) = type;
13621 else
e7c27a73 13622 SYMBOL_TYPE (sym) = die_type (die, cu);
edb3359d
DJ
13623 attr = dwarf2_attr (die,
13624 inlined_func ? DW_AT_call_line : DW_AT_decl_line,
13625 cu);
c906108c
SS
13626 if (attr)
13627 {
13628 SYMBOL_LINE (sym) = DW_UNSND (attr);
13629 }
cb1df416 13630
edb3359d
DJ
13631 attr = dwarf2_attr (die,
13632 inlined_func ? DW_AT_call_file : DW_AT_decl_file,
13633 cu);
cb1df416
DJ
13634 if (attr)
13635 {
13636 int file_index = DW_UNSND (attr);
9a619af0 13637
cb1df416
DJ
13638 if (cu->line_header == NULL
13639 || file_index > cu->line_header->num_file_names)
13640 complaint (&symfile_complaints,
13641 _("file index out of range"));
1c3d648d 13642 else if (file_index > 0)
cb1df416
DJ
13643 {
13644 struct file_entry *fe;
9a619af0 13645
cb1df416
DJ
13646 fe = &cu->line_header->file_names[file_index - 1];
13647 SYMBOL_SYMTAB (sym) = fe->symtab;
13648 }
13649 }
13650
c906108c
SS
13651 switch (die->tag)
13652 {
13653 case DW_TAG_label:
e142c38c 13654 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
c906108c
SS
13655 if (attr)
13656 {
13657 SYMBOL_VALUE_ADDRESS (sym) = DW_ADDR (attr) + baseaddr;
13658 }
0f5238ed
TT
13659 SYMBOL_TYPE (sym) = objfile_type (objfile)->builtin_core_addr;
13660 SYMBOL_DOMAIN (sym) = LABEL_DOMAIN;
c906108c 13661 SYMBOL_CLASS (sym) = LOC_LABEL;
0f5238ed 13662 add_symbol_to_list (sym, cu->list_in_scope);
c906108c
SS
13663 break;
13664 case DW_TAG_subprogram:
13665 /* SYMBOL_BLOCK_VALUE (sym) will be filled in later by
13666 finish_block. */
13667 SYMBOL_CLASS (sym) = LOC_BLOCK;
e142c38c 13668 attr2 = dwarf2_attr (die, DW_AT_external, cu);
2cfa0c8d
JB
13669 if ((attr2 && (DW_UNSND (attr2) != 0))
13670 || cu->language == language_ada)
c906108c 13671 {
2cfa0c8d
JB
13672 /* Subprograms marked external are stored as a global symbol.
13673 Ada subprograms, whether marked external or not, are always
13674 stored as a global symbol, because we want to be able to
13675 access them globally. For instance, we want to be able
13676 to break on a nested subprogram without having to
13677 specify the context. */
e37fd15a 13678 list_to_add = &global_symbols;
c906108c
SS
13679 }
13680 else
13681 {
e37fd15a 13682 list_to_add = cu->list_in_scope;
c906108c
SS
13683 }
13684 break;
edb3359d
DJ
13685 case DW_TAG_inlined_subroutine:
13686 /* SYMBOL_BLOCK_VALUE (sym) will be filled in later by
13687 finish_block. */
13688 SYMBOL_CLASS (sym) = LOC_BLOCK;
13689 SYMBOL_INLINED (sym) = 1;
481860b3 13690 list_to_add = cu->list_in_scope;
edb3359d 13691 break;
34eaf542
TT
13692 case DW_TAG_template_value_param:
13693 suppress_add = 1;
13694 /* Fall through. */
72929c62 13695 case DW_TAG_constant:
c906108c 13696 case DW_TAG_variable:
254e6b9e 13697 case DW_TAG_member:
0963b4bd
MS
13698 /* Compilation with minimal debug info may result in
13699 variables with missing type entries. Change the
13700 misleading `void' type to something sensible. */
c906108c 13701 if (TYPE_CODE (SYMBOL_TYPE (sym)) == TYPE_CODE_VOID)
64c50499 13702 SYMBOL_TYPE (sym)
46bf5051 13703 = objfile_type (objfile)->nodebug_data_symbol;
64c50499 13704
e142c38c 13705 attr = dwarf2_attr (die, DW_AT_const_value, cu);
254e6b9e
DE
13706 /* In the case of DW_TAG_member, we should only be called for
13707 static const members. */
13708 if (die->tag == DW_TAG_member)
13709 {
3863f96c
DE
13710 /* dwarf2_add_field uses die_is_declaration,
13711 so we do the same. */
254e6b9e
DE
13712 gdb_assert (die_is_declaration (die, cu));
13713 gdb_assert (attr);
13714 }
c906108c
SS
13715 if (attr)
13716 {
e7c27a73 13717 dwarf2_const_value (attr, sym, cu);
e142c38c 13718 attr2 = dwarf2_attr (die, DW_AT_external, cu);
e37fd15a 13719 if (!suppress_add)
34eaf542
TT
13720 {
13721 if (attr2 && (DW_UNSND (attr2) != 0))
e37fd15a 13722 list_to_add = &global_symbols;
34eaf542 13723 else
e37fd15a 13724 list_to_add = cu->list_in_scope;
34eaf542 13725 }
c906108c
SS
13726 break;
13727 }
e142c38c 13728 attr = dwarf2_attr (die, DW_AT_location, cu);
c906108c
SS
13729 if (attr)
13730 {
e7c27a73 13731 var_decode_location (attr, sym, cu);
e142c38c 13732 attr2 = dwarf2_attr (die, DW_AT_external, cu);
caac4577
JG
13733 if (SYMBOL_CLASS (sym) == LOC_STATIC
13734 && SYMBOL_VALUE_ADDRESS (sym) == 0
13735 && !dwarf2_per_objfile->has_section_at_zero)
13736 {
13737 /* When a static variable is eliminated by the linker,
13738 the corresponding debug information is not stripped
13739 out, but the variable address is set to null;
13740 do not add such variables into symbol table. */
13741 }
13742 else if (attr2 && (DW_UNSND (attr2) != 0))
1c809c68 13743 {
f55ee35c
JK
13744 /* Workaround gfortran PR debug/40040 - it uses
13745 DW_AT_location for variables in -fPIC libraries which may
13746 get overriden by other libraries/executable and get
13747 a different address. Resolve it by the minimal symbol
13748 which may come from inferior's executable using copy
13749 relocation. Make this workaround only for gfortran as for
13750 other compilers GDB cannot guess the minimal symbol
13751 Fortran mangling kind. */
13752 if (cu->language == language_fortran && die->parent
13753 && die->parent->tag == DW_TAG_module
13754 && cu->producer
13755 && strncmp (cu->producer, "GNU Fortran ", 12) == 0)
13756 SYMBOL_CLASS (sym) = LOC_UNRESOLVED;
13757
1c809c68
TT
13758 /* A variable with DW_AT_external is never static,
13759 but it may be block-scoped. */
13760 list_to_add = (cu->list_in_scope == &file_symbols
13761 ? &global_symbols : cu->list_in_scope);
1c809c68 13762 }
c906108c 13763 else
e37fd15a 13764 list_to_add = cu->list_in_scope;
c906108c
SS
13765 }
13766 else
13767 {
13768 /* We do not know the address of this symbol.
c5aa993b
JM
13769 If it is an external symbol and we have type information
13770 for it, enter the symbol as a LOC_UNRESOLVED symbol.
13771 The address of the variable will then be determined from
13772 the minimal symbol table whenever the variable is
13773 referenced. */
e142c38c 13774 attr2 = dwarf2_attr (die, DW_AT_external, cu);
c906108c 13775 if (attr2 && (DW_UNSND (attr2) != 0)
e142c38c 13776 && dwarf2_attr (die, DW_AT_type, cu) != NULL)
c906108c 13777 {
0fe7935b
DJ
13778 /* A variable with DW_AT_external is never static, but it
13779 may be block-scoped. */
13780 list_to_add = (cu->list_in_scope == &file_symbols
13781 ? &global_symbols : cu->list_in_scope);
13782
c906108c 13783 SYMBOL_CLASS (sym) = LOC_UNRESOLVED;
c906108c 13784 }
442ddf59
JK
13785 else if (!die_is_declaration (die, cu))
13786 {
13787 /* Use the default LOC_OPTIMIZED_OUT class. */
13788 gdb_assert (SYMBOL_CLASS (sym) == LOC_OPTIMIZED_OUT);
e37fd15a
SW
13789 if (!suppress_add)
13790 list_to_add = cu->list_in_scope;
442ddf59 13791 }
c906108c
SS
13792 }
13793 break;
13794 case DW_TAG_formal_parameter:
edb3359d
DJ
13795 /* If we are inside a function, mark this as an argument. If
13796 not, we might be looking at an argument to an inlined function
13797 when we do not have enough information to show inlined frames;
13798 pretend it's a local variable in that case so that the user can
13799 still see it. */
13800 if (context_stack_depth > 0
13801 && context_stack[context_stack_depth - 1].name != NULL)
13802 SYMBOL_IS_ARGUMENT (sym) = 1;
e142c38c 13803 attr = dwarf2_attr (die, DW_AT_location, cu);
c906108c
SS
13804 if (attr)
13805 {
e7c27a73 13806 var_decode_location (attr, sym, cu);
c906108c 13807 }
e142c38c 13808 attr = dwarf2_attr (die, DW_AT_const_value, cu);
c906108c
SS
13809 if (attr)
13810 {
e7c27a73 13811 dwarf2_const_value (attr, sym, cu);
c906108c 13812 }
f346a30d 13813
e37fd15a 13814 list_to_add = cu->list_in_scope;
c906108c
SS
13815 break;
13816 case DW_TAG_unspecified_parameters:
13817 /* From varargs functions; gdb doesn't seem to have any
13818 interest in this information, so just ignore it for now.
13819 (FIXME?) */
13820 break;
34eaf542
TT
13821 case DW_TAG_template_type_param:
13822 suppress_add = 1;
13823 /* Fall through. */
c906108c 13824 case DW_TAG_class_type:
680b30c7 13825 case DW_TAG_interface_type:
c906108c
SS
13826 case DW_TAG_structure_type:
13827 case DW_TAG_union_type:
72019c9c 13828 case DW_TAG_set_type:
c906108c
SS
13829 case DW_TAG_enumeration_type:
13830 SYMBOL_CLASS (sym) = LOC_TYPEDEF;
176620f1 13831 SYMBOL_DOMAIN (sym) = STRUCT_DOMAIN;
c906108c 13832
63d06c5c 13833 {
987504bb 13834 /* NOTE: carlton/2003-11-10: C++ and Java class symbols shouldn't
63d06c5c
DC
13835 really ever be static objects: otherwise, if you try
13836 to, say, break of a class's method and you're in a file
13837 which doesn't mention that class, it won't work unless
13838 the check for all static symbols in lookup_symbol_aux
13839 saves you. See the OtherFileClass tests in
13840 gdb.c++/namespace.exp. */
13841
e37fd15a 13842 if (!suppress_add)
34eaf542 13843 {
34eaf542
TT
13844 list_to_add = (cu->list_in_scope == &file_symbols
13845 && (cu->language == language_cplus
13846 || cu->language == language_java)
13847 ? &global_symbols : cu->list_in_scope);
63d06c5c 13848
64382290
TT
13849 /* The semantics of C++ state that "struct foo {
13850 ... }" also defines a typedef for "foo". A Java
13851 class declaration also defines a typedef for the
13852 class. */
13853 if (cu->language == language_cplus
13854 || cu->language == language_java
13855 || cu->language == language_ada)
13856 {
13857 /* The symbol's name is already allocated along
13858 with this objfile, so we don't need to
13859 duplicate it for the type. */
13860 if (TYPE_NAME (SYMBOL_TYPE (sym)) == 0)
13861 TYPE_NAME (SYMBOL_TYPE (sym)) = SYMBOL_SEARCH_NAME (sym);
13862 }
63d06c5c
DC
13863 }
13864 }
c906108c
SS
13865 break;
13866 case DW_TAG_typedef:
63d06c5c
DC
13867 SYMBOL_CLASS (sym) = LOC_TYPEDEF;
13868 SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
e37fd15a 13869 list_to_add = cu->list_in_scope;
63d06c5c 13870 break;
c906108c 13871 case DW_TAG_base_type:
a02abb62 13872 case DW_TAG_subrange_type:
c906108c 13873 SYMBOL_CLASS (sym) = LOC_TYPEDEF;
176620f1 13874 SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
e37fd15a 13875 list_to_add = cu->list_in_scope;
c906108c
SS
13876 break;
13877 case DW_TAG_enumerator:
e142c38c 13878 attr = dwarf2_attr (die, DW_AT_const_value, cu);
c906108c
SS
13879 if (attr)
13880 {
e7c27a73 13881 dwarf2_const_value (attr, sym, cu);
c906108c 13882 }
63d06c5c
DC
13883 {
13884 /* NOTE: carlton/2003-11-10: See comment above in the
13885 DW_TAG_class_type, etc. block. */
13886
e142c38c 13887 list_to_add = (cu->list_in_scope == &file_symbols
987504bb
JJ
13888 && (cu->language == language_cplus
13889 || cu->language == language_java)
e142c38c 13890 ? &global_symbols : cu->list_in_scope);
63d06c5c 13891 }
c906108c 13892 break;
5c4e30ca
DC
13893 case DW_TAG_namespace:
13894 SYMBOL_CLASS (sym) = LOC_TYPEDEF;
e37fd15a 13895 list_to_add = &global_symbols;
5c4e30ca 13896 break;
c906108c
SS
13897 default:
13898 /* Not a tag we recognize. Hopefully we aren't processing
13899 trash data, but since we must specifically ignore things
13900 we don't recognize, there is nothing else we should do at
0963b4bd 13901 this point. */
e2e0b3e5 13902 complaint (&symfile_complaints, _("unsupported tag: '%s'"),
4d3c2250 13903 dwarf_tag_name (die->tag));
c906108c
SS
13904 break;
13905 }
df8a16a1 13906
e37fd15a
SW
13907 if (suppress_add)
13908 {
13909 sym->hash_next = objfile->template_symbols;
13910 objfile->template_symbols = sym;
13911 list_to_add = NULL;
13912 }
13913
13914 if (list_to_add != NULL)
13915 add_symbol_to_list (sym, list_to_add);
13916
df8a16a1
DJ
13917 /* For the benefit of old versions of GCC, check for anonymous
13918 namespaces based on the demangled name. */
13919 if (!processing_has_namespace_info
94af9270 13920 && cu->language == language_cplus)
a10964d1 13921 cp_scan_for_anonymous_namespaces (sym, objfile);
c906108c
SS
13922 }
13923 return (sym);
13924}
13925
34eaf542
TT
13926/* A wrapper for new_symbol_full that always allocates a new symbol. */
13927
13928static struct symbol *
13929new_symbol (struct die_info *die, struct type *type, struct dwarf2_cu *cu)
13930{
13931 return new_symbol_full (die, type, cu, NULL);
13932}
13933
98bfdba5
PA
13934/* Given an attr with a DW_FORM_dataN value in host byte order,
13935 zero-extend it as appropriate for the symbol's type. The DWARF
13936 standard (v4) is not entirely clear about the meaning of using
13937 DW_FORM_dataN for a constant with a signed type, where the type is
13938 wider than the data. The conclusion of a discussion on the DWARF
13939 list was that this is unspecified. We choose to always zero-extend
13940 because that is the interpretation long in use by GCC. */
c906108c 13941
98bfdba5
PA
13942static gdb_byte *
13943dwarf2_const_value_data (struct attribute *attr, struct type *type,
13944 const char *name, struct obstack *obstack,
12df843f 13945 struct dwarf2_cu *cu, LONGEST *value, int bits)
c906108c 13946{
e7c27a73 13947 struct objfile *objfile = cu->objfile;
e17a4113
UW
13948 enum bfd_endian byte_order = bfd_big_endian (objfile->obfd) ?
13949 BFD_ENDIAN_BIG : BFD_ENDIAN_LITTLE;
98bfdba5
PA
13950 LONGEST l = DW_UNSND (attr);
13951
13952 if (bits < sizeof (*value) * 8)
13953 {
13954 l &= ((LONGEST) 1 << bits) - 1;
13955 *value = l;
13956 }
13957 else if (bits == sizeof (*value) * 8)
13958 *value = l;
13959 else
13960 {
13961 gdb_byte *bytes = obstack_alloc (obstack, bits / 8);
13962 store_unsigned_integer (bytes, bits / 8, byte_order, l);
13963 return bytes;
13964 }
13965
13966 return NULL;
13967}
13968
13969/* Read a constant value from an attribute. Either set *VALUE, or if
13970 the value does not fit in *VALUE, set *BYTES - either already
13971 allocated on the objfile obstack, or newly allocated on OBSTACK,
13972 or, set *BATON, if we translated the constant to a location
13973 expression. */
13974
13975static void
13976dwarf2_const_value_attr (struct attribute *attr, struct type *type,
13977 const char *name, struct obstack *obstack,
13978 struct dwarf2_cu *cu,
12df843f 13979 LONGEST *value, gdb_byte **bytes,
98bfdba5
PA
13980 struct dwarf2_locexpr_baton **baton)
13981{
13982 struct objfile *objfile = cu->objfile;
13983 struct comp_unit_head *cu_header = &cu->header;
c906108c 13984 struct dwarf_block *blk;
98bfdba5
PA
13985 enum bfd_endian byte_order = (bfd_big_endian (objfile->obfd) ?
13986 BFD_ENDIAN_BIG : BFD_ENDIAN_LITTLE);
13987
13988 *value = 0;
13989 *bytes = NULL;
13990 *baton = NULL;
c906108c
SS
13991
13992 switch (attr->form)
13993 {
13994 case DW_FORM_addr:
3019eac3 13995 case DW_FORM_GNU_addr_index:
ac56253d 13996 {
ac56253d
TT
13997 gdb_byte *data;
13998
98bfdba5
PA
13999 if (TYPE_LENGTH (type) != cu_header->addr_size)
14000 dwarf2_const_value_length_mismatch_complaint (name,
ac56253d 14001 cu_header->addr_size,
98bfdba5 14002 TYPE_LENGTH (type));
ac56253d
TT
14003 /* Symbols of this form are reasonably rare, so we just
14004 piggyback on the existing location code rather than writing
14005 a new implementation of symbol_computed_ops. */
98bfdba5
PA
14006 *baton = obstack_alloc (&objfile->objfile_obstack,
14007 sizeof (struct dwarf2_locexpr_baton));
14008 (*baton)->per_cu = cu->per_cu;
14009 gdb_assert ((*baton)->per_cu);
ac56253d 14010
98bfdba5
PA
14011 (*baton)->size = 2 + cu_header->addr_size;
14012 data = obstack_alloc (&objfile->objfile_obstack, (*baton)->size);
14013 (*baton)->data = data;
ac56253d
TT
14014
14015 data[0] = DW_OP_addr;
14016 store_unsigned_integer (&data[1], cu_header->addr_size,
14017 byte_order, DW_ADDR (attr));
14018 data[cu_header->addr_size + 1] = DW_OP_stack_value;
ac56253d 14019 }
c906108c 14020 break;
4ac36638 14021 case DW_FORM_string:
93b5768b 14022 case DW_FORM_strp:
3019eac3 14023 case DW_FORM_GNU_str_index:
98bfdba5
PA
14024 /* DW_STRING is already allocated on the objfile obstack, point
14025 directly to it. */
14026 *bytes = (gdb_byte *) DW_STRING (attr);
93b5768b 14027 break;
c906108c
SS
14028 case DW_FORM_block1:
14029 case DW_FORM_block2:
14030 case DW_FORM_block4:
14031 case DW_FORM_block:
2dc7f7b3 14032 case DW_FORM_exprloc:
c906108c 14033 blk = DW_BLOCK (attr);
98bfdba5
PA
14034 if (TYPE_LENGTH (type) != blk->size)
14035 dwarf2_const_value_length_mismatch_complaint (name, blk->size,
14036 TYPE_LENGTH (type));
14037 *bytes = blk->data;
c906108c 14038 break;
2df3850c
JM
14039
14040 /* The DW_AT_const_value attributes are supposed to carry the
14041 symbol's value "represented as it would be on the target
14042 architecture." By the time we get here, it's already been
14043 converted to host endianness, so we just need to sign- or
14044 zero-extend it as appropriate. */
14045 case DW_FORM_data1:
3e43a32a
MS
14046 *bytes = dwarf2_const_value_data (attr, type, name,
14047 obstack, cu, value, 8);
2df3850c 14048 break;
c906108c 14049 case DW_FORM_data2:
3e43a32a
MS
14050 *bytes = dwarf2_const_value_data (attr, type, name,
14051 obstack, cu, value, 16);
2df3850c 14052 break;
c906108c 14053 case DW_FORM_data4:
3e43a32a
MS
14054 *bytes = dwarf2_const_value_data (attr, type, name,
14055 obstack, cu, value, 32);
2df3850c 14056 break;
c906108c 14057 case DW_FORM_data8:
3e43a32a
MS
14058 *bytes = dwarf2_const_value_data (attr, type, name,
14059 obstack, cu, value, 64);
2df3850c
JM
14060 break;
14061
c906108c 14062 case DW_FORM_sdata:
98bfdba5 14063 *value = DW_SND (attr);
2df3850c
JM
14064 break;
14065
c906108c 14066 case DW_FORM_udata:
98bfdba5 14067 *value = DW_UNSND (attr);
c906108c 14068 break;
2df3850c 14069
c906108c 14070 default:
4d3c2250 14071 complaint (&symfile_complaints,
e2e0b3e5 14072 _("unsupported const value attribute form: '%s'"),
4d3c2250 14073 dwarf_form_name (attr->form));
98bfdba5 14074 *value = 0;
c906108c
SS
14075 break;
14076 }
14077}
14078
2df3850c 14079
98bfdba5
PA
14080/* Copy constant value from an attribute to a symbol. */
14081
2df3850c 14082static void
98bfdba5
PA
14083dwarf2_const_value (struct attribute *attr, struct symbol *sym,
14084 struct dwarf2_cu *cu)
2df3850c 14085{
98bfdba5
PA
14086 struct objfile *objfile = cu->objfile;
14087 struct comp_unit_head *cu_header = &cu->header;
12df843f 14088 LONGEST value;
98bfdba5
PA
14089 gdb_byte *bytes;
14090 struct dwarf2_locexpr_baton *baton;
2df3850c 14091
98bfdba5
PA
14092 dwarf2_const_value_attr (attr, SYMBOL_TYPE (sym),
14093 SYMBOL_PRINT_NAME (sym),
14094 &objfile->objfile_obstack, cu,
14095 &value, &bytes, &baton);
2df3850c 14096
98bfdba5
PA
14097 if (baton != NULL)
14098 {
14099 SYMBOL_COMPUTED_OPS (sym) = &dwarf2_locexpr_funcs;
14100 SYMBOL_LOCATION_BATON (sym) = baton;
14101 SYMBOL_CLASS (sym) = LOC_COMPUTED;
14102 }
14103 else if (bytes != NULL)
14104 {
14105 SYMBOL_VALUE_BYTES (sym) = bytes;
14106 SYMBOL_CLASS (sym) = LOC_CONST_BYTES;
14107 }
14108 else
14109 {
14110 SYMBOL_VALUE (sym) = value;
14111 SYMBOL_CLASS (sym) = LOC_CONST;
14112 }
2df3850c
JM
14113}
14114
c906108c
SS
14115/* Return the type of the die in question using its DW_AT_type attribute. */
14116
14117static struct type *
e7c27a73 14118die_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 14119{
c906108c 14120 struct attribute *type_attr;
c906108c 14121
e142c38c 14122 type_attr = dwarf2_attr (die, DW_AT_type, cu);
c906108c
SS
14123 if (!type_attr)
14124 {
14125 /* A missing DW_AT_type represents a void type. */
46bf5051 14126 return objfile_type (cu->objfile)->builtin_void;
c906108c 14127 }
348e048f 14128
673bfd45 14129 return lookup_die_type (die, type_attr, cu);
c906108c
SS
14130}
14131
b4ba55a1
JB
14132/* True iff CU's producer generates GNAT Ada auxiliary information
14133 that allows to find parallel types through that information instead
14134 of having to do expensive parallel lookups by type name. */
14135
14136static int
14137need_gnat_info (struct dwarf2_cu *cu)
14138{
14139 /* FIXME: brobecker/2010-10-12: As of now, only the AdaCore version
14140 of GNAT produces this auxiliary information, without any indication
14141 that it is produced. Part of enhancing the FSF version of GNAT
14142 to produce that information will be to put in place an indicator
14143 that we can use in order to determine whether the descriptive type
14144 info is available or not. One suggestion that has been made is
14145 to use a new attribute, attached to the CU die. For now, assume
14146 that the descriptive type info is not available. */
14147 return 0;
14148}
14149
b4ba55a1
JB
14150/* Return the auxiliary type of the die in question using its
14151 DW_AT_GNAT_descriptive_type attribute. Returns NULL if the
14152 attribute is not present. */
14153
14154static struct type *
14155die_descriptive_type (struct die_info *die, struct dwarf2_cu *cu)
14156{
b4ba55a1 14157 struct attribute *type_attr;
b4ba55a1
JB
14158
14159 type_attr = dwarf2_attr (die, DW_AT_GNAT_descriptive_type, cu);
14160 if (!type_attr)
14161 return NULL;
14162
673bfd45 14163 return lookup_die_type (die, type_attr, cu);
b4ba55a1
JB
14164}
14165
14166/* If DIE has a descriptive_type attribute, then set the TYPE's
14167 descriptive type accordingly. */
14168
14169static void
14170set_descriptive_type (struct type *type, struct die_info *die,
14171 struct dwarf2_cu *cu)
14172{
14173 struct type *descriptive_type = die_descriptive_type (die, cu);
14174
14175 if (descriptive_type)
14176 {
14177 ALLOCATE_GNAT_AUX_TYPE (type);
14178 TYPE_DESCRIPTIVE_TYPE (type) = descriptive_type;
14179 }
14180}
14181
c906108c
SS
14182/* Return the containing type of the die in question using its
14183 DW_AT_containing_type attribute. */
14184
14185static struct type *
e7c27a73 14186die_containing_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 14187{
c906108c 14188 struct attribute *type_attr;
c906108c 14189
e142c38c 14190 type_attr = dwarf2_attr (die, DW_AT_containing_type, cu);
33ac96f0
JK
14191 if (!type_attr)
14192 error (_("Dwarf Error: Problem turning containing type into gdb type "
14193 "[in module %s]"), cu->objfile->name);
14194
673bfd45 14195 return lookup_die_type (die, type_attr, cu);
c906108c
SS
14196}
14197
673bfd45
DE
14198/* Look up the type of DIE in CU using its type attribute ATTR.
14199 If there is no type substitute an error marker. */
14200
c906108c 14201static struct type *
673bfd45
DE
14202lookup_die_type (struct die_info *die, struct attribute *attr,
14203 struct dwarf2_cu *cu)
c906108c 14204{
bb5ed363 14205 struct objfile *objfile = cu->objfile;
f792889a
DJ
14206 struct type *this_type;
14207
673bfd45
DE
14208 /* First see if we have it cached. */
14209
14210 if (is_ref_attr (attr))
14211 {
b64f50a1 14212 sect_offset offset = dwarf2_get_ref_die_offset (attr);
673bfd45
DE
14213
14214 this_type = get_die_type_at_offset (offset, cu->per_cu);
14215 }
55f1336d 14216 else if (attr->form == DW_FORM_ref_sig8)
673bfd45
DE
14217 {
14218 struct signatured_type *sig_type = DW_SIGNATURED_TYPE (attr);
673bfd45
DE
14219
14220 /* sig_type will be NULL if the signatured type is missing from
14221 the debug info. */
14222 if (sig_type == NULL)
14223 error (_("Dwarf Error: Cannot find signatured DIE referenced from DIE "
14224 "at 0x%x [in module %s]"),
b64f50a1 14225 die->offset.sect_off, objfile->name);
673bfd45 14226
3019eac3
DE
14227 gdb_assert (sig_type->per_cu.is_debug_types);
14228 /* If we haven't filled in type_offset_in_section yet, then we
14229 haven't read the type in yet. */
14230 this_type = NULL;
14231 if (sig_type->type_offset_in_section.sect_off != 0)
14232 {
14233 this_type =
14234 get_die_type_at_offset (sig_type->type_offset_in_section,
14235 &sig_type->per_cu);
14236 }
673bfd45
DE
14237 }
14238 else
14239 {
14240 dump_die_for_error (die);
14241 error (_("Dwarf Error: Bad type attribute %s [in module %s]"),
bb5ed363 14242 dwarf_attr_name (attr->name), objfile->name);
673bfd45
DE
14243 }
14244
14245 /* If not cached we need to read it in. */
14246
14247 if (this_type == NULL)
14248 {
14249 struct die_info *type_die;
14250 struct dwarf2_cu *type_cu = cu;
14251
14252 type_die = follow_die_ref_or_sig (die, attr, &type_cu);
3019eac3
DE
14253 /* If we found the type now, it's probably because the type came
14254 from an inter-CU reference and the type's CU got expanded before
14255 ours. */
14256 this_type = get_die_type (type_die, type_cu);
14257 if (this_type == NULL)
14258 this_type = read_type_die_1 (type_die, type_cu);
673bfd45
DE
14259 }
14260
14261 /* If we still don't have a type use an error marker. */
14262
14263 if (this_type == NULL)
c906108c 14264 {
b00fdb78
TT
14265 char *message, *saved;
14266
14267 /* read_type_die already issued a complaint. */
14268 message = xstrprintf (_("<unknown type in %s, CU 0x%x, DIE 0x%x>"),
bb5ed363 14269 objfile->name,
b64f50a1
JK
14270 cu->header.offset.sect_off,
14271 die->offset.sect_off);
bb5ed363 14272 saved = obstack_copy0 (&objfile->objfile_obstack,
b00fdb78
TT
14273 message, strlen (message));
14274 xfree (message);
14275
bb5ed363 14276 this_type = init_type (TYPE_CODE_ERROR, 0, 0, saved, objfile);
c906108c 14277 }
673bfd45 14278
f792889a 14279 return this_type;
c906108c
SS
14280}
14281
673bfd45
DE
14282/* Return the type in DIE, CU.
14283 Returns NULL for invalid types.
14284
14285 This first does a lookup in the appropriate type_hash table,
14286 and only reads the die in if necessary.
14287
14288 NOTE: This can be called when reading in partial or full symbols. */
14289
f792889a 14290static struct type *
e7c27a73 14291read_type_die (struct die_info *die, struct dwarf2_cu *cu)
c906108c 14292{
f792889a
DJ
14293 struct type *this_type;
14294
14295 this_type = get_die_type (die, cu);
14296 if (this_type)
14297 return this_type;
14298
673bfd45
DE
14299 return read_type_die_1 (die, cu);
14300}
14301
14302/* Read the type in DIE, CU.
14303 Returns NULL for invalid types. */
14304
14305static struct type *
14306read_type_die_1 (struct die_info *die, struct dwarf2_cu *cu)
14307{
14308 struct type *this_type = NULL;
14309
c906108c
SS
14310 switch (die->tag)
14311 {
14312 case DW_TAG_class_type:
680b30c7 14313 case DW_TAG_interface_type:
c906108c
SS
14314 case DW_TAG_structure_type:
14315 case DW_TAG_union_type:
f792889a 14316 this_type = read_structure_type (die, cu);
c906108c
SS
14317 break;
14318 case DW_TAG_enumeration_type:
f792889a 14319 this_type = read_enumeration_type (die, cu);
c906108c
SS
14320 break;
14321 case DW_TAG_subprogram:
14322 case DW_TAG_subroutine_type:
edb3359d 14323 case DW_TAG_inlined_subroutine:
f792889a 14324 this_type = read_subroutine_type (die, cu);
c906108c
SS
14325 break;
14326 case DW_TAG_array_type:
f792889a 14327 this_type = read_array_type (die, cu);
c906108c 14328 break;
72019c9c 14329 case DW_TAG_set_type:
f792889a 14330 this_type = read_set_type (die, cu);
72019c9c 14331 break;
c906108c 14332 case DW_TAG_pointer_type:
f792889a 14333 this_type = read_tag_pointer_type (die, cu);
c906108c
SS
14334 break;
14335 case DW_TAG_ptr_to_member_type:
f792889a 14336 this_type = read_tag_ptr_to_member_type (die, cu);
c906108c
SS
14337 break;
14338 case DW_TAG_reference_type:
f792889a 14339 this_type = read_tag_reference_type (die, cu);
c906108c
SS
14340 break;
14341 case DW_TAG_const_type:
f792889a 14342 this_type = read_tag_const_type (die, cu);
c906108c
SS
14343 break;
14344 case DW_TAG_volatile_type:
f792889a 14345 this_type = read_tag_volatile_type (die, cu);
c906108c
SS
14346 break;
14347 case DW_TAG_string_type:
f792889a 14348 this_type = read_tag_string_type (die, cu);
c906108c
SS
14349 break;
14350 case DW_TAG_typedef:
f792889a 14351 this_type = read_typedef (die, cu);
c906108c 14352 break;
a02abb62 14353 case DW_TAG_subrange_type:
f792889a 14354 this_type = read_subrange_type (die, cu);
a02abb62 14355 break;
c906108c 14356 case DW_TAG_base_type:
f792889a 14357 this_type = read_base_type (die, cu);
c906108c 14358 break;
81a17f79 14359 case DW_TAG_unspecified_type:
f792889a 14360 this_type = read_unspecified_type (die, cu);
81a17f79 14361 break;
0114d602
DJ
14362 case DW_TAG_namespace:
14363 this_type = read_namespace_type (die, cu);
14364 break;
f55ee35c
JK
14365 case DW_TAG_module:
14366 this_type = read_module_type (die, cu);
14367 break;
c906108c 14368 default:
3e43a32a
MS
14369 complaint (&symfile_complaints,
14370 _("unexpected tag in read_type_die: '%s'"),
4d3c2250 14371 dwarf_tag_name (die->tag));
c906108c
SS
14372 break;
14373 }
63d06c5c 14374
f792889a 14375 return this_type;
63d06c5c
DC
14376}
14377
abc72ce4
DE
14378/* See if we can figure out if the class lives in a namespace. We do
14379 this by looking for a member function; its demangled name will
14380 contain namespace info, if there is any.
14381 Return the computed name or NULL.
14382 Space for the result is allocated on the objfile's obstack.
14383 This is the full-die version of guess_partial_die_structure_name.
14384 In this case we know DIE has no useful parent. */
14385
14386static char *
14387guess_full_die_structure_name (struct die_info *die, struct dwarf2_cu *cu)
14388{
14389 struct die_info *spec_die;
14390 struct dwarf2_cu *spec_cu;
14391 struct die_info *child;
14392
14393 spec_cu = cu;
14394 spec_die = die_specification (die, &spec_cu);
14395 if (spec_die != NULL)
14396 {
14397 die = spec_die;
14398 cu = spec_cu;
14399 }
14400
14401 for (child = die->child;
14402 child != NULL;
14403 child = child->sibling)
14404 {
14405 if (child->tag == DW_TAG_subprogram)
14406 {
14407 struct attribute *attr;
14408
14409 attr = dwarf2_attr (child, DW_AT_linkage_name, cu);
14410 if (attr == NULL)
14411 attr = dwarf2_attr (child, DW_AT_MIPS_linkage_name, cu);
14412 if (attr != NULL)
14413 {
14414 char *actual_name
14415 = language_class_name_from_physname (cu->language_defn,
14416 DW_STRING (attr));
14417 char *name = NULL;
14418
14419 if (actual_name != NULL)
14420 {
14421 char *die_name = dwarf2_name (die, cu);
14422
14423 if (die_name != NULL
14424 && strcmp (die_name, actual_name) != 0)
14425 {
14426 /* Strip off the class name from the full name.
14427 We want the prefix. */
14428 int die_name_len = strlen (die_name);
14429 int actual_name_len = strlen (actual_name);
14430
14431 /* Test for '::' as a sanity check. */
14432 if (actual_name_len > die_name_len + 2
3e43a32a
MS
14433 && actual_name[actual_name_len
14434 - die_name_len - 1] == ':')
abc72ce4
DE
14435 name =
14436 obsavestring (actual_name,
14437 actual_name_len - die_name_len - 2,
14438 &cu->objfile->objfile_obstack);
14439 }
14440 }
14441 xfree (actual_name);
14442 return name;
14443 }
14444 }
14445 }
14446
14447 return NULL;
14448}
14449
96408a79
SA
14450/* GCC might emit a nameless typedef that has a linkage name. Determine the
14451 prefix part in such case. See
14452 http://gcc.gnu.org/bugzilla/show_bug.cgi?id=47510. */
14453
14454static char *
14455anonymous_struct_prefix (struct die_info *die, struct dwarf2_cu *cu)
14456{
14457 struct attribute *attr;
14458 char *base;
14459
14460 if (die->tag != DW_TAG_class_type && die->tag != DW_TAG_interface_type
14461 && die->tag != DW_TAG_structure_type && die->tag != DW_TAG_union_type)
14462 return NULL;
14463
14464 attr = dwarf2_attr (die, DW_AT_name, cu);
14465 if (attr != NULL && DW_STRING (attr) != NULL)
14466 return NULL;
14467
14468 attr = dwarf2_attr (die, DW_AT_linkage_name, cu);
14469 if (attr == NULL)
14470 attr = dwarf2_attr (die, DW_AT_MIPS_linkage_name, cu);
14471 if (attr == NULL || DW_STRING (attr) == NULL)
14472 return NULL;
14473
14474 /* dwarf2_name had to be already called. */
14475 gdb_assert (DW_STRING_IS_CANONICAL (attr));
14476
14477 /* Strip the base name, keep any leading namespaces/classes. */
14478 base = strrchr (DW_STRING (attr), ':');
14479 if (base == NULL || base == DW_STRING (attr) || base[-1] != ':')
14480 return "";
14481
14482 return obsavestring (DW_STRING (attr), &base[-1] - DW_STRING (attr),
14483 &cu->objfile->objfile_obstack);
14484}
14485
fdde2d81 14486/* Return the name of the namespace/class that DIE is defined within,
0114d602 14487 or "" if we can't tell. The caller should not xfree the result.
fdde2d81 14488
0114d602
DJ
14489 For example, if we're within the method foo() in the following
14490 code:
14491
14492 namespace N {
14493 class C {
14494 void foo () {
14495 }
14496 };
14497 }
14498
14499 then determine_prefix on foo's die will return "N::C". */
fdde2d81 14500
0d5cff50 14501static const char *
e142c38c 14502determine_prefix (struct die_info *die, struct dwarf2_cu *cu)
63d06c5c 14503{
0114d602
DJ
14504 struct die_info *parent, *spec_die;
14505 struct dwarf2_cu *spec_cu;
14506 struct type *parent_type;
96408a79 14507 char *retval;
63d06c5c 14508
f55ee35c
JK
14509 if (cu->language != language_cplus && cu->language != language_java
14510 && cu->language != language_fortran)
0114d602
DJ
14511 return "";
14512
96408a79
SA
14513 retval = anonymous_struct_prefix (die, cu);
14514 if (retval)
14515 return retval;
14516
0114d602
DJ
14517 /* We have to be careful in the presence of DW_AT_specification.
14518 For example, with GCC 3.4, given the code
14519
14520 namespace N {
14521 void foo() {
14522 // Definition of N::foo.
14523 }
14524 }
14525
14526 then we'll have a tree of DIEs like this:
14527
14528 1: DW_TAG_compile_unit
14529 2: DW_TAG_namespace // N
14530 3: DW_TAG_subprogram // declaration of N::foo
14531 4: DW_TAG_subprogram // definition of N::foo
14532 DW_AT_specification // refers to die #3
14533
14534 Thus, when processing die #4, we have to pretend that we're in
14535 the context of its DW_AT_specification, namely the contex of die
14536 #3. */
14537 spec_cu = cu;
14538 spec_die = die_specification (die, &spec_cu);
14539 if (spec_die == NULL)
14540 parent = die->parent;
14541 else
63d06c5c 14542 {
0114d602
DJ
14543 parent = spec_die->parent;
14544 cu = spec_cu;
63d06c5c 14545 }
0114d602
DJ
14546
14547 if (parent == NULL)
14548 return "";
98bfdba5
PA
14549 else if (parent->building_fullname)
14550 {
14551 const char *name;
14552 const char *parent_name;
14553
14554 /* It has been seen on RealView 2.2 built binaries,
14555 DW_TAG_template_type_param types actually _defined_ as
14556 children of the parent class:
14557
14558 enum E {};
14559 template class <class Enum> Class{};
14560 Class<enum E> class_e;
14561
14562 1: DW_TAG_class_type (Class)
14563 2: DW_TAG_enumeration_type (E)
14564 3: DW_TAG_enumerator (enum1:0)
14565 3: DW_TAG_enumerator (enum2:1)
14566 ...
14567 2: DW_TAG_template_type_param
14568 DW_AT_type DW_FORM_ref_udata (E)
14569
14570 Besides being broken debug info, it can put GDB into an
14571 infinite loop. Consider:
14572
14573 When we're building the full name for Class<E>, we'll start
14574 at Class, and go look over its template type parameters,
14575 finding E. We'll then try to build the full name of E, and
14576 reach here. We're now trying to build the full name of E,
14577 and look over the parent DIE for containing scope. In the
14578 broken case, if we followed the parent DIE of E, we'd again
14579 find Class, and once again go look at its template type
14580 arguments, etc., etc. Simply don't consider such parent die
14581 as source-level parent of this die (it can't be, the language
14582 doesn't allow it), and break the loop here. */
14583 name = dwarf2_name (die, cu);
14584 parent_name = dwarf2_name (parent, cu);
14585 complaint (&symfile_complaints,
14586 _("template param type '%s' defined within parent '%s'"),
14587 name ? name : "<unknown>",
14588 parent_name ? parent_name : "<unknown>");
14589 return "";
14590 }
63d06c5c 14591 else
0114d602
DJ
14592 switch (parent->tag)
14593 {
63d06c5c 14594 case DW_TAG_namespace:
0114d602 14595 parent_type = read_type_die (parent, cu);
acebe513
UW
14596 /* GCC 4.0 and 4.1 had a bug (PR c++/28460) where they generated bogus
14597 DW_TAG_namespace DIEs with a name of "::" for the global namespace.
14598 Work around this problem here. */
14599 if (cu->language == language_cplus
14600 && strcmp (TYPE_TAG_NAME (parent_type), "::") == 0)
14601 return "";
0114d602
DJ
14602 /* We give a name to even anonymous namespaces. */
14603 return TYPE_TAG_NAME (parent_type);
63d06c5c 14604 case DW_TAG_class_type:
680b30c7 14605 case DW_TAG_interface_type:
63d06c5c 14606 case DW_TAG_structure_type:
0114d602 14607 case DW_TAG_union_type:
f55ee35c 14608 case DW_TAG_module:
0114d602
DJ
14609 parent_type = read_type_die (parent, cu);
14610 if (TYPE_TAG_NAME (parent_type) != NULL)
14611 return TYPE_TAG_NAME (parent_type);
14612 else
14613 /* An anonymous structure is only allowed non-static data
14614 members; no typedefs, no member functions, et cetera.
14615 So it does not need a prefix. */
14616 return "";
abc72ce4 14617 case DW_TAG_compile_unit:
95554aad 14618 case DW_TAG_partial_unit:
abc72ce4
DE
14619 /* gcc-4.5 -gdwarf-4 can drop the enclosing namespace. Cope. */
14620 if (cu->language == language_cplus
8b70b953 14621 && !VEC_empty (dwarf2_section_info_def, dwarf2_per_objfile->types)
abc72ce4
DE
14622 && die->child != NULL
14623 && (die->tag == DW_TAG_class_type
14624 || die->tag == DW_TAG_structure_type
14625 || die->tag == DW_TAG_union_type))
14626 {
14627 char *name = guess_full_die_structure_name (die, cu);
14628 if (name != NULL)
14629 return name;
14630 }
14631 return "";
63d06c5c 14632 default:
8176b9b8 14633 return determine_prefix (parent, cu);
63d06c5c 14634 }
63d06c5c
DC
14635}
14636
3e43a32a
MS
14637/* Return a newly-allocated string formed by concatenating PREFIX and SUFFIX
14638 with appropriate separator. If PREFIX or SUFFIX is NULL or empty, then
14639 simply copy the SUFFIX or PREFIX, respectively. If OBS is non-null, perform
14640 an obconcat, otherwise allocate storage for the result. The CU argument is
14641 used to determine the language and hence, the appropriate separator. */
987504bb 14642
f55ee35c 14643#define MAX_SEP_LEN 7 /* strlen ("__") + strlen ("_MOD_") */
63d06c5c
DC
14644
14645static char *
f55ee35c
JK
14646typename_concat (struct obstack *obs, const char *prefix, const char *suffix,
14647 int physname, struct dwarf2_cu *cu)
63d06c5c 14648{
f55ee35c 14649 const char *lead = "";
5c315b68 14650 const char *sep;
63d06c5c 14651
3e43a32a
MS
14652 if (suffix == NULL || suffix[0] == '\0'
14653 || prefix == NULL || prefix[0] == '\0')
987504bb
JJ
14654 sep = "";
14655 else if (cu->language == language_java)
14656 sep = ".";
f55ee35c
JK
14657 else if (cu->language == language_fortran && physname)
14658 {
14659 /* This is gfortran specific mangling. Normally DW_AT_linkage_name or
14660 DW_AT_MIPS_linkage_name is preferred and used instead. */
14661
14662 lead = "__";
14663 sep = "_MOD_";
14664 }
987504bb
JJ
14665 else
14666 sep = "::";
63d06c5c 14667
6dd47d34
DE
14668 if (prefix == NULL)
14669 prefix = "";
14670 if (suffix == NULL)
14671 suffix = "";
14672
987504bb
JJ
14673 if (obs == NULL)
14674 {
3e43a32a
MS
14675 char *retval
14676 = xmalloc (strlen (prefix) + MAX_SEP_LEN + strlen (suffix) + 1);
9a619af0 14677
f55ee35c
JK
14678 strcpy (retval, lead);
14679 strcat (retval, prefix);
6dd47d34
DE
14680 strcat (retval, sep);
14681 strcat (retval, suffix);
63d06c5c
DC
14682 return retval;
14683 }
987504bb
JJ
14684 else
14685 {
14686 /* We have an obstack. */
f55ee35c 14687 return obconcat (obs, lead, prefix, sep, suffix, (char *) NULL);
987504bb 14688 }
63d06c5c
DC
14689}
14690
c906108c
SS
14691/* Return sibling of die, NULL if no sibling. */
14692
f9aca02d 14693static struct die_info *
fba45db2 14694sibling_die (struct die_info *die)
c906108c 14695{
639d11d3 14696 return die->sibling;
c906108c
SS
14697}
14698
71c25dea
TT
14699/* Get name of a die, return NULL if not found. */
14700
14701static char *
14702dwarf2_canonicalize_name (char *name, struct dwarf2_cu *cu,
14703 struct obstack *obstack)
14704{
14705 if (name && cu->language == language_cplus)
14706 {
14707 char *canon_name = cp_canonicalize_string (name);
14708
14709 if (canon_name != NULL)
14710 {
14711 if (strcmp (canon_name, name) != 0)
14712 name = obsavestring (canon_name, strlen (canon_name),
14713 obstack);
14714 xfree (canon_name);
14715 }
14716 }
14717
14718 return name;
c906108c
SS
14719}
14720
9219021c
DC
14721/* Get name of a die, return NULL if not found. */
14722
14723static char *
e142c38c 14724dwarf2_name (struct die_info *die, struct dwarf2_cu *cu)
9219021c
DC
14725{
14726 struct attribute *attr;
14727
e142c38c 14728 attr = dwarf2_attr (die, DW_AT_name, cu);
53832f31
TT
14729 if ((!attr || !DW_STRING (attr))
14730 && die->tag != DW_TAG_class_type
14731 && die->tag != DW_TAG_interface_type
14732 && die->tag != DW_TAG_structure_type
14733 && die->tag != DW_TAG_union_type)
71c25dea
TT
14734 return NULL;
14735
14736 switch (die->tag)
14737 {
14738 case DW_TAG_compile_unit:
95554aad 14739 case DW_TAG_partial_unit:
71c25dea
TT
14740 /* Compilation units have a DW_AT_name that is a filename, not
14741 a source language identifier. */
14742 case DW_TAG_enumeration_type:
14743 case DW_TAG_enumerator:
14744 /* These tags always have simple identifiers already; no need
14745 to canonicalize them. */
14746 return DW_STRING (attr);
907af001 14747
418835cc
KS
14748 case DW_TAG_subprogram:
14749 /* Java constructors will all be named "<init>", so return
14750 the class name when we see this special case. */
14751 if (cu->language == language_java
14752 && DW_STRING (attr) != NULL
14753 && strcmp (DW_STRING (attr), "<init>") == 0)
14754 {
14755 struct dwarf2_cu *spec_cu = cu;
14756 struct die_info *spec_die;
14757
14758 /* GCJ will output '<init>' for Java constructor names.
14759 For this special case, return the name of the parent class. */
14760
14761 /* GCJ may output suprogram DIEs with AT_specification set.
14762 If so, use the name of the specified DIE. */
14763 spec_die = die_specification (die, &spec_cu);
14764 if (spec_die != NULL)
14765 return dwarf2_name (spec_die, spec_cu);
14766
14767 do
14768 {
14769 die = die->parent;
14770 if (die->tag == DW_TAG_class_type)
14771 return dwarf2_name (die, cu);
14772 }
95554aad
TT
14773 while (die->tag != DW_TAG_compile_unit
14774 && die->tag != DW_TAG_partial_unit);
418835cc 14775 }
907af001
UW
14776 break;
14777
14778 case DW_TAG_class_type:
14779 case DW_TAG_interface_type:
14780 case DW_TAG_structure_type:
14781 case DW_TAG_union_type:
14782 /* Some GCC versions emit spurious DW_AT_name attributes for unnamed
14783 structures or unions. These were of the form "._%d" in GCC 4.1,
14784 or simply "<anonymous struct>" or "<anonymous union>" in GCC 4.3
14785 and GCC 4.4. We work around this problem by ignoring these. */
53832f31
TT
14786 if (attr && DW_STRING (attr)
14787 && (strncmp (DW_STRING (attr), "._", 2) == 0
14788 || strncmp (DW_STRING (attr), "<anonymous", 10) == 0))
907af001 14789 return NULL;
53832f31
TT
14790
14791 /* GCC might emit a nameless typedef that has a linkage name. See
14792 http://gcc.gnu.org/bugzilla/show_bug.cgi?id=47510. */
14793 if (!attr || DW_STRING (attr) == NULL)
14794 {
df5c6c50 14795 char *demangled = NULL;
53832f31
TT
14796
14797 attr = dwarf2_attr (die, DW_AT_linkage_name, cu);
14798 if (attr == NULL)
14799 attr = dwarf2_attr (die, DW_AT_MIPS_linkage_name, cu);
14800
14801 if (attr == NULL || DW_STRING (attr) == NULL)
14802 return NULL;
14803
df5c6c50
JK
14804 /* Avoid demangling DW_STRING (attr) the second time on a second
14805 call for the same DIE. */
14806 if (!DW_STRING_IS_CANONICAL (attr))
14807 demangled = cplus_demangle (DW_STRING (attr), DMGL_TYPES);
53832f31
TT
14808
14809 if (demangled)
14810 {
96408a79
SA
14811 char *base;
14812
53832f31 14813 /* FIXME: we already did this for the partial symbol... */
96408a79
SA
14814 DW_STRING (attr) = obsavestring (demangled, strlen (demangled),
14815 &cu->objfile->objfile_obstack);
53832f31
TT
14816 DW_STRING_IS_CANONICAL (attr) = 1;
14817 xfree (demangled);
96408a79
SA
14818
14819 /* Strip any leading namespaces/classes, keep only the base name.
14820 DW_AT_name for named DIEs does not contain the prefixes. */
14821 base = strrchr (DW_STRING (attr), ':');
14822 if (base && base > DW_STRING (attr) && base[-1] == ':')
14823 return &base[1];
14824 else
14825 return DW_STRING (attr);
53832f31
TT
14826 }
14827 }
907af001
UW
14828 break;
14829
71c25dea 14830 default:
907af001
UW
14831 break;
14832 }
14833
14834 if (!DW_STRING_IS_CANONICAL (attr))
14835 {
14836 DW_STRING (attr)
14837 = dwarf2_canonicalize_name (DW_STRING (attr), cu,
14838 &cu->objfile->objfile_obstack);
14839 DW_STRING_IS_CANONICAL (attr) = 1;
71c25dea 14840 }
907af001 14841 return DW_STRING (attr);
9219021c
DC
14842}
14843
14844/* Return the die that this die in an extension of, or NULL if there
f2f0e013
DJ
14845 is none. *EXT_CU is the CU containing DIE on input, and the CU
14846 containing the return value on output. */
9219021c
DC
14847
14848static struct die_info *
f2f0e013 14849dwarf2_extension (struct die_info *die, struct dwarf2_cu **ext_cu)
9219021c
DC
14850{
14851 struct attribute *attr;
9219021c 14852
f2f0e013 14853 attr = dwarf2_attr (die, DW_AT_extension, *ext_cu);
9219021c
DC
14854 if (attr == NULL)
14855 return NULL;
14856
f2f0e013 14857 return follow_die_ref (die, attr, ext_cu);
9219021c
DC
14858}
14859
c906108c
SS
14860/* Convert a DIE tag into its string name. */
14861
f39c6ffd 14862static const char *
aa1ee363 14863dwarf_tag_name (unsigned tag)
c906108c 14864{
f39c6ffd
TT
14865 const char *name = get_DW_TAG_name (tag);
14866
14867 if (name == NULL)
14868 return "DW_TAG_<unknown>";
14869
14870 return name;
c906108c
SS
14871}
14872
14873/* Convert a DWARF attribute code into its string name. */
14874
f39c6ffd 14875static const char *
aa1ee363 14876dwarf_attr_name (unsigned attr)
c906108c 14877{
f39c6ffd
TT
14878 const char *name;
14879
c764a876 14880#ifdef MIPS /* collides with DW_AT_HP_block_index */
f39c6ffd
TT
14881 if (attr == DW_AT_MIPS_fde)
14882 return "DW_AT_MIPS_fde";
14883#else
14884 if (attr == DW_AT_HP_block_index)
14885 return "DW_AT_HP_block_index";
c764a876 14886#endif
f39c6ffd
TT
14887
14888 name = get_DW_AT_name (attr);
14889
14890 if (name == NULL)
14891 return "DW_AT_<unknown>";
14892
14893 return name;
c906108c
SS
14894}
14895
14896/* Convert a DWARF value form code into its string name. */
14897
f39c6ffd 14898static const char *
aa1ee363 14899dwarf_form_name (unsigned form)
c906108c 14900{
f39c6ffd
TT
14901 const char *name = get_DW_FORM_name (form);
14902
14903 if (name == NULL)
14904 return "DW_FORM_<unknown>";
14905
14906 return name;
c906108c
SS
14907}
14908
14909static char *
fba45db2 14910dwarf_bool_name (unsigned mybool)
c906108c
SS
14911{
14912 if (mybool)
14913 return "TRUE";
14914 else
14915 return "FALSE";
14916}
14917
14918/* Convert a DWARF type code into its string name. */
14919
f39c6ffd 14920static const char *
aa1ee363 14921dwarf_type_encoding_name (unsigned enc)
c906108c 14922{
f39c6ffd 14923 const char *name = get_DW_ATE_name (enc);
c906108c 14924
f39c6ffd
TT
14925 if (name == NULL)
14926 return "DW_ATE_<unknown>";
c906108c 14927
f39c6ffd 14928 return name;
c906108c 14929}
c906108c 14930
f9aca02d 14931static void
d97bc12b 14932dump_die_shallow (struct ui_file *f, int indent, struct die_info *die)
c906108c
SS
14933{
14934 unsigned int i;
14935
d97bc12b
DE
14936 print_spaces (indent, f);
14937 fprintf_unfiltered (f, "Die: %s (abbrev %d, offset 0x%x)\n",
b64f50a1 14938 dwarf_tag_name (die->tag), die->abbrev, die->offset.sect_off);
d97bc12b
DE
14939
14940 if (die->parent != NULL)
14941 {
14942 print_spaces (indent, f);
14943 fprintf_unfiltered (f, " parent at offset: 0x%x\n",
b64f50a1 14944 die->parent->offset.sect_off);
d97bc12b
DE
14945 }
14946
14947 print_spaces (indent, f);
14948 fprintf_unfiltered (f, " has children: %s\n",
639d11d3 14949 dwarf_bool_name (die->child != NULL));
c906108c 14950
d97bc12b
DE
14951 print_spaces (indent, f);
14952 fprintf_unfiltered (f, " attributes:\n");
14953
c906108c
SS
14954 for (i = 0; i < die->num_attrs; ++i)
14955 {
d97bc12b
DE
14956 print_spaces (indent, f);
14957 fprintf_unfiltered (f, " %s (%s) ",
c906108c
SS
14958 dwarf_attr_name (die->attrs[i].name),
14959 dwarf_form_name (die->attrs[i].form));
d97bc12b 14960
c906108c
SS
14961 switch (die->attrs[i].form)
14962 {
c906108c 14963 case DW_FORM_addr:
3019eac3 14964 case DW_FORM_GNU_addr_index:
d97bc12b 14965 fprintf_unfiltered (f, "address: ");
5af949e3 14966 fputs_filtered (hex_string (DW_ADDR (&die->attrs[i])), f);
c906108c
SS
14967 break;
14968 case DW_FORM_block2:
14969 case DW_FORM_block4:
14970 case DW_FORM_block:
14971 case DW_FORM_block1:
3e43a32a
MS
14972 fprintf_unfiltered (f, "block: size %d",
14973 DW_BLOCK (&die->attrs[i])->size);
c906108c 14974 break;
2dc7f7b3
TT
14975 case DW_FORM_exprloc:
14976 fprintf_unfiltered (f, "expression: size %u",
14977 DW_BLOCK (&die->attrs[i])->size);
14978 break;
4568ecf9
DE
14979 case DW_FORM_ref_addr:
14980 fprintf_unfiltered (f, "ref address: ");
14981 fputs_filtered (hex_string (DW_UNSND (&die->attrs[i])), f);
14982 break;
10b3939b
DJ
14983 case DW_FORM_ref1:
14984 case DW_FORM_ref2:
14985 case DW_FORM_ref4:
4568ecf9
DE
14986 case DW_FORM_ref8:
14987 case DW_FORM_ref_udata:
d97bc12b 14988 fprintf_unfiltered (f, "constant ref: 0x%lx (adjusted)",
4568ecf9 14989 (long) (DW_UNSND (&die->attrs[i])));
10b3939b 14990 break;
c906108c
SS
14991 case DW_FORM_data1:
14992 case DW_FORM_data2:
14993 case DW_FORM_data4:
ce5d95e1 14994 case DW_FORM_data8:
c906108c
SS
14995 case DW_FORM_udata:
14996 case DW_FORM_sdata:
43bbcdc2
PH
14997 fprintf_unfiltered (f, "constant: %s",
14998 pulongest (DW_UNSND (&die->attrs[i])));
c906108c 14999 break;
2dc7f7b3
TT
15000 case DW_FORM_sec_offset:
15001 fprintf_unfiltered (f, "section offset: %s",
15002 pulongest (DW_UNSND (&die->attrs[i])));
15003 break;
55f1336d 15004 case DW_FORM_ref_sig8:
348e048f
DE
15005 if (DW_SIGNATURED_TYPE (&die->attrs[i]) != NULL)
15006 fprintf_unfiltered (f, "signatured type, offset: 0x%x",
b64f50a1 15007 DW_SIGNATURED_TYPE (&die->attrs[i])->per_cu.offset.sect_off);
348e048f
DE
15008 else
15009 fprintf_unfiltered (f, "signatured type, offset: unknown");
15010 break;
c906108c 15011 case DW_FORM_string:
4bdf3d34 15012 case DW_FORM_strp:
3019eac3 15013 case DW_FORM_GNU_str_index:
8285870a 15014 fprintf_unfiltered (f, "string: \"%s\" (%s canonicalized)",
c906108c 15015 DW_STRING (&die->attrs[i])
8285870a
JK
15016 ? DW_STRING (&die->attrs[i]) : "",
15017 DW_STRING_IS_CANONICAL (&die->attrs[i]) ? "is" : "not");
c906108c
SS
15018 break;
15019 case DW_FORM_flag:
15020 if (DW_UNSND (&die->attrs[i]))
d97bc12b 15021 fprintf_unfiltered (f, "flag: TRUE");
c906108c 15022 else
d97bc12b 15023 fprintf_unfiltered (f, "flag: FALSE");
c906108c 15024 break;
2dc7f7b3
TT
15025 case DW_FORM_flag_present:
15026 fprintf_unfiltered (f, "flag: TRUE");
15027 break;
a8329558 15028 case DW_FORM_indirect:
0963b4bd
MS
15029 /* The reader will have reduced the indirect form to
15030 the "base form" so this form should not occur. */
3e43a32a
MS
15031 fprintf_unfiltered (f,
15032 "unexpected attribute form: DW_FORM_indirect");
a8329558 15033 break;
c906108c 15034 default:
d97bc12b 15035 fprintf_unfiltered (f, "unsupported attribute form: %d.",
c5aa993b 15036 die->attrs[i].form);
d97bc12b 15037 break;
c906108c 15038 }
d97bc12b 15039 fprintf_unfiltered (f, "\n");
c906108c
SS
15040 }
15041}
15042
f9aca02d 15043static void
d97bc12b 15044dump_die_for_error (struct die_info *die)
c906108c 15045{
d97bc12b
DE
15046 dump_die_shallow (gdb_stderr, 0, die);
15047}
15048
15049static void
15050dump_die_1 (struct ui_file *f, int level, int max_level, struct die_info *die)
15051{
15052 int indent = level * 4;
15053
15054 gdb_assert (die != NULL);
15055
15056 if (level >= max_level)
15057 return;
15058
15059 dump_die_shallow (f, indent, die);
15060
15061 if (die->child != NULL)
c906108c 15062 {
d97bc12b
DE
15063 print_spaces (indent, f);
15064 fprintf_unfiltered (f, " Children:");
15065 if (level + 1 < max_level)
15066 {
15067 fprintf_unfiltered (f, "\n");
15068 dump_die_1 (f, level + 1, max_level, die->child);
15069 }
15070 else
15071 {
3e43a32a
MS
15072 fprintf_unfiltered (f,
15073 " [not printed, max nesting level reached]\n");
d97bc12b
DE
15074 }
15075 }
15076
15077 if (die->sibling != NULL && level > 0)
15078 {
15079 dump_die_1 (f, level, max_level, die->sibling);
c906108c
SS
15080 }
15081}
15082
d97bc12b
DE
15083/* This is called from the pdie macro in gdbinit.in.
15084 It's not static so gcc will keep a copy callable from gdb. */
15085
15086void
15087dump_die (struct die_info *die, int max_level)
15088{
15089 dump_die_1 (gdb_stdlog, 0, max_level, die);
15090}
15091
f9aca02d 15092static void
51545339 15093store_in_ref_table (struct die_info *die, struct dwarf2_cu *cu)
c906108c 15094{
51545339 15095 void **slot;
c906108c 15096
b64f50a1
JK
15097 slot = htab_find_slot_with_hash (cu->die_hash, die, die->offset.sect_off,
15098 INSERT);
51545339
DJ
15099
15100 *slot = die;
c906108c
SS
15101}
15102
b64f50a1
JK
15103/* DW_ADDR is always stored already as sect_offset; despite for the forms
15104 besides DW_FORM_ref_addr it is stored as cu_offset in the DWARF file. */
15105
93311388
DE
15106static int
15107is_ref_attr (struct attribute *attr)
c906108c 15108{
c906108c
SS
15109 switch (attr->form)
15110 {
15111 case DW_FORM_ref_addr:
c906108c
SS
15112 case DW_FORM_ref1:
15113 case DW_FORM_ref2:
15114 case DW_FORM_ref4:
613e1657 15115 case DW_FORM_ref8:
c906108c 15116 case DW_FORM_ref_udata:
93311388 15117 return 1;
c906108c 15118 default:
93311388 15119 return 0;
c906108c 15120 }
93311388
DE
15121}
15122
b64f50a1
JK
15123/* Return DIE offset of ATTR. Return 0 with complaint if ATTR is not of the
15124 required kind. */
15125
15126static sect_offset
93311388
DE
15127dwarf2_get_ref_die_offset (struct attribute *attr)
15128{
4568ecf9 15129 sect_offset retval = { DW_UNSND (attr) };
b64f50a1 15130
93311388 15131 if (is_ref_attr (attr))
b64f50a1 15132 return retval;
93311388 15133
b64f50a1 15134 retval.sect_off = 0;
93311388
DE
15135 complaint (&symfile_complaints,
15136 _("unsupported die ref attribute form: '%s'"),
15137 dwarf_form_name (attr->form));
b64f50a1 15138 return retval;
c906108c
SS
15139}
15140
43bbcdc2
PH
15141/* Return the constant value held by ATTR. Return DEFAULT_VALUE if
15142 * the value held by the attribute is not constant. */
a02abb62 15143
43bbcdc2 15144static LONGEST
a02abb62
JB
15145dwarf2_get_attr_constant_value (struct attribute *attr, int default_value)
15146{
15147 if (attr->form == DW_FORM_sdata)
15148 return DW_SND (attr);
15149 else if (attr->form == DW_FORM_udata
15150 || attr->form == DW_FORM_data1
15151 || attr->form == DW_FORM_data2
15152 || attr->form == DW_FORM_data4
15153 || attr->form == DW_FORM_data8)
15154 return DW_UNSND (attr);
15155 else
15156 {
3e43a32a
MS
15157 complaint (&symfile_complaints,
15158 _("Attribute value is not a constant (%s)"),
a02abb62
JB
15159 dwarf_form_name (attr->form));
15160 return default_value;
15161 }
15162}
15163
03dd20cc 15164/* THIS_CU has a reference to PER_CU. If necessary, load the new compilation
348e048f
DE
15165 unit and add it to our queue.
15166 The result is non-zero if PER_CU was queued, otherwise the result is zero
15167 meaning either PER_CU is already queued or it is already loaded. */
03dd20cc 15168
348e048f 15169static int
03dd20cc 15170maybe_queue_comp_unit (struct dwarf2_cu *this_cu,
95554aad
TT
15171 struct dwarf2_per_cu_data *per_cu,
15172 enum language pretend_language)
03dd20cc 15173{
98bfdba5
PA
15174 /* We may arrive here during partial symbol reading, if we need full
15175 DIEs to process an unusual case (e.g. template arguments). Do
15176 not queue PER_CU, just tell our caller to load its DIEs. */
15177 if (dwarf2_per_objfile->reading_partial_symbols)
15178 {
15179 if (per_cu->cu == NULL || per_cu->cu->dies == NULL)
15180 return 1;
15181 return 0;
15182 }
15183
03dd20cc
DJ
15184 /* Mark the dependence relation so that we don't flush PER_CU
15185 too early. */
15186 dwarf2_add_dependence (this_cu, per_cu);
15187
15188 /* If it's already on the queue, we have nothing to do. */
15189 if (per_cu->queued)
348e048f 15190 return 0;
03dd20cc
DJ
15191
15192 /* If the compilation unit is already loaded, just mark it as
15193 used. */
15194 if (per_cu->cu != NULL)
15195 {
15196 per_cu->cu->last_used = 0;
348e048f 15197 return 0;
03dd20cc
DJ
15198 }
15199
15200 /* Add it to the queue. */
95554aad 15201 queue_comp_unit (per_cu, pretend_language);
348e048f
DE
15202
15203 return 1;
15204}
15205
15206/* Follow reference or signature attribute ATTR of SRC_DIE.
15207 On entry *REF_CU is the CU of SRC_DIE.
15208 On exit *REF_CU is the CU of the result. */
15209
15210static struct die_info *
15211follow_die_ref_or_sig (struct die_info *src_die, struct attribute *attr,
15212 struct dwarf2_cu **ref_cu)
15213{
15214 struct die_info *die;
15215
15216 if (is_ref_attr (attr))
15217 die = follow_die_ref (src_die, attr, ref_cu);
55f1336d 15218 else if (attr->form == DW_FORM_ref_sig8)
348e048f
DE
15219 die = follow_die_sig (src_die, attr, ref_cu);
15220 else
15221 {
15222 dump_die_for_error (src_die);
15223 error (_("Dwarf Error: Expected reference attribute [in module %s]"),
15224 (*ref_cu)->objfile->name);
15225 }
15226
15227 return die;
03dd20cc
DJ
15228}
15229
5c631832 15230/* Follow reference OFFSET.
673bfd45
DE
15231 On entry *REF_CU is the CU of the source die referencing OFFSET.
15232 On exit *REF_CU is the CU of the result.
15233 Returns NULL if OFFSET is invalid. */
f504f079 15234
f9aca02d 15235static struct die_info *
b64f50a1 15236follow_die_offset (sect_offset offset, struct dwarf2_cu **ref_cu)
c906108c 15237{
10b3939b 15238 struct die_info temp_die;
f2f0e013 15239 struct dwarf2_cu *target_cu, *cu = *ref_cu;
10b3939b 15240
348e048f
DE
15241 gdb_assert (cu->per_cu != NULL);
15242
98bfdba5
PA
15243 target_cu = cu;
15244
3019eac3 15245 if (cu->per_cu->is_debug_types)
348e048f
DE
15246 {
15247 /* .debug_types CUs cannot reference anything outside their CU.
15248 If they need to, they have to reference a signatured type via
55f1336d 15249 DW_FORM_ref_sig8. */
348e048f 15250 if (! offset_in_cu_p (&cu->header, offset))
5c631832 15251 return NULL;
348e048f
DE
15252 }
15253 else if (! offset_in_cu_p (&cu->header, offset))
10b3939b
DJ
15254 {
15255 struct dwarf2_per_cu_data *per_cu;
9a619af0 15256
45452591 15257 per_cu = dwarf2_find_containing_comp_unit (offset, cu->objfile);
03dd20cc
DJ
15258
15259 /* If necessary, add it to the queue and load its DIEs. */
95554aad
TT
15260 if (maybe_queue_comp_unit (cu, per_cu, cu->language))
15261 load_full_comp_unit (per_cu, cu->language);
03dd20cc 15262
10b3939b
DJ
15263 target_cu = per_cu->cu;
15264 }
98bfdba5
PA
15265 else if (cu->dies == NULL)
15266 {
15267 /* We're loading full DIEs during partial symbol reading. */
15268 gdb_assert (dwarf2_per_objfile->reading_partial_symbols);
95554aad 15269 load_full_comp_unit (cu->per_cu, language_minimal);
98bfdba5 15270 }
c906108c 15271
f2f0e013 15272 *ref_cu = target_cu;
51545339 15273 temp_die.offset = offset;
b64f50a1 15274 return htab_find_with_hash (target_cu->die_hash, &temp_die, offset.sect_off);
5c631832 15275}
10b3939b 15276
5c631832
JK
15277/* Follow reference attribute ATTR of SRC_DIE.
15278 On entry *REF_CU is the CU of SRC_DIE.
15279 On exit *REF_CU is the CU of the result. */
15280
15281static struct die_info *
15282follow_die_ref (struct die_info *src_die, struct attribute *attr,
15283 struct dwarf2_cu **ref_cu)
15284{
b64f50a1 15285 sect_offset offset = dwarf2_get_ref_die_offset (attr);
5c631832
JK
15286 struct dwarf2_cu *cu = *ref_cu;
15287 struct die_info *die;
15288
15289 die = follow_die_offset (offset, ref_cu);
15290 if (!die)
15291 error (_("Dwarf Error: Cannot find DIE at 0x%x referenced from DIE "
15292 "at 0x%x [in module %s]"),
b64f50a1 15293 offset.sect_off, src_die->offset.sect_off, cu->objfile->name);
348e048f 15294
5c631832
JK
15295 return die;
15296}
15297
d83e736b
JK
15298/* Return DWARF block referenced by DW_AT_location of DIE at OFFSET at PER_CU.
15299 Returned value is intended for DW_OP_call*. Returned
15300 dwarf2_locexpr_baton->data has lifetime of PER_CU->OBJFILE. */
5c631832
JK
15301
15302struct dwarf2_locexpr_baton
b64f50a1 15303dwarf2_fetch_die_location_block (cu_offset offset_in_cu,
8cf6f0b1
TT
15304 struct dwarf2_per_cu_data *per_cu,
15305 CORE_ADDR (*get_frame_pc) (void *baton),
15306 void *baton)
5c631832 15307{
b64f50a1 15308 sect_offset offset = { per_cu->offset.sect_off + offset_in_cu.cu_off };
918dd910 15309 struct dwarf2_cu *cu;
5c631832
JK
15310 struct die_info *die;
15311 struct attribute *attr;
15312 struct dwarf2_locexpr_baton retval;
15313
8cf6f0b1
TT
15314 dw2_setup (per_cu->objfile);
15315
918dd910
JK
15316 if (per_cu->cu == NULL)
15317 load_cu (per_cu);
15318 cu = per_cu->cu;
15319
5c631832
JK
15320 die = follow_die_offset (offset, &cu);
15321 if (!die)
15322 error (_("Dwarf Error: Cannot find DIE at 0x%x referenced in module %s"),
b64f50a1 15323 offset.sect_off, per_cu->objfile->name);
5c631832
JK
15324
15325 attr = dwarf2_attr (die, DW_AT_location, cu);
15326 if (!attr)
15327 {
e103e986
JK
15328 /* DWARF: "If there is no such attribute, then there is no effect.".
15329 DATA is ignored if SIZE is 0. */
5c631832 15330
e103e986 15331 retval.data = NULL;
5c631832
JK
15332 retval.size = 0;
15333 }
8cf6f0b1
TT
15334 else if (attr_form_is_section_offset (attr))
15335 {
15336 struct dwarf2_loclist_baton loclist_baton;
15337 CORE_ADDR pc = (*get_frame_pc) (baton);
15338 size_t size;
15339
15340 fill_in_loclist_baton (cu, &loclist_baton, attr);
15341
15342 retval.data = dwarf2_find_location_expression (&loclist_baton,
15343 &size, pc);
15344 retval.size = size;
15345 }
5c631832
JK
15346 else
15347 {
15348 if (!attr_form_is_block (attr))
15349 error (_("Dwarf Error: DIE at 0x%x referenced in module %s "
15350 "is neither DW_FORM_block* nor DW_FORM_exprloc"),
b64f50a1 15351 offset.sect_off, per_cu->objfile->name);
5c631832
JK
15352
15353 retval.data = DW_BLOCK (attr)->data;
15354 retval.size = DW_BLOCK (attr)->size;
15355 }
15356 retval.per_cu = cu->per_cu;
918dd910 15357
918dd910
JK
15358 age_cached_comp_units ();
15359
5c631832 15360 return retval;
348e048f
DE
15361}
15362
8a9b8146
TT
15363/* Return the type of the DIE at DIE_OFFSET in the CU named by
15364 PER_CU. */
15365
15366struct type *
b64f50a1 15367dwarf2_get_die_type (cu_offset die_offset,
8a9b8146
TT
15368 struct dwarf2_per_cu_data *per_cu)
15369{
b64f50a1
JK
15370 sect_offset die_offset_sect;
15371
8a9b8146 15372 dw2_setup (per_cu->objfile);
b64f50a1
JK
15373
15374 die_offset_sect.sect_off = per_cu->offset.sect_off + die_offset.cu_off;
15375 return get_die_type_at_offset (die_offset_sect, per_cu);
8a9b8146
TT
15376}
15377
348e048f
DE
15378/* Follow the signature attribute ATTR in SRC_DIE.
15379 On entry *REF_CU is the CU of SRC_DIE.
15380 On exit *REF_CU is the CU of the result. */
15381
15382static struct die_info *
15383follow_die_sig (struct die_info *src_die, struct attribute *attr,
15384 struct dwarf2_cu **ref_cu)
15385{
15386 struct objfile *objfile = (*ref_cu)->objfile;
15387 struct die_info temp_die;
15388 struct signatured_type *sig_type = DW_SIGNATURED_TYPE (attr);
15389 struct dwarf2_cu *sig_cu;
15390 struct die_info *die;
15391
15392 /* sig_type will be NULL if the signatured type is missing from
15393 the debug info. */
15394 if (sig_type == NULL)
15395 error (_("Dwarf Error: Cannot find signatured DIE referenced from DIE "
15396 "at 0x%x [in module %s]"),
b64f50a1 15397 src_die->offset.sect_off, objfile->name);
348e048f
DE
15398
15399 /* If necessary, add it to the queue and load its DIEs. */
15400
95554aad 15401 if (maybe_queue_comp_unit (*ref_cu, &sig_type->per_cu, language_minimal))
a0f42c21 15402 read_signatured_type (sig_type);
348e048f
DE
15403
15404 gdb_assert (sig_type->per_cu.cu != NULL);
15405
15406 sig_cu = sig_type->per_cu.cu;
3019eac3
DE
15407 gdb_assert (sig_type->type_offset_in_section.sect_off != 0);
15408 temp_die.offset = sig_type->type_offset_in_section;
b64f50a1
JK
15409 die = htab_find_with_hash (sig_cu->die_hash, &temp_die,
15410 temp_die.offset.sect_off);
348e048f
DE
15411 if (die)
15412 {
15413 *ref_cu = sig_cu;
15414 return die;
15415 }
15416
3e43a32a
MS
15417 error (_("Dwarf Error: Cannot find signatured DIE at 0x%x referenced "
15418 "from DIE at 0x%x [in module %s]"),
b64f50a1 15419 temp_die.offset.sect_off, src_die->offset.sect_off, objfile->name);
348e048f
DE
15420}
15421
15422/* Given an offset of a signatured type, return its signatured_type. */
15423
15424static struct signatured_type *
8b70b953
TT
15425lookup_signatured_type_at_offset (struct objfile *objfile,
15426 struct dwarf2_section_info *section,
b64f50a1 15427 sect_offset offset)
348e048f 15428{
b64f50a1 15429 gdb_byte *info_ptr = section->buffer + offset.sect_off;
348e048f
DE
15430 unsigned int length, initial_length_size;
15431 unsigned int sig_offset;
52dc124a 15432 struct signatured_type find_entry, *sig_type;
348e048f
DE
15433
15434 length = read_initial_length (objfile->obfd, info_ptr, &initial_length_size);
15435 sig_offset = (initial_length_size
15436 + 2 /*version*/
15437 + (initial_length_size == 4 ? 4 : 8) /*debug_abbrev_offset*/
15438 + 1 /*address_size*/);
15439 find_entry.signature = bfd_get_64 (objfile->obfd, info_ptr + sig_offset);
52dc124a 15440 sig_type = htab_find (dwarf2_per_objfile->signatured_types, &find_entry);
348e048f
DE
15441
15442 /* This is only used to lookup previously recorded types.
15443 If we didn't find it, it's our bug. */
52dc124a
DE
15444 gdb_assert (sig_type != NULL);
15445 gdb_assert (offset.sect_off == sig_type->per_cu.offset.sect_off);
348e048f 15446
52dc124a 15447 return sig_type;
348e048f
DE
15448}
15449
e5fe5e75 15450/* Load the DIEs associated with type unit PER_CU into memory. */
348e048f
DE
15451
15452static void
e5fe5e75 15453load_full_type_unit (struct dwarf2_per_cu_data *per_cu)
348e048f 15454{
e5fe5e75 15455 struct objfile *objfile = per_cu->objfile;
3019eac3 15456 struct dwarf2_section_info *sect = per_cu->info_or_types_section;
b64f50a1 15457 sect_offset offset = per_cu->offset;
52dc124a 15458 struct signatured_type *sig_type;
348e048f 15459
8b70b953 15460 dwarf2_read_section (objfile, sect);
be391dca 15461
348e048f 15462 /* We have the section offset, but we need the signature to do the
e5fe5e75
DE
15463 hash table lookup. */
15464 /* FIXME: This is sorta unnecessary, read_signatured_type only uses
15465 the signature to assert we found the right one.
15466 Ok, but it's a lot of work. We should simplify things so any needed
15467 assert doesn't require all this clumsiness. */
52dc124a 15468 sig_type = lookup_signatured_type_at_offset (objfile, sect, offset);
348e048f 15469
dee91e82 15470 gdb_assert (&sig_type->per_cu == per_cu);
52dc124a 15471 gdb_assert (sig_type->per_cu.cu == NULL);
348e048f 15472
52dc124a 15473 read_signatured_type (sig_type);
348e048f 15474
52dc124a 15475 gdb_assert (sig_type->per_cu.cu != NULL);
348e048f
DE
15476}
15477
dee91e82
DE
15478/* die_reader_func for read_signatured_type.
15479 This is identical to load_full_comp_unit_reader,
15480 but is kept separate for now. */
348e048f
DE
15481
15482static void
dee91e82
DE
15483read_signatured_type_reader (const struct die_reader_specs *reader,
15484 gdb_byte *info_ptr,
15485 struct die_info *comp_unit_die,
15486 int has_children,
15487 void *data)
348e048f 15488{
dee91e82 15489 struct dwarf2_cu *cu = reader->cu;
348e048f 15490
dee91e82
DE
15491 gdb_assert (cu->die_hash == NULL);
15492 cu->die_hash =
15493 htab_create_alloc_ex (cu->header.length / 12,
15494 die_hash,
15495 die_eq,
15496 NULL,
15497 &cu->comp_unit_obstack,
15498 hashtab_obstack_allocate,
15499 dummy_obstack_deallocate);
348e048f 15500
dee91e82
DE
15501 if (has_children)
15502 comp_unit_die->child = read_die_and_siblings (reader, info_ptr,
15503 &info_ptr, comp_unit_die);
15504 cu->dies = comp_unit_die;
15505 /* comp_unit_die is not stored in die_hash, no need. */
348e048f
DE
15506
15507 /* We try not to read any attributes in this function, because not
9cdd5dbd 15508 all CUs needed for references have been loaded yet, and symbol
348e048f 15509 table processing isn't initialized. But we have to set the CU language,
dee91e82
DE
15510 or we won't be able to build types correctly.
15511 Similarly, if we do not read the producer, we can not apply
15512 producer-specific interpretation. */
95554aad 15513 prepare_one_comp_unit (cu, cu->dies, language_minimal);
dee91e82 15514}
348e048f 15515
3019eac3
DE
15516/* Read in a signatured type and build its CU and DIEs.
15517 If the type is a stub for the real type in a DWO file,
15518 read in the real type from the DWO file as well. */
dee91e82
DE
15519
15520static void
15521read_signatured_type (struct signatured_type *sig_type)
15522{
15523 struct dwarf2_per_cu_data *per_cu = &sig_type->per_cu;
348e048f 15524
3019eac3 15525 gdb_assert (per_cu->is_debug_types);
dee91e82 15526 gdb_assert (per_cu->cu == NULL);
348e048f 15527
fd820528 15528 init_cutu_and_read_dies (per_cu, 0, 1, read_signatured_type_reader, NULL);
c906108c
SS
15529}
15530
c906108c
SS
15531/* Decode simple location descriptions.
15532 Given a pointer to a dwarf block that defines a location, compute
15533 the location and return the value.
15534
4cecd739
DJ
15535 NOTE drow/2003-11-18: This function is called in two situations
15536 now: for the address of static or global variables (partial symbols
15537 only) and for offsets into structures which are expected to be
15538 (more or less) constant. The partial symbol case should go away,
15539 and only the constant case should remain. That will let this
15540 function complain more accurately. A few special modes are allowed
15541 without complaint for global variables (for instance, global
15542 register values and thread-local values).
c906108c
SS
15543
15544 A location description containing no operations indicates that the
4cecd739 15545 object is optimized out. The return value is 0 for that case.
6b992462
DJ
15546 FIXME drow/2003-11-16: No callers check for this case any more; soon all
15547 callers will only want a very basic result and this can become a
21ae7a4d
JK
15548 complaint.
15549
15550 Note that stack[0] is unused except as a default error return. */
c906108c
SS
15551
15552static CORE_ADDR
e7c27a73 15553decode_locdesc (struct dwarf_block *blk, struct dwarf2_cu *cu)
c906108c 15554{
e7c27a73 15555 struct objfile *objfile = cu->objfile;
21ae7a4d
JK
15556 int i;
15557 int size = blk->size;
15558 gdb_byte *data = blk->data;
15559 CORE_ADDR stack[64];
15560 int stacki;
15561 unsigned int bytes_read, unsnd;
15562 gdb_byte op;
c906108c 15563
21ae7a4d
JK
15564 i = 0;
15565 stacki = 0;
15566 stack[stacki] = 0;
15567 stack[++stacki] = 0;
15568
15569 while (i < size)
15570 {
15571 op = data[i++];
15572 switch (op)
15573 {
15574 case DW_OP_lit0:
15575 case DW_OP_lit1:
15576 case DW_OP_lit2:
15577 case DW_OP_lit3:
15578 case DW_OP_lit4:
15579 case DW_OP_lit5:
15580 case DW_OP_lit6:
15581 case DW_OP_lit7:
15582 case DW_OP_lit8:
15583 case DW_OP_lit9:
15584 case DW_OP_lit10:
15585 case DW_OP_lit11:
15586 case DW_OP_lit12:
15587 case DW_OP_lit13:
15588 case DW_OP_lit14:
15589 case DW_OP_lit15:
15590 case DW_OP_lit16:
15591 case DW_OP_lit17:
15592 case DW_OP_lit18:
15593 case DW_OP_lit19:
15594 case DW_OP_lit20:
15595 case DW_OP_lit21:
15596 case DW_OP_lit22:
15597 case DW_OP_lit23:
15598 case DW_OP_lit24:
15599 case DW_OP_lit25:
15600 case DW_OP_lit26:
15601 case DW_OP_lit27:
15602 case DW_OP_lit28:
15603 case DW_OP_lit29:
15604 case DW_OP_lit30:
15605 case DW_OP_lit31:
15606 stack[++stacki] = op - DW_OP_lit0;
15607 break;
f1bea926 15608
21ae7a4d
JK
15609 case DW_OP_reg0:
15610 case DW_OP_reg1:
15611 case DW_OP_reg2:
15612 case DW_OP_reg3:
15613 case DW_OP_reg4:
15614 case DW_OP_reg5:
15615 case DW_OP_reg6:
15616 case DW_OP_reg7:
15617 case DW_OP_reg8:
15618 case DW_OP_reg9:
15619 case DW_OP_reg10:
15620 case DW_OP_reg11:
15621 case DW_OP_reg12:
15622 case DW_OP_reg13:
15623 case DW_OP_reg14:
15624 case DW_OP_reg15:
15625 case DW_OP_reg16:
15626 case DW_OP_reg17:
15627 case DW_OP_reg18:
15628 case DW_OP_reg19:
15629 case DW_OP_reg20:
15630 case DW_OP_reg21:
15631 case DW_OP_reg22:
15632 case DW_OP_reg23:
15633 case DW_OP_reg24:
15634 case DW_OP_reg25:
15635 case DW_OP_reg26:
15636 case DW_OP_reg27:
15637 case DW_OP_reg28:
15638 case DW_OP_reg29:
15639 case DW_OP_reg30:
15640 case DW_OP_reg31:
15641 stack[++stacki] = op - DW_OP_reg0;
15642 if (i < size)
15643 dwarf2_complex_location_expr_complaint ();
15644 break;
c906108c 15645
21ae7a4d
JK
15646 case DW_OP_regx:
15647 unsnd = read_unsigned_leb128 (NULL, (data + i), &bytes_read);
15648 i += bytes_read;
15649 stack[++stacki] = unsnd;
15650 if (i < size)
15651 dwarf2_complex_location_expr_complaint ();
15652 break;
c906108c 15653
21ae7a4d
JK
15654 case DW_OP_addr:
15655 stack[++stacki] = read_address (objfile->obfd, &data[i],
15656 cu, &bytes_read);
15657 i += bytes_read;
15658 break;
d53d4ac5 15659
21ae7a4d
JK
15660 case DW_OP_const1u:
15661 stack[++stacki] = read_1_byte (objfile->obfd, &data[i]);
15662 i += 1;
15663 break;
15664
15665 case DW_OP_const1s:
15666 stack[++stacki] = read_1_signed_byte (objfile->obfd, &data[i]);
15667 i += 1;
15668 break;
15669
15670 case DW_OP_const2u:
15671 stack[++stacki] = read_2_bytes (objfile->obfd, &data[i]);
15672 i += 2;
15673 break;
15674
15675 case DW_OP_const2s:
15676 stack[++stacki] = read_2_signed_bytes (objfile->obfd, &data[i]);
15677 i += 2;
15678 break;
d53d4ac5 15679
21ae7a4d
JK
15680 case DW_OP_const4u:
15681 stack[++stacki] = read_4_bytes (objfile->obfd, &data[i]);
15682 i += 4;
15683 break;
15684
15685 case DW_OP_const4s:
15686 stack[++stacki] = read_4_signed_bytes (objfile->obfd, &data[i]);
15687 i += 4;
15688 break;
15689
585861ea
JK
15690 case DW_OP_const8u:
15691 stack[++stacki] = read_8_bytes (objfile->obfd, &data[i]);
15692 i += 8;
15693 break;
15694
21ae7a4d
JK
15695 case DW_OP_constu:
15696 stack[++stacki] = read_unsigned_leb128 (NULL, (data + i),
15697 &bytes_read);
15698 i += bytes_read;
15699 break;
15700
15701 case DW_OP_consts:
15702 stack[++stacki] = read_signed_leb128 (NULL, (data + i), &bytes_read);
15703 i += bytes_read;
15704 break;
15705
15706 case DW_OP_dup:
15707 stack[stacki + 1] = stack[stacki];
15708 stacki++;
15709 break;
15710
15711 case DW_OP_plus:
15712 stack[stacki - 1] += stack[stacki];
15713 stacki--;
15714 break;
15715
15716 case DW_OP_plus_uconst:
15717 stack[stacki] += read_unsigned_leb128 (NULL, (data + i),
15718 &bytes_read);
15719 i += bytes_read;
15720 break;
15721
15722 case DW_OP_minus:
15723 stack[stacki - 1] -= stack[stacki];
15724 stacki--;
15725 break;
15726
15727 case DW_OP_deref:
15728 /* If we're not the last op, then we definitely can't encode
15729 this using GDB's address_class enum. This is valid for partial
15730 global symbols, although the variable's address will be bogus
15731 in the psymtab. */
15732 if (i < size)
15733 dwarf2_complex_location_expr_complaint ();
15734 break;
15735
15736 case DW_OP_GNU_push_tls_address:
15737 /* The top of the stack has the offset from the beginning
15738 of the thread control block at which the variable is located. */
15739 /* Nothing should follow this operator, so the top of stack would
15740 be returned. */
15741 /* This is valid for partial global symbols, but the variable's
585861ea
JK
15742 address will be bogus in the psymtab. Make it always at least
15743 non-zero to not look as a variable garbage collected by linker
15744 which have DW_OP_addr 0. */
21ae7a4d
JK
15745 if (i < size)
15746 dwarf2_complex_location_expr_complaint ();
585861ea 15747 stack[stacki]++;
21ae7a4d
JK
15748 break;
15749
15750 case DW_OP_GNU_uninit:
15751 break;
15752
3019eac3 15753 case DW_OP_GNU_addr_index:
49f6c839 15754 case DW_OP_GNU_const_index:
3019eac3
DE
15755 stack[++stacki] = read_addr_index_from_leb128 (cu, &data[i],
15756 &bytes_read);
15757 i += bytes_read;
15758 break;
15759
21ae7a4d
JK
15760 default:
15761 {
f39c6ffd 15762 const char *name = get_DW_OP_name (op);
21ae7a4d
JK
15763
15764 if (name)
15765 complaint (&symfile_complaints, _("unsupported stack op: '%s'"),
15766 name);
15767 else
15768 complaint (&symfile_complaints, _("unsupported stack op: '%02x'"),
15769 op);
15770 }
15771
15772 return (stack[stacki]);
d53d4ac5 15773 }
3c6e0cb3 15774
21ae7a4d
JK
15775 /* Enforce maximum stack depth of SIZE-1 to avoid writing
15776 outside of the allocated space. Also enforce minimum>0. */
15777 if (stacki >= ARRAY_SIZE (stack) - 1)
15778 {
15779 complaint (&symfile_complaints,
15780 _("location description stack overflow"));
15781 return 0;
15782 }
15783
15784 if (stacki <= 0)
15785 {
15786 complaint (&symfile_complaints,
15787 _("location description stack underflow"));
15788 return 0;
15789 }
15790 }
15791 return (stack[stacki]);
c906108c
SS
15792}
15793
15794/* memory allocation interface */
15795
c906108c 15796static struct dwarf_block *
7b5a2f43 15797dwarf_alloc_block (struct dwarf2_cu *cu)
c906108c
SS
15798{
15799 struct dwarf_block *blk;
15800
15801 blk = (struct dwarf_block *)
7b5a2f43 15802 obstack_alloc (&cu->comp_unit_obstack, sizeof (struct dwarf_block));
c906108c
SS
15803 return (blk);
15804}
15805
c906108c 15806static struct die_info *
b60c80d6 15807dwarf_alloc_die (struct dwarf2_cu *cu, int num_attrs)
c906108c
SS
15808{
15809 struct die_info *die;
b60c80d6
DJ
15810 size_t size = sizeof (struct die_info);
15811
15812 if (num_attrs > 1)
15813 size += (num_attrs - 1) * sizeof (struct attribute);
c906108c 15814
b60c80d6 15815 die = (struct die_info *) obstack_alloc (&cu->comp_unit_obstack, size);
c906108c
SS
15816 memset (die, 0, sizeof (struct die_info));
15817 return (die);
15818}
2e276125
JB
15819
15820\f
15821/* Macro support. */
15822
2e276125
JB
15823/* Return the full name of file number I in *LH's file name table.
15824 Use COMP_DIR as the name of the current directory of the
15825 compilation. The result is allocated using xmalloc; the caller is
15826 responsible for freeing it. */
15827static char *
15828file_full_name (int file, struct line_header *lh, const char *comp_dir)
15829{
6a83a1e6
EZ
15830 /* Is the file number a valid index into the line header's file name
15831 table? Remember that file numbers start with one, not zero. */
15832 if (1 <= file && file <= lh->num_file_names)
15833 {
15834 struct file_entry *fe = &lh->file_names[file - 1];
6e70227d 15835
6a83a1e6
EZ
15836 if (IS_ABSOLUTE_PATH (fe->name))
15837 return xstrdup (fe->name);
15838 else
15839 {
15840 const char *dir;
15841 int dir_len;
15842 char *full_name;
15843
15844 if (fe->dir_index)
15845 dir = lh->include_dirs[fe->dir_index - 1];
15846 else
15847 dir = comp_dir;
15848
15849 if (dir)
15850 {
15851 dir_len = strlen (dir);
15852 full_name = xmalloc (dir_len + 1 + strlen (fe->name) + 1);
15853 strcpy (full_name, dir);
15854 full_name[dir_len] = '/';
15855 strcpy (full_name + dir_len + 1, fe->name);
15856 return full_name;
15857 }
15858 else
15859 return xstrdup (fe->name);
15860 }
15861 }
2e276125
JB
15862 else
15863 {
6a83a1e6
EZ
15864 /* The compiler produced a bogus file number. We can at least
15865 record the macro definitions made in the file, even if we
15866 won't be able to find the file by name. */
15867 char fake_name[80];
9a619af0 15868
6a83a1e6 15869 sprintf (fake_name, "<bad macro file number %d>", file);
2e276125 15870
6e70227d 15871 complaint (&symfile_complaints,
6a83a1e6
EZ
15872 _("bad file number in macro information (%d)"),
15873 file);
2e276125 15874
6a83a1e6 15875 return xstrdup (fake_name);
2e276125
JB
15876 }
15877}
15878
15879
15880static struct macro_source_file *
15881macro_start_file (int file, int line,
15882 struct macro_source_file *current_file,
15883 const char *comp_dir,
15884 struct line_header *lh, struct objfile *objfile)
15885{
15886 /* The full name of this source file. */
15887 char *full_name = file_full_name (file, lh, comp_dir);
15888
15889 /* We don't create a macro table for this compilation unit
15890 at all until we actually get a filename. */
15891 if (! pending_macros)
4a146b47 15892 pending_macros = new_macro_table (&objfile->objfile_obstack,
af5f3db6 15893 objfile->macro_cache);
2e276125
JB
15894
15895 if (! current_file)
abc9d0dc
TT
15896 {
15897 /* If we have no current file, then this must be the start_file
15898 directive for the compilation unit's main source file. */
15899 current_file = macro_set_main (pending_macros, full_name);
15900 macro_define_special (pending_macros);
15901 }
2e276125
JB
15902 else
15903 current_file = macro_include (current_file, line, full_name);
15904
15905 xfree (full_name);
6e70227d 15906
2e276125
JB
15907 return current_file;
15908}
15909
15910
15911/* Copy the LEN characters at BUF to a xmalloc'ed block of memory,
15912 followed by a null byte. */
15913static char *
15914copy_string (const char *buf, int len)
15915{
15916 char *s = xmalloc (len + 1);
9a619af0 15917
2e276125
JB
15918 memcpy (s, buf, len);
15919 s[len] = '\0';
2e276125
JB
15920 return s;
15921}
15922
15923
15924static const char *
15925consume_improper_spaces (const char *p, const char *body)
15926{
15927 if (*p == ' ')
15928 {
4d3c2250 15929 complaint (&symfile_complaints,
3e43a32a
MS
15930 _("macro definition contains spaces "
15931 "in formal argument list:\n`%s'"),
4d3c2250 15932 body);
2e276125
JB
15933
15934 while (*p == ' ')
15935 p++;
15936 }
15937
15938 return p;
15939}
15940
15941
15942static void
15943parse_macro_definition (struct macro_source_file *file, int line,
15944 const char *body)
15945{
15946 const char *p;
15947
15948 /* The body string takes one of two forms. For object-like macro
15949 definitions, it should be:
15950
15951 <macro name> " " <definition>
15952
15953 For function-like macro definitions, it should be:
15954
15955 <macro name> "() " <definition>
15956 or
15957 <macro name> "(" <arg name> ( "," <arg name> ) * ") " <definition>
15958
15959 Spaces may appear only where explicitly indicated, and in the
15960 <definition>.
15961
15962 The Dwarf 2 spec says that an object-like macro's name is always
15963 followed by a space, but versions of GCC around March 2002 omit
6e70227d 15964 the space when the macro's definition is the empty string.
2e276125
JB
15965
15966 The Dwarf 2 spec says that there should be no spaces between the
15967 formal arguments in a function-like macro's formal argument list,
15968 but versions of GCC around March 2002 include spaces after the
15969 commas. */
15970
15971
15972 /* Find the extent of the macro name. The macro name is terminated
15973 by either a space or null character (for an object-like macro) or
15974 an opening paren (for a function-like macro). */
15975 for (p = body; *p; p++)
15976 if (*p == ' ' || *p == '(')
15977 break;
15978
15979 if (*p == ' ' || *p == '\0')
15980 {
15981 /* It's an object-like macro. */
15982 int name_len = p - body;
15983 char *name = copy_string (body, name_len);
15984 const char *replacement;
15985
15986 if (*p == ' ')
15987 replacement = body + name_len + 1;
15988 else
15989 {
4d3c2250 15990 dwarf2_macro_malformed_definition_complaint (body);
2e276125
JB
15991 replacement = body + name_len;
15992 }
6e70227d 15993
2e276125
JB
15994 macro_define_object (file, line, name, replacement);
15995
15996 xfree (name);
15997 }
15998 else if (*p == '(')
15999 {
16000 /* It's a function-like macro. */
16001 char *name = copy_string (body, p - body);
16002 int argc = 0;
16003 int argv_size = 1;
16004 char **argv = xmalloc (argv_size * sizeof (*argv));
16005
16006 p++;
16007
16008 p = consume_improper_spaces (p, body);
16009
16010 /* Parse the formal argument list. */
16011 while (*p && *p != ')')
16012 {
16013 /* Find the extent of the current argument name. */
16014 const char *arg_start = p;
16015
16016 while (*p && *p != ',' && *p != ')' && *p != ' ')
16017 p++;
16018
16019 if (! *p || p == arg_start)
4d3c2250 16020 dwarf2_macro_malformed_definition_complaint (body);
2e276125
JB
16021 else
16022 {
16023 /* Make sure argv has room for the new argument. */
16024 if (argc >= argv_size)
16025 {
16026 argv_size *= 2;
16027 argv = xrealloc (argv, argv_size * sizeof (*argv));
16028 }
16029
16030 argv[argc++] = copy_string (arg_start, p - arg_start);
16031 }
16032
16033 p = consume_improper_spaces (p, body);
16034
16035 /* Consume the comma, if present. */
16036 if (*p == ',')
16037 {
16038 p++;
16039
16040 p = consume_improper_spaces (p, body);
16041 }
16042 }
16043
16044 if (*p == ')')
16045 {
16046 p++;
16047
16048 if (*p == ' ')
16049 /* Perfectly formed definition, no complaints. */
16050 macro_define_function (file, line, name,
6e70227d 16051 argc, (const char **) argv,
2e276125
JB
16052 p + 1);
16053 else if (*p == '\0')
16054 {
16055 /* Complain, but do define it. */
4d3c2250 16056 dwarf2_macro_malformed_definition_complaint (body);
2e276125 16057 macro_define_function (file, line, name,
6e70227d 16058 argc, (const char **) argv,
2e276125
JB
16059 p);
16060 }
16061 else
16062 /* Just complain. */
4d3c2250 16063 dwarf2_macro_malformed_definition_complaint (body);
2e276125
JB
16064 }
16065 else
16066 /* Just complain. */
4d3c2250 16067 dwarf2_macro_malformed_definition_complaint (body);
2e276125
JB
16068
16069 xfree (name);
16070 {
16071 int i;
16072
16073 for (i = 0; i < argc; i++)
16074 xfree (argv[i]);
16075 }
16076 xfree (argv);
16077 }
16078 else
4d3c2250 16079 dwarf2_macro_malformed_definition_complaint (body);
2e276125
JB
16080}
16081
cf2c3c16
TT
16082/* Skip some bytes from BYTES according to the form given in FORM.
16083 Returns the new pointer. */
2e276125 16084
cf2c3c16 16085static gdb_byte *
f664829e 16086skip_form_bytes (bfd *abfd, gdb_byte *bytes, gdb_byte *buffer_end,
cf2c3c16
TT
16087 enum dwarf_form form,
16088 unsigned int offset_size,
16089 struct dwarf2_section_info *section)
2e276125 16090{
cf2c3c16 16091 unsigned int bytes_read;
2e276125 16092
cf2c3c16 16093 switch (form)
2e276125 16094 {
cf2c3c16
TT
16095 case DW_FORM_data1:
16096 case DW_FORM_flag:
16097 ++bytes;
16098 break;
16099
16100 case DW_FORM_data2:
16101 bytes += 2;
16102 break;
16103
16104 case DW_FORM_data4:
16105 bytes += 4;
16106 break;
16107
16108 case DW_FORM_data8:
16109 bytes += 8;
16110 break;
16111
16112 case DW_FORM_string:
16113 read_direct_string (abfd, bytes, &bytes_read);
16114 bytes += bytes_read;
16115 break;
16116
16117 case DW_FORM_sec_offset:
16118 case DW_FORM_strp:
16119 bytes += offset_size;
16120 break;
16121
16122 case DW_FORM_block:
16123 bytes += read_unsigned_leb128 (abfd, bytes, &bytes_read);
16124 bytes += bytes_read;
16125 break;
16126
16127 case DW_FORM_block1:
16128 bytes += 1 + read_1_byte (abfd, bytes);
16129 break;
16130 case DW_FORM_block2:
16131 bytes += 2 + read_2_bytes (abfd, bytes);
16132 break;
16133 case DW_FORM_block4:
16134 bytes += 4 + read_4_bytes (abfd, bytes);
16135 break;
16136
16137 case DW_FORM_sdata:
16138 case DW_FORM_udata:
3019eac3
DE
16139 case DW_FORM_GNU_addr_index:
16140 case DW_FORM_GNU_str_index:
f664829e
DE
16141 bytes = (gdb_byte *) gdb_skip_leb128 (bytes, buffer_end);
16142 if (bytes == NULL)
16143 {
16144 dwarf2_section_buffer_overflow_complaint (section);
16145 return NULL;
16146 }
cf2c3c16
TT
16147 break;
16148
16149 default:
16150 {
16151 complain:
16152 complaint (&symfile_complaints,
16153 _("invalid form 0x%x in `%s'"),
16154 form,
16155 section->asection->name);
16156 return NULL;
16157 }
2e276125
JB
16158 }
16159
cf2c3c16
TT
16160 return bytes;
16161}
757a13d0 16162
cf2c3c16
TT
16163/* A helper for dwarf_decode_macros that handles skipping an unknown
16164 opcode. Returns an updated pointer to the macro data buffer; or,
16165 on error, issues a complaint and returns NULL. */
757a13d0 16166
cf2c3c16
TT
16167static gdb_byte *
16168skip_unknown_opcode (unsigned int opcode,
16169 gdb_byte **opcode_definitions,
f664829e 16170 gdb_byte *mac_ptr, gdb_byte *mac_end,
cf2c3c16
TT
16171 bfd *abfd,
16172 unsigned int offset_size,
16173 struct dwarf2_section_info *section)
16174{
16175 unsigned int bytes_read, i;
16176 unsigned long arg;
16177 gdb_byte *defn;
2e276125 16178
cf2c3c16 16179 if (opcode_definitions[opcode] == NULL)
2e276125 16180 {
cf2c3c16
TT
16181 complaint (&symfile_complaints,
16182 _("unrecognized DW_MACFINO opcode 0x%x"),
16183 opcode);
16184 return NULL;
16185 }
2e276125 16186
cf2c3c16
TT
16187 defn = opcode_definitions[opcode];
16188 arg = read_unsigned_leb128 (abfd, defn, &bytes_read);
16189 defn += bytes_read;
2e276125 16190
cf2c3c16
TT
16191 for (i = 0; i < arg; ++i)
16192 {
f664829e
DE
16193 mac_ptr = skip_form_bytes (abfd, mac_ptr, mac_end, defn[i], offset_size,
16194 section);
cf2c3c16
TT
16195 if (mac_ptr == NULL)
16196 {
16197 /* skip_form_bytes already issued the complaint. */
16198 return NULL;
16199 }
16200 }
757a13d0 16201
cf2c3c16
TT
16202 return mac_ptr;
16203}
757a13d0 16204
cf2c3c16
TT
16205/* A helper function which parses the header of a macro section.
16206 If the macro section is the extended (for now called "GNU") type,
16207 then this updates *OFFSET_SIZE. Returns a pointer to just after
16208 the header, or issues a complaint and returns NULL on error. */
757a13d0 16209
cf2c3c16
TT
16210static gdb_byte *
16211dwarf_parse_macro_header (gdb_byte **opcode_definitions,
16212 bfd *abfd,
16213 gdb_byte *mac_ptr,
16214 unsigned int *offset_size,
16215 int section_is_gnu)
16216{
16217 memset (opcode_definitions, 0, 256 * sizeof (gdb_byte *));
757a13d0 16218
cf2c3c16
TT
16219 if (section_is_gnu)
16220 {
16221 unsigned int version, flags;
757a13d0 16222
cf2c3c16
TT
16223 version = read_2_bytes (abfd, mac_ptr);
16224 if (version != 4)
16225 {
16226 complaint (&symfile_complaints,
16227 _("unrecognized version `%d' in .debug_macro section"),
16228 version);
16229 return NULL;
16230 }
16231 mac_ptr += 2;
757a13d0 16232
cf2c3c16
TT
16233 flags = read_1_byte (abfd, mac_ptr);
16234 ++mac_ptr;
16235 *offset_size = (flags & 1) ? 8 : 4;
757a13d0 16236
cf2c3c16
TT
16237 if ((flags & 2) != 0)
16238 /* We don't need the line table offset. */
16239 mac_ptr += *offset_size;
757a13d0 16240
cf2c3c16
TT
16241 /* Vendor opcode descriptions. */
16242 if ((flags & 4) != 0)
16243 {
16244 unsigned int i, count;
757a13d0 16245
cf2c3c16
TT
16246 count = read_1_byte (abfd, mac_ptr);
16247 ++mac_ptr;
16248 for (i = 0; i < count; ++i)
16249 {
16250 unsigned int opcode, bytes_read;
16251 unsigned long arg;
16252
16253 opcode = read_1_byte (abfd, mac_ptr);
16254 ++mac_ptr;
16255 opcode_definitions[opcode] = mac_ptr;
16256 arg = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
16257 mac_ptr += bytes_read;
16258 mac_ptr += arg;
16259 }
757a13d0 16260 }
cf2c3c16 16261 }
757a13d0 16262
cf2c3c16
TT
16263 return mac_ptr;
16264}
757a13d0 16265
cf2c3c16 16266/* A helper for dwarf_decode_macros that handles the GNU extensions,
8fc3fc34 16267 including DW_MACRO_GNU_transparent_include. */
cf2c3c16
TT
16268
16269static void
16270dwarf_decode_macro_bytes (bfd *abfd, gdb_byte *mac_ptr, gdb_byte *mac_end,
16271 struct macro_source_file *current_file,
16272 struct line_header *lh, char *comp_dir,
16273 struct dwarf2_section_info *section,
16274 int section_is_gnu,
16275 unsigned int offset_size,
8fc3fc34
TT
16276 struct objfile *objfile,
16277 htab_t include_hash)
cf2c3c16
TT
16278{
16279 enum dwarf_macro_record_type macinfo_type;
16280 int at_commandline;
16281 gdb_byte *opcode_definitions[256];
757a13d0 16282
cf2c3c16
TT
16283 mac_ptr = dwarf_parse_macro_header (opcode_definitions, abfd, mac_ptr,
16284 &offset_size, section_is_gnu);
16285 if (mac_ptr == NULL)
16286 {
16287 /* We already issued a complaint. */
16288 return;
16289 }
757a13d0
JK
16290
16291 /* Determines if GDB is still before first DW_MACINFO_start_file. If true
16292 GDB is still reading the definitions from command line. First
16293 DW_MACINFO_start_file will need to be ignored as it was already executed
16294 to create CURRENT_FILE for the main source holding also the command line
16295 definitions. On first met DW_MACINFO_start_file this flag is reset to
16296 normally execute all the remaining DW_MACINFO_start_file macinfos. */
16297
16298 at_commandline = 1;
16299
16300 do
16301 {
16302 /* Do we at least have room for a macinfo type byte? */
16303 if (mac_ptr >= mac_end)
16304 {
f664829e 16305 dwarf2_section_buffer_overflow_complaint (section);
757a13d0
JK
16306 break;
16307 }
16308
16309 macinfo_type = read_1_byte (abfd, mac_ptr);
16310 mac_ptr++;
16311
cf2c3c16
TT
16312 /* Note that we rely on the fact that the corresponding GNU and
16313 DWARF constants are the same. */
757a13d0
JK
16314 switch (macinfo_type)
16315 {
16316 /* A zero macinfo type indicates the end of the macro
16317 information. */
16318 case 0:
16319 break;
2e276125 16320
cf2c3c16
TT
16321 case DW_MACRO_GNU_define:
16322 case DW_MACRO_GNU_undef:
16323 case DW_MACRO_GNU_define_indirect:
16324 case DW_MACRO_GNU_undef_indirect:
2e276125 16325 {
891d2f0b 16326 unsigned int bytes_read;
2e276125
JB
16327 int line;
16328 char *body;
cf2c3c16 16329 int is_define;
2e276125 16330
cf2c3c16
TT
16331 line = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
16332 mac_ptr += bytes_read;
16333
16334 if (macinfo_type == DW_MACRO_GNU_define
16335 || macinfo_type == DW_MACRO_GNU_undef)
16336 {
16337 body = read_direct_string (abfd, mac_ptr, &bytes_read);
16338 mac_ptr += bytes_read;
16339 }
16340 else
16341 {
16342 LONGEST str_offset;
16343
16344 str_offset = read_offset_1 (abfd, mac_ptr, offset_size);
16345 mac_ptr += offset_size;
2e276125 16346
cf2c3c16
TT
16347 body = read_indirect_string_at_offset (abfd, str_offset);
16348 }
16349
16350 is_define = (macinfo_type == DW_MACRO_GNU_define
16351 || macinfo_type == DW_MACRO_GNU_define_indirect);
2e276125 16352 if (! current_file)
757a13d0
JK
16353 {
16354 /* DWARF violation as no main source is present. */
16355 complaint (&symfile_complaints,
16356 _("debug info with no main source gives macro %s "
16357 "on line %d: %s"),
cf2c3c16
TT
16358 is_define ? _("definition") : _("undefinition"),
16359 line, body);
757a13d0
JK
16360 break;
16361 }
3e43a32a
MS
16362 if ((line == 0 && !at_commandline)
16363 || (line != 0 && at_commandline))
4d3c2250 16364 complaint (&symfile_complaints,
757a13d0
JK
16365 _("debug info gives %s macro %s with %s line %d: %s"),
16366 at_commandline ? _("command-line") : _("in-file"),
cf2c3c16 16367 is_define ? _("definition") : _("undefinition"),
757a13d0
JK
16368 line == 0 ? _("zero") : _("non-zero"), line, body);
16369
cf2c3c16 16370 if (is_define)
757a13d0 16371 parse_macro_definition (current_file, line, body);
cf2c3c16
TT
16372 else
16373 {
16374 gdb_assert (macinfo_type == DW_MACRO_GNU_undef
16375 || macinfo_type == DW_MACRO_GNU_undef_indirect);
16376 macro_undef (current_file, line, body);
16377 }
2e276125
JB
16378 }
16379 break;
16380
cf2c3c16 16381 case DW_MACRO_GNU_start_file:
2e276125 16382 {
891d2f0b 16383 unsigned int bytes_read;
2e276125
JB
16384 int line, file;
16385
16386 line = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
16387 mac_ptr += bytes_read;
16388 file = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
16389 mac_ptr += bytes_read;
16390
3e43a32a
MS
16391 if ((line == 0 && !at_commandline)
16392 || (line != 0 && at_commandline))
757a13d0
JK
16393 complaint (&symfile_complaints,
16394 _("debug info gives source %d included "
16395 "from %s at %s line %d"),
16396 file, at_commandline ? _("command-line") : _("file"),
16397 line == 0 ? _("zero") : _("non-zero"), line);
16398
16399 if (at_commandline)
16400 {
cf2c3c16
TT
16401 /* This DW_MACRO_GNU_start_file was executed in the
16402 pass one. */
757a13d0
JK
16403 at_commandline = 0;
16404 }
16405 else
16406 current_file = macro_start_file (file, line,
16407 current_file, comp_dir,
cf2c3c16 16408 lh, objfile);
2e276125
JB
16409 }
16410 break;
16411
cf2c3c16 16412 case DW_MACRO_GNU_end_file:
2e276125 16413 if (! current_file)
4d3c2250 16414 complaint (&symfile_complaints,
3e43a32a
MS
16415 _("macro debug info has an unmatched "
16416 "`close_file' directive"));
2e276125
JB
16417 else
16418 {
16419 current_file = current_file->included_by;
16420 if (! current_file)
16421 {
cf2c3c16 16422 enum dwarf_macro_record_type next_type;
2e276125
JB
16423
16424 /* GCC circa March 2002 doesn't produce the zero
16425 type byte marking the end of the compilation
16426 unit. Complain if it's not there, but exit no
16427 matter what. */
16428
16429 /* Do we at least have room for a macinfo type byte? */
16430 if (mac_ptr >= mac_end)
16431 {
f664829e 16432 dwarf2_section_buffer_overflow_complaint (section);
2e276125
JB
16433 return;
16434 }
16435
16436 /* We don't increment mac_ptr here, so this is just
16437 a look-ahead. */
16438 next_type = read_1_byte (abfd, mac_ptr);
16439 if (next_type != 0)
4d3c2250 16440 complaint (&symfile_complaints,
3e43a32a
MS
16441 _("no terminating 0-type entry for "
16442 "macros in `.debug_macinfo' section"));
2e276125
JB
16443
16444 return;
16445 }
16446 }
16447 break;
16448
cf2c3c16
TT
16449 case DW_MACRO_GNU_transparent_include:
16450 {
16451 LONGEST offset;
8fc3fc34 16452 void **slot;
cf2c3c16
TT
16453
16454 offset = read_offset_1 (abfd, mac_ptr, offset_size);
16455 mac_ptr += offset_size;
16456
8fc3fc34
TT
16457 slot = htab_find_slot (include_hash, mac_ptr, INSERT);
16458 if (*slot != NULL)
16459 {
16460 /* This has actually happened; see
16461 http://sourceware.org/bugzilla/show_bug.cgi?id=13568. */
16462 complaint (&symfile_complaints,
16463 _("recursive DW_MACRO_GNU_transparent_include in "
16464 ".debug_macro section"));
16465 }
16466 else
16467 {
16468 *slot = mac_ptr;
16469
16470 dwarf_decode_macro_bytes (abfd,
16471 section->buffer + offset,
16472 mac_end, current_file,
16473 lh, comp_dir,
16474 section, section_is_gnu,
16475 offset_size, objfile, include_hash);
16476
16477 htab_remove_elt (include_hash, mac_ptr);
16478 }
cf2c3c16
TT
16479 }
16480 break;
16481
2e276125 16482 case DW_MACINFO_vendor_ext:
cf2c3c16
TT
16483 if (!section_is_gnu)
16484 {
16485 unsigned int bytes_read;
16486 int constant;
2e276125 16487
cf2c3c16
TT
16488 constant = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
16489 mac_ptr += bytes_read;
16490 read_direct_string (abfd, mac_ptr, &bytes_read);
16491 mac_ptr += bytes_read;
2e276125 16492
cf2c3c16
TT
16493 /* We don't recognize any vendor extensions. */
16494 break;
16495 }
16496 /* FALLTHROUGH */
16497
16498 default:
16499 mac_ptr = skip_unknown_opcode (macinfo_type, opcode_definitions,
f664829e 16500 mac_ptr, mac_end, abfd, offset_size,
cf2c3c16
TT
16501 section);
16502 if (mac_ptr == NULL)
16503 return;
16504 break;
2e276125 16505 }
757a13d0 16506 } while (macinfo_type != 0);
2e276125 16507}
8e19ed76 16508
cf2c3c16 16509static void
09262596
DE
16510dwarf_decode_macros (struct dwarf2_cu *cu, unsigned int offset,
16511 char *comp_dir, int section_is_gnu)
cf2c3c16 16512{
bb5ed363 16513 struct objfile *objfile = dwarf2_per_objfile->objfile;
09262596
DE
16514 struct line_header *lh = cu->line_header;
16515 bfd *abfd;
cf2c3c16
TT
16516 gdb_byte *mac_ptr, *mac_end;
16517 struct macro_source_file *current_file = 0;
16518 enum dwarf_macro_record_type macinfo_type;
16519 unsigned int offset_size = cu->header.offset_size;
16520 gdb_byte *opcode_definitions[256];
8fc3fc34
TT
16521 struct cleanup *cleanup;
16522 htab_t include_hash;
16523 void **slot;
09262596
DE
16524 struct dwarf2_section_info *section;
16525 const char *section_name;
16526
16527 if (cu->dwo_unit != NULL)
16528 {
16529 if (section_is_gnu)
16530 {
16531 section = &cu->dwo_unit->dwo_file->sections.macro;
16532 section_name = ".debug_macro.dwo";
16533 }
16534 else
16535 {
16536 section = &cu->dwo_unit->dwo_file->sections.macinfo;
16537 section_name = ".debug_macinfo.dwo";
16538 }
16539 }
16540 else
16541 {
16542 if (section_is_gnu)
16543 {
16544 section = &dwarf2_per_objfile->macro;
16545 section_name = ".debug_macro";
16546 }
16547 else
16548 {
16549 section = &dwarf2_per_objfile->macinfo;
16550 section_name = ".debug_macinfo";
16551 }
16552 }
cf2c3c16 16553
bb5ed363 16554 dwarf2_read_section (objfile, section);
cf2c3c16
TT
16555 if (section->buffer == NULL)
16556 {
fceca515 16557 complaint (&symfile_complaints, _("missing %s section"), section_name);
cf2c3c16
TT
16558 return;
16559 }
09262596 16560 abfd = section->asection->owner;
cf2c3c16
TT
16561
16562 /* First pass: Find the name of the base filename.
16563 This filename is needed in order to process all macros whose definition
16564 (or undefinition) comes from the command line. These macros are defined
16565 before the first DW_MACINFO_start_file entry, and yet still need to be
16566 associated to the base file.
16567
16568 To determine the base file name, we scan the macro definitions until we
16569 reach the first DW_MACINFO_start_file entry. We then initialize
16570 CURRENT_FILE accordingly so that any macro definition found before the
16571 first DW_MACINFO_start_file can still be associated to the base file. */
16572
16573 mac_ptr = section->buffer + offset;
16574 mac_end = section->buffer + section->size;
16575
16576 mac_ptr = dwarf_parse_macro_header (opcode_definitions, abfd, mac_ptr,
16577 &offset_size, section_is_gnu);
16578 if (mac_ptr == NULL)
16579 {
16580 /* We already issued a complaint. */
16581 return;
16582 }
16583
16584 do
16585 {
16586 /* Do we at least have room for a macinfo type byte? */
16587 if (mac_ptr >= mac_end)
16588 {
16589 /* Complaint is printed during the second pass as GDB will probably
16590 stop the first pass earlier upon finding
16591 DW_MACINFO_start_file. */
16592 break;
16593 }
16594
16595 macinfo_type = read_1_byte (abfd, mac_ptr);
16596 mac_ptr++;
16597
16598 /* Note that we rely on the fact that the corresponding GNU and
16599 DWARF constants are the same. */
16600 switch (macinfo_type)
16601 {
16602 /* A zero macinfo type indicates the end of the macro
16603 information. */
16604 case 0:
16605 break;
16606
16607 case DW_MACRO_GNU_define:
16608 case DW_MACRO_GNU_undef:
16609 /* Only skip the data by MAC_PTR. */
16610 {
16611 unsigned int bytes_read;
16612
16613 read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
16614 mac_ptr += bytes_read;
16615 read_direct_string (abfd, mac_ptr, &bytes_read);
16616 mac_ptr += bytes_read;
16617 }
16618 break;
16619
16620 case DW_MACRO_GNU_start_file:
16621 {
16622 unsigned int bytes_read;
16623 int line, file;
16624
16625 line = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
16626 mac_ptr += bytes_read;
16627 file = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
16628 mac_ptr += bytes_read;
16629
16630 current_file = macro_start_file (file, line, current_file,
bb5ed363 16631 comp_dir, lh, objfile);
cf2c3c16
TT
16632 }
16633 break;
16634
16635 case DW_MACRO_GNU_end_file:
16636 /* No data to skip by MAC_PTR. */
16637 break;
16638
16639 case DW_MACRO_GNU_define_indirect:
16640 case DW_MACRO_GNU_undef_indirect:
16641 {
16642 unsigned int bytes_read;
16643
16644 read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
16645 mac_ptr += bytes_read;
16646 mac_ptr += offset_size;
16647 }
16648 break;
16649
16650 case DW_MACRO_GNU_transparent_include:
16651 /* Note that, according to the spec, a transparent include
16652 chain cannot call DW_MACRO_GNU_start_file. So, we can just
16653 skip this opcode. */
16654 mac_ptr += offset_size;
16655 break;
16656
16657 case DW_MACINFO_vendor_ext:
16658 /* Only skip the data by MAC_PTR. */
16659 if (!section_is_gnu)
16660 {
16661 unsigned int bytes_read;
16662
16663 read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
16664 mac_ptr += bytes_read;
16665 read_direct_string (abfd, mac_ptr, &bytes_read);
16666 mac_ptr += bytes_read;
16667 }
16668 /* FALLTHROUGH */
16669
16670 default:
16671 mac_ptr = skip_unknown_opcode (macinfo_type, opcode_definitions,
f664829e 16672 mac_ptr, mac_end, abfd, offset_size,
cf2c3c16
TT
16673 section);
16674 if (mac_ptr == NULL)
16675 return;
16676 break;
16677 }
16678 } while (macinfo_type != 0 && current_file == NULL);
16679
16680 /* Second pass: Process all entries.
16681
16682 Use the AT_COMMAND_LINE flag to determine whether we are still processing
16683 command-line macro definitions/undefinitions. This flag is unset when we
16684 reach the first DW_MACINFO_start_file entry. */
16685
8fc3fc34
TT
16686 include_hash = htab_create_alloc (1, htab_hash_pointer, htab_eq_pointer,
16687 NULL, xcalloc, xfree);
16688 cleanup = make_cleanup_htab_delete (include_hash);
16689 mac_ptr = section->buffer + offset;
16690 slot = htab_find_slot (include_hash, mac_ptr, INSERT);
16691 *slot = mac_ptr;
16692 dwarf_decode_macro_bytes (abfd, mac_ptr, mac_end,
cf2c3c16 16693 current_file, lh, comp_dir, section, section_is_gnu,
8fc3fc34
TT
16694 offset_size, objfile, include_hash);
16695 do_cleanups (cleanup);
cf2c3c16
TT
16696}
16697
8e19ed76 16698/* Check if the attribute's form is a DW_FORM_block*
0963b4bd 16699 if so return true else false. */
380bca97 16700
8e19ed76
PS
16701static int
16702attr_form_is_block (struct attribute *attr)
16703{
16704 return (attr == NULL ? 0 :
16705 attr->form == DW_FORM_block1
16706 || attr->form == DW_FORM_block2
16707 || attr->form == DW_FORM_block4
2dc7f7b3
TT
16708 || attr->form == DW_FORM_block
16709 || attr->form == DW_FORM_exprloc);
8e19ed76 16710}
4c2df51b 16711
c6a0999f
JB
16712/* Return non-zero if ATTR's value is a section offset --- classes
16713 lineptr, loclistptr, macptr or rangelistptr --- or zero, otherwise.
16714 You may use DW_UNSND (attr) to retrieve such offsets.
16715
16716 Section 7.5.4, "Attribute Encodings", explains that no attribute
16717 may have a value that belongs to more than one of these classes; it
16718 would be ambiguous if we did, because we use the same forms for all
16719 of them. */
380bca97 16720
3690dd37
JB
16721static int
16722attr_form_is_section_offset (struct attribute *attr)
16723{
16724 return (attr->form == DW_FORM_data4
2dc7f7b3
TT
16725 || attr->form == DW_FORM_data8
16726 || attr->form == DW_FORM_sec_offset);
3690dd37
JB
16727}
16728
3690dd37
JB
16729/* Return non-zero if ATTR's value falls in the 'constant' class, or
16730 zero otherwise. When this function returns true, you can apply
16731 dwarf2_get_attr_constant_value to it.
16732
16733 However, note that for some attributes you must check
16734 attr_form_is_section_offset before using this test. DW_FORM_data4
16735 and DW_FORM_data8 are members of both the constant class, and of
16736 the classes that contain offsets into other debug sections
16737 (lineptr, loclistptr, macptr or rangelistptr). The DWARF spec says
16738 that, if an attribute's can be either a constant or one of the
16739 section offset classes, DW_FORM_data4 and DW_FORM_data8 should be
16740 taken as section offsets, not constants. */
380bca97 16741
3690dd37
JB
16742static int
16743attr_form_is_constant (struct attribute *attr)
16744{
16745 switch (attr->form)
16746 {
16747 case DW_FORM_sdata:
16748 case DW_FORM_udata:
16749 case DW_FORM_data1:
16750 case DW_FORM_data2:
16751 case DW_FORM_data4:
16752 case DW_FORM_data8:
16753 return 1;
16754 default:
16755 return 0;
16756 }
16757}
16758
3019eac3
DE
16759/* Return the .debug_loc section to use for CU.
16760 For DWO files use .debug_loc.dwo. */
16761
16762static struct dwarf2_section_info *
16763cu_debug_loc_section (struct dwarf2_cu *cu)
16764{
16765 if (cu->dwo_unit)
16766 return &cu->dwo_unit->dwo_file->sections.loc;
16767 return &dwarf2_per_objfile->loc;
16768}
16769
8cf6f0b1
TT
16770/* A helper function that fills in a dwarf2_loclist_baton. */
16771
16772static void
16773fill_in_loclist_baton (struct dwarf2_cu *cu,
16774 struct dwarf2_loclist_baton *baton,
16775 struct attribute *attr)
16776{
3019eac3
DE
16777 struct dwarf2_section_info *section = cu_debug_loc_section (cu);
16778
16779 dwarf2_read_section (dwarf2_per_objfile->objfile, section);
8cf6f0b1
TT
16780
16781 baton->per_cu = cu->per_cu;
16782 gdb_assert (baton->per_cu);
16783 /* We don't know how long the location list is, but make sure we
16784 don't run off the edge of the section. */
3019eac3
DE
16785 baton->size = section->size - DW_UNSND (attr);
16786 baton->data = section->buffer + DW_UNSND (attr);
8cf6f0b1 16787 baton->base_address = cu->base_address;
f664829e 16788 baton->from_dwo = cu->dwo_unit != NULL;
8cf6f0b1
TT
16789}
16790
4c2df51b
DJ
16791static void
16792dwarf2_symbol_mark_computed (struct attribute *attr, struct symbol *sym,
e7c27a73 16793 struct dwarf2_cu *cu)
4c2df51b 16794{
bb5ed363 16795 struct objfile *objfile = dwarf2_per_objfile->objfile;
3019eac3 16796 struct dwarf2_section_info *section = cu_debug_loc_section (cu);
bb5ed363 16797
3690dd37 16798 if (attr_form_is_section_offset (attr)
3019eac3 16799 /* .debug_loc{,.dwo} may not exist at all, or the offset may be outside
99bcc461
DJ
16800 the section. If so, fall through to the complaint in the
16801 other branch. */
3019eac3 16802 && DW_UNSND (attr) < dwarf2_section_size (objfile, section))
4c2df51b 16803 {
0d53c4c4 16804 struct dwarf2_loclist_baton *baton;
4c2df51b 16805
bb5ed363 16806 baton = obstack_alloc (&objfile->objfile_obstack,
0d53c4c4 16807 sizeof (struct dwarf2_loclist_baton));
4c2df51b 16808
8cf6f0b1 16809 fill_in_loclist_baton (cu, baton, attr);
be391dca 16810
d00adf39 16811 if (cu->base_known == 0)
0d53c4c4 16812 complaint (&symfile_complaints,
3e43a32a
MS
16813 _("Location list used without "
16814 "specifying the CU base address."));
4c2df51b 16815
768a979c 16816 SYMBOL_COMPUTED_OPS (sym) = &dwarf2_loclist_funcs;
0d53c4c4
DJ
16817 SYMBOL_LOCATION_BATON (sym) = baton;
16818 }
16819 else
16820 {
16821 struct dwarf2_locexpr_baton *baton;
16822
bb5ed363 16823 baton = obstack_alloc (&objfile->objfile_obstack,
0d53c4c4 16824 sizeof (struct dwarf2_locexpr_baton));
ae0d2f24
UW
16825 baton->per_cu = cu->per_cu;
16826 gdb_assert (baton->per_cu);
0d53c4c4
DJ
16827
16828 if (attr_form_is_block (attr))
16829 {
16830 /* Note that we're just copying the block's data pointer
16831 here, not the actual data. We're still pointing into the
6502dd73
DJ
16832 info_buffer for SYM's objfile; right now we never release
16833 that buffer, but when we do clean up properly this may
16834 need to change. */
0d53c4c4
DJ
16835 baton->size = DW_BLOCK (attr)->size;
16836 baton->data = DW_BLOCK (attr)->data;
16837 }
16838 else
16839 {
16840 dwarf2_invalid_attrib_class_complaint ("location description",
16841 SYMBOL_NATURAL_NAME (sym));
16842 baton->size = 0;
0d53c4c4 16843 }
6e70227d 16844
768a979c 16845 SYMBOL_COMPUTED_OPS (sym) = &dwarf2_locexpr_funcs;
0d53c4c4
DJ
16846 SYMBOL_LOCATION_BATON (sym) = baton;
16847 }
4c2df51b 16848}
6502dd73 16849
9aa1f1e3
TT
16850/* Return the OBJFILE associated with the compilation unit CU. If CU
16851 came from a separate debuginfo file, then the master objfile is
16852 returned. */
ae0d2f24
UW
16853
16854struct objfile *
16855dwarf2_per_cu_objfile (struct dwarf2_per_cu_data *per_cu)
16856{
9291a0cd 16857 struct objfile *objfile = per_cu->objfile;
ae0d2f24
UW
16858
16859 /* Return the master objfile, so that we can report and look up the
16860 correct file containing this variable. */
16861 if (objfile->separate_debug_objfile_backlink)
16862 objfile = objfile->separate_debug_objfile_backlink;
16863
16864 return objfile;
16865}
16866
96408a79
SA
16867/* Return comp_unit_head for PER_CU, either already available in PER_CU->CU
16868 (CU_HEADERP is unused in such case) or prepare a temporary copy at
16869 CU_HEADERP first. */
16870
16871static const struct comp_unit_head *
16872per_cu_header_read_in (struct comp_unit_head *cu_headerp,
16873 struct dwarf2_per_cu_data *per_cu)
16874{
96408a79
SA
16875 gdb_byte *info_ptr;
16876
16877 if (per_cu->cu)
16878 return &per_cu->cu->header;
16879
0bc3a05c 16880 info_ptr = per_cu->info_or_types_section->buffer + per_cu->offset.sect_off;
96408a79
SA
16881
16882 memset (cu_headerp, 0, sizeof (*cu_headerp));
0bc3a05c 16883 read_comp_unit_head (cu_headerp, info_ptr, per_cu->objfile->obfd);
96408a79
SA
16884
16885 return cu_headerp;
16886}
16887
ae0d2f24
UW
16888/* Return the address size given in the compilation unit header for CU. */
16889
98714339 16890int
ae0d2f24
UW
16891dwarf2_per_cu_addr_size (struct dwarf2_per_cu_data *per_cu)
16892{
96408a79
SA
16893 struct comp_unit_head cu_header_local;
16894 const struct comp_unit_head *cu_headerp;
c471e790 16895
96408a79
SA
16896 cu_headerp = per_cu_header_read_in (&cu_header_local, per_cu);
16897
16898 return cu_headerp->addr_size;
ae0d2f24
UW
16899}
16900
9eae7c52
TT
16901/* Return the offset size given in the compilation unit header for CU. */
16902
16903int
16904dwarf2_per_cu_offset_size (struct dwarf2_per_cu_data *per_cu)
16905{
96408a79
SA
16906 struct comp_unit_head cu_header_local;
16907 const struct comp_unit_head *cu_headerp;
9c6c53f7 16908
96408a79
SA
16909 cu_headerp = per_cu_header_read_in (&cu_header_local, per_cu);
16910
16911 return cu_headerp->offset_size;
16912}
16913
16914/* See its dwarf2loc.h declaration. */
16915
16916int
16917dwarf2_per_cu_ref_addr_size (struct dwarf2_per_cu_data *per_cu)
16918{
16919 struct comp_unit_head cu_header_local;
16920 const struct comp_unit_head *cu_headerp;
16921
16922 cu_headerp = per_cu_header_read_in (&cu_header_local, per_cu);
16923
16924 if (cu_headerp->version == 2)
16925 return cu_headerp->addr_size;
16926 else
16927 return cu_headerp->offset_size;
181cebd4
JK
16928}
16929
9aa1f1e3
TT
16930/* Return the text offset of the CU. The returned offset comes from
16931 this CU's objfile. If this objfile came from a separate debuginfo
16932 file, then the offset may be different from the corresponding
16933 offset in the parent objfile. */
16934
16935CORE_ADDR
16936dwarf2_per_cu_text_offset (struct dwarf2_per_cu_data *per_cu)
16937{
bb3fa9d0 16938 struct objfile *objfile = per_cu->objfile;
9aa1f1e3
TT
16939
16940 return ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
16941}
16942
348e048f
DE
16943/* Locate the .debug_info compilation unit from CU's objfile which contains
16944 the DIE at OFFSET. Raises an error on failure. */
ae038cb0
DJ
16945
16946static struct dwarf2_per_cu_data *
b64f50a1 16947dwarf2_find_containing_comp_unit (sect_offset offset,
ae038cb0
DJ
16948 struct objfile *objfile)
16949{
16950 struct dwarf2_per_cu_data *this_cu;
16951 int low, high;
16952
ae038cb0
DJ
16953 low = 0;
16954 high = dwarf2_per_objfile->n_comp_units - 1;
16955 while (high > low)
16956 {
16957 int mid = low + (high - low) / 2;
9a619af0 16958
b64f50a1
JK
16959 if (dwarf2_per_objfile->all_comp_units[mid]->offset.sect_off
16960 >= offset.sect_off)
ae038cb0
DJ
16961 high = mid;
16962 else
16963 low = mid + 1;
16964 }
16965 gdb_assert (low == high);
b64f50a1
JK
16966 if (dwarf2_per_objfile->all_comp_units[low]->offset.sect_off
16967 > offset.sect_off)
ae038cb0 16968 {
10b3939b 16969 if (low == 0)
8a3fe4f8
AC
16970 error (_("Dwarf Error: could not find partial DIE containing "
16971 "offset 0x%lx [in module %s]"),
b64f50a1 16972 (long) offset.sect_off, bfd_get_filename (objfile->obfd));
10b3939b 16973
b64f50a1
JK
16974 gdb_assert (dwarf2_per_objfile->all_comp_units[low-1]->offset.sect_off
16975 <= offset.sect_off);
ae038cb0
DJ
16976 return dwarf2_per_objfile->all_comp_units[low-1];
16977 }
16978 else
16979 {
16980 this_cu = dwarf2_per_objfile->all_comp_units[low];
16981 if (low == dwarf2_per_objfile->n_comp_units - 1
b64f50a1
JK
16982 && offset.sect_off >= this_cu->offset.sect_off + this_cu->length)
16983 error (_("invalid dwarf2 offset %u"), offset.sect_off);
16984 gdb_assert (offset.sect_off < this_cu->offset.sect_off + this_cu->length);
ae038cb0
DJ
16985 return this_cu;
16986 }
16987}
16988
23745b47 16989/* Initialize dwarf2_cu CU, owned by PER_CU. */
93311388 16990
9816fde3 16991static void
23745b47 16992init_one_comp_unit (struct dwarf2_cu *cu, struct dwarf2_per_cu_data *per_cu)
93311388 16993{
9816fde3 16994 memset (cu, 0, sizeof (*cu));
23745b47
DE
16995 per_cu->cu = cu;
16996 cu->per_cu = per_cu;
16997 cu->objfile = per_cu->objfile;
93311388 16998 obstack_init (&cu->comp_unit_obstack);
9816fde3
JK
16999}
17000
17001/* Initialize basic fields of dwarf_cu CU according to DIE COMP_UNIT_DIE. */
17002
17003static void
95554aad
TT
17004prepare_one_comp_unit (struct dwarf2_cu *cu, struct die_info *comp_unit_die,
17005 enum language pretend_language)
9816fde3
JK
17006{
17007 struct attribute *attr;
17008
17009 /* Set the language we're debugging. */
17010 attr = dwarf2_attr (comp_unit_die, DW_AT_language, cu);
17011 if (attr)
17012 set_cu_language (DW_UNSND (attr), cu);
17013 else
9cded63f 17014 {
95554aad 17015 cu->language = pretend_language;
9cded63f
TT
17016 cu->language_defn = language_def (cu->language);
17017 }
dee91e82
DE
17018
17019 attr = dwarf2_attr (comp_unit_die, DW_AT_producer, cu);
17020 if (attr)
17021 cu->producer = DW_STRING (attr);
93311388
DE
17022}
17023
ae038cb0
DJ
17024/* Release one cached compilation unit, CU. We unlink it from the tree
17025 of compilation units, but we don't remove it from the read_in_chain;
93311388
DE
17026 the caller is responsible for that.
17027 NOTE: DATA is a void * because this function is also used as a
17028 cleanup routine. */
ae038cb0
DJ
17029
17030static void
68dc6402 17031free_heap_comp_unit (void *data)
ae038cb0
DJ
17032{
17033 struct dwarf2_cu *cu = data;
17034
23745b47
DE
17035 gdb_assert (cu->per_cu != NULL);
17036 cu->per_cu->cu = NULL;
ae038cb0
DJ
17037 cu->per_cu = NULL;
17038
17039 obstack_free (&cu->comp_unit_obstack, NULL);
17040
17041 xfree (cu);
17042}
17043
72bf9492 17044/* This cleanup function is passed the address of a dwarf2_cu on the stack
ae038cb0 17045 when we're finished with it. We can't free the pointer itself, but be
dee91e82 17046 sure to unlink it from the cache. Also release any associated storage. */
72bf9492
DJ
17047
17048static void
17049free_stack_comp_unit (void *data)
17050{
17051 struct dwarf2_cu *cu = data;
17052
23745b47
DE
17053 gdb_assert (cu->per_cu != NULL);
17054 cu->per_cu->cu = NULL;
17055 cu->per_cu = NULL;
17056
72bf9492
DJ
17057 obstack_free (&cu->comp_unit_obstack, NULL);
17058 cu->partial_dies = NULL;
ae038cb0
DJ
17059}
17060
17061/* Free all cached compilation units. */
17062
17063static void
17064free_cached_comp_units (void *data)
17065{
17066 struct dwarf2_per_cu_data *per_cu, **last_chain;
17067
17068 per_cu = dwarf2_per_objfile->read_in_chain;
17069 last_chain = &dwarf2_per_objfile->read_in_chain;
17070 while (per_cu != NULL)
17071 {
17072 struct dwarf2_per_cu_data *next_cu;
17073
17074 next_cu = per_cu->cu->read_in_chain;
17075
68dc6402 17076 free_heap_comp_unit (per_cu->cu);
ae038cb0
DJ
17077 *last_chain = next_cu;
17078
17079 per_cu = next_cu;
17080 }
17081}
17082
17083/* Increase the age counter on each cached compilation unit, and free
17084 any that are too old. */
17085
17086static void
17087age_cached_comp_units (void)
17088{
17089 struct dwarf2_per_cu_data *per_cu, **last_chain;
17090
17091 dwarf2_clear_marks (dwarf2_per_objfile->read_in_chain);
17092 per_cu = dwarf2_per_objfile->read_in_chain;
17093 while (per_cu != NULL)
17094 {
17095 per_cu->cu->last_used ++;
17096 if (per_cu->cu->last_used <= dwarf2_max_cache_age)
17097 dwarf2_mark (per_cu->cu);
17098 per_cu = per_cu->cu->read_in_chain;
17099 }
17100
17101 per_cu = dwarf2_per_objfile->read_in_chain;
17102 last_chain = &dwarf2_per_objfile->read_in_chain;
17103 while (per_cu != NULL)
17104 {
17105 struct dwarf2_per_cu_data *next_cu;
17106
17107 next_cu = per_cu->cu->read_in_chain;
17108
17109 if (!per_cu->cu->mark)
17110 {
68dc6402 17111 free_heap_comp_unit (per_cu->cu);
ae038cb0
DJ
17112 *last_chain = next_cu;
17113 }
17114 else
17115 last_chain = &per_cu->cu->read_in_chain;
17116
17117 per_cu = next_cu;
17118 }
17119}
17120
17121/* Remove a single compilation unit from the cache. */
17122
17123static void
dee91e82 17124free_one_cached_comp_unit (struct dwarf2_per_cu_data *target_per_cu)
ae038cb0
DJ
17125{
17126 struct dwarf2_per_cu_data *per_cu, **last_chain;
17127
17128 per_cu = dwarf2_per_objfile->read_in_chain;
17129 last_chain = &dwarf2_per_objfile->read_in_chain;
17130 while (per_cu != NULL)
17131 {
17132 struct dwarf2_per_cu_data *next_cu;
17133
17134 next_cu = per_cu->cu->read_in_chain;
17135
dee91e82 17136 if (per_cu == target_per_cu)
ae038cb0 17137 {
68dc6402 17138 free_heap_comp_unit (per_cu->cu);
dee91e82 17139 per_cu->cu = NULL;
ae038cb0
DJ
17140 *last_chain = next_cu;
17141 break;
17142 }
17143 else
17144 last_chain = &per_cu->cu->read_in_chain;
17145
17146 per_cu = next_cu;
17147 }
17148}
17149
fe3e1990
DJ
17150/* Release all extra memory associated with OBJFILE. */
17151
17152void
17153dwarf2_free_objfile (struct objfile *objfile)
17154{
17155 dwarf2_per_objfile = objfile_data (objfile, dwarf2_objfile_data_key);
17156
17157 if (dwarf2_per_objfile == NULL)
17158 return;
17159
17160 /* Cached DIE trees use xmalloc and the comp_unit_obstack. */
17161 free_cached_comp_units (NULL);
17162
7b9f3c50
DE
17163 if (dwarf2_per_objfile->quick_file_names_table)
17164 htab_delete (dwarf2_per_objfile->quick_file_names_table);
9291a0cd 17165
fe3e1990
DJ
17166 /* Everything else should be on the objfile obstack. */
17167}
17168
dee91e82
DE
17169/* A set of CU "per_cu" pointer, DIE offset, and GDB type pointer.
17170 We store these in a hash table separate from the DIEs, and preserve them
17171 when the DIEs are flushed out of cache.
17172
17173 The CU "per_cu" pointer is needed because offset alone is not enough to
3019eac3
DE
17174 uniquely identify the type. A file may have multiple .debug_types sections,
17175 or the type may come from a DWO file. We have to use something in
17176 dwarf2_per_cu_data (or the pointer to it) because we can enter the lookup
17177 routine, get_die_type_at_offset, from outside this file, and thus won't
17178 necessarily have PER_CU->cu. Fortunately, PER_CU is stable for the life
17179 of the objfile. */
1c379e20 17180
dee91e82 17181struct dwarf2_per_cu_offset_and_type
1c379e20 17182{
dee91e82 17183 const struct dwarf2_per_cu_data *per_cu;
b64f50a1 17184 sect_offset offset;
1c379e20
DJ
17185 struct type *type;
17186};
17187
dee91e82 17188/* Hash function for a dwarf2_per_cu_offset_and_type. */
1c379e20
DJ
17189
17190static hashval_t
dee91e82 17191per_cu_offset_and_type_hash (const void *item)
1c379e20 17192{
dee91e82 17193 const struct dwarf2_per_cu_offset_and_type *ofs = item;
9a619af0 17194
dee91e82 17195 return (uintptr_t) ofs->per_cu + ofs->offset.sect_off;
1c379e20
DJ
17196}
17197
dee91e82 17198/* Equality function for a dwarf2_per_cu_offset_and_type. */
1c379e20
DJ
17199
17200static int
dee91e82 17201per_cu_offset_and_type_eq (const void *item_lhs, const void *item_rhs)
1c379e20 17202{
dee91e82
DE
17203 const struct dwarf2_per_cu_offset_and_type *ofs_lhs = item_lhs;
17204 const struct dwarf2_per_cu_offset_and_type *ofs_rhs = item_rhs;
9a619af0 17205
dee91e82
DE
17206 return (ofs_lhs->per_cu == ofs_rhs->per_cu
17207 && ofs_lhs->offset.sect_off == ofs_rhs->offset.sect_off);
1c379e20
DJ
17208}
17209
17210/* Set the type associated with DIE to TYPE. Save it in CU's hash
7e314c57
JK
17211 table if necessary. For convenience, return TYPE.
17212
17213 The DIEs reading must have careful ordering to:
17214 * Not cause infite loops trying to read in DIEs as a prerequisite for
17215 reading current DIE.
17216 * Not trying to dereference contents of still incompletely read in types
17217 while reading in other DIEs.
17218 * Enable referencing still incompletely read in types just by a pointer to
17219 the type without accessing its fields.
17220
17221 Therefore caller should follow these rules:
17222 * Try to fetch any prerequisite types we may need to build this DIE type
17223 before building the type and calling set_die_type.
e71ec853 17224 * After building type call set_die_type for current DIE as soon as
7e314c57
JK
17225 possible before fetching more types to complete the current type.
17226 * Make the type as complete as possible before fetching more types. */
1c379e20 17227
f792889a 17228static struct type *
1c379e20
DJ
17229set_die_type (struct die_info *die, struct type *type, struct dwarf2_cu *cu)
17230{
dee91e82 17231 struct dwarf2_per_cu_offset_and_type **slot, ofs;
673bfd45 17232 struct objfile *objfile = cu->objfile;
1c379e20 17233
b4ba55a1
JB
17234 /* For Ada types, make sure that the gnat-specific data is always
17235 initialized (if not already set). There are a few types where
17236 we should not be doing so, because the type-specific area is
17237 already used to hold some other piece of info (eg: TYPE_CODE_FLT
17238 where the type-specific area is used to store the floatformat).
17239 But this is not a problem, because the gnat-specific information
17240 is actually not needed for these types. */
17241 if (need_gnat_info (cu)
17242 && TYPE_CODE (type) != TYPE_CODE_FUNC
17243 && TYPE_CODE (type) != TYPE_CODE_FLT
17244 && !HAVE_GNAT_AUX_INFO (type))
17245 INIT_GNAT_SPECIFIC (type);
17246
dee91e82 17247 if (dwarf2_per_objfile->die_type_hash == NULL)
f792889a 17248 {
dee91e82
DE
17249 dwarf2_per_objfile->die_type_hash =
17250 htab_create_alloc_ex (127,
17251 per_cu_offset_and_type_hash,
17252 per_cu_offset_and_type_eq,
17253 NULL,
17254 &objfile->objfile_obstack,
17255 hashtab_obstack_allocate,
17256 dummy_obstack_deallocate);
f792889a 17257 }
1c379e20 17258
dee91e82 17259 ofs.per_cu = cu->per_cu;
1c379e20
DJ
17260 ofs.offset = die->offset;
17261 ofs.type = type;
dee91e82
DE
17262 slot = (struct dwarf2_per_cu_offset_and_type **)
17263 htab_find_slot (dwarf2_per_objfile->die_type_hash, &ofs, INSERT);
7e314c57
JK
17264 if (*slot)
17265 complaint (&symfile_complaints,
17266 _("A problem internal to GDB: DIE 0x%x has type already set"),
b64f50a1 17267 die->offset.sect_off);
673bfd45 17268 *slot = obstack_alloc (&objfile->objfile_obstack, sizeof (**slot));
1c379e20 17269 **slot = ofs;
f792889a 17270 return type;
1c379e20
DJ
17271}
17272
380bca97 17273/* Look up the type for the die at OFFSET in the appropriate type_hash
673bfd45 17274 table, or return NULL if the die does not have a saved type. */
1c379e20
DJ
17275
17276static struct type *
b64f50a1 17277get_die_type_at_offset (sect_offset offset,
673bfd45 17278 struct dwarf2_per_cu_data *per_cu)
1c379e20 17279{
dee91e82 17280 struct dwarf2_per_cu_offset_and_type *slot, ofs;
f792889a 17281
dee91e82 17282 if (dwarf2_per_objfile->die_type_hash == NULL)
f792889a 17283 return NULL;
1c379e20 17284
dee91e82 17285 ofs.per_cu = per_cu;
673bfd45 17286 ofs.offset = offset;
dee91e82 17287 slot = htab_find (dwarf2_per_objfile->die_type_hash, &ofs);
1c379e20
DJ
17288 if (slot)
17289 return slot->type;
17290 else
17291 return NULL;
17292}
17293
673bfd45
DE
17294/* Look up the type for DIE in the appropriate type_hash table,
17295 or return NULL if DIE does not have a saved type. */
17296
17297static struct type *
17298get_die_type (struct die_info *die, struct dwarf2_cu *cu)
17299{
17300 return get_die_type_at_offset (die->offset, cu->per_cu);
17301}
17302
10b3939b
DJ
17303/* Add a dependence relationship from CU to REF_PER_CU. */
17304
17305static void
17306dwarf2_add_dependence (struct dwarf2_cu *cu,
17307 struct dwarf2_per_cu_data *ref_per_cu)
17308{
17309 void **slot;
17310
17311 if (cu->dependencies == NULL)
17312 cu->dependencies
17313 = htab_create_alloc_ex (5, htab_hash_pointer, htab_eq_pointer,
17314 NULL, &cu->comp_unit_obstack,
17315 hashtab_obstack_allocate,
17316 dummy_obstack_deallocate);
17317
17318 slot = htab_find_slot (cu->dependencies, ref_per_cu, INSERT);
17319 if (*slot == NULL)
17320 *slot = ref_per_cu;
17321}
1c379e20 17322
f504f079
DE
17323/* Subroutine of dwarf2_mark to pass to htab_traverse.
17324 Set the mark field in every compilation unit in the
ae038cb0
DJ
17325 cache that we must keep because we are keeping CU. */
17326
10b3939b
DJ
17327static int
17328dwarf2_mark_helper (void **slot, void *data)
17329{
17330 struct dwarf2_per_cu_data *per_cu;
17331
17332 per_cu = (struct dwarf2_per_cu_data *) *slot;
d07ed419
JK
17333
17334 /* cu->dependencies references may not yet have been ever read if QUIT aborts
17335 reading of the chain. As such dependencies remain valid it is not much
17336 useful to track and undo them during QUIT cleanups. */
17337 if (per_cu->cu == NULL)
17338 return 1;
17339
10b3939b
DJ
17340 if (per_cu->cu->mark)
17341 return 1;
17342 per_cu->cu->mark = 1;
17343
17344 if (per_cu->cu->dependencies != NULL)
17345 htab_traverse (per_cu->cu->dependencies, dwarf2_mark_helper, NULL);
17346
17347 return 1;
17348}
17349
f504f079
DE
17350/* Set the mark field in CU and in every other compilation unit in the
17351 cache that we must keep because we are keeping CU. */
17352
ae038cb0
DJ
17353static void
17354dwarf2_mark (struct dwarf2_cu *cu)
17355{
17356 if (cu->mark)
17357 return;
17358 cu->mark = 1;
10b3939b
DJ
17359 if (cu->dependencies != NULL)
17360 htab_traverse (cu->dependencies, dwarf2_mark_helper, NULL);
ae038cb0
DJ
17361}
17362
17363static void
17364dwarf2_clear_marks (struct dwarf2_per_cu_data *per_cu)
17365{
17366 while (per_cu)
17367 {
17368 per_cu->cu->mark = 0;
17369 per_cu = per_cu->cu->read_in_chain;
17370 }
72bf9492
DJ
17371}
17372
72bf9492
DJ
17373/* Trivial hash function for partial_die_info: the hash value of a DIE
17374 is its offset in .debug_info for this objfile. */
17375
17376static hashval_t
17377partial_die_hash (const void *item)
17378{
17379 const struct partial_die_info *part_die = item;
9a619af0 17380
b64f50a1 17381 return part_die->offset.sect_off;
72bf9492
DJ
17382}
17383
17384/* Trivial comparison function for partial_die_info structures: two DIEs
17385 are equal if they have the same offset. */
17386
17387static int
17388partial_die_eq (const void *item_lhs, const void *item_rhs)
17389{
17390 const struct partial_die_info *part_die_lhs = item_lhs;
17391 const struct partial_die_info *part_die_rhs = item_rhs;
9a619af0 17392
b64f50a1 17393 return part_die_lhs->offset.sect_off == part_die_rhs->offset.sect_off;
72bf9492
DJ
17394}
17395
ae038cb0
DJ
17396static struct cmd_list_element *set_dwarf2_cmdlist;
17397static struct cmd_list_element *show_dwarf2_cmdlist;
17398
17399static void
17400set_dwarf2_cmd (char *args, int from_tty)
17401{
17402 help_list (set_dwarf2_cmdlist, "maintenance set dwarf2 ", -1, gdb_stdout);
17403}
17404
17405static void
17406show_dwarf2_cmd (char *args, int from_tty)
6e70227d 17407{
ae038cb0
DJ
17408 cmd_show_list (show_dwarf2_cmdlist, from_tty, "");
17409}
17410
dce234bc
PP
17411/* If section described by INFO was mmapped, munmap it now. */
17412
17413static void
17414munmap_section_buffer (struct dwarf2_section_info *info)
17415{
b315ab21 17416 if (info->map_addr != NULL)
dce234bc
PP
17417 {
17418#ifdef HAVE_MMAP
b315ab21 17419 int res;
9a619af0 17420
b315ab21
TG
17421 res = munmap (info->map_addr, info->map_len);
17422 gdb_assert (res == 0);
dce234bc
PP
17423#else
17424 /* Without HAVE_MMAP, we should never be here to begin with. */
f3574227 17425 gdb_assert_not_reached ("no mmap support");
dce234bc
PP
17426#endif
17427 }
17428}
17429
17430/* munmap debug sections for OBJFILE, if necessary. */
17431
17432static void
c1bd65d0 17433dwarf2_per_objfile_free (struct objfile *objfile, void *d)
dce234bc
PP
17434{
17435 struct dwarf2_per_objfile *data = d;
8b70b953
TT
17436 int ix;
17437 struct dwarf2_section_info *section;
9a619af0 17438
16be1145
DE
17439 /* This is sorted according to the order they're defined in to make it easier
17440 to keep in sync. */
dce234bc
PP
17441 munmap_section_buffer (&data->info);
17442 munmap_section_buffer (&data->abbrev);
17443 munmap_section_buffer (&data->line);
16be1145 17444 munmap_section_buffer (&data->loc);
dce234bc 17445 munmap_section_buffer (&data->macinfo);
cf2c3c16 17446 munmap_section_buffer (&data->macro);
16be1145 17447 munmap_section_buffer (&data->str);
dce234bc 17448 munmap_section_buffer (&data->ranges);
3019eac3 17449 munmap_section_buffer (&data->addr);
dce234bc
PP
17450 munmap_section_buffer (&data->frame);
17451 munmap_section_buffer (&data->eh_frame);
9291a0cd 17452 munmap_section_buffer (&data->gdb_index);
8b70b953
TT
17453
17454 for (ix = 0;
17455 VEC_iterate (dwarf2_section_info_def, data->types, ix, section);
17456 ++ix)
17457 munmap_section_buffer (section);
17458
95554aad
TT
17459 for (ix = 0; ix < dwarf2_per_objfile->n_comp_units; ++ix)
17460 VEC_free (dwarf2_per_cu_ptr,
17461 dwarf2_per_objfile->all_comp_units[ix]->imported_symtabs);
17462
8b70b953 17463 VEC_free (dwarf2_section_info_def, data->types);
3019eac3
DE
17464
17465 if (data->dwo_files)
17466 free_dwo_files (data->dwo_files, objfile);
9291a0cd
TT
17467}
17468
17469\f
ae2de4f8 17470/* The "save gdb-index" command. */
9291a0cd
TT
17471
17472/* The contents of the hash table we create when building the string
17473 table. */
17474struct strtab_entry
17475{
17476 offset_type offset;
17477 const char *str;
17478};
17479
559a7a62
JK
17480/* Hash function for a strtab_entry.
17481
17482 Function is used only during write_hash_table so no index format backward
17483 compatibility is needed. */
b89be57b 17484
9291a0cd
TT
17485static hashval_t
17486hash_strtab_entry (const void *e)
17487{
17488 const struct strtab_entry *entry = e;
559a7a62 17489 return mapped_index_string_hash (INT_MAX, entry->str);
9291a0cd
TT
17490}
17491
17492/* Equality function for a strtab_entry. */
b89be57b 17493
9291a0cd
TT
17494static int
17495eq_strtab_entry (const void *a, const void *b)
17496{
17497 const struct strtab_entry *ea = a;
17498 const struct strtab_entry *eb = b;
17499 return !strcmp (ea->str, eb->str);
17500}
17501
17502/* Create a strtab_entry hash table. */
b89be57b 17503
9291a0cd
TT
17504static htab_t
17505create_strtab (void)
17506{
17507 return htab_create_alloc (100, hash_strtab_entry, eq_strtab_entry,
17508 xfree, xcalloc, xfree);
17509}
17510
17511/* Add a string to the constant pool. Return the string's offset in
17512 host order. */
b89be57b 17513
9291a0cd
TT
17514static offset_type
17515add_string (htab_t table, struct obstack *cpool, const char *str)
17516{
17517 void **slot;
17518 struct strtab_entry entry;
17519 struct strtab_entry *result;
17520
17521 entry.str = str;
17522 slot = htab_find_slot (table, &entry, INSERT);
17523 if (*slot)
17524 result = *slot;
17525 else
17526 {
17527 result = XNEW (struct strtab_entry);
17528 result->offset = obstack_object_size (cpool);
17529 result->str = str;
17530 obstack_grow_str0 (cpool, str);
17531 *slot = result;
17532 }
17533 return result->offset;
17534}
17535
17536/* An entry in the symbol table. */
17537struct symtab_index_entry
17538{
17539 /* The name of the symbol. */
17540 const char *name;
17541 /* The offset of the name in the constant pool. */
17542 offset_type index_offset;
17543 /* A sorted vector of the indices of all the CUs that hold an object
17544 of this name. */
17545 VEC (offset_type) *cu_indices;
17546};
17547
17548/* The symbol table. This is a power-of-2-sized hash table. */
17549struct mapped_symtab
17550{
17551 offset_type n_elements;
17552 offset_type size;
17553 struct symtab_index_entry **data;
17554};
17555
17556/* Hash function for a symtab_index_entry. */
b89be57b 17557
9291a0cd
TT
17558static hashval_t
17559hash_symtab_entry (const void *e)
17560{
17561 const struct symtab_index_entry *entry = e;
17562 return iterative_hash (VEC_address (offset_type, entry->cu_indices),
17563 sizeof (offset_type) * VEC_length (offset_type,
17564 entry->cu_indices),
17565 0);
17566}
17567
17568/* Equality function for a symtab_index_entry. */
b89be57b 17569
9291a0cd
TT
17570static int
17571eq_symtab_entry (const void *a, const void *b)
17572{
17573 const struct symtab_index_entry *ea = a;
17574 const struct symtab_index_entry *eb = b;
17575 int len = VEC_length (offset_type, ea->cu_indices);
17576 if (len != VEC_length (offset_type, eb->cu_indices))
17577 return 0;
17578 return !memcmp (VEC_address (offset_type, ea->cu_indices),
17579 VEC_address (offset_type, eb->cu_indices),
17580 sizeof (offset_type) * len);
17581}
17582
17583/* Destroy a symtab_index_entry. */
b89be57b 17584
9291a0cd
TT
17585static void
17586delete_symtab_entry (void *p)
17587{
17588 struct symtab_index_entry *entry = p;
17589 VEC_free (offset_type, entry->cu_indices);
17590 xfree (entry);
17591}
17592
17593/* Create a hash table holding symtab_index_entry objects. */
b89be57b 17594
9291a0cd 17595static htab_t
3876f04e 17596create_symbol_hash_table (void)
9291a0cd
TT
17597{
17598 return htab_create_alloc (100, hash_symtab_entry, eq_symtab_entry,
17599 delete_symtab_entry, xcalloc, xfree);
17600}
17601
17602/* Create a new mapped symtab object. */
b89be57b 17603
9291a0cd
TT
17604static struct mapped_symtab *
17605create_mapped_symtab (void)
17606{
17607 struct mapped_symtab *symtab = XNEW (struct mapped_symtab);
17608 symtab->n_elements = 0;
17609 symtab->size = 1024;
17610 symtab->data = XCNEWVEC (struct symtab_index_entry *, symtab->size);
17611 return symtab;
17612}
17613
17614/* Destroy a mapped_symtab. */
b89be57b 17615
9291a0cd
TT
17616static void
17617cleanup_mapped_symtab (void *p)
17618{
17619 struct mapped_symtab *symtab = p;
17620 /* The contents of the array are freed when the other hash table is
17621 destroyed. */
17622 xfree (symtab->data);
17623 xfree (symtab);
17624}
17625
17626/* Find a slot in SYMTAB for the symbol NAME. Returns a pointer to
559a7a62
JK
17627 the slot.
17628
17629 Function is used only during write_hash_table so no index format backward
17630 compatibility is needed. */
b89be57b 17631
9291a0cd
TT
17632static struct symtab_index_entry **
17633find_slot (struct mapped_symtab *symtab, const char *name)
17634{
559a7a62 17635 offset_type index, step, hash = mapped_index_string_hash (INT_MAX, name);
9291a0cd
TT
17636
17637 index = hash & (symtab->size - 1);
17638 step = ((hash * 17) & (symtab->size - 1)) | 1;
17639
17640 for (;;)
17641 {
17642 if (!symtab->data[index] || !strcmp (name, symtab->data[index]->name))
17643 return &symtab->data[index];
17644 index = (index + step) & (symtab->size - 1);
17645 }
17646}
17647
17648/* Expand SYMTAB's hash table. */
b89be57b 17649
9291a0cd
TT
17650static void
17651hash_expand (struct mapped_symtab *symtab)
17652{
17653 offset_type old_size = symtab->size;
17654 offset_type i;
17655 struct symtab_index_entry **old_entries = symtab->data;
17656
17657 symtab->size *= 2;
17658 symtab->data = XCNEWVEC (struct symtab_index_entry *, symtab->size);
17659
17660 for (i = 0; i < old_size; ++i)
17661 {
17662 if (old_entries[i])
17663 {
17664 struct symtab_index_entry **slot = find_slot (symtab,
17665 old_entries[i]->name);
17666 *slot = old_entries[i];
17667 }
17668 }
17669
17670 xfree (old_entries);
17671}
17672
156942c7
DE
17673/* Add an entry to SYMTAB. NAME is the name of the symbol.
17674 CU_INDEX is the index of the CU in which the symbol appears.
17675 IS_STATIC is one if the symbol is static, otherwise zero (global). */
b89be57b 17676
9291a0cd
TT
17677static void
17678add_index_entry (struct mapped_symtab *symtab, const char *name,
156942c7 17679 int is_static, gdb_index_symbol_kind kind,
9291a0cd
TT
17680 offset_type cu_index)
17681{
17682 struct symtab_index_entry **slot;
156942c7 17683 offset_type cu_index_and_attrs;
9291a0cd
TT
17684
17685 ++symtab->n_elements;
17686 if (4 * symtab->n_elements / 3 >= symtab->size)
17687 hash_expand (symtab);
17688
17689 slot = find_slot (symtab, name);
17690 if (!*slot)
17691 {
17692 *slot = XNEW (struct symtab_index_entry);
17693 (*slot)->name = name;
156942c7 17694 /* index_offset is set later. */
9291a0cd
TT
17695 (*slot)->cu_indices = NULL;
17696 }
156942c7
DE
17697
17698 cu_index_and_attrs = 0;
17699 DW2_GDB_INDEX_CU_SET_VALUE (cu_index_and_attrs, cu_index);
17700 DW2_GDB_INDEX_SYMBOL_STATIC_SET_VALUE (cu_index_and_attrs, is_static);
17701 DW2_GDB_INDEX_SYMBOL_KIND_SET_VALUE (cu_index_and_attrs, kind);
17702
17703 /* We don't want to record an index value twice as we want to avoid the
17704 duplication.
17705 We process all global symbols and then all static symbols
17706 (which would allow us to avoid the duplication by only having to check
17707 the last entry pushed), but a symbol could have multiple kinds in one CU.
17708 To keep things simple we don't worry about the duplication here and
17709 sort and uniqufy the list after we've processed all symbols. */
17710 VEC_safe_push (offset_type, (*slot)->cu_indices, cu_index_and_attrs);
17711}
17712
17713/* qsort helper routine for uniquify_cu_indices. */
17714
17715static int
17716offset_type_compare (const void *ap, const void *bp)
17717{
17718 offset_type a = *(offset_type *) ap;
17719 offset_type b = *(offset_type *) bp;
17720
17721 return (a > b) - (b > a);
17722}
17723
17724/* Sort and remove duplicates of all symbols' cu_indices lists. */
17725
17726static void
17727uniquify_cu_indices (struct mapped_symtab *symtab)
17728{
17729 int i;
17730
17731 for (i = 0; i < symtab->size; ++i)
17732 {
17733 struct symtab_index_entry *entry = symtab->data[i];
17734
17735 if (entry
17736 && entry->cu_indices != NULL)
17737 {
17738 unsigned int next_to_insert, next_to_check;
17739 offset_type last_value;
17740
17741 qsort (VEC_address (offset_type, entry->cu_indices),
17742 VEC_length (offset_type, entry->cu_indices),
17743 sizeof (offset_type), offset_type_compare);
17744
17745 last_value = VEC_index (offset_type, entry->cu_indices, 0);
17746 next_to_insert = 1;
17747 for (next_to_check = 1;
17748 next_to_check < VEC_length (offset_type, entry->cu_indices);
17749 ++next_to_check)
17750 {
17751 if (VEC_index (offset_type, entry->cu_indices, next_to_check)
17752 != last_value)
17753 {
17754 last_value = VEC_index (offset_type, entry->cu_indices,
17755 next_to_check);
17756 VEC_replace (offset_type, entry->cu_indices, next_to_insert,
17757 last_value);
17758 ++next_to_insert;
17759 }
17760 }
17761 VEC_truncate (offset_type, entry->cu_indices, next_to_insert);
17762 }
17763 }
9291a0cd
TT
17764}
17765
17766/* Add a vector of indices to the constant pool. */
b89be57b 17767
9291a0cd 17768static offset_type
3876f04e 17769add_indices_to_cpool (htab_t symbol_hash_table, struct obstack *cpool,
9291a0cd
TT
17770 struct symtab_index_entry *entry)
17771{
17772 void **slot;
17773
3876f04e 17774 slot = htab_find_slot (symbol_hash_table, entry, INSERT);
9291a0cd
TT
17775 if (!*slot)
17776 {
17777 offset_type len = VEC_length (offset_type, entry->cu_indices);
17778 offset_type val = MAYBE_SWAP (len);
17779 offset_type iter;
17780 int i;
17781
17782 *slot = entry;
17783 entry->index_offset = obstack_object_size (cpool);
17784
17785 obstack_grow (cpool, &val, sizeof (val));
17786 for (i = 0;
17787 VEC_iterate (offset_type, entry->cu_indices, i, iter);
17788 ++i)
17789 {
17790 val = MAYBE_SWAP (iter);
17791 obstack_grow (cpool, &val, sizeof (val));
17792 }
17793 }
17794 else
17795 {
17796 struct symtab_index_entry *old_entry = *slot;
17797 entry->index_offset = old_entry->index_offset;
17798 entry = old_entry;
17799 }
17800 return entry->index_offset;
17801}
17802
17803/* Write the mapped hash table SYMTAB to the obstack OUTPUT, with
17804 constant pool entries going into the obstack CPOOL. */
b89be57b 17805
9291a0cd
TT
17806static void
17807write_hash_table (struct mapped_symtab *symtab,
17808 struct obstack *output, struct obstack *cpool)
17809{
17810 offset_type i;
3876f04e 17811 htab_t symbol_hash_table;
9291a0cd
TT
17812 htab_t str_table;
17813
3876f04e 17814 symbol_hash_table = create_symbol_hash_table ();
9291a0cd 17815 str_table = create_strtab ();
3876f04e 17816
9291a0cd
TT
17817 /* We add all the index vectors to the constant pool first, to
17818 ensure alignment is ok. */
17819 for (i = 0; i < symtab->size; ++i)
17820 {
17821 if (symtab->data[i])
3876f04e 17822 add_indices_to_cpool (symbol_hash_table, cpool, symtab->data[i]);
9291a0cd
TT
17823 }
17824
17825 /* Now write out the hash table. */
17826 for (i = 0; i < symtab->size; ++i)
17827 {
17828 offset_type str_off, vec_off;
17829
17830 if (symtab->data[i])
17831 {
17832 str_off = add_string (str_table, cpool, symtab->data[i]->name);
17833 vec_off = symtab->data[i]->index_offset;
17834 }
17835 else
17836 {
17837 /* While 0 is a valid constant pool index, it is not valid
17838 to have 0 for both offsets. */
17839 str_off = 0;
17840 vec_off = 0;
17841 }
17842
17843 str_off = MAYBE_SWAP (str_off);
17844 vec_off = MAYBE_SWAP (vec_off);
17845
17846 obstack_grow (output, &str_off, sizeof (str_off));
17847 obstack_grow (output, &vec_off, sizeof (vec_off));
17848 }
17849
17850 htab_delete (str_table);
3876f04e 17851 htab_delete (symbol_hash_table);
9291a0cd
TT
17852}
17853
0a5429f6
DE
17854/* Struct to map psymtab to CU index in the index file. */
17855struct psymtab_cu_index_map
17856{
17857 struct partial_symtab *psymtab;
17858 unsigned int cu_index;
17859};
17860
17861static hashval_t
17862hash_psymtab_cu_index (const void *item)
17863{
17864 const struct psymtab_cu_index_map *map = item;
17865
17866 return htab_hash_pointer (map->psymtab);
17867}
17868
17869static int
17870eq_psymtab_cu_index (const void *item_lhs, const void *item_rhs)
17871{
17872 const struct psymtab_cu_index_map *lhs = item_lhs;
17873 const struct psymtab_cu_index_map *rhs = item_rhs;
17874
17875 return lhs->psymtab == rhs->psymtab;
17876}
17877
17878/* Helper struct for building the address table. */
17879struct addrmap_index_data
17880{
17881 struct objfile *objfile;
17882 struct obstack *addr_obstack;
17883 htab_t cu_index_htab;
17884
17885 /* Non-zero if the previous_* fields are valid.
17886 We can't write an entry until we see the next entry (since it is only then
17887 that we know the end of the entry). */
17888 int previous_valid;
17889 /* Index of the CU in the table of all CUs in the index file. */
17890 unsigned int previous_cu_index;
0963b4bd 17891 /* Start address of the CU. */
0a5429f6
DE
17892 CORE_ADDR previous_cu_start;
17893};
17894
17895/* Write an address entry to OBSTACK. */
b89be57b 17896
9291a0cd 17897static void
0a5429f6
DE
17898add_address_entry (struct objfile *objfile, struct obstack *obstack,
17899 CORE_ADDR start, CORE_ADDR end, unsigned int cu_index)
9291a0cd 17900{
0a5429f6 17901 offset_type cu_index_to_write;
9291a0cd
TT
17902 char addr[8];
17903 CORE_ADDR baseaddr;
17904
17905 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
17906
0a5429f6
DE
17907 store_unsigned_integer (addr, 8, BFD_ENDIAN_LITTLE, start - baseaddr);
17908 obstack_grow (obstack, addr, 8);
17909 store_unsigned_integer (addr, 8, BFD_ENDIAN_LITTLE, end - baseaddr);
17910 obstack_grow (obstack, addr, 8);
17911 cu_index_to_write = MAYBE_SWAP (cu_index);
17912 obstack_grow (obstack, &cu_index_to_write, sizeof (offset_type));
17913}
17914
17915/* Worker function for traversing an addrmap to build the address table. */
17916
17917static int
17918add_address_entry_worker (void *datap, CORE_ADDR start_addr, void *obj)
17919{
17920 struct addrmap_index_data *data = datap;
17921 struct partial_symtab *pst = obj;
0a5429f6
DE
17922
17923 if (data->previous_valid)
17924 add_address_entry (data->objfile, data->addr_obstack,
17925 data->previous_cu_start, start_addr,
17926 data->previous_cu_index);
17927
17928 data->previous_cu_start = start_addr;
17929 if (pst != NULL)
17930 {
17931 struct psymtab_cu_index_map find_map, *map;
17932 find_map.psymtab = pst;
17933 map = htab_find (data->cu_index_htab, &find_map);
17934 gdb_assert (map != NULL);
17935 data->previous_cu_index = map->cu_index;
17936 data->previous_valid = 1;
17937 }
17938 else
17939 data->previous_valid = 0;
17940
17941 return 0;
17942}
17943
17944/* Write OBJFILE's address map to OBSTACK.
17945 CU_INDEX_HTAB is used to map addrmap entries to their CU indices
17946 in the index file. */
17947
17948static void
17949write_address_map (struct objfile *objfile, struct obstack *obstack,
17950 htab_t cu_index_htab)
17951{
17952 struct addrmap_index_data addrmap_index_data;
17953
17954 /* When writing the address table, we have to cope with the fact that
17955 the addrmap iterator only provides the start of a region; we have to
17956 wait until the next invocation to get the start of the next region. */
17957
17958 addrmap_index_data.objfile = objfile;
17959 addrmap_index_data.addr_obstack = obstack;
17960 addrmap_index_data.cu_index_htab = cu_index_htab;
17961 addrmap_index_data.previous_valid = 0;
17962
17963 addrmap_foreach (objfile->psymtabs_addrmap, add_address_entry_worker,
17964 &addrmap_index_data);
17965
17966 /* It's highly unlikely the last entry (end address = 0xff...ff)
17967 is valid, but we should still handle it.
17968 The end address is recorded as the start of the next region, but that
17969 doesn't work here. To cope we pass 0xff...ff, this is a rare situation
17970 anyway. */
17971 if (addrmap_index_data.previous_valid)
17972 add_address_entry (objfile, obstack,
17973 addrmap_index_data.previous_cu_start, (CORE_ADDR) -1,
17974 addrmap_index_data.previous_cu_index);
9291a0cd
TT
17975}
17976
156942c7
DE
17977/* Return the symbol kind of PSYM. */
17978
17979static gdb_index_symbol_kind
17980symbol_kind (struct partial_symbol *psym)
17981{
17982 domain_enum domain = PSYMBOL_DOMAIN (psym);
17983 enum address_class aclass = PSYMBOL_CLASS (psym);
17984
17985 switch (domain)
17986 {
17987 case VAR_DOMAIN:
17988 switch (aclass)
17989 {
17990 case LOC_BLOCK:
17991 return GDB_INDEX_SYMBOL_KIND_FUNCTION;
17992 case LOC_TYPEDEF:
17993 return GDB_INDEX_SYMBOL_KIND_TYPE;
17994 case LOC_COMPUTED:
17995 case LOC_CONST_BYTES:
17996 case LOC_OPTIMIZED_OUT:
17997 case LOC_STATIC:
17998 return GDB_INDEX_SYMBOL_KIND_VARIABLE;
17999 case LOC_CONST:
18000 /* Note: It's currently impossible to recognize psyms as enum values
18001 short of reading the type info. For now punt. */
18002 return GDB_INDEX_SYMBOL_KIND_VARIABLE;
18003 default:
18004 /* There are other LOC_FOO values that one might want to classify
18005 as variables, but dwarf2read.c doesn't currently use them. */
18006 return GDB_INDEX_SYMBOL_KIND_OTHER;
18007 }
18008 case STRUCT_DOMAIN:
18009 return GDB_INDEX_SYMBOL_KIND_TYPE;
18010 default:
18011 return GDB_INDEX_SYMBOL_KIND_OTHER;
18012 }
18013}
18014
9291a0cd 18015/* Add a list of partial symbols to SYMTAB. */
b89be57b 18016
9291a0cd
TT
18017static void
18018write_psymbols (struct mapped_symtab *symtab,
987d643c 18019 htab_t psyms_seen,
9291a0cd
TT
18020 struct partial_symbol **psymp,
18021 int count,
987d643c
TT
18022 offset_type cu_index,
18023 int is_static)
9291a0cd
TT
18024{
18025 for (; count-- > 0; ++psymp)
18026 {
156942c7
DE
18027 struct partial_symbol *psym = *psymp;
18028 void **slot;
987d643c 18029
156942c7 18030 if (SYMBOL_LANGUAGE (psym) == language_ada)
9291a0cd 18031 error (_("Ada is not currently supported by the index"));
987d643c 18032
987d643c 18033 /* Only add a given psymbol once. */
156942c7 18034 slot = htab_find_slot (psyms_seen, psym, INSERT);
987d643c
TT
18035 if (!*slot)
18036 {
156942c7
DE
18037 gdb_index_symbol_kind kind = symbol_kind (psym);
18038
18039 *slot = psym;
18040 add_index_entry (symtab, SYMBOL_SEARCH_NAME (psym),
18041 is_static, kind, cu_index);
987d643c 18042 }
9291a0cd
TT
18043 }
18044}
18045
18046/* Write the contents of an ("unfinished") obstack to FILE. Throw an
18047 exception if there is an error. */
b89be57b 18048
9291a0cd
TT
18049static void
18050write_obstack (FILE *file, struct obstack *obstack)
18051{
18052 if (fwrite (obstack_base (obstack), 1, obstack_object_size (obstack),
18053 file)
18054 != obstack_object_size (obstack))
18055 error (_("couldn't data write to file"));
18056}
18057
18058/* Unlink a file if the argument is not NULL. */
b89be57b 18059
9291a0cd
TT
18060static void
18061unlink_if_set (void *p)
18062{
18063 char **filename = p;
18064 if (*filename)
18065 unlink (*filename);
18066}
18067
1fd400ff
TT
18068/* A helper struct used when iterating over debug_types. */
18069struct signatured_type_index_data
18070{
18071 struct objfile *objfile;
18072 struct mapped_symtab *symtab;
18073 struct obstack *types_list;
987d643c 18074 htab_t psyms_seen;
1fd400ff
TT
18075 int cu_index;
18076};
18077
18078/* A helper function that writes a single signatured_type to an
18079 obstack. */
b89be57b 18080
1fd400ff
TT
18081static int
18082write_one_signatured_type (void **slot, void *d)
18083{
18084 struct signatured_type_index_data *info = d;
18085 struct signatured_type *entry = (struct signatured_type *) *slot;
e254ef6a
DE
18086 struct dwarf2_per_cu_data *per_cu = &entry->per_cu;
18087 struct partial_symtab *psymtab = per_cu->v.psymtab;
1fd400ff
TT
18088 gdb_byte val[8];
18089
18090 write_psymbols (info->symtab,
987d643c 18091 info->psyms_seen,
3e43a32a
MS
18092 info->objfile->global_psymbols.list
18093 + psymtab->globals_offset,
987d643c
TT
18094 psymtab->n_global_syms, info->cu_index,
18095 0);
1fd400ff 18096 write_psymbols (info->symtab,
987d643c 18097 info->psyms_seen,
3e43a32a
MS
18098 info->objfile->static_psymbols.list
18099 + psymtab->statics_offset,
987d643c
TT
18100 psymtab->n_static_syms, info->cu_index,
18101 1);
1fd400ff 18102
b64f50a1
JK
18103 store_unsigned_integer (val, 8, BFD_ENDIAN_LITTLE,
18104 entry->per_cu.offset.sect_off);
1fd400ff 18105 obstack_grow (info->types_list, val, 8);
3019eac3
DE
18106 store_unsigned_integer (val, 8, BFD_ENDIAN_LITTLE,
18107 entry->type_offset_in_tu.cu_off);
1fd400ff
TT
18108 obstack_grow (info->types_list, val, 8);
18109 store_unsigned_integer (val, 8, BFD_ENDIAN_LITTLE, entry->signature);
18110 obstack_grow (info->types_list, val, 8);
18111
18112 ++info->cu_index;
18113
18114 return 1;
18115}
18116
95554aad
TT
18117/* Recurse into all "included" dependencies and write their symbols as
18118 if they appeared in this psymtab. */
18119
18120static void
18121recursively_write_psymbols (struct objfile *objfile,
18122 struct partial_symtab *psymtab,
18123 struct mapped_symtab *symtab,
18124 htab_t psyms_seen,
18125 offset_type cu_index)
18126{
18127 int i;
18128
18129 for (i = 0; i < psymtab->number_of_dependencies; ++i)
18130 if (psymtab->dependencies[i]->user != NULL)
18131 recursively_write_psymbols (objfile, psymtab->dependencies[i],
18132 symtab, psyms_seen, cu_index);
18133
18134 write_psymbols (symtab,
18135 psyms_seen,
18136 objfile->global_psymbols.list + psymtab->globals_offset,
18137 psymtab->n_global_syms, cu_index,
18138 0);
18139 write_psymbols (symtab,
18140 psyms_seen,
18141 objfile->static_psymbols.list + psymtab->statics_offset,
18142 psymtab->n_static_syms, cu_index,
18143 1);
18144}
18145
9291a0cd 18146/* Create an index file for OBJFILE in the directory DIR. */
b89be57b 18147
9291a0cd
TT
18148static void
18149write_psymtabs_to_index (struct objfile *objfile, const char *dir)
18150{
18151 struct cleanup *cleanup;
18152 char *filename, *cleanup_filename;
1fd400ff
TT
18153 struct obstack contents, addr_obstack, constant_pool, symtab_obstack;
18154 struct obstack cu_list, types_cu_list;
9291a0cd
TT
18155 int i;
18156 FILE *out_file;
18157 struct mapped_symtab *symtab;
18158 offset_type val, size_of_contents, total_len;
18159 struct stat st;
987d643c 18160 htab_t psyms_seen;
0a5429f6
DE
18161 htab_t cu_index_htab;
18162 struct psymtab_cu_index_map *psymtab_cu_index_map;
9291a0cd 18163
b4f2f049 18164 if (!objfile->psymtabs || !objfile->psymtabs_addrmap)
9291a0cd 18165 return;
b4f2f049 18166
9291a0cd
TT
18167 if (dwarf2_per_objfile->using_index)
18168 error (_("Cannot use an index to create the index"));
18169
8b70b953
TT
18170 if (VEC_length (dwarf2_section_info_def, dwarf2_per_objfile->types) > 1)
18171 error (_("Cannot make an index when the file has multiple .debug_types sections"));
18172
9291a0cd 18173 if (stat (objfile->name, &st) < 0)
7e17e088 18174 perror_with_name (objfile->name);
9291a0cd
TT
18175
18176 filename = concat (dir, SLASH_STRING, lbasename (objfile->name),
18177 INDEX_SUFFIX, (char *) NULL);
18178 cleanup = make_cleanup (xfree, filename);
18179
18180 out_file = fopen (filename, "wb");
18181 if (!out_file)
18182 error (_("Can't open `%s' for writing"), filename);
18183
18184 cleanup_filename = filename;
18185 make_cleanup (unlink_if_set, &cleanup_filename);
18186
18187 symtab = create_mapped_symtab ();
18188 make_cleanup (cleanup_mapped_symtab, symtab);
18189
18190 obstack_init (&addr_obstack);
18191 make_cleanup_obstack_free (&addr_obstack);
18192
18193 obstack_init (&cu_list);
18194 make_cleanup_obstack_free (&cu_list);
18195
1fd400ff
TT
18196 obstack_init (&types_cu_list);
18197 make_cleanup_obstack_free (&types_cu_list);
18198
987d643c
TT
18199 psyms_seen = htab_create_alloc (100, htab_hash_pointer, htab_eq_pointer,
18200 NULL, xcalloc, xfree);
96408a79 18201 make_cleanup_htab_delete (psyms_seen);
987d643c 18202
0a5429f6
DE
18203 /* While we're scanning CU's create a table that maps a psymtab pointer
18204 (which is what addrmap records) to its index (which is what is recorded
18205 in the index file). This will later be needed to write the address
18206 table. */
18207 cu_index_htab = htab_create_alloc (100,
18208 hash_psymtab_cu_index,
18209 eq_psymtab_cu_index,
18210 NULL, xcalloc, xfree);
96408a79 18211 make_cleanup_htab_delete (cu_index_htab);
0a5429f6
DE
18212 psymtab_cu_index_map = (struct psymtab_cu_index_map *)
18213 xmalloc (sizeof (struct psymtab_cu_index_map)
18214 * dwarf2_per_objfile->n_comp_units);
18215 make_cleanup (xfree, psymtab_cu_index_map);
18216
18217 /* The CU list is already sorted, so we don't need to do additional
1fd400ff
TT
18218 work here. Also, the debug_types entries do not appear in
18219 all_comp_units, but only in their own hash table. */
9291a0cd
TT
18220 for (i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
18221 {
3e43a32a
MS
18222 struct dwarf2_per_cu_data *per_cu
18223 = dwarf2_per_objfile->all_comp_units[i];
e254ef6a 18224 struct partial_symtab *psymtab = per_cu->v.psymtab;
9291a0cd 18225 gdb_byte val[8];
0a5429f6
DE
18226 struct psymtab_cu_index_map *map;
18227 void **slot;
9291a0cd 18228
95554aad
TT
18229 if (psymtab->user == NULL)
18230 recursively_write_psymbols (objfile, psymtab, symtab, psyms_seen, i);
9291a0cd 18231
0a5429f6
DE
18232 map = &psymtab_cu_index_map[i];
18233 map->psymtab = psymtab;
18234 map->cu_index = i;
18235 slot = htab_find_slot (cu_index_htab, map, INSERT);
18236 gdb_assert (slot != NULL);
18237 gdb_assert (*slot == NULL);
18238 *slot = map;
9291a0cd 18239
b64f50a1
JK
18240 store_unsigned_integer (val, 8, BFD_ENDIAN_LITTLE,
18241 per_cu->offset.sect_off);
9291a0cd 18242 obstack_grow (&cu_list, val, 8);
e254ef6a 18243 store_unsigned_integer (val, 8, BFD_ENDIAN_LITTLE, per_cu->length);
9291a0cd
TT
18244 obstack_grow (&cu_list, val, 8);
18245 }
18246
0a5429f6
DE
18247 /* Dump the address map. */
18248 write_address_map (objfile, &addr_obstack, cu_index_htab);
18249
1fd400ff
TT
18250 /* Write out the .debug_type entries, if any. */
18251 if (dwarf2_per_objfile->signatured_types)
18252 {
18253 struct signatured_type_index_data sig_data;
18254
18255 sig_data.objfile = objfile;
18256 sig_data.symtab = symtab;
18257 sig_data.types_list = &types_cu_list;
987d643c 18258 sig_data.psyms_seen = psyms_seen;
1fd400ff
TT
18259 sig_data.cu_index = dwarf2_per_objfile->n_comp_units;
18260 htab_traverse_noresize (dwarf2_per_objfile->signatured_types,
18261 write_one_signatured_type, &sig_data);
18262 }
18263
156942c7
DE
18264 /* Now that we've processed all symbols we can shrink their cu_indices
18265 lists. */
18266 uniquify_cu_indices (symtab);
18267
9291a0cd
TT
18268 obstack_init (&constant_pool);
18269 make_cleanup_obstack_free (&constant_pool);
18270 obstack_init (&symtab_obstack);
18271 make_cleanup_obstack_free (&symtab_obstack);
18272 write_hash_table (symtab, &symtab_obstack, &constant_pool);
18273
18274 obstack_init (&contents);
18275 make_cleanup_obstack_free (&contents);
1fd400ff 18276 size_of_contents = 6 * sizeof (offset_type);
9291a0cd
TT
18277 total_len = size_of_contents;
18278
18279 /* The version number. */
156942c7 18280 val = MAYBE_SWAP (7);
9291a0cd
TT
18281 obstack_grow (&contents, &val, sizeof (val));
18282
18283 /* The offset of the CU list from the start of the file. */
18284 val = MAYBE_SWAP (total_len);
18285 obstack_grow (&contents, &val, sizeof (val));
18286 total_len += obstack_object_size (&cu_list);
18287
1fd400ff
TT
18288 /* The offset of the types CU list from the start of the file. */
18289 val = MAYBE_SWAP (total_len);
18290 obstack_grow (&contents, &val, sizeof (val));
18291 total_len += obstack_object_size (&types_cu_list);
18292
9291a0cd
TT
18293 /* The offset of the address table from the start of the file. */
18294 val = MAYBE_SWAP (total_len);
18295 obstack_grow (&contents, &val, sizeof (val));
18296 total_len += obstack_object_size (&addr_obstack);
18297
18298 /* The offset of the symbol table from the start of the file. */
18299 val = MAYBE_SWAP (total_len);
18300 obstack_grow (&contents, &val, sizeof (val));
18301 total_len += obstack_object_size (&symtab_obstack);
18302
18303 /* The offset of the constant pool from the start of the file. */
18304 val = MAYBE_SWAP (total_len);
18305 obstack_grow (&contents, &val, sizeof (val));
18306 total_len += obstack_object_size (&constant_pool);
18307
18308 gdb_assert (obstack_object_size (&contents) == size_of_contents);
18309
18310 write_obstack (out_file, &contents);
18311 write_obstack (out_file, &cu_list);
1fd400ff 18312 write_obstack (out_file, &types_cu_list);
9291a0cd
TT
18313 write_obstack (out_file, &addr_obstack);
18314 write_obstack (out_file, &symtab_obstack);
18315 write_obstack (out_file, &constant_pool);
18316
18317 fclose (out_file);
18318
18319 /* We want to keep the file, so we set cleanup_filename to NULL
18320 here. See unlink_if_set. */
18321 cleanup_filename = NULL;
18322
18323 do_cleanups (cleanup);
18324}
18325
90476074
TT
18326/* Implementation of the `save gdb-index' command.
18327
18328 Note that the file format used by this command is documented in the
18329 GDB manual. Any changes here must be documented there. */
11570e71 18330
9291a0cd
TT
18331static void
18332save_gdb_index_command (char *arg, int from_tty)
18333{
18334 struct objfile *objfile;
18335
18336 if (!arg || !*arg)
96d19272 18337 error (_("usage: save gdb-index DIRECTORY"));
9291a0cd
TT
18338
18339 ALL_OBJFILES (objfile)
18340 {
18341 struct stat st;
18342
18343 /* If the objfile does not correspond to an actual file, skip it. */
18344 if (stat (objfile->name, &st) < 0)
18345 continue;
18346
18347 dwarf2_per_objfile = objfile_data (objfile, dwarf2_objfile_data_key);
18348 if (dwarf2_per_objfile)
18349 {
18350 volatile struct gdb_exception except;
18351
18352 TRY_CATCH (except, RETURN_MASK_ERROR)
18353 {
18354 write_psymtabs_to_index (objfile, arg);
18355 }
18356 if (except.reason < 0)
18357 exception_fprintf (gdb_stderr, except,
18358 _("Error while writing index for `%s': "),
18359 objfile->name);
18360 }
18361 }
dce234bc
PP
18362}
18363
9291a0cd
TT
18364\f
18365
9eae7c52
TT
18366int dwarf2_always_disassemble;
18367
18368static void
18369show_dwarf2_always_disassemble (struct ui_file *file, int from_tty,
18370 struct cmd_list_element *c, const char *value)
18371{
3e43a32a
MS
18372 fprintf_filtered (file,
18373 _("Whether to always disassemble "
18374 "DWARF expressions is %s.\n"),
9eae7c52
TT
18375 value);
18376}
18377
900e11f9
JK
18378static void
18379show_check_physname (struct ui_file *file, int from_tty,
18380 struct cmd_list_element *c, const char *value)
18381{
18382 fprintf_filtered (file,
18383 _("Whether to check \"physname\" is %s.\n"),
18384 value);
18385}
18386
6502dd73
DJ
18387void _initialize_dwarf2_read (void);
18388
18389void
18390_initialize_dwarf2_read (void)
18391{
96d19272
JK
18392 struct cmd_list_element *c;
18393
dce234bc 18394 dwarf2_objfile_data_key
c1bd65d0 18395 = register_objfile_data_with_cleanup (NULL, dwarf2_per_objfile_free);
ae038cb0 18396
1bedd215
AC
18397 add_prefix_cmd ("dwarf2", class_maintenance, set_dwarf2_cmd, _("\
18398Set DWARF 2 specific variables.\n\
18399Configure DWARF 2 variables such as the cache size"),
ae038cb0
DJ
18400 &set_dwarf2_cmdlist, "maintenance set dwarf2 ",
18401 0/*allow-unknown*/, &maintenance_set_cmdlist);
18402
1bedd215
AC
18403 add_prefix_cmd ("dwarf2", class_maintenance, show_dwarf2_cmd, _("\
18404Show DWARF 2 specific variables\n\
18405Show DWARF 2 variables such as the cache size"),
ae038cb0
DJ
18406 &show_dwarf2_cmdlist, "maintenance show dwarf2 ",
18407 0/*allow-unknown*/, &maintenance_show_cmdlist);
18408
18409 add_setshow_zinteger_cmd ("max-cache-age", class_obscure,
7915a72c
AC
18410 &dwarf2_max_cache_age, _("\
18411Set the upper bound on the age of cached dwarf2 compilation units."), _("\
18412Show the upper bound on the age of cached dwarf2 compilation units."), _("\
18413A higher limit means that cached compilation units will be stored\n\
18414in memory longer, and more total memory will be used. Zero disables\n\
18415caching, which can slow down startup."),
2c5b56ce 18416 NULL,
920d2a44 18417 show_dwarf2_max_cache_age,
2c5b56ce 18418 &set_dwarf2_cmdlist,
ae038cb0 18419 &show_dwarf2_cmdlist);
d97bc12b 18420
9eae7c52
TT
18421 add_setshow_boolean_cmd ("always-disassemble", class_obscure,
18422 &dwarf2_always_disassemble, _("\
18423Set whether `info address' always disassembles DWARF expressions."), _("\
18424Show whether `info address' always disassembles DWARF expressions."), _("\
18425When enabled, DWARF expressions are always printed in an assembly-like\n\
18426syntax. When disabled, expressions will be printed in a more\n\
18427conversational style, when possible."),
18428 NULL,
18429 show_dwarf2_always_disassemble,
18430 &set_dwarf2_cmdlist,
18431 &show_dwarf2_cmdlist);
18432
45cfd468
DE
18433 add_setshow_boolean_cmd ("dwarf2-read", no_class, &dwarf2_read_debug, _("\
18434Set debugging of the dwarf2 reader."), _("\
18435Show debugging of the dwarf2 reader."), _("\
18436When enabled, debugging messages are printed during dwarf2 reading\n\
18437and symtab expansion."),
18438 NULL,
18439 NULL,
18440 &setdebuglist, &showdebuglist);
18441
d97bc12b
DE
18442 add_setshow_zinteger_cmd ("dwarf2-die", no_class, &dwarf2_die_debug, _("\
18443Set debugging of the dwarf2 DIE reader."), _("\
18444Show debugging of the dwarf2 DIE reader."), _("\
18445When enabled (non-zero), DIEs are dumped after they are read in.\n\
18446The value is the maximum depth to print."),
18447 NULL,
18448 NULL,
18449 &setdebuglist, &showdebuglist);
9291a0cd 18450
900e11f9
JK
18451 add_setshow_boolean_cmd ("check-physname", no_class, &check_physname, _("\
18452Set cross-checking of \"physname\" code against demangler."), _("\
18453Show cross-checking of \"physname\" code against demangler."), _("\
18454When enabled, GDB's internal \"physname\" code is checked against\n\
18455the demangler."),
18456 NULL, show_check_physname,
18457 &setdebuglist, &showdebuglist);
18458
96d19272 18459 c = add_cmd ("gdb-index", class_files, save_gdb_index_command,
11570e71 18460 _("\
fc1a9d6e 18461Save a gdb-index file.\n\
11570e71 18462Usage: save gdb-index DIRECTORY"),
96d19272
JK
18463 &save_cmdlist);
18464 set_cmd_completer (c, filename_completer);
6502dd73 18465}
This page took 2.418616 seconds and 4 git commands to generate.