*** empty log message ***
[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 214 /* The .debug_types-related CUs (TUs). */
b4dd5633 215 struct signatured_type **all_type_units;
1fd400ff 216
ae038cb0
DJ
217 /* A chain of compilation units that are currently read in, so that
218 they can be freed later. */
219 struct dwarf2_per_cu_data *read_in_chain;
72dca2f5 220
348e048f
DE
221 /* A table mapping .debug_types signatures to its signatured_type entry.
222 This is NULL if the .debug_types section hasn't been read in yet. */
223 htab_t signatured_types;
224
3019eac3
DE
225 /* A table mapping DW_AT_dwo_name values to struct dwo_file objects.
226 This is NULL if the table hasn't been allocated yet. */
227 htab_t dwo_files;
228
72dca2f5
FR
229 /* A flag indicating wether this objfile has a section loaded at a
230 VMA of 0. */
231 int has_section_at_zero;
9291a0cd 232
ae2de4f8
DE
233 /* True if we are using the mapped index,
234 or we are faking it for OBJF_READNOW's sake. */
9291a0cd
TT
235 unsigned char using_index;
236
ae2de4f8 237 /* The mapped index, or NULL if .gdb_index is missing or not being used. */
9291a0cd 238 struct mapped_index *index_table;
98bfdba5 239
7b9f3c50
DE
240 /* When using index_table, this keeps track of all quick_file_names entries.
241 TUs can share line table entries with CUs or other TUs, and there can be
242 a lot more TUs than unique line tables, so we maintain a separate table
243 of all line table entries to support the sharing. */
244 htab_t quick_file_names_table;
245
98bfdba5
PA
246 /* Set during partial symbol reading, to prevent queueing of full
247 symbols. */
248 int reading_partial_symbols;
673bfd45 249
dee91e82 250 /* Table mapping type DIEs to their struct type *.
673bfd45 251 This is NULL if not allocated yet.
dee91e82
DE
252 The mapping is done via (CU/TU signature + DIE offset) -> type. */
253 htab_t die_type_hash;
95554aad
TT
254
255 /* The CUs we recently read. */
256 VEC (dwarf2_per_cu_ptr) *just_read_cus;
6502dd73
DJ
257};
258
259static struct dwarf2_per_objfile *dwarf2_per_objfile;
c906108c 260
251d32d9 261/* Default names of the debugging sections. */
c906108c 262
233a11ab
CS
263/* Note that if the debugging section has been compressed, it might
264 have a name like .zdebug_info. */
265
9cdd5dbd
DE
266static const struct dwarf2_debug_sections dwarf2_elf_names =
267{
251d32d9
TG
268 { ".debug_info", ".zdebug_info" },
269 { ".debug_abbrev", ".zdebug_abbrev" },
270 { ".debug_line", ".zdebug_line" },
271 { ".debug_loc", ".zdebug_loc" },
272 { ".debug_macinfo", ".zdebug_macinfo" },
cf2c3c16 273 { ".debug_macro", ".zdebug_macro" },
251d32d9
TG
274 { ".debug_str", ".zdebug_str" },
275 { ".debug_ranges", ".zdebug_ranges" },
276 { ".debug_types", ".zdebug_types" },
3019eac3 277 { ".debug_addr", ".zdebug_addr" },
251d32d9
TG
278 { ".debug_frame", ".zdebug_frame" },
279 { ".eh_frame", NULL },
24d3216f
TT
280 { ".gdb_index", ".zgdb_index" },
281 23
251d32d9 282};
c906108c 283
3019eac3
DE
284/* List of DWO sections. */
285
286static const struct dwo_section_names
287{
288 struct dwarf2_section_names abbrev_dwo;
289 struct dwarf2_section_names info_dwo;
290 struct dwarf2_section_names line_dwo;
291 struct dwarf2_section_names loc_dwo;
09262596
DE
292 struct dwarf2_section_names macinfo_dwo;
293 struct dwarf2_section_names macro_dwo;
3019eac3
DE
294 struct dwarf2_section_names str_dwo;
295 struct dwarf2_section_names str_offsets_dwo;
296 struct dwarf2_section_names types_dwo;
297}
298dwo_section_names =
299{
300 { ".debug_abbrev.dwo", ".zdebug_abbrev.dwo" },
301 { ".debug_info.dwo", ".zdebug_info.dwo" },
302 { ".debug_line.dwo", ".zdebug_line.dwo" },
303 { ".debug_loc.dwo", ".zdebug_loc.dwo" },
09262596
DE
304 { ".debug_macinfo.dwo", ".zdebug_macinfo.dwo" },
305 { ".debug_macro.dwo", ".zdebug_macro.dwo" },
3019eac3
DE
306 { ".debug_str.dwo", ".zdebug_str.dwo" },
307 { ".debug_str_offsets.dwo", ".zdebug_str_offsets.dwo" },
308 { ".debug_types.dwo", ".zdebug_types.dwo" },
309};
310
c906108c
SS
311/* local data types */
312
107d2387
AC
313/* The data in a compilation unit header, after target2host
314 translation, looks like this. */
c906108c 315struct comp_unit_head
a738430d 316{
c764a876 317 unsigned int length;
a738430d 318 short version;
a738430d
MK
319 unsigned char addr_size;
320 unsigned char signed_addr_p;
b64f50a1 321 sect_offset abbrev_offset;
57349743 322
a738430d
MK
323 /* Size of file offsets; either 4 or 8. */
324 unsigned int offset_size;
57349743 325
a738430d
MK
326 /* Size of the length field; either 4 or 12. */
327 unsigned int initial_length_size;
57349743 328
a738430d
MK
329 /* Offset to the first byte of this compilation unit header in the
330 .debug_info section, for resolving relative reference dies. */
b64f50a1 331 sect_offset offset;
57349743 332
d00adf39
DE
333 /* Offset to first die in this cu from the start of the cu.
334 This will be the first byte following the compilation unit header. */
b64f50a1 335 cu_offset first_die_offset;
a738430d 336};
c906108c 337
3da10d80
KS
338/* Type used for delaying computation of method physnames.
339 See comments for compute_delayed_physnames. */
340struct delayed_method_info
341{
342 /* The type to which the method is attached, i.e., its parent class. */
343 struct type *type;
344
345 /* The index of the method in the type's function fieldlists. */
346 int fnfield_index;
347
348 /* The index of the method in the fieldlist. */
349 int index;
350
351 /* The name of the DIE. */
352 const char *name;
353
354 /* The DIE associated with this method. */
355 struct die_info *die;
356};
357
358typedef struct delayed_method_info delayed_method_info;
359DEF_VEC_O (delayed_method_info);
360
e7c27a73
DJ
361/* Internal state when decoding a particular compilation unit. */
362struct dwarf2_cu
363{
364 /* The objfile containing this compilation unit. */
365 struct objfile *objfile;
366
d00adf39 367 /* The header of the compilation unit. */
e7c27a73 368 struct comp_unit_head header;
e142c38c 369
d00adf39
DE
370 /* Base address of this compilation unit. */
371 CORE_ADDR base_address;
372
373 /* Non-zero if base_address has been set. */
374 int base_known;
375
e142c38c
DJ
376 /* The language we are debugging. */
377 enum language language;
378 const struct language_defn *language_defn;
379
b0f35d58
DL
380 const char *producer;
381
e142c38c
DJ
382 /* The generic symbol table building routines have separate lists for
383 file scope symbols and all all other scopes (local scopes). So
384 we need to select the right one to pass to add_symbol_to_list().
385 We do it by keeping a pointer to the correct list in list_in_scope.
386
387 FIXME: The original dwarf code just treated the file scope as the
388 first local scope, and all other local scopes as nested local
389 scopes, and worked fine. Check to see if we really need to
390 distinguish these in buildsym.c. */
391 struct pending **list_in_scope;
392
433df2d4
DE
393 /* The abbrev table for this CU.
394 Normally this points to the abbrev table in the objfile.
395 But if DWO_UNIT is non-NULL this is the abbrev table in the DWO file. */
396 struct abbrev_table *abbrev_table;
72bf9492 397
b64f50a1
JK
398 /* Hash table holding all the loaded partial DIEs
399 with partial_die->offset.SECT_OFF as hash. */
72bf9492
DJ
400 htab_t partial_dies;
401
402 /* Storage for things with the same lifetime as this read-in compilation
403 unit, including partial DIEs. */
404 struct obstack comp_unit_obstack;
405
ae038cb0
DJ
406 /* When multiple dwarf2_cu structures are living in memory, this field
407 chains them all together, so that they can be released efficiently.
408 We will probably also want a generation counter so that most-recently-used
409 compilation units are cached... */
410 struct dwarf2_per_cu_data *read_in_chain;
411
412 /* Backchain to our per_cu entry if the tree has been built. */
413 struct dwarf2_per_cu_data *per_cu;
414
415 /* How many compilation units ago was this CU last referenced? */
416 int last_used;
417
b64f50a1
JK
418 /* A hash table of DIE cu_offset for following references with
419 die_info->offset.sect_off as hash. */
51545339 420 htab_t die_hash;
10b3939b
DJ
421
422 /* Full DIEs if read in. */
423 struct die_info *dies;
424
425 /* A set of pointers to dwarf2_per_cu_data objects for compilation
426 units referenced by this one. Only set during full symbol processing;
427 partial symbol tables do not have dependencies. */
428 htab_t dependencies;
429
cb1df416
DJ
430 /* Header data from the line table, during full symbol processing. */
431 struct line_header *line_header;
432
3da10d80
KS
433 /* A list of methods which need to have physnames computed
434 after all type information has been read. */
435 VEC (delayed_method_info) *method_list;
436
96408a79
SA
437 /* To be copied to symtab->call_site_htab. */
438 htab_t call_site_htab;
439
034e5797
DE
440 /* Non-NULL if this CU came from a DWO file.
441 There is an invariant here that is important to remember:
442 Except for attributes copied from the top level DIE in the "main"
443 (or "stub") file in preparation for reading the DWO file
444 (e.g., DW_AT_GNU_addr_base), we KISS: there is only *one* CU.
445 Either there isn't a DWO file (in which case this is NULL and the point
446 is moot), or there is and either we're not going to read it (in which
447 case this is NULL) or there is and we are reading it (in which case this
448 is non-NULL). */
3019eac3
DE
449 struct dwo_unit *dwo_unit;
450
451 /* The DW_AT_addr_base attribute if present, zero otherwise
452 (zero is a valid value though).
453 Note this value comes from the stub CU/TU's DIE. */
454 ULONGEST addr_base;
455
2e3cf129
DE
456 /* The DW_AT_ranges_base attribute if present, zero otherwise
457 (zero is a valid value though).
458 Note this value comes from the stub CU/TU's DIE.
459 Also note that the value is zero in the non-DWO case so this value can
460 be used without needing to know whether DWO files are in use or not. */
461 ULONGEST ranges_base;
462
ae038cb0
DJ
463 /* Mark used when releasing cached dies. */
464 unsigned int mark : 1;
465
8be455d7
JK
466 /* This CU references .debug_loc. See the symtab->locations_valid field.
467 This test is imperfect as there may exist optimized debug code not using
468 any location list and still facing inlining issues if handled as
469 unoptimized code. For a future better test see GCC PR other/32998. */
8be455d7 470 unsigned int has_loclist : 1;
ba919b58 471
685b1105
JK
472 /* These cache the results for producer_is_gxx_lt_4_6 and producer_is_icc.
473 CHECKED_PRODUCER is set if both PRODUCER_IS_GXX_LT_4_6 and PRODUCER_IS_ICC
474 are valid. This information is cached because profiling CU expansion
475 showed excessive time spent in producer_is_gxx_lt_4_6. */
ba919b58
TT
476 unsigned int checked_producer : 1;
477 unsigned int producer_is_gxx_lt_4_6 : 1;
685b1105 478 unsigned int producer_is_icc : 1;
e7c27a73
DJ
479};
480
10b3939b
DJ
481/* Persistent data held for a compilation unit, even when not
482 processing it. We put a pointer to this structure in the
28dee7f5 483 read_symtab_private field of the psymtab. */
10b3939b 484
ae038cb0
DJ
485struct dwarf2_per_cu_data
486{
348e048f 487 /* The start offset and length of this compilation unit. 2**29-1
ae038cb0 488 bytes should suffice to store the length of any compilation unit
45452591
DE
489 - if it doesn't, GDB will fall over anyway.
490 NOTE: Unlike comp_unit_head.length, this length includes
3019eac3
DE
491 initial_length_size.
492 If the DIE refers to a DWO file, this is always of the original die,
493 not the DWO file. */
b64f50a1 494 sect_offset offset;
348e048f 495 unsigned int length : 29;
ae038cb0
DJ
496
497 /* Flag indicating this compilation unit will be read in before
498 any of the current compilation units are processed. */
c764a876 499 unsigned int queued : 1;
ae038cb0 500
0d99eb77
DE
501 /* This flag will be set when reading partial DIEs if we need to load
502 absolutely all DIEs for this compilation unit, instead of just the ones
503 we think are interesting. It gets set if we look for a DIE in the
5afb4e99
DJ
504 hash table and don't find it. */
505 unsigned int load_all_dies : 1;
506
3019eac3
DE
507 /* Non-zero if this CU is from .debug_types. */
508 unsigned int is_debug_types : 1;
509
510 /* The section this CU/TU lives in.
511 If the DIE refers to a DWO file, this is always the original die,
512 not the DWO file. */
513 struct dwarf2_section_info *info_or_types_section;
348e048f 514
17ea53c3
JK
515 /* Set to non-NULL iff this CU is currently loaded. When it gets freed out
516 of the CU cache it gets reset to NULL again. */
ae038cb0 517 struct dwarf2_cu *cu;
1c379e20 518
9cdd5dbd
DE
519 /* The corresponding objfile.
520 Normally we can get the objfile from dwarf2_per_objfile.
521 However we can enter this file with just a "per_cu" handle. */
9291a0cd
TT
522 struct objfile *objfile;
523
524 /* When using partial symbol tables, the 'psymtab' field is active.
525 Otherwise the 'quick' field is active. */
526 union
527 {
528 /* The partial symbol table associated with this compilation unit,
95554aad 529 or NULL for unread partial units. */
9291a0cd
TT
530 struct partial_symtab *psymtab;
531
532 /* Data needed by the "quick" functions. */
533 struct dwarf2_per_cu_quick_data *quick;
534 } v;
95554aad
TT
535
536 /* The CUs we import using DW_TAG_imported_unit. This is filled in
537 while reading psymtabs, used to compute the psymtab dependencies,
538 and then cleared. Then it is filled in again while reading full
539 symbols, and only deleted when the objfile is destroyed. */
540 VEC (dwarf2_per_cu_ptr) *imported_symtabs;
ae038cb0
DJ
541};
542
348e048f
DE
543/* Entry in the signatured_types hash table. */
544
545struct signatured_type
546{
42e7ad6c
DE
547 /* The "per_cu" object of this type.
548 N.B.: This is the first member so that it's easy to convert pointers
549 between them. */
550 struct dwarf2_per_cu_data per_cu;
551
3019eac3 552 /* The type's signature. */
348e048f
DE
553 ULONGEST signature;
554
3019eac3
DE
555 /* Offset in the TU of the type's DIE, as read from the TU header.
556 If the definition lives in a DWO file, this value is unusable. */
557 cu_offset type_offset_in_tu;
558
559 /* Offset in the section of the type's DIE.
560 If the definition lives in a DWO file, this is the offset in the
561 .debug_types.dwo section.
562 The value is zero until the actual value is known.
563 Zero is otherwise not a valid section offset. */
564 sect_offset type_offset_in_section;
348e048f
DE
565};
566
3019eac3
DE
567/* These sections are what may appear in a "dwo" file. */
568
569struct dwo_sections
570{
571 struct dwarf2_section_info abbrev;
572 struct dwarf2_section_info info;
573 struct dwarf2_section_info line;
574 struct dwarf2_section_info loc;
09262596
DE
575 struct dwarf2_section_info macinfo;
576 struct dwarf2_section_info macro;
3019eac3
DE
577 struct dwarf2_section_info str;
578 struct dwarf2_section_info str_offsets;
579 VEC (dwarf2_section_info_def) *types;
580};
581
582/* Common bits of DWO CUs/TUs. */
583
584struct dwo_unit
585{
586 /* Backlink to the containing struct dwo_file. */
587 struct dwo_file *dwo_file;
588
589 /* The "id" that distinguishes this CU/TU.
590 .debug_info calls this "dwo_id", .debug_types calls this "signature".
591 Since signatures came first, we stick with it for consistency. */
592 ULONGEST signature;
593
594 /* The section this CU/TU lives in, in the DWO file. */
595 struct dwarf2_section_info *info_or_types_section;
596
597 /* Same as dwarf2_per_cu_data:{offset,length} but for the DWO section. */
598 sect_offset offset;
599 unsigned int length;
600
601 /* For types, offset in the type's DIE of the type defined by this TU. */
602 cu_offset type_offset_in_tu;
603};
604
605/* Data for one DWO file. */
606
607struct dwo_file
608{
609 /* The DW_AT_GNU_dwo_name attribute.
610 We don't manage space for this, it's an attribute. */
611 const char *dwo_name;
612
613 /* The bfd, when the file is open. Otherwise this is NULL. */
614 bfd *dwo_bfd;
615
616 /* Section info for this file. */
617 struct dwo_sections sections;
618
619 /* Table of CUs in the file.
620 Each element is a struct dwo_unit. */
621 htab_t cus;
622
623 /* Table of TUs in the file.
624 Each element is a struct dwo_unit. */
625 htab_t tus;
626};
627
0963b4bd
MS
628/* Struct used to pass misc. parameters to read_die_and_children, et
629 al. which are used for both .debug_info and .debug_types dies.
630 All parameters here are unchanging for the life of the call. This
dee91e82 631 struct exists to abstract away the constant parameters of die reading. */
93311388
DE
632
633struct die_reader_specs
634{
dee91e82 635 /* die_section->asection->owner. */
93311388
DE
636 bfd* abfd;
637
638 /* The CU of the DIE we are parsing. */
639 struct dwarf2_cu *cu;
640
3019eac3
DE
641 /* Non-NULL if reading a DWO file. */
642 struct dwo_file *dwo_file;
643
dee91e82 644 /* The section the die comes from.
3019eac3 645 This is either .debug_info or .debug_types, or the .dwo variants. */
dee91e82
DE
646 struct dwarf2_section_info *die_section;
647
648 /* die_section->buffer. */
649 gdb_byte *buffer;
f664829e
DE
650
651 /* The end of the buffer. */
652 const gdb_byte *buffer_end;
93311388
DE
653};
654
fd820528 655/* Type of function passed to init_cutu_and_read_dies, et.al. */
dee91e82
DE
656typedef void (die_reader_func_ftype) (const struct die_reader_specs *reader,
657 gdb_byte *info_ptr,
658 struct die_info *comp_unit_die,
659 int has_children,
660 void *data);
661
debd256d
JB
662/* The line number information for a compilation unit (found in the
663 .debug_line section) begins with a "statement program header",
664 which contains the following information. */
665struct line_header
666{
667 unsigned int total_length;
668 unsigned short version;
669 unsigned int header_length;
670 unsigned char minimum_instruction_length;
2dc7f7b3 671 unsigned char maximum_ops_per_instruction;
debd256d
JB
672 unsigned char default_is_stmt;
673 int line_base;
674 unsigned char line_range;
675 unsigned char opcode_base;
676
677 /* standard_opcode_lengths[i] is the number of operands for the
678 standard opcode whose value is i. This means that
679 standard_opcode_lengths[0] is unused, and the last meaningful
680 element is standard_opcode_lengths[opcode_base - 1]. */
681 unsigned char *standard_opcode_lengths;
682
683 /* The include_directories table. NOTE! These strings are not
684 allocated with xmalloc; instead, they are pointers into
685 debug_line_buffer. If you try to free them, `free' will get
686 indigestion. */
687 unsigned int num_include_dirs, include_dirs_size;
688 char **include_dirs;
689
690 /* The file_names table. NOTE! These strings are not allocated
691 with xmalloc; instead, they are pointers into debug_line_buffer.
692 Don't try to free them directly. */
693 unsigned int num_file_names, file_names_size;
694 struct file_entry
c906108c 695 {
debd256d
JB
696 char *name;
697 unsigned int dir_index;
698 unsigned int mod_time;
699 unsigned int length;
aaa75496 700 int included_p; /* Non-zero if referenced by the Line Number Program. */
cb1df416 701 struct symtab *symtab; /* The associated symbol table, if any. */
debd256d
JB
702 } *file_names;
703
704 /* The start and end of the statement program following this
6502dd73 705 header. These point into dwarf2_per_objfile->line_buffer. */
fe1b8b76 706 gdb_byte *statement_program_start, *statement_program_end;
debd256d 707};
c906108c
SS
708
709/* When we construct a partial symbol table entry we only
0963b4bd 710 need this much information. */
c906108c
SS
711struct partial_die_info
712 {
72bf9492 713 /* Offset of this DIE. */
b64f50a1 714 sect_offset offset;
72bf9492
DJ
715
716 /* DWARF-2 tag for this DIE. */
717 ENUM_BITFIELD(dwarf_tag) tag : 16;
718
72bf9492
DJ
719 /* Assorted flags describing the data found in this DIE. */
720 unsigned int has_children : 1;
721 unsigned int is_external : 1;
722 unsigned int is_declaration : 1;
723 unsigned int has_type : 1;
724 unsigned int has_specification : 1;
725 unsigned int has_pc_info : 1;
481860b3 726 unsigned int may_be_inlined : 1;
72bf9492
DJ
727
728 /* Flag set if the SCOPE field of this structure has been
729 computed. */
730 unsigned int scope_set : 1;
731
fa4028e9
JB
732 /* Flag set if the DIE has a byte_size attribute. */
733 unsigned int has_byte_size : 1;
734
98bfdba5
PA
735 /* Flag set if any of the DIE's children are template arguments. */
736 unsigned int has_template_arguments : 1;
737
abc72ce4
DE
738 /* Flag set if fixup_partial_die has been called on this die. */
739 unsigned int fixup_called : 1;
740
72bf9492 741 /* The name of this DIE. Normally the value of DW_AT_name, but
94af9270 742 sometimes a default name for unnamed DIEs. */
c906108c 743 char *name;
72bf9492 744
abc72ce4
DE
745 /* The linkage name, if present. */
746 const char *linkage_name;
747
72bf9492
DJ
748 /* The scope to prepend to our children. This is generally
749 allocated on the comp_unit_obstack, so will disappear
750 when this compilation unit leaves the cache. */
751 char *scope;
752
95554aad
TT
753 /* Some data associated with the partial DIE. The tag determines
754 which field is live. */
755 union
756 {
757 /* The location description associated with this DIE, if any. */
758 struct dwarf_block *locdesc;
759 /* The offset of an import, for DW_TAG_imported_unit. */
760 sect_offset offset;
761 } d;
72bf9492
DJ
762
763 /* If HAS_PC_INFO, the PC range associated with this DIE. */
c906108c
SS
764 CORE_ADDR lowpc;
765 CORE_ADDR highpc;
72bf9492 766
93311388 767 /* Pointer into the info_buffer (or types_buffer) pointing at the target of
72bf9492 768 DW_AT_sibling, if any. */
abc72ce4
DE
769 /* NOTE: This member isn't strictly necessary, read_partial_die could
770 return DW_AT_sibling values to its caller load_partial_dies. */
fe1b8b76 771 gdb_byte *sibling;
72bf9492
DJ
772
773 /* If HAS_SPECIFICATION, the offset of the DIE referred to by
774 DW_AT_specification (or DW_AT_abstract_origin or
775 DW_AT_extension). */
b64f50a1 776 sect_offset spec_offset;
72bf9492
DJ
777
778 /* Pointers to this DIE's parent, first child, and next sibling,
779 if any. */
780 struct partial_die_info *die_parent, *die_child, *die_sibling;
c906108c
SS
781 };
782
0963b4bd 783/* This data structure holds the information of an abbrev. */
c906108c
SS
784struct abbrev_info
785 {
786 unsigned int number; /* number identifying abbrev */
787 enum dwarf_tag tag; /* dwarf tag */
f3dd6933
DJ
788 unsigned short has_children; /* boolean */
789 unsigned short num_attrs; /* number of attributes */
c906108c
SS
790 struct attr_abbrev *attrs; /* an array of attribute descriptions */
791 struct abbrev_info *next; /* next in chain */
792 };
793
794struct attr_abbrev
795 {
9d25dd43
DE
796 ENUM_BITFIELD(dwarf_attribute) name : 16;
797 ENUM_BITFIELD(dwarf_form) form : 16;
c906108c
SS
798 };
799
433df2d4
DE
800/* Size of abbrev_table.abbrev_hash_table. */
801#define ABBREV_HASH_SIZE 121
802
803/* Top level data structure to contain an abbreviation table. */
804
805struct abbrev_table
806{
807 /* Where the abbrev table came from. */
808 struct dwarf2_section_info *section;
809 sect_offset offset;
810
811 /* Storage for the abbrev table. */
812 struct obstack abbrev_obstack;
813
814 /* Hash table of abbrevs.
815 This is an array of size ABBREV_HASH_SIZE allocated in abbrev_obstack.
816 It could be statically allocated, but the previous code didn't so we
817 don't either. */
818 struct abbrev_info **abbrevs;
819};
820
0963b4bd 821/* Attributes have a name and a value. */
b60c80d6
DJ
822struct attribute
823 {
9d25dd43 824 ENUM_BITFIELD(dwarf_attribute) name : 16;
8285870a
JK
825 ENUM_BITFIELD(dwarf_form) form : 15;
826
827 /* Has DW_STRING already been updated by dwarf2_canonicalize_name? This
828 field should be in u.str (existing only for DW_STRING) but it is kept
829 here for better struct attribute alignment. */
830 unsigned int string_is_canonical : 1;
831
b60c80d6
DJ
832 union
833 {
834 char *str;
835 struct dwarf_block *blk;
43bbcdc2
PH
836 ULONGEST unsnd;
837 LONGEST snd;
b60c80d6 838 CORE_ADDR addr;
348e048f 839 struct signatured_type *signatured_type;
b60c80d6
DJ
840 }
841 u;
842 };
843
0963b4bd 844/* This data structure holds a complete die structure. */
c906108c
SS
845struct die_info
846 {
76815b17
DE
847 /* DWARF-2 tag for this DIE. */
848 ENUM_BITFIELD(dwarf_tag) tag : 16;
849
850 /* Number of attributes */
98bfdba5
PA
851 unsigned char num_attrs;
852
853 /* True if we're presently building the full type name for the
854 type derived from this DIE. */
855 unsigned char building_fullname : 1;
76815b17
DE
856
857 /* Abbrev number */
858 unsigned int abbrev;
859
93311388 860 /* Offset in .debug_info or .debug_types section. */
b64f50a1 861 sect_offset offset;
78ba4af6
JB
862
863 /* The dies in a compilation unit form an n-ary tree. PARENT
864 points to this die's parent; CHILD points to the first child of
865 this node; and all the children of a given node are chained
4950bc1c 866 together via their SIBLING fields. */
639d11d3
DC
867 struct die_info *child; /* Its first child, if any. */
868 struct die_info *sibling; /* Its next sibling, if any. */
869 struct die_info *parent; /* Its parent, if any. */
c906108c 870
b60c80d6
DJ
871 /* An array of attributes, with NUM_ATTRS elements. There may be
872 zero, but it's not common and zero-sized arrays are not
873 sufficiently portable C. */
874 struct attribute attrs[1];
c906108c
SS
875 };
876
0963b4bd 877/* Get at parts of an attribute structure. */
c906108c
SS
878
879#define DW_STRING(attr) ((attr)->u.str)
8285870a 880#define DW_STRING_IS_CANONICAL(attr) ((attr)->string_is_canonical)
c906108c
SS
881#define DW_UNSND(attr) ((attr)->u.unsnd)
882#define DW_BLOCK(attr) ((attr)->u.blk)
883#define DW_SND(attr) ((attr)->u.snd)
884#define DW_ADDR(attr) ((attr)->u.addr)
348e048f 885#define DW_SIGNATURED_TYPE(attr) ((attr)->u.signatured_type)
c906108c 886
0963b4bd 887/* Blocks are a bunch of untyped bytes. */
c906108c
SS
888struct dwarf_block
889 {
890 unsigned int size;
1d6edc3c
JK
891
892 /* Valid only if SIZE is not zero. */
fe1b8b76 893 gdb_byte *data;
c906108c
SS
894 };
895
c906108c
SS
896#ifndef ATTR_ALLOC_CHUNK
897#define ATTR_ALLOC_CHUNK 4
898#endif
899
c906108c
SS
900/* Allocate fields for structs, unions and enums in this size. */
901#ifndef DW_FIELD_ALLOC_CHUNK
902#define DW_FIELD_ALLOC_CHUNK 4
903#endif
904
c906108c
SS
905/* FIXME: We might want to set this from BFD via bfd_arch_bits_per_byte,
906 but this would require a corresponding change in unpack_field_as_long
907 and friends. */
908static int bits_per_byte = 8;
909
910/* The routines that read and process dies for a C struct or C++ class
911 pass lists of data member fields and lists of member function fields
912 in an instance of a field_info structure, as defined below. */
913struct field_info
c5aa993b 914 {
0963b4bd 915 /* List of data member and baseclasses fields. */
c5aa993b
JM
916 struct nextfield
917 {
918 struct nextfield *next;
919 int accessibility;
920 int virtuality;
921 struct field field;
922 }
7d0ccb61 923 *fields, *baseclasses;
c906108c 924
7d0ccb61 925 /* Number of fields (including baseclasses). */
c5aa993b 926 int nfields;
c906108c 927
c5aa993b
JM
928 /* Number of baseclasses. */
929 int nbaseclasses;
c906108c 930
c5aa993b
JM
931 /* Set if the accesibility of one of the fields is not public. */
932 int non_public_fields;
c906108c 933
c5aa993b
JM
934 /* Member function fields array, entries are allocated in the order they
935 are encountered in the object file. */
936 struct nextfnfield
937 {
938 struct nextfnfield *next;
939 struct fn_field fnfield;
940 }
941 *fnfields;
c906108c 942
c5aa993b
JM
943 /* Member function fieldlist array, contains name of possibly overloaded
944 member function, number of overloaded member functions and a pointer
945 to the head of the member function field chain. */
946 struct fnfieldlist
947 {
948 char *name;
949 int length;
950 struct nextfnfield *head;
951 }
952 *fnfieldlists;
c906108c 953
c5aa993b
JM
954 /* Number of entries in the fnfieldlists array. */
955 int nfnfields;
98751a41
JK
956
957 /* typedefs defined inside this class. TYPEDEF_FIELD_LIST contains head of
958 a NULL terminated list of TYPEDEF_FIELD_LIST_COUNT elements. */
959 struct typedef_field_list
960 {
961 struct typedef_field field;
962 struct typedef_field_list *next;
963 }
964 *typedef_field_list;
965 unsigned typedef_field_list_count;
c5aa993b 966 };
c906108c 967
10b3939b
DJ
968/* One item on the queue of compilation units to read in full symbols
969 for. */
970struct dwarf2_queue_item
971{
972 struct dwarf2_per_cu_data *per_cu;
95554aad 973 enum language pretend_language;
10b3939b
DJ
974 struct dwarf2_queue_item *next;
975};
976
977/* The current queue. */
978static struct dwarf2_queue_item *dwarf2_queue, *dwarf2_queue_tail;
979
ae038cb0
DJ
980/* Loaded secondary compilation units are kept in memory until they
981 have not been referenced for the processing of this many
982 compilation units. Set this to zero to disable caching. Cache
983 sizes of up to at least twenty will improve startup time for
984 typical inter-CU-reference binaries, at an obvious memory cost. */
985static int dwarf2_max_cache_age = 5;
920d2a44
AC
986static void
987show_dwarf2_max_cache_age (struct ui_file *file, int from_tty,
988 struct cmd_list_element *c, const char *value)
989{
3e43a32a
MS
990 fprintf_filtered (file, _("The upper bound on the age of cached "
991 "dwarf2 compilation units is %s.\n"),
920d2a44
AC
992 value);
993}
994
ae038cb0 995
0963b4bd 996/* Various complaints about symbol reading that don't abort the process. */
c906108c 997
4d3c2250
KB
998static void
999dwarf2_statement_list_fits_in_line_number_section_complaint (void)
2e276125 1000{
4d3c2250 1001 complaint (&symfile_complaints,
e2e0b3e5 1002 _("statement list doesn't fit in .debug_line section"));
4d3c2250
KB
1003}
1004
25e43795
DJ
1005static void
1006dwarf2_debug_line_missing_file_complaint (void)
1007{
1008 complaint (&symfile_complaints,
1009 _(".debug_line section has line data without a file"));
1010}
1011
59205f5a
JB
1012static void
1013dwarf2_debug_line_missing_end_sequence_complaint (void)
1014{
1015 complaint (&symfile_complaints,
3e43a32a
MS
1016 _(".debug_line section has line "
1017 "program sequence without an end"));
59205f5a
JB
1018}
1019
4d3c2250
KB
1020static void
1021dwarf2_complex_location_expr_complaint (void)
2e276125 1022{
e2e0b3e5 1023 complaint (&symfile_complaints, _("location expression too complex"));
4d3c2250
KB
1024}
1025
4d3c2250
KB
1026static void
1027dwarf2_const_value_length_mismatch_complaint (const char *arg1, int arg2,
1028 int arg3)
2e276125 1029{
4d3c2250 1030 complaint (&symfile_complaints,
3e43a32a
MS
1031 _("const value length mismatch for '%s', got %d, expected %d"),
1032 arg1, arg2, arg3);
4d3c2250
KB
1033}
1034
1035static void
f664829e 1036dwarf2_section_buffer_overflow_complaint (struct dwarf2_section_info *section)
2e276125 1037{
4d3c2250 1038 complaint (&symfile_complaints,
f664829e
DE
1039 _("debug info runs off end of %s section"
1040 " [in module %s]"),
1041 section->asection->name,
1042 bfd_get_filename (section->asection->owner));
4d3c2250
KB
1043}
1044
1045static void
1046dwarf2_macro_malformed_definition_complaint (const char *arg1)
8e19ed76 1047{
4d3c2250 1048 complaint (&symfile_complaints,
3e43a32a
MS
1049 _("macro debug info contains a "
1050 "malformed macro definition:\n`%s'"),
4d3c2250
KB
1051 arg1);
1052}
1053
1054static void
1055dwarf2_invalid_attrib_class_complaint (const char *arg1, const char *arg2)
8b2dbe47 1056{
4d3c2250 1057 complaint (&symfile_complaints,
3e43a32a
MS
1058 _("invalid attribute class or form for '%s' in '%s'"),
1059 arg1, arg2);
4d3c2250 1060}
c906108c 1061
c906108c
SS
1062/* local function prototypes */
1063
4efb68b1 1064static void dwarf2_locate_sections (bfd *, asection *, void *);
c906108c 1065
aaa75496
JB
1066static void dwarf2_create_include_psymtab (char *, struct partial_symtab *,
1067 struct objfile *);
1068
918dd910
JK
1069static void dwarf2_find_base_address (struct die_info *die,
1070 struct dwarf2_cu *cu);
1071
c67a9c90 1072static void dwarf2_build_psymtabs_hard (struct objfile *);
c906108c 1073
72bf9492
DJ
1074static void scan_partial_symbols (struct partial_die_info *,
1075 CORE_ADDR *, CORE_ADDR *,
5734ee8b 1076 int, struct dwarf2_cu *);
c906108c 1077
72bf9492
DJ
1078static void add_partial_symbol (struct partial_die_info *,
1079 struct dwarf2_cu *);
63d06c5c 1080
72bf9492
DJ
1081static void add_partial_namespace (struct partial_die_info *pdi,
1082 CORE_ADDR *lowpc, CORE_ADDR *highpc,
5734ee8b 1083 int need_pc, struct dwarf2_cu *cu);
63d06c5c 1084
5d7cb8df
JK
1085static void add_partial_module (struct partial_die_info *pdi, CORE_ADDR *lowpc,
1086 CORE_ADDR *highpc, int need_pc,
1087 struct dwarf2_cu *cu);
1088
72bf9492
DJ
1089static void add_partial_enumeration (struct partial_die_info *enum_pdi,
1090 struct dwarf2_cu *cu);
91c24f0a 1091
bc30ff58
JB
1092static void add_partial_subprogram (struct partial_die_info *pdi,
1093 CORE_ADDR *lowpc, CORE_ADDR *highpc,
5734ee8b 1094 int need_pc, struct dwarf2_cu *cu);
bc30ff58 1095
a14ed312 1096static void dwarf2_psymtab_to_symtab (struct partial_symtab *);
c906108c 1097
a14ed312 1098static void psymtab_to_symtab_1 (struct partial_symtab *);
c906108c 1099
433df2d4
DE
1100static struct abbrev_info *abbrev_table_lookup_abbrev
1101 (const struct abbrev_table *, unsigned int);
1102
1103static struct abbrev_table *abbrev_table_read_table
1104 (struct dwarf2_section_info *, sect_offset);
1105
1106static void abbrev_table_free (struct abbrev_table *);
1107
dee91e82
DE
1108static void dwarf2_read_abbrevs (struct dwarf2_cu *,
1109 struct dwarf2_section_info *);
c906108c 1110
f3dd6933 1111static void dwarf2_free_abbrev_table (void *);
c906108c 1112
6caca83c
CC
1113static unsigned int peek_abbrev_code (bfd *, gdb_byte *);
1114
dee91e82
DE
1115static struct partial_die_info *load_partial_dies
1116 (const struct die_reader_specs *, gdb_byte *, int);
72bf9492 1117
dee91e82
DE
1118static gdb_byte *read_partial_die (const struct die_reader_specs *,
1119 struct partial_die_info *,
1120 struct abbrev_info *,
1121 unsigned int,
1122 gdb_byte *);
c906108c 1123
b64f50a1 1124static struct partial_die_info *find_partial_die (sect_offset,
10b3939b 1125 struct dwarf2_cu *);
72bf9492
DJ
1126
1127static void fixup_partial_die (struct partial_die_info *,
1128 struct dwarf2_cu *);
1129
dee91e82
DE
1130static gdb_byte *read_attribute (const struct die_reader_specs *,
1131 struct attribute *, struct attr_abbrev *,
1132 gdb_byte *);
a8329558 1133
fe1b8b76 1134static unsigned int read_1_byte (bfd *, gdb_byte *);
c906108c 1135
fe1b8b76 1136static int read_1_signed_byte (bfd *, gdb_byte *);
c906108c 1137
fe1b8b76 1138static unsigned int read_2_bytes (bfd *, gdb_byte *);
c906108c 1139
fe1b8b76 1140static unsigned int read_4_bytes (bfd *, gdb_byte *);
c906108c 1141
93311388 1142static ULONGEST read_8_bytes (bfd *, gdb_byte *);
c906108c 1143
fe1b8b76 1144static CORE_ADDR read_address (bfd *, gdb_byte *ptr, struct dwarf2_cu *,
891d2f0b 1145 unsigned int *);
c906108c 1146
c764a876
DE
1147static LONGEST read_initial_length (bfd *, gdb_byte *, unsigned int *);
1148
1149static LONGEST read_checked_initial_length_and_offset
1150 (bfd *, gdb_byte *, const struct comp_unit_head *,
1151 unsigned int *, unsigned int *);
613e1657 1152
fe1b8b76 1153static LONGEST read_offset (bfd *, gdb_byte *, const struct comp_unit_head *,
c764a876
DE
1154 unsigned int *);
1155
1156static LONGEST read_offset_1 (bfd *, gdb_byte *, unsigned int);
613e1657 1157
fe1b8b76 1158static gdb_byte *read_n_bytes (bfd *, gdb_byte *, unsigned int);
c906108c 1159
9b1c24c8 1160static char *read_direct_string (bfd *, gdb_byte *, unsigned int *);
c906108c 1161
fe1b8b76
JB
1162static char *read_indirect_string (bfd *, gdb_byte *,
1163 const struct comp_unit_head *,
1164 unsigned int *);
4bdf3d34 1165
12df843f 1166static ULONGEST read_unsigned_leb128 (bfd *, gdb_byte *, unsigned int *);
c906108c 1167
12df843f 1168static LONGEST read_signed_leb128 (bfd *, gdb_byte *, unsigned int *);
c906108c 1169
3019eac3
DE
1170static CORE_ADDR read_addr_index_from_leb128 (struct dwarf2_cu *, gdb_byte *,
1171 unsigned int *);
1172
1173static char *read_str_index (const struct die_reader_specs *reader,
1174 struct dwarf2_cu *cu, ULONGEST str_index);
1175
e142c38c 1176static void set_cu_language (unsigned int, struct dwarf2_cu *);
c906108c 1177
e142c38c
DJ
1178static struct attribute *dwarf2_attr (struct die_info *, unsigned int,
1179 struct dwarf2_cu *);
c906108c 1180
348e048f 1181static struct attribute *dwarf2_attr_no_follow (struct die_info *,
45e58e77 1182 unsigned int);
348e048f 1183
05cf31d1
JB
1184static int dwarf2_flag_true_p (struct die_info *die, unsigned name,
1185 struct dwarf2_cu *cu);
1186
e142c38c 1187static int die_is_declaration (struct die_info *, struct dwarf2_cu *cu);
3ca72b44 1188
e142c38c 1189static struct die_info *die_specification (struct die_info *die,
f2f0e013 1190 struct dwarf2_cu **);
63d06c5c 1191
debd256d
JB
1192static void free_line_header (struct line_header *lh);
1193
aaa75496
JB
1194static void add_file_name (struct line_header *, char *, unsigned int,
1195 unsigned int, unsigned int);
1196
3019eac3
DE
1197static struct line_header *dwarf_decode_line_header (unsigned int offset,
1198 struct dwarf2_cu *cu);
debd256d 1199
f3f5162e
DE
1200static void dwarf_decode_lines (struct line_header *, const char *,
1201 struct dwarf2_cu *, struct partial_symtab *,
1202 int);
c906108c 1203
72b9f47f 1204static void dwarf2_start_subfile (char *, const char *, const char *);
c906108c 1205
a14ed312 1206static struct symbol *new_symbol (struct die_info *, struct type *,
e7c27a73 1207 struct dwarf2_cu *);
c906108c 1208
34eaf542
TT
1209static struct symbol *new_symbol_full (struct die_info *, struct type *,
1210 struct dwarf2_cu *, struct symbol *);
1211
a14ed312 1212static void dwarf2_const_value (struct attribute *, struct symbol *,
e7c27a73 1213 struct dwarf2_cu *);
c906108c 1214
98bfdba5
PA
1215static void dwarf2_const_value_attr (struct attribute *attr,
1216 struct type *type,
1217 const char *name,
1218 struct obstack *obstack,
12df843f 1219 struct dwarf2_cu *cu, LONGEST *value,
98bfdba5
PA
1220 gdb_byte **bytes,
1221 struct dwarf2_locexpr_baton **baton);
2df3850c 1222
e7c27a73 1223static struct type *die_type (struct die_info *, struct dwarf2_cu *);
c906108c 1224
b4ba55a1
JB
1225static int need_gnat_info (struct dwarf2_cu *);
1226
3e43a32a
MS
1227static struct type *die_descriptive_type (struct die_info *,
1228 struct dwarf2_cu *);
b4ba55a1
JB
1229
1230static void set_descriptive_type (struct type *, struct die_info *,
1231 struct dwarf2_cu *);
1232
e7c27a73
DJ
1233static struct type *die_containing_type (struct die_info *,
1234 struct dwarf2_cu *);
c906108c 1235
673bfd45
DE
1236static struct type *lookup_die_type (struct die_info *, struct attribute *,
1237 struct dwarf2_cu *);
c906108c 1238
f792889a 1239static struct type *read_type_die (struct die_info *, struct dwarf2_cu *);
c906108c 1240
673bfd45
DE
1241static struct type *read_type_die_1 (struct die_info *, struct dwarf2_cu *);
1242
0d5cff50 1243static const char *determine_prefix (struct die_info *die, struct dwarf2_cu *);
63d06c5c 1244
6e70227d 1245static char *typename_concat (struct obstack *obs, const char *prefix,
f55ee35c
JK
1246 const char *suffix, int physname,
1247 struct dwarf2_cu *cu);
63d06c5c 1248
e7c27a73 1249static void read_file_scope (struct die_info *, struct dwarf2_cu *);
c906108c 1250
348e048f
DE
1251static void read_type_unit_scope (struct die_info *, struct dwarf2_cu *);
1252
e7c27a73 1253static void read_func_scope (struct die_info *, struct dwarf2_cu *);
c906108c 1254
e7c27a73 1255static void read_lexical_block_scope (struct die_info *, struct dwarf2_cu *);
c906108c 1256
96408a79
SA
1257static void read_call_site_scope (struct die_info *die, struct dwarf2_cu *cu);
1258
ff013f42
JK
1259static int dwarf2_ranges_read (unsigned, CORE_ADDR *, CORE_ADDR *,
1260 struct dwarf2_cu *, struct partial_symtab *);
1261
a14ed312 1262static int dwarf2_get_pc_bounds (struct die_info *,
d85a05f0
DJ
1263 CORE_ADDR *, CORE_ADDR *, struct dwarf2_cu *,
1264 struct partial_symtab *);
c906108c 1265
fae299cd
DC
1266static void get_scope_pc_bounds (struct die_info *,
1267 CORE_ADDR *, CORE_ADDR *,
1268 struct dwarf2_cu *);
1269
801e3a5b
JB
1270static void dwarf2_record_block_ranges (struct die_info *, struct block *,
1271 CORE_ADDR, struct dwarf2_cu *);
1272
a14ed312 1273static void dwarf2_add_field (struct field_info *, struct die_info *,
e7c27a73 1274 struct dwarf2_cu *);
c906108c 1275
a14ed312 1276static void dwarf2_attach_fields_to_type (struct field_info *,
e7c27a73 1277 struct type *, struct dwarf2_cu *);
c906108c 1278
a14ed312 1279static void dwarf2_add_member_fn (struct field_info *,
e26fb1d7 1280 struct die_info *, struct type *,
e7c27a73 1281 struct dwarf2_cu *);
c906108c 1282
a14ed312 1283static void dwarf2_attach_fn_fields_to_type (struct field_info *,
3e43a32a
MS
1284 struct type *,
1285 struct dwarf2_cu *);
c906108c 1286
134d01f1 1287static void process_structure_scope (struct die_info *, struct dwarf2_cu *);
c906108c 1288
e7c27a73 1289static void read_common_block (struct die_info *, struct dwarf2_cu *);
c906108c 1290
e7c27a73 1291static void read_namespace (struct die_info *die, struct dwarf2_cu *);
d9fa45fe 1292
5d7cb8df
JK
1293static void read_module (struct die_info *die, struct dwarf2_cu *cu);
1294
27aa8d6a
SW
1295static void read_import_statement (struct die_info *die, struct dwarf2_cu *);
1296
f55ee35c
JK
1297static struct type *read_module_type (struct die_info *die,
1298 struct dwarf2_cu *cu);
1299
38d518c9 1300static const char *namespace_name (struct die_info *die,
e142c38c 1301 int *is_anonymous, struct dwarf2_cu *);
38d518c9 1302
134d01f1 1303static void process_enumeration_scope (struct die_info *, struct dwarf2_cu *);
c906108c 1304
e7c27a73 1305static CORE_ADDR decode_locdesc (struct dwarf_block *, struct dwarf2_cu *);
c906108c 1306
6e70227d 1307static enum dwarf_array_dim_ordering read_array_order (struct die_info *,
7ca2d3a3
DL
1308 struct dwarf2_cu *);
1309
dee91e82 1310static struct die_info *read_die_and_children (const struct die_reader_specs *,
93311388 1311 gdb_byte *info_ptr,
fe1b8b76 1312 gdb_byte **new_info_ptr,
639d11d3
DC
1313 struct die_info *parent);
1314
dee91e82 1315static struct die_info *read_die_and_siblings (const struct die_reader_specs *,
93311388 1316 gdb_byte *info_ptr,
fe1b8b76 1317 gdb_byte **new_info_ptr,
639d11d3
DC
1318 struct die_info *parent);
1319
3019eac3
DE
1320static gdb_byte *read_full_die_1 (const struct die_reader_specs *,
1321 struct die_info **, gdb_byte *, int *, int);
1322
dee91e82
DE
1323static gdb_byte *read_full_die (const struct die_reader_specs *,
1324 struct die_info **, gdb_byte *, int *);
93311388 1325
e7c27a73 1326static void process_die (struct die_info *, struct dwarf2_cu *);
c906108c 1327
71c25dea
TT
1328static char *dwarf2_canonicalize_name (char *, struct dwarf2_cu *,
1329 struct obstack *);
1330
e142c38c 1331static char *dwarf2_name (struct die_info *die, struct dwarf2_cu *);
9219021c 1332
98bfdba5
PA
1333static const char *dwarf2_full_name (char *name,
1334 struct die_info *die,
1335 struct dwarf2_cu *cu);
1336
e142c38c 1337static struct die_info *dwarf2_extension (struct die_info *die,
f2f0e013 1338 struct dwarf2_cu **);
9219021c 1339
f39c6ffd 1340static const char *dwarf_tag_name (unsigned int);
c906108c 1341
f39c6ffd 1342static const char *dwarf_attr_name (unsigned int);
c906108c 1343
f39c6ffd 1344static const char *dwarf_form_name (unsigned int);
c906108c 1345
a14ed312 1346static char *dwarf_bool_name (unsigned int);
c906108c 1347
f39c6ffd 1348static const char *dwarf_type_encoding_name (unsigned int);
c906108c 1349
f9aca02d 1350static struct die_info *sibling_die (struct die_info *);
c906108c 1351
d97bc12b
DE
1352static void dump_die_shallow (struct ui_file *, int indent, struct die_info *);
1353
1354static void dump_die_for_error (struct die_info *);
1355
1356static void dump_die_1 (struct ui_file *, int level, int max_level,
1357 struct die_info *);
c906108c 1358
d97bc12b 1359/*static*/ void dump_die (struct die_info *, int max_level);
c906108c 1360
51545339 1361static void store_in_ref_table (struct die_info *,
10b3939b 1362 struct dwarf2_cu *);
c906108c 1363
93311388
DE
1364static int is_ref_attr (struct attribute *);
1365
b64f50a1 1366static sect_offset dwarf2_get_ref_die_offset (struct attribute *);
c906108c 1367
43bbcdc2 1368static LONGEST dwarf2_get_attr_constant_value (struct attribute *, int);
a02abb62 1369
348e048f
DE
1370static struct die_info *follow_die_ref_or_sig (struct die_info *,
1371 struct attribute *,
1372 struct dwarf2_cu **);
1373
10b3939b
DJ
1374static struct die_info *follow_die_ref (struct die_info *,
1375 struct attribute *,
f2f0e013 1376 struct dwarf2_cu **);
c906108c 1377
348e048f
DE
1378static struct die_info *follow_die_sig (struct die_info *,
1379 struct attribute *,
1380 struct dwarf2_cu **);
1381
6c83ed52
TT
1382static struct signatured_type *lookup_signatured_type_at_offset
1383 (struct objfile *objfile,
b64f50a1 1384 struct dwarf2_section_info *section, sect_offset offset);
6c83ed52 1385
e5fe5e75 1386static void load_full_type_unit (struct dwarf2_per_cu_data *per_cu);
348e048f 1387
52dc124a 1388static void read_signatured_type (struct signatured_type *);
348e048f 1389
c906108c
SS
1390/* memory allocation interface */
1391
7b5a2f43 1392static struct dwarf_block *dwarf_alloc_block (struct dwarf2_cu *);
c906108c 1393
b60c80d6 1394static struct die_info *dwarf_alloc_die (struct dwarf2_cu *, int);
c906108c 1395
09262596
DE
1396static void dwarf_decode_macros (struct dwarf2_cu *, unsigned int,
1397 char *, int);
2e276125 1398
8e19ed76
PS
1399static int attr_form_is_block (struct attribute *);
1400
3690dd37
JB
1401static int attr_form_is_section_offset (struct attribute *);
1402
1403static int attr_form_is_constant (struct attribute *);
1404
8cf6f0b1
TT
1405static void fill_in_loclist_baton (struct dwarf2_cu *cu,
1406 struct dwarf2_loclist_baton *baton,
1407 struct attribute *attr);
1408
93e7bd98
DJ
1409static void dwarf2_symbol_mark_computed (struct attribute *attr,
1410 struct symbol *sym,
1411 struct dwarf2_cu *cu);
4c2df51b 1412
dee91e82
DE
1413static gdb_byte *skip_one_die (const struct die_reader_specs *reader,
1414 gdb_byte *info_ptr,
1415 struct abbrev_info *abbrev);
4bb7a0a7 1416
72bf9492
DJ
1417static void free_stack_comp_unit (void *);
1418
72bf9492
DJ
1419static hashval_t partial_die_hash (const void *item);
1420
1421static int partial_die_eq (const void *item_lhs, const void *item_rhs);
1422
ae038cb0 1423static struct dwarf2_per_cu_data *dwarf2_find_containing_comp_unit
b64f50a1 1424 (sect_offset offset, struct objfile *objfile);
ae038cb0 1425
9816fde3 1426static void init_one_comp_unit (struct dwarf2_cu *cu,
23745b47 1427 struct dwarf2_per_cu_data *per_cu);
9816fde3
JK
1428
1429static void prepare_one_comp_unit (struct dwarf2_cu *cu,
95554aad
TT
1430 struct die_info *comp_unit_die,
1431 enum language pretend_language);
93311388 1432
68dc6402 1433static void free_heap_comp_unit (void *);
ae038cb0
DJ
1434
1435static void free_cached_comp_units (void *);
1436
1437static void age_cached_comp_units (void);
1438
dee91e82 1439static void free_one_cached_comp_unit (struct dwarf2_per_cu_data *);
ae038cb0 1440
f792889a
DJ
1441static struct type *set_die_type (struct die_info *, struct type *,
1442 struct dwarf2_cu *);
1c379e20 1443
ae038cb0
DJ
1444static void create_all_comp_units (struct objfile *);
1445
0e50663e 1446static int create_all_type_units (struct objfile *);
1fd400ff 1447
95554aad
TT
1448static void load_full_comp_unit (struct dwarf2_per_cu_data *,
1449 enum language);
10b3939b 1450
95554aad
TT
1451static void process_full_comp_unit (struct dwarf2_per_cu_data *,
1452 enum language);
10b3939b
DJ
1453
1454static void dwarf2_add_dependence (struct dwarf2_cu *,
1455 struct dwarf2_per_cu_data *);
1456
ae038cb0
DJ
1457static void dwarf2_mark (struct dwarf2_cu *);
1458
1459static void dwarf2_clear_marks (struct dwarf2_per_cu_data *);
1460
b64f50a1 1461static struct type *get_die_type_at_offset (sect_offset,
673bfd45
DE
1462 struct dwarf2_per_cu_data *per_cu);
1463
f792889a 1464static struct type *get_die_type (struct die_info *die, struct dwarf2_cu *cu);
72019c9c 1465
9291a0cd
TT
1466static void dwarf2_release_queue (void *dummy);
1467
95554aad
TT
1468static void queue_comp_unit (struct dwarf2_per_cu_data *per_cu,
1469 enum language pretend_language);
1470
1471static int maybe_queue_comp_unit (struct dwarf2_cu *this_cu,
1472 struct dwarf2_per_cu_data *per_cu,
1473 enum language pretend_language);
9291a0cd 1474
a0f42c21 1475static void process_queue (void);
9291a0cd
TT
1476
1477static void find_file_and_directory (struct die_info *die,
1478 struct dwarf2_cu *cu,
1479 char **name, char **comp_dir);
1480
1481static char *file_full_name (int file, struct line_header *lh,
1482 const char *comp_dir);
1483
fd820528
DE
1484static void init_cutu_and_read_dies
1485 (struct dwarf2_per_cu_data *this_cu, int use_existing_cu, int keep,
3019eac3
DE
1486 die_reader_func_ftype *die_reader_func, void *data);
1487
dee91e82
DE
1488static void init_cutu_and_read_dies_simple
1489 (struct dwarf2_per_cu_data *this_cu,
1490 die_reader_func_ftype *die_reader_func, void *data);
9291a0cd 1491
673bfd45 1492static htab_t allocate_signatured_type_table (struct objfile *objfile);
1fd400ff 1493
95554aad 1494static void process_psymtab_comp_unit (struct dwarf2_per_cu_data *, int);
dee91e82 1495
3019eac3
DE
1496static htab_t allocate_dwo_unit_table (struct objfile *objfile);
1497
1498static struct dwo_unit *lookup_dwo_comp_unit
1499 (struct dwarf2_per_cu_data *, char *, const char *, ULONGEST);
1500
1501static struct dwo_unit *lookup_dwo_type_unit
1502 (struct signatured_type *, char *, const char *);
1503
1504static void free_dwo_file_cleanup (void *);
1505
1506static void munmap_section_buffer (struct dwarf2_section_info *);
1507
95554aad
TT
1508static void process_cu_includes (void);
1509
9291a0cd
TT
1510#if WORDS_BIGENDIAN
1511
1512/* Convert VALUE between big- and little-endian. */
1513static offset_type
1514byte_swap (offset_type value)
1515{
1516 offset_type result;
1517
1518 result = (value & 0xff) << 24;
1519 result |= (value & 0xff00) << 8;
1520 result |= (value & 0xff0000) >> 8;
1521 result |= (value & 0xff000000) >> 24;
1522 return result;
1523}
1524
1525#define MAYBE_SWAP(V) byte_swap (V)
1526
1527#else
1528#define MAYBE_SWAP(V) (V)
1529#endif /* WORDS_BIGENDIAN */
1530
1531/* The suffix for an index file. */
1532#define INDEX_SUFFIX ".gdb-index"
1533
3da10d80
KS
1534static const char *dwarf2_physname (char *name, struct die_info *die,
1535 struct dwarf2_cu *cu);
1536
c906108c 1537/* Try to locate the sections we need for DWARF 2 debugging
251d32d9
TG
1538 information and return true if we have enough to do something.
1539 NAMES points to the dwarf2 section names, or is NULL if the standard
1540 ELF names are used. */
c906108c
SS
1541
1542int
251d32d9
TG
1543dwarf2_has_info (struct objfile *objfile,
1544 const struct dwarf2_debug_sections *names)
c906108c 1545{
be391dca
TT
1546 dwarf2_per_objfile = objfile_data (objfile, dwarf2_objfile_data_key);
1547 if (!dwarf2_per_objfile)
1548 {
1549 /* Initialize per-objfile state. */
1550 struct dwarf2_per_objfile *data
1551 = obstack_alloc (&objfile->objfile_obstack, sizeof (*data));
9a619af0 1552
be391dca
TT
1553 memset (data, 0, sizeof (*data));
1554 set_objfile_data (objfile, dwarf2_objfile_data_key, data);
1555 dwarf2_per_objfile = data;
6502dd73 1556
251d32d9
TG
1557 bfd_map_over_sections (objfile->obfd, dwarf2_locate_sections,
1558 (void *) names);
be391dca
TT
1559 dwarf2_per_objfile->objfile = objfile;
1560 }
1561 return (dwarf2_per_objfile->info.asection != NULL
1562 && dwarf2_per_objfile->abbrev.asection != NULL);
c906108c
SS
1563}
1564
251d32d9
TG
1565/* When loading sections, we look either for uncompressed section or for
1566 compressed section names. */
233a11ab
CS
1567
1568static int
251d32d9
TG
1569section_is_p (const char *section_name,
1570 const struct dwarf2_section_names *names)
233a11ab 1571{
251d32d9
TG
1572 if (names->normal != NULL
1573 && strcmp (section_name, names->normal) == 0)
1574 return 1;
1575 if (names->compressed != NULL
1576 && strcmp (section_name, names->compressed) == 0)
1577 return 1;
1578 return 0;
233a11ab
CS
1579}
1580
c906108c
SS
1581/* This function is mapped across the sections and remembers the
1582 offset and size of each of the debugging sections we are interested
1583 in. */
1584
1585static void
251d32d9 1586dwarf2_locate_sections (bfd *abfd, asection *sectp, void *vnames)
c906108c 1587{
251d32d9
TG
1588 const struct dwarf2_debug_sections *names;
1589
1590 if (vnames == NULL)
1591 names = &dwarf2_elf_names;
1592 else
1593 names = (const struct dwarf2_debug_sections *) vnames;
1594
1595 if (section_is_p (sectp->name, &names->info))
c906108c 1596 {
dce234bc
PP
1597 dwarf2_per_objfile->info.asection = sectp;
1598 dwarf2_per_objfile->info.size = bfd_get_section_size (sectp);
c906108c 1599 }
251d32d9 1600 else if (section_is_p (sectp->name, &names->abbrev))
c906108c 1601 {
dce234bc
PP
1602 dwarf2_per_objfile->abbrev.asection = sectp;
1603 dwarf2_per_objfile->abbrev.size = bfd_get_section_size (sectp);
c906108c 1604 }
251d32d9 1605 else if (section_is_p (sectp->name, &names->line))
c906108c 1606 {
dce234bc
PP
1607 dwarf2_per_objfile->line.asection = sectp;
1608 dwarf2_per_objfile->line.size = bfd_get_section_size (sectp);
c906108c 1609 }
251d32d9 1610 else if (section_is_p (sectp->name, &names->loc))
c906108c 1611 {
dce234bc
PP
1612 dwarf2_per_objfile->loc.asection = sectp;
1613 dwarf2_per_objfile->loc.size = bfd_get_section_size (sectp);
c906108c 1614 }
251d32d9 1615 else if (section_is_p (sectp->name, &names->macinfo))
c906108c 1616 {
dce234bc
PP
1617 dwarf2_per_objfile->macinfo.asection = sectp;
1618 dwarf2_per_objfile->macinfo.size = bfd_get_section_size (sectp);
c906108c 1619 }
cf2c3c16
TT
1620 else if (section_is_p (sectp->name, &names->macro))
1621 {
1622 dwarf2_per_objfile->macro.asection = sectp;
1623 dwarf2_per_objfile->macro.size = bfd_get_section_size (sectp);
1624 }
251d32d9 1625 else if (section_is_p (sectp->name, &names->str))
c906108c 1626 {
dce234bc
PP
1627 dwarf2_per_objfile->str.asection = sectp;
1628 dwarf2_per_objfile->str.size = bfd_get_section_size (sectp);
c906108c 1629 }
3019eac3
DE
1630 else if (section_is_p (sectp->name, &names->addr))
1631 {
1632 dwarf2_per_objfile->addr.asection = sectp;
1633 dwarf2_per_objfile->addr.size = bfd_get_section_size (sectp);
1634 }
251d32d9 1635 else if (section_is_p (sectp->name, &names->frame))
b6af0555 1636 {
dce234bc
PP
1637 dwarf2_per_objfile->frame.asection = sectp;
1638 dwarf2_per_objfile->frame.size = bfd_get_section_size (sectp);
b6af0555 1639 }
251d32d9 1640 else if (section_is_p (sectp->name, &names->eh_frame))
b6af0555 1641 {
81b9b86e 1642 flagword aflag = bfd_get_section_flags (abfd, sectp);
9a619af0 1643
3799ccc6
EZ
1644 if (aflag & SEC_HAS_CONTENTS)
1645 {
dce234bc
PP
1646 dwarf2_per_objfile->eh_frame.asection = sectp;
1647 dwarf2_per_objfile->eh_frame.size = bfd_get_section_size (sectp);
3799ccc6 1648 }
b6af0555 1649 }
251d32d9 1650 else if (section_is_p (sectp->name, &names->ranges))
af34e669 1651 {
dce234bc
PP
1652 dwarf2_per_objfile->ranges.asection = sectp;
1653 dwarf2_per_objfile->ranges.size = bfd_get_section_size (sectp);
af34e669 1654 }
251d32d9 1655 else if (section_is_p (sectp->name, &names->types))
348e048f 1656 {
8b70b953
TT
1657 struct dwarf2_section_info type_section;
1658
1659 memset (&type_section, 0, sizeof (type_section));
1660 type_section.asection = sectp;
1661 type_section.size = bfd_get_section_size (sectp);
1662
1663 VEC_safe_push (dwarf2_section_info_def, dwarf2_per_objfile->types,
1664 &type_section);
348e048f 1665 }
251d32d9 1666 else if (section_is_p (sectp->name, &names->gdb_index))
9291a0cd
TT
1667 {
1668 dwarf2_per_objfile->gdb_index.asection = sectp;
1669 dwarf2_per_objfile->gdb_index.size = bfd_get_section_size (sectp);
1670 }
dce234bc 1671
72dca2f5
FR
1672 if ((bfd_get_section_flags (abfd, sectp) & SEC_LOAD)
1673 && bfd_section_vma (abfd, sectp) == 0)
1674 dwarf2_per_objfile->has_section_at_zero = 1;
c906108c
SS
1675}
1676
dce234bc
PP
1677/* Decompress a section that was compressed using zlib. Store the
1678 decompressed buffer, and its size, in OUTBUF and OUTSIZE. */
233a11ab
CS
1679
1680static void
dce234bc
PP
1681zlib_decompress_section (struct objfile *objfile, asection *sectp,
1682 gdb_byte **outbuf, bfd_size_type *outsize)
1683{
3019eac3 1684 bfd *abfd = sectp->owner;
dce234bc
PP
1685#ifndef HAVE_ZLIB_H
1686 error (_("Support for zlib-compressed DWARF data (from '%s') "
1687 "is disabled in this copy of GDB"),
1688 bfd_get_filename (abfd));
1689#else
1690 bfd_size_type compressed_size = bfd_get_section_size (sectp);
1691 gdb_byte *compressed_buffer = xmalloc (compressed_size);
affddf13 1692 struct cleanup *cleanup = make_cleanup (xfree, compressed_buffer);
dce234bc
PP
1693 bfd_size_type uncompressed_size;
1694 gdb_byte *uncompressed_buffer;
1695 z_stream strm;
1696 int rc;
1697 int header_size = 12;
1698
1699 if (bfd_seek (abfd, sectp->filepos, SEEK_SET) != 0
3e43a32a
MS
1700 || bfd_bread (compressed_buffer,
1701 compressed_size, abfd) != compressed_size)
dce234bc
PP
1702 error (_("Dwarf Error: Can't read DWARF data from '%s'"),
1703 bfd_get_filename (abfd));
1704
1705 /* Read the zlib header. In this case, it should be "ZLIB" followed
1706 by the uncompressed section size, 8 bytes in big-endian order. */
1707 if (compressed_size < header_size
1708 || strncmp (compressed_buffer, "ZLIB", 4) != 0)
1709 error (_("Dwarf Error: Corrupt DWARF ZLIB header from '%s'"),
1710 bfd_get_filename (abfd));
1711 uncompressed_size = compressed_buffer[4]; uncompressed_size <<= 8;
1712 uncompressed_size += compressed_buffer[5]; uncompressed_size <<= 8;
1713 uncompressed_size += compressed_buffer[6]; uncompressed_size <<= 8;
1714 uncompressed_size += compressed_buffer[7]; uncompressed_size <<= 8;
1715 uncompressed_size += compressed_buffer[8]; uncompressed_size <<= 8;
1716 uncompressed_size += compressed_buffer[9]; uncompressed_size <<= 8;
1717 uncompressed_size += compressed_buffer[10]; uncompressed_size <<= 8;
1718 uncompressed_size += compressed_buffer[11];
1719
1720 /* It is possible the section consists of several compressed
1721 buffers concatenated together, so we uncompress in a loop. */
1722 strm.zalloc = NULL;
1723 strm.zfree = NULL;
1724 strm.opaque = NULL;
1725 strm.avail_in = compressed_size - header_size;
1726 strm.next_in = (Bytef*) compressed_buffer + header_size;
1727 strm.avail_out = uncompressed_size;
1728 uncompressed_buffer = obstack_alloc (&objfile->objfile_obstack,
1729 uncompressed_size);
1730 rc = inflateInit (&strm);
1731 while (strm.avail_in > 0)
1732 {
1733 if (rc != Z_OK)
1734 error (_("Dwarf Error: setting up DWARF uncompression in '%s': %d"),
1735 bfd_get_filename (abfd), rc);
1736 strm.next_out = ((Bytef*) uncompressed_buffer
1737 + (uncompressed_size - strm.avail_out));
1738 rc = inflate (&strm, Z_FINISH);
1739 if (rc != Z_STREAM_END)
1740 error (_("Dwarf Error: zlib error uncompressing from '%s': %d"),
1741 bfd_get_filename (abfd), rc);
1742 rc = inflateReset (&strm);
1743 }
1744 rc = inflateEnd (&strm);
1745 if (rc != Z_OK
1746 || strm.avail_out != 0)
1747 error (_("Dwarf Error: concluding DWARF uncompression in '%s': %d"),
1748 bfd_get_filename (abfd), rc);
1749
affddf13 1750 do_cleanups (cleanup);
dce234bc
PP
1751 *outbuf = uncompressed_buffer;
1752 *outsize = uncompressed_size;
1753#endif
233a11ab
CS
1754}
1755
fceca515
DE
1756/* A helper function that decides whether a section is empty,
1757 or not present. */
9e0ac564
TT
1758
1759static int
1760dwarf2_section_empty_p (struct dwarf2_section_info *info)
1761{
1762 return info->asection == NULL || info->size == 0;
1763}
1764
3019eac3
DE
1765/* Read the contents of the section INFO.
1766 OBJFILE is the main object file, but not necessarily the file where
1767 the section comes from. E.g., for DWO files INFO->asection->owner
1768 is the bfd of the DWO file.
dce234bc 1769 If the section is compressed, uncompress it before returning. */
c906108c 1770
dce234bc
PP
1771static void
1772dwarf2_read_section (struct objfile *objfile, struct dwarf2_section_info *info)
c906108c 1773{
dce234bc 1774 asection *sectp = info->asection;
3019eac3 1775 bfd *abfd;
dce234bc
PP
1776 gdb_byte *buf, *retbuf;
1777 unsigned char header[4];
c906108c 1778
be391dca
TT
1779 if (info->readin)
1780 return;
dce234bc 1781 info->buffer = NULL;
b315ab21 1782 info->map_addr = NULL;
be391dca 1783 info->readin = 1;
188dd5d6 1784
9e0ac564 1785 if (dwarf2_section_empty_p (info))
dce234bc 1786 return;
c906108c 1787
3019eac3
DE
1788 /* Note that ABFD may not be from OBJFILE, e.g. a DWO section. */
1789 abfd = sectp->owner;
1790
dce234bc
PP
1791 /* Check if the file has a 4-byte header indicating compression. */
1792 if (info->size > sizeof (header)
1793 && bfd_seek (abfd, sectp->filepos, SEEK_SET) == 0
1794 && bfd_bread (header, sizeof (header), abfd) == sizeof (header))
1795 {
1796 /* Upon decompression, update the buffer and its size. */
1797 if (strncmp (header, "ZLIB", sizeof (header)) == 0)
1798 {
1799 zlib_decompress_section (objfile, sectp, &info->buffer,
1800 &info->size);
1801 return;
1802 }
1803 }
4bdf3d34 1804
dce234bc
PP
1805#ifdef HAVE_MMAP
1806 if (pagesize == 0)
1807 pagesize = getpagesize ();
2e276125 1808
dce234bc
PP
1809 /* Only try to mmap sections which are large enough: we don't want to
1810 waste space due to fragmentation. Also, only try mmap for sections
1811 without relocations. */
1812
1813 if (info->size > 4 * pagesize && (sectp->flags & SEC_RELOC) == 0)
1814 {
b315ab21
TG
1815 info->buffer = bfd_mmap (abfd, 0, info->size, PROT_READ,
1816 MAP_PRIVATE, sectp->filepos,
1817 &info->map_addr, &info->map_len);
dce234bc 1818
b315ab21 1819 if ((caddr_t)info->buffer != MAP_FAILED)
dce234bc 1820 {
be391dca 1821#if HAVE_POSIX_MADVISE
b315ab21 1822 posix_madvise (info->map_addr, info->map_len, POSIX_MADV_WILLNEED);
be391dca 1823#endif
dce234bc
PP
1824 return;
1825 }
1826 }
1827#endif
1828
1829 /* If we get here, we are a normal, not-compressed section. */
1830 info->buffer = buf
1831 = obstack_alloc (&objfile->objfile_obstack, info->size);
1832
1833 /* When debugging .o files, we may need to apply relocations; see
1834 http://sourceware.org/ml/gdb-patches/2002-04/msg00136.html .
1835 We never compress sections in .o files, so we only need to
1836 try this when the section is not compressed. */
ac8035ab 1837 retbuf = symfile_relocate_debug_section (objfile, sectp, buf);
dce234bc
PP
1838 if (retbuf != NULL)
1839 {
1840 info->buffer = retbuf;
1841 return;
1842 }
1843
1844 if (bfd_seek (abfd, sectp->filepos, SEEK_SET) != 0
1845 || bfd_bread (buf, info->size, abfd) != info->size)
1846 error (_("Dwarf Error: Can't read DWARF data from '%s'"),
1847 bfd_get_filename (abfd));
1848}
1849
9e0ac564
TT
1850/* A helper function that returns the size of a section in a safe way.
1851 If you are positive that the section has been read before using the
1852 size, then it is safe to refer to the dwarf2_section_info object's
1853 "size" field directly. In other cases, you must call this
1854 function, because for compressed sections the size field is not set
1855 correctly until the section has been read. */
1856
1857static bfd_size_type
1858dwarf2_section_size (struct objfile *objfile,
1859 struct dwarf2_section_info *info)
1860{
1861 if (!info->readin)
1862 dwarf2_read_section (objfile, info);
1863 return info->size;
1864}
1865
dce234bc 1866/* Fill in SECTP, BUFP and SIZEP with section info, given OBJFILE and
0963b4bd 1867 SECTION_NAME. */
af34e669 1868
dce234bc 1869void
3017a003
TG
1870dwarf2_get_section_info (struct objfile *objfile,
1871 enum dwarf2_section_enum sect,
dce234bc
PP
1872 asection **sectp, gdb_byte **bufp,
1873 bfd_size_type *sizep)
1874{
1875 struct dwarf2_per_objfile *data
1876 = objfile_data (objfile, dwarf2_objfile_data_key);
1877 struct dwarf2_section_info *info;
a3b2a86b
TT
1878
1879 /* We may see an objfile without any DWARF, in which case we just
1880 return nothing. */
1881 if (data == NULL)
1882 {
1883 *sectp = NULL;
1884 *bufp = NULL;
1885 *sizep = 0;
1886 return;
1887 }
3017a003
TG
1888 switch (sect)
1889 {
1890 case DWARF2_DEBUG_FRAME:
1891 info = &data->frame;
1892 break;
1893 case DWARF2_EH_FRAME:
1894 info = &data->eh_frame;
1895 break;
1896 default:
1897 gdb_assert_not_reached ("unexpected section");
1898 }
dce234bc 1899
9e0ac564 1900 dwarf2_read_section (objfile, info);
dce234bc
PP
1901
1902 *sectp = info->asection;
1903 *bufp = info->buffer;
1904 *sizep = info->size;
1905}
1906
9291a0cd 1907\f
7b9f3c50
DE
1908/* DWARF quick_symbols_functions support. */
1909
1910/* TUs can share .debug_line entries, and there can be a lot more TUs than
1911 unique line tables, so we maintain a separate table of all .debug_line
1912 derived entries to support the sharing.
1913 All the quick functions need is the list of file names. We discard the
1914 line_header when we're done and don't need to record it here. */
1915struct quick_file_names
1916{
1917 /* The offset in .debug_line of the line table. We hash on this. */
1918 unsigned int offset;
1919
1920 /* The number of entries in file_names, real_names. */
1921 unsigned int num_file_names;
1922
1923 /* The file names from the line table, after being run through
1924 file_full_name. */
1925 const char **file_names;
1926
1927 /* The file names from the line table after being run through
1928 gdb_realpath. These are computed lazily. */
1929 const char **real_names;
1930};
1931
1932/* When using the index (and thus not using psymtabs), each CU has an
1933 object of this type. This is used to hold information needed by
1934 the various "quick" methods. */
1935struct dwarf2_per_cu_quick_data
1936{
1937 /* The file table. This can be NULL if there was no file table
1938 or it's currently not read in.
1939 NOTE: This points into dwarf2_per_objfile->quick_file_names_table. */
1940 struct quick_file_names *file_names;
1941
1942 /* The corresponding symbol table. This is NULL if symbols for this
1943 CU have not yet been read. */
1944 struct symtab *symtab;
1945
1946 /* A temporary mark bit used when iterating over all CUs in
1947 expand_symtabs_matching. */
1948 unsigned int mark : 1;
1949
1950 /* True if we've tried to read the file table and found there isn't one.
1951 There will be no point in trying to read it again next time. */
1952 unsigned int no_file_data : 1;
1953};
1954
1955/* Hash function for a quick_file_names. */
1956
1957static hashval_t
1958hash_file_name_entry (const void *e)
1959{
1960 const struct quick_file_names *file_data = e;
1961
1962 return file_data->offset;
1963}
1964
1965/* Equality function for a quick_file_names. */
1966
1967static int
1968eq_file_name_entry (const void *a, const void *b)
1969{
1970 const struct quick_file_names *ea = a;
1971 const struct quick_file_names *eb = b;
1972
1973 return ea->offset == eb->offset;
1974}
1975
1976/* Delete function for a quick_file_names. */
1977
1978static void
1979delete_file_name_entry (void *e)
1980{
1981 struct quick_file_names *file_data = e;
1982 int i;
1983
1984 for (i = 0; i < file_data->num_file_names; ++i)
1985 {
1986 xfree ((void*) file_data->file_names[i]);
1987 if (file_data->real_names)
1988 xfree ((void*) file_data->real_names[i]);
1989 }
1990
1991 /* The space for the struct itself lives on objfile_obstack,
1992 so we don't free it here. */
1993}
1994
1995/* Create a quick_file_names hash table. */
1996
1997static htab_t
1998create_quick_file_names_table (unsigned int nr_initial_entries)
1999{
2000 return htab_create_alloc (nr_initial_entries,
2001 hash_file_name_entry, eq_file_name_entry,
2002 delete_file_name_entry, xcalloc, xfree);
2003}
9291a0cd 2004
918dd910
JK
2005/* Read in PER_CU->CU. This function is unrelated to symtabs, symtab would
2006 have to be created afterwards. You should call age_cached_comp_units after
2007 processing PER_CU->CU. dw2_setup must have been already called. */
2008
2009static void
2010load_cu (struct dwarf2_per_cu_data *per_cu)
2011{
3019eac3 2012 if (per_cu->is_debug_types)
e5fe5e75 2013 load_full_type_unit (per_cu);
918dd910 2014 else
95554aad 2015 load_full_comp_unit (per_cu, language_minimal);
918dd910 2016
918dd910 2017 gdb_assert (per_cu->cu != NULL);
2dc860c0
DE
2018
2019 dwarf2_find_base_address (per_cu->cu->dies, per_cu->cu);
918dd910
JK
2020}
2021
a0f42c21 2022/* Read in the symbols for PER_CU. */
2fdf6df6 2023
9291a0cd 2024static void
a0f42c21 2025dw2_do_instantiate_symtab (struct dwarf2_per_cu_data *per_cu)
9291a0cd
TT
2026{
2027 struct cleanup *back_to;
2028
2029 back_to = make_cleanup (dwarf2_release_queue, NULL);
2030
95554aad
TT
2031 if (dwarf2_per_objfile->using_index
2032 ? per_cu->v.quick->symtab == NULL
2033 : (per_cu->v.psymtab == NULL || !per_cu->v.psymtab->readin))
2034 {
2035 queue_comp_unit (per_cu, language_minimal);
2036 load_cu (per_cu);
2037 }
9291a0cd 2038
a0f42c21 2039 process_queue ();
9291a0cd
TT
2040
2041 /* Age the cache, releasing compilation units that have not
2042 been used recently. */
2043 age_cached_comp_units ();
2044
2045 do_cleanups (back_to);
2046}
2047
2048/* Ensure that the symbols for PER_CU have been read in. OBJFILE is
2049 the objfile from which this CU came. Returns the resulting symbol
2050 table. */
2fdf6df6 2051
9291a0cd 2052static struct symtab *
a0f42c21 2053dw2_instantiate_symtab (struct dwarf2_per_cu_data *per_cu)
9291a0cd 2054{
95554aad 2055 gdb_assert (dwarf2_per_objfile->using_index);
9291a0cd
TT
2056 if (!per_cu->v.quick->symtab)
2057 {
2058 struct cleanup *back_to = make_cleanup (free_cached_comp_units, NULL);
2059 increment_reading_symtab ();
a0f42c21 2060 dw2_do_instantiate_symtab (per_cu);
95554aad 2061 process_cu_includes ();
9291a0cd
TT
2062 do_cleanups (back_to);
2063 }
2064 return per_cu->v.quick->symtab;
2065}
2066
1fd400ff 2067/* Return the CU given its index. */
2fdf6df6 2068
1fd400ff
TT
2069static struct dwarf2_per_cu_data *
2070dw2_get_cu (int index)
2071{
2072 if (index >= dwarf2_per_objfile->n_comp_units)
2073 {
2074 index -= dwarf2_per_objfile->n_comp_units;
b4dd5633 2075 return &dwarf2_per_objfile->all_type_units[index]->per_cu;
1fd400ff
TT
2076 }
2077 return dwarf2_per_objfile->all_comp_units[index];
2078}
2079
9291a0cd
TT
2080/* A helper function that knows how to read a 64-bit value in a way
2081 that doesn't make gdb die. Returns 1 if the conversion went ok, 0
2082 otherwise. */
2fdf6df6 2083
9291a0cd
TT
2084static int
2085extract_cu_value (const char *bytes, ULONGEST *result)
2086{
2087 if (sizeof (ULONGEST) < 8)
2088 {
2089 int i;
2090
2091 /* Ignore the upper 4 bytes if they are all zero. */
2092 for (i = 0; i < 4; ++i)
2093 if (bytes[i + 4] != 0)
2094 return 0;
2095
2096 *result = extract_unsigned_integer (bytes, 4, BFD_ENDIAN_LITTLE);
2097 }
2098 else
2099 *result = extract_unsigned_integer (bytes, 8, BFD_ENDIAN_LITTLE);
2100 return 1;
2101}
2102
2103/* Read the CU list from the mapped index, and use it to create all
2104 the CU objects for this objfile. Return 0 if something went wrong,
2105 1 if everything went ok. */
2fdf6df6 2106
9291a0cd 2107static int
1fd400ff
TT
2108create_cus_from_index (struct objfile *objfile, const gdb_byte *cu_list,
2109 offset_type cu_list_elements)
9291a0cd
TT
2110{
2111 offset_type i;
9291a0cd
TT
2112
2113 dwarf2_per_objfile->n_comp_units = cu_list_elements / 2;
2114 dwarf2_per_objfile->all_comp_units
2115 = obstack_alloc (&objfile->objfile_obstack,
2116 dwarf2_per_objfile->n_comp_units
2117 * sizeof (struct dwarf2_per_cu_data *));
2118
2119 for (i = 0; i < cu_list_elements; i += 2)
2120 {
2121 struct dwarf2_per_cu_data *the_cu;
2122 ULONGEST offset, length;
2123
2124 if (!extract_cu_value (cu_list, &offset)
2125 || !extract_cu_value (cu_list + 8, &length))
2126 return 0;
2127 cu_list += 2 * 8;
2128
2129 the_cu = OBSTACK_ZALLOC (&objfile->objfile_obstack,
2130 struct dwarf2_per_cu_data);
b64f50a1 2131 the_cu->offset.sect_off = offset;
9291a0cd
TT
2132 the_cu->length = length;
2133 the_cu->objfile = objfile;
3019eac3 2134 the_cu->info_or_types_section = &dwarf2_per_objfile->info;
9291a0cd
TT
2135 the_cu->v.quick = OBSTACK_ZALLOC (&objfile->objfile_obstack,
2136 struct dwarf2_per_cu_quick_data);
2137 dwarf2_per_objfile->all_comp_units[i / 2] = the_cu;
2138 }
2139
2140 return 1;
2141}
2142
1fd400ff 2143/* Create the signatured type hash table from the index. */
673bfd45 2144
1fd400ff 2145static int
673bfd45 2146create_signatured_type_table_from_index (struct objfile *objfile,
8b70b953 2147 struct dwarf2_section_info *section,
673bfd45
DE
2148 const gdb_byte *bytes,
2149 offset_type elements)
1fd400ff
TT
2150{
2151 offset_type i;
673bfd45 2152 htab_t sig_types_hash;
1fd400ff 2153
d467dd73
DE
2154 dwarf2_per_objfile->n_type_units = elements / 3;
2155 dwarf2_per_objfile->all_type_units
1fd400ff 2156 = obstack_alloc (&objfile->objfile_obstack,
d467dd73 2157 dwarf2_per_objfile->n_type_units
b4dd5633 2158 * sizeof (struct signatured_type *));
1fd400ff 2159
673bfd45 2160 sig_types_hash = allocate_signatured_type_table (objfile);
1fd400ff
TT
2161
2162 for (i = 0; i < elements; i += 3)
2163 {
52dc124a
DE
2164 struct signatured_type *sig_type;
2165 ULONGEST offset, type_offset_in_tu, signature;
1fd400ff
TT
2166 void **slot;
2167
2168 if (!extract_cu_value (bytes, &offset)
52dc124a 2169 || !extract_cu_value (bytes + 8, &type_offset_in_tu))
1fd400ff
TT
2170 return 0;
2171 signature = extract_unsigned_integer (bytes + 16, 8, BFD_ENDIAN_LITTLE);
2172 bytes += 3 * 8;
2173
52dc124a 2174 sig_type = OBSTACK_ZALLOC (&objfile->objfile_obstack,
1fd400ff 2175 struct signatured_type);
52dc124a 2176 sig_type->signature = signature;
3019eac3
DE
2177 sig_type->type_offset_in_tu.cu_off = type_offset_in_tu;
2178 sig_type->per_cu.is_debug_types = 1;
2179 sig_type->per_cu.info_or_types_section = section;
52dc124a
DE
2180 sig_type->per_cu.offset.sect_off = offset;
2181 sig_type->per_cu.objfile = objfile;
2182 sig_type->per_cu.v.quick
1fd400ff
TT
2183 = OBSTACK_ZALLOC (&objfile->objfile_obstack,
2184 struct dwarf2_per_cu_quick_data);
2185
52dc124a
DE
2186 slot = htab_find_slot (sig_types_hash, sig_type, INSERT);
2187 *slot = sig_type;
1fd400ff 2188
b4dd5633 2189 dwarf2_per_objfile->all_type_units[i / 3] = sig_type;
1fd400ff
TT
2190 }
2191
673bfd45 2192 dwarf2_per_objfile->signatured_types = sig_types_hash;
1fd400ff
TT
2193
2194 return 1;
2195}
2196
9291a0cd
TT
2197/* Read the address map data from the mapped index, and use it to
2198 populate the objfile's psymtabs_addrmap. */
2fdf6df6 2199
9291a0cd
TT
2200static void
2201create_addrmap_from_index (struct objfile *objfile, struct mapped_index *index)
2202{
2203 const gdb_byte *iter, *end;
2204 struct obstack temp_obstack;
2205 struct addrmap *mutable_map;
2206 struct cleanup *cleanup;
2207 CORE_ADDR baseaddr;
2208
2209 obstack_init (&temp_obstack);
2210 cleanup = make_cleanup_obstack_free (&temp_obstack);
2211 mutable_map = addrmap_create_mutable (&temp_obstack);
2212
2213 iter = index->address_table;
2214 end = iter + index->address_table_size;
2215
2216 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
2217
2218 while (iter < end)
2219 {
2220 ULONGEST hi, lo, cu_index;
2221 lo = extract_unsigned_integer (iter, 8, BFD_ENDIAN_LITTLE);
2222 iter += 8;
2223 hi = extract_unsigned_integer (iter, 8, BFD_ENDIAN_LITTLE);
2224 iter += 8;
2225 cu_index = extract_unsigned_integer (iter, 4, BFD_ENDIAN_LITTLE);
2226 iter += 4;
2227
2228 addrmap_set_empty (mutable_map, lo + baseaddr, hi + baseaddr - 1,
1fd400ff 2229 dw2_get_cu (cu_index));
9291a0cd
TT
2230 }
2231
2232 objfile->psymtabs_addrmap = addrmap_create_fixed (mutable_map,
2233 &objfile->objfile_obstack);
2234 do_cleanups (cleanup);
2235}
2236
59d7bcaf
JK
2237/* The hash function for strings in the mapped index. This is the same as
2238 SYMBOL_HASH_NEXT, but we keep a separate copy to maintain control over the
2239 implementation. This is necessary because the hash function is tied to the
2240 format of the mapped index file. The hash values do not have to match with
559a7a62
JK
2241 SYMBOL_HASH_NEXT.
2242
2243 Use INT_MAX for INDEX_VERSION if you generate the current index format. */
2fdf6df6 2244
9291a0cd 2245static hashval_t
559a7a62 2246mapped_index_string_hash (int index_version, const void *p)
9291a0cd
TT
2247{
2248 const unsigned char *str = (const unsigned char *) p;
2249 hashval_t r = 0;
2250 unsigned char c;
2251
2252 while ((c = *str++) != 0)
559a7a62
JK
2253 {
2254 if (index_version >= 5)
2255 c = tolower (c);
2256 r = r * 67 + c - 113;
2257 }
9291a0cd
TT
2258
2259 return r;
2260}
2261
2262/* Find a slot in the mapped index INDEX for the object named NAME.
2263 If NAME is found, set *VEC_OUT to point to the CU vector in the
2264 constant pool and return 1. If NAME cannot be found, return 0. */
2fdf6df6 2265
9291a0cd
TT
2266static int
2267find_slot_in_mapped_hash (struct mapped_index *index, const char *name,
2268 offset_type **vec_out)
2269{
0cf03b49
JK
2270 struct cleanup *back_to = make_cleanup (null_cleanup, 0);
2271 offset_type hash;
9291a0cd 2272 offset_type slot, step;
559a7a62 2273 int (*cmp) (const char *, const char *);
9291a0cd 2274
0cf03b49
JK
2275 if (current_language->la_language == language_cplus
2276 || current_language->la_language == language_java
2277 || current_language->la_language == language_fortran)
2278 {
2279 /* NAME is already canonical. Drop any qualifiers as .gdb_index does
2280 not contain any. */
2281 const char *paren = strchr (name, '(');
2282
2283 if (paren)
2284 {
2285 char *dup;
2286
2287 dup = xmalloc (paren - name + 1);
2288 memcpy (dup, name, paren - name);
2289 dup[paren - name] = 0;
2290
2291 make_cleanup (xfree, dup);
2292 name = dup;
2293 }
2294 }
2295
559a7a62 2296 /* Index version 4 did not support case insensitive searches. But the
feea76c2 2297 indices for case insensitive languages are built in lowercase, therefore
559a7a62
JK
2298 simulate our NAME being searched is also lowercased. */
2299 hash = mapped_index_string_hash ((index->version == 4
2300 && case_sensitivity == case_sensitive_off
2301 ? 5 : index->version),
2302 name);
2303
3876f04e
DE
2304 slot = hash & (index->symbol_table_slots - 1);
2305 step = ((hash * 17) & (index->symbol_table_slots - 1)) | 1;
559a7a62 2306 cmp = (case_sensitivity == case_sensitive_on ? strcmp : strcasecmp);
9291a0cd
TT
2307
2308 for (;;)
2309 {
2310 /* Convert a slot number to an offset into the table. */
2311 offset_type i = 2 * slot;
2312 const char *str;
3876f04e 2313 if (index->symbol_table[i] == 0 && index->symbol_table[i + 1] == 0)
0cf03b49
JK
2314 {
2315 do_cleanups (back_to);
2316 return 0;
2317 }
9291a0cd 2318
3876f04e 2319 str = index->constant_pool + MAYBE_SWAP (index->symbol_table[i]);
559a7a62 2320 if (!cmp (name, str))
9291a0cd
TT
2321 {
2322 *vec_out = (offset_type *) (index->constant_pool
3876f04e 2323 + MAYBE_SWAP (index->symbol_table[i + 1]));
0cf03b49 2324 do_cleanups (back_to);
9291a0cd
TT
2325 return 1;
2326 }
2327
3876f04e 2328 slot = (slot + step) & (index->symbol_table_slots - 1);
9291a0cd
TT
2329 }
2330}
2331
2332/* Read the index file. If everything went ok, initialize the "quick"
2333 elements of all the CUs and return 1. Otherwise, return 0. */
2fdf6df6 2334
9291a0cd
TT
2335static int
2336dwarf2_read_index (struct objfile *objfile)
2337{
9291a0cd
TT
2338 char *addr;
2339 struct mapped_index *map;
b3b272e1 2340 offset_type *metadata;
ac0b195c
KW
2341 const gdb_byte *cu_list;
2342 const gdb_byte *types_list = NULL;
2343 offset_type version, cu_list_elements;
2344 offset_type types_list_elements = 0;
1fd400ff 2345 int i;
9291a0cd 2346
9e0ac564 2347 if (dwarf2_section_empty_p (&dwarf2_per_objfile->gdb_index))
9291a0cd 2348 return 0;
82430852
JK
2349
2350 /* Older elfutils strip versions could keep the section in the main
2351 executable while splitting it for the separate debug info file. */
2352 if ((bfd_get_file_flags (dwarf2_per_objfile->gdb_index.asection)
2353 & SEC_HAS_CONTENTS) == 0)
2354 return 0;
2355
9291a0cd
TT
2356 dwarf2_read_section (objfile, &dwarf2_per_objfile->gdb_index);
2357
2358 addr = dwarf2_per_objfile->gdb_index.buffer;
2359 /* Version check. */
1fd400ff 2360 version = MAYBE_SWAP (*(offset_type *) addr);
987d643c 2361 /* Versions earlier than 3 emitted every copy of a psymbol. This
a6e293d1 2362 causes the index to behave very poorly for certain requests. Version 3
831adc1f 2363 contained incomplete addrmap. So, it seems better to just ignore such
481860b3 2364 indices. */
831adc1f 2365 if (version < 4)
481860b3
GB
2366 {
2367 static int warning_printed = 0;
2368 if (!warning_printed)
2369 {
2370 warning (_("Skipping obsolete .gdb_index section in %s."),
2371 objfile->name);
2372 warning_printed = 1;
2373 }
2374 return 0;
2375 }
2376 /* Index version 4 uses a different hash function than index version
2377 5 and later.
2378
2379 Versions earlier than 6 did not emit psymbols for inlined
2380 functions. Using these files will cause GDB not to be able to
2381 set breakpoints on inlined functions by name, so we ignore these
2382 indices unless the --use-deprecated-index-sections command line
2383 option was supplied. */
2384 if (version < 6 && !use_deprecated_index_sections)
2385 {
2386 static int warning_printed = 0;
2387 if (!warning_printed)
2388 {
2389 warning (_("Skipping deprecated .gdb_index section in %s, pass "
2390 "--use-deprecated-index-sections to use them anyway"),
2391 objfile->name);
2392 warning_printed = 1;
2393 }
2394 return 0;
2395 }
2396 /* Indexes with higher version than the one supported by GDB may be no
594e8718 2397 longer backward compatible. */
156942c7 2398 if (version > 7)
594e8718 2399 return 0;
9291a0cd
TT
2400
2401 map = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct mapped_index);
559a7a62 2402 map->version = version;
b3b272e1 2403 map->total_size = dwarf2_per_objfile->gdb_index.size;
9291a0cd
TT
2404
2405 metadata = (offset_type *) (addr + sizeof (offset_type));
1fd400ff
TT
2406
2407 i = 0;
2408 cu_list = addr + MAYBE_SWAP (metadata[i]);
2409 cu_list_elements = ((MAYBE_SWAP (metadata[i + 1]) - MAYBE_SWAP (metadata[i]))
9291a0cd 2410 / 8);
1fd400ff
TT
2411 ++i;
2412
987d643c
TT
2413 types_list = addr + MAYBE_SWAP (metadata[i]);
2414 types_list_elements = ((MAYBE_SWAP (metadata[i + 1])
2415 - MAYBE_SWAP (metadata[i]))
2416 / 8);
2417 ++i;
1fd400ff
TT
2418
2419 map->address_table = addr + MAYBE_SWAP (metadata[i]);
2420 map->address_table_size = (MAYBE_SWAP (metadata[i + 1])
2421 - MAYBE_SWAP (metadata[i]));
2422 ++i;
2423
3876f04e
DE
2424 map->symbol_table = (offset_type *) (addr + MAYBE_SWAP (metadata[i]));
2425 map->symbol_table_slots = ((MAYBE_SWAP (metadata[i + 1])
2426 - MAYBE_SWAP (metadata[i]))
2427 / (2 * sizeof (offset_type)));
1fd400ff 2428 ++i;
9291a0cd 2429
1fd400ff
TT
2430 map->constant_pool = addr + MAYBE_SWAP (metadata[i]);
2431
0fefef59
DE
2432 /* Don't use the index if it's empty. */
2433 if (map->symbol_table_slots == 0)
2434 return 0;
2435
1fd400ff
TT
2436 if (!create_cus_from_index (objfile, cu_list, cu_list_elements))
2437 return 0;
2438
8b70b953
TT
2439 if (types_list_elements)
2440 {
2441 struct dwarf2_section_info *section;
2442
2443 /* We can only handle a single .debug_types when we have an
2444 index. */
2445 if (VEC_length (dwarf2_section_info_def, dwarf2_per_objfile->types) != 1)
2446 return 0;
2447
2448 section = VEC_index (dwarf2_section_info_def,
2449 dwarf2_per_objfile->types, 0);
2450
2451 if (!create_signatured_type_table_from_index (objfile, section,
2452 types_list,
2453 types_list_elements))
2454 return 0;
2455 }
9291a0cd
TT
2456
2457 create_addrmap_from_index (objfile, map);
2458
2459 dwarf2_per_objfile->index_table = map;
2460 dwarf2_per_objfile->using_index = 1;
7b9f3c50
DE
2461 dwarf2_per_objfile->quick_file_names_table =
2462 create_quick_file_names_table (dwarf2_per_objfile->n_comp_units);
9291a0cd
TT
2463
2464 return 1;
2465}
2466
2467/* A helper for the "quick" functions which sets the global
2468 dwarf2_per_objfile according to OBJFILE. */
2fdf6df6 2469
9291a0cd
TT
2470static void
2471dw2_setup (struct objfile *objfile)
2472{
2473 dwarf2_per_objfile = objfile_data (objfile, dwarf2_objfile_data_key);
2474 gdb_assert (dwarf2_per_objfile);
2475}
2476
dee91e82 2477/* die_reader_func for dw2_get_file_names. */
2fdf6df6 2478
dee91e82
DE
2479static void
2480dw2_get_file_names_reader (const struct die_reader_specs *reader,
2481 gdb_byte *info_ptr,
2482 struct die_info *comp_unit_die,
2483 int has_children,
2484 void *data)
9291a0cd 2485{
dee91e82
DE
2486 struct dwarf2_cu *cu = reader->cu;
2487 struct dwarf2_per_cu_data *this_cu = cu->per_cu;
2488 struct objfile *objfile = dwarf2_per_objfile->objfile;
7b9f3c50 2489 struct line_header *lh;
9291a0cd 2490 struct attribute *attr;
dee91e82 2491 int i;
9291a0cd 2492 char *name, *comp_dir;
7b9f3c50
DE
2493 void **slot;
2494 struct quick_file_names *qfn;
2495 unsigned int line_offset;
9291a0cd 2496
07261596
TT
2497 /* Our callers never want to match partial units -- instead they
2498 will match the enclosing full CU. */
2499 if (comp_unit_die->tag == DW_TAG_partial_unit)
2500 {
2501 this_cu->v.quick->no_file_data = 1;
2502 return;
2503 }
2504
7b9f3c50
DE
2505 lh = NULL;
2506 slot = NULL;
2507 line_offset = 0;
dee91e82
DE
2508
2509 attr = dwarf2_attr (comp_unit_die, DW_AT_stmt_list, cu);
9291a0cd
TT
2510 if (attr)
2511 {
7b9f3c50
DE
2512 struct quick_file_names find_entry;
2513
2514 line_offset = DW_UNSND (attr);
2515
2516 /* We may have already read in this line header (TU line header sharing).
2517 If we have we're done. */
2518 find_entry.offset = line_offset;
2519 slot = htab_find_slot (dwarf2_per_objfile->quick_file_names_table,
2520 &find_entry, INSERT);
2521 if (*slot != NULL)
2522 {
7b9f3c50 2523 this_cu->v.quick->file_names = *slot;
dee91e82 2524 return;
7b9f3c50
DE
2525 }
2526
3019eac3 2527 lh = dwarf_decode_line_header (line_offset, cu);
9291a0cd
TT
2528 }
2529 if (lh == NULL)
2530 {
7b9f3c50 2531 this_cu->v.quick->no_file_data = 1;
dee91e82 2532 return;
9291a0cd
TT
2533 }
2534
7b9f3c50
DE
2535 qfn = obstack_alloc (&objfile->objfile_obstack, sizeof (*qfn));
2536 qfn->offset = line_offset;
2537 gdb_assert (slot != NULL);
2538 *slot = qfn;
9291a0cd 2539
dee91e82 2540 find_file_and_directory (comp_unit_die, cu, &name, &comp_dir);
9291a0cd 2541
7b9f3c50
DE
2542 qfn->num_file_names = lh->num_file_names;
2543 qfn->file_names = obstack_alloc (&objfile->objfile_obstack,
2544 lh->num_file_names * sizeof (char *));
9291a0cd 2545 for (i = 0; i < lh->num_file_names; ++i)
7b9f3c50
DE
2546 qfn->file_names[i] = file_full_name (i + 1, lh, comp_dir);
2547 qfn->real_names = NULL;
9291a0cd 2548
7b9f3c50 2549 free_line_header (lh);
7b9f3c50
DE
2550
2551 this_cu->v.quick->file_names = qfn;
dee91e82
DE
2552}
2553
2554/* A helper for the "quick" functions which attempts to read the line
2555 table for THIS_CU. */
2556
2557static struct quick_file_names *
2558dw2_get_file_names (struct objfile *objfile,
2559 struct dwarf2_per_cu_data *this_cu)
2560{
2561 if (this_cu->v.quick->file_names != NULL)
2562 return this_cu->v.quick->file_names;
2563 /* If we know there is no line data, no point in looking again. */
2564 if (this_cu->v.quick->no_file_data)
2565 return NULL;
2566
3019eac3
DE
2567 /* If DWO files are in use, we can still find the DW_AT_stmt_list attribute
2568 in the stub for CUs, there's is no need to lookup the DWO file.
2569 However, that's not the case for TUs where DW_AT_stmt_list lives in the
2570 DWO file. */
2571 if (this_cu->is_debug_types)
fd820528 2572 init_cutu_and_read_dies (this_cu, 0, 0, dw2_get_file_names_reader, NULL);
3019eac3
DE
2573 else
2574 init_cutu_and_read_dies_simple (this_cu, dw2_get_file_names_reader, NULL);
dee91e82
DE
2575
2576 if (this_cu->v.quick->no_file_data)
2577 return NULL;
2578 return this_cu->v.quick->file_names;
9291a0cd
TT
2579}
2580
2581/* A helper for the "quick" functions which computes and caches the
7b9f3c50 2582 real path for a given file name from the line table. */
2fdf6df6 2583
9291a0cd 2584static const char *
7b9f3c50
DE
2585dw2_get_real_path (struct objfile *objfile,
2586 struct quick_file_names *qfn, int index)
9291a0cd 2587{
7b9f3c50
DE
2588 if (qfn->real_names == NULL)
2589 qfn->real_names = OBSTACK_CALLOC (&objfile->objfile_obstack,
2590 qfn->num_file_names, sizeof (char *));
9291a0cd 2591
7b9f3c50
DE
2592 if (qfn->real_names[index] == NULL)
2593 qfn->real_names[index] = gdb_realpath (qfn->file_names[index]);
9291a0cd 2594
7b9f3c50 2595 return qfn->real_names[index];
9291a0cd
TT
2596}
2597
2598static struct symtab *
2599dw2_find_last_source_symtab (struct objfile *objfile)
2600{
2601 int index;
ae2de4f8 2602
9291a0cd
TT
2603 dw2_setup (objfile);
2604 index = dwarf2_per_objfile->n_comp_units - 1;
a0f42c21 2605 return dw2_instantiate_symtab (dw2_get_cu (index));
9291a0cd
TT
2606}
2607
7b9f3c50
DE
2608/* Traversal function for dw2_forget_cached_source_info. */
2609
2610static int
2611dw2_free_cached_file_names (void **slot, void *info)
9291a0cd 2612{
7b9f3c50 2613 struct quick_file_names *file_data = (struct quick_file_names *) *slot;
9291a0cd 2614
7b9f3c50 2615 if (file_data->real_names)
9291a0cd 2616 {
7b9f3c50 2617 int i;
9291a0cd 2618
7b9f3c50 2619 for (i = 0; i < file_data->num_file_names; ++i)
9291a0cd 2620 {
7b9f3c50
DE
2621 xfree ((void*) file_data->real_names[i]);
2622 file_data->real_names[i] = NULL;
9291a0cd
TT
2623 }
2624 }
7b9f3c50
DE
2625
2626 return 1;
2627}
2628
2629static void
2630dw2_forget_cached_source_info (struct objfile *objfile)
2631{
2632 dw2_setup (objfile);
2633
2634 htab_traverse_noresize (dwarf2_per_objfile->quick_file_names_table,
2635 dw2_free_cached_file_names, NULL);
9291a0cd
TT
2636}
2637
f8eba3c6
TT
2638/* Helper function for dw2_map_symtabs_matching_filename that expands
2639 the symtabs and calls the iterator. */
2640
2641static int
2642dw2_map_expand_apply (struct objfile *objfile,
2643 struct dwarf2_per_cu_data *per_cu,
2644 const char *name,
2645 const char *full_path, const char *real_path,
2646 int (*callback) (struct symtab *, void *),
2647 void *data)
2648{
2649 struct symtab *last_made = objfile->symtabs;
2650
2651 /* Don't visit already-expanded CUs. */
2652 if (per_cu->v.quick->symtab)
2653 return 0;
2654
2655 /* This may expand more than one symtab, and we want to iterate over
2656 all of them. */
a0f42c21 2657 dw2_instantiate_symtab (per_cu);
f8eba3c6
TT
2658
2659 return iterate_over_some_symtabs (name, full_path, real_path, callback, data,
2660 objfile->symtabs, last_made);
2661}
2662
2663/* Implementation of the map_symtabs_matching_filename method. */
2664
9291a0cd 2665static int
f8eba3c6
TT
2666dw2_map_symtabs_matching_filename (struct objfile *objfile, const char *name,
2667 const char *full_path, const char *real_path,
2668 int (*callback) (struct symtab *, void *),
2669 void *data)
9291a0cd
TT
2670{
2671 int i;
c011a4f4 2672 const char *name_basename = lbasename (name);
4aac40c8
TT
2673 int name_len = strlen (name);
2674 int is_abs = IS_ABSOLUTE_PATH (name);
9291a0cd
TT
2675
2676 dw2_setup (objfile);
ae2de4f8 2677
1fd400ff 2678 for (i = 0; i < (dwarf2_per_objfile->n_comp_units
d467dd73 2679 + dwarf2_per_objfile->n_type_units); ++i)
9291a0cd
TT
2680 {
2681 int j;
e254ef6a 2682 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
7b9f3c50 2683 struct quick_file_names *file_data;
9291a0cd 2684
3d7bb9d9 2685 /* We only need to look at symtabs not already expanded. */
e254ef6a 2686 if (per_cu->v.quick->symtab)
9291a0cd
TT
2687 continue;
2688
7b9f3c50
DE
2689 file_data = dw2_get_file_names (objfile, per_cu);
2690 if (file_data == NULL)
9291a0cd
TT
2691 continue;
2692
7b9f3c50 2693 for (j = 0; j < file_data->num_file_names; ++j)
9291a0cd 2694 {
7b9f3c50 2695 const char *this_name = file_data->file_names[j];
9291a0cd 2696
4aac40c8
TT
2697 if (FILENAME_CMP (name, this_name) == 0
2698 || (!is_abs && compare_filenames_for_search (this_name,
2699 name, name_len)))
9291a0cd 2700 {
f8eba3c6
TT
2701 if (dw2_map_expand_apply (objfile, per_cu,
2702 name, full_path, real_path,
2703 callback, data))
2704 return 1;
4aac40c8 2705 }
9291a0cd 2706
c011a4f4
DE
2707 /* Before we invoke realpath, which can get expensive when many
2708 files are involved, do a quick comparison of the basenames. */
2709 if (! basenames_may_differ
2710 && FILENAME_CMP (lbasename (this_name), name_basename) != 0)
2711 continue;
2712
9291a0cd
TT
2713 if (full_path != NULL)
2714 {
7b9f3c50
DE
2715 const char *this_real_name = dw2_get_real_path (objfile,
2716 file_data, j);
9291a0cd 2717
7b9f3c50 2718 if (this_real_name != NULL
4aac40c8
TT
2719 && (FILENAME_CMP (full_path, this_real_name) == 0
2720 || (!is_abs
2721 && compare_filenames_for_search (this_real_name,
2722 name, name_len))))
9291a0cd 2723 {
f8eba3c6
TT
2724 if (dw2_map_expand_apply (objfile, per_cu,
2725 name, full_path, real_path,
2726 callback, data))
2727 return 1;
9291a0cd
TT
2728 }
2729 }
2730
2731 if (real_path != NULL)
2732 {
7b9f3c50
DE
2733 const char *this_real_name = dw2_get_real_path (objfile,
2734 file_data, j);
9291a0cd 2735
7b9f3c50 2736 if (this_real_name != NULL
4aac40c8
TT
2737 && (FILENAME_CMP (real_path, this_real_name) == 0
2738 || (!is_abs
2739 && compare_filenames_for_search (this_real_name,
2740 name, name_len))))
9291a0cd 2741 {
f8eba3c6
TT
2742 if (dw2_map_expand_apply (objfile, per_cu,
2743 name, full_path, real_path,
2744 callback, data))
2745 return 1;
9291a0cd
TT
2746 }
2747 }
2748 }
2749 }
2750
9291a0cd
TT
2751 return 0;
2752}
2753
2754static struct symtab *
2755dw2_lookup_symbol (struct objfile *objfile, int block_index,
2756 const char *name, domain_enum domain)
2757{
774b6a14 2758 /* We do all the work in the pre_expand_symtabs_matching hook
9291a0cd
TT
2759 instead. */
2760 return NULL;
2761}
2762
2763/* A helper function that expands all symtabs that hold an object
156942c7
DE
2764 named NAME. If WANT_SPECIFIC_BLOCK is non-zero, only look for
2765 symbols in block BLOCK_KIND. */
2fdf6df6 2766
9291a0cd 2767static void
156942c7
DE
2768dw2_do_expand_symtabs_matching (struct objfile *objfile,
2769 int want_specific_block,
2770 enum block_enum block_kind,
2771 const char *name, domain_enum domain)
9291a0cd 2772{
156942c7
DE
2773 struct mapped_index *index;
2774
9291a0cd
TT
2775 dw2_setup (objfile);
2776
156942c7
DE
2777 index = dwarf2_per_objfile->index_table;
2778
ae2de4f8 2779 /* index_table is NULL if OBJF_READNOW. */
156942c7 2780 if (index)
9291a0cd
TT
2781 {
2782 offset_type *vec;
2783
156942c7 2784 if (find_slot_in_mapped_hash (index, name, &vec))
9291a0cd
TT
2785 {
2786 offset_type i, len = MAYBE_SWAP (*vec);
2787 for (i = 0; i < len; ++i)
2788 {
156942c7
DE
2789 offset_type cu_index_and_attrs = MAYBE_SWAP (vec[i + 1]);
2790 offset_type cu_index = GDB_INDEX_CU_VALUE (cu_index_and_attrs);
e254ef6a 2791 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (cu_index);
156942c7
DE
2792 int want_static = block_kind != GLOBAL_BLOCK;
2793 /* This value is only valid for index versions >= 7. */
2794 int is_static = GDB_INDEX_SYMBOL_STATIC_VALUE (cu_index_and_attrs);
2795 gdb_index_symbol_kind symbol_kind =
2796 GDB_INDEX_SYMBOL_KIND_VALUE (cu_index_and_attrs);
2797
2798 if (want_specific_block
2799 && index->version >= 7
2800 && want_static != is_static)
2801 continue;
2802
2803 /* Only check the symbol's kind if it has one.
2804 Indices prior to version 7 don't record it. */
2805 if (index->version >= 7)
2806 {
2807 switch (domain)
2808 {
2809 case VAR_DOMAIN:
2810 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_VARIABLE
2811 && symbol_kind != GDB_INDEX_SYMBOL_KIND_FUNCTION
2812 /* Some types are also in VAR_DOMAIN. */
2813 && symbol_kind != GDB_INDEX_SYMBOL_KIND_TYPE)
2814 continue;
2815 break;
2816 case STRUCT_DOMAIN:
2817 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_TYPE)
2818 continue;
2819 break;
2820 case LABEL_DOMAIN:
2821 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_OTHER)
2822 continue;
2823 break;
2824 default:
2825 break;
2826 }
2827 }
1fd400ff 2828
a0f42c21 2829 dw2_instantiate_symtab (per_cu);
9291a0cd
TT
2830 }
2831 }
2832 }
2833}
2834
774b6a14
TT
2835static void
2836dw2_pre_expand_symtabs_matching (struct objfile *objfile,
8903c50d 2837 enum block_enum block_kind, const char *name,
774b6a14 2838 domain_enum domain)
9291a0cd 2839{
156942c7 2840 dw2_do_expand_symtabs_matching (objfile, 1, block_kind, name, domain);
9291a0cd
TT
2841}
2842
2843static void
2844dw2_print_stats (struct objfile *objfile)
2845{
2846 int i, count;
2847
2848 dw2_setup (objfile);
2849 count = 0;
1fd400ff 2850 for (i = 0; i < (dwarf2_per_objfile->n_comp_units
d467dd73 2851 + dwarf2_per_objfile->n_type_units); ++i)
9291a0cd 2852 {
e254ef6a 2853 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
9291a0cd 2854
e254ef6a 2855 if (!per_cu->v.quick->symtab)
9291a0cd
TT
2856 ++count;
2857 }
2858 printf_filtered (_(" Number of unread CUs: %d\n"), count);
2859}
2860
2861static void
2862dw2_dump (struct objfile *objfile)
2863{
2864 /* Nothing worth printing. */
2865}
2866
2867static void
2868dw2_relocate (struct objfile *objfile, struct section_offsets *new_offsets,
2869 struct section_offsets *delta)
2870{
2871 /* There's nothing to relocate here. */
2872}
2873
2874static void
2875dw2_expand_symtabs_for_function (struct objfile *objfile,
2876 const char *func_name)
2877{
156942c7
DE
2878 /* Note: It doesn't matter what we pass for block_kind here. */
2879 dw2_do_expand_symtabs_matching (objfile, 0, GLOBAL_BLOCK, func_name,
2880 VAR_DOMAIN);
9291a0cd
TT
2881}
2882
2883static void
2884dw2_expand_all_symtabs (struct objfile *objfile)
2885{
2886 int i;
2887
2888 dw2_setup (objfile);
1fd400ff
TT
2889
2890 for (i = 0; i < (dwarf2_per_objfile->n_comp_units
d467dd73 2891 + dwarf2_per_objfile->n_type_units); ++i)
9291a0cd 2892 {
e254ef6a 2893 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
9291a0cd 2894
a0f42c21 2895 dw2_instantiate_symtab (per_cu);
9291a0cd
TT
2896 }
2897}
2898
2899static void
2900dw2_expand_symtabs_with_filename (struct objfile *objfile,
2901 const char *filename)
2902{
2903 int i;
2904
2905 dw2_setup (objfile);
d4637a04
DE
2906
2907 /* We don't need to consider type units here.
2908 This is only called for examining code, e.g. expand_line_sal.
2909 There can be an order of magnitude (or more) more type units
2910 than comp units, and we avoid them if we can. */
2911
2912 for (i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
9291a0cd
TT
2913 {
2914 int j;
e254ef6a 2915 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
7b9f3c50 2916 struct quick_file_names *file_data;
9291a0cd 2917
3d7bb9d9 2918 /* We only need to look at symtabs not already expanded. */
e254ef6a 2919 if (per_cu->v.quick->symtab)
9291a0cd
TT
2920 continue;
2921
7b9f3c50
DE
2922 file_data = dw2_get_file_names (objfile, per_cu);
2923 if (file_data == NULL)
9291a0cd
TT
2924 continue;
2925
7b9f3c50 2926 for (j = 0; j < file_data->num_file_names; ++j)
9291a0cd 2927 {
7b9f3c50 2928 const char *this_name = file_data->file_names[j];
1ef75ecc 2929 if (FILENAME_CMP (this_name, filename) == 0)
9291a0cd 2930 {
a0f42c21 2931 dw2_instantiate_symtab (per_cu);
9291a0cd
TT
2932 break;
2933 }
2934 }
2935 }
2936}
2937
356d9f9d
TT
2938/* A helper function for dw2_find_symbol_file that finds the primary
2939 file name for a given CU. This is a die_reader_func. */
2940
2941static void
2942dw2_get_primary_filename_reader (const struct die_reader_specs *reader,
2943 gdb_byte *info_ptr,
2944 struct die_info *comp_unit_die,
2945 int has_children,
2946 void *data)
2947{
2948 const char **result_ptr = data;
2949 struct dwarf2_cu *cu = reader->cu;
2950 struct attribute *attr;
2951
2952 attr = dwarf2_attr (comp_unit_die, DW_AT_name, cu);
2953 if (attr == NULL)
2954 *result_ptr = NULL;
2955 else
2956 *result_ptr = DW_STRING (attr);
2957}
2958
dd786858 2959static const char *
9291a0cd
TT
2960dw2_find_symbol_file (struct objfile *objfile, const char *name)
2961{
e254ef6a 2962 struct dwarf2_per_cu_data *per_cu;
9291a0cd 2963 offset_type *vec;
7b9f3c50 2964 struct quick_file_names *file_data;
356d9f9d 2965 const char *filename;
9291a0cd
TT
2966
2967 dw2_setup (objfile);
2968
ae2de4f8 2969 /* index_table is NULL if OBJF_READNOW. */
9291a0cd 2970 if (!dwarf2_per_objfile->index_table)
96408a79
SA
2971 {
2972 struct symtab *s;
2973
d790cf0a
DE
2974 ALL_OBJFILE_PRIMARY_SYMTABS (objfile, s)
2975 {
2976 struct blockvector *bv = BLOCKVECTOR (s);
2977 const struct block *block = BLOCKVECTOR_BLOCK (bv, GLOBAL_BLOCK);
2978 struct symbol *sym = lookup_block_symbol (block, name, VAR_DOMAIN);
2979
2980 if (sym)
2981 return sym->symtab->filename;
2982 }
96408a79
SA
2983 return NULL;
2984 }
9291a0cd
TT
2985
2986 if (!find_slot_in_mapped_hash (dwarf2_per_objfile->index_table,
2987 name, &vec))
2988 return NULL;
2989
2990 /* Note that this just looks at the very first one named NAME -- but
2991 actually we are looking for a function. find_main_filename
2992 should be rewritten so that it doesn't require a custom hook. It
2993 could just use the ordinary symbol tables. */
2994 /* vec[0] is the length, which must always be >0. */
156942c7 2995 per_cu = dw2_get_cu (GDB_INDEX_CU_VALUE (MAYBE_SWAP (vec[1])));
9291a0cd 2996
356d9f9d
TT
2997 if (per_cu->v.quick->symtab != NULL)
2998 return per_cu->v.quick->symtab->filename;
2999
663b969e
TT
3000 init_cutu_and_read_dies (per_cu, 0, 0, dw2_get_primary_filename_reader,
3001 &filename);
9291a0cd 3002
356d9f9d 3003 return filename;
9291a0cd
TT
3004}
3005
3006static void
40658b94
PH
3007dw2_map_matching_symbols (const char * name, domain_enum namespace,
3008 struct objfile *objfile, int global,
3009 int (*callback) (struct block *,
3010 struct symbol *, void *),
2edb89d3
JK
3011 void *data, symbol_compare_ftype *match,
3012 symbol_compare_ftype *ordered_compare)
9291a0cd 3013{
40658b94 3014 /* Currently unimplemented; used for Ada. The function can be called if the
a9e6a4bb
JK
3015 current language is Ada for a non-Ada objfile using GNU index. As Ada
3016 does not look for non-Ada symbols this function should just return. */
9291a0cd
TT
3017}
3018
3019static void
f8eba3c6
TT
3020dw2_expand_symtabs_matching
3021 (struct objfile *objfile,
3022 int (*file_matcher) (const char *, void *),
e078317b 3023 int (*name_matcher) (const char *, void *),
f8eba3c6
TT
3024 enum search_domain kind,
3025 void *data)
9291a0cd
TT
3026{
3027 int i;
3028 offset_type iter;
4b5246aa 3029 struct mapped_index *index;
9291a0cd
TT
3030
3031 dw2_setup (objfile);
ae2de4f8
DE
3032
3033 /* index_table is NULL if OBJF_READNOW. */
9291a0cd
TT
3034 if (!dwarf2_per_objfile->index_table)
3035 return;
4b5246aa 3036 index = dwarf2_per_objfile->index_table;
9291a0cd 3037
7b08b9eb 3038 if (file_matcher != NULL)
24c79950
TT
3039 {
3040 struct cleanup *cleanup;
3041 htab_t visited_found, visited_not_found;
3042
3043 visited_found = htab_create_alloc (10,
3044 htab_hash_pointer, htab_eq_pointer,
3045 NULL, xcalloc, xfree);
3046 cleanup = make_cleanup_htab_delete (visited_found);
3047 visited_not_found = htab_create_alloc (10,
3048 htab_hash_pointer, htab_eq_pointer,
3049 NULL, xcalloc, xfree);
3050 make_cleanup_htab_delete (visited_not_found);
3051
3052 for (i = 0; i < (dwarf2_per_objfile->n_comp_units
3053 + dwarf2_per_objfile->n_type_units); ++i)
3054 {
3055 int j;
3056 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
3057 struct quick_file_names *file_data;
3058 void **slot;
7b08b9eb 3059
24c79950 3060 per_cu->v.quick->mark = 0;
3d7bb9d9 3061
24c79950
TT
3062 /* We only need to look at symtabs not already expanded. */
3063 if (per_cu->v.quick->symtab)
3064 continue;
7b08b9eb 3065
24c79950
TT
3066 file_data = dw2_get_file_names (objfile, per_cu);
3067 if (file_data == NULL)
3068 continue;
7b08b9eb 3069
24c79950
TT
3070 if (htab_find (visited_not_found, file_data) != NULL)
3071 continue;
3072 else if (htab_find (visited_found, file_data) != NULL)
3073 {
3074 per_cu->v.quick->mark = 1;
3075 continue;
3076 }
3077
3078 for (j = 0; j < file_data->num_file_names; ++j)
3079 {
3080 if (file_matcher (file_data->file_names[j], data))
3081 {
3082 per_cu->v.quick->mark = 1;
3083 break;
3084 }
3085 }
3086
3087 slot = htab_find_slot (per_cu->v.quick->mark
3088 ? visited_found
3089 : visited_not_found,
3090 file_data, INSERT);
3091 *slot = file_data;
3092 }
3093
3094 do_cleanups (cleanup);
3095 }
9291a0cd 3096
3876f04e 3097 for (iter = 0; iter < index->symbol_table_slots; ++iter)
9291a0cd
TT
3098 {
3099 offset_type idx = 2 * iter;
3100 const char *name;
3101 offset_type *vec, vec_len, vec_idx;
3102
3876f04e 3103 if (index->symbol_table[idx] == 0 && index->symbol_table[idx + 1] == 0)
9291a0cd
TT
3104 continue;
3105
3876f04e 3106 name = index->constant_pool + MAYBE_SWAP (index->symbol_table[idx]);
9291a0cd 3107
e078317b 3108 if (! (*name_matcher) (name, data))
9291a0cd
TT
3109 continue;
3110
3111 /* The name was matched, now expand corresponding CUs that were
3112 marked. */
4b5246aa 3113 vec = (offset_type *) (index->constant_pool
3876f04e 3114 + MAYBE_SWAP (index->symbol_table[idx + 1]));
9291a0cd
TT
3115 vec_len = MAYBE_SWAP (vec[0]);
3116 for (vec_idx = 0; vec_idx < vec_len; ++vec_idx)
3117 {
e254ef6a 3118 struct dwarf2_per_cu_data *per_cu;
156942c7
DE
3119 offset_type cu_index_and_attrs = MAYBE_SWAP (vec[vec_idx + 1]);
3120 gdb_index_symbol_kind symbol_kind =
3121 GDB_INDEX_SYMBOL_KIND_VALUE (cu_index_and_attrs);
3122 int cu_index = GDB_INDEX_CU_VALUE (cu_index_and_attrs);
3123
3124 /* Don't crash on bad data. */
3125 if (cu_index >= (dwarf2_per_objfile->n_comp_units
667e0a4b 3126 + dwarf2_per_objfile->n_type_units))
156942c7 3127 continue;
1fd400ff 3128
156942c7
DE
3129 /* Only check the symbol's kind if it has one.
3130 Indices prior to version 7 don't record it. */
3131 if (index->version >= 7)
3132 {
3133 switch (kind)
3134 {
3135 case VARIABLES_DOMAIN:
3136 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_VARIABLE)
3137 continue;
3138 break;
3139 case FUNCTIONS_DOMAIN:
3140 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_FUNCTION)
3141 continue;
3142 break;
3143 case TYPES_DOMAIN:
3144 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_TYPE)
3145 continue;
3146 break;
3147 default:
3148 break;
3149 }
3150 }
3151
3152 per_cu = dw2_get_cu (cu_index);
7b08b9eb 3153 if (file_matcher == NULL || per_cu->v.quick->mark)
a0f42c21 3154 dw2_instantiate_symtab (per_cu);
9291a0cd
TT
3155 }
3156 }
3157}
3158
9703b513
TT
3159/* A helper for dw2_find_pc_sect_symtab which finds the most specific
3160 symtab. */
3161
3162static struct symtab *
3163recursively_find_pc_sect_symtab (struct symtab *symtab, CORE_ADDR pc)
3164{
3165 int i;
3166
3167 if (BLOCKVECTOR (symtab) != NULL
3168 && blockvector_contains_pc (BLOCKVECTOR (symtab), pc))
3169 return symtab;
3170
a3ec0bb1
DE
3171 if (symtab->includes == NULL)
3172 return NULL;
3173
9703b513
TT
3174 for (i = 0; symtab->includes[i]; ++i)
3175 {
a3ec0bb1 3176 struct symtab *s = symtab->includes[i];
9703b513
TT
3177
3178 s = recursively_find_pc_sect_symtab (s, pc);
3179 if (s != NULL)
3180 return s;
3181 }
3182
3183 return NULL;
3184}
3185
9291a0cd
TT
3186static struct symtab *
3187dw2_find_pc_sect_symtab (struct objfile *objfile,
3188 struct minimal_symbol *msymbol,
3189 CORE_ADDR pc,
3190 struct obj_section *section,
3191 int warn_if_readin)
3192{
3193 struct dwarf2_per_cu_data *data;
9703b513 3194 struct symtab *result;
9291a0cd
TT
3195
3196 dw2_setup (objfile);
3197
3198 if (!objfile->psymtabs_addrmap)
3199 return NULL;
3200
3201 data = addrmap_find (objfile->psymtabs_addrmap, pc);
3202 if (!data)
3203 return NULL;
3204
3205 if (warn_if_readin && data->v.quick->symtab)
abebb8b0 3206 warning (_("(Internal error: pc %s in read in CU, but not in symtab.)"),
9291a0cd
TT
3207 paddress (get_objfile_arch (objfile), pc));
3208
9703b513
TT
3209 result = recursively_find_pc_sect_symtab (dw2_instantiate_symtab (data), pc);
3210 gdb_assert (result != NULL);
3211 return result;
9291a0cd
TT
3212}
3213
9291a0cd 3214static void
44b13c5a 3215dw2_map_symbol_filenames (struct objfile *objfile, symbol_filename_ftype *fun,
74e2f255 3216 void *data, int need_fullname)
9291a0cd
TT
3217{
3218 int i;
24c79950
TT
3219 struct cleanup *cleanup;
3220 htab_t visited = htab_create_alloc (10, htab_hash_pointer, htab_eq_pointer,
3221 NULL, xcalloc, xfree);
9291a0cd 3222
24c79950 3223 cleanup = make_cleanup_htab_delete (visited);
9291a0cd 3224 dw2_setup (objfile);
ae2de4f8 3225
24c79950
TT
3226 /* We can ignore file names coming from already-expanded CUs. */
3227 for (i = 0; i < (dwarf2_per_objfile->n_comp_units
3228 + dwarf2_per_objfile->n_type_units); ++i)
3229 {
3230 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
3231
3232 if (per_cu->v.quick->symtab)
3233 {
3234 void **slot = htab_find_slot (visited, per_cu->v.quick->file_names,
3235 INSERT);
3236
3237 *slot = per_cu->v.quick->file_names;
3238 }
3239 }
3240
1fd400ff 3241 for (i = 0; i < (dwarf2_per_objfile->n_comp_units
d467dd73 3242 + dwarf2_per_objfile->n_type_units); ++i)
9291a0cd
TT
3243 {
3244 int j;
e254ef6a 3245 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
7b9f3c50 3246 struct quick_file_names *file_data;
24c79950 3247 void **slot;
9291a0cd 3248
3d7bb9d9 3249 /* We only need to look at symtabs not already expanded. */
e254ef6a 3250 if (per_cu->v.quick->symtab)
9291a0cd
TT
3251 continue;
3252
7b9f3c50
DE
3253 file_data = dw2_get_file_names (objfile, per_cu);
3254 if (file_data == NULL)
9291a0cd
TT
3255 continue;
3256
24c79950
TT
3257 slot = htab_find_slot (visited, file_data, INSERT);
3258 if (*slot)
3259 {
3260 /* Already visited. */
3261 continue;
3262 }
3263 *slot = file_data;
3264
7b9f3c50 3265 for (j = 0; j < file_data->num_file_names; ++j)
9291a0cd 3266 {
74e2f255
DE
3267 const char *this_real_name;
3268
3269 if (need_fullname)
3270 this_real_name = dw2_get_real_path (objfile, file_data, j);
3271 else
3272 this_real_name = NULL;
7b9f3c50 3273 (*fun) (file_data->file_names[j], this_real_name, data);
9291a0cd
TT
3274 }
3275 }
24c79950
TT
3276
3277 do_cleanups (cleanup);
9291a0cd
TT
3278}
3279
3280static int
3281dw2_has_symbols (struct objfile *objfile)
3282{
3283 return 1;
3284}
3285
3286const struct quick_symbol_functions dwarf2_gdb_index_functions =
3287{
3288 dw2_has_symbols,
3289 dw2_find_last_source_symtab,
3290 dw2_forget_cached_source_info,
f8eba3c6 3291 dw2_map_symtabs_matching_filename,
9291a0cd 3292 dw2_lookup_symbol,
774b6a14 3293 dw2_pre_expand_symtabs_matching,
9291a0cd
TT
3294 dw2_print_stats,
3295 dw2_dump,
3296 dw2_relocate,
3297 dw2_expand_symtabs_for_function,
3298 dw2_expand_all_symtabs,
3299 dw2_expand_symtabs_with_filename,
3300 dw2_find_symbol_file,
40658b94 3301 dw2_map_matching_symbols,
9291a0cd
TT
3302 dw2_expand_symtabs_matching,
3303 dw2_find_pc_sect_symtab,
9291a0cd
TT
3304 dw2_map_symbol_filenames
3305};
3306
3307/* Initialize for reading DWARF for this objfile. Return 0 if this
3308 file will use psymtabs, or 1 if using the GNU index. */
3309
3310int
3311dwarf2_initialize_objfile (struct objfile *objfile)
3312{
3313 /* If we're about to read full symbols, don't bother with the
3314 indices. In this case we also don't care if some other debug
3315 format is making psymtabs, because they are all about to be
3316 expanded anyway. */
3317 if ((objfile->flags & OBJF_READNOW))
3318 {
3319 int i;
3320
3321 dwarf2_per_objfile->using_index = 1;
3322 create_all_comp_units (objfile);
0e50663e 3323 create_all_type_units (objfile);
7b9f3c50
DE
3324 dwarf2_per_objfile->quick_file_names_table =
3325 create_quick_file_names_table (dwarf2_per_objfile->n_comp_units);
9291a0cd 3326
1fd400ff 3327 for (i = 0; i < (dwarf2_per_objfile->n_comp_units
d467dd73 3328 + dwarf2_per_objfile->n_type_units); ++i)
9291a0cd 3329 {
e254ef6a 3330 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
9291a0cd 3331
e254ef6a
DE
3332 per_cu->v.quick = OBSTACK_ZALLOC (&objfile->objfile_obstack,
3333 struct dwarf2_per_cu_quick_data);
9291a0cd
TT
3334 }
3335
3336 /* Return 1 so that gdb sees the "quick" functions. However,
3337 these functions will be no-ops because we will have expanded
3338 all symtabs. */
3339 return 1;
3340 }
3341
3342 if (dwarf2_read_index (objfile))
3343 return 1;
3344
9291a0cd
TT
3345 return 0;
3346}
3347
3348\f
3349
dce234bc
PP
3350/* Build a partial symbol table. */
3351
3352void
f29dff0a 3353dwarf2_build_psymtabs (struct objfile *objfile)
dce234bc 3354{
f29dff0a 3355 if (objfile->global_psymbols.size == 0 && objfile->static_psymbols.size == 0)
c906108c
SS
3356 {
3357 init_psymbol_list (objfile, 1024);
3358 }
3359
d146bf1e 3360 dwarf2_build_psymtabs_hard (objfile);
c906108c 3361}
c906108c 3362
1ce1cefd
DE
3363/* Return the total length of the CU described by HEADER. */
3364
3365static unsigned int
3366get_cu_length (const struct comp_unit_head *header)
3367{
3368 return header->initial_length_size + header->length;
3369}
3370
45452591
DE
3371/* Return TRUE if OFFSET is within CU_HEADER. */
3372
3373static inline int
b64f50a1 3374offset_in_cu_p (const struct comp_unit_head *cu_header, sect_offset offset)
45452591 3375{
b64f50a1 3376 sect_offset bottom = { cu_header->offset.sect_off };
1ce1cefd 3377 sect_offset top = { cu_header->offset.sect_off + get_cu_length (cu_header) };
9a619af0 3378
b64f50a1 3379 return (offset.sect_off >= bottom.sect_off && offset.sect_off < top.sect_off);
45452591
DE
3380}
3381
3b80fe9b
DE
3382/* Find the base address of the compilation unit for range lists and
3383 location lists. It will normally be specified by DW_AT_low_pc.
3384 In DWARF-3 draft 4, the base address could be overridden by
3385 DW_AT_entry_pc. It's been removed, but GCC still uses this for
3386 compilation units with discontinuous ranges. */
3387
3388static void
3389dwarf2_find_base_address (struct die_info *die, struct dwarf2_cu *cu)
3390{
3391 struct attribute *attr;
3392
3393 cu->base_known = 0;
3394 cu->base_address = 0;
3395
3396 attr = dwarf2_attr (die, DW_AT_entry_pc, cu);
3397 if (attr)
3398 {
3399 cu->base_address = DW_ADDR (attr);
3400 cu->base_known = 1;
3401 }
3402 else
3403 {
3404 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
3405 if (attr)
3406 {
3407 cu->base_address = DW_ADDR (attr);
3408 cu->base_known = 1;
3409 }
3410 }
3411}
3412
93311388
DE
3413/* Read in the comp unit header information from the debug_info at info_ptr.
3414 NOTE: This leaves members offset, first_die_offset to be filled in
3415 by the caller. */
107d2387 3416
fe1b8b76 3417static gdb_byte *
107d2387 3418read_comp_unit_head (struct comp_unit_head *cu_header,
fe1b8b76 3419 gdb_byte *info_ptr, bfd *abfd)
107d2387
AC
3420{
3421 int signed_addr;
891d2f0b 3422 unsigned int bytes_read;
c764a876
DE
3423
3424 cu_header->length = read_initial_length (abfd, info_ptr, &bytes_read);
3425 cu_header->initial_length_size = bytes_read;
3426 cu_header->offset_size = (bytes_read == 4) ? 4 : 8;
613e1657 3427 info_ptr += bytes_read;
107d2387
AC
3428 cu_header->version = read_2_bytes (abfd, info_ptr);
3429 info_ptr += 2;
b64f50a1
JK
3430 cu_header->abbrev_offset.sect_off = read_offset (abfd, info_ptr, cu_header,
3431 &bytes_read);
613e1657 3432 info_ptr += bytes_read;
107d2387
AC
3433 cu_header->addr_size = read_1_byte (abfd, info_ptr);
3434 info_ptr += 1;
3435 signed_addr = bfd_get_sign_extend_vma (abfd);
3436 if (signed_addr < 0)
8e65ff28 3437 internal_error (__FILE__, __LINE__,
e2e0b3e5 3438 _("read_comp_unit_head: dwarf from non elf file"));
107d2387 3439 cu_header->signed_addr_p = signed_addr;
c764a876 3440
107d2387
AC
3441 return info_ptr;
3442}
3443
9ff913ba
DE
3444/* Subroutine of read_and_check_comp_unit_head and
3445 read_and_check_type_unit_head to simplify them.
3446 Perform various error checking on the header. */
3447
3448static void
3449error_check_comp_unit_head (struct comp_unit_head *header,
4bdcc0c1
DE
3450 struct dwarf2_section_info *section,
3451 struct dwarf2_section_info *abbrev_section)
9ff913ba
DE
3452{
3453 bfd *abfd = section->asection->owner;
3454 const char *filename = bfd_get_filename (abfd);
3455
3456 if (header->version != 2 && header->version != 3 && header->version != 4)
3457 error (_("Dwarf Error: wrong version in compilation unit header "
3458 "(is %d, should be 2, 3, or 4) [in module %s]"), header->version,
3459 filename);
3460
b64f50a1 3461 if (header->abbrev_offset.sect_off
9ff913ba
DE
3462 >= dwarf2_section_size (dwarf2_per_objfile->objfile,
3463 &dwarf2_per_objfile->abbrev))
3464 error (_("Dwarf Error: bad offset (0x%lx) in compilation unit header "
3465 "(offset 0x%lx + 6) [in module %s]"),
b64f50a1 3466 (long) header->abbrev_offset.sect_off, (long) header->offset.sect_off,
9ff913ba
DE
3467 filename);
3468
3469 /* Cast to unsigned long to use 64-bit arithmetic when possible to
3470 avoid potential 32-bit overflow. */
1ce1cefd 3471 if (((unsigned long) header->offset.sect_off + get_cu_length (header))
9ff913ba
DE
3472 > section->size)
3473 error (_("Dwarf Error: bad length (0x%lx) in compilation unit header "
3474 "(offset 0x%lx + 0) [in module %s]"),
b64f50a1 3475 (long) header->length, (long) header->offset.sect_off,
9ff913ba
DE
3476 filename);
3477}
3478
3479/* Read in a CU/TU header and perform some basic error checking.
3480 The contents of the header are stored in HEADER.
3481 The result is a pointer to the start of the first DIE. */
adabb602 3482
fe1b8b76 3483static gdb_byte *
9ff913ba
DE
3484read_and_check_comp_unit_head (struct comp_unit_head *header,
3485 struct dwarf2_section_info *section,
4bdcc0c1 3486 struct dwarf2_section_info *abbrev_section,
9ff913ba
DE
3487 gdb_byte *info_ptr,
3488 int is_debug_types_section)
72bf9492 3489{
fe1b8b76 3490 gdb_byte *beg_of_comp_unit = info_ptr;
9ff913ba 3491 bfd *abfd = section->asection->owner;
72bf9492 3492
b64f50a1 3493 header->offset.sect_off = beg_of_comp_unit - section->buffer;
adabb602 3494
72bf9492
DJ
3495 info_ptr = read_comp_unit_head (header, info_ptr, abfd);
3496
460c1c54
CC
3497 /* If we're reading a type unit, skip over the signature and
3498 type_offset fields. */
b0df02fd 3499 if (is_debug_types_section)
460c1c54
CC
3500 info_ptr += 8 /*signature*/ + header->offset_size;
3501
b64f50a1 3502 header->first_die_offset.cu_off = info_ptr - beg_of_comp_unit;
adabb602 3503
4bdcc0c1 3504 error_check_comp_unit_head (header, section, abbrev_section);
72bf9492
DJ
3505
3506 return info_ptr;
3507}
3508
348e048f
DE
3509/* Read in the types comp unit header information from .debug_types entry at
3510 types_ptr. The result is a pointer to one past the end of the header. */
3511
3512static gdb_byte *
9ff913ba
DE
3513read_and_check_type_unit_head (struct comp_unit_head *header,
3514 struct dwarf2_section_info *section,
4bdcc0c1 3515 struct dwarf2_section_info *abbrev_section,
9ff913ba 3516 gdb_byte *info_ptr,
dee91e82
DE
3517 ULONGEST *signature,
3518 cu_offset *type_offset_in_tu)
348e048f 3519{
9ff913ba
DE
3520 gdb_byte *beg_of_comp_unit = info_ptr;
3521 bfd *abfd = section->asection->owner;
348e048f 3522
b64f50a1 3523 header->offset.sect_off = beg_of_comp_unit - section->buffer;
348e048f 3524
9ff913ba 3525 info_ptr = read_comp_unit_head (header, info_ptr, abfd);
348e048f 3526
9ff913ba
DE
3527 /* If we're reading a type unit, skip over the signature and
3528 type_offset fields. */
3529 if (signature != NULL)
3530 *signature = read_8_bytes (abfd, info_ptr);
3531 info_ptr += 8;
dee91e82
DE
3532 if (type_offset_in_tu != NULL)
3533 type_offset_in_tu->cu_off = read_offset_1 (abfd, info_ptr,
3534 header->offset_size);
9ff913ba
DE
3535 info_ptr += header->offset_size;
3536
b64f50a1 3537 header->first_die_offset.cu_off = info_ptr - beg_of_comp_unit;
348e048f 3538
4bdcc0c1 3539 error_check_comp_unit_head (header, section, abbrev_section);
9ff913ba
DE
3540
3541 return info_ptr;
348e048f
DE
3542}
3543
aaa75496
JB
3544/* Allocate a new partial symtab for file named NAME and mark this new
3545 partial symtab as being an include of PST. */
3546
3547static void
3548dwarf2_create_include_psymtab (char *name, struct partial_symtab *pst,
3549 struct objfile *objfile)
3550{
3551 struct partial_symtab *subpst = allocate_psymtab (name, objfile);
3552
3553 subpst->section_offsets = pst->section_offsets;
3554 subpst->textlow = 0;
3555 subpst->texthigh = 0;
3556
3557 subpst->dependencies = (struct partial_symtab **)
3558 obstack_alloc (&objfile->objfile_obstack,
3559 sizeof (struct partial_symtab *));
3560 subpst->dependencies[0] = pst;
3561 subpst->number_of_dependencies = 1;
3562
3563 subpst->globals_offset = 0;
3564 subpst->n_global_syms = 0;
3565 subpst->statics_offset = 0;
3566 subpst->n_static_syms = 0;
3567 subpst->symtab = NULL;
3568 subpst->read_symtab = pst->read_symtab;
3569 subpst->readin = 0;
3570
3571 /* No private part is necessary for include psymtabs. This property
3572 can be used to differentiate between such include psymtabs and
10b3939b 3573 the regular ones. */
58a9656e 3574 subpst->read_symtab_private = NULL;
aaa75496
JB
3575}
3576
3577/* Read the Line Number Program data and extract the list of files
3578 included by the source file represented by PST. Build an include
d85a05f0 3579 partial symtab for each of these included files. */
aaa75496
JB
3580
3581static void
3582dwarf2_build_include_psymtabs (struct dwarf2_cu *cu,
dee91e82
DE
3583 struct die_info *die,
3584 struct partial_symtab *pst)
aaa75496 3585{
d85a05f0
DJ
3586 struct line_header *lh = NULL;
3587 struct attribute *attr;
aaa75496 3588
d85a05f0
DJ
3589 attr = dwarf2_attr (die, DW_AT_stmt_list, cu);
3590 if (attr)
3019eac3 3591 lh = dwarf_decode_line_header (DW_UNSND (attr), cu);
aaa75496
JB
3592 if (lh == NULL)
3593 return; /* No linetable, so no includes. */
3594
c6da4cef 3595 /* NOTE: pst->dirname is DW_AT_comp_dir (if present). */
f3f5162e 3596 dwarf_decode_lines (lh, pst->dirname, cu, pst, 1);
aaa75496
JB
3597
3598 free_line_header (lh);
3599}
3600
348e048f 3601static hashval_t
52dc124a 3602hash_signatured_type (const void *item)
348e048f 3603{
52dc124a 3604 const struct signatured_type *sig_type = item;
9a619af0 3605
348e048f 3606 /* This drops the top 32 bits of the signature, but is ok for a hash. */
52dc124a 3607 return sig_type->signature;
348e048f
DE
3608}
3609
3610static int
52dc124a 3611eq_signatured_type (const void *item_lhs, const void *item_rhs)
348e048f
DE
3612{
3613 const struct signatured_type *lhs = item_lhs;
3614 const struct signatured_type *rhs = item_rhs;
9a619af0 3615
348e048f
DE
3616 return lhs->signature == rhs->signature;
3617}
3618
1fd400ff
TT
3619/* Allocate a hash table for signatured types. */
3620
3621static htab_t
673bfd45 3622allocate_signatured_type_table (struct objfile *objfile)
1fd400ff
TT
3623{
3624 return htab_create_alloc_ex (41,
52dc124a
DE
3625 hash_signatured_type,
3626 eq_signatured_type,
1fd400ff
TT
3627 NULL,
3628 &objfile->objfile_obstack,
3629 hashtab_obstack_allocate,
3630 dummy_obstack_deallocate);
3631}
3632
d467dd73 3633/* A helper function to add a signatured type CU to a table. */
1fd400ff
TT
3634
3635static int
d467dd73 3636add_signatured_type_cu_to_table (void **slot, void *datum)
1fd400ff
TT
3637{
3638 struct signatured_type *sigt = *slot;
b4dd5633 3639 struct signatured_type ***datap = datum;
1fd400ff 3640
b4dd5633 3641 **datap = sigt;
1fd400ff
TT
3642 ++*datap;
3643
3644 return 1;
3645}
3646
3019eac3
DE
3647/* Create the hash table of all entries in the .debug_types section.
3648 DWO_FILE is a pointer to the DWO file for .debug_types.dwo, NULL otherwise.
3649 The result is a pointer to the hash table or NULL if there are
3650 no types. */
348e048f 3651
3019eac3
DE
3652static htab_t
3653create_debug_types_hash_table (struct dwo_file *dwo_file,
3654 VEC (dwarf2_section_info_def) *types)
348e048f 3655{
3019eac3 3656 struct objfile *objfile = dwarf2_per_objfile->objfile;
8b70b953 3657 htab_t types_htab = NULL;
8b70b953
TT
3658 int ix;
3659 struct dwarf2_section_info *section;
4bdcc0c1 3660 struct dwarf2_section_info *abbrev_section;
348e048f 3661
3019eac3
DE
3662 if (VEC_empty (dwarf2_section_info_def, types))
3663 return NULL;
348e048f 3664
4bdcc0c1
DE
3665 abbrev_section = (dwo_file != NULL
3666 ? &dwo_file->sections.abbrev
3667 : &dwarf2_per_objfile->abbrev);
3668
09406207
DE
3669 if (dwarf2_read_debug)
3670 fprintf_unfiltered (gdb_stdlog, "Reading .debug_types%s for %s:\n",
3671 dwo_file ? ".dwo" : "",
3672 bfd_get_filename (abbrev_section->asection->owner));
3673
8b70b953 3674 for (ix = 0;
3019eac3 3675 VEC_iterate (dwarf2_section_info_def, types, ix, section);
8b70b953
TT
3676 ++ix)
3677 {
3019eac3 3678 bfd *abfd;
8b70b953 3679 gdb_byte *info_ptr, *end_ptr;
348e048f 3680
8b70b953
TT
3681 dwarf2_read_section (objfile, section);
3682 info_ptr = section->buffer;
348e048f 3683
8b70b953
TT
3684 if (info_ptr == NULL)
3685 continue;
348e048f 3686
3019eac3
DE
3687 /* We can't set abfd until now because the section may be empty or
3688 not present, in which case section->asection will be NULL. */
3689 abfd = section->asection->owner;
3690
8b70b953 3691 if (types_htab == NULL)
3019eac3
DE
3692 {
3693 if (dwo_file)
3694 types_htab = allocate_dwo_unit_table (objfile);
3695 else
3696 types_htab = allocate_signatured_type_table (objfile);
3697 }
348e048f 3698
dee91e82
DE
3699 /* We don't use init_cutu_and_read_dies_simple, or some such, here
3700 because we don't need to read any dies: the signature is in the
3701 header. */
8b70b953
TT
3702
3703 end_ptr = info_ptr + section->size;
3704 while (info_ptr < end_ptr)
3705 {
b64f50a1 3706 sect_offset offset;
3019eac3 3707 cu_offset type_offset_in_tu;
8b70b953 3708 ULONGEST signature;
52dc124a 3709 struct signatured_type *sig_type;
3019eac3 3710 struct dwo_unit *dwo_tu;
8b70b953
TT
3711 void **slot;
3712 gdb_byte *ptr = info_ptr;
9ff913ba 3713 struct comp_unit_head header;
dee91e82 3714 unsigned int length;
348e048f 3715
b64f50a1 3716 offset.sect_off = ptr - section->buffer;
348e048f 3717
8b70b953 3718 /* We need to read the type's signature in order to build the hash
9ff913ba 3719 table, but we don't need anything else just yet. */
348e048f 3720
4bdcc0c1
DE
3721 ptr = read_and_check_type_unit_head (&header, section,
3722 abbrev_section, ptr,
3019eac3 3723 &signature, &type_offset_in_tu);
6caca83c 3724
1ce1cefd 3725 length = get_cu_length (&header);
dee91e82 3726
6caca83c 3727 /* Skip dummy type units. */
dee91e82
DE
3728 if (ptr >= info_ptr + length
3729 || peek_abbrev_code (abfd, ptr) == 0)
6caca83c 3730 {
1ce1cefd 3731 info_ptr += length;
6caca83c
CC
3732 continue;
3733 }
8b70b953 3734
3019eac3
DE
3735 if (dwo_file)
3736 {
3737 sig_type = NULL;
3738 dwo_tu = OBSTACK_ZALLOC (&objfile->objfile_obstack,
3739 struct dwo_unit);
3740 dwo_tu->dwo_file = dwo_file;
3741 dwo_tu->signature = signature;
3742 dwo_tu->type_offset_in_tu = type_offset_in_tu;
3743 dwo_tu->info_or_types_section = section;
3744 dwo_tu->offset = offset;
3745 dwo_tu->length = length;
3746 }
3747 else
3748 {
3749 /* N.B.: type_offset is not usable if this type uses a DWO file.
3750 The real type_offset is in the DWO file. */
3751 dwo_tu = NULL;
3752 sig_type = OBSTACK_ZALLOC (&objfile->objfile_obstack,
3753 struct signatured_type);
3754 sig_type->signature = signature;
3755 sig_type->type_offset_in_tu = type_offset_in_tu;
3756 sig_type->per_cu.objfile = objfile;
3757 sig_type->per_cu.is_debug_types = 1;
3758 sig_type->per_cu.info_or_types_section = section;
3759 sig_type->per_cu.offset = offset;
3760 sig_type->per_cu.length = length;
3761 }
8b70b953 3762
3019eac3
DE
3763 slot = htab_find_slot (types_htab,
3764 dwo_file ? (void*) dwo_tu : (void *) sig_type,
3765 INSERT);
8b70b953
TT
3766 gdb_assert (slot != NULL);
3767 if (*slot != NULL)
3768 {
3019eac3
DE
3769 sect_offset dup_offset;
3770
3771 if (dwo_file)
3772 {
3773 const struct dwo_unit *dup_tu = *slot;
3774
3775 dup_offset = dup_tu->offset;
3776 }
3777 else
3778 {
3779 const struct signatured_type *dup_tu = *slot;
3780
3781 dup_offset = dup_tu->per_cu.offset;
3782 }
b3c8eb43 3783
8b70b953
TT
3784 complaint (&symfile_complaints,
3785 _("debug type entry at offset 0x%x is duplicate to the "
3786 "entry at offset 0x%x, signature 0x%s"),
3019eac3 3787 offset.sect_off, dup_offset.sect_off,
8b70b953 3788 phex (signature, sizeof (signature)));
8b70b953 3789 }
3019eac3 3790 *slot = dwo_file ? (void *) dwo_tu : (void *) sig_type;
348e048f 3791
09406207 3792 if (dwarf2_read_debug)
8b70b953 3793 fprintf_unfiltered (gdb_stdlog, " offset 0x%x, signature 0x%s\n",
b64f50a1
JK
3794 offset.sect_off,
3795 phex (signature, sizeof (signature)));
348e048f 3796
dee91e82 3797 info_ptr += length;
8b70b953 3798 }
348e048f
DE
3799 }
3800
3019eac3
DE
3801 return types_htab;
3802}
3803
3804/* Create the hash table of all entries in the .debug_types section,
3805 and initialize all_type_units.
3806 The result is zero if there is an error (e.g. missing .debug_types section),
3807 otherwise non-zero. */
3808
3809static int
3810create_all_type_units (struct objfile *objfile)
3811{
3812 htab_t types_htab;
b4dd5633 3813 struct signatured_type **iter;
3019eac3
DE
3814
3815 types_htab = create_debug_types_hash_table (NULL, dwarf2_per_objfile->types);
3816 if (types_htab == NULL)
3817 {
3818 dwarf2_per_objfile->signatured_types = NULL;
3819 return 0;
3820 }
3821
348e048f
DE
3822 dwarf2_per_objfile->signatured_types = types_htab;
3823
d467dd73
DE
3824 dwarf2_per_objfile->n_type_units = htab_elements (types_htab);
3825 dwarf2_per_objfile->all_type_units
1fd400ff 3826 = obstack_alloc (&objfile->objfile_obstack,
d467dd73 3827 dwarf2_per_objfile->n_type_units
b4dd5633 3828 * sizeof (struct signatured_type *));
d467dd73
DE
3829 iter = &dwarf2_per_objfile->all_type_units[0];
3830 htab_traverse_noresize (types_htab, add_signatured_type_cu_to_table, &iter);
3831 gdb_assert (iter - &dwarf2_per_objfile->all_type_units[0]
3832 == dwarf2_per_objfile->n_type_units);
1fd400ff 3833
348e048f
DE
3834 return 1;
3835}
3836
380bca97 3837/* Lookup a signature based type for DW_FORM_ref_sig8.
e319fa28 3838 Returns NULL if signature SIG is not present in the table. */
348e048f
DE
3839
3840static struct signatured_type *
e319fa28 3841lookup_signatured_type (ULONGEST sig)
348e048f
DE
3842{
3843 struct signatured_type find_entry, *entry;
3844
3845 if (dwarf2_per_objfile->signatured_types == NULL)
3846 {
3847 complaint (&symfile_complaints,
55f1336d 3848 _("missing `.debug_types' section for DW_FORM_ref_sig8 die"));
dcc07052 3849 return NULL;
348e048f
DE
3850 }
3851
3852 find_entry.signature = sig;
3853 entry = htab_find (dwarf2_per_objfile->signatured_types, &find_entry);
3854 return entry;
3855}
42e7ad6c
DE
3856\f
3857/* Low level DIE reading support. */
348e048f 3858
d85a05f0
DJ
3859/* Initialize a die_reader_specs struct from a dwarf2_cu struct. */
3860
3861static void
3862init_cu_die_reader (struct die_reader_specs *reader,
dee91e82 3863 struct dwarf2_cu *cu,
3019eac3
DE
3864 struct dwarf2_section_info *section,
3865 struct dwo_file *dwo_file)
d85a05f0 3866{
fceca515 3867 gdb_assert (section->readin && section->buffer != NULL);
dee91e82 3868 reader->abfd = section->asection->owner;
d85a05f0 3869 reader->cu = cu;
3019eac3 3870 reader->dwo_file = dwo_file;
dee91e82
DE
3871 reader->die_section = section;
3872 reader->buffer = section->buffer;
f664829e 3873 reader->buffer_end = section->buffer + section->size;
d85a05f0
DJ
3874}
3875
fd820528 3876/* Initialize a CU (or TU) and read its DIEs.
3019eac3 3877 If the CU defers to a DWO file, read the DWO file as well.
dee91e82
DE
3878
3879 If USE_EXISTING_CU is non-zero, and THIS_CU->cu is non-NULL, then use it.
3880 Otherwise, a new CU is allocated with xmalloc.
3881
3882 If KEEP is non-zero, then if we allocated a dwarf2_cu we add it to
3883 read_in_chain. Otherwise the dwarf2_cu data is freed at the end.
3884
3885 WARNING: If THIS_CU is a "dummy CU" (used as filler by the incremental
fd820528 3886 linker) then DIE_READER_FUNC will not get called. */
aaa75496 3887
70221824 3888static void
fd820528
DE
3889init_cutu_and_read_dies (struct dwarf2_per_cu_data *this_cu,
3890 int use_existing_cu, int keep,
3891 die_reader_func_ftype *die_reader_func,
3892 void *data)
c906108c 3893{
dee91e82 3894 struct objfile *objfile = dwarf2_per_objfile->objfile;
3019eac3
DE
3895 struct dwarf2_section_info *section = this_cu->info_or_types_section;
3896 bfd *abfd = section->asection->owner;
dee91e82
DE
3897 struct dwarf2_cu *cu;
3898 gdb_byte *begin_info_ptr, *info_ptr;
3899 struct die_reader_specs reader;
d85a05f0 3900 struct die_info *comp_unit_die;
dee91e82 3901 int has_children;
d85a05f0 3902 struct attribute *attr;
dee91e82
DE
3903 struct cleanup *cleanups, *free_cu_cleanup = NULL;
3904 struct signatured_type *sig_type = NULL;
4bdcc0c1 3905 struct dwarf2_section_info *abbrev_section;
42e7ad6c
DE
3906 /* Non-zero if CU currently points to a DWO file and we need to
3907 reread it. When this happens we need to reread the skeleton die
3908 before we can reread the DWO file. */
3909 int rereading_dwo_cu = 0;
c906108c 3910
09406207
DE
3911 if (dwarf2_die_debug)
3912 fprintf_unfiltered (gdb_stdlog, "Reading %s unit at offset 0x%x\n",
3913 this_cu->is_debug_types ? "type" : "comp",
3914 this_cu->offset.sect_off);
3915
dee91e82
DE
3916 if (use_existing_cu)
3917 gdb_assert (keep);
23745b47 3918
dee91e82
DE
3919 cleanups = make_cleanup (null_cleanup, NULL);
3920
3921 /* This is cheap if the section is already read in. */
3922 dwarf2_read_section (objfile, section);
3923
3924 begin_info_ptr = info_ptr = section->buffer + this_cu->offset.sect_off;
4bdcc0c1 3925 abbrev_section = &dwarf2_per_objfile->abbrev;
dee91e82
DE
3926
3927 if (use_existing_cu && this_cu->cu != NULL)
3928 {
3929 cu = this_cu->cu;
42e7ad6c
DE
3930
3931 /* If this CU is from a DWO file we need to start over, we need to
3932 refetch the attributes from the skeleton CU.
3933 This could be optimized by retrieving those attributes from when we
3934 were here the first time: the previous comp_unit_die was stored in
3935 comp_unit_obstack. But there's no data yet that we need this
3936 optimization. */
3937 if (cu->dwo_unit != NULL)
3938 rereading_dwo_cu = 1;
dee91e82
DE
3939 }
3940 else
3941 {
3942 /* If !use_existing_cu, this_cu->cu must be NULL. */
3943 gdb_assert (this_cu->cu == NULL);
3944
3945 cu = xmalloc (sizeof (*cu));
3946 init_one_comp_unit (cu, this_cu);
3947
3948 /* If an error occurs while loading, release our storage. */
3949 free_cu_cleanup = make_cleanup (free_heap_comp_unit, cu);
42e7ad6c 3950 }
dee91e82 3951
42e7ad6c
DE
3952 if (cu->header.first_die_offset.cu_off != 0 && ! rereading_dwo_cu)
3953 {
3954 /* We already have the header, there's no need to read it in again. */
3955 info_ptr += cu->header.first_die_offset.cu_off;
3956 }
3957 else
3958 {
3019eac3 3959 if (this_cu->is_debug_types)
dee91e82
DE
3960 {
3961 ULONGEST signature;
42e7ad6c 3962 cu_offset type_offset_in_tu;
dee91e82 3963
4bdcc0c1
DE
3964 info_ptr = read_and_check_type_unit_head (&cu->header, section,
3965 abbrev_section, info_ptr,
42e7ad6c
DE
3966 &signature,
3967 &type_offset_in_tu);
dee91e82 3968
42e7ad6c
DE
3969 /* Since per_cu is the first member of struct signatured_type,
3970 we can go from a pointer to one to a pointer to the other. */
3971 sig_type = (struct signatured_type *) this_cu;
3972 gdb_assert (sig_type->signature == signature);
3973 gdb_assert (sig_type->type_offset_in_tu.cu_off
3974 == type_offset_in_tu.cu_off);
dee91e82
DE
3975 gdb_assert (this_cu->offset.sect_off == cu->header.offset.sect_off);
3976
42e7ad6c
DE
3977 /* LENGTH has not been set yet for type units if we're
3978 using .gdb_index. */
1ce1cefd 3979 this_cu->length = get_cu_length (&cu->header);
3019eac3
DE
3980
3981 /* Establish the type offset that can be used to lookup the type. */
3982 sig_type->type_offset_in_section.sect_off =
3983 this_cu->offset.sect_off + sig_type->type_offset_in_tu.cu_off;
dee91e82
DE
3984 }
3985 else
3986 {
4bdcc0c1
DE
3987 info_ptr = read_and_check_comp_unit_head (&cu->header, section,
3988 abbrev_section,
3989 info_ptr, 0);
dee91e82
DE
3990
3991 gdb_assert (this_cu->offset.sect_off == cu->header.offset.sect_off);
1ce1cefd 3992 gdb_assert (this_cu->length == get_cu_length (&cu->header));
dee91e82
DE
3993 }
3994 }
10b3939b 3995
6caca83c 3996 /* Skip dummy compilation units. */
dee91e82 3997 if (info_ptr >= begin_info_ptr + this_cu->length
6caca83c
CC
3998 || peek_abbrev_code (abfd, info_ptr) == 0)
3999 {
dee91e82 4000 do_cleanups (cleanups);
21b2bd31 4001 return;
6caca83c
CC
4002 }
4003
433df2d4
DE
4004 /* If we don't have them yet, read the abbrevs for this compilation unit.
4005 And if we need to read them now, make sure they're freed when we're
42e7ad6c
DE
4006 done. Note that it's important that if the CU had an abbrev table
4007 on entry we don't free it when we're done: Somewhere up the call stack
4008 it may be in use. */
433df2d4 4009 if (cu->abbrev_table == NULL)
dee91e82 4010 {
4bdcc0c1 4011 dwarf2_read_abbrevs (cu, abbrev_section);
dee91e82
DE
4012 make_cleanup (dwarf2_free_abbrev_table, cu);
4013 }
42e7ad6c
DE
4014 else if (rereading_dwo_cu)
4015 {
4016 dwarf2_free_abbrev_table (cu);
4017 dwarf2_read_abbrevs (cu, abbrev_section);
4018 }
af703f96 4019
dee91e82 4020 /* Read the top level CU/TU die. */
3019eac3 4021 init_cu_die_reader (&reader, cu, section, NULL);
dee91e82 4022 info_ptr = read_full_die (&reader, &comp_unit_die, info_ptr, &has_children);
93311388 4023
3019eac3
DE
4024 /* If we have a DWO stub, process it and then read in the DWO file.
4025 Note that if USE_EXISTING_OK != 0, and THIS_CU->cu already contains
4026 a DWO CU, that this test will fail. */
4027 attr = dwarf2_attr (comp_unit_die, DW_AT_GNU_dwo_name, cu);
4028 if (attr)
4029 {
4030 char *dwo_name = DW_STRING (attr);
42e7ad6c 4031 const char *comp_dir_string;
3019eac3
DE
4032 struct dwo_unit *dwo_unit;
4033 ULONGEST signature; /* Or dwo_id. */
42e7ad6c 4034 struct attribute *comp_dir, *stmt_list, *low_pc, *high_pc, *ranges;
3019eac3 4035 int i,num_extra_attrs;
4bdcc0c1 4036 struct dwarf2_section_info *dwo_abbrev_section;
3019eac3
DE
4037
4038 if (has_children)
4039 error (_("Dwarf Error: compilation unit with DW_AT_GNU_dwo_name"
4040 " has children (offset 0x%x) [in module %s]"),
4041 this_cu->offset.sect_off, bfd_get_filename (abfd));
4042
4043 /* These attributes aren't processed until later:
4044 DW_AT_stmt_list, DW_AT_low_pc, DW_AT_high_pc, DW_AT_ranges.
4045 However, the attribute is found in the stub which we won't have later.
4046 In order to not impose this complication on the rest of the code,
4047 we read them here and copy them to the DWO CU/TU die. */
3019eac3
DE
4048
4049 /* For TUs in DWO files, the DW_AT_stmt_list attribute lives in the
4050 DWO file. */
42e7ad6c 4051 stmt_list = NULL;
3019eac3
DE
4052 if (! this_cu->is_debug_types)
4053 stmt_list = dwarf2_attr (comp_unit_die, DW_AT_stmt_list, cu);
4054 low_pc = dwarf2_attr (comp_unit_die, DW_AT_low_pc, cu);
4055 high_pc = dwarf2_attr (comp_unit_die, DW_AT_high_pc, cu);
4056 ranges = dwarf2_attr (comp_unit_die, DW_AT_ranges, cu);
42e7ad6c 4057 comp_dir = dwarf2_attr (comp_unit_die, DW_AT_comp_dir, cu);
3019eac3
DE
4058
4059 /* There should be a DW_AT_addr_base attribute here (if needed).
4060 We need the value before we can process DW_FORM_GNU_addr_index. */
4061 cu->addr_base = 0;
3019eac3
DE
4062 attr = dwarf2_attr (comp_unit_die, DW_AT_GNU_addr_base, cu);
4063 if (attr)
2e3cf129
DE
4064 cu->addr_base = DW_UNSND (attr);
4065
4066 /* There should be a DW_AT_ranges_base attribute here (if needed).
4067 We need the value before we can process DW_AT_ranges. */
4068 cu->ranges_base = 0;
4069 attr = dwarf2_attr (comp_unit_die, DW_AT_GNU_ranges_base, cu);
4070 if (attr)
4071 cu->ranges_base = DW_UNSND (attr);
3019eac3
DE
4072
4073 if (this_cu->is_debug_types)
4074 {
4075 gdb_assert (sig_type != NULL);
4076 signature = sig_type->signature;
4077 }
4078 else
4079 {
4080 attr = dwarf2_attr (comp_unit_die, DW_AT_GNU_dwo_id, cu);
4081 if (! attr)
4082 error (_("Dwarf Error: missing dwo_id [in module %s]"),
4083 dwo_name);
4084 signature = DW_UNSND (attr);
4085 }
4086
4087 /* We may need the comp_dir in order to find the DWO file. */
42e7ad6c
DE
4088 comp_dir_string = NULL;
4089 if (comp_dir)
4090 comp_dir_string = DW_STRING (comp_dir);
3019eac3
DE
4091
4092 if (this_cu->is_debug_types)
42e7ad6c 4093 dwo_unit = lookup_dwo_type_unit (sig_type, dwo_name, comp_dir_string);
3019eac3 4094 else
42e7ad6c 4095 dwo_unit = lookup_dwo_comp_unit (this_cu, dwo_name, comp_dir_string,
3019eac3
DE
4096 signature);
4097
4098 if (dwo_unit == NULL)
4099 {
4100 error (_("Dwarf Error: CU at offset 0x%x references unknown DWO"
4101 " with ID %s [in module %s]"),
4102 this_cu->offset.sect_off,
4103 phex (signature, sizeof (signature)),
4104 objfile->name);
4105 }
4106
4107 /* Set up for reading the DWO CU/TU. */
4108 cu->dwo_unit = dwo_unit;
4109 section = dwo_unit->info_or_types_section;
4110 begin_info_ptr = info_ptr = section->buffer + dwo_unit->offset.sect_off;
4bdcc0c1 4111 dwo_abbrev_section = &dwo_unit->dwo_file->sections.abbrev;
3019eac3
DE
4112 init_cu_die_reader (&reader, cu, section, dwo_unit->dwo_file);
4113
4114 if (this_cu->is_debug_types)
4115 {
4116 ULONGEST signature;
4117
4bdcc0c1
DE
4118 info_ptr = read_and_check_type_unit_head (&cu->header, section,
4119 dwo_abbrev_section,
4120 info_ptr,
3019eac3
DE
4121 &signature, NULL);
4122 gdb_assert (sig_type->signature == signature);
4123 gdb_assert (dwo_unit->offset.sect_off == cu->header.offset.sect_off);
1ce1cefd 4124 gdb_assert (dwo_unit->length == get_cu_length (&cu->header));
3019eac3
DE
4125
4126 /* Establish the type offset that can be used to lookup the type.
4127 For DWO files, we don't know it until now. */
4128 sig_type->type_offset_in_section.sect_off =
4129 dwo_unit->offset.sect_off + dwo_unit->type_offset_in_tu.cu_off;
4130 }
4131 else
4132 {
4bdcc0c1
DE
4133 info_ptr = read_and_check_comp_unit_head (&cu->header, section,
4134 dwo_abbrev_section,
4135 info_ptr, 0);
3019eac3 4136 gdb_assert (dwo_unit->offset.sect_off == cu->header.offset.sect_off);
1ce1cefd 4137 gdb_assert (dwo_unit->length == get_cu_length (&cu->header));
3019eac3
DE
4138 }
4139
4140 /* Discard the original CU's abbrev table, and read the DWO's. */
4141 dwarf2_free_abbrev_table (cu);
4bdcc0c1 4142 dwarf2_read_abbrevs (cu, dwo_abbrev_section);
3019eac3
DE
4143
4144 /* Read in the die, but leave space to copy over the attributes
4145 from the stub. This has the benefit of simplifying the rest of
4146 the code - all the real work is done here. */
4147 num_extra_attrs = ((stmt_list != NULL)
4148 + (low_pc != NULL)
4149 + (high_pc != NULL)
42e7ad6c
DE
4150 + (ranges != NULL)
4151 + (comp_dir != NULL));
3019eac3
DE
4152 info_ptr = read_full_die_1 (&reader, &comp_unit_die, info_ptr,
4153 &has_children, num_extra_attrs);
4154
4155 /* Copy over the attributes from the stub to the DWO die. */
4156 i = comp_unit_die->num_attrs;
4157 if (stmt_list != NULL)
4158 comp_unit_die->attrs[i++] = *stmt_list;
4159 if (low_pc != NULL)
4160 comp_unit_die->attrs[i++] = *low_pc;
4161 if (high_pc != NULL)
4162 comp_unit_die->attrs[i++] = *high_pc;
4163 if (ranges != NULL)
4164 comp_unit_die->attrs[i++] = *ranges;
42e7ad6c
DE
4165 if (comp_dir != NULL)
4166 comp_unit_die->attrs[i++] = *comp_dir;
3019eac3
DE
4167 comp_unit_die->num_attrs += num_extra_attrs;
4168
4169 /* Skip dummy compilation units. */
4170 if (info_ptr >= begin_info_ptr + dwo_unit->length
4171 || peek_abbrev_code (abfd, info_ptr) == 0)
4172 {
4173 do_cleanups (cleanups);
4174 return;
4175 }
4176 }
4177
dee91e82
DE
4178 die_reader_func (&reader, info_ptr, comp_unit_die, has_children, data);
4179
4180 if (free_cu_cleanup != NULL)
348e048f 4181 {
dee91e82
DE
4182 if (keep)
4183 {
4184 /* We've successfully allocated this compilation unit. Let our
4185 caller clean it up when finished with it. */
4186 discard_cleanups (free_cu_cleanup);
4187
4188 /* We can only discard free_cu_cleanup and all subsequent cleanups.
4189 So we have to manually free the abbrev table. */
4190 dwarf2_free_abbrev_table (cu);
4191
4192 /* Link this CU into read_in_chain. */
4193 this_cu->cu->read_in_chain = dwarf2_per_objfile->read_in_chain;
4194 dwarf2_per_objfile->read_in_chain = this_cu;
4195 }
4196 else
4197 do_cleanups (free_cu_cleanup);
348e048f 4198 }
dee91e82
DE
4199
4200 do_cleanups (cleanups);
4201}
4202
3019eac3
DE
4203/* Read CU/TU THIS_CU in section SECTION,
4204 but do not follow DW_AT_GNU_dwo_name if present.
4205 DWO_FILE, if non-NULL, is the DWO file to read (the caller is assumed to
4206 have already done the lookup to find the DWO file).
dee91e82
DE
4207
4208 The caller is required to fill in THIS_CU->section, THIS_CU->offset, and
3019eac3 4209 THIS_CU->is_debug_types, but nothing else.
dee91e82
DE
4210
4211 We fill in THIS_CU->length.
4212
4213 WARNING: If THIS_CU is a "dummy CU" (used as filler by the incremental
4214 linker) then DIE_READER_FUNC will not get called.
4215
4216 THIS_CU->cu is always freed when done.
3019eac3
DE
4217 This is done in order to not leave THIS_CU->cu in a state where we have
4218 to care whether it refers to the "main" CU or the DWO CU. */
dee91e82
DE
4219
4220static void
4221init_cutu_and_read_dies_no_follow (struct dwarf2_per_cu_data *this_cu,
4222 struct dwarf2_section_info *abbrev_section,
3019eac3 4223 struct dwo_file *dwo_file,
dee91e82
DE
4224 die_reader_func_ftype *die_reader_func,
4225 void *data)
4226{
4227 struct objfile *objfile = dwarf2_per_objfile->objfile;
3019eac3
DE
4228 struct dwarf2_section_info *section = this_cu->info_or_types_section;
4229 bfd *abfd = section->asection->owner;
dee91e82
DE
4230 struct dwarf2_cu cu;
4231 gdb_byte *begin_info_ptr, *info_ptr;
4232 struct die_reader_specs reader;
4233 struct cleanup *cleanups;
4234 struct die_info *comp_unit_die;
4235 int has_children;
4236
09406207
DE
4237 if (dwarf2_die_debug)
4238 fprintf_unfiltered (gdb_stdlog, "Reading %s unit at offset 0x%x\n",
4239 this_cu->is_debug_types ? "type" : "comp",
4240 this_cu->offset.sect_off);
4241
dee91e82
DE
4242 gdb_assert (this_cu->cu == NULL);
4243
dee91e82
DE
4244 /* This is cheap if the section is already read in. */
4245 dwarf2_read_section (objfile, section);
4246
4247 init_one_comp_unit (&cu, this_cu);
4248
4249 cleanups = make_cleanup (free_stack_comp_unit, &cu);
4250
4251 begin_info_ptr = info_ptr = section->buffer + this_cu->offset.sect_off;
4bdcc0c1
DE
4252 info_ptr = read_and_check_comp_unit_head (&cu.header, section,
4253 abbrev_section, info_ptr,
3019eac3 4254 this_cu->is_debug_types);
dee91e82 4255
1ce1cefd 4256 this_cu->length = get_cu_length (&cu.header);
dee91e82
DE
4257
4258 /* Skip dummy compilation units. */
4259 if (info_ptr >= begin_info_ptr + this_cu->length
4260 || peek_abbrev_code (abfd, info_ptr) == 0)
c906108c 4261 {
dee91e82 4262 do_cleanups (cleanups);
21b2bd31 4263 return;
93311388 4264 }
72bf9492 4265
dee91e82
DE
4266 dwarf2_read_abbrevs (&cu, abbrev_section);
4267 make_cleanup (dwarf2_free_abbrev_table, &cu);
4268
3019eac3 4269 init_cu_die_reader (&reader, &cu, section, dwo_file);
dee91e82
DE
4270 info_ptr = read_full_die (&reader, &comp_unit_die, info_ptr, &has_children);
4271
4272 die_reader_func (&reader, info_ptr, comp_unit_die, has_children, data);
4273
4274 do_cleanups (cleanups);
4275}
4276
3019eac3
DE
4277/* Read a CU/TU, except that this does not look for DW_AT_GNU_dwo_name and
4278 does not lookup the specified DWO file.
4279 This cannot be used to read DWO files.
dee91e82
DE
4280
4281 THIS_CU->cu is always freed when done.
3019eac3
DE
4282 This is done in order to not leave THIS_CU->cu in a state where we have
4283 to care whether it refers to the "main" CU or the DWO CU.
4284 We can revisit this if the data shows there's a performance issue. */
dee91e82
DE
4285
4286static void
4287init_cutu_and_read_dies_simple (struct dwarf2_per_cu_data *this_cu,
4288 die_reader_func_ftype *die_reader_func,
4289 void *data)
4290{
4291 init_cutu_and_read_dies_no_follow (this_cu,
4292 &dwarf2_per_objfile->abbrev,
3019eac3 4293 NULL,
dee91e82
DE
4294 die_reader_func, data);
4295}
4296
4297/* die_reader_func for process_psymtab_comp_unit. */
4298
4299static void
4300process_psymtab_comp_unit_reader (const struct die_reader_specs *reader,
4301 gdb_byte *info_ptr,
4302 struct die_info *comp_unit_die,
4303 int has_children,
4304 void *data)
4305{
4306 struct dwarf2_cu *cu = reader->cu;
4307 struct objfile *objfile = cu->objfile;
4308 struct dwarf2_per_cu_data *per_cu = cu->per_cu;
dee91e82
DE
4309 struct attribute *attr;
4310 CORE_ADDR baseaddr;
4311 CORE_ADDR best_lowpc = 0, best_highpc = 0;
4312 struct partial_symtab *pst;
4313 int has_pc_info;
4314 const char *filename;
95554aad 4315 int *want_partial_unit_ptr = data;
dee91e82 4316
95554aad
TT
4317 if (comp_unit_die->tag == DW_TAG_partial_unit
4318 && (want_partial_unit_ptr == NULL
4319 || !*want_partial_unit_ptr))
dee91e82
DE
4320 return;
4321
95554aad 4322 prepare_one_comp_unit (cu, comp_unit_die, language_minimal);
dee91e82
DE
4323
4324 cu->list_in_scope = &file_symbols;
c906108c 4325
93311388 4326 /* Allocate a new partial symbol table structure. */
dee91e82 4327 attr = dwarf2_attr (comp_unit_die, DW_AT_name, cu);
3e2a0cee
TT
4328 if (attr == NULL || !DW_STRING (attr))
4329 filename = "";
4330 else
4331 filename = DW_STRING (attr);
93311388 4332 pst = start_psymtab_common (objfile, objfile->section_offsets,
3e2a0cee 4333 filename,
93311388
DE
4334 /* TEXTLOW and TEXTHIGH are set below. */
4335 0,
4336 objfile->global_psymbols.next,
4337 objfile->static_psymbols.next);
9750bca9 4338 pst->psymtabs_addrmap_supported = 1;
72bf9492 4339
dee91e82 4340 attr = dwarf2_attr (comp_unit_die, DW_AT_comp_dir, cu);
d85a05f0
DJ
4341 if (attr != NULL)
4342 pst->dirname = DW_STRING (attr);
72bf9492 4343
dee91e82 4344 pst->read_symtab_private = per_cu;
72bf9492 4345
93311388 4346 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
e7c27a73 4347
0963b4bd 4348 /* Store the function that reads in the rest of the symbol table. */
93311388 4349 pst->read_symtab = dwarf2_psymtab_to_symtab;
57349743 4350
dee91e82 4351 per_cu->v.psymtab = pst;
c906108c 4352
dee91e82 4353 dwarf2_find_base_address (comp_unit_die, cu);
d85a05f0 4354
93311388
DE
4355 /* Possibly set the default values of LOWPC and HIGHPC from
4356 `DW_AT_ranges'. */
d85a05f0 4357 has_pc_info = dwarf2_get_pc_bounds (comp_unit_die, &best_lowpc,
dee91e82 4358 &best_highpc, cu, pst);
d85a05f0 4359 if (has_pc_info == 1 && best_lowpc < best_highpc)
93311388
DE
4360 /* Store the contiguous range if it is not empty; it can be empty for
4361 CUs with no code. */
4362 addrmap_set_empty (objfile->psymtabs_addrmap,
d85a05f0
DJ
4363 best_lowpc + baseaddr,
4364 best_highpc + baseaddr - 1, pst);
93311388
DE
4365
4366 /* Check if comp unit has_children.
4367 If so, read the rest of the partial symbols from this comp unit.
0963b4bd 4368 If not, there's no more debug_info for this comp unit. */
d85a05f0 4369 if (has_children)
93311388
DE
4370 {
4371 struct partial_die_info *first_die;
4372 CORE_ADDR lowpc, highpc;
31ffec48 4373
93311388
DE
4374 lowpc = ((CORE_ADDR) -1);
4375 highpc = ((CORE_ADDR) 0);
c906108c 4376
dee91e82 4377 first_die = load_partial_dies (reader, info_ptr, 1);
c906108c 4378
93311388 4379 scan_partial_symbols (first_die, &lowpc, &highpc,
dee91e82 4380 ! has_pc_info, cu);
57c22c6c 4381
93311388
DE
4382 /* If we didn't find a lowpc, set it to highpc to avoid
4383 complaints from `maint check'. */
4384 if (lowpc == ((CORE_ADDR) -1))
4385 lowpc = highpc;
10b3939b 4386
93311388
DE
4387 /* If the compilation unit didn't have an explicit address range,
4388 then use the information extracted from its child dies. */
d85a05f0 4389 if (! has_pc_info)
93311388 4390 {
d85a05f0
DJ
4391 best_lowpc = lowpc;
4392 best_highpc = highpc;
93311388
DE
4393 }
4394 }
d85a05f0
DJ
4395 pst->textlow = best_lowpc + baseaddr;
4396 pst->texthigh = best_highpc + baseaddr;
c906108c 4397
93311388
DE
4398 pst->n_global_syms = objfile->global_psymbols.next -
4399 (objfile->global_psymbols.list + pst->globals_offset);
4400 pst->n_static_syms = objfile->static_psymbols.next -
4401 (objfile->static_psymbols.list + pst->statics_offset);
4402 sort_pst_symbols (pst);
c906108c 4403
95554aad
TT
4404 if (!VEC_empty (dwarf2_per_cu_ptr, cu->per_cu->imported_symtabs))
4405 {
4406 int i;
4407 int len = VEC_length (dwarf2_per_cu_ptr, cu->per_cu->imported_symtabs);
4408 struct dwarf2_per_cu_data *iter;
4409
4410 /* Fill in 'dependencies' here; we fill in 'users' in a
4411 post-pass. */
4412 pst->number_of_dependencies = len;
4413 pst->dependencies = obstack_alloc (&objfile->objfile_obstack,
4414 len * sizeof (struct symtab *));
4415 for (i = 0;
4416 VEC_iterate (dwarf2_per_cu_ptr, cu->per_cu->imported_symtabs,
4417 i, iter);
4418 ++i)
4419 pst->dependencies[i] = iter->v.psymtab;
4420
4421 VEC_free (dwarf2_per_cu_ptr, cu->per_cu->imported_symtabs);
4422 }
4423
3019eac3 4424 if (per_cu->is_debug_types)
348e048f
DE
4425 {
4426 /* It's not clear we want to do anything with stmt lists here.
4427 Waiting to see what gcc ultimately does. */
4428 }
d85a05f0 4429 else
93311388
DE
4430 {
4431 /* Get the list of files included in the current compilation unit,
4432 and build a psymtab for each of them. */
dee91e82 4433 dwarf2_build_include_psymtabs (cu, comp_unit_die, pst);
93311388 4434 }
09406207
DE
4435
4436 if (dwarf2_read_debug)
4437 {
4438 struct gdbarch *gdbarch = get_objfile_arch (objfile);
4439
4440 fprintf_unfiltered (gdb_stdlog,
4441 "Psymtab for %s unit @0x%x: 0x%s - 0x%s"
4442 ", %d global, %d static syms\n",
4443 per_cu->is_debug_types ? "type" : "comp",
4444 per_cu->offset.sect_off,
4445 paddress (gdbarch, pst->textlow),
4446 paddress (gdbarch, pst->texthigh),
4447 pst->n_global_syms, pst->n_static_syms);
4448 }
dee91e82 4449}
ae038cb0 4450
dee91e82
DE
4451/* Subroutine of dwarf2_build_psymtabs_hard to simplify it.
4452 Process compilation unit THIS_CU for a psymtab. */
4453
4454static void
95554aad
TT
4455process_psymtab_comp_unit (struct dwarf2_per_cu_data *this_cu,
4456 int want_partial_unit)
dee91e82
DE
4457{
4458 /* If this compilation unit was already read in, free the
4459 cached copy in order to read it in again. This is
4460 necessary because we skipped some symbols when we first
4461 read in the compilation unit (see load_partial_dies).
4462 This problem could be avoided, but the benefit is unclear. */
4463 if (this_cu->cu != NULL)
4464 free_one_cached_comp_unit (this_cu);
4465
3019eac3 4466 gdb_assert (! this_cu->is_debug_types);
fd820528 4467 init_cutu_and_read_dies (this_cu, 0, 0, process_psymtab_comp_unit_reader,
95554aad 4468 &want_partial_unit);
dee91e82
DE
4469
4470 /* Age out any secondary CUs. */
4471 age_cached_comp_units ();
93311388 4472}
ff013f42 4473
348e048f
DE
4474/* Traversal function for htab_traverse_noresize.
4475 Process one .debug_types comp-unit. */
4476
4477static int
dee91e82 4478process_psymtab_type_unit (void **slot, void *info)
348e048f 4479{
dee91e82
DE
4480 struct signatured_type *sig_type = (struct signatured_type *) *slot;
4481 struct dwarf2_per_cu_data *per_cu = &sig_type->per_cu;
348e048f 4482
fd820528 4483 gdb_assert (per_cu->is_debug_types);
a0f42c21 4484 gdb_assert (info == NULL);
348e048f 4485
dee91e82
DE
4486 /* If this compilation unit was already read in, free the
4487 cached copy in order to read it in again. This is
4488 necessary because we skipped some symbols when we first
4489 read in the compilation unit (see load_partial_dies).
4490 This problem could be avoided, but the benefit is unclear. */
4491 if (per_cu->cu != NULL)
4492 free_one_cached_comp_unit (per_cu);
4493
fd820528
DE
4494 init_cutu_and_read_dies (per_cu, 0, 0, process_psymtab_comp_unit_reader,
4495 NULL);
dee91e82
DE
4496
4497 /* Age out any secondary CUs. */
4498 age_cached_comp_units ();
348e048f
DE
4499
4500 return 1;
4501}
4502
4503/* Subroutine of dwarf2_build_psymtabs_hard to simplify it.
4504 Build partial symbol tables for the .debug_types comp-units. */
4505
4506static void
4507build_type_psymtabs (struct objfile *objfile)
4508{
0e50663e 4509 if (! create_all_type_units (objfile))
348e048f
DE
4510 return;
4511
4512 htab_traverse_noresize (dwarf2_per_objfile->signatured_types,
dee91e82 4513 process_psymtab_type_unit, NULL);
348e048f
DE
4514}
4515
60606b2c
TT
4516/* A cleanup function that clears objfile's psymtabs_addrmap field. */
4517
4518static void
4519psymtabs_addrmap_cleanup (void *o)
4520{
4521 struct objfile *objfile = o;
ec61707d 4522
60606b2c
TT
4523 objfile->psymtabs_addrmap = NULL;
4524}
4525
95554aad
TT
4526/* Compute the 'user' field for each psymtab in OBJFILE. */
4527
4528static void
4529set_partial_user (struct objfile *objfile)
4530{
4531 int i;
4532
4533 for (i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
4534 {
4535 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
4536 struct partial_symtab *pst = per_cu->v.psymtab;
4537 int j;
4538
4539 for (j = 0; j < pst->number_of_dependencies; ++j)
4540 {
4541 /* Set the 'user' field only if it is not already set. */
4542 if (pst->dependencies[j]->user == NULL)
4543 pst->dependencies[j]->user = pst;
4544 }
4545 }
4546}
4547
93311388
DE
4548/* Build the partial symbol table by doing a quick pass through the
4549 .debug_info and .debug_abbrev sections. */
72bf9492 4550
93311388 4551static void
c67a9c90 4552dwarf2_build_psymtabs_hard (struct objfile *objfile)
93311388 4553{
60606b2c
TT
4554 struct cleanup *back_to, *addrmap_cleanup;
4555 struct obstack temp_obstack;
21b2bd31 4556 int i;
93311388 4557
45cfd468
DE
4558 if (dwarf2_read_debug)
4559 {
4560 fprintf_unfiltered (gdb_stdlog, "Building psymtabs of objfile %s ...\n",
4561 objfile->name);
4562 }
4563
98bfdba5
PA
4564 dwarf2_per_objfile->reading_partial_symbols = 1;
4565
be391dca 4566 dwarf2_read_section (objfile, &dwarf2_per_objfile->info);
91c24f0a 4567
93311388
DE
4568 /* Any cached compilation units will be linked by the per-objfile
4569 read_in_chain. Make sure to free them when we're done. */
4570 back_to = make_cleanup (free_cached_comp_units, NULL);
72bf9492 4571
348e048f
DE
4572 build_type_psymtabs (objfile);
4573
93311388 4574 create_all_comp_units (objfile);
c906108c 4575
60606b2c
TT
4576 /* Create a temporary address map on a temporary obstack. We later
4577 copy this to the final obstack. */
4578 obstack_init (&temp_obstack);
4579 make_cleanup_obstack_free (&temp_obstack);
4580 objfile->psymtabs_addrmap = addrmap_create_mutable (&temp_obstack);
4581 addrmap_cleanup = make_cleanup (psymtabs_addrmap_cleanup, objfile);
72bf9492 4582
21b2bd31 4583 for (i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
93311388 4584 {
21b2bd31 4585 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
aaa75496 4586
95554aad 4587 process_psymtab_comp_unit (per_cu, 0);
c906108c 4588 }
ff013f42 4589
95554aad
TT
4590 set_partial_user (objfile);
4591
ff013f42
JK
4592 objfile->psymtabs_addrmap = addrmap_create_fixed (objfile->psymtabs_addrmap,
4593 &objfile->objfile_obstack);
60606b2c 4594 discard_cleanups (addrmap_cleanup);
ff013f42 4595
ae038cb0 4596 do_cleanups (back_to);
45cfd468
DE
4597
4598 if (dwarf2_read_debug)
4599 fprintf_unfiltered (gdb_stdlog, "Done building psymtabs of %s\n",
4600 objfile->name);
ae038cb0
DJ
4601}
4602
3019eac3 4603/* die_reader_func for load_partial_comp_unit. */
ae038cb0
DJ
4604
4605static void
dee91e82
DE
4606load_partial_comp_unit_reader (const struct die_reader_specs *reader,
4607 gdb_byte *info_ptr,
4608 struct die_info *comp_unit_die,
4609 int has_children,
4610 void *data)
ae038cb0 4611{
dee91e82 4612 struct dwarf2_cu *cu = reader->cu;
ae038cb0 4613
95554aad 4614 prepare_one_comp_unit (cu, comp_unit_die, language_minimal);
ae038cb0 4615
ae038cb0
DJ
4616 /* Check if comp unit has_children.
4617 If so, read the rest of the partial symbols from this comp unit.
0963b4bd 4618 If not, there's no more debug_info for this comp unit. */
d85a05f0 4619 if (has_children)
dee91e82
DE
4620 load_partial_dies (reader, info_ptr, 0);
4621}
98bfdba5 4622
dee91e82
DE
4623/* Load the partial DIEs for a secondary CU into memory.
4624 This is also used when rereading a primary CU with load_all_dies. */
c5b7e1cb 4625
dee91e82
DE
4626static void
4627load_partial_comp_unit (struct dwarf2_per_cu_data *this_cu)
4628{
fd820528 4629 init_cutu_and_read_dies (this_cu, 1, 1, load_partial_comp_unit_reader, NULL);
ae038cb0
DJ
4630}
4631
9cdd5dbd
DE
4632/* Create a list of all compilation units in OBJFILE.
4633 This is only done for -readnow and building partial symtabs. */
ae038cb0
DJ
4634
4635static void
4636create_all_comp_units (struct objfile *objfile)
4637{
4638 int n_allocated;
4639 int n_comp_units;
4640 struct dwarf2_per_cu_data **all_comp_units;
be391dca
TT
4641 gdb_byte *info_ptr;
4642
4643 dwarf2_read_section (objfile, &dwarf2_per_objfile->info);
4644 info_ptr = dwarf2_per_objfile->info.buffer;
ae038cb0
DJ
4645
4646 n_comp_units = 0;
4647 n_allocated = 10;
4648 all_comp_units = xmalloc (n_allocated
4649 * sizeof (struct dwarf2_per_cu_data *));
6e70227d 4650
3e43a32a
MS
4651 while (info_ptr < dwarf2_per_objfile->info.buffer
4652 + dwarf2_per_objfile->info.size)
ae038cb0 4653 {
c764a876 4654 unsigned int length, initial_length_size;
ae038cb0 4655 struct dwarf2_per_cu_data *this_cu;
b64f50a1 4656 sect_offset offset;
ae038cb0 4657
b64f50a1 4658 offset.sect_off = info_ptr - dwarf2_per_objfile->info.buffer;
ae038cb0
DJ
4659
4660 /* Read just enough information to find out where the next
4661 compilation unit is. */
c764a876
DE
4662 length = read_initial_length (objfile->obfd, info_ptr,
4663 &initial_length_size);
ae038cb0
DJ
4664
4665 /* Save the compilation unit for later lookup. */
4666 this_cu = obstack_alloc (&objfile->objfile_obstack,
4667 sizeof (struct dwarf2_per_cu_data));
4668 memset (this_cu, 0, sizeof (*this_cu));
4669 this_cu->offset = offset;
c764a876 4670 this_cu->length = length + initial_length_size;
9291a0cd 4671 this_cu->objfile = objfile;
3019eac3 4672 this_cu->info_or_types_section = &dwarf2_per_objfile->info;
ae038cb0
DJ
4673
4674 if (n_comp_units == n_allocated)
4675 {
4676 n_allocated *= 2;
4677 all_comp_units = xrealloc (all_comp_units,
4678 n_allocated
4679 * sizeof (struct dwarf2_per_cu_data *));
4680 }
4681 all_comp_units[n_comp_units++] = this_cu;
4682
4683 info_ptr = info_ptr + this_cu->length;
4684 }
4685
4686 dwarf2_per_objfile->all_comp_units
4687 = obstack_alloc (&objfile->objfile_obstack,
4688 n_comp_units * sizeof (struct dwarf2_per_cu_data *));
4689 memcpy (dwarf2_per_objfile->all_comp_units, all_comp_units,
4690 n_comp_units * sizeof (struct dwarf2_per_cu_data *));
4691 xfree (all_comp_units);
4692 dwarf2_per_objfile->n_comp_units = n_comp_units;
c906108c
SS
4693}
4694
5734ee8b
DJ
4695/* Process all loaded DIEs for compilation unit CU, starting at
4696 FIRST_DIE. The caller should pass NEED_PC == 1 if the compilation
4697 unit DIE did not have PC info (DW_AT_low_pc and DW_AT_high_pc, or
4698 DW_AT_ranges). If NEED_PC is set, then this function will set
4699 *LOWPC and *HIGHPC to the lowest and highest PC values found in CU
4700 and record the covered ranges in the addrmap. */
c906108c 4701
72bf9492
DJ
4702static void
4703scan_partial_symbols (struct partial_die_info *first_die, CORE_ADDR *lowpc,
5734ee8b 4704 CORE_ADDR *highpc, int need_pc, struct dwarf2_cu *cu)
c906108c 4705{
72bf9492 4706 struct partial_die_info *pdi;
c906108c 4707
91c24f0a
DC
4708 /* Now, march along the PDI's, descending into ones which have
4709 interesting children but skipping the children of the other ones,
4710 until we reach the end of the compilation unit. */
c906108c 4711
72bf9492 4712 pdi = first_die;
91c24f0a 4713
72bf9492
DJ
4714 while (pdi != NULL)
4715 {
4716 fixup_partial_die (pdi, cu);
c906108c 4717
f55ee35c 4718 /* Anonymous namespaces or modules have no name but have interesting
91c24f0a
DC
4719 children, so we need to look at them. Ditto for anonymous
4720 enums. */
933c6fe4 4721
72bf9492 4722 if (pdi->name != NULL || pdi->tag == DW_TAG_namespace
95554aad
TT
4723 || pdi->tag == DW_TAG_module || pdi->tag == DW_TAG_enumeration_type
4724 || pdi->tag == DW_TAG_imported_unit)
c906108c 4725 {
72bf9492 4726 switch (pdi->tag)
c906108c
SS
4727 {
4728 case DW_TAG_subprogram:
5734ee8b 4729 add_partial_subprogram (pdi, lowpc, highpc, need_pc, cu);
c906108c 4730 break;
72929c62 4731 case DW_TAG_constant:
c906108c
SS
4732 case DW_TAG_variable:
4733 case DW_TAG_typedef:
91c24f0a 4734 case DW_TAG_union_type:
72bf9492 4735 if (!pdi->is_declaration)
63d06c5c 4736 {
72bf9492 4737 add_partial_symbol (pdi, cu);
63d06c5c
DC
4738 }
4739 break;
c906108c 4740 case DW_TAG_class_type:
680b30c7 4741 case DW_TAG_interface_type:
c906108c 4742 case DW_TAG_structure_type:
72bf9492 4743 if (!pdi->is_declaration)
c906108c 4744 {
72bf9492 4745 add_partial_symbol (pdi, cu);
c906108c
SS
4746 }
4747 break;
91c24f0a 4748 case DW_TAG_enumeration_type:
72bf9492
DJ
4749 if (!pdi->is_declaration)
4750 add_partial_enumeration (pdi, cu);
c906108c
SS
4751 break;
4752 case DW_TAG_base_type:
a02abb62 4753 case DW_TAG_subrange_type:
c906108c 4754 /* File scope base type definitions are added to the partial
c5aa993b 4755 symbol table. */
72bf9492 4756 add_partial_symbol (pdi, cu);
c906108c 4757 break;
d9fa45fe 4758 case DW_TAG_namespace:
5734ee8b 4759 add_partial_namespace (pdi, lowpc, highpc, need_pc, cu);
91c24f0a 4760 break;
5d7cb8df
JK
4761 case DW_TAG_module:
4762 add_partial_module (pdi, lowpc, highpc, need_pc, cu);
4763 break;
95554aad
TT
4764 case DW_TAG_imported_unit:
4765 {
4766 struct dwarf2_per_cu_data *per_cu;
4767
4768 per_cu = dwarf2_find_containing_comp_unit (pdi->d.offset,
4769 cu->objfile);
4770
4771 /* Go read the partial unit, if needed. */
4772 if (per_cu->v.psymtab == NULL)
4773 process_psymtab_comp_unit (per_cu, 1);
4774
4775 VEC_safe_push (dwarf2_per_cu_ptr, cu->per_cu->imported_symtabs,
4776 per_cu);
4777 }
4778 break;
c906108c
SS
4779 default:
4780 break;
4781 }
4782 }
4783
72bf9492
DJ
4784 /* If the die has a sibling, skip to the sibling. */
4785
4786 pdi = pdi->die_sibling;
4787 }
4788}
4789
4790/* Functions used to compute the fully scoped name of a partial DIE.
91c24f0a 4791
72bf9492 4792 Normally, this is simple. For C++, the parent DIE's fully scoped
987504bb
JJ
4793 name is concatenated with "::" and the partial DIE's name. For
4794 Java, the same thing occurs except that "." is used instead of "::".
72bf9492
DJ
4795 Enumerators are an exception; they use the scope of their parent
4796 enumeration type, i.e. the name of the enumeration type is not
4797 prepended to the enumerator.
91c24f0a 4798
72bf9492
DJ
4799 There are two complexities. One is DW_AT_specification; in this
4800 case "parent" means the parent of the target of the specification,
4801 instead of the direct parent of the DIE. The other is compilers
4802 which do not emit DW_TAG_namespace; in this case we try to guess
4803 the fully qualified name of structure types from their members'
4804 linkage names. This must be done using the DIE's children rather
4805 than the children of any DW_AT_specification target. We only need
4806 to do this for structures at the top level, i.e. if the target of
4807 any DW_AT_specification (if any; otherwise the DIE itself) does not
4808 have a parent. */
4809
4810/* Compute the scope prefix associated with PDI's parent, in
4811 compilation unit CU. The result will be allocated on CU's
4812 comp_unit_obstack, or a copy of the already allocated PDI->NAME
4813 field. NULL is returned if no prefix is necessary. */
4814static char *
4815partial_die_parent_scope (struct partial_die_info *pdi,
4816 struct dwarf2_cu *cu)
4817{
4818 char *grandparent_scope;
4819 struct partial_die_info *parent, *real_pdi;
91c24f0a 4820
72bf9492
DJ
4821 /* We need to look at our parent DIE; if we have a DW_AT_specification,
4822 then this means the parent of the specification DIE. */
4823
4824 real_pdi = pdi;
72bf9492 4825 while (real_pdi->has_specification)
10b3939b 4826 real_pdi = find_partial_die (real_pdi->spec_offset, cu);
72bf9492
DJ
4827
4828 parent = real_pdi->die_parent;
4829 if (parent == NULL)
4830 return NULL;
4831
4832 if (parent->scope_set)
4833 return parent->scope;
4834
4835 fixup_partial_die (parent, cu);
4836
10b3939b 4837 grandparent_scope = partial_die_parent_scope (parent, cu);
72bf9492 4838
acebe513
UW
4839 /* GCC 4.0 and 4.1 had a bug (PR c++/28460) where they generated bogus
4840 DW_TAG_namespace DIEs with a name of "::" for the global namespace.
4841 Work around this problem here. */
4842 if (cu->language == language_cplus
6e70227d 4843 && parent->tag == DW_TAG_namespace
acebe513
UW
4844 && strcmp (parent->name, "::") == 0
4845 && grandparent_scope == NULL)
4846 {
4847 parent->scope = NULL;
4848 parent->scope_set = 1;
4849 return NULL;
4850 }
4851
9c6c53f7
SA
4852 if (pdi->tag == DW_TAG_enumerator)
4853 /* Enumerators should not get the name of the enumeration as a prefix. */
4854 parent->scope = grandparent_scope;
4855 else if (parent->tag == DW_TAG_namespace
f55ee35c 4856 || parent->tag == DW_TAG_module
72bf9492
DJ
4857 || parent->tag == DW_TAG_structure_type
4858 || parent->tag == DW_TAG_class_type
680b30c7 4859 || parent->tag == DW_TAG_interface_type
ceeb3d5a
TT
4860 || parent->tag == DW_TAG_union_type
4861 || parent->tag == DW_TAG_enumeration_type)
72bf9492
DJ
4862 {
4863 if (grandparent_scope == NULL)
4864 parent->scope = parent->name;
4865 else
3e43a32a
MS
4866 parent->scope = typename_concat (&cu->comp_unit_obstack,
4867 grandparent_scope,
f55ee35c 4868 parent->name, 0, cu);
72bf9492 4869 }
72bf9492
DJ
4870 else
4871 {
4872 /* FIXME drow/2004-04-01: What should we be doing with
4873 function-local names? For partial symbols, we should probably be
4874 ignoring them. */
4875 complaint (&symfile_complaints,
e2e0b3e5 4876 _("unhandled containing DIE tag %d for DIE at %d"),
b64f50a1 4877 parent->tag, pdi->offset.sect_off);
72bf9492 4878 parent->scope = grandparent_scope;
c906108c
SS
4879 }
4880
72bf9492
DJ
4881 parent->scope_set = 1;
4882 return parent->scope;
4883}
4884
4885/* Return the fully scoped name associated with PDI, from compilation unit
4886 CU. The result will be allocated with malloc. */
4568ecf9 4887
72bf9492
DJ
4888static char *
4889partial_die_full_name (struct partial_die_info *pdi,
4890 struct dwarf2_cu *cu)
4891{
4892 char *parent_scope;
4893
98bfdba5
PA
4894 /* If this is a template instantiation, we can not work out the
4895 template arguments from partial DIEs. So, unfortunately, we have
4896 to go through the full DIEs. At least any work we do building
4897 types here will be reused if full symbols are loaded later. */
4898 if (pdi->has_template_arguments)
4899 {
4900 fixup_partial_die (pdi, cu);
4901
4902 if (pdi->name != NULL && strchr (pdi->name, '<') == NULL)
4903 {
4904 struct die_info *die;
4905 struct attribute attr;
4906 struct dwarf2_cu *ref_cu = cu;
4907
b64f50a1 4908 /* DW_FORM_ref_addr is using section offset. */
98bfdba5
PA
4909 attr.name = 0;
4910 attr.form = DW_FORM_ref_addr;
4568ecf9 4911 attr.u.unsnd = pdi->offset.sect_off;
98bfdba5
PA
4912 die = follow_die_ref (NULL, &attr, &ref_cu);
4913
4914 return xstrdup (dwarf2_full_name (NULL, die, ref_cu));
4915 }
4916 }
4917
72bf9492
DJ
4918 parent_scope = partial_die_parent_scope (pdi, cu);
4919 if (parent_scope == NULL)
4920 return NULL;
4921 else
f55ee35c 4922 return typename_concat (NULL, parent_scope, pdi->name, 0, cu);
c906108c
SS
4923}
4924
4925static void
72bf9492 4926add_partial_symbol (struct partial_die_info *pdi, struct dwarf2_cu *cu)
c906108c 4927{
e7c27a73 4928 struct objfile *objfile = cu->objfile;
c906108c 4929 CORE_ADDR addr = 0;
decbce07 4930 char *actual_name = NULL;
e142c38c 4931 CORE_ADDR baseaddr;
72bf9492 4932 int built_actual_name = 0;
e142c38c
DJ
4933
4934 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
c906108c 4935
94af9270
KS
4936 actual_name = partial_die_full_name (pdi, cu);
4937 if (actual_name)
4938 built_actual_name = 1;
63d06c5c 4939
72bf9492
DJ
4940 if (actual_name == NULL)
4941 actual_name = pdi->name;
4942
c906108c
SS
4943 switch (pdi->tag)
4944 {
4945 case DW_TAG_subprogram:
2cfa0c8d 4946 if (pdi->is_external || cu->language == language_ada)
c906108c 4947 {
2cfa0c8d
JB
4948 /* brobecker/2007-12-26: Normally, only "external" DIEs are part
4949 of the global scope. But in Ada, we want to be able to access
4950 nested procedures globally. So all Ada subprograms are stored
4951 in the global scope. */
f47fb265 4952 /* prim_record_minimal_symbol (actual_name, pdi->lowpc + baseaddr,
c5aa993b 4953 mst_text, objfile); */
f47fb265
MS
4954 add_psymbol_to_list (actual_name, strlen (actual_name),
4955 built_actual_name,
4956 VAR_DOMAIN, LOC_BLOCK,
4957 &objfile->global_psymbols,
4958 0, pdi->lowpc + baseaddr,
4959 cu->language, objfile);
c906108c
SS
4960 }
4961 else
4962 {
f47fb265 4963 /* prim_record_minimal_symbol (actual_name, pdi->lowpc + baseaddr,
c5aa993b 4964 mst_file_text, objfile); */
f47fb265
MS
4965 add_psymbol_to_list (actual_name, strlen (actual_name),
4966 built_actual_name,
4967 VAR_DOMAIN, LOC_BLOCK,
4968 &objfile->static_psymbols,
4969 0, pdi->lowpc + baseaddr,
4970 cu->language, objfile);
c906108c
SS
4971 }
4972 break;
72929c62
JB
4973 case DW_TAG_constant:
4974 {
4975 struct psymbol_allocation_list *list;
4976
4977 if (pdi->is_external)
4978 list = &objfile->global_psymbols;
4979 else
4980 list = &objfile->static_psymbols;
f47fb265
MS
4981 add_psymbol_to_list (actual_name, strlen (actual_name),
4982 built_actual_name, VAR_DOMAIN, LOC_STATIC,
4983 list, 0, 0, cu->language, objfile);
72929c62
JB
4984 }
4985 break;
c906108c 4986 case DW_TAG_variable:
95554aad
TT
4987 if (pdi->d.locdesc)
4988 addr = decode_locdesc (pdi->d.locdesc, cu);
caac4577 4989
95554aad 4990 if (pdi->d.locdesc
caac4577
JG
4991 && addr == 0
4992 && !dwarf2_per_objfile->has_section_at_zero)
4993 {
4994 /* A global or static variable may also have been stripped
4995 out by the linker if unused, in which case its address
4996 will be nullified; do not add such variables into partial
4997 symbol table then. */
4998 }
4999 else if (pdi->is_external)
c906108c
SS
5000 {
5001 /* Global Variable.
5002 Don't enter into the minimal symbol tables as there is
5003 a minimal symbol table entry from the ELF symbols already.
5004 Enter into partial symbol table if it has a location
5005 descriptor or a type.
5006 If the location descriptor is missing, new_symbol will create
5007 a LOC_UNRESOLVED symbol, the address of the variable will then
5008 be determined from the minimal symbol table whenever the variable
5009 is referenced.
5010 The address for the partial symbol table entry is not
5011 used by GDB, but it comes in handy for debugging partial symbol
5012 table building. */
5013
95554aad 5014 if (pdi->d.locdesc || pdi->has_type)
f47fb265
MS
5015 add_psymbol_to_list (actual_name, strlen (actual_name),
5016 built_actual_name,
5017 VAR_DOMAIN, LOC_STATIC,
5018 &objfile->global_psymbols,
5019 0, addr + baseaddr,
5020 cu->language, objfile);
c906108c
SS
5021 }
5022 else
5023 {
0963b4bd 5024 /* Static Variable. Skip symbols without location descriptors. */
95554aad 5025 if (pdi->d.locdesc == NULL)
decbce07
MS
5026 {
5027 if (built_actual_name)
5028 xfree (actual_name);
5029 return;
5030 }
f47fb265 5031 /* prim_record_minimal_symbol (actual_name, addr + baseaddr,
c5aa993b 5032 mst_file_data, objfile); */
f47fb265
MS
5033 add_psymbol_to_list (actual_name, strlen (actual_name),
5034 built_actual_name,
5035 VAR_DOMAIN, LOC_STATIC,
5036 &objfile->static_psymbols,
5037 0, addr + baseaddr,
5038 cu->language, objfile);
c906108c
SS
5039 }
5040 break;
5041 case DW_TAG_typedef:
5042 case DW_TAG_base_type:
a02abb62 5043 case DW_TAG_subrange_type:
38d518c9 5044 add_psymbol_to_list (actual_name, strlen (actual_name),
04a679b8 5045 built_actual_name,
176620f1 5046 VAR_DOMAIN, LOC_TYPEDEF,
c906108c 5047 &objfile->static_psymbols,
e142c38c 5048 0, (CORE_ADDR) 0, cu->language, objfile);
c906108c 5049 break;
72bf9492
DJ
5050 case DW_TAG_namespace:
5051 add_psymbol_to_list (actual_name, strlen (actual_name),
04a679b8 5052 built_actual_name,
72bf9492
DJ
5053 VAR_DOMAIN, LOC_TYPEDEF,
5054 &objfile->global_psymbols,
5055 0, (CORE_ADDR) 0, cu->language, objfile);
5056 break;
c906108c 5057 case DW_TAG_class_type:
680b30c7 5058 case DW_TAG_interface_type:
c906108c
SS
5059 case DW_TAG_structure_type:
5060 case DW_TAG_union_type:
5061 case DW_TAG_enumeration_type:
fa4028e9
JB
5062 /* Skip external references. The DWARF standard says in the section
5063 about "Structure, Union, and Class Type Entries": "An incomplete
5064 structure, union or class type is represented by a structure,
5065 union or class entry that does not have a byte size attribute
5066 and that has a DW_AT_declaration attribute." */
5067 if (!pdi->has_byte_size && pdi->is_declaration)
decbce07
MS
5068 {
5069 if (built_actual_name)
5070 xfree (actual_name);
5071 return;
5072 }
fa4028e9 5073
63d06c5c
DC
5074 /* NOTE: carlton/2003-10-07: See comment in new_symbol about
5075 static vs. global. */
38d518c9 5076 add_psymbol_to_list (actual_name, strlen (actual_name),
04a679b8 5077 built_actual_name,
176620f1 5078 STRUCT_DOMAIN, LOC_TYPEDEF,
987504bb
JJ
5079 (cu->language == language_cplus
5080 || cu->language == language_java)
63d06c5c
DC
5081 ? &objfile->global_psymbols
5082 : &objfile->static_psymbols,
e142c38c 5083 0, (CORE_ADDR) 0, cu->language, objfile);
c906108c 5084
c906108c
SS
5085 break;
5086 case DW_TAG_enumerator:
38d518c9 5087 add_psymbol_to_list (actual_name, strlen (actual_name),
04a679b8 5088 built_actual_name,
176620f1 5089 VAR_DOMAIN, LOC_CONST,
987504bb
JJ
5090 (cu->language == language_cplus
5091 || cu->language == language_java)
f6fe98ef
DJ
5092 ? &objfile->global_psymbols
5093 : &objfile->static_psymbols,
e142c38c 5094 0, (CORE_ADDR) 0, cu->language, objfile);
c906108c
SS
5095 break;
5096 default:
5097 break;
5098 }
5c4e30ca 5099
72bf9492
DJ
5100 if (built_actual_name)
5101 xfree (actual_name);
c906108c
SS
5102}
5103
5c4e30ca
DC
5104/* Read a partial die corresponding to a namespace; also, add a symbol
5105 corresponding to that namespace to the symbol table. NAMESPACE is
5106 the name of the enclosing namespace. */
91c24f0a 5107
72bf9492
DJ
5108static void
5109add_partial_namespace (struct partial_die_info *pdi,
91c24f0a 5110 CORE_ADDR *lowpc, CORE_ADDR *highpc,
5734ee8b 5111 int need_pc, struct dwarf2_cu *cu)
91c24f0a 5112{
72bf9492 5113 /* Add a symbol for the namespace. */
e7c27a73 5114
72bf9492 5115 add_partial_symbol (pdi, cu);
5c4e30ca
DC
5116
5117 /* Now scan partial symbols in that namespace. */
5118
91c24f0a 5119 if (pdi->has_children)
5734ee8b 5120 scan_partial_symbols (pdi->die_child, lowpc, highpc, need_pc, cu);
91c24f0a
DC
5121}
5122
5d7cb8df
JK
5123/* Read a partial die corresponding to a Fortran module. */
5124
5125static void
5126add_partial_module (struct partial_die_info *pdi, CORE_ADDR *lowpc,
5127 CORE_ADDR *highpc, int need_pc, struct dwarf2_cu *cu)
5128{
f55ee35c 5129 /* Now scan partial symbols in that module. */
5d7cb8df
JK
5130
5131 if (pdi->has_children)
5132 scan_partial_symbols (pdi->die_child, lowpc, highpc, need_pc, cu);
5133}
5134
bc30ff58
JB
5135/* Read a partial die corresponding to a subprogram and create a partial
5136 symbol for that subprogram. When the CU language allows it, this
5137 routine also defines a partial symbol for each nested subprogram
5138 that this subprogram contains.
6e70227d 5139
bc30ff58
JB
5140 DIE my also be a lexical block, in which case we simply search
5141 recursively for suprograms defined inside that lexical block.
5142 Again, this is only performed when the CU language allows this
5143 type of definitions. */
5144
5145static void
5146add_partial_subprogram (struct partial_die_info *pdi,
5147 CORE_ADDR *lowpc, CORE_ADDR *highpc,
5734ee8b 5148 int need_pc, struct dwarf2_cu *cu)
bc30ff58
JB
5149{
5150 if (pdi->tag == DW_TAG_subprogram)
5151 {
5152 if (pdi->has_pc_info)
5153 {
5154 if (pdi->lowpc < *lowpc)
5155 *lowpc = pdi->lowpc;
5156 if (pdi->highpc > *highpc)
5157 *highpc = pdi->highpc;
5734ee8b
DJ
5158 if (need_pc)
5159 {
5160 CORE_ADDR baseaddr;
5161 struct objfile *objfile = cu->objfile;
5162
5163 baseaddr = ANOFFSET (objfile->section_offsets,
5164 SECT_OFF_TEXT (objfile));
5165 addrmap_set_empty (objfile->psymtabs_addrmap,
01637564
DE
5166 pdi->lowpc + baseaddr,
5167 pdi->highpc - 1 + baseaddr,
9291a0cd 5168 cu->per_cu->v.psymtab);
5734ee8b 5169 }
481860b3
GB
5170 }
5171
5172 if (pdi->has_pc_info || (!pdi->is_external && pdi->may_be_inlined))
5173 {
bc30ff58 5174 if (!pdi->is_declaration)
e8d05480
JB
5175 /* Ignore subprogram DIEs that do not have a name, they are
5176 illegal. Do not emit a complaint at this point, we will
5177 do so when we convert this psymtab into a symtab. */
5178 if (pdi->name)
5179 add_partial_symbol (pdi, cu);
bc30ff58
JB
5180 }
5181 }
6e70227d 5182
bc30ff58
JB
5183 if (! pdi->has_children)
5184 return;
5185
5186 if (cu->language == language_ada)
5187 {
5188 pdi = pdi->die_child;
5189 while (pdi != NULL)
5190 {
5191 fixup_partial_die (pdi, cu);
5192 if (pdi->tag == DW_TAG_subprogram
5193 || pdi->tag == DW_TAG_lexical_block)
5734ee8b 5194 add_partial_subprogram (pdi, lowpc, highpc, need_pc, cu);
bc30ff58
JB
5195 pdi = pdi->die_sibling;
5196 }
5197 }
5198}
5199
91c24f0a
DC
5200/* Read a partial die corresponding to an enumeration type. */
5201
72bf9492
DJ
5202static void
5203add_partial_enumeration (struct partial_die_info *enum_pdi,
5204 struct dwarf2_cu *cu)
91c24f0a 5205{
72bf9492 5206 struct partial_die_info *pdi;
91c24f0a
DC
5207
5208 if (enum_pdi->name != NULL)
72bf9492
DJ
5209 add_partial_symbol (enum_pdi, cu);
5210
5211 pdi = enum_pdi->die_child;
5212 while (pdi)
91c24f0a 5213 {
72bf9492 5214 if (pdi->tag != DW_TAG_enumerator || pdi->name == NULL)
e2e0b3e5 5215 complaint (&symfile_complaints, _("malformed enumerator DIE ignored"));
91c24f0a 5216 else
72bf9492
DJ
5217 add_partial_symbol (pdi, cu);
5218 pdi = pdi->die_sibling;
91c24f0a 5219 }
91c24f0a
DC
5220}
5221
6caca83c
CC
5222/* Return the initial uleb128 in the die at INFO_PTR. */
5223
5224static unsigned int
5225peek_abbrev_code (bfd *abfd, gdb_byte *info_ptr)
5226{
5227 unsigned int bytes_read;
5228
5229 return read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
5230}
5231
4bb7a0a7
DJ
5232/* Read the initial uleb128 in the die at INFO_PTR in compilation unit CU.
5233 Return the corresponding abbrev, or NULL if the number is zero (indicating
5234 an empty DIE). In either case *BYTES_READ will be set to the length of
5235 the initial number. */
5236
5237static struct abbrev_info *
fe1b8b76 5238peek_die_abbrev (gdb_byte *info_ptr, unsigned int *bytes_read,
891d2f0b 5239 struct dwarf2_cu *cu)
4bb7a0a7
DJ
5240{
5241 bfd *abfd = cu->objfile->obfd;
5242 unsigned int abbrev_number;
5243 struct abbrev_info *abbrev;
5244
5245 abbrev_number = read_unsigned_leb128 (abfd, info_ptr, bytes_read);
5246
5247 if (abbrev_number == 0)
5248 return NULL;
5249
433df2d4 5250 abbrev = abbrev_table_lookup_abbrev (cu->abbrev_table, abbrev_number);
4bb7a0a7
DJ
5251 if (!abbrev)
5252 {
3e43a32a
MS
5253 error (_("Dwarf Error: Could not find abbrev number %d [in module %s]"),
5254 abbrev_number, bfd_get_filename (abfd));
4bb7a0a7
DJ
5255 }
5256
5257 return abbrev;
5258}
5259
93311388
DE
5260/* Scan the debug information for CU starting at INFO_PTR in buffer BUFFER.
5261 Returns a pointer to the end of a series of DIEs, terminated by an empty
4bb7a0a7
DJ
5262 DIE. Any children of the skipped DIEs will also be skipped. */
5263
fe1b8b76 5264static gdb_byte *
dee91e82 5265skip_children (const struct die_reader_specs *reader, gdb_byte *info_ptr)
4bb7a0a7 5266{
dee91e82 5267 struct dwarf2_cu *cu = reader->cu;
4bb7a0a7
DJ
5268 struct abbrev_info *abbrev;
5269 unsigned int bytes_read;
5270
5271 while (1)
5272 {
5273 abbrev = peek_die_abbrev (info_ptr, &bytes_read, cu);
5274 if (abbrev == NULL)
5275 return info_ptr + bytes_read;
5276 else
dee91e82 5277 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
4bb7a0a7
DJ
5278 }
5279}
5280
93311388
DE
5281/* Scan the debug information for CU starting at INFO_PTR in buffer BUFFER.
5282 INFO_PTR should point just after the initial uleb128 of a DIE, and the
4bb7a0a7
DJ
5283 abbrev corresponding to that skipped uleb128 should be passed in
5284 ABBREV. Returns a pointer to this DIE's sibling, skipping any
5285 children. */
5286
fe1b8b76 5287static gdb_byte *
dee91e82
DE
5288skip_one_die (const struct die_reader_specs *reader, gdb_byte *info_ptr,
5289 struct abbrev_info *abbrev)
4bb7a0a7
DJ
5290{
5291 unsigned int bytes_read;
5292 struct attribute attr;
dee91e82
DE
5293 bfd *abfd = reader->abfd;
5294 struct dwarf2_cu *cu = reader->cu;
5295 gdb_byte *buffer = reader->buffer;
f664829e
DE
5296 const gdb_byte *buffer_end = reader->buffer_end;
5297 gdb_byte *start_info_ptr = info_ptr;
4bb7a0a7
DJ
5298 unsigned int form, i;
5299
5300 for (i = 0; i < abbrev->num_attrs; i++)
5301 {
5302 /* The only abbrev we care about is DW_AT_sibling. */
5303 if (abbrev->attrs[i].name == DW_AT_sibling)
5304 {
dee91e82 5305 read_attribute (reader, &attr, &abbrev->attrs[i], info_ptr);
4bb7a0a7 5306 if (attr.form == DW_FORM_ref_addr)
3e43a32a
MS
5307 complaint (&symfile_complaints,
5308 _("ignoring absolute DW_AT_sibling"));
4bb7a0a7 5309 else
b64f50a1 5310 return buffer + dwarf2_get_ref_die_offset (&attr).sect_off;
4bb7a0a7
DJ
5311 }
5312
5313 /* If it isn't DW_AT_sibling, skip this attribute. */
5314 form = abbrev->attrs[i].form;
5315 skip_attribute:
5316 switch (form)
5317 {
4bb7a0a7 5318 case DW_FORM_ref_addr:
ae411497
TT
5319 /* In DWARF 2, DW_FORM_ref_addr is address sized; in DWARF 3
5320 and later it is offset sized. */
5321 if (cu->header.version == 2)
5322 info_ptr += cu->header.addr_size;
5323 else
5324 info_ptr += cu->header.offset_size;
5325 break;
5326 case DW_FORM_addr:
4bb7a0a7
DJ
5327 info_ptr += cu->header.addr_size;
5328 break;
5329 case DW_FORM_data1:
5330 case DW_FORM_ref1:
5331 case DW_FORM_flag:
5332 info_ptr += 1;
5333 break;
2dc7f7b3
TT
5334 case DW_FORM_flag_present:
5335 break;
4bb7a0a7
DJ
5336 case DW_FORM_data2:
5337 case DW_FORM_ref2:
5338 info_ptr += 2;
5339 break;
5340 case DW_FORM_data4:
5341 case DW_FORM_ref4:
5342 info_ptr += 4;
5343 break;
5344 case DW_FORM_data8:
5345 case DW_FORM_ref8:
55f1336d 5346 case DW_FORM_ref_sig8:
4bb7a0a7
DJ
5347 info_ptr += 8;
5348 break;
5349 case DW_FORM_string:
9b1c24c8 5350 read_direct_string (abfd, info_ptr, &bytes_read);
4bb7a0a7
DJ
5351 info_ptr += bytes_read;
5352 break;
2dc7f7b3 5353 case DW_FORM_sec_offset:
4bb7a0a7
DJ
5354 case DW_FORM_strp:
5355 info_ptr += cu->header.offset_size;
5356 break;
2dc7f7b3 5357 case DW_FORM_exprloc:
4bb7a0a7
DJ
5358 case DW_FORM_block:
5359 info_ptr += read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
5360 info_ptr += bytes_read;
5361 break;
5362 case DW_FORM_block1:
5363 info_ptr += 1 + read_1_byte (abfd, info_ptr);
5364 break;
5365 case DW_FORM_block2:
5366 info_ptr += 2 + read_2_bytes (abfd, info_ptr);
5367 break;
5368 case DW_FORM_block4:
5369 info_ptr += 4 + read_4_bytes (abfd, info_ptr);
5370 break;
5371 case DW_FORM_sdata:
5372 case DW_FORM_udata:
5373 case DW_FORM_ref_udata:
3019eac3
DE
5374 case DW_FORM_GNU_addr_index:
5375 case DW_FORM_GNU_str_index:
f664829e 5376 info_ptr = (gdb_byte *) safe_skip_leb128 (info_ptr, buffer_end);
4bb7a0a7
DJ
5377 break;
5378 case DW_FORM_indirect:
5379 form = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
5380 info_ptr += bytes_read;
5381 /* We need to continue parsing from here, so just go back to
5382 the top. */
5383 goto skip_attribute;
5384
5385 default:
3e43a32a
MS
5386 error (_("Dwarf Error: Cannot handle %s "
5387 "in DWARF reader [in module %s]"),
4bb7a0a7
DJ
5388 dwarf_form_name (form),
5389 bfd_get_filename (abfd));
5390 }
5391 }
5392
5393 if (abbrev->has_children)
dee91e82 5394 return skip_children (reader, info_ptr);
4bb7a0a7
DJ
5395 else
5396 return info_ptr;
5397}
5398
93311388 5399/* Locate ORIG_PDI's sibling.
dee91e82 5400 INFO_PTR should point to the start of the next DIE after ORIG_PDI. */
91c24f0a 5401
fe1b8b76 5402static gdb_byte *
dee91e82
DE
5403locate_pdi_sibling (const struct die_reader_specs *reader,
5404 struct partial_die_info *orig_pdi,
5405 gdb_byte *info_ptr)
91c24f0a
DC
5406{
5407 /* Do we know the sibling already? */
72bf9492 5408
91c24f0a
DC
5409 if (orig_pdi->sibling)
5410 return orig_pdi->sibling;
5411
5412 /* Are there any children to deal with? */
5413
5414 if (!orig_pdi->has_children)
5415 return info_ptr;
5416
4bb7a0a7 5417 /* Skip the children the long way. */
91c24f0a 5418
dee91e82 5419 return skip_children (reader, info_ptr);
91c24f0a
DC
5420}
5421
c906108c
SS
5422/* Expand this partial symbol table into a full symbol table. */
5423
5424static void
fba45db2 5425dwarf2_psymtab_to_symtab (struct partial_symtab *pst)
c906108c 5426{
c906108c
SS
5427 if (pst != NULL)
5428 {
5429 if (pst->readin)
5430 {
3e43a32a
MS
5431 warning (_("bug: psymtab for %s is already read in."),
5432 pst->filename);
c906108c
SS
5433 }
5434 else
5435 {
5436 if (info_verbose)
5437 {
3e43a32a
MS
5438 printf_filtered (_("Reading in symbols for %s..."),
5439 pst->filename);
c906108c
SS
5440 gdb_flush (gdb_stdout);
5441 }
5442
10b3939b
DJ
5443 /* Restore our global data. */
5444 dwarf2_per_objfile = objfile_data (pst->objfile,
5445 dwarf2_objfile_data_key);
5446
b2ab525c
KB
5447 /* If this psymtab is constructed from a debug-only objfile, the
5448 has_section_at_zero flag will not necessarily be correct. We
5449 can get the correct value for this flag by looking at the data
5450 associated with the (presumably stripped) associated objfile. */
5451 if (pst->objfile->separate_debug_objfile_backlink)
5452 {
5453 struct dwarf2_per_objfile *dpo_backlink
5454 = objfile_data (pst->objfile->separate_debug_objfile_backlink,
5455 dwarf2_objfile_data_key);
9a619af0 5456
b2ab525c
KB
5457 dwarf2_per_objfile->has_section_at_zero
5458 = dpo_backlink->has_section_at_zero;
5459 }
5460
98bfdba5
PA
5461 dwarf2_per_objfile->reading_partial_symbols = 0;
5462
c906108c
SS
5463 psymtab_to_symtab_1 (pst);
5464
5465 /* Finish up the debug error message. */
5466 if (info_verbose)
a3f17187 5467 printf_filtered (_("done.\n"));
c906108c
SS
5468 }
5469 }
95554aad
TT
5470
5471 process_cu_includes ();
c906108c 5472}
9cdd5dbd
DE
5473\f
5474/* Reading in full CUs. */
c906108c 5475
10b3939b
DJ
5476/* Add PER_CU to the queue. */
5477
5478static void
95554aad
TT
5479queue_comp_unit (struct dwarf2_per_cu_data *per_cu,
5480 enum language pretend_language)
10b3939b
DJ
5481{
5482 struct dwarf2_queue_item *item;
5483
5484 per_cu->queued = 1;
5485 item = xmalloc (sizeof (*item));
5486 item->per_cu = per_cu;
95554aad 5487 item->pretend_language = pretend_language;
10b3939b
DJ
5488 item->next = NULL;
5489
5490 if (dwarf2_queue == NULL)
5491 dwarf2_queue = item;
5492 else
5493 dwarf2_queue_tail->next = item;
5494
5495 dwarf2_queue_tail = item;
5496}
5497
0907af0c
DE
5498/* THIS_CU has a reference to PER_CU. If necessary, load the new compilation
5499 unit and add it to our queue.
5500 The result is non-zero if PER_CU was queued, otherwise the result is zero
5501 meaning either PER_CU is already queued or it is already loaded. */
5502
5503static int
5504maybe_queue_comp_unit (struct dwarf2_cu *this_cu,
5505 struct dwarf2_per_cu_data *per_cu,
5506 enum language pretend_language)
5507{
5508 /* We may arrive here during partial symbol reading, if we need full
5509 DIEs to process an unusual case (e.g. template arguments). Do
5510 not queue PER_CU, just tell our caller to load its DIEs. */
5511 if (dwarf2_per_objfile->reading_partial_symbols)
5512 {
5513 if (per_cu->cu == NULL || per_cu->cu->dies == NULL)
5514 return 1;
5515 return 0;
5516 }
5517
5518 /* Mark the dependence relation so that we don't flush PER_CU
5519 too early. */
5520 dwarf2_add_dependence (this_cu, per_cu);
5521
5522 /* If it's already on the queue, we have nothing to do. */
5523 if (per_cu->queued)
5524 return 0;
5525
5526 /* If the compilation unit is already loaded, just mark it as
5527 used. */
5528 if (per_cu->cu != NULL)
5529 {
5530 per_cu->cu->last_used = 0;
5531 return 0;
5532 }
5533
5534 /* Add it to the queue. */
5535 queue_comp_unit (per_cu, pretend_language);
5536
5537 return 1;
5538}
5539
10b3939b
DJ
5540/* Process the queue. */
5541
5542static void
a0f42c21 5543process_queue (void)
10b3939b
DJ
5544{
5545 struct dwarf2_queue_item *item, *next_item;
5546
45cfd468
DE
5547 if (dwarf2_read_debug)
5548 {
5549 fprintf_unfiltered (gdb_stdlog,
5550 "Expanding one or more symtabs of objfile %s ...\n",
5551 dwarf2_per_objfile->objfile->name);
5552 }
5553
03dd20cc
DJ
5554 /* The queue starts out with one item, but following a DIE reference
5555 may load a new CU, adding it to the end of the queue. */
10b3939b
DJ
5556 for (item = dwarf2_queue; item != NULL; dwarf2_queue = item = next_item)
5557 {
9291a0cd
TT
5558 if (dwarf2_per_objfile->using_index
5559 ? !item->per_cu->v.quick->symtab
5560 : (item->per_cu->v.psymtab && !item->per_cu->v.psymtab->readin))
95554aad 5561 process_full_comp_unit (item->per_cu, item->pretend_language);
10b3939b
DJ
5562
5563 item->per_cu->queued = 0;
5564 next_item = item->next;
5565 xfree (item);
5566 }
5567
5568 dwarf2_queue_tail = NULL;
45cfd468
DE
5569
5570 if (dwarf2_read_debug)
5571 {
5572 fprintf_unfiltered (gdb_stdlog, "Done expanding symtabs of %s.\n",
5573 dwarf2_per_objfile->objfile->name);
5574 }
10b3939b
DJ
5575}
5576
5577/* Free all allocated queue entries. This function only releases anything if
5578 an error was thrown; if the queue was processed then it would have been
5579 freed as we went along. */
5580
5581static void
5582dwarf2_release_queue (void *dummy)
5583{
5584 struct dwarf2_queue_item *item, *last;
5585
5586 item = dwarf2_queue;
5587 while (item)
5588 {
5589 /* Anything still marked queued is likely to be in an
5590 inconsistent state, so discard it. */
5591 if (item->per_cu->queued)
5592 {
5593 if (item->per_cu->cu != NULL)
dee91e82 5594 free_one_cached_comp_unit (item->per_cu);
10b3939b
DJ
5595 item->per_cu->queued = 0;
5596 }
5597
5598 last = item;
5599 item = item->next;
5600 xfree (last);
5601 }
5602
5603 dwarf2_queue = dwarf2_queue_tail = NULL;
5604}
5605
5606/* Read in full symbols for PST, and anything it depends on. */
5607
c906108c 5608static void
fba45db2 5609psymtab_to_symtab_1 (struct partial_symtab *pst)
c906108c 5610{
10b3939b 5611 struct dwarf2_per_cu_data *per_cu;
aaa75496
JB
5612 int i;
5613
95554aad
TT
5614 if (pst->readin)
5615 return;
5616
aaa75496 5617 for (i = 0; i < pst->number_of_dependencies; i++)
95554aad
TT
5618 if (!pst->dependencies[i]->readin
5619 && pst->dependencies[i]->user == NULL)
aaa75496
JB
5620 {
5621 /* Inform about additional files that need to be read in. */
5622 if (info_verbose)
5623 {
a3f17187 5624 /* FIXME: i18n: Need to make this a single string. */
aaa75496
JB
5625 fputs_filtered (" ", gdb_stdout);
5626 wrap_here ("");
5627 fputs_filtered ("and ", gdb_stdout);
5628 wrap_here ("");
5629 printf_filtered ("%s...", pst->dependencies[i]->filename);
0963b4bd 5630 wrap_here (""); /* Flush output. */
aaa75496
JB
5631 gdb_flush (gdb_stdout);
5632 }
5633 psymtab_to_symtab_1 (pst->dependencies[i]);
5634 }
5635
e38df1d0 5636 per_cu = pst->read_symtab_private;
10b3939b
DJ
5637
5638 if (per_cu == NULL)
aaa75496
JB
5639 {
5640 /* It's an include file, no symbols to read for it.
5641 Everything is in the parent symtab. */
5642 pst->readin = 1;
5643 return;
5644 }
c906108c 5645
a0f42c21 5646 dw2_do_instantiate_symtab (per_cu);
10b3939b
DJ
5647}
5648
dee91e82
DE
5649/* Trivial hash function for die_info: the hash value of a DIE
5650 is its offset in .debug_info for this objfile. */
10b3939b 5651
dee91e82
DE
5652static hashval_t
5653die_hash (const void *item)
10b3939b 5654{
dee91e82 5655 const struct die_info *die = item;
6502dd73 5656
dee91e82
DE
5657 return die->offset.sect_off;
5658}
63d06c5c 5659
dee91e82
DE
5660/* Trivial comparison function for die_info structures: two DIEs
5661 are equal if they have the same offset. */
98bfdba5 5662
dee91e82
DE
5663static int
5664die_eq (const void *item_lhs, const void *item_rhs)
5665{
5666 const struct die_info *die_lhs = item_lhs;
5667 const struct die_info *die_rhs = item_rhs;
c906108c 5668
dee91e82
DE
5669 return die_lhs->offset.sect_off == die_rhs->offset.sect_off;
5670}
c906108c 5671
dee91e82
DE
5672/* die_reader_func for load_full_comp_unit.
5673 This is identical to read_signatured_type_reader,
5674 but is kept separate for now. */
c906108c 5675
dee91e82
DE
5676static void
5677load_full_comp_unit_reader (const struct die_reader_specs *reader,
5678 gdb_byte *info_ptr,
5679 struct die_info *comp_unit_die,
5680 int has_children,
5681 void *data)
5682{
5683 struct dwarf2_cu *cu = reader->cu;
95554aad 5684 enum language *language_ptr = data;
6caca83c 5685
dee91e82
DE
5686 gdb_assert (cu->die_hash == NULL);
5687 cu->die_hash =
5688 htab_create_alloc_ex (cu->header.length / 12,
5689 die_hash,
5690 die_eq,
5691 NULL,
5692 &cu->comp_unit_obstack,
5693 hashtab_obstack_allocate,
5694 dummy_obstack_deallocate);
e142c38c 5695
dee91e82
DE
5696 if (has_children)
5697 comp_unit_die->child = read_die_and_siblings (reader, info_ptr,
5698 &info_ptr, comp_unit_die);
5699 cu->dies = comp_unit_die;
5700 /* comp_unit_die is not stored in die_hash, no need. */
10b3939b
DJ
5701
5702 /* We try not to read any attributes in this function, because not
9cdd5dbd 5703 all CUs needed for references have been loaded yet, and symbol
10b3939b 5704 table processing isn't initialized. But we have to set the CU language,
dee91e82
DE
5705 or we won't be able to build types correctly.
5706 Similarly, if we do not read the producer, we can not apply
5707 producer-specific interpretation. */
95554aad 5708 prepare_one_comp_unit (cu, cu->dies, *language_ptr);
dee91e82 5709}
10b3939b 5710
dee91e82 5711/* Load the DIEs associated with PER_CU into memory. */
a6c727b2 5712
dee91e82 5713static void
95554aad
TT
5714load_full_comp_unit (struct dwarf2_per_cu_data *this_cu,
5715 enum language pretend_language)
dee91e82 5716{
3019eac3 5717 gdb_assert (! this_cu->is_debug_types);
c5b7e1cb 5718
95554aad
TT
5719 init_cutu_and_read_dies (this_cu, 1, 1, load_full_comp_unit_reader,
5720 &pretend_language);
10b3939b
DJ
5721}
5722
3da10d80
KS
5723/* Add a DIE to the delayed physname list. */
5724
5725static void
5726add_to_method_list (struct type *type, int fnfield_index, int index,
5727 const char *name, struct die_info *die,
5728 struct dwarf2_cu *cu)
5729{
5730 struct delayed_method_info mi;
5731 mi.type = type;
5732 mi.fnfield_index = fnfield_index;
5733 mi.index = index;
5734 mi.name = name;
5735 mi.die = die;
5736 VEC_safe_push (delayed_method_info, cu->method_list, &mi);
5737}
5738
5739/* A cleanup for freeing the delayed method list. */
5740
5741static void
5742free_delayed_list (void *ptr)
5743{
5744 struct dwarf2_cu *cu = (struct dwarf2_cu *) ptr;
5745 if (cu->method_list != NULL)
5746 {
5747 VEC_free (delayed_method_info, cu->method_list);
5748 cu->method_list = NULL;
5749 }
5750}
5751
5752/* Compute the physnames of any methods on the CU's method list.
5753
5754 The computation of method physnames is delayed in order to avoid the
5755 (bad) condition that one of the method's formal parameters is of an as yet
5756 incomplete type. */
5757
5758static void
5759compute_delayed_physnames (struct dwarf2_cu *cu)
5760{
5761 int i;
5762 struct delayed_method_info *mi;
5763 for (i = 0; VEC_iterate (delayed_method_info, cu->method_list, i, mi) ; ++i)
5764 {
1d06ead6 5765 const char *physname;
3da10d80
KS
5766 struct fn_fieldlist *fn_flp
5767 = &TYPE_FN_FIELDLIST (mi->type, mi->fnfield_index);
1d06ead6 5768 physname = dwarf2_physname ((char *) mi->name, mi->die, cu);
3da10d80
KS
5769 fn_flp->fn_fields[mi->index].physname = physname ? physname : "";
5770 }
5771}
5772
a766d390
DE
5773/* Go objects should be embedded in a DW_TAG_module DIE,
5774 and it's not clear if/how imported objects will appear.
5775 To keep Go support simple until that's worked out,
5776 go back through what we've read and create something usable.
5777 We could do this while processing each DIE, and feels kinda cleaner,
5778 but that way is more invasive.
5779 This is to, for example, allow the user to type "p var" or "b main"
5780 without having to specify the package name, and allow lookups
5781 of module.object to work in contexts that use the expression
5782 parser. */
5783
5784static void
5785fixup_go_packaging (struct dwarf2_cu *cu)
5786{
5787 char *package_name = NULL;
5788 struct pending *list;
5789 int i;
5790
5791 for (list = global_symbols; list != NULL; list = list->next)
5792 {
5793 for (i = 0; i < list->nsyms; ++i)
5794 {
5795 struct symbol *sym = list->symbol[i];
5796
5797 if (SYMBOL_LANGUAGE (sym) == language_go
5798 && SYMBOL_CLASS (sym) == LOC_BLOCK)
5799 {
5800 char *this_package_name = go_symbol_package_name (sym);
5801
5802 if (this_package_name == NULL)
5803 continue;
5804 if (package_name == NULL)
5805 package_name = this_package_name;
5806 else
5807 {
5808 if (strcmp (package_name, this_package_name) != 0)
5809 complaint (&symfile_complaints,
5810 _("Symtab %s has objects from two different Go packages: %s and %s"),
5811 (sym->symtab && sym->symtab->filename
5812 ? sym->symtab->filename
5813 : cu->objfile->name),
5814 this_package_name, package_name);
5815 xfree (this_package_name);
5816 }
5817 }
5818 }
5819 }
5820
5821 if (package_name != NULL)
5822 {
5823 struct objfile *objfile = cu->objfile;
5824 struct type *type = init_type (TYPE_CODE_MODULE, 0, 0,
5825 package_name, objfile);
5826 struct symbol *sym;
5827
5828 TYPE_TAG_NAME (type) = TYPE_NAME (type);
5829
5830 sym = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct symbol);
5831 SYMBOL_SET_LANGUAGE (sym, language_go);
5832 SYMBOL_SET_NAMES (sym, package_name, strlen (package_name), 1, objfile);
5833 /* This is not VAR_DOMAIN because we want a way to ensure a lookup of,
5834 e.g., "main" finds the "main" module and not C's main(). */
5835 SYMBOL_DOMAIN (sym) = STRUCT_DOMAIN;
5836 SYMBOL_CLASS (sym) = LOC_TYPEDEF;
5837 SYMBOL_TYPE (sym) = type;
5838
5839 add_symbol_to_list (sym, &global_symbols);
5840
5841 xfree (package_name);
5842 }
5843}
5844
95554aad
TT
5845static void compute_symtab_includes (struct dwarf2_per_cu_data *per_cu);
5846
5847/* Return the symtab for PER_CU. This works properly regardless of
5848 whether we're using the index or psymtabs. */
5849
5850static struct symtab *
5851get_symtab (struct dwarf2_per_cu_data *per_cu)
5852{
5853 return (dwarf2_per_objfile->using_index
5854 ? per_cu->v.quick->symtab
5855 : per_cu->v.psymtab->symtab);
5856}
5857
5858/* A helper function for computing the list of all symbol tables
5859 included by PER_CU. */
5860
5861static void
5862recursively_compute_inclusions (VEC (dwarf2_per_cu_ptr) **result,
5863 htab_t all_children,
5864 struct dwarf2_per_cu_data *per_cu)
5865{
5866 void **slot;
5867 int ix;
5868 struct dwarf2_per_cu_data *iter;
5869
5870 slot = htab_find_slot (all_children, per_cu, INSERT);
5871 if (*slot != NULL)
5872 {
5873 /* This inclusion and its children have been processed. */
5874 return;
5875 }
5876
5877 *slot = per_cu;
5878 /* Only add a CU if it has a symbol table. */
5879 if (get_symtab (per_cu) != NULL)
5880 VEC_safe_push (dwarf2_per_cu_ptr, *result, per_cu);
5881
5882 for (ix = 0;
5883 VEC_iterate (dwarf2_per_cu_ptr, per_cu->imported_symtabs, ix, iter);
5884 ++ix)
5885 recursively_compute_inclusions (result, all_children, iter);
5886}
5887
5888/* Compute the symtab 'includes' fields for the symtab related to
5889 PER_CU. */
5890
5891static void
5892compute_symtab_includes (struct dwarf2_per_cu_data *per_cu)
5893{
5894 if (!VEC_empty (dwarf2_per_cu_ptr, per_cu->imported_symtabs))
5895 {
5896 int ix, len;
5897 struct dwarf2_per_cu_data *iter;
5898 VEC (dwarf2_per_cu_ptr) *result_children = NULL;
5899 htab_t all_children;
5900 struct symtab *symtab = get_symtab (per_cu);
5901
5902 /* If we don't have a symtab, we can just skip this case. */
5903 if (symtab == NULL)
5904 return;
5905
5906 all_children = htab_create_alloc (1, htab_hash_pointer, htab_eq_pointer,
5907 NULL, xcalloc, xfree);
5908
5909 for (ix = 0;
5910 VEC_iterate (dwarf2_per_cu_ptr, per_cu->imported_symtabs,
5911 ix, iter);
5912 ++ix)
5913 recursively_compute_inclusions (&result_children, all_children, iter);
5914
5915 /* Now we have a transitive closure of all the included CUs, so
5916 we can convert it to a list of symtabs. */
5917 len = VEC_length (dwarf2_per_cu_ptr, result_children);
5918 symtab->includes
5919 = obstack_alloc (&dwarf2_per_objfile->objfile->objfile_obstack,
5920 (len + 1) * sizeof (struct symtab *));
5921 for (ix = 0;
5922 VEC_iterate (dwarf2_per_cu_ptr, result_children, ix, iter);
5923 ++ix)
5924 symtab->includes[ix] = get_symtab (iter);
5925 symtab->includes[len] = NULL;
5926
5927 VEC_free (dwarf2_per_cu_ptr, result_children);
5928 htab_delete (all_children);
5929 }
5930}
5931
5932/* Compute the 'includes' field for the symtabs of all the CUs we just
5933 read. */
5934
5935static void
5936process_cu_includes (void)
5937{
5938 int ix;
5939 struct dwarf2_per_cu_data *iter;
5940
5941 for (ix = 0;
5942 VEC_iterate (dwarf2_per_cu_ptr, dwarf2_per_objfile->just_read_cus,
5943 ix, iter);
5944 ++ix)
5945 compute_symtab_includes (iter);
5946
5947 VEC_free (dwarf2_per_cu_ptr, dwarf2_per_objfile->just_read_cus);
5948}
5949
9cdd5dbd 5950/* Generate full symbol information for PER_CU, whose DIEs have
10b3939b
DJ
5951 already been loaded into memory. */
5952
5953static void
95554aad
TT
5954process_full_comp_unit (struct dwarf2_per_cu_data *per_cu,
5955 enum language pretend_language)
10b3939b 5956{
10b3939b 5957 struct dwarf2_cu *cu = per_cu->cu;
9291a0cd 5958 struct objfile *objfile = per_cu->objfile;
10b3939b
DJ
5959 CORE_ADDR lowpc, highpc;
5960 struct symtab *symtab;
3da10d80 5961 struct cleanup *back_to, *delayed_list_cleanup;
10b3939b
DJ
5962 CORE_ADDR baseaddr;
5963
45cfd468
DE
5964 if (dwarf2_read_debug)
5965 {
5966 fprintf_unfiltered (gdb_stdlog,
5967 "Expanding symtab of %s at offset 0x%x\n",
5968 per_cu->is_debug_types ? "TU" : "CU",
5969 per_cu->offset.sect_off);
5970 }
5971
10b3939b
DJ
5972 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
5973
10b3939b
DJ
5974 buildsym_init ();
5975 back_to = make_cleanup (really_free_pendings, NULL);
3da10d80 5976 delayed_list_cleanup = make_cleanup (free_delayed_list, cu);
10b3939b
DJ
5977
5978 cu->list_in_scope = &file_symbols;
c906108c 5979
95554aad
TT
5980 cu->language = pretend_language;
5981 cu->language_defn = language_def (cu->language);
5982
c906108c 5983 /* Do line number decoding in read_file_scope () */
10b3939b 5984 process_die (cu->dies, cu);
c906108c 5985
a766d390
DE
5986 /* For now fudge the Go package. */
5987 if (cu->language == language_go)
5988 fixup_go_packaging (cu);
5989
3da10d80
KS
5990 /* Now that we have processed all the DIEs in the CU, all the types
5991 should be complete, and it should now be safe to compute all of the
5992 physnames. */
5993 compute_delayed_physnames (cu);
5994 do_cleanups (delayed_list_cleanup);
5995
fae299cd
DC
5996 /* Some compilers don't define a DW_AT_high_pc attribute for the
5997 compilation unit. If the DW_AT_high_pc is missing, synthesize
5998 it, by scanning the DIE's below the compilation unit. */
10b3939b 5999 get_scope_pc_bounds (cu->dies, &lowpc, &highpc, cu);
c906108c 6000
613e1657 6001 symtab = end_symtab (highpc + baseaddr, objfile, SECT_OFF_TEXT (objfile));
c906108c 6002
8be455d7 6003 if (symtab != NULL)
c906108c 6004 {
df15bd07 6005 int gcc_4_minor = producer_is_gcc_ge_4 (cu->producer);
4632c0d0 6006
8be455d7
JK
6007 /* Set symtab language to language from DW_AT_language. If the
6008 compilation is from a C file generated by language preprocessors, do
6009 not set the language if it was already deduced by start_subfile. */
6010 if (!(cu->language == language_c && symtab->language != language_c))
6011 symtab->language = cu->language;
6012
6013 /* GCC-4.0 has started to support -fvar-tracking. GCC-3.x still can
6014 produce DW_AT_location with location lists but it can be possibly
ab260dad
JK
6015 invalid without -fvar-tracking. Still up to GCC-4.4.x incl. 4.4.0
6016 there were bugs in prologue debug info, fixed later in GCC-4.5
6017 by "unwind info for epilogues" patch (which is not directly related).
8be455d7
JK
6018
6019 For -gdwarf-4 type units LOCATIONS_VALID indication is fortunately not
6020 needed, it would be wrong due to missing DW_AT_producer there.
6021
6022 Still one can confuse GDB by using non-standard GCC compilation
6023 options - this waits on GCC PR other/32998 (-frecord-gcc-switches).
6024 */
ab260dad 6025 if (cu->has_loclist && gcc_4_minor >= 5)
8be455d7 6026 symtab->locations_valid = 1;
e0d00bc7
JK
6027
6028 if (gcc_4_minor >= 5)
6029 symtab->epilogue_unwind_valid = 1;
96408a79
SA
6030
6031 symtab->call_site_htab = cu->call_site_htab;
c906108c 6032 }
9291a0cd
TT
6033
6034 if (dwarf2_per_objfile->using_index)
6035 per_cu->v.quick->symtab = symtab;
6036 else
6037 {
6038 struct partial_symtab *pst = per_cu->v.psymtab;
6039 pst->symtab = symtab;
6040 pst->readin = 1;
6041 }
c906108c 6042
95554aad
TT
6043 /* Push it for inclusion processing later. */
6044 VEC_safe_push (dwarf2_per_cu_ptr, dwarf2_per_objfile->just_read_cus, per_cu);
6045
c906108c 6046 do_cleanups (back_to);
45cfd468
DE
6047
6048 if (dwarf2_read_debug)
6049 {
6050 fprintf_unfiltered (gdb_stdlog,
6051 "Done expanding symtab of %s at offset 0x%x\n",
6052 per_cu->is_debug_types ? "TU" : "CU",
6053 per_cu->offset.sect_off);
6054 }
c906108c
SS
6055}
6056
95554aad
TT
6057/* Process an imported unit DIE. */
6058
6059static void
6060process_imported_unit_die (struct die_info *die, struct dwarf2_cu *cu)
6061{
6062 struct attribute *attr;
6063
6064 attr = dwarf2_attr (die, DW_AT_import, cu);
6065 if (attr != NULL)
6066 {
6067 struct dwarf2_per_cu_data *per_cu;
6068 struct symtab *imported_symtab;
6069 sect_offset offset;
6070
6071 offset = dwarf2_get_ref_die_offset (attr);
6072 per_cu = dwarf2_find_containing_comp_unit (offset, cu->objfile);
6073
6074 /* Queue the unit, if needed. */
6075 if (maybe_queue_comp_unit (cu, per_cu, cu->language))
6076 load_full_comp_unit (per_cu, cu->language);
6077
6078 VEC_safe_push (dwarf2_per_cu_ptr, cu->per_cu->imported_symtabs,
6079 per_cu);
6080 }
6081}
6082
c906108c
SS
6083/* Process a die and its children. */
6084
6085static void
e7c27a73 6086process_die (struct die_info *die, struct dwarf2_cu *cu)
c906108c
SS
6087{
6088 switch (die->tag)
6089 {
6090 case DW_TAG_padding:
6091 break;
6092 case DW_TAG_compile_unit:
95554aad 6093 case DW_TAG_partial_unit:
e7c27a73 6094 read_file_scope (die, cu);
c906108c 6095 break;
348e048f
DE
6096 case DW_TAG_type_unit:
6097 read_type_unit_scope (die, cu);
6098 break;
c906108c 6099 case DW_TAG_subprogram:
c906108c 6100 case DW_TAG_inlined_subroutine:
edb3359d 6101 read_func_scope (die, cu);
c906108c
SS
6102 break;
6103 case DW_TAG_lexical_block:
14898363
L
6104 case DW_TAG_try_block:
6105 case DW_TAG_catch_block:
e7c27a73 6106 read_lexical_block_scope (die, cu);
c906108c 6107 break;
96408a79
SA
6108 case DW_TAG_GNU_call_site:
6109 read_call_site_scope (die, cu);
6110 break;
c906108c 6111 case DW_TAG_class_type:
680b30c7 6112 case DW_TAG_interface_type:
c906108c
SS
6113 case DW_TAG_structure_type:
6114 case DW_TAG_union_type:
134d01f1 6115 process_structure_scope (die, cu);
c906108c
SS
6116 break;
6117 case DW_TAG_enumeration_type:
134d01f1 6118 process_enumeration_scope (die, cu);
c906108c 6119 break;
134d01f1 6120
f792889a
DJ
6121 /* These dies have a type, but processing them does not create
6122 a symbol or recurse to process the children. Therefore we can
6123 read them on-demand through read_type_die. */
c906108c 6124 case DW_TAG_subroutine_type:
72019c9c 6125 case DW_TAG_set_type:
c906108c 6126 case DW_TAG_array_type:
c906108c 6127 case DW_TAG_pointer_type:
c906108c 6128 case DW_TAG_ptr_to_member_type:
c906108c 6129 case DW_TAG_reference_type:
c906108c 6130 case DW_TAG_string_type:
c906108c 6131 break;
134d01f1 6132
c906108c 6133 case DW_TAG_base_type:
a02abb62 6134 case DW_TAG_subrange_type:
cb249c71 6135 case DW_TAG_typedef:
134d01f1
DJ
6136 /* Add a typedef symbol for the type definition, if it has a
6137 DW_AT_name. */
f792889a 6138 new_symbol (die, read_type_die (die, cu), cu);
a02abb62 6139 break;
c906108c 6140 case DW_TAG_common_block:
e7c27a73 6141 read_common_block (die, cu);
c906108c
SS
6142 break;
6143 case DW_TAG_common_inclusion:
6144 break;
d9fa45fe 6145 case DW_TAG_namespace:
63d06c5c 6146 processing_has_namespace_info = 1;
e7c27a73 6147 read_namespace (die, cu);
d9fa45fe 6148 break;
5d7cb8df 6149 case DW_TAG_module:
f55ee35c 6150 processing_has_namespace_info = 1;
5d7cb8df
JK
6151 read_module (die, cu);
6152 break;
d9fa45fe
DC
6153 case DW_TAG_imported_declaration:
6154 case DW_TAG_imported_module:
63d06c5c 6155 processing_has_namespace_info = 1;
27aa8d6a
SW
6156 if (die->child != NULL && (die->tag == DW_TAG_imported_declaration
6157 || cu->language != language_fortran))
6158 complaint (&symfile_complaints, _("Tag '%s' has unexpected children"),
6159 dwarf_tag_name (die->tag));
6160 read_import_statement (die, cu);
d9fa45fe 6161 break;
95554aad
TT
6162
6163 case DW_TAG_imported_unit:
6164 process_imported_unit_die (die, cu);
6165 break;
6166
c906108c 6167 default:
e7c27a73 6168 new_symbol (die, NULL, cu);
c906108c
SS
6169 break;
6170 }
6171}
6172
94af9270
KS
6173/* A helper function for dwarf2_compute_name which determines whether DIE
6174 needs to have the name of the scope prepended to the name listed in the
6175 die. */
6176
6177static int
6178die_needs_namespace (struct die_info *die, struct dwarf2_cu *cu)
6179{
1c809c68
TT
6180 struct attribute *attr;
6181
94af9270
KS
6182 switch (die->tag)
6183 {
6184 case DW_TAG_namespace:
6185 case DW_TAG_typedef:
6186 case DW_TAG_class_type:
6187 case DW_TAG_interface_type:
6188 case DW_TAG_structure_type:
6189 case DW_TAG_union_type:
6190 case DW_TAG_enumeration_type:
6191 case DW_TAG_enumerator:
6192 case DW_TAG_subprogram:
6193 case DW_TAG_member:
6194 return 1;
6195
6196 case DW_TAG_variable:
c2b0a229 6197 case DW_TAG_constant:
94af9270
KS
6198 /* We only need to prefix "globally" visible variables. These include
6199 any variable marked with DW_AT_external or any variable that
6200 lives in a namespace. [Variables in anonymous namespaces
6201 require prefixing, but they are not DW_AT_external.] */
6202
6203 if (dwarf2_attr (die, DW_AT_specification, cu))
6204 {
6205 struct dwarf2_cu *spec_cu = cu;
9a619af0 6206
94af9270
KS
6207 return die_needs_namespace (die_specification (die, &spec_cu),
6208 spec_cu);
6209 }
6210
1c809c68 6211 attr = dwarf2_attr (die, DW_AT_external, cu);
f55ee35c
JK
6212 if (attr == NULL && die->parent->tag != DW_TAG_namespace
6213 && die->parent->tag != DW_TAG_module)
1c809c68
TT
6214 return 0;
6215 /* A variable in a lexical block of some kind does not need a
6216 namespace, even though in C++ such variables may be external
6217 and have a mangled name. */
6218 if (die->parent->tag == DW_TAG_lexical_block
6219 || die->parent->tag == DW_TAG_try_block
1054b214
TT
6220 || die->parent->tag == DW_TAG_catch_block
6221 || die->parent->tag == DW_TAG_subprogram)
1c809c68
TT
6222 return 0;
6223 return 1;
94af9270
KS
6224
6225 default:
6226 return 0;
6227 }
6228}
6229
98bfdba5
PA
6230/* Retrieve the last character from a mem_file. */
6231
6232static void
6233do_ui_file_peek_last (void *object, const char *buffer, long length)
6234{
6235 char *last_char_p = (char *) object;
6236
6237 if (length > 0)
6238 *last_char_p = buffer[length - 1];
6239}
6240
94af9270 6241/* Compute the fully qualified name of DIE in CU. If PHYSNAME is nonzero,
a766d390
DE
6242 compute the physname for the object, which include a method's:
6243 - formal parameters (C++/Java),
6244 - receiver type (Go),
6245 - return type (Java).
6246
6247 The term "physname" is a bit confusing.
6248 For C++, for example, it is the demangled name.
6249 For Go, for example, it's the mangled name.
94af9270 6250
af6b7be1
JB
6251 For Ada, return the DIE's linkage name rather than the fully qualified
6252 name. PHYSNAME is ignored..
6253
94af9270
KS
6254 The result is allocated on the objfile_obstack and canonicalized. */
6255
6256static const char *
6257dwarf2_compute_name (char *name, struct die_info *die, struct dwarf2_cu *cu,
6258 int physname)
6259{
bb5ed363
DE
6260 struct objfile *objfile = cu->objfile;
6261
94af9270
KS
6262 if (name == NULL)
6263 name = dwarf2_name (die, cu);
6264
f55ee35c
JK
6265 /* For Fortran GDB prefers DW_AT_*linkage_name if present but otherwise
6266 compute it by typename_concat inside GDB. */
6267 if (cu->language == language_ada
6268 || (cu->language == language_fortran && physname))
6269 {
6270 /* For Ada unit, we prefer the linkage name over the name, as
6271 the former contains the exported name, which the user expects
6272 to be able to reference. Ideally, we want the user to be able
6273 to reference this entity using either natural or linkage name,
6274 but we haven't started looking at this enhancement yet. */
6275 struct attribute *attr;
6276
6277 attr = dwarf2_attr (die, DW_AT_linkage_name, cu);
6278 if (attr == NULL)
6279 attr = dwarf2_attr (die, DW_AT_MIPS_linkage_name, cu);
6280 if (attr && DW_STRING (attr))
6281 return DW_STRING (attr);
6282 }
6283
94af9270
KS
6284 /* These are the only languages we know how to qualify names in. */
6285 if (name != NULL
f55ee35c
JK
6286 && (cu->language == language_cplus || cu->language == language_java
6287 || cu->language == language_fortran))
94af9270
KS
6288 {
6289 if (die_needs_namespace (die, cu))
6290 {
6291 long length;
0d5cff50 6292 const char *prefix;
94af9270
KS
6293 struct ui_file *buf;
6294
6295 prefix = determine_prefix (die, cu);
6296 buf = mem_fileopen ();
6297 if (*prefix != '\0')
6298 {
f55ee35c
JK
6299 char *prefixed_name = typename_concat (NULL, prefix, name,
6300 physname, cu);
9a619af0 6301
94af9270
KS
6302 fputs_unfiltered (prefixed_name, buf);
6303 xfree (prefixed_name);
6304 }
6305 else
62d5b8da 6306 fputs_unfiltered (name, buf);
94af9270 6307
98bfdba5
PA
6308 /* Template parameters may be specified in the DIE's DW_AT_name, or
6309 as children with DW_TAG_template_type_param or
6310 DW_TAG_value_type_param. If the latter, add them to the name
6311 here. If the name already has template parameters, then
6312 skip this step; some versions of GCC emit both, and
6313 it is more efficient to use the pre-computed name.
6314
6315 Something to keep in mind about this process: it is very
6316 unlikely, or in some cases downright impossible, to produce
6317 something that will match the mangled name of a function.
6318 If the definition of the function has the same debug info,
6319 we should be able to match up with it anyway. But fallbacks
6320 using the minimal symbol, for instance to find a method
6321 implemented in a stripped copy of libstdc++, will not work.
6322 If we do not have debug info for the definition, we will have to
6323 match them up some other way.
6324
6325 When we do name matching there is a related problem with function
6326 templates; two instantiated function templates are allowed to
6327 differ only by their return types, which we do not add here. */
6328
6329 if (cu->language == language_cplus && strchr (name, '<') == NULL)
6330 {
6331 struct attribute *attr;
6332 struct die_info *child;
6333 int first = 1;
6334
6335 die->building_fullname = 1;
6336
6337 for (child = die->child; child != NULL; child = child->sibling)
6338 {
6339 struct type *type;
12df843f 6340 LONGEST value;
98bfdba5
PA
6341 gdb_byte *bytes;
6342 struct dwarf2_locexpr_baton *baton;
6343 struct value *v;
6344
6345 if (child->tag != DW_TAG_template_type_param
6346 && child->tag != DW_TAG_template_value_param)
6347 continue;
6348
6349 if (first)
6350 {
6351 fputs_unfiltered ("<", buf);
6352 first = 0;
6353 }
6354 else
6355 fputs_unfiltered (", ", buf);
6356
6357 attr = dwarf2_attr (child, DW_AT_type, cu);
6358 if (attr == NULL)
6359 {
6360 complaint (&symfile_complaints,
6361 _("template parameter missing DW_AT_type"));
6362 fputs_unfiltered ("UNKNOWN_TYPE", buf);
6363 continue;
6364 }
6365 type = die_type (child, cu);
6366
6367 if (child->tag == DW_TAG_template_type_param)
6368 {
6369 c_print_type (type, "", buf, -1, 0);
6370 continue;
6371 }
6372
6373 attr = dwarf2_attr (child, DW_AT_const_value, cu);
6374 if (attr == NULL)
6375 {
6376 complaint (&symfile_complaints,
3e43a32a
MS
6377 _("template parameter missing "
6378 "DW_AT_const_value"));
98bfdba5
PA
6379 fputs_unfiltered ("UNKNOWN_VALUE", buf);
6380 continue;
6381 }
6382
6383 dwarf2_const_value_attr (attr, type, name,
6384 &cu->comp_unit_obstack, cu,
6385 &value, &bytes, &baton);
6386
6387 if (TYPE_NOSIGN (type))
6388 /* GDB prints characters as NUMBER 'CHAR'. If that's
6389 changed, this can use value_print instead. */
6390 c_printchar (value, type, buf);
6391 else
6392 {
6393 struct value_print_options opts;
6394
6395 if (baton != NULL)
6396 v = dwarf2_evaluate_loc_desc (type, NULL,
6397 baton->data,
6398 baton->size,
6399 baton->per_cu);
6400 else if (bytes != NULL)
6401 {
6402 v = allocate_value (type);
6403 memcpy (value_contents_writeable (v), bytes,
6404 TYPE_LENGTH (type));
6405 }
6406 else
6407 v = value_from_longest (type, value);
6408
3e43a32a
MS
6409 /* Specify decimal so that we do not depend on
6410 the radix. */
98bfdba5
PA
6411 get_formatted_print_options (&opts, 'd');
6412 opts.raw = 1;
6413 value_print (v, buf, &opts);
6414 release_value (v);
6415 value_free (v);
6416 }
6417 }
6418
6419 die->building_fullname = 0;
6420
6421 if (!first)
6422 {
6423 /* Close the argument list, with a space if necessary
6424 (nested templates). */
6425 char last_char = '\0';
6426 ui_file_put (buf, do_ui_file_peek_last, &last_char);
6427 if (last_char == '>')
6428 fputs_unfiltered (" >", buf);
6429 else
6430 fputs_unfiltered (">", buf);
6431 }
6432 }
6433
94af9270
KS
6434 /* For Java and C++ methods, append formal parameter type
6435 information, if PHYSNAME. */
6e70227d 6436
94af9270
KS
6437 if (physname && die->tag == DW_TAG_subprogram
6438 && (cu->language == language_cplus
6439 || cu->language == language_java))
6440 {
6441 struct type *type = read_type_die (die, cu);
6442
3167638f 6443 c_type_print_args (type, buf, 1, cu->language);
94af9270
KS
6444
6445 if (cu->language == language_java)
6446 {
6447 /* For java, we must append the return type to method
0963b4bd 6448 names. */
94af9270
KS
6449 if (die->tag == DW_TAG_subprogram)
6450 java_print_type (TYPE_TARGET_TYPE (type), "", buf,
6451 0, 0);
6452 }
6453 else if (cu->language == language_cplus)
6454 {
60430eff
DJ
6455 /* Assume that an artificial first parameter is
6456 "this", but do not crash if it is not. RealView
6457 marks unnamed (and thus unused) parameters as
6458 artificial; there is no way to differentiate
6459 the two cases. */
94af9270
KS
6460 if (TYPE_NFIELDS (type) > 0
6461 && TYPE_FIELD_ARTIFICIAL (type, 0)
60430eff 6462 && TYPE_CODE (TYPE_FIELD_TYPE (type, 0)) == TYPE_CODE_PTR
3e43a32a
MS
6463 && TYPE_CONST (TYPE_TARGET_TYPE (TYPE_FIELD_TYPE (type,
6464 0))))
94af9270
KS
6465 fputs_unfiltered (" const", buf);
6466 }
6467 }
6468
bb5ed363 6469 name = ui_file_obsavestring (buf, &objfile->objfile_obstack,
94af9270
KS
6470 &length);
6471 ui_file_delete (buf);
6472
6473 if (cu->language == language_cplus)
6474 {
6475 char *cname
6476 = dwarf2_canonicalize_name (name, cu,
bb5ed363 6477 &objfile->objfile_obstack);
9a619af0 6478
94af9270
KS
6479 if (cname != NULL)
6480 name = cname;
6481 }
6482 }
6483 }
6484
6485 return name;
6486}
6487
0114d602
DJ
6488/* Return the fully qualified name of DIE, based on its DW_AT_name.
6489 If scope qualifiers are appropriate they will be added. The result
6490 will be allocated on the objfile_obstack, or NULL if the DIE does
94af9270
KS
6491 not have a name. NAME may either be from a previous call to
6492 dwarf2_name or NULL.
6493
0963b4bd 6494 The output string will be canonicalized (if C++/Java). */
0114d602
DJ
6495
6496static const char *
94af9270 6497dwarf2_full_name (char *name, struct die_info *die, struct dwarf2_cu *cu)
0114d602 6498{
94af9270
KS
6499 return dwarf2_compute_name (name, die, cu, 0);
6500}
0114d602 6501
94af9270
KS
6502/* Construct a physname for the given DIE in CU. NAME may either be
6503 from a previous call to dwarf2_name or NULL. The result will be
6504 allocated on the objfile_objstack or NULL if the DIE does not have a
6505 name.
0114d602 6506
94af9270 6507 The output string will be canonicalized (if C++/Java). */
0114d602 6508
94af9270
KS
6509static const char *
6510dwarf2_physname (char *name, struct die_info *die, struct dwarf2_cu *cu)
6511{
bb5ed363 6512 struct objfile *objfile = cu->objfile;
900e11f9
JK
6513 struct attribute *attr;
6514 const char *retval, *mangled = NULL, *canon = NULL;
6515 struct cleanup *back_to;
6516 int need_copy = 1;
6517
6518 /* In this case dwarf2_compute_name is just a shortcut not building anything
6519 on its own. */
6520 if (!die_needs_namespace (die, cu))
6521 return dwarf2_compute_name (name, die, cu, 1);
6522
6523 back_to = make_cleanup (null_cleanup, NULL);
6524
6525 attr = dwarf2_attr (die, DW_AT_linkage_name, cu);
6526 if (!attr)
6527 attr = dwarf2_attr (die, DW_AT_MIPS_linkage_name, cu);
6528
6529 /* DW_AT_linkage_name is missing in some cases - depend on what GDB
6530 has computed. */
6531 if (attr && DW_STRING (attr))
6532 {
6533 char *demangled;
6534
6535 mangled = DW_STRING (attr);
6536
6537 /* Use DMGL_RET_DROP for C++ template functions to suppress their return
6538 type. It is easier for GDB users to search for such functions as
6539 `name(params)' than `long name(params)'. In such case the minimal
6540 symbol names do not match the full symbol names but for template
6541 functions there is never a need to look up their definition from their
6542 declaration so the only disadvantage remains the minimal symbol
6543 variant `long name(params)' does not have the proper inferior type.
6544 */
6545
a766d390
DE
6546 if (cu->language == language_go)
6547 {
6548 /* This is a lie, but we already lie to the caller new_symbol_full.
6549 new_symbol_full assumes we return the mangled name.
6550 This just undoes that lie until things are cleaned up. */
6551 demangled = NULL;
6552 }
6553 else
6554 {
6555 demangled = cplus_demangle (mangled,
6556 (DMGL_PARAMS | DMGL_ANSI
6557 | (cu->language == language_java
6558 ? DMGL_JAVA | DMGL_RET_POSTFIX
6559 : DMGL_RET_DROP)));
6560 }
900e11f9
JK
6561 if (demangled)
6562 {
6563 make_cleanup (xfree, demangled);
6564 canon = demangled;
6565 }
6566 else
6567 {
6568 canon = mangled;
6569 need_copy = 0;
6570 }
6571 }
6572
6573 if (canon == NULL || check_physname)
6574 {
6575 const char *physname = dwarf2_compute_name (name, die, cu, 1);
6576
6577 if (canon != NULL && strcmp (physname, canon) != 0)
6578 {
6579 /* It may not mean a bug in GDB. The compiler could also
6580 compute DW_AT_linkage_name incorrectly. But in such case
6581 GDB would need to be bug-to-bug compatible. */
6582
6583 complaint (&symfile_complaints,
6584 _("Computed physname <%s> does not match demangled <%s> "
6585 "(from linkage <%s>) - DIE at 0x%x [in module %s]"),
b64f50a1 6586 physname, canon, mangled, die->offset.sect_off, objfile->name);
900e11f9
JK
6587
6588 /* Prefer DW_AT_linkage_name (in the CANON form) - when it
6589 is available here - over computed PHYSNAME. It is safer
6590 against both buggy GDB and buggy compilers. */
6591
6592 retval = canon;
6593 }
6594 else
6595 {
6596 retval = physname;
6597 need_copy = 0;
6598 }
6599 }
6600 else
6601 retval = canon;
6602
6603 if (need_copy)
6604 retval = obsavestring (retval, strlen (retval),
bb5ed363 6605 &objfile->objfile_obstack);
900e11f9
JK
6606
6607 do_cleanups (back_to);
6608 return retval;
0114d602
DJ
6609}
6610
27aa8d6a
SW
6611/* Read the import statement specified by the given die and record it. */
6612
6613static void
6614read_import_statement (struct die_info *die, struct dwarf2_cu *cu)
6615{
bb5ed363 6616 struct objfile *objfile = cu->objfile;
27aa8d6a 6617 struct attribute *import_attr;
32019081 6618 struct die_info *imported_die, *child_die;
de4affc9 6619 struct dwarf2_cu *imported_cu;
27aa8d6a 6620 const char *imported_name;
794684b6 6621 const char *imported_name_prefix;
13387711
SW
6622 const char *canonical_name;
6623 const char *import_alias;
6624 const char *imported_declaration = NULL;
794684b6 6625 const char *import_prefix;
32019081
JK
6626 VEC (const_char_ptr) *excludes = NULL;
6627 struct cleanup *cleanups;
13387711
SW
6628
6629 char *temp;
27aa8d6a
SW
6630
6631 import_attr = dwarf2_attr (die, DW_AT_import, cu);
6632 if (import_attr == NULL)
6633 {
6634 complaint (&symfile_complaints, _("Tag '%s' has no DW_AT_import"),
6635 dwarf_tag_name (die->tag));
6636 return;
6637 }
6638
de4affc9
CC
6639 imported_cu = cu;
6640 imported_die = follow_die_ref_or_sig (die, import_attr, &imported_cu);
6641 imported_name = dwarf2_name (imported_die, imported_cu);
27aa8d6a
SW
6642 if (imported_name == NULL)
6643 {
6644 /* GCC bug: https://bugzilla.redhat.com/show_bug.cgi?id=506524
6645
6646 The import in the following code:
6647 namespace A
6648 {
6649 typedef int B;
6650 }
6651
6652 int main ()
6653 {
6654 using A::B;
6655 B b;
6656 return b;
6657 }
6658
6659 ...
6660 <2><51>: Abbrev Number: 3 (DW_TAG_imported_declaration)
6661 <52> DW_AT_decl_file : 1
6662 <53> DW_AT_decl_line : 6
6663 <54> DW_AT_import : <0x75>
6664 <2><58>: Abbrev Number: 4 (DW_TAG_typedef)
6665 <59> DW_AT_name : B
6666 <5b> DW_AT_decl_file : 1
6667 <5c> DW_AT_decl_line : 2
6668 <5d> DW_AT_type : <0x6e>
6669 ...
6670 <1><75>: Abbrev Number: 7 (DW_TAG_base_type)
6671 <76> DW_AT_byte_size : 4
6672 <77> DW_AT_encoding : 5 (signed)
6673
6674 imports the wrong die ( 0x75 instead of 0x58 ).
6675 This case will be ignored until the gcc bug is fixed. */
6676 return;
6677 }
6678
82856980
SW
6679 /* Figure out the local name after import. */
6680 import_alias = dwarf2_name (die, cu);
27aa8d6a 6681
794684b6
SW
6682 /* Figure out where the statement is being imported to. */
6683 import_prefix = determine_prefix (die, cu);
6684
6685 /* Figure out what the scope of the imported die is and prepend it
6686 to the name of the imported die. */
de4affc9 6687 imported_name_prefix = determine_prefix (imported_die, imported_cu);
794684b6 6688
f55ee35c
JK
6689 if (imported_die->tag != DW_TAG_namespace
6690 && imported_die->tag != DW_TAG_module)
794684b6 6691 {
13387711
SW
6692 imported_declaration = imported_name;
6693 canonical_name = imported_name_prefix;
794684b6 6694 }
13387711 6695 else if (strlen (imported_name_prefix) > 0)
794684b6 6696 {
13387711
SW
6697 temp = alloca (strlen (imported_name_prefix)
6698 + 2 + strlen (imported_name) + 1);
6699 strcpy (temp, imported_name_prefix);
6700 strcat (temp, "::");
6701 strcat (temp, imported_name);
6702 canonical_name = temp;
794684b6 6703 }
13387711
SW
6704 else
6705 canonical_name = imported_name;
794684b6 6706
32019081
JK
6707 cleanups = make_cleanup (VEC_cleanup (const_char_ptr), &excludes);
6708
6709 if (die->tag == DW_TAG_imported_module && cu->language == language_fortran)
6710 for (child_die = die->child; child_die && child_die->tag;
6711 child_die = sibling_die (child_die))
6712 {
6713 /* DWARF-4: A Fortran use statement with a “rename list” may be
6714 represented by an imported module entry with an import attribute
6715 referring to the module and owned entries corresponding to those
6716 entities that are renamed as part of being imported. */
6717
6718 if (child_die->tag != DW_TAG_imported_declaration)
6719 {
6720 complaint (&symfile_complaints,
6721 _("child DW_TAG_imported_declaration expected "
6722 "- DIE at 0x%x [in module %s]"),
b64f50a1 6723 child_die->offset.sect_off, objfile->name);
32019081
JK
6724 continue;
6725 }
6726
6727 import_attr = dwarf2_attr (child_die, DW_AT_import, cu);
6728 if (import_attr == NULL)
6729 {
6730 complaint (&symfile_complaints, _("Tag '%s' has no DW_AT_import"),
6731 dwarf_tag_name (child_die->tag));
6732 continue;
6733 }
6734
6735 imported_cu = cu;
6736 imported_die = follow_die_ref_or_sig (child_die, import_attr,
6737 &imported_cu);
6738 imported_name = dwarf2_name (imported_die, imported_cu);
6739 if (imported_name == NULL)
6740 {
6741 complaint (&symfile_complaints,
6742 _("child DW_TAG_imported_declaration has unknown "
6743 "imported name - DIE at 0x%x [in module %s]"),
b64f50a1 6744 child_die->offset.sect_off, objfile->name);
32019081
JK
6745 continue;
6746 }
6747
6748 VEC_safe_push (const_char_ptr, excludes, imported_name);
6749
6750 process_die (child_die, cu);
6751 }
6752
c0cc3a76
SW
6753 cp_add_using_directive (import_prefix,
6754 canonical_name,
6755 import_alias,
13387711 6756 imported_declaration,
32019081 6757 excludes,
bb5ed363 6758 &objfile->objfile_obstack);
32019081
JK
6759
6760 do_cleanups (cleanups);
27aa8d6a
SW
6761}
6762
ae2de4f8
DE
6763/* Cleanup function for read_file_scope. */
6764
cb1df416
DJ
6765static void
6766free_cu_line_header (void *arg)
6767{
6768 struct dwarf2_cu *cu = arg;
6769
6770 free_line_header (cu->line_header);
6771 cu->line_header = NULL;
6772}
6773
9291a0cd
TT
6774static void
6775find_file_and_directory (struct die_info *die, struct dwarf2_cu *cu,
6776 char **name, char **comp_dir)
6777{
6778 struct attribute *attr;
6779
6780 *name = NULL;
6781 *comp_dir = NULL;
6782
6783 /* Find the filename. Do not use dwarf2_name here, since the filename
6784 is not a source language identifier. */
6785 attr = dwarf2_attr (die, DW_AT_name, cu);
6786 if (attr)
6787 {
6788 *name = DW_STRING (attr);
6789 }
6790
6791 attr = dwarf2_attr (die, DW_AT_comp_dir, cu);
6792 if (attr)
6793 *comp_dir = DW_STRING (attr);
6794 else if (*name != NULL && IS_ABSOLUTE_PATH (*name))
6795 {
6796 *comp_dir = ldirname (*name);
6797 if (*comp_dir != NULL)
6798 make_cleanup (xfree, *comp_dir);
6799 }
6800 if (*comp_dir != NULL)
6801 {
6802 /* Irix 6.2 native cc prepends <machine>.: to the compilation
6803 directory, get rid of it. */
6804 char *cp = strchr (*comp_dir, ':');
6805
6806 if (cp && cp != *comp_dir && cp[-1] == '.' && cp[1] == '/')
6807 *comp_dir = cp + 1;
6808 }
6809
6810 if (*name == NULL)
6811 *name = "<unknown>";
6812}
6813
f3f5162e
DE
6814/* Handle DW_AT_stmt_list for a compilation unit or type unit.
6815 DIE is the DW_TAG_compile_unit or DW_TAG_type_unit die for CU.
6816 COMP_DIR is the compilation directory.
6817 WANT_LINE_INFO is non-zero if the pc/line-number mapping is needed. */
2ab95328
TT
6818
6819static void
6820handle_DW_AT_stmt_list (struct die_info *die, struct dwarf2_cu *cu,
f3f5162e 6821 const char *comp_dir, int want_line_info)
2ab95328
TT
6822{
6823 struct attribute *attr;
2ab95328 6824
2ab95328
TT
6825 attr = dwarf2_attr (die, DW_AT_stmt_list, cu);
6826 if (attr)
6827 {
6828 unsigned int line_offset = DW_UNSND (attr);
6829 struct line_header *line_header
3019eac3 6830 = dwarf_decode_line_header (line_offset, cu);
2ab95328
TT
6831
6832 if (line_header)
dee91e82
DE
6833 {
6834 cu->line_header = line_header;
6835 make_cleanup (free_cu_line_header, cu);
f3f5162e 6836 dwarf_decode_lines (line_header, comp_dir, cu, NULL, want_line_info);
dee91e82 6837 }
2ab95328
TT
6838 }
6839}
6840
95554aad 6841/* Process DW_TAG_compile_unit or DW_TAG_partial_unit. */
ae2de4f8 6842
c906108c 6843static void
e7c27a73 6844read_file_scope (struct die_info *die, struct dwarf2_cu *cu)
c906108c 6845{
dee91e82 6846 struct objfile *objfile = dwarf2_per_objfile->objfile;
debd256d 6847 struct cleanup *back_to = make_cleanup (null_cleanup, 0);
2acceee2 6848 CORE_ADDR lowpc = ((CORE_ADDR) -1);
c906108c
SS
6849 CORE_ADDR highpc = ((CORE_ADDR) 0);
6850 struct attribute *attr;
e1024ff1 6851 char *name = NULL;
c906108c
SS
6852 char *comp_dir = NULL;
6853 struct die_info *child_die;
6854 bfd *abfd = objfile->obfd;
e142c38c 6855 CORE_ADDR baseaddr;
6e70227d 6856
e142c38c 6857 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
c906108c 6858
fae299cd 6859 get_scope_pc_bounds (die, &lowpc, &highpc, cu);
c906108c
SS
6860
6861 /* If we didn't find a lowpc, set it to highpc to avoid complaints
6862 from finish_block. */
2acceee2 6863 if (lowpc == ((CORE_ADDR) -1))
c906108c
SS
6864 lowpc = highpc;
6865 lowpc += baseaddr;
6866 highpc += baseaddr;
6867
9291a0cd 6868 find_file_and_directory (die, cu, &name, &comp_dir);
e1024ff1 6869
95554aad 6870 prepare_one_comp_unit (cu, die, cu->language);
303b6f5d 6871
f4b8a18d
KW
6872 /* The XLCL doesn't generate DW_LANG_OpenCL because this attribute is not
6873 standardised yet. As a workaround for the language detection we fall
6874 back to the DW_AT_producer string. */
6875 if (cu->producer && strstr (cu->producer, "IBM XL C for OpenCL") != NULL)
6876 cu->language = language_opencl;
6877
3019eac3
DE
6878 /* Similar hack for Go. */
6879 if (cu->producer && strstr (cu->producer, "GNU Go ") != NULL)
6880 set_cu_language (DW_LANG_Go, cu);
6881
6882 /* We assume that we're processing GCC output. */
6883 processing_gcc_compilation = 2;
6884
6885 processing_has_namespace_info = 0;
6886
6887 start_symtab (name, comp_dir, lowpc);
6888 record_debugformat ("DWARF 2");
6889 record_producer (cu->producer);
6890
6891 /* Decode line number information if present. We do this before
6892 processing child DIEs, so that the line header table is available
6893 for DW_AT_decl_file. */
6894 handle_DW_AT_stmt_list (die, cu, comp_dir, 1);
6895
6896 /* Process all dies in compilation unit. */
6897 if (die->child != NULL)
6898 {
6899 child_die = die->child;
6900 while (child_die && child_die->tag)
6901 {
6902 process_die (child_die, cu);
6903 child_die = sibling_die (child_die);
6904 }
6905 }
6906
6907 /* Decode macro information, if present. Dwarf 2 macro information
6908 refers to information in the line number info statement program
6909 header, so we can only read it if we've read the header
6910 successfully. */
6911 attr = dwarf2_attr (die, DW_AT_GNU_macros, cu);
6912 if (attr && cu->line_header)
6913 {
6914 if (dwarf2_attr (die, DW_AT_macro_info, cu))
6915 complaint (&symfile_complaints,
6916 _("CU refers to both DW_AT_GNU_macros and DW_AT_macro_info"));
6917
09262596 6918 dwarf_decode_macros (cu, DW_UNSND (attr), comp_dir, 1);
3019eac3
DE
6919 }
6920 else
6921 {
6922 attr = dwarf2_attr (die, DW_AT_macro_info, cu);
6923 if (attr && cu->line_header)
6924 {
6925 unsigned int macro_offset = DW_UNSND (attr);
6926
09262596 6927 dwarf_decode_macros (cu, macro_offset, comp_dir, 0);
3019eac3
DE
6928 }
6929 }
6930
6931 do_cleanups (back_to);
6932}
6933
6934/* Process DW_TAG_type_unit.
6935 For TUs we want to skip the first top level sibling if it's not the
6936 actual type being defined by this TU. In this case the first top
6937 level sibling is there to provide context only. */
6938
6939static void
6940read_type_unit_scope (struct die_info *die, struct dwarf2_cu *cu)
6941{
6942 struct objfile *objfile = cu->objfile;
6943 struct cleanup *back_to = make_cleanup (null_cleanup, 0);
6944 CORE_ADDR lowpc;
6945 struct attribute *attr;
6946 char *name = NULL;
6947 char *comp_dir = NULL;
6948 struct die_info *child_die;
6949 bfd *abfd = objfile->obfd;
6950
6951 /* start_symtab needs a low pc, but we don't really have one.
6952 Do what read_file_scope would do in the absence of such info. */
6953 lowpc = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
6954
6955 /* Find the filename. Do not use dwarf2_name here, since the filename
6956 is not a source language identifier. */
6957 attr = dwarf2_attr (die, DW_AT_name, cu);
6958 if (attr)
6959 name = DW_STRING (attr);
6960
6961 attr = dwarf2_attr (die, DW_AT_comp_dir, cu);
6962 if (attr)
6963 comp_dir = DW_STRING (attr);
6964 else if (name != NULL && IS_ABSOLUTE_PATH (name))
6965 {
6966 comp_dir = ldirname (name);
6967 if (comp_dir != NULL)
6968 make_cleanup (xfree, comp_dir);
6969 }
6970
6971 if (name == NULL)
6972 name = "<unknown>";
6973
95554aad 6974 prepare_one_comp_unit (cu, die, language_minimal);
3019eac3
DE
6975
6976 /* We assume that we're processing GCC output. */
6977 processing_gcc_compilation = 2;
6978
6979 processing_has_namespace_info = 0;
6980
6981 start_symtab (name, comp_dir, lowpc);
6982 record_debugformat ("DWARF 2");
6983 record_producer (cu->producer);
6984
6985 /* Decode line number information if present. We do this before
6986 processing child DIEs, so that the line header table is available
6987 for DW_AT_decl_file.
6988 We don't need the pc/line-number mapping for type units. */
6989 handle_DW_AT_stmt_list (die, cu, comp_dir, 0);
6990
6991 /* Process the dies in the type unit. */
6992 if (die->child == NULL)
6993 {
6994 dump_die_for_error (die);
6995 error (_("Dwarf Error: Missing children for type unit [in module %s]"),
6996 bfd_get_filename (abfd));
6997 }
6998
6999 child_die = die->child;
7000
7001 while (child_die && child_die->tag)
7002 {
7003 process_die (child_die, cu);
7004
7005 child_die = sibling_die (child_die);
7006 }
7007
7008 do_cleanups (back_to);
7009}
7010\f
7011/* DWO files. */
7012
7013static hashval_t
7014hash_dwo_file (const void *item)
7015{
7016 const struct dwo_file *dwo_file = item;
7017
7018 return htab_hash_string (dwo_file->dwo_name);
7019}
7020
7021static int
7022eq_dwo_file (const void *item_lhs, const void *item_rhs)
7023{
7024 const struct dwo_file *lhs = item_lhs;
7025 const struct dwo_file *rhs = item_rhs;
7026
7027 return strcmp (lhs->dwo_name, rhs->dwo_name) == 0;
7028}
7029
7030/* Allocate a hash table for DWO files. */
7031
7032static htab_t
7033allocate_dwo_file_hash_table (void)
7034{
7035 struct objfile *objfile = dwarf2_per_objfile->objfile;
7036
7037 return htab_create_alloc_ex (41,
7038 hash_dwo_file,
7039 eq_dwo_file,
7040 NULL,
7041 &objfile->objfile_obstack,
7042 hashtab_obstack_allocate,
7043 dummy_obstack_deallocate);
7044}
7045
7046static hashval_t
7047hash_dwo_unit (const void *item)
7048{
7049 const struct dwo_unit *dwo_unit = item;
7050
7051 /* This drops the top 32 bits of the id, but is ok for a hash. */
7052 return dwo_unit->signature;
7053}
7054
7055static int
7056eq_dwo_unit (const void *item_lhs, const void *item_rhs)
7057{
7058 const struct dwo_unit *lhs = item_lhs;
7059 const struct dwo_unit *rhs = item_rhs;
7060
7061 /* The signature is assumed to be unique within the DWO file.
7062 So while object file CU dwo_id's always have the value zero,
7063 that's OK, assuming each object file DWO file has only one CU,
7064 and that's the rule for now. */
7065 return lhs->signature == rhs->signature;
7066}
7067
7068/* Allocate a hash table for DWO CUs,TUs.
7069 There is one of these tables for each of CUs,TUs for each DWO file. */
7070
7071static htab_t
7072allocate_dwo_unit_table (struct objfile *objfile)
7073{
7074 /* Start out with a pretty small number.
7075 Generally DWO files contain only one CU and maybe some TUs. */
7076 return htab_create_alloc_ex (3,
7077 hash_dwo_unit,
7078 eq_dwo_unit,
7079 NULL,
7080 &objfile->objfile_obstack,
7081 hashtab_obstack_allocate,
7082 dummy_obstack_deallocate);
7083}
7084
7085/* This function is mapped across the sections and remembers the offset and
7086 size of each of the DWO debugging sections we are interested in. */
7087
7088static void
7089dwarf2_locate_dwo_sections (bfd *abfd, asection *sectp, void *dwo_file_ptr)
7090{
7091 struct dwo_file *dwo_file = dwo_file_ptr;
7092 const struct dwo_section_names *names = &dwo_section_names;
7093
7094 if (section_is_p (sectp->name, &names->abbrev_dwo))
7095 {
7096 dwo_file->sections.abbrev.asection = sectp;
7097 dwo_file->sections.abbrev.size = bfd_get_section_size (sectp);
7098 }
7099 else if (section_is_p (sectp->name, &names->info_dwo))
7100 {
7101 dwo_file->sections.info.asection = sectp;
7102 dwo_file->sections.info.size = bfd_get_section_size (sectp);
7103 }
7104 else if (section_is_p (sectp->name, &names->line_dwo))
7105 {
7106 dwo_file->sections.line.asection = sectp;
7107 dwo_file->sections.line.size = bfd_get_section_size (sectp);
7108 }
7109 else if (section_is_p (sectp->name, &names->loc_dwo))
7110 {
7111 dwo_file->sections.loc.asection = sectp;
7112 dwo_file->sections.loc.size = bfd_get_section_size (sectp);
7113 }
09262596
DE
7114 else if (section_is_p (sectp->name, &names->macinfo_dwo))
7115 {
7116 dwo_file->sections.macinfo.asection = sectp;
7117 dwo_file->sections.macinfo.size = bfd_get_section_size (sectp);
7118 }
7119 else if (section_is_p (sectp->name, &names->macro_dwo))
7120 {
7121 dwo_file->sections.macro.asection = sectp;
7122 dwo_file->sections.macro.size = bfd_get_section_size (sectp);
7123 }
3019eac3
DE
7124 else if (section_is_p (sectp->name, &names->str_dwo))
7125 {
7126 dwo_file->sections.str.asection = sectp;
7127 dwo_file->sections.str.size = bfd_get_section_size (sectp);
7128 }
7129 else if (section_is_p (sectp->name, &names->str_offsets_dwo))
7130 {
7131 dwo_file->sections.str_offsets.asection = sectp;
7132 dwo_file->sections.str_offsets.size = bfd_get_section_size (sectp);
7133 }
7134 else if (section_is_p (sectp->name, &names->types_dwo))
7135 {
7136 struct dwarf2_section_info type_section;
7137
7138 memset (&type_section, 0, sizeof (type_section));
7139 type_section.asection = sectp;
7140 type_section.size = bfd_get_section_size (sectp);
7141 VEC_safe_push (dwarf2_section_info_def, dwo_file->sections.types,
7142 &type_section);
7143 }
7144}
7145
7146/* Structure used to pass data to create_debug_info_hash_table_reader. */
7147
7148struct create_dwo_info_table_data
7149{
7150 struct dwo_file *dwo_file;
7151 htab_t cu_htab;
7152};
7153
7154/* die_reader_func for create_debug_info_hash_table. */
7155
7156static void
7157create_debug_info_hash_table_reader (const struct die_reader_specs *reader,
7158 gdb_byte *info_ptr,
7159 struct die_info *comp_unit_die,
7160 int has_children,
7161 void *datap)
7162{
7163 struct dwarf2_cu *cu = reader->cu;
7164 struct objfile *objfile = dwarf2_per_objfile->objfile;
7165 sect_offset offset = cu->per_cu->offset;
7166 struct dwarf2_section_info *section = cu->per_cu->info_or_types_section;
7167 struct create_dwo_info_table_data *data = datap;
7168 struct dwo_file *dwo_file = data->dwo_file;
7169 htab_t cu_htab = data->cu_htab;
7170 void **slot;
7171 struct attribute *attr;
7172 struct dwo_unit *dwo_unit;
7173
7174 attr = dwarf2_attr (comp_unit_die, DW_AT_GNU_dwo_id, cu);
7175 if (attr == NULL)
7176 {
7177 error (_("Dwarf Error: debug entry at offset 0x%x is missing"
7178 " its dwo_id [in module %s]"),
7179 offset.sect_off, dwo_file->dwo_name);
7180 return;
7181 }
7182
7183 dwo_unit = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct dwo_unit);
7184 dwo_unit->dwo_file = dwo_file;
7185 dwo_unit->signature = DW_UNSND (attr);
7186 dwo_unit->info_or_types_section = section;
7187 dwo_unit->offset = offset;
7188 dwo_unit->length = cu->per_cu->length;
7189
7190 slot = htab_find_slot (cu_htab, dwo_unit, INSERT);
7191 gdb_assert (slot != NULL);
7192 if (*slot != NULL)
7193 {
7194 const struct dwo_unit *dup_dwo_unit = *slot;
7195
7196 complaint (&symfile_complaints,
7197 _("debug entry at offset 0x%x is duplicate to the entry at"
7198 " offset 0x%x, dwo_id 0x%s [in module %s]"),
7199 offset.sect_off, dup_dwo_unit->offset.sect_off,
7200 phex (dwo_unit->signature, sizeof (dwo_unit->signature)),
7201 dwo_file->dwo_name);
7202 }
7203 else
7204 *slot = dwo_unit;
7205
09406207 7206 if (dwarf2_read_debug)
3019eac3
DE
7207 fprintf_unfiltered (gdb_stdlog, " offset 0x%x, dwo_id 0x%s\n",
7208 offset.sect_off,
7209 phex (dwo_unit->signature,
7210 sizeof (dwo_unit->signature)));
7211}
7212
7213/* Create a hash table to map DWO IDs to their CU entry in .debug_info.dwo. */
7214
7215static htab_t
7216create_debug_info_hash_table (struct dwo_file *dwo_file)
7217{
7218 struct objfile *objfile = dwarf2_per_objfile->objfile;
7219 struct dwarf2_section_info *section = &dwo_file->sections.info;
7220 bfd *abfd;
7221 htab_t cu_htab;
7222 gdb_byte *info_ptr, *end_ptr;
7223 struct create_dwo_info_table_data create_dwo_info_table_data;
7224
7225 dwarf2_read_section (objfile, section);
7226 info_ptr = section->buffer;
7227
7228 if (info_ptr == NULL)
7229 return NULL;
7230
7231 /* We can't set abfd until now because the section may be empty or
7232 not present, in which case section->asection will be NULL. */
7233 abfd = section->asection->owner;
7234
09406207 7235 if (dwarf2_read_debug)
3019eac3
DE
7236 fprintf_unfiltered (gdb_stdlog, "Reading .debug_info.dwo for %s:\n",
7237 bfd_get_filename (abfd));
7238
7239 cu_htab = allocate_dwo_unit_table (objfile);
7240
7241 create_dwo_info_table_data.dwo_file = dwo_file;
7242 create_dwo_info_table_data.cu_htab = cu_htab;
7243
7244 end_ptr = info_ptr + section->size;
7245 while (info_ptr < end_ptr)
7246 {
7247 struct dwarf2_per_cu_data per_cu;
7248
7249 memset (&per_cu, 0, sizeof (per_cu));
7250 per_cu.objfile = objfile;
7251 per_cu.is_debug_types = 0;
7252 per_cu.offset.sect_off = info_ptr - section->buffer;
7253 per_cu.info_or_types_section = section;
7254
7255 init_cutu_and_read_dies_no_follow (&per_cu,
7256 &dwo_file->sections.abbrev,
7257 dwo_file,
7258 create_debug_info_hash_table_reader,
7259 &create_dwo_info_table_data);
7260
7261 info_ptr += per_cu.length;
7262 }
7263
7264 return cu_htab;
7265}
7266
7267/* Subroutine of open_dwo_file to simplify it.
7268 Open the file specified by FILE_NAME and hand it off to BFD for
7269 preliminary analysis. Return a newly initialized bfd *, which
7270 includes a canonicalized copy of FILE_NAME.
7271 In case of trouble, return NULL.
7272 NOTE: This function is derived from symfile_bfd_open. */
7273
7274static bfd *
7275try_open_dwo_file (const char *file_name)
7276{
7277 bfd *sym_bfd;
7278 int desc;
7279 char *absolute_name;
3019eac3
DE
7280
7281 desc = openp (debug_file_directory, OPF_TRY_CWD_FIRST, file_name,
7282 O_RDONLY | O_BINARY, &absolute_name);
7283 if (desc < 0)
7284 return NULL;
7285
7286 sym_bfd = bfd_fopen (absolute_name, gnutarget, FOPEN_RB, desc);
7287 if (!sym_bfd)
7288 {
3019eac3
DE
7289 xfree (absolute_name);
7290 return NULL;
7291 }
7292 bfd_set_cacheable (sym_bfd, 1);
7293
7294 if (!bfd_check_format (sym_bfd, bfd_object))
7295 {
7296 bfd_close (sym_bfd); /* This also closes desc. */
7297 xfree (absolute_name);
7298 return NULL;
7299 }
7300
7301 /* bfd_usrdata exists for applications and libbfd must not touch it. */
7302 gdb_assert (bfd_usrdata (sym_bfd) == NULL);
7303
7304 return sym_bfd;
7305}
7306
7307/* Try to open DWO file DWO_NAME.
7308 COMP_DIR is the DW_AT_comp_dir attribute.
7309 The result is the bfd handle of the file.
7310 If there is a problem finding or opening the file, return NULL.
7311 Upon success, the canonicalized path of the file is stored in the bfd,
7312 same as symfile_bfd_open. */
7313
7314static bfd *
7315open_dwo_file (const char *dwo_name, const char *comp_dir)
7316{
7317 bfd *abfd;
3019eac3
DE
7318
7319 if (IS_ABSOLUTE_PATH (dwo_name))
7320 return try_open_dwo_file (dwo_name);
7321
7322 /* Before trying the search path, try DWO_NAME in COMP_DIR. */
7323
7324 if (comp_dir != NULL)
7325 {
7326 char *path_to_try = concat (comp_dir, SLASH_STRING, dwo_name, NULL);
7327
7328 /* NOTE: If comp_dir is a relative path, this will also try the
7329 search path, which seems useful. */
7330 abfd = try_open_dwo_file (path_to_try);
7331 xfree (path_to_try);
7332 if (abfd != NULL)
7333 return abfd;
7334 }
7335
7336 /* That didn't work, try debug-file-directory, which, despite its name,
7337 is a list of paths. */
7338
7339 if (*debug_file_directory == '\0')
7340 return NULL;
7341
7342 return try_open_dwo_file (dwo_name);
7343}
7344
7345/* Initialize the use of the DWO file specified by DWO_NAME. */
7346
7347static struct dwo_file *
7348init_dwo_file (const char *dwo_name, const char *comp_dir)
7349{
7350 struct objfile *objfile = dwarf2_per_objfile->objfile;
7351 struct dwo_file *dwo_file = OBSTACK_ZALLOC (&objfile->objfile_obstack,
7352 struct dwo_file);
7353 bfd *abfd;
7354 struct cleanup *cleanups;
7355
09406207 7356 if (dwarf2_read_debug)
3019eac3
DE
7357 fprintf_unfiltered (gdb_stdlog, "Reading DWO file %s:\n", dwo_name);
7358
7359 abfd = open_dwo_file (dwo_name, comp_dir);
7360 if (abfd == NULL)
7361 return NULL;
7362 dwo_file->dwo_name = dwo_name;
7363 dwo_file->dwo_bfd = abfd;
7364
7365 cleanups = make_cleanup (free_dwo_file_cleanup, dwo_file);
7366
7367 bfd_map_over_sections (abfd, dwarf2_locate_dwo_sections, dwo_file);
7368
7369 dwo_file->cus = create_debug_info_hash_table (dwo_file);
7370
7371 dwo_file->tus = create_debug_types_hash_table (dwo_file,
7372 dwo_file->sections.types);
7373
7374 discard_cleanups (cleanups);
7375
7376 return dwo_file;
7377}
7378
7379/* Lookup DWO file DWO_NAME. */
7380
7381static struct dwo_file *
7382lookup_dwo_file (char *dwo_name, const char *comp_dir)
7383{
7384 struct dwo_file *dwo_file;
7385 struct dwo_file find_entry;
7386 void **slot;
7387
7388 if (dwarf2_per_objfile->dwo_files == NULL)
7389 dwarf2_per_objfile->dwo_files = allocate_dwo_file_hash_table ();
7390
7391 /* Have we already seen this DWO file? */
7392 find_entry.dwo_name = dwo_name;
7393 slot = htab_find_slot (dwarf2_per_objfile->dwo_files, &find_entry, INSERT);
7394
7395 /* If not, read it in and build a table of the DWOs it contains. */
7396 if (*slot == NULL)
7397 *slot = init_dwo_file (dwo_name, comp_dir);
7398
7399 /* NOTE: This will be NULL if unable to open the file. */
7400 dwo_file = *slot;
7401
7402 return dwo_file;
7403}
7404
7405/* Lookup the DWO CU referenced from THIS_CU in DWO file DWO_NAME.
7406 If non-NULL, comp_dir is the DW_AT_comp_dir attribute.
7407 SIGNATURE is the "dwo_id" of the CU (for consistency we use the same
7408 nomenclature as TUs).
1c658ad5 7409 The result is a pointer to the dwo_unit object or NULL if we didn't find it
3019eac3
DE
7410 (dwo_id mismatch or couldn't find the DWO file). */
7411
7412static struct dwo_unit *
7413lookup_dwo_comp_unit (struct dwarf2_per_cu_data *this_cu,
7414 char *dwo_name, const char *comp_dir,
7415 ULONGEST signature)
7416{
7417 struct objfile *objfile = dwarf2_per_objfile->objfile;
7418 struct dwo_file *dwo_file;
7419
7420 dwo_file = lookup_dwo_file (dwo_name, comp_dir);
7421 if (dwo_file == NULL)
7422 return NULL;
7423
7424 /* Look up the DWO using its signature(dwo_id). */
7425
7426 if (dwo_file->cus != NULL)
7427 {
7428 struct dwo_unit find_dwo_cu, *dwo_cu;
7429
7430 find_dwo_cu.signature = signature;
7431 dwo_cu = htab_find (dwo_file->cus, &find_dwo_cu);
a766d390 7432
3019eac3
DE
7433 if (dwo_cu != NULL)
7434 return dwo_cu;
7435 }
c906108c 7436
3019eac3 7437 /* We didn't find it. This must mean a dwo_id mismatch. */
df8a16a1 7438
3019eac3
DE
7439 complaint (&symfile_complaints,
7440 _("Could not find DWO CU referenced by CU at offset 0x%x"
7441 " [in module %s]"),
7442 this_cu->offset.sect_off, objfile->name);
7443 return NULL;
7444}
c906108c 7445
3019eac3
DE
7446/* Lookup the DWO TU referenced from THIS_TU in DWO file DWO_NAME.
7447 If non-NULL, comp_dir is the DW_AT_comp_dir attribute.
1c658ad5 7448 The result is a pointer to the dwo_unit object or NULL if we didn't find it
3019eac3 7449 (dwo_id mismatch or couldn't find the DWO file). */
debd256d 7450
3019eac3
DE
7451static struct dwo_unit *
7452lookup_dwo_type_unit (struct signatured_type *this_tu,
7453 char *dwo_name, const char *comp_dir)
7454{
7455 struct objfile *objfile = dwarf2_per_objfile->objfile;
7456 struct dwo_file *dwo_file;
cb1df416 7457
3019eac3
DE
7458 dwo_file = lookup_dwo_file (dwo_name, comp_dir);
7459 if (dwo_file == NULL)
7460 return NULL;
cf2c3c16 7461
3019eac3
DE
7462 /* Look up the DWO using its signature(dwo_id). */
7463
7464 if (dwo_file->tus != NULL)
cf2c3c16 7465 {
3019eac3 7466 struct dwo_unit find_dwo_tu, *dwo_tu;
9a619af0 7467
3019eac3
DE
7468 find_dwo_tu.signature = this_tu->signature;
7469 dwo_tu = htab_find (dwo_file->tus, &find_dwo_tu);
7470
7471 if (dwo_tu != NULL)
7472 return dwo_tu;
2e276125 7473 }
9cdd5dbd 7474
3019eac3
DE
7475 /* We didn't find it. This must mean a dwo_id mismatch. */
7476
7477 complaint (&symfile_complaints,
7478 _("Could not find DWO TU referenced by TU at offset 0x%x"
7479 " [in module %s]"),
7480 this_tu->per_cu.offset.sect_off, objfile->name);
7481 return NULL;
5fb290d7
DJ
7482}
7483
3019eac3
DE
7484/* Free all resources associated with DWO_FILE.
7485 Close the DWO file and munmap the sections.
7486 All memory should be on the objfile obstack. */
348e048f
DE
7487
7488static void
3019eac3 7489free_dwo_file (struct dwo_file *dwo_file, struct objfile *objfile)
348e048f 7490{
3019eac3
DE
7491 int ix;
7492 struct dwarf2_section_info *section;
348e048f 7493
3019eac3
DE
7494 gdb_assert (dwo_file->dwo_bfd != objfile->obfd);
7495 bfd_close (dwo_file->dwo_bfd);
348e048f 7496
3019eac3
DE
7497 munmap_section_buffer (&dwo_file->sections.abbrev);
7498 munmap_section_buffer (&dwo_file->sections.info);
7499 munmap_section_buffer (&dwo_file->sections.line);
7500 munmap_section_buffer (&dwo_file->sections.loc);
7501 munmap_section_buffer (&dwo_file->sections.str);
7502 munmap_section_buffer (&dwo_file->sections.str_offsets);
348e048f 7503
3019eac3
DE
7504 for (ix = 0;
7505 VEC_iterate (dwarf2_section_info_def, dwo_file->sections.types,
7506 ix, section);
7507 ++ix)
7508 munmap_section_buffer (section);
348e048f 7509
3019eac3
DE
7510 VEC_free (dwarf2_section_info_def, dwo_file->sections.types);
7511}
348e048f 7512
3019eac3 7513/* Wrapper for free_dwo_file for use in cleanups. */
348e048f 7514
3019eac3
DE
7515static void
7516free_dwo_file_cleanup (void *arg)
7517{
7518 struct dwo_file *dwo_file = (struct dwo_file *) arg;
7519 struct objfile *objfile = dwarf2_per_objfile->objfile;
348e048f 7520
3019eac3
DE
7521 free_dwo_file (dwo_file, objfile);
7522}
348e048f 7523
3019eac3 7524/* Traversal function for free_dwo_files. */
2ab95328 7525
3019eac3
DE
7526static int
7527free_dwo_file_from_slot (void **slot, void *info)
7528{
7529 struct dwo_file *dwo_file = (struct dwo_file *) *slot;
7530 struct objfile *objfile = (struct objfile *) info;
348e048f 7531
3019eac3 7532 free_dwo_file (dwo_file, objfile);
348e048f 7533
3019eac3
DE
7534 return 1;
7535}
348e048f 7536
3019eac3 7537/* Free all resources associated with DWO_FILES. */
348e048f 7538
3019eac3
DE
7539static void
7540free_dwo_files (htab_t dwo_files, struct objfile *objfile)
7541{
7542 htab_traverse_noresize (dwo_files, free_dwo_file_from_slot, objfile);
348e048f 7543}
3019eac3
DE
7544\f
7545/* Read in various DIEs. */
348e048f 7546
d389af10
JK
7547/* qsort helper for inherit_abstract_dies. */
7548
7549static int
7550unsigned_int_compar (const void *ap, const void *bp)
7551{
7552 unsigned int a = *(unsigned int *) ap;
7553 unsigned int b = *(unsigned int *) bp;
7554
7555 return (a > b) - (b > a);
7556}
7557
7558/* DW_AT_abstract_origin inherits whole DIEs (not just their attributes).
3e43a32a
MS
7559 Inherit only the children of the DW_AT_abstract_origin DIE not being
7560 already referenced by DW_AT_abstract_origin from the children of the
7561 current DIE. */
d389af10
JK
7562
7563static void
7564inherit_abstract_dies (struct die_info *die, struct dwarf2_cu *cu)
7565{
7566 struct die_info *child_die;
7567 unsigned die_children_count;
7568 /* CU offsets which were referenced by children of the current DIE. */
b64f50a1
JK
7569 sect_offset *offsets;
7570 sect_offset *offsets_end, *offsetp;
d389af10
JK
7571 /* Parent of DIE - referenced by DW_AT_abstract_origin. */
7572 struct die_info *origin_die;
7573 /* Iterator of the ORIGIN_DIE children. */
7574 struct die_info *origin_child_die;
7575 struct cleanup *cleanups;
7576 struct attribute *attr;
cd02d79d
PA
7577 struct dwarf2_cu *origin_cu;
7578 struct pending **origin_previous_list_in_scope;
d389af10
JK
7579
7580 attr = dwarf2_attr (die, DW_AT_abstract_origin, cu);
7581 if (!attr)
7582 return;
7583
cd02d79d
PA
7584 /* Note that following die references may follow to a die in a
7585 different cu. */
7586
7587 origin_cu = cu;
7588 origin_die = follow_die_ref (die, attr, &origin_cu);
7589
7590 /* We're inheriting ORIGIN's children into the scope we'd put DIE's
7591 symbols in. */
7592 origin_previous_list_in_scope = origin_cu->list_in_scope;
7593 origin_cu->list_in_scope = cu->list_in_scope;
7594
edb3359d
DJ
7595 if (die->tag != origin_die->tag
7596 && !(die->tag == DW_TAG_inlined_subroutine
7597 && origin_die->tag == DW_TAG_subprogram))
d389af10
JK
7598 complaint (&symfile_complaints,
7599 _("DIE 0x%x and its abstract origin 0x%x have different tags"),
b64f50a1 7600 die->offset.sect_off, origin_die->offset.sect_off);
d389af10
JK
7601
7602 child_die = die->child;
7603 die_children_count = 0;
7604 while (child_die && child_die->tag)
7605 {
7606 child_die = sibling_die (child_die);
7607 die_children_count++;
7608 }
7609 offsets = xmalloc (sizeof (*offsets) * die_children_count);
7610 cleanups = make_cleanup (xfree, offsets);
7611
7612 offsets_end = offsets;
7613 child_die = die->child;
7614 while (child_die && child_die->tag)
7615 {
c38f313d
DJ
7616 /* For each CHILD_DIE, find the corresponding child of
7617 ORIGIN_DIE. If there is more than one layer of
7618 DW_AT_abstract_origin, follow them all; there shouldn't be,
7619 but GCC versions at least through 4.4 generate this (GCC PR
7620 40573). */
7621 struct die_info *child_origin_die = child_die;
cd02d79d 7622 struct dwarf2_cu *child_origin_cu = cu;
9a619af0 7623
c38f313d
DJ
7624 while (1)
7625 {
cd02d79d
PA
7626 attr = dwarf2_attr (child_origin_die, DW_AT_abstract_origin,
7627 child_origin_cu);
c38f313d
DJ
7628 if (attr == NULL)
7629 break;
cd02d79d
PA
7630 child_origin_die = follow_die_ref (child_origin_die, attr,
7631 &child_origin_cu);
c38f313d
DJ
7632 }
7633
d389af10
JK
7634 /* According to DWARF3 3.3.8.2 #3 new entries without their abstract
7635 counterpart may exist. */
c38f313d 7636 if (child_origin_die != child_die)
d389af10 7637 {
edb3359d
DJ
7638 if (child_die->tag != child_origin_die->tag
7639 && !(child_die->tag == DW_TAG_inlined_subroutine
7640 && child_origin_die->tag == DW_TAG_subprogram))
d389af10
JK
7641 complaint (&symfile_complaints,
7642 _("Child DIE 0x%x and its abstract origin 0x%x have "
b64f50a1
JK
7643 "different tags"), child_die->offset.sect_off,
7644 child_origin_die->offset.sect_off);
c38f313d
DJ
7645 if (child_origin_die->parent != origin_die)
7646 complaint (&symfile_complaints,
7647 _("Child DIE 0x%x and its abstract origin 0x%x have "
b64f50a1
JK
7648 "different parents"), child_die->offset.sect_off,
7649 child_origin_die->offset.sect_off);
c38f313d
DJ
7650 else
7651 *offsets_end++ = child_origin_die->offset;
d389af10
JK
7652 }
7653 child_die = sibling_die (child_die);
7654 }
7655 qsort (offsets, offsets_end - offsets, sizeof (*offsets),
7656 unsigned_int_compar);
7657 for (offsetp = offsets + 1; offsetp < offsets_end; offsetp++)
b64f50a1 7658 if (offsetp[-1].sect_off == offsetp->sect_off)
3e43a32a
MS
7659 complaint (&symfile_complaints,
7660 _("Multiple children of DIE 0x%x refer "
7661 "to DIE 0x%x as their abstract origin"),
b64f50a1 7662 die->offset.sect_off, offsetp->sect_off);
d389af10
JK
7663
7664 offsetp = offsets;
7665 origin_child_die = origin_die->child;
7666 while (origin_child_die && origin_child_die->tag)
7667 {
7668 /* Is ORIGIN_CHILD_DIE referenced by any of the DIE children? */
b64f50a1
JK
7669 while (offsetp < offsets_end
7670 && offsetp->sect_off < origin_child_die->offset.sect_off)
d389af10 7671 offsetp++;
b64f50a1
JK
7672 if (offsetp >= offsets_end
7673 || offsetp->sect_off > origin_child_die->offset.sect_off)
d389af10
JK
7674 {
7675 /* Found that ORIGIN_CHILD_DIE is really not referenced. */
cd02d79d 7676 process_die (origin_child_die, origin_cu);
d389af10
JK
7677 }
7678 origin_child_die = sibling_die (origin_child_die);
7679 }
cd02d79d 7680 origin_cu->list_in_scope = origin_previous_list_in_scope;
d389af10
JK
7681
7682 do_cleanups (cleanups);
7683}
7684
c906108c 7685static void
e7c27a73 7686read_func_scope (struct die_info *die, struct dwarf2_cu *cu)
c906108c 7687{
e7c27a73 7688 struct objfile *objfile = cu->objfile;
52f0bd74 7689 struct context_stack *new;
c906108c
SS
7690 CORE_ADDR lowpc;
7691 CORE_ADDR highpc;
7692 struct die_info *child_die;
edb3359d 7693 struct attribute *attr, *call_line, *call_file;
c906108c 7694 char *name;
e142c38c 7695 CORE_ADDR baseaddr;
801e3a5b 7696 struct block *block;
edb3359d 7697 int inlined_func = (die->tag == DW_TAG_inlined_subroutine);
34eaf542
TT
7698 VEC (symbolp) *template_args = NULL;
7699 struct template_symbol *templ_func = NULL;
edb3359d
DJ
7700
7701 if (inlined_func)
7702 {
7703 /* If we do not have call site information, we can't show the
7704 caller of this inlined function. That's too confusing, so
7705 only use the scope for local variables. */
7706 call_line = dwarf2_attr (die, DW_AT_call_line, cu);
7707 call_file = dwarf2_attr (die, DW_AT_call_file, cu);
7708 if (call_line == NULL || call_file == NULL)
7709 {
7710 read_lexical_block_scope (die, cu);
7711 return;
7712 }
7713 }
c906108c 7714
e142c38c
DJ
7715 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
7716
94af9270 7717 name = dwarf2_name (die, cu);
c906108c 7718
e8d05480
JB
7719 /* Ignore functions with missing or empty names. These are actually
7720 illegal according to the DWARF standard. */
7721 if (name == NULL)
7722 {
7723 complaint (&symfile_complaints,
b64f50a1
JK
7724 _("missing name for subprogram DIE at %d"),
7725 die->offset.sect_off);
e8d05480
JB
7726 return;
7727 }
7728
7729 /* Ignore functions with missing or invalid low and high pc attributes. */
7730 if (!dwarf2_get_pc_bounds (die, &lowpc, &highpc, cu, NULL))
7731 {
ae4d0c03
PM
7732 attr = dwarf2_attr (die, DW_AT_external, cu);
7733 if (!attr || !DW_UNSND (attr))
7734 complaint (&symfile_complaints,
3e43a32a
MS
7735 _("cannot get low and high bounds "
7736 "for subprogram DIE at %d"),
b64f50a1 7737 die->offset.sect_off);
e8d05480
JB
7738 return;
7739 }
c906108c
SS
7740
7741 lowpc += baseaddr;
7742 highpc += baseaddr;
7743
34eaf542
TT
7744 /* If we have any template arguments, then we must allocate a
7745 different sort of symbol. */
7746 for (child_die = die->child; child_die; child_die = sibling_die (child_die))
7747 {
7748 if (child_die->tag == DW_TAG_template_type_param
7749 || child_die->tag == DW_TAG_template_value_param)
7750 {
7751 templ_func = OBSTACK_ZALLOC (&objfile->objfile_obstack,
7752 struct template_symbol);
7753 templ_func->base.is_cplus_template_function = 1;
7754 break;
7755 }
7756 }
7757
c906108c 7758 new = push_context (0, lowpc);
34eaf542
TT
7759 new->name = new_symbol_full (die, read_type_die (die, cu), cu,
7760 (struct symbol *) templ_func);
4c2df51b 7761
4cecd739
DJ
7762 /* If there is a location expression for DW_AT_frame_base, record
7763 it. */
e142c38c 7764 attr = dwarf2_attr (die, DW_AT_frame_base, cu);
4c2df51b 7765 if (attr)
c034e007
AC
7766 /* FIXME: cagney/2004-01-26: The DW_AT_frame_base's location
7767 expression is being recorded directly in the function's symbol
7768 and not in a separate frame-base object. I guess this hack is
7769 to avoid adding some sort of frame-base adjunct/annex to the
7770 function's symbol :-(. The problem with doing this is that it
7771 results in a function symbol with a location expression that
7772 has nothing to do with the location of the function, ouch! The
7773 relationship should be: a function's symbol has-a frame base; a
7774 frame-base has-a location expression. */
e7c27a73 7775 dwarf2_symbol_mark_computed (attr, new->name, cu);
4c2df51b 7776
e142c38c 7777 cu->list_in_scope = &local_symbols;
c906108c 7778
639d11d3 7779 if (die->child != NULL)
c906108c 7780 {
639d11d3 7781 child_die = die->child;
c906108c
SS
7782 while (child_die && child_die->tag)
7783 {
34eaf542
TT
7784 if (child_die->tag == DW_TAG_template_type_param
7785 || child_die->tag == DW_TAG_template_value_param)
7786 {
7787 struct symbol *arg = new_symbol (child_die, NULL, cu);
7788
f1078f66
DJ
7789 if (arg != NULL)
7790 VEC_safe_push (symbolp, template_args, arg);
34eaf542
TT
7791 }
7792 else
7793 process_die (child_die, cu);
c906108c
SS
7794 child_die = sibling_die (child_die);
7795 }
7796 }
7797
d389af10
JK
7798 inherit_abstract_dies (die, cu);
7799
4a811a97
UW
7800 /* If we have a DW_AT_specification, we might need to import using
7801 directives from the context of the specification DIE. See the
7802 comment in determine_prefix. */
7803 if (cu->language == language_cplus
7804 && dwarf2_attr (die, DW_AT_specification, cu))
7805 {
7806 struct dwarf2_cu *spec_cu = cu;
7807 struct die_info *spec_die = die_specification (die, &spec_cu);
7808
7809 while (spec_die)
7810 {
7811 child_die = spec_die->child;
7812 while (child_die && child_die->tag)
7813 {
7814 if (child_die->tag == DW_TAG_imported_module)
7815 process_die (child_die, spec_cu);
7816 child_die = sibling_die (child_die);
7817 }
7818
7819 /* In some cases, GCC generates specification DIEs that
7820 themselves contain DW_AT_specification attributes. */
7821 spec_die = die_specification (spec_die, &spec_cu);
7822 }
7823 }
7824
c906108c
SS
7825 new = pop_context ();
7826 /* Make a block for the local symbols within. */
801e3a5b
JB
7827 block = finish_block (new->name, &local_symbols, new->old_blocks,
7828 lowpc, highpc, objfile);
7829
df8a16a1 7830 /* For C++, set the block's scope. */
f55ee35c 7831 if (cu->language == language_cplus || cu->language == language_fortran)
df8a16a1 7832 cp_set_block_scope (new->name, block, &objfile->objfile_obstack,
0114d602 7833 determine_prefix (die, cu),
df8a16a1
DJ
7834 processing_has_namespace_info);
7835
801e3a5b
JB
7836 /* If we have address ranges, record them. */
7837 dwarf2_record_block_ranges (die, block, baseaddr, cu);
6e70227d 7838
34eaf542
TT
7839 /* Attach template arguments to function. */
7840 if (! VEC_empty (symbolp, template_args))
7841 {
7842 gdb_assert (templ_func != NULL);
7843
7844 templ_func->n_template_arguments = VEC_length (symbolp, template_args);
7845 templ_func->template_arguments
7846 = obstack_alloc (&objfile->objfile_obstack,
7847 (templ_func->n_template_arguments
7848 * sizeof (struct symbol *)));
7849 memcpy (templ_func->template_arguments,
7850 VEC_address (symbolp, template_args),
7851 (templ_func->n_template_arguments * sizeof (struct symbol *)));
7852 VEC_free (symbolp, template_args);
7853 }
7854
208d8187
JB
7855 /* In C++, we can have functions nested inside functions (e.g., when
7856 a function declares a class that has methods). This means that
7857 when we finish processing a function scope, we may need to go
7858 back to building a containing block's symbol lists. */
7859 local_symbols = new->locals;
7860 param_symbols = new->params;
27aa8d6a 7861 using_directives = new->using_directives;
208d8187 7862
921e78cf
JB
7863 /* If we've finished processing a top-level function, subsequent
7864 symbols go in the file symbol list. */
7865 if (outermost_context_p ())
e142c38c 7866 cu->list_in_scope = &file_symbols;
c906108c
SS
7867}
7868
7869/* Process all the DIES contained within a lexical block scope. Start
7870 a new scope, process the dies, and then close the scope. */
7871
7872static void
e7c27a73 7873read_lexical_block_scope (struct die_info *die, struct dwarf2_cu *cu)
c906108c 7874{
e7c27a73 7875 struct objfile *objfile = cu->objfile;
52f0bd74 7876 struct context_stack *new;
c906108c
SS
7877 CORE_ADDR lowpc, highpc;
7878 struct die_info *child_die;
e142c38c
DJ
7879 CORE_ADDR baseaddr;
7880
7881 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
c906108c
SS
7882
7883 /* Ignore blocks with missing or invalid low and high pc attributes. */
af34e669
DJ
7884 /* ??? Perhaps consider discontiguous blocks defined by DW_AT_ranges
7885 as multiple lexical blocks? Handling children in a sane way would
6e70227d 7886 be nasty. Might be easier to properly extend generic blocks to
af34e669 7887 describe ranges. */
d85a05f0 7888 if (!dwarf2_get_pc_bounds (die, &lowpc, &highpc, cu, NULL))
c906108c
SS
7889 return;
7890 lowpc += baseaddr;
7891 highpc += baseaddr;
7892
7893 push_context (0, lowpc);
639d11d3 7894 if (die->child != NULL)
c906108c 7895 {
639d11d3 7896 child_die = die->child;
c906108c
SS
7897 while (child_die && child_die->tag)
7898 {
e7c27a73 7899 process_die (child_die, cu);
c906108c
SS
7900 child_die = sibling_die (child_die);
7901 }
7902 }
7903 new = pop_context ();
7904
8540c487 7905 if (local_symbols != NULL || using_directives != NULL)
c906108c 7906 {
801e3a5b
JB
7907 struct block *block
7908 = finish_block (0, &local_symbols, new->old_blocks, new->start_addr,
7909 highpc, objfile);
7910
7911 /* Note that recording ranges after traversing children, as we
7912 do here, means that recording a parent's ranges entails
7913 walking across all its children's ranges as they appear in
7914 the address map, which is quadratic behavior.
7915
7916 It would be nicer to record the parent's ranges before
7917 traversing its children, simply overriding whatever you find
7918 there. But since we don't even decide whether to create a
7919 block until after we've traversed its children, that's hard
7920 to do. */
7921 dwarf2_record_block_ranges (die, block, baseaddr, cu);
c906108c
SS
7922 }
7923 local_symbols = new->locals;
27aa8d6a 7924 using_directives = new->using_directives;
c906108c
SS
7925}
7926
96408a79
SA
7927/* Read in DW_TAG_GNU_call_site and insert it to CU->call_site_htab. */
7928
7929static void
7930read_call_site_scope (struct die_info *die, struct dwarf2_cu *cu)
7931{
7932 struct objfile *objfile = cu->objfile;
7933 struct gdbarch *gdbarch = get_objfile_arch (objfile);
7934 CORE_ADDR pc, baseaddr;
7935 struct attribute *attr;
7936 struct call_site *call_site, call_site_local;
7937 void **slot;
7938 int nparams;
7939 struct die_info *child_die;
7940
7941 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
7942
7943 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
7944 if (!attr)
7945 {
7946 complaint (&symfile_complaints,
7947 _("missing DW_AT_low_pc for DW_TAG_GNU_call_site "
7948 "DIE 0x%x [in module %s]"),
b64f50a1 7949 die->offset.sect_off, objfile->name);
96408a79
SA
7950 return;
7951 }
7952 pc = DW_ADDR (attr) + baseaddr;
7953
7954 if (cu->call_site_htab == NULL)
7955 cu->call_site_htab = htab_create_alloc_ex (16, core_addr_hash, core_addr_eq,
7956 NULL, &objfile->objfile_obstack,
7957 hashtab_obstack_allocate, NULL);
7958 call_site_local.pc = pc;
7959 slot = htab_find_slot (cu->call_site_htab, &call_site_local, INSERT);
7960 if (*slot != NULL)
7961 {
7962 complaint (&symfile_complaints,
7963 _("Duplicate PC %s for DW_TAG_GNU_call_site "
7964 "DIE 0x%x [in module %s]"),
b64f50a1 7965 paddress (gdbarch, pc), die->offset.sect_off, objfile->name);
96408a79
SA
7966 return;
7967 }
7968
7969 /* Count parameters at the caller. */
7970
7971 nparams = 0;
7972 for (child_die = die->child; child_die && child_die->tag;
7973 child_die = sibling_die (child_die))
7974 {
7975 if (child_die->tag != DW_TAG_GNU_call_site_parameter)
7976 {
7977 complaint (&symfile_complaints,
7978 _("Tag %d is not DW_TAG_GNU_call_site_parameter in "
7979 "DW_TAG_GNU_call_site child DIE 0x%x [in module %s]"),
b64f50a1 7980 child_die->tag, child_die->offset.sect_off, objfile->name);
96408a79
SA
7981 continue;
7982 }
7983
7984 nparams++;
7985 }
7986
7987 call_site = obstack_alloc (&objfile->objfile_obstack,
7988 (sizeof (*call_site)
7989 + (sizeof (*call_site->parameter)
7990 * (nparams - 1))));
7991 *slot = call_site;
7992 memset (call_site, 0, sizeof (*call_site) - sizeof (*call_site->parameter));
7993 call_site->pc = pc;
7994
7995 if (dwarf2_flag_true_p (die, DW_AT_GNU_tail_call, cu))
7996 {
7997 struct die_info *func_die;
7998
7999 /* Skip also over DW_TAG_inlined_subroutine. */
8000 for (func_die = die->parent;
8001 func_die && func_die->tag != DW_TAG_subprogram
8002 && func_die->tag != DW_TAG_subroutine_type;
8003 func_die = func_die->parent);
8004
8005 /* DW_AT_GNU_all_call_sites is a superset
8006 of DW_AT_GNU_all_tail_call_sites. */
8007 if (func_die
8008 && !dwarf2_flag_true_p (func_die, DW_AT_GNU_all_call_sites, cu)
8009 && !dwarf2_flag_true_p (func_die, DW_AT_GNU_all_tail_call_sites, cu))
8010 {
8011 /* TYPE_TAIL_CALL_LIST is not interesting in functions where it is
8012 not complete. But keep CALL_SITE for look ups via call_site_htab,
8013 both the initial caller containing the real return address PC and
8014 the final callee containing the current PC of a chain of tail
8015 calls do not need to have the tail call list complete. But any
8016 function candidate for a virtual tail call frame searched via
8017 TYPE_TAIL_CALL_LIST must have the tail call list complete to be
8018 determined unambiguously. */
8019 }
8020 else
8021 {
8022 struct type *func_type = NULL;
8023
8024 if (func_die)
8025 func_type = get_die_type (func_die, cu);
8026 if (func_type != NULL)
8027 {
8028 gdb_assert (TYPE_CODE (func_type) == TYPE_CODE_FUNC);
8029
8030 /* Enlist this call site to the function. */
8031 call_site->tail_call_next = TYPE_TAIL_CALL_LIST (func_type);
8032 TYPE_TAIL_CALL_LIST (func_type) = call_site;
8033 }
8034 else
8035 complaint (&symfile_complaints,
8036 _("Cannot find function owning DW_TAG_GNU_call_site "
8037 "DIE 0x%x [in module %s]"),
b64f50a1 8038 die->offset.sect_off, objfile->name);
96408a79
SA
8039 }
8040 }
8041
8042 attr = dwarf2_attr (die, DW_AT_GNU_call_site_target, cu);
8043 if (attr == NULL)
8044 attr = dwarf2_attr (die, DW_AT_abstract_origin, cu);
8045 SET_FIELD_DWARF_BLOCK (call_site->target, NULL);
8046 if (!attr || (attr_form_is_block (attr) && DW_BLOCK (attr)->size == 0))
8047 /* Keep NULL DWARF_BLOCK. */;
8048 else if (attr_form_is_block (attr))
8049 {
8050 struct dwarf2_locexpr_baton *dlbaton;
8051
8052 dlbaton = obstack_alloc (&objfile->objfile_obstack, sizeof (*dlbaton));
8053 dlbaton->data = DW_BLOCK (attr)->data;
8054 dlbaton->size = DW_BLOCK (attr)->size;
8055 dlbaton->per_cu = cu->per_cu;
8056
8057 SET_FIELD_DWARF_BLOCK (call_site->target, dlbaton);
8058 }
8059 else if (is_ref_attr (attr))
8060 {
96408a79
SA
8061 struct dwarf2_cu *target_cu = cu;
8062 struct die_info *target_die;
8063
8064 target_die = follow_die_ref_or_sig (die, attr, &target_cu);
8065 gdb_assert (target_cu->objfile == objfile);
8066 if (die_is_declaration (target_die, target_cu))
8067 {
8068 const char *target_physname;
8069
8070 target_physname = dwarf2_physname (NULL, target_die, target_cu);
8071 if (target_physname == NULL)
8072 complaint (&symfile_complaints,
8073 _("DW_AT_GNU_call_site_target target DIE has invalid "
8074 "physname, for referencing DIE 0x%x [in module %s]"),
b64f50a1 8075 die->offset.sect_off, objfile->name);
96408a79
SA
8076 else
8077 SET_FIELD_PHYSNAME (call_site->target, (char *) target_physname);
8078 }
8079 else
8080 {
8081 CORE_ADDR lowpc;
8082
8083 /* DW_AT_entry_pc should be preferred. */
8084 if (!dwarf2_get_pc_bounds (target_die, &lowpc, NULL, target_cu, NULL))
8085 complaint (&symfile_complaints,
8086 _("DW_AT_GNU_call_site_target target DIE has invalid "
8087 "low pc, for referencing DIE 0x%x [in module %s]"),
b64f50a1 8088 die->offset.sect_off, objfile->name);
96408a79
SA
8089 else
8090 SET_FIELD_PHYSADDR (call_site->target, lowpc + baseaddr);
8091 }
8092 }
8093 else
8094 complaint (&symfile_complaints,
8095 _("DW_TAG_GNU_call_site DW_AT_GNU_call_site_target is neither "
8096 "block nor reference, for DIE 0x%x [in module %s]"),
b64f50a1 8097 die->offset.sect_off, objfile->name);
96408a79
SA
8098
8099 call_site->per_cu = cu->per_cu;
8100
8101 for (child_die = die->child;
8102 child_die && child_die->tag;
8103 child_die = sibling_die (child_die))
8104 {
96408a79 8105 struct call_site_parameter *parameter;
1788b2d3 8106 struct attribute *loc, *origin;
96408a79
SA
8107
8108 if (child_die->tag != DW_TAG_GNU_call_site_parameter)
8109 {
8110 /* Already printed the complaint above. */
8111 continue;
8112 }
8113
8114 gdb_assert (call_site->parameter_count < nparams);
8115 parameter = &call_site->parameter[call_site->parameter_count];
8116
1788b2d3
JK
8117 /* DW_AT_location specifies the register number or DW_AT_abstract_origin
8118 specifies DW_TAG_formal_parameter. Value of the data assumed for the
8119 register is contained in DW_AT_GNU_call_site_value. */
96408a79 8120
24c5c679 8121 loc = dwarf2_attr (child_die, DW_AT_location, cu);
1788b2d3
JK
8122 origin = dwarf2_attr (child_die, DW_AT_abstract_origin, cu);
8123 if (loc == NULL && origin != NULL && is_ref_attr (origin))
8124 {
8125 sect_offset offset;
8126
8127 parameter->kind = CALL_SITE_PARAMETER_PARAM_OFFSET;
8128 offset = dwarf2_get_ref_die_offset (origin);
8129 gdb_assert (offset.sect_off >= cu->header.offset.sect_off);
8130 parameter->u.param_offset.cu_off = (offset.sect_off
8131 - cu->header.offset.sect_off);
8132 }
8133 else if (loc == NULL || origin != NULL || !attr_form_is_block (loc))
96408a79
SA
8134 {
8135 complaint (&symfile_complaints,
8136 _("No DW_FORM_block* DW_AT_location for "
8137 "DW_TAG_GNU_call_site child DIE 0x%x [in module %s]"),
b64f50a1 8138 child_die->offset.sect_off, objfile->name);
96408a79
SA
8139 continue;
8140 }
24c5c679 8141 else
96408a79 8142 {
24c5c679
JK
8143 parameter->u.dwarf_reg = dwarf_block_to_dwarf_reg
8144 (DW_BLOCK (loc)->data, &DW_BLOCK (loc)->data[DW_BLOCK (loc)->size]);
8145 if (parameter->u.dwarf_reg != -1)
8146 parameter->kind = CALL_SITE_PARAMETER_DWARF_REG;
8147 else if (dwarf_block_to_sp_offset (gdbarch, DW_BLOCK (loc)->data,
8148 &DW_BLOCK (loc)->data[DW_BLOCK (loc)->size],
8149 &parameter->u.fb_offset))
8150 parameter->kind = CALL_SITE_PARAMETER_FB_OFFSET;
8151 else
8152 {
8153 complaint (&symfile_complaints,
8154 _("Only single DW_OP_reg or DW_OP_fbreg is supported "
8155 "for DW_FORM_block* DW_AT_location is supported for "
8156 "DW_TAG_GNU_call_site child DIE 0x%x "
8157 "[in module %s]"),
8158 child_die->offset.sect_off, objfile->name);
8159 continue;
8160 }
96408a79
SA
8161 }
8162
8163 attr = dwarf2_attr (child_die, DW_AT_GNU_call_site_value, cu);
8164 if (!attr_form_is_block (attr))
8165 {
8166 complaint (&symfile_complaints,
8167 _("No DW_FORM_block* DW_AT_GNU_call_site_value for "
8168 "DW_TAG_GNU_call_site child DIE 0x%x [in module %s]"),
b64f50a1 8169 child_die->offset.sect_off, objfile->name);
96408a79
SA
8170 continue;
8171 }
8172 parameter->value = DW_BLOCK (attr)->data;
8173 parameter->value_size = DW_BLOCK (attr)->size;
8174
8175 /* Parameters are not pre-cleared by memset above. */
8176 parameter->data_value = NULL;
8177 parameter->data_value_size = 0;
8178 call_site->parameter_count++;
8179
8180 attr = dwarf2_attr (child_die, DW_AT_GNU_call_site_data_value, cu);
8181 if (attr)
8182 {
8183 if (!attr_form_is_block (attr))
8184 complaint (&symfile_complaints,
8185 _("No DW_FORM_block* DW_AT_GNU_call_site_data_value for "
8186 "DW_TAG_GNU_call_site child DIE 0x%x [in module %s]"),
b64f50a1 8187 child_die->offset.sect_off, objfile->name);
96408a79
SA
8188 else
8189 {
8190 parameter->data_value = DW_BLOCK (attr)->data;
8191 parameter->data_value_size = DW_BLOCK (attr)->size;
8192 }
8193 }
8194 }
8195}
8196
43039443 8197/* Get low and high pc attributes from DW_AT_ranges attribute value OFFSET.
ff013f42
JK
8198 Return 1 if the attributes are present and valid, otherwise, return 0.
8199 If RANGES_PST is not NULL we should setup `objfile->psymtabs_addrmap'. */
43039443
JK
8200
8201static int
8202dwarf2_ranges_read (unsigned offset, CORE_ADDR *low_return,
ff013f42
JK
8203 CORE_ADDR *high_return, struct dwarf2_cu *cu,
8204 struct partial_symtab *ranges_pst)
43039443
JK
8205{
8206 struct objfile *objfile = cu->objfile;
8207 struct comp_unit_head *cu_header = &cu->header;
8208 bfd *obfd = objfile->obfd;
8209 unsigned int addr_size = cu_header->addr_size;
8210 CORE_ADDR mask = ~(~(CORE_ADDR)1 << (addr_size * 8 - 1));
8211 /* Base address selection entry. */
8212 CORE_ADDR base;
8213 int found_base;
8214 unsigned int dummy;
8215 gdb_byte *buffer;
8216 CORE_ADDR marker;
8217 int low_set;
8218 CORE_ADDR low = 0;
8219 CORE_ADDR high = 0;
ff013f42 8220 CORE_ADDR baseaddr;
43039443 8221
d00adf39
DE
8222 found_base = cu->base_known;
8223 base = cu->base_address;
43039443 8224
be391dca 8225 dwarf2_read_section (objfile, &dwarf2_per_objfile->ranges);
dce234bc 8226 if (offset >= dwarf2_per_objfile->ranges.size)
43039443
JK
8227 {
8228 complaint (&symfile_complaints,
8229 _("Offset %d out of bounds for DW_AT_ranges attribute"),
8230 offset);
8231 return 0;
8232 }
dce234bc 8233 buffer = dwarf2_per_objfile->ranges.buffer + offset;
43039443
JK
8234
8235 /* Read in the largest possible address. */
8236 marker = read_address (obfd, buffer, cu, &dummy);
8237 if ((marker & mask) == mask)
8238 {
8239 /* If we found the largest possible address, then
8240 read the base address. */
8241 base = read_address (obfd, buffer + addr_size, cu, &dummy);
8242 buffer += 2 * addr_size;
8243 offset += 2 * addr_size;
8244 found_base = 1;
8245 }
8246
8247 low_set = 0;
8248
e7030f15 8249 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
ff013f42 8250
43039443
JK
8251 while (1)
8252 {
8253 CORE_ADDR range_beginning, range_end;
8254
8255 range_beginning = read_address (obfd, buffer, cu, &dummy);
8256 buffer += addr_size;
8257 range_end = read_address (obfd, buffer, cu, &dummy);
8258 buffer += addr_size;
8259 offset += 2 * addr_size;
8260
8261 /* An end of list marker is a pair of zero addresses. */
8262 if (range_beginning == 0 && range_end == 0)
8263 /* Found the end of list entry. */
8264 break;
8265
8266 /* Each base address selection entry is a pair of 2 values.
8267 The first is the largest possible address, the second is
8268 the base address. Check for a base address here. */
8269 if ((range_beginning & mask) == mask)
8270 {
8271 /* If we found the largest possible address, then
8272 read the base address. */
8273 base = read_address (obfd, buffer + addr_size, cu, &dummy);
8274 found_base = 1;
8275 continue;
8276 }
8277
8278 if (!found_base)
8279 {
8280 /* We have no valid base address for the ranges
8281 data. */
8282 complaint (&symfile_complaints,
8283 _("Invalid .debug_ranges data (no base address)"));
8284 return 0;
8285 }
8286
9277c30c
UW
8287 if (range_beginning > range_end)
8288 {
8289 /* Inverted range entries are invalid. */
8290 complaint (&symfile_complaints,
8291 _("Invalid .debug_ranges data (inverted range)"));
8292 return 0;
8293 }
8294
8295 /* Empty range entries have no effect. */
8296 if (range_beginning == range_end)
8297 continue;
8298
43039443
JK
8299 range_beginning += base;
8300 range_end += base;
8301
9277c30c 8302 if (ranges_pst != NULL)
ff013f42 8303 addrmap_set_empty (objfile->psymtabs_addrmap,
3e43a32a
MS
8304 range_beginning + baseaddr,
8305 range_end - 1 + baseaddr,
ff013f42
JK
8306 ranges_pst);
8307
43039443
JK
8308 /* FIXME: This is recording everything as a low-high
8309 segment of consecutive addresses. We should have a
8310 data structure for discontiguous block ranges
8311 instead. */
8312 if (! low_set)
8313 {
8314 low = range_beginning;
8315 high = range_end;
8316 low_set = 1;
8317 }
8318 else
8319 {
8320 if (range_beginning < low)
8321 low = range_beginning;
8322 if (range_end > high)
8323 high = range_end;
8324 }
8325 }
8326
8327 if (! low_set)
8328 /* If the first entry is an end-of-list marker, the range
8329 describes an empty scope, i.e. no instructions. */
8330 return 0;
8331
8332 if (low_return)
8333 *low_return = low;
8334 if (high_return)
8335 *high_return = high;
8336 return 1;
8337}
8338
af34e669
DJ
8339/* Get low and high pc attributes from a die. Return 1 if the attributes
8340 are present and valid, otherwise, return 0. Return -1 if the range is
8341 discontinuous, i.e. derived from DW_AT_ranges information. */
380bca97 8342
c906108c 8343static int
af34e669 8344dwarf2_get_pc_bounds (struct die_info *die, CORE_ADDR *lowpc,
d85a05f0
DJ
8345 CORE_ADDR *highpc, struct dwarf2_cu *cu,
8346 struct partial_symtab *pst)
c906108c
SS
8347{
8348 struct attribute *attr;
91da1414 8349 struct attribute *attr_high;
af34e669
DJ
8350 CORE_ADDR low = 0;
8351 CORE_ADDR high = 0;
8352 int ret = 0;
c906108c 8353
91da1414
MW
8354 attr_high = dwarf2_attr (die, DW_AT_high_pc, cu);
8355 if (attr_high)
af34e669 8356 {
e142c38c 8357 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
af34e669 8358 if (attr)
91da1414
MW
8359 {
8360 low = DW_ADDR (attr);
3019eac3
DE
8361 if (attr_high->form == DW_FORM_addr
8362 || attr_high->form == DW_FORM_GNU_addr_index)
91da1414
MW
8363 high = DW_ADDR (attr_high);
8364 else
8365 high = low + DW_UNSND (attr_high);
8366 }
af34e669
DJ
8367 else
8368 /* Found high w/o low attribute. */
8369 return 0;
8370
8371 /* Found consecutive range of addresses. */
8372 ret = 1;
8373 }
c906108c 8374 else
af34e669 8375 {
e142c38c 8376 attr = dwarf2_attr (die, DW_AT_ranges, cu);
af34e669
DJ
8377 if (attr != NULL)
8378 {
2e3cf129
DE
8379 unsigned int ranges_offset = DW_UNSND (attr) + cu->ranges_base;
8380
af34e669 8381 /* Value of the DW_AT_ranges attribute is the offset in the
a604369a 8382 .debug_ranges section. */
2e3cf129 8383 if (!dwarf2_ranges_read (ranges_offset, &low, &high, cu, pst))
af34e669 8384 return 0;
43039443 8385 /* Found discontinuous range of addresses. */
af34e669
DJ
8386 ret = -1;
8387 }
8388 }
c906108c 8389
9373cf26
JK
8390 /* read_partial_die has also the strict LOW < HIGH requirement. */
8391 if (high <= low)
c906108c
SS
8392 return 0;
8393
8394 /* When using the GNU linker, .gnu.linkonce. sections are used to
8395 eliminate duplicate copies of functions and vtables and such.
8396 The linker will arbitrarily choose one and discard the others.
8397 The AT_*_pc values for such functions refer to local labels in
8398 these sections. If the section from that file was discarded, the
8399 labels are not in the output, so the relocs get a value of 0.
8400 If this is a discarded function, mark the pc bounds as invalid,
8401 so that GDB will ignore it. */
72dca2f5 8402 if (low == 0 && !dwarf2_per_objfile->has_section_at_zero)
c906108c
SS
8403 return 0;
8404
8405 *lowpc = low;
96408a79
SA
8406 if (highpc)
8407 *highpc = high;
af34e669 8408 return ret;
c906108c
SS
8409}
8410
b084d499
JB
8411/* Assuming that DIE represents a subprogram DIE or a lexical block, get
8412 its low and high PC addresses. Do nothing if these addresses could not
8413 be determined. Otherwise, set LOWPC to the low address if it is smaller,
8414 and HIGHPC to the high address if greater than HIGHPC. */
8415
8416static void
8417dwarf2_get_subprogram_pc_bounds (struct die_info *die,
8418 CORE_ADDR *lowpc, CORE_ADDR *highpc,
8419 struct dwarf2_cu *cu)
8420{
8421 CORE_ADDR low, high;
8422 struct die_info *child = die->child;
8423
d85a05f0 8424 if (dwarf2_get_pc_bounds (die, &low, &high, cu, NULL))
b084d499
JB
8425 {
8426 *lowpc = min (*lowpc, low);
8427 *highpc = max (*highpc, high);
8428 }
8429
8430 /* If the language does not allow nested subprograms (either inside
8431 subprograms or lexical blocks), we're done. */
8432 if (cu->language != language_ada)
8433 return;
6e70227d 8434
b084d499
JB
8435 /* Check all the children of the given DIE. If it contains nested
8436 subprograms, then check their pc bounds. Likewise, we need to
8437 check lexical blocks as well, as they may also contain subprogram
8438 definitions. */
8439 while (child && child->tag)
8440 {
8441 if (child->tag == DW_TAG_subprogram
8442 || child->tag == DW_TAG_lexical_block)
8443 dwarf2_get_subprogram_pc_bounds (child, lowpc, highpc, cu);
8444 child = sibling_die (child);
8445 }
8446}
8447
fae299cd
DC
8448/* Get the low and high pc's represented by the scope DIE, and store
8449 them in *LOWPC and *HIGHPC. If the correct values can't be
8450 determined, set *LOWPC to -1 and *HIGHPC to 0. */
8451
8452static void
8453get_scope_pc_bounds (struct die_info *die,
8454 CORE_ADDR *lowpc, CORE_ADDR *highpc,
8455 struct dwarf2_cu *cu)
8456{
8457 CORE_ADDR best_low = (CORE_ADDR) -1;
8458 CORE_ADDR best_high = (CORE_ADDR) 0;
8459 CORE_ADDR current_low, current_high;
8460
d85a05f0 8461 if (dwarf2_get_pc_bounds (die, &current_low, &current_high, cu, NULL))
fae299cd
DC
8462 {
8463 best_low = current_low;
8464 best_high = current_high;
8465 }
8466 else
8467 {
8468 struct die_info *child = die->child;
8469
8470 while (child && child->tag)
8471 {
8472 switch (child->tag) {
8473 case DW_TAG_subprogram:
b084d499 8474 dwarf2_get_subprogram_pc_bounds (child, &best_low, &best_high, cu);
fae299cd
DC
8475 break;
8476 case DW_TAG_namespace:
f55ee35c 8477 case DW_TAG_module:
fae299cd
DC
8478 /* FIXME: carlton/2004-01-16: Should we do this for
8479 DW_TAG_class_type/DW_TAG_structure_type, too? I think
8480 that current GCC's always emit the DIEs corresponding
8481 to definitions of methods of classes as children of a
8482 DW_TAG_compile_unit or DW_TAG_namespace (as opposed to
8483 the DIEs giving the declarations, which could be
8484 anywhere). But I don't see any reason why the
8485 standards says that they have to be there. */
8486 get_scope_pc_bounds (child, &current_low, &current_high, cu);
8487
8488 if (current_low != ((CORE_ADDR) -1))
8489 {
8490 best_low = min (best_low, current_low);
8491 best_high = max (best_high, current_high);
8492 }
8493 break;
8494 default:
0963b4bd 8495 /* Ignore. */
fae299cd
DC
8496 break;
8497 }
8498
8499 child = sibling_die (child);
8500 }
8501 }
8502
8503 *lowpc = best_low;
8504 *highpc = best_high;
8505}
8506
801e3a5b
JB
8507/* Record the address ranges for BLOCK, offset by BASEADDR, as given
8508 in DIE. */
380bca97 8509
801e3a5b
JB
8510static void
8511dwarf2_record_block_ranges (struct die_info *die, struct block *block,
8512 CORE_ADDR baseaddr, struct dwarf2_cu *cu)
8513{
bb5ed363 8514 struct objfile *objfile = cu->objfile;
801e3a5b 8515 struct attribute *attr;
91da1414 8516 struct attribute *attr_high;
801e3a5b 8517
91da1414
MW
8518 attr_high = dwarf2_attr (die, DW_AT_high_pc, cu);
8519 if (attr_high)
801e3a5b 8520 {
801e3a5b
JB
8521 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
8522 if (attr)
8523 {
8524 CORE_ADDR low = DW_ADDR (attr);
91da1414 8525 CORE_ADDR high;
3019eac3
DE
8526 if (attr_high->form == DW_FORM_addr
8527 || attr_high->form == DW_FORM_GNU_addr_index)
91da1414
MW
8528 high = DW_ADDR (attr_high);
8529 else
8530 high = low + DW_UNSND (attr_high);
9a619af0 8531
801e3a5b
JB
8532 record_block_range (block, baseaddr + low, baseaddr + high - 1);
8533 }
8534 }
8535
8536 attr = dwarf2_attr (die, DW_AT_ranges, cu);
8537 if (attr)
8538 {
bb5ed363 8539 bfd *obfd = objfile->obfd;
801e3a5b
JB
8540
8541 /* The value of the DW_AT_ranges attribute is the offset of the
8542 address range list in the .debug_ranges section. */
2e3cf129 8543 unsigned long offset = DW_UNSND (attr) + cu->ranges_base;
dce234bc 8544 gdb_byte *buffer = dwarf2_per_objfile->ranges.buffer + offset;
801e3a5b
JB
8545
8546 /* For some target architectures, but not others, the
8547 read_address function sign-extends the addresses it returns.
8548 To recognize base address selection entries, we need a
8549 mask. */
8550 unsigned int addr_size = cu->header.addr_size;
8551 CORE_ADDR base_select_mask = ~(~(CORE_ADDR)1 << (addr_size * 8 - 1));
8552
8553 /* The base address, to which the next pair is relative. Note
8554 that this 'base' is a DWARF concept: most entries in a range
8555 list are relative, to reduce the number of relocs against the
8556 debugging information. This is separate from this function's
8557 'baseaddr' argument, which GDB uses to relocate debugging
8558 information from a shared library based on the address at
8559 which the library was loaded. */
d00adf39
DE
8560 CORE_ADDR base = cu->base_address;
8561 int base_known = cu->base_known;
801e3a5b 8562
be391dca 8563 gdb_assert (dwarf2_per_objfile->ranges.readin);
dce234bc 8564 if (offset >= dwarf2_per_objfile->ranges.size)
801e3a5b
JB
8565 {
8566 complaint (&symfile_complaints,
8567 _("Offset %lu out of bounds for DW_AT_ranges attribute"),
8568 offset);
8569 return;
8570 }
8571
8572 for (;;)
8573 {
8574 unsigned int bytes_read;
8575 CORE_ADDR start, end;
8576
8577 start = read_address (obfd, buffer, cu, &bytes_read);
8578 buffer += bytes_read;
8579 end = read_address (obfd, buffer, cu, &bytes_read);
8580 buffer += bytes_read;
8581
8582 /* Did we find the end of the range list? */
8583 if (start == 0 && end == 0)
8584 break;
8585
8586 /* Did we find a base address selection entry? */
8587 else if ((start & base_select_mask) == base_select_mask)
8588 {
8589 base = end;
8590 base_known = 1;
8591 }
8592
8593 /* We found an ordinary address range. */
8594 else
8595 {
8596 if (!base_known)
8597 {
8598 complaint (&symfile_complaints,
3e43a32a
MS
8599 _("Invalid .debug_ranges data "
8600 "(no base address)"));
801e3a5b
JB
8601 return;
8602 }
8603
9277c30c
UW
8604 if (start > end)
8605 {
8606 /* Inverted range entries are invalid. */
8607 complaint (&symfile_complaints,
8608 _("Invalid .debug_ranges data "
8609 "(inverted range)"));
8610 return;
8611 }
8612
8613 /* Empty range entries have no effect. */
8614 if (start == end)
8615 continue;
8616
6e70227d
DE
8617 record_block_range (block,
8618 baseaddr + base + start,
801e3a5b
JB
8619 baseaddr + base + end - 1);
8620 }
8621 }
8622 }
8623}
8624
685b1105
JK
8625/* Check whether the producer field indicates either of GCC < 4.6, or the
8626 Intel C/C++ compiler, and cache the result in CU. */
60d5a603 8627
685b1105
JK
8628static void
8629check_producer (struct dwarf2_cu *cu)
60d5a603
JK
8630{
8631 const char *cs;
8632 int major, minor, release;
8633
8634 if (cu->producer == NULL)
8635 {
8636 /* For unknown compilers expect their behavior is DWARF version
8637 compliant.
8638
8639 GCC started to support .debug_types sections by -gdwarf-4 since
8640 gcc-4.5.x. As the .debug_types sections are missing DW_AT_producer
8641 for their space efficiency GDB cannot workaround gcc-4.5.x -gdwarf-4
8642 combination. gcc-4.5.x -gdwarf-4 binaries have DW_AT_accessibility
8643 interpreted incorrectly by GDB now - GCC PR debug/48229. */
60d5a603 8644 }
685b1105 8645 else if (strncmp (cu->producer, "GNU ", strlen ("GNU ")) == 0)
60d5a603 8646 {
685b1105
JK
8647 /* Skip any identifier after "GNU " - such as "C++" or "Java". */
8648
ba919b58
TT
8649 cs = &cu->producer[strlen ("GNU ")];
8650 while (*cs && !isdigit (*cs))
8651 cs++;
8652 if (sscanf (cs, "%d.%d.%d", &major, &minor, &release) != 3)
8653 {
8654 /* Not recognized as GCC. */
8655 }
8656 else
685b1105
JK
8657 cu->producer_is_gxx_lt_4_6 = major < 4 || (major == 4 && minor < 6);
8658 }
8659 else if (strncmp (cu->producer, "Intel(R) C", strlen ("Intel(R) C")) == 0)
8660 cu->producer_is_icc = 1;
8661 else
8662 {
8663 /* For other non-GCC compilers, expect their behavior is DWARF version
8664 compliant. */
60d5a603
JK
8665 }
8666
ba919b58 8667 cu->checked_producer = 1;
685b1105 8668}
ba919b58 8669
685b1105
JK
8670/* Check for GCC PR debug/45124 fix which is not present in any G++ version up
8671 to 4.5.any while it is present already in G++ 4.6.0 - the PR has been fixed
8672 during 4.6.0 experimental. */
8673
8674static int
8675producer_is_gxx_lt_4_6 (struct dwarf2_cu *cu)
8676{
8677 if (!cu->checked_producer)
8678 check_producer (cu);
8679
8680 return cu->producer_is_gxx_lt_4_6;
60d5a603
JK
8681}
8682
8683/* Return the default accessibility type if it is not overriden by
8684 DW_AT_accessibility. */
8685
8686static enum dwarf_access_attribute
8687dwarf2_default_access_attribute (struct die_info *die, struct dwarf2_cu *cu)
8688{
8689 if (cu->header.version < 3 || producer_is_gxx_lt_4_6 (cu))
8690 {
8691 /* The default DWARF 2 accessibility for members is public, the default
8692 accessibility for inheritance is private. */
8693
8694 if (die->tag != DW_TAG_inheritance)
8695 return DW_ACCESS_public;
8696 else
8697 return DW_ACCESS_private;
8698 }
8699 else
8700 {
8701 /* DWARF 3+ defines the default accessibility a different way. The same
8702 rules apply now for DW_TAG_inheritance as for the members and it only
8703 depends on the container kind. */
8704
8705 if (die->parent->tag == DW_TAG_class_type)
8706 return DW_ACCESS_private;
8707 else
8708 return DW_ACCESS_public;
8709 }
8710}
8711
74ac6d43
TT
8712/* Look for DW_AT_data_member_location. Set *OFFSET to the byte
8713 offset. If the attribute was not found return 0, otherwise return
8714 1. If it was found but could not properly be handled, set *OFFSET
8715 to 0. */
8716
8717static int
8718handle_data_member_location (struct die_info *die, struct dwarf2_cu *cu,
8719 LONGEST *offset)
8720{
8721 struct attribute *attr;
8722
8723 attr = dwarf2_attr (die, DW_AT_data_member_location, cu);
8724 if (attr != NULL)
8725 {
8726 *offset = 0;
8727
8728 /* Note that we do not check for a section offset first here.
8729 This is because DW_AT_data_member_location is new in DWARF 4,
8730 so if we see it, we can assume that a constant form is really
8731 a constant and not a section offset. */
8732 if (attr_form_is_constant (attr))
8733 *offset = dwarf2_get_attr_constant_value (attr, 0);
8734 else if (attr_form_is_section_offset (attr))
8735 dwarf2_complex_location_expr_complaint ();
8736 else if (attr_form_is_block (attr))
8737 *offset = decode_locdesc (DW_BLOCK (attr), cu);
8738 else
8739 dwarf2_complex_location_expr_complaint ();
8740
8741 return 1;
8742 }
8743
8744 return 0;
8745}
8746
c906108c
SS
8747/* Add an aggregate field to the field list. */
8748
8749static void
107d2387 8750dwarf2_add_field (struct field_info *fip, struct die_info *die,
e7c27a73 8751 struct dwarf2_cu *cu)
6e70227d 8752{
e7c27a73 8753 struct objfile *objfile = cu->objfile;
5e2b427d 8754 struct gdbarch *gdbarch = get_objfile_arch (objfile);
c906108c
SS
8755 struct nextfield *new_field;
8756 struct attribute *attr;
8757 struct field *fp;
8758 char *fieldname = "";
8759
8760 /* Allocate a new field list entry and link it in. */
8761 new_field = (struct nextfield *) xmalloc (sizeof (struct nextfield));
b8c9b27d 8762 make_cleanup (xfree, new_field);
c906108c 8763 memset (new_field, 0, sizeof (struct nextfield));
7d0ccb61
DJ
8764
8765 if (die->tag == DW_TAG_inheritance)
8766 {
8767 new_field->next = fip->baseclasses;
8768 fip->baseclasses = new_field;
8769 }
8770 else
8771 {
8772 new_field->next = fip->fields;
8773 fip->fields = new_field;
8774 }
c906108c
SS
8775 fip->nfields++;
8776
e142c38c 8777 attr = dwarf2_attr (die, DW_AT_accessibility, cu);
c906108c
SS
8778 if (attr)
8779 new_field->accessibility = DW_UNSND (attr);
60d5a603
JK
8780 else
8781 new_field->accessibility = dwarf2_default_access_attribute (die, cu);
c906108c
SS
8782 if (new_field->accessibility != DW_ACCESS_public)
8783 fip->non_public_fields = 1;
60d5a603 8784
e142c38c 8785 attr = dwarf2_attr (die, DW_AT_virtuality, cu);
c906108c
SS
8786 if (attr)
8787 new_field->virtuality = DW_UNSND (attr);
60d5a603
JK
8788 else
8789 new_field->virtuality = DW_VIRTUALITY_none;
c906108c
SS
8790
8791 fp = &new_field->field;
a9a9bd0f 8792
e142c38c 8793 if (die->tag == DW_TAG_member && ! die_is_declaration (die, cu))
c906108c 8794 {
74ac6d43
TT
8795 LONGEST offset;
8796
a9a9bd0f 8797 /* Data member other than a C++ static data member. */
6e70227d 8798
c906108c 8799 /* Get type of field. */
e7c27a73 8800 fp->type = die_type (die, cu);
c906108c 8801
d6a843b5 8802 SET_FIELD_BITPOS (*fp, 0);
01ad7f36 8803
c906108c 8804 /* Get bit size of field (zero if none). */
e142c38c 8805 attr = dwarf2_attr (die, DW_AT_bit_size, cu);
c906108c
SS
8806 if (attr)
8807 {
8808 FIELD_BITSIZE (*fp) = DW_UNSND (attr);
8809 }
8810 else
8811 {
8812 FIELD_BITSIZE (*fp) = 0;
8813 }
8814
8815 /* Get bit offset of field. */
74ac6d43
TT
8816 if (handle_data_member_location (die, cu, &offset))
8817 SET_FIELD_BITPOS (*fp, offset * bits_per_byte);
e142c38c 8818 attr = dwarf2_attr (die, DW_AT_bit_offset, cu);
c906108c
SS
8819 if (attr)
8820 {
5e2b427d 8821 if (gdbarch_bits_big_endian (gdbarch))
c906108c
SS
8822 {
8823 /* For big endian bits, the DW_AT_bit_offset gives the
c5aa993b
JM
8824 additional bit offset from the MSB of the containing
8825 anonymous object to the MSB of the field. We don't
8826 have to do anything special since we don't need to
8827 know the size of the anonymous object. */
f41f5e61 8828 SET_FIELD_BITPOS (*fp, FIELD_BITPOS (*fp) + DW_UNSND (attr));
c906108c
SS
8829 }
8830 else
8831 {
8832 /* For little endian bits, compute the bit offset to the
c5aa993b
JM
8833 MSB of the anonymous object, subtract off the number of
8834 bits from the MSB of the field to the MSB of the
8835 object, and then subtract off the number of bits of
8836 the field itself. The result is the bit offset of
8837 the LSB of the field. */
c906108c
SS
8838 int anonymous_size;
8839 int bit_offset = DW_UNSND (attr);
8840
e142c38c 8841 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
c906108c
SS
8842 if (attr)
8843 {
8844 /* The size of the anonymous object containing
8845 the bit field is explicit, so use the
8846 indicated size (in bytes). */
8847 anonymous_size = DW_UNSND (attr);
8848 }
8849 else
8850 {
8851 /* The size of the anonymous object containing
8852 the bit field must be inferred from the type
8853 attribute of the data member containing the
8854 bit field. */
8855 anonymous_size = TYPE_LENGTH (fp->type);
8856 }
f41f5e61
PA
8857 SET_FIELD_BITPOS (*fp,
8858 (FIELD_BITPOS (*fp)
8859 + anonymous_size * bits_per_byte
8860 - bit_offset - FIELD_BITSIZE (*fp)));
c906108c
SS
8861 }
8862 }
8863
8864 /* Get name of field. */
39cbfefa
DJ
8865 fieldname = dwarf2_name (die, cu);
8866 if (fieldname == NULL)
8867 fieldname = "";
d8151005
DJ
8868
8869 /* The name is already allocated along with this objfile, so we don't
8870 need to duplicate it for the type. */
8871 fp->name = fieldname;
c906108c
SS
8872
8873 /* Change accessibility for artificial fields (e.g. virtual table
c5aa993b 8874 pointer or virtual base class pointer) to private. */
e142c38c 8875 if (dwarf2_attr (die, DW_AT_artificial, cu))
c906108c 8876 {
d48cc9dd 8877 FIELD_ARTIFICIAL (*fp) = 1;
c906108c
SS
8878 new_field->accessibility = DW_ACCESS_private;
8879 fip->non_public_fields = 1;
8880 }
8881 }
a9a9bd0f 8882 else if (die->tag == DW_TAG_member || die->tag == DW_TAG_variable)
c906108c 8883 {
a9a9bd0f
DC
8884 /* C++ static member. */
8885
8886 /* NOTE: carlton/2002-11-05: It should be a DW_TAG_member that
8887 is a declaration, but all versions of G++ as of this writing
8888 (so through at least 3.2.1) incorrectly generate
8889 DW_TAG_variable tags. */
6e70227d 8890
ff355380 8891 const char *physname;
c906108c 8892
a9a9bd0f 8893 /* Get name of field. */
39cbfefa
DJ
8894 fieldname = dwarf2_name (die, cu);
8895 if (fieldname == NULL)
c906108c
SS
8896 return;
8897
254e6b9e 8898 attr = dwarf2_attr (die, DW_AT_const_value, cu);
3863f96c
DE
8899 if (attr
8900 /* Only create a symbol if this is an external value.
8901 new_symbol checks this and puts the value in the global symbol
8902 table, which we want. If it is not external, new_symbol
8903 will try to put the value in cu->list_in_scope which is wrong. */
8904 && dwarf2_flag_true_p (die, DW_AT_external, cu))
254e6b9e
DE
8905 {
8906 /* A static const member, not much different than an enum as far as
8907 we're concerned, except that we can support more types. */
8908 new_symbol (die, NULL, cu);
8909 }
8910
2df3850c 8911 /* Get physical name. */
ff355380 8912 physname = dwarf2_physname (fieldname, die, cu);
c906108c 8913
d8151005
DJ
8914 /* The name is already allocated along with this objfile, so we don't
8915 need to duplicate it for the type. */
8916 SET_FIELD_PHYSNAME (*fp, physname ? physname : "");
e7c27a73 8917 FIELD_TYPE (*fp) = die_type (die, cu);
d8151005 8918 FIELD_NAME (*fp) = fieldname;
c906108c
SS
8919 }
8920 else if (die->tag == DW_TAG_inheritance)
8921 {
74ac6d43 8922 LONGEST offset;
d4b96c9a 8923
74ac6d43
TT
8924 /* C++ base class field. */
8925 if (handle_data_member_location (die, cu, &offset))
8926 SET_FIELD_BITPOS (*fp, offset * bits_per_byte);
c906108c 8927 FIELD_BITSIZE (*fp) = 0;
e7c27a73 8928 FIELD_TYPE (*fp) = die_type (die, cu);
c906108c
SS
8929 FIELD_NAME (*fp) = type_name_no_tag (fp->type);
8930 fip->nbaseclasses++;
8931 }
8932}
8933
98751a41
JK
8934/* Add a typedef defined in the scope of the FIP's class. */
8935
8936static void
8937dwarf2_add_typedef (struct field_info *fip, struct die_info *die,
8938 struct dwarf2_cu *cu)
6e70227d 8939{
98751a41 8940 struct objfile *objfile = cu->objfile;
98751a41
JK
8941 struct typedef_field_list *new_field;
8942 struct attribute *attr;
8943 struct typedef_field *fp;
8944 char *fieldname = "";
8945
8946 /* Allocate a new field list entry and link it in. */
8947 new_field = xzalloc (sizeof (*new_field));
8948 make_cleanup (xfree, new_field);
8949
8950 gdb_assert (die->tag == DW_TAG_typedef);
8951
8952 fp = &new_field->field;
8953
8954 /* Get name of field. */
8955 fp->name = dwarf2_name (die, cu);
8956 if (fp->name == NULL)
8957 return;
8958
8959 fp->type = read_type_die (die, cu);
8960
8961 new_field->next = fip->typedef_field_list;
8962 fip->typedef_field_list = new_field;
8963 fip->typedef_field_list_count++;
8964}
8965
c906108c
SS
8966/* Create the vector of fields, and attach it to the type. */
8967
8968static void
fba45db2 8969dwarf2_attach_fields_to_type (struct field_info *fip, struct type *type,
e7c27a73 8970 struct dwarf2_cu *cu)
c906108c
SS
8971{
8972 int nfields = fip->nfields;
8973
8974 /* Record the field count, allocate space for the array of fields,
8975 and create blank accessibility bitfields if necessary. */
8976 TYPE_NFIELDS (type) = nfields;
8977 TYPE_FIELDS (type) = (struct field *)
8978 TYPE_ALLOC (type, sizeof (struct field) * nfields);
8979 memset (TYPE_FIELDS (type), 0, sizeof (struct field) * nfields);
8980
b4ba55a1 8981 if (fip->non_public_fields && cu->language != language_ada)
c906108c
SS
8982 {
8983 ALLOCATE_CPLUS_STRUCT_TYPE (type);
8984
8985 TYPE_FIELD_PRIVATE_BITS (type) =
8986 (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
8987 B_CLRALL (TYPE_FIELD_PRIVATE_BITS (type), nfields);
8988
8989 TYPE_FIELD_PROTECTED_BITS (type) =
8990 (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
8991 B_CLRALL (TYPE_FIELD_PROTECTED_BITS (type), nfields);
8992
774b6a14
TT
8993 TYPE_FIELD_IGNORE_BITS (type) =
8994 (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
8995 B_CLRALL (TYPE_FIELD_IGNORE_BITS (type), nfields);
c906108c
SS
8996 }
8997
8998 /* If the type has baseclasses, allocate and clear a bit vector for
8999 TYPE_FIELD_VIRTUAL_BITS. */
b4ba55a1 9000 if (fip->nbaseclasses && cu->language != language_ada)
c906108c
SS
9001 {
9002 int num_bytes = B_BYTES (fip->nbaseclasses);
fe1b8b76 9003 unsigned char *pointer;
c906108c
SS
9004
9005 ALLOCATE_CPLUS_STRUCT_TYPE (type);
fe1b8b76
JB
9006 pointer = TYPE_ALLOC (type, num_bytes);
9007 TYPE_FIELD_VIRTUAL_BITS (type) = pointer;
c906108c
SS
9008 B_CLRALL (TYPE_FIELD_VIRTUAL_BITS (type), fip->nbaseclasses);
9009 TYPE_N_BASECLASSES (type) = fip->nbaseclasses;
9010 }
9011
3e43a32a
MS
9012 /* Copy the saved-up fields into the field vector. Start from the head of
9013 the list, adding to the tail of the field array, so that they end up in
9014 the same order in the array in which they were added to the list. */
c906108c
SS
9015 while (nfields-- > 0)
9016 {
7d0ccb61
DJ
9017 struct nextfield *fieldp;
9018
9019 if (fip->fields)
9020 {
9021 fieldp = fip->fields;
9022 fip->fields = fieldp->next;
9023 }
9024 else
9025 {
9026 fieldp = fip->baseclasses;
9027 fip->baseclasses = fieldp->next;
9028 }
9029
9030 TYPE_FIELD (type, nfields) = fieldp->field;
9031 switch (fieldp->accessibility)
c906108c 9032 {
c5aa993b 9033 case DW_ACCESS_private:
b4ba55a1
JB
9034 if (cu->language != language_ada)
9035 SET_TYPE_FIELD_PRIVATE (type, nfields);
c5aa993b 9036 break;
c906108c 9037
c5aa993b 9038 case DW_ACCESS_protected:
b4ba55a1
JB
9039 if (cu->language != language_ada)
9040 SET_TYPE_FIELD_PROTECTED (type, nfields);
c5aa993b 9041 break;
c906108c 9042
c5aa993b
JM
9043 case DW_ACCESS_public:
9044 break;
c906108c 9045
c5aa993b
JM
9046 default:
9047 /* Unknown accessibility. Complain and treat it as public. */
9048 {
e2e0b3e5 9049 complaint (&symfile_complaints, _("unsupported accessibility %d"),
7d0ccb61 9050 fieldp->accessibility);
c5aa993b
JM
9051 }
9052 break;
c906108c
SS
9053 }
9054 if (nfields < fip->nbaseclasses)
9055 {
7d0ccb61 9056 switch (fieldp->virtuality)
c906108c 9057 {
c5aa993b
JM
9058 case DW_VIRTUALITY_virtual:
9059 case DW_VIRTUALITY_pure_virtual:
b4ba55a1 9060 if (cu->language == language_ada)
a73c6dcd 9061 error (_("unexpected virtuality in component of Ada type"));
c5aa993b
JM
9062 SET_TYPE_FIELD_VIRTUAL (type, nfields);
9063 break;
c906108c
SS
9064 }
9065 }
c906108c
SS
9066 }
9067}
9068
c906108c
SS
9069/* Add a member function to the proper fieldlist. */
9070
9071static void
107d2387 9072dwarf2_add_member_fn (struct field_info *fip, struct die_info *die,
e7c27a73 9073 struct type *type, struct dwarf2_cu *cu)
c906108c 9074{
e7c27a73 9075 struct objfile *objfile = cu->objfile;
c906108c
SS
9076 struct attribute *attr;
9077 struct fnfieldlist *flp;
9078 int i;
9079 struct fn_field *fnp;
9080 char *fieldname;
c906108c 9081 struct nextfnfield *new_fnfield;
f792889a 9082 struct type *this_type;
60d5a603 9083 enum dwarf_access_attribute accessibility;
c906108c 9084
b4ba55a1 9085 if (cu->language == language_ada)
a73c6dcd 9086 error (_("unexpected member function in Ada type"));
b4ba55a1 9087
2df3850c 9088 /* Get name of member function. */
39cbfefa
DJ
9089 fieldname = dwarf2_name (die, cu);
9090 if (fieldname == NULL)
2df3850c 9091 return;
c906108c 9092
c906108c
SS
9093 /* Look up member function name in fieldlist. */
9094 for (i = 0; i < fip->nfnfields; i++)
9095 {
27bfe10e 9096 if (strcmp (fip->fnfieldlists[i].name, fieldname) == 0)
c906108c
SS
9097 break;
9098 }
9099
9100 /* Create new list element if necessary. */
9101 if (i < fip->nfnfields)
9102 flp = &fip->fnfieldlists[i];
9103 else
9104 {
9105 if ((fip->nfnfields % DW_FIELD_ALLOC_CHUNK) == 0)
9106 {
9107 fip->fnfieldlists = (struct fnfieldlist *)
9108 xrealloc (fip->fnfieldlists,
9109 (fip->nfnfields + DW_FIELD_ALLOC_CHUNK)
c5aa993b 9110 * sizeof (struct fnfieldlist));
c906108c 9111 if (fip->nfnfields == 0)
c13c43fd 9112 make_cleanup (free_current_contents, &fip->fnfieldlists);
c906108c
SS
9113 }
9114 flp = &fip->fnfieldlists[fip->nfnfields];
9115 flp->name = fieldname;
9116 flp->length = 0;
9117 flp->head = NULL;
3da10d80 9118 i = fip->nfnfields++;
c906108c
SS
9119 }
9120
9121 /* Create a new member function field and chain it to the field list
0963b4bd 9122 entry. */
c906108c 9123 new_fnfield = (struct nextfnfield *) xmalloc (sizeof (struct nextfnfield));
b8c9b27d 9124 make_cleanup (xfree, new_fnfield);
c906108c
SS
9125 memset (new_fnfield, 0, sizeof (struct nextfnfield));
9126 new_fnfield->next = flp->head;
9127 flp->head = new_fnfield;
9128 flp->length++;
9129
9130 /* Fill in the member function field info. */
9131 fnp = &new_fnfield->fnfield;
3da10d80
KS
9132
9133 /* Delay processing of the physname until later. */
9134 if (cu->language == language_cplus || cu->language == language_java)
9135 {
9136 add_to_method_list (type, i, flp->length - 1, fieldname,
9137 die, cu);
9138 }
9139 else
9140 {
1d06ead6 9141 const char *physname = dwarf2_physname (fieldname, die, cu);
3da10d80
KS
9142 fnp->physname = physname ? physname : "";
9143 }
9144
c906108c 9145 fnp->type = alloc_type (objfile);
f792889a
DJ
9146 this_type = read_type_die (die, cu);
9147 if (this_type && TYPE_CODE (this_type) == TYPE_CODE_FUNC)
c906108c 9148 {
f792889a 9149 int nparams = TYPE_NFIELDS (this_type);
c906108c 9150
f792889a 9151 /* TYPE is the domain of this method, and THIS_TYPE is the type
e26fb1d7
DC
9152 of the method itself (TYPE_CODE_METHOD). */
9153 smash_to_method_type (fnp->type, type,
f792889a
DJ
9154 TYPE_TARGET_TYPE (this_type),
9155 TYPE_FIELDS (this_type),
9156 TYPE_NFIELDS (this_type),
9157 TYPE_VARARGS (this_type));
c906108c
SS
9158
9159 /* Handle static member functions.
c5aa993b 9160 Dwarf2 has no clean way to discern C++ static and non-static
0963b4bd
MS
9161 member functions. G++ helps GDB by marking the first
9162 parameter for non-static member functions (which is the this
9163 pointer) as artificial. We obtain this information from
9164 read_subroutine_type via TYPE_FIELD_ARTIFICIAL. */
f792889a 9165 if (nparams == 0 || TYPE_FIELD_ARTIFICIAL (this_type, 0) == 0)
c906108c
SS
9166 fnp->voffset = VOFFSET_STATIC;
9167 }
9168 else
e2e0b3e5 9169 complaint (&symfile_complaints, _("member function type missing for '%s'"),
3da10d80 9170 dwarf2_full_name (fieldname, die, cu));
c906108c
SS
9171
9172 /* Get fcontext from DW_AT_containing_type if present. */
e142c38c 9173 if (dwarf2_attr (die, DW_AT_containing_type, cu) != NULL)
e7c27a73 9174 fnp->fcontext = die_containing_type (die, cu);
c906108c 9175
3e43a32a
MS
9176 /* dwarf2 doesn't have stubbed physical names, so the setting of is_const and
9177 is_volatile is irrelevant, as it is needed by gdb_mangle_name only. */
c906108c
SS
9178
9179 /* Get accessibility. */
e142c38c 9180 attr = dwarf2_attr (die, DW_AT_accessibility, cu);
c906108c 9181 if (attr)
60d5a603
JK
9182 accessibility = DW_UNSND (attr);
9183 else
9184 accessibility = dwarf2_default_access_attribute (die, cu);
9185 switch (accessibility)
c906108c 9186 {
60d5a603
JK
9187 case DW_ACCESS_private:
9188 fnp->is_private = 1;
9189 break;
9190 case DW_ACCESS_protected:
9191 fnp->is_protected = 1;
9192 break;
c906108c
SS
9193 }
9194
b02dede2 9195 /* Check for artificial methods. */
e142c38c 9196 attr = dwarf2_attr (die, DW_AT_artificial, cu);
b02dede2
DJ
9197 if (attr && DW_UNSND (attr) != 0)
9198 fnp->is_artificial = 1;
9199
0d564a31 9200 /* Get index in virtual function table if it is a virtual member
aec5aa8b
TT
9201 function. For older versions of GCC, this is an offset in the
9202 appropriate virtual table, as specified by DW_AT_containing_type.
9203 For everyone else, it is an expression to be evaluated relative
0d564a31
DJ
9204 to the object address. */
9205
e142c38c 9206 attr = dwarf2_attr (die, DW_AT_vtable_elem_location, cu);
aec5aa8b 9207 if (attr)
8e19ed76 9208 {
aec5aa8b 9209 if (attr_form_is_block (attr) && DW_BLOCK (attr)->size > 0)
8e19ed76 9210 {
aec5aa8b
TT
9211 if (DW_BLOCK (attr)->data[0] == DW_OP_constu)
9212 {
9213 /* Old-style GCC. */
9214 fnp->voffset = decode_locdesc (DW_BLOCK (attr), cu) + 2;
9215 }
9216 else if (DW_BLOCK (attr)->data[0] == DW_OP_deref
9217 || (DW_BLOCK (attr)->size > 1
9218 && DW_BLOCK (attr)->data[0] == DW_OP_deref_size
9219 && DW_BLOCK (attr)->data[1] == cu->header.addr_size))
9220 {
9221 struct dwarf_block blk;
9222 int offset;
9223
9224 offset = (DW_BLOCK (attr)->data[0] == DW_OP_deref
9225 ? 1 : 2);
9226 blk.size = DW_BLOCK (attr)->size - offset;
9227 blk.data = DW_BLOCK (attr)->data + offset;
9228 fnp->voffset = decode_locdesc (DW_BLOCK (attr), cu);
9229 if ((fnp->voffset % cu->header.addr_size) != 0)
9230 dwarf2_complex_location_expr_complaint ();
9231 else
9232 fnp->voffset /= cu->header.addr_size;
9233 fnp->voffset += 2;
9234 }
9235 else
9236 dwarf2_complex_location_expr_complaint ();
9237
9238 if (!fnp->fcontext)
9239 fnp->fcontext = TYPE_TARGET_TYPE (TYPE_FIELD_TYPE (this_type, 0));
9240 }
3690dd37 9241 else if (attr_form_is_section_offset (attr))
8e19ed76 9242 {
4d3c2250 9243 dwarf2_complex_location_expr_complaint ();
8e19ed76
PS
9244 }
9245 else
9246 {
4d3c2250
KB
9247 dwarf2_invalid_attrib_class_complaint ("DW_AT_vtable_elem_location",
9248 fieldname);
8e19ed76 9249 }
0d564a31 9250 }
d48cc9dd
DJ
9251 else
9252 {
9253 attr = dwarf2_attr (die, DW_AT_virtuality, cu);
9254 if (attr && DW_UNSND (attr))
9255 {
9256 /* GCC does this, as of 2008-08-25; PR debug/37237. */
9257 complaint (&symfile_complaints,
3e43a32a
MS
9258 _("Member function \"%s\" (offset %d) is virtual "
9259 "but the vtable offset is not specified"),
b64f50a1 9260 fieldname, die->offset.sect_off);
9655fd1a 9261 ALLOCATE_CPLUS_STRUCT_TYPE (type);
d48cc9dd
DJ
9262 TYPE_CPLUS_DYNAMIC (type) = 1;
9263 }
9264 }
c906108c
SS
9265}
9266
9267/* Create the vector of member function fields, and attach it to the type. */
9268
9269static void
fba45db2 9270dwarf2_attach_fn_fields_to_type (struct field_info *fip, struct type *type,
e7c27a73 9271 struct dwarf2_cu *cu)
c906108c
SS
9272{
9273 struct fnfieldlist *flp;
c906108c
SS
9274 int i;
9275
b4ba55a1 9276 if (cu->language == language_ada)
a73c6dcd 9277 error (_("unexpected member functions in Ada type"));
b4ba55a1 9278
c906108c
SS
9279 ALLOCATE_CPLUS_STRUCT_TYPE (type);
9280 TYPE_FN_FIELDLISTS (type) = (struct fn_fieldlist *)
9281 TYPE_ALLOC (type, sizeof (struct fn_fieldlist) * fip->nfnfields);
9282
9283 for (i = 0, flp = fip->fnfieldlists; i < fip->nfnfields; i++, flp++)
9284 {
9285 struct nextfnfield *nfp = flp->head;
9286 struct fn_fieldlist *fn_flp = &TYPE_FN_FIELDLIST (type, i);
9287 int k;
9288
9289 TYPE_FN_FIELDLIST_NAME (type, i) = flp->name;
9290 TYPE_FN_FIELDLIST_LENGTH (type, i) = flp->length;
9291 fn_flp->fn_fields = (struct fn_field *)
9292 TYPE_ALLOC (type, sizeof (struct fn_field) * flp->length);
9293 for (k = flp->length; (k--, nfp); nfp = nfp->next)
c5aa993b 9294 fn_flp->fn_fields[k] = nfp->fnfield;
c906108c
SS
9295 }
9296
9297 TYPE_NFN_FIELDS (type) = fip->nfnfields;
c906108c
SS
9298}
9299
1168df01
JB
9300/* Returns non-zero if NAME is the name of a vtable member in CU's
9301 language, zero otherwise. */
9302static int
9303is_vtable_name (const char *name, struct dwarf2_cu *cu)
9304{
9305 static const char vptr[] = "_vptr";
987504bb 9306 static const char vtable[] = "vtable";
1168df01 9307
987504bb
JJ
9308 /* Look for the C++ and Java forms of the vtable. */
9309 if ((cu->language == language_java
9310 && strncmp (name, vtable, sizeof (vtable) - 1) == 0)
9311 || (strncmp (name, vptr, sizeof (vptr) - 1) == 0
9312 && is_cplus_marker (name[sizeof (vptr) - 1])))
1168df01
JB
9313 return 1;
9314
9315 return 0;
9316}
9317
c0dd20ea 9318/* GCC outputs unnamed structures that are really pointers to member
0b92b5bb
TT
9319 functions, with the ABI-specified layout. If TYPE describes
9320 such a structure, smash it into a member function type.
61049d3b
DJ
9321
9322 GCC shouldn't do this; it should just output pointer to member DIEs.
9323 This is GCC PR debug/28767. */
c0dd20ea 9324
0b92b5bb
TT
9325static void
9326quirk_gcc_member_function_pointer (struct type *type, struct objfile *objfile)
c0dd20ea 9327{
0b92b5bb 9328 struct type *pfn_type, *domain_type, *new_type;
c0dd20ea
DJ
9329
9330 /* Check for a structure with no name and two children. */
0b92b5bb
TT
9331 if (TYPE_CODE (type) != TYPE_CODE_STRUCT || TYPE_NFIELDS (type) != 2)
9332 return;
c0dd20ea
DJ
9333
9334 /* Check for __pfn and __delta members. */
0b92b5bb
TT
9335 if (TYPE_FIELD_NAME (type, 0) == NULL
9336 || strcmp (TYPE_FIELD_NAME (type, 0), "__pfn") != 0
9337 || TYPE_FIELD_NAME (type, 1) == NULL
9338 || strcmp (TYPE_FIELD_NAME (type, 1), "__delta") != 0)
9339 return;
c0dd20ea
DJ
9340
9341 /* Find the type of the method. */
0b92b5bb 9342 pfn_type = TYPE_FIELD_TYPE (type, 0);
c0dd20ea
DJ
9343 if (pfn_type == NULL
9344 || TYPE_CODE (pfn_type) != TYPE_CODE_PTR
9345 || TYPE_CODE (TYPE_TARGET_TYPE (pfn_type)) != TYPE_CODE_FUNC)
0b92b5bb 9346 return;
c0dd20ea
DJ
9347
9348 /* Look for the "this" argument. */
9349 pfn_type = TYPE_TARGET_TYPE (pfn_type);
9350 if (TYPE_NFIELDS (pfn_type) == 0
0b92b5bb 9351 /* || TYPE_FIELD_TYPE (pfn_type, 0) == NULL */
c0dd20ea 9352 || TYPE_CODE (TYPE_FIELD_TYPE (pfn_type, 0)) != TYPE_CODE_PTR)
0b92b5bb 9353 return;
c0dd20ea
DJ
9354
9355 domain_type = TYPE_TARGET_TYPE (TYPE_FIELD_TYPE (pfn_type, 0));
0b92b5bb
TT
9356 new_type = alloc_type (objfile);
9357 smash_to_method_type (new_type, domain_type, TYPE_TARGET_TYPE (pfn_type),
c0dd20ea
DJ
9358 TYPE_FIELDS (pfn_type), TYPE_NFIELDS (pfn_type),
9359 TYPE_VARARGS (pfn_type));
0b92b5bb 9360 smash_to_methodptr_type (type, new_type);
c0dd20ea 9361}
1168df01 9362
685b1105
JK
9363/* Return non-zero if the CU's PRODUCER string matches the Intel C/C++ compiler
9364 (icc). */
9365
9366static int
9367producer_is_icc (struct dwarf2_cu *cu)
9368{
9369 if (!cu->checked_producer)
9370 check_producer (cu);
9371
9372 return cu->producer_is_icc;
9373}
9374
c906108c 9375/* Called when we find the DIE that starts a structure or union scope
c767944b
DJ
9376 (definition) to create a type for the structure or union. Fill in
9377 the type's name and general properties; the members will not be
9378 processed until process_structure_type.
c906108c 9379
c767944b
DJ
9380 NOTE: we need to call these functions regardless of whether or not the
9381 DIE has a DW_AT_name attribute, since it might be an anonymous
c906108c
SS
9382 structure or union. This gets the type entered into our set of
9383 user defined types.
9384
9385 However, if the structure is incomplete (an opaque struct/union)
9386 then suppress creating a symbol table entry for it since gdb only
9387 wants to find the one with the complete definition. Note that if
9388 it is complete, we just call new_symbol, which does it's own
9389 checking about whether the struct/union is anonymous or not (and
9390 suppresses creating a symbol table entry itself). */
9391
f792889a 9392static struct type *
134d01f1 9393read_structure_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 9394{
e7c27a73 9395 struct objfile *objfile = cu->objfile;
c906108c
SS
9396 struct type *type;
9397 struct attribute *attr;
39cbfefa 9398 char *name;
c906108c 9399
348e048f
DE
9400 /* If the definition of this type lives in .debug_types, read that type.
9401 Don't follow DW_AT_specification though, that will take us back up
9402 the chain and we want to go down. */
45e58e77 9403 attr = dwarf2_attr_no_follow (die, DW_AT_signature);
348e048f
DE
9404 if (attr)
9405 {
9406 struct dwarf2_cu *type_cu = cu;
9407 struct die_info *type_die = follow_die_ref_or_sig (die, attr, &type_cu);
9a619af0 9408
348e048f
DE
9409 /* We could just recurse on read_structure_type, but we need to call
9410 get_die_type to ensure only one type for this DIE is created.
9411 This is important, for example, because for c++ classes we need
9412 TYPE_NAME set which is only done by new_symbol. Blech. */
9413 type = read_type_die (type_die, type_cu);
9dc481d3
DE
9414
9415 /* TYPE_CU may not be the same as CU.
9416 Ensure TYPE is recorded in CU's type_hash table. */
348e048f
DE
9417 return set_die_type (die, type, cu);
9418 }
9419
c0dd20ea 9420 type = alloc_type (objfile);
c906108c 9421 INIT_CPLUS_SPECIFIC (type);
93311388 9422
39cbfefa
DJ
9423 name = dwarf2_name (die, cu);
9424 if (name != NULL)
c906108c 9425 {
987504bb
JJ
9426 if (cu->language == language_cplus
9427 || cu->language == language_java)
63d06c5c 9428 {
3da10d80
KS
9429 char *full_name = (char *) dwarf2_full_name (name, die, cu);
9430
9431 /* dwarf2_full_name might have already finished building the DIE's
9432 type. If so, there is no need to continue. */
9433 if (get_die_type (die, cu) != NULL)
9434 return get_die_type (die, cu);
9435
9436 TYPE_TAG_NAME (type) = full_name;
94af9270
KS
9437 if (die->tag == DW_TAG_structure_type
9438 || die->tag == DW_TAG_class_type)
9439 TYPE_NAME (type) = TYPE_TAG_NAME (type);
63d06c5c
DC
9440 }
9441 else
9442 {
d8151005
DJ
9443 /* The name is already allocated along with this objfile, so
9444 we don't need to duplicate it for the type. */
94af9270
KS
9445 TYPE_TAG_NAME (type) = (char *) name;
9446 if (die->tag == DW_TAG_class_type)
9447 TYPE_NAME (type) = TYPE_TAG_NAME (type);
63d06c5c 9448 }
c906108c
SS
9449 }
9450
9451 if (die->tag == DW_TAG_structure_type)
9452 {
9453 TYPE_CODE (type) = TYPE_CODE_STRUCT;
9454 }
9455 else if (die->tag == DW_TAG_union_type)
9456 {
9457 TYPE_CODE (type) = TYPE_CODE_UNION;
9458 }
9459 else
9460 {
c906108c
SS
9461 TYPE_CODE (type) = TYPE_CODE_CLASS;
9462 }
9463
0cc2414c
TT
9464 if (cu->language == language_cplus && die->tag == DW_TAG_class_type)
9465 TYPE_DECLARED_CLASS (type) = 1;
9466
e142c38c 9467 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
c906108c
SS
9468 if (attr)
9469 {
9470 TYPE_LENGTH (type) = DW_UNSND (attr);
9471 }
9472 else
9473 {
9474 TYPE_LENGTH (type) = 0;
9475 }
9476
685b1105
JK
9477 if (producer_is_icc (cu))
9478 {
9479 /* ICC does not output the required DW_AT_declaration
9480 on incomplete types, but gives them a size of zero. */
9481 }
9482 else
9483 TYPE_STUB_SUPPORTED (type) = 1;
9484
dc718098 9485 if (die_is_declaration (die, cu))
876cecd0 9486 TYPE_STUB (type) = 1;
a6c727b2
DJ
9487 else if (attr == NULL && die->child == NULL
9488 && producer_is_realview (cu->producer))
9489 /* RealView does not output the required DW_AT_declaration
9490 on incomplete types. */
9491 TYPE_STUB (type) = 1;
dc718098 9492
c906108c
SS
9493 /* We need to add the type field to the die immediately so we don't
9494 infinitely recurse when dealing with pointers to the structure
0963b4bd 9495 type within the structure itself. */
1c379e20 9496 set_die_type (die, type, cu);
c906108c 9497
7e314c57
JK
9498 /* set_die_type should be already done. */
9499 set_descriptive_type (type, die, cu);
9500
c767944b
DJ
9501 return type;
9502}
9503
9504/* Finish creating a structure or union type, including filling in
9505 its members and creating a symbol for it. */
9506
9507static void
9508process_structure_scope (struct die_info *die, struct dwarf2_cu *cu)
9509{
9510 struct objfile *objfile = cu->objfile;
9511 struct die_info *child_die = die->child;
9512 struct type *type;
9513
9514 type = get_die_type (die, cu);
9515 if (type == NULL)
9516 type = read_structure_type (die, cu);
9517
e142c38c 9518 if (die->child != NULL && ! die_is_declaration (die, cu))
c906108c
SS
9519 {
9520 struct field_info fi;
9521 struct die_info *child_die;
34eaf542 9522 VEC (symbolp) *template_args = NULL;
c767944b 9523 struct cleanup *back_to = make_cleanup (null_cleanup, 0);
c906108c
SS
9524
9525 memset (&fi, 0, sizeof (struct field_info));
9526
639d11d3 9527 child_die = die->child;
c906108c
SS
9528
9529 while (child_die && child_die->tag)
9530 {
a9a9bd0f
DC
9531 if (child_die->tag == DW_TAG_member
9532 || child_die->tag == DW_TAG_variable)
c906108c 9533 {
a9a9bd0f
DC
9534 /* NOTE: carlton/2002-11-05: A C++ static data member
9535 should be a DW_TAG_member that is a declaration, but
9536 all versions of G++ as of this writing (so through at
9537 least 3.2.1) incorrectly generate DW_TAG_variable
9538 tags for them instead. */
e7c27a73 9539 dwarf2_add_field (&fi, child_die, cu);
c906108c 9540 }
8713b1b1 9541 else if (child_die->tag == DW_TAG_subprogram)
c906108c 9542 {
0963b4bd 9543 /* C++ member function. */
e7c27a73 9544 dwarf2_add_member_fn (&fi, child_die, type, cu);
c906108c
SS
9545 }
9546 else if (child_die->tag == DW_TAG_inheritance)
9547 {
9548 /* C++ base class field. */
e7c27a73 9549 dwarf2_add_field (&fi, child_die, cu);
c906108c 9550 }
98751a41
JK
9551 else if (child_die->tag == DW_TAG_typedef)
9552 dwarf2_add_typedef (&fi, child_die, cu);
34eaf542
TT
9553 else if (child_die->tag == DW_TAG_template_type_param
9554 || child_die->tag == DW_TAG_template_value_param)
9555 {
9556 struct symbol *arg = new_symbol (child_die, NULL, cu);
9557
f1078f66
DJ
9558 if (arg != NULL)
9559 VEC_safe_push (symbolp, template_args, arg);
34eaf542
TT
9560 }
9561
c906108c
SS
9562 child_die = sibling_die (child_die);
9563 }
9564
34eaf542
TT
9565 /* Attach template arguments to type. */
9566 if (! VEC_empty (symbolp, template_args))
9567 {
9568 ALLOCATE_CPLUS_STRUCT_TYPE (type);
9569 TYPE_N_TEMPLATE_ARGUMENTS (type)
9570 = VEC_length (symbolp, template_args);
9571 TYPE_TEMPLATE_ARGUMENTS (type)
9572 = obstack_alloc (&objfile->objfile_obstack,
9573 (TYPE_N_TEMPLATE_ARGUMENTS (type)
9574 * sizeof (struct symbol *)));
9575 memcpy (TYPE_TEMPLATE_ARGUMENTS (type),
9576 VEC_address (symbolp, template_args),
9577 (TYPE_N_TEMPLATE_ARGUMENTS (type)
9578 * sizeof (struct symbol *)));
9579 VEC_free (symbolp, template_args);
9580 }
9581
c906108c
SS
9582 /* Attach fields and member functions to the type. */
9583 if (fi.nfields)
e7c27a73 9584 dwarf2_attach_fields_to_type (&fi, type, cu);
c906108c
SS
9585 if (fi.nfnfields)
9586 {
e7c27a73 9587 dwarf2_attach_fn_fields_to_type (&fi, type, cu);
c906108c 9588
c5aa993b 9589 /* Get the type which refers to the base class (possibly this
c906108c 9590 class itself) which contains the vtable pointer for the current
0d564a31
DJ
9591 class from the DW_AT_containing_type attribute. This use of
9592 DW_AT_containing_type is a GNU extension. */
c906108c 9593
e142c38c 9594 if (dwarf2_attr (die, DW_AT_containing_type, cu) != NULL)
c906108c 9595 {
e7c27a73 9596 struct type *t = die_containing_type (die, cu);
c906108c
SS
9597
9598 TYPE_VPTR_BASETYPE (type) = t;
9599 if (type == t)
9600 {
c906108c
SS
9601 int i;
9602
9603 /* Our own class provides vtbl ptr. */
9604 for (i = TYPE_NFIELDS (t) - 1;
9605 i >= TYPE_N_BASECLASSES (t);
9606 --i)
9607 {
0d5cff50 9608 const char *fieldname = TYPE_FIELD_NAME (t, i);
c906108c 9609
1168df01 9610 if (is_vtable_name (fieldname, cu))
c906108c
SS
9611 {
9612 TYPE_VPTR_FIELDNO (type) = i;
9613 break;
9614 }
9615 }
9616
9617 /* Complain if virtual function table field not found. */
9618 if (i < TYPE_N_BASECLASSES (t))
4d3c2250 9619 complaint (&symfile_complaints,
3e43a32a
MS
9620 _("virtual function table pointer "
9621 "not found when defining class '%s'"),
4d3c2250
KB
9622 TYPE_TAG_NAME (type) ? TYPE_TAG_NAME (type) :
9623 "");
c906108c
SS
9624 }
9625 else
9626 {
9627 TYPE_VPTR_FIELDNO (type) = TYPE_VPTR_FIELDNO (t);
9628 }
9629 }
f6235d4c
EZ
9630 else if (cu->producer
9631 && strncmp (cu->producer,
9632 "IBM(R) XL C/C++ Advanced Edition", 32) == 0)
9633 {
9634 /* The IBM XLC compiler does not provide direct indication
9635 of the containing type, but the vtable pointer is
9636 always named __vfp. */
9637
9638 int i;
9639
9640 for (i = TYPE_NFIELDS (type) - 1;
9641 i >= TYPE_N_BASECLASSES (type);
9642 --i)
9643 {
9644 if (strcmp (TYPE_FIELD_NAME (type, i), "__vfp") == 0)
9645 {
9646 TYPE_VPTR_FIELDNO (type) = i;
9647 TYPE_VPTR_BASETYPE (type) = type;
9648 break;
9649 }
9650 }
9651 }
c906108c 9652 }
98751a41
JK
9653
9654 /* Copy fi.typedef_field_list linked list elements content into the
9655 allocated array TYPE_TYPEDEF_FIELD_ARRAY (type). */
9656 if (fi.typedef_field_list)
9657 {
9658 int i = fi.typedef_field_list_count;
9659
a0d7a4ff 9660 ALLOCATE_CPLUS_STRUCT_TYPE (type);
98751a41
JK
9661 TYPE_TYPEDEF_FIELD_ARRAY (type)
9662 = TYPE_ALLOC (type, sizeof (TYPE_TYPEDEF_FIELD (type, 0)) * i);
9663 TYPE_TYPEDEF_FIELD_COUNT (type) = i;
9664
9665 /* Reverse the list order to keep the debug info elements order. */
9666 while (--i >= 0)
9667 {
9668 struct typedef_field *dest, *src;
6e70227d 9669
98751a41
JK
9670 dest = &TYPE_TYPEDEF_FIELD (type, i);
9671 src = &fi.typedef_field_list->field;
9672 fi.typedef_field_list = fi.typedef_field_list->next;
9673 *dest = *src;
9674 }
9675 }
c767944b
DJ
9676
9677 do_cleanups (back_to);
eb2a6f42
TT
9678
9679 if (HAVE_CPLUS_STRUCT (type))
9680 TYPE_CPLUS_REALLY_JAVA (type) = cu->language == language_java;
c906108c 9681 }
63d06c5c 9682
bb5ed363 9683 quirk_gcc_member_function_pointer (type, objfile);
0b92b5bb 9684
90aeadfc
DC
9685 /* NOTE: carlton/2004-03-16: GCC 3.4 (or at least one of its
9686 snapshots) has been known to create a die giving a declaration
9687 for a class that has, as a child, a die giving a definition for a
9688 nested class. So we have to process our children even if the
9689 current die is a declaration. Normally, of course, a declaration
9690 won't have any children at all. */
134d01f1 9691
90aeadfc
DC
9692 while (child_die != NULL && child_die->tag)
9693 {
9694 if (child_die->tag == DW_TAG_member
9695 || child_die->tag == DW_TAG_variable
34eaf542
TT
9696 || child_die->tag == DW_TAG_inheritance
9697 || child_die->tag == DW_TAG_template_value_param
9698 || child_die->tag == DW_TAG_template_type_param)
134d01f1 9699 {
90aeadfc 9700 /* Do nothing. */
134d01f1 9701 }
90aeadfc
DC
9702 else
9703 process_die (child_die, cu);
134d01f1 9704
90aeadfc 9705 child_die = sibling_die (child_die);
134d01f1
DJ
9706 }
9707
fa4028e9
JB
9708 /* Do not consider external references. According to the DWARF standard,
9709 these DIEs are identified by the fact that they have no byte_size
9710 attribute, and a declaration attribute. */
9711 if (dwarf2_attr (die, DW_AT_byte_size, cu) != NULL
9712 || !die_is_declaration (die, cu))
c767944b 9713 new_symbol (die, type, cu);
134d01f1
DJ
9714}
9715
9716/* Given a DW_AT_enumeration_type die, set its type. We do not
9717 complete the type's fields yet, or create any symbols. */
c906108c 9718
f792889a 9719static struct type *
134d01f1 9720read_enumeration_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 9721{
e7c27a73 9722 struct objfile *objfile = cu->objfile;
c906108c 9723 struct type *type;
c906108c 9724 struct attribute *attr;
0114d602 9725 const char *name;
134d01f1 9726
348e048f
DE
9727 /* If the definition of this type lives in .debug_types, read that type.
9728 Don't follow DW_AT_specification though, that will take us back up
9729 the chain and we want to go down. */
45e58e77 9730 attr = dwarf2_attr_no_follow (die, DW_AT_signature);
348e048f
DE
9731 if (attr)
9732 {
9733 struct dwarf2_cu *type_cu = cu;
9734 struct die_info *type_die = follow_die_ref_or_sig (die, attr, &type_cu);
9a619af0 9735
348e048f 9736 type = read_type_die (type_die, type_cu);
9dc481d3
DE
9737
9738 /* TYPE_CU may not be the same as CU.
9739 Ensure TYPE is recorded in CU's type_hash table. */
348e048f
DE
9740 return set_die_type (die, type, cu);
9741 }
9742
c906108c
SS
9743 type = alloc_type (objfile);
9744
9745 TYPE_CODE (type) = TYPE_CODE_ENUM;
94af9270 9746 name = dwarf2_full_name (NULL, die, cu);
39cbfefa 9747 if (name != NULL)
0114d602 9748 TYPE_TAG_NAME (type) = (char *) name;
c906108c 9749
e142c38c 9750 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
c906108c
SS
9751 if (attr)
9752 {
9753 TYPE_LENGTH (type) = DW_UNSND (attr);
9754 }
9755 else
9756 {
9757 TYPE_LENGTH (type) = 0;
9758 }
9759
137033e9
JB
9760 /* The enumeration DIE can be incomplete. In Ada, any type can be
9761 declared as private in the package spec, and then defined only
9762 inside the package body. Such types are known as Taft Amendment
9763 Types. When another package uses such a type, an incomplete DIE
9764 may be generated by the compiler. */
02eb380e 9765 if (die_is_declaration (die, cu))
876cecd0 9766 TYPE_STUB (type) = 1;
02eb380e 9767
f792889a 9768 return set_die_type (die, type, cu);
134d01f1
DJ
9769}
9770
9771/* Given a pointer to a die which begins an enumeration, process all
9772 the dies that define the members of the enumeration, and create the
9773 symbol for the enumeration type.
9774
9775 NOTE: We reverse the order of the element list. */
9776
9777static void
9778process_enumeration_scope (struct die_info *die, struct dwarf2_cu *cu)
9779{
f792889a 9780 struct type *this_type;
134d01f1 9781
f792889a
DJ
9782 this_type = get_die_type (die, cu);
9783 if (this_type == NULL)
9784 this_type = read_enumeration_type (die, cu);
9dc481d3 9785
639d11d3 9786 if (die->child != NULL)
c906108c 9787 {
9dc481d3
DE
9788 struct die_info *child_die;
9789 struct symbol *sym;
9790 struct field *fields = NULL;
9791 int num_fields = 0;
9792 int unsigned_enum = 1;
9793 char *name;
cafec441
TT
9794 int flag_enum = 1;
9795 ULONGEST mask = 0;
9dc481d3 9796
639d11d3 9797 child_die = die->child;
c906108c
SS
9798 while (child_die && child_die->tag)
9799 {
9800 if (child_die->tag != DW_TAG_enumerator)
9801 {
e7c27a73 9802 process_die (child_die, cu);
c906108c
SS
9803 }
9804 else
9805 {
39cbfefa
DJ
9806 name = dwarf2_name (child_die, cu);
9807 if (name)
c906108c 9808 {
f792889a 9809 sym = new_symbol (child_die, this_type, cu);
c906108c 9810 if (SYMBOL_VALUE (sym) < 0)
cafec441
TT
9811 {
9812 unsigned_enum = 0;
9813 flag_enum = 0;
9814 }
9815 else if ((mask & SYMBOL_VALUE (sym)) != 0)
9816 flag_enum = 0;
9817 else
9818 mask |= SYMBOL_VALUE (sym);
c906108c
SS
9819
9820 if ((num_fields % DW_FIELD_ALLOC_CHUNK) == 0)
9821 {
9822 fields = (struct field *)
9823 xrealloc (fields,
9824 (num_fields + DW_FIELD_ALLOC_CHUNK)
c5aa993b 9825 * sizeof (struct field));
c906108c
SS
9826 }
9827
3567439c 9828 FIELD_NAME (fields[num_fields]) = SYMBOL_LINKAGE_NAME (sym);
c906108c 9829 FIELD_TYPE (fields[num_fields]) = NULL;
14e75d8e 9830 SET_FIELD_ENUMVAL (fields[num_fields], SYMBOL_VALUE (sym));
c906108c
SS
9831 FIELD_BITSIZE (fields[num_fields]) = 0;
9832
9833 num_fields++;
9834 }
9835 }
9836
9837 child_die = sibling_die (child_die);
9838 }
9839
9840 if (num_fields)
9841 {
f792889a
DJ
9842 TYPE_NFIELDS (this_type) = num_fields;
9843 TYPE_FIELDS (this_type) = (struct field *)
9844 TYPE_ALLOC (this_type, sizeof (struct field) * num_fields);
9845 memcpy (TYPE_FIELDS (this_type), fields,
c906108c 9846 sizeof (struct field) * num_fields);
b8c9b27d 9847 xfree (fields);
c906108c
SS
9848 }
9849 if (unsigned_enum)
876cecd0 9850 TYPE_UNSIGNED (this_type) = 1;
cafec441
TT
9851 if (flag_enum)
9852 TYPE_FLAG_ENUM (this_type) = 1;
c906108c 9853 }
134d01f1 9854
6c83ed52
TT
9855 /* If we are reading an enum from a .debug_types unit, and the enum
9856 is a declaration, and the enum is not the signatured type in the
9857 unit, then we do not want to add a symbol for it. Adding a
9858 symbol would in some cases obscure the true definition of the
9859 enum, giving users an incomplete type when the definition is
9860 actually available. Note that we do not want to do this for all
9861 enums which are just declarations, because C++0x allows forward
9862 enum declarations. */
3019eac3 9863 if (cu->per_cu->is_debug_types
6c83ed52
TT
9864 && die_is_declaration (die, cu))
9865 {
52dc124a 9866 struct signatured_type *sig_type;
6c83ed52 9867
52dc124a 9868 sig_type
6c83ed52 9869 = lookup_signatured_type_at_offset (dwarf2_per_objfile->objfile,
3019eac3 9870 cu->per_cu->info_or_types_section,
6c83ed52 9871 cu->per_cu->offset);
3019eac3
DE
9872 gdb_assert (sig_type->type_offset_in_section.sect_off != 0);
9873 if (sig_type->type_offset_in_section.sect_off != die->offset.sect_off)
6c83ed52
TT
9874 return;
9875 }
9876
f792889a 9877 new_symbol (die, this_type, cu);
c906108c
SS
9878}
9879
9880/* Extract all information from a DW_TAG_array_type DIE and put it in
9881 the DIE's type field. For now, this only handles one dimensional
9882 arrays. */
9883
f792889a 9884static struct type *
e7c27a73 9885read_array_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 9886{
e7c27a73 9887 struct objfile *objfile = cu->objfile;
c906108c 9888 struct die_info *child_die;
7e314c57 9889 struct type *type;
c906108c
SS
9890 struct type *element_type, *range_type, *index_type;
9891 struct type **range_types = NULL;
9892 struct attribute *attr;
9893 int ndim = 0;
9894 struct cleanup *back_to;
39cbfefa 9895 char *name;
c906108c 9896
e7c27a73 9897 element_type = die_type (die, cu);
c906108c 9898
7e314c57
JK
9899 /* The die_type call above may have already set the type for this DIE. */
9900 type = get_die_type (die, cu);
9901 if (type)
9902 return type;
9903
c906108c
SS
9904 /* Irix 6.2 native cc creates array types without children for
9905 arrays with unspecified length. */
639d11d3 9906 if (die->child == NULL)
c906108c 9907 {
46bf5051 9908 index_type = objfile_type (objfile)->builtin_int;
c906108c 9909 range_type = create_range_type (NULL, index_type, 0, -1);
f792889a
DJ
9910 type = create_array_type (NULL, element_type, range_type);
9911 return set_die_type (die, type, cu);
c906108c
SS
9912 }
9913
9914 back_to = make_cleanup (null_cleanup, NULL);
639d11d3 9915 child_die = die->child;
c906108c
SS
9916 while (child_die && child_die->tag)
9917 {
9918 if (child_die->tag == DW_TAG_subrange_type)
9919 {
f792889a 9920 struct type *child_type = read_type_die (child_die, cu);
9a619af0 9921
f792889a 9922 if (child_type != NULL)
a02abb62 9923 {
0963b4bd
MS
9924 /* The range type was succesfully read. Save it for the
9925 array type creation. */
a02abb62
JB
9926 if ((ndim % DW_FIELD_ALLOC_CHUNK) == 0)
9927 {
9928 range_types = (struct type **)
9929 xrealloc (range_types, (ndim + DW_FIELD_ALLOC_CHUNK)
9930 * sizeof (struct type *));
9931 if (ndim == 0)
9932 make_cleanup (free_current_contents, &range_types);
9933 }
f792889a 9934 range_types[ndim++] = child_type;
a02abb62 9935 }
c906108c
SS
9936 }
9937 child_die = sibling_die (child_die);
9938 }
9939
9940 /* Dwarf2 dimensions are output from left to right, create the
9941 necessary array types in backwards order. */
7ca2d3a3 9942
c906108c 9943 type = element_type;
7ca2d3a3
DL
9944
9945 if (read_array_order (die, cu) == DW_ORD_col_major)
9946 {
9947 int i = 0;
9a619af0 9948
7ca2d3a3
DL
9949 while (i < ndim)
9950 type = create_array_type (NULL, type, range_types[i++]);
9951 }
9952 else
9953 {
9954 while (ndim-- > 0)
9955 type = create_array_type (NULL, type, range_types[ndim]);
9956 }
c906108c 9957
f5f8a009
EZ
9958 /* Understand Dwarf2 support for vector types (like they occur on
9959 the PowerPC w/ AltiVec). Gcc just adds another attribute to the
9960 array type. This is not part of the Dwarf2/3 standard yet, but a
9961 custom vendor extension. The main difference between a regular
9962 array and the vector variant is that vectors are passed by value
9963 to functions. */
e142c38c 9964 attr = dwarf2_attr (die, DW_AT_GNU_vector, cu);
f5f8a009 9965 if (attr)
ea37ba09 9966 make_vector_type (type);
f5f8a009 9967
dbc98a8b
KW
9968 /* The DIE may have DW_AT_byte_size set. For example an OpenCL
9969 implementation may choose to implement triple vectors using this
9970 attribute. */
9971 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
9972 if (attr)
9973 {
9974 if (DW_UNSND (attr) >= TYPE_LENGTH (type))
9975 TYPE_LENGTH (type) = DW_UNSND (attr);
9976 else
3e43a32a
MS
9977 complaint (&symfile_complaints,
9978 _("DW_AT_byte_size for array type smaller "
9979 "than the total size of elements"));
dbc98a8b
KW
9980 }
9981
39cbfefa
DJ
9982 name = dwarf2_name (die, cu);
9983 if (name)
9984 TYPE_NAME (type) = name;
6e70227d 9985
0963b4bd 9986 /* Install the type in the die. */
7e314c57
JK
9987 set_die_type (die, type, cu);
9988
9989 /* set_die_type should be already done. */
b4ba55a1
JB
9990 set_descriptive_type (type, die, cu);
9991
c906108c
SS
9992 do_cleanups (back_to);
9993
7e314c57 9994 return type;
c906108c
SS
9995}
9996
7ca2d3a3 9997static enum dwarf_array_dim_ordering
6e70227d 9998read_array_order (struct die_info *die, struct dwarf2_cu *cu)
7ca2d3a3
DL
9999{
10000 struct attribute *attr;
10001
10002 attr = dwarf2_attr (die, DW_AT_ordering, cu);
10003
10004 if (attr) return DW_SND (attr);
10005
0963b4bd
MS
10006 /* GNU F77 is a special case, as at 08/2004 array type info is the
10007 opposite order to the dwarf2 specification, but data is still
10008 laid out as per normal fortran.
7ca2d3a3 10009
0963b4bd
MS
10010 FIXME: dsl/2004-8-20: If G77 is ever fixed, this will also need
10011 version checking. */
7ca2d3a3 10012
905e0470
PM
10013 if (cu->language == language_fortran
10014 && cu->producer && strstr (cu->producer, "GNU F77"))
7ca2d3a3
DL
10015 {
10016 return DW_ORD_row_major;
10017 }
10018
6e70227d 10019 switch (cu->language_defn->la_array_ordering)
7ca2d3a3
DL
10020 {
10021 case array_column_major:
10022 return DW_ORD_col_major;
10023 case array_row_major:
10024 default:
10025 return DW_ORD_row_major;
10026 };
10027}
10028
72019c9c 10029/* Extract all information from a DW_TAG_set_type DIE and put it in
0963b4bd 10030 the DIE's type field. */
72019c9c 10031
f792889a 10032static struct type *
72019c9c
GM
10033read_set_type (struct die_info *die, struct dwarf2_cu *cu)
10034{
7e314c57
JK
10035 struct type *domain_type, *set_type;
10036 struct attribute *attr;
f792889a 10037
7e314c57
JK
10038 domain_type = die_type (die, cu);
10039
10040 /* The die_type call above may have already set the type for this DIE. */
10041 set_type = get_die_type (die, cu);
10042 if (set_type)
10043 return set_type;
10044
10045 set_type = create_set_type (NULL, domain_type);
10046
10047 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
d09039dd
PM
10048 if (attr)
10049 TYPE_LENGTH (set_type) = DW_UNSND (attr);
7e314c57 10050
f792889a 10051 return set_die_type (die, set_type, cu);
72019c9c 10052}
7ca2d3a3 10053
c906108c
SS
10054/* First cut: install each common block member as a global variable. */
10055
10056static void
e7c27a73 10057read_common_block (struct die_info *die, struct dwarf2_cu *cu)
c906108c
SS
10058{
10059 struct die_info *child_die;
10060 struct attribute *attr;
10061 struct symbol *sym;
10062 CORE_ADDR base = (CORE_ADDR) 0;
10063
e142c38c 10064 attr = dwarf2_attr (die, DW_AT_location, cu);
c906108c
SS
10065 if (attr)
10066 {
0963b4bd 10067 /* Support the .debug_loc offsets. */
8e19ed76
PS
10068 if (attr_form_is_block (attr))
10069 {
e7c27a73 10070 base = decode_locdesc (DW_BLOCK (attr), cu);
8e19ed76 10071 }
3690dd37 10072 else if (attr_form_is_section_offset (attr))
8e19ed76 10073 {
4d3c2250 10074 dwarf2_complex_location_expr_complaint ();
8e19ed76
PS
10075 }
10076 else
10077 {
4d3c2250
KB
10078 dwarf2_invalid_attrib_class_complaint ("DW_AT_location",
10079 "common block member");
8e19ed76 10080 }
c906108c 10081 }
639d11d3 10082 if (die->child != NULL)
c906108c 10083 {
639d11d3 10084 child_die = die->child;
c906108c
SS
10085 while (child_die && child_die->tag)
10086 {
74ac6d43
TT
10087 LONGEST offset;
10088
e7c27a73 10089 sym = new_symbol (child_die, NULL, cu);
e8d28ef4
TT
10090 if (sym != NULL
10091 && handle_data_member_location (child_die, cu, &offset))
c906108c 10092 {
74ac6d43 10093 SYMBOL_VALUE_ADDRESS (sym) = base + offset;
c906108c
SS
10094 add_symbol_to_list (sym, &global_symbols);
10095 }
10096 child_die = sibling_die (child_die);
10097 }
10098 }
10099}
10100
0114d602 10101/* Create a type for a C++ namespace. */
d9fa45fe 10102
0114d602
DJ
10103static struct type *
10104read_namespace_type (struct die_info *die, struct dwarf2_cu *cu)
d9fa45fe 10105{
e7c27a73 10106 struct objfile *objfile = cu->objfile;
0114d602 10107 const char *previous_prefix, *name;
9219021c 10108 int is_anonymous;
0114d602
DJ
10109 struct type *type;
10110
10111 /* For extensions, reuse the type of the original namespace. */
10112 if (dwarf2_attr (die, DW_AT_extension, cu) != NULL)
10113 {
10114 struct die_info *ext_die;
10115 struct dwarf2_cu *ext_cu = cu;
9a619af0 10116
0114d602
DJ
10117 ext_die = dwarf2_extension (die, &ext_cu);
10118 type = read_type_die (ext_die, ext_cu);
9dc481d3
DE
10119
10120 /* EXT_CU may not be the same as CU.
10121 Ensure TYPE is recorded in CU's type_hash table. */
0114d602
DJ
10122 return set_die_type (die, type, cu);
10123 }
9219021c 10124
e142c38c 10125 name = namespace_name (die, &is_anonymous, cu);
9219021c
DC
10126
10127 /* Now build the name of the current namespace. */
10128
0114d602
DJ
10129 previous_prefix = determine_prefix (die, cu);
10130 if (previous_prefix[0] != '\0')
10131 name = typename_concat (&objfile->objfile_obstack,
f55ee35c 10132 previous_prefix, name, 0, cu);
0114d602
DJ
10133
10134 /* Create the type. */
10135 type = init_type (TYPE_CODE_NAMESPACE, 0, 0, NULL,
10136 objfile);
10137 TYPE_NAME (type) = (char *) name;
10138 TYPE_TAG_NAME (type) = TYPE_NAME (type);
10139
60531b24 10140 return set_die_type (die, type, cu);
0114d602
DJ
10141}
10142
10143/* Read a C++ namespace. */
10144
10145static void
10146read_namespace (struct die_info *die, struct dwarf2_cu *cu)
10147{
10148 struct objfile *objfile = cu->objfile;
0114d602 10149 int is_anonymous;
9219021c 10150
5c4e30ca
DC
10151 /* Add a symbol associated to this if we haven't seen the namespace
10152 before. Also, add a using directive if it's an anonymous
10153 namespace. */
9219021c 10154
f2f0e013 10155 if (dwarf2_attr (die, DW_AT_extension, cu) == NULL)
5c4e30ca
DC
10156 {
10157 struct type *type;
10158
0114d602 10159 type = read_type_die (die, cu);
e7c27a73 10160 new_symbol (die, type, cu);
5c4e30ca 10161
e8e80198 10162 namespace_name (die, &is_anonymous, cu);
5c4e30ca 10163 if (is_anonymous)
0114d602
DJ
10164 {
10165 const char *previous_prefix = determine_prefix (die, cu);
9a619af0 10166
c0cc3a76 10167 cp_add_using_directive (previous_prefix, TYPE_NAME (type), NULL,
32019081 10168 NULL, NULL, &objfile->objfile_obstack);
0114d602 10169 }
5c4e30ca 10170 }
9219021c 10171
639d11d3 10172 if (die->child != NULL)
d9fa45fe 10173 {
639d11d3 10174 struct die_info *child_die = die->child;
6e70227d 10175
d9fa45fe
DC
10176 while (child_die && child_die->tag)
10177 {
e7c27a73 10178 process_die (child_die, cu);
d9fa45fe
DC
10179 child_die = sibling_die (child_die);
10180 }
10181 }
38d518c9
EZ
10182}
10183
f55ee35c
JK
10184/* Read a Fortran module as type. This DIE can be only a declaration used for
10185 imported module. Still we need that type as local Fortran "use ... only"
10186 declaration imports depend on the created type in determine_prefix. */
10187
10188static struct type *
10189read_module_type (struct die_info *die, struct dwarf2_cu *cu)
10190{
10191 struct objfile *objfile = cu->objfile;
10192 char *module_name;
10193 struct type *type;
10194
10195 module_name = dwarf2_name (die, cu);
10196 if (!module_name)
3e43a32a
MS
10197 complaint (&symfile_complaints,
10198 _("DW_TAG_module has no name, offset 0x%x"),
b64f50a1 10199 die->offset.sect_off);
f55ee35c
JK
10200 type = init_type (TYPE_CODE_MODULE, 0, 0, module_name, objfile);
10201
10202 /* determine_prefix uses TYPE_TAG_NAME. */
10203 TYPE_TAG_NAME (type) = TYPE_NAME (type);
10204
10205 return set_die_type (die, type, cu);
10206}
10207
5d7cb8df
JK
10208/* Read a Fortran module. */
10209
10210static void
10211read_module (struct die_info *die, struct dwarf2_cu *cu)
10212{
10213 struct die_info *child_die = die->child;
10214
5d7cb8df
JK
10215 while (child_die && child_die->tag)
10216 {
10217 process_die (child_die, cu);
10218 child_die = sibling_die (child_die);
10219 }
10220}
10221
38d518c9
EZ
10222/* Return the name of the namespace represented by DIE. Set
10223 *IS_ANONYMOUS to tell whether or not the namespace is an anonymous
10224 namespace. */
10225
10226static const char *
e142c38c 10227namespace_name (struct die_info *die, int *is_anonymous, struct dwarf2_cu *cu)
38d518c9
EZ
10228{
10229 struct die_info *current_die;
10230 const char *name = NULL;
10231
10232 /* Loop through the extensions until we find a name. */
10233
10234 for (current_die = die;
10235 current_die != NULL;
f2f0e013 10236 current_die = dwarf2_extension (die, &cu))
38d518c9 10237 {
e142c38c 10238 name = dwarf2_name (current_die, cu);
38d518c9
EZ
10239 if (name != NULL)
10240 break;
10241 }
10242
10243 /* Is it an anonymous namespace? */
10244
10245 *is_anonymous = (name == NULL);
10246 if (*is_anonymous)
2b1dbab0 10247 name = CP_ANONYMOUS_NAMESPACE_STR;
38d518c9
EZ
10248
10249 return name;
d9fa45fe
DC
10250}
10251
c906108c
SS
10252/* Extract all information from a DW_TAG_pointer_type DIE and add to
10253 the user defined type vector. */
10254
f792889a 10255static struct type *
e7c27a73 10256read_tag_pointer_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 10257{
5e2b427d 10258 struct gdbarch *gdbarch = get_objfile_arch (cu->objfile);
e7c27a73 10259 struct comp_unit_head *cu_header = &cu->header;
c906108c 10260 struct type *type;
8b2dbe47
KB
10261 struct attribute *attr_byte_size;
10262 struct attribute *attr_address_class;
10263 int byte_size, addr_class;
7e314c57
JK
10264 struct type *target_type;
10265
10266 target_type = die_type (die, cu);
c906108c 10267
7e314c57
JK
10268 /* The die_type call above may have already set the type for this DIE. */
10269 type = get_die_type (die, cu);
10270 if (type)
10271 return type;
10272
10273 type = lookup_pointer_type (target_type);
8b2dbe47 10274
e142c38c 10275 attr_byte_size = dwarf2_attr (die, DW_AT_byte_size, cu);
8b2dbe47
KB
10276 if (attr_byte_size)
10277 byte_size = DW_UNSND (attr_byte_size);
c906108c 10278 else
8b2dbe47
KB
10279 byte_size = cu_header->addr_size;
10280
e142c38c 10281 attr_address_class = dwarf2_attr (die, DW_AT_address_class, cu);
8b2dbe47
KB
10282 if (attr_address_class)
10283 addr_class = DW_UNSND (attr_address_class);
10284 else
10285 addr_class = DW_ADDR_none;
10286
10287 /* If the pointer size or address class is different than the
10288 default, create a type variant marked as such and set the
10289 length accordingly. */
10290 if (TYPE_LENGTH (type) != byte_size || addr_class != DW_ADDR_none)
c906108c 10291 {
5e2b427d 10292 if (gdbarch_address_class_type_flags_p (gdbarch))
8b2dbe47
KB
10293 {
10294 int type_flags;
10295
849957d9 10296 type_flags = gdbarch_address_class_type_flags
5e2b427d 10297 (gdbarch, byte_size, addr_class);
876cecd0
TT
10298 gdb_assert ((type_flags & ~TYPE_INSTANCE_FLAG_ADDRESS_CLASS_ALL)
10299 == 0);
8b2dbe47
KB
10300 type = make_type_with_address_space (type, type_flags);
10301 }
10302 else if (TYPE_LENGTH (type) != byte_size)
10303 {
3e43a32a
MS
10304 complaint (&symfile_complaints,
10305 _("invalid pointer size %d"), byte_size);
8b2dbe47 10306 }
6e70227d 10307 else
9a619af0
MS
10308 {
10309 /* Should we also complain about unhandled address classes? */
10310 }
c906108c 10311 }
8b2dbe47
KB
10312
10313 TYPE_LENGTH (type) = byte_size;
f792889a 10314 return set_die_type (die, type, cu);
c906108c
SS
10315}
10316
10317/* Extract all information from a DW_TAG_ptr_to_member_type DIE and add to
10318 the user defined type vector. */
10319
f792889a 10320static struct type *
e7c27a73 10321read_tag_ptr_to_member_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c
SS
10322{
10323 struct type *type;
10324 struct type *to_type;
10325 struct type *domain;
10326
e7c27a73
DJ
10327 to_type = die_type (die, cu);
10328 domain = die_containing_type (die, cu);
0d5de010 10329
7e314c57
JK
10330 /* The calls above may have already set the type for this DIE. */
10331 type = get_die_type (die, cu);
10332 if (type)
10333 return type;
10334
0d5de010
DJ
10335 if (TYPE_CODE (check_typedef (to_type)) == TYPE_CODE_METHOD)
10336 type = lookup_methodptr_type (to_type);
10337 else
10338 type = lookup_memberptr_type (to_type, domain);
c906108c 10339
f792889a 10340 return set_die_type (die, type, cu);
c906108c
SS
10341}
10342
10343/* Extract all information from a DW_TAG_reference_type DIE and add to
10344 the user defined type vector. */
10345
f792889a 10346static struct type *
e7c27a73 10347read_tag_reference_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 10348{
e7c27a73 10349 struct comp_unit_head *cu_header = &cu->header;
7e314c57 10350 struct type *type, *target_type;
c906108c
SS
10351 struct attribute *attr;
10352
7e314c57
JK
10353 target_type = die_type (die, cu);
10354
10355 /* The die_type call above may have already set the type for this DIE. */
10356 type = get_die_type (die, cu);
10357 if (type)
10358 return type;
10359
10360 type = lookup_reference_type (target_type);
e142c38c 10361 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
c906108c
SS
10362 if (attr)
10363 {
10364 TYPE_LENGTH (type) = DW_UNSND (attr);
10365 }
10366 else
10367 {
107d2387 10368 TYPE_LENGTH (type) = cu_header->addr_size;
c906108c 10369 }
f792889a 10370 return set_die_type (die, type, cu);
c906108c
SS
10371}
10372
f792889a 10373static struct type *
e7c27a73 10374read_tag_const_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 10375{
f792889a 10376 struct type *base_type, *cv_type;
c906108c 10377
e7c27a73 10378 base_type = die_type (die, cu);
7e314c57
JK
10379
10380 /* The die_type call above may have already set the type for this DIE. */
10381 cv_type = get_die_type (die, cu);
10382 if (cv_type)
10383 return cv_type;
10384
2f608a3a
KW
10385 /* In case the const qualifier is applied to an array type, the element type
10386 is so qualified, not the array type (section 6.7.3 of C99). */
10387 if (TYPE_CODE (base_type) == TYPE_CODE_ARRAY)
10388 {
10389 struct type *el_type, *inner_array;
10390
10391 base_type = copy_type (base_type);
10392 inner_array = base_type;
10393
10394 while (TYPE_CODE (TYPE_TARGET_TYPE (inner_array)) == TYPE_CODE_ARRAY)
10395 {
10396 TYPE_TARGET_TYPE (inner_array) =
10397 copy_type (TYPE_TARGET_TYPE (inner_array));
10398 inner_array = TYPE_TARGET_TYPE (inner_array);
10399 }
10400
10401 el_type = TYPE_TARGET_TYPE (inner_array);
10402 TYPE_TARGET_TYPE (inner_array) =
10403 make_cv_type (1, TYPE_VOLATILE (el_type), el_type, NULL);
10404
10405 return set_die_type (die, base_type, cu);
10406 }
10407
f792889a
DJ
10408 cv_type = make_cv_type (1, TYPE_VOLATILE (base_type), base_type, 0);
10409 return set_die_type (die, cv_type, cu);
c906108c
SS
10410}
10411
f792889a 10412static struct type *
e7c27a73 10413read_tag_volatile_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 10414{
f792889a 10415 struct type *base_type, *cv_type;
c906108c 10416
e7c27a73 10417 base_type = die_type (die, cu);
7e314c57
JK
10418
10419 /* The die_type call above may have already set the type for this DIE. */
10420 cv_type = get_die_type (die, cu);
10421 if (cv_type)
10422 return cv_type;
10423
f792889a
DJ
10424 cv_type = make_cv_type (TYPE_CONST (base_type), 1, base_type, 0);
10425 return set_die_type (die, cv_type, cu);
c906108c
SS
10426}
10427
10428/* Extract all information from a DW_TAG_string_type DIE and add to
10429 the user defined type vector. It isn't really a user defined type,
10430 but it behaves like one, with other DIE's using an AT_user_def_type
10431 attribute to reference it. */
10432
f792889a 10433static struct type *
e7c27a73 10434read_tag_string_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 10435{
e7c27a73 10436 struct objfile *objfile = cu->objfile;
3b7538c0 10437 struct gdbarch *gdbarch = get_objfile_arch (objfile);
c906108c
SS
10438 struct type *type, *range_type, *index_type, *char_type;
10439 struct attribute *attr;
10440 unsigned int length;
10441
e142c38c 10442 attr = dwarf2_attr (die, DW_AT_string_length, cu);
c906108c
SS
10443 if (attr)
10444 {
10445 length = DW_UNSND (attr);
10446 }
10447 else
10448 {
0963b4bd 10449 /* Check for the DW_AT_byte_size attribute. */
e142c38c 10450 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
b21b22e0
PS
10451 if (attr)
10452 {
10453 length = DW_UNSND (attr);
10454 }
10455 else
10456 {
10457 length = 1;
10458 }
c906108c 10459 }
6ccb9162 10460
46bf5051 10461 index_type = objfile_type (objfile)->builtin_int;
c906108c 10462 range_type = create_range_type (NULL, index_type, 1, length);
3b7538c0
UW
10463 char_type = language_string_char_type (cu->language_defn, gdbarch);
10464 type = create_string_type (NULL, char_type, range_type);
6ccb9162 10465
f792889a 10466 return set_die_type (die, type, cu);
c906108c
SS
10467}
10468
10469/* Handle DIES due to C code like:
10470
10471 struct foo
c5aa993b
JM
10472 {
10473 int (*funcp)(int a, long l);
10474 int b;
10475 };
c906108c 10476
0963b4bd 10477 ('funcp' generates a DW_TAG_subroutine_type DIE). */
c906108c 10478
f792889a 10479static struct type *
e7c27a73 10480read_subroutine_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 10481{
bb5ed363 10482 struct objfile *objfile = cu->objfile;
0963b4bd
MS
10483 struct type *type; /* Type that this function returns. */
10484 struct type *ftype; /* Function that returns above type. */
c906108c
SS
10485 struct attribute *attr;
10486
e7c27a73 10487 type = die_type (die, cu);
7e314c57
JK
10488
10489 /* The die_type call above may have already set the type for this DIE. */
10490 ftype = get_die_type (die, cu);
10491 if (ftype)
10492 return ftype;
10493
0c8b41f1 10494 ftype = lookup_function_type (type);
c906108c 10495
5b8101ae 10496 /* All functions in C++, Pascal and Java have prototypes. */
e142c38c 10497 attr = dwarf2_attr (die, DW_AT_prototyped, cu);
c906108c 10498 if ((attr && (DW_UNSND (attr) != 0))
987504bb 10499 || cu->language == language_cplus
5b8101ae
PM
10500 || cu->language == language_java
10501 || cu->language == language_pascal)
876cecd0 10502 TYPE_PROTOTYPED (ftype) = 1;
a6c727b2
DJ
10503 else if (producer_is_realview (cu->producer))
10504 /* RealView does not emit DW_AT_prototyped. We can not
10505 distinguish prototyped and unprototyped functions; default to
10506 prototyped, since that is more common in modern code (and
10507 RealView warns about unprototyped functions). */
10508 TYPE_PROTOTYPED (ftype) = 1;
c906108c 10509
c055b101
CV
10510 /* Store the calling convention in the type if it's available in
10511 the subroutine die. Otherwise set the calling convention to
10512 the default value DW_CC_normal. */
10513 attr = dwarf2_attr (die, DW_AT_calling_convention, cu);
54fcddd0
UW
10514 if (attr)
10515 TYPE_CALLING_CONVENTION (ftype) = DW_UNSND (attr);
10516 else if (cu->producer && strstr (cu->producer, "IBM XL C for OpenCL"))
10517 TYPE_CALLING_CONVENTION (ftype) = DW_CC_GDB_IBM_OpenCL;
10518 else
10519 TYPE_CALLING_CONVENTION (ftype) = DW_CC_normal;
76c10ea2
GM
10520
10521 /* We need to add the subroutine type to the die immediately so
10522 we don't infinitely recurse when dealing with parameters
0963b4bd 10523 declared as the same subroutine type. */
76c10ea2 10524 set_die_type (die, ftype, cu);
6e70227d 10525
639d11d3 10526 if (die->child != NULL)
c906108c 10527 {
bb5ed363 10528 struct type *void_type = objfile_type (objfile)->builtin_void;
c906108c 10529 struct die_info *child_die;
8072405b 10530 int nparams, iparams;
c906108c
SS
10531
10532 /* Count the number of parameters.
10533 FIXME: GDB currently ignores vararg functions, but knows about
10534 vararg member functions. */
8072405b 10535 nparams = 0;
639d11d3 10536 child_die = die->child;
c906108c
SS
10537 while (child_die && child_die->tag)
10538 {
10539 if (child_die->tag == DW_TAG_formal_parameter)
10540 nparams++;
10541 else if (child_die->tag == DW_TAG_unspecified_parameters)
876cecd0 10542 TYPE_VARARGS (ftype) = 1;
c906108c
SS
10543 child_die = sibling_die (child_die);
10544 }
10545
10546 /* Allocate storage for parameters and fill them in. */
10547 TYPE_NFIELDS (ftype) = nparams;
10548 TYPE_FIELDS (ftype) = (struct field *)
ae5a43e0 10549 TYPE_ZALLOC (ftype, nparams * sizeof (struct field));
c906108c 10550
8072405b
JK
10551 /* TYPE_FIELD_TYPE must never be NULL. Pre-fill the array to ensure it
10552 even if we error out during the parameters reading below. */
10553 for (iparams = 0; iparams < nparams; iparams++)
10554 TYPE_FIELD_TYPE (ftype, iparams) = void_type;
10555
10556 iparams = 0;
639d11d3 10557 child_die = die->child;
c906108c
SS
10558 while (child_die && child_die->tag)
10559 {
10560 if (child_die->tag == DW_TAG_formal_parameter)
10561 {
3ce3b1ba
PA
10562 struct type *arg_type;
10563
10564 /* DWARF version 2 has no clean way to discern C++
10565 static and non-static member functions. G++ helps
10566 GDB by marking the first parameter for non-static
10567 member functions (which is the this pointer) as
10568 artificial. We pass this information to
10569 dwarf2_add_member_fn via TYPE_FIELD_ARTIFICIAL.
10570
10571 DWARF version 3 added DW_AT_object_pointer, which GCC
10572 4.5 does not yet generate. */
e142c38c 10573 attr = dwarf2_attr (child_die, DW_AT_artificial, cu);
c906108c
SS
10574 if (attr)
10575 TYPE_FIELD_ARTIFICIAL (ftype, iparams) = DW_UNSND (attr);
10576 else
418835cc
KS
10577 {
10578 TYPE_FIELD_ARTIFICIAL (ftype, iparams) = 0;
10579
10580 /* GCC/43521: In java, the formal parameter
10581 "this" is sometimes not marked with DW_AT_artificial. */
10582 if (cu->language == language_java)
10583 {
10584 const char *name = dwarf2_name (child_die, cu);
9a619af0 10585
418835cc
KS
10586 if (name && !strcmp (name, "this"))
10587 TYPE_FIELD_ARTIFICIAL (ftype, iparams) = 1;
10588 }
10589 }
3ce3b1ba
PA
10590 arg_type = die_type (child_die, cu);
10591
10592 /* RealView does not mark THIS as const, which the testsuite
10593 expects. GCC marks THIS as const in method definitions,
10594 but not in the class specifications (GCC PR 43053). */
10595 if (cu->language == language_cplus && !TYPE_CONST (arg_type)
10596 && TYPE_FIELD_ARTIFICIAL (ftype, iparams))
10597 {
10598 int is_this = 0;
10599 struct dwarf2_cu *arg_cu = cu;
10600 const char *name = dwarf2_name (child_die, cu);
10601
10602 attr = dwarf2_attr (die, DW_AT_object_pointer, cu);
10603 if (attr)
10604 {
10605 /* If the compiler emits this, use it. */
10606 if (follow_die_ref (die, attr, &arg_cu) == child_die)
10607 is_this = 1;
10608 }
10609 else if (name && strcmp (name, "this") == 0)
10610 /* Function definitions will have the argument names. */
10611 is_this = 1;
10612 else if (name == NULL && iparams == 0)
10613 /* Declarations may not have the names, so like
10614 elsewhere in GDB, assume an artificial first
10615 argument is "this". */
10616 is_this = 1;
10617
10618 if (is_this)
10619 arg_type = make_cv_type (1, TYPE_VOLATILE (arg_type),
10620 arg_type, 0);
10621 }
10622
10623 TYPE_FIELD_TYPE (ftype, iparams) = arg_type;
c906108c
SS
10624 iparams++;
10625 }
10626 child_die = sibling_die (child_die);
10627 }
10628 }
10629
76c10ea2 10630 return ftype;
c906108c
SS
10631}
10632
f792889a 10633static struct type *
e7c27a73 10634read_typedef (struct die_info *die, struct dwarf2_cu *cu)
c906108c 10635{
e7c27a73 10636 struct objfile *objfile = cu->objfile;
0114d602 10637 const char *name = NULL;
3c8e0968 10638 struct type *this_type, *target_type;
c906108c 10639
94af9270 10640 name = dwarf2_full_name (NULL, die, cu);
f792889a 10641 this_type = init_type (TYPE_CODE_TYPEDEF, 0,
0114d602
DJ
10642 TYPE_FLAG_TARGET_STUB, NULL, objfile);
10643 TYPE_NAME (this_type) = (char *) name;
f792889a 10644 set_die_type (die, this_type, cu);
3c8e0968
DE
10645 target_type = die_type (die, cu);
10646 if (target_type != this_type)
10647 TYPE_TARGET_TYPE (this_type) = target_type;
10648 else
10649 {
10650 /* Self-referential typedefs are, it seems, not allowed by the DWARF
10651 spec and cause infinite loops in GDB. */
10652 complaint (&symfile_complaints,
10653 _("Self-referential DW_TAG_typedef "
10654 "- DIE at 0x%x [in module %s]"),
b64f50a1 10655 die->offset.sect_off, objfile->name);
3c8e0968
DE
10656 TYPE_TARGET_TYPE (this_type) = NULL;
10657 }
f792889a 10658 return this_type;
c906108c
SS
10659}
10660
10661/* Find a representation of a given base type and install
10662 it in the TYPE field of the die. */
10663
f792889a 10664static struct type *
e7c27a73 10665read_base_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 10666{
e7c27a73 10667 struct objfile *objfile = cu->objfile;
c906108c
SS
10668 struct type *type;
10669 struct attribute *attr;
10670 int encoding = 0, size = 0;
39cbfefa 10671 char *name;
6ccb9162
UW
10672 enum type_code code = TYPE_CODE_INT;
10673 int type_flags = 0;
10674 struct type *target_type = NULL;
c906108c 10675
e142c38c 10676 attr = dwarf2_attr (die, DW_AT_encoding, cu);
c906108c
SS
10677 if (attr)
10678 {
10679 encoding = DW_UNSND (attr);
10680 }
e142c38c 10681 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
c906108c
SS
10682 if (attr)
10683 {
10684 size = DW_UNSND (attr);
10685 }
39cbfefa 10686 name = dwarf2_name (die, cu);
6ccb9162 10687 if (!name)
c906108c 10688 {
6ccb9162
UW
10689 complaint (&symfile_complaints,
10690 _("DW_AT_name missing from DW_TAG_base_type"));
c906108c 10691 }
6ccb9162
UW
10692
10693 switch (encoding)
c906108c 10694 {
6ccb9162
UW
10695 case DW_ATE_address:
10696 /* Turn DW_ATE_address into a void * pointer. */
10697 code = TYPE_CODE_PTR;
10698 type_flags |= TYPE_FLAG_UNSIGNED;
10699 target_type = init_type (TYPE_CODE_VOID, 1, 0, NULL, objfile);
10700 break;
10701 case DW_ATE_boolean:
10702 code = TYPE_CODE_BOOL;
10703 type_flags |= TYPE_FLAG_UNSIGNED;
10704 break;
10705 case DW_ATE_complex_float:
10706 code = TYPE_CODE_COMPLEX;
10707 target_type = init_type (TYPE_CODE_FLT, size / 2, 0, NULL, objfile);
10708 break;
10709 case DW_ATE_decimal_float:
10710 code = TYPE_CODE_DECFLOAT;
10711 break;
10712 case DW_ATE_float:
10713 code = TYPE_CODE_FLT;
10714 break;
10715 case DW_ATE_signed:
10716 break;
10717 case DW_ATE_unsigned:
10718 type_flags |= TYPE_FLAG_UNSIGNED;
3b2b8fea
TT
10719 if (cu->language == language_fortran
10720 && name
10721 && strncmp (name, "character(", sizeof ("character(") - 1) == 0)
10722 code = TYPE_CODE_CHAR;
6ccb9162
UW
10723 break;
10724 case DW_ATE_signed_char:
6e70227d 10725 if (cu->language == language_ada || cu->language == language_m2
3b2b8fea
TT
10726 || cu->language == language_pascal
10727 || cu->language == language_fortran)
6ccb9162
UW
10728 code = TYPE_CODE_CHAR;
10729 break;
10730 case DW_ATE_unsigned_char:
868a0084 10731 if (cu->language == language_ada || cu->language == language_m2
3b2b8fea
TT
10732 || cu->language == language_pascal
10733 || cu->language == language_fortran)
6ccb9162
UW
10734 code = TYPE_CODE_CHAR;
10735 type_flags |= TYPE_FLAG_UNSIGNED;
10736 break;
75079b2b
TT
10737 case DW_ATE_UTF:
10738 /* We just treat this as an integer and then recognize the
10739 type by name elsewhere. */
10740 break;
10741
6ccb9162
UW
10742 default:
10743 complaint (&symfile_complaints, _("unsupported DW_AT_encoding: '%s'"),
10744 dwarf_type_encoding_name (encoding));
10745 break;
c906108c 10746 }
6ccb9162 10747
0114d602
DJ
10748 type = init_type (code, size, type_flags, NULL, objfile);
10749 TYPE_NAME (type) = name;
6ccb9162
UW
10750 TYPE_TARGET_TYPE (type) = target_type;
10751
0114d602 10752 if (name && strcmp (name, "char") == 0)
876cecd0 10753 TYPE_NOSIGN (type) = 1;
0114d602 10754
f792889a 10755 return set_die_type (die, type, cu);
c906108c
SS
10756}
10757
a02abb62
JB
10758/* Read the given DW_AT_subrange DIE. */
10759
f792889a 10760static struct type *
a02abb62
JB
10761read_subrange_type (struct die_info *die, struct dwarf2_cu *cu)
10762{
10763 struct type *base_type;
10764 struct type *range_type;
10765 struct attribute *attr;
4fae6e18
JK
10766 LONGEST low, high;
10767 int low_default_is_valid;
39cbfefa 10768 char *name;
43bbcdc2 10769 LONGEST negative_mask;
e77813c8 10770
a02abb62 10771 base_type = die_type (die, cu);
953ac07e
JK
10772 /* Preserve BASE_TYPE's original type, just set its LENGTH. */
10773 check_typedef (base_type);
a02abb62 10774
7e314c57
JK
10775 /* The die_type call above may have already set the type for this DIE. */
10776 range_type = get_die_type (die, cu);
10777 if (range_type)
10778 return range_type;
10779
4fae6e18
JK
10780 /* Set LOW_DEFAULT_IS_VALID if current language and DWARF version allow
10781 omitting DW_AT_lower_bound. */
10782 switch (cu->language)
6e70227d 10783 {
4fae6e18
JK
10784 case language_c:
10785 case language_cplus:
10786 low = 0;
10787 low_default_is_valid = 1;
10788 break;
10789 case language_fortran:
10790 low = 1;
10791 low_default_is_valid = 1;
10792 break;
10793 case language_d:
10794 case language_java:
10795 case language_objc:
10796 low = 0;
10797 low_default_is_valid = (cu->header.version >= 4);
10798 break;
10799 case language_ada:
10800 case language_m2:
10801 case language_pascal:
a02abb62 10802 low = 1;
4fae6e18
JK
10803 low_default_is_valid = (cu->header.version >= 4);
10804 break;
10805 default:
10806 low = 0;
10807 low_default_is_valid = 0;
10808 break;
a02abb62
JB
10809 }
10810
dd5e6932
DJ
10811 /* FIXME: For variable sized arrays either of these could be
10812 a variable rather than a constant value. We'll allow it,
10813 but we don't know how to handle it. */
e142c38c 10814 attr = dwarf2_attr (die, DW_AT_lower_bound, cu);
a02abb62 10815 if (attr)
4fae6e18
JK
10816 low = dwarf2_get_attr_constant_value (attr, low);
10817 else if (!low_default_is_valid)
10818 complaint (&symfile_complaints, _("Missing DW_AT_lower_bound "
10819 "- DIE at 0x%x [in module %s]"),
10820 die->offset.sect_off, cu->objfile->name);
a02abb62 10821
e142c38c 10822 attr = dwarf2_attr (die, DW_AT_upper_bound, cu);
a02abb62 10823 if (attr)
6e70227d 10824 {
d48323d8 10825 if (attr_form_is_block (attr) || is_ref_attr (attr))
a02abb62
JB
10826 {
10827 /* GCC encodes arrays with unspecified or dynamic length
e77813c8 10828 with a DW_FORM_block1 attribute or a reference attribute.
a02abb62
JB
10829 FIXME: GDB does not yet know how to handle dynamic
10830 arrays properly, treat them as arrays with unspecified
10831 length for now.
10832
10833 FIXME: jimb/2003-09-22: GDB does not really know
10834 how to handle arrays of unspecified length
10835 either; we just represent them as zero-length
10836 arrays. Choose an appropriate upper bound given
10837 the lower bound we've computed above. */
10838 high = low - 1;
10839 }
10840 else
10841 high = dwarf2_get_attr_constant_value (attr, 1);
10842 }
e77813c8
PM
10843 else
10844 {
10845 attr = dwarf2_attr (die, DW_AT_count, cu);
10846 if (attr)
10847 {
10848 int count = dwarf2_get_attr_constant_value (attr, 1);
10849 high = low + count - 1;
10850 }
c2ff108b
JK
10851 else
10852 {
10853 /* Unspecified array length. */
10854 high = low - 1;
10855 }
e77813c8
PM
10856 }
10857
10858 /* Dwarf-2 specifications explicitly allows to create subrange types
10859 without specifying a base type.
10860 In that case, the base type must be set to the type of
10861 the lower bound, upper bound or count, in that order, if any of these
10862 three attributes references an object that has a type.
10863 If no base type is found, the Dwarf-2 specifications say that
10864 a signed integer type of size equal to the size of an address should
10865 be used.
10866 For the following C code: `extern char gdb_int [];'
10867 GCC produces an empty range DIE.
10868 FIXME: muller/2010-05-28: Possible references to object for low bound,
0963b4bd 10869 high bound or count are not yet handled by this code. */
e77813c8
PM
10870 if (TYPE_CODE (base_type) == TYPE_CODE_VOID)
10871 {
10872 struct objfile *objfile = cu->objfile;
10873 struct gdbarch *gdbarch = get_objfile_arch (objfile);
10874 int addr_size = gdbarch_addr_bit (gdbarch) /8;
10875 struct type *int_type = objfile_type (objfile)->builtin_int;
10876
10877 /* Test "int", "long int", and "long long int" objfile types,
10878 and select the first one having a size above or equal to the
10879 architecture address size. */
10880 if (int_type && TYPE_LENGTH (int_type) >= addr_size)
10881 base_type = int_type;
10882 else
10883 {
10884 int_type = objfile_type (objfile)->builtin_long;
10885 if (int_type && TYPE_LENGTH (int_type) >= addr_size)
10886 base_type = int_type;
10887 else
10888 {
10889 int_type = objfile_type (objfile)->builtin_long_long;
10890 if (int_type && TYPE_LENGTH (int_type) >= addr_size)
10891 base_type = int_type;
10892 }
10893 }
10894 }
a02abb62 10895
6e70227d 10896 negative_mask =
43bbcdc2
PH
10897 (LONGEST) -1 << (TYPE_LENGTH (base_type) * TARGET_CHAR_BIT - 1);
10898 if (!TYPE_UNSIGNED (base_type) && (low & negative_mask))
10899 low |= negative_mask;
10900 if (!TYPE_UNSIGNED (base_type) && (high & negative_mask))
10901 high |= negative_mask;
10902
a02abb62
JB
10903 range_type = create_range_type (NULL, base_type, low, high);
10904
bbb0eef6
JK
10905 /* Mark arrays with dynamic length at least as an array of unspecified
10906 length. GDB could check the boundary but before it gets implemented at
10907 least allow accessing the array elements. */
d48323d8 10908 if (attr && attr_form_is_block (attr))
bbb0eef6
JK
10909 TYPE_HIGH_BOUND_UNDEFINED (range_type) = 1;
10910
c2ff108b
JK
10911 /* Ada expects an empty array on no boundary attributes. */
10912 if (attr == NULL && cu->language != language_ada)
10913 TYPE_HIGH_BOUND_UNDEFINED (range_type) = 1;
10914
39cbfefa
DJ
10915 name = dwarf2_name (die, cu);
10916 if (name)
10917 TYPE_NAME (range_type) = name;
6e70227d 10918
e142c38c 10919 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
a02abb62
JB
10920 if (attr)
10921 TYPE_LENGTH (range_type) = DW_UNSND (attr);
10922
7e314c57
JK
10923 set_die_type (die, range_type, cu);
10924
10925 /* set_die_type should be already done. */
b4ba55a1
JB
10926 set_descriptive_type (range_type, die, cu);
10927
7e314c57 10928 return range_type;
a02abb62 10929}
6e70227d 10930
f792889a 10931static struct type *
81a17f79
JB
10932read_unspecified_type (struct die_info *die, struct dwarf2_cu *cu)
10933{
10934 struct type *type;
81a17f79 10935
81a17f79
JB
10936 /* For now, we only support the C meaning of an unspecified type: void. */
10937
0114d602
DJ
10938 type = init_type (TYPE_CODE_VOID, 0, 0, NULL, cu->objfile);
10939 TYPE_NAME (type) = dwarf2_name (die, cu);
81a17f79 10940
f792889a 10941 return set_die_type (die, type, cu);
81a17f79 10942}
a02abb62 10943
639d11d3
DC
10944/* Read a single die and all its descendents. Set the die's sibling
10945 field to NULL; set other fields in the die correctly, and set all
10946 of the descendents' fields correctly. Set *NEW_INFO_PTR to the
10947 location of the info_ptr after reading all of those dies. PARENT
10948 is the parent of the die in question. */
10949
10950static struct die_info *
dee91e82
DE
10951read_die_and_children (const struct die_reader_specs *reader,
10952 gdb_byte *info_ptr,
10953 gdb_byte **new_info_ptr,
10954 struct die_info *parent)
639d11d3
DC
10955{
10956 struct die_info *die;
fe1b8b76 10957 gdb_byte *cur_ptr;
639d11d3
DC
10958 int has_children;
10959
93311388 10960 cur_ptr = read_full_die (reader, &die, info_ptr, &has_children);
1d325ec1
DJ
10961 if (die == NULL)
10962 {
10963 *new_info_ptr = cur_ptr;
10964 return NULL;
10965 }
93311388 10966 store_in_ref_table (die, reader->cu);
639d11d3
DC
10967
10968 if (has_children)
348e048f 10969 die->child = read_die_and_siblings (reader, cur_ptr, new_info_ptr, die);
639d11d3
DC
10970 else
10971 {
10972 die->child = NULL;
10973 *new_info_ptr = cur_ptr;
10974 }
10975
10976 die->sibling = NULL;
10977 die->parent = parent;
10978 return die;
10979}
10980
10981/* Read a die, all of its descendents, and all of its siblings; set
10982 all of the fields of all of the dies correctly. Arguments are as
10983 in read_die_and_children. */
10984
10985static struct die_info *
93311388
DE
10986read_die_and_siblings (const struct die_reader_specs *reader,
10987 gdb_byte *info_ptr,
fe1b8b76 10988 gdb_byte **new_info_ptr,
639d11d3
DC
10989 struct die_info *parent)
10990{
10991 struct die_info *first_die, *last_sibling;
fe1b8b76 10992 gdb_byte *cur_ptr;
639d11d3 10993
c906108c 10994 cur_ptr = info_ptr;
639d11d3
DC
10995 first_die = last_sibling = NULL;
10996
10997 while (1)
c906108c 10998 {
639d11d3 10999 struct die_info *die
dee91e82 11000 = read_die_and_children (reader, cur_ptr, &cur_ptr, parent);
639d11d3 11001
1d325ec1 11002 if (die == NULL)
c906108c 11003 {
639d11d3
DC
11004 *new_info_ptr = cur_ptr;
11005 return first_die;
c906108c 11006 }
1d325ec1
DJ
11007
11008 if (!first_die)
11009 first_die = die;
c906108c 11010 else
1d325ec1
DJ
11011 last_sibling->sibling = die;
11012
11013 last_sibling = die;
c906108c 11014 }
c906108c
SS
11015}
11016
3019eac3
DE
11017/* Read a die and all its attributes, leave space for NUM_EXTRA_ATTRS
11018 attributes.
11019 The caller is responsible for filling in the extra attributes
11020 and updating (*DIEP)->num_attrs.
11021 Set DIEP to point to a newly allocated die with its information,
11022 except for its child, sibling, and parent fields.
11023 Set HAS_CHILDREN to tell whether the die has children or not. */
93311388
DE
11024
11025static gdb_byte *
3019eac3
DE
11026read_full_die_1 (const struct die_reader_specs *reader,
11027 struct die_info **diep, gdb_byte *info_ptr,
11028 int *has_children, int num_extra_attrs)
93311388 11029{
b64f50a1
JK
11030 unsigned int abbrev_number, bytes_read, i;
11031 sect_offset offset;
93311388
DE
11032 struct abbrev_info *abbrev;
11033 struct die_info *die;
11034 struct dwarf2_cu *cu = reader->cu;
11035 bfd *abfd = reader->abfd;
11036
b64f50a1 11037 offset.sect_off = info_ptr - reader->buffer;
93311388
DE
11038 abbrev_number = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
11039 info_ptr += bytes_read;
11040 if (!abbrev_number)
11041 {
11042 *diep = NULL;
11043 *has_children = 0;
11044 return info_ptr;
11045 }
11046
433df2d4 11047 abbrev = abbrev_table_lookup_abbrev (cu->abbrev_table, abbrev_number);
93311388 11048 if (!abbrev)
348e048f
DE
11049 error (_("Dwarf Error: could not find abbrev number %d [in module %s]"),
11050 abbrev_number,
11051 bfd_get_filename (abfd));
11052
3019eac3 11053 die = dwarf_alloc_die (cu, abbrev->num_attrs + num_extra_attrs);
93311388
DE
11054 die->offset = offset;
11055 die->tag = abbrev->tag;
11056 die->abbrev = abbrev_number;
11057
3019eac3
DE
11058 /* Make the result usable.
11059 The caller needs to update num_attrs after adding the extra
11060 attributes. */
93311388
DE
11061 die->num_attrs = abbrev->num_attrs;
11062
11063 for (i = 0; i < abbrev->num_attrs; ++i)
dee91e82
DE
11064 info_ptr = read_attribute (reader, &die->attrs[i], &abbrev->attrs[i],
11065 info_ptr);
93311388
DE
11066
11067 *diep = die;
11068 *has_children = abbrev->has_children;
11069 return info_ptr;
11070}
11071
3019eac3
DE
11072/* Read a die and all its attributes.
11073 Set DIEP to point to a newly allocated die with its information,
11074 except for its child, sibling, and parent fields.
11075 Set HAS_CHILDREN to tell whether the die has children or not. */
11076
11077static gdb_byte *
11078read_full_die (const struct die_reader_specs *reader,
11079 struct die_info **diep, gdb_byte *info_ptr,
11080 int *has_children)
11081{
11082 return read_full_die_1 (reader, diep, info_ptr, has_children, 0);
11083}
433df2d4
DE
11084\f
11085/* Abbreviation tables.
3019eac3 11086
433df2d4 11087 In DWARF version 2, the description of the debugging information is
c906108c
SS
11088 stored in a separate .debug_abbrev section. Before we read any
11089 dies from a section we read in all abbreviations and install them
433df2d4
DE
11090 in a hash table. */
11091
11092/* Allocate space for a struct abbrev_info object in ABBREV_TABLE. */
11093
11094static struct abbrev_info *
11095abbrev_table_alloc_abbrev (struct abbrev_table *abbrev_table)
11096{
11097 struct abbrev_info *abbrev;
11098
11099 abbrev = (struct abbrev_info *)
11100 obstack_alloc (&abbrev_table->abbrev_obstack, sizeof (struct abbrev_info));
11101 memset (abbrev, 0, sizeof (struct abbrev_info));
11102 return abbrev;
11103}
11104
11105/* Add an abbreviation to the table. */
c906108c
SS
11106
11107static void
433df2d4
DE
11108abbrev_table_add_abbrev (struct abbrev_table *abbrev_table,
11109 unsigned int abbrev_number,
11110 struct abbrev_info *abbrev)
11111{
11112 unsigned int hash_number;
11113
11114 hash_number = abbrev_number % ABBREV_HASH_SIZE;
11115 abbrev->next = abbrev_table->abbrevs[hash_number];
11116 abbrev_table->abbrevs[hash_number] = abbrev;
11117}
dee91e82 11118
433df2d4
DE
11119/* Look up an abbrev in the table.
11120 Returns NULL if the abbrev is not found. */
11121
11122static struct abbrev_info *
11123abbrev_table_lookup_abbrev (const struct abbrev_table *abbrev_table,
11124 unsigned int abbrev_number)
c906108c 11125{
433df2d4
DE
11126 unsigned int hash_number;
11127 struct abbrev_info *abbrev;
11128
11129 hash_number = abbrev_number % ABBREV_HASH_SIZE;
11130 abbrev = abbrev_table->abbrevs[hash_number];
11131
11132 while (abbrev)
11133 {
11134 if (abbrev->number == abbrev_number)
11135 return abbrev;
11136 abbrev = abbrev->next;
11137 }
11138 return NULL;
11139}
11140
11141/* Read in an abbrev table. */
11142
11143static struct abbrev_table *
11144abbrev_table_read_table (struct dwarf2_section_info *section,
11145 sect_offset offset)
11146{
11147 struct objfile *objfile = dwarf2_per_objfile->objfile;
11148 bfd *abfd = section->asection->owner;
11149 struct abbrev_table *abbrev_table;
fe1b8b76 11150 gdb_byte *abbrev_ptr;
c906108c
SS
11151 struct abbrev_info *cur_abbrev;
11152 unsigned int abbrev_number, bytes_read, abbrev_name;
433df2d4 11153 unsigned int abbrev_form;
f3dd6933
DJ
11154 struct attr_abbrev *cur_attrs;
11155 unsigned int allocated_attrs;
c906108c 11156
433df2d4
DE
11157 abbrev_table = XMALLOC (struct abbrev_table);
11158 obstack_init (&abbrev_table->abbrev_obstack);
11159 abbrev_table->abbrevs = obstack_alloc (&abbrev_table->abbrev_obstack,
11160 (ABBREV_HASH_SIZE
11161 * sizeof (struct abbrev_info *)));
11162 memset (abbrev_table->abbrevs, 0,
11163 ABBREV_HASH_SIZE * sizeof (struct abbrev_info *));
c906108c 11164
433df2d4
DE
11165 dwarf2_read_section (objfile, section);
11166 abbrev_ptr = section->buffer + offset.sect_off;
c906108c
SS
11167 abbrev_number = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
11168 abbrev_ptr += bytes_read;
11169
f3dd6933
DJ
11170 allocated_attrs = ATTR_ALLOC_CHUNK;
11171 cur_attrs = xmalloc (allocated_attrs * sizeof (struct attr_abbrev));
6e70227d 11172
0963b4bd 11173 /* Loop until we reach an abbrev number of 0. */
c906108c
SS
11174 while (abbrev_number)
11175 {
433df2d4 11176 cur_abbrev = abbrev_table_alloc_abbrev (abbrev_table);
c906108c
SS
11177
11178 /* read in abbrev header */
11179 cur_abbrev->number = abbrev_number;
11180 cur_abbrev->tag = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
11181 abbrev_ptr += bytes_read;
11182 cur_abbrev->has_children = read_1_byte (abfd, abbrev_ptr);
11183 abbrev_ptr += 1;
11184
11185 /* now read in declarations */
11186 abbrev_name = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
11187 abbrev_ptr += bytes_read;
11188 abbrev_form = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
11189 abbrev_ptr += bytes_read;
11190 while (abbrev_name)
11191 {
f3dd6933 11192 if (cur_abbrev->num_attrs == allocated_attrs)
c906108c 11193 {
f3dd6933
DJ
11194 allocated_attrs += ATTR_ALLOC_CHUNK;
11195 cur_attrs
11196 = xrealloc (cur_attrs, (allocated_attrs
11197 * sizeof (struct attr_abbrev)));
c906108c 11198 }
ae038cb0 11199
f3dd6933
DJ
11200 cur_attrs[cur_abbrev->num_attrs].name = abbrev_name;
11201 cur_attrs[cur_abbrev->num_attrs++].form = abbrev_form;
c906108c
SS
11202 abbrev_name = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
11203 abbrev_ptr += bytes_read;
11204 abbrev_form = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
11205 abbrev_ptr += bytes_read;
11206 }
11207
433df2d4 11208 cur_abbrev->attrs = obstack_alloc (&abbrev_table->abbrev_obstack,
f3dd6933
DJ
11209 (cur_abbrev->num_attrs
11210 * sizeof (struct attr_abbrev)));
11211 memcpy (cur_abbrev->attrs, cur_attrs,
11212 cur_abbrev->num_attrs * sizeof (struct attr_abbrev));
11213
433df2d4 11214 abbrev_table_add_abbrev (abbrev_table, abbrev_number, cur_abbrev);
c906108c
SS
11215
11216 /* Get next abbreviation.
11217 Under Irix6 the abbreviations for a compilation unit are not
c5aa993b
JM
11218 always properly terminated with an abbrev number of 0.
11219 Exit loop if we encounter an abbreviation which we have
11220 already read (which means we are about to read the abbreviations
11221 for the next compile unit) or if the end of the abbreviation
11222 table is reached. */
433df2d4 11223 if ((unsigned int) (abbrev_ptr - section->buffer) >= section->size)
c906108c
SS
11224 break;
11225 abbrev_number = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
11226 abbrev_ptr += bytes_read;
433df2d4 11227 if (abbrev_table_lookup_abbrev (abbrev_table, abbrev_number) != NULL)
c906108c
SS
11228 break;
11229 }
f3dd6933
DJ
11230
11231 xfree (cur_attrs);
433df2d4 11232 return abbrev_table;
c906108c
SS
11233}
11234
433df2d4 11235/* Free the resources held by ABBREV_TABLE. */
c906108c 11236
c906108c 11237static void
433df2d4 11238abbrev_table_free (struct abbrev_table *abbrev_table)
c906108c 11239{
433df2d4
DE
11240 obstack_free (&abbrev_table->abbrev_obstack, NULL);
11241 xfree (abbrev_table);
c906108c
SS
11242}
11243
433df2d4
DE
11244/* Read the abbrev table for CU from ABBREV_SECTION. */
11245
11246static void
11247dwarf2_read_abbrevs (struct dwarf2_cu *cu,
11248 struct dwarf2_section_info *abbrev_section)
c906108c 11249{
433df2d4
DE
11250 cu->abbrev_table =
11251 abbrev_table_read_table (abbrev_section, cu->header.abbrev_offset);
11252}
c906108c 11253
433df2d4 11254/* Release the memory used by the abbrev table for a compilation unit. */
c906108c 11255
433df2d4
DE
11256static void
11257dwarf2_free_abbrev_table (void *ptr_to_cu)
11258{
11259 struct dwarf2_cu *cu = ptr_to_cu;
c906108c 11260
433df2d4
DE
11261 abbrev_table_free (cu->abbrev_table);
11262 /* Set this to NULL so that we SEGV if we try to read it later,
11263 and also because free_comp_unit verifies this is NULL. */
11264 cu->abbrev_table = NULL;
11265}
11266\f
72bf9492
DJ
11267/* Returns nonzero if TAG represents a type that we might generate a partial
11268 symbol for. */
11269
11270static int
11271is_type_tag_for_partial (int tag)
11272{
11273 switch (tag)
11274 {
11275#if 0
11276 /* Some types that would be reasonable to generate partial symbols for,
11277 that we don't at present. */
11278 case DW_TAG_array_type:
11279 case DW_TAG_file_type:
11280 case DW_TAG_ptr_to_member_type:
11281 case DW_TAG_set_type:
11282 case DW_TAG_string_type:
11283 case DW_TAG_subroutine_type:
11284#endif
11285 case DW_TAG_base_type:
11286 case DW_TAG_class_type:
680b30c7 11287 case DW_TAG_interface_type:
72bf9492
DJ
11288 case DW_TAG_enumeration_type:
11289 case DW_TAG_structure_type:
11290 case DW_TAG_subrange_type:
11291 case DW_TAG_typedef:
11292 case DW_TAG_union_type:
11293 return 1;
11294 default:
11295 return 0;
11296 }
11297}
11298
11299/* Load all DIEs that are interesting for partial symbols into memory. */
11300
11301static struct partial_die_info *
dee91e82
DE
11302load_partial_dies (const struct die_reader_specs *reader,
11303 gdb_byte *info_ptr, int building_psymtab)
72bf9492 11304{
dee91e82 11305 struct dwarf2_cu *cu = reader->cu;
bb5ed363 11306 struct objfile *objfile = cu->objfile;
72bf9492
DJ
11307 struct partial_die_info *part_die;
11308 struct partial_die_info *parent_die, *last_die, *first_die = NULL;
11309 struct abbrev_info *abbrev;
11310 unsigned int bytes_read;
5afb4e99 11311 unsigned int load_all = 0;
72bf9492
DJ
11312 int nesting_level = 1;
11313
11314 parent_die = NULL;
11315 last_die = NULL;
11316
7adf1e79
DE
11317 gdb_assert (cu->per_cu != NULL);
11318 if (cu->per_cu->load_all_dies)
5afb4e99
DJ
11319 load_all = 1;
11320
72bf9492
DJ
11321 cu->partial_dies
11322 = htab_create_alloc_ex (cu->header.length / 12,
11323 partial_die_hash,
11324 partial_die_eq,
11325 NULL,
11326 &cu->comp_unit_obstack,
11327 hashtab_obstack_allocate,
11328 dummy_obstack_deallocate);
11329
11330 part_die = obstack_alloc (&cu->comp_unit_obstack,
11331 sizeof (struct partial_die_info));
11332
11333 while (1)
11334 {
11335 abbrev = peek_die_abbrev (info_ptr, &bytes_read, cu);
11336
11337 /* A NULL abbrev means the end of a series of children. */
11338 if (abbrev == NULL)
11339 {
11340 if (--nesting_level == 0)
11341 {
11342 /* PART_DIE was probably the last thing allocated on the
11343 comp_unit_obstack, so we could call obstack_free
11344 here. We don't do that because the waste is small,
11345 and will be cleaned up when we're done with this
11346 compilation unit. This way, we're also more robust
11347 against other users of the comp_unit_obstack. */
11348 return first_die;
11349 }
11350 info_ptr += bytes_read;
11351 last_die = parent_die;
11352 parent_die = parent_die->die_parent;
11353 continue;
11354 }
11355
98bfdba5
PA
11356 /* Check for template arguments. We never save these; if
11357 they're seen, we just mark the parent, and go on our way. */
11358 if (parent_die != NULL
11359 && cu->language == language_cplus
11360 && (abbrev->tag == DW_TAG_template_type_param
11361 || abbrev->tag == DW_TAG_template_value_param))
11362 {
11363 parent_die->has_template_arguments = 1;
11364
11365 if (!load_all)
11366 {
11367 /* We don't need a partial DIE for the template argument. */
dee91e82 11368 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
98bfdba5
PA
11369 continue;
11370 }
11371 }
11372
0d99eb77 11373 /* We only recurse into c++ subprograms looking for template arguments.
98bfdba5
PA
11374 Skip their other children. */
11375 if (!load_all
11376 && cu->language == language_cplus
11377 && parent_die != NULL
11378 && parent_die->tag == DW_TAG_subprogram)
11379 {
dee91e82 11380 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
98bfdba5
PA
11381 continue;
11382 }
11383
5afb4e99
DJ
11384 /* Check whether this DIE is interesting enough to save. Normally
11385 we would not be interested in members here, but there may be
11386 later variables referencing them via DW_AT_specification (for
11387 static members). */
11388 if (!load_all
11389 && !is_type_tag_for_partial (abbrev->tag)
72929c62 11390 && abbrev->tag != DW_TAG_constant
72bf9492
DJ
11391 && abbrev->tag != DW_TAG_enumerator
11392 && abbrev->tag != DW_TAG_subprogram
bc30ff58 11393 && abbrev->tag != DW_TAG_lexical_block
72bf9492 11394 && abbrev->tag != DW_TAG_variable
5afb4e99 11395 && abbrev->tag != DW_TAG_namespace
f55ee35c 11396 && abbrev->tag != DW_TAG_module
95554aad
TT
11397 && abbrev->tag != DW_TAG_member
11398 && abbrev->tag != DW_TAG_imported_unit)
72bf9492
DJ
11399 {
11400 /* Otherwise we skip to the next sibling, if any. */
dee91e82 11401 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
72bf9492
DJ
11402 continue;
11403 }
11404
dee91e82
DE
11405 info_ptr = read_partial_die (reader, part_die, abbrev, bytes_read,
11406 info_ptr);
72bf9492
DJ
11407
11408 /* This two-pass algorithm for processing partial symbols has a
11409 high cost in cache pressure. Thus, handle some simple cases
11410 here which cover the majority of C partial symbols. DIEs
11411 which neither have specification tags in them, nor could have
11412 specification tags elsewhere pointing at them, can simply be
11413 processed and discarded.
11414
11415 This segment is also optional; scan_partial_symbols and
11416 add_partial_symbol will handle these DIEs if we chain
11417 them in normally. When compilers which do not emit large
11418 quantities of duplicate debug information are more common,
11419 this code can probably be removed. */
11420
11421 /* Any complete simple types at the top level (pretty much all
11422 of them, for a language without namespaces), can be processed
11423 directly. */
11424 if (parent_die == NULL
11425 && part_die->has_specification == 0
11426 && part_die->is_declaration == 0
d8228535 11427 && ((part_die->tag == DW_TAG_typedef && !part_die->has_children)
72bf9492
DJ
11428 || part_die->tag == DW_TAG_base_type
11429 || part_die->tag == DW_TAG_subrange_type))
11430 {
11431 if (building_psymtab && part_die->name != NULL)
04a679b8 11432 add_psymbol_to_list (part_die->name, strlen (part_die->name), 0,
72bf9492 11433 VAR_DOMAIN, LOC_TYPEDEF,
bb5ed363
DE
11434 &objfile->static_psymbols,
11435 0, (CORE_ADDR) 0, cu->language, objfile);
dee91e82 11436 info_ptr = locate_pdi_sibling (reader, part_die, info_ptr);
72bf9492
DJ
11437 continue;
11438 }
11439
d8228535
JK
11440 /* The exception for DW_TAG_typedef with has_children above is
11441 a workaround of GCC PR debug/47510. In the case of this complaint
11442 type_name_no_tag_or_error will error on such types later.
11443
11444 GDB skipped children of DW_TAG_typedef by the shortcut above and then
11445 it could not find the child DIEs referenced later, this is checked
11446 above. In correct DWARF DW_TAG_typedef should have no children. */
11447
11448 if (part_die->tag == DW_TAG_typedef && part_die->has_children)
11449 complaint (&symfile_complaints,
11450 _("DW_TAG_typedef has childen - GCC PR debug/47510 bug "
11451 "- DIE at 0x%x [in module %s]"),
b64f50a1 11452 part_die->offset.sect_off, objfile->name);
d8228535 11453
72bf9492
DJ
11454 /* If we're at the second level, and we're an enumerator, and
11455 our parent has no specification (meaning possibly lives in a
11456 namespace elsewhere), then we can add the partial symbol now
11457 instead of queueing it. */
11458 if (part_die->tag == DW_TAG_enumerator
11459 && parent_die != NULL
11460 && parent_die->die_parent == NULL
11461 && parent_die->tag == DW_TAG_enumeration_type
11462 && parent_die->has_specification == 0)
11463 {
11464 if (part_die->name == NULL)
3e43a32a
MS
11465 complaint (&symfile_complaints,
11466 _("malformed enumerator DIE ignored"));
72bf9492 11467 else if (building_psymtab)
04a679b8 11468 add_psymbol_to_list (part_die->name, strlen (part_die->name), 0,
72bf9492 11469 VAR_DOMAIN, LOC_CONST,
987504bb
JJ
11470 (cu->language == language_cplus
11471 || cu->language == language_java)
bb5ed363
DE
11472 ? &objfile->global_psymbols
11473 : &objfile->static_psymbols,
11474 0, (CORE_ADDR) 0, cu->language, objfile);
72bf9492 11475
dee91e82 11476 info_ptr = locate_pdi_sibling (reader, part_die, info_ptr);
72bf9492
DJ
11477 continue;
11478 }
11479
11480 /* We'll save this DIE so link it in. */
11481 part_die->die_parent = parent_die;
11482 part_die->die_sibling = NULL;
11483 part_die->die_child = NULL;
11484
11485 if (last_die && last_die == parent_die)
11486 last_die->die_child = part_die;
11487 else if (last_die)
11488 last_die->die_sibling = part_die;
11489
11490 last_die = part_die;
11491
11492 if (first_die == NULL)
11493 first_die = part_die;
11494
11495 /* Maybe add the DIE to the hash table. Not all DIEs that we
11496 find interesting need to be in the hash table, because we
11497 also have the parent/sibling/child chains; only those that we
11498 might refer to by offset later during partial symbol reading.
11499
11500 For now this means things that might have be the target of a
11501 DW_AT_specification, DW_AT_abstract_origin, or
11502 DW_AT_extension. DW_AT_extension will refer only to
11503 namespaces; DW_AT_abstract_origin refers to functions (and
11504 many things under the function DIE, but we do not recurse
11505 into function DIEs during partial symbol reading) and
11506 possibly variables as well; DW_AT_specification refers to
11507 declarations. Declarations ought to have the DW_AT_declaration
11508 flag. It happens that GCC forgets to put it in sometimes, but
11509 only for functions, not for types.
11510
11511 Adding more things than necessary to the hash table is harmless
11512 except for the performance cost. Adding too few will result in
5afb4e99
DJ
11513 wasted time in find_partial_die, when we reread the compilation
11514 unit with load_all_dies set. */
72bf9492 11515
5afb4e99 11516 if (load_all
72929c62 11517 || abbrev->tag == DW_TAG_constant
5afb4e99 11518 || abbrev->tag == DW_TAG_subprogram
72bf9492
DJ
11519 || abbrev->tag == DW_TAG_variable
11520 || abbrev->tag == DW_TAG_namespace
11521 || part_die->is_declaration)
11522 {
11523 void **slot;
11524
11525 slot = htab_find_slot_with_hash (cu->partial_dies, part_die,
b64f50a1 11526 part_die->offset.sect_off, INSERT);
72bf9492
DJ
11527 *slot = part_die;
11528 }
11529
11530 part_die = obstack_alloc (&cu->comp_unit_obstack,
11531 sizeof (struct partial_die_info));
11532
11533 /* For some DIEs we want to follow their children (if any). For C
bc30ff58 11534 we have no reason to follow the children of structures; for other
98bfdba5
PA
11535 languages we have to, so that we can get at method physnames
11536 to infer fully qualified class names, for DW_AT_specification,
11537 and for C++ template arguments. For C++, we also look one level
11538 inside functions to find template arguments (if the name of the
11539 function does not already contain the template arguments).
bc30ff58
JB
11540
11541 For Ada, we need to scan the children of subprograms and lexical
11542 blocks as well because Ada allows the definition of nested
11543 entities that could be interesting for the debugger, such as
11544 nested subprograms for instance. */
72bf9492 11545 if (last_die->has_children
5afb4e99
DJ
11546 && (load_all
11547 || last_die->tag == DW_TAG_namespace
f55ee35c 11548 || last_die->tag == DW_TAG_module
72bf9492 11549 || last_die->tag == DW_TAG_enumeration_type
98bfdba5
PA
11550 || (cu->language == language_cplus
11551 && last_die->tag == DW_TAG_subprogram
11552 && (last_die->name == NULL
11553 || strchr (last_die->name, '<') == NULL))
72bf9492
DJ
11554 || (cu->language != language_c
11555 && (last_die->tag == DW_TAG_class_type
680b30c7 11556 || last_die->tag == DW_TAG_interface_type
72bf9492 11557 || last_die->tag == DW_TAG_structure_type
bc30ff58
JB
11558 || last_die->tag == DW_TAG_union_type))
11559 || (cu->language == language_ada
11560 && (last_die->tag == DW_TAG_subprogram
11561 || last_die->tag == DW_TAG_lexical_block))))
72bf9492
DJ
11562 {
11563 nesting_level++;
11564 parent_die = last_die;
11565 continue;
11566 }
11567
11568 /* Otherwise we skip to the next sibling, if any. */
dee91e82 11569 info_ptr = locate_pdi_sibling (reader, last_die, info_ptr);
72bf9492
DJ
11570
11571 /* Back to the top, do it again. */
11572 }
11573}
11574
c906108c
SS
11575/* Read a minimal amount of information into the minimal die structure. */
11576
fe1b8b76 11577static gdb_byte *
dee91e82
DE
11578read_partial_die (const struct die_reader_specs *reader,
11579 struct partial_die_info *part_die,
11580 struct abbrev_info *abbrev, unsigned int abbrev_len,
11581 gdb_byte *info_ptr)
c906108c 11582{
dee91e82 11583 struct dwarf2_cu *cu = reader->cu;
bb5ed363 11584 struct objfile *objfile = cu->objfile;
dee91e82 11585 gdb_byte *buffer = reader->buffer;
fa238c03 11586 unsigned int i;
c906108c 11587 struct attribute attr;
c5aa993b 11588 int has_low_pc_attr = 0;
c906108c 11589 int has_high_pc_attr = 0;
91da1414 11590 int high_pc_relative = 0;
c906108c 11591
72bf9492 11592 memset (part_die, 0, sizeof (struct partial_die_info));
c906108c 11593
b64f50a1 11594 part_die->offset.sect_off = info_ptr - buffer;
72bf9492
DJ
11595
11596 info_ptr += abbrev_len;
11597
11598 if (abbrev == NULL)
11599 return info_ptr;
11600
c906108c
SS
11601 part_die->tag = abbrev->tag;
11602 part_die->has_children = abbrev->has_children;
c906108c
SS
11603
11604 for (i = 0; i < abbrev->num_attrs; ++i)
11605 {
dee91e82 11606 info_ptr = read_attribute (reader, &attr, &abbrev->attrs[i], info_ptr);
c906108c
SS
11607
11608 /* Store the data if it is of an attribute we want to keep in a
c5aa993b 11609 partial symbol table. */
c906108c
SS
11610 switch (attr.name)
11611 {
11612 case DW_AT_name:
71c25dea
TT
11613 switch (part_die->tag)
11614 {
11615 case DW_TAG_compile_unit:
95554aad 11616 case DW_TAG_partial_unit:
348e048f 11617 case DW_TAG_type_unit:
71c25dea
TT
11618 /* Compilation units have a DW_AT_name that is a filename, not
11619 a source language identifier. */
11620 case DW_TAG_enumeration_type:
11621 case DW_TAG_enumerator:
11622 /* These tags always have simple identifiers already; no need
11623 to canonicalize them. */
11624 part_die->name = DW_STRING (&attr);
11625 break;
11626 default:
11627 part_die->name
11628 = dwarf2_canonicalize_name (DW_STRING (&attr), cu,
bb5ed363 11629 &objfile->objfile_obstack);
71c25dea
TT
11630 break;
11631 }
c906108c 11632 break;
31ef98ae 11633 case DW_AT_linkage_name:
c906108c 11634 case DW_AT_MIPS_linkage_name:
31ef98ae
TT
11635 /* Note that both forms of linkage name might appear. We
11636 assume they will be the same, and we only store the last
11637 one we see. */
94af9270
KS
11638 if (cu->language == language_ada)
11639 part_die->name = DW_STRING (&attr);
abc72ce4 11640 part_die->linkage_name = DW_STRING (&attr);
c906108c
SS
11641 break;
11642 case DW_AT_low_pc:
11643 has_low_pc_attr = 1;
11644 part_die->lowpc = DW_ADDR (&attr);
11645 break;
11646 case DW_AT_high_pc:
11647 has_high_pc_attr = 1;
3019eac3
DE
11648 if (attr.form == DW_FORM_addr
11649 || attr.form == DW_FORM_GNU_addr_index)
91da1414
MW
11650 part_die->highpc = DW_ADDR (&attr);
11651 else
11652 {
11653 high_pc_relative = 1;
11654 part_die->highpc = DW_UNSND (&attr);
11655 }
c906108c
SS
11656 break;
11657 case DW_AT_location:
0963b4bd 11658 /* Support the .debug_loc offsets. */
8e19ed76
PS
11659 if (attr_form_is_block (&attr))
11660 {
95554aad 11661 part_die->d.locdesc = DW_BLOCK (&attr);
8e19ed76 11662 }
3690dd37 11663 else if (attr_form_is_section_offset (&attr))
8e19ed76 11664 {
4d3c2250 11665 dwarf2_complex_location_expr_complaint ();
8e19ed76
PS
11666 }
11667 else
11668 {
4d3c2250
KB
11669 dwarf2_invalid_attrib_class_complaint ("DW_AT_location",
11670 "partial symbol information");
8e19ed76 11671 }
c906108c 11672 break;
c906108c
SS
11673 case DW_AT_external:
11674 part_die->is_external = DW_UNSND (&attr);
11675 break;
11676 case DW_AT_declaration:
11677 part_die->is_declaration = DW_UNSND (&attr);
11678 break;
11679 case DW_AT_type:
11680 part_die->has_type = 1;
11681 break;
11682 case DW_AT_abstract_origin:
11683 case DW_AT_specification:
72bf9492
DJ
11684 case DW_AT_extension:
11685 part_die->has_specification = 1;
c764a876 11686 part_die->spec_offset = dwarf2_get_ref_die_offset (&attr);
c906108c
SS
11687 break;
11688 case DW_AT_sibling:
11689 /* Ignore absolute siblings, they might point outside of
11690 the current compile unit. */
11691 if (attr.form == DW_FORM_ref_addr)
3e43a32a
MS
11692 complaint (&symfile_complaints,
11693 _("ignoring absolute DW_AT_sibling"));
c906108c 11694 else
b64f50a1 11695 part_die->sibling = buffer + dwarf2_get_ref_die_offset (&attr).sect_off;
c906108c 11696 break;
fa4028e9
JB
11697 case DW_AT_byte_size:
11698 part_die->has_byte_size = 1;
11699 break;
68511cec
CES
11700 case DW_AT_calling_convention:
11701 /* DWARF doesn't provide a way to identify a program's source-level
11702 entry point. DW_AT_calling_convention attributes are only meant
11703 to describe functions' calling conventions.
11704
11705 However, because it's a necessary piece of information in
11706 Fortran, and because DW_CC_program is the only piece of debugging
11707 information whose definition refers to a 'main program' at all,
11708 several compilers have begun marking Fortran main programs with
11709 DW_CC_program --- even when those functions use the standard
11710 calling conventions.
11711
11712 So until DWARF specifies a way to provide this information and
11713 compilers pick up the new representation, we'll support this
11714 practice. */
11715 if (DW_UNSND (&attr) == DW_CC_program
11716 && cu->language == language_fortran)
01f8c46d
JK
11717 {
11718 set_main_name (part_die->name);
11719
11720 /* As this DIE has a static linkage the name would be difficult
11721 to look up later. */
11722 language_of_main = language_fortran;
11723 }
68511cec 11724 break;
481860b3
GB
11725 case DW_AT_inline:
11726 if (DW_UNSND (&attr) == DW_INL_inlined
11727 || DW_UNSND (&attr) == DW_INL_declared_inlined)
11728 part_die->may_be_inlined = 1;
11729 break;
95554aad
TT
11730
11731 case DW_AT_import:
11732 if (part_die->tag == DW_TAG_imported_unit)
11733 part_die->d.offset = dwarf2_get_ref_die_offset (&attr);
11734 break;
11735
c906108c
SS
11736 default:
11737 break;
11738 }
11739 }
11740
91da1414
MW
11741 if (high_pc_relative)
11742 part_die->highpc += part_die->lowpc;
11743
9373cf26
JK
11744 if (has_low_pc_attr && has_high_pc_attr)
11745 {
11746 /* When using the GNU linker, .gnu.linkonce. sections are used to
11747 eliminate duplicate copies of functions and vtables and such.
11748 The linker will arbitrarily choose one and discard the others.
11749 The AT_*_pc values for such functions refer to local labels in
11750 these sections. If the section from that file was discarded, the
11751 labels are not in the output, so the relocs get a value of 0.
11752 If this is a discarded function, mark the pc bounds as invalid,
11753 so that GDB will ignore it. */
11754 if (part_die->lowpc == 0 && !dwarf2_per_objfile->has_section_at_zero)
11755 {
bb5ed363 11756 struct gdbarch *gdbarch = get_objfile_arch (objfile);
9373cf26
JK
11757
11758 complaint (&symfile_complaints,
11759 _("DW_AT_low_pc %s is zero "
11760 "for DIE at 0x%x [in module %s]"),
11761 paddress (gdbarch, part_die->lowpc),
b64f50a1 11762 part_die->offset.sect_off, objfile->name);
9373cf26
JK
11763 }
11764 /* dwarf2_get_pc_bounds has also the strict low < high requirement. */
11765 else if (part_die->lowpc >= part_die->highpc)
11766 {
bb5ed363 11767 struct gdbarch *gdbarch = get_objfile_arch (objfile);
9373cf26
JK
11768
11769 complaint (&symfile_complaints,
11770 _("DW_AT_low_pc %s is not < DW_AT_high_pc %s "
11771 "for DIE at 0x%x [in module %s]"),
11772 paddress (gdbarch, part_die->lowpc),
11773 paddress (gdbarch, part_die->highpc),
b64f50a1 11774 part_die->offset.sect_off, objfile->name);
9373cf26
JK
11775 }
11776 else
11777 part_die->has_pc_info = 1;
11778 }
85cbf3d3 11779
c906108c
SS
11780 return info_ptr;
11781}
11782
72bf9492
DJ
11783/* Find a cached partial DIE at OFFSET in CU. */
11784
11785static struct partial_die_info *
b64f50a1 11786find_partial_die_in_comp_unit (sect_offset offset, struct dwarf2_cu *cu)
72bf9492
DJ
11787{
11788 struct partial_die_info *lookup_die = NULL;
11789 struct partial_die_info part_die;
11790
11791 part_die.offset = offset;
b64f50a1
JK
11792 lookup_die = htab_find_with_hash (cu->partial_dies, &part_die,
11793 offset.sect_off);
72bf9492 11794
72bf9492
DJ
11795 return lookup_die;
11796}
11797
348e048f
DE
11798/* Find a partial DIE at OFFSET, which may or may not be in CU,
11799 except in the case of .debug_types DIEs which do not reference
11800 outside their CU (they do however referencing other types via
55f1336d 11801 DW_FORM_ref_sig8). */
72bf9492
DJ
11802
11803static struct partial_die_info *
b64f50a1 11804find_partial_die (sect_offset offset, struct dwarf2_cu *cu)
72bf9492 11805{
bb5ed363 11806 struct objfile *objfile = cu->objfile;
5afb4e99
DJ
11807 struct dwarf2_per_cu_data *per_cu = NULL;
11808 struct partial_die_info *pd = NULL;
72bf9492 11809
45452591 11810 if (offset_in_cu_p (&cu->header, offset))
5afb4e99
DJ
11811 {
11812 pd = find_partial_die_in_comp_unit (offset, cu);
11813 if (pd != NULL)
11814 return pd;
0d99eb77
DE
11815 /* We missed recording what we needed.
11816 Load all dies and try again. */
11817 per_cu = cu->per_cu;
5afb4e99 11818 }
0d99eb77
DE
11819 else
11820 {
11821 /* TUs don't reference other CUs/TUs (except via type signatures). */
3019eac3 11822 if (cu->per_cu->is_debug_types)
0d99eb77
DE
11823 {
11824 error (_("Dwarf Error: Type Unit at offset 0x%lx contains"
11825 " external reference to offset 0x%lx [in module %s].\n"),
11826 (long) cu->header.offset.sect_off, (long) offset.sect_off,
11827 bfd_get_filename (objfile->obfd));
11828 }
11829 per_cu = dwarf2_find_containing_comp_unit (offset, objfile);
72bf9492 11830
0d99eb77
DE
11831 if (per_cu->cu == NULL || per_cu->cu->partial_dies == NULL)
11832 load_partial_comp_unit (per_cu);
ae038cb0 11833
0d99eb77
DE
11834 per_cu->cu->last_used = 0;
11835 pd = find_partial_die_in_comp_unit (offset, per_cu->cu);
11836 }
5afb4e99 11837
dee91e82
DE
11838 /* If we didn't find it, and not all dies have been loaded,
11839 load them all and try again. */
11840
5afb4e99
DJ
11841 if (pd == NULL && per_cu->load_all_dies == 0)
11842 {
5afb4e99 11843 per_cu->load_all_dies = 1;
fd820528
DE
11844
11845 /* This is nasty. When we reread the DIEs, somewhere up the call chain
11846 THIS_CU->cu may already be in use. So we can't just free it and
11847 replace its DIEs with the ones we read in. Instead, we leave those
11848 DIEs alone (which can still be in use, e.g. in scan_partial_symbols),
11849 and clobber THIS_CU->cu->partial_dies with the hash table for the new
11850 set. */
dee91e82 11851 load_partial_comp_unit (per_cu);
5afb4e99
DJ
11852
11853 pd = find_partial_die_in_comp_unit (offset, per_cu->cu);
11854 }
11855
11856 if (pd == NULL)
11857 internal_error (__FILE__, __LINE__,
3e43a32a
MS
11858 _("could not find partial DIE 0x%x "
11859 "in cache [from module %s]\n"),
b64f50a1 11860 offset.sect_off, bfd_get_filename (objfile->obfd));
5afb4e99 11861 return pd;
72bf9492
DJ
11862}
11863
abc72ce4
DE
11864/* See if we can figure out if the class lives in a namespace. We do
11865 this by looking for a member function; its demangled name will
11866 contain namespace info, if there is any. */
11867
11868static void
11869guess_partial_die_structure_name (struct partial_die_info *struct_pdi,
11870 struct dwarf2_cu *cu)
11871{
11872 /* NOTE: carlton/2003-10-07: Getting the info this way changes
11873 what template types look like, because the demangler
11874 frequently doesn't give the same name as the debug info. We
11875 could fix this by only using the demangled name to get the
11876 prefix (but see comment in read_structure_type). */
11877
11878 struct partial_die_info *real_pdi;
11879 struct partial_die_info *child_pdi;
11880
11881 /* If this DIE (this DIE's specification, if any) has a parent, then
11882 we should not do this. We'll prepend the parent's fully qualified
11883 name when we create the partial symbol. */
11884
11885 real_pdi = struct_pdi;
11886 while (real_pdi->has_specification)
11887 real_pdi = find_partial_die (real_pdi->spec_offset, cu);
11888
11889 if (real_pdi->die_parent != NULL)
11890 return;
11891
11892 for (child_pdi = struct_pdi->die_child;
11893 child_pdi != NULL;
11894 child_pdi = child_pdi->die_sibling)
11895 {
11896 if (child_pdi->tag == DW_TAG_subprogram
11897 && child_pdi->linkage_name != NULL)
11898 {
11899 char *actual_class_name
11900 = language_class_name_from_physname (cu->language_defn,
11901 child_pdi->linkage_name);
11902 if (actual_class_name != NULL)
11903 {
11904 struct_pdi->name
11905 = obsavestring (actual_class_name,
11906 strlen (actual_class_name),
11907 &cu->objfile->objfile_obstack);
11908 xfree (actual_class_name);
11909 }
11910 break;
11911 }
11912 }
11913}
11914
72bf9492
DJ
11915/* Adjust PART_DIE before generating a symbol for it. This function
11916 may set the is_external flag or change the DIE's name. */
11917
11918static void
11919fixup_partial_die (struct partial_die_info *part_die,
11920 struct dwarf2_cu *cu)
11921{
abc72ce4
DE
11922 /* Once we've fixed up a die, there's no point in doing so again.
11923 This also avoids a memory leak if we were to call
11924 guess_partial_die_structure_name multiple times. */
11925 if (part_die->fixup_called)
11926 return;
11927
72bf9492
DJ
11928 /* If we found a reference attribute and the DIE has no name, try
11929 to find a name in the referred to DIE. */
11930
11931 if (part_die->name == NULL && part_die->has_specification)
11932 {
11933 struct partial_die_info *spec_die;
72bf9492 11934
10b3939b 11935 spec_die = find_partial_die (part_die->spec_offset, cu);
72bf9492 11936
10b3939b 11937 fixup_partial_die (spec_die, cu);
72bf9492
DJ
11938
11939 if (spec_die->name)
11940 {
11941 part_die->name = spec_die->name;
11942
11943 /* Copy DW_AT_external attribute if it is set. */
11944 if (spec_die->is_external)
11945 part_die->is_external = spec_die->is_external;
11946 }
11947 }
11948
11949 /* Set default names for some unnamed DIEs. */
72bf9492
DJ
11950
11951 if (part_die->name == NULL && part_die->tag == DW_TAG_namespace)
2b1dbab0 11952 part_die->name = CP_ANONYMOUS_NAMESPACE_STR;
72bf9492 11953
abc72ce4
DE
11954 /* If there is no parent die to provide a namespace, and there are
11955 children, see if we can determine the namespace from their linkage
122d1940 11956 name. */
abc72ce4 11957 if (cu->language == language_cplus
8b70b953 11958 && !VEC_empty (dwarf2_section_info_def, dwarf2_per_objfile->types)
abc72ce4
DE
11959 && part_die->die_parent == NULL
11960 && part_die->has_children
11961 && (part_die->tag == DW_TAG_class_type
11962 || part_die->tag == DW_TAG_structure_type
11963 || part_die->tag == DW_TAG_union_type))
11964 guess_partial_die_structure_name (part_die, cu);
11965
53832f31
TT
11966 /* GCC might emit a nameless struct or union that has a linkage
11967 name. See http://gcc.gnu.org/bugzilla/show_bug.cgi?id=47510. */
11968 if (part_die->name == NULL
96408a79
SA
11969 && (part_die->tag == DW_TAG_class_type
11970 || part_die->tag == DW_TAG_interface_type
11971 || part_die->tag == DW_TAG_structure_type
11972 || part_die->tag == DW_TAG_union_type)
53832f31
TT
11973 && part_die->linkage_name != NULL)
11974 {
11975 char *demangled;
11976
11977 demangled = cplus_demangle (part_die->linkage_name, DMGL_TYPES);
11978 if (demangled)
11979 {
96408a79
SA
11980 const char *base;
11981
11982 /* Strip any leading namespaces/classes, keep only the base name.
11983 DW_AT_name for named DIEs does not contain the prefixes. */
11984 base = strrchr (demangled, ':');
11985 if (base && base > demangled && base[-1] == ':')
11986 base++;
11987 else
11988 base = demangled;
11989
11990 part_die->name = obsavestring (base, strlen (base),
53832f31
TT
11991 &cu->objfile->objfile_obstack);
11992 xfree (demangled);
11993 }
11994 }
11995
abc72ce4 11996 part_die->fixup_called = 1;
72bf9492
DJ
11997}
11998
a8329558 11999/* Read an attribute value described by an attribute form. */
c906108c 12000
fe1b8b76 12001static gdb_byte *
dee91e82
DE
12002read_attribute_value (const struct die_reader_specs *reader,
12003 struct attribute *attr, unsigned form,
12004 gdb_byte *info_ptr)
c906108c 12005{
dee91e82
DE
12006 struct dwarf2_cu *cu = reader->cu;
12007 bfd *abfd = reader->abfd;
e7c27a73 12008 struct comp_unit_head *cu_header = &cu->header;
c906108c
SS
12009 unsigned int bytes_read;
12010 struct dwarf_block *blk;
12011
a8329558
KW
12012 attr->form = form;
12013 switch (form)
c906108c 12014 {
c906108c 12015 case DW_FORM_ref_addr:
ae411497 12016 if (cu->header.version == 2)
4568ecf9 12017 DW_UNSND (attr) = read_address (abfd, info_ptr, cu, &bytes_read);
ae411497 12018 else
4568ecf9
DE
12019 DW_UNSND (attr) = read_offset (abfd, info_ptr,
12020 &cu->header, &bytes_read);
ae411497
TT
12021 info_ptr += bytes_read;
12022 break;
12023 case DW_FORM_addr:
e7c27a73 12024 DW_ADDR (attr) = read_address (abfd, info_ptr, cu, &bytes_read);
107d2387 12025 info_ptr += bytes_read;
c906108c
SS
12026 break;
12027 case DW_FORM_block2:
7b5a2f43 12028 blk = dwarf_alloc_block (cu);
c906108c
SS
12029 blk->size = read_2_bytes (abfd, info_ptr);
12030 info_ptr += 2;
12031 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
12032 info_ptr += blk->size;
12033 DW_BLOCK (attr) = blk;
12034 break;
12035 case DW_FORM_block4:
7b5a2f43 12036 blk = dwarf_alloc_block (cu);
c906108c
SS
12037 blk->size = read_4_bytes (abfd, info_ptr);
12038 info_ptr += 4;
12039 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
12040 info_ptr += blk->size;
12041 DW_BLOCK (attr) = blk;
12042 break;
12043 case DW_FORM_data2:
12044 DW_UNSND (attr) = read_2_bytes (abfd, info_ptr);
12045 info_ptr += 2;
12046 break;
12047 case DW_FORM_data4:
12048 DW_UNSND (attr) = read_4_bytes (abfd, info_ptr);
12049 info_ptr += 4;
12050 break;
12051 case DW_FORM_data8:
12052 DW_UNSND (attr) = read_8_bytes (abfd, info_ptr);
12053 info_ptr += 8;
12054 break;
2dc7f7b3
TT
12055 case DW_FORM_sec_offset:
12056 DW_UNSND (attr) = read_offset (abfd, info_ptr, &cu->header, &bytes_read);
12057 info_ptr += bytes_read;
12058 break;
c906108c 12059 case DW_FORM_string:
9b1c24c8 12060 DW_STRING (attr) = read_direct_string (abfd, info_ptr, &bytes_read);
8285870a 12061 DW_STRING_IS_CANONICAL (attr) = 0;
c906108c
SS
12062 info_ptr += bytes_read;
12063 break;
4bdf3d34
JJ
12064 case DW_FORM_strp:
12065 DW_STRING (attr) = read_indirect_string (abfd, info_ptr, cu_header,
12066 &bytes_read);
8285870a 12067 DW_STRING_IS_CANONICAL (attr) = 0;
4bdf3d34
JJ
12068 info_ptr += bytes_read;
12069 break;
2dc7f7b3 12070 case DW_FORM_exprloc:
c906108c 12071 case DW_FORM_block:
7b5a2f43 12072 blk = dwarf_alloc_block (cu);
c906108c
SS
12073 blk->size = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
12074 info_ptr += bytes_read;
12075 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
12076 info_ptr += blk->size;
12077 DW_BLOCK (attr) = blk;
12078 break;
12079 case DW_FORM_block1:
7b5a2f43 12080 blk = dwarf_alloc_block (cu);
c906108c
SS
12081 blk->size = read_1_byte (abfd, info_ptr);
12082 info_ptr += 1;
12083 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
12084 info_ptr += blk->size;
12085 DW_BLOCK (attr) = blk;
12086 break;
12087 case DW_FORM_data1:
12088 DW_UNSND (attr) = read_1_byte (abfd, info_ptr);
12089 info_ptr += 1;
12090 break;
12091 case DW_FORM_flag:
12092 DW_UNSND (attr) = read_1_byte (abfd, info_ptr);
12093 info_ptr += 1;
12094 break;
2dc7f7b3
TT
12095 case DW_FORM_flag_present:
12096 DW_UNSND (attr) = 1;
12097 break;
c906108c
SS
12098 case DW_FORM_sdata:
12099 DW_SND (attr) = read_signed_leb128 (abfd, info_ptr, &bytes_read);
12100 info_ptr += bytes_read;
12101 break;
12102 case DW_FORM_udata:
12103 DW_UNSND (attr) = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
12104 info_ptr += bytes_read;
12105 break;
12106 case DW_FORM_ref1:
4568ecf9
DE
12107 DW_UNSND (attr) = (cu->header.offset.sect_off
12108 + read_1_byte (abfd, info_ptr));
c906108c
SS
12109 info_ptr += 1;
12110 break;
12111 case DW_FORM_ref2:
4568ecf9
DE
12112 DW_UNSND (attr) = (cu->header.offset.sect_off
12113 + read_2_bytes (abfd, info_ptr));
c906108c
SS
12114 info_ptr += 2;
12115 break;
12116 case DW_FORM_ref4:
4568ecf9
DE
12117 DW_UNSND (attr) = (cu->header.offset.sect_off
12118 + read_4_bytes (abfd, info_ptr));
c906108c
SS
12119 info_ptr += 4;
12120 break;
613e1657 12121 case DW_FORM_ref8:
4568ecf9
DE
12122 DW_UNSND (attr) = (cu->header.offset.sect_off
12123 + read_8_bytes (abfd, info_ptr));
613e1657
KB
12124 info_ptr += 8;
12125 break;
55f1336d 12126 case DW_FORM_ref_sig8:
348e048f
DE
12127 /* Convert the signature to something we can record in DW_UNSND
12128 for later lookup.
12129 NOTE: This is NULL if the type wasn't found. */
12130 DW_SIGNATURED_TYPE (attr) =
e319fa28 12131 lookup_signatured_type (read_8_bytes (abfd, info_ptr));
348e048f
DE
12132 info_ptr += 8;
12133 break;
c906108c 12134 case DW_FORM_ref_udata:
4568ecf9
DE
12135 DW_UNSND (attr) = (cu->header.offset.sect_off
12136 + read_unsigned_leb128 (abfd, info_ptr, &bytes_read));
c906108c
SS
12137 info_ptr += bytes_read;
12138 break;
c906108c 12139 case DW_FORM_indirect:
a8329558
KW
12140 form = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
12141 info_ptr += bytes_read;
dee91e82 12142 info_ptr = read_attribute_value (reader, attr, form, info_ptr);
a8329558 12143 break;
3019eac3
DE
12144 case DW_FORM_GNU_addr_index:
12145 if (reader->dwo_file == NULL)
12146 {
12147 /* For now flag a hard error.
12148 Later we can turn this into a complaint. */
12149 error (_("Dwarf Error: %s found in non-DWO CU [in module %s]"),
12150 dwarf_form_name (form),
12151 bfd_get_filename (abfd));
12152 }
12153 DW_ADDR (attr) = read_addr_index_from_leb128 (cu, info_ptr, &bytes_read);
12154 info_ptr += bytes_read;
12155 break;
12156 case DW_FORM_GNU_str_index:
12157 if (reader->dwo_file == NULL)
12158 {
12159 /* For now flag a hard error.
12160 Later we can turn this into a complaint if warranted. */
12161 error (_("Dwarf Error: %s found in non-DWO CU [in module %s]"),
12162 dwarf_form_name (form),
12163 bfd_get_filename (abfd));
12164 }
12165 {
12166 ULONGEST str_index =
12167 read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
12168
12169 DW_STRING (attr) = read_str_index (reader, cu, str_index);
12170 DW_STRING_IS_CANONICAL (attr) = 0;
12171 info_ptr += bytes_read;
12172 }
12173 break;
c906108c 12174 default:
8a3fe4f8 12175 error (_("Dwarf Error: Cannot handle %s in DWARF reader [in module %s]"),
659b0389
ML
12176 dwarf_form_name (form),
12177 bfd_get_filename (abfd));
c906108c 12178 }
28e94949
JB
12179
12180 /* We have seen instances where the compiler tried to emit a byte
12181 size attribute of -1 which ended up being encoded as an unsigned
12182 0xffffffff. Although 0xffffffff is technically a valid size value,
12183 an object of this size seems pretty unlikely so we can relatively
12184 safely treat these cases as if the size attribute was invalid and
12185 treat them as zero by default. */
12186 if (attr->name == DW_AT_byte_size
12187 && form == DW_FORM_data4
12188 && DW_UNSND (attr) >= 0xffffffff)
01c66ae6
JB
12189 {
12190 complaint
12191 (&symfile_complaints,
43bbcdc2
PH
12192 _("Suspicious DW_AT_byte_size value treated as zero instead of %s"),
12193 hex_string (DW_UNSND (attr)));
01c66ae6
JB
12194 DW_UNSND (attr) = 0;
12195 }
28e94949 12196
c906108c
SS
12197 return info_ptr;
12198}
12199
a8329558
KW
12200/* Read an attribute described by an abbreviated attribute. */
12201
fe1b8b76 12202static gdb_byte *
dee91e82
DE
12203read_attribute (const struct die_reader_specs *reader,
12204 struct attribute *attr, struct attr_abbrev *abbrev,
12205 gdb_byte *info_ptr)
a8329558
KW
12206{
12207 attr->name = abbrev->name;
dee91e82 12208 return read_attribute_value (reader, attr, abbrev->form, info_ptr);
a8329558
KW
12209}
12210
0963b4bd 12211/* Read dwarf information from a buffer. */
c906108c
SS
12212
12213static unsigned int
fe1b8b76 12214read_1_byte (bfd *abfd, gdb_byte *buf)
c906108c 12215{
fe1b8b76 12216 return bfd_get_8 (abfd, buf);
c906108c
SS
12217}
12218
12219static int
fe1b8b76 12220read_1_signed_byte (bfd *abfd, gdb_byte *buf)
c906108c 12221{
fe1b8b76 12222 return bfd_get_signed_8 (abfd, buf);
c906108c
SS
12223}
12224
12225static unsigned int
fe1b8b76 12226read_2_bytes (bfd *abfd, gdb_byte *buf)
c906108c 12227{
fe1b8b76 12228 return bfd_get_16 (abfd, buf);
c906108c
SS
12229}
12230
21ae7a4d
JK
12231static int
12232read_2_signed_bytes (bfd *abfd, gdb_byte *buf)
12233{
12234 return bfd_get_signed_16 (abfd, buf);
12235}
12236
c906108c 12237static unsigned int
fe1b8b76 12238read_4_bytes (bfd *abfd, gdb_byte *buf)
c906108c 12239{
fe1b8b76 12240 return bfd_get_32 (abfd, buf);
c906108c
SS
12241}
12242
21ae7a4d
JK
12243static int
12244read_4_signed_bytes (bfd *abfd, gdb_byte *buf)
12245{
12246 return bfd_get_signed_32 (abfd, buf);
12247}
12248
93311388 12249static ULONGEST
fe1b8b76 12250read_8_bytes (bfd *abfd, gdb_byte *buf)
c906108c 12251{
fe1b8b76 12252 return bfd_get_64 (abfd, buf);
c906108c
SS
12253}
12254
12255static CORE_ADDR
fe1b8b76 12256read_address (bfd *abfd, gdb_byte *buf, struct dwarf2_cu *cu,
891d2f0b 12257 unsigned int *bytes_read)
c906108c 12258{
e7c27a73 12259 struct comp_unit_head *cu_header = &cu->header;
c906108c
SS
12260 CORE_ADDR retval = 0;
12261
107d2387 12262 if (cu_header->signed_addr_p)
c906108c 12263 {
107d2387
AC
12264 switch (cu_header->addr_size)
12265 {
12266 case 2:
fe1b8b76 12267 retval = bfd_get_signed_16 (abfd, buf);
107d2387
AC
12268 break;
12269 case 4:
fe1b8b76 12270 retval = bfd_get_signed_32 (abfd, buf);
107d2387
AC
12271 break;
12272 case 8:
fe1b8b76 12273 retval = bfd_get_signed_64 (abfd, buf);
107d2387
AC
12274 break;
12275 default:
8e65ff28 12276 internal_error (__FILE__, __LINE__,
e2e0b3e5 12277 _("read_address: bad switch, signed [in module %s]"),
659b0389 12278 bfd_get_filename (abfd));
107d2387
AC
12279 }
12280 }
12281 else
12282 {
12283 switch (cu_header->addr_size)
12284 {
12285 case 2:
fe1b8b76 12286 retval = bfd_get_16 (abfd, buf);
107d2387
AC
12287 break;
12288 case 4:
fe1b8b76 12289 retval = bfd_get_32 (abfd, buf);
107d2387
AC
12290 break;
12291 case 8:
fe1b8b76 12292 retval = bfd_get_64 (abfd, buf);
107d2387
AC
12293 break;
12294 default:
8e65ff28 12295 internal_error (__FILE__, __LINE__,
a73c6dcd
MS
12296 _("read_address: bad switch, "
12297 "unsigned [in module %s]"),
659b0389 12298 bfd_get_filename (abfd));
107d2387 12299 }
c906108c 12300 }
64367e0a 12301
107d2387
AC
12302 *bytes_read = cu_header->addr_size;
12303 return retval;
c906108c
SS
12304}
12305
f7ef9339 12306/* Read the initial length from a section. The (draft) DWARF 3
613e1657
KB
12307 specification allows the initial length to take up either 4 bytes
12308 or 12 bytes. If the first 4 bytes are 0xffffffff, then the next 8
12309 bytes describe the length and all offsets will be 8 bytes in length
12310 instead of 4.
12311
f7ef9339
KB
12312 An older, non-standard 64-bit format is also handled by this
12313 function. The older format in question stores the initial length
12314 as an 8-byte quantity without an escape value. Lengths greater
12315 than 2^32 aren't very common which means that the initial 4 bytes
12316 is almost always zero. Since a length value of zero doesn't make
12317 sense for the 32-bit format, this initial zero can be considered to
12318 be an escape value which indicates the presence of the older 64-bit
12319 format. As written, the code can't detect (old format) lengths
917c78fc
MK
12320 greater than 4GB. If it becomes necessary to handle lengths
12321 somewhat larger than 4GB, we could allow other small values (such
12322 as the non-sensical values of 1, 2, and 3) to also be used as
12323 escape values indicating the presence of the old format.
f7ef9339 12324
917c78fc
MK
12325 The value returned via bytes_read should be used to increment the
12326 relevant pointer after calling read_initial_length().
c764a876 12327
613e1657
KB
12328 [ Note: read_initial_length() and read_offset() are based on the
12329 document entitled "DWARF Debugging Information Format", revision
f7ef9339 12330 3, draft 8, dated November 19, 2001. This document was obtained
613e1657
KB
12331 from:
12332
f7ef9339 12333 http://reality.sgiweb.org/davea/dwarf3-draft8-011125.pdf
6e70227d 12334
613e1657
KB
12335 This document is only a draft and is subject to change. (So beware.)
12336
f7ef9339 12337 Details regarding the older, non-standard 64-bit format were
917c78fc
MK
12338 determined empirically by examining 64-bit ELF files produced by
12339 the SGI toolchain on an IRIX 6.5 machine.
f7ef9339
KB
12340
12341 - Kevin, July 16, 2002
613e1657
KB
12342 ] */
12343
12344static LONGEST
c764a876 12345read_initial_length (bfd *abfd, gdb_byte *buf, unsigned int *bytes_read)
613e1657 12346{
fe1b8b76 12347 LONGEST length = bfd_get_32 (abfd, buf);
613e1657 12348
dd373385 12349 if (length == 0xffffffff)
613e1657 12350 {
fe1b8b76 12351 length = bfd_get_64 (abfd, buf + 4);
613e1657 12352 *bytes_read = 12;
613e1657 12353 }
dd373385 12354 else if (length == 0)
f7ef9339 12355 {
dd373385 12356 /* Handle the (non-standard) 64-bit DWARF2 format used by IRIX. */
fe1b8b76 12357 length = bfd_get_64 (abfd, buf);
f7ef9339 12358 *bytes_read = 8;
f7ef9339 12359 }
613e1657
KB
12360 else
12361 {
12362 *bytes_read = 4;
613e1657
KB
12363 }
12364
c764a876
DE
12365 return length;
12366}
dd373385 12367
c764a876
DE
12368/* Cover function for read_initial_length.
12369 Returns the length of the object at BUF, and stores the size of the
12370 initial length in *BYTES_READ and stores the size that offsets will be in
12371 *OFFSET_SIZE.
12372 If the initial length size is not equivalent to that specified in
12373 CU_HEADER then issue a complaint.
12374 This is useful when reading non-comp-unit headers. */
dd373385 12375
c764a876
DE
12376static LONGEST
12377read_checked_initial_length_and_offset (bfd *abfd, gdb_byte *buf,
12378 const struct comp_unit_head *cu_header,
12379 unsigned int *bytes_read,
12380 unsigned int *offset_size)
12381{
12382 LONGEST length = read_initial_length (abfd, buf, bytes_read);
12383
12384 gdb_assert (cu_header->initial_length_size == 4
12385 || cu_header->initial_length_size == 8
12386 || cu_header->initial_length_size == 12);
12387
12388 if (cu_header->initial_length_size != *bytes_read)
12389 complaint (&symfile_complaints,
12390 _("intermixed 32-bit and 64-bit DWARF sections"));
dd373385 12391
c764a876 12392 *offset_size = (*bytes_read == 4) ? 4 : 8;
dd373385 12393 return length;
613e1657
KB
12394}
12395
12396/* Read an offset from the data stream. The size of the offset is
917c78fc 12397 given by cu_header->offset_size. */
613e1657
KB
12398
12399static LONGEST
fe1b8b76 12400read_offset (bfd *abfd, gdb_byte *buf, const struct comp_unit_head *cu_header,
891d2f0b 12401 unsigned int *bytes_read)
c764a876
DE
12402{
12403 LONGEST offset = read_offset_1 (abfd, buf, cu_header->offset_size);
9a619af0 12404
c764a876
DE
12405 *bytes_read = cu_header->offset_size;
12406 return offset;
12407}
12408
12409/* Read an offset from the data stream. */
12410
12411static LONGEST
12412read_offset_1 (bfd *abfd, gdb_byte *buf, unsigned int offset_size)
613e1657
KB
12413{
12414 LONGEST retval = 0;
12415
c764a876 12416 switch (offset_size)
613e1657
KB
12417 {
12418 case 4:
fe1b8b76 12419 retval = bfd_get_32 (abfd, buf);
613e1657
KB
12420 break;
12421 case 8:
fe1b8b76 12422 retval = bfd_get_64 (abfd, buf);
613e1657
KB
12423 break;
12424 default:
8e65ff28 12425 internal_error (__FILE__, __LINE__,
c764a876 12426 _("read_offset_1: bad switch [in module %s]"),
659b0389 12427 bfd_get_filename (abfd));
613e1657
KB
12428 }
12429
917c78fc 12430 return retval;
613e1657
KB
12431}
12432
fe1b8b76
JB
12433static gdb_byte *
12434read_n_bytes (bfd *abfd, gdb_byte *buf, unsigned int size)
c906108c
SS
12435{
12436 /* If the size of a host char is 8 bits, we can return a pointer
12437 to the buffer, otherwise we have to copy the data to a buffer
12438 allocated on the temporary obstack. */
4bdf3d34 12439 gdb_assert (HOST_CHAR_BIT == 8);
c906108c 12440 return buf;
c906108c
SS
12441}
12442
12443static char *
9b1c24c8 12444read_direct_string (bfd *abfd, gdb_byte *buf, unsigned int *bytes_read_ptr)
c906108c
SS
12445{
12446 /* If the size of a host char is 8 bits, we can return a pointer
12447 to the string, otherwise we have to copy the string to a buffer
12448 allocated on the temporary obstack. */
4bdf3d34 12449 gdb_assert (HOST_CHAR_BIT == 8);
c906108c
SS
12450 if (*buf == '\0')
12451 {
12452 *bytes_read_ptr = 1;
12453 return NULL;
12454 }
fe1b8b76
JB
12455 *bytes_read_ptr = strlen ((char *) buf) + 1;
12456 return (char *) buf;
4bdf3d34
JJ
12457}
12458
12459static char *
cf2c3c16 12460read_indirect_string_at_offset (bfd *abfd, LONGEST str_offset)
4bdf3d34 12461{
be391dca 12462 dwarf2_read_section (dwarf2_per_objfile->objfile, &dwarf2_per_objfile->str);
dce234bc 12463 if (dwarf2_per_objfile->str.buffer == NULL)
cf2c3c16
TT
12464 error (_("DW_FORM_strp used without .debug_str section [in module %s]"),
12465 bfd_get_filename (abfd));
dce234bc 12466 if (str_offset >= dwarf2_per_objfile->str.size)
cf2c3c16
TT
12467 error (_("DW_FORM_strp pointing outside of "
12468 ".debug_str section [in module %s]"),
12469 bfd_get_filename (abfd));
4bdf3d34 12470 gdb_assert (HOST_CHAR_BIT == 8);
dce234bc 12471 if (dwarf2_per_objfile->str.buffer[str_offset] == '\0')
4bdf3d34 12472 return NULL;
dce234bc 12473 return (char *) (dwarf2_per_objfile->str.buffer + str_offset);
c906108c
SS
12474}
12475
cf2c3c16
TT
12476static char *
12477read_indirect_string (bfd *abfd, gdb_byte *buf,
12478 const struct comp_unit_head *cu_header,
12479 unsigned int *bytes_read_ptr)
12480{
12481 LONGEST str_offset = read_offset (abfd, buf, cu_header, bytes_read_ptr);
12482
12483 return read_indirect_string_at_offset (abfd, str_offset);
12484}
12485
12df843f 12486static ULONGEST
fe1b8b76 12487read_unsigned_leb128 (bfd *abfd, gdb_byte *buf, unsigned int *bytes_read_ptr)
c906108c 12488{
12df843f 12489 ULONGEST result;
ce5d95e1 12490 unsigned int num_read;
c906108c
SS
12491 int i, shift;
12492 unsigned char byte;
12493
12494 result = 0;
12495 shift = 0;
12496 num_read = 0;
12497 i = 0;
12498 while (1)
12499 {
fe1b8b76 12500 byte = bfd_get_8 (abfd, buf);
c906108c
SS
12501 buf++;
12502 num_read++;
12df843f 12503 result |= ((ULONGEST) (byte & 127) << shift);
c906108c
SS
12504 if ((byte & 128) == 0)
12505 {
12506 break;
12507 }
12508 shift += 7;
12509 }
12510 *bytes_read_ptr = num_read;
12511 return result;
12512}
12513
12df843f 12514static LONGEST
fe1b8b76 12515read_signed_leb128 (bfd *abfd, gdb_byte *buf, unsigned int *bytes_read_ptr)
c906108c 12516{
12df843f 12517 LONGEST result;
77e0b926 12518 int i, shift, num_read;
c906108c
SS
12519 unsigned char byte;
12520
12521 result = 0;
12522 shift = 0;
c906108c
SS
12523 num_read = 0;
12524 i = 0;
12525 while (1)
12526 {
fe1b8b76 12527 byte = bfd_get_8 (abfd, buf);
c906108c
SS
12528 buf++;
12529 num_read++;
12df843f 12530 result |= ((LONGEST) (byte & 127) << shift);
c906108c
SS
12531 shift += 7;
12532 if ((byte & 128) == 0)
12533 {
12534 break;
12535 }
12536 }
77e0b926 12537 if ((shift < 8 * sizeof (result)) && (byte & 0x40))
12df843f 12538 result |= -(((LONGEST) 1) << shift);
c906108c
SS
12539 *bytes_read_ptr = num_read;
12540 return result;
12541}
12542
3019eac3
DE
12543/* Given index ADDR_INDEX in .debug_addr, fetch the value.
12544 ADDR_BASE is the DW_AT_GNU_addr_base attribute or zero.
12545 ADDR_SIZE is the size of addresses from the CU header. */
12546
12547static CORE_ADDR
12548read_addr_index_1 (unsigned int addr_index, ULONGEST addr_base, int addr_size)
12549{
12550 struct objfile *objfile = dwarf2_per_objfile->objfile;
12551 bfd *abfd = objfile->obfd;
12552 const gdb_byte *info_ptr;
12553
12554 dwarf2_read_section (objfile, &dwarf2_per_objfile->addr);
12555 if (dwarf2_per_objfile->addr.buffer == NULL)
12556 error (_("DW_FORM_addr_index used without .debug_addr section [in module %s]"),
12557 objfile->name);
12558 if (addr_base + addr_index * addr_size >= dwarf2_per_objfile->addr.size)
12559 error (_("DW_FORM_addr_index pointing outside of "
12560 ".debug_addr section [in module %s]"),
12561 objfile->name);
12562 info_ptr = (dwarf2_per_objfile->addr.buffer
12563 + addr_base + addr_index * addr_size);
12564 if (addr_size == 4)
12565 return bfd_get_32 (abfd, info_ptr);
12566 else
12567 return bfd_get_64 (abfd, info_ptr);
12568}
12569
12570/* Given index ADDR_INDEX in .debug_addr, fetch the value. */
12571
12572static CORE_ADDR
12573read_addr_index (struct dwarf2_cu *cu, unsigned int addr_index)
12574{
12575 return read_addr_index_1 (addr_index, cu->addr_base, cu->header.addr_size);
12576}
12577
12578/* Given a pointer to an leb128 value, fetch the value from .debug_addr. */
12579
12580static CORE_ADDR
12581read_addr_index_from_leb128 (struct dwarf2_cu *cu, gdb_byte *info_ptr,
12582 unsigned int *bytes_read)
12583{
12584 bfd *abfd = cu->objfile->obfd;
12585 unsigned int addr_index = read_unsigned_leb128 (abfd, info_ptr, bytes_read);
12586
12587 return read_addr_index (cu, addr_index);
12588}
12589
12590/* Data structure to pass results from dwarf2_read_addr_index_reader
12591 back to dwarf2_read_addr_index. */
12592
12593struct dwarf2_read_addr_index_data
12594{
12595 ULONGEST addr_base;
12596 int addr_size;
12597};
12598
12599/* die_reader_func for dwarf2_read_addr_index. */
12600
12601static void
12602dwarf2_read_addr_index_reader (const struct die_reader_specs *reader,
12603 gdb_byte *info_ptr,
12604 struct die_info *comp_unit_die,
12605 int has_children,
12606 void *data)
12607{
12608 struct dwarf2_cu *cu = reader->cu;
12609 struct dwarf2_read_addr_index_data *aidata =
12610 (struct dwarf2_read_addr_index_data *) data;
12611
12612 aidata->addr_base = cu->addr_base;
12613 aidata->addr_size = cu->header.addr_size;
12614}
12615
12616/* Given an index in .debug_addr, fetch the value.
12617 NOTE: This can be called during dwarf expression evaluation,
12618 long after the debug information has been read, and thus per_cu->cu
12619 may no longer exist. */
12620
12621CORE_ADDR
12622dwarf2_read_addr_index (struct dwarf2_per_cu_data *per_cu,
12623 unsigned int addr_index)
12624{
12625 struct objfile *objfile = per_cu->objfile;
12626 struct dwarf2_cu *cu = per_cu->cu;
12627 ULONGEST addr_base;
12628 int addr_size;
12629
12630 /* This is intended to be called from outside this file. */
12631 dw2_setup (objfile);
12632
12633 /* We need addr_base and addr_size.
12634 If we don't have PER_CU->cu, we have to get it.
12635 Nasty, but the alternative is storing the needed info in PER_CU,
12636 which at this point doesn't seem justified: it's not clear how frequently
12637 it would get used and it would increase the size of every PER_CU.
12638 Entry points like dwarf2_per_cu_addr_size do a similar thing
12639 so we're not in uncharted territory here.
12640 Alas we need to be a bit more complicated as addr_base is contained
12641 in the DIE.
12642
12643 We don't need to read the entire CU(/TU).
12644 We just need the header and top level die.
12645 IWBN to use the aging mechanism to let us lazily later discard the CU.
12646 See however init_cutu_and_read_dies_simple. */
12647
12648 if (cu != NULL)
12649 {
12650 addr_base = cu->addr_base;
12651 addr_size = cu->header.addr_size;
12652 }
12653 else
12654 {
12655 struct dwarf2_read_addr_index_data aidata;
12656
12657 init_cutu_and_read_dies_simple (per_cu, dwarf2_read_addr_index_reader,
12658 &aidata);
12659 addr_base = aidata.addr_base;
12660 addr_size = aidata.addr_size;
12661 }
12662
12663 return read_addr_index_1 (addr_index, addr_base, addr_size);
12664}
12665
12666/* Given a DW_AT_str_index, fetch the string. */
12667
12668static char *
12669read_str_index (const struct die_reader_specs *reader,
12670 struct dwarf2_cu *cu, ULONGEST str_index)
12671{
12672 struct objfile *objfile = dwarf2_per_objfile->objfile;
12673 const char *dwo_name = objfile->name;
12674 bfd *abfd = objfile->obfd;
12675 struct dwo_sections *sections = &reader->dwo_file->sections;
12676 gdb_byte *info_ptr;
12677 ULONGEST str_offset;
12678
12679 dwarf2_read_section (objfile, &sections->str);
12680 dwarf2_read_section (objfile, &sections->str_offsets);
12681 if (sections->str.buffer == NULL)
12682 error (_("DW_FORM_str_index used without .debug_str.dwo section"
12683 " in CU at offset 0x%lx [in module %s]"),
12684 (long) cu->header.offset.sect_off, dwo_name);
12685 if (sections->str_offsets.buffer == NULL)
12686 error (_("DW_FORM_str_index used without .debug_str_offsets.dwo section"
12687 " in CU at offset 0x%lx [in module %s]"),
12688 (long) cu->header.offset.sect_off, dwo_name);
12689 if (str_index * cu->header.offset_size >= sections->str_offsets.size)
12690 error (_("DW_FORM_str_index pointing outside of .debug_str_offsets.dwo"
12691 " section in CU at offset 0x%lx [in module %s]"),
12692 (long) cu->header.offset.sect_off, dwo_name);
12693 info_ptr = (sections->str_offsets.buffer
12694 + str_index * cu->header.offset_size);
12695 if (cu->header.offset_size == 4)
12696 str_offset = bfd_get_32 (abfd, info_ptr);
12697 else
12698 str_offset = bfd_get_64 (abfd, info_ptr);
12699 if (str_offset >= sections->str.size)
12700 error (_("Offset from DW_FORM_str_index pointing outside of"
12701 " .debug_str.dwo section in CU at offset 0x%lx [in module %s]"),
12702 (long) cu->header.offset.sect_off, dwo_name);
12703 return (char *) (sections->str.buffer + str_offset);
12704}
12705
3019eac3
DE
12706/* Return the length of an LEB128 number in BUF. */
12707
12708static int
12709leb128_size (const gdb_byte *buf)
12710{
12711 const gdb_byte *begin = buf;
12712 gdb_byte byte;
12713
12714 while (1)
12715 {
12716 byte = *buf++;
12717 if ((byte & 128) == 0)
12718 return buf - begin;
12719 }
12720}
12721
c906108c 12722static void
e142c38c 12723set_cu_language (unsigned int lang, struct dwarf2_cu *cu)
c906108c
SS
12724{
12725 switch (lang)
12726 {
12727 case DW_LANG_C89:
76bee0cc 12728 case DW_LANG_C99:
c906108c 12729 case DW_LANG_C:
e142c38c 12730 cu->language = language_c;
c906108c
SS
12731 break;
12732 case DW_LANG_C_plus_plus:
e142c38c 12733 cu->language = language_cplus;
c906108c 12734 break;
6aecb9c2
JB
12735 case DW_LANG_D:
12736 cu->language = language_d;
12737 break;
c906108c
SS
12738 case DW_LANG_Fortran77:
12739 case DW_LANG_Fortran90:
b21b22e0 12740 case DW_LANG_Fortran95:
e142c38c 12741 cu->language = language_fortran;
c906108c 12742 break;
a766d390
DE
12743 case DW_LANG_Go:
12744 cu->language = language_go;
12745 break;
c906108c 12746 case DW_LANG_Mips_Assembler:
e142c38c 12747 cu->language = language_asm;
c906108c 12748 break;
bebd888e 12749 case DW_LANG_Java:
e142c38c 12750 cu->language = language_java;
bebd888e 12751 break;
c906108c 12752 case DW_LANG_Ada83:
8aaf0b47 12753 case DW_LANG_Ada95:
bc5f45f8
JB
12754 cu->language = language_ada;
12755 break;
72019c9c
GM
12756 case DW_LANG_Modula2:
12757 cu->language = language_m2;
12758 break;
fe8e67fd
PM
12759 case DW_LANG_Pascal83:
12760 cu->language = language_pascal;
12761 break;
22566fbd
DJ
12762 case DW_LANG_ObjC:
12763 cu->language = language_objc;
12764 break;
c906108c
SS
12765 case DW_LANG_Cobol74:
12766 case DW_LANG_Cobol85:
c906108c 12767 default:
e142c38c 12768 cu->language = language_minimal;
c906108c
SS
12769 break;
12770 }
e142c38c 12771 cu->language_defn = language_def (cu->language);
c906108c
SS
12772}
12773
12774/* Return the named attribute or NULL if not there. */
12775
12776static struct attribute *
e142c38c 12777dwarf2_attr (struct die_info *die, unsigned int name, struct dwarf2_cu *cu)
c906108c 12778{
a48e046c 12779 for (;;)
c906108c 12780 {
a48e046c
TT
12781 unsigned int i;
12782 struct attribute *spec = NULL;
12783
12784 for (i = 0; i < die->num_attrs; ++i)
12785 {
12786 if (die->attrs[i].name == name)
12787 return &die->attrs[i];
12788 if (die->attrs[i].name == DW_AT_specification
12789 || die->attrs[i].name == DW_AT_abstract_origin)
12790 spec = &die->attrs[i];
12791 }
12792
12793 if (!spec)
12794 break;
c906108c 12795
f2f0e013 12796 die = follow_die_ref (die, spec, &cu);
f2f0e013 12797 }
c5aa993b 12798
c906108c
SS
12799 return NULL;
12800}
12801
348e048f
DE
12802/* Return the named attribute or NULL if not there,
12803 but do not follow DW_AT_specification, etc.
12804 This is for use in contexts where we're reading .debug_types dies.
12805 Following DW_AT_specification, DW_AT_abstract_origin will take us
12806 back up the chain, and we want to go down. */
12807
12808static struct attribute *
45e58e77 12809dwarf2_attr_no_follow (struct die_info *die, unsigned int name)
348e048f
DE
12810{
12811 unsigned int i;
12812
12813 for (i = 0; i < die->num_attrs; ++i)
12814 if (die->attrs[i].name == name)
12815 return &die->attrs[i];
12816
12817 return NULL;
12818}
12819
05cf31d1
JB
12820/* Return non-zero iff the attribute NAME is defined for the given DIE,
12821 and holds a non-zero value. This function should only be used for
2dc7f7b3 12822 DW_FORM_flag or DW_FORM_flag_present attributes. */
05cf31d1
JB
12823
12824static int
12825dwarf2_flag_true_p (struct die_info *die, unsigned name, struct dwarf2_cu *cu)
12826{
12827 struct attribute *attr = dwarf2_attr (die, name, cu);
12828
12829 return (attr && DW_UNSND (attr));
12830}
12831
3ca72b44 12832static int
e142c38c 12833die_is_declaration (struct die_info *die, struct dwarf2_cu *cu)
3ca72b44 12834{
05cf31d1
JB
12835 /* A DIE is a declaration if it has a DW_AT_declaration attribute
12836 which value is non-zero. However, we have to be careful with
12837 DIEs having a DW_AT_specification attribute, because dwarf2_attr()
12838 (via dwarf2_flag_true_p) follows this attribute. So we may
12839 end up accidently finding a declaration attribute that belongs
12840 to a different DIE referenced by the specification attribute,
12841 even though the given DIE does not have a declaration attribute. */
12842 return (dwarf2_flag_true_p (die, DW_AT_declaration, cu)
12843 && dwarf2_attr (die, DW_AT_specification, cu) == NULL);
3ca72b44
AC
12844}
12845
63d06c5c 12846/* Return the die giving the specification for DIE, if there is
f2f0e013 12847 one. *SPEC_CU is the CU containing DIE on input, and the CU
edb3359d
DJ
12848 containing the return value on output. If there is no
12849 specification, but there is an abstract origin, that is
12850 returned. */
63d06c5c
DC
12851
12852static struct die_info *
f2f0e013 12853die_specification (struct die_info *die, struct dwarf2_cu **spec_cu)
63d06c5c 12854{
f2f0e013
DJ
12855 struct attribute *spec_attr = dwarf2_attr (die, DW_AT_specification,
12856 *spec_cu);
63d06c5c 12857
edb3359d
DJ
12858 if (spec_attr == NULL)
12859 spec_attr = dwarf2_attr (die, DW_AT_abstract_origin, *spec_cu);
12860
63d06c5c
DC
12861 if (spec_attr == NULL)
12862 return NULL;
12863 else
f2f0e013 12864 return follow_die_ref (die, spec_attr, spec_cu);
63d06c5c 12865}
c906108c 12866
debd256d 12867/* Free the line_header structure *LH, and any arrays and strings it
ae2de4f8
DE
12868 refers to.
12869 NOTE: This is also used as a "cleanup" function. */
12870
debd256d
JB
12871static void
12872free_line_header (struct line_header *lh)
12873{
12874 if (lh->standard_opcode_lengths)
a8bc7b56 12875 xfree (lh->standard_opcode_lengths);
debd256d
JB
12876
12877 /* Remember that all the lh->file_names[i].name pointers are
12878 pointers into debug_line_buffer, and don't need to be freed. */
12879 if (lh->file_names)
a8bc7b56 12880 xfree (lh->file_names);
debd256d
JB
12881
12882 /* Similarly for the include directory names. */
12883 if (lh->include_dirs)
a8bc7b56 12884 xfree (lh->include_dirs);
debd256d 12885
a8bc7b56 12886 xfree (lh);
debd256d
JB
12887}
12888
debd256d 12889/* Add an entry to LH's include directory table. */
ae2de4f8 12890
debd256d
JB
12891static void
12892add_include_dir (struct line_header *lh, char *include_dir)
c906108c 12893{
debd256d
JB
12894 /* Grow the array if necessary. */
12895 if (lh->include_dirs_size == 0)
c5aa993b 12896 {
debd256d
JB
12897 lh->include_dirs_size = 1; /* for testing */
12898 lh->include_dirs = xmalloc (lh->include_dirs_size
12899 * sizeof (*lh->include_dirs));
12900 }
12901 else if (lh->num_include_dirs >= lh->include_dirs_size)
12902 {
12903 lh->include_dirs_size *= 2;
12904 lh->include_dirs = xrealloc (lh->include_dirs,
12905 (lh->include_dirs_size
12906 * sizeof (*lh->include_dirs)));
c5aa993b 12907 }
c906108c 12908
debd256d
JB
12909 lh->include_dirs[lh->num_include_dirs++] = include_dir;
12910}
6e70227d 12911
debd256d 12912/* Add an entry to LH's file name table. */
ae2de4f8 12913
debd256d
JB
12914static void
12915add_file_name (struct line_header *lh,
12916 char *name,
12917 unsigned int dir_index,
12918 unsigned int mod_time,
12919 unsigned int length)
12920{
12921 struct file_entry *fe;
12922
12923 /* Grow the array if necessary. */
12924 if (lh->file_names_size == 0)
12925 {
12926 lh->file_names_size = 1; /* for testing */
12927 lh->file_names = xmalloc (lh->file_names_size
12928 * sizeof (*lh->file_names));
12929 }
12930 else if (lh->num_file_names >= lh->file_names_size)
12931 {
12932 lh->file_names_size *= 2;
12933 lh->file_names = xrealloc (lh->file_names,
12934 (lh->file_names_size
12935 * sizeof (*lh->file_names)));
12936 }
12937
12938 fe = &lh->file_names[lh->num_file_names++];
12939 fe->name = name;
12940 fe->dir_index = dir_index;
12941 fe->mod_time = mod_time;
12942 fe->length = length;
aaa75496 12943 fe->included_p = 0;
cb1df416 12944 fe->symtab = NULL;
debd256d 12945}
6e70227d 12946
debd256d 12947/* Read the statement program header starting at OFFSET in
3019eac3 12948 .debug_line, or .debug_line.dwo. Return a pointer
6502dd73 12949 to a struct line_header, allocated using xmalloc.
debd256d
JB
12950
12951 NOTE: the strings in the include directory and file name tables of
3019eac3
DE
12952 the returned object point into the dwarf line section buffer,
12953 and must not be freed. */
ae2de4f8 12954
debd256d 12955static struct line_header *
3019eac3 12956dwarf_decode_line_header (unsigned int offset, struct dwarf2_cu *cu)
debd256d
JB
12957{
12958 struct cleanup *back_to;
12959 struct line_header *lh;
fe1b8b76 12960 gdb_byte *line_ptr;
c764a876 12961 unsigned int bytes_read, offset_size;
debd256d
JB
12962 int i;
12963 char *cur_dir, *cur_file;
3019eac3
DE
12964 struct dwarf2_section_info *section;
12965 bfd *abfd;
12966
12967 /* For TUs in DWO files, the DW_AT_stmt_list attribute lives in the
12968 DWO file. */
12969 if (cu->dwo_unit && cu->per_cu->is_debug_types)
12970 section = &cu->dwo_unit->dwo_file->sections.line;
12971 else
12972 section = &dwarf2_per_objfile->line;
debd256d 12973
3019eac3
DE
12974 dwarf2_read_section (dwarf2_per_objfile->objfile, section);
12975 if (section->buffer == NULL)
debd256d 12976 {
3019eac3
DE
12977 if (cu->dwo_unit && cu->per_cu->is_debug_types)
12978 complaint (&symfile_complaints, _("missing .debug_line.dwo section"));
12979 else
12980 complaint (&symfile_complaints, _("missing .debug_line section"));
debd256d
JB
12981 return 0;
12982 }
12983
fceca515
DE
12984 /* We can't do this until we know the section is non-empty.
12985 Only then do we know we have such a section. */
12986 abfd = section->asection->owner;
12987
a738430d
MK
12988 /* Make sure that at least there's room for the total_length field.
12989 That could be 12 bytes long, but we're just going to fudge that. */
3019eac3 12990 if (offset + 4 >= section->size)
debd256d 12991 {
4d3c2250 12992 dwarf2_statement_list_fits_in_line_number_section_complaint ();
debd256d
JB
12993 return 0;
12994 }
12995
12996 lh = xmalloc (sizeof (*lh));
12997 memset (lh, 0, sizeof (*lh));
12998 back_to = make_cleanup ((make_cleanup_ftype *) free_line_header,
12999 (void *) lh);
13000
3019eac3 13001 line_ptr = section->buffer + offset;
debd256d 13002
a738430d 13003 /* Read in the header. */
6e70227d 13004 lh->total_length =
c764a876
DE
13005 read_checked_initial_length_and_offset (abfd, line_ptr, &cu->header,
13006 &bytes_read, &offset_size);
debd256d 13007 line_ptr += bytes_read;
3019eac3 13008 if (line_ptr + lh->total_length > (section->buffer + section->size))
debd256d 13009 {
4d3c2250 13010 dwarf2_statement_list_fits_in_line_number_section_complaint ();
debd256d
JB
13011 return 0;
13012 }
13013 lh->statement_program_end = line_ptr + lh->total_length;
13014 lh->version = read_2_bytes (abfd, line_ptr);
13015 line_ptr += 2;
c764a876
DE
13016 lh->header_length = read_offset_1 (abfd, line_ptr, offset_size);
13017 line_ptr += offset_size;
debd256d
JB
13018 lh->minimum_instruction_length = read_1_byte (abfd, line_ptr);
13019 line_ptr += 1;
2dc7f7b3
TT
13020 if (lh->version >= 4)
13021 {
13022 lh->maximum_ops_per_instruction = read_1_byte (abfd, line_ptr);
13023 line_ptr += 1;
13024 }
13025 else
13026 lh->maximum_ops_per_instruction = 1;
13027
13028 if (lh->maximum_ops_per_instruction == 0)
13029 {
13030 lh->maximum_ops_per_instruction = 1;
13031 complaint (&symfile_complaints,
3e43a32a
MS
13032 _("invalid maximum_ops_per_instruction "
13033 "in `.debug_line' section"));
2dc7f7b3
TT
13034 }
13035
debd256d
JB
13036 lh->default_is_stmt = read_1_byte (abfd, line_ptr);
13037 line_ptr += 1;
13038 lh->line_base = read_1_signed_byte (abfd, line_ptr);
13039 line_ptr += 1;
13040 lh->line_range = read_1_byte (abfd, line_ptr);
13041 line_ptr += 1;
13042 lh->opcode_base = read_1_byte (abfd, line_ptr);
13043 line_ptr += 1;
13044 lh->standard_opcode_lengths
fe1b8b76 13045 = xmalloc (lh->opcode_base * sizeof (lh->standard_opcode_lengths[0]));
debd256d
JB
13046
13047 lh->standard_opcode_lengths[0] = 1; /* This should never be used anyway. */
13048 for (i = 1; i < lh->opcode_base; ++i)
13049 {
13050 lh->standard_opcode_lengths[i] = read_1_byte (abfd, line_ptr);
13051 line_ptr += 1;
13052 }
13053
a738430d 13054 /* Read directory table. */
9b1c24c8 13055 while ((cur_dir = read_direct_string (abfd, line_ptr, &bytes_read)) != NULL)
debd256d
JB
13056 {
13057 line_ptr += bytes_read;
13058 add_include_dir (lh, cur_dir);
13059 }
13060 line_ptr += bytes_read;
13061
a738430d 13062 /* Read file name table. */
9b1c24c8 13063 while ((cur_file = read_direct_string (abfd, line_ptr, &bytes_read)) != NULL)
debd256d
JB
13064 {
13065 unsigned int dir_index, mod_time, length;
13066
13067 line_ptr += bytes_read;
13068 dir_index = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
13069 line_ptr += bytes_read;
13070 mod_time = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
13071 line_ptr += bytes_read;
13072 length = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
13073 line_ptr += bytes_read;
13074
13075 add_file_name (lh, cur_file, dir_index, mod_time, length);
13076 }
13077 line_ptr += bytes_read;
6e70227d 13078 lh->statement_program_start = line_ptr;
debd256d 13079
3019eac3 13080 if (line_ptr > (section->buffer + section->size))
4d3c2250 13081 complaint (&symfile_complaints,
3e43a32a
MS
13082 _("line number info header doesn't "
13083 "fit in `.debug_line' section"));
debd256d
JB
13084
13085 discard_cleanups (back_to);
13086 return lh;
13087}
c906108c 13088
c6da4cef
DE
13089/* Subroutine of dwarf_decode_lines to simplify it.
13090 Return the file name of the psymtab for included file FILE_INDEX
13091 in line header LH of PST.
13092 COMP_DIR is the compilation directory (DW_AT_comp_dir) or NULL if unknown.
13093 If space for the result is malloc'd, it will be freed by a cleanup.
13094 Returns NULL if FILE_INDEX should be ignored, i.e., it is pst->filename. */
13095
13096static char *
13097psymtab_include_file_name (const struct line_header *lh, int file_index,
13098 const struct partial_symtab *pst,
13099 const char *comp_dir)
13100{
13101 const struct file_entry fe = lh->file_names [file_index];
13102 char *include_name = fe.name;
13103 char *include_name_to_compare = include_name;
13104 char *dir_name = NULL;
72b9f47f
TT
13105 const char *pst_filename;
13106 char *copied_name = NULL;
c6da4cef
DE
13107 int file_is_pst;
13108
13109 if (fe.dir_index)
13110 dir_name = lh->include_dirs[fe.dir_index - 1];
13111
13112 if (!IS_ABSOLUTE_PATH (include_name)
13113 && (dir_name != NULL || comp_dir != NULL))
13114 {
13115 /* Avoid creating a duplicate psymtab for PST.
13116 We do this by comparing INCLUDE_NAME and PST_FILENAME.
13117 Before we do the comparison, however, we need to account
13118 for DIR_NAME and COMP_DIR.
13119 First prepend dir_name (if non-NULL). If we still don't
13120 have an absolute path prepend comp_dir (if non-NULL).
13121 However, the directory we record in the include-file's
13122 psymtab does not contain COMP_DIR (to match the
13123 corresponding symtab(s)).
13124
13125 Example:
13126
13127 bash$ cd /tmp
13128 bash$ gcc -g ./hello.c
13129 include_name = "hello.c"
13130 dir_name = "."
13131 DW_AT_comp_dir = comp_dir = "/tmp"
13132 DW_AT_name = "./hello.c" */
13133
13134 if (dir_name != NULL)
13135 {
13136 include_name = concat (dir_name, SLASH_STRING,
13137 include_name, (char *)NULL);
13138 include_name_to_compare = include_name;
13139 make_cleanup (xfree, include_name);
13140 }
13141 if (!IS_ABSOLUTE_PATH (include_name) && comp_dir != NULL)
13142 {
13143 include_name_to_compare = concat (comp_dir, SLASH_STRING,
13144 include_name, (char *)NULL);
13145 }
13146 }
13147
13148 pst_filename = pst->filename;
13149 if (!IS_ABSOLUTE_PATH (pst_filename) && pst->dirname != NULL)
13150 {
72b9f47f
TT
13151 copied_name = concat (pst->dirname, SLASH_STRING,
13152 pst_filename, (char *)NULL);
13153 pst_filename = copied_name;
c6da4cef
DE
13154 }
13155
1e3fad37 13156 file_is_pst = FILENAME_CMP (include_name_to_compare, pst_filename) == 0;
c6da4cef
DE
13157
13158 if (include_name_to_compare != include_name)
13159 xfree (include_name_to_compare);
72b9f47f
TT
13160 if (copied_name != NULL)
13161 xfree (copied_name);
c6da4cef
DE
13162
13163 if (file_is_pst)
13164 return NULL;
13165 return include_name;
13166}
13167
c91513d8
PP
13168/* Ignore this record_line request. */
13169
13170static void
13171noop_record_line (struct subfile *subfile, int line, CORE_ADDR pc)
13172{
13173 return;
13174}
13175
f3f5162e
DE
13176/* Subroutine of dwarf_decode_lines to simplify it.
13177 Process the line number information in LH. */
debd256d 13178
c906108c 13179static void
f3f5162e
DE
13180dwarf_decode_lines_1 (struct line_header *lh, const char *comp_dir,
13181 struct dwarf2_cu *cu, struct partial_symtab *pst)
c906108c 13182{
a8c50c1f 13183 gdb_byte *line_ptr, *extended_end;
fe1b8b76 13184 gdb_byte *line_end;
a8c50c1f 13185 unsigned int bytes_read, extended_len;
c906108c 13186 unsigned char op_code, extended_op, adj_opcode;
e142c38c
DJ
13187 CORE_ADDR baseaddr;
13188 struct objfile *objfile = cu->objfile;
f3f5162e 13189 bfd *abfd = objfile->obfd;
fbf65064 13190 struct gdbarch *gdbarch = get_objfile_arch (objfile);
aaa75496 13191 const int decode_for_pst_p = (pst != NULL);
f3f5162e 13192 struct subfile *last_subfile = NULL;
c91513d8
PP
13193 void (*p_record_line) (struct subfile *subfile, int line, CORE_ADDR pc)
13194 = record_line;
e142c38c
DJ
13195
13196 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
c906108c 13197
debd256d
JB
13198 line_ptr = lh->statement_program_start;
13199 line_end = lh->statement_program_end;
c906108c
SS
13200
13201 /* Read the statement sequences until there's nothing left. */
13202 while (line_ptr < line_end)
13203 {
13204 /* state machine registers */
13205 CORE_ADDR address = 0;
13206 unsigned int file = 1;
13207 unsigned int line = 1;
13208 unsigned int column = 0;
debd256d 13209 int is_stmt = lh->default_is_stmt;
c906108c
SS
13210 int basic_block = 0;
13211 int end_sequence = 0;
fbf65064 13212 CORE_ADDR addr;
2dc7f7b3 13213 unsigned char op_index = 0;
c906108c 13214
aaa75496 13215 if (!decode_for_pst_p && lh->num_file_names >= file)
c906108c 13216 {
aaa75496 13217 /* Start a subfile for the current file of the state machine. */
debd256d
JB
13218 /* lh->include_dirs and lh->file_names are 0-based, but the
13219 directory and file name numbers in the statement program
13220 are 1-based. */
13221 struct file_entry *fe = &lh->file_names[file - 1];
4f1520fb 13222 char *dir = NULL;
a738430d 13223
debd256d
JB
13224 if (fe->dir_index)
13225 dir = lh->include_dirs[fe->dir_index - 1];
4f1520fb
FR
13226
13227 dwarf2_start_subfile (fe->name, dir, comp_dir);
c906108c
SS
13228 }
13229
a738430d 13230 /* Decode the table. */
c5aa993b 13231 while (!end_sequence)
c906108c
SS
13232 {
13233 op_code = read_1_byte (abfd, line_ptr);
13234 line_ptr += 1;
59205f5a
JB
13235 if (line_ptr > line_end)
13236 {
13237 dwarf2_debug_line_missing_end_sequence_complaint ();
13238 break;
13239 }
9aa1fe7e 13240
debd256d 13241 if (op_code >= lh->opcode_base)
6e70227d 13242 {
a738430d 13243 /* Special operand. */
debd256d 13244 adj_opcode = op_code - lh->opcode_base;
2dc7f7b3
TT
13245 address += (((op_index + (adj_opcode / lh->line_range))
13246 / lh->maximum_ops_per_instruction)
13247 * lh->minimum_instruction_length);
13248 op_index = ((op_index + (adj_opcode / lh->line_range))
13249 % lh->maximum_ops_per_instruction);
debd256d 13250 line += lh->line_base + (adj_opcode % lh->line_range);
59205f5a 13251 if (lh->num_file_names < file || file == 0)
25e43795 13252 dwarf2_debug_line_missing_file_complaint ();
2dc7f7b3
TT
13253 /* For now we ignore lines not starting on an
13254 instruction boundary. */
13255 else if (op_index == 0)
25e43795
DJ
13256 {
13257 lh->file_names[file - 1].included_p = 1;
ca5f395d 13258 if (!decode_for_pst_p && is_stmt)
fbf65064
UW
13259 {
13260 if (last_subfile != current_subfile)
13261 {
13262 addr = gdbarch_addr_bits_remove (gdbarch, address);
13263 if (last_subfile)
c91513d8 13264 (*p_record_line) (last_subfile, 0, addr);
fbf65064
UW
13265 last_subfile = current_subfile;
13266 }
25e43795 13267 /* Append row to matrix using current values. */
7019d805 13268 addr = gdbarch_addr_bits_remove (gdbarch, address);
c91513d8 13269 (*p_record_line) (current_subfile, line, addr);
366da635 13270 }
25e43795 13271 }
ca5f395d 13272 basic_block = 0;
9aa1fe7e
GK
13273 }
13274 else switch (op_code)
c906108c
SS
13275 {
13276 case DW_LNS_extended_op:
3e43a32a
MS
13277 extended_len = read_unsigned_leb128 (abfd, line_ptr,
13278 &bytes_read);
473b7be6 13279 line_ptr += bytes_read;
a8c50c1f 13280 extended_end = line_ptr + extended_len;
c906108c
SS
13281 extended_op = read_1_byte (abfd, line_ptr);
13282 line_ptr += 1;
13283 switch (extended_op)
13284 {
13285 case DW_LNE_end_sequence:
c91513d8 13286 p_record_line = record_line;
c906108c 13287 end_sequence = 1;
c906108c
SS
13288 break;
13289 case DW_LNE_set_address:
e7c27a73 13290 address = read_address (abfd, line_ptr, cu, &bytes_read);
c91513d8
PP
13291
13292 if (address == 0 && !dwarf2_per_objfile->has_section_at_zero)
13293 {
13294 /* This line table is for a function which has been
13295 GCd by the linker. Ignore it. PR gdb/12528 */
13296
13297 long line_offset
13298 = line_ptr - dwarf2_per_objfile->line.buffer;
13299
13300 complaint (&symfile_complaints,
13301 _(".debug_line address at offset 0x%lx is 0 "
13302 "[in module %s]"),
bb5ed363 13303 line_offset, objfile->name);
c91513d8
PP
13304 p_record_line = noop_record_line;
13305 }
13306
2dc7f7b3 13307 op_index = 0;
107d2387
AC
13308 line_ptr += bytes_read;
13309 address += baseaddr;
c906108c
SS
13310 break;
13311 case DW_LNE_define_file:
debd256d
JB
13312 {
13313 char *cur_file;
13314 unsigned int dir_index, mod_time, length;
6e70227d 13315
3e43a32a
MS
13316 cur_file = read_direct_string (abfd, line_ptr,
13317 &bytes_read);
debd256d
JB
13318 line_ptr += bytes_read;
13319 dir_index =
13320 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
13321 line_ptr += bytes_read;
13322 mod_time =
13323 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
13324 line_ptr += bytes_read;
13325 length =
13326 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
13327 line_ptr += bytes_read;
13328 add_file_name (lh, cur_file, dir_index, mod_time, length);
13329 }
c906108c 13330 break;
d0c6ba3d
CC
13331 case DW_LNE_set_discriminator:
13332 /* The discriminator is not interesting to the debugger;
13333 just ignore it. */
13334 line_ptr = extended_end;
13335 break;
c906108c 13336 default:
4d3c2250 13337 complaint (&symfile_complaints,
e2e0b3e5 13338 _("mangled .debug_line section"));
debd256d 13339 return;
c906108c 13340 }
a8c50c1f
DJ
13341 /* Make sure that we parsed the extended op correctly. If e.g.
13342 we expected a different address size than the producer used,
13343 we may have read the wrong number of bytes. */
13344 if (line_ptr != extended_end)
13345 {
13346 complaint (&symfile_complaints,
13347 _("mangled .debug_line section"));
13348 return;
13349 }
c906108c
SS
13350 break;
13351 case DW_LNS_copy:
59205f5a 13352 if (lh->num_file_names < file || file == 0)
25e43795
DJ
13353 dwarf2_debug_line_missing_file_complaint ();
13354 else
366da635 13355 {
25e43795 13356 lh->file_names[file - 1].included_p = 1;
ca5f395d 13357 if (!decode_for_pst_p && is_stmt)
fbf65064
UW
13358 {
13359 if (last_subfile != current_subfile)
13360 {
13361 addr = gdbarch_addr_bits_remove (gdbarch, address);
13362 if (last_subfile)
c91513d8 13363 (*p_record_line) (last_subfile, 0, addr);
fbf65064
UW
13364 last_subfile = current_subfile;
13365 }
7019d805 13366 addr = gdbarch_addr_bits_remove (gdbarch, address);
c91513d8 13367 (*p_record_line) (current_subfile, line, addr);
fbf65064 13368 }
366da635 13369 }
c906108c
SS
13370 basic_block = 0;
13371 break;
13372 case DW_LNS_advance_pc:
2dc7f7b3
TT
13373 {
13374 CORE_ADDR adjust
13375 = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
13376
13377 address += (((op_index + adjust)
13378 / lh->maximum_ops_per_instruction)
13379 * lh->minimum_instruction_length);
13380 op_index = ((op_index + adjust)
13381 % lh->maximum_ops_per_instruction);
13382 line_ptr += bytes_read;
13383 }
c906108c
SS
13384 break;
13385 case DW_LNS_advance_line:
13386 line += read_signed_leb128 (abfd, line_ptr, &bytes_read);
13387 line_ptr += bytes_read;
13388 break;
13389 case DW_LNS_set_file:
debd256d 13390 {
a738430d
MK
13391 /* The arrays lh->include_dirs and lh->file_names are
13392 0-based, but the directory and file name numbers in
13393 the statement program are 1-based. */
debd256d 13394 struct file_entry *fe;
4f1520fb 13395 char *dir = NULL;
a738430d 13396
debd256d
JB
13397 file = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
13398 line_ptr += bytes_read;
59205f5a 13399 if (lh->num_file_names < file || file == 0)
25e43795
DJ
13400 dwarf2_debug_line_missing_file_complaint ();
13401 else
13402 {
13403 fe = &lh->file_names[file - 1];
13404 if (fe->dir_index)
13405 dir = lh->include_dirs[fe->dir_index - 1];
13406 if (!decode_for_pst_p)
13407 {
13408 last_subfile = current_subfile;
13409 dwarf2_start_subfile (fe->name, dir, comp_dir);
13410 }
13411 }
debd256d 13412 }
c906108c
SS
13413 break;
13414 case DW_LNS_set_column:
13415 column = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
13416 line_ptr += bytes_read;
13417 break;
13418 case DW_LNS_negate_stmt:
13419 is_stmt = (!is_stmt);
13420 break;
13421 case DW_LNS_set_basic_block:
13422 basic_block = 1;
13423 break;
c2c6d25f
JM
13424 /* Add to the address register of the state machine the
13425 address increment value corresponding to special opcode
a738430d
MK
13426 255. I.e., this value is scaled by the minimum
13427 instruction length since special opcode 255 would have
b021a221 13428 scaled the increment. */
c906108c 13429 case DW_LNS_const_add_pc:
2dc7f7b3
TT
13430 {
13431 CORE_ADDR adjust = (255 - lh->opcode_base) / lh->line_range;
13432
13433 address += (((op_index + adjust)
13434 / lh->maximum_ops_per_instruction)
13435 * lh->minimum_instruction_length);
13436 op_index = ((op_index + adjust)
13437 % lh->maximum_ops_per_instruction);
13438 }
c906108c
SS
13439 break;
13440 case DW_LNS_fixed_advance_pc:
13441 address += read_2_bytes (abfd, line_ptr);
2dc7f7b3 13442 op_index = 0;
c906108c
SS
13443 line_ptr += 2;
13444 break;
9aa1fe7e 13445 default:
a738430d
MK
13446 {
13447 /* Unknown standard opcode, ignore it. */
9aa1fe7e 13448 int i;
a738430d 13449
debd256d 13450 for (i = 0; i < lh->standard_opcode_lengths[op_code]; i++)
9aa1fe7e
GK
13451 {
13452 (void) read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
13453 line_ptr += bytes_read;
13454 }
13455 }
c906108c
SS
13456 }
13457 }
59205f5a
JB
13458 if (lh->num_file_names < file || file == 0)
13459 dwarf2_debug_line_missing_file_complaint ();
13460 else
13461 {
13462 lh->file_names[file - 1].included_p = 1;
13463 if (!decode_for_pst_p)
fbf65064
UW
13464 {
13465 addr = gdbarch_addr_bits_remove (gdbarch, address);
c91513d8 13466 (*p_record_line) (current_subfile, 0, addr);
fbf65064 13467 }
59205f5a 13468 }
c906108c 13469 }
f3f5162e
DE
13470}
13471
13472/* Decode the Line Number Program (LNP) for the given line_header
13473 structure and CU. The actual information extracted and the type
13474 of structures created from the LNP depends on the value of PST.
13475
13476 1. If PST is NULL, then this procedure uses the data from the program
13477 to create all necessary symbol tables, and their linetables.
13478
13479 2. If PST is not NULL, this procedure reads the program to determine
13480 the list of files included by the unit represented by PST, and
13481 builds all the associated partial symbol tables.
13482
13483 COMP_DIR is the compilation directory (DW_AT_comp_dir) or NULL if unknown.
13484 It is used for relative paths in the line table.
13485 NOTE: When processing partial symtabs (pst != NULL),
13486 comp_dir == pst->dirname.
13487
13488 NOTE: It is important that psymtabs have the same file name (via strcmp)
13489 as the corresponding symtab. Since COMP_DIR is not used in the name of the
13490 symtab we don't use it in the name of the psymtabs we create.
13491 E.g. expand_line_sal requires this when finding psymtabs to expand.
13492 A good testcase for this is mb-inline.exp. */
13493
13494static void
13495dwarf_decode_lines (struct line_header *lh, const char *comp_dir,
13496 struct dwarf2_cu *cu, struct partial_symtab *pst,
13497 int want_line_info)
13498{
13499 struct objfile *objfile = cu->objfile;
13500 const int decode_for_pst_p = (pst != NULL);
13501 struct subfile *first_subfile = current_subfile;
13502
13503 if (want_line_info)
13504 dwarf_decode_lines_1 (lh, comp_dir, cu, pst);
aaa75496
JB
13505
13506 if (decode_for_pst_p)
13507 {
13508 int file_index;
13509
13510 /* Now that we're done scanning the Line Header Program, we can
13511 create the psymtab of each included file. */
13512 for (file_index = 0; file_index < lh->num_file_names; file_index++)
13513 if (lh->file_names[file_index].included_p == 1)
13514 {
c6da4cef
DE
13515 char *include_name =
13516 psymtab_include_file_name (lh, file_index, pst, comp_dir);
13517 if (include_name != NULL)
aaa75496
JB
13518 dwarf2_create_include_psymtab (include_name, pst, objfile);
13519 }
13520 }
cb1df416
DJ
13521 else
13522 {
13523 /* Make sure a symtab is created for every file, even files
13524 which contain only variables (i.e. no code with associated
13525 line numbers). */
cb1df416 13526 int i;
cb1df416
DJ
13527
13528 for (i = 0; i < lh->num_file_names; i++)
13529 {
13530 char *dir = NULL;
f3f5162e 13531 struct file_entry *fe;
9a619af0 13532
cb1df416
DJ
13533 fe = &lh->file_names[i];
13534 if (fe->dir_index)
13535 dir = lh->include_dirs[fe->dir_index - 1];
13536 dwarf2_start_subfile (fe->name, dir, comp_dir);
13537
13538 /* Skip the main file; we don't need it, and it must be
13539 allocated last, so that it will show up before the
13540 non-primary symtabs in the objfile's symtab list. */
13541 if (current_subfile == first_subfile)
13542 continue;
13543
13544 if (current_subfile->symtab == NULL)
13545 current_subfile->symtab = allocate_symtab (current_subfile->name,
bb5ed363 13546 objfile);
cb1df416
DJ
13547 fe->symtab = current_subfile->symtab;
13548 }
13549 }
c906108c
SS
13550}
13551
13552/* Start a subfile for DWARF. FILENAME is the name of the file and
13553 DIRNAME the name of the source directory which contains FILENAME
4f1520fb
FR
13554 or NULL if not known. COMP_DIR is the compilation directory for the
13555 linetable's compilation unit or NULL if not known.
c906108c
SS
13556 This routine tries to keep line numbers from identical absolute and
13557 relative file names in a common subfile.
13558
13559 Using the `list' example from the GDB testsuite, which resides in
13560 /srcdir and compiling it with Irix6.2 cc in /compdir using a filename
13561 of /srcdir/list0.c yields the following debugging information for list0.c:
13562
c5aa993b
JM
13563 DW_AT_name: /srcdir/list0.c
13564 DW_AT_comp_dir: /compdir
357e46e7 13565 files.files[0].name: list0.h
c5aa993b 13566 files.files[0].dir: /srcdir
357e46e7 13567 files.files[1].name: list0.c
c5aa993b 13568 files.files[1].dir: /srcdir
c906108c
SS
13569
13570 The line number information for list0.c has to end up in a single
4f1520fb
FR
13571 subfile, so that `break /srcdir/list0.c:1' works as expected.
13572 start_subfile will ensure that this happens provided that we pass the
13573 concatenation of files.files[1].dir and files.files[1].name as the
13574 subfile's name. */
c906108c
SS
13575
13576static void
3e43a32a
MS
13577dwarf2_start_subfile (char *filename, const char *dirname,
13578 const char *comp_dir)
c906108c 13579{
4f1520fb
FR
13580 char *fullname;
13581
13582 /* While reading the DIEs, we call start_symtab(DW_AT_name, DW_AT_comp_dir).
13583 `start_symtab' will always pass the contents of DW_AT_comp_dir as
13584 second argument to start_subfile. To be consistent, we do the
13585 same here. In order not to lose the line information directory,
13586 we concatenate it to the filename when it makes sense.
13587 Note that the Dwarf3 standard says (speaking of filenames in line
13588 information): ``The directory index is ignored for file names
13589 that represent full path names''. Thus ignoring dirname in the
13590 `else' branch below isn't an issue. */
c906108c 13591
d5166ae1 13592 if (!IS_ABSOLUTE_PATH (filename) && dirname != NULL)
4f1520fb
FR
13593 fullname = concat (dirname, SLASH_STRING, filename, (char *)NULL);
13594 else
13595 fullname = filename;
c906108c 13596
4f1520fb
FR
13597 start_subfile (fullname, comp_dir);
13598
13599 if (fullname != filename)
13600 xfree (fullname);
c906108c
SS
13601}
13602
4c2df51b
DJ
13603static void
13604var_decode_location (struct attribute *attr, struct symbol *sym,
e7c27a73 13605 struct dwarf2_cu *cu)
4c2df51b 13606{
e7c27a73
DJ
13607 struct objfile *objfile = cu->objfile;
13608 struct comp_unit_head *cu_header = &cu->header;
13609
4c2df51b
DJ
13610 /* NOTE drow/2003-01-30: There used to be a comment and some special
13611 code here to turn a symbol with DW_AT_external and a
13612 SYMBOL_VALUE_ADDRESS of 0 into a LOC_UNRESOLVED symbol. This was
13613 necessary for platforms (maybe Alpha, certainly PowerPC GNU/Linux
13614 with some versions of binutils) where shared libraries could have
13615 relocations against symbols in their debug information - the
13616 minimal symbol would have the right address, but the debug info
13617 would not. It's no longer necessary, because we will explicitly
13618 apply relocations when we read in the debug information now. */
13619
13620 /* A DW_AT_location attribute with no contents indicates that a
13621 variable has been optimized away. */
13622 if (attr_form_is_block (attr) && DW_BLOCK (attr)->size == 0)
13623 {
13624 SYMBOL_CLASS (sym) = LOC_OPTIMIZED_OUT;
13625 return;
13626 }
13627
13628 /* Handle one degenerate form of location expression specially, to
13629 preserve GDB's previous behavior when section offsets are
3019eac3
DE
13630 specified. If this is just a DW_OP_addr or DW_OP_GNU_addr_index
13631 then mark this symbol as LOC_STATIC. */
4c2df51b
DJ
13632
13633 if (attr_form_is_block (attr)
3019eac3
DE
13634 && ((DW_BLOCK (attr)->data[0] == DW_OP_addr
13635 && DW_BLOCK (attr)->size == 1 + cu_header->addr_size)
13636 || (DW_BLOCK (attr)->data[0] == DW_OP_GNU_addr_index
13637 && (DW_BLOCK (attr)->size
13638 == 1 + leb128_size (&DW_BLOCK (attr)->data[1])))))
4c2df51b 13639 {
891d2f0b 13640 unsigned int dummy;
4c2df51b 13641
3019eac3
DE
13642 if (DW_BLOCK (attr)->data[0] == DW_OP_addr)
13643 SYMBOL_VALUE_ADDRESS (sym) =
13644 read_address (objfile->obfd, DW_BLOCK (attr)->data + 1, cu, &dummy);
13645 else
13646 SYMBOL_VALUE_ADDRESS (sym) =
13647 read_addr_index_from_leb128 (cu, DW_BLOCK (attr)->data + 1, &dummy);
907fc202 13648 SYMBOL_CLASS (sym) = LOC_STATIC;
4c2df51b
DJ
13649 fixup_symbol_section (sym, objfile);
13650 SYMBOL_VALUE_ADDRESS (sym) += ANOFFSET (objfile->section_offsets,
13651 SYMBOL_SECTION (sym));
4c2df51b
DJ
13652 return;
13653 }
13654
13655 /* NOTE drow/2002-01-30: It might be worthwhile to have a static
13656 expression evaluator, and use LOC_COMPUTED only when necessary
13657 (i.e. when the value of a register or memory location is
13658 referenced, or a thread-local block, etc.). Then again, it might
13659 not be worthwhile. I'm assuming that it isn't unless performance
13660 or memory numbers show me otherwise. */
13661
e7c27a73 13662 dwarf2_symbol_mark_computed (attr, sym, cu);
4c2df51b 13663 SYMBOL_CLASS (sym) = LOC_COMPUTED;
8be455d7
JK
13664
13665 if (SYMBOL_COMPUTED_OPS (sym) == &dwarf2_loclist_funcs)
13666 cu->has_loclist = 1;
4c2df51b
DJ
13667}
13668
c906108c
SS
13669/* Given a pointer to a DWARF information entry, figure out if we need
13670 to make a symbol table entry for it, and if so, create a new entry
13671 and return a pointer to it.
13672 If TYPE is NULL, determine symbol type from the die, otherwise
34eaf542
TT
13673 used the passed type.
13674 If SPACE is not NULL, use it to hold the new symbol. If it is
13675 NULL, allocate a new symbol on the objfile's obstack. */
c906108c
SS
13676
13677static struct symbol *
34eaf542
TT
13678new_symbol_full (struct die_info *die, struct type *type, struct dwarf2_cu *cu,
13679 struct symbol *space)
c906108c 13680{
e7c27a73 13681 struct objfile *objfile = cu->objfile;
c906108c
SS
13682 struct symbol *sym = NULL;
13683 char *name;
13684 struct attribute *attr = NULL;
13685 struct attribute *attr2 = NULL;
e142c38c 13686 CORE_ADDR baseaddr;
e37fd15a
SW
13687 struct pending **list_to_add = NULL;
13688
edb3359d 13689 int inlined_func = (die->tag == DW_TAG_inlined_subroutine);
e142c38c
DJ
13690
13691 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
c906108c 13692
94af9270 13693 name = dwarf2_name (die, cu);
c906108c
SS
13694 if (name)
13695 {
94af9270 13696 const char *linkagename;
34eaf542 13697 int suppress_add = 0;
94af9270 13698
34eaf542
TT
13699 if (space)
13700 sym = space;
13701 else
13702 sym = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct symbol);
c906108c 13703 OBJSTAT (objfile, n_syms++);
2de7ced7
DJ
13704
13705 /* Cache this symbol's name and the name's demangled form (if any). */
33e5013e 13706 SYMBOL_SET_LANGUAGE (sym, cu->language);
94af9270
KS
13707 linkagename = dwarf2_physname (name, die, cu);
13708 SYMBOL_SET_NAMES (sym, linkagename, strlen (linkagename), 0, objfile);
c906108c 13709
f55ee35c
JK
13710 /* Fortran does not have mangling standard and the mangling does differ
13711 between gfortran, iFort etc. */
13712 if (cu->language == language_fortran
b250c185 13713 && symbol_get_demangled_name (&(sym->ginfo)) == NULL)
29df156d
SW
13714 symbol_set_demangled_name (&(sym->ginfo),
13715 (char *) dwarf2_full_name (name, die, cu),
13716 NULL);
f55ee35c 13717
c906108c 13718 /* Default assumptions.
c5aa993b 13719 Use the passed type or decode it from the die. */
176620f1 13720 SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
875dc2fc 13721 SYMBOL_CLASS (sym) = LOC_OPTIMIZED_OUT;
c906108c
SS
13722 if (type != NULL)
13723 SYMBOL_TYPE (sym) = type;
13724 else
e7c27a73 13725 SYMBOL_TYPE (sym) = die_type (die, cu);
edb3359d
DJ
13726 attr = dwarf2_attr (die,
13727 inlined_func ? DW_AT_call_line : DW_AT_decl_line,
13728 cu);
c906108c
SS
13729 if (attr)
13730 {
13731 SYMBOL_LINE (sym) = DW_UNSND (attr);
13732 }
cb1df416 13733
edb3359d
DJ
13734 attr = dwarf2_attr (die,
13735 inlined_func ? DW_AT_call_file : DW_AT_decl_file,
13736 cu);
cb1df416
DJ
13737 if (attr)
13738 {
13739 int file_index = DW_UNSND (attr);
9a619af0 13740
cb1df416
DJ
13741 if (cu->line_header == NULL
13742 || file_index > cu->line_header->num_file_names)
13743 complaint (&symfile_complaints,
13744 _("file index out of range"));
1c3d648d 13745 else if (file_index > 0)
cb1df416
DJ
13746 {
13747 struct file_entry *fe;
9a619af0 13748
cb1df416
DJ
13749 fe = &cu->line_header->file_names[file_index - 1];
13750 SYMBOL_SYMTAB (sym) = fe->symtab;
13751 }
13752 }
13753
c906108c
SS
13754 switch (die->tag)
13755 {
13756 case DW_TAG_label:
e142c38c 13757 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
c906108c
SS
13758 if (attr)
13759 {
13760 SYMBOL_VALUE_ADDRESS (sym) = DW_ADDR (attr) + baseaddr;
13761 }
0f5238ed
TT
13762 SYMBOL_TYPE (sym) = objfile_type (objfile)->builtin_core_addr;
13763 SYMBOL_DOMAIN (sym) = LABEL_DOMAIN;
c906108c 13764 SYMBOL_CLASS (sym) = LOC_LABEL;
0f5238ed 13765 add_symbol_to_list (sym, cu->list_in_scope);
c906108c
SS
13766 break;
13767 case DW_TAG_subprogram:
13768 /* SYMBOL_BLOCK_VALUE (sym) will be filled in later by
13769 finish_block. */
13770 SYMBOL_CLASS (sym) = LOC_BLOCK;
e142c38c 13771 attr2 = dwarf2_attr (die, DW_AT_external, cu);
2cfa0c8d
JB
13772 if ((attr2 && (DW_UNSND (attr2) != 0))
13773 || cu->language == language_ada)
c906108c 13774 {
2cfa0c8d
JB
13775 /* Subprograms marked external are stored as a global symbol.
13776 Ada subprograms, whether marked external or not, are always
13777 stored as a global symbol, because we want to be able to
13778 access them globally. For instance, we want to be able
13779 to break on a nested subprogram without having to
13780 specify the context. */
e37fd15a 13781 list_to_add = &global_symbols;
c906108c
SS
13782 }
13783 else
13784 {
e37fd15a 13785 list_to_add = cu->list_in_scope;
c906108c
SS
13786 }
13787 break;
edb3359d
DJ
13788 case DW_TAG_inlined_subroutine:
13789 /* SYMBOL_BLOCK_VALUE (sym) will be filled in later by
13790 finish_block. */
13791 SYMBOL_CLASS (sym) = LOC_BLOCK;
13792 SYMBOL_INLINED (sym) = 1;
481860b3 13793 list_to_add = cu->list_in_scope;
edb3359d 13794 break;
34eaf542
TT
13795 case DW_TAG_template_value_param:
13796 suppress_add = 1;
13797 /* Fall through. */
72929c62 13798 case DW_TAG_constant:
c906108c 13799 case DW_TAG_variable:
254e6b9e 13800 case DW_TAG_member:
0963b4bd
MS
13801 /* Compilation with minimal debug info may result in
13802 variables with missing type entries. Change the
13803 misleading `void' type to something sensible. */
c906108c 13804 if (TYPE_CODE (SYMBOL_TYPE (sym)) == TYPE_CODE_VOID)
64c50499 13805 SYMBOL_TYPE (sym)
46bf5051 13806 = objfile_type (objfile)->nodebug_data_symbol;
64c50499 13807
e142c38c 13808 attr = dwarf2_attr (die, DW_AT_const_value, cu);
254e6b9e
DE
13809 /* In the case of DW_TAG_member, we should only be called for
13810 static const members. */
13811 if (die->tag == DW_TAG_member)
13812 {
3863f96c
DE
13813 /* dwarf2_add_field uses die_is_declaration,
13814 so we do the same. */
254e6b9e
DE
13815 gdb_assert (die_is_declaration (die, cu));
13816 gdb_assert (attr);
13817 }
c906108c
SS
13818 if (attr)
13819 {
e7c27a73 13820 dwarf2_const_value (attr, sym, cu);
e142c38c 13821 attr2 = dwarf2_attr (die, DW_AT_external, cu);
e37fd15a 13822 if (!suppress_add)
34eaf542
TT
13823 {
13824 if (attr2 && (DW_UNSND (attr2) != 0))
e37fd15a 13825 list_to_add = &global_symbols;
34eaf542 13826 else
e37fd15a 13827 list_to_add = cu->list_in_scope;
34eaf542 13828 }
c906108c
SS
13829 break;
13830 }
e142c38c 13831 attr = dwarf2_attr (die, DW_AT_location, cu);
c906108c
SS
13832 if (attr)
13833 {
e7c27a73 13834 var_decode_location (attr, sym, cu);
e142c38c 13835 attr2 = dwarf2_attr (die, DW_AT_external, cu);
caac4577
JG
13836 if (SYMBOL_CLASS (sym) == LOC_STATIC
13837 && SYMBOL_VALUE_ADDRESS (sym) == 0
13838 && !dwarf2_per_objfile->has_section_at_zero)
13839 {
13840 /* When a static variable is eliminated by the linker,
13841 the corresponding debug information is not stripped
13842 out, but the variable address is set to null;
13843 do not add such variables into symbol table. */
13844 }
13845 else if (attr2 && (DW_UNSND (attr2) != 0))
1c809c68 13846 {
f55ee35c
JK
13847 /* Workaround gfortran PR debug/40040 - it uses
13848 DW_AT_location for variables in -fPIC libraries which may
13849 get overriden by other libraries/executable and get
13850 a different address. Resolve it by the minimal symbol
13851 which may come from inferior's executable using copy
13852 relocation. Make this workaround only for gfortran as for
13853 other compilers GDB cannot guess the minimal symbol
13854 Fortran mangling kind. */
13855 if (cu->language == language_fortran && die->parent
13856 && die->parent->tag == DW_TAG_module
13857 && cu->producer
13858 && strncmp (cu->producer, "GNU Fortran ", 12) == 0)
13859 SYMBOL_CLASS (sym) = LOC_UNRESOLVED;
13860
1c809c68
TT
13861 /* A variable with DW_AT_external is never static,
13862 but it may be block-scoped. */
13863 list_to_add = (cu->list_in_scope == &file_symbols
13864 ? &global_symbols : cu->list_in_scope);
1c809c68 13865 }
c906108c 13866 else
e37fd15a 13867 list_to_add = cu->list_in_scope;
c906108c
SS
13868 }
13869 else
13870 {
13871 /* We do not know the address of this symbol.
c5aa993b
JM
13872 If it is an external symbol and we have type information
13873 for it, enter the symbol as a LOC_UNRESOLVED symbol.
13874 The address of the variable will then be determined from
13875 the minimal symbol table whenever the variable is
13876 referenced. */
e142c38c 13877 attr2 = dwarf2_attr (die, DW_AT_external, cu);
c906108c 13878 if (attr2 && (DW_UNSND (attr2) != 0)
e142c38c 13879 && dwarf2_attr (die, DW_AT_type, cu) != NULL)
c906108c 13880 {
0fe7935b
DJ
13881 /* A variable with DW_AT_external is never static, but it
13882 may be block-scoped. */
13883 list_to_add = (cu->list_in_scope == &file_symbols
13884 ? &global_symbols : cu->list_in_scope);
13885
c906108c 13886 SYMBOL_CLASS (sym) = LOC_UNRESOLVED;
c906108c 13887 }
442ddf59
JK
13888 else if (!die_is_declaration (die, cu))
13889 {
13890 /* Use the default LOC_OPTIMIZED_OUT class. */
13891 gdb_assert (SYMBOL_CLASS (sym) == LOC_OPTIMIZED_OUT);
e37fd15a
SW
13892 if (!suppress_add)
13893 list_to_add = cu->list_in_scope;
442ddf59 13894 }
c906108c
SS
13895 }
13896 break;
13897 case DW_TAG_formal_parameter:
edb3359d
DJ
13898 /* If we are inside a function, mark this as an argument. If
13899 not, we might be looking at an argument to an inlined function
13900 when we do not have enough information to show inlined frames;
13901 pretend it's a local variable in that case so that the user can
13902 still see it. */
13903 if (context_stack_depth > 0
13904 && context_stack[context_stack_depth - 1].name != NULL)
13905 SYMBOL_IS_ARGUMENT (sym) = 1;
e142c38c 13906 attr = dwarf2_attr (die, DW_AT_location, cu);
c906108c
SS
13907 if (attr)
13908 {
e7c27a73 13909 var_decode_location (attr, sym, cu);
c906108c 13910 }
e142c38c 13911 attr = dwarf2_attr (die, DW_AT_const_value, cu);
c906108c
SS
13912 if (attr)
13913 {
e7c27a73 13914 dwarf2_const_value (attr, sym, cu);
c906108c 13915 }
f346a30d 13916
e37fd15a 13917 list_to_add = cu->list_in_scope;
c906108c
SS
13918 break;
13919 case DW_TAG_unspecified_parameters:
13920 /* From varargs functions; gdb doesn't seem to have any
13921 interest in this information, so just ignore it for now.
13922 (FIXME?) */
13923 break;
34eaf542
TT
13924 case DW_TAG_template_type_param:
13925 suppress_add = 1;
13926 /* Fall through. */
c906108c 13927 case DW_TAG_class_type:
680b30c7 13928 case DW_TAG_interface_type:
c906108c
SS
13929 case DW_TAG_structure_type:
13930 case DW_TAG_union_type:
72019c9c 13931 case DW_TAG_set_type:
c906108c
SS
13932 case DW_TAG_enumeration_type:
13933 SYMBOL_CLASS (sym) = LOC_TYPEDEF;
176620f1 13934 SYMBOL_DOMAIN (sym) = STRUCT_DOMAIN;
c906108c 13935
63d06c5c 13936 {
987504bb 13937 /* NOTE: carlton/2003-11-10: C++ and Java class symbols shouldn't
63d06c5c
DC
13938 really ever be static objects: otherwise, if you try
13939 to, say, break of a class's method and you're in a file
13940 which doesn't mention that class, it won't work unless
13941 the check for all static symbols in lookup_symbol_aux
13942 saves you. See the OtherFileClass tests in
13943 gdb.c++/namespace.exp. */
13944
e37fd15a 13945 if (!suppress_add)
34eaf542 13946 {
34eaf542
TT
13947 list_to_add = (cu->list_in_scope == &file_symbols
13948 && (cu->language == language_cplus
13949 || cu->language == language_java)
13950 ? &global_symbols : cu->list_in_scope);
63d06c5c 13951
64382290
TT
13952 /* The semantics of C++ state that "struct foo {
13953 ... }" also defines a typedef for "foo". A Java
13954 class declaration also defines a typedef for the
13955 class. */
13956 if (cu->language == language_cplus
13957 || cu->language == language_java
13958 || cu->language == language_ada)
13959 {
13960 /* The symbol's name is already allocated along
13961 with this objfile, so we don't need to
13962 duplicate it for the type. */
13963 if (TYPE_NAME (SYMBOL_TYPE (sym)) == 0)
13964 TYPE_NAME (SYMBOL_TYPE (sym)) = SYMBOL_SEARCH_NAME (sym);
13965 }
63d06c5c
DC
13966 }
13967 }
c906108c
SS
13968 break;
13969 case DW_TAG_typedef:
63d06c5c
DC
13970 SYMBOL_CLASS (sym) = LOC_TYPEDEF;
13971 SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
e37fd15a 13972 list_to_add = cu->list_in_scope;
63d06c5c 13973 break;
c906108c 13974 case DW_TAG_base_type:
a02abb62 13975 case DW_TAG_subrange_type:
c906108c 13976 SYMBOL_CLASS (sym) = LOC_TYPEDEF;
176620f1 13977 SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
e37fd15a 13978 list_to_add = cu->list_in_scope;
c906108c
SS
13979 break;
13980 case DW_TAG_enumerator:
e142c38c 13981 attr = dwarf2_attr (die, DW_AT_const_value, cu);
c906108c
SS
13982 if (attr)
13983 {
e7c27a73 13984 dwarf2_const_value (attr, sym, cu);
c906108c 13985 }
63d06c5c
DC
13986 {
13987 /* NOTE: carlton/2003-11-10: See comment above in the
13988 DW_TAG_class_type, etc. block. */
13989
e142c38c 13990 list_to_add = (cu->list_in_scope == &file_symbols
987504bb
JJ
13991 && (cu->language == language_cplus
13992 || cu->language == language_java)
e142c38c 13993 ? &global_symbols : cu->list_in_scope);
63d06c5c 13994 }
c906108c 13995 break;
5c4e30ca
DC
13996 case DW_TAG_namespace:
13997 SYMBOL_CLASS (sym) = LOC_TYPEDEF;
e37fd15a 13998 list_to_add = &global_symbols;
5c4e30ca 13999 break;
c906108c
SS
14000 default:
14001 /* Not a tag we recognize. Hopefully we aren't processing
14002 trash data, but since we must specifically ignore things
14003 we don't recognize, there is nothing else we should do at
0963b4bd 14004 this point. */
e2e0b3e5 14005 complaint (&symfile_complaints, _("unsupported tag: '%s'"),
4d3c2250 14006 dwarf_tag_name (die->tag));
c906108c
SS
14007 break;
14008 }
df8a16a1 14009
e37fd15a
SW
14010 if (suppress_add)
14011 {
14012 sym->hash_next = objfile->template_symbols;
14013 objfile->template_symbols = sym;
14014 list_to_add = NULL;
14015 }
14016
14017 if (list_to_add != NULL)
14018 add_symbol_to_list (sym, list_to_add);
14019
df8a16a1
DJ
14020 /* For the benefit of old versions of GCC, check for anonymous
14021 namespaces based on the demangled name. */
14022 if (!processing_has_namespace_info
94af9270 14023 && cu->language == language_cplus)
a10964d1 14024 cp_scan_for_anonymous_namespaces (sym, objfile);
c906108c
SS
14025 }
14026 return (sym);
14027}
14028
34eaf542
TT
14029/* A wrapper for new_symbol_full that always allocates a new symbol. */
14030
14031static struct symbol *
14032new_symbol (struct die_info *die, struct type *type, struct dwarf2_cu *cu)
14033{
14034 return new_symbol_full (die, type, cu, NULL);
14035}
14036
98bfdba5
PA
14037/* Given an attr with a DW_FORM_dataN value in host byte order,
14038 zero-extend it as appropriate for the symbol's type. The DWARF
14039 standard (v4) is not entirely clear about the meaning of using
14040 DW_FORM_dataN for a constant with a signed type, where the type is
14041 wider than the data. The conclusion of a discussion on the DWARF
14042 list was that this is unspecified. We choose to always zero-extend
14043 because that is the interpretation long in use by GCC. */
c906108c 14044
98bfdba5
PA
14045static gdb_byte *
14046dwarf2_const_value_data (struct attribute *attr, struct type *type,
14047 const char *name, struct obstack *obstack,
12df843f 14048 struct dwarf2_cu *cu, LONGEST *value, int bits)
c906108c 14049{
e7c27a73 14050 struct objfile *objfile = cu->objfile;
e17a4113
UW
14051 enum bfd_endian byte_order = bfd_big_endian (objfile->obfd) ?
14052 BFD_ENDIAN_BIG : BFD_ENDIAN_LITTLE;
98bfdba5
PA
14053 LONGEST l = DW_UNSND (attr);
14054
14055 if (bits < sizeof (*value) * 8)
14056 {
14057 l &= ((LONGEST) 1 << bits) - 1;
14058 *value = l;
14059 }
14060 else if (bits == sizeof (*value) * 8)
14061 *value = l;
14062 else
14063 {
14064 gdb_byte *bytes = obstack_alloc (obstack, bits / 8);
14065 store_unsigned_integer (bytes, bits / 8, byte_order, l);
14066 return bytes;
14067 }
14068
14069 return NULL;
14070}
14071
14072/* Read a constant value from an attribute. Either set *VALUE, or if
14073 the value does not fit in *VALUE, set *BYTES - either already
14074 allocated on the objfile obstack, or newly allocated on OBSTACK,
14075 or, set *BATON, if we translated the constant to a location
14076 expression. */
14077
14078static void
14079dwarf2_const_value_attr (struct attribute *attr, struct type *type,
14080 const char *name, struct obstack *obstack,
14081 struct dwarf2_cu *cu,
12df843f 14082 LONGEST *value, gdb_byte **bytes,
98bfdba5
PA
14083 struct dwarf2_locexpr_baton **baton)
14084{
14085 struct objfile *objfile = cu->objfile;
14086 struct comp_unit_head *cu_header = &cu->header;
c906108c 14087 struct dwarf_block *blk;
98bfdba5
PA
14088 enum bfd_endian byte_order = (bfd_big_endian (objfile->obfd) ?
14089 BFD_ENDIAN_BIG : BFD_ENDIAN_LITTLE);
14090
14091 *value = 0;
14092 *bytes = NULL;
14093 *baton = NULL;
c906108c
SS
14094
14095 switch (attr->form)
14096 {
14097 case DW_FORM_addr:
3019eac3 14098 case DW_FORM_GNU_addr_index:
ac56253d 14099 {
ac56253d
TT
14100 gdb_byte *data;
14101
98bfdba5
PA
14102 if (TYPE_LENGTH (type) != cu_header->addr_size)
14103 dwarf2_const_value_length_mismatch_complaint (name,
ac56253d 14104 cu_header->addr_size,
98bfdba5 14105 TYPE_LENGTH (type));
ac56253d
TT
14106 /* Symbols of this form are reasonably rare, so we just
14107 piggyback on the existing location code rather than writing
14108 a new implementation of symbol_computed_ops. */
98bfdba5
PA
14109 *baton = obstack_alloc (&objfile->objfile_obstack,
14110 sizeof (struct dwarf2_locexpr_baton));
14111 (*baton)->per_cu = cu->per_cu;
14112 gdb_assert ((*baton)->per_cu);
ac56253d 14113
98bfdba5
PA
14114 (*baton)->size = 2 + cu_header->addr_size;
14115 data = obstack_alloc (&objfile->objfile_obstack, (*baton)->size);
14116 (*baton)->data = data;
ac56253d
TT
14117
14118 data[0] = DW_OP_addr;
14119 store_unsigned_integer (&data[1], cu_header->addr_size,
14120 byte_order, DW_ADDR (attr));
14121 data[cu_header->addr_size + 1] = DW_OP_stack_value;
ac56253d 14122 }
c906108c 14123 break;
4ac36638 14124 case DW_FORM_string:
93b5768b 14125 case DW_FORM_strp:
3019eac3 14126 case DW_FORM_GNU_str_index:
98bfdba5
PA
14127 /* DW_STRING is already allocated on the objfile obstack, point
14128 directly to it. */
14129 *bytes = (gdb_byte *) DW_STRING (attr);
93b5768b 14130 break;
c906108c
SS
14131 case DW_FORM_block1:
14132 case DW_FORM_block2:
14133 case DW_FORM_block4:
14134 case DW_FORM_block:
2dc7f7b3 14135 case DW_FORM_exprloc:
c906108c 14136 blk = DW_BLOCK (attr);
98bfdba5
PA
14137 if (TYPE_LENGTH (type) != blk->size)
14138 dwarf2_const_value_length_mismatch_complaint (name, blk->size,
14139 TYPE_LENGTH (type));
14140 *bytes = blk->data;
c906108c 14141 break;
2df3850c
JM
14142
14143 /* The DW_AT_const_value attributes are supposed to carry the
14144 symbol's value "represented as it would be on the target
14145 architecture." By the time we get here, it's already been
14146 converted to host endianness, so we just need to sign- or
14147 zero-extend it as appropriate. */
14148 case DW_FORM_data1:
3e43a32a
MS
14149 *bytes = dwarf2_const_value_data (attr, type, name,
14150 obstack, cu, value, 8);
2df3850c 14151 break;
c906108c 14152 case DW_FORM_data2:
3e43a32a
MS
14153 *bytes = dwarf2_const_value_data (attr, type, name,
14154 obstack, cu, value, 16);
2df3850c 14155 break;
c906108c 14156 case DW_FORM_data4:
3e43a32a
MS
14157 *bytes = dwarf2_const_value_data (attr, type, name,
14158 obstack, cu, value, 32);
2df3850c 14159 break;
c906108c 14160 case DW_FORM_data8:
3e43a32a
MS
14161 *bytes = dwarf2_const_value_data (attr, type, name,
14162 obstack, cu, value, 64);
2df3850c
JM
14163 break;
14164
c906108c 14165 case DW_FORM_sdata:
98bfdba5 14166 *value = DW_SND (attr);
2df3850c
JM
14167 break;
14168
c906108c 14169 case DW_FORM_udata:
98bfdba5 14170 *value = DW_UNSND (attr);
c906108c 14171 break;
2df3850c 14172
c906108c 14173 default:
4d3c2250 14174 complaint (&symfile_complaints,
e2e0b3e5 14175 _("unsupported const value attribute form: '%s'"),
4d3c2250 14176 dwarf_form_name (attr->form));
98bfdba5 14177 *value = 0;
c906108c
SS
14178 break;
14179 }
14180}
14181
2df3850c 14182
98bfdba5
PA
14183/* Copy constant value from an attribute to a symbol. */
14184
2df3850c 14185static void
98bfdba5
PA
14186dwarf2_const_value (struct attribute *attr, struct symbol *sym,
14187 struct dwarf2_cu *cu)
2df3850c 14188{
98bfdba5
PA
14189 struct objfile *objfile = cu->objfile;
14190 struct comp_unit_head *cu_header = &cu->header;
12df843f 14191 LONGEST value;
98bfdba5
PA
14192 gdb_byte *bytes;
14193 struct dwarf2_locexpr_baton *baton;
2df3850c 14194
98bfdba5
PA
14195 dwarf2_const_value_attr (attr, SYMBOL_TYPE (sym),
14196 SYMBOL_PRINT_NAME (sym),
14197 &objfile->objfile_obstack, cu,
14198 &value, &bytes, &baton);
2df3850c 14199
98bfdba5
PA
14200 if (baton != NULL)
14201 {
14202 SYMBOL_COMPUTED_OPS (sym) = &dwarf2_locexpr_funcs;
14203 SYMBOL_LOCATION_BATON (sym) = baton;
14204 SYMBOL_CLASS (sym) = LOC_COMPUTED;
14205 }
14206 else if (bytes != NULL)
14207 {
14208 SYMBOL_VALUE_BYTES (sym) = bytes;
14209 SYMBOL_CLASS (sym) = LOC_CONST_BYTES;
14210 }
14211 else
14212 {
14213 SYMBOL_VALUE (sym) = value;
14214 SYMBOL_CLASS (sym) = LOC_CONST;
14215 }
2df3850c
JM
14216}
14217
c906108c
SS
14218/* Return the type of the die in question using its DW_AT_type attribute. */
14219
14220static struct type *
e7c27a73 14221die_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 14222{
c906108c 14223 struct attribute *type_attr;
c906108c 14224
e142c38c 14225 type_attr = dwarf2_attr (die, DW_AT_type, cu);
c906108c
SS
14226 if (!type_attr)
14227 {
14228 /* A missing DW_AT_type represents a void type. */
46bf5051 14229 return objfile_type (cu->objfile)->builtin_void;
c906108c 14230 }
348e048f 14231
673bfd45 14232 return lookup_die_type (die, type_attr, cu);
c906108c
SS
14233}
14234
b4ba55a1
JB
14235/* True iff CU's producer generates GNAT Ada auxiliary information
14236 that allows to find parallel types through that information instead
14237 of having to do expensive parallel lookups by type name. */
14238
14239static int
14240need_gnat_info (struct dwarf2_cu *cu)
14241{
14242 /* FIXME: brobecker/2010-10-12: As of now, only the AdaCore version
14243 of GNAT produces this auxiliary information, without any indication
14244 that it is produced. Part of enhancing the FSF version of GNAT
14245 to produce that information will be to put in place an indicator
14246 that we can use in order to determine whether the descriptive type
14247 info is available or not. One suggestion that has been made is
14248 to use a new attribute, attached to the CU die. For now, assume
14249 that the descriptive type info is not available. */
14250 return 0;
14251}
14252
b4ba55a1
JB
14253/* Return the auxiliary type of the die in question using its
14254 DW_AT_GNAT_descriptive_type attribute. Returns NULL if the
14255 attribute is not present. */
14256
14257static struct type *
14258die_descriptive_type (struct die_info *die, struct dwarf2_cu *cu)
14259{
b4ba55a1 14260 struct attribute *type_attr;
b4ba55a1
JB
14261
14262 type_attr = dwarf2_attr (die, DW_AT_GNAT_descriptive_type, cu);
14263 if (!type_attr)
14264 return NULL;
14265
673bfd45 14266 return lookup_die_type (die, type_attr, cu);
b4ba55a1
JB
14267}
14268
14269/* If DIE has a descriptive_type attribute, then set the TYPE's
14270 descriptive type accordingly. */
14271
14272static void
14273set_descriptive_type (struct type *type, struct die_info *die,
14274 struct dwarf2_cu *cu)
14275{
14276 struct type *descriptive_type = die_descriptive_type (die, cu);
14277
14278 if (descriptive_type)
14279 {
14280 ALLOCATE_GNAT_AUX_TYPE (type);
14281 TYPE_DESCRIPTIVE_TYPE (type) = descriptive_type;
14282 }
14283}
14284
c906108c
SS
14285/* Return the containing type of the die in question using its
14286 DW_AT_containing_type attribute. */
14287
14288static struct type *
e7c27a73 14289die_containing_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 14290{
c906108c 14291 struct attribute *type_attr;
c906108c 14292
e142c38c 14293 type_attr = dwarf2_attr (die, DW_AT_containing_type, cu);
33ac96f0
JK
14294 if (!type_attr)
14295 error (_("Dwarf Error: Problem turning containing type into gdb type "
14296 "[in module %s]"), cu->objfile->name);
14297
673bfd45 14298 return lookup_die_type (die, type_attr, cu);
c906108c
SS
14299}
14300
673bfd45
DE
14301/* Look up the type of DIE in CU using its type attribute ATTR.
14302 If there is no type substitute an error marker. */
14303
c906108c 14304static struct type *
673bfd45
DE
14305lookup_die_type (struct die_info *die, struct attribute *attr,
14306 struct dwarf2_cu *cu)
c906108c 14307{
bb5ed363 14308 struct objfile *objfile = cu->objfile;
f792889a
DJ
14309 struct type *this_type;
14310
673bfd45
DE
14311 /* First see if we have it cached. */
14312
14313 if (is_ref_attr (attr))
14314 {
b64f50a1 14315 sect_offset offset = dwarf2_get_ref_die_offset (attr);
673bfd45
DE
14316
14317 this_type = get_die_type_at_offset (offset, cu->per_cu);
14318 }
55f1336d 14319 else if (attr->form == DW_FORM_ref_sig8)
673bfd45
DE
14320 {
14321 struct signatured_type *sig_type = DW_SIGNATURED_TYPE (attr);
673bfd45
DE
14322
14323 /* sig_type will be NULL if the signatured type is missing from
14324 the debug info. */
14325 if (sig_type == NULL)
14326 error (_("Dwarf Error: Cannot find signatured DIE referenced from DIE "
14327 "at 0x%x [in module %s]"),
b64f50a1 14328 die->offset.sect_off, objfile->name);
673bfd45 14329
3019eac3
DE
14330 gdb_assert (sig_type->per_cu.is_debug_types);
14331 /* If we haven't filled in type_offset_in_section yet, then we
14332 haven't read the type in yet. */
14333 this_type = NULL;
14334 if (sig_type->type_offset_in_section.sect_off != 0)
14335 {
14336 this_type =
14337 get_die_type_at_offset (sig_type->type_offset_in_section,
14338 &sig_type->per_cu);
14339 }
673bfd45
DE
14340 }
14341 else
14342 {
14343 dump_die_for_error (die);
14344 error (_("Dwarf Error: Bad type attribute %s [in module %s]"),
bb5ed363 14345 dwarf_attr_name (attr->name), objfile->name);
673bfd45
DE
14346 }
14347
14348 /* If not cached we need to read it in. */
14349
14350 if (this_type == NULL)
14351 {
14352 struct die_info *type_die;
14353 struct dwarf2_cu *type_cu = cu;
14354
14355 type_die = follow_die_ref_or_sig (die, attr, &type_cu);
3019eac3
DE
14356 /* If we found the type now, it's probably because the type came
14357 from an inter-CU reference and the type's CU got expanded before
14358 ours. */
14359 this_type = get_die_type (type_die, type_cu);
14360 if (this_type == NULL)
14361 this_type = read_type_die_1 (type_die, type_cu);
673bfd45
DE
14362 }
14363
14364 /* If we still don't have a type use an error marker. */
14365
14366 if (this_type == NULL)
c906108c 14367 {
b00fdb78
TT
14368 char *message, *saved;
14369
14370 /* read_type_die already issued a complaint. */
14371 message = xstrprintf (_("<unknown type in %s, CU 0x%x, DIE 0x%x>"),
bb5ed363 14372 objfile->name,
b64f50a1
JK
14373 cu->header.offset.sect_off,
14374 die->offset.sect_off);
bb5ed363 14375 saved = obstack_copy0 (&objfile->objfile_obstack,
b00fdb78
TT
14376 message, strlen (message));
14377 xfree (message);
14378
bb5ed363 14379 this_type = init_type (TYPE_CODE_ERROR, 0, 0, saved, objfile);
c906108c 14380 }
673bfd45 14381
f792889a 14382 return this_type;
c906108c
SS
14383}
14384
673bfd45
DE
14385/* Return the type in DIE, CU.
14386 Returns NULL for invalid types.
14387
14388 This first does a lookup in the appropriate type_hash table,
14389 and only reads the die in if necessary.
14390
14391 NOTE: This can be called when reading in partial or full symbols. */
14392
f792889a 14393static struct type *
e7c27a73 14394read_type_die (struct die_info *die, struct dwarf2_cu *cu)
c906108c 14395{
f792889a
DJ
14396 struct type *this_type;
14397
14398 this_type = get_die_type (die, cu);
14399 if (this_type)
14400 return this_type;
14401
673bfd45
DE
14402 return read_type_die_1 (die, cu);
14403}
14404
14405/* Read the type in DIE, CU.
14406 Returns NULL for invalid types. */
14407
14408static struct type *
14409read_type_die_1 (struct die_info *die, struct dwarf2_cu *cu)
14410{
14411 struct type *this_type = NULL;
14412
c906108c
SS
14413 switch (die->tag)
14414 {
14415 case DW_TAG_class_type:
680b30c7 14416 case DW_TAG_interface_type:
c906108c
SS
14417 case DW_TAG_structure_type:
14418 case DW_TAG_union_type:
f792889a 14419 this_type = read_structure_type (die, cu);
c906108c
SS
14420 break;
14421 case DW_TAG_enumeration_type:
f792889a 14422 this_type = read_enumeration_type (die, cu);
c906108c
SS
14423 break;
14424 case DW_TAG_subprogram:
14425 case DW_TAG_subroutine_type:
edb3359d 14426 case DW_TAG_inlined_subroutine:
f792889a 14427 this_type = read_subroutine_type (die, cu);
c906108c
SS
14428 break;
14429 case DW_TAG_array_type:
f792889a 14430 this_type = read_array_type (die, cu);
c906108c 14431 break;
72019c9c 14432 case DW_TAG_set_type:
f792889a 14433 this_type = read_set_type (die, cu);
72019c9c 14434 break;
c906108c 14435 case DW_TAG_pointer_type:
f792889a 14436 this_type = read_tag_pointer_type (die, cu);
c906108c
SS
14437 break;
14438 case DW_TAG_ptr_to_member_type:
f792889a 14439 this_type = read_tag_ptr_to_member_type (die, cu);
c906108c
SS
14440 break;
14441 case DW_TAG_reference_type:
f792889a 14442 this_type = read_tag_reference_type (die, cu);
c906108c
SS
14443 break;
14444 case DW_TAG_const_type:
f792889a 14445 this_type = read_tag_const_type (die, cu);
c906108c
SS
14446 break;
14447 case DW_TAG_volatile_type:
f792889a 14448 this_type = read_tag_volatile_type (die, cu);
c906108c
SS
14449 break;
14450 case DW_TAG_string_type:
f792889a 14451 this_type = read_tag_string_type (die, cu);
c906108c
SS
14452 break;
14453 case DW_TAG_typedef:
f792889a 14454 this_type = read_typedef (die, cu);
c906108c 14455 break;
a02abb62 14456 case DW_TAG_subrange_type:
f792889a 14457 this_type = read_subrange_type (die, cu);
a02abb62 14458 break;
c906108c 14459 case DW_TAG_base_type:
f792889a 14460 this_type = read_base_type (die, cu);
c906108c 14461 break;
81a17f79 14462 case DW_TAG_unspecified_type:
f792889a 14463 this_type = read_unspecified_type (die, cu);
81a17f79 14464 break;
0114d602
DJ
14465 case DW_TAG_namespace:
14466 this_type = read_namespace_type (die, cu);
14467 break;
f55ee35c
JK
14468 case DW_TAG_module:
14469 this_type = read_module_type (die, cu);
14470 break;
c906108c 14471 default:
3e43a32a
MS
14472 complaint (&symfile_complaints,
14473 _("unexpected tag in read_type_die: '%s'"),
4d3c2250 14474 dwarf_tag_name (die->tag));
c906108c
SS
14475 break;
14476 }
63d06c5c 14477
f792889a 14478 return this_type;
63d06c5c
DC
14479}
14480
abc72ce4
DE
14481/* See if we can figure out if the class lives in a namespace. We do
14482 this by looking for a member function; its demangled name will
14483 contain namespace info, if there is any.
14484 Return the computed name or NULL.
14485 Space for the result is allocated on the objfile's obstack.
14486 This is the full-die version of guess_partial_die_structure_name.
14487 In this case we know DIE has no useful parent. */
14488
14489static char *
14490guess_full_die_structure_name (struct die_info *die, struct dwarf2_cu *cu)
14491{
14492 struct die_info *spec_die;
14493 struct dwarf2_cu *spec_cu;
14494 struct die_info *child;
14495
14496 spec_cu = cu;
14497 spec_die = die_specification (die, &spec_cu);
14498 if (spec_die != NULL)
14499 {
14500 die = spec_die;
14501 cu = spec_cu;
14502 }
14503
14504 for (child = die->child;
14505 child != NULL;
14506 child = child->sibling)
14507 {
14508 if (child->tag == DW_TAG_subprogram)
14509 {
14510 struct attribute *attr;
14511
14512 attr = dwarf2_attr (child, DW_AT_linkage_name, cu);
14513 if (attr == NULL)
14514 attr = dwarf2_attr (child, DW_AT_MIPS_linkage_name, cu);
14515 if (attr != NULL)
14516 {
14517 char *actual_name
14518 = language_class_name_from_physname (cu->language_defn,
14519 DW_STRING (attr));
14520 char *name = NULL;
14521
14522 if (actual_name != NULL)
14523 {
14524 char *die_name = dwarf2_name (die, cu);
14525
14526 if (die_name != NULL
14527 && strcmp (die_name, actual_name) != 0)
14528 {
14529 /* Strip off the class name from the full name.
14530 We want the prefix. */
14531 int die_name_len = strlen (die_name);
14532 int actual_name_len = strlen (actual_name);
14533
14534 /* Test for '::' as a sanity check. */
14535 if (actual_name_len > die_name_len + 2
3e43a32a
MS
14536 && actual_name[actual_name_len
14537 - die_name_len - 1] == ':')
abc72ce4
DE
14538 name =
14539 obsavestring (actual_name,
14540 actual_name_len - die_name_len - 2,
14541 &cu->objfile->objfile_obstack);
14542 }
14543 }
14544 xfree (actual_name);
14545 return name;
14546 }
14547 }
14548 }
14549
14550 return NULL;
14551}
14552
96408a79
SA
14553/* GCC might emit a nameless typedef that has a linkage name. Determine the
14554 prefix part in such case. See
14555 http://gcc.gnu.org/bugzilla/show_bug.cgi?id=47510. */
14556
14557static char *
14558anonymous_struct_prefix (struct die_info *die, struct dwarf2_cu *cu)
14559{
14560 struct attribute *attr;
14561 char *base;
14562
14563 if (die->tag != DW_TAG_class_type && die->tag != DW_TAG_interface_type
14564 && die->tag != DW_TAG_structure_type && die->tag != DW_TAG_union_type)
14565 return NULL;
14566
14567 attr = dwarf2_attr (die, DW_AT_name, cu);
14568 if (attr != NULL && DW_STRING (attr) != NULL)
14569 return NULL;
14570
14571 attr = dwarf2_attr (die, DW_AT_linkage_name, cu);
14572 if (attr == NULL)
14573 attr = dwarf2_attr (die, DW_AT_MIPS_linkage_name, cu);
14574 if (attr == NULL || DW_STRING (attr) == NULL)
14575 return NULL;
14576
14577 /* dwarf2_name had to be already called. */
14578 gdb_assert (DW_STRING_IS_CANONICAL (attr));
14579
14580 /* Strip the base name, keep any leading namespaces/classes. */
14581 base = strrchr (DW_STRING (attr), ':');
14582 if (base == NULL || base == DW_STRING (attr) || base[-1] != ':')
14583 return "";
14584
14585 return obsavestring (DW_STRING (attr), &base[-1] - DW_STRING (attr),
14586 &cu->objfile->objfile_obstack);
14587}
14588
fdde2d81 14589/* Return the name of the namespace/class that DIE is defined within,
0114d602 14590 or "" if we can't tell. The caller should not xfree the result.
fdde2d81 14591
0114d602
DJ
14592 For example, if we're within the method foo() in the following
14593 code:
14594
14595 namespace N {
14596 class C {
14597 void foo () {
14598 }
14599 };
14600 }
14601
14602 then determine_prefix on foo's die will return "N::C". */
fdde2d81 14603
0d5cff50 14604static const char *
e142c38c 14605determine_prefix (struct die_info *die, struct dwarf2_cu *cu)
63d06c5c 14606{
0114d602
DJ
14607 struct die_info *parent, *spec_die;
14608 struct dwarf2_cu *spec_cu;
14609 struct type *parent_type;
96408a79 14610 char *retval;
63d06c5c 14611
f55ee35c
JK
14612 if (cu->language != language_cplus && cu->language != language_java
14613 && cu->language != language_fortran)
0114d602
DJ
14614 return "";
14615
96408a79
SA
14616 retval = anonymous_struct_prefix (die, cu);
14617 if (retval)
14618 return retval;
14619
0114d602
DJ
14620 /* We have to be careful in the presence of DW_AT_specification.
14621 For example, with GCC 3.4, given the code
14622
14623 namespace N {
14624 void foo() {
14625 // Definition of N::foo.
14626 }
14627 }
14628
14629 then we'll have a tree of DIEs like this:
14630
14631 1: DW_TAG_compile_unit
14632 2: DW_TAG_namespace // N
14633 3: DW_TAG_subprogram // declaration of N::foo
14634 4: DW_TAG_subprogram // definition of N::foo
14635 DW_AT_specification // refers to die #3
14636
14637 Thus, when processing die #4, we have to pretend that we're in
14638 the context of its DW_AT_specification, namely the contex of die
14639 #3. */
14640 spec_cu = cu;
14641 spec_die = die_specification (die, &spec_cu);
14642 if (spec_die == NULL)
14643 parent = die->parent;
14644 else
63d06c5c 14645 {
0114d602
DJ
14646 parent = spec_die->parent;
14647 cu = spec_cu;
63d06c5c 14648 }
0114d602
DJ
14649
14650 if (parent == NULL)
14651 return "";
98bfdba5
PA
14652 else if (parent->building_fullname)
14653 {
14654 const char *name;
14655 const char *parent_name;
14656
14657 /* It has been seen on RealView 2.2 built binaries,
14658 DW_TAG_template_type_param types actually _defined_ as
14659 children of the parent class:
14660
14661 enum E {};
14662 template class <class Enum> Class{};
14663 Class<enum E> class_e;
14664
14665 1: DW_TAG_class_type (Class)
14666 2: DW_TAG_enumeration_type (E)
14667 3: DW_TAG_enumerator (enum1:0)
14668 3: DW_TAG_enumerator (enum2:1)
14669 ...
14670 2: DW_TAG_template_type_param
14671 DW_AT_type DW_FORM_ref_udata (E)
14672
14673 Besides being broken debug info, it can put GDB into an
14674 infinite loop. Consider:
14675
14676 When we're building the full name for Class<E>, we'll start
14677 at Class, and go look over its template type parameters,
14678 finding E. We'll then try to build the full name of E, and
14679 reach here. We're now trying to build the full name of E,
14680 and look over the parent DIE for containing scope. In the
14681 broken case, if we followed the parent DIE of E, we'd again
14682 find Class, and once again go look at its template type
14683 arguments, etc., etc. Simply don't consider such parent die
14684 as source-level parent of this die (it can't be, the language
14685 doesn't allow it), and break the loop here. */
14686 name = dwarf2_name (die, cu);
14687 parent_name = dwarf2_name (parent, cu);
14688 complaint (&symfile_complaints,
14689 _("template param type '%s' defined within parent '%s'"),
14690 name ? name : "<unknown>",
14691 parent_name ? parent_name : "<unknown>");
14692 return "";
14693 }
63d06c5c 14694 else
0114d602
DJ
14695 switch (parent->tag)
14696 {
63d06c5c 14697 case DW_TAG_namespace:
0114d602 14698 parent_type = read_type_die (parent, cu);
acebe513
UW
14699 /* GCC 4.0 and 4.1 had a bug (PR c++/28460) where they generated bogus
14700 DW_TAG_namespace DIEs with a name of "::" for the global namespace.
14701 Work around this problem here. */
14702 if (cu->language == language_cplus
14703 && strcmp (TYPE_TAG_NAME (parent_type), "::") == 0)
14704 return "";
0114d602
DJ
14705 /* We give a name to even anonymous namespaces. */
14706 return TYPE_TAG_NAME (parent_type);
63d06c5c 14707 case DW_TAG_class_type:
680b30c7 14708 case DW_TAG_interface_type:
63d06c5c 14709 case DW_TAG_structure_type:
0114d602 14710 case DW_TAG_union_type:
f55ee35c 14711 case DW_TAG_module:
0114d602
DJ
14712 parent_type = read_type_die (parent, cu);
14713 if (TYPE_TAG_NAME (parent_type) != NULL)
14714 return TYPE_TAG_NAME (parent_type);
14715 else
14716 /* An anonymous structure is only allowed non-static data
14717 members; no typedefs, no member functions, et cetera.
14718 So it does not need a prefix. */
14719 return "";
abc72ce4 14720 case DW_TAG_compile_unit:
95554aad 14721 case DW_TAG_partial_unit:
abc72ce4
DE
14722 /* gcc-4.5 -gdwarf-4 can drop the enclosing namespace. Cope. */
14723 if (cu->language == language_cplus
8b70b953 14724 && !VEC_empty (dwarf2_section_info_def, dwarf2_per_objfile->types)
abc72ce4
DE
14725 && die->child != NULL
14726 && (die->tag == DW_TAG_class_type
14727 || die->tag == DW_TAG_structure_type
14728 || die->tag == DW_TAG_union_type))
14729 {
14730 char *name = guess_full_die_structure_name (die, cu);
14731 if (name != NULL)
14732 return name;
14733 }
14734 return "";
63d06c5c 14735 default:
8176b9b8 14736 return determine_prefix (parent, cu);
63d06c5c 14737 }
63d06c5c
DC
14738}
14739
3e43a32a
MS
14740/* Return a newly-allocated string formed by concatenating PREFIX and SUFFIX
14741 with appropriate separator. If PREFIX or SUFFIX is NULL or empty, then
14742 simply copy the SUFFIX or PREFIX, respectively. If OBS is non-null, perform
14743 an obconcat, otherwise allocate storage for the result. The CU argument is
14744 used to determine the language and hence, the appropriate separator. */
987504bb 14745
f55ee35c 14746#define MAX_SEP_LEN 7 /* strlen ("__") + strlen ("_MOD_") */
63d06c5c
DC
14747
14748static char *
f55ee35c
JK
14749typename_concat (struct obstack *obs, const char *prefix, const char *suffix,
14750 int physname, struct dwarf2_cu *cu)
63d06c5c 14751{
f55ee35c 14752 const char *lead = "";
5c315b68 14753 const char *sep;
63d06c5c 14754
3e43a32a
MS
14755 if (suffix == NULL || suffix[0] == '\0'
14756 || prefix == NULL || prefix[0] == '\0')
987504bb
JJ
14757 sep = "";
14758 else if (cu->language == language_java)
14759 sep = ".";
f55ee35c
JK
14760 else if (cu->language == language_fortran && physname)
14761 {
14762 /* This is gfortran specific mangling. Normally DW_AT_linkage_name or
14763 DW_AT_MIPS_linkage_name is preferred and used instead. */
14764
14765 lead = "__";
14766 sep = "_MOD_";
14767 }
987504bb
JJ
14768 else
14769 sep = "::";
63d06c5c 14770
6dd47d34
DE
14771 if (prefix == NULL)
14772 prefix = "";
14773 if (suffix == NULL)
14774 suffix = "";
14775
987504bb
JJ
14776 if (obs == NULL)
14777 {
3e43a32a
MS
14778 char *retval
14779 = xmalloc (strlen (prefix) + MAX_SEP_LEN + strlen (suffix) + 1);
9a619af0 14780
f55ee35c
JK
14781 strcpy (retval, lead);
14782 strcat (retval, prefix);
6dd47d34
DE
14783 strcat (retval, sep);
14784 strcat (retval, suffix);
63d06c5c
DC
14785 return retval;
14786 }
987504bb
JJ
14787 else
14788 {
14789 /* We have an obstack. */
f55ee35c 14790 return obconcat (obs, lead, prefix, sep, suffix, (char *) NULL);
987504bb 14791 }
63d06c5c
DC
14792}
14793
c906108c
SS
14794/* Return sibling of die, NULL if no sibling. */
14795
f9aca02d 14796static struct die_info *
fba45db2 14797sibling_die (struct die_info *die)
c906108c 14798{
639d11d3 14799 return die->sibling;
c906108c
SS
14800}
14801
71c25dea
TT
14802/* Get name of a die, return NULL if not found. */
14803
14804static char *
14805dwarf2_canonicalize_name (char *name, struct dwarf2_cu *cu,
14806 struct obstack *obstack)
14807{
14808 if (name && cu->language == language_cplus)
14809 {
14810 char *canon_name = cp_canonicalize_string (name);
14811
14812 if (canon_name != NULL)
14813 {
14814 if (strcmp (canon_name, name) != 0)
14815 name = obsavestring (canon_name, strlen (canon_name),
14816 obstack);
14817 xfree (canon_name);
14818 }
14819 }
14820
14821 return name;
c906108c
SS
14822}
14823
9219021c
DC
14824/* Get name of a die, return NULL if not found. */
14825
14826static char *
e142c38c 14827dwarf2_name (struct die_info *die, struct dwarf2_cu *cu)
9219021c
DC
14828{
14829 struct attribute *attr;
14830
e142c38c 14831 attr = dwarf2_attr (die, DW_AT_name, cu);
53832f31
TT
14832 if ((!attr || !DW_STRING (attr))
14833 && die->tag != DW_TAG_class_type
14834 && die->tag != DW_TAG_interface_type
14835 && die->tag != DW_TAG_structure_type
14836 && die->tag != DW_TAG_union_type)
71c25dea
TT
14837 return NULL;
14838
14839 switch (die->tag)
14840 {
14841 case DW_TAG_compile_unit:
95554aad 14842 case DW_TAG_partial_unit:
71c25dea
TT
14843 /* Compilation units have a DW_AT_name that is a filename, not
14844 a source language identifier. */
14845 case DW_TAG_enumeration_type:
14846 case DW_TAG_enumerator:
14847 /* These tags always have simple identifiers already; no need
14848 to canonicalize them. */
14849 return DW_STRING (attr);
907af001 14850
418835cc
KS
14851 case DW_TAG_subprogram:
14852 /* Java constructors will all be named "<init>", so return
14853 the class name when we see this special case. */
14854 if (cu->language == language_java
14855 && DW_STRING (attr) != NULL
14856 && strcmp (DW_STRING (attr), "<init>") == 0)
14857 {
14858 struct dwarf2_cu *spec_cu = cu;
14859 struct die_info *spec_die;
14860
14861 /* GCJ will output '<init>' for Java constructor names.
14862 For this special case, return the name of the parent class. */
14863
14864 /* GCJ may output suprogram DIEs with AT_specification set.
14865 If so, use the name of the specified DIE. */
14866 spec_die = die_specification (die, &spec_cu);
14867 if (spec_die != NULL)
14868 return dwarf2_name (spec_die, spec_cu);
14869
14870 do
14871 {
14872 die = die->parent;
14873 if (die->tag == DW_TAG_class_type)
14874 return dwarf2_name (die, cu);
14875 }
95554aad
TT
14876 while (die->tag != DW_TAG_compile_unit
14877 && die->tag != DW_TAG_partial_unit);
418835cc 14878 }
907af001
UW
14879 break;
14880
14881 case DW_TAG_class_type:
14882 case DW_TAG_interface_type:
14883 case DW_TAG_structure_type:
14884 case DW_TAG_union_type:
14885 /* Some GCC versions emit spurious DW_AT_name attributes for unnamed
14886 structures or unions. These were of the form "._%d" in GCC 4.1,
14887 or simply "<anonymous struct>" or "<anonymous union>" in GCC 4.3
14888 and GCC 4.4. We work around this problem by ignoring these. */
53832f31
TT
14889 if (attr && DW_STRING (attr)
14890 && (strncmp (DW_STRING (attr), "._", 2) == 0
14891 || strncmp (DW_STRING (attr), "<anonymous", 10) == 0))
907af001 14892 return NULL;
53832f31
TT
14893
14894 /* GCC might emit a nameless typedef that has a linkage name. See
14895 http://gcc.gnu.org/bugzilla/show_bug.cgi?id=47510. */
14896 if (!attr || DW_STRING (attr) == NULL)
14897 {
df5c6c50 14898 char *demangled = NULL;
53832f31
TT
14899
14900 attr = dwarf2_attr (die, DW_AT_linkage_name, cu);
14901 if (attr == NULL)
14902 attr = dwarf2_attr (die, DW_AT_MIPS_linkage_name, cu);
14903
14904 if (attr == NULL || DW_STRING (attr) == NULL)
14905 return NULL;
14906
df5c6c50
JK
14907 /* Avoid demangling DW_STRING (attr) the second time on a second
14908 call for the same DIE. */
14909 if (!DW_STRING_IS_CANONICAL (attr))
14910 demangled = cplus_demangle (DW_STRING (attr), DMGL_TYPES);
53832f31
TT
14911
14912 if (demangled)
14913 {
96408a79
SA
14914 char *base;
14915
53832f31 14916 /* FIXME: we already did this for the partial symbol... */
96408a79
SA
14917 DW_STRING (attr) = obsavestring (demangled, strlen (demangled),
14918 &cu->objfile->objfile_obstack);
53832f31
TT
14919 DW_STRING_IS_CANONICAL (attr) = 1;
14920 xfree (demangled);
96408a79
SA
14921
14922 /* Strip any leading namespaces/classes, keep only the base name.
14923 DW_AT_name for named DIEs does not contain the prefixes. */
14924 base = strrchr (DW_STRING (attr), ':');
14925 if (base && base > DW_STRING (attr) && base[-1] == ':')
14926 return &base[1];
14927 else
14928 return DW_STRING (attr);
53832f31
TT
14929 }
14930 }
907af001
UW
14931 break;
14932
71c25dea 14933 default:
907af001
UW
14934 break;
14935 }
14936
14937 if (!DW_STRING_IS_CANONICAL (attr))
14938 {
14939 DW_STRING (attr)
14940 = dwarf2_canonicalize_name (DW_STRING (attr), cu,
14941 &cu->objfile->objfile_obstack);
14942 DW_STRING_IS_CANONICAL (attr) = 1;
71c25dea 14943 }
907af001 14944 return DW_STRING (attr);
9219021c
DC
14945}
14946
14947/* Return the die that this die in an extension of, or NULL if there
f2f0e013
DJ
14948 is none. *EXT_CU is the CU containing DIE on input, and the CU
14949 containing the return value on output. */
9219021c
DC
14950
14951static struct die_info *
f2f0e013 14952dwarf2_extension (struct die_info *die, struct dwarf2_cu **ext_cu)
9219021c
DC
14953{
14954 struct attribute *attr;
9219021c 14955
f2f0e013 14956 attr = dwarf2_attr (die, DW_AT_extension, *ext_cu);
9219021c
DC
14957 if (attr == NULL)
14958 return NULL;
14959
f2f0e013 14960 return follow_die_ref (die, attr, ext_cu);
9219021c
DC
14961}
14962
c906108c
SS
14963/* Convert a DIE tag into its string name. */
14964
f39c6ffd 14965static const char *
aa1ee363 14966dwarf_tag_name (unsigned tag)
c906108c 14967{
f39c6ffd
TT
14968 const char *name = get_DW_TAG_name (tag);
14969
14970 if (name == NULL)
14971 return "DW_TAG_<unknown>";
14972
14973 return name;
c906108c
SS
14974}
14975
14976/* Convert a DWARF attribute code into its string name. */
14977
f39c6ffd 14978static const char *
aa1ee363 14979dwarf_attr_name (unsigned attr)
c906108c 14980{
f39c6ffd
TT
14981 const char *name;
14982
c764a876 14983#ifdef MIPS /* collides with DW_AT_HP_block_index */
f39c6ffd
TT
14984 if (attr == DW_AT_MIPS_fde)
14985 return "DW_AT_MIPS_fde";
14986#else
14987 if (attr == DW_AT_HP_block_index)
14988 return "DW_AT_HP_block_index";
c764a876 14989#endif
f39c6ffd
TT
14990
14991 name = get_DW_AT_name (attr);
14992
14993 if (name == NULL)
14994 return "DW_AT_<unknown>";
14995
14996 return name;
c906108c
SS
14997}
14998
14999/* Convert a DWARF value form code into its string name. */
15000
f39c6ffd 15001static const char *
aa1ee363 15002dwarf_form_name (unsigned form)
c906108c 15003{
f39c6ffd
TT
15004 const char *name = get_DW_FORM_name (form);
15005
15006 if (name == NULL)
15007 return "DW_FORM_<unknown>";
15008
15009 return name;
c906108c
SS
15010}
15011
15012static char *
fba45db2 15013dwarf_bool_name (unsigned mybool)
c906108c
SS
15014{
15015 if (mybool)
15016 return "TRUE";
15017 else
15018 return "FALSE";
15019}
15020
15021/* Convert a DWARF type code into its string name. */
15022
f39c6ffd 15023static const char *
aa1ee363 15024dwarf_type_encoding_name (unsigned enc)
c906108c 15025{
f39c6ffd 15026 const char *name = get_DW_ATE_name (enc);
c906108c 15027
f39c6ffd
TT
15028 if (name == NULL)
15029 return "DW_ATE_<unknown>";
c906108c 15030
f39c6ffd 15031 return name;
c906108c 15032}
c906108c 15033
f9aca02d 15034static void
d97bc12b 15035dump_die_shallow (struct ui_file *f, int indent, struct die_info *die)
c906108c
SS
15036{
15037 unsigned int i;
15038
d97bc12b
DE
15039 print_spaces (indent, f);
15040 fprintf_unfiltered (f, "Die: %s (abbrev %d, offset 0x%x)\n",
b64f50a1 15041 dwarf_tag_name (die->tag), die->abbrev, die->offset.sect_off);
d97bc12b
DE
15042
15043 if (die->parent != NULL)
15044 {
15045 print_spaces (indent, f);
15046 fprintf_unfiltered (f, " parent at offset: 0x%x\n",
b64f50a1 15047 die->parent->offset.sect_off);
d97bc12b
DE
15048 }
15049
15050 print_spaces (indent, f);
15051 fprintf_unfiltered (f, " has children: %s\n",
639d11d3 15052 dwarf_bool_name (die->child != NULL));
c906108c 15053
d97bc12b
DE
15054 print_spaces (indent, f);
15055 fprintf_unfiltered (f, " attributes:\n");
15056
c906108c
SS
15057 for (i = 0; i < die->num_attrs; ++i)
15058 {
d97bc12b
DE
15059 print_spaces (indent, f);
15060 fprintf_unfiltered (f, " %s (%s) ",
c906108c
SS
15061 dwarf_attr_name (die->attrs[i].name),
15062 dwarf_form_name (die->attrs[i].form));
d97bc12b 15063
c906108c
SS
15064 switch (die->attrs[i].form)
15065 {
c906108c 15066 case DW_FORM_addr:
3019eac3 15067 case DW_FORM_GNU_addr_index:
d97bc12b 15068 fprintf_unfiltered (f, "address: ");
5af949e3 15069 fputs_filtered (hex_string (DW_ADDR (&die->attrs[i])), f);
c906108c
SS
15070 break;
15071 case DW_FORM_block2:
15072 case DW_FORM_block4:
15073 case DW_FORM_block:
15074 case DW_FORM_block1:
3e43a32a
MS
15075 fprintf_unfiltered (f, "block: size %d",
15076 DW_BLOCK (&die->attrs[i])->size);
c906108c 15077 break;
2dc7f7b3
TT
15078 case DW_FORM_exprloc:
15079 fprintf_unfiltered (f, "expression: size %u",
15080 DW_BLOCK (&die->attrs[i])->size);
15081 break;
4568ecf9
DE
15082 case DW_FORM_ref_addr:
15083 fprintf_unfiltered (f, "ref address: ");
15084 fputs_filtered (hex_string (DW_UNSND (&die->attrs[i])), f);
15085 break;
10b3939b
DJ
15086 case DW_FORM_ref1:
15087 case DW_FORM_ref2:
15088 case DW_FORM_ref4:
4568ecf9
DE
15089 case DW_FORM_ref8:
15090 case DW_FORM_ref_udata:
d97bc12b 15091 fprintf_unfiltered (f, "constant ref: 0x%lx (adjusted)",
4568ecf9 15092 (long) (DW_UNSND (&die->attrs[i])));
10b3939b 15093 break;
c906108c
SS
15094 case DW_FORM_data1:
15095 case DW_FORM_data2:
15096 case DW_FORM_data4:
ce5d95e1 15097 case DW_FORM_data8:
c906108c
SS
15098 case DW_FORM_udata:
15099 case DW_FORM_sdata:
43bbcdc2
PH
15100 fprintf_unfiltered (f, "constant: %s",
15101 pulongest (DW_UNSND (&die->attrs[i])));
c906108c 15102 break;
2dc7f7b3
TT
15103 case DW_FORM_sec_offset:
15104 fprintf_unfiltered (f, "section offset: %s",
15105 pulongest (DW_UNSND (&die->attrs[i])));
15106 break;
55f1336d 15107 case DW_FORM_ref_sig8:
348e048f
DE
15108 if (DW_SIGNATURED_TYPE (&die->attrs[i]) != NULL)
15109 fprintf_unfiltered (f, "signatured type, offset: 0x%x",
b64f50a1 15110 DW_SIGNATURED_TYPE (&die->attrs[i])->per_cu.offset.sect_off);
348e048f
DE
15111 else
15112 fprintf_unfiltered (f, "signatured type, offset: unknown");
15113 break;
c906108c 15114 case DW_FORM_string:
4bdf3d34 15115 case DW_FORM_strp:
3019eac3 15116 case DW_FORM_GNU_str_index:
8285870a 15117 fprintf_unfiltered (f, "string: \"%s\" (%s canonicalized)",
c906108c 15118 DW_STRING (&die->attrs[i])
8285870a
JK
15119 ? DW_STRING (&die->attrs[i]) : "",
15120 DW_STRING_IS_CANONICAL (&die->attrs[i]) ? "is" : "not");
c906108c
SS
15121 break;
15122 case DW_FORM_flag:
15123 if (DW_UNSND (&die->attrs[i]))
d97bc12b 15124 fprintf_unfiltered (f, "flag: TRUE");
c906108c 15125 else
d97bc12b 15126 fprintf_unfiltered (f, "flag: FALSE");
c906108c 15127 break;
2dc7f7b3
TT
15128 case DW_FORM_flag_present:
15129 fprintf_unfiltered (f, "flag: TRUE");
15130 break;
a8329558 15131 case DW_FORM_indirect:
0963b4bd
MS
15132 /* The reader will have reduced the indirect form to
15133 the "base form" so this form should not occur. */
3e43a32a
MS
15134 fprintf_unfiltered (f,
15135 "unexpected attribute form: DW_FORM_indirect");
a8329558 15136 break;
c906108c 15137 default:
d97bc12b 15138 fprintf_unfiltered (f, "unsupported attribute form: %d.",
c5aa993b 15139 die->attrs[i].form);
d97bc12b 15140 break;
c906108c 15141 }
d97bc12b 15142 fprintf_unfiltered (f, "\n");
c906108c
SS
15143 }
15144}
15145
f9aca02d 15146static void
d97bc12b 15147dump_die_for_error (struct die_info *die)
c906108c 15148{
d97bc12b
DE
15149 dump_die_shallow (gdb_stderr, 0, die);
15150}
15151
15152static void
15153dump_die_1 (struct ui_file *f, int level, int max_level, struct die_info *die)
15154{
15155 int indent = level * 4;
15156
15157 gdb_assert (die != NULL);
15158
15159 if (level >= max_level)
15160 return;
15161
15162 dump_die_shallow (f, indent, die);
15163
15164 if (die->child != NULL)
c906108c 15165 {
d97bc12b
DE
15166 print_spaces (indent, f);
15167 fprintf_unfiltered (f, " Children:");
15168 if (level + 1 < max_level)
15169 {
15170 fprintf_unfiltered (f, "\n");
15171 dump_die_1 (f, level + 1, max_level, die->child);
15172 }
15173 else
15174 {
3e43a32a
MS
15175 fprintf_unfiltered (f,
15176 " [not printed, max nesting level reached]\n");
d97bc12b
DE
15177 }
15178 }
15179
15180 if (die->sibling != NULL && level > 0)
15181 {
15182 dump_die_1 (f, level, max_level, die->sibling);
c906108c
SS
15183 }
15184}
15185
d97bc12b
DE
15186/* This is called from the pdie macro in gdbinit.in.
15187 It's not static so gcc will keep a copy callable from gdb. */
15188
15189void
15190dump_die (struct die_info *die, int max_level)
15191{
15192 dump_die_1 (gdb_stdlog, 0, max_level, die);
15193}
15194
f9aca02d 15195static void
51545339 15196store_in_ref_table (struct die_info *die, struct dwarf2_cu *cu)
c906108c 15197{
51545339 15198 void **slot;
c906108c 15199
b64f50a1
JK
15200 slot = htab_find_slot_with_hash (cu->die_hash, die, die->offset.sect_off,
15201 INSERT);
51545339
DJ
15202
15203 *slot = die;
c906108c
SS
15204}
15205
b64f50a1
JK
15206/* DW_ADDR is always stored already as sect_offset; despite for the forms
15207 besides DW_FORM_ref_addr it is stored as cu_offset in the DWARF file. */
15208
93311388
DE
15209static int
15210is_ref_attr (struct attribute *attr)
c906108c 15211{
c906108c
SS
15212 switch (attr->form)
15213 {
15214 case DW_FORM_ref_addr:
c906108c
SS
15215 case DW_FORM_ref1:
15216 case DW_FORM_ref2:
15217 case DW_FORM_ref4:
613e1657 15218 case DW_FORM_ref8:
c906108c 15219 case DW_FORM_ref_udata:
93311388 15220 return 1;
c906108c 15221 default:
93311388 15222 return 0;
c906108c 15223 }
93311388
DE
15224}
15225
b64f50a1
JK
15226/* Return DIE offset of ATTR. Return 0 with complaint if ATTR is not of the
15227 required kind. */
15228
15229static sect_offset
93311388
DE
15230dwarf2_get_ref_die_offset (struct attribute *attr)
15231{
4568ecf9 15232 sect_offset retval = { DW_UNSND (attr) };
b64f50a1 15233
93311388 15234 if (is_ref_attr (attr))
b64f50a1 15235 return retval;
93311388 15236
b64f50a1 15237 retval.sect_off = 0;
93311388
DE
15238 complaint (&symfile_complaints,
15239 _("unsupported die ref attribute form: '%s'"),
15240 dwarf_form_name (attr->form));
b64f50a1 15241 return retval;
c906108c
SS
15242}
15243
43bbcdc2
PH
15244/* Return the constant value held by ATTR. Return DEFAULT_VALUE if
15245 * the value held by the attribute is not constant. */
a02abb62 15246
43bbcdc2 15247static LONGEST
a02abb62
JB
15248dwarf2_get_attr_constant_value (struct attribute *attr, int default_value)
15249{
15250 if (attr->form == DW_FORM_sdata)
15251 return DW_SND (attr);
15252 else if (attr->form == DW_FORM_udata
15253 || attr->form == DW_FORM_data1
15254 || attr->form == DW_FORM_data2
15255 || attr->form == DW_FORM_data4
15256 || attr->form == DW_FORM_data8)
15257 return DW_UNSND (attr);
15258 else
15259 {
3e43a32a
MS
15260 complaint (&symfile_complaints,
15261 _("Attribute value is not a constant (%s)"),
a02abb62
JB
15262 dwarf_form_name (attr->form));
15263 return default_value;
15264 }
15265}
15266
348e048f
DE
15267/* Follow reference or signature attribute ATTR of SRC_DIE.
15268 On entry *REF_CU is the CU of SRC_DIE.
15269 On exit *REF_CU is the CU of the result. */
15270
15271static struct die_info *
15272follow_die_ref_or_sig (struct die_info *src_die, struct attribute *attr,
15273 struct dwarf2_cu **ref_cu)
15274{
15275 struct die_info *die;
15276
15277 if (is_ref_attr (attr))
15278 die = follow_die_ref (src_die, attr, ref_cu);
55f1336d 15279 else if (attr->form == DW_FORM_ref_sig8)
348e048f
DE
15280 die = follow_die_sig (src_die, attr, ref_cu);
15281 else
15282 {
15283 dump_die_for_error (src_die);
15284 error (_("Dwarf Error: Expected reference attribute [in module %s]"),
15285 (*ref_cu)->objfile->name);
15286 }
15287
15288 return die;
03dd20cc
DJ
15289}
15290
5c631832 15291/* Follow reference OFFSET.
673bfd45
DE
15292 On entry *REF_CU is the CU of the source die referencing OFFSET.
15293 On exit *REF_CU is the CU of the result.
15294 Returns NULL if OFFSET is invalid. */
f504f079 15295
f9aca02d 15296static struct die_info *
b64f50a1 15297follow_die_offset (sect_offset offset, struct dwarf2_cu **ref_cu)
c906108c 15298{
10b3939b 15299 struct die_info temp_die;
f2f0e013 15300 struct dwarf2_cu *target_cu, *cu = *ref_cu;
10b3939b 15301
348e048f
DE
15302 gdb_assert (cu->per_cu != NULL);
15303
98bfdba5
PA
15304 target_cu = cu;
15305
3019eac3 15306 if (cu->per_cu->is_debug_types)
348e048f
DE
15307 {
15308 /* .debug_types CUs cannot reference anything outside their CU.
15309 If they need to, they have to reference a signatured type via
55f1336d 15310 DW_FORM_ref_sig8. */
348e048f 15311 if (! offset_in_cu_p (&cu->header, offset))
5c631832 15312 return NULL;
348e048f
DE
15313 }
15314 else if (! offset_in_cu_p (&cu->header, offset))
10b3939b
DJ
15315 {
15316 struct dwarf2_per_cu_data *per_cu;
9a619af0 15317
45452591 15318 per_cu = dwarf2_find_containing_comp_unit (offset, cu->objfile);
03dd20cc
DJ
15319
15320 /* If necessary, add it to the queue and load its DIEs. */
95554aad
TT
15321 if (maybe_queue_comp_unit (cu, per_cu, cu->language))
15322 load_full_comp_unit (per_cu, cu->language);
03dd20cc 15323
10b3939b
DJ
15324 target_cu = per_cu->cu;
15325 }
98bfdba5
PA
15326 else if (cu->dies == NULL)
15327 {
15328 /* We're loading full DIEs during partial symbol reading. */
15329 gdb_assert (dwarf2_per_objfile->reading_partial_symbols);
95554aad 15330 load_full_comp_unit (cu->per_cu, language_minimal);
98bfdba5 15331 }
c906108c 15332
f2f0e013 15333 *ref_cu = target_cu;
51545339 15334 temp_die.offset = offset;
b64f50a1 15335 return htab_find_with_hash (target_cu->die_hash, &temp_die, offset.sect_off);
5c631832 15336}
10b3939b 15337
5c631832
JK
15338/* Follow reference attribute ATTR of SRC_DIE.
15339 On entry *REF_CU is the CU of SRC_DIE.
15340 On exit *REF_CU is the CU of the result. */
15341
15342static struct die_info *
15343follow_die_ref (struct die_info *src_die, struct attribute *attr,
15344 struct dwarf2_cu **ref_cu)
15345{
b64f50a1 15346 sect_offset offset = dwarf2_get_ref_die_offset (attr);
5c631832
JK
15347 struct dwarf2_cu *cu = *ref_cu;
15348 struct die_info *die;
15349
15350 die = follow_die_offset (offset, ref_cu);
15351 if (!die)
15352 error (_("Dwarf Error: Cannot find DIE at 0x%x referenced from DIE "
15353 "at 0x%x [in module %s]"),
b64f50a1 15354 offset.sect_off, src_die->offset.sect_off, cu->objfile->name);
348e048f 15355
5c631832
JK
15356 return die;
15357}
15358
d83e736b
JK
15359/* Return DWARF block referenced by DW_AT_location of DIE at OFFSET at PER_CU.
15360 Returned value is intended for DW_OP_call*. Returned
15361 dwarf2_locexpr_baton->data has lifetime of PER_CU->OBJFILE. */
5c631832
JK
15362
15363struct dwarf2_locexpr_baton
b64f50a1 15364dwarf2_fetch_die_location_block (cu_offset offset_in_cu,
8cf6f0b1
TT
15365 struct dwarf2_per_cu_data *per_cu,
15366 CORE_ADDR (*get_frame_pc) (void *baton),
15367 void *baton)
5c631832 15368{
b64f50a1 15369 sect_offset offset = { per_cu->offset.sect_off + offset_in_cu.cu_off };
918dd910 15370 struct dwarf2_cu *cu;
5c631832
JK
15371 struct die_info *die;
15372 struct attribute *attr;
15373 struct dwarf2_locexpr_baton retval;
15374
8cf6f0b1
TT
15375 dw2_setup (per_cu->objfile);
15376
918dd910
JK
15377 if (per_cu->cu == NULL)
15378 load_cu (per_cu);
15379 cu = per_cu->cu;
15380
5c631832
JK
15381 die = follow_die_offset (offset, &cu);
15382 if (!die)
15383 error (_("Dwarf Error: Cannot find DIE at 0x%x referenced in module %s"),
b64f50a1 15384 offset.sect_off, per_cu->objfile->name);
5c631832
JK
15385
15386 attr = dwarf2_attr (die, DW_AT_location, cu);
15387 if (!attr)
15388 {
e103e986
JK
15389 /* DWARF: "If there is no such attribute, then there is no effect.".
15390 DATA is ignored if SIZE is 0. */
5c631832 15391
e103e986 15392 retval.data = NULL;
5c631832
JK
15393 retval.size = 0;
15394 }
8cf6f0b1
TT
15395 else if (attr_form_is_section_offset (attr))
15396 {
15397 struct dwarf2_loclist_baton loclist_baton;
15398 CORE_ADDR pc = (*get_frame_pc) (baton);
15399 size_t size;
15400
15401 fill_in_loclist_baton (cu, &loclist_baton, attr);
15402
15403 retval.data = dwarf2_find_location_expression (&loclist_baton,
15404 &size, pc);
15405 retval.size = size;
15406 }
5c631832
JK
15407 else
15408 {
15409 if (!attr_form_is_block (attr))
15410 error (_("Dwarf Error: DIE at 0x%x referenced in module %s "
15411 "is neither DW_FORM_block* nor DW_FORM_exprloc"),
b64f50a1 15412 offset.sect_off, per_cu->objfile->name);
5c631832
JK
15413
15414 retval.data = DW_BLOCK (attr)->data;
15415 retval.size = DW_BLOCK (attr)->size;
15416 }
15417 retval.per_cu = cu->per_cu;
918dd910 15418
918dd910
JK
15419 age_cached_comp_units ();
15420
5c631832 15421 return retval;
348e048f
DE
15422}
15423
8a9b8146
TT
15424/* Return the type of the DIE at DIE_OFFSET in the CU named by
15425 PER_CU. */
15426
15427struct type *
b64f50a1 15428dwarf2_get_die_type (cu_offset die_offset,
8a9b8146
TT
15429 struct dwarf2_per_cu_data *per_cu)
15430{
b64f50a1
JK
15431 sect_offset die_offset_sect;
15432
8a9b8146 15433 dw2_setup (per_cu->objfile);
b64f50a1
JK
15434
15435 die_offset_sect.sect_off = per_cu->offset.sect_off + die_offset.cu_off;
15436 return get_die_type_at_offset (die_offset_sect, per_cu);
8a9b8146
TT
15437}
15438
348e048f
DE
15439/* Follow the signature attribute ATTR in SRC_DIE.
15440 On entry *REF_CU is the CU of SRC_DIE.
15441 On exit *REF_CU is the CU of the result. */
15442
15443static struct die_info *
15444follow_die_sig (struct die_info *src_die, struct attribute *attr,
15445 struct dwarf2_cu **ref_cu)
15446{
15447 struct objfile *objfile = (*ref_cu)->objfile;
15448 struct die_info temp_die;
15449 struct signatured_type *sig_type = DW_SIGNATURED_TYPE (attr);
15450 struct dwarf2_cu *sig_cu;
15451 struct die_info *die;
15452
15453 /* sig_type will be NULL if the signatured type is missing from
15454 the debug info. */
15455 if (sig_type == NULL)
15456 error (_("Dwarf Error: Cannot find signatured DIE referenced from DIE "
15457 "at 0x%x [in module %s]"),
b64f50a1 15458 src_die->offset.sect_off, objfile->name);
348e048f
DE
15459
15460 /* If necessary, add it to the queue and load its DIEs. */
15461
95554aad 15462 if (maybe_queue_comp_unit (*ref_cu, &sig_type->per_cu, language_minimal))
a0f42c21 15463 read_signatured_type (sig_type);
348e048f
DE
15464
15465 gdb_assert (sig_type->per_cu.cu != NULL);
15466
15467 sig_cu = sig_type->per_cu.cu;
3019eac3
DE
15468 gdb_assert (sig_type->type_offset_in_section.sect_off != 0);
15469 temp_die.offset = sig_type->type_offset_in_section;
b64f50a1
JK
15470 die = htab_find_with_hash (sig_cu->die_hash, &temp_die,
15471 temp_die.offset.sect_off);
348e048f
DE
15472 if (die)
15473 {
15474 *ref_cu = sig_cu;
15475 return die;
15476 }
15477
3e43a32a
MS
15478 error (_("Dwarf Error: Cannot find signatured DIE at 0x%x referenced "
15479 "from DIE at 0x%x [in module %s]"),
b64f50a1 15480 temp_die.offset.sect_off, src_die->offset.sect_off, objfile->name);
348e048f
DE
15481}
15482
15483/* Given an offset of a signatured type, return its signatured_type. */
15484
15485static struct signatured_type *
8b70b953
TT
15486lookup_signatured_type_at_offset (struct objfile *objfile,
15487 struct dwarf2_section_info *section,
b64f50a1 15488 sect_offset offset)
348e048f 15489{
b64f50a1 15490 gdb_byte *info_ptr = section->buffer + offset.sect_off;
348e048f
DE
15491 unsigned int length, initial_length_size;
15492 unsigned int sig_offset;
52dc124a 15493 struct signatured_type find_entry, *sig_type;
348e048f
DE
15494
15495 length = read_initial_length (objfile->obfd, info_ptr, &initial_length_size);
15496 sig_offset = (initial_length_size
15497 + 2 /*version*/
15498 + (initial_length_size == 4 ? 4 : 8) /*debug_abbrev_offset*/
15499 + 1 /*address_size*/);
15500 find_entry.signature = bfd_get_64 (objfile->obfd, info_ptr + sig_offset);
52dc124a 15501 sig_type = htab_find (dwarf2_per_objfile->signatured_types, &find_entry);
348e048f
DE
15502
15503 /* This is only used to lookup previously recorded types.
15504 If we didn't find it, it's our bug. */
52dc124a
DE
15505 gdb_assert (sig_type != NULL);
15506 gdb_assert (offset.sect_off == sig_type->per_cu.offset.sect_off);
348e048f 15507
52dc124a 15508 return sig_type;
348e048f
DE
15509}
15510
e5fe5e75 15511/* Load the DIEs associated with type unit PER_CU into memory. */
348e048f
DE
15512
15513static void
e5fe5e75 15514load_full_type_unit (struct dwarf2_per_cu_data *per_cu)
348e048f 15515{
e5fe5e75 15516 struct objfile *objfile = per_cu->objfile;
3019eac3 15517 struct dwarf2_section_info *sect = per_cu->info_or_types_section;
b64f50a1 15518 sect_offset offset = per_cu->offset;
52dc124a 15519 struct signatured_type *sig_type;
348e048f 15520
8b70b953 15521 dwarf2_read_section (objfile, sect);
be391dca 15522
348e048f 15523 /* We have the section offset, but we need the signature to do the
e5fe5e75
DE
15524 hash table lookup. */
15525 /* FIXME: This is sorta unnecessary, read_signatured_type only uses
15526 the signature to assert we found the right one.
15527 Ok, but it's a lot of work. We should simplify things so any needed
15528 assert doesn't require all this clumsiness. */
52dc124a 15529 sig_type = lookup_signatured_type_at_offset (objfile, sect, offset);
348e048f 15530
dee91e82 15531 gdb_assert (&sig_type->per_cu == per_cu);
52dc124a 15532 gdb_assert (sig_type->per_cu.cu == NULL);
348e048f 15533
52dc124a 15534 read_signatured_type (sig_type);
348e048f 15535
52dc124a 15536 gdb_assert (sig_type->per_cu.cu != NULL);
348e048f
DE
15537}
15538
dee91e82
DE
15539/* die_reader_func for read_signatured_type.
15540 This is identical to load_full_comp_unit_reader,
15541 but is kept separate for now. */
348e048f
DE
15542
15543static void
dee91e82
DE
15544read_signatured_type_reader (const struct die_reader_specs *reader,
15545 gdb_byte *info_ptr,
15546 struct die_info *comp_unit_die,
15547 int has_children,
15548 void *data)
348e048f 15549{
dee91e82 15550 struct dwarf2_cu *cu = reader->cu;
348e048f 15551
dee91e82
DE
15552 gdb_assert (cu->die_hash == NULL);
15553 cu->die_hash =
15554 htab_create_alloc_ex (cu->header.length / 12,
15555 die_hash,
15556 die_eq,
15557 NULL,
15558 &cu->comp_unit_obstack,
15559 hashtab_obstack_allocate,
15560 dummy_obstack_deallocate);
348e048f 15561
dee91e82
DE
15562 if (has_children)
15563 comp_unit_die->child = read_die_and_siblings (reader, info_ptr,
15564 &info_ptr, comp_unit_die);
15565 cu->dies = comp_unit_die;
15566 /* comp_unit_die is not stored in die_hash, no need. */
348e048f
DE
15567
15568 /* We try not to read any attributes in this function, because not
9cdd5dbd 15569 all CUs needed for references have been loaded yet, and symbol
348e048f 15570 table processing isn't initialized. But we have to set the CU language,
dee91e82
DE
15571 or we won't be able to build types correctly.
15572 Similarly, if we do not read the producer, we can not apply
15573 producer-specific interpretation. */
95554aad 15574 prepare_one_comp_unit (cu, cu->dies, language_minimal);
dee91e82 15575}
348e048f 15576
3019eac3
DE
15577/* Read in a signatured type and build its CU and DIEs.
15578 If the type is a stub for the real type in a DWO file,
15579 read in the real type from the DWO file as well. */
dee91e82
DE
15580
15581static void
15582read_signatured_type (struct signatured_type *sig_type)
15583{
15584 struct dwarf2_per_cu_data *per_cu = &sig_type->per_cu;
348e048f 15585
3019eac3 15586 gdb_assert (per_cu->is_debug_types);
dee91e82 15587 gdb_assert (per_cu->cu == NULL);
348e048f 15588
fd820528 15589 init_cutu_and_read_dies (per_cu, 0, 1, read_signatured_type_reader, NULL);
c906108c
SS
15590}
15591
c906108c
SS
15592/* Decode simple location descriptions.
15593 Given a pointer to a dwarf block that defines a location, compute
15594 the location and return the value.
15595
4cecd739
DJ
15596 NOTE drow/2003-11-18: This function is called in two situations
15597 now: for the address of static or global variables (partial symbols
15598 only) and for offsets into structures which are expected to be
15599 (more or less) constant. The partial symbol case should go away,
15600 and only the constant case should remain. That will let this
15601 function complain more accurately. A few special modes are allowed
15602 without complaint for global variables (for instance, global
15603 register values and thread-local values).
c906108c
SS
15604
15605 A location description containing no operations indicates that the
4cecd739 15606 object is optimized out. The return value is 0 for that case.
6b992462
DJ
15607 FIXME drow/2003-11-16: No callers check for this case any more; soon all
15608 callers will only want a very basic result and this can become a
21ae7a4d
JK
15609 complaint.
15610
15611 Note that stack[0] is unused except as a default error return. */
c906108c
SS
15612
15613static CORE_ADDR
e7c27a73 15614decode_locdesc (struct dwarf_block *blk, struct dwarf2_cu *cu)
c906108c 15615{
e7c27a73 15616 struct objfile *objfile = cu->objfile;
21ae7a4d
JK
15617 int i;
15618 int size = blk->size;
15619 gdb_byte *data = blk->data;
15620 CORE_ADDR stack[64];
15621 int stacki;
15622 unsigned int bytes_read, unsnd;
15623 gdb_byte op;
c906108c 15624
21ae7a4d
JK
15625 i = 0;
15626 stacki = 0;
15627 stack[stacki] = 0;
15628 stack[++stacki] = 0;
15629
15630 while (i < size)
15631 {
15632 op = data[i++];
15633 switch (op)
15634 {
15635 case DW_OP_lit0:
15636 case DW_OP_lit1:
15637 case DW_OP_lit2:
15638 case DW_OP_lit3:
15639 case DW_OP_lit4:
15640 case DW_OP_lit5:
15641 case DW_OP_lit6:
15642 case DW_OP_lit7:
15643 case DW_OP_lit8:
15644 case DW_OP_lit9:
15645 case DW_OP_lit10:
15646 case DW_OP_lit11:
15647 case DW_OP_lit12:
15648 case DW_OP_lit13:
15649 case DW_OP_lit14:
15650 case DW_OP_lit15:
15651 case DW_OP_lit16:
15652 case DW_OP_lit17:
15653 case DW_OP_lit18:
15654 case DW_OP_lit19:
15655 case DW_OP_lit20:
15656 case DW_OP_lit21:
15657 case DW_OP_lit22:
15658 case DW_OP_lit23:
15659 case DW_OP_lit24:
15660 case DW_OP_lit25:
15661 case DW_OP_lit26:
15662 case DW_OP_lit27:
15663 case DW_OP_lit28:
15664 case DW_OP_lit29:
15665 case DW_OP_lit30:
15666 case DW_OP_lit31:
15667 stack[++stacki] = op - DW_OP_lit0;
15668 break;
f1bea926 15669
21ae7a4d
JK
15670 case DW_OP_reg0:
15671 case DW_OP_reg1:
15672 case DW_OP_reg2:
15673 case DW_OP_reg3:
15674 case DW_OP_reg4:
15675 case DW_OP_reg5:
15676 case DW_OP_reg6:
15677 case DW_OP_reg7:
15678 case DW_OP_reg8:
15679 case DW_OP_reg9:
15680 case DW_OP_reg10:
15681 case DW_OP_reg11:
15682 case DW_OP_reg12:
15683 case DW_OP_reg13:
15684 case DW_OP_reg14:
15685 case DW_OP_reg15:
15686 case DW_OP_reg16:
15687 case DW_OP_reg17:
15688 case DW_OP_reg18:
15689 case DW_OP_reg19:
15690 case DW_OP_reg20:
15691 case DW_OP_reg21:
15692 case DW_OP_reg22:
15693 case DW_OP_reg23:
15694 case DW_OP_reg24:
15695 case DW_OP_reg25:
15696 case DW_OP_reg26:
15697 case DW_OP_reg27:
15698 case DW_OP_reg28:
15699 case DW_OP_reg29:
15700 case DW_OP_reg30:
15701 case DW_OP_reg31:
15702 stack[++stacki] = op - DW_OP_reg0;
15703 if (i < size)
15704 dwarf2_complex_location_expr_complaint ();
15705 break;
c906108c 15706
21ae7a4d
JK
15707 case DW_OP_regx:
15708 unsnd = read_unsigned_leb128 (NULL, (data + i), &bytes_read);
15709 i += bytes_read;
15710 stack[++stacki] = unsnd;
15711 if (i < size)
15712 dwarf2_complex_location_expr_complaint ();
15713 break;
c906108c 15714
21ae7a4d
JK
15715 case DW_OP_addr:
15716 stack[++stacki] = read_address (objfile->obfd, &data[i],
15717 cu, &bytes_read);
15718 i += bytes_read;
15719 break;
d53d4ac5 15720
21ae7a4d
JK
15721 case DW_OP_const1u:
15722 stack[++stacki] = read_1_byte (objfile->obfd, &data[i]);
15723 i += 1;
15724 break;
15725
15726 case DW_OP_const1s:
15727 stack[++stacki] = read_1_signed_byte (objfile->obfd, &data[i]);
15728 i += 1;
15729 break;
15730
15731 case DW_OP_const2u:
15732 stack[++stacki] = read_2_bytes (objfile->obfd, &data[i]);
15733 i += 2;
15734 break;
15735
15736 case DW_OP_const2s:
15737 stack[++stacki] = read_2_signed_bytes (objfile->obfd, &data[i]);
15738 i += 2;
15739 break;
d53d4ac5 15740
21ae7a4d
JK
15741 case DW_OP_const4u:
15742 stack[++stacki] = read_4_bytes (objfile->obfd, &data[i]);
15743 i += 4;
15744 break;
15745
15746 case DW_OP_const4s:
15747 stack[++stacki] = read_4_signed_bytes (objfile->obfd, &data[i]);
15748 i += 4;
15749 break;
15750
585861ea
JK
15751 case DW_OP_const8u:
15752 stack[++stacki] = read_8_bytes (objfile->obfd, &data[i]);
15753 i += 8;
15754 break;
15755
21ae7a4d
JK
15756 case DW_OP_constu:
15757 stack[++stacki] = read_unsigned_leb128 (NULL, (data + i),
15758 &bytes_read);
15759 i += bytes_read;
15760 break;
15761
15762 case DW_OP_consts:
15763 stack[++stacki] = read_signed_leb128 (NULL, (data + i), &bytes_read);
15764 i += bytes_read;
15765 break;
15766
15767 case DW_OP_dup:
15768 stack[stacki + 1] = stack[stacki];
15769 stacki++;
15770 break;
15771
15772 case DW_OP_plus:
15773 stack[stacki - 1] += stack[stacki];
15774 stacki--;
15775 break;
15776
15777 case DW_OP_plus_uconst:
15778 stack[stacki] += read_unsigned_leb128 (NULL, (data + i),
15779 &bytes_read);
15780 i += bytes_read;
15781 break;
15782
15783 case DW_OP_minus:
15784 stack[stacki - 1] -= stack[stacki];
15785 stacki--;
15786 break;
15787
15788 case DW_OP_deref:
15789 /* If we're not the last op, then we definitely can't encode
15790 this using GDB's address_class enum. This is valid for partial
15791 global symbols, although the variable's address will be bogus
15792 in the psymtab. */
15793 if (i < size)
15794 dwarf2_complex_location_expr_complaint ();
15795 break;
15796
15797 case DW_OP_GNU_push_tls_address:
15798 /* The top of the stack has the offset from the beginning
15799 of the thread control block at which the variable is located. */
15800 /* Nothing should follow this operator, so the top of stack would
15801 be returned. */
15802 /* This is valid for partial global symbols, but the variable's
585861ea
JK
15803 address will be bogus in the psymtab. Make it always at least
15804 non-zero to not look as a variable garbage collected by linker
15805 which have DW_OP_addr 0. */
21ae7a4d
JK
15806 if (i < size)
15807 dwarf2_complex_location_expr_complaint ();
585861ea 15808 stack[stacki]++;
21ae7a4d
JK
15809 break;
15810
15811 case DW_OP_GNU_uninit:
15812 break;
15813
3019eac3 15814 case DW_OP_GNU_addr_index:
49f6c839 15815 case DW_OP_GNU_const_index:
3019eac3
DE
15816 stack[++stacki] = read_addr_index_from_leb128 (cu, &data[i],
15817 &bytes_read);
15818 i += bytes_read;
15819 break;
15820
21ae7a4d
JK
15821 default:
15822 {
f39c6ffd 15823 const char *name = get_DW_OP_name (op);
21ae7a4d
JK
15824
15825 if (name)
15826 complaint (&symfile_complaints, _("unsupported stack op: '%s'"),
15827 name);
15828 else
15829 complaint (&symfile_complaints, _("unsupported stack op: '%02x'"),
15830 op);
15831 }
15832
15833 return (stack[stacki]);
d53d4ac5 15834 }
3c6e0cb3 15835
21ae7a4d
JK
15836 /* Enforce maximum stack depth of SIZE-1 to avoid writing
15837 outside of the allocated space. Also enforce minimum>0. */
15838 if (stacki >= ARRAY_SIZE (stack) - 1)
15839 {
15840 complaint (&symfile_complaints,
15841 _("location description stack overflow"));
15842 return 0;
15843 }
15844
15845 if (stacki <= 0)
15846 {
15847 complaint (&symfile_complaints,
15848 _("location description stack underflow"));
15849 return 0;
15850 }
15851 }
15852 return (stack[stacki]);
c906108c
SS
15853}
15854
15855/* memory allocation interface */
15856
c906108c 15857static struct dwarf_block *
7b5a2f43 15858dwarf_alloc_block (struct dwarf2_cu *cu)
c906108c
SS
15859{
15860 struct dwarf_block *blk;
15861
15862 blk = (struct dwarf_block *)
7b5a2f43 15863 obstack_alloc (&cu->comp_unit_obstack, sizeof (struct dwarf_block));
c906108c
SS
15864 return (blk);
15865}
15866
c906108c 15867static struct die_info *
b60c80d6 15868dwarf_alloc_die (struct dwarf2_cu *cu, int num_attrs)
c906108c
SS
15869{
15870 struct die_info *die;
b60c80d6
DJ
15871 size_t size = sizeof (struct die_info);
15872
15873 if (num_attrs > 1)
15874 size += (num_attrs - 1) * sizeof (struct attribute);
c906108c 15875
b60c80d6 15876 die = (struct die_info *) obstack_alloc (&cu->comp_unit_obstack, size);
c906108c
SS
15877 memset (die, 0, sizeof (struct die_info));
15878 return (die);
15879}
2e276125
JB
15880
15881\f
15882/* Macro support. */
15883
2e276125
JB
15884/* Return the full name of file number I in *LH's file name table.
15885 Use COMP_DIR as the name of the current directory of the
15886 compilation. The result is allocated using xmalloc; the caller is
15887 responsible for freeing it. */
15888static char *
15889file_full_name (int file, struct line_header *lh, const char *comp_dir)
15890{
6a83a1e6
EZ
15891 /* Is the file number a valid index into the line header's file name
15892 table? Remember that file numbers start with one, not zero. */
15893 if (1 <= file && file <= lh->num_file_names)
15894 {
15895 struct file_entry *fe = &lh->file_names[file - 1];
6e70227d 15896
6a83a1e6
EZ
15897 if (IS_ABSOLUTE_PATH (fe->name))
15898 return xstrdup (fe->name);
15899 else
15900 {
15901 const char *dir;
15902 int dir_len;
15903 char *full_name;
15904
15905 if (fe->dir_index)
15906 dir = lh->include_dirs[fe->dir_index - 1];
15907 else
15908 dir = comp_dir;
15909
15910 if (dir)
15911 {
15912 dir_len = strlen (dir);
15913 full_name = xmalloc (dir_len + 1 + strlen (fe->name) + 1);
15914 strcpy (full_name, dir);
15915 full_name[dir_len] = '/';
15916 strcpy (full_name + dir_len + 1, fe->name);
15917 return full_name;
15918 }
15919 else
15920 return xstrdup (fe->name);
15921 }
15922 }
2e276125
JB
15923 else
15924 {
6a83a1e6
EZ
15925 /* The compiler produced a bogus file number. We can at least
15926 record the macro definitions made in the file, even if we
15927 won't be able to find the file by name. */
15928 char fake_name[80];
9a619af0 15929
6a83a1e6 15930 sprintf (fake_name, "<bad macro file number %d>", file);
2e276125 15931
6e70227d 15932 complaint (&symfile_complaints,
6a83a1e6
EZ
15933 _("bad file number in macro information (%d)"),
15934 file);
2e276125 15935
6a83a1e6 15936 return xstrdup (fake_name);
2e276125
JB
15937 }
15938}
15939
15940
15941static struct macro_source_file *
15942macro_start_file (int file, int line,
15943 struct macro_source_file *current_file,
15944 const char *comp_dir,
15945 struct line_header *lh, struct objfile *objfile)
15946{
15947 /* The full name of this source file. */
15948 char *full_name = file_full_name (file, lh, comp_dir);
15949
15950 /* We don't create a macro table for this compilation unit
15951 at all until we actually get a filename. */
15952 if (! pending_macros)
4a146b47 15953 pending_macros = new_macro_table (&objfile->objfile_obstack,
af5f3db6 15954 objfile->macro_cache);
2e276125
JB
15955
15956 if (! current_file)
abc9d0dc
TT
15957 {
15958 /* If we have no current file, then this must be the start_file
15959 directive for the compilation unit's main source file. */
15960 current_file = macro_set_main (pending_macros, full_name);
15961 macro_define_special (pending_macros);
15962 }
2e276125
JB
15963 else
15964 current_file = macro_include (current_file, line, full_name);
15965
15966 xfree (full_name);
6e70227d 15967
2e276125
JB
15968 return current_file;
15969}
15970
15971
15972/* Copy the LEN characters at BUF to a xmalloc'ed block of memory,
15973 followed by a null byte. */
15974static char *
15975copy_string (const char *buf, int len)
15976{
15977 char *s = xmalloc (len + 1);
9a619af0 15978
2e276125
JB
15979 memcpy (s, buf, len);
15980 s[len] = '\0';
2e276125
JB
15981 return s;
15982}
15983
15984
15985static const char *
15986consume_improper_spaces (const char *p, const char *body)
15987{
15988 if (*p == ' ')
15989 {
4d3c2250 15990 complaint (&symfile_complaints,
3e43a32a
MS
15991 _("macro definition contains spaces "
15992 "in formal argument list:\n`%s'"),
4d3c2250 15993 body);
2e276125
JB
15994
15995 while (*p == ' ')
15996 p++;
15997 }
15998
15999 return p;
16000}
16001
16002
16003static void
16004parse_macro_definition (struct macro_source_file *file, int line,
16005 const char *body)
16006{
16007 const char *p;
16008
16009 /* The body string takes one of two forms. For object-like macro
16010 definitions, it should be:
16011
16012 <macro name> " " <definition>
16013
16014 For function-like macro definitions, it should be:
16015
16016 <macro name> "() " <definition>
16017 or
16018 <macro name> "(" <arg name> ( "," <arg name> ) * ") " <definition>
16019
16020 Spaces may appear only where explicitly indicated, and in the
16021 <definition>.
16022
16023 The Dwarf 2 spec says that an object-like macro's name is always
16024 followed by a space, but versions of GCC around March 2002 omit
6e70227d 16025 the space when the macro's definition is the empty string.
2e276125
JB
16026
16027 The Dwarf 2 spec says that there should be no spaces between the
16028 formal arguments in a function-like macro's formal argument list,
16029 but versions of GCC around March 2002 include spaces after the
16030 commas. */
16031
16032
16033 /* Find the extent of the macro name. The macro name is terminated
16034 by either a space or null character (for an object-like macro) or
16035 an opening paren (for a function-like macro). */
16036 for (p = body; *p; p++)
16037 if (*p == ' ' || *p == '(')
16038 break;
16039
16040 if (*p == ' ' || *p == '\0')
16041 {
16042 /* It's an object-like macro. */
16043 int name_len = p - body;
16044 char *name = copy_string (body, name_len);
16045 const char *replacement;
16046
16047 if (*p == ' ')
16048 replacement = body + name_len + 1;
16049 else
16050 {
4d3c2250 16051 dwarf2_macro_malformed_definition_complaint (body);
2e276125
JB
16052 replacement = body + name_len;
16053 }
6e70227d 16054
2e276125
JB
16055 macro_define_object (file, line, name, replacement);
16056
16057 xfree (name);
16058 }
16059 else if (*p == '(')
16060 {
16061 /* It's a function-like macro. */
16062 char *name = copy_string (body, p - body);
16063 int argc = 0;
16064 int argv_size = 1;
16065 char **argv = xmalloc (argv_size * sizeof (*argv));
16066
16067 p++;
16068
16069 p = consume_improper_spaces (p, body);
16070
16071 /* Parse the formal argument list. */
16072 while (*p && *p != ')')
16073 {
16074 /* Find the extent of the current argument name. */
16075 const char *arg_start = p;
16076
16077 while (*p && *p != ',' && *p != ')' && *p != ' ')
16078 p++;
16079
16080 if (! *p || p == arg_start)
4d3c2250 16081 dwarf2_macro_malformed_definition_complaint (body);
2e276125
JB
16082 else
16083 {
16084 /* Make sure argv has room for the new argument. */
16085 if (argc >= argv_size)
16086 {
16087 argv_size *= 2;
16088 argv = xrealloc (argv, argv_size * sizeof (*argv));
16089 }
16090
16091 argv[argc++] = copy_string (arg_start, p - arg_start);
16092 }
16093
16094 p = consume_improper_spaces (p, body);
16095
16096 /* Consume the comma, if present. */
16097 if (*p == ',')
16098 {
16099 p++;
16100
16101 p = consume_improper_spaces (p, body);
16102 }
16103 }
16104
16105 if (*p == ')')
16106 {
16107 p++;
16108
16109 if (*p == ' ')
16110 /* Perfectly formed definition, no complaints. */
16111 macro_define_function (file, line, name,
6e70227d 16112 argc, (const char **) argv,
2e276125
JB
16113 p + 1);
16114 else if (*p == '\0')
16115 {
16116 /* Complain, but do define it. */
4d3c2250 16117 dwarf2_macro_malformed_definition_complaint (body);
2e276125 16118 macro_define_function (file, line, name,
6e70227d 16119 argc, (const char **) argv,
2e276125
JB
16120 p);
16121 }
16122 else
16123 /* Just complain. */
4d3c2250 16124 dwarf2_macro_malformed_definition_complaint (body);
2e276125
JB
16125 }
16126 else
16127 /* Just complain. */
4d3c2250 16128 dwarf2_macro_malformed_definition_complaint (body);
2e276125
JB
16129
16130 xfree (name);
16131 {
16132 int i;
16133
16134 for (i = 0; i < argc; i++)
16135 xfree (argv[i]);
16136 }
16137 xfree (argv);
16138 }
16139 else
4d3c2250 16140 dwarf2_macro_malformed_definition_complaint (body);
2e276125
JB
16141}
16142
cf2c3c16
TT
16143/* Skip some bytes from BYTES according to the form given in FORM.
16144 Returns the new pointer. */
2e276125 16145
cf2c3c16 16146static gdb_byte *
f664829e 16147skip_form_bytes (bfd *abfd, gdb_byte *bytes, gdb_byte *buffer_end,
cf2c3c16
TT
16148 enum dwarf_form form,
16149 unsigned int offset_size,
16150 struct dwarf2_section_info *section)
2e276125 16151{
cf2c3c16 16152 unsigned int bytes_read;
2e276125 16153
cf2c3c16 16154 switch (form)
2e276125 16155 {
cf2c3c16
TT
16156 case DW_FORM_data1:
16157 case DW_FORM_flag:
16158 ++bytes;
16159 break;
16160
16161 case DW_FORM_data2:
16162 bytes += 2;
16163 break;
16164
16165 case DW_FORM_data4:
16166 bytes += 4;
16167 break;
16168
16169 case DW_FORM_data8:
16170 bytes += 8;
16171 break;
16172
16173 case DW_FORM_string:
16174 read_direct_string (abfd, bytes, &bytes_read);
16175 bytes += bytes_read;
16176 break;
16177
16178 case DW_FORM_sec_offset:
16179 case DW_FORM_strp:
16180 bytes += offset_size;
16181 break;
16182
16183 case DW_FORM_block:
16184 bytes += read_unsigned_leb128 (abfd, bytes, &bytes_read);
16185 bytes += bytes_read;
16186 break;
16187
16188 case DW_FORM_block1:
16189 bytes += 1 + read_1_byte (abfd, bytes);
16190 break;
16191 case DW_FORM_block2:
16192 bytes += 2 + read_2_bytes (abfd, bytes);
16193 break;
16194 case DW_FORM_block4:
16195 bytes += 4 + read_4_bytes (abfd, bytes);
16196 break;
16197
16198 case DW_FORM_sdata:
16199 case DW_FORM_udata:
3019eac3
DE
16200 case DW_FORM_GNU_addr_index:
16201 case DW_FORM_GNU_str_index:
f664829e
DE
16202 bytes = (gdb_byte *) gdb_skip_leb128 (bytes, buffer_end);
16203 if (bytes == NULL)
16204 {
16205 dwarf2_section_buffer_overflow_complaint (section);
16206 return NULL;
16207 }
cf2c3c16
TT
16208 break;
16209
16210 default:
16211 {
16212 complain:
16213 complaint (&symfile_complaints,
16214 _("invalid form 0x%x in `%s'"),
16215 form,
16216 section->asection->name);
16217 return NULL;
16218 }
2e276125
JB
16219 }
16220
cf2c3c16
TT
16221 return bytes;
16222}
757a13d0 16223
cf2c3c16
TT
16224/* A helper for dwarf_decode_macros that handles skipping an unknown
16225 opcode. Returns an updated pointer to the macro data buffer; or,
16226 on error, issues a complaint and returns NULL. */
757a13d0 16227
cf2c3c16
TT
16228static gdb_byte *
16229skip_unknown_opcode (unsigned int opcode,
16230 gdb_byte **opcode_definitions,
f664829e 16231 gdb_byte *mac_ptr, gdb_byte *mac_end,
cf2c3c16
TT
16232 bfd *abfd,
16233 unsigned int offset_size,
16234 struct dwarf2_section_info *section)
16235{
16236 unsigned int bytes_read, i;
16237 unsigned long arg;
16238 gdb_byte *defn;
2e276125 16239
cf2c3c16 16240 if (opcode_definitions[opcode] == NULL)
2e276125 16241 {
cf2c3c16
TT
16242 complaint (&symfile_complaints,
16243 _("unrecognized DW_MACFINO opcode 0x%x"),
16244 opcode);
16245 return NULL;
16246 }
2e276125 16247
cf2c3c16
TT
16248 defn = opcode_definitions[opcode];
16249 arg = read_unsigned_leb128 (abfd, defn, &bytes_read);
16250 defn += bytes_read;
2e276125 16251
cf2c3c16
TT
16252 for (i = 0; i < arg; ++i)
16253 {
f664829e
DE
16254 mac_ptr = skip_form_bytes (abfd, mac_ptr, mac_end, defn[i], offset_size,
16255 section);
cf2c3c16
TT
16256 if (mac_ptr == NULL)
16257 {
16258 /* skip_form_bytes already issued the complaint. */
16259 return NULL;
16260 }
16261 }
757a13d0 16262
cf2c3c16
TT
16263 return mac_ptr;
16264}
757a13d0 16265
cf2c3c16
TT
16266/* A helper function which parses the header of a macro section.
16267 If the macro section is the extended (for now called "GNU") type,
16268 then this updates *OFFSET_SIZE. Returns a pointer to just after
16269 the header, or issues a complaint and returns NULL on error. */
757a13d0 16270
cf2c3c16
TT
16271static gdb_byte *
16272dwarf_parse_macro_header (gdb_byte **opcode_definitions,
16273 bfd *abfd,
16274 gdb_byte *mac_ptr,
16275 unsigned int *offset_size,
16276 int section_is_gnu)
16277{
16278 memset (opcode_definitions, 0, 256 * sizeof (gdb_byte *));
757a13d0 16279
cf2c3c16
TT
16280 if (section_is_gnu)
16281 {
16282 unsigned int version, flags;
757a13d0 16283
cf2c3c16
TT
16284 version = read_2_bytes (abfd, mac_ptr);
16285 if (version != 4)
16286 {
16287 complaint (&symfile_complaints,
16288 _("unrecognized version `%d' in .debug_macro section"),
16289 version);
16290 return NULL;
16291 }
16292 mac_ptr += 2;
757a13d0 16293
cf2c3c16
TT
16294 flags = read_1_byte (abfd, mac_ptr);
16295 ++mac_ptr;
16296 *offset_size = (flags & 1) ? 8 : 4;
757a13d0 16297
cf2c3c16
TT
16298 if ((flags & 2) != 0)
16299 /* We don't need the line table offset. */
16300 mac_ptr += *offset_size;
757a13d0 16301
cf2c3c16
TT
16302 /* Vendor opcode descriptions. */
16303 if ((flags & 4) != 0)
16304 {
16305 unsigned int i, count;
757a13d0 16306
cf2c3c16
TT
16307 count = read_1_byte (abfd, mac_ptr);
16308 ++mac_ptr;
16309 for (i = 0; i < count; ++i)
16310 {
16311 unsigned int opcode, bytes_read;
16312 unsigned long arg;
16313
16314 opcode = read_1_byte (abfd, mac_ptr);
16315 ++mac_ptr;
16316 opcode_definitions[opcode] = mac_ptr;
16317 arg = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
16318 mac_ptr += bytes_read;
16319 mac_ptr += arg;
16320 }
757a13d0 16321 }
cf2c3c16 16322 }
757a13d0 16323
cf2c3c16
TT
16324 return mac_ptr;
16325}
757a13d0 16326
cf2c3c16 16327/* A helper for dwarf_decode_macros that handles the GNU extensions,
8fc3fc34 16328 including DW_MACRO_GNU_transparent_include. */
cf2c3c16
TT
16329
16330static void
16331dwarf_decode_macro_bytes (bfd *abfd, gdb_byte *mac_ptr, gdb_byte *mac_end,
16332 struct macro_source_file *current_file,
16333 struct line_header *lh, char *comp_dir,
16334 struct dwarf2_section_info *section,
16335 int section_is_gnu,
16336 unsigned int offset_size,
8fc3fc34
TT
16337 struct objfile *objfile,
16338 htab_t include_hash)
cf2c3c16
TT
16339{
16340 enum dwarf_macro_record_type macinfo_type;
16341 int at_commandline;
16342 gdb_byte *opcode_definitions[256];
757a13d0 16343
cf2c3c16
TT
16344 mac_ptr = dwarf_parse_macro_header (opcode_definitions, abfd, mac_ptr,
16345 &offset_size, section_is_gnu);
16346 if (mac_ptr == NULL)
16347 {
16348 /* We already issued a complaint. */
16349 return;
16350 }
757a13d0
JK
16351
16352 /* Determines if GDB is still before first DW_MACINFO_start_file. If true
16353 GDB is still reading the definitions from command line. First
16354 DW_MACINFO_start_file will need to be ignored as it was already executed
16355 to create CURRENT_FILE for the main source holding also the command line
16356 definitions. On first met DW_MACINFO_start_file this flag is reset to
16357 normally execute all the remaining DW_MACINFO_start_file macinfos. */
16358
16359 at_commandline = 1;
16360
16361 do
16362 {
16363 /* Do we at least have room for a macinfo type byte? */
16364 if (mac_ptr >= mac_end)
16365 {
f664829e 16366 dwarf2_section_buffer_overflow_complaint (section);
757a13d0
JK
16367 break;
16368 }
16369
16370 macinfo_type = read_1_byte (abfd, mac_ptr);
16371 mac_ptr++;
16372
cf2c3c16
TT
16373 /* Note that we rely on the fact that the corresponding GNU and
16374 DWARF constants are the same. */
757a13d0
JK
16375 switch (macinfo_type)
16376 {
16377 /* A zero macinfo type indicates the end of the macro
16378 information. */
16379 case 0:
16380 break;
2e276125 16381
cf2c3c16
TT
16382 case DW_MACRO_GNU_define:
16383 case DW_MACRO_GNU_undef:
16384 case DW_MACRO_GNU_define_indirect:
16385 case DW_MACRO_GNU_undef_indirect:
2e276125 16386 {
891d2f0b 16387 unsigned int bytes_read;
2e276125
JB
16388 int line;
16389 char *body;
cf2c3c16 16390 int is_define;
2e276125 16391
cf2c3c16
TT
16392 line = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
16393 mac_ptr += bytes_read;
16394
16395 if (macinfo_type == DW_MACRO_GNU_define
16396 || macinfo_type == DW_MACRO_GNU_undef)
16397 {
16398 body = read_direct_string (abfd, mac_ptr, &bytes_read);
16399 mac_ptr += bytes_read;
16400 }
16401 else
16402 {
16403 LONGEST str_offset;
16404
16405 str_offset = read_offset_1 (abfd, mac_ptr, offset_size);
16406 mac_ptr += offset_size;
2e276125 16407
cf2c3c16
TT
16408 body = read_indirect_string_at_offset (abfd, str_offset);
16409 }
16410
16411 is_define = (macinfo_type == DW_MACRO_GNU_define
16412 || macinfo_type == DW_MACRO_GNU_define_indirect);
2e276125 16413 if (! current_file)
757a13d0
JK
16414 {
16415 /* DWARF violation as no main source is present. */
16416 complaint (&symfile_complaints,
16417 _("debug info with no main source gives macro %s "
16418 "on line %d: %s"),
cf2c3c16
TT
16419 is_define ? _("definition") : _("undefinition"),
16420 line, body);
757a13d0
JK
16421 break;
16422 }
3e43a32a
MS
16423 if ((line == 0 && !at_commandline)
16424 || (line != 0 && at_commandline))
4d3c2250 16425 complaint (&symfile_complaints,
757a13d0
JK
16426 _("debug info gives %s macro %s with %s line %d: %s"),
16427 at_commandline ? _("command-line") : _("in-file"),
cf2c3c16 16428 is_define ? _("definition") : _("undefinition"),
757a13d0
JK
16429 line == 0 ? _("zero") : _("non-zero"), line, body);
16430
cf2c3c16 16431 if (is_define)
757a13d0 16432 parse_macro_definition (current_file, line, body);
cf2c3c16
TT
16433 else
16434 {
16435 gdb_assert (macinfo_type == DW_MACRO_GNU_undef
16436 || macinfo_type == DW_MACRO_GNU_undef_indirect);
16437 macro_undef (current_file, line, body);
16438 }
2e276125
JB
16439 }
16440 break;
16441
cf2c3c16 16442 case DW_MACRO_GNU_start_file:
2e276125 16443 {
891d2f0b 16444 unsigned int bytes_read;
2e276125
JB
16445 int line, file;
16446
16447 line = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
16448 mac_ptr += bytes_read;
16449 file = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
16450 mac_ptr += bytes_read;
16451
3e43a32a
MS
16452 if ((line == 0 && !at_commandline)
16453 || (line != 0 && at_commandline))
757a13d0
JK
16454 complaint (&symfile_complaints,
16455 _("debug info gives source %d included "
16456 "from %s at %s line %d"),
16457 file, at_commandline ? _("command-line") : _("file"),
16458 line == 0 ? _("zero") : _("non-zero"), line);
16459
16460 if (at_commandline)
16461 {
cf2c3c16
TT
16462 /* This DW_MACRO_GNU_start_file was executed in the
16463 pass one. */
757a13d0
JK
16464 at_commandline = 0;
16465 }
16466 else
16467 current_file = macro_start_file (file, line,
16468 current_file, comp_dir,
cf2c3c16 16469 lh, objfile);
2e276125
JB
16470 }
16471 break;
16472
cf2c3c16 16473 case DW_MACRO_GNU_end_file:
2e276125 16474 if (! current_file)
4d3c2250 16475 complaint (&symfile_complaints,
3e43a32a
MS
16476 _("macro debug info has an unmatched "
16477 "`close_file' directive"));
2e276125
JB
16478 else
16479 {
16480 current_file = current_file->included_by;
16481 if (! current_file)
16482 {
cf2c3c16 16483 enum dwarf_macro_record_type next_type;
2e276125
JB
16484
16485 /* GCC circa March 2002 doesn't produce the zero
16486 type byte marking the end of the compilation
16487 unit. Complain if it's not there, but exit no
16488 matter what. */
16489
16490 /* Do we at least have room for a macinfo type byte? */
16491 if (mac_ptr >= mac_end)
16492 {
f664829e 16493 dwarf2_section_buffer_overflow_complaint (section);
2e276125
JB
16494 return;
16495 }
16496
16497 /* We don't increment mac_ptr here, so this is just
16498 a look-ahead. */
16499 next_type = read_1_byte (abfd, mac_ptr);
16500 if (next_type != 0)
4d3c2250 16501 complaint (&symfile_complaints,
3e43a32a
MS
16502 _("no terminating 0-type entry for "
16503 "macros in `.debug_macinfo' section"));
2e276125
JB
16504
16505 return;
16506 }
16507 }
16508 break;
16509
cf2c3c16
TT
16510 case DW_MACRO_GNU_transparent_include:
16511 {
16512 LONGEST offset;
8fc3fc34 16513 void **slot;
cf2c3c16
TT
16514
16515 offset = read_offset_1 (abfd, mac_ptr, offset_size);
16516 mac_ptr += offset_size;
16517
8fc3fc34
TT
16518 slot = htab_find_slot (include_hash, mac_ptr, INSERT);
16519 if (*slot != NULL)
16520 {
16521 /* This has actually happened; see
16522 http://sourceware.org/bugzilla/show_bug.cgi?id=13568. */
16523 complaint (&symfile_complaints,
16524 _("recursive DW_MACRO_GNU_transparent_include in "
16525 ".debug_macro section"));
16526 }
16527 else
16528 {
16529 *slot = mac_ptr;
16530
16531 dwarf_decode_macro_bytes (abfd,
16532 section->buffer + offset,
16533 mac_end, current_file,
16534 lh, comp_dir,
16535 section, section_is_gnu,
16536 offset_size, objfile, include_hash);
16537
16538 htab_remove_elt (include_hash, mac_ptr);
16539 }
cf2c3c16
TT
16540 }
16541 break;
16542
2e276125 16543 case DW_MACINFO_vendor_ext:
cf2c3c16
TT
16544 if (!section_is_gnu)
16545 {
16546 unsigned int bytes_read;
16547 int constant;
2e276125 16548
cf2c3c16
TT
16549 constant = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
16550 mac_ptr += bytes_read;
16551 read_direct_string (abfd, mac_ptr, &bytes_read);
16552 mac_ptr += bytes_read;
2e276125 16553
cf2c3c16
TT
16554 /* We don't recognize any vendor extensions. */
16555 break;
16556 }
16557 /* FALLTHROUGH */
16558
16559 default:
16560 mac_ptr = skip_unknown_opcode (macinfo_type, opcode_definitions,
f664829e 16561 mac_ptr, mac_end, abfd, offset_size,
cf2c3c16
TT
16562 section);
16563 if (mac_ptr == NULL)
16564 return;
16565 break;
2e276125 16566 }
757a13d0 16567 } while (macinfo_type != 0);
2e276125 16568}
8e19ed76 16569
cf2c3c16 16570static void
09262596
DE
16571dwarf_decode_macros (struct dwarf2_cu *cu, unsigned int offset,
16572 char *comp_dir, int section_is_gnu)
cf2c3c16 16573{
bb5ed363 16574 struct objfile *objfile = dwarf2_per_objfile->objfile;
09262596
DE
16575 struct line_header *lh = cu->line_header;
16576 bfd *abfd;
cf2c3c16
TT
16577 gdb_byte *mac_ptr, *mac_end;
16578 struct macro_source_file *current_file = 0;
16579 enum dwarf_macro_record_type macinfo_type;
16580 unsigned int offset_size = cu->header.offset_size;
16581 gdb_byte *opcode_definitions[256];
8fc3fc34
TT
16582 struct cleanup *cleanup;
16583 htab_t include_hash;
16584 void **slot;
09262596
DE
16585 struct dwarf2_section_info *section;
16586 const char *section_name;
16587
16588 if (cu->dwo_unit != NULL)
16589 {
16590 if (section_is_gnu)
16591 {
16592 section = &cu->dwo_unit->dwo_file->sections.macro;
16593 section_name = ".debug_macro.dwo";
16594 }
16595 else
16596 {
16597 section = &cu->dwo_unit->dwo_file->sections.macinfo;
16598 section_name = ".debug_macinfo.dwo";
16599 }
16600 }
16601 else
16602 {
16603 if (section_is_gnu)
16604 {
16605 section = &dwarf2_per_objfile->macro;
16606 section_name = ".debug_macro";
16607 }
16608 else
16609 {
16610 section = &dwarf2_per_objfile->macinfo;
16611 section_name = ".debug_macinfo";
16612 }
16613 }
cf2c3c16 16614
bb5ed363 16615 dwarf2_read_section (objfile, section);
cf2c3c16
TT
16616 if (section->buffer == NULL)
16617 {
fceca515 16618 complaint (&symfile_complaints, _("missing %s section"), section_name);
cf2c3c16
TT
16619 return;
16620 }
09262596 16621 abfd = section->asection->owner;
cf2c3c16
TT
16622
16623 /* First pass: Find the name of the base filename.
16624 This filename is needed in order to process all macros whose definition
16625 (or undefinition) comes from the command line. These macros are defined
16626 before the first DW_MACINFO_start_file entry, and yet still need to be
16627 associated to the base file.
16628
16629 To determine the base file name, we scan the macro definitions until we
16630 reach the first DW_MACINFO_start_file entry. We then initialize
16631 CURRENT_FILE accordingly so that any macro definition found before the
16632 first DW_MACINFO_start_file can still be associated to the base file. */
16633
16634 mac_ptr = section->buffer + offset;
16635 mac_end = section->buffer + section->size;
16636
16637 mac_ptr = dwarf_parse_macro_header (opcode_definitions, abfd, mac_ptr,
16638 &offset_size, section_is_gnu);
16639 if (mac_ptr == NULL)
16640 {
16641 /* We already issued a complaint. */
16642 return;
16643 }
16644
16645 do
16646 {
16647 /* Do we at least have room for a macinfo type byte? */
16648 if (mac_ptr >= mac_end)
16649 {
16650 /* Complaint is printed during the second pass as GDB will probably
16651 stop the first pass earlier upon finding
16652 DW_MACINFO_start_file. */
16653 break;
16654 }
16655
16656 macinfo_type = read_1_byte (abfd, mac_ptr);
16657 mac_ptr++;
16658
16659 /* Note that we rely on the fact that the corresponding GNU and
16660 DWARF constants are the same. */
16661 switch (macinfo_type)
16662 {
16663 /* A zero macinfo type indicates the end of the macro
16664 information. */
16665 case 0:
16666 break;
16667
16668 case DW_MACRO_GNU_define:
16669 case DW_MACRO_GNU_undef:
16670 /* Only skip the data by MAC_PTR. */
16671 {
16672 unsigned int bytes_read;
16673
16674 read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
16675 mac_ptr += bytes_read;
16676 read_direct_string (abfd, mac_ptr, &bytes_read);
16677 mac_ptr += bytes_read;
16678 }
16679 break;
16680
16681 case DW_MACRO_GNU_start_file:
16682 {
16683 unsigned int bytes_read;
16684 int line, file;
16685
16686 line = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
16687 mac_ptr += bytes_read;
16688 file = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
16689 mac_ptr += bytes_read;
16690
16691 current_file = macro_start_file (file, line, current_file,
bb5ed363 16692 comp_dir, lh, objfile);
cf2c3c16
TT
16693 }
16694 break;
16695
16696 case DW_MACRO_GNU_end_file:
16697 /* No data to skip by MAC_PTR. */
16698 break;
16699
16700 case DW_MACRO_GNU_define_indirect:
16701 case DW_MACRO_GNU_undef_indirect:
16702 {
16703 unsigned int bytes_read;
16704
16705 read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
16706 mac_ptr += bytes_read;
16707 mac_ptr += offset_size;
16708 }
16709 break;
16710
16711 case DW_MACRO_GNU_transparent_include:
16712 /* Note that, according to the spec, a transparent include
16713 chain cannot call DW_MACRO_GNU_start_file. So, we can just
16714 skip this opcode. */
16715 mac_ptr += offset_size;
16716 break;
16717
16718 case DW_MACINFO_vendor_ext:
16719 /* Only skip the data by MAC_PTR. */
16720 if (!section_is_gnu)
16721 {
16722 unsigned int bytes_read;
16723
16724 read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
16725 mac_ptr += bytes_read;
16726 read_direct_string (abfd, mac_ptr, &bytes_read);
16727 mac_ptr += bytes_read;
16728 }
16729 /* FALLTHROUGH */
16730
16731 default:
16732 mac_ptr = skip_unknown_opcode (macinfo_type, opcode_definitions,
f664829e 16733 mac_ptr, mac_end, abfd, offset_size,
cf2c3c16
TT
16734 section);
16735 if (mac_ptr == NULL)
16736 return;
16737 break;
16738 }
16739 } while (macinfo_type != 0 && current_file == NULL);
16740
16741 /* Second pass: Process all entries.
16742
16743 Use the AT_COMMAND_LINE flag to determine whether we are still processing
16744 command-line macro definitions/undefinitions. This flag is unset when we
16745 reach the first DW_MACINFO_start_file entry. */
16746
8fc3fc34
TT
16747 include_hash = htab_create_alloc (1, htab_hash_pointer, htab_eq_pointer,
16748 NULL, xcalloc, xfree);
16749 cleanup = make_cleanup_htab_delete (include_hash);
16750 mac_ptr = section->buffer + offset;
16751 slot = htab_find_slot (include_hash, mac_ptr, INSERT);
16752 *slot = mac_ptr;
16753 dwarf_decode_macro_bytes (abfd, mac_ptr, mac_end,
cf2c3c16 16754 current_file, lh, comp_dir, section, section_is_gnu,
8fc3fc34
TT
16755 offset_size, objfile, include_hash);
16756 do_cleanups (cleanup);
cf2c3c16
TT
16757}
16758
8e19ed76 16759/* Check if the attribute's form is a DW_FORM_block*
0963b4bd 16760 if so return true else false. */
380bca97 16761
8e19ed76
PS
16762static int
16763attr_form_is_block (struct attribute *attr)
16764{
16765 return (attr == NULL ? 0 :
16766 attr->form == DW_FORM_block1
16767 || attr->form == DW_FORM_block2
16768 || attr->form == DW_FORM_block4
2dc7f7b3
TT
16769 || attr->form == DW_FORM_block
16770 || attr->form == DW_FORM_exprloc);
8e19ed76 16771}
4c2df51b 16772
c6a0999f
JB
16773/* Return non-zero if ATTR's value is a section offset --- classes
16774 lineptr, loclistptr, macptr or rangelistptr --- or zero, otherwise.
16775 You may use DW_UNSND (attr) to retrieve such offsets.
16776
16777 Section 7.5.4, "Attribute Encodings", explains that no attribute
16778 may have a value that belongs to more than one of these classes; it
16779 would be ambiguous if we did, because we use the same forms for all
16780 of them. */
380bca97 16781
3690dd37
JB
16782static int
16783attr_form_is_section_offset (struct attribute *attr)
16784{
16785 return (attr->form == DW_FORM_data4
2dc7f7b3
TT
16786 || attr->form == DW_FORM_data8
16787 || attr->form == DW_FORM_sec_offset);
3690dd37
JB
16788}
16789
3690dd37
JB
16790/* Return non-zero if ATTR's value falls in the 'constant' class, or
16791 zero otherwise. When this function returns true, you can apply
16792 dwarf2_get_attr_constant_value to it.
16793
16794 However, note that for some attributes you must check
16795 attr_form_is_section_offset before using this test. DW_FORM_data4
16796 and DW_FORM_data8 are members of both the constant class, and of
16797 the classes that contain offsets into other debug sections
16798 (lineptr, loclistptr, macptr or rangelistptr). The DWARF spec says
16799 that, if an attribute's can be either a constant or one of the
16800 section offset classes, DW_FORM_data4 and DW_FORM_data8 should be
16801 taken as section offsets, not constants. */
380bca97 16802
3690dd37
JB
16803static int
16804attr_form_is_constant (struct attribute *attr)
16805{
16806 switch (attr->form)
16807 {
16808 case DW_FORM_sdata:
16809 case DW_FORM_udata:
16810 case DW_FORM_data1:
16811 case DW_FORM_data2:
16812 case DW_FORM_data4:
16813 case DW_FORM_data8:
16814 return 1;
16815 default:
16816 return 0;
16817 }
16818}
16819
3019eac3
DE
16820/* Return the .debug_loc section to use for CU.
16821 For DWO files use .debug_loc.dwo. */
16822
16823static struct dwarf2_section_info *
16824cu_debug_loc_section (struct dwarf2_cu *cu)
16825{
16826 if (cu->dwo_unit)
16827 return &cu->dwo_unit->dwo_file->sections.loc;
16828 return &dwarf2_per_objfile->loc;
16829}
16830
8cf6f0b1
TT
16831/* A helper function that fills in a dwarf2_loclist_baton. */
16832
16833static void
16834fill_in_loclist_baton (struct dwarf2_cu *cu,
16835 struct dwarf2_loclist_baton *baton,
16836 struct attribute *attr)
16837{
3019eac3
DE
16838 struct dwarf2_section_info *section = cu_debug_loc_section (cu);
16839
16840 dwarf2_read_section (dwarf2_per_objfile->objfile, section);
8cf6f0b1
TT
16841
16842 baton->per_cu = cu->per_cu;
16843 gdb_assert (baton->per_cu);
16844 /* We don't know how long the location list is, but make sure we
16845 don't run off the edge of the section. */
3019eac3
DE
16846 baton->size = section->size - DW_UNSND (attr);
16847 baton->data = section->buffer + DW_UNSND (attr);
8cf6f0b1 16848 baton->base_address = cu->base_address;
f664829e 16849 baton->from_dwo = cu->dwo_unit != NULL;
8cf6f0b1
TT
16850}
16851
4c2df51b
DJ
16852static void
16853dwarf2_symbol_mark_computed (struct attribute *attr, struct symbol *sym,
e7c27a73 16854 struct dwarf2_cu *cu)
4c2df51b 16855{
bb5ed363 16856 struct objfile *objfile = dwarf2_per_objfile->objfile;
3019eac3 16857 struct dwarf2_section_info *section = cu_debug_loc_section (cu);
bb5ed363 16858
3690dd37 16859 if (attr_form_is_section_offset (attr)
3019eac3 16860 /* .debug_loc{,.dwo} may not exist at all, or the offset may be outside
99bcc461
DJ
16861 the section. If so, fall through to the complaint in the
16862 other branch. */
3019eac3 16863 && DW_UNSND (attr) < dwarf2_section_size (objfile, section))
4c2df51b 16864 {
0d53c4c4 16865 struct dwarf2_loclist_baton *baton;
4c2df51b 16866
bb5ed363 16867 baton = obstack_alloc (&objfile->objfile_obstack,
0d53c4c4 16868 sizeof (struct dwarf2_loclist_baton));
4c2df51b 16869
8cf6f0b1 16870 fill_in_loclist_baton (cu, baton, attr);
be391dca 16871
d00adf39 16872 if (cu->base_known == 0)
0d53c4c4 16873 complaint (&symfile_complaints,
3e43a32a
MS
16874 _("Location list used without "
16875 "specifying the CU base address."));
4c2df51b 16876
768a979c 16877 SYMBOL_COMPUTED_OPS (sym) = &dwarf2_loclist_funcs;
0d53c4c4
DJ
16878 SYMBOL_LOCATION_BATON (sym) = baton;
16879 }
16880 else
16881 {
16882 struct dwarf2_locexpr_baton *baton;
16883
bb5ed363 16884 baton = obstack_alloc (&objfile->objfile_obstack,
0d53c4c4 16885 sizeof (struct dwarf2_locexpr_baton));
ae0d2f24
UW
16886 baton->per_cu = cu->per_cu;
16887 gdb_assert (baton->per_cu);
0d53c4c4
DJ
16888
16889 if (attr_form_is_block (attr))
16890 {
16891 /* Note that we're just copying the block's data pointer
16892 here, not the actual data. We're still pointing into the
6502dd73
DJ
16893 info_buffer for SYM's objfile; right now we never release
16894 that buffer, but when we do clean up properly this may
16895 need to change. */
0d53c4c4
DJ
16896 baton->size = DW_BLOCK (attr)->size;
16897 baton->data = DW_BLOCK (attr)->data;
16898 }
16899 else
16900 {
16901 dwarf2_invalid_attrib_class_complaint ("location description",
16902 SYMBOL_NATURAL_NAME (sym));
16903 baton->size = 0;
0d53c4c4 16904 }
6e70227d 16905
768a979c 16906 SYMBOL_COMPUTED_OPS (sym) = &dwarf2_locexpr_funcs;
0d53c4c4
DJ
16907 SYMBOL_LOCATION_BATON (sym) = baton;
16908 }
4c2df51b 16909}
6502dd73 16910
9aa1f1e3
TT
16911/* Return the OBJFILE associated with the compilation unit CU. If CU
16912 came from a separate debuginfo file, then the master objfile is
16913 returned. */
ae0d2f24
UW
16914
16915struct objfile *
16916dwarf2_per_cu_objfile (struct dwarf2_per_cu_data *per_cu)
16917{
9291a0cd 16918 struct objfile *objfile = per_cu->objfile;
ae0d2f24
UW
16919
16920 /* Return the master objfile, so that we can report and look up the
16921 correct file containing this variable. */
16922 if (objfile->separate_debug_objfile_backlink)
16923 objfile = objfile->separate_debug_objfile_backlink;
16924
16925 return objfile;
16926}
16927
96408a79
SA
16928/* Return comp_unit_head for PER_CU, either already available in PER_CU->CU
16929 (CU_HEADERP is unused in such case) or prepare a temporary copy at
16930 CU_HEADERP first. */
16931
16932static const struct comp_unit_head *
16933per_cu_header_read_in (struct comp_unit_head *cu_headerp,
16934 struct dwarf2_per_cu_data *per_cu)
16935{
96408a79
SA
16936 gdb_byte *info_ptr;
16937
16938 if (per_cu->cu)
16939 return &per_cu->cu->header;
16940
0bc3a05c 16941 info_ptr = per_cu->info_or_types_section->buffer + per_cu->offset.sect_off;
96408a79
SA
16942
16943 memset (cu_headerp, 0, sizeof (*cu_headerp));
0bc3a05c 16944 read_comp_unit_head (cu_headerp, info_ptr, per_cu->objfile->obfd);
96408a79
SA
16945
16946 return cu_headerp;
16947}
16948
ae0d2f24
UW
16949/* Return the address size given in the compilation unit header for CU. */
16950
98714339 16951int
ae0d2f24
UW
16952dwarf2_per_cu_addr_size (struct dwarf2_per_cu_data *per_cu)
16953{
96408a79
SA
16954 struct comp_unit_head cu_header_local;
16955 const struct comp_unit_head *cu_headerp;
c471e790 16956
96408a79
SA
16957 cu_headerp = per_cu_header_read_in (&cu_header_local, per_cu);
16958
16959 return cu_headerp->addr_size;
ae0d2f24
UW
16960}
16961
9eae7c52
TT
16962/* Return the offset size given in the compilation unit header for CU. */
16963
16964int
16965dwarf2_per_cu_offset_size (struct dwarf2_per_cu_data *per_cu)
16966{
96408a79
SA
16967 struct comp_unit_head cu_header_local;
16968 const struct comp_unit_head *cu_headerp;
9c6c53f7 16969
96408a79
SA
16970 cu_headerp = per_cu_header_read_in (&cu_header_local, per_cu);
16971
16972 return cu_headerp->offset_size;
16973}
16974
16975/* See its dwarf2loc.h declaration. */
16976
16977int
16978dwarf2_per_cu_ref_addr_size (struct dwarf2_per_cu_data *per_cu)
16979{
16980 struct comp_unit_head cu_header_local;
16981 const struct comp_unit_head *cu_headerp;
16982
16983 cu_headerp = per_cu_header_read_in (&cu_header_local, per_cu);
16984
16985 if (cu_headerp->version == 2)
16986 return cu_headerp->addr_size;
16987 else
16988 return cu_headerp->offset_size;
181cebd4
JK
16989}
16990
9aa1f1e3
TT
16991/* Return the text offset of the CU. The returned offset comes from
16992 this CU's objfile. If this objfile came from a separate debuginfo
16993 file, then the offset may be different from the corresponding
16994 offset in the parent objfile. */
16995
16996CORE_ADDR
16997dwarf2_per_cu_text_offset (struct dwarf2_per_cu_data *per_cu)
16998{
bb3fa9d0 16999 struct objfile *objfile = per_cu->objfile;
9aa1f1e3
TT
17000
17001 return ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
17002}
17003
348e048f
DE
17004/* Locate the .debug_info compilation unit from CU's objfile which contains
17005 the DIE at OFFSET. Raises an error on failure. */
ae038cb0
DJ
17006
17007static struct dwarf2_per_cu_data *
b64f50a1 17008dwarf2_find_containing_comp_unit (sect_offset offset,
ae038cb0
DJ
17009 struct objfile *objfile)
17010{
17011 struct dwarf2_per_cu_data *this_cu;
17012 int low, high;
17013
ae038cb0
DJ
17014 low = 0;
17015 high = dwarf2_per_objfile->n_comp_units - 1;
17016 while (high > low)
17017 {
17018 int mid = low + (high - low) / 2;
9a619af0 17019
b64f50a1
JK
17020 if (dwarf2_per_objfile->all_comp_units[mid]->offset.sect_off
17021 >= offset.sect_off)
ae038cb0
DJ
17022 high = mid;
17023 else
17024 low = mid + 1;
17025 }
17026 gdb_assert (low == high);
b64f50a1
JK
17027 if (dwarf2_per_objfile->all_comp_units[low]->offset.sect_off
17028 > offset.sect_off)
ae038cb0 17029 {
10b3939b 17030 if (low == 0)
8a3fe4f8
AC
17031 error (_("Dwarf Error: could not find partial DIE containing "
17032 "offset 0x%lx [in module %s]"),
b64f50a1 17033 (long) offset.sect_off, bfd_get_filename (objfile->obfd));
10b3939b 17034
b64f50a1
JK
17035 gdb_assert (dwarf2_per_objfile->all_comp_units[low-1]->offset.sect_off
17036 <= offset.sect_off);
ae038cb0
DJ
17037 return dwarf2_per_objfile->all_comp_units[low-1];
17038 }
17039 else
17040 {
17041 this_cu = dwarf2_per_objfile->all_comp_units[low];
17042 if (low == dwarf2_per_objfile->n_comp_units - 1
b64f50a1
JK
17043 && offset.sect_off >= this_cu->offset.sect_off + this_cu->length)
17044 error (_("invalid dwarf2 offset %u"), offset.sect_off);
17045 gdb_assert (offset.sect_off < this_cu->offset.sect_off + this_cu->length);
ae038cb0
DJ
17046 return this_cu;
17047 }
17048}
17049
23745b47 17050/* Initialize dwarf2_cu CU, owned by PER_CU. */
93311388 17051
9816fde3 17052static void
23745b47 17053init_one_comp_unit (struct dwarf2_cu *cu, struct dwarf2_per_cu_data *per_cu)
93311388 17054{
9816fde3 17055 memset (cu, 0, sizeof (*cu));
23745b47
DE
17056 per_cu->cu = cu;
17057 cu->per_cu = per_cu;
17058 cu->objfile = per_cu->objfile;
93311388 17059 obstack_init (&cu->comp_unit_obstack);
9816fde3
JK
17060}
17061
17062/* Initialize basic fields of dwarf_cu CU according to DIE COMP_UNIT_DIE. */
17063
17064static void
95554aad
TT
17065prepare_one_comp_unit (struct dwarf2_cu *cu, struct die_info *comp_unit_die,
17066 enum language pretend_language)
9816fde3
JK
17067{
17068 struct attribute *attr;
17069
17070 /* Set the language we're debugging. */
17071 attr = dwarf2_attr (comp_unit_die, DW_AT_language, cu);
17072 if (attr)
17073 set_cu_language (DW_UNSND (attr), cu);
17074 else
9cded63f 17075 {
95554aad 17076 cu->language = pretend_language;
9cded63f
TT
17077 cu->language_defn = language_def (cu->language);
17078 }
dee91e82
DE
17079
17080 attr = dwarf2_attr (comp_unit_die, DW_AT_producer, cu);
17081 if (attr)
17082 cu->producer = DW_STRING (attr);
93311388
DE
17083}
17084
ae038cb0
DJ
17085/* Release one cached compilation unit, CU. We unlink it from the tree
17086 of compilation units, but we don't remove it from the read_in_chain;
93311388
DE
17087 the caller is responsible for that.
17088 NOTE: DATA is a void * because this function is also used as a
17089 cleanup routine. */
ae038cb0
DJ
17090
17091static void
68dc6402 17092free_heap_comp_unit (void *data)
ae038cb0
DJ
17093{
17094 struct dwarf2_cu *cu = data;
17095
23745b47
DE
17096 gdb_assert (cu->per_cu != NULL);
17097 cu->per_cu->cu = NULL;
ae038cb0
DJ
17098 cu->per_cu = NULL;
17099
17100 obstack_free (&cu->comp_unit_obstack, NULL);
17101
17102 xfree (cu);
17103}
17104
72bf9492 17105/* This cleanup function is passed the address of a dwarf2_cu on the stack
ae038cb0 17106 when we're finished with it. We can't free the pointer itself, but be
dee91e82 17107 sure to unlink it from the cache. Also release any associated storage. */
72bf9492
DJ
17108
17109static void
17110free_stack_comp_unit (void *data)
17111{
17112 struct dwarf2_cu *cu = data;
17113
23745b47
DE
17114 gdb_assert (cu->per_cu != NULL);
17115 cu->per_cu->cu = NULL;
17116 cu->per_cu = NULL;
17117
72bf9492
DJ
17118 obstack_free (&cu->comp_unit_obstack, NULL);
17119 cu->partial_dies = NULL;
ae038cb0
DJ
17120}
17121
17122/* Free all cached compilation units. */
17123
17124static void
17125free_cached_comp_units (void *data)
17126{
17127 struct dwarf2_per_cu_data *per_cu, **last_chain;
17128
17129 per_cu = dwarf2_per_objfile->read_in_chain;
17130 last_chain = &dwarf2_per_objfile->read_in_chain;
17131 while (per_cu != NULL)
17132 {
17133 struct dwarf2_per_cu_data *next_cu;
17134
17135 next_cu = per_cu->cu->read_in_chain;
17136
68dc6402 17137 free_heap_comp_unit (per_cu->cu);
ae038cb0
DJ
17138 *last_chain = next_cu;
17139
17140 per_cu = next_cu;
17141 }
17142}
17143
17144/* Increase the age counter on each cached compilation unit, and free
17145 any that are too old. */
17146
17147static void
17148age_cached_comp_units (void)
17149{
17150 struct dwarf2_per_cu_data *per_cu, **last_chain;
17151
17152 dwarf2_clear_marks (dwarf2_per_objfile->read_in_chain);
17153 per_cu = dwarf2_per_objfile->read_in_chain;
17154 while (per_cu != NULL)
17155 {
17156 per_cu->cu->last_used ++;
17157 if (per_cu->cu->last_used <= dwarf2_max_cache_age)
17158 dwarf2_mark (per_cu->cu);
17159 per_cu = per_cu->cu->read_in_chain;
17160 }
17161
17162 per_cu = dwarf2_per_objfile->read_in_chain;
17163 last_chain = &dwarf2_per_objfile->read_in_chain;
17164 while (per_cu != NULL)
17165 {
17166 struct dwarf2_per_cu_data *next_cu;
17167
17168 next_cu = per_cu->cu->read_in_chain;
17169
17170 if (!per_cu->cu->mark)
17171 {
68dc6402 17172 free_heap_comp_unit (per_cu->cu);
ae038cb0
DJ
17173 *last_chain = next_cu;
17174 }
17175 else
17176 last_chain = &per_cu->cu->read_in_chain;
17177
17178 per_cu = next_cu;
17179 }
17180}
17181
17182/* Remove a single compilation unit from the cache. */
17183
17184static void
dee91e82 17185free_one_cached_comp_unit (struct dwarf2_per_cu_data *target_per_cu)
ae038cb0
DJ
17186{
17187 struct dwarf2_per_cu_data *per_cu, **last_chain;
17188
17189 per_cu = dwarf2_per_objfile->read_in_chain;
17190 last_chain = &dwarf2_per_objfile->read_in_chain;
17191 while (per_cu != NULL)
17192 {
17193 struct dwarf2_per_cu_data *next_cu;
17194
17195 next_cu = per_cu->cu->read_in_chain;
17196
dee91e82 17197 if (per_cu == target_per_cu)
ae038cb0 17198 {
68dc6402 17199 free_heap_comp_unit (per_cu->cu);
dee91e82 17200 per_cu->cu = NULL;
ae038cb0
DJ
17201 *last_chain = next_cu;
17202 break;
17203 }
17204 else
17205 last_chain = &per_cu->cu->read_in_chain;
17206
17207 per_cu = next_cu;
17208 }
17209}
17210
fe3e1990
DJ
17211/* Release all extra memory associated with OBJFILE. */
17212
17213void
17214dwarf2_free_objfile (struct objfile *objfile)
17215{
17216 dwarf2_per_objfile = objfile_data (objfile, dwarf2_objfile_data_key);
17217
17218 if (dwarf2_per_objfile == NULL)
17219 return;
17220
17221 /* Cached DIE trees use xmalloc and the comp_unit_obstack. */
17222 free_cached_comp_units (NULL);
17223
7b9f3c50
DE
17224 if (dwarf2_per_objfile->quick_file_names_table)
17225 htab_delete (dwarf2_per_objfile->quick_file_names_table);
9291a0cd 17226
fe3e1990
DJ
17227 /* Everything else should be on the objfile obstack. */
17228}
17229
dee91e82
DE
17230/* A set of CU "per_cu" pointer, DIE offset, and GDB type pointer.
17231 We store these in a hash table separate from the DIEs, and preserve them
17232 when the DIEs are flushed out of cache.
17233
17234 The CU "per_cu" pointer is needed because offset alone is not enough to
3019eac3
DE
17235 uniquely identify the type. A file may have multiple .debug_types sections,
17236 or the type may come from a DWO file. We have to use something in
17237 dwarf2_per_cu_data (or the pointer to it) because we can enter the lookup
17238 routine, get_die_type_at_offset, from outside this file, and thus won't
17239 necessarily have PER_CU->cu. Fortunately, PER_CU is stable for the life
17240 of the objfile. */
1c379e20 17241
dee91e82 17242struct dwarf2_per_cu_offset_and_type
1c379e20 17243{
dee91e82 17244 const struct dwarf2_per_cu_data *per_cu;
b64f50a1 17245 sect_offset offset;
1c379e20
DJ
17246 struct type *type;
17247};
17248
dee91e82 17249/* Hash function for a dwarf2_per_cu_offset_and_type. */
1c379e20
DJ
17250
17251static hashval_t
dee91e82 17252per_cu_offset_and_type_hash (const void *item)
1c379e20 17253{
dee91e82 17254 const struct dwarf2_per_cu_offset_and_type *ofs = item;
9a619af0 17255
dee91e82 17256 return (uintptr_t) ofs->per_cu + ofs->offset.sect_off;
1c379e20
DJ
17257}
17258
dee91e82 17259/* Equality function for a dwarf2_per_cu_offset_and_type. */
1c379e20
DJ
17260
17261static int
dee91e82 17262per_cu_offset_and_type_eq (const void *item_lhs, const void *item_rhs)
1c379e20 17263{
dee91e82
DE
17264 const struct dwarf2_per_cu_offset_and_type *ofs_lhs = item_lhs;
17265 const struct dwarf2_per_cu_offset_and_type *ofs_rhs = item_rhs;
9a619af0 17266
dee91e82
DE
17267 return (ofs_lhs->per_cu == ofs_rhs->per_cu
17268 && ofs_lhs->offset.sect_off == ofs_rhs->offset.sect_off);
1c379e20
DJ
17269}
17270
17271/* Set the type associated with DIE to TYPE. Save it in CU's hash
7e314c57
JK
17272 table if necessary. For convenience, return TYPE.
17273
17274 The DIEs reading must have careful ordering to:
17275 * Not cause infite loops trying to read in DIEs as a prerequisite for
17276 reading current DIE.
17277 * Not trying to dereference contents of still incompletely read in types
17278 while reading in other DIEs.
17279 * Enable referencing still incompletely read in types just by a pointer to
17280 the type without accessing its fields.
17281
17282 Therefore caller should follow these rules:
17283 * Try to fetch any prerequisite types we may need to build this DIE type
17284 before building the type and calling set_die_type.
e71ec853 17285 * After building type call set_die_type for current DIE as soon as
7e314c57
JK
17286 possible before fetching more types to complete the current type.
17287 * Make the type as complete as possible before fetching more types. */
1c379e20 17288
f792889a 17289static struct type *
1c379e20
DJ
17290set_die_type (struct die_info *die, struct type *type, struct dwarf2_cu *cu)
17291{
dee91e82 17292 struct dwarf2_per_cu_offset_and_type **slot, ofs;
673bfd45 17293 struct objfile *objfile = cu->objfile;
1c379e20 17294
b4ba55a1
JB
17295 /* For Ada types, make sure that the gnat-specific data is always
17296 initialized (if not already set). There are a few types where
17297 we should not be doing so, because the type-specific area is
17298 already used to hold some other piece of info (eg: TYPE_CODE_FLT
17299 where the type-specific area is used to store the floatformat).
17300 But this is not a problem, because the gnat-specific information
17301 is actually not needed for these types. */
17302 if (need_gnat_info (cu)
17303 && TYPE_CODE (type) != TYPE_CODE_FUNC
17304 && TYPE_CODE (type) != TYPE_CODE_FLT
17305 && !HAVE_GNAT_AUX_INFO (type))
17306 INIT_GNAT_SPECIFIC (type);
17307
dee91e82 17308 if (dwarf2_per_objfile->die_type_hash == NULL)
f792889a 17309 {
dee91e82
DE
17310 dwarf2_per_objfile->die_type_hash =
17311 htab_create_alloc_ex (127,
17312 per_cu_offset_and_type_hash,
17313 per_cu_offset_and_type_eq,
17314 NULL,
17315 &objfile->objfile_obstack,
17316 hashtab_obstack_allocate,
17317 dummy_obstack_deallocate);
f792889a 17318 }
1c379e20 17319
dee91e82 17320 ofs.per_cu = cu->per_cu;
1c379e20
DJ
17321 ofs.offset = die->offset;
17322 ofs.type = type;
dee91e82
DE
17323 slot = (struct dwarf2_per_cu_offset_and_type **)
17324 htab_find_slot (dwarf2_per_objfile->die_type_hash, &ofs, INSERT);
7e314c57
JK
17325 if (*slot)
17326 complaint (&symfile_complaints,
17327 _("A problem internal to GDB: DIE 0x%x has type already set"),
b64f50a1 17328 die->offset.sect_off);
673bfd45 17329 *slot = obstack_alloc (&objfile->objfile_obstack, sizeof (**slot));
1c379e20 17330 **slot = ofs;
f792889a 17331 return type;
1c379e20
DJ
17332}
17333
380bca97 17334/* Look up the type for the die at OFFSET in the appropriate type_hash
673bfd45 17335 table, or return NULL if the die does not have a saved type. */
1c379e20
DJ
17336
17337static struct type *
b64f50a1 17338get_die_type_at_offset (sect_offset offset,
673bfd45 17339 struct dwarf2_per_cu_data *per_cu)
1c379e20 17340{
dee91e82 17341 struct dwarf2_per_cu_offset_and_type *slot, ofs;
f792889a 17342
dee91e82 17343 if (dwarf2_per_objfile->die_type_hash == NULL)
f792889a 17344 return NULL;
1c379e20 17345
dee91e82 17346 ofs.per_cu = per_cu;
673bfd45 17347 ofs.offset = offset;
dee91e82 17348 slot = htab_find (dwarf2_per_objfile->die_type_hash, &ofs);
1c379e20
DJ
17349 if (slot)
17350 return slot->type;
17351 else
17352 return NULL;
17353}
17354
673bfd45
DE
17355/* Look up the type for DIE in the appropriate type_hash table,
17356 or return NULL if DIE does not have a saved type. */
17357
17358static struct type *
17359get_die_type (struct die_info *die, struct dwarf2_cu *cu)
17360{
17361 return get_die_type_at_offset (die->offset, cu->per_cu);
17362}
17363
10b3939b
DJ
17364/* Add a dependence relationship from CU to REF_PER_CU. */
17365
17366static void
17367dwarf2_add_dependence (struct dwarf2_cu *cu,
17368 struct dwarf2_per_cu_data *ref_per_cu)
17369{
17370 void **slot;
17371
17372 if (cu->dependencies == NULL)
17373 cu->dependencies
17374 = htab_create_alloc_ex (5, htab_hash_pointer, htab_eq_pointer,
17375 NULL, &cu->comp_unit_obstack,
17376 hashtab_obstack_allocate,
17377 dummy_obstack_deallocate);
17378
17379 slot = htab_find_slot (cu->dependencies, ref_per_cu, INSERT);
17380 if (*slot == NULL)
17381 *slot = ref_per_cu;
17382}
1c379e20 17383
f504f079
DE
17384/* Subroutine of dwarf2_mark to pass to htab_traverse.
17385 Set the mark field in every compilation unit in the
ae038cb0
DJ
17386 cache that we must keep because we are keeping CU. */
17387
10b3939b
DJ
17388static int
17389dwarf2_mark_helper (void **slot, void *data)
17390{
17391 struct dwarf2_per_cu_data *per_cu;
17392
17393 per_cu = (struct dwarf2_per_cu_data *) *slot;
d07ed419
JK
17394
17395 /* cu->dependencies references may not yet have been ever read if QUIT aborts
17396 reading of the chain. As such dependencies remain valid it is not much
17397 useful to track and undo them during QUIT cleanups. */
17398 if (per_cu->cu == NULL)
17399 return 1;
17400
10b3939b
DJ
17401 if (per_cu->cu->mark)
17402 return 1;
17403 per_cu->cu->mark = 1;
17404
17405 if (per_cu->cu->dependencies != NULL)
17406 htab_traverse (per_cu->cu->dependencies, dwarf2_mark_helper, NULL);
17407
17408 return 1;
17409}
17410
f504f079
DE
17411/* Set the mark field in CU and in every other compilation unit in the
17412 cache that we must keep because we are keeping CU. */
17413
ae038cb0
DJ
17414static void
17415dwarf2_mark (struct dwarf2_cu *cu)
17416{
17417 if (cu->mark)
17418 return;
17419 cu->mark = 1;
10b3939b
DJ
17420 if (cu->dependencies != NULL)
17421 htab_traverse (cu->dependencies, dwarf2_mark_helper, NULL);
ae038cb0
DJ
17422}
17423
17424static void
17425dwarf2_clear_marks (struct dwarf2_per_cu_data *per_cu)
17426{
17427 while (per_cu)
17428 {
17429 per_cu->cu->mark = 0;
17430 per_cu = per_cu->cu->read_in_chain;
17431 }
72bf9492
DJ
17432}
17433
72bf9492
DJ
17434/* Trivial hash function for partial_die_info: the hash value of a DIE
17435 is its offset in .debug_info for this objfile. */
17436
17437static hashval_t
17438partial_die_hash (const void *item)
17439{
17440 const struct partial_die_info *part_die = item;
9a619af0 17441
b64f50a1 17442 return part_die->offset.sect_off;
72bf9492
DJ
17443}
17444
17445/* Trivial comparison function for partial_die_info structures: two DIEs
17446 are equal if they have the same offset. */
17447
17448static int
17449partial_die_eq (const void *item_lhs, const void *item_rhs)
17450{
17451 const struct partial_die_info *part_die_lhs = item_lhs;
17452 const struct partial_die_info *part_die_rhs = item_rhs;
9a619af0 17453
b64f50a1 17454 return part_die_lhs->offset.sect_off == part_die_rhs->offset.sect_off;
72bf9492
DJ
17455}
17456
ae038cb0
DJ
17457static struct cmd_list_element *set_dwarf2_cmdlist;
17458static struct cmd_list_element *show_dwarf2_cmdlist;
17459
17460static void
17461set_dwarf2_cmd (char *args, int from_tty)
17462{
17463 help_list (set_dwarf2_cmdlist, "maintenance set dwarf2 ", -1, gdb_stdout);
17464}
17465
17466static void
17467show_dwarf2_cmd (char *args, int from_tty)
6e70227d 17468{
ae038cb0
DJ
17469 cmd_show_list (show_dwarf2_cmdlist, from_tty, "");
17470}
17471
dce234bc
PP
17472/* If section described by INFO was mmapped, munmap it now. */
17473
17474static void
17475munmap_section_buffer (struct dwarf2_section_info *info)
17476{
b315ab21 17477 if (info->map_addr != NULL)
dce234bc
PP
17478 {
17479#ifdef HAVE_MMAP
b315ab21 17480 int res;
9a619af0 17481
b315ab21
TG
17482 res = munmap (info->map_addr, info->map_len);
17483 gdb_assert (res == 0);
dce234bc
PP
17484#else
17485 /* Without HAVE_MMAP, we should never be here to begin with. */
f3574227 17486 gdb_assert_not_reached ("no mmap support");
dce234bc
PP
17487#endif
17488 }
17489}
17490
17491/* munmap debug sections for OBJFILE, if necessary. */
17492
17493static void
c1bd65d0 17494dwarf2_per_objfile_free (struct objfile *objfile, void *d)
dce234bc
PP
17495{
17496 struct dwarf2_per_objfile *data = d;
8b70b953
TT
17497 int ix;
17498 struct dwarf2_section_info *section;
9a619af0 17499
16be1145
DE
17500 /* This is sorted according to the order they're defined in to make it easier
17501 to keep in sync. */
dce234bc
PP
17502 munmap_section_buffer (&data->info);
17503 munmap_section_buffer (&data->abbrev);
17504 munmap_section_buffer (&data->line);
16be1145 17505 munmap_section_buffer (&data->loc);
dce234bc 17506 munmap_section_buffer (&data->macinfo);
cf2c3c16 17507 munmap_section_buffer (&data->macro);
16be1145 17508 munmap_section_buffer (&data->str);
dce234bc 17509 munmap_section_buffer (&data->ranges);
3019eac3 17510 munmap_section_buffer (&data->addr);
dce234bc
PP
17511 munmap_section_buffer (&data->frame);
17512 munmap_section_buffer (&data->eh_frame);
9291a0cd 17513 munmap_section_buffer (&data->gdb_index);
8b70b953
TT
17514
17515 for (ix = 0;
17516 VEC_iterate (dwarf2_section_info_def, data->types, ix, section);
17517 ++ix)
17518 munmap_section_buffer (section);
17519
95554aad
TT
17520 for (ix = 0; ix < dwarf2_per_objfile->n_comp_units; ++ix)
17521 VEC_free (dwarf2_per_cu_ptr,
17522 dwarf2_per_objfile->all_comp_units[ix]->imported_symtabs);
17523
8b70b953 17524 VEC_free (dwarf2_section_info_def, data->types);
3019eac3
DE
17525
17526 if (data->dwo_files)
17527 free_dwo_files (data->dwo_files, objfile);
9291a0cd
TT
17528}
17529
17530\f
ae2de4f8 17531/* The "save gdb-index" command. */
9291a0cd
TT
17532
17533/* The contents of the hash table we create when building the string
17534 table. */
17535struct strtab_entry
17536{
17537 offset_type offset;
17538 const char *str;
17539};
17540
559a7a62
JK
17541/* Hash function for a strtab_entry.
17542
17543 Function is used only during write_hash_table so no index format backward
17544 compatibility is needed. */
b89be57b 17545
9291a0cd
TT
17546static hashval_t
17547hash_strtab_entry (const void *e)
17548{
17549 const struct strtab_entry *entry = e;
559a7a62 17550 return mapped_index_string_hash (INT_MAX, entry->str);
9291a0cd
TT
17551}
17552
17553/* Equality function for a strtab_entry. */
b89be57b 17554
9291a0cd
TT
17555static int
17556eq_strtab_entry (const void *a, const void *b)
17557{
17558 const struct strtab_entry *ea = a;
17559 const struct strtab_entry *eb = b;
17560 return !strcmp (ea->str, eb->str);
17561}
17562
17563/* Create a strtab_entry hash table. */
b89be57b 17564
9291a0cd
TT
17565static htab_t
17566create_strtab (void)
17567{
17568 return htab_create_alloc (100, hash_strtab_entry, eq_strtab_entry,
17569 xfree, xcalloc, xfree);
17570}
17571
17572/* Add a string to the constant pool. Return the string's offset in
17573 host order. */
b89be57b 17574
9291a0cd
TT
17575static offset_type
17576add_string (htab_t table, struct obstack *cpool, const char *str)
17577{
17578 void **slot;
17579 struct strtab_entry entry;
17580 struct strtab_entry *result;
17581
17582 entry.str = str;
17583 slot = htab_find_slot (table, &entry, INSERT);
17584 if (*slot)
17585 result = *slot;
17586 else
17587 {
17588 result = XNEW (struct strtab_entry);
17589 result->offset = obstack_object_size (cpool);
17590 result->str = str;
17591 obstack_grow_str0 (cpool, str);
17592 *slot = result;
17593 }
17594 return result->offset;
17595}
17596
17597/* An entry in the symbol table. */
17598struct symtab_index_entry
17599{
17600 /* The name of the symbol. */
17601 const char *name;
17602 /* The offset of the name in the constant pool. */
17603 offset_type index_offset;
17604 /* A sorted vector of the indices of all the CUs that hold an object
17605 of this name. */
17606 VEC (offset_type) *cu_indices;
17607};
17608
17609/* The symbol table. This is a power-of-2-sized hash table. */
17610struct mapped_symtab
17611{
17612 offset_type n_elements;
17613 offset_type size;
17614 struct symtab_index_entry **data;
17615};
17616
17617/* Hash function for a symtab_index_entry. */
b89be57b 17618
9291a0cd
TT
17619static hashval_t
17620hash_symtab_entry (const void *e)
17621{
17622 const struct symtab_index_entry *entry = e;
17623 return iterative_hash (VEC_address (offset_type, entry->cu_indices),
17624 sizeof (offset_type) * VEC_length (offset_type,
17625 entry->cu_indices),
17626 0);
17627}
17628
17629/* Equality function for a symtab_index_entry. */
b89be57b 17630
9291a0cd
TT
17631static int
17632eq_symtab_entry (const void *a, const void *b)
17633{
17634 const struct symtab_index_entry *ea = a;
17635 const struct symtab_index_entry *eb = b;
17636 int len = VEC_length (offset_type, ea->cu_indices);
17637 if (len != VEC_length (offset_type, eb->cu_indices))
17638 return 0;
17639 return !memcmp (VEC_address (offset_type, ea->cu_indices),
17640 VEC_address (offset_type, eb->cu_indices),
17641 sizeof (offset_type) * len);
17642}
17643
17644/* Destroy a symtab_index_entry. */
b89be57b 17645
9291a0cd
TT
17646static void
17647delete_symtab_entry (void *p)
17648{
17649 struct symtab_index_entry *entry = p;
17650 VEC_free (offset_type, entry->cu_indices);
17651 xfree (entry);
17652}
17653
17654/* Create a hash table holding symtab_index_entry objects. */
b89be57b 17655
9291a0cd 17656static htab_t
3876f04e 17657create_symbol_hash_table (void)
9291a0cd
TT
17658{
17659 return htab_create_alloc (100, hash_symtab_entry, eq_symtab_entry,
17660 delete_symtab_entry, xcalloc, xfree);
17661}
17662
17663/* Create a new mapped symtab object. */
b89be57b 17664
9291a0cd
TT
17665static struct mapped_symtab *
17666create_mapped_symtab (void)
17667{
17668 struct mapped_symtab *symtab = XNEW (struct mapped_symtab);
17669 symtab->n_elements = 0;
17670 symtab->size = 1024;
17671 symtab->data = XCNEWVEC (struct symtab_index_entry *, symtab->size);
17672 return symtab;
17673}
17674
17675/* Destroy a mapped_symtab. */
b89be57b 17676
9291a0cd
TT
17677static void
17678cleanup_mapped_symtab (void *p)
17679{
17680 struct mapped_symtab *symtab = p;
17681 /* The contents of the array are freed when the other hash table is
17682 destroyed. */
17683 xfree (symtab->data);
17684 xfree (symtab);
17685}
17686
17687/* Find a slot in SYMTAB for the symbol NAME. Returns a pointer to
559a7a62
JK
17688 the slot.
17689
17690 Function is used only during write_hash_table so no index format backward
17691 compatibility is needed. */
b89be57b 17692
9291a0cd
TT
17693static struct symtab_index_entry **
17694find_slot (struct mapped_symtab *symtab, const char *name)
17695{
559a7a62 17696 offset_type index, step, hash = mapped_index_string_hash (INT_MAX, name);
9291a0cd
TT
17697
17698 index = hash & (symtab->size - 1);
17699 step = ((hash * 17) & (symtab->size - 1)) | 1;
17700
17701 for (;;)
17702 {
17703 if (!symtab->data[index] || !strcmp (name, symtab->data[index]->name))
17704 return &symtab->data[index];
17705 index = (index + step) & (symtab->size - 1);
17706 }
17707}
17708
17709/* Expand SYMTAB's hash table. */
b89be57b 17710
9291a0cd
TT
17711static void
17712hash_expand (struct mapped_symtab *symtab)
17713{
17714 offset_type old_size = symtab->size;
17715 offset_type i;
17716 struct symtab_index_entry **old_entries = symtab->data;
17717
17718 symtab->size *= 2;
17719 symtab->data = XCNEWVEC (struct symtab_index_entry *, symtab->size);
17720
17721 for (i = 0; i < old_size; ++i)
17722 {
17723 if (old_entries[i])
17724 {
17725 struct symtab_index_entry **slot = find_slot (symtab,
17726 old_entries[i]->name);
17727 *slot = old_entries[i];
17728 }
17729 }
17730
17731 xfree (old_entries);
17732}
17733
156942c7
DE
17734/* Add an entry to SYMTAB. NAME is the name of the symbol.
17735 CU_INDEX is the index of the CU in which the symbol appears.
17736 IS_STATIC is one if the symbol is static, otherwise zero (global). */
b89be57b 17737
9291a0cd
TT
17738static void
17739add_index_entry (struct mapped_symtab *symtab, const char *name,
156942c7 17740 int is_static, gdb_index_symbol_kind kind,
9291a0cd
TT
17741 offset_type cu_index)
17742{
17743 struct symtab_index_entry **slot;
156942c7 17744 offset_type cu_index_and_attrs;
9291a0cd
TT
17745
17746 ++symtab->n_elements;
17747 if (4 * symtab->n_elements / 3 >= symtab->size)
17748 hash_expand (symtab);
17749
17750 slot = find_slot (symtab, name);
17751 if (!*slot)
17752 {
17753 *slot = XNEW (struct symtab_index_entry);
17754 (*slot)->name = name;
156942c7 17755 /* index_offset is set later. */
9291a0cd
TT
17756 (*slot)->cu_indices = NULL;
17757 }
156942c7
DE
17758
17759 cu_index_and_attrs = 0;
17760 DW2_GDB_INDEX_CU_SET_VALUE (cu_index_and_attrs, cu_index);
17761 DW2_GDB_INDEX_SYMBOL_STATIC_SET_VALUE (cu_index_and_attrs, is_static);
17762 DW2_GDB_INDEX_SYMBOL_KIND_SET_VALUE (cu_index_and_attrs, kind);
17763
17764 /* We don't want to record an index value twice as we want to avoid the
17765 duplication.
17766 We process all global symbols and then all static symbols
17767 (which would allow us to avoid the duplication by only having to check
17768 the last entry pushed), but a symbol could have multiple kinds in one CU.
17769 To keep things simple we don't worry about the duplication here and
17770 sort and uniqufy the list after we've processed all symbols. */
17771 VEC_safe_push (offset_type, (*slot)->cu_indices, cu_index_and_attrs);
17772}
17773
17774/* qsort helper routine for uniquify_cu_indices. */
17775
17776static int
17777offset_type_compare (const void *ap, const void *bp)
17778{
17779 offset_type a = *(offset_type *) ap;
17780 offset_type b = *(offset_type *) bp;
17781
17782 return (a > b) - (b > a);
17783}
17784
17785/* Sort and remove duplicates of all symbols' cu_indices lists. */
17786
17787static void
17788uniquify_cu_indices (struct mapped_symtab *symtab)
17789{
17790 int i;
17791
17792 for (i = 0; i < symtab->size; ++i)
17793 {
17794 struct symtab_index_entry *entry = symtab->data[i];
17795
17796 if (entry
17797 && entry->cu_indices != NULL)
17798 {
17799 unsigned int next_to_insert, next_to_check;
17800 offset_type last_value;
17801
17802 qsort (VEC_address (offset_type, entry->cu_indices),
17803 VEC_length (offset_type, entry->cu_indices),
17804 sizeof (offset_type), offset_type_compare);
17805
17806 last_value = VEC_index (offset_type, entry->cu_indices, 0);
17807 next_to_insert = 1;
17808 for (next_to_check = 1;
17809 next_to_check < VEC_length (offset_type, entry->cu_indices);
17810 ++next_to_check)
17811 {
17812 if (VEC_index (offset_type, entry->cu_indices, next_to_check)
17813 != last_value)
17814 {
17815 last_value = VEC_index (offset_type, entry->cu_indices,
17816 next_to_check);
17817 VEC_replace (offset_type, entry->cu_indices, next_to_insert,
17818 last_value);
17819 ++next_to_insert;
17820 }
17821 }
17822 VEC_truncate (offset_type, entry->cu_indices, next_to_insert);
17823 }
17824 }
9291a0cd
TT
17825}
17826
17827/* Add a vector of indices to the constant pool. */
b89be57b 17828
9291a0cd 17829static offset_type
3876f04e 17830add_indices_to_cpool (htab_t symbol_hash_table, struct obstack *cpool,
9291a0cd
TT
17831 struct symtab_index_entry *entry)
17832{
17833 void **slot;
17834
3876f04e 17835 slot = htab_find_slot (symbol_hash_table, entry, INSERT);
9291a0cd
TT
17836 if (!*slot)
17837 {
17838 offset_type len = VEC_length (offset_type, entry->cu_indices);
17839 offset_type val = MAYBE_SWAP (len);
17840 offset_type iter;
17841 int i;
17842
17843 *slot = entry;
17844 entry->index_offset = obstack_object_size (cpool);
17845
17846 obstack_grow (cpool, &val, sizeof (val));
17847 for (i = 0;
17848 VEC_iterate (offset_type, entry->cu_indices, i, iter);
17849 ++i)
17850 {
17851 val = MAYBE_SWAP (iter);
17852 obstack_grow (cpool, &val, sizeof (val));
17853 }
17854 }
17855 else
17856 {
17857 struct symtab_index_entry *old_entry = *slot;
17858 entry->index_offset = old_entry->index_offset;
17859 entry = old_entry;
17860 }
17861 return entry->index_offset;
17862}
17863
17864/* Write the mapped hash table SYMTAB to the obstack OUTPUT, with
17865 constant pool entries going into the obstack CPOOL. */
b89be57b 17866
9291a0cd
TT
17867static void
17868write_hash_table (struct mapped_symtab *symtab,
17869 struct obstack *output, struct obstack *cpool)
17870{
17871 offset_type i;
3876f04e 17872 htab_t symbol_hash_table;
9291a0cd
TT
17873 htab_t str_table;
17874
3876f04e 17875 symbol_hash_table = create_symbol_hash_table ();
9291a0cd 17876 str_table = create_strtab ();
3876f04e 17877
9291a0cd
TT
17878 /* We add all the index vectors to the constant pool first, to
17879 ensure alignment is ok. */
17880 for (i = 0; i < symtab->size; ++i)
17881 {
17882 if (symtab->data[i])
3876f04e 17883 add_indices_to_cpool (symbol_hash_table, cpool, symtab->data[i]);
9291a0cd
TT
17884 }
17885
17886 /* Now write out the hash table. */
17887 for (i = 0; i < symtab->size; ++i)
17888 {
17889 offset_type str_off, vec_off;
17890
17891 if (symtab->data[i])
17892 {
17893 str_off = add_string (str_table, cpool, symtab->data[i]->name);
17894 vec_off = symtab->data[i]->index_offset;
17895 }
17896 else
17897 {
17898 /* While 0 is a valid constant pool index, it is not valid
17899 to have 0 for both offsets. */
17900 str_off = 0;
17901 vec_off = 0;
17902 }
17903
17904 str_off = MAYBE_SWAP (str_off);
17905 vec_off = MAYBE_SWAP (vec_off);
17906
17907 obstack_grow (output, &str_off, sizeof (str_off));
17908 obstack_grow (output, &vec_off, sizeof (vec_off));
17909 }
17910
17911 htab_delete (str_table);
3876f04e 17912 htab_delete (symbol_hash_table);
9291a0cd
TT
17913}
17914
0a5429f6
DE
17915/* Struct to map psymtab to CU index in the index file. */
17916struct psymtab_cu_index_map
17917{
17918 struct partial_symtab *psymtab;
17919 unsigned int cu_index;
17920};
17921
17922static hashval_t
17923hash_psymtab_cu_index (const void *item)
17924{
17925 const struct psymtab_cu_index_map *map = item;
17926
17927 return htab_hash_pointer (map->psymtab);
17928}
17929
17930static int
17931eq_psymtab_cu_index (const void *item_lhs, const void *item_rhs)
17932{
17933 const struct psymtab_cu_index_map *lhs = item_lhs;
17934 const struct psymtab_cu_index_map *rhs = item_rhs;
17935
17936 return lhs->psymtab == rhs->psymtab;
17937}
17938
17939/* Helper struct for building the address table. */
17940struct addrmap_index_data
17941{
17942 struct objfile *objfile;
17943 struct obstack *addr_obstack;
17944 htab_t cu_index_htab;
17945
17946 /* Non-zero if the previous_* fields are valid.
17947 We can't write an entry until we see the next entry (since it is only then
17948 that we know the end of the entry). */
17949 int previous_valid;
17950 /* Index of the CU in the table of all CUs in the index file. */
17951 unsigned int previous_cu_index;
0963b4bd 17952 /* Start address of the CU. */
0a5429f6
DE
17953 CORE_ADDR previous_cu_start;
17954};
17955
17956/* Write an address entry to OBSTACK. */
b89be57b 17957
9291a0cd 17958static void
0a5429f6
DE
17959add_address_entry (struct objfile *objfile, struct obstack *obstack,
17960 CORE_ADDR start, CORE_ADDR end, unsigned int cu_index)
9291a0cd 17961{
0a5429f6 17962 offset_type cu_index_to_write;
9291a0cd
TT
17963 char addr[8];
17964 CORE_ADDR baseaddr;
17965
17966 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
17967
0a5429f6
DE
17968 store_unsigned_integer (addr, 8, BFD_ENDIAN_LITTLE, start - baseaddr);
17969 obstack_grow (obstack, addr, 8);
17970 store_unsigned_integer (addr, 8, BFD_ENDIAN_LITTLE, end - baseaddr);
17971 obstack_grow (obstack, addr, 8);
17972 cu_index_to_write = MAYBE_SWAP (cu_index);
17973 obstack_grow (obstack, &cu_index_to_write, sizeof (offset_type));
17974}
17975
17976/* Worker function for traversing an addrmap to build the address table. */
17977
17978static int
17979add_address_entry_worker (void *datap, CORE_ADDR start_addr, void *obj)
17980{
17981 struct addrmap_index_data *data = datap;
17982 struct partial_symtab *pst = obj;
0a5429f6
DE
17983
17984 if (data->previous_valid)
17985 add_address_entry (data->objfile, data->addr_obstack,
17986 data->previous_cu_start, start_addr,
17987 data->previous_cu_index);
17988
17989 data->previous_cu_start = start_addr;
17990 if (pst != NULL)
17991 {
17992 struct psymtab_cu_index_map find_map, *map;
17993 find_map.psymtab = pst;
17994 map = htab_find (data->cu_index_htab, &find_map);
17995 gdb_assert (map != NULL);
17996 data->previous_cu_index = map->cu_index;
17997 data->previous_valid = 1;
17998 }
17999 else
18000 data->previous_valid = 0;
18001
18002 return 0;
18003}
18004
18005/* Write OBJFILE's address map to OBSTACK.
18006 CU_INDEX_HTAB is used to map addrmap entries to their CU indices
18007 in the index file. */
18008
18009static void
18010write_address_map (struct objfile *objfile, struct obstack *obstack,
18011 htab_t cu_index_htab)
18012{
18013 struct addrmap_index_data addrmap_index_data;
18014
18015 /* When writing the address table, we have to cope with the fact that
18016 the addrmap iterator only provides the start of a region; we have to
18017 wait until the next invocation to get the start of the next region. */
18018
18019 addrmap_index_data.objfile = objfile;
18020 addrmap_index_data.addr_obstack = obstack;
18021 addrmap_index_data.cu_index_htab = cu_index_htab;
18022 addrmap_index_data.previous_valid = 0;
18023
18024 addrmap_foreach (objfile->psymtabs_addrmap, add_address_entry_worker,
18025 &addrmap_index_data);
18026
18027 /* It's highly unlikely the last entry (end address = 0xff...ff)
18028 is valid, but we should still handle it.
18029 The end address is recorded as the start of the next region, but that
18030 doesn't work here. To cope we pass 0xff...ff, this is a rare situation
18031 anyway. */
18032 if (addrmap_index_data.previous_valid)
18033 add_address_entry (objfile, obstack,
18034 addrmap_index_data.previous_cu_start, (CORE_ADDR) -1,
18035 addrmap_index_data.previous_cu_index);
9291a0cd
TT
18036}
18037
156942c7
DE
18038/* Return the symbol kind of PSYM. */
18039
18040static gdb_index_symbol_kind
18041symbol_kind (struct partial_symbol *psym)
18042{
18043 domain_enum domain = PSYMBOL_DOMAIN (psym);
18044 enum address_class aclass = PSYMBOL_CLASS (psym);
18045
18046 switch (domain)
18047 {
18048 case VAR_DOMAIN:
18049 switch (aclass)
18050 {
18051 case LOC_BLOCK:
18052 return GDB_INDEX_SYMBOL_KIND_FUNCTION;
18053 case LOC_TYPEDEF:
18054 return GDB_INDEX_SYMBOL_KIND_TYPE;
18055 case LOC_COMPUTED:
18056 case LOC_CONST_BYTES:
18057 case LOC_OPTIMIZED_OUT:
18058 case LOC_STATIC:
18059 return GDB_INDEX_SYMBOL_KIND_VARIABLE;
18060 case LOC_CONST:
18061 /* Note: It's currently impossible to recognize psyms as enum values
18062 short of reading the type info. For now punt. */
18063 return GDB_INDEX_SYMBOL_KIND_VARIABLE;
18064 default:
18065 /* There are other LOC_FOO values that one might want to classify
18066 as variables, but dwarf2read.c doesn't currently use them. */
18067 return GDB_INDEX_SYMBOL_KIND_OTHER;
18068 }
18069 case STRUCT_DOMAIN:
18070 return GDB_INDEX_SYMBOL_KIND_TYPE;
18071 default:
18072 return GDB_INDEX_SYMBOL_KIND_OTHER;
18073 }
18074}
18075
9291a0cd 18076/* Add a list of partial symbols to SYMTAB. */
b89be57b 18077
9291a0cd
TT
18078static void
18079write_psymbols (struct mapped_symtab *symtab,
987d643c 18080 htab_t psyms_seen,
9291a0cd
TT
18081 struct partial_symbol **psymp,
18082 int count,
987d643c
TT
18083 offset_type cu_index,
18084 int is_static)
9291a0cd
TT
18085{
18086 for (; count-- > 0; ++psymp)
18087 {
156942c7
DE
18088 struct partial_symbol *psym = *psymp;
18089 void **slot;
987d643c 18090
156942c7 18091 if (SYMBOL_LANGUAGE (psym) == language_ada)
9291a0cd 18092 error (_("Ada is not currently supported by the index"));
987d643c 18093
987d643c 18094 /* Only add a given psymbol once. */
156942c7 18095 slot = htab_find_slot (psyms_seen, psym, INSERT);
987d643c
TT
18096 if (!*slot)
18097 {
156942c7
DE
18098 gdb_index_symbol_kind kind = symbol_kind (psym);
18099
18100 *slot = psym;
18101 add_index_entry (symtab, SYMBOL_SEARCH_NAME (psym),
18102 is_static, kind, cu_index);
987d643c 18103 }
9291a0cd
TT
18104 }
18105}
18106
18107/* Write the contents of an ("unfinished") obstack to FILE. Throw an
18108 exception if there is an error. */
b89be57b 18109
9291a0cd
TT
18110static void
18111write_obstack (FILE *file, struct obstack *obstack)
18112{
18113 if (fwrite (obstack_base (obstack), 1, obstack_object_size (obstack),
18114 file)
18115 != obstack_object_size (obstack))
18116 error (_("couldn't data write to file"));
18117}
18118
18119/* Unlink a file if the argument is not NULL. */
b89be57b 18120
9291a0cd
TT
18121static void
18122unlink_if_set (void *p)
18123{
18124 char **filename = p;
18125 if (*filename)
18126 unlink (*filename);
18127}
18128
1fd400ff
TT
18129/* A helper struct used when iterating over debug_types. */
18130struct signatured_type_index_data
18131{
18132 struct objfile *objfile;
18133 struct mapped_symtab *symtab;
18134 struct obstack *types_list;
987d643c 18135 htab_t psyms_seen;
1fd400ff
TT
18136 int cu_index;
18137};
18138
18139/* A helper function that writes a single signatured_type to an
18140 obstack. */
b89be57b 18141
1fd400ff
TT
18142static int
18143write_one_signatured_type (void **slot, void *d)
18144{
18145 struct signatured_type_index_data *info = d;
18146 struct signatured_type *entry = (struct signatured_type *) *slot;
e254ef6a
DE
18147 struct dwarf2_per_cu_data *per_cu = &entry->per_cu;
18148 struct partial_symtab *psymtab = per_cu->v.psymtab;
1fd400ff
TT
18149 gdb_byte val[8];
18150
18151 write_psymbols (info->symtab,
987d643c 18152 info->psyms_seen,
3e43a32a
MS
18153 info->objfile->global_psymbols.list
18154 + psymtab->globals_offset,
987d643c
TT
18155 psymtab->n_global_syms, info->cu_index,
18156 0);
1fd400ff 18157 write_psymbols (info->symtab,
987d643c 18158 info->psyms_seen,
3e43a32a
MS
18159 info->objfile->static_psymbols.list
18160 + psymtab->statics_offset,
987d643c
TT
18161 psymtab->n_static_syms, info->cu_index,
18162 1);
1fd400ff 18163
b64f50a1
JK
18164 store_unsigned_integer (val, 8, BFD_ENDIAN_LITTLE,
18165 entry->per_cu.offset.sect_off);
1fd400ff 18166 obstack_grow (info->types_list, val, 8);
3019eac3
DE
18167 store_unsigned_integer (val, 8, BFD_ENDIAN_LITTLE,
18168 entry->type_offset_in_tu.cu_off);
1fd400ff
TT
18169 obstack_grow (info->types_list, val, 8);
18170 store_unsigned_integer (val, 8, BFD_ENDIAN_LITTLE, entry->signature);
18171 obstack_grow (info->types_list, val, 8);
18172
18173 ++info->cu_index;
18174
18175 return 1;
18176}
18177
95554aad
TT
18178/* Recurse into all "included" dependencies and write their symbols as
18179 if they appeared in this psymtab. */
18180
18181static void
18182recursively_write_psymbols (struct objfile *objfile,
18183 struct partial_symtab *psymtab,
18184 struct mapped_symtab *symtab,
18185 htab_t psyms_seen,
18186 offset_type cu_index)
18187{
18188 int i;
18189
18190 for (i = 0; i < psymtab->number_of_dependencies; ++i)
18191 if (psymtab->dependencies[i]->user != NULL)
18192 recursively_write_psymbols (objfile, psymtab->dependencies[i],
18193 symtab, psyms_seen, cu_index);
18194
18195 write_psymbols (symtab,
18196 psyms_seen,
18197 objfile->global_psymbols.list + psymtab->globals_offset,
18198 psymtab->n_global_syms, cu_index,
18199 0);
18200 write_psymbols (symtab,
18201 psyms_seen,
18202 objfile->static_psymbols.list + psymtab->statics_offset,
18203 psymtab->n_static_syms, cu_index,
18204 1);
18205}
18206
9291a0cd 18207/* Create an index file for OBJFILE in the directory DIR. */
b89be57b 18208
9291a0cd
TT
18209static void
18210write_psymtabs_to_index (struct objfile *objfile, const char *dir)
18211{
18212 struct cleanup *cleanup;
18213 char *filename, *cleanup_filename;
1fd400ff
TT
18214 struct obstack contents, addr_obstack, constant_pool, symtab_obstack;
18215 struct obstack cu_list, types_cu_list;
9291a0cd
TT
18216 int i;
18217 FILE *out_file;
18218 struct mapped_symtab *symtab;
18219 offset_type val, size_of_contents, total_len;
18220 struct stat st;
987d643c 18221 htab_t psyms_seen;
0a5429f6
DE
18222 htab_t cu_index_htab;
18223 struct psymtab_cu_index_map *psymtab_cu_index_map;
9291a0cd 18224
b4f2f049 18225 if (!objfile->psymtabs || !objfile->psymtabs_addrmap)
9291a0cd 18226 return;
b4f2f049 18227
9291a0cd
TT
18228 if (dwarf2_per_objfile->using_index)
18229 error (_("Cannot use an index to create the index"));
18230
8b70b953
TT
18231 if (VEC_length (dwarf2_section_info_def, dwarf2_per_objfile->types) > 1)
18232 error (_("Cannot make an index when the file has multiple .debug_types sections"));
18233
9291a0cd 18234 if (stat (objfile->name, &st) < 0)
7e17e088 18235 perror_with_name (objfile->name);
9291a0cd
TT
18236
18237 filename = concat (dir, SLASH_STRING, lbasename (objfile->name),
18238 INDEX_SUFFIX, (char *) NULL);
18239 cleanup = make_cleanup (xfree, filename);
18240
18241 out_file = fopen (filename, "wb");
18242 if (!out_file)
18243 error (_("Can't open `%s' for writing"), filename);
18244
18245 cleanup_filename = filename;
18246 make_cleanup (unlink_if_set, &cleanup_filename);
18247
18248 symtab = create_mapped_symtab ();
18249 make_cleanup (cleanup_mapped_symtab, symtab);
18250
18251 obstack_init (&addr_obstack);
18252 make_cleanup_obstack_free (&addr_obstack);
18253
18254 obstack_init (&cu_list);
18255 make_cleanup_obstack_free (&cu_list);
18256
1fd400ff
TT
18257 obstack_init (&types_cu_list);
18258 make_cleanup_obstack_free (&types_cu_list);
18259
987d643c
TT
18260 psyms_seen = htab_create_alloc (100, htab_hash_pointer, htab_eq_pointer,
18261 NULL, xcalloc, xfree);
96408a79 18262 make_cleanup_htab_delete (psyms_seen);
987d643c 18263
0a5429f6
DE
18264 /* While we're scanning CU's create a table that maps a psymtab pointer
18265 (which is what addrmap records) to its index (which is what is recorded
18266 in the index file). This will later be needed to write the address
18267 table. */
18268 cu_index_htab = htab_create_alloc (100,
18269 hash_psymtab_cu_index,
18270 eq_psymtab_cu_index,
18271 NULL, xcalloc, xfree);
96408a79 18272 make_cleanup_htab_delete (cu_index_htab);
0a5429f6
DE
18273 psymtab_cu_index_map = (struct psymtab_cu_index_map *)
18274 xmalloc (sizeof (struct psymtab_cu_index_map)
18275 * dwarf2_per_objfile->n_comp_units);
18276 make_cleanup (xfree, psymtab_cu_index_map);
18277
18278 /* The CU list is already sorted, so we don't need to do additional
1fd400ff
TT
18279 work here. Also, the debug_types entries do not appear in
18280 all_comp_units, but only in their own hash table. */
9291a0cd
TT
18281 for (i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
18282 {
3e43a32a
MS
18283 struct dwarf2_per_cu_data *per_cu
18284 = dwarf2_per_objfile->all_comp_units[i];
e254ef6a 18285 struct partial_symtab *psymtab = per_cu->v.psymtab;
9291a0cd 18286 gdb_byte val[8];
0a5429f6
DE
18287 struct psymtab_cu_index_map *map;
18288 void **slot;
9291a0cd 18289
95554aad
TT
18290 if (psymtab->user == NULL)
18291 recursively_write_psymbols (objfile, psymtab, symtab, psyms_seen, i);
9291a0cd 18292
0a5429f6
DE
18293 map = &psymtab_cu_index_map[i];
18294 map->psymtab = psymtab;
18295 map->cu_index = i;
18296 slot = htab_find_slot (cu_index_htab, map, INSERT);
18297 gdb_assert (slot != NULL);
18298 gdb_assert (*slot == NULL);
18299 *slot = map;
9291a0cd 18300
b64f50a1
JK
18301 store_unsigned_integer (val, 8, BFD_ENDIAN_LITTLE,
18302 per_cu->offset.sect_off);
9291a0cd 18303 obstack_grow (&cu_list, val, 8);
e254ef6a 18304 store_unsigned_integer (val, 8, BFD_ENDIAN_LITTLE, per_cu->length);
9291a0cd
TT
18305 obstack_grow (&cu_list, val, 8);
18306 }
18307
0a5429f6
DE
18308 /* Dump the address map. */
18309 write_address_map (objfile, &addr_obstack, cu_index_htab);
18310
1fd400ff
TT
18311 /* Write out the .debug_type entries, if any. */
18312 if (dwarf2_per_objfile->signatured_types)
18313 {
18314 struct signatured_type_index_data sig_data;
18315
18316 sig_data.objfile = objfile;
18317 sig_data.symtab = symtab;
18318 sig_data.types_list = &types_cu_list;
987d643c 18319 sig_data.psyms_seen = psyms_seen;
1fd400ff
TT
18320 sig_data.cu_index = dwarf2_per_objfile->n_comp_units;
18321 htab_traverse_noresize (dwarf2_per_objfile->signatured_types,
18322 write_one_signatured_type, &sig_data);
18323 }
18324
156942c7
DE
18325 /* Now that we've processed all symbols we can shrink their cu_indices
18326 lists. */
18327 uniquify_cu_indices (symtab);
18328
9291a0cd
TT
18329 obstack_init (&constant_pool);
18330 make_cleanup_obstack_free (&constant_pool);
18331 obstack_init (&symtab_obstack);
18332 make_cleanup_obstack_free (&symtab_obstack);
18333 write_hash_table (symtab, &symtab_obstack, &constant_pool);
18334
18335 obstack_init (&contents);
18336 make_cleanup_obstack_free (&contents);
1fd400ff 18337 size_of_contents = 6 * sizeof (offset_type);
9291a0cd
TT
18338 total_len = size_of_contents;
18339
18340 /* The version number. */
156942c7 18341 val = MAYBE_SWAP (7);
9291a0cd
TT
18342 obstack_grow (&contents, &val, sizeof (val));
18343
18344 /* The offset of the CU list from the start of the file. */
18345 val = MAYBE_SWAP (total_len);
18346 obstack_grow (&contents, &val, sizeof (val));
18347 total_len += obstack_object_size (&cu_list);
18348
1fd400ff
TT
18349 /* The offset of the types CU list from the start of the file. */
18350 val = MAYBE_SWAP (total_len);
18351 obstack_grow (&contents, &val, sizeof (val));
18352 total_len += obstack_object_size (&types_cu_list);
18353
9291a0cd
TT
18354 /* The offset of the address table from the start of the file. */
18355 val = MAYBE_SWAP (total_len);
18356 obstack_grow (&contents, &val, sizeof (val));
18357 total_len += obstack_object_size (&addr_obstack);
18358
18359 /* The offset of the symbol table from the start of the file. */
18360 val = MAYBE_SWAP (total_len);
18361 obstack_grow (&contents, &val, sizeof (val));
18362 total_len += obstack_object_size (&symtab_obstack);
18363
18364 /* The offset of the constant pool from the start of the file. */
18365 val = MAYBE_SWAP (total_len);
18366 obstack_grow (&contents, &val, sizeof (val));
18367 total_len += obstack_object_size (&constant_pool);
18368
18369 gdb_assert (obstack_object_size (&contents) == size_of_contents);
18370
18371 write_obstack (out_file, &contents);
18372 write_obstack (out_file, &cu_list);
1fd400ff 18373 write_obstack (out_file, &types_cu_list);
9291a0cd
TT
18374 write_obstack (out_file, &addr_obstack);
18375 write_obstack (out_file, &symtab_obstack);
18376 write_obstack (out_file, &constant_pool);
18377
18378 fclose (out_file);
18379
18380 /* We want to keep the file, so we set cleanup_filename to NULL
18381 here. See unlink_if_set. */
18382 cleanup_filename = NULL;
18383
18384 do_cleanups (cleanup);
18385}
18386
90476074
TT
18387/* Implementation of the `save gdb-index' command.
18388
18389 Note that the file format used by this command is documented in the
18390 GDB manual. Any changes here must be documented there. */
11570e71 18391
9291a0cd
TT
18392static void
18393save_gdb_index_command (char *arg, int from_tty)
18394{
18395 struct objfile *objfile;
18396
18397 if (!arg || !*arg)
96d19272 18398 error (_("usage: save gdb-index DIRECTORY"));
9291a0cd
TT
18399
18400 ALL_OBJFILES (objfile)
18401 {
18402 struct stat st;
18403
18404 /* If the objfile does not correspond to an actual file, skip it. */
18405 if (stat (objfile->name, &st) < 0)
18406 continue;
18407
18408 dwarf2_per_objfile = objfile_data (objfile, dwarf2_objfile_data_key);
18409 if (dwarf2_per_objfile)
18410 {
18411 volatile struct gdb_exception except;
18412
18413 TRY_CATCH (except, RETURN_MASK_ERROR)
18414 {
18415 write_psymtabs_to_index (objfile, arg);
18416 }
18417 if (except.reason < 0)
18418 exception_fprintf (gdb_stderr, except,
18419 _("Error while writing index for `%s': "),
18420 objfile->name);
18421 }
18422 }
dce234bc
PP
18423}
18424
9291a0cd
TT
18425\f
18426
9eae7c52
TT
18427int dwarf2_always_disassemble;
18428
18429static void
18430show_dwarf2_always_disassemble (struct ui_file *file, int from_tty,
18431 struct cmd_list_element *c, const char *value)
18432{
3e43a32a
MS
18433 fprintf_filtered (file,
18434 _("Whether to always disassemble "
18435 "DWARF expressions is %s.\n"),
9eae7c52
TT
18436 value);
18437}
18438
900e11f9
JK
18439static void
18440show_check_physname (struct ui_file *file, int from_tty,
18441 struct cmd_list_element *c, const char *value)
18442{
18443 fprintf_filtered (file,
18444 _("Whether to check \"physname\" is %s.\n"),
18445 value);
18446}
18447
6502dd73
DJ
18448void _initialize_dwarf2_read (void);
18449
18450void
18451_initialize_dwarf2_read (void)
18452{
96d19272
JK
18453 struct cmd_list_element *c;
18454
dce234bc 18455 dwarf2_objfile_data_key
c1bd65d0 18456 = register_objfile_data_with_cleanup (NULL, dwarf2_per_objfile_free);
ae038cb0 18457
1bedd215
AC
18458 add_prefix_cmd ("dwarf2", class_maintenance, set_dwarf2_cmd, _("\
18459Set DWARF 2 specific variables.\n\
18460Configure DWARF 2 variables such as the cache size"),
ae038cb0
DJ
18461 &set_dwarf2_cmdlist, "maintenance set dwarf2 ",
18462 0/*allow-unknown*/, &maintenance_set_cmdlist);
18463
1bedd215
AC
18464 add_prefix_cmd ("dwarf2", class_maintenance, show_dwarf2_cmd, _("\
18465Show DWARF 2 specific variables\n\
18466Show DWARF 2 variables such as the cache size"),
ae038cb0
DJ
18467 &show_dwarf2_cmdlist, "maintenance show dwarf2 ",
18468 0/*allow-unknown*/, &maintenance_show_cmdlist);
18469
18470 add_setshow_zinteger_cmd ("max-cache-age", class_obscure,
7915a72c
AC
18471 &dwarf2_max_cache_age, _("\
18472Set the upper bound on the age of cached dwarf2 compilation units."), _("\
18473Show the upper bound on the age of cached dwarf2 compilation units."), _("\
18474A higher limit means that cached compilation units will be stored\n\
18475in memory longer, and more total memory will be used. Zero disables\n\
18476caching, which can slow down startup."),
2c5b56ce 18477 NULL,
920d2a44 18478 show_dwarf2_max_cache_age,
2c5b56ce 18479 &set_dwarf2_cmdlist,
ae038cb0 18480 &show_dwarf2_cmdlist);
d97bc12b 18481
9eae7c52
TT
18482 add_setshow_boolean_cmd ("always-disassemble", class_obscure,
18483 &dwarf2_always_disassemble, _("\
18484Set whether `info address' always disassembles DWARF expressions."), _("\
18485Show whether `info address' always disassembles DWARF expressions."), _("\
18486When enabled, DWARF expressions are always printed in an assembly-like\n\
18487syntax. When disabled, expressions will be printed in a more\n\
18488conversational style, when possible."),
18489 NULL,
18490 show_dwarf2_always_disassemble,
18491 &set_dwarf2_cmdlist,
18492 &show_dwarf2_cmdlist);
18493
45cfd468
DE
18494 add_setshow_boolean_cmd ("dwarf2-read", no_class, &dwarf2_read_debug, _("\
18495Set debugging of the dwarf2 reader."), _("\
18496Show debugging of the dwarf2 reader."), _("\
18497When enabled, debugging messages are printed during dwarf2 reading\n\
18498and symtab expansion."),
18499 NULL,
18500 NULL,
18501 &setdebuglist, &showdebuglist);
18502
d97bc12b
DE
18503 add_setshow_zinteger_cmd ("dwarf2-die", no_class, &dwarf2_die_debug, _("\
18504Set debugging of the dwarf2 DIE reader."), _("\
18505Show debugging of the dwarf2 DIE reader."), _("\
18506When enabled (non-zero), DIEs are dumped after they are read in.\n\
18507The value is the maximum depth to print."),
18508 NULL,
18509 NULL,
18510 &setdebuglist, &showdebuglist);
9291a0cd 18511
900e11f9
JK
18512 add_setshow_boolean_cmd ("check-physname", no_class, &check_physname, _("\
18513Set cross-checking of \"physname\" code against demangler."), _("\
18514Show cross-checking of \"physname\" code against demangler."), _("\
18515When enabled, GDB's internal \"physname\" code is checked against\n\
18516the demangler."),
18517 NULL, show_check_physname,
18518 &setdebuglist, &showdebuglist);
18519
96d19272 18520 c = add_cmd ("gdb-index", class_files, save_gdb_index_command,
11570e71 18521 _("\
fc1a9d6e 18522Save a gdb-index file.\n\
11570e71 18523Usage: save gdb-index DIRECTORY"),
96d19272
JK
18524 &save_cmdlist);
18525 set_cmd_completer (c, filename_completer);
6502dd73 18526}
This page took 2.449756 seconds and 4 git commands to generate.